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