sched/headers: Prepare for new header dependencies before moving code to <linux/sched...
[linux-block.git] / arch / s390 / kernel / runtime_instr.c
1 /*
2  * Copyright IBM Corp. 2012
3  * Author(s): Jan Glauber <jang@linux.vnet.ibm.com>
4  */
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
16 #include <asm/runtime_instr.h>
17 #include <asm/cpu_mf.h>
18 #include <asm/irq.h>
19
20 /* empty control block to disable RI by loading it */
21 struct runtime_instr_cb runtime_instr_empty_cb;
22
23 static void disable_runtime_instr(void)
24 {
25         struct pt_regs *regs = task_pt_regs(current);
26
27         load_runtime_instr_cb(&runtime_instr_empty_cb);
28
29         /*
30          * Make sure the RI bit is deleted from the PSW. If the user did not
31          * switch off RI before the system call the process will get a
32          * specification exception otherwise.
33          */
34         regs->psw.mask &= ~PSW_MASK_RI;
35 }
36
37 static void init_runtime_instr_cb(struct runtime_instr_cb *cb)
38 {
39         cb->buf_limit = 0xfff;
40         cb->pstate = 1;
41         cb->pstate_set_buf = 1;
42         cb->pstate_sample = 1;
43         cb->pstate_collect = 1;
44         cb->key = PAGE_DEFAULT_KEY;
45         cb->valid = 1;
46 }
47
48 void exit_thread_runtime_instr(void)
49 {
50         struct task_struct *task = current;
51
52         if (!task->thread.ri_cb)
53                 return;
54         disable_runtime_instr();
55         kfree(task->thread.ri_cb);
56         task->thread.ri_cb = NULL;
57 }
58
59 SYSCALL_DEFINE1(s390_runtime_instr, int, command)
60 {
61         struct runtime_instr_cb *cb;
62
63         if (!test_facility(64))
64                 return -EOPNOTSUPP;
65
66         if (command == S390_RUNTIME_INSTR_STOP) {
67                 preempt_disable();
68                 exit_thread_runtime_instr();
69                 preempt_enable();
70                 return 0;
71         }
72
73         if (command != S390_RUNTIME_INSTR_START)
74                 return -EINVAL;
75
76         if (!current->thread.ri_cb) {
77                 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
78                 if (!cb)
79                         return -ENOMEM;
80         } else {
81                 cb = current->thread.ri_cb;
82                 memset(cb, 0, sizeof(*cb));
83         }
84
85         init_runtime_instr_cb(cb);
86
87         /* now load the control block to make it available */
88         preempt_disable();
89         current->thread.ri_cb = cb;
90         load_runtime_instr_cb(cb);
91         preempt_enable();
92         return 0;
93 }