process.c 11 KB

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