1 | /* $Id: virtual.cpp,v 1.22 1999-11-10 14:16:01 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Win32 virtual memory functions
|
---|
5 | *
|
---|
6 | * Copyright 1998-1999 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
7 | * Copyright 1998 Knut St. Osmundsen
|
---|
8 | * Copyright 1998 Peter FitzSimmons
|
---|
9 | *
|
---|
10 | * Parts (VIRTUAL_MapFileA/W) based on Wine code (memory\virtual.c):
|
---|
11 | *
|
---|
12 | * Copyright 1997 Alexandre Julliard
|
---|
13 | *
|
---|
14 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include <odin.h>
|
---|
19 | #include <odinwrap.h>
|
---|
20 |
|
---|
21 | #include <os2win.h>
|
---|
22 | #include <stdlib.h>
|
---|
23 | #include <string.h>
|
---|
24 | #include <win\virtual.h>
|
---|
25 | #include <heapstring.h>
|
---|
26 | #include <handlemanager.h>
|
---|
27 | #include "mmap.h"
|
---|
28 | #include "oslibdos.h"
|
---|
29 |
|
---|
30 |
|
---|
31 | ODINDEBUGCHANNEL(KERNEL32-VIRTUAL)
|
---|
32 |
|
---|
33 | #define PAGE_SHIFT 12
|
---|
34 |
|
---|
35 | /***********************************************************************
|
---|
36 | * CreateFileMapping32A (KERNEL32.46)
|
---|
37 | * Creates a named or unnamed file-mapping object for the specified file
|
---|
38 | *
|
---|
39 | * RETURNS
|
---|
40 | * Handle: Success
|
---|
41 | * 0: Mapping object does not exist
|
---|
42 | * NULL: Failure
|
---|
43 | */
|
---|
44 | HANDLE WINAPI CreateFileMappingA(
|
---|
45 | HFILE hFile, /* [in] Handle of file to map */
|
---|
46 | SECURITY_ATTRIBUTES *sa, /* [in] Optional security attributes*/
|
---|
47 | DWORD protect, /* [in] Protection for mapping object */
|
---|
48 | DWORD size_high, /* [in] High-order 32 bits of object size */
|
---|
49 | DWORD size_low, /* [in] Low-order 32 bits of object size */
|
---|
50 | LPCSTR name /* [in] Name of file-mapping object */ )
|
---|
51 | {
|
---|
52 | return HMCreateFileMapping(hFile, sa, protect, size_high, size_low, name);
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | /***********************************************************************
|
---|
57 | * CreateFileMapping32W (KERNEL32.47)
|
---|
58 | * See CreateFileMapping32A
|
---|
59 | */
|
---|
60 | HANDLE WINAPI CreateFileMappingW( HFILE hFile, LPSECURITY_ATTRIBUTES attr,
|
---|
61 | DWORD protect, DWORD size_high,
|
---|
62 | DWORD size_low, LPCWSTR name )
|
---|
63 | {
|
---|
64 | LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
|
---|
65 | HANDLE ret = CreateFileMappingA( hFile, attr, protect,
|
---|
66 | size_high, size_low, nameA );
|
---|
67 | HeapFree( GetProcessHeap(), 0, nameA );
|
---|
68 | return ret;
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | /***********************************************************************
|
---|
73 | * OpenFileMapping32A (KERNEL32.397)
|
---|
74 | * Opens a named file-mapping object.
|
---|
75 | *
|
---|
76 | * RETURNS
|
---|
77 | * Handle: Success
|
---|
78 | * NULL: Failure
|
---|
79 | */
|
---|
80 | HANDLE WINAPI OpenFileMappingA(
|
---|
81 | DWORD access, /* [in] Access mode */
|
---|
82 | BOOL inherit, /* [in] Inherit flag */
|
---|
83 | LPCSTR name ) /* [in] Name of file-mapping object */
|
---|
84 | {
|
---|
85 | dprintf(("OpenFileMappingA: %x %d %s", access, inherit, name));
|
---|
86 | return HMOpenFileMapping(access, inherit, name);
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | /***********************************************************************
|
---|
91 | * OpenFileMapping32W (KERNEL32.398)
|
---|
92 | * See OpenFileMapping32A
|
---|
93 | */
|
---|
94 | HANDLE WINAPI OpenFileMappingW( DWORD access, BOOL inherit, LPCWSTR name)
|
---|
95 | {
|
---|
96 | LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
|
---|
97 | HANDLE ret = OpenFileMappingA( access, inherit, nameA );
|
---|
98 | HeapFree( GetProcessHeap(), 0, nameA );
|
---|
99 | return ret;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | /***********************************************************************
|
---|
104 | * MapViewOfFile (KERNEL32.385)
|
---|
105 | * Maps a view of a file into the address space
|
---|
106 | *
|
---|
107 | * RETURNS
|
---|
108 | * Starting address of mapped view
|
---|
109 | * NULL: Failure
|
---|
110 | */
|
---|
111 | LPVOID WINAPI MapViewOfFile(
|
---|
112 | HANDLE mapping, /* [in] File-mapping object to map */
|
---|
113 | DWORD access, /* [in] Access mode */
|
---|
114 | DWORD offset_high, /* [in] High-order 32 bits of file offset */
|
---|
115 | DWORD offset_low, /* [in] Low-order 32 bits of file offset */
|
---|
116 | DWORD count /* [in] Number of bytes to map */
|
---|
117 | )
|
---|
118 | {
|
---|
119 | return MapViewOfFileEx( mapping, access, offset_high,
|
---|
120 | offset_low, count, NULL );
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | /***********************************************************************
|
---|
125 | * MapViewOfFileEx (KERNEL32.386)
|
---|
126 | * Maps a view of a file into the address space
|
---|
127 | *
|
---|
128 | * RETURNS
|
---|
129 | * Starting address of mapped view
|
---|
130 | * NULL: Failure
|
---|
131 | */
|
---|
132 | LPVOID WINAPI MapViewOfFileEx(
|
---|
133 | HANDLE handle, /* [in] File-mapping object to map */
|
---|
134 | DWORD access, /* [in] Access mode */
|
---|
135 | DWORD offset_high, /* [in] High-order 32 bits of file offset */
|
---|
136 | DWORD offset_low, /* [in] Low-order 32 bits of file offset */
|
---|
137 | DWORD count, /* [in] Number of bytes to map */
|
---|
138 | LPVOID addr /* [in] Suggested starting address for mapped view */
|
---|
139 | )
|
---|
140 | {
|
---|
141 | return HMMapViewOfFileEx(handle, access, offset_high, offset_low, count, addr);
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | /***********************************************************************
|
---|
146 | * FlushViewOfFile (KERNEL32.262)
|
---|
147 | * Writes to the disk a byte range within a mapped view of a file
|
---|
148 | *
|
---|
149 | * RETURNS
|
---|
150 | * TRUE: Success
|
---|
151 | * FALSE: Failure
|
---|
152 | */
|
---|
153 | BOOL WINAPI FlushViewOfFile(
|
---|
154 | LPCVOID base, /* [in] Start address of byte range to flush */
|
---|
155 | DWORD cbFlush /* [in] Number of bytes in range */
|
---|
156 | )
|
---|
157 | {
|
---|
158 | Win32MemMap *map;
|
---|
159 | DWORD offset;
|
---|
160 |
|
---|
161 | if (!base)
|
---|
162 | {
|
---|
163 | SetLastError( ERROR_INVALID_PARAMETER );
|
---|
164 | return FALSE;
|
---|
165 | }
|
---|
166 | map = Win32MemMapView::findMapByView((ULONG)base, &offset, MEMMAP_ACCESS_READ);
|
---|
167 | if(map == NULL) {
|
---|
168 | SetLastError( ERROR_FILE_NOT_FOUND );
|
---|
169 | return FALSE;
|
---|
170 | }
|
---|
171 | return map->flushView(offset, cbFlush);
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | /***********************************************************************
|
---|
176 | * UnmapViewOfFile (KERNEL32.540)
|
---|
177 | * Unmaps a mapped view of a file.
|
---|
178 | *
|
---|
179 | * NOTES
|
---|
180 | * Should addr be an LPCVOID?
|
---|
181 | *
|
---|
182 | * RETURNS
|
---|
183 | * TRUE: Success
|
---|
184 | * FALSE: Failure
|
---|
185 | */
|
---|
186 | BOOL WINAPI UnmapViewOfFile(LPVOID addr /* [in] Address where mapped view begins */
|
---|
187 | )
|
---|
188 | {
|
---|
189 | Win32MemMap *map;
|
---|
190 | Win32MemMapView *view;
|
---|
191 |
|
---|
192 | DWORD offset;
|
---|
193 |
|
---|
194 | if (!addr)
|
---|
195 | {
|
---|
196 | SetLastError( ERROR_INVALID_PARAMETER );
|
---|
197 | return FALSE;
|
---|
198 | }
|
---|
199 | map = Win32MemMapView::findMapByView((ULONG)addr, &offset, MEMMAP_ACCESS_READ, &view);
|
---|
200 | if(map == NULL) {
|
---|
201 | SetLastError( ERROR_FILE_NOT_FOUND );
|
---|
202 | return FALSE;
|
---|
203 | }
|
---|
204 | return map->unmapViewOfFile(view);
|
---|
205 | }
|
---|
206 |
|
---|
207 | /***********************************************************************
|
---|
208 | * VIRTUAL_MapFileW
|
---|
209 | *
|
---|
210 | * Helper function to map a file to memory:
|
---|
211 | * name - file name
|
---|
212 | * [RETURN] ptr - pointer to mapped file
|
---|
213 | */
|
---|
214 | HANDLE WINAPI VIRTUAL_MapFileW( LPCWSTR name , LPVOID *lpMapping)
|
---|
215 | {
|
---|
216 | HANDLE hFile, hMapping = -1;
|
---|
217 |
|
---|
218 | hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
|
---|
219 | OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0);
|
---|
220 | if (hFile != INVALID_HANDLE_VALUE)
|
---|
221 | {
|
---|
222 | hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
|
---|
223 | CloseHandle( hFile );
|
---|
224 | if (hMapping != INVALID_HANDLE_VALUE)
|
---|
225 | {
|
---|
226 | *lpMapping = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
|
---|
227 | }
|
---|
228 | }
|
---|
229 | return hMapping;
|
---|
230 | }
|
---|
231 |
|
---|
232 | /***********************************************************************
|
---|
233 | * VIRTUAL_MapFileA
|
---|
234 | *
|
---|
235 | * Helper function to map a file to memory:
|
---|
236 | * name - file name
|
---|
237 | * [RETURN] ptr - pointer to mapped file
|
---|
238 | */
|
---|
239 | HANDLE WINAPI VIRTUAL_MapFileA( LPCSTR name , LPVOID *lpMapping)
|
---|
240 | {
|
---|
241 | HANDLE hFile, hMapping = -1;
|
---|
242 |
|
---|
243 | hFile = CreateFileA(name, GENERIC_READ, FILE_SHARE_READ, NULL,
|
---|
244 | OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0);
|
---|
245 | if (hFile != INVALID_HANDLE_VALUE)
|
---|
246 | {
|
---|
247 | hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
|
---|
248 | CloseHandle( hFile );
|
---|
249 | if (hMapping != INVALID_HANDLE_VALUE)
|
---|
250 | {
|
---|
251 | *lpMapping = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
|
---|
252 | }
|
---|
253 | }
|
---|
254 | return hMapping;
|
---|
255 | }
|
---|
256 |
|
---|
257 | //******************************************************************************
|
---|
258 | //******************************************************************************
|
---|
259 | ODINFUNCTION4(LPVOID, VirtualAlloc, LPVOID, lpvAddress,
|
---|
260 | DWORD, cbSize,
|
---|
261 | DWORD, fdwAllocationType,
|
---|
262 | DWORD, fdwProtect)
|
---|
263 | {
|
---|
264 | PVOID Address = lpvAddress;
|
---|
265 | ULONG flag = 0, base;
|
---|
266 | DWORD rc;
|
---|
267 |
|
---|
268 | if (cbSize > 0x7fc00000) /* 2Gb - 4Mb */
|
---|
269 | {
|
---|
270 | dprintf(("VirtualAlloc: size too large"));
|
---|
271 | SetLastError( ERROR_OUTOFMEMORY );
|
---|
272 | return NULL;
|
---|
273 | }
|
---|
274 |
|
---|
275 | if (!(fdwAllocationType & (MEM_COMMIT | MEM_RESERVE)) ||
|
---|
276 | (fdwAllocationType & ~(MEM_COMMIT | MEM_RESERVE)))
|
---|
277 | {
|
---|
278 | dprintf(("VirtualAlloc: Invalid parameter"));
|
---|
279 | SetLastError( ERROR_INVALID_PARAMETER );
|
---|
280 | return NULL;
|
---|
281 | }
|
---|
282 |
|
---|
283 | if(fdwAllocationType & MEM_COMMIT)
|
---|
284 | {
|
---|
285 | dprintf(("VirtualAlloc: commit\n"));
|
---|
286 | flag = PAG_COMMIT;
|
---|
287 | }
|
---|
288 |
|
---|
289 | if(fdwProtect & PAGE_READONLY) flag |= PAG_READ;
|
---|
290 | if(fdwProtect & PAGE_NOACCESS) flag |= PAG_READ; //can't do this in OS/2
|
---|
291 | if(fdwProtect & PAGE_READWRITE) flag |= (PAG_READ | PAG_WRITE);
|
---|
292 | if(fdwProtect & PAGE_WRITECOPY) flag |= (PAG_READ | PAG_WRITE);
|
---|
293 |
|
---|
294 | if(fdwProtect & PAGE_EXECUTE_READWRITE) flag |= (PAG_EXECUTE | PAG_WRITE | PAG_READ);
|
---|
295 | if(fdwProtect & PAGE_EXECUTE_READ) flag |= (PAG_EXECUTE | PAG_READ);
|
---|
296 | if(fdwProtect & PAGE_EXECUTE) flag |= PAG_EXECUTE;
|
---|
297 |
|
---|
298 | if(fdwProtect & PAGE_GUARD) {
|
---|
299 | dprintf(("ERROR: PAGE_GUARD bit set for VirtualAlloc -> we don't support this right now!"));
|
---|
300 | flag |= PAG_GUARD;
|
---|
301 | }
|
---|
302 |
|
---|
303 | //just do this if other options are used
|
---|
304 | if(!(flag & (PAG_READ | PAG_WRITE | PAG_EXECUTE)) || flag == 0)
|
---|
305 | {
|
---|
306 | dprintf(("VirtualAlloc: Unknown protection flags, default to read/write"));
|
---|
307 | flag |= PAG_READ | PAG_WRITE;
|
---|
308 | }
|
---|
309 |
|
---|
310 | if(lpvAddress)
|
---|
311 | {
|
---|
312 | Win32MemMap *map;
|
---|
313 | ULONG offset, nrpages;
|
---|
314 |
|
---|
315 | nrpages = cbSize >> PAGE_SHIFT;
|
---|
316 | if(cbSize & 0xFFF)
|
---|
317 | nrpages++;
|
---|
318 |
|
---|
319 | map = Win32MemMapView::findMapByView((ULONG)lpvAddress, &offset, fdwProtect);
|
---|
320 | if(map) {
|
---|
321 | //TODO: We don't allow protection flag changes for mmaped files now
|
---|
322 | map->commitPage(offset, FALSE, nrpages);
|
---|
323 | return lpvAddress;
|
---|
324 | }
|
---|
325 | }
|
---|
326 |
|
---|
327 | // commit memory
|
---|
328 | if(fdwAllocationType & MEM_COMMIT)
|
---|
329 | {
|
---|
330 | Address = lpvAddress;
|
---|
331 |
|
---|
332 | rc = OSLibDosSetMem(lpvAddress, cbSize, flag);
|
---|
333 |
|
---|
334 | //might try to commit larger part with same base address
|
---|
335 | if(rc == OSLIB_ERROR_ACCESS_DENIED && cbSize > 4096 )
|
---|
336 | { //knut: AND more than one page
|
---|
337 | char *newbase = (char *)lpvAddress + ((cbSize-1) & 0xFFFFF000); //knut: lets not start after the last page!
|
---|
338 | ULONG size, os2flags;
|
---|
339 |
|
---|
340 | while(newbase >= (char *)lpvAddress)
|
---|
341 | { //knut: should check first page to!!
|
---|
342 | size = 4096;
|
---|
343 | os2flags = 0;
|
---|
344 | rc = OSLibDosQueryMem(newbase, &size, &os2flags);
|
---|
345 | if(rc)
|
---|
346 | break;
|
---|
347 |
|
---|
348 | if(os2flags & PAG_COMMIT)
|
---|
349 | {
|
---|
350 | newbase += 4096;
|
---|
351 | break;
|
---|
352 | }
|
---|
353 | newbase -= 4096;
|
---|
354 | }
|
---|
355 |
|
---|
356 | if(rc == 0)
|
---|
357 | {
|
---|
358 | //In case it wants to commit bytes that fall into the last
|
---|
359 | //page of the previous commit command
|
---|
360 | if(cbSize > ((int)newbase - (int)lpvAddress))
|
---|
361 | rc = OSLibDosSetMem(newbase, cbSize - ((int)newbase - (int)lpvAddress), flag);
|
---|
362 | }
|
---|
363 | else return(NULL);
|
---|
364 |
|
---|
365 | }
|
---|
366 | else
|
---|
367 | {
|
---|
368 | if(rc == OSLIB_ERROR_INVALID_ADDRESS) {
|
---|
369 | rc = OSLibDosAllocMem(&Address, cbSize, flag );
|
---|
370 | }
|
---|
371 | else
|
---|
372 | if(rc) dprintf(("Unexpected DosSetMem error %x", rc));
|
---|
373 | }
|
---|
374 | }
|
---|
375 | else
|
---|
376 | {
|
---|
377 | rc = OSLibDosAllocMem(&Address, cbSize, flag);
|
---|
378 | }
|
---|
379 |
|
---|
380 | if(rc)
|
---|
381 | {
|
---|
382 | dprintf(("DosSetMem returned %d\n", rc));
|
---|
383 | SetLastError( ERROR_OUTOFMEMORY );
|
---|
384 | return(NULL);
|
---|
385 | }
|
---|
386 |
|
---|
387 | dprintf(("VirtualAlloc returned %X\n", Address));
|
---|
388 | return(Address);
|
---|
389 | }
|
---|
390 | //******************************************************************************
|
---|
391 | //******************************************************************************
|
---|
392 | ODINFUNCTION3(BOOL, VirtualFree, LPVOID, lpvAddress,
|
---|
393 | DWORD, cbSize,
|
---|
394 | DWORD, FreeType)
|
---|
395 | {
|
---|
396 | DWORD rc;
|
---|
397 |
|
---|
398 | // verify parameters
|
---|
399 | if ( (FreeType & MEM_RELEASE) && (cbSize != 0) )
|
---|
400 | {
|
---|
401 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
402 | return(FALSE);
|
---|
403 | }
|
---|
404 |
|
---|
405 | if ( (FreeType & MEM_DECOMMIT) &&
|
---|
406 | (FreeType & MEM_RELEASE) )
|
---|
407 | {
|
---|
408 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
409 | return(FALSE);
|
---|
410 | }
|
---|
411 |
|
---|
412 | // decommit memory
|
---|
413 | if (FreeType & MEM_DECOMMIT)
|
---|
414 | {
|
---|
415 | // decommit memory block
|
---|
416 | rc = OSLibDosSetMem(lpvAddress, cbSize, PAG_DECOMMIT);
|
---|
417 | if(rc)
|
---|
418 | {
|
---|
419 | dprintf(("KERNEL32:VirtualFree:OsLibSetMem rc = #%d\n",
|
---|
420 | rc));
|
---|
421 | SetLastError(ERROR_INVALID_ADDRESS);
|
---|
422 | return(FALSE);
|
---|
423 | }
|
---|
424 | }
|
---|
425 |
|
---|
426 | // release memory
|
---|
427 | if (FreeType & MEM_RELEASE)
|
---|
428 | {
|
---|
429 | rc = OSLibDosFreeMem(lpvAddress); // free the memory block
|
---|
430 | if(rc)
|
---|
431 | {
|
---|
432 | dprintf(("KERNEL32:VirtualFree:OsLibFreeMem rc = #%d\n",
|
---|
433 | rc));
|
---|
434 | SetLastError(ERROR_INVALID_ADDRESS);
|
---|
435 | return(FALSE);
|
---|
436 | }
|
---|
437 | }
|
---|
438 |
|
---|
439 | return(TRUE);
|
---|
440 | }
|
---|
441 | //******************************************************************************
|
---|
442 | //LPVOID lpvAddress; /* address of region of committed pages */
|
---|
443 | //DWORD cbSize; /* size of the region */
|
---|
444 | //DWORD fdwNewProtect; /* desired access protection */
|
---|
445 | //PDWORD pfdwOldProtect; /* address of variable to get old protection */
|
---|
446 | //TODO: Not 100% complete
|
---|
447 | //TODO: SetLastError on failure
|
---|
448 | //******************************************************************************
|
---|
449 |
|
---|
450 | ODINFUNCTION4(BOOL, VirtualProtect, LPVOID, lpvAddress,
|
---|
451 | DWORD, cbSize,
|
---|
452 | DWORD, fdwNewProtect,
|
---|
453 | DWORD*, pfdwOldProtect)
|
---|
454 | {
|
---|
455 | DWORD rc;
|
---|
456 | ULONG pageFlags = 0;
|
---|
457 | int npages;
|
---|
458 |
|
---|
459 | if(pfdwOldProtect == NULL)
|
---|
460 | return(FALSE);
|
---|
461 |
|
---|
462 | rc = OSLibDosQueryMem(lpvAddress, &cbSize, &pageFlags);
|
---|
463 | if(rc) {
|
---|
464 | dprintf(("DosQueryMem returned %d\n", rc));
|
---|
465 | return(FALSE);
|
---|
466 | }
|
---|
467 | dprintf(("Old memory flags %X\n", pageFlags));
|
---|
468 | *pfdwOldProtect = 0;
|
---|
469 | if(pageFlags & PAG_READ && !(pageFlags & PAG_WRITE))
|
---|
470 | *pfdwOldProtect |= PAGE_READONLY;
|
---|
471 | if(pageFlags & (PAG_WRITE))
|
---|
472 | *pfdwOldProtect |= PAGE_READWRITE;
|
---|
473 |
|
---|
474 | if((pageFlags & (PAG_WRITE | PAG_EXECUTE)) == (PAG_WRITE | PAG_EXECUTE))
|
---|
475 | *pfdwOldProtect |= PAGE_EXECUTE_READWRITE;
|
---|
476 | else
|
---|
477 | if(pageFlags & PAG_EXECUTE)
|
---|
478 | *pfdwOldProtect |= PAGE_EXECUTE_READ;
|
---|
479 |
|
---|
480 | if(pageFlags & PAG_GUARD)
|
---|
481 | *pfdwOldProtect |= PAGE_GUARD;
|
---|
482 | pageFlags = 0;
|
---|
483 |
|
---|
484 | if(fdwNewProtect & PAGE_READONLY) pageFlags |= PAG_READ;
|
---|
485 | if(fdwNewProtect & PAGE_READWRITE) pageFlags |= (PAG_READ | PAG_WRITE);
|
---|
486 | if(fdwNewProtect & PAGE_WRITECOPY) pageFlags |= (PAG_READ | PAG_WRITE);
|
---|
487 | if(fdwNewProtect & PAGE_EXECUTE_READ) pageFlags |= (PAG_EXECUTE | PAG_READ);
|
---|
488 | if(fdwNewProtect & PAGE_EXECUTE_READWRITE)
|
---|
489 | pageFlags |= (PAG_EXECUTE | PAG_WRITE | PAG_READ);
|
---|
490 | if(fdwNewProtect & PAGE_EXECUTE_WRITECOPY)
|
---|
491 | pageFlags |= (PAG_EXECUTE | PAG_WRITE | PAG_READ);
|
---|
492 | if(fdwNewProtect & PAGE_GUARD) pageFlags |= PAG_GUARD;
|
---|
493 | //Not supported in OS/2??
|
---|
494 | // if(fdwNewProtect & PAGE_NOACCESS)
|
---|
495 |
|
---|
496 | dprintf(("New memory flags %X\n", pageFlags));
|
---|
497 | if(pageFlags == 0) {
|
---|
498 | dprintf(("pageFlags == 0\n"));
|
---|
499 | return(TRUE); //nothing to do
|
---|
500 | }
|
---|
501 | ULONG offset = ((ULONG)lpvAddress & 0xFFF);
|
---|
502 | npages = (cbSize >> 12);
|
---|
503 | if(cbSize & 0xFFF + offset) {
|
---|
504 | npages++;
|
---|
505 | }
|
---|
506 |
|
---|
507 | lpvAddress = (LPVOID)((int)lpvAddress & ~0xFFF);
|
---|
508 | cbSize = npages*4096;
|
---|
509 | dprintf(("lpvAddress = %X, cbSize = %d\n", lpvAddress, cbSize));
|
---|
510 |
|
---|
511 | rc = OSLibDosSetMem(lpvAddress, cbSize, pageFlags);
|
---|
512 | if(rc) {
|
---|
513 | dprintf(("DosSetMem returned %d\n", rc));
|
---|
514 | return(FALSE);
|
---|
515 | }
|
---|
516 | return(TRUE);
|
---|
517 | }
|
---|
518 | //******************************************************************************
|
---|
519 | //******************************************************************************
|
---|
520 | ODINFUNCTION3(DWORD, VirtualQuery, LPCVOID, lpvAddress,
|
---|
521 | LPMEMORY_BASIC_INFORMATION, pmbiBuffer,
|
---|
522 | DWORD, cbLength)
|
---|
523 | {
|
---|
524 | ULONG cbRangeSize,
|
---|
525 | dAttr;
|
---|
526 | DWORD rc;
|
---|
527 | LPVOID lpBase;
|
---|
528 |
|
---|
529 | if(pmbiBuffer == NULL || cbLength == 0) // check parameters
|
---|
530 | {
|
---|
531 | return 0; // nothing to return
|
---|
532 | }
|
---|
533 |
|
---|
534 | // determine exact page range
|
---|
535 | lpBase = (LPVOID)((ULONG)lpvAddress & 0xFFFFF000);
|
---|
536 | cbRangeSize = cbLength & ~0x00000FFF; // assuming intel page sizes :)
|
---|
537 | if(cbLength & 0x00000FFF)
|
---|
538 | cbRangeSize += PAGE_SIZE;
|
---|
539 |
|
---|
540 | rc = OSLibDosQueryMem(lpBase,
|
---|
541 | &cbRangeSize,
|
---|
542 | &dAttr);
|
---|
543 | if(rc)
|
---|
544 | {
|
---|
545 | dprintf(("VirtualQuery - OSLibDosQueryMem %x %x returned %d\n",
|
---|
546 | lpBase,
|
---|
547 | cbLength,
|
---|
548 | rc));
|
---|
549 | return 0;
|
---|
550 | }
|
---|
551 |
|
---|
552 | memset(pmbiBuffer,
|
---|
553 | 0,
|
---|
554 | sizeof(MEMORY_BASIC_INFORMATION));
|
---|
555 |
|
---|
556 | pmbiBuffer->BaseAddress = lpBase;
|
---|
557 | pmbiBuffer->RegionSize = cbRangeSize;
|
---|
558 |
|
---|
559 | if(dAttr & PAG_READ && !(dAttr & PAG_WRITE))
|
---|
560 | pmbiBuffer->Protect |= PAGE_READONLY;
|
---|
561 |
|
---|
562 | if(dAttr & PAG_WRITE)
|
---|
563 | pmbiBuffer->Protect |= PAGE_READWRITE;
|
---|
564 |
|
---|
565 | if((dAttr & (PAG_WRITE | PAG_EXECUTE)) == (PAG_WRITE | PAG_EXECUTE))
|
---|
566 | pmbiBuffer->Protect |= PAGE_EXECUTE_READWRITE;
|
---|
567 | else
|
---|
568 | if(dAttr & PAG_EXECUTE)
|
---|
569 | pmbiBuffer->Protect |= PAGE_EXECUTE_READ;
|
---|
570 |
|
---|
571 | if(dAttr & PAG_GUARD)
|
---|
572 | pmbiBuffer->Protect |= PAGE_GUARD;
|
---|
573 |
|
---|
574 | if(dAttr & PAG_FREE)
|
---|
575 | pmbiBuffer->State = MEM_FREE;
|
---|
576 | else
|
---|
577 | if(dAttr & PAG_COMMIT)
|
---|
578 | pmbiBuffer->State = MEM_COMMIT;
|
---|
579 | else
|
---|
580 | pmbiBuffer->State = MEM_RESERVE;
|
---|
581 |
|
---|
582 | if(!(dAttr & PAG_SHARED))
|
---|
583 | pmbiBuffer->Type = MEM_PRIVATE;
|
---|
584 |
|
---|
585 | //TODO: This is not correct: AllocationProtect should contain the protection
|
---|
586 | // flags used in the initial call to VirtualAlloc
|
---|
587 | pmbiBuffer->AllocationProtect = pmbiBuffer->Protect;
|
---|
588 | if(dAttr & PAG_BASE)
|
---|
589 | pmbiBuffer->AllocationBase = lpBase;
|
---|
590 | else
|
---|
591 | {
|
---|
592 | while(lpBase > 0)
|
---|
593 | {
|
---|
594 | rc = OSLibDosQueryMem(lpBase, &cbRangeSize, &dAttr);
|
---|
595 | if(rc)
|
---|
596 | {
|
---|
597 | dprintf(("VirtualQuery - OSLibDosQueryMem %x %x returned %d\n",
|
---|
598 | lpBase,
|
---|
599 | cbLength,
|
---|
600 | rc));
|
---|
601 | break;
|
---|
602 | }
|
---|
603 | if(dAttr & PAG_BASE)
|
---|
604 | {
|
---|
605 | pmbiBuffer->AllocationBase = lpBase;
|
---|
606 | break;
|
---|
607 | }
|
---|
608 | lpBase = (LPVOID)((ULONG)lpBase - PAGE_SIZE);
|
---|
609 | }
|
---|
610 | }
|
---|
611 | return sizeof(MEMORY_BASIC_INFORMATION);
|
---|
612 | }
|
---|
613 | //******************************************************************************
|
---|
614 | //******************************************************************************
|
---|
615 | ODINFUNCTION2(BOOL, VirtualLock, LPVOID, lpAddress,
|
---|
616 | DWORD, dwSize)
|
---|
617 | {
|
---|
618 | dprintf(("VirtualLock at %d; %d bytes - stub (TRUE)\n", (int)lpAddress, dwSize));
|
---|
619 | return TRUE;
|
---|
620 | }
|
---|
621 |
|
---|
622 | //******************************************************************************
|
---|
623 | ODINFUNCTION2(BOOL, VirtualUnlock, LPVOID, lpAddress,
|
---|
624 | DWORD, dwSize)
|
---|
625 | {
|
---|
626 | dprintf(("VirtualUnlock at %d; %d bytes - stub (TRUE)\n", (int)lpAddress, dwSize));
|
---|
627 | return TRUE;
|
---|
628 | }
|
---|
629 |
|
---|
630 | /*****************************************************************************
|
---|
631 | * Name : BOOL VirtualProtectEx
|
---|
632 | * Purpose : The VirtualProtectEx function changes the access protection on
|
---|
633 | * a region of committed pages in the virtual address space of a specified
|
---|
634 | * process. Note that this function differs from VirtualProtect,
|
---|
635 | * which changes the access protection on the calling process only.
|
---|
636 | * Parameters: HANDLE hProcess handle of process
|
---|
637 | * LPVOID lpvAddress address of region of committed pages
|
---|
638 | * DWORD cbSize size of region
|
---|
639 | * DWORD fdwNewProtect desired access protection
|
---|
640 | * PDWORD pfdwOldProtect address of variable to get old protection
|
---|
641 | * Variables :
|
---|
642 | * Result : size of target buffer
|
---|
643 | * Remark :
|
---|
644 | * Status : UNTESTED STUB
|
---|
645 | *
|
---|
646 | * Author : Patrick Haller [Mon, 1998/06/15 08:00]
|
---|
647 | *****************************************************************************/
|
---|
648 |
|
---|
649 | ODINFUNCTION5(BOOL, VirtualProtectEx, HANDLE, hProcess,
|
---|
650 | LPVOID, lpvAddress,
|
---|
651 | DWORD, cbSize,
|
---|
652 | DWORD, fdwNewProtect,
|
---|
653 | LPDWORD, pfdwOldProtect)
|
---|
654 | {
|
---|
655 | // only execute API, if this is the current process !
|
---|
656 | if (GetCurrentProcess() == hProcess)
|
---|
657 | return VirtualProtect(lpvAddress, cbSize, fdwNewProtect, pfdwOldProtect);
|
---|
658 | else
|
---|
659 | {
|
---|
660 | SetLastError(ERROR_ACCESS_DENIED); // deny access to other processes
|
---|
661 | return FALSE;
|
---|
662 | }
|
---|
663 | }
|
---|
664 |
|
---|
665 |
|
---|
666 | /*****************************************************************************
|
---|
667 | * Name : DWORD VirtualQueryEx
|
---|
668 | * Purpose : The VirtualQueryEx function provides information about a range
|
---|
669 | * of pages within the virtual address space of a specified process.
|
---|
670 | * Parameters: HANDLE hProcess handle of process
|
---|
671 | * LPCVOID lpvAddress address of region
|
---|
672 | * LPMEMORY_BASIC_INFORMATION pmbiBuffer address of information buffer
|
---|
673 | * DWORD cbLength size of buffer
|
---|
674 | * Variables :
|
---|
675 | * Result : number of bytes returned in buffer
|
---|
676 | * Remark :
|
---|
677 | * Status : UNTESTED STUB
|
---|
678 | *
|
---|
679 | * Author : Patrick Haller [Mon, 1998/06/15 08:00]
|
---|
680 | *****************************************************************************/
|
---|
681 |
|
---|
682 | ODINFUNCTION4(DWORD, VirtualQueryEx, HANDLE, hProcess,
|
---|
683 | LPCVOID, lpvAddress,
|
---|
684 | LPMEMORY_BASIC_INFORMATION, pmbiBuffer,
|
---|
685 | DWORD, cbLength)
|
---|
686 | {
|
---|
687 | // only execute API, if this is the current process !
|
---|
688 | if (GetCurrentProcess() == hProcess)
|
---|
689 | return VirtualQuery(lpvAddress, pmbiBuffer, cbLength);
|
---|
690 | else
|
---|
691 | {
|
---|
692 | SetLastError(ERROR_ACCESS_DENIED); // deny access to other processes
|
---|
693 | return FALSE;
|
---|
694 | }
|
---|
695 | }
|
---|
696 |
|
---|
697 | //******************************************************************************
|
---|
698 | //SvL: Private api
|
---|
699 | //******************************************************************************
|
---|
700 | LPVOID VirtualAllocShared(DWORD cbSize, DWORD fdwAllocationType,
|
---|
701 | DWORD fdwProtect, LPSTR name)
|
---|
702 | {
|
---|
703 | LPVOID Address;
|
---|
704 | ULONG flag = 0, base;
|
---|
705 | DWORD rc;
|
---|
706 |
|
---|
707 | dprintf(("VirtualAllocShared: %x %x %x %s", cbSize, fdwAllocationType, fdwProtect, name));
|
---|
708 |
|
---|
709 | if (cbSize > 0x7fc00000) /* 2Gb - 4Mb */
|
---|
710 | {
|
---|
711 | dprintf(("VirtualAllocShared: size too large"));
|
---|
712 | SetLastError( ERROR_OUTOFMEMORY );
|
---|
713 | return NULL;
|
---|
714 | }
|
---|
715 |
|
---|
716 | if (!(fdwAllocationType & (MEM_COMMIT | MEM_RESERVE)) ||
|
---|
717 | (fdwAllocationType & ~(MEM_COMMIT | MEM_RESERVE)))
|
---|
718 | {
|
---|
719 | dprintf(("VirtualAllocShared: Invalid parameter"));
|
---|
720 | SetLastError( ERROR_INVALID_PARAMETER );
|
---|
721 | return NULL;
|
---|
722 | }
|
---|
723 |
|
---|
724 | if(fdwAllocationType & MEM_COMMIT)
|
---|
725 | {
|
---|
726 | dprintf(("VirtualAllocShared: commit\n"));
|
---|
727 | flag = PAG_COMMIT;
|
---|
728 | }
|
---|
729 |
|
---|
730 | if(fdwProtect & PAGE_READONLY) flag |= PAG_READ;
|
---|
731 | if(fdwProtect & PAGE_NOACCESS) flag |= PAG_READ; //can't do this in OS/2
|
---|
732 | if(fdwProtect & PAGE_READWRITE) flag |= (PAG_READ | PAG_WRITE);
|
---|
733 | if(fdwProtect & PAGE_WRITECOPY) flag |= (PAG_READ | PAG_WRITE);
|
---|
734 |
|
---|
735 | if(fdwProtect & PAGE_EXECUTE_READWRITE) flag |= (PAG_EXECUTE | PAG_WRITE | PAG_READ);
|
---|
736 | if(fdwProtect & PAGE_EXECUTE_READ) flag |= (PAG_EXECUTE | PAG_READ);
|
---|
737 | if(fdwProtect & PAGE_EXECUTE) flag |= PAG_EXECUTE;
|
---|
738 |
|
---|
739 | if(fdwProtect & PAGE_GUARD) {
|
---|
740 | dprintf(("ERROR: PAGE_GUARD bit set for VirtualAllocShared -> we don't support this right now!"));
|
---|
741 | flag |= PAG_GUARD;
|
---|
742 | }
|
---|
743 |
|
---|
744 | //just do this if other options are used
|
---|
745 | if(!(flag & (PAG_READ | PAG_WRITE | PAG_EXECUTE)) || flag == 0)
|
---|
746 | {
|
---|
747 | dprintf(("VirtualAllocShared: Unknown protection flags, default to read/write"));
|
---|
748 | flag |= PAG_READ | PAG_WRITE;
|
---|
749 | }
|
---|
750 |
|
---|
751 | rc = OSLibDosAllocSharedMem(&Address, cbSize, flag, name);
|
---|
752 |
|
---|
753 | if(rc)
|
---|
754 | {
|
---|
755 | dprintf(("DosAllocSharedMem returned %d\n", rc));
|
---|
756 | SetLastError( ERROR_OUTOFMEMORY );
|
---|
757 | return(NULL);
|
---|
758 | }
|
---|
759 |
|
---|
760 | dprintf(("VirtualAllocShared returned %X\n", Address));
|
---|
761 | return(Address);
|
---|
762 | }
|
---|