]> www.infradead.org Git - users/hch/block.git/commitdiff
LoongArch: KVM: Add cpucfg area for kvm hypervisor
authorBibo Mao <maobibo@loongson.cn>
Mon, 6 May 2024 14:00:47 +0000 (22:00 +0800)
committerHuacai Chen <chenhuacai@loongson.cn>
Mon, 6 May 2024 14:00:47 +0000 (22:00 +0800)
Instruction cpucfg can be used to get processor features. And there
is a trap exception when it is executed in VM mode, and also it can be
used to provide cpu features to VM. On real hardware cpucfg area 0 - 20
is used by now. Here one specified area 0x40000000 -- 0x400000ff is used
for KVM hypervisor to provide PV features, and the area can be extended
for other hypervisors in future. This area will never be used for real
HW, it is only used by software.

Signed-off-by: Bibo Mao <maobibo@loongson.cn>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
arch/loongarch/include/asm/inst.h
arch/loongarch/include/asm/loongarch.h
arch/loongarch/kvm/exit.c

index d8f637f9e400bc394efa69b9154eb2379d5bd5e0..ad120f924905045d269c7aba0c1221a736945c97 100644 (file)
@@ -67,6 +67,7 @@ enum reg2_op {
        revhd_op        = 0x11,
        extwh_op        = 0x16,
        extwb_op        = 0x17,
+       cpucfg_op       = 0x1b,
        iocsrrdb_op     = 0x19200,
        iocsrrdh_op     = 0x19201,
        iocsrrdw_op     = 0x19202,
index 46366e783c84112f40d965d10793bd14f5d4221e..dad6286d77389b64f2924c8b27e6f538352cd024 100644 (file)
 #define  CPUCFG48_VFPU_CG              BIT(2)
 #define  CPUCFG48_RAM_CG               BIT(3)
 
+/*
+ * CPUCFG index area: 0x40000000 -- 0x400000ff
+ * SW emulation for KVM hypervirsor
+ */
+#define CPUCFG_KVM_BASE                        0x40000000
+#define CPUCFG_KVM_SIZE                        0x100
+
+#define CPUCFG_KVM_SIG                 (CPUCFG_KVM_BASE + 0)
+#define  KVM_SIGNATURE                 "KVM\0"
+#define CPUCFG_KVM_FEATURE             (CPUCFG_KVM_BASE + 4)
+
 #ifndef __ASSEMBLY__
 
 /* CSR */
index f368673db395d262763052ce0ecea6f66cc77fde..3aa2dbf334733a9e4a0098a55602a99b7ee44e3c 100644 (file)
 #include <asm/kvm_vcpu.h>
 #include "trace.h"
 
+static int kvm_emu_cpucfg(struct kvm_vcpu *vcpu, larch_inst inst)
+{
+       int rd, rj;
+       unsigned int index;
+
+       if (inst.reg2_format.opcode != cpucfg_op)
+               return EMULATE_FAIL;
+
+       rd = inst.reg2_format.rd;
+       rj = inst.reg2_format.rj;
+       ++vcpu->stat.cpucfg_exits;
+       index = vcpu->arch.gprs[rj];
+
+       /*
+        * By LoongArch Reference Manual 2.2.10.5
+        * Return value is 0 for undefined CPUCFG index
+        *
+        * Disable preemption since hw gcsr is accessed
+        */
+       preempt_disable();
+       switch (index) {
+       case 0 ... (KVM_MAX_CPUCFG_REGS - 1):
+               vcpu->arch.gprs[rd] = vcpu->arch.cpucfg[index];
+               break;
+       case CPUCFG_KVM_SIG:
+               /* CPUCFG emulation between 0x40000000 -- 0x400000ff */
+               vcpu->arch.gprs[rd] = *(unsigned int *)KVM_SIGNATURE;
+               break;
+       default:
+               vcpu->arch.gprs[rd] = 0;
+               break;
+       }
+       preempt_enable();
+
+       return EMULATE_DONE;
+}
+
 static unsigned long kvm_emu_read_csr(struct kvm_vcpu *vcpu, int csrid)
 {
        unsigned long val = 0;
@@ -208,8 +245,6 @@ int kvm_emu_idle(struct kvm_vcpu *vcpu)
 
 static int kvm_trap_handle_gspr(struct kvm_vcpu *vcpu)
 {
-       int rd, rj;
-       unsigned int index;
        unsigned long curr_pc;
        larch_inst inst;
        enum emulation_result er = EMULATE_DONE;
@@ -224,21 +259,7 @@ static int kvm_trap_handle_gspr(struct kvm_vcpu *vcpu)
        er = EMULATE_FAIL;
        switch (((inst.word >> 24) & 0xff)) {
        case 0x0: /* CPUCFG GSPR */
-               if (inst.reg2_format.opcode == 0x1B) {
-                       rd = inst.reg2_format.rd;
-                       rj = inst.reg2_format.rj;
-                       ++vcpu->stat.cpucfg_exits;
-                       index = vcpu->arch.gprs[rj];
-                       er = EMULATE_DONE;
-                       /*
-                        * By LoongArch Reference Manual 2.2.10.5
-                        * return value is 0 for undefined cpucfg index
-                        */
-                       if (index < KVM_MAX_CPUCFG_REGS)
-                               vcpu->arch.gprs[rd] = vcpu->arch.cpucfg[index];
-                       else
-                               vcpu->arch.gprs[rd] = 0;
-               }
+               er = kvm_emu_cpucfg(vcpu, inst);
                break;
        case 0x4: /* CSR{RD,WR,XCHG} GSPR */
                er = kvm_handle_csr(vcpu, inst);