Skip to contentSkip to conversation

target/arm: armv7m_load_kernel() improvements

PATCH In review Peter Maydell 3 participants 8 messages Aug 23, 2022 – Aug 30, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover target/arm: armv7m_load_kernel() improvements

Two small patches to armv7m_load_kernel(). The first is just getting rid of some dead code, that I noticed while working on the function. The second is to make boards pass armv7m_load_kernel() the base address for loading guest (non-ELF) binaries. At the moment we assume all M-profile boards start at address 0; this happens to be true for all the ones we implement right now, but it's not true in general. In particular the Teeny board has its ROM at 0x0020_0000.

I thought about having armv7m_load_kernel() be "clever" and ask the CPU what init-svtor/init-nsvtor were set to, but that seems like it might have unanticipated consequences[*]. "Just pass the base address" is simpler and is how A-profile does it (though for A-profile it's the loader_start field in struct arm_boot_info rather than an extra argument).

[*] eg where the board has the rom/flash aliased at both address 0 and some other address, and init-svtor points at an alias; also Secure vs NonSecure address spaces and loading...

thanks
-- PMM

Peter Maydell (2):
target/arm: Remove useless TARGET_BIG_ENDIAN check in
armv7m_load_kernel()
target/arm: Make boards pass base address to armv7m_load_kernel()

 include/hw/arm/boot.h     |  5 ++++-
 hw/arm/armv7m.c           | 14 ++++----------
 hw/arm/aspeed.c           |  1 +
 hw/arm/microbit.c         |  2 +-
 hw/arm/mps2-tz.c          |  2 +-
 hw/arm/mps2.c             |  2 +-
 hw/arm/msf2-som.c         |  2 +-
 hw/arm/musca.c            |  3 ++-
 hw/arm/netduino2.c        |  2 +-
 hw/arm/netduinoplus2.c    |  2 +-
 hw/arm/stellaris.c        |  2 +-
 hw/arm/stm32vldiscovery.c |  2 +-
 12 files changed, 19 insertions(+), 20 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/2 target/arm: Remove useless TARGET_BIG_ENDIAN check in armv7m_load_kernel()

Arm system emulation targets always have TARGET_BIG_ENDIAN clear, so there is no need to have handling in armv7m_load_kernel() for the case when it is defined. Remove the unnecessary code.

Side notes:
* our M-profile implementation is always little-endian (that is, it makes the IMPDEF choice that the read-only AIRCR.ENDIANNESS is 0)
* if we did want to handle big-endian ELF files here we should do it the way that hw/arm/boot.c:arm_load_elf() does, by looking at the ELF header to see what endianness the file itself is

---
 hw/arm/armv7m.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
hw/arm/armv7m.c +1 -8
index 990861ee5ef..fa4c2c735da 100644
@@ -572,17 +572,10 @@ void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename, int mem_size)
 {
     ssize_t image_size;
     uint64_t entry;
-    int big_endian;
     AddressSpace *as;
     int asidx;
     CPUState *cs = CPU(cpu);
 
-#if TARGET_BIG_ENDIAN
-    big_endian = 1;
-#else
-    big_endian = 0;
-#endif
-
     if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
         asidx = ARMASIdx_S;
     } else {
@@ -593,7 +586,7 @@ void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename, int mem_size)
     if (kernel_filename) {
         image_size = load_elf_as(kernel_filename, NULL, NULL, NULL,
                                  &entry, NULL, NULL,
-                                 NULL, big_endian, EM_ARM, 1, 0, as);
+                                 NULL, 0, EM_ARM, 1, 0, as);
         if (image_size < 0) {
             image_size = load_image_targphys_as(kernel_filename, 0,
                                                 mem_size, as);
Signature · 1 line
2.25.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] target/arm: Remove useless TARGET_BIG_ENDIAN check in armv7m_load_kernel()

Quoted 4 lines · Peter Maydell

Arm system emulation targets always have TARGET_BIG_ENDIAN clear, so there is no need to have handling in armv7m_load_kernel() for the case when it is defined. Remove the unnecessary code.

Quoted 8 lines

Side notes:
* our M-profile implementation is always little-endian (that is, it makes the IMPDEF choice that the read-only AIRCR.ENDIANNESS is 0)
* if we did want to handle big-endian ELF files here we should do it the way that hw/arm/boot.c:arm_load_elf() does, by looking at the ELF header to see what endianness the file itself is

r~

Reviewed-by Philippe Mathieu-Daudé on v1 1/2
#3 lore raw

Re: [PATCH 1/2] target/arm: Remove useless TARGET_BIG_ENDIAN check in armv7m_load_kernel()

