runtime_instr.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright IBM Corp. 2012
  3. * Author(s): Jan Glauber <jang@linux.vnet.ibm.com>
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/syscalls.h>
  7. #include <linux/signal.h>
  8. #include <linux/mm.h>
  9. #include <linux/slab.h>
  10. #include <linux/init.h>
  11. #include <linux/errno.h>
  12. #include <linux/kernel_stat.h>
  13. #include <linux/sched/task_stack.h>
  14. #include <asm/runtime_instr.h>
  15. #include <asm/cpu_mf.h>
  16. #include <asm/irq.h>
  17. /* empty control block to disable RI by loading it */
  18. struct runtime_instr_cb runtime_instr_empty_cb;
  19. static void disable_runtime_instr(void)
  20. {
  21. struct pt_regs *regs = task_pt_regs(current);
  22. load_runtime_instr_cb(&runtime_instr_empty_cb);
  23. /*
  24. * Make sure the RI bit is deleted from the PSW. If the user did not
  25. * switch off RI before the system call the process will get a
  26. * specification exception otherwise.
  27. */
  28. regs->psw.mask &= ~PSW_MASK_RI;
  29. }
  30. static void init_runtime_instr_cb(struct runtime_instr_cb *cb)
  31. {
  32. cb->buf_limit = 0xfff;
  33. cb->pstate = 1;
  34. cb->pstate_set_buf = 1;
  35. cb->pstate_sample = 1;
  36. cb->pstate_collect = 1;
  37. cb->key = PAGE_DEFAULT_KEY;
  38. cb->valid = 1;
  39. }
  40. void exit_thread_runtime_instr(void)
  41. {
  42. struct task_struct *task = current;
  43. if (!task->thread.ri_cb)
  44. return;
  45. disable_runtime_instr();
  46. kfree(task->thread.ri_cb);
  47. task->thread.ri_cb = NULL;
  48. }
  49. SYSCALL_DEFINE1(s390_runtime_instr, int, command)
  50. {
  51. struct runtime_instr_cb *cb;
  52. if (!test_facility(64))
  53. return -EOPNOTSUPP;
  54. if (command == S390_RUNTIME_INSTR_STOP) {
  55. preempt_disable();
  56. exit_thread_runtime_instr();
  57. preempt_enable();
  58. return 0;
  59. }
  60. if (command != S390_RUNTIME_INSTR_START)
  61. return -EINVAL;
  62. if (!current->thread.ri_cb) {
  63. cb = kzalloc(sizeof(*cb), GFP_KERNEL);
  64. if (!cb)
  65. return -ENOMEM;
  66. } else {
  67. cb = current->thread.ri_cb;
  68. memset(cb, 0, sizeof(*cb));
  69. }
  70. init_runtime_instr_cb(cb);
  71. /* now load the control block to make it available */
  72. preempt_disable();
  73. current->thread.ri_cb = cb;
  74. load_runtime_instr_cb(cb);
  75. preempt_enable();
  76. return 0;
  77. }