1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 ARM Ltd, All Rights Reserved.
6 #include <linux/compat.h>
7 #include <linux/errno.h>
8 #include <linux/prctl.h>
9 #include <linux/sched.h>
10 #include <linux/sched/task_stack.h>
11 #include <linux/thread_info.h>
13 #include <asm/compat.h>
14 #include <asm/cpufeature.h>
16 static void ssbd_ssbs_enable(struct task_struct *task)
18 u64 val = is_compat_thread(task_thread_info(task)) ?
19 PSR_AA32_SSBS_BIT : PSR_SSBS_BIT;
21 task_pt_regs(task)->pstate |= val;
24 static void ssbd_ssbs_disable(struct task_struct *task)
26 u64 val = is_compat_thread(task_thread_info(task)) ?
27 PSR_AA32_SSBS_BIT : PSR_SSBS_BIT;
29 task_pt_regs(task)->pstate &= ~val;
33 * prctl interface for SSBD
35 static int ssbd_prctl_set(struct task_struct *task, unsigned long ctrl)
37 int state = arm64_get_ssbd_state();
40 if (state == ARM64_SSBD_UNKNOWN)
43 /* Treat the unaffected/mitigated state separately */
44 if (state == ARM64_SSBD_MITIGATED) {
49 case PR_SPEC_FORCE_DISABLE:
55 * Things are a bit backward here: the arm64 internal API
56 * *enables the mitigation* when the userspace API *disables
57 * speculation*. So much fun.
61 /* If speculation is force disabled, enable is not allowed */
62 if (state == ARM64_SSBD_FORCE_ENABLE ||
63 task_spec_ssb_force_disable(task))
65 task_clear_spec_ssb_disable(task);
66 clear_tsk_thread_flag(task, TIF_SSBD);
67 ssbd_ssbs_enable(task);
70 if (state == ARM64_SSBD_FORCE_DISABLE)
72 task_set_spec_ssb_disable(task);
73 set_tsk_thread_flag(task, TIF_SSBD);
74 ssbd_ssbs_disable(task);
76 case PR_SPEC_FORCE_DISABLE:
77 if (state == ARM64_SSBD_FORCE_DISABLE)
79 task_set_spec_ssb_disable(task);
80 task_set_spec_ssb_force_disable(task);
81 set_tsk_thread_flag(task, TIF_SSBD);
82 ssbd_ssbs_disable(task);
91 int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
95 case PR_SPEC_STORE_BYPASS:
96 return ssbd_prctl_set(task, ctrl);
102 static int ssbd_prctl_get(struct task_struct *task)
104 switch (arm64_get_ssbd_state()) {
105 case ARM64_SSBD_UNKNOWN:
107 case ARM64_SSBD_FORCE_ENABLE:
108 return PR_SPEC_DISABLE;
109 case ARM64_SSBD_KERNEL:
110 if (task_spec_ssb_force_disable(task))
111 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
112 if (task_spec_ssb_disable(task))
113 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
114 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
115 case ARM64_SSBD_FORCE_DISABLE:
116 return PR_SPEC_ENABLE;
118 return PR_SPEC_NOT_AFFECTED;
122 int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
125 case PR_SPEC_STORE_BYPASS:
126 return ssbd_prctl_get(task);