xref: /openbmc/linux/arch/arm/kernel/swp_emulate.c (revision 526c5978)
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>
2664d2dc38SLeif Lindholm #include <linux/syscalls.h>
2764d2dc38SLeif Lindholm #include <linux/perf_event.h>
2864d2dc38SLeif Lindholm 
29c245dcd3SLeif Lindholm #include <asm/opcodes.h>
3064d2dc38SLeif Lindholm #include <asm/traps.h>
3164d2dc38SLeif Lindholm #include <asm/uaccess.h>
3264d2dc38SLeif Lindholm 
3364d2dc38SLeif Lindholm /*
3464d2dc38SLeif Lindholm  * Error-checking SWP macros implemented using ldrex{b}/strex{b}
3564d2dc38SLeif Lindholm  */
3664d2dc38SLeif Lindholm #define __user_swpX_asm(data, addr, res, temp, B)		\
3764d2dc38SLeif Lindholm 	__asm__ __volatile__(					\
3864d2dc38SLeif Lindholm 	"	mov		%2, %1\n"			\
3964d2dc38SLeif Lindholm 	"0:	ldrex"B"	%1, [%3]\n"			\
4064d2dc38SLeif Lindholm 	"1:	strex"B"	%0, %2, [%3]\n"			\
4164d2dc38SLeif Lindholm 	"	cmp		%0, #0\n"			\
4264d2dc38SLeif Lindholm 	"	movne		%0, %4\n"			\
4364d2dc38SLeif Lindholm 	"2:\n"							\
4464d2dc38SLeif Lindholm 	"	.section	 .fixup,\"ax\"\n"		\
4564d2dc38SLeif Lindholm 	"	.align		2\n"				\
4664d2dc38SLeif Lindholm 	"3:	mov		%0, %5\n"			\
4764d2dc38SLeif Lindholm 	"	b		2b\n"				\
4864d2dc38SLeif Lindholm 	"	.previous\n"					\
4964d2dc38SLeif Lindholm 	"	.section	 __ex_table,\"a\"\n"		\
5064d2dc38SLeif Lindholm 	"	.align		3\n"				\
5164d2dc38SLeif Lindholm 	"	.long		0b, 3b\n"			\
5264d2dc38SLeif Lindholm 	"	.long		1b, 3b\n"			\
5364d2dc38SLeif Lindholm 	"	.previous"					\
5464d2dc38SLeif Lindholm 	: "=&r" (res), "+r" (data), "=&r" (temp)		\
5564d2dc38SLeif Lindholm 	: "r" (addr), "i" (-EAGAIN), "i" (-EFAULT)		\
5664d2dc38SLeif Lindholm 	: "cc", "memory")
5764d2dc38SLeif Lindholm 
5864d2dc38SLeif Lindholm #define __user_swp_asm(data, addr, res, temp) \
5964d2dc38SLeif Lindholm 	__user_swpX_asm(data, addr, res, temp, "")
6064d2dc38SLeif Lindholm #define __user_swpb_asm(data, addr, res, temp) \
6164d2dc38SLeif Lindholm 	__user_swpX_asm(data, addr, res, temp, "b")
6264d2dc38SLeif Lindholm 
6364d2dc38SLeif Lindholm /*
6464d2dc38SLeif Lindholm  * Macros/defines for extracting register numbers from instruction.
6564d2dc38SLeif Lindholm  */
6664d2dc38SLeif Lindholm #define EXTRACT_REG_NUM(instruction, offset) \
6764d2dc38SLeif Lindholm 	(((instruction) & (0xf << (offset))) >> (offset))
6864d2dc38SLeif Lindholm #define RN_OFFSET  16
6964d2dc38SLeif Lindholm #define RT_OFFSET  12
7064d2dc38SLeif Lindholm #define RT2_OFFSET  0
7164d2dc38SLeif Lindholm /*
7264d2dc38SLeif Lindholm  * Bit 22 of the instruction encoding distinguishes between
7364d2dc38SLeif Lindholm  * the SWP and SWPB variants (bit set means SWPB).
7464d2dc38SLeif Lindholm  */
7564d2dc38SLeif Lindholm #define TYPE_SWPB (1 << 22)
7664d2dc38SLeif Lindholm 
7764d2dc38SLeif Lindholm static unsigned long swpcounter;
7864d2dc38SLeif Lindholm static unsigned long swpbcounter;
7964d2dc38SLeif Lindholm static unsigned long abtcounter;
8064d2dc38SLeif Lindholm static pid_t         previous_pid;
8164d2dc38SLeif Lindholm 
8264d2dc38SLeif Lindholm #ifdef CONFIG_PROC_FS
83526c5978SDavid Howells static int proc_status_show(struct seq_file *m, void *v)
8464d2dc38SLeif Lindholm {
85526c5978SDavid Howells 	seq_printf(m, "Emulated SWP:\t\t%lu\n", swpcounter);
86526c5978SDavid Howells 	seq_printf(m, "Emulated SWPB:\t\t%lu\n", swpbcounter);
87526c5978SDavid Howells 	seq_printf(m, "Aborted SWP{B}:\t\t%lu\n", abtcounter);
8864d2dc38SLeif Lindholm 	if (previous_pid != 0)
89526c5978SDavid Howells 		seq_printf(m, "Last process:\t\t%d\n", previous_pid);
90526c5978SDavid Howells 	return 0;
9164d2dc38SLeif Lindholm }
92526c5978SDavid Howells 
93526c5978SDavid Howells static int proc_status_open(struct inode *inode, struct file *file)
94526c5978SDavid Howells {
95526c5978SDavid Howells 	return single_open(file, proc_status_show, PDE_DATA(inode));
96526c5978SDavid Howells }
97526c5978SDavid Howells 
98526c5978SDavid Howells static const struct file_operations proc_status_fops = {
99526c5978SDavid Howells 	.open		= proc_status_open,
100526c5978SDavid Howells 	.read		= seq_read,
101526c5978SDavid Howells 	.llseek		= seq_lseek,
102526c5978SDavid Howells 	.release	= seq_release,
103526c5978SDavid Howells };
10464d2dc38SLeif Lindholm #endif
10564d2dc38SLeif Lindholm 
10664d2dc38SLeif Lindholm /*
10764d2dc38SLeif Lindholm  * Set up process info to signal segmentation fault - called on access error.
10864d2dc38SLeif Lindholm  */
10964d2dc38SLeif Lindholm static void set_segfault(struct pt_regs *regs, unsigned long addr)
11064d2dc38SLeif Lindholm {
11164d2dc38SLeif Lindholm 	siginfo_t info;
11264d2dc38SLeif Lindholm 
1137bf9b7beSAl Viro 	down_read(&current->mm->mmap_sem);
11464d2dc38SLeif Lindholm 	if (find_vma(current->mm, addr) == NULL)
11564d2dc38SLeif Lindholm 		info.si_code = SEGV_MAPERR;
11664d2dc38SLeif Lindholm 	else
11764d2dc38SLeif Lindholm 		info.si_code = SEGV_ACCERR;
1187bf9b7beSAl Viro 	up_read(&current->mm->mmap_sem);
11964d2dc38SLeif Lindholm 
12064d2dc38SLeif Lindholm 	info.si_signo = SIGSEGV;
12164d2dc38SLeif Lindholm 	info.si_errno = 0;
12264d2dc38SLeif Lindholm 	info.si_addr  = (void *) instruction_pointer(regs);
12364d2dc38SLeif Lindholm 
12464d2dc38SLeif Lindholm 	pr_debug("SWP{B} emulation: access caused memory abort!\n");
12564d2dc38SLeif Lindholm 	arm_notify_die("Illegal memory access", regs, &info, 0, 0);
12664d2dc38SLeif Lindholm 
12764d2dc38SLeif Lindholm 	abtcounter++;
12864d2dc38SLeif Lindholm }
12964d2dc38SLeif Lindholm 
13064d2dc38SLeif Lindholm static int emulate_swpX(unsigned int address, unsigned int *data,
13164d2dc38SLeif Lindholm 			unsigned int type)
13264d2dc38SLeif Lindholm {
13364d2dc38SLeif Lindholm 	unsigned int res = 0;
13464d2dc38SLeif Lindholm 
13564d2dc38SLeif Lindholm 	if ((type != TYPE_SWPB) && (address & 0x3)) {
13664d2dc38SLeif Lindholm 		/* SWP to unaligned address not permitted */
13764d2dc38SLeif Lindholm 		pr_debug("SWP instruction on unaligned pointer!\n");
13864d2dc38SLeif Lindholm 		return -EFAULT;
13964d2dc38SLeif Lindholm 	}
14064d2dc38SLeif Lindholm 
14164d2dc38SLeif Lindholm 	while (1) {
14264d2dc38SLeif Lindholm 		unsigned long temp;
14364d2dc38SLeif Lindholm 
14464d2dc38SLeif Lindholm 		/*
14564d2dc38SLeif Lindholm 		 * Barrier required between accessing protected resource and
14664d2dc38SLeif Lindholm 		 * releasing a lock for it. Legacy code might not have done
14764d2dc38SLeif Lindholm 		 * this, and we cannot determine that this is not the case
14864d2dc38SLeif Lindholm 		 * being emulated, so insert always.
14964d2dc38SLeif Lindholm 		 */
15064d2dc38SLeif Lindholm 		smp_mb();
15164d2dc38SLeif Lindholm 
15264d2dc38SLeif Lindholm 		if (type == TYPE_SWPB)
15364d2dc38SLeif Lindholm 			__user_swpb_asm(*data, address, res, temp);
15464d2dc38SLeif Lindholm 		else
15564d2dc38SLeif Lindholm 			__user_swp_asm(*data, address, res, temp);
15664d2dc38SLeif Lindholm 
15764d2dc38SLeif Lindholm 		if (likely(res != -EAGAIN) || signal_pending(current))
15864d2dc38SLeif Lindholm 			break;
15964d2dc38SLeif Lindholm 
16064d2dc38SLeif Lindholm 		cond_resched();
16164d2dc38SLeif Lindholm 	}
16264d2dc38SLeif Lindholm 
16364d2dc38SLeif Lindholm 	if (res == 0) {
16464d2dc38SLeif Lindholm 		/*
16525985edcSLucas De Marchi 		 * Barrier also required between acquiring a lock for a
16664d2dc38SLeif Lindholm 		 * protected resource and accessing the resource. Inserted for
16764d2dc38SLeif Lindholm 		 * same reason as above.
16864d2dc38SLeif Lindholm 		 */
16964d2dc38SLeif Lindholm 		smp_mb();
17064d2dc38SLeif Lindholm 
17164d2dc38SLeif Lindholm 		if (type == TYPE_SWPB)
17264d2dc38SLeif Lindholm 			swpbcounter++;
17364d2dc38SLeif Lindholm 		else
17464d2dc38SLeif Lindholm 			swpcounter++;
17564d2dc38SLeif Lindholm 	}
17664d2dc38SLeif Lindholm 
17764d2dc38SLeif Lindholm 	return res;
17864d2dc38SLeif Lindholm }
17964d2dc38SLeif Lindholm 
18064d2dc38SLeif Lindholm /*
18164d2dc38SLeif Lindholm  * swp_handler logs the id of calling process, dissects the instruction, sanity
18264d2dc38SLeif Lindholm  * checks the memory location, calls emulate_swpX for the actual operation and
18364d2dc38SLeif Lindholm  * deals with fixup/error handling before returning
18464d2dc38SLeif Lindholm  */
18564d2dc38SLeif Lindholm static int swp_handler(struct pt_regs *regs, unsigned int instr)
18664d2dc38SLeif Lindholm {
18764d2dc38SLeif Lindholm 	unsigned int address, destreg, data, type;
18864d2dc38SLeif Lindholm 	unsigned int res = 0;
18964d2dc38SLeif Lindholm 
190a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, regs->ARM_pc);
19164d2dc38SLeif Lindholm 
192c245dcd3SLeif Lindholm 	res = arm_check_condition(instr, regs->ARM_cpsr);
193c245dcd3SLeif Lindholm 	switch (res) {
194c245dcd3SLeif Lindholm 	case ARM_OPCODE_CONDTEST_PASS:
195c245dcd3SLeif Lindholm 		break;
196c245dcd3SLeif Lindholm 	case ARM_OPCODE_CONDTEST_FAIL:
197c245dcd3SLeif Lindholm 		/* Condition failed - return to next instruction */
198c245dcd3SLeif Lindholm 		regs->ARM_pc += 4;
199c245dcd3SLeif Lindholm 		return 0;
200c245dcd3SLeif Lindholm 	case ARM_OPCODE_CONDTEST_UNCOND:
201c245dcd3SLeif Lindholm 		/* If unconditional encoding - not a SWP, undef */
202c245dcd3SLeif Lindholm 		return -EFAULT;
203c245dcd3SLeif Lindholm 	default:
204c245dcd3SLeif Lindholm 		return -EINVAL;
205c245dcd3SLeif Lindholm 	}
206c245dcd3SLeif Lindholm 
20764d2dc38SLeif Lindholm 	if (current->pid != previous_pid) {
20864d2dc38SLeif Lindholm 		pr_debug("\"%s\" (%ld) uses deprecated SWP{B} instruction\n",
20964d2dc38SLeif Lindholm 			 current->comm, (unsigned long)current->pid);
21064d2dc38SLeif Lindholm 		previous_pid = current->pid;
21164d2dc38SLeif Lindholm 	}
21264d2dc38SLeif Lindholm 
21364d2dc38SLeif Lindholm 	address = regs->uregs[EXTRACT_REG_NUM(instr, RN_OFFSET)];
21464d2dc38SLeif Lindholm 	data	= regs->uregs[EXTRACT_REG_NUM(instr, RT2_OFFSET)];
21564d2dc38SLeif Lindholm 	destreg = EXTRACT_REG_NUM(instr, RT_OFFSET);
21664d2dc38SLeif Lindholm 
21764d2dc38SLeif Lindholm 	type = instr & TYPE_SWPB;
21864d2dc38SLeif Lindholm 
21964d2dc38SLeif Lindholm 	pr_debug("addr in r%d->0x%08x, dest is r%d, source in r%d->0x%08x)\n",
22064d2dc38SLeif Lindholm 		 EXTRACT_REG_NUM(instr, RN_OFFSET), address,
22164d2dc38SLeif Lindholm 		 destreg, EXTRACT_REG_NUM(instr, RT2_OFFSET), data);
22264d2dc38SLeif Lindholm 
22364d2dc38SLeif Lindholm 	/* Check access in reasonable access range for both SWP and SWPB */
22464d2dc38SLeif Lindholm 	if (!access_ok(VERIFY_WRITE, (address & ~3), 4)) {
22564d2dc38SLeif Lindholm 		pr_debug("SWP{B} emulation: access to %p not allowed!\n",
22664d2dc38SLeif Lindholm 			 (void *)address);
22764d2dc38SLeif Lindholm 		res = -EFAULT;
22864d2dc38SLeif Lindholm 	} else {
22964d2dc38SLeif Lindholm 		res = emulate_swpX(address, &data, type);
23064d2dc38SLeif Lindholm 	}
23164d2dc38SLeif Lindholm 
23264d2dc38SLeif Lindholm 	if (res == 0) {
23364d2dc38SLeif Lindholm 		/*
23464d2dc38SLeif Lindholm 		 * On successful emulation, revert the adjustment to the PC
23564d2dc38SLeif Lindholm 		 * made in kernel/traps.c in order to resume execution at the
23664d2dc38SLeif Lindholm 		 * instruction following the SWP{B}.
23764d2dc38SLeif Lindholm 		 */
23864d2dc38SLeif Lindholm 		regs->ARM_pc += 4;
23964d2dc38SLeif Lindholm 		regs->uregs[destreg] = data;
24064d2dc38SLeif Lindholm 	} else if (res == -EFAULT) {
24164d2dc38SLeif Lindholm 		/*
24264d2dc38SLeif Lindholm 		 * Memory errors do not mean emulation failed.
24364d2dc38SLeif Lindholm 		 * Set up signal info to return SEGV, then return OK
24464d2dc38SLeif Lindholm 		 */
24564d2dc38SLeif Lindholm 		set_segfault(regs, address);
24664d2dc38SLeif Lindholm 	}
24764d2dc38SLeif Lindholm 
24864d2dc38SLeif Lindholm 	return 0;
24964d2dc38SLeif Lindholm }
25064d2dc38SLeif Lindholm 
25164d2dc38SLeif Lindholm /*
25264d2dc38SLeif Lindholm  * Only emulate SWP/SWPB executed in ARM state/User mode.
25364d2dc38SLeif Lindholm  * The kernel must be SWP free and SWP{B} does not exist in Thumb/ThumbEE.
25464d2dc38SLeif Lindholm  */
25564d2dc38SLeif Lindholm static struct undef_hook swp_hook = {
25664d2dc38SLeif Lindholm 	.instr_mask = 0x0fb00ff0,
25764d2dc38SLeif Lindholm 	.instr_val  = 0x01000090,
25864d2dc38SLeif Lindholm 	.cpsr_mask  = MODE_MASK | PSR_T_BIT | PSR_J_BIT,
25964d2dc38SLeif Lindholm 	.cpsr_val   = USR_MODE,
26064d2dc38SLeif Lindholm 	.fn	    = swp_handler
26164d2dc38SLeif Lindholm };
26264d2dc38SLeif Lindholm 
26364d2dc38SLeif Lindholm /*
26464d2dc38SLeif Lindholm  * Register handler and create status file in /proc/cpu
26564d2dc38SLeif Lindholm  * Invoked as late_initcall, since not needed before init spawned.
26664d2dc38SLeif Lindholm  */
26764d2dc38SLeif Lindholm static int __init swp_emulation_init(void)
26864d2dc38SLeif Lindholm {
26964d2dc38SLeif Lindholm #ifdef CONFIG_PROC_FS
270526c5978SDavid Howells 	if (!proc_create("cpu/swp_emulation", S_IRUGO, NULL, &proc_status_fops))
27164d2dc38SLeif Lindholm 		return -ENOMEM;
27264d2dc38SLeif Lindholm #endif /* CONFIG_PROC_FS */
27364d2dc38SLeif Lindholm 
27464d2dc38SLeif Lindholm 	printk(KERN_NOTICE "Registering SWP/SWPB emulation handler\n");
27564d2dc38SLeif Lindholm 	register_undef_hook(&swp_hook);
27664d2dc38SLeif Lindholm 
27764d2dc38SLeif Lindholm 	return 0;
27864d2dc38SLeif Lindholm }
27964d2dc38SLeif Lindholm 
28064d2dc38SLeif Lindholm late_initcall(swp_emulation_init);
281