ia32_binfmt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 <asm/segment.h>
  18. #include <asm/ptrace.h>
  19. #include <asm/processor.h>
  20. #include <asm/user32.h>
  21. #include <asm/sigcontext32.h>
  22. #include <asm/fpu32.h>
  23. #include <asm/i387.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/ia32.h>
  26. #include <asm/vsyscall32.h>
  27. #define ELF_NAME "elf/i386"
  28. #define AT_SYSINFO 32
  29. #define AT_SYSINFO_EHDR 33
  30. int sysctl_vsyscall32 = 1;
  31. #define ARCH_DLINFO do { \
  32. if (sysctl_vsyscall32) { \
  33. NEW_AUX_ENT(AT_SYSINFO, (u32)(u64)VSYSCALL32_VSYSCALL); \
  34. NEW_AUX_ENT(AT_SYSINFO_EHDR, VSYSCALL32_BASE); \
  35. } \
  36. } while(0)
  37. struct file;
  38. struct elf_phdr;
  39. #define IA32_EMULATOR 1
  40. #define ELF_ET_DYN_BASE (TASK_UNMAPPED_BASE + 0x1000000)
  41. #undef ELF_ARCH
  42. #define ELF_ARCH EM_386
  43. #undef ELF_CLASS
  44. #define ELF_CLASS ELFCLASS32
  45. #define ELF_DATA ELFDATA2LSB
  46. #define USE_ELF_CORE_DUMP 1
  47. /* Override elfcore.h */
  48. #define _LINUX_ELFCORE_H 1
  49. typedef unsigned int elf_greg_t;
  50. #define ELF_NGREG (sizeof (struct user_regs_struct32) / sizeof(elf_greg_t))
  51. typedef elf_greg_t elf_gregset_t[ELF_NGREG];
  52. /*
  53. * These macros parameterize elf_core_dump in fs/binfmt_elf.c to write out
  54. * extra segments containing the vsyscall DSO contents. Dumping its
  55. * contents makes post-mortem fully interpretable later without matching up
  56. * the same kernel and hardware config to see what PC values meant.
  57. * Dumping its extra ELF program headers includes all the other information
  58. * a debugger needs to easily find how the vsyscall DSO was being used.
  59. */
  60. #define ELF_CORE_EXTRA_PHDRS (find_vma(current->mm, VSYSCALL32_BASE) ? \
  61. (VSYSCALL32_EHDR->e_phnum) : 0)
  62. #define ELF_CORE_WRITE_EXTRA_PHDRS \
  63. do { \
  64. if (find_vma(current->mm, VSYSCALL32_BASE)) { \
  65. const struct elf32_phdr *const vsyscall_phdrs = \
  66. (const struct elf32_phdr *) (VSYSCALL32_BASE \
  67. + VSYSCALL32_EHDR->e_phoff);\
  68. int i; \
  69. Elf32_Off ofs = 0; \
  70. for (i = 0; i < VSYSCALL32_EHDR->e_phnum; ++i) { \
  71. struct elf32_phdr phdr = vsyscall_phdrs[i]; \
  72. if (phdr.p_type == PT_LOAD) { \
  73. BUG_ON(ofs != 0); \
  74. ofs = phdr.p_offset = offset; \
  75. phdr.p_memsz = PAGE_ALIGN(phdr.p_memsz); \
  76. phdr.p_filesz = phdr.p_memsz; \
  77. offset += phdr.p_filesz; \
  78. } \
  79. else \
  80. phdr.p_offset += ofs; \
  81. phdr.p_paddr = 0; /* match other core phdrs */ \
  82. DUMP_WRITE(&phdr, sizeof(phdr)); \
  83. } \
  84. } \
  85. } while (0)
  86. #define ELF_CORE_WRITE_EXTRA_DATA \
  87. do { \
  88. if (find_vma(current->mm, VSYSCALL32_BASE)) { \
  89. const struct elf32_phdr *const vsyscall_phdrs = \
  90. (const struct elf32_phdr *) (VSYSCALL32_BASE \
  91. + VSYSCALL32_EHDR->e_phoff); \
  92. int i; \
  93. for (i = 0; i < VSYSCALL32_EHDR->e_phnum; ++i) { \
  94. if (vsyscall_phdrs[i].p_type == PT_LOAD) \
  95. DUMP_WRITE((void *) (u64) vsyscall_phdrs[i].p_vaddr,\
  96. PAGE_ALIGN(vsyscall_phdrs[i].p_memsz)); \
  97. } \
  98. } \
  99. } while (0)
  100. struct elf_siginfo
  101. {
  102. int si_signo; /* signal number */
  103. int si_code; /* extra code */
  104. int si_errno; /* errno */
  105. };
  106. #define jiffies_to_timeval(a,b) do { (b)->tv_usec = 0; (b)->tv_sec = (a)/HZ; }while(0)
  107. struct elf_prstatus
  108. {
  109. struct elf_siginfo pr_info; /* Info associated with signal */
  110. short pr_cursig; /* Current signal */
  111. unsigned int pr_sigpend; /* Set of pending signals */
  112. unsigned int pr_sighold; /* Set of held signals */
  113. pid_t pr_pid;
  114. pid_t pr_ppid;
  115. pid_t pr_pgrp;
  116. pid_t pr_sid;
  117. struct compat_timeval pr_utime; /* User time */
  118. struct compat_timeval pr_stime; /* System time */
  119. struct compat_timeval pr_cutime; /* Cumulative user time */
  120. struct compat_timeval pr_cstime; /* Cumulative system time */
  121. elf_gregset_t pr_reg; /* GP registers */
  122. int pr_fpvalid; /* True if math co-processor being used. */
  123. };
  124. #define ELF_PRARGSZ (80) /* Number of chars for args */
  125. struct elf_prpsinfo
  126. {
  127. char pr_state; /* numeric process state */
  128. char pr_sname; /* char for pr_state */
  129. char pr_zomb; /* zombie */
  130. char pr_nice; /* nice val */
  131. unsigned int pr_flag; /* flags */
  132. __u16 pr_uid;
  133. __u16 pr_gid;
  134. pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
  135. /* Lots missing */
  136. char pr_fname[16]; /* filename of executable */
  137. char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
  138. };
  139. #define __STR(x) #x
  140. #define STR(x) __STR(x)
  141. #define _GET_SEG(x) \
  142. ({ __u32 seg; asm("movl %%" STR(x) ",%0" : "=r"(seg)); seg; })
  143. /* Assumes current==process to be dumped */
  144. #define ELF_CORE_COPY_REGS(pr_reg, regs) \
  145. pr_reg[0] = regs->rbx; \
  146. pr_reg[1] = regs->rcx; \
  147. pr_reg[2] = regs->rdx; \
  148. pr_reg[3] = regs->rsi; \
  149. pr_reg[4] = regs->rdi; \
  150. pr_reg[5] = regs->rbp; \
  151. pr_reg[6] = regs->rax; \
  152. pr_reg[7] = _GET_SEG(ds); \
  153. pr_reg[8] = _GET_SEG(es); \
  154. pr_reg[9] = _GET_SEG(fs); \
  155. pr_reg[10] = _GET_SEG(gs); \
  156. pr_reg[11] = regs->orig_rax; \
  157. pr_reg[12] = regs->rip; \
  158. pr_reg[13] = regs->cs; \
  159. pr_reg[14] = regs->eflags; \
  160. pr_reg[15] = regs->rsp; \
  161. pr_reg[16] = regs->ss;
  162. #define user user32
  163. #define __ASM_X86_64_ELF_H 1
  164. #define elf_read_implies_exec(ex, executable_stack) (executable_stack != EXSTACK_DISABLE_X)
  165. //#include <asm/ia32.h>
  166. #include <linux/elf.h>
  167. typedef struct user_i387_ia32_struct elf_fpregset_t;
  168. typedef struct user32_fxsr_struct elf_fpxregset_t;
  169. static inline void elf_core_copy_regs(elf_gregset_t *elfregs, struct pt_regs *regs)
  170. {
  171. ELF_CORE_COPY_REGS((*elfregs), regs)
  172. }
  173. static inline int elf_core_copy_task_regs(struct task_struct *t, elf_gregset_t* elfregs)
  174. {
  175. struct pt_regs *pp = task_pt_regs(t);
  176. ELF_CORE_COPY_REGS((*elfregs), pp);
  177. /* fix wrong segments */
  178. (*elfregs)[7] = t->thread.ds;
  179. (*elfregs)[9] = t->thread.fsindex;
  180. (*elfregs)[10] = t->thread.gsindex;
  181. (*elfregs)[8] = t->thread.es;
  182. return 1;
  183. }
  184. static inline int
  185. elf_core_copy_task_fpregs(struct task_struct *tsk, struct pt_regs *regs, elf_fpregset_t *fpu)
  186. {
  187. struct _fpstate_ia32 *fpstate = (void*)fpu;
  188. mm_segment_t oldfs = get_fs();
  189. if (!tsk_used_math(tsk))
  190. return 0;
  191. if (!regs)
  192. regs = task_pt_regs(tsk);
  193. if (tsk == current)
  194. unlazy_fpu(tsk);
  195. set_fs(KERNEL_DS);
  196. save_i387_ia32(tsk, fpstate, regs, 1);
  197. /* Correct for i386 bug. It puts the fop into the upper 16bits of
  198. the tag word (like FXSAVE), not into the fcs*/
  199. fpstate->cssel |= fpstate->tag & 0xffff0000;
  200. set_fs(oldfs);
  201. return 1;
  202. }
  203. #define ELF_CORE_COPY_XFPREGS 1
  204. static inline int
  205. elf_core_copy_task_xfpregs(struct task_struct *t, elf_fpxregset_t *xfpu)
  206. {
  207. struct pt_regs *regs = task_pt_regs(t);
  208. if (!tsk_used_math(t))
  209. return 0;
  210. if (t == current)
  211. unlazy_fpu(t);
  212. memcpy(xfpu, &t->thread.i387.fxsave, sizeof(elf_fpxregset_t));
  213. xfpu->fcs = regs->cs;
  214. xfpu->fos = t->thread.ds; /* right? */
  215. return 1;
  216. }
  217. #undef elf_check_arch
  218. #define elf_check_arch(x) \
  219. ((x)->e_machine == EM_386)
  220. extern int force_personality32;
  221. #define ELF_EXEC_PAGESIZE PAGE_SIZE
  222. #define ELF_HWCAP (boot_cpu_data.x86_capability[0])
  223. #define ELF_PLATFORM ("i686")
  224. #define SET_PERSONALITY(ex, ibcs2) \
  225. do { \
  226. unsigned long new_flags = 0; \
  227. if ((ex).e_ident[EI_CLASS] == ELFCLASS32) \
  228. new_flags = _TIF_IA32; \
  229. if ((current_thread_info()->flags & _TIF_IA32) \
  230. != new_flags) \
  231. set_thread_flag(TIF_ABI_PENDING); \
  232. else \
  233. clear_thread_flag(TIF_ABI_PENDING); \
  234. /* XXX This overwrites the user set personality */ \
  235. current->personality |= force_personality32; \
  236. } while (0)
  237. /* Override some function names */
  238. #define elf_format elf32_format
  239. #define init_elf_binfmt init_elf32_binfmt
  240. #define exit_elf_binfmt exit_elf32_binfmt
  241. #define load_elf_binary load_elf32_binary
  242. #define ELF_PLAT_INIT(r, load_addr) elf32_init(r)
  243. #define setup_arg_pages(bprm, stack_top, exec_stack) \
  244. ia32_setup_arg_pages(bprm, stack_top, exec_stack)
  245. int ia32_setup_arg_pages(struct linux_binprm *bprm, unsigned long stack_top, int executable_stack);
  246. #undef start_thread
  247. #define start_thread(regs,new_rip,new_rsp) do { \
  248. asm volatile("movl %0,%%fs" :: "r" (0)); \
  249. asm volatile("movl %0,%%es; movl %0,%%ds": :"r" (__USER32_DS)); \
  250. load_gs_index(0); \
  251. (regs)->rip = (new_rip); \
  252. (regs)->rsp = (new_rsp); \
  253. (regs)->eflags = 0x200; \
  254. (regs)->cs = __USER32_CS; \
  255. (regs)->ss = __USER32_DS; \
  256. set_fs(USER_DS); \
  257. } while(0)
  258. #include <linux/module.h>
  259. MODULE_DESCRIPTION("Binary format loader for compatibility with IA32 ELF binaries.");
  260. MODULE_AUTHOR("Eric Youngdale, Andi Kleen");
  261. #undef MODULE_DESCRIPTION
  262. #undef MODULE_AUTHOR
  263. #define elf_addr_t __u32
  264. static void elf32_init(struct pt_regs *);
  265. #define ARCH_HAS_SETUP_ADDITIONAL_PAGES 1
  266. #define arch_setup_additional_pages syscall32_setup_pages
  267. extern int syscall32_setup_pages(struct linux_binprm *, int exstack);
  268. #include "../../../fs/binfmt_elf.c"
  269. static void elf32_init(struct pt_regs *regs)
  270. {
  271. struct task_struct *me = current;
  272. regs->rdi = 0;
  273. regs->rsi = 0;
  274. regs->rdx = 0;
  275. regs->rcx = 0;
  276. regs->rax = 0;
  277. regs->rbx = 0;
  278. regs->rbp = 0;
  279. regs->r8 = regs->r9 = regs->r10 = regs->r11 = regs->r12 =
  280. regs->r13 = regs->r14 = regs->r15 = 0;
  281. me->thread.fs = 0;
  282. me->thread.gs = 0;
  283. me->thread.fsindex = 0;
  284. me->thread.gsindex = 0;
  285. me->thread.ds = __USER_DS;
  286. me->thread.es = __USER_DS;
  287. }
  288. int ia32_setup_arg_pages(struct linux_binprm *bprm, unsigned long stack_top,
  289. int executable_stack)
  290. {
  291. unsigned long stack_base;
  292. struct vm_area_struct *mpnt;
  293. struct mm_struct *mm = current->mm;
  294. int i, ret;
  295. stack_base = stack_top - MAX_ARG_PAGES * PAGE_SIZE;
  296. mm->arg_start = bprm->p + stack_base;
  297. bprm->p += stack_base;
  298. if (bprm->loader)
  299. bprm->loader += stack_base;
  300. bprm->exec += stack_base;
  301. mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
  302. if (!mpnt)
  303. return -ENOMEM;
  304. memset(mpnt, 0, sizeof(*mpnt));
  305. down_write(&mm->mmap_sem);
  306. {
  307. mpnt->vm_mm = mm;
  308. mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
  309. mpnt->vm_end = stack_top;
  310. if (executable_stack == EXSTACK_ENABLE_X)
  311. mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
  312. else if (executable_stack == EXSTACK_DISABLE_X)
  313. mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
  314. else
  315. mpnt->vm_flags = VM_STACK_FLAGS;
  316. mpnt->vm_page_prot = (mpnt->vm_flags & VM_EXEC) ?
  317. PAGE_COPY_EXEC : PAGE_COPY;
  318. if ((ret = insert_vm_struct(mm, mpnt))) {
  319. up_write(&mm->mmap_sem);
  320. kmem_cache_free(vm_area_cachep, mpnt);
  321. return ret;
  322. }
  323. mm->stack_vm = mm->total_vm = vma_pages(mpnt);
  324. }
  325. for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
  326. struct page *page = bprm->page[i];
  327. if (page) {
  328. bprm->page[i] = NULL;
  329. install_arg_page(mpnt, page, stack_base);
  330. }
  331. stack_base += PAGE_SIZE;
  332. }
  333. up_write(&mm->mmap_sem);
  334. return 0;
  335. }
  336. EXPORT_SYMBOL(ia32_setup_arg_pages);
  337. #ifdef CONFIG_SYSCTL
  338. /* Register vsyscall32 into the ABI table */
  339. #include <linux/sysctl.h>
  340. static ctl_table abi_table2[] = {
  341. { 99, "vsyscall32", &sysctl_vsyscall32, sizeof(int), 0644, NULL,
  342. proc_dointvec },
  343. { 0, }
  344. };
  345. static ctl_table abi_root_table2[] = {
  346. { .ctl_name = CTL_ABI, .procname = "abi", .mode = 0555,
  347. .child = abi_table2 },
  348. { 0 },
  349. };
  350. static __init int ia32_binfmt_init(void)
  351. {
  352. register_sysctl_table(abi_root_table2, 1);
  353. return 0;
  354. }
  355. __initcall(ia32_binfmt_init);
  356. #endif