diff --git a/src/sys/Makefile b/src/sys/Makefile index f997254..c7f5437 100644 --- a/src/sys/Makefile +++ b/src/sys/Makefile @@ -2,7 +2,13 @@ # # $Id$ -all: sys-code init-code kernel-img +all: vmm-code lib-code sys-code init-code kernel-img + +vmm-code: vmm + (cd vmm;make) + +lib-code: lib + (cd lib;make) sys-code: sys (cd sys;make) @@ -21,6 +27,8 @@ (cd ../tools/;make format-dsk) clean: + (cd vmm;make clean) + (cd lib;make clean) (cd sys;make clean) (cd init;make clean) (cd compile;make clean) diff --git a/src/sys/compile/Makefile b/src/sys/compile/Makefile index 0329fd4..9deb702 100644 --- a/src/sys/compile/Makefile +++ b/src/sys/compile/Makefile @@ -10,7 +10,7 @@ OBJS = null.o #Kernel Parts -KPARTS = ../sys/*.o ../init/*.o +KPARTS = ../vmm/*.o ../lib/*.o ../sys/*.o ../init/*.o # Link the kernel statically with fixed text+data address @1M $(KERNEL) : $(OBJS) diff --git a/src/sys/include/lib/kprintf.h b/src/sys/include/lib/kprintf.h new file mode 100644 index 0000000..facb9d9 --- /dev/null +++ b/src/sys/include/lib/kprintf.h @@ -0,0 +1,42 @@ +/***************************************************************************************** + Copyright (c) 2002-2004 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$ + +*****************************************************************************************/ + +#ifndef _KPRINTF_H +#define _KPRINTF_H + +#include + +int kprintf(const char *fmt, ...); + +#endif + +/*** + END + ***/ + diff --git a/src/sys/include/lib/string.h b/src/sys/include/lib/string.h new file mode 100644 index 0000000..9f8c51a --- /dev/null +++ b/src/sys/include/lib/string.h @@ -0,0 +1,54 @@ +/***************************************************************************************** + Copyright (c) 2002-2004 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$ + +*****************************************************************************************/ + +#ifndef _STRING_H +#define _STRING_H + +#include + +char * strcpy(char *, const char *); +int strcmp(const char *str1,const char *str2); +int strncmp(const char * a, const char * b, size_t c); +void *memcpy(const void *dst, const void * src, size_t length); +void *memset(void * dst, int c, size_t length); +int strlen(const char * string); +int memcmp(const void * dst, const void * src, size_t length); +void strncpy(char * dest, const char * src, size_t size); +char *strtok(char *str, const char *sep); +char *strtok_r(char *str, const char *sep, char **last); +char *strstr(const char *s,char *find); + +int sprintf(char *buf,const char *fmt, ...); + +#endif + +/*** + END + ***/ + diff --git a/src/sys/include/stdarg.h b/src/sys/include/stdarg.h new file mode 100644 index 0000000..abbd540 --- /dev/null +++ b/src/sys/include/stdarg.h @@ -0,0 +1,53 @@ +/***************************************************************************************** + Copyright (c) 2002-2004 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$ + +*****************************************************************************************/ + +#ifndef _STDARG_H +#define _STDARG_H + +typedef char *vaList[1]; + +#define vaStart(ap, parm) ((ap)[0] = (char *) &parm \ + + ((sizeof(parm) + sizeof(int) - 1) & ~(sizeof(int) - 1)), (void) 0) + +#define vaArg(ap, type) ((ap)[0] += \ + ((sizeof(type) + sizeof(int) - 1) & ~(sizeof(int) - 1)), \ + (*(type *) ((ap)[0] \ + - ((sizeof(type) + sizeof(int) - 1) & ~(sizeof(int) - 1)) ))) + +#define vaEnd(ap) ((ap)[0] = 0, (void) 0) + + +int vsprintf(char *buf, const char *fmt, vaList args); + +#endif + +/*** + END + ***/ + diff --git a/src/sys/include/sys/display.h b/src/sys/include/sys/display.h index 544cd44..eb1f282 100644 --- a/src/sys/include/sys/display.h +++ b/src/sys/include/sys/display.h @@ -32,6 +32,7 @@ void kprint(char *); +extern int printColor; #endif /*** diff --git a/src/sys/include/sys/spinlock.h b/src/sys/include/sys/spinlock.h new file mode 100644 index 0000000..2e1b863 --- /dev/null +++ b/src/sys/include/sys/spinlock.h @@ -0,0 +1,53 @@ +/***************************************************************************************** + Copyright (c) 2002-2004 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$ + +*****************************************************************************************/ + +#ifndef _SPINLOCK_H +#define _SPINLOCK_H + +#include + +#define SPIN_LOCK_INITIALIZER 0 + +typedef volatile int spinLock_t; + +void spinLockInit(spinLock_t *); +void spinUnlock(spinLock_t *); +int spinTryLock(spinLock_t *); +void spinLock(spinLock_t *); + +void spinLock_scheduler(spinLock_t *); /* Only use this spinlock in the sched. */ + +int spinLockLocked(spinLock_t *); + +#endif + +/*** + END + ***/ + diff --git a/src/sys/include/vmm/vmm.h b/src/sys/include/vmm/vmm.h new file mode 100644 index 0000000..cc58f87 --- /dev/null +++ b/src/sys/include/vmm/vmm.h @@ -0,0 +1,68 @@ +/***************************************************************************************** + Copyright (c) 2002-2004 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$ + +*****************************************************************************************/ + +#ifndef _VMM_H +#define _VMM_H + +#include + +#define VMM_PAGECOW 0x00000200 +#define VMM_MEMAVAIL 1 +#define VMM_MEMNOTAVAIL 2 +#define VMM_ID -3 +#define VMM_PARENTPAGEDIRADDR 0x00100000 +#define VMM_TABLESBASEADDR 0xBFC00000 +#define VMM_PAGELENGTH 0x00000400 +#define VMM_PAGESIZE 4096 +#define VMM_PAGEENTRIES (VMM_PAGESIZE/4) + + + + +typedef struct { + uInt32 pageAddr; + uInt16 status; + uInt16 reserved; + pid_t pid; + int cowCounter; + } mMap; + +void vmm_freeProcessPages(pidType); +int vmm_adjustCowCounter(uInt32,int); +int vmm_freePage(uInt32); +uInt32 vmm_findFreePage(pidType); +int vmm_buildMemoryMap(); +int vmm_init(); + +#endif + +/*** + END + ***/ + diff --git a/src/sys/init/main.c b/src/sys/init/main.c index 2f7937d..037d925 100644 --- a/src/sys/init/main.c +++ b/src/sys/init/main.c @@ -29,7 +29,7 @@ #include #include -#include +#include /***************************************************************************************** Desc: The Kernels Descriptor table: @@ -77,8 +77,17 @@ } loadGDT = { (9 * sizeof(union descriptorTableUnion) - 1), ubixGDT }; int main() { - kprint("I LIKE CHEESE!\n"); + int i = 0x0; + + while (1) { + for (i = 0x0;i <= 0xFF;i++) { + kprintf("Ubix: [0x%X]",i); + } + } + while (1) asm("hlt"); + + return(0x0); } /*** diff --git a/src/sys/lib/Makefile b/src/sys/lib/Makefile new file mode 100644 index 0000000..a157412 --- /dev/null +++ b/src/sys/lib/Makefile @@ -0,0 +1,28 @@ +# (C) 2002-2004 The UbixOS Project +# +# $Id$ + +# Include Global 'Source' Options +include ../../Makefile.inc +include ../Makefile.inc + +# Objects +OBJS = string.o vsprintf.o kprintf.o + +all: $(OBJS) + +# Compile Types +.cc.o: + $(CXX) $(CFLAGS) $(INCLUDES) -c -o $@ $< +.cc.s: + $(CXX) $(CFLAGS) $(INCLUDES) -S -o $@ $< +.c.o: + $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $< +.c.s: + $(CC) $(CFLAGS) $(INCLUDES) -S -o $@ $< +.S.o: + $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $< + +# Clean up the junk +clean: + $(REMOVE) $(OBJS) diff --git a/src/sys/lib/kprintf.c b/src/sys/lib/kprintf.c new file mode 100644 index 0000000..1d26233 --- /dev/null +++ b/src/sys/lib/kprintf.c @@ -0,0 +1,57 @@ +/***************************************************************************************** + 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 +#include +#include + +int kprintf(const char *fmt, ...) { + char buf[1024]; + vaList args; + int i = 0x0; + vaStart(args, fmt); + i=vsprintf(buf,fmt,args); + vaEnd(args); + kprint(buf); + return(i); + } + +int sprintf(char *buf,const char *fmt, ...) { + vaList args; + int i; + vaStart(args, fmt); + i=vsprintf(buf,fmt,args); + vaEnd(args); + return(i); + } + +/*** + END + ***/ + diff --git a/src/sys/lib/string.c b/src/sys/lib/string.c new file mode 100644 index 0000000..bc8a255 --- /dev/null +++ b/src/sys/lib/string.c @@ -0,0 +1,183 @@ +/***************************************************************************************** + Copyright (c) 2002-2004 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 + +char * +strcpy(char * dst, const char * src) { + char * tmp = dst; + do { + *dst = *src; + dst++; + } while (*src++ != '\0'); + return tmp; +} /* strcpy */ + +int strcmp(const char *str1,const char *str2) { + while ((*str1 == *str2) && (*str1 != 0x0) && (*str2 != 0x0)) { + str1++; + str2++; + } + if (*str1 == *str2) { + return(0); + } + else if (*str1 > *str2) { + return(1); + } + else { + return(-1); + } + } + +int strncmp(const char * a, const char * b, size_t c) { + int i = 0; + while (i < c) { + if ((a[i] != b[i]) || (a[i] == '\0') || (b[i] == '\0')) + return a[i] - b[i]; + i++; + } + return 0; + } + + + +void *memcpy(const void *dst, const void * src, size_t length) { + size_t x = length >> 2; + size_t y = length & 0xf; + size_t i; + + for (i = 0; i < x; i++) { + ((unsigned long *)dst)[i] = ((unsigned long *)src)[i]; + } + + for (i = 0; i < y; i++) { + ((char *) dst)[length-y+i] = ((char *) src)[length-y+i]; + } + + return((void *)dst); + } + + +int strlen(const char * string) { + int i = 0; + + while (1) { + if (string[i] == '\0') + return i; + i++; + } + return 0; + } + +int memcmp(const void * dst, const void * src, size_t length) +{ + size_t x = length >> 2; + size_t y = length & 0xf; + size_t i; + + for (i = 0; i < x; i++) + { + if (((unsigned long *)dst)[i] > ((unsigned long *)src)[i]) + return 1; + if (((unsigned long *)dst)[i] < ((unsigned long *)src)[i]) + return -1; + } + + for (i = 0; i < y; i++) + { + if (((char *) dst)[length-y+i] > ((char *) src)[length-y+i]) + return 1; + if (((char *) dst)[length-y+i] < ((char *) src)[length-y+i]) + return -1; + } + + return 0; +} + +void strncpy(char * dest, const char * src, size_t size) +{ + if (size == 0) + return; + do + { + *dest = *src; + dest++; src++; + size--; + } + while(('\0' != *(src-1)) && (size)); +} + +char *strstr(const char *s,char *find) { + char c, sc; + size_t len; + + if ((c = *find++) != 0) { + len = strlen(find); + do { + do { + if ((sc = *s++) == 0) + return (NULL); + } while (sc != c); + } while (strncmp(s, find, len) != 0); + s--; + } + return ((char *)s); + } + + +/*** + $Log$ + Revision 1.6 2004/07/28 15:05:43 reddawg + Major: + Pages now have strict security enforcement. + Many null dereferences have been resolved. + When apps loaded permissions set for pages rw and ro + + Revision 1.5 2004/07/20 18:42:41 flameshadow + add: strcpy() + chg: modified dirCache.c to use strcpy() + + Revision 1.4 2004/07/05 23:06:32 reddawg + Fixens + + Revision 1.3 2004/06/28 23:12:58 reddawg + file format now container:/path/to/file + + Revision 1.2 2004/05/19 14:40:58 reddawg + Cleaned up some warning from leaving out typedefs + + Revision 1.1.1.1 2004/04/15 12:07:11 reddawg + UbixOS v1.0 + + Revision 1.5 2004/04/13 16:36:33 reddawg + Changed our copyright, it is all now under a BSD-Style license + + END + ***/ + diff --git a/src/sys/lib/vsprintf.c b/src/sys/lib/vsprintf.c new file mode 100644 index 0000000..92e030a --- /dev/null +++ b/src/sys/lib/vsprintf.c @@ -0,0 +1,264 @@ +/***************************************************************************************** + 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$ + +*****************************************************************************************/ + +/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */ +/* + * Wirzenius wrote this portably, Torvalds fucked it up :-) + */ + +#include +#include + +/* we use this so that we can do without the ctype library */ +#define is_digit(c) ((c) >= '0' && (c) <= '9') + +static int skip_atoi(const char **s) +{ + int i=0; + + while (is_digit(**s)) + i = i*10 + *((*s)++) - '0'; + return i; +} + +#define ZEROPAD 1 /* pad with zero */ +#define SIGN 2 /* unsigned/signed long */ +#define PLUS 4 /* show plus */ +#define SPACE 8 /* space if plus */ +#define LEFT 16 /* left justified */ +#define SPECIAL 32 /* 0x */ +#define SMALL 64 /* use 'abcdef' instead of 'ABCDEF' */ + +#define do_div(n,base) ({ \ +int __res; \ +__asm__("divl %4":"=a" (n),"=d" (__res):"0" (n),"1" (0),"r" (base)); \ +__res; }) + +static char * number(char * str, int num, int base, int size, int precision + ,int type) +{ + char c,sign,tmp[36]; + const char *digits="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + int i; + + if (type&SMALL) digits="0123456789abcdefghijklmnopqrstuvwxyz"; + if (type&LEFT) type &= ~ZEROPAD; + if (base<2 || base>36) + return 0; + c = (type & ZEROPAD) ? '0' : ' ' ; + if (type&SIGN && num<0) { + sign='-'; + num = -num; + } else + sign=(type&PLUS) ? '+' : ((type&SPACE) ? ' ' : 0); + if (sign) size--; + if (type&SPECIAL) { + if (base==16) { size -= 2; } + else if (base==8) { size--; } + } + i=0; + if (num==0) + tmp[i++]='0'; + else while (num!=0) + tmp[i++]=digits[do_div(num,base)]; + if (i>precision) precision=i; + size -= precision; + if (!(type&(ZEROPAD+LEFT))) + while(size-->0) + *str++ = ' '; + if (sign) + *str++ = sign; + if (type&SPECIAL) { + if (base==8) { + *str++ = '0'; + } + else if (base==16) { + *str++ = '0'; + *str++ = digits[33]; + } + } + if (!(type&LEFT)) + while(size-->0) + *str++ = c; + while(i0) + *str++ = tmp[i]; + while(size-->0) + *str++ = ' '; + return str; +} + +int vsprintf(char *buf, const char *fmt, vaList args) +{ + int len; + int i; + char * str; + char *s; + int *ip; + + int flags; /* flags to number() */ + + int field_width; /* width of output field */ + int precision; /* min. # of digits for integers; max + number of chars for from string */ + int qualifier; /* 'h', 'l', or 'L' for integer fields */ + + for (str=buf ; *fmt ; ++fmt) { + if (*fmt != '%') { + *str++ = *fmt; + continue; + } + + /* process flags */ + flags = 0; + repeat: + ++fmt; /* this also skips first '%' */ + switch (*fmt) { + case '-': flags |= LEFT; goto repeat; + case '+': flags |= PLUS; goto repeat; + case ' ': flags |= SPACE; goto repeat; + case '#': flags |= SPECIAL; goto repeat; + case '0': flags |= ZEROPAD; goto repeat; + } + + /* get field width */ + field_width = -1; + if (is_digit(*fmt)) + field_width = skip_atoi(&fmt); + else if (*fmt == '*') { + /* it's the next argument */ + field_width = vaArg(args, int); + if (field_width < 0) { + field_width = -field_width; + flags |= LEFT; + } + } + + /* get the precision */ + precision = -1; + if (*fmt == '.') { + ++fmt; + if (is_digit(*fmt)) + precision = skip_atoi(&fmt); + else if (*fmt == '*') { + /* it's the next argument */ + precision = vaArg(args, int); + } + if (precision < 0) + precision = 0; + } + + /* get the conversion qualifier */ + qualifier = -1; + if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') { + qualifier = *fmt; + ++fmt; + } + + switch (*fmt) { + case 'c': + if (!(flags & LEFT)) + while (--field_width > 0) + *str++ = ' '; + *str++ = (unsigned char) vaArg(args, int); + while (--field_width > 0) + *str++ = ' '; + break; + + case 's': + s = vaArg(args, char *); + len = strlen(s); + if (precision < 0) + precision = len; + else if (len > precision) + len = precision; + + if (!(flags & LEFT)) + while (len < field_width--) + *str++ = ' '; + for (i = 0; i < len; ++i) + *str++ = *s++; + while (len < field_width--) + *str++ = ' '; + break; + + case 'o': + str = number(str, vaArg(args, unsigned long), 8, + field_width, precision, flags); + break; + + case 'p': + if (field_width == -1) { + field_width = 8; + flags |= ZEROPAD; + } + str = number(str, + (unsigned long) vaArg(args, void *), 16, + field_width, precision, flags); + break; + + case 'x': + flags |= SMALL; + case 'X': + str = number(str, vaArg(args, unsigned long), 16, + field_width, precision, flags); + break; + + case 'd': + case 'i': + flags |= SIGN; + case 'u': + str = number(str, vaArg(args, unsigned long), 10, + field_width, precision, flags); + break; + + case 'n': + ip = vaArg(args, int *); + *ip = (str - buf); + break; + + default: + if (*fmt != '%') + *str++ = '%'; + if (*fmt) + *str++ = *fmt; + else + --fmt; + break; + } + } + *str = '\0'; + return str-buf; +} + +/*** + END + ***/ + diff --git a/src/sys/sys/Makefile b/src/sys/sys/Makefile index 56defc5..5bf18d0 100644 --- a/src/sys/sys/Makefile +++ b/src/sys/sys/Makefile @@ -7,7 +7,7 @@ include ../Makefile.inc # Objects -OBJS = display.o io.o +OBJS = spinlock.o display.o io.o all: $(OBJS) diff --git a/src/sys/sys/spinlock.c b/src/sys/sys/spinlock.c new file mode 100644 index 0000000..7770c0d --- /dev/null +++ b/src/sys/sys/spinlock.c @@ -0,0 +1,77 @@ +/***************************************************************************************** + Copyright (c) 2002-2004 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 + +void spinLockInit(spinLock_t *lock) { + *lock = SPIN_LOCK_INITIALIZER; + } + +void spinUnlock(spinLock_t *lock) { + *lock = 0x0; + /* + register int unlocked; + asm volatile( + "xchgl %0, %1" + : "=&r" (unlocked), "=m" (*lock) : "0" (0) + ); + */ + } + +int spinTryLock(spinLock_t *lock) { + register int locked; + asm volatile("xchgl %0, %1" + : "=&r" (locked), "=m" (*lock) : "0" (1) + ); + return(!locked); + } + +void spinLock(spinLock_t *lock) { + while (!spinTryLock(lock)) + { + while (*lock == 1); + // sched_yield(); + } +} + +void spinLock_scheduler(spinLock_t *lock) { + while (!spinTryLock(lock)) + while (*lock == 1); + } + + +int spinLockLocked(spinLock_t *lock) { + return(*lock != 0); + } + + +/*** + END + ***/ + diff --git a/src/sys/vmm/Makefile b/src/sys/vmm/Makefile new file mode 100644 index 0000000..e7e7945 --- /dev/null +++ b/src/sys/vmm/Makefile @@ -0,0 +1,28 @@ +# (C) 2002-2004 The UbixOS Project +# +# $Id$ + +# Include Global 'Source' Options +include ../../Makefile.inc +include ../Makefile.inc + +# Objects +OBJS = memory.o + +all: $(OBJS) + +# Compile Types +.cc.o: + $(CXX) $(CFLAGS) $(INCLUDES) -c -o $@ $< +.cc.s: + $(CXX) $(CFLAGS) $(INCLUDES) -S -o $@ $< +.c.o: + $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $< +.c.s: + $(CC) $(CFLAGS) $(INCLUDES) -S -o $@ $< +.S.o: + $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $< + +# Clean up the junk +clean: + $(REMOVE) $(OBJS) diff --git a/src/sys/vmm/memory.c b/src/sys/vmm/memory.c new file mode 100644 index 0000000..b768fc8 --- /dev/null +++ b/src/sys/vmm/memory.c @@ -0,0 +1,344 @@ +/***************************************************************************************** + Copyright (c) 2002-2004 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$ + +*****************************************************************************************/ + + /* + This file containers all the physical memory abstractions + */ + +#include +#include +#include +#include + +static uInt32 freePages = 0x0; +static uInt32 numPages = 0x0; +mMap *vmm_memoryMap = (mMap *) 0x0; + + +static int vmm_countMemory(); + +static spinLock_t vmm_spinLock = SPIN_LOCK_INITIALIZER; +static spinLock_t vmm_cowSpinLock = SPIN_LOCK_INITIALIZER; + + +/************************************************************************ + + Function: void vmmMemMapInit(); + Description: This Function Initializes The Memory Map For the System + Notes: + + 02/20/2004 - Made It Report Real And Available Memory + +************************************************************************/ +int vmm_buildMemoryMap() { + int i = 0x0; + int memStart = 0x0; + + /* Count System Memory */ + numPages = vmm_countMemory(); + + /* Set Memory Map To Point To First Physical Page That We Will Use */ + vmm_memoryMap = (mMap *) 0x100000; + + /* Initialize Map Make All Pages Not Available */ + for (i = 0x0; i < numPages; i++) { + vmm_memoryMap[i].cowCounter = 0x0; + vmm_memoryMap[i].status = VMM_MEMNOTAVAIL; + vmm_memoryMap[i].pid = VMM_ID; + vmm_memoryMap[i].pageAddr = i * 4096; + } + + /* Calculate Start Of Free Memory */ + memStart = (0x100000 / 0x1000); + memStart += (((sizeof(mMap) * numPages) + (sizeof(mMap) - 1)) / 0x1000); + + for (i = memStart; i < numPages; i++) { + vmm_memoryMap[i].status = VMM_MEMAVAIL; + freePages++; + } + + /* Print Out Amount Of Memory */ + kprintf("Real Memory: %iKB\n", numPages * 4); + kprintf("Available Memory: %iKB\n", freePages * 4); + + /* Return */ + return (0x0); + } + +/************************************************************************ + + Function: int countMemory(); + Description: This Function Counts The Systems Physical Memory + Notes: + + 02/20/2004 - Inspect For Quality And Approved + +************************************************************************/ +static int vmm_countMemory() { + register uInt32 *mem = 0x0; + unsigned long memCount = -1, tempMemory = 0x0; + unsigned short memKb = 0; + unsigned char irq1State, irq2State; + unsigned long cr0 = 0x0; + + /* + * Save The States Of Both IRQ 1 And 2 So We Can Turn Them Off And Restore + * Them Later + */ + irq1State = inportByte(0x21); + irq2State = inportByte(0xA1); + + /* Turn Off IRQ 1 And 2 To Prevent Chances Of Faults While Examining Memory */ + outportByte(0x21, 0xFF); + outportByte(0xA1, 0xFF); + + /* Save The State Of Register CR0 */ + asm volatile ( + "movl %%cr0, %%ebx\n" + : "=a" (cr0) + : + : "ebx" + ); + + asm volatile ("wbinvd"); + asm volatile ( + "movl %%ebx, %%cr0\n" + : + : "a" (cr0 | 0x00000001 | 0x40000000 | 0x20000000) + : "ebx" + ); + + while (memKb < 4096 && memCount != 0) { + memKb++; + if (memCount == -1) + memCount = 0; + memCount += 1024 * 1024; + mem = (uInt32 *)memCount; + tempMemory = *mem; + *mem = 0x55AA55AA; + asm("": : :"memory"); + if (*mem != 0x55AA55AA) { + memCount = 0; + } + else { + *mem = 0xAA55AA55; + asm("": : :"memory"); + if (*mem != 0xAA55AA55) { + memCount = 0; + } + } + asm("": : :"memory"); + *mem = tempMemory; + } + + asm volatile ( + "movl %%ebx, %%cr0\n" + : + : "a" (cr0) + : "ebx" + ); + + /* Restore States For Both IRQ 1 And 2 */ + outportByte(0x21, irq1State); + outportByte(0xA1, irq2State); + + /* Return Amount Of Memory In Pages */ + return ((memKb * 1024 * 1024) / 4096); + } + +/************************************************************************ + + Function: uInt32 vmm_findFreePage(pid_t pid); + + Description: This Returns A Free Physical Page Address Then Marks It + Not Available As Well As Setting The PID To The Proccess + Allocating This Page + Notes: + +************************************************************************/ +uInt32 vmm_findFreePage(pidType pid) { + int i = 0x0; + + /* Lets Look For A Free Page */ + if (pid < 0x0) { + //kpanic("Error: invalid PID %i\n",pid); + kprintf("Error: invalid PID %i\n",pid); + } + + spinLock(&vmm_spinLock); + + for (i = 0; i <= numPages; i++) { + + /* + * If We Found A Free Page Set It To Not Available After That Set Its Own + * And Return The Address + */ + if ((vmm_memoryMap[i].status == VMM_MEMAVAIL) && (vmm_memoryMap[i].cowCounter == 0)) { + vmm_memoryMap[i].status = VMM_MEMNOTAVAIL; + vmm_memoryMap[i].pid = pid; + freePages--; + //if (systemVitals) + // systemVitals->freePages = freePages; + spinUnlock(&vmm_spinLock); + return (vmm_memoryMap[i].pageAddr); + } + } + + /* If No Free Memory Is Found Return NULL */ + //kpanic("Out Of Memory!!!!"); + kprintf("Out Of Memory\n"); + return (0x0); + } + + +/************************************************************************ + + Function: int vmm_freePage(uInt32 pageAddr); + + Description: This Function Marks The Page As Free + + Notes: + +************************************************************************/ +int vmm_freePage(uInt32 pageAddr) { + int pageIndex = 0x0; + + spinLock(&vmm_spinLock); + + /* Find The Page Index To The Memory Map */ + pageIndex = (pageAddr / 4096); + + /* Check If Page COW Is Greater Then 0 If It Is Dec It If Not Free It */ + if (vmm_memoryMap[pageIndex].cowCounter == 0) { + /* Set Page As Avail So It Can Be Used Again */ + vmm_memoryMap[pageIndex].status = VMM_MEMAVAIL; + vmm_memoryMap[pageIndex].cowCounter = 0x0; + vmm_memoryMap[pageIndex].pid = -2; + freePages++; + //systemVitals->freePages = freePages; + } + else { + /* Adjust The COW Counter */ + vmm_adjustCowCounter(((uInt32) vmm_memoryMap[pageIndex].pageAddr), -1); + } + + /* Return */ + spinUnlock(&vmm_spinLock); + return (0); + } + +/************************************************************************ + + Function: int adjustCowCounter(uInt32 baseAddr,int adjustment); + + Description: This Adjust The COW Counter For Page At baseAddr It Will + Error If The Count Goes Below 0 + + Notes: + + 08/01/02 - I Think If Counter Gets To 0 I Should Free The Page + +************************************************************************/ +int vmm_adjustCowCounter(uInt32 baseAddr, int adjustment) { + int vmm_memoryMapIndex = (baseAddr / 4096); + + spinLock(&vmm_cowSpinLock); + + /* Adjust COW Counter */ + vmm_memoryMap[vmm_memoryMapIndex].cowCounter += adjustment; + + if (vmm_memoryMap[vmm_memoryMapIndex].cowCounter == 0) { + vmm_memoryMap[vmm_memoryMapIndex].cowCounter = 0x0; + vmm_memoryMap[vmm_memoryMapIndex].pid = VMM_ID; + vmm_memoryMap[vmm_memoryMapIndex].status = VMM_MEMAVAIL; + freePages++; + //systemVitals->freePages = freePages; + } + + /* Return */ + spinUnlock(&vmm_cowSpinLock); + return (0x0); + } + +/************************************************************************ + + Function: void vmmFreeProcessPages(pid_t pid); + + Description: This Function Will Free Up Memory For The Exiting Process + + Notes: + + 08/04/02 - Added Checking For COW Pages First + +************************************************************************/ +void vmm_freeProcessPages(pidType pid) { + int i=0,x=0; + uInt32 *tmpPageTable = 0x0; + uInt32 *tmpPageDir = (uInt32 *)VMM_PARENTPAGEDIRADDR; + + spinLock(&vmm_spinLock); + + /* Check Page Directory For An Avail Page Table */ + for (i=0;i<=0x300;i++) { + if (tmpPageDir[i] != 0) { + /* Set Up Page Table Pointer */ + tmpPageTable = (uInt32 *)(VMM_TABLESBASEADDR + (i * 0x1000)); + /* Check The Page Table For COW Pages */ + for (x=0;xfreePages = freePages; + } + } + } + + /* Return */ + spinUnlock(&vmm_spinLock); + return; + } + +/*** + END + ***/ +