Import 2.1.77
[davej-history.git] / fs / proc / array.c
blob260536842971eec84f48a812f9dd49509ef5f86b
1 /*
2 * linux/fs/proc/array.c
4 * Copyright (C) 1992 by Linus Torvalds
5 * based on ideas by Darren Senn
7 * Fixes:
8 * Michael. K. Johnson: stat,statm extensions.
9 * <johnsonm@stolaf.edu>
11 * Pauline Middelink : Made cmdline,envline only break at '\0's, to
12 * make sure SET_PROCTITLE works. Also removed
13 * bad '!' which forced address recalculation for
14 * EVERY character on the current page.
15 * <middelin@polyware.iaf.nl>
17 * Danny ter Haar : added cpuinfo
18 * <dth@cistron.nl>
20 * Alessandro Rubini : profile extension.
21 * <rubini@ipvvis.unipv.it>
23 * Jeff Tranter : added BogoMips field to cpuinfo
24 * <Jeff_Tranter@Mitel.COM>
26 * Bruno Haible : remove 4K limit for the maps file
27 * <haible@ma2s2.mathematik.uni-karlsruhe.de>
29 * Yves Arrouye : remove removal of trailing spaces in get_array.
30 * <Yves.Arrouye@marin.fdn.fr>
33 #include <linux/types.h>
34 #include <linux/errno.h>
35 #include <linux/sched.h>
36 #include <linux/kernel.h>
37 #include <linux/kernel_stat.h>
38 #include <linux/tty.h>
39 #include <linux/user.h>
40 #include <linux/a.out.h>
41 #include <linux/string.h>
42 #include <linux/mman.h>
43 #include <linux/proc_fs.h>
44 #include <linux/ioport.h>
45 #include <linux/config.h>
46 #include <linux/mm.h>
47 #include <linux/pagemap.h>
48 #include <linux/swap.h>
49 #include <linux/slab.h>
50 #include <linux/smp.h>
51 #include <linux/signal.h>
53 #include <asm/uaccess.h>
54 #include <asm/pgtable.h>
55 #include <asm/io.h>
57 #define LOAD_INT(x) ((x) >> FSHIFT)
58 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
60 #ifdef CONFIG_DEBUG_MALLOC
61 int get_malloc(char * buffer);
62 #endif
65 static ssize_t read_core(struct file * file, char * buf,
66 size_t count, loff_t *ppos)
68 unsigned long p = *ppos, memsize;
69 ssize_t read;
70 ssize_t count1;
71 char * pnt;
72 struct user dump;
73 #if defined (__i386__) || defined (__mc68000__)
74 # define FIRST_MAPPED PAGE_SIZE /* we don't have page 0 mapped on x86.. */
75 #else
76 # define FIRST_MAPPED 0
77 #endif
79 memset(&dump, 0, sizeof(struct user));
80 dump.magic = CMAGIC;
81 dump.u_dsize = max_mapnr;
82 #ifdef __alpha__
83 dump.start_data = PAGE_OFFSET;
84 #endif
86 memsize = (max_mapnr + 1) << PAGE_SHIFT;
87 if (p >= memsize)
88 return 0;
89 if (count > memsize - p)
90 count = memsize - p;
91 read = 0;
93 if (p < sizeof(struct user) && count > 0) {
94 count1 = count;
95 if (p + count1 > sizeof(struct user))
96 count1 = sizeof(struct user)-p;
97 pnt = (char *) &dump + p;
98 copy_to_user(buf,(void *) pnt, count1);
99 buf += count1;
100 p += count1;
101 count -= count1;
102 read += count1;
105 if (count > 0 && p < PAGE_SIZE + FIRST_MAPPED) {
106 count1 = PAGE_SIZE + FIRST_MAPPED - p;
107 if (count1 > count)
108 count1 = count;
109 clear_user(buf, count1);
110 buf += count1;
111 p += count1;
112 count -= count1;
113 read += count1;
115 if (count > 0) {
116 copy_to_user(buf, (void *) (PAGE_OFFSET+p-PAGE_SIZE), count);
117 read += count;
119 *ppos += read;
120 return read;
123 static struct file_operations proc_kcore_operations = {
124 NULL, /* lseek */
125 read_core,
128 struct inode_operations proc_kcore_inode_operations = {
129 &proc_kcore_operations,
133 * This function accesses profiling information. The returned data is
134 * binary: the sampling step and the actual contents of the profile
135 * buffer. Use of the program readprofile is recommended in order to
136 * get meaningful info out of these data.
138 static ssize_t read_profile(struct file *file, char *buf,
139 size_t count, loff_t *ppos)
141 unsigned long p = *ppos;
142 ssize_t read;
143 char * pnt;
144 unsigned int sample_step = 1 << prof_shift;
146 if (p >= (prof_len+1)*sizeof(unsigned int))
147 return 0;
148 if (count > (prof_len+1)*sizeof(unsigned int) - p)
149 count = (prof_len+1)*sizeof(unsigned int) - p;
150 read = 0;
152 while (p < sizeof(unsigned int) && count > 0) {
153 put_user(*((char *)(&sample_step)+p),buf);
154 buf++; p++; count--; read++;
156 pnt = (char *)prof_buffer + p - sizeof(unsigned int);
157 copy_to_user(buf,(void *)pnt,count);
158 read += count;
159 *ppos += read;
160 return read;
164 * Writing to /proc/profile resets the counters
166 * Writing a 'profiling multiplier' value into it also re-sets the profiling
167 * interrupt frequency, on architectures that support this.
169 static ssize_t write_profile(struct file * file, const char * buf,
170 size_t count, loff_t *ppos)
172 #ifdef __SMP__
173 extern int setup_profiling_timer (unsigned int multiplier);
175 if (count==sizeof(int)) {
176 unsigned int multiplier;
178 if (copy_from_user(&multiplier, buf, sizeof(int)))
179 return -EFAULT;
181 if (setup_profiling_timer(multiplier))
182 return -EINVAL;
184 #endif
186 memset(prof_buffer, 0, prof_len * sizeof(*prof_buffer));
187 return count;
190 static struct file_operations proc_profile_operations = {
191 NULL, /* lseek */
192 read_profile,
193 write_profile,
196 struct inode_operations proc_profile_inode_operations = {
197 &proc_profile_operations,
201 static int get_loadavg(char * buffer)
203 int a, b, c;
205 a = avenrun[0] + (FIXED_1/200);
206 b = avenrun[1] + (FIXED_1/200);
207 c = avenrun[2] + (FIXED_1/200);
208 return sprintf(buffer,"%d.%02d %d.%02d %d.%02d %d/%d %d\n",
209 LOAD_INT(a), LOAD_FRAC(a),
210 LOAD_INT(b), LOAD_FRAC(b),
211 LOAD_INT(c), LOAD_FRAC(c),
212 nr_running, nr_tasks, last_pid);
215 static int get_kstat(char * buffer)
217 int i, len;
218 unsigned sum = 0;
219 extern unsigned long total_forks;
220 unsigned long ticks;
222 ticks = jiffies * smp_num_cpus;
223 for (i = 0 ; i < NR_IRQS ; i++)
224 sum += kstat.interrupts[i];
225 len = sprintf(buffer,
226 "cpu %u %u %u %lu\n"
227 "disk %u %u %u %u\n"
228 "disk_rio %u %u %u %u\n"
229 "disk_wio %u %u %u %u\n"
230 "disk_rblk %u %u %u %u\n"
231 "disk_wblk %u %u %u %u\n"
232 "page %u %u\n"
233 "swap %u %u\n"
234 "intr %u",
235 kstat.cpu_user,
236 kstat.cpu_nice,
237 kstat.cpu_system,
238 ticks - (kstat.cpu_user + kstat.cpu_nice + kstat.cpu_system),
239 kstat.dk_drive[0], kstat.dk_drive[1],
240 kstat.dk_drive[2], kstat.dk_drive[3],
241 kstat.dk_drive_rio[0], kstat.dk_drive_rio[1],
242 kstat.dk_drive_rio[2], kstat.dk_drive_rio[3],
243 kstat.dk_drive_wio[0], kstat.dk_drive_wio[1],
244 kstat.dk_drive_wio[2], kstat.dk_drive_wio[3],
245 kstat.dk_drive_rblk[0], kstat.dk_drive_rblk[1],
246 kstat.dk_drive_rblk[2], kstat.dk_drive_rblk[3],
247 kstat.dk_drive_wblk[0], kstat.dk_drive_wblk[1],
248 kstat.dk_drive_wblk[2], kstat.dk_drive_wblk[3],
249 kstat.pgpgin,
250 kstat.pgpgout,
251 kstat.pswpin,
252 kstat.pswpout,
253 sum);
254 for (i = 0 ; i < NR_IRQS ; i++)
255 len += sprintf(buffer + len, " %u", kstat.interrupts[i]);
256 len += sprintf(buffer + len,
257 "\nctxt %u\n"
258 "btime %lu\n"
259 "processes %lu\n",
260 kstat.context_swtch,
261 xtime.tv_sec - jiffies / HZ,
262 total_forks);
263 return len;
267 static int get_uptime(char * buffer)
269 unsigned long uptime;
270 unsigned long idle;
272 uptime = jiffies;
273 idle = task[0]->times.tms_utime + task[0]->times.tms_stime;
275 /* The formula for the fraction parts really is ((t * 100) / HZ) % 100, but
276 that would overflow about every five days at HZ == 100.
277 Therefore the identity a = (a / b) * b + a % b is used so that it is
278 calculated as (((t / HZ) * 100) + ((t % HZ) * 100) / HZ) % 100.
279 The part in front of the '+' always evaluates as 0 (mod 100). All divisions
280 in the above formulas are truncating. For HZ being a power of 10, the
281 calculations simplify to the version in the #else part (if the printf
282 format is adapted to the same number of digits as zeroes in HZ.
284 #if HZ!=100
285 return sprintf(buffer,"%lu.%02lu %lu.%02lu\n",
286 uptime / HZ,
287 (((uptime % HZ) * 100) / HZ) % 100,
288 idle / HZ,
289 (((idle % HZ) * 100) / HZ) % 100);
290 #else
291 return sprintf(buffer,"%lu.%02lu %lu.%02lu\n",
292 uptime / HZ,
293 uptime % HZ,
294 idle / HZ,
295 idle % HZ);
296 #endif
299 static int get_meminfo(char * buffer)
301 struct sysinfo i;
302 int len;
304 si_meminfo(&i);
305 si_swapinfo(&i);
306 len = sprintf(buffer, " total: used: free: shared: buffers: cached:\n"
307 "Mem: %8lu %8lu %8lu %8lu %8lu %8lu\n"
308 "Swap: %8lu %8lu %8lu\n",
309 i.totalram, i.totalram-i.freeram, i.freeram, i.sharedram, i.bufferram, page_cache_size*PAGE_SIZE,
310 i.totalswap, i.totalswap-i.freeswap, i.freeswap);
312 * Tagged format, for easy grepping and expansion. The above will go away
313 * eventually, once the tools have been updated.
315 return len + sprintf(buffer+len,
316 "MemTotal: %8lu kB\n"
317 "MemFree: %8lu kB\n"
318 "MemShared: %8lu kB\n"
319 "Buffers: %8lu kB\n"
320 "Cached: %8lu kB\n"
321 "SwapTotal: %8lu kB\n"
322 "SwapFree: %8lu kB\n",
323 i.totalram >> 10,
324 i.freeram >> 10,
325 i.sharedram >> 10,
326 i.bufferram >> 10,
327 page_cache_size << (PAGE_SHIFT - 10),
328 i.totalswap >> 10,
329 i.freeswap >> 10);
332 static int get_version(char * buffer)
334 extern char *linux_banner;
336 strcpy(buffer, linux_banner);
337 return strlen(buffer);
340 static int get_cmdline(char * buffer)
342 extern char saved_command_line[];
344 return sprintf(buffer, "%s\n", saved_command_line);
347 static unsigned long get_phys_addr(struct task_struct * p, unsigned long ptr)
349 pgd_t *page_dir;
350 pmd_t *page_middle;
351 pte_t pte;
353 if (!p || !p->mm || ptr >= TASK_SIZE)
354 return 0;
355 /* Check for NULL pgd .. shouldn't happen! */
356 if (!p->mm->pgd) {
357 printk("get_phys_addr: pid %d has NULL pgd!\n", p->pid);
358 return 0;
361 page_dir = pgd_offset(p->mm,ptr);
362 if (pgd_none(*page_dir))
363 return 0;
364 if (pgd_bad(*page_dir)) {
365 printk("bad page directory entry %08lx\n", pgd_val(*page_dir));
366 pgd_clear(page_dir);
367 return 0;
369 page_middle = pmd_offset(page_dir,ptr);
370 if (pmd_none(*page_middle))
371 return 0;
372 if (pmd_bad(*page_middle)) {
373 printk("bad page middle entry %08lx\n", pmd_val(*page_middle));
374 pmd_clear(page_middle);
375 return 0;
377 pte = *pte_offset(page_middle,ptr);
378 if (!pte_present(pte))
379 return 0;
380 return pte_page(pte) + (ptr & ~PAGE_MASK);
383 static int get_array(struct task_struct *p, unsigned long start, unsigned long end, char * buffer)
385 unsigned long addr;
386 int size = 0, result = 0;
387 char c;
389 if (start >= end)
390 return result;
391 for (;;) {
392 addr = get_phys_addr(p, start);
393 if (!addr)
394 return result;
395 do {
396 c = *(char *) addr;
397 if (!c)
398 result = size;
399 if (size < PAGE_SIZE)
400 buffer[size++] = c;
401 else
402 return result;
403 addr++;
404 start++;
405 if (!c && start >= end)
406 return result;
407 } while (addr & ~PAGE_MASK);
409 return result;
412 static int get_env(int pid, char * buffer)
414 struct task_struct *p = find_task_by_pid(pid);
416 if (!p || !p->mm)
417 return 0;
418 return get_array(p, p->mm->env_start, p->mm->env_end, buffer);
421 static int get_arg(int pid, char * buffer)
423 struct task_struct *p = find_task_by_pid(pid);
425 if (!p || !p->mm)
426 return 0;
427 return get_array(p, p->mm->arg_start, p->mm->arg_end, buffer);
431 * These bracket the sleeping functions..
433 extern void scheduling_functions_start_here(void);
434 extern void scheduling_functions_end_here(void);
435 #define first_sched ((unsigned long) scheduling_functions_start_here)
436 #define last_sched ((unsigned long) scheduling_functions_end_here)
438 static unsigned long get_wchan(struct task_struct *p)
440 if (!p || p == current || p->state == TASK_RUNNING)
441 return 0;
442 #if defined(__i386__)
444 unsigned long ebp, eip;
445 unsigned long stack_page;
446 int count = 0;
448 stack_page = 4096 + (unsigned long)p;
449 if (!stack_page)
450 return 0;
451 ebp = p->tss.ebp;
452 do {
453 if (ebp < stack_page || ebp >= 4092+stack_page)
454 return 0;
455 eip = *(unsigned long *) (ebp+4);
456 if (eip < first_sched || eip >= last_sched)
457 return eip;
458 ebp = *(unsigned long *) ebp;
459 } while (count++ < 16);
461 #elif defined(__alpha__)
463 * This one depends on the frame size of schedule(). Do a
464 * "disass schedule" in gdb to find the frame size. Also, the
465 * code assumes that sleep_on() follows immediately after
466 * interruptible_sleep_on() and that add_timer() follows
467 * immediately after interruptible_sleep(). Ugly, isn't it?
468 * Maybe adding a wchan field to task_struct would be better,
469 * after all...
472 unsigned long schedule_frame;
473 unsigned long pc;
475 pc = thread_saved_pc(&p->tss);
476 if (pc >= first_sched && pc < last_sched) {
477 schedule_frame = ((unsigned long *)p->tss.ksp)[6];
478 return ((unsigned long *)schedule_frame)[12];
480 return pc;
482 #elif defined(__mc68000__)
484 unsigned long fp, pc;
485 unsigned long stack_page;
486 int count = 0;
487 extern int sys_pause (void);
489 stack_page = p->kernel_stack_page;
490 if (!stack_page)
491 return 0;
492 fp = ((struct switch_stack *)p->tss.ksp)->a6;
493 do {
494 if (fp < stack_page || fp >= 4088+stack_page)
495 return 0;
496 pc = ((unsigned long *)fp)[1];
497 /* FIXME: This depends on the order of these functions. */
498 if (pc < first_sched || pc >= last_sched)
499 return pc;
500 fp = *(unsigned long *) fp;
501 } while (count++ < 16);
503 #elif defined(__powerpc__)
504 return (p->tss.wchan);
505 #endif
506 return 0;
509 #if defined(__i386__)
510 # define KSTK_EIP(tsk) (((unsigned long *)(4096+(unsigned long)(tsk)))[1019])
511 # define KSTK_ESP(tsk) (((unsigned long *)(4096+(unsigned long)(tsk)))[1022])
512 #elif defined(__alpha__)
514 * See arch/alpha/kernel/ptrace.c for details.
516 # define PT_REG(reg) (PAGE_SIZE - sizeof(struct pt_regs) \
517 + (long)&((struct pt_regs *)0)->reg)
518 # define KSTK_EIP(tsk) \
519 (*(unsigned long *)(PT_REG(pc) + PAGE_SIZE + (unsigned long)(tsk)))
520 # define KSTK_ESP(tsk) ((tsk) == current ? rdusp() : (tsk)->tss.usp)
521 #elif defined(__mc68000__)
522 #define KSTK_EIP(tsk) \
523 ({ \
524 unsigned long eip = 0; \
525 if ((tsk)->tss.esp0 > PAGE_SIZE && \
526 MAP_NR((tsk)->tss.esp0) < max_mapnr) \
527 eip = ((struct pt_regs *) (tsk)->tss.esp0)->pc; \
528 eip; })
529 #define KSTK_ESP(tsk) ((tsk) == current ? rdusp() : (tsk)->tss.usp)
530 #elif defined(__powerpc__)
531 #define KSTK_EIP(tsk) ((tsk)->tss.regs->nip)
532 #define KSTK_ESP(tsk) ((tsk)->tss.regs->gpr[1])
533 #elif defined (__sparc_v9__)
534 # define KSTK_EIP(tsk) ((tsk)->tss.kregs->tpc)
535 # define KSTK_ESP(tsk) ((tsk)->tss.kregs->u_regs[UREG_FP])
536 #elif defined(__sparc__)
537 # define KSTK_EIP(tsk) ((tsk)->tss.kregs->pc)
538 # define KSTK_ESP(tsk) ((tsk)->tss.kregs->u_regs[UREG_FP])
539 #endif
541 /* Gcc optimizes away "strlen(x)" for constant x */
542 #define ADDBUF(buffer, string) \
543 do { memcpy(buffer, string, strlen(string)); \
544 buffer += strlen(string); } while (0)
546 static inline char * task_name(struct task_struct *p, char * buf)
548 int i;
549 char * name;
551 ADDBUF(buf, "Name:\t");
552 name = p->comm;
553 i = sizeof(p->comm);
554 do {
555 unsigned char c = *name;
556 name++;
557 i--;
558 *buf = c;
559 if (!c)
560 break;
561 if (c == '\\') {
562 buf[1] = c;
563 buf += 2;
564 continue;
566 if (c == '\n') {
567 buf[0] = '\\';
568 buf[1] = 'n';
569 buf += 2;
570 continue;
572 buf++;
573 } while (i);
574 *buf = '\n';
575 return buf+1;
578 static inline char * task_state(struct task_struct *p, char *buffer)
580 #define NR_STATES (sizeof(states)/sizeof(const char *))
581 unsigned int n = p->state;
582 static const char * states[] = {
583 "R (running)",
584 "S (sleeping)",
585 "D (disk sleep)",
586 "Z (zombie)",
587 "T (stopped)",
588 "W (paging)",
589 ". Huh?"
592 if (n >= NR_STATES)
593 n = NR_STATES-1;
595 buffer += sprintf(buffer,
596 "State:\t%s\n"
597 "Pid:\t%d\n"
598 "PPid:\t%d\n"
599 "Uid:\t%d\t%d\t%d\t%d\n"
600 "Gid:\t%d\t%d\t%d\t%d\n",
601 states[n],
602 p->pid, p->p_pptr->pid,
603 p->uid, p->euid, p->suid, p->fsuid,
604 p->gid, p->egid, p->sgid, p->fsgid);
605 return buffer;
608 static inline char * task_mem(struct task_struct *p, char *buffer)
610 struct mm_struct * mm = p->mm;
612 if (mm && mm != &init_mm) {
613 struct vm_area_struct * vma = mm->mmap;
614 unsigned long data = 0, stack = 0;
615 unsigned long exec = 0, lib = 0;
617 for (vma = mm->mmap; vma; vma = vma->vm_next) {
618 unsigned long len = (vma->vm_end - vma->vm_start) >> 10;
619 if (!vma->vm_dentry) {
620 data += len;
621 if (vma->vm_flags & VM_GROWSDOWN)
622 stack += len;
623 continue;
625 if (vma->vm_flags & VM_WRITE)
626 continue;
627 if (vma->vm_flags & VM_EXEC) {
628 exec += len;
629 if (vma->vm_flags & VM_EXECUTABLE)
630 continue;
631 lib += len;
634 buffer += sprintf(buffer,
635 "VmSize:\t%8lu kB\n"
636 "VmLck:\t%8lu kB\n"
637 "VmRSS:\t%8lu kB\n"
638 "VmData:\t%8lu kB\n"
639 "VmStk:\t%8lu kB\n"
640 "VmExe:\t%8lu kB\n"
641 "VmLib:\t%8lu kB\n",
642 mm->total_vm << (PAGE_SHIFT-10),
643 mm->locked_vm << (PAGE_SHIFT-10),
644 mm->rss << (PAGE_SHIFT-10),
645 data - stack, stack,
646 exec - lib, lib);
648 return buffer;
651 char * render_sigset_t(sigset_t *set, char *buffer)
653 int i = _NSIG, x;
654 do {
655 i -= 4, x = 0;
656 if (sigismember(set, i+1)) x |= 1;
657 if (sigismember(set, i+2)) x |= 2;
658 if (sigismember(set, i+3)) x |= 4;
659 if (sigismember(set, i+4)) x |= 8;
660 *buffer++ = (x < 10 ? '0' : 'a' - 10) + x;
661 } while (i >= 4);
662 *buffer = 0;
663 return buffer;
666 static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
667 sigset_t *catch)
669 struct k_sigaction *k;
670 int i;
672 sigemptyset(ign);
673 sigemptyset(catch);
675 if (p->sig) {
676 k = p->sig->action;
677 for (i = 1; i <= _NSIG; ++i, ++k) {
678 if (k->sa.sa_handler == SIG_IGN)
679 sigaddset(ign, i);
680 else if (k->sa.sa_handler != SIG_DFL)
681 sigaddset(catch, i);
686 static inline char * task_sig(struct task_struct *p, char *buffer)
688 sigset_t ign, catch;
690 buffer += sprintf(buffer, "SigPnd:\t");
691 buffer = render_sigset_t(&p->signal, buffer);
692 *buffer++ = '\n';
693 buffer += sprintf(buffer, "SigBlk:\t");
694 buffer = render_sigset_t(&p->blocked, buffer);
695 *buffer++ = '\n';
697 collect_sigign_sigcatch(p, &ign, &catch);
698 buffer += sprintf(buffer, "SigIgn:\t");
699 buffer = render_sigset_t(&ign, buffer);
700 *buffer++ = '\n';
701 buffer += sprintf(buffer, "SigCat:\t");
702 buffer = render_sigset_t(&catch, buffer);
703 *buffer++ = '\n';
705 return buffer;
708 static int get_status(int pid, char * buffer)
710 char * orig = buffer;
711 struct task_struct *tsk = find_task_by_pid(pid);
713 if (!tsk)
714 return 0;
715 buffer = task_name(tsk, buffer);
716 buffer = task_state(tsk, buffer);
717 buffer = task_mem(tsk, buffer);
718 buffer = task_sig(tsk, buffer);
719 return buffer - orig;
722 static int get_stat(int pid, char * buffer)
724 struct task_struct *tsk = find_task_by_pid(pid);
725 unsigned long vsize, eip, esp, wchan;
726 long priority, nice;
727 int tty_pgrp;
728 sigset_t sigign, sigcatch;
729 char signal_str[sizeof(sigset_t)*2+1];
730 char blocked_str[sizeof(sigset_t)*2+1];
731 char sigign_str[sizeof(sigset_t)*2+1];
732 char sigcatch_str[sizeof(sigset_t)*2+1];
733 char state;
735 if (!tsk)
736 return 0;
737 if (tsk->state < 0 || tsk->state > 5)
738 state = '.';
739 else
740 state = "RSDZTW"[tsk->state];
741 vsize = eip = esp = 0;
742 if (tsk->mm && tsk->mm != &init_mm) {
743 struct vm_area_struct *vma = tsk->mm->mmap;
744 while (vma) {
745 vsize += vma->vm_end - vma->vm_start;
746 vma = vma->vm_next;
748 eip = KSTK_EIP(tsk);
749 esp = KSTK_ESP(tsk);
752 wchan = get_wchan(tsk);
754 collect_sigign_sigcatch(tsk, &sigign, &sigcatch);
755 render_sigset_t(&tsk->signal, signal_str);
756 render_sigset_t(&tsk->blocked, blocked_str);
757 render_sigset_t(&sigign, sigign_str);
758 render_sigset_t(&sigcatch, sigcatch_str);
760 if (tsk->tty)
761 tty_pgrp = tsk->tty->pgrp;
762 else
763 tty_pgrp = -1;
765 /* scale priority and nice values from timeslices to -20..20 */
766 /* to make it look like a "normal" unix priority/nice value */
767 priority = tsk->counter;
768 priority = 20 - (priority * 10 + DEF_PRIORITY / 2) / DEF_PRIORITY;
769 nice = tsk->priority;
770 nice = 20 - (nice * 20 + DEF_PRIORITY / 2) / DEF_PRIORITY;
772 return sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
773 %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %lu %lu %ld %lu %lu %lu %lu %lu \
774 %lu %s %s %s %s %lu %lu %lu\n",
775 pid,
776 tsk->comm,
777 state,
778 tsk->p_pptr->pid,
779 tsk->pgrp,
780 tsk->session,
781 tsk->tty ? kdev_t_to_nr(tsk->tty->device) : 0,
782 tty_pgrp,
783 tsk->flags,
784 tsk->min_flt,
785 tsk->cmin_flt,
786 tsk->maj_flt,
787 tsk->cmaj_flt,
788 tsk->times.tms_utime,
789 tsk->times.tms_stime,
790 tsk->times.tms_cutime,
791 tsk->times.tms_cstime,
792 priority,
793 nice,
794 tsk->timeout,
795 tsk->it_real_value,
796 tsk->start_time,
797 vsize,
798 tsk->mm ? tsk->mm->rss : 0, /* you might want to shift this left 3 */
799 tsk->rlim ? tsk->rlim[RLIMIT_RSS].rlim_cur : 0,
800 tsk->mm ? tsk->mm->start_code : 0,
801 tsk->mm ? tsk->mm->end_code : 0,
802 tsk->mm ? tsk->mm->start_stack : 0,
803 esp,
804 eip,
805 signal_str,
806 blocked_str,
807 sigign_str,
808 sigcatch_str,
809 wchan,
810 tsk->nswap,
811 tsk->cnswap);
814 static inline void statm_pte_range(pmd_t * pmd, unsigned long address, unsigned long size,
815 int * pages, int * shared, int * dirty, int * total)
817 pte_t * pte;
818 unsigned long end;
820 if (pmd_none(*pmd))
821 return;
822 if (pmd_bad(*pmd)) {
823 printk("statm_pte_range: bad pmd (%08lx)\n", pmd_val(*pmd));
824 pmd_clear(pmd);
825 return;
827 pte = pte_offset(pmd, address);
828 address &= ~PMD_MASK;
829 end = address + size;
830 if (end > PMD_SIZE)
831 end = PMD_SIZE;
832 do {
833 pte_t page = *pte;
835 address += PAGE_SIZE;
836 pte++;
837 if (pte_none(page))
838 continue;
839 ++*total;
840 if (!pte_present(page))
841 continue;
842 ++*pages;
843 if (pte_dirty(page))
844 ++*dirty;
845 if (MAP_NR(pte_page(page)) >= max_mapnr)
846 continue;
847 if (atomic_read(&mem_map[MAP_NR(pte_page(page))].count) > 1)
848 ++*shared;
849 } while (address < end);
852 static inline void statm_pmd_range(pgd_t * pgd, unsigned long address, unsigned long size,
853 int * pages, int * shared, int * dirty, int * total)
855 pmd_t * pmd;
856 unsigned long end;
858 if (pgd_none(*pgd))
859 return;
860 if (pgd_bad(*pgd)) {
861 printk("statm_pmd_range: bad pgd (%08lx)\n", pgd_val(*pgd));
862 pgd_clear(pgd);
863 return;
865 pmd = pmd_offset(pgd, address);
866 address &= ~PGDIR_MASK;
867 end = address + size;
868 if (end > PGDIR_SIZE)
869 end = PGDIR_SIZE;
870 do {
871 statm_pte_range(pmd, address, end - address, pages, shared, dirty, total);
872 address = (address + PMD_SIZE) & PMD_MASK;
873 pmd++;
874 } while (address < end);
877 static void statm_pgd_range(pgd_t * pgd, unsigned long address, unsigned long end,
878 int * pages, int * shared, int * dirty, int * total)
880 while (address < end) {
881 statm_pmd_range(pgd, address, end - address, pages, shared, dirty, total);
882 address = (address + PGDIR_SIZE) & PGDIR_MASK;
883 pgd++;
887 static int get_statm(int pid, char * buffer)
889 struct task_struct *tsk = find_task_by_pid(pid);
890 int size=0, resident=0, share=0, trs=0, lrs=0, drs=0, dt=0;
892 if (!tsk)
893 return 0;
894 if (tsk->mm && tsk->mm != &init_mm) {
895 struct vm_area_struct * vma = tsk->mm->mmap;
897 while (vma) {
898 pgd_t *pgd = pgd_offset(tsk->mm, vma->vm_start);
899 int pages = 0, shared = 0, dirty = 0, total = 0;
901 statm_pgd_range(pgd, vma->vm_start, vma->vm_end, &pages, &shared, &dirty, &total);
902 resident += pages;
903 share += shared;
904 dt += dirty;
905 size += total;
906 if (vma->vm_flags & VM_EXECUTABLE)
907 trs += pages; /* text */
908 else if (vma->vm_flags & VM_GROWSDOWN)
909 drs += pages; /* stack */
910 else if (vma->vm_end > 0x60000000)
911 lrs += pages; /* library */
912 else
913 drs += pages;
914 vma = vma->vm_next;
917 return sprintf(buffer,"%d %d %d %d %d %d %d\n",
918 size, resident, share, trs, lrs, drs, dt);
922 * The way we support synthetic files > 4K
923 * - without storing their contents in some buffer and
924 * - without walking through the entire synthetic file until we reach the
925 * position of the requested data
926 * is to cleverly encode the current position in the file's f_pos field.
927 * There is no requirement that a read() call which returns `count' bytes
928 * of data increases f_pos by exactly `count'.
930 * This idea is Linus' one. Bruno implemented it.
934 * For the /proc/<pid>/maps file, we use fixed length records, each containing
935 * a single line.
937 #define MAPS_LINE_LENGTH 4096
938 #define MAPS_LINE_SHIFT 12
940 * f_pos = (number of the vma in the task->mm->mmap list) * MAPS_LINE_LENGTH
941 * + (index into the line)
943 /* for systems with sizeof(void*) == 4: */
944 #define MAPS_LINE_FORMAT4 "%08lx-%08lx %s %08lx %s %lu"
945 #define MAPS_LINE_MAX4 49 /* sum of 8 1 8 1 4 1 8 1 5 1 10 1 */
947 /* for systems with sizeof(void*) == 8: */
948 #define MAPS_LINE_FORMAT8 "%016lx-%016lx %s %016lx %s %lu"
949 #define MAPS_LINE_MAX8 73 /* sum of 16 1 16 1 4 1 16 1 5 1 10 1 */
951 #define MAPS_LINE_MAX MAPS_LINE_MAX8
954 static ssize_t read_maps (int pid, struct file * file, char * buf,
955 size_t count, loff_t *ppos)
957 struct task_struct *p;
958 struct vm_area_struct * map, * next;
959 char * destptr = buf, * buffer;
960 loff_t lineno;
961 ssize_t column, i;
962 int volatile_task;
963 long retval;
966 * We might sleep getting the page, so get it first.
968 retval = -ENOMEM;
969 buffer = (char*)__get_free_page(GFP_KERNEL);
970 if (!buffer)
971 goto out;
973 retval = -EINVAL;
974 p = find_task_by_pid(pid);
975 if (!p)
976 goto freepage_out;
978 if (!p->mm || p->mm == &init_mm || count == 0)
979 goto getlen_out;
981 /* Check whether the mmaps could change if we sleep */
982 volatile_task = (p != current || p->mm->count > 1);
984 /* decode f_pos */
985 lineno = *ppos >> MAPS_LINE_SHIFT;
986 column = *ppos & (MAPS_LINE_LENGTH-1);
988 /* quickly go to line lineno */
989 for (map = p->mm->mmap, i = 0; map && (i < lineno); map = map->vm_next, i++)
990 continue;
992 for ( ; map ; map = next ) {
993 /* produce the next line */
994 char *line;
995 char str[5], *cp = str;
996 int flags;
997 kdev_t dev;
998 unsigned long ino;
999 int maxlen = (sizeof(void*) == 4) ?
1000 MAPS_LINE_MAX4 : MAPS_LINE_MAX8;
1001 int len;
1004 * Get the next vma now (but it won't be used if we sleep).
1006 next = map->vm_next;
1007 flags = map->vm_flags;
1009 *cp++ = flags & VM_READ ? 'r' : '-';
1010 *cp++ = flags & VM_WRITE ? 'w' : '-';
1011 *cp++ = flags & VM_EXEC ? 'x' : '-';
1012 *cp++ = flags & VM_MAYSHARE ? 's' : 'p';
1013 *cp++ = 0;
1015 dev = 0;
1016 ino = 0;
1017 if (map->vm_dentry != NULL) {
1018 dev = map->vm_dentry->d_inode->i_dev;
1019 ino = map->vm_dentry->d_inode->i_ino;
1020 line = d_path(map->vm_dentry, buffer, PAGE_SIZE);
1021 buffer[PAGE_SIZE-1] = '\n';
1022 line -= maxlen;
1023 if(line < buffer)
1024 line = buffer;
1025 } else
1026 line = buffer;
1028 len = sprintf(line,
1029 sizeof(void*) == 4 ? MAPS_LINE_FORMAT4 : MAPS_LINE_FORMAT8,
1030 map->vm_start, map->vm_end, str, map->vm_offset,
1031 kdevname(dev), ino);
1033 if(map->vm_dentry) {
1034 for(i = len; i < maxlen; i++)
1035 line[i] = ' ';
1036 len = buffer + PAGE_SIZE - line;
1037 } else
1038 line[len++] = '\n';
1039 if (column >= len) {
1040 column = 0; /* continue with next line at column 0 */
1041 lineno++;
1042 continue; /* we haven't slept */
1045 i = len-column;
1046 if (i > count)
1047 i = count;
1048 copy_to_user(destptr, line+column, i); /* may have slept */
1049 destptr += i;
1050 count -= i;
1051 column += i;
1052 if (column >= len) {
1053 column = 0; /* next time: next line at column 0 */
1054 lineno++;
1057 /* done? */
1058 if (count == 0)
1059 break;
1061 /* By writing to user space, we might have slept.
1062 * Stop the loop, to avoid a race condition.
1064 if (volatile_task)
1065 break;
1068 /* encode f_pos */
1069 *ppos = (lineno << MAPS_LINE_SHIFT) + column;
1071 getlen_out:
1072 retval = destptr - buf;
1074 freepage_out:
1075 free_page((unsigned long)buffer);
1076 out:
1077 return retval;
1080 #ifdef CONFIG_MODULES
1081 extern int get_module_list(char *);
1082 extern int get_ksyms_list(char *, char **, off_t, int);
1083 #endif
1084 extern int get_device_list(char *);
1085 extern int get_filesystem_list(char *);
1086 extern int get_filesystem_info( char * );
1087 extern int get_irq_list(char *);
1088 extern int get_dma_list(char *);
1089 extern int get_cpuinfo(char *);
1090 extern int get_pci_list(char*);
1091 extern int get_md_status (char *);
1092 extern int get_rtc_status (char *);
1093 extern int get_locks_status (char *, char **, off_t, int);
1094 extern int get_swaparea_info (char *);
1095 #ifdef __SMP_PROF__
1096 extern int get_smp_prof_list(char *);
1097 #endif
1098 #ifdef CONFIG_ZORRO
1099 extern int zorro_get_list(char *);
1100 #endif
1101 #if defined (CONFIG_AMIGA) || defined (CONFIG_ATARI)
1102 extern int get_hardware_list(char *);
1103 #endif
1105 static long get_root_array(char * page, int type, char **start,
1106 off_t offset, unsigned long length)
1108 switch (type) {
1109 case PROC_LOADAVG:
1110 return get_loadavg(page);
1112 case PROC_UPTIME:
1113 return get_uptime(page);
1115 case PROC_MEMINFO:
1116 return get_meminfo(page);
1118 #ifdef CONFIG_PCI
1119 case PROC_PCI:
1120 return get_pci_list(page);
1121 #endif
1123 case PROC_CPUINFO:
1124 return get_cpuinfo(page);
1126 case PROC_VERSION:
1127 return get_version(page);
1129 #ifdef CONFIG_DEBUG_MALLOC
1130 case PROC_MALLOC:
1131 return get_malloc(page);
1132 #endif
1134 #ifdef CONFIG_MODULES
1135 case PROC_MODULES:
1136 return get_module_list(page);
1138 case PROC_KSYMS:
1139 return get_ksyms_list(page, start, offset, length);
1140 #endif
1142 case PROC_STAT:
1143 return get_kstat(page);
1145 case PROC_SLABINFO:
1146 return get_slabinfo(page);
1148 case PROC_DEVICES:
1149 return get_device_list(page);
1151 case PROC_INTERRUPTS:
1152 return get_irq_list(page);
1154 case PROC_FILESYSTEMS:
1155 return get_filesystem_list(page);
1157 case PROC_DMA:
1158 return get_dma_list(page);
1160 case PROC_IOPORTS:
1161 return get_ioport_list(page);
1162 #ifdef CONFIG_BLK_DEV_MD
1163 case PROC_MD:
1164 return get_md_status(page);
1165 #endif
1166 #ifdef __SMP_PROF__
1167 case PROC_SMP_PROF:
1168 return get_smp_prof_list(page);
1169 #endif
1170 case PROC_CMDLINE:
1171 return get_cmdline(page);
1173 case PROC_MTAB:
1174 return get_filesystem_info( page );
1176 case PROC_SWAP:
1177 return get_swaparea_info(page);
1178 #ifdef CONFIG_RTC
1179 case PROC_RTC:
1180 return get_rtc_status(page);
1181 #endif
1182 case PROC_LOCKS:
1183 return get_locks_status(page, start, offset, length);
1184 #ifdef CONFIG_ZORRO
1185 case PROC_ZORRO:
1186 return zorro_get_list(page);
1187 #endif
1188 #if defined (CONFIG_AMIGA) || defined (CONFIG_ATARI)
1189 case PROC_HARDWARE:
1190 return get_hardware_list(page);
1191 #endif
1193 return -EBADF;
1196 static int get_process_array(char * page, int pid, int type)
1198 switch (type) {
1199 case PROC_PID_STATUS:
1200 return get_status(pid, page);
1201 case PROC_PID_ENVIRON:
1202 return get_env(pid, page);
1203 case PROC_PID_CMDLINE:
1204 return get_arg(pid, page);
1205 case PROC_PID_STAT:
1206 return get_stat(pid, page);
1207 case PROC_PID_STATM:
1208 return get_statm(pid, page);
1210 return -EBADF;
1214 static inline int fill_array(char * page, int pid, int type, char **start, off_t offset, int length)
1216 if (pid)
1217 return get_process_array(page, pid, type);
1218 return get_root_array(page, type, start, offset, length);
1221 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
1223 static ssize_t array_read(struct file * file, char * buf,
1224 size_t count, loff_t *ppos)
1226 struct inode * inode = file->f_dentry->d_inode;
1227 unsigned long page;
1228 char *start;
1229 ssize_t length;
1230 ssize_t end;
1231 unsigned int type, pid;
1232 struct proc_dir_entry *dp;
1234 if (count > PROC_BLOCK_SIZE)
1235 count = PROC_BLOCK_SIZE;
1236 if (!(page = __get_free_page(GFP_KERNEL)))
1237 return -ENOMEM;
1238 type = inode->i_ino;
1239 pid = type >> 16;
1240 type &= 0x0000ffff;
1241 start = NULL;
1242 dp = (struct proc_dir_entry *) inode->u.generic_ip;
1243 if (dp->get_info)
1244 length = dp->get_info((char *)page, &start, *ppos,
1245 count, 0);
1246 else
1247 length = fill_array((char *) page, pid, type,
1248 &start, *ppos, count);
1249 if (length < 0) {
1250 free_page(page);
1251 return length;
1253 if (start != NULL) {
1254 /* We have had block-adjusting processing! */
1255 copy_to_user(buf, start, length);
1256 *ppos += length;
1257 count = length;
1258 } else {
1259 /* Static 4kB (or whatever) block capacity */
1260 if (*ppos >= length) {
1261 free_page(page);
1262 return 0;
1264 if (count + *ppos > length)
1265 count = length - *ppos;
1266 end = count + *ppos;
1267 copy_to_user(buf, (char *) page + *ppos, count);
1268 *ppos = end;
1270 free_page(page);
1271 return count;
1274 static struct file_operations proc_array_operations = {
1275 NULL, /* array_lseek */
1276 array_read,
1277 NULL, /* array_write */
1278 NULL, /* array_readdir */
1279 NULL, /* array_poll */
1280 NULL, /* array_ioctl */
1281 NULL, /* mmap */
1282 NULL, /* no special open code */
1283 NULL, /* no special release code */
1284 NULL /* can't fsync */
1287 struct inode_operations proc_array_inode_operations = {
1288 &proc_array_operations, /* default base directory file-ops */
1289 NULL, /* create */
1290 NULL, /* lookup */
1291 NULL, /* link */
1292 NULL, /* unlink */
1293 NULL, /* symlink */
1294 NULL, /* mkdir */
1295 NULL, /* rmdir */
1296 NULL, /* mknod */
1297 NULL, /* rename */
1298 NULL, /* readlink */
1299 NULL, /* follow_link */
1300 NULL, /* readpage */
1301 NULL, /* writepage */
1302 NULL, /* bmap */
1303 NULL, /* truncate */
1304 NULL /* permission */
1307 static ssize_t arraylong_read(struct file * file, char * buf,
1308 size_t count, loff_t *ppos)
1310 struct inode * inode = file->f_dentry->d_inode;
1311 unsigned int pid = inode->i_ino >> 16;
1312 unsigned int type = inode->i_ino & 0x0000ffff;
1314 switch (type) {
1315 case PROC_PID_MAPS:
1316 return read_maps(pid, file, buf, count, ppos);
1318 return -EINVAL;
1321 static struct file_operations proc_arraylong_operations = {
1322 NULL, /* array_lseek */
1323 arraylong_read,
1324 NULL, /* array_write */
1325 NULL, /* array_readdir */
1326 NULL, /* array_poll */
1327 NULL, /* array_ioctl */
1328 NULL, /* mmap */
1329 NULL, /* no special open code */
1330 NULL, /* no special release code */
1331 NULL /* can't fsync */
1334 struct inode_operations proc_arraylong_inode_operations = {
1335 &proc_arraylong_operations, /* default base directory file-ops */
1336 NULL, /* create */
1337 NULL, /* lookup */
1338 NULL, /* link */
1339 NULL, /* unlink */
1340 NULL, /* symlink */
1341 NULL, /* mkdir */
1342 NULL, /* rmdir */
1343 NULL, /* mknod */
1344 NULL, /* rename */
1345 NULL, /* readlink */
1346 NULL, /* follow_link */
1347 NULL, /* readpage */
1348 NULL, /* writepage */
1349 NULL, /* bmap */
1350 NULL, /* truncate */
1351 NULL /* permission */