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