ia32_binfmt.c 11 KB

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