Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/i386/kernel/sysenter.c |
| 3 | * |
| 4 | * (C) Copyright 2002 Linus Torvalds |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 5 | * Portions based on the vdso-randomization code from exec-shield: |
| 6 | * Copyright(C) 2005-2006, Red Hat, Inc., Ingo Molnar |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * This file contains the needed initializations to support sysenter. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/smp.h> |
| 13 | #include <linux/thread_info.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/gfp.h> |
| 16 | #include <linux/string.h> |
| 17 | #include <linux/elf.h> |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 18 | #include <linux/mm.h> |
| 19 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | #include <asm/cpufeature.h> |
| 22 | #include <asm/msr.h> |
| 23 | #include <asm/pgtable.h> |
| 24 | #include <asm/unistd.h> |
| 25 | |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 26 | /* |
| 27 | * Should the kernel map a VDSO page into processes and pass its |
| 28 | * address down to glibc upon exec()? |
| 29 | */ |
Andi Kleen | 3bbf547 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 30 | #ifdef CONFIG_PARAVIRT |
| 31 | unsigned int __read_mostly vdso_enabled = 0; |
| 32 | #else |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 33 | unsigned int __read_mostly vdso_enabled = 1; |
Andi Kleen | 3bbf547 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 34 | #endif |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 35 | |
| 36 | EXPORT_SYMBOL_GPL(vdso_enabled); |
| 37 | |
| 38 | static int __init vdso_setup(char *s) |
| 39 | { |
| 40 | vdso_enabled = simple_strtoul(s, NULL, 0); |
| 41 | |
| 42 | return 1; |
| 43 | } |
| 44 | |
| 45 | __setup("vdso=", vdso_setup); |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | extern asmlinkage void sysenter_entry(void); |
| 48 | |
Li Shaohua | 6fe940d | 2005-06-25 14:54:53 -0700 | [diff] [blame] | 49 | void enable_sep_cpu(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
| 51 | int cpu = get_cpu(); |
| 52 | struct tss_struct *tss = &per_cpu(init_tss, cpu); |
| 53 | |
Li Shaohua | 6fe940d | 2005-06-25 14:54:53 -0700 | [diff] [blame] | 54 | if (!boot_cpu_has(X86_FEATURE_SEP)) { |
| 55 | put_cpu(); |
| 56 | return; |
| 57 | } |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | tss->ss1 = __KERNEL_CS; |
| 60 | tss->esp1 = sizeof(struct tss_struct) + (unsigned long) tss; |
| 61 | wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0); |
| 62 | wrmsr(MSR_IA32_SYSENTER_ESP, tss->esp1, 0); |
| 63 | wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long) sysenter_entry, 0); |
| 64 | put_cpu(); |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * These symbols are defined by vsyscall.o to mark the bounds |
| 69 | * of the ELF DSO images included therein. |
| 70 | */ |
| 71 | extern const char vsyscall_int80_start, vsyscall_int80_end; |
| 72 | extern const char vsyscall_sysenter_start, vsyscall_sysenter_end; |
Roland McGrath | 7d91d53 | 2007-02-08 14:20:42 -0800 | [diff] [blame] | 73 | static struct page *syscall_pages[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Jeremy Fitzhardinge | a6c4e07 | 2007-05-02 19:27:12 +0200 | [diff] [blame^] | 75 | int __init sysenter_setup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
Roland McGrath | 7d91d53 | 2007-02-08 14:20:42 -0800 | [diff] [blame] | 77 | void *syscall_page = (void *)get_zeroed_page(GFP_ATOMIC); |
| 78 | syscall_pages[0] = virt_to_page(syscall_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 80 | #ifdef CONFIG_COMPAT_VDSO |
Jan Beulich | 22c5ace | 2007-02-13 13:26:26 +0100 | [diff] [blame] | 81 | __set_fixmap(FIX_VDSO, __pa(syscall_page), PAGE_READONLY_EXEC); |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 82 | printk("Compat vDSO mapped to %08lx.\n", __fix_to_virt(FIX_VDSO)); |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 83 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | if (!boot_cpu_has(X86_FEATURE_SEP)) { |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 86 | memcpy(syscall_page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | &vsyscall_int80_start, |
| 88 | &vsyscall_int80_end - &vsyscall_int80_start); |
| 89 | return 0; |
| 90 | } |
| 91 | |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 92 | memcpy(syscall_page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | &vsyscall_sysenter_start, |
| 94 | &vsyscall_sysenter_end - &vsyscall_sysenter_start); |
| 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | return 0; |
| 97 | } |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 98 | |
Roland McGrath | a1f3bb9 | 2007-01-26 00:56:46 -0800 | [diff] [blame] | 99 | #ifndef CONFIG_COMPAT_VDSO |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 100 | /* Defined in vsyscall-sysenter.S */ |
| 101 | extern void SYSENTER_RETURN; |
| 102 | |
| 103 | /* Setup a VMA at program startup for the vsyscall page */ |
| 104 | int arch_setup_additional_pages(struct linux_binprm *bprm, int exstack) |
| 105 | { |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 106 | struct mm_struct *mm = current->mm; |
| 107 | unsigned long addr; |
| 108 | int ret; |
| 109 | |
| 110 | down_write(&mm->mmap_sem); |
| 111 | addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0); |
| 112 | if (IS_ERR_VALUE(addr)) { |
| 113 | ret = addr; |
| 114 | goto up_fail; |
| 115 | } |
| 116 | |
Roland McGrath | f47aef5 | 2007-01-26 00:56:49 -0800 | [diff] [blame] | 117 | /* |
Roland McGrath | 7d91d53 | 2007-02-08 14:20:42 -0800 | [diff] [blame] | 118 | * MAYWRITE to allow gdb to COW and set breakpoints |
| 119 | * |
Roland McGrath | f47aef5 | 2007-01-26 00:56:49 -0800 | [diff] [blame] | 120 | * Make sure the vDSO gets into every core dump. |
| 121 | * Dumping its contents makes post-mortem fully interpretable later |
| 122 | * without matching up the same kernel and hardware config to see |
| 123 | * what PC values meant. |
| 124 | */ |
Roland McGrath | 7d91d53 | 2007-02-08 14:20:42 -0800 | [diff] [blame] | 125 | ret = install_special_mapping(mm, addr, PAGE_SIZE, |
| 126 | VM_READ|VM_EXEC| |
| 127 | VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC| |
| 128 | VM_ALWAYSDUMP, |
| 129 | syscall_pages); |
| 130 | if (ret) |
pageexec@freemail.hu | 79bc79b | 2006-06-28 20:44:16 +0200 | [diff] [blame] | 131 | goto up_fail; |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 132 | |
| 133 | current->mm->context.vdso = (void *)addr; |
| 134 | current_thread_info()->sysenter_return = |
| 135 | (void *)VDSO_SYM(&SYSENTER_RETURN); |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 136 | up_fail: |
| 137 | up_write(&mm->mmap_sem); |
| 138 | return ret; |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | const char *arch_vma_name(struct vm_area_struct *vma) |
| 142 | { |
| 143 | if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso) |
| 144 | return "[vdso]"; |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | struct vm_area_struct *get_gate_vma(struct task_struct *tsk) |
| 149 | { |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | int in_gate_area(struct task_struct *task, unsigned long addr) |
| 154 | { |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | int in_gate_area_no_task(unsigned long addr) |
| 159 | { |
| 160 | return 0; |
| 161 | } |
Roland McGrath | a1f3bb9 | 2007-01-26 00:56:46 -0800 | [diff] [blame] | 162 | #endif |