Skip to content

Commit 3c42bf8

Browse files
paulusmackavikivity
authored andcommitted
KVM: PPC: Split host-state fields out of kvmppc_book3s_shadow_vcpu
There are several fields in struct kvmppc_book3s_shadow_vcpu that temporarily store bits of host state while a guest is running, rather than anything relating to the particular guest or vcpu. This splits them out into a new kvmppc_host_state structure and modifies the definitions in asm-offsets.c to suit. On 32-bit, we have a kvmppc_host_state structure inside the kvmppc_book3s_shadow_vcpu since the assembly code needs to be able to get to them both with one pointer. On 64-bit they are separate fields in the PACA. This means that on 64-bit we don't need to copy the kvmppc_host_state in and out on vcpu load/unload, and in future will mean that the book3s_hv code doesn't need a shadow_vcpu struct in the PACA at all. That does mean that we have to be careful not to rely on any values persisting in the hstate field of the paca across any point where we could block or get preempted. Signed-off-by: Paul Mackerras <paulus@samba.org> Signed-off-by: Alexander Graf <agraf@suse.de>
1 parent 923c53c commit 3c42bf8

8 files changed

Lines changed: 127 additions & 120 deletions

File tree

‎arch/powerpc/include/asm/exception-64s.h‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,16 @@
9696
EXCEPTION_PROLOG_PSERIES_1(label, h);
9797

9898
#define __KVMTEST(n) \
99-
lbz r10,PACA_KVM_SVCPU+SVCPU_IN_GUEST(r13); \
99+
lbz r10,HSTATE_IN_GUEST(r13); \
100100
cmpwi r10,0; \
101101
bne do_kvm_##n
102102

103103
#define __KVM_HANDLER(area, h, n) \
104104
do_kvm_##n: \
105105
ld r10,area+EX_R10(r13); \
106-
stw r9,PACA_KVM_SVCPU+SVCPU_SCRATCH1(r13); \
106+
stw r9,HSTATE_SCRATCH1(r13); \
107107
ld r9,area+EX_R9(r13); \
108-
std r12,PACA_KVM_SVCPU+SVCPU_SCRATCH0(r13); \
108+
std r12,HSTATE_SCRATCH0(r13); \
109109
li r12,n; \
110110
b kvmppc_interrupt
111111

@@ -114,9 +114,9 @@ do_kvm_##n: \
114114
cmpwi r10,KVM_GUEST_MODE_SKIP; \
115115
ld r10,area+EX_R10(r13); \
116116
beq 89f; \
117-
stw r9,PACA_KVM_SVCPU+SVCPU_SCRATCH1(r13); \
117+
stw r9,HSTATE_SCRATCH1(r13); \
118118
ld r9,area+EX_R9(r13); \
119-
std r12,PACA_KVM_SVCPU+SVCPU_SCRATCH0(r13); \
119+
std r12,HSTATE_SCRATCH0(r13); \
120120
li r12,n; \
121121
b kvmppc_interrupt; \
122122
89: mtocrf 0x80,r9; \

‎arch/powerpc/include/asm/kvm_book3s_asm.h‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ kvmppc_resume_\intno:
6060

6161
#else /*__ASSEMBLY__ */
6262

