Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 4 |
| 13 | * vsyscalls. One vsyscall can reserve more than 1 slot to avoid |
| 14 | * jumping out of line if necessary. We cannot add more with this |
| 15 | * mechanism because older kernels won't return -ENOSYS. |
| 16 | * If we want more than four we need a vDSO. |
| 17 | * |
| 18 | * Note: the concept clashes with user mode linux. If you use UML and |
| 19 | * want per guest time just set the kernel.vsyscall64 sysctl to 0. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/time.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/timer.h> |
| 26 | #include <linux/seqlock.h> |
| 27 | #include <linux/jiffies.h> |
| 28 | #include <linux/sysctl.h> |
Vojtech Pavlik | c08c820 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 29 | #include <linux/getcpu.h> |
Andi Kleen | 8c131af | 2006-11-14 16:57:46 +0100 | [diff] [blame] | 30 | #include <linux/cpu.h> |
| 31 | #include <linux/smp.h> |
| 32 | #include <linux/notifier.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | #include <asm/vsyscall.h> |
| 35 | #include <asm/pgtable.h> |
| 36 | #include <asm/page.h> |
| 37 | #include <asm/fixmap.h> |
| 38 | #include <asm/errno.h> |
| 39 | #include <asm/io.h> |
Vojtech Pavlik | c08c820 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 40 | #include <asm/segment.h> |
| 41 | #include <asm/desc.h> |
| 42 | #include <asm/topology.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #define __vsyscall(nr) __attribute__ ((unused,__section__(".vsyscall_" #nr))) |
Arnd Bergmann | f5738ce | 2006-12-06 20:37:29 -0800 | [diff] [blame^] | 45 | #define __syscall_clobber "r11","rcx","memory" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | int __sysctl_vsyscall __section_sysctl_vsyscall = 1; |
| 48 | seqlock_t __xtime_lock __section_xtime_lock = SEQLOCK_UNLOCKED; |
Vojtech Pavlik | c08c820 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 49 | int __vgetcpu_mode __section_vgetcpu_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | #include <asm/unistd.h> |
| 52 | |
Andi Kleen | 2c8bc94 | 2006-01-11 22:45:30 +0100 | [diff] [blame] | 53 | static __always_inline void timeval_normalize(struct timeval * tv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
| 55 | time_t __sec; |
| 56 | |
| 57 | __sec = tv->tv_usec / 1000000; |
| 58 | if (__sec) { |
| 59 | tv->tv_usec %= 1000000; |
| 60 | tv->tv_sec += __sec; |
| 61 | } |
| 62 | } |
| 63 | |
Andi Kleen | 2c8bc94 | 2006-01-11 22:45:30 +0100 | [diff] [blame] | 64 | static __always_inline void do_vgettimeofday(struct timeval * tv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { |
| 66 | long sequence, t; |
| 67 | unsigned long sec, usec; |
| 68 | |
| 69 | do { |
| 70 | sequence = read_seqbegin(&__xtime_lock); |
| 71 | |
| 72 | sec = __xtime.tv_sec; |
Atsushi Nemoto | 8ef3860 | 2006-09-30 23:28:31 -0700 | [diff] [blame] | 73 | usec = __xtime.tv_nsec / 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Andi Kleen | 312df5f | 2005-05-16 21:53:28 -0700 | [diff] [blame] | 75 | if (__vxtime.mode != VXTIME_HPET) { |
Andi Kleen | c818a18 | 2006-01-11 22:45:24 +0100 | [diff] [blame] | 76 | t = get_cycles_sync(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | if (t < __vxtime.last_tsc) |
| 78 | t = __vxtime.last_tsc; |
| 79 | usec += ((t - __vxtime.last_tsc) * |
| 80 | __vxtime.tsc_quot) >> 32; |
| 81 | /* See comment in x86_64 do_gettimeofday. */ |
| 82 | } else { |
Andi Kleen | 131cfd7 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 83 | usec += ((readl((void __iomem *) |
| 84 | fix_to_virt(VSYSCALL_HPET) + 0xf0) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | __vxtime.last) * __vxtime.quot) >> 32; |
| 86 | } |
| 87 | } while (read_seqretry(&__xtime_lock, sequence)); |
| 88 | |
| 89 | tv->tv_sec = sec + usec / 1000000; |
| 90 | tv->tv_usec = usec % 1000000; |
| 91 | } |
| 92 | |
| 93 | /* RED-PEN may want to readd seq locking, but then the variable should be write-once. */ |
Andi Kleen | 2c8bc94 | 2006-01-11 22:45:30 +0100 | [diff] [blame] | 94 | static __always_inline void do_get_tz(struct timezone * tz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
| 96 | *tz = __sys_tz; |
| 97 | } |
| 98 | |
Andi Kleen | 2c8bc94 | 2006-01-11 22:45:30 +0100 | [diff] [blame] | 99 | static __always_inline int gettimeofday(struct timeval *tv, struct timezone *tz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
| 101 | int ret; |
| 102 | asm volatile("vsysc2: syscall" |
| 103 | : "=a" (ret) |
| 104 | : "0" (__NR_gettimeofday),"D" (tv),"S" (tz) : __syscall_clobber ); |
| 105 | return ret; |
| 106 | } |
| 107 | |
Andi Kleen | 2c8bc94 | 2006-01-11 22:45:30 +0100 | [diff] [blame] | 108 | static __always_inline long time_syscall(long *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
| 110 | long secs; |
| 111 | asm volatile("vsysc1: syscall" |
| 112 | : "=a" (secs) |
| 113 | : "0" (__NR_time),"D" (t) : __syscall_clobber); |
| 114 | return secs; |
| 115 | } |
| 116 | |
Andi Kleen | 2e8ad43 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 117 | int __vsyscall(0) vgettimeofday(struct timeval * tv, struct timezone * tz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
Ingo Molnar | 14118c3 | 2006-06-26 13:56:58 +0200 | [diff] [blame] | 119 | if (!__sysctl_vsyscall) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | return gettimeofday(tv,tz); |
| 121 | if (tv) |
| 122 | do_vgettimeofday(tv); |
| 123 | if (tz) |
| 124 | do_get_tz(tz); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | /* This will break when the xtime seconds get inaccurate, but that is |
| 129 | * unlikely */ |
Andi Kleen | 2e8ad43 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 130 | time_t __vsyscall(1) vtime(time_t *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
Ingo Molnar | 14118c3 | 2006-06-26 13:56:58 +0200 | [diff] [blame] | 132 | if (!__sysctl_vsyscall) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | return time_syscall(t); |
| 134 | else if (t) |
| 135 | *t = __xtime.tv_sec; |
| 136 | return __xtime.tv_sec; |
| 137 | } |
| 138 | |
Vojtech Pavlik | c08c820 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 139 | /* Fast way to get current CPU and node. |
| 140 | This helps to do per node and per CPU caches in user space. |
| 141 | The result is not guaranteed without CPU affinity, but usually |
| 142 | works out because the scheduler tries to keep a thread on the same |
| 143 | CPU. |
| 144 | |
| 145 | tcache must point to a two element sized long array. |
| 146 | All arguments can be NULL. */ |
| 147 | long __vsyscall(2) |
| 148 | vgetcpu(unsigned *cpu, unsigned *node, struct getcpu_cache *tcache) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
Vojtech Pavlik | c08c820 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 150 | unsigned int dummy, p; |
| 151 | unsigned long j = 0; |
| 152 | |
| 153 | /* Fast cache - only recompute value once per jiffies and avoid |
| 154 | relatively costly rdtscp/cpuid otherwise. |
| 155 | This works because the scheduler usually keeps the process |
| 156 | on the same CPU and this syscall doesn't guarantee its |
| 157 | results anyways. |
| 158 | We do this here because otherwise user space would do it on |
| 159 | its own in a likely inferior way (no access to jiffies). |
| 160 | If you don't like it pass NULL. */ |
Andi Kleen | 34596dc | 2006-09-30 01:47:55 +0200 | [diff] [blame] | 161 | if (tcache && tcache->blob[0] == (j = __jiffies)) { |
| 162 | p = tcache->blob[1]; |
Vojtech Pavlik | c08c820 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 163 | } else if (__vgetcpu_mode == VGETCPU_RDTSCP) { |
| 164 | /* Load per CPU data from RDTSCP */ |
| 165 | rdtscp(dummy, dummy, p); |
| 166 | } else { |
| 167 | /* Load per CPU data from GDT */ |
| 168 | asm("lsl %1,%0" : "=r" (p) : "r" (__PER_CPU_SEG)); |
| 169 | } |
| 170 | if (tcache) { |
Andi Kleen | 34596dc | 2006-09-30 01:47:55 +0200 | [diff] [blame] | 171 | tcache->blob[0] = j; |
| 172 | tcache->blob[1] = p; |
Vojtech Pavlik | c08c820 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 173 | } |
| 174 | if (cpu) |
| 175 | *cpu = p & 0xfff; |
| 176 | if (node) |
| 177 | *node = p >> 12; |
| 178 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Andi Kleen | 2e8ad43 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 181 | long __vsyscall(3) venosys_1(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
| 183 | return -ENOSYS; |
| 184 | } |
| 185 | |
| 186 | #ifdef CONFIG_SYSCTL |
| 187 | |
| 188 | #define SYSCALL 0x050f |
| 189 | #define NOP2 0x9090 |
| 190 | |
| 191 | /* |
| 192 | * NOP out syscall in vsyscall page when not needed. |
| 193 | */ |
| 194 | static int vsyscall_sysctl_change(ctl_table *ctl, int write, struct file * filp, |
| 195 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 196 | { |
| 197 | extern u16 vsysc1, vsysc2; |
Andi Kleen | 131cfd7 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 198 | u16 __iomem *map1; |
| 199 | u16 __iomem *map2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos); |
| 201 | if (!write) |
| 202 | return ret; |
| 203 | /* gcc has some trouble with __va(__pa()), so just do it this |
| 204 | way. */ |
| 205 | map1 = ioremap(__pa_symbol(&vsysc1), 2); |
| 206 | if (!map1) |
| 207 | return -ENOMEM; |
| 208 | map2 = ioremap(__pa_symbol(&vsysc2), 2); |
| 209 | if (!map2) { |
| 210 | ret = -ENOMEM; |
| 211 | goto out; |
| 212 | } |
| 213 | if (!sysctl_vsyscall) { |
Andi Kleen | 131cfd7 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 214 | writew(SYSCALL, map1); |
| 215 | writew(SYSCALL, map2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } else { |
Andi Kleen | 131cfd7 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 217 | writew(NOP2, map1); |
| 218 | writew(NOP2, map2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |
| 220 | iounmap(map2); |
| 221 | out: |
| 222 | iounmap(map1); |
| 223 | return ret; |
| 224 | } |
| 225 | |
| 226 | static int vsyscall_sysctl_nostrat(ctl_table *t, int __user *name, int nlen, |
| 227 | void __user *oldval, size_t __user *oldlenp, |
| 228 | void __user *newval, size_t newlen, |
| 229 | void **context) |
| 230 | { |
| 231 | return -ENOSYS; |
| 232 | } |
| 233 | |
| 234 | static ctl_table kernel_table2[] = { |
| 235 | { .ctl_name = 99, .procname = "vsyscall64", |
| 236 | .data = &sysctl_vsyscall, .maxlen = sizeof(int), .mode = 0644, |
| 237 | .strategy = vsyscall_sysctl_nostrat, |
| 238 | .proc_handler = vsyscall_sysctl_change }, |
| 239 | { 0, } |
| 240 | }; |
| 241 | |
| 242 | static ctl_table kernel_root_table2[] = { |
| 243 | { .ctl_name = CTL_KERN, .procname = "kernel", .mode = 0555, |
| 244 | .child = kernel_table2 }, |
| 245 | { 0 }, |
| 246 | }; |
| 247 | |
| 248 | #endif |
| 249 | |
Andi Kleen | 8c131af | 2006-11-14 16:57:46 +0100 | [diff] [blame] | 250 | /* Assume __initcall executes before all user space. Hopefully kmod |
| 251 | doesn't violate that. We'll find out if it does. */ |
| 252 | static void __cpuinit vsyscall_set_cpu(int cpu) |
Vojtech Pavlik | c08c820 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 253 | { |
| 254 | unsigned long *d; |
| 255 | unsigned long node = 0; |
| 256 | #ifdef CONFIG_NUMA |
| 257 | node = cpu_to_node[cpu]; |
| 258 | #endif |
Andi Kleen | 8c131af | 2006-11-14 16:57:46 +0100 | [diff] [blame] | 259 | if (cpu_has(&cpu_data[cpu], X86_FEATURE_RDTSCP)) |
| 260 | write_rdtscp_aux((node << 12) | cpu); |
Vojtech Pavlik | c08c820 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 261 | |
| 262 | /* Store cpu number in limit so that it can be loaded quickly |
| 263 | in user space in vgetcpu. |
| 264 | 12 bits for the CPU and 8 bits for the node. */ |
| 265 | d = (unsigned long *)(cpu_gdt(cpu) + GDT_ENTRY_PER_CPU); |
| 266 | *d = 0x0f40000000000ULL; |
| 267 | *d |= cpu; |
| 268 | *d |= (node & 0xf) << 12; |
| 269 | *d |= (node >> 4) << 48; |
| 270 | } |
| 271 | |
Andi Kleen | 8c131af | 2006-11-14 16:57:46 +0100 | [diff] [blame] | 272 | static void __cpuinit cpu_vsyscall_init(void *arg) |
| 273 | { |
| 274 | /* preemption should be already off */ |
| 275 | vsyscall_set_cpu(raw_smp_processor_id()); |
| 276 | } |
| 277 | |
Andi Kleen | 6b3d1a9 | 2006-11-16 10:22:03 +0100 | [diff] [blame] | 278 | #ifdef CONFIG_HOTPLUG_CPU |
Andi Kleen | 8c131af | 2006-11-14 16:57:46 +0100 | [diff] [blame] | 279 | static int __cpuinit |
| 280 | cpu_vsyscall_notifier(struct notifier_block *n, unsigned long action, void *arg) |
| 281 | { |
| 282 | long cpu = (long)arg; |
| 283 | if (action == CPU_ONLINE) |
| 284 | smp_call_function_single(cpu, cpu_vsyscall_init, NULL, 0, 1); |
| 285 | return NOTIFY_DONE; |
| 286 | } |
Andi Kleen | 6b3d1a9 | 2006-11-16 10:22:03 +0100 | [diff] [blame] | 287 | #endif |
Andi Kleen | 8c131af | 2006-11-14 16:57:46 +0100 | [diff] [blame] | 288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | static void __init map_vsyscall(void) |
| 290 | { |
| 291 | extern char __vsyscall_0; |
| 292 | unsigned long physaddr_page0 = __pa_symbol(&__vsyscall_0); |
| 293 | |
| 294 | __set_fixmap(VSYSCALL_FIRST_PAGE, physaddr_page0, PAGE_KERNEL_VSYSCALL); |
| 295 | } |
| 296 | |
| 297 | static int __init vsyscall_init(void) |
| 298 | { |
| 299 | BUG_ON(((unsigned long) &vgettimeofday != |
| 300 | VSYSCALL_ADDR(__NR_vgettimeofday))); |
| 301 | BUG_ON((unsigned long) &vtime != VSYSCALL_ADDR(__NR_vtime)); |
| 302 | BUG_ON((VSYSCALL_ADDR(0) != __fix_to_virt(VSYSCALL_FIRST_PAGE))); |
Vojtech Pavlik | c08c820 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 303 | BUG_ON((unsigned long) &vgetcpu != VSYSCALL_ADDR(__NR_vgetcpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | map_vsyscall(); |
Andi Kleen | f3c5f5e | 2005-05-16 21:53:33 -0700 | [diff] [blame] | 305 | #ifdef CONFIG_SYSCTL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | register_sysctl_table(kernel_root_table2, 0); |
Andi Kleen | f3c5f5e | 2005-05-16 21:53:33 -0700 | [diff] [blame] | 307 | #endif |
Andi Kleen | 8c131af | 2006-11-14 16:57:46 +0100 | [diff] [blame] | 308 | on_each_cpu(cpu_vsyscall_init, NULL, 0, 1); |
| 309 | hotcpu_notifier(cpu_vsyscall_notifier, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | __initcall(vsyscall_init); |