Skip to contentSkip to conversation

arm: add skeleton Cortex M0 CPU model

PATCH In review Stefan Hajnoczi 3 participants 10 messages Jun 30, 2018 – Jul 11, 2018 Arm/versatile express platform

lore
Share & tools
lore
#0 lore raw

v2 cover arm: add skeleton Cortex M0 CPU model

v2:
* Share code between ARMv6-M and ARMv7-M [Peter]

This patch series adds a skeleton ARM Cortex M0 CPU model and an ARMv6M object so that machine types can make use of it.

These patches cannot be merged until Julia Suvorova's Cortex M0 work is complete, since the target/arm/ code does not implement ARMv6-M behavior faithfully yet.

The micro:bit emulation will be the first machine type to make use of ARMv6-M.

Stefan Hajnoczi (3):
hw/arm: extract ARM M Profile base class from ARMv7-M hw/arm: add ARMv6M object
target/arm: add "cortex-m0" CPU model

 hw/arm/Makefile.objs            |   2 +
 include/hw/arm/arm-m-profile.h  |  65 +++++++++++
 include/hw/arm/arm.h            |  11 +-
 include/hw/arm/armv6m.h         |  24 ++++
 include/hw/arm/armv7m.h         |  22 +---
 hw/arm/arm-m-profile.c          | 192 ++++++++++++++++++++++++++++++++
 hw/arm/armv6m.c                 |  23 ++++
 hw/arm/armv7m.c                 | 166 ++++++---------------------
 hw/arm/mps2-tz.c                |   3 +-
 hw/arm/mps2.c                   |   4 +-
 hw/arm/msf2-soc.c               |   4 +-
 hw/arm/msf2-som.c               |   4 +-
 hw/arm/netduino2.c              |   4 +-
 hw/arm/stellaris.c              |   3 +-
 target/arm/cpu.c                |  11 ++
 default-configs/arm-softmmu.mak |   2 +
 16 files changed, 374 insertions(+), 166 deletions(-)
 create mode 100644 include/hw/arm/arm-m-profile.h
 create mode 100644 include/hw/arm/armv6m.h
 create mode 100644 hw/arm/arm-m-profile.c
 create mode 100644 hw/arm/armv6m.c
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/3 hw/arm: extract ARM M Profile base class from ARMv7-M

The ARMv7-M code is largely similar to what other M Profile CPUs need. Extract the common M Profile aspects into the ARMMProfileState base class. ARMv6-M will inherit from this class in the following patch.

It might be possible to make ARMv6-M the base class of ARMv7-M, but it seems cleaner to have an M Profile base class instead of saying an "ARMv7-M is an ARMv6-M".

---
 hw/arm/Makefile.objs            |   1 +
 include/hw/arm/arm-m-profile.h  |  65 +++++++++++
 include/hw/arm/arm.h            |  11 +-
 include/hw/arm/armv7m.h         |  22 +---
 hw/arm/arm-m-profile.c          | 192 ++++++++++++++++++++++++++++++++
 hw/arm/armv7m.c                 | 166 ++++++---------------------
 hw/arm/mps2-tz.c                |   3 +-
 hw/arm/mps2.c                   |   4 +-
 hw/arm/msf2-soc.c               |   4 +-
 hw/arm/msf2-som.c               |   4 +-
 hw/arm/netduino2.c              |   4 +-
 hw/arm/stellaris.c              |   3 +-
 default-configs/arm-softmmu.mak |   1 +
 13 files changed, 314 insertions(+), 166 deletions(-)
 create mode 100644 include/hw/arm/arm-m-profile.h
 create mode 100644 hw/arm/arm-m-profile.c
hw/arm/Makefile.objs +1 -0
index d51fcecaf2..2c43d34c64 100644
@@ -16,6 +16,7 @@ obj-$(CONFIG_STRONGARM) += collie.o
 obj-$(CONFIG_VERSATILE) += vexpress.o versatilepb.o
 obj-$(CONFIG_ZYNQ) += xilinx_zynq.o
 
+obj-$(CONFIG_ARM_M_PROFILE) += arm-m-profile.o
 obj-$(CONFIG_ARM_V7M) += armv7m.o
 obj-$(CONFIG_EXYNOS4) += exynos4210.o
 obj-$(CONFIG_PXA2XX) += pxa2xx.o pxa2xx_gpio.o pxa2xx_pic.o
include/hw/arm/arm-m-profile.h new file +65 -0

65 lines not on this page · raw message

