boot.c 46 KB

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