process.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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 <trace/events/power.h>
  13. #include <asm/system.h>
  14. #include <asm/apic.h>
  15. #include <asm/syscalls.h>
  16. #include <asm/idle.h>
  17. #include <asm/uaccess.h>
  18. #include <asm/i387.h>
  19. #include <asm/ds.h>
  20. unsigned long idle_halt;
  21. EXPORT_SYMBOL(idle_halt);
  22. unsigned long idle_nomwait;
  23. EXPORT_SYMBOL(idle_nomwait);
  24. struct kmem_cache *task_xstate_cachep;
  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. WARN(tsk->thread.ds_ctx, "leaking DS context\n");
  45. }
  46. void free_thread_info(struct thread_info *ti)
  47. {
  48. free_thread_xstate(ti->task);
  49. free_pages((unsigned long)ti, get_order(THREAD_SIZE));
  50. }
  51. void arch_task_cache_init(void)
  52. {
  53. task_xstate_cachep =
  54. kmem_cache_create("task_xstate", xstate_size,
  55. __alignof__(union thread_xstate),
  56. SLAB_PANIC | SLAB_NOTRACK, NULL);
  57. }
  58. /*
  59. * Free current thread data structures etc..
  60. */
  61. void exit_thread(void)
  62. {
  63. struct task_struct *me = current;
  64. struct thread_struct *t = &me->thread;
  65. unsigned long *bp = t->io_bitmap_ptr;
  66. if (bp) {
  67. struct tss_struct *tss = &per_cpu(init_tss, get_cpu());
  68. t->io_bitmap_ptr = NULL;
  69. clear_thread_flag(TIF_IO_BITMAP);
  70. /*
  71. * Careful, clear this in the TSS too:
  72. */
  73. memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
  74. t->io_bitmap_max = 0;
  75. put_cpu();
  76. kfree(bp);
  77. }
  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. trace_power_start(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. } else {
  274. local_irq_enable();
  275. /* loop is done by the caller */
  276. cpu_relax();
  277. }
  278. }
  279. #ifdef CONFIG_APM_MODULE
  280. EXPORT_SYMBOL(default_idle);
  281. #endif
  282. void stop_this_cpu(void *dummy)
  283. {
  284. local_irq_disable();
  285. /*
  286. * Remove this CPU:
  287. */
  288. set_cpu_online(smp_processor_id(), false);
  289. disable_local_APIC();
  290. for (;;) {
  291. if (hlt_works(smp_processor_id()))
  292. halt();
  293. }
  294. }
  295. static void do_nothing(void *unused)
  296. {
  297. }
  298. /*
  299. * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
  300. * pm_idle and update to new pm_idle value. Required while changing pm_idle
  301. * handler on SMP systems.
  302. *
  303. * Caller must have changed pm_idle to the new value before the call. Old
  304. * pm_idle value will not be used by any CPU after the return of this function.
  305. */
  306. void cpu_idle_wait(void)
  307. {
  308. smp_mb();
  309. /* kick all the CPUs so that they exit out of pm_idle */
  310. smp_call_function(do_nothing, NULL, 1);
  311. }
  312. EXPORT_SYMBOL_GPL(cpu_idle_wait);
  313. /*
  314. * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
  315. * which can obviate IPI to trigger checking of need_resched.
  316. * We execute MONITOR against need_resched and enter optimized wait state
  317. * through MWAIT. Whenever someone changes need_resched, we would be woken
  318. * up from MWAIT (without an IPI).
  319. *
  320. * New with Core Duo processors, MWAIT can take some hints based on CPU
  321. * capability.
  322. */
  323. void mwait_idle_with_hints(unsigned long ax, unsigned long cx)
  324. {
  325. trace_power_start(POWER_CSTATE, (ax>>4)+1);
  326. if (!need_resched()) {
  327. if (cpu_has(&current_cpu_data, X86_FEATURE_CLFLUSH_MONITOR))
  328. clflush((void *)&current_thread_info()->flags);
  329. __monitor((void *)&current_thread_info()->flags, 0, 0);
  330. smp_mb();
  331. if (!need_resched())
  332. __mwait(ax, cx);
  333. }
  334. }
  335. /* Default MONITOR/MWAIT with no hints, used for default C1 state */
  336. static void mwait_idle(void)
  337. {
  338. if (!need_resched()) {
  339. trace_power_start(POWER_CSTATE, 1);
  340. if (cpu_has(&current_cpu_data, X86_FEATURE_CLFLUSH_MONITOR))
  341. clflush((void *)&current_thread_info()->flags);
  342. __monitor((void *)&current_thread_info()->flags, 0, 0);
  343. smp_mb();
  344. if (!need_resched())
  345. __sti_mwait(0, 0);
  346. else
  347. local_irq_enable();
  348. } else
  349. local_irq_enable();
  350. }
  351. /*
  352. * On SMP it's slightly faster (but much more power-consuming!)
  353. * to poll the ->work.need_resched flag instead of waiting for the
  354. * cross-CPU IPI to arrive. Use this option with caution.
  355. */
  356. static void poll_idle(void)
  357. {
  358. trace_power_start(POWER_CSTATE, 0);
  359. local_irq_enable();
  360. while (!need_resched())
  361. cpu_relax();
  362. trace_power_end(0);
  363. }
  364. /*
  365. * mwait selection logic:
  366. *
  367. * It depends on the CPU. For AMD CPUs that support MWAIT this is
  368. * wrong. Family 0x10 and 0x11 CPUs will enter C1 on HLT. Powersavings
  369. * then depend on a clock divisor and current Pstate of the core. If
  370. * all cores of a processor are in halt state (C1) the processor can
  371. * enter the C1E (C1 enhanced) state. If mwait is used this will never
  372. * happen.
  373. *
  374. * idle=mwait overrides this decision and forces the usage of mwait.
  375. */
  376. static int __cpuinitdata force_mwait;
  377. #define MWAIT_INFO 0x05
  378. #define MWAIT_ECX_EXTENDED_INFO 0x01
  379. #define MWAIT_EDX_C1 0xf0
  380. static int __cpuinit mwait_usable(const struct cpuinfo_x86 *c)
  381. {
  382. u32 eax, ebx, ecx, edx;
  383. if (force_mwait)
  384. return 1;
  385. if (c->cpuid_level < MWAIT_INFO)
  386. return 0;
  387. cpuid(MWAIT_INFO, &eax, &ebx, &ecx, &edx);
  388. /* Check, whether EDX has extended info about MWAIT */
  389. if (!(ecx & MWAIT_ECX_EXTENDED_INFO))
  390. return 1;
  391. /*
  392. * edx enumeratios MONITOR/MWAIT extensions. Check, whether
  393. * C1 supports MWAIT
  394. */
  395. return (edx & MWAIT_EDX_C1);
  396. }
  397. /*
  398. * Check for AMD CPUs, which have potentially C1E support
  399. */
  400. static int __cpuinit check_c1e_idle(const struct cpuinfo_x86 *c)
  401. {
  402. if (c->x86_vendor != X86_VENDOR_AMD)
  403. return 0;
  404. if (c->x86 < 0x0F)
  405. return 0;
  406. /* Family 0x0f models < rev F do not have C1E */
  407. if (c->x86 == 0x0f && c->x86_model < 0x40)
  408. return 0;
  409. return 1;
  410. }
  411. static cpumask_var_t c1e_mask;
  412. static int c1e_detected;
  413. void c1e_remove_cpu(int cpu)
  414. {
  415. if (c1e_mask != NULL)
  416. cpumask_clear_cpu(cpu, c1e_mask);
  417. }
  418. /*
  419. * C1E aware idle routine. We check for C1E active in the interrupt
  420. * pending message MSR. If we detect C1E, then we handle it the same
  421. * way as C3 power states (local apic timer and TSC stop)
  422. */
  423. static void c1e_idle(void)
  424. {
  425. if (need_resched())
  426. return;
  427. if (!c1e_detected) {
  428. u32 lo, hi;
  429. rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
  430. if (lo & K8_INTP_C1E_ACTIVE_MASK) {
  431. c1e_detected = 1;
  432. if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  433. mark_tsc_unstable("TSC halt in AMD C1E");
  434. printk(KERN_INFO "System has AMD C1E enabled\n");
  435. set_cpu_cap(&boot_cpu_data, X86_FEATURE_AMDC1E);
  436. }
  437. }
  438. if (c1e_detected) {
  439. int cpu = smp_processor_id();
  440. if (!cpumask_test_cpu(cpu, c1e_mask)) {
  441. cpumask_set_cpu(cpu, c1e_mask);
  442. /*
  443. * Force broadcast so ACPI can not interfere.
  444. */
  445. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
  446. &cpu);
  447. printk(KERN_INFO "Switch to broadcast mode on CPU%d\n",
  448. cpu);
  449. }
  450. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
  451. default_idle();
  452. /*
  453. * The switch back from broadcast mode needs to be
  454. * called with interrupts disabled.
  455. */
  456. local_irq_disable();
  457. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
  458. local_irq_enable();
  459. } else
  460. default_idle();
  461. }
  462. void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
  463. {
  464. #ifdef CONFIG_SMP
  465. if (pm_idle == poll_idle && smp_num_siblings > 1) {
  466. printk(KERN_WARNING "WARNING: polling idle and HT enabled,"
  467. " performance may degrade.\n");
  468. }
  469. #endif
  470. if (pm_idle)
  471. return;
  472. if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) {
  473. /*
  474. * One CPU supports mwait => All CPUs supports mwait
  475. */
  476. printk(KERN_INFO "using mwait in idle threads.\n");
  477. pm_idle = mwait_idle;
  478. } else if (check_c1e_idle(c)) {
  479. printk(KERN_INFO "using C1E aware idle routine\n");
  480. pm_idle = c1e_idle;
  481. } else
  482. pm_idle = default_idle;
  483. }
  484. void __init init_c1e_mask(void)
  485. {
  486. /* If we're using c1e_idle, we need to allocate c1e_mask. */
  487. if (pm_idle == c1e_idle)
  488. zalloc_cpumask_var(&c1e_mask, GFP_KERNEL);
  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);
  525. unsigned long arch_align_stack(unsigned long sp)
  526. {
  527. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  528. sp -= get_random_int() % 8192;
  529. return sp & ~0xf;
  530. }
  531. unsigned long arch_randomize_brk(struct mm_struct *mm)
  532. {
  533. unsigned long range_end = mm->brk + 0x02000000;
  534. return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
  535. }