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