2 * linux/arch/i386/kernel/process.c
4 * Copyright (C) 1995 Linus Torvalds
6 * Pentium III FXSR, SSE support
7 * Gareth Hughes <gareth@valinux.com>, May 2000
11 * This file handles the architecture-dependent parts of process handling..
14 #define __KERNEL_SYSCALLS__
17 #include <linux/errno.h>
18 #include <linux/sched.h>
20 #include <linux/kernel.h>
22 #include <linux/elfcore.h>
23 #include <linux/smp.h>
24 #include <linux/smp_lock.h>
25 #include <linux/stddef.h>
26 #include <linux/unistd.h>
27 #include <linux/slab.h>
28 #include <linux/vmalloc.h>
29 #include <linux/user.h>
30 #include <linux/a.out.h>
31 #include <linux/interrupt.h>
32 #include <linux/config.h>
33 #include <linux/delay.h>
34 #include <linux/reboot.h>
35 #include <linux/init.h>
36 #include <linux/mc146818rtc.h>
37 #include <linux/module.h>
38 #include <linux/kallsyms.h>
39 #include <linux/ptrace.h>
41 #include <asm/uaccess.h>
42 #include <asm/pgtable.h>
43 #include <asm/system.h>
46 #include <asm/processor.h>
50 #ifdef CONFIG_MATH_EMULATION
51 #include <asm/math_emu.h>
54 #include <linux/irq.h>
55 #include <linux/err.h>
57 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
62 * Return saved PC of a blocked thread.
64 unsigned long thread_saved_pc(struct task_struct *tsk)
66 return ((unsigned long *)tsk->thread.esp)[3];
70 * Powermanagement idle function, if any..
72 void (*pm_idle)(void);
74 void disable_hlt(void)
85 * We use this if we don't have any better
88 void default_idle(void)
90 if (!hlt_counter && current_cpu_data.hlt_works_ok) {
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.
104 static void poll_idle (void)
111 * Deal with another CPU just having chosen a thread to
114 oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);
117 set_thread_flag(TIF_POLLING_NRFLAG);
123 : : "i"(_TIF_NEED_RESCHED), "m" (current_thread_info()->flags));
125 clear_thread_flag(TIF_POLLING_NRFLAG);
132 * The idle thread. There's no useful work to be
133 * done, so just try to conserve power and have a
134 * low exit latency (ie sit in a loop waiting for
135 * somebody to say that they'd like to reschedule)
139 /* endless idle loop with no priority at all */
141 void (*idle)(void) = pm_idle;
144 irq_stat[smp_processor_id()].idle_timestamp = jiffies;
145 while (!need_resched())
151 static int __init idle_setup (char *str)
153 if (!strncmp(str, "poll", 4)) {
154 printk("using polling idle threads.\n");
161 __setup("idle=", idle_setup);
163 void show_regs(struct pt_regs * regs)
165 unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L;
168 printk("Pid: %d, comm: %20s\n", current->pid, current->comm);
169 printk("EIP: %04x:[<%08lx>] CPU: %d\n",0xffff & regs->xcs,regs->eip, smp_processor_id());
170 print_symbol("EIP is at %s\n", regs->eip);
173 printk(" ESP: %04x:%08lx",0xffff & regs->xss,regs->esp);
174 printk(" EFLAGS: %08lx %s\n",regs->eflags, print_tainted());
175 printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
176 regs->eax,regs->ebx,regs->ecx,regs->edx);
177 printk("ESI: %08lx EDI: %08lx EBP: %08lx",
178 regs->esi, regs->edi, regs->ebp);
179 printk(" DS: %04x ES: %04x\n",
180 0xffff & regs->xds,0xffff & regs->xes);
182 __asm__("movl %%cr0, %0": "=r" (cr0));
183 __asm__("movl %%cr2, %0": "=r" (cr2));
184 __asm__("movl %%cr3, %0": "=r" (cr3));
185 /* This could fault if %cr4 does not exist */
186 __asm__("1: movl %%cr4, %0 \n"
188 ".section __ex_table,\"a\" \n"
191 : "=r" (cr4): "0" (0));
192 printk("CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n", cr0, cr2, cr3, cr4);
193 show_trace(NULL, ®s->esp);
197 * This gets run with %ebx containing the
198 * function to call, and %edx containing
201 extern void kernel_thread_helper(void);
203 "kernel_thread_helper:\n\t"
211 * Create a kernel thread
213 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
217 memset(®s, 0, sizeof(regs));
219 regs.ebx = (unsigned long) fn;
220 regs.edx = (unsigned long) arg;
222 regs.xds = __USER_DS;
223 regs.xes = __USER_DS;
225 regs.eip = (unsigned long) kernel_thread_helper;
226 regs.xcs = __KERNEL_CS;
229 /* Ok, create the new process.. */
230 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, ®s, 0, NULL, NULL);
234 * Free current thread data structures etc..
236 void exit_thread(void)
238 struct task_struct *tsk = current;
240 /* The process may have allocated an io port bitmap... nuke it. */
241 if (unlikely(NULL != tsk->thread.ts_io_bitmap)) {
242 kfree(tsk->thread.ts_io_bitmap);
243 tsk->thread.ts_io_bitmap = NULL;
247 void flush_thread(void)
249 struct task_struct *tsk = current;
251 memset(tsk->thread.debugreg, 0, sizeof(unsigned long)*8);
252 memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
254 * Forget coprocessor state..
260 void release_thread(struct task_struct *dead_task)
263 // temporary debugging check
264 if (dead_task->mm->context.size) {
265 printk("WARNING: dead process %8s still has LDT? <%p/%d>\n",
267 dead_task->mm->context.ldt,
268 dead_task->mm->context.size);
273 release_x86_irqs(dead_task);
277 * This gets called before we allocate a new thread and copy
278 * the current task into it.
280 void prepare_to_copy(struct task_struct *tsk)
285 int copy_thread(int nr, unsigned long clone_flags, unsigned long esp,
286 unsigned long unused,
287 struct task_struct * p, struct pt_regs * regs)
289 struct pt_regs * childregs;
290 struct task_struct *tsk;
293 childregs = ((struct pt_regs *) (THREAD_SIZE + (unsigned long) p->thread_info)) - 1;
294 struct_cpy(childregs, regs);
296 childregs->esp = esp;
297 p->set_child_tid = p->clear_child_tid = NULL;
299 p->thread.esp = (unsigned long) childregs;
300 p->thread.esp0 = (unsigned long) (childregs+1);
302 p->thread.eip = (unsigned long) ret_from_fork;
304 savesegment(fs,p->thread.fs);
305 savesegment(gs,p->thread.gs);
308 if (unlikely(NULL != tsk->thread.ts_io_bitmap)) {
309 p->thread.ts_io_bitmap = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
310 if (!p->thread.ts_io_bitmap)
312 memcpy(p->thread.ts_io_bitmap, tsk->thread.ts_io_bitmap,
317 * Set a new TLS for the child thread?
319 if (clone_flags & CLONE_SETTLS) {
320 struct desc_struct *desc;
321 struct user_desc info;
325 if (copy_from_user(&info, (void __user *)childregs->esi, sizeof(info)))
328 if (LDT_empty(&info))
331 idx = info.entry_number;
332 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
335 desc = p->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
336 desc->a = LDT_entry_a(&info);
337 desc->b = LDT_entry_b(&info);
342 if (err && p->thread.ts_io_bitmap)
343 kfree(p->thread.ts_io_bitmap);
348 * fill in the user structure for a core dump..
350 void dump_thread(struct pt_regs * regs, struct user * dump)
354 /* changed the size calculations - should hopefully work better. lbt */
355 dump->magic = CMAGIC;
356 dump->start_code = 0;
357 dump->start_stack = regs->esp & ~(PAGE_SIZE - 1);
358 dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
359 dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT;
360 dump->u_dsize -= dump->u_tsize;
362 for (i = 0; i < 8; i++)
363 dump->u_debugreg[i] = current->thread.debugreg[i];
365 if (dump->start_stack < TASK_SIZE)
366 dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT;
368 dump->regs.ebx = regs->ebx;
369 dump->regs.ecx = regs->ecx;
370 dump->regs.edx = regs->edx;
371 dump->regs.esi = regs->esi;
372 dump->regs.edi = regs->edi;
373 dump->regs.ebp = regs->ebp;
374 dump->regs.eax = regs->eax;
375 dump->regs.ds = regs->xds;
376 dump->regs.es = regs->xes;
377 savesegment(fs,dump->regs.fs);
378 savesegment(gs,dump->regs.gs);
379 dump->regs.orig_eax = regs->orig_eax;
380 dump->regs.eip = regs->eip;
381 dump->regs.cs = regs->xcs;
382 dump->regs.eflags = regs->eflags;
383 dump->regs.esp = regs->esp;
384 dump->regs.ss = regs->xss;
386 dump->u_fpvalid = dump_fpu (regs, &dump->i387);
390 * Capture the user space registers if the task is not running (in user space)
392 int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
394 struct pt_regs ptregs;
396 ptregs = *(struct pt_regs *)
397 ((unsigned long)tsk->thread_info+THREAD_SIZE - sizeof(ptregs));
398 ptregs.xcs &= 0xffff;
399 ptregs.xds &= 0xffff;
400 ptregs.xes &= 0xffff;
401 ptregs.xss &= 0xffff;
403 elf_core_copy_regs(regs, &ptregs);
409 * This special macro can be used to load a debugging register
411 #define loaddebug(thread,register) \
412 __asm__("movl %0,%%db" #register \
414 :"r" (thread->debugreg[register]))
417 * switch_to(x,yn) should switch tasks from x to y.
419 * We fsave/fwait so that an exception goes off at the right time
420 * (as a call from the fsave or fwait in effect) rather than to
421 * the wrong process. Lazy FP saving no longer makes any sense
422 * with modern CPU's, and this simplifies a lot of things (SMP
423 * and UP become the same).
425 * NOTE! We used to use the x86 hardware context switching. The
426 * reason for not using it any more becomes apparent when you
427 * try to recover gracefully from saved state that is no longer
428 * valid (stale segment register values in particular). With the
429 * hardware task-switch, there is no way to fix up bad state in
430 * a reasonable manner.
432 * The fact that Intel documents the hardware task-switching to
433 * be slow is a fairly red herring - this code is not noticeably
434 * faster. However, there _is_ some room for improvement here,
435 * so the performance issues may eventually be a valid point.
436 * More important, however, is the fact that this allows us much
439 * The return value (in %eax) will be the "prev" task after
440 * the task-switch, and shows up in ret_from_fork in entry.S,
443 struct task_struct * __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
445 struct thread_struct *prev = &prev_p->thread,
446 *next = &next_p->thread;
447 int cpu = smp_processor_id();
448 struct tss_struct *tss = init_tss + cpu;
450 /* never put a printk in __switch_to... printk() calls wake_up*() indirectly */
455 * Reload esp0, LDT and the page table pointer:
457 load_esp0(tss, next->esp0);
460 * Load the per-thread Thread-Local Storage descriptor.
465 * Save away %fs and %gs. No need to save %es and %ds, as
466 * those are always kernel segments while inside the kernel.
468 asm volatile("movl %%fs,%0":"=m" (*(int *)&prev->fs));
469 asm volatile("movl %%gs,%0":"=m" (*(int *)&prev->gs));
472 * Restore %fs and %gs if needed.
474 if (unlikely(prev->fs | prev->gs | next->fs | next->gs)) {
475 loadsegment(fs, next->fs);
476 loadsegment(gs, next->gs);
480 * Now maybe reload the debug registers
482 if (unlikely(next->debugreg[7])) {
492 if (unlikely(prev->ts_io_bitmap || next->ts_io_bitmap)) {
493 if (next->ts_io_bitmap) {
495 * 4 cachelines copy ... not good, but not that
496 * bad either. Anyone got something better?
497 * This only affects processes which use ioperm().
498 * [Putting the TSSs into 4k-tlb mapped regions
499 * and playing VM tricks to switch the IO bitmap
500 * is not really acceptable.]
502 memcpy(tss->io_bitmap, next->ts_io_bitmap,
504 tss->bitmap = IO_BITMAP_OFFSET;
507 * a bitmap offset pointing outside of the TSS limit
508 * causes a nicely controllable SIGSEGV if a process
509 * tries to use a port IO instruction. The first
510 * sys_ioperm() call sets up the bitmap properly.
512 tss->bitmap = INVALID_IO_BITMAP_OFFSET;
517 asmlinkage int sys_fork(struct pt_regs regs)
519 return do_fork(SIGCHLD, regs.esp, ®s, 0, NULL, NULL);
522 asmlinkage int sys_clone(struct pt_regs regs)
524 unsigned long clone_flags;
526 int __user *parent_tidptr, *child_tidptr;
528 clone_flags = regs.ebx;
530 parent_tidptr = (int __user *)regs.edx;
531 child_tidptr = (int __user *)regs.edi;
534 return do_fork(clone_flags & ~CLONE_IDLETASK, newsp, ®s, 0, parent_tidptr, child_tidptr);
538 * This is trivial, and on the face of it looks like it
539 * could equally well be done in user mode.
541 * Not so, for quite unobvious reasons - register pressure.
542 * In user mode vfork() cannot have a stack frame, and if
543 * done by calling the "clone()" system call directly, you
544 * do not have enough call-clobbered registers to hold all
545 * the information you need.
547 asmlinkage int sys_vfork(struct pt_regs regs)
549 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.esp, ®s, 0, NULL, NULL);
553 * sys_execve() executes a new program.
555 asmlinkage int sys_execve(struct pt_regs regs)
560 filename = getname((char __user *) regs.ebx);
561 error = PTR_ERR(filename);
562 if (IS_ERR(filename))
564 error = do_execve(filename,
565 (char __user * __user *) regs.ecx,
566 (char __user * __user *) regs.edx,
569 current->ptrace &= ~PT_DTRACE;
570 /* Make sure we don't return using sysenter.. */
571 set_thread_flag(TIF_IRET);
579 * These bracket the sleeping functions..
581 extern void scheduling_functions_start_here(void);
582 extern void scheduling_functions_end_here(void);
583 #define first_sched ((unsigned long) scheduling_functions_start_here)
584 #define last_sched ((unsigned long) scheduling_functions_end_here)
586 unsigned long get_wchan(struct task_struct *p)
588 unsigned long ebp, esp, eip;
589 unsigned long stack_page;
591 if (!p || p == current || p->state == TASK_RUNNING)
593 stack_page = (unsigned long)p->thread_info;
595 if (!stack_page || esp < stack_page || esp > 8188+stack_page)
597 /* include/asm-i386/system.h:switch_to() pushes ebp last. */
598 ebp = *(unsigned long *) esp;
600 if (ebp < stack_page || ebp > 8184+stack_page)
602 eip = *(unsigned long *) (ebp+4);
603 if (eip < first_sched || eip >= last_sched)
605 ebp = *(unsigned long *) ebp;
606 } while (count++ < 16);
613 * sys_alloc_thread_area: get a yet unused TLS descriptor index.
615 static int get_free_idx(void)
617 struct thread_struct *t = ¤t->thread;
620 for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
621 if (desc_empty(t->tls_array + idx))
622 return idx + GDT_ENTRY_TLS_MIN;
627 * Set a given TLS descriptor:
629 asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
631 struct thread_struct *t = ¤t->thread;
632 struct user_desc info;
633 struct desc_struct *desc;
636 if (copy_from_user(&info, u_info, sizeof(info)))
638 idx = info.entry_number;
641 * index -1 means the kernel should try to find and
642 * allocate an empty descriptor:
645 idx = get_free_idx();
648 if (put_user(idx, &u_info->entry_number))
652 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
655 desc = t->tls_array + idx - GDT_ENTRY_TLS_MIN;
658 * We must not get preempted while modifying the TLS.
662 if (LDT_empty(&info)) {
666 desc->a = LDT_entry_a(&info);
667 desc->b = LDT_entry_b(&info);
677 * Get the current Thread-Local Storage area:
680 #define GET_BASE(desc) ( \
681 (((desc)->a >> 16) & 0x0000ffff) | \
682 (((desc)->b << 16) & 0x00ff0000) | \
683 ( (desc)->b & 0xff000000) )
685 #define GET_LIMIT(desc) ( \
686 ((desc)->a & 0x0ffff) | \
687 ((desc)->b & 0xf0000) )
689 #define GET_32BIT(desc) (((desc)->b >> 23) & 1)
690 #define GET_CONTENTS(desc) (((desc)->b >> 10) & 3)
691 #define GET_WRITABLE(desc) (((desc)->b >> 9) & 1)
692 #define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1)
693 #define GET_PRESENT(desc) (((desc)->b >> 15) & 1)
694 #define GET_USEABLE(desc) (((desc)->b >> 20) & 1)
696 asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
698 struct user_desc info;
699 struct desc_struct *desc;
702 if (get_user(idx, &u_info->entry_number))
704 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
707 desc = current->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
709 info.entry_number = idx;
710 info.base_addr = GET_BASE(desc);
711 info.limit = GET_LIMIT(desc);
712 info.seg_32bit = GET_32BIT(desc);
713 info.contents = GET_CONTENTS(desc);
714 info.read_exec_only = !GET_WRITABLE(desc);
715 info.limit_in_pages = GET_LIMIT_PAGES(desc);
716 info.seg_not_present = !GET_PRESENT(desc);
717 info.useable = GET_USEABLE(desc);
719 if (copy_to_user(u_info, &info, sizeof(info)))