Add interrupt handling code
Subsystems (2)
PATCH 8/23 Add interrupt handling code
Getting from host state to the guest is only half the story. We also need to return to our host context and handle whatever happened to get us out of the guest.
On PowerPC every guest exit is an interrupt. So all we need to do is trap the host's interrupt handlers and get into our #VMEXIT code to handle it.
PowerPCs also have a register that can add an offset to the interrupt handlers' adresses which is what the booke KVM code uses. Unfortunately that is a hypervisor ressource and we also want to be able to run KVM when we're running in an LPAR. So we have to hook into the Linux interrupt handlers.
--- arch/powerpc/kvm/970_rmhandlers.S | 128 +++++++++++++++++++++++++++++++++++++ 1 files changed, 128 insertions(+), 0 deletions(-) create mode 100644 arch/powerpc/kvm/970_rmhandlers.S
arch/powerpc/kvm/970_rmhandlers.S new file +128 -0
@@ -0,0 +1,128 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Copyright SUSE Linux Products GmbH 2009 + * + * Authors: Alexander Graf <[email protected]> + */ + +#include <asm/kvm_970_asm.h> + +/***************************************************************************** + * * + * Real Mode handlers that need to be in low physical memory * + * * + ****************************************************************************/ + + +.macro INTERRUPT_TRAMPOLINE intno + +.global kvmppc_trampoline_\intno +kvmppc_trampoline_\intno: + /* We're replacing this instruction in the real handler */ + mtspr SPRN_SPRG1, r13 /* Save r13 */ + /* + * First thing to do is to find out if we're coming + * from a KVM guest or a Linux process. + * + * To distinguish, we only need to check SPRG3 (PACA), + * because our entry code sets SPRG3 = PACA | 1, while + * Linux has the PACA on at least word boundary. + */ + mfcr r13 + mtspr SPRN_SPRG2, r13 /* Save CR */ + mfspr r13, SPRN_SPRG3 /* r13 = PACA | 1 */ + clrldi. r13, r13, 63 /* CR = ((r13 & 1) = 0) */ + bne ..kvmppc_handler_hasmagic_\intno + /* No KVM guest? Then jump back to the Linux handler! */ + mfspr r13, SPRN_SPRG2 + mtcr r13 + mfspr r13, SPRN_SPRG1 /* r13 = original r13 */ + b kvmppc_resume_\intno /* Get back original handler */ + + /* Now we know we're handling a KVM guest */ +..kvmppc_handler_hasmagic_\intno: + /* Unset SPRG3 */ + mfspr r13, SPRN_SPRG3 /* r13 = PACA */ + clrrdi r13, r13, 1 /* r13 &= ~1 */ + mtspr SPRN_SPRG3, r13 /* SPRG3 = PACA & ~1 */ + + /* As of here PACA is in r13 and we're safe to do something */ + + std r1, (PACA_EXMC+EX_R9)(r13) + std r10, (PACA_EXMC+EX_R10)(r13) + std r11, (PACA_EXMC+EX_R11)(r13) + std r12, (PACA_EXMC+EX_R12)(r13) + std r2, (PACA_EXMC+EX_R13)(r13) + + mfsrr0 r10 + mfsrr1 r11 + + /* Restore R1/R2 so we can handle faults */ + ld r1, PACAR1(r13) + ld r2, (PACA_EXMC+EX_SRR0)(r13) + + /* Let's store which interrupt we're handling */ + li r12, \intno + + /* Jump into the SLB exit code that goes to the highmem handler */ + b kvmppc_handler_trampoline_exit + +.endm + +INTERRUPT_TRAMPOLINE PPC970_INTERRUPT_SYSTEM_RESET +INTERRUPT_TRAMPOLINE PPC970_INTERRUPT_MACHINE_CHECK +INTERRUPT_TRAMPOLINE PPC970_INTERRUPT_DATA_STORAGE +INTERRUPT_TRAMPOLINE PPC970_INTERRUPT_DATA_SEGMENT +INTERRUPT_TRAMPOLINE PPC970_INTERRUPT_INST_STORAGE +INTERRUPT_TRAMPOLINE PPC970_INTERRUPT_INST_SEGMENT +INTERRUPT_TRAMPOLINE PPC970_INTERRUPT_EXTERNAL +INTERRUPT_TRAMPOLINE PPC970_INTERRUPT_ALIGNMENT +INTERRUPT_TRAMPOLINE PPC970_INTERRUPT_PROGRAM +INTERRUPT_TRAMPOLINE PPC970_INTERRUPT_FP_UNAVAIL +INTERRUPT_TRAMPOLINE PPC970_INTERRUPT_DECREMENTER +INTERRUPT_TRAMPOLINE PPC970_INTERRUPT_SYSCALL +INTERRUPT_TRAMPOLINE PPC970_INTERRUPT_TRACE +INTERRUPT_TRAMPOLINE PPC970_INTERRUPT_PERFMON +INTERRUPT_TRAMPOLINE PPC970_INTERRUPT_ALTIVEC +INTERRUPT_TRAMPOLINE PPC970_INTERRUPT_VSX + +/* + * This trampoline brings us back to a real mode handler + * + * Input Registers: + * + * R6 = SRR0 + * R7 = SRR1 + * LR = real-mode IP + * + */ +.global kvmppc_handler_lowmem_trampoline +kvmppc_handler_lowmem_trampoline: + + mtsrr0 r6 + mtsrr1 r7 + blr +kvmppc_handler_lowmem_trampoline_end: + +.global kvmppc_trampoline_lowmem +kvmppc_trampoline_lowmem: + .long kvmppc_handler_lowmem_trampoline - _stext + +.global kvmppc_trampoline_enter +kvmppc_trampoline_enter: + .long kvmppc_handler_trampoline_enter - _stext + +#include "970_slb.S" +
Signature · 1 line
1.6.0.2
Quoted 11 lines · Alexander Graf
Getting from host state to the guest is only half the story. We also need to return to our host context and handle whatever happened to get us out of the guest.
On PowerPC every guest exit is an interrupt. So all we need to do is trap the host's interrupt handlers and get into our #VMEXIT code to handle it.
PowerPCs also have a register that can add an offset to the interrupt handlers' adresses which is what the booke KVM code uses. Unfortunately that is a hypervisor ressource and we also want to be able to run KVM when we're running in an LPAR. So we have to hook into the Linux interrupt handlers.
Right, besides it doesn't always work :-)
Quoted 61 lines
Signed-off-by Alexander Graf <[email protected]>--- arch/powerpc/kvm/970_rmhandlers.S | 128 +++++++++++++++++++++++++++++++++++++ 1 files changed, 128 insertions(+), 0 deletions(-) create mode 100644 arch/powerpc/kvm/970_rmhandlers.Sarch/powerpc/kvm/970_rmhandlers.S new file +49
-0@@ -0,0 +1,128 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Copyright SUSE Linux Products GmbH 2009 + * + * Authors: Alexander Graf <[email protected]> + */ + +#include <asm/kvm_970_asm.h> + +/***************************************************************************** + * * + * Real Mode handlers that need to be in low physical memory * + * * + ****************************************************************************/ + + +.macro INTERRUPT_TRAMPOLINE intno + +.global kvmppc_trampoline_\intno +kvmppc_trampoline_\intno: + /* We're replacing this instruction in the real handler */ + mtspr SPRN_SPRG1, r13 /* Save r13 */ + /* + * First thing to do is to find out if we're coming + * from a KVM guest or a Linux process. + * + * To distinguish, we only need to check SPRG3 (PACA), + * because our entry code sets SPRG3 = PACA | 1, while + * Linux has the PACA on at least word boundary. + */ + mfcr r13 + mtspr SPRN_SPRG2, r13 /* Save CR */ + mfspr r13, SPRN_SPRG3 /* r13 = PACA | 1 */ + clrldi. r13, r13, 63 /* CR = ((r13 & 1) = 0) */ + bne ..kvmppc_handler_hasmagic_\intno + /* No KVM guest? Then jump back to the Linux handler! */ + mfspr r13, SPRN_SPRG2
arch/powerpc/kvm/970_rmhandlers.S line 59
Quoted 10 lines
arch/powerpc/kvm/970_rmhandlers.S new file +10
-0+ mtcr r13 + mfspr r13, SPRN_SPRG1 /* r13 = original r13 */ + b kvmppc_resume_\intno /* Get back original handler */ + + /* Now we know we're handling a KVM guest */ +..kvmppc_handler_hasmagic_\intno: + /* Unset SPRG3 */ + mfspr r13, SPRN_SPRG3 /* r13 = PACA */ + clrrdi r13, r13, 1 /* r13 &= ~1 */ + mtspr SPRN_SPRG3, r13 /* SPRG3 = PACA & ~1 */
What about instead, we make the generic interrupt entry test the PACA low bit and branch off to a KVM supplied function table ? That would lower the overhead and avoid having to do any kind of patching no ? I'm happy to have that logic permanently in there, maybe controlled by a CONFIG option in case somebody wants to do kernel that never do KVM, if we can make it such that it doesn't bloat things.
In fact we could even have it there, NOP it out when KVM isn't loaded and put the instructions back when KVM is.
Cheers,
Ben.
Quoted 104 lines · Benjamin Herrenschmidt
Quoted 11 lines · Alexander Graf
Getting from host state to the guest is only half the story. We also need
to return to our host context and handle whatever happened to get us out of
the guest.On PowerPC every guest exit is an interrupt. So all we need to do is trap
the host's interrupt handlers and get into our #VMEXIT code to handle it.Quoted 7 lines
PowerPCs also have a register that can add an offset to the interrupt handlers'
adresses which is what the booke KVM code uses. Unfortunately that is a
hypervisor ressource and we also want to be able to run KVM when we're running
in an LPAR. So we have to hook into the Linux interrupt handlers.Right, besides it doesn't always work :-)
Quoted 71 lines
Signed-off-by Alexander Graf <[email protected]>---
arch/powerpc/kvm/970_rmhandlers.S| 128 +++++++++++++++++++++++++++
++++++++++1 files changed, 128 insertions(+), 0 deletions(-)create mode 100644
arch/powerpc/kvm/970_rmhandlers.Sarch/powerpc/kvm/ renamed from arch/powerpc/kvm/970_rmhandlers.S +0
-0
970_rmhandlers.S
new file mode 100644
index 0000000..91dacc4arch/powerpc/kvm/970_rmhandlers.S new file +2
-0@@ -0,0 +1,128 @@ +/* + * This program is free software; you can redistribute it and/ormodify
+ * it under the terms of the GNU General Public License, version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Copyright SUSE Linux Products GmbH 2009
+ *
+ * Authors: Alexander Graf <[email protected]>
+ */arch/powerpc/kvm/970_rmhandlers.S new file +4
-0+ +#include <asm/kvm_970_asm.h> + +/*****************************************************************************
+
* *
+ * Real Mode handlers that need to be in low physical
memory *
+
* *
+ ****************************************************************************/arch/powerpc/kvm/970_rmhandlers.S new file +23
-0+ + +.macro INTERRUPT_TRAMPOLINE intno + +.global kvmppc_trampoline_\intno +kvmppc_trampoline_\intno: + /* We're replacing this instruction in the real handler */ + mtspr SPRN_SPRG1, r13 /* Save r13 */ + /* + * First thing to do is to find out if we're coming + * from a KVM guest or a Linux process. + * + * To distinguish, we only need to check SPRG3 (PACA), + * because our entry code sets SPRG3 = PACA | 1, while + * Linux has the PACA on at least word boundary. + */ + mfcr r13 + mtspr SPRN_SPRG2, r13 /* Save CR */ + mfspr r13, SPRN_SPRG3 /* r13 = PACA | 1 */ + clrldi. r13, r13, 63 /* CR = ((r13 & 1) = 0) */ + bne ..kvmppc_handler_hasmagic_\intno + /* No KVM guest? Then jump back to the Linux handler! */ + mfspr r13, SPRN_SPRG2arch/powerpc/kvm/970_rmhandlers.S line 59
Quoted 10 lines
arch/powerpc/kvm/970_rmhandlers.S new file +10
-0+ mtcr r13 + mfspr r13, SPRN_SPRG1 /* r13 = original r13 */ + b kvmppc_resume_\intno /* Get back original handler */ + + /* Now we know we're handling a KVM guest */ +..kvmppc_handler_hasmagic_\intno: + /* Unset SPRG3 */ + mfspr r13, SPRN_SPRG3 /* r13 = PACA */ + clrrdi r13, r13, 1 /* r13 &= ~1 */ + mtspr SPRN_SPRG3, r13 /* SPRG3 = PACA & ~1 */
Quoted 8 lines
What about instead, we make the generic interrupt entry test the PACA low bit and branch off to a KVM supplied function table ? That would lower the overhead and avoid having to do any kind of patching no ? I'm
happy to have that logic permanently in there, maybe controlled by a CONFIG option in case somebody wants to do kernel that never do KVM, if
we can make it such that it doesn't bloat things.
Hum, something like a generic interrupt hooking mechanism? Sounds cool :-). But not all interrupt handlers use generic code, so we'd have to duplicate some bits quite some times.
Alex
Quoted 8 lines
In fact we could even have it there, NOP it out when KVM isn't loaded and put the instructions back when KVM is.
Cheers,
Ben.
Quoted 3 lines · Alexander Graf
Hum, something like a generic interrupt hooking mechanism? Sounds cool :-). But not all interrupt handlers use generic code, so we'd have to duplicate some bits quite some times.
Right, some of them are subtly different. ..
One cool thing however is that they all have about 0x100 bytes of space and only use a portion of that so we should be able to do something about it... maybe having 2 variants of the prolog, with and without the test to jump to KVM for example and "replace" them when KVM is loaded.
I need to think about it a bit more. One thing we may want to do is instead to set SPRG3 bit 0, instead, is to set a bit in the PACA, that would make things a lot easier. We could run through the base prolog and just add three instructions (that can be noped out easily) to load that flag, test it and branch out of line to a special if set.
In the standard prolog, we can clobber r10, r11 and r12 right after EXCEPTION_PROLOG_1 and we should be able to clobber CR0 as well since we just saved CR into r9, so we could probably do something like lbz the "KVM" flag into r11 before loading PACAKBASE into r12, then compare it to 0 and branch conditional after loading PACAKMSR into r10 or something like that (to keep a gap between load and use to avoid back to back here).
In fact, more fun: You could hack PACAKBASE and PACAMSR :-) But that's a bit harder to get the offsets right. Would probably work to. Make it point to a page where the handlers are at the right offset (we could make them all be at 0x0f0 or so from the base of the exception easily instead of right after so you don't have to do black magic to find where to put them or you could just fill the whole 0x100 bytes with copies).
That way, depending on whether you are in KVM or not on that CPU, the existing code would branch to your secondary handlers with the MSR of your choice (typically still in real mode) without adding a conditional branch to the exception entry code.
We can carve out a page down there in the RMA for use by KVM easily, in fact we probably have some unused space already.
Cheers,
Ben.
PATCH 8/23 Add interrupt handling code
In reply to Alexander GrafGetting from host state to the guest is only half the story. We also need to return to our host context and handle whatever happened to get us out of the guest.
On PowerPC every guest exit is an interrupt. So all we need to do is trap the host's interrupt handlers and get into our #VMEXIT code to handle it.
PowerPCs also have a register that can add an offset to the interrupt handlers' adresses which is what the booke KVM code uses. Unfortunately that is a hypervisor ressource and we also want to be able to run KVM when we're running in an LPAR. So we have to hook into the Linux interrupt handlers.
--- arch/powerpc/kvm/book3s_64_rmhandlers.S | 133 +++++++++++++++++++++++++++++++ 1 files changed, 133 insertions(+), 0 deletions(-) create mode 100644 arch/powerpc/kvm/book3s_64_rmhandlers.S
arch/powerpc/kvm/book3s_64_rmhandlers.S new file +133 -0
@@ -0,0 +1,133 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Copyright SUSE Linux Products GmbH 2009 + * + * Authors: Alexander Graf <[email protected]> + */ + +#include <asm/ppc_asm.h> +#include <asm/kvm_asm.h> +#include <asm/reg.h> +#include <asm/page.h> +#include <asm/asm-offsets.h> +#include <asm/exception.h> + +/***************************************************************************** + * * + * Real Mode handlers that need to be in low physical memory * + * * + ****************************************************************************/ + + +.macro INTERRUPT_TRAMPOLINE intno + +.global kvmppc_trampoline_\intno +kvmppc_trampoline_\intno: + /* We're replacing this instruction in the real handler */ + mtspr SPRN_SPRG1, r13 /* Save r13 */ + /* + * First thing to do is to find out if we're coming + * from a KVM guest or a Linux process. + * + * To distinguish, we only need to check SPRG3 (PACA), + * because our entry code sets SPRG3 = PACA | 1, while + * Linux has the PACA on at least word boundary. + */ + mfcr r13 + mtspr SPRN_SPRG2, r13 /* Save CR */ + mfspr r13, SPRN_SPRG3 /* r13 = PACA | 1 */ + clrldi. r13, r13, 63 /* CR = ((r13 & 1) = 0) */ + bne ..kvmppc_handler_hasmagic_\intno + /* No KVM guest? Then jump back to the Linux handler! */ + mfspr r13, SPRN_SPRG2 + mtcr r13 + mfspr r13, SPRN_SPRG1 /* r13 = original r13 */ + b kvmppc_resume_\intno /* Get back original handler */ + + /* Now we know we're handling a KVM guest */ +..kvmppc_handler_hasmagic_\intno: + /* Unset SPRG3 */ + mfspr r13, SPRN_SPRG3 /* r13 = PACA */ + clrrdi r13, r13, 1 /* r13 &= ~1 */ + mtspr SPRN_SPRG3, r13 /* SPRG3 = PACA & ~1 */ + + /* As of here PACA is in r13 and we're safe to do something */ + + std r1, (PACA_EXMC+EX_R9)(r13) + std r10, (PACA_EXMC+EX_R10)(r13) + std r11, (PACA_EXMC+EX_R11)(r13) + std r12, (PACA_EXMC+EX_R12)(r13) + std r2, (PACA_EXMC+EX_R13)(r13) + + mfsrr0 r10 + mfsrr1 r11 + + /* Restore R1/R2 so we can handle faults */ + ld r1, PACAR1(r13) + ld r2, (PACA_EXMC+EX_SRR0)(r13) + + /* Let's store which interrupt we're handling */ + li r12, \intno + + /* Jump into the SLB exit code that goes to the highmem handler */ + b kvmppc_handler_trampoline_exit + +.endm + +INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_SYSTEM_RESET +INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_MACHINE_CHECK +INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_DATA_STORAGE +INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_DATA_SEGMENT +INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_INST_STORAGE +INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_INST_SEGMENT +INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_EXTERNAL +INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_ALIGNMENT +INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_PROGRAM +INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_FP_UNAVAIL +INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_DECREMENTER +INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_SYSCALL +INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_TRACE +INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_PERFMON +INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_ALTIVEC +INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_VSX + +/* + * This trampoline brings us back to a real mode handler + * + * Input Registers: + * + * R6 = SRR0 + * R7 = SRR1 + * LR = real-mode IP + * + */ +.global kvmppc_handler_lowmem_trampoline +kvmppc_handler_lowmem_trampoline: + + mtsrr0 r6 + mtsrr1 r7 + blr +kvmppc_handler_lowmem_trampoline_end: + +.global kvmppc_trampoline_lowmem +kvmppc_trampoline_lowmem: + .long kvmppc_handler_lowmem_trampoline - _stext + +.global kvmppc_trampoline_enter +kvmppc_trampoline_enter: + .long kvmppc_handler_trampoline_enter - _stext + +#include "book3s_64_slb.S" +
Signature · 1 line
1.6.0.2