include/hw/arm/arm.h +6 -5
index ffed39252d..2b919e57ee 100644
@@ -24,16 +24,17 @@ typedef enum {
 } arm_endianness;
 
 /**
- * armv7m_load_kernel:
+ * arm_m_profile_load_kernel:
  * @cpu: CPU
  * @kernel_filename: file to load
- * @mem_size: mem_size: maximum image size to load
+ * @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
+ * Load the guest image for an ARM M Profile system. This must be called by
+ * any ARM M Profile 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 arm_m_profile_load_kernel(ARMCPU *cpu, const char *kernel_filename,
+                               int mem_size);
 
 /* arm_boot.c */
 struct arm_boot_info {
include/hw/arm/armv7m.h +2 -20
index 78308d1484..c9b6ab37e7 100644
@@ -10,8 +10,7 @@
 #ifndef HW_ARM_ARMV7M_H
 #define HW_ARM_ARMV7M_H
 
-#include "hw/sysbus.h"
-#include "hw/intc/armv7m_nvic.h"
+#include "hw/arm/arm-m-profile.h"
 #include "target/arm/idau.h"
 
 #define TYPE_BITBAND "ARM,bitband-memory"
@@ -34,33 +33,16 @@ typedef struct {
 #define ARMV7M_NUM_BITBANDS 2
 
 /* ARMv7M container object.
- * + Unnamed GPIO input lines: external IRQ lines for the NVIC
- * + Named GPIO output SYSRESETREQ: signalled for guest AIRCR.SYSRESETREQ
- * + Property "cpu-type": CPU type to instantiate
- * + Property "num-irq": number of external IRQ lines
- * + Property "memory": MemoryRegion defining the physical address space
- *   that CPU accesses see. (The NVIC, bitbanding and other CPU-internal
- *   devices will be automatically layered on top of this view.)
  * + Property "idau": IDAU interface (forwarded to CPU object)
  * + Property "init-svtor": secure VTOR reset value (forwarded to CPU object)
  */
 typedef struct ARMv7MState {
     /*< private >*/
-    SysBusDevice parent_obj;
+    ARMMProfileState parent_obj;
     /*< public >*/
-    NVICState nvic;
     BitBandState bitband[ARMV7M_NUM_BITBANDS];
-    ARMCPU *cpu;
-
-    /* MemoryRegion we pass to the CPU, with our devices layered on
-     * top of the ones the board provides in board_memory.
-     */
-    MemoryRegion container;
 
     /* Properties */
-    char *cpu_type;
-    /* MemoryRegion the board provides to us (with its devices, RAM, etc) */
-    MemoryRegion *board_memory;
     Object *idau;
     uint32_t init_svtor;
 } ARMv7MState;
hw/arm/arm-m-profile.c new file +192 -0

192 lines not on this page · raw message

hw/arm/armv7m.c +34 -132

223 lines not on this page · raw message

hw/arm/mps2-tz.c +2 -1
index 22180c56fb..af10ee0cc9 100644
@@ -487,7 +487,8 @@ static void mps2tz_common_init(MachineState *machine)
 
     create_unimplemented_device("FPGA NS PC", 0x48007000, 0x1000);
 
-    armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename, 0x400000);
+    arm_m_profile_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
+                              0x400000);
 }
 
 static void mps2tz_class_init(ObjectClass *oc, void *data)
hw/arm/mps2.c +2 -2
index c3946da317..bcc7070104 100644
@@ -315,8 +315,8 @@ static void mps2_common_init(MachineState *machine)
 
     system_clock_scale = NANOSECONDS_PER_SECOND / SYSCLK_FRQ;
 
-    armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
-                       0x400000);
+    arm_m_profile_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
+                              0x400000);
 }
 
 static void mps2_class_init(ObjectClass *oc, void *data)
