kcore.c 15 KB

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