Skip to contentSkip to conversation

xen/ppc: Add early Radix MMU support

PATCH Superseded Shawn Anastasio 2 participants 11 messages Jul 28, 2023 – Aug 10, 2023

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover xen/ppc: Add early Radix MMU support

Hello all,

This series adds support for bringing up the Radix MMU with basic identity-mapped page tables.

In order to simplify the memory layout, the series changes XEN_VIRT _START to 0xC000_0000_0000_0000, which has a couple of convenient properties. When the MMU is off, the top 4 address bits are ignored by the hardware, so this is equivalent to physical address 0. When the MMU is enabled, the top 4 address bits are used to select the memory quadrant, and 0xC corresponds to quadrant 3 which is used for kernel memory. Thus, by linking the kernel here and ensuring its physical load address is 0x0, we are able to run at the correct address both before and after the MMU is enabled.

Additionally, as the Radix MMU was only added in ISA 3.0 (POWER9), this series also drops support for POWER8 for now, to avoid having a non-working build configuration in-tree.

Thanks,
Shawn

Shawn Anastasio (3):
xen/ppc: Bump minimum target ISA to 3.0 (POWER9) xen/ppc: Relocate kernel to physical address 0 on boot xen/ppc: Implement initial Radix MMU support

 xen/arch/ppc/Kconfig                 |   7 +-
 xen/arch/ppc/Makefile                |   2 +
 xen/arch/ppc/arch.mk                 |   1 -
 xen/arch/ppc/include/asm/bitops.h    |  11 ++
 xen/arch/ppc/include/asm/mm.h        |  19 ++
 xen/arch/ppc/include/asm/page-bits.h |   3 +-
 xen/arch/ppc/include/asm/page.h      | 178 ++++++++++++++++++
 xen/arch/ppc/include/asm/processor.h |  31 ++++
 xen/arch/ppc/include/asm/regs.h      | 138 ++++++++++++++
 xen/arch/ppc/include/asm/types.h     |   1 +
 xen/arch/ppc/mm-radix.c              | 268 +++++++++++++++++++++++++++
 xen/arch/ppc/opal.c                  |   2 +
 xen/arch/ppc/ppc64/head.S            |  42 +++++
 xen/arch/ppc/setup.c                 |   4 +
 xen/arch/ppc/tlb-radix.c             |  95 ++++++++++
 15 files changed, 794 insertions(+), 8 deletions(-)
 create mode 100644 xen/arch/ppc/include/asm/bitops.h
 create mode 100644 xen/arch/ppc/include/asm/mm.h
 create mode 100644 xen/arch/ppc/include/asm/page.h
 create mode 100644 xen/arch/ppc/include/asm/regs.h
 create mode 100644 xen/arch/ppc/mm-radix.c
 create mode 100644 xen/arch/ppc/tlb-radix.c

--
2.30.2

#7 lore raw

PATCH 3/3 xen/ppc: Implement initial Radix MMU support

Add code to construct early identity-mapped page tables as well as the required process and partition tables to enable the MMU.

---
 xen/arch/ppc/Makefile                |   2 +
 xen/arch/ppc/include/asm/bitops.h    |  11 ++
 xen/arch/ppc/include/asm/mm.h        |  19 ++
 xen/arch/ppc/include/asm/page-bits.h |   3 +-
 xen/arch/ppc/include/asm/page.h      | 178 ++++++++++++++++++
 xen/arch/ppc/include/asm/processor.h |  31 ++++
 xen/arch/ppc/include/asm/regs.h      | 138 ++++++++++++++
 xen/arch/ppc/include/asm/types.h     |   1 +
 xen/arch/ppc/mm-radix.c              | 268 +++++++++++++++++++++++++++
 xen/arch/ppc/opal.c                  |   2 +
 xen/arch/ppc/ppc64/head.S            |  15 ++
 xen/arch/ppc/setup.c                 |   4 +
 xen/arch/ppc/tlb-radix.c             |  95 ++++++++++
 13 files changed, 766 insertions(+), 1 deletion(-)
 create mode 100644 xen/arch/ppc/include/asm/bitops.h
 create mode 100644 xen/arch/ppc/include/asm/mm.h
 create mode 100644 xen/arch/ppc/include/asm/page.h
 create mode 100644 xen/arch/ppc/include/asm/regs.h
 create mode 100644 xen/arch/ppc/mm-radix.c
 create mode 100644 xen/arch/ppc/tlb-radix.c
xen/arch/ppc/Makefile +2 -0
index 0c0a7884a1..a059ac4c0a 100644
@@ -2,8 +2,10 @@ obj-$(CONFIG_PPC64) += ppc64/
 
 obj-y += boot-of.init.o
 obj-$(CONFIG_EARLY_PRINTK) += early_printk.init.o
+obj-y += mm-radix.o
 obj-y += opal.o
 obj-y += setup.o
+obj-y += tlb-radix.o
 
 $(TARGET): $(TARGET)-syms
 	cp -f $< $@
xen/arch/ppc/include/asm/bitops.h new file +11 -0
new file mode 100644
index 0000000000..a7cd8ec7c5
@@ -0,0 +1,11 @@
+#ifndef _ASM_PPC_BITOPS_H
+#define _ASM_PPC_BITOPS_H
+
+#include <xen/compiler.h>
+
+/* PPC bit number conversion */
+#define PPC_BITLSHIFT(be)	(BITS_PER_LONG - 1 - (be))
+#define PPC_BIT(bit)		(1UL << PPC_BITLSHIFT(bit))
+#define PPC_BITMASK(bs, be)	((PPC_BIT(bs) - PPC_BIT(be)) | PPC_BIT(bs))
+
+#endif /* _ASM_PPC_BITOPS_H */
xen/arch/ppc/include/asm/mm.h new file +19 -0
new file mode 100644
index 0000000000..36e44a4356
@@ -0,0 +1,19 @@
+#ifndef _ASM_PPC_MM_H
+#define _ASM_PPC_MM_H
+
+#include <asm/config.h>
+#include <asm/page-bits.h>
+
+#define pfn_to_paddr(pfn) ((paddr_t)(pfn) << PAGE_SHIFT)
+#define paddr_to_pfn(pa)  ((unsigned long)((pa) >> PAGE_SHIFT))
+
+#define virt_to_maddr(va) ((paddr_t)((vaddr_t)(va) & PADDR_MASK))
+#define maddr_to_virt(pa) ((void *)((paddr_t)(pa) | XEN_VIRT_START)
+
+/* Convert between Xen-heap virtual addresses and machine addresses. */
+#define __pa(x)             (virt_to_maddr(x))
+#define __va(x)             (maddr_to_virt(x))
+
+void setup_initial_pagetables(void);
+
+#endif /* _ASM_PPC_MM_H */
xen/arch/ppc/include/asm/page-bits.h +2 -1
index 4c01bf9716..0286177520 100644
@@ -2,6 +2,7 @@
 #define __PPC_PAGE_BITS_H__
 
 #define PAGE_SHIFT              16 /* 64 KiB Pages */
-#define PADDR_BITS              48
+#define PADDR_BITS              53
+#define VADDR_BITS              52
 
 #endif /* __PPC_PAGE_BITS_H__ */
xen/arch/ppc/include/asm/page.h new file +178 -0

178 lines not on this page · raw message

xen/arch/ppc/include/asm/processor.h +31 -0
index 2300640787..417288738c 100644
@@ -133,6 +133,37 @@ struct cpu_user_regs
     uint32_t entry_vector;
 };
 
+static __inline__ void sync(void)
+{
+    asm volatile ( "sync" );
+}
+
+static __inline__ void isync(void)
+{
+    asm volatile ( "isync" );
+}
+
+static inline unsigned long mfmsr(void)
+{
+    unsigned long msr;
+    asm volatile ( "mfmsr %0" : "=&r"(msr) );
+    return msr;
+}
+
+static inline void mtmsrd(unsigned long msr)
+{
+    asm volatile ( "mtmsrd %0" : : "r"(msr) );
+}
+
+#define mtspr(reg, val) asm volatile ( "mtspr %0,%1" : : "K"(reg), "r"(val) )
+
+#define mfspr(reg)                                                             \
+    ({                                                                         \
+        unsigned long val;                                                     \
+        asm volatile ( "mfspr %0,%1" : "=r"(val) : "K"(reg) );                 \
+        val;                                                                   \
+    })
+
 /*
  * panic() isn't available at the moment so an infinite loop will be
  * used temporarily.
xen/arch/ppc/include/asm/regs.h new file +138 -0

138 lines not on this page · raw message

xen/arch/ppc/include/asm/types.h +1 -0
index cee08e111a..325a2a27c3 100644
@@ -15,6 +15,7 @@ typedef unsigned int u32;
 typedef signed long s64;
 typedef unsigned long u64;
 typedef unsigned long paddr_t;
+typedef unsigned long vaddr_t;
 #define INVALID_PADDR (~0UL)
 #define PRIpaddr "016lx"
 
xen/arch/ppc/mm-radix.c new file +268 -0

268 lines not on this page · raw message

xen/arch/ppc/opal.c +2 -0
index cc75ccfcbe..727f8726fa 100644
@@ -1,8 +1,10 @@
 /* SPDX-License-Identifier: GPL-2.0-or-later */
+#include "xen/compiler.h"
 #include <asm/boot.h>
 #include <asm/early_printk.h>
 #include <asm/opal-api.h>
 #include <asm/processor.h>
+#include <asm/mm.h>
 #include <xen/types.h>
 #include <xen/libfdt/libfdt.h>
 #include <xen/init.h>
xen/arch/ppc/ppc64/head.S +15 -0
index beff8257fa..5ae85ae123 100644
@@ -2,6 +2,7 @@
 
 #include <asm/asm-defns.h>
 #include <asm/asm-offsets.h>
+#include <asm/msr.h>
 
     .section .text.header, "ax", %progbits
 
@@ -67,3 +68,17 @@ ENTRY(start)
 
     .size start, . - start
     .type start, %function
+
+ENTRY(enable_mmu)
+    mflr %r3
+    mfmsr %r4
+
+    /* enable instruction and data relocation (MMU) */
+    ori %r4, %r4, (MSR_IR | MSR_DR)
+
+    mtsrr0 %r3 /* return to caller after MMU enable */
+    mtsrr1 %r4
+    rfid
+
+    .size enable_mmu, . - enable_mmu
+    .type enable_mmu, %function
xen/arch/ppc/setup.c +4 -0
index bacd6d1acd..466993987b 100644
@@ -7,6 +7,8 @@
 /* Xen stack for bringing up the first CPU. */
 unsigned char __initdata cpu0_boot_stack[STACK_SIZE] __aligned(STACK_SIZE);
 
+void __init setup_initial_pagetables(void);
+
 void __init noreturn start_xen(unsigned long r3, unsigned long r4,
                                unsigned long r5, unsigned long r6,
                                unsigned long r7)
@@ -27,6 +29,8 @@ void __init noreturn start_xen(unsigned long r3, unsigned long r4,
         boot_opal_init((void *) r3);
     }
 
+    setup_initial_pagetables();
+
     early_printk("Hello, ppc64le!\n");
 
     for ( ; ; )
xen/arch/ppc/tlb-radix.c new file +95 -0

95 lines not on this page · raw message

Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/3 xen/ppc: Bump minimum target ISA to 3.0 (POWER9)

