kexec: add kexec support to Mini-OS
v2 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).
Changes in V2:
- addressed comments
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 | 272 +++++++++++++++++++++++++++++++++ arch/x86/minios-x86.lds.S | 17 +++ 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 | 300 +++++++++++++++++++++++++++++++++++++ include/kexec.h | 63 ++++++++ include/mm.h | 8 + include/x86/os.h | 5 + kexec.c | 252 +++++++++++++++++++++++++++++++ mm.c | 89 ++++++++++- 15 files changed, 1248 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
PATCH v2 1/12 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.
---
V2:
- reword some comments (Jason Andryuk)
--- 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 | 60 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 76 insertions(+) create mode 100644 arch/x86/testbuild/kexec create mode 100644 include/kexec.h create mode 100644 kexec.c
Config.mk +1 -0
@@ -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
@@ -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
@@ -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
@@ -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 not implemented for PARAVIRT +CONFIG_KEXEC = n
arch/x86/testbuild/kexec new file +4 -0
@@ -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
@@ -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 +60 -0
60 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v2 2/12 Mini-OS: add final kexec stage
In reply to Juergen GrossAdd 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.
---
V2:
- add ASSERT() to linker script (Jason Andryuk)
- fix asm() input/output parameter bug (Jason Andryuk)
--- arch/x86/kexec.c | 107 ++++++++++++++++++++++++++++++++++++++ arch/x86/minios-x86.lds.S | 9 ++++ arch/x86/x86_hvm.S | 46 ++++++++++++++++ include/kexec.h | 27 ++++++++++ include/x86/os.h | 5 ++ kexec.c | 22 ++++++++ 6 files changed, 216 insertions(+) create mode 100644 arch/x86/kexec.c
arch/x86/kexec.c new file +107 -0
@@ -0,0 +1,107 @@ +/****************************************************************************** +* 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 +9 -0
@@ -87,6 +87,15 @@ SECTIONS _edata = .; /* End of data section */ + . = ALIGN(8); + _kexec_start = .; /* Kexec relocatable code/data */ + .kexec : { + *(.text.kexec) + *(.data.kexec) + } + _kexec_end = .; + ASSERT(_kexec_end - _kexec_start <= 4096, "kexec code/data > 1 page") + __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
@@ -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
@@ -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
@@ -58,3 +58,25 @@ int kexec(void *kernel, unsigned long kernel_size, const char *cmdline)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
Re: [MINI-OS PATCH v2 02/12] Mini-OS: add final kexec stage
Quoted 10 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.
Signed-off-by Juergen Gross <[email protected]>
Thanks,
Jason
PATCH v2 3/12 mini-os: add elf.h
In reply to Juergen GrossAdd 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.
---
V2:
- remove some unneeded defines (Jan Beulich)
- remove not needed cast (Jan Beulich)
--- include/elf.h | 300 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 300 insertions(+) create mode 100644 include/elf.h
include/elf.h new file +300 -0
@@ -0,0 +1,300 @@ +#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 */ +#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_386 3/* Intel 80386 */ +#define EM_PPC64 21/* PowerPC 64-bit */ +#define EM_ARM 40/* Advanced RISC Machines ARM */ +#define EM_X86_64 62/* AMD x86-64 architecture */ +#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 ptr + add; +} +#endif /* __ELF_H__ */
Signature · 1 line
2.43.0
PATCH v2 4/12 mini-os: analyze new kernel for kexec
In reply to Juergen GrossAnalyze 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
---
V2:
- rename check_notes_entry() (Jason Andryuk)
- rename kernel_entry to kernel_phys_entry (Jason Andryuk)
--- arch/x86/kexec.c | 92 +++++++++++++++++++++++++++++++++++++ include/kexec.h | 11 +++++ kexec.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 220 insertions(+)
arch/x86/kexec.c +92 -0
@@ -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_phys_entry = ~0UL ; +/* * Final stage of kexec. Copies all data to the final destinations, zeroes * .bss and activates new kernel.
@@ -104,4 +111,89 @@ void do_kexec(void *kexec_page) : :"m" (stack), "m" (actions), "m" (phys), "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 read_note_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_phys_entry = *(uint8_t *)val; +return ; +case 2 : + kernel_phys_entry = *(uint16_t *)val; +return ; +case 4 : + kernel_phys_entry = *(uint32_t *)val; +return ; +case 8 : + kernel_phys_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_phys_entry != ~0UL ) +return 0 ; + + notes_start =elf_ptr_add (ehdr,phdr_val (ehdr, phdr, p_offset)); + notes_len =phdr_val (ehdr, phdr, p_filesz); +read_note_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_phys_entry != ~0UL ) +return 0 ; + + notes_start =elf_ptr_add (ehdr,shdr_val (ehdr, shdr, sh_offset)); + notes_len =shdr_val (ehdr, shdr, sh_size); +read_note_entry (ehdr, notes_start, notes_len); + +return 0 ; +} + +bool kexec_arch_need_analyze_shdrs (void ) +{ +return kernel_phys_entry == ~0UL ; +}#endif /* CONFIG_KEXEC */
include/kexec.h +11 -0
@@ -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 +117 -0
131 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v2 5/12 mini-os: kexec: finalize parameter location and size
In reply to Juergen GrossFinalize 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
@@ -196,4 +196,19 @@ bool kexec_arch_need_analyze_shdrs(void) {return kernel_phys_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
@@ -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
@@ -172,6 +172,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
PATCH v2 6/12 mini-os: reserve memory below boundary
In reply to Juergen GrossIn 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
@@ -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
@@ -174,6 +174,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
PATCH v2 7/12 mini-os: kexec: build parameters for new kernel
In reply to Juergen GrossBuild 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
@@ -199,6 +199,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) {
@@ -211,4 +212,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_phys_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_phys_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
@@ -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
@@ -176,10 +176,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
PATCH v2 8/12 mini-os: kexec: move used pages away for new kernel
In reply to Juergen GrossCopying 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
@@ -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; + asmvolatile ("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; + asmvolatile ("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; + asmvolatile ("lgdt %0" : :"m" (gdt_ptr)); +free_page ((void *)kexec_gdt); + kexec_gdt =0 ; + } + +if ( kexec_idt ) + { + idt_ptr.base = (unsigned long )&idt; + asmvolatile ("lidt %0" : :"m" (idt_ptr)); +free_page ((void *)kexec_idt); + kexec_idt =0 ; + } +} +#endif
include/kexec.h +5 -0
@@ -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
@@ -183,11 +183,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
PATCH v2 9/12 Mini-OS: mm: change set_readonly() to change_readonly()
In reply to Juergen GrossRename 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
@@ -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
@@ -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
PATCH v2 10/12 Mini-OS: kexec: switch read-only area to be writable again
In reply to Juergen GrossIn order to allow writing the new kernel, make the readonly area covering current kernel text writable again.
---
V2:
- modify printk() statement (Jason Andryuk)
--- arch/x86/mm.c | 3 ++- kexec.c | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-)
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" );walk_pt (start_address, ro.etext, change_readonly_func, &ro);#ifdef CONFIG_PARAVIRT
kexec.c +3 -0
@@ -183,6 +183,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 )
@@ -192,6 +194,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
PATCH v2 11/12 mini-os: kexec: add kexec callback functionality
In reply to Juergen GrossAdd 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 ++++++++ include/kexec.h | 6 ++++++ kexec.c | 13 +++++++++++++ 3 files changed, 27 insertions(+)
arch/x86/minios-x86.lds.S +8 -0
@@ -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)
include/kexec.h +6 -0
@@ -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"))) = funcextern unsigned long kexec_last_addr;
kexec.c +13 -0
@@ -167,6 +167,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 )
@@ -190,6 +191,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
PATCH v2 12/12 mini-os: kexec: do the final kexec step
In reply to Juergen GrossWith all kexec preparations done, activate the new kernel.
--- kexec.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
kexec.c +15 -2
@@ -168,6 +168,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 )
@@ -191,6 +192,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);
@@ -203,10 +211,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
Re: [MINI-OS PATCH v2 12/12] mini-os: kexec: do the final kexec step
Quoted 3 lines · Juergen Gross
With all kexec preparations done, activate the new kernel.
Signed-off-by Juergen Gross <[email protected]>
Thanks,
Jason