2 * linux/arch/x86_64/kernel/vsyscall.c
4 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Copyright 2003 Andi Kleen, SuSE Labs.
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 8192
13 * vsyscalls. One vsyscall can reserve more than 1 slot to avoid
14 * jumping out of line if necessary.
16 * Note: the concept clashes with user mode linux. If you use UML just
17 * set the kernel.vsyscall sysctl to 0.
23 * 1) make page fault handler detect faults on page1-page-last of the vsyscall
24 * virtual space, and make it increase %rip and write -ENOSYS in %rax (so
25 * we'll be able to upgrade to a new glibc without upgrading kernel after
26 * we add more vsyscalls.
27 * 2) Possibly we need a fixmap table for the vsyscalls too if we want
28 * to avoid SIGSEGV and we want to return -EFAULT from the vsyscalls as well.
29 * Can we segfault inside a "syscall"? We can fix this anytime and those fixes
30 * won't be visible for userspace. Not fixing this is a noop for correct programs,
31 * broken programs will segfault and there's no security risk until we choose to
34 * These are not urgent things that we need to address only before shipping the first
35 * production binary kernels.
38 #include <linux/time.h>
39 #include <linux/init.h>
40 #include <linux/kernel.h>
41 #include <linux/timer.h>
42 #include <linux/seqlock.h>
43 #include <linux/jiffies.h>
45 #include <asm/vsyscall.h>
46 #include <asm/pgtable.h>
48 #include <asm/fixmap.h>
49 #include <asm/errno.h>
52 #define __vsyscall(nr) __attribute__ ((unused,__section__(".vsyscall_" #nr)))
53 #define force_inline __attribute__((always_inline)) inline
55 int __sysctl_vsyscall __section_sysctl_vsyscall = 1;
56 seqlock_t __xtime_lock __section_xtime_lock = SEQLOCK_UNLOCKED;
58 #include <asm/unistd.h>
60 static force_inline void timeval_normalize(struct timeval * tv)
64 __sec = tv->tv_usec / 1000000;
67 tv->tv_usec %= 1000000;
72 static force_inline void do_vgettimeofday(struct timeval * tv)
75 unsigned long sec, usec;
78 sequence = read_seqbegin(&__xtime_lock);
81 usec = (__xtime.tv_nsec / 1000) +
82 (__jiffies - __wall_jiffies) * (1000000 / HZ);
84 if (__vxtime.mode == VXTIME_TSC) {
87 if (t < __vxtime.last_tsc) t = __vxtime.last_tsc;
88 usec += ((t - __vxtime.last_tsc) *
89 __vxtime.tsc_quot) >> 32;
90 /* See comment in x86_64 do_gettimeofday. */
92 usec += ((readl((void *)fix_to_virt(VSYSCALL_HPET) + 0xf0) -
93 __vxtime.last) * __vxtime.quot) >> 32;
95 } while (read_seqretry(&__xtime_lock, sequence));
97 tv->tv_sec = sec + usec / 1000000;
98 tv->tv_usec = usec % 1000000;
101 /* RED-PEN may want to readd seq locking, but then the variable should be write-once. */
102 static force_inline void do_get_tz(struct timezone * tz)
108 static force_inline int gettimeofday(struct timeval *tv, struct timezone *tz)
111 asm volatile("syscall"
113 : "0" (__NR_gettimeofday),"D" (tv),"S" (tz) : __syscall_clobber );
117 static force_inline long time_syscall(long *t)
120 asm volatile("syscall"
122 : "0" (__NR_time),"D" (t) : __syscall_clobber);
126 static int __vsyscall(0) vgettimeofday(struct timeval * tv, struct timezone * tz)
128 if (unlikely(!__sysctl_vsyscall))
129 return gettimeofday(tv,tz);
131 do_vgettimeofday(tv);
137 /* This will break when the xtime seconds get inaccurate, but that is
139 static time_t __vsyscall(1) vtime(time_t *t)
141 if (unlikely(!__sysctl_vsyscall))
142 return time_syscall(t);
145 return __xtime.tv_sec;
148 static long __vsyscall(2) venosys_0(void)
153 static long __vsyscall(3) venosys_1(void)
159 static void __init map_vsyscall(void)
161 extern char __vsyscall_0;
162 unsigned long physaddr_page0 = __pa_symbol(&__vsyscall_0);
164 __set_fixmap(VSYSCALL_FIRST_PAGE, physaddr_page0, PAGE_KERNEL_VSYSCALL);
167 static int __init vsyscall_init(void)
169 BUG_ON(((unsigned long) &vgettimeofday !=
170 VSYSCALL_ADDR(__NR_vgettimeofday)));
171 BUG_ON((unsigned long) &vtime != VSYSCALL_ADDR(__NR_vtime));
172 BUG_ON((VSYSCALL_ADDR(0) != __fix_to_virt(VSYSCALL_FIRST_PAGE)));
179 __initcall(vsyscall_init);