process.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. #include <linux/errno.h>
  2. #include <linux/kernel.h>
  3. #include <linux/mm.h>
  4. #include <linux/smp.h>
  5. #include <linux/prctl.h>
  6. #include <linux/slab.h>
  7. #include <linux/sched.h>
  8. #include <linux/module.h>
  9. #include <linux/pm.h>
  10. #include <linux/clockchips.h>
  11. #include <linux/random.h>
  12. #include <linux/user-return-notifier.h>
  13. #include <trace/events/power.h>
  14. #include <linux/hw_breakpoint.h>
  15. #include <asm/system.h>
  16. #include <asm/apic.h>
  17. #include <asm/syscalls.h>
  18. #include <asm/idle.h>
  19. #include <asm/uaccess.h>
  20. #include <asm/i387.h>
  21. #include <asm/ds.h>
  22. #include <asm/debugreg.h>
  23. unsigned long idle_halt;
  24. EXPORT_SYMBOL(idle_halt);
  25. unsigned long idle_nomwait;
  26. EXPORT_SYMBOL(idle_nomwait);
  27. struct kmem_cache *task_xstate_cachep;
  28. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  29. {
  30. *dst = *src;
  31. if (src->thread.xstate) {
  32. dst->thread.xstate = kmem_cache_alloc(task_xstate_cachep,
  33. GFP_KERNEL);
  34. if (!dst->thread.xstate)
  35. return -ENOMEM;
  36. WARN_ON((unsigned long)dst->thread.xstate & 15);
  37. memcpy(dst->thread.xstate, src->thread.xstate, xstate_size);
  38. }
  39. return 0;
  40. }
  41. void free_thread_xstate(struct task_struct *tsk)
  42. {
  43. if (tsk->thread.xstate) {
  44. kmem_cache_free(task_xstate_cachep, tsk->thread.xstate);
  45. tsk->thread.xstate = NULL;
  46. }
  47. WARN(tsk->thread.ds_ctx, "leaking DS context\n");
  48. }
  49. void free_thread_info(struct thread_info *ti)
  50. {
  51. free_thread_xstate(ti->task);
  52. free_pages((unsigned long)ti, get_order(THREAD_SIZE));
  53. }
  54. void arch_task_cache_init(void)
  55. {
  56. task_xstate_cachep =
  57. kmem_cache_create("task_xstate", xstate_size,
  58. __alignof__(union thread_xstate),
  59. SLAB_PANIC | SLAB_NOTRACK, NULL);
  60. }
  61. /*
  62. * Free current thread data structures etc..
  63. */
  64. void exit_thread(void)
  65. {
  66. struct task_struct *me = current;
  67. struct thread_struct *t = &me->thread;
  68. unsigned long *bp = t->io_bitmap_ptr;
  69. if (bp) {
  70. struct tss_struct *tss = &per_cpu(init_tss, get_cpu());
  71. t->io_bitmap_ptr = NULL;
  72. clear_thread_flag(TIF_IO_BITMAP);
  73. /*
  74. * Careful, clear this in the TSS too:
  75. */
  76. memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
  77. t->io_bitmap_max = 0;
  78. put_cpu();
  79. kfree(bp);
  80. }
  81. }
  82. void flush_thread(void)
  83. {
  84. struct task_struct *tsk = current;
  85. #ifdef CONFIG_X86_64
  86. if (test_tsk_thread_flag(tsk, TIF_ABI_PENDING)) {
  87. clear_tsk_thread_flag(tsk, TIF_ABI_PENDING);
  88. if (test_tsk_thread_flag(tsk, TIF_IA32)) {
  89. clear_tsk_thread_flag(tsk, TIF_IA32);
  90. } else {
  91. set_tsk_thread_flag(tsk, TIF_IA32);
  92. current_thread_info()->status |= TS_COMPAT;
  93. }
  94. }
  95. #endif
  96. flush_ptrace_hw_breakpoint(tsk);
  97. memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
  98. /*
  99. * Forget coprocessor state..
  100. */
  101. tsk->fpu_counter = 0;
  102. clear_fpu(tsk);
  103. clear_used_math();
  104. }
  105. static void hard_disable_TSC(void)
  106. {
  107. write_cr4(read_cr4() | X86_CR4_TSD);
  108. }
  109. void disable_TSC(void)
  110. {
  111. preempt_disable();
  112. if (!test_and_set_thread_flag(TIF_NOTSC))
  113. /*
  114. * Must flip the CPU state synchronously with
  115. * TIF_NOTSC in the current running context.
  116. */
  117. hard_disable_TSC();
  118. preempt_enable();
  119. }
  120. static void hard_enable_TSC(void)
  121. {
  122. write_cr4(read_cr4() & ~X86_CR4_TSD);
  123. }
  124. static void enable_TSC(void)
  125. {
  126. preempt_disable();
  127. if (test_and_clear_thread_flag(TIF_NOTSC))
  128. /*
  129. * Must flip the CPU state synchronously with
  130. * TIF_NOTSC in the current running context.
  131. */
  132. hard_enable_TSC();
  133. preempt_enable();
  134. }
  135. int get_tsc_mode(unsigned long adr)
  136. {
  137. unsigned int val;
  138. if (test_thread_flag(TIF_NOTSC))
  139. val = PR_TSC_SIGSEGV;
  140. else
  141. val = PR_TSC_ENABLE;
  142. return put_user(val, (unsigned int __user *)adr);
  143. }
  144. int set_tsc_mode(unsigned int val)
  145. {
  146. if (val == PR_TSC_SIGSEGV)
  147. disable_TSC();
  148. else if (val == PR_TSC_ENABLE)
  149. enable_TSC();
  150. else
  151. return -EINVAL;
  152. return 0;
  153. }
  154. void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
  155. struct tss_struct *tss)
  156. {
  157. struct thread_struct *prev, *next;
  158. prev = &prev_p->thread;
  159. next = &next_p->thread;
  160. if (test_tsk_thread_flag(next_p, TIF_DS_AREA_MSR) ||
  161. test_tsk_thread_flag(prev_p, TIF_DS_AREA_MSR))
  162. ds_switch_to(prev_p, next_p);
  163. else if (next->debugctlmsr != prev->debugctlmsr)
  164. update_debugctlmsr(next->debugctlmsr);
  165. if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
  166. test_tsk_thread_flag(next_p, TIF_NOTSC)) {
  167. /* prev and next are different */
  168. if (test_tsk_thread_flag(next_p, TIF_NOTSC))
  169. hard_disable_TSC();
  170. else
  171. hard_enable_TSC();
  172. }
  173. if (test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
  174. /*
  175. * Copy the relevant range of the IO bitmap.
  176. * Normally this is 128 bytes or less:
  177. */
  178. memcpy(tss->io_bitmap, next->io_bitmap_ptr,
  179. max(prev->io_bitmap_max, next->io_bitmap_max));
  180. } else if (test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)) {
  181. /*
  182. * Clear any possible leftover bits:
  183. */
  184. memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);
  185. }
  186. propagate_user_return_notify(prev_p, next_p);
  187. }
  188. int sys_fork(struct pt_regs *regs)
  189. {
  190. return do_fork(SIGCHLD, regs->sp, regs, 0, NULL, NULL);
  191. }
  192. /*
  193. * This is trivial, and on the face of it looks like it
  194. * could equally well be done in user mode.
  195. *
  196. * Not so, for quite unobvious reasons - register pressure.
  197. * In user mode vfork() cannot have a stack frame, and if
  198. * done by calling the "clone()" system call directly, you
  199. * do not have enough call-clobbered registers to hold all
  200. * the information you need.
  201. */
  202. int sys_vfork(struct pt_regs *regs)
  203. {
  204. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->sp, regs, 0,
  205. NULL, NULL);
  206. }
  207. long
  208. sys_clone(unsigned long clone_flags, unsigned long newsp,
  209. void __user *parent_tid, void __user *child_tid, struct pt_regs *regs)
  210. {
  211. if (!newsp)
  212. newsp = regs->sp;
  213. return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
  214. }
  215. /*
  216. * sys_execve() executes a new program.
  217. */
  218. long sys_execve(char __user *name, char __user * __user *argv,
  219. char __user * __user *envp, struct pt_regs *regs)
  220. {
  221. long error;
  222. char *filename;
  223. filename = getname(name);
  224. error = PTR_ERR(filename);
  225. if (IS_ERR(filename))
  226. return error;
  227. error = do_execve(filename, argv, envp, regs);
  228. #ifdef CONFIG_X86_32
  229. if (error == 0) {
  230. /* Make sure we don't return using sysenter.. */
  231. set_thread_flag(TIF_IRET);
  232. }
  233. #endif
  234. putname(filename);
  235. return error;
  236. }
  237. /*
  238. * Idle related variables and functions
  239. */
  240. unsigned long boot_option_idle_override = 0;
  241. EXPORT_SYMBOL(boot_option_idle_override);
  242. /*
  243. * Powermanagement idle function, if any..
  244. */
  245. void (*pm_idle)(void);
  246. EXPORT_SYMBOL(pm_idle);
  247. #ifdef CONFIG_X86_32
  248. /*
  249. * This halt magic was a workaround for ancient floppy DMA
  250. * wreckage. It should be safe to remove.
  251. */
  252. static int hlt_counter;
  253. void disable_hlt(void)
  254. {
  255. hlt_counter++;
  256. }
  257. EXPORT_SYMBOL(disable_hlt);
  258. void enable_hlt(void)
  259. {
  260. hlt_counter--;
  261. }
  262. EXPORT_SYMBOL(enable_hlt);
  263. static inline int hlt_use_halt(void)
  264. {
  265. return (!hlt_counter && boot_cpu_data.hlt_works_ok);
  266. }
  267. #else
  268. static inline int hlt_use_halt(void)
  269. {
  270. return 1;
  271. }
  272. #endif
  273. /*
  274. * We use this if we don't have any better
  275. * idle routine..
  276. */
  277. void default_idle(void)
  278. {
  279. if (hlt_use_halt()) {
  280. trace_power_start(POWER_CSTATE, 1);
  281. current_thread_info()->status &= ~TS_POLLING;
  282. /*
  283. * TS_POLLING-cleared state must be visible before we
  284. * test NEED_RESCHED:
  285. */
  286. smp_mb();
  287. if (!need_resched())
  288. safe_halt(); /* enables interrupts racelessly */
  289. else
  290. local_irq_enable();
  291. current_thread_info()->status |= TS_POLLING;
  292. } else {
  293. local_irq_enable();
  294. /* loop is done by the caller */
  295. cpu_relax();
  296. }
  297. }
  298. #ifdef CONFIG_APM_MODULE
  299. EXPORT_SYMBOL(default_idle);
  300. #endif
  301. void stop_this_cpu(void *dummy)
  302. {
  303. local_irq_disable();
  304. /*
  305. * Remove this CPU:
  306. */
  307. set_cpu_online(smp_processor_id(), false);
  308. disable_local_APIC();
  309. for (;;) {
  310. if (hlt_works(smp_processor_id()))
  311. halt();
  312. }
  313. }
  314. static void do_nothing(void *unused)
  315. {
  316. }
  317. /*
  318. * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
  319. * pm_idle and update to new pm_idle value. Required while changing pm_idle
  320. * handler on SMP systems.
  321. *
  322. * Caller must have changed pm_idle to the new value before the call. Old
  323. * pm_idle value will not be used by any CPU after the return of this function.
  324. */
  325. void cpu_idle_wait(void)
  326. {
  327. smp_mb();
  328. /* kick all the CPUs so that they exit out of pm_idle */
  329. smp_call_function(do_nothing, NULL, 1);
  330. }
  331. EXPORT_SYMBOL_GPL(cpu_idle_wait);
  332. /*
  333. * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
  334. * which can obviate IPI to trigger checking of need_resched.
  335. * We execute MONITOR against need_resched and enter optimized wait state
  336. * through MWAIT. Whenever someone changes need_resched, we would be woken
  337. * up from MWAIT (without an IPI).
  338. *
  339. * New with Core Duo processors, MWAIT can take some hints based on CPU
  340. * capability.
  341. */
  342. void mwait_idle_with_hints(unsigned long ax, unsigned long cx)
  343. {
  344. trace_power_start(POWER_CSTATE, (ax>>4)+1);
  345. if (!need_resched()) {
  346. if (cpu_has(&current_cpu_data, X86_FEATURE_CLFLUSH_MONITOR))
  347. clflush((void *)&current_thread_info()->flags);
  348. __monitor((void *)&current_thread_info()->flags, 0, 0);
  349. smp_mb();
  350. if (!need_resched())
  351. __mwait(ax, cx);
  352. }
  353. }
  354. /* Default MONITOR/MWAIT with no hints, used for default C1 state */
  355. static void mwait_idle(void)
  356. {
  357. if (!need_resched()) {
  358. trace_power_start(POWER_CSTATE, 1);
  359. if (cpu_has(&current_cpu_data, X86_FEATURE_CLFLUSH_MONITOR))
  360. clflush((void *)&current_thread_info()->flags);
  361. __monitor((void *)&current_thread_info()->flags, 0, 0);
  362. smp_mb();
  363. if (!need_resched())
  364. __sti_mwait(0, 0);
  365. else
  366. local_irq_enable();
  367. } else
  368. local_irq_enable();
  369. }
  370. /*
  371. * On SMP it's slightly faster (but much more power-consuming!)
  372. * to poll the ->work.need_resched flag instead of waiting for the
  373. * cross-CPU IPI to arrive. Use this option with caution.
  374. */
  375. static void poll_idle(void)
  376. {
  377. trace_power_start(POWER_CSTATE, 0);
  378. local_irq_enable();
  379. while (!need_resched())
  380. cpu_relax();
  381. trace_power_end(0);
  382. }
  383. /*
  384. * mwait selection logic:
  385. *
  386. * It depends on the CPU. For AMD CPUs that support MWAIT this is
  387. * wrong. Family 0x10 and 0x11 CPUs will enter C1 on HLT. Powersavings
  388. * then depend on a clock divisor and current Pstate of the core. If
  389. * all cores of a processor are in halt state (C1) the processor can
  390. * enter the C1E (C1 enhanced) state. If mwait is used this will never
  391. * happen.
  392. *
  393. * idle=mwait overrides this decision and forces the usage of mwait.
  394. */
  395. static int __cpuinitdata force_mwait;
  396. #define MWAIT_INFO 0x05
  397. #define MWAIT_ECX_EXTENDED_INFO 0x01
  398. #define MWAIT_EDX_C1 0xf0
  399. static int __cpuinit mwait_usable(const struct cpuinfo_x86 *c)
  400. {
  401. u32 eax, ebx, ecx, edx;
  402. if (force_mwait)
  403. return 1;
  404. if (c->cpuid_level < MWAIT_INFO)
  405. return 0;
  406. cpuid(MWAIT_INFO, &eax, &ebx, &ecx, &edx);
  407. /* Check, whether EDX has extended info about MWAIT */
  408. if (!(ecx & MWAIT_ECX_EXTENDED_INFO))
  409. return 1;
  410. /*
  411. * edx enumeratios MONITOR/MWAIT extensions. Check, whether
  412. * C1 supports MWAIT
  413. */
  414. return (edx & MWAIT_EDX_C1);
  415. }
  416. /*
  417. * Check for AMD CPUs, which have potentially C1E support
  418. */
  419. static int __cpuinit check_c1e_idle(const struct cpuinfo_x86 *c)
  420. {
  421. if (c->x86_vendor != X86_VENDOR_AMD)
  422. return 0;
  423. if (c->x86 < 0x0F)
  424. return 0;
  425. /* Family 0x0f models < rev F do not have C1E */
  426. if (c->x86 == 0x0f && c->x86_model < 0x40)
  427. return 0;
  428. return 1;
  429. }
  430. static cpumask_var_t c1e_mask;
  431. static int c1e_detected;
  432. void c1e_remove_cpu(int cpu)
  433. {
  434. if (c1e_mask != NULL)
  435. cpumask_clear_cpu(cpu, c1e_mask);
  436. }
  437. /*
  438. * C1E aware idle routine. We check for C1E active in the interrupt
  439. * pending message MSR. If we detect C1E, then we handle it the same
  440. * way as C3 power states (local apic timer and TSC stop)
  441. */
  442. static void c1e_idle(void)
  443. {
  444. if (need_resched())
  445. return;
  446. if (!c1e_detected) {
  447. u32 lo, hi;
  448. rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
  449. if (lo & K8_INTP_C1E_ACTIVE_MASK) {
  450. c1e_detected = 1;
  451. if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  452. mark_tsc_unstable("TSC halt in AMD C1E");
  453. printk(KERN_INFO "System has AMD C1E enabled\n");
  454. set_cpu_cap(&boot_cpu_data, X86_FEATURE_AMDC1E);
  455. }
  456. }
  457. if (c1e_detected) {
  458. int cpu = smp_processor_id();
  459. if (!cpumask_test_cpu(cpu, c1e_mask)) {
  460. cpumask_set_cpu(cpu, c1e_mask);
  461. /*
  462. * Force broadcast so ACPI can not interfere.
  463. */
  464. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
  465. &cpu);
  466. printk(KERN_INFO "Switch to broadcast mode on CPU%d\n",
  467. cpu);
  468. }
  469. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
  470. default_idle();
  471. /*
  472. * The switch back from broadcast mode needs to be
  473. * called with interrupts disabled.
  474. */
  475. local_irq_disable();
  476. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
  477. local_irq_enable();
  478. } else
  479. default_idle();
  480. }
  481. void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
  482. {
  483. #ifdef CONFIG_SMP
  484. if (pm_idle == poll_idle && smp_num_siblings > 1) {
  485. printk(KERN_WARNING "WARNING: polling idle and HT enabled,"
  486. " performance may degrade.\n");
  487. }
  488. #endif
  489. if (pm_idle)
  490. return;
  491. if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) {
  492. /*
  493. * One CPU supports mwait => All CPUs supports mwait
  494. */
  495. printk(KERN_INFO "using mwait in idle threads.\n");
  496. pm_idle = mwait_idle;
  497. } else if (check_c1e_idle(c)) {
  498. printk(KERN_INFO "using C1E aware idle routine\n");
  499. pm_idle = c1e_idle;
  500. } else
  501. pm_idle = default_idle;
  502. }
  503. void __init init_c1e_mask(void)
  504. {
  505. /* If we're using c1e_idle, we need to allocate c1e_mask. */
  506. if (pm_idle == c1e_idle)
  507. zalloc_cpumask_var(&c1e_mask, GFP_KERNEL);
  508. }
  509. static int __init idle_setup(char *str)
  510. {
  511. if (!str)
  512. return -EINVAL;
  513. if (!strcmp(str, "poll")) {
  514. printk("using polling idle threads.\n");
  515. pm_idle = poll_idle;
  516. } else if (!strcmp(str, "mwait"))
  517. force_mwait = 1;
  518. else if (!strcmp(str, "halt")) {
  519. /*
  520. * When the boot option of idle=halt is added, halt is
  521. * forced to be used for CPU idle. In such case CPU C2/C3
  522. * won't be used again.
  523. * To continue to load the CPU idle driver, don't touch
  524. * the boot_option_idle_override.
  525. */
  526. pm_idle = default_idle;
  527. idle_halt = 1;
  528. return 0;
  529. } else if (!strcmp(str, "nomwait")) {
  530. /*
  531. * If the boot option of "idle=nomwait" is added,
  532. * it means that mwait will be disabled for CPU C2/C3
  533. * states. In such case it won't touch the variable
  534. * of boot_option_idle_override.
  535. */
  536. idle_nomwait = 1;
  537. return 0;
  538. } else
  539. return -1;
  540. boot_option_idle_override = 1;
  541. return 0;
  542. }
  543. early_param("idle", idle_setup);
  544. unsigned long arch_align_stack(unsigned long sp)
  545. {
  546. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  547. sp -= get_random_int() % 8192;
  548. return sp & ~0xf;
  549. }
  550. unsigned long arch_randomize_brk(struct mm_struct *mm)
  551. {
  552. unsigned long range_end = mm->brk + 0x02000000;
  553. return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
  554. }