binfmt_elf32.c 6.7 KB

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