Skip to contentSkip to conversation

x86/kexec: Secure Boot support

PATCH In review Kevin Lampis 2 participants 9 messages Oct 26 – Dec 2

lore
Share & tools
lore
#0 lore raw

v3 cover x86/kexec: Secure Boot support

These changes allow loading and booting an EFI kexec crash kernel.

For this new EFI kexec type, userspace won't provide purgatory code. Instead Xen jumps directly to the new kernel.

Signature validation is done by the dom0 kernel. Xen then calculates a digest for all the segments stored in memory. This digest is verified again during execution. This digest check obviates the need for Xen to verify Secure Boot signatures directly.

Changes in v3:
- Review comments

Changes in v2:
- Addressed review comments for specific patches

Kevin Lampis (2):
x86/kexec: Pass boot params directly to new kernel x86/kexec: Zero GPRs before entering

Ross Lagerwall (5):
x86/kexec: add digest checks
x86/kexec: rename setup_header to bzimage_header x86/kexec: add new struct kimage_segment x86/kexec: Support non-page-aligned kexec segments x86/kexec: Implement new EFI load type

 tools/libs/ctrl/xc_kexec.c               |   2 +-
 xen/arch/x86/Makefile                    |   1 +
 xen/arch/x86/bzimage.c                   |  47 +----
 xen/arch/x86/include/asm/bzimage.h       |  44 +++++
 xen/arch/x86/include/asm/kexec.h         |   9 +
 xen/arch/x86/include/asm/machine_kexec.h |   2 +-
 xen/arch/x86/kexec.c                     |  58 +++++++
 xen/arch/x86/machine_kexec.c             |   3 +-
 xen/arch/x86/x86_64/kexec_reloc.S        |  23 +++
 xen/common/kexec.c                       |  38 ++++-
 xen/common/kimage.c                      | 208 ++++++++++++++++++-----
 xen/include/public/kexec.h               |  12 +-
 xen/include/xen/kimage.h                 |  20 ++-
 13 files changed, 367 insertions(+), 100 deletions(-)
 create mode 100644 xen/arch/x86/include/asm/kexec.h
 create mode 100644 xen/arch/x86/kexec.c
Signature · 1 line
2.52.0
#1 lore raw

PATCH v3 1/7 x86/kexec: add digest checks

From: Ross Lagerwall <[email protected]>

As part of the work to support UEFI Secure Boot we must ensure that the kexec image data has changed between loading and executing the image. However, this is also a good check to perform generally.

During kexec load, calculate a digest over all the kexec segments. This digest is stored and verified again later prior to entering the image.

---
Changes in v3:
- Reword the commit message to make it clear that digest checks are only part of the kexec strategy.

