process_64.c 22 KB

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