vcpu_insn.c 20 KB

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