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