process_32.c 19 KB

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