make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / fs / proc / kcore.c
1 /*
2  *      fs/proc/kcore.c kernel ELF/AOUT 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 *, size_t, loff_t *);
31
32 struct file_operations proc_kcore_operations = {
33         read:           read_kcore,
34         open:           open_kcore,
35 };
36
37 #ifdef CONFIG_KCORE_AOUT
38 static ssize_t read_kcore(struct file *file, char *buf, size_t count, loff_t *ppos)
39 {
40         unsigned long long p = *ppos, memsize;
41         ssize_t read;
42         ssize_t count1;
43         char * pnt;
44         struct user dump;
45 #if defined (__i386__) || defined (__mc68000__) || defined(__x86_64__)
46 #       define FIRST_MAPPED     PAGE_SIZE       /* we don't have page 0 mapped on x86.. */
47 #else
48 #       define FIRST_MAPPED     0
49 #endif
50
51         memset(&dump, 0, sizeof(struct user));
52         dump.magic = CMAGIC;
53         dump.u_dsize = (virt_to_phys(high_memory) >> PAGE_SHIFT);
54 #if defined (__i386__) || defined(__x86_64__)
55         dump.start_code = PAGE_OFFSET;
56 #endif
57 #ifdef __alpha__
58         dump.start_data = PAGE_OFFSET;
59 #endif
60
61         memsize = virt_to_phys(high_memory);
62         if (p >= memsize)
63                 return 0;
64         if (count > memsize - p)
65                 count = memsize - p;
66         read = 0;
67
68         if (p < sizeof(struct user) && count > 0) {
69                 count1 = count;
70                 if (p + count1 > sizeof(struct user))
71                         count1 = sizeof(struct user)-p;
72                 pnt = (char *) &dump + p;
73                 if (copy_to_user(buf,(void *) pnt, count1))
74                         return -EFAULT;
75                 buf += count1;
76                 p += count1;
77                 count -= count1;
78                 read += count1;
79         }
80
81         if (count > 0 && p < PAGE_SIZE + FIRST_MAPPED) {
82                 count1 = PAGE_SIZE + FIRST_MAPPED - p;
83                 if (count1 > count)
84                         count1 = count;
85                 if (clear_user(buf, count1))
86                         return -EFAULT;
87                 buf += count1;
88                 p += count1;
89                 count -= count1;
90                 read += count1;
91         }
92         if (count > 0) {
93                 if (copy_to_user(buf, (void *) (PAGE_OFFSET+p-PAGE_SIZE), count))
94                         return -EFAULT;
95                 read += count;
96         }
97         *ppos += read;
98         return read;
99 }
100 #else /* CONFIG_KCORE_AOUT */
101
102 #define roundup(x, y)  ((((x)+((y)-1))/(y))*(y))
103
104 /* An ELF note in memory */
105 struct memelfnote
106 {
107         const char *name;
108         int type;
109         unsigned int datasz;
110         void *data;
111 };
112
113 extern char saved_command_line[];
114
115 static size_t get_kcore_size(int *num_vma, size_t *elf_buflen)
116 {
117         size_t try, size;
118         struct vm_struct *m;
119
120         *num_vma = 0;
121         size = ((size_t)high_memory - PAGE_OFFSET + PAGE_SIZE);
122         if (!vmlist) {
123                 *elf_buflen = PAGE_SIZE;
124                 return (size);
125         }
126
127         for (m=vmlist; m; m=m->next) {
128                 try = (size_t)m->addr + m->size;
129                 if (try > size)
130                         size = try;
131                 *num_vma = *num_vma + 1;
132         }
133         *elf_buflen =   sizeof(struct elfhdr) + 
134                         (*num_vma + 2)*sizeof(struct elf_phdr) + 
135                         3 * sizeof(struct memelfnote);
136         *elf_buflen = PAGE_ALIGN(*elf_buflen);
137         return (size - PAGE_OFFSET + *elf_buflen);
138 }
139
140
141 /*****************************************************************************/
142 /*
143  * determine size of ELF note
144  */
145 static int notesize(struct memelfnote *en)
146 {
147         int sz;
148
149         sz = sizeof(struct elf_note);
150         sz += roundup(strlen(en->name), 4);
151         sz += roundup(en->datasz, 4);
152
153         return sz;
154 } /* end notesize() */
155
156 /*****************************************************************************/
157 /*
158  * store a note in the header buffer
159  */
160 static char *storenote(struct memelfnote *men, char *bufp)
161 {
162         struct elf_note en;
163
164 #define DUMP_WRITE(addr,nr) do { memcpy(bufp,addr,nr); bufp += nr; } while(0)
165
166         en.n_namesz = strlen(men->name);
167         en.n_descsz = men->datasz;
168         en.n_type = men->type;
169
170         DUMP_WRITE(&en, sizeof(en));
171         DUMP_WRITE(men->name, en.n_namesz);
172
173         /* XXX - cast from long long to long to avoid need for libgcc.a */
174         bufp = (char*) roundup((unsigned long)bufp,4);
175         DUMP_WRITE(men->data, men->datasz);
176         bufp = (char*) roundup((unsigned long)bufp,4);
177
178 #undef DUMP_WRITE
179
180         return bufp;
181 } /* end storenote() */
182
183 /*
184  * store an ELF coredump header in the supplied buffer
185  * num_vma is the number of elements in vmlist
186  */
187 static void elf_kcore_store_hdr(char *bufp, int num_vma, int dataoff)
188 {
189         struct elf_prstatus prstatus;   /* NT_PRSTATUS */
190         struct elf_prpsinfo prpsinfo;   /* NT_PRPSINFO */
191         struct elf_phdr *nhdr, *phdr;
192         struct elfhdr *elf;
193         struct memelfnote notes[3];
194         off_t offset = 0;
195         struct vm_struct *m;
196
197         /* setup ELF header */
198         elf = (struct elfhdr *) bufp;
199         bufp += sizeof(struct elfhdr);
200         offset += sizeof(struct elfhdr);
201         memcpy(elf->e_ident, ELFMAG, SELFMAG);
202         elf->e_ident[EI_CLASS]  = ELF_CLASS;
203         elf->e_ident[EI_DATA]   = ELF_DATA;
204         elf->e_ident[EI_VERSION]= EV_CURRENT;
205         memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
206         elf->e_type     = ET_CORE;
207         elf->e_machine  = ELF_ARCH;
208         elf->e_version  = EV_CURRENT;
209         elf->e_entry    = 0;
210         elf->e_phoff    = sizeof(struct elfhdr);
211         elf->e_shoff    = 0;
212         elf->e_flags    = 0;
213         elf->e_ehsize   = sizeof(struct elfhdr);
214         elf->e_phentsize= sizeof(struct elf_phdr);
215         elf->e_phnum    = 2 + num_vma;
216         elf->e_shentsize= 0;
217         elf->e_shnum    = 0;
218         elf->e_shstrndx = 0;
219
220         /* setup ELF PT_NOTE program header */
221         nhdr = (struct elf_phdr *) bufp;
222         bufp += sizeof(struct elf_phdr);
223         offset += sizeof(struct elf_phdr);
224         nhdr->p_type    = PT_NOTE;
225         nhdr->p_offset  = 0;
226         nhdr->p_vaddr   = 0;
227         nhdr->p_paddr   = 0;
228         nhdr->p_filesz  = 0;
229         nhdr->p_memsz   = 0;
230         nhdr->p_flags   = 0;
231         nhdr->p_align   = 0;
232
233         /* setup ELF PT_LOAD program header for the 
234          * virtual range 0xc0000000 -> high_memory */
235         phdr = (struct elf_phdr *) bufp;
236         bufp += sizeof(struct elf_phdr);
237         offset += sizeof(struct elf_phdr);
238         phdr->p_type    = PT_LOAD;
239         phdr->p_flags   = PF_R|PF_W|PF_X;
240         phdr->p_offset  = dataoff;
241         phdr->p_vaddr   = PAGE_OFFSET;
242         phdr->p_paddr   = __pa(PAGE_OFFSET);
243         phdr->p_filesz  = phdr->p_memsz = ((unsigned long)high_memory - PAGE_OFFSET);
244         phdr->p_align   = PAGE_SIZE;
245
246         /* setup ELF PT_LOAD program header for every vmalloc'd area */
247         for (m=vmlist; m; m=m->next) {
248                 if (m->flags & VM_IOREMAP) /* don't dump ioremap'd stuff! (TA) */
249                         continue;
250
251                 phdr = (struct elf_phdr *) bufp;
252                 bufp += sizeof(struct elf_phdr);
253                 offset += sizeof(struct elf_phdr);
254
255                 phdr->p_type    = PT_LOAD;
256                 phdr->p_flags   = PF_R|PF_W|PF_X;
257                 phdr->p_offset  = (size_t)m->addr - PAGE_OFFSET + dataoff;
258                 phdr->p_vaddr   = (size_t)m->addr;
259                 phdr->p_paddr   = __pa(m->addr);
260                 phdr->p_filesz  = phdr->p_memsz = m->size;
261                 phdr->p_align   = PAGE_SIZE;
262         }
263
264         /*
265          * Set up the notes in similar form to SVR4 core dumps made
266          * with info from their /proc.
267          */
268         nhdr->p_offset  = offset;
269
270         /* set up the process status */
271         notes[0].name = "CORE";
272         notes[0].type = NT_PRSTATUS;
273         notes[0].datasz = sizeof(struct elf_prstatus);
274         notes[0].data = &prstatus;
275
276         memset(&prstatus, 0, sizeof(struct elf_prstatus));
277
278         nhdr->p_filesz  = notesize(&notes[0]);
279         bufp = storenote(&notes[0], bufp);
280
281         /* set up the process info */
282         notes[1].name   = "CORE";
283         notes[1].type   = NT_PRPSINFO;
284         notes[1].datasz = sizeof(struct elf_prpsinfo);
285         notes[1].data   = &prpsinfo;
286
287         memset(&prpsinfo, 0, sizeof(struct elf_prpsinfo));
288         prpsinfo.pr_state       = 0;
289         prpsinfo.pr_sname       = 'R';
290         prpsinfo.pr_zomb        = 0;
291
292         strcpy(prpsinfo.pr_fname, "vmlinux");
293         strncpy(prpsinfo.pr_psargs, saved_command_line, ELF_PRARGSZ);
294
295         nhdr->p_filesz  = notesize(&notes[1]);
296         bufp = storenote(&notes[1], bufp);
297
298         /* set up the task structure */
299         notes[2].name   = "CORE";
300         notes[2].type   = NT_TASKSTRUCT;
301         notes[2].datasz = sizeof(struct task_struct);
302         notes[2].data   = current;
303
304         nhdr->p_filesz  = notesize(&notes[2]);
305         bufp = storenote(&notes[2], bufp);
306
307 } /* end elf_kcore_store_hdr() */
308
309 /*****************************************************************************/
310 /*
311  * read from the ELF header and then kernel memory
312  */
313 static ssize_t read_kcore(struct file *file, char *buffer, size_t buflen, loff_t *fpos)
314 {
315         ssize_t acc = 0;
316         size_t size, tsz;
317         size_t elf_buflen;
318         int num_vma;
319         unsigned long start;
320
321         read_lock(&vmlist_lock);
322         proc_root_kcore->size = size = get_kcore_size(&num_vma, &elf_buflen);
323         if (buflen == 0 || *fpos >= size) {
324                 read_unlock(&vmlist_lock);
325                 return 0;
326         }
327
328         /* trim buflen to not go beyond EOF */
329         if (buflen > size - *fpos)
330                 buflen = size - *fpos;
331
332         /* construct an ELF core header if we'll need some of it */
333         if (*fpos < elf_buflen) {
334                 char * elf_buf;
335
336                 tsz = elf_buflen - *fpos;
337                 if (buflen < tsz)
338                         tsz = buflen;
339                 elf_buf = kmalloc(elf_buflen, GFP_ATOMIC);
340                 if (!elf_buf) {
341                         read_unlock(&vmlist_lock);
342                         return -ENOMEM;
343                 }
344                 memset(elf_buf, 0, elf_buflen);
345                 elf_kcore_store_hdr(elf_buf, num_vma, elf_buflen);
346                 read_unlock(&vmlist_lock);
347                 if (copy_to_user(buffer, elf_buf + *fpos, tsz)) {
348                         kfree(elf_buf);
349                         return -EFAULT;
350                 }
351                 kfree(elf_buf);
352                 buflen -= tsz;
353                 *fpos += tsz;
354                 buffer += tsz;
355                 acc += tsz;
356
357                 /* leave now if filled buffer already */
358                 if (buflen == 0)
359                         return acc;
360         } else
361                 read_unlock(&vmlist_lock);
362
363         /* where page 0 not mapped, write zeros into buffer */
364 #if defined (__i386__) || defined (__mc68000__) || defined(__x86_64__)
365         if (*fpos < PAGE_SIZE + elf_buflen) {
366                 /* work out how much to clear */
367                 tsz = PAGE_SIZE + elf_buflen - *fpos;
368                 if (buflen < tsz)
369                         tsz = buflen;
370
371                 /* write zeros to buffer */
372                 if (clear_user(buffer, tsz))
373                         return -EFAULT;
374                 buflen -= tsz;
375                 *fpos += tsz;
376                 buffer += tsz;
377                 acc += tsz;
378
379                 /* leave now if filled buffer already */
380                 if (buflen == 0)
381                         return tsz;
382         }
383 #endif
384         
385         /*
386          * Fill the remainder of the buffer from kernel VM space.
387          * We said in the ELF header that the data which starts
388          * at 'elf_buflen' is virtual address PAGE_OFFSET. --rmk
389          */
390         start = PAGE_OFFSET + (*fpos - elf_buflen);
391         if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
392                 tsz = buflen;
393                 
394         while (buflen) {
395                 if ((start >= VMALLOC_START) && (start < VMALLOC_END)) {
396                         char * elf_buf;
397                         struct vm_struct *m;
398                         unsigned long curstart = start;
399                         unsigned long cursize = tsz;
400
401                         elf_buf = kmalloc(tsz, GFP_KERNEL);
402                         if (!elf_buf)
403                                 return -ENOMEM;
404                         memset(elf_buf, 0, tsz);
405
406                         read_lock(&vmlist_lock);
407                         for (m=vmlist; m && cursize; m=m->next) {
408                                 unsigned long vmstart;
409                                 unsigned long vmsize;
410                                 unsigned long msize = m->size - PAGE_SIZE;
411
412                                 if (((unsigned long)m->addr + msize) < 
413                                                                 curstart)
414                                         continue;
415                                 if ((unsigned long)m->addr > (curstart + 
416                                                                 cursize))
417                                         break;
418                                 vmstart = (curstart < (unsigned long)m->addr ? 
419                                         (unsigned long)m->addr : curstart);
420                                 if (((unsigned long)m->addr + msize) > 
421                                                         (curstart + cursize))
422                                         vmsize = curstart + cursize - vmstart;
423                                 else
424                                         vmsize = (unsigned long)m->addr + 
425                                                         msize - vmstart;
426                                 curstart = vmstart + vmsize;
427                                 cursize -= vmsize;
428                                 /* don't dump ioremap'd stuff! (TA) */
429                                 if (m->flags & VM_IOREMAP)
430                                         continue;
431                                 memcpy(elf_buf + (vmstart - start),
432                                         (char *)vmstart, vmsize);
433                         }
434                         read_unlock(&vmlist_lock);
435                         if (copy_to_user(buffer, elf_buf, tsz)) {
436                                 kfree(elf_buf);
437                                 return -EFAULT;
438                         }
439                         kfree(elf_buf);
440                 } else if ((start > PAGE_OFFSET) && (start < 
441                                                 (unsigned long)high_memory)) {
442                         if (kern_addr_valid(start)) {
443                                 if (copy_to_user(buffer, (char *)start, tsz))
444                                         return -EFAULT;
445                         } else {
446                                 if (clear_user(buffer, tsz))
447                                         return -EFAULT;
448                         }
449                 } else {
450                         if (clear_user(buffer, tsz))
451                                 return -EFAULT;
452                 }
453                 buflen -= tsz;
454                 *fpos += tsz;
455                 buffer += tsz;
456                 acc += tsz;
457                 start += tsz;
458                 tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
459         }
460
461         return acc;
462 }
463 #endif /* CONFIG_KCORE_AOUT */