OSDN Git Service

c51d2cf27d9381347c72af2b4b86ebc66ecdf862
[uclinux-h8/linux.git] / arch / x86 / kernel / machine_kexec_64.c
1 /*
2  * handle transition of Linux booting another kernel
3  * Copyright (C) 2002-2005 Eric Biederman  <ebiederm@xmission.com>
4  *
5  * This source code is licensed under the GNU General Public License,
6  * Version 2.  See the file COPYING for more details.
7  */
8
9 #define pr_fmt(fmt)     "kexec: " fmt
10
11 #include <linux/mm.h>
12 #include <linux/kexec.h>
13 #include <linux/string.h>
14 #include <linux/gfp.h>
15 #include <linux/reboot.h>
16 #include <linux/numa.h>
17 #include <linux/ftrace.h>
18 #include <linux/io.h>
19 #include <linux/suspend.h>
20 #include <linux/vmalloc.h>
21
22 #include <asm/init.h>
23 #include <asm/pgtable.h>
24 #include <asm/tlbflush.h>
25 #include <asm/mmu_context.h>
26 #include <asm/io_apic.h>
27 #include <asm/debugreg.h>
28 #include <asm/kexec-bzimage64.h>
29 #include <asm/setup.h>
30 #include <asm/set_memory.h>
31
32 #ifdef CONFIG_KEXEC_FILE
33 const struct kexec_file_ops * const kexec_file_loaders[] = {
34                 &kexec_bzImage64_ops,
35                 NULL
36 };
37 #endif
38
39 static void free_transition_pgtable(struct kimage *image)
40 {
41         free_page((unsigned long)image->arch.p4d);
42         free_page((unsigned long)image->arch.pud);
43         free_page((unsigned long)image->arch.pmd);
44         free_page((unsigned long)image->arch.pte);
45 }
46
47 static int init_transition_pgtable(struct kimage *image, pgd_t *pgd)
48 {
49         p4d_t *p4d;
50         pud_t *pud;
51         pmd_t *pmd;
52         pte_t *pte;
53         unsigned long vaddr, paddr;
54         int result = -ENOMEM;
55
56         vaddr = (unsigned long)relocate_kernel;
57         paddr = __pa(page_address(image->control_code_page)+PAGE_SIZE);
58         pgd += pgd_index(vaddr);
59         if (!pgd_present(*pgd)) {
60                 p4d = (p4d_t *)get_zeroed_page(GFP_KERNEL);
61                 if (!p4d)
62                         goto err;
63                 image->arch.p4d = p4d;
64                 set_pgd(pgd, __pgd(__pa(p4d) | _KERNPG_TABLE));
65         }
66         p4d = p4d_offset(pgd, vaddr);
67         if (!p4d_present(*p4d)) {
68                 pud = (pud_t *)get_zeroed_page(GFP_KERNEL);
69                 if (!pud)
70                         goto err;
71                 image->arch.pud = pud;
72                 set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE));
73         }
74         pud = pud_offset(p4d, vaddr);
75         if (!pud_present(*pud)) {
76                 pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL);
77                 if (!pmd)
78                         goto err;
79                 image->arch.pmd = pmd;
80                 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
81         }
82         pmd = pmd_offset(pud, vaddr);
83         if (!pmd_present(*pmd)) {
84                 pte = (pte_t *)get_zeroed_page(GFP_KERNEL);
85                 if (!pte)
86                         goto err;
87                 image->arch.pte = pte;
88                 set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE));
89         }
90         pte = pte_offset_kernel(pmd, vaddr);
91         set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL_EXEC_NOENC));
92         return 0;
93 err:
94         free_transition_pgtable(image);
95         return result;
96 }
97
98 static void *alloc_pgt_page(void *data)
99 {
100         struct kimage *image = (struct kimage *)data;
101         struct page *page;
102         void *p = NULL;
103
104         page = kimage_alloc_control_pages(image, 0);
105         if (page) {
106                 p = page_address(page);
107                 clear_page(p);
108         }
109
110         return p;
111 }
112
113 static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
114 {
115         struct x86_mapping_info info = {
116                 .alloc_pgt_page = alloc_pgt_page,
117                 .context        = image,
118                 .page_flag      = __PAGE_KERNEL_LARGE_EXEC,
119                 .kernpg_flag    = _KERNPG_TABLE_NOENC,
120         };
121         unsigned long mstart, mend;
122         pgd_t *level4p;
123         int result;
124         int i;
125
126         level4p = (pgd_t *)__va(start_pgtable);
127         clear_page(level4p);
128
129         if (direct_gbpages)
130                 info.direct_gbpages = true;
131
132         for (i = 0; i < nr_pfn_mapped; i++) {
133                 mstart = pfn_mapped[i].start << PAGE_SHIFT;
134                 mend   = pfn_mapped[i].end << PAGE_SHIFT;
135
136                 result = kernel_ident_mapping_init(&info,
137                                                  level4p, mstart, mend);
138                 if (result)
139                         return result;
140         }
141
142         /*
143          * segments's mem ranges could be outside 0 ~ max_pfn,
144          * for example when jump back to original kernel from kexeced kernel.
145          * or first kernel is booted with user mem map, and second kernel
146          * could be loaded out of that range.
147          */
148         for (i = 0; i < image->nr_segments; i++) {
149                 mstart = image->segment[i].mem;
150                 mend   = mstart + image->segment[i].memsz;
151
152                 result = kernel_ident_mapping_init(&info,
153                                                  level4p, mstart, mend);
154
155                 if (result)
156                         return result;
157         }
158
159         return init_transition_pgtable(image, level4p);
160 }
161
162 static void set_idt(void *newidt, u16 limit)
163 {
164         struct desc_ptr curidt;
165
166         /* x86-64 supports unaliged loads & stores */
167         curidt.size    = limit;
168         curidt.address = (unsigned long)newidt;
169
170         __asm__ __volatile__ (
171                 "lidtq %0\n"
172                 : : "m" (curidt)
173                 );
174 };
175
176
177 static void set_gdt(void *newgdt, u16 limit)
178 {
179         struct desc_ptr curgdt;
180
181         /* x86-64 supports unaligned loads & stores */
182         curgdt.size    = limit;
183         curgdt.address = (unsigned long)newgdt;
184
185         __asm__ __volatile__ (
186                 "lgdtq %0\n"
187                 : : "m" (curgdt)
188                 );
189 };
190
191 static void load_segments(void)
192 {
193         __asm__ __volatile__ (
194                 "\tmovl %0,%%ds\n"
195                 "\tmovl %0,%%es\n"
196                 "\tmovl %0,%%ss\n"
197                 "\tmovl %0,%%fs\n"
198                 "\tmovl %0,%%gs\n"
199                 : : "a" (__KERNEL_DS) : "memory"
200                 );
201 }
202
203 #ifdef CONFIG_KEXEC_FILE
204 /* Update purgatory as needed after various image segments have been prepared */
205 static int arch_update_purgatory(struct kimage *image)
206 {
207         int ret = 0;
208
209         if (!image->file_mode)
210                 return 0;
211
212         /* Setup copying of backup region */
213         if (image->type == KEXEC_TYPE_CRASH) {
214                 ret = kexec_purgatory_get_set_symbol(image,
215                                 "purgatory_backup_dest",
216                                 &image->arch.backup_load_addr,
217                                 sizeof(image->arch.backup_load_addr), 0);
218                 if (ret)
219                         return ret;
220
221                 ret = kexec_purgatory_get_set_symbol(image,
222                                 "purgatory_backup_src",
223                                 &image->arch.backup_src_start,
224                                 sizeof(image->arch.backup_src_start), 0);
225                 if (ret)
226                         return ret;
227
228                 ret = kexec_purgatory_get_set_symbol(image,
229                                 "purgatory_backup_sz",
230                                 &image->arch.backup_src_sz,
231                                 sizeof(image->arch.backup_src_sz), 0);
232                 if (ret)
233                         return ret;
234         }
235
236         return ret;
237 }
238 #else /* !CONFIG_KEXEC_FILE */
239 static inline int arch_update_purgatory(struct kimage *image)
240 {
241         return 0;
242 }
243 #endif /* CONFIG_KEXEC_FILE */
244
245 int machine_kexec_prepare(struct kimage *image)
246 {
247         unsigned long start_pgtable;
248         int result;
249
250         /* Calculate the offsets */
251         start_pgtable = page_to_pfn(image->control_code_page) << PAGE_SHIFT;
252
253         /* Setup the identity mapped 64bit page table */
254         result = init_pgtable(image, start_pgtable);
255         if (result)
256                 return result;
257
258         /* update purgatory as needed */
259         result = arch_update_purgatory(image);
260         if (result)
261                 return result;
262
263         return 0;
264 }
265
266 void machine_kexec_cleanup(struct kimage *image)
267 {
268         free_transition_pgtable(image);
269 }
270
271 /*
272  * Do not allocate memory (or fail in any way) in machine_kexec().
273  * We are past the point of no return, committed to rebooting now.
274  */
275 void machine_kexec(struct kimage *image)
276 {
277         unsigned long page_list[PAGES_NR];
278         void *control_page;
279         int save_ftrace_enabled;
280
281 #ifdef CONFIG_KEXEC_JUMP
282         if (image->preserve_context)
283                 save_processor_state();
284 #endif
285
286         save_ftrace_enabled = __ftrace_enabled_save();
287
288         /* Interrupts aren't acceptable while we reboot */
289         local_irq_disable();
290         hw_breakpoint_disable();
291
292         if (image->preserve_context) {
293 #ifdef CONFIG_X86_IO_APIC
294                 /*
295                  * We need to put APICs in legacy mode so that we can
296                  * get timer interrupts in second kernel. kexec/kdump
297                  * paths already have calls to restore_boot_irq_mode()
298                  * in one form or other. kexec jump path also need one.
299                  */
300                 clear_IO_APIC();
301                 restore_boot_irq_mode();
302 #endif
303         }
304
305         control_page = page_address(image->control_code_page) + PAGE_SIZE;
306         memcpy(control_page, relocate_kernel, KEXEC_CONTROL_CODE_MAX_SIZE);
307
308         page_list[PA_CONTROL_PAGE] = virt_to_phys(control_page);
309         page_list[VA_CONTROL_PAGE] = (unsigned long)control_page;
310         page_list[PA_TABLE_PAGE] =
311           (unsigned long)__pa(page_address(image->control_code_page));
312
313         if (image->type == KEXEC_TYPE_DEFAULT)
314                 page_list[PA_SWAP_PAGE] = (page_to_pfn(image->swap_page)
315                                                 << PAGE_SHIFT);
316
317         /*
318          * The segment registers are funny things, they have both a
319          * visible and an invisible part.  Whenever the visible part is
320          * set to a specific selector, the invisible part is loaded
321          * with from a table in memory.  At no other time is the
322          * descriptor table in memory accessed.
323          *
324          * I take advantage of this here by force loading the
325          * segments, before I zap the gdt with an invalid value.
326          */
327         load_segments();
328         /*
329          * The gdt & idt are now invalid.
330          * If you want to load them you must set up your own idt & gdt.
331          */
332         set_gdt(phys_to_virt(0), 0);
333         set_idt(phys_to_virt(0), 0);
334
335         /* now call it */
336         image->start = relocate_kernel((unsigned long)image->head,
337                                        (unsigned long)page_list,
338                                        image->start,
339                                        image->preserve_context,
340                                        sme_active());
341
342 #ifdef CONFIG_KEXEC_JUMP
343         if (image->preserve_context)
344                 restore_processor_state();
345 #endif
346
347         __ftrace_enabled_restore(save_ftrace_enabled);
348 }
349
350 void arch_crash_save_vmcoreinfo(void)
351 {
352         VMCOREINFO_NUMBER(phys_base);
353         VMCOREINFO_SYMBOL(init_top_pgt);
354         VMCOREINFO_NUMBER(pgtable_l5_enabled);
355
356 #ifdef CONFIG_NUMA
357         VMCOREINFO_SYMBOL(node_data);
358         VMCOREINFO_LENGTH(node_data, MAX_NUMNODES);
359 #endif
360         vmcoreinfo_append_str("KERNELOFFSET=%lx\n",
361                               kaslr_offset());
362         VMCOREINFO_NUMBER(KERNEL_IMAGE_SIZE);
363 }
364
365 /* arch-dependent functionality related to kexec file-based syscall */
366
367 #ifdef CONFIG_KEXEC_FILE
368 void *arch_kexec_kernel_image_load(struct kimage *image)
369 {
370         vfree(image->arch.elf_headers);
371         image->arch.elf_headers = NULL;
372
373         if (!image->fops || !image->fops->load)
374                 return ERR_PTR(-ENOEXEC);
375
376         return image->fops->load(image, image->kernel_buf,
377                                  image->kernel_buf_len, image->initrd_buf,
378                                  image->initrd_buf_len, image->cmdline_buf,
379                                  image->cmdline_buf_len);
380 }
381
382 /*
383  * Apply purgatory relocations.
384  *
385  * ehdr: Pointer to elf headers
386  * sechdrs: Pointer to section headers.
387  * relsec: section index of SHT_RELA section.
388  *
389  * TODO: Some of the code belongs to generic code. Move that in kexec.c.
390  */
391 int arch_kexec_apply_relocations_add(const Elf64_Ehdr *ehdr,
392                                      Elf64_Shdr *sechdrs, unsigned int relsec)
393 {
394         unsigned int i;
395         Elf64_Rela *rel;
396         Elf64_Sym *sym;
397         void *location;
398         Elf64_Shdr *section, *symtabsec;
399         unsigned long address, sec_base, value;
400         const char *strtab, *name, *shstrtab;
401
402         /*
403          * ->sh_offset has been modified to keep the pointer to section
404          * contents in memory
405          */
406         rel = (void *)sechdrs[relsec].sh_offset;
407
408         /* Section to which relocations apply */
409         section = &sechdrs[sechdrs[relsec].sh_info];
410
411         pr_debug("Applying relocate section %u to %u\n", relsec,
412                  sechdrs[relsec].sh_info);
413
414         /* Associated symbol table */
415         symtabsec = &sechdrs[sechdrs[relsec].sh_link];
416
417         /* String table */
418         if (symtabsec->sh_link >= ehdr->e_shnum) {
419                 /* Invalid strtab section number */
420                 pr_err("Invalid string table section index %d\n",
421                        symtabsec->sh_link);
422                 return -ENOEXEC;
423         }
424
425         strtab = (char *)sechdrs[symtabsec->sh_link].sh_offset;
426
427         /* section header string table */
428         shstrtab = (char *)sechdrs[ehdr->e_shstrndx].sh_offset;
429
430         for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
431
432                 /*
433                  * rel[i].r_offset contains byte offset from beginning
434                  * of section to the storage unit affected.
435                  *
436                  * This is location to update (->sh_offset). This is temporary
437                  * buffer where section is currently loaded. This will finally
438                  * be loaded to a different address later, pointed to by
439                  * ->sh_addr. kexec takes care of moving it
440                  *  (kexec_load_segment()).
441                  */
442                 location = (void *)(section->sh_offset + rel[i].r_offset);
443
444                 /* Final address of the location */
445                 address = section->sh_addr + rel[i].r_offset;
446
447                 /*
448                  * rel[i].r_info contains information about symbol table index
449                  * w.r.t which relocation must be made and type of relocation
450                  * to apply. ELF64_R_SYM() and ELF64_R_TYPE() macros get
451                  * these respectively.
452                  */
453                 sym = (Elf64_Sym *)symtabsec->sh_offset +
454                                 ELF64_R_SYM(rel[i].r_info);
455
456                 if (sym->st_name)
457                         name = strtab + sym->st_name;
458                 else
459                         name = shstrtab + sechdrs[sym->st_shndx].sh_name;
460
461                 pr_debug("Symbol: %s info: %02x shndx: %02x value=%llx size: %llx\n",
462                          name, sym->st_info, sym->st_shndx, sym->st_value,
463                          sym->st_size);
464
465                 if (sym->st_shndx == SHN_UNDEF) {
466                         pr_err("Undefined symbol: %s\n", name);
467                         return -ENOEXEC;
468                 }
469
470                 if (sym->st_shndx == SHN_COMMON) {
471                         pr_err("symbol '%s' in common section\n", name);
472                         return -ENOEXEC;
473                 }
474
475                 if (sym->st_shndx == SHN_ABS)
476                         sec_base = 0;
477                 else if (sym->st_shndx >= ehdr->e_shnum) {
478                         pr_err("Invalid section %d for symbol %s\n",
479                                sym->st_shndx, name);
480                         return -ENOEXEC;
481                 } else
482                         sec_base = sechdrs[sym->st_shndx].sh_addr;
483
484                 value = sym->st_value;
485                 value += sec_base;
486                 value += rel[i].r_addend;
487
488                 switch (ELF64_R_TYPE(rel[i].r_info)) {
489                 case R_X86_64_NONE:
490                         break;
491                 case R_X86_64_64:
492                         *(u64 *)location = value;
493                         break;
494                 case R_X86_64_32:
495                         *(u32 *)location = value;
496                         if (value != *(u32 *)location)
497                                 goto overflow;
498                         break;
499                 case R_X86_64_32S:
500                         *(s32 *)location = value;
501                         if ((s64)value != *(s32 *)location)
502                                 goto overflow;
503                         break;
504                 case R_X86_64_PC32:
505                 case R_X86_64_PLT32:
506                         value -= (u64)address;
507                         *(u32 *)location = value;
508                         break;
509                 default:
510                         pr_err("Unknown rela relocation: %llu\n",
511                                ELF64_R_TYPE(rel[i].r_info));
512                         return -ENOEXEC;
513                 }
514         }
515         return 0;
516
517 overflow:
518         pr_err("Overflow in relocation type %d value 0x%lx\n",
519                (int)ELF64_R_TYPE(rel[i].r_info), value);
520         return -ENOEXEC;
521 }
522 #endif /* CONFIG_KEXEC_FILE */
523
524 static int
525 kexec_mark_range(unsigned long start, unsigned long end, bool protect)
526 {
527         struct page *page;
528         unsigned int nr_pages;
529
530         /*
531          * For physical range: [start, end]. We must skip the unassigned
532          * crashk resource with zero-valued "end" member.
533          */
534         if (!end || start > end)
535                 return 0;
536
537         page = pfn_to_page(start >> PAGE_SHIFT);
538         nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
539         if (protect)
540                 return set_pages_ro(page, nr_pages);
541         else
542                 return set_pages_rw(page, nr_pages);
543 }
544
545 static void kexec_mark_crashkres(bool protect)
546 {
547         unsigned long control;
548
549         kexec_mark_range(crashk_low_res.start, crashk_low_res.end, protect);
550
551         /* Don't touch the control code page used in crash_kexec().*/
552         control = PFN_PHYS(page_to_pfn(kexec_crash_image->control_code_page));
553         /* Control code page is located in the 2nd page. */
554         kexec_mark_range(crashk_res.start, control + PAGE_SIZE - 1, protect);
555         control += KEXEC_CONTROL_PAGE_SIZE;
556         kexec_mark_range(control, crashk_res.end, protect);
557 }
558
559 void arch_kexec_protect_crashkres(void)
560 {
561         kexec_mark_crashkres(true);
562 }
563
564 void arch_kexec_unprotect_crashkres(void)
565 {
566         kexec_mark_crashkres(false);
567 }
568
569 int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp)
570 {
571         /*
572          * If SME is active we need to be sure that kexec pages are
573          * not encrypted because when we boot to the new kernel the
574          * pages won't be accessed encrypted (initially).
575          */
576         return set_memory_decrypted((unsigned long)vaddr, pages);
577 }
578
579 void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages)
580 {
581         /*
582          * If SME is active we need to reset the pages back to being
583          * an encrypted mapping before freeing them.
584          */
585         set_memory_encrypted((unsigned long)vaddr, pages);
586 }