fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / fs / binfmt_som.c
1 /*
2  * linux/fs/binfmt_som.c
3  *
4  * These are the functions used to load SOM format executables as used
5  * by HP-UX.  
6  *
7  * Copyright 1999 Matthew Wilcox <willy@bofh.ai>
8  * based on binfmt_elf which is
9  * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
10  */
11
12 #include <linux/module.h>
13
14 #include <linux/fs.h>
15 #include <linux/stat.h>
16 #include <linux/sched.h>
17 #include <linux/mm.h>
18 #include <linux/mman.h>
19 #include <linux/errno.h>
20 #include <linux/signal.h>
21 #include <linux/binfmts.h>
22 #include <linux/som.h>
23 #include <linux/string.h>
24 #include <linux/file.h>
25 #include <linux/fcntl.h>
26 #include <linux/ptrace.h>
27 #include <linux/slab.h>
28 #include <linux/shm.h>
29 #include <linux/personality.h>
30 #include <linux/init.h>
31 #include <linux/vs_memory.h>
32
33 #include <asm/a.out.h>
34 #include <asm/uaccess.h>
35 #include <asm/pgtable.h>
36
37
38 #include <linux/elf.h>
39
40 static int load_som_binary(struct linux_binprm * bprm, struct pt_regs * regs);
41 static int load_som_library(struct file *);
42
43 /*
44  * If we don't support core dumping, then supply a NULL so we
45  * don't even try.
46  */
47 #if 0
48 static int som_core_dump(long signr, struct pt_regs * regs);
49 #else
50 #define som_core_dump   NULL
51 #endif
52
53 #define SOM_PAGESTART(_v) ((_v) & ~(unsigned long)(SOM_PAGESIZE-1))
54 #define SOM_PAGEOFFSET(_v) ((_v) & (SOM_PAGESIZE-1))
55 #define SOM_PAGEALIGN(_v) (((_v) + SOM_PAGESIZE - 1) & ~(SOM_PAGESIZE - 1))
56
57 static struct linux_binfmt som_format = {
58         .module         = THIS_MODULE,
59         .load_binary    = load_som_binary,
60         .load_shlib     = load_som_library,
61         .core_dump      = som_core_dump,
62         .min_coredump   = SOM_PAGESIZE
63 };
64
65 /*
66  * create_som_tables() parses the env- and arg-strings in new user
67  * memory and creates the pointer tables from them, and puts their
68  * addresses on the "stack", returning the new stack pointer value.
69  */
70 static void create_som_tables(struct linux_binprm *bprm)
71 {
72         char **argv, **envp;
73         int argc = bprm->argc;
74         int envc = bprm->envc;
75         unsigned long p;
76         unsigned long *sp;
77
78         /* Word-align the stack pointer */
79         sp = (unsigned long *)((bprm->p + 3) & ~3);
80
81         envp = (char **) sp;
82         sp += envc + 1;
83         argv = (char **) sp;
84         sp += argc + 1;
85
86         __put_user((unsigned long) envp,++sp);
87         __put_user((unsigned long) argv,++sp);
88
89         __put_user(argc, ++sp);
90
91         bprm->p = (unsigned long) sp;
92
93         p = current->mm->arg_start;
94         while (argc-- > 0) {
95                 __put_user((char *)p,argv++);
96                 p += strlen_user((char *)p);
97         }
98         __put_user(NULL, argv);
99         current->mm->arg_end = current->mm->env_start = p;
100         while (envc-- > 0) {
101                 __put_user((char *)p,envp++);
102                 p += strlen_user((char *)p);
103         }
104         __put_user(NULL, envp);
105         current->mm->env_end = p;
106 }
107
108 static int check_som_header(struct som_hdr *som_ex)
109 {
110         int *buf = (int *)som_ex;
111         int i, ck;
112
113         if (som_ex->system_id != SOM_SID_PARISC_1_0 &&
114             som_ex->system_id != SOM_SID_PARISC_1_1 &&
115             som_ex->system_id != SOM_SID_PARISC_2_0)
116                 return -ENOEXEC;
117
118         if (som_ex->a_magic != SOM_EXEC_NONSHARE &&
119             som_ex->a_magic != SOM_EXEC_SHARE &&
120             som_ex->a_magic != SOM_EXEC_DEMAND)
121                 return -ENOEXEC;
122
123         if (som_ex->version_id != SOM_ID_OLD &&
124             som_ex->version_id != SOM_ID_NEW)
125                 return -ENOEXEC;
126
127         ck = 0;
128         for (i=0; i<32; i++)
129                 ck ^= buf[i];
130         if (ck != 0)
131                 return -ENOEXEC;
132
133         return 0;
134 }
135
136 static int map_som_binary(struct file *file,
137                 const struct som_exec_auxhdr *hpuxhdr)
138 {
139         unsigned long code_start, code_size, data_start, data_size;
140         unsigned long bss_start, som_brk;
141         int retval;
142         int prot = PROT_READ | PROT_EXEC;
143         int flags = MAP_FIXED|MAP_PRIVATE|MAP_DENYWRITE|MAP_EXECUTABLE;
144
145         mm_segment_t old_fs = get_fs();
146         set_fs(get_ds());
147
148         code_start = SOM_PAGESTART(hpuxhdr->exec_tmem);
149         code_size = SOM_PAGEALIGN(hpuxhdr->exec_tsize);
150         current->mm->start_code = code_start;
151         current->mm->end_code = code_start + code_size;
152         down_write(&current->mm->mmap_sem);
153         retval = do_mmap(file, code_start, code_size, prot,
154                         flags, SOM_PAGESTART(hpuxhdr->exec_tfile));
155         up_write(&current->mm->mmap_sem);
156         if (retval < 0 && retval > -1024)
157                 goto out;
158
159         data_start = SOM_PAGESTART(hpuxhdr->exec_dmem);
160         data_size = SOM_PAGEALIGN(hpuxhdr->exec_dsize);
161         current->mm->start_data = data_start;
162         current->mm->end_data = bss_start = data_start + data_size;
163         down_write(&current->mm->mmap_sem);
164         retval = do_mmap(file, data_start, data_size,
165                         prot | PROT_WRITE, flags,
166                         SOM_PAGESTART(hpuxhdr->exec_dfile));
167         up_write(&current->mm->mmap_sem);
168         if (retval < 0 && retval > -1024)
169                 goto out;
170
171         som_brk = bss_start + SOM_PAGEALIGN(hpuxhdr->exec_bsize);
172         current->mm->start_brk = current->mm->brk = som_brk;
173         down_write(&current->mm->mmap_sem);
174         retval = do_mmap(NULL, bss_start, som_brk - bss_start,
175                         prot | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, 0);
176         up_write(&current->mm->mmap_sem);
177         if (retval > 0 || retval < -1024)
178                 retval = 0;
179 out:
180         set_fs(old_fs);
181         return retval;
182 }
183
184
185 /*
186  * These are the functions used to load SOM executables and shared
187  * libraries.  There is no binary dependent code anywhere else.
188  */
189
190 static int
191 load_som_binary(struct linux_binprm * bprm, struct pt_regs * regs)
192 {
193         int som_exec_fileno;
194         int retval;
195         unsigned int size;
196         unsigned long som_entry;
197         struct som_hdr *som_ex;
198         struct som_exec_auxhdr *hpuxhdr;
199         struct files_struct *files;
200
201         /* Get the exec-header */
202         som_ex = (struct som_hdr *) bprm->buf;
203
204         retval = check_som_header(som_ex);
205         if (retval != 0)
206                 goto out;
207
208         /* Now read in the auxiliary header information */
209
210         retval = -ENOMEM;
211         size = som_ex->aux_header_size;
212         if (size > SOM_PAGESIZE)
213                 goto out;
214         hpuxhdr = kmalloc(size, GFP_KERNEL);
215         if (!hpuxhdr)
216                 goto out;
217
218         retval = kernel_read(bprm->file, som_ex->aux_header_location,
219                         (char *) hpuxhdr, size);
220         if (retval != size) {
221                 if (retval >= 0)
222                         retval = -EIO;
223                 goto out_free;
224         }
225
226         files = current->files; /* Refcounted so ok */
227         retval = unshare_files();
228         if (retval < 0)
229                 goto out_free;
230         if (files == current->files) {
231                 put_files_struct(files);
232                 files = NULL;
233         }
234
235         retval = get_unused_fd();
236         if (retval < 0)
237                 goto out_free;
238         get_file(bprm->file);
239         fd_install(som_exec_fileno = retval, bprm->file);
240
241         /* Flush all traces of the currently running executable */
242         retval = flush_old_exec(bprm);
243         if (retval)
244                 goto out_free;
245
246         /* OK, This is the point of no return */
247         current->flags &= ~PF_FORKNOEXEC;
248         current->personality = PER_HPUX;
249
250         /* Set the task size for HP-UX processes such that
251          * the gateway page is outside the address space.
252          * This can be fixed later, but for now, this is much
253          * easier.
254          */
255
256         current->thread.task_size = 0xc0000000;
257
258         /* Set map base to allow enough room for hp-ux heap growth */
259
260         current->thread.map_base = 0x80000000;
261
262         retval = map_som_binary(bprm->file, hpuxhdr);
263         if (retval < 0)
264                 goto out_free;
265
266         som_entry = hpuxhdr->exec_entry;
267         kfree(hpuxhdr);
268
269         set_binfmt(&som_format);
270         compute_creds(bprm);
271         setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT);
272
273         create_som_tables(bprm);
274
275         current->mm->start_stack = bprm->p;
276
277 #if 0
278         printk("(start_brk) %08lx\n" , (unsigned long) current->mm->start_brk);
279         printk("(end_code) %08lx\n" , (unsigned long) current->mm->end_code);
280         printk("(start_code) %08lx\n" , (unsigned long) current->mm->start_code);
281         printk("(end_data) %08lx\n" , (unsigned long) current->mm->end_data);
282         printk("(start_stack) %08lx\n" , (unsigned long) current->mm->start_stack);
283         printk("(brk) %08lx\n" , (unsigned long) current->mm->brk);
284 #endif
285
286         map_hpux_gateway_page(current,current->mm);
287
288         start_thread_som(regs, som_entry, bprm->p);
289         return 0;
290
291         /* error cleanup */
292 out_free:
293         kfree(hpuxhdr);
294 out:
295         return retval;
296 }
297
298 static int load_som_library(struct file *f)
299 {
300 /* No lib support in SOM yet.  gizza chance.. */
301         return -ENOEXEC;
302 }
303         /* Install the SOM loader.
304          * N.B. We *rely* on the table being the right size with the
305          * right number of free slots...
306          */
307
308 static int __init init_som_binfmt(void)
309 {
310         return register_binfmt(&som_format);
311 }
312
313 static void __exit exit_som_binfmt(void)
314 {
315         /* Remove the SOM loader. */
316         unregister_binfmt(&som_format);
317 }
318
319 core_initcall(init_som_binfmt);
320 module_exit(exit_som_binfmt);