1/*
2 *	fs/proc/kcore.c kernel ELF/AOUT 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
12#include <linux/config.h>
13#include <linux/mm.h>
14#include <linux/proc_fs.h>
15#include <linux/user.h>
16#include <linux/a.out.h>
17#include <linux/elf.h>
18#include <linux/elfcore.h>
19#include <linux/vmalloc.h>
20#include <linux/highmem.h>
21#include <asm/uaccess.h>
22#include <asm/io.h>
23
24
25static int open_kcore(struct inode * inode, struct file * filp)
26{
27	return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
28}
29
30static ssize_t read_kcore(struct file *, char *, size_t, loff_t *);
31
32struct file_operations proc_kcore_operations = {
33	read:		read_kcore,
34	open:		open_kcore,
35};
36
37#ifdef CONFIG_KCORE_AOUT
38static ssize_t read_kcore(struct file *file, char *buf, size_t count, loff_t *ppos)
39{
40	unsigned long long p = *ppos, memsize;
41	ssize_t read;
42	ssize_t count1;
43	char * pnt;
44	struct user dump;
45#if defined(__i386__) || defined(__mc68000__) || defined(__x86_64__)
46#	define FIRST_MAPPED	PAGE_SIZE	/* we don't have page 0 mapped on x86.. */
47#else
48#	define FIRST_MAPPED	0
49#endif
50
51	memset(&dump, 0, sizeof(struct user));
52	dump.magic = CMAGIC;
53	dump.u_dsize = (virt_to_phys(high_memory) >> PAGE_SHIFT);
54#if defined(__i386__) || defined(__x86_64__)
55	dump.start_code = PAGE_OFFSET;
56#endif
57#ifdef __alpha__
58	dump.start_data = PAGE_OFFSET;
59#endif
60
61	memsize = virt_to_phys(high_memory);
62	if (p >= memsize)
63		return 0;
64	if (count > memsize - p)
65		count = memsize - p;
66	read = 0;
67
68	if (p < sizeof(struct user) && count > 0) {
69		count1 = count;
70		if (p + count1 > sizeof(struct user))
71			count1 = sizeof(struct user)-p;
72		pnt = (char *) &dump + p;
73		if (copy_to_user(buf,(void *) pnt, count1))
74			return -EFAULT;
75		buf += count1;
76		p += count1;
77		count -= count1;
78		read += count1;
79	}
80
81	if (count > 0 && p < PAGE_SIZE + FIRST_MAPPED) {
82		count1 = PAGE_SIZE + FIRST_MAPPED - p;
83		if (count1 > count)
84			count1 = count;
85		if (clear_user(buf, count1))
86			return -EFAULT;
87		buf += count1;
88		p += count1;
89		count -= count1;
90		read += count1;
91	}
92	if (count > 0) {
93		if (copy_to_user(buf, (void *) (PAGE_OFFSET+p-PAGE_SIZE), count))
94			return -EFAULT;
95		read += count;
96	}
97	*ppos += read;
98	return read;
99}
100#else /* CONFIG_KCORE_AOUT */
101
102#define roundup(x, y)  ((((x)+((y)-1))/(y))*(y))
103
104/* An ELF note in memory */
105struct memelfnote
106{
107	const char *name;
108	int type;
109	unsigned int datasz;
110	void *data;
111};
112
113extern char saved_command_line[];
114
115static size_t get_kcore_size(int *num_vma, size_t *elf_buflen)
116{
117	size_t try, size;
118	struct vm_struct *m;
119
120	*num_vma = 0;
121	size = ((size_t)high_memory - PAGE_OFFSET + PAGE_SIZE);
122	if (!vmlist) {
123		*elf_buflen = PAGE_SIZE;
124		return (size);
125	}
126
127	for (m=vmlist; m; m=m->next) {
128		try = (size_t)m->addr + m->size;
129		if (try > size)
130			size = try;
131		*num_vma = *num_vma + 1;
132	}
133	*elf_buflen =	sizeof(struct elfhdr) +
134			(*num_vma + 2)*sizeof(struct elf_phdr) +
135			3 * sizeof(struct memelfnote);
136	*elf_buflen = PAGE_ALIGN(*elf_buflen);
137	return (size - PAGE_OFFSET + *elf_buflen);
138}
139
140
141/*****************************************************************************/
142/*
143 * determine size of ELF note
144 */
145static int notesize(struct memelfnote *en)
146{
147	int sz;
148
149	sz = sizeof(struct elf_note);
150	sz += roundup(strlen(en->name), 4);
151	sz += roundup(en->datasz, 4);
152
153	return sz;
154} /* end notesize() */
155
156/*****************************************************************************/
157/*
158 * store a note in the header buffer
159 */
160static char *storenote(struct memelfnote *men, char *bufp)
161{
162	struct elf_note en;
163
164#define DUMP_WRITE(addr,nr) do { memcpy(bufp,addr,nr); bufp += nr; } while(0)
165
166	en.n_namesz = strlen(men->name);
167	en.n_descsz = men->datasz;
168	en.n_type = men->type;
169
170	DUMP_WRITE(&en, sizeof(en));
171	DUMP_WRITE(men->name, en.n_namesz);
172
173	bufp = (char*) roundup((unsigned long)bufp,4);
174	DUMP_WRITE(men->data, men->datasz);
175	bufp = (char*) roundup((unsigned long)bufp,4);
176
177#undef DUMP_WRITE
178
179	return bufp;
180} /* end storenote() */
181
182/*
183 * store an ELF coredump header in the supplied buffer
184 * num_vma is the number of elements in vmlist
185 */
186static void elf_kcore_store_hdr(char *bufp, int num_vma, int dataoff)
187{
188	struct elf_prstatus prstatus;	/* NT_PRSTATUS */
189	struct elf_prpsinfo prpsinfo;	/* NT_PRPSINFO */
190	struct elf_phdr *nhdr, *phdr;
191	struct elfhdr *elf;
192	struct memelfnote notes[3];
193	off_t offset = 0;
194	struct vm_struct *m;
195
196	/* setup ELF header */
197	elf = (struct elfhdr *) bufp;
198	bufp += sizeof(struct elfhdr);
199	offset += sizeof(struct elfhdr);
200	memcpy(elf->e_ident, ELFMAG, SELFMAG);
201	elf->e_ident[EI_CLASS]	= ELF_CLASS;
202	elf->e_ident[EI_DATA]	= ELF_DATA;
203	elf->e_ident[EI_VERSION]= EV_CURRENT;
204	memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
205	elf->e_type	= ET_CORE;
206	elf->e_machine	= ELF_ARCH;
207	elf->e_version	= EV_CURRENT;
208	elf->e_entry	= 0;
209	elf->e_phoff	= sizeof(struct elfhdr);
210	elf->e_shoff	= 0;
211	elf->e_flags	= 0;
212	elf->e_ehsize	= sizeof(struct elfhdr);
213	elf->e_phentsize= sizeof(struct elf_phdr);
214	elf->e_phnum	= 2 + num_vma;
215	elf->e_shentsize= 0;
216	elf->e_shnum	= 0;
217	elf->e_shstrndx	= 0;
218
219	/* setup ELF PT_NOTE program header */
220	nhdr = (struct elf_phdr *) bufp;
221	bufp += sizeof(struct elf_phdr);
222	offset += sizeof(struct elf_phdr);
223	nhdr->p_type	= PT_NOTE;
224	nhdr->p_offset	= 0;
225	nhdr->p_vaddr	= 0;
226	nhdr->p_paddr	= 0;
227	nhdr->p_filesz	= 0;
228	nhdr->p_memsz	= 0;
229	nhdr->p_flags	= 0;
230	nhdr->p_align	= 0;
231
232	/* setup ELF PT_LOAD program header for the
233	 * virtual range 0xc0000000 -> high_memory */
234	phdr = (struct elf_phdr *) bufp;
235	bufp += sizeof(struct elf_phdr);
236	offset += sizeof(struct elf_phdr);
237	phdr->p_type	= PT_LOAD;
238	phdr->p_flags	= PF_R|PF_W|PF_X;
239	phdr->p_offset	= dataoff;
240	phdr->p_vaddr	= PAGE_OFFSET;
241	phdr->p_paddr	= __pa(PAGE_OFFSET);
242	phdr->p_filesz	= phdr->p_memsz = ((unsigned long)high_memory - PAGE_OFFSET);
243	phdr->p_align	= PAGE_SIZE;
244
245	/* setup ELF PT_LOAD program header for every vmalloc'd area */
246	for (m=vmlist; m; m=m->next) {
247		if (m->flags & VM_IOREMAP) /* don't dump ioremap'd stuff! (TA) */
248			continue;
249
250		phdr = (struct elf_phdr *) bufp;
251		bufp += sizeof(struct elf_phdr);
252		offset += sizeof(struct elf_phdr);
253
254		phdr->p_type	= PT_LOAD;
255		phdr->p_flags	= PF_R|PF_W|PF_X;
256		phdr->p_offset	= (size_t)m->addr - PAGE_OFFSET + dataoff;
257		phdr->p_vaddr	= (size_t)m->addr;
258		phdr->p_paddr	= __pa(m->addr);
259		phdr->p_filesz	= phdr->p_memsz	= m->size;
260		phdr->p_align	= PAGE_SIZE;
261	}
262
263	/*
264	 * Set up the notes in similar form to SVR4 core dumps made
265	 * with info from their /proc.
266	 */
267	nhdr->p_offset	= offset;
268
269	/* set up the process status */
270	notes[0].name = "CORE";
271	notes[0].type = NT_PRSTATUS;
272	notes[0].datasz = sizeof(struct elf_prstatus);
273	notes[0].data = &prstatus;
274
275	memset(&prstatus, 0, sizeof(struct elf_prstatus));
276
277	nhdr->p_filesz	= notesize(&notes[0]);
278	bufp = storenote(&notes[0], bufp);
279
280	/* set up the process info */
281	notes[1].name	= "CORE";
282	notes[1].type	= NT_PRPSINFO;
283	notes[1].datasz	= sizeof(struct elf_prpsinfo);
284	notes[1].data	= &prpsinfo;
285
286	memset(&prpsinfo, 0, sizeof(struct elf_prpsinfo));
287	prpsinfo.pr_state	= 0;
288	prpsinfo.pr_sname	= 'R';
289	prpsinfo.pr_zomb	= 0;
290
291	strcpy(prpsinfo.pr_fname, "vmlinux");
292	strncpy(prpsinfo.pr_psargs, saved_command_line, ELF_PRARGSZ);
293
294	nhdr->p_filesz	= notesize(&notes[1]);
295	bufp = storenote(&notes[1], bufp);
296
297	/* set up the task structure */
298	notes[2].name	= "CORE";
299	notes[2].type	= NT_TASKSTRUCT;
300	notes[2].datasz	= sizeof(struct task_struct);
301	notes[2].data	= current;
302
303	nhdr->p_filesz	= notesize(&notes[2]);
304	bufp = storenote(&notes[2], bufp);
305
306} /* end elf_kcore_store_hdr() */
307
308/*****************************************************************************/
309/*
310 * read from the ELF header and then kernel memory
311 */
312static ssize_t read_kcore(struct file *file, char *buffer, size_t buflen, loff_t *fpos)
313{
314	ssize_t acc = 0;
315	size_t size, tsz;
316	size_t elf_buflen;
317	int num_vma;
318	unsigned long start;
319
320	read_lock(&vmlist_lock);
321	proc_root_kcore->size = size = get_kcore_size(&num_vma, &elf_buflen);
322	if (buflen == 0 || *fpos >= size) {
323		read_unlock(&vmlist_lock);
324		return 0;
325	}
326
327	/* trim buflen to not go beyond EOF */
328	if (buflen > size - *fpos)
329		buflen = size - *fpos;
330
331	/* construct an ELF core header if we'll need some of it */
332	if (*fpos < elf_buflen) {
333		char * elf_buf;
334
335		tsz = elf_buflen - *fpos;
336		if (buflen < tsz)
337			tsz = buflen;
338		elf_buf = kmalloc(elf_buflen, GFP_ATOMIC);
339		if (!elf_buf) {
340			read_unlock(&vmlist_lock);
341			return -ENOMEM;
342		}
343		memset(elf_buf, 0, elf_buflen);
344		elf_kcore_store_hdr(elf_buf, num_vma, elf_buflen);
345		read_unlock(&vmlist_lock);
346		if (copy_to_user(buffer, elf_buf + *fpos, tsz)) {
347			kfree(elf_buf);
348			return -EFAULT;
349		}
350		kfree(elf_buf);
351		buflen -= tsz;
352		*fpos += tsz;
353		buffer += tsz;
354		acc += tsz;
355
356		/* leave now if filled buffer already */
357		if (buflen == 0)
358			return acc;
359	} else
360		read_unlock(&vmlist_lock);
361
362	/* where page 0 not mapped, write zeros into buffer */
363#if defined(__i386__) || defined(__mc68000__) || defined(__x86_64__)
364	if (*fpos < PAGE_SIZE + elf_buflen) {
365		/* work out how much to clear */
366		tsz = PAGE_SIZE + elf_buflen - *fpos;
367		if (buflen < tsz)
368			tsz = buflen;
369
370		/* write zeros to buffer */
371		if (clear_user(buffer, tsz))
372			return -EFAULT;
373		buflen -= tsz;
374		*fpos += tsz;
375		buffer += tsz;
376		acc += tsz;
377
378		/* leave now if filled buffer already */
379		if (buflen == 0)
380			return tsz;
381	}
382#endif
383
384	/*
385	 * Fill the remainder of the buffer from kernel VM space.
386	 * We said in the ELF header that the data which starts
387	 * at 'elf_buflen' is virtual address PAGE_OFFSET. --rmk
388	 */
389	start = PAGE_OFFSET + (*fpos - elf_buflen);
390	if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
391		tsz = buflen;
392
393	while (buflen) {
394		if ((start >= VMALLOC_START) && (start < VMALLOC_END)) {
395			char * elf_buf;
396			struct vm_struct *m;
397			unsigned long curstart = start;
398			unsigned long cursize = tsz;
399
400			elf_buf = kmalloc(tsz, GFP_KERNEL);
401			if (!elf_buf)
402				return -ENOMEM;
403			memset(elf_buf, 0, tsz);
404
405			read_lock(&vmlist_lock);
406			for (m=vmlist; m && cursize; m=m->next) {
407				unsigned long vmstart;
408				unsigned long vmsize;
409				unsigned long msize = m->size - PAGE_SIZE;
410
411				if (((unsigned long)m->addr + msize) <
412								curstart)
413					continue;
414				if ((unsigned long)m->addr > (curstart +
415								cursize))
416					break;
417				vmstart = (curstart < (unsigned long)m->addr ?
418					(unsigned long)m->addr : curstart);
419				if (((unsigned long)m->addr + msize) >
420							(curstart + cursize))
421					vmsize = curstart + cursize - vmstart;
422				else
423					vmsize = (unsigned long)m->addr +
424							msize - vmstart;
425				curstart = vmstart + vmsize;
426				cursize -= vmsize;
427				/* don't dump ioremap'd stuff! (TA) */
428				if (m->flags & VM_IOREMAP)
429					continue;
430				memcpy(elf_buf + (vmstart - start),
431					(char *)vmstart, vmsize);
432			}
433			read_unlock(&vmlist_lock);
434			if (copy_to_user(buffer, elf_buf, tsz)) {
435				kfree(elf_buf);
436				return -EFAULT;
437			}
438			kfree(elf_buf);
439		} else if ((start > PAGE_OFFSET) && (start <
440						(unsigned long)high_memory)) {
441			if (kern_addr_valid(start)) {
442				if (copy_to_user(buffer, (char *)start, tsz))
443					return -EFAULT;
444			} else {
445				if (clear_user(buffer, tsz))
446					return -EFAULT;
447			}
448		} else {
449			if (clear_user(buffer, tsz))
450				return -EFAULT;
451		}
452		buflen -= tsz;
453		*fpos += tsz;
454		buffer += tsz;
455		acc += tsz;
456		start += tsz;
457		tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
458	}
459
460	return acc;
461}
462#endif /* CONFIG_KCORE_AOUT */
463