ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / x86_64 / kernel / vsyscall.c
1 /*
2  *  linux/arch/x86_64/kernel/vsyscall.c
3  *
4  *  Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
5  *  Copyright 2003 Andi Kleen, SuSE Labs.
6  *
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 8192
13  *  vsyscalls. One vsyscall can reserve more than 1 slot to avoid
14  *  jumping out of line if necessary.
15  *
16  *  Note: the concept clashes with user mode linux. If you use UML just
17  *  set the kernel.vsyscall sysctl to 0.
18  */
19
20 /*
21  * TODO 2001-03-20:
22  *
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
32  *    fix it.
33  *
34  * These are not urgent things that we need to address only before shipping the first
35  * production binary kernels.
36  */
37
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
44 #include <asm/vsyscall.h>
45 #include <asm/pgtable.h>
46 #include <asm/page.h>
47 #include <asm/fixmap.h>
48 #include <asm/errno.h>
49 #include <asm/io.h>
50
51 #define __vsyscall(nr) __attribute__ ((unused,__section__(".vsyscall_" #nr)))
52 #define force_inline __attribute__((always_inline)) inline
53
54 int __sysctl_vsyscall __section_sysctl_vsyscall = 1;
55 seqlock_t __xtime_lock __section_xtime_lock = SEQLOCK_UNLOCKED;
56
57 #include <asm/unistd.h>
58
59 static force_inline void timeval_normalize(struct timeval * tv)
60 {
61         time_t __sec;
62
63         __sec = tv->tv_usec / 1000000;
64         if (__sec)
65         {
66                 tv->tv_usec %= 1000000;
67                 tv->tv_sec += __sec;
68         }
69 }
70
71 static force_inline void do_vgettimeofday(struct timeval * tv)
72 {
73         long sequence, t;
74         unsigned long sec, usec;
75
76         do {
77                 sequence = read_seqbegin(&__xtime_lock);
78                 
79                 sec = __xtime.tv_sec;
80                 usec = (__xtime.tv_nsec / 1000) +
81                         (__jiffies - __wall_jiffies) * (1000000 / HZ);
82
83                 if (__vxtime.mode == VXTIME_TSC) {
84                         sync_core();
85                         rdtscll(t);
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. */ 
90                 } else {
91                         usec += ((readl(fix_to_virt(VSYSCALL_HPET) + 0xf0) -
92                                   __vxtime.last) * __vxtime.quot) >> 32;
93                 }
94         } while (read_seqretry(&__xtime_lock, sequence));
95
96         tv->tv_sec = sec + usec / 1000000;
97         tv->tv_usec = usec % 1000000;
98 }
99
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)
102 {
103                 *tz = __sys_tz;
104 }
105
106
107 static force_inline int gettimeofday(struct timeval *tv, struct timezone *tz)
108 {
109         int ret;
110         asm volatile("syscall" 
111                 : "=a" (ret)
112                 : "0" (__NR_gettimeofday),"D" (tv),"S" (tz) : __syscall_clobber );
113         return ret;
114 }
115
116 static force_inline long time_syscall(long *t)
117 {
118         long secs;
119         asm volatile("syscall" 
120                 : "=a" (secs)
121                 : "0" (__NR_time),"D" (t) : __syscall_clobber);
122         return secs;
123 }
124
125 static int __vsyscall(0) vgettimeofday(struct timeval * tv, struct timezone * tz)
126 {
127         if (unlikely(!__sysctl_vsyscall))
128         return gettimeofday(tv,tz); 
129         if (tv)
130                 do_vgettimeofday(tv);
131         if (tz)
132                 do_get_tz(tz);
133         return 0;
134 }
135
136 /* This will break when the xtime seconds get inaccurate, but that is
137  * unlikely */
138 static time_t __vsyscall(1) vtime(time_t *t)
139 {
140         if (unlikely(!__sysctl_vsyscall))
141                 return time_syscall(t);
142         else if (t)
143                 *t = __xtime.tv_sec;            
144         return __xtime.tv_sec;
145 }
146
147 static long __vsyscall(2) venosys_0(void)
148 {
149         return -ENOSYS;
150 }
151
152 static long __vsyscall(3) venosys_1(void)
153 {
154         return -ENOSYS;
155
156 }
157
158 static void __init map_vsyscall(void)
159 {
160         extern char __vsyscall_0;
161         unsigned long physaddr_page0 = __pa_symbol(&__vsyscall_0);
162
163         __set_fixmap(VSYSCALL_FIRST_PAGE, physaddr_page0, PAGE_KERNEL_VSYSCALL);
164 }
165
166 static int __init vsyscall_init(void)
167 {
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),
174                       VSYSCALL_ADDR(0));
175         map_vsyscall();
176
177         return 0;
178 }
179
180 __initcall(vsyscall_init);