process.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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/i387.h>
  27. #include <asm/fpu-internal.h>
  28. #include <asm/debugreg.h>
  29. #include <asm/nmi.h>
  30. /*
  31. * per-CPU TSS segments. Threads are completely 'soft' on Linux,
  32. * no more per-task TSS's. The TSS size is kept cacheline-aligned
  33. * so they are allowed to end up in the .data..cacheline_aligned
  34. * section. Since TSS's are completely CPU-local, we want them
  35. * on exact cacheline boundaries, to eliminate cacheline ping-pong.
  36. */
  37. DEFINE_PER_CPU_SHARED_ALIGNED(struct tss_struct, init_tss) = INIT_TSS;
  38. #ifdef CONFIG_X86_64
  39. static DEFINE_PER_CPU(unsigned char, is_idle);
  40. static ATOMIC_NOTIFIER_HEAD(idle_notifier);
  41. void idle_notifier_register(struct notifier_block *n)
  42. {
  43. atomic_notifier_chain_register(&idle_notifier, n);
  44. }
  45. EXPORT_SYMBOL_GPL(idle_notifier_register);
  46. void idle_notifier_unregister(struct notifier_block *n)
  47. {
  48. atomic_notifier_chain_unregister(&idle_notifier, n);
  49. }
  50. EXPORT_SYMBOL_GPL(idle_notifier_unregister);
  51. #endif
  52. struct kmem_cache *task_xstate_cachep;
  53. EXPORT_SYMBOL_GPL(task_xstate_cachep);
  54. /*
  55. * this gets called so that we can store lazy state into memory and copy the
  56. * current task into the new thread.
  57. */
  58. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  59. {
  60. int ret;
  61. *dst = *src;
  62. if (fpu_allocated(&src->thread.fpu)) {
  63. memset(&dst->thread.fpu, 0, sizeof(dst->thread.fpu));
  64. ret = fpu_alloc(&dst->thread.fpu);
  65. if (ret)
  66. return ret;
  67. fpu_copy(dst, src);
  68. }
  69. return 0;
  70. }
  71. void free_thread_xstate(struct task_struct *tsk)
  72. {
  73. fpu_free(&tsk->thread.fpu);
  74. }
  75. void arch_release_task_struct(struct task_struct *tsk)
  76. {
  77. free_thread_xstate(tsk);
  78. }
  79. void arch_task_cache_init(void)
  80. {
  81. task_xstate_cachep =
  82. kmem_cache_create("task_xstate", xstate_size,
  83. __alignof__(union thread_xstate),
  84. SLAB_PANIC | SLAB_NOTRACK, NULL);
  85. }
  86. /*
  87. * Free current thread data structures etc..
  88. */
  89. void exit_thread(void)
  90. {
  91. struct task_struct *me = current;
  92. struct thread_struct *t = &me->thread;
  93. unsigned long *bp = t->io_bitmap_ptr;
  94. if (bp) {
  95. struct tss_struct *tss = &per_cpu(init_tss, get_cpu());
  96. t->io_bitmap_ptr = NULL;
  97. clear_thread_flag(TIF_IO_BITMAP);
  98. /*
  99. * Careful, clear this in the TSS too:
  100. */
  101. memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
  102. t->io_bitmap_max = 0;
  103. put_cpu();
  104. kfree(bp);
  105. }
  106. drop_fpu(me);
  107. }
  108. void show_regs_common(void)
  109. {
  110. const char *vendor, *product, *board;
  111. vendor = dmi_get_system_info(DMI_SYS_VENDOR);
  112. if (!vendor)
  113. vendor = "";
  114. product = dmi_get_system_info(DMI_PRODUCT_NAME);
  115. if (!product)
  116. product = "";
  117. /* Board Name is optional */
  118. board = dmi_get_system_info(DMI_BOARD_NAME);
  119. printk(KERN_DEFAULT "Pid: %d, comm: %.20s %s %s %.*s %s %s%s%s\n",
  120. current->pid, current->comm, print_tainted(),
  121. init_utsname()->release,
  122. (int)strcspn(init_utsname()->version, " "),
  123. init_utsname()->version,
  124. vendor, product,
  125. board ? "/" : "",
  126. board ? board : "");
  127. }
  128. void flush_thread(void)
  129. {
  130. struct task_struct *tsk = current;
  131. flush_ptrace_hw_breakpoint(tsk);
  132. memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
  133. drop_init_fpu(tsk);
  134. /*
  135. * Free the FPU state for non xsave platforms. They get reallocated
  136. * lazily at the first use.
  137. */
  138. if (!use_eager_fpu())
  139. free_thread_xstate(tsk);
  140. }
  141. static void hard_disable_TSC(void)
  142. {
  143. write_cr4(read_cr4() | X86_CR4_TSD);
  144. }
  145. void disable_TSC(void)
  146. {
  147. preempt_disable();
  148. if (!test_and_set_thread_flag(TIF_NOTSC))
  149. /*
  150. * Must flip the CPU state synchronously with
  151. * TIF_NOTSC in the current running context.
  152. */
  153. hard_disable_TSC();
  154. preempt_enable();
  155. }
  156. static void hard_enable_TSC(void)
  157. {
  158. write_cr4(read_cr4() & ~X86_CR4_TSD);
  159. }
  160. static void enable_TSC(void)
  161. {
  162. preempt_disable();
  163. if (test_and_clear_thread_flag(TIF_NOTSC))
  164. /*
  165. * Must flip the CPU state synchronously with
  166. * TIF_NOTSC in the current running context.
  167. */
  168. hard_enable_TSC();
  169. preempt_enable();
  170. }
  171. int get_tsc_mode(unsigned long adr)
  172. {
  173. unsigned int val;
  174. if (test_thread_flag(TIF_NOTSC))
  175. val = PR_TSC_SIGSEGV;
  176. else
  177. val = PR_TSC_ENABLE;
  178. return put_user(val, (unsigned int __user *)adr);
  179. }
  180. int set_tsc_mode(unsigned int val)
  181. {
  182. if (val == PR_TSC_SIGSEGV)
  183. disable_TSC();
  184. else if (val == PR_TSC_ENABLE)
  185. enable_TSC();
  186. else
  187. return -EINVAL;
  188. return 0;
  189. }
  190. void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
  191. struct tss_struct *tss)
  192. {
  193. struct thread_struct *prev, *next;
  194. prev = &prev_p->thread;
  195. next = &next_p->thread;
  196. if (test_tsk_thread_flag(prev_p, TIF_BLOCKSTEP) ^
  197. test_tsk_thread_flag(next_p, TIF_BLOCKSTEP)) {
  198. unsigned long debugctl = get_debugctlmsr();
  199. debugctl &= ~DEBUGCTLMSR_BTF;
  200. if (test_tsk_thread_flag(next_p, TIF_BLOCKSTEP))
  201. debugctl |= DEBUGCTLMSR_BTF;
  202. update_debugctlmsr(debugctl);
  203. }
  204. if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
  205. test_tsk_thread_flag(next_p, TIF_NOTSC)) {
  206. /* prev and next are different */
  207. if (test_tsk_thread_flag(next_p, TIF_NOTSC))
  208. hard_disable_TSC();
  209. else
  210. hard_enable_TSC();
  211. }
  212. if (test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
  213. /*
  214. * Copy the relevant range of the IO bitmap.
  215. * Normally this is 128 bytes or less:
  216. */
  217. memcpy(tss->io_bitmap, next->io_bitmap_ptr,
  218. max(prev->io_bitmap_max, next->io_bitmap_max));
  219. } else if (test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)) {
  220. /*
  221. * Clear any possible leftover bits:
  222. */
  223. memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);
  224. }
  225. propagate_user_return_notify(prev_p, next_p);
  226. }
  227. /*
  228. * Idle related variables and functions
  229. */
  230. unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE;
  231. EXPORT_SYMBOL(boot_option_idle_override);
  232. static void (*x86_idle)(void);
  233. #ifndef CONFIG_SMP
  234. static inline void play_dead(void)
  235. {
  236. BUG();
  237. }
  238. #endif
  239. #ifdef CONFIG_X86_64
  240. void enter_idle(void)
  241. {
  242. this_cpu_write(is_idle, 1);
  243. atomic_notifier_call_chain(&idle_notifier, IDLE_START, NULL);
  244. }
  245. static void __exit_idle(void)
  246. {
  247. if (x86_test_and_clear_bit_percpu(0, is_idle) == 0)
  248. return;
  249. atomic_notifier_call_chain(&idle_notifier, IDLE_END, NULL);
  250. }
  251. /* Called from interrupts to signify idle end */
  252. void exit_idle(void)
  253. {
  254. /* idle loop has pid 0 */
  255. if (current->pid)
  256. return;
  257. __exit_idle();
  258. }
  259. #endif
  260. /*
  261. * The idle thread. There's no useful work to be
  262. * done, so just try to conserve power and have a
  263. * low exit latency (ie sit in a loop waiting for
  264. * somebody to say that they'd like to reschedule)
  265. */
  266. void cpu_idle(void)
  267. {
  268. /*
  269. * If we're the non-boot CPU, nothing set the stack canary up
  270. * for us. CPU0 already has it initialized but no harm in
  271. * doing it again. This is a good place for updating it, as
  272. * we wont ever return from this function (so the invalid
  273. * canaries already on the stack wont ever trigger).
  274. */
  275. boot_init_stack_canary();
  276. current_thread_info()->status |= TS_POLLING;
  277. while (1) {
  278. tick_nohz_idle_enter();
  279. while (!need_resched()) {
  280. rmb();
  281. if (cpu_is_offline(smp_processor_id()))
  282. play_dead();
  283. /*
  284. * Idle routines should keep interrupts disabled
  285. * from here on, until they go to idle.
  286. * Otherwise, idle callbacks can misfire.
  287. */
  288. local_touch_nmi();
  289. local_irq_disable();
  290. enter_idle();
  291. /* Don't trace irqs off for idle */
  292. stop_critical_timings();
  293. /* enter_idle() needs rcu for notifiers */
  294. rcu_idle_enter();
  295. if (cpuidle_idle_call())
  296. x86_idle();
  297. rcu_idle_exit();
  298. start_critical_timings();
  299. /* In many cases the interrupt that ended idle
  300. has already called exit_idle. But some idle
  301. loops can be woken up without interrupt. */
  302. __exit_idle();
  303. }
  304. tick_nohz_idle_exit();
  305. preempt_enable_no_resched();
  306. schedule();
  307. preempt_disable();
  308. }
  309. }
  310. /*
  311. * We use this if we don't have any better
  312. * idle routine..
  313. */
  314. void default_idle(void)
  315. {
  316. trace_cpu_idle_rcuidle(1, smp_processor_id());
  317. current_thread_info()->status &= ~TS_POLLING;
  318. /*
  319. * TS_POLLING-cleared state must be visible before we
  320. * test NEED_RESCHED:
  321. */
  322. smp_mb();
  323. if (!need_resched())
  324. safe_halt(); /* enables interrupts racelessly */
  325. else
  326. local_irq_enable();
  327. current_thread_info()->status |= TS_POLLING;
  328. trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
  329. }
  330. #ifdef CONFIG_APM_MODULE
  331. EXPORT_SYMBOL(default_idle);
  332. #endif
  333. #ifdef CONFIG_XEN
  334. bool xen_set_default_idle(void)
  335. {
  336. bool ret = !!x86_idle;
  337. x86_idle = default_idle;
  338. return ret;
  339. }
  340. #endif
  341. void stop_this_cpu(void *dummy)
  342. {
  343. local_irq_disable();
  344. /*
  345. * Remove this CPU:
  346. */
  347. set_cpu_online(smp_processor_id(), false);
  348. disable_local_APIC();
  349. for (;;)
  350. halt();
  351. }
  352. /*
  353. * On SMP it's slightly faster (but much more power-consuming!)
  354. * to poll the ->work.need_resched flag instead of waiting for the
  355. * cross-CPU IPI to arrive. Use this option with caution.
  356. */
  357. static void poll_idle(void)
  358. {
  359. trace_cpu_idle_rcuidle(0, smp_processor_id());
  360. local_irq_enable();
  361. while (!need_resched())
  362. cpu_relax();
  363. trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
  364. }
  365. bool amd_e400_c1e_detected;
  366. EXPORT_SYMBOL(amd_e400_c1e_detected);
  367. static cpumask_var_t amd_e400_c1e_mask;
  368. void amd_e400_remove_cpu(int cpu)
  369. {
  370. if (amd_e400_c1e_mask != NULL)
  371. cpumask_clear_cpu(cpu, amd_e400_c1e_mask);
  372. }
  373. /*
  374. * AMD Erratum 400 aware idle routine. We check for C1E active in the interrupt
  375. * pending message MSR. If we detect C1E, then we handle it the same
  376. * way as C3 power states (local apic timer and TSC stop)
  377. */
  378. static void amd_e400_idle(void)
  379. {
  380. if (need_resched())
  381. return;
  382. if (!amd_e400_c1e_detected) {
  383. u32 lo, hi;
  384. rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
  385. if (lo & K8_INTP_C1E_ACTIVE_MASK) {
  386. amd_e400_c1e_detected = true;
  387. if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  388. mark_tsc_unstable("TSC halt in AMD C1E");
  389. pr_info("System has AMD C1E enabled\n");
  390. }
  391. }
  392. if (amd_e400_c1e_detected) {
  393. int cpu = smp_processor_id();
  394. if (!cpumask_test_cpu(cpu, amd_e400_c1e_mask)) {
  395. cpumask_set_cpu(cpu, amd_e400_c1e_mask);
  396. /*
  397. * Force broadcast so ACPI can not interfere.
  398. */
  399. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
  400. &cpu);
  401. pr_info("Switch to broadcast mode on CPU%d\n", cpu);
  402. }
  403. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
  404. default_idle();
  405. /*
  406. * The switch back from broadcast mode needs to be
  407. * called with interrupts disabled.
  408. */
  409. local_irq_disable();
  410. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
  411. local_irq_enable();
  412. } else
  413. default_idle();
  414. }
  415. void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
  416. {
  417. #ifdef CONFIG_SMP
  418. if (x86_idle == poll_idle && smp_num_siblings > 1)
  419. pr_warn_once("WARNING: polling idle and HT enabled, performance may degrade\n");
  420. #endif
  421. if (x86_idle)
  422. return;
  423. if (cpu_has_amd_erratum(amd_erratum_400)) {
  424. /* E400: APIC timer interrupt does not wake up CPU from C1e */
  425. pr_info("using AMD E400 aware idle routine\n");
  426. x86_idle = amd_e400_idle;
  427. } else
  428. x86_idle = default_idle;
  429. }
  430. void __init init_amd_e400_c1e_mask(void)
  431. {
  432. /* If we're using amd_e400_idle, we need to allocate amd_e400_c1e_mask. */
  433. if (x86_idle == amd_e400_idle)
  434. zalloc_cpumask_var(&amd_e400_c1e_mask, GFP_KERNEL);
  435. }
  436. static int __init idle_setup(char *str)
  437. {
  438. if (!str)
  439. return -EINVAL;
  440. if (!strcmp(str, "poll")) {
  441. pr_info("using polling idle threads\n");
  442. x86_idle = poll_idle;
  443. boot_option_idle_override = IDLE_POLL;
  444. } else if (!strcmp(str, "halt")) {
  445. /*
  446. * When the boot option of idle=halt is added, halt is
  447. * forced to be used for CPU idle. In such case CPU C2/C3
  448. * won't be used again.
  449. * To continue to load the CPU idle driver, don't touch
  450. * the boot_option_idle_override.
  451. */
  452. x86_idle = default_idle;
  453. boot_option_idle_override = IDLE_HALT;
  454. } else if (!strcmp(str, "nomwait")) {
  455. /*
  456. * If the boot option of "idle=nomwait" is added,
  457. * it means that mwait will be disabled for CPU C2/C3
  458. * states. In such case it won't touch the variable
  459. * of boot_option_idle_override.
  460. */
  461. boot_option_idle_override = IDLE_NOMWAIT;
  462. } else
  463. return -1;
  464. return 0;
  465. }
  466. early_param("idle", idle_setup);
  467. unsigned long arch_align_stack(unsigned long sp)
  468. {
  469. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  470. sp -= get_random_int() % 8192;
  471. return sp & ~0xf;
  472. }
  473. unsigned long arch_randomize_brk(struct mm_struct *mm)
  474. {
  475. unsigned long range_end = mm->brk + 0x02000000;
  476. return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
  477. }