Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/arch x86: Support EFI runtime services.



details:   https://anonhg.NetBSD.org/src/rev/81eb9d804afb
branches:  trunk
changeset: 370680:81eb9d804afb
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Sat Sep 24 11:05:17 2022 +0000

description:
x86: Support EFI runtime services.

This creates a special pmap, efi_runtime_pmap, which avoids setting
PTE_U but allows mappings to lie in what would normally be user VM --
this way we don't fall afoul of SMAP/SMEP when executing EFI runtime
services from CPL 0.  SVS does not apply to the EFI runtime pmap.

The mechanism is intended to work with either physical addressing or
virtual addressing; currently the bootloader does physical addressing
but in principle it could be modified to do virtual addressing
instead, if it allocated virtual pages, assigned them in the memory
map, and issued RT->SetVirtualAddressMap.

Not sure pmap_activate_sync and pmap_deactivate_sync are correct,
need more review from an x86 wizard.

If this causes fallout, it can be disabled temporarily without
reverting anything by just making efi_runtime_init return immediately
without doing anything, or by removing options EFI_RUNTIME.

amd64-only for now pending type fixes and testing on i386.

diffstat:

 sys/arch/amd64/conf/ALL             |    8 +-
 sys/arch/amd64/conf/GENERIC         |    8 +-
 sys/arch/amd64/include/efi.h        |    3 +
 sys/arch/i386/conf/ALL              |    8 +-
 sys/arch/i386/conf/GENERIC          |    8 +-
 sys/arch/i386/include/efi.h         |    3 +
 sys/arch/x86/conf/files.x86         |    4 +-
 sys/arch/x86/include/pmap_private.h |   20 +-
 sys/arch/x86/x86/cpu.c              |    6 +-
 sys/arch/x86/x86/efi_machdep.c      |  450 +++++++++++++++++++++++++++++++++++-
 sys/arch/x86/x86/pmap.c             |  119 +++++++++-
 sys/arch/x86/x86/svs.c              |    6 +-
 12 files changed, 621 insertions(+), 22 deletions(-)

diffs (truncated from 901 to 300 lines):

diff -r 92b25b0bd019 -r 81eb9d804afb sys/arch/amd64/conf/ALL
--- a/sys/arch/amd64/conf/ALL   Sat Sep 24 10:52:05 2022 +0000
+++ b/sys/arch/amd64/conf/ALL   Sat Sep 24 11:05:17 2022 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: ALL,v 1.173 2022/08/07 02:52:23 simonb Exp $
+# $NetBSD: ALL,v 1.174 2022/09/24 11:05:17 riastradh Exp $
 # From NetBSD: GENERIC,v 1.787 2006/10/01 18:37:54 bouyer Exp
 #
 # ALL machine description file
@@ -17,7 +17,7 @@
 
 options        INCLUDE_CONFIG_FILE     # embed config file in kernel binary
 
-#ident         "ALL-$Revision: 1.173 $"
+#ident         "ALL-$Revision: 1.174 $"
 
 maxusers       64              # estimated number of users
 
@@ -1708,6 +1708,10 @@
 # userland interface to drivers, including autoconf and properties retrieval
 pseudo-device  drvctl
 
+# EFI runtime support
+options        EFI_RUNTIME
+pseudo-device  efi                     # /dev/efi
+
 # Pass-to-Userspace Transporter
 pseudo-device  putter
 
diff -r 92b25b0bd019 -r 81eb9d804afb sys/arch/amd64/conf/GENERIC
--- a/sys/arch/amd64/conf/GENERIC       Sat Sep 24 10:52:05 2022 +0000
+++ b/sys/arch/amd64/conf/GENERIC       Sat Sep 24 11:05:17 2022 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: GENERIC,v 1.597 2022/09/01 12:35:02 bouyer Exp $
+# $NetBSD: GENERIC,v 1.598 2022/09/24 11:05:17 riastradh Exp $
 #
 # GENERIC machine description file
 #
@@ -22,7 +22,7 @@
 
 options        INCLUDE_CONFIG_FILE     # embed config file in kernel binary
 
-#ident         "GENERIC-$Revision: 1.597 $"
+#ident         "GENERIC-$Revision: 1.598 $"
 
 maxusers       64              # estimated number of users
 
@@ -1225,6 +1225,10 @@
 # userland interface to drivers, including autoconf and properties retrieval
 pseudo-device  drvctl
 
+# EFI runtime support
+options        EFI_RUNTIME
+pseudo-device  efi                     # /dev/efi
+
 include "dev/veriexec.config"
 
 options        PAX_SEGVGUARD=0         # PaX Segmentation fault guard
