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