Skip to contentSkip to conversation

x86: Trenchboot Secure Launch DRTM for AMD SKINIT (GRUB)

RFC PATCH Superseded Sergii Dmytruk 1 participant 8 messages Apr 30, 2025

lore
Share & tools
lore
#0 lore raw

v2 cover x86: Trenchboot Secure Launch DRTM for AMD SKINIT (GRUB)

This is a refresh of [rfc-v1] (no substantial changes) to accompany v4 of the main patch set, see more details there. Giving this a try and providing feedback is appreciated.

The changes can also be viewed on GitHub at [TrenchBoot/grub].

-----

[rfc-v1]: https://lists.gnu.org/archive/html/grub-devel/2024-12/msg00095.html
[TrenchBoot/grub]: https://github.com/TrenchBoot/grub/compare/tb-2.12-284-v4...tb-amd-2.12-284-rfc-v2

-----

Alec Brown (3):
i386: Add PSP discovery code
slaunch/psp: Add core PSP commands and get capability command slaunch/psp: Setup TMRs to protect RAM from DMA

Michał Żygowski (1):
multiboot2: Support SKINIT Secure Launch

Ross Philipson (3):
i386: Extra x86 definitions needed by AMD SKINIT Secure Launch slaunch/skinit: AMD SKINIT Secure Launch core implementation
efi: Add AMD SKINIT Secure Launch support

 grub-core/Makefile.core.def                 |   3 +
 grub-core/lib/i386/relocator32.S            |   6 +
 grub-core/loader/efi/chainloader.c          |   6 +
 grub-core/loader/efi/linux.c                |   9 +
 grub-core/loader/i386/linux.c               |  39 +-
 grub-core/loader/multiboot_mbi2.c           |  14 +-
 grub-core/loader/slaunch/i386_linux.c       |  42 ++
 grub-core/loader/slaunch/psp.c              | 420 ++++++++++++++++++++
 grub-core/loader/slaunch/skinit.c           | 198 +++++++++
 grub-core/loader/slaunch/skl.c              | 256 ++++++++++++
 grub-core/loader/slaunch/slaunch.c          |  24 +-
 grub-core/loader/slaunch/x86_dlstub.c       |  60 ++-
 grub-core/loader/slaunch/x86_dltrampoline.S |  35 +-
 grub-core/loader/slaunch/x86_efi.c          |  79 ++++
 include/grub/i386/cpuid.h                   |   7 +-
 include/grub/i386/msr.h                     |   6 +
 include/grub/i386/pci.h                     |   1 +
 include/grub/i386/psp.h                     |  91 +++++
 include/grub/i386/skinit.h                  |  69 ++++
 include/grub/slaunch.h                      |   8 +
 include/grub/slr_table.h                    |  16 +
 21 files changed, 1364 insertions(+), 25 deletions(-)
 create mode 100644 grub-core/loader/slaunch/psp.c
 create mode 100644 grub-core/loader/slaunch/skinit.c
 create mode 100644 grub-core/loader/slaunch/skl.c
 create mode 100644 include/grub/i386/psp.h
 create mode 100644 include/grub/i386/skinit.h

base-commit: 03088054f58ff1f6dddae3e82c60463b0a3cc83e
prerequisite-patch-id: d65d5d9a121c416652ce993ca70df02e4775a13a

Signature · 7 lines
2.49.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw

RFC PATCH v2 1/7 i386: Extra x86 definitions needed by AMD SKINIT Secure Launch

From: Ross Philipson <[email protected]>

A few MSR/CPUID/PCI x86 definitions added.

---
 include/grub/i386/cpuid.h | 7 ++++---
 include/grub/i386/msr.h   | 6 ++++++
 include/grub/i386/pci.h   | 1 +
 3 files changed, 11 insertions(+), 3 deletions(-)
include/grub/i386/cpuid.h +4 -3
index 980bf0875..023ab9431 100644
@@ -28,9 +28,10 @@
 #define GRUB_SMX_CPUID_FEATURE  (1<<6)
 
 /* AMD */
-#define GRUB_AMD_CPUID_FEATURES 0x80000001
-#define GRUB_SVM_CPUID_FEATURE  (1<<2)
-#define GRUB_AMD_CPUID_FUNC     0x8000000a
+#define GRUB_AMD_CPUID_FEATURES   0x80000001
+#define GRUB_SVM_CPUID_FEATURE    (1<<2)
+#define GRUB_AMD_CPUID_FUNC       0x8000000a
+#define GRUB_SKINIT_CPUID_FEATURE (1<<12)
 
 extern unsigned char grub_cpuid_has_longmode;
 extern unsigned char grub_cpuid_has_pae;
include/grub/i386/msr.h +6 -0
index 4487fe4c2..9adb44cd4 100644
@@ -30,6 +30,12 @@
 #define GRUB_MSR_X86_APICBASE_ENABLE    (1<<11)
 #define GRUB_MSR_X86_APICBASE_BASE      (0xfffff<<12) /* Mask for APIC base address */
 
+#define GRUB_MSR_X86_X2APICBASE_ENABLE     (1<<10)
+#define GRUB_MSR_X86_X2APICBASE_ICR        0x830
+#define GRUB_MSR_X86_LAPIC_ICR_LO          0x300
+#define GRUB_MSR_X86_ICR_MODE_INIT         (5<<8)
+#define GRUB_MSR_X86_ICR_DELIVER_EXCL_SELF (3<<18)
+
 #define GRUB_MSR_X86_FEATURE_CONTROL    0x0000003a
 #define GRUB_MSR_X86_FEATURE_CTRL_LOCK  (1<<0)     /* Lock this (feature control) MSR */
 #define GRUB_MSR_X86_ENABLE_VMX_IN_SMX  (1<<1)     /* Enable VMX inside SMX */
