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

Last change on this file since 2264 was 2264, checked in by sandervl, 26 years ago

Implemented SEC_COMMIT flag for memory maps

File size: 23.9 KB
Line 
1/* $Id: virtual.cpp,v 1.26 1999-12-30 11:19:54 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
31ODINDEBUGCHANNEL(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 */
44HANDLE 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 */
60HANDLE 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 */
80HANDLE 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 */
94HANDLE 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 */
111LPVOID 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 */
132LPVOID 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 */
153BOOL 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 */
186BOOL 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 */
214HANDLE WINAPI VIRTUAL_MapFileW( LPCWSTR name , LPVOID *lpMapping, BOOL fReadIntoMemory)
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 | ((fReadIntoMemory) ? SEC_COMMIT : 0), 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 */
239HANDLE WINAPI VIRTUAL_MapFileA( LPCSTR name , LPVOID *lpMapping, BOOL fReadIntoMemory)
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 | ((fReadIntoMemory) ? SEC_COMMIT : 0), 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//******************************************************************************
259ODINFUNCTION4(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, accessflags = 0;
314
315 nrpages = cbSize >> PAGE_SHIFT;
316 if(cbSize & 0xFFF)
317 nrpages++;
318
319 if(flag & PAG_READ) {
320 accessflags |= MEMMAP_ACCESS_READ;
321 }
322 if(flag & PAG_WRITE) {
323 accessflags |= MEMMAP_ACCESS_WRITE;
324 }
325 if(flag & PAG_EXECUTE) {
326 accessflags |= MEMMAP_ACCESS_EXECUTE;
327 }
328 map = Win32MemMapView::findMapByView((ULONG)lpvAddress, &offset, accessflags);
329 if(map) {
330 //TODO: We don't allow protection flag changes for mmaped files now
331 map->commitPage(offset, FALSE, nrpages);
332 return lpvAddress;
333 }
334 }
335
336 // commit memory
337 if(fdwAllocationType & MEM_COMMIT)
338 {
339 Address = lpvAddress;
340
341 rc = OSLibDosSetMem(lpvAddress, cbSize, flag);
342
343 //might try to commit larger part with same base address
344 if(rc == OSLIB_ERROR_ACCESS_DENIED && cbSize > 4096 )
345 { //knut: AND more than one page
346 char *newbase = (char *)lpvAddress + ((cbSize-1) & 0xFFFFF000); //knut: lets not start after the last page!
347 ULONG size, os2flags;
348
349 while(newbase >= (char *)lpvAddress)
350 { //knut: should check first page to!!
351 size = 4096;
352 os2flags = 0;
353 rc = OSLibDosQueryMem(newbase, &size, &os2flags);
354 if(rc)
355 break;
356
357 if(os2flags & PAG_COMMIT)
358 {
359 newbase += 4096;
360 break;
361 }
362 newbase -= 4096;
363 }
364
365 if(rc == 0)
366 {
367 //In case it wants to commit bytes that fall into the last
368 //page of the previous commit command
369 if(cbSize > ((int)newbase - (int)lpvAddress))
370 rc = OSLibDosSetMem(newbase, cbSize - ((int)newbase - (int)lpvAddress), flag);
371 }
372 else return(NULL);
373
374 }
375 else
376 {
377 if(rc == OSLIB_ERROR_INVALID_ADDRESS) {
378 rc = OSLibDosAllocMem(&Address, cbSize, flag );
379 }
380 else
381 if(rc) dprintf(("Unexpected DosSetMem error %x", rc));
382 }
383 }
384 else
385 {
386 rc = OSLibDosAllocMem(&Address, cbSize, flag);
387 }
388
389 if(rc)
390 {
391 dprintf(("DosSetMem returned %d\n", rc));
392 SetLastError( ERROR_OUTOFMEMORY );
393 return(NULL);
394 }
395
396 dprintf(("VirtualAlloc returned %X\n", Address));
397 return(Address);
398}
399//******************************************************************************
400//******************************************************************************
401ODINFUNCTION3(BOOL, VirtualFree, LPVOID, lpvAddress,
402 DWORD, cbSize,
403 DWORD, FreeType)
404{
405 DWORD rc;
406
407 // verify parameters
408 if ( (FreeType & MEM_RELEASE) && (cbSize != 0) )
409 {
410 SetLastError(ERROR_INVALID_PARAMETER);
411 return(FALSE);
412 }
413
414 if ( (FreeType & MEM_DECOMMIT) &&
415 (FreeType & MEM_RELEASE) )
416 {
417 SetLastError(ERROR_INVALID_PARAMETER);
418 return(FALSE);
419 }
420
421 // decommit memory
422 if (FreeType & MEM_DECOMMIT)
423 {
424 // decommit memory block
425 rc = OSLibDosSetMem(lpvAddress, cbSize, PAG_DECOMMIT);
426 if(rc)
427 {
428 dprintf(("KERNEL32:VirtualFree:OsLibSetMem rc = #%d\n",
429 rc));
430 SetLastError(ERROR_INVALID_ADDRESS);
431 return(FALSE);
432 }
433 }
434
435 // release memory
436 if (FreeType & MEM_RELEASE)
437 {
438 rc = OSLibDosFreeMem(lpvAddress); // free the memory block
439 if(rc)
440 {
441 dprintf(("KERNEL32:VirtualFree:OsLibFreeMem rc = #%d\n",
442 rc));
443 SetLastError(ERROR_INVALID_ADDRESS);
444 return(FALSE);
445 }
446 }
447
448 return(TRUE);
449}
450//******************************************************************************
451//LPVOID lpvAddress; /* address of region of committed pages */
452//DWORD cbSize; /* size of the region */
453//DWORD fdwNewProtect; /* desired access protection */
454//PDWORD pfdwOldProtect; /* address of variable to get old protection */
455//TODO: Not 100% complete
456//TODO: SetLastError on failure
457//******************************************************************************
458
459ODINFUNCTION4(BOOL, VirtualProtect, LPVOID, lpvAddress,
460 DWORD, cbSize,
461 DWORD, fdwNewProtect,
462 DWORD*, pfdwOldProtect)
463{
464 DWORD rc;
465 DWORD cb = cbSize;
466 ULONG pageFlags = 0;
467 int npages;
468
469 if(pfdwOldProtect == NULL)
470 return(FALSE);
471
472 rc = OSLibDosQueryMem(lpvAddress, &cb, &pageFlags);
473 if(rc) {
474 dprintf(("DosQueryMem returned %d\n", rc));
475 return(FALSE);
476 }
477 dprintf(("Old memory flags %X\n", pageFlags));
478 *pfdwOldProtect = 0;
479 if(pageFlags & PAG_READ && !(pageFlags & PAG_WRITE))
480 *pfdwOldProtect |= PAGE_READONLY;
481 if(pageFlags & (PAG_WRITE))
482 *pfdwOldProtect |= PAGE_READWRITE;
483
484 if((pageFlags & (PAG_WRITE | PAG_EXECUTE)) == (PAG_WRITE | PAG_EXECUTE))
485 *pfdwOldProtect |= PAGE_EXECUTE_READWRITE;
486 else
487 if(pageFlags & PAG_EXECUTE)
488 *pfdwOldProtect |= PAGE_EXECUTE_READ;
489
490 if(pageFlags & PAG_GUARD)
491 *pfdwOldProtect |= PAGE_GUARD;
492 pageFlags = 0;
493
494 if(fdwNewProtect & PAGE_READONLY) pageFlags |= PAG_READ;
495 if(fdwNewProtect & PAGE_READWRITE) pageFlags |= (PAG_READ | PAG_WRITE);
496 if(fdwNewProtect & PAGE_WRITECOPY) pageFlags |= (PAG_READ | PAG_WRITE);
497 if(fdwNewProtect & PAGE_EXECUTE_READ) pageFlags |= (PAG_EXECUTE | PAG_READ);
498 if(fdwNewProtect & PAGE_EXECUTE_READWRITE)
499 pageFlags |= (PAG_EXECUTE | PAG_WRITE | PAG_READ);
500 if(fdwNewProtect & PAGE_EXECUTE_WRITECOPY)
501 pageFlags |= (PAG_EXECUTE | PAG_WRITE | PAG_READ);
502 if(fdwNewProtect & PAGE_GUARD) pageFlags |= PAG_GUARD;
503//Not supported in OS/2??
504// if(fdwNewProtect & PAGE_NOACCESS)
505
506 dprintf(("New memory flags %X\n", pageFlags));
507 if(pageFlags == 0) {
508 dprintf(("pageFlags == 0\n"));
509 return(TRUE); //nothing to do
510 }
511 ULONG offset = ((ULONG)lpvAddress & 0xFFF);
512 npages = (cbSize >> 12);
513
514 cb = (cbSize & 0xFFF) + offset; // !!! added, some optimization :)
515 if( cb > 0 ) { // changed
516 npages++;
517 }
518 if( cb > 4096 ) { // changed, note '>' sign ( not '>=' ) 4096 is exactly one page
519 npages++;
520 }
521
522 lpvAddress = (LPVOID)((int)lpvAddress & ~0xFFF);
523 cbSize = npages*4096;
524 dprintf(("lpvAddress = %X, cbSize = %d\n", lpvAddress, cbSize));
525
526 rc = OSLibDosSetMem(lpvAddress, cbSize, pageFlags);
527 if(rc) {
528 dprintf(("DosSetMem returned %d\n", rc));
529 return(FALSE);
530 }
531 return(TRUE);
532}
533//******************************************************************************
534//******************************************************************************
535ODINFUNCTION3(DWORD, VirtualQuery, LPCVOID, lpvAddress,
536 LPMEMORY_BASIC_INFORMATION, pmbiBuffer,
537 DWORD, cbLength)
538{
539 ULONG cbRangeSize,
540 dAttr;
541 DWORD rc;
542 LPVOID lpBase;
543
544 if(pmbiBuffer == NULL || cbLength == 0) // check parameters
545 {
546 return 0; // nothing to return
547 }
548
549 // determine exact page range
550 lpBase = (LPVOID)((ULONG)lpvAddress & 0xFFFFF000);
551 cbRangeSize = cbLength & ~0x00000FFF; // assuming intel page sizes :)
552 if(cbLength & 0x00000FFF)
553 cbRangeSize += PAGE_SIZE;
554
555 rc = OSLibDosQueryMem(lpBase,
556 &cbRangeSize,
557 &dAttr);
558 if(rc)
559 {
560 dprintf(("VirtualQuery - OSLibDosQueryMem %x %x returned %d\n",
561 lpBase,
562 cbLength,
563 rc));
564 return 0;
565 }
566
567 memset(pmbiBuffer,
568 0,
569 sizeof(MEMORY_BASIC_INFORMATION));
570
571 pmbiBuffer->BaseAddress = lpBase;
572 pmbiBuffer->RegionSize = cbRangeSize;
573
574 if(dAttr & PAG_READ && !(dAttr & PAG_WRITE))
575 pmbiBuffer->Protect |= PAGE_READONLY;
576
577 if(dAttr & PAG_WRITE)
578 pmbiBuffer->Protect |= PAGE_READWRITE;
579
580 if((dAttr & (PAG_WRITE | PAG_EXECUTE)) == (PAG_WRITE | PAG_EXECUTE))
581 pmbiBuffer->Protect |= PAGE_EXECUTE_READWRITE;
582 else
583 if(dAttr & PAG_EXECUTE)
584 pmbiBuffer->Protect |= PAGE_EXECUTE_READ;
585
586 if(dAttr & PAG_GUARD)
587 pmbiBuffer->Protect |= PAGE_GUARD;
588
589 if(dAttr & PAG_FREE)
590 pmbiBuffer->State = MEM_FREE;
591 else
592 if(dAttr & PAG_COMMIT)
593 pmbiBuffer->State = MEM_COMMIT;
594 else
595 pmbiBuffer->State = MEM_RESERVE;
596
597 if(!(dAttr & PAG_SHARED))
598 pmbiBuffer->Type = MEM_PRIVATE;
599
600 //TODO: This is not correct: AllocationProtect should contain the protection
601 // flags used in the initial call to VirtualAlloc
602 pmbiBuffer->AllocationProtect = pmbiBuffer->Protect;
603 if(dAttr & PAG_BASE)
604 pmbiBuffer->AllocationBase = lpBase;
605 else
606 {
607 while(lpBase > 0)
608 {
609 rc = OSLibDosQueryMem(lpBase, &cbRangeSize, &dAttr);
610 if(rc)
611 {
612 dprintf(("VirtualQuery - OSLibDosQueryMem %x %x returned %d\n",
613 lpBase,
614 cbLength,
615 rc));
616 break;
617 }
618 if(dAttr & PAG_BASE)
619 {
620 pmbiBuffer->AllocationBase = lpBase;
621 break;
622 }
623 lpBase = (LPVOID)((ULONG)lpBase - PAGE_SIZE);
624 }
625 }
626 return sizeof(MEMORY_BASIC_INFORMATION);
627}
628//******************************************************************************
629//******************************************************************************
630ODINFUNCTION2(BOOL, VirtualLock, LPVOID, lpAddress,
631 DWORD, dwSize)
632{
633 dprintf(("VirtualLock at %d; %d bytes - stub (TRUE)\n", (int)lpAddress, dwSize));
634 return TRUE;
635}
636
637//******************************************************************************
638ODINFUNCTION2(BOOL, VirtualUnlock, LPVOID, lpAddress,
639 DWORD, dwSize)
640{
641 dprintf(("VirtualUnlock at %d; %d bytes - stub (TRUE)\n", (int)lpAddress, dwSize));
642 return TRUE;
643}
644
645/*****************************************************************************
646 * Name : BOOL VirtualProtectEx
647 * Purpose : The VirtualProtectEx function changes the access protection on
648 * a region of committed pages in the virtual address space of a specified
649 * process. Note that this function differs from VirtualProtect,
650 * which changes the access protection on the calling process only.
651 * Parameters: HANDLE hProcess handle of process
652 * LPVOID lpvAddress address of region of committed pages
653 * DWORD cbSize size of region
654 * DWORD fdwNewProtect desired access protection
655 * PDWORD pfdwOldProtect address of variable to get old protection
656 * Variables :
657 * Result : size of target buffer
658 * Remark :
659 * Status : UNTESTED STUB
660 *
661 * Author : Patrick Haller [Mon, 1998/06/15 08:00]
662 *****************************************************************************/
663
664ODINFUNCTION5(BOOL, VirtualProtectEx, HANDLE, hProcess,
665 LPVOID, lpvAddress,
666 DWORD, cbSize,
667 DWORD, fdwNewProtect,
668 LPDWORD, pfdwOldProtect)
669{
670 // only execute API, if this is the current process !
671 if (GetCurrentProcess() == hProcess)
672 return VirtualProtect(lpvAddress, cbSize, fdwNewProtect, pfdwOldProtect);
673 else
674 {
675 SetLastError(ERROR_ACCESS_DENIED); // deny access to other processes
676 return FALSE;
677 }
678}
679
680
681/*****************************************************************************
682 * Name : DWORD VirtualQueryEx
683 * Purpose : The VirtualQueryEx function provides information about a range
684 * of pages within the virtual address space of a specified process.
685 * Parameters: HANDLE hProcess handle of process
686 * LPCVOID lpvAddress address of region
687 * LPMEMORY_BASIC_INFORMATION pmbiBuffer address of information buffer
688 * DWORD cbLength size of buffer
689 * Variables :
690 * Result : number of bytes returned in buffer
691 * Remark :
692 * Status : UNTESTED STUB
693 *
694 * Author : Patrick Haller [Mon, 1998/06/15 08:00]
695 *****************************************************************************/
696
697ODINFUNCTION4(DWORD, VirtualQueryEx, HANDLE, hProcess,
698 LPCVOID, lpvAddress,
699 LPMEMORY_BASIC_INFORMATION, pmbiBuffer,
700 DWORD, cbLength)
701{
702 // only execute API, if this is the current process !
703 if (GetCurrentProcess() == hProcess)
704 return VirtualQuery(lpvAddress, pmbiBuffer, cbLength);
705 else
706 {
707 SetLastError(ERROR_ACCESS_DENIED); // deny access to other processes
708 return FALSE;
709 }
710}
711
712//******************************************************************************
713//SvL: Private api
714//******************************************************************************
715LPVOID VirtualAllocShared(DWORD cbSize, DWORD fdwAllocationType,
716 DWORD fdwProtect, LPSTR name)
717{
718 LPVOID Address;
719 ULONG flag = 0, base;
720 DWORD rc;
721
722 dprintf(("VirtualAllocShared: %x %x %x %s", cbSize, fdwAllocationType, fdwProtect, name));
723
724 if (cbSize > 0x7fc00000) /* 2Gb - 4Mb */
725 {
726 dprintf(("VirtualAllocShared: size too large"));
727 SetLastError( ERROR_OUTOFMEMORY );
728 return NULL;
729 }
730
731 if (!(fdwAllocationType & (MEM_COMMIT | MEM_RESERVE)) ||
732 (fdwAllocationType & ~(MEM_COMMIT | MEM_RESERVE)))
733 {
734 dprintf(("VirtualAllocShared: Invalid parameter"));
735 SetLastError( ERROR_INVALID_PARAMETER );
736 return NULL;
737 }
738
739 if(fdwAllocationType & MEM_COMMIT)
740 {
741 dprintf(("VirtualAllocShared: commit\n"));
742 flag = PAG_COMMIT;
743 }
744
745 if(fdwProtect & PAGE_READONLY) flag |= PAG_READ;
746 if(fdwProtect & PAGE_NOACCESS) flag |= PAG_READ; //can't do this in OS/2
747 if(fdwProtect & PAGE_READWRITE) flag |= (PAG_READ | PAG_WRITE);
748 if(fdwProtect & PAGE_WRITECOPY) flag |= (PAG_READ | PAG_WRITE);
749
750 if(fdwProtect & PAGE_EXECUTE_READWRITE) flag |= (PAG_EXECUTE | PAG_WRITE | PAG_READ);
751 if(fdwProtect & PAGE_EXECUTE_READ) flag |= (PAG_EXECUTE | PAG_READ);
752 if(fdwProtect & PAGE_EXECUTE) flag |= PAG_EXECUTE;
753
754 if(fdwProtect & PAGE_GUARD) {
755 dprintf(("ERROR: PAGE_GUARD bit set for VirtualAllocShared -> we don't support this right now!"));
756 flag |= PAG_GUARD;
757 }
758
759 //just do this if other options are used
760 if(!(flag & (PAG_READ | PAG_WRITE | PAG_EXECUTE)) || flag == 0)
761 {
762 dprintf(("VirtualAllocShared: Unknown protection flags, default to read/write"));
763 flag |= PAG_READ | PAG_WRITE;
764 }
765
766 rc = OSLibDosAllocSharedMem(&Address, cbSize, flag, name);
767
768 if(rc)
769 {
770 dprintf(("DosAllocSharedMem returned %d\n", rc));
771 SetLastError( ERROR_OUTOFMEMORY );
772 return(NULL);
773 }
774
775 dprintf(("VirtualAllocShared returned %X\n", Address));
776 return(Address);
777}
Note: See TracBrowser for help on using the repository browser.