process_32.c 17 KB

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