Merge branch 'linus' into x86/pebs
[pandora-kernel.git] / arch / x86 / kernel / process_32.c
1 /*
2  *  Copyright (C) 1995  Linus Torvalds
3  *
4  *  Pentium III FXSR, SSE support
5  *      Gareth Hughes <gareth@valinux.com>, May 2000
6  */
7
8 /*
9  * This file handles the architecture-dependent parts of process handling..
10  */
11
12 #include <stdarg.h>
13
14 #include <linux/cpu.h>
15 #include <linux/errno.h>
16 #include <linux/sched.h>
17 #include <linux/fs.h>
18 #include <linux/kernel.h>
19 #include <linux/mm.h>
20 #include <linux/elfcore.h>
21 #include <linux/smp.h>
22 #include <linux/stddef.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/user.h>
26 #include <linux/interrupt.h>
27 #include <linux/utsname.h>
28 #include <linux/delay.h>
29 #include <linux/reboot.h>
30 #include <linux/init.h>
31 #include <linux/mc146818rtc.h>
32 #include <linux/module.h>
33 #include <linux/kallsyms.h>
34 #include <linux/ptrace.h>
35 #include <linux/random.h>
36 #include <linux/personality.h>
37 #include <linux/tick.h>
38 #include <linux/percpu.h>
39 #include <linux/prctl.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/desc.h>
49 #ifdef CONFIG_MATH_EMULATION
50 #include <asm/math_emu.h>
51 #endif
52
53 #include <linux/err.h>
54
55 #include <asm/tlbflush.h>
56 #include <asm/cpu.h>
57 #include <asm/kdebug.h>
58
59 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
60
61 DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
62 EXPORT_PER_CPU_SYMBOL(current_task);
63
64 DEFINE_PER_CPU(int, cpu_number);
65 EXPORT_PER_CPU_SYMBOL(cpu_number);
66
67 /*
68  * Return saved PC of a blocked thread.
69  */
70 unsigned long thread_saved_pc(struct task_struct *tsk)
71 {
72         return ((unsigned long *)tsk->thread.sp)[3];
73 }
74
75 #ifdef CONFIG_HOTPLUG_CPU
76 #include <asm/nmi.h>
77
78 static void cpu_exit_clear(void)
79 {
80         int cpu = raw_smp_processor_id();
81
82         idle_task_exit();
83
84         cpu_uninit();
85         irq_ctx_exit(cpu);
86
87         cpu_clear(cpu, cpu_callout_map);
88         cpu_clear(cpu, cpu_callin_map);
89
90         numa_remove_cpu(cpu);
91 }
92
93 /* We don't actually take CPU down, just spin without interrupts. */
94 static inline void play_dead(void)
95 {
96         /* This must be done before dead CPU ack */
97         cpu_exit_clear();
98         wbinvd();
99         mb();
100         /* Ack it */
101         __get_cpu_var(cpu_state) = CPU_DEAD;
102
103         /*
104          * With physical CPU hotplug, we should halt the cpu
105          */
106         local_irq_disable();
107         while (1)
108                 halt();
109 }
110 #else
111 static inline void play_dead(void)
112 {
113         BUG();
114 }
115 #endif /* CONFIG_HOTPLUG_CPU */
116
117 /*
118  * The idle thread. There's no useful work to be
119  * done, so just try to conserve power and have a
120  * low exit latency (ie sit in a loop waiting for
121  * somebody to say that they'd like to reschedule)
122  */
123 void cpu_idle(void)
124 {
125         int cpu = smp_processor_id();
126
127         current_thread_info()->status |= TS_POLLING;
128
129         /* endless idle loop with no priority at all */
130         while (1) {
131                 tick_nohz_stop_sched_tick(1);
132                 while (!need_resched()) {
133
134                         check_pgt_cache();
135                         rmb();
136
137                         if (rcu_pending(cpu))
138                                 rcu_check_callbacks(cpu, 0);
139
140                         if (cpu_is_offline(cpu))
141                                 play_dead();
142
143                         local_irq_disable();
144                         __get_cpu_var(irq_stat).idle_timestamp = jiffies;
145                         /* Don't trace irqs off for idle */
146                         stop_critical_timings();
147                         pm_idle();
148                         start_critical_timings();
149                 }
150                 tick_nohz_restart_sched_tick();
151                 preempt_enable_no_resched();
152                 schedule();
153                 preempt_disable();
154         }
155 }
156
157 void __show_registers(struct pt_regs *regs, int all)
158 {
159         unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L;
160         unsigned long d0, d1, d2, d3, d6, d7;
161         unsigned long sp;
162         unsigned short ss, gs;
163
164         if (user_mode_vm(regs)) {
165                 sp = regs->sp;
166                 ss = regs->ss & 0xffff;
167                 savesegment(gs, gs);
168         } else {
169                 sp = (unsigned long) (&regs->sp);
170                 savesegment(ss, ss);
171                 savesegment(gs, gs);
172         }
173
174         printk("\n");
175         printk("Pid: %d, comm: %s %s (%s %.*s)\n",
176                         task_pid_nr(current), current->comm,
177                         print_tainted(), init_utsname()->release,
178                         (int)strcspn(init_utsname()->version, " "),
179                         init_utsname()->version);
180
181         printk("EIP: %04x:[<%08lx>] EFLAGS: %08lx CPU: %d\n",
182                         (u16)regs->cs, regs->ip, regs->flags,
183                         smp_processor_id());
184         print_symbol("EIP is at %s\n", regs->ip);
185
186         printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
187                 regs->ax, regs->bx, regs->cx, regs->dx);
188         printk("ESI: %08lx EDI: %08lx EBP: %08lx ESP: %08lx\n",
189                 regs->si, regs->di, regs->bp, sp);
190         printk(" DS: %04x ES: %04x FS: %04x GS: %04x SS: %04x\n",
191                (u16)regs->ds, (u16)regs->es, (u16)regs->fs, gs, ss);
192
193         if (!all)
194                 return;
195
196         cr0 = read_cr0();
197         cr2 = read_cr2();
198         cr3 = read_cr3();
199         cr4 = read_cr4_safe();
200         printk("CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n",
201                         cr0, cr2, cr3, cr4);
202
203         get_debugreg(d0, 0);
204         get_debugreg(d1, 1);
205         get_debugreg(d2, 2);
206         get_debugreg(d3, 3);
207         printk("DR0: %08lx DR1: %08lx DR2: %08lx DR3: %08lx\n",
208                         d0, d1, d2, d3);
209
210         get_debugreg(d6, 6);
211         get_debugreg(d7, 7);
212         printk("DR6: %08lx DR7: %08lx\n",
213                         d6, d7);
214 }
215
216 void show_regs(struct pt_regs *regs)
217 {
218         __show_registers(regs, 1);
219         show_trace(NULL, regs, &regs->sp, regs->bp);
220 }
221
222 /*
223  * This gets run with %bx containing the
224  * function to call, and %dx containing
225  * the "args".
226  */
227 extern void kernel_thread_helper(void);
228
229 /*
230  * Create a kernel thread
231  */
232 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
233 {
234         struct pt_regs regs;
235
236         memset(&regs, 0, sizeof(regs));
237
238         regs.bx = (unsigned long) fn;
239         regs.dx = (unsigned long) arg;
240
241         regs.ds = __USER_DS;
242         regs.es = __USER_DS;
243         regs.fs = __KERNEL_PERCPU;
244         regs.orig_ax = -1;
245         regs.ip = (unsigned long) kernel_thread_helper;
246         regs.cs = __KERNEL_CS | get_kernel_rpl();
247         regs.flags = X86_EFLAGS_IF | X86_EFLAGS_SF | X86_EFLAGS_PF | 0x2;
248
249         /* Ok, create the new process.. */
250         return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
251 }
252 EXPORT_SYMBOL(kernel_thread);
253
254 /*
255  * Free current thread data structures etc..
256  */
257 void exit_thread(void)
258 {
259         /* The process may have allocated an io port bitmap... nuke it. */
260         if (unlikely(test_thread_flag(TIF_IO_BITMAP))) {
261                 struct task_struct *tsk = current;
262                 struct thread_struct *t = &tsk->thread;
263                 int cpu = get_cpu();
264                 struct tss_struct *tss = &per_cpu(init_tss, cpu);
265
266                 kfree(t->io_bitmap_ptr);
267                 t->io_bitmap_ptr = NULL;
268                 clear_thread_flag(TIF_IO_BITMAP);
269                 /*
270                  * Careful, clear this in the TSS too:
271                  */
272                 memset(tss->io_bitmap, 0xff, tss->io_bitmap_max);
273                 t->io_bitmap_max = 0;
274                 tss->io_bitmap_owner = NULL;
275                 tss->io_bitmap_max = 0;
276                 tss->x86_tss.io_bitmap_base = INVALID_IO_BITMAP_OFFSET;
277                 put_cpu();
278         }
279 #ifdef CONFIG_X86_DS
280         /* Free any DS contexts that have not been properly released. */
281         if (unlikely(current->thread.ds_ctx)) {
282                 /* we clear debugctl to make sure DS is not used. */
283                 update_debugctlmsr(0);
284                 ds_free(current->thread.ds_ctx);
285         }
286 #endif /* CONFIG_X86_DS */
287 }
288
289 void flush_thread(void)
290 {
291         struct task_struct *tsk = current;
292
293         tsk->thread.debugreg0 = 0;
294         tsk->thread.debugreg1 = 0;
295         tsk->thread.debugreg2 = 0;
296         tsk->thread.debugreg3 = 0;
297         tsk->thread.debugreg6 = 0;
298         tsk->thread.debugreg7 = 0;
299         memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));        
300         clear_tsk_thread_flag(tsk, TIF_DEBUG);
301         /*
302          * Forget coprocessor state..
303          */
304         tsk->fpu_counter = 0;
305         clear_fpu(tsk);
306         clear_used_math();
307 }
308
309 void release_thread(struct task_struct *dead_task)
310 {
311         BUG_ON(dead_task->mm);
312         release_vm86_irqs(dead_task);
313 }
314
315 /*
316  * This gets called before we allocate a new thread and copy
317  * the current task into it.
318  */
319 void prepare_to_copy(struct task_struct *tsk)
320 {
321         unlazy_fpu(tsk);
322 }
323
324 int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
325         unsigned long unused,
326         struct task_struct * p, struct pt_regs * regs)
327 {
328         struct pt_regs * childregs;
329         struct task_struct *tsk;
330         int err;
331
332         childregs = task_pt_regs(p);
333         *childregs = *regs;
334         childregs->ax = 0;
335         childregs->sp = sp;
336
337         p->thread.sp = (unsigned long) childregs;
338         p->thread.sp0 = (unsigned long) (childregs+1);
339
340         p->thread.ip = (unsigned long) ret_from_fork;
341
342         savesegment(gs, p->thread.gs);
343
344         tsk = current;
345         if (unlikely(test_tsk_thread_flag(tsk, TIF_IO_BITMAP))) {
346                 p->thread.io_bitmap_ptr = kmemdup(tsk->thread.io_bitmap_ptr,
347                                                 IO_BITMAP_BYTES, GFP_KERNEL);
348                 if (!p->thread.io_bitmap_ptr) {
349                         p->thread.io_bitmap_max = 0;
350                         return -ENOMEM;
351                 }
352                 set_tsk_thread_flag(p, TIF_IO_BITMAP);
353         }
354
355         err = 0;
356
357         /*
358          * Set a new TLS for the child thread?
359          */
360         if (clone_flags & CLONE_SETTLS)
361                 err = do_set_thread_area(p, -1,
362                         (struct user_desc __user *)childregs->si, 0);
363
364         if (err && p->thread.io_bitmap_ptr) {
365                 kfree(p->thread.io_bitmap_ptr);
366                 p->thread.io_bitmap_max = 0;
367         }
368         return err;
369 }
370
371 void
372 start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
373 {
374         __asm__("movl %0, %%gs" :: "r"(0));
375         regs->fs                = 0;
376         set_fs(USER_DS);
377         regs->ds                = __USER_DS;
378         regs->es                = __USER_DS;
379         regs->ss                = __USER_DS;
380         regs->cs                = __USER_CS;
381         regs->ip                = new_ip;
382         regs->sp                = new_sp;
383         /*
384          * Free the old FP and other extended state
385          */
386         free_thread_xstate(current);
387 }
388 EXPORT_SYMBOL_GPL(start_thread);
389
390 static void hard_disable_TSC(void)
391 {
392         write_cr4(read_cr4() | X86_CR4_TSD);
393 }
394
395 void disable_TSC(void)
396 {
397         preempt_disable();
398         if (!test_and_set_thread_flag(TIF_NOTSC))
399                 /*
400                  * Must flip the CPU state synchronously with
401                  * TIF_NOTSC in the current running context.
402                  */
403                 hard_disable_TSC();
404         preempt_enable();
405 }
406
407 static void hard_enable_TSC(void)
408 {
409         write_cr4(read_cr4() & ~X86_CR4_TSD);
410 }
411
412 static void enable_TSC(void)
413 {
414         preempt_disable();
415         if (test_and_clear_thread_flag(TIF_NOTSC))
416                 /*
417                  * Must flip the CPU state synchronously with
418                  * TIF_NOTSC in the current running context.
419                  */
420                 hard_enable_TSC();
421         preempt_enable();
422 }
423
424 int get_tsc_mode(unsigned long adr)
425 {
426         unsigned int val;
427
428         if (test_thread_flag(TIF_NOTSC))
429                 val = PR_TSC_SIGSEGV;
430         else
431                 val = PR_TSC_ENABLE;
432
433         return put_user(val, (unsigned int __user *)adr);
434 }
435
436 int set_tsc_mode(unsigned int val)
437 {
438         if (val == PR_TSC_SIGSEGV)
439                 disable_TSC();
440         else if (val == PR_TSC_ENABLE)
441                 enable_TSC();
442         else
443                 return -EINVAL;
444
445         return 0;
446 }
447
448 #ifdef CONFIG_X86_DS
449 static int update_debugctl(struct thread_struct *prev,
450                         struct thread_struct *next, unsigned long debugctl)
451 {
452         unsigned long ds_prev = 0;
453         unsigned long ds_next = 0;
454
455         if (prev->ds_ctx)
456                 ds_prev = (unsigned long)prev->ds_ctx->ds;
457         if (next->ds_ctx)
458                 ds_next = (unsigned long)next->ds_ctx->ds;
459
460         if (ds_next != ds_prev) {
461                 /* we clear debugctl to make sure DS
462                  * is not in use when we change it */
463                 debugctl = 0;
464                 update_debugctlmsr(0);
465                 wrmsr(MSR_IA32_DS_AREA, ds_next, 0);
466         }
467         return debugctl;
468 }
469 #else
470 static int update_debugctl(struct thread_struct *prev,
471                         struct thread_struct *next, unsigned long debugctl)
472 {
473         return debugctl;
474 }
475 #endif /* CONFIG_X86_DS */
476
477 static noinline void
478 __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
479                  struct tss_struct *tss)
480 {
481         struct thread_struct *prev, *next;
482         unsigned long debugctl;
483
484         prev = &prev_p->thread;
485         next = &next_p->thread;
486
487         debugctl = update_debugctl(prev, next, prev->debugctlmsr);
488
489         if (next->debugctlmsr != debugctl)
490                 update_debugctlmsr(next->debugctlmsr);
491
492         if (test_tsk_thread_flag(next_p, TIF_DEBUG)) {
493                 set_debugreg(next->debugreg0, 0);
494                 set_debugreg(next->debugreg1, 1);
495                 set_debugreg(next->debugreg2, 2);
496                 set_debugreg(next->debugreg3, 3);
497                 /* no 4 and 5 */
498                 set_debugreg(next->debugreg6, 6);
499                 set_debugreg(next->debugreg7, 7);
500         }
501
502         if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
503             test_tsk_thread_flag(next_p, TIF_NOTSC)) {
504                 /* prev and next are different */
505                 if (test_tsk_thread_flag(next_p, TIF_NOTSC))
506                         hard_disable_TSC();
507                 else
508                         hard_enable_TSC();
509         }
510
511 #ifdef CONFIG_X86_PTRACE_BTS
512         if (test_tsk_thread_flag(prev_p, TIF_BTS_TRACE_TS))
513                 ptrace_bts_take_timestamp(prev_p, BTS_TASK_DEPARTS);
514
515         if (test_tsk_thread_flag(next_p, TIF_BTS_TRACE_TS))
516                 ptrace_bts_take_timestamp(next_p, BTS_TASK_ARRIVES);
517 #endif /* CONFIG_X86_PTRACE_BTS */
518
519
520         if (!test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
521                 /*
522                  * Disable the bitmap via an invalid offset. We still cache
523                  * the previous bitmap owner and the IO bitmap contents:
524                  */
525                 tss->x86_tss.io_bitmap_base = INVALID_IO_BITMAP_OFFSET;
526                 return;
527         }
528
529         if (likely(next == tss->io_bitmap_owner)) {
530                 /*
531                  * Previous owner of the bitmap (hence the bitmap content)
532                  * matches the next task, we dont have to do anything but
533                  * to set a valid offset in the TSS:
534                  */
535                 tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
536                 return;
537         }
538         /*
539          * Lazy TSS's I/O bitmap copy. We set an invalid offset here
540          * and we let the task to get a GPF in case an I/O instruction
541          * is performed.  The handler of the GPF will verify that the
542          * faulting task has a valid I/O bitmap and, it true, does the
543          * real copy and restart the instruction.  This will save us
544          * redundant copies when the currently switched task does not
545          * perform any I/O during its timeslice.
546          */
547         tss->x86_tss.io_bitmap_base = INVALID_IO_BITMAP_OFFSET_LAZY;
548 }
549
550 /*
551  *      switch_to(x,yn) should switch tasks from x to y.
552  *
553  * We fsave/fwait so that an exception goes off at the right time
554  * (as a call from the fsave or fwait in effect) rather than to
555  * the wrong process. Lazy FP saving no longer makes any sense
556  * with modern CPU's, and this simplifies a lot of things (SMP
557  * and UP become the same).
558  *
559  * NOTE! We used to use the x86 hardware context switching. The
560  * reason for not using it any more becomes apparent when you
561  * try to recover gracefully from saved state that is no longer
562  * valid (stale segment register values in particular). With the
563  * hardware task-switch, there is no way to fix up bad state in
564  * a reasonable manner.
565  *
566  * The fact that Intel documents the hardware task-switching to
567  * be slow is a fairly red herring - this code is not noticeably
568  * faster. However, there _is_ some room for improvement here,
569  * so the performance issues may eventually be a valid point.
570  * More important, however, is the fact that this allows us much
571  * more flexibility.
572  *
573  * The return value (in %ax) will be the "prev" task after
574  * the task-switch, and shows up in ret_from_fork in entry.S,
575  * for example.
576  */
577 struct task_struct * __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
578 {
579         struct thread_struct *prev = &prev_p->thread,
580                                  *next = &next_p->thread;
581         int cpu = smp_processor_id();
582         struct tss_struct *tss = &per_cpu(init_tss, cpu);
583
584         /* never put a printk in __switch_to... printk() calls wake_up*() indirectly */
585
586         __unlazy_fpu(prev_p);
587
588
589         /* we're going to use this soon, after a few expensive things */
590         if (next_p->fpu_counter > 5)
591                 prefetch(next->xstate);
592
593         /*
594          * Reload esp0.
595          */
596         load_sp0(tss, next);
597
598         /*
599          * Save away %gs. No need to save %fs, as it was saved on the
600          * stack on entry.  No need to save %es and %ds, as those are
601          * always kernel segments while inside the kernel.  Doing this
602          * before setting the new TLS descriptors avoids the situation
603          * where we temporarily have non-reloadable segments in %fs
604          * and %gs.  This could be an issue if the NMI handler ever
605          * used %fs or %gs (it does not today), or if the kernel is
606          * running inside of a hypervisor layer.
607          */
608         savesegment(gs, prev->gs);
609
610         /*
611          * Load the per-thread Thread-Local Storage descriptor.
612          */
613         load_TLS(next, cpu);
614
615         /*
616          * Restore IOPL if needed.  In normal use, the flags restore
617          * in the switch assembly will handle this.  But if the kernel
618          * is running virtualized at a non-zero CPL, the popf will
619          * not restore flags, so it must be done in a separate step.
620          */
621         if (get_kernel_rpl() && unlikely(prev->iopl != next->iopl))
622                 set_iopl_mask(next->iopl);
623
624         /*
625          * Now maybe handle debug registers and/or IO bitmaps
626          */
627         if (unlikely(task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV ||
628                      task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT))
629                 __switch_to_xtra(prev_p, next_p, tss);
630
631         /*
632          * Leave lazy mode, flushing any hypercalls made here.
633          * This must be done before restoring TLS segments so
634          * the GDT and LDT are properly updated, and must be
635          * done before math_state_restore, so the TS bit is up
636          * to date.
637          */
638         arch_leave_lazy_cpu_mode();
639
640         /* If the task has used fpu the last 5 timeslices, just do a full
641          * restore of the math state immediately to avoid the trap; the
642          * chances of needing FPU soon are obviously high now
643          *
644          * tsk_used_math() checks prevent calling math_state_restore(),
645          * which can sleep in the case of !tsk_used_math()
646          */
647         if (tsk_used_math(next_p) && next_p->fpu_counter > 5)
648                 math_state_restore();
649
650         /*
651          * Restore %gs if needed (which is common)
652          */
653         if (prev->gs | next->gs)
654                 loadsegment(gs, next->gs);
655
656         x86_write_percpu(current_task, next_p);
657
658         return prev_p;
659 }
660
661 asmlinkage int sys_fork(struct pt_regs regs)
662 {
663         return do_fork(SIGCHLD, regs.sp, &regs, 0, NULL, NULL);
664 }
665
666 asmlinkage int sys_clone(struct pt_regs regs)
667 {
668         unsigned long clone_flags;
669         unsigned long newsp;
670         int __user *parent_tidptr, *child_tidptr;
671
672         clone_flags = regs.bx;
673         newsp = regs.cx;
674         parent_tidptr = (int __user *)regs.dx;
675         child_tidptr = (int __user *)regs.di;
676         if (!newsp)
677                 newsp = regs.sp;
678         return do_fork(clone_flags, newsp, &regs, 0, parent_tidptr, child_tidptr);
679 }
680
681 /*
682  * This is trivial, and on the face of it looks like it
683  * could equally well be done in user mode.
684  *
685  * Not so, for quite unobvious reasons - register pressure.
686  * In user mode vfork() cannot have a stack frame, and if
687  * done by calling the "clone()" system call directly, you
688  * do not have enough call-clobbered registers to hold all
689  * the information you need.
690  */
691 asmlinkage int sys_vfork(struct pt_regs regs)
692 {
693         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.sp, &regs, 0, NULL, NULL);
694 }
695
696 /*
697  * sys_execve() executes a new program.
698  */
699 asmlinkage int sys_execve(struct pt_regs regs)
700 {
701         int error;
702         char * filename;
703
704         filename = getname((char __user *) regs.bx);
705         error = PTR_ERR(filename);
706         if (IS_ERR(filename))
707                 goto out;
708         error = do_execve(filename,
709                         (char __user * __user *) regs.cx,
710                         (char __user * __user *) regs.dx,
711                         &regs);
712         if (error == 0) {
713                 /* Make sure we don't return using sysenter.. */
714                 set_thread_flag(TIF_IRET);
715         }
716         putname(filename);
717 out:
718         return error;
719 }
720
721 #define top_esp                (THREAD_SIZE - sizeof(unsigned long))
722 #define top_ebp                (THREAD_SIZE - 2*sizeof(unsigned long))
723
724 unsigned long get_wchan(struct task_struct *p)
725 {
726         unsigned long bp, sp, ip;
727         unsigned long stack_page;
728         int count = 0;
729         if (!p || p == current || p->state == TASK_RUNNING)
730                 return 0;
731         stack_page = (unsigned long)task_stack_page(p);
732         sp = p->thread.sp;
733         if (!stack_page || sp < stack_page || sp > top_esp+stack_page)
734                 return 0;
735         /* include/asm-i386/system.h:switch_to() pushes bp last. */
736         bp = *(unsigned long *) sp;
737         do {
738                 if (bp < stack_page || bp > top_ebp+stack_page)
739                         return 0;
740                 ip = *(unsigned long *) (bp+4);
741                 if (!in_sched_functions(ip))
742                         return ip;
743                 bp = *(unsigned long *) bp;
744         } while (count++ < 16);
745         return 0;
746 }
747
748 unsigned long arch_align_stack(unsigned long sp)
749 {
750         if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
751                 sp -= get_random_int() % 8192;
752         return sp & ~0xf;
753 }
754
755 unsigned long arch_randomize_brk(struct mm_struct *mm)
756 {
757         unsigned long range_end = mm->brk + 0x02000000;
758         return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
759 }