ia32_binfmt.c 7.1 KB

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