Skip to content

Commit de56a94

Browse files
paulusmackavikivity
authored andcommitted
KVM: PPC: Add support for Book3S processors in hypervisor mode
This adds support for KVM running on 64-bit Book 3S processors, specifically POWER7, in hypervisor mode. Using hypervisor mode means that the guest can use the processor's supervisor mode. That means that the guest can execute privileged instructions and access privileged registers itself without trapping to the host. This gives excellent performance, but does mean that KVM cannot emulate a processor architecture other than the one that the hardware implements. This code assumes that the guest is running paravirtualized using the PAPR (Power Architecture Platform Requirements) interface, which is the interface that IBM's PowerVM hypervisor uses. That means that existing Linux distributions that run on IBM pSeries machines will also run under KVM without modification. In order to communicate the PAPR hypercalls to qemu, this adds a new KVM_EXIT_PAPR_HCALL exit code to include/linux/kvm.h. Currently the choice between book3s_hv support and book3s_pr support (i.e. the existing code, which runs the guest in user mode) has to be made at kernel configuration time, so a given kernel binary can only do one or the other. This new book3s_hv code doesn't support MMIO emulation at present. Since we are running paravirtualized guests, this isn't a serious restriction. With the guest running in supervisor mode, most exceptions go straight to the guest. We will never get data or instruction storage or segment interrupts, alignment interrupts, decrementer interrupts, program interrupts, single-step interrupts, etc., coming to the hypervisor from the guest. Therefore this introduces a new KVMTEST_NONHV macro for the exception entry path so that we don't have to do the KVM test on entry to those exception handlers. We do however get hypervisor decrementer, hypervisor data storage, hypervisor instruction storage, and hypervisor emulation assist interrupts, so we have to handle those. In hypervisor mode, real-mode accesses can access all of RAM, not just a limited amount. Therefore we put all the guest state in the vcpu.arch and use the shadow_vcpu in the PACA only for temporary scratch space. We allocate the vcpu with kzalloc rather than vzalloc, and we don't use anything in the kvmppc_vcpu_book3s struct, so we don't allocate it. We don't have a shared page with the guest, but we still need a kvm_vcpu_arch_shared struct to store the values of various registers, so we include one in the vcpu_arch struct. The POWER7 processor has a restriction that all threads in a core have to be in the same partition. MMU-on kernel code counts as a partition (partition 0), so we have to do a partition switch on every entry to and exit from the guest. At present we require the host and guest to run in single-thread mode because of this hardware restriction. This code allocates a hashed page table for the guest and initializes it with HPTEs for the guest's Virtual Real Memory Area (VRMA). We require that the guest memory is allocated using 16MB huge pages, in order to simplify the low-level memory management. This also means that we can get away without tracking paging activity in the host for now, since huge pages can't be paged or swapped. This also adds a few new exports needed by the book3s_hv code. Signed-off-by: Paul Mackerras <paulus@samba.org> Signed-off-by: Alexander Graf <agraf@suse.de>
1 parent 3c42bf8 commit de56a94

28 files changed

Lines changed: 2103 additions & 81 deletions

‎Documentation/virtual/kvm/api.txt‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,23 @@ Userspace can now handle the hypercall and when it's done modify the gprs as
15321532
necessary. Upon guest entry all guest GPRs will then be replaced by the values
15331533
in this struct.
15341534

1535+
/* KVM_EXIT_PAPR_HCALL */
1536+
struct {
1537+
__u64 nr;
1538+
__u64 ret;
1539+
__u64 args[9];
1540+
} papr_hcall;
1541+
1542+
This is used on 64-bit PowerPC when emulating a pSeries partition,
1543+
e.g. with the 'pseries' machine type in qemu. It occurs when the
1544+
guest does a hypercall using the 'sc 1' instruction. The 'nr' field
1545+
contains the hypercall number (from the guest R3), and 'args' contains
1546+
the arguments (from the guest R4 - R12). Userspace should put the
1547+
return code in 'ret' and any extra returned values in args[].
1548+
The possible hypercalls are defined in the Power Architecture Platform
1549+
Requirements (PAPR) document available from www.power.org (free
1550+
developer registration required to access it).
1551+
15351552
/* Fix the size of the union. */
15361553
char padding[256];
15371554
};

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,17 @@ do_kvm_##n: \
134134
#define KVM_HANDLER_SKIP(area, h, n)
135135
#endif
136136

137+
#ifdef CONFIG_KVM_BOOK3S_PR
138+
#define KVMTEST_PR(n) __KVMTEST(n)
139+
#define KVM_HANDLER_PR(area, h, n) __KVM_HANDLER(area, h, n)
140+
#define KVM_HANDLER_PR_SKIP(area, h, n) __KVM_HANDLER_SKIP(area, h, n)
141+
142+
#else
143+
#define KVMTEST_PR(n)
144+
#define KVM_HANDLER_PR(area, h, n)
145+
#define KVM_HANDLER_PR_SKIP(area, h, n)
146+
#endif
147+
137148
#define NOTEST(n)
138149

