1 /*-
2 * Copyright (c) 2014 Andrew Turner
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27
28 #include "opt_acpi.h"
29 #include "opt_kstack_pages.h"
30 #include "opt_platform.h"
31 #include "opt_ddb.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/asan.h>
36 #include <sys/buf.h>
37 #include <sys/bus.h>
38 #include <sys/cons.h>
39 #include <sys/cpu.h>
40 #include <sys/csan.h>
41 #include <sys/devmap.h>
42 #include <sys/efi.h>
43 #include <sys/efi_map.h>
44 #include <sys/exec.h>
45 #include <sys/imgact.h>
46 #include <sys/kdb.h>
47 #include <sys/kernel.h>
48 #include <sys/ktr.h>
49 #include <sys/limits.h>
50 #include <sys/linker.h>
51 #include <sys/msan.h>
52 #include <sys/msgbuf.h>
53 #include <sys/pcpu.h>
54 #include <sys/physmem.h>
55 #include <sys/proc.h>
56 #include <sys/ptrace.h>
57 #include <sys/reboot.h>
58 #include <sys/reg.h>
59 #include <sys/rwlock.h>
60 #include <sys/sched.h>
61 #include <sys/signalvar.h>
62 #include <sys/syscallsubr.h>
63 #include <sys/sysent.h>
64 #include <sys/sysproto.h>
65 #include <sys/ucontext.h>
66 #include <sys/vdso.h>
67 #include <sys/vmmeter.h>
68
69 #include <vm/vm.h>
70 #include <vm/vm_param.h>
71 #include <vm/vm_kern.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page.h>
74 #include <vm/vm_phys.h>
75 #include <vm/pmap.h>
76 #include <vm/vm_map.h>
77 #include <vm/vm_pager.h>
78
79 #include <machine/armreg.h>
80 #include <machine/cpu.h>
81 #include <machine/cpu_feat.h>
82 #include <machine/debug_monitor.h>
83 #include <machine/hypervisor.h>
84 #include <machine/kdb.h>
85 #include <machine/machdep.h>
86 #include <machine/metadata.h>
87 #include <machine/md_var.h>
88 #include <machine/pcb.h>
89 #include <machine/undefined.h>
90 #include <machine/vmparam.h>
91
92 #ifdef VFP
93 #include <machine/vfp.h>
94 #endif
95
96 #ifdef DEV_ACPI
97 #include <contrib/dev/acpica/include/acpi.h>
98 #include <machine/acpica_machdep.h>
99 #endif
100
101 #ifdef FDT
102 #include <dev/fdt/fdt_common.h>
103 #include <dev/ofw/openfirm.h>
104 #endif
105
106 #include <dev/smbios/smbios.h>
107
108 _Static_assert(sizeof(struct pcb) == 1248, "struct pcb is incorrect size");
109 _Static_assert(offsetof(struct pcb, pcb_fpusaved) == 136,
110 "pcb_fpusaved changed offset");
111 _Static_assert(offsetof(struct pcb, pcb_fpustate) == 192,
112 "pcb_fpustate changed offset");
113
114 enum arm64_bus arm64_bus_method = ARM64_BUS_NONE;
115
116 /*
117 * XXX: The .bss is assumed to be in the boot CPU NUMA domain. If not we
118 * could relocate this, but will need to keep the same virtual address as
119 * it's reverenced by the EARLY_COUNTER macro.
120 */
121 struct pcpu pcpu0;
122
123 #if defined(PERTHREAD_SSP)
124 /*
125 * The boot SSP canary. Will be replaced with a per-thread canary when
126 * scheduling has started.
127 */
128 uintptr_t boot_canary = 0x49a2d892bc05a0b1ul;
129 #endif
130
131 static struct trapframe proc0_tf;
132
133 int early_boot = 1;
134 int cold = 1;
135 static int boot_el;
136
137 struct kva_md_info kmi;
138
139 int64_t dczva_line_size; /* The size of cache line the dc zva zeroes */
140 int has_pan;
141
142 #if defined(SOCDEV_PA)
143 /*
144 * This is the virtual address used to access SOCDEV_PA. As it's set before
145 * .bss is cleared we need to ensure it's preserved. To do this use
146 * __read_mostly as it's only ever set once but read in the putc functions.
147 */
148 uintptr_t socdev_va __read_mostly;
149 #endif
150
151 /*
152 * Physical address of the EFI System Table. Stashed from the metadata hints
153 * passed into the kernel and used by the EFI code to call runtime services.
154 */
155 vm_paddr_t efi_systbl_phys;
156 static struct efi_map_header *efihdr;
157
158 /* pagezero_* implementations are provided in support.S */
159 void pagezero_simple(void *);
160 void pagezero_cache(void *);
161
162 /* pagezero_simple is default pagezero */
163 void (*pagezero)(void *p) = pagezero_simple;
164
165 int (*apei_nmi)(void);
166
167 #if defined(PERTHREAD_SSP_WARNING)
168 static void
print_ssp_warning(void * data __unused)169 print_ssp_warning(void *data __unused)
170 {
171 printf("WARNING: Per-thread SSP is enabled but the compiler is too old to support it\n");
172 }
173 SYSINIT(ssp_warn, SI_SUB_COPYRIGHT, SI_ORDER_ANY, print_ssp_warning, NULL);
174 SYSINIT(ssp_warn2, SI_SUB_LAST, SI_ORDER_ANY, print_ssp_warning, NULL);
175 #endif
176
177 static bool
pan_check(const struct cpu_feat * feat __unused,u_int midr __unused)178 pan_check(const struct cpu_feat *feat __unused, u_int midr __unused)
179 {
180 uint64_t id_aa64mfr1;
181
182 id_aa64mfr1 = READ_SPECIALREG(id_aa64mmfr1_el1);
183 return (ID_AA64MMFR1_PAN_VAL(id_aa64mfr1) != ID_AA64MMFR1_PAN_NONE);
184 }
185
186 static void
pan_enable(const struct cpu_feat * feat __unused,cpu_feat_errata errata_status __unused,u_int * errata_list __unused,u_int errata_count __unused)187 pan_enable(const struct cpu_feat *feat __unused,
188 cpu_feat_errata errata_status __unused, u_int *errata_list __unused,
189 u_int errata_count __unused)
190 {
191 has_pan = 1;
192
193 /*
194 * This sets the PAN bit, stopping the kernel from accessing
195 * memory when userspace can also access it unless the kernel
196 * uses the userspace load/store instructions.
197 */
198 WRITE_SPECIALREG(sctlr_el1,
199 READ_SPECIALREG(sctlr_el1) & ~SCTLR_SPAN);
200 __asm __volatile(
201 ".arch_extension pan \n"
202 "msr pan, #1 \n"
203 ".arch_extension nopan \n");
204 }
205
206 static struct cpu_feat feat_pan = {
207 .feat_name = "FEAT_PAN",
208 .feat_check = pan_check,
209 .feat_enable = pan_enable,
210 .feat_flags = CPU_FEAT_EARLY_BOOT | CPU_FEAT_PER_CPU,
211 };
212 DATA_SET(cpu_feat_set, feat_pan);
213
214 bool
has_hyp(void)215 has_hyp(void)
216 {
217 return (boot_el == CURRENTEL_EL_EL2);
218 }
219
220 bool
in_vhe(void)221 in_vhe(void)
222 {
223 /* If we are currently in EL2 then must be in VHE */
224 return ((READ_SPECIALREG(CurrentEL) & CURRENTEL_EL_MASK) ==
225 CURRENTEL_EL_EL2);
226 }
227
228 static void
cpu_startup(void * dummy)229 cpu_startup(void *dummy)
230 {
231 vm_paddr_t size;
232 int i;
233
234 printf("real memory = %ju (%ju MB)\n", ptoa((uintmax_t)realmem),
235 ptoa((uintmax_t)realmem) / 1024 / 1024);
236
237 if (bootverbose) {
238 printf("Physical memory chunk(s):\n");
239 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
240 size = phys_avail[i + 1] - phys_avail[i];
241 printf("%#016jx - %#016jx, %ju bytes (%ju pages)\n",
242 (uintmax_t)phys_avail[i],
243 (uintmax_t)phys_avail[i + 1] - 1,
244 (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
245 }
246 }
247
248 printf("avail memory = %ju (%ju MB)\n",
249 ptoa((uintmax_t)vm_free_count()),
250 ptoa((uintmax_t)vm_free_count()) / 1024 / 1024);
251
252 undef_init();
253 install_cpu_errata();
254
255 vm_ksubmap_init(&kmi);
256 bufinit();
257 vm_pager_bufferinit();
258 }
259
260 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
261
262 static void
late_ifunc_resolve(void * dummy __unused)263 late_ifunc_resolve(void *dummy __unused)
264 {
265 link_elf_late_ireloc();
266 }
267 SYSINIT(late_ifunc_resolve, SI_SUB_CPU, SI_ORDER_ANY, late_ifunc_resolve, NULL);
268
269 int
cpu_idle_wakeup(int cpu)270 cpu_idle_wakeup(int cpu)
271 {
272
273 return (0);
274 }
275
276 void
cpu_idle(int busy)277 cpu_idle(int busy)
278 {
279
280 spinlock_enter();
281 if (!busy)
282 cpu_idleclock();
283 if (!sched_runnable())
284 __asm __volatile(
285 "dsb sy \n"
286 "wfi \n");
287 if (!busy)
288 cpu_activeclock();
289 spinlock_exit();
290 }
291
292 void
cpu_halt(void)293 cpu_halt(void)
294 {
295
296 /* We should have shutdown by now, if not enter a low power sleep */
297 intr_disable();
298 while (1) {
299 __asm __volatile("wfi");
300 }
301 }
302
303 /*
304 * Flush the D-cache for non-DMA I/O so that the I-cache can
305 * be made coherent later.
306 */
307 void
cpu_flush_dcache(void * ptr,size_t len)308 cpu_flush_dcache(void *ptr, size_t len)
309 {
310
311 /* ARM64TODO TBD */
312 }
313
314 /* Get current clock frequency for the given CPU ID. */
315 int
cpu_est_clockrate(int cpu_id,uint64_t * rate)316 cpu_est_clockrate(int cpu_id, uint64_t *rate)
317 {
318 struct pcpu *pc;
319
320 pc = pcpu_find(cpu_id);
321 if (pc == NULL || rate == NULL)
322 return (EINVAL);
323
324 if (pc->pc_clock == 0)
325 return (EOPNOTSUPP);
326
327 *rate = pc->pc_clock;
328 return (0);
329 }
330
331 void
cpu_pcpu_init(struct pcpu * pcpu,int cpuid,size_t size)332 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
333 {
334
335 pcpu->pc_acpi_id = 0xffffffff;
336 pcpu->pc_mpidr = UINT64_MAX;
337 }
338
339 void
spinlock_enter(void)340 spinlock_enter(void)
341 {
342 struct thread *td;
343 register_t daif;
344
345 td = curthread;
346 if (td->td_md.md_spinlock_count == 0) {
347 daif = intr_disable();
348 td->td_md.md_spinlock_count = 1;
349 td->td_md.md_saved_daif = daif;
350 critical_enter();
351 } else
352 td->td_md.md_spinlock_count++;
353 }
354
355 void
spinlock_exit(void)356 spinlock_exit(void)
357 {
358 struct thread *td;
359 register_t daif;
360
361 td = curthread;
362 daif = td->td_md.md_saved_daif;
363 td->td_md.md_spinlock_count--;
364 if (td->td_md.md_spinlock_count == 0) {
365 critical_exit();
366 intr_restore(daif);
367 }
368 }
369
370 /*
371 * Construct a PCB from a trapframe. This is called from kdb_trap() where
372 * we want to start a backtrace from the function that caused us to enter
373 * the debugger. We have the context in the trapframe, but base the trace
374 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
375 * enough for a backtrace.
376 */
377 void
makectx(struct trapframe * tf,struct pcb * pcb)378 makectx(struct trapframe *tf, struct pcb *pcb)
379 {
380 int i;
381
382 /* NB: pcb_x[PCB_LR] is the PC, see PC_REGS() in db_machdep.h */
383 for (i = 0; i < nitems(pcb->pcb_x); i++) {
384 if (i == PCB_LR)
385 pcb->pcb_x[i] = tf->tf_elr;
386 else
387 pcb->pcb_x[i] = tf->tf_x[i + PCB_X_START];
388 }
389
390 pcb->pcb_sp = tf->tf_sp;
391 }
392
393 static void
init_proc0(vm_offset_t kstack)394 init_proc0(vm_offset_t kstack)
395 {
396 struct pcpu *pcpup;
397
398 pcpup = cpuid_to_pcpu[0];
399 MPASS(pcpup != NULL);
400
401 proc_linkup0(&proc0, &thread0);
402 thread0.td_kstack = kstack;
403 thread0.td_kstack_pages = KSTACK_PAGES;
404 #if defined(PERTHREAD_SSP)
405 thread0.td_md.md_canary = boot_canary;
406 #endif
407 thread0.td_pcb = (struct pcb *)(thread0.td_kstack +
408 thread0.td_kstack_pages * PAGE_SIZE) - 1;
409 thread0.td_pcb->pcb_flags = 0;
410 thread0.td_pcb->pcb_fpflags = 0;
411 thread0.td_pcb->pcb_fpusaved = &thread0.td_pcb->pcb_fpustate;
412 thread0.td_pcb->pcb_vfpcpu = UINT_MAX;
413 thread0.td_frame = &proc0_tf;
414 ptrauth_thread0(&thread0);
415 pcpup->pc_curpcb = thread0.td_pcb;
416
417 /*
418 * Unmask SError exceptions. They are used to signal a RAS failure,
419 * or other hardware error.
420 */
421 serror_enable();
422 }
423
424 /*
425 * Get an address to be used to write to kernel data that may be mapped
426 * read-only, e.g. to patch kernel code.
427 */
428 bool
arm64_get_writable_addr(void * addr,void ** out)429 arm64_get_writable_addr(void *addr, void **out)
430 {
431 vm_paddr_t pa;
432
433 /* Check if the page is writable */
434 if (PAR_SUCCESS(arm64_address_translate_s1e1w((vm_offset_t)addr))) {
435 *out = addr;
436 return (true);
437 }
438
439 /*
440 * Find the physical address of the given page.
441 */
442 if (!pmap_klookup((vm_offset_t)addr, &pa)) {
443 return (false);
444 }
445
446 /*
447 * If it is within the DMAP region and is writable use that.
448 */
449 if (PHYS_IN_DMAP_RANGE(pa)) {
450 addr = (void *)PHYS_TO_DMAP(pa);
451 if (PAR_SUCCESS(arm64_address_translate_s1e1w(
452 (vm_offset_t)addr))) {
453 *out = addr;
454 return (true);
455 }
456 }
457
458 return (false);
459 }
460
461 /*
462 * Map the passed in VA in EFI space to a void * using the efi memory table to
463 * find the PA and return it in the DMAP, if it exists. We're used between the
464 * calls to pmap_bootstrap() and physmem_init_kernel_globals() to parse CFG
465 * tables We assume that either the entry you are mapping fits within its page,
466 * or if it spills to the next page, that's contiguous in PA and in the DMAP.
467 * All observed tables obey the first part of this precondition.
468 */
469 struct early_map_data
470 {
471 vm_offset_t va;
472 vm_offset_t pa;
473 };
474
475 static void
efi_early_map_entry(struct efi_md * p,void * argp)476 efi_early_map_entry(struct efi_md *p, void *argp)
477 {
478 struct early_map_data *emdp = argp;
479 vm_offset_t s, e;
480
481 if (emdp->pa != 0)
482 return;
483 if ((p->md_attr & EFI_MD_ATTR_RT) == 0)
484 return;
485 s = p->md_virt;
486 e = p->md_virt + p->md_pages * EFI_PAGE_SIZE;
487 if (emdp->va < s || emdp->va >= e)
488 return;
489 emdp->pa = p->md_phys + (emdp->va - p->md_virt);
490 }
491
492 static void *
efi_early_map(vm_offset_t va)493 efi_early_map(vm_offset_t va)
494 {
495 struct early_map_data emd = { .va = va };
496
497 efi_map_foreach_entry(efihdr, efi_early_map_entry, &emd);
498 if (emd.pa == 0)
499 return NULL;
500 return (void *)PHYS_TO_DMAP(emd.pa);
501 }
502
503
504 /*
505 * When booted via kboot, the prior kernel will pass in reserved memory areas in
506 * a EFI config table. We need to find that table and walk through it excluding
507 * the memory ranges in it. btw, this is called too early for the printf to do
508 * anything since msgbufp isn't initialized, let alone a console...
509 */
510 static void
exclude_efi_memreserve(vm_paddr_t efi_systbl_phys)511 exclude_efi_memreserve(vm_paddr_t efi_systbl_phys)
512 {
513 struct efi_systbl *systbl;
514 struct uuid efi_memreserve = LINUX_EFI_MEMRESERVE_TABLE;
515
516 systbl = (struct efi_systbl *)PHYS_TO_DMAP(efi_systbl_phys);
517 if (systbl == NULL) {
518 printf("can't map systbl\n");
519 return;
520 }
521 if (systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) {
522 printf("Bad signature for systbl %#lx\n", systbl->st_hdr.th_sig);
523 return;
524 }
525
526 /*
527 * We don't yet have the pmap system booted enough to create a pmap for
528 * the efi firmware's preferred address space from the GetMemoryMap()
529 * table. The st_cfgtbl is a VA in this space, so we need to do the
530 * mapping ourselves to a kernel VA with efi_early_map. We assume that
531 * the cfgtbl entries don't span a page. Other pointers are PAs, as
532 * noted below.
533 */
534 if (systbl->st_cfgtbl == 0) /* Failsafe st_entries should == 0 in this case */
535 return;
536 for (int i = 0; i < systbl->st_entries; i++) {
537 struct efi_cfgtbl *cfgtbl;
538 struct linux_efi_memreserve *mr;
539
540 cfgtbl = efi_early_map(systbl->st_cfgtbl + i * sizeof(*cfgtbl));
541 if (cfgtbl == NULL)
542 panic("Can't map the config table entry %d\n", i);
543 if (memcmp(&cfgtbl->ct_uuid, &efi_memreserve, sizeof(struct uuid)) != 0)
544 continue;
545
546 /*
547 * cfgtbl points are either VA or PA, depending on the GUID of
548 * the table. memreserve GUID pointers are PA and not converted
549 * after a SetVirtualAddressMap(). The list's mr_next pointer
550 * is also a PA.
551 */
552 mr = (struct linux_efi_memreserve *)PHYS_TO_DMAP(
553 (vm_offset_t)cfgtbl->ct_data);
554 while (true) {
555 for (int j = 0; j < mr->mr_count; j++) {
556 struct linux_efi_memreserve_entry *mre;
557
558 mre = &mr->mr_entry[j];
559 physmem_exclude_region(mre->mre_base, mre->mre_size,
560 EXFLAG_NODUMP | EXFLAG_NOALLOC);
561 }
562 if (mr->mr_next == 0)
563 break;
564 mr = (struct linux_efi_memreserve *)PHYS_TO_DMAP(mr->mr_next);
565 };
566 }
567
568 }
569
570 #ifdef FDT
571 static void
try_load_dtb(void)572 try_load_dtb(void)
573 {
574 vm_offset_t dtbp;
575
576 dtbp = MD_FETCH(preload_kmdp, MODINFOMD_DTBP, vm_offset_t);
577 #if defined(FDT_DTB_STATIC)
578 /*
579 * In case the device tree blob was not retrieved (from metadata) try
580 * to use the statically embedded one.
581 */
582 if (dtbp == 0)
583 dtbp = (vm_offset_t)&fdt_static_dtb;
584 #endif
585
586 if (dtbp == (vm_offset_t)NULL) {
587 #ifndef TSLOG
588 printf("ERROR loading DTB\n");
589 #endif
590 return;
591 }
592
593 if (!OF_install(OFW_FDT, 0))
594 panic("Cannot install FDT");
595
596 if (OF_init((void *)dtbp) != 0)
597 panic("OF_init failed with the found device tree");
598
599 parse_fdt_bootargs();
600 }
601 #endif
602
603 static bool
bus_probe(void)604 bus_probe(void)
605 {
606 bool has_acpi, has_fdt;
607 char *order, *env;
608
609 has_acpi = has_fdt = false;
610
611 #ifdef FDT
612 has_fdt = (OF_peer(0) != 0);
613 #endif
614 #ifdef DEV_ACPI
615 has_acpi = (AcpiOsGetRootPointer() != 0);
616 #endif
617
618 env = kern_getenv("kern.cfg.order");
619 if (env != NULL) {
620 order = env;
621 while (order != NULL) {
622 if (has_acpi &&
623 strncmp(order, "acpi", 4) == 0 &&
624 (order[4] == ',' || order[4] == '\0')) {
625 arm64_bus_method = ARM64_BUS_ACPI;
626 break;
627 }
628 if (has_fdt &&
629 strncmp(order, "fdt", 3) == 0 &&
630 (order[3] == ',' || order[3] == '\0')) {
631 arm64_bus_method = ARM64_BUS_FDT;
632 break;
633 }
634 order = strchr(order, ',');
635 if (order != NULL)
636 order++; /* Skip comma */
637 }
638 freeenv(env);
639
640 /* If we set the bus method it is valid */
641 if (arm64_bus_method != ARM64_BUS_NONE)
642 return (true);
643 }
644 /* If no order or an invalid order was set use the default */
645 if (arm64_bus_method == ARM64_BUS_NONE) {
646 if (has_acpi)
647 arm64_bus_method = ARM64_BUS_ACPI;
648 else if (has_fdt)
649 arm64_bus_method = ARM64_BUS_FDT;
650 }
651
652 /*
653 * If no option was set the default is valid, otherwise we are
654 * setting one to get cninit() working, then calling panic to tell
655 * the user about the invalid bus setup.
656 */
657 return (env == NULL);
658 }
659
660 static void
cache_setup(void)661 cache_setup(void)
662 {
663 int dczva_line_shift;
664 uint32_t dczid_el0;
665
666 identify_cache(READ_SPECIALREG(ctr_el0));
667
668 dczid_el0 = READ_SPECIALREG(dczid_el0);
669
670 /* Check if dc zva is not prohibited */
671 if (dczid_el0 & DCZID_DZP)
672 dczva_line_size = 0;
673 else {
674 /* Same as with above calculations */
675 dczva_line_shift = DCZID_BS_SIZE(dczid_el0);
676 dczva_line_size = sizeof(int) << dczva_line_shift;
677
678 /* Change pagezero function */
679 pagezero = pagezero_cache;
680 }
681 }
682
683 int
memory_mapping_mode(vm_paddr_t pa)684 memory_mapping_mode(vm_paddr_t pa)
685 {
686 struct efi_md *map, *p;
687 size_t efisz;
688 int ndesc, i;
689
690 if (efihdr == NULL)
691 return (VM_MEMATTR_WRITE_BACK);
692
693 /*
694 * Memory map data provided by UEFI via the GetMemoryMap
695 * Boot Services API.
696 */
697 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
698 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
699
700 if (efihdr->descriptor_size == 0)
701 return (VM_MEMATTR_WRITE_BACK);
702 ndesc = efihdr->memory_size / efihdr->descriptor_size;
703
704 for (i = 0, p = map; i < ndesc; i++,
705 p = efi_next_descriptor(p, efihdr->descriptor_size)) {
706 if (pa < p->md_phys ||
707 pa >= p->md_phys + p->md_pages * EFI_PAGE_SIZE)
708 continue;
709 if (p->md_type == EFI_MD_TYPE_IOMEM ||
710 p->md_type == EFI_MD_TYPE_IOPORT)
711 return (VM_MEMATTR_DEVICE);
712 else if ((p->md_attr & EFI_MD_ATTR_WB) != 0 ||
713 p->md_type == EFI_MD_TYPE_RECLAIM)
714 return (VM_MEMATTR_WRITE_BACK);
715 else if ((p->md_attr & EFI_MD_ATTR_WT) != 0)
716 return (VM_MEMATTR_WRITE_THROUGH);
717 else if ((p->md_attr & EFI_MD_ATTR_WC) != 0)
718 return (VM_MEMATTR_WRITE_COMBINING);
719 break;
720 }
721
722 return (VM_MEMATTR_DEVICE);
723 }
724
725 void
initarm(struct arm64_bootparams * abp)726 initarm(struct arm64_bootparams *abp)
727 {
728 struct efi_fb *efifb;
729 struct pcpu *pcpup;
730 char *env;
731 #ifdef FDT
732 struct mem_region mem_regions[FDT_MEM_REGIONS];
733 int mem_regions_sz;
734 phandle_t root;
735 char dts_version[255];
736 #endif
737 vm_offset_t lastaddr;
738 bool valid;
739
740 TSRAW(&thread0, TS_ENTER, __func__, NULL);
741
742 boot_el = abp->boot_el;
743
744 /* Parse loader or FDT boot parameters. Determine last used address. */
745 lastaddr = parse_boot_param(abp);
746
747 identify_cpu(0);
748 identify_hypervisor_smbios();
749
750 update_special_regs(0);
751
752 /* Set the pcpu data, this is needed by pmap_bootstrap */
753 pcpup = &pcpu0;
754 pcpu_init(pcpup, 0, sizeof(struct pcpu));
755
756 /*
757 * Set the pcpu pointer with a backup in tpidr_el1 to be
758 * loaded when entering the kernel from userland.
759 */
760 __asm __volatile(
761 "mov x18, %0 \n"
762 "msr tpidr_el1, %0" :: "r"(pcpup));
763
764 /* locore.S sets sp_el0 to &thread0 so no need to set it here. */
765 PCPU_SET(curthread, &thread0);
766 PCPU_SET(midr, get_midr());
767
768 link_elf_ireloc();
769 #ifdef FDT
770 try_load_dtb();
771 #endif
772
773 efi_systbl_phys = MD_FETCH(preload_kmdp, MODINFOMD_FW_HANDLE,
774 vm_paddr_t);
775
776 /* Load the physical memory ranges */
777 efihdr = (struct efi_map_header *)preload_search_info(preload_kmdp,
778 MODINFO_METADATA | MODINFOMD_EFI_MAP);
779 if (efihdr != NULL)
780 efi_map_add_entries(efihdr);
781 #ifdef FDT
782 else {
783 /* Grab physical memory regions information from device tree. */
784 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz,
785 NULL) != 0)
786 panic("Cannot get physical memory regions");
787 physmem_hardware_regions(mem_regions, mem_regions_sz);
788 }
789 if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0)
790 physmem_exclude_regions(mem_regions, mem_regions_sz,
791 EXFLAG_NODUMP | EXFLAG_NOALLOC);
792 #endif
793
794 /* Exclude the EFI framebuffer from our view of physical memory. */
795 efifb = (struct efi_fb *)preload_search_info(preload_kmdp,
796 MODINFO_METADATA | MODINFOMD_EFI_FB);
797 if (efifb != NULL)
798 physmem_exclude_region(efifb->fb_addr, efifb->fb_size,
799 EXFLAG_NOALLOC);
800
801 /* Do basic tuning, hz etc */
802 init_param1();
803
804 cache_setup();
805
806 /* Bootstrap enough of pmap to enter the kernel proper */
807 pmap_bootstrap(lastaddr - KERNBASE);
808 /* Exclude entries needed in the DMAP region, but not phys_avail */
809 if (efihdr != NULL)
810 efi_map_exclude_entries(efihdr);
811 /* Do the same for reserve entries in the EFI MEMRESERVE table */
812 if (efi_systbl_phys != 0)
813 exclude_efi_memreserve(efi_systbl_phys);
814
815 /*
816 * We carefully bootstrap the sanitizer map after we've excluded
817 * absolutely everything else that could impact phys_avail. There's not
818 * always enough room for the initial shadow map after the kernel, so
819 * we'll end up searching for segments that we can safely use. Those
820 * segments also get excluded from phys_avail.
821 */
822 #if defined(KASAN) || defined(KMSAN)
823 pmap_bootstrap_san();
824 #endif
825
826 physmem_init_kernel_globals();
827
828 devmap_bootstrap();
829
830 valid = bus_probe();
831
832 cninit();
833 set_ttbr0(abp->kern_ttbr0);
834 cpu_tlb_flushID();
835
836 if (!valid)
837 panic("Invalid bus configuration: %s",
838 kern_getenv("kern.cfg.order"));
839
840 /* Detect early CPU feature support */
841 enable_cpu_feat(CPU_FEAT_EARLY_BOOT);
842
843 /*
844 * Dump the boot metadata. We have to wait for cninit() since console
845 * output is required. If it's grossly incorrect the kernel will never
846 * make it this far.
847 */
848 if (getenv_is_true("debug.dump_modinfo_at_boot"))
849 preload_dump();
850
851 init_proc0(abp->kern_stack);
852 msgbufinit(msgbufp, msgbufsize);
853 mutex_init();
854 init_param2(physmem);
855
856 dbg_init();
857 kdb_init();
858 #ifdef KDB
859 if ((boothowto & RB_KDB) != 0)
860 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
861 #endif
862
863 kcsan_cpu_init(0);
864 kasan_init();
865 kmsan_init();
866
867 env = kern_getenv("kernelname");
868 if (env != NULL)
869 strlcpy(kernelname, env, sizeof(kernelname));
870
871 #ifdef FDT
872 if (arm64_bus_method == ARM64_BUS_FDT) {
873 root = OF_finddevice("/");
874 if (OF_getprop(root, "freebsd,dts-version", dts_version, sizeof(dts_version)) > 0) {
875 if (strcmp(LINUX_DTS_VERSION, dts_version) != 0)
876 printf("WARNING: DTB version is %s while kernel expects %s, "
877 "please update the DTB in the ESP\n",
878 dts_version,
879 LINUX_DTS_VERSION);
880 } else {
881 printf("WARNING: Cannot find freebsd,dts-version property, "
882 "cannot check DTB compliance\n");
883 }
884 }
885 #endif
886
887 if (boothowto & RB_VERBOSE) {
888 if (efihdr != NULL)
889 efi_map_print_entries(efihdr);
890 physmem_print_tables();
891 }
892
893 early_boot = 0;
894
895 if (bootverbose && kstack_pages != KSTACK_PAGES)
896 printf("kern.kstack_pages = %d ignored for thread0\n",
897 kstack_pages);
898
899 TSEXIT();
900 }
901
902 void
dbg_init(void)903 dbg_init(void)
904 {
905
906 /* Clear OS lock */
907 WRITE_SPECIALREG(oslar_el1, 0);
908
909 /* This permits DDB to use debug registers for watchpoints. */
910 dbg_monitor_init();
911
912 /* TODO: Eventually will need to initialize debug registers here. */
913 }
914
915 #ifdef DDB
916 #include <ddb/ddb.h>
917
DB_SHOW_COMMAND(specialregs,db_show_spregs)918 DB_SHOW_COMMAND(specialregs, db_show_spregs)
919 {
920 #define PRINT_REG(reg) \
921 db_printf(__STRING(reg) " = %#016lx\n", READ_SPECIALREG(reg))
922
923 PRINT_REG(actlr_el1);
924 PRINT_REG(afsr0_el1);
925 PRINT_REG(afsr1_el1);
926 PRINT_REG(aidr_el1);
927 PRINT_REG(amair_el1);
928 PRINT_REG(ccsidr_el1);
929 PRINT_REG(clidr_el1);
930 PRINT_REG(contextidr_el1);
931 PRINT_REG(cpacr_el1);
932 PRINT_REG(csselr_el1);
933 PRINT_REG(ctr_el0);
934 PRINT_REG(currentel);
935 PRINT_REG(daif);
936 PRINT_REG(dczid_el0);
937 PRINT_REG(elr_el1);
938 PRINT_REG(esr_el1);
939 PRINT_REG(far_el1);
940 #if 0
941 /* ARM64TODO: Enable VFP before reading floating-point registers */
942 PRINT_REG(fpcr);
943 PRINT_REG(fpsr);
944 #endif
945 PRINT_REG(id_aa64afr0_el1);
946 PRINT_REG(id_aa64afr1_el1);
947 PRINT_REG(id_aa64dfr0_el1);
948 PRINT_REG(id_aa64dfr1_el1);
949 PRINT_REG(id_aa64isar0_el1);
950 PRINT_REG(id_aa64isar1_el1);
951 PRINT_REG(id_aa64pfr0_el1);
952 PRINT_REG(id_aa64pfr1_el1);
953 PRINT_REG(id_afr0_el1);
954 PRINT_REG(id_dfr0_el1);
955 PRINT_REG(id_isar0_el1);
956 PRINT_REG(id_isar1_el1);
957 PRINT_REG(id_isar2_el1);
958 PRINT_REG(id_isar3_el1);
959 PRINT_REG(id_isar4_el1);
960 PRINT_REG(id_isar5_el1);
961 PRINT_REG(id_mmfr0_el1);
962 PRINT_REG(id_mmfr1_el1);
963 PRINT_REG(id_mmfr2_el1);
964 PRINT_REG(id_mmfr3_el1);
965 #if 0
966 /* Missing from llvm */
967 PRINT_REG(id_mmfr4_el1);
968 #endif
969 PRINT_REG(id_pfr0_el1);
970 PRINT_REG(id_pfr1_el1);
971 PRINT_REG(isr_el1);
972 PRINT_REG(mair_el1);
973 PRINT_REG(midr_el1);
974 PRINT_REG(mpidr_el1);
975 PRINT_REG(mvfr0_el1);
976 PRINT_REG(mvfr1_el1);
977 PRINT_REG(mvfr2_el1);
978 PRINT_REG(revidr_el1);
979 PRINT_REG(sctlr_el1);
980 PRINT_REG(sp_el0);
981 PRINT_REG(spsel);
982 PRINT_REG(spsr_el1);
983 PRINT_REG(tcr_el1);
984 PRINT_REG(tpidr_el0);
985 PRINT_REG(tpidr_el1);
986 PRINT_REG(tpidrro_el0);
987 PRINT_REG(ttbr0_el1);
988 PRINT_REG(ttbr1_el1);
989 PRINT_REG(vbar_el1);
990 #undef PRINT_REG
991 }
992
DB_SHOW_COMMAND(vtop,db_show_vtop)993 DB_SHOW_COMMAND(vtop, db_show_vtop)
994 {
995 uint64_t phys;
996
997 if (have_addr) {
998 phys = arm64_address_translate_s1e1r(addr);
999 db_printf("EL1 physical address reg (read): 0x%016lx\n", phys);
1000 phys = arm64_address_translate_s1e1w(addr);
1001 db_printf("EL1 physical address reg (write): 0x%016lx\n", phys);
1002 phys = arm64_address_translate_s1e0r(addr);
1003 db_printf("EL0 physical address reg (read): 0x%016lx\n", phys);
1004 phys = arm64_address_translate_s1e0w(addr);
1005 db_printf("EL0 physical address reg (write): 0x%016lx\n", phys);
1006 } else
1007 db_printf("show vtop <virt_addr>\n");
1008 }
1009 #endif
1010