ia32_binfmt.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Written 2000,2002 by Andi Kleen.
  3. *
  4. * Loosely based on the sparc64 and IA64 32bit emulation loaders.
  5. * This tricks binfmt_elf.c into loading 32bit binaries using lots
  6. * of ugly preprocessor tricks. Talk about very very poor man's inheritance.
  7. */
  8. #include <linux/types.h>
  9. #include <linux/stddef.h>
  10. #include <linux/rwsem.h>
  11. #include <linux/sched.h>
  12. #include <linux/compat.h>
  13. #include <linux/string.h>
  14. #include <linux/binfmts.h>
  15. #include <linux/mm.h>
  16. #include <linux/security.h>
  17. #include <linux/elfcore-compat.h>
  18. #include <asm/segment.h>
  19. #include <asm/ptrace.h>
  20. #include <asm/processor.h>
  21. #include <asm/user32.h>
  22. #include <asm/sigcontext32.h>
  23. #include <asm/fpu32.h>
  24. #include <asm/i387.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/ia32.h>
  27. #include <asm/vdso.h>
  28. #undef ELF_ARCH
  29. #undef ELF_CLASS
  30. #define ELF_CLASS ELFCLASS32
  31. #define ELF_ARCH EM_386
  32. #undef elfhdr
  33. #undef elf_phdr
  34. #undef elf_note
  35. #undef elf_addr_t
  36. #define elfhdr elf32_hdr
  37. #define elf_phdr elf32_phdr
  38. #define elf_note elf32_note
  39. #define elf_addr_t Elf32_Off
  40. #define ELF_NAME "elf/i386"
  41. #define AT_SYSINFO 32
  42. #define AT_SYSINFO_EHDR 33
  43. extern int sysctl_vsyscall32;
  44. #undef ARCH_DLINFO
  45. #define ARCH_DLINFO do { \
  46. if (sysctl_vsyscall32) { \
  47. NEW_AUX_ENT(AT_SYSINFO, (u32)VDSO_ENTRY); \
  48. NEW_AUX_ENT(AT_SYSINFO_EHDR, (u32)VDSO_CURRENT_BASE); \
  49. } \
  50. } while(0)
  51. struct file;
  52. #define IA32_EMULATOR 1
  53. #undef ELF_ET_DYN_BASE
  54. #define ELF_ET_DYN_BASE (TASK_UNMAPPED_BASE + 0x1000000)
  55. #define jiffies_to_timeval(a,b) do { (b)->tv_usec = 0; (b)->tv_sec = (a)/HZ; }while(0)
  56. #define _GET_SEG(x) \
  57. ({ __u32 seg; asm("movl %%" __stringify(x) ",%0" : "=r"(seg)); seg; })
  58. /* Assumes current==process to be dumped */
  59. #undef ELF_CORE_COPY_REGS
  60. #define ELF_CORE_COPY_REGS(pr_reg, regs) \
  61. pr_reg[0] = regs->bx; \
  62. pr_reg[1] = regs->cx; \
  63. pr_reg[2] = regs->dx; \
  64. pr_reg[3] = regs->si; \
  65. pr_reg[4] = regs->di; \
  66. pr_reg[5] = regs->bp; \
  67. pr_reg[6] = regs->ax; \
  68. pr_reg[7] = _GET_SEG(ds); \
  69. pr_reg[8] = _GET_SEG(es); \
  70. pr_reg[9] = _GET_SEG(fs); \
  71. pr_reg[10] = _GET_SEG(gs); \
  72. pr_reg[11] = regs->orig_ax; \
  73. pr_reg[12] = regs->ip; \
  74. pr_reg[13] = regs->cs; \
  75. pr_reg[14] = regs->flags; \
  76. pr_reg[15] = regs->sp; \
  77. pr_reg[16] = regs->ss;
  78. #define elf_prstatus compat_elf_prstatus
  79. #define elf_prpsinfo compat_elf_prpsinfo
  80. #define elf_fpregset_t struct user_i387_ia32_struct
  81. #define elf_fpxregset_t struct user32_fxsr_struct
  82. #define user user32
  83. #undef elf_read_implies_exec
  84. #define elf_read_implies_exec(ex, executable_stack) (executable_stack != EXSTACK_DISABLE_X)
  85. #define elf_core_copy_regs elf32_core_copy_regs
  86. static inline void elf32_core_copy_regs(compat_elf_gregset_t *elfregs,
  87. struct pt_regs *regs)
  88. {
  89. ELF_CORE_COPY_REGS((&elfregs->ebx), regs)
  90. }
  91. #define elf_core_copy_task_regs elf32_core_copy_task_regs
  92. static inline int elf32_core_copy_task_regs(struct task_struct *t,
  93. compat_elf_gregset_t* elfregs)
  94. {
  95. struct pt_regs *pp = task_pt_regs(t);
  96. ELF_CORE_COPY_REGS((&elfregs->ebx), pp);
  97. /* fix wrong segments */
  98. elfregs->ds = t->thread.ds;
  99. elfregs->fs = t->thread.fsindex;
  100. elfregs->gs = t->thread.gsindex;
  101. elfregs->es = t->thread.es;
  102. return 1;
  103. }
  104. #define elf_core_copy_task_fpregs elf32_core_copy_task_fpregs
  105. static inline int
  106. elf32_core_copy_task_fpregs(struct task_struct *tsk, struct pt_regs *regs,
  107. elf_fpregset_t *fpu)
  108. {
  109. struct _fpstate_ia32 *fpstate = (void*)fpu;
  110. mm_segment_t oldfs = get_fs();
  111. if (!tsk_used_math(tsk))
  112. return 0;
  113. if (!regs)
  114. regs = task_pt_regs(tsk);
  115. if (tsk == current)
  116. unlazy_fpu(tsk);
  117. set_fs(KERNEL_DS);
  118. save_i387_ia32(tsk, fpstate, regs, 1);
  119. /* Correct for i386 bug. It puts the fop into the upper 16bits of
  120. the tag word (like FXSAVE), not into the fcs*/
  121. fpstate->cssel |= fpstate->tag & 0xffff0000;
  122. set_fs(oldfs);
  123. return 1;
  124. }
  125. #define ELF_CORE_COPY_XFPREGS 1
  126. #define ELF_CORE_XFPREG_TYPE NT_PRXFPREG
  127. #define elf_core_copy_task_xfpregs elf32_core_copy_task_xfpregs
  128. static inline int
  129. elf32_core_copy_task_xfpregs(struct task_struct *t, elf_fpxregset_t *xfpu)
  130. {
  131. struct pt_regs *regs = task_pt_regs(t);
  132. if (!tsk_used_math(t))
  133. return 0;
  134. if (t == current)
  135. unlazy_fpu(t);
  136. memcpy(xfpu, &t->thread.i387.fxsave, sizeof(elf_fpxregset_t));
  137. xfpu->fcs = regs->cs;
  138. xfpu->fos = t->thread.ds; /* right? */
  139. return 1;
  140. }
  141. #undef elf_check_arch
  142. #define elf_check_arch(x) \
  143. ((x)->e_machine == EM_386)
  144. extern int force_personality32;
  145. #undef ELF_EXEC_PAGESIZE
  146. #undef ELF_HWCAP
  147. #undef ELF_PLATFORM
  148. #undef SET_PERSONALITY
  149. #define ELF_EXEC_PAGESIZE PAGE_SIZE
  150. #define ELF_HWCAP (boot_cpu_data.x86_capability[0])
  151. #define ELF_PLATFORM ("i686")
  152. #define SET_PERSONALITY(ex, ibcs2) \
  153. do { \
  154. unsigned long new_flags = 0; \
  155. if ((ex).e_ident[EI_CLASS] == ELFCLASS32) \
  156. new_flags = _TIF_IA32; \
  157. if ((current_thread_info()->flags & _TIF_IA32) \
  158. != new_flags) \
  159. set_thread_flag(TIF_ABI_PENDING); \
  160. else \
  161. clear_thread_flag(TIF_ABI_PENDING); \
  162. /* XXX This overwrites the user set personality */ \
  163. current->personality |= force_personality32; \
  164. } while (0)
  165. /* Override some function names */
  166. #define elf_format elf32_format
  167. #define init_elf_binfmt init_elf32_binfmt
  168. #define exit_elf_binfmt exit_elf32_binfmt
  169. #define load_elf_binary load_elf32_binary
  170. #undef ELF_PLAT_INIT
  171. #define ELF_PLAT_INIT(r, load_addr) elf32_init(r)
  172. #undef start_thread
  173. #define start_thread(regs,new_rip,new_rsp) do { \
  174. asm volatile("movl %0,%%fs" :: "r" (0)); \
  175. asm volatile("movl %0,%%es; movl %0,%%ds": :"r" (__USER32_DS)); \
  176. load_gs_index(0); \
  177. (regs)->ip = (new_rip); \
  178. (regs)->sp = (new_rsp); \
  179. (regs)->flags = X86_EFLAGS_IF; \
  180. (regs)->cs = __USER32_CS; \
  181. (regs)->ss = __USER32_DS; \
  182. set_fs(USER_DS); \
  183. } while(0)
  184. #include <linux/module.h>
  185. MODULE_DESCRIPTION("Binary format loader for compatibility with IA32 ELF binaries.");
  186. MODULE_AUTHOR("Eric Youngdale, Andi Kleen");
  187. #undef MODULE_DESCRIPTION
  188. #undef MODULE_AUTHOR
  189. static void elf32_init(struct pt_regs *);
  190. #define ARCH_HAS_SETUP_ADDITIONAL_PAGES 1
  191. #define arch_setup_additional_pages syscall32_setup_pages
  192. extern int syscall32_setup_pages(struct linux_binprm *, int exstack);
  193. #include "../../../fs/binfmt_elf.c"
  194. static void elf32_init(struct pt_regs *regs)
  195. {
  196. struct task_struct *me = current;
  197. regs->di = 0;
  198. regs->si = 0;
  199. regs->dx = 0;
  200. regs->cx = 0;
  201. regs->ax = 0;
  202. regs->bx = 0;
  203. regs->bp = 0;
  204. regs->r8 = regs->r9 = regs->r10 = regs->r11 = regs->r12 =
  205. regs->r13 = regs->r14 = regs->r15 = 0;
  206. me->thread.fs = 0;
  207. me->thread.gs = 0;
  208. me->thread.fsindex = 0;
  209. me->thread.gsindex = 0;
  210. me->thread.ds = __USER_DS;
  211. me->thread.es = __USER_DS;
  212. }
  213. #ifdef CONFIG_SYSCTL
  214. /* Register vsyscall32 into the ABI table */
  215. #include <linux/sysctl.h>
  216. static ctl_table abi_table2[] = {
  217. {
  218. .procname = "vsyscall32",
  219. .data = &sysctl_vsyscall32,
  220. .maxlen = sizeof(int),
  221. .mode = 0644,
  222. .proc_handler = proc_dointvec
  223. },
  224. {}
  225. };
  226. static ctl_table abi_root_table2[] = {
  227. {
  228. .ctl_name = CTL_ABI,
  229. .procname = "abi",
  230. .mode = 0555,
  231. .child = abi_table2
  232. },
  233. {}
  234. };
  235. static __init int ia32_binfmt_init(void)
  236. {
  237. register_sysctl_table(abi_root_table2);
  238. return 0;
  239. }
  240. __initcall(ia32_binfmt_init);
  241. #endif