import of upstream 2.4.34.4 from kernel.org
[linux-2.4.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  *
6  *  Thanks to hpa@transmeta.com for some useful hint.
7  *  Special thanks to Ingo Molnar for his early experience with
8  *  a different vsyscall implementation for Linux/IA32 and for the name.
9  *
10  *  vsyscall 1 is located at -10Mbyte, vsyscall 2 is located
11  *  at virtual address -10Mbyte+1024bytes etc... There are at max 8192
12  *  vsyscalls. One vsyscall can reserve more than 1 slot to avoid
13  *  jumping out of line if necessary.
14  *
15  *  $Id: vsyscall.c,v 1.26 2003/02/18 11:55:47 ak Exp $
16  */
17
18 /*
19  * TODO 2001-03-20:
20  *
21  * 1) make page fault handler detect faults on page1-page-last of the vsyscall
22  *    virtual space, and make it increase %rip and write -ENOSYS in %rax (so
23  *    we'll be able to upgrade to a new glibc without upgrading kernel after
24  *    we add more vsyscalls.
25  * 2) Possibly we need a fixmap table for the vsyscalls too if we want
26  *    to avoid SIGSEGV and we want to return -EFAULT from the vsyscalls as well.
27  *    Can we segfault inside a "syscall"? We can fix this anytime and those fixes
28  *    won't be visible for userspace. Not fixing this is a noop for correct programs,
29  *    broken programs will segfault and there's no security risk until we choose to
30  *    fix it.
31  *
32  * These are not urgent things that we need to address only before shipping the first
33  * production binary kernels.
34  */
35
36 #include <linux/time.h>
37 #include <linux/init.h>
38 #include <linux/kernel.h>
39 #include <linux/mm.h>
40
41 #include <asm/vsyscall.h>
42 #include <asm/pgtable.h>
43 #include <asm/page.h>
44 #include <asm/fixmap.h>
45 #include <asm/errno.h>
46 #include <asm/io.h>
47 #include <asm/msr.h>
48 #include <asm/unistd.h>
49
50 #define __vsyscall(nr) __attribute__ ((unused,__section__(".vsyscall_" #nr)))
51
52 #define force_inline inline __attribute__((always_inline))
53
54 long __vxtime_sequence[2] __section_vxtime_sequence;
55
56 static force_inline void do_vgettimeofday(struct timeval * tv)
57 {
58         long sequence, t;
59         unsigned long sec, usec;
60
61         do {
62                 sequence = __vxtime_sequence[1];
63                 rmb();
64
65                 sec = __xtime.tv_sec;
66                 usec = __xtime.tv_usec + (__jiffies - __wall_jiffies) * (1000000 / HZ);
67
68                 switch (__vxtime.mode) {
69
70                         case VXTIME_TSC:
71                                 sync_core();
72                                 rdtscll(t);
73                                 usec += (((t  - __vxtime.last_tsc) * __vxtime.tsc_quot) >> 32);
74                                 break;
75
76                         case VXTIME_HPET:
77                                 usec += ((readl(fix_to_virt(VSYSCALL_HPET) + 0xf0) - __vxtime.last) * __vxtime.quot) >> 32;
78                                 break;
79
80                 }
81
82                 rmb();
83         } while (sequence != __vxtime_sequence[0]);
84
85         tv->tv_sec = sec + usec / 1000000;
86         tv->tv_usec = usec % 1000000;
87 }
88
89
90 static force_inline void do_get_tz(struct timezone * tz)
91 {
92         long sequence;
93
94         do {
95                 sequence = __vxtime_sequence[1];
96                 rmb();
97
98                 *tz = __sys_tz;
99
100                 rmb();
101         } while (sequence != __vxtime_sequence[0]);
102 }
103
104 static long __vsyscall(0) vgettimeofday(struct timeval * tv, struct timezone * tz)
105 {
106         if (tv)
107                         do_vgettimeofday(tv);
108
109         if (tz)
110                 do_get_tz(tz);
111
112         return 0;
113 }
114
115 static time_t __vsyscall(1) vtime(time_t * tp)
116 {
117         struct timeval tv;
118         vgettimeofday(&tv, NULL);
119         if (tp) *tp = tv.tv_sec;
120         return tv.tv_sec;
121 }
122
123 static long __vsyscall(2) venosys_0(void)
124 {
125         return -ENOSYS;
126 }
127
128 static long __vsyscall(3) venosys_1(void)
129 {
130         return -ENOSYS;
131 }
132
133 extern char vsyscall_syscall[], __vsyscall_0[];
134
135 static void __init map_vsyscall(void)
136 {
137         unsigned long physaddr_page0 = __pa_symbol(&__vsyscall_0);
138         __set_fixmap(VSYSCALL_FIRST_PAGE, physaddr_page0, PAGE_KERNEL_VSYSCALL);
139         if (hpet_address)
140                 __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VSYSCALL);
141 }
142
143 static int __init vsyscall_init(void)
144 {
145         if ((unsigned long) &vgettimeofday != VSYSCALL_ADDR(__NR_vgettimeofday))
146                 panic("vgettimeofday link addr broken");
147         if ((unsigned long) &vtime != VSYSCALL_ADDR(__NR_vtime))
148                 panic("vtime link addr broken");
149         if (VSYSCALL_ADDR(0) != __fix_to_virt(VSYSCALL_FIRST_PAGE))
150                 panic("fixmap first vsyscall %lx should be %lx", __fix_to_virt(VSYSCALL_FIRST_PAGE),
151                         VSYSCALL_ADDR(0));
152         map_vsyscall();
153         return 0;
154 }
155
156 __initcall(vsyscall_init);