blob: 3ceff385727273269018a6e4780a97a6bd234cc6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/proc/kcore.c kernel ELF core dumper
3 *
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>
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/mm.h>
13#include <linux/proc_fs.h>
14#include <linux/user.h>
15#include <linux/a.out.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080016#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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>
23#include <asm/io.h>
24
25
26static int open_kcore(struct inode * inode, struct file * filp)
27{
28 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
29}
30
31static ssize_t read_kcore(struct file *, char __user *, size_t, loff_t *);
32
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080033const struct file_operations proc_kcore_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 .read = read_kcore,
35 .open = open_kcore,
36};
37
38#ifndef kc_vaddr_to_offset
39#define kc_vaddr_to_offset(v) ((v) - PAGE_OFFSET)
40#endif
41#ifndef kc_offset_to_vaddr
42#define kc_offset_to_vaddr(o) ((o) + PAGE_OFFSET)
43#endif
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/* An ELF note in memory */
46struct memelfnote
47{
48 const char *name;
49 int type;
50 unsigned int datasz;
51 void *data;
52};
53
54static struct kcore_list *kclist;
55static DEFINE_RWLOCK(kclist_lock);
56
57void
58kclist_add(struct kcore_list *new, void *addr, size_t size)
59{
60 new->addr = (unsigned long)addr;
61 new->size = size;
62
63 write_lock(&kclist_lock);
64 new->next = kclist;
65 kclist = new;
66 write_unlock(&kclist_lock);
67}
68
69static size_t get_kcore_size(int *nphdr, size_t *elf_buflen)
70{
71 size_t try, size;
72 struct kcore_list *m;
73
74 *nphdr = 1; /* PT_NOTE */
75 size = 0;
76
77 for (m=kclist; m; m=m->next) {
78 try = kc_vaddr_to_offset((size_t)m->addr + m->size);
79 if (try > size)
80 size = try;
81 *nphdr = *nphdr + 1;
82 }
83 *elf_buflen = sizeof(struct elfhdr) +
84 (*nphdr + 2)*sizeof(struct elf_phdr) +
85 3 * (sizeof(struct elf_note) + 4) +
86 sizeof(struct elf_prstatus) +
87 sizeof(struct elf_prpsinfo) +
88 sizeof(struct task_struct);
89 *elf_buflen = PAGE_ALIGN(*elf_buflen);
90 return size + *elf_buflen;
91}
92
93
94/*****************************************************************************/
95/*
96 * determine size of ELF note
97 */
98static int notesize(struct memelfnote *en)
99{
100 int sz;
101
102 sz = sizeof(struct elf_note);
103 sz += roundup(strlen(en->name), 4);
104 sz += roundup(en->datasz, 4);
105
106 return sz;
107} /* end notesize() */
108
109/*****************************************************************************/
110/*
111 * store a note in the header buffer
112 */
113static char *storenote(struct memelfnote *men, char *bufp)
114{
115 struct elf_note en;
116
117#define DUMP_WRITE(addr,nr) do { memcpy(bufp,addr,nr); bufp += nr; } while(0)
118
119 en.n_namesz = strlen(men->name);
120 en.n_descsz = men->datasz;
121 en.n_type = men->type;
122
123 DUMP_WRITE(&en, sizeof(en));
124 DUMP_WRITE(men->name, en.n_namesz);
125
126 /* XXX - cast from long long to long to avoid need for libgcc.a */
127 bufp = (char*) roundup((unsigned long)bufp,4);
128 DUMP_WRITE(men->data, men->datasz);
129 bufp = (char*) roundup((unsigned long)bufp,4);
130
131#undef DUMP_WRITE
132
133 return bufp;
134} /* end storenote() */
135
136/*
137 * store an ELF coredump header in the supplied buffer
138 * nphdr is the number of elf_phdr to insert
139 */
140static void elf_kcore_store_hdr(char *bufp, int nphdr, int dataoff)
141{
142 struct elf_prstatus prstatus; /* NT_PRSTATUS */
143 struct elf_prpsinfo prpsinfo; /* NT_PRPSINFO */
144 struct elf_phdr *nhdr, *phdr;
145 struct elfhdr *elf;
146 struct memelfnote notes[3];
147 off_t offset = 0;
148 struct kcore_list *m;
149
150 /* setup ELF header */
151 elf = (struct elfhdr *) bufp;
152 bufp += sizeof(struct elfhdr);
153 offset += sizeof(struct elfhdr);
154 memcpy(elf->e_ident, ELFMAG, SELFMAG);
155 elf->e_ident[EI_CLASS] = ELF_CLASS;
156 elf->e_ident[EI_DATA] = ELF_DATA;
157 elf->e_ident[EI_VERSION]= EV_CURRENT;
158 elf->e_ident[EI_OSABI] = ELF_OSABI;
159 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
160 elf->e_type = ET_CORE;
161 elf->e_machine = ELF_ARCH;
162 elf->e_version = EV_CURRENT;
163 elf->e_entry = 0;
164 elf->e_phoff = sizeof(struct elfhdr);
165 elf->e_shoff = 0;
166#if defined(CONFIG_H8300)
167 elf->e_flags = ELF_FLAGS;
168#else
169 elf->e_flags = 0;
170#endif
171 elf->e_ehsize = sizeof(struct elfhdr);
172 elf->e_phentsize= sizeof(struct elf_phdr);
173 elf->e_phnum = nphdr;
174 elf->e_shentsize= 0;
175 elf->e_shnum = 0;
176 elf->e_shstrndx = 0;
177
178 /* setup ELF PT_NOTE program header */
179 nhdr = (struct elf_phdr *) bufp;
180 bufp += sizeof(struct elf_phdr);
181 offset += sizeof(struct elf_phdr);
182 nhdr->p_type = PT_NOTE;
183 nhdr->p_offset = 0;
184 nhdr->p_vaddr = 0;
185 nhdr->p_paddr = 0;
186 nhdr->p_filesz = 0;
187 nhdr->p_memsz = 0;
188 nhdr->p_flags = 0;
189 nhdr->p_align = 0;
190
191 /* setup ELF PT_LOAD program header for every area */
192 for (m=kclist; m; m=m->next) {
193 phdr = (struct elf_phdr *) bufp;
194 bufp += sizeof(struct elf_phdr);
195 offset += sizeof(struct elf_phdr);
196
197 phdr->p_type = PT_LOAD;
198 phdr->p_flags = PF_R|PF_W|PF_X;
199 phdr->p_offset = kc_vaddr_to_offset(m->addr) + dataoff;
200 phdr->p_vaddr = (size_t)m->addr;
201 phdr->p_paddr = 0;
202 phdr->p_filesz = phdr->p_memsz = m->size;
203 phdr->p_align = PAGE_SIZE;
204 }
205
206 /*
207 * Set up the notes in similar form to SVR4 core dumps made
208 * with info from their /proc.
209 */
210 nhdr->p_offset = offset;
211
212 /* set up the process status */
213 notes[0].name = "CORE";
214 notes[0].type = NT_PRSTATUS;
215 notes[0].datasz = sizeof(struct elf_prstatus);
216 notes[0].data = &prstatus;
217
218 memset(&prstatus, 0, sizeof(struct elf_prstatus));
219
220 nhdr->p_filesz = notesize(&notes[0]);
221 bufp = storenote(&notes[0], bufp);
222
223 /* set up the process info */
224 notes[1].name = "CORE";
225 notes[1].type = NT_PRPSINFO;
226 notes[1].datasz = sizeof(struct elf_prpsinfo);
227 notes[1].data = &prpsinfo;
228
229 memset(&prpsinfo, 0, sizeof(struct elf_prpsinfo));
230 prpsinfo.pr_state = 0;
231 prpsinfo.pr_sname = 'R';
232 prpsinfo.pr_zomb = 0;
233
234 strcpy(prpsinfo.pr_fname, "vmlinux");
235 strncpy(prpsinfo.pr_psargs, saved_command_line, ELF_PRARGSZ);
236
237 nhdr->p_filesz += notesize(&notes[1]);
238 bufp = storenote(&notes[1], bufp);
239
240 /* set up the task structure */
241 notes[2].name = "CORE";
242 notes[2].type = NT_TASKSTRUCT;
243 notes[2].datasz = sizeof(struct task_struct);
244 notes[2].data = current;
245
246 nhdr->p_filesz += notesize(&notes[2]);
247 bufp = storenote(&notes[2], bufp);
248
249} /* end elf_kcore_store_hdr() */
250
251/*****************************************************************************/
252/*
253 * read from the ELF header and then kernel memory
254 */
255static ssize_t
256read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
257{
258 ssize_t acc = 0;
259 size_t size, tsz;
260 size_t elf_buflen;
261 int nphdr;
262 unsigned long start;
263
264 read_lock(&kclist_lock);
265 proc_root_kcore->size = size = get_kcore_size(&nphdr, &elf_buflen);
266 if (buflen == 0 || *fpos >= size) {
267 read_unlock(&kclist_lock);
268 return 0;
269 }
270
271 /* trim buflen to not go beyond EOF */
272 if (buflen > size - *fpos)
273 buflen = size - *fpos;
274
275 /* construct an ELF core header if we'll need some of it */
276 if (*fpos < elf_buflen) {
277 char * elf_buf;
278
279 tsz = elf_buflen - *fpos;
280 if (buflen < tsz)
281 tsz = buflen;
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700282 elf_buf = kzalloc(elf_buflen, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (!elf_buf) {
284 read_unlock(&kclist_lock);
285 return -ENOMEM;
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 elf_kcore_store_hdr(elf_buf, nphdr, elf_buflen);
288 read_unlock(&kclist_lock);
289 if (copy_to_user(buffer, elf_buf + *fpos, tsz)) {
290 kfree(elf_buf);
291 return -EFAULT;
292 }
293 kfree(elf_buf);
294 buflen -= tsz;
295 *fpos += tsz;
296 buffer += tsz;
297 acc += tsz;
298
299 /* leave now if filled buffer already */
300 if (buflen == 0)
301 return acc;
302 } else
303 read_unlock(&kclist_lock);
304
305 /*
306 * Check to see if our file offset matches with any of
307 * the addresses in the elf_phdr on our list.
308 */
309 start = kc_offset_to_vaddr(*fpos - elf_buflen);
310 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
311 tsz = buflen;
312
313 while (buflen) {
314 struct kcore_list *m;
315
316 read_lock(&kclist_lock);
317 for (m=kclist; m; m=m->next) {
318 if (start >= m->addr && start < (m->addr+m->size))
319 break;
320 }
321 read_unlock(&kclist_lock);
322
323 if (m == NULL) {
324 if (clear_user(buffer, tsz))
325 return -EFAULT;
326 } else if ((start >= VMALLOC_START) && (start < VMALLOC_END)) {
327 char * elf_buf;
328 struct vm_struct *m;
329 unsigned long curstart = start;
330 unsigned long cursize = tsz;
331
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700332 elf_buf = kzalloc(tsz, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (!elf_buf)
334 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 read_lock(&vmlist_lock);
337 for (m=vmlist; m && cursize; m=m->next) {
338 unsigned long vmstart;
339 unsigned long vmsize;
340 unsigned long msize = m->size - PAGE_SIZE;
341
342 if (((unsigned long)m->addr + msize) <
343 curstart)
344 continue;
345 if ((unsigned long)m->addr > (curstart +
346 cursize))
347 break;
348 vmstart = (curstart < (unsigned long)m->addr ?
349 (unsigned long)m->addr : curstart);
350 if (((unsigned long)m->addr + msize) >
351 (curstart + cursize))
352 vmsize = curstart + cursize - vmstart;
353 else
354 vmsize = (unsigned long)m->addr +
355 msize - vmstart;
356 curstart = vmstart + vmsize;
357 cursize -= vmsize;
358 /* don't dump ioremap'd stuff! (TA) */
359 if (m->flags & VM_IOREMAP)
360 continue;
361 memcpy(elf_buf + (vmstart - start),
362 (char *)vmstart, vmsize);
363 }
364 read_unlock(&vmlist_lock);
365 if (copy_to_user(buffer, elf_buf, tsz)) {
366 kfree(elf_buf);
367 return -EFAULT;
368 }
369 kfree(elf_buf);
370 } else {
371 if (kern_addr_valid(start)) {
372 unsigned long n;
373
374 n = copy_to_user(buffer, (char *)start, tsz);
375 /*
376 * We cannot distingush between fault on source
377 * and fault on destination. When this happens
378 * we clear too and hope it will trigger the
379 * EFAULT again.
380 */
381 if (n) {
382 if (clear_user(buffer + tsz - n,
Adam B. Jerome06351702006-07-12 09:03:07 -0700383 n))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return -EFAULT;
385 }
386 } else {
387 if (clear_user(buffer, tsz))
388 return -EFAULT;
389 }
390 }
391 buflen -= tsz;
392 *fpos += tsz;
393 buffer += tsz;
394 acc += tsz;
395 start += tsz;
396 tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
397 }
398
399 return acc;
400}