153018216SPaolo Bonzini /* 253018216SPaolo Bonzini * ARM kernel loader. 353018216SPaolo Bonzini * 453018216SPaolo Bonzini * Copyright (c) 2006-2007 CodeSourcery. 553018216SPaolo Bonzini * Written by Paul Brook 653018216SPaolo Bonzini * 753018216SPaolo Bonzini * This code is licensed under the GPL. 853018216SPaolo Bonzini */ 953018216SPaolo Bonzini 1012b16722SPeter Maydell #include "qemu/osdep.h" 11c0dbca36SAlistair Francis #include "qemu/error-report.h" 12da34e65cSMarkus Armbruster #include "qapi/error.h" 13b77257d7SGuenter Roeck #include <libfdt.h> 1453018216SPaolo Bonzini #include "hw/hw.h" 15bd2be150SPeter Maydell #include "hw/arm/arm.h" 16d8b1ae42SPeter Maydell #include "hw/arm/linux-boot-if.h" 17baf6b681SPeter Crosthwaite #include "sysemu/kvm.h" 1853018216SPaolo Bonzini #include "sysemu/sysemu.h" 199695200aSShannon Zhao #include "sysemu/numa.h" 2053018216SPaolo Bonzini #include "hw/boards.h" 2153018216SPaolo Bonzini #include "hw/loader.h" 2253018216SPaolo Bonzini #include "elf.h" 2353018216SPaolo Bonzini #include "sysemu/device_tree.h" 2453018216SPaolo Bonzini #include "qemu/config-file.h" 25922a01a0SMarkus Armbruster #include "qemu/option.h" 262198a121SEdgar E. Iglesias #include "exec/address-spaces.h" 27ea358872SStewart Hildebrand #include "qemu/units.h" 2853018216SPaolo Bonzini 294d9ebf75SMian M. Hamayun /* Kernel boot protocol is specified in the kernel docs 304d9ebf75SMian M. Hamayun * Documentation/arm/Booting and Documentation/arm64/booting.txt 314d9ebf75SMian M. Hamayun * They have different preferred image load offsets from system RAM base. 324d9ebf75SMian M. Hamayun */ 3353018216SPaolo Bonzini #define KERNEL_ARGS_ADDR 0x100 34f831f955SNick Hudson #define KERNEL_NOLOAD_ADDR 0x02000000 3553018216SPaolo Bonzini #define KERNEL_LOAD_ADDR 0x00010000 364d9ebf75SMian M. Hamayun #define KERNEL64_LOAD_ADDR 0x00080000 3753018216SPaolo Bonzini 3868115ed5SArd Biesheuvel #define ARM64_TEXT_OFFSET_OFFSET 8 3968115ed5SArd Biesheuvel #define ARM64_MAGIC_OFFSET 56 4068115ed5SArd Biesheuvel 41ea358872SStewart Hildebrand #define BOOTLOADER_MAX_SIZE (4 * KiB) 42ea358872SStewart Hildebrand 433b77f6c3SIgor Mammedov AddressSpace *arm_boot_address_space(ARMCPU *cpu, 449f43d4c3SPeter Maydell const struct arm_boot_info *info) 459f43d4c3SPeter Maydell { 469f43d4c3SPeter Maydell /* Return the address space to use for bootloader reads and writes. 479f43d4c3SPeter Maydell * We prefer the secure address space if the CPU has it and we're 489f43d4c3SPeter Maydell * going to boot the guest into it. 499f43d4c3SPeter Maydell */ 509f43d4c3SPeter Maydell int asidx; 519f43d4c3SPeter Maydell CPUState *cs = CPU(cpu); 529f43d4c3SPeter Maydell 539f43d4c3SPeter Maydell if (arm_feature(&cpu->env, ARM_FEATURE_EL3) && info->secure_boot) { 549f43d4c3SPeter Maydell asidx = ARMASIdx_S; 559f43d4c3SPeter Maydell } else { 569f43d4c3SPeter Maydell asidx = ARMASIdx_NS; 579f43d4c3SPeter Maydell } 589f43d4c3SPeter Maydell 599f43d4c3SPeter Maydell return cpu_get_address_space(cs, asidx); 609f43d4c3SPeter Maydell } 619f43d4c3SPeter Maydell 6247b1da81SPeter Maydell typedef enum { 6347b1da81SPeter Maydell FIXUP_NONE = 0, /* do nothing */ 6447b1da81SPeter Maydell FIXUP_TERMINATOR, /* end of insns */ 6547b1da81SPeter Maydell FIXUP_BOARDID, /* overwrite with board ID number */ 6610b8ec73SPeter Crosthwaite FIXUP_BOARD_SETUP, /* overwrite with board specific setup code address */ 67751ebc13SRicardo Perez Blanco FIXUP_ARGPTR_LO, /* overwrite with pointer to kernel args */ 68751ebc13SRicardo Perez Blanco FIXUP_ARGPTR_HI, /* overwrite with pointer to kernel args (high half) */ 69751ebc13SRicardo Perez Blanco FIXUP_ENTRYPOINT_LO, /* overwrite with kernel entry point */ 70751ebc13SRicardo Perez Blanco FIXUP_ENTRYPOINT_HI, /* overwrite with kernel entry point (high half) */ 7147b1da81SPeter Maydell FIXUP_GIC_CPU_IF, /* overwrite with GIC CPU interface address */ 7247b1da81SPeter Maydell FIXUP_BOOTREG, /* overwrite with boot register address */ 7347b1da81SPeter Maydell FIXUP_DSB, /* overwrite with correct DSB insn for cpu */ 7447b1da81SPeter Maydell FIXUP_MAX, 7547b1da81SPeter Maydell } FixupType; 7647b1da81SPeter Maydell 7747b1da81SPeter Maydell typedef struct ARMInsnFixup { 7847b1da81SPeter Maydell uint32_t insn; 7947b1da81SPeter Maydell FixupType fixup; 8047b1da81SPeter Maydell } ARMInsnFixup; 8147b1da81SPeter Maydell 824d9ebf75SMian M. Hamayun static const ARMInsnFixup bootloader_aarch64[] = { 834d9ebf75SMian M. Hamayun { 0x580000c0 }, /* ldr x0, arg ; Load the lower 32-bits of DTB */ 844d9ebf75SMian M. Hamayun { 0xaa1f03e1 }, /* mov x1, xzr */ 854d9ebf75SMian M. Hamayun { 0xaa1f03e2 }, /* mov x2, xzr */ 864d9ebf75SMian M. Hamayun { 0xaa1f03e3 }, /* mov x3, xzr */ 874d9ebf75SMian M. Hamayun { 0x58000084 }, /* ldr x4, entry ; Load the lower 32-bits of kernel entry */ 884d9ebf75SMian M. Hamayun { 0xd61f0080 }, /* br x4 ; Jump to the kernel entry point */ 89751ebc13SRicardo Perez Blanco { 0, FIXUP_ARGPTR_LO }, /* arg: .word @DTB Lower 32-bits */ 90751ebc13SRicardo Perez Blanco { 0, FIXUP_ARGPTR_HI}, /* .word @DTB Higher 32-bits */ 91751ebc13SRicardo Perez Blanco { 0, FIXUP_ENTRYPOINT_LO }, /* entry: .word @Kernel Entry Lower 32-bits */ 92751ebc13SRicardo Perez Blanco { 0, FIXUP_ENTRYPOINT_HI }, /* .word @Kernel Entry Higher 32-bits */ 934d9ebf75SMian M. Hamayun { 0, FIXUP_TERMINATOR } 944d9ebf75SMian M. Hamayun }; 954d9ebf75SMian M. Hamayun 9610b8ec73SPeter Crosthwaite /* A very small bootloader: call the board-setup code (if needed), 9710b8ec73SPeter Crosthwaite * set r0-r2, then jump to the kernel. 9810b8ec73SPeter Crosthwaite * If we're not calling boot setup code then we don't copy across 9910b8ec73SPeter Crosthwaite * the first BOOTLOADER_NO_BOARD_SETUP_OFFSET insns in this array. 10010b8ec73SPeter Crosthwaite */ 10110b8ec73SPeter Crosthwaite 10247b1da81SPeter Maydell static const ARMInsnFixup bootloader[] = { 103b4850e5aSSylvain Garrigues { 0xe28fe004 }, /* add lr, pc, #4 */ 10410b8ec73SPeter Crosthwaite { 0xe51ff004 }, /* ldr pc, [pc, #-4] */ 10510b8ec73SPeter Crosthwaite { 0, FIXUP_BOARD_SETUP }, 10610b8ec73SPeter Crosthwaite #define BOOTLOADER_NO_BOARD_SETUP_OFFSET 3 10747b1da81SPeter Maydell { 0xe3a00000 }, /* mov r0, #0 */ 10847b1da81SPeter Maydell { 0xe59f1004 }, /* ldr r1, [pc, #4] */ 10947b1da81SPeter Maydell { 0xe59f2004 }, /* ldr r2, [pc, #4] */ 11047b1da81SPeter Maydell { 0xe59ff004 }, /* ldr pc, [pc, #4] */ 11147b1da81SPeter Maydell { 0, FIXUP_BOARDID }, 112751ebc13SRicardo Perez Blanco { 0, FIXUP_ARGPTR_LO }, 113751ebc13SRicardo Perez Blanco { 0, FIXUP_ENTRYPOINT_LO }, 11447b1da81SPeter Maydell { 0, FIXUP_TERMINATOR } 11553018216SPaolo Bonzini }; 11653018216SPaolo Bonzini 11753018216SPaolo Bonzini /* Handling for secondary CPU boot in a multicore system. 11853018216SPaolo Bonzini * Unlike the uniprocessor/primary CPU boot, this is platform 11953018216SPaolo Bonzini * dependent. The default code here is based on the secondary 12053018216SPaolo Bonzini * CPU boot protocol used on realview/vexpress boards, with 12153018216SPaolo Bonzini * some parameterisation to increase its flexibility. 12253018216SPaolo Bonzini * QEMU platform models for which this code is not appropriate 12353018216SPaolo Bonzini * should override write_secondary_boot and secondary_cpu_reset_hook 12453018216SPaolo Bonzini * instead. 12553018216SPaolo Bonzini * 12653018216SPaolo Bonzini * This code enables the interrupt controllers for the secondary 12753018216SPaolo Bonzini * CPUs and then puts all the secondary CPUs into a loop waiting 12853018216SPaolo Bonzini * for an interprocessor interrupt and polling a configurable 12953018216SPaolo Bonzini * location for the kernel secondary CPU entry point. 13053018216SPaolo Bonzini */ 13153018216SPaolo Bonzini #define DSB_INSN 0xf57ff04f 13253018216SPaolo Bonzini #define CP15_DSB_INSN 0xee070f9a /* mcr cp15, 0, r0, c7, c10, 4 */ 13353018216SPaolo Bonzini 13447b1da81SPeter Maydell static const ARMInsnFixup smpboot[] = { 13547b1da81SPeter Maydell { 0xe59f2028 }, /* ldr r2, gic_cpu_if */ 13647b1da81SPeter Maydell { 0xe59f0028 }, /* ldr r0, bootreg_addr */ 13747b1da81SPeter Maydell { 0xe3a01001 }, /* mov r1, #1 */ 13847b1da81SPeter Maydell { 0xe5821000 }, /* str r1, [r2] - set GICC_CTLR.Enable */ 13947b1da81SPeter Maydell { 0xe3a010ff }, /* mov r1, #0xff */ 14047b1da81SPeter Maydell { 0xe5821004 }, /* str r1, [r2, 4] - set GIC_PMR.Priority to 0xff */ 14147b1da81SPeter Maydell { 0, FIXUP_DSB }, /* dsb */ 14247b1da81SPeter Maydell { 0xe320f003 }, /* wfi */ 14347b1da81SPeter Maydell { 0xe5901000 }, /* ldr r1, [r0] */ 14447b1da81SPeter Maydell { 0xe1110001 }, /* tst r1, r1 */ 14547b1da81SPeter Maydell { 0x0afffffb }, /* beq <wfi> */ 14647b1da81SPeter Maydell { 0xe12fff11 }, /* bx r1 */ 14747b1da81SPeter Maydell { 0, FIXUP_GIC_CPU_IF }, /* gic_cpu_if: .word 0x.... */ 14847b1da81SPeter Maydell { 0, FIXUP_BOOTREG }, /* bootreg_addr: .word 0x.... */ 14947b1da81SPeter Maydell { 0, FIXUP_TERMINATOR } 15053018216SPaolo Bonzini }; 15153018216SPaolo Bonzini 15247b1da81SPeter Maydell static void write_bootloader(const char *name, hwaddr addr, 1539f43d4c3SPeter Maydell const ARMInsnFixup *insns, uint32_t *fixupcontext, 1549f43d4c3SPeter Maydell AddressSpace *as) 15547b1da81SPeter Maydell { 15647b1da81SPeter Maydell /* Fix up the specified bootloader fragment and write it into 15747b1da81SPeter Maydell * guest memory using rom_add_blob_fixed(). fixupcontext is 15847b1da81SPeter Maydell * an array giving the values to write in for the fixup types 15947b1da81SPeter Maydell * which write a value into the code array. 16047b1da81SPeter Maydell */ 16147b1da81SPeter Maydell int i, len; 16247b1da81SPeter Maydell uint32_t *code; 16347b1da81SPeter Maydell 16447b1da81SPeter Maydell len = 0; 16547b1da81SPeter Maydell while (insns[len].fixup != FIXUP_TERMINATOR) { 16647b1da81SPeter Maydell len++; 16747b1da81SPeter Maydell } 16847b1da81SPeter Maydell 16947b1da81SPeter Maydell code = g_new0(uint32_t, len); 17047b1da81SPeter Maydell 17147b1da81SPeter Maydell for (i = 0; i < len; i++) { 17247b1da81SPeter Maydell uint32_t insn = insns[i].insn; 17347b1da81SPeter Maydell FixupType fixup = insns[i].fixup; 17447b1da81SPeter Maydell 17547b1da81SPeter Maydell switch (fixup) { 17647b1da81SPeter Maydell case FIXUP_NONE: 17747b1da81SPeter Maydell break; 17847b1da81SPeter Maydell case FIXUP_BOARDID: 17910b8ec73SPeter Crosthwaite case FIXUP_BOARD_SETUP: 180751ebc13SRicardo Perez Blanco case FIXUP_ARGPTR_LO: 181751ebc13SRicardo Perez Blanco case FIXUP_ARGPTR_HI: 182751ebc13SRicardo Perez Blanco case FIXUP_ENTRYPOINT_LO: 183751ebc13SRicardo Perez Blanco case FIXUP_ENTRYPOINT_HI: 18447b1da81SPeter Maydell case FIXUP_GIC_CPU_IF: 18547b1da81SPeter Maydell case FIXUP_BOOTREG: 18647b1da81SPeter Maydell case FIXUP_DSB: 18747b1da81SPeter Maydell insn = fixupcontext[fixup]; 18847b1da81SPeter Maydell break; 18947b1da81SPeter Maydell default: 19047b1da81SPeter Maydell abort(); 19147b1da81SPeter Maydell } 19247b1da81SPeter Maydell code[i] = tswap32(insn); 19347b1da81SPeter Maydell } 19447b1da81SPeter Maydell 195ea358872SStewart Hildebrand assert((len * sizeof(uint32_t)) < BOOTLOADER_MAX_SIZE); 196ea358872SStewart Hildebrand 1979f43d4c3SPeter Maydell rom_add_blob_fixed_as(name, code, len * sizeof(uint32_t), addr, as); 19847b1da81SPeter Maydell 19947b1da81SPeter Maydell g_free(code); 20047b1da81SPeter Maydell } 20147b1da81SPeter Maydell 20253018216SPaolo Bonzini static void default_write_secondary(ARMCPU *cpu, 20353018216SPaolo Bonzini const struct arm_boot_info *info) 20453018216SPaolo Bonzini { 20547b1da81SPeter Maydell uint32_t fixupcontext[FIXUP_MAX]; 2069f43d4c3SPeter Maydell AddressSpace *as = arm_boot_address_space(cpu, info); 20747b1da81SPeter Maydell 20847b1da81SPeter Maydell fixupcontext[FIXUP_GIC_CPU_IF] = info->gic_cpu_if_addr; 20947b1da81SPeter Maydell fixupcontext[FIXUP_BOOTREG] = info->smp_bootreg_addr; 21047b1da81SPeter Maydell if (arm_feature(&cpu->env, ARM_FEATURE_V7)) { 21147b1da81SPeter Maydell fixupcontext[FIXUP_DSB] = DSB_INSN; 21247b1da81SPeter Maydell } else { 21347b1da81SPeter Maydell fixupcontext[FIXUP_DSB] = CP15_DSB_INSN; 21453018216SPaolo Bonzini } 21547b1da81SPeter Maydell 21647b1da81SPeter Maydell write_bootloader("smpboot", info->smp_loader_start, 2179f43d4c3SPeter Maydell smpboot, fixupcontext, as); 21853018216SPaolo Bonzini } 21953018216SPaolo Bonzini 220716536a9SAndrew Baumann void arm_write_secure_board_setup_dummy_smc(ARMCPU *cpu, 221716536a9SAndrew Baumann const struct arm_boot_info *info, 222716536a9SAndrew Baumann hwaddr mvbar_addr) 223716536a9SAndrew Baumann { 2249f43d4c3SPeter Maydell AddressSpace *as = arm_boot_address_space(cpu, info); 225716536a9SAndrew Baumann int n; 226716536a9SAndrew Baumann uint32_t mvbar_blob[] = { 227716536a9SAndrew Baumann /* mvbar_addr: secure monitor vectors 228716536a9SAndrew Baumann * Default unimplemented and unused vectors to spin. Makes it 229716536a9SAndrew Baumann * easier to debug (as opposed to the CPU running away). 230716536a9SAndrew Baumann */ 231716536a9SAndrew Baumann 0xeafffffe, /* (spin) */ 232716536a9SAndrew Baumann 0xeafffffe, /* (spin) */ 233716536a9SAndrew Baumann 0xe1b0f00e, /* movs pc, lr ;SMC exception return */ 234716536a9SAndrew Baumann 0xeafffffe, /* (spin) */ 235716536a9SAndrew Baumann 0xeafffffe, /* (spin) */ 236716536a9SAndrew Baumann 0xeafffffe, /* (spin) */ 237716536a9SAndrew Baumann 0xeafffffe, /* (spin) */ 238716536a9SAndrew Baumann 0xeafffffe, /* (spin) */ 239716536a9SAndrew Baumann }; 240716536a9SAndrew Baumann uint32_t board_setup_blob[] = { 241716536a9SAndrew Baumann /* board setup addr */ 242716536a9SAndrew Baumann 0xe3a00e00 + (mvbar_addr >> 4), /* mov r0, #mvbar_addr */ 243716536a9SAndrew Baumann 0xee0c0f30, /* mcr p15, 0, r0, c12, c0, 1 ;set MVBAR */ 244716536a9SAndrew Baumann 0xee110f11, /* mrc p15, 0, r0, c1 , c1, 0 ;read SCR */ 245716536a9SAndrew Baumann 0xe3800031, /* orr r0, #0x31 ;enable AW, FW, NS */ 246716536a9SAndrew Baumann 0xee010f11, /* mcr p15, 0, r0, c1, c1, 0 ;write SCR */ 247716536a9SAndrew Baumann 0xe1a0100e, /* mov r1, lr ;save LR across SMC */ 248716536a9SAndrew Baumann 0xe1600070, /* smc #0 ;call monitor to flush SCR */ 249716536a9SAndrew Baumann 0xe1a0f001, /* mov pc, r1 ;return */ 250716536a9SAndrew Baumann }; 251716536a9SAndrew Baumann 252716536a9SAndrew Baumann /* check that mvbar_addr is correctly aligned and relocatable (using MOV) */ 253716536a9SAndrew Baumann assert((mvbar_addr & 0x1f) == 0 && (mvbar_addr >> 4) < 0x100); 254716536a9SAndrew Baumann 255716536a9SAndrew Baumann /* check that these blobs don't overlap */ 256716536a9SAndrew Baumann assert((mvbar_addr + sizeof(mvbar_blob) <= info->board_setup_addr) 257716536a9SAndrew Baumann || (info->board_setup_addr + sizeof(board_setup_blob) <= mvbar_addr)); 258716536a9SAndrew Baumann 259716536a9SAndrew Baumann for (n = 0; n < ARRAY_SIZE(mvbar_blob); n++) { 260716536a9SAndrew Baumann mvbar_blob[n] = tswap32(mvbar_blob[n]); 261716536a9SAndrew Baumann } 2629f43d4c3SPeter Maydell rom_add_blob_fixed_as("board-setup-mvbar", mvbar_blob, sizeof(mvbar_blob), 2639f43d4c3SPeter Maydell mvbar_addr, as); 264716536a9SAndrew Baumann 265716536a9SAndrew Baumann for (n = 0; n < ARRAY_SIZE(board_setup_blob); n++) { 266716536a9SAndrew Baumann board_setup_blob[n] = tswap32(board_setup_blob[n]); 267716536a9SAndrew Baumann } 2689f43d4c3SPeter Maydell rom_add_blob_fixed_as("board-setup", board_setup_blob, 2699f43d4c3SPeter Maydell sizeof(board_setup_blob), info->board_setup_addr, as); 270716536a9SAndrew Baumann } 271716536a9SAndrew Baumann 27253018216SPaolo Bonzini static void default_reset_secondary(ARMCPU *cpu, 27353018216SPaolo Bonzini const struct arm_boot_info *info) 27453018216SPaolo Bonzini { 2759f43d4c3SPeter Maydell AddressSpace *as = arm_boot_address_space(cpu, info); 2764df81c6eSPeter Crosthwaite CPUState *cs = CPU(cpu); 27753018216SPaolo Bonzini 2789f43d4c3SPeter Maydell address_space_stl_notdirty(as, info->smp_bootreg_addr, 27942874d3aSPeter Maydell 0, MEMTXATTRS_UNSPECIFIED, NULL); 2804df81c6eSPeter Crosthwaite cpu_set_pc(cs, info->smp_loader_start); 28153018216SPaolo Bonzini } 28253018216SPaolo Bonzini 28383bfffecSPeter Maydell static inline bool have_dtb(const struct arm_boot_info *info) 28483bfffecSPeter Maydell { 28583bfffecSPeter Maydell return info->dtb_filename || info->get_dtb; 28683bfffecSPeter Maydell } 28783bfffecSPeter Maydell 28853018216SPaolo Bonzini #define WRITE_WORD(p, value) do { \ 2899f43d4c3SPeter Maydell address_space_stl_notdirty(as, p, value, \ 29042874d3aSPeter Maydell MEMTXATTRS_UNSPECIFIED, NULL); \ 29153018216SPaolo Bonzini p += 4; \ 29253018216SPaolo Bonzini } while (0) 29353018216SPaolo Bonzini 2949f43d4c3SPeter Maydell static void set_kernel_args(const struct arm_boot_info *info, AddressSpace *as) 29553018216SPaolo Bonzini { 29653018216SPaolo Bonzini int initrd_size = info->initrd_size; 29753018216SPaolo Bonzini hwaddr base = info->loader_start; 29853018216SPaolo Bonzini hwaddr p; 29953018216SPaolo Bonzini 30053018216SPaolo Bonzini p = base + KERNEL_ARGS_ADDR; 30153018216SPaolo Bonzini /* ATAG_CORE */ 30253018216SPaolo Bonzini WRITE_WORD(p, 5); 30353018216SPaolo Bonzini WRITE_WORD(p, 0x54410001); 30453018216SPaolo Bonzini WRITE_WORD(p, 1); 30553018216SPaolo Bonzini WRITE_WORD(p, 0x1000); 30653018216SPaolo Bonzini WRITE_WORD(p, 0); 30753018216SPaolo Bonzini /* ATAG_MEM */ 30853018216SPaolo Bonzini /* TODO: handle multiple chips on one ATAG list */ 30953018216SPaolo Bonzini WRITE_WORD(p, 4); 31053018216SPaolo Bonzini WRITE_WORD(p, 0x54410002); 31153018216SPaolo Bonzini WRITE_WORD(p, info->ram_size); 31253018216SPaolo Bonzini WRITE_WORD(p, info->loader_start); 31353018216SPaolo Bonzini if (initrd_size) { 31453018216SPaolo Bonzini /* ATAG_INITRD2 */ 31553018216SPaolo Bonzini WRITE_WORD(p, 4); 31653018216SPaolo Bonzini WRITE_WORD(p, 0x54420005); 31753018216SPaolo Bonzini WRITE_WORD(p, info->initrd_start); 31853018216SPaolo Bonzini WRITE_WORD(p, initrd_size); 31953018216SPaolo Bonzini } 32053018216SPaolo Bonzini if (info->kernel_cmdline && *info->kernel_cmdline) { 32153018216SPaolo Bonzini /* ATAG_CMDLINE */ 32253018216SPaolo Bonzini int cmdline_size; 32353018216SPaolo Bonzini 32453018216SPaolo Bonzini cmdline_size = strlen(info->kernel_cmdline); 3259f43d4c3SPeter Maydell address_space_write(as, p + 8, MEMTXATTRS_UNSPECIFIED, 3269f43d4c3SPeter Maydell (const uint8_t *)info->kernel_cmdline, 32753018216SPaolo Bonzini cmdline_size + 1); 32853018216SPaolo Bonzini cmdline_size = (cmdline_size >> 2) + 1; 32953018216SPaolo Bonzini WRITE_WORD(p, cmdline_size + 2); 33053018216SPaolo Bonzini WRITE_WORD(p, 0x54410009); 33153018216SPaolo Bonzini p += cmdline_size * 4; 33253018216SPaolo Bonzini } 33353018216SPaolo Bonzini if (info->atag_board) { 33453018216SPaolo Bonzini /* ATAG_BOARD */ 33553018216SPaolo Bonzini int atag_board_len; 33653018216SPaolo Bonzini uint8_t atag_board_buf[0x1000]; 33753018216SPaolo Bonzini 33853018216SPaolo Bonzini atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3; 33953018216SPaolo Bonzini WRITE_WORD(p, (atag_board_len + 8) >> 2); 34053018216SPaolo Bonzini WRITE_WORD(p, 0x414f4d50); 3419f43d4c3SPeter Maydell address_space_write(as, p, MEMTXATTRS_UNSPECIFIED, 3429f43d4c3SPeter Maydell atag_board_buf, atag_board_len); 34353018216SPaolo Bonzini p += atag_board_len; 34453018216SPaolo Bonzini } 34553018216SPaolo Bonzini /* ATAG_END */ 34653018216SPaolo Bonzini WRITE_WORD(p, 0); 34753018216SPaolo Bonzini WRITE_WORD(p, 0); 34853018216SPaolo Bonzini } 34953018216SPaolo Bonzini 3509f43d4c3SPeter Maydell static void set_kernel_args_old(const struct arm_boot_info *info, 3519f43d4c3SPeter Maydell AddressSpace *as) 35253018216SPaolo Bonzini { 35353018216SPaolo Bonzini hwaddr p; 35453018216SPaolo Bonzini const char *s; 35553018216SPaolo Bonzini int initrd_size = info->initrd_size; 35653018216SPaolo Bonzini hwaddr base = info->loader_start; 35753018216SPaolo Bonzini 35853018216SPaolo Bonzini /* see linux/include/asm-arm/setup.h */ 35953018216SPaolo Bonzini p = base + KERNEL_ARGS_ADDR; 36053018216SPaolo Bonzini /* page_size */ 36153018216SPaolo Bonzini WRITE_WORD(p, 4096); 36253018216SPaolo Bonzini /* nr_pages */ 36353018216SPaolo Bonzini WRITE_WORD(p, info->ram_size / 4096); 36453018216SPaolo Bonzini /* ramdisk_size */ 36553018216SPaolo Bonzini WRITE_WORD(p, 0); 36653018216SPaolo Bonzini #define FLAG_READONLY 1 36753018216SPaolo Bonzini #define FLAG_RDLOAD 4 36853018216SPaolo Bonzini #define FLAG_RDPROMPT 8 36953018216SPaolo Bonzini /* flags */ 37053018216SPaolo Bonzini WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT); 37153018216SPaolo Bonzini /* rootdev */ 37253018216SPaolo Bonzini WRITE_WORD(p, (31 << 8) | 0); /* /dev/mtdblock0 */ 37353018216SPaolo Bonzini /* video_num_cols */ 37453018216SPaolo Bonzini WRITE_WORD(p, 0); 37553018216SPaolo Bonzini /* video_num_rows */ 37653018216SPaolo Bonzini WRITE_WORD(p, 0); 37753018216SPaolo Bonzini /* video_x */ 37853018216SPaolo Bonzini WRITE_WORD(p, 0); 37953018216SPaolo Bonzini /* video_y */ 38053018216SPaolo Bonzini WRITE_WORD(p, 0); 38153018216SPaolo Bonzini /* memc_control_reg */ 38253018216SPaolo Bonzini WRITE_WORD(p, 0); 38353018216SPaolo Bonzini /* unsigned char sounddefault */ 38453018216SPaolo Bonzini /* unsigned char adfsdrives */ 38553018216SPaolo Bonzini /* unsigned char bytes_per_char_h */ 38653018216SPaolo Bonzini /* unsigned char bytes_per_char_v */ 38753018216SPaolo Bonzini WRITE_WORD(p, 0); 38853018216SPaolo Bonzini /* pages_in_bank[4] */ 38953018216SPaolo Bonzini WRITE_WORD(p, 0); 39053018216SPaolo Bonzini WRITE_WORD(p, 0); 39153018216SPaolo Bonzini WRITE_WORD(p, 0); 39253018216SPaolo Bonzini WRITE_WORD(p, 0); 39353018216SPaolo Bonzini /* pages_in_vram */ 39453018216SPaolo Bonzini WRITE_WORD(p, 0); 39553018216SPaolo Bonzini /* initrd_start */ 39653018216SPaolo Bonzini if (initrd_size) { 39753018216SPaolo Bonzini WRITE_WORD(p, info->initrd_start); 39853018216SPaolo Bonzini } else { 39953018216SPaolo Bonzini WRITE_WORD(p, 0); 40053018216SPaolo Bonzini } 40153018216SPaolo Bonzini /* initrd_size */ 40253018216SPaolo Bonzini WRITE_WORD(p, initrd_size); 40353018216SPaolo Bonzini /* rd_start */ 40453018216SPaolo Bonzini WRITE_WORD(p, 0); 40553018216SPaolo Bonzini /* system_rev */ 40653018216SPaolo Bonzini WRITE_WORD(p, 0); 40753018216SPaolo Bonzini /* system_serial_low */ 40853018216SPaolo Bonzini WRITE_WORD(p, 0); 40953018216SPaolo Bonzini /* system_serial_high */ 41053018216SPaolo Bonzini WRITE_WORD(p, 0); 41153018216SPaolo Bonzini /* mem_fclk_21285 */ 41253018216SPaolo Bonzini WRITE_WORD(p, 0); 41353018216SPaolo Bonzini /* zero unused fields */ 41453018216SPaolo Bonzini while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) { 41553018216SPaolo Bonzini WRITE_WORD(p, 0); 41653018216SPaolo Bonzini } 41753018216SPaolo Bonzini s = info->kernel_cmdline; 41853018216SPaolo Bonzini if (s) { 4199f43d4c3SPeter Maydell address_space_write(as, p, MEMTXATTRS_UNSPECIFIED, 4209f43d4c3SPeter Maydell (const uint8_t *)s, strlen(s) + 1); 42153018216SPaolo Bonzini } else { 42253018216SPaolo Bonzini WRITE_WORD(p, 0); 42353018216SPaolo Bonzini } 42453018216SPaolo Bonzini } 42553018216SPaolo Bonzini 4264cbca7d9SAndrey Smirnov static void fdt_add_psci_node(void *fdt) 4274cbca7d9SAndrey Smirnov { 4284cbca7d9SAndrey Smirnov uint32_t cpu_suspend_fn; 4294cbca7d9SAndrey Smirnov uint32_t cpu_off_fn; 4304cbca7d9SAndrey Smirnov uint32_t cpu_on_fn; 4314cbca7d9SAndrey Smirnov uint32_t migrate_fn; 4324cbca7d9SAndrey Smirnov ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(0)); 4334cbca7d9SAndrey Smirnov const char *psci_method; 4344cbca7d9SAndrey Smirnov int64_t psci_conduit; 435c39770cdSAndrey Smirnov int rc; 4364cbca7d9SAndrey Smirnov 4374cbca7d9SAndrey Smirnov psci_conduit = object_property_get_int(OBJECT(armcpu), 4384cbca7d9SAndrey Smirnov "psci-conduit", 4394cbca7d9SAndrey Smirnov &error_abort); 4404cbca7d9SAndrey Smirnov switch (psci_conduit) { 4414cbca7d9SAndrey Smirnov case QEMU_PSCI_CONDUIT_DISABLED: 4424cbca7d9SAndrey Smirnov return; 4434cbca7d9SAndrey Smirnov case QEMU_PSCI_CONDUIT_HVC: 4444cbca7d9SAndrey Smirnov psci_method = "hvc"; 4454cbca7d9SAndrey Smirnov break; 4464cbca7d9SAndrey Smirnov case QEMU_PSCI_CONDUIT_SMC: 4474cbca7d9SAndrey Smirnov psci_method = "smc"; 4484cbca7d9SAndrey Smirnov break; 4494cbca7d9SAndrey Smirnov default: 4504cbca7d9SAndrey Smirnov g_assert_not_reached(); 4514cbca7d9SAndrey Smirnov } 4524cbca7d9SAndrey Smirnov 453c39770cdSAndrey Smirnov /* 454c39770cdSAndrey Smirnov * If /psci node is present in provided DTB, assume that no fixup 455c39770cdSAndrey Smirnov * is necessary and all PSCI configuration should be taken as-is 456c39770cdSAndrey Smirnov */ 457c39770cdSAndrey Smirnov rc = fdt_path_offset(fdt, "/psci"); 458c39770cdSAndrey Smirnov if (rc >= 0) { 459c39770cdSAndrey Smirnov return; 460c39770cdSAndrey Smirnov } 461c39770cdSAndrey Smirnov 4624cbca7d9SAndrey Smirnov qemu_fdt_add_subnode(fdt, "/psci"); 4634cbca7d9SAndrey Smirnov if (armcpu->psci_version == 2) { 4644cbca7d9SAndrey Smirnov const char comp[] = "arm,psci-0.2\0arm,psci"; 4654cbca7d9SAndrey Smirnov qemu_fdt_setprop(fdt, "/psci", "compatible", comp, sizeof(comp)); 4664cbca7d9SAndrey Smirnov 4674cbca7d9SAndrey Smirnov cpu_off_fn = QEMU_PSCI_0_2_FN_CPU_OFF; 4684cbca7d9SAndrey Smirnov if (arm_feature(&armcpu->env, ARM_FEATURE_AARCH64)) { 4694cbca7d9SAndrey Smirnov cpu_suspend_fn = QEMU_PSCI_0_2_FN64_CPU_SUSPEND; 4704cbca7d9SAndrey Smirnov cpu_on_fn = QEMU_PSCI_0_2_FN64_CPU_ON; 4714cbca7d9SAndrey Smirnov migrate_fn = QEMU_PSCI_0_2_FN64_MIGRATE; 4724cbca7d9SAndrey Smirnov } else { 4734cbca7d9SAndrey Smirnov cpu_suspend_fn = QEMU_PSCI_0_2_FN_CPU_SUSPEND; 4744cbca7d9SAndrey Smirnov cpu_on_fn = QEMU_PSCI_0_2_FN_CPU_ON; 4754cbca7d9SAndrey Smirnov migrate_fn = QEMU_PSCI_0_2_FN_MIGRATE; 4764cbca7d9SAndrey Smirnov } 4774cbca7d9SAndrey Smirnov } else { 4784cbca7d9SAndrey Smirnov qemu_fdt_setprop_string(fdt, "/psci", "compatible", "arm,psci"); 4794cbca7d9SAndrey Smirnov 4804cbca7d9SAndrey Smirnov cpu_suspend_fn = QEMU_PSCI_0_1_FN_CPU_SUSPEND; 4814cbca7d9SAndrey Smirnov cpu_off_fn = QEMU_PSCI_0_1_FN_CPU_OFF; 4824cbca7d9SAndrey Smirnov cpu_on_fn = QEMU_PSCI_0_1_FN_CPU_ON; 4834cbca7d9SAndrey Smirnov migrate_fn = QEMU_PSCI_0_1_FN_MIGRATE; 4844cbca7d9SAndrey Smirnov } 4854cbca7d9SAndrey Smirnov 4864cbca7d9SAndrey Smirnov /* We adopt the PSCI spec's nomenclature, and use 'conduit' to refer 4874cbca7d9SAndrey Smirnov * to the instruction that should be used to invoke PSCI functions. 4884cbca7d9SAndrey Smirnov * However, the device tree binding uses 'method' instead, so that is 4894cbca7d9SAndrey Smirnov * what we should use here. 4904cbca7d9SAndrey Smirnov */ 4914cbca7d9SAndrey Smirnov qemu_fdt_setprop_string(fdt, "/psci", "method", psci_method); 4924cbca7d9SAndrey Smirnov 4934cbca7d9SAndrey Smirnov qemu_fdt_setprop_cell(fdt, "/psci", "cpu_suspend", cpu_suspend_fn); 4944cbca7d9SAndrey Smirnov qemu_fdt_setprop_cell(fdt, "/psci", "cpu_off", cpu_off_fn); 4954cbca7d9SAndrey Smirnov qemu_fdt_setprop_cell(fdt, "/psci", "cpu_on", cpu_on_fn); 4964cbca7d9SAndrey Smirnov qemu_fdt_setprop_cell(fdt, "/psci", "migrate", migrate_fn); 4974cbca7d9SAndrey Smirnov } 4984cbca7d9SAndrey Smirnov 4993b77f6c3SIgor Mammedov int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo, 5009f43d4c3SPeter Maydell hwaddr addr_limit, AddressSpace *as) 50153018216SPaolo Bonzini { 50253018216SPaolo Bonzini void *fdt = NULL; 503e2eb3d29SEric Auger int size, rc, n = 0; 50470976c41SPeter Maydell uint32_t acells, scells; 5059695200aSShannon Zhao char *nodename; 5069695200aSShannon Zhao unsigned int i; 5079695200aSShannon Zhao hwaddr mem_base, mem_len; 508e2eb3d29SEric Auger char **node_path; 509e2eb3d29SEric Auger Error *err = NULL; 51053018216SPaolo Bonzini 5110fb79851SJohn Rigby if (binfo->dtb_filename) { 5120fb79851SJohn Rigby char *filename; 51353018216SPaolo Bonzini filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename); 51453018216SPaolo Bonzini if (!filename) { 51553018216SPaolo Bonzini fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename); 516c23045deSPeter Maydell goto fail; 51753018216SPaolo Bonzini } 51853018216SPaolo Bonzini 51953018216SPaolo Bonzini fdt = load_device_tree(filename, &size); 52053018216SPaolo Bonzini if (!fdt) { 52153018216SPaolo Bonzini fprintf(stderr, "Couldn't open dtb file %s\n", filename); 52253018216SPaolo Bonzini g_free(filename); 523c23045deSPeter Maydell goto fail; 52453018216SPaolo Bonzini } 52553018216SPaolo Bonzini g_free(filename); 526a554ecb4Szhanghailiang } else { 5270fb79851SJohn Rigby fdt = binfo->get_dtb(binfo, &size); 5280fb79851SJohn Rigby if (!fdt) { 5290fb79851SJohn Rigby fprintf(stderr, "Board was unable to create a dtb blob\n"); 5300fb79851SJohn Rigby goto fail; 5310fb79851SJohn Rigby } 5320fb79851SJohn Rigby } 53353018216SPaolo Bonzini 534fee8ea12SArd Biesheuvel if (addr_limit > addr && size > (addr_limit - addr)) { 535fee8ea12SArd Biesheuvel /* Installing the device tree blob at addr would exceed addr_limit. 536fee8ea12SArd Biesheuvel * Whether this constitutes failure is up to the caller to decide, 537fee8ea12SArd Biesheuvel * so just return 0 as size, i.e., no error. 538fee8ea12SArd Biesheuvel */ 539fee8ea12SArd Biesheuvel g_free(fdt); 540fee8ea12SArd Biesheuvel return 0; 541fee8ea12SArd Biesheuvel } 542fee8ea12SArd Biesheuvel 54358e71097SEric Auger acells = qemu_fdt_getprop_cell(fdt, "/", "#address-cells", 54458e71097SEric Auger NULL, &error_fatal); 54558e71097SEric Auger scells = qemu_fdt_getprop_cell(fdt, "/", "#size-cells", 54658e71097SEric Auger NULL, &error_fatal); 54753018216SPaolo Bonzini if (acells == 0 || scells == 0) { 54853018216SPaolo Bonzini fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n"); 549c23045deSPeter Maydell goto fail; 55053018216SPaolo Bonzini } 55153018216SPaolo Bonzini 55270976c41SPeter Maydell if (scells < 2 && binfo->ram_size >= (1ULL << 32)) { 55370976c41SPeter Maydell /* This is user error so deserves a friendlier error message 55470976c41SPeter Maydell * than the failure of setprop_sized_cells would provide 55570976c41SPeter Maydell */ 55653018216SPaolo Bonzini fprintf(stderr, "qemu: dtb file not compatible with " 55753018216SPaolo Bonzini "RAM size > 4GB\n"); 558c23045deSPeter Maydell goto fail; 55953018216SPaolo Bonzini } 56053018216SPaolo Bonzini 561e2eb3d29SEric Auger /* nop all root nodes matching /memory or /memory@unit-address */ 562e2eb3d29SEric Auger node_path = qemu_fdt_node_unit_path(fdt, "memory", &err); 563e2eb3d29SEric Auger if (err) { 564e2eb3d29SEric Auger error_report_err(err); 565e2eb3d29SEric Auger goto fail; 566e2eb3d29SEric Auger } 567e2eb3d29SEric Auger while (node_path[n]) { 568e2eb3d29SEric Auger if (g_str_has_prefix(node_path[n], "/memory")) { 569e2eb3d29SEric Auger qemu_fdt_nop_node(fdt, node_path[n]); 570e2eb3d29SEric Auger } 571e2eb3d29SEric Auger n++; 572e2eb3d29SEric Auger } 573e2eb3d29SEric Auger g_strfreev(node_path); 574e2eb3d29SEric Auger 5759695200aSShannon Zhao if (nb_numa_nodes > 0) { 5769695200aSShannon Zhao mem_base = binfo->loader_start; 5779695200aSShannon Zhao for (i = 0; i < nb_numa_nodes; i++) { 5789695200aSShannon Zhao mem_len = numa_info[i].node_mem; 5799695200aSShannon Zhao nodename = g_strdup_printf("/memory@%" PRIx64, mem_base); 5809695200aSShannon Zhao qemu_fdt_add_subnode(fdt, nodename); 5819695200aSShannon Zhao qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory"); 5829695200aSShannon Zhao rc = qemu_fdt_setprop_sized_cells(fdt, nodename, "reg", 5839695200aSShannon Zhao acells, mem_base, 5849695200aSShannon Zhao scells, mem_len); 5859695200aSShannon Zhao if (rc < 0) { 5869695200aSShannon Zhao fprintf(stderr, "couldn't set %s/reg for node %d\n", nodename, 5879695200aSShannon Zhao i); 5889695200aSShannon Zhao goto fail; 5899695200aSShannon Zhao } 5909695200aSShannon Zhao 5919695200aSShannon Zhao qemu_fdt_setprop_cell(fdt, nodename, "numa-node-id", i); 5929695200aSShannon Zhao mem_base += mem_len; 5939695200aSShannon Zhao g_free(nodename); 5949695200aSShannon Zhao } 5959695200aSShannon Zhao } else { 596e2eb3d29SEric Auger nodename = g_strdup_printf("/memory@%" PRIx64, binfo->loader_start); 597e2eb3d29SEric Auger qemu_fdt_add_subnode(fdt, nodename); 598e2eb3d29SEric Auger qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory"); 599b77257d7SGuenter Roeck 600e2eb3d29SEric Auger rc = qemu_fdt_setprop_sized_cells(fdt, nodename, "reg", 60170976c41SPeter Maydell acells, binfo->loader_start, 60270976c41SPeter Maydell scells, binfo->ram_size); 60353018216SPaolo Bonzini if (rc < 0) { 604e2eb3d29SEric Auger fprintf(stderr, "couldn't set %s reg\n", nodename); 605c23045deSPeter Maydell goto fail; 60653018216SPaolo Bonzini } 607e2eb3d29SEric Auger g_free(nodename); 6089695200aSShannon Zhao } 60953018216SPaolo Bonzini 610b77257d7SGuenter Roeck rc = fdt_path_offset(fdt, "/chosen"); 611b77257d7SGuenter Roeck if (rc < 0) { 612b77257d7SGuenter Roeck qemu_fdt_add_subnode(fdt, "/chosen"); 613b77257d7SGuenter Roeck } 614b77257d7SGuenter Roeck 61553018216SPaolo Bonzini if (binfo->kernel_cmdline && *binfo->kernel_cmdline) { 6165a4348d1SPeter Crosthwaite rc = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", 61753018216SPaolo Bonzini binfo->kernel_cmdline); 61853018216SPaolo Bonzini if (rc < 0) { 61953018216SPaolo Bonzini fprintf(stderr, "couldn't set /chosen/bootargs\n"); 620c23045deSPeter Maydell goto fail; 62153018216SPaolo Bonzini } 62253018216SPaolo Bonzini } 62353018216SPaolo Bonzini 62453018216SPaolo Bonzini if (binfo->initrd_size) { 6255a4348d1SPeter Crosthwaite rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start", 62653018216SPaolo Bonzini binfo->initrd_start); 62753018216SPaolo Bonzini if (rc < 0) { 62853018216SPaolo Bonzini fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n"); 629c23045deSPeter Maydell goto fail; 63053018216SPaolo Bonzini } 63153018216SPaolo Bonzini 6325a4348d1SPeter Crosthwaite rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end", 63353018216SPaolo Bonzini binfo->initrd_start + binfo->initrd_size); 63453018216SPaolo Bonzini if (rc < 0) { 63553018216SPaolo Bonzini fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n"); 636c23045deSPeter Maydell goto fail; 63753018216SPaolo Bonzini } 63853018216SPaolo Bonzini } 6393b1cceb8SPeter Maydell 6404cbca7d9SAndrey Smirnov fdt_add_psci_node(fdt); 6414cbca7d9SAndrey Smirnov 6423b1cceb8SPeter Maydell if (binfo->modify_dtb) { 6433b1cceb8SPeter Maydell binfo->modify_dtb(binfo, fdt); 6443b1cceb8SPeter Maydell } 6453b1cceb8SPeter Maydell 6465a4348d1SPeter Crosthwaite qemu_fdt_dumpdtb(fdt, size); 64753018216SPaolo Bonzini 6484c4bf654SArd Biesheuvel /* Put the DTB into the memory map as a ROM image: this will ensure 6494c4bf654SArd Biesheuvel * the DTB is copied again upon reset, even if addr points into RAM. 6504c4bf654SArd Biesheuvel */ 6519f43d4c3SPeter Maydell rom_add_blob_fixed_as("dtb", fdt, size, addr, as); 65253018216SPaolo Bonzini 653c23045deSPeter Maydell g_free(fdt); 654c23045deSPeter Maydell 655fee8ea12SArd Biesheuvel return size; 656c23045deSPeter Maydell 657c23045deSPeter Maydell fail: 658c23045deSPeter Maydell g_free(fdt); 659c23045deSPeter Maydell return -1; 66053018216SPaolo Bonzini } 66153018216SPaolo Bonzini 66253018216SPaolo Bonzini static void do_cpu_reset(void *opaque) 66353018216SPaolo Bonzini { 66453018216SPaolo Bonzini ARMCPU *cpu = opaque; 6654df81c6eSPeter Crosthwaite CPUState *cs = CPU(cpu); 66653018216SPaolo Bonzini CPUARMState *env = &cpu->env; 66753018216SPaolo Bonzini const struct arm_boot_info *info = env->boot_info; 66853018216SPaolo Bonzini 6694df81c6eSPeter Crosthwaite cpu_reset(cs); 67053018216SPaolo Bonzini if (info) { 67153018216SPaolo Bonzini if (!info->is_linux) { 6729776f636SPeter Crosthwaite int i; 67353018216SPaolo Bonzini /* Jump to the entry point. */ 6744df81c6eSPeter Crosthwaite uint64_t entry = info->entry; 6754df81c6eSPeter Crosthwaite 6769776f636SPeter Crosthwaite switch (info->endianness) { 6779776f636SPeter Crosthwaite case ARM_ENDIANNESS_LE: 6789776f636SPeter Crosthwaite env->cp15.sctlr_el[1] &= ~SCTLR_E0E; 6799776f636SPeter Crosthwaite for (i = 1; i < 4; ++i) { 6809776f636SPeter Crosthwaite env->cp15.sctlr_el[i] &= ~SCTLR_EE; 6819776f636SPeter Crosthwaite } 6829776f636SPeter Crosthwaite env->uncached_cpsr &= ~CPSR_E; 6839776f636SPeter Crosthwaite break; 6849776f636SPeter Crosthwaite case ARM_ENDIANNESS_BE8: 6859776f636SPeter Crosthwaite env->cp15.sctlr_el[1] |= SCTLR_E0E; 6869776f636SPeter Crosthwaite for (i = 1; i < 4; ++i) { 6879776f636SPeter Crosthwaite env->cp15.sctlr_el[i] |= SCTLR_EE; 6889776f636SPeter Crosthwaite } 6899776f636SPeter Crosthwaite env->uncached_cpsr |= CPSR_E; 6909776f636SPeter Crosthwaite break; 6919776f636SPeter Crosthwaite case ARM_ENDIANNESS_BE32: 6929776f636SPeter Crosthwaite env->cp15.sctlr_el[1] |= SCTLR_B; 6939776f636SPeter Crosthwaite break; 6949776f636SPeter Crosthwaite case ARM_ENDIANNESS_UNKNOWN: 6959776f636SPeter Crosthwaite break; /* Board's decision */ 6969776f636SPeter Crosthwaite default: 6979776f636SPeter Crosthwaite g_assert_not_reached(); 6989776f636SPeter Crosthwaite } 6999776f636SPeter Crosthwaite 7004df81c6eSPeter Crosthwaite cpu_set_pc(cs, entry); 70153018216SPaolo Bonzini } else { 702c8e829b7SGreg Bellows /* If we are booting Linux then we need to check whether we are 703c8e829b7SGreg Bellows * booting into secure or non-secure state and adjust the state 704c8e829b7SGreg Bellows * accordingly. Out of reset, ARM is defined to be in secure state 705c8e829b7SGreg Bellows * (SCR.NS = 0), we change that here if non-secure boot has been 706c8e829b7SGreg Bellows * requested. 707c8e829b7SGreg Bellows */ 7085097227cSGreg Bellows if (arm_feature(env, ARM_FEATURE_EL3)) { 7095097227cSGreg Bellows /* AArch64 is defined to come out of reset into EL3 if enabled. 7105097227cSGreg Bellows * If we are booting Linux then we need to adjust our EL as 7115097227cSGreg Bellows * Linux expects us to be in EL2 or EL1. AArch32 resets into 7125097227cSGreg Bellows * SVC, which Linux expects, so no privilege/exception level to 7135097227cSGreg Bellows * adjust. 7145097227cSGreg Bellows */ 7155097227cSGreg Bellows if (env->aarch64) { 71648d21a57SEdgar E. Iglesias env->cp15.scr_el3 |= SCR_RW; 7175097227cSGreg Bellows if (arm_feature(env, ARM_FEATURE_EL2)) { 71848d21a57SEdgar E. Iglesias env->cp15.hcr_el2 |= HCR_RW; 7195097227cSGreg Bellows env->pstate = PSTATE_MODE_EL2h; 7205097227cSGreg Bellows } else { 7215097227cSGreg Bellows env->pstate = PSTATE_MODE_EL1h; 7225097227cSGreg Bellows } 72343118f43SPeter Maydell /* AArch64 kernels never boot in secure mode */ 72443118f43SPeter Maydell assert(!info->secure_boot); 72543118f43SPeter Maydell /* This hook is only supported for AArch32 currently: 72643118f43SPeter Maydell * bootloader_aarch64[] will not call the hook, and 72743118f43SPeter Maydell * the code above has already dropped us into EL2 or EL1. 72843118f43SPeter Maydell */ 72943118f43SPeter Maydell assert(!info->secure_board_setup); 7305097227cSGreg Bellows } 7315097227cSGreg Bellows 732bda816f0SPeter Maydell if (arm_feature(env, ARM_FEATURE_EL2)) { 733bda816f0SPeter Maydell /* If we have EL2 then Linux expects the HVC insn to work */ 734bda816f0SPeter Maydell env->cp15.scr_el3 |= SCR_HCE; 735bda816f0SPeter Maydell } 736bda816f0SPeter Maydell 7375097227cSGreg Bellows /* Set to non-secure if not a secure boot */ 738baf6b681SPeter Crosthwaite if (!info->secure_boot && 739baf6b681SPeter Crosthwaite (cs != first_cpu || !info->secure_board_setup)) { 7405097227cSGreg Bellows /* Linux expects non-secure state */ 741c8e829b7SGreg Bellows env->cp15.scr_el3 |= SCR_NS; 742c8e829b7SGreg Bellows } 7435097227cSGreg Bellows } 744c8e829b7SGreg Bellows 745299953b9SPeter Maydell if (!env->aarch64 && !info->secure_boot && 746299953b9SPeter Maydell arm_feature(env, ARM_FEATURE_EL2)) { 747299953b9SPeter Maydell /* 748299953b9SPeter Maydell * This is an AArch32 boot not to Secure state, and 749299953b9SPeter Maydell * we have Hyp mode available, so boot the kernel into 750299953b9SPeter Maydell * Hyp mode. This is not how the CPU comes out of reset, 751299953b9SPeter Maydell * so we need to manually put it there. 752299953b9SPeter Maydell */ 753299953b9SPeter Maydell cpsr_write(env, ARM_CPU_MODE_HYP, CPSR_M, CPSRWriteRaw); 754299953b9SPeter Maydell } 755299953b9SPeter Maydell 7564df81c6eSPeter Crosthwaite if (cs == first_cpu) { 7579f43d4c3SPeter Maydell AddressSpace *as = arm_boot_address_space(cpu, info); 7589f43d4c3SPeter Maydell 7594df81c6eSPeter Crosthwaite cpu_set_pc(cs, info->loader_start); 7604d9ebf75SMian M. Hamayun 76183bfffecSPeter Maydell if (!have_dtb(info)) { 76253018216SPaolo Bonzini if (old_param) { 7639f43d4c3SPeter Maydell set_kernel_args_old(info, as); 76453018216SPaolo Bonzini } else { 7659f43d4c3SPeter Maydell set_kernel_args(info, as); 76653018216SPaolo Bonzini } 76753018216SPaolo Bonzini } 76853018216SPaolo Bonzini } else { 76953018216SPaolo Bonzini info->secondary_cpu_reset_hook(cpu, info); 77053018216SPaolo Bonzini } 77153018216SPaolo Bonzini } 77253018216SPaolo Bonzini } 77353018216SPaolo Bonzini } 77453018216SPaolo Bonzini 77507abe45cSLaszlo Ersek /** 77607abe45cSLaszlo Ersek * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified 77707abe45cSLaszlo Ersek * by key. 77807abe45cSLaszlo Ersek * @fw_cfg: The firmware config instance to store the data in. 77907abe45cSLaszlo Ersek * @size_key: The firmware config key to store the size of the loaded 78007abe45cSLaszlo Ersek * data under, with fw_cfg_add_i32(). 78107abe45cSLaszlo Ersek * @data_key: The firmware config key to store the loaded data under, 78207abe45cSLaszlo Ersek * with fw_cfg_add_bytes(). 78307abe45cSLaszlo Ersek * @image_name: The name of the image file to load. If it is NULL, the 78407abe45cSLaszlo Ersek * function returns without doing anything. 78507abe45cSLaszlo Ersek * @try_decompress: Whether the image should be decompressed (gunzipped) before 78607abe45cSLaszlo Ersek * adding it to fw_cfg. If decompression fails, the image is 78707abe45cSLaszlo Ersek * loaded as-is. 78807abe45cSLaszlo Ersek * 78907abe45cSLaszlo Ersek * In case of failure, the function prints an error message to stderr and the 79007abe45cSLaszlo Ersek * process exits with status 1. 79107abe45cSLaszlo Ersek */ 79207abe45cSLaszlo Ersek static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key, 79307abe45cSLaszlo Ersek uint16_t data_key, const char *image_name, 79407abe45cSLaszlo Ersek bool try_decompress) 79507abe45cSLaszlo Ersek { 79607abe45cSLaszlo Ersek size_t size = -1; 79707abe45cSLaszlo Ersek uint8_t *data; 79807abe45cSLaszlo Ersek 79907abe45cSLaszlo Ersek if (image_name == NULL) { 80007abe45cSLaszlo Ersek return; 80107abe45cSLaszlo Ersek } 80207abe45cSLaszlo Ersek 80307abe45cSLaszlo Ersek if (try_decompress) { 80407abe45cSLaszlo Ersek size = load_image_gzipped_buffer(image_name, 80507abe45cSLaszlo Ersek LOAD_IMAGE_MAX_GUNZIP_BYTES, &data); 80607abe45cSLaszlo Ersek } 80707abe45cSLaszlo Ersek 80807abe45cSLaszlo Ersek if (size == (size_t)-1) { 80907abe45cSLaszlo Ersek gchar *contents; 81007abe45cSLaszlo Ersek gsize length; 81107abe45cSLaszlo Ersek 81207abe45cSLaszlo Ersek if (!g_file_get_contents(image_name, &contents, &length, NULL)) { 813c0dbca36SAlistair Francis error_report("failed to load \"%s\"", image_name); 81407abe45cSLaszlo Ersek exit(1); 81507abe45cSLaszlo Ersek } 81607abe45cSLaszlo Ersek size = length; 81707abe45cSLaszlo Ersek data = (uint8_t *)contents; 81807abe45cSLaszlo Ersek } 81907abe45cSLaszlo Ersek 82007abe45cSLaszlo Ersek fw_cfg_add_i32(fw_cfg, size_key, size); 82107abe45cSLaszlo Ersek fw_cfg_add_bytes(fw_cfg, data_key, data, size); 82207abe45cSLaszlo Ersek } 82307abe45cSLaszlo Ersek 824d8b1ae42SPeter Maydell static int do_arm_linux_init(Object *obj, void *opaque) 825d8b1ae42SPeter Maydell { 826d8b1ae42SPeter Maydell if (object_dynamic_cast(obj, TYPE_ARM_LINUX_BOOT_IF)) { 827d8b1ae42SPeter Maydell ARMLinuxBootIf *albif = ARM_LINUX_BOOT_IF(obj); 828d8b1ae42SPeter Maydell ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_GET_CLASS(obj); 829d8b1ae42SPeter Maydell struct arm_boot_info *info = opaque; 830d8b1ae42SPeter Maydell 831d8b1ae42SPeter Maydell if (albifc->arm_linux_init) { 832d8b1ae42SPeter Maydell albifc->arm_linux_init(albif, info->secure_boot); 833d8b1ae42SPeter Maydell } 834d8b1ae42SPeter Maydell } 835d8b1ae42SPeter Maydell return 0; 836d8b1ae42SPeter Maydell } 837d8b1ae42SPeter Maydell 838a3f0ecfdSAdam Lackorzynski static int64_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry, 8399776f636SPeter Crosthwaite uint64_t *lowaddr, uint64_t *highaddr, 8409f43d4c3SPeter Maydell int elf_machine, AddressSpace *as) 8419776f636SPeter Crosthwaite { 8429776f636SPeter Crosthwaite bool elf_is64; 8439776f636SPeter Crosthwaite union { 8449776f636SPeter Crosthwaite Elf32_Ehdr h32; 8459776f636SPeter Crosthwaite Elf64_Ehdr h64; 8469776f636SPeter Crosthwaite } elf_header; 8479776f636SPeter Crosthwaite int data_swab = 0; 8489776f636SPeter Crosthwaite bool big_endian; 849a3f0ecfdSAdam Lackorzynski int64_t ret = -1; 8509776f636SPeter Crosthwaite Error *err = NULL; 8519776f636SPeter Crosthwaite 8529776f636SPeter Crosthwaite 8539776f636SPeter Crosthwaite load_elf_hdr(info->kernel_filename, &elf_header, &elf_is64, &err); 8549776f636SPeter Crosthwaite if (err) { 85536f876ceSMarc-André Lureau error_free(err); 8569776f636SPeter Crosthwaite return ret; 8579776f636SPeter Crosthwaite } 8589776f636SPeter Crosthwaite 8599776f636SPeter Crosthwaite if (elf_is64) { 8609776f636SPeter Crosthwaite big_endian = elf_header.h64.e_ident[EI_DATA] == ELFDATA2MSB; 8619776f636SPeter Crosthwaite info->endianness = big_endian ? ARM_ENDIANNESS_BE8 8629776f636SPeter Crosthwaite : ARM_ENDIANNESS_LE; 8639776f636SPeter Crosthwaite } else { 8649776f636SPeter Crosthwaite big_endian = elf_header.h32.e_ident[EI_DATA] == ELFDATA2MSB; 8659776f636SPeter Crosthwaite if (big_endian) { 8669776f636SPeter Crosthwaite if (bswap32(elf_header.h32.e_flags) & EF_ARM_BE8) { 8679776f636SPeter Crosthwaite info->endianness = ARM_ENDIANNESS_BE8; 8689776f636SPeter Crosthwaite } else { 8699776f636SPeter Crosthwaite info->endianness = ARM_ENDIANNESS_BE32; 8709776f636SPeter Crosthwaite /* In BE32, the CPU has a different view of the per-byte 8719776f636SPeter Crosthwaite * address map than the rest of the system. BE32 ELF files 8729776f636SPeter Crosthwaite * are organised such that they can be programmed through 8739776f636SPeter Crosthwaite * the CPU's per-word byte-reversed view of the world. QEMU 8749776f636SPeter Crosthwaite * however loads ELF files independently of the CPU. So 8759776f636SPeter Crosthwaite * tell the ELF loader to byte reverse the data for us. 8769776f636SPeter Crosthwaite */ 8779776f636SPeter Crosthwaite data_swab = 2; 8789776f636SPeter Crosthwaite } 8799776f636SPeter Crosthwaite } else { 8809776f636SPeter Crosthwaite info->endianness = ARM_ENDIANNESS_LE; 8819776f636SPeter Crosthwaite } 8829776f636SPeter Crosthwaite } 8839776f636SPeter Crosthwaite 8849f43d4c3SPeter Maydell ret = load_elf_as(info->kernel_filename, NULL, NULL, 8859776f636SPeter Crosthwaite pentry, lowaddr, highaddr, big_endian, elf_machine, 8869f43d4c3SPeter Maydell 1, data_swab, as); 8879776f636SPeter Crosthwaite if (ret <= 0) { 8889776f636SPeter Crosthwaite /* The header loaded but the image didn't */ 8899776f636SPeter Crosthwaite exit(1); 8909776f636SPeter Crosthwaite } 8919776f636SPeter Crosthwaite 8929776f636SPeter Crosthwaite return ret; 8939776f636SPeter Crosthwaite } 8949776f636SPeter Crosthwaite 89568115ed5SArd Biesheuvel static uint64_t load_aarch64_image(const char *filename, hwaddr mem_base, 8969f43d4c3SPeter Maydell hwaddr *entry, AddressSpace *as) 89768115ed5SArd Biesheuvel { 89868115ed5SArd Biesheuvel hwaddr kernel_load_offset = KERNEL64_LOAD_ADDR; 89968115ed5SArd Biesheuvel uint8_t *buffer; 90068115ed5SArd Biesheuvel int size; 90168115ed5SArd Biesheuvel 90268115ed5SArd Biesheuvel /* On aarch64, it's the bootloader's job to uncompress the kernel. */ 90368115ed5SArd Biesheuvel size = load_image_gzipped_buffer(filename, LOAD_IMAGE_MAX_GUNZIP_BYTES, 90468115ed5SArd Biesheuvel &buffer); 90568115ed5SArd Biesheuvel 90668115ed5SArd Biesheuvel if (size < 0) { 90768115ed5SArd Biesheuvel gsize len; 90868115ed5SArd Biesheuvel 90968115ed5SArd Biesheuvel /* Load as raw file otherwise */ 91068115ed5SArd Biesheuvel if (!g_file_get_contents(filename, (char **)&buffer, &len, NULL)) { 91168115ed5SArd Biesheuvel return -1; 91268115ed5SArd Biesheuvel } 91368115ed5SArd Biesheuvel size = len; 91468115ed5SArd Biesheuvel } 91568115ed5SArd Biesheuvel 91668115ed5SArd Biesheuvel /* check the arm64 magic header value -- very old kernels may not have it */ 91727640407SMarc-André Lureau if (size > ARM64_MAGIC_OFFSET + 4 && 91827640407SMarc-André Lureau memcmp(buffer + ARM64_MAGIC_OFFSET, "ARM\x64", 4) == 0) { 91968115ed5SArd Biesheuvel uint64_t hdrvals[2]; 92068115ed5SArd Biesheuvel 92168115ed5SArd Biesheuvel /* The arm64 Image header has text_offset and image_size fields at 8 and 92268115ed5SArd Biesheuvel * 16 bytes into the Image header, respectively. The text_offset field 92368115ed5SArd Biesheuvel * is only valid if the image_size is non-zero. 92468115ed5SArd Biesheuvel */ 92568115ed5SArd Biesheuvel memcpy(&hdrvals, buffer + ARM64_TEXT_OFFSET_OFFSET, sizeof(hdrvals)); 92668115ed5SArd Biesheuvel if (hdrvals[1] != 0) { 92768115ed5SArd Biesheuvel kernel_load_offset = le64_to_cpu(hdrvals[0]); 928ea358872SStewart Hildebrand 929ea358872SStewart Hildebrand /* 930ea358872SStewart Hildebrand * We write our startup "bootloader" at the very bottom of RAM, 931ea358872SStewart Hildebrand * so that bit can't be used for the image. Luckily the Image 932ea358872SStewart Hildebrand * format specification is that the image requests only an offset 933ea358872SStewart Hildebrand * from a 2MB boundary, not an absolute load address. So if the 934ea358872SStewart Hildebrand * image requests an offset that might mean it overlaps with the 935ea358872SStewart Hildebrand * bootloader, we can just load it starting at 2MB+offset rather 936ea358872SStewart Hildebrand * than 0MB + offset. 937ea358872SStewart Hildebrand */ 938ea358872SStewart Hildebrand if (kernel_load_offset < BOOTLOADER_MAX_SIZE) { 939ea358872SStewart Hildebrand kernel_load_offset += 2 * MiB; 940ea358872SStewart Hildebrand } 94168115ed5SArd Biesheuvel } 94268115ed5SArd Biesheuvel } 94368115ed5SArd Biesheuvel 94468115ed5SArd Biesheuvel *entry = mem_base + kernel_load_offset; 9459f43d4c3SPeter Maydell rom_add_blob_fixed_as(filename, buffer, size, *entry, as); 94668115ed5SArd Biesheuvel 94768115ed5SArd Biesheuvel g_free(buffer); 94868115ed5SArd Biesheuvel 94968115ed5SArd Biesheuvel return size; 95068115ed5SArd Biesheuvel } 95168115ed5SArd Biesheuvel 9523b77f6c3SIgor Mammedov void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info) 95353018216SPaolo Bonzini { 954c6faa758SArd Biesheuvel CPUState *cs; 95553018216SPaolo Bonzini int kernel_size; 95653018216SPaolo Bonzini int initrd_size; 95753018216SPaolo Bonzini int is_linux = 0; 95892df8450SArd Biesheuvel uint64_t elf_entry, elf_low_addr, elf_high_addr; 959da0af40dSPeter Maydell int elf_machine; 96068115ed5SArd Biesheuvel hwaddr entry; 9614d9ebf75SMian M. Hamayun static const ARMInsnFixup *primary_loader; 9629f43d4c3SPeter Maydell AddressSpace *as = arm_boot_address_space(cpu, info); 96353018216SPaolo Bonzini 964*c3a42358SPeter Maydell /* 965*c3a42358SPeter Maydell * CPU objects (unlike devices) are not automatically reset on system 96660b8fe49SIgor Mammedov * reset, so we must always register a handler to do so. If we're 96760b8fe49SIgor Mammedov * actually loading a kernel, the handler is also responsible for 96860b8fe49SIgor Mammedov * arranging that we start it correctly. 96960b8fe49SIgor Mammedov */ 97060b8fe49SIgor Mammedov for (cs = first_cpu; cs; cs = CPU_NEXT(cs)) { 97160b8fe49SIgor Mammedov qemu_register_reset(do_cpu_reset, ARM_CPU(cs)); 97260b8fe49SIgor Mammedov } 97360b8fe49SIgor Mammedov 974*c3a42358SPeter Maydell /* 975*c3a42358SPeter Maydell * The board code is not supposed to set secure_board_setup unless 976baf6b681SPeter Crosthwaite * running its code in secure mode is actually possible, and KVM 977baf6b681SPeter Crosthwaite * doesn't support secure. 978baf6b681SPeter Crosthwaite */ 979baf6b681SPeter Crosthwaite assert(!(info->secure_board_setup && kvm_enabled())); 980baf6b681SPeter Crosthwaite 9814c8afda7SMichael Olbrich info->dtb_filename = qemu_opt_get(qemu_get_machine_opts(), "dtb"); 9823b77f6c3SIgor Mammedov info->dtb_limit = 0; 9834c8afda7SMichael Olbrich 98453018216SPaolo Bonzini /* Load the kernel. */ 98507abe45cSLaszlo Ersek if (!info->kernel_filename || info->firmware_loaded) { 98669e7f76fSArd Biesheuvel 98769e7f76fSArd Biesheuvel if (have_dtb(info)) { 988*c3a42358SPeter Maydell /* 989*c3a42358SPeter Maydell * If we have a device tree blob, but no kernel to supply it to (or 99007abe45cSLaszlo Ersek * the kernel is supposed to be loaded by the bootloader), copy the 99107abe45cSLaszlo Ersek * DTB to the base of RAM for the bootloader to pick up. 99269e7f76fSArd Biesheuvel */ 9933b77f6c3SIgor Mammedov info->dtb_start = info->loader_start; 99469e7f76fSArd Biesheuvel } 99569e7f76fSArd Biesheuvel 99607abe45cSLaszlo Ersek if (info->kernel_filename) { 99707abe45cSLaszlo Ersek FWCfgState *fw_cfg; 99807abe45cSLaszlo Ersek bool try_decompressing_kernel; 99907abe45cSLaszlo Ersek 100007abe45cSLaszlo Ersek fw_cfg = fw_cfg_find(); 100107abe45cSLaszlo Ersek try_decompressing_kernel = arm_feature(&cpu->env, 100207abe45cSLaszlo Ersek ARM_FEATURE_AARCH64); 100307abe45cSLaszlo Ersek 1004*c3a42358SPeter Maydell /* 1005*c3a42358SPeter Maydell * Expose the kernel, the command line, and the initrd in fw_cfg. 100607abe45cSLaszlo Ersek * We don't process them here at all, it's all left to the 100707abe45cSLaszlo Ersek * firmware. 100807abe45cSLaszlo Ersek */ 100907abe45cSLaszlo Ersek load_image_to_fw_cfg(fw_cfg, 101007abe45cSLaszlo Ersek FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA, 101107abe45cSLaszlo Ersek info->kernel_filename, 101207abe45cSLaszlo Ersek try_decompressing_kernel); 101307abe45cSLaszlo Ersek load_image_to_fw_cfg(fw_cfg, 101407abe45cSLaszlo Ersek FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA, 101507abe45cSLaszlo Ersek info->initrd_filename, false); 101607abe45cSLaszlo Ersek 101707abe45cSLaszlo Ersek if (info->kernel_cmdline) { 101807abe45cSLaszlo Ersek fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, 101907abe45cSLaszlo Ersek strlen(info->kernel_cmdline) + 1); 102007abe45cSLaszlo Ersek fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, 102107abe45cSLaszlo Ersek info->kernel_cmdline); 102207abe45cSLaszlo Ersek } 102307abe45cSLaszlo Ersek } 102407abe45cSLaszlo Ersek 1025*c3a42358SPeter Maydell /* 1026*c3a42358SPeter Maydell * We will start from address 0 (typically a boot ROM image) in the 102707abe45cSLaszlo Ersek * same way as hardware. 10289546dbabSPeter Maydell */ 10299546dbabSPeter Maydell return; 103053018216SPaolo Bonzini } 103153018216SPaolo Bonzini 10324d9ebf75SMian M. Hamayun if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { 10334d9ebf75SMian M. Hamayun primary_loader = bootloader_aarch64; 1034da0af40dSPeter Maydell elf_machine = EM_AARCH64; 10354d9ebf75SMian M. Hamayun } else { 10364d9ebf75SMian M. Hamayun primary_loader = bootloader; 103710b8ec73SPeter Crosthwaite if (!info->write_board_setup) { 103810b8ec73SPeter Crosthwaite primary_loader += BOOTLOADER_NO_BOARD_SETUP_OFFSET; 103910b8ec73SPeter Crosthwaite } 1040da0af40dSPeter Maydell elf_machine = EM_ARM; 10414d9ebf75SMian M. Hamayun } 10424d9ebf75SMian M. Hamayun 104353018216SPaolo Bonzini if (!info->secondary_cpu_reset_hook) { 104453018216SPaolo Bonzini info->secondary_cpu_reset_hook = default_reset_secondary; 104553018216SPaolo Bonzini } 104653018216SPaolo Bonzini if (!info->write_secondary_boot) { 104753018216SPaolo Bonzini info->write_secondary_boot = default_write_secondary; 104853018216SPaolo Bonzini } 104953018216SPaolo Bonzini 105053018216SPaolo Bonzini if (info->nb_cpus == 0) 105153018216SPaolo Bonzini info->nb_cpus = 1; 105253018216SPaolo Bonzini 1053*c3a42358SPeter Maydell /* 1054*c3a42358SPeter Maydell * We want to put the initrd far enough into RAM that when the 105553018216SPaolo Bonzini * kernel is uncompressed it will not clobber the initrd. However 105653018216SPaolo Bonzini * on boards without much RAM we must ensure that we still leave 105753018216SPaolo Bonzini * enough room for a decent sized initrd, and on boards with large 105853018216SPaolo Bonzini * amounts of RAM we must avoid the initrd being so far up in RAM 105953018216SPaolo Bonzini * that it is outside lowmem and inaccessible to the kernel. 106053018216SPaolo Bonzini * So for boards with less than 256MB of RAM we put the initrd 106153018216SPaolo Bonzini * halfway into RAM, and for boards with 256MB of RAM or more we put 106253018216SPaolo Bonzini * the initrd at 128MB. 106353018216SPaolo Bonzini */ 106453018216SPaolo Bonzini info->initrd_start = info->loader_start + 106553018216SPaolo Bonzini MIN(info->ram_size / 2, 128 * 1024 * 1024); 106653018216SPaolo Bonzini 106753018216SPaolo Bonzini /* Assume that raw images are linux kernels, and ELF images are not. */ 10689776f636SPeter Crosthwaite kernel_size = arm_load_elf(info, &elf_entry, &elf_low_addr, 10699f43d4c3SPeter Maydell &elf_high_addr, elf_machine, as); 107092df8450SArd Biesheuvel if (kernel_size > 0 && have_dtb(info)) { 1071*c3a42358SPeter Maydell /* 1072*c3a42358SPeter Maydell * If there is still some room left at the base of RAM, try and put 107392df8450SArd Biesheuvel * the DTB there like we do for images loaded with -bios or -pflash. 107492df8450SArd Biesheuvel */ 107592df8450SArd Biesheuvel if (elf_low_addr > info->loader_start 107692df8450SArd Biesheuvel || elf_high_addr < info->loader_start) { 1077*c3a42358SPeter Maydell /* 1078*c3a42358SPeter Maydell * Set elf_low_addr as address limit for arm_load_dtb if it may be 107992df8450SArd Biesheuvel * pointing into RAM, otherwise pass '0' (no limit) 108092df8450SArd Biesheuvel */ 108192df8450SArd Biesheuvel if (elf_low_addr < info->loader_start) { 108292df8450SArd Biesheuvel elf_low_addr = 0; 108392df8450SArd Biesheuvel } 10843b77f6c3SIgor Mammedov info->dtb_start = info->loader_start; 10853b77f6c3SIgor Mammedov info->dtb_limit = elf_low_addr; 108692df8450SArd Biesheuvel } 108792df8450SArd Biesheuvel } 108853018216SPaolo Bonzini entry = elf_entry; 108953018216SPaolo Bonzini if (kernel_size < 0) { 1090f831f955SNick Hudson uint64_t loadaddr = info->loader_start + KERNEL_NOLOAD_ADDR; 1091f831f955SNick Hudson kernel_size = load_uimage_as(info->kernel_filename, &entry, &loadaddr, 10929f43d4c3SPeter Maydell &is_linux, NULL, NULL, as); 109353018216SPaolo Bonzini } 10946f5d3cbeSRichard W.M. Jones if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && kernel_size < 0) { 109568115ed5SArd Biesheuvel kernel_size = load_aarch64_image(info->kernel_filename, 10969f43d4c3SPeter Maydell info->loader_start, &entry, as); 10976f5d3cbeSRichard W.M. Jones is_linux = 1; 109868115ed5SArd Biesheuvel } else if (kernel_size < 0) { 109968115ed5SArd Biesheuvel /* 32-bit ARM */ 110068115ed5SArd Biesheuvel entry = info->loader_start + KERNEL_LOAD_ADDR; 11019f43d4c3SPeter Maydell kernel_size = load_image_targphys_as(info->kernel_filename, entry, 11029f43d4c3SPeter Maydell info->ram_size - KERNEL_LOAD_ADDR, 11039f43d4c3SPeter Maydell as); 110453018216SPaolo Bonzini is_linux = 1; 110553018216SPaolo Bonzini } 110653018216SPaolo Bonzini if (kernel_size < 0) { 1107c0dbca36SAlistair Francis error_report("could not load kernel '%s'", info->kernel_filename); 110853018216SPaolo Bonzini exit(1); 110953018216SPaolo Bonzini } 111053018216SPaolo Bonzini info->entry = entry; 111153018216SPaolo Bonzini if (is_linux) { 111247b1da81SPeter Maydell uint32_t fixupcontext[FIXUP_MAX]; 111347b1da81SPeter Maydell 111453018216SPaolo Bonzini if (info->initrd_filename) { 11159f43d4c3SPeter Maydell initrd_size = load_ramdisk_as(info->initrd_filename, 1116fd76663eSSoren Brinkmann info->initrd_start, 11179f43d4c3SPeter Maydell info->ram_size - info->initrd_start, 11189f43d4c3SPeter Maydell as); 1119fd76663eSSoren Brinkmann if (initrd_size < 0) { 11209f43d4c3SPeter Maydell initrd_size = load_image_targphys_as(info->initrd_filename, 112153018216SPaolo Bonzini info->initrd_start, 112253018216SPaolo Bonzini info->ram_size - 11239f43d4c3SPeter Maydell info->initrd_start, 11249f43d4c3SPeter Maydell as); 1125fd76663eSSoren Brinkmann } 112653018216SPaolo Bonzini if (initrd_size < 0) { 1127c0dbca36SAlistair Francis error_report("could not load initrd '%s'", 112853018216SPaolo Bonzini info->initrd_filename); 112953018216SPaolo Bonzini exit(1); 113053018216SPaolo Bonzini } 113153018216SPaolo Bonzini } else { 113253018216SPaolo Bonzini initrd_size = 0; 113353018216SPaolo Bonzini } 113453018216SPaolo Bonzini info->initrd_size = initrd_size; 113553018216SPaolo Bonzini 113647b1da81SPeter Maydell fixupcontext[FIXUP_BOARDID] = info->board_id; 113710b8ec73SPeter Crosthwaite fixupcontext[FIXUP_BOARD_SETUP] = info->board_setup_addr; 113853018216SPaolo Bonzini 1139*c3a42358SPeter Maydell /* 1140*c3a42358SPeter Maydell * for device tree boot, we pass the DTB directly in r2. Otherwise 114153018216SPaolo Bonzini * we point to the kernel args. 114253018216SPaolo Bonzini */ 114383bfffecSPeter Maydell if (have_dtb(info)) { 114476e2aef3SAlexander Graf hwaddr align; 114576e2aef3SAlexander Graf 114676e2aef3SAlexander Graf if (elf_machine == EM_AARCH64) { 114776e2aef3SAlexander Graf /* 114876e2aef3SAlexander Graf * Some AArch64 kernels on early bootup map the fdt region as 114976e2aef3SAlexander Graf * 115076e2aef3SAlexander Graf * [ ALIGN_DOWN(fdt, 2MB) ... ALIGN_DOWN(fdt, 2MB) + 2MB ] 115176e2aef3SAlexander Graf * 115276e2aef3SAlexander Graf * Let's play safe and prealign it to 2MB to give us some space. 115353018216SPaolo Bonzini */ 115476e2aef3SAlexander Graf align = 2 * 1024 * 1024; 115576e2aef3SAlexander Graf } else { 115676e2aef3SAlexander Graf /* 115776e2aef3SAlexander Graf * Some 32bit kernels will trash anything in the 4K page the 115876e2aef3SAlexander Graf * initrd ends in, so make sure the DTB isn't caught up in that. 115976e2aef3SAlexander Graf */ 116076e2aef3SAlexander Graf align = 4096; 116176e2aef3SAlexander Graf } 116276e2aef3SAlexander Graf 116376e2aef3SAlexander Graf /* Place the DTB after the initrd in memory with alignment. */ 11643b77f6c3SIgor Mammedov info->dtb_start = QEMU_ALIGN_UP(info->initrd_start + initrd_size, 11653b77f6c3SIgor Mammedov align); 1166751ebc13SRicardo Perez Blanco fixupcontext[FIXUP_ARGPTR_LO] = info->dtb_start; 1167751ebc13SRicardo Perez Blanco fixupcontext[FIXUP_ARGPTR_HI] = info->dtb_start >> 32; 116853018216SPaolo Bonzini } else { 1169751ebc13SRicardo Perez Blanco fixupcontext[FIXUP_ARGPTR_LO] = 1170751ebc13SRicardo Perez Blanco info->loader_start + KERNEL_ARGS_ADDR; 1171751ebc13SRicardo Perez Blanco fixupcontext[FIXUP_ARGPTR_HI] = 1172751ebc13SRicardo Perez Blanco (info->loader_start + KERNEL_ARGS_ADDR) >> 32; 117353018216SPaolo Bonzini if (info->ram_size >= (1ULL << 32)) { 1174c0dbca36SAlistair Francis error_report("RAM size must be less than 4GB to boot" 117553018216SPaolo Bonzini " Linux kernel using ATAGS (try passing a device tree" 1176c0dbca36SAlistair Francis " using -dtb)"); 117753018216SPaolo Bonzini exit(1); 117853018216SPaolo Bonzini } 117953018216SPaolo Bonzini } 1180751ebc13SRicardo Perez Blanco fixupcontext[FIXUP_ENTRYPOINT_LO] = entry; 1181751ebc13SRicardo Perez Blanco fixupcontext[FIXUP_ENTRYPOINT_HI] = entry >> 32; 118247b1da81SPeter Maydell 118347b1da81SPeter Maydell write_bootloader("bootloader", info->loader_start, 11849f43d4c3SPeter Maydell primary_loader, fixupcontext, as); 118547b1da81SPeter Maydell 118653018216SPaolo Bonzini if (info->nb_cpus > 1) { 118753018216SPaolo Bonzini info->write_secondary_boot(cpu, info); 118853018216SPaolo Bonzini } 118910b8ec73SPeter Crosthwaite if (info->write_board_setup) { 119010b8ec73SPeter Crosthwaite info->write_board_setup(cpu, info); 119110b8ec73SPeter Crosthwaite } 1192d8b1ae42SPeter Maydell 1193*c3a42358SPeter Maydell /* 1194*c3a42358SPeter Maydell * Notify devices which need to fake up firmware initialization 1195d8b1ae42SPeter Maydell * that we're doing a direct kernel boot. 1196d8b1ae42SPeter Maydell */ 1197d8b1ae42SPeter Maydell object_child_foreach_recursive(object_get_root(), 1198d8b1ae42SPeter Maydell do_arm_linux_init, info); 119953018216SPaolo Bonzini } 120053018216SPaolo Bonzini info->is_linux = is_linux; 120153018216SPaolo Bonzini 12020c949276SIgor Mammedov for (cs = first_cpu; cs; cs = CPU_NEXT(cs)) { 1203c6faa758SArd Biesheuvel ARM_CPU(cs)->env.boot_info = info; 120453018216SPaolo Bonzini } 120563a183edSEric Auger 12063b77f6c3SIgor Mammedov if (!info->skip_dtb_autoload && have_dtb(info)) { 12073b77f6c3SIgor Mammedov if (arm_load_dtb(info->dtb_start, info, info->dtb_limit, as) < 0) { 12083b77f6c3SIgor Mammedov exit(1); 12093b77f6c3SIgor Mammedov } 12103b77f6c3SIgor Mammedov } 1211ac9d32e3SEric Auger } 1212d8b1ae42SPeter Maydell 1213d8b1ae42SPeter Maydell static const TypeInfo arm_linux_boot_if_info = { 1214d8b1ae42SPeter Maydell .name = TYPE_ARM_LINUX_BOOT_IF, 1215d8b1ae42SPeter Maydell .parent = TYPE_INTERFACE, 1216d8b1ae42SPeter Maydell .class_size = sizeof(ARMLinuxBootIfClass), 1217d8b1ae42SPeter Maydell }; 1218d8b1ae42SPeter Maydell 1219d8b1ae42SPeter Maydell static void arm_linux_boot_register_types(void) 1220d8b1ae42SPeter Maydell { 1221d8b1ae42SPeter Maydell type_register_static(&arm_linux_boot_if_info); 1222d8b1ae42SPeter Maydell } 1223d8b1ae42SPeter Maydell 1224d8b1ae42SPeter Maydell type_init(arm_linux_boot_register_types) 1225