]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/efirt.c
Import Amazon Elastic Network Adapter (ENA) HAL to sys/contrib/
[FreeBSD/FreeBSD.git] / sys / amd64 / amd64 / efirt.c
1 /*-
2  * Copyright (c) 2004 Marcel Moolenaar
3  * Copyright (c) 2001 Doug Rabson
4  * Copyright (c) 2016 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * Portions of this software were developed by Konstantin Belousov
8  * under sponsorship from the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/efi.h>
37 #include <sys/kernel.h>
38 #include <sys/linker.h>
39 #include <sys/lock.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/clock.h>
43 #include <sys/proc.h>
44 #include <sys/rwlock.h>
45 #include <sys/sched.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
48 #include <sys/vmmeter.h>
49 #include <machine/fpu.h>
50 #include <machine/efi.h>
51 #include <machine/metadata.h>
52 #include <machine/md_var.h>
53 #include <machine/smp.h>
54 #include <machine/vmparam.h>
55 #include <vm/vm.h>
56 #include <vm/pmap.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_object.h>
59 #include <vm/vm_page.h>
60 #include <vm/vm_pager.h>
61
62 static struct efi_systbl *efi_systbl;
63 static struct efi_cfgtbl *efi_cfgtbl;
64 static struct efi_rt *efi_runtime;
65
66 static int efi_status2err[25] = {
67         0,              /* EFI_SUCCESS */
68         ENOEXEC,        /* EFI_LOAD_ERROR */
69         EINVAL,         /* EFI_INVALID_PARAMETER */
70         ENOSYS,         /* EFI_UNSUPPORTED */
71         EMSGSIZE,       /* EFI_BAD_BUFFER_SIZE */
72         EOVERFLOW,      /* EFI_BUFFER_TOO_SMALL */
73         EBUSY,          /* EFI_NOT_READY */
74         EIO,            /* EFI_DEVICE_ERROR */
75         EROFS,          /* EFI_WRITE_PROTECTED */
76         EAGAIN,         /* EFI_OUT_OF_RESOURCES */
77         EIO,            /* EFI_VOLUME_CORRUPTED */
78         ENOSPC,         /* EFI_VOLUME_FULL */
79         ENXIO,          /* EFI_NO_MEDIA */
80         ESTALE,         /* EFI_MEDIA_CHANGED */
81         ENOENT,         /* EFI_NOT_FOUND */
82         EACCES,         /* EFI_ACCESS_DENIED */
83         ETIMEDOUT,      /* EFI_NO_RESPONSE */
84         EADDRNOTAVAIL,  /* EFI_NO_MAPPING */
85         ETIMEDOUT,      /* EFI_TIMEOUT */
86         EDOOFUS,        /* EFI_NOT_STARTED */
87         EALREADY,       /* EFI_ALREADY_STARTED */
88         ECANCELED,      /* EFI_ABORTED */
89         EPROTO,         /* EFI_ICMP_ERROR */
90         EPROTO,         /* EFI_TFTP_ERROR */
91         EPROTO          /* EFI_PROTOCOL_ERROR */
92 };
93
94 static int
95 efi_status_to_errno(efi_status status)
96 {
97         u_long code;
98
99         code = status & 0x3ffffffffffffffful;
100         return (code < nitems(efi_status2err) ? efi_status2err[code] : EDOOFUS);
101 }
102
103 static struct mtx efi_lock;
104 static pml4_entry_t *efi_pml4;
105 static vm_object_t obj_1t1_pt;
106 static vm_page_t efi_pml4_page;
107
108 static void
109 efi_destroy_1t1_map(void)
110 {
111         vm_page_t m;
112
113         if (obj_1t1_pt != NULL) {
114                 VM_OBJECT_RLOCK(obj_1t1_pt);
115                 TAILQ_FOREACH(m, &obj_1t1_pt->memq, listq)
116                         m->wire_count = 0;
117                 atomic_subtract_int(&vm_cnt.v_wire_count,
118                     obj_1t1_pt->resident_page_count);
119                 VM_OBJECT_RUNLOCK(obj_1t1_pt);
120                 vm_object_deallocate(obj_1t1_pt);
121         }
122
123         obj_1t1_pt = NULL;
124         efi_pml4 = NULL;
125         efi_pml4_page = NULL;
126 }
127
128 static vm_page_t
129 efi_1t1_page(vm_pindex_t idx)
130 {
131
132         return (vm_page_grab(obj_1t1_pt, idx, VM_ALLOC_NOBUSY |
133             VM_ALLOC_WIRED | VM_ALLOC_ZERO));
134 }
135
136 static pt_entry_t *
137 efi_1t1_pte(vm_offset_t va)
138 {
139         pml4_entry_t *pml4e;
140         pdp_entry_t *pdpe;
141         pd_entry_t *pde;
142         pt_entry_t *pte;
143         vm_page_t m;
144         vm_pindex_t pml4_idx, pdp_idx, pd_idx;
145         vm_paddr_t mphys;
146
147         pml4_idx = pmap_pml4e_index(va);
148         pml4e = &efi_pml4[pml4_idx];
149         if (*pml4e == 0) {
150                 m = efi_1t1_page(1 + pml4_idx);
151                 mphys =  VM_PAGE_TO_PHYS(m);
152                 *pml4e = mphys | X86_PG_RW | X86_PG_V;
153         } else {
154                 mphys = *pml4e & ~PAGE_MASK;
155         }
156
157         pdpe = (pdp_entry_t *)PHYS_TO_DMAP(mphys);
158         pdp_idx = pmap_pdpe_index(va);
159         pdpe += pdp_idx;
160         if (*pdpe == 0) {
161                 m = efi_1t1_page(1 + NPML4EPG + (pml4_idx + 1) * (pdp_idx + 1));
162                 mphys =  VM_PAGE_TO_PHYS(m);
163                 *pdpe = mphys | X86_PG_RW | X86_PG_V;
164         } else {
165                 mphys = *pdpe & ~PAGE_MASK;
166         }
167
168         pde = (pd_entry_t *)PHYS_TO_DMAP(mphys);
169         pd_idx = pmap_pde_index(va);
170         pde += pd_idx;
171         if (*pde == 0) {
172                 m = efi_1t1_page(1 + NPML4EPG + NPML4EPG * NPDPEPG +
173                     (pml4_idx + 1) * (pdp_idx + 1) * (pd_idx + 1));
174                 mphys = VM_PAGE_TO_PHYS(m);
175                 *pde = mphys | X86_PG_RW | X86_PG_V;
176         } else {
177                 mphys = *pde & ~PAGE_MASK;
178         }
179
180         pte = (pt_entry_t *)PHYS_TO_DMAP(mphys);
181         pte += pmap_pte_index(va);
182         KASSERT(*pte == 0, ("va %#jx *pt %#jx", va, *pte));
183
184         return (pte);
185 }
186
187 static bool
188 efi_create_1t1_map(struct efi_md *map, int ndesc, int descsz)
189 {
190         struct efi_md *p;
191         pt_entry_t *pte;
192         vm_offset_t va;
193         uint64_t idx;
194         int bits, i, mode;
195
196         obj_1t1_pt = vm_pager_allocate(OBJT_PHYS, NULL, 1 + NPML4EPG +
197             NPML4EPG * NPDPEPG + NPML4EPG * NPDPEPG * NPDEPG,
198             VM_PROT_ALL, 0, NULL);
199         VM_OBJECT_WLOCK(obj_1t1_pt);
200         efi_pml4_page = efi_1t1_page(0);
201         VM_OBJECT_WUNLOCK(obj_1t1_pt);
202         efi_pml4 = (pml4_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(efi_pml4_page));
203         pmap_pinit_pml4(efi_pml4_page);
204
205         for (i = 0, p = map; i < ndesc; i++, p = efi_next_descriptor(p,
206             descsz)) {
207                 if ((p->md_attr & EFI_MD_ATTR_RT) == 0)
208                         continue;
209                 if (p->md_virt != NULL) {
210                         if (bootverbose)
211                                 printf("EFI Runtime entry %d is mapped\n", i);
212                         goto fail;
213                 }
214                 if ((p->md_phys & EFI_PAGE_MASK) != 0) {
215                         if (bootverbose)
216                                 printf("EFI Runtime entry %d is not aligned\n",
217                                     i);
218                         goto fail;
219                 }
220                 if (p->md_phys + p->md_pages * EFI_PAGE_SIZE < p->md_phys ||
221                     p->md_phys + p->md_pages * EFI_PAGE_SIZE >=
222                     VM_MAXUSER_ADDRESS) {
223                         printf("EFI Runtime entry %d is not in mappable for RT:"
224                             "base %#016jx %#jx pages\n",
225                             i, (uintmax_t)p->md_phys,
226                             (uintmax_t)p->md_pages);
227                         goto fail;
228                 }
229                 if ((p->md_attr & EFI_MD_ATTR_WB) != 0)
230                         mode = VM_MEMATTR_WRITE_BACK;
231                 else if ((p->md_attr & EFI_MD_ATTR_WT) != 0)
232                         mode = VM_MEMATTR_WRITE_THROUGH;
233                 else if ((p->md_attr & EFI_MD_ATTR_WC) != 0)
234                         mode = VM_MEMATTR_WRITE_COMBINING;
235                 else if ((p->md_attr & EFI_MD_ATTR_WP) != 0)
236                         mode = VM_MEMATTR_WRITE_PROTECTED;
237                 else if ((p->md_attr & EFI_MD_ATTR_UC) != 0)
238                         mode = VM_MEMATTR_UNCACHEABLE;
239                 else {
240                         if (bootverbose)
241                                 printf("EFI Runtime entry %d mapping "
242                                     "attributes unsupported\n", i);
243                         mode = VM_MEMATTR_UNCACHEABLE;
244                 }
245                 bits = pmap_cache_bits(kernel_pmap, mode, FALSE) | X86_PG_RW |
246                     X86_PG_V;
247                 VM_OBJECT_WLOCK(obj_1t1_pt);
248                 for (va = p->md_phys, idx = 0; idx < p->md_pages; idx++,
249                     va += PAGE_SIZE) {
250                         pte = efi_1t1_pte(va);
251                         pte_store(pte, va | bits);
252                 }
253                 VM_OBJECT_WUNLOCK(obj_1t1_pt);
254         }
255
256         return (true);
257
258 fail:
259         efi_destroy_1t1_map();
260         return (false);
261 }
262
263 /*
264  * Create an environment for the EFI runtime code call.  The most
265  * important part is creating the required 1:1 physical->virtual
266  * mappings for the runtime segments.  To do that, we manually create
267  * page table which unmap userspace but gives correct kernel mapping.
268  * The 1:1 mappings for runtime segments usually occupy low 4G of the
269  * physical address map.
270  *
271  * The 1:1 mappings were chosen over the SetVirtualAddressMap() EFI RT
272  * service, because there are some BIOSes which fail to correctly
273  * relocate itself on the call, requiring both 1:1 and virtual
274  * mapping.  As result, we must provide 1:1 mapping anyway, so no
275  * reason to bother with the virtual map, and no need to add a
276  * complexity into loader.
277  *
278  * The fpu_kern_enter() call allows firmware to use FPU, as mandated
279  * by the specification.  In particular, CR0.TS bit is cleared.  Also
280  * it enters critical section, giving us neccessary protection against
281  * context switch.
282  *
283  * There is no need to disable interrupts around the change of %cr3,
284  * the kernel mappings are correct, while we only grabbed the
285  * userspace portion of VA.  Interrupts handlers must not access
286  * userspace.  Having interrupts enabled fixes the issue with
287  * firmware/SMM long operation, which would negatively affect IPIs,
288  * esp. TLB shootdown requests.
289  */
290 static int
291 efi_enter(void)
292 {
293         pmap_t curpmap;
294         int error;
295
296         if (efi_runtime == NULL)
297                 return (ENXIO);
298         curpmap = PCPU_GET(curpmap);
299         PMAP_LOCK(curpmap);
300         mtx_lock(&efi_lock);
301         error = fpu_kern_enter(curthread, NULL, FPU_KERN_NOCTX);
302         if (error != 0) {
303                 PMAP_UNLOCK(curpmap);
304                 return (error);
305         }
306
307         /*
308          * IPI TLB shootdown handler invltlb_pcid_handler() reloads
309          * %cr3 from the curpmap->pm_cr3, which would disable runtime
310          * segments mappings.  Block the handler's action by setting
311          * curpmap to impossible value.  See also comment in
312          * pmap.c:pmap_activate_sw().
313          */
314         if (pmap_pcid_enabled && !invpcid_works)
315                 PCPU_SET(curpmap, NULL);
316
317         load_cr3(VM_PAGE_TO_PHYS(efi_pml4_page) | (pmap_pcid_enabled ?
318             curpmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid : 0));
319         /*
320          * If PCID is enabled, the clear CR3_PCID_SAVE bit in the loaded %cr3
321          * causes TLB invalidation.
322          */
323         if (!pmap_pcid_enabled)
324                 invltlb();
325         return (0);
326 }
327
328 static void
329 efi_leave(void)
330 {
331         pmap_t curpmap;
332
333         curpmap = &curproc->p_vmspace->vm_pmap;
334         if (pmap_pcid_enabled && !invpcid_works)
335                 PCPU_SET(curpmap, curpmap);
336         load_cr3(curpmap->pm_cr3 | (pmap_pcid_enabled ?
337             curpmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid : 0));
338         if (!pmap_pcid_enabled)
339                 invltlb();
340
341         fpu_kern_leave(curthread, NULL);
342         mtx_unlock(&efi_lock);
343         PMAP_UNLOCK(curpmap);
344 }
345
346 static int
347 efi_init(void)
348 {
349         struct efi_map_header *efihdr;
350         struct efi_md *map;
351         caddr_t kmdp;
352         size_t efisz;
353
354         mtx_init(&efi_lock, "efi", NULL, MTX_DEF);
355
356         if (efi_systbl_phys == 0) {
357                 if (bootverbose)
358                         printf("EFI systbl not available\n");
359                 return (0);
360         }
361         efi_systbl = (struct efi_systbl *)PHYS_TO_DMAP(efi_systbl_phys);
362         if (efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) {
363                 efi_systbl = NULL;
364                 if (bootverbose)
365                         printf("EFI systbl signature invalid\n");
366                 return (0);
367         }
368         efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL :
369             (struct efi_cfgtbl *)efi_systbl->st_cfgtbl;
370         if (efi_cfgtbl == NULL) {
371                 if (bootverbose)
372                         printf("EFI config table is not present\n");
373         }
374
375         kmdp = preload_search_by_type("elf kernel");
376         if (kmdp == NULL)
377                 kmdp = preload_search_by_type("elf64 kernel");
378         efihdr = (struct efi_map_header *)preload_search_info(kmdp,
379             MODINFO_METADATA | MODINFOMD_EFI_MAP);
380         if (efihdr == NULL) {
381                 if (bootverbose)
382                         printf("EFI map is not present\n");
383                 return (0);
384         }
385         efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
386         map = (struct efi_md *)((uint8_t *)efihdr + efisz);
387         if (efihdr->descriptor_size == 0)
388                 return (ENOMEM);
389
390         if (!efi_create_1t1_map(map, efihdr->memory_size /
391             efihdr->descriptor_size, efihdr->descriptor_size)) {
392                 if (bootverbose)
393                         printf("EFI cannot create runtime map\n");
394                 return (ENOMEM);
395         }
396
397         efi_runtime = (efi_systbl->st_rt == 0) ? NULL :
398             (struct efi_rt *)efi_systbl->st_rt;
399         if (efi_runtime == NULL) {
400                 if (bootverbose)
401                         printf("EFI runtime services table is not present\n");
402                 efi_destroy_1t1_map();
403                 return (ENXIO);
404         }
405
406         return (0);
407 }
408
409 static void
410 efi_uninit(void)
411 {
412
413         efi_destroy_1t1_map();
414
415         efi_systbl = NULL;
416         efi_cfgtbl = NULL;
417         efi_runtime = NULL;
418
419         mtx_destroy(&efi_lock);
420 }
421
422 int
423 efi_get_table(struct uuid *uuid, void **ptr)
424 {
425         struct efi_cfgtbl *ct;
426         u_long count;
427
428         if (efi_cfgtbl == NULL)
429                 return (ENXIO);
430         count = efi_systbl->st_entries;
431         ct = efi_cfgtbl;
432         while (count--) {
433                 if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) {
434                         *ptr = (void *)PHYS_TO_DMAP(ct->ct_data);
435                         return (0);
436                 }
437                 ct++;
438         }
439         return (ENOENT);
440 }
441
442 int
443 efi_get_time_locked(struct efi_tm *tm)
444 {
445         efi_status status;
446         int error;
447
448         mtx_assert(&resettodr_lock, MA_OWNED);
449         error = efi_enter();
450         if (error != 0)
451                 return (error);
452         status = efi_runtime->rt_gettime(tm, NULL);
453         efi_leave();
454         error = efi_status_to_errno(status);
455         return (error);
456 }
457
458 int
459 efi_get_time(struct efi_tm *tm)
460 {
461         int error;
462
463         if (efi_runtime == NULL)
464                 return (ENXIO);
465         mtx_lock(&resettodr_lock);
466         error = efi_get_time_locked(tm);
467         mtx_unlock(&resettodr_lock);
468         return (error);
469 }
470
471 int
472 efi_reset_system(void)
473 {
474         int error;
475
476         error = efi_enter();
477         if (error != 0)
478                 return (error);
479         efi_runtime->rt_reset(EFI_RESET_WARM, 0, 0, NULL);
480         efi_leave();
481         return (EIO);
482 }
483
484 int
485 efi_set_time_locked(struct efi_tm *tm)
486 {
487         efi_status status;
488         int error;
489
490         mtx_assert(&resettodr_lock, MA_OWNED);
491         error = efi_enter();
492         if (error != 0)
493                 return (error);
494         status = efi_runtime->rt_settime(tm);
495         efi_leave();
496         error = efi_status_to_errno(status);
497         return (error);
498 }
499
500 int
501 efi_set_time(struct efi_tm *tm)
502 {
503         int error;
504
505         if (efi_runtime == NULL)
506                 return (ENXIO);
507         mtx_lock(&resettodr_lock);
508         error = efi_set_time_locked(tm);
509         mtx_unlock(&resettodr_lock);
510         return (error);
511 }
512
513 int
514 efi_var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib,
515     size_t *datasize, void *data)
516 {
517         efi_status status;
518         int error;
519
520         error = efi_enter();
521         if (error != 0)
522                 return (error);
523         status = efi_runtime->rt_getvar(name, vendor, attrib, datasize, data);
524         efi_leave();
525         error = efi_status_to_errno(status);
526         return (error);
527 }
528
529 int
530 efi_var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor)
531 {
532         efi_status status;
533         int error;
534
535         error = efi_enter();
536         if (error != 0)
537                 return (error);
538         status = efi_runtime->rt_scanvar(namesize, name, vendor);
539         efi_leave();
540         error = efi_status_to_errno(status);
541         return (error);
542 }
543
544 int
545 efi_var_set(efi_char *name, struct uuid *vendor, uint32_t attrib,
546     size_t datasize, void *data)
547 {
548         efi_status status;
549         int error;
550
551         error = efi_enter();
552         if (error != 0)
553                 return (error);
554         status = efi_runtime->rt_setvar(name, vendor, attrib, datasize, data);
555         efi_leave();
556         error = efi_status_to_errno(status);
557         return (error);
558 }
559
560 static int
561 efirt_modevents(module_t m, int event, void *arg __unused)
562 {
563
564         switch (event) {
565         case MOD_LOAD:
566                 return (efi_init());
567
568         case MOD_UNLOAD:
569                 efi_uninit();
570                 return (0);
571
572         case MOD_SHUTDOWN:
573                 return (0);
574
575         default:
576                 return (EOPNOTSUPP);
577         }
578 }
579
580 static moduledata_t efirt_moddata = {
581         .name = "efirt",
582         .evhand = efirt_modevents,
583         .priv = NULL,
584 };
585 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_VM_CONF, SI_ORDER_ANY);
586 MODULE_VERSION(efirt, 1);
587
588 /* XXX debug stuff */
589 static int
590 efi_time_sysctl_handler(SYSCTL_HANDLER_ARGS)
591 {
592         struct efi_tm tm;
593         int error, val;
594
595         val = 0;
596         error = sysctl_handle_int(oidp, &val, 0, req);
597         if (error != 0 || req->newptr == NULL)
598                 return (error);
599         error = efi_get_time(&tm);
600         if (error == 0) {
601                 uprintf("EFI reports: Year %d Month %d Day %d Hour %d Min %d "
602                     "Sec %d\n", tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour,
603                     tm.tm_min, tm.tm_sec);
604         }
605         return (error);
606 }
607
608 SYSCTL_PROC(_debug, OID_AUTO, efi_time, CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
609     efi_time_sysctl_handler, "I", "");