• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright IBM Corp. 2016
4  * Author(s): Martin Schwidefsky <schwidefsky@de.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 <asm/guarded_storage.h>
13 #include "entry.h"
14 
exit_thread_gs(void)15 void exit_thread_gs(void)
16 {
17 	preempt_disable();
18 	kfree(current->thread.gs_cb);
19 	kfree(current->thread.gs_bc_cb);
20 	current->thread.gs_cb = current->thread.gs_bc_cb = NULL;
21 	preempt_enable();
22 }
23 
gs_enable(void)24 static int gs_enable(void)
25 {
26 	struct gs_cb *gs_cb;
27 
28 	if (!current->thread.gs_cb) {
29 		gs_cb = kzalloc(sizeof(*gs_cb), GFP_KERNEL);
30 		if (!gs_cb)
31 			return -ENOMEM;
32 		gs_cb->gsd = 25;
33 		preempt_disable();
34 		__ctl_set_bit(2, 4);
35 		load_gs_cb(gs_cb);
36 		current->thread.gs_cb = gs_cb;
37 		preempt_enable();
38 	}
39 	return 0;
40 }
41 
gs_disable(void)42 static int gs_disable(void)
43 {
44 	if (current->thread.gs_cb) {
45 		preempt_disable();
46 		kfree(current->thread.gs_cb);
47 		current->thread.gs_cb = NULL;
48 		__ctl_clear_bit(2, 4);
49 		preempt_enable();
50 	}
51 	return 0;
52 }
53 
gs_set_bc_cb(struct gs_cb __user * u_gs_cb)54 static int gs_set_bc_cb(struct gs_cb __user *u_gs_cb)
55 {
56 	struct gs_cb *gs_cb;
57 
58 	gs_cb = current->thread.gs_bc_cb;
59 	if (!gs_cb) {
60 		gs_cb = kzalloc(sizeof(*gs_cb), GFP_KERNEL);
61 		if (!gs_cb)
62 			return -ENOMEM;
63 		current->thread.gs_bc_cb = gs_cb;
64 	}
65 	if (copy_from_user(gs_cb, u_gs_cb, sizeof(*gs_cb)))
66 		return -EFAULT;
67 	return 0;
68 }
69 
gs_clear_bc_cb(void)70 static int gs_clear_bc_cb(void)
71 {
72 	struct gs_cb *gs_cb;
73 
74 	gs_cb = current->thread.gs_bc_cb;
75 	current->thread.gs_bc_cb = NULL;
76 	kfree(gs_cb);
77 	return 0;
78 }
79 
gs_load_bc_cb(struct pt_regs * regs)80 void gs_load_bc_cb(struct pt_regs *regs)
81 {
82 	struct gs_cb *gs_cb;
83 
84 	preempt_disable();
85 	clear_thread_flag(TIF_GUARDED_STORAGE);
86 	gs_cb = current->thread.gs_bc_cb;
87 	if (gs_cb) {
88 		kfree(current->thread.gs_cb);
89 		current->thread.gs_bc_cb = NULL;
90 		__ctl_set_bit(2, 4);
91 		load_gs_cb(gs_cb);
92 		current->thread.gs_cb = gs_cb;
93 	}
94 	preempt_enable();
95 }
96 
gs_broadcast(void)97 static int gs_broadcast(void)
98 {
99 	struct task_struct *sibling;
100 
101 	read_lock(&tasklist_lock);
102 	for_each_thread(current, sibling) {
103 		if (!sibling->thread.gs_bc_cb)
104 			continue;
105 		if (test_and_set_tsk_thread_flag(sibling, TIF_GUARDED_STORAGE))
106 			kick_process(sibling);
107 	}
108 	read_unlock(&tasklist_lock);
109 	return 0;
110 }
111 
SYSCALL_DEFINE2(s390_guarded_storage,int,command,struct gs_cb __user *,gs_cb)112 SYSCALL_DEFINE2(s390_guarded_storage, int, command,
113 		struct gs_cb __user *, gs_cb)
114 {
115 	if (!MACHINE_HAS_GS)
116 		return -EOPNOTSUPP;
117 	switch (command) {
118 	case GS_ENABLE:
119 		return gs_enable();
120 	case GS_DISABLE:
121 		return gs_disable();
122 	case GS_SET_BC_CB:
123 		return gs_set_bc_cb(gs_cb);
124 	case GS_CLEAR_BC_CB:
125 		return gs_clear_bc_cb();
126 	case GS_BROADCAST:
127 		return gs_broadcast();
128 	default:
129 		return -EINVAL;
130 	}
131 }
132