diff -r 92b25b0bd019 -r 81eb9d804afb sys/arch/amd64/include/efi.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/arch/amd64/include/efi.h      Sat Sep 24 11:05:17 2022 +0000
@@ -0,0 +1,3 @@
+/*     $NetBSD: efi.h,v 1.1 2022/09/24 11:05:17 riastradh Exp $        */
+
+#include <x86/efi.h>
diff -r 92b25b0bd019 -r 81eb9d804afb sys/arch/i386/conf/ALL
--- a/sys/arch/i386/conf/ALL    Sat Sep 24 10:52:05 2022 +0000
+++ b/sys/arch/i386/conf/ALL    Sat Sep 24 11:05:17 2022 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: ALL,v 1.502 2022/08/07 02:52:26 simonb Exp $
+# $NetBSD: ALL,v 1.503 2022/09/24 11:05:17 riastradh Exp $
 # From NetBSD: GENERIC,v 1.787 2006/10/01 18:37:54 bouyer Exp
 #
 # ALL machine description file
@@ -17,7 +17,7 @@
 
 options        INCLUDE_CONFIG_FILE     # embed config file in kernel binary
 
-#ident         "ALL-$Revision: 1.502 $"
+#ident         "ALL-$Revision: 1.503 $"
 
 maxusers       64              # estimated number of users
 
@@ -1805,6 +1805,10 @@
 # userland interface to drivers, including autoconf and properties retrieval
 pseudo-device  drvctl
 
+# EFI runtime support -- doesn't build yet
+#options       EFI_RUNTIME
+#pseudo-device         efi                     # /dev/efi
+
 # Pass-to-Userspace Transporter
 pseudo-device  putter
 
diff -r 92b25b0bd019 -r 81eb9d804afb sys/arch/i386/conf/GENERIC
--- a/sys/arch/i386/conf/GENERIC        Sat Sep 24 10:52:05 2022 +0000
+++ b/sys/arch/i386/conf/GENERIC        Sat Sep 24 11:05:17 2022 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: GENERIC,v 1.1240 2022/08/07 02:52:26 simonb Exp $
+# $NetBSD: GENERIC,v 1.1241 2022/09/24 11:05:17 riastradh Exp $
 #
 # GENERIC machine description file
 #
@@ -22,7 +22,7 @@
 
 options        INCLUDE_CONFIG_FILE     # embed config file in kernel binary
 
-#ident         "GENERIC-$Revision: 1.1240 $"
+#ident         "GENERIC-$Revision: 1.1241 $"
 
 maxusers       64              # estimated number of users
 
@@ -1507,6 +1507,10 @@
 # userland interface to drivers, including autoconf and properties retrieval
 pseudo-device  drvctl
 
+# EFI runtime support -- not yet tested
+#options       EFI_RUNTIME
+#pseudo-device         efi                     # /dev/efi
+
 include "dev/veriexec.config"
 
 options        PAX_ASLR_DEBUG=1        # PaX ASLR debug
diff -r 92b25b0bd019 -r 81eb9d804afb sys/arch/i386/include/efi.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/arch/i386/include/efi.h       Sat Sep 24 11:05:17 2022 +0000
@@ -0,0 +1,3 @@
+/*     $NetBSD: efi.h,v 1.1 2022/09/24 11:05:18 riastradh Exp $        */
+
+#include <x86/efi.h>
diff -r 92b25b0bd019 -r 81eb9d804afb sys/arch/x86/conf/files.x86
--- a/sys/arch/x86/conf/files.x86       Sat Sep 24 10:52:05 2022 +0000
+++ b/sys/arch/x86/conf/files.x86       Sat Sep 24 11:05:17 2022 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.x86,v 1.123 2022/08/30 11:03:36 riastradh Exp $
+#      $NetBSD: files.x86,v 1.124 2022/09/24 11:05:18 riastradh Exp $
 
 # options for MP configuration through the MP spec
 defflag opt_mpbios.h MPBIOS MPDEBUG MPBIOS_SCANPCI
@@ -21,6 +21,8 @@
 # Option to have a static kernel memory layout
 defflag opt_kaslr.h    NO_X86_ASLR
 
+defflag opt_efi.h      EFI_RUNTIME
+
 defflag        SVS
 
 defflag        PCPU_IDT
diff -r 92b25b0bd019 -r 81eb9d804afb sys/arch/x86/include/pmap_private.h
--- a/sys/arch/x86/include/pmap_private.h       Sat Sep 24 10:52:05 2022 +0000
+++ b/sys/arch/x86/include/pmap_private.h       Sat Sep 24 11:05:17 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pmap_private.h,v 1.3 2022/09/13 09:40:17 riastradh Exp $       */
+/*     $NetBSD: pmap_private.h,v 1.4 2022/09/24 11:05:18 riastradh Exp $       */
 
 /*
  * Copyright (c) 1997 Charles D. Cranor and Washington University.
@@ -378,4 +378,22 @@
 
 void   svs_quad_copy(void *, void *, long);
 
+#ifdef _KERNEL_OPT
+#include "opt_efi.h"
+#endif
+
+#ifdef EFI_RUNTIME
+void *         pmap_activate_sync(struct pmap *);
+void           pmap_deactivate_sync(struct pmap *, void *);
+bool           pmap_is_user(struct pmap *);
+#else
+static inline bool
+pmap_is_user(struct pmap *pmap)
+{
+
+       KASSERT(pmap != pmap_kernel());
+       return true;
+}
+#endif
+
 #endif /* _X86_PMAP_PRIVATE_H_ */