include/grub/i386/pci.h +1 -0
index dffeb5695..952e8bcca 100644
@@ -27,6 +27,7 @@
 #define GRUB_PCI_DATA_REG	0xcfc
 #define GRUB_PCI_NUM_BUS        256
 #define GRUB_PCI_NUM_DEVICES    32
+#define GRUB_PCI_NUM_FUNCTIONS  8
 
 static inline grub_uint32_t
 grub_pci_read (grub_pci_address_t addr)
Signature · 7 lines
2.49.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#2 lore raw

RFC PATCH v2 2/7 i386: Add PSP discovery code

In reply to Sergii Dmytruk

From: Alec Brown <[email protected]>

Add base code for PSP discovery in grub.

---
 grub-core/Makefile.core.def    |   1 +
 grub-core/loader/slaunch/psp.c | 133 +++++++++++++++++++++++++++++++++
 include/grub/i386/psp.h        |  42 +++++++++++
 3 files changed, 176 insertions(+)
 create mode 100644 grub-core/loader/slaunch/psp.c
 create mode 100644 include/grub/i386/psp.h
grub-core/Makefile.core.def +1 -0
index 69e8fc2e4..97fc762f8 100644
@@ -1883,6 +1883,7 @@ module = {
   x86 = loader/slaunch/acmod.c;
   x86 = loader/slaunch/verify.c;
   x86 = loader/slaunch/i386_linux.c;
+  x86 = loader/slaunch/psp.c;
   x86 = loader/slaunch/x86_dlstub.c;
   x86 = loader/slaunch/x86_dltrampoline.S;
   i386_efi = loader/slaunch/x86_efi.c;
grub-core/loader/slaunch/psp.c new file +133 -0

133 lines not on this page · raw message

include/grub/i386/psp.h new file +42 -0

42 lines not on this page · raw message

Signature · 7 lines
2.49.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#3 lore raw

RFC PATCH v2 3/7 slaunch/psp: Add core PSP commands and get capability command

In reply to Sergii Dmytruk

From: Alec Brown <[email protected]>

Add various core DRTM commands for the PSP.

---
 grub-core/loader/slaunch/psp.c | 235 ++++++++++++++++++++++++++++++++-
 include/grub/i386/psp.h        |  48 +++++++
 2 files changed, 281 insertions(+), 2 deletions(-)
grub-core/loader/slaunch/psp.c +233 -2
index 3459f7e34..2bbb4c685 100644
@@ -33,6 +33,8 @@
 #include <grub/types.h>
 #include <grub/misc.h>
 #include <grub/err.h>
+#include <grub/mm.h>
+#include <grub/time.h>
 #include <grub/pci.h>
 #include <grub/i386/pci.h>
 #include <grub/i386/psp.h>
@@ -53,10 +55,71 @@ typedef enum
   PSP_V3
 } psp_version_t;
 
+struct pci_psp_device
+{
+  grub_uint16_t vendor_id;
+  grub_uint16_t dev_id;
+  psp_version_t version;
+};
+
+static const struct pci_psp_device psp_devs_list[] = {
+  {0x1022, 0x1537, PSP_NONE},
+  {0x1022, 0x1456, PSP_V1},
+  {0x1022, 0x1468, PSP_NONE},
+  {0x1022, 0x1486, PSP_V2},
+  {0x1022, 0x15DF, PSP_V3},
+  {0x1022, 0x1649, PSP_V2},
+  {0x1022, 0x14CA, PSP_V3},
+  {0x1022, 0x15C7, PSP_NONE}
+};
+
 static psp_version_t psp_version;
 static struct psp_drtm_interface psp_drtm;
 
+struct drtm_capability
+{
+  int drtm_enabled;
+  int tsme_enabled;
+  int anti_rollback_status_bit;
+  grub_uint32_t version;
+  grub_uint32_t tmr_alignment;
+  grub_uint32_t tmr_count;
+};
+
+static struct drtm_capability drtm_capability;
+
+static const char *drtm_status_strings[] = {
+	"DRTM_NO_ERROR",
+	"DRTM_NOT_SUPPORTED",
+	"DRTM_LAUNCH_ERROR",
+	"DRTM_TMR_SETUP_FAILED_ERROR",
+	"DRTM_TMR_DESTROY_FAILED_ERROR",
+	"UNDEFINED",
+	"UNDEFINED",
+	"DRTM_GET_TCG_LOGS_FAILED_ERROR",
+	"DRTM_OUT_OF_RESOURCES_ERROR",
+	"DRTM_GENERIC_ERROR",
+	"DRTM_INVALID_SERVICE_ID_ERROR",
+	"DRTM_MEMORY_UNALIGNED_ERROR",
+	"DRTM_MINIMUM_SIZE_ERROR",
+	"DRTM_GET_TMR_DESCRIPTOR_FAILED",
+	"DRTM_EXTEND_OSSL_DIGEST_FAILED",
+	"DRTM_SETUP_NOT_ALLOWED",
+	"DRTM_GET_IVRS_TABLE_FAILED"
+};
+
 static grub_err_t init_drtm_interface (grub_addr_t base_addr, psp_version_t version);
+static void init_drtm_device (grub_pci_device_t dev);
+static int drtm_wait_for_psp_ready (grub_uint32_t *status);
+
+static const char *
+drtm_status_string (grub_uint32_t status)
+{
+  if (status > DRTM_GET_IVRS_TABLE_FAILED)
+    return "UNDEFINED";
+
+  return drtm_status_strings[status];
+}
 
 static void
 smn_register_read (grub_uint32_t address, grub_uint32_t *value)
