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