process_32.c 24 KB

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