Skip to contentSkip to conversation

xen/arm32: Improve logging during early boot

PATCH Superseded Julien Grall 3 participants 10 messages Nov 1, 2023 – Nov 2, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover xen/arm32: Improve logging during early boot

From: Julien Grall <[email protected]>

Hi all,

This small series is based on some debugging I added while investigating f5a49eb7f8b3 ("xen/arm32: head: Add mising isb in switch_to_runtime_mapping()").

This will make easier to narrow down where the CPU is stuck while enabling the MMU.

Julien Grall (2):
xen/arm32: head: Rework how the fixmap and early UART mapping are
prepared
xen/arm32: head: Improve logging in head.S

 xen/arch/arm/arm32/head.S               | 106 +++++++++++++-----------
 xen/arch/arm/include/asm/asm_defns.h    |   6 +-
 xen/arch/arm/include/asm/early_printk.h |   3 +
 xen/arch/arm/include/asm/mmu/layout.h   |   4 +
 xen/arch/arm/mm.c                       |   5 ++
 xen/arch/arm/xen.lds.S                  |   1 +
 6 files changed, 75 insertions(+), 50 deletions(-)
Signature · 1 line
2.40.1
#1 lore raw

PATCH 1/2 xen/arm32: head: Rework how the fixmap and early UART mapping are prepared

From: Julien Grall <[email protected]>

Since commit 5e213f0f4d2c ("xen/arm32: head: Widen the use of the temporary mapping"), boot_second (used to cover regions like Xen and the fixmap) will not be mapped if the identity mapping overlap.

So it is ok to prepare the fixmap table and link it in boot_second earlier. With that, the fixmap can also be used earlier via the temporary mapping.

Therefore split setup_fixmap() in two:
* The table is now linked in create_page_tables() because the boot page tables needs to be recreated for every CPU.
* The early UART mapping is only added for the boot CPU0 as the fixmap table is not cleared when secondary CPUs boot.

---
 xen/arch/arm/arm32/head.S | 48 ++++++++-------------------------------
 1 file changed, 9 insertions(+), 39 deletions(-)
xen/arch/arm/arm32/head.S +9 -39
index 33b038e7e0ca..fec2433e568f 100644
@@ -183,12 +183,16 @@ past_zImage:
         bl    check_cpu_mode
         bl    cpu_init
         bl    create_page_tables
+        /* Add the UART mapping if requested */
+#ifdef CONFIG_EARLY_PRINTK
+        mov_w r0, EARLY_UART_VIRTUAL_ADDRESS
+        create_mapping_entry xen_fixmap, r0, r11, type=PT_DEV_L3
+#endif
 
         /* Address in the runtime mapping to jump to after the MMU is enabled */
         mov_w lr, primary_switched
         b     enable_mmu
 primary_switched:
-        bl    setup_fixmap
 #ifdef CONFIG_EARLY_PRINTK
         /* Use a virtual address to access the UART. */
         mov_w r11, EARLY_UART_VIRTUAL_ADDRESS
@@ -456,11 +460,6 @@ ENDPROC(cpu_init)
  * Rebuild the boot pagetable's first-level entries. The structure
  * is described in mm.c.
  *
- * After the CPU enables paging it will add the fixmap mapping
- * to these page tables, however this may clash with the 1:1
- * mapping. So each CPU must rebuild the page tables here with
- * the 1:1 in place.
- *
  * Inputs:
  *   r9 : paddr(start)
  *   r10: phys offset
@@ -488,6 +487,10 @@ create_page_tables:
         add   r5, r5, #PAGE_SIZE            /* r5 := Next table */
 .endr
 
+        /* Map the fixmap into boot_second */
+        mov_w r0, FIXMAP_ADDR(0)
+        create_table_entry boot_second, xen_fixmap, r0, 2
+
         /*
          * Find the size of Xen in pages and multiply by the size of a
          * PTE. This will then be compared in the mapping loop below.
@@ -718,39 +721,6 @@ remove_temporary_mapping:
         mov  pc, lr
 ENDPROC(remove_temporary_mapping)
 
-/*
- * Map the UART in the fixmap (when earlyprintk is used) and hook the
- * fixmap table in the page tables.
- *
- * The fixmap cannot be mapped in create_page_tables because it may
- * clash with the 1:1 mapping.
- *
- * Inputs:
- *   r10: Physical offset
- *   r11: Early UART base physical address
- *
- * Clobbers r0 - r4
- */
-setup_fixmap:
-#if defined(CONFIG_EARLY_PRINTK)
-        /* Add UART to the fixmap table */
-        mov_w r0, EARLY_UART_VIRTUAL_ADDRESS
-        create_mapping_entry xen_fixmap, r0, r11, type=PT_DEV_L3
-#endif
-        /* Map fixmap into boot_second */
-        mov_w r0, FIXMAP_ADDR(0)
-        create_table_entry boot_second, xen_fixmap, r0, 2
-        /* Ensure any page table updates made above have occurred. */
-        dsb   nshst
-        /*
-         * The fixmap area will be used soon after. So ensure no hardware
-         * translation happens before the dsb completes.
-         */
-        isb
-
-        mov   pc, lr
-ENDPROC(setup_fixmap)
-
 /*
  * Setup the initial stack and jump to the C world
  *
Signature · 1 line
2.40.1
#2 lore raw

Re: [PATCH 1/2] xen/arm32: head: Rework how the fixmap and early UART mapping are prepared

Hi Julien,

Quoted 28 lines · Julien Grall

From: Julien Grall <[email protected]>

Since commit 5e213f0f4d2c ("xen/arm32: head: Widen the use of the temporary mapping"), boot_second (used to cover regions like Xen and the fixmap) will not be mapped if the identity mapping overlap.

So it is ok to prepare the fixmap table and link it in boot_second earlier. With that, the fixmap can also be used earlier via the temporary mapping.

Therefore split setup_fixmap() in two:
* The table is now linked in create_page_tables() because the boot page tables needs to be recreated for every CPU.
* The early UART mapping is only added for the boot CPU0 as the fixmap table is not cleared when secondary CPUs boot.

---
xen/arch/arm/arm32/head.S | 48 ++++++++-------------------------------

1 file changed, 9 insertions(+), 39 deletions(-)
xen/arch/arm/arm32/head.S +0 -0
index 33b038e7e0ca..fec2433e568f 100644

xen/arch/arm/arm32/head.S past_zImage: · line 189

Quoted 7 lines
xen/arch/arm/arm32/head.S +4 -0
@@ -183,12 +183,16 @@ past_zImage:
        bl    check_cpu_mode
        bl    cpu_init
        bl    create_page_tables
+        /* Add the UART mapping if requested */
+#ifdef CONFIG_EARLY_PRINTK
+        mov_w r0, EARLY_UART_VIRTUAL_ADDRESS
+        create_mapping_entry xen_fixmap, r0, r11, type=PT_DEV_L3

