i386: hard_{en,dis}able_TSC can be static
[linux-2.6/mini2440.git] / fs / proc / kcore.c
blob1be73082edd388bd443f0be5c559cdf756eccbbb
1 /*
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/mm.h>
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>
23 #include <asm/io.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 = {
35 .read = read_kcore,
36 .open = open_kcore,
39 #ifndef kc_vaddr_to_offset
40 #define kc_vaddr_to_offset(v) ((v) - PAGE_OFFSET)
41 #endif
42 #ifndef kc_offset_to_vaddr
43 #define kc_offset_to_vaddr(o) ((o) + PAGE_OFFSET)
44 #endif
46 /* An ELF note in memory */
47 struct memelfnote
49 const char *name;
50 int type;
51 unsigned int datasz;
52 void *data;
55 static struct kcore_list *kclist;
56 static DEFINE_RWLOCK(kclist_lock);
58 void
59 kclist_add(struct kcore_list *new, void *addr, size_t size)
61 new->addr = (unsigned long)addr;
62 new->size = size;
64 write_lock(&kclist_lock);
65 new->next = kclist;
66 kclist = new;
67 write_unlock(&kclist_lock);
70 static size_t get_kcore_size(int *nphdr, size_t *elf_buflen)
72 size_t try, size;
73 struct kcore_list *m;
75 *nphdr = 1; /* PT_NOTE */
76 size = 0;
78 for (m=kclist; m; m=m->next) {
79 try = kc_vaddr_to_offset((size_t)m->addr + m->size);
80 if (try > size)
81 size = try;
82 *nphdr = *nphdr + 1;
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)
102 int sz;
104 sz = sizeof(struct elf_note);
105 sz += roundup((strlen(en->name) + 1), 4);
106 sz += roundup(en->datasz, 4);
108 return sz;
109 } /* end notesize() */
111 /*****************************************************************************/
113 * store a note in the header buffer
115 static char *storenote(struct memelfnote *men, char *bufp)
117 struct elf_note en;
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);
133 #undef DUMP_WRITE
135 return bufp;
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;
147 struct elfhdr *elf;
148 struct memelfnote notes[3];
149 off_t offset = 0;
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;
165 elf->e_entry = 0;
166 elf->e_phoff = sizeof(struct elfhdr);
167 elf->e_shoff = 0;
168 #if defined(CONFIG_H8300)
169 elf->e_flags = ELF_FLAGS;
170 #else
171 elf->e_flags = 0;
172 #endif
173 elf->e_ehsize = sizeof(struct elfhdr);
174 elf->e_phentsize= sizeof(struct elf_phdr);
175 elf->e_phnum = nphdr;
176 elf->e_shentsize= 0;
177 elf->e_shnum = 0;
178 elf->e_shstrndx = 0;
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;
185 nhdr->p_offset = 0;
186 nhdr->p_vaddr = 0;
187 nhdr->p_paddr = 0;
188 nhdr->p_filesz = 0;
189 nhdr->p_memsz = 0;
190 nhdr->p_flags = 0;
191 nhdr->p_align = 0;
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;
203 phdr->p_paddr = 0;
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(&notes[0]);
223 bufp = storenote(&notes[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(&notes[1]);
240 bufp = storenote(&notes[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(&notes[2]);
249 bufp = storenote(&notes[2], bufp);
251 } /* end elf_kcore_store_hdr() */
253 /*****************************************************************************/
255 * read from the ELF header and then kernel memory
257 static ssize_t
258 read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
260 ssize_t acc = 0;
261 size_t size, tsz;
262 size_t elf_buflen;
263 int nphdr;
264 unsigned long start;
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);
270 return 0;
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) {
279 char * elf_buf;
281 tsz = elf_buflen - *fpos;
282 if (buflen < tsz)
283 tsz = buflen;
284 elf_buf = kzalloc(elf_buflen, GFP_ATOMIC);
285 if (!elf_buf) {
286 read_unlock(&kclist_lock);
287 return -ENOMEM;
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)) {
292 kfree(elf_buf);
293 return -EFAULT;
295 kfree(elf_buf);
296 buflen -= tsz;
297 *fpos += tsz;
298 buffer += tsz;
299 acc += tsz;
301 /* leave now if filled buffer already */
302 if (buflen == 0)
303 return acc;
304 } else
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)
313 tsz = buflen;
315 while (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))
321 break;
323 read_unlock(&kclist_lock);
325 if (m == NULL) {
326 if (clear_user(buffer, tsz))
327 return -EFAULT;
328 } else if ((start >= VMALLOC_START) && (start < VMALLOC_END)) {
329 char * elf_buf;
330 struct vm_struct *m;
331 unsigned long curstart = start;
332 unsigned long cursize = tsz;
334 elf_buf = kzalloc(tsz, GFP_KERNEL);
335 if (!elf_buf)
336 return -ENOMEM;
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) <
345 curstart)
346 continue;
347 if ((unsigned long)m->addr > (curstart +
348 cursize))
349 break;
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;
355 else
356 vmsize = (unsigned long)m->addr +
357 msize - vmstart;
358 curstart = vmstart + vmsize;
359 cursize -= vmsize;
360 /* don't dump ioremap'd stuff! (TA) */
361 if (m->flags & VM_IOREMAP)
362 continue;
363 memcpy(elf_buf + (vmstart - start),
364 (char *)vmstart, vmsize);
366 read_unlock(&vmlist_lock);
367 if (copy_to_user(buffer, elf_buf, tsz)) {
368 kfree(elf_buf);
369 return -EFAULT;
371 kfree(elf_buf);
372 } else {
373 if (kern_addr_valid(start)) {
374 unsigned long n;
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
381 * EFAULT again.
383 if (n) {
384 if (clear_user(buffer + tsz - n,
386 return -EFAULT;
388 } else {
389 if (clear_user(buffer, tsz))
390 return -EFAULT;
393 buflen -= tsz;
394 *fpos += tsz;
395 buffer += tsz;
396 acc += tsz;
397 start += tsz;
398 tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
401 return acc;