Skip to contentSkip to conversation

Early exception handlers on Power

PATCH Superseded Shawn Anastasio 3 participants 11 messages Sep 28, 2023 – Oct 13, 2023

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Early exception handlers on Power

Hello all,

This series implements a basic exception handler and the required support infrastructure on Power. Currently the handler just dumps all registers to the serial console and nothing else, but even this is useful for debugging during early bring-up.

An additional patch to fix the stack setup code in head.S is also included.

Thanks,

Shawn Anastasio (3):
xen/ppc: Fix stack initialization in head.S xen/ppc: Add .text.exceptions section for exception vectors xen/ppc: Implement a basic exception handler

 xen/arch/ppc/include/asm/config.h    |   3 +
 xen/arch/ppc/include/asm/processor.h |  31 +++++++
 xen/arch/ppc/ppc64/Makefile          |   2 +
 xen/arch/ppc/ppc64/asm-offsets.c     |   1 +
 xen/arch/ppc/ppc64/exceptions-asm.S  | 122 +++++++++++++++++++++++++++
 xen/arch/ppc/ppc64/exceptions.c      | 102 ++++++++++++++++++++++
 xen/arch/ppc/ppc64/head.S            |   2 +-
 xen/arch/ppc/setup.c                 |  11 +++
 xen/arch/ppc/xen.lds.S               |   7 ++
 9 files changed, 280 insertions(+), 1 deletion(-)
 create mode 100644 xen/arch/ppc/ppc64/exceptions-asm.S
 create mode 100644 xen/arch/ppc/ppc64/exceptions.c

--
2.30.2

#1 lore raw

PATCH 1/3 xen/ppc: Fix stack initialization in head.S

The code to set up the stack in head.S erroneously loads the bottom of the stack (the symbol cpu0_boot_stack) into r1 instead of the top of the stack (cpu0_boot_stack + STACK_SIZE).

Fixes 3a4e6f67bc68 ("xen/ppc: Set up a basic C environment")
---
 xen/arch/ppc/ppc64/head.S | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/ppc/ppc64/head.S +1 -1
index e600ddb05f..6ce313aff8 100644
@@ -46,7 +46,7 @@ ENTRY(start)
 .L_correct_address:
 
     /* set up the initial stack */
-    LOAD_REG_ADDR(%r1, cpu0_boot_stack)
+    LOAD_REG_ADDR(%r1, cpu0_boot_stack + STACK_SIZE)
     li      %r11, 0
     stdu    %r11, -STACK_FRAME_OVERHEAD(%r1)
 
Signature · 1 line
2.30.2
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] xen/ppc: Fix stack initialization in head.S

Quoted 6 lines · Shawn Anastasio

The code to set up the stack in head.S erroneously loads the bottom of the stack (the symbol cpu0_boot_stack) into r1 instead of the top of the stack (cpu0_boot_stack + STACK_SIZE).

Fixes 3a4e6f67bc68 ("xen/ppc: Set up a basic C environment")

We're in code freeze for 4.18 now.  Bugfixes are still fine, and this one does want including IMO.  CC Henry.

#3 lore raw

Re: [PATCH 1/3] xen/ppc: Fix stack initialization in head.S

Hi Andrew, Shawn,

Quoted 7 lines · Andrew Cooper
Quoted 4 lines · Shawn Anastasio

The code to set up the stack in head.S erroneously loads the bottom of the stack (the symbol cpu0_boot_stack) into r1 instead of the top of the stack (cpu0_boot_stack + STACK_SIZE).

Quoted 7 lines
Quoted 2 lines
Fixes 3a4e6f67bc68 ("xen/ppc: Set up a basic C environment")

We're in code freeze for 4.18 now. Bugfixes are still fine, and this one does want including IMO.

Indeed, so:

Quoted 1 line

CC Henry.

Thanks!

Kind regards,
Henry

#4 lore raw

PATCH 2/3 xen/ppc: Add .text.exceptions section for exception vectors

In reply to Shawn Anastasio

On Power, the exception vectors must lie at a fixed address, depending on the state of the Alternate Interrupt Location (AIL) field of the Logical Partition Control Register (LPCR). Create a .text.exceptions section in the linker script at an address suitable for AIL=3 plus an accompanying assertion to pave the way for implementing exception support.

---
 xen/arch/ppc/include/asm/config.h | 3 +++
 xen/arch/ppc/xen.lds.S            | 7 +++++++
 2 files changed, 10 insertions(+)
xen/arch/ppc/include/asm/config.h +3 -0
index a11a09c570..e012b75beb 100644
@@ -42,6 +42,9 @@
 
 #define XEN_VIRT_START _AC(0xc000000000000000, UL)
 
+/* Fixed address for start of the section containing exception vectors */
+#define EXCEPTION_VECTORS_START _AC(0xc000000000000100, UL)
+
 #define VMAP_VIRT_START (XEN_VIRT_START + GB(1))
 #define VMAP_VIRT_SIZE  GB(1)
 
xen/arch/ppc/xen.lds.S +7 -0
index 9e46035155..9e888d7383 100644
@@ -24,6 +24,10 @@ SECTIONS
         _stext = .;            /* Text section */
         *(.text.header)
 
+        . = ALIGN(256);
+        _stext_exceptions = .;
+        *(.text.exceptions)
+
         *(.text.cold)
         *(.text.unlikely .text.*_unlikely .text.unlikely.*)
 
@@ -184,3 +188,6 @@ ASSERT(IS_ALIGNED(__bss_end,        POINTER_ALIGN), "__bss_end is misaligned")
 
 ASSERT(!SIZEOF(.got),      ".got non-empty")
 ASSERT(!SIZEOF(.got.plt),  ".got.plt non-empty")