In reply to Shawn Anastasio

In preparation for implementing ISA3+ Radix MMU support, drop ISA 2.07B from the supported ISA list to avoid having a non-working configuration in tree. It can be re-added at a later point when Hash MMU support is added.

---
 xen/arch/ppc/Kconfig | 7 +------
 xen/arch/ppc/arch.mk | 1 -
 2 files changed, 1 insertion(+), 7 deletions(-)
xen/arch/ppc/Kconfig +1 -6
index b32dce39b8..ab116ffb2a 100644
@@ -20,15 +20,10 @@ menu "ISA Selection"
 
 choice
 	prompt "Base ISA"
-	default POWER_ISA_2_07B
+	default POWER_ISA_3_00
 	help
 	  This selects the base ISA version that Xen will target.
 
-config POWER_ISA_2_07B
-	bool "Power ISA 2.07B (POWER8)"
-	help
-	  Target version 2.07B of the Power ISA (POWER8) or later
-
 config POWER_ISA_3_00
 	bool "Power ISA 3.00 (POWER9)"
 	help
xen/arch/ppc/arch.mk +0 -1
index 9f66f7a57b..f48ead6580 100644
@@ -1,7 +1,6 @@
 ########################################
 # Power-specific definitions
 
-ppc-march-$(CONFIG_POWER_ISA_2_07B) := power8
 ppc-march-$(CONFIG_POWER_ISA_3_00) := power9
 
 CFLAGS += -m64 -mlittle-endian -mcpu=$(ppc-march-y)
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/3 xen/ppc: Relocate kernel to physical address 0 on boot

In reply to Shawn Anastasio

Introduce a small assembly loop in start to copy the kernel to physical address 0 before continuing. This ensures that the physical address lines up with XEN_VIRT_START (0xc000000000000000) and allows us to identity map the kernel when the MMU is set up in the next patch.

We are also able to start execution at XEN_VIRT_START after the copy since hardware will ignore the top 4 address bits when operating in Real Mode (MMU off).

---
 xen/arch/ppc/ppc64/head.S | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
xen/arch/ppc/ppc64/head.S +27 -0
index 5ac2dad2ee..beff8257fa 100644
@@ -18,6 +18,33 @@ ENTRY(start)
     addis   %r2, %r12, .TOC.-1b@ha
     addi    %r2, %r2, .TOC.-1b@l
 
+    /*
+     * Copy Xen to physical address zero and jump to XEN_VIRT_START
+     * (0xc000000000000000). This works because the hardware will ignore the top
+     * four address bits when the MMU is off.
+     */
+    LOAD_REG_ADDR(%r1, start)
+    LOAD_IMM64(%r12, XEN_VIRT_START)
+
+    /* If we're at the correct address, skip copy */
+    cmpld   %r1, %r12
+    beq     .L_correct_address
+
+    /* Copy bytes until _end */
+    LOAD_REG_ADDR(%r11, _end)
+    addi    %r1, %r1, -8
+    li      %r13, -8
+.L_copy_xen:
+    ldu     %r10, 8(%r1)
+    stdu    %r10, 8(%r13)
+    cmpld   %r1, %r11
+    blt     .L_copy_xen
+
+    /* Jump to XEN_VIRT_START */
+    mtctr   %r12
+    bctr
+.L_correct_address:
+
     /* set up the initial stack */
     LOAD_REG_ADDR(%r1, cpu0_boot_stack)
     li      %r11, 0
Signature · 1 line
2.30.2
#3 lore raw

Re: [PATCH 2/3] xen/ppc: Relocate kernel to physical address 0 on boot

Quoted 4 lines · Shawn Anastasio

Introduce a small assembly loop in start to copy the kernel to physical address 0 before continuing. This ensures that the physical address lines up with XEN_VIRT_START (0xc000000000000000) and allows us to identity map the kernel when the MMU is set up in the next patch.

So PPC guarantees there's always a reasonable amount of memory at 0, and that's available for use?

xen/arch/ppc/ppc64/head.S ENTRY(start) · line 26

Quoted 12 lines
xen/arch/ppc/ppc64/head.S +6 -0
@@ -18,6 +18,33 @@ ENTRY(start)
     addis   %r2, %r12, .TOC.-1b@ha
     addi    %r2, %r2, .TOC.-1b@l
 
+    /*
+     * Copy Xen to physical address zero and jump to XEN_VIRT_START
+     * (0xc000000000000000). This works because the hardware will ignore the top
+     * four address bits when the MMU is off.
+     */
+    LOAD_REG_ADDR(%r1, start)

I think you really mean _start here (which is missing from the linker script), not start. See also Andrew's recent related RISC-V change.

xen/arch/ppc/ppc64/head.S ENTRY(start) · line 31

Quoted 5 lines
xen/arch/ppc/ppc64/head.S +5 -0
+    LOAD_IMM64(%r12, XEN_VIRT_START)
+
+    /* If we're at the correct address, skip copy */
+    cmpld   %r1, %r12
+    beq     .L_correct_address

Can this ever be the case, especially with the MMU-off behavior you describe in the comment above? Wouldn't you need to ignore the top four bits in the comparison?

Quoted 4 lines
xen/arch/ppc/ppc64/head.S +4 -0
+    /* Copy bytes until _end */
+    LOAD_REG_ADDR(%r11, _end)
+    addi    %r1, %r1, -8
+    li      %r13, -8

xen/arch/ppc/ppc64/head.S ENTRY(start) · line 46

Quoted 10 lines
xen/arch/ppc/ppc64/head.S +10 -0
+.L_copy_xen:
+    ldu     %r10, 8(%r1)
+    stdu    %r10, 8(%r13)
+    cmpld   %r1, %r11
+    blt     .L_copy_xen
+
+    /* Jump to XEN_VIRT_START */
+    mtctr   %r12
+    bctr
+.L_correct_address:

Can the two regions potentially overlap? Looking at the ELF header it's not clear to me what guarantees there are that this can't happen.

Jan

#4 lore raw

Re: [PATCH 2/3] xen/ppc: Relocate kernel to physical address 0 on boot

Quoted 8 lines · Jan Beulich
Quoted 4 lines · Shawn Anastasio

Introduce a small assembly loop in start to copy the kernel to physical address 0 before continuing. This ensures that the physical address lines up with XEN_VIRT_START (0xc000000000000000) and allows us to identity map the kernel when the MMU is set up in the next patch.

So PPC guarantees there's always a reasonable amount of memory at 0, and that's available for use?

Both Linux and FreeBSD rely on this being the case, so it's essentially a de facto standard, though I'm not aware of any specification that guarantees it.

Quoted 6 lines
Quoted 6 lines
xen/arch/ppc/ppc64/head.S +0 -0
@@ -18,6 +18,33 @@ ENTRY(start)
     addis   %r2, %r12, .TOC.-1b@ha
     addi    %r2, %r2, .TOC.-1b@l
 
Quoted 9 lines

xen/arch/ppc/ppc64/head.S ENTRY(start) · line 26

Quoted 6 lines
xen/arch/ppc/ppc64/head.S +6 -0
+    /*
+     * Copy Xen to physical address zero and jump to XEN_VIRT_START
+     * (0xc000000000000000). This works because the hardware will ignore the top
+     * four address bits when the MMU is off.
+     */
+    LOAD_REG_ADDR(%r1, start)

I think you really mean _start here (which is missing from the linker script),

The PIC patch series fixes the missing _start definition in the linker script. In the cover letter of v2 I'll add a clear note that this series is based on that one.

Quoted 1 line

not start. See also Andrew's recent related RISC-V change.

Good point. In practice this worked because the start function was the first thing in the first section of the linker script, but of course using _start here is more correct.

Quoted 10 lines

xen/arch/ppc/ppc64/head.S ENTRY(start) · line 31

Quoted 6 lines
xen/arch/ppc/ppc64/head.S +5 -0
+    LOAD_IMM64(%r12, XEN_VIRT_START)
+
+    /* If we're at the correct address, skip copy */
+    cmpld   %r1, %r12
+    beq     .L_correct_address

Can this ever be the case, especially with the MMU-off behavior you describe in the comment above? Wouldn't you need to ignore the top four bits in the comparison?

It will always be the case after the code jumps to XEN_VIRT_START after the copy takes place. I could have it jump past the copy loop entirely, but then I'd need to duplicate the TOC setup.

Quoted 15 lines
Quoted 4 lines
xen/arch/ppc/ppc64/head.S +4 -0
+    /* Copy bytes until _end */
+    LOAD_REG_ADDR(%r11, _end)
+    addi    %r1, %r1, -8
+    li      %r13, -8

xen/arch/ppc/ppc64/head.S ENTRY(start) · line 46

Quoted 10 lines
xen/arch/ppc/ppc64/head.S +10 -0
+.L_copy_xen:
+    ldu     %r10, 8(%r1)
+    stdu    %r10, 8(%r13)
+    cmpld   %r1, %r11
+    blt     .L_copy_xen
+
+    /* Jump to XEN_VIRT_START */
+    mtctr   %r12
+    bctr
+.L_correct_address:
Quoted 3 lines

Can the two regions potentially overlap? Looking at the ELF header it's not clear to me what guarantees there are that this can't happen.

As I understand it, any bootloader that placed the kernel at a low enough address for this to be an issue wouldn't be able to boot Linux or FreeBSD, so in practice it's a safe bet that this won't be the case.

Quoted 1 line

Jan

Thanks,
Shawn

#5 lore raw

Re: [PATCH 2/3] xen/ppc: Relocate kernel to physical address 0 on boot

Quoted 6 lines · Shawn Anastasio
Quoted 5 lines · Jan Beulich
Quoted 3 lines · Shawn Anastasio
xen/arch/ppc/ppc64/head.S +3 -0
+    /* If we're at the correct address, skip copy */
+    cmpld   %r1, %r12
+    beq     .L_correct_address
Quoted 6 lines
Quoted 3 lines

Can this ever be the case, especially with the MMU-off behavior you describe in the comment above? Wouldn't you need to ignore the top four bits in the comparison?

It will always be the case after the code jumps to XEN_VIRT_START after the copy takes place.

Well, of course.

Quoted 2 lines

I could have it jump past the copy loop entirely,
but then I'd need to duplicate the TOC setup.

I don't think I understand this part of your reply: .L_correct_address _is_ past the copy loop.

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
xen/arch/ppc/ppc64/head.S +14 -0
+    /* Copy bytes until _end */
+    LOAD_REG_ADDR(%r11, _end)
+    addi    %r1, %r1, -8
+    li      %r13, -8
+.L_copy_xen:
+    ldu     %r10, 8(%r1)
+    stdu    %r10, 8(%r13)
+    cmpld   %r1, %r11
+    blt     .L_copy_xen
+
+    /* Jump to XEN_VIRT_START */
+    mtctr   %r12
+    bctr
+.L_correct_address:
Quoted 7 lines
Quoted 3 lines

Can the two regions potentially overlap? Looking at the ELF header it's not clear to me what guarantees there are that this can't happen.

As I understand it, any bootloader that placed the kernel at a low enough address for this to be an issue wouldn't be able to boot Linux or FreeBSD, so in practice it's a safe bet that this won't be the case.

Fair enough then.

Jan

#8 lore raw

Re: [PATCH 3/3] xen/ppc: Implement initial Radix MMU support

In reply to Shawn Anastasio

