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