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> 307397aa48SRussell King #include <asm/system_info.h> 3164d2dc38SLeif Lindholm #include <asm/traps.h> 3264d2dc38SLeif Lindholm #include <asm/uaccess.h> 3364d2dc38SLeif Lindholm 3464d2dc38SLeif Lindholm /* 3564d2dc38SLeif Lindholm * Error-checking SWP macros implemented using ldrex{b}/strex{b} 3664d2dc38SLeif Lindholm */ 3764d2dc38SLeif Lindholm #define __user_swpX_asm(data, addr, res, temp, B) \ 3864d2dc38SLeif Lindholm __asm__ __volatile__( \ 3964d2dc38SLeif Lindholm " mov %2, %1\n" \ 4064d2dc38SLeif Lindholm "0: ldrex"B" %1, [%3]\n" \ 4164d2dc38SLeif Lindholm "1: strex"B" %0, %2, [%3]\n" \ 4264d2dc38SLeif Lindholm " cmp %0, #0\n" \ 4364d2dc38SLeif Lindholm " movne %0, %4\n" \ 4464d2dc38SLeif Lindholm "2:\n" \ 4564d2dc38SLeif Lindholm " .section .fixup,\"ax\"\n" \ 4664d2dc38SLeif Lindholm " .align 2\n" \ 4764d2dc38SLeif Lindholm "3: mov %0, %5\n" \ 4864d2dc38SLeif Lindholm " b 2b\n" \ 4964d2dc38SLeif Lindholm " .previous\n" \ 5064d2dc38SLeif Lindholm " .section __ex_table,\"a\"\n" \ 5164d2dc38SLeif Lindholm " .align 3\n" \ 5264d2dc38SLeif Lindholm " .long 0b, 3b\n" \ 5364d2dc38SLeif Lindholm " .long 1b, 3b\n" \ 5464d2dc38SLeif Lindholm " .previous" \ 5564d2dc38SLeif Lindholm : "=&r" (res), "+r" (data), "=&r" (temp) \ 5664d2dc38SLeif Lindholm : "r" (addr), "i" (-EAGAIN), "i" (-EFAULT) \ 5764d2dc38SLeif Lindholm : "cc", "memory") 5864d2dc38SLeif Lindholm 5964d2dc38SLeif Lindholm #define __user_swp_asm(data, addr, res, temp) \ 6064d2dc38SLeif Lindholm __user_swpX_asm(data, addr, res, temp, "") 6164d2dc38SLeif Lindholm #define __user_swpb_asm(data, addr, res, temp) \ 6264d2dc38SLeif Lindholm __user_swpX_asm(data, addr, res, temp, "b") 6364d2dc38SLeif Lindholm 6464d2dc38SLeif Lindholm /* 6564d2dc38SLeif Lindholm * Macros/defines for extracting register numbers from instruction. 6664d2dc38SLeif Lindholm */ 6764d2dc38SLeif Lindholm #define EXTRACT_REG_NUM(instruction, offset) \ 6864d2dc38SLeif Lindholm (((instruction) & (0xf << (offset))) >> (offset)) 6964d2dc38SLeif Lindholm #define RN_OFFSET 16 7064d2dc38SLeif Lindholm #define RT_OFFSET 12 7164d2dc38SLeif Lindholm #define RT2_OFFSET 0 7264d2dc38SLeif Lindholm /* 7364d2dc38SLeif Lindholm * Bit 22 of the instruction encoding distinguishes between 7464d2dc38SLeif Lindholm * the SWP and SWPB variants (bit set means SWPB). 7564d2dc38SLeif Lindholm */ 7664d2dc38SLeif Lindholm #define TYPE_SWPB (1 << 22) 7764d2dc38SLeif Lindholm 7864d2dc38SLeif Lindholm static unsigned long swpcounter; 7964d2dc38SLeif Lindholm static unsigned long swpbcounter; 8064d2dc38SLeif Lindholm static unsigned long abtcounter; 8164d2dc38SLeif Lindholm static pid_t previous_pid; 8264d2dc38SLeif Lindholm 8364d2dc38SLeif Lindholm #ifdef CONFIG_PROC_FS 84526c5978SDavid Howells static int proc_status_show(struct seq_file *m, void *v) 8564d2dc38SLeif Lindholm { 86526c5978SDavid Howells seq_printf(m, "Emulated SWP:\t\t%lu\n", swpcounter); 87526c5978SDavid Howells seq_printf(m, "Emulated SWPB:\t\t%lu\n", swpbcounter); 88526c5978SDavid Howells seq_printf(m, "Aborted SWP{B}:\t\t%lu\n", abtcounter); 8964d2dc38SLeif Lindholm if (previous_pid != 0) 90526c5978SDavid Howells seq_printf(m, "Last process:\t\t%d\n", previous_pid); 91526c5978SDavid Howells return 0; 9264d2dc38SLeif Lindholm } 93526c5978SDavid Howells 94526c5978SDavid Howells static int proc_status_open(struct inode *inode, struct file *file) 95526c5978SDavid Howells { 96526c5978SDavid Howells return single_open(file, proc_status_show, PDE_DATA(inode)); 97526c5978SDavid Howells } 98526c5978SDavid Howells 99526c5978SDavid Howells static const struct file_operations proc_status_fops = { 100526c5978SDavid Howells .open = proc_status_open, 101526c5978SDavid Howells .read = seq_read, 102526c5978SDavid Howells .llseek = seq_lseek, 103b11ac20eSAl Viro .release = single_release, 104526c5978SDavid Howells }; 10564d2dc38SLeif Lindholm #endif 10664d2dc38SLeif Lindholm 10764d2dc38SLeif Lindholm /* 10864d2dc38SLeif Lindholm * Set up process info to signal segmentation fault - called on access error. 10964d2dc38SLeif Lindholm */ 11064d2dc38SLeif Lindholm static void set_segfault(struct pt_regs *regs, unsigned long addr) 11164d2dc38SLeif Lindholm { 11264d2dc38SLeif Lindholm siginfo_t info; 11364d2dc38SLeif Lindholm 1147bf9b7beSAl Viro down_read(¤t->mm->mmap_sem); 11564d2dc38SLeif Lindholm if (find_vma(current->mm, addr) == NULL) 11664d2dc38SLeif Lindholm info.si_code = SEGV_MAPERR; 11764d2dc38SLeif Lindholm else 11864d2dc38SLeif Lindholm info.si_code = SEGV_ACCERR; 1197bf9b7beSAl Viro up_read(¤t->mm->mmap_sem); 12064d2dc38SLeif Lindholm 12164d2dc38SLeif Lindholm info.si_signo = SIGSEGV; 12264d2dc38SLeif Lindholm info.si_errno = 0; 12364d2dc38SLeif Lindholm info.si_addr = (void *) instruction_pointer(regs); 12464d2dc38SLeif Lindholm 12564d2dc38SLeif Lindholm pr_debug("SWP{B} emulation: access caused memory abort!\n"); 12664d2dc38SLeif Lindholm arm_notify_die("Illegal memory access", regs, &info, 0, 0); 12764d2dc38SLeif Lindholm 12864d2dc38SLeif Lindholm abtcounter++; 12964d2dc38SLeif Lindholm } 13064d2dc38SLeif Lindholm 13164d2dc38SLeif Lindholm static int emulate_swpX(unsigned int address, unsigned int *data, 13264d2dc38SLeif Lindholm unsigned int type) 13364d2dc38SLeif Lindholm { 13464d2dc38SLeif Lindholm unsigned int res = 0; 13564d2dc38SLeif Lindholm 13664d2dc38SLeif Lindholm if ((type != TYPE_SWPB) && (address & 0x3)) { 13764d2dc38SLeif Lindholm /* SWP to unaligned address not permitted */ 13864d2dc38SLeif Lindholm pr_debug("SWP instruction on unaligned pointer!\n"); 13964d2dc38SLeif Lindholm return -EFAULT; 14064d2dc38SLeif Lindholm } 14164d2dc38SLeif Lindholm 14264d2dc38SLeif Lindholm while (1) { 14364d2dc38SLeif Lindholm unsigned long temp; 14464d2dc38SLeif Lindholm 14564d2dc38SLeif Lindholm if (type == TYPE_SWPB) 14664d2dc38SLeif Lindholm __user_swpb_asm(*data, address, res, temp); 14764d2dc38SLeif Lindholm else 14864d2dc38SLeif Lindholm __user_swp_asm(*data, address, res, temp); 14964d2dc38SLeif Lindholm 15064d2dc38SLeif Lindholm if (likely(res != -EAGAIN) || signal_pending(current)) 15164d2dc38SLeif Lindholm break; 15264d2dc38SLeif Lindholm 15364d2dc38SLeif Lindholm cond_resched(); 15464d2dc38SLeif Lindholm } 15564d2dc38SLeif Lindholm 15664d2dc38SLeif Lindholm if (res == 0) { 15764d2dc38SLeif Lindholm if (type == TYPE_SWPB) 15864d2dc38SLeif Lindholm swpbcounter++; 15964d2dc38SLeif Lindholm else 16064d2dc38SLeif Lindholm swpcounter++; 16164d2dc38SLeif Lindholm } 16264d2dc38SLeif Lindholm 16364d2dc38SLeif Lindholm return res; 16464d2dc38SLeif Lindholm } 16564d2dc38SLeif Lindholm 16664d2dc38SLeif Lindholm /* 16764d2dc38SLeif Lindholm * swp_handler logs the id of calling process, dissects the instruction, sanity 16864d2dc38SLeif Lindholm * checks the memory location, calls emulate_swpX for the actual operation and 16964d2dc38SLeif Lindholm * deals with fixup/error handling before returning 17064d2dc38SLeif Lindholm */ 17164d2dc38SLeif Lindholm static int swp_handler(struct pt_regs *regs, unsigned int instr) 17264d2dc38SLeif Lindholm { 17364d2dc38SLeif Lindholm unsigned int address, destreg, data, type; 17464d2dc38SLeif Lindholm unsigned int res = 0; 17564d2dc38SLeif Lindholm 176a8b0ca17SPeter Zijlstra perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, regs->ARM_pc); 17764d2dc38SLeif Lindholm 178c245dcd3SLeif Lindholm res = arm_check_condition(instr, regs->ARM_cpsr); 179c245dcd3SLeif Lindholm switch (res) { 180c245dcd3SLeif Lindholm case ARM_OPCODE_CONDTEST_PASS: 181c245dcd3SLeif Lindholm break; 182c245dcd3SLeif Lindholm case ARM_OPCODE_CONDTEST_FAIL: 183c245dcd3SLeif Lindholm /* Condition failed - return to next instruction */ 184c245dcd3SLeif Lindholm regs->ARM_pc += 4; 185c245dcd3SLeif Lindholm return 0; 186c245dcd3SLeif Lindholm case ARM_OPCODE_CONDTEST_UNCOND: 187c245dcd3SLeif Lindholm /* If unconditional encoding - not a SWP, undef */ 188c245dcd3SLeif Lindholm return -EFAULT; 189c245dcd3SLeif Lindholm default: 190c245dcd3SLeif Lindholm return -EINVAL; 191c245dcd3SLeif Lindholm } 192c245dcd3SLeif Lindholm 19364d2dc38SLeif Lindholm if (current->pid != previous_pid) { 19464d2dc38SLeif Lindholm pr_debug("\"%s\" (%ld) uses deprecated SWP{B} instruction\n", 19564d2dc38SLeif Lindholm current->comm, (unsigned long)current->pid); 19664d2dc38SLeif Lindholm previous_pid = current->pid; 19764d2dc38SLeif Lindholm } 19864d2dc38SLeif Lindholm 19964d2dc38SLeif Lindholm address = regs->uregs[EXTRACT_REG_NUM(instr, RN_OFFSET)]; 20064d2dc38SLeif Lindholm data = regs->uregs[EXTRACT_REG_NUM(instr, RT2_OFFSET)]; 20164d2dc38SLeif Lindholm destreg = EXTRACT_REG_NUM(instr, RT_OFFSET); 20264d2dc38SLeif Lindholm 20364d2dc38SLeif Lindholm type = instr & TYPE_SWPB; 20464d2dc38SLeif Lindholm 20564d2dc38SLeif Lindholm pr_debug("addr in r%d->0x%08x, dest is r%d, source in r%d->0x%08x)\n", 20664d2dc38SLeif Lindholm EXTRACT_REG_NUM(instr, RN_OFFSET), address, 20764d2dc38SLeif Lindholm destreg, EXTRACT_REG_NUM(instr, RT2_OFFSET), data); 20864d2dc38SLeif Lindholm 20964d2dc38SLeif Lindholm /* Check access in reasonable access range for both SWP and SWPB */ 21064d2dc38SLeif Lindholm if (!access_ok(VERIFY_WRITE, (address & ~3), 4)) { 21164d2dc38SLeif Lindholm pr_debug("SWP{B} emulation: access to %p not allowed!\n", 21264d2dc38SLeif Lindholm (void *)address); 21364d2dc38SLeif Lindholm res = -EFAULT; 21464d2dc38SLeif Lindholm } else { 21564d2dc38SLeif Lindholm res = emulate_swpX(address, &data, type); 21664d2dc38SLeif Lindholm } 21764d2dc38SLeif Lindholm 21864d2dc38SLeif Lindholm if (res == 0) { 21964d2dc38SLeif Lindholm /* 22064d2dc38SLeif Lindholm * On successful emulation, revert the adjustment to the PC 22164d2dc38SLeif Lindholm * made in kernel/traps.c in order to resume execution at the 22264d2dc38SLeif Lindholm * instruction following the SWP{B}. 22364d2dc38SLeif Lindholm */ 22464d2dc38SLeif Lindholm regs->ARM_pc += 4; 22564d2dc38SLeif Lindholm regs->uregs[destreg] = data; 22664d2dc38SLeif Lindholm } else if (res == -EFAULT) { 22764d2dc38SLeif Lindholm /* 22864d2dc38SLeif Lindholm * Memory errors do not mean emulation failed. 22964d2dc38SLeif Lindholm * Set up signal info to return SEGV, then return OK 23064d2dc38SLeif Lindholm */ 23164d2dc38SLeif Lindholm set_segfault(regs, address); 23264d2dc38SLeif Lindholm } 23364d2dc38SLeif Lindholm 23464d2dc38SLeif Lindholm return 0; 23564d2dc38SLeif Lindholm } 23664d2dc38SLeif Lindholm 23764d2dc38SLeif Lindholm /* 23864d2dc38SLeif Lindholm * Only emulate SWP/SWPB executed in ARM state/User mode. 23964d2dc38SLeif Lindholm * The kernel must be SWP free and SWP{B} does not exist in Thumb/ThumbEE. 24064d2dc38SLeif Lindholm */ 24164d2dc38SLeif Lindholm static struct undef_hook swp_hook = { 24264d2dc38SLeif Lindholm .instr_mask = 0x0fb00ff0, 24364d2dc38SLeif Lindholm .instr_val = 0x01000090, 24464d2dc38SLeif Lindholm .cpsr_mask = MODE_MASK | PSR_T_BIT | PSR_J_BIT, 24564d2dc38SLeif Lindholm .cpsr_val = USR_MODE, 24664d2dc38SLeif Lindholm .fn = swp_handler 24764d2dc38SLeif Lindholm }; 24864d2dc38SLeif Lindholm 24964d2dc38SLeif Lindholm /* 25064d2dc38SLeif Lindholm * Register handler and create status file in /proc/cpu 25164d2dc38SLeif Lindholm * Invoked as late_initcall, since not needed before init spawned. 25264d2dc38SLeif Lindholm */ 25364d2dc38SLeif Lindholm static int __init swp_emulation_init(void) 25464d2dc38SLeif Lindholm { 2557397aa48SRussell King if (cpu_architecture() < CPU_ARCH_ARMv7) 2567397aa48SRussell King return 0; 2577397aa48SRussell King 25864d2dc38SLeif Lindholm #ifdef CONFIG_PROC_FS 259526c5978SDavid Howells if (!proc_create("cpu/swp_emulation", S_IRUGO, NULL, &proc_status_fops)) 26064d2dc38SLeif Lindholm return -ENOMEM; 26164d2dc38SLeif Lindholm #endif /* CONFIG_PROC_FS */ 26264d2dc38SLeif Lindholm 2634ed89f22SRussell King pr_notice("Registering SWP/SWPB emulation handler\n"); 26464d2dc38SLeif Lindholm register_undef_hook(&swp_hook); 26564d2dc38SLeif Lindholm 26664d2dc38SLeif Lindholm return 0; 26764d2dc38SLeif Lindholm } 26864d2dc38SLeif Lindholm 26964d2dc38SLeif Lindholm late_initcall(swp_emulation_init); 270