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 DEFINE_RWLOCK(kclist_lock
);
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
);
71 static size_t get_kcore_size(int *nphdr
, size_t *elf_buflen
)
76 *nphdr
= 1; /* PT_NOTE */
79 for (m
=kclist
; m
; m
=m
->next
) {
80 try = kc_vaddr_to_offset((size_t)m
->addr
+ m
->size
);
85 *elf_buflen
= sizeof(struct elfhdr
) +
86 (*nphdr
+ 2)*sizeof(struct elf_phdr
) +
87 3 * (sizeof(struct elf_note
) + 4) +
88 sizeof(struct elf_prstatus
) +
89 sizeof(struct elf_prpsinfo
) +
90 sizeof(struct task_struct
);
91 *elf_buflen
= PAGE_ALIGN(*elf_buflen
);
92 return size
+ *elf_buflen
;
96 /*****************************************************************************/
98 * determine size of ELF note
100 static int notesize(struct memelfnote
*en
)
104 sz
= sizeof(struct elf_note
);
105 sz
+= roundup(strlen(en
->name
), 4);
106 sz
+= roundup(en
->datasz
, 4);
109 } /* end notesize() */
111 /*****************************************************************************/
113 * store a note in the header buffer
115 static char *storenote(struct memelfnote
*men
, char *bufp
)
119 #define DUMP_WRITE(addr,nr) do { memcpy(bufp,addr,nr); bufp += nr; } while(0)
121 en
.n_namesz
= strlen(men
->name
);
122 en
.n_descsz
= men
->datasz
;
123 en
.n_type
= men
->type
;
125 DUMP_WRITE(&en
, sizeof(en
));
126 DUMP_WRITE(men
->name
, en
.n_namesz
);
128 /* XXX - cast from long long to long to avoid need for libgcc.a */
129 bufp
= (char*) roundup((unsigned long)bufp
,4);
130 DUMP_WRITE(men
->data
, men
->datasz
);
131 bufp
= (char*) roundup((unsigned long)bufp
,4);
136 } /* end storenote() */
139 * store an ELF coredump header in the supplied buffer
140 * nphdr is the number of elf_phdr to insert
142 static void elf_kcore_store_hdr(char *bufp
, int nphdr
, int dataoff
)
144 struct elf_prstatus prstatus
; /* NT_PRSTATUS */
145 struct elf_prpsinfo prpsinfo
; /* NT_PRPSINFO */
146 struct elf_phdr
*nhdr
, *phdr
;
148 struct memelfnote notes
[3];
150 struct kcore_list
*m
;
152 /* setup ELF header */
153 elf
= (struct elfhdr
*) bufp
;
154 bufp
+= sizeof(struct elfhdr
);
155 offset
+= sizeof(struct elfhdr
);
156 memcpy(elf
->e_ident
, ELFMAG
, SELFMAG
);
157 elf
->e_ident
[EI_CLASS
] = ELF_CLASS
;
158 elf
->e_ident
[EI_DATA
] = ELF_DATA
;
159 elf
->e_ident
[EI_VERSION
]= EV_CURRENT
;
160 elf
->e_ident
[EI_OSABI
] = ELF_OSABI
;
161 memset(elf
->e_ident
+EI_PAD
, 0, EI_NIDENT
-EI_PAD
);
162 elf
->e_type
= ET_CORE
;
163 elf
->e_machine
= ELF_ARCH
;
164 elf
->e_version
= EV_CURRENT
;
166 elf
->e_phoff
= sizeof(struct elfhdr
);
168 #if defined(CONFIG_H8300)
169 elf
->e_flags
= ELF_FLAGS
;
173 elf
->e_ehsize
= sizeof(struct elfhdr
);
174 elf
->e_phentsize
= sizeof(struct elf_phdr
);
175 elf
->e_phnum
= nphdr
;
180 /* setup ELF PT_NOTE program header */
181 nhdr
= (struct elf_phdr
*) bufp
;
182 bufp
+= sizeof(struct elf_phdr
);
183 offset
+= sizeof(struct elf_phdr
);
184 nhdr
->p_type
= PT_NOTE
;
193 /* setup ELF PT_LOAD program header for every area */
194 for (m
=kclist
; m
; m
=m
->next
) {
195 phdr
= (struct elf_phdr
*) bufp
;
196 bufp
+= sizeof(struct elf_phdr
);
197 offset
+= sizeof(struct elf_phdr
);
199 phdr
->p_type
= PT_LOAD
;
200 phdr
->p_flags
= PF_R
|PF_W
|PF_X
;
201 phdr
->p_offset
= kc_vaddr_to_offset(m
->addr
) + dataoff
;
202 phdr
->p_vaddr
= (size_t)m
->addr
;
204 phdr
->p_filesz
= phdr
->p_memsz
= m
->size
;
205 phdr
->p_align
= PAGE_SIZE
;
209 * Set up the notes in similar form to SVR4 core dumps made
210 * with info from their /proc.
212 nhdr
->p_offset
= offset
;
214 /* set up the process status */
215 notes
[0].name
= "CORE";
216 notes
[0].type
= NT_PRSTATUS
;
217 notes
[0].datasz
= sizeof(struct elf_prstatus
);
218 notes
[0].data
= &prstatus
;
220 memset(&prstatus
, 0, sizeof(struct elf_prstatus
));
222 nhdr
->p_filesz
= notesize(¬es
[0]);
223 bufp
= storenote(¬es
[0], bufp
);
225 /* set up the process info */
226 notes
[1].name
= "CORE";
227 notes
[1].type
= NT_PRPSINFO
;
228 notes
[1].datasz
= sizeof(struct elf_prpsinfo
);
229 notes
[1].data
= &prpsinfo
;
231 memset(&prpsinfo
, 0, sizeof(struct elf_prpsinfo
));
232 prpsinfo
.pr_state
= 0;
233 prpsinfo
.pr_sname
= 'R';
234 prpsinfo
.pr_zomb
= 0;
236 strcpy(prpsinfo
.pr_fname
, "vmlinux");
237 strncpy(prpsinfo
.pr_psargs
, saved_command_line
, ELF_PRARGSZ
);
239 nhdr
->p_filesz
+= notesize(¬es
[1]);
240 bufp
= storenote(¬es
[1], bufp
);
242 /* set up the task structure */
243 notes
[2].name
= "CORE";
244 notes
[2].type
= NT_TASKSTRUCT
;
245 notes
[2].datasz
= sizeof(struct task_struct
);
246 notes
[2].data
= current
;
248 nhdr
->p_filesz
+= notesize(¬es
[2]);
249 bufp
= storenote(¬es
[2], bufp
);
251 } /* end elf_kcore_store_hdr() */
253 /*****************************************************************************/
255 * read from the ELF header and then kernel memory
258 read_kcore(struct file
*file
, char __user
*buffer
, size_t buflen
, loff_t
*fpos
)
266 read_lock(&kclist_lock
);
267 proc_root_kcore
->size
= size
= get_kcore_size(&nphdr
, &elf_buflen
);
268 if (buflen
== 0 || *fpos
>= size
) {
269 read_unlock(&kclist_lock
);
273 /* trim buflen to not go beyond EOF */
274 if (buflen
> size
- *fpos
)
275 buflen
= size
- *fpos
;
277 /* construct an ELF core header if we'll need some of it */
278 if (*fpos
< elf_buflen
) {
281 tsz
= elf_buflen
- *fpos
;
284 elf_buf
= kmalloc(elf_buflen
, GFP_ATOMIC
);
286 read_unlock(&kclist_lock
);
289 memset(elf_buf
, 0, elf_buflen
);
290 elf_kcore_store_hdr(elf_buf
, nphdr
, elf_buflen
);
291 read_unlock(&kclist_lock
);
292 if (copy_to_user(buffer
, elf_buf
+ *fpos
, tsz
)) {
302 /* leave now if filled buffer already */
306 read_unlock(&kclist_lock
);
309 * Check to see if our file offset matches with any of
310 * the addresses in the elf_phdr on our list.
312 start
= kc_offset_to_vaddr(*fpos
- elf_buflen
);
313 if ((tsz
= (PAGE_SIZE
- (start
& ~PAGE_MASK
))) > buflen
)
317 struct kcore_list
*m
;
319 read_lock(&kclist_lock
);
320 for (m
=kclist
; m
; m
=m
->next
) {
321 if (start
>= m
->addr
&& start
< (m
->addr
+m
->size
))
324 read_unlock(&kclist_lock
);
327 if (clear_user(buffer
, tsz
))
329 } else if ((start
>= VMALLOC_START
) && (start
< VMALLOC_END
)) {
332 unsigned long curstart
= start
;
333 unsigned long cursize
= tsz
;
335 elf_buf
= kmalloc(tsz
, GFP_KERNEL
);
338 memset(elf_buf
, 0, tsz
);
340 read_lock(&vmlist_lock
);
341 for (m
=vmlist
; m
&& cursize
; m
=m
->next
) {
342 unsigned long vmstart
;
343 unsigned long vmsize
;
344 unsigned long msize
= m
->size
- PAGE_SIZE
;
346 if (((unsigned long)m
->addr
+ msize
) <
349 if ((unsigned long)m
->addr
> (curstart
+
352 vmstart
= (curstart
< (unsigned long)m
->addr
?
353 (unsigned long)m
->addr
: curstart
);
354 if (((unsigned long)m
->addr
+ msize
) >
355 (curstart
+ cursize
))
356 vmsize
= curstart
+ cursize
- vmstart
;
358 vmsize
= (unsigned long)m
->addr
+
360 curstart
= vmstart
+ vmsize
;
362 /* don't dump ioremap'd stuff! (TA) */
363 if (m
->flags
& VM_IOREMAP
)
365 memcpy(elf_buf
+ (vmstart
- start
),
366 (char *)vmstart
, vmsize
);
368 read_unlock(&vmlist_lock
);
369 if (copy_to_user(buffer
, elf_buf
, tsz
)) {
375 if (kern_addr_valid(start
)) {
378 n
= copy_to_user(buffer
, (char *)start
, tsz
);
380 * We cannot distingush between fault on source
381 * and fault on destination. When this happens
382 * we clear too and hope it will trigger the
386 if (clear_user(buffer
+ tsz
- n
,
391 if (clear_user(buffer
, tsz
))
400 tsz
= (buflen
> PAGE_SIZE
? PAGE_SIZE
: buflen
);