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