1 /* 2 * S390 kdump implementation 3 * 4 * Copyright IBM Corp. 2011 5 * Author(s): Michael Holzheu <holzheu@linux.vnet.ibm.com> 6 */ 7 8 #include <linux/crash_dump.h> 9 #include <asm/lowcore.h> 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/gfp.h> 13 #include <linux/slab.h> 14 #include <linux/bootmem.h> 15 #include <linux/elf.h> 16 #include <linux/memblock.h> 17 #include <asm/os_info.h> 18 #include <asm/elf.h> 19 #include <asm/ipl.h> 20 #include <asm/sclp.h> 21 22 #define PTR_ADD(x, y) (((char *) (x)) + ((unsigned long) (y))) 23 #define PTR_SUB(x, y) (((char *) (x)) - ((unsigned long) (y))) 24 #define PTR_DIFF(x, y) ((unsigned long)(((char *) (x)) - ((unsigned long) (y)))) 25 26 static struct memblock_region oldmem_region; 27 28 static struct memblock_type oldmem_type = { 29 .cnt = 1, 30 .max = 1, 31 .total_size = 0, 32 .regions = &oldmem_region, 33 }; 34 35 struct dump_save_areas dump_save_areas; 36 37 /* 38 * Return physical address for virtual address 39 */ 40 static inline void *load_real_addr(void *addr) 41 { 42 unsigned long real_addr; 43 44 asm volatile( 45 " lra %0,0(%1)\n" 46 " jz 0f\n" 47 " la %0,0\n" 48 "0:" 49 : "=a" (real_addr) : "a" (addr) : "cc"); 50 return (void *)real_addr; 51 } 52 53 /* 54 * Copy real to virtual or real memory 55 */ 56 static int copy_from_realmem(void *dest, void *src, size_t count) 57 { 58 unsigned long size; 59 60 if (!count) 61 return 0; 62 if (!is_vmalloc_or_module_addr(dest)) 63 return memcpy_real(dest, src, count); 64 do { 65 size = min(count, PAGE_SIZE - (__pa(dest) & ~PAGE_MASK)); 66 if (memcpy_real(load_real_addr(dest), src, size)) 67 return -EFAULT; 68 count -= size; 69 dest += size; 70 src += size; 71 } while (count); 72 return 0; 73 } 74 75 /* 76 * Pointer to ELF header in new kernel 77 */ 78 static void *elfcorehdr_newmem; 79 80 /* 81 * Copy one page from zfcpdump "oldmem" 82 * 83 * For pages below HSA size memory from the HSA is copied. Otherwise 84 * real memory copy is used. 85 */ 86 static ssize_t copy_oldmem_page_zfcpdump(char *buf, size_t csize, 87 unsigned long src, int userbuf) 88 { 89 int rc; 90 91 if (src < sclp.hsa_size) { 92 rc = memcpy_hsa(buf, src, csize, userbuf); 93 } else { 94 if (userbuf) 95 rc = copy_to_user_real((void __force __user *) buf, 96 (void *) src, csize); 97 else 98 rc = memcpy_real(buf, (void *) src, csize); 99 } 100 return rc ? rc : csize; 101 } 102 103 /* 104 * Copy one page from kdump "oldmem" 105 * 106 * For the kdump reserved memory this functions performs a swap operation: 107 * - [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE] is mapped to [0 - OLDMEM_SIZE]. 108 * - [0 - OLDMEM_SIZE] is mapped to [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE] 109 */ 110 static ssize_t copy_oldmem_page_kdump(char *buf, size_t csize, 111 unsigned long src, int userbuf) 112 113 { 114 int rc; 115 116 if (src < OLDMEM_SIZE) 117 src += OLDMEM_BASE; 118 else if (src > OLDMEM_BASE && 119 src < OLDMEM_BASE + OLDMEM_SIZE) 120 src -= OLDMEM_BASE; 121 if (userbuf) 122 rc = copy_to_user_real((void __force __user *) buf, 123 (void *) src, csize); 124 else 125 rc = copy_from_realmem(buf, (void *) src, csize); 126 return (rc == 0) ? rc : csize; 127 } 128 129 /* 130 * Copy one page from "oldmem" 131 */ 132 ssize_t copy_oldmem_page(unsigned long pfn, char *buf, size_t csize, 133 unsigned long offset, int userbuf) 134 { 135 unsigned long src; 136 137 if (!csize) 138 return 0; 139 src = (pfn << PAGE_SHIFT) + offset; 140 if (OLDMEM_BASE) 141 return copy_oldmem_page_kdump(buf, csize, src, userbuf); 142 else 143 return copy_oldmem_page_zfcpdump(buf, csize, src, userbuf); 144 } 145 146 /* 147 * Remap "oldmem" for kdump 148 * 149 * For the kdump reserved memory this functions performs a swap operation: 150 * [0 - OLDMEM_SIZE] is mapped to [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE] 151 */ 152 static int remap_oldmem_pfn_range_kdump(struct vm_area_struct *vma, 153 unsigned long from, unsigned long pfn, 154 unsigned long size, pgprot_t prot) 155 { 156 unsigned long size_old; 157 int rc; 158 159 if (pfn < OLDMEM_SIZE >> PAGE_SHIFT) { 160 size_old = min(size, OLDMEM_SIZE - (pfn << PAGE_SHIFT)); 161 rc = remap_pfn_range(vma, from, 162 pfn + (OLDMEM_BASE >> PAGE_SHIFT), 163 size_old, prot); 164 if (rc || size == size_old) 165 return rc; 166 size -= size_old; 167 from += size_old; 168 pfn += size_old >> PAGE_SHIFT; 169 } 170 return remap_pfn_range(vma, from, pfn, size, prot); 171 } 172 173 /* 174 * Remap "oldmem" for zfcpdump 175 * 176 * We only map available memory above HSA size. Memory below HSA size 177 * is read on demand using the copy_oldmem_page() function. 178 */ 179 static int remap_oldmem_pfn_range_zfcpdump(struct vm_area_struct *vma, 180 unsigned long from, 181 unsigned long pfn, 182 unsigned long size, pgprot_t prot) 183 { 184 unsigned long hsa_end = sclp.hsa_size; 185 unsigned long size_hsa; 186 187 if (pfn < hsa_end >> PAGE_SHIFT) { 188 size_hsa = min(size, hsa_end - (pfn << PAGE_SHIFT)); 189 if (size == size_hsa) 190 return 0; 191 size -= size_hsa; 192 from += size_hsa; 193 pfn += size_hsa >> PAGE_SHIFT; 194 } 195 return remap_pfn_range(vma, from, pfn, size, prot); 196 } 197 198 /* 199 * Remap "oldmem" for kdump or zfcpdump 200 */ 201 int remap_oldmem_pfn_range(struct vm_area_struct *vma, unsigned long from, 202 unsigned long pfn, unsigned long size, pgprot_t prot) 203 { 204 if (OLDMEM_BASE) 205 return remap_oldmem_pfn_range_kdump(vma, from, pfn, size, prot); 206 else 207 return remap_oldmem_pfn_range_zfcpdump(vma, from, pfn, size, 208 prot); 209 } 210 211 /* 212 * Copy memory from old kernel 213 */ 214 int copy_from_oldmem(void *dest, void *src, size_t count) 215 { 216 unsigned long copied = 0; 217 int rc; 218 219 if (OLDMEM_BASE) { 220 if ((unsigned long) src < OLDMEM_SIZE) { 221 copied = min(count, OLDMEM_SIZE - (unsigned long) src); 222 rc = copy_from_realmem(dest, src + OLDMEM_BASE, copied); 223 if (rc) 224 return rc; 225 } 226 } else { 227 unsigned long hsa_end = sclp.hsa_size; 228 if ((unsigned long) src < hsa_end) { 229 copied = min(count, hsa_end - (unsigned long) src); 230 rc = memcpy_hsa(dest, (unsigned long) src, copied, 0); 231 if (rc) 232 return rc; 233 } 234 } 235 return copy_from_realmem(dest + copied, src + copied, count - copied); 236 } 237 238 /* 239 * Alloc memory and panic in case of ENOMEM 240 */ 241 static void *kzalloc_panic(int len) 242 { 243 void *rc; 244 245 rc = kzalloc(len, GFP_KERNEL); 246 if (!rc) 247 panic("s390 kdump kzalloc (%d) failed", len); 248 return rc; 249 } 250 251 /* 252 * Initialize ELF note 253 */ 254 static void *nt_init(void *buf, Elf64_Word type, void *desc, int d_len, 255 const char *name) 256 { 257 Elf64_Nhdr *note; 258 u64 len; 259 260 note = (Elf64_Nhdr *)buf; 261 note->n_namesz = strlen(name) + 1; 262 note->n_descsz = d_len; 263 note->n_type = type; 264 len = sizeof(Elf64_Nhdr); 265 266 memcpy(buf + len, name, note->n_namesz); 267 len = roundup(len + note->n_namesz, 4); 268 269 memcpy(buf + len, desc, note->n_descsz); 270 len = roundup(len + note->n_descsz, 4); 271 272 return PTR_ADD(buf, len); 273 } 274 275 /* 276 * Initialize prstatus note 277 */ 278 static void *nt_prstatus(void *ptr, struct save_area *sa) 279 { 280 struct elf_prstatus nt_prstatus; 281 static int cpu_nr = 1; 282 283 memset(&nt_prstatus, 0, sizeof(nt_prstatus)); 284 memcpy(&nt_prstatus.pr_reg.gprs, sa->gp_regs, sizeof(sa->gp_regs)); 285 memcpy(&nt_prstatus.pr_reg.psw, sa->psw, sizeof(sa->psw)); 286 memcpy(&nt_prstatus.pr_reg.acrs, sa->acc_regs, sizeof(sa->acc_regs)); 287 nt_prstatus.pr_pid = cpu_nr; 288 cpu_nr++; 289 290 return nt_init(ptr, NT_PRSTATUS, &nt_prstatus, sizeof(nt_prstatus), 291 "CORE"); 292 } 293 294 /* 295 * Initialize fpregset (floating point) note 296 */ 297 static void *nt_fpregset(void *ptr, struct save_area *sa) 298 { 299 elf_fpregset_t nt_fpregset; 300 301 memset(&nt_fpregset, 0, sizeof(nt_fpregset)); 302 memcpy(&nt_fpregset.fpc, &sa->fp_ctrl_reg, sizeof(sa->fp_ctrl_reg)); 303 memcpy(&nt_fpregset.fprs, &sa->fp_regs, sizeof(sa->fp_regs)); 304 305 return nt_init(ptr, NT_PRFPREG, &nt_fpregset, sizeof(nt_fpregset), 306 "CORE"); 307 } 308 309 /* 310 * Initialize timer note 311 */ 312 static void *nt_s390_timer(void *ptr, struct save_area *sa) 313 { 314 return nt_init(ptr, NT_S390_TIMER, &sa->timer, sizeof(sa->timer), 315 KEXEC_CORE_NOTE_NAME); 316 } 317 318 /* 319 * Initialize TOD clock comparator note 320 */ 321 static void *nt_s390_tod_cmp(void *ptr, struct save_area *sa) 322 { 323 return nt_init(ptr, NT_S390_TODCMP, &sa->clk_cmp, 324 sizeof(sa->clk_cmp), KEXEC_CORE_NOTE_NAME); 325 } 326 327 /* 328 * Initialize TOD programmable register note 329 */ 330 static void *nt_s390_tod_preg(void *ptr, struct save_area *sa) 331 { 332 return nt_init(ptr, NT_S390_TODPREG, &sa->tod_reg, 333 sizeof(sa->tod_reg), KEXEC_CORE_NOTE_NAME); 334 } 335 336 /* 337 * Initialize control register note 338 */ 339 static void *nt_s390_ctrs(void *ptr, struct save_area *sa) 340 { 341 return nt_init(ptr, NT_S390_CTRS, &sa->ctrl_regs, 342 sizeof(sa->ctrl_regs), KEXEC_CORE_NOTE_NAME); 343 } 344 345 /* 346 * Initialize prefix register note 347 */ 348 static void *nt_s390_prefix(void *ptr, struct save_area *sa) 349 { 350 return nt_init(ptr, NT_S390_PREFIX, &sa->pref_reg, 351 sizeof(sa->pref_reg), KEXEC_CORE_NOTE_NAME); 352 } 353 354 /* 355 * Initialize vxrs high note (full 128 bit VX registers 16-31) 356 */ 357 static void *nt_s390_vx_high(void *ptr, __vector128 *vx_regs) 358 { 359 return nt_init(ptr, NT_S390_VXRS_HIGH, &vx_regs[16], 360 16 * sizeof(__vector128), KEXEC_CORE_NOTE_NAME); 361 } 362 363 /* 364 * Initialize vxrs low note (lower halves of VX registers 0-15) 365 */ 366 static void *nt_s390_vx_low(void *ptr, __vector128 *vx_regs) 367 { 368 Elf64_Nhdr *note; 369 u64 len; 370 int i; 371 372 note = (Elf64_Nhdr *)ptr; 373 note->n_namesz = strlen(KEXEC_CORE_NOTE_NAME) + 1; 374 note->n_descsz = 16 * 8; 375 note->n_type = NT_S390_VXRS_LOW; 376 len = sizeof(Elf64_Nhdr); 377 378 memcpy(ptr + len, KEXEC_CORE_NOTE_NAME, note->n_namesz); 379 len = roundup(len + note->n_namesz, 4); 380 381 ptr += len; 382 /* Copy lower halves of SIMD registers 0-15 */ 383 for (i = 0; i < 16; i++) { 384 memcpy(ptr, &vx_regs[i].u[2], 8); 385 ptr += 8; 386 } 387 return ptr; 388 } 389 390 /* 391 * Fill ELF notes for one CPU with save area registers 392 */ 393 void *fill_cpu_elf_notes(void *ptr, struct save_area *sa, __vector128 *vx_regs) 394 { 395 ptr = nt_prstatus(ptr, sa); 396 ptr = nt_fpregset(ptr, sa); 397 ptr = nt_s390_timer(ptr, sa); 398 ptr = nt_s390_tod_cmp(ptr, sa); 399 ptr = nt_s390_tod_preg(ptr, sa); 400 ptr = nt_s390_ctrs(ptr, sa); 401 ptr = nt_s390_prefix(ptr, sa); 402 if (MACHINE_HAS_VX && vx_regs) { 403 ptr = nt_s390_vx_low(ptr, vx_regs); 404 ptr = nt_s390_vx_high(ptr, vx_regs); 405 } 406 return ptr; 407 } 408 409 /* 410 * Initialize prpsinfo note (new kernel) 411 */ 412 static void *nt_prpsinfo(void *ptr) 413 { 414 struct elf_prpsinfo prpsinfo; 415 416 memset(&prpsinfo, 0, sizeof(prpsinfo)); 417 prpsinfo.pr_sname = 'R'; 418 strcpy(prpsinfo.pr_fname, "vmlinux"); 419 return nt_init(ptr, NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo), 420 KEXEC_CORE_NOTE_NAME); 421 } 422 423 /* 424 * Get vmcoreinfo using lowcore->vmcore_info (new kernel) 425 */ 426 static void *get_vmcoreinfo_old(unsigned long *size) 427 { 428 char nt_name[11], *vmcoreinfo; 429 Elf64_Nhdr note; 430 void *addr; 431 432 if (copy_from_oldmem(&addr, &S390_lowcore.vmcore_info, sizeof(addr))) 433 return NULL; 434 memset(nt_name, 0, sizeof(nt_name)); 435 if (copy_from_oldmem(¬e, addr, sizeof(note))) 436 return NULL; 437 if (copy_from_oldmem(nt_name, addr + sizeof(note), sizeof(nt_name) - 1)) 438 return NULL; 439 if (strcmp(nt_name, "VMCOREINFO") != 0) 440 return NULL; 441 vmcoreinfo = kzalloc_panic(note.n_descsz); 442 if (copy_from_oldmem(vmcoreinfo, addr + 24, note.n_descsz)) 443 return NULL; 444 *size = note.n_descsz; 445 return vmcoreinfo; 446 } 447 448 /* 449 * Initialize vmcoreinfo note (new kernel) 450 */ 451 static void *nt_vmcoreinfo(void *ptr) 452 { 453 unsigned long size; 454 void *vmcoreinfo; 455 456 vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size); 457 if (!vmcoreinfo) 458 vmcoreinfo = get_vmcoreinfo_old(&size); 459 if (!vmcoreinfo) 460 return ptr; 461 return nt_init(ptr, 0, vmcoreinfo, size, "VMCOREINFO"); 462 } 463 464 /* 465 * Initialize ELF header (new kernel) 466 */ 467 static void *ehdr_init(Elf64_Ehdr *ehdr, int mem_chunk_cnt) 468 { 469 memset(ehdr, 0, sizeof(*ehdr)); 470 memcpy(ehdr->e_ident, ELFMAG, SELFMAG); 471 ehdr->e_ident[EI_CLASS] = ELFCLASS64; 472 ehdr->e_ident[EI_DATA] = ELFDATA2MSB; 473 ehdr->e_ident[EI_VERSION] = EV_CURRENT; 474 memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD); 475 ehdr->e_type = ET_CORE; 476 ehdr->e_machine = EM_S390; 477 ehdr->e_version = EV_CURRENT; 478 ehdr->e_phoff = sizeof(Elf64_Ehdr); 479 ehdr->e_ehsize = sizeof(Elf64_Ehdr); 480 ehdr->e_phentsize = sizeof(Elf64_Phdr); 481 ehdr->e_phnum = mem_chunk_cnt + 1; 482 return ehdr + 1; 483 } 484 485 /* 486 * Return CPU count for ELF header (new kernel) 487 */ 488 static int get_cpu_cnt(void) 489 { 490 int i, cpus = 0; 491 492 for (i = 0; i < dump_save_areas.count; i++) { 493 if (dump_save_areas.areas[i]->sa.pref_reg == 0) 494 continue; 495 cpus++; 496 } 497 return cpus; 498 } 499 500 /* 501 * Return memory chunk count for ELF header (new kernel) 502 */ 503 static int get_mem_chunk_cnt(void) 504 { 505 int cnt = 0; 506 u64 idx; 507 508 for_each_mem_range(idx, &memblock.physmem, &oldmem_type, NUMA_NO_NODE, 509 MEMBLOCK_NONE, NULL, NULL, NULL) 510 cnt++; 511 return cnt; 512 } 513 514 /* 515 * Initialize ELF loads (new kernel) 516 */ 517 static void loads_init(Elf64_Phdr *phdr, u64 loads_offset) 518 { 519 phys_addr_t start, end; 520 u64 idx; 521 522 for_each_mem_range(idx, &memblock.physmem, &oldmem_type, NUMA_NO_NODE, 523 MEMBLOCK_NONE, &start, &end, NULL) { 524 phdr->p_filesz = end - start; 525 phdr->p_type = PT_LOAD; 526 phdr->p_offset = start; 527 phdr->p_vaddr = start; 528 phdr->p_paddr = start; 529 phdr->p_memsz = end - start; 530 phdr->p_flags = PF_R | PF_W | PF_X; 531 phdr->p_align = PAGE_SIZE; 532 phdr++; 533 } 534 } 535 536 /* 537 * Initialize notes (new kernel) 538 */ 539 static void *notes_init(Elf64_Phdr *phdr, void *ptr, u64 notes_offset) 540 { 541 struct save_area_ext *sa_ext; 542 void *ptr_start = ptr; 543 int i; 544 545 ptr = nt_prpsinfo(ptr); 546 547 for (i = 0; i < dump_save_areas.count; i++) { 548 sa_ext = dump_save_areas.areas[i]; 549 if (sa_ext->sa.pref_reg == 0) 550 continue; 551 ptr = fill_cpu_elf_notes(ptr, &sa_ext->sa, sa_ext->vx_regs); 552 } 553 ptr = nt_vmcoreinfo(ptr); 554 memset(phdr, 0, sizeof(*phdr)); 555 phdr->p_type = PT_NOTE; 556 phdr->p_offset = notes_offset; 557 phdr->p_filesz = (unsigned long) PTR_SUB(ptr, ptr_start); 558 phdr->p_memsz = phdr->p_filesz; 559 return ptr; 560 } 561 562 /* 563 * Create ELF core header (new kernel) 564 */ 565 int elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size) 566 { 567 Elf64_Phdr *phdr_notes, *phdr_loads; 568 int mem_chunk_cnt; 569 void *ptr, *hdr; 570 u32 alloc_size; 571 u64 hdr_off; 572 573 /* If we are not in kdump or zfcpdump mode return */ 574 if (!OLDMEM_BASE && ipl_info.type != IPL_TYPE_FCP_DUMP) 575 return 0; 576 /* If elfcorehdr= has been passed via cmdline, we use that one */ 577 if (elfcorehdr_addr != ELFCORE_ADDR_MAX) 578 return 0; 579 /* If we cannot get HSA size for zfcpdump return error */ 580 if (ipl_info.type == IPL_TYPE_FCP_DUMP && !sclp.hsa_size) 581 return -ENODEV; 582 583 /* For kdump, exclude previous crashkernel memory */ 584 if (OLDMEM_BASE) { 585 oldmem_region.base = OLDMEM_BASE; 586 oldmem_region.size = OLDMEM_SIZE; 587 oldmem_type.total_size = OLDMEM_SIZE; 588 } 589 590 mem_chunk_cnt = get_mem_chunk_cnt(); 591 592 alloc_size = 0x1000 + get_cpu_cnt() * 0x4a0 + 593 mem_chunk_cnt * sizeof(Elf64_Phdr); 594 hdr = kzalloc_panic(alloc_size); 595 /* Init elf header */ 596 ptr = ehdr_init(hdr, mem_chunk_cnt); 597 /* Init program headers */ 598 phdr_notes = ptr; 599 ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr)); 600 phdr_loads = ptr; 601 ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr) * mem_chunk_cnt); 602 /* Init notes */ 603 hdr_off = PTR_DIFF(ptr, hdr); 604 ptr = notes_init(phdr_notes, ptr, ((unsigned long) hdr) + hdr_off); 605 /* Init loads */ 606 hdr_off = PTR_DIFF(ptr, hdr); 607 loads_init(phdr_loads, hdr_off); 608 *addr = (unsigned long long) hdr; 609 elfcorehdr_newmem = hdr; 610 *size = (unsigned long long) hdr_off; 611 BUG_ON(elfcorehdr_size > alloc_size); 612 return 0; 613 } 614 615 /* 616 * Free ELF core header (new kernel) 617 */ 618 void elfcorehdr_free(unsigned long long addr) 619 { 620 if (!elfcorehdr_newmem) 621 return; 622 kfree((void *)(unsigned long)addr); 623 } 624 625 /* 626 * Read from ELF header 627 */ 628 ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos) 629 { 630 void *src = (void *)(unsigned long)*ppos; 631 632 src = elfcorehdr_newmem ? src : src - OLDMEM_BASE; 633 memcpy(buf, src, count); 634 *ppos += count; 635 return count; 636 } 637 638 /* 639 * Read from ELF notes data 640 */ 641 ssize_t elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos) 642 { 643 void *src = (void *)(unsigned long)*ppos; 644 int rc; 645 646 if (elfcorehdr_newmem) { 647 memcpy(buf, src, count); 648 } else { 649 rc = copy_from_oldmem(buf, src, count); 650 if (rc) 651 return rc; 652 } 653 *ppos += count; 654 return count; 655 } 656