In reply to Peter Maydell
Quoted 11 lines · Peter Maydell

Arm system emulation targets always have TARGET_BIG_ENDIAN clear, so there is no need to have handling in armv7m_load_kernel() for the case when it is defined. Remove the unnecessary code.

Side notes:
* our M-profile implementation is always little-endian (that is, it makes the IMPDEF choice that the read-only AIRCR.ENDIANNESS is 0)
* if we did want to handle big-endian ELF files here we should do it the way that hw/arm/boot.c:arm_load_elf() does, by looking at the ELF header to see what endianness the file itself is

Quoted 4 lines
---
  hw/arm/armv7m.c | 9 +--------
  1 file changed, 1 insertion(+), 8 deletions(-)
#4 lore raw

PATCH 2/2 target/arm: Make boards pass base address to armv7m_load_kernel()

In reply to Peter Maydell

Currently armv7m_load_kernel() takes the size of the block of memory where it should load the initial guest image, but assumes that it should always load it at address 0. This happens to be true of all our M-profile boards at the moment, but it isn't guaranteed to always be so: M-profile CPUs can be configured (via init-svtor and init-nsvtor, which match equivalent hardware configuration signals) to have the initial vector table at any address, not just zero. (For instance the Teeny board has the boot ROM at address 0x0200_0000.)

Add a base address argument to armv7m_load_kernel(), so that callers now pass in both base address and size. All the current callers pass 0, so this is not a behaviour change.

---
I thought about having armv7m_load_kernel() be "clever" and ask the CPU what init-svtor/init-nsvtor were set to, but that seems like it might have unanticipated consequences. "Just pass the base address" is simpler and is how A-profile does it (though for A-profile it's the loader_start field in struct arm_boot_info rather than an extra argument).

---
 include/hw/arm/boot.h     | 5 ++++-
 hw/arm/armv7m.c           | 5 +++--
 hw/arm/aspeed.c           | 1 +
 hw/arm/microbit.c         | 2 +-
 hw/arm/mps2-tz.c          | 2 +-
 hw/arm/mps2.c             | 2 +-
 hw/arm/msf2-som.c         | 2 +-
 hw/arm/musca.c            | 3 ++-
 hw/arm/netduino2.c        | 2 +-
 hw/arm/netduinoplus2.c    | 2 +-
 hw/arm/stellaris.c        | 2 +-
 hw/arm/stm32vldiscovery.c | 2 +-
 12 files changed, 18 insertions(+), 12 deletions(-)
include/hw/arm/boot.h +4 -1
index c7ebae156ec..f18cc3064ff 100644
@@ -25,13 +25,16 @@ typedef enum {
  * armv7m_load_kernel:
  * @cpu: CPU
  * @kernel_filename: file to load
+ * @mem_base: base address to load image at (should be where the
+ *            CPU expects to find its vector table on reset)
  * @mem_size: mem_size: maximum image size to load
  *
  * Load the guest image for an ARMv7M system. This must be called by
  * any ARMv7M board. (This is necessary to ensure that the CPU resets
  * correctly on system reset, as well as for kernel loading.)
  */
-void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename, int mem_size);
+void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename,
+                        hwaddr mem_base, int mem_size);
 
 /* arm_boot.c */
 struct arm_boot_info {
hw/arm/armv7m.c +3 -2
index fa4c2c735da..50a9507c0bd 100644
@@ -568,7 +568,8 @@ static void armv7m_reset(void *opaque)
     cpu_reset(CPU(cpu));
 }
 
-void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename, int mem_size)
+void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename,
+                        hwaddr mem_base, int mem_size)
 {
     ssize_t image_size;
     uint64_t entry;
@@ -588,7 +589,7 @@ void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename, int mem_size)
                                  &entry, NULL, NULL,
                                  NULL, 0, EM_ARM, 1, 0, as);
         if (image_size < 0) {
-            image_size = load_image_targphys_as(kernel_filename, 0,
+            image_size = load_image_targphys_as(kernel_filename, mem_base,
                                                 mem_size, as);
         }
         if (image_size < 0) {
hw/arm/aspeed.c +1 -0
index b3bbe06f8fa..bc3ecdb6199 100644
@@ -1430,6 +1430,7 @@ static void aspeed_minibmc_machine_init(MachineState *machine)
 
     armv7m_load_kernel(ARM_CPU(first_cpu),
                        machine->kernel_filename,
+                       0,
                        AST1030_INTERNAL_FLASH_SIZE);
 }
 
hw/arm/microbit.c +1 -1
index e9494334ce7..50df3620882 100644
@@ -57,7 +57,7 @@ static void microbit_init(MachineState *machine)
                                         mr, -1);
 
     armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
