process_32.c 17 KB

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