process.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  2. #include <linux/errno.h>
  3. #include <linux/kernel.h>
  4. #include <linux/mm.h>
  5. #include <linux/smp.h>
  6. #include <linux/prctl.h>
  7. #include <linux/slab.h>
  8. #include <linux/sched.h>
  9. #include <linux/module.h>
  10. #include <linux/pm.h>
  11. #include <linux/clockchips.h>
  12. #include <linux/random.h>
  13. #include <linux/user-return-notifier.h>
  14. #include <linux/dmi.h>
  15. #include <linux/utsname.h>
  16. #include <linux/stackprotector.h>
  17. #include <linux/tick.h>
  18. #include <linux/cpuidle.h>
  19. #include <trace/events/power.h>
  20. #include <linux/hw_breakpoint.h>
  21. #include <asm/cpu.h>
  22. #include <asm/apic.h>
  23. #include <asm/syscalls.h>
  24. #include <asm/idle.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/mwait.h>
  27. #include <asm/i387.h>
  28. #include <asm/fpu-internal.h>
  29. #include <asm/debugreg.h>
  30. #include <asm/nmi.h>
  31. #include <asm/tlbflush.h>
  32. /*
  33. * per-CPU TSS segments. Threads are completely 'soft' on Linux,
  34. * no more per-task TSS's. The TSS size is kept cacheline-aligned
  35. * so they are allowed to end up in the .data..cacheline_aligned
  36. * section. Since TSS's are completely CPU-local, we want them
  37. * on exact cacheline boundaries, to eliminate cacheline ping-pong.
  38. */
  39. __visible DEFINE_PER_CPU_SHARED_ALIGNED(struct tss_struct, init_tss) = INIT_TSS;
  40. #ifdef CONFIG_X86_64
  41. static DEFINE_PER_CPU(unsigned char, is_idle);
  42. static ATOMIC_NOTIFIER_HEAD(idle_notifier);
  43. void idle_notifier_register(struct notifier_block *n)
  44. {
  45. atomic_notifier_chain_register(&idle_notifier, n);
  46. }
  47. EXPORT_SYMBOL_GPL(idle_notifier_register);
  48. void idle_notifier_unregister(struct notifier_block *n)
  49. {
  50. atomic_notifier_chain_unregister(&idle_notifier, n);
  51. }
  52. EXPORT_SYMBOL_GPL(idle_notifier_unregister);
  53. #endif
  54. struct kmem_cache *task_xstate_cachep;
  55. EXPORT_SYMBOL_GPL(task_xstate_cachep);
  56. /*
  57. * this gets called so that we can store lazy state into memory and copy the
  58. * current task into the new thread.
  59. */
  60. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  61. {
  62. *dst = *src;
  63. dst->thread.fpu_counter = 0;
  64. dst->thread.fpu.has_fpu = 0;
  65. dst->thread.fpu.last_cpu = ~0;
  66. dst->thread.fpu.state = NULL;
  67. if (tsk_used_math(src)) {
  68. int err = fpu_alloc(&dst->thread.fpu);
  69. if (err)
  70. return err;
  71. fpu_copy(dst, src);
  72. }
  73. return 0;
  74. }
  75. void free_thread_xstate(struct task_struct *tsk)
  76. {
  77. fpu_free(&tsk->thread.fpu);
  78. }
  79. void arch_release_task_struct(struct task_struct *tsk)
  80. {
  81. free_thread_xstate(tsk);
  82. }
  83. void arch_task_cache_init(void)
  84. {
  85. task_xstate_cachep =
  86. kmem_cache_create("task_xstate", xstate_size,
  87. __alignof__(union thread_xstate),
  88. SLAB_PANIC | SLAB_NOTRACK, NULL);
  89. setup_xstate_comp();
  90. }
  91. /*
  92. * Free current thread data structures etc..
  93. */
  94. void exit_thread(void)
  95. {
  96. struct task_struct *me = current;
  97. struct thread_struct *t = &me->thread;
  98. unsigned long *bp = t->io_bitmap_ptr;
  99. if (bp) {
  100. struct tss_struct *tss = &per_cpu(init_tss, get_cpu());
  101. t->io_bitmap_ptr = NULL;
  102. clear_thread_flag(TIF_IO_BITMAP);
  103. /*
  104. * Careful, clear this in the TSS too:
  105. */
  106. memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
  107. t->io_bitmap_max = 0;
  108. put_cpu();
  109. kfree(bp);
  110. }
  111. drop_fpu(me);
  112. }
  113. void flush_thread(void)
  114. {
  115. struct task_struct *tsk = current;
  116. flush_ptrace_hw_breakpoint(tsk);
  117. memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
  118. drop_init_fpu(tsk);
  119. /*
  120. * Free the FPU state for non xsave platforms. They get reallocated
  121. * lazily at the first use.
  122. */
  123. if (!use_eager_fpu())
  124. free_thread_xstate(tsk);
  125. }
  126. static void hard_disable_TSC(void)
  127. {
  128. cr4_set_bits(X86_CR4_TSD);
  129. }
  130. void disable_TSC(void)
  131. {
  132. preempt_disable();
  133. if (!test_and_set_thread_flag(TIF_NOTSC))
  134. /*
  135. * Must flip the CPU state synchronously with
  136. * TIF_NOTSC in the current running context.
  137. */
  138. hard_disable_TSC();
  139. preempt_enable();
  140. }
  141. static void hard_enable_TSC(void)
  142. {
  143. cr4_clear_bits(X86_CR4_TSD);
  144. }
  145. static void enable_TSC(void)
  146. {
  147. preempt_disable();
  148. if (test_and_clear_thread_flag(TIF_NOTSC))
  149. /*
  150. * Must flip the CPU state synchronously with
  151. * TIF_NOTSC in the current running context.
  152. */
  153. hard_enable_TSC();
  154. preempt_enable();
  155. }
  156. int get_tsc_mode(unsigned long adr)
  157. {
  158. unsigned int val;
  159. if (test_thread_flag(TIF_NOTSC))
  160. val = PR_TSC_SIGSEGV;
  161. else
  162. val = PR_TSC_ENABLE;
  163. return put_user(val, (unsigned int __user *)adr);
  164. }
  165. int set_tsc_mode(unsigned int val)
  166. {
  167. if (val == PR_TSC_SIGSEGV)
  168. disable_TSC();
  169. else if (val == PR_TSC_ENABLE)
  170. enable_TSC();
  171. else
  172. return -EINVAL;
  173. return 0;
  174. }
  175. void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
  176. struct tss_struct *tss)
  177. {
  178. struct thread_struct *prev, *next;
  179. prev = &prev_p->thread;
  180. next = &next_p->thread;
  181. if (test_tsk_thread_flag(prev_p, TIF_BLOCKSTEP) ^
  182. test_tsk_thread_flag(next_p, TIF_BLOCKSTEP)) {
  183. unsigned long debugctl = get_debugctlmsr();
  184. debugctl &= ~DEBUGCTLMSR_BTF;
  185. if (test_tsk_thread_flag(next_p, TIF_BLOCKSTEP))
  186. debugctl |= DEBUGCTLMSR_BTF;
  187. update_debugctlmsr(debugctl);
  188. }
  189. if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
  190. test_tsk_thread_flag(next_p, TIF_NOTSC)) {
  191. /* prev and next are different */
  192. if (test_tsk_thread_flag(next_p, TIF_NOTSC))
  193. hard_disable_TSC();
  194. else
  195. hard_enable_TSC();
  196. }
  197. if (test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
  198. /*
  199. * Copy the relevant range of the IO bitmap.
  200. * Normally this is 128 bytes or less:
  201. */
  202. memcpy(tss->io_bitmap, next->io_bitmap_ptr,
  203. max(prev->io_bitmap_max, next->io_bitmap_max));
  204. } else if (test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)) {
  205. /*
  206. * Clear any possible leftover bits:
  207. */
  208. memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);
  209. }
  210. propagate_user_return_notify(prev_p, next_p);
  211. }
  212. /*
  213. * Idle related variables and functions
  214. */
  215. unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE;
  216. EXPORT_SYMBOL(boot_option_idle_override);
  217. static void (*x86_idle)(void);
  218. #ifndef CONFIG_SMP
  219. static inline void play_dead(void)
  220. {
  221. BUG();
  222. }
  223. #endif
  224. #ifdef CONFIG_X86_64
  225. void enter_idle(void)
  226. {
  227. this_cpu_write(is_idle, 1);
  228. atomic_notifier_call_chain(&idle_notifier, IDLE_START, NULL);
  229. }
  230. static void __exit_idle(void)
  231. {
  232. if (x86_test_and_clear_bit_percpu(0, is_idle) == 0)
  233. return;
  234. atomic_notifier_call_chain(&idle_notifier, IDLE_END, NULL);
  235. }
  236. /* Called from interrupts to signify idle end */
  237. void exit_idle(void)
  238. {
  239. /* idle loop has pid 0 */
  240. if (current->pid)
  241. return;
  242. __exit_idle();
  243. }
  244. #endif
  245. void arch_cpu_idle_enter(void)
  246. {
  247. local_touch_nmi();
  248. enter_idle();
  249. }
  250. void arch_cpu_idle_exit(void)
  251. {
  252. __exit_idle();
  253. }
  254. void arch_cpu_idle_dead(void)
  255. {
  256. play_dead();
  257. }
  258. /*
  259. * Called from the generic idle code.
  260. */
  261. void arch_cpu_idle(void)
  262. {
  263. x86_idle();
  264. }
  265. /*
  266. * We use this if we don't have any better idle routine..
  267. */
  268. void default_idle(void)
  269. {
  270. trace_cpu_idle_rcuidle(1, smp_processor_id());
  271. safe_halt();
  272. trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
  273. }
  274. #ifdef CONFIG_APM_MODULE
  275. EXPORT_SYMBOL(default_idle);
  276. #endif
  277. #ifdef CONFIG_XEN
  278. bool xen_set_default_idle(void)
  279. {
  280. bool ret = !!x86_idle;
  281. x86_idle = default_idle;
  282. return ret;
  283. }
  284. #endif
  285. void stop_this_cpu(void *dummy)
  286. {
  287. local_irq_disable();
  288. /*
  289. * Remove this CPU:
  290. */
  291. set_cpu_online(smp_processor_id(), false);
  292. disable_local_APIC();
  293. for (;;)
  294. halt();
  295. }
  296. bool amd_e400_c1e_detected;
  297. EXPORT_SYMBOL(amd_e400_c1e_detected);
  298. static cpumask_var_t amd_e400_c1e_mask;
  299. void amd_e400_remove_cpu(int cpu)
  300. {
  301. if (amd_e400_c1e_mask != NULL)
  302. cpumask_clear_cpu(cpu, amd_e400_c1e_mask);
  303. }
  304. /*
  305. * AMD Erratum 400 aware idle routine. We check for C1E active in the interrupt
  306. * pending message MSR. If we detect C1E, then we handle it the same
  307. * way as C3 power states (local apic timer and TSC stop)
  308. */
  309. static void amd_e400_idle(void)
  310. {
  311. if (!amd_e400_c1e_detected) {
  312. u32 lo, hi;
  313. rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
  314. if (lo & K8_INTP_C1E_ACTIVE_MASK) {
  315. amd_e400_c1e_detected = true;
  316. if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  317. mark_tsc_unstable("TSC halt in AMD C1E");
  318. pr_info("System has AMD C1E enabled\n");
  319. }
  320. }
  321. if (amd_e400_c1e_detected) {
  322. int cpu = smp_processor_id();
  323. if (!cpumask_test_cpu(cpu, amd_e400_c1e_mask)) {
  324. cpumask_set_cpu(cpu, amd_e400_c1e_mask);
  325. /*
  326. * Force broadcast so ACPI can not interfere.
  327. */
  328. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
  329. &cpu);
  330. pr_info("Switch to broadcast mode on CPU%d\n", cpu);
  331. }
  332. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
  333. default_idle();
  334. /*
  335. * The switch back from broadcast mode needs to be
  336. * called with interrupts disabled.
  337. */
  338. local_irq_disable();
  339. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
  340. local_irq_enable();
  341. } else
  342. default_idle();
  343. }
  344. /*
  345. * Intel Core2 and older machines prefer MWAIT over HALT for C1.
  346. * We can't rely on cpuidle installing MWAIT, because it will not load
  347. * on systems that support only C1 -- so the boot default must be MWAIT.
  348. *
  349. * Some AMD machines are the opposite, they depend on using HALT.
  350. *
  351. * So for default C1, which is used during boot until cpuidle loads,
  352. * use MWAIT-C1 on Intel HW that has it, else use HALT.
  353. */
  354. static int prefer_mwait_c1_over_halt(const struct cpuinfo_x86 *c)
  355. {
  356. if (c->x86_vendor != X86_VENDOR_INTEL)
  357. return 0;
  358. if (!cpu_has(c, X86_FEATURE_MWAIT))
  359. return 0;
  360. return 1;
  361. }
  362. /*
  363. * MONITOR/MWAIT with no hints, used for default default C1 state.
  364. * This invokes MWAIT with interrutps enabled and no flags,
  365. * which is backwards compatible with the original MWAIT implementation.
  366. */
  367. static void mwait_idle(void)
  368. {
  369. if (!current_set_polling_and_test()) {
  370. if (this_cpu_has(X86_BUG_CLFLUSH_MONITOR)) {
  371. smp_mb(); /* quirk */
  372. clflush((void *)&current_thread_info()->flags);
  373. smp_mb(); /* quirk */
  374. }
  375. __monitor((void *)&current_thread_info()->flags, 0, 0);
  376. if (!need_resched())
  377. __sti_mwait(0, 0);
  378. else
  379. local_irq_enable();
  380. } else {
  381. local_irq_enable();
  382. }
  383. __current_clr_polling();
  384. }
  385. void select_idle_routine(const struct cpuinfo_x86 *c)
  386. {
  387. #ifdef CONFIG_SMP
  388. if (boot_option_idle_override == IDLE_POLL && smp_num_siblings > 1)
  389. pr_warn_once("WARNING: polling idle and HT enabled, performance may degrade\n");
  390. #endif
  391. if (x86_idle || boot_option_idle_override == IDLE_POLL)
  392. return;
  393. if (cpu_has_bug(c, X86_BUG_AMD_APIC_C1E)) {
  394. /* E400: APIC timer interrupt does not wake up CPU from C1e */
  395. pr_info("using AMD E400 aware idle routine\n");
  396. x86_idle = amd_e400_idle;
  397. } else if (prefer_mwait_c1_over_halt(c)) {
  398. pr_info("using mwait in idle threads\n");
  399. x86_idle = mwait_idle;
  400. } else
  401. x86_idle = default_idle;
  402. }
  403. void __init init_amd_e400_c1e_mask(void)
  404. {
  405. /* If we're using amd_e400_idle, we need to allocate amd_e400_c1e_mask. */
  406. if (x86_idle == amd_e400_idle)
  407. zalloc_cpumask_var(&amd_e400_c1e_mask, GFP_KERNEL);
  408. }
  409. static int __init idle_setup(char *str)
  410. {
  411. if (!str)
  412. return -EINVAL;
  413. if (!strcmp(str, "poll")) {
  414. pr_info("using polling idle threads\n");
  415. boot_option_idle_override = IDLE_POLL;
  416. cpu_idle_poll_ctrl(true);
  417. } else if (!strcmp(str, "halt")) {
  418. /*
  419. * When the boot option of idle=halt is added, halt is
  420. * forced to be used for CPU idle. In such case CPU C2/C3
  421. * won't be used again.
  422. * To continue to load the CPU idle driver, don't touch
  423. * the boot_option_idle_override.
  424. */
  425. x86_idle = default_idle;
  426. boot_option_idle_override = IDLE_HALT;
  427. } else if (!strcmp(str, "nomwait")) {
  428. /*
  429. * If the boot option of "idle=nomwait" is added,
  430. * it means that mwait will be disabled for CPU C2/C3
  431. * states. In such case it won't touch the variable
  432. * of boot_option_idle_override.
  433. */
  434. boot_option_idle_override = IDLE_NOMWAIT;
  435. } else
  436. return -1;
  437. return 0;
  438. }
  439. early_param("idle", idle_setup);
  440. unsigned long arch_align_stack(unsigned long sp)
  441. {
  442. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  443. sp -= get_random_int() % 8192;
  444. return sp & ~0xf;
  445. }
  446. unsigned long arch_randomize_brk(struct mm_struct *mm)
  447. {
  448. unsigned long range_end = mm->brk + 0x02000000;
  449. return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
  450. }