boot.c 45 KB

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