process.c 21 KB

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