ia64/xen-unstable

view linux-2.6.10-xen-sparse/arch/xen/i386/kernel/process.c @ 3384:3b51a853296f

bitkeeper revision 1.1159.212.6 (41dd945bGeh1Ty0PqE-KiyLfzD5xgg)

Merge arcadians.cl.cam.ac.uk:/auto/groups/xeno/users/cl349/BK/xen-unstable.bk
into arcadians.cl.cam.ac.uk:/local/scratch-2/cl349/xen-unstable.bk
author cl349@arcadians.cl.cam.ac.uk
date Thu Jan 06 19:41:15 2005 +0000 (2005-01-06)
parents 39a7a74fd6f9 fe3021514eb9
children 0451cbfd268d
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/errno.h>
17 #include <linux/sched.h>
18 #include <linux/fs.h>
19 #include <linux/kernel.h>
20 #include <linux/mm.h>
21 #include <linux/elfcore.h>
22 #include <linux/smp.h>
23 #include <linux/smp_lock.h>
24 #include <linux/stddef.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include <linux/user.h>
28 #include <linux/a.out.h>
29 #include <linux/interrupt.h>
30 #include <linux/config.h>
31 #include <linux/utsname.h>
32 #include <linux/delay.h>
33 #include <linux/reboot.h>
34 #include <linux/init.h>
35 #include <linux/mc146818rtc.h>
36 #include <linux/module.h>
37 #include <linux/kallsyms.h>
38 #include <linux/ptrace.h>
40 #include <asm/uaccess.h>
41 #include <asm/pgtable.h>
42 #include <asm/system.h>
43 #include <asm/io.h>
44 #include <asm/ldt.h>
45 #include <asm/processor.h>
46 #include <asm/i387.h>
47 #include <asm/irq.h>
48 #include <asm/desc.h>
49 #include <asm-xen/multicall.h>
50 #include <asm-xen/xen-public/dom0_ops.h>
51 #ifdef CONFIG_MATH_EMULATION
52 #include <asm/math_emu.h>
53 #endif
55 #include <linux/irq.h>
56 #include <linux/err.h>
58 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
60 int hlt_counter;
62 unsigned long boot_option_idle_override = 0;
63 EXPORT_SYMBOL(boot_option_idle_override);
65 /*
66 * Return saved PC of a blocked thread.
67 */
68 unsigned long thread_saved_pc(struct task_struct *tsk)
69 {
70 return ((unsigned long *)tsk->thread.esp)[3];
71 }
73 /*
74 * Powermanagement idle function, if any..
75 */
76 void (*pm_idle)(void);
78 void disable_hlt(void)
79 {
80 hlt_counter++;
81 }
83 EXPORT_SYMBOL(disable_hlt);
85 void enable_hlt(void)
86 {
87 hlt_counter--;
88 }
90 EXPORT_SYMBOL(enable_hlt);
92 /* XXX XEN doesn't use default_idle(), poll_idle(). Use xen_idle() instead. */
93 extern int set_timeout_timer(void);
94 void xen_idle(void)
95 {
96 int cpu;
98 local_irq_disable();
100 cpu = smp_processor_id();
101 if (rcu_pending(cpu))
102 rcu_check_callbacks(cpu, 0);
104 if (need_resched()) {
105 local_irq_enable();
106 } else if (set_timeout_timer() == 0) {
107 /* NB. Blocking reenable events in a race-free manner. */
108 HYPERVISOR_block();
109 } else {
110 local_irq_enable();
111 HYPERVISOR_yield();
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 /* endless idle loop with no priority at all */
124 while (1) {
125 while (!need_resched()) {
126 /*
127 * Mark this as an RCU critical section so that
128 * synchronize_kernel() in the unload path waits
129 * for our completion.
130 */
131 rcu_read_lock();
132 irq_stat[smp_processor_id()].idle_timestamp = jiffies;
133 xen_idle();
134 rcu_read_unlock();
135 }
136 schedule();
137 }
138 }
140 /* XXX XEN doesn't use mwait_idle(), select_idle_routine(), idle_setup(). */
141 /* Always use xen_idle() instead. */
142 void __init select_idle_routine(const struct cpuinfo_x86 *c) {}
144 void show_regs(struct pt_regs * regs)
145 {
146 printk("\n");
147 printk("Pid: %d, comm: %20s\n", current->pid, current->comm);
148 printk("EIP: %04x:[<%08lx>] CPU: %d\n",0xffff & regs->xcs,regs->eip, smp_processor_id());
149 print_symbol("EIP is at %s\n", regs->eip);
151 if (regs->xcs & 2)
152 printk(" ESP: %04x:%08lx",0xffff & regs->xss,regs->esp);
153 printk(" EFLAGS: %08lx %s (%s)\n",
154 regs->eflags, print_tainted(),UTS_RELEASE);
155 printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
156 regs->eax,regs->ebx,regs->ecx,regs->edx);
157 printk("ESI: %08lx EDI: %08lx EBP: %08lx",
158 regs->esi, regs->edi, regs->ebp);
159 printk(" DS: %04x ES: %04x\n",
160 0xffff & regs->xds,0xffff & regs->xes);
162 show_trace(NULL, &regs->esp);
163 }
165 /*
166 * This gets run with %ebx containing the
167 * function to call, and %edx containing
168 * the "args".
169 */
170 extern void kernel_thread_helper(void);
171 __asm__(".section .text\n"
172 ".align 4\n"
173 "kernel_thread_helper:\n\t"
174 "movl %edx,%eax\n\t"
175 "pushl %edx\n\t"
176 "call *%ebx\n\t"
177 "pushl %eax\n\t"
178 "call do_exit\n"
179 ".previous");
181 /*
182 * Create a kernel thread
183 */
184 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
185 {
186 struct pt_regs regs;
188 memset(&regs, 0, sizeof(regs));
190 regs.ebx = (unsigned long) fn;
191 regs.edx = (unsigned long) arg;
193 regs.xds = __USER_DS;
194 regs.xes = __USER_DS;
195 regs.orig_eax = -1;
196 regs.eip = (unsigned long) kernel_thread_helper;
197 regs.xcs = __KERNEL_CS;
198 regs.eflags = X86_EFLAGS_IF | X86_EFLAGS_SF | X86_EFLAGS_PF | 0x2;
200 /* Ok, create the new process.. */
201 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
202 }
204 /*
205 * Free current thread data structures etc..
206 */
207 void exit_thread(void)
208 {
209 struct task_struct *tsk = current;
210 struct thread_struct *t = &tsk->thread;
212 /* The process may have allocated an io port bitmap... nuke it. */
213 if (unlikely(NULL != t->io_bitmap_ptr)) {
214 int cpu = get_cpu();
215 struct tss_struct *tss = &per_cpu(init_tss, cpu);
217 kfree(t->io_bitmap_ptr);
218 t->io_bitmap_ptr = NULL;
219 /*
220 * Careful, clear this in the TSS too:
221 */
222 memset(tss->io_bitmap, 0xff, tss->io_bitmap_max);
223 t->io_bitmap_max = 0;
224 tss->io_bitmap_owner = NULL;
225 tss->io_bitmap_max = 0;
226 tss->io_bitmap_base = INVALID_IO_BITMAP_OFFSET;
227 put_cpu();
228 }
229 }
231 void flush_thread(void)
232 {
233 struct task_struct *tsk = current;
235 memset(tsk->thread.debugreg, 0, sizeof(unsigned long)*8);
236 memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
237 /*
238 * Forget coprocessor state..
239 */
240 clear_fpu(tsk);
241 tsk->used_math = 0;
242 }
244 void release_thread(struct task_struct *dead_task)
245 {
246 if (dead_task->mm) {
247 // temporary debugging check
248 if (dead_task->mm->context.size) {
249 printk("WARNING: dead process %8s still has LDT? <%p/%d>\n",
250 dead_task->comm,
251 dead_task->mm->context.ldt,
252 dead_task->mm->context.size);
253 BUG();
254 }
255 }
257 release_vm86_irqs(dead_task);
258 }
260 /*
261 * This gets called before we allocate a new thread and copy
262 * the current task into it.
263 */
264 void prepare_to_copy(struct task_struct *tsk)
265 {
266 unlazy_fpu(tsk);
267 }
269 int copy_thread(int nr, unsigned long clone_flags, unsigned long esp,
270 unsigned long unused,
271 struct task_struct * p, struct pt_regs * regs)
272 {
273 struct pt_regs * childregs;
274 struct task_struct *tsk;
275 int err;
276 unsigned long eflags;
278 childregs = ((struct pt_regs *) (THREAD_SIZE + (unsigned long) p->thread_info)) - 1;
279 *childregs = *regs;
280 childregs->eax = 0;
281 childregs->esp = esp;
283 p->thread.esp = (unsigned long) childregs;
284 p->thread.esp0 = (unsigned long) (childregs+1);
286 p->thread.eip = (unsigned long) ret_from_fork;
288 savesegment(fs,p->thread.fs);
289 savesegment(gs,p->thread.gs);
291 tsk = current;
292 if (unlikely(NULL != tsk->thread.io_bitmap_ptr)) {
293 p->thread.io_bitmap_ptr = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
294 if (!p->thread.io_bitmap_ptr) {
295 p->thread.io_bitmap_max = 0;
296 return -ENOMEM;
297 }
298 memcpy(p->thread.io_bitmap_ptr, tsk->thread.io_bitmap_ptr,
299 IO_BITMAP_BYTES);
300 }
302 /*
303 * Set a new TLS for the child thread?
304 */
305 if (clone_flags & CLONE_SETTLS) {
306 struct desc_struct *desc;
307 struct user_desc info;
308 int idx;
310 err = -EFAULT;
311 if (copy_from_user(&info, (void __user *)childregs->esi, sizeof(info)))
312 goto out;
313 err = -EINVAL;
314 if (LDT_empty(&info))
315 goto out;
317 idx = info.entry_number;
318 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
319 goto out;
321 desc = p->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
322 desc->a = LDT_entry_a(&info);
323 desc->b = LDT_entry_b(&info);
324 }
327 __asm__ __volatile__ ( "pushfl; popl %0" : "=r" (eflags) : );
328 p->thread.io_pl = (eflags >> 12) & 3;
330 err = 0;
331 out:
332 if (err && p->thread.io_bitmap_ptr) {
333 kfree(p->thread.io_bitmap_ptr);
334 p->thread.io_bitmap_max = 0;
335 }
336 return err;
337 }
339 /*
340 * fill in the user structure for a core dump..
341 */
342 void dump_thread(struct pt_regs * regs, struct user * dump)
343 {
344 int i;
346 /* changed the size calculations - should hopefully work better. lbt */
347 dump->magic = CMAGIC;
348 dump->start_code = 0;
349 dump->start_stack = regs->esp & ~(PAGE_SIZE - 1);
350 dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
351 dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT;
352 dump->u_dsize -= dump->u_tsize;
353 dump->u_ssize = 0;
354 for (i = 0; i < 8; i++)
355 dump->u_debugreg[i] = current->thread.debugreg[i];
357 if (dump->start_stack < TASK_SIZE)
358 dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT;
360 dump->regs.ebx = regs->ebx;
361 dump->regs.ecx = regs->ecx;
362 dump->regs.edx = regs->edx;
363 dump->regs.esi = regs->esi;
364 dump->regs.edi = regs->edi;
365 dump->regs.ebp = regs->ebp;
366 dump->regs.eax = regs->eax;
367 dump->regs.ds = regs->xds;
368 dump->regs.es = regs->xes;
369 savesegment(fs,dump->regs.fs);
370 savesegment(gs,dump->regs.gs);
371 dump->regs.orig_eax = regs->orig_eax;
372 dump->regs.eip = regs->eip;
373 dump->regs.cs = regs->xcs;
374 dump->regs.eflags = regs->eflags;
375 dump->regs.esp = regs->esp;
376 dump->regs.ss = regs->xss;
378 dump->u_fpvalid = dump_fpu (regs, &dump->i387);
379 }
381 /*
382 * Capture the user space registers if the task is not running (in user space)
383 */
384 int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
385 {
386 struct pt_regs ptregs;
388 ptregs = *(struct pt_regs *)
389 ((unsigned long)tsk->thread_info+THREAD_SIZE - sizeof(ptregs));
390 ptregs.xcs &= 0xffff;
391 ptregs.xds &= 0xffff;
392 ptregs.xes &= 0xffff;
393 ptregs.xss &= 0xffff;
395 elf_core_copy_regs(regs, &ptregs);
397 boot_option_idle_override = 1;
398 return 1;
399 }
401 static inline void
402 handle_io_bitmap(struct thread_struct *next, struct tss_struct *tss)
403 {
404 if (!next->io_bitmap_ptr) {
405 /*
406 * Disable the bitmap via an invalid offset. We still cache
407 * the previous bitmap owner and the IO bitmap contents:
408 */
409 tss->io_bitmap_base = INVALID_IO_BITMAP_OFFSET;
410 return;
411 }
412 if (likely(next == tss->io_bitmap_owner)) {
413 /*
414 * Previous owner of the bitmap (hence the bitmap content)
415 * matches the next task, we dont have to do anything but
416 * to set a valid offset in the TSS:
417 */
418 tss->io_bitmap_base = IO_BITMAP_OFFSET;
419 return;
420 }
421 /*
422 * Lazy TSS's I/O bitmap copy. We set an invalid offset here
423 * and we let the task to get a GPF in case an I/O instruction
424 * is performed. The handler of the GPF will verify that the
425 * faulting task has a valid I/O bitmap and, it true, does the
426 * real copy and restart the instruction. This will save us
427 * redundant copies when the currently switched task does not
428 * perform any I/O during its timeslice.
429 */
430 tss->io_bitmap_base = INVALID_IO_BITMAP_OFFSET_LAZY;
431 }
432 /*
433 * This special macro can be used to load a debugging register
434 */
435 #define loaddebug(thread,register) \
436 HYPERVISOR_set_debugreg((register), \
437 (thread->debugreg[register]))
439 /*
440 * switch_to(x,yn) should switch tasks from x to y.
441 *
442 * We fsave/fwait so that an exception goes off at the right time
443 * (as a call from the fsave or fwait in effect) rather than to
444 * the wrong process. Lazy FP saving no longer makes any sense
445 * with modern CPU's, and this simplifies a lot of things (SMP
446 * and UP become the same).
447 *
448 * NOTE! We used to use the x86 hardware context switching. The
449 * reason for not using it any more becomes apparent when you
450 * try to recover gracefully from saved state that is no longer
451 * valid (stale segment register values in particular). With the
452 * hardware task-switch, there is no way to fix up bad state in
453 * a reasonable manner.
454 *
455 * The fact that Intel documents the hardware task-switching to
456 * be slow is a fairly red herring - this code is not noticeably
457 * faster. However, there _is_ some room for improvement here,
458 * so the performance issues may eventually be a valid point.
459 * More important, however, is the fact that this allows us much
460 * more flexibility.
461 *
462 * The return value (in %eax) will be the "prev" task after
463 * the task-switch, and shows up in ret_from_fork in entry.S,
464 * for example.
465 */
466 struct task_struct fastcall * __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
467 {
468 struct thread_struct *prev = &prev_p->thread,
469 *next = &next_p->thread;
470 int cpu = smp_processor_id();
471 struct tss_struct *tss = &per_cpu(init_tss, cpu);
472 dom0_op_t op;
474 /* NB. No need to disable interrupts as already done in sched.c */
475 /* __cli(); */
477 /*
478 * Save away %fs and %gs. No need to save %es and %ds, as
479 * those are always kernel segments while inside the kernel.
480 */
481 asm volatile("movl %%fs,%0":"=m" (*(int *)&prev->fs));
482 asm volatile("movl %%gs,%0":"=m" (*(int *)&prev->gs));
484 /*
485 * We clobber FS and GS here so that we avoid a GPF when
486 * restoring previous task's FS/GS values in Xen when the LDT
487 * is switched. If we don't do this then we can end up
488 * erroneously re-flushing the page-update queue when we
489 * 'execute_multicall_list'.
490 */
491 __asm__ __volatile__ (
492 "xorl %%eax,%%eax; movl %%eax,%%fs; movl %%eax,%%gs" : : :
493 "eax" );
495 MULTICALL_flush_page_update_queue();
497 /* never put a printk in __switch_to... printk() calls wake_up*() indirectly */
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);
506 queue_multicall0(__HYPERVISOR_fpu_taskswitch);
507 }
509 /*
510 * Reload esp0, LDT and the page table pointer:
511 * This is load_esp0(tss, next) with a multicall.
512 */
513 tss->esp0 = next->esp0;
514 /* This can only happen when SEP is enabled, no need to test
515 * "SEP"arately */
516 if (unlikely(tss->ss1 != next->sysenter_cs)) {
517 tss->ss1 = next->sysenter_cs;
518 wrmsr(MSR_IA32_SYSENTER_CS, next->sysenter_cs, 0);
519 }
520 queue_multicall2(__HYPERVISOR_stack_switch, tss->ss0, tss->esp0);
522 /*
523 * Load the per-thread Thread-Local Storage descriptor.
524 * This is load_TLS(next, cpu) with multicalls.
525 */
526 #define C(i) do { \
527 if (unlikely(next->tls_array[i].a != prev->tls_array[i].a || \
528 next->tls_array[i].b != prev->tls_array[i].b)) \
529 queue_multicall3(__HYPERVISOR_update_descriptor, \
530 virt_to_machine(&get_cpu_gdt_table(cpu) \
531 [GDT_ENTRY_TLS_MIN + i]), \
532 ((u32 *)&next->tls_array[i])[0], \
533 ((u32 *)&next->tls_array[i])[1]); \
534 } while (0)
535 C(0); C(1); C(2);
536 #undef C
538 if (xen_start_info.flags & SIF_PRIVILEGED) {
539 op.cmd = DOM0_IOPL;
540 op.u.iopl.domain = DOMID_SELF;
541 op.u.iopl.iopl = next->io_pl;
542 op.interface_version = DOM0_INTERFACE_VERSION;
543 queue_multicall1(__HYPERVISOR_dom0_op, (unsigned long)&op);
544 }
546 /* EXECUTE ALL TASK SWITCH XEN SYSCALLS AT THIS POINT. */
547 execute_multicall_list();
548 /* __sti(); */
550 /*
551 * Restore %fs and %gs if needed.
552 */
553 if (unlikely(prev->fs | prev->gs | next->fs | next->gs)) {
554 loadsegment(fs, next->fs);
555 loadsegment(gs, next->gs);
556 }
558 /*
559 * Now maybe reload the debug registers
560 */
561 if (unlikely(next->debugreg[7])) {
562 loaddebug(next, 0);
563 loaddebug(next, 1);
564 loaddebug(next, 2);
565 loaddebug(next, 3);
566 /* no 4 and 5 */
567 loaddebug(next, 6);
568 loaddebug(next, 7);
569 }
571 if (unlikely(prev->io_bitmap_ptr || next->io_bitmap_ptr))
572 handle_io_bitmap(next, tss);
574 return prev_p;
575 }
577 asmlinkage int sys_fork(struct pt_regs regs)
578 {
579 return do_fork(SIGCHLD, regs.esp, &regs, 0, NULL, NULL);
580 }
582 asmlinkage int sys_clone(struct pt_regs regs)
583 {
584 unsigned long clone_flags;
585 unsigned long newsp;
586 int __user *parent_tidptr, *child_tidptr;
588 clone_flags = regs.ebx;
589 newsp = regs.ecx;
590 parent_tidptr = (int __user *)regs.edx;
591 child_tidptr = (int __user *)regs.edi;
592 if (!newsp)
593 newsp = regs.esp;
594 return do_fork(clone_flags, newsp, &regs, 0, parent_tidptr, child_tidptr);
595 }
597 /*
598 * This is trivial, and on the face of it looks like it
599 * could equally well be done in user mode.
600 *
601 * Not so, for quite unobvious reasons - register pressure.
602 * In user mode vfork() cannot have a stack frame, and if
603 * done by calling the "clone()" system call directly, you
604 * do not have enough call-clobbered registers to hold all
605 * the information you need.
606 */
607 asmlinkage int sys_vfork(struct pt_regs regs)
608 {
609 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.esp, &regs, 0, NULL, NULL);
610 }
612 /*
613 * sys_execve() executes a new program.
614 */
615 asmlinkage int sys_execve(struct pt_regs regs)
616 {
617 int error;
618 char * filename;
620 filename = getname((char __user *) regs.ebx);
621 error = PTR_ERR(filename);
622 if (IS_ERR(filename))
623 goto out;
624 error = do_execve(filename,
625 (char __user * __user *) regs.ecx,
626 (char __user * __user *) regs.edx,
627 &regs);
628 if (error == 0) {
629 task_lock(current);
630 current->ptrace &= ~PT_DTRACE;
631 task_unlock(current);
632 /* Make sure we don't return using sysenter.. */
633 set_thread_flag(TIF_IRET);
634 }
635 putname(filename);
636 out:
637 return error;
638 }
640 #define top_esp (THREAD_SIZE - sizeof(unsigned long))
641 #define top_ebp (THREAD_SIZE - 2*sizeof(unsigned long))
643 unsigned long get_wchan(struct task_struct *p)
644 {
645 unsigned long ebp, esp, eip;
646 unsigned long stack_page;
647 int count = 0;
648 if (!p || p == current || p->state == TASK_RUNNING)
649 return 0;
650 stack_page = (unsigned long)p->thread_info;
651 esp = p->thread.esp;
652 if (!stack_page || esp < stack_page || esp > top_esp+stack_page)
653 return 0;
654 /* include/asm-i386/system.h:switch_to() pushes ebp last. */
655 ebp = *(unsigned long *) esp;
656 do {
657 if (ebp < stack_page || ebp > top_ebp+stack_page)
658 return 0;
659 eip = *(unsigned long *) (ebp+4);
660 if (!in_sched_functions(eip))
661 return eip;
662 ebp = *(unsigned long *) ebp;
663 } while (count++ < 16);
664 return 0;
665 }
667 /*
668 * sys_alloc_thread_area: get a yet unused TLS descriptor index.
669 */
670 static int get_free_idx(void)
671 {
672 struct thread_struct *t = &current->thread;
673 int idx;
675 for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
676 if (desc_empty(t->tls_array + idx))
677 return idx + GDT_ENTRY_TLS_MIN;
678 return -ESRCH;
679 }
681 /*
682 * Set a given TLS descriptor:
683 */
684 asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
685 {
686 struct thread_struct *t = &current->thread;
687 struct user_desc info;
688 struct desc_struct *desc;
689 int cpu, idx;
691 if (copy_from_user(&info, u_info, sizeof(info)))
692 return -EFAULT;
693 idx = info.entry_number;
695 /*
696 * index -1 means the kernel should try to find and
697 * allocate an empty descriptor:
698 */
699 if (idx == -1) {
700 idx = get_free_idx();
701 if (idx < 0)
702 return idx;
703 if (put_user(idx, &u_info->entry_number))
704 return -EFAULT;
705 }
707 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
708 return -EINVAL;
710 desc = t->tls_array + idx - GDT_ENTRY_TLS_MIN;
712 /*
713 * We must not get preempted while modifying the TLS.
714 */
715 cpu = get_cpu();
717 if (LDT_empty(&info)) {
718 desc->a = 0;
719 desc->b = 0;
720 } else {
721 desc->a = LDT_entry_a(&info);
722 desc->b = LDT_entry_b(&info);
723 }
724 load_TLS(t, cpu);
726 put_cpu();
728 return 0;
729 }
731 /*
732 * Get the current Thread-Local Storage area:
733 */
735 #define GET_BASE(desc) ( \
736 (((desc)->a >> 16) & 0x0000ffff) | \
737 (((desc)->b << 16) & 0x00ff0000) | \
738 ( (desc)->b & 0xff000000) )
740 #define GET_LIMIT(desc) ( \
741 ((desc)->a & 0x0ffff) | \
742 ((desc)->b & 0xf0000) )
744 #define GET_32BIT(desc) (((desc)->b >> 22) & 1)
745 #define GET_CONTENTS(desc) (((desc)->b >> 10) & 3)
746 #define GET_WRITABLE(desc) (((desc)->b >> 9) & 1)
747 #define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1)
748 #define GET_PRESENT(desc) (((desc)->b >> 15) & 1)
749 #define GET_USEABLE(desc) (((desc)->b >> 20) & 1)
751 asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
752 {
753 struct user_desc info;
754 struct desc_struct *desc;
755 int idx;
757 if (get_user(idx, &u_info->entry_number))
758 return -EFAULT;
759 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
760 return -EINVAL;
762 desc = current->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
764 info.entry_number = idx;
765 info.base_addr = GET_BASE(desc);
766 info.limit = GET_LIMIT(desc);
767 info.seg_32bit = GET_32BIT(desc);
768 info.contents = GET_CONTENTS(desc);
769 info.read_exec_only = !GET_WRITABLE(desc);
770 info.limit_in_pages = GET_LIMIT_PAGES(desc);
771 info.seg_not_present = !GET_PRESENT(desc);
772 info.useable = GET_USEABLE(desc);
774 if (copy_to_user(u_info, &info, sizeof(info)))
775 return -EFAULT;
776 return 0;
777 }