|
14 | 14 | #define INSN_MASK_WFI 0xffffffff |
15 | 15 | #define INSN_MATCH_WFI 0x10500073 |
16 | 16 |
|
| 17 | +#define INSN_MATCH_CSRRW 0x1073 |
| 18 | +#define INSN_MASK_CSRRW 0x707f |
| 19 | +#define INSN_MATCH_CSRRS 0x2073 |
| 20 | +#define INSN_MASK_CSRRS 0x707f |
| 21 | +#define INSN_MATCH_CSRRC 0x3073 |
| 22 | +#define INSN_MASK_CSRRC 0x707f |
| 23 | +#define INSN_MATCH_CSRRWI 0x5073 |
| 24 | +#define INSN_MASK_CSRRWI 0x707f |
| 25 | +#define INSN_MATCH_CSRRSI 0x6073 |
| 26 | +#define INSN_MASK_CSRRSI 0x707f |
| 27 | +#define INSN_MATCH_CSRRCI 0x7073 |
| 28 | +#define INSN_MASK_CSRRCI 0x707f |
| 29 | + |
17 | 30 | #define INSN_MATCH_LB 0x3 |
18 | 31 | #define INSN_MASK_LB 0x707f |
19 | 32 | #define INSN_MATCH_LH 0x1003 |
|
71 | 84 | #define SH_RS1 15 |
72 | 85 | #define SH_RS2 20 |
73 | 86 | #define SH_RS2C 2 |
| 87 | +#define MASK_RX 0x1f |
74 | 88 |
|
75 | 89 | #define RV_X(x, s, n) (((x) >> (s)) & ((1 << (n)) - 1)) |
76 | 90 | #define RVC_LW_IMM(x) ((RV_X(x, 6, 1) << 2) | \ |
|
104 | 118 | #define REG_PTR(insn, pos, regs) \ |
105 | 119 | ((ulong *)((ulong)(regs) + REG_OFFSET(insn, pos))) |
106 | 120 |
|
107 | | -#define GET_RM(insn) (((insn) >> 12) & 7) |
| 121 | +#define GET_FUNCT3(insn) (((insn) >> 12) & 7) |
108 | 122 |
|
109 | 123 | #define GET_RS1(insn, regs) (*REG_PTR(insn, SH_RS1, regs)) |
110 | 124 | #define GET_RS2(insn, regs) (*REG_PTR(insn, SH_RS2, regs)) |
|
116 | 130 | #define IMM_I(insn) ((s32)(insn) >> 20) |
117 | 131 | #define IMM_S(insn) (((s32)(insn) >> 25 << 5) | \ |
118 | 132 | (s32)(((insn) >> 7) & 0x1f)) |
119 | | -#define MASK_FUNCT3 0x7000 |
120 | 133 |
|
121 | 134 | struct insn_func { |
122 | 135 | unsigned long mask; |
@@ -189,7 +202,162 @@ static int wfi_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, ulong insn) |
189 | 202 | return KVM_INSN_CONTINUE_NEXT_SEPC; |
190 | 203 | } |
191 | 204 |
|
| 205 | +struct csr_func { |
| 206 | + unsigned int base; |
| 207 | + unsigned int count; |
| 208 | + /* |
| 209 | + * Possible return values are as same as "func" callback in |
| 210 | + * "struct insn_func". |
| 211 | + */ |
| 212 | + int (*func)(struct kvm_vcpu *vcpu, unsigned int csr_num, |
| 213 | + unsigned long *val, unsigned long new_val, |
| 214 | + unsigned long wr_mask); |
| 215 | +}; |
| 216 | + |
| 217 | +static const struct csr_func csr_funcs[] = { }; |
| 218 | + |
| 219 | +/** |
| 220 | + * kvm_riscv_vcpu_csr_return -- Handle CSR read/write after user space |
| 221 | + * emulation or in-kernel emulation |
| 222 | + * |
| 223 | + * @vcpu: The VCPU pointer |
| 224 | + * @run: The VCPU run struct containing the CSR data |
| 225 | + * |
| 226 | + * Returns > 0 upon failure and 0 upon success |
| 227 | + */ |
| 228 | +int kvm_riscv_vcpu_csr_return(struct kvm_vcpu *vcpu, struct kvm_run *run) |
| 229 | +{ |
| 230 | + ulong insn; |
| 231 | + |
| 232 | + if (vcpu->arch.csr_decode.return_handled) |
| 233 | + return 0; |
| 234 | + vcpu->arch.csr_decode.return_handled = 1; |
| 235 | + |
| 236 | + /* Update destination register for CSR reads */ |
| 237 | + insn = vcpu->arch.csr_decode.insn; |
| 238 | + if ((insn >> SH_RD) & MASK_RX) |
| 239 | + SET_RD(insn, &vcpu->arch.guest_context, |
| 240 | + run->riscv_csr.ret_value); |
| 241 | + |
| 242 | + /* Move to next instruction */ |
| 243 | + vcpu->arch.guest_context.sepc += INSN_LEN(insn); |
| 244 | + |
| 245 | + return 0; |
| 246 | +} |
| 247 | + |
| 248 | +static int csr_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, ulong insn) |
| 249 | +{ |
| 250 | + int i, rc = KVM_INSN_ILLEGAL_TRAP; |
| 251 | + unsigned int csr_num = insn >> SH_RS2; |
| 252 | + unsigned int rs1_num = (insn >> SH_RS1) & MASK_RX; |
| 253 | + ulong rs1_val = GET_RS1(insn, &vcpu->arch.guest_context); |
| 254 | + const struct csr_func *tcfn, *cfn = NULL; |
| 255 | + ulong val = 0, wr_mask = 0, new_val = 0; |
| 256 | + |
| 257 | + /* Decode the CSR instruction */ |
| 258 | + switch (GET_FUNCT3(insn)) { |
| 259 | + case GET_FUNCT3(INSN_MATCH_CSRRW): |
| 260 | + wr_mask = -1UL; |
| 261 | + new_val = rs1_val; |
| 262 | + break; |
| 263 | + case GET_FUNCT3(INSN_MATCH_CSRRS): |
| 264 | + wr_mask = rs1_val; |
| 265 | + new_val = -1UL; |
| 266 | + break; |
| 267 | + case GET_FUNCT3(INSN_MATCH_CSRRC): |
| 268 | + wr_mask = rs1_val; |
| 269 | + new_val = 0; |
| 270 | + break; |
| 271 | + case GET_FUNCT3(INSN_MATCH_CSRRWI): |
| 272 | + wr_mask = -1UL; |
| 273 | + new_val = rs1_num; |
| 274 | + break; |
| 275 | + case GET_FUNCT3(INSN_MATCH_CSRRSI): |
| 276 | + wr_mask = rs1_num; |
| 277 | + new_val = -1UL; |
| 278 | + break; |
| 279 | + case GET_FUNCT3(INSN_MATCH_CSRRCI): |
| 280 | + wr_mask = rs1_num; |
| 281 | + new_val = 0; |
| 282 | + break; |
| 283 | + default: |
| 284 | + return rc; |
| 285 | + } |
| 286 | + |
| 287 | + /* Save instruction decode info */ |
| 288 | + vcpu->arch.csr_decode.insn = insn; |
| 289 | + vcpu->arch.csr_decode.return_handled = 0; |
| 290 | + |
| 291 | + /* Update CSR details in kvm_run struct */ |
| 292 | + run->riscv_csr.csr_num = csr_num; |
| 293 | + run->riscv_csr.new_value = new_val; |
| 294 | + run->riscv_csr.write_mask = wr_mask; |
| 295 | + run->riscv_csr.ret_value = 0; |
| 296 | + |
| 297 | + /* Find in-kernel CSR function */ |
| 298 | + for (i = 0; i < ARRAY_SIZE(csr_funcs); i++) { |
| 299 | + tcfn = &csr_funcs[i]; |
| 300 | + if ((tcfn->base <= csr_num) && |
| 301 | + (csr_num < (tcfn->base + tcfn->count))) { |
| 302 | + cfn = tcfn; |
| 303 | + break; |
| 304 | + } |
| 305 | + } |
| 306 | + |
| 307 | + /* First try in-kernel CSR emulation */ |
| 308 | + if (cfn && cfn->func) { |
| 309 | + rc = cfn->func(vcpu, csr_num, &val, new_val, wr_mask); |
| 310 | + if (rc > KVM_INSN_EXIT_TO_USER_SPACE) { |
| 311 | + if (rc == KVM_INSN_CONTINUE_NEXT_SEPC) { |
| 312 | + run->riscv_csr.ret_value = val; |
| 313 | + vcpu->stat.csr_exit_kernel++; |
| 314 | + kvm_riscv_vcpu_csr_return(vcpu, run); |
| 315 | + rc = KVM_INSN_CONTINUE_SAME_SEPC; |
| 316 | + } |
| 317 | + return rc; |
| 318 | + } |
| 319 | + } |
| 320 | + |
| 321 | + /* Exit to user-space for CSR emulation */ |
| 322 | + if (rc <= KVM_INSN_EXIT_TO_USER_SPACE) { |
| 323 | + vcpu->stat.csr_exit_user++; |
| 324 | + run->exit_reason = KVM_EXIT_RISCV_CSR; |
| 325 | + } |
| 326 | + |
| 327 | + return rc; |
| 328 | +} |
| 329 | + |
192 | 330 | static const struct insn_func system_opcode_funcs[] = { |
| 331 | + { |
| 332 | + .mask = INSN_MASK_CSRRW, |
| 333 | + .match = INSN_MATCH_CSRRW, |
| 334 | + .func = csr_insn, |
| 335 | + }, |
| 336 | + { |
| 337 | + .mask = INSN_MASK_CSRRS, |
| 338 | + .match = INSN_MATCH_CSRRS, |
| 339 | + .func = csr_insn, |
| 340 | + }, |
| 341 | + { |
| 342 | + .mask = INSN_MASK_CSRRC, |
| 343 | + .match = INSN_MATCH_CSRRC, |
| 344 | + .func = csr_insn, |
| 345 | + }, |
| 346 | + { |
| 347 | + .mask = INSN_MASK_CSRRWI, |
| 348 | + .match = INSN_MATCH_CSRRWI, |
| 349 | + .func = csr_insn, |
| 350 | + }, |
| 351 | + { |
| 352 | + .mask = INSN_MASK_CSRRSI, |
| 353 | + .match = INSN_MATCH_CSRRSI, |
| 354 | + .func = csr_insn, |
| 355 | + }, |
| 356 | + { |
| 357 | + .mask = INSN_MASK_CSRRCI, |
| 358 | + .match = INSN_MATCH_CSRRCI, |
| 359 | + .func = csr_insn, |
| 360 | + }, |
193 | 361 | { |
194 | 362 | .mask = INSN_MASK_WFI, |
195 | 363 | .match = INSN_MATCH_WFI, |
|
0 commit comments