proc_misc.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * linux/fs/proc/proc_misc.c
  3. *
  4. * linux/fs/proc/array.c
  5. * Copyright (C) 1992 by Linus Torvalds
  6. * based on ideas by Darren Senn
  7. *
  8. * This used to be the part of array.c. See the rest of history and credits
  9. * there. I took this into a separate file and switched the thing to generic
  10. * proc_file_inode_operations, leaving in array.c only per-process stuff.
  11. * Inumbers allocation made dynamic (via create_proc_entry()). AV, May 1999.
  12. *
  13. * Changes:
  14. * Fulton Green : Encapsulated position metric calculations.
  15. * <kernel@FultonGreen.com>
  16. */
  17. #include <linux/types.h>
  18. #include <linux/errno.h>
  19. #include <linux/time.h>
  20. #include <linux/kernel.h>
  21. #include <linux/kernel_stat.h>
  22. #include <linux/fs.h>
  23. #include <linux/tty.h>
  24. #include <linux/string.h>
  25. #include <linux/mman.h>
  26. #include <linux/quicklist.h>
  27. #include <linux/proc_fs.h>
  28. #include <linux/ioport.h>
  29. #include <linux/mm.h>
  30. #include <linux/mmzone.h>
  31. #include <linux/pagemap.h>
  32. #include <linux/irq.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/swap.h>
  35. #include <linux/slab.h>
  36. #include <linux/genhd.h>
  37. #include <linux/smp.h>
  38. #include <linux/signal.h>
  39. #include <linux/module.h>
  40. #include <linux/init.h>
  41. #include <linux/seq_file.h>
  42. #include <linux/times.h>
  43. #include <linux/profile.h>
  44. #include <linux/utsname.h>
  45. #include <linux/blkdev.h>
  46. #include <linux/hugetlb.h>
  47. #include <linux/jiffies.h>
  48. #include <linux/vmalloc.h>
  49. #include <linux/crash_dump.h>
  50. #include <linux/pid_namespace.h>
  51. #include <linux/bootmem.h>
  52. #include <asm/uaccess.h>
  53. #include <asm/pgtable.h>
  54. #include <asm/io.h>
  55. #include <asm/tlb.h>
  56. #include <asm/div64.h>
  57. #include "internal.h"
  58. static int zoneinfo_open(struct inode *inode, struct file *file)
  59. {
  60. return seq_open(file, &zoneinfo_op);
  61. }
  62. static const struct file_operations proc_zoneinfo_file_operations = {
  63. .open = zoneinfo_open,
  64. .read = seq_read,
  65. .llseek = seq_lseek,
  66. .release = seq_release,
  67. };
  68. #ifdef CONFIG_BLOCK
  69. static int diskstats_open(struct inode *inode, struct file *file)
  70. {
  71. return seq_open(file, &diskstats_op);
  72. }
  73. static const struct file_operations proc_diskstats_operations = {
  74. .open = diskstats_open,
  75. .read = seq_read,
  76. .llseek = seq_lseek,
  77. .release = seq_release,
  78. };
  79. #endif
  80. #ifdef CONFIG_MODULES
  81. extern const struct seq_operations modules_op;
  82. static int modules_open(struct inode *inode, struct file *file)
  83. {
  84. return seq_open(file, &modules_op);
  85. }
  86. static const struct file_operations proc_modules_operations = {
  87. .open = modules_open,
  88. .read = seq_read,
  89. .llseek = seq_lseek,
  90. .release = seq_release,
  91. };
  92. #endif
  93. #ifdef CONFIG_PROC_PAGE_MONITOR
  94. #define KPMSIZE sizeof(u64)
  95. #define KPMMASK (KPMSIZE - 1)
  96. /* /proc/kpagecount - an array exposing page counts
  97. *
  98. * Each entry is a u64 representing the corresponding
  99. * physical page count.
  100. */
  101. static ssize_t kpagecount_read(struct file *file, char __user *buf,
  102. size_t count, loff_t *ppos)
  103. {
  104. u64 __user *out = (u64 __user *)buf;
  105. struct page *ppage;
  106. unsigned long src = *ppos;
  107. unsigned long pfn;
  108. ssize_t ret = 0;
  109. u64 pcount;
  110. pfn = src / KPMSIZE;
  111. count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
  112. if (src & KPMMASK || count & KPMMASK)
  113. return -EINVAL;
  114. while (count > 0) {
  115. ppage = NULL;
  116. if (pfn_valid(pfn))
  117. ppage = pfn_to_page(pfn);
  118. pfn++;
  119. if (!ppage)
  120. pcount = 0;
  121. else
  122. pcount = page_mapcount(ppage);
  123. if (put_user(pcount, out++)) {
  124. ret = -EFAULT;
  125. break;
  126. }
  127. count -= KPMSIZE;
  128. }
  129. *ppos += (char __user *)out - buf;
  130. if (!ret)
  131. ret = (char __user *)out - buf;
  132. return ret;
  133. }
  134. static struct file_operations proc_kpagecount_operations = {
  135. .llseek = mem_lseek,
  136. .read = kpagecount_read,
  137. };
  138. /* /proc/kpageflags - an array exposing page flags
  139. *
  140. * Each entry is a u64 representing the corresponding
  141. * physical page flags.
  142. */
  143. /* These macros are used to decouple internal flags from exported ones */
  144. #define KPF_LOCKED 0
  145. #define KPF_ERROR 1
  146. #define KPF_REFERENCED 2
  147. #define KPF_UPTODATE 3
  148. #define KPF_DIRTY 4
  149. #define KPF_LRU 5
  150. #define KPF_ACTIVE 6
  151. #define KPF_SLAB 7
  152. #define KPF_WRITEBACK 8
  153. #define KPF_RECLAIM 9
  154. #define KPF_BUDDY 10
  155. #define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
  156. static ssize_t kpageflags_read(struct file *file, char __user *buf,
  157. size_t count, loff_t *ppos)
  158. {
  159. u64 __user *out = (u64 __user *)buf;
  160. struct page *ppage;
  161. unsigned long src = *ppos;
  162. unsigned long pfn;
  163. ssize_t ret = 0;
  164. u64 kflags, uflags;
  165. pfn = src / KPMSIZE;
  166. count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
  167. if (src & KPMMASK || count & KPMMASK)
  168. return -EINVAL;
  169. while (count > 0) {
  170. ppage = NULL;
  171. if (pfn_valid(pfn))
  172. ppage = pfn_to_page(pfn);
  173. pfn++;
  174. if (!ppage)
  175. kflags = 0;
  176. else
  177. kflags = ppage->flags;
  178. uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
  179. kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
  180. kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
  181. kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
  182. kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
  183. kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
  184. kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
  185. kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
  186. kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
  187. kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
  188. kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
  189. if (put_user(uflags, out++)) {
  190. ret = -EFAULT;
  191. break;
  192. }
  193. count -= KPMSIZE;
  194. }
  195. *ppos += (char __user *)out - buf;
  196. if (!ret)
  197. ret = (char __user *)out - buf;
  198. return ret;
  199. }
  200. static struct file_operations proc_kpageflags_operations = {
  201. .llseek = mem_lseek,
  202. .read = kpageflags_read,
  203. };
  204. #endif /* CONFIG_PROC_PAGE_MONITOR */
  205. struct proc_dir_entry *proc_root_kcore;
  206. void __init proc_misc_init(void)
  207. {
  208. proc_symlink("mounts", NULL, "self/mounts");
  209. /* And now for trickier ones */
  210. proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
  211. #ifdef CONFIG_BLOCK
  212. proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
  213. #endif
  214. #ifdef CONFIG_MODULES
  215. proc_create("modules", 0, NULL, &proc_modules_operations);
  216. #endif
  217. #ifdef CONFIG_SCHEDSTATS
  218. proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
  219. #endif
  220. #ifdef CONFIG_PROC_KCORE
  221. proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
  222. if (proc_root_kcore)
  223. proc_root_kcore->size =
  224. (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
  225. #endif
  226. #ifdef CONFIG_PROC_PAGE_MONITOR
  227. proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
  228. proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
  229. #endif
  230. #ifdef CONFIG_PROC_VMCORE
  231. proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
  232. #endif
  233. }