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

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