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

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