Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE |
| 3 | * Copyright 2003 Andi Kleen, SuSE Labs. |
| 4 | * |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 5 | * [ NOTE: this mechanism is now deprecated in favor of the vDSO. ] |
| 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * Thanks to hpa@transmeta.com for some useful hint. |
| 8 | * Special thanks to Ingo Molnar for his early experience with |
| 9 | * a different vsyscall implementation for Linux/IA32 and for the name. |
| 10 | * |
| 11 | * vsyscall 1 is located at -10Mbyte, vsyscall 2 is located |
| 12 | * at virtual address -10Mbyte+1024bytes etc... There are at max 4 |
| 13 | * vsyscalls. One vsyscall can reserve more than 1 slot to avoid |
| 14 | * jumping out of line if necessary. We cannot add more with this |
| 15 | * mechanism because older kernels won't return -ENOSYS. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | * |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 17 | * Note: the concept clashes with user mode linux. UML users should |
| 18 | * use the vDSO. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include <linux/time.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/timer.h> |
| 25 | #include <linux/seqlock.h> |
| 26 | #include <linux/jiffies.h> |
| 27 | #include <linux/sysctl.h> |
Paul Gortmaker | 2957402 | 2011-05-26 12:33:18 -0400 | [diff] [blame] | 28 | #include <linux/topology.h> |
john stultz | 7460ed2 | 2007-02-16 01:28:21 -0800 | [diff] [blame] | 29 | #include <linux/clocksource.h> |
Vojtech Pavlik | c08c820 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 30 | #include <linux/getcpu.h> |
Andi Kleen | 8c131af | 2006-11-14 16:57:46 +0100 | [diff] [blame] | 31 | #include <linux/cpu.h> |
| 32 | #include <linux/smp.h> |
| 33 | #include <linux/notifier.h> |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 34 | #include <linux/syscalls.h> |
| 35 | #include <linux/ratelimit.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | #include <asm/vsyscall.h> |
| 38 | #include <asm/pgtable.h> |
Andy Lutomirski | c971294 | 2011-07-13 09:24:09 -0400 | [diff] [blame] | 39 | #include <asm/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <asm/page.h> |
john stultz | 7460ed2 | 2007-02-16 01:28:21 -0800 | [diff] [blame] | 41 | #include <asm/unistd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <asm/fixmap.h> |
| 43 | #include <asm/errno.h> |
| 44 | #include <asm/io.h> |
Vojtech Pavlik | c08c820 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 45 | #include <asm/segment.h> |
| 46 | #include <asm/desc.h> |
| 47 | #include <asm/topology.h> |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 48 | #include <asm/vgtod.h> |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 49 | #include <asm/traps.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Andy Lutomirski | c149a66 | 2011-08-03 09:31:54 -0400 | [diff] [blame] | 51 | #define CREATE_TRACE_POINTS |
| 52 | #include "vsyscall_trace.h" |
| 53 | |
Andy Lutomirski | 8c49d9a | 2011-05-23 09:31:24 -0400 | [diff] [blame] | 54 | DEFINE_VVAR(int, vgetcpu_mode); |
Thomas Gleixner | 2ab5165 | 2012-02-28 19:46:04 +0000 | [diff] [blame] | 55 | DEFINE_VVAR(struct vsyscall_gtod_data, vsyscall_gtod_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Andy Lutomirski | 2e57ae0 | 2011-11-07 16:33:41 -0800 | [diff] [blame] | 57 | static enum { EMULATE, NATIVE, NONE } vsyscall_mode = EMULATE; |
Andy Lutomirski | 3ae3665 | 2011-08-10 11:15:32 -0400 | [diff] [blame] | 58 | |
| 59 | static int __init vsyscall_setup(char *str) |
| 60 | { |
| 61 | if (str) { |
| 62 | if (!strcmp("emulate", str)) |
| 63 | vsyscall_mode = EMULATE; |
| 64 | else if (!strcmp("native", str)) |
| 65 | vsyscall_mode = NATIVE; |
| 66 | else if (!strcmp("none", str)) |
| 67 | vsyscall_mode = NONE; |
| 68 | else |
| 69 | return -EINVAL; |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | return -EINVAL; |
| 75 | } |
| 76 | early_param("vsyscall", vsyscall_setup); |
| 77 | |
Tony Breeds | 2c62214 | 2007-10-18 03:04:57 -0700 | [diff] [blame] | 78 | void update_vsyscall_tz(void) |
| 79 | { |
Tony Breeds | 2c62214 | 2007-10-18 03:04:57 -0700 | [diff] [blame] | 80 | vsyscall_gtod_data.sys_tz = sys_tz; |
Tony Breeds | 2c62214 | 2007-10-18 03:04:57 -0700 | [diff] [blame] | 81 | } |
| 82 | |
John Stultz | 7615856 | 2010-07-13 17:56:23 -0700 | [diff] [blame] | 83 | void update_vsyscall(struct timespec *wall_time, struct timespec *wtm, |
| 84 | struct clocksource *clock, u32 mult) |
john stultz | 7460ed2 | 2007-02-16 01:28:21 -0800 | [diff] [blame] | 85 | { |
Andy Lutomirski | 91ec87d | 2012-03-22 21:15:51 -0700 | [diff] [blame] | 86 | struct timespec monotonic; |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 87 | |
Thomas Gleixner | 68fe7b2 | 2012-03-24 09:29:22 +0100 | [diff] [blame] | 88 | write_seqcount_begin(&vsyscall_gtod_data.seq); |
| 89 | |
john stultz | 7460ed2 | 2007-02-16 01:28:21 -0800 | [diff] [blame] | 90 | /* copy vsyscall data */ |
Andy Lutomirski | 98d0ac3 | 2011-07-14 06:47:22 -0400 | [diff] [blame] | 91 | vsyscall_gtod_data.clock.vclock_mode = clock->archdata.vclock_mode; |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 92 | vsyscall_gtod_data.clock.cycle_last = clock->cycle_last; |
| 93 | vsyscall_gtod_data.clock.mask = clock->mask; |
| 94 | vsyscall_gtod_data.clock.mult = mult; |
| 95 | vsyscall_gtod_data.clock.shift = clock->shift; |
Andy Lutomirski | 91ec87d | 2012-03-22 21:15:51 -0700 | [diff] [blame] | 96 | |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 97 | vsyscall_gtod_data.wall_time_sec = wall_time->tv_sec; |
| 98 | vsyscall_gtod_data.wall_time_nsec = wall_time->tv_nsec; |
Andy Lutomirski | 91ec87d | 2012-03-22 21:15:51 -0700 | [diff] [blame] | 99 | |
| 100 | monotonic = timespec_add(*wall_time, *wtm); |
| 101 | vsyscall_gtod_data.monotonic_time_sec = monotonic.tv_sec; |
| 102 | vsyscall_gtod_data.monotonic_time_nsec = monotonic.tv_nsec; |
| 103 | |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 104 | vsyscall_gtod_data.wall_time_coarse = __current_kernel_time(); |
Andy Lutomirski | 91ec87d | 2012-03-22 21:15:51 -0700 | [diff] [blame] | 105 | vsyscall_gtod_data.monotonic_time_coarse = |
| 106 | timespec_add(vsyscall_gtod_data.wall_time_coarse, *wtm); |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 107 | |
Thomas Gleixner | 2ab5165 | 2012-02-28 19:46:04 +0000 | [diff] [blame] | 108 | write_seqcount_end(&vsyscall_gtod_data.seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 111 | static void warn_bad_vsyscall(const char *level, struct pt_regs *regs, |
| 112 | const char *message) |
| 113 | { |
| 114 | static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, DEFAULT_RATELIMIT_BURST); |
| 115 | struct task_struct *tsk; |
| 116 | |
| 117 | if (!show_unhandled_signals || !__ratelimit(&rs)) |
| 118 | return; |
| 119 | |
| 120 | tsk = current; |
| 121 | |
Andy Lutomirski | c971294 | 2011-07-13 09:24:09 -0400 | [diff] [blame] | 122 | printk("%s%s[%d] %s ip:%lx cs:%lx sp:%lx ax:%lx si:%lx di:%lx\n", |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 123 | level, tsk->comm, task_pid_nr(tsk), |
Andy Lutomirski | 3ae3665 | 2011-08-10 11:15:32 -0400 | [diff] [blame] | 124 | message, regs->ip, regs->cs, |
Andy Lutomirski | c971294 | 2011-07-13 09:24:09 -0400 | [diff] [blame] | 125 | regs->sp, regs->ax, regs->si, regs->di); |
| 126 | } |
| 127 | |
| 128 | static int addr_to_vsyscall_nr(unsigned long addr) |
| 129 | { |
| 130 | int nr; |
| 131 | |
| 132 | if ((addr & ~0xC00UL) != VSYSCALL_START) |
| 133 | return -EINVAL; |
| 134 | |
| 135 | nr = (addr & 0xC00UL) >> 10; |
| 136 | if (nr >= 3) |
| 137 | return -EINVAL; |
| 138 | |
| 139 | return nr; |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 140 | } |
| 141 | |
Will Drewry | 09d3144 | 2012-07-14 10:32:52 -0500 | [diff] [blame^] | 142 | #ifdef CONFIG_SECCOMP |
Will Drewry | 5651721 | 2012-07-13 12:06:35 -0500 | [diff] [blame] | 143 | static int vsyscall_seccomp(struct task_struct *tsk, int syscall_nr) |
| 144 | { |
| 145 | if (!seccomp_mode(&tsk->seccomp)) |
| 146 | return 0; |
| 147 | task_pt_regs(tsk)->orig_ax = syscall_nr; |
| 148 | task_pt_regs(tsk)->ax = syscall_nr; |
| 149 | return __secure_computing(syscall_nr); |
| 150 | } |
Will Drewry | 09d3144 | 2012-07-14 10:32:52 -0500 | [diff] [blame^] | 151 | #else |
| 152 | #define vsyscall_seccomp(_tsk, _nr) 0 |
| 153 | #endif |
Will Drewry | 5651721 | 2012-07-13 12:06:35 -0500 | [diff] [blame] | 154 | |
Andy Lutomirski | 4fc3490 | 2011-11-07 16:33:40 -0800 | [diff] [blame] | 155 | static bool write_ok_or_segv(unsigned long ptr, size_t size) |
| 156 | { |
| 157 | /* |
| 158 | * XXX: if access_ok, get_user, and put_user handled |
| 159 | * sig_on_uaccess_error, this could go away. |
| 160 | */ |
| 161 | |
| 162 | if (!access_ok(VERIFY_WRITE, (void __user *)ptr, size)) { |
| 163 | siginfo_t info; |
| 164 | struct thread_struct *thread = ¤t->thread; |
| 165 | |
| 166 | thread->error_code = 6; /* user fault, no page, write */ |
| 167 | thread->cr2 = ptr; |
Srikar Dronamraju | 51e7dc7 | 2012-03-12 14:55:55 +0530 | [diff] [blame] | 168 | thread->trap_nr = X86_TRAP_PF; |
Andy Lutomirski | 4fc3490 | 2011-11-07 16:33:40 -0800 | [diff] [blame] | 169 | |
| 170 | memset(&info, 0, sizeof(info)); |
| 171 | info.si_signo = SIGSEGV; |
| 172 | info.si_errno = 0; |
| 173 | info.si_code = SEGV_MAPERR; |
| 174 | info.si_addr = (void __user *)ptr; |
| 175 | |
| 176 | force_sig_info(SIGSEGV, &info, current); |
| 177 | return false; |
| 178 | } else { |
| 179 | return true; |
| 180 | } |
| 181 | } |
| 182 | |
Andy Lutomirski | 3ae3665 | 2011-08-10 11:15:32 -0400 | [diff] [blame] | 183 | bool emulate_vsyscall(struct pt_regs *regs, unsigned long address) |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 184 | { |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 185 | struct task_struct *tsk; |
| 186 | unsigned long caller; |
| 187 | int vsyscall_nr; |
Andy Lutomirski | 4fc3490 | 2011-11-07 16:33:40 -0800 | [diff] [blame] | 188 | int prev_sig_on_uaccess_error; |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 189 | long ret; |
Will Drewry | 5651721 | 2012-07-13 12:06:35 -0500 | [diff] [blame] | 190 | int skip; |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 191 | |
Andy Lutomirski | 3ae3665 | 2011-08-10 11:15:32 -0400 | [diff] [blame] | 192 | /* |
| 193 | * No point in checking CS -- the only way to get here is a user mode |
| 194 | * trap to a high address, which means that we're in 64-bit user code. |
| 195 | */ |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 196 | |
Andy Lutomirski | 3ae3665 | 2011-08-10 11:15:32 -0400 | [diff] [blame] | 197 | WARN_ON_ONCE(address != regs->ip); |
Andy Lutomirski | c971294 | 2011-07-13 09:24:09 -0400 | [diff] [blame] | 198 | |
Andy Lutomirski | 3ae3665 | 2011-08-10 11:15:32 -0400 | [diff] [blame] | 199 | if (vsyscall_mode == NONE) { |
| 200 | warn_bad_vsyscall(KERN_INFO, regs, |
| 201 | "vsyscall attempted with vsyscall=none"); |
| 202 | return false; |
Andy Lutomirski | c971294 | 2011-07-13 09:24:09 -0400 | [diff] [blame] | 203 | } |
| 204 | |
Andy Lutomirski | 3ae3665 | 2011-08-10 11:15:32 -0400 | [diff] [blame] | 205 | vsyscall_nr = addr_to_vsyscall_nr(address); |
Andy Lutomirski | c149a66 | 2011-08-03 09:31:54 -0400 | [diff] [blame] | 206 | |
| 207 | trace_emulate_vsyscall(vsyscall_nr); |
| 208 | |
Andy Lutomirski | c971294 | 2011-07-13 09:24:09 -0400 | [diff] [blame] | 209 | if (vsyscall_nr < 0) { |
| 210 | warn_bad_vsyscall(KERN_WARNING, regs, |
Andy Lutomirski | 3ae3665 | 2011-08-10 11:15:32 -0400 | [diff] [blame] | 211 | "misaligned vsyscall (exploit attempt or buggy program) -- look up the vsyscall kernel parameter if you need a workaround"); |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 212 | goto sigsegv; |
| 213 | } |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 214 | |
| 215 | if (get_user(caller, (unsigned long __user *)regs->sp) != 0) { |
Andy Lutomirski | 3ae3665 | 2011-08-10 11:15:32 -0400 | [diff] [blame] | 216 | warn_bad_vsyscall(KERN_WARNING, regs, |
| 217 | "vsyscall with bad stack (exploit attempt?)"); |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 218 | goto sigsegv; |
| 219 | } |
| 220 | |
| 221 | tsk = current; |
Andy Lutomirski | 4fc3490 | 2011-11-07 16:33:40 -0800 | [diff] [blame] | 222 | /* |
| 223 | * With a real vsyscall, page faults cause SIGSEGV. We want to |
| 224 | * preserve that behavior to make writing exploits harder. |
| 225 | */ |
| 226 | prev_sig_on_uaccess_error = current_thread_info()->sig_on_uaccess_error; |
| 227 | current_thread_info()->sig_on_uaccess_error = 1; |
| 228 | |
| 229 | /* |
Emil Goode | 46ed99d | 2012-04-01 20:48:04 +0200 | [diff] [blame] | 230 | * NULL is a valid user pointer (in the access_ok sense) on 32-bit and |
Andy Lutomirski | 4fc3490 | 2011-11-07 16:33:40 -0800 | [diff] [blame] | 231 | * 64-bit, so we don't need to special-case it here. For all the |
Emil Goode | 46ed99d | 2012-04-01 20:48:04 +0200 | [diff] [blame] | 232 | * vsyscalls, NULL means "don't write anything" not "write it at |
Andy Lutomirski | 4fc3490 | 2011-11-07 16:33:40 -0800 | [diff] [blame] | 233 | * address 0". |
| 234 | */ |
| 235 | ret = -EFAULT; |
Will Drewry | 5651721 | 2012-07-13 12:06:35 -0500 | [diff] [blame] | 236 | skip = 0; |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 237 | switch (vsyscall_nr) { |
| 238 | case 0: |
Will Drewry | 5651721 | 2012-07-13 12:06:35 -0500 | [diff] [blame] | 239 | skip = vsyscall_seccomp(tsk, __NR_gettimeofday); |
| 240 | if (skip) |
| 241 | break; |
| 242 | |
Andy Lutomirski | 4fc3490 | 2011-11-07 16:33:40 -0800 | [diff] [blame] | 243 | if (!write_ok_or_segv(regs->di, sizeof(struct timeval)) || |
| 244 | !write_ok_or_segv(regs->si, sizeof(struct timezone))) |
| 245 | break; |
| 246 | |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 247 | ret = sys_gettimeofday( |
| 248 | (struct timeval __user *)regs->di, |
| 249 | (struct timezone __user *)regs->si); |
| 250 | break; |
| 251 | |
| 252 | case 1: |
Will Drewry | 5651721 | 2012-07-13 12:06:35 -0500 | [diff] [blame] | 253 | skip = vsyscall_seccomp(tsk, __NR_time); |
| 254 | if (skip) |
| 255 | break; |
| 256 | |
Andy Lutomirski | 4fc3490 | 2011-11-07 16:33:40 -0800 | [diff] [blame] | 257 | if (!write_ok_or_segv(regs->di, sizeof(time_t))) |
| 258 | break; |
| 259 | |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 260 | ret = sys_time((time_t __user *)regs->di); |
| 261 | break; |
| 262 | |
| 263 | case 2: |
Will Drewry | 5651721 | 2012-07-13 12:06:35 -0500 | [diff] [blame] | 264 | skip = vsyscall_seccomp(tsk, __NR_getcpu); |
| 265 | if (skip) |
| 266 | break; |
| 267 | |
Andy Lutomirski | 4fc3490 | 2011-11-07 16:33:40 -0800 | [diff] [blame] | 268 | if (!write_ok_or_segv(regs->di, sizeof(unsigned)) || |
| 269 | !write_ok_or_segv(regs->si, sizeof(unsigned))) |
| 270 | break; |
| 271 | |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 272 | ret = sys_getcpu((unsigned __user *)regs->di, |
| 273 | (unsigned __user *)regs->si, |
Emil Goode | 46ed99d | 2012-04-01 20:48:04 +0200 | [diff] [blame] | 274 | NULL); |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 275 | break; |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 276 | } |
| 277 | |
Andy Lutomirski | 4fc3490 | 2011-11-07 16:33:40 -0800 | [diff] [blame] | 278 | current_thread_info()->sig_on_uaccess_error = prev_sig_on_uaccess_error; |
| 279 | |
Will Drewry | 5651721 | 2012-07-13 12:06:35 -0500 | [diff] [blame] | 280 | if (skip) { |
| 281 | if ((long)regs->ax <= 0L) /* seccomp errno emulation */ |
| 282 | goto do_ret; |
| 283 | goto done; /* seccomp trace/trap */ |
| 284 | } |
| 285 | |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 286 | if (ret == -EFAULT) { |
Andy Lutomirski | 4fc3490 | 2011-11-07 16:33:40 -0800 | [diff] [blame] | 287 | /* Bad news -- userspace fed a bad pointer to a vsyscall. */ |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 288 | warn_bad_vsyscall(KERN_INFO, regs, |
| 289 | "vsyscall fault (exploit attempt?)"); |
Andy Lutomirski | 4fc3490 | 2011-11-07 16:33:40 -0800 | [diff] [blame] | 290 | |
| 291 | /* |
| 292 | * If we failed to generate a signal for any reason, |
| 293 | * generate one here. (This should be impossible.) |
| 294 | */ |
| 295 | if (WARN_ON_ONCE(!sigismember(&tsk->pending.signal, SIGBUS) && |
| 296 | !sigismember(&tsk->pending.signal, SIGSEGV))) |
| 297 | goto sigsegv; |
| 298 | |
| 299 | return true; /* Don't emulate the ret. */ |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | regs->ax = ret; |
| 303 | |
Will Drewry | 5651721 | 2012-07-13 12:06:35 -0500 | [diff] [blame] | 304 | do_ret: |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 305 | /* Emulate a ret instruction. */ |
| 306 | regs->ip = caller; |
| 307 | regs->sp += 8; |
Will Drewry | 5651721 | 2012-07-13 12:06:35 -0500 | [diff] [blame] | 308 | done: |
Andy Lutomirski | 3ae3665 | 2011-08-10 11:15:32 -0400 | [diff] [blame] | 309 | return true; |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 310 | |
| 311 | sigsegv: |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 312 | force_sig(SIGSEGV, current); |
Andy Lutomirski | 3ae3665 | 2011-08-10 11:15:32 -0400 | [diff] [blame] | 313 | return true; |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /* |
| 317 | * Assume __initcall executes before all user space. Hopefully kmod |
| 318 | * doesn't violate that. We'll find out if it does. |
john stultz | 7460ed2 | 2007-02-16 01:28:21 -0800 | [diff] [blame] | 319 | */ |
Andi Kleen | 8c131af | 2006-11-14 16:57:46 +0100 | [diff] [blame] | 320 | static void __cpuinit vsyscall_set_cpu(int cpu) |
Vojtech Pavlik | c08c820 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 321 | { |
Jeremy Fitzhardinge | fc8b8a6 | 2008-06-25 00:19:01 -0400 | [diff] [blame] | 322 | unsigned long d; |
Vojtech Pavlik | c08c820 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 323 | unsigned long node = 0; |
| 324 | #ifdef CONFIG_NUMA |
Mike Travis | 98c9e27 | 2007-10-17 18:04:39 +0200 | [diff] [blame] | 325 | node = cpu_to_node(cpu); |
Vojtech Pavlik | c08c820 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 326 | #endif |
Mike Travis | 92cb761 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 327 | if (cpu_has(&cpu_data(cpu), X86_FEATURE_RDTSCP)) |
Andi Kleen | 8c131af | 2006-11-14 16:57:46 +0100 | [diff] [blame] | 328 | write_rdtscp_aux((node << 12) | cpu); |
Vojtech Pavlik | c08c820 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 329 | |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 330 | /* |
| 331 | * Store cpu number in limit so that it can be loaded quickly |
| 332 | * in user space in vgetcpu. (12 bits for the CPU and 8 bits for the node) |
| 333 | */ |
Jeremy Fitzhardinge | fc8b8a6 | 2008-06-25 00:19:01 -0400 | [diff] [blame] | 334 | d = 0x0f40000000000ULL; |
| 335 | d |= cpu; |
| 336 | d |= (node & 0xf) << 12; |
| 337 | d |= (node >> 4) << 48; |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 338 | |
Jeremy Fitzhardinge | fc8b8a6 | 2008-06-25 00:19:01 -0400 | [diff] [blame] | 339 | write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_PER_CPU, &d, DESCTYPE_S); |
Vojtech Pavlik | c08c820 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 340 | } |
| 341 | |
Andi Kleen | 8c131af | 2006-11-14 16:57:46 +0100 | [diff] [blame] | 342 | static void __cpuinit cpu_vsyscall_init(void *arg) |
| 343 | { |
| 344 | /* preemption should be already off */ |
| 345 | vsyscall_set_cpu(raw_smp_processor_id()); |
| 346 | } |
| 347 | |
| 348 | static int __cpuinit |
| 349 | cpu_vsyscall_notifier(struct notifier_block *n, unsigned long action, void *arg) |
| 350 | { |
| 351 | long cpu = (long)arg; |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 352 | |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 353 | if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 354 | smp_call_function_single(cpu, cpu_vsyscall_init, NULL, 1); |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 355 | |
Andi Kleen | 8c131af | 2006-11-14 16:57:46 +0100 | [diff] [blame] | 356 | return NOTIFY_DONE; |
| 357 | } |
| 358 | |
Ingo Molnar | e402644 | 2008-01-30 13:32:39 +0100 | [diff] [blame] | 359 | void __init map_vsyscall(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | { |
Andy Lutomirski | 3ae3665 | 2011-08-10 11:15:32 -0400 | [diff] [blame] | 361 | extern char __vsyscall_page; |
| 362 | unsigned long physaddr_vsyscall = __pa_symbol(&__vsyscall_page); |
Andy Lutomirski | 9fd67b4 | 2011-06-05 13:50:19 -0400 | [diff] [blame] | 363 | extern char __vvar_page; |
| 364 | unsigned long physaddr_vvar_page = __pa_symbol(&__vvar_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
Andy Lutomirski | 3ae3665 | 2011-08-10 11:15:32 -0400 | [diff] [blame] | 366 | __set_fixmap(VSYSCALL_FIRST_PAGE, physaddr_vsyscall, |
| 367 | vsyscall_mode == NATIVE |
| 368 | ? PAGE_KERNEL_VSYSCALL |
| 369 | : PAGE_KERNEL_VVAR); |
| 370 | BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_FIRST_PAGE) != |
| 371 | (unsigned long)VSYSCALL_START); |
| 372 | |
Andy Lutomirski | 9fd67b4 | 2011-06-05 13:50:19 -0400 | [diff] [blame] | 373 | __set_fixmap(VVAR_PAGE, physaddr_vvar_page, PAGE_KERNEL_VVAR); |
Andy Lutomirski | 3ae3665 | 2011-08-10 11:15:32 -0400 | [diff] [blame] | 374 | BUILD_BUG_ON((unsigned long)__fix_to_virt(VVAR_PAGE) != |
| 375 | (unsigned long)VVAR_ADDRESS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static int __init vsyscall_init(void) |
| 379 | { |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 380 | BUG_ON(VSYSCALL_ADDR(0) != __fix_to_virt(VSYSCALL_FIRST_PAGE)); |
| 381 | |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 382 | on_each_cpu(cpu_vsyscall_init, NULL, 1); |
Sheng Yang | be43f83 | 2009-12-18 16:48:45 +0800 | [diff] [blame] | 383 | /* notifier priority > KVM */ |
| 384 | hotcpu_notifier(cpu_vsyscall_notifier, 30); |
Andy Lutomirski | 5cec93c | 2011-06-05 13:50:24 -0400 | [diff] [blame] | 385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | return 0; |
| 387 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | __initcall(vsyscall_init); |