-                       s->nrf51.flash_size);
+                       0, s->nrf51.flash_size);
 }
 
 static void microbit_machine_class_init(ObjectClass *oc, void *data)
hw/arm/mps2-tz.c +1 -1
index 4017392bf5a..394192b9b20 100644
@@ -1197,7 +1197,7 @@ static void mps2tz_common_init(MachineState *machine)
     }
 
     armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
-                       boot_ram_size(mms));
+                       0, boot_ram_size(mms));
 }
 
 static void mps2_tz_idau_check(IDAUInterface *ii, uint32_t address,
hw/arm/mps2.c +1 -1
index bb76fa68890..a86a994dbac 100644
@@ -450,7 +450,7 @@ static void mps2_common_init(MachineState *machine)
                                   mmc->fpga_type == FPGA_AN511 ? 47 : 13));
 
     armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
-                       0x400000);
+                       0, 0x400000);
 }
 
 static void mps2_class_init(ObjectClass *oc, void *data)
hw/arm/msf2-som.c +1 -1
index d9f881690e0..a6df473ec90 100644
@@ -98,7 +98,7 @@ static void emcraft_sf2_s2s010_init(MachineState *machine)
     sysbus_connect_irq(SYS_BUS_DEVICE(&soc->spi[0]), 1, cs_line);
 
     armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
-                       soc->envm_size);
+                       0, soc->envm_size);
 }
 
 static void emcraft_sf2_machine_init(MachineClass *mc)
hw/arm/musca.c +2 -1
index 7a83f7dda7d..6eeee57c9dd 100644
@@ -597,7 +597,8 @@ static void musca_init(MachineState *machine)
                                                      "cfg_sec_resp", 0));
     }
 
-    armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename, 0x2000000);
+    armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
+                       0, 0x2000000);
 }
 
 static void musca_class_init(ObjectClass *oc, void *data)
hw/arm/netduino2.c +1 -1
index 3365da11bf7..83753d53a3f 100644
@@ -49,7 +49,7 @@ static void netduino2_init(MachineState *machine)
     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
 
     armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
-                       FLASH_SIZE);
+                       0, FLASH_SIZE);
 }
 
 static void netduino2_machine_init(MachineClass *mc)
hw/arm/netduinoplus2.c +1 -1
index 76cea8e4891..515c0816054 100644
@@ -50,7 +50,7 @@ static void netduinoplus2_init(MachineState *machine)
 
     armv7m_load_kernel(ARM_CPU(first_cpu),
                        machine->kernel_filename,
-                       FLASH_SIZE);
+                       0, FLASH_SIZE);
 }
 
 static void netduinoplus2_machine_init(MachineClass *mc)
hw/arm/stellaris.c +1 -1
index 12c673c9172..a9e96c37f89 100644
@@ -1302,7 +1302,7 @@ static void stellaris_init(MachineState *ms, stellaris_board_info *board)
     create_unimplemented_device("hibernation", 0x400fc000, 0x1000);
     create_unimplemented_device("flash-control", 0x400fd000, 0x1000);
 
-    armv7m_load_kernel(ARM_CPU(first_cpu), ms->kernel_filename, flash_size);
+    armv7m_load_kernel(ARM_CPU(first_cpu), ms->kernel_filename, 0, flash_size);
 }
 
 /* FIXME: Figure out how to generate these from stellaris_boards.  */
hw/arm/stm32vldiscovery.c +1 -1
index 04036da3ee0..67675e952fc 100644
@@ -53,7 +53,7 @@ static void stm32vldiscovery_init(MachineState *machine)
 
     armv7m_load_kernel(ARM_CPU(first_cpu),
                        machine->kernel_filename,
-                       FLASH_SIZE);
+                       0, FLASH_SIZE);
 }
 
 static void stm32vldiscovery_machine_init(MachineClass *mc)
Signature · 1 line
2.25.1
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] target/arm: Make boards pass base address to armv7m_load_kernel()

Quoted 9 lines · Peter Maydell

Currently armv7m_load_kernel() takes the size of the block of memory where it should load the initial guest image, but assumes that it should always load it at address 0. This happens to be true of all our M-profile boards at the moment, but it isn't guaranteed to always be so: M-profile CPUs can be configured (via init-svtor and init-nsvtor, which match equivalent hardware configuration signals) to have the initial vector table at any address, not just zero. (For instance the Teeny board has the boot ROM at address 0x0200_0000.)

