UbixOS  2.0
fork.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2002-2018 The UbixOS Project.
3  * All rights reserved.
4  *
5  * This was developed by Christopher W. Olsen for the UbixOS Project.
6  *
7  * Redistribution and use in source and binary forms, with or without modification, are permitted
8  * provided that the following conditions are met:
9  *
10  * 1) Redistributions of source code must retain the above copyright notice, this list of
11  * conditions, the following disclaimer and the list of authors.
12  * 2) Redistributions in binary form must reproduce the above copyright notice, this list of
13  * conditions, the following disclaimer and the list of authors in the documentation and/or
14  * other materials provided with the distribution.
15  * 3) Neither the name of the UbixOS Project nor the names of its contributors may be used to
16  * endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <ubixos/fork.h>
30 #include <sys/types.h>
31 #include <ubixos/sched.h>
32 #include <ubixos/tty.h>
33 #include <ubixos/vitals.h>
34 #include <vmm/vmm.h>
35 #include <string.h>
36 #include <assert.h>
37 #include <lib/kprintf.h>
38 #include <sys/descrip.h>
39 
40 int sys_fork(struct thread *td, struct sys_fork_args *args) {
41  struct taskStruct *newProcess;
42 
43  newProcess = schedNewTask();
44 
45  /*
46  *
47  * Initalize New Task Information From Parrent
48  *
49  */
50 
51  /* Set CWD */
52  memcpy(newProcess->oInfo.cwd, _current->oInfo.cwd, 1024);
53 
54  /* Set PPID */
55  newProcess->ppid = _current->id;
56 
57  /* Set PGRP */
58  newProcess->pgrp = _current->pgrp;
59 
60  /* Copy File Descriptor Table */
61  //memcpy(newProcess->files, _current->files, sizeof(fileDescriptor_t *) * MAX_OFILES);
62  for (int i = 3; i < 64; i++)
63  if (td->o_files[i]) {
64  newProcess->td.o_files[i] = (struct file *)kmalloc(sizeof(struct file));
65  memcpy(newProcess->td.o_files[i], td->o_files[i], sizeof(struct file));
66  if (((struct file *)td->o_files[i])->fd) {
67  ((struct file *)newProcess->td.o_files[i])->fd = kmalloc(sizeof(fileDescriptor_t));
68  memcpy( ((struct file *)newProcess->td.o_files[i])->fd, ((struct file *)td->o_files[i])->fd, sizeof(fileDescriptor_t));
69  if (((struct file *)td->o_files[i])->fd->buffer) {
70  ((struct file *)newProcess->td.o_files[i])->fd->buffer = kmalloc(4096);
71  memcpy(((struct file *)newProcess->td.o_files[i])->fd->buffer, ((struct file *)td->o_files[i])->fd->buffer, 4096);
72  }
73  }
74  }
75 
76  /* Set Up Task State */
77  newProcess->tss.eip = td->frame->tf_eip;
78  newProcess->oInfo.vmStart = _current->oInfo.vmStart;
79  newProcess->term = _current->term;
80  newProcess->term->owner = newProcess->id;
81  newProcess->uid = _current->uid;
82  newProcess->gid = _current->gid;
83  newProcess->tss.back_link = 0x0;
84  newProcess->tss.esp0 = _current->tss.esp0;
85  newProcess->tss.ss0 = 0x10;
86  newProcess->tss.esp1 = 0x0;
87  newProcess->tss.ss1 = 0x0;
88  newProcess->tss.esp2 = 0x0;
89  newProcess->tss.ss2 = 0x0;
90  newProcess->tss.eflags = td->frame->tf_eflags;
91  newProcess->tss.eax = 0x0;
92  newProcess->tss.ebx = td->frame->tf_ebx;
93  newProcess->tss.ecx = td->frame->tf_ecx;
94  newProcess->tss.edx = td->frame->tf_edx;
95  newProcess->tss.esi = td->frame->tf_esi;
96  newProcess->tss.edi = td->frame->tf_edi;
97  newProcess->tss.ebp = td->frame->tf_ebp;
98  newProcess->tss.esp = td->frame->tf_esp;
99  newProcess->tss.cs = td->frame->tf_cs; // & 0xFF;
100  newProcess->tss.ss = td->frame->tf_ss; // & 0xFF;
101  newProcess->tss.ds = td->frame->tf_ds; //_current->tss.ds & 0xFF;
102  newProcess->tss.fs = td->frame->tf_fs; //_current->tss.fs & 0xFF;
103  newProcess->tss.gs = _current->tss.gs & 0xFF;
104  newProcess->tss.es = td->frame->tf_es; //_current->tss.es & 0xFF;
105  newProcess->tss.ldt = 0x18;
106  newProcess->tss.trace_bitmap = 0x0000;
107  newProcess->tss.io_map = 0x8000;
108 
109  newProcess->td.vm_tsize = _current->td.vm_tsize;
110  newProcess->td.vm_taddr = _current->td.vm_taddr;
111  newProcess->td.vm_dsize = _current->td.vm_dsize;
112  newProcess->td.vm_daddr = _current->td.vm_daddr;
113 
114  //kprintf("Copying Mem Space! [0x%X:0x%X:0x%X:0x%X:0x%X:%i:%i]\n", newProcess->tss.esp0, newProcess->tss.esp, newProcess->tss.ebp, td->frame->tf_esi, td->frame->tf_eip, newProcess->id, _current->id);
115 
116  newProcess->tss.cr3 = (uInt32) vmm_copyVirtualSpace(newProcess->id);
117  //kprintf( "Copied Mem Space! [0x%X]\n", newProcess->tss.cr3 );
118 
119  newProcess->state = FORK;
120  /* Fix gcc optimization problems */
121  while (newProcess->state == FORK)
122  sched_yield();
123 
124  newProcess->parent = _current;
125  _current->children++;
126 
127  /* Return Id of Proccess */
128  td->td_retval[0] = newProcess->id;
129  return (0);
130 
131 }
132 
133 /*****************************************************************************************
134  Functoin: static int fork_copyProcess(struct taskStruct *newProcess,long ebp,long edi,
135  long esi, long none,long ebx,long ecx,long edx,long eip,long cs,long eflags,
136  long esp,long ss)
137 
138  Desc: This function will copy a process
139 
140  Notes:
141 
142  *****************************************************************************************/
143 
144 /* Had to remove static though tihs function is only used in this file */
145 int fork_copyProcess(struct taskStruct *newProcess, long ebp, long edi, long esi, long none, long ebx, long ecx, long edx, long eip, long cs, long eflags, long esp, long ss) {
146  volatile struct taskStruct * tmpProcPtr = newProcess;
147  assert(newProcess);
148  assert(_current);
149 
150  /* Set Up New Tasks Information */
151  memcpy(newProcess->oInfo.cwd, _current->oInfo.cwd, 1024);
152  //kprintf( "Initializing New CWD!\n" );
153 
154  newProcess->tss.eip = eip;
155  newProcess->oInfo.vmStart = _current->oInfo.vmStart;
156  newProcess->term = _current->term;
157  newProcess->term->owner = newProcess->id;
158  newProcess->uid = _current->uid;
159  newProcess->gid = _current->gid;
160  newProcess->tss.back_link = 0x0;
161  newProcess->tss.esp0 = _current->tss.esp0;
162  newProcess->tss.ss0 = 0x10;
163  newProcess->tss.esp1 = 0x0;
164  newProcess->tss.ss1 = 0x0;
165  newProcess->tss.esp2 = 0x0;
166  newProcess->tss.ss2 = 0x0;
167  newProcess->tss.eflags = eflags;
168  newProcess->tss.eax = 0x0;
169  newProcess->tss.ebx = ebx;
170  newProcess->tss.ecx = ecx;
171  newProcess->tss.edx = edx;
172  newProcess->tss.esi = esi;
173  newProcess->tss.edi = edi;
174  newProcess->tss.ebp = ebp;
175  newProcess->tss.esp = esp;
176  newProcess->tss.cs = cs & 0xFF;
177  newProcess->tss.ss = ss & 0xFF;
178  newProcess->tss.ds = _current->tss.ds & 0xFF;
179  newProcess->tss.fs = _current->tss.fs & 0xFF;
180  newProcess->tss.gs = _current->tss.gs & 0xFF;
181  newProcess->tss.es = _current->tss.es & 0xFF;
182  newProcess->tss.ldt = 0x18;
183  newProcess->tss.trace_bitmap = 0x0000;
184  newProcess->tss.io_map = 0x8000;
185 
186  newProcess->td.vm_tsize = _current->td.vm_tsize;
187  newProcess->td.vm_taddr = _current->td.vm_taddr;
188  newProcess->td.vm_dsize = _current->td.vm_dsize;
189  newProcess->td.vm_daddr = _current->td.vm_daddr;
190 
191  /* Create A Copy Of The VM Space For New Task */
192  //MrOlsen 2018kprintf("Copying Mem Space! [0x%X:0x%X:0x%X:0x%X:0x%X:%i:%i:0x%X]\n", newProcess->tss.esp0, newProcess->tss.esp, newProcess->tss.ebp, esi, eip, newProcess->id, _current->id, newProcess->td.vm_daddr);
193  newProcess->tss.cr3 = (uInt32) vmm_copyVirtualSpace(newProcess->id);
194  //kprintf( "Copied Mem Space!\n" );
195 
196  newProcess->state = FORK;
197 
198  /* Fix gcc optimization problems */
199  while (tmpProcPtr->state == FORK)
200  sched_yield();
201  /* Return Id of Proccess */
202  kprintf("Returning! [%i]", _current->id);
203 
204  return (newProcess->id);
205 }
206 
207 void qT() {
208  kprintf("qT\n");
209 }
210 
211 /*****************************************************************************************
212  Functoin: void sysFork();
213 
214  Desc: This function will fork a new task
215 
216  Notes:
217 
218  08/01/02 - This Seems To Be Working Fine However I'm Not Sure If I
219  Chose The Best Path To Impliment It I Guess We Will See
220  What The Future May Bring
221 
222  *****************************************************************************************/
223 //asm volatile(
224 __asm(
225  ".globl sysFork_old \n"
226  "sysFork_old: \n"
227  " xor %eax,%eax \n"
228  " call schedNewTask \n"
229  " testl %eax,%eax \n"
230  " je fork_ret \n"
231  " pushl %esi \n"
232  " pushl %edi \n"
233  " pushl %ebp \n"
234  " pushl %eax \n"
235  " call fork_copyProcess \n"
236  " movl %eax,(%ebx) \n"
237  " addl $16,%esp \n"
238  "fork_ret: \n"
239  " ret \n"
240 );
241 
242 /***
243  END
244  ***/
245 
taskStruct
Definition: sched.h:62
trapframe::tf_ds
int tf_ds
Definition: trap.h:38
trapframe::tf_eip
int tf_eip
Definition: trap.h:50
uInt32
unsigned long int uInt32
Definition: objgfx30.h:49
tssStruct::eip
long eip
Definition: tss.h:47
thread::o_files
void * o_files[512]
Definition: thread.h:42
fork.h
tssStruct::ebx
long ebx
Definition: tss.h:49
string.h
tssStruct::edx
long edx
Definition: tss.h:49
tssStruct::ss1
short ss1
Definition: tss.h:41
fileDescriptor
Definition: file.h:62
tssStruct::ldt
short ldt
Definition: tss.h:66
file
Definition: descrip.h:67
thread
Definition: thread.h:40
tssStruct::ds
short ds
Definition: tss.h:60
assert
#define assert(e)
Definition: assert.h:64
assert.h
trapframe::tf_esi
int tf_esi
Definition: trap.h:40
tssStruct::eflags
long eflags
Definition: tss.h:48
vmm.h
tssStruct::esp0
long esp0
Definition: tss.h:37
trapframe::tf_ebp
int tf_ebp
Definition: trap.h:41
tssStruct::back_link
short back_link
Definition: tss.h:35
taskStruct::tss
struct tssStruct tss
Definition: sched.h:67
taskStruct::ppid
pidType ppid
Definition: sched.h:86
FORK
Definition: sched.h:47
tssStruct::ss0
short ss0
Definition: tss.h:38
sched.h
types.h
trapframe::tf_eflags
int tf_eflags
Definition: trap.h:52
trapframe::tf_es
int tf_es
Definition: trap.h:37
tssStruct::ss2
short ss2
Definition: tss.h:44
memcpy
void * memcpy(const void *dst, const void *src, size_t length)
taskStruct::td
struct thread td
Definition: sched.h:78
osInfo::vmStart
uInt32 vmStart
Definition: sched.h:54
tssStruct::esp
long esp
Definition: tss.h:50
taskStruct::id
pidType id
Definition: sched.h:63
thread::vm_taddr
u_long vm_taddr
Definition: thread.h:45
thread::td_retval
int td_retval[2]
Definition: thread.h:41
kprintf.h
taskStruct::term
tty_term * term
Definition: sched.h:77
__asm
__asm(".globl sysFork_old \n" "sysFork_old: \n" " xor %eax,%eax \n" " call schedNewTask \n" " testl %eax,%eax \n" " je fork_ret \n" " pushl %esi \n" " pushl %edi \n" " pushl %ebp \n" " pushl %eax \n" " call fork_copyProcess \n" " movl %eax,(%ebx) \n" " addl $16,%esp \n" "fork_ret: \n" " ret \n")
vitals.h
trapframe::tf_fs
int tf_fs
Definition: trap.h:36
tssStruct::ss
short ss
Definition: tss.h:58
tssStruct::esp1
long esp1
Definition: tss.h:40
tty.h
trapframe::tf_ecx
int tf_ecx
Definition: trap.h:45
taskStruct::parent
struct taskStruct * parent
Definition: sched.h:90
tssStruct::es
short es
Definition: tss.h:54
thread::frame
struct trapframe * frame
Definition: thread.h:47
thread::vm_dsize
u_long vm_dsize
Definition: thread.h:44
_current
kTask_t * _current
Definition: sched.c:50
tssStruct::cs
short cs
Definition: tss.h:56
schedNewTask
kTask_t * schedNewTask()
Definition: sched.c:135
tssStruct::fs
short fs
Definition: tss.h:62
vmm_copyVirtualSpace
void * vmm_copyVirtualSpace(pidType)
Definition: copyvirtualspace.c:51
thread::vm_daddr
u_long vm_daddr
Definition: thread.h:43
tssStruct::edi
long edi
Definition: tss.h:53
taskStruct::uid
uint32_t uid
Definition: sched.h:73
tssStruct::ecx
long ecx
Definition: tss.h:49
tssStruct::io_map
short io_map
Definition: tss.h:69
tssStruct::ebp
long ebp
Definition: tss.h:51
taskStruct::pgrp
uint32_t pgrp
Definition: sched.h:87
kmalloc
void * kmalloc(uInt32 len)
Definition: kmalloc.c:241
taskStruct::state
tState state
Definition: sched.h:72
descrip.h
sys_fork_args
Definition: sysproto_posix.h:56
tssStruct::esi
long esi
Definition: tss.h:52
trapframe::tf_ss
int tf_ss
Definition: trap.h:55
taskStruct::children
uint32_t children
Definition: sched.h:88
trapframe::tf_esp
int tf_esp
Definition: trap.h:54
tssStruct::trace_bitmap
short trace_bitmap
Definition: tss.h:68
fork_copyProcess
int fork_copyProcess(struct taskStruct *newProcess, long ebp, long edi, long esi, long none, long ebx, long ecx, long edx, long eip, long cs, long eflags, long esp, long ss)
Definition: fork.c:48
trapframe::tf_edx
int tf_edx
Definition: trap.h:44
thread::vm_tsize
u_long vm_tsize
Definition: thread.h:46
kprintf
int kprintf(const char *,...)
Definition: kprintf.c:259
trapframe::tf_edi
int tf_edi
Definition: trap.h:39
tty_termNode::owner
pidType owner
Definition: tty.h:42
tssStruct::cr3
long cr3
Definition: tss.h:46
tssStruct::esp2
long esp2
Definition: tss.h:43
tssStruct::gs
short gs
Definition: tss.h:64
qT
void qT()
Definition: fork.c:206
taskStruct::oInfo
struct osInfo oInfo
Definition: sched.h:69
tssStruct::eax
long eax
Definition: tss.h:49
osInfo::cwd
char cwd[1024]
Definition: sched.h:58
trapframe::tf_cs
int tf_cs
Definition: trap.h:51
sys_fork
int sys_fork(struct thread *td, struct sys_fork_args *args)
Definition: fork.c:40
sched_yield
void sched_yield()
Definition: sched.c:244
taskStruct::gid
uint32_t gid
Definition: sched.h:73
trapframe::tf_ebx
int tf_ebx
Definition: trap.h:43