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/a.out.h>
16 #include <linux/capability.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>
25 #define CORE_STR "CORE"
27 static int open_kcore(struct inode
* inode
, struct file
* filp
)
29 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
32 static ssize_t
read_kcore(struct file
*, char __user
*, size_t, loff_t
*);
34 const struct file_operations proc_kcore_operations
= {
39 #ifndef kc_vaddr_to_offset
40 #define kc_vaddr_to_offset(v) ((v) - PAGE_OFFSET)
42 #ifndef kc_offset_to_vaddr
43 #define kc_offset_to_vaddr(o) ((o) + PAGE_OFFSET)
46 /* An ELF note in memory */
55 static struct kcore_list
*kclist
;
56 static DEFINE_RWLOCK(kclist_lock
);
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
);
70 static size_t get_kcore_size(int *nphdr
, size_t *elf_buflen
)
75 *nphdr
= 1; /* PT_NOTE */
78 for (m
=kclist
; m
; m
=m
->next
) {
79 try = kc_vaddr_to_offset((size_t)m
->addr
+ m
->size
);
84 *elf_buflen
= sizeof(struct elfhdr
) +
85 (*nphdr
+ 2)*sizeof(struct elf_phdr
) +
86 3 * ((sizeof(struct elf_note
)) +
87 roundup(sizeof(CORE_STR
), 4)) +
88 roundup(sizeof(struct elf_prstatus
), 4) +
89 roundup(sizeof(struct elf_prpsinfo
), 4) +
90 roundup(sizeof(struct task_struct
), 4);
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
) + 1), 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
) + 1;
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_STR
;
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_STR
;
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_STR
;
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
= kzalloc(elf_buflen
, GFP_ATOMIC
);
286 read_unlock(&kclist_lock
);
289 elf_kcore_store_hdr(elf_buf
, nphdr
, elf_buflen
);
290 read_unlock(&kclist_lock
);
291 if (copy_to_user(buffer
, elf_buf
+ *fpos
, tsz
)) {
301 /* leave now if filled buffer already */
305 read_unlock(&kclist_lock
);
308 * Check to see if our file offset matches with any of
309 * the addresses in the elf_phdr on our list.
311 start
= kc_offset_to_vaddr(*fpos
- elf_buflen
);
312 if ((tsz
= (PAGE_SIZE
- (start
& ~PAGE_MASK
))) > buflen
)
316 struct kcore_list
*m
;
318 read_lock(&kclist_lock
);
319 for (m
=kclist
; m
; m
=m
->next
) {
320 if (start
>= m
->addr
&& start
< (m
->addr
+m
->size
))
323 read_unlock(&kclist_lock
);
326 if (clear_user(buffer
, tsz
))
328 } else if ((start
>= VMALLOC_START
) && (start
< VMALLOC_END
)) {
331 unsigned long curstart
= start
;
332 unsigned long cursize
= tsz
;
334 elf_buf
= kzalloc(tsz
, GFP_KERNEL
);
338 read_lock(&vmlist_lock
);
339 for (m
=vmlist
; m
&& cursize
; m
=m
->next
) {
340 unsigned long vmstart
;
341 unsigned long vmsize
;
342 unsigned long msize
= m
->size
- PAGE_SIZE
;
344 if (((unsigned long)m
->addr
+ msize
) <
347 if ((unsigned long)m
->addr
> (curstart
+
350 vmstart
= (curstart
< (unsigned long)m
->addr
?
351 (unsigned long)m
->addr
: curstart
);
352 if (((unsigned long)m
->addr
+ msize
) >
353 (curstart
+ cursize
))
354 vmsize
= curstart
+ cursize
- vmstart
;
356 vmsize
= (unsigned long)m
->addr
+
358 curstart
= vmstart
+ vmsize
;
360 /* don't dump ioremap'd stuff! (TA) */
361 if (m
->flags
& VM_IOREMAP
)
363 memcpy(elf_buf
+ (vmstart
- start
),
364 (char *)vmstart
, vmsize
);
366 read_unlock(&vmlist_lock
);
367 if (copy_to_user(buffer
, elf_buf
, tsz
)) {
373 if (kern_addr_valid(start
)) {
376 n
= copy_to_user(buffer
, (char *)start
, tsz
);
378 * We cannot distingush between fault on source
379 * and fault on destination. When this happens
380 * we clear too and hope it will trigger the
384 if (clear_user(buffer
+ tsz
- n
,
389 if (clear_user(buffer
, tsz
))
398 tsz
= (buflen
> PAGE_SIZE
? PAGE_SIZE
: buflen
);