upgrade to fedora-2.6.12-1.1398.FC4 + vserver 2.0.rc7
[linux-2.6.git] / fs / proc / task_mmu.c
1 #include <linux/mm.h>
2 #include <linux/hugetlb.h>
3 #include <linux/mount.h>
4 #include <linux/seq_file.h>
5 #include <asm/elf.h>
6 #include <asm/uaccess.h>
7 #include "internal.h"
8
9 char *task_mem(struct mm_struct *mm, char *buffer)
10 {
11         unsigned long data, text, lib;
12
13         data = mm->total_vm - mm->shared_vm - mm->stack_vm;
14         text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
15         lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
16         buffer += sprintf(buffer,
17                 "VmSize:\t%8lu kB\n"
18                 "VmLck:\t%8lu kB\n"
19                 "VmRSS:\t%8lu kB\n"
20                 "VmData:\t%8lu kB\n"
21                 "VmStk:\t%8lu kB\n"
22                 "VmExe:\t%8lu kB\n"
23                 "VmLib:\t%8lu kB\n"
24                 "VmPTE:\t%8lu kB\n"
25                 "StaBrk:\t%08lx kB\n"
26                 "Brk:\t%08lx kB\n"
27                 "StaStk:\t%08lx kB\n"
28                 ,
29                 (mm->total_vm - mm->reserved_vm) << (PAGE_SHIFT-10),
30                 mm->locked_vm << (PAGE_SHIFT-10),
31                 get_mm_counter(mm, rss) << (PAGE_SHIFT-10),
32                 data << (PAGE_SHIFT-10),
33                 mm->stack_vm << (PAGE_SHIFT-10), text, lib,
34                 (PTRS_PER_PTE*sizeof(pte_t)*mm->nr_ptes) >> 10,
35                 mm->start_brk, mm->brk, mm->start_stack);
36 #if __i386__
37         if (!nx_enabled)
38                 buffer += sprintf(buffer,
39                                 "ExecLim:\t%08lx\n", mm->context.exec_limit);
40 #endif
41         return buffer;
42 }
43
44 unsigned long task_vsize(struct mm_struct *mm)
45 {
46         return PAGE_SIZE * mm->total_vm;
47 }
48
49 int task_statm(struct mm_struct *mm, int *shared, int *text,
50                int *data, int *resident)
51 {
52         int rss = get_mm_counter(mm, rss);
53
54         *shared = rss - get_mm_counter(mm, anon_rss);
55         *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
56                                                                 >> PAGE_SHIFT;
57         *data = mm->total_vm - mm->shared_vm;
58         *resident = rss;
59         return mm->total_vm;
60 }
61
62 int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
63 {
64         struct vm_area_struct * vma;
65         int result = -ENOENT;
66         struct task_struct *task = proc_task(inode);
67         struct mm_struct * mm = get_task_mm(task);
68
69         if (!mm)
70                 goto out;
71         down_read(&mm->mmap_sem);
72
73         vma = mm->mmap;
74         while (vma) {
75                 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file)
76                         break;
77                 vma = vma->vm_next;
78         }
79
80         if (vma) {
81                 *mnt = mntget(vma->vm_file->f_vfsmnt);
82                 *dentry = dget(vma->vm_file->f_dentry);
83                 result = 0;
84         }
85
86         up_read(&mm->mmap_sem);
87         mmput(mm);
88 out:
89         return result;
90 }
91
92 static void pad_len_spaces(struct seq_file *m, int len)
93 {
94         len = 25 + sizeof(void*) * 6 - len;
95         if (len < 1)
96                 len = 1;
97         seq_printf(m, "%*c", len, ' ');
98 }
99
100 static int show_map(struct seq_file *m, void *v)
101 {
102         struct task_struct *task = m->private;
103         struct vm_area_struct *map = v;
104         struct mm_struct *mm = map->vm_mm;
105         struct file *file = map->vm_file;
106         int flags = map->vm_flags;
107         unsigned long ino = 0;
108         dev_t dev = 0;
109         int len;
110
111         if (file) {
112                 struct inode *inode = map->vm_file->f_dentry->d_inode;
113                 dev = inode->i_sb->s_dev;
114                 ino = inode->i_ino;
115         }
116
117         seq_printf(m, "%08lx-%08lx %c%c%c%c %08lx %02x:%02x %lu %n",
118                         map->vm_start,
119                         map->vm_end,
120                         flags & VM_READ ? 'r' : '-',
121                         flags & VM_WRITE ? 'w' : '-',
122                         (flags & VM_EXEC
123 #ifdef __i386__
124                                 || (!nx_enabled &&
125                                 (map->vm_start < task->mm->context.exec_limit))
126 #endif
127                         )
128                                 ? 'x' : '-',
129                         flags & VM_MAYSHARE ? 's' : 'p',
130                         map->vm_pgoff << PAGE_SHIFT,
131                         MAJOR(dev), MINOR(dev), ino, &len);
132
133         /*
134          * Print the dentry name for named mappings, and a
135          * special [heap] marker for the heap:
136          */
137         if (map->vm_file) {
138                 pad_len_spaces(m, len);
139                 seq_path(m, file->f_vfsmnt, file->f_dentry, "");
140         } else {
141                 if (mm) {
142                         if (map->vm_end == mm->brk) {
143                                 pad_len_spaces(m, len);
144                                 seq_puts(m, "[heap]");
145                         } else {
146                                 if (map->vm_start <= mm->start_stack &&
147                                         map->vm_end >= mm->start_stack) {
148
149                                         pad_len_spaces(m, len);
150                                         seq_puts(m, "[stack]");
151                                 }
152                         }
153                 } else {
154                         pad_len_spaces(m, len);
155                         seq_puts(m, "[vdso]");
156                 }
157         }
158         seq_putc(m, '\n');
159         if (m->count < m->size)  /* map is copied successfully */
160                 m->version = (map != get_gate_vma(task))? map->vm_start: 0;
161         return 0;
162 }
163
164 static void *m_start(struct seq_file *m, loff_t *pos)
165 {
166         struct task_struct *task = m->private;
167         unsigned long last_addr = m->version;
168         struct mm_struct *mm;
169         struct vm_area_struct *map, *tail_map;
170         loff_t l = *pos;
171
172         /*
173          * We remember last_addr rather than next_addr to hit with
174          * mmap_cache most of the time. We have zero last_addr at
175          * the begining and also after lseek. We will have -1 last_addr
176          * after the end of the maps.
177          */
178
179         if (last_addr == -1UL)
180                 return NULL;
181
182         mm = get_task_mm(task);
183         if (!mm)
184                 return NULL;
185
186         tail_map = get_gate_vma(task);
187         down_read(&mm->mmap_sem);
188
189         /* Start with last addr hint */
190         if (last_addr && (map = find_vma(mm, last_addr))) {
191                 map = map->vm_next;
192                 goto out;
193         }
194
195         /*
196          * Check the map index is within the range and do
197          * sequential scan until m_index.
198          */
199         map = NULL;
200         if ((unsigned long)l < mm->map_count) {
201                 map = mm->mmap;
202                 while (l-- && map)
203                         map = map->vm_next;
204                 goto out;
205         }
206
207         if (l != mm->map_count)
208                 tail_map = NULL; /* After gate map */
209
210 out:
211         if (map)
212                 return map;
213
214         /* End of maps has reached */
215         m->version = (tail_map != NULL)? 0: -1UL;
216         up_read(&mm->mmap_sem);
217         mmput(mm);
218         return tail_map;
219 }
220
221 static void m_stop(struct seq_file *m, void *v)
222 {
223         struct task_struct *task = m->private;
224         struct vm_area_struct *map = v;
225         if (map && map != get_gate_vma(task)) {
226                 struct mm_struct *mm = map->vm_mm;
227                 up_read(&mm->mmap_sem);
228                 mmput(mm);
229         }
230 }
231
232 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
233 {
234         struct task_struct *task = m->private;
235         struct vm_area_struct *map = v;
236         struct vm_area_struct *tail_map = get_gate_vma(task);
237
238         (*pos)++;
239         if (map && (map != tail_map) && map->vm_next)
240                 return map->vm_next;
241         m_stop(m, v);
242         return (map != tail_map)? tail_map: NULL;
243 }
244
245 struct seq_operations proc_pid_maps_op = {
246         .start  = m_start,
247         .next   = m_next,
248         .stop   = m_stop,
249         .show   = show_map
250 };