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> 2464d2dc38SLeif Lindholm #include <linux/sched.h> 2564d2dc38SLeif Lindholm #include <linux/syscalls.h> 2664d2dc38SLeif Lindholm #include <linux/perf_event.h> 2764d2dc38SLeif Lindholm 2864d2dc38SLeif Lindholm #include <asm/traps.h> 2964d2dc38SLeif Lindholm #include <asm/uaccess.h> 3064d2dc38SLeif Lindholm 3164d2dc38SLeif Lindholm /* 3264d2dc38SLeif Lindholm * Error-checking SWP macros implemented using ldrex{b}/strex{b} 3364d2dc38SLeif Lindholm */ 3464d2dc38SLeif Lindholm #define __user_swpX_asm(data, addr, res, temp, B) \ 3564d2dc38SLeif Lindholm __asm__ __volatile__( \ 3664d2dc38SLeif Lindholm " mov %2, %1\n" \ 3764d2dc38SLeif Lindholm "0: ldrex"B" %1, [%3]\n" \ 3864d2dc38SLeif Lindholm "1: strex"B" %0, %2, [%3]\n" \ 3964d2dc38SLeif Lindholm " cmp %0, #0\n" \ 4064d2dc38SLeif Lindholm " movne %0, %4\n" \ 4164d2dc38SLeif Lindholm "2:\n" \ 4264d2dc38SLeif Lindholm " .section .fixup,\"ax\"\n" \ 4364d2dc38SLeif Lindholm " .align 2\n" \ 4464d2dc38SLeif Lindholm "3: mov %0, %5\n" \ 4564d2dc38SLeif Lindholm " b 2b\n" \ 4664d2dc38SLeif Lindholm " .previous\n" \ 4764d2dc38SLeif Lindholm " .section __ex_table,\"a\"\n" \ 4864d2dc38SLeif Lindholm " .align 3\n" \ 4964d2dc38SLeif Lindholm " .long 0b, 3b\n" \ 5064d2dc38SLeif Lindholm " .long 1b, 3b\n" \ 5164d2dc38SLeif Lindholm " .previous" \ 5264d2dc38SLeif Lindholm : "=&r" (res), "+r" (data), "=&r" (temp) \ 5364d2dc38SLeif Lindholm : "r" (addr), "i" (-EAGAIN), "i" (-EFAULT) \ 5464d2dc38SLeif Lindholm : "cc", "memory") 5564d2dc38SLeif Lindholm 5664d2dc38SLeif Lindholm #define __user_swp_asm(data, addr, res, temp) \ 5764d2dc38SLeif Lindholm __user_swpX_asm(data, addr, res, temp, "") 5864d2dc38SLeif Lindholm #define __user_swpb_asm(data, addr, res, temp) \ 5964d2dc38SLeif Lindholm __user_swpX_asm(data, addr, res, temp, "b") 6064d2dc38SLeif Lindholm 6164d2dc38SLeif Lindholm /* 6264d2dc38SLeif Lindholm * Macros/defines for extracting register numbers from instruction. 6364d2dc38SLeif Lindholm */ 6464d2dc38SLeif Lindholm #define EXTRACT_REG_NUM(instruction, offset) \ 6564d2dc38SLeif Lindholm (((instruction) & (0xf << (offset))) >> (offset)) 6664d2dc38SLeif Lindholm #define RN_OFFSET 16 6764d2dc38SLeif Lindholm #define RT_OFFSET 12 6864d2dc38SLeif Lindholm #define RT2_OFFSET 0 6964d2dc38SLeif Lindholm /* 7064d2dc38SLeif Lindholm * Bit 22 of the instruction encoding distinguishes between 7164d2dc38SLeif Lindholm * the SWP and SWPB variants (bit set means SWPB). 7264d2dc38SLeif Lindholm */ 7364d2dc38SLeif Lindholm #define TYPE_SWPB (1 << 22) 7464d2dc38SLeif Lindholm 7564d2dc38SLeif Lindholm static unsigned long swpcounter; 7664d2dc38SLeif Lindholm static unsigned long swpbcounter; 7764d2dc38SLeif Lindholm static unsigned long abtcounter; 7864d2dc38SLeif Lindholm static pid_t previous_pid; 7964d2dc38SLeif Lindholm 8064d2dc38SLeif Lindholm #ifdef CONFIG_PROC_FS 8164d2dc38SLeif Lindholm static int proc_read_status(char *page, char **start, off_t off, int count, 8264d2dc38SLeif Lindholm int *eof, void *data) 8364d2dc38SLeif Lindholm { 8464d2dc38SLeif Lindholm char *p = page; 8564d2dc38SLeif Lindholm int len; 8664d2dc38SLeif Lindholm 8764d2dc38SLeif Lindholm p += sprintf(p, "Emulated SWP:\t\t%lu\n", swpcounter); 8864d2dc38SLeif Lindholm p += sprintf(p, "Emulated SWPB:\t\t%lu\n", swpbcounter); 8964d2dc38SLeif Lindholm p += sprintf(p, "Aborted SWP{B}:\t\t%lu\n", abtcounter); 9064d2dc38SLeif Lindholm if (previous_pid != 0) 9164d2dc38SLeif Lindholm p += sprintf(p, "Last process:\t\t%d\n", previous_pid); 9264d2dc38SLeif Lindholm 9364d2dc38SLeif Lindholm len = (p - page) - off; 9464d2dc38SLeif Lindholm if (len < 0) 9564d2dc38SLeif Lindholm len = 0; 9664d2dc38SLeif Lindholm 9764d2dc38SLeif Lindholm *eof = (len <= count) ? 1 : 0; 9864d2dc38SLeif Lindholm *start = page + off; 9964d2dc38SLeif Lindholm 10064d2dc38SLeif Lindholm return len; 10164d2dc38SLeif Lindholm } 10264d2dc38SLeif Lindholm #endif 10364d2dc38SLeif Lindholm 10464d2dc38SLeif Lindholm /* 10564d2dc38SLeif Lindholm * Set up process info to signal segmentation fault - called on access error. 10664d2dc38SLeif Lindholm */ 10764d2dc38SLeif Lindholm static void set_segfault(struct pt_regs *regs, unsigned long addr) 10864d2dc38SLeif Lindholm { 10964d2dc38SLeif Lindholm siginfo_t info; 11064d2dc38SLeif Lindholm 11164d2dc38SLeif Lindholm if (find_vma(current->mm, addr) == NULL) 11264d2dc38SLeif Lindholm info.si_code = SEGV_MAPERR; 11364d2dc38SLeif Lindholm else 11464d2dc38SLeif Lindholm info.si_code = SEGV_ACCERR; 11564d2dc38SLeif Lindholm 11664d2dc38SLeif Lindholm info.si_signo = SIGSEGV; 11764d2dc38SLeif Lindholm info.si_errno = 0; 11864d2dc38SLeif Lindholm info.si_addr = (void *) instruction_pointer(regs); 11964d2dc38SLeif Lindholm 12064d2dc38SLeif Lindholm pr_debug("SWP{B} emulation: access caused memory abort!\n"); 12164d2dc38SLeif Lindholm arm_notify_die("Illegal memory access", regs, &info, 0, 0); 12264d2dc38SLeif Lindholm 12364d2dc38SLeif Lindholm abtcounter++; 12464d2dc38SLeif Lindholm } 12564d2dc38SLeif Lindholm 12664d2dc38SLeif Lindholm static int emulate_swpX(unsigned int address, unsigned int *data, 12764d2dc38SLeif Lindholm unsigned int type) 12864d2dc38SLeif Lindholm { 12964d2dc38SLeif Lindholm unsigned int res = 0; 13064d2dc38SLeif Lindholm 13164d2dc38SLeif Lindholm if ((type != TYPE_SWPB) && (address & 0x3)) { 13264d2dc38SLeif Lindholm /* SWP to unaligned address not permitted */ 13364d2dc38SLeif Lindholm pr_debug("SWP instruction on unaligned pointer!\n"); 13464d2dc38SLeif Lindholm return -EFAULT; 13564d2dc38SLeif Lindholm } 13664d2dc38SLeif Lindholm 13764d2dc38SLeif Lindholm while (1) { 13864d2dc38SLeif Lindholm unsigned long temp; 13964d2dc38SLeif Lindholm 14064d2dc38SLeif Lindholm /* 14164d2dc38SLeif Lindholm * Barrier required between accessing protected resource and 14264d2dc38SLeif Lindholm * releasing a lock for it. Legacy code might not have done 14364d2dc38SLeif Lindholm * this, and we cannot determine that this is not the case 14464d2dc38SLeif Lindholm * being emulated, so insert always. 14564d2dc38SLeif Lindholm */ 14664d2dc38SLeif Lindholm smp_mb(); 14764d2dc38SLeif Lindholm 14864d2dc38SLeif Lindholm if (type == TYPE_SWPB) 14964d2dc38SLeif Lindholm __user_swpb_asm(*data, address, res, temp); 15064d2dc38SLeif Lindholm else 15164d2dc38SLeif Lindholm __user_swp_asm(*data, address, res, temp); 15264d2dc38SLeif Lindholm 15364d2dc38SLeif Lindholm if (likely(res != -EAGAIN) || signal_pending(current)) 15464d2dc38SLeif Lindholm break; 15564d2dc38SLeif Lindholm 15664d2dc38SLeif Lindholm cond_resched(); 15764d2dc38SLeif Lindholm } 15864d2dc38SLeif Lindholm 15964d2dc38SLeif Lindholm if (res == 0) { 16064d2dc38SLeif Lindholm /* 16125985edcSLucas De Marchi * Barrier also required between acquiring a lock for a 16264d2dc38SLeif Lindholm * protected resource and accessing the resource. Inserted for 16364d2dc38SLeif Lindholm * same reason as above. 16464d2dc38SLeif Lindholm */ 16564d2dc38SLeif Lindholm smp_mb(); 16664d2dc38SLeif Lindholm 16764d2dc38SLeif Lindholm if (type == TYPE_SWPB) 16864d2dc38SLeif Lindholm swpbcounter++; 16964d2dc38SLeif Lindholm else 17064d2dc38SLeif Lindholm swpcounter++; 17164d2dc38SLeif Lindholm } 17264d2dc38SLeif Lindholm 17364d2dc38SLeif Lindholm return res; 17464d2dc38SLeif Lindholm } 17564d2dc38SLeif Lindholm 17664d2dc38SLeif Lindholm /* 17764d2dc38SLeif Lindholm * swp_handler logs the id of calling process, dissects the instruction, sanity 17864d2dc38SLeif Lindholm * checks the memory location, calls emulate_swpX for the actual operation and 17964d2dc38SLeif Lindholm * deals with fixup/error handling before returning 18064d2dc38SLeif Lindholm */ 18164d2dc38SLeif Lindholm static int swp_handler(struct pt_regs *regs, unsigned int instr) 18264d2dc38SLeif Lindholm { 18364d2dc38SLeif Lindholm unsigned int address, destreg, data, type; 18464d2dc38SLeif Lindholm unsigned int res = 0; 18564d2dc38SLeif Lindholm 18664d2dc38SLeif Lindholm perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, 0, regs, regs->ARM_pc); 18764d2dc38SLeif Lindholm 18864d2dc38SLeif Lindholm if (current->pid != previous_pid) { 18964d2dc38SLeif Lindholm pr_debug("\"%s\" (%ld) uses deprecated SWP{B} instruction\n", 19064d2dc38SLeif Lindholm current->comm, (unsigned long)current->pid); 19164d2dc38SLeif Lindholm previous_pid = current->pid; 19264d2dc38SLeif Lindholm } 19364d2dc38SLeif Lindholm 19464d2dc38SLeif Lindholm address = regs->uregs[EXTRACT_REG_NUM(instr, RN_OFFSET)]; 19564d2dc38SLeif Lindholm data = regs->uregs[EXTRACT_REG_NUM(instr, RT2_OFFSET)]; 19664d2dc38SLeif Lindholm destreg = EXTRACT_REG_NUM(instr, RT_OFFSET); 19764d2dc38SLeif Lindholm 19864d2dc38SLeif Lindholm type = instr & TYPE_SWPB; 19964d2dc38SLeif Lindholm 20064d2dc38SLeif Lindholm pr_debug("addr in r%d->0x%08x, dest is r%d, source in r%d->0x%08x)\n", 20164d2dc38SLeif Lindholm EXTRACT_REG_NUM(instr, RN_OFFSET), address, 20264d2dc38SLeif Lindholm destreg, EXTRACT_REG_NUM(instr, RT2_OFFSET), data); 20364d2dc38SLeif Lindholm 20464d2dc38SLeif Lindholm /* Check access in reasonable access range for both SWP and SWPB */ 20564d2dc38SLeif Lindholm if (!access_ok(VERIFY_WRITE, (address & ~3), 4)) { 20664d2dc38SLeif Lindholm pr_debug("SWP{B} emulation: access to %p not allowed!\n", 20764d2dc38SLeif Lindholm (void *)address); 20864d2dc38SLeif Lindholm res = -EFAULT; 20964d2dc38SLeif Lindholm } else { 21064d2dc38SLeif Lindholm res = emulate_swpX(address, &data, type); 21164d2dc38SLeif Lindholm } 21264d2dc38SLeif Lindholm 21364d2dc38SLeif Lindholm if (res == 0) { 21464d2dc38SLeif Lindholm /* 21564d2dc38SLeif Lindholm * On successful emulation, revert the adjustment to the PC 21664d2dc38SLeif Lindholm * made in kernel/traps.c in order to resume execution at the 21764d2dc38SLeif Lindholm * instruction following the SWP{B}. 21864d2dc38SLeif Lindholm */ 21964d2dc38SLeif Lindholm regs->ARM_pc += 4; 22064d2dc38SLeif Lindholm regs->uregs[destreg] = data; 22164d2dc38SLeif Lindholm } else if (res == -EFAULT) { 22264d2dc38SLeif Lindholm /* 22364d2dc38SLeif Lindholm * Memory errors do not mean emulation failed. 22464d2dc38SLeif Lindholm * Set up signal info to return SEGV, then return OK 22564d2dc38SLeif Lindholm */ 22664d2dc38SLeif Lindholm set_segfault(regs, address); 22764d2dc38SLeif Lindholm } 22864d2dc38SLeif Lindholm 22964d2dc38SLeif Lindholm return 0; 23064d2dc38SLeif Lindholm } 23164d2dc38SLeif Lindholm 23264d2dc38SLeif Lindholm /* 23364d2dc38SLeif Lindholm * Only emulate SWP/SWPB executed in ARM state/User mode. 23464d2dc38SLeif Lindholm * The kernel must be SWP free and SWP{B} does not exist in Thumb/ThumbEE. 23564d2dc38SLeif Lindholm */ 23664d2dc38SLeif Lindholm static struct undef_hook swp_hook = { 23764d2dc38SLeif Lindholm .instr_mask = 0x0fb00ff0, 23864d2dc38SLeif Lindholm .instr_val = 0x01000090, 23964d2dc38SLeif Lindholm .cpsr_mask = MODE_MASK | PSR_T_BIT | PSR_J_BIT, 24064d2dc38SLeif Lindholm .cpsr_val = USR_MODE, 24164d2dc38SLeif Lindholm .fn = swp_handler 24264d2dc38SLeif Lindholm }; 24364d2dc38SLeif Lindholm 24464d2dc38SLeif Lindholm /* 24564d2dc38SLeif Lindholm * Register handler and create status file in /proc/cpu 24664d2dc38SLeif Lindholm * Invoked as late_initcall, since not needed before init spawned. 24764d2dc38SLeif Lindholm */ 24864d2dc38SLeif Lindholm static int __init swp_emulation_init(void) 24964d2dc38SLeif Lindholm { 25064d2dc38SLeif Lindholm #ifdef CONFIG_PROC_FS 25164d2dc38SLeif Lindholm struct proc_dir_entry *res; 25264d2dc38SLeif Lindholm 25364d2dc38SLeif Lindholm res = create_proc_entry("cpu/swp_emulation", S_IRUGO, NULL); 25464d2dc38SLeif Lindholm 25564d2dc38SLeif Lindholm if (!res) 25664d2dc38SLeif Lindholm return -ENOMEM; 25764d2dc38SLeif Lindholm 25864d2dc38SLeif Lindholm res->read_proc = proc_read_status; 25964d2dc38SLeif Lindholm #endif /* CONFIG_PROC_FS */ 26064d2dc38SLeif Lindholm 26164d2dc38SLeif Lindholm printk(KERN_NOTICE "Registering SWP/SWPB emulation handler\n"); 26264d2dc38SLeif Lindholm register_undef_hook(&swp_hook); 26364d2dc38SLeif Lindholm 26464d2dc38SLeif Lindholm return 0; 26564d2dc38SLeif Lindholm } 26664d2dc38SLeif Lindholm 26764d2dc38SLeif Lindholm late_initcall(swp_emulation_init); 268