process.c 23 KB

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