This source file includes following definitions.
- read_core
- read_profile
- write_profile
- get_loadavg
- get_kstat
- get_uptime
- get_meminfo
- get_version
- get_cpuinfo
- get_task
- get_phys_addr
- get_array
- get_env
- get_arg
- get_wchan
- get_stat
- get_statm
- get_maps
- get_root_array
- get_process_array
- fill_array
- array_read
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 #include <linux/types.h>
28 #include <linux/errno.h>
29 #include <linux/sched.h>
30 #include <linux/kernel.h>
31 #include <linux/kernel_stat.h>
32 #include <linux/tty.h>
33 #include <linux/user.h>
34 #include <linux/a.out.h>
35 #include <linux/string.h>
36 #include <linux/mman.h>
37 #include <linux/proc_fs.h>
38 #include <linux/ioport.h>
39 #include <linux/config.h>
40 #include <linux/delay.h>
41
42 #include <asm/segment.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 #ifdef CONFIG_DEBUG_MALLOC
49 int get_malloc(char * buffer);
50 #endif
51
52 static int read_core(struct inode * inode, struct file * file,char * buf, int count)
53 {
54 unsigned long p = file->f_pos;
55 int read;
56 int count1;
57 char * pnt;
58 struct user dump;
59
60 memset(&dump, 0, sizeof(struct user));
61 dump.magic = CMAGIC;
62 dump.u_dsize = high_memory >> 12;
63
64 if (count < 0)
65 return -EINVAL;
66 if (p >= high_memory + PAGE_SIZE)
67 return 0;
68 if (count > high_memory + PAGE_SIZE - p)
69 count = high_memory + PAGE_SIZE - p;
70 read = 0;
71
72 if (p < sizeof(struct user) && count > 0) {
73 count1 = count;
74 if (p + count1 > sizeof(struct user))
75 count1 = sizeof(struct user)-p;
76 pnt = (char *) &dump + p;
77 memcpy_tofs(buf,(void *) pnt, count1);
78 buf += count1;
79 p += count1;
80 count -= count1;
81 read += count1;
82 }
83
84 while (p < 2*PAGE_SIZE && count > 0) {
85 put_fs_byte(0,buf);
86 buf++;
87 p++;
88 count--;
89 read++;
90 }
91 memcpy_tofs(buf,(void *) (p - PAGE_SIZE),count);
92 read += count;
93 file->f_pos += read;
94 return read;
95 }
96
97 static struct file_operations proc_kcore_operations = {
98 NULL,
99 read_core,
100 };
101
102 struct inode_operations proc_kcore_inode_operations = {
103 &proc_kcore_operations,
104 };
105
106 #ifdef CONFIG_PROFILE
107
108 extern unsigned long prof_len;
109 extern unsigned long * prof_buffer;
110
111
112
113
114
115
116 static int read_profile(struct inode *inode, struct file *file, char *buf, int count)
117 {
118 unsigned long p = file->f_pos;
119 int read;
120 char * pnt;
121 unsigned long sample_step = 1 << CONFIG_PROFILE_SHIFT;
122
123 if (count < 0)
124 return -EINVAL;
125 if (p >= (prof_len+1)*sizeof(unsigned long))
126 return 0;
127 if (count > (prof_len+1)*sizeof(unsigned long) - p)
128 count = (prof_len+1)*sizeof(unsigned long) - p;
129 read = 0;
130
131 while (p < sizeof(unsigned long) && count > 0) {
132 put_fs_byte(*((char *)(&sample_step)+p),buf);
133 buf++; p++; count--; read++;
134 }
135 pnt = (char *)prof_buffer + p - sizeof(unsigned long);
136 memcpy_tofs(buf,(void *)pnt,count);
137 read += count;
138 file->f_pos += read;
139 return read;
140 }
141
142
143 static int write_profile(struct inode * inode, struct file * file, char * buf, int count)
144 {
145 int i=prof_len;
146
147 while (i--)
148 prof_buffer[i]=0UL;
149 return count;
150 }
151
152 static struct file_operations proc_profile_operations = {
153 NULL,
154 read_profile,
155 write_profile,
156 };
157
158 struct inode_operations proc_profile_inode_operations = {
159 &proc_profile_operations,
160 };
161
162 #endif
163
164 static int get_loadavg(char * buffer)
165 {
166 int a, b, c;
167
168 a = avenrun[0] + (FIXED_1/200);
169 b = avenrun[1] + (FIXED_1/200);
170 c = avenrun[2] + (FIXED_1/200);
171 return sprintf(buffer,"%d.%02d %d.%02d %d.%02d\n",
172 LOAD_INT(a), LOAD_FRAC(a),
173 LOAD_INT(b), LOAD_FRAC(b),
174 LOAD_INT(c), LOAD_FRAC(c));
175 }
176
177 static int get_kstat(char * buffer)
178 {
179 int i, len;
180 unsigned sum = 0;
181
182 for (i = 0 ; i < 16 ; i++)
183 sum += kstat.interrupts[i];
184 len = sprintf(buffer,
185 "cpu %u %u %u %lu\n"
186 "disk %u %u %u %u\n"
187 "page %u %u\n"
188 "swap %u %u\n"
189 "intr %u",
190 kstat.cpu_user,
191 kstat.cpu_nice,
192 kstat.cpu_system,
193 jiffies - (kstat.cpu_user + kstat.cpu_nice + kstat.cpu_system),
194 kstat.dk_drive[0],
195 kstat.dk_drive[1],
196 kstat.dk_drive[2],
197 kstat.dk_drive[3],
198 kstat.pgpgin,
199 kstat.pgpgout,
200 kstat.pswpin,
201 kstat.pswpout,
202 sum);
203 for (i = 0 ; i < 16 ; i++)
204 len += sprintf(buffer + len, " %u", kstat.interrupts[i]);
205 len += sprintf(buffer + len,
206 "\nctxt %u\n"
207 "btime %lu\n",
208 kstat.context_swtch,
209 xtime.tv_sec - jiffies / HZ);
210 return len;
211 }
212
213
214 static int get_uptime(char * buffer)
215 {
216 unsigned long uptime;
217 unsigned long idle;
218
219 uptime = jiffies;
220 idle = task[0]->utime + task[0]->stime;
221
222
223
224
225
226
227
228
229
230
231 #if HZ!=100
232 return sprintf(buffer,"%lu.%02lu %lu.%02lu\n",
233 uptime / HZ,
234 (((uptime % HZ) * 100) / HZ) % 100,
235 idle / HZ,
236 (((idle % HZ) * 100) / HZ) % 100);
237 #else
238 return sprintf(buffer,"%lu.%02lu %lu.%02lu\n",
239 uptime / HZ,
240 uptime % HZ,
241 idle / HZ,
242 idle % HZ);
243 #endif
244 }
245
246 static int get_meminfo(char * buffer)
247 {
248 struct sysinfo i;
249
250 si_meminfo(&i);
251 si_swapinfo(&i);
252 return sprintf(buffer, " total: used: free: shared: buffers:\n"
253 "Mem: %8lu %8lu %8lu %8lu %8lu\n"
254 "Swap: %8lu %8lu %8lu\n",
255 i.totalram, i.totalram-i.freeram, i.freeram, i.sharedram, i.bufferram,
256 i.totalswap, i.totalswap-i.freeswap, i.freeswap);
257 }
258
259 static int get_version(char * buffer)
260 {
261 extern char *linux_banner;
262
263 strcpy(buffer, linux_banner);
264 return strlen(buffer);
265 }
266
267 static int get_cpuinfo(char * buffer)
268 {
269 #ifdef __i386__
270 char *model[2][9]={{"DX","SX","DX/2","4","SX/2","6",
271 "7","DX/4"},
272 {"Pentium 60/66","Pentium 90/100","3",
273 "4","5","6","7","8"}};
274 char mask[2];
275 mask[0] = x86_mask+'@';
276 mask[1] = '\0';
277 return sprintf(buffer,"cpu\t\t: %c86\n"
278 "model\t\t: %s\n"
279 "mask\t\t: %s\n"
280 "vid\t\t: %s\n"
281 "fdiv_bug\t: %s\n"
282 "math\t\t: %s\n"
283 "hlt\t\t: %s\n"
284 "wp\t\t: %s\n"
285 "Integrated NPU\t: %s\n"
286 "Enhanced VM86\t: %s\n"
287 "IO Breakpoints\t: %s\n"
288 "4MB Pages\t: %s\n"
289 "TS Counters\t: %s\n"
290 "Pentium MSR\t: %s\n"
291 "Mach. Ch. Exep.\t: %s\n"
292 "CMPXCHGB8B\t: %s\n"
293 "BogoMips\t: %lu.%02lu\n",
294 x86+'0',
295 x86_model ? model[x86-4][x86_model-1] : "Unknown",
296 x86_mask ? mask : "Unknown",
297 x86_vendor_id,
298 fdiv_bug ? "yes" : "no",
299 hard_math ? "yes" : "no",
300 hlt_works_ok ? "yes" : "no",
301 wp_works_ok ? "yes" : "no",
302 x86_capability & 1 ? "yes" : "no",
303 x86_capability & 2 ? "yes" : "no",
304 x86_capability & 4 ? "yes" : "no",
305 x86_capability & 8 ? "yes" : "no",
306 x86_capability & 16 ? "yes" : "no",
307 x86_capability & 32 ? "yes" : "no",
308 x86_capability & 128 ? "yes" : "no",
309 x86_capability & 256 ? "yes" : "no",
310 loops_per_sec/500000, (loops_per_sec/5000) % 100
311 );
312 #else
313 return 0;
314 #endif
315 }
316
317 static struct task_struct ** get_task(pid_t pid)
318 {
319 struct task_struct ** p;
320
321 p = task;
322 while (++p < task+NR_TASKS) {
323 if (*p && (*p)->pid == pid)
324 return p;
325 }
326 return NULL;
327 }
328
329 static unsigned long get_phys_addr(struct task_struct ** p, unsigned long ptr)
330 {
331 unsigned long page;
332
333 if (!p || !*p || ptr >= TASK_SIZE)
334 return 0;
335 page = *PAGE_DIR_OFFSET(*p,ptr);
336 if (!(page & PAGE_PRESENT))
337 return 0;
338 page &= PAGE_MASK;
339 page += PAGE_PTR(ptr);
340 page = *(unsigned long *) page;
341 if (!(page & PAGE_PRESENT))
342 return 0;
343 page &= PAGE_MASK;
344 page += ptr & ~PAGE_MASK;
345 return page;
346 }
347
348 static int get_array(struct task_struct ** p, unsigned long start, unsigned long end, char * buffer)
349 {
350 unsigned long addr;
351 int size = 0, result = 0;
352 char c;
353
354 if (start >= end)
355 return result;
356 for (;;) {
357 addr = get_phys_addr(p, start);
358 if (!addr)
359 goto ready;
360 do {
361 c = *(char *) addr;
362 if (!c)
363 result = size;
364 if (size < PAGE_SIZE)
365 buffer[size++] = c;
366 else
367 goto ready;
368 addr++;
369 start++;
370 if (!c && start >= end)
371 goto ready;
372 } while (addr & ~PAGE_MASK);
373 }
374 ready:
375
376 while (result>0 && buffer[result-1]==' ')
377 result--;
378 return result;
379 }
380
381 static int get_env(int pid, char * buffer)
382 {
383 struct task_struct ** p = get_task(pid);
384
385 if (!p || !*p)
386 return 0;
387 return get_array(p, (*p)->mm->env_start, (*p)->mm->env_end, buffer);
388 }
389
390 static int get_arg(int pid, char * buffer)
391 {
392 struct task_struct ** p = get_task(pid);
393
394 if (!p || !*p)
395 return 0;
396 return get_array(p, (*p)->mm->arg_start, (*p)->mm->arg_end, buffer);
397 }
398
399 static unsigned long get_wchan(struct task_struct *p)
400 {
401 #ifdef __i386__
402 unsigned long ebp, eip;
403 unsigned long stack_page;
404 int count = 0;
405
406 if (!p || p == current || p->state == TASK_RUNNING)
407 return 0;
408 stack_page = p->kernel_stack_page;
409 if (!stack_page)
410 return 0;
411 ebp = p->tss.ebp;
412 do {
413 if (ebp < stack_page || ebp >= 4092+stack_page)
414 return 0;
415 eip = *(unsigned long *) (ebp+4);
416 if ((void *)eip != sleep_on &&
417 (void *)eip != interruptible_sleep_on)
418 return eip;
419 ebp = *(unsigned long *) ebp;
420 } while (count++ < 16);
421 #endif
422 return 0;
423 }
424
425 #define KSTK_EIP(stack) (((unsigned long *)stack)[1019])
426 #define KSTK_ESP(stack) (((unsigned long *)stack)[1022])
427
428 static int get_stat(int pid, char * buffer)
429 {
430 struct task_struct ** p = get_task(pid);
431 unsigned long sigignore=0, sigcatch=0, bit=1, wchan;
432 unsigned long vsize, eip, esp;
433 int i,tty_pgrp;
434 char state;
435
436 if (!p || !*p)
437 return 0;
438 if ((*p)->state < 0 || (*p)->state > 5)
439 state = '.';
440 else
441 state = "RSDZTD"[(*p)->state];
442 eip = esp = 0;
443 vsize = (*p)->kernel_stack_page;
444 if (vsize) {
445 eip = KSTK_EIP(vsize);
446 esp = KSTK_ESP(vsize);
447 vsize = (*p)->mm->brk - (*p)->mm->start_code + PAGE_SIZE-1;
448 if (esp)
449 vsize += TASK_SIZE - esp;
450 }
451 wchan = get_wchan(*p);
452 for(i=0; i<32; ++i) {
453 switch((unsigned long) (*p)->sigaction[i].sa_handler) {
454 case 1: sigignore |= bit; break;
455 case 0: break;
456 default: sigcatch |= bit;
457 } bit <<= 1;
458 }
459 if ((*p)->tty)
460 tty_pgrp = (*p)->tty->pgrp;
461 else
462 tty_pgrp = -1;
463 return sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
464 %lu %lu %lu %ld %ld %ld %ld %ld %ld %lu %lu %ld %lu %lu %lu %lu %lu %lu %lu %lu %lu \
465 %lu %lu %lu %lu\n",
466 pid,
467 (*p)->comm,
468 state,
469 (*p)->p_pptr->pid,
470 (*p)->pgrp,
471 (*p)->session,
472 (*p)->tty ? (*p)->tty->device : 0,
473 tty_pgrp,
474 (*p)->flags,
475 (*p)->mm->min_flt,
476 (*p)->mm->cmin_flt,
477 (*p)->mm->maj_flt,
478 (*p)->mm->cmaj_flt,
479 (*p)->utime,
480 (*p)->stime,
481 (*p)->cutime,
482 (*p)->cstime,
483 (*p)->counter,
484
485 (*p)->priority,
486
487 (*p)->timeout,
488 (*p)->it_real_value,
489 (*p)->start_time,
490 vsize,
491 (*p)->mm->rss,
492 (*p)->rlim[RLIMIT_RSS].rlim_cur,
493 (*p)->mm->start_code,
494 (*p)->mm->end_code,
495 (*p)->mm->start_stack,
496 esp,
497 eip,
498 (*p)->signal,
499 (*p)->blocked,
500 sigignore,
501 sigcatch,
502 wchan);
503 }
504
505 static int get_statm(int pid, char * buffer)
506 {
507 struct task_struct ** p = get_task(pid);
508 int i, tpag;
509 int size=0, resident=0, share=0, trs=0, lrs=0, drs=0, dt=0;
510 unsigned long ptbl, *buf, *pte, *pagedir, map_nr;
511
512 if (!p || !*p)
513 return 0;
514 tpag = (*p)->mm->end_code / PAGE_SIZE;
515 if ((*p)->state != TASK_ZOMBIE) {
516 pagedir = PAGE_DIR_OFFSET(*p, 0);
517 for (i = 0; i < 0x300; ++i) {
518 if ((ptbl = pagedir[i]) == 0) {
519 tpag -= PTRS_PER_PAGE;
520 continue;
521 }
522 buf = (unsigned long *)(ptbl & PAGE_MASK);
523 for (pte = buf; pte < (buf + PTRS_PER_PAGE); ++pte) {
524 if (*pte != 0) {
525 ++size;
526 if (*pte & 1) {
527 ++resident;
528 if (tpag > 0)
529 ++trs;
530 else
531 ++drs;
532 if (i >= 15 && i < 0x2f0) {
533 ++lrs;
534 if (*pte & 0x40)
535 ++dt;
536 else
537 --drs;
538 }
539 map_nr = MAP_NR(*pte);
540 if (map_nr < (high_memory / PAGE_SIZE) && mem_map[map_nr] > 1)
541 ++share;
542 }
543 }
544 --tpag;
545 }
546 }
547 }
548 return sprintf(buffer,"%d %d %d %d %d %d %d\n",
549 size, resident, share, trs, lrs, drs, dt);
550 }
551
552 static int get_maps(int pid, char *buf)
553 {
554 int sz = 0;
555 struct task_struct **p = get_task(pid);
556 struct vm_area_struct *map;
557
558 if (!p || !*p)
559 return 0;
560
561 for(map = (*p)->mm->mmap; map != NULL; map = map->vm_next) {
562 char str[7], *cp = str;
563 int flags;
564 int end = sz + 80;
565 dev_t dev;
566 unsigned long ino;
567
568 flags = map->vm_flags;
569
570 *cp++ = flags & VM_READ ? 'r' : '-';
571 *cp++ = flags & VM_WRITE ? 'w' : '-';
572 *cp++ = flags & VM_EXEC ? 'x' : '-';
573 *cp++ = flags & VM_SHARED ? 's' : 'p';
574 *cp++ = 0;
575
576 if (end >= PAGE_SIZE) {
577 sprintf(buf+sz, "...\n");
578 break;
579 }
580
581 if (map->vm_inode != NULL) {
582 dev = map->vm_inode->i_dev;
583 ino = map->vm_inode->i_ino;
584 } else {
585 dev = 0;
586 ino = 0;
587 }
588
589 sz += sprintf(buf+sz, "%08lx-%08lx %s %08lx %02x:%02x %lu\n",
590 map->vm_start, map->vm_end, str, map->vm_offset,
591 MAJOR(dev),MINOR(dev), ino);
592 if (sz > end) {
593 printk("get_maps: end(%d) < sz(%d)\n", end, sz);
594 break;
595 }
596 }
597
598 return sz;
599 }
600
601 extern int get_module_list(char *);
602 extern int get_device_list(char *);
603 extern int get_filesystem_list(char *);
604 extern int get_ksyms_list(char *);
605 extern int get_irq_list(char *);
606 extern int get_dma_list(char *);
607 extern int get_cpuinfo(char *);
608 extern int get_pci_list(char*);
609
610 static int get_root_array(char * page, int type)
611 {
612 switch (type) {
613 case PROC_LOADAVG:
614 return get_loadavg(page);
615
616 case PROC_UPTIME:
617 return get_uptime(page);
618
619 case PROC_MEMINFO:
620 return get_meminfo(page);
621
622 #ifdef CONFIG_PCI
623 case PROC_PCI:
624 return get_pci_list(page);
625 #endif
626
627 case PROC_CPUINFO:
628 return get_cpuinfo(page);
629
630 case PROC_VERSION:
631 return get_version(page);
632
633 #ifdef CONFIG_DEBUG_MALLOC
634 case PROC_MALLOC:
635 return get_malloc(page);
636 #endif
637
638 case PROC_MODULES:
639 return get_module_list(page);
640
641 case PROC_STAT:
642 return get_kstat(page);
643
644 case PROC_DEVICES:
645 return get_device_list(page);
646
647 case PROC_INTERRUPTS:
648 return get_irq_list(page);
649
650 case PROC_FILESYSTEMS:
651 return get_filesystem_list(page);
652
653 case PROC_KSYMS:
654 return get_ksyms_list(page);
655
656 case PROC_DMA:
657 return get_dma_list(page);
658
659 case PROC_IOPORTS:
660 return get_ioport_list(page);
661 }
662 return -EBADF;
663 }
664
665 static int get_process_array(char * page, int pid, int type)
666 {
667 switch (type) {
668 case PROC_PID_ENVIRON:
669 return get_env(pid, page);
670 case PROC_PID_CMDLINE:
671 return get_arg(pid, page);
672 case PROC_PID_STAT:
673 return get_stat(pid, page);
674 case PROC_PID_STATM:
675 return get_statm(pid, page);
676 case PROC_PID_MAPS:
677 return get_maps(pid, page);
678 }
679 return -EBADF;
680 }
681
682
683 static inline int fill_array(char * page, int pid, int type)
684 {
685 if (pid)
686 return get_process_array(page, pid, type);
687 return get_root_array(page, type);
688 }
689
690 static int array_read(struct inode * inode, struct file * file,char * buf, int count)
691 {
692 unsigned long page;
693 int length;
694 int end;
695 unsigned int type, pid;
696
697 if (count < 0)
698 return -EINVAL;
699 if (!(page = __get_free_page(GFP_KERNEL)))
700 return -ENOMEM;
701 type = inode->i_ino;
702 pid = type >> 16;
703 type &= 0x0000ffff;
704 length = fill_array((char *) page, pid, type);
705 if (length < 0) {
706 free_page(page);
707 return length;
708 }
709 if (file->f_pos >= length) {
710 free_page(page);
711 return 0;
712 }
713 if (count + file->f_pos > length)
714 count = length - file->f_pos;
715 end = count + file->f_pos;
716 memcpy_tofs(buf, (char *) page + file->f_pos, count);
717 free_page(page);
718 file->f_pos = end;
719 return count;
720 }
721
722 static struct file_operations proc_array_operations = {
723 NULL,
724 array_read,
725 NULL,
726 NULL,
727 NULL,
728 NULL,
729 NULL,
730 NULL,
731 NULL,
732 NULL
733 };
734
735 struct inode_operations proc_array_inode_operations = {
736 &proc_array_operations,
737 NULL,
738 NULL,
739 NULL,
740 NULL,
741 NULL,
742 NULL,
743 NULL,
744 NULL,
745 NULL,
746 NULL,
747 NULL,
748 NULL,
749 NULL,
750 NULL
751 };