xref: /linux/arch/s390/kernel/runtime_instr.c (revision e16c5dd5157efeddaad7492b920192fea0e7e4ec)
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 void runtime_instr_release(struct task_struct *tsk)
24 {
25 	kfree(tsk->thread.ri_cb);
26 }
27 
28 static void disable_runtime_instr(void)
29 {
30 	struct task_struct *task = current;
31 	struct pt_regs *regs;
32 
33 	if (!task->thread.ri_cb)
34 		return;
35 	regs = task_pt_regs(task);
36 	preempt_disable();
37 	load_runtime_instr_cb(&runtime_instr_empty_cb);
38 	kfree(task->thread.ri_cb);
39 	task->thread.ri_cb = NULL;
40 	preempt_enable();
41 
42 	/*
43 	 * Make sure the RI bit is deleted from the PSW. If the user did not
44 	 * switch off RI before the system call the process will get a
45 	 * specification exception otherwise.
46 	 */
47 	regs->psw.mask &= ~PSW_MASK_RI;
48 }
49 
50 static void init_runtime_instr_cb(struct runtime_instr_cb *cb)
51 {
52 	cb->rla = 0xfff;
53 	cb->s = 1;
54 	cb->k = 1;
55 	cb->ps = 1;
56 	cb->pc = 1;
57 	cb->key = PAGE_DEFAULT_KEY;
58 	cb->v = 1;
59 }
60 
61 SYSCALL_DEFINE1(s390_runtime_instr, int, command)
62 {
63 	struct runtime_instr_cb *cb;
64 
65 	if (!test_facility(64))
66 		return -EOPNOTSUPP;
67 
68 	if (command == S390_RUNTIME_INSTR_STOP) {
69 		disable_runtime_instr();
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 }
94