diff --git a/src/bin/init/main.c b/src/bin/init/main.c index 9ad3bf4..f0a6e8f 100755 --- a/src/bin/init/main.c +++ b/src/bin/init/main.c @@ -33,20 +33,26 @@ } int main() { - int i=0; + int i=0,pid=10; i = getpid(); printf("This is my pid: [%i]\n",i); for (i=0;i<5;i++) { - //printf("Init Code [%i]\n",i); + printf("Init Code [%i]\n",i); } if (getpid() != 1) { printf("Sorry This Program Must Be Started By The Kernel!!!!\n"); - // exit(1); + //exit(1); + while (1); } printf("Initializing system.\n"); - exec("shell"); - while(1) { - //printf("Testing 123\n"); + //exec("shell"); + if (!fork()) { + while (1) { + printf("I'm The Child!\n"); + } + } + while (1) { + printf("I'm The Parent!\n"); } exit(1); } \ No newline at end of file diff --git a/src/lib/libc/include/sys/types.h b/src/lib/libc/include/sys/types.h index dd63005..d599599 100755 --- a/src/lib/libc/include/sys/types.h +++ b/src/lib/libc/include/sys/types.h @@ -29,5 +29,6 @@ typedef unsigned char uChar; typedef unsigned int uInt; typedef unsigned long size_t; +typedef int pid_t; #endif diff --git a/src/lib/libc/include/unistd.h b/src/lib/libc/include/unistd.h index 977d915..f819ca0 100755 --- a/src/lib/libc/include/unistd.h +++ b/src/lib/libc/include/unistd.h @@ -27,5 +27,6 @@ #include uShort getpid(void); +pid_t fork(); #endif \ No newline at end of file diff --git a/src/lib/libc/sys/Makefile b/src/lib/libc/sys/Makefile index 992ade4..6355524 100755 --- a/src/lib/libc/sys/Makefile +++ b/src/lib/libc/sys/Makefile @@ -14,7 +14,7 @@ REMOVE = rm -f #Objects -OBJS = exec.o getpid.o +OBJS = exec.o getpid.o fork.o #Output OUTPUT = sys.so diff --git a/src/lib/libc/sys/fork.c b/src/lib/libc/sys/fork.c new file mode 100755 index 0000000..a1a53dc --- /dev/null +++ b/src/lib/libc/sys/fork.c @@ -0,0 +1,36 @@ +/************************************************************************************** + Copyright (c) 2002 The UbixOS Project + All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list of conditions, the following disclaimer and the list of authors. +Redistributions in binary form must reproduce the above copyright notice, this list of conditions, the following disclaimer and the list of authors +in the documentation and/or other materials provided with the distribution. Neither the name of the UbixOS Project nor the names of its +contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY +OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + $Id$ + +**************************************************************************************/ + +#include + +pid_t fork() { + pid_t pid = 2; + asm( + "movl $4,%%eax\n" +// "movl %0,%%ebx\n" + "int $0x80 \n" + : + : "b" (&pid) + ); + return(pid); + } \ No newline at end of file diff --git a/src/sys/include/ubixos/fork.h b/src/sys/include/ubixos/fork.h index 27d7be5..5495db9 100755 --- a/src/sys/include/ubixos/fork.h +++ b/src/sys/include/ubixos/fork.h @@ -27,5 +27,6 @@ #include pid_t fork(); - +pid_t testFork(); +void sysFork(); #endif diff --git a/src/sys/include/ubixos/syscalls.h b/src/sys/include/ubixos/syscalls.h index 94de452..e3bf128 100755 --- a/src/sys/include/ubixos/syscalls.h +++ b/src/sys/include/ubixos/syscalls.h @@ -28,11 +28,12 @@ void sysGetpid(); void sysExit(); void sysExec(); +void sysFork(); typedef void (*functionPTR)(); functionPTR systemCalls[] = { - sysFwrite,sysGetpid,sysExit,sysExec + sysFwrite,sysGetpid,sysExit,sysExec,sysFork }; int totalCalls = sizeof(systemCalls)/sizeof(functionPTR); diff --git a/src/sys/init/main.c b/src/sys/init/main.c index f74e6f0..bce3231 100755 --- a/src/sys/init/main.c +++ b/src/sys/init/main.c @@ -90,7 +90,8 @@ initUbixFS(); execThread(idleThread,0xAFFF,"Idle Thread"); //execFile("shell"); - execThread(test,0xCFFF,"Test"); + execFile("init"); + //execThread(test,0xCFFF,"Test"); /* while(1) { fd = fopen("COPYRIGHT",1); diff --git a/src/sys/kernel/fork.c b/src/sys/kernel/fork.c index fe29a37..153ab36 100755 --- a/src/sys/kernel/fork.c +++ b/src/sys/kernel/fork.c @@ -33,9 +33,17 @@ uChar *src,*dst; uLong *pageDirsrc,*pageTablesrc,*pageDirdst,*pageTabledst; pid_t cPid = findTask(); //Find An Empty Task - //schedule(); + schedule(); + src = &_current->tss; + dst = &taskList[cPid].tss; + for (i=0;itss.cr3; - pageTabledst = (uLong *)allocPage(); + pageDirdst = (uLong *)allocPage(); + //taskList[cPid].tss.cr3 = pageDirdst; for (i=0;i<1024;i++) { if (pageDirsrc[i] > 0) { pageTablesrc = (uLong *)(pageDirsrc[i] & 0xFFFFF000); @@ -43,7 +51,17 @@ pageTabledst = (uLong *)(pageDirdst[i] & 0xFFFFF000); for (x=0;x<1024;x++) { if (pageTablesrc[x] > 0) { - pageTabledst[x] = pageTablesrc[x] & 0xFFFFFFD; +/* if (((pageTablesrc[x] & 0xFFF) == 7) || ((pageTablesrc[x] & 0xFFF) == 103)) { + kprintf("(%i){%i}",pageTablesrc[x],(pageTablesrc[x] & 0xFFF)); + pageTabledst[x] = pageTablesrc[x]; + } + else { */ + pageTabledst[x] = (pageTablesrc[x] & 0xFFFFFFBD);//(pageTablesrc[x] & 0xFFFFF000) | (pagePresent|pageUser);// & 0xFFFFFFD; + if ((pageTablesrc[x] & 0xFFF) == 103) { + kprintf("[%i]",pageTablesrc[x]); + pageTabledst[x] = pageTablesrc[x]; + } +// } } else { pageTabledst[x] = 0x0; @@ -54,20 +72,129 @@ pageDirdst[i] = 0x0; } } - kprintf("Pid: [%i]\n",cPid); - src = _current; - dst = &taskList[cPid]; - for (i=0;istatus,taskList[cPid].status); + //taskList[cPid].status = ACTIVE; if (_current->id == cPid) { return(0); } else { return(cPid); } - } \ No newline at end of file + } + +pid_t testFork(int nr,long ebp,long edi,long esi,long gs,long none, + long ebx,long ecx,long edx, + long fs,long es,long ds, + long cs,long eflags,long esp,long ss) { + kprintf("nr: [%i]\n",nr); + kprintf("ebp: [%i]\n",ebp); + kprintf("edi: [%i]\n",edi); + kprintf("esi: [%i]\n",esi); + kprintf("gs: [%i]\n",gs); + kprintf("none: [%i]\n",none); + kprintf("ebx: [%i]\n",ebx); + kprintf("ecx: [%i]\n",ecx); + kprintf("edx: [%i]\n",edx); + kprintf("fs: [%i]\n",fs); + kprintf("es: [%i]\n",es); + kprintf("ds: [%i]\n",ds); + kprintf("cs: [%i]\n",cs); + kprintf("eflags: [%i]\n",eflags); + kprintf("esp: [%i]\n",esp); + kprintf("ss: [%i]\n",ss); + return(-1); + } + +void sysFork() { + pid_t cPid = findTask(); + uLong *pageDirsrc,*pageTablesrc,*stackSrc,*pageDirdst,*pageTabledst,*stackDst; + int i,x,*ret; + uChar *src,*dst; + asm("":"=b" (ret)); + schedule(); + src = &_current->tss; + dst = &taskList[cPid].tss; + for (i=0;itss.cr3; + pageDirdst = (uLong *)allocPage(); + taskList[cPid].tss.cr3 = pageDirdst; + for (i=0;i<1024;i++) { + if (pageDirsrc[i] > 0) { + pageTablesrc = (uLong *)(pageDirsrc[i] & 0xFFFFF000); + pageDirdst[i] = allocPage() | pageDefault; + pageTabledst = (uLong *)(pageDirdst[i] & 0xFFFFF000); + for (x=0;x<1024;x++) { + if (pageTablesrc[x] > 0) { + pageTabledst[x] = pageTablesrc[x]; //(pageTablesrc[x] & 0xFFFFFFBD);//(pageTablesrc[x] & 0xFFFFF000) | (pagePresent|pageUser);// & 0xFFFFFFD; + if ((pageTablesrc[x] & 0xFFF) == 103) { + kprintf("[%i]",pageTablesrc[x]); + pageTabledst[x] = pageTablesrc[x]; + } + } + else { + pageTabledst[x] = 0x0; + } + } + } + else { + pageDirdst[i] = 0x0; + } + } + i = (_current->tss.esp & 0xFFFFF000)/(1024*4096); + pageTabledst = (uLong *) (pageDirdst[i] & 0xFFFFF000); + i = ((_current->tss.esp & 0xFFFFF000) - (((_current->tss.esp & 0xFFFFF000)/(1024*4096))*(1024*4096)))/4096; + pageTabledst[i] = allocPage() | pageDefault; + stackDst = (uLong *) (pageTabledst[i] & 0xFFFFF000); + stackSrc = (uLong *) (_current->tss.esp & 0xFFFFF000); + for (i=0;i<1024;i++) { + stackDst[i] = stackSrc[i]; + } + kprintf("Stack: [%i]\n",(_current->tss.esp & 0xFFFFF000)+0x1000); + kprintf("Changing Page Dirs"); + schedule(); + taskList[cPid].tss.eip = _current->tss.eip; + taskList[cPid].status = ACTIVE; +// taskList[cPid].tss.eip = &&retHere; +// retHere: + kprintf("[%i][%i][%i]\n",ret,cPid,_current->id); + if (_current->id == cPid) { + ret[0] = 0; + //asm("movl $0,%ebx"); + } + else { + ret[0] = cPid; + //asm("movl %0,%%ebx"::"g" (cPid)); + } + } + +/* +asm( + ".global sysFork \n" + "sysFork: \n" + " pushl %ss \n" + " pushl %esp \n" + " pushf \n" + " pushl %cs \n" + " pushl %ds \n" + " pushl %es \n" + " pushl %fs \n" + " pushl %edx \n" + " pushl %ecx \n" + " pushl %ebx \n" + " pushl $3 \n" + " pushl %gs \n" + " pushl %esi \n" + " pushl %edi \n" + " pushl %ebp \n" + " pushl $1 \n" + " call testFork \n" + ); +*/ + + diff --git a/src/sys/kernel/idlethread.c b/src/sys/kernel/idlethread.c index a6ea9b3..1675ebe 100755 --- a/src/sys/kernel/idlethread.c +++ b/src/sys/kernel/idlethread.c @@ -27,15 +27,22 @@ #include void idleThread() { - /* This thread is for maintinance */ - kprintf("[%i]\n",(pageDirectory[0] & 0xFFFFF000)); - if (!fork()) { + pid_t pid; + /*asm( + "mov $4,%%eax\n" + "int %0\n" + : + : "i" (0x80) + ); + asm("": "=b" (pid)); */ + while (1); +/* if (_sysFork()) { while (1) { kprintf("A"); } } while (1) { - kprintf("B"); + kprintf("B"); */ //checkTasks(); /* Looks for run away proccesses and kills thems */ - } +// } } diff --git a/src/sys/kernel/syscall.c b/src/sys/kernel/syscall.c index ab62564..62ab638 100755 --- a/src/sys/kernel/syscall.c +++ b/src/sys/kernel/syscall.c @@ -30,12 +30,12 @@ #include asm( - ".globl _sysCall \n" - "_sysCall: \n" - " cmpl totalCalls,%eax \n" - " jae invalidCall \n" + ".globl _sysCall \n" + "_sysCall: \n" + " cmpl totalCalls,%eax \n" + " jae invalidCall \n" " call *systemCalls(,%eax,4) \n" - " iret \n" /* Exit interrupt */ + " iret \n" /* Exit interrupt */ ); void invalidCall() { @@ -64,10 +64,7 @@ void sysGetpid() { char *pid; - asm ( - "" - : "=b" (pid) - ); + asm ("": "=b" (pid)); pid[0] = _current->id; } diff --git a/ubixos.kdevprj b/ubixos.kdevprj index 8bc0fd4..3753005 100755 --- a/ubixos.kdevprj +++ b/ubixos.kdevprj @@ -289,7 +289,7 @@ type=DATA [src/lib/libc/sys/Makefile.am] -files=src/lib/libc/sys/getpid.c,src/lib/libc/sys/Makefile,src/lib/libc/sys/exec.c +files=src/lib/libc/sys/getpid.c,src/lib/libc/sys/Makefile,src/lib/libc/sys/exec.c,src/lib/libc/sys/fork.c sharedlib_LDFLAGS=-version-info 0:0:1 sharedlib_rootname=sys sub_dirs= @@ -301,6 +301,12 @@ install_location= type=SOURCE +[src/lib/libc/sys/fork.c] +dist=true +install=false +install_location= +type=SOURCE + [src/lib/libc/sys/getpid.c] dist=true install=false