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