binfmt_som.c 7.8 KB

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