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