root/arch/i386/kernel/process.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. disable_hlt
  2. enable_hlt
  3. sys_idle
  4. kb_wait
  5. hard_reset_now
  6. show_regs
  7. exit_thread
  8. flush_thread
  9. release_thread
  10. copy_thread
  11. dump_fpu
  12. dump_thread
  13. sys_fork
  14. sys_clone
  15. sys_execve

   1 /*
   2  *  linux/arch/i386/kernel/process.c
   3  *
   4  *  Copyright (C) 1995  Linus Torvalds
   5  */
   6 
   7 /*
   8  * This file handles the architecture-dependent parts of process handling..
   9  */
  10 
  11 #include <linux/errno.h>
  12 #include <linux/sched.h>
  13 #include <linux/kernel.h>
  14 #include <linux/mm.h>
  15 #include <linux/stddef.h>
  16 #include <linux/unistd.h>
  17 #include <linux/ptrace.h>
  18 #include <linux/malloc.h>
  19 #include <linux/ldt.h>
  20 #include <linux/user.h>
  21 #include <linux/a.out.h>
  22 
  23 #include <asm/segment.h>
  24 #include <asm/pgtable.h>
  25 #include <asm/system.h>
  26 #include <asm/io.h>
  27 #include <linux/smp.h>
  28 
  29 
  30 asmlinkage void ret_from_sys_call(void) __asm__("ret_from_sys_call");
  31 
  32 static int hlt_counter=0;
  33 
  34 void disable_hlt(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  35 {
  36         hlt_counter++;
  37 }
  38 
  39 void enable_hlt(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  40 {
  41         hlt_counter--;
  42 }
  43 
  44 /*
  45  * The idle loop on a i386..
  46  */
  47 asmlinkage int sys_idle(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  48 {
  49         if (current->pid != 0)
  50         {
  51         /*      printk("Wrong process idled\n");        SMP bug check */
  52                 return -EPERM;
  53         }
  54 #ifdef __SMP__
  55         /*
  56          *      SMP locking sanity checker
  57          */
  58         if(smp_processor_id()!=active_kernel_processor)
  59                 panic("CPU is %d, kernel CPU is %d in sys_idle!\n",
  60                         smp_processor_id(), active_kernel_processor);
  61         if(syscall_count!=1)
  62                 printk("sys_idle: syscall count is not 1 (%ld)\n", syscall_count);
  63         if(kernel_counter!=1)
  64         {
  65                 printk("CPU %d, sys_idle, kernel_counter is %ld\n", smp_processor_id(), kernel_counter);
  66                 if(!kernel_counter)
  67                         panic("kernel locking botch");
  68         }
  69         /*
  70          *      Until we have C unlocking done
  71          */
  72         current->counter = -100;
  73         schedule();
  74         return 0;
  75 #endif  
  76         /* endless idle loop with no priority at all */
  77         current->counter = -100;
  78         for (;;) {
  79 #ifdef __SMP__
  80                 if (cpu_data[smp_processor_id()].hlt_works_ok && !hlt_counter && !need_resched)
  81 #else   
  82                 if (hlt_works_ok && !hlt_counter && !need_resched)
  83 #endif          
  84                         __asm__("hlt");
  85                 schedule();
  86         }
  87 }
  88 
  89 /*
  90  * This routine reboots the machine by asking the keyboard
  91  * controller to pulse the reset-line low. We try that for a while,
  92  * and if it doesn't work, we do some other stupid things.
  93  */
  94 static long no_idt[2] = {0, 0};
  95 
  96 static inline void kb_wait(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  97 {
  98         int i;
  99 
 100         for (i=0; i<0x10000; i++)
 101                 if ((inb_p(0x64) & 0x02) == 0)
 102                         break;
 103 }
 104 
 105 void hard_reset_now(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 106 {
 107         int i, j;
 108 
 109         sti();
 110 /* rebooting needs to touch the page at absolute addr 0 */
 111         pg0[0] = 7;
 112         *((unsigned short *)0x472) = 0x1234;
 113         for (;;) {
 114                 for (i=0; i<100; i++) {
 115                         kb_wait();
 116                         for(j = 0; j < 100000 ; j++)
 117                                 /* nothing */;
 118                         outb(0xfe,0x64);         /* pulse reset low */
 119                 }
 120                 __asm__ __volatile__("\tlidt %0": "=m" (no_idt));
 121         }
 122 }
 123 
 124 void show_regs(struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 125 {
 126         printk("\n");
 127         printk("EIP: %04x:%08lx",0xffff & regs->cs,regs->eip);
 128         if (regs->cs & 3)
 129                 printk(" ESP: %04x:%08lx",0xffff & regs->ss,regs->esp);
 130         printk(" EFLAGS: %08lx\n",regs->eflags);
 131         printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
 132                 regs->eax,regs->ebx,regs->ecx,regs->edx);
 133         printk("ESI: %08lx EDI: %08lx EBP: %08lx",
 134                 regs->esi, regs->edi, regs->ebp);
 135         printk(" DS: %04x ES: %04x FS: %04x GS: %04x\n",
 136                 0xffff & regs->ds,0xffff & regs->es,
 137                 0xffff & regs->fs,0xffff & regs->gs);
 138 }
 139 
 140 /*
 141  * Free current thread data structures etc..
 142  */
 143 void exit_thread(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 144 {
 145         /* forget lazy i387 state */
 146         if (last_task_used_math == current)
 147                 last_task_used_math = NULL;
 148         /* forget local segments */
 149         __asm__ __volatile__("mov %w0,%%fs ; mov %w0,%%gs ; lldt %w0"
 150                 : /* no outputs */
 151                 : "r" (0));
 152         current->tss.ldt = 0;
 153         if (current->ldt) {
 154                 void * ldt = current->ldt;
 155                 current->ldt = NULL;
 156                 vfree(ldt);
 157         }
 158 }
 159 
 160 void flush_thread(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 161 {
 162         int i;
 163 
 164         if (current->ldt) {
 165                 free_page((unsigned long) current->ldt);
 166                 current->ldt = NULL;
 167                 for (i=1 ; i<NR_TASKS ; i++) {
 168                         if (task[i] == current)  {
 169                                 set_ldt_desc(gdt+(i<<1)+
 170                                              FIRST_LDT_ENTRY,&default_ldt, 1);
 171                                 load_ldt(i);
 172                         }
 173                 }       
 174         }
 175 
 176         for (i=0 ; i<8 ; i++)
 177                 current->debugreg[i] = 0;
 178 }
 179 
 180 void release_thread(struct task_struct *dead_task)
     /* [previous][next][first][last][top][bottom][index][help] */
 181 {
 182 }
 183 
 184 void copy_thread(int nr, unsigned long clone_flags, unsigned long esp,
     /* [previous][next][first][last][top][bottom][index][help] */
 185         struct task_struct * p, struct pt_regs * regs)
 186 {
 187         int i;
 188         struct pt_regs * childregs;
 189 
 190         p->tss.es = KERNEL_DS;
 191         p->tss.cs = KERNEL_CS;
 192         p->tss.ss = KERNEL_DS;
 193         p->tss.ds = KERNEL_DS;
 194         p->tss.fs = USER_DS;
 195         p->tss.gs = KERNEL_DS;
 196         p->tss.ss0 = KERNEL_DS;
 197         p->tss.esp0 = p->kernel_stack_page + PAGE_SIZE;
 198         p->tss.tr = _TSS(nr);
 199         childregs = ((struct pt_regs *) (p->kernel_stack_page + PAGE_SIZE)) - 1;
 200         p->tss.esp = (unsigned long) childregs;
 201         p->tss.eip = (unsigned long) ret_from_sys_call;
 202         *childregs = *regs;
 203         childregs->eax = 0;
 204         childregs->esp = esp;
 205         p->tss.back_link = 0;
 206         p->tss.eflags = regs->eflags & 0xffffcfff;      /* iopl is always 0 for a new process */
 207         p->tss.ldt = _LDT(nr);
 208         if (p->ldt) {
 209                 p->ldt = (struct desc_struct*) vmalloc(LDT_ENTRIES*LDT_ENTRY_SIZE);
 210                 if (p->ldt != NULL)
 211                         memcpy(p->ldt, current->ldt, LDT_ENTRIES*LDT_ENTRY_SIZE);
 212         }
 213         set_tss_desc(gdt+(nr<<1)+FIRST_TSS_ENTRY,&(p->tss));
 214         if (p->ldt)
 215                 set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,p->ldt, 512);
 216         else
 217                 set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,&default_ldt, 1);
 218         p->tss.bitmap = offsetof(struct thread_struct,io_bitmap);
 219         for (i = 0; i < IO_BITMAP_SIZE+1 ; i++) /* IO bitmap is actually SIZE+1 */
 220                 p->tss.io_bitmap[i] = ~0;
 221         if (last_task_used_math == current)
 222                 __asm__("clts ; fnsave %0 ; frstor %0":"=m" (p->tss.i387));
 223 }
 224 
 225 /*
 226  * fill in the fpu structure for a core dump..
 227  */
 228 int dump_fpu (struct user_i387_struct* fpu)
     /* [previous][next][first][last][top][bottom][index][help] */
 229 {
 230         int fpvalid;
 231 
 232 /* Flag indicating the math stuff is valid. We don't support this for the
 233    soft-float routines yet */
 234         if (hard_math) {
 235                 if ((fpvalid = current->used_math) != 0) {
 236                         if (last_task_used_math == current)
 237                                 __asm__("clts ; fnsave %0": :"m" (*fpu));
 238                         else
 239                                 memcpy(fpu,&current->tss.i387.hard,sizeof(*fpu));
 240                 }
 241         } else {
 242                 /* we should dump the emulator state here, but we need to
 243                    convert it into standard 387 format first.. */
 244                 fpvalid = 0;
 245         }
 246 
 247         return fpvalid;
 248 }
 249 
 250 /*
 251  * fill in the user structure for a core dump..
 252  */
 253 void dump_thread(struct pt_regs * regs, struct user * dump)
     /* [previous][next][first][last][top][bottom][index][help] */
 254 {
 255         int i;
 256 
 257 /* changed the size calculations - should hopefully work better. lbt */
 258         dump->magic = CMAGIC;
 259         dump->start_code = 0;
 260         dump->start_stack = regs->esp & ~(PAGE_SIZE - 1);
 261         dump->u_tsize = ((unsigned long) current->mm->end_code) >> 12;
 262         dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> 12;
 263         dump->u_dsize -= dump->u_tsize;
 264         dump->u_ssize = 0;
 265         for (i = 0; i < 8; i++)
 266                 dump->u_debugreg[i] = current->debugreg[i];  
 267 
 268         if (dump->start_stack < TASK_SIZE) {
 269                 dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> 12;
 270         }
 271 
 272         dump->regs = *regs;
 273 
 274         dump->u_fpvalid = dump_fpu (&dump->i387);
 275 }
 276 
 277 asmlinkage int sys_fork(struct pt_regs regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 278 {
 279         return do_fork(SIGCHLD, regs.esp, &regs);
 280 }
 281 
 282 asmlinkage int sys_clone(struct pt_regs regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 283 {
 284         unsigned long clone_flags;
 285         unsigned long newsp;
 286 
 287         clone_flags = regs.ebx;
 288         newsp = regs.ecx;
 289         if (!newsp)
 290                 newsp = regs.esp;
 291         return do_fork(clone_flags, newsp, &regs);
 292 }
 293 
 294 /*
 295  * sys_execve() executes a new program.
 296  */
 297 asmlinkage int sys_execve(struct pt_regs regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 298 {
 299         int error;
 300         char * filename;
 301 
 302         error = getname((char *) regs.ebx, &filename);
 303         if (error)
 304                 return error;
 305         error = do_execve(filename, (char **) regs.ecx, (char **) regs.edx, &regs);
 306         putname(filename);
 307         return error;
 308 }

/* [previous][next][first][last][top][bottom][index][help] */