root/fs/proc/array.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. read_core
  2. read_profile
  3. write_profile
  4. get_loadavg
  5. get_kstat
  6. get_uptime
  7. get_meminfo
  8. get_version
  9. get_task
  10. get_phys_addr
  11. get_array
  12. get_env
  13. get_arg
  14. get_wchan
  15. get_stat
  16. statm_pte_range
  17. statm_pmd_range
  18. statm_pgd_range
  19. get_statm
  20. read_maps
  21. get_root_array
  22. get_process_array
  23. fill_array
  24. array_read
  25. arraylong_read

   1 /*
   2  *  linux/fs/proc/array.c
   3  *
   4  *  Copyright (C) 1992  by Linus Torvalds
   5  *  based on ideas by Darren Senn
   6  *
   7  * Fixes:
   8  * Michael. K. Johnson: stat,statm extensions.
   9  *                      <johnsonm@stolaf.edu>
  10  *
  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>
  16  *
  17  * Danny ter Haar    :  Some minor additions for cpuinfo
  18  *                      <danny@ow.nl>
  19  *
  20  * Alessandro Rubini :  profile extension.
  21  *                      <rubini@ipvvis.unipv.it>
  22  *
  23  * Jeff Tranter      :  added BogoMips field to cpuinfo
  24  *                      <Jeff_Tranter@Mitel.COM>
  25  *
  26  * Bruno Haible      :  remove 4K limit for the maps file
  27  * <haible@ma2s2.mathematik.uni-karlsruhe.de>
  28  */
  29 
  30 #include <linux/types.h>
  31 #include <linux/errno.h>
  32 #include <linux/sched.h>
  33 #include <linux/kernel.h>
  34 #include <linux/kernel_stat.h>
  35 #include <linux/tty.h>
  36 #include <linux/user.h>
  37 #include <linux/a.out.h>
  38 #include <linux/string.h>
  39 #include <linux/mman.h>
  40 #include <linux/proc_fs.h>
  41 #include <linux/ioport.h>
  42 #include <linux/config.h>
  43 #include <linux/mm.h>
  44 
  45 #include <asm/segment.h>
  46 #include <asm/pgtable.h>
  47 #include <asm/io.h>
  48 
  49 #define LOAD_INT(x) ((x) >> FSHIFT)
  50 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
  51 
  52 #ifdef CONFIG_DEBUG_MALLOC
  53 int get_malloc(char * buffer);
  54 #endif
  55 
  56 
  57 static int read_core(struct inode * inode, struct file * file,char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
  58 {
  59         unsigned long p = file->f_pos, memsize;
  60         int read;
  61         int count1;
  62         char * pnt;
  63         struct user dump;
  64 #ifdef __i386__
  65 #       define FIRST_MAPPED     PAGE_SIZE       /* we don't have page 0 mapped on x86.. */
  66 #else
  67 #       define FIRST_MAPPED     0
  68 #endif
  69 
  70         memset(&dump, 0, sizeof(struct user));
  71         dump.magic = CMAGIC;
  72         dump.u_dsize = MAP_NR(high_memory);
  73 #ifdef __alpha__
  74         dump.start_data = PAGE_OFFSET;
  75 #endif
  76 
  77         if (count < 0)
  78                 return -EINVAL;
  79         memsize = MAP_NR(high_memory + PAGE_SIZE) << PAGE_SHIFT;
  80         if (p >= memsize)
  81                 return 0;
  82         if (count > memsize - p)
  83                 count = memsize - p;
  84         read = 0;
  85 
  86         if (p < sizeof(struct user) && count > 0) {
  87                 count1 = count;
  88                 if (p + count1 > sizeof(struct user))
  89                         count1 = sizeof(struct user)-p;
  90                 pnt = (char *) &dump + p;
  91                 memcpy_tofs(buf,(void *) pnt, count1);
  92                 buf += count1;
  93                 p += count1;
  94                 count -= count1;
  95                 read += count1;
  96         }
  97 
  98         while (count > 0 && p < PAGE_SIZE + FIRST_MAPPED) {
  99                 put_user(0,buf);
 100                 buf++;
 101                 p++;
 102                 count--;
 103                 read++;
 104         }
 105         memcpy_tofs(buf, (void *) (PAGE_OFFSET + p - PAGE_SIZE), count);
 106         read += count;
 107         file->f_pos += read;
 108         return read;
 109 }
 110 
 111 static struct file_operations proc_kcore_operations = {
 112         NULL,           /* lseek */
 113         read_core,
 114 };
 115 
 116 struct inode_operations proc_kcore_inode_operations = {
 117         &proc_kcore_operations, 
 118 };
 119 
 120 
 121 extern unsigned long prof_len;
 122 extern unsigned long * prof_buffer;
 123 extern unsigned long prof_shift;
 124 /*
 125  * This function accesses profiling information. The returned data is
 126  * binary: the sampling step and the actual contents of the profile
 127  * buffer. Use of the program readprofile is recommended in order to
 128  * get meaningful info out of these data.
 129  */
 130 static int read_profile(struct inode *inode, struct file *file, char *buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 131 {
 132     unsigned long p = file->f_pos;
 133         int read;
 134         char * pnt;
 135         unsigned long sample_step = 1 << prof_shift;
 136 
 137         if (count < 0)
 138             return -EINVAL;
 139         if (p >= (prof_len+1)*sizeof(unsigned long))
 140             return 0;
 141         if (count > (prof_len+1)*sizeof(unsigned long) - p)
 142             count = (prof_len+1)*sizeof(unsigned long) - p;
 143     read = 0;
 144 
 145     while (p < sizeof(unsigned long) && count > 0) {
 146         put_user(*((char *)(&sample_step)+p),buf);
 147                 buf++; p++; count--; read++;
 148     }
 149     pnt = (char *)prof_buffer + p - sizeof(unsigned long);
 150         memcpy_tofs(buf,(void *)pnt,count);
 151         read += count;
 152         file->f_pos += read;
 153         return read;
 154 }
 155 
 156 /* Writing to /proc/profile resets the counters */
 157 static int write_profile(struct inode * inode, struct file * file, const char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 158 {
 159     int i=prof_len;
 160 
 161     while (i--)
 162             prof_buffer[i]=0UL;
 163     return count;
 164 }
 165 
 166 static struct file_operations proc_profile_operations = {
 167         NULL,           /* lseek */
 168         read_profile,
 169         write_profile,
 170 };
 171 
 172 struct inode_operations proc_profile_inode_operations = {
 173         &proc_profile_operations, 
 174 };
 175 
 176 
 177 static int get_loadavg(char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 178 {
 179         int a, b, c;
 180 
 181         a = avenrun[0] + (FIXED_1/200);
 182         b = avenrun[1] + (FIXED_1/200);
 183         c = avenrun[2] + (FIXED_1/200);
 184         return sprintf(buffer,"%d.%02d %d.%02d %d.%02d %d/%d\n",
 185                 LOAD_INT(a), LOAD_FRAC(a),
 186                 LOAD_INT(b), LOAD_FRAC(b),
 187                 LOAD_INT(c), LOAD_FRAC(c),
 188                 nr_running, nr_tasks);
 189 }
 190 
 191 static int get_kstat(char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 192 {
 193         int i, len;
 194         unsigned sum = 0;
 195 
 196         for (i = 0 ; i < NR_IRQS ; i++)
 197                 sum += kstat.interrupts[i];
 198         len = sprintf(buffer,
 199                 "cpu  %u %u %u %lu\n"
 200                 "disk %u %u %u %u\n"
 201                 "page %u %u\n"
 202                 "swap %u %u\n"
 203                 "intr %u",
 204                 kstat.cpu_user,
 205                 kstat.cpu_nice,
 206                 kstat.cpu_system,
 207                 jiffies - (kstat.cpu_user + kstat.cpu_nice + kstat.cpu_system),
 208                 kstat.dk_drive[0],
 209                 kstat.dk_drive[1],
 210                 kstat.dk_drive[2],
 211                 kstat.dk_drive[3],
 212                 kstat.pgpgin,
 213                 kstat.pgpgout,
 214                 kstat.pswpin,
 215                 kstat.pswpout,
 216                 sum);
 217         for (i = 0 ; i < NR_IRQS ; i++)
 218                 len += sprintf(buffer + len, " %u", kstat.interrupts[i]);
 219         len += sprintf(buffer + len,
 220                 "\nctxt %u\n"
 221                 "btime %lu\n",
 222                 kstat.context_swtch,
 223                 xtime.tv_sec - jiffies / HZ);
 224         return len;
 225 }
 226 
 227 
 228 static int get_uptime(char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 229 {
 230         unsigned long uptime;
 231         unsigned long idle;
 232 
 233         uptime = jiffies;
 234         idle = task[0]->utime + task[0]->stime;
 235 
 236         /* The formula for the fraction parts really is ((t * 100) / HZ) % 100, but
 237            that would overflow about every five days at HZ == 100.
 238            Therefore the identity a = (a / b) * b + a % b is used so that it is
 239            calculated as (((t / HZ) * 100) + ((t % HZ) * 100) / HZ) % 100.
 240            The part in front of the '+' always evaluates as 0 (mod 100). All divisions
 241            in the above formulas are truncating. For HZ being a power of 10, the
 242            calculations simplify to the version in the #else part (if the printf
 243            format is adapted to the same number of digits as zeroes in HZ.
 244          */
 245 #if HZ!=100
 246         return sprintf(buffer,"%lu.%02lu %lu.%02lu\n",
 247                 uptime / HZ,
 248                 (((uptime % HZ) * 100) / HZ) % 100,
 249                 idle / HZ,
 250                 (((idle % HZ) * 100) / HZ) % 100);
 251 #else
 252         return sprintf(buffer,"%lu.%02lu %lu.%02lu\n",
 253                 uptime / HZ,
 254                 uptime % HZ,
 255                 idle / HZ,
 256                 idle % HZ);
 257 #endif
 258 }
 259 
 260 static int get_meminfo(char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 261 {
 262         struct sysinfo i;
 263 
 264         si_meminfo(&i);
 265         si_swapinfo(&i);
 266         return sprintf(buffer, "        total:   used:    free:   shared:  buffers:\n"
 267                 "Mem:  %8lu %8lu %8lu %8lu %8lu\n"
 268                 "Swap: %8lu %8lu %8lu\n",
 269                 i.totalram, i.totalram-i.freeram, i.freeram, i.sharedram, i.bufferram,
 270                 i.totalswap, i.totalswap-i.freeswap, i.freeswap);
 271 }
 272 
 273 static int get_version(char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 274 {
 275         extern char *linux_banner;
 276 
 277         strcpy(buffer, linux_banner);
 278         return strlen(buffer);
 279 }
 280 
 281 static struct task_struct ** get_task(pid_t pid)
     /* [previous][next][first][last][top][bottom][index][help] */
 282 {
 283         struct task_struct ** p;
 284 
 285         p = task;
 286         while (++p < task+NR_TASKS) {
 287                 if (*p && (*p)->pid == pid)
 288                         return p;
 289         }
 290         return NULL;
 291 }
 292 
 293 static unsigned long get_phys_addr(struct task_struct * p, unsigned long ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 294 {
 295         pgd_t *page_dir;
 296         pmd_t *page_middle;
 297         pte_t pte;
 298 
 299         if (!p || !p->mm || ptr >= TASK_SIZE)
 300                 return 0;
 301         page_dir = pgd_offset(p->mm,ptr);
 302         if (pgd_none(*page_dir))
 303                 return 0;
 304         if (pgd_bad(*page_dir)) {
 305                 printk("bad page directory entry %08lx\n", pgd_val(*page_dir));
 306                 pgd_clear(page_dir);
 307                 return 0;
 308         }
 309         page_middle = pmd_offset(page_dir,ptr);
 310         if (pmd_none(*page_middle))
 311                 return 0;
 312         if (pmd_bad(*page_middle)) {
 313                 printk("bad page middle entry %08lx\n", pmd_val(*page_middle));
 314                 pmd_clear(page_middle);
 315                 return 0;
 316         }
 317         pte = *pte_offset(page_middle,ptr);
 318         if (!pte_present(pte))
 319                 return 0;
 320         return pte_page(pte) + (ptr & ~PAGE_MASK);
 321 }
 322 
 323 static int get_array(struct task_struct ** p, unsigned long start, unsigned long end, char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 324 {
 325         unsigned long addr;
 326         int size = 0, result = 0;
 327         char c;
 328 
 329         if (start >= end)
 330                 return result;
 331         for (;;) {
 332                 addr = get_phys_addr(*p, start);
 333                 if (!addr)
 334                         goto ready;
 335                 do {
 336                         c = *(char *) addr;
 337                         if (!c)
 338                                 result = size;
 339                         if (size < PAGE_SIZE)
 340                                 buffer[size++] = c;
 341                         else
 342                                 goto ready;
 343                         addr++;
 344                         start++;
 345                         if (!c && start >= end)
 346                                 goto ready;
 347                 } while (addr & ~PAGE_MASK);
 348         }
 349 ready:
 350         /* remove the trailing blanks, used to fill out argv,envp space */
 351         while (result>0 && buffer[result-1]==' ')
 352                 result--;
 353         return result;
 354 }
 355 
 356 static int get_env(int pid, char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 357 {
 358         struct task_struct ** p = get_task(pid);
 359 
 360         if (!p || !*p || !(*p)->mm)
 361                 return 0;
 362         return get_array(p, (*p)->mm->env_start, (*p)->mm->env_end, buffer);
 363 }
 364 
 365 static int get_arg(int pid, char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 366 {
 367         struct task_struct ** p = get_task(pid);
 368 
 369         if (!p || !*p || !(*p)->mm)
 370                 return 0;
 371         return get_array(p, (*p)->mm->arg_start, (*p)->mm->arg_end, buffer);
 372 }
 373 
 374 static unsigned long get_wchan(struct task_struct *p)
     /* [previous][next][first][last][top][bottom][index][help] */
 375 {
 376         if (!p || p == current || p->state == TASK_RUNNING)
 377                 return 0;
 378 #if defined(__i386__)
 379         {
 380                 unsigned long ebp, eip;
 381                 unsigned long stack_page;
 382                 int count = 0;
 383 
 384                 stack_page = p->kernel_stack_page;
 385                 if (!stack_page)
 386                         return 0;
 387                 ebp = p->tss.ebp;
 388                 do {
 389                         if (ebp < stack_page || ebp >= 4092+stack_page)
 390                                 return 0;
 391                         eip = *(unsigned long *) (ebp+4);
 392                         if ((void *)eip != sleep_on &&
 393                             (void *)eip != interruptible_sleep_on)
 394                                 return eip;
 395                         ebp = *(unsigned long *) ebp;
 396                 } while (count++ < 16);
 397         }
 398 #elif defined(__alpha__)
 399         /*
 400          * This one depends on the frame size of schedule().  Do a
 401          * "disass schedule" in gdb to find the frame size.  Also, the
 402          * code assumes that sleep_on() follows immediately after
 403          * interruptible_sleep_on() and that add_timer() follows
 404          * immediately after interruptible_sleep().  Ugly, isn't it?
 405          * Maybe adding a wchan field to task_struct would be better,
 406          * after all...
 407          */
 408         {
 409             unsigned long schedule_frame;
 410             unsigned long pc;
 411 
 412             pc = thread_saved_pc(&p->tss);
 413             if (pc >= (unsigned long) interruptible_sleep_on && pc < (unsigned long) add_timer) {
 414                 schedule_frame = ((unsigned long *)p->tss.ksp)[6];
 415                 return ((unsigned long *)schedule_frame)[12];
 416             }
 417             return pc;
 418         }
 419 #endif
 420         return 0;
 421 }
 422 
 423 #if defined(__i386__)
 424 # define KSTK_EIP(tsk)  (((unsigned long *)tsk->kernel_stack_page)[1019])
 425 # define KSTK_ESP(tsk)  (((unsigned long *)tsk->kernel_stack_page)[1022])
 426 #elif defined(__alpha__)
 427   /*
 428    * See arch/alpha/kernel/ptrace.c for details.
 429    */
 430 # define PT_REG(reg)            (PAGE_SIZE - sizeof(struct pt_regs)     \
 431                                  + (long)&((struct pt_regs *)0)->reg)
 432 # define KSTK_EIP(tsk)  (*(unsigned long *)(tsk->kernel_stack_page + PT_REG(pc)))
 433 # define KSTK_ESP(tsk)  ((tsk) == current ? rdusp() : (tsk)->tss.usp)
 434 #endif
 435 
 436 static int get_stat(int pid, char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 437 {
 438         struct task_struct ** p = get_task(pid), *tsk;
 439         unsigned long sigignore=0, sigcatch=0, wchan;
 440         unsigned long vsize, eip, esp;
 441         long priority, nice;
 442         int i,tty_pgrp;
 443         char state;
 444 
 445         if (!p || (tsk = *p) == NULL)
 446                 return 0;
 447         if (tsk->state < 0 || tsk->state > 5)
 448                 state = '.';
 449         else
 450                 state = "RSDZTD"[tsk->state];
 451         vsize = eip = esp = 0;
 452         if (tsk->mm) {
 453                 if (tsk->kernel_stack_page) {
 454                         eip = KSTK_EIP(tsk);
 455                         esp = KSTK_ESP(tsk);
 456                         vsize = (  (tsk->mm->end_code - tsk->mm->start_code)    /* text */
 457                                  + (tsk->mm->end_data - tsk->mm->start_data)    /* data */
 458                                  + (tsk->mm->brk - tsk->mm->start_brk));        /* bss + heap */
 459                         if (esp) {
 460                                 vsize += tsk->mm->start_stack - esp;            /* stack */
 461                         }
 462                 }
 463         }
 464         wchan = get_wchan(tsk);
 465         if (tsk->sig) {
 466                 unsigned long bit = 1;
 467                 for(i=0; i<32; ++i) {
 468                         switch((unsigned long) tsk->sig->action[i].sa_handler) {
 469                                 case 0:
 470                                         break;
 471                                 case 1:
 472                                         sigignore |= bit;
 473                                         break;
 474                                 default:
 475                                         sigcatch |= bit;
 476                         }
 477                         bit <<= 1;
 478                 }
 479         }
 480         if (tsk->tty)
 481                 tty_pgrp = tsk->tty->pgrp;
 482         else
 483                 tty_pgrp = -1;
 484 
 485         /* scale priority and nice values from timeslices to 0..40 */
 486         priority = tsk->counter;
 487         priority = (priority * 10 + 5) / DEF_PRIORITY;
 488         nice = tsk->priority;
 489         nice = (nice * 20 + 10) / DEF_PRIORITY;
 490         return sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
 491 %lu %lu %lu %ld %ld %ld %ld %ld %ld %lu %lu %ld %lu %lu %lu %lu %lu %lu %lu %lu %lu \
 492 %lu %lu %lu %lu\n",
 493                 pid,
 494                 tsk->comm,
 495                 state,
 496                 tsk->p_pptr->pid,
 497                 tsk->pgrp,
 498                 tsk->session,
 499                 tsk->tty ? kdev_t_to_nr(tsk->tty->device) : 0,
 500                 tty_pgrp,
 501                 tsk->flags,
 502                 tsk->min_flt,
 503                 tsk->cmin_flt,
 504                 tsk->maj_flt,
 505                 tsk->cmaj_flt,
 506                 tsk->utime,
 507                 tsk->stime,
 508                 tsk->cutime,
 509                 tsk->cstime,
 510                 priority,  /* this is the kernel priority ---
 511                                    subtract 20 in your user-level program. */
 512                 nice,      /* this is the nice value ---
 513                                    subtract 20 in your user-level program. */
 514                 tsk->timeout,
 515                 tsk->it_real_value,
 516                 tsk->start_time,
 517                 vsize,
 518                 tsk->mm ? tsk->mm->rss : 0, /* you might want to shift this left 3 */
 519                 tsk->rlim ? tsk->rlim[RLIMIT_RSS].rlim_cur : 0,
 520                 tsk->mm ? tsk->mm->start_code : 0,
 521                 tsk->mm ? tsk->mm->end_code : 0,
 522                 tsk->mm ? tsk->mm->start_stack : 0,
 523                 esp,
 524                 eip,
 525                 tsk->signal,
 526                 tsk->blocked,
 527                 sigignore,
 528                 sigcatch,
 529                 wchan);
 530 }
 531                 
 532 static inline void statm_pte_range(pmd_t * pmd, unsigned long address, unsigned long size,
     /* [previous][next][first][last][top][bottom][index][help] */
 533         int * pages, int * shared, int * dirty, int * total)
 534 {
 535         pte_t * pte;
 536         unsigned long end;
 537 
 538         if (pmd_none(*pmd))
 539                 return;
 540         if (pmd_bad(*pmd)) {
 541                 printk("statm_pte_range: bad pmd (%08lx)\n", pmd_val(*pmd));
 542                 pmd_clear(pmd);
 543                 return;
 544         }
 545         pte = pte_offset(pmd, address);
 546         address &= ~PMD_MASK;
 547         end = address + size;
 548         if (end > PMD_SIZE)
 549                 end = PMD_SIZE;
 550         do {
 551                 pte_t page = *pte;
 552 
 553                 address += PAGE_SIZE;
 554                 pte++;
 555                 if (pte_none(page))
 556                         continue;
 557                 ++*total;
 558                 if (!pte_present(page))
 559                         continue;
 560                 ++*pages;
 561                 if (pte_dirty(page))
 562                         ++*dirty;
 563                 if (pte_page(page) >= high_memory)
 564                         continue;
 565                 if (mem_map[MAP_NR(pte_page(page))].count > 1)
 566                         ++*shared;
 567         } while (address < end);
 568 }
 569 
 570 static inline void statm_pmd_range(pgd_t * pgd, unsigned long address, unsigned long size,
     /* [previous][next][first][last][top][bottom][index][help] */
 571         int * pages, int * shared, int * dirty, int * total)
 572 {
 573         pmd_t * pmd;
 574         unsigned long end;
 575 
 576         if (pgd_none(*pgd))
 577                 return;
 578         if (pgd_bad(*pgd)) {
 579                 printk("statm_pmd_range: bad pgd (%08lx)\n", pgd_val(*pgd));
 580                 pgd_clear(pgd);
 581                 return;
 582         }
 583         pmd = pmd_offset(pgd, address);
 584         address &= ~PGDIR_MASK;
 585         end = address + size;
 586         if (end > PGDIR_SIZE)
 587                 end = PGDIR_SIZE;
 588         do {
 589                 statm_pte_range(pmd, address, end - address, pages, shared, dirty, total);
 590                 address = (address + PMD_SIZE) & PMD_MASK;
 591                 pmd++;
 592         } while (address < end);
 593 }
 594 
 595 static void statm_pgd_range(pgd_t * pgd, unsigned long address, unsigned long end,
     /* [previous][next][first][last][top][bottom][index][help] */
 596         int * pages, int * shared, int * dirty, int * total)
 597 {
 598         while (address < end) {
 599                 statm_pmd_range(pgd, address, end - address, pages, shared, dirty, total);
 600                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
 601                 pgd++;
 602         }
 603 }
 604 
 605 static int get_statm(int pid, char * buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 606 {
 607         struct task_struct ** p = get_task(pid), *tsk;
 608         int size=0, resident=0, share=0, trs=0, lrs=0, drs=0, dt=0;
 609 
 610         if (!p || (tsk = *p) == NULL)
 611                 return 0;
 612         if (tsk->mm) {
 613                 struct vm_area_struct * vma = tsk->mm->mmap;
 614 
 615                 while (vma) {
 616                         pgd_t *pgd = pgd_offset(tsk->mm, vma->vm_start);
 617                         int pages = 0, shared = 0, dirty = 0, total = 0;
 618 
 619                         statm_pgd_range(pgd, vma->vm_start, vma->vm_end, &pages, &shared, &dirty, &total);
 620                         resident += pages;
 621                         share += shared;
 622                         dt += dirty;
 623                         size += total;
 624                         if (vma->vm_flags & VM_EXECUTABLE)
 625                                 trs += pages;   /* text */
 626                         else if (vma->vm_flags & VM_GROWSDOWN)
 627                                 drs += pages;   /* stack */
 628                         else if (vma->vm_end > 0x60000000)
 629                                 lrs += pages;   /* library */
 630                         else
 631                                 drs += pages;
 632                         vma = vma->vm_next;
 633                 }
 634         }
 635         return sprintf(buffer,"%d %d %d %d %d %d %d\n",
 636                        size, resident, share, trs, lrs, drs, dt);
 637 }
 638 
 639 /*
 640  * The way we support synthetic files > 4K
 641  * - without storing their contents in some buffer and
 642  * - without walking through the entire synthetic file until we reach the
 643  *   position of the requested data
 644  * is to cleverly encode the current position in the file's f_pos field.
 645  * There is no requirement that a read() call which returns `count' bytes
 646  * of data increases f_pos by exactly `count'.
 647  *
 648  * This idea is Linus' one. Bruno implemented it.
 649  */
 650 
 651 /*
 652  * For the /proc/<pid>/maps file, we use fixed length records, each containing
 653  * a single line.
 654  */
 655 #define MAPS_LINE_LENGTH        1024
 656 #define MAPS_LINE_SHIFT         10
 657 /*
 658  * f_pos = (number of the vma in the task->mm->mmap list) * MAPS_LINE_LENGTH
 659  *         + (index into the line)
 660  */
 661 #define MAPS_LINE_FORMAT          "%08lx-%08lx %s %08lx %02x:%02x %lu\n"
 662 #define MAPS_LINE_MAX   49 /* sum of 8  1  8  1 4 1 8  1  2 1  2 1 10 1 */
 663 
 664 static int read_maps (int pid, struct file * file, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 665 {
 666         struct task_struct ** p = get_task(pid);
 667         char * destptr;
 668         loff_t lineno;
 669         int column;
 670         struct vm_area_struct * map;
 671         int i;
 672 
 673         if (!p || !*p)
 674                 return -EINVAL;
 675 
 676         if (!(*p)->mm || count == 0)
 677                 return 0;
 678 
 679         /* decode f_pos */
 680         lineno = file->f_pos >> MAPS_LINE_SHIFT;
 681         column = file->f_pos & (MAPS_LINE_LENGTH-1);
 682 
 683         /* quickly go to line lineno */
 684         for (map = (*p)->mm->mmap, i = 0; map && (i < lineno); map = map->vm_next, i++)
 685                 continue;
 686 
 687         destptr = buf;
 688 
 689         for ( ; map ; ) {
 690                 /* produce the next line */
 691                 char line[MAPS_LINE_MAX+1];
 692                 char str[5], *cp = str;
 693                 int flags;
 694                 kdev_t dev;
 695                 unsigned long ino;
 696                 int len;
 697 
 698                 flags = map->vm_flags;
 699 
 700                 *cp++ = flags & VM_READ ? 'r' : '-';
 701                 *cp++ = flags & VM_WRITE ? 'w' : '-';
 702                 *cp++ = flags & VM_EXEC ? 'x' : '-';
 703                 *cp++ = flags & VM_MAYSHARE ? 's' : 'p';
 704                 *cp++ = 0;
 705 
 706                 if (map->vm_inode != NULL) {
 707                         dev = map->vm_inode->i_dev;
 708                         ino = map->vm_inode->i_ino;
 709                 } else {
 710                         dev = 0;
 711                         ino = 0;
 712                 }
 713 
 714                 len = sprintf(line, MAPS_LINE_FORMAT,
 715                               map->vm_start, map->vm_end, str, map->vm_offset,
 716                               MAJOR(dev),MINOR(dev), ino);
 717 
 718                 if (column >= len) {
 719                         column = 0; /* continue with next line at column 0 */
 720                         lineno++;
 721                         map = map->vm_next;
 722                         continue;
 723                 }
 724 
 725                 i = len-column;
 726                 if (i > count)
 727                         i = count;
 728                 memcpy_tofs(destptr, line+column, i);
 729                 destptr += i; count -= i;
 730                 column += i;
 731                 if (column >= len) {
 732                         column = 0; /* next time: next line at column 0 */
 733                         lineno++;
 734                         map = map->vm_next;
 735                 }
 736 
 737                 /* done? */
 738                 if (count == 0)
 739                         break;
 740 
 741                 /* By writing to user space, we might have slept.
 742                  * Stop the loop, to avoid a race condition.
 743                  */
 744                 if (*p != current)
 745                         break;
 746         }
 747 
 748         /* encode f_pos */
 749         file->f_pos = (lineno << MAPS_LINE_SHIFT) + column;
 750 
 751         return destptr-buf;
 752 }
 753 
 754 extern int get_module_list(char *);
 755 extern int get_device_list(char *);
 756 extern int get_filesystem_list(char *);
 757 extern int get_ksyms_list(char *, char **, off_t, int);
 758 extern int get_irq_list(char *);
 759 extern int get_dma_list(char *);
 760 extern int get_cpuinfo(char *);
 761 extern int get_pci_list(char*);
 762 
 763 static int get_root_array(char * page, int type, char **start, off_t offset, int length)
     /* [previous][next][first][last][top][bottom][index][help] */
 764 {
 765         switch (type) {
 766                 case PROC_LOADAVG:
 767                         return get_loadavg(page);
 768 
 769                 case PROC_UPTIME:
 770                         return get_uptime(page);
 771 
 772                 case PROC_MEMINFO:
 773                         return get_meminfo(page);
 774 
 775 #ifdef CONFIG_PCI
 776                 case PROC_PCI:
 777                         return get_pci_list(page);
 778 #endif
 779                         
 780                 case PROC_CPUINFO:
 781                         return get_cpuinfo(page);
 782 
 783                 case PROC_VERSION:
 784                         return get_version(page);
 785 
 786 #ifdef CONFIG_DEBUG_MALLOC
 787                 case PROC_MALLOC:
 788                         return get_malloc(page);
 789 #endif
 790 
 791                 case PROC_MODULES:
 792                         return get_module_list(page);
 793 
 794                 case PROC_STAT:
 795                         return get_kstat(page);
 796 
 797                 case PROC_DEVICES:
 798                         return get_device_list(page);
 799 
 800                 case PROC_INTERRUPTS:
 801                         return get_irq_list(page);
 802 
 803                 case PROC_FILESYSTEMS:
 804                         return get_filesystem_list(page);
 805 
 806                 case PROC_KSYMS:
 807                         return get_ksyms_list(page, start, offset, length);
 808 
 809                 case PROC_DMA:
 810                         return get_dma_list(page);
 811 
 812                 case PROC_IOPORTS:
 813                         return get_ioport_list(page);
 814         }
 815         return -EBADF;
 816 }
 817 
 818 static int get_process_array(char * page, int pid, int type)
     /* [previous][next][first][last][top][bottom][index][help] */
 819 {
 820         switch (type) {
 821                 case PROC_PID_ENVIRON:
 822                         return get_env(pid, page);
 823                 case PROC_PID_CMDLINE:
 824                         return get_arg(pid, page);
 825                 case PROC_PID_STAT:
 826                         return get_stat(pid, page);
 827                 case PROC_PID_STATM:
 828                         return get_statm(pid, page);
 829         }
 830         return -EBADF;
 831 }
 832 
 833 
 834 static inline int fill_array(char * page, int pid, int type, char **start, off_t offset, int length)
     /* [previous][next][first][last][top][bottom][index][help] */
 835 {
 836         if (pid)
 837                 return get_process_array(page, pid, type);
 838         return get_root_array(page, type, start, offset, length);
 839 }
 840 
 841 #define PROC_BLOCK_SIZE (3*1024)                /* 4K page size but our output routines use some slack for overruns */
 842 
 843 static int array_read(struct inode * inode, struct file * file,char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 844 {
 845         unsigned long page;
 846         char *start;
 847         int length;
 848         int end;
 849         unsigned int type, pid;
 850 
 851         if (count < 0)
 852                 return -EINVAL;
 853         if (count > PROC_BLOCK_SIZE)
 854                 count = PROC_BLOCK_SIZE;
 855         if (!(page = __get_free_page(GFP_KERNEL)))
 856                 return -ENOMEM;
 857         type = inode->i_ino;
 858         pid = type >> 16;
 859         type &= 0x0000ffff;
 860         start = NULL;
 861         length = fill_array((char *) page, pid, type,
 862                             &start, file->f_pos, count);
 863         if (length < 0) {
 864                 free_page(page);
 865                 return length;
 866         }
 867         if (start != NULL) {
 868                 /* We have had block-adjusting processing! */
 869                 memcpy_tofs(buf, start, length);
 870                 file->f_pos += length;
 871                 count = length;
 872         } else {
 873                 /* Static 4kB (or whatever) block capacity */
 874                 if (file->f_pos >= length) {
 875                         free_page(page);
 876                         return 0;
 877                 }
 878                 if (count + file->f_pos > length)
 879                         count = length - file->f_pos;
 880                 end = count + file->f_pos;
 881                 memcpy_tofs(buf, (char *) page + file->f_pos, count);
 882                 file->f_pos = end;
 883         }
 884         free_page(page);
 885         return count;
 886 }
 887 
 888 static struct file_operations proc_array_operations = {
 889         NULL,           /* array_lseek */
 890         array_read,
 891         NULL,           /* array_write */
 892         NULL,           /* array_readdir */
 893         NULL,           /* array_select */
 894         NULL,           /* array_ioctl */
 895         NULL,           /* mmap */
 896         NULL,           /* no special open code */
 897         NULL,           /* no special release code */
 898         NULL            /* can't fsync */
 899 };
 900 
 901 struct inode_operations proc_array_inode_operations = {
 902         &proc_array_operations, /* default base directory file-ops */
 903         NULL,                   /* create */
 904         NULL,                   /* lookup */
 905         NULL,                   /* link */
 906         NULL,                   /* unlink */
 907         NULL,                   /* symlink */
 908         NULL,                   /* mkdir */
 909         NULL,                   /* rmdir */
 910         NULL,                   /* mknod */
 911         NULL,                   /* rename */
 912         NULL,                   /* readlink */
 913         NULL,                   /* follow_link */
 914         NULL,                   /* bmap */
 915         NULL,                   /* truncate */
 916         NULL                    /* permission */
 917 };
 918 
 919 static int arraylong_read (struct inode * inode, struct file * file, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 920 {
 921         unsigned int pid = inode->i_ino >> 16;
 922         unsigned int type = inode->i_ino & 0x0000ffff;
 923 
 924         if (count < 0)
 925                 return -EINVAL;
 926 
 927         switch (type) {
 928                 case PROC_PID_MAPS:
 929                         return read_maps(pid, file, buf, count);
 930         }
 931         return -EINVAL;
 932 }
 933 
 934 static struct file_operations proc_arraylong_operations = {
 935         NULL,           /* array_lseek */
 936         arraylong_read,
 937         NULL,           /* array_write */
 938         NULL,           /* array_readdir */
 939         NULL,           /* array_select */
 940         NULL,           /* array_ioctl */
 941         NULL,           /* mmap */
 942         NULL,           /* no special open code */
 943         NULL,           /* no special release code */
 944         NULL            /* can't fsync */
 945 };
 946 
 947 struct inode_operations proc_arraylong_inode_operations = {
 948         &proc_arraylong_operations,     /* default base directory file-ops */
 949         NULL,                   /* create */
 950         NULL,                   /* lookup */
 951         NULL,                   /* link */
 952         NULL,                   /* unlink */
 953         NULL,                   /* symlink */
 954         NULL,                   /* mkdir */
 955         NULL,                   /* rmdir */
 956         NULL,                   /* mknod */
 957         NULL,                   /* rename */
 958         NULL,                   /* readlink */
 959         NULL,                   /* follow_link */
 960         NULL,                   /* bmap */
 961         NULL,                   /* truncate */
 962         NULL                    /* permission */
 963 };

/* [previous][next][first][last][top][bottom][index][help] */