@@ -89,24 +152,117 @@ get_psp_bar_addr (void)
   return (grub_uint64_t) pspbaselo;
 }
 
+static const struct pci_psp_device *
+lookup_drtm_device (grub_uint16_t vendor_id, grub_uint16_t dev_id)
+{
+  grub_uint32_t max_psp_devs = sizeof (psp_devs_list) / sizeof (psp_devs_list[0]);
+  const struct pci_psp_device *psp = NULL;
+  grub_uint32_t i;
+
+  for (i = 0; i < max_psp_devs; i++)
+    {
+      if ((psp_devs_list[i].vendor_id == vendor_id) &&
+          (psp_devs_list[i].dev_id == dev_id))
+        {
+          psp = &psp_devs_list[i];
+          break;
+        }
+    }
+
+  if (psp && psp->version == PSP_NONE)
+    {
+      grub_dprintf ("slaunch", "DRTM: AMD SP device (PCI info: 0x%04x, 0x%04x) does not have PSP\n",
+                    psp->vendor_id, psp->dev_id);
+      psp = NULL;
+    }
+
+  return psp;
+}
+
 grub_err_t
 grub_psp_discover (void)
 {
+  grub_pci_device_t dev;
+  grub_pci_address_t addr;
+  grub_uint16_t vendor_id, dev_id;
+  const struct pci_psp_device *psp = NULL;
   grub_uint64_t bar2_addr = 0;
   grub_err_t err;
 
+  for (dev.bus = 0; dev.bus < GRUB_PCI_NUM_BUS; dev.bus++)
+    {
+      for (dev.device = 0; dev.device < GRUB_PCI_NUM_DEVICES; dev.device++)
+        {
+          for (dev.function = 0; dev.function < GRUB_PCI_NUM_FUNCTIONS; dev.function++)
+            {
+              addr = grub_pci_make_address (dev, 0);
+              vendor_id = grub_pci_read_word (addr);
+              addr = grub_pci_make_address (dev, 2);
+              dev_id = grub_pci_read_word (addr);
+              psp = lookup_drtm_device (vendor_id, dev_id);
+              if (psp)
+                goto psp_found;
+            }
+        }
+    }
+
+  if (!psp)
+    return grub_error (GRUB_ERR_BAD_DEVICE, N_("DRTM: failed to find PSP\n"));
+
+psp_found:
+  init_drtm_device (dev);
+
   bar2_addr = get_psp_bar_addr();
   if (!bar2_addr)
     return grub_error (GRUB_ERR_BAD_DEVICE, N_("DRTM: failed to find PSP\n"));
 
-  err = init_drtm_interface (bar2_addr, PSP_V2);
+  err = init_drtm_interface (bar2_addr, psp->version);
   if (err)
     return err;
 
-  psp_version = PSP_V2;
+  psp_version = psp->version;
   return GRUB_ERR_NONE;
 }
 
