- Update Xen patches to 3.3-rc5 and c/s 1157.
[linux-flexiantxendom0-3.2.10.git] / arch / x86 / kernel / vsyscall_64-xen.c
1 /*
2  *  Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
3  *  Copyright 2003 Andi Kleen, SuSE Labs.
4  *
5  *  [ NOTE: this mechanism is now deprecated in favor of the vDSO. ]
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  *
17  *  Note: the concept clashes with user mode linux.  UML users should
18  *  use the vDSO.
19  */
20
21 #include <linux/time.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/timer.h>
25 #include <linux/seqlock.h>
26 #include <linux/jiffies.h>
27 #include <linux/sysctl.h>
28 #include <linux/topology.h>
29 #include <linux/clocksource.h>
30 #include <linux/getcpu.h>
31 #include <linux/cpu.h>
32 #include <linux/smp.h>
33 #include <linux/notifier.h>
34 #include <linux/syscalls.h>
35 #include <linux/ratelimit.h>
36
37 #include <asm/vsyscall.h>
38 #include <asm/pgtable.h>
39 #include <asm/compat.h>
40 #include <asm/page.h>
41 #include <asm/unistd.h>
42 #include <asm/fixmap.h>
43 #include <asm/errno.h>
44 #include <asm/io.h>
45 #include <asm/segment.h>
46 #include <asm/desc.h>
47 #include <asm/topology.h>
48 #include <asm/vgtod.h>
49 #include <asm/traps.h>
50
51 #define CREATE_TRACE_POINTS
52 #include "vsyscall_trace.h"
53
54 DEFINE_VVAR(int, vgetcpu_mode);
55 DEFINE_VVAR(struct vsyscall_gtod_data, vsyscall_gtod_data) =
56 {
57         .lock = __SEQLOCK_UNLOCKED(__vsyscall_gtod_data.lock),
58 };
59
60 static enum { EMULATE, NATIVE, NONE } vsyscall_mode = EMULATE;
61
62 static int __init vsyscall_setup(char *str)
63 {
64         if (str) {
65                 if (!strcmp("emulate", str))
66                         vsyscall_mode = EMULATE;
67                 else if (!strcmp("native", str))
68                         vsyscall_mode = NATIVE;
69                 else if (!strcmp("none", str))
70                         vsyscall_mode = NONE;
71                 else
72                         return -EINVAL;
73
74                 return 0;
75         }
76
77         return -EINVAL;
78 }
79 early_param("vsyscall", vsyscall_setup);
80
81 void update_vsyscall_tz(void)
82 {
83         unsigned long flags;
84
85         write_seqlock_irqsave(&vsyscall_gtod_data.lock, flags);
86         /* sys_tz has changed */
87         vsyscall_gtod_data.sys_tz = sys_tz;
88         write_sequnlock_irqrestore(&vsyscall_gtod_data.lock, flags);
89 }
90
91 void update_vsyscall(struct timespec *wall_time, struct timespec *wtm,
92                         struct clocksource *clock, u32 mult)
93 {
94         unsigned long flags;
95
96         write_seqlock_irqsave(&vsyscall_gtod_data.lock, flags);
97
98         /* copy vsyscall data */
99 #ifndef CONFIG_XEN
100         vsyscall_gtod_data.clock.vclock_mode    = clock->archdata.vclock_mode;
101 #endif
102         vsyscall_gtod_data.clock.cycle_last     = clock->cycle_last;
103         vsyscall_gtod_data.clock.mask           = clock->mask;
104         vsyscall_gtod_data.clock.mult           = mult;
105         vsyscall_gtod_data.clock.shift          = clock->shift;
106         vsyscall_gtod_data.wall_time_sec        = wall_time->tv_sec;
107         vsyscall_gtod_data.wall_time_nsec       = wall_time->tv_nsec;
108         vsyscall_gtod_data.wall_to_monotonic    = *wtm;
109         vsyscall_gtod_data.wall_time_coarse     = __current_kernel_time();
110
111         write_sequnlock_irqrestore(&vsyscall_gtod_data.lock, flags);
112 }
113
114 static void warn_bad_vsyscall(const char *level, struct pt_regs *regs,
115                               const char *message)
116 {
117         static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, DEFAULT_RATELIMIT_BURST);
118         struct task_struct *tsk;
119
120         if (!show_unhandled_signals || !__ratelimit(&rs))
121                 return;
122
123         tsk = current;
124
125         printk("%s%s[%d] %s ip:%lx cs:%lx sp:%lx ax:%lx si:%lx di:%lx\n",
126                level, tsk->comm, task_pid_nr(tsk),
127                message, regs->ip, regs->cs,
128                regs->sp, regs->ax, regs->si, regs->di);
129 }
130
131 static int addr_to_vsyscall_nr(unsigned long addr)
132 {
133         int nr;
134
135         if ((addr & ~0xC00UL) != VSYSCALL_START)
136                 return -EINVAL;
137
138         nr = (addr & 0xC00UL) >> 10;
139         if (nr >= 3)
140                 return -EINVAL;
141
142         return nr;
143 }
144
145 static bool write_ok_or_segv(unsigned long ptr, size_t size)
146 {
147         /*
148          * XXX: if access_ok, get_user, and put_user handled
149          * sig_on_uaccess_error, this could go away.
150          */
151
152         if (!access_ok(VERIFY_WRITE, (void __user *)ptr, size)) {
153                 siginfo_t info;
154                 struct thread_struct *thread = &current->thread;
155
156                 thread->error_code      = 6;  /* user fault, no page, write */
157                 thread->cr2             = ptr;
158                 thread->trap_no         = 14;
159
160                 memset(&info, 0, sizeof(info));
161                 info.si_signo           = SIGSEGV;
162                 info.si_errno           = 0;
163                 info.si_code            = SEGV_MAPERR;
164                 info.si_addr            = (void __user *)ptr;
165
166                 force_sig_info(SIGSEGV, &info, current);
167                 return false;
168         } else {
169                 return true;
170         }
171 }
172
173 bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
174 {
175         struct task_struct *tsk;
176         unsigned long caller;
177         int vsyscall_nr;
178         int prev_sig_on_uaccess_error;
179         long ret;
180
181         /*
182          * No point in checking CS -- the only way to get here is a user mode
183          * trap to a high address, which means that we're in 64-bit user code.
184          */
185
186         WARN_ON_ONCE(address != regs->ip);
187
188         if (vsyscall_mode == NONE) {
189                 warn_bad_vsyscall(KERN_INFO, regs,
190                                   "vsyscall attempted with vsyscall=none");
191                 return false;
192         }
193
194         vsyscall_nr = addr_to_vsyscall_nr(address);
195
196         trace_emulate_vsyscall(vsyscall_nr);
197
198         if (vsyscall_nr < 0) {
199                 warn_bad_vsyscall(KERN_WARNING, regs,
200                                   "misaligned vsyscall (exploit attempt or buggy program) -- look up the vsyscall kernel parameter if you need a workaround");
201                 goto sigsegv;
202         }
203
204         if (get_user(caller, (unsigned long __user *)regs->sp) != 0) {
205                 warn_bad_vsyscall(KERN_WARNING, regs,
206                                   "vsyscall with bad stack (exploit attempt?)");
207                 goto sigsegv;
208         }
209
210         tsk = current;
211         if (seccomp_mode(&tsk->seccomp))
212                 do_exit(SIGKILL);
213
214         /*
215          * With a real vsyscall, page faults cause SIGSEGV.  We want to
216          * preserve that behavior to make writing exploits harder.
217          */
218         prev_sig_on_uaccess_error = current_thread_info()->sig_on_uaccess_error;
219         current_thread_info()->sig_on_uaccess_error = 1;
220
221         /*
222          * 0 is a valid user pointer (in the access_ok sense) on 32-bit and
223          * 64-bit, so we don't need to special-case it here.  For all the
224          * vsyscalls, 0 means "don't write anything" not "write it at
225          * address 0".
226          */
227         ret = -EFAULT;
228         switch (vsyscall_nr) {
229         case 0:
230                 if (!write_ok_or_segv(regs->di, sizeof(struct timeval)) ||
231                     !write_ok_or_segv(regs->si, sizeof(struct timezone)))
232                         break;
233
234                 ret = sys_gettimeofday(
235                         (struct timeval __user *)regs->di,
236                         (struct timezone __user *)regs->si);
237                 break;
238
239         case 1:
240                 if (!write_ok_or_segv(regs->di, sizeof(time_t)))
241                         break;
242
243                 ret = sys_time((time_t __user *)regs->di);
244                 break;
245
246         case 2:
247                 if (!write_ok_or_segv(regs->di, sizeof(unsigned)) ||
248                     !write_ok_or_segv(regs->si, sizeof(unsigned)))
249                         break;
250
251                 ret = sys_getcpu((unsigned __user *)regs->di,
252                                  (unsigned __user *)regs->si,
253                                  0);
254                 break;
255         default:
256                 ret = -ENOSYS;
257                 break;
258         }
259
260         current_thread_info()->sig_on_uaccess_error = prev_sig_on_uaccess_error;
261
262         if (ret == -EFAULT) {
263                 /* Bad news -- userspace fed a bad pointer to a vsyscall. */
264                 warn_bad_vsyscall(KERN_INFO, regs,
265                                   "vsyscall fault (exploit attempt?)");
266
267                 /*
268                  * If we failed to generate a signal for any reason,
269                  * generate one here.  (This should be impossible.)
270                  */
271                 if (WARN_ON_ONCE(!sigismember(&tsk->pending.signal, SIGBUS) &&
272                                  !sigismember(&tsk->pending.signal, SIGSEGV)))
273                         goto sigsegv;
274
275                 return true;  /* Don't emulate the ret. */
276         }
277
278         regs->ax = ret;
279
280         /* Emulate a ret instruction. */
281         regs->ip = caller;
282         regs->sp += 8;
283
284         return true;
285
286 sigsegv:
287         force_sig(SIGSEGV, current);
288         return true;
289 }
290
291 /*
292  * Assume __initcall executes before all user space. Hopefully kmod
293  * doesn't violate that. We'll find out if it does.
294  */
295 static void __cpuinit vsyscall_set_cpu(int cpu)
296 {
297         unsigned long d;
298         unsigned long node = 0;
299 #ifdef CONFIG_NUMA
300         node = cpu_to_node(cpu);
301 #endif
302         if (cpu_has(&cpu_data(cpu), X86_FEATURE_RDTSCP))
303                 write_rdtscp_aux((node << 12) | cpu);
304
305         /*
306          * Store cpu number in limit so that it can be loaded quickly
307          * in user space in vgetcpu. (12 bits for the CPU and 8 bits for the node)
308          */
309         d = 0x0f40000000000ULL;
310         d |= cpu;
311         d |= (node & 0xf) << 12;
312         d |= (node >> 4) << 48;
313
314         write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_PER_CPU, &d, DESCTYPE_S);
315 }
316
317 static void __cpuinit cpu_vsyscall_init(void *arg)
318 {
319         /* preemption should be already off */
320         vsyscall_set_cpu(raw_smp_processor_id());
321 }
322
323 static int __cpuinit
324 cpu_vsyscall_notifier(struct notifier_block *n, unsigned long action, void *arg)
325 {
326         long cpu = (long)arg;
327
328         if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
329                 smp_call_function_single(cpu, cpu_vsyscall_init, NULL, 1);
330
331         return NOTIFY_DONE;
332 }
333
334 void __init map_vsyscall(void)
335 {
336         extern char __vsyscall_page;
337         unsigned long physaddr_vsyscall = __pa_symbol(&__vsyscall_page);
338         extern char __vvar_page;
339         unsigned long physaddr_vvar_page = __pa_symbol(&__vvar_page);
340
341         __set_fixmap(VSYSCALL_FIRST_PAGE, physaddr_vsyscall,
342                      vsyscall_mode == NATIVE
343                      ? PAGE_KERNEL_VSYSCALL
344                      : PAGE_KERNEL_VVAR);
345         BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_FIRST_PAGE) !=
346                      (unsigned long)VSYSCALL_START);
347
348         __set_fixmap(VVAR_PAGE, physaddr_vvar_page, PAGE_KERNEL_VVAR);
349         BUILD_BUG_ON((unsigned long)__fix_to_virt(VVAR_PAGE) !=
350                      (unsigned long)VVAR_ADDRESS);
351 }
352
353 static int __init vsyscall_init(void)
354 {
355         BUG_ON(VSYSCALL_ADDR(0) != __fix_to_virt(VSYSCALL_FIRST_PAGE));
356
357         on_each_cpu(cpu_vsyscall_init, NULL, 1);
358         /* notifier priority > KVM */
359         hotcpu_notifier(cpu_vsyscall_notifier, 30);
360
361         return 0;
362 }
363 __initcall(vsyscall_init);