blob: 8f3a6f085c5f0ffda0d1c4b220292d8ec757d2d0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
18#include <linux/types.h>
19#include <linux/errno.h>
20#include <linux/time.h>
21#include <linux/kernel.h>
22#include <linux/kernel_stat.h>
Neil Horman7170be52006-01-14 13:20:38 -080023#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/tty.h>
25#include <linux/string.h>
26#include <linux/mman.h>
KOSAKI Motohiro4b856152008-09-02 14:35:53 -070027#include <linux/quicklist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/proc_fs.h>
29#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/mm.h>
31#include <linux/mmzone.h>
32#include <linux/pagemap.h>
Yinghai Luc7fb03a2008-08-19 20:50:12 -070033#include <linux/irq.h>
Adrian Bunkf74596d2008-02-06 01:36:35 -080034#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/swap.h>
36#include <linux/slab.h>
Adrian Bunka0db7012008-03-04 11:23:50 +010037#include <linux/genhd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/smp.h>
39#include <linux/signal.h>
40#include <linux/module.h>
41#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/seq_file.h>
43#include <linux/times.h>
44#include <linux/profile.h>
Andrew Morton7bf65382006-12-08 02:41:14 -080045#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/blkdev.h>
47#include <linux/hugetlb.h>
48#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/vmalloc.h>
Vivek Goyal666bfdd2005-06-25 14:58:21 -070050#include <linux/crash_dump.h>
Sukadev Bhattiprolu61a58c62006-12-08 02:37:58 -080051#include <linux/pid_namespace.h>
Matt Mackall161f47b2008-02-04 22:29:05 -080052#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/uaccess.h>
54#include <asm/pgtable.h>
55#include <asm/io.h>
56#include <asm/tlb.h>
57#include <asm/div64.h>
58#include "internal.h"
59
David Howells93614012006-09-30 20:45:40 +020060#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static int diskstats_open(struct inode *inode, struct file *file)
62{
63 return seq_open(file, &diskstats_op);
64}
Arjan van de Ven00977a52007-02-12 00:55:34 -080065static const struct file_operations proc_diskstats_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 .open = diskstats_open,
67 .read = seq_read,
68 .llseek = seq_lseek,
69 .release = seq_release,
70};
David Howells93614012006-09-30 20:45:40 +020071#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73#ifdef CONFIG_MODULES
Jan Engelhardt03a44822008-02-08 04:21:19 -080074extern const struct seq_operations modules_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static int modules_open(struct inode *inode, struct file *file)
76{
77 return seq_open(file, &modules_op);
78}
Arjan van de Ven00977a52007-02-12 00:55:34 -080079static const struct file_operations proc_modules_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 .open = modules_open,
81 .read = seq_read,
82 .llseek = seq_lseek,
83 .release = seq_release,
84};
85#endif
86
Matt Mackall1e883282008-02-04 22:29:07 -080087#ifdef CONFIG_PROC_PAGE_MONITOR
Matt Mackall161f47b2008-02-04 22:29:05 -080088#define KPMSIZE sizeof(u64)
89#define KPMMASK (KPMSIZE - 1)
90/* /proc/kpagecount - an array exposing page counts
91 *
92 * Each entry is a u64 representing the corresponding
93 * physical page count.
94 */
95static ssize_t kpagecount_read(struct file *file, char __user *buf,
96 size_t count, loff_t *ppos)
97{
98 u64 __user *out = (u64 __user *)buf;
99 struct page *ppage;
100 unsigned long src = *ppos;
101 unsigned long pfn;
102 ssize_t ret = 0;
103 u64 pcount;
104
105 pfn = src / KPMSIZE;
106 count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
107 if (src & KPMMASK || count & KPMMASK)
Thomas Tuttle4710d1a2008-06-05 22:46:58 -0700108 return -EINVAL;
Matt Mackall161f47b2008-02-04 22:29:05 -0800109
110 while (count > 0) {
Matt Mackall304daa82008-02-04 22:29:06 -0800111 ppage = NULL;
112 if (pfn_valid(pfn))
113 ppage = pfn_to_page(pfn);
114 pfn++;
Matt Mackall161f47b2008-02-04 22:29:05 -0800115 if (!ppage)
116 pcount = 0;
117 else
Thomas Tuttlebbcdac02008-06-05 22:46:58 -0700118 pcount = page_mapcount(ppage);
Matt Mackall161f47b2008-02-04 22:29:05 -0800119
120 if (put_user(pcount, out++)) {
121 ret = -EFAULT;
122 break;
123 }
124
125 count -= KPMSIZE;
126 }
127
128 *ppos += (char __user *)out - buf;
129 if (!ret)
130 ret = (char __user *)out - buf;
131 return ret;
132}
133
134static struct file_operations proc_kpagecount_operations = {
135 .llseek = mem_lseek,
136 .read = kpagecount_read,
137};
138
Matt Mackall304daa82008-02-04 22:29:06 -0800139/* /proc/kpageflags - an array exposing page flags
140 *
141 * Each entry is a u64 representing the corresponding
142 * physical page flags.
143 */
144
145/* These macros are used to decouple internal flags from exported ones */
146
147#define KPF_LOCKED 0
148#define KPF_ERROR 1
149#define KPF_REFERENCED 2
150#define KPF_UPTODATE 3
151#define KPF_DIRTY 4
152#define KPF_LRU 5
153#define KPF_ACTIVE 6
154#define KPF_SLAB 7
155#define KPF_WRITEBACK 8
156#define KPF_RECLAIM 9
157#define KPF_BUDDY 10
158
159#define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
160
161static ssize_t kpageflags_read(struct file *file, char __user *buf,
162 size_t count, loff_t *ppos)
163{
164 u64 __user *out = (u64 __user *)buf;
165 struct page *ppage;
166 unsigned long src = *ppos;
167 unsigned long pfn;
168 ssize_t ret = 0;
169 u64 kflags, uflags;
170
171 pfn = src / KPMSIZE;
172 count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
173 if (src & KPMMASK || count & KPMMASK)
Thomas Tuttle4710d1a2008-06-05 22:46:58 -0700174 return -EINVAL;
Matt Mackall304daa82008-02-04 22:29:06 -0800175
176 while (count > 0) {
177 ppage = NULL;
178 if (pfn_valid(pfn))
179 ppage = pfn_to_page(pfn);
180 pfn++;
181 if (!ppage)
182 kflags = 0;
183 else
184 kflags = ppage->flags;
185
186 uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
187 kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
188 kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
189 kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
190 kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
191 kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
192 kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
193 kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
194 kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
195 kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
196 kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
197
198 if (put_user(uflags, out++)) {
199 ret = -EFAULT;
200 break;
201 }
202
203 count -= KPMSIZE;
204 }
205
206 *ppos += (char __user *)out - buf;
207 if (!ret)
208 ret = (char __user *)out - buf;
209 return ret;
210}
211
212static struct file_operations proc_kpageflags_operations = {
213 .llseek = mem_lseek,
214 .read = kpageflags_read,
215};
Matt Mackall1e883282008-02-04 22:29:07 -0800216#endif /* CONFIG_PROC_PAGE_MONITOR */
Matt Mackall304daa82008-02-04 22:29:06 -0800217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218struct proc_dir_entry *proc_root_kcore;
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220void __init proc_misc_init(void)
221{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 proc_symlink("mounts", NULL, "self/mounts");
223
224 /* And now for trickier ones */
David Howells93614012006-09-30 20:45:40 +0200225#ifdef CONFIG_BLOCK
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700226 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
David Howells93614012006-09-30 20:45:40 +0200227#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228#ifdef CONFIG_MODULES
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700229 proc_create("modules", 0, NULL, &proc_modules_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230#endif
231#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700232 proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233#endif
234#ifdef CONFIG_PROC_KCORE
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700235 proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
236 if (proc_root_kcore)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 proc_root_kcore->size =
238 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239#endif
Matt Mackall1e883282008-02-04 22:29:07 -0800240#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700241 proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
242 proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
Matt Mackall1e883282008-02-04 22:29:07 -0800243#endif
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700244#ifdef CONFIG_PROC_VMCORE
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700245 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700246#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}