boot.c 45 KB

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