boot.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  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, ARMCPU *armcpu)
  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. const char *psci_method;
  400. int64_t psci_conduit;
  401. int rc;
  402. psci_conduit = object_property_get_int(OBJECT(armcpu),
  403. "psci-conduit",
  404. &error_abort);
  405. switch (psci_conduit) {
  406. case QEMU_PSCI_CONDUIT_DISABLED:
  407. return;
  408. case QEMU_PSCI_CONDUIT_HVC:
  409. psci_method = "hvc";
  410. break;
  411. case QEMU_PSCI_CONDUIT_SMC:
  412. psci_method = "smc";
  413. break;
  414. default:
  415. g_assert_not_reached();
  416. }
  417. /*
  418. * A pre-existing /psci node might specify function ID values
  419. * that don't match QEMU's PSCI implementation. Delete the whole
  420. * node and put our own in instead.
  421. */
  422. rc = fdt_path_offset(fdt, "/psci");
  423. if (rc >= 0) {
  424. qemu_fdt_nop_node(fdt, "/psci");
  425. }
  426. qemu_fdt_add_subnode(fdt, "/psci");
  427. if (armcpu->psci_version >= QEMU_PSCI_VERSION_0_2) {
  428. if (armcpu->psci_version < QEMU_PSCI_VERSION_1_0) {
  429. const char comp[] = "arm,psci-0.2\0arm,psci";
  430. qemu_fdt_setprop(fdt, "/psci", "compatible", comp, sizeof(comp));
  431. } else {
  432. const char comp[] = "arm,psci-1.0\0arm,psci-0.2\0arm,psci";
  433. qemu_fdt_setprop(fdt, "/psci", "compatible", comp, sizeof(comp));
  434. }
  435. cpu_off_fn = QEMU_PSCI_0_2_FN_CPU_OFF;
  436. if (arm_feature(&armcpu->env, ARM_FEATURE_AARCH64)) {
  437. cpu_suspend_fn = QEMU_PSCI_0_2_FN64_CPU_SUSPEND;
  438. cpu_on_fn = QEMU_PSCI_0_2_FN64_CPU_ON;
  439. migrate_fn = QEMU_PSCI_0_2_FN64_MIGRATE;
  440. } else {
  441. cpu_suspend_fn = QEMU_PSCI_0_2_FN_CPU_SUSPEND;
  442. cpu_on_fn = QEMU_PSCI_0_2_FN_CPU_ON;
  443. migrate_fn = QEMU_PSCI_0_2_FN_MIGRATE;
  444. }
  445. } else {
  446. qemu_fdt_setprop_string(fdt, "/psci", "compatible", "arm,psci");
  447. cpu_suspend_fn = QEMU_PSCI_0_1_FN_CPU_SUSPEND;
  448. cpu_off_fn = QEMU_PSCI_0_1_FN_CPU_OFF;
  449. cpu_on_fn = QEMU_PSCI_0_1_FN_CPU_ON;
  450. migrate_fn = QEMU_PSCI_0_1_FN_MIGRATE;
  451. }
  452. /* We adopt the PSCI spec's nomenclature, and use 'conduit' to refer
  453. * to the instruction that should be used to invoke PSCI functions.
  454. * However, the device tree binding uses 'method' instead, so that is
  455. * what we should use here.
  456. */
  457. qemu_fdt_setprop_string(fdt, "/psci", "method", psci_method);
  458. qemu_fdt_setprop_cell(fdt, "/psci", "cpu_suspend", cpu_suspend_fn);
  459. qemu_fdt_setprop_cell(fdt, "/psci", "cpu_off", cpu_off_fn);
  460. qemu_fdt_setprop_cell(fdt, "/psci", "cpu_on", cpu_on_fn);
  461. qemu_fdt_setprop_cell(fdt, "/psci", "migrate", migrate_fn);
  462. }
  463. int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
  464. hwaddr addr_limit, AddressSpace *as, MachineState *ms,
  465. ARMCPU *cpu)
  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, cpu);
  595. if (binfo->modify_dtb) {
  596. binfo->modify_dtb(binfo, fdt);
  597. }
  598. /* Put the DTB into the memory map as a ROM image: this will ensure
  599. * the DTB is copied again upon reset, even if addr points into RAM.
  600. */
  601. rom_add_blob_fixed_as("dtb", fdt, size, addr, as);
  602. qemu_register_reset_nosnapshotload(qemu_fdt_randomize_seeds,
  603. rom_ptr_for_as(as, addr, size));
  604. if (fdt != ms->fdt) {
  605. g_free(ms->fdt);
  606. ms->fdt = fdt;
  607. }
  608. return size;
  609. fail:
  610. g_free(fdt);
  611. return -1;
  612. }
  613. static void do_cpu_reset(void *opaque)
  614. {
  615. ARMCPU *cpu = opaque;
  616. CPUState *cs = CPU(cpu);
  617. CPUARMState *env = &cpu->env;
  618. const struct arm_boot_info *info = env->boot_info;
  619. cpu_reset(cs);
  620. if (info) {
  621. if (!info->is_linux) {
  622. int i;
  623. /* Jump to the entry point. */
  624. uint64_t entry = info->entry;
  625. switch (info->endianness) {
  626. case ARM_ENDIANNESS_LE:
  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_BE8:
  634. env->cp15.sctlr_el[1] |= SCTLR_E0E;
  635. for (i = 1; i < 4; ++i) {
  636. env->cp15.sctlr_el[i] |= SCTLR_EE;
  637. }
  638. env->uncached_cpsr |= CPSR_E;
  639. break;
  640. case ARM_ENDIANNESS_BE32:
  641. env->cp15.sctlr_el[1] |= SCTLR_B;
  642. break;
  643. case ARM_ENDIANNESS_UNKNOWN:
  644. break; /* Board's decision */
  645. default:
  646. g_assert_not_reached();
  647. }
  648. cpu_set_pc(cs, entry);
  649. } else {
  650. /*
  651. * If we are booting Linux then we might need to do so at:
  652. * - AArch64 NS EL2 or NS EL1
  653. * - AArch32 Secure SVC (EL3)
  654. * - AArch32 NS Hyp (EL2)
  655. * - AArch32 NS SVC (EL1)
  656. * Configure the CPU in the way boot firmware would do to
  657. * drop us down to the appropriate level.
  658. */
  659. int target_el = arm_feature(env, ARM_FEATURE_EL2) ? 2 : 1;
  660. if (env->aarch64) {
  661. /*
  662. * AArch64 kernels never boot in secure mode, and we don't
  663. * support the secure_board_setup hook for AArch64.
  664. */
  665. assert(!info->secure_boot);
  666. assert(!info->secure_board_setup);
  667. } else {
  668. if (arm_feature(env, ARM_FEATURE_EL3) &&
  669. (info->secure_boot ||
  670. (info->secure_board_setup && cs == first_cpu))) {
  671. /* Start this CPU in Secure SVC */
  672. target_el = 3;
  673. }
  674. }
  675. arm_emulate_firmware_reset(cs, target_el);
  676. if (cs == first_cpu) {
  677. AddressSpace *as = arm_boot_address_space(cpu, info);
  678. cpu_set_pc(cs, info->loader_start);
  679. if (!have_dtb(info)) {
  680. if (old_param) {
  681. set_kernel_args_old(info, as);
  682. } else {
  683. set_kernel_args(info, as);
  684. }
  685. }
  686. } else if (info->secondary_cpu_reset_hook) {
  687. info->secondary_cpu_reset_hook(cpu, info);
  688. }
  689. }
  690. if (tcg_enabled()) {
  691. arm_rebuild_hflags(env);
  692. }
  693. }
  694. }
  695. static int do_arm_linux_init(Object *obj, void *opaque)
  696. {
  697. if (object_dynamic_cast(obj, TYPE_ARM_LINUX_BOOT_IF)) {
  698. ARMLinuxBootIf *albif = ARM_LINUX_BOOT_IF(obj);
  699. ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_GET_CLASS(obj);
  700. struct arm_boot_info *info = opaque;
  701. if (albifc->arm_linux_init) {
  702. albifc->arm_linux_init(albif, info->secure_boot);
  703. }
  704. }
  705. return 0;
  706. }
  707. static ssize_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry,
  708. uint64_t *lowaddr, uint64_t *highaddr,
  709. int elf_machine, AddressSpace *as)
  710. {
  711. bool elf_is64;
  712. union {
  713. Elf32_Ehdr h32;
  714. Elf64_Ehdr h64;
  715. } elf_header;
  716. int data_swab = 0;
  717. int elf_data_order;
  718. ssize_t ret;
  719. Error *err = NULL;
  720. load_elf_hdr(info->kernel_filename, &elf_header, &elf_is64, &err);
  721. if (err) {
  722. /*
  723. * If the file is not an ELF file we silently return.
  724. * The caller will fall back to try other formats.
  725. */
  726. error_free(err);
  727. return -1;
  728. }
  729. if (elf_is64) {
  730. elf_data_order = elf_header.h64.e_ident[EI_DATA];
  731. info->endianness = elf_data_order == ELFDATA2MSB ? ARM_ENDIANNESS_BE8
  732. : ARM_ENDIANNESS_LE;
  733. } else {
  734. elf_data_order = elf_header.h32.e_ident[EI_DATA];
  735. if (elf_data_order == ELFDATA2MSB) {
  736. if (bswap32(elf_header.h32.e_flags) & EF_ARM_BE8) {
  737. info->endianness = ARM_ENDIANNESS_BE8;
  738. } else {
  739. info->endianness = ARM_ENDIANNESS_BE32;
  740. /* In BE32, the CPU has a different view of the per-byte
  741. * address map than the rest of the system. BE32 ELF files
  742. * are organised such that they can be programmed through
  743. * the CPU's per-word byte-reversed view of the world. QEMU
  744. * however loads ELF files independently of the CPU. So
  745. * tell the ELF loader to byte reverse the data for us.
  746. */
  747. data_swab = 2;
  748. }
  749. } else {
  750. info->endianness = ARM_ENDIANNESS_LE;
  751. }
  752. }
  753. ret = load_elf_as(info->kernel_filename, NULL, NULL, NULL,
  754. pentry, lowaddr, highaddr, NULL, elf_data_order,
  755. elf_machine, 1, data_swab, as);
  756. if (ret <= 0) {
  757. /* The header loaded but the image didn't */
  758. error_report("Couldn't load elf '%s': %s",
  759. info->kernel_filename, load_elf_strerror(ret));
  760. exit(1);
  761. }
  762. return ret;
  763. }
  764. static uint64_t load_aarch64_image(const char *filename, hwaddr mem_base,
  765. hwaddr *entry, AddressSpace *as)
  766. {
  767. hwaddr kernel_load_offset = KERNEL64_LOAD_ADDR;
  768. uint64_t kernel_size = 0;
  769. uint8_t *buffer;
  770. ssize_t size;
  771. /* On aarch64, it's the bootloader's job to uncompress the kernel. */
  772. size = load_image_gzipped_buffer(filename, LOAD_IMAGE_MAX_GUNZIP_BYTES,
  773. &buffer);
  774. if (size < 0) {
  775. gsize len;
  776. /* Load as raw file otherwise */
  777. if (!g_file_get_contents(filename, (char **)&buffer, &len, NULL)) {
  778. return -1;
  779. }
  780. size = len;
  781. /* Unpack the image if it is a EFI zboot image */
  782. if (unpack_efi_zboot_image(&buffer, &size) < 0) {
  783. g_free(buffer);
  784. return -1;
  785. }
  786. }
  787. /* check the arm64 magic header value -- very old kernels may not have it */
  788. if (size > ARM64_MAGIC_OFFSET + 4 &&
  789. memcmp(buffer + ARM64_MAGIC_OFFSET, "ARM\x64", 4) == 0) {
  790. uint64_t hdrvals[2];
  791. /* The arm64 Image header has text_offset and image_size fields at 8 and
  792. * 16 bytes into the Image header, respectively. The text_offset field
  793. * is only valid if the image_size is non-zero.
  794. */
  795. memcpy(&hdrvals, buffer + ARM64_TEXT_OFFSET_OFFSET, sizeof(hdrvals));
  796. kernel_size = le64_to_cpu(hdrvals[1]);
  797. if (kernel_size != 0) {
  798. kernel_load_offset = le64_to_cpu(hdrvals[0]);
  799. /*
  800. * We write our startup "bootloader" at the very bottom of RAM,
  801. * so that bit can't be used for the image. Luckily the Image
  802. * format specification is that the image requests only an offset
  803. * from a 2MB boundary, not an absolute load address. So if the
  804. * image requests an offset that might mean it overlaps with the
  805. * bootloader, we can just load it starting at 2MB+offset rather
  806. * than 0MB + offset.
  807. */
  808. if (kernel_load_offset < BOOTLOADER_MAX_SIZE) {
  809. kernel_load_offset += 2 * MiB;
  810. }
  811. }
  812. }
  813. /*
  814. * Kernels before v3.17 don't populate the image_size field, and
  815. * raw images have no header. For those our best guess at the size
  816. * is the size of the Image file itself.
  817. */
  818. if (kernel_size == 0) {
  819. kernel_size = size;
  820. }
  821. *entry = mem_base + kernel_load_offset;
  822. rom_add_blob_fixed_as(filename, buffer, size, *entry, as);
  823. g_free(buffer);
  824. return kernel_size;
  825. }
  826. static void arm_setup_direct_kernel_boot(ARMCPU *cpu,
  827. struct arm_boot_info *info)
  828. {
  829. /* Set up for a direct boot of a kernel image file. */
  830. CPUState *cs;
  831. AddressSpace *as = arm_boot_address_space(cpu, info);
  832. ssize_t kernel_size;
  833. int initrd_size;
  834. int is_linux = 0;
  835. uint64_t elf_entry;
  836. /* Addresses of first byte used and first byte not used by the image */
  837. uint64_t image_low_addr = 0, image_high_addr = 0;
  838. int elf_machine;
  839. hwaddr entry;
  840. static const ARMInsnFixup *primary_loader;
  841. uint64_t ram_end = info->loader_start + info->ram_size;
  842. if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
  843. primary_loader = bootloader_aarch64;
  844. elf_machine = EM_AARCH64;
  845. } else {
  846. primary_loader = bootloader;
  847. if (!info->write_board_setup) {
  848. primary_loader += BOOTLOADER_NO_BOARD_SETUP_OFFSET;
  849. }
  850. elf_machine = EM_ARM;
  851. }
  852. /* Assume that raw images are linux kernels, and ELF images are not. */
  853. kernel_size = arm_load_elf(info, &elf_entry, &image_low_addr,
  854. &image_high_addr, elf_machine, as);
  855. if (kernel_size > 0 && have_dtb(info)) {
  856. /*
  857. * If there is still some room left at the base of RAM, try and put
  858. * the DTB there like we do for images loaded with -bios or -pflash.
  859. */
  860. if (image_low_addr > info->loader_start
  861. || image_high_addr < info->loader_start) {
  862. /*
  863. * Set image_low_addr as address limit for arm_load_dtb if it may be
  864. * pointing into RAM, otherwise pass '0' (no limit)
  865. */
  866. if (image_low_addr < info->loader_start) {
  867. image_low_addr = 0;
  868. }
  869. info->dtb_start = info->loader_start;
  870. info->dtb_limit = image_low_addr;
  871. }
  872. }
  873. entry = elf_entry;
  874. if (kernel_size < 0) {
  875. uint64_t loadaddr = info->loader_start + KERNEL_NOLOAD_ADDR;
  876. kernel_size = load_uimage_as(info->kernel_filename, &entry, &loadaddr,
  877. &is_linux, NULL, NULL, as);
  878. if (kernel_size >= 0) {
  879. image_low_addr = loadaddr;
  880. image_high_addr = image_low_addr + kernel_size;
  881. }
  882. }
  883. if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && kernel_size < 0) {
  884. kernel_size = load_aarch64_image(info->kernel_filename,
  885. info->loader_start, &entry, as);
  886. is_linux = 1;
  887. if (kernel_size >= 0) {
  888. image_low_addr = entry;
  889. image_high_addr = image_low_addr + kernel_size;
  890. }
  891. } else if (kernel_size < 0) {
  892. /* 32-bit ARM */
  893. entry = info->loader_start + KERNEL_LOAD_ADDR;
  894. kernel_size = load_image_targphys_as(info->kernel_filename, entry,
  895. ram_end - KERNEL_LOAD_ADDR, as);
  896. is_linux = 1;
  897. if (kernel_size >= 0) {
  898. image_low_addr = entry;
  899. image_high_addr = image_low_addr + kernel_size;
  900. }
  901. }
  902. if (kernel_size < 0) {
  903. error_report("could not load kernel '%s'", info->kernel_filename);
  904. exit(1);
  905. }
  906. if (kernel_size > info->ram_size) {
  907. error_report("kernel '%s' is too large to fit in RAM "
  908. "(kernel size %zd, RAM size %" PRId64 ")",
  909. info->kernel_filename, kernel_size, info->ram_size);
  910. exit(1);
  911. }
  912. info->entry = entry;
  913. /*
  914. * We want to put the initrd far enough into RAM that when the
  915. * kernel is uncompressed it will not clobber the initrd. However
  916. * on boards without much RAM we must ensure that we still leave
  917. * enough room for a decent sized initrd, and on boards with large
  918. * amounts of RAM we must avoid the initrd being so far up in RAM
  919. * that it is outside lowmem and inaccessible to the kernel.
  920. * So for boards with less than 256MB of RAM we put the initrd
  921. * halfway into RAM, and for boards with 256MB of RAM or more we put
  922. * the initrd at 128MB.
  923. * We also refuse to put the initrd somewhere that will definitely
  924. * overlay the kernel we just loaded, though for kernel formats which
  925. * don't tell us their exact size (eg self-decompressing 32-bit kernels)
  926. * we might still make a bad choice here.
  927. */
  928. info->initrd_start = info->loader_start +
  929. MIN(info->ram_size / 2, 128 * MiB);
  930. if (image_high_addr) {
  931. info->initrd_start = MAX(info->initrd_start, image_high_addr);
  932. }
  933. info->initrd_start = TARGET_PAGE_ALIGN(info->initrd_start);
  934. if (is_linux) {
  935. uint32_t fixupcontext[FIXUP_MAX];
  936. if (info->initrd_filename) {
  937. if (info->initrd_start >= ram_end) {
  938. error_report("not enough space after kernel to load initrd");
  939. exit(1);
  940. }
  941. initrd_size = load_ramdisk_as(info->initrd_filename,
  942. info->initrd_start,
  943. ram_end - info->initrd_start, as);
  944. if (initrd_size < 0) {
  945. initrd_size = load_image_targphys_as(info->initrd_filename,
  946. info->initrd_start,
  947. ram_end -
  948. info->initrd_start,
  949. as);
  950. }
  951. if (initrd_size < 0) {
  952. error_report("could not load initrd '%s'",
  953. info->initrd_filename);
  954. exit(1);
  955. }
  956. if (info->initrd_start + initrd_size > ram_end) {
  957. error_report("could not load initrd '%s': "
  958. "too big to fit into RAM after the kernel",
  959. info->initrd_filename);
  960. exit(1);
  961. }
  962. } else {
  963. initrd_size = 0;
  964. }
  965. info->initrd_size = initrd_size;
  966. fixupcontext[FIXUP_BOARDID] = info->board_id;
  967. fixupcontext[FIXUP_BOARD_SETUP] = info->board_setup_addr;
  968. /*
  969. * for device tree boot, we pass the DTB directly in r2. Otherwise
  970. * we point to the kernel args.
  971. */
  972. if (have_dtb(info)) {
  973. hwaddr align;
  974. if (elf_machine == EM_AARCH64) {
  975. /*
  976. * Some AArch64 kernels on early bootup map the fdt region as
  977. *
  978. * [ ALIGN_DOWN(fdt, 2MB) ... ALIGN_DOWN(fdt, 2MB) + 2MB ]
  979. *
  980. * Let's play safe and prealign it to 2MB to give us some space.
  981. */
  982. align = 2 * MiB;
  983. } else {
  984. /*
  985. * Some 32bit kernels will trash anything in the 4K page the
  986. * initrd ends in, so make sure the DTB isn't caught up in that.
  987. */
  988. align = 4 * KiB;
  989. }
  990. /* Place the DTB after the initrd in memory with alignment. */
  991. info->dtb_start = QEMU_ALIGN_UP(info->initrd_start + initrd_size,
  992. align);
  993. if (info->dtb_start >= ram_end) {
  994. error_report("Not enough space for DTB after kernel/initrd");
  995. exit(1);
  996. }
  997. fixupcontext[FIXUP_ARGPTR_LO] = info->dtb_start;
  998. fixupcontext[FIXUP_ARGPTR_HI] = info->dtb_start >> 32;
  999. } else {
  1000. fixupcontext[FIXUP_ARGPTR_LO] =
  1001. info->loader_start + KERNEL_ARGS_ADDR;
  1002. fixupcontext[FIXUP_ARGPTR_HI] =
  1003. (info->loader_start + KERNEL_ARGS_ADDR) >> 32;
  1004. if (info->ram_size >= 4 * GiB) {
  1005. error_report("RAM size must be less than 4GB to boot"
  1006. " Linux kernel using ATAGS (try passing a device tree"
  1007. " using -dtb)");
  1008. exit(1);
  1009. }
  1010. }
  1011. fixupcontext[FIXUP_ENTRYPOINT_LO] = entry;
  1012. fixupcontext[FIXUP_ENTRYPOINT_HI] = entry >> 32;
  1013. arm_write_bootloader("bootloader", as, info->loader_start,
  1014. primary_loader, fixupcontext);
  1015. if (info->write_board_setup) {
  1016. info->write_board_setup(cpu, info);
  1017. }
  1018. /*
  1019. * Notify devices which need to fake up firmware initialization
  1020. * that we're doing a direct kernel boot.
  1021. */
  1022. object_child_foreach_recursive(object_get_root(),
  1023. do_arm_linux_init, info);
  1024. }
  1025. info->is_linux = is_linux;
  1026. for (cs = first_cpu; cs; cs = CPU_NEXT(cs)) {
  1027. ARM_CPU(cs)->env.boot_info = info;
  1028. }
  1029. }
  1030. static void arm_setup_firmware_boot(ARMCPU *cpu, struct arm_boot_info *info)
  1031. {
  1032. /* Set up for booting firmware (which might load a kernel via fw_cfg) */
  1033. if (have_dtb(info)) {
  1034. /*
  1035. * If we have a device tree blob, but no kernel to supply it to (or
  1036. * the kernel is supposed to be loaded by the bootloader), copy the
  1037. * DTB to the base of RAM for the bootloader to pick up.
  1038. */
  1039. info->dtb_start = info->loader_start;
  1040. }
  1041. if (info->kernel_filename) {
  1042. FWCfgState *fw_cfg;
  1043. bool try_decompressing_kernel;
  1044. fw_cfg = fw_cfg_find();
  1045. if (!fw_cfg) {
  1046. error_report("This machine type does not support loading both "
  1047. "a guest firmware/BIOS image and a guest kernel at "
  1048. "the same time. You should change your QEMU command "
  1049. "line to specify one or the other, but not both.");
  1050. exit(1);
  1051. }
  1052. try_decompressing_kernel = arm_feature(&cpu->env,
  1053. ARM_FEATURE_AARCH64);
  1054. /*
  1055. * Expose the kernel, the command line, and the initrd in fw_cfg.
  1056. * We don't process them here at all, it's all left to the
  1057. * firmware.
  1058. */
  1059. load_image_to_fw_cfg(fw_cfg,
  1060. FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA,
  1061. info->kernel_filename,
  1062. try_decompressing_kernel);
  1063. load_image_to_fw_cfg(fw_cfg,
  1064. FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA,
  1065. info->initrd_filename, false);
  1066. if (info->kernel_cmdline) {
  1067. fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
  1068. strlen(info->kernel_cmdline) + 1);
  1069. fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA,
  1070. info->kernel_cmdline);
  1071. }
  1072. }
  1073. /*
  1074. * We will start from address 0 (typically a boot ROM image) in the
  1075. * same way as hardware. Leave env->boot_info NULL, so that
  1076. * do_cpu_reset() knows it does not need to alter the PC on reset.
  1077. */
  1078. }
  1079. void arm_load_kernel(ARMCPU *cpu, MachineState *ms, struct arm_boot_info *info)
  1080. {
  1081. CPUState *cs;
  1082. AddressSpace *as = arm_boot_address_space(cpu, info);
  1083. int boot_el;
  1084. CPUARMState *env = &cpu->env;
  1085. int nb_cpus = 0;
  1086. /*
  1087. * CPU objects (unlike devices) are not automatically reset on system
  1088. * reset, so we must always register a handler to do so. If we're
  1089. * actually loading a kernel, the handler is also responsible for
  1090. * arranging that we start it correctly.
  1091. */
  1092. for (cs = first_cpu; cs; cs = CPU_NEXT(cs)) {
  1093. qemu_register_reset(do_cpu_reset, ARM_CPU(cs));
  1094. nb_cpus++;
  1095. }
  1096. /*
  1097. * The board code is not supposed to set secure_board_setup unless
  1098. * running its code in secure mode is actually possible, and KVM
  1099. * doesn't support secure.
  1100. */
  1101. assert(!(info->secure_board_setup && kvm_enabled()));
  1102. info->kernel_filename = ms->kernel_filename;
  1103. info->kernel_cmdline = ms->kernel_cmdline;
  1104. info->initrd_filename = ms->initrd_filename;
  1105. info->dtb_filename = ms->dtb;
  1106. info->dtb_limit = 0;
  1107. /* Load the kernel. */
  1108. if (!info->kernel_filename || info->firmware_loaded) {
  1109. arm_setup_firmware_boot(cpu, info);
  1110. } else {
  1111. arm_setup_direct_kernel_boot(cpu, info);
  1112. }
  1113. /*
  1114. * Disable the PSCI conduit if it is set up to target the same
  1115. * or a lower EL than the one we're going to start the guest code in.
  1116. * This logic needs to agree with the code in do_cpu_reset() which
  1117. * decides whether we're going to boot the guest in the highest
  1118. * supported exception level or in a lower one.
  1119. */
  1120. /*
  1121. * If PSCI is enabled, then SMC calls all go to the PSCI handler and
  1122. * are never emulated to trap into guest code. It therefore does not
  1123. * make sense for the board to have a setup code fragment that runs
  1124. * in Secure, because this will probably need to itself issue an SMC of some
  1125. * kind as part of its operation.
  1126. */
  1127. assert(info->psci_conduit == QEMU_PSCI_CONDUIT_DISABLED ||
  1128. !info->secure_board_setup);
  1129. /* Boot into highest supported EL ... */
  1130. if (arm_feature(env, ARM_FEATURE_EL3)) {
  1131. boot_el = 3;
  1132. } else if (arm_feature(env, ARM_FEATURE_EL2)) {
  1133. boot_el = 2;
  1134. } else {
  1135. boot_el = 1;
  1136. }
  1137. /* ...except that if we're booting Linux we adjust the EL we boot into */
  1138. if (info->is_linux && !info->secure_boot) {
  1139. boot_el = arm_feature(env, ARM_FEATURE_EL2) ? 2 : 1;
  1140. }
  1141. if ((info->psci_conduit == QEMU_PSCI_CONDUIT_HVC && boot_el >= 2) ||
  1142. (info->psci_conduit == QEMU_PSCI_CONDUIT_SMC && boot_el == 3)) {
  1143. info->psci_conduit = QEMU_PSCI_CONDUIT_DISABLED;
  1144. }
  1145. if (info->psci_conduit != QEMU_PSCI_CONDUIT_DISABLED) {
  1146. for (cs = first_cpu; cs; cs = CPU_NEXT(cs)) {
  1147. Object *cpuobj = OBJECT(cs);
  1148. object_property_set_int(cpuobj, "psci-conduit", info->psci_conduit,
  1149. &error_abort);
  1150. /*
  1151. * Secondary CPUs start in PSCI powered-down state. Like the
  1152. * code in do_cpu_reset(), we assume first_cpu is the primary
  1153. * CPU.
  1154. */
  1155. if (cs != first_cpu) {
  1156. object_property_set_bool(cpuobj, "start-powered-off", true,
  1157. &error_abort);
  1158. }
  1159. }
  1160. }
  1161. if (info->psci_conduit == QEMU_PSCI_CONDUIT_DISABLED &&
  1162. info->is_linux && nb_cpus > 1) {
  1163. /*
  1164. * We're booting Linux but not using PSCI, so for SMP we need
  1165. * to write a custom secondary CPU boot loader stub, and arrange
  1166. * for the secondary CPU reset to make the accompanying initialization.
  1167. */
  1168. if (!info->secondary_cpu_reset_hook) {
  1169. info->secondary_cpu_reset_hook = default_reset_secondary;
  1170. }
  1171. if (!info->write_secondary_boot) {
  1172. info->write_secondary_boot = default_write_secondary;
  1173. }
  1174. info->write_secondary_boot(cpu, info);
  1175. } else {
  1176. /*
  1177. * No secondary boot stub; don't use the reset hook that would
  1178. * have set the CPU up to call it
  1179. */
  1180. info->write_secondary_boot = NULL;
  1181. info->secondary_cpu_reset_hook = NULL;
  1182. }
  1183. /*
  1184. * arm_load_dtb() may add a PSCI node so it must be called after we have
  1185. * decided whether to enable PSCI and set the psci-conduit CPU properties.
  1186. */
  1187. if (!info->skip_dtb_autoload && have_dtb(info)) {
  1188. if (arm_load_dtb(info->dtb_start, info, info->dtb_limit,
  1189. as, ms, cpu) < 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)