MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / fs / proc / kcore.c
blob1294eda4acaeefc5739bd4ad267a7fca2dd679dd
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>
26 static int open_kcore(struct inode * inode, struct file * filp)
28 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
31 static ssize_t read_kcore(struct file *, char __user *, size_t, loff_t *);
33 const struct file_operations proc_kcore_operations = {
34 .read = read_kcore,
35 .open = open_kcore,
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
45 /* An ELF note in memory */
46 struct memelfnote
48 const char *name;
49 int type;
50 unsigned int datasz;
51 void *data;
54 static struct kcore_list *kclist;
55 static DEFINE_RWLOCK(kclist_lock);
57 void
58 kclist_add(struct kcore_list *new, void *addr, size_t size)
60 new->addr = (unsigned long)addr;
61 new->size = size;
63 write_lock(&kclist_lock);
64 new->next = kclist;
65 kclist = new;
66 write_unlock(&kclist_lock);
69 static size_t get_kcore_size(int *nphdr, size_t *elf_buflen)
71 size_t try, size;
72 struct kcore_list *m;
74 *nphdr = 1; /* PT_NOTE */
75 size = 0;
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;
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;
94 /*****************************************************************************/
96 * determine size of ELF note
98 static int notesize(struct memelfnote *en)
100 int sz;
102 sz = sizeof(struct elf_note);
103 sz += roundup((strlen(en->name) + 1), 4);
104 sz += roundup(en->datasz, 4);
106 return sz;
107 } /* end notesize() */
109 /*****************************************************************************/
111 * store a note in the header buffer
113 static char *storenote(struct memelfnote *men, char *bufp)
115 struct elf_note en;
117 #define DUMP_WRITE(addr,nr) do { memcpy(bufp,addr,nr); bufp += nr; } while(0)
119 en.n_namesz = strlen(men->name) + 1;
120 en.n_descsz = men->datasz;
121 en.n_type = men->type;
123 DUMP_WRITE(&en, sizeof(en));
124 DUMP_WRITE(men->name, en.n_namesz);
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);
131 #undef DUMP_WRITE
133 return bufp;
134 } /* end storenote() */
137 * store an ELF coredump header in the supplied buffer
138 * nphdr is the number of elf_phdr to insert
140 static void elf_kcore_store_hdr(char *bufp, int nphdr, int dataoff)
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;
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;
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;
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);
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;
207 * Set up the notes in similar form to SVR4 core dumps made
208 * with info from their /proc.
210 nhdr->p_offset = offset;
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;
218 memset(&prstatus, 0, sizeof(struct elf_prstatus));
220 nhdr->p_filesz = notesize(&notes[0]);
221 bufp = storenote(&notes[0], bufp);
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;
229 memset(&prpsinfo, 0, sizeof(struct elf_prpsinfo));
230 prpsinfo.pr_state = 0;
231 prpsinfo.pr_sname = 'R';
232 prpsinfo.pr_zomb = 0;
234 strcpy(prpsinfo.pr_fname, "vmlinux");
235 strncpy(prpsinfo.pr_psargs, saved_command_line, ELF_PRARGSZ);
237 nhdr->p_filesz += notesize(&notes[1]);
238 bufp = storenote(&notes[1], bufp);
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;
246 nhdr->p_filesz += notesize(&notes[2]);
247 bufp = storenote(&notes[2], bufp);
249 } /* end elf_kcore_store_hdr() */
251 /*****************************************************************************/
253 * read from the ELF header and then kernel memory
255 static ssize_t
256 read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
258 ssize_t acc = 0;
259 size_t size, tsz;
260 size_t elf_buflen;
261 int nphdr;
262 unsigned long start;
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;
271 /* trim buflen to not go beyond EOF */
272 if (buflen > size - *fpos)
273 buflen = size - *fpos;
275 /* construct an ELF core header if we'll need some of it */
276 if (*fpos < elf_buflen) {
277 char * elf_buf;
279 tsz = elf_buflen - *fpos;
280 if (buflen < tsz)
281 tsz = buflen;
282 elf_buf = kzalloc(elf_buflen, GFP_ATOMIC);
283 if (!elf_buf) {
284 read_unlock(&kclist_lock);
285 return -ENOMEM;
287 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;
293 kfree(elf_buf);
294 buflen -= tsz;
295 *fpos += tsz;
296 buffer += tsz;
297 acc += tsz;
299 /* leave now if filled buffer already */
300 if (buflen == 0)
301 return acc;
302 } else
303 read_unlock(&kclist_lock);
306 * Check to see if our file offset matches with any of
307 * the addresses in the elf_phdr on our list.
309 start = kc_offset_to_vaddr(*fpos - elf_buflen);
310 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
311 tsz = buflen;
313 while (buflen) {
314 struct kcore_list *m;
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;
321 read_unlock(&kclist_lock);
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;
332 elf_buf = kzalloc(tsz, GFP_KERNEL);
333 if (!elf_buf)
334 return -ENOMEM;
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;
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);
364 read_unlock(&vmlist_lock);
365 if (copy_to_user(buffer, elf_buf, tsz)) {
366 kfree(elf_buf);
367 return -EFAULT;
369 kfree(elf_buf);
370 } else {
371 if (kern_addr_valid(start)) {
372 unsigned long n;
374 n = copy_to_user(buffer, (char *)start, tsz);
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.
381 if (n) {
382 if (clear_user(buffer + tsz - n,
384 return -EFAULT;
386 } else {
387 if (clear_user(buffer, tsz))
388 return -EFAULT;
391 buflen -= tsz;
392 *fpos += tsz;
393 buffer += tsz;
394 acc += tsz;
395 start += tsz;
396 tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
399 return acc;