ia64/xen-unstable

view linux-2.6-xen-sparse/arch/xen/i386/kernel/process.c @ 7552:07aa5213d811

Add a commented-out sanity check for lazy fpu switching.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Sun Oct 30 09:39:55 2005 +0100 (2005-10-30)
parents 5a97ee0633e8
children
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 #include <asm-xen/xen-public/vcpu.h>
53 #ifdef CONFIG_MATH_EMULATION
54 #include <asm/math_emu.h>
55 #endif
57 #include <linux/irq.h>
58 #include <linux/err.h>
60 #include <asm/tlbflush.h>
61 #include <asm/cpu.h>
63 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
65 static int hlt_counter;
67 unsigned long boot_option_idle_override = 0;
68 EXPORT_SYMBOL(boot_option_idle_override);
70 /*
71 * Return saved PC of a blocked thread.
72 */
73 unsigned long thread_saved_pc(struct task_struct *tsk)
74 {
75 return ((unsigned long *)tsk->thread.esp)[3];
76 }
78 /*
79 * Powermanagement idle function, if any..
80 */
81 void (*pm_idle)(void);
82 static DEFINE_PER_CPU(unsigned int, cpu_idle_state);
84 void disable_hlt(void)
85 {
86 hlt_counter++;
87 }
89 EXPORT_SYMBOL(disable_hlt);
91 void enable_hlt(void)
92 {
93 hlt_counter--;
94 }
96 EXPORT_SYMBOL(enable_hlt);
98 /* XXX XEN doesn't use default_idle(), poll_idle(). Use xen_idle() instead. */
99 extern void stop_hz_timer(void);
100 extern void start_hz_timer(void);
101 void xen_idle(void)
102 {
103 local_irq_disable();
105 if (need_resched()) {
106 local_irq_enable();
107 } else {
108 stop_hz_timer();
109 /* Blocking includes an implicit local_irq_enable(). */
110 HYPERVISOR_sched_op(SCHEDOP_block, 0);
111 start_hz_timer();
112 }
113 }
115 /*
116 * The idle thread. There's no useful work to be
117 * done, so just try to conserve power and have a
118 * low exit latency (ie sit in a loop waiting for
119 * somebody to say that they'd like to reschedule)
120 */
121 void cpu_idle (void)
122 {
123 #if defined(CONFIG_HOTPLUG_CPU)
124 int cpu = _smp_processor_id();
125 #endif
127 /* endless idle loop with no priority at all */
128 while (1) {
129 while (!need_resched()) {
131 if (__get_cpu_var(cpu_idle_state))
132 __get_cpu_var(cpu_idle_state) = 0;
133 rmb();
135 #if defined(CONFIG_HOTPLUG_CPU)
136 if (cpu_is_offline(cpu)) {
137 HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL);
138 local_irq_enable();
139 }
140 #endif
142 __get_cpu_var(irq_stat).idle_timestamp = jiffies;
143 xen_idle();
144 }
145 schedule();
146 }
147 }
149 void cpu_idle_wait(void)
150 {
151 unsigned int cpu, this_cpu = get_cpu();
152 cpumask_t map;
154 set_cpus_allowed(current, cpumask_of_cpu(this_cpu));
155 put_cpu();
157 cpus_clear(map);
158 for_each_online_cpu(cpu) {
159 per_cpu(cpu_idle_state, cpu) = 1;
160 cpu_set(cpu, map);
161 }
163 __get_cpu_var(cpu_idle_state) = 0;
165 wmb();
166 do {
167 ssleep(1);
168 for_each_online_cpu(cpu) {
169 if (cpu_isset(cpu, map) && !per_cpu(cpu_idle_state, cpu))
170 cpu_clear(cpu, map);
171 }
172 cpus_and(map, map, cpu_online_map);
173 } while (!cpus_empty(map));
174 }
175 EXPORT_SYMBOL_GPL(cpu_idle_wait);
177 /* XXX XEN doesn't use mwait_idle(), select_idle_routine(), idle_setup(). */
178 /* Always use xen_idle() instead. */
179 void __init select_idle_routine(const struct cpuinfo_x86 *c) {}
181 void show_regs(struct pt_regs * regs)
182 {
183 printk("\n");
184 printk("Pid: %d, comm: %20s\n", current->pid, current->comm);
185 printk("EIP: %04x:[<%08lx>] CPU: %d\n",0xffff & regs->xcs,regs->eip, smp_processor_id());
186 print_symbol("EIP is at %s\n", regs->eip);
188 if (regs->xcs & 2)
189 printk(" ESP: %04x:%08lx",0xffff & regs->xss,regs->esp);
190 printk(" EFLAGS: %08lx %s (%s)\n",
191 regs->eflags, print_tainted(), system_utsname.release);
192 printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
193 regs->eax,regs->ebx,regs->ecx,regs->edx);
194 printk("ESI: %08lx EDI: %08lx EBP: %08lx",
195 regs->esi, regs->edi, regs->ebp);
196 printk(" DS: %04x ES: %04x\n",
197 0xffff & regs->xds,0xffff & regs->xes);
199 show_trace(NULL, &regs->esp);
200 }
202 /*
203 * This gets run with %ebx containing the
204 * function to call, and %edx containing
205 * the "args".
206 */
207 extern void kernel_thread_helper(void);
208 __asm__(".section .text\n"
209 ".align 4\n"
210 "kernel_thread_helper:\n\t"
211 "movl %edx,%eax\n\t"
212 "pushl %edx\n\t"
213 "call *%ebx\n\t"
214 "pushl %eax\n\t"
215 "call do_exit\n"
216 ".previous");
218 /*
219 * Create a kernel thread
220 */
221 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
222 {
223 struct pt_regs regs;
225 memset(&regs, 0, sizeof(regs));
227 regs.ebx = (unsigned long) fn;
228 regs.edx = (unsigned long) arg;
230 regs.xds = __USER_DS;
231 regs.xes = __USER_DS;
232 regs.orig_eax = -1;
233 regs.eip = (unsigned long) kernel_thread_helper;
234 regs.xcs = __KERNEL_CS;
235 regs.eflags = X86_EFLAGS_IF | X86_EFLAGS_SF | X86_EFLAGS_PF | 0x2;
237 /* Ok, create the new process.. */
238 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
239 }
241 /*
242 * Free current thread data structures etc..
243 */
244 void exit_thread(void)
245 {
246 struct task_struct *tsk = current;
247 struct thread_struct *t = &tsk->thread;
249 /* The process may have allocated an io port bitmap... nuke it. */
250 if (unlikely(NULL != t->io_bitmap_ptr)) {
251 physdev_op_t op = { 0 };
252 op.cmd = PHYSDEVOP_SET_IOBITMAP;
253 HYPERVISOR_physdev_op(&op);
254 kfree(t->io_bitmap_ptr);
255 t->io_bitmap_ptr = NULL;
256 }
257 }
259 void flush_thread(void)
260 {
261 struct task_struct *tsk = current;
263 memset(tsk->thread.debugreg, 0, sizeof(unsigned long)*8);
264 memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
265 /*
266 * Forget coprocessor state..
267 */
268 clear_fpu(tsk);
269 clear_used_math();
270 }
272 void release_thread(struct task_struct *dead_task)
273 {
274 if (dead_task->mm) {
275 // temporary debugging check
276 if (dead_task->mm->context.size) {
277 printk("WARNING: dead process %8s still has LDT? <%p/%d>\n",
278 dead_task->comm,
279 dead_task->mm->context.ldt,
280 dead_task->mm->context.size);
281 BUG();
282 }
283 }
285 release_vm86_irqs(dead_task);
286 }
288 /*
289 * This gets called before we allocate a new thread and copy
290 * the current task into it.
291 */
292 void prepare_to_copy(struct task_struct *tsk)
293 {
294 unlazy_fpu(tsk);
295 }
297 int copy_thread(int nr, unsigned long clone_flags, unsigned long esp,
298 unsigned long unused,
299 struct task_struct * p, struct pt_regs * regs)
300 {
301 struct pt_regs * childregs;
302 struct task_struct *tsk;
303 int err;
305 childregs = ((struct pt_regs *) (THREAD_SIZE + (unsigned long) p->thread_info)) - 1;
306 /*
307 * The below -8 is to reserve 8 bytes on top of the ring0 stack.
308 * This is necessary to guarantee that the entire "struct pt_regs"
309 * is accessable even if the CPU haven't stored the SS/ESP registers
310 * on the stack (interrupt gate does not save these registers
311 * when switching to the same priv ring).
312 * Therefore beware: accessing the xss/esp fields of the
313 * "struct pt_regs" is possible, but they may contain the
314 * completely wrong values.
315 */
316 childregs = (struct pt_regs *) ((unsigned long) childregs - 8);
317 *childregs = *regs;
318 childregs->eax = 0;
319 childregs->esp = esp;
321 p->thread.esp = (unsigned long) childregs;
322 p->thread.esp0 = (unsigned long) (childregs+1);
324 p->thread.eip = (unsigned long) ret_from_fork;
326 savesegment(fs,p->thread.fs);
327 savesegment(gs,p->thread.gs);
329 tsk = current;
330 if (unlikely(NULL != tsk->thread.io_bitmap_ptr)) {
331 p->thread.io_bitmap_ptr = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
332 if (!p->thread.io_bitmap_ptr) {
333 p->thread.io_bitmap_max = 0;
334 return -ENOMEM;
335 }
336 memcpy(p->thread.io_bitmap_ptr, tsk->thread.io_bitmap_ptr,
337 IO_BITMAP_BYTES);
338 }
340 /*
341 * Set a new TLS for the child thread?
342 */
343 if (clone_flags & CLONE_SETTLS) {
344 struct desc_struct *desc;
345 struct user_desc info;
346 int idx;
348 err = -EFAULT;
349 if (copy_from_user(&info, (void __user *)childregs->esi, sizeof(info)))
350 goto out;
351 err = -EINVAL;
352 if (LDT_empty(&info))
353 goto out;
355 idx = info.entry_number;
356 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
357 goto out;
359 desc = p->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
360 desc->a = LDT_entry_a(&info);
361 desc->b = LDT_entry_b(&info);
362 }
364 p->thread.io_pl = current->thread.io_pl;
366 err = 0;
367 out:
368 if (err && p->thread.io_bitmap_ptr) {
369 kfree(p->thread.io_bitmap_ptr);
370 p->thread.io_bitmap_max = 0;
371 }
372 return err;
373 }
375 /*
376 * fill in the user structure for a core dump..
377 */
378 void dump_thread(struct pt_regs * regs, struct user * dump)
379 {
380 int i;
382 /* changed the size calculations - should hopefully work better. lbt */
383 dump->magic = CMAGIC;
384 dump->start_code = 0;
385 dump->start_stack = regs->esp & ~(PAGE_SIZE - 1);
386 dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
387 dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT;
388 dump->u_dsize -= dump->u_tsize;
389 dump->u_ssize = 0;
390 for (i = 0; i < 8; i++)
391 dump->u_debugreg[i] = current->thread.debugreg[i];
393 if (dump->start_stack < TASK_SIZE)
394 dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT;
396 dump->regs.ebx = regs->ebx;
397 dump->regs.ecx = regs->ecx;
398 dump->regs.edx = regs->edx;
399 dump->regs.esi = regs->esi;
400 dump->regs.edi = regs->edi;
401 dump->regs.ebp = regs->ebp;
402 dump->regs.eax = regs->eax;
403 dump->regs.ds = regs->xds;
404 dump->regs.es = regs->xes;
405 savesegment(fs,dump->regs.fs);
406 savesegment(gs,dump->regs.gs);
407 dump->regs.orig_eax = regs->orig_eax;
408 dump->regs.eip = regs->eip;
409 dump->regs.cs = regs->xcs;
410 dump->regs.eflags = regs->eflags;
411 dump->regs.esp = regs->esp;
412 dump->regs.ss = regs->xss;
414 dump->u_fpvalid = dump_fpu (regs, &dump->i387);
415 }
417 /*
418 * Capture the user space registers if the task is not running (in user space)
419 */
420 int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
421 {
422 struct pt_regs ptregs;
424 ptregs = *(struct pt_regs *)
425 ((unsigned long)tsk->thread_info+THREAD_SIZE - sizeof(ptregs));
426 ptregs.xcs &= 0xffff;
427 ptregs.xds &= 0xffff;
428 ptregs.xes &= 0xffff;
429 ptregs.xss &= 0xffff;
431 elf_core_copy_regs(regs, &ptregs);
433 boot_option_idle_override = 1;
434 return 1;
435 }
437 /*
438 * switch_to(x,yn) should switch tasks from x to y.
439 *
440 * We fsave/fwait so that an exception goes off at the right time
441 * (as a call from the fsave or fwait in effect) rather than to
442 * the wrong process. Lazy FP saving no longer makes any sense
443 * with modern CPU's, and this simplifies a lot of things (SMP
444 * and UP become the same).
445 *
446 * NOTE! We used to use the x86 hardware context switching. The
447 * reason for not using it any more becomes apparent when you
448 * try to recover gracefully from saved state that is no longer
449 * valid (stale segment register values in particular). With the
450 * hardware task-switch, there is no way to fix up bad state in
451 * a reasonable manner.
452 *
453 * The fact that Intel documents the hardware task-switching to
454 * be slow is a fairly red herring - this code is not noticeably
455 * faster. However, there _is_ some room for improvement here,
456 * so the performance issues may eventually be a valid point.
457 * More important, however, is the fact that this allows us much
458 * more flexibility.
459 *
460 * The return value (in %eax) will be the "prev" task after
461 * the task-switch, and shows up in ret_from_fork in entry.S,
462 * for example.
463 */
464 struct task_struct fastcall * __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
465 {
466 struct thread_struct *prev = &prev_p->thread,
467 *next = &next_p->thread;
468 int cpu = smp_processor_id();
469 struct tss_struct *tss = &per_cpu(init_tss, cpu);
470 physdev_op_t iopl_op, iobmp_op;
471 multicall_entry_t _mcl[8], *mcl = _mcl;
473 /* XEN NOTE: FS/GS saved in switch_mm(), not here. */
475 /*
476 * This is basically '__unlazy_fpu', except that we queue a
477 * multicall to indicate FPU task switch, rather than
478 * synchronously trapping to Xen.
479 */
480 if (prev_p->thread_info->status & TS_USEDFPU) {
481 __save_init_fpu(prev_p); /* _not_ save_init_fpu() */
482 mcl->op = __HYPERVISOR_fpu_taskswitch;
483 mcl->args[0] = 1;
484 mcl++;
485 }
486 #if 0 /* lazy fpu sanity check */
487 else BUG_ON(!(read_cr0() & 8));
488 #endif
490 /*
491 * Reload esp0, LDT and the page table pointer:
492 * This is load_esp0(tss, next) with a multicall.
493 */
494 tss->esp0 = next->esp0;
495 mcl->op = __HYPERVISOR_stack_switch;
496 mcl->args[0] = tss->ss0;
497 mcl->args[1] = tss->esp0;
498 mcl++;
500 /*
501 * Load the per-thread Thread-Local Storage descriptor.
502 * This is load_TLS(next, cpu) with multicalls.
503 */
504 #define C(i) do { \
505 if (unlikely(next->tls_array[i].a != prev->tls_array[i].a || \
506 next->tls_array[i].b != prev->tls_array[i].b)) { \
507 mcl->op = __HYPERVISOR_update_descriptor; \
508 *(u64 *)&mcl->args[0] = virt_to_machine( \
509 &get_cpu_gdt_table(cpu)[GDT_ENTRY_TLS_MIN + i]);\
510 *(u64 *)&mcl->args[2] = *(u64 *)&next->tls_array[i]; \
511 mcl++; \
512 } \
513 } while (0)
514 C(0); C(1); C(2);
515 #undef C
517 if (unlikely(prev->io_pl != next->io_pl)) {
518 iopl_op.cmd = PHYSDEVOP_SET_IOPL;
519 iopl_op.u.set_iopl.iopl = (next->io_pl == 0) ? 1 : next->io_pl;
520 mcl->op = __HYPERVISOR_physdev_op;
521 mcl->args[0] = (unsigned long)&iopl_op;
522 mcl++;
523 }
525 if (unlikely(prev->io_bitmap_ptr || next->io_bitmap_ptr)) {
526 iobmp_op.cmd =
527 PHYSDEVOP_SET_IOBITMAP;
528 iobmp_op.u.set_iobitmap.bitmap =
529 (char *)next->io_bitmap_ptr;
530 iobmp_op.u.set_iobitmap.nr_ports =
531 next->io_bitmap_ptr ? IO_BITMAP_BITS : 0;
532 mcl->op = __HYPERVISOR_physdev_op;
533 mcl->args[0] = (unsigned long)&iobmp_op;
534 mcl++;
535 }
537 (void)HYPERVISOR_multicall(_mcl, mcl - _mcl);
539 /*
540 * Restore %fs and %gs if needed.
541 */
542 if (unlikely(next->fs | next->gs)) {
543 loadsegment(fs, next->fs);
544 loadsegment(gs, next->gs);
545 }
547 /*
548 * Now maybe reload the debug registers
549 */
550 if (unlikely(next->debugreg[7])) {
551 loaddebug(next, 0);
552 loaddebug(next, 1);
553 loaddebug(next, 2);
554 loaddebug(next, 3);
555 /* no 4 and 5 */
556 loaddebug(next, 6);
557 loaddebug(next, 7);
558 }
560 return prev_p;
561 }
563 asmlinkage int sys_fork(struct pt_regs regs)
564 {
565 return do_fork(SIGCHLD, regs.esp, &regs, 0, NULL, NULL);
566 }
568 asmlinkage int sys_clone(struct pt_regs regs)
569 {
570 unsigned long clone_flags;
571 unsigned long newsp;
572 int __user *parent_tidptr, *child_tidptr;
574 clone_flags = regs.ebx;
575 newsp = regs.ecx;
576 parent_tidptr = (int __user *)regs.edx;
577 child_tidptr = (int __user *)regs.edi;
578 if (!newsp)
579 newsp = regs.esp;
580 return do_fork(clone_flags, newsp, &regs, 0, parent_tidptr, child_tidptr);
581 }
583 /*
584 * This is trivial, and on the face of it looks like it
585 * could equally well be done in user mode.
586 *
587 * Not so, for quite unobvious reasons - register pressure.
588 * In user mode vfork() cannot have a stack frame, and if
589 * done by calling the "clone()" system call directly, you
590 * do not have enough call-clobbered registers to hold all
591 * the information you need.
592 */
593 asmlinkage int sys_vfork(struct pt_regs regs)
594 {
595 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.esp, &regs, 0, NULL, NULL);
596 }
598 /*
599 * sys_execve() executes a new program.
600 */
601 asmlinkage int sys_execve(struct pt_regs regs)
602 {
603 int error;
604 char * filename;
606 filename = getname((char __user *) regs.ebx);
607 error = PTR_ERR(filename);
608 if (IS_ERR(filename))
609 goto out;
610 error = do_execve(filename,
611 (char __user * __user *) regs.ecx,
612 (char __user * __user *) regs.edx,
613 &regs);
614 if (error == 0) {
615 task_lock(current);
616 current->ptrace &= ~PT_DTRACE;
617 task_unlock(current);
618 /* Make sure we don't return using sysenter.. */
619 set_thread_flag(TIF_IRET);
620 }
621 putname(filename);
622 out:
623 return error;
624 }
626 #define top_esp (THREAD_SIZE - sizeof(unsigned long))
627 #define top_ebp (THREAD_SIZE - 2*sizeof(unsigned long))
629 unsigned long get_wchan(struct task_struct *p)
630 {
631 unsigned long ebp, esp, eip;
632 unsigned long stack_page;
633 int count = 0;
634 if (!p || p == current || p->state == TASK_RUNNING)
635 return 0;
636 stack_page = (unsigned long)p->thread_info;
637 esp = p->thread.esp;
638 if (!stack_page || esp < stack_page || esp > top_esp+stack_page)
639 return 0;
640 /* include/asm-i386/system.h:switch_to() pushes ebp last. */
641 ebp = *(unsigned long *) esp;
642 do {
643 if (ebp < stack_page || ebp > top_ebp+stack_page)
644 return 0;
645 eip = *(unsigned long *) (ebp+4);
646 if (!in_sched_functions(eip))
647 return eip;
648 ebp = *(unsigned long *) ebp;
649 } while (count++ < 16);
650 return 0;
651 }
653 /*
654 * sys_alloc_thread_area: get a yet unused TLS descriptor index.
655 */
656 static int get_free_idx(void)
657 {
658 struct thread_struct *t = &current->thread;
659 int idx;
661 for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
662 if (desc_empty(t->tls_array + idx))
663 return idx + GDT_ENTRY_TLS_MIN;
664 return -ESRCH;
665 }
667 /*
668 * Set a given TLS descriptor:
669 */
670 asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
671 {
672 struct thread_struct *t = &current->thread;
673 struct user_desc info;
674 struct desc_struct *desc;
675 int cpu, idx;
677 if (copy_from_user(&info, u_info, sizeof(info)))
678 return -EFAULT;
679 idx = info.entry_number;
681 /*
682 * index -1 means the kernel should try to find and
683 * allocate an empty descriptor:
684 */
685 if (idx == -1) {
686 idx = get_free_idx();
687 if (idx < 0)
688 return idx;
689 if (put_user(idx, &u_info->entry_number))
690 return -EFAULT;
691 }
693 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
694 return -EINVAL;
696 desc = t->tls_array + idx - GDT_ENTRY_TLS_MIN;
698 /*
699 * We must not get preempted while modifying the TLS.
700 */
701 cpu = get_cpu();
703 if (LDT_empty(&info)) {
704 desc->a = 0;
705 desc->b = 0;
706 } else {
707 desc->a = LDT_entry_a(&info);
708 desc->b = LDT_entry_b(&info);
709 }
710 load_TLS(t, cpu);
712 put_cpu();
714 return 0;
715 }
717 /*
718 * Get the current Thread-Local Storage area:
719 */
721 #define GET_BASE(desc) ( \
722 (((desc)->a >> 16) & 0x0000ffff) | \
723 (((desc)->b << 16) & 0x00ff0000) | \
724 ( (desc)->b & 0xff000000) )
726 #define GET_LIMIT(desc) ( \
727 ((desc)->a & 0x0ffff) | \
728 ((desc)->b & 0xf0000) )
730 #define GET_32BIT(desc) (((desc)->b >> 22) & 1)
731 #define GET_CONTENTS(desc) (((desc)->b >> 10) & 3)
732 #define GET_WRITABLE(desc) (((desc)->b >> 9) & 1)
733 #define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1)
734 #define GET_PRESENT(desc) (((desc)->b >> 15) & 1)
735 #define GET_USEABLE(desc) (((desc)->b >> 20) & 1)
737 asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
738 {
739 struct user_desc info;
740 struct desc_struct *desc;
741 int idx;
743 if (get_user(idx, &u_info->entry_number))
744 return -EFAULT;
745 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
746 return -EINVAL;
748 desc = current->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
750 info.entry_number = idx;
751 info.base_addr = GET_BASE(desc);
752 info.limit = GET_LIMIT(desc);
753 info.seg_32bit = GET_32BIT(desc);
754 info.contents = GET_CONTENTS(desc);
755 info.read_exec_only = !GET_WRITABLE(desc);
756 info.limit_in_pages = GET_LIMIT_PAGES(desc);
757 info.seg_not_present = !GET_PRESENT(desc);
758 info.useable = GET_USEABLE(desc);
760 if (copy_to_user(u_info, &info, sizeof(info)))
761 return -EFAULT;
762 return 0;
763 }
765 unsigned long arch_align_stack(unsigned long sp)
766 {
767 if (randomize_va_space)
768 sp -= get_random_int() % 8192;
769 return sp & ~0xf;
770 }