2 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
3 * Copyright 2003 Andi Kleen, SuSE Labs.
5 * [ NOTE: this mechanism is now deprecated in favor of the vDSO. ]
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.
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.
17 * Note: the concept clashes with user mode linux. UML users should
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>
28 #include <linux/topology.h>
29 #include <linux/clocksource.h>
30 #include <linux/getcpu.h>
31 #include <linux/cpu.h>
32 #include <linux/smp.h>
33 #include <linux/notifier.h>
34 #include <linux/syscalls.h>
35 #include <linux/ratelimit.h>
37 #include <asm/vsyscall.h>
38 #include <asm/pgtable.h>
39 #include <asm/compat.h>
41 #include <asm/unistd.h>
42 #include <asm/fixmap.h>
43 #include <asm/errno.h>
45 #include <asm/segment.h>
47 #include <asm/topology.h>
48 #include <asm/vgtod.h>
49 #include <asm/traps.h>
51 #define CREATE_TRACE_POINTS
52 #include "vsyscall_trace.h"
54 DEFINE_VVAR(int, vgetcpu_mode);
55 DEFINE_VVAR(struct vsyscall_gtod_data, vsyscall_gtod_data);
57 static enum { EMULATE, NATIVE, NONE } vsyscall_mode = EMULATE;
59 static int __init vsyscall_setup(char *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))
76 early_param("vsyscall", vsyscall_setup);
78 void update_vsyscall_tz(void)
80 vsyscall_gtod_data.sys_tz = sys_tz;
83 void update_vsyscall(struct timespec *wall_time, struct timespec *wtm,
84 struct clocksource *clock, u32 mult)
86 write_seqcount_begin(&vsyscall_gtod_data.seq);
88 /* copy vsyscall data */
89 vsyscall_gtod_data.clock.vclock_mode = clock->archdata.vclock_mode;
90 vsyscall_gtod_data.clock.cycle_last = clock->cycle_last;
91 vsyscall_gtod_data.clock.mask = clock->mask;
92 vsyscall_gtod_data.clock.mult = mult;
93 vsyscall_gtod_data.clock.shift = clock->shift;
94 vsyscall_gtod_data.wall_time_sec = wall_time->tv_sec;
95 vsyscall_gtod_data.wall_time_nsec = wall_time->tv_nsec;
96 vsyscall_gtod_data.wall_to_monotonic = *wtm;
97 vsyscall_gtod_data.wall_time_coarse = __current_kernel_time();
99 write_seqcount_end(&vsyscall_gtod_data.seq);
102 static void warn_bad_vsyscall(const char *level, struct pt_regs *regs,
105 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, DEFAULT_RATELIMIT_BURST);
106 struct task_struct *tsk;
108 if (!show_unhandled_signals || !__ratelimit(&rs))
113 printk("%s%s[%d] %s ip:%lx cs:%lx sp:%lx ax:%lx si:%lx di:%lx\n",
114 level, tsk->comm, task_pid_nr(tsk),
115 message, regs->ip, regs->cs,
116 regs->sp, regs->ax, regs->si, regs->di);
119 static int addr_to_vsyscall_nr(unsigned long addr)
123 if ((addr & ~0xC00UL) != VSYSCALL_START)
126 nr = (addr & 0xC00UL) >> 10;
133 static bool write_ok_or_segv(unsigned long ptr, size_t size)
136 * XXX: if access_ok, get_user, and put_user handled
137 * sig_on_uaccess_error, this could go away.
140 if (!access_ok(VERIFY_WRITE, (void __user *)ptr, size)) {
142 struct thread_struct *thread = ¤t->thread;
144 thread->error_code = 6; /* user fault, no page, write */
146 thread->trap_no = 14;
148 memset(&info, 0, sizeof(info));
149 info.si_signo = SIGSEGV;
151 info.si_code = SEGV_MAPERR;
152 info.si_addr = (void __user *)ptr;
154 force_sig_info(SIGSEGV, &info, current);
161 bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
163 struct task_struct *tsk;
164 unsigned long caller;
166 int prev_sig_on_uaccess_error;
170 * No point in checking CS -- the only way to get here is a user mode
171 * trap to a high address, which means that we're in 64-bit user code.
174 WARN_ON_ONCE(address != regs->ip);
176 if (vsyscall_mode == NONE) {
177 warn_bad_vsyscall(KERN_INFO, regs,
178 "vsyscall attempted with vsyscall=none");
182 vsyscall_nr = addr_to_vsyscall_nr(address);
184 trace_emulate_vsyscall(vsyscall_nr);
186 if (vsyscall_nr < 0) {
187 warn_bad_vsyscall(KERN_WARNING, regs,
188 "misaligned vsyscall (exploit attempt or buggy program) -- look up the vsyscall kernel parameter if you need a workaround");
192 if (get_user(caller, (unsigned long __user *)regs->sp) != 0) {
193 warn_bad_vsyscall(KERN_WARNING, regs,
194 "vsyscall with bad stack (exploit attempt?)");
199 if (seccomp_mode(&tsk->seccomp))
203 * With a real vsyscall, page faults cause SIGSEGV. We want to
204 * preserve that behavior to make writing exploits harder.
206 prev_sig_on_uaccess_error = current_thread_info()->sig_on_uaccess_error;
207 current_thread_info()->sig_on_uaccess_error = 1;
210 * 0 is a valid user pointer (in the access_ok sense) on 32-bit and
211 * 64-bit, so we don't need to special-case it here. For all the
212 * vsyscalls, 0 means "don't write anything" not "write it at
216 switch (vsyscall_nr) {
218 if (!write_ok_or_segv(regs->di, sizeof(struct timeval)) ||
219 !write_ok_or_segv(regs->si, sizeof(struct timezone)))
222 ret = sys_gettimeofday(
223 (struct timeval __user *)regs->di,
224 (struct timezone __user *)regs->si);
228 if (!write_ok_or_segv(regs->di, sizeof(time_t)))
231 ret = sys_time((time_t __user *)regs->di);
235 if (!write_ok_or_segv(regs->di, sizeof(unsigned)) ||
236 !write_ok_or_segv(regs->si, sizeof(unsigned)))
239 ret = sys_getcpu((unsigned __user *)regs->di,
240 (unsigned __user *)regs->si,
245 current_thread_info()->sig_on_uaccess_error = prev_sig_on_uaccess_error;
247 if (ret == -EFAULT) {
248 /* Bad news -- userspace fed a bad pointer to a vsyscall. */
249 warn_bad_vsyscall(KERN_INFO, regs,
250 "vsyscall fault (exploit attempt?)");
253 * If we failed to generate a signal for any reason,
254 * generate one here. (This should be impossible.)
256 if (WARN_ON_ONCE(!sigismember(&tsk->pending.signal, SIGBUS) &&
257 !sigismember(&tsk->pending.signal, SIGSEGV)))
260 return true; /* Don't emulate the ret. */
265 /* Emulate a ret instruction. */
272 force_sig(SIGSEGV, current);
277 * Assume __initcall executes before all user space. Hopefully kmod
278 * doesn't violate that. We'll find out if it does.
280 static void __cpuinit vsyscall_set_cpu(int cpu)
283 unsigned long node = 0;
285 node = cpu_to_node(cpu);
287 if (cpu_has(&cpu_data(cpu), X86_FEATURE_RDTSCP))
288 write_rdtscp_aux((node << 12) | cpu);
291 * Store cpu number in limit so that it can be loaded quickly
292 * in user space in vgetcpu. (12 bits for the CPU and 8 bits for the node)
294 d = 0x0f40000000000ULL;
296 d |= (node & 0xf) << 12;
297 d |= (node >> 4) << 48;
299 write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_PER_CPU, &d, DESCTYPE_S);
302 static void __cpuinit cpu_vsyscall_init(void *arg)
304 /* preemption should be already off */
305 vsyscall_set_cpu(raw_smp_processor_id());
309 cpu_vsyscall_notifier(struct notifier_block *n, unsigned long action, void *arg)
311 long cpu = (long)arg;
313 if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
314 smp_call_function_single(cpu, cpu_vsyscall_init, NULL, 1);
319 void __init map_vsyscall(void)
321 extern char __vsyscall_page;
322 unsigned long physaddr_vsyscall = __pa_symbol(&__vsyscall_page);
323 extern char __vvar_page;
324 unsigned long physaddr_vvar_page = __pa_symbol(&__vvar_page);
326 __set_fixmap(VSYSCALL_FIRST_PAGE, physaddr_vsyscall,
327 vsyscall_mode == NATIVE
328 ? PAGE_KERNEL_VSYSCALL
330 BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_FIRST_PAGE) !=
331 (unsigned long)VSYSCALL_START);
333 __set_fixmap(VVAR_PAGE, physaddr_vvar_page, PAGE_KERNEL_VVAR);
334 BUILD_BUG_ON((unsigned long)__fix_to_virt(VVAR_PAGE) !=
335 (unsigned long)VVAR_ADDRESS);
338 static int __init vsyscall_init(void)
340 BUG_ON(VSYSCALL_ADDR(0) != __fix_to_virt(VSYSCALL_FIRST_PAGE));
342 on_each_cpu(cpu_vsyscall_init, NULL, 1);
343 /* notifier priority > KVM */
344 hotcpu_notifier(cpu_vsyscall_notifier, 30);
348 __initcall(vsyscall_init);