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>
40 #include <asm/uaccess.h>
41 #include <asm/pgtable.h>
44 #define LOAD_INT(x) ((x) >> FSHIFT)
45 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
47 * Warning: stuff below (imported functions) assumes that its output will fit
48 * into one page. For some of those functions it may be wrong. Moreover, we
49 * have a way to deal with that gracefully. Right now I used straightforward
50 * wrappers, but this needs further analysis wrt potential overflows.
52 extern int get_hardware_list(char *);
53 extern int get_stram_list(char *);
55 extern int get_module_list(char *);
57 extern int get_device_list(char *);
58 extern int get_filesystem_list(char *);
59 extern int get_exec_domain_list(char *);
60 extern int get_irq_list(char *);
61 extern int get_dma_list(char *);
62 extern int get_locks_status (char *, char **, off_t, int);
63 extern int get_swaparea_info (char *);
64 #ifdef CONFIG_SGI_DS1286
65 extern int get_ds1286_status(char *);
68 void proc_sprintf(char *page, off_t *off, int *lenp, const char *format, ...)
73 /* try to only print whole lines */
74 if (len > PAGE_SIZE-512)
77 va_start(args, format);
78 len += vsnprintf(page + len, PAGE_SIZE-len, format, args);
89 static int proc_calc_metrics(char *page, char **start, off_t off,
90 int count, int *eof, int len)
92 if (len <= off+count) *eof = 1;
95 if (len>count) len = count;
100 static int loadavg_read_proc(char *page, char **start, off_t off,
101 int count, int *eof, void *data)
106 a = avenrun[0] + (FIXED_1/200);
107 b = avenrun[1] + (FIXED_1/200);
108 c = avenrun[2] + (FIXED_1/200);
109 len = sprintf(page,"%d.%02d %d.%02d %d.%02d %d/%d %d\n",
110 LOAD_INT(a), LOAD_FRAC(a),
111 LOAD_INT(b), LOAD_FRAC(b),
112 LOAD_INT(c), LOAD_FRAC(c),
113 nr_running, nr_threads, last_pid);
114 return proc_calc_metrics(page, start, off, count, eof, len);
117 static int uptime_read_proc(char *page, char **start, off_t off,
118 int count, int *eof, void *data)
120 unsigned long uptime;
125 idle = init_tasks[0]->times.tms_utime + init_tasks[0]->times.tms_stime;
127 /* The formula for the fraction parts really is ((t * 100) / HZ) % 100, but
128 that would overflow about every five days at HZ == 100.
129 Therefore the identity a = (a / b) * b + a % b is used so that it is
130 calculated as (((t / HZ) * 100) + ((t % HZ) * 100) / HZ) % 100.
131 The part in front of the '+' always evaluates as 0 (mod 100). All divisions
132 in the above formulas are truncating. For HZ being a power of 10, the
133 calculations simplify to the version in the #else part (if the printf
134 format is adapted to the same number of digits as zeroes in HZ.
137 len = sprintf(page,"%lu.%02lu %lu.%02lu\n",
139 (((uptime % HZ) * 100) / HZ) % 100,
141 (((idle % HZ) * 100) / HZ) % 100);
143 len = sprintf(page,"%lu.%02lu %lu.%02lu\n",
149 return proc_calc_metrics(page, start, off, count, eof, len);
152 static int meminfo_read_proc(char *page, char **start, off_t off,
153 int count, int *eof, void *data)
160 * display in kilobytes.
162 #define K(x) ((x) << (PAGE_SHIFT - 10))
163 #define B(x) ((unsigned long long)(x) << PAGE_SHIFT)
166 pg_size = atomic_read(&page_cache_size) - i.bufferram ;
168 len = sprintf(page, " total: used: free: shared: buffers: cached:\n"
169 "Mem: %8Lu %8Lu %8Lu %8Lu %8Lu %8Lu\n"
170 "Swap: %8Lu %8Lu %8Lu\n",
171 B(i.totalram), B(i.totalram-i.freeram), B(i.freeram),
172 B(i.sharedram), B(i.bufferram),
173 B(pg_size), B(i.totalswap),
174 B(i.totalswap-i.freeswap), B(i.freeswap));
176 * Tagged format, for easy grepping and expansion.
177 * The above will go away eventually, once the tools
180 len += sprintf(page+len,
181 "MemTotal: %8lu kB\n"
183 "MemShared: %8lu kB\n"
186 "SwapCached: %8lu kB\n"
189 "HighTotal: %8lu kB\n"
190 "HighFree: %8lu kB\n"
191 "LowTotal: %8lu kB\n"
193 "SwapTotal: %8lu kB\n"
194 "SwapFree: %8lu kB\n",
199 K(pg_size - swapper_space.nrpages),
200 K(swapper_space.nrpages),
202 K(nr_inactive_pages),
205 K(i.totalram-i.totalhigh),
206 K(i.freeram-i.freehigh),
210 return proc_calc_metrics(page, start, off, count, eof, len);
215 static int qos_version_read_proc(char *page, char **start, off_t off,
216 int count, int *eof, void *data)
218 extern char *qos_banner;
221 strcpy(page, qos_banner);
223 return proc_calc_metrics(page, start, off, count, eof, len);
226 static int version_read_proc(char *page, char **start, off_t off,
227 int count, int *eof, void *data)
229 extern char *linux_banner;
232 strcpy(page, linux_banner);
234 return proc_calc_metrics(page, start, off, count, eof, len);
237 extern struct seq_operations cpuinfo_op;
238 static int cpuinfo_open(struct inode *inode, struct file *file)
240 return seq_open(file, &cpuinfo_op);
242 static struct file_operations proc_cpuinfo_operations = {
246 release: seq_release,
249 #ifdef CONFIG_PROC_HARDWARE
250 static int hardware_read_proc(char *page, char **start, off_t off,
251 int count, int *eof, void *data)
253 int len = get_hardware_list(page);
254 return proc_calc_metrics(page, start, off, count, eof, len);
258 #ifdef CONFIG_STRAM_PROC
259 static int stram_read_proc(char *page, char **start, off_t off,
260 int count, int *eof, void *data)
262 int len = get_stram_list(page);
263 return proc_calc_metrics(page, start, off, count, eof, len);
267 extern struct seq_operations partitions_op;
268 static int partitions_open(struct inode *inode, struct file *file)
270 return seq_open(file, &partitions_op);
272 static struct file_operations proc_partitions_operations = {
273 open: partitions_open,
276 release: seq_release,
279 #ifdef CONFIG_MODULES
280 static int modules_read_proc(char *page, char **start, off_t off,
281 int count, int *eof, void *data)
283 int len = get_module_list(page);
284 return proc_calc_metrics(page, start, off, count, eof, len);
287 extern struct seq_operations ksyms_op;
288 static int ksyms_open(struct inode *inode, struct file *file)
290 return seq_open(file, &ksyms_op);
292 static struct file_operations proc_ksyms_operations = {
296 release: seq_release,
300 extern struct seq_operations slabinfo_op;
301 extern ssize_t slabinfo_write(struct file *, const char *, size_t, loff_t *);
302 static int slabinfo_open(struct inode *inode, struct file *file)
304 return seq_open(file, &slabinfo_op);
306 static struct file_operations proc_slabinfo_operations = {
309 write: slabinfo_write,
311 release: seq_release,
314 static int kstat_read_proc(char *page, char **start, off_t off,
315 int count, int *eof, void *data)
318 extern unsigned long total_forks;
319 unsigned long jif = jiffies;
320 unsigned int sum = 0, user = 0, nice = 0, system = 0;
323 for (i = 0 ; i < smp_num_cpus; i++) {
324 int cpu = cpu_logical_map(i), j;
326 user += kstat.per_cpu_user[cpu];
327 nice += kstat.per_cpu_nice[cpu];
328 system += kstat.per_cpu_system[cpu];
329 #if !defined(CONFIG_ARCH_S390)
330 for (j = 0 ; j < NR_IRQS ; j++)
331 sum += kstat.irqs[cpu][j];
335 proc_sprintf(page, &off, &len,
336 "cpu %u %u %u %lu\n", user, nice, system,
337 jif * smp_num_cpus - (user + nice + system));
338 for (i = 0 ; i < smp_num_cpus; i++)
339 proc_sprintf(page, &off, &len,
340 "cpu%d %u %u %u %lu\n",
342 kstat.per_cpu_user[cpu_logical_map(i)],
343 kstat.per_cpu_nice[cpu_logical_map(i)],
344 kstat.per_cpu_system[cpu_logical_map(i)],
345 jif - ( kstat.per_cpu_user[cpu_logical_map(i)] \
346 + kstat.per_cpu_nice[cpu_logical_map(i)] \
347 + kstat.per_cpu_system[cpu_logical_map(i)]));
348 proc_sprintf(page, &off, &len,
358 #if !defined(CONFIG_ARCH_S390) && !defined(CONFIG_ALPHA)
359 for (i = 0 ; i < NR_IRQS ; i++)
360 proc_sprintf(page, &off, &len,
361 " %u", kstat_irqs(i));
364 proc_sprintf(page, &off, &len, "\ndisk_io: ");
366 for (major = 0; major < DK_MAX_MAJOR; major++) {
367 for (disk = 0; disk < DK_MAX_DISK; disk++) {
368 int active = kstat.dk_drive[major][disk] +
369 kstat.dk_drive_rblk[major][disk] +
370 kstat.dk_drive_wblk[major][disk];
372 proc_sprintf(page, &off, &len,
373 "(%u,%u):(%u,%u,%u,%u,%u) ",
375 kstat.dk_drive[major][disk],
376 kstat.dk_drive_rio[major][disk],
377 kstat.dk_drive_rblk[major][disk],
378 kstat.dk_drive_wio[major][disk],
379 kstat.dk_drive_wblk[major][disk]
384 proc_sprintf(page, &off, &len,
389 xtime.tv_sec - jif / HZ,
392 return proc_calc_metrics(page, start, off, count, eof, len);
395 static int devices_read_proc(char *page, char **start, off_t off,
396 int count, int *eof, void *data)
398 int len = get_device_list(page);
399 return proc_calc_metrics(page, start, off, count, eof, len);
402 #if !defined(CONFIG_ARCH_S390)
403 static int interrupts_read_proc(char *page, char **start, off_t off,
404 int count, int *eof, void *data)
406 int len = get_irq_list(page);
407 return proc_calc_metrics(page, start, off, count, eof, len);
411 static int filesystems_read_proc(char *page, char **start, off_t off,
412 int count, int *eof, void *data)
414 int len = get_filesystem_list(page);
415 return proc_calc_metrics(page, start, off, count, eof, len);
418 static int dma_read_proc(char *page, char **start, off_t off,
419 int count, int *eof, void *data)
421 int len = get_dma_list(page);
422 return proc_calc_metrics(page, start, off, count, eof, len);
425 static int ioports_read_proc(char *page, char **start, off_t off,
426 int count, int *eof, void *data)
428 int len = get_ioport_list(page);
429 return proc_calc_metrics(page, start, off, count, eof, len);
432 static int cmdline_read_proc(char *page, char **start, off_t off,
433 int count, int *eof, void *data)
435 extern char saved_command_line[];
438 len = snprintf(page, count, "%s\n", saved_command_line);
439 return proc_calc_metrics(page, start, off, count, eof, len);
442 #ifdef CONFIG_SGI_DS1286
443 static int ds1286_read_proc(char *page, char **start, off_t off,
444 int count, int *eof, void *data)
446 int len = get_ds1286_status(page);
447 return proc_calc_metrics(page, start, off, count, eof, len);
451 static int locks_read_proc(char *page, char **start, off_t off,
452 int count, int *eof, void *data)
456 len = get_locks_status(page, start, off, count);
458 if (len < count) *eof = 1;
462 static int execdomains_read_proc(char *page, char **start, off_t off,
463 int count, int *eof, void *data)
465 int len = get_exec_domain_list(page);
466 return proc_calc_metrics(page, start, off, count, eof, len);
469 static int swaps_read_proc(char *page, char **start, off_t off,
470 int count, int *eof, void *data)
472 int len = get_swaparea_info(page);
473 return proc_calc_metrics(page, start, off, count, eof, len);
476 static int memory_read_proc(char *page, char **start, off_t off,
477 int count, int *eof, void *data)
479 int len = get_mem_list(page);
480 return proc_calc_metrics(page, start, off, count, eof, len);
484 * This function accesses profiling information. The returned data is
485 * binary: the sampling step and the actual contents of the profile
486 * buffer. Use of the program readprofile is recommended in order to
487 * get meaningful info out of these data.
489 static ssize_t read_profile(struct file *file, char *buf,
490 size_t count, loff_t *ppos)
492 unsigned long p = *ppos;
495 unsigned int sample_step = 1 << prof_shift;
497 if (p >= (prof_len+1)*sizeof(unsigned int))
499 if (count > (prof_len+1)*sizeof(unsigned int) - p)
500 count = (prof_len+1)*sizeof(unsigned int) - p;
503 while (p < sizeof(unsigned int) && count > 0) {
504 put_user(*((char *)(&sample_step)+p),buf);
505 buf++; p++; count--; read++;
507 pnt = (char *)prof_buffer + p - sizeof(unsigned int);
508 copy_to_user(buf,(void *)pnt,count);
515 * Writing to /proc/profile resets the counters
517 * Writing a 'profiling multiplier' value into it also re-sets the profiling
518 * interrupt frequency, on architectures that support this.
520 static ssize_t write_profile(struct file * file, const char * buf,
521 size_t count, loff_t *ppos)
524 extern int setup_profiling_timer (unsigned int multiplier);
526 if (count==sizeof(int)) {
527 unsigned int multiplier;
529 if (copy_from_user(&multiplier, buf, sizeof(int)))
532 if (setup_profiling_timer(multiplier))
537 memset(prof_buffer, 0, prof_len * sizeof(*prof_buffer));
541 static struct file_operations proc_profile_operations = {
543 write: write_profile,
546 struct proc_dir_entry *proc_root_kcore;
548 static void create_seq_entry(char *name, mode_t mode, struct file_operations *f)
550 struct proc_dir_entry *entry;
551 entry = create_proc_entry(name, mode, NULL);
553 entry->proc_fops = f;
556 void __init proc_misc_init(void)
558 struct proc_dir_entry *entry;
561 int (*read_proc)(char*,char**,off_t,int,int*,void*);
562 } *p, simple_ones[] = {
563 {"loadavg", loadavg_read_proc},
564 {"uptime", uptime_read_proc},
565 {"meminfo", meminfo_read_proc},
566 {"version", version_read_proc},
567 {"qosversion", qos_version_read_proc},
568 #ifdef CONFIG_PROC_HARDWARE
569 {"hardware", hardware_read_proc},
571 #ifdef CONFIG_STRAM_PROC
572 {"stram", stram_read_proc},
574 #ifdef CONFIG_MODULES
575 {"modules", modules_read_proc},
577 {"stat", kstat_read_proc},
578 {"devices", devices_read_proc},
579 #if !defined(CONFIG_ARCH_S390)
580 {"interrupts", interrupts_read_proc},
582 {"filesystems", filesystems_read_proc},
583 {"dma", dma_read_proc},
584 {"ioports", ioports_read_proc},
585 {"cmdline", cmdline_read_proc},
586 #ifdef CONFIG_SGI_DS1286
587 {"rtc", ds1286_read_proc},
589 {"locks", locks_read_proc},
590 {"swaps", swaps_read_proc},
591 {"iomem", memory_read_proc},
592 {"execdomains", execdomains_read_proc},
595 for (p = simple_ones; p->name; p++)
596 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
598 proc_symlink("mounts", NULL, "self/mounts");
600 /* And now for trickier ones */
601 entry = create_proc_entry("kmsg", S_IRUSR, &proc_root);
603 entry->proc_fops = &proc_kmsg_operations;
604 create_seq_entry("cpuinfo", 0, &proc_cpuinfo_operations);
605 create_seq_entry("partitions", 0, &proc_partitions_operations);
606 create_seq_entry("slabinfo",S_IWUSR|S_IRUGO,&proc_slabinfo_operations);
607 #ifdef CONFIG_MODULES
608 create_seq_entry("ksyms", 0, &proc_ksyms_operations);
610 proc_root_kcore = create_proc_entry("kcore", S_IRUSR, NULL);
611 if (proc_root_kcore) {
612 proc_root_kcore->proc_fops = &proc_kcore_operations;
613 proc_root_kcore->size =
614 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
617 entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL);
619 entry->proc_fops = &proc_profile_operations;
620 entry->size = (1+prof_len) * sizeof(unsigned int);
625 extern struct file_operations ppc_htab_operations;
626 entry = create_proc_entry("ppc_htab", S_IRUGO|S_IWUSR, NULL);
628 entry->proc_fops = &ppc_htab_operations;