ia64/xen-unstable

view linux-2.6-xen-sparse/arch/xen/i386/kernel/process.c @ 6283:7f9b024a509e

Actually make suspending SMP domUs work: the previous commit didn't
bring the other vcpus up correctly.

Signed-off-by: Steven Smith, sos22@cam.ac.uk
author sos22@douglas.cl.cam.ac.uk
date Thu Aug 18 15:27:55 2005 +0000 (2005-08-18)
parents d25da0ddd9d5
children 5a7efe0cf5fb
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 printk("<0>Cpu %d going offline.\n",
152 cpu);
153 local_irq_disable();
154 /* Ack it. From this point on until
155 we get woken up, we're not allowed
156 to take any locks. In particular,
157 don't printk. */
158 __get_cpu_var(cpu_state) = CPU_DEAD;
159 #if defined(CONFIG_XEN) && defined(CONFIG_HOTPLUG_CPU)
160 /* Tell hypervisor to take vcpu down. */
161 HYPERVISOR_vcpu_down(cpu);
162 #endif
163 play_dead();
164 local_irq_enable();
165 printk("<0>Cpu %d back online.\n",
166 cpu);
167 }
169 __get_cpu_var(irq_stat).idle_timestamp = jiffies;
170 xen_idle();
171 }
172 schedule();
173 }
174 }
176 void cpu_idle_wait(void)
177 {
178 unsigned int cpu, this_cpu = get_cpu();
179 cpumask_t map;
181 set_cpus_allowed(current, cpumask_of_cpu(this_cpu));
182 put_cpu();
184 cpus_clear(map);
185 for_each_online_cpu(cpu) {
186 per_cpu(cpu_idle_state, cpu) = 1;
187 cpu_set(cpu, map);
188 }
190 __get_cpu_var(cpu_idle_state) = 0;
192 wmb();
193 do {
194 ssleep(1);
195 for_each_online_cpu(cpu) {
196 if (cpu_isset(cpu, map) && !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);
204 /* XXX XEN doesn't use mwait_idle(), select_idle_routine(), idle_setup(). */
205 /* Always use xen_idle() instead. */
206 void __init select_idle_routine(const struct cpuinfo_x86 *c) {}
208 void show_regs(struct pt_regs * regs)
209 {
210 printk("\n");
211 printk("Pid: %d, comm: %20s\n", current->pid, current->comm);
212 printk("EIP: %04x:[<%08lx>] CPU: %d\n",0xffff & regs->xcs,regs->eip, smp_processor_id());
213 print_symbol("EIP is at %s\n", regs->eip);
215 if (regs->xcs & 2)
216 printk(" ESP: %04x:%08lx",0xffff & regs->xss,regs->esp);
217 printk(" EFLAGS: %08lx %s (%s)\n",
218 regs->eflags, print_tainted(), system_utsname.release);
219 printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
220 regs->eax,regs->ebx,regs->ecx,regs->edx);
221 printk("ESI: %08lx EDI: %08lx EBP: %08lx",
222 regs->esi, regs->edi, regs->ebp);
223 printk(" DS: %04x ES: %04x\n",
224 0xffff & regs->xds,0xffff & regs->xes);
226 show_trace(NULL, &regs->esp);
227 }
229 /*
230 * This gets run with %ebx containing the
231 * function to call, and %edx containing
232 * the "args".
233 */
234 extern void kernel_thread_helper(void);
235 __asm__(".section .text\n"
236 ".align 4\n"
237 "kernel_thread_helper:\n\t"
238 "movl %edx,%eax\n\t"
239 "pushl %edx\n\t"
240 "call *%ebx\n\t"
241 "pushl %eax\n\t"
242 "call do_exit\n"
243 ".previous");
245 /*
246 * Create a kernel thread
247 */
248 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
249 {
250 struct pt_regs regs;
252 memset(&regs, 0, sizeof(regs));
254 regs.ebx = (unsigned long) fn;
255 regs.edx = (unsigned long) arg;
257 regs.xds = __USER_DS;
258 regs.xes = __USER_DS;
259 regs.orig_eax = -1;
260 regs.eip = (unsigned long) kernel_thread_helper;
261 regs.xcs = __KERNEL_CS;
262 regs.eflags = X86_EFLAGS_IF | X86_EFLAGS_SF | X86_EFLAGS_PF | 0x2;
264 /* Ok, create the new process.. */
265 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
266 }
268 /*
269 * Free current thread data structures etc..
270 */
271 void exit_thread(void)
272 {
273 struct task_struct *tsk = current;
274 struct thread_struct *t = &tsk->thread;
276 /* The process may have allocated an io port bitmap... nuke it. */
277 if (unlikely(NULL != t->io_bitmap_ptr)) {
278 physdev_op_t op = { 0 };
279 op.cmd = PHYSDEVOP_SET_IOBITMAP;
280 HYPERVISOR_physdev_op(&op);
281 kfree(t->io_bitmap_ptr);
282 t->io_bitmap_ptr = NULL;
283 }
284 }
286 void flush_thread(void)
287 {
288 struct task_struct *tsk = current;
290 memset(tsk->thread.debugreg, 0, sizeof(unsigned long)*8);
291 memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
292 /*
293 * Forget coprocessor state..
294 */
295 clear_fpu(tsk);
296 clear_used_math();
297 }
299 void release_thread(struct task_struct *dead_task)
300 {
301 if (dead_task->mm) {
302 // temporary debugging check
303 if (dead_task->mm->context.size) {
304 printk("WARNING: dead process %8s still has LDT? <%p/%d>\n",
305 dead_task->comm,
306 dead_task->mm->context.ldt,
307 dead_task->mm->context.size);
308 BUG();
309 }
310 }
312 release_vm86_irqs(dead_task);
313 }
315 /*
316 * This gets called before we allocate a new thread and copy
317 * the current task into it.
318 */
319 void prepare_to_copy(struct task_struct *tsk)
320 {
321 unlazy_fpu(tsk);
322 }
324 int copy_thread(int nr, unsigned long clone_flags, unsigned long esp,
325 unsigned long unused,
326 struct task_struct * p, struct pt_regs * regs)
327 {
328 struct pt_regs * childregs;
329 struct task_struct *tsk;
330 int err;
332 childregs = ((struct pt_regs *) (THREAD_SIZE + (unsigned long) p->thread_info)) - 1;
333 /*
334 * The below -8 is to reserve 8 bytes on top of the ring0 stack.
335 * This is necessary to guarantee that the entire "struct pt_regs"
336 * is accessable even if the CPU haven't stored the SS/ESP registers
337 * on the stack (interrupt gate does not save these registers
338 * when switching to the same priv ring).
339 * Therefore beware: accessing the xss/esp fields of the
340 * "struct pt_regs" is possible, but they may contain the
341 * completely wrong values.
342 */
343 childregs = (struct pt_regs *) ((unsigned long) childregs - 8);
344 *childregs = *regs;
345 childregs->eax = 0;
346 childregs->esp = esp;
348 p->thread.esp = (unsigned long) childregs;
349 p->thread.esp0 = (unsigned long) (childregs+1);
351 p->thread.eip = (unsigned long) ret_from_fork;
353 savesegment(fs,p->thread.fs);
354 savesegment(gs,p->thread.gs);
356 tsk = current;
357 if (unlikely(NULL != tsk->thread.io_bitmap_ptr)) {
358 p->thread.io_bitmap_ptr = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
359 if (!p->thread.io_bitmap_ptr) {
360 p->thread.io_bitmap_max = 0;
361 return -ENOMEM;
362 }
363 memcpy(p->thread.io_bitmap_ptr, tsk->thread.io_bitmap_ptr,
364 IO_BITMAP_BYTES);
365 }
367 /*
368 * Set a new TLS for the child thread?
369 */
370 if (clone_flags & CLONE_SETTLS) {
371 struct desc_struct *desc;
372 struct user_desc info;
373 int idx;
375 err = -EFAULT;
376 if (copy_from_user(&info, (void __user *)childregs->esi, sizeof(info)))
377 goto out;
378 err = -EINVAL;
379 if (LDT_empty(&info))
380 goto out;
382 idx = info.entry_number;
383 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
384 goto out;
386 desc = p->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
387 desc->a = LDT_entry_a(&info);
388 desc->b = LDT_entry_b(&info);
389 }
391 p->thread.io_pl = current->thread.io_pl;
393 err = 0;
394 out:
395 if (err && p->thread.io_bitmap_ptr) {
396 kfree(p->thread.io_bitmap_ptr);
397 p->thread.io_bitmap_max = 0;
398 }
399 return err;
400 }
402 /*
403 * fill in the user structure for a core dump..
404 */
405 void dump_thread(struct pt_regs * regs, struct user * dump)
406 {
407 int i;
409 /* changed the size calculations - should hopefully work better. lbt */
410 dump->magic = CMAGIC;
411 dump->start_code = 0;
412 dump->start_stack = regs->esp & ~(PAGE_SIZE - 1);
413 dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
414 dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT;
415 dump->u_dsize -= dump->u_tsize;
416 dump->u_ssize = 0;
417 for (i = 0; i < 8; i++)
418 dump->u_debugreg[i] = current->thread.debugreg[i];
420 if (dump->start_stack < TASK_SIZE)
421 dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT;
423 dump->regs.ebx = regs->ebx;
424 dump->regs.ecx = regs->ecx;
425 dump->regs.edx = regs->edx;
426 dump->regs.esi = regs->esi;
427 dump->regs.edi = regs->edi;
428 dump->regs.ebp = regs->ebp;
429 dump->regs.eax = regs->eax;
430 dump->regs.ds = regs->xds;
431 dump->regs.es = regs->xes;
432 savesegment(fs,dump->regs.fs);
433 savesegment(gs,dump->regs.gs);
434 dump->regs.orig_eax = regs->orig_eax;
435 dump->regs.eip = regs->eip;
436 dump->regs.cs = regs->xcs;
437 dump->regs.eflags = regs->eflags;
438 dump->regs.esp = regs->esp;
439 dump->regs.ss = regs->xss;
441 dump->u_fpvalid = dump_fpu (regs, &dump->i387);
442 }
444 /*
445 * Capture the user space registers if the task is not running (in user space)
446 */
447 int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
448 {
449 struct pt_regs ptregs;
451 ptregs = *(struct pt_regs *)
452 ((unsigned long)tsk->thread_info+THREAD_SIZE - sizeof(ptregs));
453 ptregs.xcs &= 0xffff;
454 ptregs.xds &= 0xffff;
455 ptregs.xes &= 0xffff;
456 ptregs.xss &= 0xffff;
458 elf_core_copy_regs(regs, &ptregs);
460 boot_option_idle_override = 1;
461 return 1;
462 }
465 /*
466 * switch_to(x,yn) should switch tasks from x to y.
467 *
468 * We fsave/fwait so that an exception goes off at the right time
469 * (as a call from the fsave or fwait in effect) rather than to
470 * the wrong process. Lazy FP saving no longer makes any sense
471 * with modern CPU's, and this simplifies a lot of things (SMP
472 * and UP become the same).
473 *
474 * NOTE! We used to use the x86 hardware context switching. The
475 * reason for not using it any more becomes apparent when you
476 * try to recover gracefully from saved state that is no longer
477 * valid (stale segment register values in particular). With the
478 * hardware task-switch, there is no way to fix up bad state in
479 * a reasonable manner.
480 *
481 * The fact that Intel documents the hardware task-switching to
482 * be slow is a fairly red herring - this code is not noticeably
483 * faster. However, there _is_ some room for improvement here,
484 * so the performance issues may eventually be a valid point.
485 * More important, however, is the fact that this allows us much
486 * more flexibility.
487 *
488 * The return value (in %eax) will be the "prev" task after
489 * the task-switch, and shows up in ret_from_fork in entry.S,
490 * for example.
491 */
492 struct task_struct fastcall * __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
493 {
494 struct thread_struct *prev = &prev_p->thread,
495 *next = &next_p->thread;
496 int cpu = smp_processor_id();
497 struct tss_struct *tss = &per_cpu(init_tss, cpu);
498 physdev_op_t iopl_op, iobmp_op;
499 multicall_entry_t _mcl[8], *mcl = _mcl;
501 /* XEN NOTE: FS/GS saved in switch_mm(), not here. */
503 /*
504 * This is basically '__unlazy_fpu', except that we queue a
505 * multicall to indicate FPU task switch, rather than
506 * synchronously trapping to Xen.
507 */
508 if (prev_p->thread_info->status & TS_USEDFPU) {
509 __save_init_fpu(prev_p); /* _not_ save_init_fpu() */
510 mcl->op = __HYPERVISOR_fpu_taskswitch;
511 mcl->args[0] = 1;
512 mcl++;
513 }
515 /*
516 * Reload esp0, LDT and the page table pointer:
517 * This is load_esp0(tss, next) with a multicall.
518 */
519 tss->esp0 = next->esp0;
520 mcl->op = __HYPERVISOR_stack_switch;
521 mcl->args[0] = tss->ss0;
522 mcl->args[1] = tss->esp0;
523 mcl++;
525 /*
526 * Load the per-thread Thread-Local Storage descriptor.
527 * This is load_TLS(next, cpu) with multicalls.
528 */
529 #define C(i) do { \
530 if (unlikely(next->tls_array[i].a != prev->tls_array[i].a || \
531 next->tls_array[i].b != prev->tls_array[i].b)) { \
532 mcl->op = __HYPERVISOR_update_descriptor; \
533 mcl->args[0] = virt_to_machine(&get_cpu_gdt_table(cpu) \
534 [GDT_ENTRY_TLS_MIN + i]); \
535 mcl->args[1] = ((u32 *)&next->tls_array[i])[0]; \
536 mcl->args[2] = ((u32 *)&next->tls_array[i])[1]; \
537 mcl++; \
538 } \
539 } while (0)
540 C(0); C(1); C(2);
541 #undef C
543 if (unlikely(prev->io_pl != next->io_pl)) {
544 iopl_op.cmd = PHYSDEVOP_SET_IOPL;
545 iopl_op.u.set_iopl.iopl = (next->io_pl == 0) ? 1 : next->io_pl;
546 mcl->op = __HYPERVISOR_physdev_op;
547 mcl->args[0] = (unsigned long)&iopl_op;
548 mcl++;
549 }
551 if (unlikely(prev->io_bitmap_ptr || next->io_bitmap_ptr)) {
552 iobmp_op.cmd =
553 PHYSDEVOP_SET_IOBITMAP;
554 iobmp_op.u.set_iobitmap.bitmap =
555 (unsigned long)next->io_bitmap_ptr;
556 iobmp_op.u.set_iobitmap.nr_ports =
557 next->io_bitmap_ptr ? IO_BITMAP_BITS : 0;
558 mcl->op = __HYPERVISOR_physdev_op;
559 mcl->args[0] = (unsigned long)&iobmp_op;
560 mcl++;
561 }
563 (void)HYPERVISOR_multicall(_mcl, mcl - _mcl);
565 /*
566 * Restore %fs and %gs if needed.
567 */
568 if (unlikely(next->fs | next->gs)) {
569 loadsegment(fs, next->fs);
570 loadsegment(gs, next->gs);
571 }
573 /*
574 * Now maybe reload the debug registers
575 */
576 if (unlikely(next->debugreg[7])) {
577 loaddebug(next, 0);
578 loaddebug(next, 1);
579 loaddebug(next, 2);
580 loaddebug(next, 3);
581 /* no 4 and 5 */
582 loaddebug(next, 6);
583 loaddebug(next, 7);
584 }
586 return prev_p;
587 }
589 asmlinkage int sys_fork(struct pt_regs regs)
590 {
591 return do_fork(SIGCHLD, regs.esp, &regs, 0, NULL, NULL);
592 }
594 asmlinkage int sys_clone(struct pt_regs regs)
595 {
596 unsigned long clone_flags;
597 unsigned long newsp;
598 int __user *parent_tidptr, *child_tidptr;
600 clone_flags = regs.ebx;
601 newsp = regs.ecx;
602 parent_tidptr = (int __user *)regs.edx;
603 child_tidptr = (int __user *)regs.edi;
604 if (!newsp)
605 newsp = regs.esp;
606 return do_fork(clone_flags, newsp, &regs, 0, parent_tidptr, child_tidptr);
607 }
609 /*
610 * This is trivial, and on the face of it looks like it
611 * could equally well be done in user mode.
612 *
613 * Not so, for quite unobvious reasons - register pressure.
614 * In user mode vfork() cannot have a stack frame, and if
615 * done by calling the "clone()" system call directly, you
616 * do not have enough call-clobbered registers to hold all
617 * the information you need.
618 */
619 asmlinkage int sys_vfork(struct pt_regs regs)
620 {
621 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.esp, &regs, 0, NULL, NULL);
622 }
624 /*
625 * sys_execve() executes a new program.
626 */
627 asmlinkage int sys_execve(struct pt_regs regs)
628 {
629 int error;
630 char * filename;
632 filename = getname((char __user *) regs.ebx);
633 error = PTR_ERR(filename);
634 if (IS_ERR(filename))
635 goto out;
636 error = do_execve(filename,
637 (char __user * __user *) regs.ecx,
638 (char __user * __user *) regs.edx,
639 &regs);
640 if (error == 0) {
641 task_lock(current);
642 current->ptrace &= ~PT_DTRACE;
643 task_unlock(current);
644 /* Make sure we don't return using sysenter.. */
645 set_thread_flag(TIF_IRET);
646 }
647 putname(filename);
648 out:
649 return error;
650 }
652 #define top_esp (THREAD_SIZE - sizeof(unsigned long))
653 #define top_ebp (THREAD_SIZE - 2*sizeof(unsigned long))
655 unsigned long get_wchan(struct task_struct *p)
656 {
657 unsigned long ebp, esp, eip;
658 unsigned long stack_page;
659 int count = 0;
660 if (!p || p == current || p->state == TASK_RUNNING)
661 return 0;
662 stack_page = (unsigned long)p->thread_info;
663 esp = p->thread.esp;
664 if (!stack_page || esp < stack_page || esp > top_esp+stack_page)
665 return 0;
666 /* include/asm-i386/system.h:switch_to() pushes ebp last. */
667 ebp = *(unsigned long *) esp;
668 do {
669 if (ebp < stack_page || ebp > top_ebp+stack_page)
670 return 0;
671 eip = *(unsigned long *) (ebp+4);
672 if (!in_sched_functions(eip))
673 return eip;
674 ebp = *(unsigned long *) ebp;
675 } while (count++ < 16);
676 return 0;
677 }
679 /*
680 * sys_alloc_thread_area: get a yet unused TLS descriptor index.
681 */
682 static int get_free_idx(void)
683 {
684 struct thread_struct *t = &current->thread;
685 int idx;
687 for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
688 if (desc_empty(t->tls_array + idx))
689 return idx + GDT_ENTRY_TLS_MIN;
690 return -ESRCH;
691 }
693 /*
694 * Set a given TLS descriptor:
695 */
696 asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
697 {
698 struct thread_struct *t = &current->thread;
699 struct user_desc info;
700 struct desc_struct *desc;
701 int cpu, idx;
703 if (copy_from_user(&info, u_info, sizeof(info)))
704 return -EFAULT;
705 idx = info.entry_number;
707 /*
708 * index -1 means the kernel should try to find and
709 * allocate an empty descriptor:
710 */
711 if (idx == -1) {
712 idx = get_free_idx();
713 if (idx < 0)
714 return idx;
715 if (put_user(idx, &u_info->entry_number))
716 return -EFAULT;
717 }
719 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
720 return -EINVAL;
722 desc = t->tls_array + idx - GDT_ENTRY_TLS_MIN;
724 /*
725 * We must not get preempted while modifying the TLS.
726 */
727 cpu = get_cpu();
729 if (LDT_empty(&info)) {
730 desc->a = 0;
731 desc->b = 0;
732 } else {
733 desc->a = LDT_entry_a(&info);
734 desc->b = LDT_entry_b(&info);
735 }
736 load_TLS(t, cpu);
738 put_cpu();
740 return 0;
741 }
743 /*
744 * Get the current Thread-Local Storage area:
745 */
747 #define GET_BASE(desc) ( \
748 (((desc)->a >> 16) & 0x0000ffff) | \
749 (((desc)->b << 16) & 0x00ff0000) | \
750 ( (desc)->b & 0xff000000) )
752 #define GET_LIMIT(desc) ( \
753 ((desc)->a & 0x0ffff) | \
754 ((desc)->b & 0xf0000) )
756 #define GET_32BIT(desc) (((desc)->b >> 22) & 1)
757 #define GET_CONTENTS(desc) (((desc)->b >> 10) & 3)
758 #define GET_WRITABLE(desc) (((desc)->b >> 9) & 1)
759 #define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1)
760 #define GET_PRESENT(desc) (((desc)->b >> 15) & 1)
761 #define GET_USEABLE(desc) (((desc)->b >> 20) & 1)
763 asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
764 {
765 struct user_desc info;
766 struct desc_struct *desc;
767 int idx;
769 if (get_user(idx, &u_info->entry_number))
770 return -EFAULT;
771 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
772 return -EINVAL;
774 desc = current->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
776 info.entry_number = idx;
777 info.base_addr = GET_BASE(desc);
778 info.limit = GET_LIMIT(desc);
779 info.seg_32bit = GET_32BIT(desc);
780 info.contents = GET_CONTENTS(desc);
781 info.read_exec_only = !GET_WRITABLE(desc);
782 info.limit_in_pages = GET_LIMIT_PAGES(desc);
783 info.seg_not_present = !GET_PRESENT(desc);
784 info.useable = GET_USEABLE(desc);
786 if (copy_to_user(u_info, &info, sizeof(info)))
787 return -EFAULT;
788 return 0;
789 }
791 unsigned long arch_align_stack(unsigned long sp)
792 {
793 if (randomize_va_space)
794 sp -= get_random_int() % 8192;
795 return sp & ~0xf;
796 }