3 * - By John Hodge (thePowersGang)
14 #define MMAP_PAGES_PER_BLOCK 16
17 typedef struct sVFS_MMapPageBlock tVFS_MMapPageBlock;
18 struct sVFS_MMapPageBlock
20 tVFS_MMapPageBlock *Next;
21 Uint64 BaseOffset; // Must be a multiple of MMAP_PAGES_PER_BLOCK*PAGE_SIZE
22 tPAddr PhysAddrs[MMAP_PAGES_PER_BLOCK];
26 //void *VFS_MMap(void *DestHint, size_t Length, int Protection, int Flags, int FD, Uint64 Offset);
27 void *VFS_MMap_Anon(void *Destination, size_t Length, Uint FlagsSet, Uint FlagsMask);
28 int VFS_MMap_MapPage(tVFS_Node *Node, unsigned int PageNum, tVFS_MMapPageBlock *pb, void *mapping_dest, unsigned int Protection);
29 //int VFS_MUnmap(void *Addr, size_t Length);
32 void *VFS_MMap(void *DestHint, size_t Length, int Protection, int Flags, int FD, Uint64 Offset)
34 ENTER("pDestHint iLength xProtection xFlags xFD XOffset", DestHint, Length, Protection, Flags, FD, Offset);
36 if( Flags & MMAP_MAP_ANONYMOUS )
37 Offset = (tVAddr)DestHint & 0xFFF;
39 unsigned int npages = ((Offset & (PAGE_SIZE-1)) + Length + (PAGE_SIZE - 1)) / PAGE_SIZE;
40 unsigned int pagenum = Offset / PAGE_SIZE;
42 tVAddr mapping_base = (tVAddr)DestHint;
44 if( Flags & MMAP_MAP_FIXED )
46 ASSERT( (Flags & MMAP_MAP_FIXED) && DestHint != NULL );
47 // Keep and use the hint
48 // - TODO: Validate that the region pointed to by the hint is correct
52 Log_Warning("VFS", "MMap: TODO Handle non-fixed mappings");
53 if( DestHint == NULL )
55 // TODO: Locate space for the allocation
56 Log_Warning("VFS", "Mmap: Handle NULL destination hint");
61 tPage *mapping_dest = (void*)(mapping_base & ~(PAGE_SIZE-1));
63 // Handle anonymous mappings
64 if( Flags & MMAP_MAP_ANONYMOUS )
66 // TODO: Comvert \a Protection into a flag set
67 void *ret = VFS_MMap_Anon((void*)mapping_base, Length, 0, 0);
71 tVFS_Handle *h = VFS_GetHandle(FD);
72 if( !h || !h->Node ) LEAVE_RET('n', NULL);
76 Mutex_Acquire( &h->Node->Lock );
78 tVFS_MMapPageBlock *pb, **pb_pnp = (tVFS_MMapPageBlock**)&h->Node->MMapInfo;
79 // Search for existing mapping for each page
80 // - Sorted list of 16 page blocks
81 for( pb = h->Node->MMapInfo; pb; pb_pnp = &pb->Next, pb = pb->Next )
83 if( pb->BaseOffset + MMAP_PAGES_PER_BLOCK <= pagenum )
87 LOG("pb = %p, pb->BaseOffset = %X", pb, pb ? pb->BaseOffset : 0);
89 // - Allocate a block if needed
90 if( !pb || pb->BaseOffset > pagenum )
93 pb = calloc( 1, sizeof(tVFS_MMapPageBlock) );
95 Mutex_Release( &h->Node->Lock );
99 pb->BaseOffset = pagenum - pagenum % MMAP_PAGES_PER_BLOCK;
103 // - Map (and allocate) pages
106 ASSERTC( pagenum, >=, pb->BaseOffset );
107 ASSERTC( pagenum - pb->BaseOffset, <, MMAP_PAGES_PER_BLOCK );
108 if( MM_GetPhysAddr( mapping_dest ) == 0 )
110 LOG("Map page to %p", mapping_dest);
111 if( VFS_MMap_MapPage(h->Node, pagenum, pb, mapping_dest, Protection) )
113 Mutex_Release( &h->Node->Lock );
120 LOG("Flag update on %p", mapping_dest);
121 if( (MM_GetFlags(mapping_dest) & MM_PFLAG_RO) && (Protection & MMAP_PROT_WRITE) )
123 MM_SetFlags(mapping_dest, 0, MM_PFLAG_RO);
126 if( Flags & MMAP_MAP_PRIVATE ) {
127 // TODO: Don't allow the page to change underneath either
128 MM_SetFlags(mapping_dest, MM_PFLAG_COW, MM_PFLAG_COW);
133 // Roll on to next block if needed
134 if(pagenum - pb->BaseOffset == MMAP_PAGES_PER_BLOCK)
136 if( !pb->Next || pb->Next->BaseOffset != pagenum )
138 if( pb->Next ) ASSERTC(pb->Next->BaseOffset % MMAP_PAGES_PER_BLOCK, ==, 0);
139 tVFS_MMapPageBlock *newpb = malloc( sizeof(tVFS_MMapPageBlock) );
140 newpb->Next = pb->Next;
141 newpb->BaseOffset = pagenum;
142 memset(newpb->PhysAddrs, 0, sizeof(newpb->PhysAddrs));
150 Mutex_Release( &h->Node->Lock );
152 LEAVE('p', mapping_base);
153 return (void*)mapping_base;
156 void *VFS_MMap_Anon(void *Destination, size_t Length, Uint FlagsSet, Uint FlagsMask)
158 size_t ofs = (tVAddr)Destination & (PAGE_SIZE-1);
159 tPage *mapping_dest = (void*)( (char*)Destination - ofs );
163 size_t bytes = MIN(PAGE_SIZE - ofs, Length);
165 // Allocate a partial page
166 if( MM_GetPhysAddr(mapping_dest) )
168 // Already allocated page, clear the area we're touching
169 ASSERT( ofs + bytes <= PAGE_SIZE );
171 // TODO: Double check that this area isn't already zero
172 memset( Destination, 0, bytes );
174 MM_SetFlags(mapping_dest, FlagsSet, FlagsMask);
176 LOG("#1: Clear %i from %p", Length, Destination);
180 MM_AllocateZero(mapping_dest);
181 LOG("#1: Allocate for %p", Destination);
186 while( Length >= PAGE_SIZE )
188 if( MM_GetPhysAddr( mapping_dest ) )
190 // We're allocating entire pages here, so free this page and replace with a COW zero
191 MM_Deallocate(mapping_dest);
192 LOG("Replace %p with zero page", mapping_dest);
196 LOG("Allocate zero at %p", mapping_dest);
198 MM_AllocateZero(mapping_dest);
205 ASSERT(Length < PAGE_SIZE);
208 if( MM_GetPhysAddr(mapping_dest) )
210 // TODO: Don't touch page if already zero
211 memset( mapping_dest, 0, Length );
212 LOG("Clear %i in %p", Length, mapping_dest);
216 MM_AllocateZero(mapping_dest);
217 LOG("Anon map to %p", mapping_dest);
224 int VFS_MMap_MapPage(tVFS_Node *Node, unsigned int pagenum, tVFS_MMapPageBlock *pb, void *mapping_dest, unsigned int Protection)
226 if( pb->PhysAddrs[pagenum - pb->BaseOffset] != 0 )
228 MM_Map( mapping_dest, pb->PhysAddrs[pagenum - pb->BaseOffset] );
229 MM_RefPhys( pb->PhysAddrs[pagenum - pb->BaseOffset] );
230 LOG("Cached map %X to %p (%P)", pagenum*PAGE_SIZE, mapping_dest,
231 pb->PhysAddrs[pagenum - pb->BaseOffset]);
235 tVFS_NodeType *nt = Node->Type;
241 nt->MMap(Node, pagenum*PAGE_SIZE, PAGE_SIZE, mapping_dest);
245 // Allocate pages and read data
246 if( MM_Allocate(mapping_dest) == 0 ) {
250 // TODO: Clip read length
251 read_len = nt->Read(Node, pagenum*PAGE_SIZE, PAGE_SIZE, mapping_dest, 0);
252 // TODO: This was commented out, why?
253 if( read_len != PAGE_SIZE ) {
254 memset( (char*)mapping_dest + read_len, 0, PAGE_SIZE-read_len );
257 pb->PhysAddrs[pagenum - pb->BaseOffset] = MM_GetPhysAddr( mapping_dest );
258 MM_SetPageNode( pb->PhysAddrs[pagenum - pb->BaseOffset], Node );
259 MM_RefPhys( pb->PhysAddrs[pagenum - pb->BaseOffset] );
260 LOG("Read and map %X to %p (%P)", pagenum*PAGE_SIZE, mapping_dest,
261 pb->PhysAddrs[pagenum - pb->BaseOffset]);
264 Node->ReferenceCount ++;
267 if( !(Protection & MMAP_PROT_WRITE) ) {
268 MM_SetFlags(mapping_dest, MM_PFLAG_RO, MM_PFLAG_RO);
271 MM_SetFlags(mapping_dest, 0, MM_PFLAG_RO);
274 if( Protection & MMAP_PROT_EXEC ) {
275 MM_SetFlags(mapping_dest, MM_PFLAG_EXEC, MM_PFLAG_EXEC);
278 MM_SetFlags(mapping_dest, 0, MM_PFLAG_EXEC);
284 int VFS_MUnmap(void *Addr, size_t Length)