xen/arch/ppc/include/asm/bitops.h line 4

Quoted 7 lines · Shawn Anastasio
xen/arch/ppc/include/asm/bitops.h new file +4 -0
@@ -0,0 +1,11 @@
+#ifndef _ASM_PPC_BITOPS_H
+#define _ASM_PPC_BITOPS_H
+
+#include <xen/compiler.h>

Not a big deal, but ...

xen/arch/ppc/include/asm/bitops.h line 11

Quoted 6 lines
xen/arch/ppc/include/asm/bitops.h new file +6 -0
+/* PPC bit number conversion */
+#define PPC_BITLSHIFT(be)	(BITS_PER_LONG - 1 - (be))
+#define PPC_BIT(bit)		(1UL << PPC_BITLSHIFT(bit))
+#define PPC_BITMASK(bs, be)	((PPC_BIT(bs) - PPC_BIT(be)) | PPC_BIT(bs))
+
+#endif /* _ASM_PPC_BITOPS_H */

... nothing here looks to require that #include.

xen/arch/ppc/include/asm/mm.h line 4

Quoted 7 lines
xen/arch/ppc/include/asm/mm.h new file +4 -0
@@ -0,0 +1,19 @@
+#ifndef _ASM_PPC_MM_H
+#define _ASM_PPC_MM_H
+
+#include <asm/config.h>

This is included by xen/config.h, which in turn is included from the compiler command line.

Quoted 28 lines
xen/arch/ppc/include/asm/page.h new file +25 -0
@@ -0,0 +1,178 @@
+#ifndef _ASM_PPC_PAGE_H
+#define _ASM_PPC_PAGE_H
+
+#include <xen/types.h>
+
+#include <asm/bitops.h>
+#include <asm/byteorder.h>
+
+#define PDE_VALID     PPC_BIT(0)
+#define PDE_NLB_MASK  0xfffffffffffffUL
+#define PDE_NLB_SHIFT 5UL
+#define PDE_NLS_MASK  0x1f
+
+#define PTE_VALID     PPC_BIT(0)
+#define PTE_LEAF      PPC_BIT(1)
+#define PTE_REFERENCE PPC_BIT(55)
+#define PTE_CHANGE    PPC_BIT(56)
+
+/* PTE Attributes */
+#define PTE_ATT_SAO            PPC_BIT(59) /* Strong Access Ordering */
+#define PTE_ATT_NON_IDEMPOTENT PPC_BIT(58)
+#define PTE_ATT_TOLERANT       (PPC_BIT(58) | PPC_BIT(59))
+
+/* PTE Encoded Access Authority*/
+#define PTE_EAA_PRIVILEGED PPC_BIT(60)

xen/arch/ppc/include/asm/page.h line 35

Quoted 10 lines
xen/arch/ppc/include/asm/page.h new file +10 -0
+#define PTE_EAA_READ       PPC_BIT(61)
+#define PTE_EAA_WRITE      PPC_BIT(62)
+#define PTE_EAA_EXECUTE    PPC_BIT(63)
+
+/* Field shifts/masks */
+#define PTE_RPN_MASK  0x1fffffffffffUL
+#define PTE_RPN_SHIFT 12UL
+#define PTE_ATT_MASK  0x3UL
+#define PTE_ATT_SHIFT 4UL
+#define PTE_EAA_MASK  0xfUL

Did you consider introducing just *_MASK values, utilizing MASK_INSR() and MASK_EXTR() instead of open-coded shifting/masking?

Quoted 38 lines
xen/arch/ppc/include/asm/page.h new file +38 -0
+#define PTE_XEN_BASE (PTE_VALID | PTE_EAA_PRIVILEGED | PTE_REFERENCE)
+#define PTE_XEN_RW   (PTE_XEN_BASE | PTE_EAA_READ | PTE_EAA_WRITE | PTE_CHANGE)
+#define PTE_XEN_RO   (PTE_XEN_BASE | PTE_EAA_READ)
+#define PTE_XEN_RX   (PTE_XEN_BASE | PTE_EAA_READ | PTE_EAA_EXECUTE)
+
+/*
+ * Radix Tree layout for 64KB pages:
+ *
+ * [ L1 (ROOT) PAGE DIRECTORY (8192 * sizeof(pde_t)) ]
+ *                     |
+ *                     |
+ *                     v
+ *    [ L2 PAGE DIRECTORY (512 * sizeof(pde_t)) ]
+ *                     |
+ *                     |
+ *                     v
+ *    [ L3 PAGE DIRECTORY (512 * sizeof(pde_t)) ]
+ *                     |
+ *                     |
+ *                     v
+ *      [ L4 PAGE TABLE (32 * sizeof(pte_t)) ]
+ *                     |
+ *                     |
+ *                     v
+ *            [ PAGE TABLE ENTRY ]
+ */
+
+#define XEN_PT_ENTRIES_LOG2_LVL_1 13 /* 2**13 entries, maps 2**13 * 512GB = 4PB */
+#define XEN_PT_ENTRIES_LOG2_LVL_2 9  /* 2**9  entries, maps 2**9  * 1GB = 512GB */
+#define XEN_PT_ENTRIES_LOG2_LVL_3 9  /* 2**9  entries, maps 2**9  * 1GB = 512GB */
+#define XEN_PT_ENTRIES_LOG2_LVL_4 5  /* 2**5  entries, maps 2**5  * 64K = 2MB */
+
+#define XEN_PT_SHIFT_LVL_1    (XEN_PT_SHIFT_LVL_2 + XEN_PT_ENTRIES_LOG2_LVL_2)
+#define XEN_PT_SHIFT_LVL_2    (XEN_PT_SHIFT_LVL_3 + XEN_PT_ENTRIES_LOG2_LVL_3)
+#define XEN_PT_SHIFT_LVL_3    (XEN_PT_SHIFT_LVL_4 + XEN_PT_ENTRIES_LOG2_LVL_4)
+#define XEN_PT_SHIFT_LVL_4    PAGE_SHIFT
+
+#define XEN_PT_ENTRIES_LOG2_LVL(lvl) (XEN_PT_ENTRIES_LOG2_LVL_##lvl)

xen/arch/ppc/include/asm/page.h line 84

Quoted 10 lines
xen/arch/ppc/include/asm/page.h new file +10 -0
+#define XEN_PT_SHIFT_LVL(lvl)        (XEN_PT_SHIFT_LVL_##lvl)
+#define XEN_PT_ENTRIES_LVL(lvl)      (1UL << XEN_PT_ENTRIES_LOG2_LVL(lvl))
+#define XEN_PT_MASK_LVL(lvl)         (XEN_PT_ENTRIES_LVL(lvl) - 1)
+#define XEN_PT_INDEX_LVL(lvl, va)    ((va >> XEN_PT_SHIFT_LVL(lvl)) & XEN_PT_MASK_LVL(lvl))
+
+/*
+ * Calculate the index of the provided virtual address in the provided
+ * page table struct
+ */
+#define pt_index(pt, va) _Generic((pt), \

Earlier on I did ask about the minimum compiler version you require for building the PPC hypervisor. Iirc you said about any, but here you're using another feature where I'm not sure how far back it is available. As indicated back then, it would be nice if ./README could gain respective information.

xen/arch/ppc/include/asm/page.h line 90

Quoted 6 lines
xen/arch/ppc/include/asm/page.h new file +6 -0
+    struct lvl1_pd * : XEN_PT_INDEX_LVL(1, (va)), \
+    struct lvl2_pd * : XEN_PT_INDEX_LVL(2, (va)), \
+    struct lvl3_pd * : XEN_PT_INDEX_LVL(3, (va)), \
+    struct lvl4_pt * : XEN_PT_INDEX_LVL(4, (va)))
+
+#define pt_entry(pt, va) (&((pt)->entries[pt_index((pt), (va))]))

As to style: Here (and elsewhere) you put unnecessary parentheses around two of the three uses of the macro arguments. If you think you want to keep it like that, feel free; in general we're recommending to omit ones not really needed, to help readability. Otoh a little higher up in XEN_PT_INDEX_LVL() parentheses are missing around va.

Quoted 29 lines
xen/arch/ppc/include/asm/page.h new file +29 -0
+typedef struct
+{
+    __be64 pde;
+} pde_t;
+
+typedef struct
+{
+    __be64 pte;
+} pte_t;
+
+struct lvl1_pd
+{
+    pde_t entries[1U << XEN_PT_ENTRIES_LOG2_LVL_1];
+};
+
+struct lvl2_pd
+{
+    pde_t entries[1U << XEN_PT_ENTRIES_LOG2_LVL_2];
+};
+
+struct lvl3_pd
+{
+    pde_t entries[1U << XEN_PT_ENTRIES_LOG2_LVL_3];
+};
+
+struct lvl4_pt
+{
+    pte_t entries[1U << XEN_PT_ENTRIES_LOG2_LVL_4];
+};

xen/arch/ppc/include/asm/page.h line 130

Quoted 10 lines
xen/arch/ppc/include/asm/page.h new file +10 -0
+
+static inline pte_t paddr_to_pte(paddr_t paddr, unsigned long flags)
+{
+    return (pte_t){ .pte = cpu_to_be64(paddr | flags | PTE_LEAF) };
+}
+
+static inline pde_t paddr_to_pde(paddr_t paddr, unsigned long flags, unsigned long nls)
+{
+    return (pde_t){ .pde = cpu_to_be64(paddr | flags | nls) };
+}

In these two, are you sure you want to demand all callers to only ever pass in page-aligned addresses?

Quoted 33 lines
xen/arch/ppc/include/asm/page.h new file +33 -0
+static inline paddr_t pte_to_paddr(pte_t pte)
+{
+    return be64_to_cpu(pte.pte) & (PTE_RPN_MASK << PTE_RPN_SHIFT);
+}
+
+static inline paddr_t pde_to_paddr(pde_t pde)
+{
+    return be64_to_cpu(pde.pde) & (PDE_NLB_MASK << PDE_NLB_SHIFT);
+}
+
+static inline bool pte_is_valid(pte_t pte)
+{
+    return pte.pte & be64_to_cpu(PTE_VALID);
+}
+
+static inline bool pde_is_valid(pde_t pde)
+{
+    return pde.pde & be64_to_cpu(PDE_VALID);
+}
+
+/*
+ * ISA 3.0 partition and process table entry format
+ */
+struct patb_entry {
+	__be64 patb0;
+	__be64 patb1;
+};
+#define PATB0_HR PPC_BIT(0) /* host uses radix */
+#define PATB1_GR PPC_BIT(0) /* guest uses radix; must match HR */
+
+struct prtb_entry {
+	__be64 prtb0;
+	__be64 reserved;

xen/arch/ppc/include/asm/page.h line 174

Quoted 10 lines
xen/arch/ppc/include/asm/page.h new file +10 -0
+};
+
+/*
+ * We support 52 bits, hence:
+ * bits 52 - 31 = 21, 0b10101
+ * RTS encoding details
+ * bits 0 - 3 of rts -> bits 6 - 8 unsigned long
+ * bits 4 - 5 of rts -> bits 62 - 63 of unsigned long
+ */
+#define RTS_FIELD ((0x2UL << 61) | (0x5UL << 5))

I'm afraid I can't bring comment and #define together. Bits 0-3 are 4 bits; bits 6-8 are only three. And I'd expect the lower ends of the ranges to appear as shift counts in the expression.