I tried to test this series today and found a corner case in this patch:

If we build Xen only with this patch, and CONFIG_EARLY_PRINTK=y, we will get below from compiler:

```
arch/arm/arm32/head.S: Assembler messages:
arch/arm/arm32/head.S:189: Error: bad instruction `create_mapping_entry xen_fixmap,r0,r11,type=0xe73'
make[3]: *** [Rules.mk:253: arch/arm/arm32/head.o] Error 1
```

With the second patch applied, the above error will disappear and I confirm arm32 will work fine with both patch applied + either CONFIG_EARLY_PRINTK={y,n}.

Kind regards,
Henry

#3 lore raw

Re: [PATCH 1/2] xen/arm32: head: Rework how the fixmap and early UART mapping are prepared

Hi,

Quoted 43 lines · Henry Wang

Hi Julien,

Quoted 28 lines · Julien Grall

From: Julien Grall <[email protected]>

Since commit 5e213f0f4d2c ("xen/arm32: head: Widen the use of the temporary mapping"), boot_second (used to cover regions like Xen and the fixmap) will not be mapped if the identity mapping overlap.

So it is ok to prepare the fixmap table and link it in boot_second earlier. With that, the fixmap can also be used earlier via the temporary mapping.

Therefore split setup_fixmap() in two:
* The table is now linked in create_page_tables() because the boot page tables needs to be recreated for every CPU.
* The early UART mapping is only added for the boot CPU0 as the fixmap table is not cleared when secondary CPUs boot.

---
xen/arch/arm/arm32/head.S | 48 ++++++++-------------------------------

1 file changed, 9 insertions(+), 39 deletions(-)
xen/arch/arm/arm32/head.S +0 -0
index 33b038e7e0ca..fec2433e568f 100644

xen/arch/arm/arm32/head.S past_zImage: · line 189

Quoted 7 lines
xen/arch/arm/arm32/head.S +4 -0
@@ -183,12 +183,16 @@ past_zImage:
         bl    check_cpu_mode
         bl    cpu_init
         bl    create_page_tables
+        /* Add the UART mapping if requested */
+#ifdef CONFIG_EARLY_PRINTK
+        mov_w r0, EARLY_UART_VIRTUAL_ADDRESS
+        create_mapping_entry xen_fixmap, r0, r11, type=PT_DEV_L3

I tried to test this series today and found a corner case in this patch:

If we build Xen only with this patch, and CONFIG_EARLY_PRINTK=y, we will get below from compiler:

Quoted 8 lines
```
arch/arm/arm32/head.S: Assembler messages:
arch/arm/arm32/head.S:189: Error: bad instruction `create_mapping_entry xen_fixmap,r0,r11,type=0xe73'
make[3]: *** [Rules.mk:253: arch/arm/arm32/head.o] Error 1
```

With the second patch applied, the above error will disappear and I confirm arm32 will work fine with both patch applied + either CONFIG_EARLY_PRINTK={y,n}.

Thanks for testing. Yes I merged one hunk into the wrong patch. I will re-order the code.

Cheers,

Signature · 1 line
Julien Grall
Reviewed-by on v1 1/2
#4 lore raw

Re: [PATCH 1/2] xen/arm32: head: Rework how the fixmap and early UART mapping are prepared

In reply to Julien Grall

Hi Julien,

Quoted 28 lines · Julien Grall

From: Julien Grall <[email protected]>

Since commit 5e213f0f4d2c ("xen/arm32: head: Widen the use of the temporary mapping"), boot_second (used to cover regions like Xen and the fixmap) will not be mapped if the identity mapping overlap.

So it is ok to prepare the fixmap table and link it in boot_second earlier. With that, the fixmap can also be used earlier via the temporary mapping.

Therefore split setup_fixmap() in two:
* The table is now linked in create_page_tables() because the boot page tables needs to be recreated for every CPU.
* The early UART mapping is only added for the boot CPU0 as the fixmap table is not cleared when secondary CPUs boot.

---
 xen/arch/arm/arm32/head.S | 48 ++++++++-------------------------------
 1 file changed, 9 insertions(+), 39 deletions(-)
xen/arch/arm/arm32/head.S +0 -0
index 33b038e7e0ca..fec2433e568f 100644

xen/arch/arm/arm32/head.S past_zImage: · line 189

Quoted 7 lines
xen/arch/arm/arm32/head.S +4 -0
@@ -183,12 +183,16 @@ past_zImage:
         bl    check_cpu_mode
         bl    cpu_init
         bl    create_page_tables
+        /* Add the UART mapping if requested */
+#ifdef CONFIG_EARLY_PRINTK
+        mov_w r0, EARLY_UART_VIRTUAL_ADDRESS
+        create_mapping_entry xen_fixmap, r0, r11, type=PT_DEV_L3

When building this patch, I'm getting a "Bad instruction" build error.
Quick look shows that you are trying to use the macro create_mapping_entry before macro definition. Unless, I'm using a different baseline (latest staging) than you, this needs to be fixed.

Apart from that:

~Michal

#5 lore raw

Re: [PATCH 1/2] xen/arm32: head: Rework how the fixmap and early UART mapping are prepared

Hi Michal,

Quoted 24 lines · Michal Orzel
Quoted 23 lines · Julien Grall

From: Julien Grall <[email protected]>

Since commit 5e213f0f4d2c ("xen/arm32: head: Widen the use of the temporary mapping"), boot_second (used to cover regions like Xen and the fixmap) will not be mapped if the identity mapping overlap.

So it is ok to prepare the fixmap table and link it in boot_second earlier. With that, the fixmap can also be used earlier via the temporary mapping.

