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_MUnmap(void *Addr, size_t Length);
31 void *VFS_MMap(void *DestHint, size_t Length, int Protection, int Flags, int FD, Uint64 Offset)
35 tVFS_MMapPageBlock *pb, *prev;
37 ENTER("pDestHint iLength xProtection xFlags xFD XOffset", DestHint, Length, Protection, Flags, FD, Offset);
39 if( Flags & MMAP_MAP_ANONYMOUS )
40 Offset = (tVAddr)DestHint & 0xFFF;
42 npages = ((Offset & (PAGE_SIZE-1)) + Length + (PAGE_SIZE - 1)) / PAGE_SIZE;
43 pagenum = Offset / PAGE_SIZE;
45 mapping_base = (tVAddr)DestHint;
46 tPage *mapping_dest = (void*)(mapping_base & ~(PAGE_SIZE-1));
48 if( DestHint == NULL )
50 // TODO: Locate space for the allocation
55 // Handle anonymous mappings
56 if( Flags & MMAP_MAP_ANONYMOUS )
58 // TODO: Comvert \a Protection into a flag set
59 void *ret = VFS_MMap_Anon((void*)mapping_base, Length, 0, 0);
63 tVFS_Handle *h = VFS_GetHandle(FD);
64 if( !h || !h->Node ) LEAVE_RET('n', NULL);
68 Mutex_Acquire( &h->Node->Lock );
70 // Search for existing mapping for each page
71 // - Sorted list of 16 page blocks
73 pb = h->Node->MMapInfo, prev = NULL;
74 pb && pb->BaseOffset + MMAP_PAGES_PER_BLOCK < pagenum;
75 prev = pb, pb = pb->Next
79 LOG("pb = %p, pb->BaseOffset = %X", pb, pb ? pb->BaseOffset : 0);
81 // - Allocate a block if needed
82 if( !pb || pb->BaseOffset > pagenum )
85 pb = calloc( 1, sizeof(tVFS_MMapPageBlock) );
87 Mutex_Release( &h->Node->Lock );
91 pb->BaseOffset = pagenum - pagenum % MMAP_PAGES_PER_BLOCK;
95 h->Node->MMapInfo = pb;
98 // - Map (and allocate) pages
101 if( MM_GetPhysAddr( mapping_dest ) == 0 )
103 if( pb->PhysAddrs[pagenum - pb->BaseOffset] == 0 )
105 tVFS_NodeType *nt = h->Node->Type;
111 nt->MMap(h->Node, pagenum*PAGE_SIZE, PAGE_SIZE, mapping_dest);
115 // Allocate pages and read data
116 if( MM_Allocate(mapping_dest) == 0 ) {
118 Mutex_Release( &h->Node->Lock );
122 // TODO: Clip read length
123 read_len = nt->Read(h->Node, pagenum*PAGE_SIZE, PAGE_SIZE,
125 // TODO: This was commented out, why?
126 if( read_len != PAGE_SIZE ) {
127 memset( (char*)mapping_dest + read_len, 0, PAGE_SIZE-read_len );
130 pb->PhysAddrs[pagenum - pb->BaseOffset] = MM_GetPhysAddr( mapping_dest );
131 MM_SetPageNode( pb->PhysAddrs[pagenum - pb->BaseOffset], h->Node );
132 MM_RefPhys( pb->PhysAddrs[pagenum - pb->BaseOffset] );
133 LOG("Read and map %X to %p (%P)", pagenum*PAGE_SIZE, mapping_dest,
134 pb->PhysAddrs[pagenum - pb->BaseOffset]);
138 MM_Map( mapping_dest, pb->PhysAddrs[pagenum - pb->BaseOffset] );
139 MM_RefPhys( pb->PhysAddrs[pagenum - pb->BaseOffset] );
140 LOG("Cached map %X to %p (%P)", pagenum*PAGE_SIZE, mapping_dest,
141 pb->PhysAddrs[pagenum - pb->BaseOffset]);
143 h->Node->ReferenceCount ++;
146 if( !(Protection & MMAP_PROT_WRITE) ) {
147 MM_SetFlags(mapping_dest, MM_PFLAG_RO, MM_PFLAG_RO);
150 MM_SetFlags(mapping_dest, 0, MM_PFLAG_RO);
153 if( Protection & MMAP_PROT_EXEC ) {
154 MM_SetFlags(mapping_dest, MM_PFLAG_EXEC, MM_PFLAG_EXEC);
157 MM_SetFlags(mapping_dest, 0, MM_PFLAG_EXEC);
162 LOG("Flag update on %p", mapping_dest);
163 if( (MM_GetFlags(mapping_dest) & MM_PFLAG_RO) && (Protection & MMAP_PROT_WRITE) )
165 MM_SetFlags(mapping_dest, 0, MM_PFLAG_RO);
168 if( Flags & MMAP_MAP_PRIVATE )
169 MM_SetFlags(mapping_dest, MM_PFLAG_COW, MM_PFLAG_COW);
173 // Roll on to next block if needed
174 if(pagenum - pb->BaseOffset == MMAP_PAGES_PER_BLOCK)
176 if( pb->Next && pb->Next->BaseOffset == pagenum )
180 tVFS_MMapPageBlock *oldpb = pb;
181 pb = malloc( sizeof(tVFS_MMapPageBlock) );
182 pb->Next = oldpb->Next;
183 pb->BaseOffset = pagenum;
184 memset(pb->PhysAddrs, 0, sizeof(pb->PhysAddrs));
190 Mutex_Release( &h->Node->Lock );
192 LEAVE('p', mapping_base);
193 return (void*)mapping_base;
196 void *VFS_MMap_Anon(void *Destination, size_t Length, Uint FlagsSet, Uint FlagsMask)
198 size_t ofs = (tVAddr)Destination & (PAGE_SIZE-1);
199 tPage *mapping_dest = (void*)( (char*)Destination - ofs );
203 size_t bytes = MIN(PAGE_SIZE - ofs, Length);
205 // Allocate a partial page
206 if( MM_GetPhysAddr(mapping_dest) )
208 // Already allocated page, clear the area we're touching
209 ASSERT( ofs + bytes <= PAGE_SIZE );
211 // TODO: Double check that this area isn't already zero
212 memset( Destination, 0, bytes );
214 MM_SetFlags(mapping_dest, FlagsSet, FlagsMask);
216 LOG("#1: Clear %i from %p", Length, Destination);
220 MM_AllocateZero(mapping_dest);
221 LOG("#1: Allocate for %p", Destination);
226 while( Length >= PAGE_SIZE )
228 if( MM_GetPhysAddr( mapping_dest ) )
230 // We're allocating entire pages here, so free this page and replace with a COW zero
231 MM_Deallocate(mapping_dest);
232 LOG("Replace %p with zero page", mapping_dest);
236 LOG("Allocate zero at %p", mapping_dest);
238 MM_AllocateZero(mapping_dest);
245 ASSERT(Length < PAGE_SIZE);
248 if( MM_GetPhysAddr(mapping_dest) )
250 // TODO: Don't touch page if already zero
251 memset( mapping_dest, 0, Length );
252 LOG("Clear %i in %p", Length, mapping_dest);
256 MM_AllocateZero(mapping_dest);
257 LOG("Anon map to %p", mapping_dest);
264 int VFS_MUnmap(void *Addr, size_t Length)