qemu/hw/arm/boot.c
<<
>>
Prefs
   1/*
   2 * ARM kernel loader.
   3 *
   4 * Copyright (c) 2006-2007 CodeSourcery.
   5 * Written by Paul Brook
   6 *
   7 * This code is licensed under the GPL.
   8 */
   9
  10#include "qemu/osdep.h"
  11#include "qapi/error.h"
  12#include "hw/hw.h"
  13#include "hw/arm/arm.h"
  14#include "hw/arm/linux-boot-if.h"
  15#include "sysemu/kvm.h"
  16#include "sysemu/sysemu.h"
  17#include "sysemu/numa.h"
  18#include "hw/boards.h"
  19#include "hw/loader.h"
  20#include "elf.h"
  21#include "sysemu/device_tree.h"
  22#include "qemu/config-file.h"
  23#include "exec/address-spaces.h"
  24
  25#include <libfdt.h>
  26
  27/* Kernel boot protocol is specified in the kernel docs
  28 * Documentation/arm/Booting and Documentation/arm64/booting.txt
  29 * They have different preferred image load offsets from system RAM base.
  30 */
  31#define KERNEL_ARGS_ADDR 0x100
  32#define KERNEL_LOAD_ADDR 0x00010000
  33#define KERNEL64_LOAD_ADDR 0x00080000
  34
  35typedef enum {
  36    FIXUP_NONE = 0,     /* do nothing */
  37    FIXUP_TERMINATOR,   /* end of insns */
  38    FIXUP_BOARDID,      /* overwrite with board ID number */
  39    FIXUP_BOARD_SETUP,  /* overwrite with board specific setup code address */
  40    FIXUP_ARGPTR,       /* overwrite with pointer to kernel args */
  41    FIXUP_ENTRYPOINT,   /* overwrite with kernel entry point */
  42    FIXUP_GIC_CPU_IF,   /* overwrite with GIC CPU interface address */
  43    FIXUP_BOOTREG,      /* overwrite with boot register address */
  44    FIXUP_DSB,          /* overwrite with correct DSB insn for cpu */
  45    FIXUP_MAX,
  46} FixupType;
  47
  48typedef struct ARMInsnFixup {
  49    uint32_t insn;
  50    FixupType fixup;
  51} ARMInsnFixup;
  52
  53static const ARMInsnFixup bootloader_aarch64[] = {
  54    { 0x580000c0 }, /* ldr x0, arg ; Load the lower 32-bits of DTB */
  55    { 0xaa1f03e1 }, /* mov x1, xzr */
  56    { 0xaa1f03e2 }, /* mov x2, xzr */
  57    { 0xaa1f03e3 }, /* mov x3, xzr */
  58    { 0x58000084 }, /* ldr x4, entry ; Load the lower 32-bits of kernel entry */
  59    { 0xd61f0080 }, /* br x4      ; Jump to the kernel entry point */
  60    { 0, FIXUP_ARGPTR }, /* arg: .word @DTB Lower 32-bits */
  61    { 0 }, /* .word @DTB Higher 32-bits */
  62    { 0, FIXUP_ENTRYPOINT }, /* entry: .word @Kernel Entry Lower 32-bits */
  63    { 0 }, /* .word @Kernel Entry Higher 32-bits */
  64    { 0, FIXUP_TERMINATOR }
  65};
  66
  67/* A very small bootloader: call the board-setup code (if needed),
  68 * set r0-r2, then jump to the kernel.
  69 * If we're not calling boot setup code then we don't copy across
  70 * the first BOOTLOADER_NO_BOARD_SETUP_OFFSET insns in this array.
  71 */
  72
  73static const ARMInsnFixup bootloader[] = {
  74    { 0xe28fe004 }, /* add     lr, pc, #4 */
  75    { 0xe51ff004 }, /* ldr     pc, [pc, #-4] */
  76    { 0, FIXUP_BOARD_SETUP },
  77#define BOOTLOADER_NO_BOARD_SETUP_OFFSET 3
  78    { 0xe3a00000 }, /* mov     r0, #0 */
  79    { 0xe59f1004 }, /* ldr     r1, [pc, #4] */
  80    { 0xe59f2004 }, /* ldr     r2, [pc, #4] */
  81    { 0xe59ff004 }, /* ldr     pc, [pc, #4] */
  82    { 0, FIXUP_BOARDID },
  83    { 0, FIXUP_ARGPTR },
  84    { 0, FIXUP_ENTRYPOINT },
  85    { 0, FIXUP_TERMINATOR }
  86};
  87
  88/* Handling for secondary CPU boot in a multicore system.
  89 * Unlike the uniprocessor/primary CPU boot, this is platform
  90 * dependent. The default code here is based on the secondary
  91 * CPU boot protocol used on realview/vexpress boards, with
  92 * some parameterisation to increase its flexibility.
  93 * QEMU platform models for which this code is not appropriate
  94 * should override write_secondary_boot and secondary_cpu_reset_hook
  95 * instead.
  96 *
  97 * This code enables the interrupt controllers for the secondary
  98 * CPUs and then puts all the secondary CPUs into a loop waiting
  99 * for an interprocessor interrupt and polling a configurable
 100 * location for the kernel secondary CPU entry point.
 101 */
 102#define DSB_INSN 0xf57ff04f
 103#define CP15_DSB_INSN 0xee070f9a /* mcr cp15, 0, r0, c7, c10, 4 */
 104
 105static const ARMInsnFixup smpboot[] = {
 106    { 0xe59f2028 }, /* ldr r2, gic_cpu_if */
 107    { 0xe59f0028 }, /* ldr r0, bootreg_addr */
 108    { 0xe3a01001 }, /* mov r1, #1 */
 109    { 0xe5821000 }, /* str r1, [r2] - set GICC_CTLR.Enable */
 110    { 0xe3a010ff }, /* mov r1, #0xff */
 111    { 0xe5821004 }, /* str r1, [r2, 4] - set GIC_PMR.Priority to 0xff */
 112    { 0, FIXUP_DSB },   /* dsb */
 113    { 0xe320f003 }, /* wfi */
 114    { 0xe5901000 }, /* ldr     r1, [r0] */
 115    { 0xe1110001 }, /* tst     r1, r1 */
 116    { 0x0afffffb }, /* beq     <wfi> */
 117    { 0xe12fff11 }, /* bx      r1 */
 118    { 0, FIXUP_GIC_CPU_IF }, /* gic_cpu_if: .word 0x.... */
 119    { 0, FIXUP_BOOTREG }, /* bootreg_addr: .word 0x.... */
 120    { 0, FIXUP_TERMINATOR }
 121};
 122
 123static void write_bootloader(const char *name, hwaddr addr,
 124                             const ARMInsnFixup *insns, uint32_t *fixupcontext)
 125{
 126    /* Fix up the specified bootloader fragment and write it into
 127     * guest memory using rom_add_blob_fixed(). fixupcontext is
 128     * an array giving the values to write in for the fixup types
 129     * which write a value into the code array.
 130     */
 131    int i, len;
 132    uint32_t *code;
 133
 134    len = 0;
 135    while (insns[len].fixup != FIXUP_TERMINATOR) {
 136        len++;
 137    }
 138
 139    code = g_new0(uint32_t, len);
 140
 141    for (i = 0; i < len; i++) {
 142        uint32_t insn = insns[i].insn;
 143        FixupType fixup = insns[i].fixup;
 144
 145        switch (fixup) {
 146        case FIXUP_NONE:
 147            break;
 148        case FIXUP_BOARDID:
 149        case FIXUP_BOARD_SETUP:
 150        case FIXUP_ARGPTR:
 151        case FIXUP_ENTRYPOINT:
 152        case FIXUP_GIC_CPU_IF:
 153        case FIXUP_BOOTREG:
 154        case FIXUP_DSB:
 155            insn = fixupcontext[fixup];
 156            break;
 157        default:
 158            abort();
 159        }
 160        code[i] = tswap32(insn);
 161    }
 162
 163    rom_add_blob_fixed(name, code, len * sizeof(uint32_t), addr);
 164
 165    g_free(code);
 166}
 167
 168static void default_write_secondary(ARMCPU *cpu,
 169                                    const struct arm_boot_info *info)
 170{
 171    uint32_t fixupcontext[FIXUP_MAX];
 172
 173    fixupcontext[FIXUP_GIC_CPU_IF] = info->gic_cpu_if_addr;
 174    fixupcontext[FIXUP_BOOTREG] = info->smp_bootreg_addr;
 175    if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
 176        fixupcontext[FIXUP_DSB] = DSB_INSN;
 177    } else {
 178        fixupcontext[FIXUP_DSB] = CP15_DSB_INSN;
 179    }
 180
 181    write_bootloader("smpboot", info->smp_loader_start,
 182                     smpboot, fixupcontext);
 183}
 184
 185void arm_write_secure_board_setup_dummy_smc(ARMCPU *cpu,
 186                                            const struct arm_boot_info *info,
 187                                            hwaddr mvbar_addr)
 188{
 189    int n;
 190    uint32_t mvbar_blob[] = {
 191        /* mvbar_addr: secure monitor vectors
 192         * Default unimplemented and unused vectors to spin. Makes it
 193         * easier to debug (as opposed to the CPU running away).
 194         */
 195        0xeafffffe, /* (spin) */
 196        0xeafffffe, /* (spin) */
 197        0xe1b0f00e, /* movs pc, lr ;SMC exception return */
 198        0xeafffffe, /* (spin) */
 199        0xeafffffe, /* (spin) */
 200        0xeafffffe, /* (spin) */
 201        0xeafffffe, /* (spin) */
 202        0xeafffffe, /* (spin) */
 203    };
 204    uint32_t board_setup_blob[] = {
 205        /* board setup addr */
 206        0xe3a00e00 + (mvbar_addr >> 4), /* mov r0, #mvbar_addr */
 207        0xee0c0f30, /* mcr     p15, 0, r0, c12, c0, 1 ;set MVBAR */
 208        0xee110f11, /* mrc     p15, 0, r0, c1 , c1, 0 ;read SCR */
 209        0xe3800031, /* orr     r0, #0x31              ;enable AW, FW, NS */
 210        0xee010f11, /* mcr     p15, 0, r0, c1, c1, 0  ;write SCR */
 211        0xe1a0100e, /* mov     r1, lr                 ;save LR across SMC */
 212        0xe1600070, /* smc     #0                     ;call monitor to flush SCR */
 213        0xe1a0f001, /* mov     pc, r1                 ;return */
 214    };
 215
 216    /* check that mvbar_addr is correctly aligned and relocatable (using MOV) */
 217    assert((mvbar_addr & 0x1f) == 0 && (mvbar_addr >> 4) < 0x100);
 218
 219    /* check that these blobs don't overlap */
 220    assert((mvbar_addr + sizeof(mvbar_blob) <= info->board_setup_addr)
 221          || (info->board_setup_addr + sizeof(board_setup_blob) <= mvbar_addr));
 222
 223    for (n = 0; n < ARRAY_SIZE(mvbar_blob); n++) {
 224        mvbar_blob[n] = tswap32(mvbar_blob[n]);
 225    }
 226    rom_add_blob_fixed("board-setup-mvbar", mvbar_blob, sizeof(mvbar_blob),
 227                       mvbar_addr);
 228
 229    for (n = 0; n < ARRAY_SIZE(board_setup_blob); n++) {
 230        board_setup_blob[n] = tswap32(board_setup_blob[n]);
 231    }
 232    rom_add_blob_fixed("board-setup", board_setup_blob,
 233                       sizeof(board_setup_blob), info->board_setup_addr);
 234}
 235
 236static void default_reset_secondary(ARMCPU *cpu,
 237                                    const struct arm_boot_info *info)
 238{
 239    CPUState *cs = CPU(cpu);
 240
 241    address_space_stl_notdirty(&address_space_memory, info->smp_bootreg_addr,
 242                               0, MEMTXATTRS_UNSPECIFIED, NULL);
 243    cpu_set_pc(cs, info->smp_loader_start);
 244}
 245
 246static inline bool have_dtb(const struct arm_boot_info *info)
 247{
 248    return info->dtb_filename || info->get_dtb;
 249}
 250
 251#define WRITE_WORD(p, value) do { \
 252    address_space_stl_notdirty(&address_space_memory, p, value, \
 253                               MEMTXATTRS_UNSPECIFIED, NULL);  \
 254    p += 4;                       \
 255} while (0)
 256
 257static void set_kernel_args(const struct arm_boot_info *info)
 258{
 259    int initrd_size = info->initrd_size;
 260    hwaddr base = info->loader_start;
 261    hwaddr p;
 262
 263    p = base + KERNEL_ARGS_ADDR;
 264    /* ATAG_CORE */
 265    WRITE_WORD(p, 5);
 266    WRITE_WORD(p, 0x54410001);
 267    WRITE_WORD(p, 1);
 268    WRITE_WORD(p, 0x1000);
 269    WRITE_WORD(p, 0);
 270    /* ATAG_MEM */
 271    /* TODO: handle multiple chips on one ATAG list */
 272    WRITE_WORD(p, 4);
 273    WRITE_WORD(p, 0x54410002);
 274    WRITE_WORD(p, info->ram_size);
 275    WRITE_WORD(p, info->loader_start);
 276    if (initrd_size) {
 277        /* ATAG_INITRD2 */
 278        WRITE_WORD(p, 4);
 279        WRITE_WORD(p, 0x54420005);
 280        WRITE_WORD(p, info->initrd_start);
 281        WRITE_WORD(p, initrd_size);
 282    }
 283    if (info->kernel_cmdline && *info->kernel_cmdline) {
 284        /* ATAG_CMDLINE */
 285        int cmdline_size;
 286
 287        cmdline_size = strlen(info->kernel_cmdline);
 288        cpu_physical_memory_write(p + 8, info->kernel_cmdline,
 289                                  cmdline_size + 1);
 290        cmdline_size = (cmdline_size >> 2) + 1;
 291        WRITE_WORD(p, cmdline_size + 2);
 292        WRITE_WORD(p, 0x54410009);
 293        p += cmdline_size * 4;
 294    }
 295    if (info->atag_board) {
 296        /* ATAG_BOARD */
 297        int atag_board_len;
 298        uint8_t atag_board_buf[0x1000];
 299
 300        atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3;
 301        WRITE_WORD(p, (atag_board_len + 8) >> 2);
 302        WRITE_WORD(p, 0x414f4d50);
 303        cpu_physical_memory_write(p, atag_board_buf, atag_board_len);
 304        p += atag_board_len;
 305    }
 306    /* ATAG_END */
 307    WRITE_WORD(p, 0);
 308    WRITE_WORD(p, 0);
 309}
 310
 311static void set_kernel_args_old(const struct arm_boot_info *info)
 312{
 313    hwaddr p;
 314    const char *s;
 315    int initrd_size = info->initrd_size;
 316    hwaddr base = info->loader_start;
 317
 318    /* see linux/include/asm-arm/setup.h */
 319    p = base + KERNEL_ARGS_ADDR;
 320    /* page_size */
 321    WRITE_WORD(p, 4096);
 322    /* nr_pages */
 323    WRITE_WORD(p, info->ram_size / 4096);
 324    /* ramdisk_size */
 325    WRITE_WORD(p, 0);
 326#define FLAG_READONLY   1
 327#define FLAG_RDLOAD     4
 328#define FLAG_RDPROMPT   8
 329    /* flags */
 330    WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT);
 331    /* rootdev */
 332    WRITE_WORD(p, (31 << 8) | 0);       /* /dev/mtdblock0 */
 333    /* video_num_cols */
 334    WRITE_WORD(p, 0);
 335    /* video_num_rows */
 336    WRITE_WORD(p, 0);
 337    /* video_x */
 338    WRITE_WORD(p, 0);
 339    /* video_y */
 340    WRITE_WORD(p, 0);
 341    /* memc_control_reg */
 342    WRITE_WORD(p, 0);
 343    /* unsigned char sounddefault */
 344    /* unsigned char adfsdrives */
 345    /* unsigned char bytes_per_char_h */
 346    /* unsigned char bytes_per_char_v */
 347    WRITE_WORD(p, 0);
 348    /* pages_in_bank[4] */
 349    WRITE_WORD(p, 0);
 350    WRITE_WORD(p, 0);
 351    WRITE_WORD(p, 0);
 352    WRITE_WORD(p, 0);
 353    /* pages_in_vram */
 354    WRITE_WORD(p, 0);
 355    /* initrd_start */
 356    if (initrd_size) {
 357        WRITE_WORD(p, info->initrd_start);
 358    } else {
 359        WRITE_WORD(p, 0);
 360    }
 361    /* initrd_size */
 362    WRITE_WORD(p, initrd_size);
 363    /* rd_start */
 364    WRITE_WORD(p, 0);
 365    /* system_rev */
 366    WRITE_WORD(p, 0);
 367    /* system_serial_low */
 368    WRITE_WORD(p, 0);
 369    /* system_serial_high */
 370    WRITE_WORD(p, 0);
 371    /* mem_fclk_21285 */
 372    WRITE_WORD(p, 0);
 373    /* zero unused fields */
 374    while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) {
 375        WRITE_WORD(p, 0);
 376    }
 377    s = info->kernel_cmdline;
 378    if (s) {
 379        cpu_physical_memory_write(p, s, strlen(s) + 1);
 380    } else {
 381        WRITE_WORD(p, 0);
 382    }
 383}
 384
 385/**
 386 * load_dtb() - load a device tree binary image into memory
 387 * @addr:       the address to load the image at
 388 * @binfo:      struct describing the boot environment
 389 * @addr_limit: upper limit of the available memory area at @addr
 390 *
 391 * Load a device tree supplied by the machine or by the user  with the
 392 * '-dtb' command line option, and put it at offset @addr in target
 393 * memory.
 394 *
 395 * If @addr_limit contains a meaningful value (i.e., it is strictly greater
 396 * than @addr), the device tree is only loaded if its size does not exceed
 397 * the limit.
 398 *
 399 * Returns: the size of the device tree image on success,
 400 *          0 if the image size exceeds the limit,
 401 *          -1 on errors.
 402 *
 403 * Note: Must not be called unless have_dtb(binfo) is true.
 404 */
 405static int load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
 406                    hwaddr addr_limit)
 407{
 408    void *fdt = NULL;
 409    int size, rc;
 410    uint32_t acells, scells;
 411    char *nodename;
 412    unsigned int i;
 413    hwaddr mem_base, mem_len;
 414
 415    if (binfo->dtb_filename) {
 416        char *filename;
 417        filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename);
 418        if (!filename) {
 419            fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename);
 420            goto fail;
 421        }
 422
 423        fdt = load_device_tree(filename, &size);
 424        if (!fdt) {
 425            fprintf(stderr, "Couldn't open dtb file %s\n", filename);
 426            g_free(filename);
 427            goto fail;
 428        }
 429        g_free(filename);
 430    } else {
 431        fdt = binfo->get_dtb(binfo, &size);
 432        if (!fdt) {
 433            fprintf(stderr, "Board was unable to create a dtb blob\n");
 434            goto fail;
 435        }
 436    }
 437
 438    if (addr_limit > addr && size > (addr_limit - addr)) {
 439        /* Installing the device tree blob at addr would exceed addr_limit.
 440         * Whether this constitutes failure is up to the caller to decide,
 441         * so just return 0 as size, i.e., no error.
 442         */
 443        g_free(fdt);
 444        return 0;
 445    }
 446
 447    acells = qemu_fdt_getprop_cell(fdt, "/", "#address-cells", 0,
 448                                   false, &error_abort);
 449    scells = qemu_fdt_getprop_cell(fdt, "/", "#size-cells", 0,
 450                                   false, &error_abort);
 451    if (acells == 0 || scells == 0) {
 452        fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n");
 453        goto fail;
 454    }
 455
 456    if (scells < 2 && binfo->ram_size >= (1ULL << 32)) {
 457        /* This is user error so deserves a friendlier error message
 458         * than the failure of setprop_sized_cells would provide
 459         */
 460        fprintf(stderr, "qemu: dtb file not compatible with "
 461                "RAM size > 4GB\n");
 462        goto fail;
 463    }
 464
 465    if (nb_numa_nodes > 0) {
 466        /*
 467         * Turn the /memory node created before into a NOP node, then create
 468         * /memory@addr nodes for all numa nodes respectively.
 469         */
 470        qemu_fdt_nop_node(fdt, "/memory");
 471        mem_base = binfo->loader_start;
 472        for (i = 0; i < nb_numa_nodes; i++) {
 473            mem_len = numa_info[i].node_mem;
 474            nodename = g_strdup_printf("/memory@%" PRIx64, mem_base);
 475            qemu_fdt_add_subnode(fdt, nodename);
 476            qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory");
 477            rc = qemu_fdt_setprop_sized_cells(fdt, nodename, "reg",
 478                                              acells, mem_base,
 479                                              scells, mem_len);
 480            if (rc < 0) {
 481                fprintf(stderr, "couldn't set %s/reg for node %d\n", nodename,
 482                        i);
 483                goto fail;
 484            }
 485
 486            qemu_fdt_setprop_cell(fdt, nodename, "numa-node-id", i);
 487            mem_base += mem_len;
 488            g_free(nodename);
 489        }
 490    } else {
 491        rc = qemu_fdt_setprop_sized_cells(fdt, "/memory", "reg",
 492                                          acells, binfo->loader_start,
 493                                          scells, binfo->ram_size);
 494        if (rc < 0) {
 495            fprintf(stderr, "couldn't set /memory/reg\n");
 496            goto fail;
 497        }
 498    }
 499
 500    if (binfo->kernel_cmdline && *binfo->kernel_cmdline) {
 501        rc = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
 502                                     binfo->kernel_cmdline);
 503        if (rc < 0) {
 504            fprintf(stderr, "couldn't set /chosen/bootargs\n");
 505            goto fail;
 506        }
 507    }
 508
 509    if (binfo->initrd_size) {
 510        rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
 511                                   binfo->initrd_start);
 512        if (rc < 0) {
 513            fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
 514            goto fail;
 515        }
 516
 517        rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
 518                                   binfo->initrd_start + binfo->initrd_size);
 519        if (rc < 0) {
 520            fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
 521            goto fail;
 522        }
 523    }
 524
 525    if (binfo->modify_dtb) {
 526        binfo->modify_dtb(binfo, fdt);
 527    }
 528
 529    qemu_fdt_dumpdtb(fdt, size);
 530
 531    /* Put the DTB into the memory map as a ROM image: this will ensure
 532     * the DTB is copied again upon reset, even if addr points into RAM.
 533     */
 534    rom_add_blob_fixed("dtb", fdt, size, addr);
 535
 536    g_free(fdt);
 537
 538    return size;
 539
 540fail:
 541    g_free(fdt);
 542    return -1;
 543}
 544
 545static void do_cpu_reset(void *opaque)
 546{
 547    ARMCPU *cpu = opaque;
 548    CPUState *cs = CPU(cpu);
 549    CPUARMState *env = &cpu->env;
 550    const struct arm_boot_info *info = env->boot_info;
 551
 552    cpu_reset(cs);
 553    if (info) {
 554        if (!info->is_linux) {
 555            int i;
 556            /* Jump to the entry point.  */
 557            uint64_t entry = info->entry;
 558
 559            switch (info->endianness) {
 560            case ARM_ENDIANNESS_LE:
 561                env->cp15.sctlr_el[1] &= ~SCTLR_E0E;
 562                for (i = 1; i < 4; ++i) {
 563                    env->cp15.sctlr_el[i] &= ~SCTLR_EE;
 564                }
 565                env->uncached_cpsr &= ~CPSR_E;
 566                break;
 567            case ARM_ENDIANNESS_BE8:
 568                env->cp15.sctlr_el[1] |= SCTLR_E0E;
 569                for (i = 1; i < 4; ++i) {
 570                    env->cp15.sctlr_el[i] |= SCTLR_EE;
 571                }
 572                env->uncached_cpsr |= CPSR_E;
 573                break;
 574            case ARM_ENDIANNESS_BE32:
 575                env->cp15.sctlr_el[1] |= SCTLR_B;
 576                break;
 577            case ARM_ENDIANNESS_UNKNOWN:
 578                break; /* Board's decision */
 579            default:
 580                g_assert_not_reached();
 581            }
 582
 583            if (!env->aarch64) {
 584                env->thumb = info->entry & 1;
 585                entry &= 0xfffffffe;
 586            }
 587            cpu_set_pc(cs, entry);
 588        } else {
 589            /* If we are booting Linux then we need to check whether we are
 590             * booting into secure or non-secure state and adjust the state
 591             * accordingly.  Out of reset, ARM is defined to be in secure state
 592             * (SCR.NS = 0), we change that here if non-secure boot has been
 593             * requested.
 594             */
 595            if (arm_feature(env, ARM_FEATURE_EL3)) {
 596                /* AArch64 is defined to come out of reset into EL3 if enabled.
 597                 * If we are booting Linux then we need to adjust our EL as
 598                 * Linux expects us to be in EL2 or EL1.  AArch32 resets into
 599                 * SVC, which Linux expects, so no privilege/exception level to
 600                 * adjust.
 601                 */
 602                if (env->aarch64) {
 603                    env->cp15.scr_el3 |= SCR_RW;
 604                    if (arm_feature(env, ARM_FEATURE_EL2)) {
 605                        env->cp15.hcr_el2 |= HCR_RW;
 606                        env->pstate = PSTATE_MODE_EL2h;
 607                    } else {
 608                        env->pstate = PSTATE_MODE_EL1h;
 609                    }
 610                }
 611
 612                /* Set to non-secure if not a secure boot */
 613                if (!info->secure_boot &&
 614                    (cs != first_cpu || !info->secure_board_setup)) {
 615                    /* Linux expects non-secure state */
 616                    env->cp15.scr_el3 |= SCR_NS;
 617                }
 618            }
 619
 620            if (cs == first_cpu) {
 621                cpu_set_pc(cs, info->loader_start);
 622
 623                if (!have_dtb(info)) {
 624                    if (old_param) {
 625                        set_kernel_args_old(info);
 626                    } else {
 627                        set_kernel_args(info);
 628                    }
 629                }
 630            } else {
 631                info->secondary_cpu_reset_hook(cpu, info);
 632            }
 633        }
 634    }
 635}
 636
 637/**
 638 * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified
 639 *                          by key.
 640 * @fw_cfg:         The firmware config instance to store the data in.
 641 * @size_key:       The firmware config key to store the size of the loaded
 642 *                  data under, with fw_cfg_add_i32().
 643 * @data_key:       The firmware config key to store the loaded data under,
 644 *                  with fw_cfg_add_bytes().
 645 * @image_name:     The name of the image file to load. If it is NULL, the
 646 *                  function returns without doing anything.
 647 * @try_decompress: Whether the image should be decompressed (gunzipped) before
 648 *                  adding it to fw_cfg. If decompression fails, the image is
 649 *                  loaded as-is.
 650 *
 651 * In case of failure, the function prints an error message to stderr and the
 652 * process exits with status 1.
 653 */
 654static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
 655                                 uint16_t data_key, const char *image_name,
 656                                 bool try_decompress)
 657{
 658    size_t size = -1;
 659    uint8_t *data;
 660
 661    if (image_name == NULL) {
 662        return;
 663    }
 664
 665    if (try_decompress) {
 666        size = load_image_gzipped_buffer(image_name,
 667                                         LOAD_IMAGE_MAX_GUNZIP_BYTES, &data);
 668    }
 669
 670    if (size == (size_t)-1) {
 671        gchar *contents;
 672        gsize length;
 673
 674        if (!g_file_get_contents(image_name, &contents, &length, NULL)) {
 675            fprintf(stderr, "failed to load \"%s\"\n", image_name);
 676            exit(1);
 677        }
 678        size = length;
 679        data = (uint8_t *)contents;
 680    }
 681
 682    fw_cfg_add_i32(fw_cfg, size_key, size);
 683    fw_cfg_add_bytes(fw_cfg, data_key, data, size);
 684}
 685
 686static int do_arm_linux_init(Object *obj, void *opaque)
 687{
 688    if (object_dynamic_cast(obj, TYPE_ARM_LINUX_BOOT_IF)) {
 689        ARMLinuxBootIf *albif = ARM_LINUX_BOOT_IF(obj);
 690        ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_GET_CLASS(obj);
 691        struct arm_boot_info *info = opaque;
 692
 693        if (albifc->arm_linux_init) {
 694            albifc->arm_linux_init(albif, info->secure_boot);
 695        }
 696    }
 697    return 0;
 698}
 699
 700static uint64_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry,
 701                             uint64_t *lowaddr, uint64_t *highaddr,
 702                             int elf_machine)
 703{
 704    bool elf_is64;
 705    union {
 706        Elf32_Ehdr h32;
 707        Elf64_Ehdr h64;
 708    } elf_header;
 709    int data_swab = 0;
 710    bool big_endian;
 711    uint64_t ret = -1;
 712    Error *err = NULL;
 713
 714
 715    load_elf_hdr(info->kernel_filename, &elf_header, &elf_is64, &err);
 716    if (err) {
 717        return ret;
 718    }
 719
 720    if (elf_is64) {
 721        big_endian = elf_header.h64.e_ident[EI_DATA] == ELFDATA2MSB;
 722        info->endianness = big_endian ? ARM_ENDIANNESS_BE8
 723                                      : ARM_ENDIANNESS_LE;
 724    } else {
 725        big_endian = elf_header.h32.e_ident[EI_DATA] == ELFDATA2MSB;
 726        if (big_endian) {
 727            if (bswap32(elf_header.h32.e_flags) & EF_ARM_BE8) {
 728                info->endianness = ARM_ENDIANNESS_BE8;
 729            } else {
 730                info->endianness = ARM_ENDIANNESS_BE32;
 731                /* In BE32, the CPU has a different view of the per-byte
 732                 * address map than the rest of the system. BE32 ELF files
 733                 * are organised such that they can be programmed through
 734                 * the CPU's per-word byte-reversed view of the world. QEMU
 735                 * however loads ELF files independently of the CPU. So
 736                 * tell the ELF loader to byte reverse the data for us.
 737                 */
 738                data_swab = 2;
 739            }
 740        } else {
 741            info->endianness = ARM_ENDIANNESS_LE;
 742        }
 743    }
 744
 745    ret = load_elf(info->kernel_filename, NULL, NULL,
 746                   pentry, lowaddr, highaddr, big_endian, elf_machine,
 747                   1, data_swab);
 748    if (ret <= 0) {
 749        /* The header loaded but the image didn't */
 750        exit(1);
 751    }
 752
 753    return ret;
 754}
 755
 756static void arm_load_kernel_notify(Notifier *notifier, void *data)
 757{
 758    CPUState *cs;
 759    int kernel_size;
 760    int initrd_size;
 761    int is_linux = 0;
 762    uint64_t elf_entry, elf_low_addr, elf_high_addr;
 763    int elf_machine;
 764    hwaddr entry, kernel_load_offset;
 765    static const ARMInsnFixup *primary_loader;
 766    ArmLoadKernelNotifier *n = DO_UPCAST(ArmLoadKernelNotifier,
 767                                         notifier, notifier);
 768    ARMCPU *cpu = n->cpu;
 769    struct arm_boot_info *info =
 770        container_of(n, struct arm_boot_info, load_kernel_notifier);
 771
 772    /* The board code is not supposed to set secure_board_setup unless
 773     * running its code in secure mode is actually possible, and KVM
 774     * doesn't support secure.
 775     */
 776    assert(!(info->secure_board_setup && kvm_enabled()));
 777
 778    /* Load the kernel.  */
 779    if (!info->kernel_filename || info->firmware_loaded) {
 780
 781        if (have_dtb(info)) {
 782            /* If we have a device tree blob, but no kernel to supply it to (or
 783             * the kernel is supposed to be loaded by the bootloader), copy the
 784             * DTB to the base of RAM for the bootloader to pick up.
 785             */
 786            if (load_dtb(info->loader_start, info, 0) < 0) {
 787                exit(1);
 788            }
 789        }
 790
 791        if (info->kernel_filename) {
 792            FWCfgState *fw_cfg;
 793            bool try_decompressing_kernel;
 794
 795            fw_cfg = fw_cfg_find();
 796            try_decompressing_kernel = arm_feature(&cpu->env,
 797                                                   ARM_FEATURE_AARCH64);
 798
 799            /* Expose the kernel, the command line, and the initrd in fw_cfg.
 800             * We don't process them here at all, it's all left to the
 801             * firmware.
 802             */
 803            load_image_to_fw_cfg(fw_cfg,
 804                                 FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA,
 805                                 info->kernel_filename,
 806                                 try_decompressing_kernel);
 807            load_image_to_fw_cfg(fw_cfg,
 808                                 FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA,
 809                                 info->initrd_filename, false);
 810
 811            if (info->kernel_cmdline) {
 812                fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
 813                               strlen(info->kernel_cmdline) + 1);
 814                fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA,
 815                                  info->kernel_cmdline);
 816            }
 817        }
 818
 819        /* We will start from address 0 (typically a boot ROM image) in the
 820         * same way as hardware.
 821         */
 822        return;
 823    }
 824
 825    if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
 826        primary_loader = bootloader_aarch64;
 827        kernel_load_offset = KERNEL64_LOAD_ADDR;
 828        elf_machine = EM_AARCH64;
 829    } else {
 830        primary_loader = bootloader;
 831        if (!info->write_board_setup) {
 832            primary_loader += BOOTLOADER_NO_BOARD_SETUP_OFFSET;
 833        }
 834        kernel_load_offset = KERNEL_LOAD_ADDR;
 835        elf_machine = EM_ARM;
 836    }
 837
 838    info->dtb_filename = qemu_opt_get(qemu_get_machine_opts(), "dtb");
 839    is_linux = object_property_get_bool(OBJECT(qdev_get_machine()),
 840                                        "linux", NULL);
 841
 842    if (!info->secondary_cpu_reset_hook) {
 843        info->secondary_cpu_reset_hook = default_reset_secondary;
 844    }
 845    if (!info->write_secondary_boot) {
 846        info->write_secondary_boot = default_write_secondary;
 847    }
 848
 849    if (info->nb_cpus == 0)
 850        info->nb_cpus = 1;
 851
 852    /* We want to put the initrd far enough into RAM that when the
 853     * kernel is uncompressed it will not clobber the initrd. However
 854     * on boards without much RAM we must ensure that we still leave
 855     * enough room for a decent sized initrd, and on boards with large
 856     * amounts of RAM we must avoid the initrd being so far up in RAM
 857     * that it is outside lowmem and inaccessible to the kernel.
 858     * So for boards with less  than 256MB of RAM we put the initrd
 859     * halfway into RAM, and for boards with 256MB of RAM or more we put
 860     * the initrd at 128MB.
 861     */
 862    info->initrd_start = info->loader_start +
 863        MIN(info->ram_size / 2, 128 * 1024 * 1024);
 864
 865    /* Assume that raw images are linux kernels, and ELF images are not.  */
 866    /* Xilinx: Don't use a specified ELF machine, instead let the loader read
 867     * it from the ELF file
 868     */
 869    kernel_size = arm_load_elf(info, &elf_entry, &elf_low_addr,
 870                               &elf_high_addr, 0);
 871    if (kernel_size > 0 && have_dtb(info)) {
 872        /* If there is still some room left at the base of RAM, try and put
 873         * the DTB there like we do for images loaded with -bios or -pflash.
 874         */
 875        if (elf_low_addr > info->loader_start
 876            || elf_high_addr < info->loader_start) {
 877            /* Pass elf_low_addr as address limit to load_dtb if it may be
 878             * pointing into RAM, otherwise pass '0' (no limit)
 879             */
 880            if (elf_low_addr < info->loader_start) {
 881                elf_low_addr = 0;
 882            }
 883            if (load_dtb(info->loader_start, info, elf_low_addr) < 0) {
 884                exit(1);
 885            }
 886        }
 887    }
 888    entry = elf_entry;
 889    if (kernel_size < 0) {
 890        kernel_size = load_uimage(info->kernel_filename, &entry, NULL,
 891                                  &is_linux, NULL, NULL);
 892    }
 893    /* On aarch64, it's the bootloader's job to uncompress the kernel. */
 894    if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && kernel_size < 0) {
 895        entry = info->loader_start + kernel_load_offset;
 896        kernel_size = load_image_gzipped(info->kernel_filename, entry,
 897                                         info->ram_size - kernel_load_offset);
 898        is_linux = 1;
 899    }
 900    if (kernel_size < 0) {
 901        entry = info->loader_start + kernel_load_offset;
 902        kernel_size = load_image_targphys(info->kernel_filename, entry,
 903                                          info->ram_size - kernel_load_offset);
 904        is_linux = 1;
 905    }
 906    if (kernel_size < 0) {
 907        fprintf(stderr, "qemu: could not load kernel '%s'\n",
 908                info->kernel_filename);
 909        exit(1);
 910    }
 911    info->entry = entry;
 912    if (is_linux) {
 913        uint32_t fixupcontext[FIXUP_MAX];
 914
 915        if (info->initrd_filename) {
 916            initrd_size = load_ramdisk(info->initrd_filename,
 917                                       info->initrd_start,
 918                                       info->ram_size -
 919                                       info->initrd_start);
 920            if (initrd_size < 0) {
 921                initrd_size = load_image_targphys(info->initrd_filename,
 922                                                  info->initrd_start,
 923                                                  info->ram_size -
 924                                                  info->initrd_start);
 925            }
 926            if (initrd_size < 0) {
 927                fprintf(stderr, "qemu: could not load initrd '%s'\n",
 928                        info->initrd_filename);
 929                exit(1);
 930            }
 931        } else {
 932            initrd_size = 0;
 933        }
 934        info->initrd_size = initrd_size;
 935
 936        fixupcontext[FIXUP_BOARDID] = info->board_id;
 937        fixupcontext[FIXUP_BOARD_SETUP] = info->board_setup_addr;
 938
 939        if (info->fdt && fdt_path_offset(info->fdt, "/psci") > 0) {
 940            /* There is a PSCI node in the DTS and the image being loaded is a
 941             * Linux image. Therefore tell QEMU to handle the PSCI calls as
 942             * ATF is not loaded.
 943             */
 944            char *method = NULL;
 945
 946            method = qemu_fdt_getprop_string(info->fdt, "/psci", "method",
 947                                             0, false, NULL);
 948
 949            for (cs = CPU(cpu); cs; cs = CPU_NEXT(cs)) {
 950                if (!strcmp(method, "smc")) {
 951                    cpu->psci_conduit = QEMU_PSCI_CONDUIT_SMC;
 952                } else if (!strcmp(method, "hvc")) {
 953                    cpu->psci_conduit = QEMU_PSCI_CONDUIT_HVC;
 954                }
 955            }
 956
 957            g_free(method);
 958        }
 959
 960        /* for device tree boot, we pass the DTB directly in r2. Otherwise
 961         * we point to the kernel args.
 962         */
 963        if (have_dtb(info)) {
 964            hwaddr align;
 965            hwaddr dtb_start;
 966
 967            if (elf_machine == EM_AARCH64) {
 968                /*
 969                 * Some AArch64 kernels on early bootup map the fdt region as
 970                 *
 971                 *   [ ALIGN_DOWN(fdt, 2MB) ... ALIGN_DOWN(fdt, 2MB) + 2MB ]
 972                 *
 973                 * Let's play safe and prealign it to 2MB to give us some space.
 974                 */
 975                align = 2 * 1024 * 1024;
 976            } else {
 977                /*
 978                 * Some 32bit kernels will trash anything in the 4K page the
 979                 * initrd ends in, so make sure the DTB isn't caught up in that.
 980                 */
 981                align = 4096;
 982            }
 983
 984            /* Place the DTB after the initrd in memory with alignment. */
 985            dtb_start = QEMU_ALIGN_UP(info->initrd_start + initrd_size, align);
 986            if (load_dtb(dtb_start, info, 0) < 0) {
 987                exit(1);
 988            }
 989            fixupcontext[FIXUP_ARGPTR] = dtb_start;
 990        } else {
 991            fixupcontext[FIXUP_ARGPTR] = info->loader_start + KERNEL_ARGS_ADDR;
 992            if (info->ram_size >= (1ULL << 32)) {
 993                fprintf(stderr, "qemu: RAM size must be less than 4GB to boot"
 994                        " Linux kernel using ATAGS (try passing a device tree"
 995                        " using -dtb)\n");
 996                exit(1);
 997            }
 998        }
 999        fixupcontext[FIXUP_ENTRYPOINT] = entry;
1000
1001        write_bootloader("bootloader", info->loader_start,
1002                         primary_loader, fixupcontext);
1003
1004        if (info->nb_cpus > 1) {
1005            info->write_secondary_boot(cpu, info);
1006        }
1007        if (info->write_board_setup) {
1008            info->write_board_setup(cpu, info);
1009        }
1010
1011        /* Notify devices which need to fake up firmware initialization
1012         * that we're doing a direct kernel boot.
1013         */
1014        object_child_foreach_recursive(object_get_root(),
1015                                       do_arm_linux_init, info);
1016    }
1017    info->is_linux = is_linux;
1018
1019    for (cs = CPU(cpu); cs; cs = CPU_NEXT(cs)) {
1020        ARM_CPU(cs)->env.boot_info = info;
1021    }
1022}
1023
1024void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
1025{
1026    CPUState *cs;
1027
1028    info->load_kernel_notifier.cpu = cpu;
1029    info->load_kernel_notifier.notifier.notify = arm_load_kernel_notify;
1030    qemu_add_machine_init_done_notifier(&info->load_kernel_notifier.notifier);
1031
1032    /* CPU objects (unlike devices) are not automatically reset on system
1033     * reset, so we must always register a handler to do so. If we're
1034     * actually loading a kernel, the handler is also responsible for
1035     * arranging that we start it correctly.
1036     */
1037    for (cs = CPU(cpu); cs; cs = CPU_NEXT(cs)) {
1038        qemu_register_reset(do_cpu_reset, ARM_CPU(cs));
1039    }
1040}
1041
1042static const TypeInfo arm_linux_boot_if_info = {
1043    .name = TYPE_ARM_LINUX_BOOT_IF,
1044    .parent = TYPE_INTERFACE,
1045    .class_size = sizeof(ARMLinuxBootIfClass),
1046};
1047
1048static void arm_linux_boot_register_types(void)
1049{
1050    type_register_static(&arm_linux_boot_if_info);
1051}
1052
1053type_init(arm_linux_boot_register_types)
1054