armv8_deprecated.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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/alternative.h>
  16. #include <asm/cpufeature.h>
  17. #include <asm/insn.h>
  18. #include <asm/opcodes.h>
  19. #include <asm/sysreg.h>
  20. #include <asm/system_misc.h>
  21. #include <asm/traps.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/cpufeature.h>
  24. #define CREATE_TRACE_POINTS
  25. #include "trace-events-emulation.h"
  26. /*
  27. * The runtime support for deprecated instruction support can be in one of
  28. * following three states -
  29. *
  30. * 0 = undef
  31. * 1 = emulate (software emulation)
  32. * 2 = hw (supported in hardware)
  33. */
  34. enum insn_emulation_mode {
  35. INSN_UNDEF,
  36. INSN_EMULATE,
  37. INSN_HW,
  38. };
  39. enum legacy_insn_status {
  40. INSN_DEPRECATED,
  41. INSN_OBSOLETE,
  42. };
  43. struct insn_emulation_ops {
  44. const char *name;
  45. enum legacy_insn_status status;
  46. struct undef_hook *hooks;
  47. int (*set_hw_mode)(bool enable);
  48. };
  49. struct insn_emulation {
  50. struct list_head node;
  51. struct insn_emulation_ops *ops;
  52. int current_mode;
  53. int min;
  54. int max;
  55. };
  56. static LIST_HEAD(insn_emulation);
  57. static int nr_insn_emulated __initdata;
  58. static DEFINE_RAW_SPINLOCK(insn_emulation_lock);
  59. static void register_emulation_hooks(struct insn_emulation_ops *ops)
  60. {
  61. struct undef_hook *hook;
  62. BUG_ON(!ops->hooks);
  63. for (hook = ops->hooks; hook->instr_mask; hook++)
  64. register_undef_hook(hook);
  65. pr_notice("Registered %s emulation handler\n", ops->name);
  66. }
  67. static void remove_emulation_hooks(struct insn_emulation_ops *ops)
  68. {
  69. struct undef_hook *hook;
  70. BUG_ON(!ops->hooks);
  71. for (hook = ops->hooks; hook->instr_mask; hook++)
  72. unregister_undef_hook(hook);
  73. pr_notice("Removed %s emulation handler\n", ops->name);
  74. }
  75. static void enable_insn_hw_mode(void *data)
  76. {
  77. struct insn_emulation *insn = (struct insn_emulation *)data;
  78. if (insn->ops->set_hw_mode)
  79. insn->ops->set_hw_mode(true);
  80. }
  81. static void disable_insn_hw_mode(void *data)
  82. {
  83. struct insn_emulation *insn = (struct insn_emulation *)data;
  84. if (insn->ops->set_hw_mode)
  85. insn->ops->set_hw_mode(false);
  86. }
  87. /* Run set_hw_mode(mode) on all active CPUs */
  88. static int run_all_cpu_set_hw_mode(struct insn_emulation *insn, bool enable)
  89. {
  90. if (!insn->ops->set_hw_mode)
  91. return -EINVAL;
  92. if (enable)
  93. on_each_cpu(enable_insn_hw_mode, (void *)insn, true);
  94. else
  95. on_each_cpu(disable_insn_hw_mode, (void *)insn, true);
  96. return 0;
  97. }
  98. /*
  99. * Run set_hw_mode for all insns on a starting CPU.
  100. * Returns:
  101. * 0 - If all the hooks ran successfully.
  102. * -EINVAL - At least one hook is not supported by the CPU.
  103. */
  104. static int run_all_insn_set_hw_mode(unsigned int cpu)
  105. {
  106. int rc = 0;
  107. unsigned long flags;
  108. struct insn_emulation *insn;
  109. raw_spin_lock_irqsave(&insn_emulation_lock, flags);
  110. list_for_each_entry(insn, &insn_emulation, node) {
  111. bool enable = (insn->current_mode == INSN_HW);
  112. if (insn->ops->set_hw_mode && insn->ops->set_hw_mode(enable)) {
  113. pr_warn("CPU[%u] cannot support the emulation of %s",
  114. cpu, insn->ops->name);
  115. rc = -EINVAL;
  116. }
  117. }
  118. raw_spin_unlock_irqrestore(&insn_emulation_lock, flags);
  119. return rc;
  120. }
  121. static int update_insn_emulation_mode(struct insn_emulation *insn,
  122. enum insn_emulation_mode prev)
  123. {
  124. int ret = 0;
  125. switch (prev) {
  126. case INSN_UNDEF: /* Nothing to be done */
  127. break;
  128. case INSN_EMULATE:
  129. remove_emulation_hooks(insn->ops);
  130. break;
  131. case INSN_HW:
  132. if (!run_all_cpu_set_hw_mode(insn, false))
  133. pr_notice("Disabled %s support\n", insn->ops->name);
  134. break;
  135. }
  136. switch (insn->current_mode) {
  137. case INSN_UNDEF:
  138. break;
  139. case INSN_EMULATE:
  140. register_emulation_hooks(insn->ops);
  141. break;
  142. case INSN_HW:
  143. ret = run_all_cpu_set_hw_mode(insn, true);
  144. if (!ret)
  145. pr_notice("Enabled %s support\n", insn->ops->name);
  146. break;
  147. }
  148. return ret;
  149. }
  150. static void __init register_insn_emulation(struct insn_emulation_ops *ops)
  151. {
  152. unsigned long flags;
  153. struct insn_emulation *insn;
  154. insn = kzalloc(sizeof(*insn), GFP_KERNEL);
  155. insn->ops = ops;
  156. insn->min = INSN_UNDEF;
  157. switch (ops->status) {
  158. case INSN_DEPRECATED:
  159. insn->current_mode = INSN_EMULATE;
  160. /* Disable the HW mode if it was turned on at early boot time */
  161. run_all_cpu_set_hw_mode(insn, false);
  162. insn->max = INSN_HW;
  163. break;
  164. case INSN_OBSOLETE:
  165. insn->current_mode = INSN_UNDEF;
  166. insn->max = INSN_EMULATE;
  167. break;
  168. }
  169. raw_spin_lock_irqsave(&insn_emulation_lock, flags);
  170. list_add(&insn->node, &insn_emulation);
  171. nr_insn_emulated++;
  172. raw_spin_unlock_irqrestore(&insn_emulation_lock, flags);
  173. /* Register any handlers if required */
  174. update_insn_emulation_mode(insn, INSN_UNDEF);
  175. }
  176. static int emulation_proc_handler(struct ctl_table *table, int write,
  177. void __user *buffer, size_t *lenp,
  178. loff_t *ppos)
  179. {
  180. int ret = 0;
  181. struct insn_emulation *insn = (struct insn_emulation *) table->data;
  182. enum insn_emulation_mode prev_mode = insn->current_mode;
  183. table->data = &insn->current_mode;
  184. ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  185. if (ret || !write || prev_mode == insn->current_mode)
  186. goto ret;
  187. ret = update_insn_emulation_mode(insn, prev_mode);
  188. if (ret) {
  189. /* Mode change failed, revert to previous mode. */
  190. insn->current_mode = prev_mode;
  191. update_insn_emulation_mode(insn, INSN_UNDEF);
  192. }
  193. ret:
  194. table->data = insn;
  195. return ret;
  196. }
  197. static struct ctl_table ctl_abi[] = {
  198. {
  199. .procname = "abi",
  200. .mode = 0555,
  201. },
  202. { }
  203. };
  204. static void __init register_insn_emulation_sysctl(struct ctl_table *table)
  205. {
  206. unsigned long flags;
  207. int i = 0;
  208. struct insn_emulation *insn;
  209. struct ctl_table *insns_sysctl, *sysctl;
  210. insns_sysctl = kzalloc(sizeof(*sysctl) * (nr_insn_emulated + 1),
  211. GFP_KERNEL);
  212. raw_spin_lock_irqsave(&insn_emulation_lock, flags);
  213. list_for_each_entry(insn, &insn_emulation, node) {
  214. sysctl = &insns_sysctl[i];
  215. sysctl->mode = 0644;
  216. sysctl->maxlen = sizeof(int);
  217. sysctl->procname = insn->ops->name;
  218. sysctl->data = insn;
  219. sysctl->extra1 = &insn->min;
  220. sysctl->extra2 = &insn->max;
  221. sysctl->proc_handler = emulation_proc_handler;
  222. i++;
  223. }
  224. raw_spin_unlock_irqrestore(&insn_emulation_lock, flags);
  225. table->child = insns_sysctl;
  226. register_sysctl_table(table);
  227. }
  228. /*
  229. * Implement emulation of the SWP/SWPB instructions using load-exclusive and
  230. * store-exclusive.
  231. *
  232. * Syntax of SWP{B} instruction: SWP{B}<c> <Rt>, <Rt2>, [<Rn>]
  233. * Where: Rt = destination
  234. * Rt2 = source
  235. * Rn = address
  236. */
  237. /*
  238. * Error-checking SWP macros implemented using ldxr{b}/stxr{b}
  239. */
  240. #define __user_swpX_asm(data, addr, res, temp, B) \
  241. __asm__ __volatile__( \
  242. ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_HAS_PAN, \
  243. CONFIG_ARM64_PAN) \
  244. "0: ldxr"B" %w2, [%3]\n" \
  245. "1: stxr"B" %w0, %w1, [%3]\n" \
  246. " cbz %w0, 2f\n" \
  247. " mov %w0, %w4\n" \
  248. " b 3f\n" \
  249. "2:\n" \
  250. " mov %w1, %w2\n" \
  251. "3:\n" \
  252. " .pushsection .fixup,\"ax\"\n" \
  253. " .align 2\n" \
  254. "4: mov %w0, %w5\n" \
  255. " b 3b\n" \
  256. " .popsection" \
  257. _ASM_EXTABLE(0b, 4b) \
  258. _ASM_EXTABLE(1b, 4b) \
  259. ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN, \
  260. CONFIG_ARM64_PAN) \
  261. : "=&r" (res), "+r" (data), "=&r" (temp) \
  262. : "r" (addr), "i" (-EAGAIN), "i" (-EFAULT) \
  263. : "memory")
  264. #define __user_swp_asm(data, addr, res, temp) \
  265. __user_swpX_asm(data, addr, res, temp, "")
  266. #define __user_swpb_asm(data, addr, res, temp) \
  267. __user_swpX_asm(data, addr, res, temp, "b")
  268. /*
  269. * Bit 22 of the instruction encoding distinguishes between
  270. * the SWP and SWPB variants (bit set means SWPB).
  271. */
  272. #define TYPE_SWPB (1 << 22)
  273. static int emulate_swpX(unsigned int address, unsigned int *data,
  274. unsigned int type)
  275. {
  276. unsigned int res = 0;
  277. if ((type != TYPE_SWPB) && (address & 0x3)) {
  278. /* SWP to unaligned address not permitted */
  279. pr_debug("SWP instruction on unaligned pointer!\n");
  280. return -EFAULT;
  281. }
  282. while (1) {
  283. unsigned long temp;
  284. if (type == TYPE_SWPB)
  285. __user_swpb_asm(*data, address, res, temp);
  286. else
  287. __user_swp_asm(*data, address, res, temp);
  288. if (likely(res != -EAGAIN) || signal_pending(current))
  289. break;
  290. cond_resched();
  291. }
  292. return res;
  293. }
  294. #define ARM_OPCODE_CONDITION_UNCOND 0xf
  295. static unsigned int __kprobes aarch32_check_condition(u32 opcode, u32 psr)
  296. {
  297. u32 cc_bits = opcode >> 28;
  298. if (cc_bits != ARM_OPCODE_CONDITION_UNCOND) {
  299. if ((*aarch32_opcode_cond_checks[cc_bits])(psr))
  300. return ARM_OPCODE_CONDTEST_PASS;
  301. else
  302. return ARM_OPCODE_CONDTEST_FAIL;
  303. }
  304. return ARM_OPCODE_CONDTEST_UNCOND;
  305. }
  306. /*
  307. * swp_handler logs the id of calling process, dissects the instruction, sanity
  308. * checks the memory location, calls emulate_swpX for the actual operation and
  309. * deals with fixup/error handling before returning
  310. */
  311. static int swp_handler(struct pt_regs *regs, u32 instr)
  312. {
  313. u32 destreg, data, type, address = 0;
  314. int rn, rt2, res = 0;
  315. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, regs->pc);
  316. type = instr & TYPE_SWPB;
  317. switch (aarch32_check_condition(instr, regs->pstate)) {
  318. case ARM_OPCODE_CONDTEST_PASS:
  319. break;
  320. case ARM_OPCODE_CONDTEST_FAIL:
  321. /* Condition failed - return to next instruction */
  322. goto ret;
  323. case ARM_OPCODE_CONDTEST_UNCOND:
  324. /* If unconditional encoding - not a SWP, undef */
  325. return -EFAULT;
  326. default:
  327. return -EINVAL;
  328. }
  329. rn = aarch32_insn_extract_reg_num(instr, A32_RN_OFFSET);
  330. rt2 = aarch32_insn_extract_reg_num(instr, A32_RT2_OFFSET);
  331. address = (u32)regs->user_regs.regs[rn];
  332. data = (u32)regs->user_regs.regs[rt2];
  333. destreg = aarch32_insn_extract_reg_num(instr, A32_RT_OFFSET);
  334. pr_debug("addr in r%d->0x%08x, dest is r%d, source in r%d->0x%08x)\n",
  335. rn, address, destreg,
  336. aarch32_insn_extract_reg_num(instr, A32_RT2_OFFSET), data);
  337. /* Check access in reasonable access range for both SWP and SWPB */
  338. if (!access_ok(VERIFY_WRITE, (address & ~3), 4)) {
  339. pr_debug("SWP{B} emulation: access to 0x%08x not allowed!\n",
  340. address);
  341. goto fault;
  342. }
  343. res = emulate_swpX(address, &data, type);
  344. if (res == -EFAULT)
  345. goto fault;
  346. else if (res == 0)
  347. regs->user_regs.regs[destreg] = data;
  348. ret:
  349. if (type == TYPE_SWPB)
  350. trace_instruction_emulation("swpb", regs->pc);
  351. else
  352. trace_instruction_emulation("swp", regs->pc);
  353. pr_warn_ratelimited("\"%s\" (%ld) uses obsolete SWP{B} instruction at 0x%llx\n",
  354. current->comm, (unsigned long)current->pid, regs->pc);
  355. regs->pc += 4;
  356. return 0;
  357. fault:
  358. pr_debug("SWP{B} emulation: access caused memory abort!\n");
  359. arm64_notify_segfault(regs, address);
  360. return 0;
  361. }
  362. /*
  363. * Only emulate SWP/SWPB executed in ARM state/User mode.
  364. * The kernel must be SWP free and SWP{B} does not exist in Thumb.
  365. */
  366. static struct undef_hook swp_hooks[] = {
  367. {
  368. .instr_mask = 0x0fb00ff0,
  369. .instr_val = 0x01000090,
  370. .pstate_mask = COMPAT_PSR_MODE_MASK,
  371. .pstate_val = COMPAT_PSR_MODE_USR,
  372. .fn = swp_handler
  373. },
  374. { }
  375. };
  376. static struct insn_emulation_ops swp_ops = {
  377. .name = "swp",
  378. .status = INSN_OBSOLETE,
  379. .hooks = swp_hooks,
  380. .set_hw_mode = NULL,
  381. };
  382. static int cp15barrier_handler(struct pt_regs *regs, u32 instr)
  383. {
  384. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, regs->pc);
  385. switch (aarch32_check_condition(instr, regs->pstate)) {
  386. case ARM_OPCODE_CONDTEST_PASS:
  387. break;
  388. case ARM_OPCODE_CONDTEST_FAIL:
  389. /* Condition failed - return to next instruction */
  390. goto ret;
  391. case ARM_OPCODE_CONDTEST_UNCOND:
  392. /* If unconditional encoding - not a barrier instruction */
  393. return -EFAULT;
  394. default:
  395. return -EINVAL;
  396. }
  397. switch (aarch32_insn_mcr_extract_crm(instr)) {
  398. case 10:
  399. /*
  400. * dmb - mcr p15, 0, Rt, c7, c10, 5
  401. * dsb - mcr p15, 0, Rt, c7, c10, 4
  402. */
  403. if (aarch32_insn_mcr_extract_opc2(instr) == 5) {
  404. dmb(sy);
  405. trace_instruction_emulation(
  406. "mcr p15, 0, Rt, c7, c10, 5 ; dmb", regs->pc);
  407. } else {
  408. dsb(sy);
  409. trace_instruction_emulation(
  410. "mcr p15, 0, Rt, c7, c10, 4 ; dsb", regs->pc);
  411. }
  412. break;
  413. case 5:
  414. /*
  415. * isb - mcr p15, 0, Rt, c7, c5, 4
  416. *
  417. * Taking an exception or returning from one acts as an
  418. * instruction barrier. So no explicit barrier needed here.
  419. */
  420. trace_instruction_emulation(
  421. "mcr p15, 0, Rt, c7, c5, 4 ; isb", regs->pc);
  422. break;
  423. }
  424. ret:
  425. pr_warn_ratelimited("\"%s\" (%ld) uses deprecated CP15 Barrier instruction at 0x%llx\n",
  426. current->comm, (unsigned long)current->pid, regs->pc);
  427. regs->pc += 4;
  428. return 0;
  429. }
  430. static int cp15_barrier_set_hw_mode(bool enable)
  431. {
  432. if (enable)
  433. config_sctlr_el1(0, SCTLR_EL1_CP15BEN);
  434. else
  435. config_sctlr_el1(SCTLR_EL1_CP15BEN, 0);
  436. return 0;
  437. }
  438. static struct undef_hook cp15_barrier_hooks[] = {
  439. {
  440. .instr_mask = 0x0fff0fdf,
  441. .instr_val = 0x0e070f9a,
  442. .pstate_mask = COMPAT_PSR_MODE_MASK,
  443. .pstate_val = COMPAT_PSR_MODE_USR,
  444. .fn = cp15barrier_handler,
  445. },
  446. {
  447. .instr_mask = 0x0fff0fff,
  448. .instr_val = 0x0e070f95,
  449. .pstate_mask = COMPAT_PSR_MODE_MASK,
  450. .pstate_val = COMPAT_PSR_MODE_USR,
  451. .fn = cp15barrier_handler,
  452. },
  453. { }
  454. };
  455. static struct insn_emulation_ops cp15_barrier_ops = {
  456. .name = "cp15_barrier",
  457. .status = INSN_DEPRECATED,
  458. .hooks = cp15_barrier_hooks,
  459. .set_hw_mode = cp15_barrier_set_hw_mode,
  460. };
  461. static int setend_set_hw_mode(bool enable)
  462. {
  463. if (!cpu_supports_mixed_endian_el0())
  464. return -EINVAL;
  465. if (enable)
  466. config_sctlr_el1(SCTLR_EL1_SED, 0);
  467. else
  468. config_sctlr_el1(0, SCTLR_EL1_SED);
  469. return 0;
  470. }
  471. static int compat_setend_handler(struct pt_regs *regs, u32 big_endian)
  472. {
  473. char *insn;
  474. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, regs->pc);
  475. if (big_endian) {
  476. insn = "setend be";
  477. regs->pstate |= COMPAT_PSR_E_BIT;
  478. } else {
  479. insn = "setend le";
  480. regs->pstate &= ~COMPAT_PSR_E_BIT;
  481. }
  482. trace_instruction_emulation(insn, regs->pc);
  483. pr_warn_ratelimited("\"%s\" (%ld) uses deprecated setend instruction at 0x%llx\n",
  484. current->comm, (unsigned long)current->pid, regs->pc);
  485. return 0;
  486. }
  487. static int a32_setend_handler(struct pt_regs *regs, u32 instr)
  488. {
  489. int rc = compat_setend_handler(regs, (instr >> 9) & 1);
  490. regs->pc += 4;
  491. return rc;
  492. }
  493. static int t16_setend_handler(struct pt_regs *regs, u32 instr)
  494. {
  495. int rc = compat_setend_handler(regs, (instr >> 3) & 1);
  496. regs->pc += 2;
  497. return rc;
  498. }
  499. static struct undef_hook setend_hooks[] = {
  500. {
  501. .instr_mask = 0xfffffdff,
  502. .instr_val = 0xf1010000,
  503. .pstate_mask = COMPAT_PSR_MODE_MASK,
  504. .pstate_val = COMPAT_PSR_MODE_USR,
  505. .fn = a32_setend_handler,
  506. },
  507. {
  508. /* Thumb mode */
  509. .instr_mask = 0x0000fff7,
  510. .instr_val = 0x0000b650,
  511. .pstate_mask = (COMPAT_PSR_T_BIT | COMPAT_PSR_MODE_MASK),
  512. .pstate_val = (COMPAT_PSR_T_BIT | COMPAT_PSR_MODE_USR),
  513. .fn = t16_setend_handler,
  514. },
  515. {}
  516. };
  517. static struct insn_emulation_ops setend_ops = {
  518. .name = "setend",
  519. .status = INSN_DEPRECATED,
  520. .hooks = setend_hooks,
  521. .set_hw_mode = setend_set_hw_mode,
  522. };
  523. /*
  524. * Invoked as late_initcall, since not needed before init spawned.
  525. */
  526. static int __init armv8_deprecated_init(void)
  527. {
  528. if (IS_ENABLED(CONFIG_SWP_EMULATION))
  529. register_insn_emulation(&swp_ops);
  530. if (IS_ENABLED(CONFIG_CP15_BARRIER_EMULATION))
  531. register_insn_emulation(&cp15_barrier_ops);
  532. if (IS_ENABLED(CONFIG_SETEND_EMULATION)) {
  533. if(system_supports_mixed_endian_el0())
  534. register_insn_emulation(&setend_ops);
  535. else
  536. pr_info("setend instruction emulation is not supported on the system");
  537. }
  538. cpuhp_setup_state_nocalls(CPUHP_AP_ARM64_ISNDEP_STARTING,
  539. "AP_ARM64_ISNDEP_STARTING",
  540. run_all_insn_set_hw_mode, NULL);
  541. register_insn_emulation_sysctl(ctl_abi);
  542. return 0;
  543. }
  544. late_initcall(armv8_deprecated_init);