2 * linux/fs/proc/proc_misc.c
4 * linux/fs/proc/array.c
5 * Copyright (C) 1992 by Linus Torvalds
6 * based on ideas by Darren Senn
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.
14 * Fulton Green : Encapsulated position metric calculations.
15 * <kernel@FultonGreen.com>
18 #include <linux/types.h>
19 #include <linux/errno.h>
20 #include <linux/sched.h>
21 #include <linux/kernel.h>
22 #include <linux/kernel_stat.h>
23 #include <linux/tty.h>
24 #include <linux/string.h>
25 #include <linux/mman.h>
26 #include <linux/proc_fs.h>
27 #include <linux/ioport.h>
28 #include <linux/config.h>
30 #include <linux/pagemap.h>
31 #include <linux/swap.h>
32 #include <linux/slab.h>
33 #include <linux/smp.h>
34 #include <linux/signal.h>
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/smp_lock.h>
38 #include <linux/seq_file.h>
39 #include <linux/sysrq.h>
41 #include <asm/uaccess.h>
42 #include <asm/pgtable.h>
45 #define LOAD_INT(x) ((x) >> FSHIFT)
46 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
48 * Warning: stuff below (imported functions) assumes that its output will fit
49 * into one page. For some of those functions it may be wrong. Moreover, we
50 * have a way to deal with that gracefully. Right now I used straightforward
51 * wrappers, but this needs further analysis wrt potential overflows.
53 extern int get_hardware_list(char *);
54 extern int get_stram_list(char *);
56 extern int get_module_list(char *);
58 extern int get_device_list(char *);
59 extern int get_filesystem_list(char *);
60 extern int get_exec_domain_list(char *);
62 extern int get_irq_list(char *);
64 extern int get_dma_list(char *);
65 extern int get_locks_status (char *, char **, off_t, int);
66 extern int get_swaparea_info (char *);
67 #ifdef CONFIG_SGI_DS1286
68 extern int get_ds1286_status(char *);
71 void proc_sprintf(char *page, off_t *off, int *lenp, const char *format, ...)
76 /* try to only print whole lines */
77 if (len > PAGE_SIZE-512)
80 va_start(args, format);
81 len += vsnprintf(page + len, PAGE_SIZE-len, format, args);
92 static int proc_calc_metrics(char *page, char **start, off_t off,
93 int count, int *eof, int len)
95 if (len <= off+count) *eof = 1;
98 if (len>count) len = count;
103 static int loadavg_read_proc(char *page, char **start, off_t off,
104 int count, int *eof, void *data)
109 a = avenrun[0] + (FIXED_1/200);
110 b = avenrun[1] + (FIXED_1/200);
111 c = avenrun[2] + (FIXED_1/200);
112 len = sprintf(page,"%d.%02d %d.%02d %d.%02d %d/%d %d\n",
113 LOAD_INT(a), LOAD_FRAC(a),
114 LOAD_INT(b), LOAD_FRAC(b),
115 LOAD_INT(c), LOAD_FRAC(c),
116 nr_running, nr_threads, last_pid);
117 return proc_calc_metrics(page, start, off, count, eof, len);
120 static int uptime_read_proc(char *page, char **start, off_t off,
121 int count, int *eof, void *data)
123 unsigned long uptime;
128 idle = init_tasks[0]->times.tms_utime + init_tasks[0]->times.tms_stime;
130 /* The formula for the fraction parts really is ((t * 100) / HZ) % 100, but
131 that would overflow about every five days at HZ == 100.
132 Therefore the identity a = (a / b) * b + a % b is used so that it is
133 calculated as (((t / HZ) * 100) + ((t % HZ) * 100) / HZ) % 100.
134 The part in front of the '+' always evaluates as 0 (mod 100). All divisions
135 in the above formulas are truncating. For HZ being a power of 10, the
136 calculations simplify to the version in the #else part (if the printf
137 format is adapted to the same number of digits as zeroes in HZ.
140 len = sprintf(page,"%lu.%02lu %lu.%02lu\n",
142 (((uptime % HZ) * 100) / HZ) % 100,
144 (((idle % HZ) * 100) / HZ) % 100);
146 len = sprintf(page,"%lu.%02lu %lu.%02lu\n",
152 return proc_calc_metrics(page, start, off, count, eof, len);
155 static int meminfo_read_proc(char *page, char **start, off_t off,
156 int count, int *eof, void *data)
163 * display in kilobytes.
165 #define K(x) ((x) << (PAGE_SHIFT - 10))
166 #define B(x) ((unsigned long long)(x) << PAGE_SHIFT)
169 pg_size = page_cache_size - i.bufferram;
171 len = sprintf(page, " total: used: free: shared: buffers: cached:\n"
172 "Mem: %8Lu %8Lu %8Lu %8Lu %8Lu %8Lu\n"
173 "Swap: %8Lu %8Lu %8Lu\n",
174 B(i.totalram), B(i.totalram-i.freeram), B(i.freeram),
175 B(i.sharedram), B(i.bufferram),
176 B(pg_size), B(i.totalswap),
177 B(i.totalswap-i.freeswap), B(i.freeswap));
179 * Tagged format, for easy grepping and expansion.
180 * The above will go away eventually, once the tools
183 len += sprintf(page+len,
184 "MemTotal: %8lu kB\n"
186 "MemShared: %8lu kB\n"
189 "SwapCached: %8lu kB\n"
192 "HighTotal: %8lu kB\n"
193 "HighFree: %8lu kB\n"
194 "LowTotal: %8lu kB\n"
196 "SwapTotal: %8lu kB\n"
197 "SwapFree: %8lu kB\n",
202 K(pg_size - swapper_space.nrpages),
203 K(swapper_space.nrpages),
205 K(nr_inactive_pages),
208 K(i.totalram-i.totalhigh),
209 K(i.freeram-i.freehigh),
213 return proc_calc_metrics(page, start, off, count, eof, len);
218 static int version_read_proc(char *page, char **start, off_t off,
219 int count, int *eof, void *data)
221 extern char *linux_banner;
224 strcpy(page, linux_banner);
226 return proc_calc_metrics(page, start, off, count, eof, len);
229 extern struct seq_operations cpuinfo_op;
230 static int cpuinfo_open(struct inode *inode, struct file *file)
232 return seq_open(file, &cpuinfo_op);
234 static struct file_operations proc_cpuinfo_operations = {
238 release: seq_release,
241 #ifdef CONFIG_PROC_HARDWARE
242 static int hardware_read_proc(char *page, char **start, off_t off,
243 int count, int *eof, void *data)
245 int len = get_hardware_list(page);
246 return proc_calc_metrics(page, start, off, count, eof, len);
250 #ifdef CONFIG_STRAM_PROC
251 static int stram_read_proc(char *page, char **start, off_t off,
252 int count, int *eof, void *data)
254 int len = get_stram_list(page);
255 return proc_calc_metrics(page, start, off, count, eof, len);
259 extern struct seq_operations partitions_op;
260 static int partitions_open(struct inode *inode, struct file *file)
262 return seq_open(file, &partitions_op);
264 static struct file_operations proc_partitions_operations = {
265 open: partitions_open,
268 release: seq_release,
271 #ifdef CONFIG_MODULES
272 static int modules_read_proc(char *page, char **start, off_t off,
273 int count, int *eof, void *data)
275 int len = get_module_list(page);
276 return proc_calc_metrics(page, start, off, count, eof, len);
279 extern struct seq_operations ksyms_op;
280 static int ksyms_open(struct inode *inode, struct file *file)
282 return seq_open(file, &ksyms_op);
284 static struct file_operations proc_ksyms_operations = {
288 release: seq_release,
292 extern struct seq_operations slabinfo_op;
293 extern ssize_t slabinfo_write(struct file *, const char *, size_t, loff_t *);
294 static int slabinfo_open(struct inode *inode, struct file *file)
296 return seq_open(file, &slabinfo_op);
298 static struct file_operations proc_slabinfo_operations = {
301 write: slabinfo_write,
303 release: seq_release,
306 static int kstat_read_proc(char *page, char **start, off_t off,
307 int count, int *eof, void *data)
310 extern unsigned long total_forks;
311 unsigned long jif = jiffies;
312 unsigned int sum = 0, user = 0, nice = 0, system = 0;
315 for (i = 0 ; i < smp_num_cpus; i++) {
316 int cpu = cpu_logical_map(i), j;
318 user += kstat.per_cpu_user[cpu];
319 nice += kstat.per_cpu_nice[cpu];
320 system += kstat.per_cpu_system[cpu];
321 #if !defined(CONFIG_ARCH_S390)
322 for (j = 0 ; j < NR_IRQS ; j++)
323 sum += kstat.irqs[cpu][j];
327 proc_sprintf(page, &off, &len,
328 "cpu %u %u %u %lu\n", user, nice, system,
329 jif * smp_num_cpus - (user + nice + system));
330 for (i = 0 ; i < smp_num_cpus; i++)
331 proc_sprintf(page, &off, &len,
332 "cpu%d %u %u %u %lu\n",
334 kstat.per_cpu_user[cpu_logical_map(i)],
335 kstat.per_cpu_nice[cpu_logical_map(i)],
336 kstat.per_cpu_system[cpu_logical_map(i)],
337 jif - ( kstat.per_cpu_user[cpu_logical_map(i)] \
338 + kstat.per_cpu_nice[cpu_logical_map(i)] \
339 + kstat.per_cpu_system[cpu_logical_map(i)]));
340 proc_sprintf(page, &off, &len,
350 #if !defined(CONFIG_ARCH_S390) && !defined(CONFIG_ALPHA)
351 for (i = 0 ; i < NR_IRQS ; i++)
352 proc_sprintf(page, &off, &len,
353 " %u", kstat_irqs(i));
356 proc_sprintf(page, &off, &len, "\ndisk_io: ");
358 for (major = 0; major < DK_MAX_MAJOR; major++) {
359 for (disk = 0; disk < DK_MAX_DISK; disk++) {
360 int active = kstat.dk_drive[major][disk] +
361 kstat.dk_drive_rblk[major][disk] +
362 kstat.dk_drive_wblk[major][disk];
364 proc_sprintf(page, &off, &len,
365 "(%u,%u):(%u,%u,%u,%u,%u) ",
367 kstat.dk_drive[major][disk],
368 kstat.dk_drive_rio[major][disk],
369 kstat.dk_drive_rblk[major][disk],
370 kstat.dk_drive_wio[major][disk],
371 kstat.dk_drive_wblk[major][disk]
376 proc_sprintf(page, &off, &len,
381 xtime.tv_sec - jif / HZ,
384 return proc_calc_metrics(page, start, off, count, eof, len);
387 static int devices_read_proc(char *page, char **start, off_t off,
388 int count, int *eof, void *data)
390 int len = get_device_list(page);
391 return proc_calc_metrics(page, start, off, count, eof, len);
395 #if !defined(CONFIG_ARCH_S390)
396 static int interrupts_read_proc(char *page, char **start, off_t off,
397 int count, int *eof, void *data)
399 int len = get_irq_list(page);
400 return proc_calc_metrics(page, start, off, count, eof, len);
404 #else /* !CONFIG_X86 */
406 extern int show_interrupts(struct seq_file *p, void *v);
407 static int interrupts_open(struct inode *inode, struct file *file)
409 unsigned size = PAGE_SIZE * (1 + smp_num_cpus / 8);
410 char *buf = kmalloc(size, GFP_KERNEL);
416 res = single_open(file, show_interrupts, NULL);
418 m = file->private_data;
425 static struct file_operations proc_interrupts_operations = {
426 .open = interrupts_open,
429 .release = single_release,
431 #endif /* !CONFIG_X86 */
433 extern struct file_operations proc_ioports_operations;
434 extern struct file_operations proc_iomem_operations;
436 static int filesystems_read_proc(char *page, char **start, off_t off,
437 int count, int *eof, void *data)
439 int len = get_filesystem_list(page);
440 return proc_calc_metrics(page, start, off, count, eof, len);
443 static int dma_read_proc(char *page, char **start, off_t off,
444 int count, int *eof, void *data)
446 int len = get_dma_list(page);
447 return proc_calc_metrics(page, start, off, count, eof, len);
450 static int cmdline_read_proc(char *page, char **start, off_t off,
451 int count, int *eof, void *data)
453 extern char saved_command_line[];
456 proc_sprintf(page, &off, &len, "%s\n", saved_command_line);
457 return proc_calc_metrics(page, start, off, count, eof, len);
460 #ifdef CONFIG_SGI_DS1286
461 static int ds1286_read_proc(char *page, char **start, off_t off,
462 int count, int *eof, void *data)
464 int len = get_ds1286_status(page);
465 return proc_calc_metrics(page, start, off, count, eof, len);
469 static int locks_read_proc(char *page, char **start, off_t off,
470 int count, int *eof, void *data)
474 len = get_locks_status(page, start, off, count);
476 if (len < count) *eof = 1;
480 static int execdomains_read_proc(char *page, char **start, off_t off,
481 int count, int *eof, void *data)
483 int len = get_exec_domain_list(page);
484 return proc_calc_metrics(page, start, off, count, eof, len);
487 static int swaps_read_proc(char *page, char **start, off_t off,
488 int count, int *eof, void *data)
490 int len = get_swaparea_info(page);
491 return proc_calc_metrics(page, start, off, count, eof, len);
495 * This function accesses profiling information. The returned data is
496 * binary: the sampling step and the actual contents of the profile
497 * buffer. Use of the program readprofile is recommended in order to
498 * get meaningful info out of these data.
500 static ssize_t read_profile(struct file *file, char *buf,
501 size_t count, loff_t *ppos)
507 unsigned int sample_step = 1 << prof_shift;
509 if (p != n || p >= (prof_len+1)*sizeof(unsigned int))
511 if (count > (prof_len+1)*sizeof(unsigned int) - p)
512 count = (prof_len+1)*sizeof(unsigned int) - p;
515 while (p < sizeof(unsigned int) && count > 0) {
516 put_user(*((char *)(&sample_step)+p),buf);
517 buf++; p++; count--; read++;
519 pnt = (char *)prof_buffer + p - sizeof(unsigned int);
520 if (copy_to_user(buf,(void *)pnt,count))
528 * Writing to /proc/profile resets the counters
530 * Writing a 'profiling multiplier' value into it also re-sets the profiling
531 * interrupt frequency, on architectures that support this.
533 static ssize_t write_profile(struct file * file, const char * buf,
534 size_t count, loff_t *ppos)
537 extern int setup_profiling_timer (unsigned int multiplier);
539 if (count==sizeof(int)) {
540 unsigned int multiplier;
542 if (copy_from_user(&multiplier, buf, sizeof(int)))
545 if (setup_profiling_timer(multiplier))
550 memset(prof_buffer, 0, prof_len * sizeof(*prof_buffer));
554 static struct file_operations proc_profile_operations = {
556 write: write_profile,
559 #ifdef CONFIG_MAGIC_SYSRQ
561 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
563 static ssize_t write_sysrq_trigger(struct file *file, const char *buf,
564 size_t count, loff_t *ppos)
569 if (get_user(c, buf))
571 handle_sysrq(c, NULL, NULL, NULL);
576 static struct file_operations proc_sysrq_trigger_operations = {
577 .write = write_sysrq_trigger,
581 struct proc_dir_entry *proc_root_kcore;
583 static void create_seq_entry(char *name, mode_t mode, struct file_operations *f)
585 struct proc_dir_entry *entry;
586 entry = create_proc_entry(name, mode, NULL);
588 entry->proc_fops = f;
591 void __init proc_misc_init(void)
593 struct proc_dir_entry *entry;
596 int (*read_proc)(char*,char**,off_t,int,int*,void*);
597 } *p, simple_ones[] = {
598 {"loadavg", loadavg_read_proc},
599 {"uptime", uptime_read_proc},
600 {"meminfo", meminfo_read_proc},
601 {"version", version_read_proc},
602 #ifdef CONFIG_PROC_HARDWARE
603 {"hardware", hardware_read_proc},
605 #ifdef CONFIG_STRAM_PROC
606 {"stram", stram_read_proc},
608 #ifdef CONFIG_MODULES
609 {"modules", modules_read_proc},
611 {"stat", kstat_read_proc},
612 {"devices", devices_read_proc},
613 #if !defined(CONFIG_ARCH_S390) && !defined(CONFIG_X86)
614 {"interrupts", interrupts_read_proc},
616 {"filesystems", filesystems_read_proc},
617 {"dma", dma_read_proc},
618 {"cmdline", cmdline_read_proc},
619 #ifdef CONFIG_SGI_DS1286
620 {"rtc", ds1286_read_proc},
622 {"locks", locks_read_proc},
623 {"swaps", swaps_read_proc},
624 {"execdomains", execdomains_read_proc},
627 for (p = simple_ones; p->name; p++)
628 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
630 proc_symlink("mounts", NULL, "self/mounts");
632 /* And now for trickier ones */
633 entry = create_proc_entry("kmsg", S_IRUSR, &proc_root);
635 entry->proc_fops = &proc_kmsg_operations;
636 create_seq_entry("cpuinfo", 0, &proc_cpuinfo_operations);
637 #if defined(CONFIG_X86)
638 create_seq_entry("interrupts", 0, &proc_interrupts_operations);
640 create_seq_entry("ioports", 0, &proc_ioports_operations);
641 create_seq_entry("iomem", 0, &proc_iomem_operations);
642 create_seq_entry("partitions", 0, &proc_partitions_operations);
643 create_seq_entry("slabinfo",S_IWUSR|S_IRUGO,&proc_slabinfo_operations);
644 #ifdef CONFIG_MODULES
645 create_seq_entry("ksyms", 0, &proc_ksyms_operations);
647 proc_root_kcore = create_proc_entry("kcore", S_IRUSR, NULL);
648 if (proc_root_kcore) {
649 proc_root_kcore->proc_fops = &proc_kcore_operations;
650 proc_root_kcore->size =
651 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
654 entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL);
656 entry->proc_fops = &proc_profile_operations;
657 entry->size = (1+prof_len) * sizeof(unsigned int);
660 #ifdef CONFIG_MAGIC_SYSRQ
661 entry = create_proc_entry("sysrq-trigger", S_IWUSR, NULL);
663 entry->proc_fops = &proc_sysrq_trigger_operations;
667 extern struct file_operations ppc_htab_operations;
668 entry = create_proc_entry("ppc_htab", S_IRUGO|S_IWUSR, NULL);
670 entry->proc_fops = &ppc_htab_operations;