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);
187 elf->e_ehsize = sizeof(struct elfhdr);
188 elf->e_phentsize= sizeof(struct elf_phdr);
189 elf->e_phnum = nphdr;
194 /* setup ELF PT_NOTE program header */
195 nhdr = (struct elf_phdr *) bufp;
196 bufp += sizeof(struct elf_phdr);
197 offset += sizeof(struct elf_phdr);
198 nhdr->p_type = PT_NOTE;
207 /* setup ELF PT_LOAD program header for every area */
208 for (m=kclist; m; m=m->next) {
209 phdr = (struct elf_phdr *) bufp;
210 bufp += sizeof(struct elf_phdr);
211 offset += sizeof(struct elf_phdr);
213 phdr->p_type = PT_LOAD;
214 phdr->p_flags = PF_R|PF_W|PF_X;
215 phdr->p_offset = kc_vaddr_to_offset(m->addr) + dataoff;
216 phdr->p_vaddr = (size_t)m->addr;
218 phdr->p_filesz = phdr->p_memsz = m->size;
219 phdr->p_align = PAGE_SIZE;
223 * Set up the notes in similar form to SVR4 core dumps made
224 * with info from their /proc.
226 nhdr->p_offset = offset;
228 /* set up the process status */
229 notes[0].name = "CORE";
230 notes[0].type = NT_PRSTATUS;
231 notes[0].datasz = sizeof(struct elf_prstatus);
232 notes[0].data = &prstatus;
234 memset(&prstatus, 0, sizeof(struct elf_prstatus));
236 nhdr->p_filesz = notesize(¬es[0]);
237 bufp = storenote(¬es[0], bufp);
239 /* set up the process info */
240 notes[1].name = "CORE";
241 notes[1].type = NT_PRPSINFO;
242 notes[1].datasz = sizeof(struct elf_prpsinfo);
243 notes[1].data = &prpsinfo;
245 memset(&prpsinfo, 0, sizeof(struct elf_prpsinfo));
246 prpsinfo.pr_state = 0;
247 prpsinfo.pr_sname = 'R';
248 prpsinfo.pr_zomb = 0;
250 strcpy(prpsinfo.pr_fname, "vmlinux");
251 strncpy(prpsinfo.pr_psargs, saved_command_line, ELF_PRARGSZ);
253 nhdr->p_filesz += notesize(¬es[1]);
254 bufp = storenote(¬es[1], bufp);
256 /* set up the task structure */
257 notes[2].name = "CORE";
258 notes[2].type = NT_TASKSTRUCT;
259 notes[2].datasz = sizeof(struct task_struct);
260 notes[2].data = current;
262 nhdr->p_filesz += notesize(¬es[2]);
263 bufp = storenote(¬es[2], bufp);
265 } /* end elf_kcore_store_hdr() */
267 /*****************************************************************************/
269 * read from the ELF header and then kernel memory
272 read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
280 read_lock(&kclist_lock);
281 tsz = get_kcore_size(&nphdr, &elf_buflen);
282 proc_root_kcore->size = size = tsz + elf_buflen;
283 if (buflen == 0 || *fpos >= size) {
284 read_unlock(&kclist_lock);
288 /* trim buflen to not go beyond EOF */
289 if (buflen > size - *fpos)
290 buflen = size - *fpos;
292 /* construct an ELF core header if we'll need some of it */
293 if (*fpos < elf_buflen) {
296 tsz = elf_buflen - *fpos;
299 elf_buf = kmalloc(elf_buflen, GFP_ATOMIC);
301 read_unlock(&kclist_lock);
304 memset(elf_buf, 0, elf_buflen);
305 elf_kcore_store_hdr(elf_buf, nphdr, elf_buflen);
306 read_unlock(&kclist_lock);
307 if (copy_to_user(buffer, elf_buf + *fpos, tsz)) {
317 /* leave now if filled buffer already */
321 read_unlock(&kclist_lock);
324 * Check to see if our file offset matches with any of
325 * the addresses in the elf_phdr on our list.
327 start = kc_offset_to_vaddr(*fpos - elf_buflen);
328 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
332 struct kcore_list *m;
334 read_lock(&kclist_lock);
335 for (m=kclist; m; m=m->next) {
336 if (start >= m->addr && start < (m->addr+m->size))
339 read_unlock(&kclist_lock);
342 if (clear_user(buffer, tsz))
344 } else if ((start >= VMALLOC_START) && (start < VMALLOC_END)) {
347 unsigned long curstart = start;
348 unsigned long cursize = tsz;
350 elf_buf = kmalloc(tsz, GFP_KERNEL);
353 memset(elf_buf, 0, tsz);
355 read_lock(&vmlist_lock);
356 for (m=vmlist; m && cursize; m=m->next) {
357 unsigned long vmstart;
358 unsigned long vmsize;
359 unsigned long msize = m->size - PAGE_SIZE;
361 if (((unsigned long)m->addr + msize) <
364 if ((unsigned long)m->addr > (curstart +
367 vmstart = (curstart < (unsigned long)m->addr ?
368 (unsigned long)m->addr : curstart);
369 if (((unsigned long)m->addr + msize) >
370 (curstart + cursize))
371 vmsize = curstart + cursize - vmstart;
373 vmsize = (unsigned long)m->addr +
375 curstart = vmstart + vmsize;
377 /* don't dump ioremap'd stuff! (TA) */
378 if (m->flags & VM_IOREMAP)
380 memcpy(elf_buf + (vmstart - start),
381 (char *)vmstart, vmsize);
383 read_unlock(&vmlist_lock);
384 if (copy_to_user(buffer, elf_buf, tsz)) {
390 if (kern_addr_valid(start)) {
393 n = copy_to_user(buffer, (char *)start, tsz);
395 * We cannot distingush between fault on source
396 * and fault on destination. When this happens
397 * we clear too and hope it will trigger the
401 if (clear_user(buffer + tsz - n,
406 if (clear_user(buffer, tsz))
415 tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);