ia32_binfmt.c 12 KB

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