2 * fs/proc/kcore.c kernel ELF 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>
13 #include <linux/proc_fs.h>
14 #include <linux/user.h>
15 #include <linux/a.out.h>
16 #include <linux/capability.h>
17 #include <linux/elf.h>
18 #include <linux/elfcore.h>
19 #include <linux/vmalloc.h>
20 #include <linux/highmem.h>
21 #include <linux/init.h>
22 #include <asm/uaccess.h>
26 static int open_kcore(struct inode * inode, struct file * filp)
28 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
31 static ssize_t read_kcore(struct file *, char __user *, size_t, loff_t *);
33 const struct file_operations proc_kcore_operations = {
38 #ifndef kc_vaddr_to_offset
39 #define kc_vaddr_to_offset(v) ((v) - PAGE_OFFSET)
41 #ifndef kc_offset_to_vaddr
42 #define kc_offset_to_vaddr(o) ((o) + PAGE_OFFSET)
45 /* An ELF note in memory */
54 static struct kcore_list *kclist;
55 static DEFINE_RWLOCK(kclist_lock);
58 kclist_add(struct kcore_list *new, void *addr, size_t size)
60 new->addr = (unsigned long)addr;
63 write_lock(&kclist_lock);
66 write_unlock(&kclist_lock);
69 static size_t get_kcore_size(int *nphdr, size_t *elf_buflen)
74 *nphdr = 1; /* PT_NOTE */
77 for (m=kclist; m; m=m->next) {
78 try = kc_vaddr_to_offset((size_t)m->addr + m->size);
83 *elf_buflen = sizeof(struct elfhdr) +
84 (*nphdr + 2)*sizeof(struct elf_phdr) +
85 3 * (sizeof(struct elf_note) + 4) +
86 sizeof(struct elf_prstatus) +
87 sizeof(struct elf_prpsinfo) +
88 sizeof(struct task_struct);
89 *elf_buflen = PAGE_ALIGN(*elf_buflen);
90 return size + *elf_buflen;
94 /*****************************************************************************/
96 * determine size of ELF note
98 static int notesize(struct memelfnote *en)
102 sz = sizeof(struct elf_note);
103 sz += roundup(strlen(en->name), 4);
104 sz += roundup(en->datasz, 4);
107 } /* end notesize() */
109 /*****************************************************************************/
111 * store a note in the header buffer
113 static char *storenote(struct memelfnote *men, char *bufp)
117 #define DUMP_WRITE(addr,nr) do { memcpy(bufp,addr,nr); bufp += nr; } while(0)
119 en.n_namesz = strlen(men->name);
120 en.n_descsz = men->datasz;
121 en.n_type = men->type;
123 DUMP_WRITE(&en, sizeof(en));
124 DUMP_WRITE(men->name, en.n_namesz);
126 /* XXX - cast from long long to long to avoid need for libgcc.a */
127 bufp = (char*) roundup((unsigned long)bufp,4);
128 DUMP_WRITE(men->data, men->datasz);
129 bufp = (char*) roundup((unsigned long)bufp,4);
134 } /* end storenote() */
137 * store an ELF coredump header in the supplied buffer
138 * nphdr is the number of elf_phdr to insert
140 static void elf_kcore_store_hdr(char *bufp, int nphdr, int dataoff)
142 struct elf_prstatus prstatus; /* NT_PRSTATUS */
143 struct elf_prpsinfo prpsinfo; /* NT_PRPSINFO */
144 struct elf_phdr *nhdr, *phdr;
146 struct memelfnote notes[3];
148 struct kcore_list *m;
150 /* setup ELF header */
151 elf = (struct elfhdr *) bufp;
152 bufp += sizeof(struct elfhdr);
153 offset += sizeof(struct elfhdr);
154 memcpy(elf->e_ident, ELFMAG, SELFMAG);
155 elf->e_ident[EI_CLASS] = ELF_CLASS;
156 elf->e_ident[EI_DATA] = ELF_DATA;
157 elf->e_ident[EI_VERSION]= EV_CURRENT;
158 elf->e_ident[EI_OSABI] = ELF_OSABI;
159 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
160 elf->e_type = ET_CORE;
161 elf->e_machine = ELF_ARCH;
162 elf->e_version = EV_CURRENT;
164 elf->e_phoff = sizeof(struct elfhdr);
166 #if defined(CONFIG_H8300)
167 elf->e_flags = ELF_FLAGS;
171 elf->e_ehsize = sizeof(struct elfhdr);
172 elf->e_phentsize= sizeof(struct elf_phdr);
173 elf->e_phnum = nphdr;
178 /* setup ELF PT_NOTE program header */
179 nhdr = (struct elf_phdr *) bufp;
180 bufp += sizeof(struct elf_phdr);
181 offset += sizeof(struct elf_phdr);
182 nhdr->p_type = PT_NOTE;
191 /* setup ELF PT_LOAD program header for every area */
192 for (m=kclist; m; m=m->next) {
193 phdr = (struct elf_phdr *) bufp;
194 bufp += sizeof(struct elf_phdr);
195 offset += sizeof(struct elf_phdr);
197 phdr->p_type = PT_LOAD;
198 phdr->p_flags = PF_R|PF_W|PF_X;
199 phdr->p_offset = kc_vaddr_to_offset(m->addr) + dataoff;
200 phdr->p_vaddr = (size_t)m->addr;
202 phdr->p_filesz = phdr->p_memsz = m->size;
203 phdr->p_align = PAGE_SIZE;
207 * Set up the notes in similar form to SVR4 core dumps made
208 * with info from their /proc.
210 nhdr->p_offset = offset;
212 /* set up the process status */
213 notes[0].name = "CORE";
214 notes[0].type = NT_PRSTATUS;
215 notes[0].datasz = sizeof(struct elf_prstatus);
216 notes[0].data = &prstatus;
218 memset(&prstatus, 0, sizeof(struct elf_prstatus));
220 nhdr->p_filesz = notesize(¬es[0]);
221 bufp = storenote(¬es[0], bufp);
223 /* set up the process info */
224 notes[1].name = "CORE";
225 notes[1].type = NT_PRPSINFO;
226 notes[1].datasz = sizeof(struct elf_prpsinfo);
227 notes[1].data = &prpsinfo;
229 memset(&prpsinfo, 0, sizeof(struct elf_prpsinfo));
230 prpsinfo.pr_state = 0;
231 prpsinfo.pr_sname = 'R';
232 prpsinfo.pr_zomb = 0;
234 strcpy(prpsinfo.pr_fname, "vmlinux");
235 strncpy(prpsinfo.pr_psargs, saved_command_line, ELF_PRARGSZ);
237 nhdr->p_filesz += notesize(¬es[1]);
238 bufp = storenote(¬es[1], bufp);
240 /* set up the task structure */
241 notes[2].name = "CORE";
242 notes[2].type = NT_TASKSTRUCT;
243 notes[2].datasz = sizeof(struct task_struct);
244 notes[2].data = current;
246 nhdr->p_filesz += notesize(¬es[2]);
247 bufp = storenote(¬es[2], bufp);
249 } /* end elf_kcore_store_hdr() */
251 /*****************************************************************************/
253 * read from the ELF header and then kernel memory
256 read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
264 read_lock(&kclist_lock);
265 proc_root_kcore->size = size = get_kcore_size(&nphdr, &elf_buflen);
266 if (buflen == 0 || *fpos >= size) {
267 read_unlock(&kclist_lock);
271 /* trim buflen to not go beyond EOF */
272 if (buflen > size - *fpos)
273 buflen = size - *fpos;
275 /* construct an ELF core header if we'll need some of it */
276 if (*fpos < elf_buflen) {
279 tsz = elf_buflen - *fpos;
282 elf_buf = kmalloc(elf_buflen, GFP_ATOMIC);
284 read_unlock(&kclist_lock);
287 memset(elf_buf, 0, elf_buflen);
288 elf_kcore_store_hdr(elf_buf, nphdr, elf_buflen);
289 read_unlock(&kclist_lock);
290 if (copy_to_user(buffer, elf_buf + *fpos, tsz)) {
300 /* leave now if filled buffer already */
304 read_unlock(&kclist_lock);
307 * Check to see if our file offset matches with any of
308 * the addresses in the elf_phdr on our list.
310 start = kc_offset_to_vaddr(*fpos - elf_buflen);
311 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
315 struct kcore_list *m;
317 read_lock(&kclist_lock);
318 for (m=kclist; m; m=m->next) {
319 if (start >= m->addr && start < (m->addr+m->size))
322 read_unlock(&kclist_lock);
325 if (clear_user(buffer, tsz))
327 } else if ((start >= VMALLOC_START) && (start < VMALLOC_END)) {
330 unsigned long curstart = start;
331 unsigned long cursize = tsz;
333 elf_buf = kmalloc(tsz, GFP_KERNEL);
336 memset(elf_buf, 0, tsz);
338 read_lock(&vmlist_lock);
339 for (m=vmlist; m && cursize; m=m->next) {
340 unsigned long vmstart;
341 unsigned long vmsize;
342 unsigned long msize = m->size - PAGE_SIZE;
344 if (((unsigned long)m->addr + msize) <
347 if ((unsigned long)m->addr > (curstart +
350 vmstart = (curstart < (unsigned long)m->addr ?
351 (unsigned long)m->addr : curstart);
352 if (((unsigned long)m->addr + msize) >
353 (curstart + cursize))
354 vmsize = curstart + cursize - vmstart;
356 vmsize = (unsigned long)m->addr +
358 curstart = vmstart + vmsize;
360 /* don't dump ioremap'd stuff! (TA) */
361 if (m->flags & VM_IOREMAP)
363 memcpy(elf_buf + (vmstart - start),
364 (char *)vmstart, vmsize);
366 read_unlock(&vmlist_lock);
367 if (copy_to_user(buffer, elf_buf, tsz)) {
373 if (kern_addr_valid(start)) {
376 n = copy_to_user(buffer, (char *)start, tsz);
378 * We cannot distingush between fault on source
379 * and fault on destination. When this happens
380 * we clear too and hope it will trigger the
384 if (clear_user(buffer + tsz - n,
389 if (clear_user(buffer, tsz))
398 tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);