root/arch/i386/kernel/process.c

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

DEFINITIONS

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

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