4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
12 #include "hw/arm/arm.h"
13 #include "sysemu/sysemu.h"
14 #include "hw/boards.h"
15 #include "hw/loader.h"
17 #include "sysemu/device_tree.h"
18 #include "qemu/config-file.h"
20 /* Kernel boot protocol is specified in the kernel docs
21 * Documentation/arm/Booting and Documentation/arm64/booting.txt
22 * They have different preferred image load offsets from system RAM base.
24 #define KERNEL_ARGS_ADDR 0x100
25 #define KERNEL_LOAD_ADDR 0x00010000
26 #define KERNEL64_LOAD_ADDR 0x00080000
29 FIXUP_NONE
= 0, /* do nothing */
30 FIXUP_TERMINATOR
, /* end of insns */
31 FIXUP_BOARDID
, /* overwrite with board ID number */
32 FIXUP_ARGPTR
, /* overwrite with pointer to kernel args */
33 FIXUP_ENTRYPOINT
, /* overwrite with kernel entry point */
34 FIXUP_GIC_CPU_IF
, /* overwrite with GIC CPU interface address */
35 FIXUP_BOOTREG
, /* overwrite with boot register address */
36 FIXUP_DSB
, /* overwrite with correct DSB insn for cpu */
40 typedef struct ARMInsnFixup
{
45 static const ARMInsnFixup bootloader_aarch64
[] = {
46 { 0x580000c0 }, /* ldr x0, arg ; Load the lower 32-bits of DTB */
47 { 0xaa1f03e1 }, /* mov x1, xzr */
48 { 0xaa1f03e2 }, /* mov x2, xzr */
49 { 0xaa1f03e3 }, /* mov x3, xzr */
50 { 0x58000084 }, /* ldr x4, entry ; Load the lower 32-bits of kernel entry */
51 { 0xd61f0080 }, /* br x4 ; Jump to the kernel entry point */
52 { 0, FIXUP_ARGPTR
}, /* arg: .word @DTB Lower 32-bits */
53 { 0 }, /* .word @DTB Higher 32-bits */
54 { 0, FIXUP_ENTRYPOINT
}, /* entry: .word @Kernel Entry Lower 32-bits */
55 { 0 }, /* .word @Kernel Entry Higher 32-bits */
56 { 0, FIXUP_TERMINATOR
}
59 /* The worlds second smallest bootloader. Set r0-r2, then jump to kernel. */
60 static const ARMInsnFixup bootloader
[] = {
61 { 0xe3a00000 }, /* mov r0, #0 */
62 { 0xe59f1004 }, /* ldr r1, [pc, #4] */
63 { 0xe59f2004 }, /* ldr r2, [pc, #4] */
64 { 0xe59ff004 }, /* ldr pc, [pc, #4] */
67 { 0, FIXUP_ENTRYPOINT
},
68 { 0, FIXUP_TERMINATOR
}
71 /* Handling for secondary CPU boot in a multicore system.
72 * Unlike the uniprocessor/primary CPU boot, this is platform
73 * dependent. The default code here is based on the secondary
74 * CPU boot protocol used on realview/vexpress boards, with
75 * some parameterisation to increase its flexibility.
76 * QEMU platform models for which this code is not appropriate
77 * should override write_secondary_boot and secondary_cpu_reset_hook
80 * This code enables the interrupt controllers for the secondary
81 * CPUs and then puts all the secondary CPUs into a loop waiting
82 * for an interprocessor interrupt and polling a configurable
83 * location for the kernel secondary CPU entry point.
85 #define DSB_INSN 0xf57ff04f
86 #define CP15_DSB_INSN 0xee070f9a /* mcr cp15, 0, r0, c7, c10, 4 */
88 static const ARMInsnFixup smpboot
[] = {
89 { 0xe59f2028 }, /* ldr r2, gic_cpu_if */
90 { 0xe59f0028 }, /* ldr r0, bootreg_addr */
91 { 0xe3a01001 }, /* mov r1, #1 */
92 { 0xe5821000 }, /* str r1, [r2] - set GICC_CTLR.Enable */
93 { 0xe3a010ff }, /* mov r1, #0xff */
94 { 0xe5821004 }, /* str r1, [r2, 4] - set GIC_PMR.Priority to 0xff */
95 { 0, FIXUP_DSB
}, /* dsb */
96 { 0xe320f003 }, /* wfi */
97 { 0xe5901000 }, /* ldr r1, [r0] */
98 { 0xe1110001 }, /* tst r1, r1 */
99 { 0x0afffffb }, /* beq <wfi> */
100 { 0xe12fff11 }, /* bx r1 */
101 { 0, FIXUP_GIC_CPU_IF
}, /* gic_cpu_if: .word 0x.... */
102 { 0, FIXUP_BOOTREG
}, /* bootreg_addr: .word 0x.... */
103 { 0, FIXUP_TERMINATOR
}
106 static void write_bootloader(const char *name
, hwaddr addr
,
107 const ARMInsnFixup
*insns
, uint32_t *fixupcontext
)
109 /* Fix up the specified bootloader fragment and write it into
110 * guest memory using rom_add_blob_fixed(). fixupcontext is
111 * an array giving the values to write in for the fixup types
112 * which write a value into the code array.
118 while (insns
[len
].fixup
!= FIXUP_TERMINATOR
) {
122 code
= g_new0(uint32_t, len
);
124 for (i
= 0; i
< len
; i
++) {
125 uint32_t insn
= insns
[i
].insn
;
126 FixupType fixup
= insns
[i
].fixup
;
133 case FIXUP_ENTRYPOINT
:
134 case FIXUP_GIC_CPU_IF
:
137 insn
= fixupcontext
[fixup
];
142 code
[i
] = tswap32(insn
);
145 rom_add_blob_fixed(name
, code
, len
* sizeof(uint32_t), addr
);
150 static void default_write_secondary(ARMCPU
*cpu
,
151 const struct arm_boot_info
*info
)
153 uint32_t fixupcontext
[FIXUP_MAX
];
155 fixupcontext
[FIXUP_GIC_CPU_IF
] = info
->gic_cpu_if_addr
;
156 fixupcontext
[FIXUP_BOOTREG
] = info
->smp_bootreg_addr
;
157 if (arm_feature(&cpu
->env
, ARM_FEATURE_V7
)) {
158 fixupcontext
[FIXUP_DSB
] = DSB_INSN
;
160 fixupcontext
[FIXUP_DSB
] = CP15_DSB_INSN
;
163 write_bootloader("smpboot", info
->smp_loader_start
,
164 smpboot
, fixupcontext
);
167 static void default_reset_secondary(ARMCPU
*cpu
,
168 const struct arm_boot_info
*info
)
170 CPUARMState
*env
= &cpu
->env
;
172 stl_phys_notdirty(info
->smp_bootreg_addr
, 0);
173 env
->regs
[15] = info
->smp_loader_start
;
176 #define WRITE_WORD(p, value) do { \
177 stl_phys_notdirty(p, value); \
181 static void set_kernel_args(const struct arm_boot_info
*info
)
183 int initrd_size
= info
->initrd_size
;
184 hwaddr base
= info
->loader_start
;
187 p
= base
+ KERNEL_ARGS_ADDR
;
190 WRITE_WORD(p
, 0x54410001);
192 WRITE_WORD(p
, 0x1000);
195 /* TODO: handle multiple chips on one ATAG list */
197 WRITE_WORD(p
, 0x54410002);
198 WRITE_WORD(p
, info
->ram_size
);
199 WRITE_WORD(p
, info
->loader_start
);
203 WRITE_WORD(p
, 0x54420005);
204 WRITE_WORD(p
, info
->initrd_start
);
205 WRITE_WORD(p
, initrd_size
);
207 if (info
->kernel_cmdline
&& *info
->kernel_cmdline
) {
211 cmdline_size
= strlen(info
->kernel_cmdline
);
212 cpu_physical_memory_write(p
+ 8, info
->kernel_cmdline
,
214 cmdline_size
= (cmdline_size
>> 2) + 1;
215 WRITE_WORD(p
, cmdline_size
+ 2);
216 WRITE_WORD(p
, 0x54410009);
217 p
+= cmdline_size
* 4;
219 if (info
->atag_board
) {
222 uint8_t atag_board_buf
[0x1000];
224 atag_board_len
= (info
->atag_board(info
, atag_board_buf
) + 3) & ~3;
225 WRITE_WORD(p
, (atag_board_len
+ 8) >> 2);
226 WRITE_WORD(p
, 0x414f4d50);
227 cpu_physical_memory_write(p
, atag_board_buf
, atag_board_len
);
235 static void set_kernel_args_old(const struct arm_boot_info
*info
)
239 int initrd_size
= info
->initrd_size
;
240 hwaddr base
= info
->loader_start
;
242 /* see linux/include/asm-arm/setup.h */
243 p
= base
+ KERNEL_ARGS_ADDR
;
247 WRITE_WORD(p
, info
->ram_size
/ 4096);
250 #define FLAG_READONLY 1
251 #define FLAG_RDLOAD 4
252 #define FLAG_RDPROMPT 8
254 WRITE_WORD(p
, FLAG_READONLY
| FLAG_RDLOAD
| FLAG_RDPROMPT
);
256 WRITE_WORD(p
, (31 << 8) | 0); /* /dev/mtdblock0 */
265 /* memc_control_reg */
267 /* unsigned char sounddefault */
268 /* unsigned char adfsdrives */
269 /* unsigned char bytes_per_char_h */
270 /* unsigned char bytes_per_char_v */
272 /* pages_in_bank[4] */
281 WRITE_WORD(p
, info
->initrd_start
);
286 WRITE_WORD(p
, initrd_size
);
291 /* system_serial_low */
293 /* system_serial_high */
297 /* zero unused fields */
298 while (p
< base
+ KERNEL_ARGS_ADDR
+ 256 + 1024) {
301 s
= info
->kernel_cmdline
;
303 cpu_physical_memory_write(p
, s
, strlen(s
) + 1);
309 static int load_dtb(hwaddr addr
, const struct arm_boot_info
*binfo
)
313 uint32_t acells
, scells
;
315 if (binfo
->dtb_filename
) {
317 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, binfo
->dtb_filename
);
319 fprintf(stderr
, "Couldn't open dtb file %s\n", binfo
->dtb_filename
);
323 fdt
= load_device_tree(filename
, &size
);
325 fprintf(stderr
, "Couldn't open dtb file %s\n", filename
);
330 } else if (binfo
->get_dtb
) {
331 fdt
= binfo
->get_dtb(binfo
, &size
);
333 fprintf(stderr
, "Board was unable to create a dtb blob\n");
338 acells
= qemu_fdt_getprop_cell(fdt
, "/", "#address-cells");
339 scells
= qemu_fdt_getprop_cell(fdt
, "/", "#size-cells");
340 if (acells
== 0 || scells
== 0) {
341 fprintf(stderr
, "dtb file invalid (#address-cells or #size-cells 0)\n");
345 if (scells
< 2 && binfo
->ram_size
>= (1ULL << 32)) {
346 /* This is user error so deserves a friendlier error message
347 * than the failure of setprop_sized_cells would provide
349 fprintf(stderr
, "qemu: dtb file not compatible with "
354 rc
= qemu_fdt_setprop_sized_cells(fdt
, "/memory", "reg",
355 acells
, binfo
->loader_start
,
356 scells
, binfo
->ram_size
);
358 fprintf(stderr
, "couldn't set /memory/reg\n");
362 if (binfo
->kernel_cmdline
&& *binfo
->kernel_cmdline
) {
363 rc
= qemu_fdt_setprop_string(fdt
, "/chosen", "bootargs",
364 binfo
->kernel_cmdline
);
366 fprintf(stderr
, "couldn't set /chosen/bootargs\n");
371 if (binfo
->initrd_size
) {
372 rc
= qemu_fdt_setprop_cell(fdt
, "/chosen", "linux,initrd-start",
373 binfo
->initrd_start
);
375 fprintf(stderr
, "couldn't set /chosen/linux,initrd-start\n");
379 rc
= qemu_fdt_setprop_cell(fdt
, "/chosen", "linux,initrd-end",
380 binfo
->initrd_start
+ binfo
->initrd_size
);
382 fprintf(stderr
, "couldn't set /chosen/linux,initrd-end\n");
387 if (binfo
->modify_dtb
) {
388 binfo
->modify_dtb(binfo
, fdt
);
391 qemu_fdt_dumpdtb(fdt
, size
);
393 cpu_physical_memory_write(addr
, fdt
, size
);
404 static void do_cpu_reset(void *opaque
)
406 ARMCPU
*cpu
= opaque
;
407 CPUARMState
*env
= &cpu
->env
;
408 const struct arm_boot_info
*info
= env
->boot_info
;
412 if (!info
->is_linux
) {
413 /* Jump to the entry point. */
414 env
->regs
[15] = info
->entry
& 0xfffffffe;
415 env
->thumb
= info
->entry
& 1;
417 if (CPU(cpu
) == first_cpu
) {
419 env
->pc
= info
->loader_start
;
421 env
->regs
[15] = info
->loader_start
;
424 if (!info
->dtb_filename
) {
426 set_kernel_args_old(info
);
428 set_kernel_args(info
);
432 info
->secondary_cpu_reset_hook(cpu
, info
);
438 void arm_load_kernel(ARMCPU
*cpu
, struct arm_boot_info
*info
)
440 CPUState
*cs
= CPU(cpu
);
445 hwaddr entry
, kernel_load_offset
;
447 static const ARMInsnFixup
*primary_loader
;
449 /* Load the kernel. */
450 if (!info
->kernel_filename
) {
451 /* If no kernel specified, do nothing; we will start from address 0
452 * (typically a boot ROM image) in the same way as hardware.
457 if (arm_feature(&cpu
->env
, ARM_FEATURE_AARCH64
)) {
458 primary_loader
= bootloader_aarch64
;
459 kernel_load_offset
= KERNEL64_LOAD_ADDR
;
461 primary_loader
= bootloader
;
462 kernel_load_offset
= KERNEL_LOAD_ADDR
;
465 info
->dtb_filename
= qemu_opt_get(qemu_get_machine_opts(), "dtb");
467 if (!info
->secondary_cpu_reset_hook
) {
468 info
->secondary_cpu_reset_hook
= default_reset_secondary
;
470 if (!info
->write_secondary_boot
) {
471 info
->write_secondary_boot
= default_write_secondary
;
474 if (info
->nb_cpus
== 0)
477 #ifdef TARGET_WORDS_BIGENDIAN
483 /* We want to put the initrd far enough into RAM that when the
484 * kernel is uncompressed it will not clobber the initrd. However
485 * on boards without much RAM we must ensure that we still leave
486 * enough room for a decent sized initrd, and on boards with large
487 * amounts of RAM we must avoid the initrd being so far up in RAM
488 * that it is outside lowmem and inaccessible to the kernel.
489 * So for boards with less than 256MB of RAM we put the initrd
490 * halfway into RAM, and for boards with 256MB of RAM or more we put
491 * the initrd at 128MB.
493 info
->initrd_start
= info
->loader_start
+
494 MIN(info
->ram_size
/ 2, 128 * 1024 * 1024);
496 /* Assume that raw images are linux kernels, and ELF images are not. */
497 kernel_size
= load_elf(info
->kernel_filename
, NULL
, NULL
, &elf_entry
,
498 NULL
, NULL
, big_endian
, ELF_MACHINE
, 1);
500 if (kernel_size
< 0) {
501 kernel_size
= load_uimage(info
->kernel_filename
, &entry
, NULL
,
504 if (kernel_size
< 0) {
505 entry
= info
->loader_start
+ kernel_load_offset
;
506 kernel_size
= load_image_targphys(info
->kernel_filename
, entry
,
507 info
->ram_size
- kernel_load_offset
);
510 if (kernel_size
< 0) {
511 fprintf(stderr
, "qemu: could not load kernel '%s'\n",
512 info
->kernel_filename
);
517 uint32_t fixupcontext
[FIXUP_MAX
];
519 if (info
->initrd_filename
) {
520 initrd_size
= load_ramdisk(info
->initrd_filename
,
524 if (initrd_size
< 0) {
525 initrd_size
= load_image_targphys(info
->initrd_filename
,
530 if (initrd_size
< 0) {
531 fprintf(stderr
, "qemu: could not load initrd '%s'\n",
532 info
->initrd_filename
);
538 info
->initrd_size
= initrd_size
;
540 fixupcontext
[FIXUP_BOARDID
] = info
->board_id
;
542 /* for device tree boot, we pass the DTB directly in r2. Otherwise
543 * we point to the kernel args.
545 if (info
->dtb_filename
|| info
->get_dtb
) {
546 /* Place the DTB after the initrd in memory. Note that some
547 * kernels will trash anything in the 4K page the initrd
548 * ends in, so make sure the DTB isn't caught up in that.
550 hwaddr dtb_start
= QEMU_ALIGN_UP(info
->initrd_start
+ initrd_size
,
552 if (load_dtb(dtb_start
, info
)) {
555 fixupcontext
[FIXUP_ARGPTR
] = dtb_start
;
557 fixupcontext
[FIXUP_ARGPTR
] = info
->loader_start
+ KERNEL_ARGS_ADDR
;
558 if (info
->ram_size
>= (1ULL << 32)) {
559 fprintf(stderr
, "qemu: RAM size must be less than 4GB to boot"
560 " Linux kernel using ATAGS (try passing a device tree"
565 fixupcontext
[FIXUP_ENTRYPOINT
] = entry
;
567 write_bootloader("bootloader", info
->loader_start
,
568 primary_loader
, fixupcontext
);
570 if (info
->nb_cpus
> 1) {
571 info
->write_secondary_boot(cpu
, info
);
574 info
->is_linux
= is_linux
;
576 for (; cs
; cs
= CPU_NEXT(cs
)) {
578 cpu
->env
.boot_info
= info
;
579 qemu_register_reset(do_cpu_reset
, cpu
);