Therefore split setup_fixmap() in two:
* The table is now linked in create_page_tables() because the boot page tables needs to be recreated for every CPU.
* The early UART mapping is only added for the boot CPU0 as the fixmap table is not cleared when secondary CPUs boot.

---
  xen/arch/arm/arm32/head.S | 48 ++++++++-------------------------------
  1 file changed, 9 insertions(+), 39 deletions(-)
Quoted 15 lines

xen/arch/arm/arm32/head.S past_zImage: · line 189

Quoted 12 lines
xen/arch/arm/arm32/head.S +4 -0
index 33b038e7e0ca..fec2433e568f 100644
@@ -183,12 +183,16 @@ past_zImage:
          bl    check_cpu_mode
          bl    cpu_init
          bl    create_page_tables
+        /* Add the UART mapping if requested */
+#ifdef CONFIG_EARLY_PRINTK
+        mov_w r0, EARLY_UART_VIRTUAL_ADDRESS
+        create_mapping_entry xen_fixmap, r0, r11, type=PT_DEV_L3

When building this patch, I'm getting a "Bad instruction" build error.
Quick look shows that you are trying to use the macro create_mapping_entry before macro definition. Unless, I'm using a different baseline (latest staging) than you, this needs to be fixed.

You are using the correct baseline. I have just ended up fix the build in patch #2 by mistake. I will move the change here.

Quoted 3 lines

Apart from that:

Thanks!

Cheers,

Signature · 1 line
Julien Grall
#6 lore raw

PATCH 2/2 xen/arm32: head: Improve logging in head.S

In reply to Julien Grall

From: Julien Grall <[email protected]>

The sequence to enable the MMU on arm32 is quite complex as we may need to jump to a temporary mapping to map Xen.

