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