x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / arch / s390 / kernel / guarded_storage.c
blob9ee794e14f33e72dd2e69032560ef4073fd542bb
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright IBM Corp. 2016
4 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
5 */
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"
15 void exit_thread_gs(void)
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();
24 static int gs_enable(void)
26 struct gs_cb *gs_cb;
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();
39 return 0;
42 static int gs_disable(void)
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();
51 return 0;
54 static int gs_set_bc_cb(struct gs_cb __user *u_gs_cb)
56 struct gs_cb *gs_cb;
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;
65 if (copy_from_user(gs_cb, u_gs_cb, sizeof(*gs_cb)))
66 return -EFAULT;
67 return 0;
70 static int gs_clear_bc_cb(void)
72 struct gs_cb *gs_cb;
74 gs_cb = current->thread.gs_bc_cb;
75 current->thread.gs_bc_cb = NULL;
76 kfree(gs_cb);
77 return 0;
80 void gs_load_bc_cb(struct pt_regs *regs)
82 struct gs_cb *gs_cb;
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;
94 preempt_enable();
97 static int gs_broadcast(void)
99 struct task_struct *sibling;
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);
108 read_unlock(&tasklist_lock);
109 return 0;
112 SYSCALL_DEFINE2(s390_guarded_storage, int, command,
113 struct gs_cb __user *, gs_cb)
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;