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 <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 __user *, size_t, loff_t *);
32 struct file_operations proc_kcore_operations = {
37 #ifndef kc_vaddr_to_offset
38 #define kc_vaddr_to_offset(v) ((v) - PAGE_OFFSET)
40 #ifndef kc_offset_to_vaddr
41 #define kc_offset_to_vaddr(o) ((o) + PAGE_OFFSET)
44 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
46 /* An ELF note in memory */
55 static struct kcore_list *kclist;
56 static rwlock_t kclist_lock = RW_LOCK_UNLOCKED;
59 kclist_add(struct kcore_list *new, void *addr, size_t size)
61 new->addr = (unsigned long)addr;
64 write_lock(&kclist_lock);
67 write_unlock(&kclist_lock);
71 kclist_del(void *addr)
73 struct kcore_list *m, **p = &kclist;
75 write_lock(&kclist_lock);
76 for (m = *p; m; p = &m->next) {
77 if (m->addr == (unsigned long)addr) {
79 write_unlock(&kclist_lock);
83 write_unlock(&kclist_lock);
87 extern char saved_command_line[];
89 static size_t get_kcore_size(int *nphdr, size_t *elf_buflen)
94 *nphdr = 1; /* PT_NOTE */
97 for (m=kclist; m; m=m->next) {
98 try = kc_vaddr_to_offset((size_t)m->addr + m->size);
103 *elf_buflen = sizeof(struct elfhdr) +
104 (*nphdr + 2)*sizeof(struct elf_phdr) +
105 3 * sizeof(struct memelfnote) +
106 sizeof(struct elf_prstatus) +
107 sizeof(struct elf_prpsinfo) +
108 sizeof(struct task_struct);
109 *elf_buflen = PAGE_ALIGN(*elf_buflen);
110 return size + *elf_buflen;
114 /*****************************************************************************/
116 * determine size of ELF note
118 static int notesize(struct memelfnote *en)
122 sz = sizeof(struct elf_note);
123 sz += roundup(strlen(en->name), 4);
124 sz += roundup(en->datasz, 4);
127 } /* end notesize() */
129 /*****************************************************************************/
131 * store a note in the header buffer
133 static char *storenote(struct memelfnote *men, char *bufp)
137 #define DUMP_WRITE(addr,nr) do { memcpy(bufp,addr,nr); bufp += nr; } while(0)
139 en.n_namesz = strlen(men->name);
140 en.n_descsz = men->datasz;
141 en.n_type = men->type;
143 DUMP_WRITE(&en, sizeof(en));
144 DUMP_WRITE(men->name, en.n_namesz);
146 /* XXX - cast from long long to long to avoid need for libgcc.a */
147 bufp = (char*) roundup((unsigned long)bufp,4);
148 DUMP_WRITE(men->data, men->datasz);
149 bufp = (char*) roundup((unsigned long)bufp,4);
154 } /* end storenote() */
157 * store an ELF coredump header in the supplied buffer
158 * nphdr is the number of elf_phdr to insert
160 static void elf_kcore_store_hdr(char *bufp, int nphdr, int dataoff)
162 struct elf_prstatus prstatus; /* NT_PRSTATUS */
163 struct elf_prpsinfo prpsinfo; /* NT_PRPSINFO */
164 struct elf_phdr *nhdr, *phdr;
166 struct memelfnote notes[3];
168 struct kcore_list *m;
170 /* setup ELF header */
171 elf = (struct elfhdr *) bufp;
172 bufp += sizeof(struct elfhdr);
173 offset += sizeof(struct elfhdr);
174 memcpy(elf->e_ident, ELFMAG, SELFMAG);
175 elf->e_ident[EI_CLASS] = ELF_CLASS;
176 elf->e_ident[EI_DATA] = ELF_DATA;
177 elf->e_ident[EI_VERSION]= EV_CURRENT;
178 elf->e_ident[EI_OSABI] = ELF_OSABI;
179 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
180 elf->e_type = ET_CORE;
181 elf->e_machine = ELF_ARCH;
182 elf->e_version = EV_CURRENT;
184 elf->e_phoff = sizeof(struct elfhdr);
186 #if defined(CONFIG_H8300)
187 elf->e_flags = ELF_FLAGS;
191 elf->e_ehsize = sizeof(struct elfhdr);
192 elf->e_phentsize= sizeof(struct elf_phdr);
193 elf->e_phnum = nphdr;
198 /* setup ELF PT_NOTE program header */
199 nhdr = (struct elf_phdr *) bufp;
200 bufp += sizeof(struct elf_phdr);
201 offset += sizeof(struct elf_phdr);
202 nhdr->p_type = PT_NOTE;
211 /* setup ELF PT_LOAD program header for every area */
212 for (m=kclist; m; m=m->next) {
213 phdr = (struct elf_phdr *) bufp;
214 bufp += sizeof(struct elf_phdr);
215 offset += sizeof(struct elf_phdr);
217 phdr->p_type = PT_LOAD;
218 phdr->p_flags = PF_R|PF_W|PF_X;
219 phdr->p_offset = kc_vaddr_to_offset(m->addr) + dataoff;
220 phdr->p_vaddr = (size_t)m->addr;
222 phdr->p_filesz = phdr->p_memsz = m->size;
223 phdr->p_align = PAGE_SIZE;
227 * Set up the notes in similar form to SVR4 core dumps made
228 * with info from their /proc.
230 nhdr->p_offset = offset;
232 /* set up the process status */
233 notes[0].name = "CORE";
234 notes[0].type = NT_PRSTATUS;
235 notes[0].datasz = sizeof(struct elf_prstatus);
236 notes[0].data = &prstatus;
238 memset(&prstatus, 0, sizeof(struct elf_prstatus));
240 nhdr->p_filesz = notesize(¬es[0]);
241 bufp = storenote(¬es[0], bufp);
243 /* set up the process info */
244 notes[1].name = "CORE";
245 notes[1].type = NT_PRPSINFO;
246 notes[1].datasz = sizeof(struct elf_prpsinfo);
247 notes[1].data = &prpsinfo;
249 memset(&prpsinfo, 0, sizeof(struct elf_prpsinfo));
250 prpsinfo.pr_state = 0;
251 prpsinfo.pr_sname = 'R';
252 prpsinfo.pr_zomb = 0;
254 strcpy(prpsinfo.pr_fname, "vmlinux");
255 strncpy(prpsinfo.pr_psargs, saved_command_line, ELF_PRARGSZ);
257 nhdr->p_filesz += notesize(¬es[1]);
258 bufp = storenote(¬es[1], bufp);
260 /* set up the task structure */
261 notes[2].name = "CORE";
262 notes[2].type = NT_TASKSTRUCT;
263 notes[2].datasz = sizeof(struct task_struct);
264 notes[2].data = current;
266 nhdr->p_filesz += notesize(¬es[2]);
267 bufp = storenote(¬es[2], bufp);
269 } /* end elf_kcore_store_hdr() */
271 /*****************************************************************************/
273 * read from the ELF header and then kernel memory
276 read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
284 read_lock(&kclist_lock);
285 tsz = get_kcore_size(&nphdr, &elf_buflen);
286 proc_root_kcore->size = size = tsz + elf_buflen;
287 if (buflen == 0 || *fpos >= size) {
288 read_unlock(&kclist_lock);
292 /* trim buflen to not go beyond EOF */
293 if (buflen > size - *fpos)
294 buflen = size - *fpos;
296 /* construct an ELF core header if we'll need some of it */
297 if (*fpos < elf_buflen) {
300 tsz = elf_buflen - *fpos;
303 elf_buf = kmalloc(elf_buflen, GFP_ATOMIC);
305 read_unlock(&kclist_lock);
308 memset(elf_buf, 0, elf_buflen);
309 elf_kcore_store_hdr(elf_buf, nphdr, elf_buflen);
310 read_unlock(&kclist_lock);
311 if (copy_to_user(buffer, elf_buf + *fpos, tsz)) {
321 /* leave now if filled buffer already */
325 read_unlock(&kclist_lock);
328 * Check to see if our file offset matches with any of
329 * the addresses in the elf_phdr on our list.
331 start = kc_offset_to_vaddr(*fpos - elf_buflen);
332 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
336 struct kcore_list *m;
338 read_lock(&kclist_lock);
339 for (m=kclist; m; m=m->next) {
340 if (start >= m->addr && start < (m->addr+m->size))
343 read_unlock(&kclist_lock);
346 if (clear_user(buffer, tsz))
348 } else if ((start >= VMALLOC_START) && (start < VMALLOC_END)) {
351 unsigned long curstart = start;
352 unsigned long cursize = tsz;
354 elf_buf = kmalloc(tsz, GFP_KERNEL);
357 memset(elf_buf, 0, tsz);
359 read_lock(&vmlist_lock);
360 for (m=vmlist; m && cursize; m=m->next) {
361 unsigned long vmstart;
362 unsigned long vmsize;
363 unsigned long msize = m->size - PAGE_SIZE;
365 if (((unsigned long)m->addr + msize) <
368 if ((unsigned long)m->addr > (curstart +
371 vmstart = (curstart < (unsigned long)m->addr ?
372 (unsigned long)m->addr : curstart);
373 if (((unsigned long)m->addr + msize) >
374 (curstart + cursize))
375 vmsize = curstart + cursize - vmstart;
377 vmsize = (unsigned long)m->addr +
379 curstart = vmstart + vmsize;
381 /* don't dump ioremap'd stuff! (TA) */
382 if (m->flags & VM_IOREMAP)
384 memcpy(elf_buf + (vmstart - start),
385 (char *)vmstart, vmsize);
387 read_unlock(&vmlist_lock);
388 if (copy_to_user(buffer, elf_buf, tsz)) {
394 if (kern_addr_valid(start)) {
397 n = copy_to_user(buffer, (char *)start, tsz);
399 * We cannot distingush between fault on source
400 * and fault on destination. When this happens
401 * we clear too and hope it will trigger the
405 if (clear_user(buffer + tsz - n,
410 if (clear_user(buffer, tsz))
419 tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);