Merge to Fedora kernel-2.6.18-1.2255_FC5-vs2.0.2.2-rc9 patched with stable patch...
[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         /* Must be after DS reload */
531         if (prev_p->thread_info->status & TS_USEDFPU) {
532                 __save_init_fpu(prev_p); /* _not_ save_init_fpu() */
533                 HYPERVISOR_fpu_taskswitch(1);
534         }
535
536         /* 
537          * Switch the PDA and FPU contexts.
538          */
539         prev->userrsp = read_pda(oldrsp); 
540         write_pda(oldrsp, next->userrsp); 
541         write_pda(pcurrent, next_p); 
542
543         write_pda(kernelstack,
544                   task_stack_page(next_p) + THREAD_SIZE - PDA_STACKOFFSET);
545
546         /*
547          * Now maybe reload the debug registers
548          */
549         if (unlikely(next->debugreg7)) {
550                 set_debugreg(next->debugreg0, 0);
551                 set_debugreg(next->debugreg1, 1);
552                 set_debugreg(next->debugreg2, 2);
553                 set_debugreg(next->debugreg3, 3);
554                 /* no 4 and 5 */
555                 set_debugreg(next->debugreg6, 6);
556                 set_debugreg(next->debugreg7, 7);
557         }
558
559         return prev_p;
560 }
561
562 /*
563  * sys_execve() executes a new program.
564  */
565 asmlinkage 
566 long sys_execve(char __user *name, char __user * __user *argv,
567                 char __user * __user *envp, struct pt_regs regs)
568 {
569         long error;
570         char * filename;
571
572         filename = getname(name);
573         error = PTR_ERR(filename);
574         if (IS_ERR(filename)) 
575                 return error;
576         error = do_execve(filename, argv, envp, &regs); 
577         if (error == 0) {
578                 task_lock(current);
579                 current->ptrace &= ~PT_DTRACE;
580                 task_unlock(current);
581         }
582         putname(filename);
583         return error;
584 }
585
586 void set_personality_64bit(void)
587 {
588         /* inherit personality from parent */
589
590         /* Make sure to be in 64bit mode */
591         clear_thread_flag(TIF_IA32); 
592 }
593
594 asmlinkage long sys_fork(struct pt_regs *regs)
595 {
596         return do_fork(SIGCHLD, regs->rsp, regs, 0, NULL, NULL);
597 }
598
599 asmlinkage long
600 sys_clone(unsigned long clone_flags, unsigned long newsp,
601           void __user *parent_tid, void __user *child_tid, struct pt_regs *regs)
602 {
603         if (!newsp)
604                 newsp = regs->rsp;
605         return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
606 }
607
608 /*
609  * This is trivial, and on the face of it looks like it
610  * could equally well be done in user mode.
611  *
612  * Not so, for quite unobvious reasons - register pressure.
613  * In user mode vfork() cannot have a stack frame, and if
614  * done by calling the "clone()" system call directly, you
615  * do not have enough call-clobbered registers to hold all
616  * the information you need.
617  */
618 asmlinkage long sys_vfork(struct pt_regs *regs)
619 {
620         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->rsp, regs, 0,
621                     NULL, NULL);
622 }
623
624 unsigned long get_wchan(struct task_struct *p)
625 {
626         unsigned long stack;
627         u64 fp,rip;
628         int count = 0;
629
630         if (!p || p == current || p->state==TASK_RUNNING)
631                 return 0; 
632         stack = (unsigned long)task_stack_page(p);
633         if (p->thread.rsp < stack || p->thread.rsp > stack+THREAD_SIZE)
634                 return 0;
635         fp = *(u64 *)(p->thread.rsp);
636         do { 
637                 if (fp < (unsigned long)stack ||
638                     fp > (unsigned long)stack+THREAD_SIZE)
639                         return 0; 
640                 rip = *(u64 *)(fp+8); 
641                 if (!in_sched_functions(rip))
642                         return rip; 
643                 fp = *(u64 *)fp; 
644         } while (count++ < 16); 
645         return 0;
646 }
647
648 long do_arch_prctl(struct task_struct *task, int code, unsigned long addr)
649
650         int ret = 0; 
651         int doit = task == current;
652         int cpu;
653
654         switch (code) { 
655         case ARCH_SET_GS:
656                 if (addr >= TASK_SIZE_OF(task))
657                         return -EPERM; 
658                 cpu = get_cpu();
659                 /* handle small bases via the GDT because that's faster to 
660                    switch. */
661                 if (addr <= 0xffffffff) {  
662                         set_32bit_tls(task, GS_TLS, addr); 
663                         if (doit) { 
664                                 load_TLS(&task->thread, cpu);
665                                 load_gs_index(GS_TLS_SEL); 
666                         }
667                         task->thread.gsindex = GS_TLS_SEL; 
668                         task->thread.gs = 0;
669                 } else { 
670                         task->thread.gsindex = 0;
671                         task->thread.gs = addr;
672                         if (doit) {
673                                 load_gs_index(0);
674                                 ret = HYPERVISOR_set_segment_base(
675                                         SEGBASE_GS_USER, addr);
676                         } 
677                 }
678                 put_cpu();
679                 break;
680         case ARCH_SET_FS:
681                 /* Not strictly needed for fs, but do it for symmetry
682                    with gs */
683                 if (addr >= TASK_SIZE_OF(task))
684                         return -EPERM; 
685                 cpu = get_cpu();
686                 /* handle small bases via the GDT because that's faster to 
687                    switch. */
688                 if (addr <= 0xffffffff) { 
689                         set_32bit_tls(task, FS_TLS, addr);
690                         if (doit) { 
691                                 load_TLS(&task->thread, cpu); 
692                                 asm volatile("movl %0,%%fs" :: "r"(FS_TLS_SEL));
693                         }
694                         task->thread.fsindex = FS_TLS_SEL;
695                         task->thread.fs = 0;
696                 } else { 
697                         task->thread.fsindex = 0;
698                         task->thread.fs = addr;
699                         if (doit) {
700                                 /* set the selector to 0 to not confuse
701                                    __switch_to */
702                                 asm volatile("movl %0,%%fs" :: "r" (0));
703                                 ret = HYPERVISOR_set_segment_base(SEGBASE_FS,
704                                                                   addr);
705                         }
706                 }
707                 put_cpu();
708                 break;
709         case ARCH_GET_FS: { 
710                 unsigned long base; 
711                 if (task->thread.fsindex == FS_TLS_SEL)
712                         base = read_32bit_tls(task, FS_TLS);
713                 else if (doit)
714                         rdmsrl(MSR_FS_BASE, base);
715                 else
716                         base = task->thread.fs;
717                 ret = put_user(base, (unsigned long __user *)addr); 
718                 break; 
719         }
720         case ARCH_GET_GS: { 
721                 unsigned long base;
722                 unsigned gsindex;
723                 if (task->thread.gsindex == GS_TLS_SEL)
724                         base = read_32bit_tls(task, GS_TLS);
725                 else if (doit) {
726                         asm("movl %%gs,%0" : "=r" (gsindex));
727                         if (gsindex)
728                                 rdmsrl(MSR_KERNEL_GS_BASE, base);
729                         else
730                                 base = task->thread.gs;
731                 }
732                 else
733                         base = task->thread.gs;
734                 ret = put_user(base, (unsigned long __user *)addr); 
735                 break;
736         }
737
738         default:
739                 ret = -EINVAL;
740                 break;
741         } 
742
743         return ret;     
744
745
746 long sys_arch_prctl(int code, unsigned long addr)
747 {
748         return do_arch_prctl(current, code, addr);
749
750
751 /* 
752  * Capture the user space registers if the task is not running (in user space)
753  */
754 int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
755 {
756         struct pt_regs *pp, ptregs;
757
758         pp = task_pt_regs(tsk);
759
760         ptregs = *pp; 
761         ptregs.cs &= 0xffff;
762         ptregs.ss &= 0xffff;
763
764         elf_core_copy_regs(regs, &ptregs);
765  
766         boot_option_idle_override = 1;
767         return 1;
768 }
769
770 unsigned long arch_align_stack(unsigned long sp)
771 {
772         if (randomize_va_space)
773                 sp -= get_random_int() % 8192;
774         return sp & ~0xf;
775 }
776
777 #ifndef CONFIG_SMP
778 void _restore_vcpu(void)
779 {
780 }
781 #endif