kcore.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /*
  2. * fs/proc/kcore.c kernel ELF core dumper
  3. *
  4. * Modelled on fs/exec.c:aout_core_dump()
  5. * Jeremy Fitzhardinge <jeremy@sw.oz.au>
  6. * ELF version written by David Howells <David.Howells@nexor.co.uk>
  7. * Modified and incorporated into 2.3.x by Tigran Aivazian <tigran@veritas.com>
  8. * Support to dump vmalloc'd areas (ELF only), Tigran Aivazian <tigran@veritas.com>
  9. * Safe accesses to vmalloc/direct-mapped discontiguous areas, Kanoj Sarcar <kanoj@sgi.com>
  10. */
  11. #include <linux/mm.h>
  12. #include <linux/proc_fs.h>
  13. #include <linux/user.h>
  14. #include <linux/capability.h>
  15. #include <linux/elf.h>
  16. #include <linux/elfcore.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/highmem.h>
  19. #include <linux/bootmem.h>
  20. #include <linux/init.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/io.h>
  23. #include <linux/list.h>
  24. #include <linux/ioport.h>
  25. #include <linux/mm.h>
  26. #include <linux/memory.h>
  27. #include <asm/sections.h>
  28. #define CORE_STR "CORE"
  29. #ifndef ELF_CORE_EFLAGS
  30. #define ELF_CORE_EFLAGS 0
  31. #endif
  32. static struct proc_dir_entry *proc_root_kcore;
  33. #ifndef kc_vaddr_to_offset
  34. #define kc_vaddr_to_offset(v) ((v) - PAGE_OFFSET)
  35. #endif
  36. #ifndef kc_offset_to_vaddr
  37. #define kc_offset_to_vaddr(o) ((o) + PAGE_OFFSET)
  38. #endif
  39. /* An ELF note in memory */
  40. struct memelfnote
  41. {
  42. const char *name;
  43. int type;
  44. unsigned int datasz;
  45. void *data;
  46. };
  47. static LIST_HEAD(kclist_head);
  48. static DEFINE_RWLOCK(kclist_lock);
  49. static int kcore_need_update = 1;
  50. void
  51. kclist_add(struct kcore_list *new, void *addr, size_t size, int type)
  52. {
  53. new->addr = (unsigned long)addr;
  54. new->size = size;
  55. new->type = type;
  56. write_lock(&kclist_lock);
  57. list_add_tail(&new->list, &kclist_head);
  58. write_unlock(&kclist_lock);
  59. }
  60. static size_t get_kcore_size(int *nphdr, size_t *elf_buflen)
  61. {
  62. size_t try, size;
  63. struct kcore_list *m;
  64. *nphdr = 1; /* PT_NOTE */
  65. size = 0;
  66. list_for_each_entry(m, &kclist_head, list) {
  67. try = kc_vaddr_to_offset((size_t)m->addr + m->size);
  68. if (try > size)
  69. size = try;
  70. *nphdr = *nphdr + 1;
  71. }
  72. *elf_buflen = sizeof(struct elfhdr) +
  73. (*nphdr + 2)*sizeof(struct elf_phdr) +
  74. 3 * ((sizeof(struct elf_note)) +
  75. roundup(sizeof(CORE_STR), 4)) +
  76. roundup(sizeof(struct elf_prstatus), 4) +
  77. roundup(sizeof(struct elf_prpsinfo), 4) +
  78. roundup(sizeof(struct task_struct), 4);
  79. *elf_buflen = PAGE_ALIGN(*elf_buflen);
  80. return size + *elf_buflen;
  81. }
  82. static void free_kclist_ents(struct list_head *head)
  83. {
  84. struct kcore_list *tmp, *pos;
  85. list_for_each_entry_safe(pos, tmp, head, list) {
  86. list_del(&pos->list);
  87. kfree(pos);
  88. }
  89. }
  90. /*
  91. * Replace all KCORE_RAM/KCORE_VMEMMAP information with passed list.
  92. */
  93. static void __kcore_update_ram(struct list_head *list)
  94. {
  95. struct kcore_list *tmp, *pos;
  96. LIST_HEAD(garbage);
  97. write_lock(&kclist_lock);
  98. if (kcore_need_update) {
  99. list_for_each_entry_safe(pos, tmp, &kclist_head, list) {
  100. if (pos->type == KCORE_RAM
  101. || pos->type == KCORE_VMEMMAP)
  102. list_move(&pos->list, &garbage);
  103. }
  104. list_splice_tail(list, &kclist_head);
  105. } else
  106. list_splice(list, &garbage);
  107. kcore_need_update = 0;
  108. write_unlock(&kclist_lock);
  109. free_kclist_ents(&garbage);
  110. }
  111. #ifdef CONFIG_HIGHMEM
  112. /*
  113. * If no highmem, we can assume [0...max_low_pfn) continuous range of memory
  114. * because memory hole is not as big as !HIGHMEM case.
  115. * (HIGHMEM is special because part of memory is _invisible_ from the kernel.)
  116. */
  117. static int kcore_update_ram(void)
  118. {
  119. LIST_HEAD(head);
  120. struct kcore_list *ent;
  121. int ret = 0;
  122. ent = kmalloc(sizeof(*ent), GFP_KERNEL);
  123. if (!ent)
  124. return -ENOMEM;
  125. ent->addr = (unsigned long)__va(0);
  126. ent->size = max_low_pfn << PAGE_SHIFT;
  127. ent->type = KCORE_RAM;
  128. list_add(&ent->list, &head);
  129. __kcore_update_ram(&head);
  130. return ret;
  131. }
  132. #else /* !CONFIG_HIGHMEM */
  133. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  134. /* calculate vmemmap's address from given system ram pfn and register it */
  135. int get_sparsemem_vmemmap_info(struct kcore_list *ent, struct list_head *head)
  136. {
  137. unsigned long pfn = __pa(ent->addr) >> PAGE_SHIFT;
  138. unsigned long nr_pages = ent->size >> PAGE_SHIFT;
  139. unsigned long start, end;
  140. struct kcore_list *vmm, *tmp;
  141. start = ((unsigned long)pfn_to_page(pfn)) & PAGE_MASK;
  142. end = ((unsigned long)pfn_to_page(pfn + nr_pages)) - 1;
  143. end = ALIGN(end, PAGE_SIZE);
  144. /* overlap check (because we have to align page */
  145. list_for_each_entry(tmp, head, list) {
  146. if (tmp->type != KCORE_VMEMMAP)
  147. continue;
  148. if (start < tmp->addr + tmp->size)
  149. if (end > tmp->addr)
  150. end = tmp->addr;
  151. }
  152. if (start < end) {
  153. vmm = kmalloc(sizeof(*vmm), GFP_KERNEL);
  154. if (!vmm)
  155. return 0;
  156. vmm->addr = start;
  157. vmm->size = end - start;
  158. vmm->type = KCORE_VMEMMAP;
  159. list_add_tail(&vmm->list, head);
  160. }
  161. return 1;
  162. }
  163. #else
  164. int get_sparsemem_vmemmap_info(struct kcore_list *ent, struct list_head *head)
  165. {
  166. return 1;
  167. }
  168. #endif
  169. static int
  170. kclist_add_private(unsigned long pfn, unsigned long nr_pages, void *arg)
  171. {
  172. struct list_head *head = (struct list_head *)arg;
  173. struct kcore_list *ent;
  174. ent = kmalloc(sizeof(*ent), GFP_KERNEL);
  175. if (!ent)
  176. return -ENOMEM;
  177. ent->addr = (unsigned long)__va((pfn << PAGE_SHIFT));
  178. ent->size = nr_pages << PAGE_SHIFT;
  179. /* Sanity check: Can happen in 32bit arch...maybe */
  180. if (ent->addr < (unsigned long) __va(0))
  181. goto free_out;
  182. /* cut not-mapped area. ....from ppc-32 code. */
  183. if (ULONG_MAX - ent->addr < ent->size)
  184. ent->size = ULONG_MAX - ent->addr;
  185. /* cut when vmalloc() area is higher than direct-map area */
  186. if (VMALLOC_START > (unsigned long)__va(0)) {
  187. if (ent->addr > VMALLOC_START)
  188. goto free_out;
  189. if (VMALLOC_START - ent->addr < ent->size)
  190. ent->size = VMALLOC_START - ent->addr;
  191. }
  192. ent->type = KCORE_RAM;
  193. list_add_tail(&ent->list, head);
  194. if (!get_sparsemem_vmemmap_info(ent, head)) {
  195. list_del(&ent->list);
  196. goto free_out;
  197. }
  198. return 0;
  199. free_out:
  200. kfree(ent);
  201. return 1;
  202. }
  203. static int kcore_update_ram(void)
  204. {
  205. int nid, ret;
  206. unsigned long end_pfn;
  207. LIST_HEAD(head);
  208. /* Not inialized....update now */
  209. /* find out "max pfn" */
  210. end_pfn = 0;
  211. for_each_node_state(nid, N_HIGH_MEMORY) {
  212. unsigned long node_end;
  213. node_end = NODE_DATA(nid)->node_start_pfn +
  214. NODE_DATA(nid)->node_spanned_pages;
  215. if (end_pfn < node_end)
  216. end_pfn = node_end;
  217. }
  218. /* scan 0 to max_pfn */
  219. ret = walk_system_ram_range(0, end_pfn, &head, kclist_add_private);
  220. if (ret) {
  221. free_kclist_ents(&head);
  222. return -ENOMEM;
  223. }
  224. __kcore_update_ram(&head);
  225. return ret;
  226. }
  227. #endif /* CONFIG_HIGHMEM */
  228. /*****************************************************************************/
  229. /*
  230. * determine size of ELF note
  231. */
  232. static int notesize(struct memelfnote *en)
  233. {
  234. int sz;
  235. sz = sizeof(struct elf_note);
  236. sz += roundup((strlen(en->name) + 1), 4);
  237. sz += roundup(en->datasz, 4);
  238. return sz;
  239. } /* end notesize() */
  240. /*****************************************************************************/
  241. /*
  242. * store a note in the header buffer
  243. */
  244. static char *storenote(struct memelfnote *men, char *bufp)
  245. {
  246. struct elf_note en;
  247. #define DUMP_WRITE(addr,nr) do { memcpy(bufp,addr,nr); bufp += nr; } while(0)
  248. en.n_namesz = strlen(men->name) + 1;
  249. en.n_descsz = men->datasz;
  250. en.n_type = men->type;
  251. DUMP_WRITE(&en, sizeof(en));
  252. DUMP_WRITE(men->name, en.n_namesz);
  253. /* XXX - cast from long long to long to avoid need for libgcc.a */
  254. bufp = (char*) roundup((unsigned long)bufp,4);
  255. DUMP_WRITE(men->data, men->datasz);
  256. bufp = (char*) roundup((unsigned long)bufp,4);
  257. #undef DUMP_WRITE
  258. return bufp;
  259. } /* end storenote() */
  260. /*
  261. * store an ELF coredump header in the supplied buffer
  262. * nphdr is the number of elf_phdr to insert
  263. */
  264. static void elf_kcore_store_hdr(char *bufp, int nphdr, int dataoff)
  265. {
  266. struct elf_prstatus prstatus; /* NT_PRSTATUS */
  267. struct elf_prpsinfo prpsinfo; /* NT_PRPSINFO */
  268. struct elf_phdr *nhdr, *phdr;
  269. struct elfhdr *elf;
  270. struct memelfnote notes[3];
  271. off_t offset = 0;
  272. struct kcore_list *m;
  273. /* setup ELF header */
  274. elf = (struct elfhdr *) bufp;
  275. bufp += sizeof(struct elfhdr);
  276. offset += sizeof(struct elfhdr);
  277. memcpy(elf->e_ident, ELFMAG, SELFMAG);
  278. elf->e_ident[EI_CLASS] = ELF_CLASS;
  279. elf->e_ident[EI_DATA] = ELF_DATA;
  280. elf->e_ident[EI_VERSION]= EV_CURRENT;
  281. elf->e_ident[EI_OSABI] = ELF_OSABI;
  282. memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
  283. elf->e_type = ET_CORE;
  284. elf->e_machine = ELF_ARCH;
  285. elf->e_version = EV_CURRENT;
  286. elf->e_entry = 0;
  287. elf->e_phoff = sizeof(struct elfhdr);
  288. elf->e_shoff = 0;
  289. elf->e_flags = ELF_CORE_EFLAGS;
  290. elf->e_ehsize = sizeof(struct elfhdr);
  291. elf->e_phentsize= sizeof(struct elf_phdr);
  292. elf->e_phnum = nphdr;
  293. elf->e_shentsize= 0;
  294. elf->e_shnum = 0;
  295. elf->e_shstrndx = 0;
  296. /* setup ELF PT_NOTE program header */
  297. nhdr = (struct elf_phdr *) bufp;
  298. bufp += sizeof(struct elf_phdr);
  299. offset += sizeof(struct elf_phdr);
  300. nhdr->p_type = PT_NOTE;
  301. nhdr->p_offset = 0;
  302. nhdr->p_vaddr = 0;
  303. nhdr->p_paddr = 0;
  304. nhdr->p_filesz = 0;
  305. nhdr->p_memsz = 0;
  306. nhdr->p_flags = 0;
  307. nhdr->p_align = 0;
  308. /* setup ELF PT_LOAD program header for every area */
  309. list_for_each_entry(m, &kclist_head, list) {
  310. phdr = (struct elf_phdr *) bufp;
  311. bufp += sizeof(struct elf_phdr);
  312. offset += sizeof(struct elf_phdr);
  313. phdr->p_type = PT_LOAD;
  314. phdr->p_flags = PF_R|PF_W|PF_X;
  315. phdr->p_offset = kc_vaddr_to_offset(m->addr) + dataoff;
  316. phdr->p_vaddr = (size_t)m->addr;
  317. phdr->p_paddr = 0;
  318. phdr->p_filesz = phdr->p_memsz = m->size;
  319. phdr->p_align = PAGE_SIZE;
  320. }
  321. /*
  322. * Set up the notes in similar form to SVR4 core dumps made
  323. * with info from their /proc.
  324. */
  325. nhdr->p_offset = offset;
  326. /* set up the process status */
  327. notes[0].name = CORE_STR;
  328. notes[0].type = NT_PRSTATUS;
  329. notes[0].datasz = sizeof(struct elf_prstatus);
  330. notes[0].data = &prstatus;
  331. memset(&prstatus, 0, sizeof(struct elf_prstatus));
  332. nhdr->p_filesz = notesize(&notes[0]);
  333. bufp = storenote(&notes[0], bufp);
  334. /* set up the process info */
  335. notes[1].name = CORE_STR;
  336. notes[1].type = NT_PRPSINFO;
  337. notes[1].datasz = sizeof(struct elf_prpsinfo);
  338. notes[1].data = &prpsinfo;
  339. memset(&prpsinfo, 0, sizeof(struct elf_prpsinfo));
  340. prpsinfo.pr_state = 0;
  341. prpsinfo.pr_sname = 'R';
  342. prpsinfo.pr_zomb = 0;
  343. strcpy(prpsinfo.pr_fname, "vmlinux");
  344. strncpy(prpsinfo.pr_psargs, saved_command_line, ELF_PRARGSZ);
  345. nhdr->p_filesz += notesize(&notes[1]);
  346. bufp = storenote(&notes[1], bufp);
  347. /* set up the task structure */
  348. notes[2].name = CORE_STR;
  349. notes[2].type = NT_TASKSTRUCT;
  350. notes[2].datasz = sizeof(struct task_struct);
  351. notes[2].data = current;
  352. nhdr->p_filesz += notesize(&notes[2]);
  353. bufp = storenote(&notes[2], bufp);
  354. } /* end elf_kcore_store_hdr() */
  355. /*****************************************************************************/
  356. /*
  357. * read from the ELF header and then kernel memory
  358. */
  359. static ssize_t
  360. read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
  361. {
  362. ssize_t acc = 0;
  363. size_t size, tsz;
  364. size_t elf_buflen;
  365. int nphdr;
  366. unsigned long start;
  367. read_lock(&kclist_lock);
  368. proc_root_kcore->size = size = get_kcore_size(&nphdr, &elf_buflen);
  369. if (buflen == 0 || *fpos >= size) {
  370. read_unlock(&kclist_lock);
  371. return 0;
  372. }
  373. /* trim buflen to not go beyond EOF */
  374. if (buflen > size - *fpos)
  375. buflen = size - *fpos;
  376. /* construct an ELF core header if we'll need some of it */
  377. if (*fpos < elf_buflen) {
  378. char * elf_buf;
  379. tsz = elf_buflen - *fpos;
  380. if (buflen < tsz)
  381. tsz = buflen;
  382. elf_buf = kzalloc(elf_buflen, GFP_ATOMIC);
  383. if (!elf_buf) {
  384. read_unlock(&kclist_lock);
  385. return -ENOMEM;
  386. }
  387. elf_kcore_store_hdr(elf_buf, nphdr, elf_buflen);
  388. read_unlock(&kclist_lock);
  389. if (copy_to_user(buffer, elf_buf + *fpos, tsz)) {
  390. kfree(elf_buf);
  391. return -EFAULT;
  392. }
  393. kfree(elf_buf);
  394. buflen -= tsz;
  395. *fpos += tsz;
  396. buffer += tsz;
  397. acc += tsz;
  398. /* leave now if filled buffer already */
  399. if (buflen == 0)
  400. return acc;
  401. } else
  402. read_unlock(&kclist_lock);
  403. /*
  404. * Check to see if our file offset matches with any of
  405. * the addresses in the elf_phdr on our list.
  406. */
  407. start = kc_offset_to_vaddr(*fpos - elf_buflen);
  408. if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
  409. tsz = buflen;
  410. while (buflen) {
  411. struct kcore_list *m;
  412. read_lock(&kclist_lock);
  413. list_for_each_entry(m, &kclist_head, list) {
  414. if (start >= m->addr && start < (m->addr+m->size))
  415. break;
  416. }
  417. read_unlock(&kclist_lock);
  418. if (m == NULL) {
  419. if (clear_user(buffer, tsz))
  420. return -EFAULT;
  421. } else if (is_vmalloc_or_module_addr((void *)start)) {
  422. char * elf_buf;
  423. elf_buf = kzalloc(tsz, GFP_KERNEL);
  424. if (!elf_buf)
  425. return -ENOMEM;
  426. vread(elf_buf, (char *)start, tsz);
  427. /* we have to zero-fill user buffer even if no read */
  428. if (copy_to_user(buffer, elf_buf, tsz)) {
  429. kfree(elf_buf);
  430. return -EFAULT;
  431. }
  432. kfree(elf_buf);
  433. } else {
  434. if (kern_addr_valid(start)) {
  435. unsigned long n;
  436. n = copy_to_user(buffer, (char *)start, tsz);
  437. /*
  438. * We cannot distingush between fault on source
  439. * and fault on destination. When this happens
  440. * we clear too and hope it will trigger the
  441. * EFAULT again.
  442. */
  443. if (n) {
  444. if (clear_user(buffer + tsz - n,
  445. n))
  446. return -EFAULT;
  447. }
  448. } else {
  449. if (clear_user(buffer, tsz))
  450. return -EFAULT;
  451. }
  452. }
  453. buflen -= tsz;
  454. *fpos += tsz;
  455. buffer += tsz;
  456. acc += tsz;
  457. start += tsz;
  458. tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
  459. }
  460. return acc;
  461. }
  462. static int open_kcore(struct inode *inode, struct file *filp)
  463. {
  464. if (!capable(CAP_SYS_RAWIO))
  465. return -EPERM;
  466. if (kcore_need_update)
  467. kcore_update_ram();
  468. return 0;
  469. }
  470. static const struct file_operations proc_kcore_operations = {
  471. .read = read_kcore,
  472. .open = open_kcore,
  473. };
  474. #ifdef CONFIG_MEMORY_HOTPLUG
  475. /* just remember that we have to update kcore */
  476. static int __meminit kcore_callback(struct notifier_block *self,
  477. unsigned long action, void *arg)
  478. {
  479. switch (action) {
  480. case MEM_ONLINE:
  481. case MEM_OFFLINE:
  482. write_lock(&kclist_lock);
  483. kcore_need_update = 1;
  484. write_unlock(&kclist_lock);
  485. }
  486. return NOTIFY_OK;
  487. }
  488. #endif
  489. static struct kcore_list kcore_vmalloc;
  490. #ifdef CONFIG_ARCH_PROC_KCORE_TEXT
  491. static struct kcore_list kcore_text;
  492. /*
  493. * If defined, special segment is used for mapping kernel text instead of
  494. * direct-map area. We need to create special TEXT section.
  495. */
  496. static void __init proc_kcore_text_init(void)
  497. {
  498. kclist_add(&kcore_text, _stext, _end - _stext, KCORE_TEXT);
  499. }
  500. #else
  501. static void __init proc_kcore_text_init(void)
  502. {
  503. }
  504. #endif
  505. #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
  506. /*
  507. * MODULES_VADDR has no intersection with VMALLOC_ADDR.
  508. */
  509. struct kcore_list kcore_modules;
  510. static void __init add_modules_range(void)
  511. {
  512. kclist_add(&kcore_modules, (void *)MODULES_VADDR,
  513. MODULES_END - MODULES_VADDR, KCORE_VMALLOC);
  514. }
  515. #else
  516. static void __init add_modules_range(void)
  517. {
  518. }
  519. #endif
  520. static int __init proc_kcore_init(void)
  521. {
  522. proc_root_kcore = proc_create("kcore", S_IRUSR, NULL,
  523. &proc_kcore_operations);
  524. if (!proc_root_kcore) {
  525. printk(KERN_ERR "couldn't create /proc/kcore\n");
  526. return 0; /* Always returns 0. */
  527. }
  528. /* Store text area if it's special */
  529. proc_kcore_text_init();
  530. /* Store vmalloc area */
  531. kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
  532. VMALLOC_END - VMALLOC_START, KCORE_VMALLOC);
  533. add_modules_range();
  534. /* Store direct-map area from physical memory map */
  535. kcore_update_ram();
  536. hotplug_memory_notifier(kcore_callback, 0);
  537. return 0;
  538. }
  539. module_init(proc_kcore_init);