diff --git a/src/bin/ls/main.c b/src/bin/ls/main.c index 9bd7ab0..03be170 100755 --- a/src/bin/ls/main.c +++ b/src/bin/ls/main.c @@ -24,6 +24,11 @@ #include int main() { + long *test = malloc(4096); + int i=0; printf("pwd init shell ls\n"); + for (i=0;i<1024;i++) { + test[i] = 0x0; + } exit(1); } diff --git a/src/lib/libc/stdlib/Makefile b/src/lib/libc/stdlib/Makefile index dd4eb84..0edcb02 100755 --- a/src/lib/libc/stdlib/Makefile +++ b/src/lib/libc/stdlib/Makefile @@ -14,7 +14,7 @@ REMOVE = rm -f #Objects -OBJS = exit.o +OBJS = exit.o malloc.o #Output OUTPUT = libc.so diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c new file mode 100755 index 0000000..8754aa3 --- /dev/null +++ b/src/lib/libc/stdlib/malloc.c @@ -0,0 +1,129 @@ +/************************************************************************************** + 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 + +struct memDescriptor { + void *page; + void *base; + uShort limit; + uShort counter; + struct memDescriptor *next; + }; + +struct memDescriptor *kernDesc = 0x0; + +void initMalloc() { + int i = 0; + struct memDescriptor *tmpDesc = 0x0; + kernDesc = (void *)getPage(); + tmpDesc = kernDesc; + for (i=0;i<4096;i+=sizeof(struct memDescriptor)) { + tmpDesc->page = 0x0; + tmpDesc->base = 0x0; + tmpDesc->limit = 0x0; + tmpDesc->counter = 0x0; + if ((i+sizeof(struct memDescriptor)) < 4096) { + tmpDesc->next = (struct memDescriptor *)((uLong)tmpDesc+sizeof(struct memDescriptor)); + } + else { + tmpDesc->next = 0x0; + } + tmpDesc = tmpDesc->next; + } + } + +void *findFreeDesc() { + int i = 0; + struct memDescriptor *findDesc = 0x0,*tmpDesc = 0x0; + for (findDesc=(struct memDescriptor *)kernDesc;(findDesc != 0x0);findDesc=(struct memDescriptor *)findDesc->next) { + if (findDesc->base == 0) { + return(findDesc); + } + if (findDesc->next == 0) { break; } + } + tmpDesc = (void *)getPage(); + findDesc->next = tmpDesc; + for (i=0;i<4096;i+=sizeof(struct memDescriptor)) { + tmpDesc->page = 0x0; + tmpDesc->base = 0x0; + tmpDesc->limit = 0x0; + tmpDesc->counter = 0x0; + if ((i+sizeof(struct memDescriptor)) < 4096) { + tmpDesc->next = (struct memDescriptor *)((uLong)tmpDesc+sizeof(struct memDescriptor)); + } + else { + tmpDesc->next = 0x0; + } + tmpDesc = tmpDesc->next; + } + return(findFreeDesc()); + return(0); + } + +void *malloc(uInt len) { + void *ret = 0x0; + struct memDescriptor *tmpDesc = 0x0,*newDesc = 0x0; + if (!kernDesc) { + initMalloc(); + } + for(tmpDesc=kernDesc;tmpDesc;tmpDesc=tmpDesc->next) { + if (tmpDesc->counter == 0) { + if (tmpDesc->limit >= len) { + if (tmpDesc->limit > len+4) { + newDesc = findFreeDesc(); + newDesc->base = tmpDesc->base+tmpDesc->limit + 1; + newDesc->limit = tmpDesc->limit - len; + } + ret = tmpDesc->base; + break; + } + if (tmpDesc->page == 0x0) { + tmpDesc->page = (void *)getPage(); + tmpDesc->base = tmpDesc->page; + tmpDesc->limit = len; + tmpDesc->counter = 1; + newDesc = findFreeDesc(); + newDesc->page = tmpDesc->page; + newDesc->base = tmpDesc->base + tmpDesc->limit + 1; + newDesc->limit = 4096 - tmpDesc->limit; + ret = tmpDesc->base; + break; + } + } + } + return(ret); + } + + +void free(void *baseAddr) { + struct memDescriptor *tmpDesc = 0x0; + for (tmpDesc=kernDesc;tmpDesc;tmpDesc=tmpDesc->next) { + if (tmpDesc->base == baseAddr) { + tmpDesc->counter = 0; + return; + } + } + printf("Error Freeing!!!\n"); + return; + } diff --git a/src/lib/libc/sys/Makefile b/src/lib/libc/sys/Makefile index 9225575..f4e66b3 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 fork.o pidstatus.o +OBJS = exec.o getpid.o fork.o pidstatus.o getpage.o #Output OUTPUT = sys.so diff --git a/src/lib/libc/sys/getpage.c b/src/lib/libc/sys/getpage.c new file mode 100755 index 0000000..57c88d2 --- /dev/null +++ b/src/lib/libc/sys/getpage.c @@ -0,0 +1,31 @@ +/************************************************************************************** + 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$ + +**************************************************************************************/ + +void *getPage() { + long pageAddr = 0x0; + asm( + "int %0\n" + : : "i" (0x80),"a" (7),"b" (&pageAddr) + ); + return(pageAddr); + } diff --git a/src/sys/include/ubixos/syscalls.h b/src/sys/include/ubixos/syscalls.h index cb88338..096181d 100755 --- a/src/sys/include/ubixos/syscalls.h +++ b/src/sys/include/ubixos/syscalls.h @@ -31,11 +31,13 @@ void sysFork(); void sysFgetc(); void sysCheckPid(); +void sysGetFreePage(); typedef void (*functionPTR)(); functionPTR systemCalls[] = { - sysFwrite,sysGetpid,sysExit,sysExec,sysFork,sysFgetc,sysCheckPid + sysFwrite,sysGetpid,sysExit,sysExec,sysFork,sysFgetc,sysCheckPid, + sysGetFreePage, }; int totalCalls = sizeof(systemCalls)/sizeof(functionPTR); diff --git a/src/sys/include/vmm/paging.h b/src/sys/include/vmm/paging.h index 078cc41..c53ec87 100755 --- a/src/sys/include/vmm/paging.h +++ b/src/sys/include/vmm/paging.h @@ -49,6 +49,7 @@ void freePage(uLong pageAddr); void unmapPage(uLong pageAddr,int flags); void *getFreePage(pid_t pid); +void *getFreeVirtualPage(pid_t pid); void *getPhysicalAddr(uLong pageAddr); void setPageAttribute(uLong pageAddr,int attributes); void clearVirtualPage(uLong pageAddr); diff --git a/src/sys/kernel/exec.c b/src/sys/kernel/exec.c index 7a01cd1..3bee998 100755 --- a/src/sys/kernel/exec.c +++ b/src/sys/kernel/exec.c @@ -88,6 +88,7 @@ */ for (x=0;x<=((programHeader[i].phMemsz & 0xFFFFF000)+4095);x+=4096) { remapPage(findFreePage(_current->id),((programHeader[i].phVaddr & 0xFFFFF000) + (0x1000 * x))); + _current->vmStart = ((programHeader[i].phVaddr & 0xFFFFF000) + (0x1000 * x) + 0x1000); } //Now Copy The Binary To Its Correct Location for (x=0;xid),((programHeader[i].phVaddr & 0xFFFFF000) + (0x1000 * x))); + _current->vmStart = ((programHeader[i].phVaddr & 0xFFFFF000) + (0x1000 * x) + 0x1000); } //Now Copy The Binary To Its Correct Location for (x=0;xid); + return; + } diff --git a/src/sys/vmm/paging.c b/src/sys/vmm/paging.c index ac60908..458a43f 100755 --- a/src/sys/vmm/paging.c +++ b/src/sys/vmm/paging.c @@ -514,6 +514,38 @@ /************************************************************************ +Function: void *getFreeVirtualPage(pid_t pid); +Description: Returns A Free Page Mapped To The VM Space +Notes: + +08/11/02 - This Will Return Next Avilable Free Page Of Tasks VM Space + +************************************************************************/ +void *getFreeVirtualPage(pid_t pid) { + int x=0,y=0; + uLong *pageTableSrc = 0x0; + //Lets Search For A Free Page + for (x=(_current->vmStart/(1024*4096));x<1024;x++) { + //Set Page Table Address + pageTableSrc = (uLong *)(tablesBaseAddress + (4096*x)); + for (y=0;y<1024;y++) { + //Loop Through The Page Table Find An UnAllocated Page + if ((uLong)pageTableSrc[y] == (uLong)0x0) { + //Map A Physical Page To The Virtual Page + remapPage(findFreePage(pid),((x*(1024*4096))+(y*4096))); + //Clear This Page So No Garbage Is There + clearVirtualPage((uLong)((x*(1024*4096))+(y*4096))); + //Return The Address Of The Newly Allocate Page + return((void *)((x*(1024*4096))+(y*4096))); + } + } + } + //If No Free Page Was Found Return NULL + return(0x0); + } + +/************************************************************************ + Function: void remapPage(Physical Source,Virtual Destination) Description: This Function Will Remap A Physical Page Into Virtual Space Notes: @@ -591,7 +623,10 @@ pageTableIndex = ((memAddr-(pageDirectoryIndex*(1024*4096)))/4096); if (pageDir[pageDirectoryIndex] == 0) { //Creat A Routine For Non Mapped Memory - kprintf("Error: [%i]\n",memAddr); + kprintf("Segfault At Address: [%i]\n",memAddr); + freeProcessPages(_current->id); + _current->status = EMPTY; + schedule(); while (1); } else { @@ -615,7 +650,10 @@ } else { //Need To Create A Routine For Attempting To Access Non Mapped Memory - kprintf("Error: [%i]\n",memAddr); + kprintf("Segfault At Address: [%i]\n",memAddr); + freeProcessPages(_current->id); + _current->status = EMPTY; + schedule(); while(1); } } diff --git a/ubixos.kdevprj b/ubixos.kdevprj index f9b275a..805bc11 100755 --- a/ubixos.kdevprj +++ b/ubixos.kdevprj @@ -365,7 +365,7 @@ type=DATA [src/lib/libc/stdlib/Makefile.am] -files=src/lib/libc/stdlib/exit.c,src/lib/libc/stdlib/Makefile +files=src/lib/libc/stdlib/exit.c,src/lib/libc/stdlib/Makefile,src/lib/libc/stdlib/malloc.c sharedlib_LDFLAGS=-version-info 0:0:1 sharedlib_rootname=stdlib sub_dirs= @@ -377,6 +377,12 @@ install_location= type=SOURCE +[src/lib/libc/stdlib/malloc.c] +dist=true +install=false +install_location= +type=SOURCE + [src/lib/libc/sys/Makefile] dist=true install=false @@ -384,7 +390,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,src/lib/libc/sys/fork.c,src/lib/libc/sys/pidstatus.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,src/lib/libc/sys/pidstatus.c,src/lib/libc/sys/getpage.c sharedlib_LDFLAGS=-version-info 0:0:1 sharedlib_rootname=sys sub_dirs= @@ -402,6 +408,12 @@ install_location= type=SOURCE +[src/lib/libc/sys/getpage.c] +dist=true +install=false +install_location= +type=SOURCE + [src/lib/libc/sys/getpid.c] dist=true install=false