3 * - Open, Close and ChDir
11 #define MMAP_PAGES_PER_BLOCK 16
14 typedef struct sVFS_MMapPageBlock tVFS_MMapPageBlock;
15 struct sVFS_MMapPageBlock
17 tVFS_MMapPageBlock *Next;
18 Uint64 BaseOffset; // Must be a multiple of MMAP_PAGES_PER_BLOCK*PAGE_SIZE
19 tPAddr PhysAddrs[MMAP_PAGES_PER_BLOCK];
23 void *VFS_MMap(void *DestHint, size_t Length, int Protection, int Flags, int FD, Uint64 Offset)
26 tVAddr mapping_dest, mapping_base;
28 tVFS_MMapPageBlock *pb, *prev;
30 ENTER("pDestHint iLength xProtection xFlags xFD XOffset", DestHint, Length, Protection, Flags, FD, Offset);
32 npages = ((Offset & (PAGE_SIZE-1)) + Length + (PAGE_SIZE - 1)) / PAGE_SIZE;
33 pagenum = Offset / PAGE_SIZE;
35 mapping_base = (tVAddr)DestHint;
36 mapping_dest = mapping_base & ~(PAGE_SIZE-1);
38 // TODO: Locate space for the allocation
40 // Handle anonymous mappings
41 if( Flags & MMAP_MAP_ANONYMOUS )
44 for( ; npages --; mapping_dest += PAGE_SIZE, ofs += PAGE_SIZE )
46 if( MM_GetPhysAddr(mapping_dest) ) {
47 // TODO: Set flags to COW if needed (well, if shared)
48 MM_SetFlags(mapping_dest, MM_PFLAG_COW, MM_PFLAG_COW);
49 memset( (void*)(mapping_base + ofs), 0, PAGE_SIZE - (mapping_base & (PAGE_SIZE-1)));
52 // TODO: Map a COW zero page instead
53 if( !MM_Allocate(mapping_dest) ) {
55 Log_Warning("VFS", "VFS_MMap: Anon alloc to %p failed", mapping_dest);
57 memset((void*)mapping_dest, 0, PAGE_SIZE);
58 LOG("Anon map to %p", mapping_dest);
61 LEAVE_RET('p', (void*)mapping_base);
64 h = VFS_GetHandle(FD);
65 if( !h || !h->Node ) LEAVE_RET('n', NULL);
69 Mutex_Acquire( &h->Node->Lock );
71 // Search for existing mapping for each page
72 // - Sorted list of 16 page blocks
74 pb = h->Node->MMapInfo, prev = NULL;
75 pb && pb->BaseOffset + MMAP_PAGES_PER_BLOCK < pagenum;
76 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 = malloc( sizeof(tVFS_MMapPageBlock) );
87 Mutex_Release( &h->Node->Lock );
91 pb->BaseOffset = pagenum - pagenum % MMAP_PAGES_PER_BLOCK;
92 memset(pb->PhysAddrs, 0, sizeof(pb->PhysAddrs));
96 h->Node->MMapInfo = pb;
99 // - Map (and allocate) pages
102 if( MM_GetPhysAddr(mapping_dest) == 0 )
104 if( pb->PhysAddrs[pagenum - pb->BaseOffset] == 0 )
107 h->Node->MMap(h->Node, pagenum*PAGE_SIZE, PAGE_SIZE, (void*)mapping_dest);
111 // Allocate pages and read data
112 if( MM_Allocate(mapping_dest) == 0 ) {
114 Mutex_Release( &h->Node->Lock );
118 // TODO: Clip read length
119 read_len = h->Node->Read(h->Node, pagenum*PAGE_SIZE, PAGE_SIZE, (void*)mapping_dest);
120 // if( read_len != PAGE_SIZE ) {
121 // memset( (void*)(mapping_dest+read_len), 0, PAGE_SIZE-read_len );
124 pb->PhysAddrs[pagenum - pb->BaseOffset] = MM_GetPhysAddr( mapping_dest );
125 MM_SetPageNode( pb->PhysAddrs[pagenum - pb->BaseOffset], h->Node );
126 MM_RefPhys( pb->PhysAddrs[pagenum - pb->BaseOffset] );
127 LOG("Read and map %X to %p (%P)", pagenum*PAGE_SIZE, mapping_dest,
128 pb->PhysAddrs[pagenum - pb->BaseOffset]);
132 MM_Map( mapping_dest, pb->PhysAddrs[pagenum - pb->BaseOffset] );
133 MM_RefPhys( pb->PhysAddrs[pagenum - pb->BaseOffset] );
134 LOG("Cached map %X to %p (%P)", pagenum*PAGE_SIZE, mapping_dest,
135 pb->PhysAddrs[pagenum - pb->BaseOffset]);
137 h->Node->ReferenceCount ++;
140 if( !(Protection & MMAP_PROT_WRITE) ) {
141 MM_SetFlags(mapping_dest, MM_PFLAG_RO, MM_PFLAG_RO);
144 MM_SetFlags(mapping_dest, 0, MM_PFLAG_RO);
149 LOG("Flag update on %p", mapping_dest);
150 if( (MM_GetFlags(mapping_dest) & MM_PFLAG_RO) && (Protection & MMAP_PROT_WRITE) )
152 MM_SetFlags(mapping_dest, 0, MM_PFLAG_RO);
155 if( Flags & MMAP_MAP_PRIVATE )
156 MM_SetFlags(mapping_dest, MM_PFLAG_COW, MM_PFLAG_COW);
158 mapping_dest += PAGE_SIZE;
160 // Roll on to next block if needed
161 if(pagenum - pb->BaseOffset == MMAP_PAGES_PER_BLOCK)
163 if( pb->Next && pb->Next->BaseOffset == pagenum )
167 tVFS_MMapPageBlock *oldpb = pb;
168 pb = malloc( sizeof(tVFS_MMapPageBlock) );
169 pb->Next = oldpb->Next;
170 pb->BaseOffset = pagenum;
171 memset(pb->PhysAddrs, 0, sizeof(pb->PhysAddrs));
178 Mutex_Release( &h->Node->Lock );
180 LEAVE('p', mapping_base);
181 return (void*)mapping_base;
184 int VFS_MUnmap(void *Addr, size_t Length)