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

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

RegSetValue workaround + HMOpenFile fix

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