process.c 22 KB

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