process.c 23 KB

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