armv8_deprecated.c 15 KB

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