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

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

Rewrite of PE loader code, EB's fixes + VirtualProtect bugfix

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