Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Copyright 2002,2003 Andi Kleen, SuSE Labs */ |
| 2 | |
| 3 | /* vsyscall handling for 32bit processes. Map a stub page into it |
| 4 | on demand because 32bit cannot reach the kernel's fixmaps */ |
| 5 | |
| 6 | #include <linux/mm.h> |
| 7 | #include <linux/string.h> |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/gfp.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/stringify.h> |
Andi Kleen | 1e01441 | 2005-04-16 15:24:55 -0700 | [diff] [blame] | 12 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <asm/proto.h> |
| 14 | #include <asm/tlbflush.h> |
| 15 | #include <asm/ia32_unistd.h> |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | extern unsigned char syscall32_syscall[], syscall32_syscall_end[]; |
| 18 | extern unsigned char syscall32_sysenter[], syscall32_sysenter_end[]; |
| 19 | extern int sysctl_vsyscall32; |
| 20 | |
| 21 | char *syscall32_page; |
| 22 | static int use_sysenter = -1; |
| 23 | |
Andi Kleen | 1e01441 | 2005-04-16 15:24:55 -0700 | [diff] [blame] | 24 | static struct page * |
| 25 | syscall32_nopage(struct vm_area_struct *vma, unsigned long adr, int *type) |
| 26 | { |
| 27 | struct page *p = virt_to_page(adr - vma->vm_start + syscall32_page); |
| 28 | get_page(p); |
| 29 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Andi Kleen | 1e01441 | 2005-04-16 15:24:55 -0700 | [diff] [blame] | 32 | /* Prevent VMA merging */ |
| 33 | static void syscall32_vma_close(struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
Andi Kleen | 1e01441 | 2005-04-16 15:24:55 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | static struct vm_operations_struct syscall32_vm_ops = { |
| 38 | .close = syscall32_vma_close, |
| 39 | .nopage = syscall32_nopage, |
| 40 | }; |
| 41 | |
| 42 | struct linux_binprm; |
| 43 | |
| 44 | /* Setup a VMA at program startup for the vsyscall page */ |
| 45 | int syscall32_setup_pages(struct linux_binprm *bprm, int exstack) |
| 46 | { |
| 47 | int npages = (VSYSCALL32_END - VSYSCALL32_BASE) >> PAGE_SHIFT; |
| 48 | struct vm_area_struct *vma; |
| 49 | struct mm_struct *mm = current->mm; |
Siddha, Suresh B | 9fb1759 | 2005-07-15 19:17:44 -0700 | [diff] [blame] | 50 | int ret; |
Andi Kleen | 1e01441 | 2005-04-16 15:24:55 -0700 | [diff] [blame] | 51 | |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 52 | vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); |
Andi Kleen | 1e01441 | 2005-04-16 15:24:55 -0700 | [diff] [blame] | 53 | if (!vma) |
| 54 | return -ENOMEM; |
Andi Kleen | 1e01441 | 2005-04-16 15:24:55 -0700 | [diff] [blame] | 55 | |
| 56 | memset(vma, 0, sizeof(struct vm_area_struct)); |
| 57 | /* Could randomize here */ |
| 58 | vma->vm_start = VSYSCALL32_BASE; |
| 59 | vma->vm_end = VSYSCALL32_END; |
| 60 | /* MAYWRITE to allow gdb to COW and set breakpoints */ |
Hugh Dickins | 2fd4ef8 | 2005-09-14 06:13:02 +0100 | [diff] [blame] | 61 | vma->vm_flags = VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC|VM_MAYWRITE; |
Roland McGrath | e03f0ca | 2007-01-26 00:56:50 -0800 | [diff] [blame] | 62 | /* |
| 63 | * Make sure the vDSO gets into every core dump. |
| 64 | * Dumping its contents makes post-mortem fully interpretable later |
| 65 | * without matching up the same kernel and hardware config to see |
| 66 | * what PC values meant. |
| 67 | */ |
| 68 | vma->vm_flags |= VM_ALWAYSDUMP; |
Andi Kleen | 1e01441 | 2005-04-16 15:24:55 -0700 | [diff] [blame] | 69 | vma->vm_flags |= mm->def_flags; |
| 70 | vma->vm_page_prot = protection_map[vma->vm_flags & 7]; |
| 71 | vma->vm_ops = &syscall32_vm_ops; |
| 72 | vma->vm_mm = mm; |
| 73 | |
| 74 | down_write(&mm->mmap_sem); |
Siddha, Suresh B | 9fb1759 | 2005-07-15 19:17:44 -0700 | [diff] [blame] | 75 | if ((ret = insert_vm_struct(mm, vma))) { |
| 76 | up_write(&mm->mmap_sem); |
| 77 | kmem_cache_free(vm_area_cachep, vma); |
| 78 | return ret; |
| 79 | } |
Andi Kleen | 1e01441 | 2005-04-16 15:24:55 -0700 | [diff] [blame] | 80 | mm->total_vm += npages; |
| 81 | up_write(&mm->mmap_sem); |
| 82 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Roland McGrath | c633090 | 2007-01-26 00:56:52 -0800 | [diff] [blame^] | 85 | const char *arch_vma_name(struct vm_area_struct *vma) |
| 86 | { |
| 87 | if (vma->vm_start == VSYSCALL32_BASE && |
| 88 | vma->vm_mm && vma->vm_mm->task_size == IA32_PAGE_OFFSET) |
| 89 | return "[vdso]"; |
| 90 | return NULL; |
| 91 | } |
| 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | static int __init init_syscall32(void) |
| 94 | { |
| 95 | syscall32_page = (void *)get_zeroed_page(GFP_KERNEL); |
| 96 | if (!syscall32_page) |
| 97 | panic("Cannot allocate syscall32 page"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | if (use_sysenter > 0) { |
| 99 | memcpy(syscall32_page, syscall32_sysenter, |
| 100 | syscall32_sysenter_end - syscall32_sysenter); |
| 101 | } else { |
| 102 | memcpy(syscall32_page, syscall32_syscall, |
| 103 | syscall32_syscall_end - syscall32_syscall); |
| 104 | } |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | __initcall(init_syscall32); |
| 109 | |
| 110 | /* May not be __init: called during resume */ |
| 111 | void syscall32_cpu_init(void) |
| 112 | { |
| 113 | if (use_sysenter < 0) |
| 114 | use_sysenter = (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL); |
| 115 | |
| 116 | /* Load these always in case some future AMD CPU supports |
| 117 | SYSENTER from compat mode too. */ |
| 118 | checking_wrmsrl(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS); |
| 119 | checking_wrmsrl(MSR_IA32_SYSENTER_ESP, 0ULL); |
| 120 | checking_wrmsrl(MSR_IA32_SYSENTER_EIP, (u64)ia32_sysenter_target); |
| 121 | |
| 122 | wrmsrl(MSR_CSTAR, ia32_cstar_target); |
| 123 | } |