process.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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 <linux/user-return-notifier.h>
  13. #include <linux/dmi.h>
  14. #include <linux/utsname.h>
  15. #include <trace/events/power.h>
  16. #include <linux/hw_breakpoint.h>
  17. #include <asm/system.h>
  18. #include <asm/apic.h>
  19. #include <asm/syscalls.h>
  20. #include <asm/idle.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/i387.h>
  23. #include <asm/ds.h>
  24. #include <asm/debugreg.h>
  25. unsigned long idle_halt;
  26. EXPORT_SYMBOL(idle_halt);
  27. unsigned long idle_nomwait;
  28. EXPORT_SYMBOL(idle_nomwait);
  29. struct kmem_cache *task_xstate_cachep;
  30. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  31. {
  32. *dst = *src;
  33. if (src->thread.xstate) {
  34. dst->thread.xstate = kmem_cache_alloc(task_xstate_cachep,
  35. GFP_KERNEL);
  36. if (!dst->thread.xstate)
  37. return -ENOMEM;
  38. WARN_ON((unsigned long)dst->thread.xstate & 15);
  39. memcpy(dst->thread.xstate, src->thread.xstate, xstate_size);
  40. }
  41. return 0;
  42. }
  43. void free_thread_xstate(struct task_struct *tsk)
  44. {
  45. if (tsk->thread.xstate) {
  46. kmem_cache_free(task_xstate_cachep, tsk->thread.xstate);
  47. tsk->thread.xstate = NULL;
  48. }
  49. WARN(tsk->thread.ds_ctx, "leaking DS context\n");
  50. }
  51. void free_thread_info(struct thread_info *ti)
  52. {
  53. free_thread_xstate(ti->task);
  54. free_pages((unsigned long)ti, get_order(THREAD_SIZE));
  55. }
  56. void arch_task_cache_init(void)
  57. {
  58. task_xstate_cachep =
  59. kmem_cache_create("task_xstate", xstate_size,
  60. __alignof__(union thread_xstate),
  61. SLAB_PANIC | SLAB_NOTRACK, NULL);
  62. }
  63. /*
  64. * Free current thread data structures etc..
  65. */
  66. void exit_thread(void)
  67. {
  68. struct task_struct *me = current;
  69. struct thread_struct *t = &me->thread;
  70. unsigned long *bp = t->io_bitmap_ptr;
  71. if (bp) {
  72. struct tss_struct *tss = &per_cpu(init_tss, get_cpu());
  73. t->io_bitmap_ptr = NULL;
  74. clear_thread_flag(TIF_IO_BITMAP);
  75. /*
  76. * Careful, clear this in the TSS too:
  77. */
  78. memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
  79. t->io_bitmap_max = 0;
  80. put_cpu();
  81. kfree(bp);
  82. }
  83. }
  84. void show_regs_common(void)
  85. {
  86. const char *board, *product;
  87. board = dmi_get_system_info(DMI_BOARD_NAME);
  88. if (!board)
  89. board = "";
  90. product = dmi_get_system_info(DMI_PRODUCT_NAME);
  91. if (!product)
  92. product = "";
  93. printk("\n");
  94. printk(KERN_INFO "Pid: %d, comm: %.20s %s %s %.*s %s/%s\n",
  95. current->pid, current->comm, print_tainted(),
  96. init_utsname()->release,
  97. (int)strcspn(init_utsname()->version, " "),
  98. init_utsname()->version, board, product);
  99. }
  100. void flush_thread(void)
  101. {
  102. struct task_struct *tsk = current;
  103. #ifdef CONFIG_X86_64
  104. if (test_tsk_thread_flag(tsk, TIF_ABI_PENDING)) {
  105. clear_tsk_thread_flag(tsk, TIF_ABI_PENDING);
  106. if (test_tsk_thread_flag(tsk, TIF_IA32)) {
  107. clear_tsk_thread_flag(tsk, TIF_IA32);
  108. } else {
  109. set_tsk_thread_flag(tsk, TIF_IA32);
  110. current_thread_info()->status |= TS_COMPAT;
  111. }
  112. }
  113. #endif
  114. flush_ptrace_hw_breakpoint(tsk);
  115. memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
  116. /*
  117. * Forget coprocessor state..
  118. */
  119. tsk->fpu_counter = 0;
  120. clear_fpu(tsk);
  121. clear_used_math();
  122. }
  123. static void hard_disable_TSC(void)
  124. {
  125. write_cr4(read_cr4() | X86_CR4_TSD);
  126. }
  127. void disable_TSC(void)
  128. {
  129. preempt_disable();
  130. if (!test_and_set_thread_flag(TIF_NOTSC))
  131. /*
  132. * Must flip the CPU state synchronously with
  133. * TIF_NOTSC in the current running context.
  134. */
  135. hard_disable_TSC();
  136. preempt_enable();
  137. }
  138. static void hard_enable_TSC(void)
  139. {
  140. write_cr4(read_cr4() & ~X86_CR4_TSD);
  141. }
  142. static void enable_TSC(void)
  143. {
  144. preempt_disable();
  145. if (test_and_clear_thread_flag(TIF_NOTSC))
  146. /*
  147. * Must flip the CPU state synchronously with
  148. * TIF_NOTSC in the current running context.
  149. */
  150. hard_enable_TSC();
  151. preempt_enable();
  152. }
  153. int get_tsc_mode(unsigned long adr)
  154. {
  155. unsigned int val;
  156. if (test_thread_flag(TIF_NOTSC))
  157. val = PR_TSC_SIGSEGV;
  158. else
  159. val = PR_TSC_ENABLE;
  160. return put_user(val, (unsigned int __user *)adr);
  161. }
  162. int set_tsc_mode(unsigned int val)
  163. {
  164. if (val == PR_TSC_SIGSEGV)
  165. disable_TSC();
  166. else if (val == PR_TSC_ENABLE)
  167. enable_TSC();
  168. else
  169. return -EINVAL;
  170. return 0;
  171. }
  172. void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
  173. struct tss_struct *tss)
  174. {
  175. struct thread_struct *prev, *next;
  176. prev = &prev_p->thread;
  177. next = &next_p->thread;
  178. if (test_tsk_thread_flag(next_p, TIF_DS_AREA_MSR) ||
  179. test_tsk_thread_flag(prev_p, TIF_DS_AREA_MSR))
  180. ds_switch_to(prev_p, next_p);
  181. else if (next->debugctlmsr != prev->debugctlmsr)
  182. update_debugctlmsr(next->debugctlmsr);
  183. if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
  184. test_tsk_thread_flag(next_p, TIF_NOTSC)) {
  185. /* prev and next are different */
  186. if (test_tsk_thread_flag(next_p, TIF_NOTSC))
  187. hard_disable_TSC();
  188. else
  189. hard_enable_TSC();
  190. }
  191. if (test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
  192. /*
  193. * Copy the relevant range of the IO bitmap.
  194. * Normally this is 128 bytes or less:
  195. */
  196. memcpy(tss->io_bitmap, next->io_bitmap_ptr,
  197. max(prev->io_bitmap_max, next->io_bitmap_max));
  198. } else if (test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)) {
  199. /*
  200. * Clear any possible leftover bits:
  201. */
  202. memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);
  203. }
  204. propagate_user_return_notify(prev_p, next_p);
  205. }
  206. int sys_fork(struct pt_regs *regs)
  207. {
  208. return do_fork(SIGCHLD, regs->sp, regs, 0, NULL, NULL);
  209. }
  210. /*
  211. * This is trivial, and on the face of it looks like it
  212. * could equally well be done in user mode.
  213. *
  214. * Not so, for quite unobvious reasons - register pressure.
  215. * In user mode vfork() cannot have a stack frame, and if
  216. * done by calling the "clone()" system call directly, you
  217. * do not have enough call-clobbered registers to hold all
  218. * the information you need.
  219. */
  220. int sys_vfork(struct pt_regs *regs)
  221. {
  222. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->sp, regs, 0,
  223. NULL, NULL);
  224. }
  225. /*
  226. * Idle related variables and functions
  227. */
  228. unsigned long boot_option_idle_override = 0;
  229. EXPORT_SYMBOL(boot_option_idle_override);
  230. /*
  231. * Powermanagement idle function, if any..
  232. */
  233. void (*pm_idle)(void);
  234. EXPORT_SYMBOL(pm_idle);
  235. #ifdef CONFIG_X86_32
  236. /*
  237. * This halt magic was a workaround for ancient floppy DMA
  238. * wreckage. It should be safe to remove.
  239. */
  240. static int hlt_counter;
  241. void disable_hlt(void)
  242. {
  243. hlt_counter++;
  244. }
  245. EXPORT_SYMBOL(disable_hlt);
  246. void enable_hlt(void)
  247. {
  248. hlt_counter--;
  249. }
  250. EXPORT_SYMBOL(enable_hlt);
  251. static inline int hlt_use_halt(void)
  252. {
  253. return (!hlt_counter && boot_cpu_data.hlt_works_ok);
  254. }
  255. #else
  256. static inline int hlt_use_halt(void)
  257. {
  258. return 1;
  259. }
  260. #endif
  261. /*
  262. * We use this if we don't have any better
  263. * idle routine..
  264. */
  265. void default_idle(void)
  266. {
  267. if (hlt_use_halt()) {
  268. trace_power_start(POWER_CSTATE, 1);
  269. current_thread_info()->status &= ~TS_POLLING;
  270. /*
  271. * TS_POLLING-cleared state must be visible before we
  272. * test NEED_RESCHED:
  273. */
  274. smp_mb();
  275. if (!need_resched())
  276. safe_halt(); /* enables interrupts racelessly */
  277. else
  278. local_irq_enable();
  279. current_thread_info()->status |= TS_POLLING;
  280. } else {
  281. local_irq_enable();
  282. /* loop is done by the caller */
  283. cpu_relax();
  284. }
  285. }
  286. #ifdef CONFIG_APM_MODULE
  287. EXPORT_SYMBOL(default_idle);
  288. #endif
  289. void stop_this_cpu(void *dummy)
  290. {
  291. local_irq_disable();
  292. /*
  293. * Remove this CPU:
  294. */
  295. set_cpu_online(smp_processor_id(), false);
  296. disable_local_APIC();
  297. for (;;) {
  298. if (hlt_works(smp_processor_id()))
  299. halt();
  300. }
  301. }
  302. static void do_nothing(void *unused)
  303. {
  304. }
  305. /*
  306. * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
  307. * pm_idle and update to new pm_idle value. Required while changing pm_idle
  308. * handler on SMP systems.
  309. *
  310. * Caller must have changed pm_idle to the new value before the call. Old
  311. * pm_idle value will not be used by any CPU after the return of this function.
  312. */
  313. void cpu_idle_wait(void)
  314. {
  315. smp_mb();
  316. /* kick all the CPUs so that they exit out of pm_idle */
  317. smp_call_function(do_nothing, NULL, 1);
  318. }
  319. EXPORT_SYMBOL_GPL(cpu_idle_wait);
  320. /*
  321. * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
  322. * which can obviate IPI to trigger checking of need_resched.
  323. * We execute MONITOR against need_resched and enter optimized wait state
  324. * through MWAIT. Whenever someone changes need_resched, we would be woken
  325. * up from MWAIT (without an IPI).
  326. *
  327. * New with Core Duo processors, MWAIT can take some hints based on CPU
  328. * capability.
  329. */
  330. void mwait_idle_with_hints(unsigned long ax, unsigned long cx)
  331. {
  332. trace_power_start(POWER_CSTATE, (ax>>4)+1);
  333. if (!need_resched()) {
  334. if (cpu_has(&current_cpu_data, X86_FEATURE_CLFLUSH_MONITOR))
  335. clflush((void *)&current_thread_info()->flags);
  336. __monitor((void *)&current_thread_info()->flags, 0, 0);
  337. smp_mb();
  338. if (!need_resched())
  339. __mwait(ax, cx);
  340. }
  341. }
  342. /* Default MONITOR/MWAIT with no hints, used for default C1 state */
  343. static void mwait_idle(void)
  344. {
  345. if (!need_resched()) {
  346. trace_power_start(POWER_CSTATE, 1);
  347. if (cpu_has(&current_cpu_data, X86_FEATURE_CLFLUSH_MONITOR))
  348. clflush((void *)&current_thread_info()->flags);
  349. __monitor((void *)&current_thread_info()->flags, 0, 0);
  350. smp_mb();
  351. if (!need_resched())
  352. __sti_mwait(0, 0);
  353. else
  354. local_irq_enable();
  355. } else
  356. local_irq_enable();
  357. }
  358. /*
  359. * On SMP it's slightly faster (but much more power-consuming!)
  360. * to poll the ->work.need_resched flag instead of waiting for the
  361. * cross-CPU IPI to arrive. Use this option with caution.
  362. */
  363. static void poll_idle(void)
  364. {
  365. trace_power_start(POWER_CSTATE, 0);
  366. local_irq_enable();
  367. while (!need_resched())
  368. cpu_relax();
  369. trace_power_end(0);
  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.
  451. */
  452. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
  453. &cpu);
  454. printk(KERN_INFO "Switch to broadcast mode on CPU%d\n",
  455. cpu);
  456. }
  457. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
  458. default_idle();
  459. /*
  460. * The switch back from broadcast mode needs to be
  461. * called with interrupts disabled.
  462. */
  463. local_irq_disable();
  464. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
  465. local_irq_enable();
  466. } else
  467. default_idle();
  468. }
  469. void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
  470. {
  471. #ifdef CONFIG_SMP
  472. if (pm_idle == poll_idle && smp_num_siblings > 1) {
  473. printk(KERN_WARNING "WARNING: polling idle and HT enabled,"
  474. " performance may degrade.\n");
  475. }
  476. #endif
  477. if (pm_idle)
  478. return;
  479. if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) {
  480. /*
  481. * One CPU supports mwait => All CPUs supports mwait
  482. */
  483. printk(KERN_INFO "using mwait in idle threads.\n");
  484. pm_idle = mwait_idle;
  485. } else if (check_c1e_idle(c)) {
  486. printk(KERN_INFO "using C1E aware idle routine\n");
  487. pm_idle = c1e_idle;
  488. } else
  489. pm_idle = default_idle;
  490. }
  491. void __init init_c1e_mask(void)
  492. {
  493. /* If we're using c1e_idle, we need to allocate c1e_mask. */
  494. if (pm_idle == c1e_idle)
  495. zalloc_cpumask_var(&c1e_mask, GFP_KERNEL);
  496. }
  497. static int __init idle_setup(char *str)
  498. {
  499. if (!str)
  500. return -EINVAL;
  501. if (!strcmp(str, "poll")) {
  502. printk("using polling idle threads.\n");
  503. pm_idle = poll_idle;
  504. } else if (!strcmp(str, "mwait"))
  505. force_mwait = 1;
  506. else if (!strcmp(str, "halt")) {
  507. /*
  508. * When the boot option of idle=halt is added, halt is
  509. * forced to be used for CPU idle. In such case CPU C2/C3
  510. * won't be used again.
  511. * To continue to load the CPU idle driver, don't touch
  512. * the boot_option_idle_override.
  513. */
  514. pm_idle = default_idle;
  515. idle_halt = 1;
  516. return 0;
  517. } else if (!strcmp(str, "nomwait")) {
  518. /*
  519. * If the boot option of "idle=nomwait" is added,
  520. * it means that mwait will be disabled for CPU C2/C3
  521. * states. In such case it won't touch the variable
  522. * of boot_option_idle_override.
  523. */
  524. idle_nomwait = 1;
  525. return 0;
  526. } else
  527. return -1;
  528. boot_option_idle_override = 1;
  529. return 0;
  530. }
  531. early_param("idle", idle_setup);
  532. unsigned long arch_align_stack(unsigned long sp)
  533. {
  534. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  535. sp -= get_random_int() % 8192;
  536. return sp & ~0xf;
  537. }
  538. unsigned long arch_randomize_brk(struct mm_struct *mm)
  539. {
  540. unsigned long range_end = mm->brk + 0x02000000;
  541. return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
  542. }