2 * Copyright (c) 2004 Marcel Moolenaar
3 * Copyright (c) 2001 Doug Rabson
4 * Copyright (c) 2016 The FreeBSD Foundation
5 * Copyright (c) 2017 Andrew Turner
8 * Portions of this software were developed by Konstantin Belousov
9 * under sponsorship from the FreeBSD Foundation.
11 * This software was developed by SRI International and the University of
12 * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
13 * ("CTSRD"), as part of the DARPA CRASH research programme.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/linker.h>
45 #include <sys/mutex.h>
47 #include <sys/rwlock.h>
48 #include <sys/systm.h>
49 #include <sys/vmmeter.h>
51 #include <machine/metadata.h>
52 #include <machine/pcb.h>
53 #include <machine/pte.h>
54 #include <machine/vfp.h>
55 #include <machine/vmparam.h>
59 #include <vm/vm_map.h>
60 #include <vm/vm_object.h>
61 #include <vm/vm_page.h>
62 #include <vm/vm_pager.h>
64 static vm_object_t obj_1t1_pt;
65 static vm_page_t efi_l0_page;
66 static pd_entry_t *efi_l0;
67 static vm_pindex_t efi_1t1_idx;
70 efi_destroy_1t1_map(void)
74 if (obj_1t1_pt != NULL) {
75 VM_OBJECT_RLOCK(obj_1t1_pt);
76 TAILQ_FOREACH(m, &obj_1t1_pt->memq, listq)
78 vm_wire_sub(obj_1t1_pt->resident_page_count);
79 VM_OBJECT_RUNLOCK(obj_1t1_pt);
80 vm_object_deallocate(obj_1t1_pt);
92 return (vm_page_grab(obj_1t1_pt, efi_1t1_idx++, VM_ALLOC_NOBUSY |
93 VM_ALLOC_WIRED | VM_ALLOC_ZERO));
97 efi_1t1_l3(vm_offset_t va)
99 pd_entry_t *l0, *l1, *l2;
101 vm_pindex_t l0_idx, l1_idx, l2_idx;
105 l0_idx = pmap_l0_index(va);
106 l0 = &efi_l0[l0_idx];
109 mphys = VM_PAGE_TO_PHYS(m);
110 *l0 = mphys | L0_TABLE;
112 mphys = *l0 & ~ATTR_MASK;
115 l1 = (pd_entry_t *)PHYS_TO_DMAP(mphys);
116 l1_idx = pmap_l1_index(va);
120 mphys = VM_PAGE_TO_PHYS(m);
121 *l1 = mphys | L1_TABLE;
123 mphys = *l1 & ~ATTR_MASK;
126 l2 = (pd_entry_t *)PHYS_TO_DMAP(mphys);
127 l2_idx = pmap_l2_index(va);
131 mphys = VM_PAGE_TO_PHYS(m);
132 *l2 = mphys | L2_TABLE;
134 mphys = *l2 & ~ATTR_MASK;
137 l3 = (pt_entry_t *)PHYS_TO_DMAP(mphys);
138 l3 += pmap_l3_index(va);
139 KASSERT(*l3 == 0, ("%s: Already mapped: va %#jx *pt %#jx", __func__,
146 * Create the 1:1 virtual to physical map for EFI
149 efi_create_1t1_map(struct efi_md *map, int ndesc, int descsz)
157 obj_1t1_pt = vm_pager_allocate(OBJT_PHYS, NULL, L0_ENTRIES +
158 L0_ENTRIES * Ln_ENTRIES + L0_ENTRIES * Ln_ENTRIES * Ln_ENTRIES +
159 L0_ENTRIES * Ln_ENTRIES * Ln_ENTRIES * Ln_ENTRIES,
160 VM_PROT_ALL, 0, NULL);
161 VM_OBJECT_WLOCK(obj_1t1_pt);
163 efi_l0_page = efi_1t1_page();
164 VM_OBJECT_WUNLOCK(obj_1t1_pt);
165 efi_l0 = (pd_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(efi_l0_page));
166 bzero(efi_l0, L0_ENTRIES * sizeof(*efi_l0));
168 for (i = 0, p = map; i < ndesc; i++, p = efi_next_descriptor(p,
170 if ((p->md_attr & EFI_MD_ATTR_RT) == 0)
172 if (p->md_virt != NULL && (uint64_t)p->md_virt != p->md_phys) {
174 printf("EFI Runtime entry %d is mapped\n", i);
177 if ((p->md_phys & EFI_PAGE_MASK) != 0) {
179 printf("EFI Runtime entry %d is not aligned\n",
183 if (p->md_phys + p->md_pages * EFI_PAGE_SIZE < p->md_phys ||
184 p->md_phys + p->md_pages * EFI_PAGE_SIZE >=
185 VM_MAXUSER_ADDRESS) {
186 printf("EFI Runtime entry %d is not in mappable for RT:"
187 "base %#016jx %#jx pages\n",
188 i, (uintmax_t)p->md_phys,
189 (uintmax_t)p->md_pages);
192 if ((p->md_attr & EFI_MD_ATTR_WB) != 0)
193 mode = VM_MEMATTR_WRITE_BACK;
194 else if ((p->md_attr & EFI_MD_ATTR_WT) != 0)
195 mode = VM_MEMATTR_WRITE_THROUGH;
196 else if ((p->md_attr & EFI_MD_ATTR_WC) != 0)
197 mode = VM_MEMATTR_WRITE_COMBINING;
198 else if ((p->md_attr & EFI_MD_ATTR_UC) != 0)
199 mode = VM_MEMATTR_DEVICE;
202 printf("EFI Runtime entry %d mapping "
203 "attributes unsupported\n", i);
204 mode = VM_MEMATTR_UNCACHEABLE;
207 printf("MAP %lx mode %x pages %lu\n", p->md_phys, mode, p->md_pages);
208 VM_OBJECT_WLOCK(obj_1t1_pt);
209 for (va = p->md_phys, idx = 0; idx < p->md_pages; idx++,
212 *l3 = va | ATTR_DEFAULT | ATTR_IDX(mode) |
213 ATTR_AP(ATTR_AP_RW) | L3_PAGE;
215 VM_OBJECT_WUNLOCK(obj_1t1_pt);
220 efi_destroy_1t1_map();
229 "msr ttbr0_el1, %0 \n"
234 : : "r"(VM_PAGE_TO_PHYS(efi_l0_page)));
246 "msr ttbr0_el1, %0 \n"
251 : : "r"(td->td_proc->p_md.md_l0addr));