process_64.c 21 KB

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