+static void
+init_drtm_device (grub_pci_device_t dev)
+{
+  grub_uint16_t pci_cmd;
+  grub_uint8_t pin, lat;
+  grub_pci_address_t pci_cmd_addr, pin_addr, lat_addr;
+
+  /* Enable memory space access for PSP */
+  pci_cmd_addr = grub_pci_make_address (dev, GRUB_PCI_REG_COMMAND);
+  pci_cmd = grub_pci_read_word (pci_cmd_addr) | 0x2;
+  grub_pci_write_word (pci_cmd_addr, pci_cmd);
+
+  /* Enable PCI interrupts */
+  pin_addr = grub_pci_make_address (dev, GRUB_PCI_REG_IRQ_PIN);
+  pin = grub_pci_read_byte (pin_addr);
+  if (pin)
+    {
+      pci_cmd = grub_pci_read_word (pci_cmd_addr);
+      if (pci_cmd & 0x400)
+        {
+          pci_cmd &= ~0x400;
+          grub_pci_write_word (pci_cmd_addr, pci_cmd);
+        }
+    }
+
+  /* Set PSP at bus master */
+  pci_cmd = grub_pci_read_word (pci_cmd_addr) | 0x4;
+  grub_pci_write_word (pci_cmd_addr, pci_cmd);
+
+  /* Set PCI latency timer */
+  lat_addr = grub_pci_make_address (dev, GRUB_PCI_REG_LAT_TIMER);
+  lat = grub_pci_read_byte (lat_addr);
+  if (lat < 16)
+    {
+      lat = 64;
+      grub_pci_write_byte (lat_addr, lat);
+    }
+}
+
 static grub_err_t
 init_drtm_interface (grub_addr_t base_addr, psp_version_t version)
 {
@@ -131,3 +287,78 @@ grub_psp_version (void)
 {
   return psp_version;
 }
+
+static int
+drtm_wait_for_psp_ready (grub_uint32_t *status)
+{
+  int retry = 5;
+  grub_uint32_t reg_val = 0;
+
+  while (--retry)
+    {
+      reg_val = *psp_drtm.c2pmsg_72;
+
+      if (reg_val & DRTM_MBOX_READY_MASK)
+	break;
+
+      /* TODO: select wait time appropriately */
+      grub_millisleep (100);
+    }
+
+  if (!retry)
+    return 0;
+
+  if (status)
+    *status = reg_val & 0xffff;
+
+  return 1;
+}
+
+/*
+ * For some reason this workaround is necessary to
+ * kickstart the DRTM. Without this step, DRTM does
+ * not return the capability.
+ *
+ * TODO: Check with AMD about why this is necessary.
+ */
+void
+grub_drtm_kick_psp (void)
+{
+  *psp_drtm.c2pmsg_72 = 0;
+
+  (void)drtm_wait_for_psp_ready (NULL);
+}
+
+grub_err_t
+grub_drtm_get_capability (void)
+{
+  grub_uint32_t reg_val = 0;
+  grub_uint32_t status = 0;
+
+  if (!drtm_wait_for_psp_ready (NULL))
+    return grub_error (GRUB_ERR_TIMEOUT, N_("DRTM: %s: PSP not ready to accept commands\n"), __func__);
+
+  reg_val = (DRTM_CMD_GET_CAPABILITY << DRTM_MBOX_CMD_SHIFT);
+
+  *psp_drtm.c2pmsg_72 = reg_val;
+
+  if (!drtm_wait_for_psp_ready (&status))
+    return grub_error (GRUB_ERR_TIMEOUT, N_("DRTM: %s: failed to get a response from PSP\n"), __func__);
+
+  if (status != DRTM_NO_ERROR)
+    return grub_error (GRUB_ERR_BAD_DEVICE, N_("DRTM: %s: failed to get PSP capability - %s\n"),
+		       __func__, drtm_status_string (status));
+
+  reg_val = *psp_drtm.c2pmsg_93;
+  drtm_capability.drtm_enabled = (reg_val & 0x1) != 0;
+  drtm_capability.tsme_enabled = (reg_val & 0x2) != 0;
+  drtm_capability.anti_rollback_status_bit = (reg_val & 0x4) != 0;
+
+  drtm_capability.version = *psp_drtm.c2pmsg_94;
+
+  reg_val = *psp_drtm.c2pmsg_95;
+  drtm_capability.tmr_alignment = reg_val & 0x00FFFFFF;
+  drtm_capability.tmr_count = (reg_val & 0xFF000000) >> 24;
+
+  return GRUB_ERR_NONE;
+}
include/grub/i386/psp.h +48 -0

55 lines not on this page · raw message

Signature · 7 lines
2.49.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#4 lore raw

RFC PATCH v2 4/7 slaunch/psp: Setup TMRs to protect RAM from DMA

In reply to Sergii Dmytruk

From: Alec Brown <[email protected]>

TMRs are setup and used to protect ranges of memory from outside access like DMA. Setting them up to cover all memory protects from DMA during the establishment of the DRTM environment.

---
 grub-core/loader/slaunch/psp.c | 56 ++++++++++++++++++++++++++++++++++
 include/grub/i386/psp.h        |  1 +
 2 files changed, 57 insertions(+)
grub-core/loader/slaunch/psp.c +56 -0
index 2bbb4c685..553e53ed4 100644
@@ -36,6 +36,9 @@
 #include <grub/mm.h>
 #include <grub/time.h>
 #include <grub/pci.h>
+#include <grub/efi/efi.h>
+#include <grub/efi/api.h>
+#include <grub/i386/linux.h>
 #include <grub/i386/pci.h>
 #include <grub/i386/psp.h>
 
@@ -362,3 +365,56 @@ grub_drtm_get_capability (void)
 
   return GRUB_ERR_NONE;
 }
+
+/**
+ * Setup Trusted Memory Region (TMR). The PSP supports only
+ * 1 TMR - as such all of the sysmem region is covered in
+ * a single TMR.
+ *
+ * Walk the E820 MB2 memory map table to figure out the end
+ * of the memory addresses. Setup the TMR to cover address
+ * ranges from 0x0 to the end calculated during the walk.
+ */
+int
+grub_drtm_setup_tmrs (grub_uint64_t tmr_end)
+{
+  grub_uint64_t tmr_count = 0;
+  grub_uint64_t rem = 0;
+  grub_uint32_t status = 0;
+
+  tmr_count = grub_divmod64 (tmr_end, drtm_capability.tmr_alignment, &rem);
+  if (rem != 0)
+    tmr_count++;
+
+  if (tmr_count > GRUB_UINT_MAX)
+    {
+      grub_error (GRUB_ERR_BAD_DEVICE, N_("DRTM: %s: memory region bigger than TMR\n"), __func__);
+      return -1;
+    }
+
+  /*
+   * Setup TMR for address range 0x0 to tmr_end. Size is in
+   * multiples of tmr_alignment.
+   */
+  *psp_drtm.c2pmsg_93 = (grub_uint32_t)tmr_count;
+  *psp_drtm.c2pmsg_94 = 0;
+  *psp_drtm.c2pmsg_95 = 0;
+
+  *psp_drtm.c2pmsg_72 = (DRTM_TMR_INDEX_0 << 24) |
+			(DRTM_CMD_TMR_SETUP << DRTM_MBOX_CMD_SHIFT);
+
+  if (!drtm_wait_for_psp_ready (&status))
+    {
+      grub_error (GRUB_ERR_TIMEOUT, N_("DRTM: %s: failed to get a response from PSP\n"), __func__);
+      return -1;
+    }
+
+  if (status != DRTM_NO_ERROR)
+    {
+      grub_error (GRUB_ERR_BAD_DEVICE, N_("DRTM: %s: failed to setup TMRs - %s\n"),
+		  __func__, drtm_status_string (status));
+      return -1;
+    }
+
+  return 0;
+}
include/grub/i386/psp.h +1 -0
index 915435311..4df31735d 100644
@@ -86,5 +86,6 @@ extern grub_err_t grub_psp_discover (void);
 extern grub_uint16_t grub_psp_version (void);
 extern void grub_drtm_kick_psp (void);
 extern grub_err_t grub_drtm_get_capability (void);
+extern int grub_drtm_setup_tmrs (grub_uint64_t tmr_end);
 
 #endif /* __PSP_H__ */
