binfmt_elf32.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * IA-32 ELF support.
  3. *
  4. * Copyright (C) 1999 Arun Sharma <arun.sharma@intel.com>
  5. * Copyright (C) 2001 Hewlett-Packard Co
  6. * David Mosberger-Tang <davidm@hpl.hp.com>
  7. *
  8. * 06/16/00 A. Mallick initialize csd/ssd/tssd/cflg for ia32_load_state
  9. * 04/13/01 D. Mosberger dropped saving tssd in ar.k1---it's not needed
  10. * 09/14/01 D. Mosberger fixed memory management for gdt/tss page
  11. */
  12. #include <linux/config.h>
  13. #include <linux/types.h>
  14. #include <linux/mm.h>
  15. #include <linux/security.h>
  16. #include <asm/param.h>
  17. #include <asm/signal.h>
  18. #include "ia32priv.h"
  19. #include "elfcore32.h"
  20. /* Override some function names */
  21. #undef start_thread
  22. #define start_thread ia32_start_thread
  23. #define elf_format elf32_format
  24. #define init_elf_binfmt init_elf32_binfmt
  25. #define exit_elf_binfmt exit_elf32_binfmt
  26. #undef CLOCKS_PER_SEC
  27. #define CLOCKS_PER_SEC IA32_CLOCKS_PER_SEC
  28. extern void ia64_elf32_init (struct pt_regs *regs);
  29. static void elf32_set_personality (void);
  30. #define setup_arg_pages(bprm,tos,exec) ia32_setup_arg_pages(bprm,exec)
  31. #define elf_map elf32_map
  32. #undef SET_PERSONALITY
  33. #define SET_PERSONALITY(ex, ibcs2) elf32_set_personality()
  34. #define elf_read_implies_exec(ex, have_pt_gnu_stack) (!(have_pt_gnu_stack))
  35. /* Ugly but avoids duplication */
  36. #include "../../../fs/binfmt_elf.c"
  37. extern struct page *ia32_shared_page[];
  38. extern unsigned long *ia32_gdt;
  39. extern struct page *ia32_gate_page;
  40. struct page *
  41. ia32_install_shared_page (struct vm_area_struct *vma, unsigned long address, int *type)
  42. {
  43. struct page *pg = ia32_shared_page[smp_processor_id()];
  44. get_page(pg);
  45. if (type)
  46. *type = VM_FAULT_MINOR;
  47. return pg;
  48. }
  49. struct page *
  50. ia32_install_gate_page (struct vm_area_struct *vma, unsigned long address, int *type)
  51. {
  52. struct page *pg = ia32_gate_page;
  53. get_page(pg);
  54. if (type)
  55. *type = VM_FAULT_MINOR;
  56. return pg;
  57. }
  58. static struct vm_operations_struct ia32_shared_page_vm_ops = {
  59. .nopage = ia32_install_shared_page
  60. };
  61. static struct vm_operations_struct ia32_gate_page_vm_ops = {
  62. .nopage = ia32_install_gate_page
  63. };
  64. void
  65. ia64_elf32_init (struct pt_regs *regs)
  66. {
  67. struct vm_area_struct *vma;
  68. /*
  69. * Map GDT below 4GB, where the processor can find it. We need to map
  70. * it with privilege level 3 because the IVE uses non-privileged accesses to these
  71. * tables. IA-32 segmentation is used to protect against IA-32 accesses to them.
  72. */
  73. vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
  74. if (vma) {
  75. memset(vma, 0, sizeof(*vma));
  76. vma->vm_mm = current->mm;
  77. vma->vm_start = IA32_GDT_OFFSET;
  78. vma->vm_end = vma->vm_start + PAGE_SIZE;
  79. vma->vm_page_prot = PAGE_SHARED;
  80. vma->vm_flags = VM_READ|VM_MAYREAD|VM_RESERVED;
  81. vma->vm_ops = &ia32_shared_page_vm_ops;
  82. down_write(&current->mm->mmap_sem);
  83. {
  84. if (insert_vm_struct(current->mm, vma)) {
  85. kmem_cache_free(vm_area_cachep, vma);
  86. up_write(&current->mm->mmap_sem);
  87. BUG();
  88. }
  89. }
  90. up_write(&current->mm->mmap_sem);
  91. }
  92. /*
  93. * When user stack is not executable, push sigreturn code to stack makes
  94. * segmentation fault raised when returning to kernel. So now sigreturn
  95. * code is locked in specific gate page, which is pointed by pretcode
  96. * when setup_frame_ia32
  97. */
  98. vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
  99. if (vma) {
  100. memset(vma, 0, sizeof(*vma));
  101. vma->vm_mm = current->mm;
  102. vma->vm_start = IA32_GATE_OFFSET;
  103. vma->vm_end = vma->vm_start + PAGE_SIZE;
  104. vma->vm_page_prot = PAGE_COPY_EXEC;
  105. vma->vm_flags = VM_READ | VM_MAYREAD | VM_EXEC
  106. | VM_MAYEXEC | VM_RESERVED;
  107. vma->vm_ops = &ia32_gate_page_vm_ops;
  108. down_write(&current->mm->mmap_sem);
  109. {
  110. if (insert_vm_struct(current->mm, vma)) {
  111. kmem_cache_free(vm_area_cachep, vma);
  112. up_write(&current->mm->mmap_sem);
  113. BUG();
  114. }
  115. }
  116. up_write(&current->mm->mmap_sem);
  117. }
  118. /*
  119. * Install LDT as anonymous memory. This gives us all-zero segment descriptors
  120. * until a task modifies them via modify_ldt().
  121. */
  122. vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
  123. if (vma) {
  124. memset(vma, 0, sizeof(*vma));
  125. vma->vm_mm = current->mm;
  126. vma->vm_start = IA32_LDT_OFFSET;
  127. vma->vm_end = vma->vm_start + PAGE_ALIGN(IA32_LDT_ENTRIES*IA32_LDT_ENTRY_SIZE);
  128. vma->vm_page_prot = PAGE_SHARED;
  129. vma->vm_flags = VM_READ|VM_WRITE|VM_MAYREAD|VM_MAYWRITE;
  130. down_write(&current->mm->mmap_sem);
  131. {
  132. if (insert_vm_struct(current->mm, vma)) {
  133. kmem_cache_free(vm_area_cachep, vma);
  134. up_write(&current->mm->mmap_sem);
  135. BUG();
  136. }
  137. }
  138. up_write(&current->mm->mmap_sem);
  139. }
  140. ia64_psr(regs)->ac = 0; /* turn off alignment checking */
  141. regs->loadrs = 0;
  142. /*
  143. * According to the ABI %edx points to an `atexit' handler. Since we don't have
  144. * one we'll set it to 0 and initialize all the other registers just to make
  145. * things more deterministic, ala the i386 implementation.
  146. */
  147. regs->r8 = 0; /* %eax */
  148. regs->r11 = 0; /* %ebx */
  149. regs->r9 = 0; /* %ecx */
  150. regs->r10 = 0; /* %edx */
  151. regs->r13 = 0; /* %ebp */
  152. regs->r14 = 0; /* %esi */
  153. regs->r15 = 0; /* %edi */
  154. current->thread.eflag = IA32_EFLAG;
  155. current->thread.fsr = IA32_FSR_DEFAULT;
  156. current->thread.fcr = IA32_FCR_DEFAULT;
  157. current->thread.fir = 0;
  158. current->thread.fdr = 0;
  159. /*
  160. * Setup GDTD. Note: GDTD is the descrambled version of the pseudo-descriptor
  161. * format defined by Figure 3-11 "Pseudo-Descriptor Format" in the IA-32
  162. * architecture manual. Also note that the only fields that are not ignored are
  163. * `base', `limit', 'G', `P' (must be 1) and `S' (must be 0).
  164. */
  165. regs->r31 = IA32_SEG_UNSCRAMBLE(IA32_SEG_DESCRIPTOR(IA32_GDT_OFFSET, IA32_PAGE_SIZE - 1,
  166. 0, 0, 0, 1, 0, 0, 0));
  167. /* Setup the segment selectors */
  168. regs->r16 = (__USER_DS << 16) | __USER_DS; /* ES == DS, GS, FS are zero */
  169. regs->r17 = (__USER_DS << 16) | __USER_CS; /* SS, CS; ia32_load_state() sets TSS and LDT */
  170. ia32_load_segment_descriptors(current);
  171. ia32_load_state(current);
  172. }
  173. int
  174. ia32_setup_arg_pages (struct linux_binprm *bprm, int executable_stack)
  175. {
  176. unsigned long stack_base;
  177. struct vm_area_struct *mpnt;
  178. struct mm_struct *mm = current->mm;
  179. int i, ret;
  180. stack_base = IA32_STACK_TOP - MAX_ARG_PAGES*PAGE_SIZE;
  181. mm->arg_start = bprm->p + stack_base;
  182. bprm->p += stack_base;
  183. if (bprm->loader)
  184. bprm->loader += stack_base;
  185. bprm->exec += stack_base;
  186. mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
  187. if (!mpnt)
  188. return -ENOMEM;
  189. if (security_vm_enough_memory((IA32_STACK_TOP - (PAGE_MASK & (unsigned long) bprm->p))
  190. >> PAGE_SHIFT)) {
  191. kmem_cache_free(vm_area_cachep, mpnt);
  192. return -ENOMEM;
  193. }
  194. memset(mpnt, 0, sizeof(*mpnt));
  195. down_write(&current->mm->mmap_sem);
  196. {
  197. mpnt->vm_mm = current->mm;
  198. mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
  199. mpnt->vm_end = IA32_STACK_TOP;
  200. if (executable_stack == EXSTACK_ENABLE_X)
  201. mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
  202. else if (executable_stack == EXSTACK_DISABLE_X)
  203. mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
  204. else
  205. mpnt->vm_flags = VM_STACK_FLAGS;
  206. mpnt->vm_page_prot = (mpnt->vm_flags & VM_EXEC)?
  207. PAGE_COPY_EXEC: PAGE_COPY;
  208. if ((ret = insert_vm_struct(current->mm, mpnt))) {
  209. up_write(&current->mm->mmap_sem);
  210. kmem_cache_free(vm_area_cachep, mpnt);
  211. return ret;
  212. }
  213. current->mm->stack_vm = current->mm->total_vm = vma_pages(mpnt);
  214. }
  215. for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
  216. struct page *page = bprm->page[i];
  217. if (page) {
  218. bprm->page[i] = NULL;
  219. install_arg_page(mpnt, page, stack_base);
  220. }
  221. stack_base += PAGE_SIZE;
  222. }
  223. up_write(&current->mm->mmap_sem);
  224. /* Can't do it in ia64_elf32_init(). Needs to be done before calls to
  225. elf32_map() */
  226. current->thread.ppl = ia32_init_pp_list();
  227. return 0;
  228. }
  229. static void
  230. elf32_set_personality (void)
  231. {
  232. set_personality(PER_LINUX32);
  233. current->thread.map_base = IA32_PAGE_OFFSET/3;
  234. current->thread.task_size = IA32_PAGE_OFFSET; /* use what Linux/x86 uses... */
  235. set_fs(USER_DS); /* set addr limit for new TASK_SIZE */
  236. }
  237. static unsigned long
  238. elf32_map (struct file *filep, unsigned long addr, struct elf_phdr *eppnt, int prot, int type)
  239. {
  240. unsigned long pgoff = (eppnt->p_vaddr) & ~IA32_PAGE_MASK;
  241. return ia32_do_mmap(filep, (addr & IA32_PAGE_MASK), eppnt->p_filesz + pgoff, prot, type,
  242. eppnt->p_offset - pgoff);
  243. }
  244. #define cpu_uses_ia32el() (local_cpu_data->family > 0x1f)
  245. static int __init check_elf32_binfmt(void)
  246. {
  247. if (cpu_uses_ia32el()) {
  248. printk("Please use IA-32 EL for executing IA-32 binaries\n");
  249. return unregister_binfmt(&elf_format);
  250. }
  251. return 0;
  252. }
  253. module_init(check_elf32_binfmt)