process.c 19 KB

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