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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
28 #include <sys/types.h>
34 #define WIN32_NO_STATUS
35 #define NONAMELESSUNION
42 #include "wine/exception.h"
43 #include "wine/debug.h"
45 #include "kernel_private.h"
47 WINE_DECLARE_DEBUG_CHANNEL(seh
);
48 WINE_DECLARE_DEBUG_CHANNEL(file
);
49 WINE_DECLARE_DEBUG_CHANNEL(virtual);
52 /***********************************************************************
53 * VirtualAlloc (KERNEL32.@)
55 * Reserves or commits a region of pages in virtual address space.
58 * addr [I] Address of region to reserve or commit.
59 * size [I] Size of region.
60 * type [I] Type of allocation.
61 * protect [I] Type of access protection.
64 * Success: Base address of allocated region of pages.
67 LPVOID WINAPI DECLSPEC_HOTPATCH
VirtualAlloc( void *addr
, SIZE_T size
, DWORD type
, DWORD protect
)
69 return VirtualAllocEx( GetCurrentProcess(), addr
, size
, type
, protect
);
73 /***********************************************************************
74 * VirtualAllocEx (KERNEL32.@)
76 * Seems to be just as VirtualAlloc, but with process handle.
79 * hProcess [I] Handle to process to do mem operation.
80 * addr [I] Address of region to reserve or commit.
81 * size [I] Size of region.
82 * type [I] Type of allocation.
83 * protect [I] Type of access protection.
87 * Success: Base address of allocated region of pages.
90 LPVOID WINAPI DECLSPEC_HOTPATCH
VirtualAllocEx( HANDLE hProcess
, LPVOID addr
, SIZE_T size
,
91 DWORD type
, DWORD protect
)
96 if ((status
= NtAllocateVirtualMemory( hProcess
, &ret
, 0, &size
, type
, protect
)))
98 SetLastError( RtlNtStatusToDosError(status
) );
105 /***********************************************************************
106 * VirtualAllocExNuma (KERNEL32.@)
108 LPVOID WINAPI DECLSPEC_HOTPATCH
VirtualAllocExNuma(HANDLE process
, void *addr
, SIZE_T size
,
109 DWORD type
, DWORD protect
, DWORD numa_node
)
111 FIXME_(virtual)("Ignoring preferred numa_node\n");
112 return VirtualAllocEx(process
, addr
, size
, type
, protect
);
116 /***********************************************************************
117 * VirtualFree (KERNEL32.@)
119 * Releases or decommits a region of pages in virtual address space.
122 * addr [I] Address of region of committed pages.
123 * size [I] Size of region.
124 * type [I] Type of operation.
130 BOOL WINAPI
VirtualFree( LPVOID addr
, SIZE_T size
, DWORD type
)
132 return VirtualFreeEx( GetCurrentProcess(), addr
, size
, type
);
136 /***********************************************************************
137 * VirtualFreeEx (KERNEL32.@)
139 * Releases or decommits a region of pages in virtual address space.
142 * process [I] Handle to process.
143 * addr [I] Address of region to free.
144 * size [I] Size of region.
145 * type [I] Type of operation.
151 BOOL WINAPI
VirtualFreeEx( HANDLE process
, LPVOID addr
, SIZE_T size
, DWORD type
)
153 NTSTATUS status
= NtFreeVirtualMemory( process
, &addr
, &size
, type
);
154 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
159 /***********************************************************************
160 * VirtualLock (KERNEL32.@)
162 * Locks the specified region of virtual address space.
165 * addr [I] Address of first byte of range to lock.
166 * size [I] Number of bytes in range to lock.
173 * Always returns TRUE.
176 BOOL WINAPI
VirtualLock( LPVOID addr
, SIZE_T size
)
178 NTSTATUS status
= NtLockVirtualMemory( GetCurrentProcess(), &addr
, &size
, 1 );
179 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
184 /***********************************************************************
185 * VirtualUnlock (KERNEL32.@)
187 * Unlocks a range of pages in the virtual address space.
190 * addr [I] Address of first byte of range.
191 * size [I] Number of bytes in range.
198 * Always returns TRUE.
201 BOOL WINAPI
VirtualUnlock( LPVOID addr
, SIZE_T size
)
203 NTSTATUS status
= NtUnlockVirtualMemory( GetCurrentProcess(), &addr
, &size
, 1 );
204 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
209 /***********************************************************************
210 * VirtualProtect (KERNEL32.@)
212 * Changes the access protection on a region of committed pages.
215 * addr [I] Address of region of committed pages.
216 * size [I] Size of region.
217 * new_prot [I] Desired access protection.
218 * old_prot [O] Address of variable to get old protection.
224 BOOL WINAPI
VirtualProtect( LPVOID addr
, SIZE_T size
, DWORD new_prot
, LPDWORD old_prot
)
226 return VirtualProtectEx( GetCurrentProcess(), addr
, size
, new_prot
, old_prot
);
230 /***********************************************************************
231 * VirtualProtectEx (KERNEL32.@)
233 * Changes the access protection on a region of committed pages in the
234 * virtual address space of a specified process.
237 * process [I] Handle of process.
238 * addr [I] Address of region of committed pages.
239 * size [I] Size of region.
240 * new_prot [I] Desired access protection.
241 * old_prot [O] Address of variable to get old protection.
247 BOOL WINAPI
VirtualProtectEx( HANDLE process
, LPVOID addr
, SIZE_T size
,
248 DWORD new_prot
, LPDWORD old_prot
)
253 /* Win9x allows passing NULL as old_prot while this fails on NT */
254 if (!old_prot
&& (GetVersion() & 0x80000000)) old_prot
= &prot
;
256 status
= NtProtectVirtualMemory( process
, &addr
, &size
, new_prot
, old_prot
);
257 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
262 /***********************************************************************
263 * VirtualQuery (KERNEL32.@)
265 * Provides info about a range of pages in virtual address space.
268 * addr [I] Address of region.
269 * info [O] Address of info buffer.
270 * len [I] Size of buffer.
273 * Number of bytes returned in information buffer or 0 if
274 * addr >= 0xc0000000 (kernel space).
276 SIZE_T WINAPI
VirtualQuery( LPCVOID addr
, PMEMORY_BASIC_INFORMATION info
,
279 return VirtualQueryEx( GetCurrentProcess(), addr
, info
, len
);
283 /***********************************************************************
284 * VirtualQueryEx (KERNEL32.@)
286 * Provides info about a range of pages in virtual address space of a
290 * process [I] Handle to process.
291 * addr [I] Address of region.
292 * info [O] Address of info buffer.
293 * len [I] Size of buffer.
296 * Number of bytes returned in information buffer.
298 SIZE_T WINAPI
VirtualQueryEx( HANDLE process
, LPCVOID addr
,
299 PMEMORY_BASIC_INFORMATION info
, SIZE_T len
)
304 if ((status
= NtQueryVirtualMemory( process
, addr
, MemoryBasicInformation
, info
, len
, &ret
)))
306 SetLastError( RtlNtStatusToDosError(status
) );
313 /***********************************************************************
314 * MapViewOfFile (KERNEL32.@)
316 * Maps a view of a file into the address space.
319 * mapping [I] File-mapping object to map.
320 * access [I] Access mode.
321 * offset_high [I] High-order 32 bits of file offset.
322 * offset_low [I] Low-order 32 bits of file offset.
323 * count [I] Number of bytes to map.
326 * Success: Starting address of mapped view.
329 LPVOID WINAPI DECLSPEC_HOTPATCH
MapViewOfFile( HANDLE mapping
, DWORD access
,
330 DWORD offset_high
, DWORD offset_low
, SIZE_T count
)
332 return MapViewOfFileEx( mapping
, access
, offset_high
,
333 offset_low
, count
, NULL
);
337 /***********************************************************************
338 * MapViewOfFileEx (KERNEL32.@)
340 * Maps a view of a file into the address space.
343 * handle [I] File-mapping object to map.
344 * access [I] Access mode.
345 * offset_high [I] High-order 32 bits of file offset.
346 * offset_low [I] Low-order 32 bits of file offset.
347 * count [I] Number of bytes to map.
348 * addr [I] Suggested starting address for mapped view.
351 * Success: Starting address of mapped view.
354 LPVOID WINAPI
MapViewOfFileEx( HANDLE handle
, DWORD access
,
355 DWORD offset_high
, DWORD offset_low
, SIZE_T count
, LPVOID addr
)
358 LARGE_INTEGER offset
;
362 offset
.u
.LowPart
= offset_low
;
363 offset
.u
.HighPart
= offset_high
;
365 exec
= access
& FILE_MAP_EXECUTE
;
366 access
&= ~FILE_MAP_EXECUTE
;
368 if (access
== FILE_MAP_COPY
)
369 protect
= exec
? PAGE_EXECUTE_WRITECOPY
: PAGE_WRITECOPY
;
370 else if (access
& FILE_MAP_WRITE
)
371 protect
= exec
? PAGE_EXECUTE_READWRITE
: PAGE_READWRITE
;
372 else if (access
& FILE_MAP_READ
)
373 protect
= exec
? PAGE_EXECUTE_READ
: PAGE_READONLY
;
374 else protect
= PAGE_NOACCESS
;
376 if ((status
= NtMapViewOfSection( handle
, GetCurrentProcess(), &addr
, 0, 0, &offset
,
377 &count
, ViewShare
, 0, protect
)) < 0)
379 SetLastError( RtlNtStatusToDosError(status
) );
386 /***********************************************************************
387 * UnmapViewOfFile (KERNEL32.@)
389 * Unmaps a mapped view of a file.
392 * addr [I] Address where mapped view begins.
399 BOOL WINAPI DECLSPEC_HOTPATCH
UnmapViewOfFile( const void *addr
)
403 if (GetVersion() & 0x80000000)
405 MEMORY_BASIC_INFORMATION info
;
406 if (!VirtualQuery( addr
, &info
, sizeof(info
) ) || info
.AllocationBase
!= addr
)
408 SetLastError( ERROR_INVALID_ADDRESS
);
413 status
= NtUnmapViewOfSection( GetCurrentProcess(), (void *)addr
);
414 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
419 /***********************************************************************
420 * FlushViewOfFile (KERNEL32.@)
422 * Writes to the disk a byte range within a mapped view of a file.
425 * base [I] Start address of byte range to flush.
426 * size [I] Number of bytes in range.
432 BOOL WINAPI
FlushViewOfFile( LPCVOID base
, SIZE_T size
)
434 NTSTATUS status
= NtFlushVirtualMemory( GetCurrentProcess(), &base
, &size
, 0 );
437 if (status
== STATUS_NOT_MAPPED_DATA
) status
= STATUS_SUCCESS
;
438 else SetLastError( RtlNtStatusToDosError(status
) );
444 /***********************************************************************
445 * GetWriteWatch (KERNEL32.@)
447 UINT WINAPI
GetWriteWatch( DWORD flags
, LPVOID base
, SIZE_T size
, LPVOID
*addresses
,
448 ULONG_PTR
*count
, ULONG
*granularity
)
452 status
= NtGetWriteWatch( GetCurrentProcess(), flags
, base
, size
, addresses
, count
, granularity
);
453 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
454 return status
? ~0u : 0;
458 /***********************************************************************
459 * ResetWriteWatch (KERNEL32.@)
461 UINT WINAPI
ResetWriteWatch( LPVOID base
, SIZE_T size
)
465 status
= NtResetWriteWatch( GetCurrentProcess(), base
, size
);
466 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
467 return status
? ~0u : 0;
471 /***********************************************************************
472 * IsBadReadPtr (KERNEL32.@)
474 * Check for read access on a memory block.
476 * ptr [I] Address of memory block.
477 * size [I] Size of block.
481 * Failure: FALSE. Process has read access to entire block.
483 BOOL WINAPI
IsBadReadPtr( LPCVOID ptr
, UINT_PTR size
)
485 if (!size
) return FALSE
; /* handle 0 size case w/o reference */
486 if (!ptr
) return TRUE
;
489 volatile const char *p
= ptr
;
490 char dummy
__attribute__((unused
));
491 UINT_PTR count
= size
;
493 while (count
> system_info
.PageSize
)
496 p
+= system_info
.PageSize
;
497 count
-= system_info
.PageSize
;
500 dummy
= p
[count
- 1];
504 TRACE_(seh
)("%p caused page fault during read\n", ptr
);
512 /***********************************************************************
513 * IsBadWritePtr (KERNEL32.@)
515 * Check for write access on a memory block.
518 * ptr [I] Address of memory block.
519 * size [I] Size of block in bytes.
523 * Failure: FALSE. Process has write access to entire block.
525 BOOL WINAPI
IsBadWritePtr( LPVOID ptr
, UINT_PTR size
)
527 if (!size
) return FALSE
; /* handle 0 size case w/o reference */
528 if (!ptr
) return TRUE
;
531 volatile char *p
= ptr
;
532 UINT_PTR count
= size
;
534 while (count
> system_info
.PageSize
)
537 p
+= system_info
.PageSize
;
538 count
-= system_info
.PageSize
;
545 TRACE_(seh
)("%p caused page fault during write\n", ptr
);
553 /***********************************************************************
554 * IsBadHugeReadPtr (KERNEL32.@)
556 * Check for read access on a memory block.
559 * ptr [I] Address of memory block.
560 * size [I] Size of block.
564 * Failure: FALSE. Process has read access to entire block.
566 BOOL WINAPI
IsBadHugeReadPtr( LPCVOID ptr
, UINT_PTR size
)
568 return IsBadReadPtr( ptr
, size
);
572 /***********************************************************************
573 * IsBadHugeWritePtr (KERNEL32.@)
575 * Check for write access on a memory block.
578 * ptr [I] Address of memory block.
579 * size [I] Size of block.
583 * Failure: FALSE. Process has write access to entire block.
585 BOOL WINAPI
IsBadHugeWritePtr( LPVOID ptr
, UINT_PTR size
)
587 return IsBadWritePtr( ptr
, size
);
591 /***********************************************************************
592 * IsBadCodePtr (KERNEL32.@)
594 * Check for read access on a memory address.
597 * ptr [I] Address of function.
601 * Failure: FALSE. Process has read access to specified memory.
603 BOOL WINAPI
IsBadCodePtr( FARPROC ptr
)
605 return IsBadReadPtr( ptr
, 1 );
609 /***********************************************************************
610 * IsBadStringPtrA (KERNEL32.@)
612 * Check for read access on a range of memory pointed to by a string pointer.
615 * str [I] Address of string.
616 * max [I] Maximum size of string.
620 * Failure: FALSE. Read access to all bytes in string.
622 BOOL WINAPI
IsBadStringPtrA( LPCSTR str
, UINT_PTR max
)
624 if (!str
) return TRUE
;
628 volatile const char *p
= str
;
629 while (p
!= str
+ max
) if (!*p
++) break;
633 TRACE_(seh
)("%p caused page fault during read\n", str
);
641 /***********************************************************************
642 * IsBadStringPtrW (KERNEL32.@)
644 * See IsBadStringPtrA.
646 BOOL WINAPI
IsBadStringPtrW( LPCWSTR str
, UINT_PTR max
)
648 if (!str
) return TRUE
;
652 volatile const WCHAR
*p
= str
;
653 while (p
!= str
+ max
) if (!*p
++) break;
657 TRACE_(seh
)("%p caused page fault during read\n", str
);
664 /***********************************************************************
665 * K32GetMappedFileNameA (KERNEL32.@)
667 DWORD WINAPI
K32GetMappedFileNameA(HANDLE process
, LPVOID lpv
, LPSTR file_name
, DWORD size
)
669 FIXME_(file
)("(%p, %p, %p, %d): stub\n", process
, lpv
, file_name
, size
);
671 if (file_name
&& size
)
677 /***********************************************************************
678 * K32GetMappedFileNameW (KERNEL32.@)
680 DWORD WINAPI
K32GetMappedFileNameW(HANDLE process
, LPVOID lpv
, LPWSTR file_name
, DWORD size
)
682 FIXME_(file
)("(%p, %p, %p, %d): stub\n", process
, lpv
, file_name
, size
);
684 if (file_name
&& size
)
690 /***********************************************************************
691 * K32EnumPageFilesA (KERNEL32.@)
693 BOOL WINAPI
K32EnumPageFilesA( PENUM_PAGE_FILE_CALLBACKA callback
, LPVOID context
)
695 FIXME_(file
)("(%p, %p) stub\n", callback
, context
);
699 /***********************************************************************
700 * K32EnumPageFilesW (KERNEL32.@)
702 BOOL WINAPI
K32EnumPageFilesW( PENUM_PAGE_FILE_CALLBACKW callback
, LPVOID context
)
704 FIXME_(file
)("(%p, %p) stub\n", callback
, context
);
708 /***********************************************************************
709 * K32GetWsChanges (KERNEL32.@)
711 BOOL WINAPI
K32GetWsChanges(HANDLE process
, PPSAPI_WS_WATCH_INFORMATION watchinfo
, DWORD size
)
715 TRACE_(seh
)("(%p, %p, %d)\n", process
, watchinfo
, size
);
717 status
= NtQueryInformationProcess( process
, ProcessWorkingSetWatch
, watchinfo
, size
, NULL
);
721 SetLastError( RtlNtStatusToDosError( status
) );
727 /***********************************************************************
728 * K32GetWsChangesEx (KERNEL32.@)
730 BOOL WINAPI
K32GetWsChangesEx(HANDLE process
, PSAPI_WS_WATCH_INFORMATION_EX
*watchinfoex
, DWORD
*size
)
732 FIXME_(seh
)("(%p, %p, %p)\n", process
, watchinfoex
, size
);
734 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
738 /***********************************************************************
739 * K32InitializeProcessForWsWatch (KERNEL32.@)
741 BOOL WINAPI
K32InitializeProcessForWsWatch(HANDLE process
)
743 FIXME_(seh
)("(process=%p): stub\n", process
);