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 <ubixos/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 
38 /*****************************************************************************************
39  Functoin: static int fork_copyProcess(struct taskStruct *newProcess,long ebp,long edi,
40  long esi, long none,long ebx,long ecx,long edx,long eip,long cs,long eflags,
41  long esp,long ss)
42 
43  Desc: This function will copy a process
44 
45  Notes:
46 
47  *****************************************************************************************/
48 /* Had to remove static though tihs function is only used in this file */
49 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) {
50  volatile struct taskStruct * tmpProcPtr = newProcess;
51  assert(newProcess);
53 
54  /* Set Up New Tasks Information */
55  memcpy(newProcess->oInfo.cwd, _current->oInfo.cwd, 1024);
56 
57  newProcess->tss.eip = eip;
58  newProcess->oInfo.vmStart = _current->oInfo.vmStart;
59  newProcess->term = _current->term;
60  newProcess->term->owner = newProcess->id;
61  newProcess->uid = _current->uid;
62  newProcess->gid = _current->gid;
63  newProcess->tss.back_link = 0x0;
64  newProcess->tss.esp0 = _current->tss.esp0;
65  newProcess->tss.ss0 = 0x10;
66  newProcess->tss.esp1 = 0x0;
67  newProcess->tss.ss1 = 0x0;
68  newProcess->tss.esp2 = 0x0;
69  newProcess->tss.ss2 = 0x0;
70  newProcess->tss.eflags = eflags;
71  newProcess->tss.eax = 0x0;
72  newProcess->tss.ebx = ebx;
73  newProcess->tss.ecx = ecx;
74  newProcess->tss.edx = edx;
75  newProcess->tss.esi = esi;
76  newProcess->tss.edi = edi;
77  newProcess->tss.ebp = ebp;
78  newProcess->tss.esp = esp;
79  newProcess->tss.cs = cs & 0xFF;
80  newProcess->tss.ss = ss & 0xFF;
81  newProcess->tss.ds = _current->tss.ds & 0xFF;
82  newProcess->tss.fs = _current->tss.fs & 0xFF;
83  newProcess->tss.gs = _current->tss.gs & 0xFF;
84  newProcess->tss.es = _current->tss.es & 0xFF;
85  newProcess->tss.ldt = 0x18;
86  newProcess->tss.trace_bitmap = 0x0000;
87  newProcess->tss.io_map = 0x8000;
88  /* Create A Copy Of The VM Space For New Task */
89  newProcess->tss.cr3 = (uInt32) vmmCopyVirtualSpace(newProcess->id);
90  newProcess->state = FORK;
91 
92  /* Fix gcc optimization problems */
93  while (tmpProcPtr->state == FORK)
94  sched_yield();
95 
96  /* Return Id of Proccess */
97  return (newProcess->id);
98 }
99 
100 /*****************************************************************************************
101  Functoin: void sysFork();
102 
103  Desc: This function will fork a new task
104 
105  Notes:
106 
107  08/01/02 - This Seems To Be Working Fine However I'm Not Sure If I
108  Chose The Best Path To Impliment It I Guess We Will See
109  What The Future May Bring
110 
111  *****************************************************************************************/
112 asm(
113  ".globl sysFork \n"
114  "sysFork: \n"
115  " xor %eax,%eax \n"
116  " call schedNewTask \n"
117  " testl %eax,%eax \n"
118  " je fork_ret \n"
119  " pushl %esi \n"
120  " pushl %edi \n"
121  " pushl %ebp \n"
122  " pushl %eax \n"
123  " call fork_copyProcess \n"
124  " movl %eax,(%ebx) \n"
125  " addl $16,%esp \n"
126  "fork_ret: \n"
127  " ret \n"
128 );
taskStruct
Definition: sched.h:62
uInt32
unsigned long int uInt32
Definition: objgfx30.h:49
tssStruct::eip
long eip
Definition: tss.h:47
fork.h
i386_frame::cs
uint32_t cs
Definition: tss.h:103
tssStruct::ebx
long ebx
Definition: tss.h:49
string.h
i386_frame::esi
uint32_t esi
Definition: tss.h:91
tssStruct::edx
long edx
Definition: tss.h:49
tssStruct::ss1
short ss1
Definition: tss.h:41
tssStruct::ldt
short ldt
Definition: tss.h:66
i386_frame::esp
uint32_t esp
Definition: tss.h:93
tssStruct::ds
short ds
Definition: tss.h:60
assert
#define assert(e)
Definition: assert.h:64
assert.h
tssStruct::eflags
long eflags
Definition: tss.h:48
vmm.h
tssStruct::esp0
long esp0
Definition: tss.h:37
tssStruct::back_link
short back_link
Definition: tss.h:35
i386_frame::ebx
uint32_t ebx
Definition: tss.h:94
taskStruct::tss
struct tssStruct tss
Definition: sched.h:67
i386_frame::eip
uint32_t eip
Definition: tss.h:102
FORK
Definition: sched.h:47
tssStruct::ss0
short ss0
Definition: tss.h:38
sched.h
tssStruct::ss2
short ss2
Definition: tss.h:44
memcpy
void * memcpy(const void *dst, const void *src, size_t length)
osInfo::vmStart
uInt32 vmStart
Definition: sched.h:54
tssStruct::esp
long esp
Definition: tss.h:50
taskStruct::id
pidType id
Definition: sched.h:63
taskStruct::term
tty_term * term
Definition: sched.h:77
vitals.h
tssStruct::ss
short ss
Definition: tss.h:58
i386_frame::ss
uint32_t ss
Definition: tss.h:89
tssStruct::esp1
long esp1
Definition: tss.h:40
tty.h
i386_frame::ebp
uint32_t ebp
Definition: tss.h:92
tssStruct::es
short es
Definition: tss.h:54
_current
kTask_t * _current
Definition: sched.c:50
tssStruct::cs
short cs
Definition: tss.h:56
tssStruct::fs
short fs
Definition: tss.h:62
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
i386_frame::edx
uint32_t edx
Definition: tss.h:95
taskStruct::state
tState state
Definition: sched.h:72
tssStruct::esi
long esi
Definition: tss.h:52
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
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
i386_frame::ecx
uint32_t ecx
Definition: tss.h:96
tssStruct::gs
short gs
Definition: tss.h:64
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
i386_frame::edi
uint32_t edi
Definition: tss.h:90
sched_yield
void sched_yield()
Definition: sched.c:244
taskStruct::gid
uint32_t gid
Definition: sched.h:73