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>
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
12 #include <linux/config.h>
14 #include <linux/types.h>
16 #include <linux/security.h>
18 #include <asm/param.h>
19 #include <asm/signal.h>
22 #include "elfcore32.h"
24 /* Override some function names */
26 #define start_thread ia32_start_thread
27 #define elf_format elf32_format
28 #define init_elf_binfmt init_elf32_binfmt
29 #define exit_elf_binfmt exit_elf32_binfmt
32 #define CLOCKS_PER_SEC IA32_CLOCKS_PER_SEC
34 extern void ia64_elf32_init (struct pt_regs *regs);
36 static void elf32_set_personality (void);
38 #define setup_arg_pages(bprm,exec) ia32_setup_arg_pages(bprm,exec)
39 #define elf_map elf32_map
41 #undef SET_PERSONALITY
42 #define SET_PERSONALITY(ex, ibcs2) elf32_set_personality()
44 /* Ugly but avoids duplication */
45 #include "../../../fs/binfmt_elf.c"
47 extern struct page *ia32_shared_page[];
48 extern unsigned long *ia32_gdt;
51 ia32_install_shared_page (struct vm_area_struct *vma, unsigned long address, int *type)
53 struct page *pg = ia32_shared_page[smp_processor_id()];
56 *type = VM_FAULT_MINOR;
60 static struct vm_operations_struct ia32_shared_page_vm_ops = {
61 .nopage = ia32_install_shared_page
65 ia64_elf32_init (struct pt_regs *regs)
67 struct vm_area_struct *vma;
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.
74 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
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;
81 vma->vm_ops = &ia32_shared_page_vm_ops;
84 vma->vm_private_data = NULL;
85 down_write(¤t->mm->mmap_sem);
87 insert_vm_struct(current->mm, vma);
89 up_write(¤t->mm->mmap_sem);
93 * Install LDT as anonymous memory. This gives us all-zero segment descriptors
94 * until a task modifies them via modify_ldt().
96 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
98 vma->vm_mm = current->mm;
99 vma->vm_start = IA32_LDT_OFFSET;
100 vma->vm_end = vma->vm_start + PAGE_ALIGN(IA32_LDT_ENTRIES*IA32_LDT_ENTRY_SIZE);
101 vma->vm_page_prot = PAGE_SHARED;
102 vma->vm_flags = VM_READ|VM_WRITE|VM_MAYREAD|VM_MAYWRITE;
106 vma->vm_private_data = NULL;
107 down_write(¤t->mm->mmap_sem);
109 insert_vm_struct(current->mm, vma);
111 up_write(¤t->mm->mmap_sem);
114 ia64_psr(regs)->ac = 0; /* turn off alignment checking */
117 * According to the ABI %edx points to an `atexit' handler. Since we don't have
118 * one we'll set it to 0 and initialize all the other registers just to make
119 * things more deterministic, ala the i386 implementation.
121 regs->r8 = 0; /* %eax */
122 regs->r11 = 0; /* %ebx */
123 regs->r9 = 0; /* %ecx */
124 regs->r10 = 0; /* %edx */
125 regs->r13 = 0; /* %ebp */
126 regs->r14 = 0; /* %esi */
127 regs->r15 = 0; /* %edi */
129 current->thread.eflag = IA32_EFLAG;
130 current->thread.fsr = IA32_FSR_DEFAULT;
131 current->thread.fcr = IA32_FCR_DEFAULT;
132 current->thread.fir = 0;
133 current->thread.fdr = 0;
136 * Setup GDTD. Note: GDTD is the descrambled version of the pseudo-descriptor
137 * format defined by Figure 3-11 "Pseudo-Descriptor Format" in the IA-32
138 * architecture manual. Also note that the only fields that are not ignored are
139 * `base', `limit', 'G', `P' (must be 1) and `S' (must be 0).
141 regs->r31 = IA32_SEG_UNSCRAMBLE(IA32_SEG_DESCRIPTOR(IA32_GDT_OFFSET, IA32_PAGE_SIZE - 1,
142 0, 0, 0, 1, 0, 0, 0));
143 /* Setup the segment selectors */
144 regs->r16 = (__USER_DS << 16) | __USER_DS; /* ES == DS, GS, FS are zero */
145 regs->r17 = (__USER_DS << 16) | __USER_CS; /* SS, CS; ia32_load_state() sets TSS and LDT */
147 ia32_load_segment_descriptors(current);
148 ia32_load_state(current);
152 ia32_setup_arg_pages (struct linux_binprm *bprm, int executable_stack)
154 unsigned long stack_base;
155 struct vm_area_struct *mpnt;
156 struct mm_struct *mm = current->mm;
159 stack_base = IA32_STACK_TOP - MAX_ARG_PAGES*PAGE_SIZE;
160 mm->arg_start = bprm->p + stack_base;
162 bprm->p += stack_base;
164 bprm->loader += stack_base;
165 bprm->exec += stack_base;
167 mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
171 if (security_vm_enough_memory((IA32_STACK_TOP - (PAGE_MASK & (unsigned long) bprm->p))>>PAGE_SHIFT)) {
172 kmem_cache_free(vm_area_cachep, mpnt);
176 down_write(¤t->mm->mmap_sem);
178 mpnt->vm_mm = current->mm;
179 mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
180 mpnt->vm_end = IA32_STACK_TOP;
181 if (executable_stack == EXSTACK_ENABLE_X)
182 mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
183 else if (executable_stack == EXSTACK_DISABLE_X)
184 mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
186 mpnt->vm_flags = VM_STACK_FLAGS;
187 mpnt->vm_page_prot = (mpnt->vm_flags & VM_EXEC)?
188 PAGE_COPY_EXEC: PAGE_COPY;
191 mpnt->vm_file = NULL;
192 mpnt->vm_private_data = 0;
193 insert_vm_struct(current->mm, mpnt);
194 current->mm->total_vm = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
197 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
198 struct page *page = bprm->page[i];
200 bprm->page[i] = NULL;
201 put_dirty_page(current, page, stack_base, mpnt->vm_page_prot);
203 stack_base += PAGE_SIZE;
205 up_write(¤t->mm->mmap_sem);
211 elf32_set_personality (void)
213 set_personality(PER_LINUX32);
214 current->thread.map_base = IA32_PAGE_OFFSET/3;
215 current->thread.task_size = IA32_PAGE_OFFSET; /* use what Linux/x86 uses... */
216 current->thread.flags |= IA64_THREAD_XSTACK; /* data must be executable */
217 set_fs(USER_DS); /* set addr limit for new TASK_SIZE */
221 elf32_map (struct file *filep, unsigned long addr, struct elf_phdr *eppnt, int prot, int type)
223 unsigned long pgoff = (eppnt->p_vaddr) & ~IA32_PAGE_MASK;
225 return ia32_do_mmap(filep, (addr & IA32_PAGE_MASK), eppnt->p_filesz + pgoff, prot, type,
226 eppnt->p_offset - pgoff);