Quoted 10 lines
xen/arch/ppc/include/asm/processor.h +4 -0
@@ -133,6 +133,37 @@ struct cpu_user_regs
     uint32_t entry_vector;
 };
 
+static __inline__ void sync(void)
+{
+    asm volatile ( "sync" );
+}

xen/arch/ppc/include/asm/processor.h struct cpu_user_regs · line 149

Quoted 10 lines
xen/arch/ppc/include/asm/processor.h +10 -0
+
+static __inline__ void isync(void)
+{
+    asm volatile ( "isync" );
+}
+
+static inline unsigned long mfmsr(void)
+{
+    unsigned long msr;
+    asm volatile ( "mfmsr %0" : "=&r"(msr) );

Why the &?

Also nit: Missing blank between closing double quote and opening paren. More instances of this below.

Quoted 6 lines
xen/arch/ppc/include/asm/processor.h +6 -0
+    return msr;
+}
+
+static inline void mtmsrd(unsigned long msr)
+{
+    asm volatile ( "mtmsrd %0" : : "r"(msr) );

xen/arch/ppc/include/asm/processor.h struct cpu_user_regs · line 165

Quoted 10 lines
xen/arch/ppc/include/asm/processor.h +10 -0
+}
+
+#define mtspr(reg, val) asm volatile ( "mtspr %0,%1" : : "K"(reg), "r"(val) )
+
+#define mfspr(reg)                                                             \
+    ({                                                                         \
+        unsigned long val;                                                     \
+        asm volatile ( "mfspr %0,%1" : "=r"(val) : "K"(reg) );                 \
+        val;                                                                   \
+    })

Why #define-s here when higher up you successfully use inline functions?

Also (nit) blanks would again be nice ahead of the commas.

Quoted 8 lines
xen/arch/ppc/include/asm/regs.h new file +5 -0
@@ -0,0 +1,138 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or

xen/arch/ppc/include/asm/regs.h line 15

Quoted 10 lines
xen/arch/ppc/include/asm/regs.h new file +10 -0
+ * (at your option) any later version.
+ *
+ * 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.

You have an SPDX line first thing - why the spelled out license?

xen/arch/ppc/mm-radix.c line 5

Quoted 8 lines
xen/arch/ppc/mm-radix.c new file +5 -0
@@ -0,0 +1,268 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#include <xen/init.h>
+#include <xen/kernel.h>
+#include <xen/types.h>
+#include <xen/lib.h>

Nit: Please sort headers whenever possibly; also ...

xen/arch/ppc/mm-radix.c line 14

Quoted 8 lines
xen/arch/ppc/mm-radix.c new file +8 -0
+#include <asm/bitops.h>
+#include <asm/byteorder.h>
+#include <asm/early_printk.h>
+#include <asm/mm.h>
+#include <asm/page.h>
+#include <asm/processor.h>
+#include <asm/regs.h>
+#include <asm/msr.h>

... this group then.

Quoted 7 lines
xen/arch/ppc/mm-radix.c new file +7 -0
+void enable_mmu(void);
+
+#define INITIAL_LVL1_PD_COUNT      1
+#define INITIAL_LVL2_LVL3_PD_COUNT 2
+#define INITIAL_LVL4_PT_COUNT      256
+
+static size_t initial_lvl1_pd_pool_used;

xen/arch/ppc/mm-radix.c line 32

Quoted 10 lines
xen/arch/ppc/mm-radix.c new file +10 -0
+static struct lvl1_pd __aligned(sizeof(struct lvl1_pd))
+initial_lvl1_pd_pool[INITIAL_LVL1_PD_COUNT];
+
+static size_t initial_lvl2_lvl3_pd_pool_used;
+static struct lvl2_pd __aligned(sizeof(struct lvl2_pd))
+initial_lvl2_lvl3_pd_pool[INITIAL_LVL2_LVL3_PD_COUNT];
+
+static size_t initial_lvl4_pt_pool_used;
+static struct lvl4_pt __aligned(sizeof(struct lvl4_pt))
+initial_lvl4_pt_pool[INITIAL_LVL4_PT_COUNT];

Can instances of these structures usefully exist and be not suitably aligned? If not, having the types rather than the instances already declare the necessary alignment would seem better to me.

xen/arch/ppc/mm-radix.c line 44

Quoted 11 lines
xen/arch/ppc/mm-radix.c new file +11 -0
+/* Only reserve minimum Partition and Process tables  */
+#define PATB_SIZE_LOG2 16 /* Only supported partition table size on POWER9 */
+#define PATB_SIZE      (1UL << PATB_SIZE_LOG2)
+#define PRTB_SIZE_LOG2 12
+#define PRTB_SIZE      (1UL << PRTB_SIZE_LOG2)
+
+static struct patb_entry
+    __aligned(PATB_SIZE) initial_patb[PATB_SIZE / sizeof(struct patb_entry)];
+
+static struct prtb_entry
+    __aligned(PRTB_SIZE) initial_prtb[PRTB_SIZE / sizeof(struct prtb_entry)];

The situation is different here, as array here aren't embedded into the type (being just single entries).

xen/arch/ppc/mm-radix.c line 46

Quoted 1 line
xen/arch/ppc/mm-radix.c new file +1 -0
+_Static_assert(sizeof(initial_patb) == PATB_SIZE);

Is this really useful. And if so, why no similar check for prtb? Furthermore BUILD_BUG_ON() would be better, playing again into what the range of suitable compilers is for this to build.

Quoted 4 lines
xen/arch/ppc/mm-radix.c new file +4 -0
+static void __init setup_initial_mapping(struct lvl1_pd *lvl1,
+                                         vaddr_t map_start,
+                                         vaddr_t map_end,
+                                         paddr_t phys_base)

xen/arch/ppc/mm-radix.c line 140

Quoted 10 lines
xen/arch/ppc/mm-radix.c new file +10 -0
+{
+    uint64_t page_addr;
+
+    if ( map_start & ~PAGE_MASK )
+    {
+        early_printk("Xen _start be aligned to 64k (PAGE_SIZE) boundary\n");
+        die();
+    }
+
+    if ( (uint64_t) phys_base & ~PAGE_MASK )

Why the cast?

Quoted 9 lines
xen/arch/ppc/mm-radix.c new file +9 -0
+    {
+        early_printk("Xen should be loaded at 64k (PAGE_SIZE) boundary\n");
+        die();
+    }
+
+    for ( page_addr = map_start; page_addr < map_end; page_addr += PAGE_SIZE )
+    {
+        struct lvl2_pd *lvl2;
+        struct lvl3_pd *lvl3;

xen/arch/ppc/mm-radix.c line 159

Quoted 10 lines
xen/arch/ppc/mm-radix.c new file +10 -0
+        struct lvl4_pt *lvl4;
+        pde_t *pde;
+        pte_t *pte;
+
+        /* Allocate LVL 2 PD if necessary */
+        pde = pt_entry(lvl1, page_addr);
+        if ( !pde_is_valid(*pde) )
+        {
+            lvl2 = lvl2_pd_pool_alloc();
+            *pde = paddr_to_pde(__pa(lvl2), PDE_VALID, XEN_PT_ENTRIES_LOG2_LVL_2);

paddr_to_pde() doesn't mask off the top bits. Are you relying on lvl2_pd_pool_alloc() using PIC addressing to get at the value it then returns?

Also this and the similar lines below look to be a little too long.

xen/arch/ppc/mm-radix.c line 162

Quoted 3 lines
xen/arch/ppc/mm-radix.c new file +3 -0
+        }
+        else
+            lvl2 = (struct lvl2_pd *) pde_to_paddr(*pde);

This casts a physical address to a pointer; normally only virtual addresses can be used this way. Could there be a helper added to have such a dangerous-looking construct in (ideally) just a single place?

Quoted 17 lines
xen/arch/ppc/mm-radix.c new file +17 -0
+        /* Allocate LVL 3 PD if necessary */
+        pde = pt_entry(lvl2, page_addr);
+        if ( !pde_is_valid(*pde) )
+        {
+            lvl3 = lvl3_pd_pool_alloc();
+            *pde = paddr_to_pde(__pa(lvl3), PDE_VALID, XEN_PT_ENTRIES_LOG2_LVL_3);
+        }
+        else
+            lvl3 = (struct lvl3_pd *) pde_to_paddr(*pde);
+
+        /* Allocate LVL 4 PT if necessary */
+        pde = pt_entry(lvl3, page_addr);
+        if ( !pde_is_valid(*pde) )
+        {
+            lvl4 = lvl4_pt_pool_alloc();
+            *pde = paddr_to_pde(__pa(lvl4), PDE_VALID, XEN_PT_ENTRIES_LOG2_LVL_4);
+        }

xen/arch/ppc/mm-radix.c line 190

Quoted 10 lines
xen/arch/ppc/mm-radix.c new file +10 -0
+        else
+            lvl4 = (struct lvl4_pt *) pde_to_paddr(*pde);
+
+        /* Finally, create PTE in LVL 4 PT */
+        pte = pt_entry(lvl4, page_addr);
+        if ( !pte_is_valid(*pte) )
+        {
+            unsigned long paddr = (page_addr - map_start) + phys_base;
+            unsigned long flags;
+            radix_dprint(paddr, "being mapped to ");

Blank line again please between declarations and statements.

Quoted 20 lines
xen/arch/ppc/mm-radix.c new file +20 -0
+            radix_dprint(page_addr, "!\n");
+            if ( is_kernel_text(page_addr) || is_kernel_inittext(page_addr) )
+            {
+                radix_dprint(page_addr, "being marked as TEXT (RX)\n");
+                flags = PTE_XEN_RX;
+            }
+            else if ( is_kernel_rodata(page_addr) )
+            {
+                radix_dprint(page_addr, "being marked as RODATA (RO)\n");
+                flags = PTE_XEN_RO;
+            }
+            else
+            {
+                radix_dprint(page_addr, "being marked as DEFAULT (RW)\n");
+                flags = PTE_XEN_RW;
+            }
+
+            *pte = paddr_to_pte(paddr, flags);
+            radix_dprint(paddr_to_pte(paddr, flags).pte,
+                             "is result of PTE map!\n");

xen/arch/ppc/mm-radix.c line 220

Quoted 10 lines
xen/arch/ppc/mm-radix.c new file +10 -0
+        }
+        else
+        {
+            early_printk("BUG: Tried to create PTE for already-mapped page!");
+            die();
+        }
+    }
+}
+
+static void setup_partition_table(struct patb_entry *patb, struct lvl1_pd *root)

__init?

Quoted 4 lines
xen/arch/ppc/mm-radix.c new file +4 -0
+{
+    unsigned long ptcr;
+
+    /* Configure entry for LPID 0 to enable Radix and point to root PD */

xen/arch/ppc/mm-radix.c line 234

Quoted 10 lines
xen/arch/ppc/mm-radix.c new file +10 -0
+    uint64_t patb0 = RTS_FIELD | __pa(root) | XEN_PT_ENTRIES_LOG2_LVL_1 | PATB0_HR;
+    uint64_t patb1 = __pa(initial_prtb) | (PRTB_SIZE_LOG2 - 12) | PATB1_GR;
+    patb[0].patb0 = cpu_to_be64(patb0);
+    patb[0].patb1 = cpu_to_be64(patb1);
+
+    ptcr = __pa(initial_patb) | (PATB_SIZE_LOG2 - 12);
+    mtspr(SPRN_PTCR, ptcr);
+}
+
+static void setup_process_table(struct prtb_entry *prtb, struct lvl1_pd *root)