hw/arm/msf2-soc.c +3 -1
index 75c44adf7d..2ac870d17b 100644
@@ -88,6 +88,7 @@ static void m2sxxx_soc_realize(DeviceState *dev_soc, Error **errp)
 {
     MSF2State *s = MSF2_SOC(dev_soc);
     DeviceState *dev, *armv7m;
+    ARMMProfileState *arm_m_profile;
     SysBusDevice *busdev;
     Error *err = NULL;
     int i;
@@ -116,6 +117,7 @@ static void m2sxxx_soc_realize(DeviceState *dev_soc, Error **errp)
     memory_region_add_subregion(system_memory, SRAM_BASE_ADDRESS, sram);
 
     armv7m = DEVICE(&s->armv7m);
+    arm_m_profile = ARM_M_PROFILE(armv7m);
     qdev_prop_set_uint32(armv7m, "num-irq", 81);
     qdev_prop_set_string(armv7m, "cpu-type", s->cpu_type);
     object_property_set_link(OBJECT(&s->armv7m), OBJECT(get_system_memory()),
@@ -132,7 +134,7 @@ static void m2sxxx_soc_realize(DeviceState *dev_soc, Error **errp)
         return;
     }
 
-    qdev_connect_gpio_out_named(DEVICE(&s->armv7m.nvic), "SYSRESETREQ", 0,
+    qdev_connect_gpio_out_named(DEVICE(&arm_m_profile->nvic), "SYSRESETREQ", 0,
                                 qemu_allocate_irq(&do_sys_reset, NULL, 0));
 
     system_clock_scale = NANOSECONDS_PER_SECOND / s->m3clk;
hw/arm/msf2-som.c +2 -2
index 0795a3a3a1..8eb7564f0a 100644
@@ -91,8 +91,8 @@ static void emcraft_sf2_s2s010_init(MachineState *machine)
     cs_line = qdev_get_gpio_in_named(spi_flash, SSI_GPIO_CS, 0);
     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);
+    arm_m_profile_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
+                              soc->envm_size);
 }
 
 static void emcraft_sf2_machine_init(MachineClass *mc)
hw/arm/netduino2.c +2 -2
index f936017d4a..e18d377f94 100644
@@ -37,8 +37,8 @@ static void netduino2_init(MachineState *machine)
     qdev_prop_set_string(dev, "cpu-type", ARM_CPU_TYPE_NAME("cortex-m3"));
     object_property_set_bool(OBJECT(dev), true, "realized", &error_fatal);
 
-    armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
-                       FLASH_SIZE);
+    arm_m_profile_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
+                              FLASH_SIZE);
 }
 
 static void netduino2_machine_init(MachineClass *mc)
hw/arm/stellaris.c +2 -1
index dc521b4a5a..68e52367c0 100644
@@ -1440,7 +1440,8 @@ 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);
+    arm_m_profile_load_kernel(ARM_CPU(first_cpu), ms->kernel_filename,
+                              flash_size);
 }
 
 /* FIXME: Figure out how to generate these from stellaris_boards.  */
default-configs/arm-softmmu.mak +1 -0
index 834d45cfaf..e704cb6e34 100644
@@ -48,6 +48,7 @@ CONFIG_ARM11MPCORE=y
 CONFIG_A9MPCORE=y
 CONFIG_A15MPCORE=y
 
+CONFIG_ARM_M_PROFILE=y
 CONFIG_ARM_V7M=y
 CONFIG_NETDUINO2=y
 
Signature · 1 line
2.17.1
#2 lore raw

Re: [Qemu-arm] [PATCH v2 1/3] hw/arm: extract ARM M Profile base class from ARMv7-M

Quoted 9 lines · Stefan Hajnoczi

The ARMv7-M code is largely similar to what other M Profile CPUs need. Extract the common M Profile aspects into the ARMMProfileState base class. ARMv6-M will inherit from this class in the following patch.

It might be possible to make ARMv6-M the base class of ARMv7-M, but it seems cleaner to have an M Profile base class instead of saying an "ARMv7-M is an ARMv6-M".

This makes sense, I guess (though it currently leaves us in the odd position that we have separate a object for v6m, but the v7m object handles both v7m and v8m...)

Quoted 14 lines

/**
- * armv7m_load_kernel:
+ * arm_m_profile_load_kernel:
* @cpu: CPU
* @kernel_filename: file to load
- * @mem_size: mem_size: maximum image size to load
+ * @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
+ * Load the guest image for an ARM M Profile system. This must be called by
+ * any ARM M Profile board. (This is necessary to ensure that the CPU resets
* correctly on system reset, as well as for kernel loading.)
*/

include/hw/arm/arm.h typedef enum { · line 37

Quoted 3 lines
include/hw/arm/arm.h +2 -1
-void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename, int mem_size);
+void arm_m_profile_load_kernel(ARMCPU *cpu, const char *kernel_filename,
+                               int mem_size);

This is presumably just a code movement and rename, but it's a bit hard to confirm that. Could you split it out into its own patch, please?

thanks
-- PMM

#3 lore raw

Re: [Qemu-arm] [PATCH v2 1/3] hw/arm: extract ARM M Profile base class from ARMv7-M

Quoted 9 lines · Peter Maydell
Quoted 8 lines · Stefan Hajnoczi

