Add KVM support for Book3s_64 (PPC64) hosts v4
cover Add KVM support for Book3s_64 (PPC64) hosts v4
KVM for PowerPC only supports embedded cores at the moment.
While it makes sense to virtualize on small machines, it's even more fun to do so on big boxes. So I figured we need KVM for PowerPC64 as well.
This patchset implements KVM support for Book3s_64 hosts and guest support for Book3s_64 and G3/G4.
To really make use of this, you also need a recent version of qemu.
Don't want to apply patches? Get the git tree!
$ git clone git://csgraf.de/kvm $ git checkout origin/ppc-v4
V1 -> V2:
- extend sregs with padding
- new naming scheme (ppc64 -> book3s_64; 74xx -> book3s_32)
- to_phys -> in-kernel tophys()
- loadimm -> LOAD_REG_IMMEDIATE
- call .ko kvm.ko
- set magic paca bit later
- run guest code with PACA->soft_enabled=true
- pt_regs for host state saving (guest too?)
- only do HV dcbz trick on 970
- refuse to run on LPAR because of missing SLB pieces
V2 -> V3:
- fix DAR/DSISR saving
- allow running on LPAR by modifying the SLB shadow
- change the SLB implementation to use a mem-backed cache and do full world switch on enter/exit. gets rid of "context" magic
- be more aggressive about DEC injection
- remove fast ld/st because we're always in host context
- don't use SPRGs in real->paged transition
- implement dirty log
- remove MMIO speedup code
- SPRG cleanup
- rename SPRG3 -> SPRN_SPRG_PACA
- rename SPRG1 -> SPRN_SPRG_SCRATCH0
- don't use SPRG2
V3 -> V4:
- use context_id instead of mm_alloc
- export less
TODO:
- use MMU Notifiers
Alexander Graf (27):
Move dirty logging code to sub-arch
Pass PVR in sregs
Add Book3s definitions
Add Book3s fields to vcpu structs
Add asm/kvm_book3s.h
Add Book3s_64 intercept helpers
Add book3s_64 highmem asm code
Add SLB switching code for entry/exit
Add interrupt handling code
Add book3s.c
Add book3s_64 Host MMU handling
Add book3s_64 guest MMU
Add book3s_32 guest MMU
Add book3s_64 specific opcode emulation
Add mfdec emulation
Add desktop PowerPC specific emulation
Make head_64.S aware of KVM real mode code
Add Book3s_64 offsets to asm-offsets.c
Export symbols for KVM module
Split init_new_context and destroy_context
Export KVM symbols for module
Add fields to PACA
Export new PACA constants in asm-offsets
Include Book3s_64 target in buildsystem
Fix trace.h
Enable 32bit dirty log pointers on 64bit host
Use Little Endian for Dirty Bitmap
arch/powerpc/include/asm/exception-64s.h | 2 + arch/powerpc/include/asm/kvm.h | 2 + arch/powerpc/include/asm/kvm_asm.h | 39 ++ arch/powerpc/include/asm/kvm_book3s.h | 136 ++++ arch/powerpc/include/asm/kvm_book3s_64_asm.h | 58 ++ arch/powerpc/include/asm/kvm_host.h | 75 ++- arch/powerpc/include/asm/kvm_ppc.h | 1 + arch/powerpc/include/asm/mmu_context.h | 5 + arch/powerpc/include/asm/paca.h | 9 + arch/powerpc/kernel/asm-offsets.c | 18 + arch/powerpc/kernel/exceptions-64s.S | 8 + arch/powerpc/kernel/head_64.S | 7 + arch/powerpc/kernel/ppc_ksyms.c | 3 +- arch/powerpc/kernel/time.c | 1 + arch/powerpc/kvm/Kconfig | 17 + arch/powerpc/kvm/Makefile | 27 +- arch/powerpc/kvm/book3s.c | 919 ++++++++++++++++++++++++++ arch/powerpc/kvm/book3s_32_mmu.c | 354 ++++++++++ arch/powerpc/kvm/book3s_64_emulate.c | 338 ++++++++++ arch/powerpc/kvm/book3s_64_exports.c | 24 + arch/powerpc/kvm/book3s_64_interrupts.S | 392 +++++++++++ arch/powerpc/kvm/book3s_64_mmu.c | 469 +++++++++++++ arch/powerpc/kvm/book3s_64_mmu_host.c | 412 ++++++++++++ arch/powerpc/kvm/book3s_64_rmhandlers.S | 131 ++++ arch/powerpc/kvm/book3s_64_slb.S | 277 ++++++++ arch/powerpc/kvm/booke.c | 5 + arch/powerpc/kvm/emulate.c | 43 ++- arch/powerpc/kvm/powerpc.c | 5 - arch/powerpc/kvm/trace.h | 6 +- arch/powerpc/mm/hash_utils_64.c | 2 + arch/powerpc/mm/mmu_context_hash64.c | 24 +- virt/kvm/kvm_main.c | 10 +- 32 files changed, 3799 insertions(+), 20 deletions(-) create mode 100644 arch/powerpc/include/asm/kvm_book3s.h create mode 100644 arch/powerpc/include/asm/kvm_book3s_64_asm.h create mode 100644 arch/powerpc/kvm/book3s.c create mode 100644 arch/powerpc/kvm/book3s_32_mmu.c create mode 100644 arch/powerpc/kvm/book3s_64_emulate.c create mode 100644 arch/powerpc/kvm/book3s_64_exports.c create mode 100644 arch/powerpc/kvm/book3s_64_interrupts.S create mode 100644 arch/powerpc/kvm/book3s_64_mmu.c create mode 100644 arch/powerpc/kvm/book3s_64_mmu_host.c create mode 100644 arch/powerpc/kvm/book3s_64_rmhandlers.S create mode 100644 arch/powerpc/kvm/book3s_64_slb.S
Quoted 10 lines · Alexander Graf
KVM for PowerPC only supports embedded cores at the moment.
While it makes sense to virtualize on small machines, it's even more fun to do so on big boxes. So I figured we need KVM for PowerPC64 as well.
This patchset implements KVM support for
Book3s_64hosts and guest support forBook3s_64and G3/G4.To really make use of this, you also need a recent version of qemu.
Looks good to my non-ppc eyes. I'd like to see thus reviewed by the powerpc people, then it's good to go.
Quoted 4 lines
TODO:
- use MMU Notifiers
What's the plan here? While not a requirement for merging, that's one of the kvm points of strength and I'd like to see it supported across the board.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Quoted 21 lines · Avi Kivity
Quoted 7 lines · Alexander Graf
KVM for PowerPC only supports embedded cores at the moment.
While it makes sense to virtualize on small machines, it's even more fun
to do so on big boxes. So I figured we need KVM for PowerPC64 as well.Quoted 6 lines
This patchset implements KVM support for
Book3s_64hosts and guest support
forBook3s_64and G3/G4.To really make use of this, you also need a recent version of qemu.
Looks good to my non-ppc eyes. I'd like to see thus reviewed by the powerpc people, then it's good to go.
Quoted 2 lines
TODO:
Quoted 7 lines
Quoted 2 lines
- use MMU Notifiers
What's the plan here? While not a requirement for merging, that's one of the kvm points of strength and I'd like to see it supported across the board.
I'm having a deja vu :-).
The plan is to get qemu ppc64 guest support in a shape where it can actually use the KVM support. As it is it's rather useless. When we have that, a PV interface would be needed to get things fast and then the next thing on my list is the MMU notifiers.
Alex
Quoted 7 lines · Alexander Graf
Quoted 4 lines
What's the plan here? While not a requirement for merging, that's one of the kvm points of strength and I'd like to see it supported across the board.
I'm having a deja vu :-).
Will probably get one on every repost.
Quoted 5 lines
The plan is to get qemu ppc64 guest support in a shape where it can actually use the KVM support. As it is it's rather useless. When we have that, a PV interface would be needed to get things fast and then the next thing on my list is the MMU notifiers.
Um. How slow is it today? What paths are problematic? mmu, context switch?
Our experience with pv on x86 has been mostly negative. It's not trivial to get security right, it ended up slower than non-pv, and hardware obsoleted it fairly quickly.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Quoted 10 lines · Avi Kivity
Quoted 7 lines · Alexander Graf
Quoted 4 lines
What's the plan here? While not a requirement for merging, that's one of the kvm points of strength and I'd like to see it supported across the board.
I'm having a deja vu :-).
Will probably get one on every repost.
Yippie :)
Quoted 7 lines
Quoted 4 lines
The plan is to get qemu ppc64 guest support in a shape where it can actually use the KVM support. As it is it's rather useless. When we have that, a PV interface would be needed to get things fast and then the next thing on my list is the MMU notifiers.
Um. How slow is it today? What paths are problematic? mmu, context switch?
Instruction emulation.
X86 with virtualization extensions doesn't trap often, as most of the state can be safely handled within the guest mode.
Now with PPC we're basically running in "ring 3" (called "problem state" in ppc speech) which traps all the time because guests change the IF or access some SPRs that we don't really need to trap on, but only need to sync state with on #VMEXIT.
So the PV idea here is to have a shared page between host and guest that contains guest specific SPRs and other state (an MSR shadow for example). That way the guest can patch itself to use that shared page and KVM always knows about the most current state on #VMEXIT. At the same time we're reducing exits by a _lot_.
A short kvm_stat during boot of a ppc32 guest on ppc64 shows what I'm talking about:
dec 3224 168
exits 18957500 1037240
ext_intr 75 5
halt_wakeup 6874 0
inst_emu 8570503 818597
ld 0 0 ld_slow 0 0 mmio 8719444 26249 pf_instruc 302572 35379 pf_storage 9215970 86750 queue_intr 354020 31482 sig 7244 188 sp_instruc 302541 35365 sp_storage 370002 45370 st 0 0 st_slow 0 0
sysc 57907 5342
As you can see the bulk of exits are from MMIO and emulation.
We certainly won't be able to get rid of all the emulation exits, but quite a bunch of them aren't really that useful.
For MMIO we'll hopefully be able to use virtio.
Quoted 3 lines
Our experience with pv on x86 has been mostly negative. It's not trivial to get security right, it ended up slower than non-pv, and hardware obsoleted it fairly quickly.
Yes, and I really don't want to overdo it. PV for mfmsr/mtmsr and mfspr/mtspr is really necessary. X86 simply has that in hardware.
Alex
Quoted 17 lines · Alexander Graf
Quoted 7 lines
Quoted 4 lines
The plan is to get qemu ppc64 guest support in a shape where it can actually use the KVM support. As it is it's rather useless. When we have that, a PV interface would be needed to get things fast and then the next thing on my list is the MMU notifiers.
Um. How slow is it today? What paths are problematic? mmu, context switch?
Instruction emulation.
X86 with virtualization extensions doesn't trap often, as most of the state can be safely handled within the guest mode.
Now with PPC we're basically running in "ring 3" (called "problem state" in ppc speech) which traps all the time because guests change the IF or access some SPRs that we don't really need to trap on, but only need to sync state with on #VMEXIT.
Quoted 5 lines
So the PV idea here is to have a shared page between host and guest that contains guest specific SPRs and other state (an MSR shadow for example). That way the guest can patch itself to use that shared page and KVM always knows about the most current state on #VMEXIT. At the same time we're reducing exits by a _lot_.
But writing those registers often has side effects. For example, enabling interrupts should also inject an interrupt when one is pending. On x86 we have the same problem with the TPR on Windows XP, so we copy it to the guest on entry (along with the pending interrupt state) and back to the host on exit. The guest uses an atomic operation to change the TPR and read pending interrupt information, and if an interrupt becomes unmasked, it calls a hypercall to trigger it.
Presumably you'll be doing something similar?
In any case, I recommend keeping fine-grained control over those bits so they can be enabled/disabled/expanded as needed.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Quoted 33 lines · Avi Kivity
Quoted 17 lines · Alexander Graf
Quoted 7 lines
Quoted 4 lines
The plan is to get qemu ppc64 guest support in a shape where it can actually use the KVM support. As it is it's rather useless. When we have that, a PV interface would be needed to get things fast and then the next thing on my list is the MMU notifiers.
Um. How slow is it today? What paths are problematic? mmu, context switch?
Instruction emulation.
X86 with virtualization extensions doesn't trap often, as most of the state can be safely handled within the guest mode. Now with PPC we're basically running in "ring 3" (called "problem state" in ppc speech) which traps all the time because guests change the IF or access some SPRs that we don't really need to trap on, but only need to sync state with on #VMEXIT.
Quoted 5 lines
So the PV idea here is to have a shared page between host and guest that contains guest specific SPRs and other state (an MSR shadow for example). That way the guest can patch itself to use that shared page and KVM always knows about the most current state on #VMEXIT. At the same time we're reducing exits by a _lot_.
But writing those registers often has side effects. For example, enabling interrupts should also inject an interrupt when one is pending. On x86 we have the same problem with the TPR on Windows XP, so we copy it to the guest on entry (along with the pending interrupt state) and back to the host on exit. The guest uses an atomic operation to change the TPR and read pending interrupt information, and if an interrupt becomes unmasked, it calls a hypercall to trigger it.
Quoted 1 line
Presumably you'll be doing something similar?
Yes, very similar to the TPR hack. Just on a broader scale :-).
As far as MSR_IF = 1 goes, you're right. It does make sense to trap it in certain circumstances. Setting it to 0 doesn't need a trap though.
Also since we're PV'ed we could write an atomic bit in the shared page that says "hey guest - I have an interrupt waiting for you". When that is set MSR_IF = 1 returns to the host. Something like that.
Quoted 2 lines
In any case, I recommend keeping fine-grained control over those bits so they can be enabled/disabled/expanded as needed.
Yeah, sounds like a good idea. We'll see how this goes. As long as there are only very few users we can change the interface as much as we like since we don't break anyone :-). And the PPC user base is smaller than the x86 one in general.
Alex
Quoted 3 lines
Yes, and I really don't want to overdo it. PV for mfmsr/mtmsr and mfspr/mtspr is really necessary. X86 simply has that in hardware.
Note to Avi: This is also because we aren't actually using the virtualization feature of the processor, but instead running the guest basically in user space.
The reason for that is that today, you pretty much cannot access the "hypervisor" mode of the CPU on any ppc64 machine, it's either disabled by the service processor (Apple G5s, AFAIK PowerStation too) or you are already running under some kind of hypervisor (All IBM machines, PS3)...
Cheers,
Ben.
Quoted 8 lines · Avi Kivity
But writing those registers often has side effects. For example, enabling interrupts should also inject an interrupt when one is pending. On x86 we have the same problem with the TPR on Windows XP, so we copy it to the guest on entry (along with the pending interrupt state) and back to the host on exit. The guest uses an atomic operation to change the TPR and read pending interrupt information, and if an interrupt becomes unmasked, it calls a hypercall to trigger it.
Quoted 4 lines
Presumably you'll be doing something similar?
In any case, I recommend keeping fine-grained control over those bits so they can be enabled/disabled/expanded as needed.
Sure, for those who have side-effects, special care must be taken, either with PV tricks as Alex suggested in another reply, or by emulation.
But for example, pretty much every time the MSR is written, it's also previously -read-. If we keep a shadow of the guest MSR in the "magic page", then we can already half the number of emulation traps simply by having the guest read from there instead, and still trap on writes.
Those reads don't have side effects.
There's also a bunch of SPRs that don't have a direct side effect such as the SRRs and SPRGs which are heavily used for exception entry and exit. The former are storage for the PC and MSR values for a subsequent rfi instruction (return from interrupt) and the later are just general purpose storage for the kernel to save a few GPRs into in the exception handling code.
By replacing these by, for example, absolute load/stores in a magic page mapped differently per-CPU (one trick we have in mind) we can very significantly speed up the guest kernel exception entry and exit, and this is without dealing with side effects.
As Alex mention, we can from there try to go further for things like MSR changes, but you are right that this needs to be done more carefully.
Cheers,
Ben.
Quoted 20 lines · Benjamin Herrenschmidt
Quoted 8 lines · Avi Kivity
But writing those registers often has side effects. For example, enabling interrupts should also inject an interrupt when one is pending. On x86 we have the same problem with the TPR on Windows XP, so we copy it to the guest on entry (along with the pending interrupt state) and back to the host on exit. The guest uses an atomic operation to change the TPR and read pending interrupt information, and if an interrupt becomes unmasked, it calls a hypercall to trigger it.
Quoted 5 lines
Presumably you'll be doing something similar?
In any case, I recommend keeping fine-grained control over those bits so they can be enabled/disabled/expanded as needed.
Sure, for those who have side-effects, special care must be taken, either with PV tricks as Alex suggested in another reply, or by emulation.
Quoted 7 lines
But for example, pretty much every time the MSR is written, it's also previously -read-. If we keep a shadow of the guest MSR in the "magic page", then we can already half the number of emulation traps simply by having the guest read from there instead, and still trap on writes.
Those reads don't have side effects.
So these MSRs can be modified by the hypervisor? Otherwise you'd cache them in the guest with no hypervisor involvement, right? (just making sure :)
Quoted 7 lines
There's also a bunch of SPRs that don't have a direct side effect such as the SRRs and SPRGs which are heavily used for exception entry and exit. The former are storage for the PC and MSR values for a subsequent rfi instruction (return from interrupt) and the later are just general purpose storage for the kernel to save a few GPRs into in the exception handling code.
Quoted 9 lines
By replacing these by, for example, absolute load/stores in a magic page mapped differently per-CPU (one trick we have in mind) we can very significantly speed up the guest kernel exception entry and exit, and this is without dealing with side effects.
As Alex mention, we can from there try to go further for things like MSR changes, but you are right that this needs to be done more carefully.
Thanks for the explanations.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Quoted 4 lines · Avi Kivity
So these MSRs can be modified by the hypervisor? Otherwise you'd cache them in the guest with no hypervisor involvement, right? (just making sure :)
There's one MSR :-) Among others, it can be altered by the act of taking an interrupt (for example, it contains the PR bit, which means user vs. supervisor, things like that).
Cheers,
Ben.
Quoted 9 lines · Benjamin Herrenschmidt
Quoted 4 lines · Avi Kivity
So these MSRs can be modified by the hypervisor? Otherwise you'd cache them in the guest with no hypervisor involvement, right? (just making sure :)
There's one MSR :-) Among others, it can be altered by the act of taking an interrupt (for example, it contains the PR bit, which means user vs. supervisor, things like that).
For a bit more context...
On PowerPC, all those "special" registers are called "SPR"s (special registers, surprise ! :-)
They are generally accessed via mfspr/mtspr instructions that encode the SPR number, though some of them can also have decicated instructions or be set as a side effect of some instructions or events etc...
MSR is a bit special here because it's not per-se an SPR. It's the Machine State Register, in the core, it's in the fast path of a whole bunch of pipeline stages, and it contains the state of things such as the current privilege level, the state of MMU translation for I and D, the interrupt enable bit, etc... It's accessed via specific mfmsr/mtmsr instructions (to simplify as there are other instructions that modify the MSR as a side effect, interrupts do that too, etc...).
So the MSR warrants special treatment for KVM. Other SPRs may or may not depending on what they are. Some are just storage like the SPRGs, some contain a copy of the previous PC and MSR when taking an interrupt (SRR0 and SRR1) and are used by the rfi instruction to restore them when returning from an interrupt, and some are totally unrelated (such as the decrementer which is our core timer facility) or other processor specific registers containing various things like cache configuration etc...
The main issue with kernel entry / exit performances, though, revolve around MSR, SPRG and SRR0/1 accesses. SPRGs could -almost- be entirely guest cached, but since the goal is to save a register to use as scrach at a time when no register can be clobbered, saving a register to them must fit in one instruction that has no side effect. The typical option we are thinking about here is a store-absolute to an address that KVM can then map to some per-CPU storage page.
Things like SRR0/SRR1 can be replaced by similar load/stores as long as the HV sets them appropriately with the original MSR (or emulated MSR) and PC when directing an interrupt to the guest, and know where to retrieve the content set by the kernel when emulating an "rfi" instruction. The MSR can be read from cache always by the guest as long as the HV knows how to alter its cached value when directing an interrupt to the guest or emulating another of those instructions that can affect it (such as rfi of course), etc...
So in our case, that (relatively small) level of paravirt provides a tremendous performance boost, since every guest interrupt (syscall, etc...) goes down from something like a good dozen emulation traps to maybe a couple just for the base entry/exit path from the kernel.
This is very different from the issues around PV that you guys had in x86 world related to MMU emulation, though in our case, PV may also prove useful, as our MMU structure is very different, this is a completely orthogonal matter.
Cheers,
Ben.