process.c 14 KB

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