blob: e7a301d5d4323deb48a4ac24b678af6c6e73ab78 [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
Nikita Danilov295ab932005-06-21 17:14:38 -070060static int zoneinfo_open(struct inode *inode, struct file *file)
61{
62 return seq_open(file, &zoneinfo_op);
63}
64
Arjan van de Ven00977a52007-02-12 00:55:34 -080065static const struct file_operations proc_zoneinfo_file_operations = {
Nikita Danilov295ab932005-06-21 17:14:38 -070066 .open = zoneinfo_open,
67 .read = seq_read,
68 .llseek = seq_lseek,
69 .release = seq_release,
70};
71
David Howells93614012006-09-30 20:45:40 +020072#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static int diskstats_open(struct inode *inode, struct file *file)
74{
75 return seq_open(file, &diskstats_op);
76}
Arjan van de Ven00977a52007-02-12 00:55:34 -080077static const struct file_operations proc_diskstats_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 .open = diskstats_open,
79 .read = seq_read,
80 .llseek = seq_lseek,
81 .release = seq_release,
82};
David Howells93614012006-09-30 20:45:40 +020083#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85#ifdef CONFIG_MODULES
Jan Engelhardt03a44822008-02-08 04:21:19 -080086extern const struct seq_operations modules_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static int modules_open(struct inode *inode, struct file *file)
88{
89 return seq_open(file, &modules_op);
90}
Arjan van de Ven00977a52007-02-12 00:55:34 -080091static const struct file_operations proc_modules_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 .open = modules_open,
93 .read = seq_read,
94 .llseek = seq_lseek,
95 .release = seq_release,
96};
97#endif
98
Matt Mackall1e883282008-02-04 22:29:07 -080099#ifdef CONFIG_PROC_PAGE_MONITOR
Matt Mackall161f47b2008-02-04 22:29:05 -0800100#define KPMSIZE sizeof(u64)
101#define KPMMASK (KPMSIZE - 1)
102/* /proc/kpagecount - an array exposing page counts
103 *
104 * Each entry is a u64 representing the corresponding
105 * physical page count.
106 */
107static ssize_t kpagecount_read(struct file *file, char __user *buf,
108 size_t count, loff_t *ppos)
109{
110 u64 __user *out = (u64 __user *)buf;
111 struct page *ppage;
112 unsigned long src = *ppos;
113 unsigned long pfn;
114 ssize_t ret = 0;
115 u64 pcount;
116
117 pfn = src / KPMSIZE;
118 count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
119 if (src & KPMMASK || count & KPMMASK)
Thomas Tuttle4710d1a2008-06-05 22:46:58 -0700120 return -EINVAL;
Matt Mackall161f47b2008-02-04 22:29:05 -0800121
122 while (count > 0) {
Matt Mackall304daa82008-02-04 22:29:06 -0800123 ppage = NULL;
124 if (pfn_valid(pfn))
125 ppage = pfn_to_page(pfn);
126 pfn++;
Matt Mackall161f47b2008-02-04 22:29:05 -0800127 if (!ppage)
128 pcount = 0;
129 else
Thomas Tuttlebbcdac02008-06-05 22:46:58 -0700130 pcount = page_mapcount(ppage);
Matt Mackall161f47b2008-02-04 22:29:05 -0800131
132 if (put_user(pcount, out++)) {
133 ret = -EFAULT;
134 break;
135 }
136
137 count -= KPMSIZE;
138 }
139
140 *ppos += (char __user *)out - buf;
141 if (!ret)
142 ret = (char __user *)out - buf;
143 return ret;
144}
145
146static struct file_operations proc_kpagecount_operations = {
147 .llseek = mem_lseek,
148 .read = kpagecount_read,
149};
150
Matt Mackall304daa82008-02-04 22:29:06 -0800151/* /proc/kpageflags - an array exposing page flags
152 *
153 * Each entry is a u64 representing the corresponding
154 * physical page flags.
155 */
156
157/* These macros are used to decouple internal flags from exported ones */
158
159#define KPF_LOCKED 0
160#define KPF_ERROR 1
161#define KPF_REFERENCED 2
162#define KPF_UPTODATE 3
163#define KPF_DIRTY 4
164#define KPF_LRU 5
165#define KPF_ACTIVE 6
166#define KPF_SLAB 7
167#define KPF_WRITEBACK 8
168#define KPF_RECLAIM 9
169#define KPF_BUDDY 10
170
171#define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
172
173static ssize_t kpageflags_read(struct file *file, char __user *buf,
174 size_t count, loff_t *ppos)
175{
176 u64 __user *out = (u64 __user *)buf;
177 struct page *ppage;
178 unsigned long src = *ppos;
179 unsigned long pfn;
180 ssize_t ret = 0;
181 u64 kflags, uflags;
182
183 pfn = src / KPMSIZE;
184 count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
185 if (src & KPMMASK || count & KPMMASK)
Thomas Tuttle4710d1a2008-06-05 22:46:58 -0700186 return -EINVAL;
Matt Mackall304daa82008-02-04 22:29:06 -0800187
188 while (count > 0) {
189 ppage = NULL;
190 if (pfn_valid(pfn))
191 ppage = pfn_to_page(pfn);
192 pfn++;
193 if (!ppage)
194 kflags = 0;
195 else
196 kflags = ppage->flags;
197
198 uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
199 kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
200 kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
201 kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
202 kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
203 kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
204 kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
205 kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
206 kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
207 kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
208 kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
209
210 if (put_user(uflags, out++)) {
211 ret = -EFAULT;
212 break;
213 }
214
215 count -= KPMSIZE;
216 }
217
218 *ppos += (char __user *)out - buf;
219 if (!ret)
220 ret = (char __user *)out - buf;
221 return ret;
222}
223
224static struct file_operations proc_kpageflags_operations = {
225 .llseek = mem_lseek,
226 .read = kpageflags_read,
227};
Matt Mackall1e883282008-02-04 22:29:07 -0800228#endif /* CONFIG_PROC_PAGE_MONITOR */
Matt Mackall304daa82008-02-04 22:29:06 -0800229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230struct proc_dir_entry *proc_root_kcore;
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232void __init proc_misc_init(void)
233{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 proc_symlink("mounts", NULL, "self/mounts");
235
236 /* And now for trickier ones */
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700237 proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
David Howells93614012006-09-30 20:45:40 +0200238#ifdef CONFIG_BLOCK
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700239 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
David Howells93614012006-09-30 20:45:40 +0200240#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241#ifdef CONFIG_MODULES
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700242 proc_create("modules", 0, NULL, &proc_modules_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#endif
244#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700245 proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246#endif
247#ifdef CONFIG_PROC_KCORE
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700248 proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
249 if (proc_root_kcore)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 proc_root_kcore->size =
251 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252#endif
Matt Mackall1e883282008-02-04 22:29:07 -0800253#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700254 proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
255 proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
Matt Mackall1e883282008-02-04 22:29:07 -0800256#endif
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700257#ifdef CONFIG_PROC_VMCORE
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700258 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700259#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}