139150
/*
@@ -210,7 +221,7 @@ label##_pSeries: \
210221
HMT_MEDIUM; \
211222
SET_SCRATCH0(r13); /* save r13 */ \
212223
EXCEPTION_PROLOG_PSERIES(PACA_EXGEN, label##_common, \
213-
EXC_STD, KVMTEST, vec)
224+
EXC_STD, KVMTEST_PR, vec)
214225

215226
#define STD_EXCEPTION_HV(loc, vec, label) \
216227
. = loc; \
@@ -227,8 +238,8 @@ label##_hv: \
227238
beq masked_##h##interrupt
228239
#define _SOFTEN_TEST(h) __SOFTEN_TEST(h)
229240

230-
#define SOFTEN_TEST(vec) \
231-
KVMTEST(vec); \
241+
#define SOFTEN_TEST_PR(vec) \
242+
KVMTEST_PR(vec); \
232243
_SOFTEN_TEST(EXC_STD)
233244

234245
#define SOFTEN_TEST_HV(vec) \
@@ -248,7 +259,7 @@ label##_hv: \
248259
.globl label##_pSeries; \
249260
label##_pSeries: \
250261
_MASKABLE_EXCEPTION_PSERIES(vec, label, \
251-
EXC_STD, SOFTEN_TEST)
262+
EXC_STD, SOFTEN_TEST_PR)
252263

253264
#define MASKABLE_EXCEPTION_HV(loc, vec, label) \
254265
. = loc; \

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,12 @@
6464
#define BOOK3S_INTERRUPT_PROGRAM 0x700
6565
#define BOOK3S_INTERRUPT_FP_UNAVAIL 0x800
6666
#define BOOK3S_INTERRUPT_DECREMENTER 0x900
67+
#define BOOK3S_INTERRUPT_HV_DECREMENTER 0x980
6768
#define BOOK3S_INTERRUPT_SYSCALL 0xc00
6869
#define BOOK3S_INTERRUPT_TRACE 0xd00
70+
#define BOOK3S_INTERRUPT_H_DATA_STORAGE 0xe00
71+
#define BOOK3S_INTERRUPT_H_INST_STORAGE 0xe20
72+
#define BOOK3S_INTERRUPT_H_EMUL_ASSIST 0xe40
6973
#define BOOK3S_INTERRUPT_PERFMON 0xf00
7074
#define BOOK3S_INTERRUPT_ALTIVEC 0xf20
7175
#define BOOK3S_INTERRUPT_VSX 0xf40

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

Lines changed: 119 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ extern void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 new_msr);
116116
extern void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr);
117117
extern void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu);
118118
extern void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu);
119+
extern void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu);
119120
extern int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte);
120121
extern int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr);
121122
extern void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu);
@@ -127,10 +128,12 @@ extern int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu);
127128
extern void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
128129
extern int kvmppc_mmu_hpte_sysinit(void);
129130
extern void kvmppc_mmu_hpte_sysexit(void);
131+
extern int kvmppc_mmu_hv_init(void);
130132

131133
extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
132134
extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
133135
extern void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec);
136+
extern void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags);
134137
extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat,
135138
bool upper, u32 val);
136139
extern void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr);
@@ -140,6 +143,7 @@ extern pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn);
140143
extern void kvmppc_handler_lowmem_trampoline(void);
141144
extern void kvmppc_handler_trampoline_enter(void);
142145
extern void kvmppc_rmcall(ulong srr0, ulong srr1);
146+
extern void kvmppc_hv_entry_trampoline(void);
143147
extern void kvmppc_load_up_fpu(void);
144148
extern void kvmppc_load_up_altivec(void);
145149
extern void kvmppc_load_up_vsx(void);
@@ -151,6 +155,19 @@ static inline struct kvmppc_vcpu_book3s *to_book3s(struct kvm_vcpu *vcpu)
151155
return container_of(vcpu, struct kvmppc_vcpu_book3s, vcpu);
152156
}
153157

158+
extern void kvm_return_point(void);
159+
160+
/* Also add subarch specific defines */
161+
162+
#ifdef CONFIG_KVM_BOOK3S_32_HANDLER
163+
#include <asm/kvm_book3s_32.h>
164+
#endif
165+
#ifdef CONFIG_KVM_BOOK3S_64_HANDLER
166+
#include <asm/kvm_book3s_64.h>
167+
#endif
168+
169+
#ifdef CONFIG_KVM_BOOK3S_PR
170+
154171
static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu *vcpu)
155172
{
156173
return to_book3s(vcpu)->hior;
@@ -165,16 +182,6 @@ static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
165182
vcpu->arch.shared->int_pending = 0;
166183
}
167184