Quoted 6 lines

Add a base address argument to armv7m_load_kernel(), so that callers now pass in both base address and size. All the current callers pass 0, so this is not a behaviour change.

---

r~

Reviewed-by Philippe Mathieu-Daudé on v1 2/2
#6 lore raw

Re: [PATCH 2/2] target/arm: Make boards pass base address to armv7m_load_kernel()

In reply to Peter Maydell
Quoted 9 lines · Peter Maydell

Currently armv7m_load_kernel() takes the size of the block of memory where it should load the initial guest image, but assumes that it should always load it at address 0. This happens to be true of all our M-profile boards at the moment, but it isn't guaranteed to always be so: M-profile CPUs can be configured (via init-svtor and init-nsvtor, which match equivalent hardware configuration signals) to have the initial vector table at any address, not just zero. (For instance the Teeny board has the boot ROM at address 0x0200_0000.)

Quoted 5 lines

Add a base address argument to armv7m_load_kernel(), so that callers now pass in both base address and size. All the current callers pass 0, so this is not a behaviour change.

Quoted 197 lines

---
I thought about having armv7m_load_kernel() be "clever" and ask the CPU what init-svtor/init-nsvtor were set to, but that seems like it might have unanticipated consequences. "Just pass the base address" is simpler and is how A-profile does it (though for A-profile it's the loader_start field in struct arm_boot_info rather than an extra argument).

---
  include/hw/arm/boot.h     | 5 ++++-
  hw/arm/armv7m.c           | 5 +++--
  hw/arm/aspeed.c           | 1 +
  hw/arm/microbit.c         | 2 +-
  hw/arm/mps2-tz.c          | 2 +-
  hw/arm/mps2.c             | 2 +-
  hw/arm/msf2-som.c         | 2 +-
  hw/arm/musca.c            | 3 ++-
  hw/arm/netduino2.c        | 2 +-
  hw/arm/netduinoplus2.c    | 2 +-
  hw/arm/stellaris.c        | 2 +-
  hw/arm/stm32vldiscovery.c | 2 +-
  12 files changed, 18 insertions(+), 12 deletions(-)
include/hw/arm/boot.h +4 -1
index c7ebae156ec..f18cc3064ff 100644
@@ -25,13 +25,16 @@ typedef enum {
   * armv7m_load_kernel:
   * @cpu: CPU
   * @kernel_filename: file to load
+ * @mem_base: base address to load image at (should be where the
+ *            CPU expects to find its vector table on reset)
   * @mem_size: mem_size: maximum image size to load
   *
   * Load the guest image for an ARMv7M system. This must be called by
   * any ARMv7M board. (This is necessary to ensure that the CPU resets
   * correctly on system reset, as well as for kernel loading.)
   */
-void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename, int mem_size);
+void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename,
+                        hwaddr mem_base, int mem_size);
 
  /* arm_boot.c */
  struct arm_boot_info {
hw/arm/armv7m.c +3 -2
index fa4c2c735da..50a9507c0bd 100644
@@ -568,7 +568,8 @@ static void armv7m_reset(void *opaque)
      cpu_reset(CPU(cpu));
  }
 
-void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename, int mem_size)
+void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename,
+                        hwaddr mem_base, int mem_size)
  {
      ssize_t image_size;
      uint64_t entry;
@@ -588,7 +589,7 @@ void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename, int mem_size)
                                   &entry, NULL, NULL,
                                   NULL, 0, EM_ARM, 1, 0, as);
          if (image_size < 0) {
-            image_size = load_image_targphys_as(kernel_filename, 0,
+            image_size = load_image_targphys_as(kernel_filename, mem_base,
                                                  mem_size, as);
          }
          if (image_size < 0) {
hw/arm/aspeed.c +1 -0
index b3bbe06f8fa..bc3ecdb6199 100644
@@ -1430,6 +1430,7 @@ static void aspeed_minibmc_machine_init(MachineState *machine)
 
      armv7m_load_kernel(ARM_CPU(first_cpu),
                         machine->kernel_filename,
+                       0,
                         AST1030_INTERNAL_FLASH_SIZE);
  }
 
hw/arm/microbit.c +1 -1
index e9494334ce7..50df3620882 100644
@@ -57,7 +57,7 @@ static void microbit_init(MachineState *machine)
                                          mr, -1);
 
      armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
-                       s->nrf51.flash_size);
+                       0, s->nrf51.flash_size);
  }
 
  static void microbit_machine_class_init(ObjectClass *oc, void *data)
