From 1961808f2b5eea942c11391d91a50e28eb780dde Mon Sep 17 00:00:00 2001 From: John Hodge Date: Wed, 24 Aug 2011 10:57:04 +0800 Subject: [PATCH] Kernel - Bugfixing mmap and new binary loading --- Kernel/binary.c | 11 ++++++++--- Kernel/threads.c | 5 ++++- Kernel/vfs/mmap.c | 30 ++++++++++++++++++++---------- Kernel/vfs/open.c | 7 ++++++- 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/Kernel/binary.c b/Kernel/binary.c index f00e8518..fb31fc3b 100644 --- a/Kernel/binary.c +++ b/Kernel/binary.c @@ -76,13 +76,13 @@ int Proc_Spawn(const char *Path) strcpy(stackPath, Path); - LOG("stackPath = '%s'\n", stackPath); + LOG("stackPath = '%s'", stackPath); if(Proc_Clone(CLONE_VM) == 0) { // CHILD const char *args[2] = {stackPath, NULL}; - LOG("stackPath = '%s'\n", stackPath); + LOG("stackPath = '%s'", stackPath); Proc_Execve(stackPath, args, &args[1]); for(;;); } @@ -281,6 +281,9 @@ tVAddr Binary_MapIn(tBinary *Binary, const char *Path, tVAddr LoadMin, tVAddr Lo { tVAddr base; int i, fd; + + ENTER("pBinary sPath pLoadMin pLoadMax", Binary, Path, LoadMin, LoadMax); + // Reference Executable (Makes sure that it isn't unloaded) Binary->ReferenceCount ++; @@ -324,6 +327,7 @@ tVAddr Binary_MapIn(tBinary *Binary, const char *Path, tVAddr LoadMin, tVAddr Lo // Error Check if(base < LoadMin) { Log_Warning("Binary", "Executable '%s' cannot be loaded, no space", Path); + LEAVE('i', 0); return 0; } @@ -362,6 +366,7 @@ tVAddr Binary_MapIn(tBinary *Binary, const char *Path, tVAddr LoadMin, tVAddr Lo //LOG("*0x%x = 0x%x\n", binary->Pages[0].Virtual, *(Uint*)binary->Pages[0].Virtual); + LEAVE('p', base); return base; } @@ -405,7 +410,7 @@ tBinary *Binary_DoLoad(tMount MountID, tInode Inode, const char *Path) Uint ident; tBinaryType *bt = gRegBinTypes; - ENTER("iMountID XInode sPath", Path); + ENTER("iMountID XInode sPath", MountID, Inode, Path); // Open File fp = VFS_OpenInode(MountID, Inode, VFS_OPENFLAG_READ); diff --git a/Kernel/threads.c b/Kernel/threads.c index 7114745a..4a2ccf4c 100644 --- a/Kernel/threads.c +++ b/Kernel/threads.c @@ -988,13 +988,16 @@ void Threads_Fault(int Num) Proc_CallFaultHandler(thread); } +extern void MM_DumpTables(tVAddr Start, tVAddr End); + /** * \fn void Threads_SegFault(tVAddr Addr) * \brief Called when a Segment Fault occurs */ void Threads_SegFault(tVAddr Addr) { - Warning("Thread #%i committed a segfault at address %p", Proc_GetCurThread()->TID, Addr); + Log_Warning("Threads", "Thread #%i committed a segfault at address %p", Proc_GetCurThread()->TID, Addr); + MM_DumpTables(0, 0xC0000000); Threads_Fault( 1 ); //Threads_Exit( 0, -1 ); } diff --git a/Kernel/vfs/mmap.c b/Kernel/vfs/mmap.c index 455ac96d..709686bb 100644 --- a/Kernel/vfs/mmap.c +++ b/Kernel/vfs/mmap.c @@ -9,7 +9,6 @@ #include #define MMAP_PAGES_PER_BLOCK 16 -#define PAGE_SIZE 0x1000 // Should be in mm_virt.h // === STRUCTURES === typedef struct sVFS_MMapPageBlock tVFS_MMapPageBlock; @@ -27,23 +26,33 @@ void *VFS_MMap(void *DestHint, size_t Length, int Protection, int Flags, int FD, tVAddr mapping_dest; int npages, pagenum; tVFS_MMapPageBlock *pb, *prev; + + ENTER("pDestHint iLength xProtection xFlags xFD XOffset", DestHint, Length, Protection, Flags, FD, Offset); - npages = ((Offset & (PAGE_SIZE-1)) + Length) / PAGE_SIZE; + npages = ((Offset & (PAGE_SIZE-1)) + Length + (PAGE_SIZE - 1)) / PAGE_SIZE; pagenum = Offset / PAGE_SIZE; mapping_dest = (tVAddr)DestHint; -#if 0 // TODO: Locate space for the allocation - if( Flags & MAP_ANONYMOUS ) + + // Handle anonymous mappings + if( Flags & MMAP_MAP_ANONYMOUS ) { - MM_Allocate(mapping_dest); - return (void*)mapping_dest; + int i; + for( i = 0; i < npages; i ++ ) { + tPAddr rv = MM_Allocate(mapping_dest + i * PAGE_SIZE); + if( !rv ) { + // TODO: Error + } + } + LEAVE_RET('p', (void*)mapping_dest); } -#endif h = VFS_GetHandle(FD); - if( !h || !h->Node ) return NULL; + if( !h || !h->Node ) LEAVE_RET('n', NULL); + + LOG("h = %p", h); // Search for existing mapping for each page // - Sorted list of 16 page blocks @@ -58,7 +67,7 @@ void *VFS_MMap(void *DestHint, size_t Length, int Protection, int Flags, int FD, { void *old_pb = pb; pb = malloc( sizeof(tVFS_MMapPageBlock) ); - if(!pb) return NULL; + if(!pb) LEAVE_RET('n', NULL); pb->Next = old_pb; pb->BaseOffset = pagenum - pagenum % MMAP_PAGES_PER_BLOCK; memset(pb->PhysAddrs, 0, sizeof(pb->PhysAddrs)); @@ -111,7 +120,8 @@ void *VFS_MMap(void *DestHint, size_t Length, int Protection, int Flags, int FD, pagenum = 0; } } - + + LEAVE('n'); return NULL; } diff --git a/Kernel/vfs/open.c b/Kernel/vfs/open.c index f549d2bd..c4c9bbfa 100644 --- a/Kernel/vfs/open.c +++ b/Kernel/vfs/open.c @@ -452,6 +452,9 @@ restart_parse: int VFS_int_CreateHandle( tVFS_Node *Node, tVFS_Mount *Mount, int Mode ) { int i; + + ENTER("pNode pMount iMode", Node, Mount, Mode); + i = 0; i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0; i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0; @@ -578,7 +581,7 @@ int VFS_OpenInode(Uint32 Mount, Uint64 Inode, int Mode) tVFS_Mount *mnt; tVFS_Node *node; - ENTER("iMount iInode xMode", Mount, Inode, Mode); + ENTER("iMount XInode xMode", Mount, Inode, Mode); // Get mount point mnt = VFS_GetMountByIdent(Mount); @@ -590,6 +593,7 @@ int VFS_OpenInode(Uint32 Mount, Uint64 Inode, int Mode) // Does the filesystem support this? if( !mnt->Filesystem->GetNodeFromINode ) { + LOG("Filesystem does not support inode accesses"); errno = ENOENT; LEAVE_RET('i', -1); } @@ -597,6 +601,7 @@ int VFS_OpenInode(Uint32 Mount, Uint64 Inode, int Mode) // Get node node = mnt->Filesystem->GetNodeFromINode(mnt->RootNode, Inode); if( !node ) { + LOG("Unable to find inode"); errno = ENOENT; LEAVE_RET('i', -1); } -- 2.20.1