process_32.c 22 KB

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