ia64/xen-unstable

view linux-2.6-xen-sparse/arch/i386/kernel/process-xen.c @ 14127:bfd188f0cf5d

Remove duplicate #includes from Linux sparese-tree file.
From: Chuck Short <zulcss@gmail.com>
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Mon Feb 26 09:12:55 2007 +0000 (2007-02-26)
parents 3adf00179a63
children 868c28c0a4f4
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/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>
39 #include <linux/random.h>
41 #include <asm/uaccess.h>
42 #include <asm/pgtable.h>
43 #include <asm/system.h>
44 #include <asm/io.h>
45 #include <asm/ldt.h>
46 #include <asm/processor.h>
47 #include <asm/i387.h>
48 #include <asm/desc.h>
49 #include <asm/vm86.h>
50 #ifdef CONFIG_MATH_EMULATION
51 #include <asm/math_emu.h>
52 #endif
54 #include <xen/interface/physdev.h>
55 #include <xen/interface/vcpu.h>
56 #include <xen/cpu_hotplug.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 EXPORT_SYMBOL(pm_idle);
83 static DEFINE_PER_CPU(unsigned int, cpu_idle_state);
85 void disable_hlt(void)
86 {
87 hlt_counter++;
88 }
90 EXPORT_SYMBOL(disable_hlt);
92 void enable_hlt(void)
93 {
94 hlt_counter--;
95 }
97 EXPORT_SYMBOL(enable_hlt);
99 /*
100 * On SMP it's slightly faster (but much more power-consuming!)
101 * to poll the ->work.need_resched flag instead of waiting for the
102 * cross-CPU IPI to arrive. Use this option with caution.
103 */
104 static void poll_idle(void)
105 {
106 local_irq_enable();
108 asm volatile(
109 "2:"
110 "testl %0, %1;"
111 "rep; nop;"
112 "je 2b;"
113 : : "i"(_TIF_NEED_RESCHED), "m" (current_thread_info()->flags));
114 }
116 static void xen_idle(void)
117 {
118 local_irq_disable();
120 if (need_resched())
121 local_irq_enable();
122 else {
123 current_thread_info()->status &= ~TS_POLLING;
124 smp_mb__after_clear_bit();
125 safe_halt();
126 current_thread_info()->status |= TS_POLLING;
127 }
128 }
129 #ifdef CONFIG_APM_MODULE
130 EXPORT_SYMBOL(default_idle);
131 #endif
133 #ifdef CONFIG_HOTPLUG_CPU
134 extern cpumask_t cpu_initialized;
135 static inline void play_dead(void)
136 {
137 idle_task_exit();
138 local_irq_disable();
139 cpu_clear(smp_processor_id(), cpu_initialized);
140 preempt_enable_no_resched();
141 HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL);
142 cpu_bringup();
143 }
144 #else
145 static inline void play_dead(void)
146 {
147 BUG();
148 }
149 #endif /* CONFIG_HOTPLUG_CPU */
151 /*
152 * The idle thread. There's no useful work to be
153 * done, so just try to conserve power and have a
154 * low exit latency (ie sit in a loop waiting for
155 * somebody to say that they'd like to reschedule)
156 */
157 void cpu_idle(void)
158 {
159 int cpu = smp_processor_id();
161 current_thread_info()->status |= TS_POLLING;
163 /* endless idle loop with no priority at all */
164 while (1) {
165 while (!need_resched()) {
166 void (*idle)(void);
168 if (__get_cpu_var(cpu_idle_state))
169 __get_cpu_var(cpu_idle_state) = 0;
171 rmb();
172 idle = pm_idle;
174 if (!idle)
175 idle = xen_idle;
177 if (cpu_is_offline(cpu))
178 play_dead();
180 __get_cpu_var(irq_stat).idle_timestamp = jiffies;
181 idle();
182 }
183 preempt_enable_no_resched();
184 schedule();
185 preempt_disable();
186 }
187 }
189 void cpu_idle_wait(void)
190 {
191 unsigned int cpu, this_cpu = get_cpu();
192 cpumask_t map;
194 set_cpus_allowed(current, cpumask_of_cpu(this_cpu));
195 put_cpu();
197 cpus_clear(map);
198 for_each_online_cpu(cpu) {
199 per_cpu(cpu_idle_state, cpu) = 1;
200 cpu_set(cpu, map);
201 }
203 __get_cpu_var(cpu_idle_state) = 0;
205 wmb();
206 do {
207 ssleep(1);
208 for_each_online_cpu(cpu) {
209 if (cpu_isset(cpu, map) && !per_cpu(cpu_idle_state, cpu))
210 cpu_clear(cpu, map);
211 }
212 cpus_and(map, map, cpu_online_map);
213 } while (!cpus_empty(map));
214 }
215 EXPORT_SYMBOL_GPL(cpu_idle_wait);
217 void __devinit select_idle_routine(const struct cpuinfo_x86 *c)
218 {
219 }
221 static int __init idle_setup (char *str)
222 {
223 if (!strncmp(str, "poll", 4)) {
224 printk("using polling idle threads.\n");
225 pm_idle = poll_idle;
226 }
228 boot_option_idle_override = 1;
229 return 1;
230 }
232 __setup("idle=", idle_setup);
234 void show_regs(struct pt_regs * regs)
235 {
236 unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L;
238 printk("\n");
239 printk("Pid: %d, comm: %20s\n", current->pid, current->comm);
240 printk("EIP: %04x:[<%08lx>] CPU: %d\n",0xffff & regs->xcs,regs->eip, smp_processor_id());
241 print_symbol("EIP is at %s\n", regs->eip);
243 if (user_mode_vm(regs))
244 printk(" ESP: %04x:%08lx",0xffff & regs->xss,regs->esp);
245 printk(" EFLAGS: %08lx %s (%s %.*s)\n",
246 regs->eflags, print_tainted(), system_utsname.release,
247 (int)strcspn(system_utsname.version, " "),
248 system_utsname.version);
249 printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
250 regs->eax,regs->ebx,regs->ecx,regs->edx);
251 printk("ESI: %08lx EDI: %08lx EBP: %08lx",
252 regs->esi, regs->edi, regs->ebp);
253 printk(" DS: %04x ES: %04x\n",
254 0xffff & regs->xds,0xffff & regs->xes);
256 cr0 = read_cr0();
257 cr2 = read_cr2();
258 cr3 = read_cr3();
259 cr4 = read_cr4_safe();
260 printk("CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n", cr0, cr2, cr3, cr4);
261 show_trace(NULL, regs, &regs->esp);
262 }
264 /*
265 * This gets run with %ebx containing the
266 * function to call, and %edx containing
267 * the "args".
268 */
269 extern void kernel_thread_helper(void);
270 __asm__(".section .text\n"
271 ".align 4\n"
272 "kernel_thread_helper:\n\t"
273 "movl %edx,%eax\n\t"
274 "pushl %edx\n\t"
275 "call *%ebx\n\t"
276 "pushl %eax\n\t"
277 "call do_exit\n"
278 ".previous");
280 /*
281 * Create a kernel thread
282 */
283 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
284 {
285 struct pt_regs regs;
287 memset(&regs, 0, sizeof(regs));
289 regs.ebx = (unsigned long) fn;
290 regs.edx = (unsigned long) arg;
292 regs.xds = __USER_DS;
293 regs.xes = __USER_DS;
294 regs.orig_eax = -1;
295 regs.eip = (unsigned long) kernel_thread_helper;
296 regs.xcs = GET_KERNEL_CS();
297 regs.eflags = X86_EFLAGS_IF | X86_EFLAGS_SF | X86_EFLAGS_PF | 0x2;
299 /* Ok, create the new process.. */
300 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
301 }
302 EXPORT_SYMBOL(kernel_thread);
304 /*
305 * Free current thread data structures etc..
306 */
307 void exit_thread(void)
308 {
309 /* The process may have allocated an io port bitmap... nuke it. */
310 if (unlikely(test_thread_flag(TIF_IO_BITMAP))) {
311 struct task_struct *tsk = current;
312 struct thread_struct *t = &tsk->thread;
313 struct physdev_set_iobitmap set_iobitmap = { 0 };
314 HYPERVISOR_physdev_op(PHYSDEVOP_set_iobitmap, &set_iobitmap);
315 kfree(t->io_bitmap_ptr);
316 t->io_bitmap_ptr = NULL;
317 clear_thread_flag(TIF_IO_BITMAP);
318 }
319 }
321 void flush_thread(void)
322 {
323 struct task_struct *tsk = current;
325 memset(tsk->thread.debugreg, 0, sizeof(unsigned long)*8);
326 memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
327 clear_tsk_thread_flag(tsk, TIF_DEBUG);
328 /*
329 * Forget coprocessor state..
330 */
331 clear_fpu(tsk);
332 clear_used_math();
333 }
335 void release_thread(struct task_struct *dead_task)
336 {
337 BUG_ON(dead_task->mm);
338 release_vm86_irqs(dead_task);
339 }
341 /*
342 * This gets called before we allocate a new thread and copy
343 * the current task into it.
344 */
345 void prepare_to_copy(struct task_struct *tsk)
346 {
347 unlazy_fpu(tsk);
348 }
350 int copy_thread(int nr, unsigned long clone_flags, unsigned long esp,
351 unsigned long unused,
352 struct task_struct * p, struct pt_regs * regs)
353 {
354 struct pt_regs * childregs;
355 struct task_struct *tsk;
356 int err;
358 childregs = task_pt_regs(p);
359 *childregs = *regs;
360 childregs->eax = 0;
361 childregs->esp = esp;
363 p->thread.esp = (unsigned long) childregs;
364 p->thread.esp0 = (unsigned long) (childregs+1);
366 p->thread.eip = (unsigned long) ret_from_fork;
368 savesegment(fs,p->thread.fs);
369 savesegment(gs,p->thread.gs);
371 tsk = current;
372 if (unlikely(test_tsk_thread_flag(tsk, TIF_IO_BITMAP))) {
373 p->thread.io_bitmap_ptr = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
374 if (!p->thread.io_bitmap_ptr) {
375 p->thread.io_bitmap_max = 0;
376 return -ENOMEM;
377 }
378 memcpy(p->thread.io_bitmap_ptr, tsk->thread.io_bitmap_ptr,
379 IO_BITMAP_BYTES);
380 set_tsk_thread_flag(p, TIF_IO_BITMAP);
381 }
383 /*
384 * Set a new TLS for the child thread?
385 */
386 if (clone_flags & CLONE_SETTLS) {
387 struct desc_struct *desc;
388 struct user_desc info;
389 int idx;
391 err = -EFAULT;
392 if (copy_from_user(&info, (void __user *)childregs->esi, sizeof(info)))
393 goto out;
394 err = -EINVAL;
395 if (LDT_empty(&info))
396 goto out;
398 idx = info.entry_number;
399 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
400 goto out;
402 desc = p->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
403 desc->a = LDT_entry_a(&info);
404 desc->b = LDT_entry_b(&info);
405 }
407 p->thread.iopl = current->thread.iopl;
409 err = 0;
410 out:
411 if (err && p->thread.io_bitmap_ptr) {
412 kfree(p->thread.io_bitmap_ptr);
413 p->thread.io_bitmap_max = 0;
414 }
415 return err;
416 }
418 /*
419 * fill in the user structure for a core dump..
420 */
421 void dump_thread(struct pt_regs * regs, struct user * dump)
422 {
423 int i;
425 /* changed the size calculations - should hopefully work better. lbt */
426 dump->magic = CMAGIC;
427 dump->start_code = 0;
428 dump->start_stack = regs->esp & ~(PAGE_SIZE - 1);
429 dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
430 dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT;
431 dump->u_dsize -= dump->u_tsize;
432 dump->u_ssize = 0;
433 for (i = 0; i < 8; i++)
434 dump->u_debugreg[i] = current->thread.debugreg[i];
436 if (dump->start_stack < TASK_SIZE)
437 dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT;
439 dump->regs.ebx = regs->ebx;
440 dump->regs.ecx = regs->ecx;
441 dump->regs.edx = regs->edx;
442 dump->regs.esi = regs->esi;
443 dump->regs.edi = regs->edi;
444 dump->regs.ebp = regs->ebp;
445 dump->regs.eax = regs->eax;
446 dump->regs.ds = regs->xds;
447 dump->regs.es = regs->xes;
448 savesegment(fs,dump->regs.fs);
449 savesegment(gs,dump->regs.gs);
450 dump->regs.orig_eax = regs->orig_eax;
451 dump->regs.eip = regs->eip;
452 dump->regs.cs = regs->xcs;
453 dump->regs.eflags = regs->eflags;
454 dump->regs.esp = regs->esp;
455 dump->regs.ss = regs->xss;
457 dump->u_fpvalid = dump_fpu (regs, &dump->i387);
458 }
459 EXPORT_SYMBOL(dump_thread);
461 /*
462 * Capture the user space registers if the task is not running (in user space)
463 */
464 int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
465 {
466 struct pt_regs ptregs = *task_pt_regs(tsk);
467 ptregs.xcs &= 0xffff;
468 ptregs.xds &= 0xffff;
469 ptregs.xes &= 0xffff;
470 ptregs.xss &= 0xffff;
472 elf_core_copy_regs(regs, &ptregs);
474 return 1;
475 }
477 static noinline void __switch_to_xtra(struct task_struct *next_p)
478 {
479 struct thread_struct *next;
481 next = &next_p->thread;
483 if (test_tsk_thread_flag(next_p, TIF_DEBUG)) {
484 set_debugreg(next->debugreg[0], 0);
485 set_debugreg(next->debugreg[1], 1);
486 set_debugreg(next->debugreg[2], 2);
487 set_debugreg(next->debugreg[3], 3);
488 /* no 4 and 5 */
489 set_debugreg(next->debugreg[6], 6);
490 set_debugreg(next->debugreg[7], 7);
491 }
492 }
494 /*
495 * This function selects if the context switch from prev to next
496 * has to tweak the TSC disable bit in the cr4.
497 */
498 static inline void disable_tsc(struct task_struct *prev_p,
499 struct task_struct *next_p)
500 {
501 struct thread_info *prev, *next;
503 /*
504 * gcc should eliminate the ->thread_info dereference if
505 * has_secure_computing returns 0 at compile time (SECCOMP=n).
506 */
507 prev = task_thread_info(prev_p);
508 next = task_thread_info(next_p);
510 if (has_secure_computing(prev) || has_secure_computing(next)) {
511 /* slow path here */
512 if (has_secure_computing(prev) &&
513 !has_secure_computing(next)) {
514 write_cr4(read_cr4() & ~X86_CR4_TSD);
515 } else if (!has_secure_computing(prev) &&
516 has_secure_computing(next))
517 write_cr4(read_cr4() | X86_CR4_TSD);
518 }
519 }
521 /*
522 * switch_to(x,yn) should switch tasks from x to y.
523 *
524 * We fsave/fwait so that an exception goes off at the right time
525 * (as a call from the fsave or fwait in effect) rather than to
526 * the wrong process. Lazy FP saving no longer makes any sense
527 * with modern CPU's, and this simplifies a lot of things (SMP
528 * and UP become the same).
529 *
530 * NOTE! We used to use the x86 hardware context switching. The
531 * reason for not using it any more becomes apparent when you
532 * try to recover gracefully from saved state that is no longer
533 * valid (stale segment register values in particular). With the
534 * hardware task-switch, there is no way to fix up bad state in
535 * a reasonable manner.
536 *
537 * The fact that Intel documents the hardware task-switching to
538 * be slow is a fairly red herring - this code is not noticeably
539 * faster. However, there _is_ some room for improvement here,
540 * so the performance issues may eventually be a valid point.
541 * More important, however, is the fact that this allows us much
542 * more flexibility.
543 *
544 * The return value (in %eax) will be the "prev" task after
545 * the task-switch, and shows up in ret_from_fork in entry.S,
546 * for example.
547 */
548 struct task_struct fastcall * __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
549 {
550 struct thread_struct *prev = &prev_p->thread,
551 *next = &next_p->thread;
552 int cpu = smp_processor_id();
553 #ifndef CONFIG_X86_NO_TSS
554 struct tss_struct *tss = &per_cpu(init_tss, cpu);
555 #endif
556 struct physdev_set_iopl iopl_op;
557 struct physdev_set_iobitmap iobmp_op;
558 multicall_entry_t _mcl[8], *mcl = _mcl;
560 /* XEN NOTE: FS/GS saved in switch_mm(), not here. */
562 /*
563 * This is basically '__unlazy_fpu', except that we queue a
564 * multicall to indicate FPU task switch, rather than
565 * synchronously trapping to Xen.
566 */
567 if (prev_p->thread_info->status & TS_USEDFPU) {
568 __save_init_fpu(prev_p); /* _not_ save_init_fpu() */
569 mcl->op = __HYPERVISOR_fpu_taskswitch;
570 mcl->args[0] = 1;
571 mcl++;
572 }
573 #if 0 /* lazy fpu sanity check */
574 else BUG_ON(!(read_cr0() & 8));
575 #endif
577 /*
578 * Reload esp0.
579 * This is load_esp0(tss, next) with a multicall.
580 */
581 mcl->op = __HYPERVISOR_stack_switch;
582 mcl->args[0] = __KERNEL_DS;
583 mcl->args[1] = next->esp0;
584 mcl++;
586 /*
587 * Load the per-thread Thread-Local Storage descriptor.
588 * This is load_TLS(next, cpu) with multicalls.
589 */
590 #define C(i) do { \
591 if (unlikely(next->tls_array[i].a != prev->tls_array[i].a || \
592 next->tls_array[i].b != prev->tls_array[i].b)) { \
593 mcl->op = __HYPERVISOR_update_descriptor; \
594 *(u64 *)&mcl->args[0] = virt_to_machine( \
595 &get_cpu_gdt_table(cpu)[GDT_ENTRY_TLS_MIN + i]);\
596 *(u64 *)&mcl->args[2] = *(u64 *)&next->tls_array[i]; \
597 mcl++; \
598 } \
599 } while (0)
600 C(0); C(1); C(2);
601 #undef C
603 if (unlikely(prev->iopl != next->iopl)) {
604 iopl_op.iopl = (next->iopl == 0) ? 1 : (next->iopl >> 12) & 3;
605 mcl->op = __HYPERVISOR_physdev_op;
606 mcl->args[0] = PHYSDEVOP_set_iopl;
607 mcl->args[1] = (unsigned long)&iopl_op;
608 mcl++;
609 }
611 if (unlikely(prev->io_bitmap_ptr || next->io_bitmap_ptr)) {
612 iobmp_op.bitmap = (char *)next->io_bitmap_ptr;
613 iobmp_op.nr_ports = next->io_bitmap_ptr ? IO_BITMAP_BITS : 0;
614 mcl->op = __HYPERVISOR_physdev_op;
615 mcl->args[0] = PHYSDEVOP_set_iobitmap;
616 mcl->args[1] = (unsigned long)&iobmp_op;
617 mcl++;
618 }
620 (void)HYPERVISOR_multicall(_mcl, mcl - _mcl);
622 /*
623 * Restore %fs and %gs if needed.
624 *
625 * Glibc normally makes %fs be zero, and %gs is one of
626 * the TLS segments.
627 */
628 if (unlikely(next->fs))
629 loadsegment(fs, next->fs);
631 if (next->gs)
632 loadsegment(gs, next->gs);
634 /*
635 * Now maybe handle debug registers
636 */
637 if (unlikely(task_thread_info(next_p)->flags & _TIF_WORK_CTXSW))
638 __switch_to_xtra(next_p);
640 disable_tsc(prev_p, next_p);
642 return prev_p;
643 }
645 asmlinkage int sys_fork(struct pt_regs regs)
646 {
647 return do_fork(SIGCHLD, regs.esp, &regs, 0, NULL, NULL);
648 }
650 asmlinkage int sys_clone(struct pt_regs regs)
651 {
652 unsigned long clone_flags;
653 unsigned long newsp;
654 int __user *parent_tidptr, *child_tidptr;
656 clone_flags = regs.ebx;
657 newsp = regs.ecx;
658 parent_tidptr = (int __user *)regs.edx;
659 child_tidptr = (int __user *)regs.edi;
660 if (!newsp)
661 newsp = regs.esp;
662 return do_fork(clone_flags, newsp, &regs, 0, parent_tidptr, child_tidptr);
663 }
665 /*
666 * This is trivial, and on the face of it looks like it
667 * could equally well be done in user mode.
668 *
669 * Not so, for quite unobvious reasons - register pressure.
670 * In user mode vfork() cannot have a stack frame, and if
671 * done by calling the "clone()" system call directly, you
672 * do not have enough call-clobbered registers to hold all
673 * the information you need.
674 */
675 asmlinkage int sys_vfork(struct pt_regs regs)
676 {
677 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.esp, &regs, 0, NULL, NULL);
678 }
680 /*
681 * sys_execve() executes a new program.
682 */
683 asmlinkage int sys_execve(struct pt_regs regs)
684 {
685 int error;
686 char * filename;
688 filename = getname((char __user *) regs.ebx);
689 error = PTR_ERR(filename);
690 if (IS_ERR(filename))
691 goto out;
692 error = do_execve(filename,
693 (char __user * __user *) regs.ecx,
694 (char __user * __user *) regs.edx,
695 &regs);
696 if (error == 0) {
697 task_lock(current);
698 current->ptrace &= ~PT_DTRACE;
699 task_unlock(current);
700 /* Make sure we don't return using sysenter.. */
701 set_thread_flag(TIF_IRET);
702 }
703 putname(filename);
704 out:
705 return error;
706 }
708 #define top_esp (THREAD_SIZE - sizeof(unsigned long))
709 #define top_ebp (THREAD_SIZE - 2*sizeof(unsigned long))
711 unsigned long get_wchan(struct task_struct *p)
712 {
713 unsigned long ebp, esp, eip;
714 unsigned long stack_page;
715 int count = 0;
716 if (!p || p == current || p->state == TASK_RUNNING)
717 return 0;
718 stack_page = (unsigned long)task_stack_page(p);
719 esp = p->thread.esp;
720 if (!stack_page || esp < stack_page || esp > top_esp+stack_page)
721 return 0;
722 /* include/asm-i386/system.h:switch_to() pushes ebp last. */
723 ebp = *(unsigned long *) esp;
724 do {
725 if (ebp < stack_page || ebp > top_ebp+stack_page)
726 return 0;
727 eip = *(unsigned long *) (ebp+4);
728 if (!in_sched_functions(eip))
729 return eip;
730 ebp = *(unsigned long *) ebp;
731 } while (count++ < 16);
732 return 0;
733 }
735 /*
736 * sys_alloc_thread_area: get a yet unused TLS descriptor index.
737 */
738 static int get_free_idx(void)
739 {
740 struct thread_struct *t = &current->thread;
741 int idx;
743 for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
744 if (desc_empty(t->tls_array + idx))
745 return idx + GDT_ENTRY_TLS_MIN;
746 return -ESRCH;
747 }
749 /*
750 * Set a given TLS descriptor:
751 */
752 asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
753 {
754 struct thread_struct *t = &current->thread;
755 struct user_desc info;
756 struct desc_struct *desc;
757 int cpu, idx;
759 if (copy_from_user(&info, u_info, sizeof(info)))
760 return -EFAULT;
761 idx = info.entry_number;
763 /*
764 * index -1 means the kernel should try to find and
765 * allocate an empty descriptor:
766 */
767 if (idx == -1) {
768 idx = get_free_idx();
769 if (idx < 0)
770 return idx;
771 if (put_user(idx, &u_info->entry_number))
772 return -EFAULT;
773 }
775 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
776 return -EINVAL;
778 desc = t->tls_array + idx - GDT_ENTRY_TLS_MIN;
780 /*
781 * We must not get preempted while modifying the TLS.
782 */
783 cpu = get_cpu();
785 if (LDT_empty(&info)) {
786 desc->a = 0;
787 desc->b = 0;
788 } else {
789 desc->a = LDT_entry_a(&info);
790 desc->b = LDT_entry_b(&info);
791 }
792 load_TLS(t, cpu);
794 put_cpu();
796 return 0;
797 }
799 /*
800 * Get the current Thread-Local Storage area:
801 */
803 #define GET_BASE(desc) ( \
804 (((desc)->a >> 16) & 0x0000ffff) | \
805 (((desc)->b << 16) & 0x00ff0000) | \
806 ( (desc)->b & 0xff000000) )
808 #define GET_LIMIT(desc) ( \
809 ((desc)->a & 0x0ffff) | \
810 ((desc)->b & 0xf0000) )
812 #define GET_32BIT(desc) (((desc)->b >> 22) & 1)
813 #define GET_CONTENTS(desc) (((desc)->b >> 10) & 3)
814 #define GET_WRITABLE(desc) (((desc)->b >> 9) & 1)
815 #define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1)
816 #define GET_PRESENT(desc) (((desc)->b >> 15) & 1)
817 #define GET_USEABLE(desc) (((desc)->b >> 20) & 1)
819 asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
820 {
821 struct user_desc info;
822 struct desc_struct *desc;
823 int idx;
825 if (get_user(idx, &u_info->entry_number))
826 return -EFAULT;
827 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
828 return -EINVAL;
830 memset(&info, 0, sizeof(info));
832 desc = current->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
834 info.entry_number = idx;
835 info.base_addr = GET_BASE(desc);
836 info.limit = GET_LIMIT(desc);
837 info.seg_32bit = GET_32BIT(desc);
838 info.contents = GET_CONTENTS(desc);
839 info.read_exec_only = !GET_WRITABLE(desc);
840 info.limit_in_pages = GET_LIMIT_PAGES(desc);
841 info.seg_not_present = !GET_PRESENT(desc);
842 info.useable = GET_USEABLE(desc);
844 if (copy_to_user(u_info, &info, sizeof(info)))
845 return -EFAULT;
846 return 0;
847 }
849 unsigned long arch_align_stack(unsigned long sp)
850 {
851 if (randomize_va_space)
852 sp -= get_random_int() % 8192;
853 return sp & ~0xf;
854 }