+
+ASSERT(_stext_exceptions == EXCEPTION_VECTORS_START, \
+       ".text.exceptions not at expected location -- .text.header too big?");
Signature · 1 line
2.30.2
#5 lore raw

Re: [PATCH 2/3] xen/ppc: Add .text.exceptions section for exception vectors

Quoted 6 lines · Shawn Anastasio

On Power, the exception vectors must lie at a fixed address, depending on the state of the Alternate Interrupt Location (AIL) field of the Logical Partition Control Register (LPCR). Create a .text.exceptions section in the linker script at an address suitable for AIL=3 plus an accompanying assertion to pave the way for implementing exception support.

Thanks - this is a perfect level of detail as far as I'm concerned as a PPC novice.

Quoted 12 lines
---
 xen/arch/ppc/include/asm/config.h | 3 +++
 xen/arch/ppc/xen.lds.S            | 7 +++++++
 2 files changed, 10 insertions(+)
xen/arch/ppc/include/asm/config.h +0 -0
index a11a09c570..e012b75beb 100644

xen/arch/ppc/include/asm/config.h line 46

Quoted 5 lines
xen/arch/ppc/include/asm/config.h +2 -0
@@ -42,6 +42,9 @@
 
 #define XEN_VIRT_START _AC(0xc000000000000000, UL)
 
+/* Fixed address for start of the section containing exception vectors */
+#define EXCEPTION_VECTORS_START _AC(0xc000000000000100, UL)

The patch looks fine, but a PPC question.  Does AIL=3 really mean a hard coded address at 0xc000000000000100 ?

Or is it +0x100 from something else that happens to be programmed to XEN_VIRT_START ?

Quoted 9 lines
xen/arch/ppc/include/asm/config.h +1 -0
+
 #define VMAP_VIRT_START (XEN_VIRT_START + GB(1))
 #define VMAP_VIRT_SIZE  GB(1)
 
xen/arch/ppc/xen.lds.S +0 -0
index 9e46035155..9e888d7383 100644

xen/arch/ppc/xen.lds.S SECTIONS · line 28

Quoted 5 lines
xen/arch/ppc/xen.lds.S +2 -0
@@ -24,6 +24,10 @@ SECTIONS
         _stext = .;            /* Text section */
         *(.text.header)
 
+        . = ALIGN(256);
+        _stext_exceptions = .;

If this is really only used for the linker assertion, then it wants to be

    HIDDEN(_stext_exceptions = .);

otherwise the debugging symbols will have _stext_exceptions typically hiding exc_sysreset in the disassembly and symbol table.

xen/arch/ppc/xen.lds.S ASSERT(IS_ALIGNED(__bss_end, POINTER_ALIGN), "__bss_end is misaligned") · line 193

Quoted 12 lines
xen/arch/ppc/xen.lds.S +5 -0
+        *(.text.exceptions)
+
         *(.text.cold)
         *(.text.unlikely .text.*_unlikely .text.unlikely.*)
 
@@ -184,3 +188,6 @@ ASSERT(IS_ALIGNED(__bss_end,        POINTER_ALIGN), "__bss_end is misaligned")
 
 ASSERT(!SIZEOF(.got),      ".got non-empty")
 ASSERT(!SIZEOF(.got.plt),  ".got.plt non-empty")
+
+ASSERT(_stext_exceptions == EXCEPTION_VECTORS_START, \
+       ".text.exceptions not at expected location -- .text.header too big?");

No need for ; at the end, and no need for \ either.

As I said for patch 1, we're now at 4.18-rc1.   Does this need to be included now, or wait for 4.19?  There's something to be said for having a basic exception handler, but it is technically a new feature...

~Andrew

#6 lore raw

Re: [PATCH 2/3] xen/ppc: Add .text.exceptions section for exception vectors

Quoted 26 lines · Andrew Cooper
Quoted 6 lines · Shawn Anastasio

On Power, the exception vectors must lie at a fixed address, depending on the state of the Alternate Interrupt Location (AIL) field of the Logical Partition Control Register (LPCR). Create a .text.exceptions section in the linker script at an address suitable for AIL=3 plus an accompanying assertion to pave the way for implementing exception support.

Thanks - this is a perfect level of detail as far as I'm concerned as a PPC novice.

Quoted 15 lines
---
 xen/arch/ppc/include/asm/config.h | 3 +++
 xen/arch/ppc/xen.lds.S            | 7 +++++++
 2 files changed, 10 insertions(+)
xen/arch/ppc/include/asm/config.h +0 -0
index a11a09c570..e012b75beb 100644
@@ -42,6 +42,9 @@
 
 #define XEN_VIRT_START _AC(0xc000000000000000, UL)
 
Quoted 9 lines

xen/arch/ppc/include/asm/config.h line 46

Quoted 2 lines
xen/arch/ppc/include/asm/config.h +2 -0
+/* Fixed address for start of the section containing exception vectors */
+#define EXCEPTION_VECTORS_START _AC(0xc000000000000100, UL)

The patch looks fine, but a PPC question. Does AIL=3 really mean a hard coded address at 0xc000000000000100 ?

Or is it +0x100 from something else that happens to be programmed to XEN_VIRT_START ?

AIL=3 means a hardcoded address at 0xc000000000004000, actually, but by placing the section earlier we can take advantage of some of the space in between ...0100 and ...4000 for things like the {h_,}exception_common routines introduced in the next patch instead of just having the linker pad it with nops.

(Now that I look closely though, I see that I erroneously placed those routines in .text rather than .text.exceptions in the next patch -- that's something I'll fix for v2).

Quoted 17 lines
Quoted 9 lines
xen/arch/ppc/include/asm/config.h +1 -0
+
 #define VMAP_VIRT_START (XEN_VIRT_START + GB(1))
 #define VMAP_VIRT_SIZE  GB(1)
 
xen/arch/ppc/xen.lds.S +0 -0
index 9e46035155..9e888d7383 100644

