VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / fs / proc / kcore.c
1 /*
2  *      fs/proc/kcore.c kernel ELF core dumper
3  *
4  *      Modelled on fs/exec.c:aout_core_dump()
5  *      Jeremy Fitzhardinge <jeremy@sw.oz.au>
6  *      ELF version written by David Howells <David.Howells@nexor.co.uk>
7  *      Modified and incorporated into 2.3.x by Tigran Aivazian <tigran@veritas.com>
8  *      Support to dump vmalloc'd areas (ELF only), Tigran Aivazian <tigran@veritas.com>
9  *      Safe accesses to vmalloc/direct-mapped discontiguous areas, Kanoj Sarcar <kanoj@sgi.com>
10  */
11
12 #include <linux/config.h>
13 #include <linux/mm.h>
14 #include <linux/proc_fs.h>
15 #include <linux/user.h>
16 #include <linux/a.out.h>
17 #include <linux/elf.h>
18 #include <linux/elfcore.h>
19 #include <linux/vmalloc.h>
20 #include <linux/highmem.h>
21 #include <linux/init.h>
22 #include <asm/uaccess.h>
23 #include <asm/io.h>
24
25
26 static int open_kcore(struct inode * inode, struct file * filp)
27 {
28         return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
29 }
30
31 static ssize_t read_kcore(struct file *, char __user *, size_t, loff_t *);
32
33 struct file_operations proc_kcore_operations = {
34         .read           = read_kcore,
35         .open           = open_kcore,
36 };
37
38 #ifndef kc_vaddr_to_offset
39 #define kc_vaddr_to_offset(v) ((v) - PAGE_OFFSET)
40 #endif
41 #ifndef kc_offset_to_vaddr
42 #define kc_offset_to_vaddr(o) ((o) + PAGE_OFFSET)
43 #endif
44
45 #define roundup(x, y)  ((((x)+((y)-1))/(y))*(y))
46
47 /* An ELF note in memory */
48 struct memelfnote
49 {
50         const char *name;
51         int type;
52         unsigned int datasz;
53         void *data;
54 };
55
56 static struct kcore_list *kclist;
57 static rwlock_t kclist_lock = RW_LOCK_UNLOCKED;
58
59 void
60 kclist_add(struct kcore_list *new, void *addr, size_t size)
61 {
62         new->addr = (unsigned long)addr;
63         new->size = size;
64
65         write_lock(&kclist_lock);
66         new->next = kclist;
67         kclist = new;
68         write_unlock(&kclist_lock);
69 }
70
71 struct kcore_list *
72 kclist_del(void *addr)
73 {
74         struct kcore_list *m, **p = &kclist;
75
76         write_lock(&kclist_lock);
77         for (m = *p; m; p = &m->next) {
78                 if (m->addr == (unsigned long)addr) {
79                         *p = m->next;
80                         write_unlock(&kclist_lock);
81                         return m;
82                 }
83         }
84         write_unlock(&kclist_lock);
85         return NULL;
86 }
87
88 static size_t get_kcore_size(int *nphdr, size_t *elf_buflen)
89 {
90         size_t try, size;
91         struct kcore_list *m;
92
93         *nphdr = 1; /* PT_NOTE */
94         size = 0;
95
96         for (m=kclist; m; m=m->next) {
97                 try = kc_vaddr_to_offset((size_t)m->addr + m->size);
98                 if (try > size)
99                         size = try;
100                 *nphdr = *nphdr + 1;
101         }
102         *elf_buflen =   sizeof(struct elfhdr) + 
103                         (*nphdr + 2)*sizeof(struct elf_phdr) + 
104                         3 * sizeof(struct memelfnote) +
105                         sizeof(struct elf_prstatus) +
106                         sizeof(struct elf_prpsinfo) +
107                         sizeof(struct task_struct);
108         *elf_buflen = PAGE_ALIGN(*elf_buflen);
109         return size + *elf_buflen;
110 }
111
112
113 /*****************************************************************************/
114 /*
115  * determine size of ELF note
116  */
117 static int notesize(struct memelfnote *en)
118 {
119         int sz;
120
121         sz = sizeof(struct elf_note);
122         sz += roundup(strlen(en->name), 4);
123         sz += roundup(en->datasz, 4);
124
125         return sz;
126 } /* end notesize() */
127
128 /*****************************************************************************/
129 /*
130  * store a note in the header buffer
131  */
132 static char *storenote(struct memelfnote *men, char *bufp)
133 {
134         struct elf_note en;
135
136 #define DUMP_WRITE(addr,nr) do { memcpy(bufp,addr,nr); bufp += nr; } while(0)
137
138         en.n_namesz = strlen(men->name);
139         en.n_descsz = men->datasz;
140         en.n_type = men->type;
141
142         DUMP_WRITE(&en, sizeof(en));
143         DUMP_WRITE(men->name, en.n_namesz);
144
145         /* XXX - cast from long long to long to avoid need for libgcc.a */
146         bufp = (char*) roundup((unsigned long)bufp,4);
147         DUMP_WRITE(men->data, men->datasz);
148         bufp = (char*) roundup((unsigned long)bufp,4);
149
150 #undef DUMP_WRITE
151
152         return bufp;
153 } /* end storenote() */
154
155 /*
156  * store an ELF coredump header in the supplied buffer
157  * nphdr is the number of elf_phdr to insert
158  */
159 static void elf_kcore_store_hdr(char *bufp, int nphdr, int dataoff)
160 {
161         struct elf_prstatus prstatus;   /* NT_PRSTATUS */
162         struct elf_prpsinfo prpsinfo;   /* NT_PRPSINFO */
163         struct elf_phdr *nhdr, *phdr;
164         struct elfhdr *elf;
165         struct memelfnote notes[3];
166         off_t offset = 0;
167         struct kcore_list *m;
168
169         /* setup ELF header */
170         elf = (struct elfhdr *) bufp;
171         bufp += sizeof(struct elfhdr);
172         offset += sizeof(struct elfhdr);
173         memcpy(elf->e_ident, ELFMAG, SELFMAG);
174         elf->e_ident[EI_CLASS]  = ELF_CLASS;
175         elf->e_ident[EI_DATA]   = ELF_DATA;
176         elf->e_ident[EI_VERSION]= EV_CURRENT;
177         elf->e_ident[EI_OSABI] = ELF_OSABI;
178         memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
179         elf->e_type     = ET_CORE;
180         elf->e_machine  = ELF_ARCH;
181         elf->e_version  = EV_CURRENT;
182         elf->e_entry    = 0;
183         elf->e_phoff    = sizeof(struct elfhdr);
184         elf->e_shoff    = 0;
185 #if defined(CONFIG_H8300)
186         elf->e_flags    = ELF_FLAGS;
187 #else
188         elf->e_flags    = 0;
189 #endif
190         elf->e_ehsize   = sizeof(struct elfhdr);
191         elf->e_phentsize= sizeof(struct elf_phdr);
192         elf->e_phnum    = nphdr;
193         elf->e_shentsize= 0;
194         elf->e_shnum    = 0;
195         elf->e_shstrndx = 0;
196
197         /* setup ELF PT_NOTE program header */
198         nhdr = (struct elf_phdr *) bufp;
199         bufp += sizeof(struct elf_phdr);
200         offset += sizeof(struct elf_phdr);
201         nhdr->p_type    = PT_NOTE;
202         nhdr->p_offset  = 0;
203         nhdr->p_vaddr   = 0;
204         nhdr->p_paddr   = 0;
205         nhdr->p_filesz  = 0;
206         nhdr->p_memsz   = 0;
207         nhdr->p_flags   = 0;
208         nhdr->p_align   = 0;
209
210         /* setup ELF PT_LOAD program header for every area */
211         for (m=kclist; m; m=m->next) {
212                 phdr = (struct elf_phdr *) bufp;
213                 bufp += sizeof(struct elf_phdr);
214                 offset += sizeof(struct elf_phdr);
215
216                 phdr->p_type    = PT_LOAD;
217                 phdr->p_flags   = PF_R|PF_W|PF_X;
218                 phdr->p_offset  = kc_vaddr_to_offset(m->addr) + dataoff;
219                 phdr->p_vaddr   = (size_t)m->addr;
220                 phdr->p_paddr   = 0;
221                 phdr->p_filesz  = phdr->p_memsz = m->size;
222                 phdr->p_align   = PAGE_SIZE;
223         }
224
225         /*
226          * Set up the notes in similar form to SVR4 core dumps made
227          * with info from their /proc.
228          */
229         nhdr->p_offset  = offset;
230
231         /* set up the process status */
232         notes[0].name = "CORE";
233         notes[0].type = NT_PRSTATUS;
234         notes[0].datasz = sizeof(struct elf_prstatus);
235         notes[0].data = &prstatus;
236
237         memset(&prstatus, 0, sizeof(struct elf_prstatus));
238
239         nhdr->p_filesz  = notesize(&notes[0]);
240         bufp = storenote(&notes[0], bufp);
241
242         /* set up the process info */
243         notes[1].name   = "CORE";
244         notes[1].type   = NT_PRPSINFO;
245         notes[1].datasz = sizeof(struct elf_prpsinfo);
246         notes[1].data   = &prpsinfo;
247
248         memset(&prpsinfo, 0, sizeof(struct elf_prpsinfo));
249         prpsinfo.pr_state       = 0;
250         prpsinfo.pr_sname       = 'R';
251         prpsinfo.pr_zomb        = 0;
252
253         strcpy(prpsinfo.pr_fname, "vmlinux");
254         strncpy(prpsinfo.pr_psargs, saved_command_line, ELF_PRARGSZ);
255
256         nhdr->p_filesz  += notesize(&notes[1]);
257         bufp = storenote(&notes[1], bufp);
258
259         /* set up the task structure */
260         notes[2].name   = "CORE";
261         notes[2].type   = NT_TASKSTRUCT;
262         notes[2].datasz = sizeof(struct task_struct);
263         notes[2].data   = current;
264
265         nhdr->p_filesz  += notesize(&notes[2]);
266         bufp = storenote(&notes[2], bufp);
267
268 } /* end elf_kcore_store_hdr() */
269
270 /*****************************************************************************/
271 /*
272  * read from the ELF header and then kernel memory
273  */
274 static ssize_t
275 read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
276 {
277         ssize_t acc = 0;
278         size_t size, tsz;
279         size_t elf_buflen;
280         int nphdr;
281         unsigned long start;
282
283         read_lock(&kclist_lock);
284         tsz =  get_kcore_size(&nphdr, &elf_buflen);
285         proc_root_kcore->size = size = tsz + elf_buflen;
286         if (buflen == 0 || *fpos >= size) {
287                 read_unlock(&kclist_lock);
288                 return 0;
289         }
290
291         /* trim buflen to not go beyond EOF */
292         if (buflen > size - *fpos)
293                 buflen = size - *fpos;
294
295         /* construct an ELF core header if we'll need some of it */
296         if (*fpos < elf_buflen) {
297                 char * elf_buf;
298
299                 tsz = elf_buflen - *fpos;
300                 if (buflen < tsz)
301                         tsz = buflen;
302                 elf_buf = kmalloc(elf_buflen, GFP_ATOMIC);
303                 if (!elf_buf) {
304                         read_unlock(&kclist_lock);
305                         return -ENOMEM;
306                 }
307                 memset(elf_buf, 0, elf_buflen);
308                 elf_kcore_store_hdr(elf_buf, nphdr, elf_buflen);
309                 read_unlock(&kclist_lock);
310                 if (copy_to_user(buffer, elf_buf + *fpos, tsz)) {
311                         kfree(elf_buf);
312                         return -EFAULT;
313                 }
314                 kfree(elf_buf);
315                 buflen -= tsz;
316                 *fpos += tsz;
317                 buffer += tsz;
318                 acc += tsz;
319
320                 /* leave now if filled buffer already */
321                 if (buflen == 0)
322                         return acc;
323         } else
324                 read_unlock(&kclist_lock);
325
326         /*
327          * Check to see if our file offset matches with any of
328          * the addresses in the elf_phdr on our list.
329          */
330         start = kc_offset_to_vaddr(*fpos - elf_buflen);
331         if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
332                 tsz = buflen;
333                 
334         while (buflen) {
335                 struct kcore_list *m;
336
337                 read_lock(&kclist_lock);
338                 for (m=kclist; m; m=m->next) {
339                         if (start >= m->addr && start < (m->addr+m->size))
340                                 break;
341                 }
342                 read_unlock(&kclist_lock);
343
344                 if (m == NULL) {
345                         if (clear_user(buffer, tsz))
346                                 return -EFAULT;
347                 } else if ((start >= VMALLOC_START) && (start < VMALLOC_END)) {
348                         char * elf_buf;
349                         struct vm_struct *m;
350                         unsigned long curstart = start;
351                         unsigned long cursize = tsz;
352
353                         elf_buf = kmalloc(tsz, GFP_KERNEL);
354                         if (!elf_buf)
355                                 return -ENOMEM;
356                         memset(elf_buf, 0, tsz);
357
358                         read_lock(&vmlist_lock);
359                         for (m=vmlist; m && cursize; m=m->next) {
360                                 unsigned long vmstart;
361                                 unsigned long vmsize;
362                                 unsigned long msize = m->size - PAGE_SIZE;
363
364                                 if (((unsigned long)m->addr + msize) < 
365                                                                 curstart)
366                                         continue;
367                                 if ((unsigned long)m->addr > (curstart + 
368                                                                 cursize))
369                                         break;
370                                 vmstart = (curstart < (unsigned long)m->addr ? 
371                                         (unsigned long)m->addr : curstart);
372                                 if (((unsigned long)m->addr + msize) > 
373                                                         (curstart + cursize))
374                                         vmsize = curstart + cursize - vmstart;
375                                 else
376                                         vmsize = (unsigned long)m->addr + 
377                                                         msize - vmstart;
378                                 curstart = vmstart + vmsize;
379                                 cursize -= vmsize;
380                                 /* don't dump ioremap'd stuff! (TA) */
381                                 if (m->flags & VM_IOREMAP)
382                                         continue;
383                                 memcpy(elf_buf + (vmstart - start),
384                                         (char *)vmstart, vmsize);
385                         }
386                         read_unlock(&vmlist_lock);
387                         if (copy_to_user(buffer, elf_buf, tsz)) {
388                                 kfree(elf_buf);
389                                 return -EFAULT;
390                         }
391                         kfree(elf_buf);
392                 } else {
393                         if (kern_addr_valid(start)) {
394                                 unsigned long n;
395
396                                 n = copy_to_user(buffer, (char *)start, tsz);
397                                 /*
398                                  * We cannot distingush between fault on source
399                                  * and fault on destination. When this happens
400                                  * we clear too and hope it will trigger the
401                                  * EFAULT again.
402                                  */
403                                 if (n) { 
404                                         if (clear_user(buffer + tsz - n,
405                                                                 tsz - n))
406                                                 return -EFAULT;
407                                 }
408                         } else {
409                                 if (clear_user(buffer, tsz))
410                                         return -EFAULT;
411                         }
412                 }
413                 buflen -= tsz;
414                 *fpos += tsz;
415                 buffer += tsz;
416                 acc += tsz;
417                 start += tsz;
418                 tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
419         }
420
421         return acc;
422 }