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_cpuinfo
  10. get_task
  11. get_phys_addr
  12. get_array
  13. get_env
  14. get_arg
  15. get_wchan
  16. get_stat
  17. statm_pte_range
  18. statm_pmd_range
  19. statm_pgd_range
  20. get_statm
  21. read_maps
  22. get_root_array
  23. get_process_array
  24. fill_array
  25. array_read
  26. arraylong_read

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

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