x86/iopl/64: Properly context-switch IOPL on Xen PV
[pandora-kernel.git] / arch / x86 / kernel / process_64.c
1 /*
2  *  Copyright (C) 1995  Linus Torvalds
3  *
4  *  Pentium III FXSR, SSE support
5  *      Gareth Hughes <gareth@valinux.com>, May 2000
6  *
7  *  X86-64 port
8  *      Andi Kleen.
9  *
10  *      CPU hotplug support - ashok.raj@intel.com
11  */
12
13 /*
14  * This file handles the architecture-dependent parts of process handling..
15  */
16
17 #include <linux/stackprotector.h>
18 #include <linux/cpu.h>
19 #include <linux/errno.h>
20 #include <linux/sched.h>
21 #include <linux/fs.h>
22 #include <linux/kernel.h>
23 #include <linux/mm.h>
24 #include <linux/elfcore.h>
25 #include <linux/smp.h>
26 #include <linux/slab.h>
27 #include <linux/user.h>
28 #include <linux/interrupt.h>
29 #include <linux/delay.h>
30 #include <linux/module.h>
31 #include <linux/ptrace.h>
32 #include <linux/notifier.h>
33 #include <linux/kprobes.h>
34 #include <linux/kdebug.h>
35 #include <linux/tick.h>
36 #include <linux/prctl.h>
37 #include <linux/uaccess.h>
38 #include <linux/io.h>
39 #include <linux/ftrace.h>
40 #include <linux/cpuidle.h>
41 #include <xen/xen.h>
42
43 #include <asm/pgtable.h>
44 #include <asm/system.h>
45 #include <asm/processor.h>
46 #include <asm/i387.h>
47 #include <asm/mmu_context.h>
48 #include <asm/prctl.h>
49 #include <asm/desc.h>
50 #include <asm/proto.h>
51 #include <asm/ia32.h>
52 #include <asm/idle.h>
53 #include <asm/syscalls.h>
54 #include <asm/debugreg.h>
55 #include <asm/nmi.h>
56 #include <asm/xen/hypervisor.h>
57
58 asmlinkage extern void ret_from_fork(void);
59
60 DEFINE_PER_CPU(unsigned long, old_rsp);
61 static DEFINE_PER_CPU(unsigned char, is_idle);
62
63 static ATOMIC_NOTIFIER_HEAD(idle_notifier);
64
65 void idle_notifier_register(struct notifier_block *n)
66 {
67         atomic_notifier_chain_register(&idle_notifier, n);
68 }
69 EXPORT_SYMBOL_GPL(idle_notifier_register);
70
71 void idle_notifier_unregister(struct notifier_block *n)
72 {
73         atomic_notifier_chain_unregister(&idle_notifier, n);
74 }
75 EXPORT_SYMBOL_GPL(idle_notifier_unregister);
76
77 void enter_idle(void)
78 {
79         percpu_write(is_idle, 1);
80         atomic_notifier_call_chain(&idle_notifier, IDLE_START, NULL);
81 }
82
83 static void __exit_idle(void)
84 {
85         if (x86_test_and_clear_bit_percpu(0, is_idle) == 0)
86                 return;
87         atomic_notifier_call_chain(&idle_notifier, IDLE_END, NULL);
88 }
89
90 /* Called from interrupts to signify idle end */
91 void exit_idle(void)
92 {
93         /* idle loop has pid 0 */
94         if (current->pid)
95                 return;
96         __exit_idle();
97 }
98
99 #ifndef CONFIG_SMP
100 static inline void play_dead(void)
101 {
102         BUG();
103 }
104 #endif
105
106 /*
107  * The idle thread. There's no useful work to be
108  * done, so just try to conserve power and have a
109  * low exit latency (ie sit in a loop waiting for
110  * somebody to say that they'd like to reschedule)
111  */
112 void cpu_idle(void)
113 {
114         current_thread_info()->status |= TS_POLLING;
115
116         /*
117          * If we're the non-boot CPU, nothing set the stack canary up
118          * for us.  CPU0 already has it initialized but no harm in
119          * doing it again.  This is a good place for updating it, as
120          * we wont ever return from this function (so the invalid
121          * canaries already on the stack wont ever trigger).
122          */
123         boot_init_stack_canary();
124
125         /* endless idle loop with no priority at all */
126         while (1) {
127                 tick_nohz_stop_sched_tick(1);
128                 while (!need_resched()) {
129
130                         rmb();
131
132                         if (cpu_is_offline(smp_processor_id()))
133                                 play_dead();
134                         /*
135                          * Idle routines should keep interrupts disabled
136                          * from here on, until they go to idle.
137                          * Otherwise, idle callbacks can misfire.
138                          */
139                         local_touch_nmi();
140                         local_irq_disable();
141                         enter_idle();
142                         /* Don't trace irqs off for idle */
143                         stop_critical_timings();
144                         if (cpuidle_idle_call())
145                                 pm_idle();
146                         start_critical_timings();
147
148                         /* In many cases the interrupt that ended idle
149                            has already called exit_idle. But some idle
150                            loops can be woken up without interrupt. */
151                         __exit_idle();
152                 }
153
154                 tick_nohz_restart_sched_tick();
155                 preempt_enable_no_resched();
156                 schedule();
157                 preempt_disable();
158         }
159 }
160
161 /* Prints also some state that isn't saved in the pt_regs */
162 void __show_regs(struct pt_regs *regs, int all)
163 {
164         unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L, fs, gs, shadowgs;
165         unsigned long d0, d1, d2, d3, d6, d7;
166         unsigned int fsindex, gsindex;
167         unsigned int ds, cs, es;
168
169         show_regs_common();
170         printk(KERN_DEFAULT "RIP: %04lx:[<%016lx>] ", regs->cs & 0xffff, regs->ip);
171         printk_address(regs->ip, 1);
172         printk(KERN_DEFAULT "RSP: %04lx:%016lx  EFLAGS: %08lx\n", regs->ss,
173                         regs->sp, regs->flags);
174         printk(KERN_DEFAULT "RAX: %016lx RBX: %016lx RCX: %016lx\n",
175                regs->ax, regs->bx, regs->cx);
176         printk(KERN_DEFAULT "RDX: %016lx RSI: %016lx RDI: %016lx\n",
177                regs->dx, regs->si, regs->di);
178         printk(KERN_DEFAULT "RBP: %016lx R08: %016lx R09: %016lx\n",
179                regs->bp, regs->r8, regs->r9);
180         printk(KERN_DEFAULT "R10: %016lx R11: %016lx R12: %016lx\n",
181                regs->r10, regs->r11, regs->r12);
182         printk(KERN_DEFAULT "R13: %016lx R14: %016lx R15: %016lx\n",
183                regs->r13, regs->r14, regs->r15);
184
185         asm("movl %%ds,%0" : "=r" (ds));
186         asm("movl %%cs,%0" : "=r" (cs));
187         asm("movl %%es,%0" : "=r" (es));
188         asm("movl %%fs,%0" : "=r" (fsindex));
189         asm("movl %%gs,%0" : "=r" (gsindex));
190
191         rdmsrl(MSR_FS_BASE, fs);
192         rdmsrl(MSR_GS_BASE, gs);
193         rdmsrl(MSR_KERNEL_GS_BASE, shadowgs);
194
195         if (!all)
196                 return;
197
198         cr0 = read_cr0();
199         cr2 = read_cr2();
200         cr3 = read_cr3();
201         cr4 = read_cr4();
202
203         printk(KERN_DEFAULT "FS:  %016lx(%04x) GS:%016lx(%04x) knlGS:%016lx\n",
204                fs, fsindex, gs, gsindex, shadowgs);
205         printk(KERN_DEFAULT "CS:  %04x DS: %04x ES: %04x CR0: %016lx\n", cs, ds,
206                         es, cr0);
207         printk(KERN_DEFAULT "CR2: %016lx CR3: %016lx CR4: %016lx\n", cr2, cr3,
208                         cr4);
209
210         get_debugreg(d0, 0);
211         get_debugreg(d1, 1);
212         get_debugreg(d2, 2);
213         printk(KERN_DEFAULT "DR0: %016lx DR1: %016lx DR2: %016lx\n", d0, d1, d2);
214         get_debugreg(d3, 3);
215         get_debugreg(d6, 6);
216         get_debugreg(d7, 7);
217         printk(KERN_DEFAULT "DR3: %016lx DR6: %016lx DR7: %016lx\n", d3, d6, d7);
218 }
219
220 void release_thread(struct task_struct *dead_task)
221 {
222         if (dead_task->mm) {
223                 if (dead_task->mm->context.ldt) {
224                         printk("WARNING: dead process %8s still has LDT? <%p/%d>\n",
225                                         dead_task->comm,
226                                         dead_task->mm->context.ldt->entries,
227                                         dead_task->mm->context.ldt->size);
228                         BUG();
229                 }
230         }
231 }
232
233 static inline void set_32bit_tls(struct task_struct *t, int tls, u32 addr)
234 {
235         struct user_desc ud = {
236                 .base_addr = addr,
237                 .limit = 0xfffff,
238                 .seg_32bit = 1,
239                 .limit_in_pages = 1,
240                 .useable = 1,
241         };
242         struct desc_struct *desc = t->thread.tls_array;
243         desc += tls;
244         fill_ldt(desc, &ud);
245 }
246
247 static inline u32 read_32bit_tls(struct task_struct *t, int tls)
248 {
249         return get_desc_base(&t->thread.tls_array[tls]);
250 }
251
252 /*
253  * This gets called before we allocate a new thread and copy
254  * the current task into it.
255  */
256 void prepare_to_copy(struct task_struct *tsk)
257 {
258         unlazy_fpu(tsk);
259 }
260
261 int copy_thread(unsigned long clone_flags, unsigned long sp,
262                 unsigned long unused,
263         struct task_struct *p, struct pt_regs *regs)
264 {
265         int err;
266         struct pt_regs *childregs;
267         struct task_struct *me = current;
268
269         childregs = ((struct pt_regs *)
270                         (THREAD_SIZE + task_stack_page(p))) - 1;
271         *childregs = *regs;
272
273         childregs->ax = 0;
274         if (user_mode(regs))
275                 childregs->sp = sp;
276         else
277                 childregs->sp = (unsigned long)childregs;
278
279         p->thread.sp = (unsigned long) childregs;
280         p->thread.sp0 = (unsigned long) (childregs+1);
281         p->thread.usersp = me->thread.usersp;
282
283         set_tsk_thread_flag(p, TIF_FORK);
284
285         p->thread.io_bitmap_ptr = NULL;
286
287         savesegment(gs, p->thread.gsindex);
288         p->thread.gs = p->thread.gsindex ? 0 : me->thread.gs;
289         savesegment(fs, p->thread.fsindex);
290         p->thread.fs = p->thread.fsindex ? 0 : me->thread.fs;
291         savesegment(es, p->thread.es);
292         savesegment(ds, p->thread.ds);
293
294         err = -ENOMEM;
295         memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
296
297         if (unlikely(test_tsk_thread_flag(me, TIF_IO_BITMAP))) {
298                 p->thread.io_bitmap_ptr = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
299                 if (!p->thread.io_bitmap_ptr) {
300                         p->thread.io_bitmap_max = 0;
301                         return -ENOMEM;
302                 }
303                 memcpy(p->thread.io_bitmap_ptr, me->thread.io_bitmap_ptr,
304                                 IO_BITMAP_BYTES);
305                 set_tsk_thread_flag(p, TIF_IO_BITMAP);
306         }
307
308         /*
309          * Set a new TLS for the child thread?
310          */
311         if (clone_flags & CLONE_SETTLS) {
312 #ifdef CONFIG_IA32_EMULATION
313                 if (test_thread_flag(TIF_IA32))
314                         err = do_set_thread_area(p, -1,
315                                 (struct user_desc __user *)childregs->si, 0);
316                 else
317 #endif
318                         err = do_arch_prctl(p, ARCH_SET_FS, childregs->r8);
319                 if (err)
320                         goto out;
321         }
322         err = 0;
323 out:
324         if (err && p->thread.io_bitmap_ptr) {
325                 kfree(p->thread.io_bitmap_ptr);
326                 p->thread.io_bitmap_max = 0;
327         }
328
329         return err;
330 }
331
332 static void
333 start_thread_common(struct pt_regs *regs, unsigned long new_ip,
334                     unsigned long new_sp,
335                     unsigned int _cs, unsigned int _ss, unsigned int _ds)
336 {
337         loadsegment(fs, 0);
338         loadsegment(es, _ds);
339         loadsegment(ds, _ds);
340         load_gs_index(0);
341         regs->ip                = new_ip;
342         regs->sp                = new_sp;
343         percpu_write(old_rsp, new_sp);
344         regs->cs                = _cs;
345         regs->ss                = _ss;
346         regs->flags             = X86_EFLAGS_IF;
347         /*
348          * Free the old FP and other extended state
349          */
350         free_thread_xstate(current);
351 }
352
353 void
354 start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
355 {
356         start_thread_common(regs, new_ip, new_sp,
357                             __USER_CS, __USER_DS, 0);
358 }
359
360 #ifdef CONFIG_IA32_EMULATION
361 void start_thread_ia32(struct pt_regs *regs, u32 new_ip, u32 new_sp)
362 {
363         start_thread_common(regs, new_ip, new_sp,
364                             __USER32_CS, __USER32_DS, __USER32_DS);
365 }
366 #endif
367
368 /*
369  *      switch_to(x,y) should switch tasks from x to y.
370  *
371  * This could still be optimized:
372  * - fold all the options into a flag word and test it with a single test.
373  * - could test fs/gs bitsliced
374  *
375  * Kprobes not supported here. Set the probe on schedule instead.
376  * Function graph tracer not supported too.
377  */
378 __notrace_funcgraph struct task_struct *
379 __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
380 {
381         struct thread_struct *prev = &prev_p->thread;
382         struct thread_struct *next = &next_p->thread;
383         int cpu = smp_processor_id();
384         struct tss_struct *tss = &per_cpu(init_tss, cpu);
385         unsigned fsindex, gsindex;
386         fpu_switch_t fpu;
387
388         fpu = switch_fpu_prepare(prev_p, next_p);
389
390         /* Reload esp0 and ss1. */
391         load_sp0(tss, next);
392
393         /* We must save %fs and %gs before load_TLS() because
394          * %fs and %gs may be cleared by load_TLS().
395          *
396          * (e.g. xen_load_tls())
397          */
398         savesegment(fs, fsindex);
399         savesegment(gs, gsindex);
400
401         /*
402          * Load TLS before restoring any segments so that segment loads
403          * reference the correct GDT entries.
404          */
405         load_TLS(next, cpu);
406
407         /*
408          * Leave lazy mode, flushing any hypercalls made here.  This
409          * must be done after loading TLS entries in the GDT but before
410          * loading segments that might reference them, and and it must
411          * be done before math_state_restore, so the TS bit is up to
412          * date.
413          */
414         arch_end_context_switch(next_p);
415
416         /* Switch DS and ES.
417          *
418          * Reading them only returns the selectors, but writing them (if
419          * nonzero) loads the full descriptor from the GDT or LDT.  The
420          * LDT for next is loaded in switch_mm, and the GDT is loaded
421          * above.
422          *
423          * We therefore need to write new values to the segment
424          * registers on every context switch unless both the new and old
425          * values are zero.
426          *
427          * Note that we don't need to do anything for CS and SS, as
428          * those are saved and restored as part of pt_regs.
429          */
430         savesegment(es, prev->es);
431         if (unlikely(next->es | prev->es))
432                 loadsegment(es, next->es);
433
434         savesegment(ds, prev->ds);
435         if (unlikely(next->ds | prev->ds))
436                 loadsegment(ds, next->ds);
437
438         /*
439          * Switch FS and GS.
440          *
441          * These are even more complicated than FS and GS: they have
442          * 64-bit bases are that controlled by arch_prctl.  Those bases
443          * only differ from the values in the GDT or LDT if the selector
444          * is 0.
445          *
446          * Loading the segment register resets the hidden base part of
447          * the register to 0 or the value from the GDT / LDT.  If the
448          * next base address zero, writing 0 to the segment register is
449          * much faster than using wrmsr to explicitly zero the base.
450          *
451          * The thread_struct.fs and thread_struct.gs values are 0
452          * if the fs and gs bases respectively are not overridden
453          * from the values implied by fsindex and gsindex.  They
454          * are nonzero, and store the nonzero base addresses, if
455          * the bases are overridden.
456          *
457          * (fs != 0 && fsindex != 0) || (gs != 0 && gsindex != 0) should
458          * be impossible.
459          *
460          * Therefore we need to reload the segment registers if either
461          * the old or new selector is nonzero, and we need to override
462          * the base address if next thread expects it to be overridden.
463          *
464          * This code is unnecessarily slow in the case where the old and
465          * new indexes are zero and the new base is nonzero -- it will
466          * unnecessarily write 0 to the selector before writing the new
467          * base address.
468          *
469          * Note: This all depends on arch_prctl being the only way that
470          * user code can override the segment base.  Once wrfsbase and
471          * wrgsbase are enabled, most of this code will need to change.
472          */
473         if (unlikely(fsindex | next->fsindex | prev->fs)) {
474                 loadsegment(fs, next->fsindex);
475
476                 /*
477                  * If user code wrote a nonzero value to FS, then it also
478                  * cleared the overridden base address.
479                  *
480                  * XXX: if user code wrote 0 to FS and cleared the base
481                  * address itself, we won't notice and we'll incorrectly
482                  * restore the prior base address next time we reschdule
483                  * the process.
484                  */
485                 if (fsindex)
486                         prev->fs = 0;
487         }
488         if (next->fs)
489                 wrmsrl(MSR_FS_BASE, next->fs);
490         prev->fsindex = fsindex;
491
492         if (unlikely(gsindex | next->gsindex | prev->gs)) {
493                 load_gs_index(next->gsindex);
494
495                 /* This works (and fails) the same way as fsindex above. */
496                 if (gsindex)
497                         prev->gs = 0;
498         }
499         if (next->gs)
500                 wrmsrl(MSR_KERNEL_GS_BASE, next->gs);
501         prev->gsindex = gsindex;
502
503         switch_fpu_finish(next_p, fpu);
504
505         /*
506          * Switch the PDA and FPU contexts.
507          */
508         prev->usersp = percpu_read(old_rsp);
509         percpu_write(old_rsp, next->usersp);
510         percpu_write(current_task, next_p);
511
512         percpu_write(kernel_stack,
513                   (unsigned long)task_stack_page(next_p) +
514                   THREAD_SIZE - KERNEL_STACK_OFFSET);
515
516         /*
517          * Now maybe reload the debug registers and handle I/O bitmaps
518          */
519         if (unlikely(task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT ||
520                      task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV))
521                 __switch_to_xtra(prev_p, next_p, tss);
522
523 #ifdef CONFIG_XEN
524         /*
525          * On Xen PV, IOPL bits in pt_regs->flags have no effect, and
526          * current_pt_regs()->flags may not match the current task's
527          * intended IOPL.  We need to switch it manually.
528          */
529         if (unlikely(xen_pv_domain() && prev->iopl != next->iopl))
530                 xen_set_iopl_mask(next->iopl);
531 #endif
532
533         return prev_p;
534 }
535
536 void set_personality_64bit(void)
537 {
538         /* inherit personality from parent */
539
540         /* Make sure to be in 64bit mode */
541         clear_thread_flag(TIF_IA32);
542
543         /* Ensure the corresponding mm is not marked. */
544         if (current->mm)
545                 current->mm->context.ia32_compat = 0;
546
547         /* TBD: overwrites user setup. Should have two bits.
548            But 64bit processes have always behaved this way,
549            so it's not too bad. The main problem is just that
550            32bit childs are affected again. */
551         current->personality &= ~READ_IMPLIES_EXEC;
552 }
553
554 void set_personality_ia32(void)
555 {
556         /* inherit personality from parent */
557
558         /* Make sure to be in 32bit mode */
559         set_thread_flag(TIF_IA32);
560         current->personality |= force_personality32;
561
562         /* Mark the associated mm as containing 32-bit tasks. */
563         if (current->mm)
564                 current->mm->context.ia32_compat = 1;
565
566         /* Prepare the first "return" to user space */
567         current_thread_info()->status |= TS_COMPAT;
568 }
569
570 /*
571  * Called from fs/proc with a reference on @p to find the function
572  * which called into schedule(). This needs to be done carefully
573  * because the task might wake up and we might look at a stack
574  * changing under us.
575  */
576 unsigned long get_wchan(struct task_struct *p)
577 {
578         unsigned long start, bottom, top, sp, fp, ip;
579         int count = 0;
580
581         if (!p || p == current || p->state == TASK_RUNNING)
582                 return 0;
583
584         start = (unsigned long)task_stack_page(p);
585         if (!start)
586                 return 0;
587
588         /*
589          * Layout of the stack page:
590          *
591          * ----------- topmax = start + THREAD_SIZE - sizeof(unsigned long)
592          * PADDING
593          * ----------- top = topmax - TOP_OF_KERNEL_STACK_PADDING
594          * stack
595          * ----------- bottom = start + sizeof(thread_info)
596          * thread_info
597          * ----------- start
598          *
599          * The tasks stack pointer points at the location where the
600          * framepointer is stored. The data on the stack is:
601          * ... IP FP ... IP FP
602          *
603          * We need to read FP and IP, so we need to adjust the upper
604          * bound by another unsigned long.
605          */
606         top = start + THREAD_SIZE;
607         top -= 2 * sizeof(unsigned long);
608         bottom = start + sizeof(struct thread_info);
609
610         sp = ACCESS_ONCE(p->thread.sp);
611         if (sp < bottom || sp > top)
612                 return 0;
613
614         fp = ACCESS_ONCE(*(unsigned long *)sp);
615         do {
616                 if (fp < bottom || fp > top)
617                         return 0;
618                 ip = ACCESS_ONCE(*(unsigned long *)(fp + sizeof(unsigned long)));
619                 if (!in_sched_functions(ip))
620                         return ip;
621                 fp = ACCESS_ONCE(*(unsigned long *)fp);
622         } while (count++ < 16 && p->state != TASK_RUNNING);
623         return 0;
624 }
625
626 long do_arch_prctl(struct task_struct *task, int code, unsigned long addr)
627 {
628         int ret = 0;
629         int doit = task == current;
630         int cpu;
631
632         switch (code) {
633         case ARCH_SET_GS:
634                 if (addr >= TASK_SIZE_OF(task))
635                         return -EPERM;
636                 cpu = get_cpu();
637                 /* handle small bases via the GDT because that's faster to
638                    switch. */
639                 if (addr <= 0xffffffff) {
640                         set_32bit_tls(task, GS_TLS, addr);
641                         if (doit) {
642                                 load_TLS(&task->thread, cpu);
643                                 load_gs_index(GS_TLS_SEL);
644                         }
645                         task->thread.gsindex = GS_TLS_SEL;
646                         task->thread.gs = 0;
647                 } else {
648                         task->thread.gsindex = 0;
649                         task->thread.gs = addr;
650                         if (doit) {
651                                 load_gs_index(0);
652                                 ret = checking_wrmsrl(MSR_KERNEL_GS_BASE, addr);
653                         }
654                 }
655                 put_cpu();
656                 break;
657         case ARCH_SET_FS:
658                 /* Not strictly needed for fs, but do it for symmetry
659                    with gs */
660                 if (addr >= TASK_SIZE_OF(task))
661                         return -EPERM;
662                 cpu = get_cpu();
663                 /* handle small bases via the GDT because that's faster to
664                    switch. */
665                 if (addr <= 0xffffffff) {
666                         set_32bit_tls(task, FS_TLS, addr);
667                         if (doit) {
668                                 load_TLS(&task->thread, cpu);
669                                 loadsegment(fs, FS_TLS_SEL);
670                         }
671                         task->thread.fsindex = FS_TLS_SEL;
672                         task->thread.fs = 0;
673                 } else {
674                         task->thread.fsindex = 0;
675                         task->thread.fs = addr;
676                         if (doit) {
677                                 /* set the selector to 0 to not confuse
678                                    __switch_to */
679                                 loadsegment(fs, 0);
680                                 ret = checking_wrmsrl(MSR_FS_BASE, addr);
681                         }
682                 }
683                 put_cpu();
684                 break;
685         case ARCH_GET_FS: {
686                 unsigned long base;
687                 if (task->thread.fsindex == FS_TLS_SEL)
688                         base = read_32bit_tls(task, FS_TLS);
689                 else if (doit)
690                         rdmsrl(MSR_FS_BASE, base);
691                 else
692                         base = task->thread.fs;
693                 ret = put_user(base, (unsigned long __user *)addr);
694                 break;
695         }
696         case ARCH_GET_GS: {
697                 unsigned long base;
698                 unsigned gsindex;
699                 if (task->thread.gsindex == GS_TLS_SEL)
700                         base = read_32bit_tls(task, GS_TLS);
701                 else if (doit) {
702                         savesegment(gs, gsindex);
703                         if (gsindex)
704                                 rdmsrl(MSR_KERNEL_GS_BASE, base);
705                         else
706                                 base = task->thread.gs;
707                 } else
708                         base = task->thread.gs;
709                 ret = put_user(base, (unsigned long __user *)addr);
710                 break;
711         }
712
713         default:
714                 ret = -EINVAL;
715                 break;
716         }
717
718         return ret;
719 }
720
721 long sys_arch_prctl(int code, unsigned long addr)
722 {
723         return do_arch_prctl(current, code, addr);
724 }
725
726 unsigned long KSTK_ESP(struct task_struct *task)
727 {
728         return (test_tsk_thread_flag(task, TIF_IA32)) ?
729                         (task_pt_regs(task)->sp) : ((task)->thread.usersp);
730 }