process.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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 <trace/power.h>
  12. #include <asm/system.h>
  13. #include <asm/apic.h>
  14. #include <asm/syscalls.h>
  15. #include <asm/idle.h>
  16. #include <asm/uaccess.h>
  17. #include <asm/i387.h>
  18. unsigned long idle_halt;
  19. EXPORT_SYMBOL(idle_halt);
  20. unsigned long idle_nomwait;
  21. EXPORT_SYMBOL(idle_nomwait);
  22. struct kmem_cache *task_xstate_cachep;
  23. DEFINE_TRACE(power_start);
  24. DEFINE_TRACE(power_end);
  25. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  26. {
  27. *dst = *src;
  28. if (src->thread.xstate) {
  29. dst->thread.xstate = kmem_cache_alloc(task_xstate_cachep,
  30. GFP_KERNEL);
  31. if (!dst->thread.xstate)
  32. return -ENOMEM;
  33. WARN_ON((unsigned long)dst->thread.xstate & 15);
  34. memcpy(dst->thread.xstate, src->thread.xstate, xstate_size);
  35. }
  36. return 0;
  37. }
  38. void free_thread_xstate(struct task_struct *tsk)
  39. {
  40. if (tsk->thread.xstate) {
  41. kmem_cache_free(task_xstate_cachep, tsk->thread.xstate);
  42. tsk->thread.xstate = NULL;
  43. }
  44. }
  45. void free_thread_info(struct thread_info *ti)
  46. {
  47. free_thread_xstate(ti->task);
  48. free_pages((unsigned long)ti, get_order(THREAD_SIZE));
  49. }
  50. void arch_task_cache_init(void)
  51. {
  52. task_xstate_cachep =
  53. kmem_cache_create("task_xstate", xstate_size,
  54. __alignof__(union thread_xstate),
  55. SLAB_PANIC, NULL);
  56. }
  57. /*
  58. * Free current thread data structures etc..
  59. */
  60. void exit_thread(void)
  61. {
  62. struct task_struct *me = current;
  63. struct thread_struct *t = &me->thread;
  64. unsigned long *bp = t->io_bitmap_ptr;
  65. if (bp) {
  66. struct tss_struct *tss = &per_cpu(init_tss, get_cpu());
  67. t->io_bitmap_ptr = NULL;
  68. clear_thread_flag(TIF_IO_BITMAP);
  69. /*
  70. * Careful, clear this in the TSS too:
  71. */
  72. memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
  73. t->io_bitmap_max = 0;
  74. put_cpu();
  75. kfree(bp);
  76. }
  77. ds_exit_thread(current);
  78. }
  79. void flush_thread(void)
  80. {
  81. struct task_struct *tsk = current;
  82. #ifdef CONFIG_X86_64
  83. if (test_tsk_thread_flag(tsk, TIF_ABI_PENDING)) {
  84. clear_tsk_thread_flag(tsk, TIF_ABI_PENDING);
  85. if (test_tsk_thread_flag(tsk, TIF_IA32)) {
  86. clear_tsk_thread_flag(tsk, TIF_IA32);
  87. } else {
  88. set_tsk_thread_flag(tsk, TIF_IA32);
  89. current_thread_info()->status |= TS_COMPAT;
  90. }
  91. }
  92. #endif
  93. clear_tsk_thread_flag(tsk, TIF_DEBUG);
  94. tsk->thread.debugreg0 = 0;
  95. tsk->thread.debugreg1 = 0;
  96. tsk->thread.debugreg2 = 0;
  97. tsk->thread.debugreg3 = 0;
  98. tsk->thread.debugreg6 = 0;
  99. tsk->thread.debugreg7 = 0;
  100. memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
  101. /*
  102. * Forget coprocessor state..
  103. */
  104. tsk->fpu_counter = 0;
  105. clear_fpu(tsk);
  106. clear_used_math();
  107. }
  108. static void hard_disable_TSC(void)
  109. {
  110. write_cr4(read_cr4() | X86_CR4_TSD);
  111. }
  112. void disable_TSC(void)
  113. {
  114. preempt_disable();
  115. if (!test_and_set_thread_flag(TIF_NOTSC))
  116. /*
  117. * Must flip the CPU state synchronously with
  118. * TIF_NOTSC in the current running context.
  119. */
  120. hard_disable_TSC();
  121. preempt_enable();
  122. }
  123. static void hard_enable_TSC(void)
  124. {
  125. write_cr4(read_cr4() & ~X86_CR4_TSD);
  126. }
  127. static void enable_TSC(void)
  128. {
  129. preempt_disable();
  130. if (test_and_clear_thread_flag(TIF_NOTSC))
  131. /*
  132. * Must flip the CPU state synchronously with
  133. * TIF_NOTSC in the current running context.
  134. */
  135. hard_enable_TSC();
  136. preempt_enable();
  137. }
  138. int get_tsc_mode(unsigned long adr)
  139. {
  140. unsigned int val;
  141. if (test_thread_flag(TIF_NOTSC))
  142. val = PR_TSC_SIGSEGV;
  143. else
  144. val = PR_TSC_ENABLE;
  145. return put_user(val, (unsigned int __user *)adr);
  146. }
  147. int set_tsc_mode(unsigned int val)
  148. {
  149. if (val == PR_TSC_SIGSEGV)
  150. disable_TSC();
  151. else if (val == PR_TSC_ENABLE)
  152. enable_TSC();
  153. else
  154. return -EINVAL;
  155. return 0;
  156. }
  157. void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
  158. struct tss_struct *tss)
  159. {
  160. struct thread_struct *prev, *next;
  161. prev = &prev_p->thread;
  162. next = &next_p->thread;
  163. if (test_tsk_thread_flag(next_p, TIF_DS_AREA_MSR) ||
  164. test_tsk_thread_flag(prev_p, TIF_DS_AREA_MSR))
  165. ds_switch_to(prev_p, next_p);
  166. else if (next->debugctlmsr != prev->debugctlmsr)
  167. update_debugctlmsr(next->debugctlmsr);
  168. if (test_tsk_thread_flag(next_p, TIF_DEBUG)) {
  169. set_debugreg(next->debugreg0, 0);
  170. set_debugreg(next->debugreg1, 1);
  171. set_debugreg(next->debugreg2, 2);
  172. set_debugreg(next->debugreg3, 3);
  173. /* no 4 and 5 */
  174. set_debugreg(next->debugreg6, 6);
  175. set_debugreg(next->debugreg7, 7);
  176. }
  177. if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
  178. test_tsk_thread_flag(next_p, TIF_NOTSC)) {
  179. /* prev and next are different */
  180. if (test_tsk_thread_flag(next_p, TIF_NOTSC))
  181. hard_disable_TSC();
  182. else
  183. hard_enable_TSC();
  184. }
  185. if (test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
  186. /*
  187. * Copy the relevant range of the IO bitmap.
  188. * Normally this is 128 bytes or less:
  189. */
  190. memcpy(tss->io_bitmap, next->io_bitmap_ptr,
  191. max(prev->io_bitmap_max, next->io_bitmap_max));
  192. } else if (test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)) {
  193. /*
  194. * Clear any possible leftover bits:
  195. */
  196. memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);
  197. }
  198. }
  199. int sys_fork(struct pt_regs *regs)
  200. {
  201. return do_fork(SIGCHLD, regs->sp, regs, 0, NULL, NULL);
  202. }
  203. /*
  204. * This is trivial, and on the face of it looks like it
  205. * could equally well be done in user mode.
  206. *
  207. * Not so, for quite unobvious reasons - register pressure.
  208. * In user mode vfork() cannot have a stack frame, and if
  209. * done by calling the "clone()" system call directly, you
  210. * do not have enough call-clobbered registers to hold all
  211. * the information you need.
  212. */
  213. int sys_vfork(struct pt_regs *regs)
  214. {
  215. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->sp, regs, 0,
  216. NULL, NULL);
  217. }
  218. /*
  219. * Idle related variables and functions
  220. */
  221. unsigned long boot_option_idle_override = 0;
  222. EXPORT_SYMBOL(boot_option_idle_override);
  223. /*
  224. * Powermanagement idle function, if any..
  225. */
  226. void (*pm_idle)(void);
  227. EXPORT_SYMBOL(pm_idle);
  228. #ifdef CONFIG_X86_32
  229. /*
  230. * This halt magic was a workaround for ancient floppy DMA
  231. * wreckage. It should be safe to remove.
  232. */
  233. static int hlt_counter;
  234. void disable_hlt(void)
  235. {
  236. hlt_counter++;
  237. }
  238. EXPORT_SYMBOL(disable_hlt);
  239. void enable_hlt(void)
  240. {
  241. hlt_counter--;
  242. }
  243. EXPORT_SYMBOL(enable_hlt);
  244. static inline int hlt_use_halt(void)
  245. {
  246. return (!hlt_counter && boot_cpu_data.hlt_works_ok);
  247. }
  248. #else
  249. static inline int hlt_use_halt(void)
  250. {
  251. return 1;
  252. }
  253. #endif
  254. /*
  255. * We use this if we don't have any better
  256. * idle routine..
  257. */
  258. void default_idle(void)
  259. {
  260. if (hlt_use_halt()) {
  261. struct power_trace it;
  262. trace_power_start(&it, POWER_CSTATE, 1);
  263. current_thread_info()->status &= ~TS_POLLING;
  264. /*
  265. * TS_POLLING-cleared state must be visible before we
  266. * test NEED_RESCHED:
  267. */
  268. smp_mb();
  269. if (!need_resched())
  270. safe_halt(); /* enables interrupts racelessly */
  271. else
  272. local_irq_enable();
  273. current_thread_info()->status |= TS_POLLING;
  274. trace_power_end(&it);
  275. } else {
  276. local_irq_enable();
  277. /* loop is done by the caller */
  278. cpu_relax();
  279. }
  280. }
  281. #ifdef CONFIG_APM_MODULE
  282. EXPORT_SYMBOL(default_idle);
  283. #endif
  284. void stop_this_cpu(void *dummy)
  285. {
  286. local_irq_disable();
  287. /*
  288. * Remove this CPU:
  289. */
  290. set_cpu_online(smp_processor_id(), false);
  291. disable_local_APIC();
  292. for (;;) {
  293. if (hlt_works(smp_processor_id()))
  294. halt();
  295. }
  296. }
  297. static void do_nothing(void *unused)
  298. {
  299. }
  300. /*
  301. * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
  302. * pm_idle and update to new pm_idle value. Required while changing pm_idle
  303. * handler on SMP systems.
  304. *
  305. * Caller must have changed pm_idle to the new value before the call. Old
  306. * pm_idle value will not be used by any CPU after the return of this function.
  307. */
  308. void cpu_idle_wait(void)
  309. {
  310. smp_mb();
  311. /* kick all the CPUs so that they exit out of pm_idle */
  312. smp_call_function(do_nothing, NULL, 1);
  313. }
  314. EXPORT_SYMBOL_GPL(cpu_idle_wait);
  315. /*
  316. * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
  317. * which can obviate IPI to trigger checking of need_resched.
  318. * We execute MONITOR against need_resched and enter optimized wait state
  319. * through MWAIT. Whenever someone changes need_resched, we would be woken
  320. * up from MWAIT (without an IPI).
  321. *
  322. * New with Core Duo processors, MWAIT can take some hints based on CPU
  323. * capability.
  324. */
  325. void mwait_idle_with_hints(unsigned long ax, unsigned long cx)
  326. {
  327. struct power_trace it;
  328. trace_power_start(&it, POWER_CSTATE, (ax>>4)+1);
  329. if (!need_resched()) {
  330. if (cpu_has(&current_cpu_data, X86_FEATURE_CLFLUSH_MONITOR))
  331. clflush((void *)&current_thread_info()->flags);
  332. __monitor((void *)&current_thread_info()->flags, 0, 0);
  333. smp_mb();
  334. if (!need_resched())
  335. __mwait(ax, cx);
  336. }
  337. trace_power_end(&it);
  338. }
  339. /* Default MONITOR/MWAIT with no hints, used for default C1 state */
  340. static void mwait_idle(void)
  341. {
  342. struct power_trace it;
  343. if (!need_resched()) {
  344. trace_power_start(&it, POWER_CSTATE, 1);
  345. if (cpu_has(&current_cpu_data, X86_FEATURE_CLFLUSH_MONITOR))
  346. clflush((void *)&current_thread_info()->flags);
  347. __monitor((void *)&current_thread_info()->flags, 0, 0);
  348. smp_mb();
  349. if (!need_resched())
  350. __sti_mwait(0, 0);
  351. else
  352. local_irq_enable();
  353. trace_power_end(&it);
  354. } else
  355. local_irq_enable();
  356. }
  357. /*
  358. * On SMP it's slightly faster (but much more power-consuming!)
  359. * to poll the ->work.need_resched flag instead of waiting for the
  360. * cross-CPU IPI to arrive. Use this option with caution.
  361. */
  362. static void poll_idle(void)
  363. {
  364. struct power_trace it;
  365. trace_power_start(&it, POWER_CSTATE, 0);
  366. local_irq_enable();
  367. while (!need_resched())
  368. cpu_relax();
  369. trace_power_end(&it);
  370. }
  371. /*
  372. * mwait selection logic:
  373. *
  374. * It depends on the CPU. For AMD CPUs that support MWAIT this is
  375. * wrong. Family 0x10 and 0x11 CPUs will enter C1 on HLT. Powersavings
  376. * then depend on a clock divisor and current Pstate of the core. If
  377. * all cores of a processor are in halt state (C1) the processor can
  378. * enter the C1E (C1 enhanced) state. If mwait is used this will never
  379. * happen.
  380. *
  381. * idle=mwait overrides this decision and forces the usage of mwait.
  382. */
  383. static int __cpuinitdata force_mwait;
  384. #define MWAIT_INFO 0x05
  385. #define MWAIT_ECX_EXTENDED_INFO 0x01
  386. #define MWAIT_EDX_C1 0xf0
  387. static int __cpuinit mwait_usable(const struct cpuinfo_x86 *c)
  388. {
  389. u32 eax, ebx, ecx, edx;
  390. if (force_mwait)
  391. return 1;
  392. if (c->cpuid_level < MWAIT_INFO)
  393. return 0;
  394. cpuid(MWAIT_INFO, &eax, &ebx, &ecx, &edx);
  395. /* Check, whether EDX has extended info about MWAIT */
  396. if (!(ecx & MWAIT_ECX_EXTENDED_INFO))
  397. return 1;
  398. /*
  399. * edx enumeratios MONITOR/MWAIT extensions. Check, whether
  400. * C1 supports MWAIT
  401. */
  402. return (edx & MWAIT_EDX_C1);
  403. }
  404. /*
  405. * Check for AMD CPUs, which have potentially C1E support
  406. */
  407. static int __cpuinit check_c1e_idle(const struct cpuinfo_x86 *c)
  408. {
  409. if (c->x86_vendor != X86_VENDOR_AMD)
  410. return 0;
  411. if (c->x86 < 0x0F)
  412. return 0;
  413. /* Family 0x0f models < rev F do not have C1E */
  414. if (c->x86 == 0x0f && c->x86_model < 0x40)
  415. return 0;
  416. return 1;
  417. }
  418. static cpumask_var_t c1e_mask;
  419. static int c1e_detected;
  420. void c1e_remove_cpu(int cpu)
  421. {
  422. if (c1e_mask != NULL)
  423. cpumask_clear_cpu(cpu, c1e_mask);
  424. }
  425. /*
  426. * C1E aware idle routine. We check for C1E active in the interrupt
  427. * pending message MSR. If we detect C1E, then we handle it the same
  428. * way as C3 power states (local apic timer and TSC stop)
  429. */
  430. static void c1e_idle(void)
  431. {
  432. if (need_resched())
  433. return;
  434. if (!c1e_detected) {
  435. u32 lo, hi;
  436. rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
  437. if (lo & K8_INTP_C1E_ACTIVE_MASK) {
  438. c1e_detected = 1;
  439. if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  440. mark_tsc_unstable("TSC halt in AMD C1E");
  441. printk(KERN_INFO "System has AMD C1E enabled\n");
  442. set_cpu_cap(&boot_cpu_data, X86_FEATURE_AMDC1E);
  443. }
  444. }
  445. if (c1e_detected) {
  446. int cpu = smp_processor_id();
  447. if (!cpumask_test_cpu(cpu, c1e_mask)) {
  448. cpumask_set_cpu(cpu, c1e_mask);
  449. /*
  450. * Force broadcast so ACPI can not interfere. Needs
  451. * to run with interrupts enabled as it uses
  452. * smp_function_call.
  453. */
  454. local_irq_enable();
  455. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
  456. &cpu);
  457. printk(KERN_INFO "Switch to broadcast mode on CPU%d\n",
  458. cpu);
  459. local_irq_disable();
  460. }
  461. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
  462. default_idle();
  463. /*
  464. * The switch back from broadcast mode needs to be
  465. * called with interrupts disabled.
  466. */
  467. local_irq_disable();
  468. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
  469. local_irq_enable();
  470. } else
  471. default_idle();
  472. }
  473. void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
  474. {
  475. #ifdef CONFIG_SMP
  476. if (pm_idle == poll_idle && smp_num_siblings > 1) {
  477. printk(KERN_WARNING "WARNING: polling idle and HT enabled,"
  478. " performance may degrade.\n");
  479. }
  480. #endif
  481. if (pm_idle)
  482. return;
  483. if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) {
  484. /*
  485. * One CPU supports mwait => All CPUs supports mwait
  486. */
  487. printk(KERN_INFO "using mwait in idle threads.\n");
  488. pm_idle = mwait_idle;
  489. } else if (check_c1e_idle(c)) {
  490. printk(KERN_INFO "using C1E aware idle routine\n");
  491. pm_idle = c1e_idle;
  492. } else
  493. pm_idle = default_idle;
  494. }
  495. void __init init_c1e_mask(void)
  496. {
  497. /* If we're using c1e_idle, we need to allocate c1e_mask. */
  498. if (pm_idle == c1e_idle) {
  499. alloc_cpumask_var(&c1e_mask, GFP_KERNEL);
  500. cpumask_clear(c1e_mask);
  501. }
  502. }
  503. static int __init idle_setup(char *str)
  504. {
  505. if (!str)
  506. return -EINVAL;
  507. if (!strcmp(str, "poll")) {
  508. printk("using polling idle threads.\n");
  509. pm_idle = poll_idle;
  510. } else if (!strcmp(str, "mwait"))
  511. force_mwait = 1;
  512. else if (!strcmp(str, "halt")) {
  513. /*
  514. * When the boot option of idle=halt is added, halt is
  515. * forced to be used for CPU idle. In such case CPU C2/C3
  516. * won't be used again.
  517. * To continue to load the CPU idle driver, don't touch
  518. * the boot_option_idle_override.
  519. */
  520. pm_idle = default_idle;
  521. idle_halt = 1;
  522. return 0;
  523. } else if (!strcmp(str, "nomwait")) {
  524. /*
  525. * If the boot option of "idle=nomwait" is added,
  526. * it means that mwait will be disabled for CPU C2/C3
  527. * states. In such case it won't touch the variable
  528. * of boot_option_idle_override.
  529. */
  530. idle_nomwait = 1;
  531. return 0;
  532. } else
  533. return -1;
  534. boot_option_idle_override = 1;
  535. return 0;
  536. }
  537. early_param("idle", idle_setup);