Skip to contentSkip to conversation

kexec: add kexec support to Mini-OS

PATCH Superseded Juergen Gross 4 participants 37 messages Mar 21, 2025 – Jun 16, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover kexec: add kexec support to Mini-OS

Add basic kexec support to Mini-OS for running in x86 PVH mode.

With this series applied it is possible to activate another kernel from within Mini-OS.

Right now no Xen related teardown is done (so no reset of grant table, event channels, PV devices). These should be added via kexec callbacks which are added as a framework.

This is a major building block for support of Xenstore-stubdom live update (in fact I've tested the kexec path to work using the PVH variant of Xenstore-stubdom).

Juergen Gross (12):
add kexec framework
Mini-OS: add final kexec stage
mini-os: add elf.h
mini-os: analyze new kernel for kexec
mini-os: kexec: finalize parameter location and size
mini-os: reserve memory below boundary
mini-os: kexec: build parameters for new kernel
mini-os: kexec: move used pages away for new kernel
Mini-OS: mm: change set_readonly() to change_readonly()
Mini-OS: kexec: switch read-only area to be writable again
mini-os: kexec: add kexec callback functionality
mini-os: kexec: do the final kexec step

 Config.mk                  |   1 +
 Makefile                   |   1 +
 arch/x86/kexec.c           | 273 +++++++++++++++++++++++++++++
 arch/x86/minios-x86.lds.S  |  16 ++
 arch/x86/mm.c              | 238 ++++++++++++++++++++------
 arch/x86/testbuild/all-no  |   1 +
 arch/x86/testbuild/all-yes |   2 +
 arch/x86/testbuild/kexec   |   4 +
 arch/x86/x86_hvm.S         |  46 +++++
 include/elf.h              | 340 +++++++++++++++++++++++++++++++++++++
 include/kexec.h            |  63 +++++++
 include/mm.h               |   8 +
 include/x86/os.h           |   5 +
 kexec.c                    | 253 +++++++++++++++++++++++++++
 mm.c                       |  89 +++++++++-
 15 files changed, 1289 insertions(+), 51 deletions(-)
 create mode 100644 arch/x86/kexec.c
 create mode 100644 arch/x86/testbuild/kexec
 create mode 100644 include/elf.h
 create mode 100644 include/kexec.h
 create mode 100644 kexec.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/12 kexec: add kexec framework

Add a new config option CONFIG_KEXEC for support of kexec-ing into a new mini-os kernel. Add a related kexec.c source and a kexec.h header.

For now allow CONFIG_KEXEC to be set only for PVH variant of mini-os.

---
 Config.mk                  |  1 +
 Makefile                   |  1 +
 arch/x86/testbuild/all-no  |  1 +
 arch/x86/testbuild/all-yes |  2 ++
 arch/x86/testbuild/kexec   |  4 +++
 include/kexec.h            |  7 +++++
 kexec.c                    | 62 ++++++++++++++++++++++++++++++++++++++
 7 files changed, 78 insertions(+)
 create mode 100644 arch/x86/testbuild/kexec
 create mode 100644 include/kexec.h
 create mode 100644 kexec.c
Config.mk +1 -0
index e493533a..e2afb1b4 100644
@@ -204,6 +204,7 @@ CONFIG-n += CONFIG_LIBXENGUEST
 CONFIG-n += CONFIG_LIBXENTOOLCORE
 CONFIG-n += CONFIG_LIBXENTOOLLOG
 CONFIG-n += CONFIG_LIBXENMANAGE
+CONFIG-n += CONFIG_KEXEC
 # Setting CONFIG_USE_XEN_CONSOLE copies all print output to the Xen emergency
 # console apart of standard dom0 handled console.
 CONFIG-n += CONFIG_USE_XEN_CONSOLE
Makefile +1 -0
index d094858a..a64913ad 100644
@@ -51,6 +51,7 @@ src-y += gntmap.c
 src-y += gnttab.c
 src-y += hypervisor.c
 src-y += kernel.c
+src-$(CONFIG_KEXEC) += kexec.c
 src-y += lock.c
 src-y += main.c
 src-y += mm.c
arch/x86/testbuild/all-no +1 -0
index 5b3e99ed..b2ee5ce8 100644
@@ -18,3 +18,4 @@ CONFIG_LIBXS = n
 CONFIG_LWIP = n
 CONFIG_BALLOON = n
 CONFIG_USE_XEN_CONSOLE = n
+CONFIG_KEXEC = n
arch/x86/testbuild/all-yes +2 -0
index 8ae489a4..99ba75dd 100644
@@ -19,3 +19,5 @@ CONFIG_BALLOON = y
 CONFIG_USE_XEN_CONSOLE = y
 # The following are special: they need support from outside
 CONFIG_LWIP = n
+# KEXEC only without PARAVIRT
+CONFIG_KEXEC = n
arch/x86/testbuild/kexec new file +4 -0
new file mode 100644
index 00000000..ea17b4d9
@@ -0,0 +1,4 @@
+CONFIG_PARAVIRT = n
+CONFIG_BALLOON = y
+CONFIG_USE_XEN_CONSOLE = y
+CONFIG_KEXEC = y
include/kexec.h new file +7 -0
new file mode 100644
index 00000000..6fd96774
@@ -0,0 +1,7 @@
+#ifndef _KEXEC_H
+#define _KEXEC_H
+
+int kexec(void *kernel, unsigned long kernel_size,
+          const char *cmdline);
+
+#endif /* _KEXEC_H */
kexec.c new file +62 -0

62 lines not on this page · raw message

Signature · 1 line
2.43.0
Reviewed-by on v1 1/12
#2 lore raw

Re: [MINI-OS PATCH 01/12] kexec: add kexec framework

Quoted 15 lines · Juergen Gross

Add a new config option CONFIG_KEXEC for support of kexec-ing into a new mini-os kernel. Add a related kexec.c source and a kexec.h header.

For now allow CONFIG_KEXEC to be set only for PVH variant of mini-os.

---

arch/x86/testbuild/all-yes +0 -0
index 8ae489a4..99ba75dd 100644

arch/x86/testbuild/all-yes CONFIG_BALLOON = y · line 22

Quoted 4 lines
arch/x86/testbuild/all-yes +1 -0
@@ -19,3 +19,5 @@ CONFIG_BALLOON = y
 CONFIG_USE_XEN_CONSOLE = y
 # The following are special: they need support from outside
 CONFIG_LWIP = n
+# KEXEC only without PARAVIRT

Maybe: "KEXEC not implemented for PARAVIRT"?

Quoted 9 lines
arch/x86/testbuild/all-yes +1 -0
+CONFIG_KEXEC = n
kexec.c new file +0 -0
new file mode 100644
index 00000000..53528169
@@ -0,0 +1,62 @@
 

kexec.c line 39

Quoted 10 lines
kexec.c new file +10 -0
+
+#include <errno.h>
+#include <mini-os/os.h>
+#include <mini-os/lib.h>
+#include <mini-os/kexec.h>
+
+/*
+ * General approach for kexec support (PVH only) is as follows:
+ *
+ * - New kernel needs to be in memory in form of a ELF file in a virtual

"in the form of an ELF binary"

Quoted 1 line

+ * memory region.

Maybe just "The new kernel needs to be an ELF binary loaded into the Mini-OS address space"?

Quoted 5 lines

+ * - A new start_info structure is constructed in memory with the final
+ * memory locations included.
+ * - All memory areas needed for kexec execution are being finalized.
+ * - From here on a graceful failure is no longer possible.
+ * - Grants and event channels are torn down.

Quoted 8 lines

+ * - A temporary set of page tables is constructed at a location where it
+ * doesn't conflict with old and new kernel or start_info.
+ * - The final kexec execution stage is copied to a memory area below 4G which
+ * doesn't conflict with the target areas of kernel etc.
+ * - Cr3 is switched to the new set of page tables.
+ * - Execution continues in the final execution stage.
+ * - All data is copied to its final addresses.
+ * - Processing is switched to 32-bit mode without address translation.

Maybe "CPU is switched to 32-bit mode with paging disabled."?

Is the following memory layout correct?

[ 0 ... 8MB ] ... [ X ... X + Y ] ... [ Z ... ]
Old stubdom New stubdom kexec code

kexec code copies New stubdom to 0 and later jumps to New stubdom @ 0

The temporary page tables are to allow old stubdom and kexec code to be called while overwriting the "Old stubdom" range which would include the page tables originally used? Or it can only run the kexec code once old stubdom is overwritten, right?

I think some comments tweaks would be helpful, but code-wise everything is okay, so:

Regards,
Jason

#3 lore raw

Re: [MINI-OS PATCH 01/12] kexec: add kexec framework

Jason,

thanks for having a look at the series! I very much appreciate that!

Quoted 11 lines · Jason Andryuk
Quoted 8 lines · Juergen Gross

Add a new config option CONFIG_KEXEC for support of kexec-ing into a new mini-os kernel. Add a related kexec.c source and a kexec.h header.

For now allow CONFIG_KEXEC to be set only for PVH variant of mini-os.

---

Quoted 11 lines

arch/x86/testbuild/all-yes CONFIG_BALLOON = y · line 22

Quoted 9 lines
arch/x86/testbuild/all-yes +1 -0
index 8ae489a4..99ba75dd 100644
@@ -19,3 +19,5 @@ CONFIG_BALLOON = y
  CONFIG_USE_XEN_CONSOLE = y
  # The following are special: they need support from outside
  CONFIG_LWIP = n
+# KEXEC only without PARAVIRT

Maybe: "KEXEC not implemented for PARAVIRT"?

Fine with me.

Quoted 23 lines
Quoted 10 lines
arch/x86/testbuild/all-yes +1 -0
+CONFIG_KEXEC = n
kexec.c new file +0 -0
new file mode 100644
index 00000000..53528169
@@ -0,0 +1,62 @@
 

kexec.c line 39

Quoted 10 lines
kexec.c new file +10 -0
+
+#include <errno.h>
+#include <mini-os/os.h>
+#include <mini-os/lib.h>
+#include <mini-os/kexec.h>
+
+/*
+ * General approach for kexec support (PVH only) is as follows:
+ *
+ * - New kernel needs to be in memory in form of a ELF file in a virtual

"in the form of an ELF binary"

Quoted 4 lines
Quoted 1 line

+ * memory region.

Maybe just "The new kernel needs to be an ELF binary loaded into the Mini-OS address space"?

The "virtual memory region" is quite important, as this allows to handle conflicts with the target memory layout on a per-page basis.

Quoted 15 lines
Quoted 14 lines

+ * - A new start_info structure is constructed in memory with the final
+ * memory locations included.
+ * - All memory areas needed for kexec execution are being finalized.
+ * - From here on a graceful failure is no longer possible.
+ * - Grants and event channels are torn down.
+ * - A temporary set of page tables is constructed at a location where it
+ * doesn't conflict with old and new kernel or start_info.
+ * - The final kexec execution stage is copied to a memory area below 4G which
+ * doesn't conflict with the target areas of kernel etc.
+ * - Cr3 is switched to the new set of page tables.
+ * - Execution continues in the final execution stage.
+ * - All data is copied to its final addresses.
+ * - Processing is switched to 32-bit mode without address translation.

Quoted 1 line

Maybe "CPU is switched to 32-bit mode with paging disabled."?

Okay.

Quoted 5 lines

Is the following memory layout correct?

[ 0 ... 8MB ] ... [ X ... X + Y ] ... [ Z ... ]
Old stubdom New stubdom kexec code

With:
O: old stubdom kernel
P: active page tables
N: new stubdom kernel
Z: kexec code.

The guest physical memory layout is more like:
OPOOONP.NN.N.NNN..ZNN..PP..

The target layout of this example (before the final kexec stage) will be:
O.OOO....N.N.NNNP.ZNNP.PPNN

Note that all conflicting N and P entries have been moved to a position behind the target position of the new kernel. This includes the page tables in the old kernel which were pre-populated at boot time.

And before passing control to the new kernel it will be:
NNNNNNNNN.........Z........

Quoted 1 line

kexec code copies New stubdom to 0 and later jumps to New stubdom @ 0

Kind of. The "0" is not hard wired in the kexec code.

Quoted 4 lines

The temporary page tables are to allow old stubdom and kexec code to be called while overwriting the "Old stubdom" range which would include the page tables originally used? Or it can only run the kexec code once old stubdom is overwritten, right?

Yes.

I just realized that some of the comments are stale now. The current implementation doesn't setup a new set of page tables, but is tweaking the existing one to avoid conflicts.

Quoted 4 lines

I think some comments tweaks would be helpful, but code-wise everything is okay, so:

Thanks,

Juergen

#4 lore raw

PATCH 2/12 kexec: add final kexec stage

In reply to Juergen Gross

Add the code and data definitions of the final kexec stage.

Put the code and related data into a dedicated section in order to be able to copy it to another location. For this reason there must be no absolute relocations being used in the code or data.

Being functionally related, add a function for adding a final kexec action.

---
 arch/x86/kexec.c          | 109 ++++++++++++++++++++++++++++++++++++++
 arch/x86/minios-x86.lds.S |   8 +++
 arch/x86/x86_hvm.S        |  46 ++++++++++++++++
 include/kexec.h           |  27 ++++++++++
 include/x86/os.h          |   5 ++
 kexec.c                   |  22 ++++++++
 6 files changed, 217 insertions(+)
 create mode 100644 arch/x86/kexec.c
arch/x86/kexec.c new file +109 -0
new file mode 100644
index 00000000..bf247797
@@ -0,0 +1,109 @@
+/******************************************************************************
+ * kexec.c
+ *
+ * Support of kexec (reboot locally into new mini-os kernel).
+ *
+ * Copyright (c) 2024, Juergen Gross, SUSE Linux GmbH
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifdef CONFIG_KEXEC
+
+#include <mini-os/os.h>
+#include <mini-os/lib.h>
+#include <mini-os/kexec.h>
+
+/*
+ * Final stage of kexec. Copies all data to the final destinations, zeroes
+ * .bss and activates new kernel.
+ * Must be called with interrupts off. Stack, code and data must be
+ * accessible via identity mapped virtual addresses (virt == phys). Copying
+ * and zeroing is done using virtual addresses.
+ * No relocations inside the function are allowed, as it is copied to an
+ * allocated page before being executed.
+ */
+static void __attribute__((__section__(".text.kexec")))
+    kexec_final(struct kexec_action *actions, unsigned long real)
+{
+    char *src, *dest;
+    unsigned int a, cnt;
+
+    for ( a = 0; ; a++ )
+    {
+        switch ( actions[a].action )
+        {
+        case KEXEC_COPY:
+            dest = actions[a].dest;
+            src = actions[a].src;
+            for ( cnt = 0; cnt < actions[a].len; cnt++ )
+                *dest++ = *src++;
+            break;
+
+        case KEXEC_ZERO:
+            dest = actions[a].dest;
+            for ( cnt = 0; cnt < actions[a].len; cnt++ )
+                *dest++ = 0;
+            break;
+
+        case KEXEC_CALL:
+            asm("movl %0, %%ebx\n\t"
+                "movl %1, %%edi\n\t"
+                "jmp *%2"
+                :"=m" (actions[a].src), "=m" (actions[a].dest)
+                :"m" (real));
+            break;
+        }
+    }
+}
+
+#define KEXEC_STACK_LONGS  8
+static unsigned long __attribute__((__section__(".data.kexec")))
+    kexec_stack[KEXEC_STACK_LONGS];
+
+static unsigned long get_kexec_addr(void *kexec_page, void *addr)
+{
+    unsigned long off = (unsigned long)addr - (unsigned long)_kexec_start;
+
+    return (unsigned long)kexec_page + off;
+}
+
+void do_kexec(void *kexec_page)
+{
+    unsigned long actions;
+    unsigned long stack;
+    unsigned long final;
+    unsigned long phys;
+
+    actions = get_kexec_addr(kexec_page, kexec_actions);
+    stack = get_kexec_addr(kexec_page, kexec_stack + KEXEC_STACK_LONGS);
+    final = get_kexec_addr(kexec_page, kexec_final);
+    phys = get_kexec_addr(kexec_page, kexec_phys);
+
+    memcpy(kexec_page, _kexec_start, KEXEC_SECSIZE);
+    asm("cli\n\t"
+        "mov %0, %%"ASM_SP"\n\t"
+        "mov %1, %%"ASM_ARG1"\n\t"
+        "mov %2, %%"ASM_ARG2"\n\t"
+        "jmp *%3"
+        :"=m" (stack), "=m" (actions), "=m" (phys)
+        :"m" (final));
+}
+
+#endif /* CONFIG_KEXEC */
arch/x86/minios-x86.lds.S +8 -0
index 8aae2fd6..83ec41ce 100644
@@ -87,6 +87,14 @@ SECTIONS
 
         _edata = .;			/* End of data section */
 
+        . = ALIGN(8);
+        _kexec_start = .;		/* Kexec relocatable code/data */
+        .kexec : {
+                *(.text.kexec)
+                *(.data.kexec)
+        }
+        _kexec_end = .;
+
         __bss_start = .;		/* BSS */
         .bss : {
                 *(.bss)
arch/x86/x86_hvm.S +46 -0

50 lines not on this page · raw message

include/kexec.h +27 -0
index 6fd96774..722be456 100644
@@ -1,7 +1,34 @@
 #ifndef _KEXEC_H
 #define _KEXEC_H
 
+/* One element of kexec actions (last element must have action KEXEC_CALL): */
+struct kexec_action {
+    enum {
+        KEXEC_COPY,   /* Copy len bytes from src to dest. */
+        KEXEC_ZERO,   /* Zero len bytes at dest. */
+        KEXEC_CALL    /* Call dest with paging turned off, param is src. */
+    } action;
+    unsigned int len;
+    void *dest;
+    void *src;
+};
+
+#define KEXEC_MAX_ACTIONS  16
+
+extern char _kexec_start[], _kexec_end[];
+extern struct kexec_action kexec_actions[KEXEC_MAX_ACTIONS];
+
+int kexec_add_action(int action, void *dest, void *src, unsigned int len);
+
+#define KEXEC_SECSIZE ((unsigned long)_kexec_end - (unsigned long)_kexec_start)
+
 int kexec(void *kernel, unsigned long kernel_size,
           const char *cmdline);
 
+/* Initiate final kexec stage. */
+void do_kexec(void *kexec_page);
+
+/* Assembler code for switching off paging and passing execution to new OS. */
+void kexec_phys(void);
+
 #endif /* _KEXEC_H */
include/x86/os.h +5 -0
index 0095be13..8a057d81 100644
@@ -27,6 +27,7 @@
 #define MSR_EFER          0xc0000080
 #define _EFER_LME         8             /* Long mode enable */
 
+#define X86_CR0_PE        0x00000001    /* Protected mode enable */
 #define X86_CR0_WP        0x00010000    /* Write protect */
 #define X86_CR0_PG        0x80000000    /* Paging */
 #define X86_CR4_PAE       0x00000020    /* enable physical address extensions */
@@ -64,9 +65,13 @@
 #if defined(__i386__)
 #define __SZ    "l"
 #define __REG   "e"
+#define ASM_ARG1 "eax"
+#define ASM_ARG2 "edx"
 #else
 #define __SZ    "q"
 #define __REG   "r"
+#define ASM_ARG1 "rdi"
+#define ASM_ARG2 "rsi"
 #endif
 
 #define ASM_SP  __REG"sp"
kexec.c +22 -0
index 53528169..849a98e4 100644
@@ -60,3 +60,25 @@ int kexec(void *kernel, unsigned long kernel_size,
     return ENOSYS;
 }
 EXPORT_SYMBOL(kexec);
+
+struct kexec_action __attribute__((__section__(".data.kexec")))
+    kexec_actions[KEXEC_MAX_ACTIONS];
+static unsigned int act_idx;
+
+int kexec_add_action(int action, void *dest, void *src, unsigned int len)
+{
+    struct kexec_action *act;
+
+    if ( act_idx == KEXEC_MAX_ACTIONS )
+        return -ENOSPC;
+
+    act = kexec_actions + act_idx;
+    act_idx++;
+
+    act->action = action;
+    act->len = len;
+    act->dest = dest;
+    act->src = src;
+
+    return 0;
+}
Signature · 1 line
2.43.0
#5 lore raw

Re: [MINI-OS PATCH 02/12] kexec: add final kexec stage

Quoted 15 lines · Juergen Gross

Add the code and data definitions of the final kexec stage.

Put the code and related data into a dedicated section in order to be able to copy it to another location. For this reason there must be no absolute relocations being used in the code or data.

Being functionally related, add a function for adding a final kexec action.

arch/x86/kexec.c line 40

Quoted 10 lines
arch/x86/kexec.c new file +9 -0
@@ -0,0 +1,109 @@
 
+
+/*
+ * Final stage of kexec. Copies all data to the final destinations, zeroes
+ * .bss and activates new kernel.
+ * Must be called with interrupts off. Stack, code and data must be
+ * accessible via identity mapped virtual addresses (virt == phys). Copying
+ * and zeroing is done using virtual addresses.
+ * No relocations inside the function are allowed, as it is copied to an
+ * allocated page before being executed.

"page" is stated here. Do we need an ASSERT later?

Quoted 11 lines

+ */

arch/x86/kexec.c new file +9 -0
+void do_kexec(void *kexec_page)
+{
+    unsigned long actions;
+    unsigned long stack;
+    unsigned long final;
+    unsigned long phys;
+
+    actions = get_kexec_addr(kexec_page, kexec_actions);
+    stack = get_kexec_addr(kexec_page, kexec_stack + KEXEC_STACK_LONGS);

arch/x86/kexec.c line 105

Quoted 10 lines
arch/x86/kexec.c new file +10 -0
+    final = get_kexec_addr(kexec_page, kexec_final);
+    phys = get_kexec_addr(kexec_page, kexec_phys);
+
+    memcpy(kexec_page, _kexec_start, KEXEC_SECSIZE);
+    asm("cli\n\t"
+        "mov %0, %%"ASM_SP"\n\t"
+        "mov %1, %%"ASM_ARG1"\n\t"
+        "mov %2, %%"ASM_ARG2"\n\t"
+        "jmp *%3"
+        :"=m" (stack), "=m" (actions), "=m" (phys)

Aren't these inputs and not outputs?

Quoted 11 lines
arch/x86/kexec.c new file +4 -0
+        :"m" (final));
+}
+
+#endif /* CONFIG_KEXEC */
include/kexec.h +0 -0
index 6fd96774..722be456 100644

include/kexec.h line 5

Quoted 5 lines
include/kexec.h +4 -0
@@ -1,7 +1,34 @@
 
+
+int kexec_add_action(int action, void *dest, void *src, unsigned int len);
+
+#define KEXEC_SECSIZE ((unsigned long)_kexec_end - (unsigned long)_kexec_start)

Add a build assertion here? Or maybe the correct amount is allocated and it doesn't matter.

Generally looks good.

Regards,
Jason

#6 lore raw

Re: [MINI-OS PATCH 02/12] kexec: add final kexec stage

Quoted 17 lines · Jason Andryuk
Quoted 15 lines · Juergen Gross

Add the code and data definitions of the final kexec stage.

Put the code and related data into a dedicated section in order to be able to copy it to another location. For this reason there must be no absolute relocations being used in the code or data.

Being functionally related, add a function for adding a final kexec action.

arch/x86/kexec.c new file +0 -0
@@ -0,0 +1,109 @@
Quoted 11 lines

arch/x86/kexec.c line 40

Quoted 9 lines
arch/x86/kexec.c new file +9 -0
+
+/*
+ * Final stage of kexec. Copies all data to the final destinations, zeroes
+ * .bss and activates new kernel.
+ * Must be called with interrupts off. Stack, code and data must be
+ * accessible via identity mapped virtual addresses (virt == phys). Copying
+ * and zeroing is done using virtual addresses.
+ * No relocations inside the function are allowed, as it is copied to an
+ * allocated page before being executed.

"page" is stated here. Do we need an ASSERT later?

Good idea. I'll ad an ASSERT() to the linker script in order to catch such an issue at build time.

Quoted 16 lines
Quoted 16 lines
arch/x86/kexec.c new file +14 -0
+ */
 
+void do_kexec(void *kexec_page)
+{
+    unsigned long actions;
+    unsigned long stack;
+    unsigned long final;
+    unsigned long phys;
+
+    actions = get_kexec_addr(kexec_page, kexec_actions);
+    stack = get_kexec_addr(kexec_page, kexec_stack + KEXEC_STACK_LONGS);
+    final = get_kexec_addr(kexec_page, kexec_final);
+    phys = get_kexec_addr(kexec_page, kexec_phys);
+
+    memcpy(kexec_page, _kexec_start, KEXEC_SECSIZE);
Quoted 8 lines

arch/x86/kexec.c line 105

Quoted 6 lines
arch/x86/kexec.c new file +6 -0
+    asm("cli\n\t"
+        "mov %0, %%"ASM_SP"\n\t"
+        "mov %1, %%"ASM_ARG1"\n\t"
+        "mov %2, %%"ASM_ARG2"\n\t"
+        "jmp *%3"
+        :"=m" (stack), "=m" (actions), "=m" (phys)

Aren't these inputs and not outputs?

Oh, of course they are.

Quoted 21 lines
Quoted 12 lines
arch/x86/kexec.c new file +4 -0
+        :"m" (final));
+}
+
+#endif /* CONFIG_KEXEC */
include/kexec.h +0 -0
index 6fd96774..722be456 100644

include/kexec.h line 5

Quoted 5 lines
include/kexec.h +4 -0
@@ -1,7 +1,34 @@
 
+
+int kexec_add_action(int action, void *dest, void *src, unsigned int len);
+
+#define KEXEC_SECSIZE ((unsigned long)_kexec_end - (unsigned long)_kexec_start)

Add a build assertion here? Or maybe the correct amount is allocated and it doesn't matter.

Quoted 1 line

Generally looks good.

Thanks,

Juergen

#7 lore raw

PATCH 3/12 add elf.h

In reply to Juergen Gross

Add some definitions for accessing an ELF file. Only the file header and the program header are needed.

The main source for those are elfstructs.h and libelf.h from the Xen tree. The license boiler plate of those files is being kept in the resulting header file.

---
 include/elf.h | 340 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 340 insertions(+)
 create mode 100644 include/elf.h
include/elf.h new file +340 -0
new file mode 100644
index 00000000..35a9c9fe
@@ -0,0 +1,340 @@
+#ifndef __ELF_H__
+#define __ELF_H__
+/*
+ * Copyright (c) 1995, 1996 Erik Theisen.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdbool.h>
+#include <mini-os/types.h>
+
+typedef uint32_t    Elf32_Addr;  /* Unsigned program address */
+typedef uint32_t    Elf32_Off;   /* Unsigned file offset */
+typedef uint16_t    Elf32_Half;  /* Unsigned medium integer */
+typedef uint32_t    Elf32_Word;  /* Unsigned large integer */
+
+typedef uint64_t    Elf64_Addr;
+typedef uint64_t    Elf64_Off;
+typedef uint16_t    Elf64_Half;
+typedef uint32_t    Elf64_Word;
+typedef uint64_t    Elf64_Xword;
+
+/* Unique build id string format when using --build-id. */
+#define NT_GNU_BUILD_ID 3
+
+/*
+ * e_ident[] identification indexes
+ * See http://www.caldera.com/developers/gabi/2000-07-17/ch4.eheader.html
+ */
+#define EI_MAG0        0         /* file ID */
+#define EI_MAG1        1         /* file ID */
+#define EI_MAG2        2         /* file ID */
+#define EI_MAG3        3         /* file ID */
+#define EI_CLASS       4         /* file class */
+#define EI_DATA        5         /* data encoding */
+#define EI_VERSION     6         /* ELF header version */
+#define EI_OSABI       7         /* OS/ABI ID */
+#define EI_ABIVERSION  8         /* ABI version */
+#define EI_PAD         9         /* start of pad bytes */
+#define EI_NIDENT     16         /* Size of e_ident[] */
+
+/* e_ident[] magic number */
+#define ELFMAG0        0x7f      /* e_ident[EI_MAG0] */
+#define ELFMAG1        'E'       /* e_ident[EI_MAG1] */
+#define ELFMAG2        'L'       /* e_ident[EI_MAG2] */
+#define ELFMAG3        'F'       /* e_ident[EI_MAG3] */
+#define ELFMAG         "\177ELF" /* magic */
+#define SELFMAG        4         /* size of magic */
+
+/* e_ident[] file class */
+#define ELFCLASSNONE   0         /* invalid */
+#define ELFCLASS32     1         /* 32-bit objs */
+#define ELFCLASS64     2         /* 64-bit objs */
+#define ELFCLASSNUM    3         /* number of classes */
+
+/* e_ident[] data encoding */
+#define ELFDATANONE    0         /* invalid */
+#define ELFDATA2LSB    1         /* Little-Endian */
+#define ELFDATA2MSB    2         /* Big-Endian */
+#define ELFDATANUM     3         /* number of data encode defines */
+
+/* e_ident[] Operating System/ABI */
+#define ELFOSABI_SYSV         0  /* UNIX System V ABI */
+#define ELFOSABI_NONE         0  /* Same as ELFOSABI_SYSV */
+#define ELFOSABI_HPUX         1  /* HP-UX operating system */
+#define ELFOSABI_NETBSD       2  /* NetBSD */
+#define ELFOSABI_LINUX        3  /* GNU/Linux */
+#define ELFOSABI_HURD         4  /* GNU/Hurd */
+#define ELFOSABI_86OPEN       5  /* 86Open common IA32 ABI */
+#define ELFOSABI_SOLARIS      6  /* Solaris */
+#define ELFOSABI_MONTEREY     7  /* Monterey */
+#define ELFOSABI_IRIX         8  /* IRIX */
+#define ELFOSABI_FREEBSD      9  /* FreeBSD */
+#define ELFOSABI_TRU64       10  /* TRU64 UNIX */
+#define ELFOSABI_MODESTO     11  /* Novell Modesto */
+#define ELFOSABI_OPENBSD     12  /* OpenBSD */
+#define ELFOSABI_ARM         97  /* ARM */
+#define ELFOSABI_STANDALONE 255  /* Standalone (embedded) application */
+
+/* e_ident */
+#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
+                      (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
+                      (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
+                      (ehdr).e_ident[EI_MAG3] == ELFMAG3)
+
+/* e_flags */
+#define EF_ARM_EABI_MASK    0xff000000
+#define EF_ARM_EABI_UNKNOWN 0x00000000
+#define EF_ARM_EABI_VER1    0x01000000
+#define EF_ARM_EABI_VER2    0x02000000
+#define EF_ARM_EABI_VER3    0x03000000
+#define EF_ARM_EABI_VER4    0x04000000
+#define EF_ARM_EABI_VER5    0x05000000
+
+/* ELF Header */
+typedef struct {
+    unsigned char e_ident[EI_NIDENT]; /* ELF Identification */
+    Elf32_Half    e_type;        /* object file type */
+    Elf32_Half    e_machine;     /* machine */
+    Elf32_Word    e_version;     /* object file version */
+    Elf32_Addr    e_entry;       /* virtual entry point */
+    Elf32_Off     e_phoff;       /* program header table offset */
+    Elf32_Off     e_shoff;       /* section header table offset */
+    Elf32_Word    e_flags;       /* processor-specific flags */
+    Elf32_Half    e_ehsize;      /* ELF header size */
+    Elf32_Half    e_phentsize;   /* program header entry size */
+    Elf32_Half    e_phnum;       /* number of program header entries */
+    Elf32_Half    e_shentsize;   /* section header entry size */
+    Elf32_Half    e_shnum;       /* number of section header entries */
+    Elf32_Half    e_shstrndx;    /* section header table's "section
+                                    header string table" entry offset */
+} Elf32_Ehdr;
+
+typedef struct {
+    unsigned char e_ident[EI_NIDENT]; /* Id bytes */
+    Elf64_Half    e_type;        /* file type */
+    Elf64_Half    e_machine;     /* machine type */
+    Elf64_Word    e_version;     /* version number */
+    Elf64_Addr    e_entry;       /* entry point */
+    Elf64_Off     e_phoff;       /* Program hdr offset */
+    Elf64_Off     e_shoff;       /* Section hdr offset */
+    Elf64_Word    e_flags;       /* Processor flags */
+    Elf64_Half    e_ehsize;      /* sizeof ehdr */
+    Elf64_Half    e_phentsize;   /* Program header entry size */
+    Elf64_Half    e_phnum;       /* Number of program headers */
+    Elf64_Half    e_shentsize;   /* Section header entry size */
+    Elf64_Half    e_shnum;       /* Number of section headers */
+    Elf64_Half    e_shstrndx;    /* String table index */
+} Elf64_Ehdr;
+
+/* e_type */
+#define ET_NONE      0           /* No file type */
+#define ET_REL       1           /* relocatable file */
+#define ET_EXEC      2           /* executable file */
+#define ET_DYN       3           /* shared object file */
+#define ET_CORE      4           /* core file */
+#define ET_NUM       5           /* number of types */
+#define ET_LOPROC    0xff00      /* reserved range for processor */
+#define ET_HIPROC    0xffff      /*   specific e_type */
+
+/* e_machine */
+#define EM_NONE      0           /* No Machine */
+#define EM_M32       1           /* AT&T WE 32100 */
+#define EM_SPARC     2           /* SPARC */
+#define EM_386       3           /* Intel 80386 */
+#define EM_68K       4           /* Motorola 68000 */
+#define EM_88K       5           /* Motorola 88000 */
+#define EM_486       6           /* Intel 80486 - unused? */
+#define EM_860       7           /* Intel 80860 */
+#define EM_MIPS      8           /* MIPS R3000 Big-Endian only */
+/*
+ * Don't know if EM_MIPS_RS4_BE,
+ * EM_SPARC64, EM_PARISC,
+ * or EM_PPC are ABI compliant
+ */
+#define EM_MIPS_RS4_BE 10        /* MIPS R4000 Big-Endian */
+#define EM_SPARC64     11        /* SPARC v9 64-bit unoffical */
+#define EM_PARISC      15        /* HPPA */
+#define EM_SPARC32PLUS 18        /* Enhanced instruction set SPARC */
+#define EM_PPC         20        /* PowerPC */
+#define EM_PPC64       21        /* PowerPC 64-bit */
+#define EM_ARM         40        /* Advanced RISC Machines ARM */
+#define EM_ALPHA       41        /* DEC ALPHA */
+#define EM_SPARCV9     43        /* SPARC version 9 */
+#define EM_ALPHA_EXP   0x9026    /* DEC ALPHA */
+#define EM_IA_64       50        /* Intel Merced */
+#define EM_X86_64      62        /* AMD x86-64 architecture */
+#define EM_VAX         75        /* DEC VAX */
+#define EM_AARCH64    183        /* ARM 64-bit */
+
+/* Version */
+#define EV_NONE      0           /* Invalid */
+#define EV_CURRENT   1           /* Current */
+#define EV_NUM       2           /* number of versions */
+
+/* Program Header */
+typedef struct {
+    Elf32_Word    p_type;        /* segment type */
+    Elf32_Off     p_offset;      /* segment offset */
+    Elf32_Addr    p_vaddr;       /* virtual address of segment */
+    Elf32_Addr    p_paddr;       /* physical address - ignored? */
+    Elf32_Word    p_filesz;      /* number of bytes in file for seg. */
+    Elf32_Word    p_memsz;       /* number of bytes in mem. for seg. */
+    Elf32_Word    p_flags;       /* flags */
+    Elf32_Word    p_align;       /* memory alignment */
+} Elf32_Phdr;
+
+typedef struct {
+    Elf64_Word    p_type;        /* entry type */
+    Elf64_Word    p_flags;       /* flags */
+    Elf64_Off     p_offset;      /* offset */
+    Elf64_Addr    p_vaddr;       /* virtual address */
+    Elf64_Addr    p_paddr;       /* physical address */
+    Elf64_Xword   p_filesz;      /* file size */
+    Elf64_Xword   p_memsz;       /* memory size */
+    Elf64_Xword   p_align;       /* memory & file alignment */
+} Elf64_Phdr;
+
+/* Segment types - p_type */
+#define PT_NULL      0           /* unused */
+#define PT_LOAD      1           /* loadable segment */
+#define PT_DYNAMIC   2           /* dynamic linking section */
+#define PT_INTERP    3           /* the RTLD */
+#define PT_NOTE      4           /* auxiliary information */
+#define PT_SHLIB     5           /* reserved - purpose undefined */
+#define PT_PHDR      6           /* program header */
+#define PT_NUM       7           /* Number of segment types */
+#define PT_LOPROC    0x70000000  /* reserved range for processor */
+#define PT_HIPROC    0x7fffffff  /*  specific segment types */
+
+/* Segment flags - p_flags */
+#define PF_X         0x1        /* Executable */
+#define PF_W         0x2        /* Writable */
+#define PF_R         0x4        /* Readable */
+#define PF_MASKPROC  0xf0000000 /* reserved bits for processor */
+                                /*  specific segment flags */
+
+/* Section Header */
+typedef struct {
+    Elf32_Word    sh_name;      /* name - index into section header
+                                   string table section */
+    Elf32_Word    sh_type;      /* type */
+    Elf32_Word    sh_flags;     /* flags */
+    Elf32_Addr    sh_addr;      /* address */
+    Elf32_Off     sh_offset;    /* file offset */
+    Elf32_Word    sh_size;      /* section size */
+    Elf32_Word    sh_link;      /* section header table index link */
+    Elf32_Word    sh_info;      /* extra information */
+    Elf32_Word    sh_addralign; /* address alignment */
+    Elf32_Word    sh_entsize;   /* section entry size */
+} Elf32_Shdr;
+
+typedef struct {
+    Elf64_Word    sh_name;      /* section name */
+    Elf64_Word    sh_type;      /* section type */
+    Elf64_Xword   sh_flags;     /* section flags */
+    Elf64_Addr    sh_addr;      /* virtual address */
+    Elf64_Off     sh_offset;    /* file offset */
+    Elf64_Xword   sh_size;      /* section size */
+    Elf64_Word    sh_link;      /* link to another */
+    Elf64_Word    sh_info;      /* misc info */
+    Elf64_Xword   sh_addralign; /* memory alignment */
+    Elf64_Xword   sh_entsize;   /* table entry size */
+} Elf64_Shdr;
+
+/* sh_type */
+#define SHT_NULL        0       /* inactive */
+#define SHT_PROGBITS    1       /* program defined information */
+#define SHT_SYMTAB      2       /* symbol table section */
+#define SHT_STRTAB      3       /* string table section */
+#define SHT_RELA        4       /* relocation section with addends*/
+#define SHT_HASH        5       /* symbol hash table section */
+#define SHT_DYNAMIC     6       /* dynamic section */
+#define SHT_NOTE        7       /* note section */
+#define SHT_NOBITS      8       /* no space section */
+#define SHT_REL         9       /* relation section without addends */
+#define SHT_SHLIB      10       /* reserved - purpose unknown */
+#define SHT_DYNSYM     11       /* dynamic symbol table section */
+#define SHT_NUM        12       /* number of section types */
+
+/* Note definitions */
+typedef struct {
+    Elf32_Word namesz;
+    Elf32_Word descsz;
+    Elf32_Word type;
+    char data[];
+} Elf32_Note;
+
+typedef struct {
+    Elf64_Word namesz;
+    Elf64_Word descsz;
+    Elf64_Word type;
+    char data[];
+} Elf64_Note;
+
+/* Abstraction layer for handling 32- and 64-bit ELF files. */
+
+typedef union {
+    Elf32_Ehdr e32;
+    Elf64_Ehdr e64;
+} elf_ehdr;
+
+static inline bool elf_is_32bit(elf_ehdr *ehdr)
+{
+    return ehdr->e32.e_ident[EI_CLASS] == ELFCLASS32;
+}
+
+static inline bool elf_is_64bit(elf_ehdr *ehdr)
+{
+    return ehdr->e32.e_ident[EI_CLASS] == ELFCLASS64;
+}
+
+#define ehdr_val(ehdr, elem) (elf_is_32bit(ehdr) ? (ehdr)->e32.elem : (ehdr)->e64.elem)
+
+typedef union {
+    Elf32_Phdr e32;
+    Elf64_Phdr e64;
+} elf_phdr;
+
+#define phdr_val(ehdr, phdr, elem) (elf_is_32bit(ehdr) ? (phdr)->e32.elem : (phdr)->e64.elem)
+
+typedef union {
+    Elf32_Shdr e32;
+    Elf64_Shdr e64;
+} elf_shdr;
+
+#define shdr_val(ehdr, shdr, elem) (elf_is_32bit(ehdr) ? (shdr)->e32.elem : (shdr)->e64.elem)
+
+typedef union {
+    Elf32_Note e32;
+    Elf64_Note e64;
+} elf_note;
+
+#define note_val(ehdr, note, elem) (elf_is_32bit(ehdr) ? (note)->e32.elem : (note)->e64.elem)
+
+static inline void *elf_ptr_add(void *ptr, unsigned long add)
+{
+    return (char *)ptr + add;
+}
+#endif /* __ELF_H__ */
Signature · 1 line
2.43.0
Reviewed-by on v1 3/12
#8 lore raw

Re: [MINI-OS PATCH 03/12] add elf.h

Quoted 6 lines · Juergen Gross

Add some definitions for accessing an ELF file. Only the file header and the program header are needed.

The main source for those are elfstructs.h and libelf.h from the Xen tree. The license boiler plate of those files is being kept in the resulting header file.

Maybe the copying was a bit too literal.

Quoted 91 lines
include/elf.h new file +88 -0
@@ -0,0 +1,340 @@
+#ifndef __ELF_H__
+#define __ELF_H__
+/*
+ * Copyright (c) 1995, 1996 Erik Theisen.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdbool.h>
+#include <mini-os/types.h>
+
+typedef uint32_t    Elf32_Addr;  /* Unsigned program address */
+typedef uint32_t    Elf32_Off;   /* Unsigned file offset */
+typedef uint16_t    Elf32_Half;  /* Unsigned medium integer */
+typedef uint32_t    Elf32_Word;  /* Unsigned large integer */
+
+typedef uint64_t    Elf64_Addr;
+typedef uint64_t    Elf64_Off;
+typedef uint16_t    Elf64_Half;
+typedef uint32_t    Elf64_Word;
+typedef uint64_t    Elf64_Xword;
+
+/* Unique build id string format when using --build-id. */
+#define NT_GNU_BUILD_ID 3
+
+/*
+ * e_ident[] identification indexes
+ * See http://www.caldera.com/developers/gabi/2000-07-17/ch4.eheader.html
+ */
+#define EI_MAG0        0         /* file ID */
+#define EI_MAG1        1         /* file ID */
+#define EI_MAG2        2         /* file ID */
+#define EI_MAG3        3         /* file ID */
+#define EI_CLASS       4         /* file class */
+#define EI_DATA        5         /* data encoding */
+#define EI_VERSION     6         /* ELF header version */
+#define EI_OSABI       7         /* OS/ABI ID */
+#define EI_ABIVERSION  8         /* ABI version */
+#define EI_PAD         9         /* start of pad bytes */
+#define EI_NIDENT     16         /* Size of e_ident[] */
+
+/* e_ident[] magic number */
+#define ELFMAG0        0x7f      /* e_ident[EI_MAG0] */
+#define ELFMAG1        'E'       /* e_ident[EI_MAG1] */
+#define ELFMAG2        'L'       /* e_ident[EI_MAG2] */
+#define ELFMAG3        'F'       /* e_ident[EI_MAG3] */
+#define ELFMAG         "\177ELF" /* magic */
+#define SELFMAG        4         /* size of magic */
+
+/* e_ident[] file class */
+#define ELFCLASSNONE   0         /* invalid */
+#define ELFCLASS32     1         /* 32-bit objs */
+#define ELFCLASS64     2         /* 64-bit objs */
+#define ELFCLASSNUM    3         /* number of classes */
+
+/* e_ident[] data encoding */
+#define ELFDATANONE    0         /* invalid */
+#define ELFDATA2LSB    1         /* Little-Endian */
+#define ELFDATA2MSB    2         /* Big-Endian */
+#define ELFDATANUM     3         /* number of data encode defines */
+
+/* e_ident[] Operating System/ABI */
+#define ELFOSABI_SYSV         0  /* UNIX System V ABI */
+#define ELFOSABI_NONE         0  /* Same as ELFOSABI_SYSV */
+#define ELFOSABI_HPUX         1  /* HP-UX operating system */
+#define ELFOSABI_NETBSD       2  /* NetBSD */
+#define ELFOSABI_LINUX        3  /* GNU/Linux */
+#define ELFOSABI_HURD         4  /* GNU/Hurd */

include/elf.h line 98

Quoted 10 lines
include/elf.h new file +10 -0
+#define ELFOSABI_86OPEN       5  /* 86Open common IA32 ABI */
+#define ELFOSABI_SOLARIS      6  /* Solaris */
+#define ELFOSABI_MONTEREY     7  /* Monterey */
+#define ELFOSABI_IRIX         8  /* IRIX */
+#define ELFOSABI_FREEBSD      9  /* FreeBSD */
+#define ELFOSABI_TRU64       10  /* TRU64 UNIX */
+#define ELFOSABI_MODESTO     11  /* Novell Modesto */
+#define ELFOSABI_OPENBSD     12  /* OpenBSD */
+#define ELFOSABI_ARM         97  /* ARM */
+#define ELFOSABI_STANDALONE 255  /* Standalone (embedded) application */

While I'm happy to see Modesto mentioned in yet another places, I don't think you need the majority of these?

Quoted 80 lines
include/elf.h new file +80 -0
+/* e_ident */
+#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
+                      (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
+                      (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
+                      (ehdr).e_ident[EI_MAG3] == ELFMAG3)
+
+/* e_flags */
+#define EF_ARM_EABI_MASK    0xff000000
+#define EF_ARM_EABI_UNKNOWN 0x00000000
+#define EF_ARM_EABI_VER1    0x01000000
+#define EF_ARM_EABI_VER2    0x02000000
+#define EF_ARM_EABI_VER3    0x03000000
+#define EF_ARM_EABI_VER4    0x04000000
+#define EF_ARM_EABI_VER5    0x05000000
+
+/* ELF Header */
+typedef struct {
+    unsigned char e_ident[EI_NIDENT]; /* ELF Identification */
+    Elf32_Half    e_type;        /* object file type */
+    Elf32_Half    e_machine;     /* machine */
+    Elf32_Word    e_version;     /* object file version */
+    Elf32_Addr    e_entry;       /* virtual entry point */
+    Elf32_Off     e_phoff;       /* program header table offset */
+    Elf32_Off     e_shoff;       /* section header table offset */
+    Elf32_Word    e_flags;       /* processor-specific flags */
+    Elf32_Half    e_ehsize;      /* ELF header size */
+    Elf32_Half    e_phentsize;   /* program header entry size */
+    Elf32_Half    e_phnum;       /* number of program header entries */
+    Elf32_Half    e_shentsize;   /* section header entry size */
+    Elf32_Half    e_shnum;       /* number of section header entries */
+    Elf32_Half    e_shstrndx;    /* section header table's "section
+                                    header string table" entry offset */
+} Elf32_Ehdr;
+
+typedef struct {
+    unsigned char e_ident[EI_NIDENT]; /* Id bytes */
+    Elf64_Half    e_type;        /* file type */
+    Elf64_Half    e_machine;     /* machine type */
+    Elf64_Word    e_version;     /* version number */
+    Elf64_Addr    e_entry;       /* entry point */
+    Elf64_Off     e_phoff;       /* Program hdr offset */
+    Elf64_Off     e_shoff;       /* Section hdr offset */
+    Elf64_Word    e_flags;       /* Processor flags */
+    Elf64_Half    e_ehsize;      /* sizeof ehdr */
+    Elf64_Half    e_phentsize;   /* Program header entry size */
+    Elf64_Half    e_phnum;       /* Number of program headers */
+    Elf64_Half    e_shentsize;   /* Section header entry size */
+    Elf64_Half    e_shnum;       /* Number of section headers */
+    Elf64_Half    e_shstrndx;    /* String table index */
+} Elf64_Ehdr;
+
+/* e_type */
+#define ET_NONE      0           /* No file type */
+#define ET_REL       1           /* relocatable file */
+#define ET_EXEC      2           /* executable file */
+#define ET_DYN       3           /* shared object file */
+#define ET_CORE      4           /* core file */
+#define ET_NUM       5           /* number of types */
+#define ET_LOPROC    0xff00      /* reserved range for processor */
+#define ET_HIPROC    0xffff      /*   specific e_type */
+
+/* e_machine */
+#define EM_NONE      0           /* No Machine */
+#define EM_M32       1           /* AT&T WE 32100 */
+#define EM_SPARC     2           /* SPARC */
+#define EM_386       3           /* Intel 80386 */
+#define EM_68K       4           /* Motorola 68000 */
+#define EM_88K       5           /* Motorola 88000 */
+#define EM_486       6           /* Intel 80486 - unused? */
+#define EM_860       7           /* Intel 80860 */
+#define EM_MIPS      8           /* MIPS R3000 Big-Endian only */
+/*
+ * Don't know if EM_MIPS_RS4_BE,
+ * EM_SPARC64, EM_PARISC,
+ * or EM_PPC are ABI compliant
+ */
+#define EM_MIPS_RS4_BE 10        /* MIPS R4000 Big-Endian */
+#define EM_SPARC64     11        /* SPARC v9 64-bit unoffical */
+#define EM_PARISC      15        /* HPPA */
+#define EM_SPARC32PLUS 18        /* Enhanced instruction set SPARC */

include/elf.h line 189

Quoted 10 lines
include/elf.h new file +10 -0
+#define EM_PPC         20        /* PowerPC */
+#define EM_PPC64       21        /* PowerPC 64-bit */
+#define EM_ARM         40        /* Advanced RISC Machines ARM */
+#define EM_ALPHA       41        /* DEC ALPHA */
+#define EM_SPARCV9     43        /* SPARC version 9 */
+#define EM_ALPHA_EXP   0x9026    /* DEC ALPHA */
+#define EM_IA_64       50        /* Intel Merced */
+#define EM_X86_64      62        /* AMD x86-64 architecture */
+#define EM_VAX         75        /* DEC VAX */
+#define EM_AARCH64    183        /* ARM 64-bit */

Here I similarly think some stripping down might help. Doing so would then also permit to leave out the comment in the middle.

Quoted 138 lines
include/elf.h new file +138 -0
+/* Version */
+#define EV_NONE      0           /* Invalid */
+#define EV_CURRENT   1           /* Current */
+#define EV_NUM       2           /* number of versions */
+
+/* Program Header */
+typedef struct {
+    Elf32_Word    p_type;        /* segment type */
+    Elf32_Off     p_offset;      /* segment offset */
+    Elf32_Addr    p_vaddr;       /* virtual address of segment */
+    Elf32_Addr    p_paddr;       /* physical address - ignored? */
+    Elf32_Word    p_filesz;      /* number of bytes in file for seg. */
+    Elf32_Word    p_memsz;       /* number of bytes in mem. for seg. */
+    Elf32_Word    p_flags;       /* flags */
+    Elf32_Word    p_align;       /* memory alignment */
+} Elf32_Phdr;
+
+typedef struct {
+    Elf64_Word    p_type;        /* entry type */
+    Elf64_Word    p_flags;       /* flags */
+    Elf64_Off     p_offset;      /* offset */
+    Elf64_Addr    p_vaddr;       /* virtual address */
+    Elf64_Addr    p_paddr;       /* physical address */
+    Elf64_Xword   p_filesz;      /* file size */
+    Elf64_Xword   p_memsz;       /* memory size */
+    Elf64_Xword   p_align;       /* memory & file alignment */
+} Elf64_Phdr;
+
+/* Segment types - p_type */
+#define PT_NULL      0           /* unused */
+#define PT_LOAD      1           /* loadable segment */
+#define PT_DYNAMIC   2           /* dynamic linking section */
+#define PT_INTERP    3           /* the RTLD */
+#define PT_NOTE      4           /* auxiliary information */
+#define PT_SHLIB     5           /* reserved - purpose undefined */
+#define PT_PHDR      6           /* program header */
+#define PT_NUM       7           /* Number of segment types */
+#define PT_LOPROC    0x70000000  /* reserved range for processor */
+#define PT_HIPROC    0x7fffffff  /*  specific segment types */
+
+/* Segment flags - p_flags */
+#define PF_X         0x1        /* Executable */
+#define PF_W         0x2        /* Writable */
+#define PF_R         0x4        /* Readable */
+#define PF_MASKPROC  0xf0000000 /* reserved bits for processor */
+                                /*  specific segment flags */
+
+/* Section Header */
+typedef struct {
+    Elf32_Word    sh_name;      /* name - index into section header
+                                   string table section */
+    Elf32_Word    sh_type;      /* type */
+    Elf32_Word    sh_flags;     /* flags */
+    Elf32_Addr    sh_addr;      /* address */
+    Elf32_Off     sh_offset;    /* file offset */
+    Elf32_Word    sh_size;      /* section size */
+    Elf32_Word    sh_link;      /* section header table index link */
+    Elf32_Word    sh_info;      /* extra information */
+    Elf32_Word    sh_addralign; /* address alignment */
+    Elf32_Word    sh_entsize;   /* section entry size */
+} Elf32_Shdr;
+
+typedef struct {
+    Elf64_Word    sh_name;      /* section name */
+    Elf64_Word    sh_type;      /* section type */
+    Elf64_Xword   sh_flags;     /* section flags */
+    Elf64_Addr    sh_addr;      /* virtual address */
+    Elf64_Off     sh_offset;    /* file offset */
+    Elf64_Xword   sh_size;      /* section size */
+    Elf64_Word    sh_link;      /* link to another */
+    Elf64_Word    sh_info;      /* misc info */
+    Elf64_Xword   sh_addralign; /* memory alignment */
+    Elf64_Xword   sh_entsize;   /* table entry size */
+} Elf64_Shdr;
+
+/* sh_type */
+#define SHT_NULL        0       /* inactive */
+#define SHT_PROGBITS    1       /* program defined information */
+#define SHT_SYMTAB      2       /* symbol table section */
+#define SHT_STRTAB      3       /* string table section */
+#define SHT_RELA        4       /* relocation section with addends*/
+#define SHT_HASH        5       /* symbol hash table section */
+#define SHT_DYNAMIC     6       /* dynamic section */
+#define SHT_NOTE        7       /* note section */
+#define SHT_NOBITS      8       /* no space section */
+#define SHT_REL         9       /* relation section without addends */
+#define SHT_SHLIB      10       /* reserved - purpose unknown */
+#define SHT_DYNSYM     11       /* dynamic symbol table section */
+#define SHT_NUM        12       /* number of section types */
+
+/* Note definitions */
+typedef struct {
+    Elf32_Word namesz;
+    Elf32_Word descsz;
+    Elf32_Word type;
+    char data[];
+} Elf32_Note;
+
+typedef struct {
+    Elf64_Word namesz;
+    Elf64_Word descsz;
+    Elf64_Word type;
+    char data[];
+} Elf64_Note;
+
+/* Abstraction layer for handling 32- and 64-bit ELF files. */
+
+typedef union {
+    Elf32_Ehdr e32;
+    Elf64_Ehdr e64;
+} elf_ehdr;
+
+static inline bool elf_is_32bit(elf_ehdr *ehdr)
+{
+    return ehdr->e32.e_ident[EI_CLASS] == ELFCLASS32;
+}
+
+static inline bool elf_is_64bit(elf_ehdr *ehdr)
+{
+    return ehdr->e32.e_ident[EI_CLASS] == ELFCLASS64;
+}
+
+#define ehdr_val(ehdr, elem) (elf_is_32bit(ehdr) ? (ehdr)->e32.elem : (ehdr)->e64.elem)
+
+typedef union {
+    Elf32_Phdr e32;
+    Elf64_Phdr e64;
+} elf_phdr;
+
+#define phdr_val(ehdr, phdr, elem) (elf_is_32bit(ehdr) ? (phdr)->e32.elem : (phdr)->e64.elem)
+
+typedef union {
+    Elf32_Shdr e32;
+    Elf64_Shdr e64;
+} elf_shdr;
+
+#define shdr_val(ehdr, shdr, elem) (elf_is_32bit(ehdr) ? (shdr)->e32.elem : (shdr)->e64.elem)
+

include/elf.h line 338

Quoted 10 lines
include/elf.h new file +10 -0
+typedef union {
+    Elf32_Note e32;
+    Elf64_Note e64;
+} elf_note;
+
+#define note_val(ehdr, note, elem) (elf_is_32bit(ehdr) ? (note)->e32.elem : (note)->e64.elem)
+
+static inline void *elf_ptr_add(void *ptr, unsigned long add)
+{
+    return (char *)ptr + add;

You can omit the cast here, can't you?

You're the maintainer, so you'll know how many of the comments you want to address. Either way:

Jan

#9 lore raw

Re: [MINI-OS PATCH 03/12] add elf.h

Quoted 106 lines · Jan Beulich
Quoted 6 lines · Juergen Gross

Add some definitions for accessing an ELF file. Only the file header and the program header are needed.

The main source for those are elfstructs.h and libelf.h from the Xen tree. The license boiler plate of those files is being kept in the resulting header file.

Maybe the copying was a bit too literal.

Quoted 96 lines
include/elf.h new file +93 -0
@@ -0,0 +1,340 @@
+#ifndef __ELF_H__
+#define __ELF_H__
+/*
+ * Copyright (c) 1995, 1996 Erik Theisen.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdbool.h>
+#include <mini-os/types.h>
+
+typedef uint32_t    Elf32_Addr;  /* Unsigned program address */
+typedef uint32_t    Elf32_Off;   /* Unsigned file offset */
+typedef uint16_t    Elf32_Half;  /* Unsigned medium integer */
+typedef uint32_t    Elf32_Word;  /* Unsigned large integer */
+
+typedef uint64_t    Elf64_Addr;
+typedef uint64_t    Elf64_Off;
+typedef uint16_t    Elf64_Half;
+typedef uint32_t    Elf64_Word;
+typedef uint64_t    Elf64_Xword;
+
+/* Unique build id string format when using --build-id. */
+#define NT_GNU_BUILD_ID 3
+
+/*
+ * e_ident[] identification indexes
+ * See http://www.caldera.com/developers/gabi/2000-07-17/ch4.eheader.html
+ */
+#define EI_MAG0        0         /* file ID */
+#define EI_MAG1        1         /* file ID */
+#define EI_MAG2        2         /* file ID */
+#define EI_MAG3        3         /* file ID */
+#define EI_CLASS       4         /* file class */
+#define EI_DATA        5         /* data encoding */
+#define EI_VERSION     6         /* ELF header version */
+#define EI_OSABI       7         /* OS/ABI ID */
+#define EI_ABIVERSION  8         /* ABI version */
+#define EI_PAD         9         /* start of pad bytes */
+#define EI_NIDENT     16         /* Size of e_ident[] */
+
+/* e_ident[] magic number */
+#define ELFMAG0        0x7f      /* e_ident[EI_MAG0] */
+#define ELFMAG1        'E'       /* e_ident[EI_MAG1] */
+#define ELFMAG2        'L'       /* e_ident[EI_MAG2] */
+#define ELFMAG3        'F'       /* e_ident[EI_MAG3] */
+#define ELFMAG         "\177ELF" /* magic */
+#define SELFMAG        4         /* size of magic */
+
+/* e_ident[] file class */
+#define ELFCLASSNONE   0         /* invalid */
+#define ELFCLASS32     1         /* 32-bit objs */
+#define ELFCLASS64     2         /* 64-bit objs */
+#define ELFCLASSNUM    3         /* number of classes */
+
+/* e_ident[] data encoding */
+#define ELFDATANONE    0         /* invalid */
+#define ELFDATA2LSB    1         /* Little-Endian */
+#define ELFDATA2MSB    2         /* Big-Endian */
+#define ELFDATANUM     3         /* number of data encode defines */
+
+/* e_ident[] Operating System/ABI */
+#define ELFOSABI_SYSV         0  /* UNIX System V ABI */
+#define ELFOSABI_NONE         0  /* Same as ELFOSABI_SYSV */
+#define ELFOSABI_HPUX         1  /* HP-UX operating system */
+#define ELFOSABI_NETBSD       2  /* NetBSD */
+#define ELFOSABI_LINUX        3  /* GNU/Linux */
+#define ELFOSABI_HURD         4  /* GNU/Hurd */
+#define ELFOSABI_86OPEN       5  /* 86Open common IA32 ABI */
+#define ELFOSABI_SOLARIS      6  /* Solaris */
+#define ELFOSABI_MONTEREY     7  /* Monterey */
+#define ELFOSABI_IRIX         8  /* IRIX */
+#define ELFOSABI_FREEBSD      9  /* FreeBSD */
Quoted 8 lines

include/elf.h line 98

Quoted 5 lines
include/elf.h new file +5 -0
+#define ELFOSABI_TRU64       10  /* TRU64 UNIX */
+#define ELFOSABI_MODESTO     11  /* Novell Modesto */
+#define ELFOSABI_OPENBSD     12  /* OpenBSD */
+#define ELFOSABI_ARM         97  /* ARM */
+#define ELFOSABI_STANDALONE 255  /* Standalone (embedded) application */

While I'm happy to see Modesto mentioned in yet another places, I don't think you need the majority of these?

Hmm, true. In the end I don't need any of those, as the handled binary won't have any external ABI which the running kernel would need to know (apart from the PVH boot interface, which isn't OS dependent).

Quoted 86 lines
Quoted 86 lines
include/elf.h new file +85 -0
+/* e_ident */
+#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
+                      (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
+                      (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
+                      (ehdr).e_ident[EI_MAG3] == ELFMAG3)
+
+/* e_flags */
+#define EF_ARM_EABI_MASK    0xff000000
+#define EF_ARM_EABI_UNKNOWN 0x00000000
+#define EF_ARM_EABI_VER1    0x01000000
+#define EF_ARM_EABI_VER2    0x02000000
+#define EF_ARM_EABI_VER3    0x03000000
+#define EF_ARM_EABI_VER4    0x04000000
+#define EF_ARM_EABI_VER5    0x05000000
+
+/* ELF Header */
+typedef struct {
+    unsigned char e_ident[EI_NIDENT]; /* ELF Identification */
+    Elf32_Half    e_type;        /* object file type */
+    Elf32_Half    e_machine;     /* machine */
+    Elf32_Word    e_version;     /* object file version */
+    Elf32_Addr    e_entry;       /* virtual entry point */
+    Elf32_Off     e_phoff;       /* program header table offset */
+    Elf32_Off     e_shoff;       /* section header table offset */
+    Elf32_Word    e_flags;       /* processor-specific flags */
+    Elf32_Half    e_ehsize;      /* ELF header size */
+    Elf32_Half    e_phentsize;   /* program header entry size */
+    Elf32_Half    e_phnum;       /* number of program header entries */
+    Elf32_Half    e_shentsize;   /* section header entry size */
+    Elf32_Half    e_shnum;       /* number of section header entries */
+    Elf32_Half    e_shstrndx;    /* section header table's "section
+                                    header string table" entry offset */
+} Elf32_Ehdr;
+
+typedef struct {
+    unsigned char e_ident[EI_NIDENT]; /* Id bytes */
+    Elf64_Half    e_type;        /* file type */
+    Elf64_Half    e_machine;     /* machine type */
+    Elf64_Word    e_version;     /* version number */
+    Elf64_Addr    e_entry;       /* entry point */
+    Elf64_Off     e_phoff;       /* Program hdr offset */
+    Elf64_Off     e_shoff;       /* Section hdr offset */
+    Elf64_Word    e_flags;       /* Processor flags */
+    Elf64_Half    e_ehsize;      /* sizeof ehdr */
+    Elf64_Half    e_phentsize;   /* Program header entry size */
+    Elf64_Half    e_phnum;       /* Number of program headers */
+    Elf64_Half    e_shentsize;   /* Section header entry size */
+    Elf64_Half    e_shnum;       /* Number of section headers */
+    Elf64_Half    e_shstrndx;    /* String table index */
+} Elf64_Ehdr;
+
+/* e_type */
+#define ET_NONE      0           /* No file type */
+#define ET_REL       1           /* relocatable file */
+#define ET_EXEC      2           /* executable file */
+#define ET_DYN       3           /* shared object file */
+#define ET_CORE      4           /* core file */
+#define ET_NUM       5           /* number of types */
+#define ET_LOPROC    0xff00      /* reserved range for processor */
+#define ET_HIPROC    0xffff      /*   specific e_type */
+
+/* e_machine */
+#define EM_NONE      0           /* No Machine */
+#define EM_M32       1           /* AT&T WE 32100 */
+#define EM_SPARC     2           /* SPARC */
+#define EM_386       3           /* Intel 80386 */
+#define EM_68K       4           /* Motorola 68000 */
+#define EM_88K       5           /* Motorola 88000 */
+#define EM_486       6           /* Intel 80486 - unused? */
+#define EM_860       7           /* Intel 80860 */
+#define EM_MIPS      8           /* MIPS R3000 Big-Endian only */
+/*
+ * Don't know if EM_MIPS_RS4_BE,
+ * EM_SPARC64, EM_PARISC,
+ * or EM_PPC are ABI compliant
+ */
+#define EM_MIPS_RS4_BE 10        /* MIPS R4000 Big-Endian */
+#define EM_SPARC64     11        /* SPARC v9 64-bit unoffical */
+#define EM_PARISC      15        /* HPPA */
+#define EM_SPARC32PLUS 18        /* Enhanced instruction set SPARC */
+#define EM_PPC         20        /* PowerPC */
+#define EM_PPC64       21        /* PowerPC 64-bit */
+#define EM_ARM         40        /* Advanced RISC Machines ARM */
+#define EM_ALPHA       41        /* DEC ALPHA */
+#define EM_SPARCV9     43        /* SPARC version 9 */
Quoted 8 lines

include/elf.h line 189

Quoted 5 lines
include/elf.h new file +5 -0
+#define EM_ALPHA_EXP   0x9026    /* DEC ALPHA */
+#define EM_IA_64       50        /* Intel Merced */
+#define EM_X86_64      62        /* AMD x86-64 architecture */
+#define EM_VAX         75        /* DEC VAX */
+#define EM_AARCH64    183        /* ARM 64-bit */

Here I similarly think some stripping down might help. Doing so would then also permit to leave out the comment in the middle.

Here I'm a little bit more on the edge. Some historical entries can probably be dropped, but which ones would want to stay?

Quoted 143 lines
Quoted 143 lines
include/elf.h new file +142 -0
+/* Version */
+#define EV_NONE      0           /* Invalid */
+#define EV_CURRENT   1           /* Current */
+#define EV_NUM       2           /* number of versions */
+
+/* Program Header */
+typedef struct {
+    Elf32_Word    p_type;        /* segment type */
+    Elf32_Off     p_offset;      /* segment offset */
+    Elf32_Addr    p_vaddr;       /* virtual address of segment */
+    Elf32_Addr    p_paddr;       /* physical address - ignored? */
+    Elf32_Word    p_filesz;      /* number of bytes in file for seg. */
+    Elf32_Word    p_memsz;       /* number of bytes in mem. for seg. */
+    Elf32_Word    p_flags;       /* flags */
+    Elf32_Word    p_align;       /* memory alignment */
+} Elf32_Phdr;
+
+typedef struct {
+    Elf64_Word    p_type;        /* entry type */
+    Elf64_Word    p_flags;       /* flags */
+    Elf64_Off     p_offset;      /* offset */
+    Elf64_Addr    p_vaddr;       /* virtual address */
+    Elf64_Addr    p_paddr;       /* physical address */
+    Elf64_Xword   p_filesz;      /* file size */
+    Elf64_Xword   p_memsz;       /* memory size */
+    Elf64_Xword   p_align;       /* memory & file alignment */
+} Elf64_Phdr;
+
+/* Segment types - p_type */
+#define PT_NULL      0           /* unused */
+#define PT_LOAD      1           /* loadable segment */
+#define PT_DYNAMIC   2           /* dynamic linking section */
+#define PT_INTERP    3           /* the RTLD */
+#define PT_NOTE      4           /* auxiliary information */
+#define PT_SHLIB     5           /* reserved - purpose undefined */
+#define PT_PHDR      6           /* program header */
+#define PT_NUM       7           /* Number of segment types */
+#define PT_LOPROC    0x70000000  /* reserved range for processor */
+#define PT_HIPROC    0x7fffffff  /*  specific segment types */
+
+/* Segment flags - p_flags */
+#define PF_X         0x1        /* Executable */
+#define PF_W         0x2        /* Writable */
+#define PF_R         0x4        /* Readable */
+#define PF_MASKPROC  0xf0000000 /* reserved bits for processor */
+                                /*  specific segment flags */
+
+/* Section Header */
+typedef struct {
+    Elf32_Word    sh_name;      /* name - index into section header
+                                   string table section */
+    Elf32_Word    sh_type;      /* type */
+    Elf32_Word    sh_flags;     /* flags */
+    Elf32_Addr    sh_addr;      /* address */
+    Elf32_Off     sh_offset;    /* file offset */
+    Elf32_Word    sh_size;      /* section size */
+    Elf32_Word    sh_link;      /* section header table index link */
+    Elf32_Word    sh_info;      /* extra information */
+    Elf32_Word    sh_addralign; /* address alignment */
+    Elf32_Word    sh_entsize;   /* section entry size */
+} Elf32_Shdr;
+
+typedef struct {
+    Elf64_Word    sh_name;      /* section name */
+    Elf64_Word    sh_type;      /* section type */
+    Elf64_Xword   sh_flags;     /* section flags */
+    Elf64_Addr    sh_addr;      /* virtual address */
+    Elf64_Off     sh_offset;    /* file offset */
+    Elf64_Xword   sh_size;      /* section size */
+    Elf64_Word    sh_link;      /* link to another */
+    Elf64_Word    sh_info;      /* misc info */
+    Elf64_Xword   sh_addralign; /* memory alignment */
+    Elf64_Xword   sh_entsize;   /* table entry size */
+} Elf64_Shdr;
+
+/* sh_type */
+#define SHT_NULL        0       /* inactive */
+#define SHT_PROGBITS    1       /* program defined information */
+#define SHT_SYMTAB      2       /* symbol table section */
+#define SHT_STRTAB      3       /* string table section */
+#define SHT_RELA        4       /* relocation section with addends*/
+#define SHT_HASH        5       /* symbol hash table section */
+#define SHT_DYNAMIC     6       /* dynamic section */
+#define SHT_NOTE        7       /* note section */
+#define SHT_NOBITS      8       /* no space section */
+#define SHT_REL         9       /* relation section without addends */
+#define SHT_SHLIB      10       /* reserved - purpose unknown */
+#define SHT_DYNSYM     11       /* dynamic symbol table section */
+#define SHT_NUM        12       /* number of section types */
+
+/* Note definitions */
+typedef struct {
+    Elf32_Word namesz;
+    Elf32_Word descsz;
+    Elf32_Word type;
+    char data[];
+} Elf32_Note;
+
+typedef struct {
+    Elf64_Word namesz;
+    Elf64_Word descsz;
+    Elf64_Word type;
+    char data[];
+} Elf64_Note;
+
+/* Abstraction layer for handling 32- and 64-bit ELF files. */
+
+typedef union {
+    Elf32_Ehdr e32;
+    Elf64_Ehdr e64;
+} elf_ehdr;
+
+static inline bool elf_is_32bit(elf_ehdr *ehdr)
+{
+    return ehdr->e32.e_ident[EI_CLASS] == ELFCLASS32;
+}
+
+static inline bool elf_is_64bit(elf_ehdr *ehdr)
+{
+    return ehdr->e32.e_ident[EI_CLASS] == ELFCLASS64;
+}
+
+#define ehdr_val(ehdr, elem) (elf_is_32bit(ehdr) ? (ehdr)->e32.elem : (ehdr)->e64.elem)
+
+typedef union {
+    Elf32_Phdr e32;
+    Elf64_Phdr e64;
+} elf_phdr;
+
+#define phdr_val(ehdr, phdr, elem) (elf_is_32bit(ehdr) ? (phdr)->e32.elem : (phdr)->e64.elem)
+
+typedef union {
+    Elf32_Shdr e32;
+    Elf64_Shdr e64;
+} elf_shdr;
+
+#define shdr_val(ehdr, shdr, elem) (elf_is_32bit(ehdr) ? (shdr)->e32.elem : (shdr)->e64.elem)
+
+typedef union {
+    Elf32_Note e32;
+    Elf64_Note e64;
+} elf_note;
Quoted 8 lines

include/elf.h line 338

Quoted 6 lines
include/elf.h new file +6 -0
+
+#define note_val(ehdr, note, elem) (elf_is_32bit(ehdr) ? (note)->e32.elem : (note)->e64.elem)
+
+static inline void *elf_ptr_add(void *ptr, unsigned long add)
+{
+    return (char *)ptr + add;

You can omit the cast here, can't you?

Yes.

Quoted 4 lines

You're the maintainer, so you'll know how many of the comments you want to address. Either way:

Thanks,

Juergen

#10 lore raw

Re: [MINI-OS PATCH 03/12] add elf.h

Quoted 45 lines · Jürgen Groß
Quoted 35 lines · Jan Beulich
Quoted 34 lines · Juergen Gross
include/elf.h new file +34 -0
+/* e_type */
+#define ET_NONE      0           /* No file type */
+#define ET_REL       1           /* relocatable file */
+#define ET_EXEC      2           /* executable file */
+#define ET_DYN       3           /* shared object file */
+#define ET_CORE      4           /* core file */
+#define ET_NUM       5           /* number of types */
+#define ET_LOPROC    0xff00      /* reserved range for processor */
+#define ET_HIPROC    0xffff      /*   specific e_type */
+
+/* e_machine */
+#define EM_NONE      0           /* No Machine */
+#define EM_M32       1           /* AT&T WE 32100 */
+#define EM_SPARC     2           /* SPARC */
+#define EM_386       3           /* Intel 80386 */
+#define EM_68K       4           /* Motorola 68000 */
+#define EM_88K       5           /* Motorola 88000 */
+#define EM_486       6           /* Intel 80486 - unused? */
+#define EM_860       7           /* Intel 80860 */
+#define EM_MIPS      8           /* MIPS R3000 Big-Endian only */
+/*
+ * Don't know if EM_MIPS_RS4_BE,
+ * EM_SPARC64, EM_PARISC,
+ * or EM_PPC are ABI compliant
+ */
+#define EM_MIPS_RS4_BE 10        /* MIPS R4000 Big-Endian */
+#define EM_SPARC64     11        /* SPARC v9 64-bit unoffical */
+#define EM_PARISC      15        /* HPPA */
+#define EM_SPARC32PLUS 18        /* Enhanced instruction set SPARC */
+#define EM_PPC         20        /* PowerPC */
+#define EM_PPC64       21        /* PowerPC 64-bit */
+#define EM_ARM         40        /* Advanced RISC Machines ARM */
+#define EM_ALPHA       41        /* DEC ALPHA */
+#define EM_SPARCV9     43        /* SPARC version 9 */
Quoted 8 lines

include/elf.h line 189

Quoted 5 lines
include/elf.h new file +5 -0
+#define EM_ALPHA_EXP   0x9026    /* DEC ALPHA */
+#define EM_IA_64       50        /* Intel Merced */
+#define EM_X86_64      62        /* AMD x86-64 architecture */
+#define EM_VAX         75        /* DEC VAX */
+#define EM_AARCH64    183        /* ARM 64-bit */

Here I similarly think some stripping down might help. Doing so would then also permit to leave out the comment in the middle.

Quoted 2 lines

Here I'm a little bit more on the edge. Some historical entries can probably be dropped, but which ones would want to stay?

The ones we presently need for any of the architectures we have (partial) ports for in the hypervisor, or that the hypervisor targets for compatibility.

Jan

#11 lore raw

PATCH 4/12 kexec: analyze new kernel for kexec

In reply to Juergen Gross

Analyze the properties of the new kernel to be loaded by kexec. The data needed is:

- upper boundary in final location
- copy and memory clear operations
- entry point and entry parameter

---
 arch/x86/kexec.c |  91 +++++++++++++++++++++++++++++++++++
 include/kexec.h  |  11 +++++
 kexec.c          | 120 ++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 220 insertions(+), 2 deletions(-)
arch/x86/kexec.c +91 -0
index bf247797..2069f3c6 100644
@@ -28,8 +28,15 @@
 
 #include <mini-os/os.h>
 #include <mini-os/lib.h>
+#include <mini-os/e820.h>
+#include <mini-os/err.h>
 #include <mini-os/kexec.h>
 
+#include <xen/elfnote.h>
+#include <xen/arch-x86/hvm/start_info.h>
+
+static unsigned long kernel_entry = ~0UL;
+
 /*
  * Final stage of kexec. Copies all data to the final destinations, zeroes
  * .bss and activates new kernel.
@@ -106,4 +113,88 @@ void do_kexec(void *kexec_page)
         :"m" (final));
 }
 
+bool kexec_chk_arch(elf_ehdr *ehdr)
+{
+    return ehdr->e32.e_machine == EM_386 || ehdr->e32.e_machine == EM_X86_64;
+}
+
+static unsigned int note_data_sz(unsigned int sz)
+{
+    return (sz + 3) & ~3;
+}
+
+static void check_notes_entry(elf_ehdr *ehdr, void *start, unsigned int len)
+{
+    elf_note *note = start;
+    unsigned int off, note_len, namesz, descsz;
+    char *val;
+
+    for ( off = 0; off < len; off += note_len )
+    {
+        namesz = note_data_sz(note_val(ehdr, note, namesz));
+        descsz = note_data_sz(note_val(ehdr, note, descsz));
+        val = note_val(ehdr, note, data);
+        note_len = val - (char *)note + namesz + descsz;
+
+        if ( !strncmp(val, "Xen", namesz) &&
+             note_val(ehdr, note, type) == XEN_ELFNOTE_PHYS32_ENTRY )
+        {
+            val += namesz;
+            switch ( note_val(ehdr, note, descsz) )
+            {
+            case 1:
+                kernel_entry = *(uint8_t *)val;
+                return;
+            case 2:
+                kernel_entry = *(uint16_t *)val;
+                return;
+            case 4:
+                kernel_entry = *(uint32_t *)val;
+                return;
+            case 8:
+                kernel_entry = *(uint64_t *)val;
+                return;
+            default:
+                break;
+            }
+        }
+
+        note = elf_ptr_add(note, note_len);
+    }
+}
+
+int kexec_arch_analyze_phdr(elf_ehdr *ehdr, elf_phdr *phdr)
+{
+    void *notes_start;
+    unsigned int notes_len;
+
+    if ( phdr_val(ehdr, phdr, p_type) != PT_NOTE || kernel_entry != ~0UL )
+        return 0;
+
+    notes_start = elf_ptr_add(ehdr, phdr_val(ehdr, phdr, p_offset));
+    notes_len = phdr_val(ehdr, phdr, p_filesz);
+    check_notes_entry(ehdr, notes_start, notes_len);
+
+    return 0;
+}
+
+int kexec_arch_analyze_shdr(elf_ehdr *ehdr, elf_shdr *shdr)
+{
+    void *notes_start;
+    unsigned int notes_len;
+
+    if ( shdr_val(ehdr, shdr, sh_type) != SHT_NOTE || kernel_entry != ~0UL )
+        return 0;
+
+    notes_start = elf_ptr_add(ehdr, shdr_val(ehdr, shdr, sh_offset));
+    notes_len = shdr_val(ehdr, shdr, sh_size);
+    check_notes_entry(ehdr, notes_start, notes_len);
+
+    return 0;
+}
+
+bool kexec_arch_need_analyze_shdrs(void)
+{
+    return kernel_entry == ~0UL;
+}
 #endif /* CONFIG_KEXEC */
include/kexec.h +11 -0
index 722be456..f54cbb90 100644
@@ -1,5 +1,6 @@
 #ifndef _KEXEC_H
 #define _KEXEC_H
+#include <mini-os/elf.h>
 
 /* One element of kexec actions (last element must have action KEXEC_CALL): */
 struct kexec_action {
@@ -18,6 +19,8 @@ struct kexec_action {
 extern char _kexec_start[], _kexec_end[];
 extern struct kexec_action kexec_actions[KEXEC_MAX_ACTIONS];
 
+extern unsigned long kexec_last_addr;
+
 int kexec_add_action(int action, void *dest, void *src, unsigned int len);
 
 #define KEXEC_SECSIZE ((unsigned long)_kexec_end - (unsigned long)_kexec_start)
@@ -31,4 +34,12 @@ void do_kexec(void *kexec_page);
 /* Assembler code for switching off paging and passing execution to new OS. */
 void kexec_phys(void);
 
+/* Check kernel to match current architecture. */
+bool kexec_chk_arch(elf_ehdr *ehdr);
+
+/* Architecture specific ELF handling functions. */
+int kexec_arch_analyze_phdr(elf_ehdr *ehdr, elf_phdr *phdr);
+int kexec_arch_analyze_shdr(elf_ehdr *ehdr, elf_shdr *shdr);
+bool kexec_arch_need_analyze_shdrs(void);
+
 #endif /* _KEXEC_H */
kexec.c +118 -2

133 lines not on this page · raw message

Signature · 1 line
2.43.0
Reviewed-by on v1 4/12
#12 lore raw

Re: [MINI-OS PATCH 04/12] kexec: analyze new kernel for kexec

Review

Quoted 12 lines · Juergen Gross

Analyze the properties of the new kernel to be loaded by kexec. The data needed is:

- upper boundary in final location
- copy and memory clear operations
- entry point and entry parameter

(quoted diff) +2 -0
+
+static void check_notes_entry(elf_ehdr *ehdr, void *start, unsigned int len)

I think you should rename this to include read_ since it is necessary to set kernel_entry. read_phys32_entry_note() or read_note_kernel_entry() or some variation. To me, check_ implies a boolean return without a side effect.

kexec.c line 61

Quoted 8 lines
kexec.c +1 -2
@@ -54,9 +57,122 @@
  * - The new kernel is activated.
  */
 
-int kexec(void *kernel, unsigned long kernel_size,
-          const char *cmdline)
 
+int kexec(void *kernel, unsigned long kernel_size, const char *cmdline)

NIT: introduce kexec() with the single line form to avoid changing it

Everything else looks good, so preferably with the renaming:

Regards,
Jason

#13 lore raw

Re: [MINI-OS PATCH 04/12] kexec: analyze new kernel for kexec

Quoted 11 lines · Jason Andryuk
Quoted 9 lines · Juergen Gross

Analyze the properties of the new kernel to be loaded by kexec. The data needed is:

- upper boundary in final location
- copy and memory clear operations
- entry point and entry parameter

Quoted 7 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+static void check_notes_entry(elf_ehdr *ehdr, void *start, unsigned int len)

I think you should rename this to include read_ since it is necessary to set kernel_entry. read_phys32_entry_note() or read_note_kernel_entry() or some variation. To me, check_ implies a boolean return without a side effect.

I'll go with read_note_entry().

Quoted 8 lines
Quoted 7 lines
kexec.c +0 -2
@@ -54,9 +57,122 @@
   * - The new kernel is activated.
   */
 
-int kexec(void *kernel, unsigned long kernel_size,
-          const char *cmdline)
Quoted 7 lines

kexec.c line 168

Quoted 1 line
kexec.c +1 -0
+int kexec(void *kernel, unsigned long kernel_size, const char *cmdline)

NIT: introduce kexec() with the single line form to avoid changing it

Everything else looks good, so preferably with the renaming:

Thanks,

Juergen

#14 lore raw

PATCH 5/12 kexec: finalize parameter location and size

In reply to Juergen Gross

Finalize the location and the size of the parameters for the new kernel. This is needed in order to avoid allocating new memory in the area occupied by the new kernel and parameters.

---
 arch/x86/kexec.c | 15 +++++++++++++++
 include/kexec.h  |  3 +++
 kexec.c          |  2 ++
 3 files changed, 20 insertions(+)
arch/x86/kexec.c +15 -0
index 2069f3c6..98a478d3 100644
@@ -197,4 +197,19 @@ bool kexec_arch_need_analyze_shdrs(void)
 {
     return kernel_entry == ~0UL;
 }
+
+static unsigned long kexec_param_loc;
+static unsigned int kexec_param_size;
+
+void kexec_set_param_loc(const char *cmdline)
+{
+    kexec_param_size = sizeof(struct hvm_start_info);
+    kexec_param_size += e820_entries * sizeof(struct hvm_memmap_table_entry);
+    kexec_param_size += strlen(cmdline) + 1;
+
+    kexec_last_addr = (kexec_last_addr + 7) & ~7UL;
+    kexec_param_loc = kexec_last_addr;
+    kexec_last_addr += kexec_param_size;
+    kexec_last_addr = round_pgup(kexec_last_addr);
+}
 #endif /* CONFIG_KEXEC */
include/kexec.h +3 -0
index f54cbb90..8a2b552f 100644
@@ -42,4 +42,7 @@ int kexec_arch_analyze_phdr(elf_ehdr *ehdr, elf_phdr *phdr);
 int kexec_arch_analyze_shdr(elf_ehdr *ehdr, elf_shdr *shdr);
 bool kexec_arch_need_analyze_shdrs(void);
 
+/* Finalize parameter location and size. */
+void kexec_set_param_loc(const char *cmdline);
+
 #endif /* _KEXEC_H */
kexec.c +2 -0
index 3ff4ea07..7e559994 100644
@@ -173,6 +173,8 @@ int kexec(void *kernel, unsigned long kernel_size, const char *cmdline)
     if ( ret )
         return ret;
 
+    kexec_set_param_loc(cmdline);
+
     return ENOSYS;
 }
 EXPORT_SYMBOL(kexec);
Signature · 1 line
2.43.0
#16 lore raw

PATCH 6/12 kexec: reserve memory below boundary

In reply to Juergen Gross

In order to support kexec any memory used during copying the new kernel to its final destination must not overlap with the destination area.

In order to achieve that add a new interface allowing to mark all allocatable memory below a specific physical address as not available.

This is done by walking through all chunks of the buddy allocator and removing the chunks (or chunk parts) below the boundary. The removed chunks are put into a list in order to be able to undo the operation in case kexec is failing before doing any unrecoverable system modifications.

Any pages freed located below the boundary need to go directly into the list of reserved pages instead of the free pool.

Call the new function from kexec code.

---
 include/mm.h |  5 +++
 kexec.c      |  5 +++
 mm.c         | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 98 insertions(+), 1 deletion(-)
include/mm.h +5 -0
index 4fc364ff..5775c3e1 100644
@@ -57,6 +57,11 @@ unsigned long alloc_pages(int order);
 void free_pages(void *pointer, int order);
 #define free_page(p)    free_pages(p, 0)
 
+#ifdef CONFIG_KEXEC
+void reserve_memory_below(unsigned long boundary);
+void unreserve_memory_below(void);
+#endif
+
 static __inline__ int get_order(unsigned long size)
 {
     int order;
kexec.c +5 -0
index 7e559994..68457711 100644
@@ -175,6 +175,11 @@ int kexec(void *kernel, unsigned long kernel_size, const char *cmdline)
 
     kexec_set_param_loc(cmdline);
 
+    reserve_memory_below(kexec_last_addr);
+
+    /* Error exit. */
+    unreserve_memory_below();
+
     return ENOSYS;
 }
 EXPORT_SYMBOL(kexec);
mm.c +88 -1

104 lines not on this page · raw message

Signature · 1 line
2.43.0
Reviewed-by on v1 6/12
#17 lore raw

Re: [MINI-OS PATCH 06/12] kexec: reserve memory below boundary

Quoted 14 lines · Juergen Gross

In order to support kexec any memory used during copying the new kernel to its final destination must not overlap with the destination area.

In order to achieve that add a new interface allowing to mark all allocatable memory below a specific physical address as not available.

This is done by walking through all chunks of the buddy allocator and removing the chunks (or chunk parts) below the boundary. The removed chunks are put into a list in order to be able to undo the operation in case kexec is failing before doing any unrecoverable system modifications.

Quoted 6 lines

Any pages freed located below the boundary need to go directly into the list of reserved pages instead of the free pool.

Call the new function from kexec code.

#18 lore raw

PATCH 7/12 kexec: build parameters for new kernel

In reply to Juergen Gross

Build the parameters for the new kernel, consisting of the hvm_start_info struct, the memory map and the command line.

---
 arch/x86/kexec.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/kexec.h  |  4 ++++
 kexec.c          | 13 ++++++++++-
 3 files changed, 74 insertions(+), 1 deletion(-)
arch/x86/kexec.c +58 -0
index 98a478d3..6fc7d02d 100644
@@ -200,6 +200,7 @@ bool kexec_arch_need_analyze_shdrs(void)
 
 static unsigned long kexec_param_loc;
 static unsigned int kexec_param_size;
+static unsigned long kexec_param_mem;
 
 void kexec_set_param_loc(const char *cmdline)
 {
@@ -212,4 +213,61 @@ void kexec_set_param_loc(const char *cmdline)
     kexec_last_addr += kexec_param_size;
     kexec_last_addr = round_pgup(kexec_last_addr);
 }
+
+int kexec_get_entry(const char *cmdline)
+{
+    struct hvm_start_info *info;
+    struct hvm_memmap_table_entry *mmap;
+    unsigned int order;
+    unsigned int i;
+
+    if ( kernel_entry == ~0UL )
+        return ENOEXEC;
+
+    order = get_order(kexec_param_size);
+
+    kexec_param_mem = alloc_pages(order);
+    if ( !kexec_param_mem )
+        return ENOMEM;
+
+    info = (struct hvm_start_info *)kexec_param_mem;
+    memset(info, 0, sizeof(*info));
+    info->magic = XEN_HVM_START_MAGIC_VALUE;
+    info->version = 1;
+    info->cmdline_paddr = kexec_param_mem + sizeof(*info) +
+                          e820_entries * sizeof(struct hvm_memmap_table_entry);
+    info->memmap_paddr = kexec_param_mem + sizeof(*info);
+    info->memmap_entries = e820_entries;
+
+    mmap = (struct hvm_memmap_table_entry *)(info + 1);
+    for ( i = 0; i < e820_entries; i++ )
+    {
+        mmap->addr = e820_map[i].addr;
+        mmap->size = e820_map[i].size;
+        mmap->type = e820_map[i].type;
+        mmap++;
+    }
+
+    strcpy((char *)mmap, cmdline);
+
+    if ( kexec_add_action(KEXEC_COPY, to_virt(kexec_param_loc), info,
+                          kexec_param_size) )
+        return ENOSPC;
+
+    /* The call of the new kernel happens via the physical address! */
+    if ( kexec_add_action(KEXEC_CALL, (void *)kernel_entry,
+                          (void *)kexec_param_loc, 0) )
+        return ENOSPC;
+
+    return 0;
+}
+
+void kexec_get_entry_undo(void)
+{
+    if ( kexec_param_mem )
+    {
+        free_pages((void *)kexec_param_mem, get_order(kexec_param_size));
+        kexec_param_mem = 0;
+    }
+}
 #endif /* CONFIG_KEXEC */
include/kexec.h +4 -0
index 8a2b552f..7b103dea 100644
@@ -45,4 +45,8 @@ bool kexec_arch_need_analyze_shdrs(void);
 /* Finalize parameter location and size. */
 void kexec_set_param_loc(const char *cmdline);
 
+/* Get entry point and parameter of new kernel. */
+int kexec_get_entry(const char *cmdline);
+void kexec_get_entry_undo(void);
+
 #endif /* _KEXEC_H */
kexec.c +12 -1
index 68457711..0ef8eb35 100644
@@ -177,10 +177,21 @@ int kexec(void *kernel, unsigned long kernel_size, const char *cmdline)
 
     reserve_memory_below(kexec_last_addr);
 
+    ret = kexec_get_entry(cmdline);
+    if ( ret )
+    {
+        printk("kexec: ELF file of new kernel has no valid entry point\n");
+        goto err;
+    }
+
     /* Error exit. */
+    ret = ENOSYS;
+
+ err:
     unreserve_memory_below();
+    kexec_get_entry_undo();
 
-    return ENOSYS;
+    return ret;
 }
 EXPORT_SYMBOL(kexec);
 
Signature · 1 line
2.43.0
Reviewed-by on v1 7/12
#19 lore raw

Re: [MINI-OS PATCH 07/12] kexec: build parameters for new kernel

arch/x86/kexec.c void kexec_set_param_loc(const char *cmdline) · line 216

Quoted 11 lines · Juergen Gross

Build the parameters for the new kernel, consisting of the hvm_start_info struct, the memory map and the command line.

arch/x86/kexec.c +3 -0
@@ -212,4 +213,61 @@ void kexec_set_param_loc(const char *cmdline)
 
+
+    /* The call of the new kernel happens via the physical address! */
+    if ( kexec_add_action(KEXEC_CALL, (void *)kernel_entry,

Maybe kernel_entry_pa, kernel_phys32_entry, or kernel_phys_entry would be a better name to make the physical address clear?

Either way:

Regards,
Jason

#20 lore raw

Re: [MINI-OS PATCH 07/12] kexec: build parameters for new kernel

Quoted 7 lines · Jason Andryuk
Quoted 5 lines · Juergen Gross

Build the parameters for the new kernel, consisting of the hvm_start_info struct, the memory map and the command line.

Quoted 8 lines

arch/x86/kexec.c void kexec_set_param_loc(const char *cmdline) · line 216

Quoted 5 lines
arch/x86/kexec.c +3 -0
@@ -212,4 +213,61 @@ void kexec_set_param_loc(const char *cmdline)
 
+
+    /* The call of the new kernel happens via the physical address! */
+    if ( kexec_add_action(KEXEC_CALL, (void *)kernel_entry,

Maybe kernel_entry_pa, kernel_phys32_entry, or kernel_phys_entry would be a better name to make the physical address clear?

Fine with me.

Quoted 3 lines

Either way:

Thanks,

Juergen

#21 lore raw

PATCH 8/12 kexec: move used pages away for new kernel

In reply to Juergen Gross

Copying the new kexec kernel must not overwrite any pages still needed during this process. Those are especially the GDT, IDT and page tables.

Move those to new allocated pages and update any related pointers.

In case the kexec process is cancelled later, don't undo any page table moves, as the system can just be used with the new layout. By freeing the original pages there is no memory leaking.

GDT and IDT should be reverted to their original locations, as their original memory can't be freed due to not being whole pages.

---
 arch/x86/mm.c   | 126 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/kexec.h |   5 ++
 kexec.c         |   6 +++
 3 files changed, 137 insertions(+)
arch/x86/mm.c +126 -0
index 3ba6d917..a71eb192 100644
@@ -42,6 +42,7 @@
 #include <mini-os/paravirt.h>
 #include <mini-os/types.h>
 #include <mini-os/lib.h>
+#include <mini-os/kexec.h>
 #include <mini-os/xmalloc.h>
 #include <mini-os/e820.h>
 #include <xen/memory.h>
@@ -923,3 +924,128 @@ unsigned long map_frame_virt(unsigned long mfn)
     return addr;
 }
 EXPORT_SYMBOL(map_frame_virt);
+
+#ifdef CONFIG_KEXEC
+static unsigned long kexec_gdt;
+static unsigned long kexec_idt;
+
+static int move_pt(unsigned long va, unsigned int lvl, bool is_leaf,
+                   pgentry_t *pte, void *par)
+{
+    unsigned long boundary_pfn = *(unsigned long *)par;
+    unsigned long pfn;
+    void *old_pg, *new_pg;
+
+    if ( is_leaf )
+        return 0;
+
+    pfn = (lvl == PAGETABLE_LEVELS + 1) ? PHYS_PFN(*(unsigned long *)pte)
+                                        : pte_to_mfn(*pte);
+    if ( pfn >= boundary_pfn )
+        return 0;
+
+    new_pg = (void *)alloc_page();
+    if ( !new_pg )
+        return ENOMEM;
+    old_pg = pfn_to_virt(pfn);
+    memcpy(new_pg, old_pg, PAGE_SIZE);
+    if ( lvl == PAGETABLE_LEVELS + 1 )
+        *(pgentry_t **)pte = new_pg;
+    else
+        *pte = ((unsigned long)new_pg & PAGE_MASK) | ptdata[lvl].prot;
+
+    tlb_flush();
+
+    free_page(old_pg);
+
+    return 0;
+}
+
+static int move_leaf(unsigned long va, unsigned int lvl, bool is_leaf,
+                     pgentry_t *pte, void *par)
+{
+    unsigned long boundary_pfn = *(unsigned long *)par;
+    unsigned long pfn;
+    void *old_pg, *new_pg;
+
+    if ( !is_leaf )
+        return 0;
+
+    /* No large page support, all pages must be valid. */
+    if ( (*pte & _PAGE_PSE) || !(*pte & _PAGE_PRESENT) )
+        return EINVAL;
+
+    pfn = pte_to_mfn(*pte);
+    if ( pfn >= boundary_pfn )
+        return 0;
+
+    new_pg = (void *)alloc_page();
+    if ( !new_pg )
+        return ENOMEM;
+    old_pg = pfn_to_virt(pfn);
+    memcpy(new_pg, old_pg, PAGE_SIZE);
+    *pte = ((unsigned long)new_pg & PAGE_MASK) | ptdata[lvl].prot;
+
+    invlpg(va);
+
+    free_page(old_pg);
+
+    return 0;
+}
+
+int kexec_move_used_pages(unsigned long boundary, unsigned long kernel,
+                          unsigned long kernel_size)
+{
+    int ret;
+    unsigned long boundary_pfn = PHYS_PFN(boundary);
+
+    kexec_gdt = alloc_page();
+    if ( !kexec_gdt )
+        return ENOMEM;
+    memcpy((char *)kexec_gdt, &gdt, sizeof(gdt));
+    gdt_ptr.base = kexec_gdt;
+    asm volatile("lgdt %0" : : "m" (gdt_ptr));
+
+    kexec_idt = alloc_page();
+    if ( !kexec_idt )
+        return ENOMEM;
+    memcpy((char *)kexec_idt, &idt, sizeof(idt));
+    idt_ptr.base = kexec_idt;
+    asm volatile("lidt %0" : : "m" (idt_ptr));
+
+    /* Top level page table needs special handling. */
+    ret = move_pt(0, PAGETABLE_LEVELS + 1, false, (pgentry_t *)(&pt_base),
+                  &boundary_pfn);
+    if ( ret )
+        return ret;
+    ret = walk_pt(0, ~0UL, move_pt, &boundary_pfn);
+    if ( ret )
+        return ret;
+
+    /* Move new kernel image pages. */
+    ret = walk_pt(kernel, kernel + kernel_size - 1, move_leaf, &boundary_pfn);
+    if ( ret )
+        return ret;
+
+    return 0;
+}
+
+void kexec_move_used_pages_undo(void)
+{
+    if ( kexec_gdt )
+    {
+        gdt_ptr.base = (unsigned long)&gdt;
+        asm volatile("lgdt %0" : : "m" (gdt_ptr));
+        free_page((void *)kexec_gdt);
+        kexec_gdt = 0;
+    }
+
+    if ( kexec_idt )
+    {
+        idt_ptr.base = (unsigned long)&idt;
+        asm volatile("lidt %0" : : "m" (idt_ptr));
+        free_page((void *)kexec_idt);
+        kexec_idt = 0;
+    }
+}
+#endif
include/kexec.h +5 -0
index 7b103dea..411fa013 100644
@@ -49,4 +49,9 @@ void kexec_set_param_loc(const char *cmdline);
 int kexec_get_entry(const char *cmdline);
 void kexec_get_entry_undo(void);
 
+/* Move used pages away from new kernel area. */
+int kexec_move_used_pages(unsigned long boundary, unsigned long kernel,
+                          unsigned long kernel_size);
+void kexec_move_used_pages_undo(void);
+
 #endif /* _KEXEC_H */
kexec.c +6 -0
index 0ef8eb35..16a0030a 100644
@@ -184,11 +184,17 @@ int kexec(void *kernel, unsigned long kernel_size, const char *cmdline)
         goto err;
     }
 
+    ret = kexec_move_used_pages(kexec_last_addr, (unsigned long)kernel,
+                                kernel_size);
+    if ( ret )
+        goto err;
+
     /* Error exit. */
     ret = ENOSYS;
 
  err:
     unreserve_memory_below();
+    kexec_move_used_pages_undo();
     kexec_get_entry_undo();
 
     return ret;
Signature · 1 line
2.43.0
Reviewed-by on v1 8/12
#22 lore raw

Re: [MINI-OS PATCH 08/12] kexec: move used pages away for new kernel

Quoted 6 lines · Juergen Gross

Copying the new kexec kernel must not overwrite any pages still needed during this process. Those are especially the GDT, IDT and page tables.

Move those to new allocated pages and update any related pointers.

Quoted 8 lines

In case the kexec process is cancelled later, don't undo any page table moves, as the system can just be used with the new layout. By freeing the original pages there is no memory leaking.

GDT and IDT should be reverted to their original locations, as their original memory can't be freed due to not being whole pages.

#23 lore raw

PATCH 9/12 mm: change set_readonly() to change_readonly()

In reply to Juergen Gross

Rename set_readonly() to change_readonly() and add a parameter specifying whether it should set the kernel to readonly or to writable. At the same time move the boundary setting from the only caller into the function itself, avoiding the need to use the same boundaries in future, when it will be called to set the kernel to writable again. Make the function globally visible in order to allow calling it from kexec coding later.

Merge clear_bootstrap() into change_readonly() and undo its setting of page 0 to invalid when setting the kernel writable.

---
 arch/x86/mm.c | 111 +++++++++++++++++++++++++++-----------------------
 include/mm.h  |   3 ++
 2 files changed, 64 insertions(+), 50 deletions(-)
arch/x86/mm.c +61 -50
index a71eb192..f4419d95 100644
@@ -405,17 +405,19 @@ static void build_pagetable(unsigned long *start_pfn, unsigned long *max_pfn)
  */
 extern struct shared_info shared_info;
 
-struct set_readonly_par {
+struct change_readonly_par {
     unsigned long etext;
 #ifdef CONFIG_PARAVIRT
     unsigned int count;
 #endif
+    bool readonly;
 };
 
-static int set_readonly_func(unsigned long va, unsigned int lvl, bool is_leaf,
-                             pgentry_t *pte, void *par)
+static int change_readonly_func(unsigned long va, unsigned int lvl,
+                                bool is_leaf, pgentry_t *pte, void *par)
 {
-    struct set_readonly_par *ro = par;
+    struct change_readonly_par *ro = par;
+    pgentry_t newval;
 
     if ( !is_leaf )
         return 0;
@@ -429,9 +431,11 @@ static int set_readonly_func(unsigned long va, unsigned int lvl, bool is_leaf,
         return 0;
     }
 
+    newval = ro->readonly ? (*pte & ~_PAGE_RW) : (*pte | _PAGE_RW);
+
 #ifdef CONFIG_PARAVIRT
     mmu_updates[ro->count].ptr = virt_to_mach(pte);
-    mmu_updates[ro->count].val = *pte & ~_PAGE_RW;
+    mmu_updates[ro->count].val = newval;
     ro->count++;
 
     if ( ro->count == L1_PAGETABLE_ENTRIES )
@@ -442,7 +446,7 @@ static int set_readonly_func(unsigned long va, unsigned int lvl, bool is_leaf,
          ro->count = 0;
     }
 #else
-    *pte &= ~_PAGE_RW;
+    *pte = newval;
 #endif
 
     return 0;
@@ -463,23 +467,6 @@ static void tlb_flush(void)
 }
 #endif
 
-static void set_readonly(void *text, void *etext)
-{
-    struct set_readonly_par setro = { .etext = (unsigned long)etext };
-    unsigned long start_address = PAGE_ALIGN((unsigned long)text);
-
-    printk("setting %p-%p readonly\n", text, etext);
-    walk_pt(start_address, setro.etext, set_readonly_func, &setro);
-
-#ifdef CONFIG_PARAVIRT
-    if ( setro.count &&
-         HYPERVISOR_mmu_update(mmu_updates, setro.count, NULL, DOMID_SELF) < 0)
-        BUG();
-#endif
-
-    tlb_flush();
-}
-
 /*
  * get the PTE for virtual address va if it exists. Otherwise NULL.
  */
@@ -508,6 +495,51 @@ static pgentry_t *get_pgt(unsigned long va)
     return tab;
 }
 
+void change_readonly(bool readonly)
+{
+    struct change_readonly_par ro = {
+        .etext = (unsigned long)&_erodata,
+        .readonly = readonly,
+    };
+    unsigned long start_address = PAGE_ALIGN((unsigned long)&_text);
+#ifdef CONFIG_PARAVIRT
+    pte_t nullpte = { };
+    int rc;
+#else
+    pgentry_t *pgt = get_pgt((unsigned long)&_text);
+#endif
+
+    if ( readonly )
+    {
+#ifdef CONFIG_PARAVIRT
+        if ( (rc = HYPERVISOR_update_va_mapping(0, nullpte, UVMF_INVLPG)) )
+            printk("Unable to unmap NULL page. rc=%d\n", rc);
+#else
+        *pgt = 0;
+        invlpg((unsigned long)&_text);
+#endif
+    }
+    else
+    {
+#ifdef CONFIG_PARAVIRT
+        /* No kexec support with PARAVIRT. */
+        BUG();
+#else
+        *pgt = L1_PROT;
+#endif
+    }
+
+    printk("setting %p-%p readonly\n", &_text, &_erodata);
+    walk_pt(start_address, ro.etext, change_readonly_func, &ro);
+
+#ifdef CONFIG_PARAVIRT
+    if ( ro.count &&
+         HYPERVISOR_mmu_update(mmu_updates, ro.count, NULL, DOMID_SELF) < 0)
+        BUG();
+#endif
+
+    tlb_flush();
+}
 
 /*
  * return a valid PTE for a given virtual address. If PTE does not exist,
@@ -789,31 +821,6 @@ int unmap_frames(unsigned long va, unsigned long num_frames)
 }
 EXPORT_SYMBOL(unmap_frames);
 
-/*
- * Clear some of the bootstrap memory
- */
-static void clear_bootstrap(void)
-{
-#ifdef CONFIG_PARAVIRT
-    pte_t nullpte = { };
-    int rc;
-#else
-    pgentry_t *pgt;
-#endif
-
-    /* Use first page as the CoW zero page */
-    memset(&_text, 0, PAGE_SIZE);
-    mfn_zero = virt_to_mfn((unsigned long) &_text);
-#ifdef CONFIG_PARAVIRT
-    if ( (rc = HYPERVISOR_update_va_mapping(0, nullpte, UVMF_INVLPG)) )
-        printk("Unable to unmap NULL page. rc=%d\n", rc);
-#else
-    pgt = get_pgt((unsigned long)&_text);
-    *pgt = 0;
-    invlpg((unsigned long)&_text);
-#endif
-}
-
 #ifdef CONFIG_PARAVIRT
 void p2m_chk_pfn(unsigned long pfn)
 {
@@ -884,8 +891,12 @@ void arch_init_mm(unsigned long* start_pfn_p, unsigned long* max_pfn_p)
     printk("    max_pfn: %lx\n", max_pfn);
 
     build_pagetable(&start_pfn, &max_pfn);
-    clear_bootstrap();
-    set_readonly(&_text, &_erodata);
+
+    /* Prepare page 0 as CoW page. */
+    memset(&_text, 0, PAGE_SIZE);
+    mfn_zero = virt_to_mfn((unsigned long)&_text);
+
+    change_readonly(true);
 
     *start_pfn_p = start_pfn;
     *max_pfn_p = max_pfn;
include/mm.h +3 -0
index 5775c3e1..0a16d56c 100644
@@ -25,6 +25,7 @@
 #ifndef _MM_H_
 #define _MM_H_
 
+#include <stdbool.h>
 #if defined(__i386__)
 #include <xen/arch-x86_32.h>
 #elif defined(__x86_64__)
@@ -92,4 +93,6 @@ extern unsigned long heap, brk, heap_mapped, heap_end;
 int free_physical_pages(xen_pfn_t *mfns, int n);
 void fini_mm(void);
 
+void change_readonly(bool readonly);
+
 #endif /* _MM_H_ */
Signature · 1 line
2.43.0
Reviewed-by on v1 9/12
#24 lore raw

Re: [MINI-OS PATCH 09/12] mm: change set_readonly() to change_readonly()

Quoted 9 lines · Juergen Gross

Rename set_readonly() to change_readonly() and add a parameter specifying whether it should set the kernel to readonly or to writable. At the same time move the boundary setting from the only caller into the function itself, avoiding the need to use the same boundaries in future, when it will be called to set the kernel to writable again. Make the function globally visible in order to allow calling it from kexec coding later.

Quoted 4 lines

Merge clear_bootstrap() into change_readonly() and undo its setting of page 0 to invalid when setting the kernel writable.

#25 lore raw

PATCH 10/12 kexec: switch read-only area to be writable again

In reply to Juergen Gross

In order to allow writing the new kernel, make the readonly area covering current kernel text writable again.

---
 kexec.c | 3 +++
 1 file changed, 3 insertions(+)
kexec.c +3 -0
index 16a0030a..2992f58f 100644
@@ -184,6 +184,8 @@ int kexec(void *kernel, unsigned long kernel_size, const char *cmdline)
         goto err;
     }
 
+    change_readonly(false);
+
     ret = kexec_move_used_pages(kexec_last_addr, (unsigned long)kernel,
                                 kernel_size);
     if ( ret )
@@ -193,6 +195,7 @@ int kexec(void *kernel, unsigned long kernel_size, const char *cmdline)
     ret = ENOSYS;
 
  err:
+    change_readonly(true);
     unreserve_memory_below();
     kexec_move_used_pages_undo();
     kexec_get_entry_undo();
Signature · 1 line
2.43.0
#27 lore raw

PATCH 11/12 kexec: add kexec callback functionality

In reply to Juergen Gross

Add a kexec_call() macro which will provide the capability to register a function for being called when doing a kexec() call. The called functions will be called with a boolean parameter "undo" indicating whether a previous call needs to be undone due to a failure during kexec().

The related loop to call all callbacks is added to kexec().

---
 arch/x86/minios-x86.lds.S |  8 ++++++++
 arch/x86/mm.c             |  3 ++-
 include/kexec.h           |  6 ++++++
 kexec.c                   | 13 +++++++++++++
 4 files changed, 29 insertions(+), 1 deletion(-)
arch/x86/minios-x86.lds.S +8 -0
index 83ec41ce..35356b34 100644
@@ -58,6 +58,14 @@ SECTIONS
         }
         PROVIDE (__fini_array_end = .);
 
+#if defined(CONFIG_KEXEC)
+        PROVIDE (__kexec_array_start = .);
+        .kexec_array : {
+                *(.kexec_array)
+        }
+        PROVIDE (__kexec_array_end = .);
+#endif
+
         .ctors : {
                 __CTOR_LIST__ = .;
                 *(.ctors)
arch/x86/mm.c +2 -1
index f4419d95..26ede6f4 100644
@@ -529,7 +529,8 @@ void change_readonly(bool readonly)
 #endif
     }
 
-    printk("setting %p-%p readonly\n", &_text, &_erodata);
+    printk("setting %p-%p %s\n", &_text, &_erodata,
+           readonly ? "readonly" : "writable");
     walk_pt(start_address, ro.etext, change_readonly_func, &ro);
 
 #ifdef CONFIG_PARAVIRT
include/kexec.h +6 -0
index 411fa013..b89c3000 100644
@@ -18,6 +18,12 @@ struct kexec_action {
 
 extern char _kexec_start[], _kexec_end[];
 extern struct kexec_action kexec_actions[KEXEC_MAX_ACTIONS];
+extern unsigned long __kexec_array_start[], __kexec_array_end[];
+
+typedef int(*kexeccall_t)(bool undo);
+#define kexec_call(func)                                                   \
+    static kexeccall_t __kexeccall_##func __attribute__((__used__))        \
+                       __attribute__((__section__(".kexec_array"))) = func
 
 extern unsigned long kexec_last_addr;
 
kexec.c +13 -0
index 2992f58f..2db876e8 100644
@@ -168,6 +168,7 @@ static int analyze_kernel(void *kernel, unsigned long size)
 int kexec(void *kernel, unsigned long kernel_size, const char *cmdline)
 {
     int ret;
+    unsigned long *func;
 
     ret = analyze_kernel(kernel, kernel_size);
     if ( ret )
@@ -191,6 +192,18 @@ int kexec(void *kernel, unsigned long kernel_size, const char *cmdline)
     if ( ret )
         goto err;
 
+    for ( func = __kexec_array_start; func < __kexec_array_end; func++ )
+    {
+        ret = ((kexeccall_t)(*func))(false);
+        if ( ret )
+        {
+            for ( func--; func >= __kexec_array_start; func-- )
+                ((kexeccall_t)(*func))(true);
+
+            goto err;
+        }
+    }
+
     /* Error exit. */
     ret = ENOSYS;
 
Signature · 1 line
2.43.0
Reviewed-by on v1 11/12
#28 lore raw

Re: [MINI-OS PATCH 11/12] kexec: add kexec callback functionality

Quoted 16 lines · Juergen Gross

Add a kexec_call() macro which will provide the capability to register a function for being called when doing a kexec() call. The called functions will be called with a boolean parameter "undo" indicating whether a previous call needs to be undone due to a failure during kexec().

The related loop to call all callbacks is added to kexec().

arch/x86/mm.c +0 -0
index f4419d95..26ede6f4 100644

arch/x86/mm.c void change_readonly(bool readonly) · line 533

Quoted 6 lines
arch/x86/mm.c +2 -1
@@ -529,7 +529,8 @@ void change_readonly(bool readonly)
 #endif
     }
 
-    printk("setting %p-%p readonly\n", &_text, &_erodata);
+    printk("setting %p-%p %s\n", &_text, &_erodata,
+           readonly ? "readonly" : "writable");

Oh, I think this belongs in the earlier change.

With that moved, this one (and the earlier one still)

Code wise:

But this kexec_call() macro isn't actually used? xenstore needs this to prepare for kexec?

Regards,
Jason

#29 lore raw

Re: [MINI-OS PATCH 11/12] kexec: add kexec callback functionality

Quoted 20 lines · Jason Andryuk
Quoted 19 lines · Juergen Gross

Add a kexec_call() macro which will provide the capability to register a function for being called when doing a kexec() call. The called functions will be called with a boolean parameter "undo" indicating whether a previous call needs to be undone due to a failure during kexec().

The related loop to call all callbacks is added to kexec().

arch/x86/mm.c +0 -0
index f4419d95..26ede6f4 100644
@@ -529,7 +529,8 @@ void change_readonly(bool readonly)
  #endif
      }
 
Quoted 5 lines

arch/x86/mm.c void change_readonly(bool readonly) · line 533

Quoted 3 lines
arch/x86/mm.c +2 -1
-    printk("setting %p-%p readonly\n", &_text, &_erodata);
+    printk("setting %p-%p %s\n", &_text, &_erodata,
+           readonly ? "readonly" : "writable");

Oh, I think this belongs in the earlier change.

Indeed.

Quoted 8 lines

With that moved, this one (and the earlier one still)

Code wise:

But this kexec_call() macro isn't actually used? xenstore needs this to prepare for kexec?

This will be needed to e.g. handle FD_CLOEXEC.

Juergen

#30 lore raw

PATCH 12/12 kexec: do the final kexec step

In reply to Juergen Gross

With all kexec preparations done, activate the new kernel.

---
 kexec.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
kexec.c +15 -2
index 2db876e8..85b09959 100644
@@ -169,6 +169,7 @@ int kexec(void *kernel, unsigned long kernel_size, const char *cmdline)
 {
     int ret;
     unsigned long *func;
+    void *kexec_page;
 
     ret = analyze_kernel(kernel, kernel_size);
     if ( ret )
@@ -192,6 +193,13 @@ int kexec(void *kernel, unsigned long kernel_size, const char *cmdline)
     if ( ret )
         goto err;
 
+    kexec_page = (void *)alloc_page();
+    if ( !kexec_page )
+    {
+        ret = ENOMEM;
+        goto err;
+    }
+
     for ( func = __kexec_array_start; func < __kexec_array_end; func++ )
     {
         ret = ((kexeccall_t)(*func))(false);
@@ -204,10 +212,15 @@ int kexec(void *kernel, unsigned long kernel_size, const char *cmdline)
         }
     }
 
-    /* Error exit. */
-    ret = ENOSYS;
+    /* Activate the new kernel. */
+    do_kexec(kexec_page);
+
+    /* do_kexec() shouldn't return, crash. */
+    BUG();
 
  err:
+    if ( kexec_page )
+        free_page(kexec_page);
     change_readonly(true);
     unreserve_memory_below();
     kexec_move_used_pages_undo();
Signature · 1 line
2.43.0
#31 lore raw

Re: [MINI-OS PATCH 12/12] kexec: do the final kexec step

Quoted 15 lines · Juergen Gross

With all kexec preparations done, activate the new kernel.

---
 kexec.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
kexec.c +0 -0
index 2db876e8..85b09959 100644
@@ -169,6 +169,7 @@ int kexec(void *kernel, unsigned long kernel_size, const char *cmdline)
 

kexec.c int kexec(void *kernel, unsigned long kernel_size, const char *cmdline) · line 196

Quoted 4 lines
kexec.c +1 -0
@@ -192,6 +193,13 @@ int kexec(void *kernel, unsigned long kernel_size, const char *cmdline)
     if ( ret )
         goto err;
 
+    kexec_page = (void *)alloc_page();

kexec_page() is referenced already in do_kexec(), but it hasn't been hooked up yet, right? I guess that is okay.

If not an ASSERT on 1 page, then allocate KEXEC_SECSIZE?

Regards,
Jason

#32 lore raw

Re: [MINI-OS PATCH 12/12] kexec: do the final kexec step

Quoted 15 lines · Jason Andryuk
Quoted 13 lines · Juergen Gross

With all kexec preparations done, activate the new kernel.

---
  kexec.c | 17 +++++++++++++++--
  1 file changed, 15 insertions(+), 2 deletions(-)
kexec.c +0 -0
index 2db876e8..85b09959 100644
@@ -169,6 +169,7 @@ int kexec(void *kernel, unsigned long kernel_size, const char *cmdline)
Quoted 8 lines

kexec.c int kexec(void *kernel, unsigned long kernel_size, const char *cmdline) · line 196

Quoted 5 lines
kexec.c +1 -0
@@ -192,6 +193,13 @@ int kexec(void *kernel, unsigned long kernel_size, const char *cmdline)
      if ( ret )
          goto err;
 
+    kexec_page = (void *)alloc_page();

kexec_page() is referenced already in do_kexec(), but it hasn't been hooked up yet, right? I guess that is okay.

Yes, shouldn't cause any issues.

Quoted 2 lines

If not an ASSERT on 1 page, then allocate KEXEC_SECSIZE?

Handled now via an ASSERT() in the linker script.

Juergen

#33 lore raw
In reply to Juergen Gross

Hello,

Juergen Gross, le ven. 21 mars 2025 10:24:39 +0100, a ecrit:

Quoted 1 line

Add basic kexec support to Mini-OS for running in x86 PVH mode.

I am wondering if you had considered using libxc to implement this? The original pv-grub1 is doing it (xen/stubdom/grub/kexec.c)

Samuel

#34 lore raw
Quoted 7 lines · Samuel Thibault

Hello,

Juergen Gross, le ven. 21 mars 2025 10:24:39 +0100, a ecrit:

Quoted 1 line

Add basic kexec support to Mini-OS for running in x86 PVH mode.

I am wondering if you had considered using libxc to implement this? The original pv-grub1 is doing it (xen/stubdom/grub/kexec.c)

The libxc (or more precise: libxenguest) usage is needed there only for construction of the PV specific parts like the initial page tables and the p2m map. The main kexec functionality as I need it for PVH stubdom is in grub itself.

Additionally I've worked hard to get rid of non-stable Xen libraries in Xenstore-stubdom. Adding them again just after removal would be weird.

Juergen

#35 lore raw
In reply to Juergen Gross

Ping?

Quoted 49 lines · Juergen Gross

Add basic kexec support to Mini-OS for running in x86 PVH mode.

With this series applied it is possible to activate another kernel from within Mini-OS.

Right now no Xen related teardown is done (so no reset of grant table, event channels, PV devices). These should be added via kexec callbacks which are added as a framework.

This is a major building block for support of Xenstore-stubdom live update (in fact I've tested the kexec path to work using the PVH variant of Xenstore-stubdom).

Juergen Gross (12):
add kexec framework
Mini-OS: add final kexec stage
mini-os: add elf.h
mini-os: analyze new kernel for kexec
mini-os: kexec: finalize parameter location and size
mini-os: reserve memory below boundary
mini-os: kexec: build parameters for new kernel
mini-os: kexec: move used pages away for new kernel
Mini-OS: mm: change set_readonly() to change_readonly()
Mini-OS: kexec: switch read-only area to be writable again
mini-os: kexec: add kexec callback functionality
mini-os: kexec: do the final kexec step

  Config.mk                  |   1 +
  Makefile                   |   1 +
  arch/x86/kexec.c           | 273 +++++++++++++++++++++++++++++
  arch/x86/minios-x86.lds.S  |  16 ++
  arch/x86/mm.c              | 238 ++++++++++++++++++++------
  arch/x86/testbuild/all-no  |   1 +
  arch/x86/testbuild/all-yes |   2 +
  arch/x86/testbuild/kexec   |   4 +
  arch/x86/x86_hvm.S         |  46 +++++
  include/elf.h              | 340 +++++++++++++++++++++++++++++++++++++
  include/kexec.h            |  63 +++++++
  include/mm.h               |   8 +
  include/x86/os.h           |   5 +
  kexec.c                    | 253 +++++++++++++++++++++++++++
  mm.c                       |  89 +++++++++-
  15 files changed, 1289 insertions(+), 51 deletions(-)

create mode 100644 arch/x86/kexec.c
create mode 100644 arch/x86/testbuild/kexec
create mode 100644 include/elf.h
create mode 100644 include/kexec.h
create mode 100644 kexec.c

#36 lore raw
Quoted 1 line · Juergen Gross

Ping?

I'd really appreciate some feedback.

Juergen

Quoted 52 lines · Juergen Gross
Quoted 49 lines

Add basic kexec support to Mini-OS for running in x86 PVH mode.

With this series applied it is possible to activate another kernel from within Mini-OS.

Right now no Xen related teardown is done (so no reset of grant table, event channels, PV devices). These should be added via kexec callbacks which are added as a framework.

This is a major building block for support of Xenstore-stubdom live update (in fact I've tested the kexec path to work using the PVH variant of Xenstore-stubdom).

Juergen Gross (12):
   add kexec framework
   Mini-OS: add final kexec stage
   mini-os: add elf.h
   mini-os: analyze new kernel for kexec
   mini-os: kexec: finalize parameter location and size
   mini-os: reserve memory below boundary
   mini-os: kexec: build parameters for new kernel
   mini-os: kexec: move used pages away for new kernel
   Mini-OS: mm: change set_readonly() to change_readonly()
   Mini-OS: kexec: switch read-only area to be writable again
   mini-os: kexec: add kexec callback functionality
   mini-os: kexec: do the final kexec step

  Config.mk                  |   1 +
  Makefile                   |   1 +
  arch/x86/kexec.c           | 273 +++++++++++++++++++++++++++++ arch/x86/minios-x86.lds.S  |  16 ++
  arch/x86/mm.c              | 238 ++++++++++++++++++++------ arch/x86/testbuild/all-no  |   1 + arch/x86/testbuild/all-yes |   2 + arch/x86/testbuild/kexec   |   4 +
  arch/x86/x86_hvm.S         |  46 +++++
  include/elf.h              | 340 +++++++++++++++++++++++++++++++++++++ include/kexec.h            |  63 +++++++
  include/mm.h               |   8 +
  include/x86/os.h           |   5 +
  kexec.c                    | 253 +++++++++++++++++++++++++++ mm.c                       |  89 +++++++++-

  15 files changed, 1289 insertions(+), 51 deletions(-)

  create mode 100644 arch/x86/kexec.c
  create mode 100644 arch/x86/testbuild/kexec
  create mode 100644 include/elf.h
  create mode 100644 include/kexec.h
  create mode 100644 kexec.c