2 * Copyright (C) 2014 ARM Limited
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
10 #include <linux/init.h>
11 #include <linux/list.h>
12 #include <linux/perf_event.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 #include <linux/sysctl.h>
18 #include <asm/opcodes.h>
19 #include <asm/system_misc.h>
20 #include <asm/traps.h>
21 #include <asm/uaccess.h>
24 * The runtime support for deprecated instruction support can be in one of
25 * following three states -
28 * 1 = emulate (software emulation)
29 * 2 = hw (supported in hardware)
31 enum insn_emulation_mode
{
37 enum legacy_insn_status
{
42 struct insn_emulation_ops
{
44 enum legacy_insn_status status
;
45 struct undef_hook
*hooks
;
46 int (*set_hw_mode
)(bool enable
);
49 struct insn_emulation
{
50 struct list_head node
;
51 struct insn_emulation_ops
*ops
;
57 static LIST_HEAD(insn_emulation
);
58 static int nr_insn_emulated
;
59 static DEFINE_RAW_SPINLOCK(insn_emulation_lock
);
61 static void register_emulation_hooks(struct insn_emulation_ops
*ops
)
63 struct undef_hook
*hook
;
67 for (hook
= ops
->hooks
; hook
->instr_mask
; hook
++)
68 register_undef_hook(hook
);
70 pr_notice("Registered %s emulation handler\n", ops
->name
);
73 static void remove_emulation_hooks(struct insn_emulation_ops
*ops
)
75 struct undef_hook
*hook
;
79 for (hook
= ops
->hooks
; hook
->instr_mask
; hook
++)
80 unregister_undef_hook(hook
);
82 pr_notice("Removed %s emulation handler\n", ops
->name
);
85 static int update_insn_emulation_mode(struct insn_emulation
*insn
,
86 enum insn_emulation_mode prev
)
91 case INSN_UNDEF
: /* Nothing to be done */
94 remove_emulation_hooks(insn
->ops
);
97 if (insn
->ops
->set_hw_mode
) {
98 insn
->ops
->set_hw_mode(false);
99 pr_notice("Disabled %s support\n", insn
->ops
->name
);
104 switch (insn
->current_mode
) {
108 register_emulation_hooks(insn
->ops
);
111 if (insn
->ops
->set_hw_mode
&& insn
->ops
->set_hw_mode(true))
112 pr_notice("Enabled %s support\n", insn
->ops
->name
);
121 static void register_insn_emulation(struct insn_emulation_ops
*ops
)
124 struct insn_emulation
*insn
;
126 insn
= kzalloc(sizeof(*insn
), GFP_KERNEL
);
128 insn
->min
= INSN_UNDEF
;
130 switch (ops
->status
) {
131 case INSN_DEPRECATED
:
132 insn
->current_mode
= INSN_EMULATE
;
136 insn
->current_mode
= INSN_UNDEF
;
137 insn
->max
= INSN_EMULATE
;
141 raw_spin_lock_irqsave(&insn_emulation_lock
, flags
);
142 list_add(&insn
->node
, &insn_emulation
);
144 raw_spin_unlock_irqrestore(&insn_emulation_lock
, flags
);
146 /* Register any handlers if required */
147 update_insn_emulation_mode(insn
, INSN_UNDEF
);
150 static int emulation_proc_handler(struct ctl_table
*table
, int write
,
151 void __user
*buffer
, size_t *lenp
,
155 struct insn_emulation
*insn
= (struct insn_emulation
*) table
->data
;
156 enum insn_emulation_mode prev_mode
= insn
->current_mode
;
158 table
->data
= &insn
->current_mode
;
159 ret
= proc_dointvec_minmax(table
, write
, buffer
, lenp
, ppos
);
161 if (ret
|| !write
|| prev_mode
== insn
->current_mode
)
164 ret
= update_insn_emulation_mode(insn
, prev_mode
);
166 /* Mode change failed, revert to previous mode. */
167 insn
->current_mode
= prev_mode
;
168 update_insn_emulation_mode(insn
, INSN_UNDEF
);
175 static struct ctl_table ctl_abi
[] = {
183 static void register_insn_emulation_sysctl(struct ctl_table
*table
)
187 struct insn_emulation
*insn
;
188 struct ctl_table
*insns_sysctl
, *sysctl
;
190 insns_sysctl
= kzalloc(sizeof(*sysctl
) * (nr_insn_emulated
+ 1),
193 raw_spin_lock_irqsave(&insn_emulation_lock
, flags
);
194 list_for_each_entry(insn
, &insn_emulation
, node
) {
195 sysctl
= &insns_sysctl
[i
];
198 sysctl
->maxlen
= sizeof(int);
200 sysctl
->procname
= insn
->ops
->name
;
202 sysctl
->extra1
= &insn
->min
;
203 sysctl
->extra2
= &insn
->max
;
204 sysctl
->proc_handler
= emulation_proc_handler
;
207 raw_spin_unlock_irqrestore(&insn_emulation_lock
, flags
);
209 table
->child
= insns_sysctl
;
210 register_sysctl_table(table
);
214 * Implement emulation of the SWP/SWPB instructions using load-exclusive and
217 * Syntax of SWP{B} instruction: SWP{B}<c> <Rt>, <Rt2>, [<Rn>]
218 * Where: Rt = destination
224 * Error-checking SWP macros implemented using ldxr{b}/stxr{b}
226 #define __user_swpX_asm(data, addr, res, temp, B) \
227 __asm__ __volatile__( \
229 "0: ldxr"B" %w1, [%3]\n" \
230 "1: stxr"B" %w0, %w2, [%3]\n" \
234 " .pushsection .fixup,\"ax\"\n" \
236 "3: mov %w0, %w5\n" \
239 " .pushsection __ex_table,\"a\"\n" \
244 : "=&r" (res), "+r" (data), "=&r" (temp) \
245 : "r" (addr), "i" (-EAGAIN), "i" (-EFAULT) \
248 #define __user_swp_asm(data, addr, res, temp) \
249 __user_swpX_asm(data, addr, res, temp, "")
250 #define __user_swpb_asm(data, addr, res, temp) \
251 __user_swpX_asm(data, addr, res, temp, "b")
254 * Bit 22 of the instruction encoding distinguishes between
255 * the SWP and SWPB variants (bit set means SWPB).
257 #define TYPE_SWPB (1 << 22)
260 * Set up process info to signal segmentation fault - called on access error.
262 static void set_segfault(struct pt_regs
*regs
, unsigned long addr
)
266 down_read(¤t
->mm
->mmap_sem
);
267 if (find_vma(current
->mm
, addr
) == NULL
)
268 info
.si_code
= SEGV_MAPERR
;
270 info
.si_code
= SEGV_ACCERR
;
271 up_read(¤t
->mm
->mmap_sem
);
273 info
.si_signo
= SIGSEGV
;
275 info
.si_addr
= (void *) instruction_pointer(regs
);
277 pr_debug("SWP{B} emulation: access caused memory abort!\n");
278 arm64_notify_die("Illegal memory access", regs
, &info
, 0);
281 static int emulate_swpX(unsigned int address
, unsigned int *data
,
284 unsigned int res
= 0;
286 if ((type
!= TYPE_SWPB
) && (address
& 0x3)) {
287 /* SWP to unaligned address not permitted */
288 pr_debug("SWP instruction on unaligned pointer!\n");
295 if (type
== TYPE_SWPB
)
296 __user_swpb_asm(*data
, address
, res
, temp
);
298 __user_swp_asm(*data
, address
, res
, temp
);
300 if (likely(res
!= -EAGAIN
) || signal_pending(current
))
310 * swp_handler logs the id of calling process, dissects the instruction, sanity
311 * checks the memory location, calls emulate_swpX for the actual operation and
312 * deals with fixup/error handling before returning
314 static int swp_handler(struct pt_regs
*regs
, u32 instr
)
316 u32 destreg
, data
, type
, address
= 0;
317 int rn
, rt2
, res
= 0;
319 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS
, 1, regs
, regs
->pc
);
321 type
= instr
& TYPE_SWPB
;
323 switch (arm_check_condition(instr
, regs
->pstate
)) {
324 case ARM_OPCODE_CONDTEST_PASS
:
326 case ARM_OPCODE_CONDTEST_FAIL
:
327 /* Condition failed - return to next instruction */
329 case ARM_OPCODE_CONDTEST_UNCOND
:
330 /* If unconditional encoding - not a SWP, undef */
336 rn
= aarch32_insn_extract_reg_num(instr
, A32_RN_OFFSET
);
337 rt2
= aarch32_insn_extract_reg_num(instr
, A32_RT2_OFFSET
);
339 address
= (u32
)regs
->user_regs
.regs
[rn
];
340 data
= (u32
)regs
->user_regs
.regs
[rt2
];
341 destreg
= aarch32_insn_extract_reg_num(instr
, A32_RT_OFFSET
);
343 pr_debug("addr in r%d->0x%08x, dest is r%d, source in r%d->0x%08x)\n",
344 rn
, address
, destreg
,
345 aarch32_insn_extract_reg_num(instr
, A32_RT2_OFFSET
), data
);
347 /* Check access in reasonable access range for both SWP and SWPB */
348 if (!access_ok(VERIFY_WRITE
, (address
& ~3), 4)) {
349 pr_debug("SWP{B} emulation: access to 0x%08x not allowed!\n",
354 res
= emulate_swpX(address
, &data
, type
);
358 regs
->user_regs
.regs
[destreg
] = data
;
361 pr_warn_ratelimited("\"%s\" (%ld) uses obsolete SWP{B} instruction at 0x%llx\n",
362 current
->comm
, (unsigned long)current
->pid
, regs
->pc
);
368 set_segfault(regs
, address
);
374 * Only emulate SWP/SWPB executed in ARM state/User mode.
375 * The kernel must be SWP free and SWP{B} does not exist in Thumb.
377 static struct undef_hook swp_hooks
[] = {
379 .instr_mask
= 0x0fb00ff0,
380 .instr_val
= 0x01000090,
381 .pstate_mask
= COMPAT_PSR_MODE_MASK
,
382 .pstate_val
= COMPAT_PSR_MODE_USR
,
388 static struct insn_emulation_ops swp_ops
= {
390 .status
= INSN_OBSOLETE
,
395 static int cp15barrier_handler(struct pt_regs
*regs
, u32 instr
)
397 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS
, 1, regs
, regs
->pc
);
399 switch (arm_check_condition(instr
, regs
->pstate
)) {
400 case ARM_OPCODE_CONDTEST_PASS
:
402 case ARM_OPCODE_CONDTEST_FAIL
:
403 /* Condition failed - return to next instruction */
405 case ARM_OPCODE_CONDTEST_UNCOND
:
406 /* If unconditional encoding - not a barrier instruction */
412 switch (aarch32_insn_mcr_extract_crm(instr
)) {
415 * dmb - mcr p15, 0, Rt, c7, c10, 5
416 * dsb - mcr p15, 0, Rt, c7, c10, 4
418 if (aarch32_insn_mcr_extract_opc2(instr
) == 5)
425 * isb - mcr p15, 0, Rt, c7, c5, 4
427 * Taking an exception or returning from one acts as an
428 * instruction barrier. So no explicit barrier needed here.
434 pr_warn_ratelimited("\"%s\" (%ld) uses deprecated CP15 Barrier instruction at 0x%llx\n",
435 current
->comm
, (unsigned long)current
->pid
, regs
->pc
);
441 #define SCTLR_EL1_CP15BEN (1 << 5)
443 static inline void config_sctlr_el1(u32 clear
, u32 set
)
447 asm volatile("mrs %0, sctlr_el1" : "=r" (val
));
450 asm volatile("msr sctlr_el1, %0" : : "r" (val
));
453 static void enable_cp15_ben(void *info
)
455 config_sctlr_el1(0, SCTLR_EL1_CP15BEN
);
458 static void disable_cp15_ben(void *info
)
460 config_sctlr_el1(SCTLR_EL1_CP15BEN
, 0);
463 static int cpu_hotplug_notify(struct notifier_block
*b
,
464 unsigned long action
, void *hcpu
)
468 case CPU_STARTING_FROZEN
:
469 enable_cp15_ben(NULL
);
472 case CPU_DYING_FROZEN
:
473 disable_cp15_ben(NULL
);
480 static struct notifier_block cpu_hotplug_notifier
= {
481 .notifier_call
= cpu_hotplug_notify
,
484 static int cp15_barrier_set_hw_mode(bool enable
)
487 register_cpu_notifier(&cpu_hotplug_notifier
);
488 on_each_cpu(enable_cp15_ben
, NULL
, true);
490 unregister_cpu_notifier(&cpu_hotplug_notifier
);
491 on_each_cpu(disable_cp15_ben
, NULL
, true);
497 static struct undef_hook cp15_barrier_hooks
[] = {
499 .instr_mask
= 0x0fff0fdf,
500 .instr_val
= 0x0e070f9a,
501 .pstate_mask
= COMPAT_PSR_MODE_MASK
,
502 .pstate_val
= COMPAT_PSR_MODE_USR
,
503 .fn
= cp15barrier_handler
,
506 .instr_mask
= 0x0fff0fff,
507 .instr_val
= 0x0e070f95,
508 .pstate_mask
= COMPAT_PSR_MODE_MASK
,
509 .pstate_val
= COMPAT_PSR_MODE_USR
,
510 .fn
= cp15barrier_handler
,
515 static struct insn_emulation_ops cp15_barrier_ops
= {
516 .name
= "cp15_barrier",
517 .status
= INSN_DEPRECATED
,
518 .hooks
= cp15_barrier_hooks
,
519 .set_hw_mode
= cp15_barrier_set_hw_mode
,
523 * Invoked as late_initcall, since not needed before init spawned.
525 static int __init
armv8_deprecated_init(void)
527 if (IS_ENABLED(CONFIG_SWP_EMULATION
))
528 register_insn_emulation(&swp_ops
);
530 if (IS_ENABLED(CONFIG_CP15_BARRIER_EMULATION
))
531 register_insn_emulation(&cp15_barrier_ops
);
533 register_insn_emulation_sysctl(ctl_abi
);
538 late_initcall(armv8_deprecated_init
);