xref: /openbmc/qemu/hw/arm/boot.c (revision 4cbca7d9b4de5ecf6a8447b2eba89d591717f6c4)
153018216SPaolo Bonzini /*
253018216SPaolo Bonzini  * ARM kernel loader.
353018216SPaolo Bonzini  *
453018216SPaolo Bonzini  * Copyright (c) 2006-2007 CodeSourcery.
553018216SPaolo Bonzini  * Written by Paul Brook
653018216SPaolo Bonzini  *
753018216SPaolo Bonzini  * This code is licensed under the GPL.
853018216SPaolo Bonzini  */
953018216SPaolo Bonzini 
1012b16722SPeter Maydell #include "qemu/osdep.h"
11c0dbca36SAlistair Francis #include "qemu/error-report.h"
12da34e65cSMarkus Armbruster #include "qapi/error.h"
13b77257d7SGuenter Roeck #include <libfdt.h>
1453018216SPaolo Bonzini #include "hw/hw.h"
15bd2be150SPeter Maydell #include "hw/arm/arm.h"
16d8b1ae42SPeter Maydell #include "hw/arm/linux-boot-if.h"
17baf6b681SPeter Crosthwaite #include "sysemu/kvm.h"
1853018216SPaolo Bonzini #include "sysemu/sysemu.h"
199695200aSShannon Zhao #include "sysemu/numa.h"
2053018216SPaolo Bonzini #include "hw/boards.h"
2153018216SPaolo Bonzini #include "hw/loader.h"
2253018216SPaolo Bonzini #include "elf.h"
2353018216SPaolo Bonzini #include "sysemu/device_tree.h"
2453018216SPaolo Bonzini #include "qemu/config-file.h"
252198a121SEdgar E. Iglesias #include "exec/address-spaces.h"
2653018216SPaolo Bonzini 
274d9ebf75SMian M. Hamayun /* Kernel boot protocol is specified in the kernel docs
284d9ebf75SMian M. Hamayun  * Documentation/arm/Booting and Documentation/arm64/booting.txt
294d9ebf75SMian M. Hamayun  * They have different preferred image load offsets from system RAM base.
304d9ebf75SMian M. Hamayun  */
3153018216SPaolo Bonzini #define KERNEL_ARGS_ADDR 0x100
3253018216SPaolo Bonzini #define KERNEL_LOAD_ADDR 0x00010000
334d9ebf75SMian M. Hamayun #define KERNEL64_LOAD_ADDR 0x00080000
3453018216SPaolo Bonzini 
3568115ed5SArd Biesheuvel #define ARM64_TEXT_OFFSET_OFFSET    8
3668115ed5SArd Biesheuvel #define ARM64_MAGIC_OFFSET          56
3768115ed5SArd Biesheuvel 
3847b1da81SPeter Maydell typedef enum {
3947b1da81SPeter Maydell     FIXUP_NONE = 0,     /* do nothing */
4047b1da81SPeter Maydell     FIXUP_TERMINATOR,   /* end of insns */
4147b1da81SPeter Maydell     FIXUP_BOARDID,      /* overwrite with board ID number */
4210b8ec73SPeter Crosthwaite     FIXUP_BOARD_SETUP,  /* overwrite with board specific setup code address */
4347b1da81SPeter Maydell     FIXUP_ARGPTR,       /* overwrite with pointer to kernel args */
4447b1da81SPeter Maydell     FIXUP_ENTRYPOINT,   /* overwrite with kernel entry point */
4547b1da81SPeter Maydell     FIXUP_GIC_CPU_IF,   /* overwrite with GIC CPU interface address */
4647b1da81SPeter Maydell     FIXUP_BOOTREG,      /* overwrite with boot register address */
4747b1da81SPeter Maydell     FIXUP_DSB,          /* overwrite with correct DSB insn for cpu */
4847b1da81SPeter Maydell     FIXUP_MAX,
4947b1da81SPeter Maydell } FixupType;
5047b1da81SPeter Maydell 
5147b1da81SPeter Maydell typedef struct ARMInsnFixup {
5247b1da81SPeter Maydell     uint32_t insn;
5347b1da81SPeter Maydell     FixupType fixup;
5447b1da81SPeter Maydell } ARMInsnFixup;
5547b1da81SPeter Maydell 
564d9ebf75SMian M. Hamayun static const ARMInsnFixup bootloader_aarch64[] = {
574d9ebf75SMian M. Hamayun     { 0x580000c0 }, /* ldr x0, arg ; Load the lower 32-bits of DTB */
584d9ebf75SMian M. Hamayun     { 0xaa1f03e1 }, /* mov x1, xzr */
594d9ebf75SMian M. Hamayun     { 0xaa1f03e2 }, /* mov x2, xzr */
604d9ebf75SMian M. Hamayun     { 0xaa1f03e3 }, /* mov x3, xzr */
614d9ebf75SMian M. Hamayun     { 0x58000084 }, /* ldr x4, entry ; Load the lower 32-bits of kernel entry */
624d9ebf75SMian M. Hamayun     { 0xd61f0080 }, /* br x4      ; Jump to the kernel entry point */
634d9ebf75SMian M. Hamayun     { 0, FIXUP_ARGPTR }, /* arg: .word @DTB Lower 32-bits */
644d9ebf75SMian M. Hamayun     { 0 }, /* .word @DTB Higher 32-bits */
654d9ebf75SMian M. Hamayun     { 0, FIXUP_ENTRYPOINT }, /* entry: .word @Kernel Entry Lower 32-bits */
664d9ebf75SMian M. Hamayun     { 0 }, /* .word @Kernel Entry Higher 32-bits */
674d9ebf75SMian M. Hamayun     { 0, FIXUP_TERMINATOR }
684d9ebf75SMian M. Hamayun };
694d9ebf75SMian M. Hamayun 
7010b8ec73SPeter Crosthwaite /* A very small bootloader: call the board-setup code (if needed),
7110b8ec73SPeter Crosthwaite  * set r0-r2, then jump to the kernel.
7210b8ec73SPeter Crosthwaite  * If we're not calling boot setup code then we don't copy across
7310b8ec73SPeter Crosthwaite  * the first BOOTLOADER_NO_BOARD_SETUP_OFFSET insns in this array.
7410b8ec73SPeter Crosthwaite  */
7510b8ec73SPeter Crosthwaite 
7647b1da81SPeter Maydell static const ARMInsnFixup bootloader[] = {
77b4850e5aSSylvain Garrigues     { 0xe28fe004 }, /* add     lr, pc, #4 */
7810b8ec73SPeter Crosthwaite     { 0xe51ff004 }, /* ldr     pc, [pc, #-4] */
7910b8ec73SPeter Crosthwaite     { 0, FIXUP_BOARD_SETUP },
8010b8ec73SPeter Crosthwaite #define BOOTLOADER_NO_BOARD_SETUP_OFFSET 3
8147b1da81SPeter Maydell     { 0xe3a00000 }, /* mov     r0, #0 */
8247b1da81SPeter Maydell     { 0xe59f1004 }, /* ldr     r1, [pc, #4] */
8347b1da81SPeter Maydell     { 0xe59f2004 }, /* ldr     r2, [pc, #4] */
8447b1da81SPeter Maydell     { 0xe59ff004 }, /* ldr     pc, [pc, #4] */
8547b1da81SPeter Maydell     { 0, FIXUP_BOARDID },
8647b1da81SPeter Maydell     { 0, FIXUP_ARGPTR },
8747b1da81SPeter Maydell     { 0, FIXUP_ENTRYPOINT },
8847b1da81SPeter Maydell     { 0, FIXUP_TERMINATOR }
8953018216SPaolo Bonzini };
9053018216SPaolo Bonzini 
9153018216SPaolo Bonzini /* Handling for secondary CPU boot in a multicore system.
9253018216SPaolo Bonzini  * Unlike the uniprocessor/primary CPU boot, this is platform
9353018216SPaolo Bonzini  * dependent. The default code here is based on the secondary
9453018216SPaolo Bonzini  * CPU boot protocol used on realview/vexpress boards, with
9553018216SPaolo Bonzini  * some parameterisation to increase its flexibility.
9653018216SPaolo Bonzini  * QEMU platform models for which this code is not appropriate
9753018216SPaolo Bonzini  * should override write_secondary_boot and secondary_cpu_reset_hook
9853018216SPaolo Bonzini  * instead.
9953018216SPaolo Bonzini  *
10053018216SPaolo Bonzini  * This code enables the interrupt controllers for the secondary
10153018216SPaolo Bonzini  * CPUs and then puts all the secondary CPUs into a loop waiting
10253018216SPaolo Bonzini  * for an interprocessor interrupt and polling a configurable
10353018216SPaolo Bonzini  * location for the kernel secondary CPU entry point.
10453018216SPaolo Bonzini  */
10553018216SPaolo Bonzini #define DSB_INSN 0xf57ff04f
10653018216SPaolo Bonzini #define CP15_DSB_INSN 0xee070f9a /* mcr cp15, 0, r0, c7, c10, 4 */
10753018216SPaolo Bonzini 
10847b1da81SPeter Maydell static const ARMInsnFixup smpboot[] = {
10947b1da81SPeter Maydell     { 0xe59f2028 }, /* ldr r2, gic_cpu_if */
11047b1da81SPeter Maydell     { 0xe59f0028 }, /* ldr r0, bootreg_addr */
11147b1da81SPeter Maydell     { 0xe3a01001 }, /* mov r1, #1 */
11247b1da81SPeter Maydell     { 0xe5821000 }, /* str r1, [r2] - set GICC_CTLR.Enable */
11347b1da81SPeter Maydell     { 0xe3a010ff }, /* mov r1, #0xff */
11447b1da81SPeter Maydell     { 0xe5821004 }, /* str r1, [r2, 4] - set GIC_PMR.Priority to 0xff */
11547b1da81SPeter Maydell     { 0, FIXUP_DSB },   /* dsb */
11647b1da81SPeter Maydell     { 0xe320f003 }, /* wfi */
11747b1da81SPeter Maydell     { 0xe5901000 }, /* ldr     r1, [r0] */
11847b1da81SPeter Maydell     { 0xe1110001 }, /* tst     r1, r1 */
11947b1da81SPeter Maydell     { 0x0afffffb }, /* beq     <wfi> */
12047b1da81SPeter Maydell     { 0xe12fff11 }, /* bx      r1 */
12147b1da81SPeter Maydell     { 0, FIXUP_GIC_CPU_IF }, /* gic_cpu_if: .word 0x.... */
12247b1da81SPeter Maydell     { 0, FIXUP_BOOTREG }, /* bootreg_addr: .word 0x.... */
12347b1da81SPeter Maydell     { 0, FIXUP_TERMINATOR }
12453018216SPaolo Bonzini };
12553018216SPaolo Bonzini 
12647b1da81SPeter Maydell static void write_bootloader(const char *name, hwaddr addr,
12747b1da81SPeter Maydell                              const ARMInsnFixup *insns, uint32_t *fixupcontext)
12847b1da81SPeter Maydell {
12947b1da81SPeter Maydell     /* Fix up the specified bootloader fragment and write it into
13047b1da81SPeter Maydell      * guest memory using rom_add_blob_fixed(). fixupcontext is
13147b1da81SPeter Maydell      * an array giving the values to write in for the fixup types
13247b1da81SPeter Maydell      * which write a value into the code array.
13347b1da81SPeter Maydell      */
13447b1da81SPeter Maydell     int i, len;
13547b1da81SPeter Maydell     uint32_t *code;
13647b1da81SPeter Maydell 
13747b1da81SPeter Maydell     len = 0;
13847b1da81SPeter Maydell     while (insns[len].fixup != FIXUP_TERMINATOR) {
13947b1da81SPeter Maydell         len++;
14047b1da81SPeter Maydell     }
14147b1da81SPeter Maydell 
14247b1da81SPeter Maydell     code = g_new0(uint32_t, len);
14347b1da81SPeter Maydell 
14447b1da81SPeter Maydell     for (i = 0; i < len; i++) {
14547b1da81SPeter Maydell         uint32_t insn = insns[i].insn;
14647b1da81SPeter Maydell         FixupType fixup = insns[i].fixup;
14747b1da81SPeter Maydell 
14847b1da81SPeter Maydell         switch (fixup) {
14947b1da81SPeter Maydell         case FIXUP_NONE:
15047b1da81SPeter Maydell             break;
15147b1da81SPeter Maydell         case FIXUP_BOARDID:
15210b8ec73SPeter Crosthwaite         case FIXUP_BOARD_SETUP:
15347b1da81SPeter Maydell         case FIXUP_ARGPTR:
15447b1da81SPeter Maydell         case FIXUP_ENTRYPOINT:
15547b1da81SPeter Maydell         case FIXUP_GIC_CPU_IF:
15647b1da81SPeter Maydell         case FIXUP_BOOTREG:
15747b1da81SPeter Maydell         case FIXUP_DSB:
15847b1da81SPeter Maydell             insn = fixupcontext[fixup];
15947b1da81SPeter Maydell             break;
16047b1da81SPeter Maydell         default:
16147b1da81SPeter Maydell             abort();
16247b1da81SPeter Maydell         }
16347b1da81SPeter Maydell         code[i] = tswap32(insn);
16447b1da81SPeter Maydell     }
16547b1da81SPeter Maydell 
16647b1da81SPeter Maydell     rom_add_blob_fixed(name, code, len * sizeof(uint32_t), addr);
16747b1da81SPeter Maydell 
16847b1da81SPeter Maydell     g_free(code);
16947b1da81SPeter Maydell }
17047b1da81SPeter Maydell 
17153018216SPaolo Bonzini static void default_write_secondary(ARMCPU *cpu,
17253018216SPaolo Bonzini                                     const struct arm_boot_info *info)
17353018216SPaolo Bonzini {
17447b1da81SPeter Maydell     uint32_t fixupcontext[FIXUP_MAX];
17547b1da81SPeter Maydell 
17647b1da81SPeter Maydell     fixupcontext[FIXUP_GIC_CPU_IF] = info->gic_cpu_if_addr;
17747b1da81SPeter Maydell     fixupcontext[FIXUP_BOOTREG] = info->smp_bootreg_addr;
17847b1da81SPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
17947b1da81SPeter Maydell         fixupcontext[FIXUP_DSB] = DSB_INSN;
18047b1da81SPeter Maydell     } else {
18147b1da81SPeter Maydell         fixupcontext[FIXUP_DSB] = CP15_DSB_INSN;
18253018216SPaolo Bonzini     }
18347b1da81SPeter Maydell 
18447b1da81SPeter Maydell     write_bootloader("smpboot", info->smp_loader_start,
18547b1da81SPeter Maydell                      smpboot, fixupcontext);
18653018216SPaolo Bonzini }
18753018216SPaolo Bonzini 
188716536a9SAndrew Baumann void arm_write_secure_board_setup_dummy_smc(ARMCPU *cpu,
189716536a9SAndrew Baumann                                             const struct arm_boot_info *info,
190716536a9SAndrew Baumann                                             hwaddr mvbar_addr)
191716536a9SAndrew Baumann {
192716536a9SAndrew Baumann     int n;
193716536a9SAndrew Baumann     uint32_t mvbar_blob[] = {
194716536a9SAndrew Baumann         /* mvbar_addr: secure monitor vectors
195716536a9SAndrew Baumann          * Default unimplemented and unused vectors to spin. Makes it
196716536a9SAndrew Baumann          * easier to debug (as opposed to the CPU running away).
197716536a9SAndrew Baumann          */
198716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
199716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
200716536a9SAndrew Baumann         0xe1b0f00e, /* movs pc, lr ;SMC exception return */
201716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
202716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
203716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
204716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
205716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
206716536a9SAndrew Baumann     };
207716536a9SAndrew Baumann     uint32_t board_setup_blob[] = {
208716536a9SAndrew Baumann         /* board setup addr */
209716536a9SAndrew Baumann         0xe3a00e00 + (mvbar_addr >> 4), /* mov r0, #mvbar_addr */
210716536a9SAndrew Baumann         0xee0c0f30, /* mcr     p15, 0, r0, c12, c0, 1 ;set MVBAR */
211716536a9SAndrew Baumann         0xee110f11, /* mrc     p15, 0, r0, c1 , c1, 0 ;read SCR */
212716536a9SAndrew Baumann         0xe3800031, /* orr     r0, #0x31              ;enable AW, FW, NS */
213716536a9SAndrew Baumann         0xee010f11, /* mcr     p15, 0, r0, c1, c1, 0  ;write SCR */
214716536a9SAndrew Baumann         0xe1a0100e, /* mov     r1, lr                 ;save LR across SMC */
215716536a9SAndrew Baumann         0xe1600070, /* smc     #0                     ;call monitor to flush SCR */
216716536a9SAndrew Baumann         0xe1a0f001, /* mov     pc, r1                 ;return */
217716536a9SAndrew Baumann     };
218716536a9SAndrew Baumann 
219716536a9SAndrew Baumann     /* check that mvbar_addr is correctly aligned and relocatable (using MOV) */
220716536a9SAndrew Baumann     assert((mvbar_addr & 0x1f) == 0 && (mvbar_addr >> 4) < 0x100);
221716536a9SAndrew Baumann 
222716536a9SAndrew Baumann     /* check that these blobs don't overlap */
223716536a9SAndrew Baumann     assert((mvbar_addr + sizeof(mvbar_blob) <= info->board_setup_addr)
224716536a9SAndrew Baumann           || (info->board_setup_addr + sizeof(board_setup_blob) <= mvbar_addr));
225716536a9SAndrew Baumann 
226716536a9SAndrew Baumann     for (n = 0; n < ARRAY_SIZE(mvbar_blob); n++) {
227716536a9SAndrew Baumann         mvbar_blob[n] = tswap32(mvbar_blob[n]);
228716536a9SAndrew Baumann     }
229716536a9SAndrew Baumann     rom_add_blob_fixed("board-setup-mvbar", mvbar_blob, sizeof(mvbar_blob),
230716536a9SAndrew Baumann                        mvbar_addr);
231716536a9SAndrew Baumann 
232716536a9SAndrew Baumann     for (n = 0; n < ARRAY_SIZE(board_setup_blob); n++) {
233716536a9SAndrew Baumann         board_setup_blob[n] = tswap32(board_setup_blob[n]);
234716536a9SAndrew Baumann     }
235716536a9SAndrew Baumann     rom_add_blob_fixed("board-setup", board_setup_blob,
236716536a9SAndrew Baumann                        sizeof(board_setup_blob), info->board_setup_addr);
237716536a9SAndrew Baumann }
238716536a9SAndrew Baumann 
23953018216SPaolo Bonzini static void default_reset_secondary(ARMCPU *cpu,
24053018216SPaolo Bonzini                                     const struct arm_boot_info *info)
24153018216SPaolo Bonzini {
2424df81c6eSPeter Crosthwaite     CPUState *cs = CPU(cpu);
24353018216SPaolo Bonzini 
24442874d3aSPeter Maydell     address_space_stl_notdirty(&address_space_memory, info->smp_bootreg_addr,
24542874d3aSPeter Maydell                                0, MEMTXATTRS_UNSPECIFIED, NULL);
2464df81c6eSPeter Crosthwaite     cpu_set_pc(cs, info->smp_loader_start);
24753018216SPaolo Bonzini }
24853018216SPaolo Bonzini 
24983bfffecSPeter Maydell static inline bool have_dtb(const struct arm_boot_info *info)
25083bfffecSPeter Maydell {
25183bfffecSPeter Maydell     return info->dtb_filename || info->get_dtb;
25283bfffecSPeter Maydell }
25383bfffecSPeter Maydell 
25453018216SPaolo Bonzini #define WRITE_WORD(p, value) do { \
25542874d3aSPeter Maydell     address_space_stl_notdirty(&address_space_memory, p, value, \
25642874d3aSPeter Maydell                                MEMTXATTRS_UNSPECIFIED, NULL);  \
25753018216SPaolo Bonzini     p += 4;                       \
25853018216SPaolo Bonzini } while (0)
25953018216SPaolo Bonzini 
26053018216SPaolo Bonzini static void set_kernel_args(const struct arm_boot_info *info)
26153018216SPaolo Bonzini {
26253018216SPaolo Bonzini     int initrd_size = info->initrd_size;
26353018216SPaolo Bonzini     hwaddr base = info->loader_start;
26453018216SPaolo Bonzini     hwaddr p;
26553018216SPaolo Bonzini 
26653018216SPaolo Bonzini     p = base + KERNEL_ARGS_ADDR;
26753018216SPaolo Bonzini     /* ATAG_CORE */
26853018216SPaolo Bonzini     WRITE_WORD(p, 5);
26953018216SPaolo Bonzini     WRITE_WORD(p, 0x54410001);
27053018216SPaolo Bonzini     WRITE_WORD(p, 1);
27153018216SPaolo Bonzini     WRITE_WORD(p, 0x1000);
27253018216SPaolo Bonzini     WRITE_WORD(p, 0);
27353018216SPaolo Bonzini     /* ATAG_MEM */
27453018216SPaolo Bonzini     /* TODO: handle multiple chips on one ATAG list */
27553018216SPaolo Bonzini     WRITE_WORD(p, 4);
27653018216SPaolo Bonzini     WRITE_WORD(p, 0x54410002);
27753018216SPaolo Bonzini     WRITE_WORD(p, info->ram_size);
27853018216SPaolo Bonzini     WRITE_WORD(p, info->loader_start);
27953018216SPaolo Bonzini     if (initrd_size) {
28053018216SPaolo Bonzini         /* ATAG_INITRD2 */
28153018216SPaolo Bonzini         WRITE_WORD(p, 4);
28253018216SPaolo Bonzini         WRITE_WORD(p, 0x54420005);
28353018216SPaolo Bonzini         WRITE_WORD(p, info->initrd_start);
28453018216SPaolo Bonzini         WRITE_WORD(p, initrd_size);
28553018216SPaolo Bonzini     }
28653018216SPaolo Bonzini     if (info->kernel_cmdline && *info->kernel_cmdline) {
28753018216SPaolo Bonzini         /* ATAG_CMDLINE */
28853018216SPaolo Bonzini         int cmdline_size;
28953018216SPaolo Bonzini 
29053018216SPaolo Bonzini         cmdline_size = strlen(info->kernel_cmdline);
291e1fe50dcSStefan Weil         cpu_physical_memory_write(p + 8, info->kernel_cmdline,
29253018216SPaolo Bonzini                                   cmdline_size + 1);
29353018216SPaolo Bonzini         cmdline_size = (cmdline_size >> 2) + 1;
29453018216SPaolo Bonzini         WRITE_WORD(p, cmdline_size + 2);
29553018216SPaolo Bonzini         WRITE_WORD(p, 0x54410009);
29653018216SPaolo Bonzini         p += cmdline_size * 4;
29753018216SPaolo Bonzini     }
29853018216SPaolo Bonzini     if (info->atag_board) {
29953018216SPaolo Bonzini         /* ATAG_BOARD */
30053018216SPaolo Bonzini         int atag_board_len;
30153018216SPaolo Bonzini         uint8_t atag_board_buf[0x1000];
30253018216SPaolo Bonzini 
30353018216SPaolo Bonzini         atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3;
30453018216SPaolo Bonzini         WRITE_WORD(p, (atag_board_len + 8) >> 2);
30553018216SPaolo Bonzini         WRITE_WORD(p, 0x414f4d50);
30653018216SPaolo Bonzini         cpu_physical_memory_write(p, atag_board_buf, atag_board_len);
30753018216SPaolo Bonzini         p += atag_board_len;
30853018216SPaolo Bonzini     }
30953018216SPaolo Bonzini     /* ATAG_END */
31053018216SPaolo Bonzini     WRITE_WORD(p, 0);
31153018216SPaolo Bonzini     WRITE_WORD(p, 0);
31253018216SPaolo Bonzini }
31353018216SPaolo Bonzini 
31453018216SPaolo Bonzini static void set_kernel_args_old(const struct arm_boot_info *info)
31553018216SPaolo Bonzini {
31653018216SPaolo Bonzini     hwaddr p;
31753018216SPaolo Bonzini     const char *s;
31853018216SPaolo Bonzini     int initrd_size = info->initrd_size;
31953018216SPaolo Bonzini     hwaddr base = info->loader_start;
32053018216SPaolo Bonzini 
32153018216SPaolo Bonzini     /* see linux/include/asm-arm/setup.h */
32253018216SPaolo Bonzini     p = base + KERNEL_ARGS_ADDR;
32353018216SPaolo Bonzini     /* page_size */
32453018216SPaolo Bonzini     WRITE_WORD(p, 4096);
32553018216SPaolo Bonzini     /* nr_pages */
32653018216SPaolo Bonzini     WRITE_WORD(p, info->ram_size / 4096);
32753018216SPaolo Bonzini     /* ramdisk_size */
32853018216SPaolo Bonzini     WRITE_WORD(p, 0);
32953018216SPaolo Bonzini #define FLAG_READONLY	1
33053018216SPaolo Bonzini #define FLAG_RDLOAD	4
33153018216SPaolo Bonzini #define FLAG_RDPROMPT	8
33253018216SPaolo Bonzini     /* flags */
33353018216SPaolo Bonzini     WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT);
33453018216SPaolo Bonzini     /* rootdev */
33553018216SPaolo Bonzini     WRITE_WORD(p, (31 << 8) | 0);	/* /dev/mtdblock0 */
33653018216SPaolo Bonzini     /* video_num_cols */
33753018216SPaolo Bonzini     WRITE_WORD(p, 0);
33853018216SPaolo Bonzini     /* video_num_rows */
33953018216SPaolo Bonzini     WRITE_WORD(p, 0);
34053018216SPaolo Bonzini     /* video_x */
34153018216SPaolo Bonzini     WRITE_WORD(p, 0);
34253018216SPaolo Bonzini     /* video_y */
34353018216SPaolo Bonzini     WRITE_WORD(p, 0);
34453018216SPaolo Bonzini     /* memc_control_reg */
34553018216SPaolo Bonzini     WRITE_WORD(p, 0);
34653018216SPaolo Bonzini     /* unsigned char sounddefault */
34753018216SPaolo Bonzini     /* unsigned char adfsdrives */
34853018216SPaolo Bonzini     /* unsigned char bytes_per_char_h */
34953018216SPaolo Bonzini     /* unsigned char bytes_per_char_v */
35053018216SPaolo Bonzini     WRITE_WORD(p, 0);
35153018216SPaolo Bonzini     /* pages_in_bank[4] */
35253018216SPaolo Bonzini     WRITE_WORD(p, 0);
35353018216SPaolo Bonzini     WRITE_WORD(p, 0);
35453018216SPaolo Bonzini     WRITE_WORD(p, 0);
35553018216SPaolo Bonzini     WRITE_WORD(p, 0);
35653018216SPaolo Bonzini     /* pages_in_vram */
35753018216SPaolo Bonzini     WRITE_WORD(p, 0);
35853018216SPaolo Bonzini     /* initrd_start */
35953018216SPaolo Bonzini     if (initrd_size) {
36053018216SPaolo Bonzini         WRITE_WORD(p, info->initrd_start);
36153018216SPaolo Bonzini     } else {
36253018216SPaolo Bonzini         WRITE_WORD(p, 0);
36353018216SPaolo Bonzini     }
36453018216SPaolo Bonzini     /* initrd_size */
36553018216SPaolo Bonzini     WRITE_WORD(p, initrd_size);
36653018216SPaolo Bonzini     /* rd_start */
36753018216SPaolo Bonzini     WRITE_WORD(p, 0);
36853018216SPaolo Bonzini     /* system_rev */
36953018216SPaolo Bonzini     WRITE_WORD(p, 0);
37053018216SPaolo Bonzini     /* system_serial_low */
37153018216SPaolo Bonzini     WRITE_WORD(p, 0);
37253018216SPaolo Bonzini     /* system_serial_high */
37353018216SPaolo Bonzini     WRITE_WORD(p, 0);
37453018216SPaolo Bonzini     /* mem_fclk_21285 */
37553018216SPaolo Bonzini     WRITE_WORD(p, 0);
37653018216SPaolo Bonzini     /* zero unused fields */
37753018216SPaolo Bonzini     while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) {
37853018216SPaolo Bonzini         WRITE_WORD(p, 0);
37953018216SPaolo Bonzini     }
38053018216SPaolo Bonzini     s = info->kernel_cmdline;
38153018216SPaolo Bonzini     if (s) {
382e1fe50dcSStefan Weil         cpu_physical_memory_write(p, s, strlen(s) + 1);
38353018216SPaolo Bonzini     } else {
38453018216SPaolo Bonzini         WRITE_WORD(p, 0);
38553018216SPaolo Bonzini     }
38653018216SPaolo Bonzini }
38753018216SPaolo Bonzini 
388*4cbca7d9SAndrey Smirnov static void fdt_add_psci_node(void *fdt)
389*4cbca7d9SAndrey Smirnov {
390*4cbca7d9SAndrey Smirnov     uint32_t cpu_suspend_fn;
391*4cbca7d9SAndrey Smirnov     uint32_t cpu_off_fn;
392*4cbca7d9SAndrey Smirnov     uint32_t cpu_on_fn;
393*4cbca7d9SAndrey Smirnov     uint32_t migrate_fn;
394*4cbca7d9SAndrey Smirnov     ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(0));
395*4cbca7d9SAndrey Smirnov     const char *psci_method;
396*4cbca7d9SAndrey Smirnov     int64_t psci_conduit;
397*4cbca7d9SAndrey Smirnov 
398*4cbca7d9SAndrey Smirnov     psci_conduit = object_property_get_int(OBJECT(armcpu),
399*4cbca7d9SAndrey Smirnov                                            "psci-conduit",
400*4cbca7d9SAndrey Smirnov                                            &error_abort);
401*4cbca7d9SAndrey Smirnov     switch (psci_conduit) {
402*4cbca7d9SAndrey Smirnov     case QEMU_PSCI_CONDUIT_DISABLED:
403*4cbca7d9SAndrey Smirnov         return;
404*4cbca7d9SAndrey Smirnov     case QEMU_PSCI_CONDUIT_HVC:
405*4cbca7d9SAndrey Smirnov         psci_method = "hvc";
406*4cbca7d9SAndrey Smirnov         break;
407*4cbca7d9SAndrey Smirnov     case QEMU_PSCI_CONDUIT_SMC:
408*4cbca7d9SAndrey Smirnov         psci_method = "smc";
409*4cbca7d9SAndrey Smirnov         break;
410*4cbca7d9SAndrey Smirnov     default:
411*4cbca7d9SAndrey Smirnov         g_assert_not_reached();
412*4cbca7d9SAndrey Smirnov     }
413*4cbca7d9SAndrey Smirnov 
414*4cbca7d9SAndrey Smirnov     qemu_fdt_add_subnode(fdt, "/psci");
415*4cbca7d9SAndrey Smirnov     if (armcpu->psci_version == 2) {
416*4cbca7d9SAndrey Smirnov         const char comp[] = "arm,psci-0.2\0arm,psci";
417*4cbca7d9SAndrey Smirnov         qemu_fdt_setprop(fdt, "/psci", "compatible", comp, sizeof(comp));
418*4cbca7d9SAndrey Smirnov 
419*4cbca7d9SAndrey Smirnov         cpu_off_fn = QEMU_PSCI_0_2_FN_CPU_OFF;
420*4cbca7d9SAndrey Smirnov         if (arm_feature(&armcpu->env, ARM_FEATURE_AARCH64)) {
421*4cbca7d9SAndrey Smirnov             cpu_suspend_fn = QEMU_PSCI_0_2_FN64_CPU_SUSPEND;
422*4cbca7d9SAndrey Smirnov             cpu_on_fn = QEMU_PSCI_0_2_FN64_CPU_ON;
423*4cbca7d9SAndrey Smirnov             migrate_fn = QEMU_PSCI_0_2_FN64_MIGRATE;
424*4cbca7d9SAndrey Smirnov         } else {
425*4cbca7d9SAndrey Smirnov             cpu_suspend_fn = QEMU_PSCI_0_2_FN_CPU_SUSPEND;
426*4cbca7d9SAndrey Smirnov             cpu_on_fn = QEMU_PSCI_0_2_FN_CPU_ON;
427*4cbca7d9SAndrey Smirnov             migrate_fn = QEMU_PSCI_0_2_FN_MIGRATE;
428*4cbca7d9SAndrey Smirnov         }
429*4cbca7d9SAndrey Smirnov     } else {
430*4cbca7d9SAndrey Smirnov         qemu_fdt_setprop_string(fdt, "/psci", "compatible", "arm,psci");
431*4cbca7d9SAndrey Smirnov 
432*4cbca7d9SAndrey Smirnov         cpu_suspend_fn = QEMU_PSCI_0_1_FN_CPU_SUSPEND;
433*4cbca7d9SAndrey Smirnov         cpu_off_fn = QEMU_PSCI_0_1_FN_CPU_OFF;
434*4cbca7d9SAndrey Smirnov         cpu_on_fn = QEMU_PSCI_0_1_FN_CPU_ON;
435*4cbca7d9SAndrey Smirnov         migrate_fn = QEMU_PSCI_0_1_FN_MIGRATE;
436*4cbca7d9SAndrey Smirnov     }
437*4cbca7d9SAndrey Smirnov 
438*4cbca7d9SAndrey Smirnov     /* We adopt the PSCI spec's nomenclature, and use 'conduit' to refer
439*4cbca7d9SAndrey Smirnov      * to the instruction that should be used to invoke PSCI functions.
440*4cbca7d9SAndrey Smirnov      * However, the device tree binding uses 'method' instead, so that is
441*4cbca7d9SAndrey Smirnov      * what we should use here.
442*4cbca7d9SAndrey Smirnov      */
443*4cbca7d9SAndrey Smirnov     qemu_fdt_setprop_string(fdt, "/psci", "method", psci_method);
444*4cbca7d9SAndrey Smirnov 
445*4cbca7d9SAndrey Smirnov     qemu_fdt_setprop_cell(fdt, "/psci", "cpu_suspend", cpu_suspend_fn);
446*4cbca7d9SAndrey Smirnov     qemu_fdt_setprop_cell(fdt, "/psci", "cpu_off", cpu_off_fn);
447*4cbca7d9SAndrey Smirnov     qemu_fdt_setprop_cell(fdt, "/psci", "cpu_on", cpu_on_fn);
448*4cbca7d9SAndrey Smirnov     qemu_fdt_setprop_cell(fdt, "/psci", "migrate", migrate_fn);
449*4cbca7d9SAndrey Smirnov }
450*4cbca7d9SAndrey Smirnov 
451fee8ea12SArd Biesheuvel /**
452fee8ea12SArd Biesheuvel  * load_dtb() - load a device tree binary image into memory
453fee8ea12SArd Biesheuvel  * @addr:       the address to load the image at
454fee8ea12SArd Biesheuvel  * @binfo:      struct describing the boot environment
455fee8ea12SArd Biesheuvel  * @addr_limit: upper limit of the available memory area at @addr
456fee8ea12SArd Biesheuvel  *
457fee8ea12SArd Biesheuvel  * Load a device tree supplied by the machine or by the user  with the
458fee8ea12SArd Biesheuvel  * '-dtb' command line option, and put it at offset @addr in target
459fee8ea12SArd Biesheuvel  * memory.
460fee8ea12SArd Biesheuvel  *
461fee8ea12SArd Biesheuvel  * If @addr_limit contains a meaningful value (i.e., it is strictly greater
462fee8ea12SArd Biesheuvel  * than @addr), the device tree is only loaded if its size does not exceed
463fee8ea12SArd Biesheuvel  * the limit.
464fee8ea12SArd Biesheuvel  *
465fee8ea12SArd Biesheuvel  * Returns: the size of the device tree image on success,
466fee8ea12SArd Biesheuvel  *          0 if the image size exceeds the limit,
467fee8ea12SArd Biesheuvel  *          -1 on errors.
468a554ecb4Szhanghailiang  *
469a554ecb4Szhanghailiang  * Note: Must not be called unless have_dtb(binfo) is true.
470fee8ea12SArd Biesheuvel  */
471fee8ea12SArd Biesheuvel static int load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
472fee8ea12SArd Biesheuvel                     hwaddr addr_limit)
47353018216SPaolo Bonzini {
47453018216SPaolo Bonzini     void *fdt = NULL;
47553018216SPaolo Bonzini     int size, rc;
47670976c41SPeter Maydell     uint32_t acells, scells;
4779695200aSShannon Zhao     char *nodename;
4789695200aSShannon Zhao     unsigned int i;
4799695200aSShannon Zhao     hwaddr mem_base, mem_len;
48053018216SPaolo Bonzini 
4810fb79851SJohn Rigby     if (binfo->dtb_filename) {
4820fb79851SJohn Rigby         char *filename;
48353018216SPaolo Bonzini         filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename);
48453018216SPaolo Bonzini         if (!filename) {
48553018216SPaolo Bonzini             fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename);
486c23045deSPeter Maydell             goto fail;
48753018216SPaolo Bonzini         }
48853018216SPaolo Bonzini 
48953018216SPaolo Bonzini         fdt = load_device_tree(filename, &size);
49053018216SPaolo Bonzini         if (!fdt) {
49153018216SPaolo Bonzini             fprintf(stderr, "Couldn't open dtb file %s\n", filename);
49253018216SPaolo Bonzini             g_free(filename);
493c23045deSPeter Maydell             goto fail;
49453018216SPaolo Bonzini         }
49553018216SPaolo Bonzini         g_free(filename);
496a554ecb4Szhanghailiang     } else {
4970fb79851SJohn Rigby         fdt = binfo->get_dtb(binfo, &size);
4980fb79851SJohn Rigby         if (!fdt) {
4990fb79851SJohn Rigby             fprintf(stderr, "Board was unable to create a dtb blob\n");
5000fb79851SJohn Rigby             goto fail;
5010fb79851SJohn Rigby         }
5020fb79851SJohn Rigby     }
50353018216SPaolo Bonzini 
504fee8ea12SArd Biesheuvel     if (addr_limit > addr && size > (addr_limit - addr)) {
505fee8ea12SArd Biesheuvel         /* Installing the device tree blob at addr would exceed addr_limit.
506fee8ea12SArd Biesheuvel          * Whether this constitutes failure is up to the caller to decide,
507fee8ea12SArd Biesheuvel          * so just return 0 as size, i.e., no error.
508fee8ea12SArd Biesheuvel          */
509fee8ea12SArd Biesheuvel         g_free(fdt);
510fee8ea12SArd Biesheuvel         return 0;
511fee8ea12SArd Biesheuvel     }
512fee8ea12SArd Biesheuvel 
51358e71097SEric Auger     acells = qemu_fdt_getprop_cell(fdt, "/", "#address-cells",
51458e71097SEric Auger                                    NULL, &error_fatal);
51558e71097SEric Auger     scells = qemu_fdt_getprop_cell(fdt, "/", "#size-cells",
51658e71097SEric Auger                                    NULL, &error_fatal);
51753018216SPaolo Bonzini     if (acells == 0 || scells == 0) {
51853018216SPaolo Bonzini         fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n");
519c23045deSPeter Maydell         goto fail;
52053018216SPaolo Bonzini     }
52153018216SPaolo Bonzini 
52270976c41SPeter Maydell     if (scells < 2 && binfo->ram_size >= (1ULL << 32)) {
52370976c41SPeter Maydell         /* This is user error so deserves a friendlier error message
52470976c41SPeter Maydell          * than the failure of setprop_sized_cells would provide
52570976c41SPeter Maydell          */
52653018216SPaolo Bonzini         fprintf(stderr, "qemu: dtb file not compatible with "
52753018216SPaolo Bonzini                 "RAM size > 4GB\n");
528c23045deSPeter Maydell         goto fail;
52953018216SPaolo Bonzini     }
53053018216SPaolo Bonzini 
5319695200aSShannon Zhao     if (nb_numa_nodes > 0) {
5329695200aSShannon Zhao         /*
5339695200aSShannon Zhao          * Turn the /memory node created before into a NOP node, then create
5349695200aSShannon Zhao          * /memory@addr nodes for all numa nodes respectively.
5359695200aSShannon Zhao          */
5369695200aSShannon Zhao         qemu_fdt_nop_node(fdt, "/memory");
5379695200aSShannon Zhao         mem_base = binfo->loader_start;
5389695200aSShannon Zhao         for (i = 0; i < nb_numa_nodes; i++) {
5399695200aSShannon Zhao             mem_len = numa_info[i].node_mem;
5409695200aSShannon Zhao             nodename = g_strdup_printf("/memory@%" PRIx64, mem_base);
5419695200aSShannon Zhao             qemu_fdt_add_subnode(fdt, nodename);
5429695200aSShannon Zhao             qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory");
5439695200aSShannon Zhao             rc = qemu_fdt_setprop_sized_cells(fdt, nodename, "reg",
5449695200aSShannon Zhao                                               acells, mem_base,
5459695200aSShannon Zhao                                               scells, mem_len);
5469695200aSShannon Zhao             if (rc < 0) {
5479695200aSShannon Zhao                 fprintf(stderr, "couldn't set %s/reg for node %d\n", nodename,
5489695200aSShannon Zhao                         i);
5499695200aSShannon Zhao                 goto fail;
5509695200aSShannon Zhao             }
5519695200aSShannon Zhao 
5529695200aSShannon Zhao             qemu_fdt_setprop_cell(fdt, nodename, "numa-node-id", i);
5539695200aSShannon Zhao             mem_base += mem_len;
5549695200aSShannon Zhao             g_free(nodename);
5559695200aSShannon Zhao         }
5569695200aSShannon Zhao     } else {
557b77257d7SGuenter Roeck         Error *err = NULL;
558b77257d7SGuenter Roeck 
559b77257d7SGuenter Roeck         rc = fdt_path_offset(fdt, "/memory");
560b77257d7SGuenter Roeck         if (rc < 0) {
561b77257d7SGuenter Roeck             qemu_fdt_add_subnode(fdt, "/memory");
562b77257d7SGuenter Roeck         }
563b77257d7SGuenter Roeck 
564b77257d7SGuenter Roeck         if (!qemu_fdt_getprop(fdt, "/memory", "device_type", NULL, &err)) {
565b77257d7SGuenter Roeck             qemu_fdt_setprop_string(fdt, "/memory", "device_type", "memory");
566b77257d7SGuenter Roeck         }
567b77257d7SGuenter Roeck 
5685a4348d1SPeter Crosthwaite         rc = qemu_fdt_setprop_sized_cells(fdt, "/memory", "reg",
56970976c41SPeter Maydell                                           acells, binfo->loader_start,
57070976c41SPeter Maydell                                           scells, binfo->ram_size);
57153018216SPaolo Bonzini         if (rc < 0) {
57253018216SPaolo Bonzini             fprintf(stderr, "couldn't set /memory/reg\n");
573c23045deSPeter Maydell             goto fail;
57453018216SPaolo Bonzini         }
5759695200aSShannon Zhao     }
57653018216SPaolo Bonzini 
577b77257d7SGuenter Roeck     rc = fdt_path_offset(fdt, "/chosen");
578b77257d7SGuenter Roeck     if (rc < 0) {
579b77257d7SGuenter Roeck         qemu_fdt_add_subnode(fdt, "/chosen");
580b77257d7SGuenter Roeck     }
581b77257d7SGuenter Roeck 
58253018216SPaolo Bonzini     if (binfo->kernel_cmdline && *binfo->kernel_cmdline) {
5835a4348d1SPeter Crosthwaite         rc = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
58453018216SPaolo Bonzini                                      binfo->kernel_cmdline);
58553018216SPaolo Bonzini         if (rc < 0) {
58653018216SPaolo Bonzini             fprintf(stderr, "couldn't set /chosen/bootargs\n");
587c23045deSPeter Maydell             goto fail;
58853018216SPaolo Bonzini         }
58953018216SPaolo Bonzini     }
59053018216SPaolo Bonzini 
59153018216SPaolo Bonzini     if (binfo->initrd_size) {
5925a4348d1SPeter Crosthwaite         rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
59353018216SPaolo Bonzini                                    binfo->initrd_start);
59453018216SPaolo Bonzini         if (rc < 0) {
59553018216SPaolo Bonzini             fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
596c23045deSPeter Maydell             goto fail;
59753018216SPaolo Bonzini         }
59853018216SPaolo Bonzini 
5995a4348d1SPeter Crosthwaite         rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
60053018216SPaolo Bonzini                                    binfo->initrd_start + binfo->initrd_size);
60153018216SPaolo Bonzini         if (rc < 0) {
60253018216SPaolo Bonzini             fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
603c23045deSPeter Maydell             goto fail;
60453018216SPaolo Bonzini         }
60553018216SPaolo Bonzini     }
6063b1cceb8SPeter Maydell 
607*4cbca7d9SAndrey Smirnov     fdt_add_psci_node(fdt);
608*4cbca7d9SAndrey Smirnov 
6093b1cceb8SPeter Maydell     if (binfo->modify_dtb) {
6103b1cceb8SPeter Maydell         binfo->modify_dtb(binfo, fdt);
6113b1cceb8SPeter Maydell     }
6123b1cceb8SPeter Maydell 
6135a4348d1SPeter Crosthwaite     qemu_fdt_dumpdtb(fdt, size);
61453018216SPaolo Bonzini 
6154c4bf654SArd Biesheuvel     /* Put the DTB into the memory map as a ROM image: this will ensure
6164c4bf654SArd Biesheuvel      * the DTB is copied again upon reset, even if addr points into RAM.
6174c4bf654SArd Biesheuvel      */
6184c4bf654SArd Biesheuvel     rom_add_blob_fixed("dtb", fdt, size, addr);
61953018216SPaolo Bonzini 
620c23045deSPeter Maydell     g_free(fdt);
621c23045deSPeter Maydell 
622fee8ea12SArd Biesheuvel     return size;
623c23045deSPeter Maydell 
624c23045deSPeter Maydell fail:
625c23045deSPeter Maydell     g_free(fdt);
626c23045deSPeter Maydell     return -1;
62753018216SPaolo Bonzini }
62853018216SPaolo Bonzini 
62953018216SPaolo Bonzini static void do_cpu_reset(void *opaque)
63053018216SPaolo Bonzini {
63153018216SPaolo Bonzini     ARMCPU *cpu = opaque;
6324df81c6eSPeter Crosthwaite     CPUState *cs = CPU(cpu);
63353018216SPaolo Bonzini     CPUARMState *env = &cpu->env;
63453018216SPaolo Bonzini     const struct arm_boot_info *info = env->boot_info;
63553018216SPaolo Bonzini 
6364df81c6eSPeter Crosthwaite     cpu_reset(cs);
63753018216SPaolo Bonzini     if (info) {
63853018216SPaolo Bonzini         if (!info->is_linux) {
6399776f636SPeter Crosthwaite             int i;
64053018216SPaolo Bonzini             /* Jump to the entry point.  */
6414df81c6eSPeter Crosthwaite             uint64_t entry = info->entry;
6424df81c6eSPeter Crosthwaite 
6439776f636SPeter Crosthwaite             switch (info->endianness) {
6449776f636SPeter Crosthwaite             case ARM_ENDIANNESS_LE:
6459776f636SPeter Crosthwaite                 env->cp15.sctlr_el[1] &= ~SCTLR_E0E;
6469776f636SPeter Crosthwaite                 for (i = 1; i < 4; ++i) {
6479776f636SPeter Crosthwaite                     env->cp15.sctlr_el[i] &= ~SCTLR_EE;
6489776f636SPeter Crosthwaite                 }
6499776f636SPeter Crosthwaite                 env->uncached_cpsr &= ~CPSR_E;
6509776f636SPeter Crosthwaite                 break;
6519776f636SPeter Crosthwaite             case ARM_ENDIANNESS_BE8:
6529776f636SPeter Crosthwaite                 env->cp15.sctlr_el[1] |= SCTLR_E0E;
6539776f636SPeter Crosthwaite                 for (i = 1; i < 4; ++i) {
6549776f636SPeter Crosthwaite                     env->cp15.sctlr_el[i] |= SCTLR_EE;
6559776f636SPeter Crosthwaite                 }
6569776f636SPeter Crosthwaite                 env->uncached_cpsr |= CPSR_E;
6579776f636SPeter Crosthwaite                 break;
6589776f636SPeter Crosthwaite             case ARM_ENDIANNESS_BE32:
6599776f636SPeter Crosthwaite                 env->cp15.sctlr_el[1] |= SCTLR_B;
6609776f636SPeter Crosthwaite                 break;
6619776f636SPeter Crosthwaite             case ARM_ENDIANNESS_UNKNOWN:
6629776f636SPeter Crosthwaite                 break; /* Board's decision */
6639776f636SPeter Crosthwaite             default:
6649776f636SPeter Crosthwaite                 g_assert_not_reached();
6659776f636SPeter Crosthwaite             }
6669776f636SPeter Crosthwaite 
6674df81c6eSPeter Crosthwaite             if (!env->aarch64) {
66853018216SPaolo Bonzini                 env->thumb = info->entry & 1;
6694df81c6eSPeter Crosthwaite                 entry &= 0xfffffffe;
670a9047ec3SPeter Maydell             }
6714df81c6eSPeter Crosthwaite             cpu_set_pc(cs, entry);
67253018216SPaolo Bonzini         } else {
673c8e829b7SGreg Bellows             /* If we are booting Linux then we need to check whether we are
674c8e829b7SGreg Bellows              * booting into secure or non-secure state and adjust the state
675c8e829b7SGreg Bellows              * accordingly.  Out of reset, ARM is defined to be in secure state
676c8e829b7SGreg Bellows              * (SCR.NS = 0), we change that here if non-secure boot has been
677c8e829b7SGreg Bellows              * requested.
678c8e829b7SGreg Bellows              */
6795097227cSGreg Bellows             if (arm_feature(env, ARM_FEATURE_EL3)) {
6805097227cSGreg Bellows                 /* AArch64 is defined to come out of reset into EL3 if enabled.
6815097227cSGreg Bellows                  * If we are booting Linux then we need to adjust our EL as
6825097227cSGreg Bellows                  * Linux expects us to be in EL2 or EL1.  AArch32 resets into
6835097227cSGreg Bellows                  * SVC, which Linux expects, so no privilege/exception level to
6845097227cSGreg Bellows                  * adjust.
6855097227cSGreg Bellows                  */
6865097227cSGreg Bellows                 if (env->aarch64) {
68748d21a57SEdgar E. Iglesias                     env->cp15.scr_el3 |= SCR_RW;
6885097227cSGreg Bellows                     if (arm_feature(env, ARM_FEATURE_EL2)) {
68948d21a57SEdgar E. Iglesias                         env->cp15.hcr_el2 |= HCR_RW;
6905097227cSGreg Bellows                         env->pstate = PSTATE_MODE_EL2h;
6915097227cSGreg Bellows                     } else {
6925097227cSGreg Bellows                         env->pstate = PSTATE_MODE_EL1h;
6935097227cSGreg Bellows                     }
6945097227cSGreg Bellows                 }
6955097227cSGreg Bellows 
6965097227cSGreg Bellows                 /* Set to non-secure if not a secure boot */
697baf6b681SPeter Crosthwaite                 if (!info->secure_boot &&
698baf6b681SPeter Crosthwaite                     (cs != first_cpu || !info->secure_board_setup)) {
6995097227cSGreg Bellows                     /* Linux expects non-secure state */
700c8e829b7SGreg Bellows                     env->cp15.scr_el3 |= SCR_NS;
701c8e829b7SGreg Bellows                 }
7025097227cSGreg Bellows             }
703c8e829b7SGreg Bellows 
7044df81c6eSPeter Crosthwaite             if (cs == first_cpu) {
7054df81c6eSPeter Crosthwaite                 cpu_set_pc(cs, info->loader_start);
7064d9ebf75SMian M. Hamayun 
70783bfffecSPeter Maydell                 if (!have_dtb(info)) {
70853018216SPaolo Bonzini                     if (old_param) {
70953018216SPaolo Bonzini                         set_kernel_args_old(info);
71053018216SPaolo Bonzini                     } else {
71153018216SPaolo Bonzini                         set_kernel_args(info);
71253018216SPaolo Bonzini                     }
71353018216SPaolo Bonzini                 }
71453018216SPaolo Bonzini             } else {
71553018216SPaolo Bonzini                 info->secondary_cpu_reset_hook(cpu, info);
71653018216SPaolo Bonzini             }
71753018216SPaolo Bonzini         }
71853018216SPaolo Bonzini     }
71953018216SPaolo Bonzini }
72053018216SPaolo Bonzini 
72107abe45cSLaszlo Ersek /**
72207abe45cSLaszlo Ersek  * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified
72307abe45cSLaszlo Ersek  *                          by key.
72407abe45cSLaszlo Ersek  * @fw_cfg:         The firmware config instance to store the data in.
72507abe45cSLaszlo Ersek  * @size_key:       The firmware config key to store the size of the loaded
72607abe45cSLaszlo Ersek  *                  data under, with fw_cfg_add_i32().
72707abe45cSLaszlo Ersek  * @data_key:       The firmware config key to store the loaded data under,
72807abe45cSLaszlo Ersek  *                  with fw_cfg_add_bytes().
72907abe45cSLaszlo Ersek  * @image_name:     The name of the image file to load. If it is NULL, the
73007abe45cSLaszlo Ersek  *                  function returns without doing anything.
73107abe45cSLaszlo Ersek  * @try_decompress: Whether the image should be decompressed (gunzipped) before
73207abe45cSLaszlo Ersek  *                  adding it to fw_cfg. If decompression fails, the image is
73307abe45cSLaszlo Ersek  *                  loaded as-is.
73407abe45cSLaszlo Ersek  *
73507abe45cSLaszlo Ersek  * In case of failure, the function prints an error message to stderr and the
73607abe45cSLaszlo Ersek  * process exits with status 1.
73707abe45cSLaszlo Ersek  */
73807abe45cSLaszlo Ersek static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
73907abe45cSLaszlo Ersek                                  uint16_t data_key, const char *image_name,
74007abe45cSLaszlo Ersek                                  bool try_decompress)
74107abe45cSLaszlo Ersek {
74207abe45cSLaszlo Ersek     size_t size = -1;
74307abe45cSLaszlo Ersek     uint8_t *data;
74407abe45cSLaszlo Ersek 
74507abe45cSLaszlo Ersek     if (image_name == NULL) {
74607abe45cSLaszlo Ersek         return;
74707abe45cSLaszlo Ersek     }
74807abe45cSLaszlo Ersek 
74907abe45cSLaszlo Ersek     if (try_decompress) {
75007abe45cSLaszlo Ersek         size = load_image_gzipped_buffer(image_name,
75107abe45cSLaszlo Ersek                                          LOAD_IMAGE_MAX_GUNZIP_BYTES, &data);
75207abe45cSLaszlo Ersek     }
75307abe45cSLaszlo Ersek 
75407abe45cSLaszlo Ersek     if (size == (size_t)-1) {
75507abe45cSLaszlo Ersek         gchar *contents;
75607abe45cSLaszlo Ersek         gsize length;
75707abe45cSLaszlo Ersek 
75807abe45cSLaszlo Ersek         if (!g_file_get_contents(image_name, &contents, &length, NULL)) {
759c0dbca36SAlistair Francis             error_report("failed to load \"%s\"", image_name);
76007abe45cSLaszlo Ersek             exit(1);
76107abe45cSLaszlo Ersek         }
76207abe45cSLaszlo Ersek         size = length;
76307abe45cSLaszlo Ersek         data = (uint8_t *)contents;
76407abe45cSLaszlo Ersek     }
76507abe45cSLaszlo Ersek 
76607abe45cSLaszlo Ersek     fw_cfg_add_i32(fw_cfg, size_key, size);
76707abe45cSLaszlo Ersek     fw_cfg_add_bytes(fw_cfg, data_key, data, size);
76807abe45cSLaszlo Ersek }
76907abe45cSLaszlo Ersek 
770d8b1ae42SPeter Maydell static int do_arm_linux_init(Object *obj, void *opaque)
771d8b1ae42SPeter Maydell {
772d8b1ae42SPeter Maydell     if (object_dynamic_cast(obj, TYPE_ARM_LINUX_BOOT_IF)) {
773d8b1ae42SPeter Maydell         ARMLinuxBootIf *albif = ARM_LINUX_BOOT_IF(obj);
774d8b1ae42SPeter Maydell         ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_GET_CLASS(obj);
775d8b1ae42SPeter Maydell         struct arm_boot_info *info = opaque;
776d8b1ae42SPeter Maydell 
777d8b1ae42SPeter Maydell         if (albifc->arm_linux_init) {
778d8b1ae42SPeter Maydell             albifc->arm_linux_init(albif, info->secure_boot);
779d8b1ae42SPeter Maydell         }
780d8b1ae42SPeter Maydell     }
781d8b1ae42SPeter Maydell     return 0;
782d8b1ae42SPeter Maydell }
783d8b1ae42SPeter Maydell 
7849776f636SPeter Crosthwaite static uint64_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry,
7859776f636SPeter Crosthwaite                              uint64_t *lowaddr, uint64_t *highaddr,
7869776f636SPeter Crosthwaite                              int elf_machine)
7879776f636SPeter Crosthwaite {
7889776f636SPeter Crosthwaite     bool elf_is64;
7899776f636SPeter Crosthwaite     union {
7909776f636SPeter Crosthwaite         Elf32_Ehdr h32;
7919776f636SPeter Crosthwaite         Elf64_Ehdr h64;
7929776f636SPeter Crosthwaite     } elf_header;
7939776f636SPeter Crosthwaite     int data_swab = 0;
7949776f636SPeter Crosthwaite     bool big_endian;
7959776f636SPeter Crosthwaite     uint64_t ret = -1;
7969776f636SPeter Crosthwaite     Error *err = NULL;
7979776f636SPeter Crosthwaite 
7989776f636SPeter Crosthwaite 
7999776f636SPeter Crosthwaite     load_elf_hdr(info->kernel_filename, &elf_header, &elf_is64, &err);
8009776f636SPeter Crosthwaite     if (err) {
8019776f636SPeter Crosthwaite         return ret;
8029776f636SPeter Crosthwaite     }
8039776f636SPeter Crosthwaite 
8049776f636SPeter Crosthwaite     if (elf_is64) {
8059776f636SPeter Crosthwaite         big_endian = elf_header.h64.e_ident[EI_DATA] == ELFDATA2MSB;
8069776f636SPeter Crosthwaite         info->endianness = big_endian ? ARM_ENDIANNESS_BE8
8079776f636SPeter Crosthwaite                                       : ARM_ENDIANNESS_LE;
8089776f636SPeter Crosthwaite     } else {
8099776f636SPeter Crosthwaite         big_endian = elf_header.h32.e_ident[EI_DATA] == ELFDATA2MSB;
8109776f636SPeter Crosthwaite         if (big_endian) {
8119776f636SPeter Crosthwaite             if (bswap32(elf_header.h32.e_flags) & EF_ARM_BE8) {
8129776f636SPeter Crosthwaite                 info->endianness = ARM_ENDIANNESS_BE8;
8139776f636SPeter Crosthwaite             } else {
8149776f636SPeter Crosthwaite                 info->endianness = ARM_ENDIANNESS_BE32;
8159776f636SPeter Crosthwaite                 /* In BE32, the CPU has a different view of the per-byte
8169776f636SPeter Crosthwaite                  * address map than the rest of the system. BE32 ELF files
8179776f636SPeter Crosthwaite                  * are organised such that they can be programmed through
8189776f636SPeter Crosthwaite                  * the CPU's per-word byte-reversed view of the world. QEMU
8199776f636SPeter Crosthwaite                  * however loads ELF files independently of the CPU. So
8209776f636SPeter Crosthwaite                  * tell the ELF loader to byte reverse the data for us.
8219776f636SPeter Crosthwaite                  */
8229776f636SPeter Crosthwaite                 data_swab = 2;
8239776f636SPeter Crosthwaite             }
8249776f636SPeter Crosthwaite         } else {
8259776f636SPeter Crosthwaite             info->endianness = ARM_ENDIANNESS_LE;
8269776f636SPeter Crosthwaite         }
8279776f636SPeter Crosthwaite     }
8289776f636SPeter Crosthwaite 
8299776f636SPeter Crosthwaite     ret = load_elf(info->kernel_filename, NULL, NULL,
8309776f636SPeter Crosthwaite                    pentry, lowaddr, highaddr, big_endian, elf_machine,
8319776f636SPeter Crosthwaite                    1, data_swab);
8329776f636SPeter Crosthwaite     if (ret <= 0) {
8339776f636SPeter Crosthwaite         /* The header loaded but the image didn't */
8349776f636SPeter Crosthwaite         exit(1);
8359776f636SPeter Crosthwaite     }
8369776f636SPeter Crosthwaite 
8379776f636SPeter Crosthwaite     return ret;
8389776f636SPeter Crosthwaite }
8399776f636SPeter Crosthwaite 
84068115ed5SArd Biesheuvel static uint64_t load_aarch64_image(const char *filename, hwaddr mem_base,
84168115ed5SArd Biesheuvel                                    hwaddr *entry)
84268115ed5SArd Biesheuvel {
84368115ed5SArd Biesheuvel     hwaddr kernel_load_offset = KERNEL64_LOAD_ADDR;
84468115ed5SArd Biesheuvel     uint8_t *buffer;
84568115ed5SArd Biesheuvel     int size;
84668115ed5SArd Biesheuvel 
84768115ed5SArd Biesheuvel     /* On aarch64, it's the bootloader's job to uncompress the kernel. */
84868115ed5SArd Biesheuvel     size = load_image_gzipped_buffer(filename, LOAD_IMAGE_MAX_GUNZIP_BYTES,
84968115ed5SArd Biesheuvel                                      &buffer);
85068115ed5SArd Biesheuvel 
85168115ed5SArd Biesheuvel     if (size < 0) {
85268115ed5SArd Biesheuvel         gsize len;
85368115ed5SArd Biesheuvel 
85468115ed5SArd Biesheuvel         /* Load as raw file otherwise */
85568115ed5SArd Biesheuvel         if (!g_file_get_contents(filename, (char **)&buffer, &len, NULL)) {
85668115ed5SArd Biesheuvel             return -1;
85768115ed5SArd Biesheuvel         }
85868115ed5SArd Biesheuvel         size = len;
85968115ed5SArd Biesheuvel     }
86068115ed5SArd Biesheuvel 
86168115ed5SArd Biesheuvel     /* check the arm64 magic header value -- very old kernels may not have it */
86268115ed5SArd Biesheuvel     if (memcmp(buffer + ARM64_MAGIC_OFFSET, "ARM\x64", 4) == 0) {
86368115ed5SArd Biesheuvel         uint64_t hdrvals[2];
86468115ed5SArd Biesheuvel 
86568115ed5SArd Biesheuvel         /* The arm64 Image header has text_offset and image_size fields at 8 and
86668115ed5SArd Biesheuvel          * 16 bytes into the Image header, respectively. The text_offset field
86768115ed5SArd Biesheuvel          * is only valid if the image_size is non-zero.
86868115ed5SArd Biesheuvel          */
86968115ed5SArd Biesheuvel         memcpy(&hdrvals, buffer + ARM64_TEXT_OFFSET_OFFSET, sizeof(hdrvals));
87068115ed5SArd Biesheuvel         if (hdrvals[1] != 0) {
87168115ed5SArd Biesheuvel             kernel_load_offset = le64_to_cpu(hdrvals[0]);
87268115ed5SArd Biesheuvel         }
87368115ed5SArd Biesheuvel     }
87468115ed5SArd Biesheuvel 
87568115ed5SArd Biesheuvel     *entry = mem_base + kernel_load_offset;
87668115ed5SArd Biesheuvel     rom_add_blob_fixed(filename, buffer, size, *entry);
87768115ed5SArd Biesheuvel 
87868115ed5SArd Biesheuvel     g_free(buffer);
87968115ed5SArd Biesheuvel 
88068115ed5SArd Biesheuvel     return size;
88168115ed5SArd Biesheuvel }
88268115ed5SArd Biesheuvel 
883ac9d32e3SEric Auger static void arm_load_kernel_notify(Notifier *notifier, void *data)
88453018216SPaolo Bonzini {
885c6faa758SArd Biesheuvel     CPUState *cs;
88653018216SPaolo Bonzini     int kernel_size;
88753018216SPaolo Bonzini     int initrd_size;
88853018216SPaolo Bonzini     int is_linux = 0;
88992df8450SArd Biesheuvel     uint64_t elf_entry, elf_low_addr, elf_high_addr;
890da0af40dSPeter Maydell     int elf_machine;
89168115ed5SArd Biesheuvel     hwaddr entry;
8924d9ebf75SMian M. Hamayun     static const ARMInsnFixup *primary_loader;
893ac9d32e3SEric Auger     ArmLoadKernelNotifier *n = DO_UPCAST(ArmLoadKernelNotifier,
894ac9d32e3SEric Auger                                          notifier, notifier);
895ac9d32e3SEric Auger     ARMCPU *cpu = n->cpu;
896ac9d32e3SEric Auger     struct arm_boot_info *info =
897ac9d32e3SEric Auger         container_of(n, struct arm_boot_info, load_kernel_notifier);
89853018216SPaolo Bonzini 
899baf6b681SPeter Crosthwaite     /* The board code is not supposed to set secure_board_setup unless
900baf6b681SPeter Crosthwaite      * running its code in secure mode is actually possible, and KVM
901baf6b681SPeter Crosthwaite      * doesn't support secure.
902baf6b681SPeter Crosthwaite      */
903baf6b681SPeter Crosthwaite     assert(!(info->secure_board_setup && kvm_enabled()));
904baf6b681SPeter Crosthwaite 
9054c8afda7SMichael Olbrich     info->dtb_filename = qemu_opt_get(qemu_get_machine_opts(), "dtb");
9064c8afda7SMichael Olbrich 
90753018216SPaolo Bonzini     /* Load the kernel.  */
90807abe45cSLaszlo Ersek     if (!info->kernel_filename || info->firmware_loaded) {
90969e7f76fSArd Biesheuvel 
91069e7f76fSArd Biesheuvel         if (have_dtb(info)) {
91107abe45cSLaszlo Ersek             /* If we have a device tree blob, but no kernel to supply it to (or
91207abe45cSLaszlo Ersek              * the kernel is supposed to be loaded by the bootloader), copy the
91307abe45cSLaszlo Ersek              * DTB to the base of RAM for the bootloader to pick up.
91469e7f76fSArd Biesheuvel              */
91569e7f76fSArd Biesheuvel             if (load_dtb(info->loader_start, info, 0) < 0) {
91669e7f76fSArd Biesheuvel                 exit(1);
91769e7f76fSArd Biesheuvel             }
91869e7f76fSArd Biesheuvel         }
91969e7f76fSArd Biesheuvel 
92007abe45cSLaszlo Ersek         if (info->kernel_filename) {
92107abe45cSLaszlo Ersek             FWCfgState *fw_cfg;
92207abe45cSLaszlo Ersek             bool try_decompressing_kernel;
92307abe45cSLaszlo Ersek 
92407abe45cSLaszlo Ersek             fw_cfg = fw_cfg_find();
92507abe45cSLaszlo Ersek             try_decompressing_kernel = arm_feature(&cpu->env,
92607abe45cSLaszlo Ersek                                                    ARM_FEATURE_AARCH64);
92707abe45cSLaszlo Ersek 
92807abe45cSLaszlo Ersek             /* Expose the kernel, the command line, and the initrd in fw_cfg.
92907abe45cSLaszlo Ersek              * We don't process them here at all, it's all left to the
93007abe45cSLaszlo Ersek              * firmware.
93107abe45cSLaszlo Ersek              */
93207abe45cSLaszlo Ersek             load_image_to_fw_cfg(fw_cfg,
93307abe45cSLaszlo Ersek                                  FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA,
93407abe45cSLaszlo Ersek                                  info->kernel_filename,
93507abe45cSLaszlo Ersek                                  try_decompressing_kernel);
93607abe45cSLaszlo Ersek             load_image_to_fw_cfg(fw_cfg,
93707abe45cSLaszlo Ersek                                  FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA,
93807abe45cSLaszlo Ersek                                  info->initrd_filename, false);
93907abe45cSLaszlo Ersek 
94007abe45cSLaszlo Ersek             if (info->kernel_cmdline) {
94107abe45cSLaszlo Ersek                 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
94207abe45cSLaszlo Ersek                                strlen(info->kernel_cmdline) + 1);
94307abe45cSLaszlo Ersek                 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA,
94407abe45cSLaszlo Ersek                                   info->kernel_cmdline);
94507abe45cSLaszlo Ersek             }
94607abe45cSLaszlo Ersek         }
94707abe45cSLaszlo Ersek 
94807abe45cSLaszlo Ersek         /* We will start from address 0 (typically a boot ROM image) in the
94907abe45cSLaszlo Ersek          * same way as hardware.
9509546dbabSPeter Maydell          */
9519546dbabSPeter Maydell         return;
95253018216SPaolo Bonzini     }
95353018216SPaolo Bonzini 
9544d9ebf75SMian M. Hamayun     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
9554d9ebf75SMian M. Hamayun         primary_loader = bootloader_aarch64;
956da0af40dSPeter Maydell         elf_machine = EM_AARCH64;
9574d9ebf75SMian M. Hamayun     } else {
9584d9ebf75SMian M. Hamayun         primary_loader = bootloader;
95910b8ec73SPeter Crosthwaite         if (!info->write_board_setup) {
96010b8ec73SPeter Crosthwaite             primary_loader += BOOTLOADER_NO_BOARD_SETUP_OFFSET;
96110b8ec73SPeter Crosthwaite         }
962da0af40dSPeter Maydell         elf_machine = EM_ARM;
9634d9ebf75SMian M. Hamayun     }
9644d9ebf75SMian M. Hamayun 
96553018216SPaolo Bonzini     if (!info->secondary_cpu_reset_hook) {
96653018216SPaolo Bonzini         info->secondary_cpu_reset_hook = default_reset_secondary;
96753018216SPaolo Bonzini     }
96853018216SPaolo Bonzini     if (!info->write_secondary_boot) {
96953018216SPaolo Bonzini         info->write_secondary_boot = default_write_secondary;
97053018216SPaolo Bonzini     }
97153018216SPaolo Bonzini 
97253018216SPaolo Bonzini     if (info->nb_cpus == 0)
97353018216SPaolo Bonzini         info->nb_cpus = 1;
97453018216SPaolo Bonzini 
97553018216SPaolo Bonzini     /* We want to put the initrd far enough into RAM that when the
97653018216SPaolo Bonzini      * kernel is uncompressed it will not clobber the initrd. However
97753018216SPaolo Bonzini      * on boards without much RAM we must ensure that we still leave
97853018216SPaolo Bonzini      * enough room for a decent sized initrd, and on boards with large
97953018216SPaolo Bonzini      * amounts of RAM we must avoid the initrd being so far up in RAM
98053018216SPaolo Bonzini      * that it is outside lowmem and inaccessible to the kernel.
98153018216SPaolo Bonzini      * So for boards with less  than 256MB of RAM we put the initrd
98253018216SPaolo Bonzini      * halfway into RAM, and for boards with 256MB of RAM or more we put
98353018216SPaolo Bonzini      * the initrd at 128MB.
98453018216SPaolo Bonzini      */
98553018216SPaolo Bonzini     info->initrd_start = info->loader_start +
98653018216SPaolo Bonzini         MIN(info->ram_size / 2, 128 * 1024 * 1024);
98753018216SPaolo Bonzini 
98853018216SPaolo Bonzini     /* Assume that raw images are linux kernels, and ELF images are not.  */
9899776f636SPeter Crosthwaite     kernel_size = arm_load_elf(info, &elf_entry, &elf_low_addr,
9909776f636SPeter Crosthwaite                                &elf_high_addr, elf_machine);
99192df8450SArd Biesheuvel     if (kernel_size > 0 && have_dtb(info)) {
99292df8450SArd Biesheuvel         /* If there is still some room left at the base of RAM, try and put
99392df8450SArd Biesheuvel          * the DTB there like we do for images loaded with -bios or -pflash.
99492df8450SArd Biesheuvel          */
99592df8450SArd Biesheuvel         if (elf_low_addr > info->loader_start
99692df8450SArd Biesheuvel             || elf_high_addr < info->loader_start) {
99792df8450SArd Biesheuvel             /* Pass elf_low_addr as address limit to load_dtb if it may be
99892df8450SArd Biesheuvel              * pointing into RAM, otherwise pass '0' (no limit)
99992df8450SArd Biesheuvel              */
100092df8450SArd Biesheuvel             if (elf_low_addr < info->loader_start) {
100192df8450SArd Biesheuvel                 elf_low_addr = 0;
100292df8450SArd Biesheuvel             }
100392df8450SArd Biesheuvel             if (load_dtb(info->loader_start, info, elf_low_addr) < 0) {
100492df8450SArd Biesheuvel                 exit(1);
100592df8450SArd Biesheuvel             }
100692df8450SArd Biesheuvel         }
100792df8450SArd Biesheuvel     }
100853018216SPaolo Bonzini     entry = elf_entry;
100953018216SPaolo Bonzini     if (kernel_size < 0) {
101053018216SPaolo Bonzini         kernel_size = load_uimage(info->kernel_filename, &entry, NULL,
101125bda50aSMax Filippov                                   &is_linux, NULL, NULL);
101253018216SPaolo Bonzini     }
10136f5d3cbeSRichard W.M. Jones     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && kernel_size < 0) {
101468115ed5SArd Biesheuvel         kernel_size = load_aarch64_image(info->kernel_filename,
101568115ed5SArd Biesheuvel                                          info->loader_start, &entry);
10166f5d3cbeSRichard W.M. Jones         is_linux = 1;
101768115ed5SArd Biesheuvel     } else if (kernel_size < 0) {
101868115ed5SArd Biesheuvel         /* 32-bit ARM */
101968115ed5SArd Biesheuvel         entry = info->loader_start + KERNEL_LOAD_ADDR;
102053018216SPaolo Bonzini         kernel_size = load_image_targphys(info->kernel_filename, entry,
102168115ed5SArd Biesheuvel                                           info->ram_size - KERNEL_LOAD_ADDR);
102253018216SPaolo Bonzini         is_linux = 1;
102353018216SPaolo Bonzini     }
102453018216SPaolo Bonzini     if (kernel_size < 0) {
1025c0dbca36SAlistair Francis         error_report("could not load kernel '%s'", info->kernel_filename);
102653018216SPaolo Bonzini         exit(1);
102753018216SPaolo Bonzini     }
102853018216SPaolo Bonzini     info->entry = entry;
102953018216SPaolo Bonzini     if (is_linux) {
103047b1da81SPeter Maydell         uint32_t fixupcontext[FIXUP_MAX];
103147b1da81SPeter Maydell 
103253018216SPaolo Bonzini         if (info->initrd_filename) {
1033fd76663eSSoren Brinkmann             initrd_size = load_ramdisk(info->initrd_filename,
1034fd76663eSSoren Brinkmann                                        info->initrd_start,
1035fd76663eSSoren Brinkmann                                        info->ram_size -
1036fd76663eSSoren Brinkmann                                        info->initrd_start);
1037fd76663eSSoren Brinkmann             if (initrd_size < 0) {
103853018216SPaolo Bonzini                 initrd_size = load_image_targphys(info->initrd_filename,
103953018216SPaolo Bonzini                                                   info->initrd_start,
104053018216SPaolo Bonzini                                                   info->ram_size -
104153018216SPaolo Bonzini                                                   info->initrd_start);
1042fd76663eSSoren Brinkmann             }
104353018216SPaolo Bonzini             if (initrd_size < 0) {
1044c0dbca36SAlistair Francis                 error_report("could not load initrd '%s'",
104553018216SPaolo Bonzini                              info->initrd_filename);
104653018216SPaolo Bonzini                 exit(1);
104753018216SPaolo Bonzini             }
104853018216SPaolo Bonzini         } else {
104953018216SPaolo Bonzini             initrd_size = 0;
105053018216SPaolo Bonzini         }
105153018216SPaolo Bonzini         info->initrd_size = initrd_size;
105253018216SPaolo Bonzini 
105347b1da81SPeter Maydell         fixupcontext[FIXUP_BOARDID] = info->board_id;
105410b8ec73SPeter Crosthwaite         fixupcontext[FIXUP_BOARD_SETUP] = info->board_setup_addr;
105553018216SPaolo Bonzini 
105653018216SPaolo Bonzini         /* for device tree boot, we pass the DTB directly in r2. Otherwise
105753018216SPaolo Bonzini          * we point to the kernel args.
105853018216SPaolo Bonzini          */
105983bfffecSPeter Maydell         if (have_dtb(info)) {
106076e2aef3SAlexander Graf             hwaddr align;
106176e2aef3SAlexander Graf             hwaddr dtb_start;
106276e2aef3SAlexander Graf 
106376e2aef3SAlexander Graf             if (elf_machine == EM_AARCH64) {
106476e2aef3SAlexander Graf                 /*
106576e2aef3SAlexander Graf                  * Some AArch64 kernels on early bootup map the fdt region as
106676e2aef3SAlexander Graf                  *
106776e2aef3SAlexander Graf                  *   [ ALIGN_DOWN(fdt, 2MB) ... ALIGN_DOWN(fdt, 2MB) + 2MB ]
106876e2aef3SAlexander Graf                  *
106976e2aef3SAlexander Graf                  * Let's play safe and prealign it to 2MB to give us some space.
107053018216SPaolo Bonzini                  */
107176e2aef3SAlexander Graf                 align = 2 * 1024 * 1024;
107276e2aef3SAlexander Graf             } else {
107376e2aef3SAlexander Graf                 /*
107476e2aef3SAlexander Graf                  * Some 32bit kernels will trash anything in the 4K page the
107576e2aef3SAlexander Graf                  * initrd ends in, so make sure the DTB isn't caught up in that.
107676e2aef3SAlexander Graf                  */
107776e2aef3SAlexander Graf                 align = 4096;
107876e2aef3SAlexander Graf             }
107976e2aef3SAlexander Graf 
108076e2aef3SAlexander Graf             /* Place the DTB after the initrd in memory with alignment. */
108176e2aef3SAlexander Graf             dtb_start = QEMU_ALIGN_UP(info->initrd_start + initrd_size, align);
1082fee8ea12SArd Biesheuvel             if (load_dtb(dtb_start, info, 0) < 0) {
108353018216SPaolo Bonzini                 exit(1);
108453018216SPaolo Bonzini             }
108547b1da81SPeter Maydell             fixupcontext[FIXUP_ARGPTR] = dtb_start;
108653018216SPaolo Bonzini         } else {
108747b1da81SPeter Maydell             fixupcontext[FIXUP_ARGPTR] = info->loader_start + KERNEL_ARGS_ADDR;
108853018216SPaolo Bonzini             if (info->ram_size >= (1ULL << 32)) {
1089c0dbca36SAlistair Francis                 error_report("RAM size must be less than 4GB to boot"
109053018216SPaolo Bonzini                              " Linux kernel using ATAGS (try passing a device tree"
1091c0dbca36SAlistair Francis                              " using -dtb)");
109253018216SPaolo Bonzini                 exit(1);
109353018216SPaolo Bonzini             }
109453018216SPaolo Bonzini         }
109547b1da81SPeter Maydell         fixupcontext[FIXUP_ENTRYPOINT] = entry;
109647b1da81SPeter Maydell 
109747b1da81SPeter Maydell         write_bootloader("bootloader", info->loader_start,
10984d9ebf75SMian M. Hamayun                          primary_loader, fixupcontext);
109947b1da81SPeter Maydell 
110053018216SPaolo Bonzini         if (info->nb_cpus > 1) {
110153018216SPaolo Bonzini             info->write_secondary_boot(cpu, info);
110253018216SPaolo Bonzini         }
110310b8ec73SPeter Crosthwaite         if (info->write_board_setup) {
110410b8ec73SPeter Crosthwaite             info->write_board_setup(cpu, info);
110510b8ec73SPeter Crosthwaite         }
1106d8b1ae42SPeter Maydell 
1107d8b1ae42SPeter Maydell         /* Notify devices which need to fake up firmware initialization
1108d8b1ae42SPeter Maydell          * that we're doing a direct kernel boot.
1109d8b1ae42SPeter Maydell          */
1110d8b1ae42SPeter Maydell         object_child_foreach_recursive(object_get_root(),
1111d8b1ae42SPeter Maydell                                        do_arm_linux_init, info);
111253018216SPaolo Bonzini     }
111353018216SPaolo Bonzini     info->is_linux = is_linux;
111453018216SPaolo Bonzini 
1115c6faa758SArd Biesheuvel     for (cs = CPU(cpu); cs; cs = CPU_NEXT(cs)) {
1116c6faa758SArd Biesheuvel         ARM_CPU(cs)->env.boot_info = info;
111753018216SPaolo Bonzini     }
111853018216SPaolo Bonzini }
1119ac9d32e3SEric Auger 
1120ac9d32e3SEric Auger void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
1121ac9d32e3SEric Auger {
112263a183edSEric Auger     CPUState *cs;
112363a183edSEric Auger 
1124ac9d32e3SEric Auger     info->load_kernel_notifier.cpu = cpu;
1125ac9d32e3SEric Auger     info->load_kernel_notifier.notifier.notify = arm_load_kernel_notify;
1126ac9d32e3SEric Auger     qemu_add_machine_init_done_notifier(&info->load_kernel_notifier.notifier);
112763a183edSEric Auger 
112863a183edSEric Auger     /* CPU objects (unlike devices) are not automatically reset on system
112963a183edSEric Auger      * reset, so we must always register a handler to do so. If we're
113063a183edSEric Auger      * actually loading a kernel, the handler is also responsible for
113163a183edSEric Auger      * arranging that we start it correctly.
113263a183edSEric Auger      */
113363a183edSEric Auger     for (cs = CPU(cpu); cs; cs = CPU_NEXT(cs)) {
113463a183edSEric Auger         qemu_register_reset(do_cpu_reset, ARM_CPU(cs));
113563a183edSEric Auger     }
1136ac9d32e3SEric Auger }
1137d8b1ae42SPeter Maydell 
1138d8b1ae42SPeter Maydell static const TypeInfo arm_linux_boot_if_info = {
1139d8b1ae42SPeter Maydell     .name = TYPE_ARM_LINUX_BOOT_IF,
1140d8b1ae42SPeter Maydell     .parent = TYPE_INTERFACE,
1141d8b1ae42SPeter Maydell     .class_size = sizeof(ARMLinuxBootIfClass),
1142d8b1ae42SPeter Maydell };
1143d8b1ae42SPeter Maydell 
1144d8b1ae42SPeter Maydell static void arm_linux_boot_register_types(void)
1145d8b1ae42SPeter Maydell {
1146d8b1ae42SPeter Maydell     type_register_static(&arm_linux_boot_if_info);
1147d8b1ae42SPeter Maydell }
1148d8b1ae42SPeter Maydell 
1149d8b1ae42SPeter Maydell type_init(arm_linux_boot_register_types)
1150