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>
44 #include <asm/vsyscall.h>
45 #include <asm/pgtable.h>
47 #include <asm/fixmap.h>
48 #include <asm/errno.h>
51 #define __vsyscall(nr) __attribute__ ((unused,__section__(".vsyscall_" #nr)))
52 #define force_inline __attribute__((always_inline)) inline
54 int __sysctl_vsyscall __section_sysctl_vsyscall
= 1;
55 seqlock_t __xtime_lock __section_xtime_lock
= SEQLOCK_UNLOCKED
;
57 #include <asm/unistd.h>
59 static force_inline
void timeval_normalize(struct timeval
* tv
)
63 __sec
= tv
->tv_usec
/ 1000000;
66 tv
->tv_usec
%= 1000000;
71 static force_inline
void do_vgettimeofday(struct timeval
* tv
)
74 unsigned long sec
, usec
;
77 sequence
= read_seqbegin(&__xtime_lock
);
80 usec
= (__xtime
.tv_nsec
/ 1000) +
81 (__jiffies
- __wall_jiffies
) * (1000000 / HZ
);
83 if (__vxtime
.mode
== VXTIME_TSC
) {
86 if (t
< __vxtime
.last_tsc
) t
= __vxtime
.last_tsc
;
87 usec
+= ((t
- __vxtime
.last_tsc
) *
88 __vxtime
.tsc_quot
) >> 32;
89 /* See comment in x86_64 do_gettimeofday. */
91 usec
+= ((readl(fix_to_virt(VSYSCALL_HPET
) + 0xf0) -
92 __vxtime
.last
) * __vxtime
.quot
) >> 32;
94 } while (read_seqretry(&__xtime_lock
, sequence
));
96 tv
->tv_sec
= sec
+ usec
/ 1000000;
97 tv
->tv_usec
= usec
% 1000000;
100 /* RED-PEN may want to readd seq locking, but then the variable should be write-once. */
101 static force_inline
void do_get_tz(struct timezone
* tz
)
107 static force_inline
int gettimeofday(struct timeval
*tv
, struct timezone
*tz
)
110 asm volatile("syscall"
112 : "0" (__NR_gettimeofday
),"D" (tv
),"S" (tz
) : __syscall_clobber
);
116 static force_inline
long time_syscall(long *t
)
119 asm volatile("syscall"
121 : "0" (__NR_time
),"D" (t
) : __syscall_clobber
);
125 static int __vsyscall(0) vgettimeofday(struct timeval
* tv
, struct timezone
* tz
)
127 if (unlikely(!__sysctl_vsyscall
))
128 return gettimeofday(tv
,tz
);
130 do_vgettimeofday(tv
);
136 /* This will break when the xtime seconds get inaccurate, but that is
138 static time_t __vsyscall(1) vtime(time_t *t
)
140 if (unlikely(!__sysctl_vsyscall
))
141 return time_syscall(t
);
144 return __xtime
.tv_sec
;
147 static long __vsyscall(2) venosys_0(void)
152 static long __vsyscall(3) venosys_1(void)
158 static void __init
map_vsyscall(void)
160 extern char __vsyscall_0
;
161 unsigned long physaddr_page0
= __pa_symbol(&__vsyscall_0
);
163 __set_fixmap(VSYSCALL_FIRST_PAGE
, physaddr_page0
, PAGE_KERNEL_VSYSCALL
);
166 static int __init
vsyscall_init(void)
168 if ((unsigned long) &vgettimeofday
!= VSYSCALL_ADDR(__NR_vgettimeofday
))
169 panic("vgettimeofday link addr broken");
170 if ((unsigned long) &vtime
!= VSYSCALL_ADDR(__NR_vtime
))
171 panic("vtime link addr broken");
172 if (VSYSCALL_ADDR(0) != __fix_to_virt(VSYSCALL_FIRST_PAGE
))
173 panic("fixmap first vsyscall %lx should be %lx", __fix_to_virt(VSYSCALL_FIRST_PAGE
),
180 __initcall(vsyscall_init
);