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

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

matchModName bugs fixed

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