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