process.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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/debugreg.h>
  24. struct kmem_cache *task_xstate_cachep;
  25. EXPORT_SYMBOL_GPL(task_xstate_cachep);
  26. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  27. {
  28. int ret;
  29. *dst = *src;
  30. if (fpu_allocated(&src->thread.fpu)) {
  31. memset(&dst->thread.fpu, 0, sizeof(dst->thread.fpu));
  32. ret = fpu_alloc(&dst->thread.fpu);
  33. if (ret)
  34. return ret;
  35. fpu_copy(&dst->thread.fpu, &src->thread.fpu);
  36. }
  37. return 0;
  38. }
  39. void free_thread_xstate(struct task_struct *tsk)
  40. {
  41. fpu_free(&tsk->thread.fpu);
  42. }
  43. void free_thread_info(struct thread_info *ti)
  44. {
  45. free_thread_xstate(ti->task);
  46. free_pages((unsigned long)ti, get_order(THREAD_SIZE));
  47. }
  48. void arch_task_cache_init(void)
  49. {
  50. task_xstate_cachep =
  51. kmem_cache_create("task_xstate", xstate_size,
  52. __alignof__(union thread_xstate),
  53. SLAB_PANIC | SLAB_NOTRACK, NULL);
  54. }
  55. /*
  56. * Free current thread data structures etc..
  57. */
  58. void exit_thread(void)
  59. {
  60. struct task_struct *me = current;
  61. struct thread_struct *t = &me->thread;
  62. unsigned long *bp = t->io_bitmap_ptr;
  63. if (bp) {
  64. struct tss_struct *tss = &per_cpu(init_tss, get_cpu());
  65. t->io_bitmap_ptr = NULL;
  66. clear_thread_flag(TIF_IO_BITMAP);
  67. /*
  68. * Careful, clear this in the TSS too:
  69. */
  70. memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
  71. t->io_bitmap_max = 0;
  72. put_cpu();
  73. kfree(bp);
  74. }
  75. }
  76. void show_regs(struct pt_regs *regs)
  77. {
  78. show_registers(regs);
  79. show_trace(NULL, regs, (unsigned long *)kernel_stack_pointer(regs));
  80. }
  81. void show_regs_common(void)
  82. {
  83. const char *board, *product;
  84. board = dmi_get_system_info(DMI_BOARD_NAME);
  85. if (!board)
  86. board = "";
  87. product = dmi_get_system_info(DMI_PRODUCT_NAME);
  88. if (!product)
  89. product = "";
  90. printk(KERN_CONT "\n");
  91. printk(KERN_DEFAULT "Pid: %d, comm: %.20s %s %s %.*s %s/%s\n",
  92. current->pid, current->comm, print_tainted(),
  93. init_utsname()->release,
  94. (int)strcspn(init_utsname()->version, " "),
  95. init_utsname()->version, board, product);
  96. }
  97. void flush_thread(void)
  98. {
  99. struct task_struct *tsk = current;
  100. flush_ptrace_hw_breakpoint(tsk);
  101. memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
  102. /*
  103. * Forget coprocessor state..
  104. */
  105. tsk->fpu_counter = 0;
  106. clear_fpu(tsk);
  107. clear_used_math();
  108. }
  109. static void hard_disable_TSC(void)
  110. {
  111. write_cr4(read_cr4() | X86_CR4_TSD);
  112. }
  113. void disable_TSC(void)
  114. {
  115. preempt_disable();
  116. if (!test_and_set_thread_flag(TIF_NOTSC))
  117. /*
  118. * Must flip the CPU state synchronously with
  119. * TIF_NOTSC in the current running context.
  120. */
  121. hard_disable_TSC();
  122. preempt_enable();
  123. }
  124. static void hard_enable_TSC(void)
  125. {
  126. write_cr4(read_cr4() & ~X86_CR4_TSD);
  127. }
  128. static void enable_TSC(void)
  129. {
  130. preempt_disable();
  131. if (test_and_clear_thread_flag(TIF_NOTSC))
  132. /*
  133. * Must flip the CPU state synchronously with
  134. * TIF_NOTSC in the current running context.
  135. */
  136. hard_enable_TSC();
  137. preempt_enable();
  138. }
  139. int get_tsc_mode(unsigned long adr)
  140. {
  141. unsigned int val;
  142. if (test_thread_flag(TIF_NOTSC))
  143. val = PR_TSC_SIGSEGV;
  144. else
  145. val = PR_TSC_ENABLE;
  146. return put_user(val, (unsigned int __user *)adr);
  147. }
  148. int set_tsc_mode(unsigned int val)
  149. {
  150. if (val == PR_TSC_SIGSEGV)
  151. disable_TSC();
  152. else if (val == PR_TSC_ENABLE)
  153. enable_TSC();
  154. else
  155. return -EINVAL;
  156. return 0;
  157. }
  158. void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
  159. struct tss_struct *tss)
  160. {
  161. struct thread_struct *prev, *next;
  162. prev = &prev_p->thread;
  163. next = &next_p->thread;
  164. if (test_tsk_thread_flag(prev_p, TIF_BLOCKSTEP) ^
  165. test_tsk_thread_flag(next_p, TIF_BLOCKSTEP)) {
  166. unsigned long debugctl = get_debugctlmsr();
  167. debugctl &= ~DEBUGCTLMSR_BTF;
  168. if (test_tsk_thread_flag(next_p, TIF_BLOCKSTEP))
  169. debugctl |= DEBUGCTLMSR_BTF;
  170. update_debugctlmsr(debugctl);
  171. }
  172. if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
  173. test_tsk_thread_flag(next_p, TIF_NOTSC)) {
  174. /* prev and next are different */
  175. if (test_tsk_thread_flag(next_p, TIF_NOTSC))
  176. hard_disable_TSC();
  177. else
  178. hard_enable_TSC();
  179. }
  180. if (test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
  181. /*
  182. * Copy the relevant range of the IO bitmap.
  183. * Normally this is 128 bytes or less:
  184. */
  185. memcpy(tss->io_bitmap, next->io_bitmap_ptr,
  186. max(prev->io_bitmap_max, next->io_bitmap_max));
  187. } else if (test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)) {
  188. /*
  189. * Clear any possible leftover bits:
  190. */
  191. memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);
  192. }
  193. propagate_user_return_notify(prev_p, next_p);
  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. long
  215. sys_clone(unsigned long clone_flags, unsigned long newsp,
  216. void __user *parent_tid, void __user *child_tid, struct pt_regs *regs)
  217. {
  218. if (!newsp)
  219. newsp = regs->sp;
  220. return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
  221. }
  222. /*
  223. * This gets run with %si containing the
  224. * function to call, and %di containing
  225. * the "args".
  226. */
  227. extern void kernel_thread_helper(void);
  228. /*
  229. * Create a kernel thread
  230. */
  231. int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  232. {
  233. struct pt_regs regs;
  234. memset(&regs, 0, sizeof(regs));
  235. regs.si = (unsigned long) fn;
  236. regs.di = (unsigned long) arg;
  237. #ifdef CONFIG_X86_32
  238. regs.ds = __USER_DS;
  239. regs.es = __USER_DS;
  240. regs.fs = __KERNEL_PERCPU;
  241. regs.gs = __KERNEL_STACK_CANARY;
  242. #else
  243. regs.ss = __KERNEL_DS;
  244. #endif
  245. regs.orig_ax = -1;
  246. regs.ip = (unsigned long) kernel_thread_helper;
  247. regs.cs = __KERNEL_CS | get_kernel_rpl();
  248. regs.flags = X86_EFLAGS_IF | 0x2;
  249. /* Ok, create the new process.. */
  250. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
  251. }
  252. EXPORT_SYMBOL(kernel_thread);
  253. /*
  254. * sys_execve() executes a new program.
  255. */
  256. long sys_execve(const char __user *name,
  257. const char __user *const __user *argv,
  258. const char __user *const __user *envp, struct pt_regs *regs)
  259. {
  260. long error;
  261. char *filename;
  262. filename = getname(name);
  263. error = PTR_ERR(filename);
  264. if (IS_ERR(filename))
  265. return error;
  266. error = do_execve(filename, argv, envp, regs);
  267. #ifdef CONFIG_X86_32
  268. if (error == 0) {
  269. /* Make sure we don't return using sysenter.. */
  270. set_thread_flag(TIF_IRET);
  271. }
  272. #endif
  273. putname(filename);
  274. return error;
  275. }
  276. /*
  277. * Idle related variables and functions
  278. */
  279. unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE;
  280. EXPORT_SYMBOL(boot_option_idle_override);
  281. /*
  282. * Powermanagement idle function, if any..
  283. */
  284. void (*pm_idle)(void);
  285. EXPORT_SYMBOL(pm_idle);
  286. #ifdef CONFIG_X86_32
  287. /*
  288. * This halt magic was a workaround for ancient floppy DMA
  289. * wreckage. It should be safe to remove.
  290. */
  291. static int hlt_counter;
  292. void disable_hlt(void)
  293. {
  294. hlt_counter++;
  295. }
  296. EXPORT_SYMBOL(disable_hlt);
  297. void enable_hlt(void)
  298. {
  299. hlt_counter--;
  300. }
  301. EXPORT_SYMBOL(enable_hlt);
  302. static inline int hlt_use_halt(void)
  303. {
  304. return (!hlt_counter && boot_cpu_data.hlt_works_ok);
  305. }
  306. #else
  307. static inline int hlt_use_halt(void)
  308. {
  309. return 1;
  310. }
  311. #endif
  312. /*
  313. * We use this if we don't have any better
  314. * idle routine..
  315. */
  316. void default_idle(void)
  317. {
  318. if (hlt_use_halt()) {
  319. trace_power_start(POWER_CSTATE, 1, smp_processor_id());
  320. trace_cpu_idle(1, smp_processor_id());
  321. current_thread_info()->status &= ~TS_POLLING;
  322. /*
  323. * TS_POLLING-cleared state must be visible before we
  324. * test NEED_RESCHED:
  325. */
  326. smp_mb();
  327. if (!need_resched())
  328. safe_halt(); /* enables interrupts racelessly */
  329. else
  330. local_irq_enable();
  331. current_thread_info()->status |= TS_POLLING;
  332. trace_power_end(smp_processor_id());
  333. trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
  334. } else {
  335. local_irq_enable();
  336. /* loop is done by the caller */
  337. cpu_relax();
  338. }
  339. }
  340. #ifdef CONFIG_APM_MODULE
  341. EXPORT_SYMBOL(default_idle);
  342. #endif
  343. void stop_this_cpu(void *dummy)
  344. {
  345. local_irq_disable();
  346. /*
  347. * Remove this CPU:
  348. */
  349. set_cpu_online(smp_processor_id(), false);
  350. disable_local_APIC();
  351. for (;;) {
  352. if (hlt_works(smp_processor_id()))
  353. halt();
  354. }
  355. }
  356. static void do_nothing(void *unused)
  357. {
  358. }
  359. /*
  360. * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
  361. * pm_idle and update to new pm_idle value. Required while changing pm_idle
  362. * handler on SMP systems.
  363. *
  364. * Caller must have changed pm_idle to the new value before the call. Old
  365. * pm_idle value will not be used by any CPU after the return of this function.
  366. */
  367. void cpu_idle_wait(void)
  368. {
  369. smp_mb();
  370. /* kick all the CPUs so that they exit out of pm_idle */
  371. smp_call_function(do_nothing, NULL, 1);
  372. }
  373. EXPORT_SYMBOL_GPL(cpu_idle_wait);
  374. /*
  375. * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
  376. * which can obviate IPI to trigger checking of need_resched.
  377. * We execute MONITOR against need_resched and enter optimized wait state
  378. * through MWAIT. Whenever someone changes need_resched, we would be woken
  379. * up from MWAIT (without an IPI).
  380. *
  381. * New with Core Duo processors, MWAIT can take some hints based on CPU
  382. * capability.
  383. */
  384. void mwait_idle_with_hints(unsigned long ax, unsigned long cx)
  385. {
  386. if (!need_resched()) {
  387. if (cpu_has(__this_cpu_ptr(&cpu_info), X86_FEATURE_CLFLUSH_MONITOR))
  388. clflush((void *)&current_thread_info()->flags);
  389. __monitor((void *)&current_thread_info()->flags, 0, 0);
  390. smp_mb();
  391. if (!need_resched())
  392. __mwait(ax, cx);
  393. }
  394. }
  395. /* Default MONITOR/MWAIT with no hints, used for default C1 state */
  396. static void mwait_idle(void)
  397. {
  398. if (!need_resched()) {
  399. trace_power_start(POWER_CSTATE, 1, smp_processor_id());
  400. trace_cpu_idle(1, smp_processor_id());
  401. if (cpu_has(__this_cpu_ptr(&cpu_info), X86_FEATURE_CLFLUSH_MONITOR))
  402. clflush((void *)&current_thread_info()->flags);
  403. __monitor((void *)&current_thread_info()->flags, 0, 0);
  404. smp_mb();
  405. if (!need_resched())
  406. __sti_mwait(0, 0);
  407. else
  408. local_irq_enable();
  409. trace_power_end(smp_processor_id());
  410. trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
  411. } else
  412. local_irq_enable();
  413. }
  414. /*
  415. * On SMP it's slightly faster (but much more power-consuming!)
  416. * to poll the ->work.need_resched flag instead of waiting for the
  417. * cross-CPU IPI to arrive. Use this option with caution.
  418. */
  419. static void poll_idle(void)
  420. {
  421. trace_power_start(POWER_CSTATE, 0, smp_processor_id());
  422. trace_cpu_idle(0, smp_processor_id());
  423. local_irq_enable();
  424. while (!need_resched())
  425. cpu_relax();
  426. trace_power_end(smp_processor_id());
  427. trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
  428. }
  429. /*
  430. * mwait selection logic:
  431. *
  432. * It depends on the CPU. For AMD CPUs that support MWAIT this is
  433. * wrong. Family 0x10 and 0x11 CPUs will enter C1 on HLT. Powersavings
  434. * then depend on a clock divisor and current Pstate of the core. If
  435. * all cores of a processor are in halt state (C1) the processor can
  436. * enter the C1E (C1 enhanced) state. If mwait is used this will never
  437. * happen.
  438. *
  439. * idle=mwait overrides this decision and forces the usage of mwait.
  440. */
  441. #define MWAIT_INFO 0x05
  442. #define MWAIT_ECX_EXTENDED_INFO 0x01
  443. #define MWAIT_EDX_C1 0xf0
  444. static int __cpuinit mwait_usable(const struct cpuinfo_x86 *c)
  445. {
  446. u32 eax, ebx, ecx, edx;
  447. if (boot_option_idle_override == IDLE_FORCE_MWAIT)
  448. return 1;
  449. if (c->cpuid_level < MWAIT_INFO)
  450. return 0;
  451. cpuid(MWAIT_INFO, &eax, &ebx, &ecx, &edx);
  452. /* Check, whether EDX has extended info about MWAIT */
  453. if (!(ecx & MWAIT_ECX_EXTENDED_INFO))
  454. return 1;
  455. /*
  456. * edx enumeratios MONITOR/MWAIT extensions. Check, whether
  457. * C1 supports MWAIT
  458. */
  459. return (edx & MWAIT_EDX_C1);
  460. }
  461. bool c1e_detected;
  462. EXPORT_SYMBOL(c1e_detected);
  463. static cpumask_var_t c1e_mask;
  464. void c1e_remove_cpu(int cpu)
  465. {
  466. if (c1e_mask != NULL)
  467. cpumask_clear_cpu(cpu, c1e_mask);
  468. }
  469. /*
  470. * C1E aware idle routine. We check for C1E active in the interrupt
  471. * pending message MSR. If we detect C1E, then we handle it the same
  472. * way as C3 power states (local apic timer and TSC stop)
  473. */
  474. static void c1e_idle(void)
  475. {
  476. if (need_resched())
  477. return;
  478. if (!c1e_detected) {
  479. u32 lo, hi;
  480. rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
  481. if (lo & K8_INTP_C1E_ACTIVE_MASK) {
  482. c1e_detected = true;
  483. if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  484. mark_tsc_unstable("TSC halt in AMD C1E");
  485. printk(KERN_INFO "System has AMD C1E enabled\n");
  486. }
  487. }
  488. if (c1e_detected) {
  489. int cpu = smp_processor_id();
  490. if (!cpumask_test_cpu(cpu, c1e_mask)) {
  491. cpumask_set_cpu(cpu, c1e_mask);
  492. /*
  493. * Force broadcast so ACPI can not interfere.
  494. */
  495. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
  496. &cpu);
  497. printk(KERN_INFO "Switch to broadcast mode on CPU%d\n",
  498. cpu);
  499. }
  500. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
  501. default_idle();
  502. /*
  503. * The switch back from broadcast mode needs to be
  504. * called with interrupts disabled.
  505. */
  506. local_irq_disable();
  507. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
  508. local_irq_enable();
  509. } else
  510. default_idle();
  511. }
  512. void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
  513. {
  514. #ifdef CONFIG_SMP
  515. if (pm_idle == poll_idle && smp_num_siblings > 1) {
  516. printk_once(KERN_WARNING "WARNING: polling idle and HT enabled,"
  517. " performance may degrade.\n");
  518. }
  519. #endif
  520. if (pm_idle)
  521. return;
  522. if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) {
  523. /*
  524. * One CPU supports mwait => All CPUs supports mwait
  525. */
  526. printk(KERN_INFO "using mwait in idle threads.\n");
  527. pm_idle = mwait_idle;
  528. } else if (cpu_has_amd_erratum(amd_erratum_400)) {
  529. /* E400: APIC timer interrupt does not wake up CPU from C1e */
  530. printk(KERN_INFO "using C1E aware idle routine\n");
  531. pm_idle = c1e_idle;
  532. } else
  533. pm_idle = default_idle;
  534. }
  535. void __init init_c1e_mask(void)
  536. {
  537. /* If we're using c1e_idle, we need to allocate c1e_mask. */
  538. if (pm_idle == c1e_idle)
  539. zalloc_cpumask_var(&c1e_mask, GFP_KERNEL);
  540. }
  541. static int __init idle_setup(char *str)
  542. {
  543. if (!str)
  544. return -EINVAL;
  545. if (!strcmp(str, "poll")) {
  546. printk("using polling idle threads.\n");
  547. pm_idle = poll_idle;
  548. boot_option_idle_override = IDLE_POLL;
  549. } else if (!strcmp(str, "mwait")) {
  550. boot_option_idle_override = IDLE_FORCE_MWAIT;
  551. } else if (!strcmp(str, "halt")) {
  552. /*
  553. * When the boot option of idle=halt is added, halt is
  554. * forced to be used for CPU idle. In such case CPU C2/C3
  555. * won't be used again.
  556. * To continue to load the CPU idle driver, don't touch
  557. * the boot_option_idle_override.
  558. */
  559. pm_idle = default_idle;
  560. boot_option_idle_override = IDLE_HALT;
  561. } else if (!strcmp(str, "nomwait")) {
  562. /*
  563. * If the boot option of "idle=nomwait" is added,
  564. * it means that mwait will be disabled for CPU C2/C3
  565. * states. In such case it won't touch the variable
  566. * of boot_option_idle_override.
  567. */
  568. boot_option_idle_override = IDLE_NOMWAIT;
  569. } else
  570. return -1;
  571. return 0;
  572. }
  573. early_param("idle", idle_setup);
  574. unsigned long arch_align_stack(unsigned long sp)
  575. {
  576. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  577. sp -= get_random_int() % 8192;
  578. return sp & ~0xf;
  579. }
  580. unsigned long arch_randomize_brk(struct mm_struct *mm)
  581. {
  582. unsigned long range_end = mm->brk + 0x02000000;
  583. return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
  584. }