__init?

Quoted 5 lines
xen/arch/ppc/mm-radix.c new file +5 -0
+{
+    /* Configure entry for PID 0 to point to root PD */
+    uint64_t prtb0 = RTS_FIELD | __pa(root) | XEN_PT_ENTRIES_LOG2_LVL_1;
+    initial_prtb[0].prtb0 = cpu_to_be64(prtb0);
+}

xen/arch/ppc/mm-radix.c line 249

Quoted 10 lines
xen/arch/ppc/mm-radix.c new file +10 -0
+
+void __init setup_initial_pagetables(void)
+{
+    struct lvl1_pd *root = lvl1_pd_pool_alloc();
+    unsigned long lpcr;
+
+    setup_initial_mapping(root,
+                         (vaddr_t) _start,
+                         (vaddr_t) _end,
+                          __pa(_start));
    setup_initial_mapping(root,
                          (vaddr_t)_start,
                          (vaddr_t)_end,
                          __pa(_start));

(if flowing across multiple lines is needed / wanted)

xen/arch/ppc/opal.c line 2

Quoted 5 lines
xen/arch/ppc/opal.c +1 -0
@@ -1,8 +1,10 @@
 /* SPDX-License-Identifier: GPL-2.0-or-later */
+#include "xen/compiler.h"

Surely <xen/compiler.h>, but then neither this nor ...

xen/arch/ppc/opal.c line 7

Quoted 5 lines
xen/arch/ppc/opal.c +1 -0
 #include <asm/boot.h>
 #include <asm/early_printk.h>
 #include <asm/opal-api.h>
 #include <asm/processor.h>
+#include <asm/mm.h>

... this addition look motivated when nothing else changes in this file.

Also (I overlooked this in the earlier patch) all xen/ headers generally want to come ahead of all asm/ ones.

xen/arch/ppc/setup.c line 10

Quoted 7 lines
xen/arch/ppc/setup.c +1 -0
@@ -7,6 +7,8 @@
 /* Xen stack for bringing up the first CPU. */
 unsigned char __initdata cpu0_boot_stack[STACK_SIZE] __aligned(STACK_SIZE);
 
+void __init setup_initial_pagetables(void);

This needs to live in a header which is included both here and in the file defining the function.

xen/arch/ppc/setup.c

Quoted 12 lines
xen/arch/ppc/setup.c +12 -0
+static void tlbiel_radix_set_isa300(unsigned int set, unsigned int is,
+                                    unsigned int pid, unsigned int ric,
+                                    unsigned int prs)
+{
+    unsigned long rb;
+    unsigned long rs;
+
+    rb = (set << PPC_BITLSHIFT(51)) | (is << PPC_BITLSHIFT(53));
+    rs = ((unsigned long) pid << PPC_BITLSHIFT(31));
+
+    asm volatile ( "tlbiel %0, %1, %2, %3, 1" :
+                  : "r"(rb), "r"(rs), "i"(ric), "i"(prs) : "memory" );

Nit: One too few indenting blanks (besides others that are missing). Perhaps also better to pull the standalone : onto this second line. Overall perhaps (also for line length)

    asm volatile ( "tlbiel %0, %1, %2, %3, 1"
                   :: "r" (rb), "r" (rs), "i" (ric), "i" (prs)
                   : "memory" );
Quoted 1 line

+void radix__tlbiel_all(unsigned int action)

Is the double underscore in here intentional?

Jan

#6 lore raw

Re: [PATCH 2/3] xen/ppc: Relocate kernel to physical address 0 on boot

In reply to Jan Beulich
Quoted 14 lines · Jan Beulich
Quoted 6 lines · Shawn Anastasio
Quoted 5 lines · Jan Beulich
Quoted 3 lines · Shawn Anastasio
xen/arch/ppc/ppc64/head.S +3 -0
+    /* If we're at the correct address, skip copy */
+    cmpld   %r1, %r12
+    beq     .L_correct_address
Quoted 6 lines
Quoted 3 lines

Can this ever be the case, especially with the MMU-off behavior you describe in the comment above? Wouldn't you need to ignore the top four bits in the comparison?

It will always be the case after the code jumps to XEN_VIRT_START after the copy takes place.

Quoted 7 lines

Well, of course.

Quoted 2 lines

I could have it jump past the copy loop entirely,
but then I'd need to duplicate the TOC setup.

I don't think I understand this part of your reply: .L_correct_address _is_ past the copy loop.

Sorry, let me elaborate. I meant that I could have the end of the copy loop (the mtctr + btctr preceeding .L_correct_address) jump to (XEN_VIRT_START + .L_correct_address) as opposed to XEN_VIRT_START so that the address comparison you originally commented on wouldn't be hit again.

This would mean adding another TOC setup block at .L_correct_address, though, since we'd be skipping over the one at the beginning of the routine and the TOC needs to be reconfigured after the relocation.

Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
Quoted 4 lines
xen/arch/ppc/ppc64/head.S +4 -0
+    /* Copy bytes until _end */
+    LOAD_REG_ADDR(%r11, _end)
+    addi    %r1, %r1, -8
+    li      %r13, -8

xen/arch/ppc/ppc64/head.S ENTRY(start) · line 46

Quoted 10 lines
xen/arch/ppc/ppc64/head.S +10 -0
+.L_copy_xen:
+    ldu     %r10, 8(%r1)
+    stdu    %r10, 8(%r13)
+    cmpld   %r1, %r11
+    blt     .L_copy_xen
+
+    /* Jump to XEN_VIRT_START */
+    mtctr   %r12
+    bctr
+.L_correct_address:

Can the two regions potentially overlap? Looking at the ELF header it's not clear to me what guarantees there are that this can't happen.

Quoted 7 lines
Quoted 3 lines

As I understand it, any bootloader that placed the kernel at a low enough address for this to be an issue wouldn't be able to boot Linux or FreeBSD, so in practice it's a safe bet that this won't be the case.

Fair enough then.

Jan

Thanks,
Shawn

#9 lore raw

Re: [PATCH 3/3] xen/ppc: Implement initial Radix MMU support

In reply to Jan Beulich
Quoted 11 lines · Jan Beulich

xen/arch/ppc/include/asm/bitops.h line 4

Quoted 7 lines · Shawn Anastasio
xen/arch/ppc/include/asm/bitops.h new file +4 -0
@@ -0,0 +1,11 @@
+#ifndef _ASM_PPC_BITOPS_H
+#define _ASM_PPC_BITOPS_H
+
+#include <xen/compiler.h>

Not a big deal, but ...

Quoted 9 lines

xen/arch/ppc/include/asm/bitops.h line 11

Quoted 6 lines
xen/arch/ppc/include/asm/bitops.h new file +6 -0
+/* PPC bit number conversion */
+#define PPC_BITLSHIFT(be)	(BITS_PER_LONG - 1 - (be))
+#define PPC_BIT(bit)		(1UL << PPC_BITLSHIFT(bit))
+#define PPC_BITMASK(bs, be)	((PPC_BIT(bs) - PPC_BIT(be)) | PPC_BIT(bs))
+
+#endif /* _ASM_PPC_BITOPS_H */

... nothing here looks to require that #include.

Good point, I'll drop it.

Quoted 11 lines

xen/arch/ppc/include/asm/mm.h line 4

Quoted 7 lines
xen/arch/ppc/include/asm/mm.h new file +4 -0
@@ -0,0 +1,19 @@
+#ifndef _ASM_PPC_MM_H
+#define _ASM_PPC_MM_H
+
+#include <asm/config.h>

This is included by xen/config.h, which in turn is included from the compiler command line.

Thanks for letting me know. Will drop.

Quoted 33 lines
Quoted 33 lines
xen/arch/ppc/include/asm/page.h new file +30 -0
@@ -0,0 +1,178 @@
+#ifndef _ASM_PPC_PAGE_H
+#define _ASM_PPC_PAGE_H
+
+#include <xen/types.h>
+
+#include <asm/bitops.h>
+#include <asm/byteorder.h>
+
+#define PDE_VALID     PPC_BIT(0)
+#define PDE_NLB_MASK  0xfffffffffffffUL
+#define PDE_NLB_SHIFT 5UL
+#define PDE_NLS_MASK  0x1f
+
+#define PTE_VALID     PPC_BIT(0)
+#define PTE_LEAF      PPC_BIT(1)
+#define PTE_REFERENCE PPC_BIT(55)
+#define PTE_CHANGE    PPC_BIT(56)
+
+/* PTE Attributes */
+#define PTE_ATT_SAO            PPC_BIT(59) /* Strong Access Ordering */
+#define PTE_ATT_NON_IDEMPOTENT PPC_BIT(58)
+#define PTE_ATT_TOLERANT       (PPC_BIT(58) | PPC_BIT(59))
+
+/* PTE Encoded Access Authority*/
+#define PTE_EAA_PRIVILEGED PPC_BIT(60)
+#define PTE_EAA_READ       PPC_BIT(61)
+#define PTE_EAA_WRITE      PPC_BIT(62)
+#define PTE_EAA_EXECUTE    PPC_BIT(63)
+
+/* Field shifts/masks */
Quoted 9 lines

xen/arch/ppc/include/asm/page.h line 35

Quoted 5 lines
xen/arch/ppc/include/asm/page.h new file +5 -0
+#define PTE_RPN_MASK  0x1fffffffffffUL
+#define PTE_RPN_SHIFT 12UL
+#define PTE_ATT_MASK  0x3UL
+#define PTE_ATT_SHIFT 4UL
+#define PTE_EAA_MASK  0xfUL

Did you consider introducing just *_MASK values, utilizing MASK_INSR() and MASK_EXTR() instead of open-coded shifting/masking?

I actually wasn't aware of MASK_INSR/MASK_EXTR when writing this. I've just looked into it though, and I don't think using them makes the code much cleaner. Specifically I'm looking at the implementations of pte_to_paddr and pde_to_paddr which both need to ensure that the returned value retains its original shift.

For example, with pte_to_paddr, this change would be:
- return be64_to_cpu(pte.pte) & (PTE_RPN_MASK << PTE_RPN_SHIFT);
+ return MASK_EXTR(be64_to_cpu(pte.pte), PTE_RPN_MASK) << PTE_RPN_SHIFT;

In addition to updating the definitions of the *_MASK macros to include the right-most padding zeros.

- #define PTE_RPN_MASK 0x1fffffffffffUL
+ #define PTE_RPN_MASK 0x1fffffffffff000UL

I don't really think this is an improvement, so I'm tempted to keep the code as-is. If you feel strongly about the usage of MASK_{INSR,EXTR}, though, I could make the change.

Quoted 46 lines
Quoted 46 lines
xen/arch/ppc/include/asm/page.h new file +46 -0
+#define PTE_XEN_BASE (PTE_VALID | PTE_EAA_PRIVILEGED | PTE_REFERENCE)
+#define PTE_XEN_RW   (PTE_XEN_BASE | PTE_EAA_READ | PTE_EAA_WRITE | PTE_CHANGE)
+#define PTE_XEN_RO   (PTE_XEN_BASE | PTE_EAA_READ)
+#define PTE_XEN_RX   (PTE_XEN_BASE | PTE_EAA_READ | PTE_EAA_EXECUTE)
+
+/*
+ * Radix Tree layout for 64KB pages:
+ *
+ * [ L1 (ROOT) PAGE DIRECTORY (8192 * sizeof(pde_t)) ]
+ *                     |
+ *                     |
+ *                     v
+ *    [ L2 PAGE DIRECTORY (512 * sizeof(pde_t)) ]
+ *                     |
+ *                     |
+ *                     v
+ *    [ L3 PAGE DIRECTORY (512 * sizeof(pde_t)) ]
+ *                     |
+ *                     |
+ *                     v
+ *      [ L4 PAGE TABLE (32 * sizeof(pte_t)) ]
+ *                     |
+ *                     |
+ *                     v
+ *            [ PAGE TABLE ENTRY ]
+ */
+
+#define XEN_PT_ENTRIES_LOG2_LVL_1 13 /* 2**13 entries, maps 2**13 * 512GB = 4PB */
+#define XEN_PT_ENTRIES_LOG2_LVL_2 9  /* 2**9  entries, maps 2**9  * 1GB = 512GB */
+#define XEN_PT_ENTRIES_LOG2_LVL_3 9  /* 2**9  entries, maps 2**9  * 1GB = 512GB */
+#define XEN_PT_ENTRIES_LOG2_LVL_4 5  /* 2**5  entries, maps 2**5  * 64K = 2MB */
+
+#define XEN_PT_SHIFT_LVL_1    (XEN_PT_SHIFT_LVL_2 + XEN_PT_ENTRIES_LOG2_LVL_2)
+#define XEN_PT_SHIFT_LVL_2    (XEN_PT_SHIFT_LVL_3 + XEN_PT_ENTRIES_LOG2_LVL_3)
+#define XEN_PT_SHIFT_LVL_3    (XEN_PT_SHIFT_LVL_4 + XEN_PT_ENTRIES_LOG2_LVL_4)
+#define XEN_PT_SHIFT_LVL_4    PAGE_SHIFT
+
+#define XEN_PT_ENTRIES_LOG2_LVL(lvl) (XEN_PT_ENTRIES_LOG2_LVL_##lvl)
+#define XEN_PT_SHIFT_LVL(lvl)        (XEN_PT_SHIFT_LVL_##lvl)
+#define XEN_PT_ENTRIES_LVL(lvl)      (1UL << XEN_PT_ENTRIES_LOG2_LVL(lvl))
+#define XEN_PT_MASK_LVL(lvl)         (XEN_PT_ENTRIES_LVL(lvl) - 1)
+#define XEN_PT_INDEX_LVL(lvl, va)    ((va >> XEN_PT_SHIFT_LVL(lvl)) & XEN_PT_MASK_LVL(lvl))
+
+/*
+ * Calculate the index of the provided virtual address in the provided
+ * page table struct
Quoted 9 lines

xen/arch/ppc/include/asm/page.h line 83

Quoted 2 lines
xen/arch/ppc/include/asm/page.h new file +2 -0
+ */
+#define pt_index(pt, va) _Generic((pt), \

Earlier on I did ask about the minimum compiler version you require for building the PPC hypervisor. Iirc you said about any, but here you're using another feature where I'm not sure how far back it is available. As indicated back then, it would be nice if ./README could gain respective information.

I'm locally building with gcc 10 (shipped with Debian 11), and the Xen CI image for ppc64le builds uses gcc 11 I believe. _Generic and the other features I'm using are certainly available further back, but I haven't personally tested anything earlier. If there's no objections, I'm tempted to just codify gcc 10 as the minimum supported compiler version and leave it up to users if they want to try testing on earlier versions.

Quoted 4 lines
Quoted 4 lines
xen/arch/ppc/include/asm/page.h new file +4 -0
+    struct lvl1_pd * : XEN_PT_INDEX_LVL(1, (va)), \
+    struct lvl2_pd * : XEN_PT_INDEX_LVL(2, (va)), \
+    struct lvl3_pd * : XEN_PT_INDEX_LVL(3, (va)), \
+    struct lvl4_pt * : XEN_PT_INDEX_LVL(4, (va)))
Quoted 9 lines

xen/arch/ppc/include/asm/page.h line 89

Quoted 2 lines
xen/arch/ppc/include/asm/page.h new file +2 -0
+
+#define pt_entry(pt, va) (&((pt)->entries[pt_index((pt), (va))]))

As to style: Here (and elsewhere) you put unnecessary parentheses around two of the three uses of the macro arguments. If you think you want to keep it like that, feel free; in general we're recommending to omit ones not really needed, to help readability. Otoh a little higher up in XEN_PT_INDEX_LVL() parentheses are missing around va.

If possible I'd prefer to just leave it as-is. Unconditionally parenthesizing macro arguments seems less error-prone as a general policy, though I agree the latter two here are unnecessary.

That said, the lack of parenthesis around va in XEN_PT_INDEX_LEVEL was definitely an oversight and I'll fix that.

Quoted 34 lines
Quoted 34 lines
xen/arch/ppc/include/asm/page.h new file +34 -0
+typedef struct
+{
+    __be64 pde;
+} pde_t;
+
+typedef struct
+{
+    __be64 pte;
+} pte_t;
+
+struct lvl1_pd
+{
+    pde_t entries[1U << XEN_PT_ENTRIES_LOG2_LVL_1];
+};
+
+struct lvl2_pd
+{
+    pde_t entries[1U << XEN_PT_ENTRIES_LOG2_LVL_2];
+};
+
+struct lvl3_pd
+{
+    pde_t entries[1U << XEN_PT_ENTRIES_LOG2_LVL_3];
+};
+
+struct lvl4_pt
+{
+    pte_t entries[1U << XEN_PT_ENTRIES_LOG2_LVL_4];
+};
+
+static inline pte_t paddr_to_pte(paddr_t paddr, unsigned long flags)
+{
+    return (pte_t){ .pte = cpu_to_be64(paddr | flags | PTE_LEAF) };
+}
Quoted 9 lines

xen/arch/ppc/include/asm/page.h line 130

Quoted 5 lines
xen/arch/ppc/include/asm/page.h new file +5 -0
+
+static inline pde_t paddr_to_pde(paddr_t paddr, unsigned long flags, unsigned long nls)
+{
+    return (pde_t){ .pde = cpu_to_be64(paddr | flags | nls) };
+}

In these two, are you sure you want to demand all callers to only ever pass in page-aligned addresses?

Fair point. Perhaps it would be best to mask the caller-provided address.

Quoted 39 lines
Quoted 39 lines
xen/arch/ppc/include/asm/page.h new file +39 -0
+static inline paddr_t pte_to_paddr(pte_t pte)
+{
+    return be64_to_cpu(pte.pte) & (PTE_RPN_MASK << PTE_RPN_SHIFT);
+}
+
+static inline paddr_t pde_to_paddr(pde_t pde)
+{
+    return be64_to_cpu(pde.pde) & (PDE_NLB_MASK << PDE_NLB_SHIFT);
+}
+
+static inline bool pte_is_valid(pte_t pte)
+{
+    return pte.pte & be64_to_cpu(PTE_VALID);
+}
+
+static inline bool pde_is_valid(pde_t pde)
+{
+    return pde.pde & be64_to_cpu(PDE_VALID);
+}
+
+/*
+ * ISA 3.0 partition and process table entry format
+ */
+struct patb_entry {
+	__be64 patb0;
+	__be64 patb1;
+};
+#define PATB0_HR PPC_BIT(0) /* host uses radix */
+#define PATB1_GR PPC_BIT(0) /* guest uses radix; must match HR */
+
+struct prtb_entry {
+	__be64 prtb0;
+	__be64 reserved;
+};
+
+/*
+ * We support 52 bits, hence:
+ * bits 52 - 31 = 21, 0b10101
+ * RTS encoding details
Quoted 9 lines

xen/arch/ppc/include/asm/page.h line 174

Quoted 4 lines
xen/arch/ppc/include/asm/page.h new file +4 -0
+ * bits 0 - 3 of rts -> bits 6 - 8 unsigned long
+ * bits 4 - 5 of rts -> bits 62 - 63 of unsigned long
+ */
+#define RTS_FIELD ((0x2UL << 61) | (0x5UL << 5))

I'm afraid I can't bring comment and #define together. Bits 0-3 are 4 bits; bits 6-8 are only three. And I'd expect the lower ends of the ranges to appear as shift counts in the expression.

Yes, I agree that this is confusing. For reference, this comment was verbatim copied from a comment in Linux[*] and I admittedly didn't pay as much attention to it as I did to the expression itself.

As far as I can tell, the original author's intent was to use 1-indexed non-PPC-style bit numbering and wrote 0-3 instead of 1-3 as a mistake. Under this interpretation, the comment makes more sense as do the shift counts in the expression being off by one.

I'll rewrite it to correct the mistake and use standard 0-indexing for bits. Something like:

/*
* We support 52 bits, hence:
* bits 52 - 31 = 21, 0b10101
* RTS encoding details
* bits 0 - 2 of rts -> bits 5 - 7 of unsigned long
* bits 3 - 4 of rts -> bits 61 - 62 of unsigned long */

[*] arch/powerpc/include/asm/book3s/64/radix.h:radix__get_tree_size

Quoted 6 lines
Quoted 6 lines
xen/arch/ppc/include/asm/processor.h +0 -0
@@ -133,6 +133,37 @@ struct cpu_user_regs
     uint32_t entry_vector;
 };
 
