process.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include <linux/errno.h>
  2. #include <linux/kernel.h>
  3. #include <linux/mm.h>
  4. #include <linux/smp.h>
  5. #include <linux/slab.h>
  6. #include <linux/sched.h>
  7. #include <linux/module.h>
  8. #include <linux/pm.h>
  9. struct kmem_cache *task_xstate_cachep;
  10. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  11. {
  12. *dst = *src;
  13. if (src->thread.xstate) {
  14. dst->thread.xstate = kmem_cache_alloc(task_xstate_cachep,
  15. GFP_KERNEL);
  16. if (!dst->thread.xstate)
  17. return -ENOMEM;
  18. WARN_ON((unsigned long)dst->thread.xstate & 15);
  19. memcpy(dst->thread.xstate, src->thread.xstate, xstate_size);
  20. }
  21. return 0;
  22. }
  23. void free_thread_xstate(struct task_struct *tsk)
  24. {
  25. if (tsk->thread.xstate) {
  26. kmem_cache_free(task_xstate_cachep, tsk->thread.xstate);
  27. tsk->thread.xstate = NULL;
  28. }
  29. }
  30. void free_thread_info(struct thread_info *ti)
  31. {
  32. free_thread_xstate(ti->task);
  33. free_pages((unsigned long)ti, get_order(THREAD_SIZE));
  34. }
  35. void arch_task_cache_init(void)
  36. {
  37. task_xstate_cachep =
  38. kmem_cache_create("task_xstate", xstate_size,
  39. __alignof__(union thread_xstate),
  40. SLAB_PANIC, NULL);
  41. }
  42. /*
  43. * Idle related variables and functions
  44. */
  45. unsigned long boot_option_idle_override = 0;
  46. EXPORT_SYMBOL(boot_option_idle_override);
  47. /*
  48. * Powermanagement idle function, if any..
  49. */
  50. void (*pm_idle)(void);
  51. EXPORT_SYMBOL(pm_idle);
  52. #ifdef CONFIG_X86_32
  53. /*
  54. * This halt magic was a workaround for ancient floppy DMA
  55. * wreckage. It should be safe to remove.
  56. */
  57. static int hlt_counter;
  58. void disable_hlt(void)
  59. {
  60. hlt_counter++;
  61. }
  62. EXPORT_SYMBOL(disable_hlt);
  63. void enable_hlt(void)
  64. {
  65. hlt_counter--;
  66. }
  67. EXPORT_SYMBOL(enable_hlt);
  68. static inline int hlt_use_halt(void)
  69. {
  70. return (!hlt_counter && boot_cpu_data.hlt_works_ok);
  71. }
  72. #else
  73. static inline int hlt_use_halt(void)
  74. {
  75. return 1;
  76. }
  77. #endif
  78. /*
  79. * We use this if we don't have any better
  80. * idle routine..
  81. */
  82. void default_idle(void)
  83. {
  84. if (hlt_use_halt()) {
  85. current_thread_info()->status &= ~TS_POLLING;
  86. /*
  87. * TS_POLLING-cleared state must be visible before we
  88. * test NEED_RESCHED:
  89. */
  90. smp_mb();
  91. if (!need_resched())
  92. safe_halt(); /* enables interrupts racelessly */
  93. else
  94. local_irq_enable();
  95. current_thread_info()->status |= TS_POLLING;
  96. } else {
  97. local_irq_enable();
  98. /* loop is done by the caller */
  99. cpu_relax();
  100. }
  101. }
  102. #ifdef CONFIG_APM_MODULE
  103. EXPORT_SYMBOL(default_idle);
  104. #endif
  105. static void do_nothing(void *unused)
  106. {
  107. }
  108. /*
  109. * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
  110. * pm_idle and update to new pm_idle value. Required while changing pm_idle
  111. * handler on SMP systems.
  112. *
  113. * Caller must have changed pm_idle to the new value before the call. Old
  114. * pm_idle value will not be used by any CPU after the return of this function.
  115. */
  116. void cpu_idle_wait(void)
  117. {
  118. smp_mb();
  119. /* kick all the CPUs so that they exit out of pm_idle */
  120. smp_call_function(do_nothing, NULL, 0, 1);
  121. }
  122. EXPORT_SYMBOL_GPL(cpu_idle_wait);
  123. /*
  124. * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
  125. * which can obviate IPI to trigger checking of need_resched.
  126. * We execute MONITOR against need_resched and enter optimized wait state
  127. * through MWAIT. Whenever someone changes need_resched, we would be woken
  128. * up from MWAIT (without an IPI).
  129. *
  130. * New with Core Duo processors, MWAIT can take some hints based on CPU
  131. * capability.
  132. */
  133. void mwait_idle_with_hints(unsigned long ax, unsigned long cx)
  134. {
  135. if (!need_resched()) {
  136. __monitor((void *)&current_thread_info()->flags, 0, 0);
  137. smp_mb();
  138. if (!need_resched())
  139. __mwait(ax, cx);
  140. }
  141. }
  142. /* Default MONITOR/MWAIT with no hints, used for default C1 state */
  143. static void mwait_idle(void)
  144. {
  145. if (!need_resched()) {
  146. __monitor((void *)&current_thread_info()->flags, 0, 0);
  147. smp_mb();
  148. if (!need_resched())
  149. __sti_mwait(0, 0);
  150. else
  151. local_irq_enable();
  152. } else
  153. local_irq_enable();
  154. }
  155. /*
  156. * On SMP it's slightly faster (but much more power-consuming!)
  157. * to poll the ->work.need_resched flag instead of waiting for the
  158. * cross-CPU IPI to arrive. Use this option with caution.
  159. */
  160. static void poll_idle(void)
  161. {
  162. local_irq_enable();
  163. cpu_relax();
  164. }
  165. /*
  166. * mwait selection logic:
  167. *
  168. * It depends on the CPU. For AMD CPUs that support MWAIT this is
  169. * wrong. Family 0x10 and 0x11 CPUs will enter C1 on HLT. Powersavings
  170. * then depend on a clock divisor and current Pstate of the core. If
  171. * all cores of a processor are in halt state (C1) the processor can
  172. * enter the C1E (C1 enhanced) state. If mwait is used this will never
  173. * happen.
  174. *
  175. * idle=mwait overrides this decision and forces the usage of mwait.
  176. */
  177. #define MWAIT_INFO 0x05
  178. #define MWAIT_ECX_EXTENDED_INFO 0x01
  179. #define MWAIT_EDX_C1 0xf0
  180. static int __cpuinit mwait_usable(const struct cpuinfo_x86 *c)
  181. {
  182. u32 eax, ebx, ecx, edx;
  183. if (force_mwait)
  184. return 1;
  185. if (c->cpuid_level < MWAIT_INFO)
  186. return 0;
  187. cpuid(MWAIT_INFO, &eax, &ebx, &ecx, &edx);
  188. /* Check, whether EDX has extended info about MWAIT */
  189. if (!(ecx & MWAIT_ECX_EXTENDED_INFO))
  190. return 1;
  191. /*
  192. * edx enumeratios MONITOR/MWAIT extensions. Check, whether
  193. * C1 supports MWAIT
  194. */
  195. return (edx & MWAIT_EDX_C1);
  196. }
  197. void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
  198. {
  199. #ifdef CONFIG_X86_SMP
  200. if (pm_idle == poll_idle && smp_num_siblings > 1) {
  201. printk(KERN_WARNING "WARNING: polling idle and HT enabled,"
  202. " performance may degrade.\n");
  203. }
  204. #endif
  205. if (pm_idle)
  206. return;
  207. if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) {
  208. /*
  209. * One CPU supports mwait => All CPUs supports mwait
  210. */
  211. printk(KERN_INFO "using mwait in idle threads.\n");
  212. pm_idle = mwait_idle;
  213. } else
  214. pm_idle = default_idle;
  215. }
  216. static int __init idle_setup(char *str)
  217. {
  218. if (!strcmp(str, "poll")) {
  219. printk("using polling idle threads.\n");
  220. pm_idle = poll_idle;
  221. } else if (!strcmp(str, "mwait"))
  222. force_mwait = 1;
  223. else
  224. return -1;
  225. boot_option_idle_override = 1;
  226. return 0;
  227. }
  228. early_param("idle", idle_setup);