process.c 14 KB

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