Quoted 16 lines
Quoted 4 lines
xen/arch/ppc/include/asm/processor.h +4 -0
+static __inline__ void sync(void)
+{
+    asm volatile ( "sync" );
+}

xen/arch/ppc/include/asm/processor.h struct cpu_user_regs · line 149

Quoted 10 lines
xen/arch/ppc/include/asm/processor.h +10 -0
+
+static __inline__ void isync(void)
+{
+    asm volatile ( "isync" );
+}
+
+static inline unsigned long mfmsr(void)
+{
+    unsigned long msr;
+    asm volatile ( "mfmsr %0" : "=&r"(msr) );

Why the &?

This was in the original Xen 3.2 header that this function was taken from, but I agree that it seems strange. I'll drop it in v2.

Quoted 3 lines

Also nit: Missing blank between closing double quote and opening paren. More instances of this below.

Will fix.

Quoted 10 lines
Quoted 10 lines
xen/arch/ppc/include/asm/processor.h +10 -0
+    return msr;
+}
+
+static inline void mtmsrd(unsigned long msr)
+{
+    asm volatile ( "mtmsrd %0" : : "r"(msr) );
+}
+
+#define mtspr(reg, val) asm volatile ( "mtspr %0,%1" : : "K"(reg), "r"(val) )
+
Quoted 9 lines

xen/arch/ppc/include/asm/processor.h struct cpu_user_regs · line 165

Quoted 6 lines
xen/arch/ppc/include/asm/processor.h +6 -0
+#define mfspr(reg)                                                             \
+    ({                                                                         \
+        unsigned long val;                                                     \
+        asm volatile ( "mfspr %0,%1" : "=r"(val) : "K"(reg) );                 \
+        val;                                                                   \
+    })

Why #define-s here when higher up you successfully use inline functions?

This was a temporary thing that I meant to convert into a full function before committing but seem to have overlooked. Thanks for pointing it out.

