Use list_head in binfmt handling
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / sparc64 / kernel / binfmt_aout32.c
blobc8acbeab49b482c0bd96d554edd12aad769214d6
1 /*
2 * linux/fs/binfmt_aout.c
4 * Copyright (C) 1991, 1992, 1996 Linus Torvalds
6 * Hacked a bit by DaveM to make it work with 32-bit SunOS
7 * binaries on the sparc64 port.
8 */
10 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/mm.h>
15 #include <linux/mman.h>
16 #include <linux/a.out.h>
17 #include <linux/errno.h>
18 #include <linux/signal.h>
19 #include <linux/string.h>
20 #include <linux/fs.h>
21 #include <linux/file.h>
22 #include <linux/stat.h>
23 #include <linux/fcntl.h>
24 #include <linux/ptrace.h>
25 #include <linux/user.h>
26 #include <linux/slab.h>
27 #include <linux/binfmts.h>
28 #include <linux/personality.h>
29 #include <linux/init.h>
31 #include <asm/system.h>
32 #include <asm/uaccess.h>
33 #include <asm/pgalloc.h>
34 #include <asm/mmu_context.h>
36 static int load_aout32_binary(struct linux_binprm *, struct pt_regs * regs);
37 static int load_aout32_library(struct file*);
38 static int aout32_core_dump(long signr, struct pt_regs * regs, struct file *file);
40 static struct linux_binfmt aout32_format = {
41 .module = THIS_MODULE,
42 .load_binary = load_aout32_binary,
43 .load_shlib = load_aout32_library,
44 .core_dump = aout32_core_dump,
45 .min_coredump = PAGE_SIZE,
48 static void set_brk(unsigned long start, unsigned long end)
50 start = PAGE_ALIGN(start);
51 end = PAGE_ALIGN(end);
52 if (end <= start)
53 return;
54 down_write(&current->mm->mmap_sem);
55 do_brk(start, end - start);
56 up_write(&current->mm->mmap_sem);
60 * These are the only things you should do on a core-file: use only these
61 * macros to write out all the necessary info.
64 static int dump_write(struct file *file, const void *addr, int nr)
66 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
69 #define DUMP_WRITE(addr, nr) \
70 if (!dump_write(file, (void *)(addr), (nr))) \
71 goto end_coredump;
73 #define DUMP_SEEK(offset) \
74 if (file->f_op->llseek) { \
75 if (file->f_op->llseek(file,(offset),0) != (offset)) \
76 goto end_coredump; \
77 } else file->f_pos = (offset)
80 * Routine writes a core dump image in the current directory.
81 * Currently only a stub-function.
83 * Note that setuid/setgid files won't make a core-dump if the uid/gid
84 * changed due to the set[u|g]id. It's enforced by the "current->mm->dumpable"
85 * field, which also makes sure the core-dumps won't be recursive if the
86 * dumping of the process results in another error..
89 static int aout32_core_dump(long signr, struct pt_regs *regs, struct file *file)
91 mm_segment_t fs;
92 int has_dumped = 0;
93 unsigned long dump_start, dump_size;
94 struct user dump;
95 # define START_DATA(u) (u.u_tsize)
96 # define START_STACK(u) ((regs->u_regs[UREG_FP]) & ~(PAGE_SIZE - 1))
98 fs = get_fs();
99 set_fs(KERNEL_DS);
100 has_dumped = 1;
101 current->flags |= PF_DUMPCORE;
102 strncpy(dump.u_comm, current->comm, sizeof(dump.u_comm));
103 dump.signal = signr;
104 dump_thread(regs, &dump);
106 /* If the size of the dump file exceeds the rlimit, then see what would happen
107 if we wrote the stack, but not the data area. */
108 if ((dump.u_dsize+dump.u_ssize) >
109 current->signal->rlim[RLIMIT_CORE].rlim_cur)
110 dump.u_dsize = 0;
112 /* Make sure we have enough room to write the stack and data areas. */
113 if ((dump.u_ssize) >
114 current->signal->rlim[RLIMIT_CORE].rlim_cur)
115 dump.u_ssize = 0;
117 /* make sure we actually have a data and stack area to dump */
118 set_fs(USER_DS);
119 if (!access_ok(VERIFY_READ, (void __user *) START_DATA(dump), dump.u_dsize))
120 dump.u_dsize = 0;
121 if (!access_ok(VERIFY_READ, (void __user *) START_STACK(dump), dump.u_ssize))
122 dump.u_ssize = 0;
124 set_fs(KERNEL_DS);
125 /* struct user */
126 DUMP_WRITE(&dump,sizeof(dump));
127 /* now we start writing out the user space info */
128 set_fs(USER_DS);
129 /* Dump the data area */
130 if (dump.u_dsize != 0) {
131 dump_start = START_DATA(dump);
132 dump_size = dump.u_dsize;
133 DUMP_WRITE(dump_start,dump_size);
135 /* Now prepare to dump the stack area */
136 if (dump.u_ssize != 0) {
137 dump_start = START_STACK(dump);
138 dump_size = dump.u_ssize;
139 DUMP_WRITE(dump_start,dump_size);
141 /* Finally dump the task struct. Not be used by gdb, but could be useful */
142 set_fs(KERNEL_DS);
143 DUMP_WRITE(current,sizeof(*current));
144 end_coredump:
145 set_fs(fs);
146 return has_dumped;
150 * create_aout32_tables() parses the env- and arg-strings in new user
151 * memory and creates the pointer tables from them, and puts their
152 * addresses on the "stack", returning the new stack pointer value.
155 static u32 __user *create_aout32_tables(char __user *p, struct linux_binprm *bprm)
157 u32 __user *argv;
158 u32 __user *envp;
159 u32 __user *sp;
160 int argc = bprm->argc;
161 int envc = bprm->envc;
163 sp = (u32 __user *)((-(unsigned long)sizeof(char *))&(unsigned long)p);
165 /* This imposes the proper stack alignment for a new process. */
166 sp = (u32 __user *) (((unsigned long) sp) & ~7);
167 if ((envc+argc+3)&1)
168 --sp;
170 sp -= envc+1;
171 envp = sp;
172 sp -= argc+1;
173 argv = sp;
174 put_user(argc,--sp);
175 current->mm->arg_start = (unsigned long) p;
176 while (argc-->0) {
177 char c;
178 put_user(((u32)(unsigned long)(p)),argv++);
179 do {
180 get_user(c,p++);
181 } while (c);
183 put_user(0,argv);
184 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
185 while (envc-->0) {
186 char c;
187 put_user(((u32)(unsigned long)(p)),envp++);
188 do {
189 get_user(c,p++);
190 } while (c);
192 put_user(0,envp);
193 current->mm->env_end = (unsigned long) p;
194 return sp;
198 * These are the functions used to load a.out style executables and shared
199 * libraries. There is no binary dependent code anywhere else.
202 static int load_aout32_binary(struct linux_binprm * bprm, struct pt_regs * regs)
204 struct exec ex;
205 unsigned long error;
206 unsigned long fd_offset;
207 unsigned long rlim;
208 unsigned long orig_thr_flags;
209 int retval;
211 ex = *((struct exec *) bprm->buf); /* exec-header */
212 if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
213 N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
214 N_TRSIZE(ex) || N_DRSIZE(ex) ||
215 bprm->file->f_path.dentry->d_inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
216 return -ENOEXEC;
219 fd_offset = N_TXTOFF(ex);
221 /* Check initial limits. This avoids letting people circumvent
222 * size limits imposed on them by creating programs with large
223 * arrays in the data or bss.
225 rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
226 if (rlim >= RLIM_INFINITY)
227 rlim = ~0;
228 if (ex.a_data + ex.a_bss > rlim)
229 return -ENOMEM;
231 /* Flush all traces of the currently running executable */
232 retval = flush_old_exec(bprm);
233 if (retval)
234 return retval;
236 /* OK, This is the point of no return */
237 set_personality(PER_SUNOS);
239 current->mm->end_code = ex.a_text +
240 (current->mm->start_code = N_TXTADDR(ex));
241 current->mm->end_data = ex.a_data +
242 (current->mm->start_data = N_DATADDR(ex));
243 current->mm->brk = ex.a_bss +
244 (current->mm->start_brk = N_BSSADDR(ex));
245 current->mm->free_area_cache = current->mm->mmap_base;
246 current->mm->cached_hole_size = 0;
248 current->mm->mmap = NULL;
249 compute_creds(bprm);
250 current->flags &= ~PF_FORKNOEXEC;
251 if (N_MAGIC(ex) == NMAGIC) {
252 loff_t pos = fd_offset;
253 /* Fuck me plenty... */
254 down_write(&current->mm->mmap_sem);
255 error = do_brk(N_TXTADDR(ex), ex.a_text);
256 up_write(&current->mm->mmap_sem);
257 bprm->file->f_op->read(bprm->file, (char __user *)N_TXTADDR(ex),
258 ex.a_text, &pos);
259 down_write(&current->mm->mmap_sem);
260 error = do_brk(N_DATADDR(ex), ex.a_data);
261 up_write(&current->mm->mmap_sem);
262 bprm->file->f_op->read(bprm->file, (char __user *)N_DATADDR(ex),
263 ex.a_data, &pos);
264 goto beyond_if;
267 if (N_MAGIC(ex) == OMAGIC) {
268 loff_t pos = fd_offset;
269 down_write(&current->mm->mmap_sem);
270 do_brk(N_TXTADDR(ex) & PAGE_MASK,
271 ex.a_text+ex.a_data + PAGE_SIZE - 1);
272 up_write(&current->mm->mmap_sem);
273 bprm->file->f_op->read(bprm->file, (char __user *)N_TXTADDR(ex),
274 ex.a_text+ex.a_data, &pos);
275 } else {
276 static unsigned long error_time;
277 if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
278 (N_MAGIC(ex) != NMAGIC) && (jiffies-error_time) > 5*HZ)
280 printk(KERN_NOTICE "executable not page aligned\n");
281 error_time = jiffies;
284 if (!bprm->file->f_op->mmap) {
285 loff_t pos = fd_offset;
286 down_write(&current->mm->mmap_sem);
287 do_brk(0, ex.a_text+ex.a_data);
288 up_write(&current->mm->mmap_sem);
289 bprm->file->f_op->read(bprm->file,
290 (char __user *)N_TXTADDR(ex),
291 ex.a_text+ex.a_data, &pos);
292 goto beyond_if;
295 down_write(&current->mm->mmap_sem);
296 error = do_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
297 PROT_READ | PROT_EXEC,
298 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
299 fd_offset);
300 up_write(&current->mm->mmap_sem);
302 if (error != N_TXTADDR(ex)) {
303 send_sig(SIGKILL, current, 0);
304 return error;
307 down_write(&current->mm->mmap_sem);
308 error = do_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
309 PROT_READ | PROT_WRITE | PROT_EXEC,
310 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
311 fd_offset + ex.a_text);
312 up_write(&current->mm->mmap_sem);
313 if (error != N_DATADDR(ex)) {
314 send_sig(SIGKILL, current, 0);
315 return error;
318 beyond_if:
319 set_binfmt(&aout32_format);
321 set_brk(current->mm->start_brk, current->mm->brk);
323 /* Make sure STACK_TOP returns the right thing. */
324 orig_thr_flags = current_thread_info()->flags;
325 current_thread_info()->flags |= _TIF_32BIT;
327 retval = setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT);
328 if (retval < 0) {
329 current_thread_info()->flags = orig_thr_flags;
331 /* Someone check-me: is this error path enough? */
332 send_sig(SIGKILL, current, 0);
333 return retval;
336 current->mm->start_stack =
337 (unsigned long) create_aout32_tables((char __user *)bprm->p, bprm);
338 tsb_context_switch(current->mm);
340 start_thread32(regs, ex.a_entry, current->mm->start_stack);
341 if (current->ptrace & PT_PTRACED)
342 send_sig(SIGTRAP, current, 0);
343 return 0;
346 /* N.B. Move to .h file and use code in fs/binfmt_aout.c? */
347 static int load_aout32_library(struct file *file)
349 struct inode * inode;
350 unsigned long bss, start_addr, len;
351 unsigned long error;
352 int retval;
353 struct exec ex;
355 inode = file->f_path.dentry->d_inode;
357 retval = -ENOEXEC;
358 error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
359 if (error != sizeof(ex))
360 goto out;
362 /* We come in here for the regular a.out style of shared libraries */
363 if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
364 N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
365 inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
366 goto out;
369 if (N_MAGIC(ex) == ZMAGIC && N_TXTOFF(ex) &&
370 (N_TXTOFF(ex) < inode->i_sb->s_blocksize)) {
371 printk("N_TXTOFF < BLOCK_SIZE. Please convert library\n");
372 goto out;
375 if (N_FLAGS(ex))
376 goto out;
378 /* For QMAGIC, the starting address is 0x20 into the page. We mask
379 this off to get the starting address for the page */
381 start_addr = ex.a_entry & 0xfffff000;
383 /* Now use mmap to map the library into memory. */
384 down_write(&current->mm->mmap_sem);
385 error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
386 PROT_READ | PROT_WRITE | PROT_EXEC,
387 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
388 N_TXTOFF(ex));
389 up_write(&current->mm->mmap_sem);
390 retval = error;
391 if (error != start_addr)
392 goto out;
394 len = PAGE_ALIGN(ex.a_text + ex.a_data);
395 bss = ex.a_text + ex.a_data + ex.a_bss;
396 if (bss > len) {
397 down_write(&current->mm->mmap_sem);
398 error = do_brk(start_addr + len, bss - len);
399 up_write(&current->mm->mmap_sem);
400 retval = error;
401 if (error != start_addr + len)
402 goto out;
404 retval = 0;
405 out:
406 return retval;
409 static int __init init_aout32_binfmt(void)
411 return register_binfmt(&aout32_format);
414 static void __exit exit_aout32_binfmt(void)
416 unregister_binfmt(&aout32_format);
419 module_init(init_aout32_binfmt);
420 module_exit(exit_aout32_binfmt);