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>
24 #define CORE_STR "CORE"
26 #ifndef ELF_CORE_EFLAGS
27 #define ELF_CORE_EFLAGS 0
30 static int open_kcore(struct inode
* inode
, struct file
* filp
)
32 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
35 static ssize_t
read_kcore(struct file
*, char __user
*, size_t, loff_t
*);
37 const struct file_operations proc_kcore_operations
= {
42 #ifndef kc_vaddr_to_offset
43 #define kc_vaddr_to_offset(v) ((v) - PAGE_OFFSET)
45 #ifndef kc_offset_to_vaddr
46 #define kc_offset_to_vaddr(o) ((o) + PAGE_OFFSET)
49 /* An ELF note in memory */
58 static struct kcore_list
*kclist
;
59 static DEFINE_RWLOCK(kclist_lock
);
62 kclist_add(struct kcore_list
*new, void *addr
, size_t size
)
64 new->addr
= (unsigned long)addr
;
67 write_lock(&kclist_lock
);
70 write_unlock(&kclist_lock
);
73 static size_t get_kcore_size(int *nphdr
, size_t *elf_buflen
)
78 *nphdr
= 1; /* PT_NOTE */
81 for (m
=kclist
; m
; m
=m
->next
) {
82 try = kc_vaddr_to_offset((size_t)m
->addr
+ m
->size
);
87 *elf_buflen
= sizeof(struct elfhdr
) +
88 (*nphdr
+ 2)*sizeof(struct elf_phdr
) +
89 3 * ((sizeof(struct elf_note
)) +
90 roundup(sizeof(CORE_STR
), 4)) +
91 roundup(sizeof(struct elf_prstatus
), 4) +
92 roundup(sizeof(struct elf_prpsinfo
), 4) +
93 roundup(sizeof(struct task_struct
), 4);
94 *elf_buflen
= PAGE_ALIGN(*elf_buflen
);
95 return size
+ *elf_buflen
;
99 /*****************************************************************************/
101 * determine size of ELF note
103 static int notesize(struct memelfnote
*en
)
107 sz
= sizeof(struct elf_note
);
108 sz
+= roundup((strlen(en
->name
) + 1), 4);
109 sz
+= roundup(en
->datasz
, 4);
112 } /* end notesize() */
114 /*****************************************************************************/
116 * store a note in the header buffer
118 static char *storenote(struct memelfnote
*men
, char *bufp
)
122 #define DUMP_WRITE(addr,nr) do { memcpy(bufp,addr,nr); bufp += nr; } while(0)
124 en
.n_namesz
= strlen(men
->name
) + 1;
125 en
.n_descsz
= men
->datasz
;
126 en
.n_type
= men
->type
;
128 DUMP_WRITE(&en
, sizeof(en
));
129 DUMP_WRITE(men
->name
, en
.n_namesz
);
131 /* XXX - cast from long long to long to avoid need for libgcc.a */
132 bufp
= (char*) roundup((unsigned long)bufp
,4);
133 DUMP_WRITE(men
->data
, men
->datasz
);
134 bufp
= (char*) roundup((unsigned long)bufp
,4);
139 } /* end storenote() */
142 * store an ELF coredump header in the supplied buffer
143 * nphdr is the number of elf_phdr to insert
145 static void elf_kcore_store_hdr(char *bufp
, int nphdr
, int dataoff
)
147 struct elf_prstatus prstatus
; /* NT_PRSTATUS */
148 struct elf_prpsinfo prpsinfo
; /* NT_PRPSINFO */
149 struct elf_phdr
*nhdr
, *phdr
;
151 struct memelfnote notes
[3];
153 struct kcore_list
*m
;
155 /* setup ELF header */
156 elf
= (struct elfhdr
*) bufp
;
157 bufp
+= sizeof(struct elfhdr
);
158 offset
+= sizeof(struct elfhdr
);
159 memcpy(elf
->e_ident
, ELFMAG
, SELFMAG
);
160 elf
->e_ident
[EI_CLASS
] = ELF_CLASS
;
161 elf
->e_ident
[EI_DATA
] = ELF_DATA
;
162 elf
->e_ident
[EI_VERSION
]= EV_CURRENT
;
163 elf
->e_ident
[EI_OSABI
] = ELF_OSABI
;
164 memset(elf
->e_ident
+EI_PAD
, 0, EI_NIDENT
-EI_PAD
);
165 elf
->e_type
= ET_CORE
;
166 elf
->e_machine
= ELF_ARCH
;
167 elf
->e_version
= EV_CURRENT
;
169 elf
->e_phoff
= sizeof(struct elfhdr
);
171 elf
->e_flags
= ELF_CORE_EFLAGS
;
172 elf
->e_ehsize
= sizeof(struct elfhdr
);
173 elf
->e_phentsize
= sizeof(struct elf_phdr
);
174 elf
->e_phnum
= nphdr
;
179 /* setup ELF PT_NOTE program header */
180 nhdr
= (struct elf_phdr
*) bufp
;
181 bufp
+= sizeof(struct elf_phdr
);
182 offset
+= sizeof(struct elf_phdr
);
183 nhdr
->p_type
= PT_NOTE
;
192 /* setup ELF PT_LOAD program header for every area */
193 for (m
=kclist
; m
; m
=m
->next
) {
194 phdr
= (struct elf_phdr
*) bufp
;
195 bufp
+= sizeof(struct elf_phdr
);
196 offset
+= sizeof(struct elf_phdr
);
198 phdr
->p_type
= PT_LOAD
;
199 phdr
->p_flags
= PF_R
|PF_W
|PF_X
;
200 phdr
->p_offset
= kc_vaddr_to_offset(m
->addr
) + dataoff
;
201 phdr
->p_vaddr
= (size_t)m
->addr
;
203 phdr
->p_filesz
= phdr
->p_memsz
= m
->size
;
204 phdr
->p_align
= PAGE_SIZE
;
208 * Set up the notes in similar form to SVR4 core dumps made
209 * with info from their /proc.
211 nhdr
->p_offset
= offset
;
213 /* set up the process status */
214 notes
[0].name
= CORE_STR
;
215 notes
[0].type
= NT_PRSTATUS
;
216 notes
[0].datasz
= sizeof(struct elf_prstatus
);
217 notes
[0].data
= &prstatus
;
219 memset(&prstatus
, 0, sizeof(struct elf_prstatus
));
221 nhdr
->p_filesz
= notesize(¬es
[0]);
222 bufp
= storenote(¬es
[0], bufp
);
224 /* set up the process info */
225 notes
[1].name
= CORE_STR
;
226 notes
[1].type
= NT_PRPSINFO
;
227 notes
[1].datasz
= sizeof(struct elf_prpsinfo
);
228 notes
[1].data
= &prpsinfo
;
230 memset(&prpsinfo
, 0, sizeof(struct elf_prpsinfo
));
231 prpsinfo
.pr_state
= 0;
232 prpsinfo
.pr_sname
= 'R';
233 prpsinfo
.pr_zomb
= 0;
235 strcpy(prpsinfo
.pr_fname
, "vmlinux");
236 strncpy(prpsinfo
.pr_psargs
, saved_command_line
, ELF_PRARGSZ
);
238 nhdr
->p_filesz
+= notesize(¬es
[1]);
239 bufp
= storenote(¬es
[1], bufp
);
241 /* set up the task structure */
242 notes
[2].name
= CORE_STR
;
243 notes
[2].type
= NT_TASKSTRUCT
;
244 notes
[2].datasz
= sizeof(struct task_struct
);
245 notes
[2].data
= current
;
247 nhdr
->p_filesz
+= notesize(¬es
[2]);
248 bufp
= storenote(¬es
[2], bufp
);
250 } /* end elf_kcore_store_hdr() */
252 /*****************************************************************************/
254 * read from the ELF header and then kernel memory
257 read_kcore(struct file
*file
, char __user
*buffer
, size_t buflen
, loff_t
*fpos
)
265 read_lock(&kclist_lock
);
266 proc_root_kcore
->size
= size
= get_kcore_size(&nphdr
, &elf_buflen
);
267 if (buflen
== 0 || *fpos
>= size
) {
268 read_unlock(&kclist_lock
);
272 /* trim buflen to not go beyond EOF */
273 if (buflen
> size
- *fpos
)
274 buflen
= size
- *fpos
;
276 /* construct an ELF core header if we'll need some of it */
277 if (*fpos
< elf_buflen
) {
280 tsz
= elf_buflen
- *fpos
;
283 elf_buf
= kzalloc(elf_buflen
, GFP_ATOMIC
);
285 read_unlock(&kclist_lock
);
288 elf_kcore_store_hdr(elf_buf
, nphdr
, elf_buflen
);
289 read_unlock(&kclist_lock
);
290 if (copy_to_user(buffer
, elf_buf
+ *fpos
, tsz
)) {
300 /* leave now if filled buffer already */
304 read_unlock(&kclist_lock
);
307 * Check to see if our file offset matches with any of
308 * the addresses in the elf_phdr on our list.
310 start
= kc_offset_to_vaddr(*fpos
- elf_buflen
);
311 if ((tsz
= (PAGE_SIZE
- (start
& ~PAGE_MASK
))) > buflen
)
315 struct kcore_list
*m
;
317 read_lock(&kclist_lock
);
318 for (m
=kclist
; m
; m
=m
->next
) {
319 if (start
>= m
->addr
&& start
< (m
->addr
+m
->size
))
322 read_unlock(&kclist_lock
);
325 if (clear_user(buffer
, tsz
))
327 } else if (is_vmalloc_addr((void *)start
)) {
330 unsigned long curstart
= start
;
331 unsigned long cursize
= tsz
;
333 elf_buf
= kzalloc(tsz
, GFP_KERNEL
);
337 read_lock(&vmlist_lock
);
338 for (m
=vmlist
; m
&& cursize
; m
=m
->next
) {
339 unsigned long vmstart
;
340 unsigned long vmsize
;
341 unsigned long msize
= m
->size
- PAGE_SIZE
;
343 if (((unsigned long)m
->addr
+ msize
) <
346 if ((unsigned long)m
->addr
> (curstart
+
349 vmstart
= (curstart
< (unsigned long)m
->addr
?
350 (unsigned long)m
->addr
: curstart
);
351 if (((unsigned long)m
->addr
+ msize
) >
352 (curstart
+ cursize
))
353 vmsize
= curstart
+ cursize
- vmstart
;
355 vmsize
= (unsigned long)m
->addr
+
357 curstart
= vmstart
+ vmsize
;
359 /* don't dump ioremap'd stuff! (TA) */
360 if (m
->flags
& VM_IOREMAP
)
362 memcpy(elf_buf
+ (vmstart
- start
),
363 (char *)vmstart
, vmsize
);
365 read_unlock(&vmlist_lock
);
366 if (copy_to_user(buffer
, elf_buf
, tsz
)) {
372 if (kern_addr_valid(start
)) {
375 n
= copy_to_user(buffer
, (char *)start
, tsz
);
377 * We cannot distingush between fault on source
378 * and fault on destination. When this happens
379 * we clear too and hope it will trigger the
383 if (clear_user(buffer
+ tsz
- n
,
388 if (clear_user(buffer
, tsz
))
397 tsz
= (buflen
> PAGE_SIZE
? PAGE_SIZE
: buflen
);