binfmt_elf32.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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_zalloc(vm_area_cachep, GFP_KERNEL);
  75. if (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_zalloc(vm_area_cachep, GFP_KERNEL);
  99. if (vma) {
  100. vma->vm_mm = current->mm;
  101. vma->vm_start = IA32_GATE_OFFSET;
  102. vma->vm_end = vma->vm_start + PAGE_SIZE;
  103. vma->vm_page_prot = PAGE_COPY_EXEC;
  104. vma->vm_flags = VM_READ | VM_MAYREAD | VM_EXEC
  105. | VM_MAYEXEC | VM_RESERVED;
  106. vma->vm_ops = &ia32_gate_page_vm_ops;
  107. down_write(&current->mm->mmap_sem);
  108. {
  109. if (insert_vm_struct(current->mm, vma)) {
  110. kmem_cache_free(vm_area_cachep, vma);
  111. up_write(&current->mm->mmap_sem);
  112. BUG();
  113. }
  114. }
  115. up_write(&current->mm->mmap_sem);
  116. }
  117. /*
  118. * Install LDT as anonymous memory. This gives us all-zero segment descriptors
  119. * until a task modifies them via modify_ldt().
  120. */
  121. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  122. if (vma) {
  123. vma->vm_mm = current->mm;
  124. vma->vm_start = IA32_LDT_OFFSET;
  125. vma->vm_end = vma->vm_start + PAGE_ALIGN(IA32_LDT_ENTRIES*IA32_LDT_ENTRY_SIZE);
  126. vma->vm_page_prot = PAGE_SHARED;
  127. vma->vm_flags = VM_READ|VM_WRITE|VM_MAYREAD|VM_MAYWRITE;
  128. down_write(&current->mm->mmap_sem);
  129. {
  130. if (insert_vm_struct(current->mm, vma)) {
  131. kmem_cache_free(vm_area_cachep, vma);
  132. up_write(&current->mm->mmap_sem);
  133. BUG();
  134. }
  135. }
  136. up_write(&current->mm->mmap_sem);
  137. }
  138. ia64_psr(regs)->ac = 0; /* turn off alignment checking */
  139. regs->loadrs = 0;
  140. /*
  141. * According to the ABI %edx points to an `atexit' handler. Since we don't have
  142. * one we'll set it to 0 and initialize all the other registers just to make
  143. * things more deterministic, ala the i386 implementation.
  144. */
  145. regs->r8 = 0; /* %eax */
  146. regs->r11 = 0; /* %ebx */
  147. regs->r9 = 0; /* %ecx */
  148. regs->r10 = 0; /* %edx */
  149. regs->r13 = 0; /* %ebp */
  150. regs->r14 = 0; /* %esi */
  151. regs->r15 = 0; /* %edi */
  152. current->thread.eflag = IA32_EFLAG;
  153. current->thread.fsr = IA32_FSR_DEFAULT;
  154. current->thread.fcr = IA32_FCR_DEFAULT;
  155. current->thread.fir = 0;
  156. current->thread.fdr = 0;
  157. /*
  158. * Setup GDTD. Note: GDTD is the descrambled version of the pseudo-descriptor
  159. * format defined by Figure 3-11 "Pseudo-Descriptor Format" in the IA-32
  160. * architecture manual. Also note that the only fields that are not ignored are
  161. * `base', `limit', 'G', `P' (must be 1) and `S' (must be 0).
  162. */
  163. regs->r31 = IA32_SEG_UNSCRAMBLE(IA32_SEG_DESCRIPTOR(IA32_GDT_OFFSET, IA32_PAGE_SIZE - 1,
  164. 0, 0, 0, 1, 0, 0, 0));
  165. /* Setup the segment selectors */
  166. regs->r16 = (__USER_DS << 16) | __USER_DS; /* ES == DS, GS, FS are zero */
  167. regs->r17 = (__USER_DS << 16) | __USER_CS; /* SS, CS; ia32_load_state() sets TSS and LDT */
  168. ia32_load_segment_descriptors(current);
  169. ia32_load_state(current);
  170. }
  171. int
  172. ia32_setup_arg_pages (struct linux_binprm *bprm, int executable_stack)
  173. {
  174. unsigned long stack_base;
  175. struct vm_area_struct *mpnt;
  176. struct mm_struct *mm = current->mm;
  177. int i, ret;
  178. stack_base = IA32_STACK_TOP - MAX_ARG_PAGES*PAGE_SIZE;
  179. mm->arg_start = bprm->p + stack_base;
  180. bprm->p += stack_base;
  181. if (bprm->loader)
  182. bprm->loader += stack_base;
  183. bprm->exec += stack_base;
  184. mpnt = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  185. if (!mpnt)
  186. return -ENOMEM;
  187. down_write(&current->mm->mmap_sem);
  188. {
  189. mpnt->vm_mm = current->mm;
  190. mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
  191. mpnt->vm_end = IA32_STACK_TOP;
  192. if (executable_stack == EXSTACK_ENABLE_X)
  193. mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
  194. else if (executable_stack == EXSTACK_DISABLE_X)
  195. mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
  196. else
  197. mpnt->vm_flags = VM_STACK_FLAGS;
  198. mpnt->vm_page_prot = (mpnt->vm_flags & VM_EXEC)?
  199. PAGE_COPY_EXEC: PAGE_COPY;
  200. if ((ret = insert_vm_struct(current->mm, mpnt))) {
  201. up_write(&current->mm->mmap_sem);
  202. kmem_cache_free(vm_area_cachep, mpnt);
  203. return ret;
  204. }
  205. current->mm->stack_vm = current->mm->total_vm = vma_pages(mpnt);
  206. }
  207. for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
  208. struct page *page = bprm->page[i];
  209. if (page) {
  210. bprm->page[i] = NULL;
  211. install_arg_page(mpnt, page, stack_base);
  212. }
  213. stack_base += PAGE_SIZE;
  214. }
  215. up_write(&current->mm->mmap_sem);
  216. /* Can't do it in ia64_elf32_init(). Needs to be done before calls to
  217. elf32_map() */
  218. current->thread.ppl = ia32_init_pp_list();
  219. return 0;
  220. }
  221. static void
  222. elf32_set_personality (void)
  223. {
  224. set_personality(PER_LINUX32);
  225. current->thread.map_base = IA32_PAGE_OFFSET/3;
  226. }
  227. static unsigned long
  228. elf32_map (struct file *filep, unsigned long addr, struct elf_phdr *eppnt, int prot, int type)
  229. {
  230. unsigned long pgoff = (eppnt->p_vaddr) & ~IA32_PAGE_MASK;
  231. return ia32_do_mmap(filep, (addr & IA32_PAGE_MASK), eppnt->p_filesz + pgoff, prot, type,
  232. eppnt->p_offset - pgoff);
  233. }
  234. #define cpu_uses_ia32el() (local_cpu_data->family > 0x1f)
  235. static int __init check_elf32_binfmt(void)
  236. {
  237. if (cpu_uses_ia32el()) {
  238. printk("Please use IA-32 EL for executing IA-32 binaries\n");
  239. return unregister_binfmt(&elf_format);
  240. }
  241. return 0;
  242. }
  243. module_init(check_elf32_binfmt)