kexec: Move segment code to assembly files
cover kexec: Move segment code to assembly files
kexec: Move segment code to assembly files
The following patches rearrange the lowlevel kexec code to perform idt, gdt and segment setup code in assembly on the code page instead of doing it in inline assembly in the C files.
Our dom0 Xen port of kexec and kdump executes the code page from the hypervisor when kexec:ing into a new kernel. Putting as much code as possible on the code page allows us to keep the amount of duplicated code low.
These patches are part of the Xen port of kexec and kdump which recently has been accepted into the xen-unstable.hg tree. Sending them upstream now is an attempt to simplify future porting work.
---
Applies to 2.6.19.
arch/i386/kernel/machine_kexec.c | 59 ---------------------------------- arch/i386/kernel/relocate_kernel.S | 58 ++++++++++++++++++++++++++++++--- arch/x86_64/kernel/machine_kexec.c | 58 --------------------------------- arch/x86_64/kernel/relocate_kernel.S | 50 +++++++++++++++++++++++++--- 4 files changed, 98 insertions(+), 127 deletions(-)
PATCH 1/2 kexec: Move segment code to assembly file (i386)
kexec: Move segment code to assembly file (i386)
This patch moves the idt, gdt, and segment handling code from machine_kexec.c to relocate_kernel.S. The main reason behind this move is to avoid code duplication in the Xen hypervisor. With this patch all code required to kexec is put on the control page.
---
Applies to 2.6.19.
arch/i386/kernel/machine_kexec.c | 59 ------------------------------------ arch/i386/kernel/relocate_kernel.S | 58 ++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 64 deletions(-)
arch/i386/kernel/machine_kexec.c +0 -59
@@ -29,48 +29,6 @@ static u32 kexec_pmd1[1024] PAGE_ALIGNED static u32 kexec_pte0[1024] PAGE_ALIGNED; static u32 kexec_pte1[1024] PAGE_ALIGNED; -static void set_idt (void *newidt,__u16 limit) -{ -struct Xgt_desc_struct curidt; - -/* ia32 supports unaliged loads & stores */ - curidt.size = limit; - curidt.address = (unsigned long )newidt; - -load_idt (&curidt); -}; - - -static void set_gdt (void *newgdt,__u16 limit) -{ -struct Xgt_desc_struct curgdt; - -/* ia32 supports unaligned loads & stores */ - curgdt.size = limit; - curgdt.address = (unsigned long )newgdt; - -load_gdt (&curgdt); -}; - -static void load_segments (void ) -{ -#define __STR (X ) #X -#define STR (X ) __STR(X) - - __asm__ __volatile__ ( - "\tljmp $"STR(__KERNEL_CS)",$1f\n" -"\t1:\n" - "\tmovl $"STR(__KERNEL_DS)",%%eax\n" -"\tmovl %%eax,%%ds\n" -"\tmovl %%eax,%%es\n" -"\tmovl %%eax,%%fs\n" -"\tmovl %%eax,%%gs\n" -"\tmovl %%eax,%%ss\n" - ::: "eax", "memory"); -#undef STR -#undef __STR -} -/* * A architecture hook called to validate the * proposed image and prepare the control pages
@@ -127,23 +85,6 @@ NORET_TYPE void machine_kexec(struct kim page_list[PA_PTE_1 ] =__pa (kexec_pte1); page_list[VA_PTE_1 ] = (unsigned long )kexec_pte1; -/* The segment registers are funny things, they have both a -* visible and an invisible part. Whenever the visible part is -* set to a specific selector, the invisible part is loaded -* with from a table in memory. At no other time is the -* descriptor table in memory accessed. -* -* I take advantage of this here by force loading the -* segments, before I zap the gdt with an invalid value. -*/ -load_segments (); -/* The gdt & idt are now invalid. -* If you want to load them you must set up your own idt & gdt. -*/ -set_gdt (phys_to_virt (0 ),0 ); -set_idt (phys_to_virt (0 ),0 ); - -/* now call it */ relocate_kernel ((unsigned long )image->head, (unsigned long )page_list, image->start, cpu_has_pae); }
arch/i386/kernel/relocate_kernel.S +53 -5
70 lines not on this page · raw message
PATCH 2/2 kexec: Move segment code to assembly file (x86_64)
In reply to Magnus Dammkexec: Move segment code to assembly file (x86_64)
This patch moves the idt, gdt, and segment handling code from machine_kexec.c to relocate_kernel.S. The main reason behind this move is to avoid code duplication in the Xen hypervisor. With this patch all code required to kexec is put on the control page.
---
Applies to 2.6.19.
arch/x86_64/kernel/machine_kexec.c | 58 ---------------------------------- arch/x86_64/kernel/relocate_kernel.S | 50 ++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 63 deletions(-)
arch/x86_64/kernel/machine_kexec.c +0 -58
@@ -112,47 +112,6 @@ static int init_pgtable(struct kimage *ireturn init_level4_page (image, level4p,0 , end_pfn <<PAGE_SHIFT ); } -static void set_idt (void *newidt,u16 limit) -{ -struct desc_ptr curidt; - -/* x86-64 supports unaliged loads & stores */ - curidt.size = limit; - curidt.address = (unsigned long )newidt; - - __asm__ __volatile__ ( -"lidtq %0\n" - : :"m" (curidt) - ); -}; - - -static void set_gdt (void *newgdt,u16 limit) -{ -struct desc_ptr curgdt; - -/* x86-64 supports unaligned loads & stores */ - curgdt.size = limit; - curgdt.address = (unsigned long )newgdt; - - __asm__ __volatile__ ( -"lgdtq %0\n" - : :"m" (curgdt) - ); -}; - -static void load_segments (void ) -{ - __asm__ __volatile__ ( -"\tmovl %0,%%ds\n" -"\tmovl %0,%%es\n" -"\tmovl %0,%%ss\n" -"\tmovl %0,%%fs\n" -"\tmovl %0,%%gs\n" - : :"a" (__KERNEL_DS) :"memory" - ); -} -int machine_kexec_prepare (struct kimage *image) {unsigned long start_pgtable;
@@ -209,23 +168,6 @@ NORET_TYPE void machine_kexec(struct kim page_list[PA_TABLE_PAGE ] = (unsigned long )__pa (page_address (image->control_code_page)); -/* The segment registers are funny things, they have both a -* visible and an invisible part. Whenever the visible part is -* set to a specific selector, the invisible part is loaded -* with from a table in memory. At no other time is the -* descriptor table in memory accessed. -* -* I take advantage of this here by force loading the -* segments, before I zap the gdt with an invalid value. -*/ -load_segments (); -/* The gdt & idt are now invalid. -* If you want to load them you must set up your own idt & gdt. -*/ -set_gdt (phys_to_virt (0 ),0 ); -set_idt (phys_to_virt (0 ),0 ); - -/* now call it */ relocate_kernel ((unsigned long )image->head, (unsigned long )page_list, image->start); }
arch/x86_64/kernel/relocate_kernel.S +45 -5
63 lines not on this page · raw message
Quoted 6 lines · Magnus Damm
kexec: Move segment code to assembly files
The following patches rearrange the lowlevel kexec code to perform idt, gdt and segment setup code in assembly on the code page instead of doing it in inline assembly in the C files.
I don't think we should be doing this. I would rather prefer code to keep in C for easier debugging, readability and maintenance.
Quoted 5 lines
Our dom0 Xen port of kexec and kdump executes the code page from the hypervisor when kexec:ing into a new kernel. Putting as much code as possible on the code page allows us to keep the amount of duplicated code low.
Is Xen going upstream now? I heard now lhype+KVM seems to be the way. Even if it is required, we should do it once Xen goes in.
You have already moved page table setup code to assembly and we should be getting rid of that code too.
I would rather live with duplicated code than moving more code in assembly which can be written in C. Understanding and debugging assembly code is such a big pain.
Thanks
Vivek
Quoted 10 lines · Vivek Goyal
Quoted 6 lines · Magnus Damm
kexec: Move segment code to assembly files
The following patches rearrange the lowlevel kexec code to perform idt, gdt and segment setup code in assembly on the code page instead of doing it in inline assembly in the C files.
I don't think we should be doing this. I would rather prefer code to keep in C for easier debugging, readability and maintenance.
I prefer to write code in C too, but I don't see how wrapping assembly instructions in inline C makes the code any easier compared to raw assembly. Either you understand the assembly or you don't.
Quoted 8 lines
Quoted 5 lines
Our dom0 Xen port of kexec and kdump executes the code page from the hypervisor when kexec:ing into a new kernel. Putting as much code as possible on the code page allows us to keep the amount of duplicated code low.
Is Xen going upstream now? I heard now lhype+KVM seems to be the way. Even if it is required, we should do it once Xen goes in.
I am not sure about status of the Xen merging effort. domU seemed to be the top priority last time I heard something, but this change only affects dom0 so it is probably even further away.
Quoted 2 lines
You have already moved page table setup code to assembly and we should be getting rid of that code too.
This was recommended to me by Eric if I'm not mistaken, but if we can move out parts of the assembly code to C then that would be great.
Quoted 3 lines
I would rather live with duplicated code than moving more code in assembly which can be written in C. Understanding and debugging assembly code is such a big pain.
Again, I think that is true for C code - not for inline assembly in C files. But I guess you are talking about the already merged page table a patches. My first version implemented the code in C, have a look at the function create_mapping() which I think is very clear:
http://lists.osdl.org/pipermail/fastboot/2006-May/002838.html
The important question IMO is if this should be merged ahead of the rest of the Xen stuff, and maybe it shouldn't.
Thanks,
/ magnus