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/capability.h>
16 #include <linux/elf.h>
17 #include <linux/elfcore.h>
18 #include <linux/vmalloc.h>
19 #include <linux/highmem.h>
20 #include <linux/init.h>
21 #include <asm/uaccess.h>
23 #include <linux/list.h>
24 #include <asm/sections.h>
26 #define CORE_STR "CORE"
28 #ifndef ELF_CORE_EFLAGS
29 #define ELF_CORE_EFLAGS 0
32 static struct proc_dir_entry *proc_root_kcore;
34 static int open_kcore(struct inode * inode, struct file * filp)
36 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
39 static ssize_t read_kcore(struct file *, char __user *, size_t, loff_t *);
41 static const struct file_operations proc_kcore_operations = {
46 #ifndef kc_vaddr_to_offset
47 #define kc_vaddr_to_offset(v) ((v) - PAGE_OFFSET)
49 #ifndef kc_offset_to_vaddr
50 #define kc_offset_to_vaddr(o) ((o) + PAGE_OFFSET)
53 /* An ELF note in memory */
62 static LIST_HEAD(kclist_head);
63 static DEFINE_RWLOCK(kclist_lock);
66 kclist_add(struct kcore_list *new, void *addr, size_t size, int type)
68 new->addr = (unsigned long)addr;
72 write_lock(&kclist_lock);
73 list_add_tail(&new->list, &kclist_head);
74 write_unlock(&kclist_lock);
77 static size_t get_kcore_size(int *nphdr, size_t *elf_buflen)
82 *nphdr = 1; /* PT_NOTE */
85 list_for_each_entry(m, &kclist_head, list) {
86 try = kc_vaddr_to_offset((size_t)m->addr + m->size);
91 *elf_buflen = sizeof(struct elfhdr) +
92 (*nphdr + 2)*sizeof(struct elf_phdr) +
93 3 * ((sizeof(struct elf_note)) +
94 roundup(sizeof(CORE_STR), 4)) +
95 roundup(sizeof(struct elf_prstatus), 4) +
96 roundup(sizeof(struct elf_prpsinfo), 4) +
97 roundup(sizeof(struct task_struct), 4);
98 *elf_buflen = PAGE_ALIGN(*elf_buflen);
99 return size + *elf_buflen;
103 /*****************************************************************************/
105 * determine size of ELF note
107 static int notesize(struct memelfnote *en)
111 sz = sizeof(struct elf_note);
112 sz += roundup((strlen(en->name) + 1), 4);
113 sz += roundup(en->datasz, 4);
116 } /* end notesize() */
118 /*****************************************************************************/
120 * store a note in the header buffer
122 static char *storenote(struct memelfnote *men, char *bufp)
126 #define DUMP_WRITE(addr,nr) do { memcpy(bufp,addr,nr); bufp += nr; } while(0)
128 en.n_namesz = strlen(men->name) + 1;
129 en.n_descsz = men->datasz;
130 en.n_type = men->type;
132 DUMP_WRITE(&en, sizeof(en));
133 DUMP_WRITE(men->name, en.n_namesz);
135 /* XXX - cast from long long to long to avoid need for libgcc.a */
136 bufp = (char*) roundup((unsigned long)bufp,4);
137 DUMP_WRITE(men->data, men->datasz);
138 bufp = (char*) roundup((unsigned long)bufp,4);
143 } /* end storenote() */
146 * store an ELF coredump header in the supplied buffer
147 * nphdr is the number of elf_phdr to insert
149 static void elf_kcore_store_hdr(char *bufp, int nphdr, int dataoff)
151 struct elf_prstatus prstatus; /* NT_PRSTATUS */
152 struct elf_prpsinfo prpsinfo; /* NT_PRPSINFO */
153 struct elf_phdr *nhdr, *phdr;
155 struct memelfnote notes[3];
157 struct kcore_list *m;
159 /* setup ELF header */
160 elf = (struct elfhdr *) bufp;
161 bufp += sizeof(struct elfhdr);
162 offset += sizeof(struct elfhdr);
163 memcpy(elf->e_ident, ELFMAG, SELFMAG);
164 elf->e_ident[EI_CLASS] = ELF_CLASS;
165 elf->e_ident[EI_DATA] = ELF_DATA;
166 elf->e_ident[EI_VERSION]= EV_CURRENT;
167 elf->e_ident[EI_OSABI] = ELF_OSABI;
168 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
169 elf->e_type = ET_CORE;
170 elf->e_machine = ELF_ARCH;
171 elf->e_version = EV_CURRENT;
173 elf->e_phoff = sizeof(struct elfhdr);
175 elf->e_flags = ELF_CORE_EFLAGS;
176 elf->e_ehsize = sizeof(struct elfhdr);
177 elf->e_phentsize= sizeof(struct elf_phdr);
178 elf->e_phnum = nphdr;
183 /* setup ELF PT_NOTE program header */
184 nhdr = (struct elf_phdr *) bufp;
185 bufp += sizeof(struct elf_phdr);
186 offset += sizeof(struct elf_phdr);
187 nhdr->p_type = PT_NOTE;
196 /* setup ELF PT_LOAD program header for every area */
197 list_for_each_entry(m, &kclist_head, list) {
198 phdr = (struct elf_phdr *) bufp;
199 bufp += sizeof(struct elf_phdr);
200 offset += sizeof(struct elf_phdr);
202 phdr->p_type = PT_LOAD;
203 phdr->p_flags = PF_R|PF_W|PF_X;
204 phdr->p_offset = kc_vaddr_to_offset(m->addr) + dataoff;
205 phdr->p_vaddr = (size_t)m->addr;
207 phdr->p_filesz = phdr->p_memsz = m->size;
208 phdr->p_align = PAGE_SIZE;
212 * Set up the notes in similar form to SVR4 core dumps made
213 * with info from their /proc.
215 nhdr->p_offset = offset;
217 /* set up the process status */
218 notes[0].name = CORE_STR;
219 notes[0].type = NT_PRSTATUS;
220 notes[0].datasz = sizeof(struct elf_prstatus);
221 notes[0].data = &prstatus;
223 memset(&prstatus, 0, sizeof(struct elf_prstatus));
225 nhdr->p_filesz = notesize(¬es[0]);
226 bufp = storenote(¬es[0], bufp);
228 /* set up the process info */
229 notes[1].name = CORE_STR;
230 notes[1].type = NT_PRPSINFO;
231 notes[1].datasz = sizeof(struct elf_prpsinfo);
232 notes[1].data = &prpsinfo;
234 memset(&prpsinfo, 0, sizeof(struct elf_prpsinfo));
235 prpsinfo.pr_state = 0;
236 prpsinfo.pr_sname = 'R';
237 prpsinfo.pr_zomb = 0;
239 strcpy(prpsinfo.pr_fname, "vmlinux");
240 strncpy(prpsinfo.pr_psargs, saved_command_line, ELF_PRARGSZ);
242 nhdr->p_filesz += notesize(¬es[1]);
243 bufp = storenote(¬es[1], bufp);
245 /* set up the task structure */
246 notes[2].name = CORE_STR;
247 notes[2].type = NT_TASKSTRUCT;
248 notes[2].datasz = sizeof(struct task_struct);
249 notes[2].data = current;
251 nhdr->p_filesz += notesize(¬es[2]);
252 bufp = storenote(¬es[2], bufp);
254 } /* end elf_kcore_store_hdr() */
256 /*****************************************************************************/
258 * read from the ELF header and then kernel memory
261 read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
269 read_lock(&kclist_lock);
270 proc_root_kcore->size = size = get_kcore_size(&nphdr, &elf_buflen);
271 if (buflen == 0 || *fpos >= size) {
272 read_unlock(&kclist_lock);
276 /* trim buflen to not go beyond EOF */
277 if (buflen > size - *fpos)
278 buflen = size - *fpos;
280 /* construct an ELF core header if we'll need some of it */
281 if (*fpos < elf_buflen) {
284 tsz = elf_buflen - *fpos;
287 elf_buf = kzalloc(elf_buflen, GFP_ATOMIC);
289 read_unlock(&kclist_lock);
292 elf_kcore_store_hdr(elf_buf, nphdr, elf_buflen);
293 read_unlock(&kclist_lock);
294 if (copy_to_user(buffer, elf_buf + *fpos, tsz)) {
304 /* leave now if filled buffer already */
308 read_unlock(&kclist_lock);
311 * Check to see if our file offset matches with any of
312 * the addresses in the elf_phdr on our list.
314 start = kc_offset_to_vaddr(*fpos - elf_buflen);
315 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
319 struct kcore_list *m;
321 read_lock(&kclist_lock);
322 list_for_each_entry(m, &kclist_head, list) {
323 if (start >= m->addr && start < (m->addr+m->size))
326 read_unlock(&kclist_lock);
329 if (clear_user(buffer, tsz))
331 } else if (is_vmalloc_addr((void *)start)) {
334 elf_buf = kzalloc(tsz, GFP_KERNEL);
337 vread(elf_buf, (char *)start, tsz);
338 /* we have to zero-fill user buffer even if no read */
339 if (copy_to_user(buffer, elf_buf, tsz)) {
345 if (kern_addr_valid(start)) {
348 n = copy_to_user(buffer, (char *)start, tsz);
350 * We cannot distingush between fault on source
351 * and fault on destination. When this happens
352 * we clear too and hope it will trigger the
356 if (clear_user(buffer + tsz - n,
361 if (clear_user(buffer, tsz))
370 tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
376 static struct kcore_list kcore_vmalloc;
378 #ifdef CONFIG_ARCH_PROC_KCORE_TEXT
379 static struct kcore_list kcore_text;
381 * If defined, special segment is used for mapping kernel text instead of
382 * direct-map area. We need to create special TEXT section.
384 static void __init proc_kcore_text_init(void)
386 kclist_add(&kcore_text, _stext, _end - _stext, KCORE_TEXT);
389 static void __init proc_kcore_text_init(void)
394 static int __init proc_kcore_init(void)
396 proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
397 proc_kcore_text_init();
398 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
399 VMALLOC_END - VMALLOC_START, KCORE_VMALLOC);
402 module_init(proc_kcore_init);