From 47e9dfd89189fc6b150bd6b20229cb047c7e0858 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Thu, 19 Nov 2009 17:24:00 +0800 Subject: [PATCH] Cleanup and Bugfixes - Fixed bugs with handling of SYS_READ and SYS_WRITE - Added EXPORT definitions to functions used by the IPStack - Updated the core Makefile to use loops to allow easier maintainence - Fixed bug in IPStack/main.c (Null pointer dereference) - Cleaned up some of the debug code --- Kernel/arch/x86/errors.c | 3 + Kernel/arch/x86/lib.c | 4 + Kernel/arch/x86/proc.c | 3 + Kernel/arch/x86/time.c | 6 + Kernel/bin/elf.c | 18 +- Kernel/binary.c | 3 + Kernel/debug.c | 3 +- Kernel/drv/vterm.c | 6 +- Kernel/heap.c | 5 + Kernel/lib.c | 4 + Kernel/modules.c | 2 - Kernel/syscalls.c | 40 +++-- Kernel/system.c | 8 +- Kernel/threads.c | 3 + Kernel/vfs/io.c | 9 + Kernel/vfs/open.c | 4 + Makefile | 86 ++++++---- Modules/IPStack/ipstack.h | 4 +- Modules/IPStack/link.c | 8 +- Modules/IPStack/main.c | 172 ++++++++++++++++++-- Modules/Makefile.tpl | 1 + Usermode/Applications/init_src/Makefile | 9 +- Usermode/Applications/init_src/main.c | 30 ++-- Usermode/Libraries/Makefile.cfg | 2 + Usermode/Libraries/libacess.so_src/Makefile | 2 +- Usermode/Libraries/libacess.so_src/mm.asm | 2 +- Usermode/Libraries/libacess.so_src/vfs.asm | 6 +- Usermode/Libraries/libc.so_src/Makefile | 20 ++- Usermode/Libraries/libc.so_src/heap.c | 27 ++- Usermode/Libraries/libc.so_src/stub.c | 12 +- Usermode/include/acess/sys.h | 4 +- 31 files changed, 377 insertions(+), 129 deletions(-) diff --git a/Kernel/arch/x86/errors.c b/Kernel/arch/x86/errors.c index 2f94972c..d6daace5 100644 --- a/Kernel/arch/x86/errors.c +++ b/Kernel/arch/x86/errors.c @@ -143,3 +143,6 @@ void StartupPrint(char *Str) memset(&buf[80*24], 0, 80*2); } } + +// === EXPORTS === +EXPORT(__stack_chk_fail); diff --git a/Kernel/arch/x86/lib.c b/Kernel/arch/x86/lib.c index 8b86145e..990d1dca 100644 --- a/Kernel/arch/x86/lib.c +++ b/Kernel/arch/x86/lib.c @@ -189,8 +189,12 @@ Uint32 BigEndian32(Uint32 Val) // --- EXPORTS --- EXPORT(memcpy); EXPORT(memset); +EXPORT(memcmp); //EXPORT(memcpyw); EXPORT(memsetw); EXPORT(memcpyd); EXPORT(memsetd); EXPORT(inb); EXPORT(inw); EXPORT(ind); EXPORT(outb); EXPORT(outw); EXPORT(outd); EXPORT(__udivdi3); EXPORT(__umoddi3); + +EXPORT(LittleEndian16); EXPORT(BigEndian16); +EXPORT(LittleEndian32); EXPORT(BigEndian32); diff --git a/Kernel/arch/x86/proc.c b/Kernel/arch/x86/proc.c index d7f460fa..f9c5ba1f 100644 --- a/Kernel/arch/x86/proc.c +++ b/Kernel/arch/x86/proc.c @@ -568,3 +568,6 @@ void Proc_Scheduler(int CPU) ); for(;;); // Shouldn't reach here } + +// === EXPORTS === +EXPORT(Proc_SpawnWorker); diff --git a/Kernel/arch/x86/time.c b/Kernel/arch/x86/time.c index cfe2f036..abdbb118 100644 --- a/Kernel/arch/x86/time.c +++ b/Kernel/arch/x86/time.c @@ -177,3 +177,9 @@ void Time_Delay(int Delay) Sint64 dest = giTimestamp + Delay; while(dest < giTimestamp) Threads_Yield(); } + +// === EXPORTS === +EXPORT(now); +EXPORT(Time_CreateTimer); +EXPORT(Time_RemoveTimer); +EXPORT(Time_Delay); diff --git a/Kernel/bin/elf.c b/Kernel/bin/elf.c index dcfe35c6..745c2c7a 100644 --- a/Kernel/bin/elf.c +++ b/Kernel/bin/elf.c @@ -277,6 +277,7 @@ int Elf_Relocate(void *Base) Elf32_Dyn *dynamicTab = NULL; // Dynamic Table Pointer char *dynstrtab = NULL; // .dynamic String Table Elf32_Sym *dynsymtab = NULL; + int bFailed = 0; ENTER("pBase", Base); @@ -393,8 +394,7 @@ int Elf_Relocate(void *Base) { ptr = (void*)(iBaseDiff + rel[i].r_offset); if( !Elf_Int_DoRelocate(rel[i].r_info, ptr, *ptr, dynsymtab, (Uint)Base) ) { - LEAVE('x', 0); - return 0; + bFailed = 1; } } } @@ -406,8 +406,7 @@ int Elf_Relocate(void *Base) { ptr = (void*)(iBaseDiff + rela[i].r_offset); if( !Elf_Int_DoRelocate(rel[i].r_info, ptr, rela[i].r_addend, dynsymtab, (Uint)Base) ) { - LEAVE('x', 0); - return 0; + bFailed = 1; } } } @@ -423,8 +422,7 @@ int Elf_Relocate(void *Base) { ptr = (void*)(iBaseDiff + pltRel[i].r_offset); if( !Elf_Int_DoRelocate(pltRel[i].r_info, ptr, *ptr, dynsymtab, (Uint)Base) ) { - LEAVE('x', 0); - return 0; + bFailed = 1; } } } @@ -436,13 +434,17 @@ int Elf_Relocate(void *Base) { ptr = (void*)((Uint)Base + pltRela[i].r_offset); if( !Elf_Int_DoRelocate(pltRela[i].r_info, ptr, pltRela[i].r_addend, dynsymtab, (Uint)Base) ) { - LEAVE('x', 0); - return 0; + bFailed = 1; } } } } + if(bFailed) { + LEAVE('i', 0); + return 0; + } + LEAVE('x', hdr->entrypoint); return hdr->entrypoint; } diff --git a/Kernel/binary.c b/Kernel/binary.c index 2237d1b1..43d8cb35 100644 --- a/Kernel/binary.c +++ b/Kernel/binary.c @@ -9,6 +9,7 @@ // === CONSTANTS === #define BIN_LOWEST MM_USER_MIN // 1MiB #define BIN_GRANUALITY 0x10000 // 64KiB +//! \todo Move 0xBC000000 to mm_virt.h #define BIN_HIGHEST (0xBC000000-BIN_GRANUALITY) // Just below the kernel #define KLIB_LOWEST MM_MODULE_MIN #define KLIB_GRANUALITY 0x8000 // 32KiB @@ -322,6 +323,8 @@ Uint Binary_MapIn(tBinary *binary) MM_SetFlags( addr, MM_PFLAG_COW, -1 ); } + //Log("Mapped '%s' to 0x%x", binary->TruePath, base); + //LOG("*0x%x = 0x%x\n", binary->Pages[0].Virtual, *(Uint*)binary->Pages[0].Virtual); return base; diff --git a/Kernel/debug.c b/Kernel/debug.c index 854876ad..a23c4c41 100644 --- a/Kernel/debug.c +++ b/Kernel/debug.c @@ -200,7 +200,7 @@ void Debug_SetKTerminal(char *File) if(giDebug_KTerm != -1) VFS_Close(giDebug_KTerm); giDebug_KTerm = VFS_Open(File, VFS_OPENFLAG_WRITE); - Log("Opend '%s' as %i\n", File, giDebug_KTerm); + Log("Opened '%s' as %i\n", File, giDebug_KTerm); } void Debug_Enter(char *FuncName, char *ArgTypes, ...) @@ -330,6 +330,7 @@ void Debug_HexDump(char *Header, void *Data, Uint Length) } // --- EXPORTS --- +EXPORT(Log); EXPORT(Warning); EXPORT(Debug_Enter); EXPORT(Debug_Log); diff --git a/Kernel/drv/vterm.c b/Kernel/drv/vterm.c index 87f71ede..96f02ed5 100644 --- a/Kernel/drv/vterm.c +++ b/Kernel/drv/vterm.c @@ -197,7 +197,6 @@ int VT_Install(char **Arguments) void VT_InitOutput() { giVT_OutputDevHandle = VFS_Open(gsVT_OutputDevice, VFS_OPENFLAG_WRITE); - Log("giVT_OutputDevHandle = %x\n", giVT_OutputDevHandle); VT_SetTerminal( 0 ); } @@ -208,7 +207,6 @@ void VT_InitOutput() void VT_InitInput() { giVT_InputDevHandle = VFS_Open(gsVT_InputDevice, VFS_OPENFLAG_READ); - LOG("giVT_InputDevHandle = %x\n", giVT_InputDevHandle); if(giVT_InputDevHandle == -1) return ; VFS_IOCtl(giVT_InputDevHandle, KB_IOCTL_SETCALLBACK, VT_KBCallBack); } @@ -495,9 +493,9 @@ void VT_KBCallBack(Uint32 Codepoint) case KEY_F11: VT_SetTerminal(10); return; case KEY_F12: VT_SetTerminal(11); return; case KEY_PGUP: - break; + return; case KEY_PGDOWN: - break; + return; } } diff --git a/Kernel/heap.c b/Kernel/heap.c index 0e3c5f46..a8fdd536 100644 --- a/Kernel/heap.c +++ b/Kernel/heap.c @@ -444,3 +444,8 @@ void Heap_Dump() } } #endif + +// === EXPORTS === +EXPORT(malloc); +EXPORT(realloc); +EXPORT(free); diff --git a/Kernel/lib.c b/Kernel/lib.c index 70d116e0..f6149652 100644 --- a/Kernel/lib.c +++ b/Kernel/lib.c @@ -421,11 +421,15 @@ int LookupString(char **Array, char *Needle) return -1; } +EXPORT(strlen); EXPORT(strdup); EXPORT(strcmp); +EXPORT(strncmp); EXPORT(strcpy); +//EXPORT(strncpy); EXPORT(timestamp); EXPORT(ReadUTF8); EXPORT(CheckMem); EXPORT(CheckString); +EXPORT(LookupString); diff --git a/Kernel/modules.c b/Kernel/modules.c index a4e639b8..9ae940a6 100644 --- a/Kernel/modules.c +++ b/Kernel/modules.c @@ -46,7 +46,6 @@ int Modules_LoadBuiltins() if(strcmp(deps[j], gKernelModules[k].Name) == 0) break; } - Log("%s requires %s\n", gKernelModules[i].Name, deps[j]); if(k == giNumBuiltinModules) { Warning("Unable to find dependency '%s' for '%s' in kernel", deps[j], gKernelModules[i].Name); @@ -78,7 +77,6 @@ int Modules_LoadBuiltins() } // `k` is assumed to be less than `giNumBuiltinModules` - Log("baIsLoaded[%i(%s)] = %i\n", k, deps[j], baIsLoaded[k]); // If a dependency failed, skip and mark as failed if( baIsLoaded[k] == -1 ) { baIsLoaded[i] = -1; diff --git a/Kernel/syscalls.c b/Kernel/syscalls.c index fb574ef2..87701873 100644 --- a/Kernel/syscalls.c +++ b/Kernel/syscalls.c @@ -9,18 +9,14 @@ #include #include -#define CHECK_NUM_NULLOK(v,size) do {\ - if((v)&&!Syscall_Valid((size),(Uint)(v))){ret=-1;err=-EINVAL;break;}\ - }while(0) -#define CHECK_STR_NULLOK(v) do {\ - if((v)&&!Syscall_ValidString((Uint)(v))){ret=-1;err=-EINVAL;break;}\ - }while(0) -#define CHECK_NUM_NONULL(v,size) do {\ - if(!(v)||!Syscall_Valid((size),(Uint)(v))){ret=-1;err=-EINVAL;break;}\ - }while(0) -#define CHECK_STR_NONULL(v) do {\ - if(!(v)||!Syscall_ValidString((Uint)(v))){ret=-1;err=-EINVAL;break;}\ - }while(0) +#define CHECK_NUM_NULLOK(v,size) \ + if((v)&&!Syscall_Valid((size),(Uint)(v))){ret=-1;err=-EINVAL;break;} +#define CHECK_STR_NULLOK(v) \ + if((v)&&!Syscall_ValidString((Uint)(v))){ret=-1;err=-EINVAL;break;} +#define CHECK_NUM_NONULL(v,size) \ + if(!(v)||!Syscall_Valid((size),(Uint)(v))){ret=-1;err=-EINVAL;break;} +#define CHECK_STR_NONULL(v) \ + if(!(v)||!Syscall_ValidString((Uint)(v))){ret=-1;err=-EINVAL;break;} // === IMPORTS === extern int Proc_Clone(Uint *Err, Uint Flags); @@ -48,12 +44,12 @@ void SyscallHandler(tSyscallRegs *Regs) { Uint64 ret = 0; Uint err = 0; - #if DEBUG + ENTER("iThread iNum", Threads_GetTID(), Regs->Num); if(Regs->Num < NUM_SYSCALLS) LOG("Syscall %s", cSYSCALL_NAMES[Regs->Num]); - LOG("Arg1: 0x%x, Arg2: 0x%x, Arg3: 0x%x", Regs->Arg1, Regs->Arg2, Regs->Arg3); - #endif + LOG("Arg1: 0x%x, Arg2: 0x%x, Arg3: 0x%x, Arg4: 0x%x", Regs->Arg1, Regs->Arg2, Regs->Arg3, Regs->Arg4); + //#endif switch(Regs->Num) { @@ -142,13 +138,15 @@ void SyscallHandler(tSyscallRegs *Regs) // --- case SYS_EXECVE: CHECK_STR_NONULL(Regs->Arg1); + //Log(" Regs = {Arg2: %x, Arg3: %x}", Regs->Arg2, Regs->Arg3); { int i; char **tmp = (char**)Regs->Arg2; // Check ArgV (traverse array checking all string pointers) CHECK_NUM_NONULL( tmp, sizeof(char**) ); + //Log("tmp = %p", tmp); for(i=0;tmp[i];i++) { - CHECK_NUM_NULLOK( &tmp[i], sizeof(char*) ); + CHECK_NUM_NONULL( &tmp[i], sizeof(char*) ); CHECK_STR_NONULL( tmp[i] ); } // Check EnvP also @@ -156,6 +154,7 @@ void SyscallHandler(tSyscallRegs *Regs) if( Regs->Arg3 ) { tmp = (char**)Regs->Arg3; + //Log("tmp = %p", tmp); for(i=0;tmp[i];i++) { CHECK_NUM_NULLOK( &tmp[i], sizeof(char*) ); CHECK_STR_NONULL( tmp[i] ); @@ -278,12 +277,12 @@ void SyscallHandler(tSyscallRegs *Regs) break; // -- Debug - #if DEBUG_BUILD + //#if DEBUG_BUILD case SYS_DEBUG: Log((char*)Regs->Arg1, Regs->Arg2, Regs->Arg3, Regs->Arg4, Regs->Arg5, Regs->Arg6); break; - #endif + //#endif // -- Default (Return Error) default: @@ -294,6 +293,11 @@ void SyscallHandler(tSyscallRegs *Regs) ret = -1; break; } + + if(err != 0) { + LOG("ID: %i, Return errno = %i", Regs->Num, err); + } + #if BITS < 64 Regs->Return = ret&0xFFFFFFFF; Regs->RetHi = ret >> 32; diff --git a/Kernel/system.c b/Kernel/system.c index 49af4797..af668513 100644 --- a/Kernel/system.c +++ b/Kernel/system.c @@ -252,7 +252,7 @@ void System_ExecuteScript() i += 7; i += System_Int_GetString(fData+i, &sArg1); if(!sArg1) goto read2eol; - Log("[CFG ] Load EDI Module '%s'\n", sArg1); + Log("[CFG ] Load EDI Module '%s'", sArg1); Module_LoadFile(sArg1, ""); } // - Symlink @@ -262,7 +262,7 @@ void System_ExecuteScript() if(!sArg1) goto read2eol; i += System_Int_GetString(fData+i, &sArg2); if(!sArg2) goto read2eol; - Log("[CFG ] Symlink '%s' pointing to '%s'\n", sArg1, sArg2); + Log("[CFG ] Symlink '%s' pointing to '%s'", sArg1, sArg2); VFS_Symlink(sArg1, sArg2); } // - New Directory @@ -270,7 +270,7 @@ void System_ExecuteScript() i += 6; i += System_Int_GetString(fData+i, &sArg1); if(!sArg1) goto read2eol; - Log("[CFG ] New Directory '%s'\n", sArg1); + Log("[CFG ] New Directory '%s'", sArg1); VFS_MkDir(sArg1); } // - Spawn a task @@ -278,7 +278,7 @@ void System_ExecuteScript() i += 6; i += System_Int_GetString(fData+i, &sArg1); if(!sArg1) goto read2eol; - Log("[CFG ] Starting '%s' as a new task\n", sArg1); + Log("[CFG ] Starting '%s' as a new task", sArg1); Proc_Spawn(sArg1); } else { diff --git a/Kernel/threads.c b/Kernel/threads.c index 00fc0506..3515fe09 100644 --- a/Kernel/threads.c +++ b/Kernel/threads.c @@ -708,3 +708,6 @@ void Threads_SegFault(tVAddr Addr) Warning("Thread #%i committed a segfault at address %p", Proc_GetCurThread()->TID, Addr); Threads_Exit( 0, -1 ); } + +// === EXPORTS === +EXPORT(Threads_GetUID); diff --git a/Kernel/vfs/io.c b/Kernel/vfs/io.c index b27e7f09..67a7075b 100644 --- a/Kernel/vfs/io.c +++ b/Kernel/vfs/io.c @@ -212,3 +212,12 @@ int VFS_FInfo(int FD, struct s_sysFInfo *Dest, int MaxACLs) return max; } + +// === EXPORTS === +EXPORT(VFS_Read); +EXPORT(VFS_Write); +EXPORT(VFS_ReadAt); +EXPORT(VFS_WriteAt); +EXPORT(VFS_IOCtl); +EXPORT(VFS_Seek); +EXPORT(VFS_Tell); diff --git a/Kernel/vfs/open.c b/Kernel/vfs/open.c index 507f1826..3a687037 100644 --- a/Kernel/vfs/open.c +++ b/Kernel/vfs/open.c @@ -666,3 +666,7 @@ tVFS_Handle *VFS_GetHandle(int FD) if(h->Node == NULL) return NULL; return h; } + +// === EXPORTS === +EXPORT(VFS_Open); +EXPORT(VFS_Close); diff --git a/Makefile b/Makefile index 27f4fa8c..88b8377d 100644 --- a/Makefile +++ b/Makefile @@ -4,39 +4,65 @@ .PHONY: all clean +MODULES = IPStack +USRLIBS = ld-acess.so libacess.so libgcc.so libc.so +USRAPPS = init login CLIShell cat ls mount + all: + @for mod in $(MODULES); do \ + (echo === $$mod && $(MAKE) all --no-print-directory -C Modules/$$mod) \ + done + @echo === Kernel @$(MAKE) all --no-print-directory -C Kernel - @echo === ld-acess.so - @$(MAKE) all --no-print-directory -C Usermode/Libraries/ld-acess.so_src - @echo === libacess.so - @$(MAKE) all --no-print-directory -C Usermode/Libraries/libacess.so_src - @echo === libgcc.so - @$(MAKE) all --no-print-directory -C Usermode/Libraries/libgcc.so_src - @echo === libc.so - @$(MAKE) all --no-print-directory -C Usermode/Libraries/libc.so_src - @echo === init - @$(MAKE) all --no-print-directory -C Usermode/Applications/init_src - @echo === login - @$(MAKE) all --no-print-directory -C Usermode/Applications/login_src - @echo === CLIShell - @$(MAKE) all --no-print-directory -C Usermode/Applications/CLIShell_src - @echo === cat - @$(MAKE) all --no-print-directory -C Usermode/Applications/cat_src - @echo === ls - @$(MAKE) all --no-print-directory -C Usermode/Applications/ls_src - @echo === mount - @$(MAKE) all --no-print-directory -C Usermode/Applications/mount_src + + @for lib in $(USRLIBS); do \ + (echo === $$lib && $(MAKE) all --no-print-directory -C Usermode/Libraries/`echo $$lib`_src) \ + done + + @for app in $(USRAPPS); do \ + (echo === $$app && $(MAKE) all --no-print-directory -C Usermode/Applications/`echo $$app`_src) \ + done + +# @echo === ld-acess.so +# @$(MAKE) all --no-print-directory -C Usermode/Libraries/ld-acess.so_src +# @echo === libacess.so +# @$(MAKE) all --no-print-directory -C Usermode/Libraries/libacess.so_src +# @echo === libgcc.so +# @$(MAKE) all --no-print-directory -C Usermode/Libraries/libgcc.so_src +# @echo === libc.so +# @$(MAKE) all --no-print-directory -C Usermode/Libraries/libc.so_src +# @echo === init +# @$(MAKE) all --no-print-directory -C Usermode/Applications/init_src +# @echo === login +# @$(MAKE) all --no-print-directory -C Usermode/Applications/login_src +# @echo === CLIShell +# @$(MAKE) all --no-print-directory -C Usermode/Applications/CLIShell_src +# @echo === cat +# @$(MAKE) all --no-print-directory -C Usermode/Applications/cat_src +# @echo === ls +# @$(MAKE) all --no-print-directory -C Usermode/Applications/ls_src +# @echo === mount +# @$(MAKE) all --no-print-directory -C Usermode/Applications/mount_src clean: @make clean --no-print-directory -C Kernel/ - @make clean --no-print-directory -C Usermode/Libraries/ld-acess.so_src - @make clean --no-print-directory -C Usermode/Libraries/libacess.so_src - @make clean --no-print-directory -C Usermode/Libraries/libc.so_src - @make clean --no-print-directory -C Usermode/Libraries/libgcc.so_src - @make clean --no-print-directory -C Usermode/Applications/init_src - @make clean --no-print-directory -C Usermode/Applications/login_src - @make clean --no-print-directory -C Usermode/Applications/CLIShell_src - @make clean --no-print-directory -C Usermode/Applications/cat_src - @make clean --no-print-directory -C Usermode/Applications/ls_src - @make clean --no-print-directory -C Usermode/Applications/mount_src + + @for lib in $(USRLIBS); do \ + ($(MAKE) clean --no-print-directory -C Usermode/Libraries/`echo $$lib`_src) \ + done + + @for app in $(USRAPPS); do \ + ($(MAKE) clean --no-print-directory -C Usermode/Applications/`echo $$app`_src) \ + done + +# @make clean --no-print-directory -C Usermode/Libraries/ld-acess.so_src +# @make clean --no-print-directory -C Usermode/Libraries/libacess.so_src +# @make clean --no-print-directory -C Usermode/Libraries/libc.so_src +# @make clean --no-print-directory -C Usermode/Libraries/libgcc.so_src +# @make clean --no-print-directory -C Usermode/Applications/init_src +# @make clean --no-print-directory -C Usermode/Applications/login_src +# @make clean --no-print-directory -C Usermode/Applications/CLIShell_src +# @make clean --no-print-directory -C Usermode/Applications/cat_src +# @make clean --no-print-directory -C Usermode/Applications/ls_src +# @make clean --no-print-directory -C Usermode/Applications/mount_src diff --git a/Modules/IPStack/ipstack.h b/Modules/IPStack/ipstack.h index 526ff79c..8c88daca 100644 --- a/Modules/IPStack/ipstack.h +++ b/Modules/IPStack/ipstack.h @@ -34,11 +34,11 @@ struct sInterface { struct sInterface *Next; tVFS_Node Node; tAdapter *Adapter; - int Type; // 4 for IPv4 and 6 for IPv6 + int Type; // 0 for disabled, 4 for IPv4 and 6 for IPv6 union { struct { tIPv6 Address; - int SubnetBits; + int SubnetBits; //Should this be outside the union? } IP6; struct { diff --git a/Modules/IPStack/link.c b/Modules/IPStack/link.c index 9dee6e45..4489b522 100644 --- a/Modules/IPStack/link.c +++ b/Modules/IPStack/link.c @@ -39,7 +39,10 @@ void Link_RegisterType(Uint16 Type, tPacketCallback Callback) if(i == -1) { tmp = realloc(gaRegisteredTypes, (giRegisteredTypes+1)*sizeof(*gaRegisteredTypes)); - if(!tmp) Panic("[NET ] Out of heap space!"); + if(!tmp) { + Warning("[NET ] Out of heap space!"); + return ; + } i = giRegisteredTypes; giRegisteredTypes ++; gaRegisteredTypes = tmp; @@ -77,7 +80,8 @@ void Link_WatchDevice(tAdapter *Adapter) int tid = Proc_SpawnWorker(); // Create a new worker thread if(tid < 0) { - Panic("[NET ] Unable to create watcher thread for '%s'", Adapter->Device); + Warning("[NET ] Unable to create watcher thread for '%s'", Adapter->Device); + return ; } if(tid > 0) { diff --git a/Modules/IPStack/main.c b/Modules/IPStack/main.c index 2a22d1e0..051d064d 100644 --- a/Modules/IPStack/main.c +++ b/Modules/IPStack/main.c @@ -52,12 +52,15 @@ int IPStack_Install(char **Arguments) // Install Handlers ARP_Initialise(); - // Parse module arguments - for( i = 0; Arguments[i]; i++ ) + if(Arguments) { - //if(strcmp(Arguments[i], "Device") == '=') { - // - //} + // Parse module arguments + for( i = 0; Arguments[i]; i++ ) + { + //if(strcmp(Arguments[i], "Device") == '=') { + // + //} + } } return 1; @@ -117,9 +120,17 @@ tVFS_Node *IPStack_FindDir(tVFS_Node *Node, char *Name) return &iface->Node; } -static const char *casIOCtls[] = { DRV_IOCTLNAMES, "add_interface", NULL }; +static const char *casIOCtls_Root[] = { DRV_IOCTLNAMES, "add_interface", NULL }; +static const char *casIOCtls_Iface[] = { + DRV_IOCTLNAMES, + "getset_type", + "get_address", "set_address", + "getset_subnet", + "get_gateway", "set_gateway", + NULL + }; /** - * \brief Handles IOCtls for the IPStack root + * \brief Handles IOCtls for the IPStack root/interfaces */ int IPStack_IOCtl(tVFS_Node *Node, int ID, void *Data) { @@ -139,17 +150,144 @@ int IPStack_IOCtl(tVFS_Node *Node, int ID, void *Data) case DRV_IOCTL_LOOKUP: if( !CheckString( Data ) ) return -1; - return LookupString( (char**)casIOCtls, (char*)Data ); - - // --- Non-Standard IOCtls --- - /* - * add_interface - * - Adds a new IP interface and binds it to a device - */ - case 4: - if( !CheckString( Data ) ) return -1; - return IPStack_AddInterface(Data); + + if( Node == &gIP_DriverInfo.RootNode ) + return LookupString( (char**)casIOCtls_Root, (char*)Data ); + else + return LookupString( (char**)casIOCtls_Iface, (char*)Data ); + } + + if(Node == &gIP_DriverInfo.RootNode) + { + switch(ID) + { + /* + * add_interface + * - Adds a new IP interface and binds it to a device + */ + case 4: + if( Threads_GetUID() != 0 ) return -1; + if( !CheckString( Data ) ) return -1; + return IPStack_AddInterface(Data); + } + return 0; } + else + { + tInterface *iface = (tInterface*)Node->ImplPtr; + switch(ID) + { + /* + * getset_type + * - Get/Set the interface type + */ + case 4: + // Get Type? + if( !Data ) return iface->Type; + // Ok, it's set type + if( Threads_GetUID() != 0 ) return -1; + if( !CheckMem( Data, sizeof(int) ) ) return -1; + switch( *(int*)Data ) + { + case 0: // Disable + iface->Type = 0; + memset(&iface->IP6, 0, sizeof(tIPv6)); // Clear address + break; + case 4: // IPv4 + iface->Type = 4; + memset(&iface->IP4, 0, sizeof(tIPv4)); + break; + case 6: // IPv6 + iface->Type = 6; + memset(&iface->IP6, 0, sizeof(tIPv6)); + break; + default: + return -1; + } + return 0; + + /* + * get_address + * - Get the interface's address + */ + case 5: + switch(iface->Type) + { + case 0: return 1; + case 4: + if( !CheckMem( Data, sizeof(tIPv4) ) ) return -1; + memcpy( Data, &iface->IP4, sizeof(tIPv4) ); + return 1; + case 6: + if( !CheckMem( Data, sizeof(tIPv6) ) ) return -1; + memcpy( Data, &iface->IP6, sizeof(tIPv6) ); + return 1; + } + return 0; + + /* + * set_address + * - Get the interface's address + */ + case 6: + if( Threads_GetUID() != 0 ) return -1; + switch(iface->Type) + { + case 0: return 1; + case 4: + if( !CheckMem( Data, sizeof(tIPv4) ) ) return -1; + iface->Type = 0; // One very hacky mutex/trash protector + memcpy( &iface->IP4, Data, sizeof(tIPv4) ); + iface->Type = 4; + return 1; + case 6: + if( !CheckMem( Data, sizeof(tIPv6) ) ) return -1; + iface->Type = 0; + memcpy( &iface->IP6, Data, sizeof(tIPv6) ); + iface->Type = 6; + return 1; + } + return 0; + + /* + * getset_subnet + * - Get/Set the bits in the address subnet + */ + case 7: + // Get? + if( Data == NULL ) + { + switch( iface->Type ) + { + case 4: return iface->IP4.SubnetBits; + case 6: return iface->IP6.SubnetBits; + default: return 0; + } + } + + // Ok, set. + if( Threads_GetUID() != 0 ) return -1; + if( !CheckMem(Data, sizeof(int)) ) return -1; + + // Check and set the subnet bits + switch( iface->Type ) + { + case 4: + if( *(int*)Data < 0 || *(int*)Data > 31 ) return -1; + iface->IP4.SubnetBits = *(int*)Data; + return iface->IP4.SubnetBits; + case 6: + if( *(int*)Data < 0 || *(int*)Data > 127 ) return -1; + iface->IP6.SubnetBits = *(int*)Data; + return iface->IP6.SubnetBits; + default: + break; + } + + return 0; + } + } + return 0; } diff --git a/Modules/Makefile.tpl b/Modules/Makefile.tpl index 7d20cd3d..a9a6d8db 100644 --- a/Modules/Makefile.tpl +++ b/Modules/Makefile.tpl @@ -25,6 +25,7 @@ $(BIN): $(OBJ) @echo --- $(LD) -o $@ @$(LD) -T ../link.ld -shared -nostdlib -o $@ $(OBJ) # @$(LD) -shared -nostdlib -o $@ $(OBJ) + cp $@ $(DISTROOT)/Modules/$(NAME).kmd @echo --- $(LD) -o $(KOBJ) @$(CC) -Wl,-r -nostdlib -o $(KOBJ) $(OBJ) diff --git a/Usermode/Applications/init_src/Makefile b/Usermode/Applications/init_src/Makefile index 0ec77391..9331f6ed 100644 --- a/Usermode/Applications/init_src/Makefile +++ b/Usermode/Applications/init_src/Makefile @@ -14,11 +14,14 @@ OBJ = main.o all: $(BIN) clean: - $(RM) $(BIN) $(OBJ) + $(RM) $(BIN) $(OBJ) $(BIN).dsm $(BIN): $(OBJ) Makefile - $(LD) $(LDFLAGS) $(OBJ) -o $(BIN) + @echo --- ld -o $(BIN) + @$(LD) $(LDFLAGS) $(OBJ) -o $(BIN) + $(OBJDUMP) -d $(BIN) > $(BIN).dsm cp $(BIN) $(DISTROOT)/SBin/ %.o: %.c - $(CC) $(CFLAGS) -c $< -o $@ + @echo --- cc -o $@ + @$(CC) $(CFLAGS) -c $< -o $@ diff --git a/Usermode/Applications/init_src/main.c b/Usermode/Applications/init_src/main.c index 3473eb51..c1423315 100644 --- a/Usermode/Applications/init_src/main.c +++ b/Usermode/Applications/init_src/main.c @@ -5,7 +5,7 @@ // === CONSTANTS === #define NULL ((void*)0) -#define NUM_TERMS 4 +#define NUM_TERMS 1 #define DEFAULT_TERMINAL "/Devices/VTerm/0" #define DEFAULT_SHELL "/Acess/SBin/login" @@ -17,24 +17,28 @@ int main(int argc, char *argv[]) { int tid; - // int i; - char termpath[sizeof(DEFAULT_TERMINAL)+1] = DEFAULT_TERMINAL; + int i; + char termpath[sizeof(DEFAULT_TERMINAL)] = DEFAULT_TERMINAL; + char *child_argv[2] = {DEFAULT_SHELL, 0}; - //for( i = 0; i < NUM_TERMS; i++ ) - //{ - //termpath[ sizeof(DEFAULT_TERMINAL)-1 ] = '0' + i; - open(termpath, OPENFLAG_READ); // Stdin - open(termpath, OPENFLAG_WRITE); // Stdout - open(termpath, OPENFLAG_WRITE); // Stderr - + for( i = 0; i < NUM_TERMS; i++ ) + { tid = clone(CLONE_VM, 0); if(tid == 0) { - execve(DEFAULT_SHELL, NULL, NULL); - for(;;) __asm__ __volatile__("hlt"); + termpath[sizeof(DEFAULT_TERMINAL)-2] = '0' + i; + + //__asm__ __volatile__ ("int $0xAC" :: "a" (256), "b" ("%s"), "c" (termpath)); + + open(termpath, OPENFLAG_READ); // Stdin + open(termpath, OPENFLAG_WRITE); // Stdout + open(termpath, OPENFLAG_WRITE); // Stderr + execve(DEFAULT_SHELL, child_argv, NULL); + for(;;) sleep(); } - //} + } + // TODO: Implement message watching for(;;) sleep(); return 42; diff --git a/Usermode/Libraries/Makefile.cfg b/Usermode/Libraries/Makefile.cfg index 2e09392f..e4a4aaca 100644 --- a/Usermode/Libraries/Makefile.cfg +++ b/Usermode/Libraries/Makefile.cfg @@ -3,6 +3,8 @@ -include ../../../Makefile.cfg +MAKEDEP = $(CC) -M + ASFLAGS = -felf CPPFLAGS = -I../../include/ CFLAGS = -Wall -fPIC -fno-builtin -fno-stack-protector $(CPPFLAGS) diff --git a/Usermode/Libraries/libacess.so_src/Makefile b/Usermode/Libraries/libacess.so_src/Makefile index 3118cb67..0bb0fe4e 100644 --- a/Usermode/Libraries/libacess.so_src/Makefile +++ b/Usermode/Libraries/libacess.so_src/Makefile @@ -20,7 +20,7 @@ clean: $(BIN): $(OBJ) @echo --- $(LD) -shared -o $@ @$(LD) $(LDFLAGS) -o $(BIN) $(OBJ) - $(STRIP) $(BIN) + @$(STRIP) $(BIN) cp $(BIN) $(DISTROOT)/Libs %.ao: %.asm syscalls.inc.asm diff --git a/Usermode/Libraries/libacess.so_src/mm.asm b/Usermode/Libraries/libacess.so_src/mm.asm index e16ef184..bede9aef 100644 --- a/Usermode/Libraries/libacess.so_src/mm.asm +++ b/Usermode/Libraries/libacess.so_src/mm.asm @@ -7,5 +7,5 @@ [extern _errno] [section .text] -SYSCALL0 _SysGetPhys, SYS_GETPHYS ; uint64_t _SysGetPhys(uint addr) +SYSCALL1 _SysGetPhys, SYS_GETPHYS ; uint64_t _SysGetPhys(uint addr) SYSCALL1 _SysAllocate, SYS_ALLOCATE ; uint64_t _SysAllocate(uint addr) diff --git a/Usermode/Libraries/libacess.so_src/vfs.asm b/Usermode/Libraries/libacess.so_src/vfs.asm index 77c30784..deb9751d 100644 --- a/Usermode/Libraries/libacess.so_src/vfs.asm +++ b/Usermode/Libraries/libacess.so_src/vfs.asm @@ -10,9 +10,9 @@ SYSCALL2 open, SYS_OPEN ; char*, int SYSCALL3 reopen, SYS_REOPEN ; int, char*, int SYSCALL1 close, SYS_CLOSE ; int -SYSCALL4 read, SYS_READ ; int, int64_t, void* -SYSCALL4 write, SYS_WRITE ; int, int64_t, void* -SYSCALL4 seek, SYS_SEEK ; int, int64_t, int +SYSCALL3 read, SYS_READ ; int, uint, void* +SYSCALL3 write, SYS_WRITE ; int, uint, void* +SYSCALL4 seek, SYS_SEEK ; int, uint64_t, int SYSCALL1 tell, SYS_TELL ; int SYSCALL3 finfo, SYS_FINFO ; int, void*, int SYSCALL2 readdir, SYS_READDIR ; int, char* diff --git a/Usermode/Libraries/libc.so_src/Makefile b/Usermode/Libraries/libc.so_src/Makefile index f9895a8f..ada30884 100644 --- a/Usermode/Libraries/libc.so_src/Makefile +++ b/Usermode/Libraries/libc.so_src/Makefile @@ -8,23 +8,24 @@ CFLAGS += ASFLAGS += LDFLAGS += -soname libc.so.1 -Map map.txt -lgcc -OBJ_LIBC = heap.o stdlib.o stub.o env.o fileIO.o string.o +OBJ = stub.o heap.o stdlib.o env.o fileIO.o string.o +DEPFILES := $(OBJ:%.o=%.d) # signals.o BIN = ../libc.so.1 .PHONY: all clean -all: $(BIN) $(OBJ_LIBC) +all: $(BIN) clean: - $(RM) $(BIN) ../libc.so $(OBJ_LIBC) libc.so.1.dsm libc.so.1.dmp map.txt + $(RM) $(BIN) ../libc.so $(OBJ) $(DEPFILES) libc.so.1.dsm libc.so.1.dmp map.txt # Core C Library -$(BIN): $(OBJ_LIBC) +$(BIN): $(OBJ) @echo --- ld -shared -o $@ - @$(LD) $(LDFLAGS) $(OBJ_LIBC) -o $@ - $(OBJDUMP) -d $@ > libc.so.1.dsm - $(OBJDUMP) -x -r -R $@ > libc.so.1.dmp + @$(LD) $(LDFLAGS) $(OBJ) -o $@ + @$(OBJDUMP) -d $@ > libc.so.1.dsm + @$(OBJDUMP) -x -r -R $@ > libc.so.1.dmp cp ../libc.so.1 ../libc.so cp ../libc.so.1 $(DISTROOT)/Libs/ @@ -33,10 +34,11 @@ $(BIN): $(OBJ_LIBC) @echo --- $(AS) -o $@ @$(AS) $(ASFLAGS) -o $@ $< -$(filter %.o, $(OBJ_LIBC)): %.o: %.c config.h +$(filter %.o, $(OBJ)): %.o: %.c @echo --- $(CC) -o $@ @$(CC) $(CFLAGS) -DBUILD_SO -o $@ -c $< + @$(MAKEDEP) $(CPPFLAGS) -MT $@ -o $*.d $< -$(filter %.ao, $(OBJ_LIBC)): %.ao: %.asm +$(filter %.ao, $(OBJ)): %.ao: %.asm @echo --- $(AS) -o $@ @$(AS) $(ASFLAGS) -o $@ $< diff --git a/Usermode/Libraries/libc.so_src/heap.c b/Usermode/Libraries/libc.so_src/heap.c index 10cdb24a..499d24c9 100644 --- a/Usermode/Libraries/libc.so_src/heap.c +++ b/Usermode/Libraries/libc.so_src/heap.c @@ -140,7 +140,10 @@ EXPORT void *malloc(size_t bytes) */ EXPORT void free(void *mem) { - heap_head *head = mem; + heap_head *head = (void*)((intptr_t)mem-sizeof(heap_head)); + + // Sanity please! + if(!mem) return; if(head->magic != MAGIC) //Valid Heap Address return; @@ -148,19 +151,19 @@ EXPORT void free(void *mem) head->magic = MAGIC_FREE; //Unify Right - if((Uint)head + head->size < (Uint)_heap_end) + if((intptr_t)head + head->size < (intptr_t)_heap_end) { - heap_head *nextHead = (heap_head*)((Uint)head + head->size); + heap_head *nextHead = (heap_head*)((intptr_t)head + head->size); if(nextHead->magic == MAGIC_FREE) { //Is the next block free head->size += nextHead->size; //Amalgamate nextHead->magic = 0; //For Security } } //Unify Left - if((Uint)head - sizeof(heap_foot) > (Uint)_heap_start) + if((intptr_t)head - sizeof(heap_foot) > (intptr_t)_heap_start) { heap_head *prevHead; - heap_foot *prevFoot = (heap_foot *)((Uint)head - sizeof(heap_foot)); + heap_foot *prevFoot = (heap_foot *)((intptr_t)head - sizeof(heap_foot)); if(prevFoot->magic == MAGIC) { prevHead = prevFoot->header; if(prevHead->magic == MAGIC_FREE) { @@ -304,10 +307,12 @@ EXPORT int IsHeap(void *ptr) */ static void *FindHeapBase() { + #if 0 #define MAX 0xC0000000 // Address #define THRESHOLD 512 // Pages uint addr; uint stretch = 0; + uint64_t tmp; // Scan address space for(addr = 0; @@ -315,17 +320,25 @@ static void *FindHeapBase() addr += 0x1000 ) { - if( _SysGetPhys(addr) == 0 ) { + tmp = _SysGetPhys(addr); + if( tmp != 0 ) { stretch = 0; } else { stretch ++; if(stretch > THRESHOLD) { - return (void*)( addr + stretch*0x1000 ); + return (void*)( addr - stretch*0x1000 ); } } + //__asm__ __volatile__ ( + // "push %%ebx;mov %%edx,%%ebx;int $0xAC;pop %%ebx" + // ::"a"(256),"d"("%x"),"c"(addr)); } + return NULL; + #else + return (void*)0x00900000; + #endif } LOCAL uint brk(Uint newpos) diff --git a/Usermode/Libraries/libc.so_src/stub.c b/Usermode/Libraries/libc.so_src/stub.c index 0233b977..9f629ba4 100644 --- a/Usermode/Libraries/libc.so_src/stub.c +++ b/Usermode/Libraries/libc.so_src/stub.c @@ -4,8 +4,12 @@ #include "stdio_int.h" #include "lib.h" +#define USE_CPUID 0 + // === PROTOTYPES === +#if USE_CPUID static void cpuid(uint32_t Num, uint32_t *EAX, uint32_t *EBX, uint32_t *EDX, uint32_t *ECX); +#endif // === GLOBALS === extern char **_envp; @@ -14,7 +18,9 @@ extern struct sFILE *stdin; extern struct sFILE *stdout; extern struct sFILE *stderr; // --- CPU Features --- +#if USE_CPUID tCPUID gCPU_Features; +#endif // === CODE === /** @@ -38,19 +44,20 @@ int SoMain(unsigned int BaseAddress, int argc, char **argv, char **envp) stderr = &_iob[2]; stderr->FD = 2; stderr->Flags = FILE_FLAG_MODE_WRITE; - /* + #if USE_CPUID { uint32_t ecx, edx; cpuid(1, NULL, NULL, &edx, &ecx); gCPU_Features.SSE = edx & (1 << 25); // SSE gCPU_Features.SSE2 = edx & (1 << 26); // SSE2 } - */ + #endif return 1; } +#if USE_CPUID /** * \brief Call the CPUID opcode */ @@ -69,3 +76,4 @@ static void cpuid(uint32_t Num, uint32_t *EAX, uint32_t *EBX, uint32_t *EDX, uin if(EDX) *EDX = edx; if(ECX) *ECX = ecx; } +#endif diff --git a/Usermode/include/acess/sys.h b/Usermode/include/acess/sys.h index a1bdbbbf..f7deac7d 100644 --- a/Usermode/include/acess/sys.h +++ b/Usermode/include/acess/sys.h @@ -75,8 +75,8 @@ extern int chdir(const char *dir); extern int open(const char *path, int flags); extern int reopen(int fd, const char *path, int flags); extern void close(int fd); -extern uint64_t read(int fd, uint64_t length, void *buffer); -extern uint64_t write(int fd, uint64_t length, void *buffer); +extern uint read(int fd, uint length, void *buffer); +extern uint write(int fd, uint length, void *buffer); extern int seek(int fd, uint64_t offset, int whence); extern uint64_t tell(int fd); extern int ioctl(int fd, int id, void *data); -- 2.20.1