vcpu_insn.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2019 Western Digital Corporation or its affiliates.
  4. * Copyright (c) 2022 Ventana Micro Systems Inc.
  5. */
  6. #include <linux/bitops.h>
  7. #include <linux/kvm_host.h>
  8. #define INSN_OPCODE_MASK 0x007c
  9. #define INSN_OPCODE_SHIFT 2
  10. #define INSN_OPCODE_SYSTEM 28
  11. #define INSN_MASK_WFI 0xffffffff
  12. #define INSN_MATCH_WFI 0x10500073
  13. #define INSN_MATCH_CSRRW 0x1073
  14. #define INSN_MASK_CSRRW 0x707f
  15. #define INSN_MATCH_CSRRS 0x2073
  16. #define INSN_MASK_CSRRS 0x707f
  17. #define INSN_MATCH_CSRRC 0x3073
  18. #define INSN_MASK_CSRRC 0x707f
  19. #define INSN_MATCH_CSRRWI 0x5073
  20. #define INSN_MASK_CSRRWI 0x707f
  21. #define INSN_MATCH_CSRRSI 0x6073
  22. #define INSN_MASK_CSRRSI 0x707f
  23. #define INSN_MATCH_CSRRCI 0x7073
  24. #define INSN_MASK_CSRRCI 0x707f
  25. #define INSN_MATCH_LB 0x3
  26. #define INSN_MASK_LB 0x707f
  27. #define INSN_MATCH_LH 0x1003
  28. #define INSN_MASK_LH 0x707f
  29. #define INSN_MATCH_LW 0x2003
  30. #define INSN_MASK_LW 0x707f
  31. #define INSN_MATCH_LD 0x3003
  32. #define INSN_MASK_LD 0x707f
  33. #define INSN_MATCH_LBU 0x4003
  34. #define INSN_MASK_LBU 0x707f
  35. #define INSN_MATCH_LHU 0x5003
  36. #define INSN_MASK_LHU 0x707f
  37. #define INSN_MATCH_LWU 0x6003
  38. #define INSN_MASK_LWU 0x707f
  39. #define INSN_MATCH_SB 0x23
  40. #define INSN_MASK_SB 0x707f
  41. #define INSN_MATCH_SH 0x1023
  42. #define INSN_MASK_SH 0x707f
  43. #define INSN_MATCH_SW 0x2023
  44. #define INSN_MASK_SW 0x707f
  45. #define INSN_MATCH_SD 0x3023
  46. #define INSN_MASK_SD 0x707f
  47. #define INSN_MATCH_C_LD 0x6000
  48. #define INSN_MASK_C_LD 0xe003
  49. #define INSN_MATCH_C_SD 0xe000
  50. #define INSN_MASK_C_SD 0xe003
  51. #define INSN_MATCH_C_LW 0x4000
  52. #define INSN_MASK_C_LW 0xe003
  53. #define INSN_MATCH_C_SW 0xc000
  54. #define INSN_MASK_C_SW 0xe003
  55. #define INSN_MATCH_C_LDSP 0x6002
  56. #define INSN_MASK_C_LDSP 0xe003
  57. #define INSN_MATCH_C_SDSP 0xe002
  58. #define INSN_MASK_C_SDSP 0xe003
  59. #define INSN_MATCH_C_LWSP 0x4002
  60. #define INSN_MASK_C_LWSP 0xe003
  61. #define INSN_MATCH_C_SWSP 0xc002
  62. #define INSN_MASK_C_SWSP 0xe003
  63. #define INSN_16BIT_MASK 0x3
  64. #define INSN_IS_16BIT(insn) (((insn) & INSN_16BIT_MASK) != INSN_16BIT_MASK)
  65. #define INSN_LEN(insn) (INSN_IS_16BIT(insn) ? 2 : 4)
  66. #ifdef CONFIG_64BIT
  67. #define LOG_REGBYTES 3
  68. #else
  69. #define LOG_REGBYTES 2
  70. #endif
  71. #define REGBYTES (1 << LOG_REGBYTES)
  72. #define SH_RD 7
  73. #define SH_RS1 15
  74. #define SH_RS2 20
  75. #define SH_RS2C 2
  76. #define MASK_RX 0x1f
  77. #define RV_X(x, s, n) (((x) >> (s)) & ((1 << (n)) - 1))
  78. #define RVC_LW_IMM(x) ((RV_X(x, 6, 1) << 2) | \
  79. (RV_X(x, 10, 3) << 3) | \
  80. (RV_X(x, 5, 1) << 6))
  81. #define RVC_LD_IMM(x) ((RV_X(x, 10, 3) << 3) | \
  82. (RV_X(x, 5, 2) << 6))
  83. #define RVC_LWSP_IMM(x) ((RV_X(x, 4, 3) << 2) | \
  84. (RV_X(x, 12, 1) << 5) | \
  85. (RV_X(x, 2, 2) << 6))
  86. #define RVC_LDSP_IMM(x) ((RV_X(x, 5, 2) << 3) | \
  87. (RV_X(x, 12, 1) << 5) | \
  88. (RV_X(x, 2, 3) << 6))
  89. #define RVC_SWSP_IMM(x) ((RV_X(x, 9, 4) << 2) | \
  90. (RV_X(x, 7, 2) << 6))
  91. #define RVC_SDSP_IMM(x) ((RV_X(x, 10, 3) << 3) | \
  92. (RV_X(x, 7, 3) << 6))
  93. #define RVC_RS1S(insn) (8 + RV_X(insn, SH_RD, 3))
  94. #define RVC_RS2S(insn) (8 + RV_X(insn, SH_RS2C, 3))
  95. #define RVC_RS2(insn) RV_X(insn, SH_RS2C, 5)
  96. #define SHIFT_RIGHT(x, y) \
  97. ((y) < 0 ? ((x) << -(y)) : ((x) >> (y)))
  98. #define REG_MASK \
  99. ((1 << (5 + LOG_REGBYTES)) - (1 << LOG_REGBYTES))
  100. #define REG_OFFSET(insn, pos) \
  101. (SHIFT_RIGHT((insn), (pos) - LOG_REGBYTES) & REG_MASK)
  102. #define REG_PTR(insn, pos, regs) \
  103. ((ulong *)((ulong)(regs) + REG_OFFSET(insn, pos)))
  104. #define GET_FUNCT3(insn) (((insn) >> 12) & 7)
  105. #define GET_RS1(insn, regs) (*REG_PTR(insn, SH_RS1, regs))
  106. #define GET_RS2(insn, regs) (*REG_PTR(insn, SH_RS2, regs))
  107. #define GET_RS1S(insn, regs) (*REG_PTR(RVC_RS1S(insn), 0, regs))
  108. #define GET_RS2S(insn, regs) (*REG_PTR(RVC_RS2S(insn), 0, regs))
  109. #define GET_RS2C(insn, regs) (*REG_PTR(insn, SH_RS2C, regs))
  110. #define GET_SP(regs) (*REG_PTR(2, 0, regs))
  111. #define SET_RD(insn, regs, val) (*REG_PTR(insn, SH_RD, regs) = (val))
  112. #define IMM_I(insn) ((s32)(insn) >> 20)
  113. #define IMM_S(insn) (((s32)(insn) >> 25 << 5) | \
  114. (s32)(((insn) >> 7) & 0x1f))
  115. struct insn_func {
  116. unsigned long mask;
  117. unsigned long match;
  118. /*
  119. * Possible return values are as follows:
  120. * 1) Returns < 0 for error case
  121. * 2) Returns 0 for exit to user-space
  122. * 3) Returns 1 to continue with next sepc
  123. * 4) Returns 2 to continue with same sepc
  124. * 5) Returns 3 to inject illegal instruction trap and continue
  125. * 6) Returns 4 to inject virtual instruction trap and continue
  126. *
  127. * Use enum kvm_insn_return for return values
  128. */
  129. int (*func)(struct kvm_vcpu *vcpu, struct kvm_run *run, ulong insn);
  130. };
  131. static int truly_illegal_insn(struct kvm_vcpu *vcpu, struct kvm_run *run,
  132. ulong insn)
  133. {
  134. struct kvm_cpu_trap utrap = { 0 };
  135. /* Redirect trap to Guest VCPU */
  136. utrap.sepc = vcpu->arch.guest_context.sepc;
  137. utrap.scause = EXC_INST_ILLEGAL;
  138. utrap.stval = insn;
  139. utrap.htval = 0;
  140. utrap.htinst = 0;
  141. kvm_riscv_vcpu_trap_redirect(vcpu, &utrap);
  142. return 1;
  143. }
  144. static int truly_virtual_insn(struct kvm_vcpu *vcpu, struct kvm_run *run,
  145. ulong insn)
  146. {
  147. struct kvm_cpu_trap utrap = { 0 };
  148. /* Redirect trap to Guest VCPU */
  149. utrap.sepc = vcpu->arch.guest_context.sepc;
  150. utrap.scause = EXC_VIRTUAL_INST_FAULT;
  151. utrap.stval = insn;
  152. utrap.htval = 0;
  153. utrap.htinst = 0;
  154. kvm_riscv_vcpu_trap_redirect(vcpu, &utrap);
  155. return 1;
  156. }
  157. /**
  158. * kvm_riscv_vcpu_wfi -- Emulate wait for interrupt (WFI) behaviour
  159. *
  160. * @vcpu: The VCPU pointer
  161. */
  162. void kvm_riscv_vcpu_wfi(struct kvm_vcpu *vcpu)
  163. {
  164. if (!kvm_arch_vcpu_runnable(vcpu)) {
  165. kvm_vcpu_srcu_read_unlock(vcpu);
  166. kvm_vcpu_halt(vcpu);
  167. kvm_vcpu_srcu_read_lock(vcpu);
  168. }
  169. }
  170. static int wfi_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, ulong insn)
  171. {
  172. vcpu->stat.wfi_exit_stat++;
  173. kvm_riscv_vcpu_wfi(vcpu);
  174. return KVM_INSN_CONTINUE_NEXT_SEPC;
  175. }
  176. struct csr_func {
  177. unsigned int base;
  178. unsigned int count;
  179. /*
  180. * Possible return values are as same as "func" callback in
  181. * "struct insn_func".
  182. */
  183. int (*func)(struct kvm_vcpu *vcpu, unsigned int csr_num,
  184. unsigned long *val, unsigned long new_val,
  185. unsigned long wr_mask);
  186. };
  187. static const struct csr_func csr_funcs[] = { };
  188. /**
  189. * kvm_riscv_vcpu_csr_return -- Handle CSR read/write after user space
  190. * emulation or in-kernel emulation
  191. *
  192. * @vcpu: The VCPU pointer
  193. * @run: The VCPU run struct containing the CSR data
  194. *
  195. * Returns > 0 upon failure and 0 upon success
  196. */
  197. int kvm_riscv_vcpu_csr_return(struct kvm_vcpu *vcpu, struct kvm_run *run)
  198. {
  199. ulong insn;
  200. if (vcpu->arch.csr_decode.return_handled)
  201. return 0;
  202. vcpu->arch.csr_decode.return_handled = 1;
  203. /* Update destination register for CSR reads */
  204. insn = vcpu->arch.csr_decode.insn;
  205. if ((insn >> SH_RD) & MASK_RX)
  206. SET_RD(insn, &vcpu->arch.guest_context,
  207. run->riscv_csr.ret_value);
  208. /* Move to next instruction */
  209. vcpu->arch.guest_context.sepc += INSN_LEN(insn);
  210. return 0;
  211. }
  212. static int csr_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, ulong insn)
  213. {
  214. int i, rc = KVM_INSN_ILLEGAL_TRAP;
  215. unsigned int csr_num = insn >> SH_RS2;
  216. unsigned int rs1_num = (insn >> SH_RS1) & MASK_RX;
  217. ulong rs1_val = GET_RS1(insn, &vcpu->arch.guest_context);
  218. const struct csr_func *tcfn, *cfn = NULL;
  219. ulong val = 0, wr_mask = 0, new_val = 0;
  220. /* Decode the CSR instruction */
  221. switch (GET_FUNCT3(insn)) {
  222. case GET_FUNCT3(INSN_MATCH_CSRRW):
  223. wr_mask = -1UL;
  224. new_val = rs1_val;
  225. break;
  226. case GET_FUNCT3(INSN_MATCH_CSRRS):
  227. wr_mask = rs1_val;
  228. new_val = -1UL;
  229. break;
  230. case GET_FUNCT3(INSN_MATCH_CSRRC):
  231. wr_mask = rs1_val;
  232. new_val = 0;
  233. break;
  234. case GET_FUNCT3(INSN_MATCH_CSRRWI):
  235. wr_mask = -1UL;
  236. new_val = rs1_num;
  237. break;
  238. case GET_FUNCT3(INSN_MATCH_CSRRSI):
  239. wr_mask = rs1_num;
  240. new_val = -1UL;
  241. break;
  242. case GET_FUNCT3(INSN_MATCH_CSRRCI):
  243. wr_mask = rs1_num;
  244. new_val = 0;
  245. break;
  246. default:
  247. return rc;
  248. }
  249. /* Save instruction decode info */
  250. vcpu->arch.csr_decode.insn = insn;
  251. vcpu->arch.csr_decode.return_handled = 0;
  252. /* Update CSR details in kvm_run struct */
  253. run->riscv_csr.csr_num = csr_num;
  254. run->riscv_csr.new_value = new_val;
  255. run->riscv_csr.write_mask = wr_mask;
  256. run->riscv_csr.ret_value = 0;
  257. /* Find in-kernel CSR function */
  258. for (i = 0; i < ARRAY_SIZE(csr_funcs); i++) {
  259. tcfn = &csr_funcs[i];
  260. if ((tcfn->base <= csr_num) &&
  261. (csr_num < (tcfn->base + tcfn->count))) {
  262. cfn = tcfn;
  263. break;
  264. }
  265. }
  266. /* First try in-kernel CSR emulation */
  267. if (cfn && cfn->func) {
  268. rc = cfn->func(vcpu, csr_num, &val, new_val, wr_mask);
  269. if (rc > KVM_INSN_EXIT_TO_USER_SPACE) {
  270. if (rc == KVM_INSN_CONTINUE_NEXT_SEPC) {
  271. run->riscv_csr.ret_value = val;
  272. vcpu->stat.csr_exit_kernel++;
  273. kvm_riscv_vcpu_csr_return(vcpu, run);
  274. rc = KVM_INSN_CONTINUE_SAME_SEPC;
  275. }
  276. return rc;
  277. }
  278. }
  279. /* Exit to user-space for CSR emulation */
  280. if (rc <= KVM_INSN_EXIT_TO_USER_SPACE) {
  281. vcpu->stat.csr_exit_user++;
  282. run->exit_reason = KVM_EXIT_RISCV_CSR;
  283. }
  284. return rc;
  285. }
  286. static const struct insn_func system_opcode_funcs[] = {
  287. {
  288. .mask = INSN_MASK_CSRRW,
  289. .match = INSN_MATCH_CSRRW,
  290. .func = csr_insn,
  291. },
  292. {
  293. .mask = INSN_MASK_CSRRS,
  294. .match = INSN_MATCH_CSRRS,
  295. .func = csr_insn,
  296. },
  297. {
  298. .mask = INSN_MASK_CSRRC,
  299. .match = INSN_MATCH_CSRRC,
  300. .func = csr_insn,
  301. },
  302. {
  303. .mask = INSN_MASK_CSRRWI,
  304. .match = INSN_MATCH_CSRRWI,
  305. .func = csr_insn,
  306. },
  307. {
  308. .mask = INSN_MASK_CSRRSI,
  309. .match = INSN_MATCH_CSRRSI,
  310. .func = csr_insn,
  311. },
  312. {
  313. .mask = INSN_MASK_CSRRCI,
  314. .match = INSN_MATCH_CSRRCI,
  315. .func = csr_insn,
  316. },
  317. {
  318. .mask = INSN_MASK_WFI,
  319. .match = INSN_MATCH_WFI,
  320. .func = wfi_insn,
  321. },
  322. };
  323. static int system_opcode_insn(struct kvm_vcpu *vcpu, struct kvm_run *run,
  324. ulong insn)
  325. {
  326. int i, rc = KVM_INSN_ILLEGAL_TRAP;
  327. const struct insn_func *ifn;
  328. for (i = 0; i < ARRAY_SIZE(system_opcode_funcs); i++) {
  329. ifn = &system_opcode_funcs[i];
  330. if ((insn & ifn->mask) == ifn->match) {
  331. rc = ifn->func(vcpu, run, insn);
  332. break;
  333. }
  334. }
  335. switch (rc) {
  336. case KVM_INSN_ILLEGAL_TRAP:
  337. return truly_illegal_insn(vcpu, run, insn);
  338. case KVM_INSN_VIRTUAL_TRAP:
  339. return truly_virtual_insn(vcpu, run, insn);
  340. case KVM_INSN_CONTINUE_NEXT_SEPC:
  341. vcpu->arch.guest_context.sepc += INSN_LEN(insn);
  342. break;
  343. default:
  344. break;
  345. }
  346. return (rc <= 0) ? rc : 1;
  347. }
  348. /**
  349. * kvm_riscv_vcpu_virtual_insn -- Handle virtual instruction trap
  350. *
  351. * @vcpu: The VCPU pointer
  352. * @run: The VCPU run struct containing the mmio data
  353. * @trap: Trap details
  354. *
  355. * Returns > 0 to continue run-loop
  356. * Returns 0 to exit run-loop and handle in user-space.
  357. * Returns < 0 to report failure and exit run-loop
  358. */
  359. int kvm_riscv_vcpu_virtual_insn(struct kvm_vcpu *vcpu, struct kvm_run *run,
  360. struct kvm_cpu_trap *trap)
  361. {
  362. unsigned long insn = trap->stval;
  363. struct kvm_cpu_trap utrap = { 0 };
  364. struct kvm_cpu_context *ct;
  365. if (unlikely(INSN_IS_16BIT(insn))) {
  366. if (insn == 0) {
  367. ct = &vcpu->arch.guest_context;
  368. insn = kvm_riscv_vcpu_unpriv_read(vcpu, true,
  369. ct->sepc,
  370. &utrap);
  371. if (utrap.scause) {
  372. utrap.sepc = ct->sepc;
  373. kvm_riscv_vcpu_trap_redirect(vcpu, &utrap);
  374. return 1;
  375. }
  376. }
  377. if (INSN_IS_16BIT(insn))
  378. return truly_illegal_insn(vcpu, run, insn);
  379. }
  380. switch ((insn & INSN_OPCODE_MASK) >> INSN_OPCODE_SHIFT) {
  381. case INSN_OPCODE_SYSTEM:
  382. return system_opcode_insn(vcpu, run, insn);
  383. default:
  384. return truly_illegal_insn(vcpu, run, insn);
  385. }
  386. }
  387. /**
  388. * kvm_riscv_vcpu_mmio_load -- Emulate MMIO load instruction
  389. *
  390. * @vcpu: The VCPU pointer
  391. * @run: The VCPU run struct containing the mmio data
  392. * @fault_addr: Guest physical address to load
  393. * @htinst: Transformed encoding of the load instruction
  394. *
  395. * Returns > 0 to continue run-loop
  396. * Returns 0 to exit run-loop and handle in user-space.
  397. * Returns < 0 to report failure and exit run-loop
  398. */
  399. int kvm_riscv_vcpu_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run,
  400. unsigned long fault_addr,
  401. unsigned long htinst)
  402. {
  403. u8 data_buf[8];
  404. unsigned long insn;
  405. int shift = 0, len = 0, insn_len = 0;
  406. struct kvm_cpu_trap utrap = { 0 };
  407. struct kvm_cpu_context *ct = &vcpu->arch.guest_context;
  408. /* Determine trapped instruction */
  409. if (htinst & 0x1) {
  410. /*
  411. * Bit[0] == 1 implies trapped instruction value is
  412. * transformed instruction or custom instruction.
  413. */
  414. insn = htinst | INSN_16BIT_MASK;
  415. insn_len = (htinst & BIT(1)) ? INSN_LEN(insn) : 2;
  416. } else {
  417. /*
  418. * Bit[0] == 0 implies trapped instruction value is
  419. * zero or special value.
  420. */
  421. insn = kvm_riscv_vcpu_unpriv_read(vcpu, true, ct->sepc,
  422. &utrap);
  423. if (utrap.scause) {
  424. /* Redirect trap if we failed to read instruction */
  425. utrap.sepc = ct->sepc;
  426. kvm_riscv_vcpu_trap_redirect(vcpu, &utrap);
  427. return 1;
  428. }
  429. insn_len = INSN_LEN(insn);
  430. }
  431. /* Decode length of MMIO and shift */
  432. if ((insn & INSN_MASK_LW) == INSN_MATCH_LW) {
  433. len = 4;
  434. shift = 8 * (sizeof(ulong) - len);
  435. } else if ((insn & INSN_MASK_LB) == INSN_MATCH_LB) {
  436. len = 1;
  437. shift = 8 * (sizeof(ulong) - len);
  438. } else if ((insn & INSN_MASK_LBU) == INSN_MATCH_LBU) {
  439. len = 1;
  440. shift = 8 * (sizeof(ulong) - len);
  441. #ifdef CONFIG_64BIT
  442. } else if ((insn & INSN_MASK_LD) == INSN_MATCH_LD) {
  443. len = 8;
  444. shift = 8 * (sizeof(ulong) - len);
  445. } else if ((insn & INSN_MASK_LWU) == INSN_MATCH_LWU) {
  446. len = 4;
  447. #endif
  448. } else if ((insn & INSN_MASK_LH) == INSN_MATCH_LH) {
  449. len = 2;
  450. shift = 8 * (sizeof(ulong) - len);
  451. } else if ((insn & INSN_MASK_LHU) == INSN_MATCH_LHU) {
  452. len = 2;
  453. #ifdef CONFIG_64BIT
  454. } else if ((insn & INSN_MASK_C_LD) == INSN_MATCH_C_LD) {
  455. len = 8;
  456. shift = 8 * (sizeof(ulong) - len);
  457. insn = RVC_RS2S(insn) << SH_RD;
  458. } else if ((insn & INSN_MASK_C_LDSP) == INSN_MATCH_C_LDSP &&
  459. ((insn >> SH_RD) & 0x1f)) {
  460. len = 8;
  461. shift = 8 * (sizeof(ulong) - len);
  462. #endif
  463. } else if ((insn & INSN_MASK_C_LW) == INSN_MATCH_C_LW) {
  464. len = 4;
  465. shift = 8 * (sizeof(ulong) - len);
  466. insn = RVC_RS2S(insn) << SH_RD;
  467. } else if ((insn & INSN_MASK_C_LWSP) == INSN_MATCH_C_LWSP &&
  468. ((insn >> SH_RD) & 0x1f)) {
  469. len = 4;
  470. shift = 8 * (sizeof(ulong) - len);
  471. } else {
  472. return -EOPNOTSUPP;
  473. }
  474. /* Fault address should be aligned to length of MMIO */
  475. if (fault_addr & (len - 1))
  476. return -EIO;
  477. /* Save instruction decode info */
  478. vcpu->arch.mmio_decode.insn = insn;
  479. vcpu->arch.mmio_decode.insn_len = insn_len;
  480. vcpu->arch.mmio_decode.shift = shift;
  481. vcpu->arch.mmio_decode.len = len;
  482. vcpu->arch.mmio_decode.return_handled = 0;
  483. /* Update MMIO details in kvm_run struct */
  484. run->mmio.is_write = false;
  485. run->mmio.phys_addr = fault_addr;
  486. run->mmio.len = len;
  487. /* Try to handle MMIO access in the kernel */
  488. if (!kvm_io_bus_read(vcpu, KVM_MMIO_BUS, fault_addr, len, data_buf)) {
  489. /* Successfully handled MMIO access in the kernel so resume */
  490. memcpy(run->mmio.data, data_buf, len);
  491. vcpu->stat.mmio_exit_kernel++;
  492. kvm_riscv_vcpu_mmio_return(vcpu, run);
  493. return 1;
  494. }
  495. /* Exit to userspace for MMIO emulation */
  496. vcpu->stat.mmio_exit_user++;
  497. run->exit_reason = KVM_EXIT_MMIO;
  498. return 0;
  499. }
  500. /**
  501. * kvm_riscv_vcpu_mmio_store -- Emulate MMIO store instruction
  502. *
  503. * @vcpu: The VCPU pointer
  504. * @run: The VCPU run struct containing the mmio data
  505. * @fault_addr: Guest physical address to store
  506. * @htinst: Transformed encoding of the store instruction
  507. *
  508. * Returns > 0 to continue run-loop
  509. * Returns 0 to exit run-loop and handle in user-space.
  510. * Returns < 0 to report failure and exit run-loop
  511. */
  512. int kvm_riscv_vcpu_mmio_store(struct kvm_vcpu *vcpu, struct kvm_run *run,
  513. unsigned long fault_addr,
  514. unsigned long htinst)
  515. {
  516. u8 data8;
  517. u16 data16;
  518. u32 data32;
  519. u64 data64;
  520. ulong data;
  521. unsigned long insn;
  522. int len = 0, insn_len = 0;
  523. struct kvm_cpu_trap utrap = { 0 };
  524. struct kvm_cpu_context *ct = &vcpu->arch.guest_context;
  525. /* Determine trapped instruction */
  526. if (htinst & 0x1) {
  527. /*
  528. * Bit[0] == 1 implies trapped instruction value is
  529. * transformed instruction or custom instruction.
  530. */
  531. insn = htinst | INSN_16BIT_MASK;
  532. insn_len = (htinst & BIT(1)) ? INSN_LEN(insn) : 2;
  533. } else {
  534. /*
  535. * Bit[0] == 0 implies trapped instruction value is
  536. * zero or special value.
  537. */
  538. insn = kvm_riscv_vcpu_unpriv_read(vcpu, true, ct->sepc,
  539. &utrap);
  540. if (utrap.scause) {
  541. /* Redirect trap if we failed to read instruction */
  542. utrap.sepc = ct->sepc;
  543. kvm_riscv_vcpu_trap_redirect(vcpu, &utrap);
  544. return 1;
  545. }
  546. insn_len = INSN_LEN(insn);
  547. }
  548. data = GET_RS2(insn, &vcpu->arch.guest_context);
  549. data8 = data16 = data32 = data64 = data;
  550. if ((insn & INSN_MASK_SW) == INSN_MATCH_SW) {
  551. len = 4;
  552. } else if ((insn & INSN_MASK_SB) == INSN_MATCH_SB) {
  553. len = 1;
  554. #ifdef CONFIG_64BIT
  555. } else if ((insn & INSN_MASK_SD) == INSN_MATCH_SD) {
  556. len = 8;
  557. #endif
  558. } else if ((insn & INSN_MASK_SH) == INSN_MATCH_SH) {
  559. len = 2;
  560. #ifdef CONFIG_64BIT
  561. } else if ((insn & INSN_MASK_C_SD) == INSN_MATCH_C_SD) {
  562. len = 8;
  563. data64 = GET_RS2S(insn, &vcpu->arch.guest_context);
  564. } else if ((insn & INSN_MASK_C_SDSP) == INSN_MATCH_C_SDSP &&
  565. ((insn >> SH_RD) & 0x1f)) {
  566. len = 8;
  567. data64 = GET_RS2C(insn, &vcpu->arch.guest_context);
  568. #endif
  569. } else if ((insn & INSN_MASK_C_SW) == INSN_MATCH_C_SW) {
  570. len = 4;
  571. data32 = GET_RS2S(insn, &vcpu->arch.guest_context);
  572. } else if ((insn & INSN_MASK_C_SWSP) == INSN_MATCH_C_SWSP &&
  573. ((insn >> SH_RD) & 0x1f)) {
  574. len = 4;
  575. data32 = GET_RS2C(insn, &vcpu->arch.guest_context);
  576. } else {
  577. return -EOPNOTSUPP;
  578. }
  579. /* Fault address should be aligned to length of MMIO */
  580. if (fault_addr & (len - 1))
  581. return -EIO;
  582. /* Save instruction decode info */
  583. vcpu->arch.mmio_decode.insn = insn;
  584. vcpu->arch.mmio_decode.insn_len = insn_len;
  585. vcpu->arch.mmio_decode.shift = 0;
  586. vcpu->arch.mmio_decode.len = len;
  587. vcpu->arch.mmio_decode.return_handled = 0;
  588. /* Copy data to kvm_run instance */
  589. switch (len) {
  590. case 1:
  591. *((u8 *)run->mmio.data) = data8;
  592. break;
  593. case 2:
  594. *((u16 *)run->mmio.data) = data16;
  595. break;
  596. case 4:
  597. *((u32 *)run->mmio.data) = data32;
  598. break;
  599. case 8:
  600. *((u64 *)run->mmio.data) = data64;
  601. break;
  602. default:
  603. return -EOPNOTSUPP;
  604. }
  605. /* Update MMIO details in kvm_run struct */
  606. run->mmio.is_write = true;
  607. run->mmio.phys_addr = fault_addr;
  608. run->mmio.len = len;
  609. /* Try to handle MMIO access in the kernel */
  610. if (!kvm_io_bus_write(vcpu, KVM_MMIO_BUS,
  611. fault_addr, len, run->mmio.data)) {
  612. /* Successfully handled MMIO access in the kernel so resume */
  613. vcpu->stat.mmio_exit_kernel++;
  614. kvm_riscv_vcpu_mmio_return(vcpu, run);
  615. return 1;
  616. }
  617. /* Exit to userspace for MMIO emulation */
  618. vcpu->stat.mmio_exit_user++;
  619. run->exit_reason = KVM_EXIT_MMIO;
  620. return 0;
  621. }
  622. /**
  623. * kvm_riscv_vcpu_mmio_return -- Handle MMIO loads after user space emulation
  624. * or in-kernel IO emulation
  625. *
  626. * @vcpu: The VCPU pointer
  627. * @run: The VCPU run struct containing the mmio data
  628. */
  629. int kvm_riscv_vcpu_mmio_return(struct kvm_vcpu *vcpu, struct kvm_run *run)
  630. {
  631. u8 data8;
  632. u16 data16;
  633. u32 data32;
  634. u64 data64;
  635. ulong insn;
  636. int len, shift;
  637. if (vcpu->arch.mmio_decode.return_handled)
  638. return 0;
  639. vcpu->arch.mmio_decode.return_handled = 1;
  640. insn = vcpu->arch.mmio_decode.insn;
  641. if (run->mmio.is_write)
  642. goto done;
  643. len = vcpu->arch.mmio_decode.len;
  644. shift = vcpu->arch.mmio_decode.shift;
  645. switch (len) {
  646. case 1:
  647. data8 = *((u8 *)run->mmio.data);
  648. SET_RD(insn, &vcpu->arch.guest_context,
  649. (ulong)data8 << shift >> shift);
  650. break;
  651. case 2:
  652. data16 = *((u16 *)run->mmio.data);
  653. SET_RD(insn, &vcpu->arch.guest_context,
  654. (ulong)data16 << shift >> shift);
  655. break;
  656. case 4:
  657. data32 = *((u32 *)run->mmio.data);
  658. SET_RD(insn, &vcpu->arch.guest_context,
  659. (ulong)data32 << shift >> shift);
  660. break;
  661. case 8:
  662. data64 = *((u64 *)run->mmio.data);
  663. SET_RD(insn, &vcpu->arch.guest_context,
  664. (ulong)data64 << shift >> shift);
  665. break;
  666. default:
  667. return -EOPNOTSUPP;
  668. }
  669. done:
  670. /* Move to next instruction */
  671. vcpu->arch.guest_context.sepc += vcpu->arch.mmio_decode.insn_len;
  672. return 0;
  673. }