process.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. /*
  2. * linux/arch/i386/kernel/process.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. *
  6. * Pentium III FXSR, SSE support
  7. * Gareth Hughes <gareth@valinux.com>, May 2000
  8. */
  9. /*
  10. * This file handles the architecture-dependent parts of process handling..
  11. */
  12. #include <stdarg.h>
  13. #include <linux/cpu.h>
  14. #include <linux/errno.h>
  15. #include <linux/sched.h>
  16. #include <linux/fs.h>
  17. #include <linux/kernel.h>
  18. #include <linux/mm.h>
  19. #include <linux/elfcore.h>
  20. #include <linux/smp.h>
  21. #include <linux/smp_lock.h>
  22. #include <linux/stddef.h>
  23. #include <linux/slab.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/user.h>
  26. #include <linux/a.out.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/utsname.h>
  29. #include <linux/delay.h>
  30. #include <linux/reboot.h>
  31. #include <linux/init.h>
  32. #include <linux/mc146818rtc.h>
  33. #include <linux/module.h>
  34. #include <linux/kallsyms.h>
  35. #include <linux/ptrace.h>
  36. #include <linux/random.h>
  37. #include <linux/personality.h>
  38. #include <linux/tick.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/pgtable.h>
  41. #include <asm/system.h>
  42. #include <asm/io.h>
  43. #include <asm/ldt.h>
  44. #include <asm/processor.h>
  45. #include <asm/i387.h>
  46. #include <asm/desc.h>
  47. #include <asm/vm86.h>
  48. #include <asm/idle.h>
  49. #ifdef CONFIG_MATH_EMULATION
  50. #include <asm/math_emu.h>
  51. #endif
  52. #include <linux/err.h>
  53. #include <asm/tlbflush.h>
  54. #include <asm/cpu.h>
  55. #include <asm/pda.h>
  56. asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
  57. static int hlt_counter;
  58. unsigned long boot_option_idle_override = 0;
  59. EXPORT_SYMBOL(boot_option_idle_override);
  60. /*
  61. * Return saved PC of a blocked thread.
  62. */
  63. unsigned long thread_saved_pc(struct task_struct *tsk)
  64. {
  65. return ((unsigned long *)tsk->thread.esp)[3];
  66. }
  67. /*
  68. * Powermanagement idle function, if any..
  69. */
  70. void (*pm_idle)(void);
  71. EXPORT_SYMBOL(pm_idle);
  72. static DEFINE_PER_CPU(unsigned int, cpu_idle_state);
  73. static ATOMIC_NOTIFIER_HEAD(idle_notifier);
  74. void idle_notifier_register(struct notifier_block *n)
  75. {
  76. atomic_notifier_chain_register(&idle_notifier, n);
  77. }
  78. void idle_notifier_unregister(struct notifier_block *n)
  79. {
  80. atomic_notifier_chain_unregister(&idle_notifier, n);
  81. }
  82. static DEFINE_PER_CPU(volatile unsigned long, idle_state);
  83. void enter_idle(void)
  84. {
  85. /* needs to be atomic w.r.t. interrupts, not against other CPUs */
  86. __set_bit(0, &__get_cpu_var(idle_state));
  87. atomic_notifier_call_chain(&idle_notifier, IDLE_START, NULL);
  88. }
  89. static void __exit_idle(void)
  90. {
  91. /* needs to be atomic w.r.t. interrupts, not against other CPUs */
  92. if (__test_and_clear_bit(0, &__get_cpu_var(idle_state)) == 0)
  93. return;
  94. atomic_notifier_call_chain(&idle_notifier, IDLE_END, NULL);
  95. }
  96. void exit_idle(void)
  97. {
  98. if (current->pid)
  99. return;
  100. __exit_idle();
  101. }
  102. void disable_hlt(void)
  103. {
  104. hlt_counter++;
  105. }
  106. EXPORT_SYMBOL(disable_hlt);
  107. void enable_hlt(void)
  108. {
  109. hlt_counter--;
  110. }
  111. EXPORT_SYMBOL(enable_hlt);
  112. /*
  113. * We use this if we don't have any better
  114. * idle routine..
  115. */
  116. void default_idle(void)
  117. {
  118. if (!hlt_counter && boot_cpu_data.hlt_works_ok) {
  119. current_thread_info()->status &= ~TS_POLLING;
  120. /*
  121. * TS_POLLING-cleared state must be visible before we
  122. * test NEED_RESCHED:
  123. */
  124. smp_mb();
  125. local_irq_disable();
  126. if (!need_resched())
  127. safe_halt(); /* enables interrupts racelessly */
  128. else
  129. local_irq_enable();
  130. current_thread_info()->status |= TS_POLLING;
  131. } else {
  132. /* loop is done by the caller */
  133. cpu_relax();
  134. }
  135. }
  136. #ifdef CONFIG_APM_MODULE
  137. EXPORT_SYMBOL(default_idle);
  138. #endif
  139. /*
  140. * On SMP it's slightly faster (but much more power-consuming!)
  141. * to poll the ->work.need_resched flag instead of waiting for the
  142. * cross-CPU IPI to arrive. Use this option with caution.
  143. */
  144. static void poll_idle (void)
  145. {
  146. local_irq_enable();
  147. cpu_relax();
  148. }
  149. #ifdef CONFIG_HOTPLUG_CPU
  150. #include <asm/nmi.h>
  151. /* We don't actually take CPU down, just spin without interrupts. */
  152. static inline void play_dead(void)
  153. {
  154. /* This must be done before dead CPU ack */
  155. cpu_exit_clear();
  156. wbinvd();
  157. mb();
  158. /* Ack it */
  159. __get_cpu_var(cpu_state) = CPU_DEAD;
  160. /*
  161. * With physical CPU hotplug, we should halt the cpu
  162. */
  163. local_irq_disable();
  164. while (1)
  165. halt();
  166. }
  167. #else
  168. static inline void play_dead(void)
  169. {
  170. BUG();
  171. }
  172. #endif /* CONFIG_HOTPLUG_CPU */
  173. /*
  174. * The idle thread. There's no useful work to be
  175. * done, so just try to conserve power and have a
  176. * low exit latency (ie sit in a loop waiting for
  177. * somebody to say that they'd like to reschedule)
  178. */
  179. void cpu_idle(void)
  180. {
  181. int cpu = smp_processor_id();
  182. current_thread_info()->status |= TS_POLLING;
  183. /* endless idle loop with no priority at all */
  184. while (1) {
  185. tick_nohz_stop_sched_tick();
  186. while (!need_resched()) {
  187. void (*idle)(void);
  188. if (__get_cpu_var(cpu_idle_state))
  189. __get_cpu_var(cpu_idle_state) = 0;
  190. rmb();
  191. idle = pm_idle;
  192. if (!idle)
  193. idle = default_idle;
  194. if (cpu_is_offline(cpu))
  195. play_dead();
  196. __get_cpu_var(irq_stat).idle_timestamp = jiffies;
  197. /*
  198. * Idle routines should keep interrupts disabled
  199. * from here on, until they go to idle.
  200. * Otherwise, idle callbacks can misfire.
  201. */
  202. local_irq_disable();
  203. enter_idle();
  204. idle();
  205. __exit_idle();
  206. }
  207. tick_nohz_restart_sched_tick();
  208. preempt_enable_no_resched();
  209. schedule();
  210. preempt_disable();
  211. }
  212. }
  213. void cpu_idle_wait(void)
  214. {
  215. unsigned int cpu, this_cpu = get_cpu();
  216. cpumask_t map, tmp = current->cpus_allowed;
  217. set_cpus_allowed(current, cpumask_of_cpu(this_cpu));
  218. put_cpu();
  219. cpus_clear(map);
  220. for_each_online_cpu(cpu) {
  221. per_cpu(cpu_idle_state, cpu) = 1;
  222. cpu_set(cpu, map);
  223. }
  224. __get_cpu_var(cpu_idle_state) = 0;
  225. wmb();
  226. do {
  227. ssleep(1);
  228. for_each_online_cpu(cpu) {
  229. if (cpu_isset(cpu, map) && !per_cpu(cpu_idle_state, cpu))
  230. cpu_clear(cpu, map);
  231. }
  232. cpus_and(map, map, cpu_online_map);
  233. } while (!cpus_empty(map));
  234. set_cpus_allowed(current, tmp);
  235. }
  236. EXPORT_SYMBOL_GPL(cpu_idle_wait);
  237. /*
  238. * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
  239. * which can obviate IPI to trigger checking of need_resched.
  240. * We execute MONITOR against need_resched and enter optimized wait state
  241. * through MWAIT. Whenever someone changes need_resched, we would be woken
  242. * up from MWAIT (without an IPI).
  243. *
  244. * New with Core Duo processors, MWAIT can take some hints based on CPU
  245. * capability.
  246. */
  247. void mwait_idle_with_hints(unsigned long eax, unsigned long ecx)
  248. {
  249. if (!need_resched()) {
  250. __monitor((void *)&current_thread_info()->flags, 0, 0);
  251. smp_mb();
  252. if (!need_resched())
  253. __sti_mwait(eax, ecx);
  254. else
  255. local_irq_enable();
  256. } else {
  257. local_irq_enable();
  258. }
  259. }
  260. /* Default MONITOR/MWAIT with no hints, used for default C1 state */
  261. static void mwait_idle(void)
  262. {
  263. local_irq_enable();
  264. mwait_idle_with_hints(0, 0);
  265. }
  266. void __devinit select_idle_routine(const struct cpuinfo_x86 *c)
  267. {
  268. if (cpu_has(c, X86_FEATURE_MWAIT)) {
  269. printk("monitor/mwait feature present.\n");
  270. /*
  271. * Skip, if setup has overridden idle.
  272. * One CPU supports mwait => All CPUs supports mwait
  273. */
  274. if (!pm_idle) {
  275. printk("using mwait in idle threads.\n");
  276. pm_idle = mwait_idle;
  277. }
  278. }
  279. }
  280. static int __init idle_setup (char *str)
  281. {
  282. if (!strncmp(str, "poll", 4)) {
  283. printk("using polling idle threads.\n");
  284. pm_idle = poll_idle;
  285. #ifdef CONFIG_X86_SMP
  286. if (smp_num_siblings > 1)
  287. printk("WARNING: polling idle and HT enabled, performance may degrade.\n");
  288. #endif
  289. } else if (!strncmp(str, "halt", 4)) {
  290. printk("using halt in idle threads.\n");
  291. pm_idle = default_idle;
  292. }
  293. boot_option_idle_override = 1;
  294. return 1;
  295. }
  296. __setup("idle=", idle_setup);
  297. void show_regs(struct pt_regs * regs)
  298. {
  299. unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L;
  300. printk("\n");
  301. printk("Pid: %d, comm: %20s\n", current->pid, current->comm);
  302. printk("EIP: %04x:[<%08lx>] CPU: %d\n",0xffff & regs->xcs,regs->eip, smp_processor_id());
  303. print_symbol("EIP is at %s\n", regs->eip);
  304. if (user_mode_vm(regs))
  305. printk(" ESP: %04x:%08lx",0xffff & regs->xss,regs->esp);
  306. printk(" EFLAGS: %08lx %s (%s %.*s)\n",
  307. regs->eflags, print_tainted(), init_utsname()->release,
  308. (int)strcspn(init_utsname()->version, " "),
  309. init_utsname()->version);
  310. printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
  311. regs->eax,regs->ebx,regs->ecx,regs->edx);
  312. printk("ESI: %08lx EDI: %08lx EBP: %08lx",
  313. regs->esi, regs->edi, regs->ebp);
  314. printk(" DS: %04x ES: %04x FS: %04x\n",
  315. 0xffff & regs->xds,0xffff & regs->xes, 0xffff & regs->xfs);
  316. cr0 = read_cr0();
  317. cr2 = read_cr2();
  318. cr3 = read_cr3();
  319. cr4 = read_cr4_safe();
  320. printk("CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n", cr0, cr2, cr3, cr4);
  321. show_trace(NULL, regs, &regs->esp);
  322. }
  323. /*
  324. * This gets run with %ebx containing the
  325. * function to call, and %edx containing
  326. * the "args".
  327. */
  328. extern void kernel_thread_helper(void);
  329. /*
  330. * Create a kernel thread
  331. */
  332. int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  333. {
  334. struct pt_regs regs;
  335. memset(&regs, 0, sizeof(regs));
  336. regs.ebx = (unsigned long) fn;
  337. regs.edx = (unsigned long) arg;
  338. regs.xds = __USER_DS;
  339. regs.xes = __USER_DS;
  340. regs.xfs = __KERNEL_PDA;
  341. regs.orig_eax = -1;
  342. regs.eip = (unsigned long) kernel_thread_helper;
  343. regs.xcs = __KERNEL_CS | get_kernel_rpl();
  344. regs.eflags = X86_EFLAGS_IF | X86_EFLAGS_SF | X86_EFLAGS_PF | 0x2;
  345. /* Ok, create the new process.. */
  346. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
  347. }
  348. EXPORT_SYMBOL(kernel_thread);
  349. /*
  350. * Free current thread data structures etc..
  351. */
  352. void exit_thread(void)
  353. {
  354. /* The process may have allocated an io port bitmap... nuke it. */
  355. if (unlikely(test_thread_flag(TIF_IO_BITMAP))) {
  356. struct task_struct *tsk = current;
  357. struct thread_struct *t = &tsk->thread;
  358. int cpu = get_cpu();
  359. struct tss_struct *tss = &per_cpu(init_tss, cpu);
  360. kfree(t->io_bitmap_ptr);
  361. t->io_bitmap_ptr = NULL;
  362. clear_thread_flag(TIF_IO_BITMAP);
  363. /*
  364. * Careful, clear this in the TSS too:
  365. */
  366. memset(tss->io_bitmap, 0xff, tss->io_bitmap_max);
  367. t->io_bitmap_max = 0;
  368. tss->io_bitmap_owner = NULL;
  369. tss->io_bitmap_max = 0;
  370. tss->io_bitmap_base = INVALID_IO_BITMAP_OFFSET;
  371. put_cpu();
  372. }
  373. }
  374. void flush_thread(void)
  375. {
  376. struct task_struct *tsk = current;
  377. memset(tsk->thread.debugreg, 0, sizeof(unsigned long)*8);
  378. memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
  379. clear_tsk_thread_flag(tsk, TIF_DEBUG);
  380. /*
  381. * Forget coprocessor state..
  382. */
  383. clear_fpu(tsk);
  384. clear_used_math();
  385. }
  386. void release_thread(struct task_struct *dead_task)
  387. {
  388. BUG_ON(dead_task->mm);
  389. release_vm86_irqs(dead_task);
  390. }
  391. /*
  392. * This gets called before we allocate a new thread and copy
  393. * the current task into it.
  394. */
  395. void prepare_to_copy(struct task_struct *tsk)
  396. {
  397. unlazy_fpu(tsk);
  398. }
  399. int copy_thread(int nr, unsigned long clone_flags, unsigned long esp,
  400. unsigned long unused,
  401. struct task_struct * p, struct pt_regs * regs)
  402. {
  403. struct pt_regs * childregs;
  404. struct task_struct *tsk;
  405. int err;
  406. childregs = task_pt_regs(p);
  407. *childregs = *regs;
  408. childregs->eax = 0;
  409. childregs->esp = esp;
  410. p->thread.esp = (unsigned long) childregs;
  411. p->thread.esp0 = (unsigned long) (childregs+1);
  412. p->thread.eip = (unsigned long) ret_from_fork;
  413. savesegment(gs,p->thread.gs);
  414. tsk = current;
  415. if (unlikely(test_tsk_thread_flag(tsk, TIF_IO_BITMAP))) {
  416. p->thread.io_bitmap_ptr = kmemdup(tsk->thread.io_bitmap_ptr,
  417. IO_BITMAP_BYTES, GFP_KERNEL);
  418. if (!p->thread.io_bitmap_ptr) {
  419. p->thread.io_bitmap_max = 0;
  420. return -ENOMEM;
  421. }
  422. set_tsk_thread_flag(p, TIF_IO_BITMAP);
  423. }
  424. /*
  425. * Set a new TLS for the child thread?
  426. */
  427. if (clone_flags & CLONE_SETTLS) {
  428. struct desc_struct *desc;
  429. struct user_desc info;
  430. int idx;
  431. err = -EFAULT;
  432. if (copy_from_user(&info, (void __user *)childregs->esi, sizeof(info)))
  433. goto out;
  434. err = -EINVAL;
  435. if (LDT_empty(&info))
  436. goto out;
  437. idx = info.entry_number;
  438. if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
  439. goto out;
  440. desc = p->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
  441. desc->a = LDT_entry_a(&info);
  442. desc->b = LDT_entry_b(&info);
  443. }
  444. err = 0;
  445. out:
  446. if (err && p->thread.io_bitmap_ptr) {
  447. kfree(p->thread.io_bitmap_ptr);
  448. p->thread.io_bitmap_max = 0;
  449. }
  450. return err;
  451. }
  452. /*
  453. * fill in the user structure for a core dump..
  454. */
  455. void dump_thread(struct pt_regs * regs, struct user * dump)
  456. {
  457. int i;
  458. /* changed the size calculations - should hopefully work better. lbt */
  459. dump->magic = CMAGIC;
  460. dump->start_code = 0;
  461. dump->start_stack = regs->esp & ~(PAGE_SIZE - 1);
  462. dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
  463. dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT;
  464. dump->u_dsize -= dump->u_tsize;
  465. dump->u_ssize = 0;
  466. for (i = 0; i < 8; i++)
  467. dump->u_debugreg[i] = current->thread.debugreg[i];
  468. if (dump->start_stack < TASK_SIZE)
  469. dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT;
  470. dump->regs.ebx = regs->ebx;
  471. dump->regs.ecx = regs->ecx;
  472. dump->regs.edx = regs->edx;
  473. dump->regs.esi = regs->esi;
  474. dump->regs.edi = regs->edi;
  475. dump->regs.ebp = regs->ebp;
  476. dump->regs.eax = regs->eax;
  477. dump->regs.ds = regs->xds;
  478. dump->regs.es = regs->xes;
  479. dump->regs.fs = regs->xfs;
  480. savesegment(gs,dump->regs.gs);
  481. dump->regs.orig_eax = regs->orig_eax;
  482. dump->regs.eip = regs->eip;
  483. dump->regs.cs = regs->xcs;
  484. dump->regs.eflags = regs->eflags;
  485. dump->regs.esp = regs->esp;
  486. dump->regs.ss = regs->xss;
  487. dump->u_fpvalid = dump_fpu (regs, &dump->i387);
  488. }
  489. EXPORT_SYMBOL(dump_thread);
  490. /*
  491. * Capture the user space registers if the task is not running (in user space)
  492. */
  493. int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
  494. {
  495. struct pt_regs ptregs = *task_pt_regs(tsk);
  496. ptregs.xcs &= 0xffff;
  497. ptregs.xds &= 0xffff;
  498. ptregs.xes &= 0xffff;
  499. ptregs.xss &= 0xffff;
  500. elf_core_copy_regs(regs, &ptregs);
  501. return 1;
  502. }
  503. static noinline void __switch_to_xtra(struct task_struct *next_p,
  504. struct tss_struct *tss)
  505. {
  506. struct thread_struct *next;
  507. next = &next_p->thread;
  508. if (test_tsk_thread_flag(next_p, TIF_DEBUG)) {
  509. set_debugreg(next->debugreg[0], 0);
  510. set_debugreg(next->debugreg[1], 1);
  511. set_debugreg(next->debugreg[2], 2);
  512. set_debugreg(next->debugreg[3], 3);
  513. /* no 4 and 5 */
  514. set_debugreg(next->debugreg[6], 6);
  515. set_debugreg(next->debugreg[7], 7);
  516. }
  517. if (!test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
  518. /*
  519. * Disable the bitmap via an invalid offset. We still cache
  520. * the previous bitmap owner and the IO bitmap contents:
  521. */
  522. tss->io_bitmap_base = INVALID_IO_BITMAP_OFFSET;
  523. return;
  524. }
  525. if (likely(next == tss->io_bitmap_owner)) {
  526. /*
  527. * Previous owner of the bitmap (hence the bitmap content)
  528. * matches the next task, we dont have to do anything but
  529. * to set a valid offset in the TSS:
  530. */
  531. tss->io_bitmap_base = IO_BITMAP_OFFSET;
  532. return;
  533. }
  534. /*
  535. * Lazy TSS's I/O bitmap copy. We set an invalid offset here
  536. * and we let the task to get a GPF in case an I/O instruction
  537. * is performed. The handler of the GPF will verify that the
  538. * faulting task has a valid I/O bitmap and, it true, does the
  539. * real copy and restart the instruction. This will save us
  540. * redundant copies when the currently switched task does not
  541. * perform any I/O during its timeslice.
  542. */
  543. tss->io_bitmap_base = INVALID_IO_BITMAP_OFFSET_LAZY;
  544. }
  545. /*
  546. * This function selects if the context switch from prev to next
  547. * has to tweak the TSC disable bit in the cr4.
  548. */
  549. static inline void disable_tsc(struct task_struct *prev_p,
  550. struct task_struct *next_p)
  551. {
  552. struct thread_info *prev, *next;
  553. /*
  554. * gcc should eliminate the ->thread_info dereference if
  555. * has_secure_computing returns 0 at compile time (SECCOMP=n).
  556. */
  557. prev = task_thread_info(prev_p);
  558. next = task_thread_info(next_p);
  559. if (has_secure_computing(prev) || has_secure_computing(next)) {
  560. /* slow path here */
  561. if (has_secure_computing(prev) &&
  562. !has_secure_computing(next)) {
  563. write_cr4(read_cr4() & ~X86_CR4_TSD);
  564. } else if (!has_secure_computing(prev) &&
  565. has_secure_computing(next))
  566. write_cr4(read_cr4() | X86_CR4_TSD);
  567. }
  568. }
  569. /*
  570. * switch_to(x,yn) should switch tasks from x to y.
  571. *
  572. * We fsave/fwait so that an exception goes off at the right time
  573. * (as a call from the fsave or fwait in effect) rather than to
  574. * the wrong process. Lazy FP saving no longer makes any sense
  575. * with modern CPU's, and this simplifies a lot of things (SMP
  576. * and UP become the same).
  577. *
  578. * NOTE! We used to use the x86 hardware context switching. The
  579. * reason for not using it any more becomes apparent when you
  580. * try to recover gracefully from saved state that is no longer
  581. * valid (stale segment register values in particular). With the
  582. * hardware task-switch, there is no way to fix up bad state in
  583. * a reasonable manner.
  584. *
  585. * The fact that Intel documents the hardware task-switching to
  586. * be slow is a fairly red herring - this code is not noticeably
  587. * faster. However, there _is_ some room for improvement here,
  588. * so the performance issues may eventually be a valid point.
  589. * More important, however, is the fact that this allows us much
  590. * more flexibility.
  591. *
  592. * The return value (in %eax) will be the "prev" task after
  593. * the task-switch, and shows up in ret_from_fork in entry.S,
  594. * for example.
  595. */
  596. struct task_struct fastcall * __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
  597. {
  598. struct thread_struct *prev = &prev_p->thread,
  599. *next = &next_p->thread;
  600. int cpu = smp_processor_id();
  601. struct tss_struct *tss = &per_cpu(init_tss, cpu);
  602. /* never put a printk in __switch_to... printk() calls wake_up*() indirectly */
  603. __unlazy_fpu(prev_p);
  604. /* we're going to use this soon, after a few expensive things */
  605. if (next_p->fpu_counter > 5)
  606. prefetch(&next->i387.fxsave);
  607. /*
  608. * Reload esp0.
  609. */
  610. load_esp0(tss, next);
  611. /*
  612. * Save away %gs. No need to save %fs, as it was saved on the
  613. * stack on entry. No need to save %es and %ds, as those are
  614. * always kernel segments while inside the kernel. Doing this
  615. * before setting the new TLS descriptors avoids the situation
  616. * where we temporarily have non-reloadable segments in %fs
  617. * and %gs. This could be an issue if the NMI handler ever
  618. * used %fs or %gs (it does not today), or if the kernel is
  619. * running inside of a hypervisor layer.
  620. */
  621. savesegment(gs, prev->gs);
  622. /*
  623. * Load the per-thread Thread-Local Storage descriptor.
  624. */
  625. load_TLS(next, cpu);
  626. /*
  627. * Restore IOPL if needed. In normal use, the flags restore
  628. * in the switch assembly will handle this. But if the kernel
  629. * is running virtualized at a non-zero CPL, the popf will
  630. * not restore flags, so it must be done in a separate step.
  631. */
  632. if (get_kernel_rpl() && unlikely(prev->iopl != next->iopl))
  633. set_iopl_mask(next->iopl);
  634. /*
  635. * Now maybe handle debug registers and/or IO bitmaps
  636. */
  637. if (unlikely((task_thread_info(next_p)->flags & _TIF_WORK_CTXSW)
  638. || test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)))
  639. __switch_to_xtra(next_p, tss);
  640. disable_tsc(prev_p, next_p);
  641. /*
  642. * Leave lazy mode, flushing any hypercalls made here.
  643. * This must be done before restoring TLS segments so
  644. * the GDT and LDT are properly updated, and must be
  645. * done before math_state_restore, so the TS bit is up
  646. * to date.
  647. */
  648. arch_leave_lazy_cpu_mode();
  649. /* If the task has used fpu the last 5 timeslices, just do a full
  650. * restore of the math state immediately to avoid the trap; the
  651. * chances of needing FPU soon are obviously high now
  652. */
  653. if (next_p->fpu_counter > 5)
  654. math_state_restore();
  655. /*
  656. * Restore %gs if needed (which is common)
  657. */
  658. if (prev->gs | next->gs)
  659. loadsegment(gs, next->gs);
  660. write_pda(pcurrent, next_p);
  661. return prev_p;
  662. }
  663. asmlinkage int sys_fork(struct pt_regs regs)
  664. {
  665. return do_fork(SIGCHLD, regs.esp, &regs, 0, NULL, NULL);
  666. }
  667. asmlinkage int sys_clone(struct pt_regs regs)
  668. {
  669. unsigned long clone_flags;
  670. unsigned long newsp;
  671. int __user *parent_tidptr, *child_tidptr;
  672. clone_flags = regs.ebx;
  673. newsp = regs.ecx;
  674. parent_tidptr = (int __user *)regs.edx;
  675. child_tidptr = (int __user *)regs.edi;
  676. if (!newsp)
  677. newsp = regs.esp;
  678. return do_fork(clone_flags, newsp, &regs, 0, parent_tidptr, child_tidptr);
  679. }
  680. /*
  681. * This is trivial, and on the face of it looks like it
  682. * could equally well be done in user mode.
  683. *
  684. * Not so, for quite unobvious reasons - register pressure.
  685. * In user mode vfork() cannot have a stack frame, and if
  686. * done by calling the "clone()" system call directly, you
  687. * do not have enough call-clobbered registers to hold all
  688. * the information you need.
  689. */
  690. asmlinkage int sys_vfork(struct pt_regs regs)
  691. {
  692. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.esp, &regs, 0, NULL, NULL);
  693. }
  694. /*
  695. * sys_execve() executes a new program.
  696. */
  697. asmlinkage int sys_execve(struct pt_regs regs)
  698. {
  699. int error;
  700. char * filename;
  701. filename = getname((char __user *) regs.ebx);
  702. error = PTR_ERR(filename);
  703. if (IS_ERR(filename))
  704. goto out;
  705. error = do_execve(filename,
  706. (char __user * __user *) regs.ecx,
  707. (char __user * __user *) regs.edx,
  708. &regs);
  709. if (error == 0) {
  710. task_lock(current);
  711. current->ptrace &= ~PT_DTRACE;
  712. task_unlock(current);
  713. /* Make sure we don't return using sysenter.. */
  714. set_thread_flag(TIF_IRET);
  715. }
  716. putname(filename);
  717. out:
  718. return error;
  719. }
  720. #define top_esp (THREAD_SIZE - sizeof(unsigned long))
  721. #define top_ebp (THREAD_SIZE - 2*sizeof(unsigned long))
  722. unsigned long get_wchan(struct task_struct *p)
  723. {
  724. unsigned long ebp, esp, eip;
  725. unsigned long stack_page;
  726. int count = 0;
  727. if (!p || p == current || p->state == TASK_RUNNING)
  728. return 0;
  729. stack_page = (unsigned long)task_stack_page(p);
  730. esp = p->thread.esp;
  731. if (!stack_page || esp < stack_page || esp > top_esp+stack_page)
  732. return 0;
  733. /* include/asm-i386/system.h:switch_to() pushes ebp last. */
  734. ebp = *(unsigned long *) esp;
  735. do {
  736. if (ebp < stack_page || ebp > top_ebp+stack_page)
  737. return 0;
  738. eip = *(unsigned long *) (ebp+4);
  739. if (!in_sched_functions(eip))
  740. return eip;
  741. ebp = *(unsigned long *) ebp;
  742. } while (count++ < 16);
  743. return 0;
  744. }
  745. /*
  746. * sys_alloc_thread_area: get a yet unused TLS descriptor index.
  747. */
  748. static int get_free_idx(void)
  749. {
  750. struct thread_struct *t = &current->thread;
  751. int idx;
  752. for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
  753. if (desc_empty(t->tls_array + idx))
  754. return idx + GDT_ENTRY_TLS_MIN;
  755. return -ESRCH;
  756. }
  757. /*
  758. * Set a given TLS descriptor:
  759. */
  760. asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
  761. {
  762. struct thread_struct *t = &current->thread;
  763. struct user_desc info;
  764. struct desc_struct *desc;
  765. int cpu, idx;
  766. if (copy_from_user(&info, u_info, sizeof(info)))
  767. return -EFAULT;
  768. idx = info.entry_number;
  769. /*
  770. * index -1 means the kernel should try to find and
  771. * allocate an empty descriptor:
  772. */
  773. if (idx == -1) {
  774. idx = get_free_idx();
  775. if (idx < 0)
  776. return idx;
  777. if (put_user(idx, &u_info->entry_number))
  778. return -EFAULT;
  779. }
  780. if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
  781. return -EINVAL;
  782. desc = t->tls_array + idx - GDT_ENTRY_TLS_MIN;
  783. /*
  784. * We must not get preempted while modifying the TLS.
  785. */
  786. cpu = get_cpu();
  787. if (LDT_empty(&info)) {
  788. desc->a = 0;
  789. desc->b = 0;
  790. } else {
  791. desc->a = LDT_entry_a(&info);
  792. desc->b = LDT_entry_b(&info);
  793. }
  794. load_TLS(t, cpu);
  795. put_cpu();
  796. return 0;
  797. }
  798. /*
  799. * Get the current Thread-Local Storage area:
  800. */
  801. #define GET_BASE(desc) ( \
  802. (((desc)->a >> 16) & 0x0000ffff) | \
  803. (((desc)->b << 16) & 0x00ff0000) | \
  804. ( (desc)->b & 0xff000000) )
  805. #define GET_LIMIT(desc) ( \
  806. ((desc)->a & 0x0ffff) | \
  807. ((desc)->b & 0xf0000) )
  808. #define GET_32BIT(desc) (((desc)->b >> 22) & 1)
  809. #define GET_CONTENTS(desc) (((desc)->b >> 10) & 3)
  810. #define GET_WRITABLE(desc) (((desc)->b >> 9) & 1)
  811. #define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1)
  812. #define GET_PRESENT(desc) (((desc)->b >> 15) & 1)
  813. #define GET_USEABLE(desc) (((desc)->b >> 20) & 1)
  814. asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
  815. {
  816. struct user_desc info;
  817. struct desc_struct *desc;
  818. int idx;
  819. if (get_user(idx, &u_info->entry_number))
  820. return -EFAULT;
  821. if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
  822. return -EINVAL;
  823. memset(&info, 0, sizeof(info));
  824. desc = current->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
  825. info.entry_number = idx;
  826. info.base_addr = GET_BASE(desc);
  827. info.limit = GET_LIMIT(desc);
  828. info.seg_32bit = GET_32BIT(desc);
  829. info.contents = GET_CONTENTS(desc);
  830. info.read_exec_only = !GET_WRITABLE(desc);
  831. info.limit_in_pages = GET_LIMIT_PAGES(desc);
  832. info.seg_not_present = !GET_PRESENT(desc);
  833. info.useable = GET_USEABLE(desc);
  834. if (copy_to_user(u_info, &info, sizeof(info)))
  835. return -EFAULT;
  836. return 0;
  837. }
  838. unsigned long arch_align_stack(unsigned long sp)
  839. {
  840. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  841. sp -= get_random_int() % 8192;
  842. return sp & ~0xf;
  843. }