process.c 12 KB

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