kexec/i386: kexec page table code clean up - page table setup in C
PATCH 2/2 kexec/i386: kexec page table code clean up - page table setup in C
This patch transforms the kexec page tables setup code from assembler code to C code in machine_kexec_prepare. This improves readability and reduces code line number.
--- arch/x86/kernel/machine_kexec_32.c | 50 +++++++++++---- arch/x86/kernel/relocate_kernel_32.S | 114 ----------------------------------- include/asm-x86/kexec_32.h | 18 ----- 3 files changed, 40 insertions(+), 142 deletions(-)
arch/x86/kernel/machine_kexec_32.c +38 -12
@@ -86,6 +86,42 @@ static void free_page_tables(struct kimafree_page ((unsigned long )image->arch_kimage.pte1); } +static void page_table_set_one (pgd_t *pgd,pmd_t *pmd,pte_t *pte, +unsigned long vaddr,unsigned long paddr) +{ +pud_t *pud; + + pgd +=pgd_index (vaddr); +#ifdef CONFIG_X86_PAE +if (!(pgd_val (*pgd) & _PAGE_PRESENT)) +set_pgd (pgd,__pgd (__pa (pmd) | _PAGE_PRESENT)); +#endif + pud =pud_offset (pgd, vaddr); + pmd =pmd_offset (pud, vaddr); +if (!(pmd_val (*pmd) & _PAGE_PRESENT)) +set_pmd (pmd,__pmd (__pa (pte) | _PAGE_TABLE)); + pte =pte_offset_kernel (pmd, vaddr); +set_pte (pte,pfn_pte (paddr >>PAGE_SHIFT ,PAGE_KERNEL_EXEC )); +} + +static void prepare_page_tables (struct kimage *image) +{ +void *control_page; +pmd_t *pmd =0 ; + + control_page =page_address (image->control_code_page); +#ifdef CONFIG_X86_PAE + pmd = image->arch_kimage.pmd0; +#endif +page_table_set_one (image->arch_kimage.pgd, pmd, image->arch_kimage.pte0, + (unsigned long )relocate_kernel,__pa (control_page)); +#ifdef CONFIG_X86_PAE + pmd = image->arch_kimage.pmd1; +#endif +page_table_set_one (image->arch_kimage.pgd, pmd, image->arch_kimage.pte1, +__pa (control_page),__pa (control_page)); +} +/* * A architecture hook called to validate the * proposed image and prepare the control pages
@@ -98,6 +134,7 @@ static void free_page_tables(struct kima* later. * * - Allocate page tables +* - Setup page tables */ int machine_kexec_prepare (struct kimage *image) {
@@ -112,6 +149,7 @@ int machine_kexec_prepare(struct kimagefree_page_tables (image);return -ENOMEM ; } +prepare_page_tables (image);return 0 ; }
@@ -140,19 +178,7 @@ NORET_TYPE void machine_kexec(struct kimmemcpy (control_page, relocate_kernel,PAGE_SIZE ); page_list[PA_CONTROL_PAGE ] =__pa (control_page); - page_list[VA_CONTROL_PAGE ] = (unsigned long )relocate_kernel; page_list[PA_PGD ] =__pa (image->arch_kimage.pgd); - page_list[VA_PGD ] = (unsigned long )image->arch_kimage.pgd; -#ifdef CONFIG_X86_PAE - page_list[PA_PMD_0 ] =__pa (image->arch_kimage.pmd0); - page_list[VA_PMD_0 ] = (unsigned long )image->arch_kimage.pmd0; - page_list[PA_PMD_1 ] =__pa (image->arch_kimage.pmd1); - page_list[VA_PMD_1 ] = (unsigned long )image->arch_kimage.pmd1; -#endif - page_list[PA_PTE_0 ] =__pa (image->arch_kimage.pte0); - page_list[VA_PTE_0 ] = (unsigned long )image->arch_kimage.pte0; - page_list[PA_PTE_1 ] =__pa (image->arch_kimage.pte1); - page_list[VA_PTE_1 ] = (unsigned long )image->arch_kimage.pte1;/* The segment registers are funny things, they have both a * visible and an invisible part. Whenever the visible part is
arch/x86/kernel/relocate_kernel_32.S +0 -114
126 lines not on this page · raw message
include/asm-x86/kexec_32.h +2 -16
@@ -2,22 +2,8 @@#define _I386_KEXEC_H#define PA_CONTROL_PAGE 0 -#define VA_CONTROL_PAGE 1 -#define PA_PGD 2 -#define VA_PGD 3 -#define PA_PTE_0 4 -#define VA_PTE_0 5 -#define PA_PTE_1 6 -#define VA_PTE_1 7 -#ifdef CONFIG_X86_PAE -#define PA_PMD_0 8 -#define VA_PMD_0 9 -#define PA_PMD_1 10 -#define VA_PMD_1 11 -#define PAGES_NR 12 -#else -#define PAGES_NR 8 -#endif +#define PA_PGD 1 +#define PAGES_NR 2#ifndef __ASSEMBLY__
Quoted 4 lines · Ying
This patch transforms the kexec page tables setup code from asseumbler code to iC code in
machine_kexec_prepare. This improves readability and reduces code line number.
I think this will create issues for Xen. Initially page table setup was in C but Xen Guests could not modify the page tables. I think Xen folks implemented a hypercall where they passed all the page table pages and the control pages and then hypervisor executed the control page(which in turn setup the page tables). I think that's why page table setup code is on the control page in assembly.
You might want to go through Xen kexec implementation and dig through kexec mailing list archive.
CCing Magnus and Horms. They had done the page tables related changes for Xen.
Thanks
Vivek
Quoted 13 lines · Vivek Goyal
Quoted 4 lines · Ying
This patch transforms the kexec page tables setup code from asseumbler code to iC code in
machine_kexec_prepare. This improves readability and reduces code line number.I think this will create issues for Xen. Initially page table setup was in C but Xen Guests could not modify the page tables. I think Xen folks implemented a hypercall where they passed all the page table pages and the control pages and then hypervisor executed the control page(which in turn setup the page tables). I think that's why page table setup code is on the control page in assembly.
Quoted 2 lines
You might want to go through Xen kexec implementation and dig through kexec mailing list archive.
OK, I will check the Xen kexec implementation.
Quoted 2 lines
CCing Magnus and Horms. They had done the page tables related changes for Xen.
Best Regards,
Huang Ying
[ CCing Ian Campbell who handles much of the maintenance of kexec in Xen ]
Quoted 14 lines · Ying
Quoted 13 lines · Vivek Goyal
Quoted 4 lines · Ying
This patch transforms the kexec page tables setup code from asseumbler code to iC code in
machine_kexec_prepare. This improves readability and reduces code line number.I think this will create issues for Xen. Initially page table setup was in C but Xen Guests could not modify the page tables. I think Xen folks implemented a hypercall where they passed all the page table pages and the control pages and then hypervisor executed the control page(which in turn setup the page tables). I think that's why page table setup code is on the control page in assembly.
Quoted 7 lines
Quoted 2 lines
You might want to go through Xen kexec implementation and dig through kexec mailing list archive.
OK, I will check the Xen kexec implementation.
Quoted 2 lines
CCing Magnus and Horms. They had done the page tables related changes for Xen.
I think that potentially there is a problem here, though weather or not it manifests is another question.
In machine_kexec() a PAGE_SIZE blob of code starting at relocate_kernel gets saved. I think that previously x86 Linux excuded this saved blob, though the current code seems to execute relocate_kernel() directly - then again perhaps I'm confusing things with ia64 which still executes the saved blob.
In any case, in the case of xen, the hypervisor will execute this saved blob. So I think that the crux of the issue is that the blob contains all the instructions required to run relocate_kernel, then it should work from inside xen, if and if it doesn't, then i'll blow up.
The code that you have seems to satisfy this requirement, so relocate_kernel itself shouldn't be a problem. And as long as the preparation work does exacltly what the removed portions of relocate_kernel used to do, which it seems to, things should be fine.
That said, this certainly warrants testing :-)
I'm all in favour of this kind of consolodation,
so hopefully we can bend Xen's will if it doesn't work as is.
Speaking of which, I strongly suspect machine_kexec_32.c and machine_kexec_64.c an be consolidated a bit.
Signature · 1 line
Horms
Quoted 39 lines · Simon Horman
[ CCing Ian Campbell who handles much of the maintenance of kexec in Xen ]
Quoted 14 lines · Ying
Quoted 13 lines · Vivek Goyal
Quoted 4 lines · Ying
This patch transforms the kexec page tables setup code from asseumbler code to iC code in
machine_kexec_prepare. This improves readability and reduces code line number.I think this will create issues for Xen. Initially page table setup was in C but Xen Guests could not modify the page tables. I think Xen folks implemented a hypercall where they passed all the page table pages and the control pages and then hypervisor executed the control page(which in turn setup the page tables). I think that's why page table setup code is on the control page in assembly.
Quoted 7 lines
Quoted 2 lines
You might want to go through Xen kexec implementation and dig through kexec mailing list archive.
OK, I will check the Xen kexec implementation.
Quoted 2 lines
CCing Magnus and Horms. They had done the page tables related changes for Xen.
I think that potentially there is a problem here, though weather or not it manifests is another question.
In
machine_kexec()aPAGE_SIZEblob of code starting atrelocate_kernelgets saved. I think that previously x86 Linux excuded this saved blob, though the current code seems to executerelocate_kernel()directly - then again perhaps I'm confusing things with ia64 which still executes the saved blob.In any case, in the case of xen, the hypervisor will execute this saved blob. So I think that the crux of the issue is that the blob contains all the instructions required to run
relocate_kernel, then it should work from inside xen, if and if it doesn't, then i'll blow up.
Quoted 7 lines
The code that you have seems to satisfy this requirement, so
relocate_kernelitself shouldn't be a problem. And as long as the preparation work does exacltly what the removed portions ofrelocate_kernelused to do, which it seems to, things should be fine.That said, this certainly warrants testing :-)
In the absence of domain 0 or Xen kexec in the upstream kernel testing might be quite hard.
Quoted 2 lines
I'm all in favour of this kind of consolodation,
so hopefully we can bend Xen's will if it doesn't work as is.
Indeed, patch seem reasonable to me. Whatever issues this causes for Xen will be stuff that can be fixed once someone does the kexec for Xen work in the upstream kernel, I don't think it paints us into an inescapable corner.
Ian.