The ARMv7-M code is largely similar to what other M Profile CPUs need. Extract the common M Profile aspects into the ARMMProfileState base class. ARMv6-M will inherit from this class in the following patch.

It might be possible to make ARMv6-M the base class of ARMv7-M, but it seems cleaner to have an M Profile base class instead of saying an "ARMv7-M is an ARMv6-M".

Quoted 5 lines
Quoted 1 line

This makes sense, I guess (though it currently leaves us in the odd position that we have separate a object for v6m, but the v7m object handles both v7m and v8m...)

...though I guess the counter-argument is that the only thing that the v7m object is doing that v6m doesn't want is creating the bitbanding device, and in fact bitbanding is optional in v7m (you can configure a Cortex-M3 without it). So maybe we should instead just have a QOM property to let you turn off the bitbanding ?

thanks
-- PMM

#4 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH v2 1/3] hw/arm: extract ARM M Profile base class from ARMv7-M

Quoted 16 lines · Peter Maydell
Quoted 9 lines · Peter Maydell
Quoted 8 lines · Stefan Hajnoczi

The ARMv7-M code is largely similar to what other M Profile CPUs need. Extract the common M Profile aspects into the ARMMProfileState base class. ARMv6-M will inherit from this class in the following patch.

It might be possible to make ARMv6-M the base class of ARMv7-M, but it seems cleaner to have an M Profile base class instead of saying an "ARMv7-M is an ARMv6-M".

Quoted 5 lines
Quoted 1 line

This makes sense, I guess (though it currently leaves us in the odd position that we have separate a object for v6m, but the v7m object handles both v7m and v8m...)

Quoted 6 lines

...though I guess the counter-argument is that the only thing that the v7m object is doing that v6m doesn't want is creating the bitbanding device, and in fact bitbanding is optional in v7m (you can configure a Cortex-M3 without it). So maybe we should instead just have a QOM property to let you turn off the bitbanding ?

Okay, we can do that. So how about a single ARMMProfileState class for v6, v7, and v8?

Stefan

#5 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH v2 1/3] hw/arm: extract ARM M Profile base class from ARMv7-M

Quoted 24 lines · Stefan Hajnoczi
Quoted 16 lines · Peter Maydell
Quoted 9 lines · Peter Maydell
Quoted 8 lines · Stefan Hajnoczi

The ARMv7-M code is largely similar to what other M Profile CPUs need. Extract the common M Profile aspects into the ARMMProfileState base class. ARMv6-M will inherit from this class in the following patch.

It might be possible to make ARMv6-M the base class of ARMv7-M, but it seems cleaner to have an M Profile base class instead of saying an "ARMv7-M is an ARMv6-M".

Quoted 5 lines
Quoted 1 line

This makes sense, I guess (though it currently leaves us in the odd position that we have separate a object for v6m, but the v7m object handles both v7m and v8m...)

Quoted 6 lines

...though I guess the counter-argument is that the only thing that the v7m object is doing that v6m doesn't want is creating the bitbanding device, and in fact bitbanding is optional in v7m (you can configure a Cortex-M3 without it). So maybe we should instead just have a QOM property to let you turn off the bitbanding ?

Quoted 2 lines

Okay, we can do that. So how about a single ARMMProfileState class for v6, v7, and v8?

