guarded_storage.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2016
  4. * Author(s): Martin Schwidefsky <[email protected]>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/syscalls.h>
  8. #include <linux/signal.h>
  9. #include <linux/mm.h>
  10. #include <linux/slab.h>
  11. #include <asm/guarded_storage.h>
  12. #include "entry.h"
  13. void guarded_storage_release(struct task_struct *tsk)
  14. {
  15. kfree(tsk->thread.gs_cb);
  16. kfree(tsk->thread.gs_bc_cb);
  17. }
  18. static int gs_enable(void)
  19. {
  20. struct gs_cb *gs_cb;
  21. if (!current->thread.gs_cb) {
  22. gs_cb = kzalloc(sizeof(*gs_cb), GFP_KERNEL);
  23. if (!gs_cb)
  24. return -ENOMEM;
  25. gs_cb->gsd = 25;
  26. preempt_disable();
  27. __ctl_set_bit(2, 4);
  28. load_gs_cb(gs_cb);
  29. current->thread.gs_cb = gs_cb;
  30. preempt_enable();
  31. }
  32. return 0;
  33. }
  34. static int gs_disable(void)
  35. {
  36. if (current->thread.gs_cb) {
  37. preempt_disable();
  38. kfree(current->thread.gs_cb);
  39. current->thread.gs_cb = NULL;
  40. __ctl_clear_bit(2, 4);
  41. preempt_enable();
  42. }
  43. return 0;
  44. }
  45. static int gs_set_bc_cb(struct gs_cb __user *u_gs_cb)
  46. {
  47. struct gs_cb *gs_cb;
  48. gs_cb = current->thread.gs_bc_cb;
  49. if (!gs_cb) {
  50. gs_cb = kzalloc(sizeof(*gs_cb), GFP_KERNEL);
  51. if (!gs_cb)
  52. return -ENOMEM;
  53. current->thread.gs_bc_cb = gs_cb;
  54. }
  55. if (copy_from_user(gs_cb, u_gs_cb, sizeof(*gs_cb)))
  56. return -EFAULT;
  57. return 0;
  58. }
  59. static int gs_clear_bc_cb(void)
  60. {
  61. struct gs_cb *gs_cb;
  62. gs_cb = current->thread.gs_bc_cb;
  63. current->thread.gs_bc_cb = NULL;
  64. kfree(gs_cb);
  65. return 0;
  66. }
  67. void gs_load_bc_cb(struct pt_regs *regs)
  68. {
  69. struct gs_cb *gs_cb;
  70. preempt_disable();
  71. clear_thread_flag(TIF_GUARDED_STORAGE);
  72. gs_cb = current->thread.gs_bc_cb;
  73. if (gs_cb) {
  74. kfree(current->thread.gs_cb);
  75. current->thread.gs_bc_cb = NULL;
  76. __ctl_set_bit(2, 4);
  77. load_gs_cb(gs_cb);
  78. current->thread.gs_cb = gs_cb;
  79. }
  80. preempt_enable();
  81. }
  82. static int gs_broadcast(void)
  83. {
  84. struct task_struct *sibling;
  85. read_lock(&tasklist_lock);
  86. for_each_thread(current, sibling) {
  87. if (!sibling->thread.gs_bc_cb)
  88. continue;
  89. if (test_and_set_tsk_thread_flag(sibling, TIF_GUARDED_STORAGE))
  90. kick_process(sibling);
  91. }
  92. read_unlock(&tasklist_lock);
  93. return 0;
  94. }
  95. SYSCALL_DEFINE2(s390_guarded_storage, int, command,
  96. struct gs_cb __user *, gs_cb)
  97. {
  98. if (!MACHINE_HAS_GS)
  99. return -EOPNOTSUPP;
  100. switch (command) {
  101. case GS_ENABLE:
  102. return gs_enable();
  103. case GS_DISABLE:
  104. return gs_disable();
  105. case GS_SET_BC_CB:
  106. return gs_set_bc_cb(gs_cb);
  107. case GS_CLEAR_BC_CB:
  108. return gs_clear_bc_cb();
  109. case GS_BROADCAST:
  110. return gs_broadcast();
  111. default:
  112. return -EINVAL;
  113. }
  114. }