process_64.c 20 KB

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