runtime_instr.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 <asm/runtime_instr.h>
  14. #include <asm/cpu_mf.h>
  15. #include <asm/irq.h>
  16. /* empty control block to disable RI by loading it */
  17. struct runtime_instr_cb runtime_instr_empty_cb;
  18. static int runtime_instr_avail(void)
  19. {
  20. return test_facility(64);
  21. }
  22. static void disable_runtime_instr(void)
  23. {
  24. struct pt_regs *regs = task_pt_regs(current);
  25. load_runtime_instr_cb(&runtime_instr_empty_cb);
  26. /*
  27. * Make sure the RI bit is deleted from the PSW. If the user did not
  28. * switch off RI before the system call the process will get a
  29. * specification exception otherwise.
  30. */
  31. regs->psw.mask &= ~PSW_MASK_RI;
  32. }
  33. static void init_runtime_instr_cb(struct runtime_instr_cb *cb)
  34. {
  35. cb->buf_limit = 0xfff;
  36. if (s390_user_mode == HOME_SPACE_MODE)
  37. cb->home_space = 1;
  38. cb->int_requested = 1;
  39. cb->pstate = 1;
  40. cb->pstate_set_buf = 1;
  41. cb->pstate_sample = 1;
  42. cb->pstate_collect = 1;
  43. cb->key = PAGE_DEFAULT_KEY;
  44. cb->valid = 1;
  45. }
  46. void exit_thread_runtime_instr(void)
  47. {
  48. struct task_struct *task = current;
  49. if (!task->thread.ri_cb)
  50. return;
  51. disable_runtime_instr();
  52. kfree(task->thread.ri_cb);
  53. task->thread.ri_signum = 0;
  54. task->thread.ri_cb = NULL;
  55. }
  56. static void runtime_instr_int_handler(struct ext_code ext_code,
  57. unsigned int param32, unsigned long param64)
  58. {
  59. struct siginfo info;
  60. if (!(param32 & CPU_MF_INT_RI_MASK))
  61. return;
  62. inc_irq_stat(IRQEXT_CMR);
  63. if (!current->thread.ri_cb)
  64. return;
  65. if (current->thread.ri_signum < SIGRTMIN ||
  66. current->thread.ri_signum > SIGRTMAX) {
  67. WARN_ON_ONCE(1);
  68. return;
  69. }
  70. memset(&info, 0, sizeof(info));
  71. info.si_signo = current->thread.ri_signum;
  72. info.si_code = SI_QUEUE;
  73. if (param32 & CPU_MF_INT_RI_BUF_FULL)
  74. info.si_int = ENOBUFS;
  75. else if (param32 & CPU_MF_INT_RI_HALTED)
  76. info.si_int = ECANCELED;
  77. else
  78. return; /* unknown reason */
  79. send_sig_info(current->thread.ri_signum, &info, current);
  80. }
  81. SYSCALL_DEFINE2(s390_runtime_instr, int, command, int, signum)
  82. {
  83. struct runtime_instr_cb *cb;
  84. if (!runtime_instr_avail())
  85. return -EOPNOTSUPP;
  86. if (command == S390_RUNTIME_INSTR_STOP) {
  87. preempt_disable();
  88. exit_thread_runtime_instr();
  89. preempt_enable();
  90. return 0;
  91. }
  92. if (command != S390_RUNTIME_INSTR_START ||
  93. (signum < SIGRTMIN || signum > SIGRTMAX))
  94. return -EINVAL;
  95. if (!current->thread.ri_cb) {
  96. cb = kzalloc(sizeof(*cb), GFP_KERNEL);
  97. if (!cb)
  98. return -ENOMEM;
  99. } else {
  100. cb = current->thread.ri_cb;
  101. memset(cb, 0, sizeof(*cb));
  102. }
  103. init_runtime_instr_cb(cb);
  104. current->thread.ri_signum = signum;
  105. /* now load the control block to make it available */
  106. preempt_disable();
  107. current->thread.ri_cb = cb;
  108. load_runtime_instr_cb(cb);
  109. preempt_enable();
  110. return 0;
  111. }
  112. static int __init runtime_instr_init(void)
  113. {
  114. int rc;
  115. if (!runtime_instr_avail())
  116. return 0;
  117. measurement_alert_subclass_register();
  118. rc = register_external_interrupt(0x1407, runtime_instr_int_handler);
  119. if (rc)
  120. measurement_alert_subclass_unregister();
  121. else
  122. pr_info("Runtime instrumentation facility initialized\n");
  123. return rc;
  124. }
  125. device_initcall(runtime_instr_init);