process.c 14 KB

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