runtime_instr.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2012
  4. * Author(s): Jan Glauber <jang@linux.vnet.ibm.com>
  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 <linux/init.h>
  12. #include <linux/errno.h>
  13. #include <linux/kernel_stat.h>
  14. #include <linux/sched/task_stack.h>
  15. #include <asm/runtime_instr.h>
  16. #include <asm/cpu_mf.h>
  17. #include <asm/irq.h>
  18. #include "entry.h"
  19. /* empty control block to disable RI by loading it */
  20. struct runtime_instr_cb runtime_instr_empty_cb;
  21. void runtime_instr_release(struct task_struct *tsk)
  22. {
  23. kfree(tsk->thread.ri_cb);
  24. }
  25. static void disable_runtime_instr(void)
  26. {
  27. struct task_struct *task = current;
  28. struct pt_regs *regs;
  29. if (!task->thread.ri_cb)
  30. return;
  31. regs = task_pt_regs(task);
  32. preempt_disable();
  33. load_runtime_instr_cb(&runtime_instr_empty_cb);
  34. kfree(task->thread.ri_cb);
  35. task->thread.ri_cb = NULL;
  36. preempt_enable();
  37. /*
  38. * Make sure the RI bit is deleted from the PSW. If the user did not
  39. * switch off RI before the system call the process will get a
  40. * specification exception otherwise.
  41. */
  42. regs->psw.mask &= ~PSW_MASK_RI;
  43. }
  44. static void init_runtime_instr_cb(struct runtime_instr_cb *cb)
  45. {
  46. cb->rla = 0xfff;
  47. cb->s = 1;
  48. cb->k = 1;
  49. cb->ps = 1;
  50. cb->pc = 1;
  51. cb->key = PAGE_DEFAULT_KEY;
  52. cb->v = 1;
  53. }
  54. /*
  55. * The signum argument is unused. In older kernels it was used to
  56. * specify a real-time signal. For backwards compatibility user space
  57. * should pass a valid real-time signal number (the signum argument
  58. * was checked in older kernels).
  59. */
  60. SYSCALL_DEFINE2(s390_runtime_instr, int, command, int, signum)
  61. {
  62. struct runtime_instr_cb *cb;
  63. if (!test_facility(64))
  64. return -EOPNOTSUPP;
  65. if (command == S390_RUNTIME_INSTR_STOP) {
  66. disable_runtime_instr();
  67. return 0;
  68. }
  69. if (command != S390_RUNTIME_INSTR_START)
  70. return -EINVAL;
  71. if (!current->thread.ri_cb) {
  72. cb = kzalloc(sizeof(*cb), GFP_KERNEL);
  73. if (!cb)
  74. return -ENOMEM;
  75. } else {
  76. cb = current->thread.ri_cb;
  77. memset(cb, 0, sizeof(*cb));
  78. }
  79. init_runtime_instr_cb(cb);
  80. /* now load the control block to make it available */
  81. preempt_disable();
  82. current->thread.ri_cb = cb;
  83. load_runtime_instr_cb(cb);
  84. preempt_enable();
  85. return 0;
  86. }