process.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /*
  2. * linux/arch/x86-64/kernel/process.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. *
  6. * Pentium III FXSR, SSE support
  7. * Gareth Hughes <gareth@valinux.com>, May 2000
  8. *
  9. * X86-64 port
  10. * Andi Kleen.
  11. *
  12. * CPU hotplug support - ashok.raj@intel.com
  13. * $Id: process.c,v 1.38 2002/01/15 10:08:03 ak Exp $
  14. */
  15. /*
  16. * This file handles the architecture-dependent parts of process handling..
  17. */
  18. #include <stdarg.h>
  19. #include <linux/cpu.h>
  20. #include <linux/errno.h>
  21. #include <linux/sched.h>
  22. #include <linux/kernel.h>
  23. #include <linux/mm.h>
  24. #include <linux/elfcore.h>
  25. #include <linux/smp.h>
  26. #include <linux/slab.h>
  27. #include <linux/user.h>
  28. #include <linux/module.h>
  29. #include <linux/a.out.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/delay.h>
  32. #include <linux/ptrace.h>
  33. #include <linux/utsname.h>
  34. #include <linux/random.h>
  35. #include <linux/kprobes.h>
  36. #include <linux/notifier.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/kdebug.h>
  47. #include <asm/desc.h>
  48. #include <asm/proto.h>
  49. #include <asm/ia32.h>
  50. #include <asm/idle.h>
  51. asmlinkage extern void ret_from_fork(void);
  52. unsigned long kernel_thread_flags = CLONE_VM | CLONE_UNTRACED;
  53. unsigned long boot_option_idle_override = 0;
  54. EXPORT_SYMBOL(boot_option_idle_override);
  55. /*
  56. * Powermanagement idle function, if any..
  57. */
  58. void (*pm_idle)(void);
  59. static DEFINE_PER_CPU(unsigned int, cpu_idle_state);
  60. static struct notifier_block *idle_notifier;
  61. static DEFINE_SPINLOCK(idle_notifier_lock);
  62. void idle_notifier_register(struct notifier_block *n)
  63. {
  64. unsigned long flags;
  65. spin_lock_irqsave(&idle_notifier_lock, flags);
  66. notifier_chain_register(&idle_notifier, n);
  67. spin_unlock_irqrestore(&idle_notifier_lock, flags);
  68. }
  69. EXPORT_SYMBOL_GPL(idle_notifier_register);
  70. void idle_notifier_unregister(struct notifier_block *n)
  71. {
  72. unsigned long flags;
  73. spin_lock_irqsave(&idle_notifier_lock, flags);
  74. notifier_chain_unregister(&idle_notifier, n);
  75. spin_unlock_irqrestore(&idle_notifier_lock, flags);
  76. }
  77. EXPORT_SYMBOL(idle_notifier_unregister);
  78. enum idle_state { CPU_IDLE, CPU_NOT_IDLE };
  79. static DEFINE_PER_CPU(enum idle_state, idle_state) = CPU_NOT_IDLE;
  80. void enter_idle(void)
  81. {
  82. __get_cpu_var(idle_state) = CPU_IDLE;
  83. notifier_call_chain(&idle_notifier, IDLE_START, NULL);
  84. }
  85. static void __exit_idle(void)
  86. {
  87. __get_cpu_var(idle_state) = CPU_NOT_IDLE;
  88. notifier_call_chain(&idle_notifier, IDLE_END, NULL);
  89. }
  90. /* Called from interrupts to signify idle end */
  91. void exit_idle(void)
  92. {
  93. if (current->pid | read_pda(irqcount))
  94. return;
  95. __exit_idle();
  96. }
  97. /*
  98. * We use this if we don't have any better
  99. * idle routine..
  100. */
  101. void default_idle(void)
  102. {
  103. local_irq_enable();
  104. clear_thread_flag(TIF_POLLING_NRFLAG);
  105. smp_mb__after_clear_bit();
  106. while (!need_resched()) {
  107. local_irq_disable();
  108. if (!need_resched())
  109. safe_halt();
  110. else
  111. local_irq_enable();
  112. }
  113. set_thread_flag(TIF_POLLING_NRFLAG);
  114. }
  115. /*
  116. * On SMP it's slightly faster (but much more power-consuming!)
  117. * to poll the ->need_resched flag instead of waiting for the
  118. * cross-CPU IPI to arrive. Use this option with caution.
  119. */
  120. static void poll_idle (void)
  121. {
  122. local_irq_enable();
  123. asm volatile(
  124. "2:"
  125. "testl %0,%1;"
  126. "rep; nop;"
  127. "je 2b;"
  128. : :
  129. "i" (_TIF_NEED_RESCHED),
  130. "m" (current_thread_info()->flags));
  131. }
  132. void cpu_idle_wait(void)
  133. {
  134. unsigned int cpu, this_cpu = get_cpu();
  135. cpumask_t map;
  136. set_cpus_allowed(current, cpumask_of_cpu(this_cpu));
  137. put_cpu();
  138. cpus_clear(map);
  139. for_each_online_cpu(cpu) {
  140. per_cpu(cpu_idle_state, cpu) = 1;
  141. cpu_set(cpu, map);
  142. }
  143. __get_cpu_var(cpu_idle_state) = 0;
  144. wmb();
  145. do {
  146. ssleep(1);
  147. for_each_online_cpu(cpu) {
  148. if (cpu_isset(cpu, map) &&
  149. !per_cpu(cpu_idle_state, cpu))
  150. cpu_clear(cpu, map);
  151. }
  152. cpus_and(map, map, cpu_online_map);
  153. } while (!cpus_empty(map));
  154. }
  155. EXPORT_SYMBOL_GPL(cpu_idle_wait);
  156. #ifdef CONFIG_HOTPLUG_CPU
  157. DECLARE_PER_CPU(int, cpu_state);
  158. #include <asm/nmi.h>
  159. /* We halt the CPU with physical CPU hotplug */
  160. static inline void play_dead(void)
  161. {
  162. idle_task_exit();
  163. wbinvd();
  164. mb();
  165. /* Ack it */
  166. __get_cpu_var(cpu_state) = CPU_DEAD;
  167. local_irq_disable();
  168. while (1)
  169. halt();
  170. }
  171. #else
  172. static inline void play_dead(void)
  173. {
  174. BUG();
  175. }
  176. #endif /* CONFIG_HOTPLUG_CPU */
  177. /*
  178. * The idle thread. There's no useful work to be
  179. * done, so just try to conserve power and have a
  180. * low exit latency (ie sit in a loop waiting for
  181. * somebody to say that they'd like to reschedule)
  182. */
  183. void cpu_idle (void)
  184. {
  185. set_thread_flag(TIF_POLLING_NRFLAG);
  186. /* endless idle loop with no priority at all */
  187. while (1) {
  188. while (!need_resched()) {
  189. void (*idle)(void);
  190. if (__get_cpu_var(cpu_idle_state))
  191. __get_cpu_var(cpu_idle_state) = 0;
  192. rmb();
  193. idle = pm_idle;
  194. if (!idle)
  195. idle = default_idle;
  196. if (cpu_is_offline(smp_processor_id()))
  197. play_dead();
  198. enter_idle();
  199. idle();
  200. __exit_idle();
  201. }
  202. preempt_enable_no_resched();
  203. schedule();
  204. preempt_disable();
  205. }
  206. }
  207. /*
  208. * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
  209. * which can obviate IPI to trigger checking of need_resched.
  210. * We execute MONITOR against need_resched and enter optimized wait state
  211. * through MWAIT. Whenever someone changes need_resched, we would be woken
  212. * up from MWAIT (without an IPI).
  213. */
  214. static void mwait_idle(void)
  215. {
  216. local_irq_enable();
  217. while (!need_resched()) {
  218. __monitor((void *)&current_thread_info()->flags, 0, 0);
  219. smp_mb();
  220. if (need_resched())
  221. break;
  222. __mwait(0, 0);
  223. }
  224. }
  225. void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
  226. {
  227. static int printed;
  228. if (cpu_has(c, X86_FEATURE_MWAIT)) {
  229. /*
  230. * Skip, if setup has overridden idle.
  231. * One CPU supports mwait => All CPUs supports mwait
  232. */
  233. if (!pm_idle) {
  234. if (!printed) {
  235. printk("using mwait in idle threads.\n");
  236. printed = 1;
  237. }
  238. pm_idle = mwait_idle;
  239. }
  240. }
  241. }
  242. static int __init idle_setup (char *str)
  243. {
  244. if (!strncmp(str, "poll", 4)) {
  245. printk("using polling idle threads.\n");
  246. pm_idle = poll_idle;
  247. }
  248. boot_option_idle_override = 1;
  249. return 1;
  250. }
  251. __setup("idle=", idle_setup);
  252. /* Prints also some state that isn't saved in the pt_regs */
  253. void __show_regs(struct pt_regs * regs)
  254. {
  255. unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L, fs, gs, shadowgs;
  256. unsigned int fsindex,gsindex;
  257. unsigned int ds,cs,es;
  258. printk("\n");
  259. print_modules();
  260. printk("Pid: %d, comm: %.20s %s %s %.*s\n",
  261. current->pid, current->comm, print_tainted(),
  262. system_utsname.release,
  263. (int)strcspn(system_utsname.version, " "),
  264. system_utsname.version);
  265. printk("RIP: %04lx:[<%016lx>] ", regs->cs & 0xffff, regs->rip);
  266. printk_address(regs->rip);
  267. printk("\nRSP: %04lx:%016lx EFLAGS: %08lx\n", regs->ss, regs->rsp,
  268. regs->eflags);
  269. printk("RAX: %016lx RBX: %016lx RCX: %016lx\n",
  270. regs->rax, regs->rbx, regs->rcx);
  271. printk("RDX: %016lx RSI: %016lx RDI: %016lx\n",
  272. regs->rdx, regs->rsi, regs->rdi);
  273. printk("RBP: %016lx R08: %016lx R09: %016lx\n",
  274. regs->rbp, regs->r8, regs->r9);
  275. printk("R10: %016lx R11: %016lx R12: %016lx\n",
  276. regs->r10, regs->r11, regs->r12);
  277. printk("R13: %016lx R14: %016lx R15: %016lx\n",
  278. regs->r13, regs->r14, regs->r15);
  279. asm("movl %%ds,%0" : "=r" (ds));
  280. asm("movl %%cs,%0" : "=r" (cs));
  281. asm("movl %%es,%0" : "=r" (es));
  282. asm("movl %%fs,%0" : "=r" (fsindex));
  283. asm("movl %%gs,%0" : "=r" (gsindex));
  284. rdmsrl(MSR_FS_BASE, fs);
  285. rdmsrl(MSR_GS_BASE, gs);
  286. rdmsrl(MSR_KERNEL_GS_BASE, shadowgs);
  287. asm("movq %%cr0, %0": "=r" (cr0));
  288. asm("movq %%cr2, %0": "=r" (cr2));
  289. asm("movq %%cr3, %0": "=r" (cr3));
  290. asm("movq %%cr4, %0": "=r" (cr4));
  291. printk("FS: %016lx(%04x) GS:%016lx(%04x) knlGS:%016lx\n",
  292. fs,fsindex,gs,gsindex,shadowgs);
  293. printk("CS: %04x DS: %04x ES: %04x CR0: %016lx\n", cs, ds, es, cr0);
  294. printk("CR2: %016lx CR3: %016lx CR4: %016lx\n", cr2, cr3, cr4);
  295. }
  296. void show_regs(struct pt_regs *regs)
  297. {
  298. printk("CPU %d:", smp_processor_id());
  299. __show_regs(regs);
  300. show_trace(&regs->rsp);
  301. }
  302. /*
  303. * Free current thread data structures etc..
  304. */
  305. void exit_thread(void)
  306. {
  307. struct task_struct *me = current;
  308. struct thread_struct *t = &me->thread;
  309. /*
  310. * Remove function-return probe instances associated with this task
  311. * and put them back on the free list. Do not insert an exit probe for
  312. * this function, it will be disabled by kprobe_flush_task if you do.
  313. */
  314. kprobe_flush_task(me);
  315. if (me->thread.io_bitmap_ptr) {
  316. struct tss_struct *tss = &per_cpu(init_tss, get_cpu());
  317. kfree(t->io_bitmap_ptr);
  318. t->io_bitmap_ptr = NULL;
  319. /*
  320. * Careful, clear this in the TSS too:
  321. */
  322. memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
  323. t->io_bitmap_max = 0;
  324. put_cpu();
  325. }
  326. }
  327. void flush_thread(void)
  328. {
  329. struct task_struct *tsk = current;
  330. struct thread_info *t = current_thread_info();
  331. if (t->flags & _TIF_ABI_PENDING)
  332. t->flags ^= (_TIF_ABI_PENDING | _TIF_IA32);
  333. tsk->thread.debugreg0 = 0;
  334. tsk->thread.debugreg1 = 0;
  335. tsk->thread.debugreg2 = 0;
  336. tsk->thread.debugreg3 = 0;
  337. tsk->thread.debugreg6 = 0;
  338. tsk->thread.debugreg7 = 0;
  339. memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
  340. /*
  341. * Forget coprocessor state..
  342. */
  343. clear_fpu(tsk);
  344. clear_used_math();
  345. }
  346. void release_thread(struct task_struct *dead_task)
  347. {
  348. if (dead_task->mm) {
  349. if (dead_task->mm->context.size) {
  350. printk("WARNING: dead process %8s still has LDT? <%p/%d>\n",
  351. dead_task->comm,
  352. dead_task->mm->context.ldt,
  353. dead_task->mm->context.size);
  354. BUG();
  355. }
  356. }
  357. }
  358. static inline void set_32bit_tls(struct task_struct *t, int tls, u32 addr)
  359. {
  360. struct user_desc ud = {
  361. .base_addr = addr,
  362. .limit = 0xfffff,
  363. .seg_32bit = 1,
  364. .limit_in_pages = 1,
  365. .useable = 1,
  366. };
  367. struct n_desc_struct *desc = (void *)t->thread.tls_array;
  368. desc += tls;
  369. desc->a = LDT_entry_a(&ud);
  370. desc->b = LDT_entry_b(&ud);
  371. }
  372. static inline u32 read_32bit_tls(struct task_struct *t, int tls)
  373. {
  374. struct desc_struct *desc = (void *)t->thread.tls_array;
  375. desc += tls;
  376. return desc->base0 |
  377. (((u32)desc->base1) << 16) |
  378. (((u32)desc->base2) << 24);
  379. }
  380. /*
  381. * This gets called before we allocate a new thread and copy
  382. * the current task into it.
  383. */
  384. void prepare_to_copy(struct task_struct *tsk)
  385. {
  386. unlazy_fpu(tsk);
  387. }
  388. int copy_thread(int nr, unsigned long clone_flags, unsigned long rsp,
  389. unsigned long unused,
  390. struct task_struct * p, struct pt_regs * regs)
  391. {
  392. int err;
  393. struct pt_regs * childregs;
  394. struct task_struct *me = current;
  395. childregs = ((struct pt_regs *)
  396. (THREAD_SIZE + task_stack_page(p))) - 1;
  397. *childregs = *regs;
  398. childregs->rax = 0;
  399. childregs->rsp = rsp;
  400. if (rsp == ~0UL)
  401. childregs->rsp = (unsigned long)childregs;
  402. p->thread.rsp = (unsigned long) childregs;
  403. p->thread.rsp0 = (unsigned long) (childregs+1);
  404. p->thread.userrsp = me->thread.userrsp;
  405. set_tsk_thread_flag(p, TIF_FORK);
  406. p->thread.fs = me->thread.fs;
  407. p->thread.gs = me->thread.gs;
  408. asm("mov %%gs,%0" : "=m" (p->thread.gsindex));
  409. asm("mov %%fs,%0" : "=m" (p->thread.fsindex));
  410. asm("mov %%es,%0" : "=m" (p->thread.es));
  411. asm("mov %%ds,%0" : "=m" (p->thread.ds));
  412. if (unlikely(me->thread.io_bitmap_ptr != NULL)) {
  413. p->thread.io_bitmap_ptr = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
  414. if (!p->thread.io_bitmap_ptr) {
  415. p->thread.io_bitmap_max = 0;
  416. return -ENOMEM;
  417. }
  418. memcpy(p->thread.io_bitmap_ptr, me->thread.io_bitmap_ptr,
  419. IO_BITMAP_BYTES);
  420. }
  421. /*
  422. * Set a new TLS for the child thread?
  423. */
  424. if (clone_flags & CLONE_SETTLS) {
  425. #ifdef CONFIG_IA32_EMULATION
  426. if (test_thread_flag(TIF_IA32))
  427. err = ia32_child_tls(p, childregs);
  428. else
  429. #endif
  430. err = do_arch_prctl(p, ARCH_SET_FS, childregs->r8);
  431. if (err)
  432. goto out;
  433. }
  434. err = 0;
  435. out:
  436. if (err && p->thread.io_bitmap_ptr) {
  437. kfree(p->thread.io_bitmap_ptr);
  438. p->thread.io_bitmap_max = 0;
  439. }
  440. return err;
  441. }
  442. /*
  443. * This special macro can be used to load a debugging register
  444. */
  445. #define loaddebug(thread,r) set_debug(thread->debugreg ## r, r)
  446. /*
  447. * switch_to(x,y) should switch tasks from x to y.
  448. *
  449. * This could still be optimized:
  450. * - fold all the options into a flag word and test it with a single test.
  451. * - could test fs/gs bitsliced
  452. *
  453. * Kprobes not supported here. Set the probe on schedule instead.
  454. */
  455. __kprobes struct task_struct *
  456. __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
  457. {
  458. struct thread_struct *prev = &prev_p->thread,
  459. *next = &next_p->thread;
  460. int cpu = smp_processor_id();
  461. struct tss_struct *tss = &per_cpu(init_tss, cpu);
  462. unlazy_fpu(prev_p);
  463. /*
  464. * Reload esp0, LDT and the page table pointer:
  465. */
  466. tss->rsp0 = next->rsp0;
  467. /*
  468. * Switch DS and ES.
  469. * This won't pick up thread selector changes, but I guess that is ok.
  470. */
  471. asm volatile("mov %%es,%0" : "=m" (prev->es));
  472. if (unlikely(next->es | prev->es))
  473. loadsegment(es, next->es);
  474. asm volatile ("mov %%ds,%0" : "=m" (prev->ds));
  475. if (unlikely(next->ds | prev->ds))
  476. loadsegment(ds, next->ds);
  477. load_TLS(next, cpu);
  478. /*
  479. * Switch FS and GS.
  480. */
  481. {
  482. unsigned fsindex;
  483. asm volatile("movl %%fs,%0" : "=r" (fsindex));
  484. /* segment register != 0 always requires a reload.
  485. also reload when it has changed.
  486. when prev process used 64bit base always reload
  487. to avoid an information leak. */
  488. if (unlikely(fsindex | next->fsindex | prev->fs)) {
  489. loadsegment(fs, next->fsindex);
  490. /* check if the user used a selector != 0
  491. * if yes clear 64bit base, since overloaded base
  492. * is always mapped to the Null selector
  493. */
  494. if (fsindex)
  495. prev->fs = 0;
  496. }
  497. /* when next process has a 64bit base use it */
  498. if (next->fs)
  499. wrmsrl(MSR_FS_BASE, next->fs);
  500. prev->fsindex = fsindex;
  501. }
  502. {
  503. unsigned gsindex;
  504. asm volatile("movl %%gs,%0" : "=r" (gsindex));
  505. if (unlikely(gsindex | next->gsindex | prev->gs)) {
  506. load_gs_index(next->gsindex);
  507. if (gsindex)
  508. prev->gs = 0;
  509. }
  510. if (next->gs)
  511. wrmsrl(MSR_KERNEL_GS_BASE, next->gs);
  512. prev->gsindex = gsindex;
  513. }
  514. /*
  515. * Switch the PDA context.
  516. */
  517. prev->userrsp = read_pda(oldrsp);
  518. write_pda(oldrsp, next->userrsp);
  519. write_pda(pcurrent, next_p);
  520. write_pda(kernelstack,
  521. task_stack_page(next_p) + THREAD_SIZE - PDA_STACKOFFSET);
  522. /*
  523. * Now maybe reload the debug registers
  524. */
  525. if (unlikely(next->debugreg7)) {
  526. loaddebug(next, 0);
  527. loaddebug(next, 1);
  528. loaddebug(next, 2);
  529. loaddebug(next, 3);
  530. /* no 4 and 5 */
  531. loaddebug(next, 6);
  532. loaddebug(next, 7);
  533. }
  534. /*
  535. * Handle the IO bitmap
  536. */
  537. if (unlikely(prev->io_bitmap_ptr || next->io_bitmap_ptr)) {
  538. if (next->io_bitmap_ptr)
  539. /*
  540. * Copy the relevant range of the IO bitmap.
  541. * Normally this is 128 bytes or less:
  542. */
  543. memcpy(tss->io_bitmap, next->io_bitmap_ptr,
  544. max(prev->io_bitmap_max, next->io_bitmap_max));
  545. else {
  546. /*
  547. * Clear any possible leftover bits:
  548. */
  549. memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);
  550. }
  551. }
  552. return prev_p;
  553. }
  554. /*
  555. * sys_execve() executes a new program.
  556. */
  557. asmlinkage
  558. long sys_execve(char __user *name, char __user * __user *argv,
  559. char __user * __user *envp, struct pt_regs regs)
  560. {
  561. long error;
  562. char * filename;
  563. filename = getname(name);
  564. error = PTR_ERR(filename);
  565. if (IS_ERR(filename))
  566. return error;
  567. error = do_execve(filename, argv, envp, &regs);
  568. if (error == 0) {
  569. task_lock(current);
  570. current->ptrace &= ~PT_DTRACE;
  571. task_unlock(current);
  572. }
  573. putname(filename);
  574. return error;
  575. }
  576. void set_personality_64bit(void)
  577. {
  578. /* inherit personality from parent */
  579. /* Make sure to be in 64bit mode */
  580. clear_thread_flag(TIF_IA32);
  581. /* TBD: overwrites user setup. Should have two bits.
  582. But 64bit processes have always behaved this way,
  583. so it's not too bad. The main problem is just that
  584. 32bit childs are affected again. */
  585. current->personality &= ~READ_IMPLIES_EXEC;
  586. }
  587. asmlinkage long sys_fork(struct pt_regs *regs)
  588. {
  589. return do_fork(SIGCHLD, regs->rsp, regs, 0, NULL, NULL);
  590. }
  591. asmlinkage long
  592. sys_clone(unsigned long clone_flags, unsigned long newsp,
  593. void __user *parent_tid, void __user *child_tid, struct pt_regs *regs)
  594. {
  595. if (!newsp)
  596. newsp = regs->rsp;
  597. return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
  598. }
  599. /*
  600. * This is trivial, and on the face of it looks like it
  601. * could equally well be done in user mode.
  602. *
  603. * Not so, for quite unobvious reasons - register pressure.
  604. * In user mode vfork() cannot have a stack frame, and if
  605. * done by calling the "clone()" system call directly, you
  606. * do not have enough call-clobbered registers to hold all
  607. * the information you need.
  608. */
  609. asmlinkage long sys_vfork(struct pt_regs *regs)
  610. {
  611. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->rsp, regs, 0,
  612. NULL, NULL);
  613. }
  614. unsigned long get_wchan(struct task_struct *p)
  615. {
  616. unsigned long stack;
  617. u64 fp,rip;
  618. int count = 0;
  619. if (!p || p == current || p->state==TASK_RUNNING)
  620. return 0;
  621. stack = (unsigned long)task_stack_page(p);
  622. if (p->thread.rsp < stack || p->thread.rsp > stack+THREAD_SIZE)
  623. return 0;
  624. fp = *(u64 *)(p->thread.rsp);
  625. do {
  626. if (fp < (unsigned long)stack ||
  627. fp > (unsigned long)stack+THREAD_SIZE)
  628. return 0;
  629. rip = *(u64 *)(fp+8);
  630. if (!in_sched_functions(rip))
  631. return rip;
  632. fp = *(u64 *)fp;
  633. } while (count++ < 16);
  634. return 0;
  635. }
  636. long do_arch_prctl(struct task_struct *task, int code, unsigned long addr)
  637. {
  638. int ret = 0;
  639. int doit = task == current;
  640. int cpu;
  641. switch (code) {
  642. case ARCH_SET_GS:
  643. if (addr >= TASK_SIZE_OF(task))
  644. return -EPERM;
  645. cpu = get_cpu();
  646. /* handle small bases via the GDT because that's faster to
  647. switch. */
  648. if (addr <= 0xffffffff) {
  649. set_32bit_tls(task, GS_TLS, addr);
  650. if (doit) {
  651. load_TLS(&task->thread, cpu);
  652. load_gs_index(GS_TLS_SEL);
  653. }
  654. task->thread.gsindex = GS_TLS_SEL;
  655. task->thread.gs = 0;
  656. } else {
  657. task->thread.gsindex = 0;
  658. task->thread.gs = addr;
  659. if (doit) {
  660. load_gs_index(0);
  661. ret = checking_wrmsrl(MSR_KERNEL_GS_BASE, addr);
  662. }
  663. }
  664. put_cpu();
  665. break;
  666. case ARCH_SET_FS:
  667. /* Not strictly needed for fs, but do it for symmetry
  668. with gs */
  669. if (addr >= TASK_SIZE_OF(task))
  670. return -EPERM;
  671. cpu = get_cpu();
  672. /* handle small bases via the GDT because that's faster to
  673. switch. */
  674. if (addr <= 0xffffffff) {
  675. set_32bit_tls(task, FS_TLS, addr);
  676. if (doit) {
  677. load_TLS(&task->thread, cpu);
  678. asm volatile("movl %0,%%fs" :: "r"(FS_TLS_SEL));
  679. }
  680. task->thread.fsindex = FS_TLS_SEL;
  681. task->thread.fs = 0;
  682. } else {
  683. task->thread.fsindex = 0;
  684. task->thread.fs = addr;
  685. if (doit) {
  686. /* set the selector to 0 to not confuse
  687. __switch_to */
  688. asm volatile("movl %0,%%fs" :: "r" (0));
  689. ret = checking_wrmsrl(MSR_FS_BASE, addr);
  690. }
  691. }
  692. put_cpu();
  693. break;
  694. case ARCH_GET_FS: {
  695. unsigned long base;
  696. if (task->thread.fsindex == FS_TLS_SEL)
  697. base = read_32bit_tls(task, FS_TLS);
  698. else if (doit)
  699. rdmsrl(MSR_FS_BASE, base);
  700. else
  701. base = task->thread.fs;
  702. ret = put_user(base, (unsigned long __user *)addr);
  703. break;
  704. }
  705. case ARCH_GET_GS: {
  706. unsigned long base;
  707. if (task->thread.gsindex == GS_TLS_SEL)
  708. base = read_32bit_tls(task, GS_TLS);
  709. else if (doit)
  710. rdmsrl(MSR_KERNEL_GS_BASE, base);
  711. else
  712. base = task->thread.gs;
  713. ret = put_user(base, (unsigned long __user *)addr);
  714. break;
  715. }
  716. default:
  717. ret = -EINVAL;
  718. break;
  719. }
  720. return ret;
  721. }
  722. long sys_arch_prctl(int code, unsigned long addr)
  723. {
  724. return do_arch_prctl(current, code, addr);
  725. }
  726. /*
  727. * Capture the user space registers if the task is not running (in user space)
  728. */
  729. int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
  730. {
  731. struct pt_regs *pp, ptregs;
  732. pp = task_pt_regs(tsk);
  733. ptregs = *pp;
  734. ptregs.cs &= 0xffff;
  735. ptregs.ss &= 0xffff;
  736. elf_core_copy_regs(regs, &ptregs);
  737. return 1;
  738. }
  739. unsigned long arch_align_stack(unsigned long sp)
  740. {
  741. if (randomize_va_space)
  742. sp -= get_random_int() % 8192;
  743. return sp & ~0xf;
  744. }