process_32.c 19 KB

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