On Tue, Jul 30, 2019 at 4:50 PM Paolo Bonzini <pbonzini@xxxxxxxxxx> wrote: > > On 29/07/19 13:57, Anup Patel wrote: > > +static ulong get_insn(struct kvm_vcpu *vcpu) > > +{ > > + ulong __sepc = vcpu->arch.guest_context.sepc; > > + ulong __hstatus, __sstatus, __vsstatus; > > +#ifdef CONFIG_RISCV_ISA_C > > + ulong rvc_mask = 3, tmp; > > +#endif > > + ulong flags, val; > > + > > + local_irq_save(flags); > > + > > + __vsstatus = csr_read(CSR_VSSTATUS); > > + __sstatus = csr_read(CSR_SSTATUS); > > + __hstatus = csr_read(CSR_HSTATUS); > > + > > + csr_write(CSR_VSSTATUS, __vsstatus | SR_MXR); > > + csr_write(CSR_SSTATUS, vcpu->arch.guest_context.sstatus | SR_MXR); > > + csr_write(CSR_HSTATUS, vcpu->arch.guest_context.hstatus | HSTATUS_SPRV); > > + > > +#ifndef CONFIG_RISCV_ISA_C > > + asm ("\n" > > +#ifdef CONFIG_64BIT > > + STR(LWU) " %[insn], (%[addr])\n" > > +#else > > + STR(LW) " %[insn], (%[addr])\n" > > +#endif > > + : [insn] "=&r" (val) : [addr] "r" (__sepc)); > > +#else > > + asm ("and %[tmp], %[addr], 2\n" > > + "bnez %[tmp], 1f\n" > > +#ifdef CONFIG_64BIT > > + STR(LWU) " %[insn], (%[addr])\n" > > +#else > > + STR(LW) " %[insn], (%[addr])\n" > > +#endif > > + "and %[tmp], %[insn], %[rvc_mask]\n" > > + "beq %[tmp], %[rvc_mask], 2f\n" > > + "sll %[insn], %[insn], %[xlen_minus_16]\n" > > + "srl %[insn], %[insn], %[xlen_minus_16]\n" > > + "j 2f\n" > > + "1:\n" > > + "lhu %[insn], (%[addr])\n" > > + "and %[tmp], %[insn], %[rvc_mask]\n" > > + "bne %[tmp], %[rvc_mask], 2f\n" > > + "lhu %[tmp], 2(%[addr])\n" > > + "sll %[tmp], %[tmp], 16\n" > > + "add %[insn], %[insn], %[tmp]\n" > > + "2:" > > + : [vsstatus] "+&r" (__vsstatus), [insn] "=&r" (val), > > + [tmp] "=&r" (tmp) > > + : [addr] "r" (__sepc), [rvc_mask] "r" (rvc_mask), > > + [xlen_minus_16] "i" (__riscv_xlen - 16)); > > +#endif > > + > > + csr_write(CSR_HSTATUS, __hstatus); > > + csr_write(CSR_SSTATUS, __sstatus); > > + csr_write(CSR_VSSTATUS, __vsstatus); > > + > > + local_irq_restore(flags); > > + > > + return val; > > +} > > + > > This also needs fixups for exceptions, because the guest can race > against the host and modify its page tables concurrently with the > vmexit. (How effective this is, of course, depends on how the TLB is > implemented in hardware, but you need to do the safe thing anyway). For Guest with single VCPU, we won't see any issue but we might get an exception for Guest with multiple VCPUs. We have added this in our TODO list. In this context, I have proposed to have separate CSR holding trapped instruction value so that we don't need to use unpriv load/store for figuring out trapped instruction. Refer, https://github.com/riscv/riscv-isa-manual/issues/394 The above Github issue and missing time delta CSR will be last two unaddressed Github issues from RISC-V spec perspective. Regards, Anup