^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright IBM Corp. 2012
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Author(s): Jan Glauber <jang@linux.vnet.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kernel_stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/sched/task_stack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/runtime_instr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/cpu_mf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "entry.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /* empty control block to disable RI by loading it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct runtime_instr_cb runtime_instr_empty_cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) void runtime_instr_release(struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) kfree(tsk->thread.ri_cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static void disable_runtime_instr(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct task_struct *task = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct pt_regs *regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (!task->thread.ri_cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) regs = task_pt_regs(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) load_runtime_instr_cb(&runtime_instr_empty_cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) kfree(task->thread.ri_cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) task->thread.ri_cb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * Make sure the RI bit is deleted from the PSW. If the user did not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * switch off RI before the system call the process will get a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * specification exception otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) regs->psw.mask &= ~PSW_MASK_RI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static void init_runtime_instr_cb(struct runtime_instr_cb *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) cb->rla = 0xfff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) cb->s = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) cb->k = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) cb->ps = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) cb->pc = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) cb->key = PAGE_DEFAULT_KEY >> 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) cb->v = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * The signum argument is unused. In older kernels it was used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * specify a real-time signal. For backwards compatibility user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * should pass a valid real-time signal number (the signum argument
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * was checked in older kernels).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) SYSCALL_DEFINE2(s390_runtime_instr, int, command, int, signum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct runtime_instr_cb *cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (!test_facility(64))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (command == S390_RUNTIME_INSTR_STOP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) disable_runtime_instr();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (command != S390_RUNTIME_INSTR_START)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (!current->thread.ri_cb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) cb = kzalloc(sizeof(*cb), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (!cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) cb = current->thread.ri_cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) memset(cb, 0, sizeof(*cb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) init_runtime_instr_cb(cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* now load the control block to make it available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) current->thread.ri_cb = cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) load_runtime_instr_cb(cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }