process_64.c 20 KB

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