Quoted 2 lines

Also (nit) blanks would again be nice ahead of the commas.

Will do.

Quoted 12 lines
Quoted 12 lines
xen/arch/ppc/include/asm/regs.h new file +9 -0
@@ -0,0 +1,138 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
Quoted 9 lines

xen/arch/ppc/include/asm/regs.h line 15

Quoted 6 lines
xen/arch/ppc/include/asm/regs.h new file +6 -0
+ * 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.

You have an SPDX line first thing - why the spelled out license?

Leftover from importing the header from Xen 3.2. Will drop.

Quoted 11 lines

xen/arch/ppc/mm-radix.c line 5

Quoted 8 lines
xen/arch/ppc/mm-radix.c new file +5 -0
@@ -0,0 +1,268 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#include <xen/init.h>
+#include <xen/kernel.h>
+#include <xen/types.h>
+#include <xen/lib.h>

Nit: Please sort headers whenever possibly; also ...

Quoted 11 lines

xen/arch/ppc/mm-radix.c line 14

Quoted 8 lines
xen/arch/ppc/mm-radix.c new file +8 -0
+#include <asm/bitops.h>
+#include <asm/byteorder.h>
+#include <asm/early_printk.h>
+#include <asm/mm.h>
+#include <asm/page.h>
+#include <asm/processor.h>
+#include <asm/regs.h>
+#include <asm/msr.h>

... this group then.

I'll alphabetically sort both groups.

Quoted 13 lines
Quoted 13 lines
xen/arch/ppc/mm-radix.c new file +13 -0
+void enable_mmu(void);
+
+#define INITIAL_LVL1_PD_COUNT      1
+#define INITIAL_LVL2_LVL3_PD_COUNT 2
+#define INITIAL_LVL4_PT_COUNT      256
+
+static size_t initial_lvl1_pd_pool_used;
+static struct lvl1_pd __aligned(sizeof(struct lvl1_pd))
+initial_lvl1_pd_pool[INITIAL_LVL1_PD_COUNT];
+
+static size_t initial_lvl2_lvl3_pd_pool_used;
+static struct lvl2_pd __aligned(sizeof(struct lvl2_pd))
+initial_lvl2_lvl3_pd_pool[INITIAL_LVL2_LVL3_PD_COUNT];
Quoted 9 lines

xen/arch/ppc/mm-radix.c line 32

Quoted 4 lines
xen/arch/ppc/mm-radix.c new file +4 -0
+
+static size_t initial_lvl4_pt_pool_used;
+static struct lvl4_pt __aligned(sizeof(struct lvl4_pt))
+initial_lvl4_pt_pool[INITIAL_LVL4_PT_COUNT];

Can instances of these structures usefully exist and be not suitably aligned? If not, having the types rather than the instances already declare the necessary alignment would seem better to me.

I can't think of a scenario in which we'd want instances of these structures without alignment. I'll go ahead and apply the alignment to the struct definitions instead.

Quoted 6 lines
Quoted 6 lines
xen/arch/ppc/mm-radix.c new file +6 -0
+/* Only reserve minimum Partition and Process tables  */
+#define PATB_SIZE_LOG2 16 /* Only supported partition table size on POWER9 */
+#define PATB_SIZE      (1UL << PATB_SIZE_LOG2)
+#define PRTB_SIZE_LOG2 12
+#define PRTB_SIZE      (1UL << PRTB_SIZE_LOG2)
+
Quoted 9 lines

xen/arch/ppc/mm-radix.c line 44

Quoted 5 lines
xen/arch/ppc/mm-radix.c new file +5 -0
+static struct patb_entry
+    __aligned(PATB_SIZE) initial_patb[PATB_SIZE / sizeof(struct patb_entry)];
+
+static struct prtb_entry
+    __aligned(PRTB_SIZE) initial_prtb[PRTB_SIZE / sizeof(struct prtb_entry)];

The situation is different here, as array here aren't embedded into the type (being just single entries).

Right.

Quoted 6 lines

xen/arch/ppc/mm-radix.c line 46

Quoted 1 line
xen/arch/ppc/mm-radix.c new file +1 -0
+_Static_assert(sizeof(initial_patb) == PATB_SIZE);

Is this really useful. And if so, why no similar check for prtb? Furthermore BUILD_BUG_ON() would be better, playing again into what the range of suitable compilers is for this to build.

This was a debug statement that I had inadvertently left in. I'll remove it.

Quoted 8 lines
Quoted 8 lines
xen/arch/ppc/mm-radix.c new file +8 -0
+static void __init setup_initial_mapping(struct lvl1_pd *lvl1,
+                                         vaddr_t map_start,
+                                         vaddr_t map_end,
+                                         paddr_t phys_base)
+{
+    uint64_t page_addr;
+
+    if ( map_start & ~PAGE_MASK )
Quoted 9 lines

xen/arch/ppc/mm-radix.c line 140

Quoted 6 lines
xen/arch/ppc/mm-radix.c new file +6 -0
+    {
+        early_printk("Xen _start be aligned to 64k (PAGE_SIZE) boundary\n");
+        die();
+    }
+
+    if ( (uint64_t) phys_base & ~PAGE_MASK )

Why the cast?

I believe in an earlier revision of this code I had phys_base's type as void* or similar, so this is just a leftover that I forgot to clean up. I'll remove the cast.

Quoted 15 lines
Quoted 15 lines
xen/arch/ppc/mm-radix.c new file +15 -0
+    {
+        early_printk("Xen should be loaded at 64k (PAGE_SIZE) boundary\n");
+        die();
+    }
+
+    for ( page_addr = map_start; page_addr < map_end; page_addr += PAGE_SIZE )
+    {
+        struct lvl2_pd *lvl2;
+        struct lvl3_pd *lvl3;
+        struct lvl4_pt *lvl4;
+        pde_t *pde;
+        pte_t *pte;
+
+        /* Allocate LVL 2 PD if necessary */
+        pde = pt_entry(lvl1, page_addr);
Quoted 9 lines

xen/arch/ppc/mm-radix.c line 159

Quoted 4 lines
xen/arch/ppc/mm-radix.c new file +4 -0
+        if ( !pde_is_valid(*pde) )
+        {
+            lvl2 = lvl2_pd_pool_alloc();
+            *pde = paddr_to_pde(__pa(lvl2), PDE_VALID, XEN_PT_ENTRIES_LOG2_LVL_2);

paddr_to_pde() doesn't mask off the top bits. Are you relying on lvl2_pd_pool_alloc() using PIC addressing to get at the value it then returns?

I'm not sure I understand the question here. The pointer returned by lvl2_pd_pool_alloc() will indeed have the top address bits set in accordance with the link address, even before paging is enabled because of the relocation code and jump to XEN_VIRT_START in patch 2. This is fine though, since I call __pa() to strip off these bits before passing it to paddr_to_pde.

Quoted 2 lines

Also this and the similar lines below look to be a little too long.

Yeah, I noticed this as well, but my understanding was that lines longer than 80 columns were permitted in cases where they don't hurt readability. In any case, I can fix this.

Quoted 9 lines

xen/arch/ppc/mm-radix.c line 162

Quoted 3 lines
xen/arch/ppc/mm-radix.c new file +3 -0
+        }
+        else
+            lvl2 = (struct lvl2_pd *) pde_to_paddr(*pde);

This casts a physical address to a pointer; normally only virtual addresses can be used this way. Could there be a helper added to have such a dangerous-looking construct in (ideally) just a single place?

Fair point. We already have __va() which will work due to the previously mentioned relocation code in patch 2 even before the MMU is enabled. I think it might make sense to use that.

Quoted 21 lines
Quoted 21 lines
xen/arch/ppc/mm-radix.c new file +21 -0
+        /* Allocate LVL 3 PD if necessary */
+        pde = pt_entry(lvl2, page_addr);
+        if ( !pde_is_valid(*pde) )
+        {
+            lvl3 = lvl3_pd_pool_alloc();
+            *pde = paddr_to_pde(__pa(lvl3), PDE_VALID, XEN_PT_ENTRIES_LOG2_LVL_3);
+        }
+        else
+            lvl3 = (struct lvl3_pd *) pde_to_paddr(*pde);
+
+        /* Allocate LVL 4 PT if necessary */
+        pde = pt_entry(lvl3, page_addr);
+        if ( !pde_is_valid(*pde) )
+        {
+            lvl4 = lvl4_pt_pool_alloc();
+            *pde = paddr_to_pde(__pa(lvl4), PDE_VALID, XEN_PT_ENTRIES_LOG2_LVL_4);
+        }
+        else
+            lvl4 = (struct lvl4_pt *) pde_to_paddr(*pde);
+
+        /* Finally, create PTE in LVL 4 PT */
Quoted 9 lines

xen/arch/ppc/mm-radix.c line 190

Quoted 6 lines
xen/arch/ppc/mm-radix.c new file +6 -0
+        pte = pt_entry(lvl4, page_addr);
+        if ( !pte_is_valid(*pte) )
+        {
+            unsigned long paddr = (page_addr - map_start) + phys_base;
+            unsigned long flags;
+            radix_dprint(paddr, "being mapped to ");

Blank line again please between declarations and statements.

Will fix.

Quoted 24 lines
Quoted 24 lines
xen/arch/ppc/mm-radix.c new file +24 -0
+            radix_dprint(page_addr, "!\n");
+            if ( is_kernel_text(page_addr) || is_kernel_inittext(page_addr) )
+            {
+                radix_dprint(page_addr, "being marked as TEXT (RX)\n");
+                flags = PTE_XEN_RX;
+            }
+            else if ( is_kernel_rodata(page_addr) )
+            {
+                radix_dprint(page_addr, "being marked as RODATA (RO)\n");
+                flags = PTE_XEN_RO;
+            }
+            else
+            {
+                radix_dprint(page_addr, "being marked as DEFAULT (RW)\n");
+                flags = PTE_XEN_RW;
+            }
+
+            *pte = paddr_to_pte(paddr, flags);
+            radix_dprint(paddr_to_pte(paddr, flags).pte,
+                             "is result of PTE map!\n");
+        }
+        else
+        {
+            early_printk("BUG: Tried to create PTE for already-mapped page!");
Quoted 9 lines

xen/arch/ppc/mm-radix.c line 220

Quoted 6 lines
xen/arch/ppc/mm-radix.c new file +6 -0
+            die();
+        }
+    }
+}
+
+static void setup_partition_table(struct patb_entry *patb, struct lvl1_pd *root)

__init?

Yes, good call. Will add.

Quoted 8 lines
Quoted 8 lines
xen/arch/ppc/mm-radix.c new file +8 -0
+{
+    unsigned long ptcr;
+
+    /* Configure entry for LPID 0 to enable Radix and point to root PD */
+    uint64_t patb0 = RTS_FIELD | __pa(root) | XEN_PT_ENTRIES_LOG2_LVL_1 | PATB0_HR;
+    uint64_t patb1 = __pa(initial_prtb) | (PRTB_SIZE_LOG2 - 12) | PATB1_GR;
+    patb[0].patb0 = cpu_to_be64(patb0);
+    patb[0].patb1 = cpu_to_be64(patb1);
Quoted 9 lines

xen/arch/ppc/mm-radix.c line 234

Quoted 6 lines
xen/arch/ppc/mm-radix.c new file +6 -0
+
+    ptcr = __pa(initial_patb) | (PATB_SIZE_LOG2 - 12);
+    mtspr(SPRN_PTCR, ptcr);
+}
+
+static void setup_process_table(struct prtb_entry *prtb, struct lvl1_pd *root)

