process.c 12 KB

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