process_32.c 18 KB

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