xen/arch/ppc/xen.lds.S SECTIONS · line 28

Quoted 5 lines
xen/arch/ppc/xen.lds.S +2 -0
@@ -24,6 +24,10 @@ SECTIONS
         _stext = .;            /* Text section */
         *(.text.header)
 
+        . = ALIGN(256);
+        _stext_exceptions = .;

If this is really only used for the linker assertion, then it wants to be

Quoted 5 lines
    HIDDEN(_stext_exceptions = .);

otherwise the debugging symbols will have _stext_exceptions typically hiding exc_sysreset in the disassembly and symbol table.

It is indeed only used for the assertion -- I'll make this change in v2.

Quoted 7 lines
Quoted 7 lines
xen/arch/ppc/xen.lds.S +2 -0
+        *(.text.exceptions)
+
         *(.text.cold)
         *(.text.unlikely .text.*_unlikely .text.unlikely.*)
 
@@ -184,3 +188,6 @@ ASSERT(IS_ALIGNED(__bss_end,        POINTER_ALIGN), "__bss_end is misaligned")
 
Quoted 8 lines

xen/arch/ppc/xen.lds.S ASSERT(IS_ALIGNED(__bss_end, POINTER_ALIGN), "__bss_end is misaligned") · line 193

Quoted 5 lines
xen/arch/ppc/xen.lds.S +3 -0
 ASSERT(!SIZEOF(.got),      ".got non-empty")
 ASSERT(!SIZEOF(.got.plt),  ".got.plt non-empty")
+
+ASSERT(_stext_exceptions == EXCEPTION_VECTORS_START, \
+       ".text.exceptions not at expected location -- .text.header too big?");

No need for ; at the end, and no need for \ either.

Will fix.

Quoted 4 lines

As I said for patch 1, we're now at 4.18-rc1. Does this need to be
included now, or wait for 4.19? There's something to be said for having a basic exception handler, but it is technically a new feature...

I don't think there's any pressing need to bring this into 4.18, unless the burden of doing so is trivial.

Quoted 1 line

~Andrew

Thanks,
Shawn

#7 lore raw

Re: [PATCH 2/3] xen/ppc: Add .text.exceptions section for exception vectors

Quoted 7 lines · Shawn Anastasio
Quoted 4 lines · Andrew Cooper

As I said for patch 1, we're now at 4.18-rc1. Does this need to be
included now, or wait for 4.19? There's something to be said for having a basic exception handler, but it is technically a new feature...

I don't think there's any pressing need to bring this into 4.18, unless the burden of doing so is trivial.

Ok, in which case lets not complicate 4.18 with this.

Shortly I'll start up my usual xen-next branch to hold okay'd content for 4.19, but the patches can sit on mailing list in the short term.

Thanks,

~Andrew

#8 lore raw

PATCH 3/3 xen/ppc: Implement a basic exception handler

In reply to Shawn Anastasio

Implement a basic exception handler that dumps the CPU state to the console, as well as the code required to set the correct exception vector table's base address in setup.c.

---
 xen/arch/ppc/include/asm/processor.h |  31 +++++++
 xen/arch/ppc/ppc64/Makefile          |   2 +
 xen/arch/ppc/ppc64/asm-offsets.c     |   1 +
 xen/arch/ppc/ppc64/exceptions-asm.S  | 122 +++++++++++++++++++++++++++
 xen/arch/ppc/ppc64/exceptions.c      | 102 ++++++++++++++++++++++
 xen/arch/ppc/setup.c                 |  11 +++
 6 files changed, 269 insertions(+)
 create mode 100644 xen/arch/ppc/ppc64/exceptions-asm.S
 create mode 100644 xen/arch/ppc/ppc64/exceptions.c
xen/arch/ppc/include/asm/processor.h +31 -0
index d3dd943c20..a01b62b8a4 100644
@@ -103,6 +103,37 @@
 #define PVR_BE        0x0070
 #define PVR_PA6T      0x0090
 
+/* Exception Definitions */
+#define EXC_SYSTEM_RESET    0x0100 /* System Reset Interrupt */
+#define EXC_MACHINE_CHECK   0x0200 /* Machine Check Interrupt */
+#define EXC_DATA_STORAGE    0x0300 /* Data Storage Interrupt */
+#define EXC_DATA_SEGMENT    0x0380 /* Data Segment Interrupt */
+#define EXC_INSN_STORAGE    0x0400 /* Instruction Storage Interrupt */
+#define EXC_INSN_SEGMENT    0x0480 /* Instruction Segment Interrupt */
+#define EXC_EXTERNAL        0x0500 /* External Interrupt */
+#define EXC_ALIGNMENT       0x0600 /* Alignment Interrupt */
+#define EXC_PROGRAM         0x0700 /* Program Interrupt */
+#define EXC_FPU_UNAVAIL     0x0800 /* Floating-Point Unavailable Interrupt */
+#define EXC_DECREMENTER     0x0900 /* Decrementer Interrupt */
+#define EXC_H_DECREMENTER   0x0980 /* Hypervisor Decrementer Interrupt */
+#define EXC_PRIV_DOORBELL   0x0A00 /* Directed Privileged Doorbell Interrupt */
+#define EXC_SYSTEM_CALL     0x0C00 /* System Call Interrupt */
+#define EXC_TRACE           0x0D00 /* Trace Interrupt */
+#define EXC_H_DATA_STORAGE  0x0E00 /* Hypervisor Data Storage Interrupt */
+#define EXC_H_INSN_STORAGE  0x0E20 /* Hypervisor Instruction Storage Interrupt */
+#define EXC_H_EMUL_ASST     0x0E40 /* Hypervisor Emulation Assistance Interrupt */
+#define EXC_H_MAINTENANCE   0x0E60 /* Hypervisor Maintenance Interrupt */
+#define EXC_H_DOORBELL      0x0E80 /* Directed Hypervisor Doorbell Interrupt */
+#define EXC_H_VIRT          0x0EA0 /* Hypervisor Virtualization Interrupt */
+#define EXC_PERF_MON        0x0F00 /* Performance Monitor Interrupt */
+#define EXC_VECTOR_UNAVAIL  0x0F20 /* Vector Unavailable Interrupt */
+#define EXC_VSX_UNAVAIL     0x0F40 /* VSX Unavailable Interrupt */
+#define EXC_FACIL_UNAVAIL   0x0F60 /* Facility Unavailable Interrupt */
+#define EXC_H_FACIL_UNAVAIL 0x0F80 /* Hypervisor Facility Unavailable Interrupt */
+
+/* Base address of interrupt vector table when LPCR[AIL]=3 */
+#define AIL_VECTOR_BASE _AC(0xc000000000004000, UL)
+
 #ifndef __ASSEMBLY__
 
 #include <xen/types.h>
