xref: /openbmc/linux/arch/arm/kernel/swp_emulate.c (revision 05e792e3)
164d2dc38SLeif Lindholm /*
264d2dc38SLeif Lindholm  *  linux/arch/arm/kernel/swp_emulate.c
364d2dc38SLeif Lindholm  *
464d2dc38SLeif Lindholm  *  Copyright (C) 2009 ARM Limited
564d2dc38SLeif Lindholm  *  __user_* functions adapted from include/asm/uaccess.h
664d2dc38SLeif Lindholm  *
764d2dc38SLeif Lindholm  * This program is free software; you can redistribute it and/or modify
864d2dc38SLeif Lindholm  * it under the terms of the GNU General Public License version 2 as
964d2dc38SLeif Lindholm  * published by the Free Software Foundation.
1064d2dc38SLeif Lindholm  *
1164d2dc38SLeif Lindholm  *  Implements emulation of the SWP/SWPB instructions using load-exclusive and
1264d2dc38SLeif Lindholm  *  store-exclusive for processors that have them disabled (or future ones that
1364d2dc38SLeif Lindholm  *  might not implement them).
1464d2dc38SLeif Lindholm  *
1564d2dc38SLeif Lindholm  *  Syntax of SWP{B} instruction: SWP{B}<c> <Rt>, <Rt2>, [<Rn>]
1664d2dc38SLeif Lindholm  *  Where: Rt  = destination
1764d2dc38SLeif Lindholm  *	   Rt2 = source
1864d2dc38SLeif Lindholm  *	   Rn  = address
1964d2dc38SLeif Lindholm  */
2064d2dc38SLeif Lindholm 
2164d2dc38SLeif Lindholm #include <linux/init.h>
2264d2dc38SLeif Lindholm #include <linux/kernel.h>
2364d2dc38SLeif Lindholm #include <linux/proc_fs.h>
24526c5978SDavid Howells #include <linux/seq_file.h>
2564d2dc38SLeif Lindholm #include <linux/sched.h>
26589ee628SIngo Molnar #include <linux/sched/mm.h>
2764d2dc38SLeif Lindholm #include <linux/syscalls.h>
2864d2dc38SLeif Lindholm #include <linux/perf_event.h>
2964d2dc38SLeif Lindholm 
30c245dcd3SLeif Lindholm #include <asm/opcodes.h>
317397aa48SRussell King #include <asm/system_info.h>
3264d2dc38SLeif Lindholm #include <asm/traps.h>
337c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
3464d2dc38SLeif Lindholm 
3564d2dc38SLeif Lindholm /*
3664d2dc38SLeif Lindholm  * Error-checking SWP macros implemented using ldrex{b}/strex{b}
3764d2dc38SLeif Lindholm  */
3864d2dc38SLeif Lindholm #define __user_swpX_asm(data, addr, res, temp, B)		\
3964d2dc38SLeif Lindholm 	__asm__ __volatile__(					\
4034bfbae3SShengjiu Wang 	"0:	ldrex"B"	%2, [%3]\n"			\
4134bfbae3SShengjiu Wang 	"1:	strex"B"	%0, %1, [%3]\n"			\
4264d2dc38SLeif Lindholm 	"	cmp		%0, #0\n"			\
4334bfbae3SShengjiu Wang 	"	moveq		%1, %2\n"			\
4464d2dc38SLeif Lindholm 	"	movne		%0, %4\n"			\
4564d2dc38SLeif Lindholm 	"2:\n"							\
46c4a84ae3SArd Biesheuvel 	"	.section	 .text.fixup,\"ax\"\n"		\
4764d2dc38SLeif Lindholm 	"	.align		2\n"				\
4864d2dc38SLeif Lindholm 	"3:	mov		%0, %5\n"			\
4964d2dc38SLeif Lindholm 	"	b		2b\n"				\
5064d2dc38SLeif Lindholm 	"	.previous\n"					\
5164d2dc38SLeif Lindholm 	"	.section	 __ex_table,\"a\"\n"		\
5264d2dc38SLeif Lindholm 	"	.align		3\n"				\
5364d2dc38SLeif Lindholm 	"	.long		0b, 3b\n"			\
5464d2dc38SLeif Lindholm 	"	.long		1b, 3b\n"			\
5564d2dc38SLeif Lindholm 	"	.previous"					\
5664d2dc38SLeif Lindholm 	: "=&r" (res), "+r" (data), "=&r" (temp)		\
5764d2dc38SLeif Lindholm 	: "r" (addr), "i" (-EAGAIN), "i" (-EFAULT)		\
5864d2dc38SLeif Lindholm 	: "cc", "memory")
5964d2dc38SLeif Lindholm 
6064d2dc38SLeif Lindholm #define __user_swp_asm(data, addr, res, temp) \
6164d2dc38SLeif Lindholm 	__user_swpX_asm(data, addr, res, temp, "")
6264d2dc38SLeif Lindholm #define __user_swpb_asm(data, addr, res, temp) \
6364d2dc38SLeif Lindholm 	__user_swpX_asm(data, addr, res, temp, "b")
6464d2dc38SLeif Lindholm 
6564d2dc38SLeif Lindholm /*
6664d2dc38SLeif Lindholm  * Macros/defines for extracting register numbers from instruction.
6764d2dc38SLeif Lindholm  */
6864d2dc38SLeif Lindholm #define EXTRACT_REG_NUM(instruction, offset) \
6964d2dc38SLeif Lindholm 	(((instruction) & (0xf << (offset))) >> (offset))
7064d2dc38SLeif Lindholm #define RN_OFFSET  16
7164d2dc38SLeif Lindholm #define RT_OFFSET  12
7264d2dc38SLeif Lindholm #define RT2_OFFSET  0
7364d2dc38SLeif Lindholm /*
7464d2dc38SLeif Lindholm  * Bit 22 of the instruction encoding distinguishes between
7564d2dc38SLeif Lindholm  * the SWP and SWPB variants (bit set means SWPB).
7664d2dc38SLeif Lindholm  */
7764d2dc38SLeif Lindholm #define TYPE_SWPB (1 << 22)
7864d2dc38SLeif Lindholm 
7964d2dc38SLeif Lindholm static unsigned long swpcounter;
8064d2dc38SLeif Lindholm static unsigned long swpbcounter;
8164d2dc38SLeif Lindholm static unsigned long abtcounter;
8264d2dc38SLeif Lindholm static pid_t         previous_pid;
8364d2dc38SLeif Lindholm 
8464d2dc38SLeif Lindholm #ifdef CONFIG_PROC_FS
85526c5978SDavid Howells static int proc_status_show(struct seq_file *m, void *v)
8664d2dc38SLeif Lindholm {
87526c5978SDavid Howells 	seq_printf(m, "Emulated SWP:\t\t%lu\n", swpcounter);
88526c5978SDavid Howells 	seq_printf(m, "Emulated SWPB:\t\t%lu\n", swpbcounter);
89526c5978SDavid Howells 	seq_printf(m, "Aborted SWP{B}:\t\t%lu\n", abtcounter);
9064d2dc38SLeif Lindholm 	if (previous_pid != 0)
91526c5978SDavid Howells 		seq_printf(m, "Last process:\t\t%d\n", previous_pid);
92526c5978SDavid Howells 	return 0;
9364d2dc38SLeif Lindholm }
9464d2dc38SLeif Lindholm #endif
9564d2dc38SLeif Lindholm 
9664d2dc38SLeif Lindholm /*
9764d2dc38SLeif Lindholm  * Set up process info to signal segmentation fault - called on access error.
9864d2dc38SLeif Lindholm  */
9964d2dc38SLeif Lindholm static void set_segfault(struct pt_regs *regs, unsigned long addr)
10064d2dc38SLeif Lindholm {
10105e792e3SEric W. Biederman 	int si_code;
10264d2dc38SLeif Lindholm 
1037bf9b7beSAl Viro 	down_read(&current->mm->mmap_sem);
10464d2dc38SLeif Lindholm 	if (find_vma(current->mm, addr) == NULL)
10505e792e3SEric W. Biederman 		si_code = SEGV_MAPERR;
10664d2dc38SLeif Lindholm 	else
10705e792e3SEric W. Biederman 		si_code = SEGV_ACCERR;
1087bf9b7beSAl Viro 	up_read(&current->mm->mmap_sem);
10964d2dc38SLeif Lindholm 
11064d2dc38SLeif Lindholm 	pr_debug("SWP{B} emulation: access caused memory abort!\n");
11105e792e3SEric W. Biederman 	arm_notify_die("Illegal memory access", regs,
11205e792e3SEric W. Biederman 		       SIGSEGV, si_code,
11305e792e3SEric W. Biederman 		       (void __user *)instruction_pointer(regs),
11405e792e3SEric W. Biederman 		       0, 0);
11564d2dc38SLeif Lindholm 
11664d2dc38SLeif Lindholm 	abtcounter++;
11764d2dc38SLeif Lindholm }
11864d2dc38SLeif Lindholm 
11964d2dc38SLeif Lindholm static int emulate_swpX(unsigned int address, unsigned int *data,
12064d2dc38SLeif Lindholm 			unsigned int type)
12164d2dc38SLeif Lindholm {
12264d2dc38SLeif Lindholm 	unsigned int res = 0;
12364d2dc38SLeif Lindholm 
12464d2dc38SLeif Lindholm 	if ((type != TYPE_SWPB) && (address & 0x3)) {
12564d2dc38SLeif Lindholm 		/* SWP to unaligned address not permitted */
12664d2dc38SLeif Lindholm 		pr_debug("SWP instruction on unaligned pointer!\n");
12764d2dc38SLeif Lindholm 		return -EFAULT;
12864d2dc38SLeif Lindholm 	}
12964d2dc38SLeif Lindholm 
13064d2dc38SLeif Lindholm 	while (1) {
13164d2dc38SLeif Lindholm 		unsigned long temp;
132a5e090acSRussell King 		unsigned int __ua_flags;
13364d2dc38SLeif Lindholm 
134a5e090acSRussell King 		__ua_flags = uaccess_save_and_enable();
13564d2dc38SLeif Lindholm 		if (type == TYPE_SWPB)
13664d2dc38SLeif Lindholm 			__user_swpb_asm(*data, address, res, temp);
13764d2dc38SLeif Lindholm 		else
13864d2dc38SLeif Lindholm 			__user_swp_asm(*data, address, res, temp);
139a5e090acSRussell King 		uaccess_restore(__ua_flags);
14064d2dc38SLeif Lindholm 
14164d2dc38SLeif Lindholm 		if (likely(res != -EAGAIN) || signal_pending(current))
14264d2dc38SLeif Lindholm 			break;
14364d2dc38SLeif Lindholm 
14464d2dc38SLeif Lindholm 		cond_resched();
14564d2dc38SLeif Lindholm 	}
14664d2dc38SLeif Lindholm 
14764d2dc38SLeif Lindholm 	if (res == 0) {
14864d2dc38SLeif Lindholm 		if (type == TYPE_SWPB)
14964d2dc38SLeif Lindholm 			swpbcounter++;
15064d2dc38SLeif Lindholm 		else
15164d2dc38SLeif Lindholm 			swpcounter++;
15264d2dc38SLeif Lindholm 	}
15364d2dc38SLeif Lindholm 
15464d2dc38SLeif Lindholm 	return res;
15564d2dc38SLeif Lindholm }
15664d2dc38SLeif Lindholm 
15764d2dc38SLeif Lindholm /*
15864d2dc38SLeif Lindholm  * swp_handler logs the id of calling process, dissects the instruction, sanity
15964d2dc38SLeif Lindholm  * checks the memory location, calls emulate_swpX for the actual operation and
16064d2dc38SLeif Lindholm  * deals with fixup/error handling before returning
16164d2dc38SLeif Lindholm  */
16264d2dc38SLeif Lindholm static int swp_handler(struct pt_regs *regs, unsigned int instr)
16364d2dc38SLeif Lindholm {
16464d2dc38SLeif Lindholm 	unsigned int address, destreg, data, type;
16564d2dc38SLeif Lindholm 	unsigned int res = 0;
16664d2dc38SLeif Lindholm 
167a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, regs->ARM_pc);
16864d2dc38SLeif Lindholm 
169c245dcd3SLeif Lindholm 	res = arm_check_condition(instr, regs->ARM_cpsr);
170c245dcd3SLeif Lindholm 	switch (res) {
171c245dcd3SLeif Lindholm 	case ARM_OPCODE_CONDTEST_PASS:
172c245dcd3SLeif Lindholm 		break;
173c245dcd3SLeif Lindholm 	case ARM_OPCODE_CONDTEST_FAIL:
174c245dcd3SLeif Lindholm 		/* Condition failed - return to next instruction */
175c245dcd3SLeif Lindholm 		regs->ARM_pc += 4;
176c245dcd3SLeif Lindholm 		return 0;
177c245dcd3SLeif Lindholm 	case ARM_OPCODE_CONDTEST_UNCOND:
178c245dcd3SLeif Lindholm 		/* If unconditional encoding - not a SWP, undef */
179c245dcd3SLeif Lindholm 		return -EFAULT;
180c245dcd3SLeif Lindholm 	default:
181c245dcd3SLeif Lindholm 		return -EINVAL;
182c245dcd3SLeif Lindholm 	}
183c245dcd3SLeif Lindholm 
18464d2dc38SLeif Lindholm 	if (current->pid != previous_pid) {
18564d2dc38SLeif Lindholm 		pr_debug("\"%s\" (%ld) uses deprecated SWP{B} instruction\n",
18664d2dc38SLeif Lindholm 			 current->comm, (unsigned long)current->pid);
18764d2dc38SLeif Lindholm 		previous_pid = current->pid;
18864d2dc38SLeif Lindholm 	}
18964d2dc38SLeif Lindholm 
19064d2dc38SLeif Lindholm 	address = regs->uregs[EXTRACT_REG_NUM(instr, RN_OFFSET)];
19164d2dc38SLeif Lindholm 	data	= regs->uregs[EXTRACT_REG_NUM(instr, RT2_OFFSET)];
19264d2dc38SLeif Lindholm 	destreg = EXTRACT_REG_NUM(instr, RT_OFFSET);
19364d2dc38SLeif Lindholm 
19464d2dc38SLeif Lindholm 	type = instr & TYPE_SWPB;
19564d2dc38SLeif Lindholm 
19664d2dc38SLeif Lindholm 	pr_debug("addr in r%d->0x%08x, dest is r%d, source in r%d->0x%08x)\n",
19764d2dc38SLeif Lindholm 		 EXTRACT_REG_NUM(instr, RN_OFFSET), address,
19864d2dc38SLeif Lindholm 		 destreg, EXTRACT_REG_NUM(instr, RT2_OFFSET), data);
19964d2dc38SLeif Lindholm 
20064d2dc38SLeif Lindholm 	/* Check access in reasonable access range for both SWP and SWPB */
20164d2dc38SLeif Lindholm 	if (!access_ok(VERIFY_WRITE, (address & ~3), 4)) {
20264d2dc38SLeif Lindholm 		pr_debug("SWP{B} emulation: access to %p not allowed!\n",
20364d2dc38SLeif Lindholm 			 (void *)address);
20464d2dc38SLeif Lindholm 		res = -EFAULT;
20564d2dc38SLeif Lindholm 	} else {
20664d2dc38SLeif Lindholm 		res = emulate_swpX(address, &data, type);
20764d2dc38SLeif Lindholm 	}
20864d2dc38SLeif Lindholm 
20964d2dc38SLeif Lindholm 	if (res == 0) {
21064d2dc38SLeif Lindholm 		/*
21164d2dc38SLeif Lindholm 		 * On successful emulation, revert the adjustment to the PC
21264d2dc38SLeif Lindholm 		 * made in kernel/traps.c in order to resume execution at the
21364d2dc38SLeif Lindholm 		 * instruction following the SWP{B}.
21464d2dc38SLeif Lindholm 		 */
21564d2dc38SLeif Lindholm 		regs->ARM_pc += 4;
21664d2dc38SLeif Lindholm 		regs->uregs[destreg] = data;
21764d2dc38SLeif Lindholm 	} else if (res == -EFAULT) {
21864d2dc38SLeif Lindholm 		/*
21964d2dc38SLeif Lindholm 		 * Memory errors do not mean emulation failed.
22064d2dc38SLeif Lindholm 		 * Set up signal info to return SEGV, then return OK
22164d2dc38SLeif Lindholm 		 */
22264d2dc38SLeif Lindholm 		set_segfault(regs, address);
22364d2dc38SLeif Lindholm 	}
22464d2dc38SLeif Lindholm 
22564d2dc38SLeif Lindholm 	return 0;
22664d2dc38SLeif Lindholm }
22764d2dc38SLeif Lindholm 
22864d2dc38SLeif Lindholm /*
22964d2dc38SLeif Lindholm  * Only emulate SWP/SWPB executed in ARM state/User mode.
23064d2dc38SLeif Lindholm  * The kernel must be SWP free and SWP{B} does not exist in Thumb/ThumbEE.
23164d2dc38SLeif Lindholm  */
23264d2dc38SLeif Lindholm static struct undef_hook swp_hook = {
23364d2dc38SLeif Lindholm 	.instr_mask = 0x0fb00ff0,
23464d2dc38SLeif Lindholm 	.instr_val  = 0x01000090,
23564d2dc38SLeif Lindholm 	.cpsr_mask  = MODE_MASK | PSR_T_BIT | PSR_J_BIT,
23664d2dc38SLeif Lindholm 	.cpsr_val   = USR_MODE,
23764d2dc38SLeif Lindholm 	.fn	    = swp_handler
23864d2dc38SLeif Lindholm };
23964d2dc38SLeif Lindholm 
24064d2dc38SLeif Lindholm /*
24164d2dc38SLeif Lindholm  * Register handler and create status file in /proc/cpu
24264d2dc38SLeif Lindholm  * Invoked as late_initcall, since not needed before init spawned.
24364d2dc38SLeif Lindholm  */
24464d2dc38SLeif Lindholm static int __init swp_emulation_init(void)
24564d2dc38SLeif Lindholm {
2467397aa48SRussell King 	if (cpu_architecture() < CPU_ARCH_ARMv7)
2477397aa48SRussell King 		return 0;
2487397aa48SRussell King 
24964d2dc38SLeif Lindholm #ifdef CONFIG_PROC_FS
2503f3942acSChristoph Hellwig 	if (!proc_create_single("cpu/swp_emulation", S_IRUGO, NULL,
2513f3942acSChristoph Hellwig 			proc_status_show))
25264d2dc38SLeif Lindholm 		return -ENOMEM;
25364d2dc38SLeif Lindholm #endif /* CONFIG_PROC_FS */
25464d2dc38SLeif Lindholm 
2554ed89f22SRussell King 	pr_notice("Registering SWP/SWPB emulation handler\n");
25664d2dc38SLeif Lindholm 	register_undef_hook(&swp_hook);
25764d2dc38SLeif Lindholm 
25864d2dc38SLeif Lindholm 	return 0;
25964d2dc38SLeif Lindholm }
26064d2dc38SLeif Lindholm 
26164d2dc38SLeif Lindholm late_initcall(swp_emulation_init);
262