Skip to content

Commit b590497

Browse files
Scott Woodavikivity
authored andcommitted
KVM: PPC: Paravirtualize SPRG4-7, ESR, PIR, MASn
This allows additional registers to be accessed by the guest in PR-mode KVM without trapping. SPRG4-7 are readable from userspace. On booke, KVM will sync these registers when it enters the guest, so that accesses from guest userspace will work. The guest kernel, OTOH, must consistently use either the real registers or the shared area between exits. This also applies to the already-paravirted SPRG3. On non-booke, it's not clear to what extent SPRG4-7 are supported (they're not architected for book3s, but exist on at least some classic chips). They are copied in the get/set regs ioctls, but I do not see any non-booke emulation. I also do not see any syncing with real registers (in PR-mode) including the user-readable SPRG3. This patch should not make that situation any worse. Signed-off-by: Scott Wood <scottwood@freescale.com> Signed-off-by: Alexander Graf <agraf@suse.de> Signed-off-by: Avi Kivity <avi@redhat.com>
1 parent 940b45e commit b590497

15 files changed

Lines changed: 339 additions & 169 deletions

File tree

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,6 @@ struct kvmppc_vcpu_e500 {
7171
u32 pid[E500_PID_NUM];
7272
u32 svr;
7373

74-
u32 mas0;
75-
u32 mas1;
76-
u32 mas2;
77-
u64 mas7_3;
78-
u32 mas4;
79-
u32 mas5;
80-
u32 mas6;
81-
8274
/* vcpu id table */
8375
struct vcpu_id_table *idt;
8476

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -318,18 +318,13 @@ struct kvm_vcpu_arch {
318318
u32 vrsave; /* also USPRG0 */
319319
u32 mmucr;
320320
ulong shadow_msr;
321-
ulong sprg4;
322-
ulong sprg5;
323-
ulong sprg6;
324-
ulong sprg7;
325321
ulong csrr0;
326322
ulong csrr1;
327323
ulong dsrr0;
328324
ulong dsrr1;
329325
ulong mcsrr0;
330326
ulong mcsrr1;
331327
ulong mcsr;
332-
ulong esr;
333328
u32 dec;
334329
u32 decar;
335330
u32 tbl;
@@ -338,7 +333,6 @@ struct kvm_vcpu_arch {
338333
u32 tsr;
339334
u32 ivor[64];
340335
ulong ivpr;
341-
u32 pir;
342336
u32 pvr;
343337

344338
u32 shadow_pid;

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,35 @@ struct kvm_vcpu_arch_shared {
3333
__u64 sprg3;
3434
__u64 srr0;
3535
__u64 srr1;
36-
__u64 dar;
36+
__u64 dar; /* dear on BookE */
3737
__u64 msr;
3838
__u32 dsisr;
3939
__u32 int_pending; /* Tells the guest if we have an interrupt */
4040
__u32 sr[16];
41+
__u32 mas0;
42+
__u32 mas1;
43+
__u64 mas7_3;
44+
__u64 mas2;
45+
__u32 mas4;
46+
__u32 mas6;
47+
__u32 esr;
48+
__u32 pir;
49+
50+
/*
51+
* SPRG4-7 are user-readable, so we can only keep these consistent
52+
* between the shared area and the real registers when there's an
53+
* intervening exit to KVM. This also applies to SPRG3 on some
54+
* chips.
55+
*
56+
* This suffices for access by guest userspace, since in PR-mode
57+
* KVM, an exit must occur when changing the guest's MSR[PR].
58+
* If the guest kernel writes to SPRG3-7 via the shared area, it
59+
* must also use the shared area for reading while in kernel space.
60+
*/
61+
__u64 sprg4;
62+
__u64 sprg5;
63+
__u64 sprg6;
64+
__u64 sprg7;
4165
};
4266

4367
#define KVM_SC_MAGIC_R0 0x4b564d21 /* "KVM!" */
@@ -47,7 +71,10 @@ struct kvm_vcpu_arch_shared {
4771

4872
#define KVM_FEATURE_MAGIC_PAGE 1
4973

50-
#define KVM_MAGIC_FEAT_SR (1 << 0)
74+
#define KVM_MAGIC_FEAT_SR (1 << 0)
75+
76+
/* MASn, ESR, PIR, and high SPRGs */
77+
#define KVM_MAGIC_FEAT_MAS0_TO_SPRG7 (1 << 1)
5178

5279
#ifdef __KERNEL__
5380

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,16 +426,23 @@ int main(void)
426426
DEFINE(VCPU_SPRG2, offsetof(struct kvm_vcpu, arch.shregs.sprg2));
427427
DEFINE(VCPU_SPRG3, offsetof(struct kvm_vcpu, arch.shregs.sprg3));
428428
#endif
429-
DEFINE(VCPU_SPRG4, offsetof(struct kvm_vcpu, arch.sprg4));
430-
DEFINE(VCPU_SPRG5, offsetof(struct kvm_vcpu, arch.sprg5));
431-
DEFINE(VCPU_SPRG6, offsetof(struct kvm_vcpu, arch.sprg6));
432-
DEFINE(VCPU_SPRG7, offsetof(struct kvm_vcpu, arch.sprg7));
429+
DEFINE(VCPU_SHARED_SPRG4, offsetof(struct kvm_vcpu_arch_shared, sprg4));
430+
DEFINE(VCPU_SHARED_SPRG5, offsetof(struct kvm_vcpu_arch_shared, sprg5));
431+
DEFINE(VCPU_SHARED_SPRG6, offsetof(struct kvm_vcpu_arch_shared, sprg6));
432+
DEFINE(VCPU_SHARED_SPRG7, offsetof(struct kvm_vcpu_arch_shared, sprg7));
433433
DEFINE(VCPU_SHADOW_PID, offsetof(struct kvm_vcpu, arch.shadow_pid));
434434
DEFINE(VCPU_SHADOW_PID1, offsetof(struct kvm_vcpu, arch.shadow_pid1));
435435
DEFINE(VCPU_SHARED, offsetof(struct kvm_vcpu, arch.shared));
436436
DEFINE(VCPU_SHARED_MSR, offsetof(struct kvm_vcpu_arch_shared, msr));
437437
DEFINE(VCPU_SHADOW_MSR, offsetof(struct kvm_vcpu, arch.shadow_msr));
438438

439+
DEFINE(VCPU_SHARED_MAS0, offsetof(struct kvm_vcpu_arch_shared, mas0));
440+
DEFINE(VCPU_SHARED_MAS1, offsetof(struct kvm_vcpu_arch_shared, mas1));
441+
DEFINE(VCPU_SHARED_MAS2, offsetof(struct kvm_vcpu_arch_shared, mas2));
442+
DEFINE(VCPU_SHARED_MAS7_3, offsetof(struct kvm_vcpu_arch_shared, mas7_3));
443+
DEFINE(VCPU_SHARED_MAS4, offsetof(struct kvm_vcpu_arch_shared, mas4));
444+
DEFINE(VCPU_SHARED_MAS6, offsetof(struct kvm_vcpu_arch_shared, mas6));
445+
439446
/* book3s */
440447
#ifdef CONFIG_KVM_BOOK3S_64_HV
441448
DEFINE(KVM_LPID, offsetof(struct kvm, arch.lpid));

‎arch/powerpc/kernel/kvm.c‎

Lines changed: 171 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,17 @@
4949
#define KVM_RT_30 0x03c00000
5050
#define KVM_MASK_RB 0x0000f800
5151
#define KVM_INST_MFMSR 0x7c0000a6
52-
#define KVM_INST_MFSPR_SPRG0 0x7c1042a6
53-
#define KVM_INST_MFSPR_SPRG1 0x7c1142a6
54-
#define KVM_INST_MFSPR_SPRG2 0x7c1242a6
55-
#define KVM_INST_MFSPR_SPRG3 0x7c1342a6
56-
#define KVM_INST_MFSPR_SRR0 0x7c1a02a6
57-
#define KVM_INST_MFSPR_SRR1 0x7c1b02a6
58-
#define KVM_INST_MFSPR_DAR 0x7c1302a6
59-
#define KVM_INST_MFSPR_DSISR 0x7c1202a6
60-
61-
#define KVM_INST_MTSPR_SPRG0 0x7c1043a6
62-
#define KVM_INST_MTSPR_SPRG1 0x7c1143a6
63-
#define KVM_INST_MTSPR_SPRG2 0x7c1243a6
64-
#define KVM_INST_MTSPR_SPRG3 0x7c1343a6
65-
#define KVM_INST_MTSPR_SRR0 0x7c1a03a6
66-
#define KVM_INST_MTSPR_SRR1 0x7c1b03a6
67-
#define KVM_INST_MTSPR_DAR 0x7c1303a6
68-
#define KVM_INST_MTSPR_DSISR 0x7c1203a6
52+
53+
#define SPR_FROM 0
54+
#define SPR_TO 0x100
55+
56+
#define KVM_INST_SPR(sprn, moveto) (0x7c0002a6 | \
57+
(((sprn) & 0x1f) << 16) | \
58+
(((sprn) & 0x3e0) << 6) | \
59+
(moveto))
60+
61+
#define KVM_INST_MFSPR(sprn) KVM_INST_SPR(sprn, SPR_FROM)
62+
#define KVM_INST_MTSPR(sprn) KVM_INST_SPR(sprn, SPR_TO)
6963

7064
#define KVM_INST_TLBSYNC 0x7c00046c
7165
#define KVM_INST_MTMSRD_L0 0x7c000164
@@ -440,56 +434,191 @@ static void kvm_check_ins(u32 *inst, u32 features)
440434
case KVM_INST_MFMSR:
441435
kvm_patch_ins_ld(inst, magic_var(msr), inst_rt);
442436
break;
443-
case KVM_INST_MFSPR_SPRG0:
437+
case KVM_INST_MFSPR(SPRN_SPRG0):
444438
kvm_patch_ins_ld(inst, magic_var(sprg0), inst_rt);
445439
break;
446-
case KVM_INST_MFSPR_SPRG1:
440+
case KVM_INST_MFSPR(SPRN_SPRG1):
447441
kvm_patch_ins_ld(inst, magic_var(sprg1), inst_rt);
448442
break;
449-
case KVM_INST_MFSPR_SPRG2:
443+
case KVM_INST_MFSPR(SPRN_SPRG2):
450444
kvm_patch_ins_ld(inst, magic_var(sprg2), inst_rt);
451445
break;
452-
case KVM_INST_MFSPR_SPRG3:
446+
case KVM_INST_MFSPR(SPRN_SPRG3):
453447
kvm_patch_ins_ld(inst, magic_var(sprg3), inst_rt);
454448
break;
455-
case KVM_INST_MFSPR_SRR0:
449+
case KVM_INST_MFSPR(SPRN_SRR0):
456450
kvm_patch_ins_ld(inst, magic_var(srr0), inst_rt);
457451
break;
458-
case KVM_INST_MFSPR_SRR1:
452+
case KVM_INST_MFSPR(SPRN_SRR1):
459453
kvm_patch_ins_ld(inst, magic_var(srr1), inst_rt);
460454
break;
461-
case KVM_INST_MFSPR_DAR:
455+
#ifdef CONFIG_BOOKE
456+
case KVM_INST_MFSPR(SPRN_DEAR):
457+
#else
458+
case KVM_INST_MFSPR(SPRN_DAR):
459+
#endif
462460
kvm_patch_ins_ld(inst, magic_var(dar), inst_rt);
463461
break;
464-
case KVM_INST_MFSPR_DSISR:
462+
case KVM_INST_MFSPR(SPRN_DSISR):
465463
kvm_patch_ins_lwz(inst, magic_var(dsisr), inst_rt);
466464
break;
467465

466+
#ifdef CONFIG_PPC_BOOK3E_MMU
467+
case KVM_INST_MFSPR(SPRN_MAS0):
468+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
469+
kvm_patch_ins_lwz(inst, magic_var(mas0), inst_rt);
470+
break;
471+
case KVM_INST_MFSPR(SPRN_MAS1):
472+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
473+
kvm_patch_ins_lwz(inst, magic_var(mas1), inst_rt);
474+
break;
475+
case KVM_INST_MFSPR(SPRN_MAS2):
476+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
477+
kvm_patch_ins_ld(inst, magic_var(mas2), inst_rt);
478+
break;
479+
case KVM_INST_MFSPR(SPRN_MAS3):
480+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
481+
kvm_patch_ins_lwz(inst, magic_var(mas7_3) + 4, inst_rt);
482+
break;
483+
case KVM_INST_MFSPR(SPRN_MAS4):
484+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
485+
kvm_patch_ins_lwz(inst, magic_var(mas4), inst_rt);
486+
break;
487+
case KVM_INST_MFSPR(SPRN_MAS6):
488+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
489+
kvm_patch_ins_lwz(inst, magic_var(mas6), inst_rt);
490+
break;
491+
case KVM_INST_MFSPR(SPRN_MAS7):
492+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
493+
kvm_patch_ins_lwz(inst, magic_var(mas7_3), inst_rt);
494+
break;
495+
#endif /* CONFIG_PPC_BOOK3E_MMU */
496+
497+
case KVM_INST_MFSPR(SPRN_SPRG4):
498+
#ifdef CONFIG_BOOKE
499+
case KVM_INST_MFSPR(SPRN_SPRG4R):
500+
#endif
501+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
502+
kvm_patch_ins_ld(inst, magic_var(sprg4), inst_rt);
503+
break;
504+
case KVM_INST_MFSPR(SPRN_SPRG5):
505+
#ifdef CONFIG_BOOKE
506+
case KVM_INST_MFSPR(SPRN_SPRG5R):
507+
#endif
508+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
509+
kvm_patch_ins_ld(inst, magic_var(sprg5), inst_rt);
510+
break;
511+
case KVM_INST_MFSPR(SPRN_SPRG6):
512+
#ifdef CONFIG_BOOKE
513+
case KVM_INST_MFSPR(SPRN_SPRG6R):
514+
#endif
515+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
516+
kvm_patch_ins_ld(inst, magic_var(sprg6), inst_rt);
517+
break;
518+
case KVM_INST_MFSPR(SPRN_SPRG7):
519+
#ifdef CONFIG_BOOKE
520+
case KVM_INST_MFSPR(SPRN_SPRG7R):
521+
#endif
522+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
523+
kvm_patch_ins_ld(inst, magic_var(sprg7), inst_rt);
524+
break;
525+
526+
#ifdef CONFIG_BOOKE
527+
case KVM_INST_MFSPR(SPRN_ESR):
528+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
529+
kvm_patch_ins_lwz(inst, magic_var(esr), inst_rt);
530+
break;
531+
#endif
532+
533+
case KVM_INST_MFSPR(SPRN_PIR):
534+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
535+
kvm_patch_ins_lwz(inst, magic_var(pir), inst_rt);
536+
break;
537+
538+
468539
/* Stores */
469-
case KVM_INST_MTSPR_SPRG0:
540+
case KVM_INST_MTSPR(SPRN_SPRG0):
470541
kvm_patch_ins_std(inst, magic_var(sprg0), inst_rt);
471542
break;
472-
case KVM_INST_MTSPR_SPRG1:
543+
case KVM_INST_MTSPR(SPRN_SPRG1):
473544
kvm_patch_ins_std(inst, magic_var(sprg1), inst_rt);
474545
break;
475-
case KVM_INST_MTSPR_SPRG2:
546+
case KVM_INST_MTSPR(SPRN_SPRG2):
476547
kvm_patch_ins_std(inst, magic_var(sprg2), inst_rt);
477548
break;
478-
case KVM_INST_MTSPR_SPRG3:
549+
case KVM_INST_MTSPR(SPRN_SPRG3):
479550
kvm_patch_ins_std(inst, magic_var(sprg3), inst_rt);
480551
break;
481-
case KVM_INST_MTSPR_SRR0:
552+
case KVM_INST_MTSPR(SPRN_SRR0):
482553
kvm_patch_ins_std(inst, magic_var(srr0), inst_rt);
483554
break;
484-
case KVM_INST_MTSPR_SRR1:
555+
case KVM_INST_MTSPR(SPRN_SRR1):
485556
kvm_patch_ins_std(inst, magic_var(srr1), inst_rt);
486557
break;
487-
case KVM_INST_MTSPR_DAR:
558+
#ifdef CONFIG_BOOKE
559+
case KVM_INST_MTSPR(SPRN_DEAR):
560+
#else
561+
case KVM_INST_MTSPR(SPRN_DAR):
562+
#endif
488563
kvm_patch_ins_std(inst, magic_var(dar), inst_rt);
489564
break;
490-
case KVM_INST_MTSPR_DSISR:
565+
case KVM_INST_MTSPR(SPRN_DSISR):
491566
kvm_patch_ins_stw(inst, magic_var(dsisr), inst_rt);
492567
break;
568+
#ifdef CONFIG_PPC_BOOK3E_MMU
569+
case KVM_INST_MTSPR(SPRN_MAS0):
570+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
571+
kvm_patch_ins_stw(inst, magic_var(mas0), inst_rt);
572+
break;
573+
case KVM_INST_MTSPR(SPRN_MAS1):
574+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
575+
kvm_patch_ins_stw(inst, magic_var(mas1), inst_rt);
576+
break;
577+
case KVM_INST_MTSPR(SPRN_MAS2):
578+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
579+
kvm_patch_ins_std(inst, magic_var(mas2), inst_rt);
580+
break;
581+
case KVM_INST_MTSPR(SPRN_MAS3):
582+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
583+
kvm_patch_ins_stw(inst, magic_var(mas7_3) + 4, inst_rt);
584+
break;
585+
case KVM_INST_MTSPR(SPRN_MAS4):
586+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
587+
kvm_patch_ins_stw(inst, magic_var(mas4), inst_rt);
588+
break;
589+
case KVM_INST_MTSPR(SPRN_MAS6):
590+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
591+
kvm_patch_ins_stw(inst, magic_var(mas6), inst_rt);
592+
break;
593+
case KVM_INST_MTSPR(SPRN_MAS7):
594+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
595+
kvm_patch_ins_stw(inst, magic_var(mas7_3), inst_rt);
596+
break;
597+
#endif /* CONFIG_PPC_BOOK3E_MMU */
598+
599+
case KVM_INST_MTSPR(SPRN_SPRG4):
600+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
601+
kvm_patch_ins_std(inst, magic_var(sprg4), inst_rt);
602+
break;
603+
case KVM_INST_MTSPR(SPRN_SPRG5):
604+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
605+
kvm_patch_ins_std(inst, magic_var(sprg5), inst_rt);
606+
break;
607+
case KVM_INST_MTSPR(SPRN_SPRG6):
608+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
609+
kvm_patch_ins_std(inst, magic_var(sprg6), inst_rt);
610+
break;
611+
case KVM_INST_MTSPR(SPRN_SPRG7):
612+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
613+
kvm_patch_ins_std(inst, magic_var(sprg7), inst_rt);
614+
break;
615+
616+
#ifdef CONFIG_BOOKE
617+
case KVM_INST_MTSPR(SPRN_ESR):
618+
if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
619+
kvm_patch_ins_stw(inst, magic_var(esr), inst_rt);
620+
break;
621+
#endif
493622

494623
/* Nops */
495624
case KVM_INST_TLBSYNC:
@@ -556,9 +685,18 @@ static void kvm_use_magic_page(void)
556685
start = (void*)_stext;
557686
end = (void*)_etext;
558687

688+
/*
689+
* Being interrupted in the middle of patching would
690+
* be bad for SPRG4-7, which KVM can't keep in sync
691+
* with emulated accesses because reads don't trap.
692+
*/
693+
local_irq_disable();
694+
559695
for (p = start; p < end; p++)
560696
kvm_check_ins(p, features);
561697

698+
local_irq_enable();
699+
562700
printk(KERN_INFO "KVM: Live patching for a fast VM %s\n",
563701
kvm_patching_worked ? "worked" : "failed");
564702
}

0 commit comments

Comments
 (0)