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