root/kernel/fork.c

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

DEFINITIONS

This source file includes following definitions.
  1. verify_area
  2. copy_mem
  3. find_empty_process
  4. sys_fork

   1 /*
   2  *  linux/kernel/fork.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 /*
   8  *  'fork.c' contains the help-routines for the 'fork' system call
   9  * (see also system_call.s), and some misc functions ('verify_area').
  10  * Fork is rather simple, once you get the hang of it, but the memory
  11  * management can be a bitch. See 'mm/mm.c': 'copy_page_tables()'
  12  */
  13 
  14 #include <linux/errno.h>
  15 #include <linux/sched.h>
  16 #include <linux/kernel.h>
  17 #include <linux/mm.h>
  18 #include <linux/stddef.h>
  19 
  20 #include <asm/segment.h>
  21 #include <asm/system.h>
  22 
  23 #define MAX_TASKS_PER_USER ((NR_TASKS/4)*3)
  24 
  25 long last_pid=0;
  26 
  27 void verify_area(void * addr,int size)
     /* [previous][next][first][last][top][bottom][index][help] */
  28 {
  29         unsigned long start;
  30 
  31         start = (unsigned long) addr;
  32         size += start & 0xfff;
  33         start &= 0xfffff000;
  34         start += get_base(current->ldt[2]);
  35         while (size>0) {
  36                 size -= 4096;
  37                 write_verify(start);
  38                 start += 4096;
  39         }
  40 }
  41 
  42 int copy_mem(int nr,struct task_struct * p)
     /* [previous][next][first][last][top][bottom][index][help] */
  43 {
  44         unsigned long old_data_base,new_data_base,data_limit;
  45         unsigned long old_code_base,new_code_base,code_limit;
  46 
  47         code_limit = get_limit(0x0f);
  48         data_limit = get_limit(0x17);
  49         old_code_base = get_base(current->ldt[1]);
  50         old_data_base = get_base(current->ldt[2]);
  51         if (old_data_base != old_code_base) {
  52                 printk("ldt[0]: %08x %08x\n",current->ldt[0].a,current->ldt[0].b);
  53                 printk("ldt[1]: %08x %08x\n",current->ldt[1].a,current->ldt[1].b);
  54                 printk("ldt[2]: %08x %08x\n",current->ldt[2].a,current->ldt[2].b);
  55                 panic("We don't support separate I&D");
  56         }
  57         if (data_limit < code_limit)
  58                 panic("Bad data_limit");
  59         new_data_base = old_data_base;
  60         new_code_base = old_code_base;
  61         p->start_code = new_code_base;
  62         set_base(p->ldt[1],new_code_base);
  63         set_base(p->ldt[2],new_data_base);
  64         return copy_page_tables(p);
  65 }
  66 
  67 static int find_empty_process(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  68 {
  69         int i, task_nr;
  70         int this_user_tasks = 0;
  71 
  72 repeat:
  73         if ((++last_pid) & 0xffff0000)
  74                 last_pid=1;
  75         for(i=0 ; i < NR_TASKS ; i++) {
  76                 if (!task[i])
  77                         continue;
  78                 if (task[i]->uid == current->uid)
  79                         this_user_tasks++;
  80                 if (task[i]->pid == last_pid || task[i]->pgrp == last_pid)
  81                         goto repeat;
  82         }
  83         if (this_user_tasks > MAX_TASKS_PER_USER && !suser())
  84                 return -EAGAIN;
  85 /* Only the super-user can fill the last available slot */
  86         task_nr = 0;
  87         for(i=1 ; i<NR_TASKS ; i++)
  88                 if (!task[i])
  89                         if (task_nr)
  90                                 return task_nr;
  91                         else
  92                                 task_nr = i;
  93         if (task_nr && suser())
  94                 return task_nr;
  95         return -EAGAIN;
  96 }
  97 
  98 /*
  99  *  Ok, this is the main fork-routine. It copies the system process
 100  * information (task[nr]) and sets up the necessary registers. It
 101  * also copies the data segment in it's entirety.
 102  */
 103 int sys_fork(long ebx,long ecx,long edx,
     /* [previous][next][first][last][top][bottom][index][help] */
 104                 long esi, long edi, long ebp, long eax, long ds,
 105                 long es, long fs, long gs, long orig_eax,
 106                 long eip,long cs,long eflags,long esp,long ss)
 107 {
 108         struct task_struct *p;
 109         int i,nr;
 110         struct file *f;
 111 
 112         p = (struct task_struct *) get_free_page(GFP_KERNEL);
 113         if (!p)
 114                 return -EAGAIN;
 115         nr = find_empty_process();
 116         if (nr < 0) {
 117                 free_page((unsigned long) p);
 118                 return nr;
 119         }
 120         task[nr] = p;
 121         *p = *current;
 122         p->kernel_stack_page = 0;
 123         p->state = TASK_UNINTERRUPTIBLE;
 124         p->flags &= ~(PF_PTRACED|PF_TRACESYS);
 125         p->pid = last_pid;
 126         if (p->pid > 1)
 127                 p->swappable = 1;
 128         p->p_pptr = p->p_opptr = current;
 129         p->p_cptr = NULL;
 130         SET_LINKS(p);
 131         p->counter = p->priority;
 132         p->signal = 0;
 133         p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
 134         p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
 135         p->leader = 0;          /* process leadership doesn't inherit */
 136         p->utime = p->stime = 0;
 137         p->cutime = p->cstime = 0;
 138         p->min_flt = p->maj_flt = 0;
 139         p->cmin_flt = p->cmaj_flt = 0;
 140         p->start_time = jiffies;
 141         p->tss.back_link = 0;
 142         p->tss.ss0 = 0x10;
 143         p->tss.eip = eip;
 144         p->tss.eflags = eflags & 0xffffcfff;    /* iopl is always 0 for a new process */
 145         p->tss.eax = 0;
 146         p->tss.ecx = ecx;
 147         p->tss.edx = edx;
 148         p->tss.ebx = ebx;
 149         p->tss.esp = esp;
 150         p->tss.ebp = ebp;
 151         p->tss.esi = esi;
 152         p->tss.edi = edi;
 153         p->tss.es = es & 0xffff;
 154         p->tss.cs = cs & 0xffff;
 155         p->tss.ss = ss & 0xffff;
 156         p->tss.ds = ds & 0xffff;
 157         p->tss.fs = fs & 0xffff;
 158         p->tss.gs = gs & 0xffff;
 159         p->tss.ldt = _LDT(nr);
 160         p->tss.trace_bitmap = offsetof(struct tss_struct,io_bitmap) << 16;
 161         for (i = 0; i<IO_BITMAP_SIZE ; i++)
 162                 p->tss.io_bitmap[i] = ~0;
 163         if (last_task_used_math == current)
 164                 __asm__("clts ; fnsave %0 ; frstor %0"::"m" (p->tss.i387));
 165         p->kernel_stack_page = get_free_page(GFP_KERNEL);
 166         if (!p->kernel_stack_page || copy_mem(nr,p)) {
 167                 task[nr] = NULL;
 168                 REMOVE_LINKS(p);
 169                 free_page(p->kernel_stack_page);
 170                 free_page((long) p);
 171                 return -EAGAIN;
 172         }
 173         p->tss.esp0 = PAGE_SIZE + p->kernel_stack_page;
 174         for (i=0; i<NR_OPEN;i++)
 175                 if (f=p->filp[i])
 176                         f->f_count++;
 177         if (current->pwd)
 178                 current->pwd->i_count++;
 179         if (current->root)
 180                 current->root->i_count++;
 181         if (current->executable)
 182                 current->executable->i_count++;
 183         for (i=0; i < current->numlibraries ; i++)
 184                 if (current->libraries[i].library)
 185                         current->libraries[i].library->i_count++;
 186         set_tss_desc(gdt+(nr<<1)+FIRST_TSS_ENTRY,&(p->tss));
 187         set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,&(p->ldt));
 188         p->state = TASK_RUNNING;        /* do this last, just in case */
 189         return p->pid;
 190 }

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