From: John Hodge Date: Mon, 18 Jul 2011 02:11:42 +0000 (+0800) Subject: Kernel - Fiddling with MMap X-Git-Tag: rel0.10~35 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=386adcea9ab9e3b30a83c57ea6cfffe011536891;p=tpg%2Facess2.git Kernel - Fiddling with MMap --- diff --git a/Kernel/include/vfs_ext.h b/Kernel/include/vfs_ext.h index ec3e0f65..4fe89233 100644 --- a/Kernel/include/vfs_ext.h +++ b/Kernel/include/vfs_ext.h @@ -304,4 +304,23 @@ extern int VFS_OpenChild(Uint *Errno, int FD, const char *Name, Uint Mode); */ extern int VFS_Select(int MaxHandle, fd_set *ReadHandles, fd_set *WriteHandles, fd_set *ErrHandles, tTime *Timeout, BOOL IsKernel); +/** + * \brief Map a file into memory + * \param ErrNo Error status pointer + * \param DestHint Suggested place for read data + * \param Length Size of area to map + * \param Protection Protection type (see `man mmap`) + * \param Flags Mapping flags + * \param FD File descriptor to load from + * \param Offset Start of region + */ +extern void *VFS_MMap(int *ErrNo, void *DestHint, size_t Length, int Protection, int Flags, int FD, Uint64 Offset); + +/** + * \brief Unmap memory allocated by VFS_MMap + * \param ErrNo Error status pointer + * \param Addr Address of data to unmap + * \param Length Length of data + */ +extern int VFS_MUnmap(int *ErrNo, void *Addr, size_t Length); #endif diff --git a/Kernel/vfs/mmap.c b/Kernel/vfs/mmap.c index dbdd4b2a..98137d44 100644 --- a/Kernel/vfs/mmap.c +++ b/Kernel/vfs/mmap.c @@ -31,14 +31,16 @@ void *VFS_MMap(int *ErrNo, void *DestHint, size_t Length, int Protection, int Fl npages = ((Offset & (PAGE_SIZE-1)) + Length) / PAGE_SIZE; pagenum = Offset / PAGE_SIZE; - mapping_dest = DestHint; + mapping_dest = (tVAddr)DestHint; +#if 0 // TODO: Locate space for the allocation if( Flags & MAP_ANONYMOUS ) { MM_Allocate(mapping_dest); return (void*)mapping_dest; } +#endif h = VFS_GetHandle(FD); if( !h || !h->Node ) return NULL; @@ -51,10 +53,10 @@ void *VFS_MMap(int *ErrNo, void *DestHint, size_t Length, int Protection, int Fl prev = pb, pb = pb->Next ); + // - Allocate a block if needed if( !pb || pb->BaseOffset > pagenum ) { void *old_pb = pb; - // Allocate if needed pb = malloc( sizeof(tVFS_MMapPageBlock) ); if(!pb) return NULL; pb->Next = old_pb; @@ -66,6 +68,7 @@ void *VFS_MMap(int *ErrNo, void *DestHint, size_t Length, int Protection, int Fl h->Node->MMapInfo = pb; } + // - Map (and allocate) pages while( npages -- ) { if( pb->PhysAddrs[pagenum - pb->BaseOffset] == 0 ) @@ -75,10 +78,14 @@ void *VFS_MMap(int *ErrNo, void *DestHint, size_t Length, int Protection, int Fl else { // Allocate pages and read data - MM_Allocate(mapping_dest); + if( MM_Allocate(mapping_dest) == 0 ) { + // TODO: Unwrap + return NULL; + } h->Node->Read(h->Node, pagenum*PAGE_SIZE, PAGE_SIZE, (void*)mapping_dest); } pb->PhysAddrs[pagenum - pb->BaseOffset] = MM_GetPhysAddr( mapping_dest ); +// MM_SetPageInfo( pb->PhysAddrs[pagenum - pb->BaseOffset], h->Node, pagenum*PAGE_SIZE ); } else {