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