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)
29 tVAddr mapping_dest, mapping_base;
31 tVFS_MMapPageBlock *pb, *prev;
33 ENTER("pDestHint iLength xProtection xFlags xFD XOffset", DestHint, Length, Protection, Flags, FD, Offset);
35 if( Flags & MMAP_MAP_ANONYMOUS )
36 Offset = (tVAddr)DestHint & 0xFFF;
38 npages = ((Offset & (PAGE_SIZE-1)) + Length + (PAGE_SIZE - 1)) / PAGE_SIZE;
39 pagenum = Offset / PAGE_SIZE;
41 mapping_base = (tVAddr)DestHint;
42 mapping_dest = mapping_base & ~(PAGE_SIZE-1);
44 // TODO: Locate space for the allocation
46 // Handle anonymous mappings
47 if( Flags & MMAP_MAP_ANONYMOUS )
50 LOG("%i pages anonymous to %p", npages, mapping_dest);
51 for( ; npages --; mapping_dest += PAGE_SIZE, ofs += PAGE_SIZE )
53 if( MM_GetPhysAddr((void*)mapping_dest) ) {
54 // TODO: Set flags to COW if needed (well, if shared)
55 MM_SetFlags(mapping_dest, MM_PFLAG_COW, MM_PFLAG_COW);
56 LOG("clear from %p, %i bytes", (void*)(mapping_base + ofs),
57 PAGE_SIZE - (mapping_base & (PAGE_SIZE-1))
59 memset( (void*)(mapping_base + ofs), 0, PAGE_SIZE - (mapping_base & (PAGE_SIZE-1)));
62 LOG("New empty page");
63 // TODO: Map a COW zero page instead
64 if( !MM_Allocate(mapping_dest) ) {
66 Log_Warning("VFS", "VFS_MMap: Anon alloc to %p failed", mapping_dest);
68 memset((void*)mapping_dest, 0, PAGE_SIZE);
69 LOG("Anon map to %p", mapping_dest);
72 LEAVE_RET('p', (void*)mapping_base);
75 h = VFS_GetHandle(FD);
76 if( !h || !h->Node ) LEAVE_RET('n', NULL);
80 Mutex_Acquire( &h->Node->Lock );
82 // Search for existing mapping for each page
83 // - Sorted list of 16 page blocks
85 pb = h->Node->MMapInfo, prev = NULL;
86 pb && pb->BaseOffset + MMAP_PAGES_PER_BLOCK < pagenum;
87 prev = pb, pb = pb->Next
90 LOG("pb = %p, pb->BaseOffset = %X", pb, pb ? pb->BaseOffset : 0);
92 // - Allocate a block if needed
93 if( !pb || pb->BaseOffset > pagenum )
96 pb = calloc( 1, sizeof(tVFS_MMapPageBlock) );
98 Mutex_Release( &h->Node->Lock );
102 pb->BaseOffset = pagenum - pagenum % MMAP_PAGES_PER_BLOCK;
106 h->Node->MMapInfo = pb;
109 // - Map (and allocate) pages
112 if( MM_GetPhysAddr( (void*)mapping_dest ) == 0 )
114 if( pb->PhysAddrs[pagenum - pb->BaseOffset] == 0 )
116 tVFS_NodeType *nt = h->Node->Type;
122 nt->MMap(h->Node, pagenum*PAGE_SIZE, PAGE_SIZE, (void*)mapping_dest);
126 // Allocate pages and read data
127 if( MM_Allocate(mapping_dest) == 0 ) {
129 Mutex_Release( &h->Node->Lock );
133 // TODO: Clip read length
134 read_len = nt->Read(h->Node, pagenum*PAGE_SIZE, PAGE_SIZE, (void*)mapping_dest);
135 // TODO: This was commented out, why?
136 if( read_len != PAGE_SIZE ) {
137 memset( (void*)(mapping_dest+read_len), 0, PAGE_SIZE-read_len );
140 pb->PhysAddrs[pagenum - pb->BaseOffset] = MM_GetPhysAddr( (void*)mapping_dest );
141 MM_SetPageNode( pb->PhysAddrs[pagenum - pb->BaseOffset], h->Node );
142 MM_RefPhys( pb->PhysAddrs[pagenum - pb->BaseOffset] );
143 LOG("Read and map %X to %p (%P)", pagenum*PAGE_SIZE, mapping_dest,
144 pb->PhysAddrs[pagenum - pb->BaseOffset]);
148 MM_Map( mapping_dest, pb->PhysAddrs[pagenum - pb->BaseOffset] );
149 MM_RefPhys( pb->PhysAddrs[pagenum - pb->BaseOffset] );
150 LOG("Cached map %X to %p (%P)", pagenum*PAGE_SIZE, mapping_dest,
151 pb->PhysAddrs[pagenum - pb->BaseOffset]);
153 h->Node->ReferenceCount ++;
156 if( !(Protection & MMAP_PROT_WRITE) ) {
157 MM_SetFlags(mapping_dest, MM_PFLAG_RO, MM_PFLAG_RO);
160 MM_SetFlags(mapping_dest, 0, MM_PFLAG_RO);
163 if( Protection & MMAP_PROT_EXEC ) {
164 MM_SetFlags(mapping_dest, MM_PFLAG_EXEC, MM_PFLAG_EXEC);
167 MM_SetFlags(mapping_dest, 0, MM_PFLAG_EXEC);
172 LOG("Flag update on %p", mapping_dest);
173 if( (MM_GetFlags(mapping_dest) & MM_PFLAG_RO) && (Protection & MMAP_PROT_WRITE) )
175 MM_SetFlags(mapping_dest, 0, MM_PFLAG_RO);
178 if( Flags & MMAP_MAP_PRIVATE )
179 MM_SetFlags(mapping_dest, MM_PFLAG_COW, MM_PFLAG_COW);
181 mapping_dest += PAGE_SIZE;
183 // Roll on to next block if needed
184 if(pagenum - pb->BaseOffset == MMAP_PAGES_PER_BLOCK)
186 if( pb->Next && pb->Next->BaseOffset == pagenum )
190 tVFS_MMapPageBlock *oldpb = pb;
191 pb = malloc( sizeof(tVFS_MMapPageBlock) );
192 pb->Next = oldpb->Next;
193 pb->BaseOffset = pagenum;
194 memset(pb->PhysAddrs, 0, sizeof(pb->PhysAddrs));
200 Mutex_Release( &h->Node->Lock );
202 LEAVE('p', mapping_base);
203 return (void*)mapping_base;
206 int VFS_MUnmap(void *Addr, size_t Length)