Signature · 7 lines
2.49.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#5 lore raw

RFC PATCH v2 5/7 slaunch/skinit: AMD SKINIT Secure Launch core implementation

In reply to Sergii Dmytruk

From: Ross Philipson <[email protected]>

Add core implementation for AMD SKINIT Secure Launch in GRUB based on Linux legacy boot protocol.

---
 grub-core/Makefile.core.def           |   2 +
 grub-core/lib/i386/relocator32.S      |   6 +
 grub-core/loader/i386/linux.c         |  39 ++++-
 grub-core/loader/slaunch/i386_linux.c |  42 +++++
 grub-core/loader/slaunch/skinit.c     | 184 +++++++++++++++++++++
 grub-core/loader/slaunch/skl.c        | 223 ++++++++++++++++++++++++++
 grub-core/loader/slaunch/slaunch.c    |  24 ++-
 grub-core/loader/slaunch/x86_dlstub.c |  56 ++++++-
 include/grub/i386/skinit.h            |  69 ++++++++
 include/grub/slaunch.h                |   6 +
 include/grub/slr_table.h              |  16 ++
 11 files changed, 654 insertions(+), 13 deletions(-)
 create mode 100644 grub-core/loader/slaunch/skinit.c
 create mode 100644 grub-core/loader/slaunch/skl.c
 create mode 100644 include/grub/i386/skinit.h
grub-core/Makefile.core.def +2 -0
index 97fc762f8..7b63095ce 100644
@@ -1884,6 +1884,8 @@ module = {
   x86 = loader/slaunch/verify.c;
   x86 = loader/slaunch/i386_linux.c;
   x86 = loader/slaunch/psp.c;
+  x86 = loader/slaunch/skinit.c;
+  x86 = loader/slaunch/skl.c;
   x86 = loader/slaunch/x86_dlstub.c;
   x86 = loader/slaunch/x86_dltrampoline.S;
   i386_efi = loader/slaunch/x86_efi.c;
grub-core/lib/i386/relocator32.S +6 -0
index 25f162b0e..837d2e70f 100644
@@ -115,6 +115,9 @@ VARIABLE(grub_relocator32_edx)
 	cmpl	$SLP_INTEL_TXT, %edi
 	je	LOCAL(intel_txt)
 
+	cmpl	$SLP_AMD_SKINIT, %edi
+	je	LOCAL(amd_skinit)
+
 	.byte	0xea
 VARIABLE(grub_relocator32_eip)
 	.long	0
@@ -123,6 +126,9 @@ VARIABLE(grub_relocator32_eip)
 LOCAL(intel_txt):
 	getsec
 
+LOCAL(amd_skinit):
+	skinit
+
 	/* GDT. Copied from loader/i386/linux.c. */
 	.p2align	4
 LOCAL(gdt):
grub-core/loader/i386/linux.c +35 -4
index e7c61dfbc..eb854374d 100644
@@ -38,6 +38,7 @@
 #include <grub/lib/cmdline.h>
 #include <grub/i386/mmio.h>
 #include <grub/i386/txt.h>
+#include <grub/i386/skinit.h>
 #include <grub/linux.h>
 #include <grub/machine/kernel.h>
 #include <grub/safemath.h>
@@ -227,6 +228,13 @@ allocate_pages (grub_size_t prot_size, grub_size_t *align,
       if (err)
         goto fail;
     }
+  else if (grub_slaunch_platform_type () == SLP_AMD_SKINIT)
+    {
+      err = grub_sl_skinit_setup_linux (&slparams, relocator, total_size, prot_file_size,
+                                        prot_mode_mem, prot_mode_target);
+      if (err)
+        goto fail;
+    }
 
   grub_dprintf ("linux", "prot_mode_mem = %p, prot_mode_target = %lx, prot_size = %x\n",
                 prot_mode_mem, (unsigned long) prot_mode_target,
@@ -659,16 +667,39 @@ grub_linux_boot (void)
   }
 #endif
 
-  if (grub_slaunch_platform_type () == SLP_INTEL_TXT)
+  if (grub_slaunch_platform_type () == SLP_AMD_SKINIT)
+    {
+      /*
+       * AMD SKL final setup may relocate the SKL module. It is also what sets the SLRT and DCE
+       * values in slparams so this must be done before final setup and launch below.
+       */
+      err = grub_skl_setup_module (&slparams);
+      if (err != GRUB_ERR_NONE)
+        return err;
+    }
+
+  if (grub_slaunch_platform_type () != SLP_NONE)
     {
       struct grub_slr_table *slrt = (struct grub_slr_table *)slparams.slr_table_mem;
       struct grub_slr_entry_dl_info *dlinfo;
 
+      slparams.efi_memmap_mem = efi_mmap_buf;
       slparams.platform_type = grub_slaunch_platform_type();
 
-      err = grub_txt_boot_prepare (&slparams);
-      if (err != GRUB_ERR_NONE)
-        return err;
+      if (grub_slaunch_platform_type () == SLP_INTEL_TXT)
+        {
+          err = grub_txt_boot_prepare (&slparams);
+          if (err != GRUB_ERR_NONE)
+	    return err;
+        }
+      else if (grub_slaunch_platform_type () == SLP_AMD_SKINIT)
+        {
+          err = grub_skl_prepare_bootloader_data (&slparams);
+          if (err != GRUB_ERR_NONE)
+            return err;
+        }
+      else
+        return GRUB_ERR_BAD_DEVICE;
 
       dlinfo = grub_slr_next_entry_by_tag (slrt, NULL, GRUB_SLR_ENTRY_DL_INFO);
       dl_entry ((grub_uint64_t)(grub_addr_t)&dlinfo->bl_context);
grub-core/loader/slaunch/i386_linux.c +42 -0

45 lines not on this page · raw message

grub-core/loader/slaunch/skinit.c new file +184 -0

184 lines not on this page · raw message

grub-core/loader/slaunch/skl.c new file +223 -0

223 lines not on this page · raw message

grub-core/loader/slaunch/slaunch.c +23 -1
index 8079bae7a..15a2b5a09 100644
@@ -28,6 +28,7 @@
 #include <grub/i386/msr.h>
 #include <grub/i386/mmio.h>
 #include <grub/i386/txt.h>
+#include <grub/i386/skinit.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -75,6 +76,15 @@ grub_cmd_slaunch (grub_command_t cmd __attribute__ ((unused)),
 
       slp = SLP_INTEL_TXT;
     }
+  else if (!grub_memcmp (manufacturer, "AuthenticAMD", 12))
+    {
+      err = grub_skinit_is_supported ();
+
+      if (err != GRUB_ERR_NONE)
+	return err;
+
+      slp = SLP_AMD_SKINIT;
+    }
   else
     return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("CPU is unsupported"));
 
