process_32.c 17 KB

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