process.c 22 KB

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