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) + 1), 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) + 1;
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 = kzalloc(elf_buflen, GFP_ATOMIC);
284 read_unlock(&kclist_lock);
287 elf_kcore_store_hdr(elf_buf, nphdr, elf_buflen);
288 read_unlock(&kclist_lock);
289 if (copy_to_user(buffer, elf_buf + *fpos, tsz)) {
299 /* leave now if filled buffer already */
303 read_unlock(&kclist_lock);
306 * Check to see if our file offset matches with any of
307 * the addresses in the elf_phdr on our list.
309 start = kc_offset_to_vaddr(*fpos - elf_buflen);
310 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
314 struct kcore_list *m;
316 read_lock(&kclist_lock);
317 for (m=kclist; m; m=m->next) {
318 if (start >= m->addr && start < (m->addr+m->size))
321 read_unlock(&kclist_lock);
324 if (clear_user(buffer, tsz))
326 } else if ((start >= VMALLOC_START) && (start < VMALLOC_END)) {
329 unsigned long curstart = start;
330 unsigned long cursize = tsz;
332 elf_buf = kzalloc(tsz, GFP_KERNEL);
336 read_lock(&vmlist_lock);
337 for (m=vmlist; m && cursize; m=m->next) {
338 unsigned long vmstart;
339 unsigned long vmsize;
340 unsigned long msize = m->size - PAGE_SIZE;
342 if (((unsigned long)m->addr + msize) <
345 if ((unsigned long)m->addr > (curstart +
348 vmstart = (curstart < (unsigned long)m->addr ?
349 (unsigned long)m->addr : curstart);
350 if (((unsigned long)m->addr + msize) >
351 (curstart + cursize))
352 vmsize = curstart + cursize - vmstart;
354 vmsize = (unsigned long)m->addr +
356 curstart = vmstart + vmsize;
358 /* don't dump ioremap'd stuff! (TA) */
359 if (m->flags & VM_IOREMAP)
361 memcpy(elf_buf + (vmstart - start),
362 (char *)vmstart, vmsize);
364 read_unlock(&vmlist_lock);
365 if (copy_to_user(buffer, elf_buf, tsz)) {
371 if (kern_addr_valid(start)) {
374 n = copy_to_user(buffer, (char *)start, tsz);
376 * We cannot distingush between fault on source
377 * and fault on destination. When this happens
378 * we clear too and hope it will trigger the
382 if (clear_user(buffer + tsz - n,
387 if (clear_user(buffer, tsz))
396 tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);