hw/arm/mps2-tz.c +1 -1
index 4017392bf5a..394192b9b20 100644
@@ -1197,7 +1197,7 @@ static void mps2tz_common_init(MachineState *machine)
      }
 
      armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
-                       boot_ram_size(mms));
+                       0, boot_ram_size(mms));
  }
 
  static void mps2_tz_idau_check(IDAUInterface *ii, uint32_t address,
hw/arm/mps2.c +1 -1
index bb76fa68890..a86a994dbac 100644
@@ -450,7 +450,7 @@ static void mps2_common_init(MachineState *machine)
                                    mmc->fpga_type == FPGA_AN511 ? 47 : 13));
 
      armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
-                       0x400000);
+                       0, 0x400000);
  }
 
  static void mps2_class_init(ObjectClass *oc, void *data)
hw/arm/msf2-som.c +1 -1
index d9f881690e0..a6df473ec90 100644
@@ -98,7 +98,7 @@ static void emcraft_sf2_s2s010_init(MachineState *machine)
      sysbus_connect_irq(SYS_BUS_DEVICE(&soc->spi[0]), 1, cs_line);
 
      armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
-                       soc->envm_size);
+                       0, soc->envm_size);
  }
 
  static void emcraft_sf2_machine_init(MachineClass *mc)
hw/arm/musca.c +2 -1
index 7a83f7dda7d..6eeee57c9dd 100644
@@ -597,7 +597,8 @@ static void musca_init(MachineState *machine)
                                                       "cfg_sec_resp", 0));
      }
 
-    armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename, 0x2000000);
+    armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
+                       0, 0x2000000);
  }
 
  static void musca_class_init(ObjectClass *oc, void *data)
hw/arm/netduino2.c +1 -1
index 3365da11bf7..83753d53a3f 100644
@@ -49,7 +49,7 @@ static void netduino2_init(MachineState *machine)
      sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
 
      armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
-                       FLASH_SIZE);
+                       0, FLASH_SIZE);
  }
 
  static void netduino2_machine_init(MachineClass *mc)
hw/arm/netduinoplus2.c +1 -1
index 76cea8e4891..515c0816054 100644
@@ -50,7 +50,7 @@ static void netduinoplus2_init(MachineState *machine)
 
      armv7m_load_kernel(ARM_CPU(first_cpu),
                         machine->kernel_filename,
-                       FLASH_SIZE);
+                       0, FLASH_SIZE);
  }
 
  static void netduinoplus2_machine_init(MachineClass *mc)
hw/arm/stellaris.c +1 -1
index 12c673c9172..a9e96c37f89 100644
@@ -1302,7 +1302,7 @@ static void stellaris_init(MachineState *ms, stellaris_board_info *board)
      create_unimplemented_device("hibernation", 0x400fc000, 0x1000);
      create_unimplemented_device("flash-control", 0x400fd000, 0x1000);
 
-    armv7m_load_kernel(ARM_CPU(first_cpu), ms->kernel_filename, flash_size);
+    armv7m_load_kernel(ARM_CPU(first_cpu), ms->kernel_filename, 0, flash_size);
  }
 
  /* FIXME: Figure out how to generate these from stellaris_boards.  */
hw/arm/stm32vldiscovery.c +1 -1
index 04036da3ee0..67675e952fc 100644
@@ -53,7 +53,7 @@ static void stm32vldiscovery_init(MachineState *machine)
 
      armv7m_load_kernel(ARM_CPU(first_cpu),
                         machine->kernel_filename,
-                       FLASH_SIZE);
+                       0, FLASH_SIZE);
  }
 
  static void stm32vldiscovery_machine_init(MachineClass *mc)
#7 lore raw
In reply to Peter Maydell
Quoted 15 lines · Peter Maydell

Two small patches to armv7m_load_kernel(). The first is just getting rid of some dead code, that I noticed while working on the function. The second is to make boards pass armv7m_load_kernel() the base address for loading guest (non-ELF) binaries. At the moment we assume all M-profile boards start at address 0; this happens to be true for all the ones we implement right now, but it's not true in general. In particular the Teeny board has its ROM at 0x0020_0000.

I thought about having armv7m_load_kernel() be "clever" and ask the CPU what init-svtor/init-nsvtor were set to, but that seems like it might have unanticipated consequences[*]. "Just pass the base address" is simpler and is how A-profile does it (though for A-profile it's the loader_start field in struct arm_boot_info rather than an extra argument).

Quoted 3 lines

[*] eg where the board has the rom/flash aliased at both address 0 and some other address, and init-svtor points at an alias; also Secure vs NonSecure address spaces and loading...

Thanks, queued to target-arm.next.

r~