ia32_binfmt.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. #define __ASM_X86_64_ELF_H 1
  9. #undef ELF_CLASS
  10. #define ELF_CLASS ELFCLASS32
  11. #include <linux/types.h>
  12. #include <linux/stddef.h>
  13. #include <linux/rwsem.h>
  14. #include <linux/sched.h>
  15. #include <linux/compat.h>
  16. #include <linux/string.h>
  17. #include <linux/binfmts.h>
  18. #include <linux/mm.h>
  19. #include <linux/security.h>
  20. #include <asm/segment.h>
  21. #include <asm/ptrace.h>
  22. #include <asm/processor.h>
  23. #include <asm/user32.h>
  24. #include <asm/sigcontext32.h>
  25. #include <asm/fpu32.h>
  26. #include <asm/i387.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/ia32.h>
  29. #include <asm/vsyscall32.h>
  30. #define ELF_NAME "elf/i386"
  31. #define AT_SYSINFO 32
  32. #define AT_SYSINFO_EHDR 33
  33. int sysctl_vsyscall32 = 1;
  34. #undef ARCH_DLINFO
  35. #define ARCH_DLINFO do { \
  36. if (sysctl_vsyscall32) { \
  37. current->mm->context.vdso = (void *)VSYSCALL32_BASE; \
  38. NEW_AUX_ENT(AT_SYSINFO, (u32)(u64)VSYSCALL32_VSYSCALL); \
  39. NEW_AUX_ENT(AT_SYSINFO_EHDR, VSYSCALL32_BASE); \
  40. } \
  41. } while(0)
  42. struct file;
  43. struct elf_phdr;
  44. #define IA32_EMULATOR 1
  45. #define ELF_ET_DYN_BASE (TASK_UNMAPPED_BASE + 0x1000000)
  46. #undef ELF_ARCH
  47. #define ELF_ARCH EM_386
  48. #define ELF_DATA ELFDATA2LSB
  49. #define USE_ELF_CORE_DUMP 1
  50. /* Override elfcore.h */
  51. #define _LINUX_ELFCORE_H 1
  52. typedef unsigned int elf_greg_t;
  53. #define ELF_NGREG (sizeof (struct user_regs_struct32) / sizeof(elf_greg_t))
  54. typedef elf_greg_t elf_gregset_t[ELF_NGREG];
  55. struct elf_siginfo
  56. {
  57. int si_signo; /* signal number */
  58. int si_code; /* extra code */
  59. int si_errno; /* errno */
  60. };
  61. #define jiffies_to_timeval(a,b) do { (b)->tv_usec = 0; (b)->tv_sec = (a)/HZ; }while(0)
  62. struct elf_prstatus
  63. {
  64. struct elf_siginfo pr_info; /* Info associated with signal */
  65. short pr_cursig; /* Current signal */
  66. unsigned int pr_sigpend; /* Set of pending signals */
  67. unsigned int pr_sighold; /* Set of held signals */
  68. pid_t pr_pid;
  69. pid_t pr_ppid;
  70. pid_t pr_pgrp;
  71. pid_t pr_sid;
  72. struct compat_timeval pr_utime; /* User time */
  73. struct compat_timeval pr_stime; /* System time */
  74. struct compat_timeval pr_cutime; /* Cumulative user time */
  75. struct compat_timeval pr_cstime; /* Cumulative system time */
  76. elf_gregset_t pr_reg; /* GP registers */
  77. int pr_fpvalid; /* True if math co-processor being used. */
  78. };
  79. #define ELF_PRARGSZ (80) /* Number of chars for args */
  80. struct elf_prpsinfo
  81. {
  82. char pr_state; /* numeric process state */
  83. char pr_sname; /* char for pr_state */
  84. char pr_zomb; /* zombie */
  85. char pr_nice; /* nice val */
  86. unsigned int pr_flag; /* flags */
  87. __u16 pr_uid;
  88. __u16 pr_gid;
  89. pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
  90. /* Lots missing */
  91. char pr_fname[16]; /* filename of executable */
  92. char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
  93. };
  94. #define __STR(x) #x
  95. #define STR(x) __STR(x)
  96. #define _GET_SEG(x) \
  97. ({ __u32 seg; asm("movl %%" STR(x) ",%0" : "=r"(seg)); seg; })
  98. /* Assumes current==process to be dumped */
  99. #define ELF_CORE_COPY_REGS(pr_reg, regs) \
  100. pr_reg[0] = regs->rbx; \
  101. pr_reg[1] = regs->rcx; \
  102. pr_reg[2] = regs->rdx; \
  103. pr_reg[3] = regs->rsi; \
  104. pr_reg[4] = regs->rdi; \
  105. pr_reg[5] = regs->rbp; \
  106. pr_reg[6] = regs->rax; \
  107. pr_reg[7] = _GET_SEG(ds); \
  108. pr_reg[8] = _GET_SEG(es); \
  109. pr_reg[9] = _GET_SEG(fs); \
  110. pr_reg[10] = _GET_SEG(gs); \
  111. pr_reg[11] = regs->orig_rax; \
  112. pr_reg[12] = regs->rip; \
  113. pr_reg[13] = regs->cs; \
  114. pr_reg[14] = regs->eflags; \
  115. pr_reg[15] = regs->rsp; \
  116. pr_reg[16] = regs->ss;
  117. #define user user32
  118. #undef elf_read_implies_exec
  119. #define elf_read_implies_exec(ex, executable_stack) (executable_stack != EXSTACK_DISABLE_X)
  120. //#include <asm/ia32.h>
  121. #include <linux/elf.h>
  122. typedef struct user_i387_ia32_struct elf_fpregset_t;
  123. typedef struct user32_fxsr_struct elf_fpxregset_t;
  124. static inline void elf_core_copy_regs(elf_gregset_t *elfregs, struct pt_regs *regs)
  125. {
  126. ELF_CORE_COPY_REGS((*elfregs), regs)
  127. }
  128. static inline int elf_core_copy_task_regs(struct task_struct *t, elf_gregset_t* elfregs)
  129. {
  130. struct pt_regs *pp = task_pt_regs(t);
  131. ELF_CORE_COPY_REGS((*elfregs), pp);
  132. /* fix wrong segments */
  133. (*elfregs)[7] = t->thread.ds;
  134. (*elfregs)[9] = t->thread.fsindex;
  135. (*elfregs)[10] = t->thread.gsindex;
  136. (*elfregs)[8] = t->thread.es;
  137. return 1;
  138. }
  139. static inline int
  140. elf_core_copy_task_fpregs(struct task_struct *tsk, struct pt_regs *regs, elf_fpregset_t *fpu)
  141. {
  142. struct _fpstate_ia32 *fpstate = (void*)fpu;
  143. mm_segment_t oldfs = get_fs();
  144. if (!tsk_used_math(tsk))
  145. return 0;
  146. if (!regs)
  147. regs = task_pt_regs(tsk);
  148. if (tsk == current)
  149. unlazy_fpu(tsk);
  150. set_fs(KERNEL_DS);
  151. save_i387_ia32(tsk, fpstate, regs, 1);
  152. /* Correct for i386 bug. It puts the fop into the upper 16bits of
  153. the tag word (like FXSAVE), not into the fcs*/
  154. fpstate->cssel |= fpstate->tag & 0xffff0000;
  155. set_fs(oldfs);
  156. return 1;
  157. }
  158. #define ELF_CORE_COPY_XFPREGS 1
  159. static inline int
  160. elf_core_copy_task_xfpregs(struct task_struct *t, elf_fpxregset_t *xfpu)
  161. {
  162. struct pt_regs *regs = task_pt_regs(t);
  163. if (!tsk_used_math(t))
  164. return 0;
  165. if (t == current)
  166. unlazy_fpu(t);
  167. memcpy(xfpu, &t->thread.i387.fxsave, sizeof(elf_fpxregset_t));
  168. xfpu->fcs = regs->cs;
  169. xfpu->fos = t->thread.ds; /* right? */
  170. return 1;
  171. }
  172. #undef elf_check_arch
  173. #define elf_check_arch(x) \
  174. ((x)->e_machine == EM_386)
  175. extern int force_personality32;
  176. #define ELF_EXEC_PAGESIZE PAGE_SIZE
  177. #define ELF_HWCAP (boot_cpu_data.x86_capability[0])
  178. #define ELF_PLATFORM ("i686")
  179. #define SET_PERSONALITY(ex, ibcs2) \
  180. do { \
  181. unsigned long new_flags = 0; \
  182. if ((ex).e_ident[EI_CLASS] == ELFCLASS32) \
  183. new_flags = _TIF_IA32; \
  184. if ((current_thread_info()->flags & _TIF_IA32) \
  185. != new_flags) \
  186. set_thread_flag(TIF_ABI_PENDING); \
  187. else \
  188. clear_thread_flag(TIF_ABI_PENDING); \
  189. /* XXX This overwrites the user set personality */ \
  190. current->personality |= force_personality32; \
  191. } while (0)
  192. /* Override some function names */
  193. #define elf_format elf32_format
  194. #define init_elf_binfmt init_elf32_binfmt
  195. #define exit_elf_binfmt exit_elf32_binfmt
  196. #define load_elf_binary load_elf32_binary
  197. #define ELF_PLAT_INIT(r, load_addr) elf32_init(r)
  198. #undef start_thread
  199. #define start_thread(regs,new_rip,new_rsp) do { \
  200. asm volatile("movl %0,%%fs" :: "r" (0)); \
  201. asm volatile("movl %0,%%es; movl %0,%%ds": :"r" (__USER32_DS)); \
  202. load_gs_index(0); \
  203. (regs)->rip = (new_rip); \
  204. (regs)->rsp = (new_rsp); \
  205. (regs)->eflags = 0x200; \
  206. (regs)->cs = __USER32_CS; \
  207. (regs)->ss = __USER32_DS; \
  208. set_fs(USER_DS); \
  209. } while(0)
  210. #include <linux/module.h>
  211. MODULE_DESCRIPTION("Binary format loader for compatibility with IA32 ELF binaries.");
  212. MODULE_AUTHOR("Eric Youngdale, Andi Kleen");
  213. #undef MODULE_DESCRIPTION
  214. #undef MODULE_AUTHOR
  215. static void elf32_init(struct pt_regs *);
  216. #define ARCH_HAS_SETUP_ADDITIONAL_PAGES 1
  217. #define arch_setup_additional_pages syscall32_setup_pages
  218. extern int syscall32_setup_pages(struct linux_binprm *, int exstack);
  219. #include "../../../fs/binfmt_elf.c"
  220. static void elf32_init(struct pt_regs *regs)
  221. {
  222. struct task_struct *me = current;
  223. regs->rdi = 0;
  224. regs->rsi = 0;
  225. regs->rdx = 0;
  226. regs->rcx = 0;
  227. regs->rax = 0;
  228. regs->rbx = 0;
  229. regs->rbp = 0;
  230. regs->r8 = regs->r9 = regs->r10 = regs->r11 = regs->r12 =
  231. regs->r13 = regs->r14 = regs->r15 = 0;
  232. me->thread.fs = 0;
  233. me->thread.gs = 0;
  234. me->thread.fsindex = 0;
  235. me->thread.gsindex = 0;
  236. me->thread.ds = __USER_DS;
  237. me->thread.es = __USER_DS;
  238. }
  239. #ifdef CONFIG_SYSCTL
  240. /* Register vsyscall32 into the ABI table */
  241. #include <linux/sysctl.h>
  242. static ctl_table abi_table2[] = {
  243. {
  244. .ctl_name = 99,
  245. .procname = "vsyscall32",
  246. .data = &sysctl_vsyscall32,
  247. .maxlen = sizeof(int),
  248. .mode = 0644,
  249. .proc_handler = proc_dointvec
  250. },
  251. {}
  252. };
  253. static ctl_table abi_root_table2[] = {
  254. {
  255. .ctl_name = CTL_ABI,
  256. .procname = "abi",
  257. .mode = 0555,
  258. .child = abi_table2
  259. },
  260. {}
  261. };
  262. static __init int ia32_binfmt_init(void)
  263. {
  264. register_sysctl_table(abi_root_table2);
  265. return 0;
  266. }
  267. __initcall(ia32_binfmt_init);
  268. #endif