]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/machdep.c
Allocate arm64 per-CPU data in the correct domain
[FreeBSD/FreeBSD.git] / sys / arm64 / arm64 / machdep.c
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_platform.h"
30 #include "opt_ddb.h"
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/buf.h>
38 #include <sys/bus.h>
39 #include <sys/cons.h>
40 #include <sys/cpu.h>
41 #include <sys/csan.h>
42 #include <sys/devmap.h>
43 #include <sys/efi.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/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/rwlock.h>
58 #include <sys/sched.h>
59 #include <sys/signalvar.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/sysent.h>
62 #include <sys/sysproto.h>
63 #include <sys/ucontext.h>
64 #include <sys/vdso.h>
65 #include <sys/vmmeter.h>
66
67 #include <vm/vm.h>
68 #include <vm/vm_param.h>
69 #include <vm/vm_kern.h>
70 #include <vm/vm_object.h>
71 #include <vm/vm_page.h>
72 #include <vm/vm_phys.h>
73 #include <vm/pmap.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_pager.h>
76
77 #include <machine/armreg.h>
78 #include <machine/cpu.h>
79 #include <machine/debug_monitor.h>
80 #include <machine/kdb.h>
81 #include <machine/machdep.h>
82 #include <machine/metadata.h>
83 #include <machine/md_var.h>
84 #include <machine/pcb.h>
85 #include <machine/reg.h>
86 #include <machine/undefined.h>
87 #include <machine/vmparam.h>
88
89 #ifdef VFP
90 #include <machine/vfp.h>
91 #endif
92
93 #ifdef DEV_ACPI
94 #include <contrib/dev/acpica/include/acpi.h>
95 #include <machine/acpica_machdep.h>
96 #endif
97
98 #ifdef FDT
99 #include <dev/fdt/fdt_common.h>
100 #include <dev/ofw/openfirm.h>
101 #endif
102
103 enum arm64_bus arm64_bus_method = ARM64_BUS_NONE;
104
105 /*
106  * XXX: The .bss is assumed to be in the boot CPU NUMA domain. If not we
107  * could relocate this, but will need to keep the same virtual address as
108  * it's reverenced by the EARLY_COUNTER macro.
109  */
110 struct pcpu pcpu0;
111
112 static struct trapframe proc0_tf;
113
114 int early_boot = 1;
115 int cold = 1;
116 static int boot_el;
117
118 struct kva_md_info kmi;
119
120 int64_t dczva_line_size;        /* The size of cache line the dc zva zeroes */
121 int has_pan;
122
123 /*
124  * Physical address of the EFI System Table. Stashed from the metadata hints
125  * passed into the kernel and used by the EFI code to call runtime services.
126  */
127 vm_paddr_t efi_systbl_phys;
128 static struct efi_map_header *efihdr;
129
130 /* pagezero_* implementations are provided in support.S */
131 void pagezero_simple(void *);
132 void pagezero_cache(void *);
133
134 /* pagezero_simple is default pagezero */
135 void (*pagezero)(void *p) = pagezero_simple;
136
137 int (*apei_nmi)(void);
138
139 static void
140 pan_setup(void)
141 {
142         uint64_t id_aa64mfr1;
143
144         id_aa64mfr1 = READ_SPECIALREG(id_aa64mmfr1_el1);
145         if (ID_AA64MMFR1_PAN_VAL(id_aa64mfr1) != ID_AA64MMFR1_PAN_NONE)
146                 has_pan = 1;
147 }
148
149 void
150 pan_enable(void)
151 {
152
153         /*
154          * The LLVM integrated assembler doesn't understand the PAN
155          * PSTATE field. Because of this we need to manually create
156          * the instruction in an asm block. This is equivalent to:
157          * msr pan, #1
158          *
159          * This sets the PAN bit, stopping the kernel from accessing
160          * memory when userspace can also access it unless the kernel
161          * uses the userspace load/store instructions.
162          */
163         if (has_pan) {
164                 WRITE_SPECIALREG(sctlr_el1,
165                     READ_SPECIALREG(sctlr_el1) & ~SCTLR_SPAN);
166                 __asm __volatile(".inst 0xd500409f | (0x1 << 8)");
167         }
168 }
169
170 bool
171 has_hyp(void)
172 {
173
174         return (boot_el == 2);
175 }
176
177 static void
178 cpu_startup(void *dummy)
179 {
180         vm_paddr_t size;
181         int i;
182
183         printf("real memory  = %ju (%ju MB)\n", ptoa((uintmax_t)realmem),
184             ptoa((uintmax_t)realmem) / 1024 / 1024);
185
186         if (bootverbose) {
187                 printf("Physical memory chunk(s):\n");
188                 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
189                         size = phys_avail[i + 1] - phys_avail[i];
190                         printf("%#016jx - %#016jx, %ju bytes (%ju pages)\n",
191                             (uintmax_t)phys_avail[i],
192                             (uintmax_t)phys_avail[i + 1] - 1,
193                             (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
194                 }
195         }
196
197         printf("avail memory = %ju (%ju MB)\n",
198             ptoa((uintmax_t)vm_free_count()),
199             ptoa((uintmax_t)vm_free_count()) / 1024 / 1024);
200
201         undef_init();
202         install_cpu_errata();
203
204         vm_ksubmap_init(&kmi);
205         bufinit();
206         vm_pager_bufferinit();
207 }
208
209 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
210
211 static void
212 late_ifunc_resolve(void *dummy __unused)
213 {
214         link_elf_late_ireloc();
215 }
216 SYSINIT(late_ifunc_resolve, SI_SUB_CPU, SI_ORDER_ANY, late_ifunc_resolve, NULL);
217
218 int
219 cpu_idle_wakeup(int cpu)
220 {
221
222         return (0);
223 }
224
225 void
226 cpu_idle(int busy)
227 {
228
229         spinlock_enter();
230         if (!busy)
231                 cpu_idleclock();
232         if (!sched_runnable())
233                 __asm __volatile(
234                     "dsb sy \n"
235                     "wfi    \n");
236         if (!busy)
237                 cpu_activeclock();
238         spinlock_exit();
239 }
240
241 void
242 cpu_halt(void)
243 {
244
245         /* We should have shutdown by now, if not enter a low power sleep */
246         intr_disable();
247         while (1) {
248                 __asm __volatile("wfi");
249         }
250 }
251
252 /*
253  * Flush the D-cache for non-DMA I/O so that the I-cache can
254  * be made coherent later.
255  */
256 void
257 cpu_flush_dcache(void *ptr, size_t len)
258 {
259
260         /* ARM64TODO TBD */
261 }
262
263 /* Get current clock frequency for the given CPU ID. */
264 int
265 cpu_est_clockrate(int cpu_id, uint64_t *rate)
266 {
267         struct pcpu *pc;
268
269         pc = pcpu_find(cpu_id);
270         if (pc == NULL || rate == NULL)
271                 return (EINVAL);
272
273         if (pc->pc_clock == 0)
274                 return (EOPNOTSUPP);
275
276         *rate = pc->pc_clock;
277         return (0);
278 }
279
280 void
281 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
282 {
283
284         pcpu->pc_acpi_id = 0xffffffff;
285         pcpu->pc_mpidr = 0xffffffff;
286 }
287
288 void
289 spinlock_enter(void)
290 {
291         struct thread *td;
292         register_t daif;
293
294         td = curthread;
295         if (td->td_md.md_spinlock_count == 0) {
296                 daif = intr_disable();
297                 td->td_md.md_spinlock_count = 1;
298                 td->td_md.md_saved_daif = daif;
299                 critical_enter();
300         } else
301                 td->td_md.md_spinlock_count++;
302 }
303
304 void
305 spinlock_exit(void)
306 {
307         struct thread *td;
308         register_t daif;
309
310         td = curthread;
311         daif = td->td_md.md_saved_daif;
312         td->td_md.md_spinlock_count--;
313         if (td->td_md.md_spinlock_count == 0) {
314                 critical_exit();
315                 intr_restore(daif);
316         }
317 }
318
319 /*
320  * Construct a PCB from a trapframe. This is called from kdb_trap() where
321  * we want to start a backtrace from the function that caused us to enter
322  * the debugger. We have the context in the trapframe, but base the trace
323  * on the PCB. The PCB doesn't have to be perfect, as long as it contains
324  * enough for a backtrace.
325  */
326 void
327 makectx(struct trapframe *tf, struct pcb *pcb)
328 {
329         int i;
330
331         for (i = 0; i < nitems(pcb->pcb_x); i++)
332                 pcb->pcb_x[i] = tf->tf_x[i];
333
334         /* NB: pcb_lr is the PC, see PC_REGS() in db_machdep.h */
335         pcb->pcb_lr = tf->tf_elr;
336         pcb->pcb_sp = tf->tf_sp;
337 }
338
339 static void
340 init_proc0(vm_offset_t kstack)
341 {
342         struct pcpu *pcpup;
343
344         pcpup = cpuid_to_pcpu[0];
345         MPASS(pcpup != NULL);
346
347         proc_linkup0(&proc0, &thread0);
348         thread0.td_kstack = kstack;
349         thread0.td_kstack_pages = KSTACK_PAGES;
350         thread0.td_pcb = (struct pcb *)(thread0.td_kstack +
351             thread0.td_kstack_pages * PAGE_SIZE) - 1;
352         thread0.td_pcb->pcb_fpflags = 0;
353         thread0.td_pcb->pcb_fpusaved = &thread0.td_pcb->pcb_fpustate;
354         thread0.td_pcb->pcb_vfpcpu = UINT_MAX;
355         thread0.td_frame = &proc0_tf;
356         pcpup->pc_curpcb = thread0.td_pcb;
357
358         /*
359          * Unmask SError exceptions. They are used to signal a RAS failure,
360          * or other hardware error.
361          */
362         serror_enable();
363 }
364
365 typedef struct {
366         uint32_t type;
367         uint64_t phys_start;
368         uint64_t virt_start;
369         uint64_t num_pages;
370         uint64_t attr;
371 } EFI_MEMORY_DESCRIPTOR;
372
373 typedef void (*efi_map_entry_cb)(struct efi_md *);
374
375 static void
376 foreach_efi_map_entry(struct efi_map_header *efihdr, efi_map_entry_cb cb)
377 {
378         struct efi_md *map, *p;
379         size_t efisz;
380         int ndesc, i;
381
382         /*
383          * Memory map data provided by UEFI via the GetMemoryMap
384          * Boot Services API.
385          */
386         efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
387         map = (struct efi_md *)((uint8_t *)efihdr + efisz); 
388
389         if (efihdr->descriptor_size == 0)
390                 return;
391         ndesc = efihdr->memory_size / efihdr->descriptor_size;
392
393         for (i = 0, p = map; i < ndesc; i++,
394             p = efi_next_descriptor(p, efihdr->descriptor_size)) {
395                 cb(p);
396         }
397 }
398
399 static void
400 exclude_efi_map_entry(struct efi_md *p)
401 {
402
403         switch (p->md_type) {
404         case EFI_MD_TYPE_CODE:
405         case EFI_MD_TYPE_DATA:
406         case EFI_MD_TYPE_BS_CODE:
407         case EFI_MD_TYPE_BS_DATA:
408         case EFI_MD_TYPE_FREE:
409                 /*
410                  * We're allowed to use any entry with these types.
411                  */
412                 break;
413         default:
414                 physmem_exclude_region(p->md_phys, p->md_pages * PAGE_SIZE,
415                     EXFLAG_NOALLOC);
416         }
417 }
418
419 static void
420 exclude_efi_map_entries(struct efi_map_header *efihdr)
421 {
422
423         foreach_efi_map_entry(efihdr, exclude_efi_map_entry);
424 }
425
426 static void
427 add_efi_map_entry(struct efi_md *p)
428 {
429
430         switch (p->md_type) {
431         case EFI_MD_TYPE_RT_DATA:
432                 /*
433                  * Runtime data will be excluded after the DMAP
434                  * region is created to stop it from being added
435                  * to phys_avail.
436                  */
437         case EFI_MD_TYPE_CODE:
438         case EFI_MD_TYPE_DATA:
439         case EFI_MD_TYPE_BS_CODE:
440         case EFI_MD_TYPE_BS_DATA:
441         case EFI_MD_TYPE_FREE:
442                 /*
443                  * We're allowed to use any entry with these types.
444                  */
445                 physmem_hardware_region(p->md_phys,
446                     p->md_pages * PAGE_SIZE);
447                 break;
448         }
449 }
450
451 static void
452 add_efi_map_entries(struct efi_map_header *efihdr)
453 {
454
455         foreach_efi_map_entry(efihdr, add_efi_map_entry);
456 }
457
458 static void
459 print_efi_map_entry(struct efi_md *p)
460 {
461         const char *type;
462         static const char *types[] = {
463                 "Reserved",
464                 "LoaderCode",
465                 "LoaderData",
466                 "BootServicesCode",
467                 "BootServicesData",
468                 "RuntimeServicesCode",
469                 "RuntimeServicesData",
470                 "ConventionalMemory",
471                 "UnusableMemory",
472                 "ACPIReclaimMemory",
473                 "ACPIMemoryNVS",
474                 "MemoryMappedIO",
475                 "MemoryMappedIOPortSpace",
476                 "PalCode",
477                 "PersistentMemory"
478         };
479
480         if (p->md_type < nitems(types))
481                 type = types[p->md_type];
482         else
483                 type = "<INVALID>";
484         printf("%23s %012lx %12p %08lx ", type, p->md_phys,
485             p->md_virt, p->md_pages);
486         if (p->md_attr & EFI_MD_ATTR_UC)
487                 printf("UC ");
488         if (p->md_attr & EFI_MD_ATTR_WC)
489                 printf("WC ");
490         if (p->md_attr & EFI_MD_ATTR_WT)
491                 printf("WT ");
492         if (p->md_attr & EFI_MD_ATTR_WB)
493                 printf("WB ");
494         if (p->md_attr & EFI_MD_ATTR_UCE)
495                 printf("UCE ");
496         if (p->md_attr & EFI_MD_ATTR_WP)
497                 printf("WP ");
498         if (p->md_attr & EFI_MD_ATTR_RP)
499                 printf("RP ");
500         if (p->md_attr & EFI_MD_ATTR_XP)
501                 printf("XP ");
502         if (p->md_attr & EFI_MD_ATTR_NV)
503                 printf("NV ");
504         if (p->md_attr & EFI_MD_ATTR_MORE_RELIABLE)
505                 printf("MORE_RELIABLE ");
506         if (p->md_attr & EFI_MD_ATTR_RO)
507                 printf("RO ");
508         if (p->md_attr & EFI_MD_ATTR_RT)
509                 printf("RUNTIME");
510         printf("\n");
511 }
512
513 static void
514 print_efi_map_entries(struct efi_map_header *efihdr)
515 {
516
517         printf("%23s %12s %12s %8s %4s\n",
518             "Type", "Physical", "Virtual", "#Pages", "Attr");
519         foreach_efi_map_entry(efihdr, print_efi_map_entry);
520 }
521
522 #ifdef FDT
523 static void
524 try_load_dtb(caddr_t kmdp)
525 {
526         vm_offset_t dtbp;
527
528         dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
529 #if defined(FDT_DTB_STATIC)
530         /*
531          * In case the device tree blob was not retrieved (from metadata) try
532          * to use the statically embedded one.
533          */
534         if (dtbp == 0)
535                 dtbp = (vm_offset_t)&fdt_static_dtb;
536 #endif
537
538         if (dtbp == (vm_offset_t)NULL) {
539                 printf("ERROR loading DTB\n");
540                 return;
541         }
542
543         if (OF_install(OFW_FDT, 0) == FALSE)
544                 panic("Cannot install FDT");
545
546         if (OF_init((void *)dtbp) != 0)
547                 panic("OF_init failed with the found device tree");
548
549         parse_fdt_bootargs();
550 }
551 #endif
552
553 static bool
554 bus_probe(void)
555 {
556         bool has_acpi, has_fdt;
557         char *order, *env;
558
559         has_acpi = has_fdt = false;
560
561 #ifdef FDT
562         has_fdt = (OF_peer(0) != 0);
563 #endif
564 #ifdef DEV_ACPI
565         has_acpi = (AcpiOsGetRootPointer() != 0);
566 #endif
567
568         env = kern_getenv("kern.cfg.order");
569         if (env != NULL) {
570                 order = env;
571                 while (order != NULL) {
572                         if (has_acpi &&
573                             strncmp(order, "acpi", 4) == 0 &&
574                             (order[4] == ',' || order[4] == '\0')) {
575                                 arm64_bus_method = ARM64_BUS_ACPI;
576                                 break;
577                         }
578                         if (has_fdt &&
579                             strncmp(order, "fdt", 3) == 0 &&
580                             (order[3] == ',' || order[3] == '\0')) {
581                                 arm64_bus_method = ARM64_BUS_FDT;
582                                 break;
583                         }
584                         order = strchr(order, ',');
585                 }
586                 freeenv(env);
587
588                 /* If we set the bus method it is valid */
589                 if (arm64_bus_method != ARM64_BUS_NONE)
590                         return (true);
591         }
592         /* If no order or an invalid order was set use the default */
593         if (arm64_bus_method == ARM64_BUS_NONE) {
594                 if (has_fdt)
595                         arm64_bus_method = ARM64_BUS_FDT;
596                 else if (has_acpi)
597                         arm64_bus_method = ARM64_BUS_ACPI;
598         }
599
600         /*
601          * If no option was set the default is valid, otherwise we are
602          * setting one to get cninit() working, then calling panic to tell
603          * the user about the invalid bus setup.
604          */
605         return (env == NULL);
606 }
607
608 static void
609 cache_setup(void)
610 {
611         int dczva_line_shift;
612         uint32_t dczid_el0;
613
614         identify_cache(READ_SPECIALREG(ctr_el0));
615
616         dczid_el0 = READ_SPECIALREG(dczid_el0);
617
618         /* Check if dc zva is not prohibited */
619         if (dczid_el0 & DCZID_DZP)
620                 dczva_line_size = 0;
621         else {
622                 /* Same as with above calculations */
623                 dczva_line_shift = DCZID_BS_SIZE(dczid_el0);
624                 dczva_line_size = sizeof(int) << dczva_line_shift;
625
626                 /* Change pagezero function */
627                 pagezero = pagezero_cache;
628         }
629 }
630
631 int
632 memory_mapping_mode(vm_paddr_t pa)
633 {
634         struct efi_md *map, *p;
635         size_t efisz;
636         int ndesc, i;
637
638         if (efihdr == NULL)
639                 return (VM_MEMATTR_WRITE_BACK);
640
641         /*
642          * Memory map data provided by UEFI via the GetMemoryMap
643          * Boot Services API.
644          */
645         efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
646         map = (struct efi_md *)((uint8_t *)efihdr + efisz);
647
648         if (efihdr->descriptor_size == 0)
649                 return (VM_MEMATTR_WRITE_BACK);
650         ndesc = efihdr->memory_size / efihdr->descriptor_size;
651
652         for (i = 0, p = map; i < ndesc; i++,
653             p = efi_next_descriptor(p, efihdr->descriptor_size)) {
654                 if (pa < p->md_phys ||
655                     pa >= p->md_phys + p->md_pages * EFI_PAGE_SIZE)
656                         continue;
657                 if (p->md_type == EFI_MD_TYPE_IOMEM ||
658                     p->md_type == EFI_MD_TYPE_IOPORT)
659                         return (VM_MEMATTR_DEVICE);
660                 else if ((p->md_attr & EFI_MD_ATTR_WB) != 0 ||
661                     p->md_type == EFI_MD_TYPE_RECLAIM)
662                         return (VM_MEMATTR_WRITE_BACK);
663                 else if ((p->md_attr & EFI_MD_ATTR_WT) != 0)
664                         return (VM_MEMATTR_WRITE_THROUGH);
665                 else if ((p->md_attr & EFI_MD_ATTR_WC) != 0)
666                         return (VM_MEMATTR_WRITE_COMBINING);
667                 break;
668         }
669
670         return (VM_MEMATTR_DEVICE);
671 }
672
673 void
674 initarm(struct arm64_bootparams *abp)
675 {
676         struct efi_fb *efifb;
677         struct pcpu *pcpup;
678         char *env;
679 #ifdef FDT
680         struct mem_region mem_regions[FDT_MEM_REGIONS];
681         int mem_regions_sz;
682         phandle_t root;
683         char dts_version[255];
684 #endif
685         vm_offset_t lastaddr;
686         caddr_t kmdp;
687         bool valid;
688
689         boot_el = abp->boot_el;
690
691         /* Parse loader or FDT boot parametes. Determine last used address. */
692         lastaddr = parse_boot_param(abp);
693
694         /* Find the kernel address */
695         kmdp = preload_search_by_type("elf kernel");
696         if (kmdp == NULL)
697                 kmdp = preload_search_by_type("elf64 kernel");
698
699         identify_cpu(0);
700         update_special_regs(0);
701
702         link_elf_ireloc(kmdp);
703         try_load_dtb(kmdp);
704
705         efi_systbl_phys = MD_FETCH(kmdp, MODINFOMD_FW_HANDLE, vm_paddr_t);
706
707         /* Load the physical memory ranges */
708         efihdr = (struct efi_map_header *)preload_search_info(kmdp,
709             MODINFO_METADATA | MODINFOMD_EFI_MAP);
710         if (efihdr != NULL)
711                 add_efi_map_entries(efihdr);
712 #ifdef FDT
713         else {
714                 /* Grab physical memory regions information from device tree. */
715                 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz,
716                     NULL) != 0)
717                         panic("Cannot get physical memory regions");
718                 physmem_hardware_regions(mem_regions, mem_regions_sz);
719         }
720         if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0)
721                 physmem_exclude_regions(mem_regions, mem_regions_sz,
722                     EXFLAG_NODUMP | EXFLAG_NOALLOC);
723 #endif
724
725         /* Exclude the EFI framebuffer from our view of physical memory. */
726         efifb = (struct efi_fb *)preload_search_info(kmdp,
727             MODINFO_METADATA | MODINFOMD_EFI_FB);
728         if (efifb != NULL)
729                 physmem_exclude_region(efifb->fb_addr, efifb->fb_size,
730                     EXFLAG_NOALLOC);
731
732         /* Set the pcpu data, this is needed by pmap_bootstrap */
733         pcpup = &pcpu0;
734         pcpu_init(pcpup, 0, sizeof(struct pcpu));
735
736         /*
737          * Set the pcpu pointer with a backup in tpidr_el1 to be
738          * loaded when entering the kernel from userland.
739          */
740         __asm __volatile(
741             "mov x18, %0 \n"
742             "msr tpidr_el1, %0" :: "r"(pcpup));
743
744         PCPU_SET(curthread, &thread0);
745         PCPU_SET(midr, get_midr());
746
747         /* Do basic tuning, hz etc */
748         init_param1();
749
750         cache_setup();
751         pan_setup();
752
753         /* Bootstrap enough of pmap  to enter the kernel proper */
754         pmap_bootstrap(abp->kern_l0pt, abp->kern_l1pt,
755             KERNBASE - abp->kern_delta, lastaddr - KERNBASE);
756         /* Exclude entries neexed in teh DMAP region, but not phys_avail */
757         if (efihdr != NULL)
758                 exclude_efi_map_entries(efihdr);
759         physmem_init_kernel_globals();
760
761         devmap_bootstrap(0, NULL);
762
763         valid = bus_probe();
764
765         cninit();
766         set_ttbr0(abp->kern_ttbr0);
767         cpu_tlb_flushID();
768
769         if (!valid)
770                 panic("Invalid bus configuration: %s",
771                     kern_getenv("kern.cfg.order"));
772
773         /*
774          * Dump the boot metadata. We have to wait for cninit() since console
775          * output is required. If it's grossly incorrect the kernel will never
776          * make it this far.
777          */
778         if (getenv_is_true("debug.dump_modinfo_at_boot"))
779                 preload_dump();
780
781         init_proc0(abp->kern_stack);
782         msgbufinit(msgbufp, msgbufsize);
783         mutex_init();
784         init_param2(physmem);
785
786         dbg_init();
787         kdb_init();
788         pan_enable();
789
790         kcsan_cpu_init(0);
791
792         env = kern_getenv("kernelname");
793         if (env != NULL)
794                 strlcpy(kernelname, env, sizeof(kernelname));
795
796 #ifdef FDT
797         if (arm64_bus_method == ARM64_BUS_FDT) {
798                 root = OF_finddevice("/");
799                 if (OF_getprop(root, "freebsd,dts-version", dts_version, sizeof(dts_version)) > 0) {
800                         if (strcmp(LINUX_DTS_VERSION, dts_version) != 0)
801                                 printf("WARNING: DTB version is %s while kernel expects %s, "
802                                     "please update the DTB in the ESP\n",
803                                     dts_version,
804                                     LINUX_DTS_VERSION);
805                 } else {
806                         printf("WARNING: Cannot find freebsd,dts-version property, "
807                             "cannot check DTB compliance\n");
808                 }
809         }
810 #endif
811
812         if (boothowto & RB_VERBOSE) {
813                 if (efihdr != NULL)
814                         print_efi_map_entries(efihdr);
815                 physmem_print_tables();
816         }
817
818         early_boot = 0;
819 }
820
821 void
822 dbg_init(void)
823 {
824
825         /* Clear OS lock */
826         WRITE_SPECIALREG(oslar_el1, 0);
827
828         /* This permits DDB to use debug registers for watchpoints. */
829         dbg_monitor_init();
830
831         /* TODO: Eventually will need to initialize debug registers here. */
832 }
833
834 #ifdef DDB
835 #include <ddb/ddb.h>
836
837 DB_SHOW_COMMAND(specialregs, db_show_spregs)
838 {
839 #define PRINT_REG(reg)  \
840     db_printf(__STRING(reg) " = %#016lx\n", READ_SPECIALREG(reg))
841
842         PRINT_REG(actlr_el1);
843         PRINT_REG(afsr0_el1);
844         PRINT_REG(afsr1_el1);
845         PRINT_REG(aidr_el1);
846         PRINT_REG(amair_el1);
847         PRINT_REG(ccsidr_el1);
848         PRINT_REG(clidr_el1);
849         PRINT_REG(contextidr_el1);
850         PRINT_REG(cpacr_el1);
851         PRINT_REG(csselr_el1);
852         PRINT_REG(ctr_el0);
853         PRINT_REG(currentel);
854         PRINT_REG(daif);
855         PRINT_REG(dczid_el0);
856         PRINT_REG(elr_el1);
857         PRINT_REG(esr_el1);
858         PRINT_REG(far_el1);
859 #if 0
860         /* ARM64TODO: Enable VFP before reading floating-point registers */
861         PRINT_REG(fpcr);
862         PRINT_REG(fpsr);
863 #endif
864         PRINT_REG(id_aa64afr0_el1);
865         PRINT_REG(id_aa64afr1_el1);
866         PRINT_REG(id_aa64dfr0_el1);
867         PRINT_REG(id_aa64dfr1_el1);
868         PRINT_REG(id_aa64isar0_el1);
869         PRINT_REG(id_aa64isar1_el1);
870         PRINT_REG(id_aa64pfr0_el1);
871         PRINT_REG(id_aa64pfr1_el1);
872         PRINT_REG(id_afr0_el1);
873         PRINT_REG(id_dfr0_el1);
874         PRINT_REG(id_isar0_el1);
875         PRINT_REG(id_isar1_el1);
876         PRINT_REG(id_isar2_el1);
877         PRINT_REG(id_isar3_el1);
878         PRINT_REG(id_isar4_el1);
879         PRINT_REG(id_isar5_el1);
880         PRINT_REG(id_mmfr0_el1);
881         PRINT_REG(id_mmfr1_el1);
882         PRINT_REG(id_mmfr2_el1);
883         PRINT_REG(id_mmfr3_el1);
884 #if 0
885         /* Missing from llvm */
886         PRINT_REG(id_mmfr4_el1);
887 #endif
888         PRINT_REG(id_pfr0_el1);
889         PRINT_REG(id_pfr1_el1);
890         PRINT_REG(isr_el1);
891         PRINT_REG(mair_el1);
892         PRINT_REG(midr_el1);
893         PRINT_REG(mpidr_el1);
894         PRINT_REG(mvfr0_el1);
895         PRINT_REG(mvfr1_el1);
896         PRINT_REG(mvfr2_el1);
897         PRINT_REG(revidr_el1);
898         PRINT_REG(sctlr_el1);
899         PRINT_REG(sp_el0);
900         PRINT_REG(spsel);
901         PRINT_REG(spsr_el1);
902         PRINT_REG(tcr_el1);
903         PRINT_REG(tpidr_el0);
904         PRINT_REG(tpidr_el1);
905         PRINT_REG(tpidrro_el0);
906         PRINT_REG(ttbr0_el1);
907         PRINT_REG(ttbr1_el1);
908         PRINT_REG(vbar_el1);
909 #undef PRINT_REG
910 }
911
912 DB_SHOW_COMMAND(vtop, db_show_vtop)
913 {
914         uint64_t phys;
915
916         if (have_addr) {
917                 phys = arm64_address_translate_s1e1r(addr);
918                 db_printf("EL1 physical address reg (read):  0x%016lx\n", phys);
919                 phys = arm64_address_translate_s1e1w(addr);
920                 db_printf("EL1 physical address reg (write): 0x%016lx\n", phys);
921                 phys = arm64_address_translate_s1e0r(addr);
922                 db_printf("EL0 physical address reg (read):  0x%016lx\n", phys);
923                 phys = arm64_address_translate_s1e0w(addr);
924                 db_printf("EL0 physical address reg (write): 0x%016lx\n", phys);
925         } else
926                 db_printf("show vtop <virt_addr>\n");
927 }
928 #endif