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