Merge git://git.kernel.org/pub/scm/linux/kernel/git/lethal/sh-2.6.24
[linux-2.6] / arch / x86 / kernel / sysenter_32.c
1 /*
2  * (C) Copyright 2002 Linus Torvalds
3  * Portions based on the vdso-randomization code from exec-shield:
4  * Copyright(C) 2005-2006, Red Hat, Inc., Ingo Molnar
5  *
6  * This file contains the needed initializations to support sysenter.
7  */
8
9 #include <linux/init.h>
10 #include <linux/smp.h>
11 #include <linux/thread_info.h>
12 #include <linux/sched.h>
13 #include <linux/gfp.h>
14 #include <linux/string.h>
15 #include <linux/elf.h>
16 #include <linux/mm.h>
17 #include <linux/err.h>
18 #include <linux/module.h>
19
20 #include <asm/cpufeature.h>
21 #include <asm/msr.h>
22 #include <asm/pgtable.h>
23 #include <asm/unistd.h>
24 #include <asm/elf.h>
25 #include <asm/tlbflush.h>
26
27 enum {
28         VDSO_DISABLED = 0,
29         VDSO_ENABLED = 1,
30         VDSO_COMPAT = 2,
31 };
32
33 #ifdef CONFIG_COMPAT_VDSO
34 #define VDSO_DEFAULT    VDSO_COMPAT
35 #else
36 #define VDSO_DEFAULT    VDSO_ENABLED
37 #endif
38
39 /*
40  * Should the kernel map a VDSO page into processes and pass its
41  * address down to glibc upon exec()?
42  */
43 unsigned int __read_mostly vdso_enabled = VDSO_DEFAULT;
44
45 EXPORT_SYMBOL_GPL(vdso_enabled);
46
47 static int __init vdso_setup(char *s)
48 {
49         vdso_enabled = simple_strtoul(s, NULL, 0);
50
51         return 1;
52 }
53
54 __setup("vdso=", vdso_setup);
55
56 extern asmlinkage void sysenter_entry(void);
57
58 static __init void reloc_symtab(Elf32_Ehdr *ehdr,
59                                 unsigned offset, unsigned size)
60 {
61         Elf32_Sym *sym = (void *)ehdr + offset;
62         unsigned nsym = size / sizeof(*sym);
63         unsigned i;
64
65         for(i = 0; i < nsym; i++, sym++) {
66                 if (sym->st_shndx == SHN_UNDEF ||
67                     sym->st_shndx == SHN_ABS)
68                         continue;  /* skip */
69
70                 if (sym->st_shndx > SHN_LORESERVE) {
71                         printk(KERN_INFO "VDSO: unexpected st_shndx %x\n",
72                                sym->st_shndx);
73                         continue;
74                 }
75
76                 switch(ELF_ST_TYPE(sym->st_info)) {
77                 case STT_OBJECT:
78                 case STT_FUNC:
79                 case STT_SECTION:
80                 case STT_FILE:
81                         sym->st_value += VDSO_HIGH_BASE;
82                 }
83         }
84 }
85
86 static __init void reloc_dyn(Elf32_Ehdr *ehdr, unsigned offset)
87 {
88         Elf32_Dyn *dyn = (void *)ehdr + offset;
89
90         for(; dyn->d_tag != DT_NULL; dyn++)
91                 switch(dyn->d_tag) {
92                 case DT_PLTGOT:
93                 case DT_HASH:
94                 case DT_STRTAB:
95                 case DT_SYMTAB:
96                 case DT_RELA:
97                 case DT_INIT:
98                 case DT_FINI:
99                 case DT_REL:
100                 case DT_DEBUG:
101                 case DT_JMPREL:
102                 case DT_VERSYM:
103                 case DT_VERDEF:
104                 case DT_VERNEED:
105                 case DT_ADDRRNGLO ... DT_ADDRRNGHI:
106                         /* definitely pointers needing relocation */
107                         dyn->d_un.d_ptr += VDSO_HIGH_BASE;
108                         break;
109
110                 case DT_ENCODING ... OLD_DT_LOOS-1:
111                 case DT_LOOS ... DT_HIOS-1:
112                         /* Tags above DT_ENCODING are pointers if
113                            they're even */
114                         if (dyn->d_tag >= DT_ENCODING &&
115                             (dyn->d_tag & 1) == 0)
116                                 dyn->d_un.d_ptr += VDSO_HIGH_BASE;
117                         break;
118
119                 case DT_VERDEFNUM:
120                 case DT_VERNEEDNUM:
121                 case DT_FLAGS_1:
122                 case DT_RELACOUNT:
123                 case DT_RELCOUNT:
124                 case DT_VALRNGLO ... DT_VALRNGHI:
125                         /* definitely not pointers */
126                         break;
127
128                 case OLD_DT_LOOS ... DT_LOOS-1:
129                 case DT_HIOS ... DT_VALRNGLO-1:
130                 default:
131                         if (dyn->d_tag > DT_ENCODING)
132                                 printk(KERN_INFO "VDSO: unexpected DT_tag %x\n",
133                                        dyn->d_tag);
134                         break;
135                 }
136 }
137
138 static __init void relocate_vdso(Elf32_Ehdr *ehdr)
139 {
140         Elf32_Phdr *phdr;
141         Elf32_Shdr *shdr;
142         int i;
143
144         BUG_ON(memcmp(ehdr->e_ident, ELFMAG, 4) != 0 ||
145                !elf_check_arch(ehdr) ||
146                ehdr->e_type != ET_DYN);
147
148         ehdr->e_entry += VDSO_HIGH_BASE;
149
150         /* rebase phdrs */
151         phdr = (void *)ehdr + ehdr->e_phoff;
152         for (i = 0; i < ehdr->e_phnum; i++) {
153                 phdr[i].p_vaddr += VDSO_HIGH_BASE;
154
155                 /* relocate dynamic stuff */
156                 if (phdr[i].p_type == PT_DYNAMIC)
157                         reloc_dyn(ehdr, phdr[i].p_offset);
158         }
159
160         /* rebase sections */
161         shdr = (void *)ehdr + ehdr->e_shoff;
162         for(i = 0; i < ehdr->e_shnum; i++) {
163                 if (!(shdr[i].sh_flags & SHF_ALLOC))
164                         continue;
165
166                 shdr[i].sh_addr += VDSO_HIGH_BASE;
167
168                 if (shdr[i].sh_type == SHT_SYMTAB ||
169                     shdr[i].sh_type == SHT_DYNSYM)
170                         reloc_symtab(ehdr, shdr[i].sh_offset,
171                                      shdr[i].sh_size);
172         }
173 }
174
175 void enable_sep_cpu(void)
176 {
177         int cpu = get_cpu();
178         struct tss_struct *tss = &per_cpu(init_tss, cpu);
179
180         if (!boot_cpu_has(X86_FEATURE_SEP)) {
181                 put_cpu();
182                 return;
183         }
184
185         tss->x86_tss.ss1 = __KERNEL_CS;
186         tss->x86_tss.esp1 = sizeof(struct tss_struct) + (unsigned long) tss;
187         wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
188         wrmsr(MSR_IA32_SYSENTER_ESP, tss->x86_tss.esp1, 0);
189         wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long) sysenter_entry, 0);
190         put_cpu();      
191 }
192
193 static struct vm_area_struct gate_vma;
194
195 static int __init gate_vma_init(void)
196 {
197         gate_vma.vm_mm = NULL;
198         gate_vma.vm_start = FIXADDR_USER_START;
199         gate_vma.vm_end = FIXADDR_USER_END;
200         gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC;
201         gate_vma.vm_page_prot = __P101;
202         /*
203          * Make sure the vDSO gets into every core dump.
204          * Dumping its contents makes post-mortem fully interpretable later
205          * without matching up the same kernel and hardware config to see
206          * what PC values meant.
207          */
208         gate_vma.vm_flags |= VM_ALWAYSDUMP;
209         return 0;
210 }
211
212 /*
213  * These symbols are defined by vsyscall.o to mark the bounds
214  * of the ELF DSO images included therein.
215  */
216 extern const char vsyscall_int80_start, vsyscall_int80_end;
217 extern const char vsyscall_sysenter_start, vsyscall_sysenter_end;
218 static struct page *syscall_pages[1];
219
220 static void map_compat_vdso(int map)
221 {
222         static int vdso_mapped;
223
224         if (map == vdso_mapped)
225                 return;
226
227         vdso_mapped = map;
228
229         __set_fixmap(FIX_VDSO, page_to_pfn(syscall_pages[0]) << PAGE_SHIFT,
230                      map ? PAGE_READONLY_EXEC : PAGE_NONE);
231
232         /* flush stray tlbs */
233         flush_tlb_all();
234 }
235
236 int __init sysenter_setup(void)
237 {
238         void *syscall_page = (void *)get_zeroed_page(GFP_ATOMIC);
239         const void *vsyscall;
240         size_t vsyscall_len;
241
242         syscall_pages[0] = virt_to_page(syscall_page);
243
244         gate_vma_init();
245
246         printk("Compat vDSO mapped to %08lx.\n", __fix_to_virt(FIX_VDSO));
247
248         if (!boot_cpu_has(X86_FEATURE_SEP)) {
249                 vsyscall = &vsyscall_int80_start;
250                 vsyscall_len = &vsyscall_int80_end - &vsyscall_int80_start;
251         } else {
252                 vsyscall = &vsyscall_sysenter_start;
253                 vsyscall_len = &vsyscall_sysenter_end - &vsyscall_sysenter_start;
254         }
255
256         memcpy(syscall_page, vsyscall, vsyscall_len);
257         relocate_vdso(syscall_page);
258
259         return 0;
260 }
261
262 /* Defined in vsyscall-sysenter.S */
263 extern void SYSENTER_RETURN;
264
265 /* Setup a VMA at program startup for the vsyscall page */
266 int arch_setup_additional_pages(struct linux_binprm *bprm, int exstack)
267 {
268         struct mm_struct *mm = current->mm;
269         unsigned long addr;
270         int ret = 0;
271         bool compat;
272
273         down_write(&mm->mmap_sem);
274
275         /* Test compat mode once here, in case someone
276            changes it via sysctl */
277         compat = (vdso_enabled == VDSO_COMPAT);
278
279         map_compat_vdso(compat);
280
281         if (compat)
282                 addr = VDSO_HIGH_BASE;
283         else {
284                 addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
285                 if (IS_ERR_VALUE(addr)) {
286                         ret = addr;
287                         goto up_fail;
288                 }
289
290                 /*
291                  * MAYWRITE to allow gdb to COW and set breakpoints
292                  *
293                  * Make sure the vDSO gets into every core dump.
294                  * Dumping its contents makes post-mortem fully
295                  * interpretable later without matching up the same
296                  * kernel and hardware config to see what PC values
297                  * meant.
298                  */
299                 ret = install_special_mapping(mm, addr, PAGE_SIZE,
300                                               VM_READ|VM_EXEC|
301                                               VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC|
302                                               VM_ALWAYSDUMP,
303                                               syscall_pages);
304
305                 if (ret)
306                         goto up_fail;
307         }
308
309         current->mm->context.vdso = (void *)addr;
310         current_thread_info()->sysenter_return =
311                 (void *)VDSO_SYM(&SYSENTER_RETURN);
312
313   up_fail:
314         up_write(&mm->mmap_sem);
315
316         return ret;
317 }
318
319 const char *arch_vma_name(struct vm_area_struct *vma)
320 {
321         if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
322                 return "[vdso]";
323         return NULL;
324 }
325
326 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
327 {
328         struct mm_struct *mm = tsk->mm;
329
330         /* Check to see if this task was created in compat vdso mode */
331         if (mm && mm->context.vdso == (void *)VDSO_HIGH_BASE)
332                 return &gate_vma;
333         return NULL;
334 }
335
336 int in_gate_area(struct task_struct *task, unsigned long addr)
337 {
338         const struct vm_area_struct *vma = get_gate_vma(task);
339
340         return vma && addr >= vma->vm_start && addr < vma->vm_end;
341 }
342
343 int in_gate_area_no_task(unsigned long addr)
344 {
345         return 0;
346 }