process.c 23 KB

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