home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.2 / LINUX-1.2 / LINUX-1 / linux / fs / proc / array.c next >
Encoding:
C/C++ Source or Header  |  1995-02-23  |  21.8 KB  |  942 lines

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