upgrade to fedora-2.6.12-1.1398.FC4 + vserver 2.0.rc7
[linux-2.6.git] / arch / i386 / kernel / process.c
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  */
9
10 /*
11  * This file handles the architecture-dependent parts of process handling..
12  */
13
14 #include <stdarg.h>
15
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>
39 #include <linux/random.h>
40
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/irq.h>
49 #include <asm/desc.h>
50 #ifdef CONFIG_MATH_EMULATION
51 #include <asm/math_emu.h>
52 #endif
53
54 #include <linux/irq.h>
55 #include <linux/err.h>
56
57 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
58
59 static int hlt_counter;
60
61 unsigned long boot_option_idle_override = 0;
62 EXPORT_SYMBOL(boot_option_idle_override);
63
64 /*
65  * Return saved PC of a blocked thread.
66  */
67 unsigned long thread_saved_pc(struct task_struct *tsk)
68 {
69         return ((unsigned long *)tsk->thread.esp)[3];
70 }
71
72 /*
73  * Powermanagement idle function, if any..
74  */
75 void (*pm_idle)(void);
76 static DEFINE_PER_CPU(unsigned int, cpu_idle_state);
77
78 void disable_hlt(void)
79 {
80         hlt_counter++;
81 }
82
83 EXPORT_SYMBOL(disable_hlt);
84
85 void enable_hlt(void)
86 {
87         hlt_counter--;
88 }
89
90 EXPORT_SYMBOL(enable_hlt);
91
92 /*
93  * We use this if we don't have any better
94  * idle routine..
95  */
96 void default_idle(void)
97 {
98         if (!hlt_counter && boot_cpu_data.hlt_works_ok) {
99                 local_irq_disable();
100                 if (!need_resched())
101                         safe_halt();
102                 else
103                         local_irq_enable();
104         } else {
105                 cpu_relax();
106         }
107 }
108
109 /*
110  * On SMP it's slightly faster (but much more power-consuming!)
111  * to poll the ->work.need_resched flag instead of waiting for the
112  * cross-CPU IPI to arrive. Use this option with caution.
113  */
114 static void poll_idle (void)
115 {
116         int oldval;
117
118         local_irq_enable();
119
120         /*
121          * Deal with another CPU just having chosen a thread to
122          * run here:
123          */
124         oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);
125
126         if (!oldval) {
127                 set_thread_flag(TIF_POLLING_NRFLAG);
128                 asm volatile(
129                         "2:"
130                         "testl %0, %1;"
131                         "rep; nop;"
132                         "je 2b;"
133                         : : "i"(_TIF_NEED_RESCHED), "m" (current_thread_info()->flags));
134
135                 clear_thread_flag(TIF_POLLING_NRFLAG);
136         } else {
137                 set_need_resched();
138         }
139 }
140
141 /*
142  * The idle thread. There's no useful work to be
143  * done, so just try to conserve power and have a
144  * low exit latency (ie sit in a loop waiting for
145  * somebody to say that they'd like to reschedule)
146  */
147 void cpu_idle (void)
148 {
149         /* endless idle loop with no priority at all */
150         while (1) {
151                 while (!need_resched()) {
152                         void (*idle)(void);
153
154                         if (__get_cpu_var(cpu_idle_state))
155                                 __get_cpu_var(cpu_idle_state) = 0;
156
157                         rmb();
158                         idle = pm_idle;
159
160                         if (!idle)
161                                 idle = default_idle;
162
163                         __get_cpu_var(irq_stat).idle_timestamp = jiffies;
164                         idle();
165                 }
166                 schedule();
167         }
168 }
169
170 void cpu_idle_wait(void)
171 {
172         unsigned int cpu, this_cpu = get_cpu();
173         cpumask_t map;
174
175         set_cpus_allowed(current, cpumask_of_cpu(this_cpu));
176         put_cpu();
177
178         cpus_clear(map);
179         for_each_online_cpu(cpu) {
180                 per_cpu(cpu_idle_state, cpu) = 1;
181                 cpu_set(cpu, map);
182         }
183
184         __get_cpu_var(cpu_idle_state) = 0;
185
186         wmb();
187         do {
188                 ssleep(1);
189                 for_each_online_cpu(cpu) {
190                         if (cpu_isset(cpu, map) && !per_cpu(cpu_idle_state, cpu))
191                                 cpu_clear(cpu, map);
192                 }
193                 cpus_and(map, map, cpu_online_map);
194         } while (!cpus_empty(map));
195 }
196 EXPORT_SYMBOL_GPL(cpu_idle_wait);
197
198 /*
199  * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
200  * which can obviate IPI to trigger checking of need_resched.
201  * We execute MONITOR against need_resched and enter optimized wait state
202  * through MWAIT. Whenever someone changes need_resched, we would be woken
203  * up from MWAIT (without an IPI).
204  */
205 static void mwait_idle(void)
206 {
207         local_irq_enable();
208
209         if (!need_resched()) {
210                 set_thread_flag(TIF_POLLING_NRFLAG);
211                 do {
212                         __monitor((void *)&current_thread_info()->flags, 0, 0);
213                         if (need_resched())
214                                 break;
215                         __mwait(0, 0);
216                 } while (!need_resched());
217                 clear_thread_flag(TIF_POLLING_NRFLAG);
218         }
219 }
220
221 void __init select_idle_routine(const struct cpuinfo_x86 *c)
222 {
223         if (cpu_has(c, X86_FEATURE_MWAIT)) {
224                 printk("monitor/mwait feature present.\n");
225                 /*
226                  * Skip, if setup has overridden idle.
227                  * One CPU supports mwait => All CPUs supports mwait
228                  */
229                 if (!pm_idle) {
230                         printk("using mwait in idle threads.\n");
231                         pm_idle = mwait_idle;
232                 }
233         }
234 }
235
236 static int __init idle_setup (char *str)
237 {
238         if (!strncmp(str, "poll", 4)) {
239                 printk("using polling idle threads.\n");
240                 pm_idle = poll_idle;
241 #ifdef CONFIG_X86_SMP
242                 if (smp_num_siblings > 1)
243                         printk("WARNING: polling idle and HT enabled, performance may degrade.\n");
244 #endif
245         } else if (!strncmp(str, "halt", 4)) {
246                 printk("using halt in idle threads.\n");
247                 pm_idle = default_idle;
248         }
249
250         boot_option_idle_override = 1;
251         return 1;
252 }
253
254 __setup("idle=", idle_setup);
255
256 void show_regs(struct pt_regs * regs)
257 {
258         unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L;
259
260         printk("\n");
261         printk("Pid: %d, comm: %20s\n", current->pid, current->comm);
262         printk("EIP: %04x:[<%08lx>] CPU: %d\n",0xffff & regs->xcs,regs->eip, smp_processor_id());
263         print_symbol("EIP is at %s\n", regs->eip);
264
265         if (regs->xcs & 3)
266                 printk(" ESP: %04x:%08lx",0xffff & regs->xss,regs->esp);
267         printk(" EFLAGS: %08lx    %s  (%s)\n",
268                regs->eflags, print_tainted(), system_utsname.release);
269         printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
270                 regs->eax,regs->ebx,regs->ecx,regs->edx);
271         printk("ESI: %08lx EDI: %08lx EBP: %08lx",
272                 regs->esi, regs->edi, regs->ebp);
273         printk(" DS: %04x ES: %04x\n",
274                 0xffff & regs->xds,0xffff & regs->xes);
275
276         __asm__("movl %%cr0, %0": "=r" (cr0));
277         __asm__("movl %%cr2, %0": "=r" (cr2));
278         __asm__("movl %%cr3, %0": "=r" (cr3));
279         /* This could fault if %cr4 does not exist */
280         __asm__("1: movl %%cr4, %0              \n"
281                 "2:                             \n"
282                 ".section __ex_table,\"a\"      \n"
283                 ".long 1b,2b                    \n"
284                 ".previous                      \n"
285                 : "=r" (cr4): "0" (0));
286         printk("CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n", cr0, cr2, cr3, cr4);
287         show_trace(NULL, &regs->esp);
288 }
289
290 EXPORT_SYMBOL_GPL(show_regs);
291
292 /*
293  * This gets run with %ebx containing the
294  * function to call, and %edx containing
295  * the "args".
296  */
297 extern void kernel_thread_helper(void);
298 __asm__(".section .text\n"
299         ".align 4\n"
300         "kernel_thread_helper:\n\t"
301         "movl %edx,%eax\n\t"
302         "pushl %edx\n\t"
303         "call *%ebx\n\t"
304         "pushl %eax\n\t"
305         "call do_exit\n"
306         ".previous");
307
308 /*
309  * Create a kernel thread
310  */
311 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
312 {
313         struct pt_regs regs;
314
315         memset(&regs, 0, sizeof(regs));
316
317         regs.ebx = (unsigned long) fn;
318         regs.edx = (unsigned long) arg;
319
320         regs.xds = __USER_DS;
321         regs.xes = __USER_DS;
322         regs.orig_eax = -1;
323         regs.eip = (unsigned long) kernel_thread_helper;
324         regs.xcs = __KERNEL_CS;
325         regs.eflags = X86_EFLAGS_IF | X86_EFLAGS_SF | X86_EFLAGS_PF | 0x2;
326
327         /* Ok, create the new process.. */
328         return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
329 }
330
331 /*
332  * Free current thread data structures etc..
333  */
334 void exit_thread(void)
335 {
336         struct task_struct *tsk = current;
337         struct thread_struct *t = &tsk->thread;
338
339         /* The process may have allocated an io port bitmap... nuke it. */
340         if (unlikely(NULL != t->io_bitmap_ptr)) {
341                 int cpu = get_cpu();
342                 struct tss_struct *tss = &per_cpu(init_tss, cpu);
343
344                 kfree(t->io_bitmap_ptr);
345                 t->io_bitmap_ptr = NULL;
346                 /*
347                  * Careful, clear this in the TSS too:
348                  */
349                 memset(tss->io_bitmap, 0xff, tss->io_bitmap_max);
350                 t->io_bitmap_max = 0;
351                 tss->io_bitmap_owner = NULL;
352                 tss->io_bitmap_max = 0;
353                 tss->io_bitmap_base = INVALID_IO_BITMAP_OFFSET;
354                 put_cpu();
355         }
356 }
357
358 void flush_thread(void)
359 {
360         struct task_struct *tsk = current;
361
362         memset(tsk->thread.debugreg, 0, sizeof(unsigned long)*8);
363         memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));        
364         /*
365          * Forget coprocessor state..
366          */
367         clear_fpu(tsk);
368         clear_used_math();
369 }
370
371 void release_thread(struct task_struct *dead_task)
372 {
373         if (dead_task->mm) {
374                 // temporary debugging check
375                 if (dead_task->mm->context.size) {
376                         printk("WARNING: dead process %8s still has LDT? <%p/%d>\n",
377                                         dead_task->comm,
378                                         dead_task->mm->context.ldt,
379                                         dead_task->mm->context.size);
380                         BUG();
381                 }
382         }
383
384         release_vm86_irqs(dead_task);
385 }
386
387 /*
388  * This gets called before we allocate a new thread and copy
389  * the current task into it.
390  */
391 void prepare_to_copy(struct task_struct *tsk)
392 {
393         unlazy_fpu(tsk);
394 }
395
396 int copy_thread(int nr, unsigned long clone_flags, unsigned long esp,
397         unsigned long unused,
398         struct task_struct * p, struct pt_regs * regs)
399 {
400         struct pt_regs * childregs;
401         struct task_struct *tsk;
402         int err;
403
404         childregs = ((struct pt_regs *) (THREAD_SIZE + (unsigned long) p->thread_info)) - 1;
405         /*
406          * The below -8 is to reserve 8 bytes on top of the ring0 stack.
407          * This is necessary to guarantee that the entire "struct pt_regs"
408          * is accessable even if the CPU haven't stored the SS/ESP registers
409          * on the stack (interrupt gate does not save these registers
410          * when switching to the same priv ring).
411          * Therefore beware: accessing the xss/esp fields of the
412          * "struct pt_regs" is possible, but they may contain the
413          * completely wrong values.
414          */
415         childregs = (struct pt_regs *) ((unsigned long) childregs - 8);
416         *childregs = *regs;
417         childregs->eax = 0;
418         childregs->esp = esp;
419
420         p->thread.esp = (unsigned long) childregs;
421         p->thread.esp0 = (unsigned long) (childregs+1);
422
423         p->thread.eip = (unsigned long) ret_from_fork;
424
425         savesegment(fs,p->thread.fs);
426         savesegment(gs,p->thread.gs);
427
428         tsk = current;
429         if (unlikely(NULL != tsk->thread.io_bitmap_ptr)) {
430                 p->thread.io_bitmap_ptr = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
431                 if (!p->thread.io_bitmap_ptr) {
432                         p->thread.io_bitmap_max = 0;
433                         return -ENOMEM;
434                 }
435                 memcpy(p->thread.io_bitmap_ptr, tsk->thread.io_bitmap_ptr,
436                         IO_BITMAP_BYTES);
437         }
438
439         /*
440          * Set a new TLS for the child thread?
441          */
442         if (clone_flags & CLONE_SETTLS) {
443                 struct desc_struct *desc;
444                 struct user_desc info;
445                 int idx;
446
447                 err = -EFAULT;
448                 if (copy_from_user(&info, (void __user *)childregs->esi, sizeof(info)))
449                         goto out;
450                 err = -EINVAL;
451                 if (LDT_empty(&info))
452                         goto out;
453
454                 idx = info.entry_number;
455                 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
456                         goto out;
457
458                 desc = p->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
459                 desc->a = LDT_entry_a(&info);
460                 desc->b = LDT_entry_b(&info);
461         }
462
463         err = 0;
464  out:
465         if (err && p->thread.io_bitmap_ptr) {
466                 kfree(p->thread.io_bitmap_ptr);
467                 p->thread.io_bitmap_max = 0;
468         }
469         return err;
470 }
471
472 /*
473  * fill in the user structure for a core dump..
474  */
475 void dump_thread(struct pt_regs * regs, struct user * dump)
476 {
477         int i;
478
479 /* changed the size calculations - should hopefully work better. lbt */
480         dump->magic = CMAGIC;
481         dump->start_code = 0;
482         dump->start_stack = regs->esp & ~(PAGE_SIZE - 1);
483         dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
484         dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT;
485         dump->u_dsize -= dump->u_tsize;
486         dump->u_ssize = 0;
487         for (i = 0; i < 8; i++)
488                 dump->u_debugreg[i] = current->thread.debugreg[i];  
489
490         if (dump->start_stack < TASK_SIZE)
491                 dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT;
492
493         dump->regs.ebx = regs->ebx;
494         dump->regs.ecx = regs->ecx;
495         dump->regs.edx = regs->edx;
496         dump->regs.esi = regs->esi;
497         dump->regs.edi = regs->edi;
498         dump->regs.ebp = regs->ebp;
499         dump->regs.eax = regs->eax;
500         dump->regs.ds = regs->xds;
501         dump->regs.es = regs->xes;
502         savesegment(fs,dump->regs.fs);
503         savesegment(gs,dump->regs.gs);
504         dump->regs.orig_eax = regs->orig_eax;
505         dump->regs.eip = regs->eip;
506         dump->regs.cs = regs->xcs;
507         dump->regs.eflags = regs->eflags;
508         dump->regs.esp = regs->esp;
509         dump->regs.ss = regs->xss;
510
511         dump->u_fpvalid = dump_fpu (regs, &dump->i387);
512 }
513
514 /* 
515  * Capture the user space registers if the task is not running (in user space)
516  */
517 int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
518 {
519         struct pt_regs ptregs;
520         
521         ptregs = *(struct pt_regs *)
522                 ((unsigned long)tsk->thread_info+THREAD_SIZE - sizeof(ptregs));
523         ptregs.xcs &= 0xffff;
524         ptregs.xds &= 0xffff;
525         ptregs.xes &= 0xffff;
526         ptregs.xss &= 0xffff;
527
528         elf_core_copy_regs(regs, &ptregs);
529
530         return 1;
531 }
532
533 static inline void
534 handle_io_bitmap(struct thread_struct *next, struct tss_struct *tss)
535 {
536         if (!next->io_bitmap_ptr) {
537                 /*
538                  * Disable the bitmap via an invalid offset. We still cache
539                  * the previous bitmap owner and the IO bitmap contents:
540                  */
541                 tss->io_bitmap_base = INVALID_IO_BITMAP_OFFSET;
542                 return;
543         }
544         if (likely(next == tss->io_bitmap_owner)) {
545                 /*
546                  * Previous owner of the bitmap (hence the bitmap content)
547                  * matches the next task, we dont have to do anything but
548                  * to set a valid offset in the TSS:
549                  */
550                 tss->io_bitmap_base = IO_BITMAP_OFFSET;
551                 return;
552         }
553         /*
554          * Lazy TSS's I/O bitmap copy. We set an invalid offset here
555          * and we let the task to get a GPF in case an I/O instruction
556          * is performed.  The handler of the GPF will verify that the
557          * faulting task has a valid I/O bitmap and, it true, does the
558          * real copy and restart the instruction.  This will save us
559          * redundant copies when the currently switched task does not
560          * perform any I/O during its timeslice.
561          */
562         tss->io_bitmap_base = INVALID_IO_BITMAP_OFFSET_LAZY;
563 }
564
565 /*
566  *      switch_to(x,yn) should switch tasks from x to y.
567  *
568  * We fsave/fwait so that an exception goes off at the right time
569  * (as a call from the fsave or fwait in effect) rather than to
570  * the wrong process. Lazy FP saving no longer makes any sense
571  * with modern CPU's, and this simplifies a lot of things (SMP
572  * and UP become the same).
573  *
574  * NOTE! We used to use the x86 hardware context switching. The
575  * reason for not using it any more becomes apparent when you
576  * try to recover gracefully from saved state that is no longer
577  * valid (stale segment register values in particular). With the
578  * hardware task-switch, there is no way to fix up bad state in
579  * a reasonable manner.
580  *
581  * The fact that Intel documents the hardware task-switching to
582  * be slow is a fairly red herring - this code is not noticeably
583  * faster. However, there _is_ some room for improvement here,
584  * so the performance issues may eventually be a valid point.
585  * More important, however, is the fact that this allows us much
586  * more flexibility.
587  *
588  * The return value (in %eax) will be the "prev" task after
589  * the task-switch, and shows up in ret_from_fork in entry.S,
590  * for example.
591  */
592 struct task_struct fastcall * __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
593 {
594         struct thread_struct *prev = &prev_p->thread,
595                                  *next = &next_p->thread;
596         int cpu = smp_processor_id();
597         struct tss_struct *tss = &per_cpu(init_tss, cpu);
598
599         /* never put a printk in __switch_to... printk() calls wake_up*() indirectly */
600
601         __unlazy_fpu(prev_p);
602         if (next_p->mm)
603                 load_user_cs_desc(cpu, next_p->mm);
604
605         /*
606          * Reload esp0, LDT and the page table pointer:
607          */
608         load_esp0(tss, next);
609
610         /*
611          * Load the per-thread Thread-Local Storage descriptor.
612          */
613         load_TLS(next, cpu);
614
615         /*
616          * Save away %fs and %gs. No need to save %es and %ds, as
617          * those are always kernel segments while inside the kernel.
618          */
619         asm volatile("mov %%fs,%0":"=m" (prev->fs));
620         asm volatile("mov %%gs,%0":"=m" (prev->gs));
621
622         /*
623          * Restore %fs and %gs if needed.
624          */
625         if (unlikely(prev->fs | prev->gs | next->fs | next->gs)) {
626                 loadsegment(fs, next->fs);
627                 loadsegment(gs, next->gs);
628         }
629
630         /*
631          * Now maybe reload the debug registers
632          */
633         if (unlikely(next->debugreg[7])) {
634                 loaddebug(next, 0);
635                 loaddebug(next, 1);
636                 loaddebug(next, 2);
637                 loaddebug(next, 3);
638                 /* no 4 and 5 */
639                 loaddebug(next, 6);
640                 loaddebug(next, 7);
641         }
642
643         if (unlikely(prev->io_bitmap_ptr || next->io_bitmap_ptr))
644                 handle_io_bitmap(next, tss);
645
646         return prev_p;
647 }
648
649 asmlinkage int sys_fork(struct pt_regs regs)
650 {
651         return do_fork(SIGCHLD, regs.esp, &regs, 0, NULL, NULL);
652 }
653
654 asmlinkage int sys_clone(struct pt_regs regs)
655 {
656         unsigned long clone_flags;
657         unsigned long newsp;
658         int __user *parent_tidptr, *child_tidptr;
659
660         clone_flags = regs.ebx;
661         newsp = regs.ecx;
662         parent_tidptr = (int __user *)regs.edx;
663         child_tidptr = (int __user *)regs.edi;
664         if (!newsp)
665                 newsp = regs.esp;
666         return do_fork(clone_flags, newsp, &regs, 0, parent_tidptr, child_tidptr);
667 }
668
669 /*
670  * This is trivial, and on the face of it looks like it
671  * could equally well be done in user mode.
672  *
673  * Not so, for quite unobvious reasons - register pressure.
674  * In user mode vfork() cannot have a stack frame, and if
675  * done by calling the "clone()" system call directly, you
676  * do not have enough call-clobbered registers to hold all
677  * the information you need.
678  */
679 asmlinkage int sys_vfork(struct pt_regs regs)
680 {
681         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.esp, &regs, 0, NULL, NULL);
682 }
683
684 /*
685  * sys_execve() executes a new program.
686  */
687 asmlinkage int sys_execve(struct pt_regs regs)
688 {
689         int error;
690         char * filename;
691
692         filename = getname((char __user *) regs.ebx);
693         error = PTR_ERR(filename);
694         if (IS_ERR(filename))
695                 goto out;
696         error = do_execve(filename,
697                         (char __user * __user *) regs.ecx,
698                         (char __user * __user *) regs.edx,
699                         &regs);
700         if (error == 0) {
701                 task_lock(current);
702                 current->ptrace &= ~PT_DTRACE;
703                 task_unlock(current);
704                 /* Make sure we don't return using sysenter.. */
705                 set_thread_flag(TIF_IRET);
706         }
707         putname(filename);
708 out:
709         return error;
710 }
711
712 #define top_esp                (THREAD_SIZE - sizeof(unsigned long))
713 #define top_ebp                (THREAD_SIZE - 2*sizeof(unsigned long))
714
715 unsigned long get_wchan(struct task_struct *p)
716 {
717         unsigned long ebp, esp, eip;
718         unsigned long stack_page;
719         int count = 0;
720         if (!p || p == current || p->state == TASK_RUNNING)
721                 return 0;
722         stack_page = (unsigned long)p->thread_info;
723         esp = p->thread.esp;
724         if (!stack_page || esp < stack_page || esp > top_esp+stack_page)
725                 return 0;
726         /* include/asm-i386/system.h:switch_to() pushes ebp last. */
727         ebp = *(unsigned long *) esp;
728         do {
729                 if (ebp < stack_page || ebp > top_ebp+stack_page)
730                         return 0;
731                 eip = *(unsigned long *) (ebp+4);
732                 if (!in_sched_functions(eip))
733                         return eip;
734                 ebp = *(unsigned long *) ebp;
735         } while (count++ < 16);
736         return 0;
737 }
738
739 /*
740  * sys_alloc_thread_area: get a yet unused TLS descriptor index.
741  */
742 static int get_free_idx(void)
743 {
744         struct thread_struct *t = &current->thread;
745         int idx;
746
747         for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
748                 if (desc_empty(t->tls_array + idx))
749                         return idx + GDT_ENTRY_TLS_MIN;
750         return -ESRCH;
751 }
752
753 /*
754  * Set a given TLS descriptor:
755  */
756 asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
757 {
758         struct thread_struct *t = &current->thread;
759         struct user_desc info;
760         struct desc_struct *desc;
761         int cpu, idx;
762
763         if (copy_from_user(&info, u_info, sizeof(info)))
764                 return -EFAULT;
765         idx = info.entry_number;
766
767         /*
768          * index -1 means the kernel should try to find and
769          * allocate an empty descriptor:
770          */
771         if (idx == -1) {
772                 idx = get_free_idx();
773                 if (idx < 0)
774                         return idx;
775                 if (put_user(idx, &u_info->entry_number))
776                         return -EFAULT;
777         }
778
779         if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
780                 return -EINVAL;
781
782         desc = t->tls_array + idx - GDT_ENTRY_TLS_MIN;
783
784         /*
785          * We must not get preempted while modifying the TLS.
786          */
787         cpu = get_cpu();
788
789         if (LDT_empty(&info)) {
790                 desc->a = 0;
791                 desc->b = 0;
792         } else {
793                 desc->a = LDT_entry_a(&info);
794                 desc->b = LDT_entry_b(&info);
795         }
796         load_TLS(t, cpu);
797
798         put_cpu();
799
800         return 0;
801 }
802
803 /*
804  * Get the current Thread-Local Storage area:
805  */
806
807 #define GET_BASE(desc) ( \
808         (((desc)->a >> 16) & 0x0000ffff) | \
809         (((desc)->b << 16) & 0x00ff0000) | \
810         ( (desc)->b        & 0xff000000)   )
811
812 #define GET_LIMIT(desc) ( \
813         ((desc)->a & 0x0ffff) | \
814          ((desc)->b & 0xf0000) )
815         
816 #define GET_32BIT(desc)         (((desc)->b >> 22) & 1)
817 #define GET_CONTENTS(desc)      (((desc)->b >> 10) & 3)
818 #define GET_WRITABLE(desc)      (((desc)->b >>  9) & 1)
819 #define GET_LIMIT_PAGES(desc)   (((desc)->b >> 23) & 1)
820 #define GET_PRESENT(desc)       (((desc)->b >> 15) & 1)
821 #define GET_USEABLE(desc)       (((desc)->b >> 20) & 1)
822
823 asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
824 {
825         struct user_desc info;
826         struct desc_struct *desc;
827         int idx;
828
829         if (get_user(idx, &u_info->entry_number))
830                 return -EFAULT;
831         if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
832                 return -EINVAL;
833
834         desc = current->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
835
836         info.entry_number = idx;
837         info.base_addr = GET_BASE(desc);
838         info.limit = GET_LIMIT(desc);
839         info.seg_32bit = GET_32BIT(desc);
840         info.contents = GET_CONTENTS(desc);
841         info.read_exec_only = !GET_WRITABLE(desc);
842         info.limit_in_pages = GET_LIMIT_PAGES(desc);
843         info.seg_not_present = !GET_PRESENT(desc);
844         info.useable = GET_USEABLE(desc);
845
846         if (copy_to_user(u_info, &info, sizeof(info)))
847                 return -EFAULT;
848         return 0;
849 }
850
851 unsigned long arch_align_stack(unsigned long sp)
852 {
853         if (randomize_va_space)
854                 sp -= get_random_int() % 8192;
855         return sp & ~0xf;
856 }
857
858 void arch_add_exec_range(struct mm_struct *mm, unsigned long limit)
859 {
860         if (limit > mm->context.exec_limit) {
861                 mm->context.exec_limit = limit;
862                 set_user_cs(&mm->context.user_cs, limit);
863                 if (mm == current->mm) {
864                         preempt_disable();
865                         load_user_cs_desc(smp_processor_id(), mm);
866                         preempt_enable();
867                 }
868         }
869 }
870
871 void arch_remove_exec_range(struct mm_struct *mm, unsigned long old_end)
872 {
873         struct vm_area_struct *vma;
874         unsigned long limit = PAGE_SIZE;
875
876         if (old_end == mm->context.exec_limit) {
877                 for (vma = mm->mmap; vma; vma = vma->vm_next)
878                         if ((vma->vm_flags & VM_EXEC) && (vma->vm_end > limit))
879                                 limit = vma->vm_end;
880
881                 mm->context.exec_limit = limit;
882                 set_user_cs(&mm->context.user_cs, limit);
883                 if (mm == current->mm) {
884                         preempt_disable();
885                         load_user_cs_desc(smp_processor_id(), mm);
886                         preempt_enable();
887                 }
888         }
889 }
890
891 void arch_flush_exec_range(struct mm_struct *mm)
892 {
893         mm->context.exec_limit = 0;
894         set_user_cs(&mm->context.user_cs, 0);
895 }
896
897 /*
898  * Generate random brk address between 128MB and 196MB. (if the layout
899  * allows it.)
900  */
901 void randomize_brk(unsigned long old_brk)
902 {
903         unsigned long new_brk, range_start, range_end;
904
905         range_start = 0x08000000;
906         if (current->mm->brk >= range_start)
907                 range_start = current->mm->brk;
908         range_end = range_start + 0x02000000;
909         new_brk = randomize_range(range_start, range_end, 0);
910         if (new_brk)
911                 current->mm->brk = new_brk;
912 }
913