ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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         elf->e_flags    = 0;
187         elf->e_ehsize   = sizeof(struct elfhdr);
188         elf->e_phentsize= sizeof(struct elf_phdr);
189         elf->e_phnum    = nphdr;
190         elf->e_shentsize= 0;
191         elf->e_shnum    = 0;
192         elf->e_shstrndx = 0;
193
194         /* setup ELF PT_NOTE program header */
195         nhdr = (struct elf_phdr *) bufp;
196         bufp += sizeof(struct elf_phdr);
197         offset += sizeof(struct elf_phdr);
198         nhdr->p_type    = PT_NOTE;
199         nhdr->p_offset  = 0;
200         nhdr->p_vaddr   = 0;
201         nhdr->p_paddr   = 0;
202         nhdr->p_filesz  = 0;
203         nhdr->p_memsz   = 0;
204         nhdr->p_flags   = 0;
205         nhdr->p_align   = 0;
206
207         /* setup ELF PT_LOAD program header for every area */
208         for (m=kclist; m; m=m->next) {
209                 phdr = (struct elf_phdr *) bufp;
210                 bufp += sizeof(struct elf_phdr);
211                 offset += sizeof(struct elf_phdr);
212
213                 phdr->p_type    = PT_LOAD;
214                 phdr->p_flags   = PF_R|PF_W|PF_X;
215                 phdr->p_offset  = kc_vaddr_to_offset(m->addr) + dataoff;
216                 phdr->p_vaddr   = (size_t)m->addr;
217                 phdr->p_paddr   = 0;
218                 phdr->p_filesz  = phdr->p_memsz = m->size;
219                 phdr->p_align   = PAGE_SIZE;
220         }
221
222         /*
223          * Set up the notes in similar form to SVR4 core dumps made
224          * with info from their /proc.
225          */
226         nhdr->p_offset  = offset;
227
228         /* set up the process status */
229         notes[0].name = "CORE";
230         notes[0].type = NT_PRSTATUS;
231         notes[0].datasz = sizeof(struct elf_prstatus);
232         notes[0].data = &prstatus;
233
234         memset(&prstatus, 0, sizeof(struct elf_prstatus));
235
236         nhdr->p_filesz  = notesize(&notes[0]);
237         bufp = storenote(&notes[0], bufp);
238
239         /* set up the process info */
240         notes[1].name   = "CORE";
241         notes[1].type   = NT_PRPSINFO;
242         notes[1].datasz = sizeof(struct elf_prpsinfo);
243         notes[1].data   = &prpsinfo;
244
245         memset(&prpsinfo, 0, sizeof(struct elf_prpsinfo));
246         prpsinfo.pr_state       = 0;
247         prpsinfo.pr_sname       = 'R';
248         prpsinfo.pr_zomb        = 0;
249
250         strcpy(prpsinfo.pr_fname, "vmlinux");
251         strncpy(prpsinfo.pr_psargs, saved_command_line, ELF_PRARGSZ);
252
253         nhdr->p_filesz  += notesize(&notes[1]);
254         bufp = storenote(&notes[1], bufp);
255
256         /* set up the task structure */
257         notes[2].name   = "CORE";
258         notes[2].type   = NT_TASKSTRUCT;
259         notes[2].datasz = sizeof(struct task_struct);
260         notes[2].data   = current;
261
262         nhdr->p_filesz  += notesize(&notes[2]);
263         bufp = storenote(&notes[2], bufp);
264
265 } /* end elf_kcore_store_hdr() */
266
267 /*****************************************************************************/
268 /*
269  * read from the ELF header and then kernel memory
270  */
271 static ssize_t
272 read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
273 {
274         ssize_t acc = 0;
275         size_t size, tsz;
276         size_t elf_buflen;
277         int nphdr;
278         unsigned long start;
279
280         read_lock(&kclist_lock);
281         tsz =  get_kcore_size(&nphdr, &elf_buflen);
282         proc_root_kcore->size = size = tsz + elf_buflen;
283         if (buflen == 0 || *fpos >= size) {
284                 read_unlock(&kclist_lock);
285                 return 0;
286         }
287
288         /* trim buflen to not go beyond EOF */
289         if (buflen > size - *fpos)
290                 buflen = size - *fpos;
291
292         /* construct an ELF core header if we'll need some of it */
293         if (*fpos < elf_buflen) {
294                 char * elf_buf;
295
296                 tsz = elf_buflen - *fpos;
297                 if (buflen < tsz)
298                         tsz = buflen;
299                 elf_buf = kmalloc(elf_buflen, GFP_ATOMIC);
300                 if (!elf_buf) {
301                         read_unlock(&kclist_lock);
302                         return -ENOMEM;
303                 }
304                 memset(elf_buf, 0, elf_buflen);
305                 elf_kcore_store_hdr(elf_buf, nphdr, elf_buflen);
306                 read_unlock(&kclist_lock);
307                 if (copy_to_user(buffer, elf_buf + *fpos, tsz)) {
308                         kfree(elf_buf);
309                         return -EFAULT;
310                 }
311                 kfree(elf_buf);
312                 buflen -= tsz;
313                 *fpos += tsz;
314                 buffer += tsz;
315                 acc += tsz;
316
317                 /* leave now if filled buffer already */
318                 if (buflen == 0)
319                         return acc;
320         } else
321                 read_unlock(&kclist_lock);
322
323         /*
324          * Check to see if our file offset matches with any of
325          * the addresses in the elf_phdr on our list.
326          */
327         start = kc_offset_to_vaddr(*fpos - elf_buflen);
328         if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
329                 tsz = buflen;
330                 
331         while (buflen) {
332                 struct kcore_list *m;
333
334                 read_lock(&kclist_lock);
335                 for (m=kclist; m; m=m->next) {
336                         if (start >= m->addr && start < (m->addr+m->size))
337                                 break;
338                 }
339                 read_unlock(&kclist_lock);
340
341                 if (m == NULL) {
342                         if (clear_user(buffer, tsz))
343                                 return -EFAULT;
344                 } else if ((start >= VMALLOC_START) && (start < VMALLOC_END)) {
345                         char * elf_buf;
346                         struct vm_struct *m;
347                         unsigned long curstart = start;
348                         unsigned long cursize = tsz;
349
350                         elf_buf = kmalloc(tsz, GFP_KERNEL);
351                         if (!elf_buf)
352                                 return -ENOMEM;
353                         memset(elf_buf, 0, tsz);
354
355                         read_lock(&vmlist_lock);
356                         for (m=vmlist; m && cursize; m=m->next) {
357                                 unsigned long vmstart;
358                                 unsigned long vmsize;
359                                 unsigned long msize = m->size - PAGE_SIZE;
360
361                                 if (((unsigned long)m->addr + msize) < 
362                                                                 curstart)
363                                         continue;
364                                 if ((unsigned long)m->addr > (curstart + 
365                                                                 cursize))
366                                         break;
367                                 vmstart = (curstart < (unsigned long)m->addr ? 
368                                         (unsigned long)m->addr : curstart);
369                                 if (((unsigned long)m->addr + msize) > 
370                                                         (curstart + cursize))
371                                         vmsize = curstart + cursize - vmstart;
372                                 else
373                                         vmsize = (unsigned long)m->addr + 
374                                                         msize - vmstart;
375                                 curstart = vmstart + vmsize;
376                                 cursize -= vmsize;
377                                 /* don't dump ioremap'd stuff! (TA) */
378                                 if (m->flags & VM_IOREMAP)
379                                         continue;
380                                 memcpy(elf_buf + (vmstart - start),
381                                         (char *)vmstart, vmsize);
382                         }
383                         read_unlock(&vmlist_lock);
384                         if (copy_to_user(buffer, elf_buf, tsz)) {
385                                 kfree(elf_buf);
386                                 return -EFAULT;
387                         }
388                         kfree(elf_buf);
389                 } else {
390                         if (kern_addr_valid(start)) {
391                                 unsigned long n;
392
393                                 n = copy_to_user(buffer, (char *)start, tsz);
394                                 /*
395                                  * We cannot distingush between fault on source
396                                  * and fault on destination. When this happens
397                                  * we clear too and hope it will trigger the
398                                  * EFAULT again.
399                                  */
400                                 if (n) { 
401                                         if (clear_user(buffer + tsz - n,
402                                                                 tsz - n))
403                                                 return -EFAULT;
404                                 }
405                         } else {
406                                 if (clear_user(buffer, tsz))
407                                         return -EFAULT;
408                         }
409                 }
410                 buflen -= tsz;
411                 *fpos += tsz;
412                 buffer += tsz;
413                 acc += tsz;
414                 start += tsz;
415                 tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
416         }
417
418         return acc;
419 }