2 * fs/proc/kcore.c kernel ELF/AOUT core dumper
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>
12 #include <linux/config.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>
25 static int open_kcore(struct inode * inode, struct file * filp)
27 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
30 static ssize_t read_kcore(struct file *, char *, size_t, loff_t *);
32 struct file_operations proc_kcore_operations = {
37 #ifdef CONFIG_KCORE_AOUT
38 static ssize_t read_kcore(struct file *file, char *buf, size_t count, loff_t *ppos)
40 unsigned long long p = *ppos, memsize;
45 #if defined (__i386__) || defined (__mc68000__) || defined(__x86_64__)
46 # define FIRST_MAPPED PAGE_SIZE /* we don't have page 0 mapped on x86.. */
48 # define FIRST_MAPPED 0
51 memset(&dump, 0, sizeof(struct user));
53 dump.u_dsize = (virt_to_phys(high_memory) >> PAGE_SHIFT);
54 #if defined (__i386__) || defined(__x86_64__)
55 dump.start_code = PAGE_OFFSET;
58 dump.start_data = PAGE_OFFSET;
61 memsize = virt_to_phys(high_memory);
64 if (count > memsize - p)
68 if (p < sizeof(struct user) && count > 0) {
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))
81 if (count > 0 && p < PAGE_SIZE + FIRST_MAPPED) {
82 count1 = PAGE_SIZE + FIRST_MAPPED - p;
85 if (clear_user(buf, count1))
93 if (copy_to_user(buf, (void *) (PAGE_OFFSET+p-PAGE_SIZE), count))
100 #else /* CONFIG_KCORE_AOUT */
102 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
104 /* An ELF note in memory */
113 extern char saved_command_line[];
115 static size_t get_kcore_size(int *num_vma, size_t *elf_buflen)
121 size = ((size_t)high_memory - PAGE_OFFSET + PAGE_SIZE);
123 *elf_buflen = PAGE_SIZE;
127 for (m=vmlist; m; m=m->next) {
128 try = (size_t)m->addr + m->size;
131 *num_vma = *num_vma + 1;
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);
141 /*****************************************************************************/
143 * determine size of ELF note
145 static int notesize(struct memelfnote *en)
149 sz = sizeof(struct elf_note);
150 sz += roundup(strlen(en->name), 4);
151 sz += roundup(en->datasz, 4);
154 } /* end notesize() */
156 /*****************************************************************************/
158 * store a note in the header buffer
160 static char *storenote(struct memelfnote *men, char *bufp)
164 #define DUMP_WRITE(addr,nr) do { memcpy(bufp,addr,nr); bufp += nr; } while(0)
166 en.n_namesz = strlen(men->name);
167 en.n_descsz = men->datasz;
168 en.n_type = men->type;
170 DUMP_WRITE(&en, sizeof(en));
171 DUMP_WRITE(men->name, en.n_namesz);
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);
181 } /* end storenote() */
184 * store an ELF coredump header in the supplied buffer
185 * num_vma is the number of elements in vmlist
187 static void elf_kcore_store_hdr(char *bufp, int num_vma, int dataoff)
189 struct elf_prstatus prstatus; /* NT_PRSTATUS */
190 struct elf_prpsinfo prpsinfo; /* NT_PRPSINFO */
191 struct elf_phdr *nhdr, *phdr;
193 struct memelfnote notes[3];
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;
210 elf->e_phoff = sizeof(struct elfhdr);
213 elf->e_ehsize = sizeof(struct elfhdr);
214 elf->e_phentsize= sizeof(struct elf_phdr);
215 elf->e_phnum = 2 + num_vma;
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;
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;
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) */
251 phdr = (struct elf_phdr *) bufp;
252 bufp += sizeof(struct elf_phdr);
253 offset += sizeof(struct elf_phdr);
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;
265 * Set up the notes in similar form to SVR4 core dumps made
266 * with info from their /proc.
268 nhdr->p_offset = offset;
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;
276 memset(&prstatus, 0, sizeof(struct elf_prstatus));
278 nhdr->p_filesz = notesize(¬es[0]);
279 bufp = storenote(¬es[0], bufp);
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;
287 memset(&prpsinfo, 0, sizeof(struct elf_prpsinfo));
288 prpsinfo.pr_state = 0;
289 prpsinfo.pr_sname = 'R';
290 prpsinfo.pr_zomb = 0;
292 strcpy(prpsinfo.pr_fname, "vmlinux");
293 strncpy(prpsinfo.pr_psargs, saved_command_line, ELF_PRARGSZ);
295 nhdr->p_filesz = notesize(¬es[1]);
296 bufp = storenote(¬es[1], bufp);
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;
304 nhdr->p_filesz = notesize(¬es[2]);
305 bufp = storenote(¬es[2], bufp);
307 } /* end elf_kcore_store_hdr() */
309 /*****************************************************************************/
311 * read from the ELF header and then kernel memory
313 static ssize_t read_kcore(struct file *file, char *buffer, size_t buflen, loff_t *fpos)
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);
328 /* trim buflen to not go beyond EOF */
329 if (buflen > size - *fpos)
330 buflen = size - *fpos;
332 /* construct an ELF core header if we'll need some of it */
333 if (*fpos < elf_buflen) {
336 tsz = elf_buflen - *fpos;
339 elf_buf = kmalloc(elf_buflen, GFP_ATOMIC);
341 read_unlock(&vmlist_lock);
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)) {
357 /* leave now if filled buffer already */
361 read_unlock(&vmlist_lock);
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;
371 /* write zeros to buffer */
372 if (clear_user(buffer, tsz))
379 /* leave now if filled buffer already */
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
390 start = PAGE_OFFSET + (*fpos - elf_buflen);
391 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
395 if ((start >= VMALLOC_START) && (start < VMALLOC_END)) {
398 unsigned long curstart = start;
399 unsigned long cursize = tsz;
401 elf_buf = kmalloc(tsz, GFP_KERNEL);
404 memset(elf_buf, 0, tsz);
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;
412 if (((unsigned long)m->addr + msize) <
415 if ((unsigned long)m->addr > (curstart +
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;
424 vmsize = (unsigned long)m->addr +
426 curstart = vmstart + vmsize;
428 /* don't dump ioremap'd stuff! (TA) */
429 if (m->flags & VM_IOREMAP)
431 memcpy(elf_buf + (vmstart - start),
432 (char *)vmstart, vmsize);
434 read_unlock(&vmlist_lock);
435 if (copy_to_user(buffer, elf_buf, tsz)) {
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))
446 if (clear_user(buffer, tsz))
450 if (clear_user(buffer, tsz))
458 tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
463 #endif /* CONFIG_KCORE_AOUT */