kcore.c 15 KB

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