armv8_deprecated.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. * Copyright (C) 2014 ARM Limited
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/cpu.h>
  9. #include <linux/init.h>
  10. #include <linux/list.h>
  11. #include <linux/perf_event.h>
  12. #include <linux/sched.h>
  13. #include <linux/slab.h>
  14. #include <linux/sysctl.h>
  15. #include <asm/insn.h>
  16. #include <asm/opcodes.h>
  17. #include <asm/system_misc.h>
  18. #include <asm/traps.h>
  19. #include <asm/uaccess.h>
  20. #define CREATE_TRACE_POINTS
  21. #include "trace-events-emulation.h"
  22. /*
  23. * The runtime support for deprecated instruction support can be in one of
  24. * following three states -
  25. *
  26. * 0 = undef
  27. * 1 = emulate (software emulation)
  28. * 2 = hw (supported in hardware)
  29. */
  30. enum insn_emulation_mode {
  31. INSN_UNDEF,
  32. INSN_EMULATE,
  33. INSN_HW,
  34. };
  35. enum legacy_insn_status {
  36. INSN_DEPRECATED,
  37. INSN_OBSOLETE,
  38. };
  39. struct insn_emulation_ops {
  40. const char *name;
  41. enum legacy_insn_status status;
  42. struct undef_hook *hooks;
  43. int (*set_hw_mode)(bool enable);
  44. };
  45. struct insn_emulation {
  46. struct list_head node;
  47. struct insn_emulation_ops *ops;
  48. int current_mode;
  49. int min;
  50. int max;
  51. };
  52. static LIST_HEAD(insn_emulation);
  53. static int nr_insn_emulated;
  54. static DEFINE_RAW_SPINLOCK(insn_emulation_lock);
  55. static void register_emulation_hooks(struct insn_emulation_ops *ops)
  56. {
  57. struct undef_hook *hook;
  58. BUG_ON(!ops->hooks);
  59. for (hook = ops->hooks; hook->instr_mask; hook++)
  60. register_undef_hook(hook);
  61. pr_notice("Registered %s emulation handler\n", ops->name);
  62. }
  63. static void remove_emulation_hooks(struct insn_emulation_ops *ops)
  64. {
  65. struct undef_hook *hook;
  66. BUG_ON(!ops->hooks);
  67. for (hook = ops->hooks; hook->instr_mask; hook++)
  68. unregister_undef_hook(hook);
  69. pr_notice("Removed %s emulation handler\n", ops->name);
  70. }
  71. static int update_insn_emulation_mode(struct insn_emulation *insn,
  72. enum insn_emulation_mode prev)
  73. {
  74. int ret = 0;
  75. switch (prev) {
  76. case INSN_UNDEF: /* Nothing to be done */
  77. break;
  78. case INSN_EMULATE:
  79. remove_emulation_hooks(insn->ops);
  80. break;
  81. case INSN_HW:
  82. if (insn->ops->set_hw_mode) {
  83. insn->ops->set_hw_mode(false);
  84. pr_notice("Disabled %s support\n", insn->ops->name);
  85. }
  86. break;
  87. }
  88. switch (insn->current_mode) {
  89. case INSN_UNDEF:
  90. break;
  91. case INSN_EMULATE:
  92. register_emulation_hooks(insn->ops);
  93. break;
  94. case INSN_HW:
  95. if (insn->ops->set_hw_mode && insn->ops->set_hw_mode(true))
  96. pr_notice("Enabled %s support\n", insn->ops->name);
  97. else
  98. ret = -EINVAL;
  99. break;
  100. }
  101. return ret;
  102. }
  103. static void register_insn_emulation(struct insn_emulation_ops *ops)
  104. {
  105. unsigned long flags;
  106. struct insn_emulation *insn;
  107. insn = kzalloc(sizeof(*insn), GFP_KERNEL);
  108. insn->ops = ops;
  109. insn->min = INSN_UNDEF;
  110. switch (ops->status) {
  111. case INSN_DEPRECATED:
  112. insn->current_mode = INSN_EMULATE;
  113. insn->max = INSN_HW;
  114. break;
  115. case INSN_OBSOLETE:
  116. insn->current_mode = INSN_UNDEF;
  117. insn->max = INSN_EMULATE;
  118. break;
  119. }
  120. raw_spin_lock_irqsave(&insn_emulation_lock, flags);
  121. list_add(&insn->node, &insn_emulation);
  122. nr_insn_emulated++;
  123. raw_spin_unlock_irqrestore(&insn_emulation_lock, flags);
  124. /* Register any handlers if required */
  125. update_insn_emulation_mode(insn, INSN_UNDEF);
  126. }
  127. static int emulation_proc_handler(struct ctl_table *table, int write,
  128. void __user *buffer, size_t *lenp,
  129. loff_t *ppos)
  130. {
  131. int ret = 0;
  132. struct insn_emulation *insn = (struct insn_emulation *) table->data;
  133. enum insn_emulation_mode prev_mode = insn->current_mode;
  134. table->data = &insn->current_mode;
  135. ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  136. if (ret || !write || prev_mode == insn->current_mode)
  137. goto ret;
  138. ret = update_insn_emulation_mode(insn, prev_mode);
  139. if (ret) {
  140. /* Mode change failed, revert to previous mode. */
  141. insn->current_mode = prev_mode;
  142. update_insn_emulation_mode(insn, INSN_UNDEF);
  143. }
  144. ret:
  145. table->data = insn;
  146. return ret;
  147. }
  148. static struct ctl_table ctl_abi[] = {
  149. {
  150. .procname = "abi",
  151. .mode = 0555,
  152. },
  153. { }
  154. };
  155. static void register_insn_emulation_sysctl(struct ctl_table *table)
  156. {
  157. unsigned long flags;
  158. int i = 0;
  159. struct insn_emulation *insn;
  160. struct ctl_table *insns_sysctl, *sysctl;
  161. insns_sysctl = kzalloc(sizeof(*sysctl) * (nr_insn_emulated + 1),
  162. GFP_KERNEL);
  163. raw_spin_lock_irqsave(&insn_emulation_lock, flags);
  164. list_for_each_entry(insn, &insn_emulation, node) {
  165. sysctl = &insns_sysctl[i];
  166. sysctl->mode = 0644;
  167. sysctl->maxlen = sizeof(int);
  168. sysctl->procname = insn->ops->name;
  169. sysctl->data = insn;
  170. sysctl->extra1 = &insn->min;
  171. sysctl->extra2 = &insn->max;
  172. sysctl->proc_handler = emulation_proc_handler;
  173. i++;
  174. }
  175. raw_spin_unlock_irqrestore(&insn_emulation_lock, flags);
  176. table->child = insns_sysctl;
  177. register_sysctl_table(table);
  178. }
  179. /*
  180. * Implement emulation of the SWP/SWPB instructions using load-exclusive and
  181. * store-exclusive.
  182. *
  183. * Syntax of SWP{B} instruction: SWP{B}<c> <Rt>, <Rt2>, [<Rn>]
  184. * Where: Rt = destination
  185. * Rt2 = source
  186. * Rn = address
  187. */
  188. /*
  189. * Error-checking SWP macros implemented using ldxr{b}/stxr{b}
  190. */
  191. #define __user_swpX_asm(data, addr, res, temp, B) \
  192. __asm__ __volatile__( \
  193. " mov %w2, %w1\n" \
  194. "0: ldxr"B" %w1, [%3]\n" \
  195. "1: stxr"B" %w0, %w2, [%3]\n" \
  196. " cbz %w0, 2f\n" \
  197. " mov %w0, %w4\n" \
  198. "2:\n" \
  199. " .pushsection .fixup,\"ax\"\n" \
  200. " .align 2\n" \
  201. "3: mov %w0, %w5\n" \
  202. " b 2b\n" \
  203. " .popsection" \
  204. " .pushsection __ex_table,\"a\"\n" \
  205. " .align 3\n" \
  206. " .quad 0b, 3b\n" \
  207. " .quad 1b, 3b\n" \
  208. " .popsection" \
  209. : "=&r" (res), "+r" (data), "=&r" (temp) \
  210. : "r" (addr), "i" (-EAGAIN), "i" (-EFAULT) \
  211. : "memory")
  212. #define __user_swp_asm(data, addr, res, temp) \
  213. __user_swpX_asm(data, addr, res, temp, "")
  214. #define __user_swpb_asm(data, addr, res, temp) \
  215. __user_swpX_asm(data, addr, res, temp, "b")
  216. /*
  217. * Bit 22 of the instruction encoding distinguishes between
  218. * the SWP and SWPB variants (bit set means SWPB).
  219. */
  220. #define TYPE_SWPB (1 << 22)
  221. /*
  222. * Set up process info to signal segmentation fault - called on access error.
  223. */
  224. static void set_segfault(struct pt_regs *regs, unsigned long addr)
  225. {
  226. siginfo_t info;
  227. down_read(&current->mm->mmap_sem);
  228. if (find_vma(current->mm, addr) == NULL)
  229. info.si_code = SEGV_MAPERR;
  230. else
  231. info.si_code = SEGV_ACCERR;
  232. up_read(&current->mm->mmap_sem);
  233. info.si_signo = SIGSEGV;
  234. info.si_errno = 0;
  235. info.si_addr = (void *) instruction_pointer(regs);
  236. pr_debug("SWP{B} emulation: access caused memory abort!\n");
  237. arm64_notify_die("Illegal memory access", regs, &info, 0);
  238. }
  239. static int emulate_swpX(unsigned int address, unsigned int *data,
  240. unsigned int type)
  241. {
  242. unsigned int res = 0;
  243. if ((type != TYPE_SWPB) && (address & 0x3)) {
  244. /* SWP to unaligned address not permitted */
  245. pr_debug("SWP instruction on unaligned pointer!\n");
  246. return -EFAULT;
  247. }
  248. while (1) {
  249. unsigned long temp;
  250. if (type == TYPE_SWPB)
  251. __user_swpb_asm(*data, address, res, temp);
  252. else
  253. __user_swp_asm(*data, address, res, temp);
  254. if (likely(res != -EAGAIN) || signal_pending(current))
  255. break;
  256. cond_resched();
  257. }
  258. return res;
  259. }
  260. /*
  261. * swp_handler logs the id of calling process, dissects the instruction, sanity
  262. * checks the memory location, calls emulate_swpX for the actual operation and
  263. * deals with fixup/error handling before returning
  264. */
  265. static int swp_handler(struct pt_regs *regs, u32 instr)
  266. {
  267. u32 destreg, data, type, address = 0;
  268. int rn, rt2, res = 0;
  269. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, regs->pc);
  270. type = instr & TYPE_SWPB;
  271. switch (arm_check_condition(instr, regs->pstate)) {
  272. case ARM_OPCODE_CONDTEST_PASS:
  273. break;
  274. case ARM_OPCODE_CONDTEST_FAIL:
  275. /* Condition failed - return to next instruction */
  276. goto ret;
  277. case ARM_OPCODE_CONDTEST_UNCOND:
  278. /* If unconditional encoding - not a SWP, undef */
  279. return -EFAULT;
  280. default:
  281. return -EINVAL;
  282. }
  283. rn = aarch32_insn_extract_reg_num(instr, A32_RN_OFFSET);
  284. rt2 = aarch32_insn_extract_reg_num(instr, A32_RT2_OFFSET);
  285. address = (u32)regs->user_regs.regs[rn];
  286. data = (u32)regs->user_regs.regs[rt2];
  287. destreg = aarch32_insn_extract_reg_num(instr, A32_RT_OFFSET);
  288. pr_debug("addr in r%d->0x%08x, dest is r%d, source in r%d->0x%08x)\n",
  289. rn, address, destreg,
  290. aarch32_insn_extract_reg_num(instr, A32_RT2_OFFSET), data);
  291. /* Check access in reasonable access range for both SWP and SWPB */
  292. if (!access_ok(VERIFY_WRITE, (address & ~3), 4)) {
  293. pr_debug("SWP{B} emulation: access to 0x%08x not allowed!\n",
  294. address);
  295. goto fault;
  296. }
  297. res = emulate_swpX(address, &data, type);
  298. if (res == -EFAULT)
  299. goto fault;
  300. else if (res == 0)
  301. regs->user_regs.regs[destreg] = data;
  302. ret:
  303. if (type == TYPE_SWPB)
  304. trace_instruction_emulation("swpb", regs->pc);
  305. else
  306. trace_instruction_emulation("swp", regs->pc);
  307. pr_warn_ratelimited("\"%s\" (%ld) uses obsolete SWP{B} instruction at 0x%llx\n",
  308. current->comm, (unsigned long)current->pid, regs->pc);
  309. regs->pc += 4;
  310. return 0;
  311. fault:
  312. set_segfault(regs, address);
  313. return 0;
  314. }
  315. /*
  316. * Only emulate SWP/SWPB executed in ARM state/User mode.
  317. * The kernel must be SWP free and SWP{B} does not exist in Thumb.
  318. */
  319. static struct undef_hook swp_hooks[] = {
  320. {
  321. .instr_mask = 0x0fb00ff0,
  322. .instr_val = 0x01000090,
  323. .pstate_mask = COMPAT_PSR_MODE_MASK,
  324. .pstate_val = COMPAT_PSR_MODE_USR,
  325. .fn = swp_handler
  326. },
  327. { }
  328. };
  329. static struct insn_emulation_ops swp_ops = {
  330. .name = "swp",
  331. .status = INSN_OBSOLETE,
  332. .hooks = swp_hooks,
  333. .set_hw_mode = NULL,
  334. };
  335. static int cp15barrier_handler(struct pt_regs *regs, u32 instr)
  336. {
  337. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, regs->pc);
  338. switch (arm_check_condition(instr, regs->pstate)) {
  339. case ARM_OPCODE_CONDTEST_PASS:
  340. break;
  341. case ARM_OPCODE_CONDTEST_FAIL:
  342. /* Condition failed - return to next instruction */
  343. goto ret;
  344. case ARM_OPCODE_CONDTEST_UNCOND:
  345. /* If unconditional encoding - not a barrier instruction */
  346. return -EFAULT;
  347. default:
  348. return -EINVAL;
  349. }
  350. switch (aarch32_insn_mcr_extract_crm(instr)) {
  351. case 10:
  352. /*
  353. * dmb - mcr p15, 0, Rt, c7, c10, 5
  354. * dsb - mcr p15, 0, Rt, c7, c10, 4
  355. */
  356. if (aarch32_insn_mcr_extract_opc2(instr) == 5) {
  357. dmb(sy);
  358. trace_instruction_emulation(
  359. "mcr p15, 0, Rt, c7, c10, 5 ; dmb", regs->pc);
  360. } else {
  361. dsb(sy);
  362. trace_instruction_emulation(
  363. "mcr p15, 0, Rt, c7, c10, 4 ; dsb", regs->pc);
  364. }
  365. break;
  366. case 5:
  367. /*
  368. * isb - mcr p15, 0, Rt, c7, c5, 4
  369. *
  370. * Taking an exception or returning from one acts as an
  371. * instruction barrier. So no explicit barrier needed here.
  372. */
  373. trace_instruction_emulation(
  374. "mcr p15, 0, Rt, c7, c5, 4 ; isb", regs->pc);
  375. break;
  376. }
  377. ret:
  378. pr_warn_ratelimited("\"%s\" (%ld) uses deprecated CP15 Barrier instruction at 0x%llx\n",
  379. current->comm, (unsigned long)current->pid, regs->pc);
  380. regs->pc += 4;
  381. return 0;
  382. }
  383. #define SCTLR_EL1_CP15BEN (1 << 5)
  384. static inline void config_sctlr_el1(u32 clear, u32 set)
  385. {
  386. u32 val;
  387. asm volatile("mrs %0, sctlr_el1" : "=r" (val));
  388. val &= ~clear;
  389. val |= set;
  390. asm volatile("msr sctlr_el1, %0" : : "r" (val));
  391. }
  392. static void enable_cp15_ben(void *info)
  393. {
  394. config_sctlr_el1(0, SCTLR_EL1_CP15BEN);
  395. }
  396. static void disable_cp15_ben(void *info)
  397. {
  398. config_sctlr_el1(SCTLR_EL1_CP15BEN, 0);
  399. }
  400. static int cpu_hotplug_notify(struct notifier_block *b,
  401. unsigned long action, void *hcpu)
  402. {
  403. switch (action) {
  404. case CPU_STARTING:
  405. case CPU_STARTING_FROZEN:
  406. enable_cp15_ben(NULL);
  407. return NOTIFY_DONE;
  408. case CPU_DYING:
  409. case CPU_DYING_FROZEN:
  410. disable_cp15_ben(NULL);
  411. return NOTIFY_DONE;
  412. }
  413. return NOTIFY_OK;
  414. }
  415. static struct notifier_block cpu_hotplug_notifier = {
  416. .notifier_call = cpu_hotplug_notify,
  417. };
  418. static int cp15_barrier_set_hw_mode(bool enable)
  419. {
  420. if (enable) {
  421. register_cpu_notifier(&cpu_hotplug_notifier);
  422. on_each_cpu(enable_cp15_ben, NULL, true);
  423. } else {
  424. unregister_cpu_notifier(&cpu_hotplug_notifier);
  425. on_each_cpu(disable_cp15_ben, NULL, true);
  426. }
  427. return true;
  428. }
  429. static struct undef_hook cp15_barrier_hooks[] = {
  430. {
  431. .instr_mask = 0x0fff0fdf,
  432. .instr_val = 0x0e070f9a,
  433. .pstate_mask = COMPAT_PSR_MODE_MASK,
  434. .pstate_val = COMPAT_PSR_MODE_USR,
  435. .fn = cp15barrier_handler,
  436. },
  437. {
  438. .instr_mask = 0x0fff0fff,
  439. .instr_val = 0x0e070f95,
  440. .pstate_mask = COMPAT_PSR_MODE_MASK,
  441. .pstate_val = COMPAT_PSR_MODE_USR,
  442. .fn = cp15barrier_handler,
  443. },
  444. { }
  445. };
  446. static struct insn_emulation_ops cp15_barrier_ops = {
  447. .name = "cp15_barrier",
  448. .status = INSN_DEPRECATED,
  449. .hooks = cp15_barrier_hooks,
  450. .set_hw_mode = cp15_barrier_set_hw_mode,
  451. };
  452. /*
  453. * Invoked as late_initcall, since not needed before init spawned.
  454. */
  455. static int __init armv8_deprecated_init(void)
  456. {
  457. if (IS_ENABLED(CONFIG_SWP_EMULATION))
  458. register_insn_emulation(&swp_ops);
  459. if (IS_ENABLED(CONFIG_CP15_BARRIER_EMULATION))
  460. register_insn_emulation(&cp15_barrier_ops);
  461. register_insn_emulation_sysctl(ctl_abi);
  462. return 0;
  463. }
  464. late_initcall(armv8_deprecated_init);