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);
30 void *VFS_MMap(void *DestHint, size_t Length, int Protection, int Flags, int FD, Uint64 Offset)
34 tVFS_MMapPageBlock *pb, *prev;
36 ENTER("pDestHint iLength xProtection xFlags xFD XOffset", DestHint, Length, Protection, Flags, FD, Offset);
38 if( Flags & MMAP_MAP_ANONYMOUS )
39 Offset = (tVAddr)DestHint & 0xFFF;
41 npages = ((Offset & (PAGE_SIZE-1)) + Length + (PAGE_SIZE - 1)) / PAGE_SIZE;
42 pagenum = Offset / PAGE_SIZE;
44 mapping_base = (tVAddr)DestHint;
45 tPage *mapping_dest = (void*)(mapping_base & ~(PAGE_SIZE-1));
47 if( DestHint == NULL )
49 // TODO: Locate space for the allocation
54 // Handle anonymous mappings
55 if( Flags & MMAP_MAP_ANONYMOUS )
57 // TODO: Comvert \a Protection into a flag set
58 void *ret = VFS_MMap_Anon((void*)mapping_base, Length, 0, 0);
62 tVFS_Handle *h = VFS_GetHandle(FD);
63 if( !h || !h->Node ) LEAVE_RET('n', NULL);
67 Mutex_Acquire( &h->Node->Lock );
69 // Search for existing mapping for each page
70 // - Sorted list of 16 page blocks
72 pb = h->Node->MMapInfo, prev = NULL;
73 pb && pb->BaseOffset + MMAP_PAGES_PER_BLOCK < pagenum;
74 prev = pb, pb = pb->Next
78 LOG("pb = %p, pb->BaseOffset = %X", pb, pb ? pb->BaseOffset : 0);
80 // - Allocate a block if needed
81 if( !pb || pb->BaseOffset > pagenum )
84 pb = calloc( 1, sizeof(tVFS_MMapPageBlock) );
86 Mutex_Release( &h->Node->Lock );
90 pb->BaseOffset = pagenum - pagenum % MMAP_PAGES_PER_BLOCK;
94 h->Node->MMapInfo = pb;
97 // - Map (and allocate) pages
100 if( MM_GetPhysAddr( mapping_dest ) == 0 )
102 if( pb->PhysAddrs[pagenum - pb->BaseOffset] == 0 )
104 tVFS_NodeType *nt = h->Node->Type;
110 nt->MMap(h->Node, pagenum*PAGE_SIZE, PAGE_SIZE, mapping_dest);
114 // Allocate pages and read data
115 if( MM_Allocate(mapping_dest) == 0 ) {
117 Mutex_Release( &h->Node->Lock );
121 // TODO: Clip read length
122 read_len = nt->Read(h->Node, pagenum*PAGE_SIZE, PAGE_SIZE,
124 // TODO: This was commented out, why?
125 if( read_len != PAGE_SIZE ) {
126 memset( (char*)mapping_dest + read_len, 0, PAGE_SIZE-read_len );
129 pb->PhysAddrs[pagenum - pb->BaseOffset] = MM_GetPhysAddr( mapping_dest );
130 MM_SetPageNode( pb->PhysAddrs[pagenum - pb->BaseOffset], h->Node );
131 MM_RefPhys( pb->PhysAddrs[pagenum - pb->BaseOffset] );
132 LOG("Read and map %X to %p (%P)", pagenum*PAGE_SIZE, mapping_dest,
133 pb->PhysAddrs[pagenum - pb->BaseOffset]);
137 MM_Map( mapping_dest, pb->PhysAddrs[pagenum - pb->BaseOffset] );
138 MM_RefPhys( pb->PhysAddrs[pagenum - pb->BaseOffset] );
139 LOG("Cached map %X to %p (%P)", pagenum*PAGE_SIZE, mapping_dest,
140 pb->PhysAddrs[pagenum - pb->BaseOffset]);
142 h->Node->ReferenceCount ++;
145 if( !(Protection & MMAP_PROT_WRITE) ) {
146 MM_SetFlags(mapping_dest, MM_PFLAG_RO, MM_PFLAG_RO);
149 MM_SetFlags(mapping_dest, 0, MM_PFLAG_RO);
152 if( Protection & MMAP_PROT_EXEC ) {
153 MM_SetFlags(mapping_dest, MM_PFLAG_EXEC, MM_PFLAG_EXEC);
156 MM_SetFlags(mapping_dest, 0, MM_PFLAG_EXEC);
161 LOG("Flag update on %p", mapping_dest);
162 if( (MM_GetFlags(mapping_dest) & MM_PFLAG_RO) && (Protection & MMAP_PROT_WRITE) )
164 MM_SetFlags(mapping_dest, 0, MM_PFLAG_RO);
167 if( Flags & MMAP_MAP_PRIVATE )
168 MM_SetFlags(mapping_dest, MM_PFLAG_COW, MM_PFLAG_COW);
172 // Roll on to next block if needed
173 if(pagenum - pb->BaseOffset == MMAP_PAGES_PER_BLOCK)
175 if( pb->Next && pb->Next->BaseOffset == pagenum )
179 tVFS_MMapPageBlock *oldpb = pb;
180 pb = malloc( sizeof(tVFS_MMapPageBlock) );
181 pb->Next = oldpb->Next;
182 pb->BaseOffset = pagenum;
183 memset(pb->PhysAddrs, 0, sizeof(pb->PhysAddrs));
189 Mutex_Release( &h->Node->Lock );
191 LEAVE('p', mapping_base);
192 return (void*)mapping_base;
195 void *VFS_MMap_Anon(void *Destination, size_t Length, Uint FlagsSet, Uint FlagsMask)
197 size_t ofs = (tVAddr)Destination & (PAGE_SIZE-1);
198 tPage *mapping_dest = (void*)( (char*)Destination - ofs );
202 size_t bytes = MIN(PAGE_SIZE - ofs, Length);
204 // Allocate a partial page
205 if( MM_GetPhysAddr(mapping_dest) )
207 // Already allocated page, clear the area we're touching
208 ASSERT( ofs + bytes <= PAGE_SIZE );
210 // TODO: Double check that this area isn't already zero
211 memset( Destination, 0, bytes );
213 MM_SetFlags(mapping_dest, FlagsSet, FlagsMask);
215 LOG("#1: Clear %i from %p", Length, Destination);
219 MM_AllocateZero(mapping_dest);
220 LOG("#1: Allocate for %p", Destination);
225 while( Length >= PAGE_SIZE )
227 if( MM_GetPhysAddr( mapping_dest ) )
229 // We're allocating entire pages here, so free this page and replace with a COW zero
230 MM_Deallocate(mapping_dest);
231 LOG("Replace %p with zero page", mapping_dest);
235 LOG("Allocate zero at %p", mapping_dest);
237 MM_AllocateZero(mapping_dest);
244 ASSERT(Length < PAGE_SIZE);
247 if( MM_GetPhysAddr(mapping_dest) )
249 // TODO: Don't touch page if already zero
250 memset( mapping_dest, 0, Length );
251 LOG("Clear %i in %p", Length, mapping_dest);
255 MM_AllocateZero(mapping_dest);
256 LOG("Anon map to %p", mapping_dest);
263 int VFS_MUnmap(void *Addr, size_t Length)