pandora: defconfig: update
[pandora-kernel.git] / arch / x86 / kernel / vsyscall_64.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 = NATIVE;
61 unsigned long vsyscall_pgprot = __PAGE_KERNEL_VSYSCALL;
62
63 static int __init vsyscall_setup(char *str)
64 {
65         if (str) {
66                 if (!strcmp("emulate", str))
67                         vsyscall_mode = EMULATE;
68                 else if (!strcmp("native", str))
69                         vsyscall_mode = NATIVE;
70                 else if (!strcmp("none", str))
71                         vsyscall_mode = NONE;
72                 else
73                         return -EINVAL;
74
75                 return 0;
76         }
77
78         return -EINVAL;
79 }
80 early_param("vsyscall", vsyscall_setup);
81
82 void update_vsyscall_tz(void)
83 {
84         unsigned long flags;
85
86         write_seqlock_irqsave(&vsyscall_gtod_data.lock, flags);
87         /* sys_tz has changed */
88         vsyscall_gtod_data.sys_tz = sys_tz;
89         write_sequnlock_irqrestore(&vsyscall_gtod_data.lock, flags);
90 }
91
92 void update_vsyscall(struct timespec *wall_time, struct timespec *wtm,
93                         struct clocksource *clock, u32 mult)
94 {
95         unsigned long flags;
96
97         write_seqlock_irqsave(&vsyscall_gtod_data.lock, flags);
98
99         /* copy vsyscall data */
100         vsyscall_gtod_data.clock.vclock_mode    = clock->archdata.vclock_mode;
101         vsyscall_gtod_data.clock.cycle_last     = clock->cycle_last;
102         vsyscall_gtod_data.clock.mask           = clock->mask;
103         vsyscall_gtod_data.clock.mult           = mult;
104         vsyscall_gtod_data.clock.shift          = clock->shift;
105         vsyscall_gtod_data.wall_time_sec        = wall_time->tv_sec;
106         vsyscall_gtod_data.wall_time_nsec       = wall_time->tv_nsec;
107         vsyscall_gtod_data.wall_to_monotonic    = *wtm;
108         vsyscall_gtod_data.wall_time_coarse     = __current_kernel_time();
109
110         write_sequnlock_irqrestore(&vsyscall_gtod_data.lock, flags);
111 }
112
113 static void warn_bad_vsyscall(const char *level, struct pt_regs *regs,
114                               const char *message)
115 {
116         static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, DEFAULT_RATELIMIT_BURST);
117         struct task_struct *tsk;
118
119         if (!show_unhandled_signals || !__ratelimit(&rs))
120                 return;
121
122         tsk = current;
123
124         printk("%s%s[%d] %s ip:%lx cs:%lx sp:%lx ax:%lx si:%lx di:%lx\n",
125                level, tsk->comm, task_pid_nr(tsk),
126                message, regs->ip, regs->cs,
127                regs->sp, regs->ax, regs->si, regs->di);
128 }
129
130 static int addr_to_vsyscall_nr(unsigned long addr)
131 {
132         int nr;
133
134         if ((addr & ~0xC00UL) != VSYSCALL_START)
135                 return -EINVAL;
136
137         nr = (addr & 0xC00UL) >> 10;
138         if (nr >= 3)
139                 return -EINVAL;
140
141         return nr;
142 }
143
144 bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
145 {
146         struct task_struct *tsk;
147         unsigned long caller;
148         int vsyscall_nr;
149         long ret;
150
151         /*
152          * No point in checking CS -- the only way to get here is a user mode
153          * trap to a high address, which means that we're in 64-bit user code.
154          */
155
156         WARN_ON_ONCE(address != regs->ip);
157
158         if (vsyscall_mode == NONE) {
159                 warn_bad_vsyscall(KERN_INFO, regs,
160                                   "vsyscall attempted with vsyscall=none");
161                 return false;
162         }
163
164         vsyscall_nr = addr_to_vsyscall_nr(address);
165
166         trace_emulate_vsyscall(vsyscall_nr);
167
168         if (vsyscall_nr < 0) {
169                 warn_bad_vsyscall(KERN_WARNING, regs,
170                                   "misaligned vsyscall (exploit attempt or buggy program) -- look up the vsyscall kernel parameter if you need a workaround");
171                 goto sigsegv;
172         }
173
174         if (get_user(caller, (unsigned long __user *)regs->sp) != 0) {
175                 warn_bad_vsyscall(KERN_WARNING, regs,
176                                   "vsyscall with bad stack (exploit attempt?)");
177                 goto sigsegv;
178         }
179
180         tsk = current;
181         if (seccomp_mode(&tsk->seccomp))
182                 do_exit(SIGKILL);
183
184         switch (vsyscall_nr) {
185         case 0:
186                 ret = sys_gettimeofday(
187                         (struct timeval __user *)regs->di,
188                         (struct timezone __user *)regs->si);
189                 break;
190
191         case 1:
192                 ret = sys_time((time_t __user *)regs->di);
193                 break;
194
195         case 2:
196                 ret = sys_getcpu((unsigned __user *)regs->di,
197                                  (unsigned __user *)regs->si,
198                                  0);
199                 break;
200         }
201
202         if (ret == -EFAULT) {
203                 /*
204                  * Bad news -- userspace fed a bad pointer to a vsyscall.
205                  *
206                  * With a real vsyscall, that would have caused SIGSEGV.
207                  * To make writing reliable exploits using the emulated
208                  * vsyscalls harder, generate SIGSEGV here as well.
209                  */
210                 warn_bad_vsyscall(KERN_INFO, regs,
211                                   "vsyscall fault (exploit attempt?)");
212                 goto sigsegv;
213         }
214
215         regs->ax = ret;
216
217         /* Emulate a ret instruction. */
218         regs->ip = caller;
219         regs->sp += 8;
220
221         return true;
222
223 sigsegv:
224         force_sig(SIGSEGV, current);
225         return true;
226 }
227
228 /*
229  * Assume __initcall executes before all user space. Hopefully kmod
230  * doesn't violate that. We'll find out if it does.
231  */
232 static void __cpuinit vsyscall_set_cpu(int cpu)
233 {
234         unsigned long d;
235         unsigned long node = 0;
236 #ifdef CONFIG_NUMA
237         node = cpu_to_node(cpu);
238 #endif
239         if (cpu_has(&cpu_data(cpu), X86_FEATURE_RDTSCP))
240                 write_rdtscp_aux((node << 12) | cpu);
241
242         /*
243          * Store cpu number in limit so that it can be loaded quickly
244          * in user space in vgetcpu. (12 bits for the CPU and 8 bits for the node)
245          */
246         d = 0x0f40000000000ULL;
247         d |= cpu;
248         d |= (node & 0xf) << 12;
249         d |= (node >> 4) << 48;
250
251         write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_PER_CPU, &d, DESCTYPE_S);
252 }
253
254 static void __cpuinit cpu_vsyscall_init(void *arg)
255 {
256         /* preemption should be already off */
257         vsyscall_set_cpu(raw_smp_processor_id());
258 }
259
260 static int __cpuinit
261 cpu_vsyscall_notifier(struct notifier_block *n, unsigned long action, void *arg)
262 {
263         long cpu = (long)arg;
264
265         if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
266                 smp_call_function_single(cpu, cpu_vsyscall_init, NULL, 1);
267
268         return NOTIFY_DONE;
269 }
270
271 void __init map_vsyscall(void)
272 {
273         extern char __vsyscall_page;
274         unsigned long physaddr_vsyscall = __pa_symbol(&__vsyscall_page);
275         extern char __vvar_page;
276         unsigned long physaddr_vvar_page = __pa_symbol(&__vvar_page);
277
278         if (vsyscall_mode != NATIVE)
279                 vsyscall_pgprot = __PAGE_KERNEL_VVAR;
280         __set_fixmap(VSYSCALL_FIRST_PAGE, physaddr_vsyscall,
281                      __pgprot(vsyscall_pgprot));
282         BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_FIRST_PAGE) !=
283                      (unsigned long)VSYSCALL_START);
284
285         __set_fixmap(VVAR_PAGE, physaddr_vvar_page, PAGE_KERNEL_VVAR);
286         BUILD_BUG_ON((unsigned long)__fix_to_virt(VVAR_PAGE) !=
287                      (unsigned long)VVAR_ADDRESS);
288 }
289
290 static int __init vsyscall_init(void)
291 {
292         BUG_ON(VSYSCALL_ADDR(0) != __fix_to_virt(VSYSCALL_FIRST_PAGE));
293
294         on_each_cpu(cpu_vsyscall_init, NULL, 1);
295         /* notifier priority > KVM */
296         hotcpu_notifier(cpu_vsyscall_notifier, 30);
297
298         return 0;
299 }
300 __initcall(vsyscall_init);