Recently, we had one bug in the logic (see f5a49eb7f8b3 ("xen/arm32:
head: Add mising isb in switch_to_runtime_mapping()") and it was a pain to debug because there are no logging.

In order to improve the logging in the MMU switch we need to add support for early printk while running on the identity mapping and also on the temporary mapping.

For the identity mapping, we have only the first page of Xen mapped. So all the strings should reside in the first page. For that purpose a new macro PRINT_ID is introduced.

For the temporary mapping, the fixmap is already linked the temporary area (and so does the UART). So we just need to update the register storing the UART address (i.e. r11) to point to the UART temporary mapping.

Take the opportunity to introduce mov_w_on_cond in order to conditionally execute mov_w and avoid branches.

---
 xen/arch/arm/arm32/head.S               | 68 ++++++++++++++++++++-----
 xen/arch/arm/include/asm/asm_defns.h    |  6 ++-
 xen/arch/arm/include/asm/early_printk.h |  3 ++
 xen/arch/arm/include/asm/mmu/layout.h   |  4 ++
 xen/arch/arm/mm.c                       |  5 ++
 xen/arch/arm/xen.lds.S                  |  1 +
 6 files changed, 71 insertions(+), 16 deletions(-)
xen/arch/arm/arm32/head.S +54 -14
index fec2433e568f..bd61521a9dea 100644
@@ -46,9 +46,13 @@
  * Move an immediate constant into a 32-bit register using movw/movt
  * instructions.
  */
+.macro mov_w_on_cond cond, reg, word
+        movw\cond  \reg, #:lower16:\word
+        movt\cond  \reg, #:upper16:\word
+.endm
+
 .macro mov_w reg, word
-        movw  \reg, #:lower16:\word
-        movt  \reg, #:upper16:\word
+        mov_w_on_cond al, \reg, \word
 .endm
 
 /*
@@ -104,16 +108,26 @@
  */
 #ifdef CONFIG_EARLY_PRINTK
 /*
- * Macro to print a string to the UART, if there is one.
+ * Macros to print a string to the UART, if there is one.
+ *
+ * There are multiple flavors:
+ *  - PRINT_SECT(section, string): The @string will be located in @section
+ *  - PRINT(): The string will be located in .rodata.str.
+ *  - PRINT_ID(): When Xen is running on the Identity Mapping, it is
+ *    only possible to have a limited amount of Xen. This will create
+ *    the string in .rodata.idmap which will always be mapped.
  *
  * Clobbers r0 - r3
  */
-#define PRINT(_s)           \
-        mov   r3, lr       ;\
-        adr_l r0, 98f      ;\
-        bl    puts         ;\
-        mov   lr, r3       ;\
-        RODATA_STR(98, _s)
+#define PRINT_SECT(section, string)         \
+        mov   r3, lr                       ;\
+        adr_l r0, 98f                      ;\
+        bl    puts                         ;\
+        mov   lr, r3                       ;\
+        RODATA_SECT(section, 98, string)
+
+#define PRINT(string) PRINT_SECT(.rodata.str, string)
+#define PRINT_ID(string) PRINT_SECT(.rodata.idmap, string)
 
 /*
  * Macro to print the value of register \rb
@@ -129,6 +143,7 @@
 
 #else /* CONFIG_EARLY_PRINTK */
 #define PRINT(s)
+#define PRINT_ID(s)
 
 .macro print_reg rb
 .endm
@@ -183,11 +198,6 @@ past_zImage:
         bl    check_cpu_mode
         bl    cpu_init
         bl    create_page_tables
-        /* Add the UART mapping if requested */
-#ifdef CONFIG_EARLY_PRINTK
-        mov_w r0, EARLY_UART_VIRTUAL_ADDRESS
-        create_mapping_entry xen_fixmap, r0, r11, type=PT_DEV_L3
-#endif
 
         /* Address in the runtime mapping to jump to after the MMU is enabled */
         mov_w lr, primary_switched
@@ -593,6 +603,21 @@ enable_mmu:
         mcr   CP32(r0, HSCTLR)       /* now paging is enabled */
         isb                          /* Now, flush the icache */
 
+        /*
+         * At this stage, the UART address will depend on whether the
+         * temporary mapping was created or not.
+         *
+         * If it was, then the UART will be mapped in the temporary
+         * area. Otherwise, it will be mapped at runtime virtual
+         * mapping.
+         */
+#ifdef CONFIG_EARLY_PRINTK
+        teq   r12, #1               /* Was the temporary mapping created? */
+        mov_w_on_cond eq, r11, TEMPORARY_EARLY_UART_VIRTUAL_ADDRESS
+        mov_w_on_cond ne, r11, EARLY_UART_VIRTUAL_ADDRESS
+#endif
+        PRINT_ID("- Paging turned on -\r\n")
+
         /*
          * The MMU is turned on and we are in the 1:1 mapping. Switch
          * to the runtime mapping.
@@ -643,12 +668,14 @@ switch_to_runtime_mapping:
         teq   r12, #0
         beq   ready_to_switch
 
+        PRINT_ID("- Switching to the temporary mapping -\r\n")
         /* We are still in the 1:1 mapping. Jump to the temporary Virtual address. */
         mov_w r0, 1f
         add   r0, r0, #XEN_TEMPORARY_OFFSET /* r0 := address in temporary mapping */
         mov   pc, r0
 
 1:
+        PRINT("- Running on the temporary mapping  -\r\n")
         /* Remove boot_second_id */
         mov   r2, #0
         mov   r3, #0
@@ -659,6 +686,8 @@ switch_to_runtime_mapping:
 
         flush_xen_tlb_local r0
 
+        PRINT("- 1:1 mapping removed -\r\n")
+
         /* Map boot_second into boot_pgtable */
         mov_w r0, XEN_VIRT_START
         create_table_entry boot_pgtable, boot_second, r0, 1
@@ -671,7 +700,10 @@ switch_to_runtime_mapping:
          */
         isb
 
+        PRINT("- Runtime mapping mapped -\r\n")
 ready_to_switch:
+        PRINT("- Jumping to runtime address -\r\n")
+
         mov   pc, lr
 ENDPROC(switch_to_runtime_mapping)
 
@@ -795,6 +827,14 @@ init_uart:
         early_uart_init r11, r1, r2
 #endif
         PRINT("- UART enabled -\r\n")
+
+        /*
+         * Add the UART mapping to the fixmap so the UART can be used
+         * as the MMU is on.
+         */
+        mov_w r0, EARLY_UART_VIRTUAL_ADDRESS
+        create_mapping_entry xen_fixmap, r0, r11, type=PT_DEV_L3
+
         mov   pc, lr
 ENDPROC(init_uart)
 
xen/arch/arm/include/asm/asm_defns.h +4 -2
index 29a9dbb002fa..ec803c0a370c 100644
@@ -22,11 +22,13 @@
 # error "unknown ARM variant"
 #endif
 
-#define RODATA_STR(label, msg)                  \
-.pushsection .rodata.str, "aMS", %progbits, 1 ; \
+#define RODATA_SECT(section, label, msg)         \
+.pushsection section, "aMS", %progbits, 1 ;     \
 label:  .asciz msg;                             \
 .popsection
 
+#define RODATA_STR(label, msg) RODATA_SECT(.rodata.str, label, msg)
+
 #define ASM_INT(label, val)                 \
     .p2align 2;                             \
 label: .long (val);                         \
xen/arch/arm/include/asm/early_printk.h +3 -0
index c5149b2976da..c1e84f8b0009 100644
@@ -19,6 +19,9 @@
 #define EARLY_UART_VIRTUAL_ADDRESS \
     (FIXMAP_ADDR(FIXMAP_CONSOLE) + (CONFIG_EARLY_UART_BASE_ADDRESS & ~PAGE_MASK))
 
+#define TEMPORARY_EARLY_UART_VIRTUAL_ADDRESS \
+    (TEMPORARY_FIXMAP_ADDR(FIXMAP_CONSOLE) + (CONFIG_EARLY_UART_BASE_ADDRESS & ~PAGE_MASK))
+
 #endif /* !CONFIG_EARLY_PRINTK */
 
 #endif
xen/arch/arm/include/asm/mmu/layout.h +4 -0
index da6be276ac5f..68855c4f3184 100644
@@ -113,6 +113,10 @@
       (TEMPORARY_AREA_FIRST_SLOT << XEN_PT_LEVEL_SHIFT(1)))
 
 #define TEMPORARY_XEN_VIRT_START    TEMPORARY_AREA_ADDR(XEN_VIRT_START)
+#define TEMPORARY_FIXMAP_VIRT_START TEMPORARY_AREA_ADDR(FIXMAP_VIRT_START)
+
+#define TEMPORARY_FIXMAP_ADDR(n)                    \
+     (TEMPORARY_FIXMAP_VIRT_START + (n) * PAGE_SIZE)
 
 #else /* ARM_64 */
 
xen/arch/arm/mm.c +5 -0
index c34cc94c907a..6ffa45c53be6 100644
@@ -193,7 +193,12 @@ static void __init __maybe_unused build_assertions(void)
 #ifdef CONFIG_ARM_32
     CHECK_SAME_SLOT(first, TEMPORARY_XEN_VIRT_START, DOMHEAP_VIRT_START);
     CHECK_DIFFERENT_SLOT(first, XEN_VIRT_START, TEMPORARY_XEN_VIRT_START);
+    CHECK_SAME_SLOT(first, TEMPORARY_XEN_VIRT_START,
+                    TEMPORARY_FIXMAP_VIRT_START);
+    CHECK_SAME_SLOT(first, TEMPORARY_XEN_VIRT_START,
+                    TEMPORARY_FIXMAP_VIRT_START);
     CHECK_SAME_SLOT(second, XEN_VIRT_START, TEMPORARY_XEN_VIRT_START);
+    CHECK_SAME_SLOT(second, FIXMAP_VIRT_START, TEMPORARY_FIXMAP_VIRT_START);
 #endif
 
 #undef CHECK_SAME_SLOT
xen/arch/arm/xen.lds.S +1 -0
index 59b80d122fd0..20598c6963ce 100644
@@ -35,6 +35,7 @@ SECTIONS
        _idmap_start = .;
        *(.text.header)
        *(.text.idmap)
+       *(.rodata.idmap)
        _idmap_end = .;
 
        *(.text.cold)
Signature · 1 line
2.40.1
#7 lore raw

Re: [PATCH 2/2] xen/arm32: head: Improve logging in head.S

Hi Julien,

Quoted 100 lines · Julien Grall

From: Julien Grall <[email protected]>

The sequence to enable the MMU on arm32 is quite complex as we may need to jump to a temporary mapping to map Xen.

Recently, we had one bug in the logic (see f5a49eb7f8b3 ("xen/arm32:
head: Add mising isb in switch_to_runtime_mapping()") and it was a pain to debug because there are no logging.

In order to improve the logging in the MMU switch we need to add support for early printk while running on the identity mapping and also on the temporary mapping.

For the identity mapping, we have only the first page of Xen mapped. So all the strings should reside in the first page. For that purpose a new macro PRINT_ID is introduced.

For the temporary mapping, the fixmap is already linked the temporary area (and so does the UART). So we just need to update the register storing the UART address (i.e. r11) to point to the UART temporary mapping.

Take the opportunity to introduce mov_w_on_cond in order to conditionally execute mov_w and avoid branches.

---
 xen/arch/arm/arm32/head.S               | 68 ++++++++++++++++++++-----
 xen/arch/arm/include/asm/asm_defns.h    |  6 ++-
 xen/arch/arm/include/asm/early_printk.h |  3 ++
 xen/arch/arm/include/asm/mmu/layout.h   |  4 ++
 xen/arch/arm/mm.c                       |  5 ++
 xen/arch/arm/xen.lds.S                  |  1 +
 6 files changed, 71 insertions(+), 16 deletions(-)
xen/arch/arm/arm32/head.S +24 -9
index fec2433e568f..bd61521a9dea 100644
@@ -46,9 +46,13 @@
  * Move an immediate constant into a 32-bit register using movw/movt
  * instructions.
  */
+.macro mov_w_on_cond cond, reg, word
+        movw\cond  \reg, #:lower16:\word
+        movt\cond  \reg, #:upper16:\word
+.endm
+
 .macro mov_w reg, word
-        movw  \reg, #:lower16:\word
-        movt  \reg, #:upper16:\word
+        mov_w_on_cond al, \reg, \word
 .endm
 
 /*
@@ -104,16 +108,26 @@
  */
 #ifdef CONFIG_EARLY_PRINTK
 /*
- * Macro to print a string to the UART, if there is one.
+ * Macros to print a string to the UART, if there is one.
+ *
+ * There are multiple flavors:
+ *  - PRINT_SECT(section, string): The @string will be located in @section
+ *  - PRINT(): The string will be located in .rodata.str.
+ *  - PRINT_ID(): When Xen is running on the Identity Mapping, it is
+ *    only possible to have a limited amount of Xen. This will create
+ *    the string in .rodata.idmap which will always be mapped.
  *
  * Clobbers r0 - r3
  */
-#define PRINT(_s)           \
-        mov   r3, lr       ;\
-        adr_l r0, 98f      ;\
-        bl    puts         ;\
-        mov   lr, r3       ;\
-        RODATA_STR(98, _s)
+#define PRINT_SECT(section, string)         \
+        mov   r3, lr                       ;\
+        adr_l r0, 98f                      ;\
+        bl    puts                         ;\
+        mov   lr, r3                       ;\
+        RODATA_SECT(section, 98, string)
+
+#define PRINT(string) PRINT_SECT(.rodata.str, string)
+#define PRINT_ID(string) PRINT_SECT(.rodata.idmap, string)
 
 /*
  * Macro to print the value of register \rb
@@ -129,6 +143,7 @@
 
 #else /* CONFIG_EARLY_PRINTK */
 #define PRINT(s)
+#define PRINT_ID(s)
 
 .macro print_reg rb
 .endm

xen/arch/arm/arm32/head.S past_zImage: · line 190

Quoted 8 lines
xen/arch/arm/arm32/head.S +0 -5
@@ -183,11 +198,6 @@ past_zImage:
         bl    check_cpu_mode
         bl    cpu_init
         bl    create_page_tables
-        /* Add the UART mapping if requested */
-#ifdef CONFIG_EARLY_PRINTK
-        mov_w r0, EARLY_UART_VIRTUAL_ADDRESS
-        create_mapping_entry xen_fixmap, r0, r11, type=PT_DEV_L3
-#endif

I think that patch no.1 should move the mapping into init_uart right away. This way the issue I mentioned in that patch will be fixed and there will be no need to remove the code introduced just before.

Quoted 8 lines
xen/arch/arm/arm32/head.S +1 -0
         /* Address in the runtime mapping to jump to after the MMU is enabled */
         mov_w lr, primary_switched
@@ -593,6 +603,21 @@ enable_mmu:
         mcr   CP32(r0, HSCTLR)       /* now paging is enabled */
         isb                          /* Now, flush the icache */
 
+        /*

xen/arch/arm/arm32/head.S enable_mmu: · line 616

Quoted 10 lines
xen/arch/arm/arm32/head.S +10 -0
+         * At this stage, the UART address will depend on whether the
+         * temporary mapping was created or not.
+         *
+         * If it was, then the UART will be mapped in the temporary
+         * area. Otherwise, it will be mapped at runtime virtual
+         * mapping.
+         */
+#ifdef CONFIG_EARLY_PRINTK
+        teq   r12, #1               /* Was the temporary mapping created? */
+        mov_w_on_cond eq, r11, TEMPORARY_EARLY_UART_VIRTUAL_ADDRESS

Shouldn't the clobber list be updated?

Quoted 32 lines
xen/arch/arm/arm32/head.S +8 -0
+        mov_w_on_cond ne, r11, EARLY_UART_VIRTUAL_ADDRESS
+#endif
+        PRINT_ID("- Paging turned on -\r\n")
+
         /*
          * The MMU is turned on and we are in the 1:1 mapping. Switch
          * to the runtime mapping.
@@ -643,12 +668,14 @@ switch_to_runtime_mapping:
         teq   r12, #0
         beq   ready_to_switch
 
+        PRINT_ID("- Switching to the temporary mapping -\r\n")
         /* We are still in the 1:1 mapping. Jump to the temporary Virtual address. */
         mov_w r0, 1f
         add   r0, r0, #XEN_TEMPORARY_OFFSET /* r0 := address in temporary mapping */
         mov   pc, r0
 
 1:
+        PRINT("- Running on the temporary mapping  -\r\n")
         /* Remove boot_second_id */
         mov   r2, #0
         mov   r3, #0
@@ -659,6 +686,8 @@ switch_to_runtime_mapping:
 
         flush_xen_tlb_local r0
 
+        PRINT("- 1:1 mapping removed -\r\n")
+
         /* Map boot_second into boot_pgtable */
         mov_w r0, XEN_VIRT_START
         create_table_entry boot_pgtable, boot_second, r0, 1

xen/arch/arm/arm32/head.S switch_to_runtime_mapping: · line 705

Quoted 6 lines
xen/arch/arm/arm32/head.S +2 -0
@@ -671,7 +700,10 @@ switch_to_runtime_mapping:
          */
         isb
 
+        PRINT("- Runtime mapping mapped -\r\n")
 ready_to_switch:
+        PRINT("- Jumping to runtime address -\r\n")

This is causing a boot hang in a configuration where there is no need for temporary mapping. In that case, we are still in 1:1 map and should use PRINT_ID

Quoted 5 lines
xen/arch/arm/arm32/head.S +1 -0
+
         mov   pc, lr
 ENDPROC(switch_to_runtime_mapping)
 

xen/arch/arm/arm32/head.S init_uart: · line 836

Quoted 10 lines
xen/arch/arm/arm32/head.S +7 -0
@@ -795,6 +827,14 @@ init_uart:
         early_uart_init r11, r1, r2
 #endif
         PRINT("- UART enabled -\r\n")
+
+        /*
+         * Add the UART mapping to the fixmap so the UART can be used
+         * as the MMU is on.
+         */
+        mov_w r0, EARLY_UART_VIRTUAL_ADDRESS
+        create_mapping_entry xen_fixmap, r0, r11, type=PT_DEV_L3

Shouldn't the clobber list be updated to include r4?

Quoted 58 lines
xen/arch/arm/arm32/head.S +1 -0
+
         mov   pc, lr
 ENDPROC(init_uart)
 
xen/arch/arm/include/asm/asm_defns.h +4 -2
index 29a9dbb002fa..ec803c0a370c 100644
@@ -22,11 +22,13 @@
 # error "unknown ARM variant"
 #endif
 
-#define RODATA_STR(label, msg)                  \
-.pushsection .rodata.str, "aMS", %progbits, 1 ; \
+#define RODATA_SECT(section, label, msg)         \
+.pushsection section, "aMS", %progbits, 1 ;     \
 label:  .asciz msg;                             \
 .popsection
 
+#define RODATA_STR(label, msg) RODATA_SECT(.rodata.str, label, msg)
+
 #define ASM_INT(label, val)                 \
     .p2align 2;                             \
 label: .long (val);                         \
xen/arch/arm/include/asm/early_printk.h +3 -0
index c5149b2976da..c1e84f8b0009 100644
@@ -19,6 +19,9 @@
 #define EARLY_UART_VIRTUAL_ADDRESS \
     (FIXMAP_ADDR(FIXMAP_CONSOLE) + (CONFIG_EARLY_UART_BASE_ADDRESS & ~PAGE_MASK))
 
+#define TEMPORARY_EARLY_UART_VIRTUAL_ADDRESS \
+    (TEMPORARY_FIXMAP_ADDR(FIXMAP_CONSOLE) + (CONFIG_EARLY_UART_BASE_ADDRESS & ~PAGE_MASK))
+
 #endif /* !CONFIG_EARLY_PRINTK */
 
 #endif
xen/arch/arm/include/asm/mmu/layout.h +4 -0
index da6be276ac5f..68855c4f3184 100644
@@ -113,6 +113,10 @@
       (TEMPORARY_AREA_FIRST_SLOT << XEN_PT_LEVEL_SHIFT(1)))
 
 #define TEMPORARY_XEN_VIRT_START    TEMPORARY_AREA_ADDR(XEN_VIRT_START)
+#define TEMPORARY_FIXMAP_VIRT_START TEMPORARY_AREA_ADDR(FIXMAP_VIRT_START)
+
+#define TEMPORARY_FIXMAP_ADDR(n)                    \
+     (TEMPORARY_FIXMAP_VIRT_START + (n) * PAGE_SIZE)
 
 #else /* ARM_64 */
 
xen/arch/arm/mm.c +0 -0
index c34cc94c907a..6ffa45c53be6 100644

xen/arch/arm/mm.c static void __init __maybe_unused build_assertions(void) · line 199

Quoted 7 lines
xen/arch/arm/mm.c +4 -0
@@ -193,7 +193,12 @@ static void __init __maybe_unused build_assertions(void)
 #ifdef CONFIG_ARM_32
     CHECK_SAME_SLOT(first, TEMPORARY_XEN_VIRT_START, DOMHEAP_VIRT_START);
     CHECK_DIFFERENT_SLOT(first, XEN_VIRT_START, TEMPORARY_XEN_VIRT_START);
+    CHECK_SAME_SLOT(first, TEMPORARY_XEN_VIRT_START,
+                    TEMPORARY_FIXMAP_VIRT_START);
+    CHECK_SAME_SLOT(first, TEMPORARY_XEN_VIRT_START,
+                    TEMPORARY_FIXMAP_VIRT_START);

duplicated entry?

~Michal

#8 lore raw

Re: [PATCH 2/2] xen/arm32: head: Improve logging in head.S

Quoted 1 line · Michal Orzel

Hi Julien,

Hi Michal,

Quoted 98 lines · Julien Grall
Quoted 97 lines

From: Julien Grall <[email protected]>

The sequence to enable the MMU on arm32 is quite complex as we may need to jump to a temporary mapping to map Xen.

Recently, we had one bug in the logic (see f5a49eb7f8b3 ("xen/arm32:
head: Add mising isb in switch_to_runtime_mapping()") and it was a pain to debug because there are no logging.

In order to improve the logging in the MMU switch we need to add support for early printk while running on the identity mapping and also on the temporary mapping.

For the identity mapping, we have only the first page of Xen mapped. So all the strings should reside in the first page. For that purpose a new macro PRINT_ID is introduced.

For the temporary mapping, the fixmap is already linked the temporary area (and so does the UART). So we just need to update the register storing the UART address (i.e. r11) to point to the UART temporary mapping.

Take the opportunity to introduce mov_w_on_cond in order to conditionally execute mov_w and avoid branches.

---
  xen/arch/arm/arm32/head.S               | 68 ++++++++++++++++++++-----
  xen/arch/arm/include/asm/asm_defns.h    |  6 ++-
  xen/arch/arm/include/asm/early_printk.h |  3 ++
  xen/arch/arm/include/asm/mmu/layout.h   |  4 ++
  xen/arch/arm/mm.c                       |  5 ++
  xen/arch/arm/xen.lds.S                  |  1 +
  6 files changed, 71 insertions(+), 16 deletions(-)
xen/arch/arm/arm32/head.S +24 -9
index fec2433e568f..bd61521a9dea 100644
@@ -46,9 +46,13 @@
   * Move an immediate constant into a 32-bit register using movw/movt
   * instructions.
   */
+.macro mov_w_on_cond cond, reg, word
+        movw\cond  \reg, #:lower16:\word
+        movt\cond  \reg, #:upper16:\word
+.endm
+
  .macro mov_w reg, word
-        movw  \reg, #:lower16:\word
-        movt  \reg, #:upper16:\word
+        mov_w_on_cond al, \reg, \word
  .endm
 
  /*
@@ -104,16 +108,26 @@
   */
  #ifdef CONFIG_EARLY_PRINTK
  /*
- * Macro to print a string to the UART, if there is one.
+ * Macros to print a string to the UART, if there is one.
+ *
+ * There are multiple flavors:
+ *  - PRINT_SECT(section, string): The @string will be located in @section
+ *  - PRINT(): The string will be located in .rodata.str.
+ *  - PRINT_ID(): When Xen is running on the Identity Mapping, it is
+ *    only possible to have a limited amount of Xen. This will create
+ *    the string in .rodata.idmap which will always be mapped.
   *
   * Clobbers r0 - r3
   */
-#define PRINT(_s)           \
-        mov   r3, lr       ;\
-        adr_l r0, 98f      ;\
-        bl    puts         ;\
-        mov   lr, r3       ;\
-        RODATA_STR(98, _s)
+#define PRINT_SECT(section, string)         \
+        mov   r3, lr                       ;\
+        adr_l r0, 98f                      ;\
+        bl    puts                         ;\
+        mov   lr, r3                       ;\
+        RODATA_SECT(section, 98, string)
+
+#define PRINT(string) PRINT_SECT(.rodata.str, string)
+#define PRINT_ID(string) PRINT_SECT(.rodata.idmap, string)
 
  /*
   * Macro to print the value of register \rb
@@ -129,6 +143,7 @@
 
  #else /* CONFIG_EARLY_PRINTK */
  #define PRINT(s)
+#define PRINT_ID(s)
 
Quoted 14 lines

xen/arch/arm/arm32/head.S past_zImage: · line 190

Quoted 11 lines
xen/arch/arm/arm32/head.S +0 -5
  .macro print_reg rb
  .endm
@@ -183,11 +198,6 @@ past_zImage:
          bl    check_cpu_mode
          bl    cpu_init
          bl    create_page_tables
-        /* Add the UART mapping if requested */
-#ifdef CONFIG_EARLY_PRINTK
-        mov_w r0, EARLY_UART_VIRTUAL_ADDRESS
-        create_mapping_entry xen_fixmap, r0, r11, type=PT_DEV_L3
-#endif

I think that patch no.1 should move the mapping into init_uart right away. This way the issue I mentioned in that patch will be fixed and there will be no need to remove the code introduced just before.

Correct. I folded the fix in the wrong patch.

Quoted 56 lines
Quoted 9 lines
xen/arch/arm/arm32/head.S +1 -0
          /* Address in the runtime mapping to jump to after the MMU is enabled */
          mov_w lr, primary_switched
@@ -593,6 +603,21 @@ enable_mmu:
          mcr   CP32(r0, HSCTLR)       /* now paging is enabled */
          isb                          /* Now, flush the icache */
 
+        /*

xen/arch/arm/arm32/head.S enable_mmu: · line 616

Quoted 10 lines
xen/arch/arm/arm32/head.S +10 -0
+         * At this stage, the UART address will depend on whether the
+         * temporary mapping was created or not.
+         *
+         * If it was, then the UART will be mapped in the temporary
+         * area. Otherwise, it will be mapped at runtime virtual
+         * mapping.
+         */
+#ifdef CONFIG_EARLY_PRINTK
+        teq   r12, #1               /* Was the temporary mapping created? */
+        mov_w_on_cond eq, r11, TEMPORARY_EARLY_UART_VIRTUAL_ADDRESS

Shouldn't the clobber list be updated?

Quoted 35 lines
xen/arch/arm/arm32/head.S +8 -0
+        mov_w_on_cond ne, r11, EARLY_UART_VIRTUAL_ADDRESS
+#endif
+        PRINT_ID("- Paging turned on -\r\n")
+
          /*
           * The MMU is turned on and we are in the 1:1 mapping. Switch
           * to the runtime mapping.
@@ -643,12 +668,14 @@ switch_to_runtime_mapping:
          teq   r12, #0
          beq   ready_to_switch
 
+        PRINT_ID("- Switching to the temporary mapping -\r\n")
          /* We are still in the 1:1 mapping. Jump to the temporary Virtual address. */
          mov_w r0, 1f
          add   r0, r0, #XEN_TEMPORARY_OFFSET /* r0 := address in temporary mapping */
          mov   pc, r0
 
  1:
+        PRINT("- Running on the temporary mapping  -\r\n")
          /* Remove boot_second_id */
          mov   r2, #0
          mov   r3, #0
@@ -659,6 +686,8 @@ switch_to_runtime_mapping:
 
          flush_xen_tlb_local r0
 
+        PRINT("- 1:1 mapping removed -\r\n")
+
          /* Map boot_second into boot_pgtable */
          mov_w r0, XEN_VIRT_START
          create_table_entry boot_pgtable, boot_second, r0, 1
@@ -671,7 +700,10 @@ switch_to_runtime_mapping:
           */
          isb
 
Quoted 5 lines

xen/arch/arm/arm32/head.S switch_to_runtime_mapping: · line 705

Quoted 3 lines
xen/arch/arm/arm32/head.S +2 -0
+        PRINT("- Runtime mapping mapped -\r\n")
  ready_to_switch:
+        PRINT("- Jumping to runtime address -\r\n")

This is causing a boot hang in a configuration where there is no need for temporary mapping. In that case, we are still in 1:1 map and should use PRINT_ID

Good point. I will switch.

Quoted 5 lines
Quoted 5 lines
xen/arch/arm/arm32/head.S +1 -0
+
          mov   pc, lr
  ENDPROC(switch_to_runtime_mapping)
 
Quoted 12 lines

xen/arch/arm/arm32/head.S init_uart: · line 836

Quoted 11 lines
xen/arch/arm/arm32/head.S +7 -0
@@ -795,6 +827,14 @@ init_uart:
          early_uart_init r11, r1, r2
  #endif
          PRINT("- UART enabled -\r\n")
+
+        /*
+         * Add the UART mapping to the fixmap so the UART can be used
+         * as the MMU is on.
+         */
+        mov_w r0, EARLY_UART_VIRTUAL_ADDRESS
+        create_mapping_entry xen_fixmap, r0, r11, type=PT_DEV_L3

Shouldn't the clobber list be updated to include r4?

It should.

Quoted 54 lines
Quoted 54 lines
xen/arch/arm/arm32/head.S +1 -0
+
          mov   pc, lr
  ENDPROC(init_uart)
 
xen/arch/arm/include/asm/asm_defns.h +4 -2
index 29a9dbb002fa..ec803c0a370c 100644
@@ -22,11 +22,13 @@
  # error "unknown ARM variant"
  #endif
 
-#define RODATA_STR(label, msg)                  \
-.pushsection .rodata.str, "aMS", %progbits, 1 ; \
+#define RODATA_SECT(section, label, msg)         \
+.pushsection section, "aMS", %progbits, 1 ;     \
  label:  .asciz msg;                             \
  .popsection
 
+#define RODATA_STR(label, msg) RODATA_SECT(.rodata.str, label, msg)
+
  #define ASM_INT(label, val)                 \
      .p2align 2;                             \
  label: .long (val);                         \
xen/arch/arm/include/asm/early_printk.h +3 -0
index c5149b2976da..c1e84f8b0009 100644
@@ -19,6 +19,9 @@
  #define EARLY_UART_VIRTUAL_ADDRESS \
      (FIXMAP_ADDR(FIXMAP_CONSOLE) + (CONFIG_EARLY_UART_BASE_ADDRESS & ~PAGE_MASK))
 
+#define TEMPORARY_EARLY_UART_VIRTUAL_ADDRESS \
+    (TEMPORARY_FIXMAP_ADDR(FIXMAP_CONSOLE) + (CONFIG_EARLY_UART_BASE_ADDRESS & ~PAGE_MASK))
+
  #endif /* !CONFIG_EARLY_PRINTK */
 
  #endif
xen/arch/arm/include/asm/mmu/layout.h +4 -0
index da6be276ac5f..68855c4f3184 100644
@@ -113,6 +113,10 @@
        (TEMPORARY_AREA_FIRST_SLOT << XEN_PT_LEVEL_SHIFT(1)))
 
  #define TEMPORARY_XEN_VIRT_START    TEMPORARY_AREA_ADDR(XEN_VIRT_START)
+#define TEMPORARY_FIXMAP_VIRT_START TEMPORARY_AREA_ADDR(FIXMAP_VIRT_START)
+
+#define TEMPORARY_FIXMAP_ADDR(n)                    \
+     (TEMPORARY_FIXMAP_VIRT_START + (n) * PAGE_SIZE)
 
  #else /* ARM_64 */
 
Quoted 13 lines

xen/arch/arm/mm.c static void __init __maybe_unused build_assertions(void) · line 199

Quoted 12 lines
xen/arch/arm/mm.c +4 -0
index c34cc94c907a..6ffa45c53be6 100644
@@ -193,7 +193,12 @@ static void __init __maybe_unused build_assertions(void)
  #ifdef CONFIG_ARM_32
      CHECK_SAME_SLOT(first, TEMPORARY_XEN_VIRT_START, DOMHEAP_VIRT_START);
      CHECK_DIFFERENT_SLOT(first, XEN_VIRT_START, TEMPORARY_XEN_VIRT_START);
+    CHECK_SAME_SLOT(first, TEMPORARY_XEN_VIRT_START,
+                    TEMPORARY_FIXMAP_VIRT_START);
+    CHECK_SAME_SLOT(first, TEMPORARY_XEN_VIRT_START,
+                    TEMPORARY_FIXMAP_VIRT_START);

duplicated entry?

Yes it is duplicated.

Cheers,

Signature · 1 line
Julien Grall
#9 lore raw

Re: [PATCH 2/2] xen/arm32: head: Improve logging in head.S

In reply to Michal Orzel
Quoted 6 lines · Michal Orzel
Quoted 6 lines

/* Address in the runtime mapping to jump to after the MMU is enabled */ mov_w lr, primary_switched

xen/arch/arm/arm32/head.S +0 -0
@@ -593,6 +603,21 @@ enable_mmu:
          mcr   CP32(r0, HSCTLR)       /* now paging is enabled */
          isb                          /* Now, flush the icache */
 
Quoted 12 lines

xen/arch/arm/arm32/head.S enable_mmu: · line 616

Quoted 11 lines
xen/arch/arm/arm32/head.S +11 -0
+        /*
+         * At this stage, the UART address will depend on whether the
+         * temporary mapping was created or not.
+         *
+         * If it was, then the UART will be mapped in the temporary
+         * area. Otherwise, it will be mapped at runtime virtual
+         * mapping.
+         */
+#ifdef CONFIG_EARLY_PRINTK
+        teq   r12, #1               /* Was the temporary mapping created? */
+        mov_w_on_cond eq, r11, TEMPORARY_EARLY_UART_VIRTUAL_ADDRESS

Shouldn't the clobber list be updated?

I missed this comment. I understand it will get clobbered today, but thinking a bit more, I would actually prefer if r11 is updated to EARLY_UART_ADDRESS just right after calling switch_to_runtime_mapping.

So we can add more prints in remove_identity_mapping() and remove_temporary_mapping().

I will have a look to update this patch.

Cheers,

Signature · 1 line
Julien Grall