xen/arch/ppc/ppc64/Makefile +2 -0
index 5b88355bb2..914bb21c40 100644
@@ -1,2 +1,4 @@
+obj-y += exceptions.o
+obj-y += exceptions-asm.o
 obj-y += head.o
 obj-y += opal-calls.o
xen/arch/ppc/ppc64/asm-offsets.c +1 -0
index c15c1bf136..634d7260e3 100644
@@ -46,6 +46,7 @@ void __dummy__(void)
     OFFSET(UREGS_dsisr, struct cpu_user_regs, dsisr);
     OFFSET(UREGS_cr, struct cpu_user_regs, cr);
     OFFSET(UREGS_fpscr, struct cpu_user_regs, fpscr);
+    OFFSET(UREGS_entry_vector, struct cpu_user_regs, entry_vector);
     DEFINE(UREGS_sizeof, sizeof(struct cpu_user_regs));
 
     OFFSET(OPAL_base, struct opal, base);
xen/arch/ppc/ppc64/exceptions-asm.S new file +122 -0

122 lines not on this page · raw message

xen/arch/ppc/ppc64/exceptions.c new file +102 -0

102 lines not on this page · raw message

xen/arch/ppc/setup.c +11 -0
index 959c1454a0..101bdd8bb6 100644
@@ -11,6 +11,15 @@
 /* Xen stack for bringing up the first CPU. */
 unsigned char __initdata cpu0_boot_stack[STACK_SIZE] __aligned(STACK_SIZE);
 
+void setup_exceptions(void)
+{
+    unsigned long lpcr;
+
+    /* Set appropriate interrupt location in LPCR */
+    lpcr = mfspr(SPRN_LPCR);
+    mtspr(SPRN_LPCR, lpcr | LPCR_AIL_3);
+}
+
 void __init noreturn start_xen(unsigned long r3, unsigned long r4,
                                unsigned long r5, unsigned long r6,
                                unsigned long r7)
@@ -26,6 +35,8 @@ void __init noreturn start_xen(unsigned long r3, unsigned long r4,
         boot_opal_init((void *)r3);
     }
 
+    setup_exceptions();
+
     setup_initial_pagetables();
 
     early_printk("Hello, ppc64le!\n");
Signature · 1 line
2.30.2
#9 lore raw

Re: [PATCH 3/3] xen/ppc: Implement a basic exception handler

Quoted 160 lines · Shawn Anastasio

Implement a basic exception handler that dumps the CPU state to the console, as well as the code required to set the correct exception vector table's base address in setup.c.

---
 xen/arch/ppc/include/asm/processor.h |  31 +++++++
 xen/arch/ppc/ppc64/Makefile          |   2 +
 xen/arch/ppc/ppc64/asm-offsets.c     |   1 +
 xen/arch/ppc/ppc64/exceptions-asm.S  | 122 +++++++++++++++++++++++++++
 xen/arch/ppc/ppc64/exceptions.c      | 102 ++++++++++++++++++++++
 xen/arch/ppc/setup.c                 |  11 +++
 6 files changed, 269 insertions(+)
 create mode 100644 xen/arch/ppc/ppc64/exceptions-asm.S
 create mode 100644 xen/arch/ppc/ppc64/exceptions.c
xen/arch/ppc/include/asm/processor.h +31 -0
index d3dd943c20..a01b62b8a4 100644
@@ -103,6 +103,37 @@
 #define PVR_BE        0x0070
 #define PVR_PA6T      0x0090
 
