1 /*
2  * irixelf.c: Code to load IRIX ELF executables which conform to
3  *            the MIPS ABI.
4  *
5  * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
6  *
7  * Based upon work which is:
8  * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/fs.h>
14 #include <linux/stat.h>
15 #include <linux/sched.h>
16 #include <linux/mm.h>
17 #include <linux/mman.h>
18 #include <linux/a.out.h>
19 #include <linux/errno.h>
20 #include <linux/init.h>
21 #include <linux/signal.h>
22 #include <linux/binfmts.h>
23 #include <linux/string.h>
24 #include <linux/file.h>
25 #include <linux/fcntl.h>
26 #include <linux/slab.h>
27 #include <linux/shm.h>
28 #include <linux/personality.h>
29 #include <linux/elfcore.h>
30 #include <linux/smp_lock.h>
31 
32 #include <asm/uaccess.h>
33 #include <asm/pgalloc.h>
34 #include <asm/ptrace.h>
35 #include <asm/mipsregs.h>
36 #include <asm/prctl.h>
37 
38 #define DLINFO_ITEMS 12
39 
40 #include <linux/elf.h>
41 
42 #undef DEBUG_ELF
43 
44 static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs);
45 static int load_irix_library(struct file *);
46 static int irix_core_dump(long signr, struct pt_regs * regs,
47                           struct file *file);
48 extern int dump_fpu (elf_fpregset_t *);
49 
50 static struct linux_binfmt irix_format = {
51 	NULL, THIS_MODULE, load_irix_binary, load_irix_library,
52 	irix_core_dump, PAGE_SIZE
53 };
54 
55 #ifndef elf_addr_t
56 #define elf_addr_t unsigned long
57 #define elf_caddr_t char *
58 #endif
59 
60 #ifdef DEBUG_ELF
61 /* Debugging routines. */
get_elf_p_type(Elf32_Word p_type)62 static char *get_elf_p_type(Elf32_Word p_type)
63 {
64 	int i = (int) p_type;
65 
66 	switch(i) {
67 	case PT_NULL: return("PT_NULL"); break;
68 	case PT_LOAD: return("PT_LOAD"); break;
69 	case PT_DYNAMIC: return("PT_DYNAMIC"); break;
70 	case PT_INTERP: return("PT_INTERP"); break;
71 	case PT_NOTE: return("PT_NOTE"); break;
72 	case PT_SHLIB: return("PT_SHLIB"); break;
73 	case PT_PHDR: return("PT_PHDR"); break;
74 	case PT_LOPROC: return("PT_LOPROC/REGINFO"); break;
75 	case PT_HIPROC: return("PT_HIPROC"); break;
76 	default: return("PT_BOGUS"); break;
77 	}
78 }
79 
print_elfhdr(struct elfhdr * ehp)80 static void print_elfhdr(struct elfhdr *ehp)
81 {
82 	int i;
83 
84 	printk("ELFHDR: e_ident<");
85 	for(i = 0; i < (EI_NIDENT - 1); i++) printk("%x ", ehp->e_ident[i]);
86 	printk("%x>\n", ehp->e_ident[i]);
87 	printk("        e_type[%04x] e_machine[%04x] e_version[%08lx]\n",
88 	       (unsigned short) ehp->e_type, (unsigned short) ehp->e_machine,
89 	       (unsigned long) ehp->e_version);
90 	printk("        e_entry[%08lx] e_phoff[%08lx] e_shoff[%08lx] "
91 	       "e_flags[%08lx]\n",
92 	       (unsigned long) ehp->e_entry, (unsigned long) ehp->e_phoff,
93 	       (unsigned long) ehp->e_shoff, (unsigned long) ehp->e_flags);
94 	printk("        e_ehsize[%04x] e_phentsize[%04x] e_phnum[%04x]\n",
95 	       (unsigned short) ehp->e_ehsize, (unsigned short) ehp->e_phentsize,
96 	       (unsigned short) ehp->e_phnum);
97 	printk("        e_shentsize[%04x] e_shnum[%04x] e_shstrndx[%04x]\n",
98 	       (unsigned short) ehp->e_shentsize, (unsigned short) ehp->e_shnum,
99 	       (unsigned short) ehp->e_shstrndx);
100 }
101 
print_phdr(int i,struct elf_phdr * ep)102 static void print_phdr(int i, struct elf_phdr *ep)
103 {
104 	printk("PHDR[%d]: p_type[%s] p_offset[%08lx] p_vaddr[%08lx] "
105 	       "p_paddr[%08lx]\n", i, get_elf_p_type(ep->p_type),
106 	       (unsigned long) ep->p_offset, (unsigned long) ep->p_vaddr,
107 	       (unsigned long) ep->p_paddr);
108 	printk("         p_filesz[%08lx] p_memsz[%08lx] p_flags[%08lx] "
109 	       "p_align[%08lx]\n", (unsigned long) ep->p_filesz,
110 	       (unsigned long) ep->p_memsz, (unsigned long) ep->p_flags,
111 	       (unsigned long) ep->p_align);
112 }
113 
dump_phdrs(struct elf_phdr * ep,int pnum)114 static void dump_phdrs(struct elf_phdr *ep, int pnum)
115 {
116 	int i;
117 
118 	for(i = 0; i < pnum; i++, ep++) {
119 		if((ep->p_type == PT_LOAD) ||
120 		   (ep->p_type == PT_INTERP) ||
121 		   (ep->p_type == PT_PHDR))
122 			print_phdr(i, ep);
123 	}
124 }
125 #endif /* (DEBUG_ELF) */
126 
set_brk(unsigned long start,unsigned long end)127 static void set_brk(unsigned long start, unsigned long end)
128 {
129 	start = PAGE_ALIGN(start);
130 	end = PAGE_ALIGN(end);
131 	if (end <= start)
132 		return;
133 
134 	down_write(&current->mm->mmap_sem);
135 	do_brk(start, end - start);
136 	up_write(&current->mm->mmap_sem);
137 }
138 
139 
140 /* We need to explicitly zero any fractional pages
141  * after the data section (i.e. bss).  This would
142  * contain the junk from the file that should not
143  * be in memory.
144  */
padzero(unsigned long elf_bss)145 static void padzero(unsigned long elf_bss)
146 {
147 	unsigned long nbyte;
148 
149 	nbyte = elf_bss & (PAGE_SIZE-1);
150 	if (nbyte) {
151 		nbyte = PAGE_SIZE - nbyte;
152 		clear_user((void *) elf_bss, nbyte);
153 	}
154 }
155 
create_irix_tables(char * p,int argc,int envc,struct elfhdr * exec,unsigned int load_addr,unsigned int interp_load_addr,struct pt_regs * regs,struct elf_phdr * ephdr)156 unsigned long * create_irix_tables(char * p, int argc, int envc,
157 				   struct elfhdr * exec, unsigned int load_addr,
158 				   unsigned int interp_load_addr,
159 				   struct pt_regs *regs, struct elf_phdr *ephdr)
160 {
161 	elf_caddr_t *argv;
162 	elf_caddr_t *envp;
163 	elf_addr_t *sp, *csp;
164 
165 #ifdef DEBUG_ELF
166 	printk("create_irix_tables: p[%p] argc[%d] envc[%d] "
167 	       "load_addr[%08x] interp_load_addr[%08x]\n",
168 	       p, argc, envc, load_addr, interp_load_addr);
169 #endif
170 	sp = (elf_addr_t *) (~15UL & (unsigned long) p);
171 	csp = sp;
172 	csp -= exec ? DLINFO_ITEMS*2 : 2;
173 	csp -= envc+1;
174 	csp -= argc+1;
175 	csp -= 1;		/* argc itself */
176 	if ((unsigned long)csp & 15UL) {
177 		sp -= (16UL - ((unsigned long)csp & 15UL)) / sizeof(*sp);
178 	}
179 
180 	/*
181 	 * Put the ELF interpreter info on the stack
182 	 */
183 #define NEW_AUX_ENT(nr, id, val) \
184 	  __put_user ((id), sp+(nr*2)); \
185 	  __put_user ((val), sp+(nr*2+1)); \
186 
187 	sp -= 2;
188 	NEW_AUX_ENT(0, AT_NULL, 0);
189 
190 	if(exec) {
191 		sp -= 11*2;
192 
193 		NEW_AUX_ENT (0, AT_PHDR, load_addr + exec->e_phoff);
194 		NEW_AUX_ENT (1, AT_PHENT, sizeof (struct elf_phdr));
195 		NEW_AUX_ENT (2, AT_PHNUM, exec->e_phnum);
196 		NEW_AUX_ENT (3, AT_PAGESZ, ELF_EXEC_PAGESIZE);
197 		NEW_AUX_ENT (4, AT_BASE, interp_load_addr);
198 		NEW_AUX_ENT (5, AT_FLAGS, 0);
199 		NEW_AUX_ENT (6, AT_ENTRY, (elf_addr_t) exec->e_entry);
200 		NEW_AUX_ENT (7, AT_UID, (elf_addr_t) current->uid);
201 		NEW_AUX_ENT (8, AT_EUID, (elf_addr_t) current->euid);
202 		NEW_AUX_ENT (9, AT_GID, (elf_addr_t) current->gid);
203 		NEW_AUX_ENT (10, AT_EGID, (elf_addr_t) current->egid);
204 	}
205 #undef NEW_AUX_ENT
206 
207 	sp -= envc+1;
208 	envp = (elf_caddr_t *) sp;
209 	sp -= argc+1;
210 	argv = (elf_caddr_t *) sp;
211 
212 	__put_user((elf_addr_t)argc,--sp);
213 	current->mm->arg_start = (unsigned long) p;
214 	while (argc-->0) {
215 		__put_user((elf_caddr_t)(unsigned long)p,argv++);
216 		p += strlen_user(p);
217 	}
218 	__put_user(NULL, argv);
219 	current->mm->arg_end = current->mm->env_start = (unsigned long) p;
220 	while (envc-->0) {
221 		__put_user((elf_caddr_t)(unsigned long)p,envp++);
222 		p += strlen_user(p);
223 	}
224 	__put_user(NULL, envp);
225 	current->mm->env_end = (unsigned long) p;
226 	return sp;
227 }
228 
229 
230 /* This is much more generalized than the library routine read function,
231  * so we keep this separate.  Technically the library read function
232  * is only provided so that we can read a.out libraries that have
233  * an ELF header.
234  */
load_irix_interp(struct elfhdr * interp_elf_ex,struct file * interpreter,unsigned int * interp_load_addr)235 static unsigned int load_irix_interp(struct elfhdr * interp_elf_ex,
236 				     struct file * interpreter,
237 				     unsigned int *interp_load_addr)
238 {
239 	struct elf_phdr *elf_phdata  =  NULL;
240 	struct elf_phdr *eppnt;
241 	unsigned int len;
242 	unsigned int load_addr;
243 	int elf_bss;
244 	int retval;
245 	unsigned int last_bss;
246 	int error;
247 	int i;
248 	unsigned int k;
249 
250 	elf_bss = 0;
251 	last_bss = 0;
252 	error = load_addr = 0;
253 
254 #ifdef DEBUG_ELF
255 	print_elfhdr(interp_elf_ex);
256 #endif
257 
258 	/* First of all, some simple consistency checks */
259 	if ((interp_elf_ex->e_type != ET_EXEC &&
260 	     interp_elf_ex->e_type != ET_DYN) ||
261 	     !irix_elf_check_arch(interp_elf_ex) ||
262 	     !interpreter->f_op->mmap) {
263 		printk("IRIX interp has bad e_type %d\n", interp_elf_ex->e_type);
264 		return 0xffffffff;
265 	}
266 
267 	/* Now read in all of the header information */
268 	if(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > PAGE_SIZE) {
269 	    printk("IRIX interp header bigger than a page (%d)\n",
270 		   (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum));
271 	    return 0xffffffff;
272 	}
273 
274 	elf_phdata =  (struct elf_phdr *)
275 		kmalloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum,
276 			GFP_KERNEL);
277 
278 	if(!elf_phdata) {
279           printk("Cannot kmalloc phdata for IRIX interp.\n");
280 	  return 0xffffffff;
281 	}
282 
283 	/* If the size of this structure has changed, then punt, since
284 	 * we will be doing the wrong thing.
285 	 */
286 	if(interp_elf_ex->e_phentsize != 32) {
287 		printk("IRIX interp e_phentsize == %d != 32 ",
288 		       interp_elf_ex->e_phentsize);
289 		kfree(elf_phdata);
290 		return 0xffffffff;
291 	}
292 
293 	retval = kernel_read(interpreter, interp_elf_ex->e_phoff,
294 			   (char *) elf_phdata,
295 			   sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
296 
297 #ifdef DEBUG_ELF
298 	dump_phdrs(elf_phdata, interp_elf_ex->e_phnum);
299 #endif
300 
301 	eppnt = elf_phdata;
302 	for(i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
303 	  if(eppnt->p_type == PT_LOAD) {
304 	    int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
305 	    int elf_prot = 0;
306 	    unsigned long vaddr = 0;
307 	    if (eppnt->p_flags & PF_R) elf_prot =  PROT_READ;
308 	    if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
309 	    if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
310 	    elf_type |= MAP_FIXED;
311 	    vaddr = eppnt->p_vaddr;
312 
313 #ifdef DEBUG_ELF
314 	    printk("INTERP do_mmap(%p, %08lx, %08lx, %08lx, %08lx, %08lx) ",
315 		   interpreter, vaddr,
316 		   (unsigned long) (eppnt->p_filesz + (eppnt->p_vaddr & 0xfff)),
317 		   (unsigned long) elf_prot, (unsigned long) elf_type,
318 		   (unsigned long) (eppnt->p_offset & 0xfffff000));
319 #endif
320 	    down_write(&current->mm->mmap_sem);
321 	    error = do_mmap(interpreter, vaddr,
322 			    eppnt->p_filesz + (eppnt->p_vaddr & 0xfff),
323 			    elf_prot, elf_type,
324 			    eppnt->p_offset & 0xfffff000);
325 	    up_write(&current->mm->mmap_sem);
326 
327 	    if(error < 0 && error > -1024) {
328 		    printk("Aieee IRIX interp mmap error=%d\n", error);
329 		    break;  /* Real error */
330 	    }
331 #ifdef DEBUG_ELF
332 	    printk("error=%08lx ", (unsigned long) error);
333 #endif
334 	    if(!load_addr && interp_elf_ex->e_type == ET_DYN) {
335 	      load_addr = error;
336 #ifdef DEBUG_ELF
337               printk("load_addr = error ");
338 #endif
339 	    }
340 
341 	    /* Find the end of the file  mapping for this phdr, and keep
342 	     * track of the largest address we see for this.
343 	     */
344 	    k = eppnt->p_vaddr + eppnt->p_filesz;
345 	    if(k > elf_bss) elf_bss = k;
346 
347 	    /* Do the same thing for the memory mapping - between
348 	     * elf_bss and last_bss is the bss section.
349 	     */
350 	    k = eppnt->p_memsz + eppnt->p_vaddr;
351 	    if(k > last_bss) last_bss = k;
352 #ifdef DEBUG_ELF
353 	    printk("\n");
354 #endif
355 	  }
356 	}
357 
358 	/* Now use mmap to map the library into memory. */
359 	if(error < 0 && error > -1024) {
360 #ifdef DEBUG_ELF
361 		printk("got error %d\n", error);
362 #endif
363 		kfree(elf_phdata);
364 		return 0xffffffff;
365 	}
366 
367 	/* Now fill out the bss section.  First pad the last page up
368 	 * to the page boundary, and then perform a mmap to make sure
369 	 * that there are zero-mapped pages up to and including the
370 	 * last bss page.
371 	 */
372 #ifdef DEBUG_ELF
373 	printk("padzero(%08lx) ", (unsigned long) (elf_bss));
374 #endif
375 	padzero(elf_bss);
376 	len = (elf_bss + 0xfff) & 0xfffff000; /* What we have mapped so far */
377 
378 #ifdef DEBUG_ELF
379 	printk("last_bss[%08lx] len[%08lx]\n", (unsigned long) last_bss,
380 	       (unsigned long) len);
381 #endif
382 
383 	/* Map the last of the bss segment */
384 	if (last_bss > len) {
385 		down_write(&current->mm->mmap_sem);
386 		do_brk(len, (last_bss - len));
387 		up_write(&current->mm->mmap_sem);
388 	}
389 	kfree(elf_phdata);
390 
391 	*interp_load_addr = load_addr;
392 	return ((unsigned int) interp_elf_ex->e_entry);
393 }
394 
395 /* Check sanity of IRIX elf executable header. */
verify_binary(struct elfhdr * ehp,struct linux_binprm * bprm)396 static int verify_binary(struct elfhdr *ehp, struct linux_binprm *bprm)
397 {
398 	if (memcmp(ehp->e_ident, ELFMAG, SELFMAG) != 0)
399 		return -ENOEXEC;
400 
401 	/* First of all, some simple consistency checks */
402 	if((ehp->e_type != ET_EXEC && ehp->e_type != ET_DYN) ||
403 	    !irix_elf_check_arch(ehp) || !bprm->file->f_op->mmap) {
404 		return -ENOEXEC;
405 	}
406 
407 	/* Only support MIPS ARCH2 or greater IRIX binaries for now. */
408 	if(!(ehp->e_flags & EF_MIPS_ARCH) && !(ehp->e_flags & 0x04)) {
409 		return -ENOEXEC;
410 	}
411 
412 	/* XXX Don't support N32 or 64bit binaries yet because they can
413 	 * XXX and do execute 64 bit instructions and expect all registers
414 	 * XXX to be 64 bit as well.  We need to make the kernel save
415 	 * XXX all registers as 64bits on cpu's capable of this at
416 	 * XXX exception time plus frob the XTLB exception vector.
417 	 */
418 	if((ehp->e_flags & 0x20)) {
419 		return -ENOEXEC;
420 	}
421 
422 	return 0; /* It's ok. */
423 }
424 
425 #define IRIX_INTERP_PREFIX "/usr/gnemul/irix"
426 
427 /* Look for an IRIX ELF interpreter. */
look_for_irix_interpreter(char ** name,struct file ** interpreter,struct elfhdr * interp_elf_ex,struct elf_phdr * epp,struct linux_binprm * bprm,int pnum)428 static inline int look_for_irix_interpreter(char **name,
429 					    struct file **interpreter,
430 					    struct elfhdr *interp_elf_ex,
431 					    struct elf_phdr *epp,
432 					    struct linux_binprm *bprm, int pnum)
433 {
434 	int i;
435 	int retval = -EINVAL;
436 	struct file *file = NULL;
437 
438 	*name = NULL;
439 	for(i = 0; i < pnum; i++, epp++) {
440 		if (epp->p_type != PT_INTERP)
441 			continue;
442 
443 		/* It is illegal to have two interpreters for one executable. */
444 		if (*name != NULL)
445 			goto out;
446 
447 		*name = (char *) kmalloc((epp->p_filesz +
448 					  strlen(IRIX_INTERP_PREFIX)),
449 					 GFP_KERNEL);
450 		if (!*name)
451 			return -ENOMEM;
452 
453 		strcpy(*name, IRIX_INTERP_PREFIX);
454 		retval = kernel_read(bprm->file, epp->p_offset, (*name + 16),
455 		                     epp->p_filesz);
456 		if (retval < 0)
457 			goto out;
458 
459 		file = open_exec(*name);
460 		if (IS_ERR(file)) {
461 			retval = PTR_ERR(file);
462 			goto out;
463 		}
464 		retval = kernel_read(file, 0, bprm->buf, 128);
465 		if (retval < 0)
466 			goto dput_and_out;
467 
468 		*interp_elf_ex = *(struct elfhdr *) bprm->buf;
469 	}
470 	*interpreter = file;
471 	return 0;
472 
473 dput_and_out:
474 	fput(file);
475 out:
476 	kfree(*name);
477 	return retval;
478 }
479 
verify_irix_interpreter(struct elfhdr * ihp)480 static inline int verify_irix_interpreter(struct elfhdr *ihp)
481 {
482 	if (memcmp(ihp->e_ident, ELFMAG, SELFMAG) != 0)
483 		return -ELIBBAD;
484 	return 0;
485 }
486 
487 #define EXEC_MAP_FLAGS (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE)
488 
map_executable(struct file * fp,struct elf_phdr * epp,int pnum,unsigned int * estack,unsigned int * laddr,unsigned int * scode,unsigned int * ebss,unsigned int * ecode,unsigned int * edata,unsigned int * ebrk)489 static inline void map_executable(struct file *fp, struct elf_phdr *epp, int pnum,
490 				  unsigned int *estack, unsigned int *laddr,
491 				  unsigned int *scode, unsigned int *ebss,
492 				  unsigned int *ecode, unsigned int *edata,
493 				  unsigned int *ebrk)
494 {
495 	unsigned int tmp;
496 	int i, prot;
497 
498 	for(i = 0; i < pnum; i++, epp++) {
499 		if(epp->p_type != PT_LOAD)
500 			continue;
501 
502 		/* Map it. */
503 		prot  = (epp->p_flags & PF_R) ? PROT_READ : 0;
504 		prot |= (epp->p_flags & PF_W) ? PROT_WRITE : 0;
505 		prot |= (epp->p_flags & PF_X) ? PROT_EXEC : 0;
506 	        down_write(&current->mm->mmap_sem);
507 		(void) do_mmap(fp, (epp->p_vaddr & 0xfffff000),
508 			       (epp->p_filesz + (epp->p_vaddr & 0xfff)),
509 			       prot, EXEC_MAP_FLAGS,
510 			       (epp->p_offset & 0xfffff000));
511 	        up_write(&current->mm->mmap_sem);
512 
513 		/* Fixup location tracking vars. */
514 		if((epp->p_vaddr & 0xfffff000) < *estack)
515 			*estack = (epp->p_vaddr & 0xfffff000);
516 		if(!*laddr)
517 			*laddr = epp->p_vaddr - epp->p_offset;
518 		if(epp->p_vaddr < *scode)
519 			*scode = epp->p_vaddr;
520 
521 		tmp = epp->p_vaddr + epp->p_filesz;
522 		if(tmp > *ebss)
523 			*ebss = tmp;
524 		if((epp->p_flags & PF_X) && *ecode < tmp)
525 			*ecode = tmp;
526 		if(*edata < tmp)
527 			*edata = tmp;
528 
529 		tmp = epp->p_vaddr + epp->p_memsz;
530 		if(tmp > *ebrk)
531 			*ebrk = tmp;
532 	}
533 
534 }
535 
map_interpreter(struct elf_phdr * epp,struct elfhdr * ihp,struct file * interp,unsigned int * iladdr,int pnum,mm_segment_t old_fs,unsigned int * eentry)536 static inline int map_interpreter(struct elf_phdr *epp, struct elfhdr *ihp,
537 				  struct file *interp, unsigned int *iladdr,
538 				  int pnum, mm_segment_t old_fs,
539 				  unsigned int *eentry)
540 {
541 	int i;
542 
543 	*eentry = 0xffffffff;
544 	for(i = 0; i < pnum; i++, epp++) {
545 		if(epp->p_type != PT_INTERP)
546 			continue;
547 
548 		/* We should have fielded this error elsewhere... */
549 		if(*eentry != 0xffffffff)
550 			return -1;
551 
552 		set_fs(old_fs);
553 		*eentry = load_irix_interp(ihp, interp, iladdr);
554 		old_fs = get_fs();
555 		set_fs(get_ds());
556 
557 		fput(interp);
558 
559 		if (*eentry == 0xffffffff)
560 			return -1;
561 	}
562 	return 0;
563 }
564 
565 /*
566  * IRIX maps a page at 0x200000 that holds information about the
567  * process and the system, here we map the page and fill the
568  * structure
569  */
irix_map_prda_page(void)570 void irix_map_prda_page (void)
571 {
572 	unsigned long v;
573 	struct prda *pp;
574 
575 	down_write(&current->mm->mmap_sem);
576 	v =  do_brk (PRDA_ADDRESS, PAGE_SIZE);
577 	up_write(&current->mm->mmap_sem);
578 
579 	if (v < 0)
580 		return;
581 
582 	pp = (struct prda *) v;
583 	pp->prda_sys.t_pid  = current->pid;
584 	pp->prda_sys.t_prid = read_c0_prid();
585 	pp->prda_sys.t_rpid = current->pid;
586 
587 	/* We leave the rest set to zero */
588 }
589 
590 
591 
592 /* These are the functions used to load ELF style executables and shared
593  * libraries.  There is no binary dependent code anywhere else.
594  */
load_irix_binary(struct linux_binprm * bprm,struct pt_regs * regs)595 static int load_irix_binary(struct linux_binprm * bprm, struct pt_regs * regs)
596 {
597 	struct elfhdr elf_ex, interp_elf_ex;
598 	struct file *interpreter;
599 	struct elf_phdr *elf_phdata, *elf_ihdr, *elf_ephdr;
600 	unsigned int load_addr, elf_bss, elf_brk;
601 	unsigned int elf_entry, interp_load_addr = 0;
602 	unsigned int start_code, end_code, end_data, elf_stack;
603 	int retval, has_interp, has_ephdr, size, i;
604 	char *elf_interpreter;
605 	mm_segment_t old_fs;
606 
607 	load_addr = 0;
608 	has_interp = has_ephdr = 0;
609 	elf_ihdr = elf_ephdr = 0;
610 	elf_ex = *((struct elfhdr *) bprm->buf);
611 	retval = -ENOEXEC;
612 
613 	if (verify_binary(&elf_ex, bprm))
614 		goto out;
615 
616 #ifdef DEBUG_ELF
617 	print_elfhdr(&elf_ex);
618 #endif
619 
620 	/* Now read in all of the header information */
621 	size = elf_ex.e_phentsize * elf_ex.e_phnum;
622 	if (size > 65536)
623 		goto out;
624 	elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
625 	if (elf_phdata == NULL) {
626 		retval = -ENOMEM;
627 		goto out;
628 	}
629 
630 	retval = kernel_read(bprm->file, elf_ex.e_phoff, (char *)elf_phdata, size);
631 	if (retval < 0)
632 		goto out_free_ph;
633 
634 #ifdef DEBUG_ELF
635 	dump_phdrs(elf_phdata, elf_ex.e_phnum);
636 #endif
637 
638 	/* Set some things for later. */
639 	for(i = 0; i < elf_ex.e_phnum; i++) {
640 		switch(elf_phdata[i].p_type) {
641 		case PT_INTERP:
642 			has_interp = 1;
643 			elf_ihdr = &elf_phdata[i];
644 			break;
645 		case PT_PHDR:
646 			has_ephdr = 1;
647 			elf_ephdr = &elf_phdata[i];
648 			break;
649 		};
650 	}
651 #ifdef DEBUG_ELF
652 	printk("\n");
653 #endif
654 
655 	elf_bss = 0;
656 	elf_brk = 0;
657 
658 	elf_stack = 0xffffffff;
659 	elf_interpreter = NULL;
660 	start_code = 0xffffffff;
661 	end_code = 0;
662 	end_data = 0;
663 
664 	retval = look_for_irix_interpreter(&elf_interpreter,
665 	                                   &interpreter,
666 					   &interp_elf_ex, elf_phdata, bprm,
667 					   elf_ex.e_phnum);
668 	if (retval)
669 		goto out_free_file;
670 
671 	if (elf_interpreter) {
672 		retval = verify_irix_interpreter(&interp_elf_ex);
673 		if(retval)
674 			goto out_free_interp;
675 	}
676 
677 	/* OK, we are done with that, now set up the arg stuff,
678 	 * and then start this sucker up.
679 	 */
680 	retval = -E2BIG;
681 	if (!bprm->sh_bang && !bprm->p)
682 		goto out_free_interp;
683 
684 	/* Flush all traces of the currently running executable */
685 	retval = flush_old_exec(bprm);
686 	if (retval)
687 		goto out_free_dentry;
688 
689 	/* OK, This is the point of no return */
690 	current->mm->end_data = 0;
691 	current->mm->end_code = 0;
692 	current->mm->mmap = NULL;
693 	current->flags &= ~PF_FORKNOEXEC;
694 	elf_entry = (unsigned int) elf_ex.e_entry;
695 
696 	/* Do this so that we can load the interpreter, if need be.  We will
697 	 * change some of these later.
698 	 */
699 	current->mm->rss = 0;
700 	setup_arg_pages(bprm);
701 	current->mm->start_stack = bprm->p;
702 
703 	/* At this point, we assume that the image should be loaded at
704 	 * fixed address, not at a variable address.
705 	 */
706 	old_fs = get_fs();
707 	set_fs(get_ds());
708 
709 	map_executable(bprm->file, elf_phdata, elf_ex.e_phnum, &elf_stack,
710 	               &load_addr, &start_code, &elf_bss, &end_code,
711 	               &end_data, &elf_brk);
712 
713 	if(elf_interpreter) {
714 		retval = map_interpreter(elf_phdata, &interp_elf_ex,
715 					 interpreter, &interp_load_addr,
716 					 elf_ex.e_phnum, old_fs, &elf_entry);
717 		kfree(elf_interpreter);
718 		if(retval) {
719 			set_fs(old_fs);
720 			printk("Unable to load IRIX ELF interpreter\n");
721 			send_sig(SIGSEGV, current, 0);
722 			retval = 0;
723 			goto out_free_file;
724 		}
725 	}
726 
727 	set_fs(old_fs);
728 
729 	kfree(elf_phdata);
730 	set_personality(PER_IRIX32);
731 	set_binfmt(&irix_format);
732 	compute_creds(bprm);
733 	current->flags &= ~PF_FORKNOEXEC;
734 	bprm->p = (unsigned long)
735 	  create_irix_tables((char *)bprm->p, bprm->argc, bprm->envc,
736 			(elf_interpreter ? &elf_ex : NULL),
737 			load_addr, interp_load_addr, regs, elf_ephdr);
738 	current->mm->start_brk = current->mm->brk = elf_brk;
739 	current->mm->end_code = end_code;
740 	current->mm->start_code = start_code;
741 	current->mm->end_data = end_data;
742 	current->mm->start_stack = bprm->p;
743 
744 	/* Calling set_brk effectively mmaps the pages that we need for the
745 	 * bss and break sections.
746 	 */
747 	set_brk(elf_bss, elf_brk);
748 
749 	/*
750 	 * IRIX maps a page at 0x200000 which holds some system
751 	 * information.  Programs depend on this.
752 	 */
753 	irix_map_prda_page ();
754 
755 	padzero(elf_bss);
756 
757 #ifdef DEBUG_ELF
758 	printk("(start_brk) %lx\n" , (long) current->mm->start_brk);
759 	printk("(end_code) %lx\n" , (long) current->mm->end_code);
760 	printk("(start_code) %lx\n" , (long) current->mm->start_code);
761 	printk("(end_data) %lx\n" , (long) current->mm->end_data);
762 	printk("(start_stack) %lx\n" , (long) current->mm->start_stack);
763 	printk("(brk) %lx\n" , (long) current->mm->brk);
764 #endif
765 
766 #if 0 /* XXX No fucking way dude... */
767 	/* Why this, you ask???  Well SVr4 maps page 0 as read-only,
768 	 * and some applications "depend" upon this behavior.
769 	 * Since we do not have the power to recompile these, we
770 	 * emulate the SVr4 behavior.  Sigh.
771 	 */
772 	down_write(&current->mm->mmap_sem);
773 	(void) do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
774 		       MAP_FIXED | MAP_PRIVATE, 0);
775 	up_write(&current->mm->mmap_sem);
776 #endif
777 
778 	start_thread(regs, elf_entry, bprm->p);
779 	if (current->ptrace & PT_PTRACED)
780 		send_sig(SIGTRAP, current, 0);
781 	return 0;
782 out:
783 	return retval;
784 
785 out_free_dentry:
786 	allow_write_access(interpreter);
787 	fput(interpreter);
788 out_free_interp:
789 	if (elf_interpreter)
790 		kfree(elf_interpreter);
791 out_free_file:
792 out_free_ph:
793 	kfree (elf_phdata);
794 	goto out;
795 }
796 
797 /* This is really simpleminded and specialized - we are loading an
798  * a.out library that is given an ELF header.
799  */
load_irix_library(struct file * file)800 static int load_irix_library(struct file *file)
801 {
802 	struct elfhdr elf_ex;
803 	struct elf_phdr *elf_phdata  =  NULL;
804 	unsigned int len = 0;
805 	int elf_bss = 0;
806 	int retval;
807 	unsigned int bss;
808 	int error;
809 	int i,j, k;
810 
811 	error = kernel_read(file, 0, (char *) &elf_ex, sizeof(elf_ex));
812 	if (error != sizeof(elf_ex))
813 		return -ENOEXEC;
814 
815 	if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
816 		return -ENOEXEC;
817 
818 	/* First of all, some simple consistency checks. */
819 	if(elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
820 	   !irix_elf_check_arch(&elf_ex) || !file->f_op->mmap)
821 		return -ENOEXEC;
822 
823 	/* Now read in all of the header information. */
824 	if(sizeof(struct elf_phdr) * elf_ex.e_phnum > PAGE_SIZE)
825 		return -ENOEXEC;
826 
827 	elf_phdata =  (struct elf_phdr *)
828 		kmalloc(sizeof(struct elf_phdr) * elf_ex.e_phnum, GFP_KERNEL);
829 	if (elf_phdata == NULL)
830 		return -ENOMEM;
831 
832 	retval = kernel_read(file, elf_ex.e_phoff, (char *) elf_phdata,
833 			   sizeof(struct elf_phdr) * elf_ex.e_phnum);
834 
835 	j = 0;
836 	for(i=0; i<elf_ex.e_phnum; i++)
837 		if((elf_phdata + i)->p_type == PT_LOAD) j++;
838 
839 	if(j != 1)  {
840 		kfree(elf_phdata);
841 		return -ENOEXEC;
842 	}
843 
844 	while(elf_phdata->p_type != PT_LOAD) elf_phdata++;
845 
846 	/* Now use mmap to map the library into memory. */
847 	down_write(&current->mm->mmap_sem);
848 	error = do_mmap(file,
849 			elf_phdata->p_vaddr & 0xfffff000,
850 			elf_phdata->p_filesz + (elf_phdata->p_vaddr & 0xfff),
851 			PROT_READ | PROT_WRITE | PROT_EXEC,
852 			MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
853 			elf_phdata->p_offset & 0xfffff000);
854 	up_write(&current->mm->mmap_sem);
855 
856 	k = elf_phdata->p_vaddr + elf_phdata->p_filesz;
857 	if (k > elf_bss) elf_bss = k;
858 
859 	if (error != (elf_phdata->p_vaddr & 0xfffff000)) {
860 		kfree(elf_phdata);
861 		return error;
862 	}
863 
864 	padzero(elf_bss);
865 
866 	len = (elf_phdata->p_filesz + elf_phdata->p_vaddr+ 0xfff) & 0xfffff000;
867 	bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
868 	if (bss > len) {
869 		down_write(&current->mm->mmap_sem);
870 		do_brk(len, bss-len);
871 		up_write(&current->mm->mmap_sem);
872 	}
873 	kfree(elf_phdata);
874 	return 0;
875 }
876 
877 /* Called through irix_syssgi() to map an elf image given an FD,
878  * a phdr ptr USER_PHDRP in userspace, and a count CNT telling how many
879  * phdrs there are in the USER_PHDRP array.  We return the vaddr the
880  * first phdr was successfully mapped to.
881  */
irix_mapelf(int fd,struct elf_phdr * user_phdrp,int cnt)882 unsigned long irix_mapelf(int fd, struct elf_phdr *user_phdrp, int cnt)
883 {
884 	struct elf_phdr *hp;
885 	struct file *filp;
886 	int i, retval;
887 
888 #ifdef DEBUG_ELF
889 	printk("irix_mapelf: fd[%d] user_phdrp[%p] cnt[%d]\n",
890 	       fd, user_phdrp, cnt);
891 #endif
892 
893 	/* First get the verification out of the way. */
894 	hp = user_phdrp;
895 	retval = verify_area(VERIFY_READ, hp, (sizeof(struct elf_phdr) * cnt));
896 	if(retval) {
897 #ifdef DEBUG_ELF
898 		printk("irix_mapelf: verify_area fails!\n");
899 #endif
900 		return retval;
901 	}
902 
903 #ifdef DEBUG_ELF
904 	dump_phdrs(user_phdrp, cnt);
905 #endif
906 
907 	for(i = 0; i < cnt; i++, hp++)
908 		if(hp->p_type != PT_LOAD) {
909 			printk("irix_mapelf: One section is not PT_LOAD!\n");
910 			return -ENOEXEC;
911 		}
912 
913 	filp = fget(fd);
914 	if (!filp)
915 		return -EACCES;
916 	if(!filp->f_op) {
917 		printk("irix_mapelf: Bogon filp!\n");
918 		fput(filp);
919 		return -EACCES;
920 	}
921 
922 	hp = user_phdrp;
923 	for(i = 0; i < cnt; i++, hp++) {
924 		int prot;
925 
926 		prot  = (hp->p_flags & PF_R) ? PROT_READ : 0;
927 		prot |= (hp->p_flags & PF_W) ? PROT_WRITE : 0;
928 		prot |= (hp->p_flags & PF_X) ? PROT_EXEC : 0;
929 		down_write(&current->mm->mmap_sem);
930 		retval = do_mmap(filp, (hp->p_vaddr & 0xfffff000),
931 				 (hp->p_filesz + (hp->p_vaddr & 0xfff)),
932 				 prot, (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
933 				 (hp->p_offset & 0xfffff000));
934 		up_write(&current->mm->mmap_sem);
935 
936 		if(retval != (hp->p_vaddr & 0xfffff000)) {
937 			printk("irix_mapelf: do_mmap fails with %d!\n", retval);
938 			fput(filp);
939 			return retval;
940 		}
941 	}
942 
943 #ifdef DEBUG_ELF
944 	printk("irix_mapelf: Success, returning %08lx\n", user_phdrp->p_vaddr);
945 #endif
946 	fput(filp);
947 	return user_phdrp->p_vaddr;
948 }
949 
950 /*
951  * ELF core dumper
952  *
953  * Modelled on fs/exec.c:aout_core_dump()
954  * Jeremy Fitzhardinge <jeremy@sw.oz.au>
955  */
956 
957 /* These are the only things you should do on a core-file: use only these
958  * functions to write out all the necessary info.
959  */
dump_write(struct file * file,const void * addr,int nr)960 static int dump_write(struct file *file, const void *addr, int nr)
961 {
962 	return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
963 }
964 
dump_seek(struct file * file,off_t off)965 static int dump_seek(struct file *file, off_t off)
966 {
967 	if (file->f_op->llseek) {
968 		if (file->f_op->llseek(file, off, 0) != off)
969 			return 0;
970 	} else
971 		file->f_pos = off;
972 	return 1;
973 }
974 
975 /* Decide whether a segment is worth dumping; default is yes to be
976  * sure (missing info is worse than too much; etc).
977  * Personally I'd include everything, and use the coredump limit...
978  *
979  * I think we should skip something. But I am not sure how. H.J.
980  */
maydump(struct vm_area_struct * vma)981 static inline int maydump(struct vm_area_struct *vma)
982 {
983 	if (!(vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC)))
984 		return 0;
985 #if 1
986 	if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
987 		return 1;
988 	if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
989 		return 0;
990 #endif
991 	return 1;
992 }
993 
994 #define roundup(x, y)  ((((x)+((y)-1))/(y))*(y))
995 
996 /* An ELF note in memory. */
997 struct memelfnote
998 {
999 	const char *name;
1000 	int type;
1001 	unsigned int datasz;
1002 	void *data;
1003 };
1004 
notesize(struct memelfnote * en)1005 static int notesize(struct memelfnote *en)
1006 {
1007 	int sz;
1008 
1009 	sz = sizeof(struct elf_note);
1010 	sz += roundup(strlen(en->name), 4);
1011 	sz += roundup(en->datasz, 4);
1012 
1013 	return sz;
1014 }
1015 
1016 /* #define DEBUG */
1017 
1018 #define DUMP_WRITE(addr, nr)	\
1019 	if (!dump_write(file, (addr), (nr))) \
1020 		goto end_coredump;
1021 #define DUMP_SEEK(off)	\
1022 	if (!dump_seek(file, (off))) \
1023 		goto end_coredump;
1024 
writenote(struct memelfnote * men,struct file * file)1025 static int writenote(struct memelfnote *men, struct file *file)
1026 {
1027 	struct elf_note en;
1028 
1029 	en.n_namesz = strlen(men->name);
1030 	en.n_descsz = men->datasz;
1031 	en.n_type = men->type;
1032 
1033 	DUMP_WRITE(&en, sizeof(en));
1034 	DUMP_WRITE(men->name, en.n_namesz);
1035 	/* XXX - cast from long long to long to avoid need for libgcc.a */
1036 	DUMP_SEEK(roundup((unsigned long)file->f_pos, 4));	/* XXX */
1037 	DUMP_WRITE(men->data, men->datasz);
1038 	DUMP_SEEK(roundup((unsigned long)file->f_pos, 4));	/* XXX */
1039 
1040 	return 1;
1041 
1042 end_coredump:
1043 	return 0;
1044 }
1045 #undef DUMP_WRITE
1046 #undef DUMP_SEEK
1047 
1048 #define DUMP_WRITE(addr, nr)	\
1049 	if (!dump_write(file, (addr), (nr))) \
1050 		goto end_coredump;
1051 #define DUMP_SEEK(off)	\
1052 	if (!dump_seek(file, (off))) \
1053 		goto end_coredump;
1054 
1055 /* Actual dumper.
1056  *
1057  * This is a two-pass process; first we find the offsets of the bits,
1058  * and then they are actually written out.  If we run out of core limit
1059  * we just truncate.
1060  */
irix_core_dump(long signr,struct pt_regs * regs,struct file * file)1061 static int irix_core_dump(long signr, struct pt_regs * regs, struct file *file)
1062 {
1063 	int has_dumped = 0;
1064 	mm_segment_t fs;
1065 	int segs;
1066 	int i;
1067 	size_t size;
1068 	struct vm_area_struct *vma;
1069 	struct elfhdr elf;
1070 	off_t offset = 0, dataoff;
1071 	int limit = current->rlim[RLIMIT_CORE].rlim_cur;
1072 	int numnote = 4;
1073 	struct memelfnote notes[4];
1074 	struct elf_prstatus prstatus;	/* NT_PRSTATUS */
1075 	elf_fpregset_t fpu;		/* NT_PRFPREG */
1076 	struct elf_prpsinfo psinfo;	/* NT_PRPSINFO */
1077 
1078 	/* Count what's needed to dump, up to the limit of coredump size. */
1079 	segs = 0;
1080 	size = 0;
1081 	for(vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
1082 		if (maydump(vma))
1083 		{
1084 			int sz = vma->vm_end-vma->vm_start;
1085 
1086 			if (size+sz >= limit)
1087 				break;
1088 			else
1089 				size += sz;
1090 		}
1091 
1092 		segs++;
1093 	}
1094 #ifdef DEBUG
1095 	printk("irix_core_dump: %d segs taking %d bytes\n", segs, size);
1096 #endif
1097 
1098 	/* Set up header. */
1099 	memcpy(elf.e_ident, ELFMAG, SELFMAG);
1100 	elf.e_ident[EI_CLASS] = ELFCLASS32;
1101 	elf.e_ident[EI_DATA] = ELFDATA2LSB;
1102 	elf.e_ident[EI_VERSION] = EV_CURRENT;
1103 	memset(elf.e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1104 
1105 	elf.e_type = ET_CORE;
1106 	elf.e_machine = ELF_ARCH;
1107 	elf.e_version = EV_CURRENT;
1108 	elf.e_entry = 0;
1109 	elf.e_phoff = sizeof(elf);
1110 	elf.e_shoff = 0;
1111 	elf.e_flags = 0;
1112 	elf.e_ehsize = sizeof(elf);
1113 	elf.e_phentsize = sizeof(struct elf_phdr);
1114 	elf.e_phnum = segs+1;		/* Include notes. */
1115 	elf.e_shentsize = 0;
1116 	elf.e_shnum = 0;
1117 	elf.e_shstrndx = 0;
1118 
1119 	fs = get_fs();
1120 	set_fs(KERNEL_DS);
1121 
1122 	has_dumped = 1;
1123 	current->flags |= PF_DUMPCORE;
1124 
1125 	DUMP_WRITE(&elf, sizeof(elf));
1126 	offset += sizeof(elf);				/* Elf header. */
1127 	offset += (segs+1) * sizeof(struct elf_phdr);	/* Program headers. */
1128 
1129 	/* Set up the notes in similar form to SVR4 core dumps made
1130 	 * with info from their /proc.
1131 	 */
1132 	memset(&psinfo, 0, sizeof(psinfo));
1133 	memset(&prstatus, 0, sizeof(prstatus));
1134 
1135 	notes[0].name = "CORE";
1136 	notes[0].type = NT_PRSTATUS;
1137 	notes[0].datasz = sizeof(prstatus);
1138 	notes[0].data = &prstatus;
1139 	prstatus.pr_info.si_signo = prstatus.pr_cursig = signr;
1140 	prstatus.pr_sigpend = current->pending.signal.sig[0];
1141 	prstatus.pr_sighold = current->blocked.sig[0];
1142 	psinfo.pr_pid = prstatus.pr_pid = current->pid;
1143 	psinfo.pr_ppid = prstatus.pr_ppid = current->p_pptr->pid;
1144 	psinfo.pr_pgrp = prstatus.pr_pgrp = current->pgrp;
1145 	psinfo.pr_sid = prstatus.pr_sid = current->session;
1146 	prstatus.pr_utime.tv_sec = CT_TO_SECS(current->times.tms_utime);
1147 	prstatus.pr_utime.tv_usec = CT_TO_USECS(current->times.tms_utime);
1148 	prstatus.pr_stime.tv_sec = CT_TO_SECS(current->times.tms_stime);
1149 	prstatus.pr_stime.tv_usec = CT_TO_USECS(current->times.tms_stime);
1150 	prstatus.pr_cutime.tv_sec = CT_TO_SECS(current->times.tms_cutime);
1151 	prstatus.pr_cutime.tv_usec = CT_TO_USECS(current->times.tms_cutime);
1152 	prstatus.pr_cstime.tv_sec = CT_TO_SECS(current->times.tms_cstime);
1153 	prstatus.pr_cstime.tv_usec = CT_TO_USECS(current->times.tms_cstime);
1154 	if (sizeof(elf_gregset_t) != sizeof(struct pt_regs)) {
1155 		printk("sizeof(elf_gregset_t) (%d) != sizeof(struct pt_regs) "
1156 		       "(%d)\n", sizeof(elf_gregset_t), sizeof(struct pt_regs));
1157 	} else {
1158 		*(struct pt_regs *)&prstatus.pr_reg = *regs;
1159 	}
1160 
1161 	notes[1].name = "CORE";
1162 	notes[1].type = NT_PRPSINFO;
1163 	notes[1].datasz = sizeof(psinfo);
1164 	notes[1].data = &psinfo;
1165 	i = current->state ? ffz(~current->state) + 1 : 0;
1166 	psinfo.pr_state = i;
1167 	psinfo.pr_sname = (i < 0 || i > 5) ? '.' : "RSDZTD"[i];
1168 	psinfo.pr_zomb = psinfo.pr_sname == 'Z';
1169 	psinfo.pr_nice = current->nice;
1170 	psinfo.pr_flag = current->flags;
1171 	psinfo.pr_uid = current->uid;
1172 	psinfo.pr_gid = current->gid;
1173 	{
1174 		int i, len;
1175 
1176 		set_fs(fs);
1177 
1178 		len = current->mm->arg_end - current->mm->arg_start;
1179 		len = len >= ELF_PRARGSZ ? ELF_PRARGSZ : len;
1180 		copy_from_user(&psinfo.pr_psargs,
1181 			       (const char *)current->mm->arg_start, len);
1182 		for(i = 0; i < len; i++)
1183 			if (psinfo.pr_psargs[i] == 0)
1184 				psinfo.pr_psargs[i] = ' ';
1185 		psinfo.pr_psargs[len] = 0;
1186 
1187 		set_fs(KERNEL_DS);
1188 	}
1189 	strncpy(psinfo.pr_fname, current->comm, sizeof(psinfo.pr_fname));
1190 
1191 	notes[2].name = "CORE";
1192 	notes[2].type = NT_TASKSTRUCT;
1193 	notes[2].datasz = sizeof(*current);
1194 	notes[2].data = current;
1195 
1196 	/* Try to dump the FPU. */
1197 	prstatus.pr_fpvalid = dump_fpu (&fpu);
1198 	if (!prstatus.pr_fpvalid) {
1199 		numnote--;
1200 	} else {
1201 		notes[3].name = "CORE";
1202 		notes[3].type = NT_PRFPREG;
1203 		notes[3].datasz = sizeof(fpu);
1204 		notes[3].data = &fpu;
1205 	}
1206 
1207 	/* Write notes phdr entry. */
1208 	{
1209 		struct elf_phdr phdr;
1210 		int sz = 0;
1211 
1212 		for(i = 0; i < numnote; i++)
1213 			sz += notesize(&notes[i]);
1214 
1215 		phdr.p_type = PT_NOTE;
1216 		phdr.p_offset = offset;
1217 		phdr.p_vaddr = 0;
1218 		phdr.p_paddr = 0;
1219 		phdr.p_filesz = sz;
1220 		phdr.p_memsz = 0;
1221 		phdr.p_flags = 0;
1222 		phdr.p_align = 0;
1223 
1224 		offset += phdr.p_filesz;
1225 		DUMP_WRITE(&phdr, sizeof(phdr));
1226 	}
1227 
1228 	/* Page-align dumped data. */
1229 	dataoff = offset = roundup(offset, PAGE_SIZE);
1230 
1231 	/* Write program headers for segments dump. */
1232 	for(vma = current->mm->mmap, i = 0;
1233 		i < segs && vma != NULL; vma = vma->vm_next) {
1234 		struct elf_phdr phdr;
1235 		size_t sz;
1236 
1237 		i++;
1238 
1239 		sz = vma->vm_end - vma->vm_start;
1240 
1241 		phdr.p_type = PT_LOAD;
1242 		phdr.p_offset = offset;
1243 		phdr.p_vaddr = vma->vm_start;
1244 		phdr.p_paddr = 0;
1245 		phdr.p_filesz = maydump(vma) ? sz : 0;
1246 		phdr.p_memsz = sz;
1247 		offset += phdr.p_filesz;
1248 		phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1249 		if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
1250 		if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
1251 		phdr.p_align = PAGE_SIZE;
1252 
1253 		DUMP_WRITE(&phdr, sizeof(phdr));
1254 	}
1255 
1256 	for(i = 0; i < numnote; i++)
1257 		if (!writenote(&notes[i], file))
1258 			goto end_coredump;
1259 
1260 	set_fs(fs);
1261 
1262 	DUMP_SEEK(dataoff);
1263 
1264 	for(i = 0, vma = current->mm->mmap;
1265 	    i < segs && vma != NULL;
1266 	    vma = vma->vm_next) {
1267 		unsigned long addr = vma->vm_start;
1268 		unsigned long len = vma->vm_end - vma->vm_start;
1269 
1270 		if (!maydump(vma))
1271 			continue;
1272 		i++;
1273 #ifdef DEBUG
1274 		printk("elf_core_dump: writing %08lx %lx\n", addr, len);
1275 #endif
1276 		DUMP_WRITE((void *)addr, len);
1277 	}
1278 
1279 	if ((off_t) file->f_pos != offset) {
1280 		/* Sanity check. */
1281 		printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
1282 		       (off_t) file->f_pos, offset);
1283 	}
1284 
1285 end_coredump:
1286 	set_fs(fs);
1287 	return has_dumped;
1288 }
1289 
init_irix_binfmt(void)1290 static int __init init_irix_binfmt(void)
1291 {
1292 	return register_binfmt(&irix_format);
1293 }
1294 
exit_irix_binfmt(void)1295 static void __exit exit_irix_binfmt(void)
1296 {
1297 	/* Remove the IRIX ELF loaders. */
1298 	unregister_binfmt(&irix_format);
1299 }
1300 
1301 module_init(init_irix_binfmt)
1302 module_exit(exit_irix_binfmt)
1303