2 * Win32 virtual memory functions
4 * Copyright 1997 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
28 #include <sys/types.h>
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
41 #include "wine/exception.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
46 WINE_DECLARE_DEBUG_CHANNEL(seh);
48 static unsigned int page_size;
50 /* filter for page-fault exceptions */
51 static WINE_EXCEPTION_FILTER(page_fault)
53 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
54 return EXCEPTION_EXECUTE_HANDLER;
55 return EXCEPTION_CONTINUE_SEARCH;
59 /***********************************************************************
60 * VirtualAlloc (KERNEL32.@)
61 * Reserves or commits a region of pages in virtual address space
64 * Base address of allocated region of pages
67 LPVOID WINAPI VirtualAlloc(
68 LPVOID addr, /* [in] Address of region to reserve or commit */
69 SIZE_T size, /* [in] Size of region */
70 DWORD type, /* [in] Type of allocation */
71 DWORD protect)/* [in] Type of access protection */
73 return VirtualAllocEx( GetCurrentProcess(), addr, size, type, protect );
77 /***********************************************************************
78 * VirtualAllocEx (KERNEL32.@)
80 * Seems to be just as VirtualAlloc, but with process handle.
83 * Base address of allocated region of pages
86 LPVOID WINAPI VirtualAllocEx(
87 HANDLE hProcess, /* [in] Handle of process to do mem operation */
88 LPVOID addr, /* [in] Address of region to reserve or commit */
89 SIZE_T size, /* [in] Size of region */
90 DWORD type, /* [in] Type of allocation */
91 DWORD protect ) /* [in] Type of access protection */
96 if ((status = NtAllocateVirtualMemory( hProcess, &ret, 0, &size, type, protect )))
98 SetLastError( RtlNtStatusToDosError(status) );
105 /***********************************************************************
106 * VirtualFree (KERNEL32.@)
107 * Release or decommits a region of pages in virtual address space.
113 BOOL WINAPI VirtualFree(
114 LPVOID addr, /* [in] Address of region of committed pages */
115 SIZE_T size, /* [in] Size of region */
116 DWORD type /* [in] Type of operation */
118 return VirtualFreeEx( GetCurrentProcess(), addr, size, type );
122 /***********************************************************************
123 * VirtualFreeEx (KERNEL32.@)
124 * Release or decommits a region of pages in virtual address space.
130 BOOL WINAPI VirtualFreeEx( HANDLE process, LPVOID addr, SIZE_T size, DWORD type )
132 NTSTATUS status = NtFreeVirtualMemory( process, &addr, &size, type );
133 if (status) SetLastError( RtlNtStatusToDosError(status) );
138 /***********************************************************************
139 * VirtualLock (KERNEL32.@)
140 * Locks the specified region of virtual address space
143 * Always returns TRUE
149 BOOL WINAPI VirtualLock( LPVOID addr, /* [in] Address of first byte of range to lock */
150 SIZE_T size ) /* [in] Number of bytes in range to lock */
152 NTSTATUS status = NtLockVirtualMemory( GetCurrentProcess(), &addr, &size, 1 );
153 if (status) SetLastError( RtlNtStatusToDosError(status) );
158 /***********************************************************************
159 * VirtualUnlock (KERNEL32.@)
160 * Unlocks a range of pages in the virtual address space
163 * Always returns TRUE
169 BOOL WINAPI VirtualUnlock( LPVOID addr, /* [in] Address of first byte of range */
170 SIZE_T size ) /* [in] Number of bytes in range */
172 NTSTATUS status = NtUnlockVirtualMemory( GetCurrentProcess(), &addr, &size, 1 );
173 if (status) SetLastError( RtlNtStatusToDosError(status) );
178 /***********************************************************************
179 * VirtualProtect (KERNEL32.@)
180 * Changes the access protection on a region of committed pages
186 BOOL WINAPI VirtualProtect(
187 LPVOID addr, /* [in] Address of region of committed pages */
188 SIZE_T size, /* [in] Size of region */
189 DWORD new_prot, /* [in] Desired access protection */
190 LPDWORD old_prot /* [out] Address of variable to get old protection */
192 return VirtualProtectEx( GetCurrentProcess(), addr, size, new_prot, old_prot );
196 /***********************************************************************
197 * VirtualProtectEx (KERNEL32.@)
198 * Changes the access protection on a region of committed pages in the
199 * virtual address space of a specified process
205 BOOL WINAPI VirtualProtectEx(
206 HANDLE process, /* [in] Handle of process */
207 LPVOID addr, /* [in] Address of region of committed pages */
208 SIZE_T size, /* [in] Size of region */
209 DWORD new_prot, /* [in] Desired access protection */
210 LPDWORD old_prot /* [out] Address of variable to get old protection */ )
212 NTSTATUS status = NtProtectVirtualMemory( process, &addr, &size, new_prot, old_prot );
213 if (status) SetLastError( RtlNtStatusToDosError(status) );
218 /***********************************************************************
219 * VirtualQuery (KERNEL32.@)
220 * Provides info about a range of pages in virtual address space
223 * Number of bytes returned in information buffer
224 * or 0 if addr is >= 0xc0000000 (kernel space).
226 SIZE_T WINAPI VirtualQuery(
227 LPCVOID addr, /* [in] Address of region */
228 PMEMORY_BASIC_INFORMATION info, /* [out] Address of info buffer */
229 SIZE_T len /* [in] Size of buffer */
231 return VirtualQueryEx( GetCurrentProcess(), addr, info, len );
235 /***********************************************************************
236 * VirtualQueryEx (KERNEL32.@)
237 * Provides info about a range of pages in virtual address space of a
241 * Number of bytes returned in information buffer
243 SIZE_T WINAPI VirtualQueryEx(
244 HANDLE process, /* [in] Handle of process */
245 LPCVOID addr, /* [in] Address of region */
246 PMEMORY_BASIC_INFORMATION info, /* [out] Address of info buffer */
247 SIZE_T len /* [in] Size of buffer */ )
252 if ((status = NtQueryVirtualMemory( process, addr, MemoryBasicInformation, info, len, &ret )))
254 SetLastError( RtlNtStatusToDosError(status) );
261 /***********************************************************************
262 * CreateFileMappingA (KERNEL32.@)
263 * Creates a named or unnamed file-mapping object for the specified file
267 * 0: Mapping object does not exist
270 HANDLE WINAPI CreateFileMappingA(
271 HANDLE hFile, /* [in] Handle of file to map */
272 SECURITY_ATTRIBUTES *sa, /* [in] Optional security attributes*/
273 DWORD protect, /* [in] Protection for mapping object */
274 DWORD size_high, /* [in] High-order 32 bits of object size */
275 DWORD size_low, /* [in] Low-order 32 bits of object size */
276 LPCSTR name /* [in] Name of file-mapping object */ )
278 WCHAR buffer[MAX_PATH];
280 if (!name) return CreateFileMappingW( hFile, sa, protect, size_high, size_low, NULL );
282 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
284 SetLastError( ERROR_FILENAME_EXCED_RANGE );
287 return CreateFileMappingW( hFile, sa, protect, size_high, size_low, buffer );
291 /***********************************************************************
292 * CreateFileMappingW (KERNEL32.@)
293 * See CreateFileMappingA.
295 HANDLE WINAPI CreateFileMappingW( HANDLE hFile, LPSECURITY_ATTRIBUTES sa,
296 DWORD protect, DWORD size_high,
297 DWORD size_low, LPCWSTR name )
299 static const int sec_flags = SEC_FILE | SEC_IMAGE | SEC_RESERVE | SEC_COMMIT | SEC_NOCACHE;
302 OBJECT_ATTRIBUTES attr;
303 UNICODE_STRING nameW;
305 DWORD access, sec_type;
308 attr.Length = sizeof(attr);
309 attr.RootDirectory = 0;
310 attr.ObjectName = NULL;
311 attr.Attributes = OBJ_CASE_INSENSITIVE |
312 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
313 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
314 attr.SecurityQualityOfService = NULL;
318 RtlInitUnicodeString( &nameW, name );
319 attr.ObjectName = &nameW;
322 sec_type = protect & sec_flags;
323 protect &= ~sec_flags;
324 if (!sec_type) sec_type = SEC_COMMIT;
329 protect = PAGE_READONLY; /* Win9x compatibility */
333 access = STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ;
336 access = STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_WRITE;
339 SetLastError( ERROR_INVALID_PARAMETER );
343 if (hFile == INVALID_HANDLE_VALUE)
346 if (!size_low && !size_high)
348 SetLastError( ERROR_INVALID_PARAMETER );
353 size.u.LowPart = size_low;
354 size.u.HighPart = size_high;
356 status = NtCreateSection( &ret, access, &attr, &size, protect, sec_type, hFile );
357 SetLastError( RtlNtStatusToDosError(status) );
362 /***********************************************************************
363 * OpenFileMappingA (KERNEL32.@)
364 * Opens a named file-mapping object.
370 HANDLE WINAPI OpenFileMappingA(
371 DWORD access, /* [in] Access mode */
372 BOOL inherit, /* [in] Inherit flag */
373 LPCSTR name ) /* [in] Name of file-mapping object */
375 WCHAR buffer[MAX_PATH];
377 if (!name) return OpenFileMappingW( access, inherit, NULL );
379 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
381 SetLastError( ERROR_FILENAME_EXCED_RANGE );
384 return OpenFileMappingW( access, inherit, buffer );
388 /***********************************************************************
389 * OpenFileMappingW (KERNEL32.@)
390 * See OpenFileMappingA.
392 HANDLE WINAPI OpenFileMappingW( DWORD access, BOOL inherit, LPCWSTR name)
394 OBJECT_ATTRIBUTES attr;
395 UNICODE_STRING nameW;
401 SetLastError( ERROR_INVALID_PARAMETER );
404 attr.Length = sizeof(attr);
405 attr.RootDirectory = 0;
406 attr.ObjectName = &nameW;
407 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
408 attr.SecurityDescriptor = NULL;
409 attr.SecurityQualityOfService = NULL;
410 RtlInitUnicodeString( &nameW, name );
412 if (access == FILE_MAP_COPY) access = FILE_MAP_READ;
414 if ((status = NtOpenSection( &ret, access, &attr )))
416 SetLastError( RtlNtStatusToDosError(status) );
423 /***********************************************************************
424 * MapViewOfFile (KERNEL32.@)
425 * Maps a view of a file into the address space
428 * Starting address of mapped view
431 LPVOID WINAPI MapViewOfFile(
432 HANDLE mapping, /* [in] File-mapping object to map */
433 DWORD access, /* [in] Access mode */
434 DWORD offset_high, /* [in] High-order 32 bits of file offset */
435 DWORD offset_low, /* [in] Low-order 32 bits of file offset */
436 SIZE_T count /* [in] Number of bytes to map */
438 return MapViewOfFileEx( mapping, access, offset_high,
439 offset_low, count, NULL );
443 /***********************************************************************
444 * MapViewOfFileEx (KERNEL32.@)
445 * Maps a view of a file into the address space
448 * Starting address of mapped view
451 LPVOID WINAPI MapViewOfFileEx(
452 HANDLE handle, /* [in] File-mapping object to map */
453 DWORD access, /* [in] Access mode */
454 DWORD offset_high, /* [in] High-order 32 bits of file offset */
455 DWORD offset_low, /* [in] Low-order 32 bits of file offset */
456 SIZE_T count, /* [in] Number of bytes to map */
457 LPVOID addr /* [in] Suggested starting address for mapped view */
460 LARGE_INTEGER offset;
463 offset.u.LowPart = offset_low;
464 offset.u.HighPart = offset_high;
466 if (access & FILE_MAP_WRITE) protect = PAGE_READWRITE;
467 else if (access & FILE_MAP_READ) protect = PAGE_READONLY;
468 else if (access & FILE_MAP_COPY) protect = PAGE_WRITECOPY;
469 else protect = PAGE_NOACCESS;
471 if ((status = NtMapViewOfSection( handle, GetCurrentProcess(), &addr, 0, 0, &offset,
472 &count, ViewShare, 0, protect )))
474 SetLastError( RtlNtStatusToDosError(status) );
481 /***********************************************************************
482 * UnmapViewOfFile (KERNEL32.@)
483 * Unmaps a mapped view of a file.
486 * Should addr be an LPCVOID?
492 BOOL WINAPI UnmapViewOfFile( LPVOID addr ) /* [in] Address where mapped view begins */
494 NTSTATUS status = NtUnmapViewOfSection( GetCurrentProcess(), addr );
495 if (status) SetLastError( RtlNtStatusToDosError(status) );
500 /***********************************************************************
501 * FlushViewOfFile (KERNEL32.@)
502 * Writes to the disk a byte range within a mapped view of a file
508 BOOL WINAPI FlushViewOfFile( LPCVOID base, /* [in] Start address of byte range to flush */
509 SIZE_T size ) /* [in] Number of bytes in range */
511 NTSTATUS status = NtFlushVirtualMemory( GetCurrentProcess(), &base, &size, 0 );
514 if (status == STATUS_NOT_MAPPED_DATA) status = STATUS_SUCCESS;
515 else SetLastError( RtlNtStatusToDosError(status) );
521 /***********************************************************************
522 * IsBadReadPtr (KERNEL32.@)
524 * Check for read access on a memory block.
527 * FALSE: Process has read access to entire block
530 BOOL WINAPI IsBadReadPtr(
531 LPCVOID ptr, /* [in] Address of memory block */
532 UINT size ) /* [in] Size of block */
534 if (!size) return FALSE; /* handle 0 size case w/o reference */
535 if (!ptr) return TRUE;
537 if (!page_size) page_size = getpagesize();
540 volatile const char *p = ptr;
544 while (count > page_size)
551 dummy = p[count - 1];
555 TRACE_(seh)("%p caused page fault during read\n", ptr);
563 /***********************************************************************
564 * IsBadWritePtr (KERNEL32.@)
566 * Check for write access on a memory block.
569 * FALSE: Process has write access to entire block
572 BOOL WINAPI IsBadWritePtr(
573 LPVOID ptr, /* [in] Address of memory block */
574 UINT size ) /* [in] Size of block in bytes */
576 if (!size) return FALSE; /* handle 0 size case w/o reference */
577 if (!ptr) return TRUE;
579 if (!page_size) page_size = getpagesize();
582 volatile char *p = ptr;
585 while (count > page_size)
596 TRACE_(seh)("%p caused page fault during write\n", ptr);
604 /***********************************************************************
605 * IsBadHugeReadPtr (KERNEL32.@)
607 * Check for read access on a memory block.
610 * FALSE: Process has read access to entire block
613 BOOL WINAPI IsBadHugeReadPtr(
614 LPCVOID ptr, /* [in] Address of memory block */
615 UINT size /* [in] Size of block */
617 return IsBadReadPtr( ptr, size );
621 /***********************************************************************
622 * IsBadHugeWritePtr (KERNEL32.@)
624 * Check for write access on a memory block.
627 * FALSE: Process has write access to entire block
630 BOOL WINAPI IsBadHugeWritePtr(
631 LPVOID ptr, /* [in] Address of memory block */
632 UINT size /* [in] Size of block */
634 return IsBadWritePtr( ptr, size );
638 /***********************************************************************
639 * IsBadCodePtr (KERNEL32.@)
641 * Check for read access on a memory address.
644 * FALSE: Process has read access to specified memory
647 BOOL WINAPI IsBadCodePtr( FARPROC ptr ) /* [in] Address of function */
649 return IsBadReadPtr( ptr, 1 );
653 /***********************************************************************
654 * IsBadStringPtrA (KERNEL32.@)
656 * Check for read access on a range of memory pointed to by a string pointer.
659 * FALSE: Read access to all bytes in string
662 BOOL WINAPI IsBadStringPtrA(
663 LPCSTR str, /* [in] Address of string */
664 UINT max ) /* [in] Maximum size of string */
666 if (!str) return TRUE;
670 volatile const char *p = str;
671 while (p != str + max) if (!*p++) break;
675 TRACE_(seh)("%p caused page fault during read\n", str);
683 /***********************************************************************
684 * IsBadStringPtrW (KERNEL32.@)
685 * See IsBadStringPtrA.
687 BOOL WINAPI IsBadStringPtrW( LPCWSTR str, UINT max )
689 if (!str) return TRUE;
693 volatile const WCHAR *p = str;
694 while (p != str + max) if (!*p++) break;
698 TRACE_(seh)("%p caused page fault during read\n", str);