Changes in v2:
- Reword the commit message
- Change the "kexec digest failed" error message
- Remove the != 0 from the call to kimage_verify_digest()
- Remove include sha2.h from kimage.h
- Remove error checking from kimage_calc_one_digest() because map_domain_page() doesn't fail
- Change kimage_calc_digest() to a void return too
- Remove references to KIMAGE_SHA256_REGIONS
It was a purgatory thing which is not used anymore
- Fix style on for ( s = 0; s < image->nr_segments; s++ ) {
- Add newlines and indentation to digest check fail message
- Make kimage_verify_digest() a bool return value

---
 xen/common/kexec.c       |  8 +++++
 xen/common/kimage.c      | 73 ++++++++++++++++++++++++++++++++++++++++
 xen/include/xen/kimage.h |  6 ++++
 3 files changed, 87 insertions(+)
xen/common/kexec.c +8 -0
index 8ccaef1e0258..68f4c2cb2671 100644
@@ -389,6 +389,12 @@ void kexec_crash(enum crash_reason reason)
     if ( !test_bit(KEXEC_IMAGE_CRASH_BASE + pos, &kexec_flags) )
         return;
 
+    if ( !kimage_verify_digest(kexec_image[KEXEC_IMAGE_CRASH_BASE + pos]) )
+    {
+        printk(XENLOG_ERR "kexec digest failed, aborting kexec crash transfer\n");
+        return;
+    }
+
     kexecing = true;
 
     if ( kexec_common_shutdown() != 0 )
@@ -940,6 +946,8 @@ static int kexec_load(XEN_GUEST_HANDLE_PARAM(void) uarg)
     if ( ret < 0 )
         goto error;
 
+    kimage_calc_digest(kimage, kimage->digest);
+
     ret = kexec_load_slot(kimage);
     if ( ret < 0 )
         goto error;
xen/common/kimage.c +73 -0

79 lines not on this page · raw message

xen/include/xen/kimage.h +6 -0
index fccba1d88d5c..8ed89d4fa306 100644
@@ -11,6 +11,7 @@
 
 #include <xen/list.h>
 #include <xen/mm.h>
+#include <xen/sha2.h>
 #include <public/kexec.h>
 
 #define KEXEC_SEGMENT_MAX 16
@@ -37,6 +38,8 @@ struct kexec_image {
 
     /* Address of next control page to allocate for crash kernels. */
     paddr_t next_crash_page;
+
+    uint8_t digest[SHA2_256_DIGEST_SIZE];
 };
 
 int kimage_alloc(struct kexec_image **rimage, uint8_t type, uint16_t arch,
@@ -52,6 +55,9 @@ mfn_t kimage_entry_mfn(kimage_entry_t *entry, bool compat);
 unsigned long kimage_entry_ind(kimage_entry_t *entry, bool compat);
 int kimage_build_ind(struct kexec_image *image, mfn_t ind_mfn,
                      bool compat);
+bool kimage_verify_digest(const struct kexec_image *image);
+void kimage_calc_digest(const struct kexec_image *image,
+                        uint8_t digest[SHA2_256_DIGEST_SIZE]);
 
 #endif /* __ASSEMBLER__ */
 
Signature · 1 line
2.52.0
#2 lore raw

PATCH v3 2/7 x86/kexec: rename setup_header to bzimage_header

In reply to Kevin Lampis

From: Ross Lagerwall <[email protected]>

The newly named bzimage_header is moved to a header file because part of it will be needed for Secure Boot kexec.

---
Changes in v3:
- No changes

Changes in v2:
- Remove unneeded casts
- Move setup_header to appropriate header file bzimage.h
- Renamed setup_header to less generic name bzimage_header

---
 xen/arch/x86/bzimage.c             | 47 +++---------------------------
 xen/arch/x86/include/asm/bzimage.h | 39 +++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 43 deletions(-)
xen/arch/x86/bzimage.c +4 -43
index 66f648f311e4..02433ac46f0b 100644
@@ -13,48 +13,9 @@ static __init unsigned long output_length(void *image, unsigned long image_len)
     return *(uint32_t *)(image + image_len - 4);
 }
 
-struct __packed setup_header {
-        uint8_t         _pad0[0x1f1];           /* skip uninteresting stuff */
-        uint8_t         setup_sects;
-        uint16_t        root_flags;
-        uint32_t        syssize;
-        uint16_t        ram_size;
-        uint16_t        vid_mode;
-        uint16_t        root_dev;
-        uint16_t        boot_flag;
-        uint16_t        jump;
-        uint32_t        header;
-#define HDR_MAGIC               "HdrS"
-#define HDR_MAGIC_SZ    4
-        uint16_t        version;
-#define VERSION(h,l)    (((h)<<8) | (l))
-        uint32_t        realmode_swtch;
-        uint16_t        start_sys;
-        uint16_t        kernel_version;
-        uint8_t         type_of_loader;
-        uint8_t         loadflags;
-        uint16_t        setup_move_size;
-        uint32_t        code32_start;
-        uint32_t        ramdisk_image;
-        uint32_t        ramdisk_size;
-        uint32_t        bootsect_kludge;
-        uint16_t        heap_end_ptr;
-        uint16_t        _pad1;
-        uint32_t        cmd_line_ptr;
-        uint32_t        initrd_addr_max;
-        uint32_t        kernel_alignment;
-        uint8_t         relocatable_kernel;
-        uint8_t         _pad2[3];
-        uint32_t        cmdline_size;
-        uint32_t        hardware_subarch;
-        uint64_t        hardware_subarch_data;
-        uint32_t        payload_offset;
-        uint32_t        payload_length;
-    };
-
-static __init int bzimage_check(struct setup_header *hdr, unsigned long len)
+static __init int bzimage_check(struct bzimage_header *hdr, unsigned long len)
 {
-    if ( len < sizeof(struct setup_header) )
+    if ( len < sizeof(struct bzimage_header) )
         return 0;
 
     if ( memcmp(&hdr->header, HDR_MAGIC, HDR_MAGIC_SZ) != 0 )
@@ -73,7 +34,7 @@ static unsigned long __initdata orig_image_len;
 unsigned long __init bzimage_headroom(void *image_start,
                                       unsigned long image_length)
 {
-    struct setup_header *hdr = (struct setup_header *)image_start;
+    struct bzimage_header *hdr = image_start;
     int err;
     unsigned long headroom;
 
@@ -106,7 +67,7 @@ unsigned long __init bzimage_headroom(void *image_start,
 int __init bzimage_parse(void *image_base, void **image_start,
                          unsigned long *image_len)
 {
-    struct setup_header *hdr = (struct setup_header *)(*image_start);
+    struct bzimage_header *hdr = *image_start;
     int err = bzimage_check(hdr, *image_len);
     unsigned long output_len;
 
xen/arch/x86/include/asm/bzimage.h +39 -0
index 7ed69d39103d..8c54b21d0685 100644
@@ -8,4 +8,43 @@ unsigned long bzimage_headroom(void *image_start, unsigned long image_length);
 int bzimage_parse(void *image_base, void **image_start,
                   unsigned long *image_len);
 
+struct __packed bzimage_header {
+        uint8_t         _pad0[0x1f1];           /* skip uninteresting stuff */
+        uint8_t         setup_sects;
+        uint16_t        root_flags;
+        uint32_t        syssize;
+        uint16_t        ram_size;
+        uint16_t        vid_mode;
+        uint16_t        root_dev;
+        uint16_t        boot_flag;
+        uint16_t        jump;
+        uint32_t        header;
+#define HDR_MAGIC               "HdrS"
+#define HDR_MAGIC_SZ    4
+        uint16_t        version;
+#define VERSION(h,l)    (((h)<<8) | (l))
+        uint32_t        realmode_swtch;
+        uint16_t        start_sys;
+        uint16_t        kernel_version;
+        uint8_t         type_of_loader;
+        uint8_t         loadflags;
+        uint16_t        setup_move_size;
+        uint32_t        code32_start;
+        uint32_t        ramdisk_image;
+        uint32_t        ramdisk_size;
+        uint32_t        bootsect_kludge;
+        uint16_t        heap_end_ptr;
+        uint16_t        _pad1;
+        uint32_t        cmd_line_ptr;
+        uint32_t        initrd_addr_max;
+        uint32_t        kernel_alignment;
+        uint8_t         relocatable_kernel;
+        uint8_t         _pad2[3];
+        uint32_t        cmdline_size;
+        uint32_t        hardware_subarch;
+        uint64_t        hardware_subarch_data;
+        uint32_t        payload_offset;
+        uint32_t        payload_length;
+    };
+
 #endif /* __X86_BZIMAGE_H__ */
Signature · 1 line
2.52.0
#3 lore raw

PATCH v3 3/7 x86/kexec: add new struct kimage_segment

In reply to Kevin Lampis

From: Ross Lagerwall <[email protected]>

New struct for internal use because Secure Boot code needs to add an extra field in a future patch.

---
Changes in v3:
- kexec_load: move variable i into loop scope
- kexec_load: Add blank line after tmp_seg declaration
- kexec_load: Don't initialise tmp_seg
- (Jan) I decided not to change uint64_t to size_t/paddr_t because it would require pulling in more headers and I don't want to add more complexity here.

Changes in v2:
- Read into a local xen_kexec_segment_t variable and fill in segments[i] manually instead of casting from a different sized type
- Remove union and _pad from new struct kimage_segment type
- Swap xen_kexec_segment_t to struct kimage_segment in separate patch

---
 xen/common/kexec.c       | 19 ++++++++++++++-----
 xen/common/kimage.c      | 25 +++++++++++++------------
 xen/include/xen/kimage.h | 11 +++++++++--
 3 files changed, 36 insertions(+), 19 deletions(-)
xen/common/kexec.c +14 -5
index 68f4c2cb2671..c92c464fe0ec 100644
@@ -917,7 +917,7 @@ static int kexec_load_slot(struct kexec_image *kimage)
 static int kexec_load(XEN_GUEST_HANDLE_PARAM(void) uarg)
 {
     xen_kexec_load_t load;
-    xen_kexec_segment_t *segments;
+    struct kimage_segment *segments;
     struct kexec_image *kimage = NULL;
     int ret;
 
@@ -927,14 +927,23 @@ static int kexec_load(XEN_GUEST_HANDLE_PARAM(void) uarg)
     if ( load.nr_segments >= KEXEC_SEGMENT_MAX )
         return -EINVAL;
 
-    segments = xmalloc_array(xen_kexec_segment_t, load.nr_segments);
+    segments = xmalloc_array(struct kimage_segment, load.nr_segments);
     if ( segments == NULL )
         return -ENOMEM;
 
-    if ( copy_from_guest(segments, load.segments.h, load.nr_segments) )
+    for ( unsigned int i = 0; i < load.nr_segments; i++ )
     {
-        ret = -EFAULT;
-        goto error;
+        xen_kexec_segment_t tmp_seg;
+
+        if ( copy_from_guest_offset(&tmp_seg, load.segments.h, i, 1) )
+        {
+            ret = -EFAULT;
+            goto error;
+        }
+        segments[i].h = tmp_seg.buf.h;
+        segments[i].buf_size = tmp_seg.buf_size;
+        segments[i].dest_maddr = tmp_seg.dest_maddr;
+        segments[i].dest_size = tmp_seg.dest_size;
     }
 
     ret = kimage_alloc(&kimage, load.type, load.arch, load.entry_maddr,
xen/common/kimage.c +13 -12
index 6e009529ae01..dc4730622371 100644
@@ -81,7 +81,7 @@ static struct page_info *kimage_alloc_zeroed_page(unsigned memflags)
 
 static int do_kimage_alloc(struct kexec_image **rimage, paddr_t entry,
                            unsigned long nr_segments,
-                           xen_kexec_segment_t *segments, uint8_t type)
+                           struct kimage_segment *segments, uint8_t type)
 {
     struct kexec_image *image;
     unsigned long i;
@@ -206,7 +206,7 @@ out:
 
 static int kimage_normal_alloc(struct kexec_image **rimage, paddr_t entry,
                                unsigned long nr_segments,
-                               xen_kexec_segment_t *segments)
+                               struct kimage_segment *segments)
 {
     return do_kimage_alloc(rimage, entry, nr_segments, segments,
                            KEXEC_TYPE_DEFAULT);
@@ -214,7 +214,7 @@ static int kimage_normal_alloc(struct kexec_image **rimage, paddr_t entry,
 
 static int kimage_crash_alloc(struct kexec_image **rimage, paddr_t entry,
                               unsigned long nr_segments,
-                              xen_kexec_segment_t *segments)
+                              struct kimage_segment *segments)
 {
     unsigned long i;
 
@@ -236,7 +236,7 @@ static int kimage_crash_alloc(struct kexec_image **rimage, paddr_t entry,
     {
         paddr_t mstart, mend;
 
-        if ( guest_handle_is_null(segments[i].buf.h) )
+        if ( guest_handle_is_null(segments[i].h) )
             continue;
 
         mstart = segments[i].dest_maddr;
@@ -664,7 +664,7 @@ found:
 }
 
 static int kimage_load_normal_segment(struct kexec_image *image,
-                                      xen_kexec_segment_t *segment)
+                                      struct kimage_segment *segment)
 {
     unsigned long to_copy;
     unsigned long src_offset;
@@ -698,7 +698,7 @@ static int kimage_load_normal_segment(struct kexec_image *image,
             return ret;
 
         dest_va = __map_domain_page(page);
-        ret = copy_from_guest_offset(dest_va, segment->buf.h, src_offset, size);
+        ret = copy_from_guest_offset(dest_va, segment->h, src_offset, size);
         unmap_domain_page(dest_va);
         if ( ret )
             return -EFAULT;
@@ -717,7 +717,7 @@ static int kimage_load_normal_segment(struct kexec_image *image,
 }
 
 static int kimage_load_crash_segment(struct kexec_image *image,
-                                     xen_kexec_segment_t *segment)
+                                     struct kimage_segment *segment)
 {
     /*
      * For crash dumps kernels we simply copy the data from user space
@@ -747,7 +747,7 @@ static int kimage_load_crash_segment(struct kexec_image *image,
         if ( !dest_va )
             return -EINVAL;
 
-        ret = copy_from_guest_offset(dest_va, segment->buf.h, src_offset, schunk);
+        ret = copy_from_guest_offset(dest_va, segment->h, src_offset, schunk);
         memset(dest_va + schunk, 0, dchunk - schunk);
 
         unmap_domain_page(dest_va);
@@ -763,12 +763,13 @@ static int kimage_load_crash_segment(struct kexec_image *image,
     return 0;
 }
 
-static int kimage_load_segment(struct kexec_image *image, xen_kexec_segment_t *segment)
+static int kimage_load_segment(struct kexec_image *image,
+                               struct kimage_segment *segment)
 {
     int result = -ENOMEM;
     paddr_t addr;
 
-    if ( !guest_handle_is_null(segment->buf.h) )
+    if ( !guest_handle_is_null(segment->h) )
     {
         switch ( image->type )
         {
@@ -794,7 +795,7 @@ static int kimage_load_segment(struct kexec_image *image, xen_kexec_segment_t *s
 
 int kimage_alloc(struct kexec_image **rimage, uint8_t type, uint16_t arch,
                  uint64_t entry_maddr,
-                 uint32_t nr_segments, xen_kexec_segment_t *segment)
+                 uint32_t nr_segments, struct kimage_segment *segment)
 {
     int result;
 
@@ -819,7 +820,7 @@ int kimage_alloc(struct kexec_image **rimage, uint8_t type, uint16_t arch,
 }
 
 static void kimage_calc_one_digest(struct sha2_256_state *ctx,
-                                   xen_kexec_segment_t *segment)
+                                   struct kimage_segment *segment)
 {
     paddr_t dest;
     unsigned long sbytes;
xen/include/xen/kimage.h +9 -2
index 8ed89d4fa306..0841b6f32114 100644
@@ -18,12 +18,19 @@
 
 typedef paddr_t kimage_entry_t;
 
+struct kimage_segment {
+    XEN_GUEST_HANDLE(const_void) h;
+    uint64_t buf_size;
+    uint64_t dest_maddr;
+    uint64_t dest_size;
+};
+
 struct kexec_image {
     uint8_t type;
     uint16_t arch;
     uint64_t entry_maddr;
     uint32_t nr_segments;
-    xen_kexec_segment_t *segments;
+    struct kimage_segment *segments;
 
     kimage_entry_t head;
     struct page_info *entry_page;
@@ -44,7 +51,7 @@ struct kexec_image {
 
 int kimage_alloc(struct kexec_image **rimage, uint8_t type, uint16_t arch,
                  uint64_t entry_maddr,
-                 uint32_t nr_segments, xen_kexec_segment_t *segment);
+                 uint32_t nr_segments, struct kimage_segment *segment);
 void kimage_free(struct kexec_image *image);
 int kimage_load_segments(struct kexec_image *image);
 struct page_info *kimage_alloc_control_page(struct kexec_image *image,
Signature · 1 line
2.52.0
#4 lore raw

PATCH v3 4/7 x86/kexec: Support non-page-aligned kexec segments

In reply to Kevin Lampis

From: Ross Lagerwall <[email protected]>

With Secure Boot, userspace passes in the entire kernel loaded for verification purposes. However, the kernel's startup32 function needs to be aligned (e.g. to 16 MiB) and this results in the start of the segment not being page-aligned (depending on where the startup32 function lands in the kernel binary). Relax this restriction in Xen to support this use case.

---
Changes in v3:
- No changes

Changes in v2:
- Remove error handling around map_domain_page because it doesn't fail

---
 xen/common/kimage.c      | 47 +++++++++++++++++++++++++++++++---------
 xen/include/xen/kimage.h |  1 +
 2 files changed, 38 insertions(+), 10 deletions(-)
xen/common/kimage.c +37 -10
index dc4730622371..d4a695a2bdb9 100644
@@ -725,12 +725,14 @@ static int kimage_load_crash_segment(struct kexec_image *image,
      */
     paddr_t dest;
     unsigned long sbytes, dbytes;
+    unsigned int dest_offset;
     int ret = 0;
     unsigned long src_offset = 0;
 
     sbytes = segment->buf_size;
     dbytes = segment->dest_size;
     dest = segment->dest_maddr;
+    dest_offset = segment->dest_offset;
 
     while ( dbytes )
     {
@@ -740,24 +742,26 @@ static int kimage_load_crash_segment(struct kexec_image *image,
 
         dest_mfn = dest >> PAGE_SHIFT;
 
-        dchunk = PAGE_SIZE;
+        dchunk = PAGE_SIZE - dest_offset;
         schunk = min(dchunk, sbytes);
 
         dest_va = map_domain_page(_mfn(dest_mfn));
-        if ( !dest_va )
-            return -EINVAL;
 
-        ret = copy_from_guest_offset(dest_va, segment->h, src_offset, schunk);
-        memset(dest_va + schunk, 0, dchunk - schunk);
+        if ( dest_offset )
+            memset(dest_va, 0, dest_offset);
+        ret = copy_from_guest_offset(dest_va + dest_offset, segment->h,
+                                     src_offset, schunk);
+        memset(dest_va + dest_offset + schunk, 0, dchunk - schunk);
 
         unmap_domain_page(dest_va);
         if ( ret )
             return -EFAULT;
 
-        dbytes -= dchunk;
+        dbytes -= dchunk + dest_offset;
         sbytes -= schunk;
-        dest += dchunk;
+        dest += dchunk + dest_offset;
         src_offset += schunk;
+        dest_offset = 0;
     }
 
     return 0;
@@ -798,6 +802,26 @@ int kimage_alloc(struct kexec_image **rimage, uint8_t type, uint16_t arch,
                  uint32_t nr_segments, struct kimage_segment *segment)
 {
     int result;
+    unsigned int i;
+
+    for ( i = 0; i < nr_segments; i++ )
+    {
+        paddr_t mend;
+
+        /*
+         * Stash the destination offset-in-page for use when copying the
+         * buffer later.
+         */
+        segment[i].dest_offset = PAGE_OFFSET(segment[i].dest_maddr);
+
+        /*
+         * Align down the start address to page size and align up the end
+         * address to page size.
+         */
+        mend = segment[i].dest_maddr + segment[i].dest_size;
+        segment[i].dest_maddr &= PAGE_MASK;
+        segment[i].dest_size = ROUNDUP(mend, PAGE_SIZE) - segment[i].dest_maddr;
+    }
 
     switch( type )
     {
@@ -824,9 +848,11 @@ static void kimage_calc_one_digest(struct sha2_256_state *ctx,
 {
     paddr_t dest;
     unsigned long sbytes;
+    unsigned int dest_offset;
 
     sbytes = segment->buf_size;
     dest = segment->dest_maddr;
+    dest_offset = segment->dest_offset;
 
     while ( sbytes )
     {
@@ -836,15 +862,16 @@ static void kimage_calc_one_digest(struct sha2_256_state *ctx,
 
         dest_mfn = dest >> PAGE_SHIFT;
 
-        dchunk = PAGE_SIZE;
+        dchunk = PAGE_SIZE - dest_offset;
         schunk = min(dchunk, sbytes);
 
         dest_va = map_domain_page(_mfn(dest_mfn));
-        sha2_256_update(ctx, dest_va, schunk);
+        sha2_256_update(ctx, dest_va + dest_offset, schunk);
         unmap_domain_page(dest_va);
 
         sbytes -= schunk;
-        dest += dchunk;
+        dest += dchunk + dest_offset;
+        dest_offset = 0;
     }
 }
 
xen/include/xen/kimage.h +1 -0
index 0841b6f32114..86250f29394e 100644
@@ -23,6 +23,7 @@ struct kimage_segment {
     uint64_t buf_size;
     uint64_t dest_maddr;
     uint64_t dest_size;
+    unsigned int dest_offset;
 };
 
 struct kexec_image {
Signature · 1 line
2.52.0
#5 lore raw

PATCH v3 5/7 x86/kexec: Implement new EFI load type

In reply to Kevin Lampis

From: Ross Lagerwall <[email protected]>

Add new EFI load type for kexec. This load type is suitable for use when Secure Boot is enabled.

When this load type is used, the caller should not pass purgatory as one of the kexec segments. Instead, Xen will prepare any glue code needed internally.

The new load type only works for Linux bzImages using the x86 64-bit boot protocol documented at linux/Documentation/arch/x86/boot.rst.

---
Changes in v3:
- Makefile: insert kexec.o alphabetically
- kimage_find_kernel_entry_maddr: use MB() macro
- public/kexec.h: Don't use anonymous union
This required a new change to tools/libs/ctrl/xc_kexec.c

Changes in v2:
- Move kimage_find_kernel_entry_maddr to arch specific kexec.c file Didn't add stubs for other archs because CONFIG_HAS_KEXEC is x86 only
- When parsing kernel entry check for header magic and boot protocol version
- When parsing kernel entry use setup_sects instead of ROUNDUP
- Update commit message to explain new kexec type only works for x86 linux
- Rename kexec_image->boot_params to entry_arg
- Use size_t for return type of kernel_alignment_offset()
- Remove error handling around map_domain_page because it doesn't fail

---
 tools/libs/ctrl/xc_kexec.c         |  2 +-
 xen/arch/x86/Makefile              |  1 +
 xen/arch/x86/include/asm/bzimage.h |  5 ++
 xen/arch/x86/include/asm/kexec.h   |  9 ++++
 xen/arch/x86/kexec.c               | 58 +++++++++++++++++++++++
 xen/common/kexec.c                 | 11 ++++-
 xen/common/kimage.c                | 75 +++++++++++++++++++-----------
 xen/include/public/kexec.h         | 12 ++++-
 xen/include/xen/kimage.h           |  2 +
 9 files changed, 143 insertions(+), 32 deletions(-)
 create mode 100644 xen/arch/x86/include/asm/kexec.h
 create mode 100644 xen/arch/x86/kexec.c
tools/libs/ctrl/xc_kexec.c +1 -1
index a4e8966adc32..d54353b2ddc9 100644
@@ -88,7 +88,7 @@ int xc_kexec_load(xc_interface *xch, uint8_t type, uint16_t arch,
 
     load->type = type;
     load->arch = arch;
-    load->entry_maddr = entry_maddr;
+    load->p.entry_maddr = entry_maddr;
     load->nr_segments = nr_segments;
     set_xen_guest_handle(load->segments.h, segments);
 
xen/arch/x86/Makefile +1 -0
index 2be90ea09880..cd08180c37e1 100644
@@ -46,6 +46,7 @@ obj-$(CONFIG_PV) += ioport_emulate.o
 obj-y += io_apic.o
 obj-y += irq.o
 obj-$(CONFIG_LIVEPATCH) += livepatch.o
+obj-$(CONFIG_KEXEC) += kexec.o
 obj-$(CONFIG_KEXEC) += machine_kexec.o
 obj-y += mm.o x86_64/mm.o
 obj-$(CONFIG_VM_EVENT) += monitor.o
xen/arch/x86/include/asm/bzimage.h +5 -0
index 8c54b21d0685..e363cc9a4f84 100644
@@ -47,4 +47,9 @@ struct __packed bzimage_header {
         uint32_t        payload_length;
     };
 
+static inline size_t kernel_alignment_offset(void)
+{
+    return offsetof(struct bzimage_header, kernel_alignment);
+}
+
 #endif /* __X86_BZIMAGE_H__ */
xen/arch/x86/include/asm/kexec.h new file +9 -0
new file mode 100644
index 000000000000..04b7eee4c1a9
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef ASM_X86_KEXEC_H
+#define ASM_X86_KEXEC_H
+
+struct kexec_image;
+int64_t kimage_find_kernel_entry_maddr(struct kexec_image *image);
+
+#endif /* ASM_X86_KEXEC_H */
xen/arch/x86/kexec.c new file +58 -0

58 lines not on this page · raw message

xen/common/kexec.c +10 -1
index c92c464fe0ec..2ad09cbdc1f3 100644
@@ -757,6 +757,7 @@ static int kexec_load_get_bits(int type, int *base, int *bit)
         *bit = KEXEC_FLAG_DEFAULT_POS;
         break;
     case KEXEC_TYPE_CRASH:
+    case KEXEC_TYPE_CRASH_EFI:
         *base = KEXEC_IMAGE_CRASH_BASE;
         *bit = KEXEC_FLAG_CRASH_POS;
         break;
@@ -856,6 +857,7 @@ static int kexec_exec(XEN_GUEST_HANDLE_PARAM(void) uarg)
         break;
 
     case KEXEC_TYPE_CRASH:
+    case KEXEC_TYPE_CRASH_EFI:
         kexec_crash(CRASHREASON_KEXECCMD); /* Does not return */
         break;
     }
@@ -946,7 +948,7 @@ static int kexec_load(XEN_GUEST_HANDLE_PARAM(void) uarg)
         segments[i].dest_size = tmp_seg.dest_size;
     }
 
-    ret = kimage_alloc(&kimage, load.type, load.arch, load.entry_maddr,
+    ret = kimage_alloc(&kimage, load.type, load.arch, load.p.entry_maddr,
                        load.nr_segments, segments);
     if ( ret < 0 )
         goto error;
@@ -957,6 +959,13 @@ static int kexec_load(XEN_GUEST_HANDLE_PARAM(void) uarg)
 
     kimage_calc_digest(kimage, kimage->digest);
 
+    if ( load.type == KEXEC_TYPE_CRASH_EFI )
+    {
+        ret = kimage_efi_setup(kimage, load.p.parameters);
+        if ( ret < 0 )
+            goto error;
+    }
+
     ret = kexec_load_slot(kimage);
     if ( ret < 0 )
         goto error;
xen/common/kimage.c +47 -28

123 lines not on this page · raw message

xen/include/public/kexec.h +10 -2
index abb2a49238f1..25eef43dab53 100644
@@ -54,13 +54,16 @@
  * - kexec into a regular kernel, very similar to a standard reboot
  *   - KEXEC_TYPE_DEFAULT is used to specify this type
  * - kexec into a special "crash kernel", aka kexec-on-panic
- *   - KEXEC_TYPE_CRASH is used to specify this type
+ *   - KEXEC_TYPE_CRASH or KEXEC_TYPE_CRASH_EFI are used to specify this type
+ *   - in case of KEXEC_TYPE_CRASH_EFI the first segment will point to the
+ *     full kernel to load and entry point will point to boot params
  *   - parts of our system may be broken at kexec-on-panic time
  *     - the code should be kept as simple and self-contained as possible
  */
 
 #define KEXEC_TYPE_DEFAULT 0
 #define KEXEC_TYPE_CRASH   1
+#define KEXEC_TYPE_CRASH_EFI 3
 
 /*
  * Perform kexec having previously loaded a kexec or kdump kernel
@@ -167,7 +170,12 @@ typedef struct xen_kexec_load {
         XEN_GUEST_HANDLE(xen_kexec_segment_t) h;
         uint64_t _pad;
     } segments;
-    uint64_t entry_maddr; /* image entry point machine address. */
+
+    /* Image entry point machine address or parameters in case of EFI. */
+    union {
+        uint64_t entry_maddr;
+        uint64_t parameters;
+    } p;
 } xen_kexec_load_t;
 DEFINE_XEN_GUEST_HANDLE(xen_kexec_load_t);
 
xen/include/xen/kimage.h +2 -0
index 86250f29394e..fdda0a89a53a 100644
@@ -48,6 +48,7 @@ struct kexec_image {
     paddr_t next_crash_page;
 
     uint8_t digest[SHA2_256_DIGEST_SIZE];
+    uint64_t entry_arg;
 };
 
 int kimage_alloc(struct kexec_image **rimage, uint8_t type, uint16_t arch,
@@ -66,6 +67,7 @@ int kimage_build_ind(struct kexec_image *image, mfn_t ind_mfn,
 bool kimage_verify_digest(const struct kexec_image *image);
 void kimage_calc_digest(const struct kexec_image *image,
                         uint8_t digest[SHA2_256_DIGEST_SIZE]);
+int kimage_efi_setup(struct kexec_image *image, uint64_t parameters);
 
 #endif /* __ASSEMBLER__ */
 
Signature · 1 line
2.52.0
#6 lore raw

Re: [PATCH v3 5/7] x86/kexec: Implement new EFI load type

tools/libs/ctrl/xc_kexec.c int xc_kexec_load(xc_interface *xch, uint8_t type, uint16_t arch, · line 93

Quoted 10 lines · Kevin Lampis
tools/libs/ctrl/xc_kexec.c +1 -1
@@ -88,7 +88,7 @@ int xc_kexec_load(xc_interface *xch, uint8_t type, uint16_t arch,
 
     load->type = type;
     load->arch = arch;
-    load->entry_maddr = entry_maddr;
+    load->p.entry_maddr = entry_maddr;
     load->nr_segments = nr_segments;
     set_xen_guest_handle(load->segments.h, segments);

As can be seen from the need for this adjustment, ...

tools/libs/ctrl/xc_kexec.c typedef struct xen_kexec_load { · line 180

Quoted 13 lines
tools/libs/ctrl/xc_kexec.c +6 -1
@@ -167,7 +170,12 @@ typedef struct xen_kexec_load {
         XEN_GUEST_HANDLE(xen_kexec_segment_t) h;
         uint64_t _pad;
     } segments;
-    uint64_t entry_maddr; /* image entry point machine address. */
+
+    /* Image entry point machine address or parameters in case of EFI. */
+    union {
+        uint64_t entry_maddr;
+        uint64_t parameters;
+    } p;
 } xen_kexec_load_t;
 DEFINE_XEN_GUEST_HANDLE(xen_kexec_load_t);

... this is a (build) breaking change to a public header.

Jan

#7 lore raw

PATCH v3 6/7 x86/kexec: Pass boot params directly to new kernel

In reply to Kevin Lampis

When doing kexec on an EFI image there is no purgatory and kexec_reloc.S hands off straight to the new kernel. To facilitate this the Linux kernel boot params need to be passed through %rsi.

---
Changes in v2:
- Rename kexec_image->boot_params to entry_arg
- Fix indentation

---
 xen/arch/x86/include/asm/machine_kexec.h | 2 +-
 xen/arch/x86/machine_kexec.c             | 3 ++-
 xen/arch/x86/x86_64/kexec_reloc.S        | 3 +++
 3 files changed, 6 insertions(+), 2 deletions(-)
xen/arch/x86/include/asm/machine_kexec.h +1 -1
index 3e189acf2464..010e0c704626 100644
@@ -7,7 +7,7 @@
 
 extern void kexec_reloc(unsigned long reloc_code, unsigned long reloc_pt,
                         unsigned long ind_maddr, unsigned long entry_maddr,
-                        unsigned long flags);
+                        unsigned long flags, unsigned long arg);
 
 extern const char kexec_reloc_end[];
 
xen/arch/x86/machine_kexec.c +2 -1
index 038670922d47..67ca3e09bece 100644
@@ -199,7 +199,8 @@ void machine_kexec(struct kexec_image *image)
 
     kexec_reloc(page_to_maddr(image->control_code_page),
                 page_to_maddr(image->aux_page),
-                image->head, image->entry_maddr, reloc_flags);
+                image->head, image->entry_maddr, reloc_flags,
+                image->entry_arg);
 }
 
 int machine_kexec_get(xen_kexec_range_t *range)
xen/arch/x86/x86_64/kexec_reloc.S +3 -0
index 1d023733a916..0f163862b858 100644
@@ -41,6 +41,7 @@ FUNC(kexec_reloc, PAGE_SIZE)
         /* %rdx - indirection page maddr */
         /* %rcx - entry maddr (%rbp) */
         /* %r8 - flags */
+        /* %r9 - entry arg */
 
         movq    %rcx, %rbp
 
@@ -104,6 +105,8 @@ FUNC(kexec_reloc, PAGE_SIZE)
         movq    %rdx, %rdi
         call    relocate_pages
 
+        movq    %r9, %rsi /* entry arg */
+
         /* Need to switch to 32-bit mode? */
         testq   $KEXEC_RELOC_FLAG_COMPAT, %r8
         jnz     .L_call_32_bit
Signature · 1 line
2.52.0
#8 lore raw

PATCH v3 7/7 x86/kexec: Zero GPRs before entering

In reply to Kevin Lampis

Purgatory zeros these general purpose registers before jumping to the new kernel. When doing kexec on an EFI kernel there is no purgatory so we need to zero the registers here instead. This change shouldn't affect the functionality of legacy kexec.

---
Changes in v3:
- No changes

Changes in v2:
- Move the 32bit lines to the correct place
- Reword the patch subject line
- Fix indentation

---
 xen/arch/x86/x86_64/kexec_reloc.S | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
xen/arch/x86/x86_64/kexec_reloc.S +20 -0
index 0f163862b858..ed1cf15e6c94 100644
@@ -111,6 +111,20 @@ FUNC(kexec_reloc, PAGE_SIZE)
         testq   $KEXEC_RELOC_FLAG_COMPAT, %r8
         jnz     .L_call_32_bit
 
+        xor     %edi, %edi
+        xor     %eax, %eax
+        xor     %ebx, %ebx
+        xor     %ecx, %ecx
+        xor     %edx, %edx
+        xor     %r8d, %r8d
+        xor     %r9d, %r9d
+        xor     %r10d, %r10d
+        xor     %r11d, %r11d
+        xor     %r12d, %r12d
+        xor     %r13d, %r13d
+        xor     %r14d, %r14d
+        xor     %r15d, %r15d
+
         /* Jump to the image entry point */
         jmp     *%rbp
 
@@ -127,6 +141,7 @@ FUNC(kexec_reloc, PAGE_SIZE)
         lea     compatibility_mode(%rip), %rax
         push    $0x10
         push    %rax
+
         lretq
 END(kexec_reloc)
 
@@ -204,6 +219,11 @@ FUNC_LOCAL(compatibility_mode)
         xorl    %eax, %eax
         movl    %eax, %cr4
 
+        xor     %edi, %edi
+        xor     %ebx, %ebx
+        xor     %ecx, %ecx
+        xor     %edx, %edx
+
         /* Jump to the image entry point. */
         jmp     *%ebp
 END(compatibility_mode)
Signature · 1 line
2.52.0