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"
112c65db5eSPaolo Bonzini #include "qemu/datadir.h"
12c0dbca36SAlistair Francis #include "qemu/error-report.h"
13da34e65cSMarkus Armbruster #include "qapi/error.h"
14b77257d7SGuenter Roeck #include <libfdt.h>
1512ec8bd5SPeter Maydell #include "hw/arm/boot.h"
16d8b1ae42SPeter Maydell #include "hw/arm/linux-boot-if.h"
17baf6b681SPeter Crosthwaite #include "sysemu/kvm.h"
182b77ad4dSFabiano Rosas #include "sysemu/tcg.h"
1953018216SPaolo Bonzini #include "sysemu/sysemu.h"
209695200aSShannon Zhao #include "sysemu/numa.h"
212744ece8STao Xu #include "hw/boards.h"
2271e8a915SMarkus Armbruster #include "sysemu/reset.h"
2353018216SPaolo Bonzini #include "hw/loader.h"
2453018216SPaolo Bonzini #include "elf.h"
2553018216SPaolo Bonzini #include "sysemu/device_tree.h"
2653018216SPaolo Bonzini #include "qemu/config-file.h"
27922a01a0SMarkus Armbruster #include "qemu/option.h"
28ea358872SStewart Hildebrand #include "qemu/units.h"
2953018216SPaolo Bonzini
304d9ebf75SMian M. Hamayun /* Kernel boot protocol is specified in the kernel docs
314d9ebf75SMian M. Hamayun * Documentation/arm/Booting and Documentation/arm64/booting.txt
324d9ebf75SMian M. Hamayun * They have different preferred image load offsets from system RAM base.
334d9ebf75SMian M. Hamayun */
3453018216SPaolo Bonzini #define KERNEL_ARGS_ADDR 0x100
35f831f955SNick Hudson #define KERNEL_NOLOAD_ADDR 0x02000000
3653018216SPaolo Bonzini #define KERNEL_LOAD_ADDR 0x00010000
374d9ebf75SMian M. Hamayun #define KERNEL64_LOAD_ADDR 0x00080000
3853018216SPaolo Bonzini
3968115ed5SArd Biesheuvel #define ARM64_TEXT_OFFSET_OFFSET 8
4068115ed5SArd Biesheuvel #define ARM64_MAGIC_OFFSET 56
4168115ed5SArd Biesheuvel
42ea358872SStewart Hildebrand #define BOOTLOADER_MAX_SIZE (4 * KiB)
43ea358872SStewart Hildebrand
arm_boot_address_space(ARMCPU * cpu,const struct arm_boot_info * info)443b77f6c3SIgor Mammedov AddressSpace *arm_boot_address_space(ARMCPU *cpu,
459f43d4c3SPeter Maydell const struct arm_boot_info *info)
469f43d4c3SPeter Maydell {
479f43d4c3SPeter Maydell /* Return the address space to use for bootloader reads and writes.
489f43d4c3SPeter Maydell * We prefer the secure address space if the CPU has it and we're
499f43d4c3SPeter Maydell * going to boot the guest into it.
509f43d4c3SPeter Maydell */
519f43d4c3SPeter Maydell int asidx;
529f43d4c3SPeter Maydell CPUState *cs = CPU(cpu);
539f43d4c3SPeter Maydell
549f43d4c3SPeter Maydell if (arm_feature(&cpu->env, ARM_FEATURE_EL3) && info->secure_boot) {
559f43d4c3SPeter Maydell asidx = ARMASIdx_S;
569f43d4c3SPeter Maydell } else {
579f43d4c3SPeter Maydell asidx = ARMASIdx_NS;
589f43d4c3SPeter Maydell }
599f43d4c3SPeter Maydell
609f43d4c3SPeter Maydell return cpu_get_address_space(cs, asidx);
619f43d4c3SPeter Maydell }
629f43d4c3SPeter Maydell
634d9ebf75SMian M. Hamayun static const ARMInsnFixup bootloader_aarch64[] = {
644d9ebf75SMian M. Hamayun { 0x580000c0 }, /* ldr x0, arg ; Load the lower 32-bits of DTB */
654d9ebf75SMian M. Hamayun { 0xaa1f03e1 }, /* mov x1, xzr */
664d9ebf75SMian M. Hamayun { 0xaa1f03e2 }, /* mov x2, xzr */
674d9ebf75SMian M. Hamayun { 0xaa1f03e3 }, /* mov x3, xzr */
684d9ebf75SMian M. Hamayun { 0x58000084 }, /* ldr x4, entry ; Load the lower 32-bits of kernel entry */
694d9ebf75SMian M. Hamayun { 0xd61f0080 }, /* br x4 ; Jump to the kernel entry point */
70751ebc13SRicardo Perez Blanco { 0, FIXUP_ARGPTR_LO }, /* arg: .word @DTB Lower 32-bits */
71751ebc13SRicardo Perez Blanco { 0, FIXUP_ARGPTR_HI}, /* .word @DTB Higher 32-bits */
72751ebc13SRicardo Perez Blanco { 0, FIXUP_ENTRYPOINT_LO }, /* entry: .word @Kernel Entry Lower 32-bits */
73751ebc13SRicardo Perez Blanco { 0, FIXUP_ENTRYPOINT_HI }, /* .word @Kernel Entry Higher 32-bits */
744d9ebf75SMian M. Hamayun { 0, FIXUP_TERMINATOR }
754d9ebf75SMian M. Hamayun };
764d9ebf75SMian M. Hamayun
7710b8ec73SPeter Crosthwaite /* A very small bootloader: call the board-setup code (if needed),
7810b8ec73SPeter Crosthwaite * set r0-r2, then jump to the kernel.
7910b8ec73SPeter Crosthwaite * If we're not calling boot setup code then we don't copy across
8010b8ec73SPeter Crosthwaite * the first BOOTLOADER_NO_BOARD_SETUP_OFFSET insns in this array.
8110b8ec73SPeter Crosthwaite */
8210b8ec73SPeter Crosthwaite
8347b1da81SPeter Maydell static const ARMInsnFixup bootloader[] = {
84b4850e5aSSylvain Garrigues { 0xe28fe004 }, /* add lr, pc, #4 */
8510b8ec73SPeter Crosthwaite { 0xe51ff004 }, /* ldr pc, [pc, #-4] */
8610b8ec73SPeter Crosthwaite { 0, FIXUP_BOARD_SETUP },
8710b8ec73SPeter Crosthwaite #define BOOTLOADER_NO_BOARD_SETUP_OFFSET 3
8847b1da81SPeter Maydell { 0xe3a00000 }, /* mov r0, #0 */
8947b1da81SPeter Maydell { 0xe59f1004 }, /* ldr r1, [pc, #4] */
9047b1da81SPeter Maydell { 0xe59f2004 }, /* ldr r2, [pc, #4] */
9147b1da81SPeter Maydell { 0xe59ff004 }, /* ldr pc, [pc, #4] */
9247b1da81SPeter Maydell { 0, FIXUP_BOARDID },
93751ebc13SRicardo Perez Blanco { 0, FIXUP_ARGPTR_LO },
94751ebc13SRicardo Perez Blanco { 0, FIXUP_ENTRYPOINT_LO },
9547b1da81SPeter Maydell { 0, FIXUP_TERMINATOR }
9653018216SPaolo Bonzini };
9753018216SPaolo Bonzini
9853018216SPaolo Bonzini /* Handling for secondary CPU boot in a multicore system.
9953018216SPaolo Bonzini * Unlike the uniprocessor/primary CPU boot, this is platform
10053018216SPaolo Bonzini * dependent. The default code here is based on the secondary
10153018216SPaolo Bonzini * CPU boot protocol used on realview/vexpress boards, with
10253018216SPaolo Bonzini * some parameterisation to increase its flexibility.
10353018216SPaolo Bonzini * QEMU platform models for which this code is not appropriate
10453018216SPaolo Bonzini * should override write_secondary_boot and secondary_cpu_reset_hook
10553018216SPaolo Bonzini * instead.
10653018216SPaolo Bonzini *
10753018216SPaolo Bonzini * This code enables the interrupt controllers for the secondary
10853018216SPaolo Bonzini * CPUs and then puts all the secondary CPUs into a loop waiting
10953018216SPaolo Bonzini * for an interprocessor interrupt and polling a configurable
11053018216SPaolo Bonzini * location for the kernel secondary CPU entry point.
11153018216SPaolo Bonzini */
11253018216SPaolo Bonzini #define DSB_INSN 0xf57ff04f
11353018216SPaolo Bonzini #define CP15_DSB_INSN 0xee070f9a /* mcr cp15, 0, r0, c7, c10, 4 */
11453018216SPaolo Bonzini
11547b1da81SPeter Maydell static const ARMInsnFixup smpboot[] = {
11647b1da81SPeter Maydell { 0xe59f2028 }, /* ldr r2, gic_cpu_if */
11747b1da81SPeter Maydell { 0xe59f0028 }, /* ldr r0, bootreg_addr */
11847b1da81SPeter Maydell { 0xe3a01001 }, /* mov r1, #1 */
11947b1da81SPeter Maydell { 0xe5821000 }, /* str r1, [r2] - set GICC_CTLR.Enable */
12047b1da81SPeter Maydell { 0xe3a010ff }, /* mov r1, #0xff */
12147b1da81SPeter Maydell { 0xe5821004 }, /* str r1, [r2, 4] - set GIC_PMR.Priority to 0xff */
12247b1da81SPeter Maydell { 0, FIXUP_DSB }, /* dsb */
12347b1da81SPeter Maydell { 0xe320f003 }, /* wfi */
12447b1da81SPeter Maydell { 0xe5901000 }, /* ldr r1, [r0] */
12547b1da81SPeter Maydell { 0xe1110001 }, /* tst r1, r1 */
12647b1da81SPeter Maydell { 0x0afffffb }, /* beq <wfi> */
12747b1da81SPeter Maydell { 0xe12fff11 }, /* bx r1 */
12847b1da81SPeter Maydell { 0, FIXUP_GIC_CPU_IF }, /* gic_cpu_if: .word 0x.... */
12947b1da81SPeter Maydell { 0, FIXUP_BOOTREG }, /* bootreg_addr: .word 0x.... */
13047b1da81SPeter Maydell { 0, FIXUP_TERMINATOR }
13153018216SPaolo Bonzini };
13253018216SPaolo Bonzini
arm_write_bootloader(const char * name,AddressSpace * as,hwaddr addr,const ARMInsnFixup * insns,const uint32_t * fixupcontext)1330fe43f0aSCédric Le Goater void arm_write_bootloader(const char *name,
1340fe43f0aSCédric Le Goater AddressSpace *as, hwaddr addr,
1350fe43f0aSCédric Le Goater const ARMInsnFixup *insns,
1360fe43f0aSCédric Le Goater const uint32_t *fixupcontext)
13747b1da81SPeter Maydell {
13847b1da81SPeter Maydell /* Fix up the specified bootloader fragment and write it into
13947b1da81SPeter Maydell * guest memory using rom_add_blob_fixed(). fixupcontext is
14047b1da81SPeter Maydell * an array giving the values to write in for the fixup types
14147b1da81SPeter Maydell * which write a value into the code array.
14247b1da81SPeter Maydell */
14347b1da81SPeter Maydell int i, len;
14447b1da81SPeter Maydell uint32_t *code;
14547b1da81SPeter Maydell
14647b1da81SPeter Maydell len = 0;
14747b1da81SPeter Maydell while (insns[len].fixup != FIXUP_TERMINATOR) {
14847b1da81SPeter Maydell len++;
14947b1da81SPeter Maydell }
15047b1da81SPeter Maydell
15147b1da81SPeter Maydell code = g_new0(uint32_t, len);
15247b1da81SPeter Maydell
15347b1da81SPeter Maydell for (i = 0; i < len; i++) {
15447b1da81SPeter Maydell uint32_t insn = insns[i].insn;
15547b1da81SPeter Maydell FixupType fixup = insns[i].fixup;
15647b1da81SPeter Maydell
15747b1da81SPeter Maydell switch (fixup) {
15847b1da81SPeter Maydell case FIXUP_NONE:
15947b1da81SPeter Maydell break;
16047b1da81SPeter Maydell case FIXUP_BOARDID:
16110b8ec73SPeter Crosthwaite case FIXUP_BOARD_SETUP:
162751ebc13SRicardo Perez Blanco case FIXUP_ARGPTR_LO:
163751ebc13SRicardo Perez Blanco case FIXUP_ARGPTR_HI:
164751ebc13SRicardo Perez Blanco case FIXUP_ENTRYPOINT_LO:
165751ebc13SRicardo Perez Blanco case FIXUP_ENTRYPOINT_HI:
16647b1da81SPeter Maydell case FIXUP_GIC_CPU_IF:
16747b1da81SPeter Maydell case FIXUP_BOOTREG:
16847b1da81SPeter Maydell case FIXUP_DSB:
16947b1da81SPeter Maydell insn = fixupcontext[fixup];
17047b1da81SPeter Maydell break;
17147b1da81SPeter Maydell default:
17247b1da81SPeter Maydell abort();
17347b1da81SPeter Maydell }
17447b1da81SPeter Maydell code[i] = tswap32(insn);
17547b1da81SPeter Maydell }
17647b1da81SPeter Maydell
177ea358872SStewart Hildebrand assert((len * sizeof(uint32_t)) < BOOTLOADER_MAX_SIZE);
178ea358872SStewart Hildebrand
1799f43d4c3SPeter Maydell rom_add_blob_fixed_as(name, code, len * sizeof(uint32_t), addr, as);
18047b1da81SPeter Maydell
18147b1da81SPeter Maydell g_free(code);
18247b1da81SPeter Maydell }
18347b1da81SPeter Maydell
default_write_secondary(ARMCPU * cpu,const struct arm_boot_info * info)18453018216SPaolo Bonzini static void default_write_secondary(ARMCPU *cpu,
18553018216SPaolo Bonzini const struct arm_boot_info *info)
18653018216SPaolo Bonzini {
18747b1da81SPeter Maydell uint32_t fixupcontext[FIXUP_MAX];
1889f43d4c3SPeter Maydell AddressSpace *as = arm_boot_address_space(cpu, info);
18947b1da81SPeter Maydell
19047b1da81SPeter Maydell fixupcontext[FIXUP_GIC_CPU_IF] = info->gic_cpu_if_addr;
19147b1da81SPeter Maydell fixupcontext[FIXUP_BOOTREG] = info->smp_bootreg_addr;
19247b1da81SPeter Maydell if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
19347b1da81SPeter Maydell fixupcontext[FIXUP_DSB] = DSB_INSN;
19447b1da81SPeter Maydell } else {
19547b1da81SPeter Maydell fixupcontext[FIXUP_DSB] = CP15_DSB_INSN;
19653018216SPaolo Bonzini }
19747b1da81SPeter Maydell
1980fe43f0aSCédric Le Goater arm_write_bootloader("smpboot", as, info->smp_loader_start,
1990fe43f0aSCédric Le Goater smpboot, fixupcontext);
20053018216SPaolo Bonzini }
20153018216SPaolo Bonzini
arm_write_secure_board_setup_dummy_smc(ARMCPU * cpu,const struct arm_boot_info * info,hwaddr mvbar_addr)202716536a9SAndrew Baumann void arm_write_secure_board_setup_dummy_smc(ARMCPU *cpu,
203716536a9SAndrew Baumann const struct arm_boot_info *info,
204716536a9SAndrew Baumann hwaddr mvbar_addr)
205716536a9SAndrew Baumann {
2069f43d4c3SPeter Maydell AddressSpace *as = arm_boot_address_space(cpu, info);
207716536a9SAndrew Baumann int n;
208716536a9SAndrew Baumann uint32_t mvbar_blob[] = {
209716536a9SAndrew Baumann /* mvbar_addr: secure monitor vectors
210716536a9SAndrew Baumann * Default unimplemented and unused vectors to spin. Makes it
211716536a9SAndrew Baumann * easier to debug (as opposed to the CPU running away).
212716536a9SAndrew Baumann */
213716536a9SAndrew Baumann 0xeafffffe, /* (spin) */
214716536a9SAndrew Baumann 0xeafffffe, /* (spin) */
215716536a9SAndrew Baumann 0xe1b0f00e, /* movs pc, lr ;SMC exception return */
216716536a9SAndrew Baumann 0xeafffffe, /* (spin) */
217716536a9SAndrew Baumann 0xeafffffe, /* (spin) */
218716536a9SAndrew Baumann 0xeafffffe, /* (spin) */
219716536a9SAndrew Baumann 0xeafffffe, /* (spin) */
220716536a9SAndrew Baumann 0xeafffffe, /* (spin) */
221716536a9SAndrew Baumann };
222716536a9SAndrew Baumann uint32_t board_setup_blob[] = {
223716536a9SAndrew Baumann /* board setup addr */
22445c078f1SClement Deschamps 0xee110f51, /* mrc p15, 0, r0, c1, c1, 2 ;read NSACR */
22545c078f1SClement Deschamps 0xe3800b03, /* orr r0, #0xc00 ;set CP11, CP10 */
22645c078f1SClement Deschamps 0xee010f51, /* mcr p15, 0, r0, c1, c1, 2 ;write NSACR */
227716536a9SAndrew Baumann 0xe3a00e00 + (mvbar_addr >> 4), /* mov r0, #mvbar_addr */
228716536a9SAndrew Baumann 0xee0c0f30, /* mcr p15, 0, r0, c12, c0, 1 ;set MVBAR */
229716536a9SAndrew Baumann 0xee110f11, /* mrc p15, 0, r0, c1 , c1, 0 ;read SCR */
230716536a9SAndrew Baumann 0xe3800031, /* orr r0, #0x31 ;enable AW, FW, NS */
231716536a9SAndrew Baumann 0xee010f11, /* mcr p15, 0, r0, c1, c1, 0 ;write SCR */
232716536a9SAndrew Baumann 0xe1a0100e, /* mov r1, lr ;save LR across SMC */
233716536a9SAndrew Baumann 0xe1600070, /* smc #0 ;call monitor to flush SCR */
234716536a9SAndrew Baumann 0xe1a0f001, /* mov pc, r1 ;return */
235716536a9SAndrew Baumann };
236716536a9SAndrew Baumann
237716536a9SAndrew Baumann /* check that mvbar_addr is correctly aligned and relocatable (using MOV) */
238716536a9SAndrew Baumann assert((mvbar_addr & 0x1f) == 0 && (mvbar_addr >> 4) < 0x100);
239716536a9SAndrew Baumann
240716536a9SAndrew Baumann /* check that these blobs don't overlap */
241716536a9SAndrew Baumann assert((mvbar_addr + sizeof(mvbar_blob) <= info->board_setup_addr)
242716536a9SAndrew Baumann || (info->board_setup_addr + sizeof(board_setup_blob) <= mvbar_addr));
243716536a9SAndrew Baumann
244716536a9SAndrew Baumann for (n = 0; n < ARRAY_SIZE(mvbar_blob); n++) {
245716536a9SAndrew Baumann mvbar_blob[n] = tswap32(mvbar_blob[n]);
246716536a9SAndrew Baumann }
2479f43d4c3SPeter Maydell rom_add_blob_fixed_as("board-setup-mvbar", mvbar_blob, sizeof(mvbar_blob),
2489f43d4c3SPeter Maydell mvbar_addr, as);
249716536a9SAndrew Baumann
250716536a9SAndrew Baumann for (n = 0; n < ARRAY_SIZE(board_setup_blob); n++) {
251716536a9SAndrew Baumann board_setup_blob[n] = tswap32(board_setup_blob[n]);
252716536a9SAndrew Baumann }
2539f43d4c3SPeter Maydell rom_add_blob_fixed_as("board-setup", board_setup_blob,
2549f43d4c3SPeter Maydell sizeof(board_setup_blob), info->board_setup_addr, as);
255716536a9SAndrew Baumann }
256716536a9SAndrew Baumann
default_reset_secondary(ARMCPU * cpu,const struct arm_boot_info * info)25753018216SPaolo Bonzini static void default_reset_secondary(ARMCPU *cpu,
25853018216SPaolo Bonzini const struct arm_boot_info *info)
25953018216SPaolo Bonzini {
2609f43d4c3SPeter Maydell AddressSpace *as = arm_boot_address_space(cpu, info);
2614df81c6eSPeter Crosthwaite CPUState *cs = CPU(cpu);
26253018216SPaolo Bonzini
2639f43d4c3SPeter Maydell address_space_stl_notdirty(as, info->smp_bootreg_addr,
26442874d3aSPeter Maydell 0, MEMTXATTRS_UNSPECIFIED, NULL);
2654df81c6eSPeter Crosthwaite cpu_set_pc(cs, info->smp_loader_start);
26653018216SPaolo Bonzini }
26753018216SPaolo Bonzini
have_dtb(const struct arm_boot_info * info)26883bfffecSPeter Maydell static inline bool have_dtb(const struct arm_boot_info *info)
26983bfffecSPeter Maydell {
27083bfffecSPeter Maydell return info->dtb_filename || info->get_dtb;
27183bfffecSPeter Maydell }
27283bfffecSPeter Maydell
27353018216SPaolo Bonzini #define WRITE_WORD(p, value) do { \
2749f43d4c3SPeter Maydell address_space_stl_notdirty(as, p, value, \
27542874d3aSPeter Maydell MEMTXATTRS_UNSPECIFIED, NULL); \
27653018216SPaolo Bonzini p += 4; \
27753018216SPaolo Bonzini } while (0)
27853018216SPaolo Bonzini
set_kernel_args(const struct arm_boot_info * info,AddressSpace * as)2799f43d4c3SPeter Maydell static void set_kernel_args(const struct arm_boot_info *info, AddressSpace *as)
28053018216SPaolo Bonzini {
28153018216SPaolo Bonzini int initrd_size = info->initrd_size;
28253018216SPaolo Bonzini hwaddr base = info->loader_start;
28353018216SPaolo Bonzini hwaddr p;
28453018216SPaolo Bonzini
28553018216SPaolo Bonzini p = base + KERNEL_ARGS_ADDR;
28653018216SPaolo Bonzini /* ATAG_CORE */
28753018216SPaolo Bonzini WRITE_WORD(p, 5);
28853018216SPaolo Bonzini WRITE_WORD(p, 0x54410001);
28953018216SPaolo Bonzini WRITE_WORD(p, 1);
29053018216SPaolo Bonzini WRITE_WORD(p, 0x1000);
29153018216SPaolo Bonzini WRITE_WORD(p, 0);
29253018216SPaolo Bonzini /* ATAG_MEM */
29353018216SPaolo Bonzini /* TODO: handle multiple chips on one ATAG list */
29453018216SPaolo Bonzini WRITE_WORD(p, 4);
29553018216SPaolo Bonzini WRITE_WORD(p, 0x54410002);
29653018216SPaolo Bonzini WRITE_WORD(p, info->ram_size);
29753018216SPaolo Bonzini WRITE_WORD(p, info->loader_start);
29853018216SPaolo Bonzini if (initrd_size) {
29953018216SPaolo Bonzini /* ATAG_INITRD2 */
30053018216SPaolo Bonzini WRITE_WORD(p, 4);
30153018216SPaolo Bonzini WRITE_WORD(p, 0x54420005);
30253018216SPaolo Bonzini WRITE_WORD(p, info->initrd_start);
30353018216SPaolo Bonzini WRITE_WORD(p, initrd_size);
30453018216SPaolo Bonzini }
30553018216SPaolo Bonzini if (info->kernel_cmdline && *info->kernel_cmdline) {
30653018216SPaolo Bonzini /* ATAG_CMDLINE */
30753018216SPaolo Bonzini int cmdline_size;
30853018216SPaolo Bonzini
30953018216SPaolo Bonzini cmdline_size = strlen(info->kernel_cmdline);
3109f43d4c3SPeter Maydell address_space_write(as, p + 8, MEMTXATTRS_UNSPECIFIED,
311b7cbebf2SPhilippe Mathieu-Daudé info->kernel_cmdline, cmdline_size + 1);
31253018216SPaolo Bonzini cmdline_size = (cmdline_size >> 2) + 1;
31353018216SPaolo Bonzini WRITE_WORD(p, cmdline_size + 2);
31453018216SPaolo Bonzini WRITE_WORD(p, 0x54410009);
31553018216SPaolo Bonzini p += cmdline_size * 4;
31653018216SPaolo Bonzini }
31753018216SPaolo Bonzini if (info->atag_board) {
31853018216SPaolo Bonzini /* ATAG_BOARD */
31953018216SPaolo Bonzini int atag_board_len;
32053018216SPaolo Bonzini uint8_t atag_board_buf[0x1000];
32153018216SPaolo Bonzini
32253018216SPaolo Bonzini atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3;
32353018216SPaolo Bonzini WRITE_WORD(p, (atag_board_len + 8) >> 2);
32453018216SPaolo Bonzini WRITE_WORD(p, 0x414f4d50);
3259f43d4c3SPeter Maydell address_space_write(as, p, MEMTXATTRS_UNSPECIFIED,
3269f43d4c3SPeter Maydell atag_board_buf, atag_board_len);
32753018216SPaolo Bonzini p += atag_board_len;
32853018216SPaolo Bonzini }
32953018216SPaolo Bonzini /* ATAG_END */
33053018216SPaolo Bonzini WRITE_WORD(p, 0);
33153018216SPaolo Bonzini WRITE_WORD(p, 0);
33253018216SPaolo Bonzini }
33353018216SPaolo Bonzini
set_kernel_args_old(const struct arm_boot_info * info,AddressSpace * as)3349f43d4c3SPeter Maydell static void set_kernel_args_old(const struct arm_boot_info *info,
3359f43d4c3SPeter Maydell AddressSpace *as)
33653018216SPaolo Bonzini {
33753018216SPaolo Bonzini hwaddr p;
33853018216SPaolo Bonzini const char *s;
33953018216SPaolo Bonzini int initrd_size = info->initrd_size;
34053018216SPaolo Bonzini hwaddr base = info->loader_start;
34153018216SPaolo Bonzini
34253018216SPaolo Bonzini /* see linux/include/asm-arm/setup.h */
34353018216SPaolo Bonzini p = base + KERNEL_ARGS_ADDR;
34453018216SPaolo Bonzini /* page_size */
34553018216SPaolo Bonzini WRITE_WORD(p, 4096);
34653018216SPaolo Bonzini /* nr_pages */
34753018216SPaolo Bonzini WRITE_WORD(p, info->ram_size / 4096);
34853018216SPaolo Bonzini /* ramdisk_size */
34953018216SPaolo Bonzini WRITE_WORD(p, 0);
35053018216SPaolo Bonzini #define FLAG_READONLY 1
35153018216SPaolo Bonzini #define FLAG_RDLOAD 4
35253018216SPaolo Bonzini #define FLAG_RDPROMPT 8
35353018216SPaolo Bonzini /* flags */
35453018216SPaolo Bonzini WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT);
35553018216SPaolo Bonzini /* rootdev */
35653018216SPaolo Bonzini WRITE_WORD(p, (31 << 8) | 0); /* /dev/mtdblock0 */
35753018216SPaolo Bonzini /* video_num_cols */
35853018216SPaolo Bonzini WRITE_WORD(p, 0);
35953018216SPaolo Bonzini /* video_num_rows */
36053018216SPaolo Bonzini WRITE_WORD(p, 0);
36153018216SPaolo Bonzini /* video_x */
36253018216SPaolo Bonzini WRITE_WORD(p, 0);
36353018216SPaolo Bonzini /* video_y */
36453018216SPaolo Bonzini WRITE_WORD(p, 0);
36553018216SPaolo Bonzini /* memc_control_reg */
36653018216SPaolo Bonzini WRITE_WORD(p, 0);
36753018216SPaolo Bonzini /* unsigned char sounddefault */
36853018216SPaolo Bonzini /* unsigned char adfsdrives */
36953018216SPaolo Bonzini /* unsigned char bytes_per_char_h */
37053018216SPaolo Bonzini /* unsigned char bytes_per_char_v */
37153018216SPaolo Bonzini WRITE_WORD(p, 0);
37253018216SPaolo Bonzini /* pages_in_bank[4] */
37353018216SPaolo Bonzini WRITE_WORD(p, 0);
37453018216SPaolo Bonzini WRITE_WORD(p, 0);
37553018216SPaolo Bonzini WRITE_WORD(p, 0);
37653018216SPaolo Bonzini WRITE_WORD(p, 0);
37753018216SPaolo Bonzini /* pages_in_vram */
37853018216SPaolo Bonzini WRITE_WORD(p, 0);
37953018216SPaolo Bonzini /* initrd_start */
38053018216SPaolo Bonzini if (initrd_size) {
38153018216SPaolo Bonzini WRITE_WORD(p, info->initrd_start);
38253018216SPaolo Bonzini } else {
38353018216SPaolo Bonzini WRITE_WORD(p, 0);
38453018216SPaolo Bonzini }
38553018216SPaolo Bonzini /* initrd_size */
38653018216SPaolo Bonzini WRITE_WORD(p, initrd_size);
38753018216SPaolo Bonzini /* rd_start */
38853018216SPaolo Bonzini WRITE_WORD(p, 0);
38953018216SPaolo Bonzini /* system_rev */
39053018216SPaolo Bonzini WRITE_WORD(p, 0);
39153018216SPaolo Bonzini /* system_serial_low */
39253018216SPaolo Bonzini WRITE_WORD(p, 0);
39353018216SPaolo Bonzini /* system_serial_high */
39453018216SPaolo Bonzini WRITE_WORD(p, 0);
39553018216SPaolo Bonzini /* mem_fclk_21285 */
39653018216SPaolo Bonzini WRITE_WORD(p, 0);
39753018216SPaolo Bonzini /* zero unused fields */
39853018216SPaolo Bonzini while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) {
39953018216SPaolo Bonzini WRITE_WORD(p, 0);
40053018216SPaolo Bonzini }
40153018216SPaolo Bonzini s = info->kernel_cmdline;
40253018216SPaolo Bonzini if (s) {
403b7cbebf2SPhilippe Mathieu-Daudé address_space_write(as, p, MEMTXATTRS_UNSPECIFIED, s, strlen(s) + 1);
40453018216SPaolo Bonzini } else {
40553018216SPaolo Bonzini WRITE_WORD(p, 0);
40653018216SPaolo Bonzini }
40753018216SPaolo Bonzini }
40853018216SPaolo Bonzini
fdt_add_memory_node(void * fdt,uint32_t acells,hwaddr mem_base,uint32_t scells,hwaddr mem_len,int numa_node_id)409f08ced69SShameer Kolothum static int fdt_add_memory_node(void *fdt, uint32_t acells, hwaddr mem_base,
410f08ced69SShameer Kolothum uint32_t scells, hwaddr mem_len,
411f08ced69SShameer Kolothum int numa_node_id)
412f08ced69SShameer Kolothum {
413f08ced69SShameer Kolothum char *nodename;
414f08ced69SShameer Kolothum int ret;
415f08ced69SShameer Kolothum
416f08ced69SShameer Kolothum nodename = g_strdup_printf("/memory@%" PRIx64, mem_base);
417f08ced69SShameer Kolothum qemu_fdt_add_subnode(fdt, nodename);
418f08ced69SShameer Kolothum qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory");
419f08ced69SShameer Kolothum ret = qemu_fdt_setprop_sized_cells(fdt, nodename, "reg", acells, mem_base,
420f08ced69SShameer Kolothum scells, mem_len);
421f08ced69SShameer Kolothum if (ret < 0) {
422f08ced69SShameer Kolothum goto out;
423f08ced69SShameer Kolothum }
424f08ced69SShameer Kolothum
425f08ced69SShameer Kolothum /* only set the NUMA ID if it is specified */
426f08ced69SShameer Kolothum if (numa_node_id >= 0) {
427f08ced69SShameer Kolothum ret = qemu_fdt_setprop_cell(fdt, nodename,
428f08ced69SShameer Kolothum "numa-node-id", numa_node_id);
429f08ced69SShameer Kolothum }
430f08ced69SShameer Kolothum out:
431f08ced69SShameer Kolothum g_free(nodename);
432f08ced69SShameer Kolothum return ret;
433f08ced69SShameer Kolothum }
434f08ced69SShameer Kolothum
fdt_add_psci_node(void * fdt)4354cbca7d9SAndrey Smirnov static void fdt_add_psci_node(void *fdt)
4364cbca7d9SAndrey Smirnov {
4374cbca7d9SAndrey Smirnov uint32_t cpu_suspend_fn;
4384cbca7d9SAndrey Smirnov uint32_t cpu_off_fn;
4394cbca7d9SAndrey Smirnov uint32_t cpu_on_fn;
4404cbca7d9SAndrey Smirnov uint32_t migrate_fn;
4414cbca7d9SAndrey Smirnov ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(0));
4424cbca7d9SAndrey Smirnov const char *psci_method;
4434cbca7d9SAndrey Smirnov int64_t psci_conduit;
444c39770cdSAndrey Smirnov int rc;
4454cbca7d9SAndrey Smirnov
4464cbca7d9SAndrey Smirnov psci_conduit = object_property_get_int(OBJECT(armcpu),
4474cbca7d9SAndrey Smirnov "psci-conduit",
4484cbca7d9SAndrey Smirnov &error_abort);
4494cbca7d9SAndrey Smirnov switch (psci_conduit) {
4504cbca7d9SAndrey Smirnov case QEMU_PSCI_CONDUIT_DISABLED:
4514cbca7d9SAndrey Smirnov return;
4524cbca7d9SAndrey Smirnov case QEMU_PSCI_CONDUIT_HVC:
4534cbca7d9SAndrey Smirnov psci_method = "hvc";
4544cbca7d9SAndrey Smirnov break;
4554cbca7d9SAndrey Smirnov case QEMU_PSCI_CONDUIT_SMC:
4564cbca7d9SAndrey Smirnov psci_method = "smc";
4574cbca7d9SAndrey Smirnov break;
4584cbca7d9SAndrey Smirnov default:
4594cbca7d9SAndrey Smirnov g_assert_not_reached();
4604cbca7d9SAndrey Smirnov }
4614cbca7d9SAndrey Smirnov
462c39770cdSAndrey Smirnov /*
463e4b0bb80SPeter Maydell * A pre-existing /psci node might specify function ID values
464e4b0bb80SPeter Maydell * that don't match QEMU's PSCI implementation. Delete the whole
465e4b0bb80SPeter Maydell * node and put our own in instead.
466c39770cdSAndrey Smirnov */
467c39770cdSAndrey Smirnov rc = fdt_path_offset(fdt, "/psci");
468c39770cdSAndrey Smirnov if (rc >= 0) {
469e4b0bb80SPeter Maydell qemu_fdt_nop_node(fdt, "/psci");
470c39770cdSAndrey Smirnov }
471c39770cdSAndrey Smirnov
4724cbca7d9SAndrey Smirnov qemu_fdt_add_subnode(fdt, "/psci");
473dc8bc9d6SPeter Maydell if (armcpu->psci_version >= QEMU_PSCI_VERSION_0_2) {
474dc8bc9d6SPeter Maydell if (armcpu->psci_version < QEMU_PSCI_VERSION_1_0) {
4754cbca7d9SAndrey Smirnov const char comp[] = "arm,psci-0.2\0arm,psci";
4764cbca7d9SAndrey Smirnov qemu_fdt_setprop(fdt, "/psci", "compatible", comp, sizeof(comp));
4770dc71c70SAkihiko Odaki } else {
4780dc71c70SAkihiko Odaki const char comp[] = "arm,psci-1.0\0arm,psci-0.2\0arm,psci";
4790dc71c70SAkihiko Odaki qemu_fdt_setprop(fdt, "/psci", "compatible", comp, sizeof(comp));
4800dc71c70SAkihiko Odaki }
4814cbca7d9SAndrey Smirnov
4824cbca7d9SAndrey Smirnov cpu_off_fn = QEMU_PSCI_0_2_FN_CPU_OFF;
4834cbca7d9SAndrey Smirnov if (arm_feature(&armcpu->env, ARM_FEATURE_AARCH64)) {
4844cbca7d9SAndrey Smirnov cpu_suspend_fn = QEMU_PSCI_0_2_FN64_CPU_SUSPEND;
4854cbca7d9SAndrey Smirnov cpu_on_fn = QEMU_PSCI_0_2_FN64_CPU_ON;
4864cbca7d9SAndrey Smirnov migrate_fn = QEMU_PSCI_0_2_FN64_MIGRATE;
4874cbca7d9SAndrey Smirnov } else {
4884cbca7d9SAndrey Smirnov cpu_suspend_fn = QEMU_PSCI_0_2_FN_CPU_SUSPEND;
4894cbca7d9SAndrey Smirnov cpu_on_fn = QEMU_PSCI_0_2_FN_CPU_ON;
4904cbca7d9SAndrey Smirnov migrate_fn = QEMU_PSCI_0_2_FN_MIGRATE;
4914cbca7d9SAndrey Smirnov }
4924cbca7d9SAndrey Smirnov } else {
4934cbca7d9SAndrey Smirnov qemu_fdt_setprop_string(fdt, "/psci", "compatible", "arm,psci");
4944cbca7d9SAndrey Smirnov
4954cbca7d9SAndrey Smirnov cpu_suspend_fn = QEMU_PSCI_0_1_FN_CPU_SUSPEND;
4964cbca7d9SAndrey Smirnov cpu_off_fn = QEMU_PSCI_0_1_FN_CPU_OFF;
4974cbca7d9SAndrey Smirnov cpu_on_fn = QEMU_PSCI_0_1_FN_CPU_ON;
4984cbca7d9SAndrey Smirnov migrate_fn = QEMU_PSCI_0_1_FN_MIGRATE;
4994cbca7d9SAndrey Smirnov }
5004cbca7d9SAndrey Smirnov
5014cbca7d9SAndrey Smirnov /* We adopt the PSCI spec's nomenclature, and use 'conduit' to refer
5024cbca7d9SAndrey Smirnov * to the instruction that should be used to invoke PSCI functions.
5034cbca7d9SAndrey Smirnov * However, the device tree binding uses 'method' instead, so that is
5044cbca7d9SAndrey Smirnov * what we should use here.
5054cbca7d9SAndrey Smirnov */
5064cbca7d9SAndrey Smirnov qemu_fdt_setprop_string(fdt, "/psci", "method", psci_method);
5074cbca7d9SAndrey Smirnov
5084cbca7d9SAndrey Smirnov qemu_fdt_setprop_cell(fdt, "/psci", "cpu_suspend", cpu_suspend_fn);
5094cbca7d9SAndrey Smirnov qemu_fdt_setprop_cell(fdt, "/psci", "cpu_off", cpu_off_fn);
5104cbca7d9SAndrey Smirnov qemu_fdt_setprop_cell(fdt, "/psci", "cpu_on", cpu_on_fn);
5114cbca7d9SAndrey Smirnov qemu_fdt_setprop_cell(fdt, "/psci", "migrate", migrate_fn);
5124cbca7d9SAndrey Smirnov }
5134cbca7d9SAndrey Smirnov
arm_load_dtb(hwaddr addr,const struct arm_boot_info * binfo,hwaddr addr_limit,AddressSpace * as,MachineState * ms)5143b77f6c3SIgor Mammedov int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
5152744ece8STao Xu hwaddr addr_limit, AddressSpace *as, MachineState *ms)
51653018216SPaolo Bonzini {
51753018216SPaolo Bonzini void *fdt = NULL;
518e2eb3d29SEric Auger int size, rc, n = 0;
51970976c41SPeter Maydell uint32_t acells, scells;
5209695200aSShannon Zhao unsigned int i;
5219695200aSShannon Zhao hwaddr mem_base, mem_len;
522e2eb3d29SEric Auger char **node_path;
523e2eb3d29SEric Auger Error *err = NULL;
52453018216SPaolo Bonzini
5250fb79851SJohn Rigby if (binfo->dtb_filename) {
5260fb79851SJohn Rigby char *filename;
52753018216SPaolo Bonzini filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename);
52853018216SPaolo Bonzini if (!filename) {
52953018216SPaolo Bonzini fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename);
530c23045deSPeter Maydell goto fail;
53153018216SPaolo Bonzini }
53253018216SPaolo Bonzini
53353018216SPaolo Bonzini fdt = load_device_tree(filename, &size);
53453018216SPaolo Bonzini if (!fdt) {
53553018216SPaolo Bonzini fprintf(stderr, "Couldn't open dtb file %s\n", filename);
53653018216SPaolo Bonzini g_free(filename);
537c23045deSPeter Maydell goto fail;
53853018216SPaolo Bonzini }
53953018216SPaolo Bonzini g_free(filename);
540a554ecb4Szhanghailiang } else {
5410fb79851SJohn Rigby fdt = binfo->get_dtb(binfo, &size);
5420fb79851SJohn Rigby if (!fdt) {
5430fb79851SJohn Rigby fprintf(stderr, "Board was unable to create a dtb blob\n");
5440fb79851SJohn Rigby goto fail;
5450fb79851SJohn Rigby }
5460fb79851SJohn Rigby }
54753018216SPaolo Bonzini
548fee8ea12SArd Biesheuvel if (addr_limit > addr && size > (addr_limit - addr)) {
549fee8ea12SArd Biesheuvel /* Installing the device tree blob at addr would exceed addr_limit.
550fee8ea12SArd Biesheuvel * Whether this constitutes failure is up to the caller to decide,
551fee8ea12SArd Biesheuvel * so just return 0 as size, i.e., no error.
552fee8ea12SArd Biesheuvel */
553fee8ea12SArd Biesheuvel g_free(fdt);
554fee8ea12SArd Biesheuvel return 0;
555fee8ea12SArd Biesheuvel }
556fee8ea12SArd Biesheuvel
55758e71097SEric Auger acells = qemu_fdt_getprop_cell(fdt, "/", "#address-cells",
55858e71097SEric Auger NULL, &error_fatal);
55958e71097SEric Auger scells = qemu_fdt_getprop_cell(fdt, "/", "#size-cells",
56058e71097SEric Auger NULL, &error_fatal);
56153018216SPaolo Bonzini if (acells == 0 || scells == 0) {
56253018216SPaolo Bonzini fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n");
563c23045deSPeter Maydell goto fail;
56453018216SPaolo Bonzini }
56553018216SPaolo Bonzini
566e4e34855SPhilippe Mathieu-Daudé if (scells < 2 && binfo->ram_size >= 4 * GiB) {
56770976c41SPeter Maydell /* This is user error so deserves a friendlier error message
56870976c41SPeter Maydell * than the failure of setprop_sized_cells would provide
56970976c41SPeter Maydell */
57053018216SPaolo Bonzini fprintf(stderr, "qemu: dtb file not compatible with "
57153018216SPaolo Bonzini "RAM size > 4GB\n");
572c23045deSPeter Maydell goto fail;
57353018216SPaolo Bonzini }
57453018216SPaolo Bonzini
575e2eb3d29SEric Auger /* nop all root nodes matching /memory or /memory@unit-address */
576e2eb3d29SEric Auger node_path = qemu_fdt_node_unit_path(fdt, "memory", &err);
577e2eb3d29SEric Auger if (err) {
578e2eb3d29SEric Auger error_report_err(err);
579e2eb3d29SEric Auger goto fail;
580e2eb3d29SEric Auger }
581e2eb3d29SEric Auger while (node_path[n]) {
582e2eb3d29SEric Auger if (g_str_has_prefix(node_path[n], "/memory")) {
583e2eb3d29SEric Auger qemu_fdt_nop_node(fdt, node_path[n]);
584e2eb3d29SEric Auger }
585e2eb3d29SEric Auger n++;
586e2eb3d29SEric Auger }
587e2eb3d29SEric Auger g_strfreev(node_path);
588e2eb3d29SEric Auger
58999abb725SGavin Shan /*
59099abb725SGavin Shan * We drop all the memory nodes which correspond to empty NUMA nodes
59199abb725SGavin Shan * from the device tree, because the Linux NUMA binding document
59299abb725SGavin Shan * states they should not be generated. Linux will get the NUMA node
59399abb725SGavin Shan * IDs of the empty NUMA nodes from the distance map if they are needed.
59499abb725SGavin Shan * This means QEMU users may be obliged to provide command lines which
59599abb725SGavin Shan * configure distance maps when the empty NUMA node IDs are needed and
59699abb725SGavin Shan * Linux's default distance map isn't sufficient.
59799abb725SGavin Shan */
598aa570207STao Xu if (ms->numa_state != NULL && ms->numa_state->num_nodes > 0) {
5999695200aSShannon Zhao mem_base = binfo->loader_start;
600aa570207STao Xu for (i = 0; i < ms->numa_state->num_nodes; i++) {
6017e721e7bSTao Xu mem_len = ms->numa_state->nodes[i].node_mem;
60299abb725SGavin Shan if (!mem_len) {
60399abb725SGavin Shan continue;
60499abb725SGavin Shan }
60599abb725SGavin Shan
606f08ced69SShameer Kolothum rc = fdt_add_memory_node(fdt, acells, mem_base,
607f08ced69SShameer Kolothum scells, mem_len, i);
6089695200aSShannon Zhao if (rc < 0) {
609f08ced69SShameer Kolothum fprintf(stderr, "couldn't add /memory@%"PRIx64" node\n",
610f08ced69SShameer Kolothum mem_base);
6119695200aSShannon Zhao goto fail;
6129695200aSShannon Zhao }
6139695200aSShannon Zhao
6149695200aSShannon Zhao mem_base += mem_len;
6159695200aSShannon Zhao }
6169695200aSShannon Zhao } else {
617f08ced69SShameer Kolothum rc = fdt_add_memory_node(fdt, acells, binfo->loader_start,
618f08ced69SShameer Kolothum scells, binfo->ram_size, -1);
61953018216SPaolo Bonzini if (rc < 0) {
620f08ced69SShameer Kolothum fprintf(stderr, "couldn't add /memory@%"PRIx64" node\n",
621f08ced69SShameer Kolothum binfo->loader_start);
622c23045deSPeter Maydell goto fail;
62353018216SPaolo Bonzini }
6249695200aSShannon Zhao }
62553018216SPaolo Bonzini
626b77257d7SGuenter Roeck rc = fdt_path_offset(fdt, "/chosen");
627b77257d7SGuenter Roeck if (rc < 0) {
628b77257d7SGuenter Roeck qemu_fdt_add_subnode(fdt, "/chosen");
629b77257d7SGuenter Roeck }
630b77257d7SGuenter Roeck
6312744ece8STao Xu if (ms->kernel_cmdline && *ms->kernel_cmdline) {
6325a4348d1SPeter Crosthwaite rc = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
6332744ece8STao Xu ms->kernel_cmdline);
63453018216SPaolo Bonzini if (rc < 0) {
63553018216SPaolo Bonzini fprintf(stderr, "couldn't set /chosen/bootargs\n");
636c23045deSPeter Maydell goto fail;
63753018216SPaolo Bonzini }
63853018216SPaolo Bonzini }
63953018216SPaolo Bonzini
64053018216SPaolo Bonzini if (binfo->initrd_size) {
641990f49cfSSchspa Shi rc = qemu_fdt_setprop_sized_cells(fdt, "/chosen", "linux,initrd-start",
642990f49cfSSchspa Shi acells, binfo->initrd_start);
64353018216SPaolo Bonzini if (rc < 0) {
64453018216SPaolo Bonzini fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
645c23045deSPeter Maydell goto fail;
64653018216SPaolo Bonzini }
64753018216SPaolo Bonzini
648990f49cfSSchspa Shi rc = qemu_fdt_setprop_sized_cells(fdt, "/chosen", "linux,initrd-end",
649990f49cfSSchspa Shi acells,
650990f49cfSSchspa Shi binfo->initrd_start +
651990f49cfSSchspa Shi binfo->initrd_size);
65253018216SPaolo Bonzini if (rc < 0) {
65353018216SPaolo Bonzini fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
654c23045deSPeter Maydell goto fail;
65553018216SPaolo Bonzini }
65653018216SPaolo Bonzini }
6573b1cceb8SPeter Maydell
6584cbca7d9SAndrey Smirnov fdt_add_psci_node(fdt);
6594cbca7d9SAndrey Smirnov
6603b1cceb8SPeter Maydell if (binfo->modify_dtb) {
6613b1cceb8SPeter Maydell binfo->modify_dtb(binfo, fdt);
6623b1cceb8SPeter Maydell }
6633b1cceb8SPeter Maydell
6645a4348d1SPeter Crosthwaite qemu_fdt_dumpdtb(fdt, size);
66553018216SPaolo Bonzini
6664c4bf654SArd Biesheuvel /* Put the DTB into the memory map as a ROM image: this will ensure
6674c4bf654SArd Biesheuvel * the DTB is copied again upon reset, even if addr points into RAM.
6684c4bf654SArd Biesheuvel */
6699f43d4c3SPeter Maydell rom_add_blob_fixed_as("dtb", fdt, size, addr, as);
67098aa4c83SJason A. Donenfeld qemu_register_reset_nosnapshotload(qemu_fdt_randomize_seeds,
67198aa4c83SJason A. Donenfeld rom_ptr_for_as(as, addr, size));
67253018216SPaolo Bonzini
67312148d44SMarkus Armbruster if (fdt != ms->fdt) {
67412148d44SMarkus Armbruster g_free(ms->fdt);
67512148d44SMarkus Armbruster ms->fdt = fdt;
67612148d44SMarkus Armbruster }
677c23045deSPeter Maydell
678fee8ea12SArd Biesheuvel return size;
679c23045deSPeter Maydell
680c23045deSPeter Maydell fail:
681c23045deSPeter Maydell g_free(fdt);
682c23045deSPeter Maydell return -1;
68353018216SPaolo Bonzini }
68453018216SPaolo Bonzini
do_cpu_reset(void * opaque)68553018216SPaolo Bonzini static void do_cpu_reset(void *opaque)
68653018216SPaolo Bonzini {
68753018216SPaolo Bonzini ARMCPU *cpu = opaque;
6884df81c6eSPeter Crosthwaite CPUState *cs = CPU(cpu);
68953018216SPaolo Bonzini CPUARMState *env = &cpu->env;
69053018216SPaolo Bonzini const struct arm_boot_info *info = env->boot_info;
69153018216SPaolo Bonzini
6924df81c6eSPeter Crosthwaite cpu_reset(cs);
69353018216SPaolo Bonzini if (info) {
69453018216SPaolo Bonzini if (!info->is_linux) {
6959776f636SPeter Crosthwaite int i;
69653018216SPaolo Bonzini /* Jump to the entry point. */
6974df81c6eSPeter Crosthwaite uint64_t entry = info->entry;
6984df81c6eSPeter Crosthwaite
6999776f636SPeter Crosthwaite switch (info->endianness) {
7009776f636SPeter Crosthwaite case ARM_ENDIANNESS_LE:
7019776f636SPeter Crosthwaite env->cp15.sctlr_el[1] &= ~SCTLR_E0E;
7029776f636SPeter Crosthwaite for (i = 1; i < 4; ++i) {
7039776f636SPeter Crosthwaite env->cp15.sctlr_el[i] &= ~SCTLR_EE;
7049776f636SPeter Crosthwaite }
7059776f636SPeter Crosthwaite env->uncached_cpsr &= ~CPSR_E;
7069776f636SPeter Crosthwaite break;
7079776f636SPeter Crosthwaite case ARM_ENDIANNESS_BE8:
7089776f636SPeter Crosthwaite env->cp15.sctlr_el[1] |= SCTLR_E0E;
7099776f636SPeter Crosthwaite for (i = 1; i < 4; ++i) {
7109776f636SPeter Crosthwaite env->cp15.sctlr_el[i] |= SCTLR_EE;
7119776f636SPeter Crosthwaite }
7129776f636SPeter Crosthwaite env->uncached_cpsr |= CPSR_E;
7139776f636SPeter Crosthwaite break;
7149776f636SPeter Crosthwaite case ARM_ENDIANNESS_BE32:
7159776f636SPeter Crosthwaite env->cp15.sctlr_el[1] |= SCTLR_B;
7169776f636SPeter Crosthwaite break;
7179776f636SPeter Crosthwaite case ARM_ENDIANNESS_UNKNOWN:
7189776f636SPeter Crosthwaite break; /* Board's decision */
7199776f636SPeter Crosthwaite default:
7209776f636SPeter Crosthwaite g_assert_not_reached();
7219776f636SPeter Crosthwaite }
7229776f636SPeter Crosthwaite
7234df81c6eSPeter Crosthwaite cpu_set_pc(cs, entry);
72453018216SPaolo Bonzini } else {
725299953b9SPeter Maydell /*
7263a45f4f5SPeter Maydell * If we are booting Linux then we might need to do so at:
7273a45f4f5SPeter Maydell * - AArch64 NS EL2 or NS EL1
7283a45f4f5SPeter Maydell * - AArch32 Secure SVC (EL3)
7293a45f4f5SPeter Maydell * - AArch32 NS Hyp (EL2)
7303a45f4f5SPeter Maydell * - AArch32 NS SVC (EL1)
7313a45f4f5SPeter Maydell * Configure the CPU in the way boot firmware would do to
7323a45f4f5SPeter Maydell * drop us down to the appropriate level.
733299953b9SPeter Maydell */
7343a45f4f5SPeter Maydell int target_el = arm_feature(env, ARM_FEATURE_EL2) ? 2 : 1;
7353a45f4f5SPeter Maydell
7363a45f4f5SPeter Maydell if (env->aarch64) {
7373a45f4f5SPeter Maydell /*
7383a45f4f5SPeter Maydell * AArch64 kernels never boot in secure mode, and we don't
7393a45f4f5SPeter Maydell * support the secure_board_setup hook for AArch64.
7403a45f4f5SPeter Maydell */
7413a45f4f5SPeter Maydell assert(!info->secure_boot);
7423a45f4f5SPeter Maydell assert(!info->secure_board_setup);
7433a45f4f5SPeter Maydell } else {
7443a45f4f5SPeter Maydell if (arm_feature(env, ARM_FEATURE_EL3) &&
7453a45f4f5SPeter Maydell (info->secure_boot ||
7463a45f4f5SPeter Maydell (info->secure_board_setup && cs == first_cpu))) {
7473a45f4f5SPeter Maydell /* Start this CPU in Secure SVC */
7483a45f4f5SPeter Maydell target_el = 3;
749299953b9SPeter Maydell }
7503a45f4f5SPeter Maydell }
7513a45f4f5SPeter Maydell
7523a45f4f5SPeter Maydell arm_emulate_firmware_reset(cs, target_el);
753299953b9SPeter Maydell
7544df81c6eSPeter Crosthwaite if (cs == first_cpu) {
7559f43d4c3SPeter Maydell AddressSpace *as = arm_boot_address_space(cpu, info);
7569f43d4c3SPeter Maydell
7574df81c6eSPeter Crosthwaite cpu_set_pc(cs, info->loader_start);
7584d9ebf75SMian M. Hamayun
75983bfffecSPeter Maydell if (!have_dtb(info)) {
76053018216SPaolo Bonzini if (old_param) {
7619f43d4c3SPeter Maydell set_kernel_args_old(info, as);
76253018216SPaolo Bonzini } else {
7639f43d4c3SPeter Maydell set_kernel_args(info, as);
76453018216SPaolo Bonzini }
76553018216SPaolo Bonzini }
766d4a29ed6SPeter Maydell } else if (info->secondary_cpu_reset_hook) {
76753018216SPaolo Bonzini info->secondary_cpu_reset_hook(cpu, info);
76853018216SPaolo Bonzini }
76953018216SPaolo Bonzini }
7702b77ad4dSFabiano Rosas
7712b77ad4dSFabiano Rosas if (tcg_enabled()) {
77298be6b7dSEdgar E. Iglesias arm_rebuild_hflags(env);
77353018216SPaolo Bonzini }
77453018216SPaolo Bonzini }
7752b77ad4dSFabiano Rosas }
77653018216SPaolo Bonzini
do_arm_linux_init(Object * obj,void * opaque)777d8b1ae42SPeter Maydell static int do_arm_linux_init(Object *obj, void *opaque)
778d8b1ae42SPeter Maydell {
779d8b1ae42SPeter Maydell if (object_dynamic_cast(obj, TYPE_ARM_LINUX_BOOT_IF)) {
780d8b1ae42SPeter Maydell ARMLinuxBootIf *albif = ARM_LINUX_BOOT_IF(obj);
781d8b1ae42SPeter Maydell ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_GET_CLASS(obj);
782d8b1ae42SPeter Maydell struct arm_boot_info *info = opaque;
783d8b1ae42SPeter Maydell
784d8b1ae42SPeter Maydell if (albifc->arm_linux_init) {
785d8b1ae42SPeter Maydell albifc->arm_linux_init(albif, info->secure_boot);
786d8b1ae42SPeter Maydell }
787d8b1ae42SPeter Maydell }
788d8b1ae42SPeter Maydell return 0;
789d8b1ae42SPeter Maydell }
790d8b1ae42SPeter Maydell
arm_load_elf(struct arm_boot_info * info,uint64_t * pentry,uint64_t * lowaddr,uint64_t * highaddr,int elf_machine,AddressSpace * as)791af975131SJamie Iles static ssize_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry,
7929776f636SPeter Crosthwaite uint64_t *lowaddr, uint64_t *highaddr,
7939f43d4c3SPeter Maydell int elf_machine, AddressSpace *as)
7949776f636SPeter Crosthwaite {
7959776f636SPeter Crosthwaite bool elf_is64;
7969776f636SPeter Crosthwaite union {
7979776f636SPeter Crosthwaite Elf32_Ehdr h32;
7989776f636SPeter Crosthwaite Elf64_Ehdr h64;
7999776f636SPeter Crosthwaite } elf_header;
8009776f636SPeter Crosthwaite int data_swab = 0;
8019776f636SPeter Crosthwaite bool big_endian;
802*17e93dd5SPhilippe Mathieu-Daudé ssize_t ret;
8039776f636SPeter Crosthwaite Error *err = NULL;
8049776f636SPeter Crosthwaite
8059776f636SPeter Crosthwaite
8069776f636SPeter Crosthwaite load_elf_hdr(info->kernel_filename, &elf_header, &elf_is64, &err);
8079776f636SPeter Crosthwaite if (err) {
808*17e93dd5SPhilippe Mathieu-Daudé /*
809*17e93dd5SPhilippe Mathieu-Daudé * If the file is not an ELF file we silently return.
810*17e93dd5SPhilippe Mathieu-Daudé * The caller will fall back to try other formats.
811*17e93dd5SPhilippe Mathieu-Daudé */
81236f876ceSMarc-André Lureau error_free(err);
813*17e93dd5SPhilippe Mathieu-Daudé return -1;
8149776f636SPeter Crosthwaite }
8159776f636SPeter Crosthwaite
8169776f636SPeter Crosthwaite if (elf_is64) {
8179776f636SPeter Crosthwaite big_endian = elf_header.h64.e_ident[EI_DATA] == ELFDATA2MSB;
8189776f636SPeter Crosthwaite info->endianness = big_endian ? ARM_ENDIANNESS_BE8
8199776f636SPeter Crosthwaite : ARM_ENDIANNESS_LE;
8209776f636SPeter Crosthwaite } else {
8219776f636SPeter Crosthwaite big_endian = elf_header.h32.e_ident[EI_DATA] == ELFDATA2MSB;
8229776f636SPeter Crosthwaite if (big_endian) {
8239776f636SPeter Crosthwaite if (bswap32(elf_header.h32.e_flags) & EF_ARM_BE8) {
8249776f636SPeter Crosthwaite info->endianness = ARM_ENDIANNESS_BE8;
8259776f636SPeter Crosthwaite } else {
8269776f636SPeter Crosthwaite info->endianness = ARM_ENDIANNESS_BE32;
8279776f636SPeter Crosthwaite /* In BE32, the CPU has a different view of the per-byte
8289776f636SPeter Crosthwaite * address map than the rest of the system. BE32 ELF files
8299776f636SPeter Crosthwaite * are organised such that they can be programmed through
8309776f636SPeter Crosthwaite * the CPU's per-word byte-reversed view of the world. QEMU
8319776f636SPeter Crosthwaite * however loads ELF files independently of the CPU. So
8329776f636SPeter Crosthwaite * tell the ELF loader to byte reverse the data for us.
8339776f636SPeter Crosthwaite */
8349776f636SPeter Crosthwaite data_swab = 2;
8359776f636SPeter Crosthwaite }
8369776f636SPeter Crosthwaite } else {
8379776f636SPeter Crosthwaite info->endianness = ARM_ENDIANNESS_LE;
8389776f636SPeter Crosthwaite }
8399776f636SPeter Crosthwaite }
8409776f636SPeter Crosthwaite
8414366e1dbSLiam Merwick ret = load_elf_as(info->kernel_filename, NULL, NULL, NULL,
8426cdda0ffSAleksandar Markovic pentry, lowaddr, highaddr, NULL, big_endian, elf_machine,
8439f43d4c3SPeter Maydell 1, data_swab, as);
8449776f636SPeter Crosthwaite if (ret <= 0) {
8459776f636SPeter Crosthwaite /* The header loaded but the image didn't */
8462b490f15SChangbin Du error_report("Couldn't load elf '%s': %s",
8472b490f15SChangbin Du info->kernel_filename, load_elf_strerror(ret));
8489776f636SPeter Crosthwaite exit(1);
8499776f636SPeter Crosthwaite }
8509776f636SPeter Crosthwaite
8519776f636SPeter Crosthwaite return ret;
8529776f636SPeter Crosthwaite }
8539776f636SPeter Crosthwaite
load_aarch64_image(const char * filename,hwaddr mem_base,hwaddr * entry,AddressSpace * as)85468115ed5SArd Biesheuvel static uint64_t load_aarch64_image(const char *filename, hwaddr mem_base,
8559f43d4c3SPeter Maydell hwaddr *entry, AddressSpace *as)
85668115ed5SArd Biesheuvel {
85768115ed5SArd Biesheuvel hwaddr kernel_load_offset = KERNEL64_LOAD_ADDR;
8585e6dbe1eSPeter Maydell uint64_t kernel_size = 0;
85968115ed5SArd Biesheuvel uint8_t *buffer;
86068115ed5SArd Biesheuvel int size;
86168115ed5SArd Biesheuvel
86268115ed5SArd Biesheuvel /* On aarch64, it's the bootloader's job to uncompress the kernel. */
86368115ed5SArd Biesheuvel size = load_image_gzipped_buffer(filename, LOAD_IMAGE_MAX_GUNZIP_BYTES,
86468115ed5SArd Biesheuvel &buffer);
86568115ed5SArd Biesheuvel
86668115ed5SArd Biesheuvel if (size < 0) {
86768115ed5SArd Biesheuvel gsize len;
86868115ed5SArd Biesheuvel
86968115ed5SArd Biesheuvel /* Load as raw file otherwise */
87068115ed5SArd Biesheuvel if (!g_file_get_contents(filename, (char **)&buffer, &len, NULL)) {
87168115ed5SArd Biesheuvel return -1;
87268115ed5SArd Biesheuvel }
87368115ed5SArd Biesheuvel size = len;
874ff114228SArd Biesheuvel
875ff114228SArd Biesheuvel /* Unpack the image if it is a EFI zboot image */
876ff114228SArd Biesheuvel if (unpack_efi_zboot_image(&buffer, &size) < 0) {
877ff114228SArd Biesheuvel g_free(buffer);
878ff114228SArd Biesheuvel return -1;
879ff114228SArd Biesheuvel }
88068115ed5SArd Biesheuvel }
88168115ed5SArd Biesheuvel
88268115ed5SArd Biesheuvel /* check the arm64 magic header value -- very old kernels may not have it */
88327640407SMarc-André Lureau if (size > ARM64_MAGIC_OFFSET + 4 &&
88427640407SMarc-André Lureau memcmp(buffer + ARM64_MAGIC_OFFSET, "ARM\x64", 4) == 0) {
88568115ed5SArd Biesheuvel uint64_t hdrvals[2];
88668115ed5SArd Biesheuvel
88768115ed5SArd Biesheuvel /* The arm64 Image header has text_offset and image_size fields at 8 and
88868115ed5SArd Biesheuvel * 16 bytes into the Image header, respectively. The text_offset field
88968115ed5SArd Biesheuvel * is only valid if the image_size is non-zero.
89068115ed5SArd Biesheuvel */
89168115ed5SArd Biesheuvel memcpy(&hdrvals, buffer + ARM64_TEXT_OFFSET_OFFSET, sizeof(hdrvals));
8925e6dbe1eSPeter Maydell
8935e6dbe1eSPeter Maydell kernel_size = le64_to_cpu(hdrvals[1]);
8945e6dbe1eSPeter Maydell
8955e6dbe1eSPeter Maydell if (kernel_size != 0) {
89668115ed5SArd Biesheuvel kernel_load_offset = le64_to_cpu(hdrvals[0]);
897ea358872SStewart Hildebrand
898ea358872SStewart Hildebrand /*
899ea358872SStewart Hildebrand * We write our startup "bootloader" at the very bottom of RAM,
900ea358872SStewart Hildebrand * so that bit can't be used for the image. Luckily the Image
901ea358872SStewart Hildebrand * format specification is that the image requests only an offset
902ea358872SStewart Hildebrand * from a 2MB boundary, not an absolute load address. So if the
903ea358872SStewart Hildebrand * image requests an offset that might mean it overlaps with the
904ea358872SStewart Hildebrand * bootloader, we can just load it starting at 2MB+offset rather
905ea358872SStewart Hildebrand * than 0MB + offset.
906ea358872SStewart Hildebrand */
907ea358872SStewart Hildebrand if (kernel_load_offset < BOOTLOADER_MAX_SIZE) {
908ea358872SStewart Hildebrand kernel_load_offset += 2 * MiB;
909ea358872SStewart Hildebrand }
91068115ed5SArd Biesheuvel }
91168115ed5SArd Biesheuvel }
91268115ed5SArd Biesheuvel
9135e6dbe1eSPeter Maydell /*
9145e6dbe1eSPeter Maydell * Kernels before v3.17 don't populate the image_size field, and
9155e6dbe1eSPeter Maydell * raw images have no header. For those our best guess at the size
9165e6dbe1eSPeter Maydell * is the size of the Image file itself.
9175e6dbe1eSPeter Maydell */
9185e6dbe1eSPeter Maydell if (kernel_size == 0) {
9195e6dbe1eSPeter Maydell kernel_size = size;
9205e6dbe1eSPeter Maydell }
9215e6dbe1eSPeter Maydell
92268115ed5SArd Biesheuvel *entry = mem_base + kernel_load_offset;
9239f43d4c3SPeter Maydell rom_add_blob_fixed_as(filename, buffer, size, *entry, as);
92468115ed5SArd Biesheuvel
92568115ed5SArd Biesheuvel g_free(buffer);
92668115ed5SArd Biesheuvel
9275e6dbe1eSPeter Maydell return kernel_size;
92868115ed5SArd Biesheuvel }
92968115ed5SArd Biesheuvel
arm_setup_direct_kernel_boot(ARMCPU * cpu,struct arm_boot_info * info)930d33774eeSPeter Maydell static void arm_setup_direct_kernel_boot(ARMCPU *cpu,
931d33774eeSPeter Maydell struct arm_boot_info *info)
93253018216SPaolo Bonzini {
933d33774eeSPeter Maydell /* Set up for a direct boot of a kernel image file. */
934c6faa758SArd Biesheuvel CPUState *cs;
935d33774eeSPeter Maydell AddressSpace *as = arm_boot_address_space(cpu, info);
936af975131SJamie Iles ssize_t kernel_size;
93753018216SPaolo Bonzini int initrd_size;
93853018216SPaolo Bonzini int is_linux = 0;
939d5fef92fSPeter Maydell uint64_t elf_entry;
940d5fef92fSPeter Maydell /* Addresses of first byte used and first byte not used by the image */
94167505c11SPeter Maydell uint64_t image_low_addr = 0, image_high_addr = 0;
942da0af40dSPeter Maydell int elf_machine;
94368115ed5SArd Biesheuvel hwaddr entry;
9444d9ebf75SMian M. Hamayun static const ARMInsnFixup *primary_loader;
945e70af24bSPeter Maydell uint64_t ram_end = info->loader_start + info->ram_size;
94653018216SPaolo Bonzini
9474d9ebf75SMian M. Hamayun if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
9484d9ebf75SMian M. Hamayun primary_loader = bootloader_aarch64;
949da0af40dSPeter Maydell elf_machine = EM_AARCH64;
9504d9ebf75SMian M. Hamayun } else {
9514d9ebf75SMian M. Hamayun primary_loader = bootloader;
95210b8ec73SPeter Crosthwaite if (!info->write_board_setup) {
95310b8ec73SPeter Crosthwaite primary_loader += BOOTLOADER_NO_BOARD_SETUP_OFFSET;
95410b8ec73SPeter Crosthwaite }
955da0af40dSPeter Maydell elf_machine = EM_ARM;
9564d9ebf75SMian M. Hamayun }
9574d9ebf75SMian M. Hamayun
95853018216SPaolo Bonzini /* Assume that raw images are linux kernels, and ELF images are not. */
959d5fef92fSPeter Maydell kernel_size = arm_load_elf(info, &elf_entry, &image_low_addr,
960d5fef92fSPeter Maydell &image_high_addr, elf_machine, as);
96192df8450SArd Biesheuvel if (kernel_size > 0 && have_dtb(info)) {
962c3a42358SPeter Maydell /*
963c3a42358SPeter Maydell * If there is still some room left at the base of RAM, try and put
96492df8450SArd Biesheuvel * the DTB there like we do for images loaded with -bios or -pflash.
96592df8450SArd Biesheuvel */
966d5fef92fSPeter Maydell if (image_low_addr > info->loader_start
967d5fef92fSPeter Maydell || image_high_addr < info->loader_start) {
968c3a42358SPeter Maydell /*
969d5fef92fSPeter Maydell * Set image_low_addr as address limit for arm_load_dtb if it may be
97092df8450SArd Biesheuvel * pointing into RAM, otherwise pass '0' (no limit)
97192df8450SArd Biesheuvel */
972d5fef92fSPeter Maydell if (image_low_addr < info->loader_start) {
973d5fef92fSPeter Maydell image_low_addr = 0;
97492df8450SArd Biesheuvel }
9753b77f6c3SIgor Mammedov info->dtb_start = info->loader_start;
976d5fef92fSPeter Maydell info->dtb_limit = image_low_addr;
97792df8450SArd Biesheuvel }
97892df8450SArd Biesheuvel }
97953018216SPaolo Bonzini entry = elf_entry;
98053018216SPaolo Bonzini if (kernel_size < 0) {
981f831f955SNick Hudson uint64_t loadaddr = info->loader_start + KERNEL_NOLOAD_ADDR;
982f831f955SNick Hudson kernel_size = load_uimage_as(info->kernel_filename, &entry, &loadaddr,
9839f43d4c3SPeter Maydell &is_linux, NULL, NULL, as);
98467505c11SPeter Maydell if (kernel_size >= 0) {
98567505c11SPeter Maydell image_low_addr = loadaddr;
98667505c11SPeter Maydell image_high_addr = image_low_addr + kernel_size;
98767505c11SPeter Maydell }
98853018216SPaolo Bonzini }
9896f5d3cbeSRichard W.M. Jones if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && kernel_size < 0) {
99068115ed5SArd Biesheuvel kernel_size = load_aarch64_image(info->kernel_filename,
9919f43d4c3SPeter Maydell info->loader_start, &entry, as);
9926f5d3cbeSRichard W.M. Jones is_linux = 1;
99367505c11SPeter Maydell if (kernel_size >= 0) {
99467505c11SPeter Maydell image_low_addr = entry;
99567505c11SPeter Maydell image_high_addr = image_low_addr + kernel_size;
99667505c11SPeter Maydell }
99768115ed5SArd Biesheuvel } else if (kernel_size < 0) {
99868115ed5SArd Biesheuvel /* 32-bit ARM */
99968115ed5SArd Biesheuvel entry = info->loader_start + KERNEL_LOAD_ADDR;
10009f43d4c3SPeter Maydell kernel_size = load_image_targphys_as(info->kernel_filename, entry,
1001e70af24bSPeter Maydell ram_end - KERNEL_LOAD_ADDR, as);
100253018216SPaolo Bonzini is_linux = 1;
100367505c11SPeter Maydell if (kernel_size >= 0) {
100467505c11SPeter Maydell image_low_addr = entry;
100567505c11SPeter Maydell image_high_addr = image_low_addr + kernel_size;
100667505c11SPeter Maydell }
100753018216SPaolo Bonzini }
100853018216SPaolo Bonzini if (kernel_size < 0) {
1009c0dbca36SAlistair Francis error_report("could not load kernel '%s'", info->kernel_filename);
101053018216SPaolo Bonzini exit(1);
101153018216SPaolo Bonzini }
1012852dc64dSPeter Maydell
1013852dc64dSPeter Maydell if (kernel_size > info->ram_size) {
1014852dc64dSPeter Maydell error_report("kernel '%s' is too large to fit in RAM "
1015af975131SJamie Iles "(kernel size %zd, RAM size %" PRId64 ")",
1016852dc64dSPeter Maydell info->kernel_filename, kernel_size, info->ram_size);
1017852dc64dSPeter Maydell exit(1);
1018852dc64dSPeter Maydell }
1019852dc64dSPeter Maydell
102053018216SPaolo Bonzini info->entry = entry;
1021e6b2b20dSPeter Maydell
1022e6b2b20dSPeter Maydell /*
1023e6b2b20dSPeter Maydell * We want to put the initrd far enough into RAM that when the
1024e6b2b20dSPeter Maydell * kernel is uncompressed it will not clobber the initrd. However
1025e6b2b20dSPeter Maydell * on boards without much RAM we must ensure that we still leave
1026e6b2b20dSPeter Maydell * enough room for a decent sized initrd, and on boards with large
1027e6b2b20dSPeter Maydell * amounts of RAM we must avoid the initrd being so far up in RAM
1028e6b2b20dSPeter Maydell * that it is outside lowmem and inaccessible to the kernel.
1029e6b2b20dSPeter Maydell * So for boards with less than 256MB of RAM we put the initrd
1030e6b2b20dSPeter Maydell * halfway into RAM, and for boards with 256MB of RAM or more we put
1031e6b2b20dSPeter Maydell * the initrd at 128MB.
1032e6b2b20dSPeter Maydell * We also refuse to put the initrd somewhere that will definitely
1033e6b2b20dSPeter Maydell * overlay the kernel we just loaded, though for kernel formats which
1034e6b2b20dSPeter Maydell * don't tell us their exact size (eg self-decompressing 32-bit kernels)
1035e6b2b20dSPeter Maydell * we might still make a bad choice here.
1036e6b2b20dSPeter Maydell */
1037e6b2b20dSPeter Maydell info->initrd_start = info->loader_start +
1038e4e34855SPhilippe Mathieu-Daudé MIN(info->ram_size / 2, 128 * MiB);
103967505c11SPeter Maydell if (image_high_addr) {
104067505c11SPeter Maydell info->initrd_start = MAX(info->initrd_start, image_high_addr);
104167505c11SPeter Maydell }
1042e6b2b20dSPeter Maydell info->initrd_start = TARGET_PAGE_ALIGN(info->initrd_start);
1043e6b2b20dSPeter Maydell
104453018216SPaolo Bonzini if (is_linux) {
104547b1da81SPeter Maydell uint32_t fixupcontext[FIXUP_MAX];
104647b1da81SPeter Maydell
104753018216SPaolo Bonzini if (info->initrd_filename) {
1048852dc64dSPeter Maydell
1049852dc64dSPeter Maydell if (info->initrd_start >= ram_end) {
1050852dc64dSPeter Maydell error_report("not enough space after kernel to load initrd");
1051852dc64dSPeter Maydell exit(1);
1052852dc64dSPeter Maydell }
1053852dc64dSPeter Maydell
10549f43d4c3SPeter Maydell initrd_size = load_ramdisk_as(info->initrd_filename,
1055fd76663eSSoren Brinkmann info->initrd_start,
1056e70af24bSPeter Maydell ram_end - info->initrd_start, as);
1057fd76663eSSoren Brinkmann if (initrd_size < 0) {
10589f43d4c3SPeter Maydell initrd_size = load_image_targphys_as(info->initrd_filename,
105953018216SPaolo Bonzini info->initrd_start,
1060e70af24bSPeter Maydell ram_end -
10619f43d4c3SPeter Maydell info->initrd_start,
10629f43d4c3SPeter Maydell as);
1063fd76663eSSoren Brinkmann }
106453018216SPaolo Bonzini if (initrd_size < 0) {
1065c0dbca36SAlistair Francis error_report("could not load initrd '%s'",
106653018216SPaolo Bonzini info->initrd_filename);
106753018216SPaolo Bonzini exit(1);
106853018216SPaolo Bonzini }
1069b48b0640SAndrew Jones if (info->initrd_start + initrd_size > ram_end) {
1070852dc64dSPeter Maydell error_report("could not load initrd '%s': "
1071852dc64dSPeter Maydell "too big to fit into RAM after the kernel",
1072852dc64dSPeter Maydell info->initrd_filename);
1073b48b0640SAndrew Jones exit(1);
1074852dc64dSPeter Maydell }
107553018216SPaolo Bonzini } else {
107653018216SPaolo Bonzini initrd_size = 0;
107753018216SPaolo Bonzini }
107853018216SPaolo Bonzini info->initrd_size = initrd_size;
107953018216SPaolo Bonzini
108047b1da81SPeter Maydell fixupcontext[FIXUP_BOARDID] = info->board_id;
108110b8ec73SPeter Crosthwaite fixupcontext[FIXUP_BOARD_SETUP] = info->board_setup_addr;
108253018216SPaolo Bonzini
1083c3a42358SPeter Maydell /*
1084c3a42358SPeter Maydell * for device tree boot, we pass the DTB directly in r2. Otherwise
108553018216SPaolo Bonzini * we point to the kernel args.
108653018216SPaolo Bonzini */
108783bfffecSPeter Maydell if (have_dtb(info)) {
108876e2aef3SAlexander Graf hwaddr align;
108976e2aef3SAlexander Graf
109076e2aef3SAlexander Graf if (elf_machine == EM_AARCH64) {
109176e2aef3SAlexander Graf /*
109276e2aef3SAlexander Graf * Some AArch64 kernels on early bootup map the fdt region as
109376e2aef3SAlexander Graf *
109476e2aef3SAlexander Graf * [ ALIGN_DOWN(fdt, 2MB) ... ALIGN_DOWN(fdt, 2MB) + 2MB ]
109576e2aef3SAlexander Graf *
109676e2aef3SAlexander Graf * Let's play safe and prealign it to 2MB to give us some space.
109753018216SPaolo Bonzini */
1098e4e34855SPhilippe Mathieu-Daudé align = 2 * MiB;
109976e2aef3SAlexander Graf } else {
110076e2aef3SAlexander Graf /*
110176e2aef3SAlexander Graf * Some 32bit kernels will trash anything in the 4K page the
110276e2aef3SAlexander Graf * initrd ends in, so make sure the DTB isn't caught up in that.
110376e2aef3SAlexander Graf */
1104e4e34855SPhilippe Mathieu-Daudé align = 4 * KiB;
110576e2aef3SAlexander Graf }
110676e2aef3SAlexander Graf
110776e2aef3SAlexander Graf /* Place the DTB after the initrd in memory with alignment. */
11083b77f6c3SIgor Mammedov info->dtb_start = QEMU_ALIGN_UP(info->initrd_start + initrd_size,
11093b77f6c3SIgor Mammedov align);
1110852dc64dSPeter Maydell if (info->dtb_start >= ram_end) {
1111852dc64dSPeter Maydell error_report("Not enough space for DTB after kernel/initrd");
1112852dc64dSPeter Maydell exit(1);
1113852dc64dSPeter Maydell }
1114751ebc13SRicardo Perez Blanco fixupcontext[FIXUP_ARGPTR_LO] = info->dtb_start;
1115751ebc13SRicardo Perez Blanco fixupcontext[FIXUP_ARGPTR_HI] = info->dtb_start >> 32;
111653018216SPaolo Bonzini } else {
1117751ebc13SRicardo Perez Blanco fixupcontext[FIXUP_ARGPTR_LO] =
1118751ebc13SRicardo Perez Blanco info->loader_start + KERNEL_ARGS_ADDR;
1119751ebc13SRicardo Perez Blanco fixupcontext[FIXUP_ARGPTR_HI] =
1120751ebc13SRicardo Perez Blanco (info->loader_start + KERNEL_ARGS_ADDR) >> 32;
1121e4e34855SPhilippe Mathieu-Daudé if (info->ram_size >= 4 * GiB) {
1122c0dbca36SAlistair Francis error_report("RAM size must be less than 4GB to boot"
112353018216SPaolo Bonzini " Linux kernel using ATAGS (try passing a device tree"
1124c0dbca36SAlistair Francis " using -dtb)");
112553018216SPaolo Bonzini exit(1);
112653018216SPaolo Bonzini }
112753018216SPaolo Bonzini }
1128751ebc13SRicardo Perez Blanco fixupcontext[FIXUP_ENTRYPOINT_LO] = entry;
1129751ebc13SRicardo Perez Blanco fixupcontext[FIXUP_ENTRYPOINT_HI] = entry >> 32;
113047b1da81SPeter Maydell
11310fe43f0aSCédric Le Goater arm_write_bootloader("bootloader", as, info->loader_start,
11320fe43f0aSCédric Le Goater primary_loader, fixupcontext);
113347b1da81SPeter Maydell
113410b8ec73SPeter Crosthwaite if (info->write_board_setup) {
113510b8ec73SPeter Crosthwaite info->write_board_setup(cpu, info);
113610b8ec73SPeter Crosthwaite }
1137d8b1ae42SPeter Maydell
1138c3a42358SPeter Maydell /*
1139c3a42358SPeter Maydell * Notify devices which need to fake up firmware initialization
1140d8b1ae42SPeter Maydell * that we're doing a direct kernel boot.
1141d8b1ae42SPeter Maydell */
1142d8b1ae42SPeter Maydell object_child_foreach_recursive(object_get_root(),
1143d8b1ae42SPeter Maydell do_arm_linux_init, info);
114453018216SPaolo Bonzini }
114553018216SPaolo Bonzini info->is_linux = is_linux;
114653018216SPaolo Bonzini
11470c949276SIgor Mammedov for (cs = first_cpu; cs; cs = CPU_NEXT(cs)) {
1148c6faa758SArd Biesheuvel ARM_CPU(cs)->env.boot_info = info;
114953018216SPaolo Bonzini }
1150d33774eeSPeter Maydell }
1151d33774eeSPeter Maydell
arm_setup_firmware_boot(ARMCPU * cpu,struct arm_boot_info * info)11524c0f2687SPeter Maydell static void arm_setup_firmware_boot(ARMCPU *cpu, struct arm_boot_info *info)
1153d33774eeSPeter Maydell {
11544c0f2687SPeter Maydell /* Set up for booting firmware (which might load a kernel via fw_cfg) */
1155d33774eeSPeter Maydell
1156d33774eeSPeter Maydell if (have_dtb(info)) {
1157d33774eeSPeter Maydell /*
1158d33774eeSPeter Maydell * If we have a device tree blob, but no kernel to supply it to (or
1159d33774eeSPeter Maydell * the kernel is supposed to be loaded by the bootloader), copy the
1160d33774eeSPeter Maydell * DTB to the base of RAM for the bootloader to pick up.
1161d33774eeSPeter Maydell */
1162d33774eeSPeter Maydell info->dtb_start = info->loader_start;
1163d33774eeSPeter Maydell }
1164d33774eeSPeter Maydell
1165d33774eeSPeter Maydell if (info->kernel_filename) {
1166d33774eeSPeter Maydell FWCfgState *fw_cfg;
1167d33774eeSPeter Maydell bool try_decompressing_kernel;
1168d33774eeSPeter Maydell
1169d33774eeSPeter Maydell fw_cfg = fw_cfg_find();
1170dae25739SPeter Maydell
1171dae25739SPeter Maydell if (!fw_cfg) {
1172dae25739SPeter Maydell error_report("This machine type does not support loading both "
1173dae25739SPeter Maydell "a guest firmware/BIOS image and a guest kernel at "
1174dae25739SPeter Maydell "the same time. You should change your QEMU command "
1175dae25739SPeter Maydell "line to specify one or the other, but not both.");
1176dae25739SPeter Maydell exit(1);
1177dae25739SPeter Maydell }
1178dae25739SPeter Maydell
1179d33774eeSPeter Maydell try_decompressing_kernel = arm_feature(&cpu->env,
1180d33774eeSPeter Maydell ARM_FEATURE_AARCH64);
1181d33774eeSPeter Maydell
1182d33774eeSPeter Maydell /*
1183d33774eeSPeter Maydell * Expose the kernel, the command line, and the initrd in fw_cfg.
1184d33774eeSPeter Maydell * We don't process them here at all, it's all left to the
1185d33774eeSPeter Maydell * firmware.
1186d33774eeSPeter Maydell */
1187d33774eeSPeter Maydell load_image_to_fw_cfg(fw_cfg,
1188d33774eeSPeter Maydell FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA,
1189d33774eeSPeter Maydell info->kernel_filename,
1190d33774eeSPeter Maydell try_decompressing_kernel);
1191d33774eeSPeter Maydell load_image_to_fw_cfg(fw_cfg,
1192d33774eeSPeter Maydell FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA,
1193d33774eeSPeter Maydell info->initrd_filename, false);
1194d33774eeSPeter Maydell
1195d33774eeSPeter Maydell if (info->kernel_cmdline) {
1196d33774eeSPeter Maydell fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
1197d33774eeSPeter Maydell strlen(info->kernel_cmdline) + 1);
1198d33774eeSPeter Maydell fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA,
1199d33774eeSPeter Maydell info->kernel_cmdline);
1200d33774eeSPeter Maydell }
1201d33774eeSPeter Maydell }
1202d33774eeSPeter Maydell
1203d33774eeSPeter Maydell /*
1204d33774eeSPeter Maydell * We will start from address 0 (typically a boot ROM image) in the
12052a5bdfc8SPeter Maydell * same way as hardware. Leave env->boot_info NULL, so that
12062a5bdfc8SPeter Maydell * do_cpu_reset() knows it does not need to alter the PC on reset.
1207d33774eeSPeter Maydell */
12084c0f2687SPeter Maydell }
12094c0f2687SPeter Maydell
arm_load_kernel(ARMCPU * cpu,MachineState * ms,struct arm_boot_info * info)12102744ece8STao Xu void arm_load_kernel(ARMCPU *cpu, MachineState *ms, struct arm_boot_info *info)
12114c0f2687SPeter Maydell {
12124c0f2687SPeter Maydell CPUState *cs;
12134c0f2687SPeter Maydell AddressSpace *as = arm_boot_address_space(cpu, info);
1214817e2db8SPeter Maydell int boot_el;
1215817e2db8SPeter Maydell CPUARMState *env = &cpu->env;
1216d6dc926eSPeter Maydell int nb_cpus = 0;
12174c0f2687SPeter Maydell
12184c0f2687SPeter Maydell /*
12194c0f2687SPeter Maydell * CPU objects (unlike devices) are not automatically reset on system
12204c0f2687SPeter Maydell * reset, so we must always register a handler to do so. If we're
12214c0f2687SPeter Maydell * actually loading a kernel, the handler is also responsible for
12224c0f2687SPeter Maydell * arranging that we start it correctly.
12234c0f2687SPeter Maydell */
12244c0f2687SPeter Maydell for (cs = first_cpu; cs; cs = CPU_NEXT(cs)) {
12254c0f2687SPeter Maydell qemu_register_reset(do_cpu_reset, ARM_CPU(cs));
1226d6dc926eSPeter Maydell nb_cpus++;
12274c0f2687SPeter Maydell }
12284c0f2687SPeter Maydell
12294c0f2687SPeter Maydell /*
12304c0f2687SPeter Maydell * The board code is not supposed to set secure_board_setup unless
12314c0f2687SPeter Maydell * running its code in secure mode is actually possible, and KVM
12324c0f2687SPeter Maydell * doesn't support secure.
12334c0f2687SPeter Maydell */
12344c0f2687SPeter Maydell assert(!(info->secure_board_setup && kvm_enabled()));
12352744ece8STao Xu info->kernel_filename = ms->kernel_filename;
12362744ece8STao Xu info->kernel_cmdline = ms->kernel_cmdline;
12372744ece8STao Xu info->initrd_filename = ms->initrd_filename;
1238f2ce39b4SPaolo Bonzini info->dtb_filename = ms->dtb;
12394c0f2687SPeter Maydell info->dtb_limit = 0;
12404c0f2687SPeter Maydell
12414c0f2687SPeter Maydell /* Load the kernel. */
12424c0f2687SPeter Maydell if (!info->kernel_filename || info->firmware_loaded) {
12434c0f2687SPeter Maydell arm_setup_firmware_boot(cpu, info);
1244d33774eeSPeter Maydell } else {
1245d33774eeSPeter Maydell arm_setup_direct_kernel_boot(cpu, info);
1246d33774eeSPeter Maydell }
124763a183edSEric Auger
1248817e2db8SPeter Maydell /*
1249817e2db8SPeter Maydell * Disable the PSCI conduit if it is set up to target the same
1250817e2db8SPeter Maydell * or a lower EL than the one we're going to start the guest code in.
1251817e2db8SPeter Maydell * This logic needs to agree with the code in do_cpu_reset() which
1252817e2db8SPeter Maydell * decides whether we're going to boot the guest in the highest
1253817e2db8SPeter Maydell * supported exception level or in a lower one.
1254817e2db8SPeter Maydell */
1255817e2db8SPeter Maydell
1256dc888dd4SPeter Maydell /*
1257dc888dd4SPeter Maydell * If PSCI is enabled, then SMC calls all go to the PSCI handler and
1258dc888dd4SPeter Maydell * are never emulated to trap into guest code. It therefore does not
1259dc888dd4SPeter Maydell * make sense for the board to have a setup code fragment that runs
1260dc888dd4SPeter Maydell * in Secure, because this will probably need to itself issue an SMC of some
1261dc888dd4SPeter Maydell * kind as part of its operation.
1262dc888dd4SPeter Maydell */
1263dc888dd4SPeter Maydell assert(info->psci_conduit == QEMU_PSCI_CONDUIT_DISABLED ||
1264dc888dd4SPeter Maydell !info->secure_board_setup);
1265dc888dd4SPeter Maydell
1266817e2db8SPeter Maydell /* Boot into highest supported EL ... */
1267817e2db8SPeter Maydell if (arm_feature(env, ARM_FEATURE_EL3)) {
1268817e2db8SPeter Maydell boot_el = 3;
1269817e2db8SPeter Maydell } else if (arm_feature(env, ARM_FEATURE_EL2)) {
1270817e2db8SPeter Maydell boot_el = 2;
1271817e2db8SPeter Maydell } else {
1272817e2db8SPeter Maydell boot_el = 1;
1273817e2db8SPeter Maydell }
1274817e2db8SPeter Maydell /* ...except that if we're booting Linux we adjust the EL we boot into */
1275817e2db8SPeter Maydell if (info->is_linux && !info->secure_boot) {
1276817e2db8SPeter Maydell boot_el = arm_feature(env, ARM_FEATURE_EL2) ? 2 : 1;
1277817e2db8SPeter Maydell }
1278817e2db8SPeter Maydell
1279817e2db8SPeter Maydell if ((info->psci_conduit == QEMU_PSCI_CONDUIT_HVC && boot_el >= 2) ||
1280817e2db8SPeter Maydell (info->psci_conduit == QEMU_PSCI_CONDUIT_SMC && boot_el == 3)) {
1281817e2db8SPeter Maydell info->psci_conduit = QEMU_PSCI_CONDUIT_DISABLED;
1282817e2db8SPeter Maydell }
1283817e2db8SPeter Maydell
1284817e2db8SPeter Maydell if (info->psci_conduit != QEMU_PSCI_CONDUIT_DISABLED) {
1285817e2db8SPeter Maydell for (cs = first_cpu; cs; cs = CPU_NEXT(cs)) {
1286817e2db8SPeter Maydell Object *cpuobj = OBJECT(cs);
1287817e2db8SPeter Maydell
1288817e2db8SPeter Maydell object_property_set_int(cpuobj, "psci-conduit", info->psci_conduit,
1289817e2db8SPeter Maydell &error_abort);
1290817e2db8SPeter Maydell /*
1291817e2db8SPeter Maydell * Secondary CPUs start in PSCI powered-down state. Like the
1292817e2db8SPeter Maydell * code in do_cpu_reset(), we assume first_cpu is the primary
1293817e2db8SPeter Maydell * CPU.
1294817e2db8SPeter Maydell */
1295817e2db8SPeter Maydell if (cs != first_cpu) {
1296817e2db8SPeter Maydell object_property_set_bool(cpuobj, "start-powered-off", true,
1297817e2db8SPeter Maydell &error_abort);
1298817e2db8SPeter Maydell }
1299817e2db8SPeter Maydell }
1300817e2db8SPeter Maydell }
1301817e2db8SPeter Maydell
1302d4a29ed6SPeter Maydell if (info->psci_conduit == QEMU_PSCI_CONDUIT_DISABLED &&
1303d6dc926eSPeter Maydell info->is_linux && nb_cpus > 1) {
1304d4a29ed6SPeter Maydell /*
1305d4a29ed6SPeter Maydell * We're booting Linux but not using PSCI, so for SMP we need
1306d4a29ed6SPeter Maydell * to write a custom secondary CPU boot loader stub, and arrange
1307d4a29ed6SPeter Maydell * for the secondary CPU reset to make the accompanying initialization.
1308d4a29ed6SPeter Maydell */
1309d4a29ed6SPeter Maydell if (!info->secondary_cpu_reset_hook) {
1310d4a29ed6SPeter Maydell info->secondary_cpu_reset_hook = default_reset_secondary;
1311d4a29ed6SPeter Maydell }
1312d4a29ed6SPeter Maydell if (!info->write_secondary_boot) {
1313d4a29ed6SPeter Maydell info->write_secondary_boot = default_write_secondary;
1314d4a29ed6SPeter Maydell }
1315d4a29ed6SPeter Maydell info->write_secondary_boot(cpu, info);
1316d4a29ed6SPeter Maydell } else {
1317d4a29ed6SPeter Maydell /*
1318d4a29ed6SPeter Maydell * No secondary boot stub; don't use the reset hook that would
1319d4a29ed6SPeter Maydell * have set the CPU up to call it
1320d4a29ed6SPeter Maydell */
1321d4a29ed6SPeter Maydell info->write_secondary_boot = NULL;
1322d4a29ed6SPeter Maydell info->secondary_cpu_reset_hook = NULL;
1323d4a29ed6SPeter Maydell }
1324d4a29ed6SPeter Maydell
1325817e2db8SPeter Maydell /*
1326817e2db8SPeter Maydell * arm_load_dtb() may add a PSCI node so it must be called after we have
1327817e2db8SPeter Maydell * decided whether to enable PSCI and set the psci-conduit CPU properties.
1328817e2db8SPeter Maydell */
13293b77f6c3SIgor Mammedov if (!info->skip_dtb_autoload && have_dtb(info)) {
13302744ece8STao Xu if (arm_load_dtb(info->dtb_start, info, info->dtb_limit, as, ms) < 0) {
13313b77f6c3SIgor Mammedov exit(1);
13323b77f6c3SIgor Mammedov }
13333b77f6c3SIgor Mammedov }
1334ac9d32e3SEric Auger }
1335d8b1ae42SPeter Maydell
1336d8b1ae42SPeter Maydell static const TypeInfo arm_linux_boot_if_info = {
1337d8b1ae42SPeter Maydell .name = TYPE_ARM_LINUX_BOOT_IF,
1338d8b1ae42SPeter Maydell .parent = TYPE_INTERFACE,
1339d8b1ae42SPeter Maydell .class_size = sizeof(ARMLinuxBootIfClass),
1340d8b1ae42SPeter Maydell };
1341d8b1ae42SPeter Maydell
arm_linux_boot_register_types(void)1342d8b1ae42SPeter Maydell static void arm_linux_boot_register_types(void)
1343d8b1ae42SPeter Maydell {
1344d8b1ae42SPeter Maydell type_register_static(&arm_linux_boot_if_info);
1345d8b1ae42SPeter Maydell }
1346d8b1ae42SPeter Maydell
1347d8b1ae42SPeter Maydell type_init(arm_linux_boot_register_types)
1348