process.c 23 KB

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