crash_dump.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * S390 kdump implementation
  3. *
  4. * Copyright IBM Corp. 2011
  5. * Author(s): Michael Holzheu <holzheu@linux.vnet.ibm.com>
  6. */
  7. #include <linux/crash_dump.h>
  8. #include <asm/lowcore.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/gfp.h>
  12. #include <linux/slab.h>
  13. #include <linux/bootmem.h>
  14. #include <linux/elf.h>
  15. #include <asm/os_info.h>
  16. #include <asm/elf.h>
  17. #include <asm/ipl.h>
  18. #define PTR_ADD(x, y) (((char *) (x)) + ((unsigned long) (y)))
  19. #define PTR_SUB(x, y) (((char *) (x)) - ((unsigned long) (y)))
  20. #define PTR_DIFF(x, y) ((unsigned long)(((char *) (x)) - ((unsigned long) (y))))
  21. /*
  22. * Copy one page from "oldmem"
  23. *
  24. * For the kdump reserved memory this functions performs a swap operation:
  25. * - [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE] is mapped to [0 - OLDMEM_SIZE].
  26. * - [0 - OLDMEM_SIZE] is mapped to [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE]
  27. */
  28. ssize_t copy_oldmem_page(unsigned long pfn, char *buf,
  29. size_t csize, unsigned long offset, int userbuf)
  30. {
  31. unsigned long src;
  32. if (!csize)
  33. return 0;
  34. src = (pfn << PAGE_SHIFT) + offset;
  35. if (src < OLDMEM_SIZE)
  36. src += OLDMEM_BASE;
  37. else if (src > OLDMEM_BASE &&
  38. src < OLDMEM_BASE + OLDMEM_SIZE)
  39. src -= OLDMEM_BASE;
  40. if (userbuf)
  41. copy_to_user_real((void __force __user *) buf, (void *) src,
  42. csize);
  43. else
  44. memcpy_real(buf, (void *) src, csize);
  45. return csize;
  46. }
  47. /*
  48. * Copy memory from old kernel
  49. */
  50. int copy_from_oldmem(void *dest, void *src, size_t count)
  51. {
  52. unsigned long copied = 0;
  53. int rc;
  54. if ((unsigned long) src < OLDMEM_SIZE) {
  55. copied = min(count, OLDMEM_SIZE - (unsigned long) src);
  56. rc = memcpy_real(dest, src + OLDMEM_BASE, copied);
  57. if (rc)
  58. return rc;
  59. }
  60. return memcpy_real(dest + copied, src + copied, count - copied);
  61. }
  62. /*
  63. * Alloc memory and panic in case of ENOMEM
  64. */
  65. static void *kzalloc_panic(int len)
  66. {
  67. void *rc;
  68. rc = kzalloc(len, GFP_KERNEL);
  69. if (!rc)
  70. panic("s390 kdump kzalloc (%d) failed", len);
  71. return rc;
  72. }
  73. /*
  74. * Get memory layout and create hole for oldmem
  75. */
  76. static struct mem_chunk *get_memory_layout(void)
  77. {
  78. struct mem_chunk *chunk_array;
  79. chunk_array = kzalloc_panic(MEMORY_CHUNKS * sizeof(struct mem_chunk));
  80. detect_memory_layout(chunk_array);
  81. create_mem_hole(chunk_array, OLDMEM_BASE, OLDMEM_SIZE, CHUNK_CRASHK);
  82. return chunk_array;
  83. }
  84. /*
  85. * Initialize ELF note
  86. */
  87. static void *nt_init(void *buf, Elf64_Word type, void *desc, int d_len,
  88. const char *name)
  89. {
  90. Elf64_Nhdr *note;
  91. u64 len;
  92. note = (Elf64_Nhdr *)buf;
  93. note->n_namesz = strlen(name) + 1;
  94. note->n_descsz = d_len;
  95. note->n_type = type;
  96. len = sizeof(Elf64_Nhdr);
  97. memcpy(buf + len, name, note->n_namesz);
  98. len = roundup(len + note->n_namesz, 4);
  99. memcpy(buf + len, desc, note->n_descsz);
  100. len = roundup(len + note->n_descsz, 4);
  101. return PTR_ADD(buf, len);
  102. }
  103. /*
  104. * Initialize prstatus note
  105. */
  106. static void *nt_prstatus(void *ptr, struct save_area *sa)
  107. {
  108. struct elf_prstatus nt_prstatus;
  109. static int cpu_nr = 1;
  110. memset(&nt_prstatus, 0, sizeof(nt_prstatus));
  111. memcpy(&nt_prstatus.pr_reg.gprs, sa->gp_regs, sizeof(sa->gp_regs));
  112. memcpy(&nt_prstatus.pr_reg.psw, sa->psw, sizeof(sa->psw));
  113. memcpy(&nt_prstatus.pr_reg.acrs, sa->acc_regs, sizeof(sa->acc_regs));
  114. nt_prstatus.pr_pid = cpu_nr;
  115. cpu_nr++;
  116. return nt_init(ptr, NT_PRSTATUS, &nt_prstatus, sizeof(nt_prstatus),
  117. "CORE");
  118. }
  119. /*
  120. * Initialize fpregset (floating point) note
  121. */
  122. static void *nt_fpregset(void *ptr, struct save_area *sa)
  123. {
  124. elf_fpregset_t nt_fpregset;
  125. memset(&nt_fpregset, 0, sizeof(nt_fpregset));
  126. memcpy(&nt_fpregset.fpc, &sa->fp_ctrl_reg, sizeof(sa->fp_ctrl_reg));
  127. memcpy(&nt_fpregset.fprs, &sa->fp_regs, sizeof(sa->fp_regs));
  128. return nt_init(ptr, NT_PRFPREG, &nt_fpregset, sizeof(nt_fpregset),
  129. "CORE");
  130. }
  131. /*
  132. * Initialize timer note
  133. */
  134. static void *nt_s390_timer(void *ptr, struct save_area *sa)
  135. {
  136. return nt_init(ptr, NT_S390_TIMER, &sa->timer, sizeof(sa->timer),
  137. KEXEC_CORE_NOTE_NAME);
  138. }
  139. /*
  140. * Initialize TOD clock comparator note
  141. */
  142. static void *nt_s390_tod_cmp(void *ptr, struct save_area *sa)
  143. {
  144. return nt_init(ptr, NT_S390_TODCMP, &sa->clk_cmp,
  145. sizeof(sa->clk_cmp), KEXEC_CORE_NOTE_NAME);
  146. }
  147. /*
  148. * Initialize TOD programmable register note
  149. */
  150. static void *nt_s390_tod_preg(void *ptr, struct save_area *sa)
  151. {
  152. return nt_init(ptr, NT_S390_TODPREG, &sa->tod_reg,
  153. sizeof(sa->tod_reg), KEXEC_CORE_NOTE_NAME);
  154. }
  155. /*
  156. * Initialize control register note
  157. */
  158. static void *nt_s390_ctrs(void *ptr, struct save_area *sa)
  159. {
  160. return nt_init(ptr, NT_S390_CTRS, &sa->ctrl_regs,
  161. sizeof(sa->ctrl_regs), KEXEC_CORE_NOTE_NAME);
  162. }
  163. /*
  164. * Initialize prefix register note
  165. */
  166. static void *nt_s390_prefix(void *ptr, struct save_area *sa)
  167. {
  168. return nt_init(ptr, NT_S390_PREFIX, &sa->pref_reg,
  169. sizeof(sa->pref_reg), KEXEC_CORE_NOTE_NAME);
  170. }
  171. /*
  172. * Fill ELF notes for one CPU with save area registers
  173. */
  174. void *fill_cpu_elf_notes(void *ptr, struct save_area *sa)
  175. {
  176. ptr = nt_prstatus(ptr, sa);
  177. ptr = nt_fpregset(ptr, sa);
  178. ptr = nt_s390_timer(ptr, sa);
  179. ptr = nt_s390_tod_cmp(ptr, sa);
  180. ptr = nt_s390_tod_preg(ptr, sa);
  181. ptr = nt_s390_ctrs(ptr, sa);
  182. ptr = nt_s390_prefix(ptr, sa);
  183. return ptr;
  184. }
  185. /*
  186. * Initialize prpsinfo note (new kernel)
  187. */
  188. static void *nt_prpsinfo(void *ptr)
  189. {
  190. struct elf_prpsinfo prpsinfo;
  191. memset(&prpsinfo, 0, sizeof(prpsinfo));
  192. prpsinfo.pr_sname = 'R';
  193. strcpy(prpsinfo.pr_fname, "vmlinux");
  194. return nt_init(ptr, NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo),
  195. KEXEC_CORE_NOTE_NAME);
  196. }
  197. /*
  198. * Get vmcoreinfo using lowcore->vmcore_info (new kernel)
  199. */
  200. static void *get_vmcoreinfo_old(unsigned long *size)
  201. {
  202. char nt_name[11], *vmcoreinfo;
  203. Elf64_Nhdr note;
  204. void *addr;
  205. if (copy_from_oldmem(&addr, &S390_lowcore.vmcore_info, sizeof(addr)))
  206. return NULL;
  207. memset(nt_name, 0, sizeof(nt_name));
  208. if (copy_from_oldmem(&note, addr, sizeof(note)))
  209. return NULL;
  210. if (copy_from_oldmem(nt_name, addr + sizeof(note), sizeof(nt_name) - 1))
  211. return NULL;
  212. if (strcmp(nt_name, "VMCOREINFO") != 0)
  213. return NULL;
  214. vmcoreinfo = kzalloc_panic(note.n_descsz);
  215. if (copy_from_oldmem(vmcoreinfo, addr + 24, note.n_descsz))
  216. return NULL;
  217. *size = note.n_descsz;
  218. return vmcoreinfo;
  219. }
  220. /*
  221. * Initialize vmcoreinfo note (new kernel)
  222. */
  223. static void *nt_vmcoreinfo(void *ptr)
  224. {
  225. unsigned long size;
  226. void *vmcoreinfo;
  227. vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size);
  228. if (!vmcoreinfo)
  229. vmcoreinfo = get_vmcoreinfo_old(&size);
  230. if (!vmcoreinfo)
  231. return ptr;
  232. return nt_init(ptr, 0, vmcoreinfo, size, "VMCOREINFO");
  233. }
  234. /*
  235. * Initialize ELF header (new kernel)
  236. */
  237. static void *ehdr_init(Elf64_Ehdr *ehdr, int mem_chunk_cnt)
  238. {
  239. memset(ehdr, 0, sizeof(*ehdr));
  240. memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
  241. ehdr->e_ident[EI_CLASS] = ELFCLASS64;
  242. ehdr->e_ident[EI_DATA] = ELFDATA2MSB;
  243. ehdr->e_ident[EI_VERSION] = EV_CURRENT;
  244. memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
  245. ehdr->e_type = ET_CORE;
  246. ehdr->e_machine = EM_S390;
  247. ehdr->e_version = EV_CURRENT;
  248. ehdr->e_phoff = sizeof(Elf64_Ehdr);
  249. ehdr->e_ehsize = sizeof(Elf64_Ehdr);
  250. ehdr->e_phentsize = sizeof(Elf64_Phdr);
  251. ehdr->e_phnum = mem_chunk_cnt + 1;
  252. return ehdr + 1;
  253. }
  254. /*
  255. * Return CPU count for ELF header (new kernel)
  256. */
  257. static int get_cpu_cnt(void)
  258. {
  259. int i, cpus = 0;
  260. for (i = 0; zfcpdump_save_areas[i]; i++) {
  261. if (zfcpdump_save_areas[i]->pref_reg == 0)
  262. continue;
  263. cpus++;
  264. }
  265. return cpus;
  266. }
  267. /*
  268. * Return memory chunk count for ELF header (new kernel)
  269. */
  270. static int get_mem_chunk_cnt(void)
  271. {
  272. struct mem_chunk *chunk_array, *mem_chunk;
  273. int i, cnt = 0;
  274. chunk_array = get_memory_layout();
  275. for (i = 0; i < MEMORY_CHUNKS; i++) {
  276. mem_chunk = &chunk_array[i];
  277. if (chunk_array[i].type != CHUNK_READ_WRITE &&
  278. chunk_array[i].type != CHUNK_READ_ONLY)
  279. continue;
  280. if (mem_chunk->size == 0)
  281. continue;
  282. cnt++;
  283. }
  284. kfree(chunk_array);
  285. return cnt;
  286. }
  287. /*
  288. * Relocate pointer in order to allow vmcore code access the data
  289. */
  290. static inline unsigned long relocate(unsigned long addr)
  291. {
  292. return OLDMEM_BASE + addr;
  293. }
  294. /*
  295. * Initialize ELF loads (new kernel)
  296. */
  297. static int loads_init(Elf64_Phdr *phdr, u64 loads_offset)
  298. {
  299. struct mem_chunk *chunk_array, *mem_chunk;
  300. int i;
  301. chunk_array = get_memory_layout();
  302. for (i = 0; i < MEMORY_CHUNKS; i++) {
  303. mem_chunk = &chunk_array[i];
  304. if (mem_chunk->size == 0)
  305. break;
  306. if (chunk_array[i].type != CHUNK_READ_WRITE &&
  307. chunk_array[i].type != CHUNK_READ_ONLY)
  308. continue;
  309. else
  310. phdr->p_filesz = mem_chunk->size;
  311. phdr->p_type = PT_LOAD;
  312. phdr->p_offset = mem_chunk->addr;
  313. phdr->p_vaddr = mem_chunk->addr;
  314. phdr->p_paddr = mem_chunk->addr;
  315. phdr->p_memsz = mem_chunk->size;
  316. phdr->p_flags = PF_R | PF_W | PF_X;
  317. phdr->p_align = PAGE_SIZE;
  318. phdr++;
  319. }
  320. kfree(chunk_array);
  321. return i;
  322. }
  323. /*
  324. * Initialize notes (new kernel)
  325. */
  326. static void *notes_init(Elf64_Phdr *phdr, void *ptr, u64 notes_offset)
  327. {
  328. struct save_area *sa;
  329. void *ptr_start = ptr;
  330. int i;
  331. ptr = nt_prpsinfo(ptr);
  332. for (i = 0; zfcpdump_save_areas[i]; i++) {
  333. sa = zfcpdump_save_areas[i];
  334. if (sa->pref_reg == 0)
  335. continue;
  336. ptr = fill_cpu_elf_notes(ptr, sa);
  337. }
  338. ptr = nt_vmcoreinfo(ptr);
  339. memset(phdr, 0, sizeof(*phdr));
  340. phdr->p_type = PT_NOTE;
  341. phdr->p_offset = relocate(notes_offset);
  342. phdr->p_filesz = (unsigned long) PTR_SUB(ptr, ptr_start);
  343. phdr->p_memsz = phdr->p_filesz;
  344. return ptr;
  345. }
  346. /*
  347. * Create ELF core header (new kernel)
  348. */
  349. static void s390_elf_corehdr_create(char **elfcorebuf, size_t *elfcorebuf_sz)
  350. {
  351. Elf64_Phdr *phdr_notes, *phdr_loads;
  352. int mem_chunk_cnt;
  353. void *ptr, *hdr;
  354. u32 alloc_size;
  355. u64 hdr_off;
  356. mem_chunk_cnt = get_mem_chunk_cnt();
  357. alloc_size = 0x1000 + get_cpu_cnt() * 0x300 +
  358. mem_chunk_cnt * sizeof(Elf64_Phdr);
  359. hdr = kzalloc_panic(alloc_size);
  360. /* Init elf header */
  361. ptr = ehdr_init(hdr, mem_chunk_cnt);
  362. /* Init program headers */
  363. phdr_notes = ptr;
  364. ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr));
  365. phdr_loads = ptr;
  366. ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr) * mem_chunk_cnt);
  367. /* Init notes */
  368. hdr_off = PTR_DIFF(ptr, hdr);
  369. ptr = notes_init(phdr_notes, ptr, ((unsigned long) hdr) + hdr_off);
  370. /* Init loads */
  371. hdr_off = PTR_DIFF(ptr, hdr);
  372. loads_init(phdr_loads, ((unsigned long) hdr) + hdr_off);
  373. *elfcorebuf_sz = hdr_off;
  374. *elfcorebuf = (void *) relocate((unsigned long) hdr);
  375. BUG_ON(*elfcorebuf_sz > alloc_size);
  376. }
  377. /*
  378. * Create kdump ELF core header in new kernel, if it has not been passed via
  379. * the "elfcorehdr" kernel parameter
  380. */
  381. static int setup_kdump_elfcorehdr(void)
  382. {
  383. size_t elfcorebuf_sz;
  384. char *elfcorebuf;
  385. if (!OLDMEM_BASE || is_kdump_kernel())
  386. return -EINVAL;
  387. s390_elf_corehdr_create(&elfcorebuf, &elfcorebuf_sz);
  388. elfcorehdr_addr = (unsigned long long) elfcorebuf;
  389. elfcorehdr_size = elfcorebuf_sz;
  390. return 0;
  391. }
  392. subsys_initcall(setup_kdump_elfcorehdr);