binfmt_elf32.c 8.0 KB

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