arm_boot.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 "config.h"
  10. #include "hw.h"
  11. #include "arm-misc.h"
  12. #include "sysemu/sysemu.h"
  13. #include "boards.h"
  14. #include "loader.h"
  15. #include "elf.h"
  16. #include "sysemu/device_tree.h"
  17. #include "qemu/config-file.h"
  18. #define KERNEL_ARGS_ADDR 0x100
  19. #define KERNEL_LOAD_ADDR 0x00010000
  20. /* The worlds second smallest bootloader. Set r0-r2, then jump to kernel. */
  21. static uint32_t bootloader[] = {
  22. 0xe3a00000, /* mov r0, #0 */
  23. 0xe59f1004, /* ldr r1, [pc, #4] */
  24. 0xe59f2004, /* ldr r2, [pc, #4] */
  25. 0xe59ff004, /* ldr pc, [pc, #4] */
  26. 0, /* Board ID */
  27. 0, /* Address of kernel args. Set by integratorcp_init. */
  28. 0 /* Kernel entry point. Set by integratorcp_init. */
  29. };
  30. /* Handling for secondary CPU boot in a multicore system.
  31. * Unlike the uniprocessor/primary CPU boot, this is platform
  32. * dependent. The default code here is based on the secondary
  33. * CPU boot protocol used on realview/vexpress boards, with
  34. * some parameterisation to increase its flexibility.
  35. * QEMU platform models for which this code is not appropriate
  36. * should override write_secondary_boot and secondary_cpu_reset_hook
  37. * instead.
  38. *
  39. * This code enables the interrupt controllers for the secondary
  40. * CPUs and then puts all the secondary CPUs into a loop waiting
  41. * for an interprocessor interrupt and polling a configurable
  42. * location for the kernel secondary CPU entry point.
  43. */
  44. #define DSB_INSN 0xf57ff04f
  45. #define CP15_DSB_INSN 0xee070f9a /* mcr cp15, 0, r0, c7, c10, 4 */
  46. static uint32_t smpboot[] = {
  47. 0xe59f2028, /* ldr r2, gic_cpu_if */
  48. 0xe59f0028, /* ldr r0, startaddr */
  49. 0xe3a01001, /* mov r1, #1 */
  50. 0xe5821000, /* str r1, [r2] - set GICC_CTLR.Enable */
  51. 0xe3a010ff, /* mov r1, #0xff */
  52. 0xe5821004, /* str r1, [r2, 4] - set GIC_PMR.Priority to 0xff */
  53. DSB_INSN, /* dsb */
  54. 0xe320f003, /* wfi */
  55. 0xe5901000, /* ldr r1, [r0] */
  56. 0xe1110001, /* tst r1, r1 */
  57. 0x0afffffb, /* beq <wfi> */
  58. 0xe12fff11, /* bx r1 */
  59. 0, /* gic_cpu_if: base address of GIC CPU interface */
  60. 0 /* bootreg: Boot register address is held here */
  61. };
  62. static void default_write_secondary(ARMCPU *cpu,
  63. const struct arm_boot_info *info)
  64. {
  65. int n;
  66. smpboot[ARRAY_SIZE(smpboot) - 1] = info->smp_bootreg_addr;
  67. smpboot[ARRAY_SIZE(smpboot) - 2] = info->gic_cpu_if_addr;
  68. for (n = 0; n < ARRAY_SIZE(smpboot); n++) {
  69. /* Replace DSB with the pre-v7 DSB if necessary. */
  70. if (!arm_feature(&cpu->env, ARM_FEATURE_V7) &&
  71. smpboot[n] == DSB_INSN) {
  72. smpboot[n] = CP15_DSB_INSN;
  73. }
  74. smpboot[n] = tswap32(smpboot[n]);
  75. }
  76. rom_add_blob_fixed("smpboot", smpboot, sizeof(smpboot),
  77. info->smp_loader_start);
  78. }
  79. static void default_reset_secondary(ARMCPU *cpu,
  80. const struct arm_boot_info *info)
  81. {
  82. CPUARMState *env = &cpu->env;
  83. stl_phys_notdirty(info->smp_bootreg_addr, 0);
  84. env->regs[15] = info->smp_loader_start;
  85. }
  86. #define WRITE_WORD(p, value) do { \
  87. stl_phys_notdirty(p, value); \
  88. p += 4; \
  89. } while (0)
  90. static void set_kernel_args(const struct arm_boot_info *info)
  91. {
  92. int initrd_size = info->initrd_size;
  93. hwaddr base = info->loader_start;
  94. hwaddr p;
  95. p = base + KERNEL_ARGS_ADDR;
  96. /* ATAG_CORE */
  97. WRITE_WORD(p, 5);
  98. WRITE_WORD(p, 0x54410001);
  99. WRITE_WORD(p, 1);
  100. WRITE_WORD(p, 0x1000);
  101. WRITE_WORD(p, 0);
  102. /* ATAG_MEM */
  103. /* TODO: handle multiple chips on one ATAG list */
  104. WRITE_WORD(p, 4);
  105. WRITE_WORD(p, 0x54410002);
  106. WRITE_WORD(p, info->ram_size);
  107. WRITE_WORD(p, info->loader_start);
  108. if (initrd_size) {
  109. /* ATAG_INITRD2 */
  110. WRITE_WORD(p, 4);
  111. WRITE_WORD(p, 0x54420005);
  112. WRITE_WORD(p, info->initrd_start);
  113. WRITE_WORD(p, initrd_size);
  114. }
  115. if (info->kernel_cmdline && *info->kernel_cmdline) {
  116. /* ATAG_CMDLINE */
  117. int cmdline_size;
  118. cmdline_size = strlen(info->kernel_cmdline);
  119. cpu_physical_memory_write(p + 8, (void *)info->kernel_cmdline,
  120. cmdline_size + 1);
  121. cmdline_size = (cmdline_size >> 2) + 1;
  122. WRITE_WORD(p, cmdline_size + 2);
  123. WRITE_WORD(p, 0x54410009);
  124. p += cmdline_size * 4;
  125. }
  126. if (info->atag_board) {
  127. /* ATAG_BOARD */
  128. int atag_board_len;
  129. uint8_t atag_board_buf[0x1000];
  130. atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3;
  131. WRITE_WORD(p, (atag_board_len + 8) >> 2);
  132. WRITE_WORD(p, 0x414f4d50);
  133. cpu_physical_memory_write(p, atag_board_buf, atag_board_len);
  134. p += atag_board_len;
  135. }
  136. /* ATAG_END */
  137. WRITE_WORD(p, 0);
  138. WRITE_WORD(p, 0);
  139. }
  140. static void set_kernel_args_old(const struct arm_boot_info *info)
  141. {
  142. hwaddr p;
  143. const char *s;
  144. int initrd_size = info->initrd_size;
  145. hwaddr base = info->loader_start;
  146. /* see linux/include/asm-arm/setup.h */
  147. p = base + KERNEL_ARGS_ADDR;
  148. /* page_size */
  149. WRITE_WORD(p, 4096);
  150. /* nr_pages */
  151. WRITE_WORD(p, info->ram_size / 4096);
  152. /* ramdisk_size */
  153. WRITE_WORD(p, 0);
  154. #define FLAG_READONLY 1
  155. #define FLAG_RDLOAD 4
  156. #define FLAG_RDPROMPT 8
  157. /* flags */
  158. WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT);
  159. /* rootdev */
  160. WRITE_WORD(p, (31 << 8) | 0); /* /dev/mtdblock0 */
  161. /* video_num_cols */
  162. WRITE_WORD(p, 0);
  163. /* video_num_rows */
  164. WRITE_WORD(p, 0);
  165. /* video_x */
  166. WRITE_WORD(p, 0);
  167. /* video_y */
  168. WRITE_WORD(p, 0);
  169. /* memc_control_reg */
  170. WRITE_WORD(p, 0);
  171. /* unsigned char sounddefault */
  172. /* unsigned char adfsdrives */
  173. /* unsigned char bytes_per_char_h */
  174. /* unsigned char bytes_per_char_v */
  175. WRITE_WORD(p, 0);
  176. /* pages_in_bank[4] */
  177. WRITE_WORD(p, 0);
  178. WRITE_WORD(p, 0);
  179. WRITE_WORD(p, 0);
  180. WRITE_WORD(p, 0);
  181. /* pages_in_vram */
  182. WRITE_WORD(p, 0);
  183. /* initrd_start */
  184. if (initrd_size) {
  185. WRITE_WORD(p, info->initrd_start);
  186. } else {
  187. WRITE_WORD(p, 0);
  188. }
  189. /* initrd_size */
  190. WRITE_WORD(p, initrd_size);
  191. /* rd_start */
  192. WRITE_WORD(p, 0);
  193. /* system_rev */
  194. WRITE_WORD(p, 0);
  195. /* system_serial_low */
  196. WRITE_WORD(p, 0);
  197. /* system_serial_high */
  198. WRITE_WORD(p, 0);
  199. /* mem_fclk_21285 */
  200. WRITE_WORD(p, 0);
  201. /* zero unused fields */
  202. while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) {
  203. WRITE_WORD(p, 0);
  204. }
  205. s = info->kernel_cmdline;
  206. if (s) {
  207. cpu_physical_memory_write(p, (void *)s, strlen(s) + 1);
  208. } else {
  209. WRITE_WORD(p, 0);
  210. }
  211. }
  212. static int load_dtb(hwaddr addr, const struct arm_boot_info *binfo)
  213. {
  214. #ifdef CONFIG_FDT
  215. uint32_t *mem_reg_property;
  216. uint32_t mem_reg_propsize;
  217. void *fdt = NULL;
  218. char *filename;
  219. int size, rc;
  220. uint32_t acells, scells, hival;
  221. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename);
  222. if (!filename) {
  223. fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename);
  224. return -1;
  225. }
  226. fdt = load_device_tree(filename, &size);
  227. if (!fdt) {
  228. fprintf(stderr, "Couldn't open dtb file %s\n", filename);
  229. g_free(filename);
  230. return -1;
  231. }
  232. g_free(filename);
  233. acells = qemu_devtree_getprop_cell(fdt, "/", "#address-cells");
  234. scells = qemu_devtree_getprop_cell(fdt, "/", "#size-cells");
  235. if (acells == 0 || scells == 0) {
  236. fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n");
  237. return -1;
  238. }
  239. mem_reg_propsize = acells + scells;
  240. mem_reg_property = g_new0(uint32_t, mem_reg_propsize);
  241. mem_reg_property[acells - 1] = cpu_to_be32(binfo->loader_start);
  242. hival = cpu_to_be32(binfo->loader_start >> 32);
  243. if (acells > 1) {
  244. mem_reg_property[acells - 2] = hival;
  245. } else if (hival != 0) {
  246. fprintf(stderr, "qemu: dtb file not compatible with "
  247. "RAM start address > 4GB\n");
  248. exit(1);
  249. }
  250. mem_reg_property[acells + scells - 1] = cpu_to_be32(binfo->ram_size);
  251. hival = cpu_to_be32(binfo->ram_size >> 32);
  252. if (scells > 1) {
  253. mem_reg_property[acells + scells - 2] = hival;
  254. } else if (hival != 0) {
  255. fprintf(stderr, "qemu: dtb file not compatible with "
  256. "RAM size > 4GB\n");
  257. exit(1);
  258. }
  259. rc = qemu_devtree_setprop(fdt, "/memory", "reg", mem_reg_property,
  260. mem_reg_propsize * sizeof(uint32_t));
  261. if (rc < 0) {
  262. fprintf(stderr, "couldn't set /memory/reg\n");
  263. }
  264. if (binfo->kernel_cmdline && *binfo->kernel_cmdline) {
  265. rc = qemu_devtree_setprop_string(fdt, "/chosen", "bootargs",
  266. binfo->kernel_cmdline);
  267. if (rc < 0) {
  268. fprintf(stderr, "couldn't set /chosen/bootargs\n");
  269. }
  270. }
  271. if (binfo->initrd_size) {
  272. rc = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-start",
  273. binfo->initrd_start);
  274. if (rc < 0) {
  275. fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
  276. }
  277. rc = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-end",
  278. binfo->initrd_start + binfo->initrd_size);
  279. if (rc < 0) {
  280. fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
  281. }
  282. }
  283. cpu_physical_memory_write(addr, fdt, size);
  284. return 0;
  285. #else
  286. fprintf(stderr, "Device tree requested, "
  287. "but qemu was compiled without fdt support\n");
  288. return -1;
  289. #endif
  290. }
  291. static void do_cpu_reset(void *opaque)
  292. {
  293. ARMCPU *cpu = opaque;
  294. CPUARMState *env = &cpu->env;
  295. const struct arm_boot_info *info = env->boot_info;
  296. cpu_reset(CPU(cpu));
  297. if (info) {
  298. if (!info->is_linux) {
  299. /* Jump to the entry point. */
  300. env->regs[15] = info->entry & 0xfffffffe;
  301. env->thumb = info->entry & 1;
  302. } else {
  303. if (env == first_cpu) {
  304. env->regs[15] = info->loader_start;
  305. if (!info->dtb_filename) {
  306. if (old_param) {
  307. set_kernel_args_old(info);
  308. } else {
  309. set_kernel_args(info);
  310. }
  311. }
  312. } else {
  313. info->secondary_cpu_reset_hook(cpu, info);
  314. }
  315. }
  316. }
  317. }
  318. void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
  319. {
  320. CPUARMState *env = &cpu->env;
  321. int kernel_size;
  322. int initrd_size;
  323. int n;
  324. int is_linux = 0;
  325. uint64_t elf_entry;
  326. hwaddr entry;
  327. int big_endian;
  328. QemuOpts *machine_opts;
  329. /* Load the kernel. */
  330. if (!info->kernel_filename) {
  331. fprintf(stderr, "Kernel image must be specified\n");
  332. exit(1);
  333. }
  334. machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
  335. if (machine_opts) {
  336. info->dtb_filename = qemu_opt_get(machine_opts, "dtb");
  337. } else {
  338. info->dtb_filename = NULL;
  339. }
  340. if (!info->secondary_cpu_reset_hook) {
  341. info->secondary_cpu_reset_hook = default_reset_secondary;
  342. }
  343. if (!info->write_secondary_boot) {
  344. info->write_secondary_boot = default_write_secondary;
  345. }
  346. if (info->nb_cpus == 0)
  347. info->nb_cpus = 1;
  348. #ifdef TARGET_WORDS_BIGENDIAN
  349. big_endian = 1;
  350. #else
  351. big_endian = 0;
  352. #endif
  353. /* We want to put the initrd far enough into RAM that when the
  354. * kernel is uncompressed it will not clobber the initrd. However
  355. * on boards without much RAM we must ensure that we still leave
  356. * enough room for a decent sized initrd, and on boards with large
  357. * amounts of RAM we must avoid the initrd being so far up in RAM
  358. * that it is outside lowmem and inaccessible to the kernel.
  359. * So for boards with less than 256MB of RAM we put the initrd
  360. * halfway into RAM, and for boards with 256MB of RAM or more we put
  361. * the initrd at 128MB.
  362. */
  363. info->initrd_start = info->loader_start +
  364. MIN(info->ram_size / 2, 128 * 1024 * 1024);
  365. /* Assume that raw images are linux kernels, and ELF images are not. */
  366. kernel_size = load_elf(info->kernel_filename, NULL, NULL, &elf_entry,
  367. NULL, NULL, big_endian, ELF_MACHINE, 1);
  368. entry = elf_entry;
  369. if (kernel_size < 0) {
  370. kernel_size = load_uimage(info->kernel_filename, &entry, NULL,
  371. &is_linux);
  372. }
  373. if (kernel_size < 0) {
  374. entry = info->loader_start + KERNEL_LOAD_ADDR;
  375. kernel_size = load_image_targphys(info->kernel_filename, entry,
  376. info->ram_size - KERNEL_LOAD_ADDR);
  377. is_linux = 1;
  378. }
  379. if (kernel_size < 0) {
  380. fprintf(stderr, "qemu: could not load kernel '%s'\n",
  381. info->kernel_filename);
  382. exit(1);
  383. }
  384. info->entry = entry;
  385. if (is_linux) {
  386. if (info->initrd_filename) {
  387. initrd_size = load_image_targphys(info->initrd_filename,
  388. info->initrd_start,
  389. info->ram_size -
  390. info->initrd_start);
  391. if (initrd_size < 0) {
  392. fprintf(stderr, "qemu: could not load initrd '%s'\n",
  393. info->initrd_filename);
  394. exit(1);
  395. }
  396. } else {
  397. initrd_size = 0;
  398. }
  399. info->initrd_size = initrd_size;
  400. bootloader[4] = info->board_id;
  401. /* for device tree boot, we pass the DTB directly in r2. Otherwise
  402. * we point to the kernel args.
  403. */
  404. if (info->dtb_filename) {
  405. /* Place the DTB after the initrd in memory. Note that some
  406. * kernels will trash anything in the 4K page the initrd
  407. * ends in, so make sure the DTB isn't caught up in that.
  408. */
  409. hwaddr dtb_start = QEMU_ALIGN_UP(info->initrd_start + initrd_size,
  410. 4096);
  411. if (load_dtb(dtb_start, info)) {
  412. exit(1);
  413. }
  414. bootloader[5] = dtb_start;
  415. } else {
  416. bootloader[5] = info->loader_start + KERNEL_ARGS_ADDR;
  417. if (info->ram_size >= (1ULL << 32)) {
  418. fprintf(stderr, "qemu: RAM size must be less than 4GB to boot"
  419. " Linux kernel using ATAGS (try passing a device tree"
  420. " using -dtb)\n");
  421. exit(1);
  422. }
  423. }
  424. bootloader[6] = entry;
  425. for (n = 0; n < sizeof(bootloader) / 4; n++) {
  426. bootloader[n] = tswap32(bootloader[n]);
  427. }
  428. rom_add_blob_fixed("bootloader", bootloader, sizeof(bootloader),
  429. info->loader_start);
  430. if (info->nb_cpus > 1) {
  431. info->write_secondary_boot(cpu, info);
  432. }
  433. }
  434. info->is_linux = is_linux;
  435. for (; env; env = env->next_cpu) {
  436. cpu = arm_env_get_cpu(env);
  437. env->boot_info = info;
  438. qemu_register_reset(do_cpu_reset, cpu);
  439. }
  440. }