binfmt_som.c 7.4 KB

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