__init?

Ditto.

Quoted 21 lines
Quoted 5 lines
xen/arch/ppc/mm-radix.c new file +5 -0
+{
+    /* Configure entry for PID 0 to point to root PD */
+    uint64_t prtb0 = RTS_FIELD | __pa(root) | XEN_PT_ENTRIES_LOG2_LVL_1;
+    initial_prtb[0].prtb0 = cpu_to_be64(prtb0);
+}

xen/arch/ppc/mm-radix.c line 249

Quoted 10 lines
xen/arch/ppc/mm-radix.c new file +10 -0
+
+void __init setup_initial_pagetables(void)
+{
+    struct lvl1_pd *root = lvl1_pd_pool_alloc();
+    unsigned long lpcr;
+
+    setup_initial_mapping(root,
+                         (vaddr_t) _start,
+                         (vaddr_t) _end,
+                          __pa(_start));
    setup_initial_mapping(root,
                          (vaddr_t)_start,
                          (vaddr_t)_end,
                          __pa(_start));
Quoted 2 lines

(if flowing across multiple lines is needed / wanted)

I just checked and the line without flowing fits within exactly 80 columns, so I'll make that change.

Quoted 8 lines

xen/arch/ppc/opal.c line 2

Quoted 5 lines
xen/arch/ppc/opal.c +1 -0
@@ -1,8 +1,10 @@
 /* SPDX-License-Identifier: GPL-2.0-or-later */
+#include "xen/compiler.h"

Surely <xen/compiler.h>, but then neither this nor ...

This was accidentally added by my editor/clangd and was not my intention. I'll remove all change to this file in this patch.

Quoted 8 lines

xen/arch/ppc/opal.c line 7

Quoted 5 lines
xen/arch/ppc/opal.c +1 -0
 #include <asm/boot.h>
 #include <asm/early_printk.h>
 #include <asm/opal-api.h>
 #include <asm/processor.h>
+#include <asm/mm.h>

... this addition look motivated when nothing else changes in this file.

See above.

Quoted 3 lines

Also (I overlooked this in the earlier patch) all xen/ headers generally want to come ahead of all asm/ ones.

I'll keep that in mind going forward and for the next time I have a valid reason to touch this file.

Quoted 11 lines

xen/arch/ppc/setup.c line 10

Quoted 7 lines
xen/arch/ppc/setup.c +1 -0
@@ -7,6 +7,8 @@
 /* Xen stack for bringing up the first CPU. */
 unsigned char __initdata cpu0_boot_stack[STACK_SIZE] __aligned(STACK_SIZE);
 
+void __init setup_initial_pagetables(void);

This needs to live in a header which is included both here and in the file defining the function.

I see I already declared the function's prototype in mm.h. This was probably an oversight from before I added that. I'll drop the prototype from setup.c and include asm/mm.h instead.

Quoted 17 lines

xen/arch/ppc/setup.c

Quoted 12 lines
xen/arch/ppc/setup.c +12 -0
+static void tlbiel_radix_set_isa300(unsigned int set, unsigned int is,
+                                    unsigned int pid, unsigned int ric,
+                                    unsigned int prs)
+{
+    unsigned long rb;
+    unsigned long rs;
+
+    rb = (set << PPC_BITLSHIFT(51)) | (is << PPC_BITLSHIFT(53));
+    rs = ((unsigned long) pid << PPC_BITLSHIFT(31));
+
+    asm volatile ( "tlbiel %0, %1, %2, %3, 1" :
+                  : "r"(rb), "r"(rs), "i"(ric), "i"(prs) : "memory" );

Nit: One too few indenting blanks (besides others that are missing). Perhaps also better to pull the standalone : onto this second line. Overall perhaps (also for line length)

Quoted 4 lines
    asm volatile ( "tlbiel %0, %1, %2, %3, 1"
                   :: "r" (rb), "r" (rs), "i" (ric), "i" (prs)
                   : "memory" );

Will do.

Quoted 4 lines
Quoted 1 line

+void radix__tlbiel_all(unsigned int action)

Is the double underscore in here intentional?

It matches the original Linux file from which this routine was imported (referenced in the file's top-level comment).

As I understand it, this is meant to indicate a private function that shouldn't be called outside of radix-specific MMU or TLB code. As Linux's radix support code spans multiple files, a static function wouldn't work for that.

Quoted 1 line

Jan

Thanks,
Shawn

#10 lore raw

Re: [PATCH 3/3] xen/ppc: Implement initial Radix MMU support

Quoted 55 lines · Shawn Anastasio
Quoted 34 lines · Jan Beulich
Quoted 33 lines · Shawn Anastasio
xen/arch/ppc/include/asm/page.h new file +30 -0
@@ -0,0 +1,178 @@
+#ifndef _ASM_PPC_PAGE_H
+#define _ASM_PPC_PAGE_H
+
+#include <xen/types.h>
+
+#include <asm/bitops.h>
+#include <asm/byteorder.h>
+
+#define PDE_VALID     PPC_BIT(0)
+#define PDE_NLB_MASK  0xfffffffffffffUL
+#define PDE_NLB_SHIFT 5UL
+#define PDE_NLS_MASK  0x1f
+
+#define PTE_VALID     PPC_BIT(0)
+#define PTE_LEAF      PPC_BIT(1)
+#define PTE_REFERENCE PPC_BIT(55)
+#define PTE_CHANGE    PPC_BIT(56)
+
+/* PTE Attributes */
+#define PTE_ATT_SAO            PPC_BIT(59) /* Strong Access Ordering */
+#define PTE_ATT_NON_IDEMPOTENT PPC_BIT(58)
+#define PTE_ATT_TOLERANT       (PPC_BIT(58) | PPC_BIT(59))
+
+/* PTE Encoded Access Authority*/
+#define PTE_EAA_PRIVILEGED PPC_BIT(60)
+#define PTE_EAA_READ       PPC_BIT(61)
+#define PTE_EAA_WRITE      PPC_BIT(62)
+#define PTE_EAA_EXECUTE    PPC_BIT(63)
+
+/* Field shifts/masks */
Quoted 9 lines

xen/arch/ppc/include/asm/page.h line 35

Quoted 5 lines
xen/arch/ppc/include/asm/page.h new file +5 -0
+#define PTE_RPN_MASK  0x1fffffffffffUL
+#define PTE_RPN_SHIFT 12UL
+#define PTE_ATT_MASK  0x3UL
+#define PTE_ATT_SHIFT 4UL
+#define PTE_EAA_MASK  0xfUL

Did you consider introducing just *_MASK values, utilizing MASK_INSR() and MASK_EXTR() instead of open-coded shifting/masking?

I actually wasn't aware of MASK_INSR/MASK_EXTR when writing this. I've just looked into it though, and I don't think using them makes the code much cleaner. Specifically I'm looking at the implementations of pte_to_paddr and pde_to_paddr which both need to ensure that the returned value retains its original shift.

For example, with pte_to_paddr, this change would be:

(quoted diff) +1 -1
-     return be64_to_cpu(pte.pte) & (PTE_RPN_MASK << PTE_RPN_SHIFT);
+     return MASK_EXTR(be64_to_cpu(pte.pte), PTE_RPN_MASK) << PTE_RPN_SHIFT;
Quoted 5 lines

In addition to updating the definitions of the *_MASK macros to include the right-most padding zeros.

- #define PTE_RPN_MASK 0x1fffffffffffUL
+ #define PTE_RPN_MASK 0x1fffffffffff000UL

This is the important change. With that the above will be

     return be64_to_cpu(pte.pte) & PTE_RPN_MASK;

and in cases where you want the un-shifted value you'd use MASK_EXTR().

Quoted 13 lines
Quoted 12 lines

xen/arch/ppc/include/asm/page.h line 84

Quoted 5 lines
xen/arch/ppc/include/asm/page.h new file +5 -0
+/*
+ * Calculate the index of the provided virtual address in the provided
+ * page table struct
+ */
+#define pt_index(pt, va) _Generic((pt), \

Earlier on I did ask about the minimum compiler version you require for building the PPC hypervisor. Iirc you said about any, but here you're using another feature where I'm not sure how far back it is available. As indicated back then, it would be nice if ./README could gain respective information.

Quoted 7 lines

I'm locally building with gcc 10 (shipped with Debian 11), and the Xen CI image for ppc64le builds uses gcc 11 I believe. _Generic and the other features I'm using are certainly available further back, but I haven't personally tested anything earlier. If there's no objections, I'm tempted to just codify gcc 10 as the minimum supported compiler version and leave it up to users if they want to try testing on earlier versions.

Spelling out what you know works is good enough for starters. As you say, people can propose updating when found too high.

Quoted 20 lines
Quoted 15 lines
Quoted 4 lines
xen/arch/ppc/include/asm/page.h new file +4 -0
+    for ( page_addr = map_start; page_addr < map_end; page_addr += PAGE_SIZE )
+    {
+        struct lvl2_pd *lvl2;
+        struct lvl3_pd *lvl3;

xen/arch/ppc/include/asm/page.h line 54

Quoted 10 lines
xen/arch/ppc/include/asm/page.h new file +10 -0
+        struct lvl4_pt *lvl4;
+        pde_t *pde;
+        pte_t *pte;
+
+        /* Allocate LVL 2 PD if necessary */
+        pde = pt_entry(lvl1, page_addr);
+        if ( !pde_is_valid(*pde) )
+        {
+            lvl2 = lvl2_pd_pool_alloc();
+            *pde = paddr_to_pde(__pa(lvl2), PDE_VALID, XEN_PT_ENTRIES_LOG2_LVL_2);
Quoted 4 lines

paddr_to_pde() doesn't mask off the top bits. Are you relying on lvl2_pd_pool_alloc() using PIC addressing to get at the value it then returns?

Quoted 6 lines

I'm not sure I understand the question here. The pointer returned by lvl2_pd_pool_alloc() will indeed have the top address bits set in accordance with the link address, even before paging is enabled because of the relocation code and jump to XEN_VIRT_START in patch 2. This is fine though, since I call __pa() to strip off these bits before passing it to paddr_to_pde.

I'm sorry, I managed to overlook the __pa().

Quoted 5 lines
Quoted 1 line

Also this and the similar lines below look to be a little too long.

Yeah, I noticed this as well, but my understanding was that lines longer than 80 columns were permitted in cases where they don't hurt readability. In any case, I can fix this.

There's only one general exception to line limit: printk() (and alike) format strings want to live all on one line (unless there are embedded \n), to allow reasonably grep-ing for them.

Quoted 5 lines
Quoted 4 lines

xen/arch/ppc/include/asm/page.h line 55

Quoted 1 line
xen/arch/ppc/include/asm/page.h new file +1 -0
+void radix__tlbiel_all(unsigned int action)

Is the double underscore in here intentional?

Quoted 7 lines

It matches the original Linux file from which this routine was imported (referenced in the file's top-level comment).

As I understand it, this is meant to indicate a private function that shouldn't be called outside of radix-specific MMU or TLB code. As Linux's radix support code spans multiple files, a static function wouldn't work for that.

We haven't adopted that style yet(?), but I'm also not opposed to you doing locally.

Jan