ia64/xen-unstable

view linux-2.6-xen-sparse/arch/xen/i386/kernel/process.c @ 6316:f7dfaa2af90c

merge?
author cl349@firebug.cl.cam.ac.uk
date Sun Aug 21 11:02:00 2005 +0000 (2005-08-21)
parents 1872e09bfba3
children 6721abf6b16d
line source
1 /*
2 * linux/arch/i386/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 */
10 /*
11 * This file handles the architecture-dependent parts of process handling..
12 */
14 #include <stdarg.h>
16 #include <linux/cpu.h>
17 #include <linux/errno.h>
18 #include <linux/sched.h>
19 #include <linux/fs.h>
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/elfcore.h>
23 #include <linux/smp.h>
24 #include <linux/smp_lock.h>
25 #include <linux/stddef.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/user.h>
29 #include <linux/a.out.h>
30 #include <linux/interrupt.h>
31 #include <linux/config.h>
32 #include <linux/utsname.h>
33 #include <linux/delay.h>
34 #include <linux/reboot.h>
35 #include <linux/init.h>
36 #include <linux/mc146818rtc.h>
37 #include <linux/module.h>
38 #include <linux/kallsyms.h>
39 #include <linux/ptrace.h>
40 #include <linux/random.h>
42 #include <asm/uaccess.h>
43 #include <asm/pgtable.h>
44 #include <asm/system.h>
45 #include <asm/io.h>
46 #include <asm/ldt.h>
47 #include <asm/processor.h>
48 #include <asm/i387.h>
49 #include <asm/irq.h>
50 #include <asm/desc.h>
51 #include <asm-xen/xen-public/physdev.h>
52 #ifdef CONFIG_MATH_EMULATION
53 #include <asm/math_emu.h>
54 #endif
56 #include <linux/irq.h>
57 #include <linux/err.h>
59 #include <asm/tlbflush.h>
60 #include <asm/cpu.h>
62 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
64 static int hlt_counter;
66 unsigned long boot_option_idle_override = 0;
67 EXPORT_SYMBOL(boot_option_idle_override);
69 /*
70 * Return saved PC of a blocked thread.
71 */
72 unsigned long thread_saved_pc(struct task_struct *tsk)
73 {
74 return ((unsigned long *)tsk->thread.esp)[3];
75 }
77 /*
78 * Powermanagement idle function, if any..
79 */
80 void (*pm_idle)(void);
81 static DEFINE_PER_CPU(unsigned int, cpu_idle_state);
83 void disable_hlt(void)
84 {
85 hlt_counter++;
86 }
88 EXPORT_SYMBOL(disable_hlt);
90 void enable_hlt(void)
91 {
92 hlt_counter--;
93 }
95 EXPORT_SYMBOL(enable_hlt);
97 /* XXX XEN doesn't use default_idle(), poll_idle(). Use xen_idle() instead. */
98 extern void stop_hz_timer(void);
99 extern void start_hz_timer(void);
100 void xen_idle(void)
101 {
102 local_irq_disable();
104 if (need_resched()) {
105 local_irq_enable();
106 } else {
107 stop_hz_timer();
108 HYPERVISOR_block(); /* implicit local_irq_enable() */
109 start_hz_timer();
110 }
111 }
113 #ifdef CONFIG_HOTPLUG_CPU
114 #include <asm/nmi.h>
115 /* We don't actually take CPU down, just spin without interrupts. */
116 static inline void play_dead(void)
117 {
118 /* Death loop */
119 while (__get_cpu_var(cpu_state) != CPU_UP_PREPARE)
120 HYPERVISOR_yield();
122 __flush_tlb_all();
123 cpu_set(smp_processor_id(), cpu_online_map);
124 }
125 #else
126 static inline void play_dead(void)
127 {
128 BUG();
129 }
130 #endif /* CONFIG_HOTPLUG_CPU */
132 /*
133 * The idle thread. There's no useful work to be
134 * done, so just try to conserve power and have a
135 * low exit latency (ie sit in a loop waiting for
136 * somebody to say that they'd like to reschedule)
137 */
138 void cpu_idle (void)
139 {
140 int cpu = _smp_processor_id();
142 /* endless idle loop with no priority at all */
143 while (1) {
144 while (!need_resched()) {
146 if (__get_cpu_var(cpu_idle_state))
147 __get_cpu_var(cpu_idle_state) = 0;
148 rmb();
150 if (cpu_is_offline(cpu)) {
151 local_irq_disable();
152 /* Ack it. From this point on until
153 we get woken up, we're not allowed
154 to take any locks. In particular,
155 don't printk. */
156 __get_cpu_var(cpu_state) = CPU_DEAD;
157 #if defined(CONFIG_XEN) && defined(CONFIG_HOTPLUG_CPU)
158 /* Tell hypervisor to take vcpu down. */
159 HYPERVISOR_vcpu_down(cpu);
160 #endif
161 play_dead();
162 local_irq_enable();
163 }
165 __get_cpu_var(irq_stat).idle_timestamp = jiffies;
166 xen_idle();
167 }
168 schedule();
169 }
170 }
172 void cpu_idle_wait(void)
173 {
174 unsigned int cpu, this_cpu = get_cpu();
175 cpumask_t map;
177 set_cpus_allowed(current, cpumask_of_cpu(this_cpu));
178 put_cpu();
180 cpus_clear(map);
181 for_each_online_cpu(cpu) {
182 per_cpu(cpu_idle_state, cpu) = 1;
183 cpu_set(cpu, map);
184 }
186 __get_cpu_var(cpu_idle_state) = 0;
188 wmb();
189 do {
190 ssleep(1);
191 for_each_online_cpu(cpu) {
192 if (cpu_isset(cpu, map) && !per_cpu(cpu_idle_state, cpu))
193 cpu_clear(cpu, map);
194 }
195 cpus_and(map, map, cpu_online_map);
196 } while (!cpus_empty(map));
197 }
198 EXPORT_SYMBOL_GPL(cpu_idle_wait);
200 /* XXX XEN doesn't use mwait_idle(), select_idle_routine(), idle_setup(). */
201 /* Always use xen_idle() instead. */
202 void __init select_idle_routine(const struct cpuinfo_x86 *c) {}
204 void show_regs(struct pt_regs * regs)
205 {
206 printk("\n");
207 printk("Pid: %d, comm: %20s\n", current->pid, current->comm);
208 printk("EIP: %04x:[<%08lx>] CPU: %d\n",0xffff & regs->xcs,regs->eip, smp_processor_id());
209 print_symbol("EIP is at %s\n", regs->eip);
211 if (regs->xcs & 2)
212 printk(" ESP: %04x:%08lx",0xffff & regs->xss,regs->esp);
213 printk(" EFLAGS: %08lx %s (%s)\n",
214 regs->eflags, print_tainted(), system_utsname.release);
215 printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
216 regs->eax,regs->ebx,regs->ecx,regs->edx);
217 printk("ESI: %08lx EDI: %08lx EBP: %08lx",
218 regs->esi, regs->edi, regs->ebp);
219 printk(" DS: %04x ES: %04x\n",
220 0xffff & regs->xds,0xffff & regs->xes);
222 show_trace(NULL, &regs->esp);
223 }
225 /*
226 * This gets run with %ebx containing the
227 * function to call, and %edx containing
228 * the "args".
229 */
230 extern void kernel_thread_helper(void);
231 __asm__(".section .text\n"
232 ".align 4\n"
233 "kernel_thread_helper:\n\t"
234 "movl %edx,%eax\n\t"
235 "pushl %edx\n\t"
236 "call *%ebx\n\t"
237 "pushl %eax\n\t"
238 "call do_exit\n"
239 ".previous");
241 /*
242 * Create a kernel thread
243 */
244 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
245 {
246 struct pt_regs regs;
248 memset(&regs, 0, sizeof(regs));
250 regs.ebx = (unsigned long) fn;
251 regs.edx = (unsigned long) arg;
253 regs.xds = __USER_DS;
254 regs.xes = __USER_DS;
255 regs.orig_eax = -1;
256 regs.eip = (unsigned long) kernel_thread_helper;
257 regs.xcs = __KERNEL_CS;
258 regs.eflags = X86_EFLAGS_IF | X86_EFLAGS_SF | X86_EFLAGS_PF | 0x2;
260 /* Ok, create the new process.. */
261 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
262 }
264 /*
265 * Free current thread data structures etc..
266 */
267 void exit_thread(void)
268 {
269 struct task_struct *tsk = current;
270 struct thread_struct *t = &tsk->thread;
272 /* The process may have allocated an io port bitmap... nuke it. */
273 if (unlikely(NULL != t->io_bitmap_ptr)) {
274 physdev_op_t op = { 0 };
275 op.cmd = PHYSDEVOP_SET_IOBITMAP;
276 HYPERVISOR_physdev_op(&op);
277 kfree(t->io_bitmap_ptr);
278 t->io_bitmap_ptr = NULL;
279 }
280 }
282 void flush_thread(void)
283 {
284 struct task_struct *tsk = current;
286 memset(tsk->thread.debugreg, 0, sizeof(unsigned long)*8);
287 memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
288 /*
289 * Forget coprocessor state..
290 */
291 clear_fpu(tsk);
292 clear_used_math();
293 }
295 void release_thread(struct task_struct *dead_task)
296 {
297 if (dead_task->mm) {
298 // temporary debugging check
299 if (dead_task->mm->context.size) {
300 printk("WARNING: dead process %8s still has LDT? <%p/%d>\n",
301 dead_task->comm,
302 dead_task->mm->context.ldt,
303 dead_task->mm->context.size);
304 BUG();
305 }
306 }
308 release_vm86_irqs(dead_task);
309 }
311 /*
312 * This gets called before we allocate a new thread and copy
313 * the current task into it.
314 */
315 void prepare_to_copy(struct task_struct *tsk)
316 {
317 unlazy_fpu(tsk);
318 }
320 int copy_thread(int nr, unsigned long clone_flags, unsigned long esp,
321 unsigned long unused,
322 struct task_struct * p, struct pt_regs * regs)
323 {
324 struct pt_regs * childregs;
325 struct task_struct *tsk;
326 int err;
328 childregs = ((struct pt_regs *) (THREAD_SIZE + (unsigned long) p->thread_info)) - 1;
329 /*
330 * The below -8 is to reserve 8 bytes on top of the ring0 stack.
331 * This is necessary to guarantee that the entire "struct pt_regs"
332 * is accessable even if the CPU haven't stored the SS/ESP registers
333 * on the stack (interrupt gate does not save these registers
334 * when switching to the same priv ring).
335 * Therefore beware: accessing the xss/esp fields of the
336 * "struct pt_regs" is possible, but they may contain the
337 * completely wrong values.
338 */
339 childregs = (struct pt_regs *) ((unsigned long) childregs - 8);
340 *childregs = *regs;
341 childregs->eax = 0;
342 childregs->esp = esp;
344 p->thread.esp = (unsigned long) childregs;
345 p->thread.esp0 = (unsigned long) (childregs+1);
347 p->thread.eip = (unsigned long) ret_from_fork;
349 savesegment(fs,p->thread.fs);
350 savesegment(gs,p->thread.gs);
352 tsk = current;
353 if (unlikely(NULL != tsk->thread.io_bitmap_ptr)) {
354 p->thread.io_bitmap_ptr = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
355 if (!p->thread.io_bitmap_ptr) {
356 p->thread.io_bitmap_max = 0;
357 return -ENOMEM;
358 }
359 memcpy(p->thread.io_bitmap_ptr, tsk->thread.io_bitmap_ptr,
360 IO_BITMAP_BYTES);
361 }
363 /*
364 * Set a new TLS for the child thread?
365 */
366 if (clone_flags & CLONE_SETTLS) {
367 struct desc_struct *desc;
368 struct user_desc info;
369 int idx;
371 err = -EFAULT;
372 if (copy_from_user(&info, (void __user *)childregs->esi, sizeof(info)))
373 goto out;
374 err = -EINVAL;
375 if (LDT_empty(&info))
376 goto out;
378 idx = info.entry_number;
379 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
380 goto out;
382 desc = p->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
383 desc->a = LDT_entry_a(&info);
384 desc->b = LDT_entry_b(&info);
385 }
387 p->thread.io_pl = current->thread.io_pl;
389 err = 0;
390 out:
391 if (err && p->thread.io_bitmap_ptr) {
392 kfree(p->thread.io_bitmap_ptr);
393 p->thread.io_bitmap_max = 0;
394 }
395 return err;
396 }
398 /*
399 * fill in the user structure for a core dump..
400 */
401 void dump_thread(struct pt_regs * regs, struct user * dump)
402 {
403 int i;
405 /* changed the size calculations - should hopefully work better. lbt */
406 dump->magic = CMAGIC;
407 dump->start_code = 0;
408 dump->start_stack = regs->esp & ~(PAGE_SIZE - 1);
409 dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
410 dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT;
411 dump->u_dsize -= dump->u_tsize;
412 dump->u_ssize = 0;
413 for (i = 0; i < 8; i++)
414 dump->u_debugreg[i] = current->thread.debugreg[i];
416 if (dump->start_stack < TASK_SIZE)
417 dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT;
419 dump->regs.ebx = regs->ebx;
420 dump->regs.ecx = regs->ecx;
421 dump->regs.edx = regs->edx;
422 dump->regs.esi = regs->esi;
423 dump->regs.edi = regs->edi;
424 dump->regs.ebp = regs->ebp;
425 dump->regs.eax = regs->eax;
426 dump->regs.ds = regs->xds;
427 dump->regs.es = regs->xes;
428 savesegment(fs,dump->regs.fs);
429 savesegment(gs,dump->regs.gs);
430 dump->regs.orig_eax = regs->orig_eax;
431 dump->regs.eip = regs->eip;
432 dump->regs.cs = regs->xcs;
433 dump->regs.eflags = regs->eflags;
434 dump->regs.esp = regs->esp;
435 dump->regs.ss = regs->xss;
437 dump->u_fpvalid = dump_fpu (regs, &dump->i387);
438 }
440 /*
441 * Capture the user space registers if the task is not running (in user space)
442 */
443 int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
444 {
445 struct pt_regs ptregs;
447 ptregs = *(struct pt_regs *)
448 ((unsigned long)tsk->thread_info+THREAD_SIZE - sizeof(ptregs));
449 ptregs.xcs &= 0xffff;
450 ptregs.xds &= 0xffff;
451 ptregs.xes &= 0xffff;
452 ptregs.xss &= 0xffff;
454 elf_core_copy_regs(regs, &ptregs);
456 boot_option_idle_override = 1;
457 return 1;
458 }
461 /*
462 * switch_to(x,yn) should switch tasks from x to y.
463 *
464 * We fsave/fwait so that an exception goes off at the right time
465 * (as a call from the fsave or fwait in effect) rather than to
466 * the wrong process. Lazy FP saving no longer makes any sense
467 * with modern CPU's, and this simplifies a lot of things (SMP
468 * and UP become the same).
469 *
470 * NOTE! We used to use the x86 hardware context switching. The
471 * reason for not using it any more becomes apparent when you
472 * try to recover gracefully from saved state that is no longer
473 * valid (stale segment register values in particular). With the
474 * hardware task-switch, there is no way to fix up bad state in
475 * a reasonable manner.
476 *
477 * The fact that Intel documents the hardware task-switching to
478 * be slow is a fairly red herring - this code is not noticeably
479 * faster. However, there _is_ some room for improvement here,
480 * so the performance issues may eventually be a valid point.
481 * More important, however, is the fact that this allows us much
482 * more flexibility.
483 *
484 * The return value (in %eax) will be the "prev" task after
485 * the task-switch, and shows up in ret_from_fork in entry.S,
486 * for example.
487 */
488 struct task_struct fastcall * __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
489 {
490 struct thread_struct *prev = &prev_p->thread,
491 *next = &next_p->thread;
492 int cpu = smp_processor_id();
493 struct tss_struct *tss = &per_cpu(init_tss, cpu);
494 physdev_op_t iopl_op, iobmp_op;
495 multicall_entry_t _mcl[8], *mcl = _mcl;
497 /* XEN NOTE: FS/GS saved in switch_mm(), not here. */
499 /*
500 * This is basically '__unlazy_fpu', except that we queue a
501 * multicall to indicate FPU task switch, rather than
502 * synchronously trapping to Xen.
503 */
504 if (prev_p->thread_info->status & TS_USEDFPU) {
505 __save_init_fpu(prev_p); /* _not_ save_init_fpu() */
506 mcl->op = __HYPERVISOR_fpu_taskswitch;
507 mcl->args[0] = 1;
508 mcl++;
509 }
511 /*
512 * Reload esp0, LDT and the page table pointer:
513 * This is load_esp0(tss, next) with a multicall.
514 */
515 tss->esp0 = next->esp0;
516 mcl->op = __HYPERVISOR_stack_switch;
517 mcl->args[0] = tss->ss0;
518 mcl->args[1] = tss->esp0;
519 mcl++;
521 /*
522 * Load the per-thread Thread-Local Storage descriptor.
523 * This is load_TLS(next, cpu) with multicalls.
524 */
525 #define C(i) do { \
526 if (unlikely(next->tls_array[i].a != prev->tls_array[i].a || \
527 next->tls_array[i].b != prev->tls_array[i].b)) { \
528 mcl->op = __HYPERVISOR_update_descriptor; \
529 *(u64 *)&mcl->args[0] = virt_to_machine( \
530 &get_cpu_gdt_table(cpu)[GDT_ENTRY_TLS_MIN + i]);\
531 *(u64 *)&mcl->args[2] = *(u64 *)&next->tls_array[i]; \
532 mcl++; \
533 } \
534 } while (0)
535 C(0); C(1); C(2);
536 #undef C
538 if (unlikely(prev->io_pl != next->io_pl)) {
539 iopl_op.cmd = PHYSDEVOP_SET_IOPL;
540 iopl_op.u.set_iopl.iopl = (next->io_pl == 0) ? 1 : next->io_pl;
541 mcl->op = __HYPERVISOR_physdev_op;
542 mcl->args[0] = (unsigned long)&iopl_op;
543 mcl++;
544 }
546 if (unlikely(prev->io_bitmap_ptr || next->io_bitmap_ptr)) {
547 iobmp_op.cmd =
548 PHYSDEVOP_SET_IOBITMAP;
549 iobmp_op.u.set_iobitmap.bitmap =
550 (char *)next->io_bitmap_ptr;
551 iobmp_op.u.set_iobitmap.nr_ports =
552 next->io_bitmap_ptr ? IO_BITMAP_BITS : 0;
553 mcl->op = __HYPERVISOR_physdev_op;
554 mcl->args[0] = (unsigned long)&iobmp_op;
555 mcl++;
556 }
558 (void)HYPERVISOR_multicall(_mcl, mcl - _mcl);
560 /*
561 * Restore %fs and %gs if needed.
562 */
563 if (unlikely(next->fs | next->gs)) {
564 loadsegment(fs, next->fs);
565 loadsegment(gs, next->gs);
566 }
568 /*
569 * Now maybe reload the debug registers
570 */
571 if (unlikely(next->debugreg[7])) {
572 loaddebug(next, 0);
573 loaddebug(next, 1);
574 loaddebug(next, 2);
575 loaddebug(next, 3);
576 /* no 4 and 5 */
577 loaddebug(next, 6);
578 loaddebug(next, 7);
579 }
581 return prev_p;
582 }
584 asmlinkage int sys_fork(struct pt_regs regs)
585 {
586 return do_fork(SIGCHLD, regs.esp, &regs, 0, NULL, NULL);
587 }
589 asmlinkage int sys_clone(struct pt_regs regs)
590 {
591 unsigned long clone_flags;
592 unsigned long newsp;
593 int __user *parent_tidptr, *child_tidptr;
595 clone_flags = regs.ebx;
596 newsp = regs.ecx;
597 parent_tidptr = (int __user *)regs.edx;
598 child_tidptr = (int __user *)regs.edi;
599 if (!newsp)
600 newsp = regs.esp;
601 return do_fork(clone_flags, newsp, &regs, 0, parent_tidptr, child_tidptr);
602 }
604 /*
605 * This is trivial, and on the face of it looks like it
606 * could equally well be done in user mode.
607 *
608 * Not so, for quite unobvious reasons - register pressure.
609 * In user mode vfork() cannot have a stack frame, and if
610 * done by calling the "clone()" system call directly, you
611 * do not have enough call-clobbered registers to hold all
612 * the information you need.
613 */
614 asmlinkage int sys_vfork(struct pt_regs regs)
615 {
616 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.esp, &regs, 0, NULL, NULL);
617 }
619 /*
620 * sys_execve() executes a new program.
621 */
622 asmlinkage int sys_execve(struct pt_regs regs)
623 {
624 int error;
625 char * filename;
627 filename = getname((char __user *) regs.ebx);
628 error = PTR_ERR(filename);
629 if (IS_ERR(filename))
630 goto out;
631 error = do_execve(filename,
632 (char __user * __user *) regs.ecx,
633 (char __user * __user *) regs.edx,
634 &regs);
635 if (error == 0) {
636 task_lock(current);
637 current->ptrace &= ~PT_DTRACE;
638 task_unlock(current);
639 /* Make sure we don't return using sysenter.. */
640 set_thread_flag(TIF_IRET);
641 }
642 putname(filename);
643 out:
644 return error;
645 }
647 #define top_esp (THREAD_SIZE - sizeof(unsigned long))
648 #define top_ebp (THREAD_SIZE - 2*sizeof(unsigned long))
650 unsigned long get_wchan(struct task_struct *p)
651 {
652 unsigned long ebp, esp, eip;
653 unsigned long stack_page;
654 int count = 0;
655 if (!p || p == current || p->state == TASK_RUNNING)
656 return 0;
657 stack_page = (unsigned long)p->thread_info;
658 esp = p->thread.esp;
659 if (!stack_page || esp < stack_page || esp > top_esp+stack_page)
660 return 0;
661 /* include/asm-i386/system.h:switch_to() pushes ebp last. */
662 ebp = *(unsigned long *) esp;
663 do {
664 if (ebp < stack_page || ebp > top_ebp+stack_page)
665 return 0;
666 eip = *(unsigned long *) (ebp+4);
667 if (!in_sched_functions(eip))
668 return eip;
669 ebp = *(unsigned long *) ebp;
670 } while (count++ < 16);
671 return 0;
672 }
674 /*
675 * sys_alloc_thread_area: get a yet unused TLS descriptor index.
676 */
677 static int get_free_idx(void)
678 {
679 struct thread_struct *t = &current->thread;
680 int idx;
682 for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
683 if (desc_empty(t->tls_array + idx))
684 return idx + GDT_ENTRY_TLS_MIN;
685 return -ESRCH;
686 }
688 /*
689 * Set a given TLS descriptor:
690 */
691 asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
692 {
693 struct thread_struct *t = &current->thread;
694 struct user_desc info;
695 struct desc_struct *desc;
696 int cpu, idx;
698 if (copy_from_user(&info, u_info, sizeof(info)))
699 return -EFAULT;
700 idx = info.entry_number;
702 /*
703 * index -1 means the kernel should try to find and
704 * allocate an empty descriptor:
705 */
706 if (idx == -1) {
707 idx = get_free_idx();
708 if (idx < 0)
709 return idx;
710 if (put_user(idx, &u_info->entry_number))
711 return -EFAULT;
712 }
714 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
715 return -EINVAL;
717 desc = t->tls_array + idx - GDT_ENTRY_TLS_MIN;
719 /*
720 * We must not get preempted while modifying the TLS.
721 */
722 cpu = get_cpu();
724 if (LDT_empty(&info)) {
725 desc->a = 0;
726 desc->b = 0;
727 } else {
728 desc->a = LDT_entry_a(&info);
729 desc->b = LDT_entry_b(&info);
730 }
731 load_TLS(t, cpu);
733 put_cpu();
735 return 0;
736 }
738 /*
739 * Get the current Thread-Local Storage area:
740 */
742 #define GET_BASE(desc) ( \
743 (((desc)->a >> 16) & 0x0000ffff) | \
744 (((desc)->b << 16) & 0x00ff0000) | \
745 ( (desc)->b & 0xff000000) )
747 #define GET_LIMIT(desc) ( \
748 ((desc)->a & 0x0ffff) | \
749 ((desc)->b & 0xf0000) )
751 #define GET_32BIT(desc) (((desc)->b >> 22) & 1)
752 #define GET_CONTENTS(desc) (((desc)->b >> 10) & 3)
753 #define GET_WRITABLE(desc) (((desc)->b >> 9) & 1)
754 #define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1)
755 #define GET_PRESENT(desc) (((desc)->b >> 15) & 1)
756 #define GET_USEABLE(desc) (((desc)->b >> 20) & 1)
758 asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
759 {
760 struct user_desc info;
761 struct desc_struct *desc;
762 int idx;
764 if (get_user(idx, &u_info->entry_number))
765 return -EFAULT;
766 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
767 return -EINVAL;
769 desc = current->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
771 info.entry_number = idx;
772 info.base_addr = GET_BASE(desc);
773 info.limit = GET_LIMIT(desc);
774 info.seg_32bit = GET_32BIT(desc);
775 info.contents = GET_CONTENTS(desc);
776 info.read_exec_only = !GET_WRITABLE(desc);
777 info.limit_in_pages = GET_LIMIT_PAGES(desc);
778 info.seg_not_present = !GET_PRESENT(desc);
779 info.useable = GET_USEABLE(desc);
781 if (copy_to_user(u_info, &info, sizeof(info)))
782 return -EFAULT;
783 return 0;
784 }
786 unsigned long arch_align_stack(unsigned long sp)
787 {
788 if (randomize_va_space)
789 sp -= get_random_int() % 8192;
790 return sp & ~0xf;
791 }
794 #ifndef CONFIG_X86_SMP
795 void _restore_vcpu(void)
796 {
797 }
798 #endif