+/* Exception Definitions */
+#define EXC_SYSTEM_RESET    0x0100 /* System Reset Interrupt */
+#define EXC_MACHINE_CHECK   0x0200 /* Machine Check Interrupt */
+#define EXC_DATA_STORAGE    0x0300 /* Data Storage Interrupt */
+#define EXC_DATA_SEGMENT    0x0380 /* Data Segment Interrupt */
+#define EXC_INSN_STORAGE    0x0400 /* Instruction Storage Interrupt */
+#define EXC_INSN_SEGMENT    0x0480 /* Instruction Segment Interrupt */
+#define EXC_EXTERNAL        0x0500 /* External Interrupt */
+#define EXC_ALIGNMENT       0x0600 /* Alignment Interrupt */
+#define EXC_PROGRAM         0x0700 /* Program Interrupt */
+#define EXC_FPU_UNAVAIL     0x0800 /* Floating-Point Unavailable Interrupt */
+#define EXC_DECREMENTER     0x0900 /* Decrementer Interrupt */
+#define EXC_H_DECREMENTER   0x0980 /* Hypervisor Decrementer Interrupt */
+#define EXC_PRIV_DOORBELL   0x0A00 /* Directed Privileged Doorbell Interrupt */
+#define EXC_SYSTEM_CALL     0x0C00 /* System Call Interrupt */
+#define EXC_TRACE           0x0D00 /* Trace Interrupt */
+#define EXC_H_DATA_STORAGE  0x0E00 /* Hypervisor Data Storage Interrupt */
+#define EXC_H_INSN_STORAGE  0x0E20 /* Hypervisor Instruction Storage Interrupt */
+#define EXC_H_EMUL_ASST     0x0E40 /* Hypervisor Emulation Assistance Interrupt */
+#define EXC_H_MAINTENANCE   0x0E60 /* Hypervisor Maintenance Interrupt */
+#define EXC_H_DOORBELL      0x0E80 /* Directed Hypervisor Doorbell Interrupt */
+#define EXC_H_VIRT          0x0EA0 /* Hypervisor Virtualization Interrupt */
+#define EXC_PERF_MON        0x0F00 /* Performance Monitor Interrupt */
+#define EXC_VECTOR_UNAVAIL  0x0F20 /* Vector Unavailable Interrupt */
+#define EXC_VSX_UNAVAIL     0x0F40 /* VSX Unavailable Interrupt */
+#define EXC_FACIL_UNAVAIL   0x0F60 /* Facility Unavailable Interrupt */
+#define EXC_H_FACIL_UNAVAIL 0x0F80 /* Hypervisor Facility Unavailable Interrupt */
+
+/* Base address of interrupt vector table when LPCR[AIL]=3 */
+#define AIL_VECTOR_BASE _AC(0xc000000000004000, UL)
+
 #ifndef __ASSEMBLY__
 
 #include <xen/types.h>
xen/arch/ppc/ppc64/Makefile +2 -0
index 5b88355bb2..914bb21c40 100644
@@ -1,2 +1,4 @@
+obj-y += exceptions.o
+obj-y += exceptions-asm.o
 obj-y += head.o
 obj-y += opal-calls.o
xen/arch/ppc/ppc64/asm-offsets.c +1 -0
index c15c1bf136..634d7260e3 100644
@@ -46,6 +46,7 @@ void __dummy__(void)
     OFFSET(UREGS_dsisr, struct cpu_user_regs, dsisr);
     OFFSET(UREGS_cr, struct cpu_user_regs, cr);
     OFFSET(UREGS_fpscr, struct cpu_user_regs, fpscr);
+    OFFSET(UREGS_entry_vector, struct cpu_user_regs, entry_vector);
     DEFINE(UREGS_sizeof, sizeof(struct cpu_user_regs));
 
     OFFSET(OPAL_base, struct opal, base);
xen/arch/ppc/ppc64/exceptions-asm.S new file +75 -0
new file mode 100644
index 0000000000..877df97c9b
@@ -0,0 +1,122 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include <asm/asm-defns.h>
+#include <asm/processor.h>
+
+    /* Helper to dump CPU state to struct cpu_user_regs pointed to by r1. */
+ENTRY(exception_common)
+    /* Save GPRs 1-31 */
+    SAVE_GPRS(1, 31, %r1)
+
+    /* Save LR, CTR, CR */
+    mflr    %r0
+    std     %r0, UREGS_lr(%r1)
+    mfctr   %r0
+    std     %r0, UREGS_ctr(%r1)
+    mfcr    %r0
+    stw     %r0, UREGS_cr(%r1) /* 32-bit */
+
+    /* Save Exception Registers */
+    mfsrr0  %r0
+    std     %r0, UREGS_pc(%r1)
+    mfsrr1  %r0
+    std     %r0, UREGS_msr(%r1)
+    mfdsisr %r0
+    stw     %r0, UREGS_dsisr(%r1) /* 32-bit */
+    mfdar   %r0
+    std     %r0, UREGS_dar(%r1)
+    li      %r0, -1 /* OS's SRR0/SRR1 have been clobbered */
+    std     %r0, UREGS_srr0(%r1)
+    std     %r0, UREGS_srr1(%r1)
+
+    /* Setup TOC and a stack frame then call C exception handler */
+    mr      %r3, %r1
+    bcl     20, 31, 1f
+1:  mflr    %r12
+    addis   %r2, %r12, .TOC.-1b@ha
+    addi    %r2, %r2, .TOC.-1b@l
+
+    li      %r0, 0
+    stdu    %r0, -STACK_FRAME_OVERHEAD(%r1)
+    bl      exception_handler
+
+    .size exception_common, . - exception_common
+    .type exception_common, %function
+
+    /* Same as exception_common, but for exceptions that set HSRR{0,1} */
+ENTRY(h_exception_common)
+    /* Save GPRs 1-31 */
+    SAVE_GPRS(1, 31, %r1)
+
+    /* Save LR, CTR, CR */
+    mflr    %r0
+    std     %r0, UREGS_lr(%r1)
+    mfctr   %r0
+    std     %r0, UREGS_ctr(%r1)
+    mfcr    %r0
+    stw     %r0, UREGS_cr(%r1) /* 32-bit */
+
+    /* Save Exception Registers */
+    mfhsrr0 %r0
+    std     %r0, UREGS_pc(%r1)
+    mfhsrr1 %r0
+    std     %r0, UREGS_msr(%r1)
+    mfsrr0  %r0
+    std     %r0, UREGS_srr0(%r1)
+    mfsrr1  %r0
+    std     %r0, UREGS_srr1(%r1)
+    mfdsisr %r0
+    stw     %r0, UREGS_dsisr(%r1) /* 32-bit */
+    mfdar   %r0
+    std     %r0, UREGS_dar(%r1)
+
+    /* Setup TOC and a stack frame then call C exception handler */
+    mr      %r3, %r1
+    bcl     20, 31, 1f

xen/arch/ppc/ppc64/exceptions-asm.S line 85

