kcore.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/proc/kcore.c kernel ELF core dumper
  4. *
  5. * Modelled on fs/exec.c:aout_core_dump()
  6. * Jeremy Fitzhardinge <[email protected]>
  7. * ELF version written by David Howells <[email protected]>
  8. * Modified and incorporated into 2.3.x by Tigran Aivazian <[email protected]>
  9. * Support to dump vmalloc'd areas (ELF only), Tigran Aivazian <[email protected]>
  10. * Safe accesses to vmalloc/direct-mapped discontiguous areas, Kanoj Sarcar <[email protected]>
  11. */
  12. #include <linux/crash_core.h>
  13. #include <linux/mm.h>
  14. #include <linux/proc_fs.h>
  15. #include <linux/kcore.h>
  16. #include <linux/user.h>
  17. #include <linux/capability.h>
  18. #include <linux/elf.h>
  19. #include <linux/elfcore.h>
  20. #include <linux/notifier.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/highmem.h>
  23. #include <linux/printk.h>
  24. #include <linux/memblock.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/uaccess.h>
  28. #include <asm/io.h>
  29. #include <linux/list.h>
  30. #include <linux/ioport.h>
  31. #include <linux/memory.h>
  32. #include <linux/sched/task.h>
  33. #include <linux/security.h>
  34. #include <asm/sections.h>
  35. #include "internal.h"
  36. #define CORE_STR "CORE"
  37. #ifndef ELF_CORE_EFLAGS
  38. #define ELF_CORE_EFLAGS 0
  39. #endif
  40. static struct proc_dir_entry *proc_root_kcore;
  41. #ifndef kc_vaddr_to_offset
  42. #define kc_vaddr_to_offset(v) ((v) - PAGE_OFFSET)
  43. #endif
  44. #ifndef kc_offset_to_vaddr
  45. #define kc_offset_to_vaddr(o) ((o) + PAGE_OFFSET)
  46. #endif
  47. static LIST_HEAD(kclist_head);
  48. static DECLARE_RWSEM(kclist_lock);
  49. static int kcore_need_update = 1;
  50. /*
  51. * Returns > 0 for RAM pages, 0 for non-RAM pages, < 0 on error
  52. * Same as oldmem_pfn_is_ram in vmcore
  53. */
  54. static int (*mem_pfn_is_ram)(unsigned long pfn);
  55. int __init register_mem_pfn_is_ram(int (*fn)(unsigned long pfn))
  56. {
  57. if (mem_pfn_is_ram)
  58. return -EBUSY;
  59. mem_pfn_is_ram = fn;
  60. return 0;
  61. }
  62. static int pfn_is_ram(unsigned long pfn)
  63. {
  64. if (mem_pfn_is_ram)
  65. return mem_pfn_is_ram(pfn);
  66. else
  67. return 1;
  68. }
  69. /* This doesn't grab kclist_lock, so it should only be used at init time. */
  70. void __init kclist_add(struct kcore_list *new, void *addr, size_t size,
  71. int type)
  72. {
  73. new->addr = (unsigned long)addr;
  74. new->size = size;
  75. new->type = type;
  76. list_add_tail(&new->list, &kclist_head);
  77. }
  78. static size_t get_kcore_size(int *nphdr, size_t *phdrs_len, size_t *notes_len,
  79. size_t *data_offset)
  80. {
  81. size_t try, size;
  82. struct kcore_list *m;
  83. *nphdr = 1; /* PT_NOTE */
  84. size = 0;
  85. list_for_each_entry(m, &kclist_head, list) {
  86. try = kc_vaddr_to_offset((size_t)m->addr + m->size);
  87. if (try > size)
  88. size = try;
  89. *nphdr = *nphdr + 1;
  90. }
  91. *phdrs_len = *nphdr * sizeof(struct elf_phdr);
  92. *notes_len = (4 * sizeof(struct elf_note) +
  93. 3 * ALIGN(sizeof(CORE_STR), 4) +
  94. VMCOREINFO_NOTE_NAME_BYTES +
  95. ALIGN(sizeof(struct elf_prstatus), 4) +
  96. ALIGN(sizeof(struct elf_prpsinfo), 4) +
  97. ALIGN(arch_task_struct_size, 4) +
  98. ALIGN(vmcoreinfo_size, 4));
  99. *data_offset = PAGE_ALIGN(sizeof(struct elfhdr) + *phdrs_len +
  100. *notes_len);
  101. return *data_offset + size;
  102. }
  103. #ifdef CONFIG_HIGHMEM
  104. /*
  105. * If no highmem, we can assume [0...max_low_pfn) continuous range of memory
  106. * because memory hole is not as big as !HIGHMEM case.
  107. * (HIGHMEM is special because part of memory is _invisible_ from the kernel.)
  108. */
  109. static int kcore_ram_list(struct list_head *head)
  110. {
  111. struct kcore_list *ent;
  112. ent = kmalloc(sizeof(*ent), GFP_KERNEL);
  113. if (!ent)
  114. return -ENOMEM;
  115. ent->addr = (unsigned long)__va(0);
  116. ent->size = max_low_pfn << PAGE_SHIFT;
  117. ent->type = KCORE_RAM;
  118. list_add(&ent->list, head);
  119. return 0;
  120. }
  121. #else /* !CONFIG_HIGHMEM */
  122. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  123. /* calculate vmemmap's address from given system ram pfn and register it */
  124. static int
  125. get_sparsemem_vmemmap_info(struct kcore_list *ent, struct list_head *head)
  126. {
  127. unsigned long pfn = __pa(ent->addr) >> PAGE_SHIFT;
  128. unsigned long nr_pages = ent->size >> PAGE_SHIFT;
  129. unsigned long start, end;
  130. struct kcore_list *vmm, *tmp;
  131. start = ((unsigned long)pfn_to_page(pfn)) & PAGE_MASK;
  132. end = ((unsigned long)pfn_to_page(pfn + nr_pages)) - 1;
  133. end = PAGE_ALIGN(end);
  134. /* overlap check (because we have to align page */
  135. list_for_each_entry(tmp, head, list) {
  136. if (tmp->type != KCORE_VMEMMAP)
  137. continue;
  138. if (start < tmp->addr + tmp->size)
  139. if (end > tmp->addr)
  140. end = tmp->addr;
  141. }
  142. if (start < end) {
  143. vmm = kmalloc(sizeof(*vmm), GFP_KERNEL);
  144. if (!vmm)
  145. return 0;
  146. vmm->addr = start;
  147. vmm->size = end - start;
  148. vmm->type = KCORE_VMEMMAP;
  149. list_add_tail(&vmm->list, head);
  150. }
  151. return 1;
  152. }
  153. #else
  154. static int
  155. get_sparsemem_vmemmap_info(struct kcore_list *ent, struct list_head *head)
  156. {
  157. return 1;
  158. }
  159. #endif
  160. static int
  161. kclist_add_private(unsigned long pfn, unsigned long nr_pages, void *arg)
  162. {
  163. struct list_head *head = (struct list_head *)arg;
  164. struct kcore_list *ent;
  165. struct page *p;
  166. if (!pfn_valid(pfn))
  167. return 1;
  168. p = pfn_to_page(pfn);
  169. ent = kmalloc(sizeof(*ent), GFP_KERNEL);
  170. if (!ent)
  171. return -ENOMEM;
  172. ent->addr = (unsigned long)page_to_virt(p);
  173. ent->size = nr_pages << PAGE_SHIFT;
  174. if (!virt_addr_valid(ent->addr))
  175. goto free_out;
  176. /* cut not-mapped area. ....from ppc-32 code. */
  177. if (ULONG_MAX - ent->addr < ent->size)
  178. ent->size = ULONG_MAX - ent->addr;
  179. /*
  180. * We've already checked virt_addr_valid so we know this address
  181. * is a valid pointer, therefore we can check against it to determine
  182. * if we need to trim
  183. */
  184. if (VMALLOC_START > ent->addr) {
  185. if (VMALLOC_START - ent->addr < ent->size)
  186. ent->size = VMALLOC_START - ent->addr;
  187. }
  188. ent->type = KCORE_RAM;
  189. list_add_tail(&ent->list, head);
  190. if (!get_sparsemem_vmemmap_info(ent, head)) {
  191. list_del(&ent->list);
  192. goto free_out;
  193. }
  194. return 0;
  195. free_out:
  196. kfree(ent);
  197. return 1;
  198. }
  199. static int kcore_ram_list(struct list_head *list)
  200. {
  201. int nid, ret;
  202. unsigned long end_pfn;
  203. /* Not inialized....update now */
  204. /* find out "max pfn" */
  205. end_pfn = 0;
  206. for_each_node_state(nid, N_MEMORY) {
  207. unsigned long node_end;
  208. node_end = node_end_pfn(nid);
  209. if (end_pfn < node_end)
  210. end_pfn = node_end;
  211. }
  212. /* scan 0 to max_pfn */
  213. ret = walk_system_ram_range(0, end_pfn, list, kclist_add_private);
  214. if (ret)
  215. return -ENOMEM;
  216. return 0;
  217. }
  218. #endif /* CONFIG_HIGHMEM */
  219. static int kcore_update_ram(void)
  220. {
  221. LIST_HEAD(list);
  222. LIST_HEAD(garbage);
  223. int nphdr;
  224. size_t phdrs_len, notes_len, data_offset;
  225. struct kcore_list *tmp, *pos;
  226. int ret = 0;
  227. down_write(&kclist_lock);
  228. if (!xchg(&kcore_need_update, 0))
  229. goto out;
  230. ret = kcore_ram_list(&list);
  231. if (ret) {
  232. /* Couldn't get the RAM list, try again next time. */
  233. WRITE_ONCE(kcore_need_update, 1);
  234. list_splice_tail(&list, &garbage);
  235. goto out;
  236. }
  237. list_for_each_entry_safe(pos, tmp, &kclist_head, list) {
  238. if (pos->type == KCORE_RAM || pos->type == KCORE_VMEMMAP)
  239. list_move(&pos->list, &garbage);
  240. }
  241. list_splice_tail(&list, &kclist_head);
  242. proc_root_kcore->size = get_kcore_size(&nphdr, &phdrs_len, &notes_len,
  243. &data_offset);
  244. out:
  245. up_write(&kclist_lock);
  246. list_for_each_entry_safe(pos, tmp, &garbage, list) {
  247. list_del(&pos->list);
  248. kfree(pos);
  249. }
  250. return ret;
  251. }
  252. static void append_kcore_note(char *notes, size_t *i, const char *name,
  253. unsigned int type, const void *desc,
  254. size_t descsz)
  255. {
  256. struct elf_note *note = (struct elf_note *)&notes[*i];
  257. note->n_namesz = strlen(name) + 1;
  258. note->n_descsz = descsz;
  259. note->n_type = type;
  260. *i += sizeof(*note);
  261. memcpy(&notes[*i], name, note->n_namesz);
  262. *i = ALIGN(*i + note->n_namesz, 4);
  263. memcpy(&notes[*i], desc, descsz);
  264. *i = ALIGN(*i + descsz, 4);
  265. }
  266. static ssize_t
  267. read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
  268. {
  269. char *buf = file->private_data;
  270. size_t phdrs_offset, notes_offset, data_offset;
  271. size_t page_offline_frozen = 1;
  272. size_t phdrs_len, notes_len;
  273. struct kcore_list *m;
  274. size_t tsz;
  275. int nphdr;
  276. unsigned long start;
  277. size_t orig_buflen = buflen;
  278. int ret = 0;
  279. down_read(&kclist_lock);
  280. /*
  281. * Don't race against drivers that set PageOffline() and expect no
  282. * further page access.
  283. */
  284. page_offline_freeze();
  285. get_kcore_size(&nphdr, &phdrs_len, &notes_len, &data_offset);
  286. phdrs_offset = sizeof(struct elfhdr);
  287. notes_offset = phdrs_offset + phdrs_len;
  288. /* ELF file header. */
  289. if (buflen && *fpos < sizeof(struct elfhdr)) {
  290. struct elfhdr ehdr = {
  291. .e_ident = {
  292. [EI_MAG0] = ELFMAG0,
  293. [EI_MAG1] = ELFMAG1,
  294. [EI_MAG2] = ELFMAG2,
  295. [EI_MAG3] = ELFMAG3,
  296. [EI_CLASS] = ELF_CLASS,
  297. [EI_DATA] = ELF_DATA,
  298. [EI_VERSION] = EV_CURRENT,
  299. [EI_OSABI] = ELF_OSABI,
  300. },
  301. .e_type = ET_CORE,
  302. .e_machine = ELF_ARCH,
  303. .e_version = EV_CURRENT,
  304. .e_phoff = sizeof(struct elfhdr),
  305. .e_flags = ELF_CORE_EFLAGS,
  306. .e_ehsize = sizeof(struct elfhdr),
  307. .e_phentsize = sizeof(struct elf_phdr),
  308. .e_phnum = nphdr,
  309. };
  310. tsz = min_t(size_t, buflen, sizeof(struct elfhdr) - *fpos);
  311. if (copy_to_user(buffer, (char *)&ehdr + *fpos, tsz)) {
  312. ret = -EFAULT;
  313. goto out;
  314. }
  315. buffer += tsz;
  316. buflen -= tsz;
  317. *fpos += tsz;
  318. }
  319. /* ELF program headers. */
  320. if (buflen && *fpos < phdrs_offset + phdrs_len) {
  321. struct elf_phdr *phdrs, *phdr;
  322. phdrs = kzalloc(phdrs_len, GFP_KERNEL);
  323. if (!phdrs) {
  324. ret = -ENOMEM;
  325. goto out;
  326. }
  327. phdrs[0].p_type = PT_NOTE;
  328. phdrs[0].p_offset = notes_offset;
  329. phdrs[0].p_filesz = notes_len;
  330. phdr = &phdrs[1];
  331. list_for_each_entry(m, &kclist_head, list) {
  332. phdr->p_type = PT_LOAD;
  333. phdr->p_flags = PF_R | PF_W | PF_X;
  334. phdr->p_offset = kc_vaddr_to_offset(m->addr) + data_offset;
  335. phdr->p_vaddr = (size_t)m->addr;
  336. if (m->type == KCORE_RAM)
  337. phdr->p_paddr = __pa(m->addr);
  338. else if (m->type == KCORE_TEXT)
  339. phdr->p_paddr = __pa_symbol(m->addr);
  340. else
  341. phdr->p_paddr = (elf_addr_t)-1;
  342. phdr->p_filesz = phdr->p_memsz = m->size;
  343. phdr->p_align = PAGE_SIZE;
  344. phdr++;
  345. }
  346. tsz = min_t(size_t, buflen, phdrs_offset + phdrs_len - *fpos);
  347. if (copy_to_user(buffer, (char *)phdrs + *fpos - phdrs_offset,
  348. tsz)) {
  349. kfree(phdrs);
  350. ret = -EFAULT;
  351. goto out;
  352. }
  353. kfree(phdrs);
  354. buffer += tsz;
  355. buflen -= tsz;
  356. *fpos += tsz;
  357. }
  358. /* ELF note segment. */
  359. if (buflen && *fpos < notes_offset + notes_len) {
  360. struct elf_prstatus prstatus = {};
  361. struct elf_prpsinfo prpsinfo = {
  362. .pr_sname = 'R',
  363. .pr_fname = "vmlinux",
  364. };
  365. char *notes;
  366. size_t i = 0;
  367. strlcpy(prpsinfo.pr_psargs, saved_command_line,
  368. sizeof(prpsinfo.pr_psargs));
  369. notes = kzalloc(notes_len, GFP_KERNEL);
  370. if (!notes) {
  371. ret = -ENOMEM;
  372. goto out;
  373. }
  374. append_kcore_note(notes, &i, CORE_STR, NT_PRSTATUS, &prstatus,
  375. sizeof(prstatus));
  376. append_kcore_note(notes, &i, CORE_STR, NT_PRPSINFO, &prpsinfo,
  377. sizeof(prpsinfo));
  378. append_kcore_note(notes, &i, CORE_STR, NT_TASKSTRUCT, current,
  379. arch_task_struct_size);
  380. /*
  381. * vmcoreinfo_size is mostly constant after init time, but it
  382. * can be changed by crash_save_vmcoreinfo(). Racing here with a
  383. * panic on another CPU before the machine goes down is insanely
  384. * unlikely, but it's better to not leave potential buffer
  385. * overflows lying around, regardless.
  386. */
  387. append_kcore_note(notes, &i, VMCOREINFO_NOTE_NAME, 0,
  388. vmcoreinfo_data,
  389. min(vmcoreinfo_size, notes_len - i));
  390. tsz = min_t(size_t, buflen, notes_offset + notes_len - *fpos);
  391. if (copy_to_user(buffer, notes + *fpos - notes_offset, tsz)) {
  392. kfree(notes);
  393. ret = -EFAULT;
  394. goto out;
  395. }
  396. kfree(notes);
  397. buffer += tsz;
  398. buflen -= tsz;
  399. *fpos += tsz;
  400. }
  401. /*
  402. * Check to see if our file offset matches with any of
  403. * the addresses in the elf_phdr on our list.
  404. */
  405. start = kc_offset_to_vaddr(*fpos - data_offset);
  406. if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
  407. tsz = buflen;
  408. m = NULL;
  409. while (buflen) {
  410. struct page *page;
  411. unsigned long pfn;
  412. /*
  413. * If this is the first iteration or the address is not within
  414. * the previous entry, search for a matching entry.
  415. */
  416. if (!m || start < m->addr || start >= m->addr + m->size) {
  417. struct kcore_list *iter;
  418. m = NULL;
  419. list_for_each_entry(iter, &kclist_head, list) {
  420. if (start >= iter->addr &&
  421. start < iter->addr + iter->size) {
  422. m = iter;
  423. break;
  424. }
  425. }
  426. }
  427. if (page_offline_frozen++ % MAX_ORDER_NR_PAGES == 0) {
  428. page_offline_thaw();
  429. cond_resched();
  430. page_offline_freeze();
  431. }
  432. if (!m) {
  433. if (clear_user(buffer, tsz)) {
  434. ret = -EFAULT;
  435. goto out;
  436. }
  437. goto skip;
  438. }
  439. switch (m->type) {
  440. case KCORE_VMALLOC:
  441. vread(buf, (char *)start, tsz);
  442. /* we have to zero-fill user buffer even if no read */
  443. if (copy_to_user(buffer, buf, tsz)) {
  444. ret = -EFAULT;
  445. goto out;
  446. }
  447. break;
  448. case KCORE_USER:
  449. /* User page is handled prior to normal kernel page: */
  450. if (copy_to_user(buffer, (char *)start, tsz)) {
  451. ret = -EFAULT;
  452. goto out;
  453. }
  454. break;
  455. case KCORE_RAM:
  456. pfn = __pa(start) >> PAGE_SHIFT;
  457. page = pfn_to_online_page(pfn);
  458. /*
  459. * Don't read offline sections, logically offline pages
  460. * (e.g., inflated in a balloon), hwpoisoned pages,
  461. * and explicitly excluded physical ranges.
  462. */
  463. if (!page || PageOffline(page) ||
  464. is_page_hwpoison(page) || !pfn_is_ram(pfn)) {
  465. if (clear_user(buffer, tsz)) {
  466. ret = -EFAULT;
  467. goto out;
  468. }
  469. break;
  470. }
  471. fallthrough;
  472. case KCORE_VMEMMAP:
  473. case KCORE_TEXT:
  474. if (kern_addr_valid(start)) {
  475. /*
  476. * Using bounce buffer to bypass the
  477. * hardened user copy kernel text checks.
  478. */
  479. if (copy_from_kernel_nofault(buf, (void *)start,
  480. tsz)) {
  481. if (clear_user(buffer, tsz)) {
  482. ret = -EFAULT;
  483. goto out;
  484. }
  485. } else {
  486. if (copy_to_user(buffer, buf, tsz)) {
  487. ret = -EFAULT;
  488. goto out;
  489. }
  490. }
  491. } else {
  492. if (clear_user(buffer, tsz)) {
  493. ret = -EFAULT;
  494. goto out;
  495. }
  496. }
  497. break;
  498. default:
  499. pr_warn_once("Unhandled KCORE type: %d\n", m->type);
  500. if (clear_user(buffer, tsz)) {
  501. ret = -EFAULT;
  502. goto out;
  503. }
  504. }
  505. skip:
  506. buflen -= tsz;
  507. *fpos += tsz;
  508. buffer += tsz;
  509. start += tsz;
  510. tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
  511. }
  512. out:
  513. page_offline_thaw();
  514. up_read(&kclist_lock);
  515. if (ret)
  516. return ret;
  517. return orig_buflen - buflen;
  518. }
  519. static int open_kcore(struct inode *inode, struct file *filp)
  520. {
  521. int ret = security_locked_down(LOCKDOWN_KCORE);
  522. if (!capable(CAP_SYS_RAWIO))
  523. return -EPERM;
  524. if (ret)
  525. return ret;
  526. filp->private_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
  527. if (!filp->private_data)
  528. return -ENOMEM;
  529. if (kcore_need_update)
  530. kcore_update_ram();
  531. if (i_size_read(inode) != proc_root_kcore->size) {
  532. inode_lock(inode);
  533. i_size_write(inode, proc_root_kcore->size);
  534. inode_unlock(inode);
  535. }
  536. return 0;
  537. }
  538. static int release_kcore(struct inode *inode, struct file *file)
  539. {
  540. kfree(file->private_data);
  541. return 0;
  542. }
  543. static const struct proc_ops kcore_proc_ops = {
  544. .proc_read = read_kcore,
  545. .proc_open = open_kcore,
  546. .proc_release = release_kcore,
  547. .proc_lseek = default_llseek,
  548. };
  549. /* just remember that we have to update kcore */
  550. static int __meminit kcore_callback(struct notifier_block *self,
  551. unsigned long action, void *arg)
  552. {
  553. switch (action) {
  554. case MEM_ONLINE:
  555. case MEM_OFFLINE:
  556. kcore_need_update = 1;
  557. break;
  558. }
  559. return NOTIFY_OK;
  560. }
  561. static struct notifier_block kcore_callback_nb __meminitdata = {
  562. .notifier_call = kcore_callback,
  563. .priority = 0,
  564. };
  565. static struct kcore_list kcore_vmalloc;
  566. #ifdef CONFIG_ARCH_PROC_KCORE_TEXT
  567. static struct kcore_list kcore_text;
  568. /*
  569. * If defined, special segment is used for mapping kernel text instead of
  570. * direct-map area. We need to create special TEXT section.
  571. */
  572. static void __init proc_kcore_text_init(void)
  573. {
  574. kclist_add(&kcore_text, _text, _end - _text, KCORE_TEXT);
  575. }
  576. #else
  577. static void __init proc_kcore_text_init(void)
  578. {
  579. }
  580. #endif
  581. #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
  582. /*
  583. * MODULES_VADDR has no intersection with VMALLOC_ADDR.
  584. */
  585. static struct kcore_list kcore_modules;
  586. static void __init add_modules_range(void)
  587. {
  588. if (MODULES_VADDR != VMALLOC_START && MODULES_END != VMALLOC_END) {
  589. kclist_add(&kcore_modules, (void *)MODULES_VADDR,
  590. MODULES_END - MODULES_VADDR, KCORE_VMALLOC);
  591. }
  592. }
  593. #else
  594. static void __init add_modules_range(void)
  595. {
  596. }
  597. #endif
  598. static int __init proc_kcore_init(void)
  599. {
  600. proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &kcore_proc_ops);
  601. if (!proc_root_kcore) {
  602. pr_err("couldn't create /proc/kcore\n");
  603. return 0; /* Always returns 0. */
  604. }
  605. /* Store text area if it's special */
  606. proc_kcore_text_init();
  607. /* Store vmalloc area */
  608. kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
  609. VMALLOC_END - VMALLOC_START, KCORE_VMALLOC);
  610. add_modules_range();
  611. /* Store direct-map area from physical memory map */
  612. kcore_update_ram();
  613. register_hotmemory_notifier(&kcore_callback_nb);
  614. return 0;
  615. }
  616. fs_initcall(proc_kcore_init);