Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / arch / x86_64 / kernel / process-xen.c
1 /*
2  *  linux/arch/x86-64/kernel/process.c
3  *
4  *  Copyright (C) 1995  Linus Torvalds
5  *
6  *  Pentium III FXSR, SSE support
7  *      Gareth Hughes <gareth@valinux.com>, May 2000
8  * 
9  *  X86-64 port
10  *      Andi Kleen.
11  *
12  *      CPU hotplug support - ashok.raj@intel.com
13  * 
14  *  Jun Nakajima <jun.nakajima@intel.com> 
15  *     Modified for Xen
16  */
17
18 /*
19  * This file handles the architecture-dependent parts of process handling..
20  */
21
22 #include <stdarg.h>
23
24 #include <linux/cpu.h>
25 #include <linux/errno.h>
26 #include <linux/sched.h>
27 #include <linux/kernel.h>
28 #include <linux/mm.h>
29 #include <linux/elfcore.h>
30 #include <linux/smp.h>
31 #include <linux/slab.h>
32 #include <linux/user.h>
33 #include <linux/module.h>
34 #include <linux/a.out.h>
35 #include <linux/interrupt.h>
36 #include <linux/delay.h>
37 #include <linux/ptrace.h>
38 #include <linux/utsname.h>
39 #include <linux/random.h>
40 #include <linux/notifier.h>
41 #include <linux/kprobes.h>
42
43 #include <asm/uaccess.h>
44 #include <asm/pgtable.h>
45 #include <asm/system.h>
46 #include <asm/io.h>
47 #include <asm/processor.h>
48 #include <asm/i387.h>
49 #include <asm/mmu_context.h>
50 #include <asm/pda.h>
51 #include <asm/prctl.h>
52 #include <asm/kdebug.h>
53 #include <xen/interface/dom0_ops.h>
54 #include <xen/interface/physdev.h>
55 #include <xen/interface/vcpu.h>
56 #include <asm/desc.h>
57 #include <asm/proto.h>
58 #include <asm/hardirq.h>
59 #include <asm/ia32.h>
60 #include <asm/idle.h>
61
62 #include <xen/cpu_hotplug.h>
63
64 asmlinkage extern void ret_from_fork(void);
65
66 unsigned long kernel_thread_flags = CLONE_VM | CLONE_UNTRACED;
67
68 unsigned long boot_option_idle_override = 0;
69 EXPORT_SYMBOL(boot_option_idle_override);
70
71 /*
72  * Powermanagement idle function, if any..
73  */
74 void (*pm_idle)(void);
75 static DEFINE_PER_CPU(unsigned int, cpu_idle_state);
76
77 static ATOMIC_NOTIFIER_HEAD(idle_notifier);
78
79 void idle_notifier_register(struct notifier_block *n)
80 {
81         atomic_notifier_chain_register(&idle_notifier, n);
82 }
83 EXPORT_SYMBOL_GPL(idle_notifier_register);
84
85 void idle_notifier_unregister(struct notifier_block *n)
86 {
87         atomic_notifier_chain_unregister(&idle_notifier, n);
88 }
89 EXPORT_SYMBOL(idle_notifier_unregister);
90
91 enum idle_state { CPU_IDLE, CPU_NOT_IDLE };
92 static DEFINE_PER_CPU(enum idle_state, idle_state) = CPU_NOT_IDLE;
93
94 void enter_idle(void)
95 {
96         __get_cpu_var(idle_state) = CPU_IDLE;
97         atomic_notifier_call_chain(&idle_notifier, IDLE_START, NULL);
98 }
99
100 static void __exit_idle(void)
101 {
102         __get_cpu_var(idle_state) = CPU_NOT_IDLE;
103         atomic_notifier_call_chain(&idle_notifier, IDLE_END, NULL);
104 }
105
106 /* Called from interrupts to signify idle end */
107 void exit_idle(void)
108 {
109         if (current->pid | read_pda(irqcount))
110                 return;
111         __exit_idle();
112 }
113
114 /* XXX XEN doesn't use default_idle(), poll_idle(). Use xen_idle() instead. */
115 void xen_idle(void)
116 {
117         local_irq_disable();
118
119         if (need_resched())
120                 local_irq_enable();
121         else {
122                 current_thread_info()->status &= ~TS_POLLING;
123                 smp_mb__after_clear_bit();
124                 safe_halt();
125                 current_thread_info()->status |= TS_POLLING;
126         }
127 }
128
129 #ifdef CONFIG_HOTPLUG_CPU
130 static inline void play_dead(void)
131 {
132         idle_task_exit();
133         local_irq_disable();
134         cpu_clear(smp_processor_id(), cpu_initialized);
135         preempt_enable_no_resched();
136         HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL);
137         cpu_bringup();
138 }
139 #else
140 static inline void play_dead(void)
141 {
142         BUG();
143 }
144 #endif /* CONFIG_HOTPLUG_CPU */
145
146 /*
147  * The idle thread. There's no useful work to be
148  * done, so just try to conserve power and have a
149  * low exit latency (ie sit in a loop waiting for
150  * somebody to say that they'd like to reschedule)
151  */
152 void cpu_idle (void)
153 {
154         current_thread_info()->status |= TS_POLLING;
155         /* endless idle loop with no priority at all */
156         while (1) {
157                 while (!need_resched()) {
158                         if (__get_cpu_var(cpu_idle_state))
159                                 __get_cpu_var(cpu_idle_state) = 0;
160                         rmb();
161                         
162                         if (cpu_is_offline(smp_processor_id()))
163                                 play_dead();
164                         enter_idle();
165                         xen_idle();
166                         __exit_idle();
167                 }
168
169                 preempt_enable_no_resched();
170                 schedule();
171                 preempt_disable();
172         }
173 }
174
175 void cpu_idle_wait(void)
176 {
177         unsigned int cpu, this_cpu = get_cpu();
178         cpumask_t map;
179
180         set_cpus_allowed(current, cpumask_of_cpu(this_cpu));
181         put_cpu();
182
183         cpus_clear(map);
184         for_each_online_cpu(cpu) {
185                 per_cpu(cpu_idle_state, cpu) = 1;
186                 cpu_set(cpu, map);
187         }
188
189         __get_cpu_var(cpu_idle_state) = 0;
190
191         wmb();
192         do {
193                 ssleep(1);
194                 for_each_online_cpu(cpu) {
195                         if (cpu_isset(cpu, map) &&
196                                         !per_cpu(cpu_idle_state, cpu))
197                                 cpu_clear(cpu, map);
198                 }
199                 cpus_and(map, map, cpu_online_map);
200         } while (!cpus_empty(map));
201 }
202 EXPORT_SYMBOL_GPL(cpu_idle_wait);
203
204 /* XXX XEN doesn't use mwait_idle(), select_idle_routine(), idle_setup(). */
205 /* Always use xen_idle() instead. */
206 void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c) {}
207
208 /* Prints also some state that isn't saved in the pt_regs */ 
209 void __show_regs(struct pt_regs * regs)
210 {
211         unsigned long fs, gs, shadowgs;
212         unsigned int fsindex,gsindex;
213         unsigned int ds,cs,es; 
214
215         printk("\n");
216         print_modules();
217         printk("Pid: %d, comm: %.20s %s %s %.*s\n",
218                 current->pid, current->comm, print_tainted(),
219                 system_utsname.release,
220                 (int)strcspn(system_utsname.version, " "),
221                 system_utsname.version);
222         printk("RIP: %04lx:[<%016lx>] ", regs->cs & 0xffff, regs->rip);
223         printk_address(regs->rip); 
224         printk("RSP: %04lx:%016lx  EFLAGS: %08lx\n", regs->ss, regs->rsp,
225                 regs->eflags);
226         printk("RAX: %016lx RBX: %016lx RCX: %016lx\n",
227                regs->rax, regs->rbx, regs->rcx);
228         printk("RDX: %016lx RSI: %016lx RDI: %016lx\n",
229                regs->rdx, regs->rsi, regs->rdi); 
230         printk("RBP: %016lx R08: %016lx R09: %016lx\n",
231                regs->rbp, regs->r8, regs->r9); 
232         printk("R10: %016lx R11: %016lx R12: %016lx\n",
233                regs->r10, regs->r11, regs->r12); 
234         printk("R13: %016lx R14: %016lx R15: %016lx\n",
235                regs->r13, regs->r14, regs->r15); 
236
237         asm("mov %%ds,%0" : "=r" (ds)); 
238         asm("mov %%cs,%0" : "=r" (cs)); 
239         asm("mov %%es,%0" : "=r" (es)); 
240         asm("mov %%fs,%0" : "=r" (fsindex));
241         asm("mov %%gs,%0" : "=r" (gsindex));
242
243         rdmsrl(MSR_FS_BASE, fs);
244         rdmsrl(MSR_GS_BASE, gs); 
245         rdmsrl(MSR_KERNEL_GS_BASE, shadowgs); 
246
247         printk("FS:  %016lx(%04x) GS:%016lx(%04x) knlGS:%016lx\n", 
248                fs,fsindex,gs,gsindex,shadowgs); 
249         printk("CS:  %04x DS: %04x ES: %04x\n", cs, ds, es); 
250
251 }
252
253 void show_regs(struct pt_regs *regs)
254 {
255         printk("CPU %d:", smp_processor_id());
256         __show_regs(regs);
257         show_trace(NULL, regs, &regs->rsp);
258 }
259
260 /*
261  * Free current thread data structures etc..
262  */
263 void exit_thread(void)
264 {
265         struct task_struct *me = current;
266         struct thread_struct *t = &me->thread;
267
268         if (me->thread.io_bitmap_ptr) { 
269 #ifndef CONFIG_X86_NO_TSS
270                 struct tss_struct *tss = &per_cpu(init_tss, get_cpu());
271 #endif
272 #ifdef CONFIG_XEN
273                 struct physdev_set_iobitmap iobmp_op = { 0 };
274 #endif
275
276                 kfree(t->io_bitmap_ptr);
277                 t->io_bitmap_ptr = NULL;
278                 /*
279                  * Careful, clear this in the TSS too:
280                  */
281 #ifndef CONFIG_X86_NO_TSS
282                 memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
283                 put_cpu();
284 #endif
285 #ifdef CONFIG_XEN
286                 HYPERVISOR_physdev_op(PHYSDEVOP_set_iobitmap, &iobmp_op);
287 #endif
288                 t->io_bitmap_max = 0;
289         }
290 }
291
292 void load_gs_index(unsigned gs)
293 {
294         HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, gs);
295 }
296
297 void flush_thread(void)
298 {
299         struct task_struct *tsk = current;
300         struct thread_info *t = current_thread_info();
301
302         if (t->flags & _TIF_ABI_PENDING) {
303                 t->flags ^= (_TIF_ABI_PENDING | _TIF_IA32);
304                 if (t->flags & _TIF_IA32)
305                         current_thread_info()->status |= TS_COMPAT;
306         }
307
308
309         tsk->thread.debugreg0 = 0;
310         tsk->thread.debugreg1 = 0;
311         tsk->thread.debugreg2 = 0;
312         tsk->thread.debugreg3 = 0;
313         tsk->thread.debugreg6 = 0;
314         tsk->thread.debugreg7 = 0;
315         memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));        
316         /*
317          * Forget coprocessor state..
318          */
319         clear_fpu(tsk);
320         clear_used_math();
321 }
322
323 void release_thread(struct task_struct *dead_task)
324 {
325         if (dead_task->mm) {
326                 if (dead_task->mm->context.size) {
327                         printk("WARNING: dead process %8s still has LDT? <%p/%d>\n",
328                                         dead_task->comm,
329                                         dead_task->mm->context.ldt,
330                                         dead_task->mm->context.size);
331                         BUG();
332                 }
333         }
334 }
335
336 static inline void set_32bit_tls(struct task_struct *t, int tls, u32 addr)
337 {
338         struct user_desc ud = { 
339                 .base_addr = addr,
340                 .limit = 0xfffff,
341                 .seg_32bit = 1,
342                 .limit_in_pages = 1,
343                 .useable = 1,
344         };
345         struct n_desc_struct *desc = (void *)t->thread.tls_array;
346         desc += tls;
347         desc->a = LDT_entry_a(&ud); 
348         desc->b = LDT_entry_b(&ud); 
349 }
350
351 static inline u32 read_32bit_tls(struct task_struct *t, int tls)
352 {
353         struct desc_struct *desc = (void *)t->thread.tls_array;
354         desc += tls;
355         return desc->base0 | 
356                 (((u32)desc->base1) << 16) | 
357                 (((u32)desc->base2) << 24);
358 }
359
360 /*
361  * This gets called before we allocate a new thread and copy
362  * the current task into it.
363  */
364 void prepare_to_copy(struct task_struct *tsk)
365 {
366         unlazy_fpu(tsk);
367 }
368
369 int copy_thread(int nr, unsigned long clone_flags, unsigned long rsp, 
370                 unsigned long unused,
371         struct task_struct * p, struct pt_regs * regs)
372 {
373         int err;
374         struct pt_regs * childregs;
375         struct task_struct *me = current;
376
377         childregs = ((struct pt_regs *)
378                         (THREAD_SIZE + task_stack_page(p))) - 1;
379         *childregs = *regs;
380
381         childregs->rax = 0;
382         childregs->rsp = rsp;
383         if (rsp == ~0UL)
384                 childregs->rsp = (unsigned long)childregs;
385
386         p->thread.rsp = (unsigned long) childregs;
387         p->thread.rsp0 = (unsigned long) (childregs+1);
388         p->thread.userrsp = me->thread.userrsp; 
389
390         set_tsk_thread_flag(p, TIF_FORK);
391
392         p->thread.fs = me->thread.fs;
393         p->thread.gs = me->thread.gs;
394
395         asm("mov %%gs,%0" : "=m" (p->thread.gsindex));
396         asm("mov %%fs,%0" : "=m" (p->thread.fsindex));
397         asm("mov %%es,%0" : "=m" (p->thread.es));
398         asm("mov %%ds,%0" : "=m" (p->thread.ds));
399
400         if (unlikely(me->thread.io_bitmap_ptr != NULL)) { 
401                 p->thread.io_bitmap_ptr = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
402                 if (!p->thread.io_bitmap_ptr) {
403                         p->thread.io_bitmap_max = 0;
404                         return -ENOMEM;
405                 }
406                 memcpy(p->thread.io_bitmap_ptr, me->thread.io_bitmap_ptr,
407                                 IO_BITMAP_BYTES);
408         } 
409
410         /*
411          * Set a new TLS for the child thread?
412          */
413         if (clone_flags & CLONE_SETTLS) {
414 #ifdef CONFIG_IA32_EMULATION
415                 if (test_thread_flag(TIF_IA32))
416                         err = ia32_child_tls(p, childregs); 
417                 else                    
418 #endif   
419                         err = do_arch_prctl(p, ARCH_SET_FS, childregs->r8); 
420                 if (err) 
421                         goto out;
422         }
423         p->thread.iopl = current->thread.iopl;
424
425         err = 0;
426 out:
427         if (err && p->thread.io_bitmap_ptr) {
428                 kfree(p->thread.io_bitmap_ptr);
429                 p->thread.io_bitmap_max = 0;
430         }
431         return err;
432 }
433
434 static inline void __save_init_fpu( struct task_struct *tsk )
435 {
436         asm volatile( "rex64 ; fxsave %0 ; fnclex"
437                       : "=m" (tsk->thread.i387.fxsave));
438         tsk->thread_info->status &= ~TS_USEDFPU;
439 }
440
441 /*
442  *      switch_to(x,y) should switch tasks from x to y.
443  *
444  * This could still be optimized: 
445  * - fold all the options into a flag word and test it with a single test.
446  * - could test fs/gs bitsliced
447  *
448  * Kprobes not supported here. Set the probe on schedule instead.
449  */
450 __kprobes struct task_struct *
451 __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
452 {
453         struct thread_struct *prev = &prev_p->thread,
454                                  *next = &next_p->thread;
455         int cpu = smp_processor_id();  
456 #ifndef CONFIG_X86_NO_TSS
457         struct tss_struct *tss = &per_cpu(init_tss, cpu);
458 #endif
459         struct physdev_set_iopl iopl_op;
460         struct physdev_set_iobitmap iobmp_op;
461         multicall_entry_t _mcl[8], *mcl = _mcl;
462
463         /*
464          * Reload esp0, LDT and the page table pointer:
465          */
466         mcl->op      = __HYPERVISOR_stack_switch;
467         mcl->args[0] = __KERNEL_DS;
468         mcl->args[1] = next->rsp0;
469         mcl++;
470
471         /*
472          * Load the per-thread Thread-Local Storage descriptor.
473          * This is load_TLS(next, cpu) with multicalls.
474          */
475 #define C(i) do {                                                       \
476         if (unlikely(next->tls_array[i] != prev->tls_array[i])) {       \
477                 mcl->op      = __HYPERVISOR_update_descriptor;          \
478                 mcl->args[0] = virt_to_machine(                         \
479                         &cpu_gdt(cpu)[GDT_ENTRY_TLS_MIN + i]);          \
480                 mcl->args[1] = next->tls_array[i];                      \
481                 mcl++;                                                  \
482         }                                                               \
483 } while (0)
484         C(0); C(1); C(2);
485 #undef C
486
487         if (unlikely(prev->iopl != next->iopl)) {
488                 iopl_op.iopl = (next->iopl == 0) ? 1 : next->iopl;
489                 mcl->op      = __HYPERVISOR_physdev_op;
490                 mcl->args[0] = PHYSDEVOP_set_iopl;
491                 mcl->args[1] = (unsigned long)&iopl_op;
492                 mcl++;
493         }
494
495         if (unlikely(prev->io_bitmap_ptr || next->io_bitmap_ptr)) {
496                 iobmp_op.bitmap   = (char *)next->io_bitmap_ptr;
497                 iobmp_op.nr_ports = next->io_bitmap_ptr ? IO_BITMAP_BITS : 0;
498                 mcl->op      = __HYPERVISOR_physdev_op;
499                 mcl->args[0] = PHYSDEVOP_set_iobitmap;
500                 mcl->args[1] = (unsigned long)&iobmp_op;
501                 mcl++;
502         }
503
504         (void)HYPERVISOR_multicall(_mcl, mcl - _mcl);
505         /* 
506          * Switch DS and ES.
507          * This won't pick up thread selector changes, but I guess that is ok.
508          */
509         if (unlikely(next->es))
510                 loadsegment(es, next->es); 
511         
512         if (unlikely(next->ds))
513                 loadsegment(ds, next->ds);
514
515         /* 
516          * Switch FS and GS.
517          */
518         if (unlikely(next->fsindex))
519                 loadsegment(fs, next->fsindex);
520
521         if (next->fs)
522                 HYPERVISOR_set_segment_base(SEGBASE_FS, next->fs); 
523         
524         if (unlikely(next->gsindex))
525                 load_gs_index(next->gsindex);
526
527         if (next->gs)
528                 HYPERVISOR_set_segment_base(SEGBASE_GS_USER, next->gs); 
529
530         /* 
531          * Switch the PDA and FPU context.
532          */
533         prev->userrsp = read_pda(oldrsp); 
534         write_pda(oldrsp, next->userrsp); 
535         write_pda(pcurrent, next_p); 
536
537         /* This must be here to ensure both math_state_restore() and
538            kernel_fpu_begin() work consistently. 
539            And the AMD workaround requires it to be after DS reload. */
540         /*
541          * This is basically '__unlazy_fpu', except that we queue a
542          * multicall to indicate FPU task switch, rather than
543          * synchronously trapping to Xen.
544          */
545         if (prev_p->thread_info->status & TS_USEDFPU) {
546                 __save_init_fpu(prev_p); /* _not_ save_init_fpu() */
547                 HYPERVISOR_fpu_taskswitch(1);
548         }
549         write_pda(kernelstack,
550                   task_stack_page(next_p) + THREAD_SIZE - PDA_STACKOFFSET);
551
552         /*
553          * Now maybe reload the debug registers
554          */
555         if (unlikely(next->debugreg7)) {
556                 set_debugreg(next->debugreg0, 0);
557                 set_debugreg(next->debugreg1, 1);
558                 set_debugreg(next->debugreg2, 2);
559                 set_debugreg(next->debugreg3, 3);
560                 /* no 4 and 5 */
561                 set_debugreg(next->debugreg6, 6);
562                 set_debugreg(next->debugreg7, 7);
563         }
564
565         return prev_p;
566 }
567
568 /*
569  * sys_execve() executes a new program.
570  */
571 asmlinkage 
572 long sys_execve(char __user *name, char __user * __user *argv,
573                 char __user * __user *envp, struct pt_regs regs)
574 {
575         long error;
576         char * filename;
577
578         filename = getname(name);
579         error = PTR_ERR(filename);
580         if (IS_ERR(filename)) 
581                 return error;
582         error = do_execve(filename, argv, envp, &regs); 
583         putname(filename);
584         return error;
585 }
586
587 void set_personality_64bit(void)
588 {
589         /* inherit personality from parent */
590
591         /* Make sure to be in 64bit mode */
592         clear_thread_flag(TIF_IA32); 
593 }
594
595 asmlinkage long sys_fork(struct pt_regs *regs)
596 {
597         return do_fork(SIGCHLD, regs->rsp, regs, 0, NULL, NULL);
598 }
599
600 asmlinkage long
601 sys_clone(unsigned long clone_flags, unsigned long newsp,
602           void __user *parent_tid, void __user *child_tid, struct pt_regs *regs)
603 {
604         if (!newsp)
605                 newsp = regs->rsp;
606         return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
607 }
608
609 /*
610  * This is trivial, and on the face of it looks like it
611  * could equally well be done in user mode.
612  *
613  * Not so, for quite unobvious reasons - register pressure.
614  * In user mode vfork() cannot have a stack frame, and if
615  * done by calling the "clone()" system call directly, you
616  * do not have enough call-clobbered registers to hold all
617  * the information you need.
618  */
619 asmlinkage long sys_vfork(struct pt_regs *regs)
620 {
621         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->rsp, regs, 0,
622                     NULL, NULL);
623 }
624
625 unsigned long get_wchan(struct task_struct *p)
626 {
627         unsigned long stack;
628         u64 fp,rip;
629         int count = 0;
630
631         if (!p || p == current || p->state==TASK_RUNNING)
632                 return 0; 
633         stack = (unsigned long)task_stack_page(p);
634         if (p->thread.rsp < stack || p->thread.rsp > stack+THREAD_SIZE)
635                 return 0;
636         fp = *(u64 *)(p->thread.rsp);
637         do { 
638                 if (fp < (unsigned long)stack ||
639                     fp > (unsigned long)stack+THREAD_SIZE)
640                         return 0; 
641                 rip = *(u64 *)(fp+8); 
642                 if (!in_sched_functions(rip))
643                         return rip; 
644                 fp = *(u64 *)fp; 
645         } while (count++ < 16); 
646         return 0;
647 }
648
649 long do_arch_prctl(struct task_struct *task, int code, unsigned long addr)
650
651         int ret = 0; 
652         int doit = task == current;
653         int cpu;
654
655         switch (code) { 
656         case ARCH_SET_GS:
657                 if (addr >= TASK_SIZE_OF(task))
658                         return -EPERM; 
659                 cpu = get_cpu();
660                 /* handle small bases via the GDT because that's faster to 
661                    switch. */
662                 if (addr <= 0xffffffff) {  
663                         set_32bit_tls(task, GS_TLS, addr); 
664                         if (doit) { 
665                                 load_TLS(&task->thread, cpu);
666                                 load_gs_index(GS_TLS_SEL); 
667                         }
668                         task->thread.gsindex = GS_TLS_SEL; 
669                         task->thread.gs = 0;
670                 } else { 
671                         task->thread.gsindex = 0;
672                         task->thread.gs = addr;
673                         if (doit) {
674                                 load_gs_index(0);
675                                 ret = HYPERVISOR_set_segment_base(
676                                         SEGBASE_GS_USER, addr);
677                         } 
678                 }
679                 put_cpu();
680                 break;
681         case ARCH_SET_FS:
682                 /* Not strictly needed for fs, but do it for symmetry
683                    with gs */
684                 if (addr >= TASK_SIZE_OF(task))
685                         return -EPERM; 
686                 cpu = get_cpu();
687                 /* handle small bases via the GDT because that's faster to 
688                    switch. */
689                 if (addr <= 0xffffffff) { 
690                         set_32bit_tls(task, FS_TLS, addr);
691                         if (doit) { 
692                                 load_TLS(&task->thread, cpu); 
693                                 asm volatile("movl %0,%%fs" :: "r"(FS_TLS_SEL));
694                         }
695                         task->thread.fsindex = FS_TLS_SEL;
696                         task->thread.fs = 0;
697                 } else { 
698                         task->thread.fsindex = 0;
699                         task->thread.fs = addr;
700                         if (doit) {
701                                 /* set the selector to 0 to not confuse
702                                    __switch_to */
703                                 asm volatile("movl %0,%%fs" :: "r" (0));
704                                 ret = HYPERVISOR_set_segment_base(SEGBASE_FS,
705                                                                   addr);
706                         }
707                 }
708                 put_cpu();
709                 break;
710         case ARCH_GET_FS: { 
711                 unsigned long base; 
712                 if (task->thread.fsindex == FS_TLS_SEL)
713                         base = read_32bit_tls(task, FS_TLS);
714                 else if (doit)
715                         rdmsrl(MSR_FS_BASE, base);
716                 else
717                         base = task->thread.fs;
718                 ret = put_user(base, (unsigned long __user *)addr); 
719                 break; 
720         }
721         case ARCH_GET_GS: { 
722                 unsigned long base;
723                 unsigned gsindex;
724                 if (task->thread.gsindex == GS_TLS_SEL)
725                         base = read_32bit_tls(task, GS_TLS);
726                 else if (doit) {
727                         asm("movl %%gs,%0" : "=r" (gsindex));
728                         if (gsindex)
729                                 rdmsrl(MSR_KERNEL_GS_BASE, base);
730                         else
731                                 base = task->thread.gs;
732                 }
733                 else
734                         base = task->thread.gs;
735                 ret = put_user(base, (unsigned long __user *)addr); 
736                 break;
737         }
738
739         default:
740                 ret = -EINVAL;
741                 break;
742         } 
743
744         return ret;     
745
746
747 long sys_arch_prctl(int code, unsigned long addr)
748 {
749         return do_arch_prctl(current, code, addr);
750
751
752 /* 
753  * Capture the user space registers if the task is not running (in user space)
754  */
755 int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
756 {
757         struct pt_regs *pp, ptregs;
758
759         pp = task_pt_regs(tsk);
760
761         ptregs = *pp; 
762         ptregs.cs &= 0xffff;
763         ptregs.ss &= 0xffff;
764
765         elf_core_copy_regs(regs, &ptregs);
766  
767         boot_option_idle_override = 1;
768         return 1;
769 }
770
771 unsigned long arch_align_stack(unsigned long sp)
772 {
773         if (randomize_va_space)
774                 sp -= get_random_int() % 8192;
775         return sp & ~0xf;
776 }
777
778 #ifndef CONFIG_SMP
779 void _restore_vcpu(void)
780 {
781 }
782 #endif