process_32.c 24 KB

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