0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #include <linux/init.h>
0019 #include <linux/kernel.h>
0020 #include <linux/proc_fs.h>
0021 #include <linux/seq_file.h>
0022 #include <linux/sched.h>
0023 #include <linux/sched/mm.h>
0024 #include <linux/syscalls.h>
0025 #include <linux/perf_event.h>
0026
0027 #include <asm/opcodes.h>
0028 #include <asm/system_info.h>
0029 #include <asm/traps.h>
0030 #include <linux/uaccess.h>
0031
0032
0033
0034
0035 #define __user_swpX_asm(data, addr, res, temp, B) \
0036 __asm__ __volatile__( \
0037 "0: ldrex"B" %2, [%3]\n" \
0038 "1: strex"B" %0, %1, [%3]\n" \
0039 " cmp %0, #0\n" \
0040 " moveq %1, %2\n" \
0041 " movne %0, %4\n" \
0042 "2:\n" \
0043 " .section .text.fixup,\"ax\"\n" \
0044 " .align 2\n" \
0045 "3: mov %0, %5\n" \
0046 " b 2b\n" \
0047 " .previous\n" \
0048 " .section __ex_table,\"a\"\n" \
0049 " .align 3\n" \
0050 " .long 0b, 3b\n" \
0051 " .long 1b, 3b\n" \
0052 " .previous" \
0053 : "=&r" (res), "+r" (data), "=&r" (temp) \
0054 : "r" (addr), "i" (-EAGAIN), "i" (-EFAULT) \
0055 : "cc", "memory")
0056
0057 #define __user_swp_asm(data, addr, res, temp) \
0058 __user_swpX_asm(data, addr, res, temp, "")
0059 #define __user_swpb_asm(data, addr, res, temp) \
0060 __user_swpX_asm(data, addr, res, temp, "b")
0061
0062
0063
0064
0065 #define EXTRACT_REG_NUM(instruction, offset) \
0066 (((instruction) & (0xf << (offset))) >> (offset))
0067 #define RN_OFFSET 16
0068 #define RT_OFFSET 12
0069 #define RT2_OFFSET 0
0070
0071
0072
0073
0074 #define TYPE_SWPB (1 << 22)
0075
0076 static unsigned long swpcounter;
0077 static unsigned long swpbcounter;
0078 static unsigned long abtcounter;
0079 static pid_t previous_pid;
0080
0081 #ifdef CONFIG_PROC_FS
0082 static int proc_status_show(struct seq_file *m, void *v)
0083 {
0084 seq_printf(m, "Emulated SWP:\t\t%lu\n", swpcounter);
0085 seq_printf(m, "Emulated SWPB:\t\t%lu\n", swpbcounter);
0086 seq_printf(m, "Aborted SWP{B}:\t\t%lu\n", abtcounter);
0087 if (previous_pid != 0)
0088 seq_printf(m, "Last process:\t\t%d\n", previous_pid);
0089 return 0;
0090 }
0091 #endif
0092
0093
0094
0095
0096 static void set_segfault(struct pt_regs *regs, unsigned long addr)
0097 {
0098 int si_code;
0099
0100 mmap_read_lock(current->mm);
0101 if (find_vma(current->mm, addr) == NULL)
0102 si_code = SEGV_MAPERR;
0103 else
0104 si_code = SEGV_ACCERR;
0105 mmap_read_unlock(current->mm);
0106
0107 pr_debug("SWP{B} emulation: access caused memory abort!\n");
0108 arm_notify_die("Illegal memory access", regs,
0109 SIGSEGV, si_code,
0110 (void __user *)instruction_pointer(regs),
0111 0, 0);
0112
0113 abtcounter++;
0114 }
0115
0116 static int emulate_swpX(unsigned int address, unsigned int *data,
0117 unsigned int type)
0118 {
0119 unsigned int res = 0;
0120
0121 if ((type != TYPE_SWPB) && (address & 0x3)) {
0122
0123 pr_debug("SWP instruction on unaligned pointer!\n");
0124 return -EFAULT;
0125 }
0126
0127 while (1) {
0128 unsigned long temp;
0129 unsigned int __ua_flags;
0130
0131 __ua_flags = uaccess_save_and_enable();
0132 if (type == TYPE_SWPB)
0133 __user_swpb_asm(*data, address, res, temp);
0134 else
0135 __user_swp_asm(*data, address, res, temp);
0136 uaccess_restore(__ua_flags);
0137
0138 if (likely(res != -EAGAIN) || signal_pending(current))
0139 break;
0140
0141 cond_resched();
0142 }
0143
0144 if (res == 0) {
0145 if (type == TYPE_SWPB)
0146 swpbcounter++;
0147 else
0148 swpcounter++;
0149 }
0150
0151 return res;
0152 }
0153
0154
0155
0156
0157
0158
0159 static int swp_handler(struct pt_regs *regs, unsigned int instr)
0160 {
0161 unsigned int address, destreg, data, type;
0162 unsigned int res = 0;
0163
0164 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, regs->ARM_pc);
0165
0166 res = arm_check_condition(instr, regs->ARM_cpsr);
0167 switch (res) {
0168 case ARM_OPCODE_CONDTEST_PASS:
0169 break;
0170 case ARM_OPCODE_CONDTEST_FAIL:
0171
0172 regs->ARM_pc += 4;
0173 return 0;
0174 case ARM_OPCODE_CONDTEST_UNCOND:
0175
0176 return -EFAULT;
0177 default:
0178 return -EINVAL;
0179 }
0180
0181 if (current->pid != previous_pid) {
0182 pr_debug("\"%s\" (%ld) uses deprecated SWP{B} instruction\n",
0183 current->comm, (unsigned long)current->pid);
0184 previous_pid = current->pid;
0185 }
0186
0187 address = regs->uregs[EXTRACT_REG_NUM(instr, RN_OFFSET)];
0188 data = regs->uregs[EXTRACT_REG_NUM(instr, RT2_OFFSET)];
0189 destreg = EXTRACT_REG_NUM(instr, RT_OFFSET);
0190
0191 type = instr & TYPE_SWPB;
0192
0193 pr_debug("addr in r%d->0x%08x, dest is r%d, source in r%d->0x%08x)\n",
0194 EXTRACT_REG_NUM(instr, RN_OFFSET), address,
0195 destreg, EXTRACT_REG_NUM(instr, RT2_OFFSET), data);
0196
0197
0198 if (!access_ok((void __user *)(address & ~3), 4)) {
0199 pr_debug("SWP{B} emulation: access to %p not allowed!\n",
0200 (void *)address);
0201 res = -EFAULT;
0202 } else {
0203 res = emulate_swpX(address, &data, type);
0204 }
0205
0206 if (res == 0) {
0207
0208
0209
0210
0211
0212 regs->ARM_pc += 4;
0213 regs->uregs[destreg] = data;
0214 } else if (res == -EFAULT) {
0215
0216
0217
0218
0219 set_segfault(regs, address);
0220 }
0221
0222 return 0;
0223 }
0224
0225
0226
0227
0228
0229 static struct undef_hook swp_hook = {
0230 .instr_mask = 0x0fb00ff0,
0231 .instr_val = 0x01000090,
0232 .cpsr_mask = MODE_MASK | PSR_T_BIT | PSR_J_BIT,
0233 .cpsr_val = USR_MODE,
0234 .fn = swp_handler
0235 };
0236
0237
0238
0239
0240
0241 static int __init swp_emulation_init(void)
0242 {
0243 if (cpu_architecture() < CPU_ARCH_ARMv7)
0244 return 0;
0245
0246 #ifdef CONFIG_PROC_FS
0247 if (!proc_create_single("cpu/swp_emulation", S_IRUGO, NULL,
0248 proc_status_show))
0249 return -ENOMEM;
0250 #endif
0251
0252 pr_notice("Registering SWP/SWPB emulation handler\n");
0253 register_undef_hook(&swp_hook);
0254
0255 return 0;
0256 }
0257
0258 late_initcall(swp_emulation_init);