wined3d: Drop support for WINED3DFMT_D32_UNORM.
[wine.git] / dlls / kernel32 / virtual.c
bloba5a2dfdf270fc044ce4c48a09bcd8231e99d991c
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <fcntl.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
33 #include "ntstatus.h"
34 #define WIN32_NO_STATUS
35 #define NONAMELESSUNION
36 #include "windef.h"
37 #include "winbase.h"
38 #include "winnls.h"
39 #include "winternl.h"
40 #include "winerror.h"
41 #include "psapi.h"
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.
57 * PARAMS
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.
63 * RETURNS
64 * Success: Base address of allocated region of pages.
65 * Failure: NULL.
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.
78 * PARAMS
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.
86 * RETURNS
87 * Success: Base address of allocated region of pages.
88 * Failure: NULL.
90 LPVOID WINAPI DECLSPEC_HOTPATCH VirtualAllocEx( HANDLE hProcess, LPVOID addr, SIZE_T size,
91 DWORD type, DWORD protect )
93 LPVOID ret = addr;
94 NTSTATUS status;
96 if ((status = NtAllocateVirtualMemory( hProcess, &ret, 0, &size, type, protect )))
98 SetLastError( RtlNtStatusToDosError(status) );
99 ret = NULL;
101 return ret;
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.
121 * PARAMS
122 * addr [I] Address of region of committed pages.
123 * size [I] Size of region.
124 * type [I] Type of operation.
126 * RETURNS
127 * Success: TRUE.
128 * Failure: FALSE.
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.
141 * PARAMS
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.
147 * RETURNS
148 * Success: TRUE.
149 * Failure: FALSE.
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) );
155 return !status;
159 /***********************************************************************
160 * VirtualLock (KERNEL32.@)
162 * Locks the specified region of virtual address space.
164 * PARAMS
165 * addr [I] Address of first byte of range to lock.
166 * size [I] Number of bytes in range to lock.
168 * RETURNS
169 * Success: TRUE.
170 * Failure: FALSE.
172 * NOTES
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) );
180 return !status;
184 /***********************************************************************
185 * VirtualUnlock (KERNEL32.@)
187 * Unlocks a range of pages in the virtual address space.
189 * PARAMS
190 * addr [I] Address of first byte of range.
191 * size [I] Number of bytes in range.
193 * RETURNS
194 * Success: TRUE.
195 * Failure: FALSE.
197 * NOTES
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) );
205 return !status;
209 /***********************************************************************
210 * VirtualProtect (KERNEL32.@)
212 * Changes the access protection on a region of committed pages.
214 * PARAMS
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.
220 * RETURNS
221 * Success: TRUE.
222 * Failure: FALSE.
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.
236 * PARAMS
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.
243 * RETURNS
244 * Success: TRUE.
245 * Failure: FALSE.
247 BOOL WINAPI VirtualProtectEx( HANDLE process, LPVOID addr, SIZE_T size,
248 DWORD new_prot, LPDWORD old_prot )
250 NTSTATUS status;
251 DWORD 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) );
258 return !status;
262 /***********************************************************************
263 * VirtualQuery (KERNEL32.@)
265 * Provides info about a range of pages in virtual address space.
267 * PARAMS
268 * addr [I] Address of region.
269 * info [O] Address of info buffer.
270 * len [I] Size of buffer.
272 * RETURNS
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,
277 SIZE_T len )
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
287 * specified process.
289 * PARAMS
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.
295 * RETURNS
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 )
301 SIZE_T ret;
302 NTSTATUS status;
304 if ((status = NtQueryVirtualMemory( process, addr, MemoryBasicInformation, info, len, &ret )))
306 SetLastError( RtlNtStatusToDosError(status) );
307 ret = 0;
309 return ret;
313 /***********************************************************************
314 * MapViewOfFile (KERNEL32.@)
316 * Maps a view of a file into the address space.
318 * PARAMS
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.
325 * RETURNS
326 * Success: Starting address of mapped view.
327 * Failure: NULL.
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.
342 * PARAMS
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.
350 * RETURNS
351 * Success: Starting address of mapped view.
352 * Failure: NULL.
354 LPVOID WINAPI MapViewOfFileEx( HANDLE handle, DWORD access,
355 DWORD offset_high, DWORD offset_low, SIZE_T count, LPVOID addr )
357 NTSTATUS status;
358 LARGE_INTEGER offset;
359 ULONG protect;
360 BOOL exec;
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) );
380 addr = NULL;
382 return addr;
386 /***********************************************************************
387 * UnmapViewOfFile (KERNEL32.@)
389 * Unmaps a mapped view of a file.
391 * PARAMS
392 * addr [I] Address where mapped view begins.
394 * RETURNS
395 * Success: TRUE.
396 * Failure: FALSE.
399 BOOL WINAPI DECLSPEC_HOTPATCH UnmapViewOfFile( const void *addr )
401 NTSTATUS status;
403 if (GetVersion() & 0x80000000)
405 MEMORY_BASIC_INFORMATION info;
406 if (!VirtualQuery( addr, &info, sizeof(info) ) || info.AllocationBase != addr)
408 SetLastError( ERROR_INVALID_ADDRESS );
409 return FALSE;
413 status = NtUnmapViewOfSection( GetCurrentProcess(), (void *)addr );
414 if (status) SetLastError( RtlNtStatusToDosError(status) );
415 return !status;
419 /***********************************************************************
420 * FlushViewOfFile (KERNEL32.@)
422 * Writes to the disk a byte range within a mapped view of a file.
424 * PARAMS
425 * base [I] Start address of byte range to flush.
426 * size [I] Number of bytes in range.
428 * RETURNS
429 * Success: TRUE.
430 * Failure: FALSE.
432 BOOL WINAPI FlushViewOfFile( LPCVOID base, SIZE_T size )
434 NTSTATUS status = NtFlushVirtualMemory( GetCurrentProcess(), &base, &size, 0 );
435 if (status)
437 if (status == STATUS_NOT_MAPPED_DATA) status = STATUS_SUCCESS;
438 else SetLastError( RtlNtStatusToDosError(status) );
440 return !status;
444 /***********************************************************************
445 * GetWriteWatch (KERNEL32.@)
447 UINT WINAPI GetWriteWatch( DWORD flags, LPVOID base, SIZE_T size, LPVOID *addresses,
448 ULONG_PTR *count, ULONG *granularity )
450 NTSTATUS status;
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 )
463 NTSTATUS status;
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.
479 * RETURNS
480 * Success: TRUE.
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;
487 __TRY
489 volatile const char *p = ptr;
490 char dummy __attribute__((unused));
491 UINT_PTR count = size;
493 while (count > system_info.PageSize)
495 dummy = *p;
496 p += system_info.PageSize;
497 count -= system_info.PageSize;
499 dummy = p[0];
500 dummy = p[count - 1];
502 __EXCEPT_PAGE_FAULT
504 TRACE_(seh)("%p caused page fault during read\n", ptr);
505 return TRUE;
507 __ENDTRY
508 return FALSE;
512 /***********************************************************************
513 * IsBadWritePtr (KERNEL32.@)
515 * Check for write access on a memory block.
517 * PARAMS
518 * ptr [I] Address of memory block.
519 * size [I] Size of block in bytes.
521 * RETURNS
522 * Success: TRUE.
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;
529 __TRY
531 volatile char *p = ptr;
532 UINT_PTR count = size;
534 while (count > system_info.PageSize)
536 *p |= 0;
537 p += system_info.PageSize;
538 count -= system_info.PageSize;
540 p[0] |= 0;
541 p[count - 1] |= 0;
543 __EXCEPT_PAGE_FAULT
545 TRACE_(seh)("%p caused page fault during write\n", ptr);
546 return TRUE;
548 __ENDTRY
549 return FALSE;
553 /***********************************************************************
554 * IsBadHugeReadPtr (KERNEL32.@)
556 * Check for read access on a memory block.
558 * PARAMS
559 * ptr [I] Address of memory block.
560 * size [I] Size of block.
562 * RETURNS
563 * Success: TRUE.
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.
577 * PARAMS
578 * ptr [I] Address of memory block.
579 * size [I] Size of block.
581 * RETURNS
582 * Success: TRUE.
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.
596 * PARAMS
597 * ptr [I] Address of function.
599 * RETURNS
600 * Success: TRUE.
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.
614 * PARAMS
615 * str [I] Address of string.
616 * max [I] Maximum size of string.
618 * RETURNS
619 * Success: TRUE.
620 * Failure: FALSE. Read access to all bytes in string.
622 BOOL WINAPI IsBadStringPtrA( LPCSTR str, UINT_PTR max )
624 if (!str) return TRUE;
626 __TRY
628 volatile const char *p = str;
629 while (p != str + max) if (!*p++) break;
631 __EXCEPT_PAGE_FAULT
633 TRACE_(seh)("%p caused page fault during read\n", str);
634 return TRUE;
636 __ENDTRY
637 return FALSE;
641 /***********************************************************************
642 * IsBadStringPtrW (KERNEL32.@)
644 * See IsBadStringPtrA.
646 BOOL WINAPI IsBadStringPtrW( LPCWSTR str, UINT_PTR max )
648 if (!str) return TRUE;
650 __TRY
652 volatile const WCHAR *p = str;
653 while (p != str + max) if (!*p++) break;
655 __EXCEPT_PAGE_FAULT
657 TRACE_(seh)("%p caused page fault during read\n", str);
658 return TRUE;
660 __ENDTRY
661 return FALSE;
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)
672 file_name[0] = '\0';
674 return 0;
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)
685 file_name[0] = '\0';
687 return 0;
690 /***********************************************************************
691 * K32EnumPageFilesA (KERNEL32.@)
693 BOOL WINAPI K32EnumPageFilesA( PENUM_PAGE_FILE_CALLBACKA callback, LPVOID context )
695 FIXME_(file)("(%p, %p) stub\n", callback, context );
696 return FALSE;
699 /***********************************************************************
700 * K32EnumPageFilesW (KERNEL32.@)
702 BOOL WINAPI K32EnumPageFilesW( PENUM_PAGE_FILE_CALLBACKW callback, LPVOID context )
704 FIXME_(file)("(%p, %p) stub\n", callback, context );
705 return FALSE;
708 /***********************************************************************
709 * K32GetWsChanges (KERNEL32.@)
711 BOOL WINAPI K32GetWsChanges(HANDLE process, PPSAPI_WS_WATCH_INFORMATION watchinfo, DWORD size)
713 NTSTATUS status;
715 TRACE_(seh)("(%p, %p, %d)\n", process, watchinfo, size);
717 status = NtQueryInformationProcess( process, ProcessWorkingSetWatch, watchinfo, size, NULL );
719 if (status)
721 SetLastError( RtlNtStatusToDosError( status ) );
722 return FALSE;
724 return TRUE;
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);
735 return FALSE;
738 /***********************************************************************
739 * K32InitializeProcessForWsWatch (KERNEL32.@)
741 BOOL WINAPI K32InitializeProcessForWsWatch(HANDLE process)
743 FIXME_(seh)("(process=%p): stub\n", process);
745 return TRUE;