vserver 1.9.5.x5
[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 #include <linux/jiffies.h>
44
45 #include <asm/vsyscall.h>
46 #include <asm/pgtable.h>
47 #include <asm/page.h>
48 #include <asm/fixmap.h>
49 #include <asm/errno.h>
50 #include <asm/io.h>
51
52 #define __vsyscall(nr) __attribute__ ((unused,__section__(".vsyscall_" #nr)))
53 #define force_inline __attribute__((always_inline)) inline
54
55 int __sysctl_vsyscall __section_sysctl_vsyscall = 1;
56 seqlock_t __xtime_lock __section_xtime_lock = SEQLOCK_UNLOCKED;
57
58 #include <asm/unistd.h>
59
60 static force_inline void timeval_normalize(struct timeval * tv)
61 {
62         time_t __sec;
63
64         __sec = tv->tv_usec / 1000000;
65         if (__sec)
66         {
67                 tv->tv_usec %= 1000000;
68                 tv->tv_sec += __sec;
69         }
70 }
71
72 static force_inline void do_vgettimeofday(struct timeval * tv)
73 {
74         long sequence, t;
75         unsigned long sec, usec;
76
77         do {
78                 sequence = read_seqbegin(&__xtime_lock);
79                 
80                 sec = __xtime.tv_sec;
81                 usec = (__xtime.tv_nsec / 1000) +
82                         (__jiffies - __wall_jiffies) * (1000000 / HZ);
83
84                 if (__vxtime.mode == VXTIME_TSC) {
85                         sync_core();
86                         rdtscll(t);
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. */ 
91                 } else {
92                         usec += ((readl((void *)fix_to_virt(VSYSCALL_HPET) + 0xf0) -
93                                   __vxtime.last) * __vxtime.quot) >> 32;
94                 }
95         } while (read_seqretry(&__xtime_lock, sequence));
96
97         tv->tv_sec = sec + usec / 1000000;
98         tv->tv_usec = usec % 1000000;
99 }
100
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)
103 {
104                 *tz = __sys_tz;
105 }
106
107
108 static force_inline int gettimeofday(struct timeval *tv, struct timezone *tz)
109 {
110         int ret;
111         asm volatile("syscall" 
112                 : "=a" (ret)
113                 : "0" (__NR_gettimeofday),"D" (tv),"S" (tz) : __syscall_clobber );
114         return ret;
115 }
116
117 static force_inline long time_syscall(long *t)
118 {
119         long secs;
120         asm volatile("syscall" 
121                 : "=a" (secs)
122                 : "0" (__NR_time),"D" (t) : __syscall_clobber);
123         return secs;
124 }
125
126 static int __vsyscall(0) vgettimeofday(struct timeval * tv, struct timezone * tz)
127 {
128         if (unlikely(!__sysctl_vsyscall))
129         return gettimeofday(tv,tz); 
130         if (tv)
131                 do_vgettimeofday(tv);
132         if (tz)
133                 do_get_tz(tz);
134         return 0;
135 }
136
137 /* This will break when the xtime seconds get inaccurate, but that is
138  * unlikely */
139 static time_t __vsyscall(1) vtime(time_t *t)
140 {
141         if (unlikely(!__sysctl_vsyscall))
142                 return time_syscall(t);
143         else if (t)
144                 *t = __xtime.tv_sec;            
145         return __xtime.tv_sec;
146 }
147
148 static long __vsyscall(2) venosys_0(void)
149 {
150         return -ENOSYS;
151 }
152
153 static long __vsyscall(3) venosys_1(void)
154 {
155         return -ENOSYS;
156
157 }
158
159 static void __init map_vsyscall(void)
160 {
161         extern char __vsyscall_0;
162         unsigned long physaddr_page0 = __pa_symbol(&__vsyscall_0);
163
164         __set_fixmap(VSYSCALL_FIRST_PAGE, physaddr_page0, PAGE_KERNEL_VSYSCALL);
165 }
166
167 static int __init vsyscall_init(void)
168 {
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)));
173         map_vsyscall();
174         sysctl_vsyscall = 1; 
175
176         return 0;
177 }
178
179 __initcall(vsyscall_init);