@@ -95,7 +105,7 @@ grub_cmd_slaunch_module (grub_command_t cmd __attribute__ ((unused)),
   if (slp == SLP_NONE)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("secure launch not enabled"));
 
-  if (slp > SLP_INTEL_TXT)
+  if (slp > SLP_AMD_SKINIT)
     return grub_error (GRUB_ERR_BAD_ARGUMENT,
 		       N_("unknown secure launch platform type: %d"), slp);
 
@@ -141,6 +151,14 @@ grub_cmd_slaunch_module (grub_command_t cmd __attribute__ ((unused)),
 	  goto fail;
 	}
     }
+  else if (slp == SLP_AMD_SKINIT)
+    {
+      if (!grub_skl_set_module (new_module, size))
+	{
+	  grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("SKL module isn't correct"));
+	  goto fail;
+	}
+    }
 
   grub_file_close (file);
 
@@ -172,6 +190,10 @@ grub_cmd_slaunch_state (grub_command_t cmd __attribute__ ((unused)),
       grub_printf ("Secure launcher: Intel TXT\n");
       grub_txt_state_show ();
     }
+  else if (slp == SLP_AMD_SKINIT)
+    {
+      grub_printf ("Secure launcher: AMD SKINIT\n");
+    }
 
   return GRUB_ERR_NONE;
 }
grub-core/loader/slaunch/x86_dlstub.c +48 -8

88 lines not on this page · raw message

include/grub/i386/skinit.h new file +69 -0

69 lines not on this page · raw message

include/grub/slaunch.h +6 -0
index d48c09434..70c94b3e7 100644
@@ -24,6 +24,7 @@
 /* Secure launch platform types. */
 #define SLP_NONE	0
 #define SLP_INTEL_TXT	1
+#define SLP_AMD_SKINIT	2
 
 #define GRUB_SLAUNCH_TPM_EVT_LOG_SIZE	(8 * GRUB_PAGE_SIZE)
 
@@ -72,6 +73,7 @@ struct grub_slaunch_params
   grub_uint32_t platform_type;
   struct linux_kernel_params *boot_params;
   grub_uint64_t boot_params_base;
+  void *efi_memmap_mem;
   struct grub_relocator *relocator;
   grub_uint64_t slr_table_base;
   grub_uint32_t slr_table_size;
@@ -141,6 +143,10 @@ grub_err_t grub_sl_txt_setup_linux (struct grub_slaunch_params *slparams,
                                     struct grub_relocator *relocator,
                                     grub_size_t total_size, grub_size_t prot_size,
                                     void **prot_mode_mem, grub_addr_t *prot_mode_target);
+grub_err_t grub_sl_skinit_setup_linux (struct grub_slaunch_params *slparams,
+                                       struct grub_relocator *relocator,
+                                       grub_size_t total_size, grub_size_t prot_file_size,
+                                       void *prot_mode_mem, grub_addr_t prot_mode_target);
 
 /* EFI functions */
 grub_err_t grub_sl_efi_txt_setup (struct grub_slaunch_params *slparams, void *kernel_addr,
include/grub/slr_table.h +16 -0
index 7d2751c33..117018d67 100644
@@ -195,6 +195,22 @@ struct grub_slr_entry_intel_info
   struct grub_slr_txt_mtrr_state saved_bsp_mtrrs;
 } GRUB_PACKED;
 
+/*
+ * AMD SKINIT Info table
+ */
+struct grub_slr_entry_amd_info
+{
+  struct grub_slr_entry_hdr hdr;
+  grub_uint64_t next;
+  grub_uint32_t type;
+  grub_uint32_t len;
+  grub_uint64_t slrt_size;
+  grub_uint64_t slrt_base;
+  grub_uint64_t boot_params_base;
+  grub_uint16_t psp_version;
+  grub_uint16_t reserved[3];
+} GRUB_PACKED;
+
 /*
  * UEFI config measurement entry
  */
Signature · 7 lines
2.49.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#6 lore raw

RFC PATCH v2 6/7 efi: Add AMD SKINIT Secure Launch support

In reply to Sergii Dmytruk

From: Ross Philipson <[email protected]>

Extend code introduced for legacy boot with AMD Secure Launch to support EFI.

This is for Linux with its EFI stub and also for anything else that can be loaded by chainloader.

