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_thread
  11. sys_fork
  12. sys_clone
  13. 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 user structure for a core dump..
 198  */
 199 void dump_thread(struct pt_regs * regs, struct user * dump)
     /* [previous][next][first][last][top][bottom][index][help] */
 200 {
 201         int i;
 202 
 203 /* changed the size calculations - should hopefully work better. lbt */
 204         dump->magic = CMAGIC;
 205         dump->start_code = 0;
 206         dump->start_stack = regs->esp & ~(PAGE_SIZE - 1);
 207         dump->u_tsize = ((unsigned long) current->mm->end_code) >> 12;
 208         dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> 12;
 209         dump->u_dsize -= dump->u_tsize;
 210         dump->u_ssize = 0;
 211         for (i = 0; i < 8; i++)
 212                 dump->u_debugreg[i] = current->debugreg[i];  
 213 
 214         if (dump->start_stack < TASK_SIZE)
 215                 dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> 12;
 216 
 217         dump->regs = *regs;
 218 
 219 /* Flag indicating the math stuff is valid. We don't support this for the
 220    soft-float routines yet */
 221         if (hard_math) {
 222                 if ((dump->u_fpvalid = current->used_math) != 0) {
 223                         if (last_task_used_math == current)
 224                                 __asm__("clts ; fnsave %0": :"m" (dump->i387));
 225                         else
 226                                 memcpy(&dump->i387,&current->tss.i387.hard,sizeof(dump->i387));
 227                 }
 228         } else {
 229                 /* we should dump the emulator state here, but we need to
 230                    convert it into standard 387 format first.. */
 231                 dump->u_fpvalid = 0;
 232         }
 233 }
 234 
 235 asmlinkage int sys_fork(struct pt_regs regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 236 {
 237         return do_fork(COPYVM | SIGCHLD, regs.esp, &regs);
 238 }
 239 
 240 asmlinkage int sys_clone(struct pt_regs regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 241 {
 242 #ifdef CLONE_ACTUALLY_WORKS_OK
 243         unsigned long clone_flags;
 244         unsigned long newsp;
 245 
 246         newsp = regs.ebx;
 247         clone_flags = regs.ecx;
 248         if (!newsp)
 249                 newsp = regs.esp;
 250         if (newsp == regs.esp)
 251                 clone_flags |= COPYVM;
 252         return do_fork(clone_flags, newsp, &regs);
 253 #else
 254         return -ENOSYS;
 255 #endif
 256 }
 257 
 258 /*
 259  * sys_execve() executes a new program.
 260  */
 261 asmlinkage int sys_execve(struct pt_regs regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 262 {
 263         int error;
 264         char * filename;
 265 
 266         error = getname((char *) regs.ebx, &filename);
 267         if (error)
 268                 return error;
 269         error = do_execve(filename, (char **) regs.ecx, (char **) regs.edx, &regs);
 270         putname(filename);
 271         return error;
 272 }

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