Skip to contentSkip to conversation

target-arm: Support AArch64 KVM

PATCH In review Peter Maydell 3 participants 13 messages Dec 17, 2013 – Dec 18, 2013

lore
Share & tools
lore
#0 lore raw

v2 cover target-arm: Support AArch64 KVM

This patchset adds support for basic AArch64 KVM VM control; it's based on current master. This is a quick resend with the very minor nits Christoffer pointed out fixed; I'm planning to put it into a target-arm pullreq in the next day or two.

This patch series supports:
* 64 bit KVM VM control
* SMP and UP
* PSCI boot of secondary CPUs
It doesn't support:
* migration
* reset (partly because there's no way to reset a mach-virt system yet)
* anything except "-cpu host"
* debugging the VM via qemu gdbstub
* running 32 bit VMs on a 64 bit system
[Mian's patchset includes support for that but I have left it out for the moment because it needs more thought about UI and so on]

Changes v1->v2:
* improved a couple of comments
* compat string for 64 bit is "arm,arm-v8", not -v7
* removed superfluous include of usb.mak and pci.mak from config

Mian M. Hamayun (2):
target-arm: Add minimal KVM AArch64 support hw/arm/boot: Add boot support for AArch64 processor

Peter Maydell (5):
target-arm/kvm: Split 32 bit only code into its own file
target-arm: Clean up handling of AArch64 PSTATE
configure: Enable KVM for aarch64 host/target combination hw/arm/boot: Allow easier swapping in of different loader code
default-configs: Add config for aarch64-softmmu

 configure                           |   2 +-
 default-configs/aarch64-softmmu.mak |   6 +
 hw/arm/boot.c                       | 193 ++++++++++----
 linux-user/signal.c                 |   6 +-
 target-arm/Makefile.objs            |   2 +
 target-arm/cpu.c                    |   6 +
 target-arm/cpu.h                    |  70 ++++-
 target-arm/gdbstub64.c              |   4 +-
 target-arm/kvm.c                    | 495 +---------------------------------
 target-arm/kvm32.c                  | 515 ++++++++++++++++++++++++++++++++++++
 target-arm/kvm64.c                  | 204 ++++++++++++++
 target-arm/translate-a64.c          |  12 +-
 12 files changed, 954 insertions(+), 561 deletions(-)
 create mode 100644 default-configs/aarch64-softmmu.mak
 create mode 100644 target-arm/kvm32.c
 create mode 100644 target-arm/kvm64.c
Signature · 1 line
1.8.5
#1 lore raw

PATCH v2 1/7 target-arm/kvm: Split 32 bit only code into its own file

Split ARM KVM support code which is 32 bit specific out into its own file, which we only compile on 32 bit hosts. This will give us a place to add the 64 bit support code without adding lots of ifdefs to kvm.c.

Message-id: [email protected]

---
 target-arm/Makefile.objs |   1 +
 target-arm/kvm.c         | 491 --------------------------------------------
 target-arm/kvm32.c       | 515 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 516 insertions(+), 491 deletions(-)
 create mode 100644 target-arm/kvm32.c
target-arm/Makefile.objs +1 -0
index 356fbfc..d1db77c 100644
@@ -6,3 +6,4 @@ obj-y += translate.o op_helper.o helper.o cpu.o
 obj-y += neon_helper.o iwmmxt_helper.o
 obj-y += gdbstub.o
 obj-$(TARGET_AARCH64) += cpu64.o translate-a64.o gdbstub64.o
+obj-$(call land,$(CONFIG_KVM),$(call lnot,$(TARGET_AARCH64))) += kvm32.o
target-arm/kvm.c +0 -491

515 lines not on this page · raw message

target-arm/kvm32.c new file +515 -0

515 lines not on this page · raw message

Signature · 1 line
1.8.5
#2 lore raw

PATCH v2 2/7 target-arm: Clean up handling of AArch64 PSTATE

In reply to Peter Maydell

The env->pstate field is a little odd since it doesn't strictly speaking represent an architectural register. However it's convenient for QEMU to use it to hold the various PSTATE architectural bits in the same format the architecture specifies for SPSR registers (since this is the same format the kernel uses for signal handlers and the KVM register). Add some structure to how we deal with it:
* document what env->pstate is
* add some #defines for various bits in it
* add helpers for reading/writing it taking account of caching of NZCV, and use them where appropriate
* reset it on startup

Message-id: [email protected]

---
 linux-user/signal.c        |  6 ++--
 target-arm/cpu.c           |  6 ++++
 target-arm/cpu.h           | 70 ++++++++++++++++++++++++++++++++++++++--------
 target-arm/gdbstub64.c     |  4 +--
 target-arm/translate-a64.c | 12 ++++----
 5 files changed, 78 insertions(+), 20 deletions(-)
linux-user/signal.c +4 -2
index 7751c47..4e7148a 100644
@@ -1171,7 +1171,7 @@ static int target_setup_sigframe(struct target_rt_sigframe *sf,
     }
     __put_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
     __put_user(env->pc, &sf->uc.tuc_mcontext.pc);
-    __put_user(env->pstate, &sf->uc.tuc_mcontext.pstate);
+    __put_user(pstate_read(env), &sf->uc.tuc_mcontext.pstate);
 
     __put_user(/*current->thread.fault_address*/ 0,
             &sf->uc.tuc_mcontext.fault_address);
@@ -1210,6 +1210,7 @@ static int target_restore_sigframe(CPUARMState *env,
     struct target_aux_context *aux =
         (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved;
     uint32_t magic, size;
+    uint64_t pstate;
 
     target_to_host_sigset(&set, &sf->uc.tuc_sigmask);
     sigprocmask(SIG_SETMASK, &set, NULL);
@@ -1220,7 +1221,8 @@ static int target_restore_sigframe(CPUARMState *env,
 
     __get_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
     __get_user(env->pc, &sf->uc.tuc_mcontext.pc);
-    __get_user(env->pstate, &sf->uc.tuc_mcontext.pstate);
+    __get_user(pstate, &sf->uc.tuc_mcontext.pstate);
+    pstate_write(env, pstate);
 
     __get_user(magic, &aux->fpsimd.head.magic);
     __get_user(size, &aux->fpsimd.head.size);
target-arm/cpu.c +6 -0
index 0635e78..42057ad 100644
@@ -88,6 +88,12 @@ static void arm_cpu_reset(CPUState *s)
     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
         /* 64 bit CPUs always start in 64 bit mode */
         env->aarch64 = 1;
+#if defined(CONFIG_USER_ONLY)
+        env->pstate = PSTATE_MODE_EL0t;
+#else
+        env->pstate = PSTATE_D | PSTATE_A | PSTATE_I | PSTATE_F
+            | PSTATE_MODE_EL1h;
+#endif
     }
 
 #if defined(CONFIG_USER_ONLY)
target-arm/cpu.h +59 -11

88 lines not on this page · raw message

target-arm/gdbstub64.c +2 -2
index 7cb6a7c..e8a8295 100644
@@ -37,7 +37,7 @@ int aarch64_cpu_gdb_read_register(CPUState *cs, uint8_t *mem_buf, int n)
         return gdb_get_reg64(mem_buf, env->pc);
         break;
     case 33:
-        return gdb_get_reg32(mem_buf, env->pstate);
+        return gdb_get_reg32(mem_buf, pstate_read(env));
     }
     /* Unknown register.  */
     return 0;
@@ -65,7 +65,7 @@ int aarch64_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
         return 8;
     case 33:
         /* CPSR */
-        env->pstate = tmp;
+        pstate_write(env, tmp);
         return 4;
     }
     /* Unknown register.  */
target-arm/translate-a64.c +7 -5
index f120088..932b601 100644
@@ -67,6 +67,7 @@ void aarch64_cpu_dump_state(CPUState *cs, FILE *f,
 {
     ARMCPU *cpu = ARM_CPU(cs);
     CPUARMState *env = &cpu->env;
+    uint32_t psr = pstate_read(env);
     int i;
 
     cpu_fprintf(f, "PC=%016"PRIx64"  SP=%016"PRIx64"\n",
@@ -79,11 +80,12 @@ void aarch64_cpu_dump_state(CPUState *cs, FILE *f,
             cpu_fprintf(f, " ");
         }
     }
-    cpu_fprintf(f, "PSTATE=%c%c%c%c\n",
-        env->pstate & PSTATE_N ? 'n' : '.',
-        env->pstate & PSTATE_Z ? 'z' : '.',
-        env->pstate & PSTATE_C ? 'c' : '.',
-        env->pstate & PSTATE_V ? 'v' : '.');
+    cpu_fprintf(f, "PSTATE=%08x (flags %c%c%c%c)\n",
+                psr,
+                psr & PSTATE_N ? 'N' : '-',
+                psr & PSTATE_Z ? 'Z' : '-',
+                psr & PSTATE_C ? 'C' : '-',
+                psr & PSTATE_V ? 'V' : '-');
     cpu_fprintf(f, "\n");
 }
 
Signature · 1 line
1.8.5
#3 lore raw

PATCH v2 3/7 target-arm: Add minimal KVM AArch64 support

In reply to Peter Maydell

From: "Mian M. Hamayun" <[email protected]>

Add the bare minimum set of functions needed for control of an AArch64 KVM vcpu:
* CPU initialization
* minimal get/put register functions which only handle the basic state of the CPU

Message-id: [email protected] [PMM: significantly overhauled; most notably:
* code lives in kvm64.c rather than using #ifdefs
* support '-cpu host' rather than implicitly using whatever the host's CPU is regardless of what the user requests
* fix bug attempting to get/set nonexistent X[31]
* fix bug writing 64 bit kernel pstate into uint32_t env field
]

---
 target-arm/Makefile.objs |   1 +
 target-arm/kvm.c         |   4 +
 target-arm/kvm64.c       | 204 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 209 insertions(+)
 create mode 100644 target-arm/kvm64.c
target-arm/Makefile.objs +1 -0
index d1db77c..5493a4c 100644
@@ -7,3 +7,4 @@ obj-y += neon_helper.o iwmmxt_helper.o
 obj-y += gdbstub.o
 obj-$(TARGET_AARCH64) += cpu64.o translate-a64.o gdbstub64.o
 obj-$(call land,$(CONFIG_KVM),$(call lnot,$(TARGET_AARCH64))) += kvm32.o
+obj-$(call land,$(CONFIG_KVM),$(TARGET_AARCH64)) += kvm64.o
target-arm/kvm.c +4 -0
index 5cdb3b9..1d2688d 100644
@@ -128,7 +128,11 @@ static void kvm_arm_host_cpu_initfn(Object *obj)
 
 static const TypeInfo host_arm_cpu_type_info = {
     .name = TYPE_ARM_HOST_CPU,
+#ifdef TARGET_AARCH64
+    .parent = TYPE_AARCH64_CPU,
+#else
     .parent = TYPE_ARM_CPU,
+#endif
     .instance_init = kvm_arm_host_cpu_initfn,
     .class_init = kvm_arm_host_cpu_class_init,
     .class_size = sizeof(ARMHostCPUClass),
target-arm/kvm64.c new file +204 -0

204 lines not on this page · raw message

Signature · 1 line
1.8.5
#4 lore raw

PATCH v2 4/7 configure: Enable KVM for aarch64 host/target combination

In reply to Peter Maydell

Enable KVM if the host and target CPU are both aarch64. Note that host aarch64 + target arm is not valid for KVM acceleration:
the 64 bit kernel does not support the ioctl interface for 32 bit CPUs. 32 bit VMs on 64 bit hosts need to be created using the 64 bit ioctl interface; when QEMU supports this it will be on the arch64-softmmu target with a -cpu parameter for a 32 bit CPU, which is still an aarch64/aarch64 combination as far as configure is concerned.

Message-id: [email protected]

---
 configure | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
configure +1 -1
index edfea95..02c94e2 100755
@@ -4550,7 +4550,7 @@ case "$target_name" in
   *)
 esac
 case "$target_name" in
-  arm|i386|x86_64|ppcemb|ppc|ppc64|s390x)
+  aarch64|arm|i386|x86_64|ppcemb|ppc|ppc64|s390x)
     # Make sure the target and host cpus are compatible
     if test "$kvm" = "yes" -a "$target_softmmu" = "yes" -a \
       \( "$target_name" = "$cpu" -o \
Signature · 1 line
1.8.5
#5 lore raw

PATCH v2 5/7 hw/arm/boot: Allow easier swapping in of different loader code

In reply to Peter Maydell

For AArch64 we will obviously require a different set of primary and secondary boot loader code fragments. However currently we hardcode the offsets into the loader code where we must write the entrypoint and other data into arm_load_kernel(). This makes it hard to substitute a different loader fragment, so switch to a more flexible scheme where instead of a raw array of instructions we use an array of (instruction, fixup-type) pairs that indicate which words need special action or data written into them.

Message-id: [email protected]

---
 hw/arm/boot.c | 152 +++++++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 107 insertions(+), 45 deletions(-)
hw/arm/boot.c +107 -45
index 55d552f..0c05a64 100644
@@ -20,15 +20,33 @@
 #define KERNEL_ARGS_ADDR 0x100
 #define KERNEL_LOAD_ADDR 0x00010000
 
+typedef enum {
+    FIXUP_NONE = 0,   /* do nothing */
+    FIXUP_TERMINATOR, /* end of insns */
+    FIXUP_BOARDID,    /* overwrite with board ID number */
+    FIXUP_ARGPTR,     /* overwrite with pointer to kernel args */
+    FIXUP_ENTRYPOINT, /* overwrite with kernel entry point */
+    FIXUP_GIC_CPU_IF, /* overwrite with GIC CPU interface address */
+    FIXUP_BOOTREG,    /* overwrite with boot register address */
+    FIXUP_DSB,        /* overwrite with correct DSB insn for cpu */
+    FIXUP_MAX,
+} FixupType;
+
+typedef struct ARMInsnFixup {
+    uint32_t insn;
+    FixupType fixup;
+} ARMInsnFixup;
+
 /* The worlds second smallest bootloader.  Set r0-r2, then jump to kernel.  */
-static uint32_t bootloader[] = {
-  0xe3a00000, /* mov     r0, #0 */
-  0xe59f1004, /* ldr     r1, [pc, #4] */
-  0xe59f2004, /* ldr     r2, [pc, #4] */
-  0xe59ff004, /* ldr     pc, [pc, #4] */
-  0, /* Board ID */
-  0, /* Address of kernel args.  Set by integratorcp_init.  */
-  0  /* Kernel entry point.  Set by integratorcp_init.  */
+static const ARMInsnFixup bootloader[] = {
+    { 0xe3a00000 }, /* mov     r0, #0 */
+    { 0xe59f1004 }, /* ldr     r1, [pc, #4] */
+    { 0xe59f2004 }, /* ldr     r2, [pc, #4] */
+    { 0xe59ff004 }, /* ldr     pc, [pc, #4] */
+    { 0, FIXUP_BOARDID },
+    { 0, FIXUP_ARGPTR },
+    { 0, FIXUP_ENTRYPOINT },
+    { 0, FIXUP_TERMINATOR }
 };
 
 /* Handling for secondary CPU boot in a multicore system.
@@ -48,39 +66,83 @@ static uint32_t bootloader[] = {
 #define DSB_INSN 0xf57ff04f
 #define CP15_DSB_INSN 0xee070f9a /* mcr cp15, 0, r0, c7, c10, 4 */
 
-static uint32_t smpboot[] = {
-  0xe59f2028, /* ldr r2, gic_cpu_if */
-  0xe59f0028, /* ldr r0, startaddr */
-  0xe3a01001, /* mov r1, #1 */
-  0xe5821000, /* str r1, [r2] - set GICC_CTLR.Enable */
-  0xe3a010ff, /* mov r1, #0xff */
-  0xe5821004, /* str r1, [r2, 4] - set GIC_PMR.Priority to 0xff */
-  DSB_INSN,   /* dsb */
-  0xe320f003, /* wfi */
-  0xe5901000, /* ldr     r1, [r0] */
-  0xe1110001, /* tst     r1, r1 */
-  0x0afffffb, /* beq     <wfi> */
-  0xe12fff11, /* bx      r1 */
-  0,          /* gic_cpu_if: base address of GIC CPU interface */
-  0           /* bootreg: Boot register address is held here */
+static const ARMInsnFixup smpboot[] = {
+    { 0xe59f2028 }, /* ldr r2, gic_cpu_if */
+    { 0xe59f0028 }, /* ldr r0, bootreg_addr */
+    { 0xe3a01001 }, /* mov r1, #1 */
+    { 0xe5821000 }, /* str r1, [r2] - set GICC_CTLR.Enable */
+    { 0xe3a010ff }, /* mov r1, #0xff */
+    { 0xe5821004 }, /* str r1, [r2, 4] - set GIC_PMR.Priority to 0xff */
+    { 0, FIXUP_DSB },   /* dsb */
+    { 0xe320f003 }, /* wfi */
+    { 0xe5901000 }, /* ldr     r1, [r0] */
+    { 0xe1110001 }, /* tst     r1, r1 */
+    { 0x0afffffb }, /* beq     <wfi> */
+    { 0xe12fff11 }, /* bx      r1 */
+    { 0, FIXUP_GIC_CPU_IF }, /* gic_cpu_if: .word 0x.... */
+    { 0, FIXUP_BOOTREG }, /* bootreg_addr: .word 0x.... */
+    { 0, FIXUP_TERMINATOR }
 };
 
+static void write_bootloader(const char *name, hwaddr addr,
+                             const ARMInsnFixup *insns, uint32_t *fixupcontext)
+{
+    /* Fix up the specified bootloader fragment and write it into
+     * guest memory using rom_add_blob_fixed(). fixupcontext is
+     * an array giving the values to write in for the fixup types
+     * which write a value into the code array.
+     */
+    int i, len;
+    uint32_t *code;
+
+    len = 0;
+    while (insns[len].fixup != FIXUP_TERMINATOR) {
+        len++;
+    }
+
+    code = g_new0(uint32_t, len);
+
+    for (i = 0; i < len; i++) {
+        uint32_t insn = insns[i].insn;
+        FixupType fixup = insns[i].fixup;
+
+        switch (fixup) {
+        case FIXUP_NONE:
+            break;
+        case FIXUP_BOARDID:
+        case FIXUP_ARGPTR:
+        case FIXUP_ENTRYPOINT:
+        case FIXUP_GIC_CPU_IF:
+        case FIXUP_BOOTREG:
+        case FIXUP_DSB:
+            insn = fixupcontext[fixup];
+            break;
+        default:
+            abort();
+        }
+        code[i] = tswap32(insn);
+    }
+
+    rom_add_blob_fixed(name, code, len * sizeof(uint32_t), addr);
+
+    g_free(code);
+}
+
 static void default_write_secondary(ARMCPU *cpu,
                                     const struct arm_boot_info *info)
 {
-    int n;
-    smpboot[ARRAY_SIZE(smpboot) - 1] = info->smp_bootreg_addr;
-    smpboot[ARRAY_SIZE(smpboot) - 2] = info->gic_cpu_if_addr;
-    for (n = 0; n < ARRAY_SIZE(smpboot); n++) {
-        /* Replace DSB with the pre-v7 DSB if necessary. */
-        if (!arm_feature(&cpu->env, ARM_FEATURE_V7) &&
-            smpboot[n] == DSB_INSN) {
-            smpboot[n] = CP15_DSB_INSN;
-        }
-        smpboot[n] = tswap32(smpboot[n]);
+    uint32_t fixupcontext[FIXUP_MAX];
+
+    fixupcontext[FIXUP_GIC_CPU_IF] = info->gic_cpu_if_addr;
+    fixupcontext[FIXUP_BOOTREG] = info->smp_bootreg_addr;
+    if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
+        fixupcontext[FIXUP_DSB] = DSB_INSN;
+    } else {
+        fixupcontext[FIXUP_DSB] = CP15_DSB_INSN;
     }
-    rom_add_blob_fixed("smpboot", smpboot, sizeof(smpboot),
-                       info->smp_loader_start);
+
+    write_bootloader("smpboot", info->smp_loader_start,
+                     smpboot, fixupcontext);
 }
 
 static void default_reset_secondary(ARMCPU *cpu,
@@ -354,7 +416,6 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
     CPUState *cs = CPU(cpu);
     int kernel_size;
     int initrd_size;
-    int n;
     int is_linux = 0;
     uint64_t elf_entry;
     hwaddr entry;
@@ -420,6 +481,8 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
     }
     info->entry = entry;
     if (is_linux) {
+        uint32_t fixupcontext[FIXUP_MAX];
+
         if (info->initrd_filename) {
             initrd_size = load_ramdisk(info->initrd_filename,
                                        info->initrd_start,
@@ -441,7 +504,7 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
         }
         info->initrd_size = initrd_size;
 
-        bootloader[4] = info->board_id;
+        fixupcontext[FIXUP_BOARDID] = info->board_id;
 
         /* for device tree boot, we pass the DTB directly in r2. Otherwise
          * we point to the kernel args.
@@ -456,9 +519,9 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
             if (load_dtb(dtb_start, info)) {
                 exit(1);
             }
-            bootloader[5] = dtb_start;
+            fixupcontext[FIXUP_ARGPTR] = dtb_start;
         } else {
-            bootloader[5] = info->loader_start + KERNEL_ARGS_ADDR;
+            fixupcontext[FIXUP_ARGPTR] = info->loader_start + KERNEL_ARGS_ADDR;
             if (info->ram_size >= (1ULL << 32)) {
                 fprintf(stderr, "qemu: RAM size must be less than 4GB to boot"
                         " Linux kernel using ATAGS (try passing a device tree"
@@ -466,12 +529,11 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
                 exit(1);
             }
         }
-        bootloader[6] = entry;
-        for (n = 0; n < sizeof(bootloader) / 4; n++) {
-            bootloader[n] = tswap32(bootloader[n]);
-        }
-        rom_add_blob_fixed("bootloader", bootloader, sizeof(bootloader),
-                           info->loader_start);
+        fixupcontext[FIXUP_ENTRYPOINT] = entry;
+
+        write_bootloader("bootloader", info->loader_start,
+                         bootloader, fixupcontext);
+
         if (info->nb_cpus > 1) {
             info->write_secondary_boot(cpu, info);
         }
Signature · 1 line
1.8.5
#6 lore raw

PATCH v2 6/7 hw/arm/boot: Add boot support for AArch64 processor

In reply to Peter Maydell

From: "Mian M. Hamayun" <[email protected]>

This commit adds support for booting a single AArch64 CPU by setting appropriate registers. The bootloader includes placehoders for Board-ID that are used to implement uniform indexing across different bootloaders.

Message-id: [email protected] [PMM:
* updated to use ARMInsnFixup style bootloader fragments
* dropped virt.c additions
* use runtime checks for "is this an AArch64 core" rather than ifdefs
* drop some unnecessary setting of registers in reset hook
]

---
 hw/arm/boot.c | 43 ++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 38 insertions(+), 5 deletions(-)
hw/arm/boot.c +38 -5
index 0c05a64..90e9534 100644
@@ -17,8 +17,13 @@
 #include "sysemu/device_tree.h"
 #include "qemu/config-file.h"
 
+/* Kernel boot protocol is specified in the kernel docs
+ * Documentation/arm/Booting and Documentation/arm64/booting.txt
+ * They have different preferred image load offsets from system RAM base.
+ */
 #define KERNEL_ARGS_ADDR 0x100
 #define KERNEL_LOAD_ADDR 0x00010000
+#define KERNEL64_LOAD_ADDR 0x00080000
 
 typedef enum {
     FIXUP_NONE = 0,   /* do nothing */
@@ -37,6 +42,20 @@ typedef struct ARMInsnFixup {
     FixupType fixup;
 } ARMInsnFixup;
 
+static const ARMInsnFixup bootloader_aarch64[] = {
+    { 0x580000c0 }, /* ldr x0, arg ; Load the lower 32-bits of DTB */
+    { 0xaa1f03e1 }, /* mov x1, xzr */
+    { 0xaa1f03e2 }, /* mov x2, xzr */
+    { 0xaa1f03e3 }, /* mov x3, xzr */
+    { 0x58000084 }, /* ldr x4, entry ; Load the lower 32-bits of kernel entry */
+    { 0xd61f0080 }, /* br x4      ; Jump to the kernel entry point */
+    { 0, FIXUP_ARGPTR }, /* arg: .word @DTB Lower 32-bits */
+    { 0 }, /* .word @DTB Higher 32-bits */
+    { 0, FIXUP_ENTRYPOINT }, /* entry: .word @Kernel Entry Lower 32-bits */
+    { 0 }, /* .word @Kernel Entry Higher 32-bits */
+    { 0, FIXUP_TERMINATOR }
+};
+
 /* The worlds second smallest bootloader.  Set r0-r2, then jump to kernel.  */
 static const ARMInsnFixup bootloader[] = {
     { 0xe3a00000 }, /* mov     r0, #0 */
@@ -396,7 +415,12 @@ static void do_cpu_reset(void *opaque)
             env->thumb = info->entry & 1;
         } else {
             if (CPU(cpu) == first_cpu) {
-                env->regs[15] = info->loader_start;
+                if (env->aarch64) {
+                    env->pc = info->loader_start;
+                } else {
+                    env->regs[15] = info->loader_start;
+                }
+
                 if (!info->dtb_filename) {
                     if (old_param) {
                         set_kernel_args_old(info);
@@ -418,8 +442,9 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
     int initrd_size;
     int is_linux = 0;
     uint64_t elf_entry;
-    hwaddr entry;
+    hwaddr entry, kernel_load_offset;
     int big_endian;
+    static const ARMInsnFixup *primary_loader;
 
     /* Load the kernel.  */
     if (!info->kernel_filename) {
@@ -429,6 +454,14 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
         return;
     }
 
+    if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
+        primary_loader = bootloader_aarch64;
+        kernel_load_offset = KERNEL64_LOAD_ADDR;
+    } else {
+        primary_loader = bootloader;
+        kernel_load_offset = KERNEL_LOAD_ADDR;
+    }
+
     info->dtb_filename = qemu_opt_get(qemu_get_machine_opts(), "dtb");
 
     if (!info->secondary_cpu_reset_hook) {
@@ -469,9 +502,9 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
                                   &is_linux);
     }
     if (kernel_size < 0) {
-        entry = info->loader_start + KERNEL_LOAD_ADDR;
+        entry = info->loader_start + kernel_load_offset;
         kernel_size = load_image_targphys(info->kernel_filename, entry,
-                                          info->ram_size - KERNEL_LOAD_ADDR);
+                                          info->ram_size - kernel_load_offset);
         is_linux = 1;
     }
     if (kernel_size < 0) {
@@ -532,7 +565,7 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
         fixupcontext[FIXUP_ENTRYPOINT] = entry;
 
         write_bootloader("bootloader", info->loader_start,
-                         bootloader, fixupcontext);
+                         primary_loader, fixupcontext);
 
         if (info->nb_cpus > 1) {
             info->write_secondary_boot(cpu, info);
Signature · 1 line
1.8.5
#7 lore raw

Re: [Qemu-devel] [PATCH v2 6/7] hw/arm/boot: Add boot support for AArch64 processor

Quoted 4 lines · Peter Maydell

From: "Mian M. Hamayun" <[email protected]>

This commit adds support for booting a single AArch64 CPU by setting appropriate registers. The bootloader includes placehoders for Board-ID

"placeholders"

Quoted 22 lines

that are used to implement uniform indexing across different bootloaders.

Message-id: [email protected] [PMM:
* updated to use ARMInsnFixup style bootloader fragments
* dropped virt.c additions
* use runtime checks for "is this an AArch64 core" rather than ifdefs
* drop some unnecessary setting of registers in reset hook
]

---
 hw/arm/boot.c | 43 ++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 38 insertions(+), 5 deletions(-)
hw/arm/boot.c +0 -0
index 0c05a64..90e9534 100644

hw/arm/boot.c line 25

Quoted 9 lines
hw/arm/boot.c +4 -0
@@ -17,8 +17,13 @@
 #include "sysemu/device_tree.h"
 #include "qemu/config-file.h"
 
+/* Kernel boot protocol is specified in the kernel docs
+ * Documentation/arm/Booting and Documentation/arm64/booting.txt
+ * They have different preferred image load offsets from system RAM base.
+ */
 #define KERNEL_ARGS_ADDR 0x100
 #define KERNEL_LOAD_ADDR 0x00010000

So out of context, but I have been booting an ARMv7 multi defconfig on a few qemu platforms recently (spec. allwinner, highbank and zynq) and I found I had to patch this to 0x8000 due to some image alignment expectations of head.S. Could we patch this to 0x8000 - is there this same sense of preferred image offset in KERNEL32_LOAD_ADDR?

Quoted 26 lines
hw/arm/boot.c +15 -0
+#define KERNEL64_LOAD_ADDR 0x00080000
 
 typedef enum {
     FIXUP_NONE = 0,   /* do nothing */
@@ -37,6 +42,20 @@ typedef struct ARMInsnFixup {
     FixupType fixup;
 } ARMInsnFixup;
 
+static const ARMInsnFixup bootloader_aarch64[] = {
+    { 0x580000c0 }, /* ldr x0, arg ; Load the lower 32-bits of DTB */
+    { 0xaa1f03e1 }, /* mov x1, xzr */
+    { 0xaa1f03e2 }, /* mov x2, xzr */
+    { 0xaa1f03e3 }, /* mov x3, xzr */
+    { 0x58000084 }, /* ldr x4, entry ; Load the lower 32-bits of kernel entry */
+    { 0xd61f0080 }, /* br x4      ; Jump to the kernel entry point */
+    { 0, FIXUP_ARGPTR }, /* arg: .word @DTB Lower 32-bits */
+    { 0 }, /* .word @DTB Higher 32-bits */
+    { 0, FIXUP_ENTRYPOINT }, /* entry: .word @Kernel Entry Lower 32-bits */
+    { 0 }, /* .word @Kernel Entry Higher 32-bits */
+    { 0, FIXUP_TERMINATOR }
+};
+
 /* The worlds second smallest bootloader.  Set r0-r2, then jump to kernel.  */
 static const ARMInsnFixup bootloader[] = {
     { 0xe3a00000 }, /* mov     r0, #0 */

hw/arm/boot.c static void do_cpu_reset(void *opaque) · line 418

Quoted 5 lines
hw/arm/boot.c +1 -1
@@ -396,7 +415,12 @@ static void do_cpu_reset(void *opaque)
             env->thumb = info->entry & 1;
         } else {
             if (CPU(cpu) == first_cpu) {
-                env->regs[15] = info->loader_start;
+                if (env->aarch64) {

Curious, why does this 'if' directly deref env, while the one below (for primary_loader selection) uses ARM_FEATURE?

Regards,
Peter

Quoted 59 lines
hw/arm/boot.c +18 -5
+                    env->pc = info->loader_start;
+                } else {
+                    env->regs[15] = info->loader_start;
+                }
+
                 if (!info->dtb_filename) {
                     if (old_param) {
                         set_kernel_args_old(info);
@@ -418,8 +442,9 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
     int initrd_size;
     int is_linux = 0;
     uint64_t elf_entry;
-    hwaddr entry;
+    hwaddr entry, kernel_load_offset;
     int big_endian;
+    static const ARMInsnFixup *primary_loader;
 
     /* Load the kernel.  */
     if (!info->kernel_filename) {
@@ -429,6 +454,14 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
         return;
     }
 
+    if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
+        primary_loader = bootloader_aarch64;
+        kernel_load_offset = KERNEL64_LOAD_ADDR;
+    } else {
+        primary_loader = bootloader;
+        kernel_load_offset = KERNEL_LOAD_ADDR;
+    }
+
     info->dtb_filename = qemu_opt_get(qemu_get_machine_opts(), "dtb");
 
     if (!info->secondary_cpu_reset_hook) {
@@ -469,9 +502,9 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
                                   &is_linux);
     }
     if (kernel_size < 0) {
-        entry = info->loader_start + KERNEL_LOAD_ADDR;
+        entry = info->loader_start + kernel_load_offset;
         kernel_size = load_image_targphys(info->kernel_filename, entry,
-                                          info->ram_size - KERNEL_LOAD_ADDR);
+                                          info->ram_size - kernel_load_offset);
         is_linux = 1;
     }
     if (kernel_size < 0) {
@@ -532,7 +565,7 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
         fixupcontext[FIXUP_ENTRYPOINT] = entry;
 
         write_bootloader("bootloader", info->loader_start,
-                         bootloader, fixupcontext);
+                         primary_loader, fixupcontext);
 
         if (info->nb_cpus > 1) {
             info->write_secondary_boot(cpu, info);
--

1.8.5

#8 lore raw

Re: [Qemu-devel] [PATCH v2 6/7] hw/arm/boot: Add boot support for AArch64 processor

Quoted 8 lines · Peter Crosthwaite
Quoted 4 lines · Peter Maydell

From: "Mian M. Hamayun" <[email protected]>

This commit adds support for booting a single AArch64 CPU by setting appropriate registers. The bootloader includes placehoders for Board-ID

"placeholders"

Fixed (not going to respin just for that though :-))

Quoted 25 lines
Quoted 25 lines

that are used to implement uniform indexing across different bootloaders.

Message-id: [email protected] [PMM:
* updated to use ARMInsnFixup style bootloader fragments
* dropped virt.c additions
* use runtime checks for "is this an AArch64 core" rather than ifdefs
* drop some unnecessary setting of registers in reset hook
]

---
 hw/arm/boot.c | 43 ++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 38 insertions(+), 5 deletions(-)
hw/arm/boot.c +0 -0
index 0c05a64..90e9534 100644
@@ -17,8 +17,13 @@
 #include "sysemu/device_tree.h"
 #include "qemu/config-file.h"
 
Quoted 12 lines

hw/arm/boot.c line 25

Quoted 6 lines
hw/arm/boot.c +4 -0
+/* Kernel boot protocol is specified in the kernel docs
+ * Documentation/arm/Booting and Documentation/arm64/booting.txt
+ * They have different preferred image load offsets from system RAM base.
+ */
 #define KERNEL_ARGS_ADDR 0x100
 #define KERNEL_LOAD_ADDR 0x00010000

So out of context, but I have been booting an ARMv7 multi defconfig on a few qemu platforms recently (spec. allwinner, highbank and zynq) and I found I had to patch this to 0x8000 due to some image alignment expectations of head.S. Could we patch this to 0x8000 - is there this same sense of preferred image offset in KERNEL32_LOAD_ADDR?

Hmm. Are you booting non-zImage kernels? The booting doc says

# When booting a raw (non-zImage) kernel the constraints are tighter. # In this case the kernel must be loaded at an offset into system equal

# to TEXT_OFFSET - PAGE_OFFSET.

so maybe that's what we're not getting right there? I practically always use a zImage so I wouldn't ever see that.

In any case if you can nail down whether that's the problem and submit a patch that would be good, though we'll have to test on a bunch of boards that we haven't broken something else. (I don't particularly want to change to 0x8000 because it "happens to work", but the same change with a rationale attached is fine :-))

Quoted 26 lines
Quoted 26 lines
hw/arm/boot.c +15 -0
+#define KERNEL64_LOAD_ADDR 0x00080000
 
 typedef enum {
     FIXUP_NONE = 0,   /* do nothing */
@@ -37,6 +42,20 @@ typedef struct ARMInsnFixup {
     FixupType fixup;
 } ARMInsnFixup;
 
+static const ARMInsnFixup bootloader_aarch64[] = {
+    { 0x580000c0 }, /* ldr x0, arg ; Load the lower 32-bits of DTB */
+    { 0xaa1f03e1 }, /* mov x1, xzr */
+    { 0xaa1f03e2 }, /* mov x2, xzr */
+    { 0xaa1f03e3 }, /* mov x3, xzr */
+    { 0x58000084 }, /* ldr x4, entry ; Load the lower 32-bits of kernel entry */
+    { 0xd61f0080 }, /* br x4      ; Jump to the kernel entry point */
+    { 0, FIXUP_ARGPTR }, /* arg: .word @DTB Lower 32-bits */
+    { 0 }, /* .word @DTB Higher 32-bits */
+    { 0, FIXUP_ENTRYPOINT }, /* entry: .word @Kernel Entry Lower 32-bits */
+    { 0 }, /* .word @Kernel Entry Higher 32-bits */
+    { 0, FIXUP_TERMINATOR }
+};
+
 /* The worlds second smallest bootloader.  Set r0-r2, then jump to kernel.  */
 static const ARMInsnFixup bootloader[] = {
     { 0xe3a00000 }, /* mov     r0, #0 */
@@ -396,7 +415,12 @@ static void do_cpu_reset(void *opaque)
Quoted 8 lines

hw/arm/boot.c static void do_cpu_reset(void *opaque) · line 418

Quoted 5 lines
hw/arm/boot.c +1 -1
             env->thumb = info->entry & 1;
         } else {
             if (CPU(cpu) == first_cpu) {
-                env->regs[15] = info->loader_start;
+                if (env->aarch64) {

Curious, why does this 'if' directly deref env, while the one below (for primary_loader selection) uses ARM_FEATURE?

When we're picking the bootloader the CPU isn't started, so its internal state (including the aarch64 flag, which is a reflection of PSTATE.nRW) isn't valid. We want to pick a bootloader on the basis of "if this CPU can do AArch64 then use the AArch64 loader". This code however runs after reset and so we can say "if you're currently in AArch64 mode then this is how the PC is set, otherwise like this".

I'm mulling a cleanup at some point that makes the AArch32 also use env->pc, at which point this if() will just evaporate anyway.

thanks
-- PMM

#9 lore raw

Re: [Qemu-devel] [PATCH v2 6/7] hw/arm/boot: Add boot support for AArch64 processor

Quoted 51 lines · Peter Maydell
Quoted 8 lines · Peter Crosthwaite
Quoted 4 lines · Peter Maydell

From: "Mian M. Hamayun" <[email protected]>

This commit adds support for booting a single AArch64 CPU by setting appropriate registers. The bootloader includes placehoders for Board-ID

"placeholders"

Fixed (not going to respin just for that though :-))

Quoted 25 lines
Quoted 25 lines

that are used to implement uniform indexing across different bootloaders.

Message-id: [email protected] [PMM:
* updated to use ARMInsnFixup style bootloader fragments
* dropped virt.c additions
* use runtime checks for "is this an AArch64 core" rather than ifdefs
* drop some unnecessary setting of registers in reset hook
]

---
 hw/arm/boot.c | 43 ++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 38 insertions(+), 5 deletions(-)
hw/arm/boot.c +0 -0
index 0c05a64..90e9534 100644
@@ -17,8 +17,13 @@
 #include "sysemu/device_tree.h"
 #include "qemu/config-file.h"
 
Quoted 12 lines

hw/arm/boot.c line 25

Quoted 6 lines
hw/arm/boot.c +4 -0
+/* Kernel boot protocol is specified in the kernel docs
+ * Documentation/arm/Booting and Documentation/arm64/booting.txt
+ * They have different preferred image load offsets from system RAM base.
+ */
 #define KERNEL_ARGS_ADDR 0x100
 #define KERNEL_LOAD_ADDR 0x00010000

So out of context, but I have been booting an ARMv7 multi defconfig on a few qemu platforms recently (spec. allwinner, highbank and zynq) and I found I had to patch this to 0x8000 due to some image alignment expectations of head.S. Could we patch this to 0x8000 - is there this same sense of preferred image offset in KERNEL32_LOAD_ADDR?

Quoted 2 lines

Hmm. Are you booting non-zImage kernels? The booting doc says

Yep.

Quoted 4 lines

# When booting a raw (non-zImage) kernel the constraints are tighter. # In this case the kernel must be loaded at an offset into system equal

# to TEXT_OFFSET - PAGE_OFFSET.

Yes it'll probably that, although my trace through head.S had significant variation with different Kconfigs and normal Image has booted in the past. Would have to get back with specifics.

Quoted 44 lines

so maybe that's what we're not getting right there? I practically always use a zImage so I wouldn't ever see that.

In any case if you can nail down whether that's the problem and submit a patch that would be good, though we'll have to test on a bunch of boards that we haven't broken something else. (I don't particularly want to change to 0x8000 because it "happens to work", but the same change with a rationale attached is fine :-))

Quoted 26 lines
Quoted 26 lines
hw/arm/boot.c +15 -0
+#define KERNEL64_LOAD_ADDR 0x00080000
 
 typedef enum {
     FIXUP_NONE = 0,   /* do nothing */
@@ -37,6 +42,20 @@ typedef struct ARMInsnFixup {
     FixupType fixup;
 } ARMInsnFixup;
 
+static const ARMInsnFixup bootloader_aarch64[] = {
+    { 0x580000c0 }, /* ldr x0, arg ; Load the lower 32-bits of DTB */
+    { 0xaa1f03e1 }, /* mov x1, xzr */
+    { 0xaa1f03e2 }, /* mov x2, xzr */
+    { 0xaa1f03e3 }, /* mov x3, xzr */
+    { 0x58000084 }, /* ldr x4, entry ; Load the lower 32-bits of kernel entry */
+    { 0xd61f0080 }, /* br x4      ; Jump to the kernel entry point */
+    { 0, FIXUP_ARGPTR }, /* arg: .word @DTB Lower 32-bits */
+    { 0 }, /* .word @DTB Higher 32-bits */
+    { 0, FIXUP_ENTRYPOINT }, /* entry: .word @Kernel Entry Lower 32-bits */
+    { 0 }, /* .word @Kernel Entry Higher 32-bits */
+    { 0, FIXUP_TERMINATOR }
+};
+
 /* The worlds second smallest bootloader.  Set r0-r2, then jump to kernel.  */
 static const ARMInsnFixup bootloader[] = {
     { 0xe3a00000 }, /* mov     r0, #0 */
@@ -396,7 +415,12 @@ static void do_cpu_reset(void *opaque)
Quoted 8 lines

hw/arm/boot.c static void do_cpu_reset(void *opaque) · line 418

Quoted 5 lines
hw/arm/boot.c +1 -1
             env->thumb = info->entry & 1;
         } else {
             if (CPU(cpu) == first_cpu) {
-                env->regs[15] = info->loader_start;
+                if (env->aarch64) {

Curious, why does this 'if' directly deref env, while the one below (for primary_loader selection) uses ARM_FEATURE?

Quoted 8 lines

When we're picking the bootloader the CPU isn't started, so its internal state (including the aarch64 flag, which is a reflection of PSTATE.nRW) isn't valid. We want to pick a bootloader on the basis of "if this CPU can do AArch64 then use the AArch64 loader". This code however runs after reset and so we can say "if you're currently in AArch64 mode then this is how the PC is set, otherwise like this".

But isn't the selection of which primary bootloader pre-decided by arm_feature? This would lead to an error if (somehow) the bootloader blobbed in the 64 bit version and the CPU reset into 32 (via this aarch64 flag). Perhaps this check should also be arm_feature, and then the code can addtionally assert correctness of this ->aarch64 CPU state rather than trusting in coherency between the two stages.

Regards,
Peter

Quoted 6 lines

I'm mulling a cleanup at some point that makes the AArch32 also use env->pc, at which point this if() will just evaporate anyway.

thanks
-- PMM
64

#10 lore raw

PATCH v2 7/7 default-configs: Add config for aarch64-softmmu

In reply to Peter Maydell

Add a config for aarch64-softmmu; this enables building of this target. The resulting executable doesn't know about any 64 bit CPUs, but all the 32 bit CPUs and board models work.

Message-id: [email protected]

---
 default-configs/aarch64-softmmu.mak | 6 ++++++
 1 file changed, 6 insertions(+)
 create mode 100644 default-configs/aarch64-softmmu.mak
default-configs/aarch64-softmmu.mak new file +6 -0
new file mode 100644
index 0000000..6d3b5c7
@@ -0,0 +1,6 @@
+# Default configuration for aarch64-softmmu
+
+# We support all the 32 bit boards so need all their config
+include arm-softmmu.mak
+
+# Currently no 64-bit specific config requirements
Signature · 1 line
1.8.5
Reviewed-by on v2 7/7
#11 lore raw

Re: [Qemu-devel] [PATCH v2 7/7] default-configs: Add config for aarch64-softmmu

Quoted 5 lines · Peter Maydell

Add a config for aarch64-softmmu; this enables building of this target. The resulting executable doesn't know about any 64 bit CPUs, but all the 32 bit CPUs and board models work.

Quoted 22 lines

Message-id: [email protected]

---
 default-configs/aarch64-softmmu.mak | 6 ++++++
 1 file changed, 6 insertions(+)
 create mode 100644 default-configs/aarch64-softmmu.mak
default-configs/aarch64-softmmu.mak new file +6 -0
new file mode 100644
index 0000000..6d3b5c7
@@ -0,0 +1,6 @@
+# Default configuration for aarch64-softmmu
+
+# We support all the 32 bit boards so need all their config
+include arm-softmmu.mak
+
+# Currently no 64-bit specific config requirements

1.8.5

#12 lore raw
In reply to Peter Maydell
Quoted 5 lines · Peter Maydell

This patchset adds support for basic AArch64 KVM VM control; it's based on current master. This is a quick resend with the very minor nits Christoffer pointed out fixed; I'm planning to put it into a target-arm pullreq in the next day or two.

Quoted 12 lines

This patch series supports:
* 64 bit KVM VM control
* SMP and UP
* PSCI boot of secondary CPUs
It doesn't support:
* migration
* reset (partly because there's no way to reset a mach-virt system yet)
* anything except "-cpu host"
* debugging the VM via qemu gdbstub
* running 32 bit VMs on a 64 bit system
[Mian's patchset includes support for that but I have left it out for the moment because it needs more thought about UI and so on]

I finally managed to test this on the foundation model. I verified the following works:

- virtio-block
- virtio-net
- ssh/shell environemnt in guest
- apache in guest (tested with apachebench - the model is slow)
- gcc in guest
- hackbench in guest (the model is slow)
- php in guest
- python in guest

-Christoffer

Quoted 37 lines

Changes v1->v2:
* improved a couple of comments
* compat string for 64 bit is "arm,arm-v8", not -v7
* removed superfluous include of usb.mak and pci.mak from config

Mian M. Hamayun (2):
target-arm: Add minimal KVM AArch64 support hw/arm/boot: Add boot support for AArch64 processor

Peter Maydell (5):
target-arm/kvm: Split 32 bit only code into its own file
target-arm: Clean up handling of AArch64 PSTATE
configure: Enable KVM for aarch64 host/target combination hw/arm/boot: Allow easier swapping in of different loader code
default-configs: Add config for aarch64-softmmu

 configure                           |   2 +-
 default-configs/aarch64-softmmu.mak |   6 +
 hw/arm/boot.c                       | 193 ++++++++++----
 linux-user/signal.c                 |   6 +-
 target-arm/Makefile.objs            |   2 +
 target-arm/cpu.c                    |   6 +
 target-arm/cpu.h                    |  70 ++++-
 target-arm/gdbstub64.c              |   4 +-
 target-arm/kvm.c                    | 495 +---------------------------------
 target-arm/kvm32.c                  | 515 ++++++++++++++++++++++++++++++++++++
 target-arm/kvm64.c                  | 204 ++++++++++++++
 target-arm/translate-a64.c          |  12 +-
 12 files changed, 954 insertions(+), 561 deletions(-)
 create mode 100644 default-configs/aarch64-softmmu.mak
 create mode 100644 target-arm/kvm32.c
 create mode 100644 target-arm/kvm64.c
Signature · 1 line
1.8.5
Signature · 1 line
Christoffer