Quoted 10 lines
xen/arch/ppc/ppc64/exceptions-asm.S new file +10 -0
+1:  mflr    %r12
+    addis   %r2, %r12, .TOC.-1b@ha
+    addi    %r2, %r2, .TOC.-1b@l
+
+    li      %r0, 0
+    stdu    %r0, -STACK_FRAME_OVERHEAD(%r1)
+    bl      exception_handler
+
+    .size h_exception_common, . - h_exception_common
+    .type h_exception_common, %function

These two are almost identical, and differ only by the handling of srr{0,1} as far as I can tell.

Is that just because the handler is fatal, or are they likely to stay this similar longterm?

One thing you could do, which would remove the duplicated logic constructing cpu_regs would be

exception_common_ssr
    mfsrr0  %r0
    std     %r0, UREGS_srr0(%r1)
    mfsrr1  %r0
    std     %r0, UREGS_srr1(%r1)
    b exception_common

exception_common_clobber_ssr
    li      %r0, -1 /* OS's SRR0/SRR1 have been clobbered */ std     %r0, UREGS_srr0(%r1)
    std     %r0, UREGS_srr1(%r1)
    b exception_common

but this only works if the internal differences aren't going to get larger.

Quoted 9 lines
xen/arch/ppc/ppc64/exceptions-asm.S new file +9 -0
+
+/*
+ * Declare an ISR for the provided exception that jumps to `continue`
+ */
+#define DEFINE_ISR(name, exc, continue)                                        \
+    . = (AIL_VECTOR_BASE - EXCEPTION_VECTORS_START) + (exc);                   \
+    ENTRY(name)                                                                \
+    /* TODO: switch stack */                                                   \
+    /* Reserve space for struct cpu_user_regs */                               \

xen/arch/ppc/ppc64/exceptions-asm.S line 104

Quoted 10 lines
xen/arch/ppc/ppc64/exceptions-asm.S new file +10 -0
+    subi    %r1, %r1, UREGS_sizeof;                                            \
+    /* Save r0 immediately so we can use it as scratch space */                \
+    SAVE_GPR(0, %r1);                                                          \
+    /* Save exception vector number */                                         \
+    li      %r0, (exc);                                                        \
+    std     %r0, UREGS_entry_vector(%r1);                                      \
+    /* Branch to common code */                                                \
+    b       (continue);                                                        \
+    .size name, . - name;                                                      \
+    .type name, %function;

This will be much nicer as an ASM macro rather than a preprocessor macro.

.macro ISR name, exc, handler
    ...
.endm

Everything inside can stay the same, but no need for ; or \, and a few newlines go a long way for readability.

Quoted 8 lines
xen/arch/ppc/ppc64/exceptions-asm.S new file +8 -0
+
+    .section .text.exceptions, "ax", %progbits
+
+DEFINE_ISR(exc_sysreset, EXC_SYSTEM_RESET, exception_common)
+DEFINE_ISR(exc_mcheck, EXC_MACHINE_CHECK, exception_common)
+DEFINE_ISR(exc_dstore, EXC_DATA_STORAGE, exception_common)
+DEFINE_ISR(exc_dsegment, EXC_DATA_SEGMENT, exception_common)
+DEFINE_ISR(exc_istore, EXC_INSN_STORAGE, exception_common)

xen/arch/ppc/ppc64/exceptions-asm.S line 122

Quoted 10 lines
xen/arch/ppc/ppc64/exceptions-asm.S new file +10 -0
+DEFINE_ISR(exc_isegment, EXC_INSN_SEGMENT, exception_common)
+DEFINE_ISR(exc_extern, EXC_EXTERNAL, exception_common)
+DEFINE_ISR(exc_align, EXC_ALIGNMENT, exception_common)
+DEFINE_ISR(exc_program, EXC_PROGRAM, exception_common)
+DEFINE_ISR(exc_fpu, EXC_FPU_UNAVAIL, exception_common)
+DEFINE_ISR(exc_dec, EXC_DECREMENTER, exception_common)
+DEFINE_ISR(exc_h_dec, EXC_H_DECREMENTER, h_exception_common)
+/* EXC_PRIV_DOORBELL ... EXC_TRACE */
+DEFINE_ISR(exc_h_dstore, EXC_H_DATA_STORAGE, h_exception_common)
+DEFINE_ISR(exc_h_istore, EXC_H_INSN_STORAGE, h_exception_common)

It also makes this less cluttered, and I'd recommend tabulating it too.

~Andrew

#10 lore raw

Re: [PATCH 3/3] xen/ppc: Implement a basic exception handler

Quoted 188 lines · Andrew Cooper
Quoted 160 lines · Shawn Anastasio

Implement a basic exception handler that dumps the CPU state to the console, as well as the code required to set the correct exception vector table's base address in setup.c.

---
 xen/arch/ppc/include/asm/processor.h |  31 +++++++
 xen/arch/ppc/ppc64/Makefile          |   2 +
 xen/arch/ppc/ppc64/asm-offsets.c     |   1 +
 xen/arch/ppc/ppc64/exceptions-asm.S  | 122 +++++++++++++++++++++++++++
 xen/arch/ppc/ppc64/exceptions.c      | 102 ++++++++++++++++++++++
 xen/arch/ppc/setup.c                 |  11 +++
 6 files changed, 269 insertions(+)
 create mode 100644 xen/arch/ppc/ppc64/exceptions-asm.S
 create mode 100644 xen/arch/ppc/ppc64/exceptions.c
xen/arch/ppc/include/asm/processor.h +31 -0
index d3dd943c20..a01b62b8a4 100644
@@ -103,6 +103,37 @@
 #define PVR_BE        0x0070
 #define PVR_PA6T      0x0090
 