63+
/*
64+
* This struct goes in the PACA on 64-bit processors. It is used
65+
* to store host state that needs to be saved when we enter a guest
66+
* and restored when we exit, but isn't specific to any particular
67+
* guest or vcpu. It also has some scratch fields used by the guest
68+
* exit code.
69+
*/
70+
struct kvmppc_host_state {
71+
ulong host_r1;
72+
ulong host_r2;
73+
ulong vmhandler;
74+
ulong scratch0;
75+
ulong scratch1;
76+
u8 in_guest;
77+
};
78+
6379
struct kvmppc_book3s_shadow_vcpu {
6480
ulong gpr[14];
6581
u32 cr;
@@ -73,17 +89,12 @@ struct kvmppc_book3s_shadow_vcpu {
7389
ulong shadow_srr1;
7490
ulong fault_dar;
7591

76-
ulong host_r1;
77-
ulong host_r2;
78-
ulong handler;
79-
ulong scratch0;
80-
ulong scratch1;
81-
ulong vmhandler;
82-
u8 in_guest;
83-
8492
#ifdef CONFIG_PPC_BOOK3S_32
8593
u32 sr[16]; /* Guest SRs */
94+
95+
struct kvmppc_host_state hstate;
8696
#endif
97+
8798
#ifdef CONFIG_PPC_BOOK3S_64
8899
u8 slb_max; /* highest used guest slb entry */
89100
struct {

‎arch/powerpc/include/asm/paca.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ struct paca_struct {
149149
#ifdef CONFIG_KVM_BOOK3S_HANDLER
150150
/* We use this to store guest state in */
151151
struct kvmppc_book3s_shadow_vcpu shadow_vcpu;
152+
struct kvmppc_host_state kvm_hstate;
152153
#endif
153154
};
154155

‎arch/powerpc/kernel/asm-offsets.c‎

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,6 @@ int main(void)
198198
DEFINE(PACA_USER_TIME, offsetof(struct paca_struct, user_time));
199199
DEFINE(PACA_SYSTEM_TIME, offsetof(struct paca_struct, system_time));
200200
DEFINE(PACA_TRAP_SAVE, offsetof(struct paca_struct, trap_save));
201-
#ifdef CONFIG_KVM_BOOK3S_64_HANDLER
202-
DEFINE(PACA_KVM_SVCPU, offsetof(struct paca_struct, shadow_vcpu));
203-
DEFINE(SVCPU_SLB, offsetof(struct kvmppc_book3s_shadow_vcpu, slb));
204-
DEFINE(SVCPU_SLB_MAX, offsetof(struct kvmppc_book3s_shadow_vcpu, slb_max));
205-
#endif
206201
#endif /* CONFIG_PPC64 */
207202

208203
/* RTAS */
@@ -416,49 +411,54 @@ int main(void)
416411
DEFINE(VCPU_HIGHMEM_HANDLER, offsetof(struct kvm_vcpu, arch.highmem_handler));
417412
DEFINE(VCPU_RMCALL, offsetof(struct kvm_vcpu, arch.rmcall));
418413
DEFINE(VCPU_HFLAGS, offsetof(struct kvm_vcpu, arch.hflags));
419-
DEFINE(VCPU_SVCPU, offsetof(struct kvmppc_vcpu_book3s, shadow_vcpu) -
420-
offsetof(struct kvmppc_vcpu_book3s, vcpu));
421-
DEFINE(SVCPU_CR, offsetof(struct kvmppc_book3s_shadow_vcpu, cr));
422-
DEFINE(SVCPU_XER, offsetof(struct kvmppc_book3s_shadow_vcpu, xer));
423-
DEFINE(SVCPU_CTR, offsetof(struct kvmppc_book3s_shadow_vcpu, ctr));
424-
DEFINE(SVCPU_LR, offsetof(struct kvmppc_book3s_shadow_vcpu, lr));
425-
DEFINE(SVCPU_PC, offsetof(struct kvmppc_book3s_shadow_vcpu, pc));
426-
DEFINE(SVCPU_R0, offsetof(struct kvmppc_book3s_shadow_vcpu, gpr[0]));
427-
DEFINE(SVCPU_R1, offsetof(struct kvmppc_book3s_shadow_vcpu, gpr[1]));
428-
DEFINE(SVCPU_R2, offsetof(struct kvmppc_book3s_shadow_vcpu, gpr[2]));
429-
DEFINE(SVCPU_R3, offsetof(struct kvmppc_book3s_shadow_vcpu, gpr[3]));
430-
DEFINE(SVCPU_R4, offsetof(struct kvmppc_book3s_shadow_vcpu, gpr[4]));
431-
DEFINE(SVCPU_R5, offsetof(struct kvmppc_book3s_shadow_vcpu, gpr[5]));
432-
DEFINE(SVCPU_R6, offsetof(struct kvmppc_book3s_shadow_vcpu, gpr[6]));
433-
DEFINE(SVCPU_R7, offsetof(struct kvmppc_book3s_shadow_vcpu, gpr[7]));
434-
DEFINE(SVCPU_R8, offsetof(struct kvmppc_book3s_shadow_vcpu, gpr[8]));
435-
DEFINE(SVCPU_R9, offsetof(struct kvmppc_book3s_shadow_vcpu, gpr[9]));
436-
DEFINE(SVCPU_R10, offsetof(struct kvmppc_book3s_shadow_vcpu, gpr[10]));
437-
DEFINE(SVCPU_R11, offsetof(struct kvmppc_book3s_shadow_vcpu, gpr[11]));
438-
DEFINE(SVCPU_R12, offsetof(struct kvmppc_book3s_shadow_vcpu, gpr[12]));
439-
DEFINE(SVCPU_R13, offsetof(struct kvmppc_book3s_shadow_vcpu, gpr[13]));
440-
DEFINE(SVCPU_HOST_R1, offsetof(struct kvmppc_book3s_shadow_vcpu, host_r1));
441-
DEFINE(SVCPU_HOST_R2, offsetof(struct kvmppc_book3s_shadow_vcpu, host_r2));
442-
DEFINE(SVCPU_VMHANDLER, offsetof(struct kvmppc_book3s_shadow_vcpu,
443-
vmhandler));
444-
DEFINE(SVCPU_SCRATCH0, offsetof(struct kvmppc_book3s_shadow_vcpu,
445-
scratch0));
446-
DEFINE(SVCPU_SCRATCH1, offsetof(struct kvmppc_book3s_shadow_vcpu,
447-
scratch1));
448-
DEFINE(SVCPU_IN_GUEST, offsetof(struct kvmppc_book3s_shadow_vcpu,
449-
in_guest));
450-
DEFINE(SVCPU_FAULT_DSISR, offsetof(struct kvmppc_book3s_shadow_vcpu,
451-
fault_dsisr));
452-
DEFINE(SVCPU_FAULT_DAR, offsetof(struct kvmppc_book3s_shadow_vcpu,
453-
fault_dar));
454-
DEFINE(SVCPU_LAST_INST, offsetof(struct kvmppc_book3s_shadow_vcpu,
455-
last_inst));
456-
DEFINE(SVCPU_SHADOW_SRR1, offsetof(struct kvmppc_book3s_shadow_vcpu,
457-
shadow_srr1));
414+
415+
#ifdef CONFIG_PPC_BOOK3S_64
416+
# define SVCPU_FIELD(x, f) DEFINE(x, offsetof(struct paca_struct, shadow_vcpu.f))
417+
# define HSTATE_FIELD(x, f) DEFINE(x, offsetof(struct paca_struct, kvm_hstate.f))
418+
#else /* 32-bit */
419+
# define SVCPU_FIELD(x, f) DEFINE(x, offsetof(struct kvmppc_book3s_shadow_vcpu, f))
420+
# define HSTATE_FIELD(x, f) DEFINE(x, offsetof(struct kvmppc_book3s_shadow_vcpu, hstate.f))
421+
#endif
422+
423+
SVCPU_FIELD(SVCPU_CR, cr);
424+
SVCPU_FIELD(SVCPU_XER, xer);
425+
SVCPU_FIELD(SVCPU_CTR, ctr);
426+
SVCPU_FIELD(SVCPU_LR, lr);
427+
SVCPU_FIELD(SVCPU_PC, pc);
428+
SVCPU_FIELD(SVCPU_R0, gpr[0]);
429+
SVCPU_FIELD(SVCPU_R1, gpr[1]);
430+
SVCPU_FIELD(SVCPU_R2, gpr[2]);
431+
SVCPU_FIELD(SVCPU_R3, gpr[3]);
432+
SVCPU_FIELD(SVCPU_R4, gpr[4]);
433+
SVCPU_FIELD(SVCPU_R5, gpr[5]);
434+
SVCPU_FIELD(SVCPU_R6, gpr[6]);
435+
SVCPU_FIELD(SVCPU_R7, gpr[7]);
436+
SVCPU_FIELD(SVCPU_R8, gpr[8]);
437+
SVCPU_FIELD(SVCPU_R9, gpr[9]);
438+
SVCPU_FIELD(SVCPU_R10, gpr[10]);
439+
SVCPU_FIELD(SVCPU_R11, gpr[11]);
440+
SVCPU_FIELD(SVCPU_R12, gpr[12]);
441+
SVCPU_FIELD(SVCPU_R13, gpr[13]);
442+
SVCPU_FIELD(SVCPU_FAULT_DSISR, fault_dsisr);
443+
SVCPU_FIELD(SVCPU_FAULT_DAR, fault_dar);
444+
SVCPU_FIELD(SVCPU_LAST_INST, last_inst);
445+
SVCPU_FIELD(SVCPU_SHADOW_SRR1, shadow_srr1);
458446
#ifdef CONFIG_PPC_BOOK3S_32
459-
DEFINE(SVCPU_SR, offsetof(struct kvmppc_book3s_shadow_vcpu, sr));
447+
SVCPU_FIELD(SVCPU_SR, sr);
460448
#endif
461-
#else
449+
#ifdef CONFIG_PPC64
450+
SVCPU_FIELD(SVCPU_SLB, slb);
451+
SVCPU_FIELD(SVCPU_SLB_MAX, slb_max);
452+
#endif
453+
454+
HSTATE_FIELD(HSTATE_HOST_R1, host_r1);
455+
HSTATE_FIELD(HSTATE_HOST_R2, host_r2);
456+
HSTATE_FIELD(HSTATE_VMHANDLER, vmhandler);
457+
HSTATE_FIELD(HSTATE_SCRATCH0, scratch0);
458+
HSTATE_FIELD(HSTATE_SCRATCH1, scratch1);
459+
HSTATE_FIELD(HSTATE_IN_GUEST, in_guest);
460+
461+
#else /* CONFIG_PPC_BOOK3S */
462462
DEFINE(VCPU_CR, offsetof(struct kvm_vcpu, arch.cr));
463463
DEFINE(VCPU_XER, offsetof(struct kvm_vcpu, arch.xer));
464464
DEFINE(VCPU_LR, offsetof(struct kvm_vcpu, arch.lr));
@@ -468,7 +468,7 @@ int main(void)
468468
DEFINE(VCPU_FAULT_DEAR, offsetof(struct kvm_vcpu, arch.fault_dear));
469469
DEFINE(VCPU_FAULT_ESR, offsetof(struct kvm_vcpu, arch.fault_esr));
470470
#endif /* CONFIG_PPC_BOOK3S */
471-
#endif
471+
#endif /* CONFIG_KVM */
472472

473473
#ifdef CONFIG_KVM_GUEST
474474
DEFINE(KVM_MAGIC_SCRATCH1, offsetof(struct kvm_vcpu_arch_shared,

‎arch/powerpc/kernel/exceptions-64s.S‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ data_access_check_stab:
298298
srdi r10,r10,60
299299
rlwimi r10,r9,16,0x20
300300
#ifdef CONFIG_KVM_BOOK3S_64_HANDLER
301-
lbz r9,PACA_KVM_SVCPU+SVCPU_IN_GUEST(r13)
301+
lbz r9,HSTATE_IN_GUEST(r13)
302302
rlwimi r10,r9,8,0x300
303303
#endif
304304
mfcr r9

‎arch/powerpc/kvm/book3s_interrupts.S‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
#define ULONG_SIZE 8
3030
#define FUNC(name) GLUE(.,name)
3131

32-
#define GET_SHADOW_VCPU(reg) \
33-
addi reg, r13, PACA_KVM_SVCPU
32+
#define GET_SHADOW_VCPU_R13
3433

3534
#define DISABLE_INTERRUPTS \
3635
mfmsr r0; \
@@ -43,8 +42,8 @@
4342
#define ULONG_SIZE 4
4443
#define FUNC(name) name
4544

46-
#define GET_SHADOW_VCPU(reg) \
47-
lwz reg, (THREAD + THREAD_KVM_SVCPU)(r2)
45+
#define GET_SHADOW_VCPU_R13 \
46+
lwz r13, (THREAD + THREAD_KVM_SVCPU)(r2)
4847

4948
#define DISABLE_INTERRUPTS \
5049
mfmsr r0; \
@@ -107,17 +106,11 @@ kvm_start_entry:
107106
/* Load non-volatile guest state from the vcpu */
108107
VCPU_LOAD_NVGPRS(r4)
109108

110-
GET_SHADOW_VCPU(r5)
111-
112-
/* Save R1/R2 in the PACA */
113-
PPC_STL r1, SVCPU_HOST_R1(r5)
114-
PPC_STL r2, SVCPU_HOST_R2(r5)
109+
kvm_start_lightweight:
115110

116-
/* XXX swap in/out on load? */
111+
GET_SHADOW_VCPU_R13
117112
PPC_LL r3, VCPU_HIGHMEM_HANDLER(r4)
118-
PPC_STL r3, SVCPU_VMHANDLER(r5)
119-
120-
kvm_start_lightweight:
113+
PPC_STL r3, HSTATE_VMHANDLER(r13)
121114

122115
PPC_LL r10, VCPU_SHADOW_MSR(r4) /* r10 = vcpu->arch.shadow_msr */
123116

‎arch/powerpc/kvm/book3s_rmhandlers.S‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#if defined(CONFIG_PPC_BOOK3S_64)
3737

3838
#define LOAD_SHADOW_VCPU(reg) GET_PACA(reg)
39-
#define SHADOW_VCPU_OFF PACA_KVM_SVCPU
4039
#define MSR_NOIRQ MSR_KERNEL & ~(MSR_IR | MSR_DR)
4140
#define FUNC(name) GLUE(.,name)
4241

@@ -66,7 +65,6 @@ kvmppc_skip_Hinterrupt:
6665

6766
#elif defined(CONFIG_PPC_BOOK3S_32)
6867

69-
#define SHADOW_VCPU_OFF 0
7068
#define MSR_NOIRQ MSR_KERNEL
7169
#define FUNC(name) name
7270

@@ -96,14 +94,14 @@ kvmppc_trampoline_\intno:
9694
b kvmppc_resume_\intno /* Get back original handler */
9795

9896
1: tophys(r13, r13)
99-
stw r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH1)(r13)
97+
stw r12, HSTATE_SCRATCH1(r13)
10098
mfspr r12, SPRN_SPRG_SCRATCH1
101-
stw r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH0)(r13)
102-
lbz r12, (SHADOW_VCPU_OFF + SVCPU_IN_GUEST)(r13)
99+
stw r12, HSTATE_SCRATCH0(r13)
100+
lbz r12, HSTATE_IN_GUEST(r13)
103101
cmpwi r12, KVM_GUEST_MODE_NONE
104102
bne ..kvmppc_handler_hasmagic_\intno
105103
/* No KVM guest? Then jump back to the Linux handler! */
106-
lwz r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH1)(r13)
104+
lwz r12, HSTATE_SCRATCH1(r13)
107105
b 2b
108106

109107
/* Now we know we're handling a KVM guest */
@@ -146,8 +144,8 @@ INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_ALTIVEC
146144
*
147145
* R12 = free
148146
* R13 = Shadow VCPU (PACA)
149-
* SVCPU.SCRATCH0 = guest R12
150-
* SVCPU.SCRATCH1 = guest CR
147+
* HSTATE.SCRATCH0 = guest R12
148+
* HSTATE.SCRATCH1 = guest CR
151149
* SPRG_SCRATCH0 = guest R13
152150
*
153151
*/
@@ -159,9 +157,9 @@ kvmppc_handler_skip_ins:
159157
mtsrr0 r12
160158

161159
/* Clean up all state */
162-
lwz r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH1)(r13)
160+
lwz r12, HSTATE_SCRATCH1(r13)
163161
mtcr r12
164-
PPC_LL r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH0)(r13)
162+
PPC_LL r12, HSTATE_SCRATCH0(r13)
165163
GET_SCRATCH0(r13)
166164

167165
/* And get back into the code */

0 commit comments

Comments
 (0)