[source navigation] [diff markup] [identifier search] [freetext search] [file search]

Oldlinux Cross Reference
Linux/kernel/fork.c

Version: [1.0] [0.99.11] [0.99] [0.98] [0.97] [0.96a] [0.95] [0.12] [0.11] [0.01]
Architecture: [i386]

  1 /*
  2  *  linux/kernel/fork.c
  3  *
  4  *  (C) 1991  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 #include <errno.h>
 14 
 15 #include <linux/sched.h>
 16 #include <linux/kernel.h>
 17 #include <asm/segment.h>
 18 #include <asm/system.h>
 19 
 20 extern void write_verify(unsigned long address);
 21 
 22 long last_pid=0;
 23 
 24 void verify_area(void * addr,int size)
 25 {
 26         unsigned long start;
 27 
 28         start = (unsigned long) addr;
 29         size += start & 0xfff;
 30         start &= 0xfffff000;
 31         start += get_base(current->ldt[2]);
 32         while (size>0) {
 33                 size -= 4096;
 34                 write_verify(start);
 35                 start += 4096;
 36         }
 37 }
 38 
 39 int copy_mem(int nr,struct task_struct * p)
 40 {
 41         unsigned long old_data_base,new_data_base,data_limit;
 42         unsigned long old_code_base,new_code_base,code_limit;
 43 
 44         code_limit=get_limit(0x0f);
 45         data_limit=get_limit(0x17);
 46         old_code_base = get_base(current->ldt[1]);
 47         old_data_base = get_base(current->ldt[2]);
 48         if (old_data_base != old_code_base)
 49                 panic("We don't support separate I&D");
 50         if (data_limit < code_limit)
 51                 panic("Bad data_limit");
 52         new_data_base = new_code_base = nr * 0x4000000;
 53         p->start_code = new_code_base;
 54         set_base(p->ldt[1],new_code_base);
 55         set_base(p->ldt[2],new_data_base);
 56         if (copy_page_tables(old_data_base,new_data_base,data_limit)) {
 57                 free_page_tables(new_data_base,data_limit);
 58                 return -ENOMEM;
 59         }
 60         return 0;
 61 }
 62 
 63 /*
 64  *  Ok, this is the main fork-routine. It copies the system process
 65  * information (task[nr]) and sets up the necessary registers. It
 66  * also copies the data segment in it's entirety.
 67  */
 68 int copy_process(int nr,long ebp,long edi,long esi,long gs,long none,
 69                 long ebx,long ecx,long edx,
 70                 long fs,long es,long ds,
 71                 long eip,long cs,long eflags,long esp,long ss)
 72 {
 73         struct task_struct *p;
 74         int i;
 75         struct file *f;
 76 
 77         p = (struct task_struct *) get_free_page();
 78         if (!p)
 79                 return -EAGAIN;
 80         task[nr] = p;
 81         *p = *current;  /* NOTE! this doesn't copy the supervisor stack */
 82         p->state = TASK_UNINTERRUPTIBLE;
 83         p->pid = last_pid;
 84         p->father = current->pid;
 85         p->counter = p->priority;
 86         p->signal = 0;
 87         p->alarm = 0;
 88         p->leader = 0;          /* process leadership doesn't inherit */
 89         p->utime = p->stime = 0;
 90         p->cutime = p->cstime = 0;
 91         p->start_time = jiffies;
 92         p->tss.back_link = 0;
 93         p->tss.esp0 = PAGE_SIZE + (long) p;
 94         p->tss.ss0 = 0x10;
 95         p->tss.eip = eip;
 96         p->tss.eflags = eflags;
 97         p->tss.eax = 0;
 98         p->tss.ecx = ecx;
 99         p->tss.edx = edx;
100         p->tss.ebx = ebx;
101         p->tss.esp = esp;
102         p->tss.ebp = ebp;
103         p->tss.esi = esi;
104         p->tss.edi = edi;
105         p->tss.es = es & 0xffff;
106         p->tss.cs = cs & 0xffff;
107         p->tss.ss = ss & 0xffff;
108         p->tss.ds = ds & 0xffff;
109         p->tss.fs = fs & 0xffff;
110         p->tss.gs = gs & 0xffff;
111         p->tss.ldt = _LDT(nr);
112         p->tss.trace_bitmap = 0x80000000;
113         if (last_task_used_math == current)
114                 __asm__("clts ; fnsave %0"::"m" (p->tss.i387));
115         if (copy_mem(nr,p)) {
116                 task[nr] = NULL;
117                 free_page((long) p);
118                 return -EAGAIN;
119         }
120         for (i=0; i<NR_OPEN;i++)
121                 if (f=p->filp[i])
122                         f->f_count++;
123         if (current->pwd)
124                 current->pwd->i_count++;
125         if (current->root)
126                 current->root->i_count++;
127         if (current->executable)
128                 current->executable->i_count++;
129         set_tss_desc(gdt+(nr<<1)+FIRST_TSS_ENTRY,&(p->tss));
130         set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,&(p->ldt));
131         p->state = TASK_RUNNING;        /* do this last, just in case */
132         return last_pid;
133 }
134 
135 int find_empty_process(void)
136 {
137         int i;
138 
139         repeat:
140                 if ((++last_pid)<0) last_pid=1;
141                 for(i=0 ; i<NR_TASKS ; i++)
142                         if (task[i] && task[i]->pid == last_pid) goto repeat;
143         for(i=1 ; i<NR_TASKS ; i++)
144                 if (!task[i])
145                         return i;
146         return -EAGAIN;
147 }
148 

[source navigation] [diff markup] [identifier search] [freetext search] [file search]

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.