diff -r 92b25b0bd019 -r 81eb9d804afb sys/arch/x86/x86/cpu.c
--- a/sys/arch/x86/x86/cpu.c    Sat Sep 24 10:52:05 2022 +0000
+++ b/sys/arch/x86/x86/cpu.c    Sat Sep 24 11:05:17 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cpu.c,v 1.205 2022/08/20 23:48:51 riastradh Exp $      */
+/*     $NetBSD: cpu.c,v 1.206 2022/09/24 11:05:18 riastradh Exp $      */
 
 /*
  * Copyright (c) 2000-2020 NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.205 2022/08/20 23:48:51 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.206 2022/09/24 11:05:18 riastradh Exp $");
 
 #include "opt_ddb.h"
 #include "opt_mpbios.h"                /* for MPDEBUG */
@@ -1434,7 +1434,7 @@
 cpu_load_pmap(struct pmap *pmap, struct pmap *oldpmap)
 {
 #ifdef SVS
-       if (svs_enabled) {
+       if (svs_enabled && pmap_is_user(pmap)) {
                svs_pdir_switch(pmap);
        }
 #endif
diff -r 92b25b0bd019 -r 81eb9d804afb sys/arch/x86/x86/efi_machdep.c
--- a/sys/arch/x86/x86/efi_machdep.c    Sat Sep 24 10:52:05 2022 +0000
+++ b/sys/arch/x86/x86/efi_machdep.c    Sat Sep 24 11:05:17 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: efi_machdep.c,v 1.1 2022/08/30 11:03:36 riastradh Exp $        */
+/*     $NetBSD: efi_machdep.c,v 1.2 2022/09/24 11:05:18 riastradh Exp $        */
 
 /*-
  * Copyright (c) 2016 The NetBSD Foundation, Inc.
@@ -27,7 +27,10 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: efi_machdep.c,v 1.1 2022/08/30 11:03:36 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: efi_machdep.c,v 1.2 2022/09/24 11:05:18 riastradh Exp $");
+
+#include "efi.h"
+#include "opt_efi.h"
 
 #include <sys/kmem.h>
 #include <sys/param.h>
@@ -37,9 +40,12 @@
 #include <uvm/uvm_extern.h>
 
 #include <machine/bootinfo.h>
+#include <machine/pmap_private.h>
+
 #include <x86/bus_defs.h>
 #include <x86/bus_funcs.h>
 #include <x86/efi.h>
+#include <x86/fpu.h>
 
 #include <dev/mm.h>
 #if NPCI > 0
@@ -67,6 +73,26 @@
        struct bi_memmap_entry entry[VM_PHYSSEG_MAX - 1];
 } efi_e820memmap;
 
+#ifdef EFI_RUNTIME
+
+#include <dev/efivar.h>
+
+#include <uvm/uvm_extern.h>
+
+#if !(NEFI > 0)
+#error options EFI_RUNTIME makes no sense without pseudo-device efi.
+#endif
+
+struct pmap *efi_runtime_pmap __read_mostly;
+
+static kmutex_t efi_runtime_lock __cacheline_aligned;
+static struct efi_rt efi_rt __read_mostly;
+static struct efi_ops efi_runtime_ops __read_mostly;
+
+static void efi_runtime_init(void);
+
+#endif
+
 /*
  * Map a physical address (PA) to a newly allocated virtual address (VA).
  * The VA must be freed using efi_relva().
@@ -408,6 +434,10 @@
 #if NPCI > 0
        pci_mapreg_map_enable_decode = true; /* PR port-amd64/53286 */
 #endif
+
+#ifdef EFI_RUNTIME
+       efi_runtime_init();
+#endif
 }
 
 bool
@@ -548,3 +578,419 @@
        efi_e820memmap.bim.common.type = BTINFO_MEMMAP;
        return &efi_e820memmap.bim;
 }
+
+#ifdef EFI_RUNTIME
+
+/*
+ * XXX move to sys/dev/efi/efi.h
+ */
+#ifdef _LP64
+#define        EFIERR(x)       (0x8000000000000000ul | (x))
+#else
+#define        EFIERR(x)       (0x80000000ul | (x))
+#endif
+
+#define        EFI_UNSUPPORTED         EFIERR(3)
+#define        EFI_DEVICE_ERROR        EFIERR(7)
+
+/*
+ * efi_runtime_init()



Home | Main Index | Thread Index | Old Index