binfmt_elf32.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:6k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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 <asm/param.h>
  15. #include <asm/signal.h>
  16. #include <asm/ia32.h>
  17. #define CONFIG_BINFMT_ELF32
  18. /* Override some function names */
  19. #undef start_thread
  20. #define start_thread ia32_start_thread
  21. #define elf_format elf32_format
  22. #define init_elf_binfmt init_elf32_binfmt
  23. #define exit_elf_binfmt exit_elf32_binfmt
  24. #undef CONFIG_BINFMT_ELF
  25. #ifdef CONFIG_BINFMT_ELF32
  26. # define CONFIG_BINFMT_ELF CONFIG_BINFMT_ELF32
  27. #endif
  28. #undef CONFIG_BINFMT_ELF_MODULE
  29. #ifdef CONFIG_BINFMT_ELF32_MODULE
  30. # define CONFIG_BINFMT_ELF_MODULE CONFIG_BINFMT_ELF32_MODULE
  31. #endif
  32. #undef CLOCKS_PER_SEC
  33. #define CLOCKS_PER_SEC IA32_CLOCKS_PER_SEC
  34. extern void ia64_elf32_init (struct pt_regs *regs);
  35. extern void put_dirty_page (struct task_struct * tsk, struct page *page, unsigned long address);
  36. static void elf32_set_personality (void);
  37. #define ELF_PLAT_INIT(_r) ia64_elf32_init(_r)
  38. #define setup_arg_pages(bprm) ia32_setup_arg_pages(bprm)
  39. #define elf_map elf32_map
  40. #undef SET_PERSONALITY
  41. #define SET_PERSONALITY(ex, ibcs2) elf32_set_personality()
  42. /* Ugly but avoids duplication */
  43. #include "../../../fs/binfmt_elf.c"
  44. extern struct page *ia32_shared_page[];
  45. extern unsigned long *ia32_gdt;
  46. struct page *
  47. ia32_install_shared_page (struct vm_area_struct *vma, unsigned long address, int no_share)
  48. {
  49. struct page *pg = ia32_shared_page[(address - vma->vm_start)/PAGE_SIZE];
  50. get_page(pg);
  51. return pg;
  52. }
  53. static struct vm_operations_struct ia32_shared_page_vm_ops = {
  54. nopage: ia32_install_shared_page
  55. };
  56. void
  57. ia64_elf32_init (struct pt_regs *regs)
  58. {
  59. struct vm_area_struct *vma;
  60. /*
  61.  * Map GDT and TSS below 4GB, where the processor can find them.  We need to map
  62.  * it with privilege level 3 because the IVE uses non-privileged accesses to these
  63.  * tables.  IA-32 segmentation is used to protect against IA-32 accesses to them.
  64.  */
  65. vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
  66. if (vma) {
  67. vma->vm_mm = current->mm;
  68. vma->vm_start = IA32_GDT_OFFSET;
  69. vma->vm_end = vma->vm_start + max(PAGE_SIZE, 2*IA32_PAGE_SIZE);
  70. vma->vm_page_prot = PAGE_SHARED;
  71. vma->vm_flags = VM_READ|VM_MAYREAD;
  72. vma->vm_ops = &ia32_shared_page_vm_ops;
  73. vma->vm_pgoff = 0;
  74. vma->vm_file = NULL;
  75. vma->vm_private_data = NULL;
  76. down_write(&current->mm->mmap_sem);
  77. {
  78. insert_vm_struct(current->mm, vma);
  79. }
  80. up_write(&current->mm->mmap_sem);
  81. }
  82. /*
  83.  * Install LDT as anonymous memory.  This gives us all-zero segment descriptors
  84.  * until a task modifies them via modify_ldt().
  85.  */
  86. vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
  87. if (vma) {
  88. vma->vm_mm = current->mm;
  89. vma->vm_start = IA32_LDT_OFFSET;
  90. vma->vm_end = vma->vm_start + PAGE_ALIGN(IA32_LDT_ENTRIES*IA32_LDT_ENTRY_SIZE);
  91. vma->vm_page_prot = PAGE_SHARED;
  92. vma->vm_flags = VM_READ|VM_WRITE|VM_MAYREAD|VM_MAYWRITE;
  93. vma->vm_ops = NULL;
  94. vma->vm_pgoff = 0;
  95. vma->vm_file = NULL;
  96. vma->vm_private_data = NULL;
  97. down_write(&current->mm->mmap_sem);
  98. {
  99. insert_vm_struct(current->mm, vma);
  100. }
  101. up_write(&current->mm->mmap_sem);
  102. }
  103. ia64_psr(regs)->ac = 0; /* turn off alignment checking */
  104. regs->loadrs = 0;
  105. /*
  106.  *  According to the ABI %edx points to an `atexit' handler.  Since we don't have
  107.  *  one we'll set it to 0 and initialize all the other registers just to make
  108.  *  things more deterministic, ala the i386 implementation.
  109.  */
  110. regs->r8 = 0; /* %eax */
  111. regs->r11 = 0; /* %ebx */
  112. regs->r9 = 0; /* %ecx */
  113. regs->r10 = 0; /* %edx */
  114. regs->r13 = 0; /* %ebp */
  115. regs->r14 = 0; /* %esi */
  116. regs->r15 = 0; /* %edi */
  117. current->thread.eflag = IA32_EFLAG;
  118. current->thread.fsr = IA32_FSR_DEFAULT;
  119. current->thread.fcr = IA32_FCR_DEFAULT;
  120. current->thread.fir = 0;
  121. current->thread.fdr = 0;
  122. /*
  123.  * Setup GDTD.  Note: GDTD is the descrambled version of the pseudo-descriptor
  124.  * format defined by Figure 3-11 "Pseudo-Descriptor Format" in the IA-32
  125.  * architecture manual. Also note that the only fields that are not ignored are
  126.  * `base', `limit', 'G', `P' (must be 1) and `S' (must be 0).
  127.  */
  128. regs->r31 = IA32_SEG_UNSCRAMBLE(IA32_SEG_DESCRIPTOR(IA32_GDT_OFFSET, IA32_PAGE_SIZE - 1,
  129.     0, 0, 0, 1, 0, 0, 0));
  130. /* Setup the segment selectors */
  131. regs->r16 = (__USER_DS << 16) | __USER_DS; /* ES == DS, GS, FS are zero */
  132. regs->r17 = (__USER_DS << 16) | __USER_CS; /* SS, CS; ia32_load_state() sets TSS and LDT */
  133. ia32_load_segment_descriptors(current);
  134. ia32_load_state(current);
  135. }
  136. int
  137. ia32_setup_arg_pages (struct linux_binprm *bprm)
  138. {
  139. unsigned long stack_base;
  140. struct vm_area_struct *mpnt;
  141. int i;
  142. stack_base = IA32_STACK_TOP - MAX_ARG_PAGES*PAGE_SIZE;
  143. bprm->p += stack_base;
  144. if (bprm->loader)
  145. bprm->loader += stack_base;
  146. bprm->exec += stack_base;
  147. mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
  148. if (!mpnt)
  149. return -ENOMEM;
  150. down_write(&current->mm->mmap_sem);
  151. {
  152. mpnt->vm_mm = current->mm;
  153. mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
  154. mpnt->vm_end = IA32_STACK_TOP;
  155. mpnt->vm_page_prot = PAGE_COPY;
  156. mpnt->vm_flags = VM_STACK_FLAGS;
  157. mpnt->vm_ops = NULL;
  158. mpnt->vm_pgoff = 0;
  159. mpnt->vm_file = NULL;
  160. mpnt->vm_private_data = 0;
  161. insert_vm_struct(current->mm, mpnt);
  162. current->mm->total_vm = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
  163. }
  164. for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
  165. struct page *page = bprm->page[i];
  166. if (page) {
  167. bprm->page[i] = NULL;
  168. put_dirty_page(current, page, stack_base);
  169. }
  170. stack_base += PAGE_SIZE;
  171. }
  172. up_write(&current->mm->mmap_sem);
  173. return 0;
  174. }
  175. static void
  176. elf32_set_personality (void)
  177. {
  178. set_personality(PER_LINUX32);
  179. current->thread.map_base  = IA32_PAGE_OFFSET/3;
  180. current->thread.task_size = IA32_PAGE_OFFSET; /* use what Linux/x86 uses... */
  181. current->thread.flags |= IA64_THREAD_XSTACK; /* data must be executable */
  182. set_fs(USER_DS); /* set addr limit for new TASK_SIZE */
  183. }
  184. static unsigned long
  185. elf32_map (struct file *filep, unsigned long addr, struct elf_phdr *eppnt, int prot, int type)
  186. {
  187. unsigned long pgoff = (eppnt->p_vaddr) & ~IA32_PAGE_MASK;
  188. return ia32_do_mmap(filep, (addr & IA32_PAGE_MASK), eppnt->p_filesz + pgoff, prot, type,
  189.     eppnt->p_offset - pgoff);
  190. }