source: trunk/src/kernel32/virtual.cpp@ 4583

Last change on this file since 4583 was 4583, checked in by sandervl, 25 years ago

MN: VirtualQuery fix

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