+/* Exception Definitions */
+#define EXC_SYSTEM_RESET    0x0100 /* System Reset Interrupt */
+#define EXC_MACHINE_CHECK   0x0200 /* Machine Check Interrupt */
+#define EXC_DATA_STORAGE    0x0300 /* Data Storage Interrupt */
+#define EXC_DATA_SEGMENT    0x0380 /* Data Segment Interrupt */
+#define EXC_INSN_STORAGE    0x0400 /* Instruction Storage Interrupt */
+#define EXC_INSN_SEGMENT    0x0480 /* Instruction Segment Interrupt */
+#define EXC_EXTERNAL        0x0500 /* External Interrupt */
+#define EXC_ALIGNMENT       0x0600 /* Alignment Interrupt */
+#define EXC_PROGRAM         0x0700 /* Program Interrupt */
+#define EXC_FPU_UNAVAIL     0x0800 /* Floating-Point Unavailable Interrupt */
+#define EXC_DECREMENTER     0x0900 /* Decrementer Interrupt */
+#define EXC_H_DECREMENTER   0x0980 /* Hypervisor Decrementer Interrupt */
+#define EXC_PRIV_DOORBELL   0x0A00 /* Directed Privileged Doorbell Interrupt */
+#define EXC_SYSTEM_CALL     0x0C00 /* System Call Interrupt */
+#define EXC_TRACE           0x0D00 /* Trace Interrupt */
+#define EXC_H_DATA_STORAGE  0x0E00 /* Hypervisor Data Storage Interrupt */
+#define EXC_H_INSN_STORAGE  0x0E20 /* Hypervisor Instruction Storage Interrupt */
+#define EXC_H_EMUL_ASST     0x0E40 /* Hypervisor Emulation Assistance Interrupt */
+#define EXC_H_MAINTENANCE   0x0E60 /* Hypervisor Maintenance Interrupt */
+#define EXC_H_DOORBELL      0x0E80 /* Directed Hypervisor Doorbell Interrupt */
+#define EXC_H_VIRT          0x0EA0 /* Hypervisor Virtualization Interrupt */
+#define EXC_PERF_MON        0x0F00 /* Performance Monitor Interrupt */
+#define EXC_VECTOR_UNAVAIL  0x0F20 /* Vector Unavailable Interrupt */
+#define EXC_VSX_UNAVAIL     0x0F40 /* VSX Unavailable Interrupt */
+#define EXC_FACIL_UNAVAIL   0x0F60 /* Facility Unavailable Interrupt */
+#define EXC_H_FACIL_UNAVAIL 0x0F80 /* Hypervisor Facility Unavailable Interrupt */
+
+/* Base address of interrupt vector table when LPCR[AIL]=3 */
+#define AIL_VECTOR_BASE _AC(0xc000000000004000, UL)
+
 #ifndef __ASSEMBLY__
 
 #include <xen/types.h>
xen/arch/ppc/ppc64/Makefile +2 -0
index 5b88355bb2..914bb21c40 100644
@@ -1,2 +1,4 @@
+obj-y += exceptions.o
+obj-y += exceptions-asm.o
 obj-y += head.o
 obj-y += opal-calls.o
xen/arch/ppc/ppc64/asm-offsets.c +1 -0
index c15c1bf136..634d7260e3 100644
@@ -46,6 +46,7 @@ void __dummy__(void)
     OFFSET(UREGS_dsisr, struct cpu_user_regs, dsisr);
     OFFSET(UREGS_cr, struct cpu_user_regs, cr);
     OFFSET(UREGS_fpscr, struct cpu_user_regs, fpscr);
+    OFFSET(UREGS_entry_vector, struct cpu_user_regs, entry_vector);
     DEFINE(UREGS_sizeof, sizeof(struct cpu_user_regs));
 
     OFFSET(OPAL_base, struct opal, base);
xen/arch/ppc/ppc64/exceptions-asm.S new file +75 -0
new file mode 100644
index 0000000000..877df97c9b
@@ -0,0 +1,122 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include <asm/asm-defns.h>
+#include <asm/processor.h>
+
+    /* Helper to dump CPU state to struct cpu_user_regs pointed to by r1. */
+ENTRY(exception_common)
+    /* Save GPRs 1-31 */
+    SAVE_GPRS(1, 31, %r1)
+
+    /* Save LR, CTR, CR */
+    mflr    %r0
+    std     %r0, UREGS_lr(%r1)
+    mfctr   %r0
+    std     %r0, UREGS_ctr(%r1)
+    mfcr    %r0
+    stw     %r0, UREGS_cr(%r1) /* 32-bit */
+
+    /* Save Exception Registers */
+    mfsrr0  %r0
+    std     %r0, UREGS_pc(%r1)
+    mfsrr1  %r0
+    std     %r0, UREGS_msr(%r1)
+    mfdsisr %r0
+    stw     %r0, UREGS_dsisr(%r1) /* 32-bit */
+    mfdar   %r0
+    std     %r0, UREGS_dar(%r1)
+    li      %r0, -1 /* OS's SRR0/SRR1 have been clobbered */
+    std     %r0, UREGS_srr0(%r1)
+    std     %r0, UREGS_srr1(%r1)
+
+    /* Setup TOC and a stack frame then call C exception handler */
+    mr      %r3, %r1
+    bcl     20, 31, 1f
+1:  mflr    %r12
+    addis   %r2, %r12, .TOC.-1b@ha
+    addi    %r2, %r2, .TOC.-1b@l
+
+    li      %r0, 0
+    stdu    %r0, -STACK_FRAME_OVERHEAD(%r1)
+    bl      exception_handler
+
+    .size exception_common, . - exception_common
+    .type exception_common, %function
+
+    /* Same as exception_common, but for exceptions that set HSRR{0,1} */
+ENTRY(h_exception_common)
+    /* Save GPRs 1-31 */
+    SAVE_GPRS(1, 31, %r1)
+
+    /* Save LR, CTR, CR */
+    mflr    %r0
+    std     %r0, UREGS_lr(%r1)
+    mfctr   %r0
+    std     %r0, UREGS_ctr(%r1)
+    mfcr    %r0
+    stw     %r0, UREGS_cr(%r1) /* 32-bit */
+
+    /* Save Exception Registers */
+    mfhsrr0 %r0
+    std     %r0, UREGS_pc(%r1)
+    mfhsrr1 %r0
+    std     %r0, UREGS_msr(%r1)
+    mfsrr0  %r0
+    std     %r0, UREGS_srr0(%r1)
+    mfsrr1  %r0
+    std     %r0, UREGS_srr1(%r1)
+    mfdsisr %r0
+    stw     %r0, UREGS_dsisr(%r1) /* 32-bit */
+    mfdar   %r0
+    std     %r0, UREGS_dar(%r1)
+
+    /* Setup TOC and a stack frame then call C exception handler */
+    mr      %r3, %r1
+    bcl     20, 31, 1f

