process.c 16 KB

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