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