home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / fs / proc / array.c next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  13.4 KB  |  608 lines

  1. /*
  2.  *  linux/fs/proc/array.c
  3.  *
  4.  *  Copyright (C) 1992  by Linus Torvalds
  5.  *  based on ideas by Darren Senn
  6.  *
  7.  *  stat,statm extensions by Michael K. Johnson, johnsonm@stolaf.edu
  8.  */
  9.  
  10. #include <linux/types.h>
  11. #include <linux/errno.h>
  12. #include <linux/sched.h>
  13. #include <linux/kernel.h>
  14. #include <linux/tty.h>
  15. #include <linux/user.h>
  16. #include <linux/a.out.h>
  17. #include <linux/string.h>
  18. #include <linux/mman.h>
  19.  
  20. #include <asm/segment.h>
  21. #include <asm/io.h>
  22.  
  23. #define LOAD_INT(x) ((x) >> FSHIFT)
  24. #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
  25.  
  26. #ifdef CONFIG_DEBUG_MALLOC
  27. int get_malloc(char * buffer);
  28. #endif
  29.  
  30. static int read_core(struct inode * inode, struct file * file,char * buf, int count)
  31. {
  32.     unsigned long p = file->f_pos;
  33.     int read;
  34.     int count1;
  35.     char * pnt;
  36.     struct user dump;
  37.  
  38.     memset(&dump, 0, sizeof(struct user));
  39.     dump.magic = CMAGIC;
  40.     dump.u_dsize = (high_memory - KSTART_ADDR) >> 12;
  41.  
  42.     if (count < 0)
  43.         return -EINVAL;
  44.     if (p >= (high_memory - KSTART_ADDR) + 2*PAGE_SIZE)
  45.         return 0;
  46.     if (count > (high_memory - KSTART_ADDR) - p)
  47.         count = (high_memory - KSTART_ADDR) - p;
  48.     read = 0;
  49.  
  50.     if (p < sizeof(struct user) && count > 0) {
  51.         count1 = count;
  52.         if (p + count1 > sizeof(struct user))
  53.             count1 = sizeof(struct user)-p;
  54.         pnt = (char *) &dump + p;
  55.         memcpy_tofs(buf,(void *) pnt, count1);
  56.         buf += count1;
  57.         p += count1;
  58.         count -= count1;
  59.         read += count1;
  60.     }
  61.  
  62.     while (p < 2*PAGE_SIZE && count > 0) {
  63.         put_fs_byte(0,buf);
  64.         buf++;
  65.         p++;
  66.         count--;
  67.         read++;
  68.     }
  69.     memcpy_tofs(buf,(void *) (p + KSTART_ADDR - 2*PAGE_SIZE),count);
  70.     read += count;
  71.     file->f_pos += read;
  72.     return read;
  73. }
  74.  
  75. static int get_loadavg(char * buffer)
  76. {
  77.     int a, b, c;
  78.  
  79.     a = avenrun[0] + (FIXED_1/200);
  80.     b = avenrun[1] + (FIXED_1/200);
  81.     c = avenrun[2] + (FIXED_1/200);
  82.     return sprintf(buffer,"%d.%02d %d.%02d %d.%02d\n",
  83.         LOAD_INT(a), LOAD_FRAC(a),
  84.         LOAD_INT(b), LOAD_FRAC(b),
  85.         LOAD_INT(c), LOAD_FRAC(c));
  86. }
  87.  
  88. static int get_uptime(char * buffer)
  89. {
  90.     unsigned long uptime;
  91.     unsigned long idle;
  92.  
  93.     uptime = jiffies;
  94.     idle = task[0]->utime + task[0]->stime;
  95.     return sprintf(buffer,"%lu.%02lu %lu.%02lu\n",
  96.         uptime / HZ,
  97.         uptime % HZ,
  98.         idle / HZ,
  99.         idle % HZ);
  100. }
  101.  
  102. static int get_meminfo(char * buffer)
  103. {
  104.     struct sysinfo i;
  105.  
  106.     si_meminfo(&i);
  107.     si_swapinfo(&i);
  108.     return sprintf(buffer, "        total:   used:    free:   shared:  buffers:\n"
  109.         "Mem:  %8lu %8lu %8lu %8lu %8lu\n"
  110.         "Swap: %8lu %8lu %8lu\n",
  111.         i.totalram, i.totalram-i.freeram, i.freeram, i.sharedram, i.bufferram,
  112.         i.totalswap, i.totalswap-i.freeswap, i.freeswap);
  113. }
  114.  
  115. static int get_version(char * buffer)
  116. {
  117.     extern char *linux_banner;
  118.  
  119.     strcpy(buffer, linux_banner);
  120.     return strlen(buffer);
  121. }
  122.  
  123. static struct task_struct ** get_task(pid_t pid)
  124. {
  125.     struct task_struct ** p;
  126.  
  127.     p = task;
  128.     while (++p < task+NR_TASKS) {
  129.         if (*p && (*p)->pid == pid)
  130.             return p;
  131.     }
  132.     return NULL;
  133. }
  134.  
  135. static unsigned long get_phys_addr(struct task_struct ** p, unsigned long ptr)
  136. {
  137.     unsigned long page;
  138.  
  139.     if (!p || !*p || ptr >= TASK_SIZE)
  140.         return 0;
  141.  
  142. #if defined(__i386__)
  143.     page = *PAGE_DIR_OFFSET((*p)->tss.cr3,ptr);
  144.     if (!(page & 1))
  145.         return 0;
  146.     page &= PAGE_MASK;
  147.     page += PAGE_PTR(ptr);
  148.     page = *(unsigned long *) page;
  149.     if (!(page & 1))
  150.         return 0;
  151.     page &= PAGE_MASK;
  152.     page += ptr & ~PAGE_MASK;
  153. #elif defined(__mc68000__)
  154.     page = (*p)->tss.pagedir_v[L1_INDEX(ptr)];
  155.     if (!(page & PAGE_TABLE))
  156.         return 0;
  157.     page = PTOV(page & TABLE_MASK);
  158.     page += L2_INDEX(ptr) * sizeof(long);
  159.     page = *(unsigned long *) page;
  160.     if (!(page & PAGE_TABLE))
  161.         return 0;
  162.     page = PTOV(page & PAGE_MASK);
  163.     page += L3_INDEX(ptr) * sizeof(long);
  164.     page = *(unsigned long *) page;
  165.     if (!(page & PAGE_PRESENT))
  166.         return 0;
  167.     page = PTOV(page & PAGE_MASK);
  168.     page += ptr & ~PAGE_MASK;
  169. #endif
  170.     return page;
  171. }
  172.  
  173. static int get_array(struct task_struct ** p, unsigned long start, unsigned long end, char * buffer)
  174. {
  175.     unsigned long addr;
  176.     int size = 0, result = 0;
  177.     char c;
  178.  
  179.     if (start >= end)
  180.         return result;
  181.     for (;;) {
  182.         addr = get_phys_addr(p, start);
  183.         if (!addr)
  184.             return result;
  185.         do {
  186.             c = *(char *) addr;
  187.             if (!c)
  188.                 result = size;
  189.             if (size < PAGE_SIZE)
  190.                 buffer[size++] = c;
  191.             else
  192.                 return result;
  193.             addr++;
  194.             start++;
  195.             if (start >= end)
  196.                 return result;
  197.         } while (!(addr & ~PAGE_MASK));
  198.     }
  199. }
  200.  
  201. static int get_env(int pid, char * buffer)
  202. {
  203.     struct task_struct ** p = get_task(pid);
  204.  
  205.     if (!p || !*p)
  206.         return 0;
  207.     return get_array(p, (*p)->env_start, (*p)->env_end, buffer);
  208. }
  209.  
  210. static int get_arg(int pid, char * buffer)
  211. {
  212.     struct task_struct ** p = get_task(pid);
  213.  
  214.     if (!p || !*p)
  215.         return 0;
  216.     return get_array(p, (*p)->arg_start, (*p)->arg_end, buffer);
  217. }
  218.  
  219. #if defined(__i386__)
  220. static unsigned long get_wchan(struct task_struct *p)
  221. {
  222.     unsigned long ebp, eip;
  223.     unsigned long stack_page;
  224.     int count = 0;
  225.  
  226.     if (!p || p == current || p->state == TASK_RUNNING)
  227.         return 0;
  228.     stack_page = p->kernel_stack_page;
  229.     if (!stack_page)
  230.         return 0;
  231.     ebp = p->tss.ebp;
  232.     do {
  233.         if (ebp < stack_page || ebp >= 4092+stack_page)
  234.             return 0;
  235.         eip = *(unsigned long *) (ebp+4);
  236.         if ((void *)eip != sleep_on &&
  237.             (void *)eip != interruptible_sleep_on)
  238.             return eip;
  239.         ebp = *(unsigned long *) ebp;
  240.     } while (count++ < 16);
  241.     return 0;
  242. }
  243. #elif defined(__mc68000__)
  244. static unsigned long get_wchan(struct task_struct *p)
  245. {
  246.     unsigned long sp, fp, pc;
  247.     unsigned long stack_page;
  248.     int count = 0;
  249.  
  250.     if (!p || p == current || p->state == TASK_RUNNING)
  251.         return 0;
  252.     stack_page = p->kernel_stack_page;
  253.     if (!stack_page)
  254.         return 0;
  255.     sp = p->tss.regs[11];
  256.     fp = p->tss.regs[10];
  257.     do {
  258.         if (sp < stack_page || sp >= 4092+stack_page 
  259.             || fp < stack_page || fp >= 4092+stack_page)
  260.             return 0;
  261.         pc = *(unsigned long *)sp;
  262.         if ((void *)pc != sleep_on &&
  263.             (void *)pc != interruptible_sleep_on)
  264.             return pc;
  265.         sp = fp;
  266.         fp = *(unsigned long *) fp;
  267.     } while (count++ < 16);
  268.     return 0;
  269. }
  270. #endif
  271.  
  272. #if defined(__i386__)
  273. #define    KSTK_EIP(stack)    (((unsigned long *)stack)[1019])
  274. #define    KSTK_ESP(stack)    (((unsigned long *)stack)[1022])
  275. #elif defined(__mc68000__)
  276. #define    KSTK_EIP(stack)    (*(unsigned long *)((stack)+4090))
  277. #define    KSTK_ESP(stack)    (*(unsigned long *)((stack)+4078))
  278. #endif /* arch */
  279.  
  280. static int get_stat(int pid, char * buffer)
  281. {
  282.     struct task_struct ** p = get_task(pid);
  283.     unsigned long sigignore=0, sigcatch=0, bit=1, wchan;
  284.     unsigned long vsize, eip, esp;
  285.     int i,tty_pgrp;
  286.     char state;
  287.  
  288.     if (!p || !*p)
  289.         return 0;
  290.     if ((*p)->state < 0 || (*p)->state > 5)
  291.         state = '.';
  292.     else
  293.         state = "RSDZTD"[(*p)->state];
  294.     eip = esp = 0;
  295.     vsize = (*p)->kernel_stack_page;
  296.     if (vsize) {
  297.         eip = KSTK_EIP(vsize);
  298.         esp = KSTK_ESP(vsize);
  299.         vsize = (*p)->brk - (*p)->start_code + PAGE_SIZE-1;
  300.         if (esp)
  301.             vsize += TASK_SIZE - esp;
  302.     }
  303.     wchan = get_wchan(*p);
  304.     for(i=0; i<32; ++i) {
  305.         switch((int) (*p)->sigaction[i].sa_handler) {
  306.         case 1: sigignore |= bit; break;
  307.         case 0: break;
  308.         default: sigcatch |= bit;
  309.         } bit <<= 1;
  310.     }
  311.     tty_pgrp = (*p)->tty;
  312.     if (tty_pgrp > 0 && tty_table[tty_pgrp])
  313.         tty_pgrp = tty_table[tty_pgrp]->pgrp;
  314.     else
  315.         tty_pgrp = -1;
  316.     return sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
  317. %lu %lu %lu %ld %ld %ld %ld %ld %ld %lu %lu %ld %lu %u %u %lu %lu %lu %lu %lu %lu \
  318. %lu %lu %lu %lu\n",
  319.         pid,
  320.         (*p)->comm,
  321.         state,
  322.         (*p)->p_pptr->pid,
  323.         (*p)->pgrp,
  324.         (*p)->session,
  325.         (*p)->tty,
  326.         tty_pgrp,
  327.         (*p)->flags,
  328.         (*p)->min_flt,
  329.         (*p)->cmin_flt,
  330.         (*p)->maj_flt,
  331.         (*p)->cmaj_flt,
  332.         (*p)->utime,
  333.         (*p)->stime,
  334.         (*p)->cutime,
  335.         (*p)->cstime,
  336.         (*p)->counter,  /* this is the kernel priority ---
  337.                    subtract 30 in your user-level program. */
  338.         (*p)->priority, /* this is the nice value ---
  339.                    subtract 15 in your user-level program. */
  340.         (*p)->timeout,
  341.         (*p)->it_real_value,
  342.         (*p)->start_time,
  343.         vsize,
  344.         (*p)->rss, /* you might want to shift this left 3 */
  345.         (*p)->rlim[RLIMIT_RSS].rlim_cur,
  346.         (*p)->start_code,
  347.         (*p)->end_code,
  348.         (*p)->start_stack,
  349.         esp,
  350.         eip,
  351.         (*p)->signal,
  352.         (*p)->blocked,
  353.         sigignore,
  354.         sigcatch,
  355.         wchan);
  356. }
  357.  
  358. static int get_statm(int pid, char * buffer)
  359. {
  360.     struct task_struct ** p = get_task(pid);
  361.     int i, tpag;
  362.     int size=0, resident=0, share=0, trs=0, lrs=0, drs=0, dt=0;
  363.     unsigned long ptbl, *buf, *pte, *pagedir, map_nr;
  364.  
  365.     if (!p || !*p)
  366.         return 0;
  367.     tpag = (*p)->end_code / PAGE_SIZE;
  368.     if ((*p)->state != TASK_ZOMBIE) {
  369. #if defined(__i386__)
  370.       pagedir = (unsigned long *) (*p)->tss.cr3;
  371.       for (i = 0; i < 0x300; ++i) {
  372.         if ((ptbl = pagedir[i]) == 0) {
  373.           tpag -= PTRS_PER_PAGE;
  374.           continue;
  375.         }
  376.         buf = (unsigned long *)(ptbl & PAGE_MASK);
  377.         for (pte = buf; pte < (buf + PTRS_PER_PAGE); ++pte) {
  378.           if (*pte != 0) {
  379.         ++size;
  380.         if (*pte & 1) {
  381.           ++resident;
  382.           if (tpag > 0)
  383.             ++trs;
  384.           else
  385.             ++drs;
  386.           if (i >= 15 && i < 0x2f0) {
  387.             ++lrs;
  388.             if (*pte & 0x40)
  389.               ++dt;
  390.             else
  391.               --drs;
  392.           }
  393.           map_nr = MAP_NR(*pte);
  394.           if (map_nr < ((high_memory - KSTART_ADDR) / PAGE_SIZE) && mem_map[map_nr] > 1)
  395.             ++share;
  396.         }
  397.           }
  398.           --tpag;
  399.         }
  400.       }
  401. #elif defined(__mc68000__)
  402.       pagedir = (*p)->tss.pagedir_v;
  403.       for (i = 0; i < NUM_L1_ENTRIES-1; ++i) {
  404.             unsigned long *ptrp, *ptrpe;
  405.         if ((ptbl = pagedir[i]) == 0) {
  406.           tpag -= NUM_L2_ENTRIES * NUM_L3_ENTRIES;
  407.           continue;
  408.         }
  409.         ptrp = (unsigned long *)PTOV(ptbl & TABLE_MASK);
  410.         for (ptrpe = ptrp; ptrpe < (ptrp + NUM_L2_ENTRIES); ptrpe += 16) {
  411.               if (*ptrpe == 0) {
  412.         tpag -= NUM_L3_ENTRIES;
  413.         continue;
  414.           }
  415.           buf = (unsigned long *)PTOV(*ptrpe & PAGE_MASK);
  416.           for (pte = buf; pte < (buf + PTRS_PER_PAGE); ++pte) {
  417.             if (*pte != 0) {
  418.           ++size;
  419.           if (*pte & PAGE_PRESENT) {
  420.             ++resident;
  421.             if (tpag > 0)
  422.               ++trs;
  423.             else
  424.               ++drs;
  425.             if (i >= 2 && i < 94) { /* YUK! */
  426.               ++lrs;
  427.               if (*pte & PAGE_DIRTY)
  428.                 ++dt;
  429.               else
  430.                 --drs;
  431.             }
  432.             map_nr = MAP_NR(PTOV(*pte & PAGE_MASK));
  433.             if (map_nr < ((high_memory - KSTART_ADDR) / PAGE_SIZE) && mem_map[map_nr] > 1)
  434.               ++share;
  435.           }
  436.             }
  437.         --tpag;
  438.           }
  439.         }
  440.       }
  441. #endif /* arch */
  442.     }
  443.     return sprintf(buffer,"%d %d %d %d %d %d %d\n",
  444.                size, resident, share, trs, lrs, drs, dt);
  445. }
  446.  
  447. static int get_maps(int pid, char *buf)
  448. {
  449.     int sz = 0;
  450.     struct task_struct **p = get_task(pid);
  451.     struct vm_area_struct *map;
  452.  
  453.     if (!p || !*p)
  454.         return 0;
  455.  
  456.     for(map = (*p)->mmap; map != NULL; map = map->vm_next) {
  457.         char str[7], *cp = str;
  458.         int prot = map->vm_page_prot;
  459.         int perms, flags;
  460.         int end = sz + 80;    /* Length of line */
  461.         dev_t dev;
  462.         unsigned long ino;
  463.  
  464.         /*
  465.          * This tries to get an "rwxsp" string out of silly
  466.          * intel page permissions.  The vm_area_struct should
  467.          * probably have the original mmap args preserved.
  468.          */
  469.         
  470.         flags = perms = 0;
  471.  
  472.         if ((prot & PAGE_READONLY) == PAGE_READONLY)
  473.             perms |= PROT_READ | PROT_EXEC;
  474.         if (prot & (PAGE_COW|PAGE_RW)) {
  475.             perms |= PROT_WRITE | PROT_READ;
  476.             flags = prot & PAGE_COW ? MAP_PRIVATE : MAP_SHARED;
  477.         }
  478.  
  479.         *cp++ = perms & PROT_READ ? 'r' : '-';
  480.         *cp++ = perms & PROT_WRITE ? 'w' : '-';
  481.         *cp++ = perms & PROT_EXEC ? 'x' : '-';
  482.         *cp++ = flags & MAP_SHARED ? 's' : '-';
  483.         *cp++ = flags & MAP_PRIVATE ? 'p' : '-';
  484.         *cp++ = 0;
  485.         
  486.         if (end >= PAGE_SIZE) {
  487.             sprintf(buf+sz, "...\n");
  488.             break;
  489.         }
  490.         
  491.         if (map->vm_inode != NULL) {
  492.             dev = map->vm_inode->i_dev;
  493.             ino = map->vm_inode->i_ino;
  494.         } else {
  495.             dev = 0;
  496.             ino = 0;
  497.         }
  498.  
  499.         sz += sprintf(buf+sz, "%08lx-%08lx %s %08lx %02x:%02x %lu\n",
  500.                   map->vm_start, map->vm_end, str, map->vm_offset,
  501.                   MAJOR(dev),MINOR(dev), ino);
  502.         if (sz > end) {
  503.             printk("get_maps: end(%d) < sz(%d)\n", end, sz);
  504.             break;
  505.         }
  506.     }
  507.     
  508.     return sz;
  509. }
  510.  
  511. static int array_read(struct inode * inode, struct file * file,char * buf, int count)
  512. {
  513.     char * page;
  514.     int length;
  515.     int end;
  516.     unsigned int type, pid;
  517.  
  518.     if (count < 0)
  519.         return -EINVAL;
  520.     if (!(page = (char*) __get_free_page(GFP_KERNEL)))
  521.         return -ENOMEM;
  522.     type = inode->i_ino;
  523.     pid = type >> 16;
  524.     type &= 0x0000ffff;
  525.     switch (type) {
  526.         case 2:
  527.             length = get_loadavg(page);
  528.             break;
  529.         case 3:
  530.             length = get_uptime(page);
  531.             break;
  532.         case 4:
  533.             length = get_meminfo(page);
  534.             break;
  535.         case 6:
  536.             length = get_version(page);
  537.             break;
  538.         case 9:
  539.             length = get_env(pid, page);
  540.             break;
  541.         case 10:
  542.             length = get_arg(pid, page);
  543.             break;
  544.         case 11:
  545.             length = get_stat(pid, page);
  546.             break;
  547.         case 12:
  548.             length = get_statm(pid, page);
  549.             break;
  550. #ifdef CONFIG_DEBUG_MALLOC
  551.         case 13:
  552.             length = get_malloc(page);
  553.             break;
  554. #endif
  555.         case 14:
  556.             free_page((unsigned long) page);
  557.             return read_core(inode, file, buf, count);
  558.         case 15:
  559.             length = get_maps(pid, page);
  560.             break;
  561.         default:
  562.             free_page((unsigned long) page);
  563.             return -EBADF;
  564.     }
  565.     if (file->f_pos >= length) {
  566.         free_page((unsigned long) page);
  567.         return 0;
  568.     }
  569.     if (count + file->f_pos > length)
  570.         count = length - file->f_pos;
  571.     end = count + file->f_pos;
  572.     memcpy_tofs(buf, page + file->f_pos, count);
  573.     free_page((unsigned long) page);
  574.     file->f_pos = end;
  575.     return count;
  576. }
  577.  
  578. static struct file_operations proc_array_operations = {
  579.     NULL,        /* array_lseek */
  580.     array_read,
  581.     NULL,        /* array_write */
  582.     NULL,        /* array_readdir */
  583.     NULL,        /* array_select */
  584.     NULL,        /* array_ioctl */
  585.     NULL,        /* mmap */
  586.     NULL,        /* no special open code */
  587.     NULL,        /* no special release code */
  588.     NULL        /* can't fsync */
  589. };
  590.  
  591. struct inode_operations proc_array_inode_operations = {
  592.     &proc_array_operations,    /* default base directory file-ops */
  593.     NULL,            /* create */
  594.     NULL,            /* lookup */
  595.     NULL,            /* link */
  596.     NULL,            /* unlink */
  597.     NULL,            /* symlink */
  598.     NULL,            /* mkdir */
  599.     NULL,            /* rmdir */
  600.     NULL,            /* mknod */
  601.     NULL,            /* rename */
  602.     NULL,            /* readlink */
  603.     NULL,            /* follow_link */
  604.     NULL,            /* bmap */
  605.     NULL,            /* truncate */
  606.     NULL            /* permission */
  607. };
  608.