1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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>
29#include <linux/mm.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>
40
41#include <asm/uaccess.h>
42#include <asm/pgtable.h>
43#include <asm/io.h>
44
45#define LOAD_INT(x) ((x) >> FSHIFT)
46#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
47
48
49
50
51
52
53extern int get_hardware_list(char *);
54extern int get_stram_list(char *);
55#ifdef CONFIG_MODULES
56extern int get_module_list(char *);
57#endif
58extern int get_device_list(char *);
59extern int get_filesystem_list(char *);
60extern int get_exec_domain_list(char *);
61#ifndef CONFIG_X86
62extern int get_irq_list(char *);
63#endif
64extern int get_dma_list(char *);
65extern int get_locks_status (char *, char **, off_t, int);
66extern int get_swaparea_info (char *);
67#ifdef CONFIG_SGI_DS1286
68extern int get_ds1286_status(char *);
69#endif
70
71void proc_sprintf(char *page, off_t *off, int *lenp, const char *format, ...)
72{
73 int len = *lenp;
74 va_list args;
75
76
77 if (len > PAGE_SIZE-512)
78 return;
79
80 va_start(args, format);
81 len += vsnprintf(page + len, PAGE_SIZE-len, format, args);
82 va_end(args);
83
84 if (len <= *off) {
85 *off -= len;
86 len = 0;
87 }
88
89 *lenp = len;
90}
91
92static int proc_calc_metrics(char *page, char **start, off_t off,
93 int count, int *eof, int len)
94{
95 if (len <= off+count) *eof = 1;
96 *start = page + off;
97 len -= off;
98 if (len>count) len = count;
99 if (len<0) len = 0;
100 return len;
101}
102
103static int loadavg_read_proc(char *page, char **start, off_t off,
104 int count, int *eof, void *data)
105{
106 int a, b, c;
107 int len;
108
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);
118}
119
120static int uptime_read_proc(char *page, char **start, off_t off,
121 int count, int *eof, void *data)
122{
123 unsigned long uptime;
124 unsigned long idle;
125 int len;
126
127 uptime = jiffies;
128 idle = init_tasks[0]->times.tms_utime + init_tasks[0]->times.tms_stime;
129
130
131
132
133
134
135
136
137
138
139#if HZ!=100
140 len = sprintf(page,"%lu.%02lu %lu.%02lu\n",
141 uptime / HZ,
142 (((uptime % HZ) * 100) / HZ) % 100,
143 idle / HZ,
144 (((idle % HZ) * 100) / HZ) % 100);
145#else
146 len = sprintf(page,"%lu.%02lu %lu.%02lu\n",
147 uptime / HZ,
148 uptime % HZ,
149 idle / HZ,
150 idle % HZ);
151#endif
152 return proc_calc_metrics(page, start, off, count, eof, len);
153}
154
155static int meminfo_read_proc(char *page, char **start, off_t off,
156 int count, int *eof, void *data)
157{
158 struct sysinfo i;
159 int len;
160 int pg_size ;
161
162
163
164
165#define K(x) ((x) << (PAGE_SHIFT - 10))
166#define B(x) ((unsigned long long)(x) << PAGE_SHIFT)
167 si_meminfo(&i);
168 si_swapinfo(&i);
169 pg_size = page_cache_size - i.bufferram;
170
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));
178
179
180
181
182
183 len += sprintf(page+len,
184 "MemTotal: %8lu kB\n"
185 "MemFree: %8lu kB\n"
186 "MemShared: %8lu kB\n"
187 "Buffers: %8lu kB\n"
188 "Cached: %8lu kB\n"
189 "SwapCached: %8lu kB\n"
190 "Active: %8u kB\n"
191 "Inactive: %8u kB\n"
192 "HighTotal: %8lu kB\n"
193 "HighFree: %8lu kB\n"
194 "LowTotal: %8lu kB\n"
195 "LowFree: %8lu kB\n"
196 "SwapTotal: %8lu kB\n"
197 "SwapFree: %8lu kB\n",
198 K(i.totalram),
199 K(i.freeram),
200 K(i.sharedram),
201 K(i.bufferram),
202 K(pg_size - swapper_space.nrpages),
203 K(swapper_space.nrpages),
204 K(nr_active_pages),
205 K(nr_inactive_pages),
206 K(i.totalhigh),
207 K(i.freehigh),
208 K(i.totalram-i.totalhigh),
209 K(i.freeram-i.freehigh),
210 K(i.totalswap),
211 K(i.freeswap));
212
213 return proc_calc_metrics(page, start, off, count, eof, len);
214#undef B
215#undef K
216}
217
218static int version_read_proc(char *page, char **start, off_t off,
219 int count, int *eof, void *data)
220{
221 extern char *linux_banner;
222 int len;
223
224 strcpy(page, linux_banner);
225 len = strlen(page);
226 return proc_calc_metrics(page, start, off, count, eof, len);
227}
228
229extern struct seq_operations cpuinfo_op;
230static int cpuinfo_open(struct inode *inode, struct file *file)
231{
232 return seq_open(file, &cpuinfo_op);
233}
234static struct file_operations proc_cpuinfo_operations = {
235 open: cpuinfo_open,
236 read: seq_read,
237 llseek: seq_lseek,
238 release: seq_release,
239};
240
241#ifdef CONFIG_PROC_HARDWARE
242static int hardware_read_proc(char *page, char **start, off_t off,
243 int count, int *eof, void *data)
244{
245 int len = get_hardware_list(page);
246 return proc_calc_metrics(page, start, off, count, eof, len);
247}
248#endif
249
250#ifdef CONFIG_STRAM_PROC
251static int stram_read_proc(char *page, char **start, off_t off,
252 int count, int *eof, void *data)
253{
254 int len = get_stram_list(page);
255 return proc_calc_metrics(page, start, off, count, eof, len);
256}
257#endif
258
259extern struct seq_operations partitions_op;
260static int partitions_open(struct inode *inode, struct file *file)
261{
262 return seq_open(file, &partitions_op);
263}
264static struct file_operations proc_partitions_operations = {
265 open: partitions_open,
266 read: seq_read,
267 llseek: seq_lseek,
268 release: seq_release,
269};
270
271#ifdef CONFIG_MODULES
272static int modules_read_proc(char *page, char **start, off_t off,
273 int count, int *eof, void *data)
274{
275 int len = get_module_list(page);
276 return proc_calc_metrics(page, start, off, count, eof, len);
277}
278
279extern struct seq_operations ksyms_op;
280static int ksyms_open(struct inode *inode, struct file *file)
281{
282 return seq_open(file, &ksyms_op);
283}
284static struct file_operations proc_ksyms_operations = {
285 open: ksyms_open,
286 read: seq_read,
287 llseek: seq_lseek,
288 release: seq_release,
289};
290#endif
291
292extern struct seq_operations slabinfo_op;
293extern ssize_t slabinfo_write(struct file *, const char *, size_t, loff_t *);
294static int slabinfo_open(struct inode *inode, struct file *file)
295{
296 return seq_open(file, &slabinfo_op);
297}
298static struct file_operations proc_slabinfo_operations = {
299 open: slabinfo_open,
300 read: seq_read,
301 write: slabinfo_write,
302 llseek: seq_lseek,
303 release: seq_release,
304};
305
306static int kstat_read_proc(char *page, char **start, off_t off,
307 int count, int *eof, void *data)
308{
309 int i, len = 0;
310 extern unsigned long total_forks;
311 unsigned long jif = jiffies;
312 unsigned int sum = 0, user = 0, nice = 0, system = 0;
313 int major, disk;
314
315 for (i = 0 ; i < smp_num_cpus; i++) {
316 int cpu = cpu_logical_map(i), j;
317
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];
324#endif
325 }
326
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",
333 i,
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,
341 "page %u %u\n"
342 "swap %u %u\n"
343 "intr %u",
344 kstat.pgpgin >> 1,
345 kstat.pgpgout >> 1,
346 kstat.pswpin,
347 kstat.pswpout,
348 sum
349 );
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));
354#endif
355
356 proc_sprintf(page, &off, &len, "\ndisk_io: ");
357
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];
363 if (active)
364 proc_sprintf(page, &off, &len,
365 "(%u,%u):(%u,%u,%u,%u,%u) ",
366 major, disk,
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]
372 );
373 }
374 }
375
376 proc_sprintf(page, &off, &len,
377 "\nctxt %u\n"
378 "btime %lu\n"
379 "processes %lu\n",
380 kstat.context_swtch,
381 xtime.tv_sec - jif / HZ,
382 total_forks);
383
384 return proc_calc_metrics(page, start, off, count, eof, len);
385}
386
387static int devices_read_proc(char *page, char **start, off_t off,
388 int count, int *eof, void *data)
389{
390 int len = get_device_list(page);
391 return proc_calc_metrics(page, start, off, count, eof, len);
392}
393
394#ifndef CONFIG_X86
395#if !defined(CONFIG_ARCH_S390)
396static int interrupts_read_proc(char *page, char **start, off_t off,
397 int count, int *eof, void *data)
398{
399 int len = get_irq_list(page);
400 return proc_calc_metrics(page, start, off, count, eof, len);
401}
402#endif
403
404#else
405
406extern int show_interrupts(struct seq_file *p, void *v);
407static int interrupts_open(struct inode *inode, struct file *file)
408{
409 unsigned size = PAGE_SIZE * (1 + smp_num_cpus / 8);
410 char *buf = kmalloc(size, GFP_KERNEL);
411 struct seq_file *m;
412 int res;
413
414 if (!buf)
415 return -ENOMEM;
416 res = single_open(file, show_interrupts, NULL);
417 if (!res) {
418 m = file->private_data;
419 m->buf = buf;
420 m->size = size;
421 } else
422 kfree(buf);
423 return res;
424}
425static struct file_operations proc_interrupts_operations = {
426 .open = interrupts_open,
427 .read = seq_read,
428 .llseek = seq_lseek,
429 .release = single_release,
430};
431#endif
432
433extern struct file_operations proc_ioports_operations;
434extern struct file_operations proc_iomem_operations;
435
436static int filesystems_read_proc(char *page, char **start, off_t off,
437 int count, int *eof, void *data)
438{
439 int len = get_filesystem_list(page);
440 return proc_calc_metrics(page, start, off, count, eof, len);
441}
442
443static int dma_read_proc(char *page, char **start, off_t off,
444 int count, int *eof, void *data)
445{
446 int len = get_dma_list(page);
447 return proc_calc_metrics(page, start, off, count, eof, len);
448}
449
450static int cmdline_read_proc(char *page, char **start, off_t off,
451 int count, int *eof, void *data)
452{
453 extern char saved_command_line[];
454 int len = 0;
455
456 proc_sprintf(page, &off, &len, "%s\n", saved_command_line);
457 return proc_calc_metrics(page, start, off, count, eof, len);
458}
459
460#ifdef CONFIG_SGI_DS1286
461static int ds1286_read_proc(char *page, char **start, off_t off,
462 int count, int *eof, void *data)
463{
464 int len = get_ds1286_status(page);
465 return proc_calc_metrics(page, start, off, count, eof, len);
466}
467#endif
468
469static int locks_read_proc(char *page, char **start, off_t off,
470 int count, int *eof, void *data)
471{
472 int len;
473 lock_kernel();
474 len = get_locks_status(page, start, off, count);
475 unlock_kernel();
476 if (len < count) *eof = 1;
477 return len;
478}
479
480static int execdomains_read_proc(char *page, char **start, off_t off,
481 int count, int *eof, void *data)
482{
483 int len = get_exec_domain_list(page);
484 return proc_calc_metrics(page, start, off, count, eof, len);
485}
486
487static int swaps_read_proc(char *page, char **start, off_t off,
488 int count, int *eof, void *data)
489{
490 int len = get_swaparea_info(page);
491 return proc_calc_metrics(page, start, off, count, eof, len);
492}
493
494
495
496
497
498
499
500static ssize_t read_profile(struct file *file, char *buf,
501 size_t count, loff_t *ppos)
502{
503 loff_t n = *ppos;
504 unsigned p = n;
505 ssize_t read;
506 char * pnt;
507 unsigned int sample_step = 1 << prof_shift;
508
509 if (p != n || p >= (prof_len+1)*sizeof(unsigned int))
510 return 0;
511 if (count > (prof_len+1)*sizeof(unsigned int) - p)
512 count = (prof_len+1)*sizeof(unsigned int) - p;
513 read = 0;
514
515 while (p < sizeof(unsigned int) && count > 0) {
516 put_user(*((char *)(&sample_step)+p),buf);
517 buf++; p++; count--; read++;
518 }
519 pnt = (char *)prof_buffer + p - sizeof(unsigned int);
520 if (copy_to_user(buf,(void *)pnt,count))
521 return -EFAULT;
522 read += count;
523 *ppos = n + read;
524 return read;
525}
526
527
528
529
530
531
532
533static ssize_t write_profile(struct file * file, const char * buf,
534 size_t count, loff_t *ppos)
535{
536#ifdef CONFIG_SMP
537 extern int setup_profiling_timer (unsigned int multiplier);
538
539 if (count==sizeof(int)) {
540 unsigned int multiplier;
541
542 if (copy_from_user(&multiplier, buf, sizeof(int)))
543 return -EFAULT;
544
545 if (setup_profiling_timer(multiplier))
546 return -EINVAL;
547 }
548#endif
549
550 memset(prof_buffer, 0, prof_len * sizeof(*prof_buffer));
551 return count;
552}
553
554static struct file_operations proc_profile_operations = {
555 read: read_profile,
556 write: write_profile,
557};
558
559#ifdef CONFIG_MAGIC_SYSRQ
560
561
562
563static ssize_t write_sysrq_trigger(struct file *file, const char *buf,
564 size_t count, loff_t *ppos)
565{
566 if (count) {
567 char c;
568
569 if (get_user(c, buf))
570 return -EFAULT;
571 handle_sysrq(c, NULL, NULL, NULL);
572 }
573 return count;
574}
575
576static struct file_operations proc_sysrq_trigger_operations = {
577 .write = write_sysrq_trigger,
578};
579#endif
580
581struct proc_dir_entry *proc_root_kcore;
582
583static void create_seq_entry(char *name, mode_t mode, struct file_operations *f)
584{
585 struct proc_dir_entry *entry;
586 entry = create_proc_entry(name, mode, NULL);
587 if (entry)
588 entry->proc_fops = f;
589}
590
591void __init proc_misc_init(void)
592{
593 struct proc_dir_entry *entry;
594 static struct {
595 char *name;
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},
604#endif
605#ifdef CONFIG_STRAM_PROC
606 {"stram", stram_read_proc},
607#endif
608#ifdef CONFIG_MODULES
609 {"modules", modules_read_proc},
610#endif
611 {"stat", kstat_read_proc},
612 {"devices", devices_read_proc},
613#if !defined(CONFIG_ARCH_S390) && !defined(CONFIG_X86)
614 {"interrupts", interrupts_read_proc},
615#endif
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},
621#endif
622 {"locks", locks_read_proc},
623 {"swaps", swaps_read_proc},
624 {"execdomains", execdomains_read_proc},
625 {NULL,}
626 };
627 for (p = simple_ones; p->name; p++)
628 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
629
630 proc_symlink("mounts", NULL, "self/mounts");
631
632
633 entry = create_proc_entry("kmsg", S_IRUSR, &proc_root);
634 if (entry)
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);
639#endif
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);
646#endif
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;
652 }
653 if (prof_shift) {
654 entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL);
655 if (entry) {
656 entry->proc_fops = &proc_profile_operations;
657 entry->size = (1+prof_len) * sizeof(unsigned int);
658 }
659 }
660#ifdef CONFIG_MAGIC_SYSRQ
661 entry = create_proc_entry("sysrq-trigger", S_IWUSR, NULL);
662 if (entry)
663 entry->proc_fops = &proc_sysrq_trigger_operations;
664#endif
665#ifdef CONFIG_PPC32
666 {
667 extern struct file_operations ppc_htab_operations;
668 entry = create_proc_entry("ppc_htab", S_IRUGO|S_IWUSR, NULL);
669 if (entry)
670 entry->proc_fops = &ppc_htab_operations;
671 }
672#endif
673}
674