Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * handle transition of Linux booting another kernel
0004  * Copyright (C) 2002-2005 Eric Biederman  <ebiederm@xmission.com>
0005  */
0006 
0007 #include <linux/mm.h>
0008 #include <linux/kexec.h>
0009 #include <linux/delay.h>
0010 #include <linux/numa.h>
0011 #include <linux/ftrace.h>
0012 #include <linux/suspend.h>
0013 #include <linux/gfp.h>
0014 #include <linux/io.h>
0015 
0016 #include <asm/pgalloc.h>
0017 #include <asm/tlbflush.h>
0018 #include <asm/mmu_context.h>
0019 #include <asm/apic.h>
0020 #include <asm/io_apic.h>
0021 #include <asm/cpufeature.h>
0022 #include <asm/desc.h>
0023 #include <asm/set_memory.h>
0024 #include <asm/debugreg.h>
0025 
0026 static void load_segments(void)
0027 {
0028 #define __STR(X) #X
0029 #define STR(X) __STR(X)
0030 
0031     __asm__ __volatile__ (
0032         "\tljmp $"STR(__KERNEL_CS)",$1f\n"
0033         "\t1:\n"
0034         "\tmovl $"STR(__KERNEL_DS)",%%eax\n"
0035         "\tmovl %%eax,%%ds\n"
0036         "\tmovl %%eax,%%es\n"
0037         "\tmovl %%eax,%%ss\n"
0038         : : : "eax", "memory");
0039 #undef STR
0040 #undef __STR
0041 }
0042 
0043 static void machine_kexec_free_page_tables(struct kimage *image)
0044 {
0045     free_pages((unsigned long)image->arch.pgd, PGD_ALLOCATION_ORDER);
0046     image->arch.pgd = NULL;
0047 #ifdef CONFIG_X86_PAE
0048     free_page((unsigned long)image->arch.pmd0);
0049     image->arch.pmd0 = NULL;
0050     free_page((unsigned long)image->arch.pmd1);
0051     image->arch.pmd1 = NULL;
0052 #endif
0053     free_page((unsigned long)image->arch.pte0);
0054     image->arch.pte0 = NULL;
0055     free_page((unsigned long)image->arch.pte1);
0056     image->arch.pte1 = NULL;
0057 }
0058 
0059 static int machine_kexec_alloc_page_tables(struct kimage *image)
0060 {
0061     image->arch.pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
0062                             PGD_ALLOCATION_ORDER);
0063 #ifdef CONFIG_X86_PAE
0064     image->arch.pmd0 = (pmd_t *)get_zeroed_page(GFP_KERNEL);
0065     image->arch.pmd1 = (pmd_t *)get_zeroed_page(GFP_KERNEL);
0066 #endif
0067     image->arch.pte0 = (pte_t *)get_zeroed_page(GFP_KERNEL);
0068     image->arch.pte1 = (pte_t *)get_zeroed_page(GFP_KERNEL);
0069     if (!image->arch.pgd ||
0070 #ifdef CONFIG_X86_PAE
0071         !image->arch.pmd0 || !image->arch.pmd1 ||
0072 #endif
0073         !image->arch.pte0 || !image->arch.pte1) {
0074         return -ENOMEM;
0075     }
0076     return 0;
0077 }
0078 
0079 static void machine_kexec_page_table_set_one(
0080     pgd_t *pgd, pmd_t *pmd, pte_t *pte,
0081     unsigned long vaddr, unsigned long paddr)
0082 {
0083     p4d_t *p4d;
0084     pud_t *pud;
0085 
0086     pgd += pgd_index(vaddr);
0087 #ifdef CONFIG_X86_PAE
0088     if (!(pgd_val(*pgd) & _PAGE_PRESENT))
0089         set_pgd(pgd, __pgd(__pa(pmd) | _PAGE_PRESENT));
0090 #endif
0091     p4d = p4d_offset(pgd, vaddr);
0092     pud = pud_offset(p4d, vaddr);
0093     pmd = pmd_offset(pud, vaddr);
0094     if (!(pmd_val(*pmd) & _PAGE_PRESENT))
0095         set_pmd(pmd, __pmd(__pa(pte) | _PAGE_TABLE));
0096     pte = pte_offset_kernel(pmd, vaddr);
0097     set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL_EXEC));
0098 }
0099 
0100 static void machine_kexec_prepare_page_tables(struct kimage *image)
0101 {
0102     void *control_page;
0103     pmd_t *pmd = NULL;
0104 
0105     control_page = page_address(image->control_code_page);
0106 #ifdef CONFIG_X86_PAE
0107     pmd = image->arch.pmd0;
0108 #endif
0109     machine_kexec_page_table_set_one(
0110         image->arch.pgd, pmd, image->arch.pte0,
0111         (unsigned long)control_page, __pa(control_page));
0112 #ifdef CONFIG_X86_PAE
0113     pmd = image->arch.pmd1;
0114 #endif
0115     machine_kexec_page_table_set_one(
0116         image->arch.pgd, pmd, image->arch.pte1,
0117         __pa(control_page), __pa(control_page));
0118 }
0119 
0120 /*
0121  * A architecture hook called to validate the
0122  * proposed image and prepare the control pages
0123  * as needed.  The pages for KEXEC_CONTROL_PAGE_SIZE
0124  * have been allocated, but the segments have yet
0125  * been copied into the kernel.
0126  *
0127  * Do what every setup is needed on image and the
0128  * reboot code buffer to allow us to avoid allocations
0129  * later.
0130  *
0131  * - Make control page executable.
0132  * - Allocate page tables
0133  * - Setup page tables
0134  */
0135 int machine_kexec_prepare(struct kimage *image)
0136 {
0137     int error;
0138 
0139     set_memory_x((unsigned long)page_address(image->control_code_page), 1);
0140     error = machine_kexec_alloc_page_tables(image);
0141     if (error)
0142         return error;
0143     machine_kexec_prepare_page_tables(image);
0144     return 0;
0145 }
0146 
0147 /*
0148  * Undo anything leftover by machine_kexec_prepare
0149  * when an image is freed.
0150  */
0151 void machine_kexec_cleanup(struct kimage *image)
0152 {
0153     set_memory_nx((unsigned long)page_address(image->control_code_page), 1);
0154     machine_kexec_free_page_tables(image);
0155 }
0156 
0157 /*
0158  * Do not allocate memory (or fail in any way) in machine_kexec().
0159  * We are past the point of no return, committed to rebooting now.
0160  */
0161 void machine_kexec(struct kimage *image)
0162 {
0163     unsigned long page_list[PAGES_NR];
0164     void *control_page;
0165     int save_ftrace_enabled;
0166     asmlinkage unsigned long
0167         (*relocate_kernel_ptr)(unsigned long indirection_page,
0168                        unsigned long control_page,
0169                        unsigned long start_address,
0170                        unsigned int has_pae,
0171                        unsigned int preserve_context);
0172 
0173 #ifdef CONFIG_KEXEC_JUMP
0174     if (image->preserve_context)
0175         save_processor_state();
0176 #endif
0177 
0178     save_ftrace_enabled = __ftrace_enabled_save();
0179 
0180     /* Interrupts aren't acceptable while we reboot */
0181     local_irq_disable();
0182     hw_breakpoint_disable();
0183 
0184     if (image->preserve_context) {
0185 #ifdef CONFIG_X86_IO_APIC
0186         /*
0187          * We need to put APICs in legacy mode so that we can
0188          * get timer interrupts in second kernel. kexec/kdump
0189          * paths already have calls to restore_boot_irq_mode()
0190          * in one form or other. kexec jump path also need one.
0191          */
0192         clear_IO_APIC();
0193         restore_boot_irq_mode();
0194 #endif
0195     }
0196 
0197     control_page = page_address(image->control_code_page);
0198     memcpy(control_page, relocate_kernel, KEXEC_CONTROL_CODE_MAX_SIZE);
0199 
0200     relocate_kernel_ptr = control_page;
0201     page_list[PA_CONTROL_PAGE] = __pa(control_page);
0202     page_list[VA_CONTROL_PAGE] = (unsigned long)control_page;
0203     page_list[PA_PGD] = __pa(image->arch.pgd);
0204 
0205     if (image->type == KEXEC_TYPE_DEFAULT)
0206         page_list[PA_SWAP_PAGE] = (page_to_pfn(image->swap_page)
0207                         << PAGE_SHIFT);
0208 
0209     /*
0210      * The segment registers are funny things, they have both a
0211      * visible and an invisible part.  Whenever the visible part is
0212      * set to a specific selector, the invisible part is loaded
0213      * with from a table in memory.  At no other time is the
0214      * descriptor table in memory accessed.
0215      *
0216      * I take advantage of this here by force loading the
0217      * segments, before I zap the gdt with an invalid value.
0218      */
0219     load_segments();
0220     /*
0221      * The gdt & idt are now invalid.
0222      * If you want to load them you must set up your own idt & gdt.
0223      */
0224     native_idt_invalidate();
0225     native_gdt_invalidate();
0226 
0227     /* now call it */
0228     image->start = relocate_kernel_ptr((unsigned long)image->head,
0229                        (unsigned long)page_list,
0230                        image->start,
0231                        boot_cpu_has(X86_FEATURE_PAE),
0232                        image->preserve_context);
0233 
0234 #ifdef CONFIG_KEXEC_JUMP
0235     if (image->preserve_context)
0236         restore_processor_state();
0237 #endif
0238 
0239     __ftrace_enabled_restore(save_ftrace_enabled);
0240 }