process.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. #include <linux/errno.h>
  2. #include <linux/kernel.h>
  3. #include <linux/mm.h>
  4. #include <linux/smp.h>
  5. #include <linux/prctl.h>
  6. #include <linux/slab.h>
  7. #include <linux/sched.h>
  8. #include <linux/module.h>
  9. #include <linux/pm.h>
  10. #include <linux/clockchips.h>
  11. #include <linux/random.h>
  12. #include <linux/user-return-notifier.h>
  13. #include <linux/dmi.h>
  14. #include <linux/utsname.h>
  15. #include <linux/stackprotector.h>
  16. #include <linux/tick.h>
  17. #include <linux/cpuidle.h>
  18. #include <trace/events/power.h>
  19. #include <linux/hw_breakpoint.h>
  20. #include <asm/cpu.h>
  21. #include <asm/apic.h>
  22. #include <asm/syscalls.h>
  23. #include <asm/idle.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/i387.h>
  26. #include <asm/fpu-internal.h>
  27. #include <asm/debugreg.h>
  28. #include <asm/nmi.h>
  29. #ifdef CONFIG_X86_64
  30. static DEFINE_PER_CPU(unsigned char, is_idle);
  31. static ATOMIC_NOTIFIER_HEAD(idle_notifier);
  32. void idle_notifier_register(struct notifier_block *n)
  33. {
  34. atomic_notifier_chain_register(&idle_notifier, n);
  35. }
  36. EXPORT_SYMBOL_GPL(idle_notifier_register);
  37. void idle_notifier_unregister(struct notifier_block *n)
  38. {
  39. atomic_notifier_chain_unregister(&idle_notifier, n);
  40. }
  41. EXPORT_SYMBOL_GPL(idle_notifier_unregister);
  42. #endif
  43. struct kmem_cache *task_xstate_cachep;
  44. EXPORT_SYMBOL_GPL(task_xstate_cachep);
  45. /*
  46. * this gets called so that we can store lazy state into memory and copy the
  47. * current task into the new thread.
  48. */
  49. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  50. {
  51. int ret;
  52. unlazy_fpu(src);
  53. *dst = *src;
  54. if (fpu_allocated(&src->thread.fpu)) {
  55. memset(&dst->thread.fpu, 0, sizeof(dst->thread.fpu));
  56. ret = fpu_alloc(&dst->thread.fpu);
  57. if (ret)
  58. return ret;
  59. fpu_copy(&dst->thread.fpu, &src->thread.fpu);
  60. }
  61. return 0;
  62. }
  63. void free_thread_xstate(struct task_struct *tsk)
  64. {
  65. fpu_free(&tsk->thread.fpu);
  66. }
  67. void free_thread_info(struct thread_info *ti)
  68. {
  69. free_thread_xstate(ti->task);
  70. free_pages((unsigned long)ti, THREAD_ORDER);
  71. }
  72. void arch_task_cache_init(void)
  73. {
  74. task_xstate_cachep =
  75. kmem_cache_create("task_xstate", xstate_size,
  76. __alignof__(union thread_xstate),
  77. SLAB_PANIC | SLAB_NOTRACK, NULL);
  78. }
  79. static inline void drop_fpu(struct task_struct *tsk)
  80. {
  81. /*
  82. * Forget coprocessor state..
  83. */
  84. tsk->fpu_counter = 0;
  85. clear_fpu(tsk);
  86. clear_used_math();
  87. }
  88. /*
  89. * Free current thread data structures etc..
  90. */
  91. void exit_thread(void)
  92. {
  93. struct task_struct *me = current;
  94. struct thread_struct *t = &me->thread;
  95. unsigned long *bp = t->io_bitmap_ptr;
  96. if (bp) {
  97. struct tss_struct *tss = &per_cpu(init_tss, get_cpu());
  98. t->io_bitmap_ptr = NULL;
  99. clear_thread_flag(TIF_IO_BITMAP);
  100. /*
  101. * Careful, clear this in the TSS too:
  102. */
  103. memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
  104. t->io_bitmap_max = 0;
  105. put_cpu();
  106. kfree(bp);
  107. }
  108. drop_fpu(me);
  109. }
  110. void show_regs(struct pt_regs *regs)
  111. {
  112. show_registers(regs);
  113. show_trace(NULL, regs, (unsigned long *)kernel_stack_pointer(regs), 0);
  114. }
  115. void show_regs_common(void)
  116. {
  117. const char *vendor, *product, *board;
  118. vendor = dmi_get_system_info(DMI_SYS_VENDOR);
  119. if (!vendor)
  120. vendor = "";
  121. product = dmi_get_system_info(DMI_PRODUCT_NAME);
  122. if (!product)
  123. product = "";
  124. /* Board Name is optional */
  125. board = dmi_get_system_info(DMI_BOARD_NAME);
  126. printk(KERN_CONT "\n");
  127. printk(KERN_DEFAULT "Pid: %d, comm: %.20s %s %s %.*s",
  128. current->pid, current->comm, print_tainted(),
  129. init_utsname()->release,
  130. (int)strcspn(init_utsname()->version, " "),
  131. init_utsname()->version);
  132. printk(KERN_CONT " %s %s", vendor, product);
  133. if (board)
  134. printk(KERN_CONT "/%s", board);
  135. printk(KERN_CONT "\n");
  136. }
  137. void flush_thread(void)
  138. {
  139. struct task_struct *tsk = current;
  140. flush_ptrace_hw_breakpoint(tsk);
  141. memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
  142. drop_fpu(tsk);
  143. }
  144. static void hard_disable_TSC(void)
  145. {
  146. write_cr4(read_cr4() | X86_CR4_TSD);
  147. }
  148. void disable_TSC(void)
  149. {
  150. preempt_disable();
  151. if (!test_and_set_thread_flag(TIF_NOTSC))
  152. /*
  153. * Must flip the CPU state synchronously with
  154. * TIF_NOTSC in the current running context.
  155. */
  156. hard_disable_TSC();
  157. preempt_enable();
  158. }
  159. static void hard_enable_TSC(void)
  160. {
  161. write_cr4(read_cr4() & ~X86_CR4_TSD);
  162. }
  163. static void enable_TSC(void)
  164. {
  165. preempt_disable();
  166. if (test_and_clear_thread_flag(TIF_NOTSC))
  167. /*
  168. * Must flip the CPU state synchronously with
  169. * TIF_NOTSC in the current running context.
  170. */
  171. hard_enable_TSC();
  172. preempt_enable();
  173. }
  174. int get_tsc_mode(unsigned long adr)
  175. {
  176. unsigned int val;
  177. if (test_thread_flag(TIF_NOTSC))
  178. val = PR_TSC_SIGSEGV;
  179. else
  180. val = PR_TSC_ENABLE;
  181. return put_user(val, (unsigned int __user *)adr);
  182. }
  183. int set_tsc_mode(unsigned int val)
  184. {
  185. if (val == PR_TSC_SIGSEGV)
  186. disable_TSC();
  187. else if (val == PR_TSC_ENABLE)
  188. enable_TSC();
  189. else
  190. return -EINVAL;
  191. return 0;
  192. }
  193. void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
  194. struct tss_struct *tss)
  195. {
  196. struct thread_struct *prev, *next;
  197. prev = &prev_p->thread;
  198. next = &next_p->thread;
  199. if (test_tsk_thread_flag(prev_p, TIF_BLOCKSTEP) ^
  200. test_tsk_thread_flag(next_p, TIF_BLOCKSTEP)) {
  201. unsigned long debugctl = get_debugctlmsr();
  202. debugctl &= ~DEBUGCTLMSR_BTF;
  203. if (test_tsk_thread_flag(next_p, TIF_BLOCKSTEP))
  204. debugctl |= DEBUGCTLMSR_BTF;
  205. update_debugctlmsr(debugctl);
  206. }
  207. if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
  208. test_tsk_thread_flag(next_p, TIF_NOTSC)) {
  209. /* prev and next are different */
  210. if (test_tsk_thread_flag(next_p, TIF_NOTSC))
  211. hard_disable_TSC();
  212. else
  213. hard_enable_TSC();
  214. }
  215. if (test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
  216. /*
  217. * Copy the relevant range of the IO bitmap.
  218. * Normally this is 128 bytes or less:
  219. */
  220. memcpy(tss->io_bitmap, next->io_bitmap_ptr,
  221. max(prev->io_bitmap_max, next->io_bitmap_max));
  222. } else if (test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)) {
  223. /*
  224. * Clear any possible leftover bits:
  225. */
  226. memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);
  227. }
  228. propagate_user_return_notify(prev_p, next_p);
  229. }
  230. int sys_fork(struct pt_regs *regs)
  231. {
  232. return do_fork(SIGCHLD, regs->sp, regs, 0, NULL, NULL);
  233. }
  234. /*
  235. * This is trivial, and on the face of it looks like it
  236. * could equally well be done in user mode.
  237. *
  238. * Not so, for quite unobvious reasons - register pressure.
  239. * In user mode vfork() cannot have a stack frame, and if
  240. * done by calling the "clone()" system call directly, you
  241. * do not have enough call-clobbered registers to hold all
  242. * the information you need.
  243. */
  244. int sys_vfork(struct pt_regs *regs)
  245. {
  246. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->sp, regs, 0,
  247. NULL, NULL);
  248. }
  249. long
  250. sys_clone(unsigned long clone_flags, unsigned long newsp,
  251. void __user *parent_tid, void __user *child_tid, struct pt_regs *regs)
  252. {
  253. if (!newsp)
  254. newsp = regs->sp;
  255. return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
  256. }
  257. /*
  258. * This gets run with %si containing the
  259. * function to call, and %di containing
  260. * the "args".
  261. */
  262. extern void kernel_thread_helper(void);
  263. /*
  264. * Create a kernel thread
  265. */
  266. int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  267. {
  268. struct pt_regs regs;
  269. memset(&regs, 0, sizeof(regs));
  270. regs.si = (unsigned long) fn;
  271. regs.di = (unsigned long) arg;
  272. #ifdef CONFIG_X86_32
  273. regs.ds = __USER_DS;
  274. regs.es = __USER_DS;
  275. regs.fs = __KERNEL_PERCPU;
  276. regs.gs = __KERNEL_STACK_CANARY;
  277. #else
  278. regs.ss = __KERNEL_DS;
  279. #endif
  280. regs.orig_ax = -1;
  281. regs.ip = (unsigned long) kernel_thread_helper;
  282. regs.cs = __KERNEL_CS | get_kernel_rpl();
  283. regs.flags = X86_EFLAGS_IF | X86_EFLAGS_BIT1;
  284. /* Ok, create the new process.. */
  285. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
  286. }
  287. EXPORT_SYMBOL(kernel_thread);
  288. /*
  289. * sys_execve() executes a new program.
  290. */
  291. long sys_execve(const char __user *name,
  292. const char __user *const __user *argv,
  293. const char __user *const __user *envp, struct pt_regs *regs)
  294. {
  295. long error;
  296. char *filename;
  297. filename = getname(name);
  298. error = PTR_ERR(filename);
  299. if (IS_ERR(filename))
  300. return error;
  301. error = do_execve(filename, argv, envp, regs);
  302. #ifdef CONFIG_X86_32
  303. if (error == 0) {
  304. /* Make sure we don't return using sysenter.. */
  305. set_thread_flag(TIF_IRET);
  306. }
  307. #endif
  308. putname(filename);
  309. return error;
  310. }
  311. /*
  312. * Idle related variables and functions
  313. */
  314. unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE;
  315. EXPORT_SYMBOL(boot_option_idle_override);
  316. /*
  317. * Powermanagement idle function, if any..
  318. */
  319. void (*pm_idle)(void);
  320. #ifdef CONFIG_APM_MODULE
  321. EXPORT_SYMBOL(pm_idle);
  322. #endif
  323. static inline int hlt_use_halt(void)
  324. {
  325. return 1;
  326. }
  327. #ifndef CONFIG_SMP
  328. static inline void play_dead(void)
  329. {
  330. BUG();
  331. }
  332. #endif
  333. #ifdef CONFIG_X86_64
  334. void enter_idle(void)
  335. {
  336. percpu_write(is_idle, 1);
  337. atomic_notifier_call_chain(&idle_notifier, IDLE_START, NULL);
  338. }
  339. static void __exit_idle(void)
  340. {
  341. if (x86_test_and_clear_bit_percpu(0, is_idle) == 0)
  342. return;
  343. atomic_notifier_call_chain(&idle_notifier, IDLE_END, NULL);
  344. }
  345. /* Called from interrupts to signify idle end */
  346. void exit_idle(void)
  347. {
  348. /* idle loop has pid 0 */
  349. if (current->pid)
  350. return;
  351. __exit_idle();
  352. }
  353. #endif
  354. /*
  355. * The idle thread. There's no useful work to be
  356. * done, so just try to conserve power and have a
  357. * low exit latency (ie sit in a loop waiting for
  358. * somebody to say that they'd like to reschedule)
  359. */
  360. void cpu_idle(void)
  361. {
  362. /*
  363. * If we're the non-boot CPU, nothing set the stack canary up
  364. * for us. CPU0 already has it initialized but no harm in
  365. * doing it again. This is a good place for updating it, as
  366. * we wont ever return from this function (so the invalid
  367. * canaries already on the stack wont ever trigger).
  368. */
  369. boot_init_stack_canary();
  370. current_thread_info()->status |= TS_POLLING;
  371. while (1) {
  372. tick_nohz_idle_enter();
  373. while (!need_resched()) {
  374. rmb();
  375. if (cpu_is_offline(smp_processor_id()))
  376. play_dead();
  377. /*
  378. * Idle routines should keep interrupts disabled
  379. * from here on, until they go to idle.
  380. * Otherwise, idle callbacks can misfire.
  381. */
  382. local_touch_nmi();
  383. local_irq_disable();
  384. enter_idle();
  385. /* Don't trace irqs off for idle */
  386. stop_critical_timings();
  387. /* enter_idle() needs rcu for notifiers */
  388. rcu_idle_enter();
  389. if (cpuidle_idle_call())
  390. pm_idle();
  391. rcu_idle_exit();
  392. start_critical_timings();
  393. /* In many cases the interrupt that ended idle
  394. has already called exit_idle. But some idle
  395. loops can be woken up without interrupt. */
  396. __exit_idle();
  397. }
  398. tick_nohz_idle_exit();
  399. preempt_enable_no_resched();
  400. schedule();
  401. preempt_disable();
  402. }
  403. }
  404. /*
  405. * We use this if we don't have any better
  406. * idle routine..
  407. */
  408. void default_idle(void)
  409. {
  410. if (hlt_use_halt()) {
  411. trace_power_start_rcuidle(POWER_CSTATE, 1, smp_processor_id());
  412. trace_cpu_idle_rcuidle(1, smp_processor_id());
  413. current_thread_info()->status &= ~TS_POLLING;
  414. /*
  415. * TS_POLLING-cleared state must be visible before we
  416. * test NEED_RESCHED:
  417. */
  418. smp_mb();
  419. if (!need_resched())
  420. safe_halt(); /* enables interrupts racelessly */
  421. else
  422. local_irq_enable();
  423. current_thread_info()->status |= TS_POLLING;
  424. trace_power_end_rcuidle(smp_processor_id());
  425. trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
  426. } else {
  427. local_irq_enable();
  428. /* loop is done by the caller */
  429. cpu_relax();
  430. }
  431. }
  432. #ifdef CONFIG_APM_MODULE
  433. EXPORT_SYMBOL(default_idle);
  434. #endif
  435. bool set_pm_idle_to_default(void)
  436. {
  437. bool ret = !!pm_idle;
  438. pm_idle = default_idle;
  439. return ret;
  440. }
  441. void stop_this_cpu(void *dummy)
  442. {
  443. local_irq_disable();
  444. /*
  445. * Remove this CPU:
  446. */
  447. set_cpu_online(smp_processor_id(), false);
  448. disable_local_APIC();
  449. for (;;) {
  450. if (hlt_works(smp_processor_id()))
  451. halt();
  452. }
  453. }
  454. static void do_nothing(void *unused)
  455. {
  456. }
  457. /*
  458. * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
  459. * pm_idle and update to new pm_idle value. Required while changing pm_idle
  460. * handler on SMP systems.
  461. *
  462. * Caller must have changed pm_idle to the new value before the call. Old
  463. * pm_idle value will not be used by any CPU after the return of this function.
  464. */
  465. void cpu_idle_wait(void)
  466. {
  467. smp_mb();
  468. /* kick all the CPUs so that they exit out of pm_idle */
  469. smp_call_function(do_nothing, NULL, 1);
  470. }
  471. EXPORT_SYMBOL_GPL(cpu_idle_wait);
  472. /* Default MONITOR/MWAIT with no hints, used for default C1 state */
  473. static void mwait_idle(void)
  474. {
  475. if (!need_resched()) {
  476. trace_power_start_rcuidle(POWER_CSTATE, 1, smp_processor_id());
  477. trace_cpu_idle_rcuidle(1, smp_processor_id());
  478. if (this_cpu_has(X86_FEATURE_CLFLUSH_MONITOR))
  479. clflush((void *)&current_thread_info()->flags);
  480. __monitor((void *)&current_thread_info()->flags, 0, 0);
  481. smp_mb();
  482. if (!need_resched())
  483. __sti_mwait(0, 0);
  484. else
  485. local_irq_enable();
  486. trace_power_end_rcuidle(smp_processor_id());
  487. trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
  488. } else
  489. local_irq_enable();
  490. }
  491. /*
  492. * On SMP it's slightly faster (but much more power-consuming!)
  493. * to poll the ->work.need_resched flag instead of waiting for the
  494. * cross-CPU IPI to arrive. Use this option with caution.
  495. */
  496. static void poll_idle(void)
  497. {
  498. trace_power_start_rcuidle(POWER_CSTATE, 0, smp_processor_id());
  499. trace_cpu_idle_rcuidle(0, smp_processor_id());
  500. local_irq_enable();
  501. while (!need_resched())
  502. cpu_relax();
  503. trace_power_end_rcuidle(smp_processor_id());
  504. trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
  505. }
  506. /*
  507. * mwait selection logic:
  508. *
  509. * It depends on the CPU. For AMD CPUs that support MWAIT this is
  510. * wrong. Family 0x10 and 0x11 CPUs will enter C1 on HLT. Powersavings
  511. * then depend on a clock divisor and current Pstate of the core. If
  512. * all cores of a processor are in halt state (C1) the processor can
  513. * enter the C1E (C1 enhanced) state. If mwait is used this will never
  514. * happen.
  515. *
  516. * idle=mwait overrides this decision and forces the usage of mwait.
  517. */
  518. #define MWAIT_INFO 0x05
  519. #define MWAIT_ECX_EXTENDED_INFO 0x01
  520. #define MWAIT_EDX_C1 0xf0
  521. int mwait_usable(const struct cpuinfo_x86 *c)
  522. {
  523. u32 eax, ebx, ecx, edx;
  524. if (boot_option_idle_override == IDLE_FORCE_MWAIT)
  525. return 1;
  526. if (c->cpuid_level < MWAIT_INFO)
  527. return 0;
  528. cpuid(MWAIT_INFO, &eax, &ebx, &ecx, &edx);
  529. /* Check, whether EDX has extended info about MWAIT */
  530. if (!(ecx & MWAIT_ECX_EXTENDED_INFO))
  531. return 1;
  532. /*
  533. * edx enumeratios MONITOR/MWAIT extensions. Check, whether
  534. * C1 supports MWAIT
  535. */
  536. return (edx & MWAIT_EDX_C1);
  537. }
  538. bool amd_e400_c1e_detected;
  539. EXPORT_SYMBOL(amd_e400_c1e_detected);
  540. static cpumask_var_t amd_e400_c1e_mask;
  541. void amd_e400_remove_cpu(int cpu)
  542. {
  543. if (amd_e400_c1e_mask != NULL)
  544. cpumask_clear_cpu(cpu, amd_e400_c1e_mask);
  545. }
  546. /*
  547. * AMD Erratum 400 aware idle routine. We check for C1E active in the interrupt
  548. * pending message MSR. If we detect C1E, then we handle it the same
  549. * way as C3 power states (local apic timer and TSC stop)
  550. */
  551. static void amd_e400_idle(void)
  552. {
  553. if (need_resched())
  554. return;
  555. if (!amd_e400_c1e_detected) {
  556. u32 lo, hi;
  557. rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
  558. if (lo & K8_INTP_C1E_ACTIVE_MASK) {
  559. amd_e400_c1e_detected = true;
  560. if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  561. mark_tsc_unstable("TSC halt in AMD C1E");
  562. printk(KERN_INFO "System has AMD C1E enabled\n");
  563. }
  564. }
  565. if (amd_e400_c1e_detected) {
  566. int cpu = smp_processor_id();
  567. if (!cpumask_test_cpu(cpu, amd_e400_c1e_mask)) {
  568. cpumask_set_cpu(cpu, amd_e400_c1e_mask);
  569. /*
  570. * Force broadcast so ACPI can not interfere.
  571. */
  572. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
  573. &cpu);
  574. printk(KERN_INFO "Switch to broadcast mode on CPU%d\n",
  575. cpu);
  576. }
  577. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
  578. default_idle();
  579. /*
  580. * The switch back from broadcast mode needs to be
  581. * called with interrupts disabled.
  582. */
  583. local_irq_disable();
  584. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
  585. local_irq_enable();
  586. } else
  587. default_idle();
  588. }
  589. void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
  590. {
  591. #ifdef CONFIG_SMP
  592. if (pm_idle == poll_idle && smp_num_siblings > 1) {
  593. printk_once(KERN_WARNING "WARNING: polling idle and HT enabled,"
  594. " performance may degrade.\n");
  595. }
  596. #endif
  597. if (pm_idle)
  598. return;
  599. if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) {
  600. /*
  601. * One CPU supports mwait => All CPUs supports mwait
  602. */
  603. printk(KERN_INFO "using mwait in idle threads.\n");
  604. pm_idle = mwait_idle;
  605. } else if (cpu_has_amd_erratum(amd_erratum_400)) {
  606. /* E400: APIC timer interrupt does not wake up CPU from C1e */
  607. printk(KERN_INFO "using AMD E400 aware idle routine\n");
  608. pm_idle = amd_e400_idle;
  609. } else
  610. pm_idle = default_idle;
  611. }
  612. void __init init_amd_e400_c1e_mask(void)
  613. {
  614. /* If we're using amd_e400_idle, we need to allocate amd_e400_c1e_mask. */
  615. if (pm_idle == amd_e400_idle)
  616. zalloc_cpumask_var(&amd_e400_c1e_mask, GFP_KERNEL);
  617. }
  618. static int __init idle_setup(char *str)
  619. {
  620. if (!str)
  621. return -EINVAL;
  622. if (!strcmp(str, "poll")) {
  623. printk("using polling idle threads.\n");
  624. pm_idle = poll_idle;
  625. boot_option_idle_override = IDLE_POLL;
  626. } else if (!strcmp(str, "mwait")) {
  627. boot_option_idle_override = IDLE_FORCE_MWAIT;
  628. WARN_ONCE(1, "\"idle=mwait\" will be removed in 2012\n");
  629. } else if (!strcmp(str, "halt")) {
  630. /*
  631. * When the boot option of idle=halt is added, halt is
  632. * forced to be used for CPU idle. In such case CPU C2/C3
  633. * won't be used again.
  634. * To continue to load the CPU idle driver, don't touch
  635. * the boot_option_idle_override.
  636. */
  637. pm_idle = default_idle;
  638. boot_option_idle_override = IDLE_HALT;
  639. } else if (!strcmp(str, "nomwait")) {
  640. /*
  641. * If the boot option of "idle=nomwait" is added,
  642. * it means that mwait will be disabled for CPU C2/C3
  643. * states. In such case it won't touch the variable
  644. * of boot_option_idle_override.
  645. */
  646. boot_option_idle_override = IDLE_NOMWAIT;
  647. } else
  648. return -1;
  649. return 0;
  650. }
  651. early_param("idle", idle_setup);
  652. unsigned long arch_align_stack(unsigned long sp)
  653. {
  654. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  655. sp -= get_random_int() % 8192;
  656. return sp & ~0xf;
  657. }
  658. unsigned long arch_randomize_brk(struct mm_struct *mm)
  659. {
  660. unsigned long range_end = mm->brk + 0x02000000;
  661. return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
  662. }