Kernel - Fiddling with MMap
authorJohn Hodge <[email protected]>
Mon, 18 Jul 2011 02:11:42 +0000 (10:11 +0800)
committerJohn Hodge <[email protected]>
Mon, 18 Jul 2011 02:11:42 +0000 (10:11 +0800)
Kernel/include/vfs_ext.h
Kernel/vfs/mmap.c

index ec3e0f6..4fe8923 100644 (file)
@@ -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
index dbdd4b2..98137d4 100644 (file)
@@ -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
                {

UCC git Repository :: git.ucc.asn.au