Skip to content

Commit 9753d30

Browse files
bibo-maochenhuacai
authored andcommitted
LoongArch: KVM: Add cpucfg area for kvm hypervisor
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 <[email protected]> Signed-off-by: Huacai Chen <[email protected]>
1 parent 372631b commit 9753d30

File tree

3 files changed

+50
-17
lines changed

3 files changed

+50
-17
lines changed

arch/loongarch/include/asm/inst.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ enum reg2_op {
6767
revhd_op = 0x11,
6868
extwh_op = 0x16,
6969
extwb_op = 0x17,
70+
cpucfg_op = 0x1b,
7071
iocsrrdb_op = 0x19200,
7172
iocsrrdh_op = 0x19201,
7273
iocsrrdw_op = 0x19202,

arch/loongarch/include/asm/loongarch.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,17 @@
158158
#define CPUCFG48_VFPU_CG BIT(2)
159159
#define CPUCFG48_RAM_CG BIT(3)
160160

161+
/*
162+
* CPUCFG index area: 0x40000000 -- 0x400000ff
163+
* SW emulation for KVM hypervirsor
164+
*/
165+
#define CPUCFG_KVM_BASE 0x40000000
166+
#define CPUCFG_KVM_SIZE 0x100
167+
168+
#define CPUCFG_KVM_SIG (CPUCFG_KVM_BASE + 0)
169+
#define KVM_SIGNATURE "KVM\0"
170+
#define CPUCFG_KVM_FEATURE (CPUCFG_KVM_BASE + 4)
171+
161172
#ifndef __ASSEMBLY__
162173

163174
/* CSR */

arch/loongarch/kvm/exit.c

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,43 @@
2020
#include <asm/kvm_vcpu.h>
2121
#include "trace.h"
2222

23+
static int kvm_emu_cpucfg(struct kvm_vcpu *vcpu, larch_inst inst)
24+
{
25+
int rd, rj;
26+
unsigned int index;
27+
28+
if (inst.reg2_format.opcode != cpucfg_op)
29+
return EMULATE_FAIL;
30+
31+
rd = inst.reg2_format.rd;
32+
rj = inst.reg2_format.rj;
33+
++vcpu->stat.cpucfg_exits;
34+
index = vcpu->arch.gprs[rj];
35+
36+
/*
37+
* By LoongArch Reference Manual 2.2.10.5
38+
* Return value is 0 for undefined CPUCFG index
39+
*
40+
* Disable preemption since hw gcsr is accessed
41+
*/
42+
preempt_disable();
43+
switch (index) {
44+
case 0 ... (KVM_MAX_CPUCFG_REGS - 1):
45+
vcpu->arch.gprs[rd] = vcpu->arch.cpucfg[index];
46+
break;
47+
case CPUCFG_KVM_SIG:
48+
/* CPUCFG emulation between 0x40000000 -- 0x400000ff */
49+
vcpu->arch.gprs[rd] = *(unsigned int *)KVM_SIGNATURE;
50+
break;
51+
default:
52+
vcpu->arch.gprs[rd] = 0;
53+
break;
54+
}
55+
preempt_enable();
56+
57+
return EMULATE_DONE;
58+
}
59+
2360
static unsigned long kvm_emu_read_csr(struct kvm_vcpu *vcpu, int csrid)
2461
{
2562
unsigned long val = 0;
@@ -208,8 +245,6 @@ int kvm_emu_idle(struct kvm_vcpu *vcpu)
208245

209246
static int kvm_trap_handle_gspr(struct kvm_vcpu *vcpu)
210247
{
211-
int rd, rj;
212-
unsigned int index;
213248
unsigned long curr_pc;
214249
larch_inst inst;
215250
enum emulation_result er = EMULATE_DONE;
@@ -224,21 +259,7 @@ static int kvm_trap_handle_gspr(struct kvm_vcpu *vcpu)
224259
er = EMULATE_FAIL;
225260
switch (((inst.word >> 24) & 0xff)) {
226261
case 0x0: /* CPUCFG GSPR */
227-
if (inst.reg2_format.opcode == 0x1B) {
228-
rd = inst.reg2_format.rd;
229-
rj = inst.reg2_format.rj;
230-
++vcpu->stat.cpucfg_exits;
231-
index = vcpu->arch.gprs[rj];
232-
er = EMULATE_DONE;
233-
/*
234-
* By LoongArch Reference Manual 2.2.10.5
235-
* return value is 0 for undefined cpucfg index
236-
*/
237-
if (index < KVM_MAX_CPUCFG_REGS)
238-
vcpu->arch.gprs[rd] = vcpu->arch.cpucfg[index];
239-
else
240-
vcpu->arch.gprs[rd] = 0;
241-
}
262+
er = kvm_emu_cpucfg(vcpu, inst);
242263
break;
243264
case 0x4: /* CSR{RD,WR,XCHG} GSPR */
244265
er = kvm_handle_csr(vcpu, inst);

0 commit comments

Comments
 (0)