Yes, I think that makes sense. (A lot of our code says 'v7m' when it really means 'm profile in general' for historical reasons. I'm not too worried about trying to tidy that up.)

thanks
-- PMM

#6 lore raw

PATCH v2 2/3 hw/arm: add ARMv6M object

In reply to Stefan Hajnoczi

The ARMv6M object will be used by machine types that need ARMv6-M CPUs.

There is no logic beyond what the ARMMProfile base class already provides because ARMv6-M is a pretty small microcontroller without many bells and whistles.

---
 hw/arm/Makefile.objs            |  1 +
 include/hw/arm/armv6m.h         | 24 ++++++++++++++++++++++++
 hw/arm/armv6m.c                 | 23 +++++++++++++++++++++++
 default-configs/arm-softmmu.mak |  1 +
 4 files changed, 49 insertions(+)
 create mode 100644 include/hw/arm/armv6m.h
 create mode 100644 hw/arm/armv6m.c
hw/arm/Makefile.objs +1 -0
index 2c43d34c64..e693d86cd3 100644
@@ -17,6 +17,7 @@ obj-$(CONFIG_VERSATILE) += vexpress.o versatilepb.o
 obj-$(CONFIG_ZYNQ) += xilinx_zynq.o
 
 obj-$(CONFIG_ARM_M_PROFILE) += arm-m-profile.o
+obj-$(CONFIG_ARM_V6M) += armv6m.o
 obj-$(CONFIG_ARM_V7M) += armv7m.o
 obj-$(CONFIG_EXYNOS4) += exynos4210.o
 obj-$(CONFIG_PXA2XX) += pxa2xx.o pxa2xx_gpio.o pxa2xx_pic.o
include/hw/arm/armv6m.h new file +24 -0
new file mode 100644
index 0000000000..5f80120266
@@ -0,0 +1,24 @@
+/*
+ * ARMv6M CPU object
+ *
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * This code is licensed under the GPL version 2 or later.
+ */
+
+#ifndef HW_ARM_ARMV6M_H
+#define HW_ARM_ARMV6M_H
+
+#include "hw/arm/arm-m-profile.h"
+
+#define TYPE_ARMV6M "armv6m"
+#define ARMV6M(obj) OBJECT_CHECK(ARMv6MState, (obj), TYPE_ARMV6M)
+
+/* ARMv6M container object.
+ */
+typedef struct ARMv6MState {
+    /*< private >*/
+    ARMMProfileState parent_obj;
+} ARMv6MState;
+
+#endif
hw/arm/armv6m.c new file +23 -0
new file mode 100644
index 0000000000..89c4727a87
@@ -0,0 +1,23 @@
+/*
+ * ARMv6M CPU object
+ *
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * This code is licensed under the GPL version 2 or later.
+ */
+
+#include "qemu/osdep.h"
+#include "hw/arm/armv6m.h"
+
+static const TypeInfo armv6m_info = {
+    .name = TYPE_ARMV6M,
+    .parent = TYPE_ARM_M_PROFILE,
+    .instance_size = sizeof(ARMv6MState),
+};
+
+static void armv6m_register_types(void)
+{
+    type_register_static(&armv6m_info);
+}
+
+type_init(armv6m_register_types)
default-configs/arm-softmmu.mak +1 -0
index e704cb6e34..a381817126 100644
@@ -49,6 +49,7 @@ CONFIG_A9MPCORE=y
 CONFIG_A15MPCORE=y
 
 CONFIG_ARM_M_PROFILE=y
+CONFIG_ARM_V6M=y
 CONFIG_ARM_V7M=y
 CONFIG_NETDUINO2=y
 
Signature · 1 line
2.17.1
Reviewed-by on v2 2/3
#7 lore raw

Re: [Qemu-arm] [PATCH v2 2/3] hw/arm: add ARMv6M object

Quoted 7 lines · Stefan Hajnoczi

The ARMv6M object will be used by machine types that need ARMv6-M CPUs.

There is no logic beyond what the ARMMProfile base class already provides because ARMv6-M is a pretty small microcontroller without many bells and whistles.

thanks
-- PMM

#8 lore raw

PATCH v2 3/3 target/arm: add "cortex-m0" CPU model

In reply to Stefan Hajnoczi

Define a "cortex-m0" ARMv6-M CPU model.

Most of the register reset values set by other CPU models are not relevant for the cut-down ARMv6-M architecture.

---
 target/arm/cpu.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
target/arm/cpu.c +11 -0
index 2ae4fffafb..10951fa0eb 100644
@@ -1233,6 +1233,15 @@ static void arm11mpcore_initfn(Object *obj)
     cpu->reset_auxcr = 1;
 }
 
+static void cortex_m0_initfn(Object *obj)
+{
+    ARMCPU *cpu = ARM_CPU(obj);
+    set_feature(&cpu->env, ARM_FEATURE_V6);
+    set_feature(&cpu->env, ARM_FEATURE_M);
+
+    cpu->midr = 0x410cc200;
+}
+
 static void cortex_m3_initfn(Object *obj)
 {
     ARMCPU *cpu = ARM_CPU(obj);
@@ -1825,6 +1834,8 @@ static const ARMCPUInfo arm_cpus[] = {
     { .name = "arm1136",     .initfn = arm1136_initfn },
     { .name = "arm1176",     .initfn = arm1176_initfn },
     { .name = "arm11mpcore", .initfn = arm11mpcore_initfn },
+    { .name = "cortex-m0",   .initfn = cortex_m0_initfn,
+                             .class_init = arm_v7m_class_init },
     { .name = "cortex-m3",   .initfn = cortex_m3_initfn,
                              .class_init = arm_v7m_class_init },
     { .name = "cortex-m4",   .initfn = cortex_m4_initfn,
Signature · 1 line
2.17.1