process_32.c 18 KB

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