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" 11a8d25326SMarkus Armbruster #include "qemu-common.h" 12*2c65db5eSPaolo Bonzini #include "qemu/datadir.h" 13c0dbca36SAlistair Francis #include "qemu/error-report.h" 14da34e65cSMarkus Armbruster #include "qapi/error.h" 15b77257d7SGuenter Roeck #include <libfdt.h> 1612ec8bd5SPeter Maydell #include "hw/arm/boot.h" 17d8b1ae42SPeter Maydell #include "hw/arm/linux-boot-if.h" 18baf6b681SPeter Crosthwaite #include "sysemu/kvm.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" 282198a121SEdgar E. Iglesias #include "exec/address-spaces.h" 29ea358872SStewart Hildebrand #include "qemu/units.h" 3053018216SPaolo Bonzini 314d9ebf75SMian M. Hamayun /* Kernel boot protocol is specified in the kernel docs 324d9ebf75SMian M. Hamayun * Documentation/arm/Booting and Documentation/arm64/booting.txt 334d9ebf75SMian M. Hamayun * They have different preferred image load offsets from system RAM base. 344d9ebf75SMian M. Hamayun */ 3553018216SPaolo Bonzini #define KERNEL_ARGS_ADDR 0x100 36f831f955SNick Hudson #define KERNEL_NOLOAD_ADDR 0x02000000 3753018216SPaolo Bonzini #define KERNEL_LOAD_ADDR 0x00010000 384d9ebf75SMian M. Hamayun #define KERNEL64_LOAD_ADDR 0x00080000 3953018216SPaolo Bonzini 4068115ed5SArd Biesheuvel #define ARM64_TEXT_OFFSET_OFFSET 8 4168115ed5SArd Biesheuvel #define ARM64_MAGIC_OFFSET 56 4268115ed5SArd Biesheuvel 43ea358872SStewart Hildebrand #define BOOTLOADER_MAX_SIZE (4 * KiB) 44ea358872SStewart Hildebrand 453b77f6c3SIgor Mammedov AddressSpace *arm_boot_address_space(ARMCPU *cpu, 469f43d4c3SPeter Maydell const struct arm_boot_info *info) 479f43d4c3SPeter Maydell { 489f43d4c3SPeter Maydell /* Return the address space to use for bootloader reads and writes. 499f43d4c3SPeter Maydell * We prefer the secure address space if the CPU has it and we're 509f43d4c3SPeter Maydell * going to boot the guest into it. 519f43d4c3SPeter Maydell */ 529f43d4c3SPeter Maydell int asidx; 539f43d4c3SPeter Maydell CPUState *cs = CPU(cpu); 549f43d4c3SPeter Maydell 559f43d4c3SPeter Maydell if (arm_feature(&cpu->env, ARM_FEATURE_EL3) && info->secure_boot) { 569f43d4c3SPeter Maydell asidx = ARMASIdx_S; 579f43d4c3SPeter Maydell } else { 589f43d4c3SPeter Maydell asidx = ARMASIdx_NS; 599f43d4c3SPeter Maydell } 609f43d4c3SPeter Maydell 619f43d4c3SPeter Maydell return cpu_get_address_space(cs, asidx); 629f43d4c3SPeter Maydell } 639f43d4c3SPeter Maydell 6447b1da81SPeter Maydell typedef enum { 6547b1da81SPeter Maydell FIXUP_NONE = 0, /* do nothing */ 6647b1da81SPeter Maydell FIXUP_TERMINATOR, /* end of insns */ 6747b1da81SPeter Maydell FIXUP_BOARDID, /* overwrite with board ID number */ 6810b8ec73SPeter Crosthwaite FIXUP_BOARD_SETUP, /* overwrite with board specific setup code address */ 69751ebc13SRicardo Perez Blanco FIXUP_ARGPTR_LO, /* overwrite with pointer to kernel args */ 70751ebc13SRicardo Perez Blanco FIXUP_ARGPTR_HI, /* overwrite with pointer to kernel args (high half) */ 71751ebc13SRicardo Perez Blanco FIXUP_ENTRYPOINT_LO, /* overwrite with kernel entry point */ 72751ebc13SRicardo Perez Blanco FIXUP_ENTRYPOINT_HI, /* overwrite with kernel entry point (high half) */ 7347b1da81SPeter Maydell FIXUP_GIC_CPU_IF, /* overwrite with GIC CPU interface address */ 7447b1da81SPeter Maydell FIXUP_BOOTREG, /* overwrite with boot register address */ 7547b1da81SPeter Maydell FIXUP_DSB, /* overwrite with correct DSB insn for cpu */ 7647b1da81SPeter Maydell FIXUP_MAX, 7747b1da81SPeter Maydell } FixupType; 7847b1da81SPeter Maydell 7947b1da81SPeter Maydell typedef struct ARMInsnFixup { 8047b1da81SPeter Maydell uint32_t insn; 8147b1da81SPeter Maydell FixupType fixup; 8247b1da81SPeter Maydell } ARMInsnFixup; 8347b1da81SPeter Maydell 844d9ebf75SMian M. Hamayun static const ARMInsnFixup bootloader_aarch64[] = { 854d9ebf75SMian M. Hamayun { 0x580000c0 }, /* ldr x0, arg ; Load the lower 32-bits of DTB */ 864d9ebf75SMian M. Hamayun { 0xaa1f03e1 }, /* mov x1, xzr */ 874d9ebf75SMian M. Hamayun { 0xaa1f03e2 }, /* mov x2, xzr */ 884d9ebf75SMian M. Hamayun { 0xaa1f03e3 }, /* mov x3, xzr */ 894d9ebf75SMian M. Hamayun { 0x58000084 }, /* ldr x4, entry ; Load the lower 32-bits of kernel entry */ 904d9ebf75SMian M. Hamayun { 0xd61f0080 }, /* br x4 ; Jump to the kernel entry point */ 91751ebc13SRicardo Perez Blanco { 0, FIXUP_ARGPTR_LO }, /* arg: .word @DTB Lower 32-bits */ 92751ebc13SRicardo Perez Blanco { 0, FIXUP_ARGPTR_HI}, /* .word @DTB Higher 32-bits */ 93751ebc13SRicardo Perez Blanco { 0, FIXUP_ENTRYPOINT_LO }, /* entry: .word @Kernel Entry Lower 32-bits */ 94751ebc13SRicardo Perez Blanco { 0, FIXUP_ENTRYPOINT_HI }, /* .word @Kernel Entry Higher 32-bits */ 954d9ebf75SMian M. Hamayun { 0, FIXUP_TERMINATOR } 964d9ebf75SMian M. Hamayun }; 974d9ebf75SMian M. Hamayun 9810b8ec73SPeter Crosthwaite /* A very small bootloader: call the board-setup code (if needed), 9910b8ec73SPeter Crosthwaite * set r0-r2, then jump to the kernel. 10010b8ec73SPeter Crosthwaite * If we're not calling boot setup code then we don't copy across 10110b8ec73SPeter Crosthwaite * the first BOOTLOADER_NO_BOARD_SETUP_OFFSET insns in this array. 10210b8ec73SPeter Crosthwaite */ 10310b8ec73SPeter Crosthwaite 10447b1da81SPeter Maydell static const ARMInsnFixup bootloader[] = { 105b4850e5aSSylvain Garrigues { 0xe28fe004 }, /* add lr, pc, #4 */ 10610b8ec73SPeter Crosthwaite { 0xe51ff004 }, /* ldr pc, [pc, #-4] */ 10710b8ec73SPeter Crosthwaite { 0, FIXUP_BOARD_SETUP }, 10810b8ec73SPeter Crosthwaite #define BOOTLOADER_NO_BOARD_SETUP_OFFSET 3 10947b1da81SPeter Maydell { 0xe3a00000 }, /* mov r0, #0 */ 11047b1da81SPeter Maydell { 0xe59f1004 }, /* ldr r1, [pc, #4] */ 11147b1da81SPeter Maydell { 0xe59f2004 }, /* ldr r2, [pc, #4] */ 11247b1da81SPeter Maydell { 0xe59ff004 }, /* ldr pc, [pc, #4] */ 11347b1da81SPeter Maydell { 0, FIXUP_BOARDID }, 114751ebc13SRicardo Perez Blanco { 0, FIXUP_ARGPTR_LO }, 115751ebc13SRicardo Perez Blanco { 0, FIXUP_ENTRYPOINT_LO }, 11647b1da81SPeter Maydell { 0, FIXUP_TERMINATOR } 11753018216SPaolo Bonzini }; 11853018216SPaolo Bonzini 11953018216SPaolo Bonzini /* Handling for secondary CPU boot in a multicore system. 12053018216SPaolo Bonzini * Unlike the uniprocessor/primary CPU boot, this is platform 12153018216SPaolo Bonzini * dependent. The default code here is based on the secondary 12253018216SPaolo Bonzini * CPU boot protocol used on realview/vexpress boards, with 12353018216SPaolo Bonzini * some parameterisation to increase its flexibility. 12453018216SPaolo Bonzini * QEMU platform models for which this code is not appropriate 12553018216SPaolo Bonzini * should override write_secondary_boot and secondary_cpu_reset_hook 12653018216SPaolo Bonzini * instead. 12753018216SPaolo Bonzini * 12853018216SPaolo Bonzini * This code enables the interrupt controllers for the secondary 12953018216SPaolo Bonzini * CPUs and then puts all the secondary CPUs into a loop waiting 13053018216SPaolo Bonzini * for an interprocessor interrupt and polling a configurable 13153018216SPaolo Bonzini * location for the kernel secondary CPU entry point. 13253018216SPaolo Bonzini */ 13353018216SPaolo Bonzini #define DSB_INSN 0xf57ff04f 13453018216SPaolo Bonzini #define CP15_DSB_INSN 0xee070f9a /* mcr cp15, 0, r0, c7, c10, 4 */ 13553018216SPaolo Bonzini 13647b1da81SPeter Maydell static const ARMInsnFixup smpboot[] = { 13747b1da81SPeter Maydell { 0xe59f2028 }, /* ldr r2, gic_cpu_if */ 13847b1da81SPeter Maydell { 0xe59f0028 }, /* ldr r0, bootreg_addr */ 13947b1da81SPeter Maydell { 0xe3a01001 }, /* mov r1, #1 */ 14047b1da81SPeter Maydell { 0xe5821000 }, /* str r1, [r2] - set GICC_CTLR.Enable */ 14147b1da81SPeter Maydell { 0xe3a010ff }, /* mov r1, #0xff */ 14247b1da81SPeter Maydell { 0xe5821004 }, /* str r1, [r2, 4] - set GIC_PMR.Priority to 0xff */ 14347b1da81SPeter Maydell { 0, FIXUP_DSB }, /* dsb */ 14447b1da81SPeter Maydell { 0xe320f003 }, /* wfi */ 14547b1da81SPeter Maydell { 0xe5901000 }, /* ldr r1, [r0] */ 14647b1da81SPeter Maydell { 0xe1110001 }, /* tst r1, r1 */ 14747b1da81SPeter Maydell { 0x0afffffb }, /* beq <wfi> */ 14847b1da81SPeter Maydell { 0xe12fff11 }, /* bx r1 */ 14947b1da81SPeter Maydell { 0, FIXUP_GIC_CPU_IF }, /* gic_cpu_if: .word 0x.... */ 15047b1da81SPeter Maydell { 0, FIXUP_BOOTREG }, /* bootreg_addr: .word 0x.... */ 15147b1da81SPeter Maydell { 0, FIXUP_TERMINATOR } 15253018216SPaolo Bonzini }; 15353018216SPaolo Bonzini 15447b1da81SPeter Maydell static void write_bootloader(const char *name, hwaddr addr, 1559f43d4c3SPeter Maydell const ARMInsnFixup *insns, uint32_t *fixupcontext, 1569f43d4c3SPeter Maydell AddressSpace *as) 15747b1da81SPeter Maydell { 15847b1da81SPeter Maydell /* Fix up the specified bootloader fragment and write it into 15947b1da81SPeter Maydell * guest memory using rom_add_blob_fixed(). fixupcontext is 16047b1da81SPeter Maydell * an array giving the values to write in for the fixup types 16147b1da81SPeter Maydell * which write a value into the code array. 16247b1da81SPeter Maydell */ 16347b1da81SPeter Maydell int i, len; 16447b1da81SPeter Maydell uint32_t *code; 16547b1da81SPeter Maydell 16647b1da81SPeter Maydell len = 0; 16747b1da81SPeter Maydell while (insns[len].fixup != FIXUP_TERMINATOR) { 16847b1da81SPeter Maydell len++; 16947b1da81SPeter Maydell } 17047b1da81SPeter Maydell 17147b1da81SPeter Maydell code = g_new0(uint32_t, len); 17247b1da81SPeter Maydell 17347b1da81SPeter Maydell for (i = 0; i < len; i++) { 17447b1da81SPeter Maydell uint32_t insn = insns[i].insn; 17547b1da81SPeter Maydell FixupType fixup = insns[i].fixup; 17647b1da81SPeter Maydell 17747b1da81SPeter Maydell switch (fixup) { 17847b1da81SPeter Maydell case FIXUP_NONE: 17947b1da81SPeter Maydell break; 18047b1da81SPeter Maydell case FIXUP_BOARDID: 18110b8ec73SPeter Crosthwaite case FIXUP_BOARD_SETUP: 182751ebc13SRicardo Perez Blanco case FIXUP_ARGPTR_LO: 183751ebc13SRicardo Perez Blanco case FIXUP_ARGPTR_HI: 184751ebc13SRicardo Perez Blanco case FIXUP_ENTRYPOINT_LO: 185751ebc13SRicardo Perez Blanco case FIXUP_ENTRYPOINT_HI: 18647b1da81SPeter Maydell case FIXUP_GIC_CPU_IF: 18747b1da81SPeter Maydell case FIXUP_BOOTREG: 18847b1da81SPeter Maydell case FIXUP_DSB: 18947b1da81SPeter Maydell insn = fixupcontext[fixup]; 19047b1da81SPeter Maydell break; 19147b1da81SPeter Maydell default: 19247b1da81SPeter Maydell abort(); 19347b1da81SPeter Maydell } 19447b1da81SPeter Maydell code[i] = tswap32(insn); 19547b1da81SPeter Maydell } 19647b1da81SPeter Maydell 197ea358872SStewart Hildebrand assert((len * sizeof(uint32_t)) < BOOTLOADER_MAX_SIZE); 198ea358872SStewart Hildebrand 1999f43d4c3SPeter Maydell rom_add_blob_fixed_as(name, code, len * sizeof(uint32_t), addr, as); 20047b1da81SPeter Maydell 20147b1da81SPeter Maydell g_free(code); 20247b1da81SPeter Maydell } 20347b1da81SPeter Maydell 20453018216SPaolo Bonzini static void default_write_secondary(ARMCPU *cpu, 20553018216SPaolo Bonzini const struct arm_boot_info *info) 20653018216SPaolo Bonzini { 20747b1da81SPeter Maydell uint32_t fixupcontext[FIXUP_MAX]; 2089f43d4c3SPeter Maydell AddressSpace *as = arm_boot_address_space(cpu, info); 20947b1da81SPeter Maydell 21047b1da81SPeter Maydell fixupcontext[FIXUP_GIC_CPU_IF] = info->gic_cpu_if_addr; 21147b1da81SPeter Maydell fixupcontext[FIXUP_BOOTREG] = info->smp_bootreg_addr; 21247b1da81SPeter Maydell if (arm_feature(&cpu->env, ARM_FEATURE_V7)) { 21347b1da81SPeter Maydell fixupcontext[FIXUP_DSB] = DSB_INSN; 21447b1da81SPeter Maydell } else { 21547b1da81SPeter Maydell fixupcontext[FIXUP_DSB] = CP15_DSB_INSN; 21653018216SPaolo Bonzini } 21747b1da81SPeter Maydell 21847b1da81SPeter Maydell write_bootloader("smpboot", info->smp_loader_start, 2199f43d4c3SPeter Maydell smpboot, fixupcontext, as); 22053018216SPaolo Bonzini } 22153018216SPaolo Bonzini 222716536a9SAndrew Baumann void arm_write_secure_board_setup_dummy_smc(ARMCPU *cpu, 223716536a9SAndrew Baumann const struct arm_boot_info *info, 224716536a9SAndrew Baumann hwaddr mvbar_addr) 225716536a9SAndrew Baumann { 2269f43d4c3SPeter Maydell AddressSpace *as = arm_boot_address_space(cpu, info); 227716536a9SAndrew Baumann int n; 228716536a9SAndrew Baumann uint32_t mvbar_blob[] = { 229716536a9SAndrew Baumann /* mvbar_addr: secure monitor vectors 230716536a9SAndrew Baumann * Default unimplemented and unused vectors to spin. Makes it 231716536a9SAndrew Baumann * easier to debug (as opposed to the CPU running away). 232716536a9SAndrew Baumann */ 233716536a9SAndrew Baumann 0xeafffffe, /* (spin) */ 234716536a9SAndrew Baumann 0xeafffffe, /* (spin) */ 235716536a9SAndrew Baumann 0xe1b0f00e, /* movs pc, lr ;SMC exception return */ 236716536a9SAndrew Baumann 0xeafffffe, /* (spin) */ 237716536a9SAndrew Baumann 0xeafffffe, /* (spin) */ 238716536a9SAndrew Baumann 0xeafffffe, /* (spin) */ 239716536a9SAndrew Baumann 0xeafffffe, /* (spin) */ 240716536a9SAndrew Baumann 0xeafffffe, /* (spin) */ 241716536a9SAndrew Baumann }; 242716536a9SAndrew Baumann uint32_t board_setup_blob[] = { 243716536a9SAndrew Baumann /* board setup addr */ 24445c078f1SClement Deschamps 0xee110f51, /* mrc p15, 0, r0, c1, c1, 2 ;read NSACR */ 24545c078f1SClement Deschamps 0xe3800b03, /* orr r0, #0xc00 ;set CP11, CP10 */ 24645c078f1SClement Deschamps 0xee010f51, /* mcr p15, 0, r0, c1, c1, 2 ;write NSACR */ 247716536a9SAndrew Baumann 0xe3a00e00 + (mvbar_addr >> 4), /* mov r0, #mvbar_addr */ 248716536a9SAndrew Baumann 0xee0c0f30, /* mcr p15, 0, r0, c12, c0, 1 ;set MVBAR */ 249716536a9SAndrew Baumann 0xee110f11, /* mrc p15, 0, r0, c1 , c1, 0 ;read SCR */ 250716536a9SAndrew Baumann 0xe3800031, /* orr r0, #0x31 ;enable AW, FW, NS */ 251716536a9SAndrew Baumann 0xee010f11, /* mcr p15, 0, r0, c1, c1, 0 ;write SCR */ 252716536a9SAndrew Baumann 0xe1a0100e, /* mov r1, lr ;save LR across SMC */ 253716536a9SAndrew Baumann 0xe1600070, /* smc #0 ;call monitor to flush SCR */ 254716536a9SAndrew Baumann 0xe1a0f001, /* mov pc, r1 ;return */ 255716536a9SAndrew Baumann }; 256716536a9SAndrew Baumann 257716536a9SAndrew Baumann /* check that mvbar_addr is correctly aligned and relocatable (using MOV) */ 258716536a9SAndrew Baumann assert((mvbar_addr & 0x1f) == 0 && (mvbar_addr >> 4) < 0x100); 259716536a9SAndrew Baumann 260716536a9SAndrew Baumann /* check that these blobs don't overlap */ 261716536a9SAndrew Baumann assert((mvbar_addr + sizeof(mvbar_blob) <= info->board_setup_addr) 262716536a9SAndrew Baumann || (info->board_setup_addr + sizeof(board_setup_blob) <= mvbar_addr)); 263716536a9SAndrew Baumann 264716536a9SAndrew Baumann for (n = 0; n < ARRAY_SIZE(mvbar_blob); n++) { 265716536a9SAndrew Baumann mvbar_blob[n] = tswap32(mvbar_blob[n]); 266716536a9SAndrew Baumann } 2679f43d4c3SPeter Maydell rom_add_blob_fixed_as("board-setup-mvbar", mvbar_blob, sizeof(mvbar_blob), 2689f43d4c3SPeter Maydell mvbar_addr, as); 269716536a9SAndrew Baumann 270716536a9SAndrew Baumann for (n = 0; n < ARRAY_SIZE(board_setup_blob); n++) { 271716536a9SAndrew Baumann board_setup_blob[n] = tswap32(board_setup_blob[n]); 272716536a9SAndrew Baumann } 2739f43d4c3SPeter Maydell rom_add_blob_fixed_as("board-setup", board_setup_blob, 2749f43d4c3SPeter Maydell sizeof(board_setup_blob), info->board_setup_addr, as); 275716536a9SAndrew Baumann } 276716536a9SAndrew Baumann 27753018216SPaolo Bonzini static void default_reset_secondary(ARMCPU *cpu, 27853018216SPaolo Bonzini const struct arm_boot_info *info) 27953018216SPaolo Bonzini { 2809f43d4c3SPeter Maydell AddressSpace *as = arm_boot_address_space(cpu, info); 2814df81c6eSPeter Crosthwaite CPUState *cs = CPU(cpu); 28253018216SPaolo Bonzini 2839f43d4c3SPeter Maydell address_space_stl_notdirty(as, info->smp_bootreg_addr, 28442874d3aSPeter Maydell 0, MEMTXATTRS_UNSPECIFIED, NULL); 2854df81c6eSPeter Crosthwaite cpu_set_pc(cs, info->smp_loader_start); 28653018216SPaolo Bonzini } 28753018216SPaolo Bonzini 28883bfffecSPeter Maydell static inline bool have_dtb(const struct arm_boot_info *info) 28983bfffecSPeter Maydell { 29083bfffecSPeter Maydell return info->dtb_filename || info->get_dtb; 29183bfffecSPeter Maydell } 29283bfffecSPeter Maydell 29353018216SPaolo Bonzini #define WRITE_WORD(p, value) do { \ 2949f43d4c3SPeter Maydell address_space_stl_notdirty(as, p, value, \ 29542874d3aSPeter Maydell MEMTXATTRS_UNSPECIFIED, NULL); \ 29653018216SPaolo Bonzini p += 4; \ 29753018216SPaolo Bonzini } while (0) 29853018216SPaolo Bonzini 2999f43d4c3SPeter Maydell static void set_kernel_args(const struct arm_boot_info *info, AddressSpace *as) 30053018216SPaolo Bonzini { 30153018216SPaolo Bonzini int initrd_size = info->initrd_size; 30253018216SPaolo Bonzini hwaddr base = info->loader_start; 30353018216SPaolo Bonzini hwaddr p; 30453018216SPaolo Bonzini 30553018216SPaolo Bonzini p = base + KERNEL_ARGS_ADDR; 30653018216SPaolo Bonzini /* ATAG_CORE */ 30753018216SPaolo Bonzini WRITE_WORD(p, 5); 30853018216SPaolo Bonzini WRITE_WORD(p, 0x54410001); 30953018216SPaolo Bonzini WRITE_WORD(p, 1); 31053018216SPaolo Bonzini WRITE_WORD(p, 0x1000); 31153018216SPaolo Bonzini WRITE_WORD(p, 0); 31253018216SPaolo Bonzini /* ATAG_MEM */ 31353018216SPaolo Bonzini /* TODO: handle multiple chips on one ATAG list */ 31453018216SPaolo Bonzini WRITE_WORD(p, 4); 31553018216SPaolo Bonzini WRITE_WORD(p, 0x54410002); 31653018216SPaolo Bonzini WRITE_WORD(p, info->ram_size); 31753018216SPaolo Bonzini WRITE_WORD(p, info->loader_start); 31853018216SPaolo Bonzini if (initrd_size) { 31953018216SPaolo Bonzini /* ATAG_INITRD2 */ 32053018216SPaolo Bonzini WRITE_WORD(p, 4); 32153018216SPaolo Bonzini WRITE_WORD(p, 0x54420005); 32253018216SPaolo Bonzini WRITE_WORD(p, info->initrd_start); 32353018216SPaolo Bonzini WRITE_WORD(p, initrd_size); 32453018216SPaolo Bonzini } 32553018216SPaolo Bonzini if (info->kernel_cmdline && *info->kernel_cmdline) { 32653018216SPaolo Bonzini /* ATAG_CMDLINE */ 32753018216SPaolo Bonzini int cmdline_size; 32853018216SPaolo Bonzini 32953018216SPaolo Bonzini cmdline_size = strlen(info->kernel_cmdline); 3309f43d4c3SPeter Maydell address_space_write(as, p + 8, MEMTXATTRS_UNSPECIFIED, 331b7cbebf2SPhilippe Mathieu-Daudé info->kernel_cmdline, cmdline_size + 1); 33253018216SPaolo Bonzini cmdline_size = (cmdline_size >> 2) + 1; 33353018216SPaolo Bonzini WRITE_WORD(p, cmdline_size + 2); 33453018216SPaolo Bonzini WRITE_WORD(p, 0x54410009); 33553018216SPaolo Bonzini p += cmdline_size * 4; 33653018216SPaolo Bonzini } 33753018216SPaolo Bonzini if (info->atag_board) { 33853018216SPaolo Bonzini /* ATAG_BOARD */ 33953018216SPaolo Bonzini int atag_board_len; 34053018216SPaolo Bonzini uint8_t atag_board_buf[0x1000]; 34153018216SPaolo Bonzini 34253018216SPaolo Bonzini atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3; 34353018216SPaolo Bonzini WRITE_WORD(p, (atag_board_len + 8) >> 2); 34453018216SPaolo Bonzini WRITE_WORD(p, 0x414f4d50); 3459f43d4c3SPeter Maydell address_space_write(as, p, MEMTXATTRS_UNSPECIFIED, 3469f43d4c3SPeter Maydell atag_board_buf, atag_board_len); 34753018216SPaolo Bonzini p += atag_board_len; 34853018216SPaolo Bonzini } 34953018216SPaolo Bonzini /* ATAG_END */ 35053018216SPaolo Bonzini WRITE_WORD(p, 0); 35153018216SPaolo Bonzini WRITE_WORD(p, 0); 35253018216SPaolo Bonzini } 35353018216SPaolo Bonzini 3549f43d4c3SPeter Maydell static void set_kernel_args_old(const struct arm_boot_info *info, 3559f43d4c3SPeter Maydell AddressSpace *as) 35653018216SPaolo Bonzini { 35753018216SPaolo Bonzini hwaddr p; 35853018216SPaolo Bonzini const char *s; 35953018216SPaolo Bonzini int initrd_size = info->initrd_size; 36053018216SPaolo Bonzini hwaddr base = info->loader_start; 36153018216SPaolo Bonzini 36253018216SPaolo Bonzini /* see linux/include/asm-arm/setup.h */ 36353018216SPaolo Bonzini p = base + KERNEL_ARGS_ADDR; 36453018216SPaolo Bonzini /* page_size */ 36553018216SPaolo Bonzini WRITE_WORD(p, 4096); 36653018216SPaolo Bonzini /* nr_pages */ 36753018216SPaolo Bonzini WRITE_WORD(p, info->ram_size / 4096); 36853018216SPaolo Bonzini /* ramdisk_size */ 36953018216SPaolo Bonzini WRITE_WORD(p, 0); 37053018216SPaolo Bonzini #define FLAG_READONLY 1 37153018216SPaolo Bonzini #define FLAG_RDLOAD 4 37253018216SPaolo Bonzini #define FLAG_RDPROMPT 8 37353018216SPaolo Bonzini /* flags */ 37453018216SPaolo Bonzini WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT); 37553018216SPaolo Bonzini /* rootdev */ 37653018216SPaolo Bonzini WRITE_WORD(p, (31 << 8) | 0); /* /dev/mtdblock0 */ 37753018216SPaolo Bonzini /* video_num_cols */ 37853018216SPaolo Bonzini WRITE_WORD(p, 0); 37953018216SPaolo Bonzini /* video_num_rows */ 38053018216SPaolo Bonzini WRITE_WORD(p, 0); 38153018216SPaolo Bonzini /* video_x */ 38253018216SPaolo Bonzini WRITE_WORD(p, 0); 38353018216SPaolo Bonzini /* video_y */ 38453018216SPaolo Bonzini WRITE_WORD(p, 0); 38553018216SPaolo Bonzini /* memc_control_reg */ 38653018216SPaolo Bonzini WRITE_WORD(p, 0); 38753018216SPaolo Bonzini /* unsigned char sounddefault */ 38853018216SPaolo Bonzini /* unsigned char adfsdrives */ 38953018216SPaolo Bonzini /* unsigned char bytes_per_char_h */ 39053018216SPaolo Bonzini /* unsigned char bytes_per_char_v */ 39153018216SPaolo Bonzini WRITE_WORD(p, 0); 39253018216SPaolo Bonzini /* pages_in_bank[4] */ 39353018216SPaolo Bonzini WRITE_WORD(p, 0); 39453018216SPaolo Bonzini WRITE_WORD(p, 0); 39553018216SPaolo Bonzini WRITE_WORD(p, 0); 39653018216SPaolo Bonzini WRITE_WORD(p, 0); 39753018216SPaolo Bonzini /* pages_in_vram */ 39853018216SPaolo Bonzini WRITE_WORD(p, 0); 39953018216SPaolo Bonzini /* initrd_start */ 40053018216SPaolo Bonzini if (initrd_size) { 40153018216SPaolo Bonzini WRITE_WORD(p, info->initrd_start); 40253018216SPaolo Bonzini } else { 40353018216SPaolo Bonzini WRITE_WORD(p, 0); 40453018216SPaolo Bonzini } 40553018216SPaolo Bonzini /* initrd_size */ 40653018216SPaolo Bonzini WRITE_WORD(p, initrd_size); 40753018216SPaolo Bonzini /* rd_start */ 40853018216SPaolo Bonzini WRITE_WORD(p, 0); 40953018216SPaolo Bonzini /* system_rev */ 41053018216SPaolo Bonzini WRITE_WORD(p, 0); 41153018216SPaolo Bonzini /* system_serial_low */ 41253018216SPaolo Bonzini WRITE_WORD(p, 0); 41353018216SPaolo Bonzini /* system_serial_high */ 41453018216SPaolo Bonzini WRITE_WORD(p, 0); 41553018216SPaolo Bonzini /* mem_fclk_21285 */ 41653018216SPaolo Bonzini WRITE_WORD(p, 0); 41753018216SPaolo Bonzini /* zero unused fields */ 41853018216SPaolo Bonzini while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) { 41953018216SPaolo Bonzini WRITE_WORD(p, 0); 42053018216SPaolo Bonzini } 42153018216SPaolo Bonzini s = info->kernel_cmdline; 42253018216SPaolo Bonzini if (s) { 423b7cbebf2SPhilippe Mathieu-Daudé address_space_write(as, p, MEMTXATTRS_UNSPECIFIED, s, strlen(s) + 1); 42453018216SPaolo Bonzini } else { 42553018216SPaolo Bonzini WRITE_WORD(p, 0); 42653018216SPaolo Bonzini } 42753018216SPaolo Bonzini } 42853018216SPaolo Bonzini 429f08ced69SShameer Kolothum static int fdt_add_memory_node(void *fdt, uint32_t acells, hwaddr mem_base, 430f08ced69SShameer Kolothum uint32_t scells, hwaddr mem_len, 431f08ced69SShameer Kolothum int numa_node_id) 432f08ced69SShameer Kolothum { 433f08ced69SShameer Kolothum char *nodename; 434f08ced69SShameer Kolothum int ret; 435f08ced69SShameer Kolothum 436f08ced69SShameer Kolothum nodename = g_strdup_printf("/memory@%" PRIx64, mem_base); 437f08ced69SShameer Kolothum qemu_fdt_add_subnode(fdt, nodename); 438f08ced69SShameer Kolothum qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory"); 439f08ced69SShameer Kolothum ret = qemu_fdt_setprop_sized_cells(fdt, nodename, "reg", acells, mem_base, 440f08ced69SShameer Kolothum scells, mem_len); 441f08ced69SShameer Kolothum if (ret < 0) { 442f08ced69SShameer Kolothum goto out; 443f08ced69SShameer Kolothum } 444f08ced69SShameer Kolothum 445f08ced69SShameer Kolothum /* only set the NUMA ID if it is specified */ 446f08ced69SShameer Kolothum if (numa_node_id >= 0) { 447f08ced69SShameer Kolothum ret = qemu_fdt_setprop_cell(fdt, nodename, 448f08ced69SShameer Kolothum "numa-node-id", numa_node_id); 449f08ced69SShameer Kolothum } 450f08ced69SShameer Kolothum out: 451f08ced69SShameer Kolothum g_free(nodename); 452f08ced69SShameer Kolothum return ret; 453f08ced69SShameer Kolothum } 454f08ced69SShameer Kolothum 4554cbca7d9SAndrey Smirnov static void fdt_add_psci_node(void *fdt) 4564cbca7d9SAndrey Smirnov { 4574cbca7d9SAndrey Smirnov uint32_t cpu_suspend_fn; 4584cbca7d9SAndrey Smirnov uint32_t cpu_off_fn; 4594cbca7d9SAndrey Smirnov uint32_t cpu_on_fn; 4604cbca7d9SAndrey Smirnov uint32_t migrate_fn; 4614cbca7d9SAndrey Smirnov ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(0)); 4624cbca7d9SAndrey Smirnov const char *psci_method; 4634cbca7d9SAndrey Smirnov int64_t psci_conduit; 464c39770cdSAndrey Smirnov int rc; 4654cbca7d9SAndrey Smirnov 4664cbca7d9SAndrey Smirnov psci_conduit = object_property_get_int(OBJECT(armcpu), 4674cbca7d9SAndrey Smirnov "psci-conduit", 4684cbca7d9SAndrey Smirnov &error_abort); 4694cbca7d9SAndrey Smirnov switch (psci_conduit) { 4704cbca7d9SAndrey Smirnov case QEMU_PSCI_CONDUIT_DISABLED: 4714cbca7d9SAndrey Smirnov return; 4724cbca7d9SAndrey Smirnov case QEMU_PSCI_CONDUIT_HVC: 4734cbca7d9SAndrey Smirnov psci_method = "hvc"; 4744cbca7d9SAndrey Smirnov break; 4754cbca7d9SAndrey Smirnov case QEMU_PSCI_CONDUIT_SMC: 4764cbca7d9SAndrey Smirnov psci_method = "smc"; 4774cbca7d9SAndrey Smirnov break; 4784cbca7d9SAndrey Smirnov default: 4794cbca7d9SAndrey Smirnov g_assert_not_reached(); 4804cbca7d9SAndrey Smirnov } 4814cbca7d9SAndrey Smirnov 482c39770cdSAndrey Smirnov /* 483c39770cdSAndrey Smirnov * If /psci node is present in provided DTB, assume that no fixup 484c39770cdSAndrey Smirnov * is necessary and all PSCI configuration should be taken as-is 485c39770cdSAndrey Smirnov */ 486c39770cdSAndrey Smirnov rc = fdt_path_offset(fdt, "/psci"); 487c39770cdSAndrey Smirnov if (rc >= 0) { 488c39770cdSAndrey Smirnov return; 489c39770cdSAndrey Smirnov } 490c39770cdSAndrey Smirnov 4914cbca7d9SAndrey Smirnov qemu_fdt_add_subnode(fdt, "/psci"); 4924cbca7d9SAndrey Smirnov if (armcpu->psci_version == 2) { 4934cbca7d9SAndrey Smirnov const char comp[] = "arm,psci-0.2\0arm,psci"; 4944cbca7d9SAndrey Smirnov qemu_fdt_setprop(fdt, "/psci", "compatible", comp, sizeof(comp)); 4954cbca7d9SAndrey Smirnov 4964cbca7d9SAndrey Smirnov cpu_off_fn = QEMU_PSCI_0_2_FN_CPU_OFF; 4974cbca7d9SAndrey Smirnov if (arm_feature(&armcpu->env, ARM_FEATURE_AARCH64)) { 4984cbca7d9SAndrey Smirnov cpu_suspend_fn = QEMU_PSCI_0_2_FN64_CPU_SUSPEND; 4994cbca7d9SAndrey Smirnov cpu_on_fn = QEMU_PSCI_0_2_FN64_CPU_ON; 5004cbca7d9SAndrey Smirnov migrate_fn = QEMU_PSCI_0_2_FN64_MIGRATE; 5014cbca7d9SAndrey Smirnov } else { 5024cbca7d9SAndrey Smirnov cpu_suspend_fn = QEMU_PSCI_0_2_FN_CPU_SUSPEND; 5034cbca7d9SAndrey Smirnov cpu_on_fn = QEMU_PSCI_0_2_FN_CPU_ON; 5044cbca7d9SAndrey Smirnov migrate_fn = QEMU_PSCI_0_2_FN_MIGRATE; 5054cbca7d9SAndrey Smirnov } 5064cbca7d9SAndrey Smirnov } else { 5074cbca7d9SAndrey Smirnov qemu_fdt_setprop_string(fdt, "/psci", "compatible", "arm,psci"); 5084cbca7d9SAndrey Smirnov 5094cbca7d9SAndrey Smirnov cpu_suspend_fn = QEMU_PSCI_0_1_FN_CPU_SUSPEND; 5104cbca7d9SAndrey Smirnov cpu_off_fn = QEMU_PSCI_0_1_FN_CPU_OFF; 5114cbca7d9SAndrey Smirnov cpu_on_fn = QEMU_PSCI_0_1_FN_CPU_ON; 5124cbca7d9SAndrey Smirnov migrate_fn = QEMU_PSCI_0_1_FN_MIGRATE; 5134cbca7d9SAndrey Smirnov } 5144cbca7d9SAndrey Smirnov 5154cbca7d9SAndrey Smirnov /* We adopt the PSCI spec's nomenclature, and use 'conduit' to refer 5164cbca7d9SAndrey Smirnov * to the instruction that should be used to invoke PSCI functions. 5174cbca7d9SAndrey Smirnov * However, the device tree binding uses 'method' instead, so that is 5184cbca7d9SAndrey Smirnov * what we should use here. 5194cbca7d9SAndrey Smirnov */ 5204cbca7d9SAndrey Smirnov qemu_fdt_setprop_string(fdt, "/psci", "method", psci_method); 5214cbca7d9SAndrey Smirnov 5224cbca7d9SAndrey Smirnov qemu_fdt_setprop_cell(fdt, "/psci", "cpu_suspend", cpu_suspend_fn); 5234cbca7d9SAndrey Smirnov qemu_fdt_setprop_cell(fdt, "/psci", "cpu_off", cpu_off_fn); 5244cbca7d9SAndrey Smirnov qemu_fdt_setprop_cell(fdt, "/psci", "cpu_on", cpu_on_fn); 5254cbca7d9SAndrey Smirnov qemu_fdt_setprop_cell(fdt, "/psci", "migrate", migrate_fn); 5264cbca7d9SAndrey Smirnov } 5274cbca7d9SAndrey Smirnov 5283b77f6c3SIgor Mammedov int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo, 5292744ece8STao Xu hwaddr addr_limit, AddressSpace *as, MachineState *ms) 53053018216SPaolo Bonzini { 53153018216SPaolo Bonzini void *fdt = NULL; 532e2eb3d29SEric Auger int size, rc, n = 0; 53370976c41SPeter Maydell uint32_t acells, scells; 5349695200aSShannon Zhao unsigned int i; 5359695200aSShannon Zhao hwaddr mem_base, mem_len; 536e2eb3d29SEric Auger char **node_path; 537e2eb3d29SEric Auger Error *err = NULL; 53853018216SPaolo Bonzini 5390fb79851SJohn Rigby if (binfo->dtb_filename) { 5400fb79851SJohn Rigby char *filename; 54153018216SPaolo Bonzini filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename); 54253018216SPaolo Bonzini if (!filename) { 54353018216SPaolo Bonzini fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename); 544c23045deSPeter Maydell goto fail; 54553018216SPaolo Bonzini } 54653018216SPaolo Bonzini 54753018216SPaolo Bonzini fdt = load_device_tree(filename, &size); 54853018216SPaolo Bonzini if (!fdt) { 54953018216SPaolo Bonzini fprintf(stderr, "Couldn't open dtb file %s\n", filename); 55053018216SPaolo Bonzini g_free(filename); 551c23045deSPeter Maydell goto fail; 55253018216SPaolo Bonzini } 55353018216SPaolo Bonzini g_free(filename); 554a554ecb4Szhanghailiang } else { 5550fb79851SJohn Rigby fdt = binfo->get_dtb(binfo, &size); 5560fb79851SJohn Rigby if (!fdt) { 5570fb79851SJohn Rigby fprintf(stderr, "Board was unable to create a dtb blob\n"); 5580fb79851SJohn Rigby goto fail; 5590fb79851SJohn Rigby } 5600fb79851SJohn Rigby } 56153018216SPaolo Bonzini 562fee8ea12SArd Biesheuvel if (addr_limit > addr && size > (addr_limit - addr)) { 563fee8ea12SArd Biesheuvel /* Installing the device tree blob at addr would exceed addr_limit. 564fee8ea12SArd Biesheuvel * Whether this constitutes failure is up to the caller to decide, 565fee8ea12SArd Biesheuvel * so just return 0 as size, i.e., no error. 566fee8ea12SArd Biesheuvel */ 567fee8ea12SArd Biesheuvel g_free(fdt); 568fee8ea12SArd Biesheuvel return 0; 569fee8ea12SArd Biesheuvel } 570fee8ea12SArd Biesheuvel 57158e71097SEric Auger acells = qemu_fdt_getprop_cell(fdt, "/", "#address-cells", 57258e71097SEric Auger NULL, &error_fatal); 57358e71097SEric Auger scells = qemu_fdt_getprop_cell(fdt, "/", "#size-cells", 57458e71097SEric Auger NULL, &error_fatal); 57553018216SPaolo Bonzini if (acells == 0 || scells == 0) { 57653018216SPaolo Bonzini fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n"); 577c23045deSPeter Maydell goto fail; 57853018216SPaolo Bonzini } 57953018216SPaolo Bonzini 580e4e34855SPhilippe Mathieu-Daudé if (scells < 2 && binfo->ram_size >= 4 * GiB) { 58170976c41SPeter Maydell /* This is user error so deserves a friendlier error message 58270976c41SPeter Maydell * than the failure of setprop_sized_cells would provide 58370976c41SPeter Maydell */ 58453018216SPaolo Bonzini fprintf(stderr, "qemu: dtb file not compatible with " 58553018216SPaolo Bonzini "RAM size > 4GB\n"); 586c23045deSPeter Maydell goto fail; 58753018216SPaolo Bonzini } 58853018216SPaolo Bonzini 589e2eb3d29SEric Auger /* nop all root nodes matching /memory or /memory@unit-address */ 590e2eb3d29SEric Auger node_path = qemu_fdt_node_unit_path(fdt, "memory", &err); 591e2eb3d29SEric Auger if (err) { 592e2eb3d29SEric Auger error_report_err(err); 593e2eb3d29SEric Auger goto fail; 594e2eb3d29SEric Auger } 595e2eb3d29SEric Auger while (node_path[n]) { 596e2eb3d29SEric Auger if (g_str_has_prefix(node_path[n], "/memory")) { 597e2eb3d29SEric Auger qemu_fdt_nop_node(fdt, node_path[n]); 598e2eb3d29SEric Auger } 599e2eb3d29SEric Auger n++; 600e2eb3d29SEric Auger } 601e2eb3d29SEric Auger g_strfreev(node_path); 602e2eb3d29SEric Auger 603aa570207STao Xu if (ms->numa_state != NULL && ms->numa_state->num_nodes > 0) { 6049695200aSShannon Zhao mem_base = binfo->loader_start; 605aa570207STao Xu for (i = 0; i < ms->numa_state->num_nodes; i++) { 6067e721e7bSTao Xu mem_len = ms->numa_state->nodes[i].node_mem; 607f08ced69SShameer Kolothum rc = fdt_add_memory_node(fdt, acells, mem_base, 608f08ced69SShameer Kolothum scells, mem_len, i); 6099695200aSShannon Zhao if (rc < 0) { 610f08ced69SShameer Kolothum fprintf(stderr, "couldn't add /memory@%"PRIx64" node\n", 611f08ced69SShameer Kolothum mem_base); 6129695200aSShannon Zhao goto fail; 6139695200aSShannon Zhao } 6149695200aSShannon Zhao 6159695200aSShannon Zhao mem_base += mem_len; 6169695200aSShannon Zhao } 6179695200aSShannon Zhao } else { 618f08ced69SShameer Kolothum rc = fdt_add_memory_node(fdt, acells, binfo->loader_start, 619f08ced69SShameer Kolothum scells, binfo->ram_size, -1); 62053018216SPaolo Bonzini if (rc < 0) { 621f08ced69SShameer Kolothum fprintf(stderr, "couldn't add /memory@%"PRIx64" node\n", 622f08ced69SShameer Kolothum binfo->loader_start); 623c23045deSPeter Maydell goto fail; 62453018216SPaolo Bonzini } 6259695200aSShannon Zhao } 62653018216SPaolo Bonzini 627b77257d7SGuenter Roeck rc = fdt_path_offset(fdt, "/chosen"); 628b77257d7SGuenter Roeck if (rc < 0) { 629b77257d7SGuenter Roeck qemu_fdt_add_subnode(fdt, "/chosen"); 630b77257d7SGuenter Roeck } 631b77257d7SGuenter Roeck 6322744ece8STao Xu if (ms->kernel_cmdline && *ms->kernel_cmdline) { 6335a4348d1SPeter Crosthwaite rc = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", 6342744ece8STao Xu ms->kernel_cmdline); 63553018216SPaolo Bonzini if (rc < 0) { 63653018216SPaolo Bonzini fprintf(stderr, "couldn't set /chosen/bootargs\n"); 637c23045deSPeter Maydell goto fail; 63853018216SPaolo Bonzini } 63953018216SPaolo Bonzini } 64053018216SPaolo Bonzini 64153018216SPaolo Bonzini if (binfo->initrd_size) { 6425a4348d1SPeter Crosthwaite rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start", 64353018216SPaolo Bonzini binfo->initrd_start); 64453018216SPaolo Bonzini if (rc < 0) { 64553018216SPaolo Bonzini fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n"); 646c23045deSPeter Maydell goto fail; 64753018216SPaolo Bonzini } 64853018216SPaolo Bonzini 6495a4348d1SPeter Crosthwaite rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end", 65053018216SPaolo Bonzini binfo->initrd_start + binfo->initrd_size); 65153018216SPaolo Bonzini if (rc < 0) { 65253018216SPaolo Bonzini fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n"); 653c23045deSPeter Maydell goto fail; 65453018216SPaolo Bonzini } 65553018216SPaolo Bonzini } 6563b1cceb8SPeter Maydell 6574cbca7d9SAndrey Smirnov fdt_add_psci_node(fdt); 6584cbca7d9SAndrey Smirnov 6593b1cceb8SPeter Maydell if (binfo->modify_dtb) { 6603b1cceb8SPeter Maydell binfo->modify_dtb(binfo, fdt); 6613b1cceb8SPeter Maydell } 6623b1cceb8SPeter Maydell 6635a4348d1SPeter Crosthwaite qemu_fdt_dumpdtb(fdt, size); 66453018216SPaolo Bonzini 6654c4bf654SArd Biesheuvel /* Put the DTB into the memory map as a ROM image: this will ensure 6664c4bf654SArd Biesheuvel * the DTB is copied again upon reset, even if addr points into RAM. 6674c4bf654SArd Biesheuvel */ 6689f43d4c3SPeter Maydell rom_add_blob_fixed_as("dtb", fdt, size, addr, as); 66953018216SPaolo Bonzini 670c23045deSPeter Maydell g_free(fdt); 671c23045deSPeter Maydell 672fee8ea12SArd Biesheuvel return size; 673c23045deSPeter Maydell 674c23045deSPeter Maydell fail: 675c23045deSPeter Maydell g_free(fdt); 676c23045deSPeter Maydell return -1; 67753018216SPaolo Bonzini } 67853018216SPaolo Bonzini 67953018216SPaolo Bonzini static void do_cpu_reset(void *opaque) 68053018216SPaolo Bonzini { 68153018216SPaolo Bonzini ARMCPU *cpu = opaque; 6824df81c6eSPeter Crosthwaite CPUState *cs = CPU(cpu); 68353018216SPaolo Bonzini CPUARMState *env = &cpu->env; 68453018216SPaolo Bonzini const struct arm_boot_info *info = env->boot_info; 68553018216SPaolo Bonzini 6864df81c6eSPeter Crosthwaite cpu_reset(cs); 68753018216SPaolo Bonzini if (info) { 68853018216SPaolo Bonzini if (!info->is_linux) { 6899776f636SPeter Crosthwaite int i; 69053018216SPaolo Bonzini /* Jump to the entry point. */ 6914df81c6eSPeter Crosthwaite uint64_t entry = info->entry; 6924df81c6eSPeter Crosthwaite 6939776f636SPeter Crosthwaite switch (info->endianness) { 6949776f636SPeter Crosthwaite case ARM_ENDIANNESS_LE: 6959776f636SPeter Crosthwaite env->cp15.sctlr_el[1] &= ~SCTLR_E0E; 6969776f636SPeter Crosthwaite for (i = 1; i < 4; ++i) { 6979776f636SPeter Crosthwaite env->cp15.sctlr_el[i] &= ~SCTLR_EE; 6989776f636SPeter Crosthwaite } 6999776f636SPeter Crosthwaite env->uncached_cpsr &= ~CPSR_E; 7009776f636SPeter Crosthwaite break; 7019776f636SPeter Crosthwaite case ARM_ENDIANNESS_BE8: 7029776f636SPeter Crosthwaite env->cp15.sctlr_el[1] |= SCTLR_E0E; 7039776f636SPeter Crosthwaite for (i = 1; i < 4; ++i) { 7049776f636SPeter Crosthwaite env->cp15.sctlr_el[i] |= SCTLR_EE; 7059776f636SPeter Crosthwaite } 7069776f636SPeter Crosthwaite env->uncached_cpsr |= CPSR_E; 7079776f636SPeter Crosthwaite break; 7089776f636SPeter Crosthwaite case ARM_ENDIANNESS_BE32: 7099776f636SPeter Crosthwaite env->cp15.sctlr_el[1] |= SCTLR_B; 7109776f636SPeter Crosthwaite break; 7119776f636SPeter Crosthwaite case ARM_ENDIANNESS_UNKNOWN: 7129776f636SPeter Crosthwaite break; /* Board's decision */ 7139776f636SPeter Crosthwaite default: 7149776f636SPeter Crosthwaite g_assert_not_reached(); 7159776f636SPeter Crosthwaite } 7169776f636SPeter Crosthwaite 7174df81c6eSPeter Crosthwaite cpu_set_pc(cs, entry); 71853018216SPaolo Bonzini } else { 719c8e829b7SGreg Bellows /* If we are booting Linux then we need to check whether we are 720c8e829b7SGreg Bellows * booting into secure or non-secure state and adjust the state 721c8e829b7SGreg Bellows * accordingly. Out of reset, ARM is defined to be in secure state 722c8e829b7SGreg Bellows * (SCR.NS = 0), we change that here if non-secure boot has been 723c8e829b7SGreg Bellows * requested. 724c8e829b7SGreg Bellows */ 7255097227cSGreg Bellows if (arm_feature(env, ARM_FEATURE_EL3)) { 7265097227cSGreg Bellows /* AArch64 is defined to come out of reset into EL3 if enabled. 7275097227cSGreg Bellows * If we are booting Linux then we need to adjust our EL as 7285097227cSGreg Bellows * Linux expects us to be in EL2 or EL1. AArch32 resets into 7295097227cSGreg Bellows * SVC, which Linux expects, so no privilege/exception level to 7305097227cSGreg Bellows * adjust. 7315097227cSGreg Bellows */ 7325097227cSGreg Bellows if (env->aarch64) { 73348d21a57SEdgar E. Iglesias env->cp15.scr_el3 |= SCR_RW; 7345097227cSGreg Bellows if (arm_feature(env, ARM_FEATURE_EL2)) { 73548d21a57SEdgar E. Iglesias env->cp15.hcr_el2 |= HCR_RW; 7365097227cSGreg Bellows env->pstate = PSTATE_MODE_EL2h; 7375097227cSGreg Bellows } else { 7385097227cSGreg Bellows env->pstate = PSTATE_MODE_EL1h; 7395097227cSGreg Bellows } 74024ac0d30SRichard Henderson if (cpu_isar_feature(aa64_pauth, cpu)) { 74124ac0d30SRichard Henderson env->cp15.scr_el3 |= SCR_API | SCR_APK; 74224ac0d30SRichard Henderson } 7437ad01d78SRichard Henderson if (cpu_isar_feature(aa64_mte, cpu)) { 7447ad01d78SRichard Henderson env->cp15.scr_el3 |= SCR_ATA; 7457ad01d78SRichard Henderson } 7463f0b5907SRémi Denis-Courmont if (cpu_isar_feature(aa64_sve, cpu)) { 7473f0b5907SRémi Denis-Courmont env->cp15.cptr_el[3] |= CPTR_EZ; 7483f0b5907SRémi Denis-Courmont } 74943118f43SPeter Maydell /* AArch64 kernels never boot in secure mode */ 75043118f43SPeter Maydell assert(!info->secure_boot); 75143118f43SPeter Maydell /* This hook is only supported for AArch32 currently: 75243118f43SPeter Maydell * bootloader_aarch64[] will not call the hook, and 75343118f43SPeter Maydell * the code above has already dropped us into EL2 or EL1. 75443118f43SPeter Maydell */ 75543118f43SPeter Maydell assert(!info->secure_board_setup); 7565097227cSGreg Bellows } 7575097227cSGreg Bellows 758bda816f0SPeter Maydell if (arm_feature(env, ARM_FEATURE_EL2)) { 759bda816f0SPeter Maydell /* If we have EL2 then Linux expects the HVC insn to work */ 760bda816f0SPeter Maydell env->cp15.scr_el3 |= SCR_HCE; 761bda816f0SPeter Maydell } 762bda816f0SPeter Maydell 7635097227cSGreg Bellows /* Set to non-secure if not a secure boot */ 764baf6b681SPeter Crosthwaite if (!info->secure_boot && 765baf6b681SPeter Crosthwaite (cs != first_cpu || !info->secure_board_setup)) { 7665097227cSGreg Bellows /* Linux expects non-secure state */ 767c8e829b7SGreg Bellows env->cp15.scr_el3 |= SCR_NS; 768ece628fcSPeter Maydell /* Set NSACR.{CP11,CP10} so NS can access the FPU */ 769ece628fcSPeter Maydell env->cp15.nsacr |= 3 << 10; 770c8e829b7SGreg Bellows } 7715097227cSGreg Bellows } 772c8e829b7SGreg Bellows 773299953b9SPeter Maydell if (!env->aarch64 && !info->secure_boot && 774299953b9SPeter Maydell arm_feature(env, ARM_FEATURE_EL2)) { 775299953b9SPeter Maydell /* 776299953b9SPeter Maydell * This is an AArch32 boot not to Secure state, and 777299953b9SPeter Maydell * we have Hyp mode available, so boot the kernel into 778299953b9SPeter Maydell * Hyp mode. This is not how the CPU comes out of reset, 779299953b9SPeter Maydell * so we need to manually put it there. 780299953b9SPeter Maydell */ 781299953b9SPeter Maydell cpsr_write(env, ARM_CPU_MODE_HYP, CPSR_M, CPSRWriteRaw); 782299953b9SPeter Maydell } 783299953b9SPeter Maydell 7844df81c6eSPeter Crosthwaite if (cs == first_cpu) { 7859f43d4c3SPeter Maydell AddressSpace *as = arm_boot_address_space(cpu, info); 7869f43d4c3SPeter Maydell 7874df81c6eSPeter Crosthwaite cpu_set_pc(cs, info->loader_start); 7884d9ebf75SMian M. Hamayun 78983bfffecSPeter Maydell if (!have_dtb(info)) { 79053018216SPaolo Bonzini if (old_param) { 7919f43d4c3SPeter Maydell set_kernel_args_old(info, as); 79253018216SPaolo Bonzini } else { 7939f43d4c3SPeter Maydell set_kernel_args(info, as); 79453018216SPaolo Bonzini } 79553018216SPaolo Bonzini } 79653018216SPaolo Bonzini } else { 79753018216SPaolo Bonzini info->secondary_cpu_reset_hook(cpu, info); 79853018216SPaolo Bonzini } 79953018216SPaolo Bonzini } 80098be6b7dSEdgar E. Iglesias arm_rebuild_hflags(env); 80153018216SPaolo Bonzini } 80253018216SPaolo Bonzini } 80353018216SPaolo Bonzini 80407abe45cSLaszlo Ersek /** 80507abe45cSLaszlo Ersek * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified 80607abe45cSLaszlo Ersek * by key. 80707abe45cSLaszlo Ersek * @fw_cfg: The firmware config instance to store the data in. 80807abe45cSLaszlo Ersek * @size_key: The firmware config key to store the size of the loaded 80907abe45cSLaszlo Ersek * data under, with fw_cfg_add_i32(). 81007abe45cSLaszlo Ersek * @data_key: The firmware config key to store the loaded data under, 81107abe45cSLaszlo Ersek * with fw_cfg_add_bytes(). 81207abe45cSLaszlo Ersek * @image_name: The name of the image file to load. If it is NULL, the 81307abe45cSLaszlo Ersek * function returns without doing anything. 81407abe45cSLaszlo Ersek * @try_decompress: Whether the image should be decompressed (gunzipped) before 81507abe45cSLaszlo Ersek * adding it to fw_cfg. If decompression fails, the image is 81607abe45cSLaszlo Ersek * loaded as-is. 81707abe45cSLaszlo Ersek * 81807abe45cSLaszlo Ersek * In case of failure, the function prints an error message to stderr and the 81907abe45cSLaszlo Ersek * process exits with status 1. 82007abe45cSLaszlo Ersek */ 82107abe45cSLaszlo Ersek static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key, 82207abe45cSLaszlo Ersek uint16_t data_key, const char *image_name, 82307abe45cSLaszlo Ersek bool try_decompress) 82407abe45cSLaszlo Ersek { 82507abe45cSLaszlo Ersek size_t size = -1; 82607abe45cSLaszlo Ersek uint8_t *data; 82707abe45cSLaszlo Ersek 82807abe45cSLaszlo Ersek if (image_name == NULL) { 82907abe45cSLaszlo Ersek return; 83007abe45cSLaszlo Ersek } 83107abe45cSLaszlo Ersek 83207abe45cSLaszlo Ersek if (try_decompress) { 83307abe45cSLaszlo Ersek size = load_image_gzipped_buffer(image_name, 83407abe45cSLaszlo Ersek LOAD_IMAGE_MAX_GUNZIP_BYTES, &data); 83507abe45cSLaszlo Ersek } 83607abe45cSLaszlo Ersek 83707abe45cSLaszlo Ersek if (size == (size_t)-1) { 83807abe45cSLaszlo Ersek gchar *contents; 83907abe45cSLaszlo Ersek gsize length; 84007abe45cSLaszlo Ersek 84107abe45cSLaszlo Ersek if (!g_file_get_contents(image_name, &contents, &length, NULL)) { 842c0dbca36SAlistair Francis error_report("failed to load \"%s\"", image_name); 84307abe45cSLaszlo Ersek exit(1); 84407abe45cSLaszlo Ersek } 84507abe45cSLaszlo Ersek size = length; 84607abe45cSLaszlo Ersek data = (uint8_t *)contents; 84707abe45cSLaszlo Ersek } 84807abe45cSLaszlo Ersek 84907abe45cSLaszlo Ersek fw_cfg_add_i32(fw_cfg, size_key, size); 85007abe45cSLaszlo Ersek fw_cfg_add_bytes(fw_cfg, data_key, data, size); 85107abe45cSLaszlo Ersek } 85207abe45cSLaszlo Ersek 853d8b1ae42SPeter Maydell static int do_arm_linux_init(Object *obj, void *opaque) 854d8b1ae42SPeter Maydell { 855d8b1ae42SPeter Maydell if (object_dynamic_cast(obj, TYPE_ARM_LINUX_BOOT_IF)) { 856d8b1ae42SPeter Maydell ARMLinuxBootIf *albif = ARM_LINUX_BOOT_IF(obj); 857d8b1ae42SPeter Maydell ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_GET_CLASS(obj); 858d8b1ae42SPeter Maydell struct arm_boot_info *info = opaque; 859d8b1ae42SPeter Maydell 860d8b1ae42SPeter Maydell if (albifc->arm_linux_init) { 861d8b1ae42SPeter Maydell albifc->arm_linux_init(albif, info->secure_boot); 862d8b1ae42SPeter Maydell } 863d8b1ae42SPeter Maydell } 864d8b1ae42SPeter Maydell return 0; 865d8b1ae42SPeter Maydell } 866d8b1ae42SPeter Maydell 867a3f0ecfdSAdam Lackorzynski static int64_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry, 8689776f636SPeter Crosthwaite uint64_t *lowaddr, uint64_t *highaddr, 8699f43d4c3SPeter Maydell int elf_machine, AddressSpace *as) 8709776f636SPeter Crosthwaite { 8719776f636SPeter Crosthwaite bool elf_is64; 8729776f636SPeter Crosthwaite union { 8739776f636SPeter Crosthwaite Elf32_Ehdr h32; 8749776f636SPeter Crosthwaite Elf64_Ehdr h64; 8759776f636SPeter Crosthwaite } elf_header; 8769776f636SPeter Crosthwaite int data_swab = 0; 8779776f636SPeter Crosthwaite bool big_endian; 878a3f0ecfdSAdam Lackorzynski int64_t ret = -1; 8799776f636SPeter Crosthwaite Error *err = NULL; 8809776f636SPeter Crosthwaite 8819776f636SPeter Crosthwaite 8829776f636SPeter Crosthwaite load_elf_hdr(info->kernel_filename, &elf_header, &elf_is64, &err); 8839776f636SPeter Crosthwaite if (err) { 88436f876ceSMarc-André Lureau error_free(err); 8859776f636SPeter Crosthwaite return ret; 8869776f636SPeter Crosthwaite } 8879776f636SPeter Crosthwaite 8889776f636SPeter Crosthwaite if (elf_is64) { 8899776f636SPeter Crosthwaite big_endian = elf_header.h64.e_ident[EI_DATA] == ELFDATA2MSB; 8909776f636SPeter Crosthwaite info->endianness = big_endian ? ARM_ENDIANNESS_BE8 8919776f636SPeter Crosthwaite : ARM_ENDIANNESS_LE; 8929776f636SPeter Crosthwaite } else { 8939776f636SPeter Crosthwaite big_endian = elf_header.h32.e_ident[EI_DATA] == ELFDATA2MSB; 8949776f636SPeter Crosthwaite if (big_endian) { 8959776f636SPeter Crosthwaite if (bswap32(elf_header.h32.e_flags) & EF_ARM_BE8) { 8969776f636SPeter Crosthwaite info->endianness = ARM_ENDIANNESS_BE8; 8979776f636SPeter Crosthwaite } else { 8989776f636SPeter Crosthwaite info->endianness = ARM_ENDIANNESS_BE32; 8999776f636SPeter Crosthwaite /* In BE32, the CPU has a different view of the per-byte 9009776f636SPeter Crosthwaite * address map than the rest of the system. BE32 ELF files 9019776f636SPeter Crosthwaite * are organised such that they can be programmed through 9029776f636SPeter Crosthwaite * the CPU's per-word byte-reversed view of the world. QEMU 9039776f636SPeter Crosthwaite * however loads ELF files independently of the CPU. So 9049776f636SPeter Crosthwaite * tell the ELF loader to byte reverse the data for us. 9059776f636SPeter Crosthwaite */ 9069776f636SPeter Crosthwaite data_swab = 2; 9079776f636SPeter Crosthwaite } 9089776f636SPeter Crosthwaite } else { 9099776f636SPeter Crosthwaite info->endianness = ARM_ENDIANNESS_LE; 9109776f636SPeter Crosthwaite } 9119776f636SPeter Crosthwaite } 9129776f636SPeter Crosthwaite 9134366e1dbSLiam Merwick ret = load_elf_as(info->kernel_filename, NULL, NULL, NULL, 9146cdda0ffSAleksandar Markovic pentry, lowaddr, highaddr, NULL, big_endian, elf_machine, 9159f43d4c3SPeter Maydell 1, data_swab, as); 9169776f636SPeter Crosthwaite if (ret <= 0) { 9179776f636SPeter Crosthwaite /* The header loaded but the image didn't */ 9189776f636SPeter Crosthwaite exit(1); 9199776f636SPeter Crosthwaite } 9209776f636SPeter Crosthwaite 9219776f636SPeter Crosthwaite return ret; 9229776f636SPeter Crosthwaite } 9239776f636SPeter Crosthwaite 92468115ed5SArd Biesheuvel static uint64_t load_aarch64_image(const char *filename, hwaddr mem_base, 9259f43d4c3SPeter Maydell hwaddr *entry, AddressSpace *as) 92668115ed5SArd Biesheuvel { 92768115ed5SArd Biesheuvel hwaddr kernel_load_offset = KERNEL64_LOAD_ADDR; 9285e6dbe1eSPeter Maydell uint64_t kernel_size = 0; 92968115ed5SArd Biesheuvel uint8_t *buffer; 93068115ed5SArd Biesheuvel int size; 93168115ed5SArd Biesheuvel 93268115ed5SArd Biesheuvel /* On aarch64, it's the bootloader's job to uncompress the kernel. */ 93368115ed5SArd Biesheuvel size = load_image_gzipped_buffer(filename, LOAD_IMAGE_MAX_GUNZIP_BYTES, 93468115ed5SArd Biesheuvel &buffer); 93568115ed5SArd Biesheuvel 93668115ed5SArd Biesheuvel if (size < 0) { 93768115ed5SArd Biesheuvel gsize len; 93868115ed5SArd Biesheuvel 93968115ed5SArd Biesheuvel /* Load as raw file otherwise */ 94068115ed5SArd Biesheuvel if (!g_file_get_contents(filename, (char **)&buffer, &len, NULL)) { 94168115ed5SArd Biesheuvel return -1; 94268115ed5SArd Biesheuvel } 94368115ed5SArd Biesheuvel size = len; 94468115ed5SArd Biesheuvel } 94568115ed5SArd Biesheuvel 94668115ed5SArd Biesheuvel /* check the arm64 magic header value -- very old kernels may not have it */ 94727640407SMarc-André Lureau if (size > ARM64_MAGIC_OFFSET + 4 && 94827640407SMarc-André Lureau memcmp(buffer + ARM64_MAGIC_OFFSET, "ARM\x64", 4) == 0) { 94968115ed5SArd Biesheuvel uint64_t hdrvals[2]; 95068115ed5SArd Biesheuvel 95168115ed5SArd Biesheuvel /* The arm64 Image header has text_offset and image_size fields at 8 and 95268115ed5SArd Biesheuvel * 16 bytes into the Image header, respectively. The text_offset field 95368115ed5SArd Biesheuvel * is only valid if the image_size is non-zero. 95468115ed5SArd Biesheuvel */ 95568115ed5SArd Biesheuvel memcpy(&hdrvals, buffer + ARM64_TEXT_OFFSET_OFFSET, sizeof(hdrvals)); 9565e6dbe1eSPeter Maydell 9575e6dbe1eSPeter Maydell kernel_size = le64_to_cpu(hdrvals[1]); 9585e6dbe1eSPeter Maydell 9595e6dbe1eSPeter Maydell if (kernel_size != 0) { 96068115ed5SArd Biesheuvel kernel_load_offset = le64_to_cpu(hdrvals[0]); 961ea358872SStewart Hildebrand 962ea358872SStewart Hildebrand /* 963ea358872SStewart Hildebrand * We write our startup "bootloader" at the very bottom of RAM, 964ea358872SStewart Hildebrand * so that bit can't be used for the image. Luckily the Image 965ea358872SStewart Hildebrand * format specification is that the image requests only an offset 966ea358872SStewart Hildebrand * from a 2MB boundary, not an absolute load address. So if the 967ea358872SStewart Hildebrand * image requests an offset that might mean it overlaps with the 968ea358872SStewart Hildebrand * bootloader, we can just load it starting at 2MB+offset rather 969ea358872SStewart Hildebrand * than 0MB + offset. 970ea358872SStewart Hildebrand */ 971ea358872SStewart Hildebrand if (kernel_load_offset < BOOTLOADER_MAX_SIZE) { 972ea358872SStewart Hildebrand kernel_load_offset += 2 * MiB; 973ea358872SStewart Hildebrand } 97468115ed5SArd Biesheuvel } 97568115ed5SArd Biesheuvel } 97668115ed5SArd Biesheuvel 9775e6dbe1eSPeter Maydell /* 9785e6dbe1eSPeter Maydell * Kernels before v3.17 don't populate the image_size field, and 9795e6dbe1eSPeter Maydell * raw images have no header. For those our best guess at the size 9805e6dbe1eSPeter Maydell * is the size of the Image file itself. 9815e6dbe1eSPeter Maydell */ 9825e6dbe1eSPeter Maydell if (kernel_size == 0) { 9835e6dbe1eSPeter Maydell kernel_size = size; 9845e6dbe1eSPeter Maydell } 9855e6dbe1eSPeter Maydell 98668115ed5SArd Biesheuvel *entry = mem_base + kernel_load_offset; 9879f43d4c3SPeter Maydell rom_add_blob_fixed_as(filename, buffer, size, *entry, as); 98868115ed5SArd Biesheuvel 98968115ed5SArd Biesheuvel g_free(buffer); 99068115ed5SArd Biesheuvel 9915e6dbe1eSPeter Maydell return kernel_size; 99268115ed5SArd Biesheuvel } 99368115ed5SArd Biesheuvel 994d33774eeSPeter Maydell static void arm_setup_direct_kernel_boot(ARMCPU *cpu, 995d33774eeSPeter Maydell struct arm_boot_info *info) 99653018216SPaolo Bonzini { 997d33774eeSPeter Maydell /* Set up for a direct boot of a kernel image file. */ 998c6faa758SArd Biesheuvel CPUState *cs; 999d33774eeSPeter Maydell AddressSpace *as = arm_boot_address_space(cpu, info); 100053018216SPaolo Bonzini int kernel_size; 100153018216SPaolo Bonzini int initrd_size; 100253018216SPaolo Bonzini int is_linux = 0; 1003d5fef92fSPeter Maydell uint64_t elf_entry; 1004d5fef92fSPeter Maydell /* Addresses of first byte used and first byte not used by the image */ 100567505c11SPeter Maydell uint64_t image_low_addr = 0, image_high_addr = 0; 1006da0af40dSPeter Maydell int elf_machine; 100768115ed5SArd Biesheuvel hwaddr entry; 10084d9ebf75SMian M. Hamayun static const ARMInsnFixup *primary_loader; 1009e70af24bSPeter Maydell uint64_t ram_end = info->loader_start + info->ram_size; 101053018216SPaolo Bonzini 10114d9ebf75SMian M. Hamayun if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { 10124d9ebf75SMian M. Hamayun primary_loader = bootloader_aarch64; 1013da0af40dSPeter Maydell elf_machine = EM_AARCH64; 10144d9ebf75SMian M. Hamayun } else { 10154d9ebf75SMian M. Hamayun primary_loader = bootloader; 101610b8ec73SPeter Crosthwaite if (!info->write_board_setup) { 101710b8ec73SPeter Crosthwaite primary_loader += BOOTLOADER_NO_BOARD_SETUP_OFFSET; 101810b8ec73SPeter Crosthwaite } 1019da0af40dSPeter Maydell elf_machine = EM_ARM; 10204d9ebf75SMian M. Hamayun } 10214d9ebf75SMian M. Hamayun 102253018216SPaolo Bonzini if (!info->secondary_cpu_reset_hook) { 102353018216SPaolo Bonzini info->secondary_cpu_reset_hook = default_reset_secondary; 102453018216SPaolo Bonzini } 102553018216SPaolo Bonzini if (!info->write_secondary_boot) { 102653018216SPaolo Bonzini info->write_secondary_boot = default_write_secondary; 102753018216SPaolo Bonzini } 102853018216SPaolo Bonzini 102953018216SPaolo Bonzini if (info->nb_cpus == 0) 103053018216SPaolo Bonzini info->nb_cpus = 1; 103153018216SPaolo Bonzini 103253018216SPaolo Bonzini /* Assume that raw images are linux kernels, and ELF images are not. */ 1033d5fef92fSPeter Maydell kernel_size = arm_load_elf(info, &elf_entry, &image_low_addr, 1034d5fef92fSPeter Maydell &image_high_addr, elf_machine, as); 103592df8450SArd Biesheuvel if (kernel_size > 0 && have_dtb(info)) { 1036c3a42358SPeter Maydell /* 1037c3a42358SPeter Maydell * If there is still some room left at the base of RAM, try and put 103892df8450SArd Biesheuvel * the DTB there like we do for images loaded with -bios or -pflash. 103992df8450SArd Biesheuvel */ 1040d5fef92fSPeter Maydell if (image_low_addr > info->loader_start 1041d5fef92fSPeter Maydell || image_high_addr < info->loader_start) { 1042c3a42358SPeter Maydell /* 1043d5fef92fSPeter Maydell * Set image_low_addr as address limit for arm_load_dtb if it may be 104492df8450SArd Biesheuvel * pointing into RAM, otherwise pass '0' (no limit) 104592df8450SArd Biesheuvel */ 1046d5fef92fSPeter Maydell if (image_low_addr < info->loader_start) { 1047d5fef92fSPeter Maydell image_low_addr = 0; 104892df8450SArd Biesheuvel } 10493b77f6c3SIgor Mammedov info->dtb_start = info->loader_start; 1050d5fef92fSPeter Maydell info->dtb_limit = image_low_addr; 105192df8450SArd Biesheuvel } 105292df8450SArd Biesheuvel } 105353018216SPaolo Bonzini entry = elf_entry; 105453018216SPaolo Bonzini if (kernel_size < 0) { 1055f831f955SNick Hudson uint64_t loadaddr = info->loader_start + KERNEL_NOLOAD_ADDR; 1056f831f955SNick Hudson kernel_size = load_uimage_as(info->kernel_filename, &entry, &loadaddr, 10579f43d4c3SPeter Maydell &is_linux, NULL, NULL, as); 105867505c11SPeter Maydell if (kernel_size >= 0) { 105967505c11SPeter Maydell image_low_addr = loadaddr; 106067505c11SPeter Maydell image_high_addr = image_low_addr + kernel_size; 106167505c11SPeter Maydell } 106253018216SPaolo Bonzini } 10636f5d3cbeSRichard W.M. Jones if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && kernel_size < 0) { 106468115ed5SArd Biesheuvel kernel_size = load_aarch64_image(info->kernel_filename, 10659f43d4c3SPeter Maydell info->loader_start, &entry, as); 10666f5d3cbeSRichard W.M. Jones is_linux = 1; 106767505c11SPeter Maydell if (kernel_size >= 0) { 106867505c11SPeter Maydell image_low_addr = entry; 106967505c11SPeter Maydell image_high_addr = image_low_addr + kernel_size; 107067505c11SPeter Maydell } 107168115ed5SArd Biesheuvel } else if (kernel_size < 0) { 107268115ed5SArd Biesheuvel /* 32-bit ARM */ 107368115ed5SArd Biesheuvel entry = info->loader_start + KERNEL_LOAD_ADDR; 10749f43d4c3SPeter Maydell kernel_size = load_image_targphys_as(info->kernel_filename, entry, 1075e70af24bSPeter Maydell ram_end - KERNEL_LOAD_ADDR, as); 107653018216SPaolo Bonzini is_linux = 1; 107767505c11SPeter Maydell if (kernel_size >= 0) { 107867505c11SPeter Maydell image_low_addr = entry; 107967505c11SPeter Maydell image_high_addr = image_low_addr + kernel_size; 108067505c11SPeter Maydell } 108153018216SPaolo Bonzini } 108253018216SPaolo Bonzini if (kernel_size < 0) { 1083c0dbca36SAlistair Francis error_report("could not load kernel '%s'", info->kernel_filename); 108453018216SPaolo Bonzini exit(1); 108553018216SPaolo Bonzini } 1086852dc64dSPeter Maydell 1087852dc64dSPeter Maydell if (kernel_size > info->ram_size) { 1088852dc64dSPeter Maydell error_report("kernel '%s' is too large to fit in RAM " 1089852dc64dSPeter Maydell "(kernel size %d, RAM size %" PRId64 ")", 1090852dc64dSPeter Maydell info->kernel_filename, kernel_size, info->ram_size); 1091852dc64dSPeter Maydell exit(1); 1092852dc64dSPeter Maydell } 1093852dc64dSPeter Maydell 109453018216SPaolo Bonzini info->entry = entry; 1095e6b2b20dSPeter Maydell 1096e6b2b20dSPeter Maydell /* 1097e6b2b20dSPeter Maydell * We want to put the initrd far enough into RAM that when the 1098e6b2b20dSPeter Maydell * kernel is uncompressed it will not clobber the initrd. However 1099e6b2b20dSPeter Maydell * on boards without much RAM we must ensure that we still leave 1100e6b2b20dSPeter Maydell * enough room for a decent sized initrd, and on boards with large 1101e6b2b20dSPeter Maydell * amounts of RAM we must avoid the initrd being so far up in RAM 1102e6b2b20dSPeter Maydell * that it is outside lowmem and inaccessible to the kernel. 1103e6b2b20dSPeter Maydell * So for boards with less than 256MB of RAM we put the initrd 1104e6b2b20dSPeter Maydell * halfway into RAM, and for boards with 256MB of RAM or more we put 1105e6b2b20dSPeter Maydell * the initrd at 128MB. 1106e6b2b20dSPeter Maydell * We also refuse to put the initrd somewhere that will definitely 1107e6b2b20dSPeter Maydell * overlay the kernel we just loaded, though for kernel formats which 1108e6b2b20dSPeter Maydell * don't tell us their exact size (eg self-decompressing 32-bit kernels) 1109e6b2b20dSPeter Maydell * we might still make a bad choice here. 1110e6b2b20dSPeter Maydell */ 1111e6b2b20dSPeter Maydell info->initrd_start = info->loader_start + 1112e4e34855SPhilippe Mathieu-Daudé MIN(info->ram_size / 2, 128 * MiB); 111367505c11SPeter Maydell if (image_high_addr) { 111467505c11SPeter Maydell info->initrd_start = MAX(info->initrd_start, image_high_addr); 111567505c11SPeter Maydell } 1116e6b2b20dSPeter Maydell info->initrd_start = TARGET_PAGE_ALIGN(info->initrd_start); 1117e6b2b20dSPeter Maydell 111853018216SPaolo Bonzini if (is_linux) { 111947b1da81SPeter Maydell uint32_t fixupcontext[FIXUP_MAX]; 112047b1da81SPeter Maydell 112153018216SPaolo Bonzini if (info->initrd_filename) { 1122852dc64dSPeter Maydell 1123852dc64dSPeter Maydell if (info->initrd_start >= ram_end) { 1124852dc64dSPeter Maydell error_report("not enough space after kernel to load initrd"); 1125852dc64dSPeter Maydell exit(1); 1126852dc64dSPeter Maydell } 1127852dc64dSPeter Maydell 11289f43d4c3SPeter Maydell initrd_size = load_ramdisk_as(info->initrd_filename, 1129fd76663eSSoren Brinkmann info->initrd_start, 1130e70af24bSPeter Maydell ram_end - info->initrd_start, as); 1131fd76663eSSoren Brinkmann if (initrd_size < 0) { 11329f43d4c3SPeter Maydell initrd_size = load_image_targphys_as(info->initrd_filename, 113353018216SPaolo Bonzini info->initrd_start, 1134e70af24bSPeter Maydell ram_end - 11359f43d4c3SPeter Maydell info->initrd_start, 11369f43d4c3SPeter Maydell as); 1137fd76663eSSoren Brinkmann } 113853018216SPaolo Bonzini if (initrd_size < 0) { 1139c0dbca36SAlistair Francis error_report("could not load initrd '%s'", 114053018216SPaolo Bonzini info->initrd_filename); 114153018216SPaolo Bonzini exit(1); 114253018216SPaolo Bonzini } 1143b48b0640SAndrew Jones if (info->initrd_start + initrd_size > ram_end) { 1144852dc64dSPeter Maydell error_report("could not load initrd '%s': " 1145852dc64dSPeter Maydell "too big to fit into RAM after the kernel", 1146852dc64dSPeter Maydell info->initrd_filename); 1147b48b0640SAndrew Jones exit(1); 1148852dc64dSPeter Maydell } 114953018216SPaolo Bonzini } else { 115053018216SPaolo Bonzini initrd_size = 0; 115153018216SPaolo Bonzini } 115253018216SPaolo Bonzini info->initrd_size = initrd_size; 115353018216SPaolo Bonzini 115447b1da81SPeter Maydell fixupcontext[FIXUP_BOARDID] = info->board_id; 115510b8ec73SPeter Crosthwaite fixupcontext[FIXUP_BOARD_SETUP] = info->board_setup_addr; 115653018216SPaolo Bonzini 1157c3a42358SPeter Maydell /* 1158c3a42358SPeter Maydell * for device tree boot, we pass the DTB directly in r2. Otherwise 115953018216SPaolo Bonzini * we point to the kernel args. 116053018216SPaolo Bonzini */ 116183bfffecSPeter Maydell if (have_dtb(info)) { 116276e2aef3SAlexander Graf hwaddr align; 116376e2aef3SAlexander Graf 116476e2aef3SAlexander Graf if (elf_machine == EM_AARCH64) { 116576e2aef3SAlexander Graf /* 116676e2aef3SAlexander Graf * Some AArch64 kernels on early bootup map the fdt region as 116776e2aef3SAlexander Graf * 116876e2aef3SAlexander Graf * [ ALIGN_DOWN(fdt, 2MB) ... ALIGN_DOWN(fdt, 2MB) + 2MB ] 116976e2aef3SAlexander Graf * 117076e2aef3SAlexander Graf * Let's play safe and prealign it to 2MB to give us some space. 117153018216SPaolo Bonzini */ 1172e4e34855SPhilippe Mathieu-Daudé align = 2 * MiB; 117376e2aef3SAlexander Graf } else { 117476e2aef3SAlexander Graf /* 117576e2aef3SAlexander Graf * Some 32bit kernels will trash anything in the 4K page the 117676e2aef3SAlexander Graf * initrd ends in, so make sure the DTB isn't caught up in that. 117776e2aef3SAlexander Graf */ 1178e4e34855SPhilippe Mathieu-Daudé align = 4 * KiB; 117976e2aef3SAlexander Graf } 118076e2aef3SAlexander Graf 118176e2aef3SAlexander Graf /* Place the DTB after the initrd in memory with alignment. */ 11823b77f6c3SIgor Mammedov info->dtb_start = QEMU_ALIGN_UP(info->initrd_start + initrd_size, 11833b77f6c3SIgor Mammedov align); 1184852dc64dSPeter Maydell if (info->dtb_start >= ram_end) { 1185852dc64dSPeter Maydell error_report("Not enough space for DTB after kernel/initrd"); 1186852dc64dSPeter Maydell exit(1); 1187852dc64dSPeter Maydell } 1188751ebc13SRicardo Perez Blanco fixupcontext[FIXUP_ARGPTR_LO] = info->dtb_start; 1189751ebc13SRicardo Perez Blanco fixupcontext[FIXUP_ARGPTR_HI] = info->dtb_start >> 32; 119053018216SPaolo Bonzini } else { 1191751ebc13SRicardo Perez Blanco fixupcontext[FIXUP_ARGPTR_LO] = 1192751ebc13SRicardo Perez Blanco info->loader_start + KERNEL_ARGS_ADDR; 1193751ebc13SRicardo Perez Blanco fixupcontext[FIXUP_ARGPTR_HI] = 1194751ebc13SRicardo Perez Blanco (info->loader_start + KERNEL_ARGS_ADDR) >> 32; 1195e4e34855SPhilippe Mathieu-Daudé if (info->ram_size >= 4 * GiB) { 1196c0dbca36SAlistair Francis error_report("RAM size must be less than 4GB to boot" 119753018216SPaolo Bonzini " Linux kernel using ATAGS (try passing a device tree" 1198c0dbca36SAlistair Francis " using -dtb)"); 119953018216SPaolo Bonzini exit(1); 120053018216SPaolo Bonzini } 120153018216SPaolo Bonzini } 1202751ebc13SRicardo Perez Blanco fixupcontext[FIXUP_ENTRYPOINT_LO] = entry; 1203751ebc13SRicardo Perez Blanco fixupcontext[FIXUP_ENTRYPOINT_HI] = entry >> 32; 120447b1da81SPeter Maydell 120547b1da81SPeter Maydell write_bootloader("bootloader", info->loader_start, 12069f43d4c3SPeter Maydell primary_loader, fixupcontext, as); 120747b1da81SPeter Maydell 120853018216SPaolo Bonzini if (info->nb_cpus > 1) { 120953018216SPaolo Bonzini info->write_secondary_boot(cpu, info); 121053018216SPaolo Bonzini } 121110b8ec73SPeter Crosthwaite if (info->write_board_setup) { 121210b8ec73SPeter Crosthwaite info->write_board_setup(cpu, info); 121310b8ec73SPeter Crosthwaite } 1214d8b1ae42SPeter Maydell 1215c3a42358SPeter Maydell /* 1216c3a42358SPeter Maydell * Notify devices which need to fake up firmware initialization 1217d8b1ae42SPeter Maydell * that we're doing a direct kernel boot. 1218d8b1ae42SPeter Maydell */ 1219d8b1ae42SPeter Maydell object_child_foreach_recursive(object_get_root(), 1220d8b1ae42SPeter Maydell do_arm_linux_init, info); 122153018216SPaolo Bonzini } 122253018216SPaolo Bonzini info->is_linux = is_linux; 122353018216SPaolo Bonzini 12240c949276SIgor Mammedov for (cs = first_cpu; cs; cs = CPU_NEXT(cs)) { 1225c6faa758SArd Biesheuvel ARM_CPU(cs)->env.boot_info = info; 122653018216SPaolo Bonzini } 1227d33774eeSPeter Maydell } 1228d33774eeSPeter Maydell 12294c0f2687SPeter Maydell static void arm_setup_firmware_boot(ARMCPU *cpu, struct arm_boot_info *info) 1230d33774eeSPeter Maydell { 12314c0f2687SPeter Maydell /* Set up for booting firmware (which might load a kernel via fw_cfg) */ 1232d33774eeSPeter Maydell 1233d33774eeSPeter Maydell if (have_dtb(info)) { 1234d33774eeSPeter Maydell /* 1235d33774eeSPeter Maydell * If we have a device tree blob, but no kernel to supply it to (or 1236d33774eeSPeter Maydell * the kernel is supposed to be loaded by the bootloader), copy the 1237d33774eeSPeter Maydell * DTB to the base of RAM for the bootloader to pick up. 1238d33774eeSPeter Maydell */ 1239d33774eeSPeter Maydell info->dtb_start = info->loader_start; 1240d33774eeSPeter Maydell } 1241d33774eeSPeter Maydell 1242d33774eeSPeter Maydell if (info->kernel_filename) { 1243d33774eeSPeter Maydell FWCfgState *fw_cfg; 1244d33774eeSPeter Maydell bool try_decompressing_kernel; 1245d33774eeSPeter Maydell 1246d33774eeSPeter Maydell fw_cfg = fw_cfg_find(); 1247d33774eeSPeter Maydell try_decompressing_kernel = arm_feature(&cpu->env, 1248d33774eeSPeter Maydell ARM_FEATURE_AARCH64); 1249d33774eeSPeter Maydell 1250d33774eeSPeter Maydell /* 1251d33774eeSPeter Maydell * Expose the kernel, the command line, and the initrd in fw_cfg. 1252d33774eeSPeter Maydell * We don't process them here at all, it's all left to the 1253d33774eeSPeter Maydell * firmware. 1254d33774eeSPeter Maydell */ 1255d33774eeSPeter Maydell load_image_to_fw_cfg(fw_cfg, 1256d33774eeSPeter Maydell FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA, 1257d33774eeSPeter Maydell info->kernel_filename, 1258d33774eeSPeter Maydell try_decompressing_kernel); 1259d33774eeSPeter Maydell load_image_to_fw_cfg(fw_cfg, 1260d33774eeSPeter Maydell FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA, 1261d33774eeSPeter Maydell info->initrd_filename, false); 1262d33774eeSPeter Maydell 1263d33774eeSPeter Maydell if (info->kernel_cmdline) { 1264d33774eeSPeter Maydell fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, 1265d33774eeSPeter Maydell strlen(info->kernel_cmdline) + 1); 1266d33774eeSPeter Maydell fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, 1267d33774eeSPeter Maydell info->kernel_cmdline); 1268d33774eeSPeter Maydell } 1269d33774eeSPeter Maydell } 1270d33774eeSPeter Maydell 1271d33774eeSPeter Maydell /* 1272d33774eeSPeter Maydell * We will start from address 0 (typically a boot ROM image) in the 12732a5bdfc8SPeter Maydell * same way as hardware. Leave env->boot_info NULL, so that 12742a5bdfc8SPeter Maydell * do_cpu_reset() knows it does not need to alter the PC on reset. 1275d33774eeSPeter Maydell */ 12764c0f2687SPeter Maydell } 12774c0f2687SPeter Maydell 12782744ece8STao Xu void arm_load_kernel(ARMCPU *cpu, MachineState *ms, struct arm_boot_info *info) 12794c0f2687SPeter Maydell { 12804c0f2687SPeter Maydell CPUState *cs; 12814c0f2687SPeter Maydell AddressSpace *as = arm_boot_address_space(cpu, info); 12824c0f2687SPeter Maydell 12834c0f2687SPeter Maydell /* 12844c0f2687SPeter Maydell * CPU objects (unlike devices) are not automatically reset on system 12854c0f2687SPeter Maydell * reset, so we must always register a handler to do so. If we're 12864c0f2687SPeter Maydell * actually loading a kernel, the handler is also responsible for 12874c0f2687SPeter Maydell * arranging that we start it correctly. 12884c0f2687SPeter Maydell */ 12894c0f2687SPeter Maydell for (cs = first_cpu; cs; cs = CPU_NEXT(cs)) { 12904c0f2687SPeter Maydell qemu_register_reset(do_cpu_reset, ARM_CPU(cs)); 12914c0f2687SPeter Maydell } 12924c0f2687SPeter Maydell 12934c0f2687SPeter Maydell /* 12944c0f2687SPeter Maydell * The board code is not supposed to set secure_board_setup unless 12954c0f2687SPeter Maydell * running its code in secure mode is actually possible, and KVM 12964c0f2687SPeter Maydell * doesn't support secure. 12974c0f2687SPeter Maydell */ 12984c0f2687SPeter Maydell assert(!(info->secure_board_setup && kvm_enabled())); 12992744ece8STao Xu info->kernel_filename = ms->kernel_filename; 13002744ece8STao Xu info->kernel_cmdline = ms->kernel_cmdline; 13012744ece8STao Xu info->initrd_filename = ms->initrd_filename; 13024c0f2687SPeter Maydell info->dtb_filename = qemu_opt_get(qemu_get_machine_opts(), "dtb"); 13034c0f2687SPeter Maydell info->dtb_limit = 0; 13044c0f2687SPeter Maydell 13054c0f2687SPeter Maydell /* Load the kernel. */ 13064c0f2687SPeter Maydell if (!info->kernel_filename || info->firmware_loaded) { 13074c0f2687SPeter Maydell arm_setup_firmware_boot(cpu, info); 1308d33774eeSPeter Maydell } else { 1309d33774eeSPeter Maydell arm_setup_direct_kernel_boot(cpu, info); 1310d33774eeSPeter Maydell } 131163a183edSEric Auger 13123b77f6c3SIgor Mammedov if (!info->skip_dtb_autoload && have_dtb(info)) { 13132744ece8STao Xu if (arm_load_dtb(info->dtb_start, info, info->dtb_limit, as, ms) < 0) { 13143b77f6c3SIgor Mammedov exit(1); 13153b77f6c3SIgor Mammedov } 13163b77f6c3SIgor Mammedov } 1317ac9d32e3SEric Auger } 1318d8b1ae42SPeter Maydell 1319d8b1ae42SPeter Maydell static const TypeInfo arm_linux_boot_if_info = { 1320d8b1ae42SPeter Maydell .name = TYPE_ARM_LINUX_BOOT_IF, 1321d8b1ae42SPeter Maydell .parent = TYPE_INTERFACE, 1322d8b1ae42SPeter Maydell .class_size = sizeof(ARMLinuxBootIfClass), 1323d8b1ae42SPeter Maydell }; 1324d8b1ae42SPeter Maydell 1325d8b1ae42SPeter Maydell static void arm_linux_boot_register_types(void) 1326d8b1ae42SPeter Maydell { 1327d8b1ae42SPeter Maydell type_register_static(&arm_linux_boot_if_info); 1328d8b1ae42SPeter Maydell } 1329d8b1ae42SPeter Maydell 1330d8b1ae42SPeter Maydell type_init(arm_linux_boot_register_types) 1331