168-
static inline ulong dsisr(void)
169-
{
170-
ulong r;
171-
asm ( "mfdsisr %0 " : "=r" (r) );
172-
return r;
173-
}
174-
175-
extern void kvm_return_point(void);
176-
static inline struct kvmppc_book3s_shadow_vcpu *to_svcpu(struct kvm_vcpu *vcpu);
177-
178185
static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
179186
{
180187
if ( num < 14 ) {
@@ -281,6 +288,108 @@ static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
281288

282289
return crit;
283290
}
291+
#else /* CONFIG_KVM_BOOK3S_PR */
292+
293+
static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu *vcpu)
294+
{
295+
return 0;
296+
}
297+
298+
static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
299+
unsigned long pending_now, unsigned long old_pending)
300+
{
301+
/* Recalculate LPCR:MER based on the presence of
302+
* a pending external interrupt
303+
*/
304+
if (test_bit(BOOK3S_IRQPRIO_EXTERNAL, &pending_now) ||
305+
test_bit(BOOK3S_IRQPRIO_EXTERNAL_LEVEL, &pending_now))
306+
vcpu->arch.lpcr |= LPCR_MER;
307+
else
308+
vcpu->arch.lpcr &= ~((u64)LPCR_MER);
309+
}
310+
311+
static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
312+
{
313+
vcpu->arch.gpr[num] = val;
314+
}
315+
316+
static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
317+
{
318+
return vcpu->arch.gpr[num];
319+
}
320+
321+
static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
322+
{
323+
vcpu->arch.cr = val;
324+
}
325+
326+
static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
327+
{
328+
return vcpu->arch.cr;
329+
}
330+
331+
static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, u32 val)
332+
{
333+
vcpu->arch.xer = val;
334+
}
335+
336+
static inline u32 kvmppc_get_xer(struct kvm_vcpu *vcpu)
337+
{
338+
return vcpu->arch.xer;
339+
}
340+
341+
static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
342+
{
343+
vcpu->arch.ctr = val;
344+
}
345+
346+
static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
347+
{
348+
return vcpu->arch.ctr;
349+
}
350+
351+
static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
352+
{
353+
vcpu->arch.lr = val;
354+
}
355+
356+
static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
357+
{
358+
return vcpu->arch.lr;
359+
}
360+
361+
static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
362+
{
363+
vcpu->arch.pc = val;
364+
}
365+
366+
static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
367+
{
368+
return vcpu->arch.pc;
369+
}
370+
371+
static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu)
372+
{
373+
ulong pc = kvmppc_get_pc(vcpu);
374+
375+
/* Load the instruction manually if it failed to do so in the
376+
* exit path */
377+
if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED)
378+
kvmppc_ld(vcpu, &pc, sizeof(u32), &vcpu->arch.last_inst, false);
379+
380+
return vcpu->arch.last_inst;
381+
}
382+
383+
static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
384+
{
385+
return vcpu->arch.fault_dar;
386+
}
387+
388+
static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
389+
{
390+
return false;
391+
}
392+
#endif
284393

285394
/* Magic register values loaded into r3 and r4 before the 'sc' assembly
286395
* instruction for the OSI hypercalls */
@@ -289,12 +398,4 @@ static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
289398

290399
#define INS_DCBZ 0x7c0007ec
291400

292-
/* Also add subarch specific defines */
293-
294-
#ifdef CONFIG_PPC_BOOK3S_32
295-
#include <asm/kvm_book3s_32.h>
296-
#else
297-
#include <asm/kvm_book3s_64.h>
298-
#endif
299-
300401
#endif /* __ASM_KVM_BOOK3S_H__ */

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
#ifndef __ASM_KVM_BOOK3S_64_H__
2121
#define __ASM_KVM_BOOK3S_64_H__
2222

23+
#ifdef CONFIG_KVM_BOOK3S_PR
2324
static inline struct kvmppc_book3s_shadow_vcpu *to_svcpu(struct kvm_vcpu *vcpu)
2425
{
2526
return &get_paca()->shadow_vcpu;
2627
}
28+
#endif
2729

2830
#endif /* __ASM_KVM_BOOK3S_64_H__ */

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,22 @@ kvmppc_resume_\intno:
7070
struct kvmppc_host_state {
7171
ulong host_r1;
7272
ulong host_r2;
73+
ulong host_msr;
7374
ulong vmhandler;
7475
ulong scratch0;
7576
ulong scratch1;
7677
u8 in_guest;
78+
79+
#ifdef CONFIG_KVM_BOOK3S_64_HV
80+
struct kvm_vcpu *kvm_vcpu;
81+
u64 dabr;
82+
u64 host_mmcr[3];
83+
u32 host_pmc[6];
84+
u64 host_purr;
85+
u64 host_spurr;
86+
u64 host_dscr;
87+
u64 dec_expires;
88+
#endif
7789
};
7890

7991
struct kvmppc_book3s_shadow_vcpu {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,8 @@ static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
9393
return vcpu->arch.fault_dear;
9494
}
9595

96+
static inline ulong kvmppc_get_msr(struct kvm_vcpu *vcpu)
97+
{
98+
return vcpu->arch.shared->msr;
99+
}
96100
#endif /* __ASM_KVM_BOOKE_H__ */

0 commit comments

Comments
 (0)