Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
2
Pull requests
0
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
6ca01a1
Documentation
arch
alpha
arc
arm
boot
common
configs
crypto
firmware
include
kernel
.gitignore
Makefile
arch_timer.c
armksyms.c
arthur.c
asm-offsets.c
atags.h
atags_compat.c
atags_parse.c
atags_proc.c
bios32.c
calls.S
cpuidle.c
crash_dump.c
debug.S
devtree.c
dma-isa.c
dma.c
early_printk.c
elf.c
entry-armv.S
entry-common.S
entry-header.S
entry-v7m.S
etm.c
fiq.c
fiqasm.S
ftrace.c
head-common.S
head-nommu.S
head.S
hibernate.c
hw_breakpoint.c
hyp-stub.S
insn.c
insn.h
io.c
irq.c
isa.c
iwmmxt.S
jump_label.c
kgdb.c
kprobes-arm.c
kprobes-common.c
kprobes-test-arm.c
kprobes-test-thumb.c
kprobes-test.c
kprobes-test.h
kprobes-thumb.c
kprobes.c
kprobes.h
machine_kexec.c
module.c
opcodes.c
patch.c
patch.h
perf_event.c
perf_event_cpu.c
perf_event_v6.c
perf_event_v7.c
perf_event_xscale.c
perf_regs.c
pj4-cp0.c
probes-arm.c
probes-arm.h
probes-thumb.c
probes-thumb.h
probes.c
probes.h
process.c
psci.c
psci_smp.c
ptrace.c
relocate_kernel.S
return_address.c
setup.c
signal.c
sigreturn_codes.S
sleep.S
smp.c
smp_scu.c
smp_tlb.c
smp_twd.c
stacktrace.c
suspend.c
swp_emulate.c
sys_arm.c
sys_oabi-compat.c
tcm.c
thumbee.c
time.c
topology.c
traps.c
unwind.c
uprobes-arm.c
uprobes.c
uprobes.h
v7m.c
vmlinux.lds.S
xscale-cp0.c
kvm
lib
mach-at91
mach-axxia
mach-bcm
mach-berlin
mach-clps711x
mach-cns3xxx
mach-davinci
mach-dove
mach-ebsa110
mach-efm32
mach-ep93xx
mach-exynos
mach-footbridge
mach-gemini
mach-highbank
mach-hisi
mach-imx
mach-integrator
mach-iop13xx
mach-iop32x
mach-iop33x
mach-ixp4xx
mach-keystone
mach-ks8695
mach-lpc32xx
mach-mediatek
mach-mmp
mach-moxart
mach-msm
mach-mv78xx0
mach-mvebu
mach-mxs
mach-netx
mach-nomadik
mach-nspire
mach-omap1
mach-omap2
mach-orion5x
mach-picoxcell
mach-prima2
mach-pxa
mach-qcom
mach-realview
mach-rockchip
mach-rpc
mach-s3c24xx
mach-s3c64xx
mach-s5pv210
mach-sa1100
mach-shmobile
mach-socfpga
mach-spear
mach-sti
mach-sunxi
mach-tegra
mach-u300
mach-ux500
mach-versatile
mach-vexpress
mach-vt8500
mach-w90x900
mach-zynq
mm
net
nwfpe
oprofile
plat-iop
plat-omap
plat-orion
plat-pxa
plat-samsung
plat-versatile
tools
vfp
xen
Kconfig
Kconfig-nommu
Kconfig.debug
Makefile
arm64
avr32
blackfin
c6x
cris
frv
hexagon
ia64
m32r
m68k
metag
microblaze
mips
mn10300
openrisc
parisc
powerpc
s390
score
sh
sparc
tile
um
unicore32
x86
xtensa
.gitignore
Kconfig
block
crypto
drivers
firmware
fs
include
init
ipc
kernel
lib
mm
net
samples
scripts
security
sound
tools
usr
virt
.gitignore
.mailmap
COPYING
CREDITS
Kbuild
Kconfig
MAINTAINERS
Makefile
README
REPORTING-BUGS
Breadcrumbs
linux
/
arch
/
arm
/
kernel
/
swp_emulate.c
Copy path
Blame
Blame
Latest commit
History
History
269 lines (231 loc) · 6.94 KB
Breadcrumbs
linux
/
arch
/
arm
/
kernel
/
swp_emulate.c
Top
File metadata and controls
Code
Blame
269 lines (231 loc) · 6.94 KB
Raw
/* * linux/arch/arm/kernel/swp_emulate.c * * Copyright (C) 2009 ARM Limited * __user_* functions adapted from include/asm/uaccess.h * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * Implements emulation of the SWP/SWPB instructions using load-exclusive and * store-exclusive for processors that have them disabled (or future ones that * might not implement them). * * Syntax of SWP{B} instruction: SWP{B}<c> <Rt>, <Rt2>, [<Rn>] * Where: Rt = destination * Rt2 = source * Rn = address */ #include <linux/init.h> #include <linux/kernel.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> #include <linux/sched.h> #include <linux/syscalls.h> #include <linux/perf_event.h> #include <asm/opcodes.h> #include <asm/system_info.h> #include <asm/traps.h> #include <asm/uaccess.h> /* * Error-checking SWP macros implemented using ldrex{b}/strex{b} */ #define __user_swpX_asm(data, addr, res, temp, B) \ __asm__ __volatile__( \ " mov %2, %1\n" \ "0: ldrex"B" %1, [%3]\n" \ "1: strex"B" %0, %2, [%3]\n" \ " cmp %0, #0\n" \ " movne %0, %4\n" \ "2:\n" \ " .section .fixup,\"ax\"\n" \ " .align 2\n" \ "3: mov %0, %5\n" \ " b 2b\n" \ " .previous\n" \ " .section __ex_table,\"a\"\n" \ " .align 3\n" \ " .long 0b, 3b\n" \ " .long 1b, 3b\n" \ " .previous" \ : "=&r" (res), "+r" (data), "=&r" (temp) \ : "r" (addr), "i" (-EAGAIN), "i" (-EFAULT) \ : "cc", "memory") #define __user_swp_asm(data, addr, res, temp) \ __user_swpX_asm(data, addr, res, temp, "") #define __user_swpb_asm(data, addr, res, temp) \ __user_swpX_asm(data, addr, res, temp, "b") /* * Macros/defines for extracting register numbers from instruction. */ #define EXTRACT_REG_NUM(instruction, offset) \ (((instruction) & (0xf << (offset))) >> (offset)) #define RN_OFFSET 16 #define RT_OFFSET 12 #define RT2_OFFSET 0 /* * Bit 22 of the instruction encoding distinguishes between * the SWP and SWPB variants (bit set means SWPB). */ #define TYPE_SWPB (1 << 22) static unsigned long swpcounter; static unsigned long swpbcounter; static unsigned long abtcounter; static pid_t previous_pid; #ifdef CONFIG_PROC_FS static int proc_status_show(struct seq_file *m, void *v) { seq_printf(m, "Emulated SWP:\t\t%lu\n", swpcounter); seq_printf(m, "Emulated SWPB:\t\t%lu\n", swpbcounter); seq_printf(m, "Aborted SWP{B}:\t\t%lu\n", abtcounter); if (previous_pid != 0) seq_printf(m, "Last process:\t\t%d\n", previous_pid); return 0; } static int proc_status_open(struct inode *inode, struct file *file) { return single_open(file, proc_status_show, PDE_DATA(inode)); } static const struct file_operations proc_status_fops = { .open = proc_status_open, .read = seq_read, .llseek = seq_lseek, .release = single_release, }; #endif /* * Set up process info to signal segmentation fault - called on access error. */ static void set_segfault(struct pt_regs *regs, unsigned long addr) { siginfo_t info; down_read(¤t->mm->mmap_sem); if (find_vma(current->mm, addr) == NULL) info.si_code = SEGV_MAPERR; else info.si_code = SEGV_ACCERR; up_read(¤t->mm->mmap_sem); info.si_signo = SIGSEGV; info.si_errno = 0; info.si_addr = (void *) instruction_pointer(regs); pr_debug("SWP{B} emulation: access caused memory abort!\n"); arm_notify_die("Illegal memory access", regs, &info, 0, 0); abtcounter++; } static int emulate_swpX(unsigned int address, unsigned int *data, unsigned int type) { unsigned int res = 0; if ((type != TYPE_SWPB) && (address & 0x3)) { /* SWP to unaligned address not permitted */ pr_debug("SWP instruction on unaligned pointer!\n"); return -EFAULT; } while (1) { unsigned long temp; if (type == TYPE_SWPB) __user_swpb_asm(*data, address, res, temp); else __user_swp_asm(*data, address, res, temp); if (likely(res != -EAGAIN) || signal_pending(current)) break; cond_resched(); } if (res == 0) { if (type == TYPE_SWPB) swpbcounter++; else swpcounter++; } return res; } /* * swp_handler logs the id of calling process, dissects the instruction, sanity * checks the memory location, calls emulate_swpX for the actual operation and * deals with fixup/error handling before returning */ static int swp_handler(struct pt_regs *regs, unsigned int instr) { unsigned int address, destreg, data, type; unsigned int res = 0; perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, regs->ARM_pc); res = arm_check_condition(instr, regs->ARM_cpsr); switch (res) { case ARM_OPCODE_CONDTEST_PASS: break; case ARM_OPCODE_CONDTEST_FAIL: /* Condition failed - return to next instruction */ regs->ARM_pc += 4; return 0; case ARM_OPCODE_CONDTEST_UNCOND: /* If unconditional encoding - not a SWP, undef */ return -EFAULT; default: return -EINVAL; } if (current->pid != previous_pid) { pr_debug("\"%s\" (%ld) uses deprecated SWP{B} instruction\n", current->comm, (unsigned long)current->pid); previous_pid = current->pid; } address = regs->uregs[EXTRACT_REG_NUM(instr, RN_OFFSET)]; data = regs->uregs[EXTRACT_REG_NUM(instr, RT2_OFFSET)]; destreg = EXTRACT_REG_NUM(instr, RT_OFFSET); type = instr & TYPE_SWPB; pr_debug("addr in r%d->0x%08x, dest is r%d, source in r%d->0x%08x)\n", EXTRACT_REG_NUM(instr, RN_OFFSET), address, destreg, EXTRACT_REG_NUM(instr, RT2_OFFSET), data); /* Check access in reasonable access range for both SWP and SWPB */ if (!access_ok(VERIFY_WRITE, (address & ~3), 4)) { pr_debug("SWP{B} emulation: access to %p not allowed!\n", (void *)address); res = -EFAULT; } else { res = emulate_swpX(address, &data, type); } if (res == 0) { /* * On successful emulation, revert the adjustment to the PC * made in kernel/traps.c in order to resume execution at the * instruction following the SWP{B}. */ regs->ARM_pc += 4; regs->uregs[destreg] = data; } else if (res == -EFAULT) { /* * Memory errors do not mean emulation failed. * Set up signal info to return SEGV, then return OK */ set_segfault(regs, address); } return 0; } /* * Only emulate SWP/SWPB executed in ARM state/User mode. * The kernel must be SWP free and SWP{B} does not exist in Thumb/ThumbEE. */ static struct undef_hook swp_hook = { .instr_mask = 0x0fb00ff0, .instr_val = 0x01000090, .cpsr_mask = MODE_MASK | PSR_T_BIT | PSR_J_BIT, .cpsr_val = USR_MODE, .fn = swp_handler }; /* * Register handler and create status file in /proc/cpu * Invoked as late_initcall, since not needed before init spawned. */ static int __init swp_emulation_init(void) { if (cpu_architecture() < CPU_ARCH_ARMv7) return 0; #ifdef CONFIG_PROC_FS if (!proc_create("cpu/swp_emulation", S_IRUGO, NULL, &proc_status_fops)) return -ENOMEM; #endif /* CONFIG_PROC_FS */ printk(KERN_NOTICE "Registering SWP/SWPB emulation handler\n"); register_undef_hook(&swp_hook); return 0; } late_initcall(swp_emulation_init);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
You can’t perform that action at this time.