binfmt_elf32.c 8.0 KB

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