process.c 18 KB

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