]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/efidev/efirt.c
Update to bmake-20201101
[FreeBSD/FreeBSD.git] / sys / dev / efidev / efirt.c
1 /*-
2  * Copyright (c) 2004 Marcel Moolenaar
3  * Copyright (c) 2001 Doug Rabson
4  * Copyright (c) 2016, 2018 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/eventhandler.h>
38 #include <sys/kernel.h>
39 #include <sys/linker.h>
40 #include <sys/lock.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/clock.h>
44 #include <sys/proc.h>
45 #include <sys/reboot.h>
46 #include <sys/rwlock.h>
47 #include <sys/sched.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
50 #include <sys/vmmeter.h>
51
52 #include <machine/fpu.h>
53 #include <machine/efi.h>
54 #include <machine/metadata.h>
55 #include <machine/vmparam.h>
56
57 #include <vm/vm.h>
58 #include <vm/pmap.h>
59 #include <vm/vm_map.h>
60
61 static struct efi_systbl *efi_systbl;
62 static eventhandler_tag efi_shutdown_tag;
63 /*
64  * The following pointers point to tables in the EFI runtime service data pages.
65  * Care should be taken to make sure that we've properly entered the EFI runtime
66  * environment (efi_enter()) before dereferencing them.
67  */
68 static struct efi_cfgtbl *efi_cfgtbl;
69 static struct efi_rt *efi_runtime;
70
71 static int efi_status2err[25] = {
72         0,              /* EFI_SUCCESS */
73         ENOEXEC,        /* EFI_LOAD_ERROR */
74         EINVAL,         /* EFI_INVALID_PARAMETER */
75         ENOSYS,         /* EFI_UNSUPPORTED */
76         EMSGSIZE,       /* EFI_BAD_BUFFER_SIZE */
77         EOVERFLOW,      /* EFI_BUFFER_TOO_SMALL */
78         EBUSY,          /* EFI_NOT_READY */
79         EIO,            /* EFI_DEVICE_ERROR */
80         EROFS,          /* EFI_WRITE_PROTECTED */
81         EAGAIN,         /* EFI_OUT_OF_RESOURCES */
82         EIO,            /* EFI_VOLUME_CORRUPTED */
83         ENOSPC,         /* EFI_VOLUME_FULL */
84         ENXIO,          /* EFI_NO_MEDIA */
85         ESTALE,         /* EFI_MEDIA_CHANGED */
86         ENOENT,         /* EFI_NOT_FOUND */
87         EACCES,         /* EFI_ACCESS_DENIED */
88         ETIMEDOUT,      /* EFI_NO_RESPONSE */
89         EADDRNOTAVAIL,  /* EFI_NO_MAPPING */
90         ETIMEDOUT,      /* EFI_TIMEOUT */
91         EDOOFUS,        /* EFI_NOT_STARTED */
92         EALREADY,       /* EFI_ALREADY_STARTED */
93         ECANCELED,      /* EFI_ABORTED */
94         EPROTO,         /* EFI_ICMP_ERROR */
95         EPROTO,         /* EFI_TFTP_ERROR */
96         EPROTO          /* EFI_PROTOCOL_ERROR */
97 };
98
99 static int efi_enter(void);
100 static void efi_leave(void);
101
102 static int
103 efi_status_to_errno(efi_status status)
104 {
105         u_long code;
106
107         code = status & 0x3ffffffffffffffful;
108         return (code < nitems(efi_status2err) ? efi_status2err[code] : EDOOFUS);
109 }
110
111 static struct mtx efi_lock;
112 static SYSCTL_NODE(_hw, OID_AUTO, efi, CTLFLAG_RWTUN | CTLFLAG_MPSAFE, NULL,
113     "EFI");
114 static bool efi_poweroff = true;
115 SYSCTL_BOOL(_hw_efi, OID_AUTO, poweroff, CTLFLAG_RWTUN, &efi_poweroff, 0,
116     "If true, use EFI runtime services to power off in preference to ACPI");
117
118 static bool
119 efi_is_in_map(struct efi_md *map, int ndesc, int descsz, vm_offset_t addr)
120 {
121         struct efi_md *p;
122         int i;
123
124         for (i = 0, p = map; i < ndesc; i++, p = efi_next_descriptor(p,
125             descsz)) {
126                 if ((p->md_attr & EFI_MD_ATTR_RT) == 0)
127                         continue;
128
129                 if (addr >= (uintptr_t)p->md_virt &&
130                     addr < (uintptr_t)p->md_virt + p->md_pages * PAGE_SIZE)
131                         return (true);
132         }
133
134         return (false);
135 }
136
137 static void
138 efi_shutdown_final(void *dummy __unused, int howto)
139 {
140
141         /*
142          * On some systems, ACPI S5 is missing or does not function properly.
143          * When present, shutdown via EFI Runtime Services instead, unless
144          * disabled.
145          */
146         if ((howto & RB_POWEROFF) != 0 && efi_poweroff)
147                 (void)efi_reset_system(EFI_RESET_SHUTDOWN);
148 }
149
150 static int
151 efi_init(void)
152 {
153         struct efi_map_header *efihdr;
154         struct efi_md *map;
155         struct efi_rt *rtdm;
156         caddr_t kmdp;
157         size_t efisz;
158         int ndesc, rt_disabled;
159
160         rt_disabled = 0;
161         TUNABLE_INT_FETCH("efi.rt.disabled", &rt_disabled);
162         if (rt_disabled == 1)
163                 return (0);
164         mtx_init(&efi_lock, "efi", NULL, MTX_DEF);
165
166         if (efi_systbl_phys == 0) {
167                 if (bootverbose)
168                         printf("EFI systbl not available\n");
169                 return (0);
170         }
171
172         efi_systbl = (struct efi_systbl *)efi_phys_to_kva(efi_systbl_phys);
173         if (efi_systbl == NULL || efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) {
174                 efi_systbl = NULL;
175                 if (bootverbose)
176                         printf("EFI systbl signature invalid\n");
177                 return (0);
178         }
179         efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL :
180             (struct efi_cfgtbl *)efi_systbl->st_cfgtbl;
181         if (efi_cfgtbl == NULL) {
182                 if (bootverbose)
183                         printf("EFI config table is not present\n");
184         }
185
186         kmdp = preload_search_by_type("elf kernel");
187         if (kmdp == NULL)
188                 kmdp = preload_search_by_type("elf64 kernel");
189         efihdr = (struct efi_map_header *)preload_search_info(kmdp,
190             MODINFO_METADATA | MODINFOMD_EFI_MAP);
191         if (efihdr == NULL) {
192                 if (bootverbose)
193                         printf("EFI map is not present\n");
194                 return (0);
195         }
196         efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
197         map = (struct efi_md *)((uint8_t *)efihdr + efisz);
198         if (efihdr->descriptor_size == 0)
199                 return (ENOMEM);
200
201         ndesc = efihdr->memory_size / efihdr->descriptor_size;
202         if (!efi_create_1t1_map(map, ndesc, efihdr->descriptor_size)) {
203                 if (bootverbose)
204                         printf("EFI cannot create runtime map\n");
205                 return (ENOMEM);
206         }
207
208         efi_runtime = (efi_systbl->st_rt == 0) ? NULL :
209             (struct efi_rt *)efi_systbl->st_rt;
210         if (efi_runtime == NULL) {
211                 if (bootverbose)
212                         printf("EFI runtime services table is not present\n");
213                 efi_destroy_1t1_map();
214                 return (ENXIO);
215         }
216
217 #if defined(__aarch64__) || defined(__amd64__)
218         /*
219          * Some UEFI implementations have multiple implementations of the
220          * RS->GetTime function. They switch from one we can only use early
221          * in the boot process to one valid as a RunTime service only when we
222          * call RS->SetVirtualAddressMap. As this is not always the case, e.g.
223          * with an old loader.efi, check if the RS->GetTime function is within
224          * the EFI map, and fail to attach if not.
225          */
226         rtdm = (struct efi_rt *)efi_phys_to_kva((uintptr_t)efi_runtime);
227         if (rtdm == NULL || !efi_is_in_map(map, ndesc, efihdr->descriptor_size,
228             (vm_offset_t)rtdm->rt_gettime)) {
229                 if (bootverbose)
230                         printf(
231                          "EFI runtime services table has an invalid pointer\n");
232                 efi_runtime = NULL;
233                 efi_destroy_1t1_map();
234                 return (ENXIO);
235         }
236 #endif
237
238         /*
239          * We use SHUTDOWN_PRI_LAST - 1 to trigger after IPMI, but before ACPI.
240          */
241         efi_shutdown_tag = EVENTHANDLER_REGISTER(shutdown_final,
242             efi_shutdown_final, NULL, SHUTDOWN_PRI_LAST - 1);
243
244         return (0);
245 }
246
247 static void
248 efi_uninit(void)
249 {
250
251         /* Most likely disabled by tunable */
252         if (efi_runtime == NULL)
253                 return;
254         if (efi_shutdown_tag != NULL)
255                 EVENTHANDLER_DEREGISTER(shutdown_final, efi_shutdown_tag);
256         efi_destroy_1t1_map();
257
258         efi_systbl = NULL;
259         efi_cfgtbl = NULL;
260         efi_runtime = NULL;
261
262         mtx_destroy(&efi_lock);
263 }
264
265 int
266 efi_rt_ok(void)
267 {
268
269         if (efi_runtime == NULL)
270                 return (ENXIO);
271         return (0);
272 }
273
274 static int
275 efi_enter(void)
276 {
277         struct thread *td;
278         pmap_t curpmap;
279         int error;
280
281         if (efi_runtime == NULL)
282                 return (ENXIO);
283         td = curthread;
284         curpmap = &td->td_proc->p_vmspace->vm_pmap;
285         PMAP_LOCK(curpmap);
286         mtx_lock(&efi_lock);
287         fpu_kern_enter(td, NULL, FPU_KERN_NOCTX);
288         error = efi_arch_enter();
289         if (error != 0) {
290                 fpu_kern_leave(td, NULL);
291                 mtx_unlock(&efi_lock);
292                 PMAP_UNLOCK(curpmap);
293         }
294         return (error);
295 }
296
297 static void
298 efi_leave(void)
299 {
300         struct thread *td;
301         pmap_t curpmap;
302
303         efi_arch_leave();
304
305         curpmap = &curproc->p_vmspace->vm_pmap;
306         td = curthread;
307         fpu_kern_leave(td, NULL);
308         mtx_unlock(&efi_lock);
309         PMAP_UNLOCK(curpmap);
310 }
311
312 int
313 efi_get_table(struct uuid *uuid, void **ptr)
314 {
315         struct efi_cfgtbl *ct;
316         u_long count;
317
318         if (efi_cfgtbl == NULL || efi_systbl == NULL)
319                 return (ENXIO);
320         count = efi_systbl->st_entries;
321         ct = efi_cfgtbl;
322         while (count--) {
323                 if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) {
324                         *ptr = (void *)efi_phys_to_kva(ct->ct_data);
325                         return (0);
326                 }
327                 ct++;
328         }
329         return (ENOENT);
330 }
331
332 static int efi_rt_handle_faults = EFI_RT_HANDLE_FAULTS_DEFAULT;
333 SYSCTL_INT(_machdep, OID_AUTO, efi_rt_handle_faults, CTLFLAG_RWTUN,
334     &efi_rt_handle_faults, 0,
335     "Call EFI RT methods with fault handler wrapper around");
336
337 static int
338 efi_rt_arch_call_nofault(struct efirt_callinfo *ec)
339 {
340
341         switch (ec->ec_argcnt) {
342         case 0:
343                 ec->ec_efi_status = ((register_t (*)(void))ec->ec_fptr)();
344                 break;
345         case 1:
346                 ec->ec_efi_status = ((register_t (*)(register_t))ec->ec_fptr)
347                     (ec->ec_arg1);
348                 break;
349         case 2:
350                 ec->ec_efi_status = ((register_t (*)(register_t, register_t))
351                     ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2);
352                 break;
353         case 3:
354                 ec->ec_efi_status = ((register_t (*)(register_t, register_t,
355                     register_t))ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2,
356                     ec->ec_arg3);
357                 break;
358         case 4:
359                 ec->ec_efi_status = ((register_t (*)(register_t, register_t,
360                     register_t, register_t))ec->ec_fptr)(ec->ec_arg1,
361                     ec->ec_arg2, ec->ec_arg3, ec->ec_arg4);
362                 break;
363         case 5:
364                 ec->ec_efi_status = ((register_t (*)(register_t, register_t,
365                     register_t, register_t, register_t))ec->ec_fptr)(
366                     ec->ec_arg1, ec->ec_arg2, ec->ec_arg3, ec->ec_arg4,
367                     ec->ec_arg5);
368                 break;
369         default:
370                 panic("efi_rt_arch_call: %d args", (int)ec->ec_argcnt);
371         }
372
373         return (0);
374 }
375
376 static int
377 efi_call(struct efirt_callinfo *ecp)
378 {
379         int error;
380
381         error = efi_enter();
382         if (error != 0)
383                 return (error);
384         error = efi_rt_handle_faults ? efi_rt_arch_call(ecp) :
385             efi_rt_arch_call_nofault(ecp);
386         efi_leave();
387         if (error == 0)
388                 error = efi_status_to_errno(ecp->ec_efi_status);
389         else if (bootverbose)
390                 printf("EFI %s call faulted, error %d\n", ecp->ec_name, error);
391         return (error);
392 }
393
394 #define EFI_RT_METHOD_PA(method)                                \
395     ((uintptr_t)((struct efi_rt *)efi_phys_to_kva((uintptr_t)   \
396     efi_runtime))->method)
397
398 static int
399 efi_get_time_locked(struct efi_tm *tm, struct efi_tmcap *tmcap)
400 {
401         struct efirt_callinfo ec;
402
403         EFI_TIME_OWNED();
404         if (efi_runtime == NULL)
405                 return (ENXIO);
406         bzero(&ec, sizeof(ec));
407         ec.ec_name = "rt_gettime";
408         ec.ec_argcnt = 2;
409         ec.ec_arg1 = (uintptr_t)tm;
410         ec.ec_arg2 = (uintptr_t)tmcap;
411         ec.ec_fptr = EFI_RT_METHOD_PA(rt_gettime);
412         return (efi_call(&ec));
413 }
414
415 int
416 efi_get_time(struct efi_tm *tm)
417 {
418         struct efi_tmcap dummy;
419         int error;
420
421         if (efi_runtime == NULL)
422                 return (ENXIO);
423         EFI_TIME_LOCK();
424         /*
425          * UEFI spec states that the Capabilities argument to GetTime is
426          * optional, but some UEFI implementations choke when passed a NULL
427          * pointer. Pass a dummy efi_tmcap, even though we won't use it,
428          * to workaround such implementations.
429          */
430         error = efi_get_time_locked(tm, &dummy);
431         EFI_TIME_UNLOCK();
432         return (error);
433 }
434
435 int
436 efi_get_time_capabilities(struct efi_tmcap *tmcap)
437 {
438         struct efi_tm dummy;
439         int error;
440
441         if (efi_runtime == NULL)
442                 return (ENXIO);
443         EFI_TIME_LOCK();
444         error = efi_get_time_locked(&dummy, tmcap);
445         EFI_TIME_UNLOCK();
446         return (error);
447 }
448
449 int
450 efi_reset_system(enum efi_reset type)
451 {
452         struct efirt_callinfo ec;
453
454         switch (type) {
455         case EFI_RESET_COLD:
456         case EFI_RESET_WARM:
457         case EFI_RESET_SHUTDOWN:
458                 break;
459         default:
460                 return (EINVAL);
461         }
462         if (efi_runtime == NULL)
463                 return (ENXIO);
464         bzero(&ec, sizeof(ec));
465         ec.ec_name = "rt_reset";
466         ec.ec_argcnt = 4;
467         ec.ec_arg1 = (uintptr_t)type;
468         ec.ec_arg2 = (uintptr_t)0;
469         ec.ec_arg3 = (uintptr_t)0;
470         ec.ec_arg4 = (uintptr_t)NULL;
471         ec.ec_fptr = EFI_RT_METHOD_PA(rt_reset);
472         return (efi_call(&ec));
473 }
474
475 static int
476 efi_set_time_locked(struct efi_tm *tm)
477 {
478         struct efirt_callinfo ec;
479
480         EFI_TIME_OWNED();
481         if (efi_runtime == NULL)
482                 return (ENXIO);
483         bzero(&ec, sizeof(ec));
484         ec.ec_name = "rt_settime";
485         ec.ec_argcnt = 1;
486         ec.ec_arg1 = (uintptr_t)tm;
487         ec.ec_fptr = EFI_RT_METHOD_PA(rt_settime);
488         return (efi_call(&ec));
489 }
490
491 int
492 efi_set_time(struct efi_tm *tm)
493 {
494         int error;
495
496         if (efi_runtime == NULL)
497                 return (ENXIO);
498         EFI_TIME_LOCK();
499         error = efi_set_time_locked(tm);
500         EFI_TIME_UNLOCK();
501         return (error);
502 }
503
504 int
505 efi_var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib,
506     size_t *datasize, void *data)
507 {
508         struct efirt_callinfo ec;
509
510         if (efi_runtime == NULL)
511                 return (ENXIO);
512         bzero(&ec, sizeof(ec));
513         ec.ec_argcnt = 5;
514         ec.ec_name = "rt_getvar";
515         ec.ec_arg1 = (uintptr_t)name;
516         ec.ec_arg2 = (uintptr_t)vendor;
517         ec.ec_arg3 = (uintptr_t)attrib;
518         ec.ec_arg4 = (uintptr_t)datasize;
519         ec.ec_arg5 = (uintptr_t)data;
520         ec.ec_fptr = EFI_RT_METHOD_PA(rt_getvar);
521         return (efi_call(&ec));
522 }
523
524 int
525 efi_var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor)
526 {
527         struct efirt_callinfo ec;
528
529         if (efi_runtime == NULL)
530                 return (ENXIO);
531         bzero(&ec, sizeof(ec));
532         ec.ec_argcnt = 3;
533         ec.ec_name = "rt_scanvar";
534         ec.ec_arg1 = (uintptr_t)namesize;
535         ec.ec_arg2 = (uintptr_t)name;
536         ec.ec_arg3 = (uintptr_t)vendor;
537         ec.ec_fptr = EFI_RT_METHOD_PA(rt_scanvar);
538         return (efi_call(&ec));
539 }
540
541 int
542 efi_var_set(efi_char *name, struct uuid *vendor, uint32_t attrib,
543     size_t datasize, void *data)
544 {
545         struct efirt_callinfo ec;
546
547         if (efi_runtime == NULL)
548                 return (ENXIO);
549         bzero(&ec, sizeof(ec));
550         ec.ec_argcnt = 5;
551         ec.ec_name = "rt_setvar";
552         ec.ec_arg1 = (uintptr_t)name;
553         ec.ec_arg2 = (uintptr_t)vendor;
554         ec.ec_arg3 = (uintptr_t)attrib;
555         ec.ec_arg4 = (uintptr_t)datasize;
556         ec.ec_arg5 = (uintptr_t)data;
557         ec.ec_fptr = EFI_RT_METHOD_PA(rt_setvar);
558         return (efi_call(&ec));
559 }
560
561 static int
562 efirt_modevents(module_t m, int event, void *arg __unused)
563 {
564
565         switch (event) {
566         case MOD_LOAD:
567                 return (efi_init());
568
569         case MOD_UNLOAD:
570                 efi_uninit();
571                 return (0);
572
573         case MOD_SHUTDOWN:
574                 return (0);
575
576         default:
577                 return (EOPNOTSUPP);
578         }
579 }
580
581 static moduledata_t efirt_moddata = {
582         .name = "efirt",
583         .evhand = efirt_modevents,
584         .priv = NULL,
585 };
586 /* After fpuinitstate, before efidev */
587 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_DRIVERS, SI_ORDER_SECOND);
588 MODULE_VERSION(efirt, 1);