---
 grub-core/loader/efi/chainloader.c          |  6 ++
 grub-core/loader/efi/linux.c                |  9 +++
 grub-core/loader/slaunch/skinit.c           | 26 +++++--
 grub-core/loader/slaunch/skl.c              | 51 ++++++++++---
 grub-core/loader/slaunch/x86_dlstub.c       |  4 +-
 grub-core/loader/slaunch/x86_dltrampoline.S | 35 +++++++--
 grub-core/loader/slaunch/x86_efi.c          | 79 +++++++++++++++++++++
 include/grub/slaunch.h                      |  2 +
 8 files changed, 189 insertions(+), 23 deletions(-)
grub-core/loader/efi/chainloader.c +6 -0
index 0dfb44412..7321a6a75 100644
@@ -108,6 +108,12 @@ grub_chainloader_boot (void *context)
       if (err != GRUB_ERR_NONE)
         return grub_error (err, "Secure Launch setup TXT failed");
     }
+  else if (grub_slaunch_platform_type () == SLP_AMD_SKINIT)
+    {
+      err = grub_sl_efi_skinit_setup (&slparams, image_mem, loaded_image, /*is_linux=*/false);
+      if (err != GRUB_ERR_NONE)
+        return grub_error (err, "Secure Launch setup SKINIT failed");
+    }
 
   b = grub_efi_system_table->boot_services;
   status = b->start_image (image_handle, &exit_data_size, &exit_data);
grub-core/loader/efi/linux.c +9 -0
index e7c0797a7..ac8ee9f61 100644
@@ -249,6 +249,15 @@ grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args)
           goto unload;
         }
     }
+  else if (grub_slaunch_platform_type () == SLP_AMD_SKINIT)
+    {
+      err = grub_sl_efi_skinit_setup (&slparams, kernel_addr, loaded_image, /*is_linux=*/true);
+      if (err != GRUB_ERR_NONE)
+        {
+          grub_error (err, "Secure Launch setup SKINIT failed");
+          goto unload;
+        }
+    }
 
   grub_dprintf ("linux", "starting image %p\n", image_handle);
   status = b->start_image (image_handle, 0, NULL);
grub-core/loader/slaunch/skinit.c +20 -6
index 05539518d..6691fe2b7 100644
@@ -122,18 +122,32 @@ grub_skinit_psp_memory_protect (struct grub_slaunch_params *slparams)
   grub_efi_memory_descriptor_t *memory_map_end;
   grub_efi_memory_descriptor_t *desc;
   struct grub_efi_info *efi_info;
-  grub_uint64_t efi_memmap, tmr_end = 0;
+  grub_uint64_t efi_memmap, hi_val, tmr_end = 0;
   grub_err_t err;
 
   /* A bit of work to extract the v2.08 EFI info from the linux params */
   efi_info = (struct grub_efi_info *)((grub_uint8_t *)&(boot_params->efi_info.v0208)
                                       + 2*sizeof(grub_uint32_t));
 
-  /*
-   * On legacy Linux boots, the relocator is used to map the EFI memory map buffer
-   * and return a virtual address to use. This virtual address is stashed in slparams.
-   */
-  efi_memmap = (grub_uint64_t)(grub_addr_t)slparams->efi_memmap_mem;
+  if (slparams->boot_type == GRUB_SL_BOOT_TYPE_EFI)
+    {
+      /*
+       * On EFI stub boots, the EFI memory map is fetched from the stub code so it
+       * needs to be gotten from the boot params on re-entry to the DL stub. The EFI
+       * info in boot params has physical addresses but everything is identity mapped.
+       */
+      efi_memmap = efi_info->efi_mmap;
+      hi_val = efi_info->efi_mmap_hi;
+      efi_memmap |= (hi_val << 32);
+    }
+  else
+    {
+      /*
+       * On legacy Linux boots, the relocator is used to map the EFI memory map buffer
+       * and return a virtual address to use. This virtual address is stashed in slparams.
+       */
+      efi_memmap = (grub_uint64_t)(grub_addr_t)slparams->efi_memmap_mem;
+    }
 
   desc = (grub_efi_memory_descriptor_t *)(grub_addr_t) efi_memmap;
   memory_map_end = (grub_efi_memory_descriptor_t *)(grub_addr_t) (efi_memmap + efi_info->efi_mmap_size);
grub-core/loader/slaunch/skl.c +42 -9

66 lines not on this page · raw message

