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