2 * Derived from from linux/arch/arm/kernel/swp_emulate.c
4 * Copyright (C) 2009 ARM Limited
5 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Implements emulation of the SWP/SWPB instructions using load-exclusive and
12 * store-exclusive for processors that have them disabled (or future ones that
13 * might not implement them).
15 * Syntax of SWP{B} instruction: SWP{B}<c> <Rt>, <Rt2>, [<Rn>]
16 * Where: Rt = destination
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/proc_fs.h>
24 #include <linux/seq_file.h>
25 #include <linux/sched.h>
26 #include <linux/syscalls.h>
27 #include <linux/perf_event.h>
29 #include <asm/opcodes.h>
30 #include <asm/traps.h>
31 #include <asm/uaccess.h>
32 #include <asm/system_misc.h>
33 #include <linux/debugfs.h>
36 * Error-checking SWP macros implemented using ldrex{b}/strex{b}
39 static int swpb(u8 in
, u8
*out
, u8
*addr
)
48 "1: stxrb %w0, %w3, %4\n"
51 " .section .fixup,\"ax\"\n"
56 " .section __ex_table,\"a\"\n"
61 : "=&r" (res
), "=r" (_out
), "=r" (err
)
62 : "r" (in
), "Q" (*addr
), "i" (-EFAULT
)
64 } while (err
== 0 && res
!= 0);
71 static int swp(u32 in
, u32
*out
, u32
*addr
)
80 "1: stxr %w0, %w3, %4\n"
83 " .section .fixup,\"ax\"\n"
88 " .section __ex_table,\"a\"\n"
93 : "=&r" (res
), "=r" (_out
), "=r" (err
)
94 : "r" (in
), "Q" (*addr
), "i" (-EFAULT
)
96 } while (err
== 0 && res
!= 0);
103 * Macros/defines for extracting register numbers from instruction.
105 #define EXTRACT_REG_NUM(instruction, offset) \
106 (((instruction) & (0xf << (offset))) >> (offset))
111 * Bit 22 of the instruction encoding distinguishes between
112 * the SWP and SWPB variants (bit set means SWPB).
114 #define TYPE_SWPB (1 << 22)
116 static pid_t previous_pid
;
122 * swp_handler logs the id of calling process, dissects the instruction, sanity
123 * checks the memory location, calls emulate_swpX for the actual operation and
124 * deals with fixup/error handling before returning
126 static int swp_handler(struct pt_regs
*regs
, unsigned int instr
)
128 u32 destreg
, data
, type
;
130 unsigned int res
= 0;
135 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS
, 1, regs
, regs
->pc
);
137 res
= arm_check_condition(instr
, regs
->pstate
);
139 case ARM_OPCODE_CONDTEST_PASS
:
141 case ARM_OPCODE_CONDTEST_FAIL
:
142 /* Condition failed - return to next instruction */
145 case ARM_OPCODE_CONDTEST_UNCOND
:
146 /* If unconditional encoding - not a SWP, undef */
152 if (current
->pid
!= previous_pid
) {
153 pr_warn("\"%s\" (%ld) uses obsolete SWP{B} instruction\n",
154 current
->comm
, (unsigned long)current
->pid
);
155 previous_pid
= current
->pid
;
158 address
= regs
->regs
[EXTRACT_REG_NUM(instr
, RN_OFFSET
)] & 0xffffffff;
159 data
= regs
->regs
[EXTRACT_REG_NUM(instr
, RT2_OFFSET
)];
160 destreg
= EXTRACT_REG_NUM(instr
, RT_OFFSET
);
162 type
= instr
& TYPE_SWPB
;
164 /* Check access in reasonable access range for both SWP and SWPB */
165 if (!access_ok(VERIFY_WRITE
, (address
& ~3), 4)) {
166 pr_debug("SWP{B} emulation: access to %p not allowed!\n",
170 if (type
== TYPE_SWPB
) {
171 err
= swpb((u8
) data
, &temp8
, (u8
*) address
);
174 regs
->regs
[destreg
] = temp8
;
177 } else if (address
& 0x3) {
178 /* SWP to unaligned address not permitted */
179 pr_debug("SWP instruction on unaligned pointer!\n");
182 err
= swp((u32
) data
, &temp32
, (u32
*) address
);
185 regs
->regs
[destreg
] = temp32
;
194 * Only emulate SWP/SWPB executed in ARM state/User mode.
195 * The kernel must be SWP free and SWP{B} does not exist in Thumb/ThumbEE.
197 static struct undef_hook swp_hook
= {
198 .instr_mask
= 0x0fb00ff0,
199 .instr_val
= 0x01000090,
200 .pstate_mask
= COMPAT_PSR_MODE_MASK
| COMPAT_PSR_T_BIT
,
201 .pstate_val
= COMPAT_PSR_MODE_USR
,
206 * Register handler and create status file in /proc/cpu
207 * Invoked as late_initcall, since not needed before init spawned.
209 static int __init
swp_emulation_init(void)
212 dir
= debugfs_create_dir("swp_emulate", NULL
);
213 debugfs_create_u64("swp_count", S_IRUGO
| S_IWUSR
, dir
, &swp_count
);
214 debugfs_create_u64("swpb_count", S_IRUGO
| S_IWUSR
, dir
, &swpb_count
);
216 pr_notice("Registering SWP/SWPB emulation handler\n");
217 register_undef_hook(&swp_hook
);
223 late_initcall(swp_emulation_init
);