grub-core/loader/slaunch/x86_dlstub.c +2 -2
index fc1afed33..dbbd46f7d 100644
@@ -35,7 +35,7 @@
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
-extern void dl_trampoline(grub_uint32_t dce_base, grub_uint32_t dce_size);
+extern void dl_trampoline(grub_uint32_t dce_base, grub_uint32_t dce_size, grub_uint32_t platform);
 
 void dl_entry (grub_uint64_t dl_ctx)
 {
@@ -124,7 +124,7 @@ void dl_entry (grub_uint64_t dl_ctx)
     }
   else if (slparams->boot_type == GRUB_SL_BOOT_TYPE_EFI)
     {
-      dl_trampoline (slparams->dce_base, slparams->dce_size);
+      dl_trampoline (slparams->dce_base, slparams->dce_size, slparams->platform_type);
     }
   else
     {
grub-core/loader/slaunch/x86_dltrampoline.S +29 -6
index 461e14271..e2d7a674f 100644
@@ -18,16 +18,14 @@
 
 #include <config.h>
 #include <grub/symbol.h>
+#include <grub/slaunch.h>
+#include <grub/i386/crfr.h>
+#include <grub/i386/msr.h>
 
 #define GRUB_SMX_LEAF_SENTER	4
 #define CS_SEL32		0x0008
 #define DS_SEL			0x0010
 
-#define CR0_PE  0x00000001
-#define CR0_MP  0x00000002
-#define CR0_TS  0x00000008
-#define CR0_NE  0x00000020
-
 	.file	"dltrampoline.S"
 	.text
 
@@ -53,7 +51,7 @@ dl_trampoline:
 	lretq
 
 	.code32
-1:	/* Now in IA-32e compatibility mode load data segments and do senter */
+1:	/* Now in long compatibility (IA-32e), mode load data segments */
 	movw	$DS_SEL, %ax
 	movw	%ax, %ds
 	movw	%ax, %es
@@ -61,12 +59,37 @@ dl_trampoline:
 	movw	%ax, %fs
 	movw	%ax, %gs
 
+	cmpl	$SLP_AMD_SKINIT, %edx
+	je	1f
+
+	/* Intel SENTER */
 	movl	$GRUB_SMX_LEAF_SENTER, %eax
 	movl	%edi, %ebx
 	movl	%esi, %ecx
 	xorl	%edx, %edx
 	getsec
 
+1:
+	/* Turn paging off - we are identity mapped so we will survive */
+	movl	%cr0, %eax
+	andl	$~(GRUB_CR0_X86_PG | GRUB_CR0_X86_NE | GRUB_CR0_X86_TS | GRUB_CR0_X86_MP), %eax
+	movl	%eax, %cr0
+
+	/* Disable long mode */
+	movl	$(GRUB_MSR_X86_EFER), %ecx
+	rdmsr
+	andl	$~(GRUB_MSR_EFER_LME), %eax
+	wrmsr
+
+	/* Now in protected mode, disable PAE */
+	movl	%cr4, %eax
+	andl	$~(GRUB_CR4_X86_PAE), %eax
+	movl	%eax, %cr4
+
+	/* AMD SKINIT */
+	movl	%edi, %eax
+	skinit
+
 	.align 8
 dl_gdt:
 	/* Null Segment */
grub-core/loader/slaunch/x86_efi.c +79 -0

88 lines not on this page · raw message

include/grub/slaunch.h +2 -0
index 70c94b3e7..1b29d4c35 100644
@@ -151,6 +151,8 @@ grub_err_t grub_sl_skinit_setup_linux (struct grub_slaunch_params *slparams,
 /* EFI functions */
 grub_err_t grub_sl_efi_txt_setup (struct grub_slaunch_params *slparams, void *kernel_addr,
                                   grub_efi_loaded_image_t *loaded_image, bool is_linux);
+grub_err_t grub_sl_efi_skinit_setup (struct grub_slaunch_params *slparams, void *kernel_addr,
+                                     grub_efi_loaded_image_t *loaded_image, bool is_linux);
 
 #endif /* ASM_FILE */
 
Signature · 7 lines
2.49.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#7 lore raw

RFC PATCH v2 7/7 multiboot2: Support SKINIT Secure Launch

In reply to Sergii Dmytruk

From: Michał Żygowski <[email protected]>

---
 grub-core/loader/multiboot_mbi2.c | 14 +++++++++++++-
 grub-core/loader/slaunch/skl.c    |  2 +-
 2 files changed, 14 insertions(+), 2 deletions(-)
grub-core/loader/multiboot_mbi2.c +13 -1
index e71b4d59f..9bd48bf30 100644
@@ -37,6 +37,7 @@
 #include <grub/net.h>
 #include <grub/lib/cmdline.h>
 #include <grub/i386/memory.h>
+#include <grub/i386/skinit.h>
 #include <grub/i386/txt.h>
 #include <grub/slaunch.h>
 #include <grub/slr_table.h>
@@ -430,7 +431,8 @@ grub_multiboot2_load (grub_file_t file, const char *filename)
       slparams->tpm_evt_log_base = get_physical_target_address (ch);
       slparams->tpm_evt_log_size = GRUB_SLAUNCH_TPM_EVT_LOG_SIZE;
 
-      if (slparams->platform_type == SLP_INTEL_TXT)
+      /* It's OK to call this for AMD SKINIT because SKL erases the log before use. */
+      if (slparams->platform_type == SLP_INTEL_TXT || slparams->platform_type == SLP_AMD_SKINIT)
         grub_txt_init_tpm_event_log (get_virtual_current_address (ch),
                                      slparams->tpm_evt_log_size);
 
@@ -1257,6 +1259,16 @@ grub_multiboot2_perform_slaunch (grub_uint32_t mbi_target,
                    slparams->slr_table_mem,
                    slparams->slr_table_size);
     }
+  else if (slparams->platform_type == SLP_AMD_SKINIT)
+    {
+      err = grub_skl_setup_module (slparams);
+      if (err != GRUB_ERR_NONE)
+        return grub_error (err, "Failed to setup SKL for Multiboot2");
+
+      err = grub_skl_prepare_bootloader_data (slparams);
+      if (err != GRUB_ERR_NONE)
+        return grub_error (err, "SKL preparations have failed");
+    }
   else
     return grub_error (GRUB_ERR_BAD_DEVICE,
                        N_("Unknown secure launcher platform type: %d\n"), slparams->platform_type);
grub-core/loader/slaunch/skl.c +1 -1
index cbabc6854..34a800596 100644
@@ -149,7 +149,7 @@ grub_skl_setup_module (struct grub_slaunch_params *slparams)
   grub_addr_t max_addr;
 #endif
 
-  if (slparams->boot_type == GRUB_SL_BOOT_TYPE_LINUX)
+  if (slparams->boot_type == GRUB_SL_BOOT_TYPE_LINUX || slparams->boot_type == GRUB_SL_BOOT_TYPE_MB2)
     {
       err = grub_relocator_alloc_chunk_align (slparams->relocator, &ch,
                                               0, UP_TO_TOP32(SLB_SIZE), SLB_SIZE,
Signature · 7 lines
2.49.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel