process_64.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. /*
  2. * Copyright (C) 1995 Linus Torvalds
  3. *
  4. * Pentium III FXSR, SSE support
  5. * Gareth Hughes <gareth@valinux.com>, May 2000
  6. *
  7. * X86-64 port
  8. * Andi Kleen.
  9. *
  10. * CPU hotplug support - ashok.raj@intel.com
  11. */
  12. /*
  13. * This file handles the architecture-dependent parts of process handling..
  14. */
  15. #include <stdarg.h>
  16. #include <linux/cpu.h>
  17. #include <linux/errno.h>
  18. #include <linux/sched.h>
  19. #include <linux/fs.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mm.h>
  22. #include <linux/elfcore.h>
  23. #include <linux/smp.h>
  24. #include <linux/slab.h>
  25. #include <linux/user.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/utsname.h>
  28. #include <linux/delay.h>
  29. #include <linux/module.h>
  30. #include <linux/ptrace.h>
  31. #include <linux/random.h>
  32. #include <linux/notifier.h>
  33. #include <linux/kprobes.h>
  34. #include <linux/kdebug.h>
  35. #include <linux/tick.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/processor.h>
  42. #include <asm/i387.h>
  43. #include <asm/mmu_context.h>
  44. #include <asm/pda.h>
  45. #include <asm/prctl.h>
  46. #include <asm/desc.h>
  47. #include <asm/proto.h>
  48. #include <asm/ia32.h>
  49. #include <asm/idle.h>
  50. asmlinkage extern void ret_from_fork(void);
  51. unsigned long kernel_thread_flags = CLONE_VM | CLONE_UNTRACED;
  52. unsigned long boot_option_idle_override = 0;
  53. EXPORT_SYMBOL(boot_option_idle_override);
  54. /*
  55. * Powermanagement idle function, if any..
  56. */
  57. void (*pm_idle)(void);
  58. EXPORT_SYMBOL(pm_idle);
  59. static ATOMIC_NOTIFIER_HEAD(idle_notifier);
  60. void idle_notifier_register(struct notifier_block *n)
  61. {
  62. atomic_notifier_chain_register(&idle_notifier, n);
  63. }
  64. void enter_idle(void)
  65. {
  66. write_pda(isidle, 1);
  67. atomic_notifier_call_chain(&idle_notifier, IDLE_START, NULL);
  68. }
  69. static void __exit_idle(void)
  70. {
  71. if (test_and_clear_bit_pda(0, isidle) == 0)
  72. return;
  73. atomic_notifier_call_chain(&idle_notifier, IDLE_END, NULL);
  74. }
  75. /* Called from interrupts to signify idle end */
  76. void exit_idle(void)
  77. {
  78. /* idle loop has pid 0 */
  79. if (current->pid)
  80. return;
  81. __exit_idle();
  82. }
  83. /*
  84. * We use this if we don't have any better
  85. * idle routine..
  86. */
  87. void default_idle(void)
  88. {
  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. local_irq_disable();
  96. if (!need_resched()) {
  97. safe_halt(); /* enables interrupts racelessly */
  98. local_irq_disable();
  99. }
  100. local_irq_enable();
  101. current_thread_info()->status |= TS_POLLING;
  102. }
  103. /*
  104. * On SMP it's slightly faster (but much more power-consuming!)
  105. * to poll the ->need_resched flag instead of waiting for the
  106. * cross-CPU IPI to arrive. Use this option with caution.
  107. */
  108. static void poll_idle(void)
  109. {
  110. local_irq_enable();
  111. cpu_relax();
  112. }
  113. #ifdef CONFIG_HOTPLUG_CPU
  114. DECLARE_PER_CPU(int, cpu_state);
  115. #include <asm/nmi.h>
  116. /* We halt the CPU with physical CPU hotplug */
  117. static inline void play_dead(void)
  118. {
  119. idle_task_exit();
  120. wbinvd();
  121. mb();
  122. /* Ack it */
  123. __get_cpu_var(cpu_state) = CPU_DEAD;
  124. local_irq_disable();
  125. while (1)
  126. halt();
  127. }
  128. #else
  129. static inline void play_dead(void)
  130. {
  131. BUG();
  132. }
  133. #endif /* CONFIG_HOTPLUG_CPU */
  134. /*
  135. * The idle thread. There's no useful work to be
  136. * done, so just try to conserve power and have a
  137. * low exit latency (ie sit in a loop waiting for
  138. * somebody to say that they'd like to reschedule)
  139. */
  140. void cpu_idle(void)
  141. {
  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. rmb();
  149. idle = pm_idle;
  150. if (!idle)
  151. idle = default_idle;
  152. if (cpu_is_offline(smp_processor_id()))
  153. play_dead();
  154. /*
  155. * Idle routines should keep interrupts disabled
  156. * from here on, until they go to idle.
  157. * Otherwise, idle callbacks can misfire.
  158. */
  159. local_irq_disable();
  160. enter_idle();
  161. idle();
  162. /* In many cases the interrupt that ended idle
  163. has already called exit_idle. But some idle
  164. loops can be woken up without interrupt. */
  165. __exit_idle();
  166. }
  167. tick_nohz_restart_sched_tick();
  168. preempt_enable_no_resched();
  169. schedule();
  170. preempt_disable();
  171. }
  172. }
  173. static void do_nothing(void *unused)
  174. {
  175. }
  176. /*
  177. * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
  178. * pm_idle and update to new pm_idle value. Required while changing pm_idle
  179. * handler on SMP systems.
  180. *
  181. * Caller must have changed pm_idle to the new value before the call. Old
  182. * pm_idle value will not be used by any CPU after the return of this function.
  183. */
  184. void cpu_idle_wait(void)
  185. {
  186. smp_mb();
  187. /* kick all the CPUs so that they exit out of pm_idle */
  188. smp_call_function(do_nothing, NULL, 0, 1);
  189. }
  190. EXPORT_SYMBOL_GPL(cpu_idle_wait);
  191. /*
  192. * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
  193. * which can obviate IPI to trigger checking of need_resched.
  194. * We execute MONITOR against need_resched and enter optimized wait state
  195. * through MWAIT. Whenever someone changes need_resched, we would be woken
  196. * up from MWAIT (without an IPI).
  197. *
  198. * New with Core Duo processors, MWAIT can take some hints based on CPU
  199. * capability.
  200. */
  201. void mwait_idle_with_hints(unsigned long ax, unsigned long cx)
  202. {
  203. if (!need_resched()) {
  204. __monitor((void *)&current_thread_info()->flags, 0, 0);
  205. smp_mb();
  206. if (!need_resched())
  207. __mwait(ax, cx);
  208. }
  209. }
  210. /* Default MONITOR/MWAIT with no hints, used for default C1 state */
  211. static void mwait_idle(void)
  212. {
  213. if (!need_resched()) {
  214. __monitor((void *)&current_thread_info()->flags, 0, 0);
  215. smp_mb();
  216. if (!need_resched())
  217. __sti_mwait(0, 0);
  218. else
  219. local_irq_enable();
  220. } else {
  221. local_irq_enable();
  222. }
  223. }
  224. static int __cpuinit mwait_usable(const struct cpuinfo_x86 *c)
  225. {
  226. if (force_mwait)
  227. return 1;
  228. /* Any C1 states supported? */
  229. return c->cpuid_level >= 5 && ((cpuid_edx(5) >> 4) & 0xf) > 0;
  230. }
  231. void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
  232. {
  233. static int selected;
  234. if (selected)
  235. return;
  236. #ifdef CONFIG_X86_SMP
  237. if (pm_idle == poll_idle && smp_num_siblings > 1) {
  238. printk(KERN_WARNING "WARNING: polling idle and HT enabled,"
  239. " performance may degrade.\n");
  240. }
  241. #endif
  242. if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) {
  243. /*
  244. * Skip, if setup has overridden idle.
  245. * One CPU supports mwait => All CPUs supports mwait
  246. */
  247. if (!pm_idle) {
  248. printk(KERN_INFO "using mwait in idle threads.\n");
  249. pm_idle = mwait_idle;
  250. }
  251. }
  252. selected = 1;
  253. }
  254. static int __init idle_setup(char *str)
  255. {
  256. if (!strcmp(str, "poll")) {
  257. printk("using polling idle threads.\n");
  258. pm_idle = poll_idle;
  259. } else if (!strcmp(str, "mwait"))
  260. force_mwait = 1;
  261. else
  262. return -1;
  263. boot_option_idle_override = 1;
  264. return 0;
  265. }
  266. early_param("idle", idle_setup);
  267. /* Prints also some state that isn't saved in the pt_regs */
  268. void __show_regs(struct pt_regs * regs)
  269. {
  270. unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L, fs, gs, shadowgs;
  271. unsigned long d0, d1, d2, d3, d6, d7;
  272. unsigned int fsindex, gsindex;
  273. unsigned int ds, cs, es;
  274. printk("\n");
  275. print_modules();
  276. printk("Pid: %d, comm: %.20s %s %s %.*s\n",
  277. current->pid, current->comm, print_tainted(),
  278. init_utsname()->release,
  279. (int)strcspn(init_utsname()->version, " "),
  280. init_utsname()->version);
  281. printk("RIP: %04lx:[<%016lx>] ", regs->cs & 0xffff, regs->ip);
  282. printk_address(regs->ip, 1);
  283. printk("RSP: %04lx:%016lx EFLAGS: %08lx\n", regs->ss, regs->sp,
  284. regs->flags);
  285. printk("RAX: %016lx RBX: %016lx RCX: %016lx\n",
  286. regs->ax, regs->bx, regs->cx);
  287. printk("RDX: %016lx RSI: %016lx RDI: %016lx\n",
  288. regs->dx, regs->si, regs->di);
  289. printk("RBP: %016lx R08: %016lx R09: %016lx\n",
  290. regs->bp, regs->r8, regs->r9);
  291. printk("R10: %016lx R11: %016lx R12: %016lx\n",
  292. regs->r10, regs->r11, regs->r12);
  293. printk("R13: %016lx R14: %016lx R15: %016lx\n",
  294. regs->r13, regs->r14, regs->r15);
  295. asm("movl %%ds,%0" : "=r" (ds));
  296. asm("movl %%cs,%0" : "=r" (cs));
  297. asm("movl %%es,%0" : "=r" (es));
  298. asm("movl %%fs,%0" : "=r" (fsindex));
  299. asm("movl %%gs,%0" : "=r" (gsindex));
  300. rdmsrl(MSR_FS_BASE, fs);
  301. rdmsrl(MSR_GS_BASE, gs);
  302. rdmsrl(MSR_KERNEL_GS_BASE, shadowgs);
  303. cr0 = read_cr0();
  304. cr2 = read_cr2();
  305. cr3 = read_cr3();
  306. cr4 = read_cr4();
  307. printk("FS: %016lx(%04x) GS:%016lx(%04x) knlGS:%016lx\n",
  308. fs,fsindex,gs,gsindex,shadowgs);
  309. printk("CS: %04x DS: %04x ES: %04x CR0: %016lx\n", cs, ds, es, cr0);
  310. printk("CR2: %016lx CR3: %016lx CR4: %016lx\n", cr2, cr3, cr4);
  311. get_debugreg(d0, 0);
  312. get_debugreg(d1, 1);
  313. get_debugreg(d2, 2);
  314. printk("DR0: %016lx DR1: %016lx DR2: %016lx\n", d0, d1, d2);
  315. get_debugreg(d3, 3);
  316. get_debugreg(d6, 6);
  317. get_debugreg(d7, 7);
  318. printk("DR3: %016lx DR6: %016lx DR7: %016lx\n", d3, d6, d7);
  319. }
  320. void show_regs(struct pt_regs *regs)
  321. {
  322. printk("CPU %d:", smp_processor_id());
  323. __show_regs(regs);
  324. show_trace(NULL, regs, (void *)(regs + 1), regs->bp);
  325. }
  326. /*
  327. * Free current thread data structures etc..
  328. */
  329. void exit_thread(void)
  330. {
  331. struct task_struct *me = current;
  332. struct thread_struct *t = &me->thread;
  333. if (me->thread.io_bitmap_ptr) {
  334. struct tss_struct *tss = &per_cpu(init_tss, get_cpu());
  335. kfree(t->io_bitmap_ptr);
  336. t->io_bitmap_ptr = NULL;
  337. clear_thread_flag(TIF_IO_BITMAP);
  338. /*
  339. * Careful, clear this in the TSS too:
  340. */
  341. memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
  342. t->io_bitmap_max = 0;
  343. put_cpu();
  344. }
  345. }
  346. void flush_thread(void)
  347. {
  348. struct task_struct *tsk = current;
  349. if (test_tsk_thread_flag(tsk, TIF_ABI_PENDING)) {
  350. clear_tsk_thread_flag(tsk, TIF_ABI_PENDING);
  351. if (test_tsk_thread_flag(tsk, TIF_IA32)) {
  352. clear_tsk_thread_flag(tsk, TIF_IA32);
  353. } else {
  354. set_tsk_thread_flag(tsk, TIF_IA32);
  355. current_thread_info()->status |= TS_COMPAT;
  356. }
  357. }
  358. clear_tsk_thread_flag(tsk, TIF_DEBUG);
  359. tsk->thread.debugreg0 = 0;
  360. tsk->thread.debugreg1 = 0;
  361. tsk->thread.debugreg2 = 0;
  362. tsk->thread.debugreg3 = 0;
  363. tsk->thread.debugreg6 = 0;
  364. tsk->thread.debugreg7 = 0;
  365. memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
  366. /*
  367. * Forget coprocessor state..
  368. */
  369. clear_fpu(tsk);
  370. clear_used_math();
  371. }
  372. void release_thread(struct task_struct *dead_task)
  373. {
  374. if (dead_task->mm) {
  375. if (dead_task->mm->context.size) {
  376. printk("WARNING: dead process %8s still has LDT? <%p/%d>\n",
  377. dead_task->comm,
  378. dead_task->mm->context.ldt,
  379. dead_task->mm->context.size);
  380. BUG();
  381. }
  382. }
  383. }
  384. static inline void set_32bit_tls(struct task_struct *t, int tls, u32 addr)
  385. {
  386. struct user_desc ud = {
  387. .base_addr = addr,
  388. .limit = 0xfffff,
  389. .seg_32bit = 1,
  390. .limit_in_pages = 1,
  391. .useable = 1,
  392. };
  393. struct desc_struct *desc = t->thread.tls_array;
  394. desc += tls;
  395. fill_ldt(desc, &ud);
  396. }
  397. static inline u32 read_32bit_tls(struct task_struct *t, int tls)
  398. {
  399. return get_desc_base(&t->thread.tls_array[tls]);
  400. }
  401. /*
  402. * This gets called before we allocate a new thread and copy
  403. * the current task into it.
  404. */
  405. void prepare_to_copy(struct task_struct *tsk)
  406. {
  407. unlazy_fpu(tsk);
  408. }
  409. int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
  410. unsigned long unused,
  411. struct task_struct * p, struct pt_regs * regs)
  412. {
  413. int err;
  414. struct pt_regs * childregs;
  415. struct task_struct *me = current;
  416. childregs = ((struct pt_regs *)
  417. (THREAD_SIZE + task_stack_page(p))) - 1;
  418. *childregs = *regs;
  419. childregs->ax = 0;
  420. childregs->sp = sp;
  421. if (sp == ~0UL)
  422. childregs->sp = (unsigned long)childregs;
  423. p->thread.sp = (unsigned long) childregs;
  424. p->thread.sp0 = (unsigned long) (childregs+1);
  425. p->thread.usersp = me->thread.usersp;
  426. set_tsk_thread_flag(p, TIF_FORK);
  427. p->thread.fs = me->thread.fs;
  428. p->thread.gs = me->thread.gs;
  429. asm("mov %%gs,%0" : "=m" (p->thread.gsindex));
  430. asm("mov %%fs,%0" : "=m" (p->thread.fsindex));
  431. asm("mov %%es,%0" : "=m" (p->thread.es));
  432. asm("mov %%ds,%0" : "=m" (p->thread.ds));
  433. if (unlikely(test_tsk_thread_flag(me, TIF_IO_BITMAP))) {
  434. p->thread.io_bitmap_ptr = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
  435. if (!p->thread.io_bitmap_ptr) {
  436. p->thread.io_bitmap_max = 0;
  437. return -ENOMEM;
  438. }
  439. memcpy(p->thread.io_bitmap_ptr, me->thread.io_bitmap_ptr,
  440. IO_BITMAP_BYTES);
  441. set_tsk_thread_flag(p, TIF_IO_BITMAP);
  442. }
  443. /*
  444. * Set a new TLS for the child thread?
  445. */
  446. if (clone_flags & CLONE_SETTLS) {
  447. #ifdef CONFIG_IA32_EMULATION
  448. if (test_thread_flag(TIF_IA32))
  449. err = do_set_thread_area(p, -1,
  450. (struct user_desc __user *)childregs->si, 0);
  451. else
  452. #endif
  453. err = do_arch_prctl(p, ARCH_SET_FS, childregs->r8);
  454. if (err)
  455. goto out;
  456. }
  457. err = 0;
  458. out:
  459. if (err && p->thread.io_bitmap_ptr) {
  460. kfree(p->thread.io_bitmap_ptr);
  461. p->thread.io_bitmap_max = 0;
  462. }
  463. return err;
  464. }
  465. void
  466. start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
  467. {
  468. asm volatile("movl %0, %%fs; movl %0, %%es; movl %0, %%ds" :: "r"(0));
  469. load_gs_index(0);
  470. regs->ip = new_ip;
  471. regs->sp = new_sp;
  472. write_pda(oldrsp, new_sp);
  473. regs->cs = __USER_CS;
  474. regs->ss = __USER_DS;
  475. regs->flags = 0x200;
  476. set_fs(USER_DS);
  477. /*
  478. * Free the old FP and other extended state
  479. */
  480. free_thread_xstate(current);
  481. }
  482. EXPORT_SYMBOL_GPL(start_thread);
  483. static void hard_disable_TSC(void)
  484. {
  485. write_cr4(read_cr4() | X86_CR4_TSD);
  486. }
  487. void disable_TSC(void)
  488. {
  489. preempt_disable();
  490. if (!test_and_set_thread_flag(TIF_NOTSC))
  491. /*
  492. * Must flip the CPU state synchronously with
  493. * TIF_NOTSC in the current running context.
  494. */
  495. hard_disable_TSC();
  496. preempt_enable();
  497. }
  498. static void hard_enable_TSC(void)
  499. {
  500. write_cr4(read_cr4() & ~X86_CR4_TSD);
  501. }
  502. static void enable_TSC(void)
  503. {
  504. preempt_disable();
  505. if (test_and_clear_thread_flag(TIF_NOTSC))
  506. /*
  507. * Must flip the CPU state synchronously with
  508. * TIF_NOTSC in the current running context.
  509. */
  510. hard_enable_TSC();
  511. preempt_enable();
  512. }
  513. int get_tsc_mode(unsigned long adr)
  514. {
  515. unsigned int val;
  516. if (test_thread_flag(TIF_NOTSC))
  517. val = PR_TSC_SIGSEGV;
  518. else
  519. val = PR_TSC_ENABLE;
  520. return put_user(val, (unsigned int __user *)adr);
  521. }
  522. int set_tsc_mode(unsigned int val)
  523. {
  524. if (val == PR_TSC_SIGSEGV)
  525. disable_TSC();
  526. else if (val == PR_TSC_ENABLE)
  527. enable_TSC();
  528. else
  529. return -EINVAL;
  530. return 0;
  531. }
  532. /*
  533. * This special macro can be used to load a debugging register
  534. */
  535. #define loaddebug(thread, r) set_debugreg(thread->debugreg ## r, r)
  536. static inline void __switch_to_xtra(struct task_struct *prev_p,
  537. struct task_struct *next_p,
  538. struct tss_struct *tss)
  539. {
  540. struct thread_struct *prev, *next;
  541. unsigned long debugctl;
  542. prev = &prev_p->thread,
  543. next = &next_p->thread;
  544. debugctl = prev->debugctlmsr;
  545. if (next->ds_area_msr != prev->ds_area_msr) {
  546. /* we clear debugctl to make sure DS
  547. * is not in use when we change it */
  548. debugctl = 0;
  549. update_debugctlmsr(0);
  550. wrmsrl(MSR_IA32_DS_AREA, next->ds_area_msr);
  551. }
  552. if (next->debugctlmsr != debugctl)
  553. update_debugctlmsr(next->debugctlmsr);
  554. if (test_tsk_thread_flag(next_p, TIF_DEBUG)) {
  555. loaddebug(next, 0);
  556. loaddebug(next, 1);
  557. loaddebug(next, 2);
  558. loaddebug(next, 3);
  559. /* no 4 and 5 */
  560. loaddebug(next, 6);
  561. loaddebug(next, 7);
  562. }
  563. if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
  564. test_tsk_thread_flag(next_p, TIF_NOTSC)) {
  565. /* prev and next are different */
  566. if (test_tsk_thread_flag(next_p, TIF_NOTSC))
  567. hard_disable_TSC();
  568. else
  569. hard_enable_TSC();
  570. }
  571. if (test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
  572. /*
  573. * Copy the relevant range of the IO bitmap.
  574. * Normally this is 128 bytes or less:
  575. */
  576. memcpy(tss->io_bitmap, next->io_bitmap_ptr,
  577. max(prev->io_bitmap_max, next->io_bitmap_max));
  578. } else if (test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)) {
  579. /*
  580. * Clear any possible leftover bits:
  581. */
  582. memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);
  583. }
  584. #ifdef X86_BTS
  585. if (test_tsk_thread_flag(prev_p, TIF_BTS_TRACE_TS))
  586. ptrace_bts_take_timestamp(prev_p, BTS_TASK_DEPARTS);
  587. if (test_tsk_thread_flag(next_p, TIF_BTS_TRACE_TS))
  588. ptrace_bts_take_timestamp(next_p, BTS_TASK_ARRIVES);
  589. #endif
  590. }
  591. /*
  592. * switch_to(x,y) should switch tasks from x to y.
  593. *
  594. * This could still be optimized:
  595. * - fold all the options into a flag word and test it with a single test.
  596. * - could test fs/gs bitsliced
  597. *
  598. * Kprobes not supported here. Set the probe on schedule instead.
  599. */
  600. struct task_struct *
  601. __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
  602. {
  603. struct thread_struct *prev = &prev_p->thread,
  604. *next = &next_p->thread;
  605. int cpu = smp_processor_id();
  606. struct tss_struct *tss = &per_cpu(init_tss, cpu);
  607. /* we're going to use this soon, after a few expensive things */
  608. if (next_p->fpu_counter>5)
  609. prefetch(next->xstate);
  610. /*
  611. * Reload esp0, LDT and the page table pointer:
  612. */
  613. load_sp0(tss, next);
  614. /*
  615. * Switch DS and ES.
  616. * This won't pick up thread selector changes, but I guess that is ok.
  617. */
  618. asm volatile("mov %%es,%0" : "=m" (prev->es));
  619. if (unlikely(next->es | prev->es))
  620. loadsegment(es, next->es);
  621. asm volatile ("mov %%ds,%0" : "=m" (prev->ds));
  622. if (unlikely(next->ds | prev->ds))
  623. loadsegment(ds, next->ds);
  624. load_TLS(next, cpu);
  625. /*
  626. * Switch FS and GS.
  627. */
  628. {
  629. unsigned fsindex;
  630. asm volatile("movl %%fs,%0" : "=r" (fsindex));
  631. /* segment register != 0 always requires a reload.
  632. also reload when it has changed.
  633. when prev process used 64bit base always reload
  634. to avoid an information leak. */
  635. if (unlikely(fsindex | next->fsindex | prev->fs)) {
  636. loadsegment(fs, next->fsindex);
  637. /* check if the user used a selector != 0
  638. * if yes clear 64bit base, since overloaded base
  639. * is always mapped to the Null selector
  640. */
  641. if (fsindex)
  642. prev->fs = 0;
  643. }
  644. /* when next process has a 64bit base use it */
  645. if (next->fs)
  646. wrmsrl(MSR_FS_BASE, next->fs);
  647. prev->fsindex = fsindex;
  648. }
  649. {
  650. unsigned gsindex;
  651. asm volatile("movl %%gs,%0" : "=r" (gsindex));
  652. if (unlikely(gsindex | next->gsindex | prev->gs)) {
  653. load_gs_index(next->gsindex);
  654. if (gsindex)
  655. prev->gs = 0;
  656. }
  657. if (next->gs)
  658. wrmsrl(MSR_KERNEL_GS_BASE, next->gs);
  659. prev->gsindex = gsindex;
  660. }
  661. /* Must be after DS reload */
  662. unlazy_fpu(prev_p);
  663. /*
  664. * Switch the PDA and FPU contexts.
  665. */
  666. prev->usersp = read_pda(oldrsp);
  667. write_pda(oldrsp, next->usersp);
  668. write_pda(pcurrent, next_p);
  669. write_pda(kernelstack,
  670. (unsigned long)task_stack_page(next_p) + THREAD_SIZE - PDA_STACKOFFSET);
  671. #ifdef CONFIG_CC_STACKPROTECTOR
  672. write_pda(stack_canary, next_p->stack_canary);
  673. /*
  674. * Build time only check to make sure the stack_canary is at
  675. * offset 40 in the pda; this is a gcc ABI requirement
  676. */
  677. BUILD_BUG_ON(offsetof(struct x8664_pda, stack_canary) != 40);
  678. #endif
  679. /*
  680. * Now maybe reload the debug registers and handle I/O bitmaps
  681. */
  682. if (unlikely(task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT ||
  683. task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV))
  684. __switch_to_xtra(prev_p, next_p, tss);
  685. /* If the task has used fpu the last 5 timeslices, just do a full
  686. * restore of the math state immediately to avoid the trap; the
  687. * chances of needing FPU soon are obviously high now
  688. */
  689. if (next_p->fpu_counter>5)
  690. math_state_restore();
  691. return prev_p;
  692. }
  693. /*
  694. * sys_execve() executes a new program.
  695. */
  696. asmlinkage
  697. long sys_execve(char __user *name, char __user * __user *argv,
  698. char __user * __user *envp, struct pt_regs *regs)
  699. {
  700. long error;
  701. char * filename;
  702. filename = getname(name);
  703. error = PTR_ERR(filename);
  704. if (IS_ERR(filename))
  705. return error;
  706. error = do_execve(filename, argv, envp, regs);
  707. putname(filename);
  708. return error;
  709. }
  710. void set_personality_64bit(void)
  711. {
  712. /* inherit personality from parent */
  713. /* Make sure to be in 64bit mode */
  714. clear_thread_flag(TIF_IA32);
  715. /* TBD: overwrites user setup. Should have two bits.
  716. But 64bit processes have always behaved this way,
  717. so it's not too bad. The main problem is just that
  718. 32bit childs are affected again. */
  719. current->personality &= ~READ_IMPLIES_EXEC;
  720. }
  721. asmlinkage long sys_fork(struct pt_regs *regs)
  722. {
  723. return do_fork(SIGCHLD, regs->sp, regs, 0, NULL, NULL);
  724. }
  725. asmlinkage long
  726. sys_clone(unsigned long clone_flags, unsigned long newsp,
  727. void __user *parent_tid, void __user *child_tid, struct pt_regs *regs)
  728. {
  729. if (!newsp)
  730. newsp = regs->sp;
  731. return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
  732. }
  733. /*
  734. * This is trivial, and on the face of it looks like it
  735. * could equally well be done in user mode.
  736. *
  737. * Not so, for quite unobvious reasons - register pressure.
  738. * In user mode vfork() cannot have a stack frame, and if
  739. * done by calling the "clone()" system call directly, you
  740. * do not have enough call-clobbered registers to hold all
  741. * the information you need.
  742. */
  743. asmlinkage long sys_vfork(struct pt_regs *regs)
  744. {
  745. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->sp, regs, 0,
  746. NULL, NULL);
  747. }
  748. unsigned long get_wchan(struct task_struct *p)
  749. {
  750. unsigned long stack;
  751. u64 fp,ip;
  752. int count = 0;
  753. if (!p || p == current || p->state==TASK_RUNNING)
  754. return 0;
  755. stack = (unsigned long)task_stack_page(p);
  756. if (p->thread.sp < stack || p->thread.sp > stack+THREAD_SIZE)
  757. return 0;
  758. fp = *(u64 *)(p->thread.sp);
  759. do {
  760. if (fp < (unsigned long)stack ||
  761. fp > (unsigned long)stack+THREAD_SIZE)
  762. return 0;
  763. ip = *(u64 *)(fp+8);
  764. if (!in_sched_functions(ip))
  765. return ip;
  766. fp = *(u64 *)fp;
  767. } while (count++ < 16);
  768. return 0;
  769. }
  770. long do_arch_prctl(struct task_struct *task, int code, unsigned long addr)
  771. {
  772. int ret = 0;
  773. int doit = task == current;
  774. int cpu;
  775. switch (code) {
  776. case ARCH_SET_GS:
  777. if (addr >= TASK_SIZE_OF(task))
  778. return -EPERM;
  779. cpu = get_cpu();
  780. /* handle small bases via the GDT because that's faster to
  781. switch. */
  782. if (addr <= 0xffffffff) {
  783. set_32bit_tls(task, GS_TLS, addr);
  784. if (doit) {
  785. load_TLS(&task->thread, cpu);
  786. load_gs_index(GS_TLS_SEL);
  787. }
  788. task->thread.gsindex = GS_TLS_SEL;
  789. task->thread.gs = 0;
  790. } else {
  791. task->thread.gsindex = 0;
  792. task->thread.gs = addr;
  793. if (doit) {
  794. load_gs_index(0);
  795. ret = checking_wrmsrl(MSR_KERNEL_GS_BASE, addr);
  796. }
  797. }
  798. put_cpu();
  799. break;
  800. case ARCH_SET_FS:
  801. /* Not strictly needed for fs, but do it for symmetry
  802. with gs */
  803. if (addr >= TASK_SIZE_OF(task))
  804. return -EPERM;
  805. cpu = get_cpu();
  806. /* handle small bases via the GDT because that's faster to
  807. switch. */
  808. if (addr <= 0xffffffff) {
  809. set_32bit_tls(task, FS_TLS, addr);
  810. if (doit) {
  811. load_TLS(&task->thread, cpu);
  812. asm volatile("movl %0,%%fs" :: "r"(FS_TLS_SEL));
  813. }
  814. task->thread.fsindex = FS_TLS_SEL;
  815. task->thread.fs = 0;
  816. } else {
  817. task->thread.fsindex = 0;
  818. task->thread.fs = addr;
  819. if (doit) {
  820. /* set the selector to 0 to not confuse
  821. __switch_to */
  822. asm volatile("movl %0,%%fs" :: "r" (0));
  823. ret = checking_wrmsrl(MSR_FS_BASE, addr);
  824. }
  825. }
  826. put_cpu();
  827. break;
  828. case ARCH_GET_FS: {
  829. unsigned long base;
  830. if (task->thread.fsindex == FS_TLS_SEL)
  831. base = read_32bit_tls(task, FS_TLS);
  832. else if (doit)
  833. rdmsrl(MSR_FS_BASE, base);
  834. else
  835. base = task->thread.fs;
  836. ret = put_user(base, (unsigned long __user *)addr);
  837. break;
  838. }
  839. case ARCH_GET_GS: {
  840. unsigned long base;
  841. unsigned gsindex;
  842. if (task->thread.gsindex == GS_TLS_SEL)
  843. base = read_32bit_tls(task, GS_TLS);
  844. else if (doit) {
  845. asm("movl %%gs,%0" : "=r" (gsindex));
  846. if (gsindex)
  847. rdmsrl(MSR_KERNEL_GS_BASE, base);
  848. else
  849. base = task->thread.gs;
  850. }
  851. else
  852. base = task->thread.gs;
  853. ret = put_user(base, (unsigned long __user *)addr);
  854. break;
  855. }
  856. default:
  857. ret = -EINVAL;
  858. break;
  859. }
  860. return ret;
  861. }
  862. long sys_arch_prctl(int code, unsigned long addr)
  863. {
  864. return do_arch_prctl(current, code, addr);
  865. }
  866. unsigned long arch_align_stack(unsigned long sp)
  867. {
  868. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  869. sp -= get_random_int() % 8192;
  870. return sp & ~0xf;
  871. }
  872. unsigned long arch_randomize_brk(struct mm_struct *mm)
  873. {
  874. unsigned long range_end = mm->brk + 0x02000000;
  875. return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
  876. }