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

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

Added process api + virtualprotect fix

File size: 23.6 KB
Line 
1/* $Id: virtual.cpp,v 1.24 1999-11-30 14:15:55 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)
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 */
239HANDLE 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//******************************************************************************
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;
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//******************************************************************************
392ODINFUNCTION3(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
450ODINFUNCTION4(BOOL, VirtualProtect, LPVOID, lpvAddress,
451 DWORD, cbSize,
452 DWORD, fdwNewProtect,
453 DWORD*, pfdwOldProtect)
454{
455 DWORD rc;
456 DWORD cb = cbSize;
457 ULONG pageFlags = 0;
458 int npages;
459
460 if(pfdwOldProtect == NULL)
461 return(FALSE);
462
463 rc = OSLibDosQueryMem(lpvAddress, &cb, &pageFlags);
464 if(rc) {
465 dprintf(("DosQueryMem returned %d\n", rc));
466 return(FALSE);
467 }
468 dprintf(("Old memory flags %X\n", pageFlags));
469 *pfdwOldProtect = 0;
470 if(pageFlags & PAG_READ && !(pageFlags & PAG_WRITE))
471 *pfdwOldProtect |= PAGE_READONLY;
472 if(pageFlags & (PAG_WRITE))
473 *pfdwOldProtect |= PAGE_READWRITE;
474
475 if((pageFlags & (PAG_WRITE | PAG_EXECUTE)) == (PAG_WRITE | PAG_EXECUTE))
476 *pfdwOldProtect |= PAGE_EXECUTE_READWRITE;
477 else
478 if(pageFlags & PAG_EXECUTE)
479 *pfdwOldProtect |= PAGE_EXECUTE_READ;
480
481 if(pageFlags & PAG_GUARD)
482 *pfdwOldProtect |= PAGE_GUARD;
483 pageFlags = 0;
484
485 if(fdwNewProtect & PAGE_READONLY) pageFlags |= PAG_READ;
486 if(fdwNewProtect & PAGE_READWRITE) pageFlags |= (PAG_READ | PAG_WRITE);
487 if(fdwNewProtect & PAGE_WRITECOPY) pageFlags |= (PAG_READ | PAG_WRITE);
488 if(fdwNewProtect & PAGE_EXECUTE_READ) pageFlags |= (PAG_EXECUTE | PAG_READ);
489 if(fdwNewProtect & PAGE_EXECUTE_READWRITE)
490 pageFlags |= (PAG_EXECUTE | PAG_WRITE | PAG_READ);
491 if(fdwNewProtect & PAGE_EXECUTE_WRITECOPY)
492 pageFlags |= (PAG_EXECUTE | PAG_WRITE | PAG_READ);
493 if(fdwNewProtect & PAGE_GUARD) pageFlags |= PAG_GUARD;
494//Not supported in OS/2??
495// if(fdwNewProtect & PAGE_NOACCESS)
496
497 dprintf(("New memory flags %X\n", pageFlags));
498 if(pageFlags == 0) {
499 dprintf(("pageFlags == 0\n"));
500 return(TRUE); //nothing to do
501 }
502 ULONG offset = ((ULONG)lpvAddress & 0xFFF);
503 npages = (cbSize >> 12);
504
505 cb = (cbSize & 0xFFF) + offset; // !!! added, some optimization :)
506 if( cb > 0 ) { // changed
507 npages++;
508 }
509 if( cb > 4096 ) { // changed, note '>' sign ( not '>=' ) 4096 is exactly one page
510 npages++;
511 }
512
513 lpvAddress = (LPVOID)((int)lpvAddress & ~0xFFF);
514 cbSize = npages*4096;
515 dprintf(("lpvAddress = %X, cbSize = %d\n", lpvAddress, cbSize));
516
517 rc = OSLibDosSetMem(lpvAddress, cbSize, pageFlags);
518 if(rc) {
519 dprintf(("DosSetMem returned %d\n", rc));
520 return(FALSE);
521 }
522 return(TRUE);
523}
524//******************************************************************************
525//******************************************************************************
526ODINFUNCTION3(DWORD, VirtualQuery, LPCVOID, lpvAddress,
527 LPMEMORY_BASIC_INFORMATION, pmbiBuffer,
528 DWORD, cbLength)
529{
530 ULONG cbRangeSize,
531 dAttr;
532 DWORD rc;
533 LPVOID lpBase;
534
535 if(pmbiBuffer == NULL || cbLength == 0) // check parameters
536 {
537 return 0; // nothing to return
538 }
539
540 // determine exact page range
541 lpBase = (LPVOID)((ULONG)lpvAddress & 0xFFFFF000);
542 cbRangeSize = cbLength & ~0x00000FFF; // assuming intel page sizes :)
543 if(cbLength & 0x00000FFF)
544 cbRangeSize += PAGE_SIZE;
545
546 rc = OSLibDosQueryMem(lpBase,
547 &cbRangeSize,
548 &dAttr);
549 if(rc)
550 {
551 dprintf(("VirtualQuery - OSLibDosQueryMem %x %x returned %d\n",
552 lpBase,
553 cbLength,
554 rc));
555 return 0;
556 }
557
558 memset(pmbiBuffer,
559 0,
560 sizeof(MEMORY_BASIC_INFORMATION));
561
562 pmbiBuffer->BaseAddress = lpBase;
563 pmbiBuffer->RegionSize = cbRangeSize;
564
565 if(dAttr & PAG_READ && !(dAttr & PAG_WRITE))
566 pmbiBuffer->Protect |= PAGE_READONLY;
567
568 if(dAttr & PAG_WRITE)
569 pmbiBuffer->Protect |= PAGE_READWRITE;
570
571 if((dAttr & (PAG_WRITE | PAG_EXECUTE)) == (PAG_WRITE | PAG_EXECUTE))
572 pmbiBuffer->Protect |= PAGE_EXECUTE_READWRITE;
573 else
574 if(dAttr & PAG_EXECUTE)
575 pmbiBuffer->Protect |= PAGE_EXECUTE_READ;
576
577 if(dAttr & PAG_GUARD)
578 pmbiBuffer->Protect |= PAGE_GUARD;
579
580 if(dAttr & PAG_FREE)
581 pmbiBuffer->State = MEM_FREE;
582 else
583 if(dAttr & PAG_COMMIT)
584 pmbiBuffer->State = MEM_COMMIT;
585 else
586 pmbiBuffer->State = MEM_RESERVE;
587
588 if(!(dAttr & PAG_SHARED))
589 pmbiBuffer->Type = MEM_PRIVATE;
590
591 //TODO: This is not correct: AllocationProtect should contain the protection
592 // flags used in the initial call to VirtualAlloc
593 pmbiBuffer->AllocationProtect = pmbiBuffer->Protect;
594 if(dAttr & PAG_BASE)
595 pmbiBuffer->AllocationBase = lpBase;
596 else
597 {
598 while(lpBase > 0)
599 {
600 rc = OSLibDosQueryMem(lpBase, &cbRangeSize, &dAttr);
601 if(rc)
602 {
603 dprintf(("VirtualQuery - OSLibDosQueryMem %x %x returned %d\n",
604 lpBase,
605 cbLength,
606 rc));
607 break;
608 }
609 if(dAttr & PAG_BASE)
610 {
611 pmbiBuffer->AllocationBase = lpBase;
612 break;
613 }
614 lpBase = (LPVOID)((ULONG)lpBase - PAGE_SIZE);
615 }
616 }
617 return sizeof(MEMORY_BASIC_INFORMATION);
618}
619//******************************************************************************
620//******************************************************************************
621ODINFUNCTION2(BOOL, VirtualLock, LPVOID, lpAddress,
622 DWORD, dwSize)
623{
624 dprintf(("VirtualLock at %d; %d bytes - stub (TRUE)\n", (int)lpAddress, dwSize));
625 return TRUE;
626}
627
628//******************************************************************************
629ODINFUNCTION2(BOOL, VirtualUnlock, LPVOID, lpAddress,
630 DWORD, dwSize)
631{
632 dprintf(("VirtualUnlock at %d; %d bytes - stub (TRUE)\n", (int)lpAddress, dwSize));
633 return TRUE;
634}
635
636/*****************************************************************************
637 * Name : BOOL VirtualProtectEx
638 * Purpose : The VirtualProtectEx function changes the access protection on
639 * a region of committed pages in the virtual address space of a specified
640 * process. Note that this function differs from VirtualProtect,
641 * which changes the access protection on the calling process only.
642 * Parameters: HANDLE hProcess handle of process
643 * LPVOID lpvAddress address of region of committed pages
644 * DWORD cbSize size of region
645 * DWORD fdwNewProtect desired access protection
646 * PDWORD pfdwOldProtect address of variable to get old protection
647 * Variables :
648 * Result : size of target buffer
649 * Remark :
650 * Status : UNTESTED STUB
651 *
652 * Author : Patrick Haller [Mon, 1998/06/15 08:00]
653 *****************************************************************************/
654
655ODINFUNCTION5(BOOL, VirtualProtectEx, HANDLE, hProcess,
656 LPVOID, lpvAddress,
657 DWORD, cbSize,
658 DWORD, fdwNewProtect,
659 LPDWORD, pfdwOldProtect)
660{
661 // only execute API, if this is the current process !
662 if (GetCurrentProcess() == hProcess)
663 return VirtualProtect(lpvAddress, cbSize, fdwNewProtect, pfdwOldProtect);
664 else
665 {
666 SetLastError(ERROR_ACCESS_DENIED); // deny access to other processes
667 return FALSE;
668 }
669}
670
671
672/*****************************************************************************
673 * Name : DWORD VirtualQueryEx
674 * Purpose : The VirtualQueryEx function provides information about a range
675 * of pages within the virtual address space of a specified process.
676 * Parameters: HANDLE hProcess handle of process
677 * LPCVOID lpvAddress address of region
678 * LPMEMORY_BASIC_INFORMATION pmbiBuffer address of information buffer
679 * DWORD cbLength size of buffer
680 * Variables :
681 * Result : number of bytes returned in buffer
682 * Remark :
683 * Status : UNTESTED STUB
684 *
685 * Author : Patrick Haller [Mon, 1998/06/15 08:00]
686 *****************************************************************************/
687
688ODINFUNCTION4(DWORD, VirtualQueryEx, HANDLE, hProcess,
689 LPCVOID, lpvAddress,
690 LPMEMORY_BASIC_INFORMATION, pmbiBuffer,
691 DWORD, cbLength)
692{
693 // only execute API, if this is the current process !
694 if (GetCurrentProcess() == hProcess)
695 return VirtualQuery(lpvAddress, pmbiBuffer, cbLength);
696 else
697 {
698 SetLastError(ERROR_ACCESS_DENIED); // deny access to other processes
699 return FALSE;
700 }
701}
702
703//******************************************************************************
704//SvL: Private api
705//******************************************************************************
706LPVOID VirtualAllocShared(DWORD cbSize, DWORD fdwAllocationType,
707 DWORD fdwProtect, LPSTR name)
708{
709 LPVOID Address;
710 ULONG flag = 0, base;
711 DWORD rc;
712
713 dprintf(("VirtualAllocShared: %x %x %x %s", cbSize, fdwAllocationType, fdwProtect, name));
714
715 if (cbSize > 0x7fc00000) /* 2Gb - 4Mb */
716 {
717 dprintf(("VirtualAllocShared: size too large"));
718 SetLastError( ERROR_OUTOFMEMORY );
719 return NULL;
720 }
721
722 if (!(fdwAllocationType & (MEM_COMMIT | MEM_RESERVE)) ||
723 (fdwAllocationType & ~(MEM_COMMIT | MEM_RESERVE)))
724 {
725 dprintf(("VirtualAllocShared: Invalid parameter"));
726 SetLastError( ERROR_INVALID_PARAMETER );
727 return NULL;
728 }
729
730 if(fdwAllocationType & MEM_COMMIT)
731 {
732 dprintf(("VirtualAllocShared: commit\n"));
733 flag = PAG_COMMIT;
734 }
735
736 if(fdwProtect & PAGE_READONLY) flag |= PAG_READ;
737 if(fdwProtect & PAGE_NOACCESS) flag |= PAG_READ; //can't do this in OS/2
738 if(fdwProtect & PAGE_READWRITE) flag |= (PAG_READ | PAG_WRITE);
739 if(fdwProtect & PAGE_WRITECOPY) flag |= (PAG_READ | PAG_WRITE);
740
741 if(fdwProtect & PAGE_EXECUTE_READWRITE) flag |= (PAG_EXECUTE | PAG_WRITE | PAG_READ);
742 if(fdwProtect & PAGE_EXECUTE_READ) flag |= (PAG_EXECUTE | PAG_READ);
743 if(fdwProtect & PAGE_EXECUTE) flag |= PAG_EXECUTE;
744
745 if(fdwProtect & PAGE_GUARD) {
746 dprintf(("ERROR: PAGE_GUARD bit set for VirtualAllocShared -> we don't support this right now!"));
747 flag |= PAG_GUARD;
748 }
749
750 //just do this if other options are used
751 if(!(flag & (PAG_READ | PAG_WRITE | PAG_EXECUTE)) || flag == 0)
752 {
753 dprintf(("VirtualAllocShared: Unknown protection flags, default to read/write"));
754 flag |= PAG_READ | PAG_WRITE;
755 }
756
757 rc = OSLibDosAllocSharedMem(&Address, cbSize, flag, name);
758
759 if(rc)
760 {
761 dprintf(("DosAllocSharedMem returned %d\n", rc));
762 SetLastError( ERROR_OUTOFMEMORY );
763 return(NULL);
764 }
765
766 dprintf(("VirtualAllocShared returned %X\n", Address));
767 return(Address);
768}
Note: See TracBrowser for help on using the repository browser.