Import 2.1.118
[davej-history.git] / fs / proc / array.c
blobf235a56df5aed1a3df6a1c9c19bbbe96661f1992
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>
32 * Jerome Forissier : added per-CPU time information to /proc/stat
33 * and /proc/<pid>/cpu extension
34 * <forissier@isia.cma.fr>
35 * - Incorporation and non-SMP safe operation
36 * of forissier patch in 2.1.78 by
37 * Hans Marcus <crowbar@concepts.nl>
39 * aeb@cwi.nl : /proc/partitions
42 #include <linux/types.h>
43 #include <linux/errno.h>
44 #include <linux/sched.h>
45 #include <linux/kernel.h>
46 #include <linux/kernel_stat.h>
47 #include <linux/tty.h>
48 #include <linux/user.h>
49 #include <linux/a.out.h>
50 #include <linux/string.h>
51 #include <linux/mman.h>
52 #include <linux/proc_fs.h>
53 #include <linux/ioport.h>
54 #include <linux/config.h>
55 #include <linux/mm.h>
56 #include <linux/pagemap.h>
57 #include <linux/swap.h>
58 #include <linux/slab.h>
59 #include <linux/smp.h>
60 #include <linux/signal.h>
62 #include <asm/uaccess.h>
63 #include <asm/pgtable.h>
64 #include <asm/io.h>
66 #define LOAD_INT(x) ((x) >> FSHIFT)
67 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
69 #ifdef CONFIG_DEBUG_MALLOC
70 int get_malloc(char * buffer);
71 #endif
74 static ssize_t read_core(struct file * file, char * buf,
75 size_t count, loff_t *ppos)
77 unsigned long p = *ppos, memsize;
78 ssize_t read;
79 ssize_t count1;
80 char * pnt;
81 struct user dump;
82 #if defined (__i386__) || defined (__mc68000__)
83 # define FIRST_MAPPED PAGE_SIZE /* we don't have page 0 mapped on x86.. */
84 #else
85 # define FIRST_MAPPED 0
86 #endif
88 memset(&dump, 0, sizeof(struct user));
89 dump.magic = CMAGIC;
90 dump.u_dsize = max_mapnr;
91 #ifdef __alpha__
92 dump.start_data = PAGE_OFFSET;
93 #endif
95 memsize = (max_mapnr + 1) << PAGE_SHIFT;
96 if (p >= memsize)
97 return 0;
98 if (count > memsize - p)
99 count = memsize - p;
100 read = 0;
102 if (p < sizeof(struct user) && count > 0) {
103 count1 = count;
104 if (p + count1 > sizeof(struct user))
105 count1 = sizeof(struct user)-p;
106 pnt = (char *) &dump + p;
107 copy_to_user(buf,(void *) pnt, count1);
108 buf += count1;
109 p += count1;
110 count -= count1;
111 read += count1;
114 if (count > 0 && p < PAGE_SIZE + FIRST_MAPPED) {
115 count1 = PAGE_SIZE + FIRST_MAPPED - p;
116 if (count1 > count)
117 count1 = count;
118 clear_user(buf, count1);
119 buf += count1;
120 p += count1;
121 count -= count1;
122 read += count1;
124 if (count > 0) {
125 copy_to_user(buf, (void *) (PAGE_OFFSET+p-PAGE_SIZE), count);
126 read += count;
128 *ppos += read;
129 return read;
132 static struct file_operations proc_kcore_operations = {
133 NULL, /* lseek */
134 read_core,
137 struct inode_operations proc_kcore_inode_operations = {
138 &proc_kcore_operations,
142 * This function accesses profiling information. The returned data is
143 * binary: the sampling step and the actual contents of the profile
144 * buffer. Use of the program readprofile is recommended in order to
145 * get meaningful info out of these data.
147 static ssize_t read_profile(struct file *file, char *buf,
148 size_t count, loff_t *ppos)
150 unsigned long p = *ppos;
151 ssize_t read;
152 char * pnt;
153 unsigned int sample_step = 1 << prof_shift;
155 if (p >= (prof_len+1)*sizeof(unsigned int))
156 return 0;
157 if (count > (prof_len+1)*sizeof(unsigned int) - p)
158 count = (prof_len+1)*sizeof(unsigned int) - p;
159 read = 0;
161 while (p < sizeof(unsigned int) && count > 0) {
162 put_user(*((char *)(&sample_step)+p),buf);
163 buf++; p++; count--; read++;
165 pnt = (char *)prof_buffer + p - sizeof(unsigned int);
166 copy_to_user(buf,(void *)pnt,count);
167 read += count;
168 *ppos += read;
169 return read;
173 * Writing to /proc/profile resets the counters
175 * Writing a 'profiling multiplier' value into it also re-sets the profiling
176 * interrupt frequency, on architectures that support this.
178 static ssize_t write_profile(struct file * file, const char * buf,
179 size_t count, loff_t *ppos)
181 #ifdef __SMP__
182 extern int setup_profiling_timer (unsigned int multiplier);
184 if (count==sizeof(int)) {
185 unsigned int multiplier;
187 if (copy_from_user(&multiplier, buf, sizeof(int)))
188 return -EFAULT;
190 if (setup_profiling_timer(multiplier))
191 return -EINVAL;
193 #endif
195 memset(prof_buffer, 0, prof_len * sizeof(*prof_buffer));
196 return count;
199 static struct file_operations proc_profile_operations = {
200 NULL, /* lseek */
201 read_profile,
202 write_profile,
205 struct inode_operations proc_profile_inode_operations = {
206 &proc_profile_operations,
210 static int get_loadavg(char * buffer)
212 int a, b, c;
214 a = avenrun[0] + (FIXED_1/200);
215 b = avenrun[1] + (FIXED_1/200);
216 c = avenrun[2] + (FIXED_1/200);
217 return sprintf(buffer,"%d.%02d %d.%02d %d.%02d %d/%d %d\n",
218 LOAD_INT(a), LOAD_FRAC(a),
219 LOAD_INT(b), LOAD_FRAC(b),
220 LOAD_INT(c), LOAD_FRAC(c),
221 nr_running, nr_tasks, last_pid);
224 static int get_kstat(char * buffer)
226 int i, len;
227 unsigned sum = 0;
228 extern unsigned long total_forks;
229 unsigned long ticks;
231 ticks = jiffies * smp_num_cpus;
232 for (i = 0 ; i < NR_IRQS ; i++)
233 sum += kstat_irqs(i);
235 #ifdef __SMP__
236 len = sprintf(buffer,
237 "cpu %u %u %u %lu\n",
238 kstat.cpu_user,
239 kstat.cpu_nice,
240 kstat.cpu_system,
241 jiffies*smp_num_cpus - (kstat.cpu_user + kstat.cpu_nice + kstat.cpu_system));
242 for (i = 0 ; i < smp_num_cpus; i++)
243 len += sprintf(buffer + len, "cpu%d %u %u %u %lu\n",
245 kstat.per_cpu_user[cpu_logical_map(i)],
246 kstat.per_cpu_nice[cpu_logical_map(i)],
247 kstat.per_cpu_system[cpu_logical_map(i)],
248 jiffies - ( kstat.per_cpu_user[cpu_logical_map(i)] \
249 + kstat.per_cpu_nice[cpu_logical_map(i)] \
250 + kstat.per_cpu_system[cpu_logical_map(i)]));
251 len += sprintf(buffer + len,
252 "disk %u %u %u %u\n"
253 "disk_rio %u %u %u %u\n"
254 "disk_wio %u %u %u %u\n"
255 "disk_rblk %u %u %u %u\n"
256 "disk_wblk %u %u %u %u\n"
257 "page %u %u\n"
258 "swap %u %u\n"
259 "intr %u",
260 #else
261 len = sprintf(buffer,
262 "cpu %u %u %u %lu\n"
263 "disk %u %u %u %u\n"
264 "disk_rio %u %u %u %u\n"
265 "disk_wio %u %u %u %u\n"
266 "disk_rblk %u %u %u %u\n"
267 "disk_wblk %u %u %u %u\n"
268 "page %u %u\n"
269 "swap %u %u\n"
270 "intr %u",
271 kstat.cpu_user,
272 kstat.cpu_nice,
273 kstat.cpu_system,
274 ticks - (kstat.cpu_user + kstat.cpu_nice + kstat.cpu_system),
275 #endif
276 kstat.dk_drive[0], kstat.dk_drive[1],
277 kstat.dk_drive[2], kstat.dk_drive[3],
278 kstat.dk_drive_rio[0], kstat.dk_drive_rio[1],
279 kstat.dk_drive_rio[2], kstat.dk_drive_rio[3],
280 kstat.dk_drive_wio[0], kstat.dk_drive_wio[1],
281 kstat.dk_drive_wio[2], kstat.dk_drive_wio[3],
282 kstat.dk_drive_rblk[0], kstat.dk_drive_rblk[1],
283 kstat.dk_drive_rblk[2], kstat.dk_drive_rblk[3],
284 kstat.dk_drive_wblk[0], kstat.dk_drive_wblk[1],
285 kstat.dk_drive_wblk[2], kstat.dk_drive_wblk[3],
286 kstat.pgpgin,
287 kstat.pgpgout,
288 kstat.pswpin,
289 kstat.pswpout,
290 sum);
291 for (i = 0 ; i < NR_IRQS ; i++)
292 len += sprintf(buffer + len, " %u", kstat_irqs(i));
293 len += sprintf(buffer + len,
294 "\nctxt %u\n"
295 "btime %lu\n"
296 "processes %lu\n",
297 kstat.context_swtch,
298 xtime.tv_sec - jiffies / HZ,
299 total_forks);
300 return len;
304 static int get_uptime(char * buffer)
306 unsigned long uptime;
307 unsigned long idle;
309 uptime = jiffies;
310 idle = task[0]->times.tms_utime + task[0]->times.tms_stime;
312 /* The formula for the fraction parts really is ((t * 100) / HZ) % 100, but
313 that would overflow about every five days at HZ == 100.
314 Therefore the identity a = (a / b) * b + a % b is used so that it is
315 calculated as (((t / HZ) * 100) + ((t % HZ) * 100) / HZ) % 100.
316 The part in front of the '+' always evaluates as 0 (mod 100). All divisions
317 in the above formulas are truncating. For HZ being a power of 10, the
318 calculations simplify to the version in the #else part (if the printf
319 format is adapted to the same number of digits as zeroes in HZ.
321 #if HZ!=100
322 return sprintf(buffer,"%lu.%02lu %lu.%02lu\n",
323 uptime / HZ,
324 (((uptime % HZ) * 100) / HZ) % 100,
325 idle / HZ,
326 (((idle % HZ) * 100) / HZ) % 100);
327 #else
328 return sprintf(buffer,"%lu.%02lu %lu.%02lu\n",
329 uptime / HZ,
330 uptime % HZ,
331 idle / HZ,
332 idle % HZ);
333 #endif
336 static int get_meminfo(char * buffer)
338 struct sysinfo i;
339 int len;
341 si_meminfo(&i);
342 si_swapinfo(&i);
343 len = sprintf(buffer, " total: used: free: shared: buffers: cached:\n"
344 "Mem: %8lu %8lu %8lu %8lu %8lu %8lu\n"
345 "Swap: %8lu %8lu %8lu\n",
346 i.totalram, i.totalram-i.freeram, i.freeram, i.sharedram, i.bufferram, page_cache_size*PAGE_SIZE,
347 i.totalswap, i.totalswap-i.freeswap, i.freeswap);
349 * Tagged format, for easy grepping and expansion. The above will go away
350 * eventually, once the tools have been updated.
352 return len + sprintf(buffer+len,
353 "MemTotal: %8lu kB\n"
354 "MemFree: %8lu kB\n"
355 "MemShared: %8lu kB\n"
356 "Buffers: %8lu kB\n"
357 "Cached: %8lu kB\n"
358 "SwapTotal: %8lu kB\n"
359 "SwapFree: %8lu kB\n",
360 i.totalram >> 10,
361 i.freeram >> 10,
362 i.sharedram >> 10,
363 i.bufferram >> 10,
364 page_cache_size << (PAGE_SHIFT - 10),
365 i.totalswap >> 10,
366 i.freeswap >> 10);
369 static int get_version(char * buffer)
371 extern char *linux_banner;
373 strcpy(buffer, linux_banner);
374 return strlen(buffer);
377 static int get_cmdline(char * buffer)
379 extern char saved_command_line[];
381 return sprintf(buffer, "%s\n", saved_command_line);
384 static unsigned long get_phys_addr(struct task_struct * p, unsigned long ptr)
386 pgd_t *page_dir;
387 pmd_t *page_middle;
388 pte_t pte;
390 if (!p || !p->mm || ptr >= TASK_SIZE)
391 return 0;
392 /* Check for NULL pgd .. shouldn't happen! */
393 if (!p->mm->pgd) {
394 printk("get_phys_addr: pid %d has NULL pgd!\n", p->pid);
395 return 0;
398 page_dir = pgd_offset(p->mm,ptr);
399 if (pgd_none(*page_dir))
400 return 0;
401 if (pgd_bad(*page_dir)) {
402 printk("bad page directory entry %08lx\n", pgd_val(*page_dir));
403 pgd_clear(page_dir);
404 return 0;
406 page_middle = pmd_offset(page_dir,ptr);
407 if (pmd_none(*page_middle))
408 return 0;
409 if (pmd_bad(*page_middle)) {
410 printk("bad page middle entry %08lx\n", pmd_val(*page_middle));
411 pmd_clear(page_middle);
412 return 0;
414 pte = *pte_offset(page_middle,ptr);
415 if (!pte_present(pte))
416 return 0;
417 return pte_page(pte) + (ptr & ~PAGE_MASK);
420 static int get_array(struct task_struct *p, unsigned long start, unsigned long end, char * buffer)
422 unsigned long addr;
423 int size = 0, result = 0;
424 char c;
426 if (start >= end)
427 return result;
428 for (;;) {
429 addr = get_phys_addr(p, start);
430 if (!addr)
431 return result;
432 do {
433 c = *(char *) addr;
434 if (!c)
435 result = size;
436 if (size < PAGE_SIZE)
437 buffer[size++] = c;
438 else
439 return result;
440 addr++;
441 start++;
442 if (!c && start >= end)
443 return result;
444 } while (addr & ~PAGE_MASK);
446 return result;
449 static int get_env(int pid, char * buffer)
451 struct task_struct *p;
453 read_lock(&tasklist_lock);
454 p = find_task_by_pid(pid);
455 read_unlock(&tasklist_lock); /* FIXME!! This should be done after the last use */
457 if (!p || !p->mm)
458 return 0;
459 return get_array(p, p->mm->env_start, p->mm->env_end, buffer);
462 static int get_arg(int pid, char * buffer)
464 struct task_struct *p;
466 read_lock(&tasklist_lock);
467 p = find_task_by_pid(pid);
468 read_unlock(&tasklist_lock); /* FIXME!! This should be done after the last use */
469 if (!p || !p->mm)
470 return 0;
471 return get_array(p, p->mm->arg_start, p->mm->arg_end, buffer);
475 * These bracket the sleeping functions..
477 extern void scheduling_functions_start_here(void);
478 extern void scheduling_functions_end_here(void);
479 #define first_sched ((unsigned long) scheduling_functions_start_here)
480 #define last_sched ((unsigned long) scheduling_functions_end_here)
482 static unsigned long get_wchan(struct task_struct *p)
484 if (!p || p == current || p->state == TASK_RUNNING)
485 return 0;
486 #if defined(__i386__)
488 unsigned long ebp, esp, eip;
489 unsigned long stack_page;
490 int count = 0;
492 stack_page = (unsigned long)p;
493 esp = p->tss.esp;
494 if (!stack_page || esp < stack_page || esp >= 8188+stack_page)
495 return 0;
496 /* include/asm-i386/system.h:switch_to() pushes ebp last. */
497 ebp = *(unsigned long *) esp;
498 do {
499 if (ebp < stack_page || ebp >= 8188+stack_page)
500 return 0;
501 eip = *(unsigned long *) (ebp+4);
502 if (eip < first_sched || eip >= last_sched)
503 return eip;
504 ebp = *(unsigned long *) ebp;
505 } while (count++ < 16);
507 #elif defined(__alpha__)
509 * This one depends on the frame size of schedule(). Do a
510 * "disass schedule" in gdb to find the frame size. Also, the
511 * code assumes that sleep_on() follows immediately after
512 * interruptible_sleep_on() and that add_timer() follows
513 * immediately after interruptible_sleep(). Ugly, isn't it?
514 * Maybe adding a wchan field to task_struct would be better,
515 * after all...
518 unsigned long schedule_frame;
519 unsigned long pc;
521 pc = thread_saved_pc(&p->tss);
522 if (pc >= first_sched && pc < last_sched) {
523 schedule_frame = ((unsigned long *)p->tss.ksp)[6];
524 return ((unsigned long *)schedule_frame)[12];
526 return pc;
528 #elif defined(__mc68000__)
530 unsigned long fp, pc;
531 unsigned long stack_page;
532 int count = 0;
533 extern int sys_pause (void);
535 stack_page = p->kernel_stack_page;
536 if (!stack_page)
537 return 0;
538 fp = ((struct switch_stack *)p->tss.ksp)->a6;
539 do {
540 if (fp < stack_page || fp >= 4088+stack_page)
541 return 0;
542 pc = ((unsigned long *)fp)[1];
543 /* FIXME: This depends on the order of these functions. */
544 if (pc < first_sched || pc >= last_sched)
545 return pc;
546 fp = *(unsigned long *) fp;
547 } while (count++ < 16);
549 #elif defined(__powerpc__)
550 return (p->tss.wchan);
551 #elif defined (CONFIG_ARM)
553 unsigned long fp, lr;
554 unsigned long stack_page;
555 int count = 0;
557 stack_page = 4096 + (unsigned long)p;
558 fp = get_css_fp (&p->tss);
559 do {
560 if (fp < stack_page || fp > 4092+stack_page)
561 return 0;
562 lr = pc_pointer (((unsigned long *)fp)[-1]);
563 if (lr < first_sched || lr > last_sched)
564 return lr;
565 fp = *(unsigned long *) (fp - 12);
566 } while (count ++ < 16);
568 #endif
569 return 0;
572 #if defined(__i386__)
573 # define KSTK_EIP(tsk) (((unsigned long *)(4096+(unsigned long)(tsk)))[1019])
574 # define KSTK_ESP(tsk) (((unsigned long *)(4096+(unsigned long)(tsk)))[1022])
575 #elif defined(__alpha__)
577 * See arch/alpha/kernel/ptrace.c for details.
579 # define PT_REG(reg) (PAGE_SIZE - sizeof(struct pt_regs) \
580 + (long)&((struct pt_regs *)0)->reg)
581 # define KSTK_EIP(tsk) \
582 (*(unsigned long *)(PT_REG(pc) + PAGE_SIZE + (unsigned long)(tsk)))
583 # define KSTK_ESP(tsk) ((tsk) == current ? rdusp() : (tsk)->tss.usp)
584 #elif defined(CONFIG_ARM)
585 # define KSTK_EIP(tsk) (((unsigned long *)(4096+(unsigned long)(tsk)))[1022])
586 # define KSTK_ESP(tsk) (((unsigned long *)(4096+(unsigned long)(tsk)))[1020])
587 #elif defined(__mc68000__)
588 #define KSTK_EIP(tsk) \
589 ({ \
590 unsigned long eip = 0; \
591 if ((tsk)->tss.esp0 > PAGE_SIZE && \
592 MAP_NR((tsk)->tss.esp0) < max_mapnr) \
593 eip = ((struct pt_regs *) (tsk)->tss.esp0)->pc; \
594 eip; })
595 #define KSTK_ESP(tsk) ((tsk) == current ? rdusp() : (tsk)->tss.usp)
596 #elif defined(__powerpc__)
597 #define KSTK_EIP(tsk) ((tsk)->tss.regs->nip)
598 #define KSTK_ESP(tsk) ((tsk)->tss.regs->gpr[1])
599 #elif defined (__sparc_v9__)
600 # define KSTK_EIP(tsk) ((tsk)->tss.kregs->tpc)
601 # define KSTK_ESP(tsk) ((tsk)->tss.kregs->u_regs[UREG_FP])
602 #elif defined(__sparc__)
603 # define KSTK_EIP(tsk) ((tsk)->tss.kregs->pc)
604 # define KSTK_ESP(tsk) ((tsk)->tss.kregs->u_regs[UREG_FP])
605 #endif
607 /* Gcc optimizes away "strlen(x)" for constant x */
608 #define ADDBUF(buffer, string) \
609 do { memcpy(buffer, string, strlen(string)); \
610 buffer += strlen(string); } while (0)
612 static inline char * task_name(struct task_struct *p, char * buf)
614 int i;
615 char * name;
617 ADDBUF(buf, "Name:\t");
618 name = p->comm;
619 i = sizeof(p->comm);
620 do {
621 unsigned char c = *name;
622 name++;
623 i--;
624 *buf = c;
625 if (!c)
626 break;
627 if (c == '\\') {
628 buf[1] = c;
629 buf += 2;
630 continue;
632 if (c == '\n') {
633 buf[0] = '\\';
634 buf[1] = 'n';
635 buf += 2;
636 continue;
638 buf++;
639 } while (i);
640 *buf = '\n';
641 return buf+1;
645 * The task state array is a strange "bitmap" of
646 * reasons to sleep. Thus "running" is zero, and
647 * you can test for combinations of others with
648 * simple bit tests.
650 static const char *task_state_array[] = {
651 "R (running)", /* 0 */
652 "S (sleeping)", /* 1 */
653 "D (disk sleep)", /* 2 */
654 "Z (zombie)", /* 4 */
655 "T (stopped)", /* 8 */
656 "W (paging)" /* 16 */
659 static inline const char * get_task_state(struct task_struct *tsk)
661 unsigned int state = tsk->state & (TASK_RUNNING |
662 TASK_INTERRUPTIBLE |
663 TASK_UNINTERRUPTIBLE |
664 TASK_ZOMBIE |
665 TASK_STOPPED |
666 TASK_SWAPPING);
667 const char **p = &task_state_array[0];
669 while (state) {
670 p++;
671 state >>= 1;
673 return *p;
676 static inline char * task_state(struct task_struct *p, char *buffer)
678 buffer += sprintf(buffer,
679 "State:\t%s\n"
680 "Pid:\t%d\n"
681 "PPid:\t%d\n"
682 "Uid:\t%d\t%d\t%d\t%d\n"
683 "Gid:\t%d\t%d\t%d\t%d\n",
684 get_task_state(p),
685 p->pid, p->p_pptr->pid,
686 p->uid, p->euid, p->suid, p->fsuid,
687 p->gid, p->egid, p->sgid, p->fsgid);
688 return buffer;
691 static inline char * task_mem(struct task_struct *p, char *buffer)
693 struct mm_struct * mm = p->mm;
695 if (mm && mm != &init_mm) {
696 struct vm_area_struct * vma = mm->mmap;
697 unsigned long data = 0, stack = 0;
698 unsigned long exec = 0, lib = 0;
700 for (vma = mm->mmap; vma; vma = vma->vm_next) {
701 unsigned long len = (vma->vm_end - vma->vm_start) >> 10;
702 if (!vma->vm_file) {
703 data += len;
704 if (vma->vm_flags & VM_GROWSDOWN)
705 stack += len;
706 continue;
708 if (vma->vm_flags & VM_WRITE)
709 continue;
710 if (vma->vm_flags & VM_EXEC) {
711 exec += len;
712 if (vma->vm_flags & VM_EXECUTABLE)
713 continue;
714 lib += len;
717 buffer += sprintf(buffer,
718 "VmSize:\t%8lu kB\n"
719 "VmLck:\t%8lu kB\n"
720 "VmRSS:\t%8lu kB\n"
721 "VmData:\t%8lu kB\n"
722 "VmStk:\t%8lu kB\n"
723 "VmExe:\t%8lu kB\n"
724 "VmLib:\t%8lu kB\n",
725 mm->total_vm << (PAGE_SHIFT-10),
726 mm->locked_vm << (PAGE_SHIFT-10),
727 mm->rss << (PAGE_SHIFT-10),
728 data - stack, stack,
729 exec - lib, lib);
731 return buffer;
734 static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
735 sigset_t *catch)
737 struct k_sigaction *k;
738 int i;
740 sigemptyset(ign);
741 sigemptyset(catch);
743 if (p->sig) {
744 k = p->sig->action;
745 for (i = 1; i <= _NSIG; ++i, ++k) {
746 if (k->sa.sa_handler == SIG_IGN)
747 sigaddset(ign, i);
748 else if (k->sa.sa_handler != SIG_DFL)
749 sigaddset(catch, i);
754 static inline char * task_sig(struct task_struct *p, char *buffer)
756 sigset_t ign, catch;
758 buffer += sprintf(buffer, "SigPnd:\t");
759 buffer = render_sigset_t(&p->signal, buffer);
760 *buffer++ = '\n';
761 buffer += sprintf(buffer, "SigBlk:\t");
762 buffer = render_sigset_t(&p->blocked, buffer);
763 *buffer++ = '\n';
765 collect_sigign_sigcatch(p, &ign, &catch);
766 buffer += sprintf(buffer, "SigIgn:\t");
767 buffer = render_sigset_t(&ign, buffer);
768 *buffer++ = '\n';
769 buffer += sprintf(buffer, "SigCgt:\t"); /* Linux 2.0 uses "SigCgt" */
770 buffer = render_sigset_t(&catch, buffer);
771 *buffer++ = '\n';
773 return buffer;
776 extern inline char *task_cap(struct task_struct *p, char *buffer)
778 return buffer + sprintf(buffer, "CapInh:\t%016x\n"
779 "CapPrm:\t%016x\n"
780 "CapEff:\t%016x\n",
781 p->cap_inheritable.cap,
782 p->cap_permitted.cap,
783 p->cap_effective.cap);
787 static int get_status(int pid, char * buffer)
789 char * orig = buffer;
790 struct task_struct *tsk;
792 read_lock(&tasklist_lock);
793 tsk = find_task_by_pid(pid);
794 read_unlock(&tasklist_lock); /* FIXME!! This should be done after the last use */
795 if (!tsk)
796 return 0;
797 buffer = task_name(tsk, buffer);
798 buffer = task_state(tsk, buffer);
799 buffer = task_mem(tsk, buffer);
800 buffer = task_sig(tsk, buffer);
801 buffer = task_cap(tsk, buffer);
802 return buffer - orig;
805 static int get_stat(int pid, char * buffer)
807 struct task_struct *tsk;
808 unsigned long vsize, eip, esp, wchan;
809 long priority, nice;
810 int tty_pgrp;
811 sigset_t sigign, sigcatch;
812 char state;
814 read_lock(&tasklist_lock);
815 tsk = find_task_by_pid(pid);
816 read_unlock(&tasklist_lock); /* FIXME!! This should be done after the last use */
817 if (!tsk)
818 return 0;
819 state = *get_task_state(tsk);
820 vsize = eip = esp = 0;
821 if (tsk->mm && tsk->mm != &init_mm) {
822 struct vm_area_struct *vma = tsk->mm->mmap;
823 while (vma) {
824 vsize += vma->vm_end - vma->vm_start;
825 vma = vma->vm_next;
827 eip = KSTK_EIP(tsk);
828 esp = KSTK_ESP(tsk);
831 wchan = get_wchan(tsk);
833 collect_sigign_sigcatch(tsk, &sigign, &sigcatch);
835 if (tsk->tty)
836 tty_pgrp = tsk->tty->pgrp;
837 else
838 tty_pgrp = -1;
840 /* scale priority and nice values from timeslices to -20..20 */
841 /* to make it look like a "normal" Unix priority/nice value */
842 priority = tsk->counter;
843 priority = 20 - (priority * 10 + DEF_PRIORITY / 2) / DEF_PRIORITY;
844 nice = tsk->priority;
845 nice = 20 - (nice * 20 + DEF_PRIORITY / 2) / DEF_PRIORITY;
847 return sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
848 %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %lu %lu %ld %lu %lu %lu %lu %lu \
849 %lu %lu %lu %lu %lu %lu %lu %lu\n",
850 pid,
851 tsk->comm,
852 state,
853 tsk->p_pptr->pid,
854 tsk->pgrp,
855 tsk->session,
856 tsk->tty ? kdev_t_to_nr(tsk->tty->device) : 0,
857 tty_pgrp,
858 tsk->flags,
859 tsk->min_flt,
860 tsk->cmin_flt,
861 tsk->maj_flt,
862 tsk->cmaj_flt,
863 tsk->times.tms_utime,
864 tsk->times.tms_stime,
865 tsk->times.tms_cutime,
866 tsk->times.tms_cstime,
867 priority,
868 nice,
869 tsk->timeout,
870 tsk->it_real_value,
871 tsk->start_time,
872 vsize,
873 tsk->mm ? tsk->mm->rss : 0, /* you might want to shift this left 3 */
874 tsk->rlim ? tsk->rlim[RLIMIT_RSS].rlim_cur : 0,
875 tsk->mm ? tsk->mm->start_code : 0,
876 tsk->mm ? tsk->mm->end_code : 0,
877 tsk->mm ? tsk->mm->start_stack : 0,
878 esp,
879 eip,
880 /* The signal information here is obsolete.
881 * It must be decimal for Linux 2.0 compatibility.
882 * Use /proc/#/status for real-time signals.
884 tsk->signal .sig[0] & 0x7fffffffUL,
885 tsk->blocked.sig[0] & 0x7fffffffUL,
886 sigign .sig[0] & 0x7fffffffUL,
887 sigcatch .sig[0] & 0x7fffffffUL,
888 wchan,
889 tsk->nswap,
890 tsk->cnswap);
893 static inline void statm_pte_range(pmd_t * pmd, unsigned long address, unsigned long size,
894 int * pages, int * shared, int * dirty, int * total)
896 pte_t * pte;
897 unsigned long end;
899 if (pmd_none(*pmd))
900 return;
901 if (pmd_bad(*pmd)) {
902 printk("statm_pte_range: bad pmd (%08lx)\n", pmd_val(*pmd));
903 pmd_clear(pmd);
904 return;
906 pte = pte_offset(pmd, address);
907 address &= ~PMD_MASK;
908 end = address + size;
909 if (end > PMD_SIZE)
910 end = PMD_SIZE;
911 do {
912 pte_t page = *pte;
914 address += PAGE_SIZE;
915 pte++;
916 if (pte_none(page))
917 continue;
918 ++*total;
919 if (!pte_present(page))
920 continue;
921 ++*pages;
922 if (pte_dirty(page))
923 ++*dirty;
924 if (MAP_NR(pte_page(page)) >= max_mapnr)
925 continue;
926 if (atomic_read(&mem_map[MAP_NR(pte_page(page))].count) > 1)
927 ++*shared;
928 } while (address < end);
931 static inline void statm_pmd_range(pgd_t * pgd, unsigned long address, unsigned long size,
932 int * pages, int * shared, int * dirty, int * total)
934 pmd_t * pmd;
935 unsigned long end;
937 if (pgd_none(*pgd))
938 return;
939 if (pgd_bad(*pgd)) {
940 printk("statm_pmd_range: bad pgd (%08lx)\n", pgd_val(*pgd));
941 pgd_clear(pgd);
942 return;
944 pmd = pmd_offset(pgd, address);
945 address &= ~PGDIR_MASK;
946 end = address + size;
947 if (end > PGDIR_SIZE)
948 end = PGDIR_SIZE;
949 do {
950 statm_pte_range(pmd, address, end - address, pages, shared, dirty, total);
951 address = (address + PMD_SIZE) & PMD_MASK;
952 pmd++;
953 } while (address < end);
956 static void statm_pgd_range(pgd_t * pgd, unsigned long address, unsigned long end,
957 int * pages, int * shared, int * dirty, int * total)
959 while (address < end) {
960 statm_pmd_range(pgd, address, end - address, pages, shared, dirty, total);
961 address = (address + PGDIR_SIZE) & PGDIR_MASK;
962 pgd++;
966 static int get_statm(int pid, char * buffer)
968 struct task_struct *tsk;
969 int size=0, resident=0, share=0, trs=0, lrs=0, drs=0, dt=0;
971 read_lock(&tasklist_lock);
972 tsk = find_task_by_pid(pid);
973 read_unlock(&tasklist_lock); /* FIXME!! This should be done after the last use */
974 if (!tsk)
975 return 0;
976 if (tsk->mm && tsk->mm != &init_mm) {
977 struct vm_area_struct * vma = tsk->mm->mmap;
979 while (vma) {
980 pgd_t *pgd = pgd_offset(tsk->mm, vma->vm_start);
981 int pages = 0, shared = 0, dirty = 0, total = 0;
983 statm_pgd_range(pgd, vma->vm_start, vma->vm_end, &pages, &shared, &dirty, &total);
984 resident += pages;
985 share += shared;
986 dt += dirty;
987 size += total;
988 if (vma->vm_flags & VM_EXECUTABLE)
989 trs += pages; /* text */
990 else if (vma->vm_flags & VM_GROWSDOWN)
991 drs += pages; /* stack */
992 else if (vma->vm_end > 0x60000000)
993 lrs += pages; /* library */
994 else
995 drs += pages;
996 vma = vma->vm_next;
999 return sprintf(buffer,"%d %d %d %d %d %d %d\n",
1000 size, resident, share, trs, lrs, drs, dt);
1004 * The way we support synthetic files > 4K
1005 * - without storing their contents in some buffer and
1006 * - without walking through the entire synthetic file until we reach the
1007 * position of the requested data
1008 * is to cleverly encode the current position in the file's f_pos field.
1009 * There is no requirement that a read() call which returns `count' bytes
1010 * of data increases f_pos by exactly `count'.
1012 * This idea is Linus' one. Bruno implemented it.
1016 * For the /proc/<pid>/maps file, we use fixed length records, each containing
1017 * a single line.
1019 #define MAPS_LINE_LENGTH 4096
1020 #define MAPS_LINE_SHIFT 12
1022 * f_pos = (number of the vma in the task->mm->mmap list) * MAPS_LINE_LENGTH
1023 * + (index into the line)
1025 /* for systems with sizeof(void*) == 4: */
1026 #define MAPS_LINE_FORMAT4 "%08lx-%08lx %s %08lx %s %lu"
1027 #define MAPS_LINE_MAX4 49 /* sum of 8 1 8 1 4 1 8 1 5 1 10 1 */
1029 /* for systems with sizeof(void*) == 8: */
1030 #define MAPS_LINE_FORMAT8 "%016lx-%016lx %s %016lx %s %lu"
1031 #define MAPS_LINE_MAX8 73 /* sum of 16 1 16 1 4 1 16 1 5 1 10 1 */
1033 #define MAPS_LINE_MAX MAPS_LINE_MAX8
1036 static ssize_t read_maps (int pid, struct file * file, char * buf,
1037 size_t count, loff_t *ppos)
1039 struct task_struct *p;
1040 struct vm_area_struct * map, * next;
1041 char * destptr = buf, * buffer;
1042 loff_t lineno;
1043 ssize_t column, i;
1044 int volatile_task;
1045 long retval;
1048 * We might sleep getting the page, so get it first.
1050 retval = -ENOMEM;
1051 buffer = (char*)__get_free_page(GFP_KERNEL);
1052 if (!buffer)
1053 goto out;
1055 retval = -EINVAL;
1056 read_lock(&tasklist_lock);
1057 p = find_task_by_pid(pid);
1058 read_unlock(&tasklist_lock); /* FIXME!! This should be done after the last use */
1059 if (!p)
1060 goto freepage_out;
1062 if (!p->mm || p->mm == &init_mm || count == 0)
1063 goto getlen_out;
1065 /* Check whether the mmaps could change if we sleep */
1066 volatile_task = (p != current || atomic_read(&p->mm->count) > 1);
1068 /* decode f_pos */
1069 lineno = *ppos >> MAPS_LINE_SHIFT;
1070 column = *ppos & (MAPS_LINE_LENGTH-1);
1072 /* quickly go to line lineno */
1073 for (map = p->mm->mmap, i = 0; map && (i < lineno); map = map->vm_next, i++)
1074 continue;
1076 for ( ; map ; map = next ) {
1077 /* produce the next line */
1078 char *line;
1079 char str[5], *cp = str;
1080 int flags;
1081 kdev_t dev;
1082 unsigned long ino;
1083 int maxlen = (sizeof(void*) == 4) ?
1084 MAPS_LINE_MAX4 : MAPS_LINE_MAX8;
1085 int len;
1088 * Get the next vma now (but it won't be used if we sleep).
1090 next = map->vm_next;
1091 flags = map->vm_flags;
1093 *cp++ = flags & VM_READ ? 'r' : '-';
1094 *cp++ = flags & VM_WRITE ? 'w' : '-';
1095 *cp++ = flags & VM_EXEC ? 'x' : '-';
1096 *cp++ = flags & VM_MAYSHARE ? 's' : 'p';
1097 *cp++ = 0;
1099 dev = 0;
1100 ino = 0;
1101 if (map->vm_file != NULL) {
1102 dev = map->vm_file->f_dentry->d_inode->i_dev;
1103 ino = map->vm_file->f_dentry->d_inode->i_ino;
1104 line = d_path(map->vm_file->f_dentry, buffer, PAGE_SIZE);
1105 buffer[PAGE_SIZE-1] = '\n';
1106 line -= maxlen;
1107 if(line < buffer)
1108 line = buffer;
1109 } else
1110 line = buffer;
1112 len = sprintf(line,
1113 sizeof(void*) == 4 ? MAPS_LINE_FORMAT4 : MAPS_LINE_FORMAT8,
1114 map->vm_start, map->vm_end, str, map->vm_offset,
1115 kdevname(dev), ino);
1117 if(map->vm_file) {
1118 for(i = len; i < maxlen; i++)
1119 line[i] = ' ';
1120 len = buffer + PAGE_SIZE - line;
1121 } else
1122 line[len++] = '\n';
1123 if (column >= len) {
1124 column = 0; /* continue with next line at column 0 */
1125 lineno++;
1126 continue; /* we haven't slept */
1129 i = len-column;
1130 if (i > count)
1131 i = count;
1132 copy_to_user(destptr, line+column, i); /* may have slept */
1133 destptr += i;
1134 count -= i;
1135 column += i;
1136 if (column >= len) {
1137 column = 0; /* next time: next line at column 0 */
1138 lineno++;
1141 /* done? */
1142 if (count == 0)
1143 break;
1145 /* By writing to user space, we might have slept.
1146 * Stop the loop, to avoid a race condition.
1148 if (volatile_task)
1149 break;
1152 /* encode f_pos */
1153 *ppos = (lineno << MAPS_LINE_SHIFT) + column;
1155 getlen_out:
1156 retval = destptr - buf;
1158 freepage_out:
1159 free_page((unsigned long)buffer);
1160 out:
1161 return retval;
1164 #ifdef __SMP__
1165 static int get_pidcpu(int pid, char * buffer)
1167 struct task_struct * tsk = current ;
1168 int i, len;
1170 read_lock(&tasklist_lock);
1171 if (pid != tsk->pid)
1172 tsk = find_task_by_pid(pid);
1173 read_unlock(&tasklist_lock); /* FIXME!! This should be done after the last use */
1175 if (tsk == NULL)
1176 return 0;
1178 len = sprintf(buffer,
1179 "cpu %lu %lu\n",
1180 tsk->times.tms_utime,
1181 tsk->times.tms_stime);
1183 for (i = 0 ; i < smp_num_cpus; i++)
1184 len += sprintf(buffer + len, "cpu%d %lu %lu\n",
1186 tsk->per_cpu_utime[cpu_logical_map(i)],
1187 tsk->per_cpu_stime[cpu_logical_map(i)]);
1189 return len;
1191 #endif
1193 #ifdef CONFIG_MODULES
1194 extern int get_module_list(char *);
1195 extern int get_ksyms_list(char *, char **, off_t, int);
1196 #endif
1197 extern int get_device_list(char *);
1198 extern int get_partition_list(char *);
1199 extern int get_filesystem_list(char *);
1200 extern int get_filesystem_info( char * );
1201 extern int get_irq_list(char *);
1202 extern int get_dma_list(char *);
1203 extern int get_cpuinfo(char *);
1204 extern int get_pci_list(char *);
1205 extern int get_md_status (char *);
1206 extern int get_rtc_status (char *);
1207 extern int get_locks_status (char *, char **, off_t, int);
1208 extern int get_swaparea_info (char *);
1209 extern int get_hardware_list(char *);
1210 extern int get_stram_list(char *);
1212 static long get_root_array(char * page, int type, char **start,
1213 off_t offset, unsigned long length)
1215 switch (type) {
1216 case PROC_LOADAVG:
1217 return get_loadavg(page);
1219 case PROC_UPTIME:
1220 return get_uptime(page);
1222 case PROC_MEMINFO:
1223 return get_meminfo(page);
1225 #ifdef CONFIG_PCI_OLD_PROC
1226 case PROC_PCI:
1227 return get_pci_list(page);
1228 #endif
1230 case PROC_CPUINFO:
1231 return get_cpuinfo(page);
1233 case PROC_VERSION:
1234 return get_version(page);
1236 #ifdef CONFIG_DEBUG_MALLOC
1237 case PROC_MALLOC:
1238 return get_malloc(page);
1239 #endif
1241 #ifdef CONFIG_MODULES
1242 case PROC_MODULES:
1243 return get_module_list(page);
1245 case PROC_KSYMS:
1246 return get_ksyms_list(page, start, offset, length);
1247 #endif
1249 case PROC_STAT:
1250 return get_kstat(page);
1252 case PROC_SLABINFO:
1253 return get_slabinfo(page);
1255 case PROC_DEVICES:
1256 return get_device_list(page);
1258 case PROC_PARTITIONS:
1259 return get_partition_list(page);
1261 case PROC_INTERRUPTS:
1262 return get_irq_list(page);
1264 case PROC_FILESYSTEMS:
1265 return get_filesystem_list(page);
1267 case PROC_DMA:
1268 return get_dma_list(page);
1270 case PROC_IOPORTS:
1271 return get_ioport_list(page);
1272 #ifdef CONFIG_BLK_DEV_MD
1273 case PROC_MD:
1274 return get_md_status(page);
1275 #endif
1276 case PROC_CMDLINE:
1277 return get_cmdline(page);
1279 case PROC_MTAB:
1280 return get_filesystem_info( page );
1282 case PROC_SWAP:
1283 return get_swaparea_info(page);
1284 #ifdef CONFIG_RTC
1285 case PROC_RTC:
1286 return get_rtc_status(page);
1287 #endif
1288 case PROC_LOCKS:
1289 return get_locks_status(page, start, offset, length);
1290 #ifdef CONFIG_PROC_HARDWARE
1291 case PROC_HARDWARE:
1292 return get_hardware_list(page);
1293 #endif
1294 #ifdef CONFIG_STRAM_PROC
1295 case PROC_STRAM:
1296 return get_stram_list(page);
1297 #endif
1299 return -EBADF;
1302 static int get_process_array(char * page, int pid, int type)
1304 switch (type) {
1305 case PROC_PID_STATUS:
1306 return get_status(pid, page);
1307 case PROC_PID_ENVIRON:
1308 return get_env(pid, page);
1309 case PROC_PID_CMDLINE:
1310 return get_arg(pid, page);
1311 case PROC_PID_STAT:
1312 return get_stat(pid, page);
1313 case PROC_PID_STATM:
1314 return get_statm(pid, page);
1315 #ifdef __SMP__
1316 case PROC_PID_CPU:
1317 return get_pidcpu(pid, page);
1318 #endif
1320 return -EBADF;
1324 static inline int fill_array(char * page, int pid, int type, char **start, off_t offset, int length)
1326 if (pid)
1327 return get_process_array(page, pid, type);
1328 return get_root_array(page, type, start, offset, length);
1331 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
1333 static ssize_t array_read(struct file * file, char * buf,
1334 size_t count, loff_t *ppos)
1336 struct inode * inode = file->f_dentry->d_inode;
1337 unsigned long page;
1338 char *start;
1339 ssize_t length;
1340 ssize_t end;
1341 unsigned int type, pid;
1342 struct proc_dir_entry *dp;
1344 if (count > PROC_BLOCK_SIZE)
1345 count = PROC_BLOCK_SIZE;
1346 if (!(page = __get_free_page(GFP_KERNEL)))
1347 return -ENOMEM;
1348 type = inode->i_ino;
1349 pid = type >> 16;
1350 type &= 0x0000ffff;
1351 start = NULL;
1352 dp = (struct proc_dir_entry *) inode->u.generic_ip;
1353 if (dp->get_info)
1354 length = dp->get_info((char *)page, &start, *ppos,
1355 count, 0);
1356 else
1357 length = fill_array((char *) page, pid, type,
1358 &start, *ppos, count);
1359 if (length < 0) {
1360 free_page(page);
1361 return length;
1363 if (start != NULL) {
1364 /* We have had block-adjusting processing! */
1365 copy_to_user(buf, start, length);
1366 *ppos += length;
1367 count = length;
1368 } else {
1369 /* Static 4kB (or whatever) block capacity */
1370 if (*ppos >= length) {
1371 free_page(page);
1372 return 0;
1374 if (count + *ppos > length)
1375 count = length - *ppos;
1376 end = count + *ppos;
1377 copy_to_user(buf, (char *) page + *ppos, count);
1378 *ppos = end;
1380 free_page(page);
1381 return count;
1384 static struct file_operations proc_array_operations = {
1385 NULL, /* array_lseek */
1386 array_read,
1387 NULL, /* array_write */
1388 NULL, /* array_readdir */
1389 NULL, /* array_poll */
1390 NULL, /* array_ioctl */
1391 NULL, /* mmap */
1392 NULL, /* no special open code */
1393 NULL, /* flush */
1394 NULL, /* no special release code */
1395 NULL /* can't fsync */
1398 struct inode_operations proc_array_inode_operations = {
1399 &proc_array_operations, /* default base directory file-ops */
1400 NULL, /* create */
1401 NULL, /* lookup */
1402 NULL, /* link */
1403 NULL, /* unlink */
1404 NULL, /* symlink */
1405 NULL, /* mkdir */
1406 NULL, /* rmdir */
1407 NULL, /* mknod */
1408 NULL, /* rename */
1409 NULL, /* readlink */
1410 NULL, /* follow_link */
1411 NULL, /* readpage */
1412 NULL, /* writepage */
1413 NULL, /* bmap */
1414 NULL, /* truncate */
1415 NULL /* permission */
1418 static ssize_t arraylong_read(struct file * file, char * buf,
1419 size_t count, loff_t *ppos)
1421 struct inode * inode = file->f_dentry->d_inode;
1422 unsigned int pid = inode->i_ino >> 16;
1423 unsigned int type = inode->i_ino & 0x0000ffff;
1425 switch (type) {
1426 case PROC_PID_MAPS:
1427 return read_maps(pid, file, buf, count, ppos);
1429 return -EINVAL;
1432 static struct file_operations proc_arraylong_operations = {
1433 NULL, /* array_lseek */
1434 arraylong_read,
1435 NULL, /* array_write */
1436 NULL, /* array_readdir */
1437 NULL, /* array_poll */
1438 NULL, /* array_ioctl */
1439 NULL, /* mmap */
1440 NULL, /* no special open code */
1441 NULL, /* flush */
1442 NULL, /* no special release code */
1443 NULL /* can't fsync */
1446 struct inode_operations proc_arraylong_inode_operations = {
1447 &proc_arraylong_operations, /* default base directory file-ops */
1448 NULL, /* create */
1449 NULL, /* lookup */
1450 NULL, /* link */
1451 NULL, /* unlink */
1452 NULL, /* symlink */
1453 NULL, /* mkdir */
1454 NULL, /* rmdir */
1455 NULL, /* mknod */
1456 NULL, /* rename */
1457 NULL, /* readlink */
1458 NULL, /* follow_link */
1459 NULL, /* readpage */
1460 NULL, /* writepage */
1461 NULL, /* bmap */
1462 NULL, /* truncate */
1463 NULL /* permission */