1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * LoongArch boot helper functions.
4 *
5 * Copyright (c) 2023 Loongson Technology Corporation Limited
6 */
7
8 #include "qemu/osdep.h"
9 #include "qemu/units.h"
10 #include "target/loongarch/cpu.h"
11 #include "hw/loongarch/virt.h"
12 #include "hw/loader.h"
13 #include "elf.h"
14 #include "qemu/error-report.h"
15 #include "system/reset.h"
16 #include "system/qtest.h"
17
18 /*
19 * Linux Image Format
20 * https://docs.kernel.org/arch/loongarch/booting.html
21 */
22 #define LINUX_PE_MAGIC 0x818223cd
23 #define MZ_MAGIC 0x5a4d /* "MZ" */
24
25 struct loongarch_linux_hdr {
26 uint32_t mz_magic;
27 uint32_t res0;
28 uint64_t kernel_entry;
29 uint64_t kernel_size;
30 uint64_t load_offset;
31 uint64_t res1;
32 uint64_t res2;
33 uint64_t res3;
34 uint32_t linux_pe_magic;
35 uint32_t pe_header_offset;
36 } QEMU_PACKED;
37
38 struct memmap_entry *memmap_table;
39 unsigned memmap_entries;
40
41 ram_addr_t initrd_offset;
42 uint64_t initrd_size;
43
44 static const unsigned int slave_boot_code[] = {
45 /* Configure reset ebase. */
46 0x0400302c, /* csrwr $t0, LOONGARCH_CSR_EENTRY */
47
48 /* Disable interrupt. */
49 0x0380100c, /* ori $t0, $zero,0x4 */
50 0x04000180, /* csrxchg $zero, $t0, LOONGARCH_CSR_CRMD */
51
52 /* Clear mailbox. */
53 0x1400002d, /* lu12i.w $t1, 1(0x1) */
54 0x038081ad, /* ori $t1, $t1, CORE_BUF_20 */
55 0x06481da0, /* iocsrwr.d $zero, $t1 */
56
57 /* Enable IPI interrupt. */
58 0x1400002c, /* lu12i.w $t0, 1(0x1) */
59 0x0400118c, /* csrxchg $t0, $t0, LOONGARCH_CSR_ECFG */
60 0x02fffc0c, /* addi.d $t0, $r0,-1(0xfff) */
61 0x1400002d, /* lu12i.w $t1, 1(0x1) */
62 0x038011ad, /* ori $t1, $t1, CORE_EN_OFF */
63 0x064819ac, /* iocsrwr.w $t0, $t1 */
64 0x1400002d, /* lu12i.w $t1, 1(0x1) */
65 0x038081ad, /* ori $t1, $t1, CORE_BUF_20 */
66
67 /* Wait for wakeup <.L11>: */
68 0x06488000, /* idle 0x0 */
69 0x03400000, /* andi $zero, $zero, 0x0 */
70 0x064809ac, /* iocsrrd.w $t0, $t1 */
71 0x43fff59f, /* beqz $t0, -12(0x7ffff4) # 48 <.L11> */
72
73 /* Read and clear IPI interrupt. */
74 0x1400002d, /* lu12i.w $t1, 1(0x1) */
75 0x064809ac, /* iocsrrd.w $t0, $t1 */
76 0x1400002d, /* lu12i.w $t1, 1(0x1) */
77 0x038031ad, /* ori $t1, $t1, CORE_CLEAR_OFF */
78 0x064819ac, /* iocsrwr.w $t0, $t1 */
79
80 /* Disable IPI interrupt. */
81 0x1400002c, /* lu12i.w $t0, 1(0x1) */
82 0x04001180, /* csrxchg $zero, $t0, LOONGARCH_CSR_ECFG */
83
84 /* Read mail buf and jump to specified entry */
85 0x1400002d, /* lu12i.w $t1, 1(0x1) */
86 0x038081ad, /* ori $t1, $t1, CORE_BUF_20 */
87 0x06480dac, /* iocsrrd.d $t0, $t1 */
88 0x00150181, /* move $ra, $t0 */
89 0x4c000020, /* jirl $zero, $ra,0 */
90 };
91
guidcpy(void * dst,const void * src)92 static inline void *guidcpy(void *dst, const void *src)
93 {
94 return memcpy(dst, src, sizeof(efi_guid_t));
95 }
96
init_efi_boot_memmap(struct efi_system_table * systab,void * p,void * start)97 static void init_efi_boot_memmap(struct efi_system_table *systab,
98 void *p, void *start)
99 {
100 unsigned i;
101 struct efi_boot_memmap *boot_memmap = p;
102 efi_guid_t tbl_guid = LINUX_EFI_BOOT_MEMMAP_GUID;
103
104 /* efi_configuration_table 1 */
105 guidcpy(&systab->tables[0].guid, &tbl_guid);
106 systab->tables[0].table = (struct efi_configuration_table *)(p - start);
107 systab->nr_tables = 1;
108
109 boot_memmap->desc_size = sizeof(efi_memory_desc_t);
110 boot_memmap->desc_ver = 1;
111 boot_memmap->map_size = 0;
112
113 efi_memory_desc_t *map = p + sizeof(struct efi_boot_memmap);
114 for (i = 0; i < memmap_entries; i++) {
115 map = (void *)boot_memmap + sizeof(*map);
116 map[i].type = memmap_table[i].type;
117 map[i].phys_addr = ROUND_UP(memmap_table[i].address, 64 * KiB);
118 map[i].num_pages = ROUND_DOWN(memmap_table[i].address +
119 memmap_table[i].length - map[i].phys_addr, 64 * KiB);
120 p += sizeof(efi_memory_desc_t);
121 }
122 }
123
init_efi_initrd_table(struct efi_system_table * systab,void * p,void * start)124 static void init_efi_initrd_table(struct efi_system_table *systab,
125 void *p, void *start)
126 {
127 efi_guid_t tbl_guid = LINUX_EFI_INITRD_MEDIA_GUID;
128 struct efi_initrd *initrd_table = p;
129
130 /* efi_configuration_table 2 */
131 guidcpy(&systab->tables[1].guid, &tbl_guid);
132 systab->tables[1].table = (struct efi_configuration_table *)(p - start);
133 systab->nr_tables = 2;
134
135 initrd_table->base = initrd_offset;
136 initrd_table->size = initrd_size;
137 }
138
init_efi_fdt_table(struct efi_system_table * systab)139 static void init_efi_fdt_table(struct efi_system_table *systab)
140 {
141 efi_guid_t tbl_guid = DEVICE_TREE_GUID;
142
143 /* efi_configuration_table 3 */
144 guidcpy(&systab->tables[2].guid, &tbl_guid);
145 systab->tables[2].table = (void *)FDT_BASE;
146 systab->nr_tables = 3;
147 }
148
init_systab(struct loongarch_boot_info * info,void * p,void * start)149 static void init_systab(struct loongarch_boot_info *info, void *p, void *start)
150 {
151 void *bp_tables_start;
152 struct efi_system_table *systab = p;
153
154 info->a2 = p - start;
155
156 systab->hdr.signature = EFI_SYSTEM_TABLE_SIGNATURE;
157 systab->hdr.revision = EFI_SPECIFICATION_VERSION;
158 systab->hdr.revision = sizeof(struct efi_system_table),
159 systab->fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8;
160 systab->runtime = 0;
161 systab->boottime = 0;
162 systab->nr_tables = 0;
163
164 p += ROUND_UP(sizeof(struct efi_system_table), 64 * KiB);
165
166 systab->tables = p;
167 bp_tables_start = p;
168
169 init_efi_boot_memmap(systab, p, start);
170 p += ROUND_UP(sizeof(struct efi_boot_memmap) +
171 sizeof(efi_memory_desc_t) * memmap_entries, 64 * KiB);
172 init_efi_initrd_table(systab, p, start);
173 p += ROUND_UP(sizeof(struct efi_initrd), 64 * KiB);
174 init_efi_fdt_table(systab);
175
176 systab->tables = (struct efi_configuration_table *)(bp_tables_start - start);
177 }
178
init_cmdline(struct loongarch_boot_info * info,void * p,void * start)179 static void init_cmdline(struct loongarch_boot_info *info, void *p, void *start)
180 {
181 hwaddr cmdline_addr = p - start;
182
183 info->a0 = 1;
184 info->a1 = cmdline_addr;
185
186 g_strlcpy(p, info->kernel_cmdline, COMMAND_LINE_SIZE);
187 }
188
cpu_loongarch_virt_to_phys(void * opaque,uint64_t addr)189 static uint64_t cpu_loongarch_virt_to_phys(void *opaque, uint64_t addr)
190 {
191 return addr & MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
192 }
193
load_loongarch_linux_image(const char * filename,uint64_t * kernel_entry,uint64_t * kernel_low,uint64_t * kernel_high)194 static int64_t load_loongarch_linux_image(const char *filename,
195 uint64_t *kernel_entry,
196 uint64_t *kernel_low,
197 uint64_t *kernel_high)
198 {
199 gsize len;
200 ssize_t size;
201 uint8_t *buffer;
202 struct loongarch_linux_hdr *hdr;
203
204 /* Load as raw file otherwise */
205 if (!g_file_get_contents(filename, (char **)&buffer, &len, NULL)) {
206 return -1;
207 }
208 size = len;
209
210 /* Unpack the image if it is a EFI zboot image */
211 if (unpack_efi_zboot_image(&buffer, &size) < 0) {
212 g_free(buffer);
213 return -1;
214 }
215
216 hdr = (struct loongarch_linux_hdr *)buffer;
217
218 if (extract32(le32_to_cpu(hdr->mz_magic), 0, 16) != MZ_MAGIC ||
219 le32_to_cpu(hdr->linux_pe_magic) != LINUX_PE_MAGIC) {
220 g_free(buffer);
221 return -1;
222 }
223
224 /* Early kernel versions may have those fields in virtual address */
225 *kernel_entry = extract64(le64_to_cpu(hdr->kernel_entry),
226 0, TARGET_PHYS_ADDR_SPACE_BITS);
227 *kernel_low = extract64(le64_to_cpu(hdr->load_offset),
228 0, TARGET_PHYS_ADDR_SPACE_BITS);
229 *kernel_high = *kernel_low + size;
230
231 rom_add_blob_fixed(filename, buffer, size, *kernel_low);
232
233 g_free(buffer);
234
235 return size;
236 }
237
alloc_initrd_memory(struct loongarch_boot_info * info,uint64_t advice_start,ssize_t rd_size)238 static ram_addr_t alloc_initrd_memory(struct loongarch_boot_info *info,
239 uint64_t advice_start, ssize_t rd_size)
240 {
241 hwaddr base, ram_size, gap, low_end;
242 ram_addr_t initrd_end, initrd_start;
243
244 base = VIRT_LOWMEM_BASE;
245 gap = VIRT_LOWMEM_SIZE;
246 initrd_start = advice_start;
247 initrd_end = initrd_start + rd_size;
248
249 ram_size = info->ram_size;
250 low_end = base + MIN(ram_size, gap);
251 if (initrd_end <= low_end) {
252 return initrd_start;
253 }
254
255 if (ram_size <= gap) {
256 error_report("The low memory too small for initial ram disk '%s',"
257 "You need to expand the ram",
258 info->initrd_filename);
259 exit(1);
260 }
261
262 /*
263 * Try to load initrd in the high memory
264 */
265 ram_size -= gap;
266 initrd_start = VIRT_HIGHMEM_BASE;
267 if (rd_size <= ram_size) {
268 return initrd_start;
269 }
270
271 error_report("The high memory too small for initial ram disk '%s',"
272 "You need to expand the ram",
273 info->initrd_filename);
274 exit(1);
275 }
276
load_kernel_info(struct loongarch_boot_info * info)277 static int64_t load_kernel_info(struct loongarch_boot_info *info)
278 {
279 uint64_t kernel_entry, kernel_low, kernel_high;
280 ssize_t kernel_size;
281
282 kernel_size = load_elf(info->kernel_filename, NULL,
283 cpu_loongarch_virt_to_phys, NULL,
284 &kernel_entry, &kernel_low,
285 &kernel_high, NULL, ELFDATA2LSB,
286 EM_LOONGARCH, 1, 0);
287 kernel_entry = cpu_loongarch_virt_to_phys(NULL, kernel_entry);
288 if (kernel_size < 0) {
289 kernel_size = load_loongarch_linux_image(info->kernel_filename,
290 &kernel_entry, &kernel_low,
291 &kernel_high);
292 }
293
294 if (kernel_size < 0) {
295 error_report("could not load kernel '%s': %s",
296 info->kernel_filename,
297 load_elf_strerror(kernel_size));
298 exit(1);
299 }
300
301 if (info->initrd_filename) {
302 initrd_size = get_image_size(info->initrd_filename);
303 if (initrd_size > 0) {
304 initrd_offset = ROUND_UP(kernel_high + 4 * kernel_size, 64 * KiB);
305 initrd_offset = alloc_initrd_memory(info, initrd_offset,
306 initrd_size);
307 initrd_size = load_image_targphys(info->initrd_filename,
308 initrd_offset, initrd_size);
309 }
310
311 if (initrd_size == (target_ulong)-1) {
312 error_report("could not load initial ram disk '%s'",
313 info->initrd_filename);
314 exit(1);
315 }
316 } else {
317 initrd_size = 0;
318 }
319
320 return kernel_entry;
321 }
322
reset_load_elf(void * opaque)323 static void reset_load_elf(void *opaque)
324 {
325 LoongArchCPU *cpu = opaque;
326 CPULoongArchState *env = &cpu->env;
327
328 cpu_reset(CPU(cpu));
329 if (env->load_elf) {
330 if (cpu == LOONGARCH_CPU(first_cpu)) {
331 env->gpr[4] = env->boot_info->a0;
332 env->gpr[5] = env->boot_info->a1;
333 env->gpr[6] = env->boot_info->a2;
334 }
335 cpu_set_pc(CPU(cpu), env->elf_address);
336 }
337 }
338
fw_cfg_add_kernel_info(struct loongarch_boot_info * info,FWCfgState * fw_cfg)339 static void fw_cfg_add_kernel_info(struct loongarch_boot_info *info,
340 FWCfgState *fw_cfg)
341 {
342 /*
343 * Expose the kernel, the command line, and the initrd in fw_cfg.
344 * We don't process them here at all, it's all left to the
345 * firmware.
346 */
347 load_image_to_fw_cfg(fw_cfg,
348 FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA,
349 info->kernel_filename,
350 false);
351
352 if (info->initrd_filename) {
353 load_image_to_fw_cfg(fw_cfg,
354 FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA,
355 info->initrd_filename, false);
356 }
357
358 if (info->kernel_cmdline) {
359 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
360 strlen(info->kernel_cmdline) + 1);
361 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA,
362 info->kernel_cmdline);
363 }
364 }
365
loongarch_firmware_boot(LoongArchVirtMachineState * lvms,struct loongarch_boot_info * info)366 static void loongarch_firmware_boot(LoongArchVirtMachineState *lvms,
367 struct loongarch_boot_info *info)
368 {
369 fw_cfg_add_kernel_info(info, lvms->fw_cfg);
370 }
371
init_boot_rom(struct loongarch_boot_info * info,void * p)372 static void init_boot_rom(struct loongarch_boot_info *info, void *p)
373 {
374 void *start = p;
375
376 init_cmdline(info, p, start);
377 p += COMMAND_LINE_SIZE;
378
379 init_systab(info, p, start);
380 }
381
loongarch_direct_kernel_boot(struct loongarch_boot_info * info)382 static void loongarch_direct_kernel_boot(struct loongarch_boot_info *info)
383 {
384 void *p, *bp;
385 int64_t kernel_addr = VIRT_FLASH0_BASE;
386 LoongArchCPU *lacpu;
387 CPUState *cs;
388
389 if (info->kernel_filename) {
390 kernel_addr = load_kernel_info(info);
391 } else {
392 if (!qtest_enabled()) {
393 warn_report("No kernel provided, booting from flash drive.");
394 }
395 }
396
397 /* Load cmdline and system tables at [0 - 1 MiB] */
398 p = g_malloc0(1 * MiB);
399 bp = p;
400 init_boot_rom(info, p);
401 rom_add_blob_fixed_as("boot_info", bp, 1 * MiB, 0, &address_space_memory);
402
403 /* Load slave boot code at pflash0 . */
404 void *boot_code = g_malloc0(VIRT_FLASH0_SIZE);
405 memcpy(boot_code, &slave_boot_code, sizeof(slave_boot_code));
406 rom_add_blob_fixed("boot_code", boot_code, VIRT_FLASH0_SIZE, VIRT_FLASH0_BASE);
407
408 CPU_FOREACH(cs) {
409 lacpu = LOONGARCH_CPU(cs);
410 lacpu->env.load_elf = true;
411 if (cs == first_cpu) {
412 lacpu->env.elf_address = kernel_addr;
413 } else {
414 lacpu->env.elf_address = VIRT_FLASH0_BASE;
415 }
416 lacpu->env.boot_info = info;
417 }
418
419 g_free(boot_code);
420 g_free(bp);
421 }
422
loongarch_load_kernel(MachineState * ms,struct loongarch_boot_info * info)423 void loongarch_load_kernel(MachineState *ms, struct loongarch_boot_info *info)
424 {
425 LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(ms);
426 int i;
427
428 /* register reset function */
429 for (i = 0; i < ms->smp.cpus; i++) {
430 qemu_register_reset(reset_load_elf, LOONGARCH_CPU(qemu_get_cpu(i)));
431 }
432
433 info->kernel_filename = ms->kernel_filename;
434 info->kernel_cmdline = ms->kernel_cmdline;
435 info->initrd_filename = ms->initrd_filename;
436
437 if (lvms->bios_loaded) {
438 loongarch_firmware_boot(lvms, info);
439 } else {
440 loongarch_direct_kernel_boot(info);
441 }
442 }
443