xen/arch/ppc/ppc64/exceptions-asm.S line 85

Quoted 10 lines
xen/arch/ppc/ppc64/exceptions-asm.S new file +10 -0
+1:  mflr    %r12
+    addis   %r2, %r12, .TOC.-1b@ha
+    addi    %r2, %r2, .TOC.-1b@l
+
+    li      %r0, 0
+    stdu    %r0, -STACK_FRAME_OVERHEAD(%r1)
+    bl      exception_handler
+
+    .size h_exception_common, . - h_exception_common
+    .type h_exception_common, %function

These two are almost identical, and differ only by the handling of srr{0,1} as far as I can tell.

Is that just because the handler is fatal, or are they likely to stay this similar longterm?

One thing you could do, which would remove the duplicated logic constructing cpu_regs would be

exception_common_ssr

    mfsrr0  %r0
    std     %r0, UREGS_srr0(%r1)
    mfsrr1  %r0
    std     %r0, UREGS_srr1(%r1)

b exception_common

Quoted 8 lines

exception_common_clobber_ssr

    li      %r0, -1 /* OS's SRR0/SRR1 have been clobbered */
    std     %r0, UREGS_srr0(%r1)
    std     %r0, UREGS_srr1(%r1)

b exception_common

but this only works if the internal differences aren't going to get larger.

I anticipate that these two will end up diverging further in the medium-to-long term, so if you'd accept it I'd rather keep them separate initially.

Quoted 22 lines
Quoted 9 lines
xen/arch/ppc/ppc64/exceptions-asm.S new file +9 -0
+
+/*
+ * Declare an ISR for the provided exception that jumps to `continue`
+ */
+#define DEFINE_ISR(name, exc, continue)                                        \
+    . = (AIL_VECTOR_BASE - EXCEPTION_VECTORS_START) + (exc);                   \
+    ENTRY(name)                                                                \
+    /* TODO: switch stack */                                                   \
+    /* Reserve space for struct cpu_user_regs */                               \

xen/arch/ppc/ppc64/exceptions-asm.S line 104

Quoted 10 lines
xen/arch/ppc/ppc64/exceptions-asm.S new file +10 -0
+    subi    %r1, %r1, UREGS_sizeof;                                            \
+    /* Save r0 immediately so we can use it as scratch space */                \
+    SAVE_GPR(0, %r1);                                                          \
+    /* Save exception vector number */                                         \
+    li      %r0, (exc);                                                        \
+    std     %r0, UREGS_entry_vector(%r1);                                      \
+    /* Branch to common code */                                                \
+    b       (continue);                                                        \
+    .size name, . - name;                                                      \
+    .type name, %function;

This will be much nicer as an ASM macro rather than a preprocessor macro.

Quoted 7 lines

.macro ISR name, exc, handler

    ...

.endm

Everything inside can stay the same, but no need for ; or \, and a few newlines go a long way for readability.

Fair enough, will do.

Quoted 13 lines
Quoted 13 lines
xen/arch/ppc/ppc64/exceptions-asm.S new file +12 -0
+
+    .section .text.exceptions, "ax", %progbits
+
+DEFINE_ISR(exc_sysreset, EXC_SYSTEM_RESET, exception_common)
+DEFINE_ISR(exc_mcheck, EXC_MACHINE_CHECK, exception_common)
+DEFINE_ISR(exc_dstore, EXC_DATA_STORAGE, exception_common)
+DEFINE_ISR(exc_dsegment, EXC_DATA_SEGMENT, exception_common)
+DEFINE_ISR(exc_istore, EXC_INSN_STORAGE, exception_common)
+DEFINE_ISR(exc_isegment, EXC_INSN_SEGMENT, exception_common)
+DEFINE_ISR(exc_extern, EXC_EXTERNAL, exception_common)
+DEFINE_ISR(exc_align, EXC_ALIGNMENT, exception_common)
+DEFINE_ISR(exc_program, EXC_PROGRAM, exception_common)
Quoted 9 lines

xen/arch/ppc/ppc64/exceptions-asm.S line 122

Quoted 6 lines
xen/arch/ppc/ppc64/exceptions-asm.S new file +6 -0
+DEFINE_ISR(exc_fpu, EXC_FPU_UNAVAIL, exception_common)
+DEFINE_ISR(exc_dec, EXC_DECREMENTER, exception_common)
+DEFINE_ISR(exc_h_dec, EXC_H_DECREMENTER, h_exception_common)
+/* EXC_PRIV_DOORBELL ... EXC_TRACE */
+DEFINE_ISR(exc_h_dstore, EXC_H_DATA_STORAGE, h_exception_common)
+DEFINE_ISR(exc_h_istore, EXC_H_INSN_STORAGE, h_exception_common)

It also makes this less cluttered, and I'd recommend tabulating it too.

Ditto.

Quoted 1 line

~Andrew

Thanks,
Shawn