]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/efidev/efirt.c
MFV r357783:
[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, NULL, "EFI");
113 static bool efi_poweroff = true;
114 SYSCTL_BOOL(_hw_efi, OID_AUTO, poweroff, CTLFLAG_RWTUN, &efi_poweroff, 0,
115     "If true, use EFI runtime services to power off in preference to ACPI");
116
117 static bool
118 efi_is_in_map(struct efi_md *map, int ndesc, int descsz, vm_offset_t addr)
119 {
120         struct efi_md *p;
121         int i;
122
123         for (i = 0, p = map; i < ndesc; i++, p = efi_next_descriptor(p,
124             descsz)) {
125                 if ((p->md_attr & EFI_MD_ATTR_RT) == 0)
126                         continue;
127
128                 if (addr >= (uintptr_t)p->md_virt &&
129                     addr < (uintptr_t)p->md_virt + p->md_pages * PAGE_SIZE)
130                         return (true);
131         }
132
133         return (false);
134 }
135
136 static void
137 efi_shutdown_final(void *dummy __unused, int howto)
138 {
139
140         /*
141          * On some systems, ACPI S5 is missing or does not function properly.
142          * When present, shutdown via EFI Runtime Services instead, unless
143          * disabled.
144          */
145         if ((howto & RB_POWEROFF) != 0 && efi_poweroff)
146                 (void)efi_reset_system(EFI_RESET_SHUTDOWN);
147 }
148
149 static int
150 efi_init(void)
151 {
152         struct efi_map_header *efihdr;
153         struct efi_md *map;
154         struct efi_rt *rtdm;
155         caddr_t kmdp;
156         size_t efisz;
157         int ndesc, rt_disabled;
158
159         rt_disabled = 0;
160         TUNABLE_INT_FETCH("efi.rt.disabled", &rt_disabled);
161         if (rt_disabled == 1)
162                 return (0);
163         mtx_init(&efi_lock, "efi", NULL, MTX_DEF);
164
165         if (efi_systbl_phys == 0) {
166                 if (bootverbose)
167                         printf("EFI systbl not available\n");
168                 return (0);
169         }
170
171         efi_systbl = (struct efi_systbl *)efi_phys_to_kva(efi_systbl_phys);
172         if (efi_systbl == NULL || efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) {
173                 efi_systbl = NULL;
174                 if (bootverbose)
175                         printf("EFI systbl signature invalid\n");
176                 return (0);
177         }
178         efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL :
179             (struct efi_cfgtbl *)efi_systbl->st_cfgtbl;
180         if (efi_cfgtbl == NULL) {
181                 if (bootverbose)
182                         printf("EFI config table is not present\n");
183         }
184
185         kmdp = preload_search_by_type("elf kernel");
186         if (kmdp == NULL)
187                 kmdp = preload_search_by_type("elf64 kernel");
188         efihdr = (struct efi_map_header *)preload_search_info(kmdp,
189             MODINFO_METADATA | MODINFOMD_EFI_MAP);
190         if (efihdr == NULL) {
191                 if (bootverbose)
192                         printf("EFI map is not present\n");
193                 return (0);
194         }
195         efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
196         map = (struct efi_md *)((uint8_t *)efihdr + efisz);
197         if (efihdr->descriptor_size == 0)
198                 return (ENOMEM);
199
200         ndesc = efihdr->memory_size / efihdr->descriptor_size;
201         if (!efi_create_1t1_map(map, ndesc, efihdr->descriptor_size)) {
202                 if (bootverbose)
203                         printf("EFI cannot create runtime map\n");
204                 return (ENOMEM);
205         }
206
207         efi_runtime = (efi_systbl->st_rt == 0) ? NULL :
208             (struct efi_rt *)efi_systbl->st_rt;
209         if (efi_runtime == NULL) {
210                 if (bootverbose)
211                         printf("EFI runtime services table is not present\n");
212                 efi_destroy_1t1_map();
213                 return (ENXIO);
214         }
215
216 #if defined(__aarch64__) || defined(__amd64__)
217         /*
218          * Some UEFI implementations have multiple implementations of the
219          * RS->GetTime function. They switch from one we can only use early
220          * in the boot process to one valid as a RunTime service only when we
221          * call RS->SetVirtualAddressMap. As this is not always the case, e.g.
222          * with an old loader.efi, check if the RS->GetTime function is within
223          * the EFI map, and fail to attach if not.
224          */
225         rtdm = (struct efi_rt *)efi_phys_to_kva((uintptr_t)efi_runtime);
226         if (rtdm == NULL || !efi_is_in_map(map, ndesc, efihdr->descriptor_size,
227             (vm_offset_t)rtdm->rt_gettime)) {
228                 if (bootverbose)
229                         printf(
230                          "EFI runtime services table has an invalid pointer\n");
231                 efi_runtime = NULL;
232                 efi_destroy_1t1_map();
233                 return (ENXIO);
234         }
235 #endif
236
237         /*
238          * We use SHUTDOWN_PRI_LAST - 1 to trigger after IPMI, but before ACPI.
239          */
240         efi_shutdown_tag = EVENTHANDLER_REGISTER(shutdown_final,
241             efi_shutdown_final, NULL, SHUTDOWN_PRI_LAST - 1);
242
243         return (0);
244 }
245
246 static void
247 efi_uninit(void)
248 {
249
250         /* Most likely disabled by tunable */
251         if (efi_runtime == NULL)
252                 return;
253         if (efi_shutdown_tag != NULL)
254                 EVENTHANDLER_DEREGISTER(shutdown_final, efi_shutdown_tag);
255         efi_destroy_1t1_map();
256
257         efi_systbl = NULL;
258         efi_cfgtbl = NULL;
259         efi_runtime = NULL;
260
261         mtx_destroy(&efi_lock);
262 }
263
264 int
265 efi_rt_ok(void)
266 {
267
268         if (efi_runtime == NULL)
269                 return (ENXIO);
270         return (0);
271 }
272
273 static int
274 efi_enter(void)
275 {
276         struct thread *td;
277         pmap_t curpmap;
278         int error;
279
280         if (efi_runtime == NULL)
281                 return (ENXIO);
282         td = curthread;
283         curpmap = &td->td_proc->p_vmspace->vm_pmap;
284         PMAP_LOCK(curpmap);
285         mtx_lock(&efi_lock);
286         fpu_kern_enter(td, NULL, FPU_KERN_NOCTX);
287         error = efi_arch_enter();
288         if (error != 0) {
289                 fpu_kern_leave(td, NULL);
290                 mtx_unlock(&efi_lock);
291                 PMAP_UNLOCK(curpmap);
292         }
293         return (error);
294 }
295
296 static void
297 efi_leave(void)
298 {
299         struct thread *td;
300         pmap_t curpmap;
301
302         efi_arch_leave();
303
304         curpmap = &curproc->p_vmspace->vm_pmap;
305         td = curthread;
306         fpu_kern_leave(td, NULL);
307         mtx_unlock(&efi_lock);
308         PMAP_UNLOCK(curpmap);
309 }
310
311 int
312 efi_get_table(struct uuid *uuid, void **ptr)
313 {
314         struct efi_cfgtbl *ct;
315         u_long count;
316
317         if (efi_cfgtbl == NULL || efi_systbl == NULL)
318                 return (ENXIO);
319         count = efi_systbl->st_entries;
320         ct = efi_cfgtbl;
321         while (count--) {
322                 if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) {
323                         *ptr = (void *)efi_phys_to_kva(ct->ct_data);
324                         return (0);
325                 }
326                 ct++;
327         }
328         return (ENOENT);
329 }
330
331 static int efi_rt_handle_faults = EFI_RT_HANDLE_FAULTS_DEFAULT;
332 SYSCTL_INT(_machdep, OID_AUTO, efi_rt_handle_faults, CTLFLAG_RWTUN,
333     &efi_rt_handle_faults, 0,
334     "Call EFI RT methods with fault handler wrapper around");
335
336 static int
337 efi_rt_arch_call_nofault(struct efirt_callinfo *ec)
338 {
339
340         switch (ec->ec_argcnt) {
341         case 0:
342                 ec->ec_efi_status = ((register_t (*)(void))ec->ec_fptr)();
343                 break;
344         case 1:
345                 ec->ec_efi_status = ((register_t (*)(register_t))ec->ec_fptr)
346                     (ec->ec_arg1);
347                 break;
348         case 2:
349                 ec->ec_efi_status = ((register_t (*)(register_t, register_t))
350                     ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2);
351                 break;
352         case 3:
353                 ec->ec_efi_status = ((register_t (*)(register_t, register_t,
354                     register_t))ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2,
355                     ec->ec_arg3);
356                 break;
357         case 4:
358                 ec->ec_efi_status = ((register_t (*)(register_t, register_t,
359                     register_t, register_t))ec->ec_fptr)(ec->ec_arg1,
360                     ec->ec_arg2, ec->ec_arg3, ec->ec_arg4);
361                 break;
362         case 5:
363                 ec->ec_efi_status = ((register_t (*)(register_t, register_t,
364                     register_t, register_t, register_t))ec->ec_fptr)(
365                     ec->ec_arg1, ec->ec_arg2, ec->ec_arg3, ec->ec_arg4,
366                     ec->ec_arg5);
367                 break;
368         default:
369                 panic("efi_rt_arch_call: %d args", (int)ec->ec_argcnt);
370         }
371
372         return (0);
373 }
374
375 static int
376 efi_call(struct efirt_callinfo *ecp)
377 {
378         int error;
379
380         error = efi_enter();
381         if (error != 0)
382                 return (error);
383         error = efi_rt_handle_faults ? efi_rt_arch_call(ecp) :
384             efi_rt_arch_call_nofault(ecp);
385         efi_leave();
386         if (error == 0)
387                 error = efi_status_to_errno(ecp->ec_efi_status);
388         else if (bootverbose)
389                 printf("EFI %s call faulted, error %d\n", ecp->ec_name, error);
390         return (error);
391 }
392
393 #define EFI_RT_METHOD_PA(method)                                \
394     ((uintptr_t)((struct efi_rt *)efi_phys_to_kva((uintptr_t)   \
395     efi_runtime))->method)
396
397 static int
398 efi_get_time_locked(struct efi_tm *tm, struct efi_tmcap *tmcap)
399 {
400         struct efirt_callinfo ec;
401
402         EFI_TIME_OWNED();
403         if (efi_runtime == NULL)
404                 return (ENXIO);
405         bzero(&ec, sizeof(ec));
406         ec.ec_name = "rt_gettime";
407         ec.ec_argcnt = 2;
408         ec.ec_arg1 = (uintptr_t)tm;
409         ec.ec_arg2 = (uintptr_t)tmcap;
410         ec.ec_fptr = EFI_RT_METHOD_PA(rt_gettime);
411         return (efi_call(&ec));
412 }
413
414 int
415 efi_get_time(struct efi_tm *tm)
416 {
417         struct efi_tmcap dummy;
418         int error;
419
420         if (efi_runtime == NULL)
421                 return (ENXIO);
422         EFI_TIME_LOCK();
423         /*
424          * UEFI spec states that the Capabilities argument to GetTime is
425          * optional, but some UEFI implementations choke when passed a NULL
426          * pointer. Pass a dummy efi_tmcap, even though we won't use it,
427          * to workaround such implementations.
428          */
429         error = efi_get_time_locked(tm, &dummy);
430         EFI_TIME_UNLOCK();
431         return (error);
432 }
433
434 int
435 efi_get_time_capabilities(struct efi_tmcap *tmcap)
436 {
437         struct efi_tm dummy;
438         int error;
439
440         if (efi_runtime == NULL)
441                 return (ENXIO);
442         EFI_TIME_LOCK();
443         error = efi_get_time_locked(&dummy, tmcap);
444         EFI_TIME_UNLOCK();
445         return (error);
446 }
447
448 int
449 efi_reset_system(enum efi_reset type)
450 {
451         struct efirt_callinfo ec;
452
453         switch (type) {
454         case EFI_RESET_COLD:
455         case EFI_RESET_WARM:
456         case EFI_RESET_SHUTDOWN:
457                 break;
458         default:
459                 return (EINVAL);
460         }
461         if (efi_runtime == NULL)
462                 return (ENXIO);
463         bzero(&ec, sizeof(ec));
464         ec.ec_name = "rt_reset";
465         ec.ec_argcnt = 4;
466         ec.ec_arg1 = (uintptr_t)type;
467         ec.ec_arg2 = (uintptr_t)0;
468         ec.ec_arg3 = (uintptr_t)0;
469         ec.ec_arg4 = (uintptr_t)NULL;
470         ec.ec_fptr = EFI_RT_METHOD_PA(rt_reset);
471         return (efi_call(&ec));
472 }
473
474 static int
475 efi_set_time_locked(struct efi_tm *tm)
476 {
477         struct efirt_callinfo ec;
478
479         EFI_TIME_OWNED();
480         if (efi_runtime == NULL)
481                 return (ENXIO);
482         bzero(&ec, sizeof(ec));
483         ec.ec_name = "rt_settime";
484         ec.ec_argcnt = 1;
485         ec.ec_arg1 = (uintptr_t)tm;
486         ec.ec_fptr = EFI_RT_METHOD_PA(rt_settime);
487         return (efi_call(&ec));
488 }
489
490 int
491 efi_set_time(struct efi_tm *tm)
492 {
493         int error;
494
495         if (efi_runtime == NULL)
496                 return (ENXIO);
497         EFI_TIME_LOCK();
498         error = efi_set_time_locked(tm);
499         EFI_TIME_UNLOCK();
500         return (error);
501 }
502
503 int
504 efi_var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib,
505     size_t *datasize, void *data)
506 {
507         struct efirt_callinfo ec;
508
509         if (efi_runtime == NULL)
510                 return (ENXIO);
511         bzero(&ec, sizeof(ec));
512         ec.ec_argcnt = 5;
513         ec.ec_name = "rt_getvar";
514         ec.ec_arg1 = (uintptr_t)name;
515         ec.ec_arg2 = (uintptr_t)vendor;
516         ec.ec_arg3 = (uintptr_t)attrib;
517         ec.ec_arg4 = (uintptr_t)datasize;
518         ec.ec_arg5 = (uintptr_t)data;
519         ec.ec_fptr = EFI_RT_METHOD_PA(rt_getvar);
520         return (efi_call(&ec));
521 }
522
523 int
524 efi_var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor)
525 {
526         struct efirt_callinfo ec;
527
528         if (efi_runtime == NULL)
529                 return (ENXIO);
530         bzero(&ec, sizeof(ec));
531         ec.ec_argcnt = 3;
532         ec.ec_name = "rt_scanvar";
533         ec.ec_arg1 = (uintptr_t)namesize;
534         ec.ec_arg2 = (uintptr_t)name;
535         ec.ec_arg3 = (uintptr_t)vendor;
536         ec.ec_fptr = EFI_RT_METHOD_PA(rt_scanvar);
537         return (efi_call(&ec));
538 }
539
540 int
541 efi_var_set(efi_char *name, struct uuid *vendor, uint32_t attrib,
542     size_t datasize, void *data)
543 {
544         struct efirt_callinfo ec;
545
546         if (efi_runtime == NULL)
547                 return (ENXIO);
548         bzero(&ec, sizeof(ec));
549         ec.ec_argcnt = 5;
550         ec.ec_name = "rt_setvar";
551         ec.ec_arg1 = (uintptr_t)name;
552         ec.ec_arg2 = (uintptr_t)vendor;
553         ec.ec_arg3 = (uintptr_t)attrib;
554         ec.ec_arg4 = (uintptr_t)datasize;
555         ec.ec_arg5 = (uintptr_t)data;
556         ec.ec_fptr = EFI_RT_METHOD_PA(rt_setvar);
557         return (efi_call(&ec));
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 /* After fpuinitstate, before efidev */
586 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_DRIVERS, SI_ORDER_SECOND);
587 MODULE_VERSION(efirt, 1);