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>
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 <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 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 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
47 /* An ELF note in memory */
56 static struct kcore_list *kclist;
57 static rwlock_t kclist_lock = RW_LOCK_UNLOCKED;
60 kclist_add(struct kcore_list *new, void *addr, size_t size)
62 new->addr = (unsigned long)addr;
65 write_lock(&kclist_lock);
68 write_unlock(&kclist_lock);
72 kclist_del(void *addr)
74 struct kcore_list *m, **p = &kclist;
76 write_lock(&kclist_lock);
77 for (m = *p; m; p = &m->next) {
78 if (m->addr == (unsigned long)addr) {
80 write_unlock(&kclist_lock);
84 write_unlock(&kclist_lock);
88 static size_t get_kcore_size(int *nphdr, size_t *elf_buflen)
93 *nphdr = 1; /* PT_NOTE */
96 for (m=kclist; m; m=m->next) {
97 try = kc_vaddr_to_offset((size_t)m->addr + m->size);
102 *elf_buflen = sizeof(struct elfhdr) +
103 (*nphdr + 2)*sizeof(struct elf_phdr) +
104 3 * sizeof(struct memelfnote) +
105 sizeof(struct elf_prstatus) +
106 sizeof(struct elf_prpsinfo) +
107 sizeof(struct task_struct);
108 *elf_buflen = PAGE_ALIGN(*elf_buflen);
109 return size + *elf_buflen;
113 /*****************************************************************************/
115 * determine size of ELF note
117 static int notesize(struct memelfnote *en)
121 sz = sizeof(struct elf_note);
122 sz += roundup(strlen(en->name), 4);
123 sz += roundup(en->datasz, 4);
126 } /* end notesize() */
128 /*****************************************************************************/
130 * store a note in the header buffer
132 static char *storenote(struct memelfnote *men, char *bufp)
136 #define DUMP_WRITE(addr,nr) do { memcpy(bufp,addr,nr); bufp += nr; } while(0)
138 en.n_namesz = strlen(men->name);
139 en.n_descsz = men->datasz;
140 en.n_type = men->type;
142 DUMP_WRITE(&en, sizeof(en));
143 DUMP_WRITE(men->name, en.n_namesz);
145 /* XXX - cast from long long to long to avoid need for libgcc.a */
146 bufp = (char*) roundup((unsigned long)bufp,4);
147 DUMP_WRITE(men->data, men->datasz);
148 bufp = (char*) roundup((unsigned long)bufp,4);
153 } /* end storenote() */
156 * store an ELF coredump header in the supplied buffer
157 * nphdr is the number of elf_phdr to insert
159 static void elf_kcore_store_hdr(char *bufp, int nphdr, int dataoff)
161 struct elf_prstatus prstatus; /* NT_PRSTATUS */
162 struct elf_prpsinfo prpsinfo; /* NT_PRPSINFO */
163 struct elf_phdr *nhdr, *phdr;
165 struct memelfnote notes[3];
167 struct kcore_list *m;
169 /* setup ELF header */
170 elf = (struct elfhdr *) bufp;
171 bufp += sizeof(struct elfhdr);
172 offset += sizeof(struct elfhdr);
173 memcpy(elf->e_ident, ELFMAG, SELFMAG);
174 elf->e_ident[EI_CLASS] = ELF_CLASS;
175 elf->e_ident[EI_DATA] = ELF_DATA;
176 elf->e_ident[EI_VERSION]= EV_CURRENT;
177 elf->e_ident[EI_OSABI] = ELF_OSABI;
178 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
179 elf->e_type = ET_CORE;
180 elf->e_machine = ELF_ARCH;
181 elf->e_version = EV_CURRENT;
183 elf->e_phoff = sizeof(struct elfhdr);
185 #if defined(CONFIG_H8300)
186 elf->e_flags = ELF_FLAGS;
190 elf->e_ehsize = sizeof(struct elfhdr);
191 elf->e_phentsize= sizeof(struct elf_phdr);
192 elf->e_phnum = nphdr;
197 /* setup ELF PT_NOTE program header */
198 nhdr = (struct elf_phdr *) bufp;
199 bufp += sizeof(struct elf_phdr);
200 offset += sizeof(struct elf_phdr);
201 nhdr->p_type = PT_NOTE;
210 /* setup ELF PT_LOAD program header for every area */
211 for (m=kclist; m; m=m->next) {
212 phdr = (struct elf_phdr *) bufp;
213 bufp += sizeof(struct elf_phdr);
214 offset += sizeof(struct elf_phdr);
216 phdr->p_type = PT_LOAD;
217 phdr->p_flags = PF_R|PF_W|PF_X;
218 phdr->p_offset = kc_vaddr_to_offset(m->addr) + dataoff;
219 phdr->p_vaddr = (size_t)m->addr;
221 phdr->p_filesz = phdr->p_memsz = m->size;
222 phdr->p_align = PAGE_SIZE;
226 * Set up the notes in similar form to SVR4 core dumps made
227 * with info from their /proc.
229 nhdr->p_offset = offset;
231 /* set up the process status */
232 notes[0].name = "CORE";
233 notes[0].type = NT_PRSTATUS;
234 notes[0].datasz = sizeof(struct elf_prstatus);
235 notes[0].data = &prstatus;
237 memset(&prstatus, 0, sizeof(struct elf_prstatus));
239 nhdr->p_filesz = notesize(¬es[0]);
240 bufp = storenote(¬es[0], bufp);
242 /* set up the process info */
243 notes[1].name = "CORE";
244 notes[1].type = NT_PRPSINFO;
245 notes[1].datasz = sizeof(struct elf_prpsinfo);
246 notes[1].data = &prpsinfo;
248 memset(&prpsinfo, 0, sizeof(struct elf_prpsinfo));
249 prpsinfo.pr_state = 0;
250 prpsinfo.pr_sname = 'R';
251 prpsinfo.pr_zomb = 0;
253 strcpy(prpsinfo.pr_fname, "vmlinux");
254 strncpy(prpsinfo.pr_psargs, saved_command_line, ELF_PRARGSZ);
256 nhdr->p_filesz += notesize(¬es[1]);
257 bufp = storenote(¬es[1], bufp);
259 /* set up the task structure */
260 notes[2].name = "CORE";
261 notes[2].type = NT_TASKSTRUCT;
262 notes[2].datasz = sizeof(struct task_struct);
263 notes[2].data = current;
265 nhdr->p_filesz += notesize(¬es[2]);
266 bufp = storenote(¬es[2], bufp);
268 } /* end elf_kcore_store_hdr() */
270 /*****************************************************************************/
272 * read from the ELF header and then kernel memory
275 read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
283 read_lock(&kclist_lock);
284 tsz = get_kcore_size(&nphdr, &elf_buflen);
285 proc_root_kcore->size = size = tsz + elf_buflen;
286 if (buflen == 0 || *fpos >= size) {
287 read_unlock(&kclist_lock);
291 /* trim buflen to not go beyond EOF */
292 if (buflen > size - *fpos)
293 buflen = size - *fpos;
295 /* construct an ELF core header if we'll need some of it */
296 if (*fpos < elf_buflen) {
299 tsz = elf_buflen - *fpos;
302 elf_buf = kmalloc(elf_buflen, GFP_ATOMIC);
304 read_unlock(&kclist_lock);
307 memset(elf_buf, 0, elf_buflen);
308 elf_kcore_store_hdr(elf_buf, nphdr, elf_buflen);
309 read_unlock(&kclist_lock);
310 if (copy_to_user(buffer, elf_buf + *fpos, tsz)) {
320 /* leave now if filled buffer already */
324 read_unlock(&kclist_lock);
327 * Check to see if our file offset matches with any of
328 * the addresses in the elf_phdr on our list.
330 start = kc_offset_to_vaddr(*fpos - elf_buflen);
331 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
335 struct kcore_list *m;
337 read_lock(&kclist_lock);
338 for (m=kclist; m; m=m->next) {
339 if (start >= m->addr && start < (m->addr+m->size))
342 read_unlock(&kclist_lock);
345 if (clear_user(buffer, tsz))
347 } else if ((start >= VMALLOC_START) && (start < VMALLOC_END)) {
350 unsigned long curstart = start;
351 unsigned long cursize = tsz;
353 elf_buf = kmalloc(tsz, GFP_KERNEL);
356 memset(elf_buf, 0, tsz);
358 read_lock(&vmlist_lock);
359 for (m=vmlist; m && cursize; m=m->next) {
360 unsigned long vmstart;
361 unsigned long vmsize;
362 unsigned long msize = m->size - PAGE_SIZE;
364 if (((unsigned long)m->addr + msize) <
367 if ((unsigned long)m->addr > (curstart +
370 vmstart = (curstart < (unsigned long)m->addr ?
371 (unsigned long)m->addr : curstart);
372 if (((unsigned long)m->addr + msize) >
373 (curstart + cursize))
374 vmsize = curstart + cursize - vmstart;
376 vmsize = (unsigned long)m->addr +
378 curstart = vmstart + vmsize;
380 /* don't dump ioremap'd stuff! (TA) */
381 if (m->flags & VM_IOREMAP)
383 memcpy(elf_buf + (vmstart - start),
384 (char *)vmstart, vmsize);
386 read_unlock(&vmlist_lock);
387 if (copy_to_user(buffer, elf_buf, tsz)) {
393 if (kern_addr_valid(start)) {
396 n = copy_to_user(buffer, (char *)start, tsz);
398 * We cannot distingush between fault on source
399 * and fault on destination. When this happens
400 * we clear too and hope it will trigger the
404 if (clear_user(buffer + tsz - n,
409 if (clear_user(buffer, tsz))
418 tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);