blob: fdde1cc78392cf92040c357e86271c15f4992459 [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>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080015#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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>
22#include <asm/io.h>
KAMEZAWA Hiroyuki2ef43ec2009-09-22 16:45:41 -070023#include <linux/list.h>
KAMEZAWA Hiroyuki94925872009-09-22 16:45:45 -070024#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Magnus Damm36027602006-12-06 20:38:00 -080026#define CORE_STR "CORE"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Edgar E. Iglesias79885b22008-07-25 01:48:10 -070028#ifndef ELF_CORE_EFLAGS
29#define ELF_CORE_EFLAGS 0
30#endif
31
Alexey Dobriyan97ce5d62008-10-06 14:14:19 +040032static struct proc_dir_entry *proc_root_kcore;
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static int open_kcore(struct inode * inode, struct file * filp)
35{
36 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
37}
38
39static ssize_t read_kcore(struct file *, char __user *, size_t, loff_t *);
40
Alexey Dobriyan97ce5d62008-10-06 14:14:19 +040041static const struct file_operations proc_kcore_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 .read = read_kcore,
43 .open = open_kcore,
44};
45
46#ifndef kc_vaddr_to_offset
47#define kc_vaddr_to_offset(v) ((v) - PAGE_OFFSET)
48#endif
49#ifndef kc_offset_to_vaddr
50#define kc_offset_to_vaddr(o) ((o) + PAGE_OFFSET)
51#endif
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/* An ELF note in memory */
54struct memelfnote
55{
56 const char *name;
57 int type;
58 unsigned int datasz;
59 void *data;
60};
61
KAMEZAWA Hiroyuki2ef43ec2009-09-22 16:45:41 -070062static LIST_HEAD(kclist_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static DEFINE_RWLOCK(kclist_lock);
64
65void
KAMEZAWA Hiroyukic30bb2a2009-09-22 16:45:43 -070066kclist_add(struct kcore_list *new, void *addr, size_t size, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 new->addr = (unsigned long)addr;
69 new->size = size;
KAMEZAWA Hiroyukic30bb2a2009-09-22 16:45:43 -070070 new->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 write_lock(&kclist_lock);
KAMEZAWA Hiroyuki2ef43ec2009-09-22 16:45:41 -070073 list_add_tail(&new->list, &kclist_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 write_unlock(&kclist_lock);
75}
76
77static size_t get_kcore_size(int *nphdr, size_t *elf_buflen)
78{
79 size_t try, size;
80 struct kcore_list *m;
81
82 *nphdr = 1; /* PT_NOTE */
83 size = 0;
84
KAMEZAWA Hiroyuki2ef43ec2009-09-22 16:45:41 -070085 list_for_each_entry(m, &kclist_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 try = kc_vaddr_to_offset((size_t)m->addr + m->size);
87 if (try > size)
88 size = try;
89 *nphdr = *nphdr + 1;
90 }
91 *elf_buflen = sizeof(struct elfhdr) +
92 (*nphdr + 2)*sizeof(struct elf_phdr) +
Magnus Damm36027602006-12-06 20:38:00 -080093 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 *elf_buflen = PAGE_ALIGN(*elf_buflen);
99 return size + *elf_buflen;
100}
101
102
103/*****************************************************************************/
104/*
105 * determine size of ELF note
106 */
107static int notesize(struct memelfnote *en)
108{
109 int sz;
110
111 sz = sizeof(struct elf_note);
Vivek Goyal632dd202006-09-29 02:01:45 -0700112 sz += roundup((strlen(en->name) + 1), 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 sz += roundup(en->datasz, 4);
114
115 return sz;
116} /* end notesize() */
117
118/*****************************************************************************/
119/*
120 * store a note in the header buffer
121 */
122static char *storenote(struct memelfnote *men, char *bufp)
123{
124 struct elf_note en;
125
126#define DUMP_WRITE(addr,nr) do { memcpy(bufp,addr,nr); bufp += nr; } while(0)
127
Vivek Goyal632dd202006-09-29 02:01:45 -0700128 en.n_namesz = strlen(men->name) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 en.n_descsz = men->datasz;
130 en.n_type = men->type;
131
132 DUMP_WRITE(&en, sizeof(en));
133 DUMP_WRITE(men->name, en.n_namesz);
134
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);
139
140#undef DUMP_WRITE
141
142 return bufp;
143} /* end storenote() */
144
145/*
146 * store an ELF coredump header in the supplied buffer
147 * nphdr is the number of elf_phdr to insert
148 */
149static void elf_kcore_store_hdr(char *bufp, int nphdr, int dataoff)
150{
151 struct elf_prstatus prstatus; /* NT_PRSTATUS */
152 struct elf_prpsinfo prpsinfo; /* NT_PRPSINFO */
153 struct elf_phdr *nhdr, *phdr;
154 struct elfhdr *elf;
155 struct memelfnote notes[3];
156 off_t offset = 0;
157 struct kcore_list *m;
158
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;
172 elf->e_entry = 0;
173 elf->e_phoff = sizeof(struct elfhdr);
174 elf->e_shoff = 0;
Edgar E. Iglesias79885b22008-07-25 01:48:10 -0700175 elf->e_flags = ELF_CORE_EFLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 elf->e_ehsize = sizeof(struct elfhdr);
177 elf->e_phentsize= sizeof(struct elf_phdr);
178 elf->e_phnum = nphdr;
179 elf->e_shentsize= 0;
180 elf->e_shnum = 0;
181 elf->e_shstrndx = 0;
182
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;
188 nhdr->p_offset = 0;
189 nhdr->p_vaddr = 0;
190 nhdr->p_paddr = 0;
191 nhdr->p_filesz = 0;
192 nhdr->p_memsz = 0;
193 nhdr->p_flags = 0;
194 nhdr->p_align = 0;
195
196 /* setup ELF PT_LOAD program header for every area */
KAMEZAWA Hiroyuki2ef43ec2009-09-22 16:45:41 -0700197 list_for_each_entry(m, &kclist_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 phdr = (struct elf_phdr *) bufp;
199 bufp += sizeof(struct elf_phdr);
200 offset += sizeof(struct elf_phdr);
201
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;
206 phdr->p_paddr = 0;
207 phdr->p_filesz = phdr->p_memsz = m->size;
208 phdr->p_align = PAGE_SIZE;
209 }
210
211 /*
212 * Set up the notes in similar form to SVR4 core dumps made
213 * with info from their /proc.
214 */
215 nhdr->p_offset = offset;
216
217 /* set up the process status */
Magnus Damm36027602006-12-06 20:38:00 -0800218 notes[0].name = CORE_STR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 notes[0].type = NT_PRSTATUS;
220 notes[0].datasz = sizeof(struct elf_prstatus);
221 notes[0].data = &prstatus;
222
223 memset(&prstatus, 0, sizeof(struct elf_prstatus));
224
225 nhdr->p_filesz = notesize(&notes[0]);
226 bufp = storenote(&notes[0], bufp);
227
228 /* set up the process info */
Magnus Damm36027602006-12-06 20:38:00 -0800229 notes[1].name = CORE_STR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 notes[1].type = NT_PRPSINFO;
231 notes[1].datasz = sizeof(struct elf_prpsinfo);
232 notes[1].data = &prpsinfo;
233
234 memset(&prpsinfo, 0, sizeof(struct elf_prpsinfo));
235 prpsinfo.pr_state = 0;
236 prpsinfo.pr_sname = 'R';
237 prpsinfo.pr_zomb = 0;
238
239 strcpy(prpsinfo.pr_fname, "vmlinux");
240 strncpy(prpsinfo.pr_psargs, saved_command_line, ELF_PRARGSZ);
241
242 nhdr->p_filesz += notesize(&notes[1]);
243 bufp = storenote(&notes[1], bufp);
244
245 /* set up the task structure */
Magnus Damm36027602006-12-06 20:38:00 -0800246 notes[2].name = CORE_STR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 notes[2].type = NT_TASKSTRUCT;
248 notes[2].datasz = sizeof(struct task_struct);
249 notes[2].data = current;
250
251 nhdr->p_filesz += notesize(&notes[2]);
252 bufp = storenote(&notes[2], bufp);
253
254} /* end elf_kcore_store_hdr() */
255
256/*****************************************************************************/
257/*
258 * read from the ELF header and then kernel memory
259 */
260static ssize_t
261read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
262{
263 ssize_t acc = 0;
264 size_t size, tsz;
265 size_t elf_buflen;
266 int nphdr;
267 unsigned long start;
268
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);
273 return 0;
274 }
275
276 /* trim buflen to not go beyond EOF */
277 if (buflen > size - *fpos)
278 buflen = size - *fpos;
279
280 /* construct an ELF core header if we'll need some of it */
281 if (*fpos < elf_buflen) {
282 char * elf_buf;
283
284 tsz = elf_buflen - *fpos;
285 if (buflen < tsz)
286 tsz = buflen;
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700287 elf_buf = kzalloc(elf_buflen, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (!elf_buf) {
289 read_unlock(&kclist_lock);
290 return -ENOMEM;
291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 elf_kcore_store_hdr(elf_buf, nphdr, elf_buflen);
293 read_unlock(&kclist_lock);
294 if (copy_to_user(buffer, elf_buf + *fpos, tsz)) {
295 kfree(elf_buf);
296 return -EFAULT;
297 }
298 kfree(elf_buf);
299 buflen -= tsz;
300 *fpos += tsz;
301 buffer += tsz;
302 acc += tsz;
303
304 /* leave now if filled buffer already */
305 if (buflen == 0)
306 return acc;
307 } else
308 read_unlock(&kclist_lock);
309
310 /*
311 * Check to see if our file offset matches with any of
312 * the addresses in the elf_phdr on our list.
313 */
314 start = kc_offset_to_vaddr(*fpos - elf_buflen);
315 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
316 tsz = buflen;
317
318 while (buflen) {
319 struct kcore_list *m;
320
321 read_lock(&kclist_lock);
KAMEZAWA Hiroyuki2ef43ec2009-09-22 16:45:41 -0700322 list_for_each_entry(m, &kclist_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (start >= m->addr && start < (m->addr+m->size))
324 break;
325 }
326 read_unlock(&kclist_lock);
327
328 if (m == NULL) {
329 if (clear_user(buffer, tsz))
330 return -EFAULT;
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800331 } else if (is_vmalloc_addr((void *)start)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 char * elf_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700334 elf_buf = kzalloc(tsz, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (!elf_buf)
336 return -ENOMEM;
KAMEZAWA Hiroyuki73d7c332009-09-21 17:02:35 -0700337 vread(elf_buf, (char *)start, tsz);
338 /* we have to zero-fill user buffer even if no read */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (copy_to_user(buffer, elf_buf, tsz)) {
340 kfree(elf_buf);
341 return -EFAULT;
342 }
343 kfree(elf_buf);
344 } else {
345 if (kern_addr_valid(start)) {
346 unsigned long n;
347
348 n = copy_to_user(buffer, (char *)start, tsz);
349 /*
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
353 * EFAULT again.
354 */
355 if (n) {
356 if (clear_user(buffer + tsz - n,
Adam B. Jerome06351702006-07-12 09:03:07 -0700357 n))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return -EFAULT;
359 }
360 } else {
361 if (clear_user(buffer, tsz))
362 return -EFAULT;
363 }
364 }
365 buflen -= tsz;
366 *fpos += tsz;
367 buffer += tsz;
368 acc += tsz;
369 start += tsz;
370 tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
371 }
372
373 return acc;
374}
Alexey Dobriyan97ce5d62008-10-06 14:14:19 +0400375
KAMEZAWA Hiroyukia0614da2009-09-22 16:45:44 -0700376static struct kcore_list kcore_vmalloc;
377
KAMEZAWA Hiroyuki94925872009-09-22 16:45:45 -0700378#ifdef CONFIG_ARCH_PROC_KCORE_TEXT
379static struct kcore_list kcore_text;
380/*
381 * If defined, special segment is used for mapping kernel text instead of
382 * direct-map area. We need to create special TEXT section.
383 */
384static void __init proc_kcore_text_init(void)
385{
386 kclist_add(&kcore_text, _stext, _end - _stext, KCORE_TEXT);
387}
388#else
389static void __init proc_kcore_text_init(void)
390{
391}
392#endif
393
Alexey Dobriyan97ce5d62008-10-06 14:14:19 +0400394static int __init proc_kcore_init(void)
395{
396 proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
KAMEZAWA Hiroyuki94925872009-09-22 16:45:45 -0700397 proc_kcore_text_init();
KAMEZAWA Hiroyukia0614da2009-09-22 16:45:44 -0700398 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
399 VMALLOC_END - VMALLOC_START, KCORE_VMALLOC);
Alexey Dobriyan97ce5d62008-10-06 14:14:19 +0400400 return 0;
401}
402module_init(proc_kcore_init);