process.c 15 KB

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