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 * @pi: Purgatory to be relocated. 386 * @section: Section relocations applying to. 387 * @relsec: Section containing RELAs. 388 * @symtabsec: Corresponding symtab. 389 * 390 * TODO: Some of the code belongs to generic code. Move that in kexec.c. 391 */ 392 int arch_kexec_apply_relocations_add(struct purgatory_info *pi, 393 Elf_Shdr *section, const Elf_Shdr *relsec, 394 const Elf_Shdr *symtabsec) 395 { 396 unsigned int i; 397 Elf64_Rela *rel; 398 Elf64_Sym *sym; 399 void *location; 400 unsigned long address, sec_base, value; 401 const char *strtab, *name, *shstrtab; 402 const Elf_Shdr *sechdrs; 403 404 /* String & section header string table */ 405 sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff; 406 strtab = (char *)pi->ehdr + sechdrs[symtabsec->sh_link].sh_offset; 407 shstrtab = (char *)pi->ehdr + sechdrs[pi->ehdr->e_shstrndx].sh_offset; 408 409 rel = (void *)pi->ehdr + relsec->sh_offset; 410 411 pr_debug("Applying relocate section %s to %u\n", 412 shstrtab + relsec->sh_name, relsec->sh_info); 413 414 for (i = 0; i < relsec->sh_size / sizeof(*rel); i++) { 415 416 /* 417 * rel[i].r_offset contains byte offset from beginning 418 * of section to the storage unit affected. 419 * 420 * This is location to update. This is temporary buffer 421 * where section is currently loaded. This will finally be 422 * loaded to a different address later, pointed to by 423 * ->sh_addr. kexec takes care of moving it 424 * (kexec_load_segment()). 425 */ 426 location = pi->purgatory_buf; 427 location += section->sh_offset; 428 location += rel[i].r_offset; 429 430 /* Final address of the location */ 431 address = section->sh_addr + rel[i].r_offset; 432 433 /* 434 * rel[i].r_info contains information about symbol table index 435 * w.r.t which relocation must be made and type of relocation 436 * to apply. ELF64_R_SYM() and ELF64_R_TYPE() macros get 437 * these respectively. 438 */ 439 sym = (void *)pi->ehdr + symtabsec->sh_offset; 440 sym += ELF64_R_SYM(rel[i].r_info); 441 442 if (sym->st_name) 443 name = strtab + sym->st_name; 444 else 445 name = shstrtab + sechdrs[sym->st_shndx].sh_name; 446 447 pr_debug("Symbol: %s info: %02x shndx: %02x value=%llx size: %llx\n", 448 name, sym->st_info, sym->st_shndx, sym->st_value, 449 sym->st_size); 450 451 if (sym->st_shndx == SHN_UNDEF) { 452 pr_err("Undefined symbol: %s\n", name); 453 return -ENOEXEC; 454 } 455 456 if (sym->st_shndx == SHN_COMMON) { 457 pr_err("symbol '%s' in common section\n", name); 458 return -ENOEXEC; 459 } 460 461 if (sym->st_shndx == SHN_ABS) 462 sec_base = 0; 463 else if (sym->st_shndx >= pi->ehdr->e_shnum) { 464 pr_err("Invalid section %d for symbol %s\n", 465 sym->st_shndx, name); 466 return -ENOEXEC; 467 } else 468 sec_base = pi->sechdrs[sym->st_shndx].sh_addr; 469 470 value = sym->st_value; 471 value += sec_base; 472 value += rel[i].r_addend; 473 474 switch (ELF64_R_TYPE(rel[i].r_info)) { 475 case R_X86_64_NONE: 476 break; 477 case R_X86_64_64: 478 *(u64 *)location = value; 479 break; 480 case R_X86_64_32: 481 *(u32 *)location = value; 482 if (value != *(u32 *)location) 483 goto overflow; 484 break; 485 case R_X86_64_32S: 486 *(s32 *)location = value; 487 if ((s64)value != *(s32 *)location) 488 goto overflow; 489 break; 490 case R_X86_64_PC32: 491 case R_X86_64_PLT32: 492 value -= (u64)address; 493 *(u32 *)location = value; 494 break; 495 default: 496 pr_err("Unknown rela relocation: %llu\n", 497 ELF64_R_TYPE(rel[i].r_info)); 498 return -ENOEXEC; 499 } 500 } 501 return 0; 502 503 overflow: 504 pr_err("Overflow in relocation type %d value 0x%lx\n", 505 (int)ELF64_R_TYPE(rel[i].r_info), value); 506 return -ENOEXEC; 507 } 508 #endif /* CONFIG_KEXEC_FILE */ 509 510 static int 511 kexec_mark_range(unsigned long start, unsigned long end, bool protect) 512 { 513 struct page *page; 514 unsigned int nr_pages; 515 516 /* 517 * For physical range: [start, end]. We must skip the unassigned 518 * crashk resource with zero-valued "end" member. 519 */ 520 if (!end || start > end) 521 return 0; 522 523 page = pfn_to_page(start >> PAGE_SHIFT); 524 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; 525 if (protect) 526 return set_pages_ro(page, nr_pages); 527 else 528 return set_pages_rw(page, nr_pages); 529 } 530 531 static void kexec_mark_crashkres(bool protect) 532 { 533 unsigned long control; 534 535 kexec_mark_range(crashk_low_res.start, crashk_low_res.end, protect); 536 537 /* Don't touch the control code page used in crash_kexec().*/ 538 control = PFN_PHYS(page_to_pfn(kexec_crash_image->control_code_page)); 539 /* Control code page is located in the 2nd page. */ 540 kexec_mark_range(crashk_res.start, control + PAGE_SIZE - 1, protect); 541 control += KEXEC_CONTROL_PAGE_SIZE; 542 kexec_mark_range(control, crashk_res.end, protect); 543 } 544 545 void arch_kexec_protect_crashkres(void) 546 { 547 kexec_mark_crashkres(true); 548 } 549 550 void arch_kexec_unprotect_crashkres(void) 551 { 552 kexec_mark_crashkres(false); 553 } 554 555 int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp) 556 { 557 /* 558 * If SME is active we need to be sure that kexec pages are 559 * not encrypted because when we boot to the new kernel the 560 * pages won't be accessed encrypted (initially). 561 */ 562 return set_memory_decrypted((unsigned long)vaddr, pages); 563 } 564 565 void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages) 566 { 567 /* 568 * If SME is active we need to reset the pages back to being 569 * an encrypted mapping before freeing them. 570 */ 571 set_memory_encrypted((unsigned long)vaddr, pages); 572 } 573