process_32.c 19 KB

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