Fixed COORD structure definition.
[wine/multimedia.git] / memory / virtual.c
blobdd2281d6138a769391a468af6dbb9e78d184b9cd
1 /*
2 * Win32 virtual memory functions
4 * Copyright 1997 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <errno.h>
11 #ifdef HAVE_SYS_ERRNO_H
12 #include <sys/errno.h>
13 #endif
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <sys/types.h>
20 #ifdef HAVE_SYS_MMAN_H
21 #include <sys/mman.h>
22 #endif
23 #include "winbase.h"
24 #include "wine/exception.h"
25 #include "winerror.h"
26 #include "file.h"
27 #include "process.h"
28 #include "global.h"
29 #include "server.h"
30 #include "debugtools.h"
32 DEFAULT_DEBUG_CHANNEL(virtual);
34 #ifndef MS_SYNC
35 #define MS_SYNC 0
36 #endif
38 /* File view */
39 typedef struct _FV
41 struct _FV *next; /* Next view */
42 struct _FV *prev; /* Prev view */
43 UINT base; /* Base address */
44 UINT size; /* Size in bytes */
45 UINT flags; /* Allocation flags */
46 HANDLE mapping; /* Handle to the file mapping */
47 HANDLERPROC handlerProc; /* Fault handler */
48 LPVOID handlerArg; /* Fault handler argument */
49 BYTE protect; /* Protection for all pages at allocation time */
50 BYTE prot[1]; /* Protection byte for each page */
51 } FILE_VIEW;
53 /* Per-view flags */
54 #define VFLAG_SYSTEM 0x01
56 /* Conversion from VPROT_* to Win32 flags */
57 static const BYTE VIRTUAL_Win32Flags[16] =
59 PAGE_NOACCESS, /* 0 */
60 PAGE_READONLY, /* READ */
61 PAGE_READWRITE, /* WRITE */
62 PAGE_READWRITE, /* READ | WRITE */
63 PAGE_EXECUTE, /* EXEC */
64 PAGE_EXECUTE_READ, /* READ | EXEC */
65 PAGE_EXECUTE_READWRITE, /* WRITE | EXEC */
66 PAGE_EXECUTE_READWRITE, /* READ | WRITE | EXEC */
67 PAGE_WRITECOPY, /* WRITECOPY */
68 PAGE_WRITECOPY, /* READ | WRITECOPY */
69 PAGE_WRITECOPY, /* WRITE | WRITECOPY */
70 PAGE_WRITECOPY, /* READ | WRITE | WRITECOPY */
71 PAGE_EXECUTE_WRITECOPY, /* EXEC | WRITECOPY */
72 PAGE_EXECUTE_WRITECOPY, /* READ | EXEC | WRITECOPY */
73 PAGE_EXECUTE_WRITECOPY, /* WRITE | EXEC | WRITECOPY */
74 PAGE_EXECUTE_WRITECOPY /* READ | WRITE | EXEC | WRITECOPY */
78 static FILE_VIEW *VIRTUAL_FirstView;
80 #ifdef __i386__
81 /* These are always the same on an i386, and it will be faster this way */
82 # define page_mask 0xfff
83 # define page_shift 12
84 #else
85 static UINT page_shift;
86 static UINT page_mask;
87 #endif /* __i386__ */
88 #define granularity_mask 0xffff /* Allocation granularity (usually 64k) */
90 #define ROUND_ADDR(addr) \
91 ((UINT)(addr) & ~page_mask)
93 #define ROUND_SIZE(addr,size) \
94 (((UINT)(size) + ((UINT)(addr) & page_mask) + page_mask) & ~page_mask)
96 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
97 if (!TRACE_ON(virtual)); else VIRTUAL_DumpView(view)
100 /* filter for page-fault exceptions */
101 static WINE_EXCEPTION_FILTER(page_fault)
103 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
104 return EXCEPTION_EXECUTE_HANDLER;
105 return EXCEPTION_CONTINUE_SEARCH;
108 /***********************************************************************
109 * VIRTUAL_GetProtStr
111 static const char *VIRTUAL_GetProtStr( BYTE prot )
113 static char buffer[6];
114 buffer[0] = (prot & VPROT_COMMITTED) ? 'c' : '-';
115 buffer[1] = (prot & VPROT_GUARD) ? 'g' : '-';
116 buffer[2] = (prot & VPROT_READ) ? 'r' : '-';
117 buffer[3] = (prot & VPROT_WRITE) ?
118 ((prot & VPROT_WRITECOPY) ? 'w' : 'W') : '-';
119 buffer[4] = (prot & VPROT_EXEC) ? 'x' : '-';
120 buffer[5] = 0;
121 return buffer;
125 /***********************************************************************
126 * VIRTUAL_DumpView
128 static void VIRTUAL_DumpView( FILE_VIEW *view )
130 UINT i, count;
131 UINT addr = view->base;
132 BYTE prot = view->prot[0];
134 DPRINTF( "View: %08x - %08x%s",
135 view->base, view->base + view->size - 1,
136 (view->flags & VFLAG_SYSTEM) ? " (system)" : "" );
137 if (view->mapping)
138 DPRINTF( " %d\n", view->mapping );
139 else
140 DPRINTF( " (anonymous)\n");
142 for (count = i = 1; i < view->size >> page_shift; i++, count++)
144 if (view->prot[i] == prot) continue;
145 DPRINTF( " %08x - %08x %s\n",
146 addr, addr + (count << page_shift) - 1,
147 VIRTUAL_GetProtStr(prot) );
148 addr += (count << page_shift);
149 prot = view->prot[i];
150 count = 0;
152 if (count)
153 DPRINTF( " %08x - %08x %s\n",
154 addr, addr + (count << page_shift) - 1,
155 VIRTUAL_GetProtStr(prot) );
159 /***********************************************************************
160 * VIRTUAL_Dump
162 void VIRTUAL_Dump(void)
164 FILE_VIEW *view = VIRTUAL_FirstView;
165 DPRINTF( "\nDump of all virtual memory views:\n\n" );
166 while (view)
168 VIRTUAL_DumpView( view );
169 view = view->next;
174 /***********************************************************************
175 * VIRTUAL_FindView
177 * Find the view containing a given address.
179 * RETURNS
180 * View: Success
181 * NULL: Failure
183 static FILE_VIEW *VIRTUAL_FindView(
184 UINT addr /* [in] Address */
186 FILE_VIEW *view = VIRTUAL_FirstView;
187 while (view)
189 if (view->base > addr) return NULL;
190 if (view->base + view->size > addr) return view;
191 view = view->next;
193 return NULL;
197 /***********************************************************************
198 * VIRTUAL_CreateView
200 * Create a new view and add it in the linked list.
202 static FILE_VIEW *VIRTUAL_CreateView( UINT base, UINT size, UINT offset,
203 UINT flags, BYTE vprot,
204 HANDLE mapping )
206 FILE_VIEW *view, *prev;
208 /* Create the view structure */
210 assert( !(base & page_mask) );
211 assert( !(size & page_mask) );
212 size >>= page_shift;
213 if (!(view = (FILE_VIEW *)malloc( sizeof(*view) + size - 1 ))) return NULL;
214 view->base = base;
215 view->size = size << page_shift;
216 view->flags = flags;
217 view->mapping = mapping;
218 view->protect = vprot;
219 view->handlerProc = NULL;
220 memset( view->prot, vprot, size );
222 /* Duplicate the mapping handle */
224 if ((view->mapping != -1) &&
225 !DuplicateHandle( GetCurrentProcess(), view->mapping,
226 GetCurrentProcess(), &view->mapping,
227 0, FALSE, DUPLICATE_SAME_ACCESS ))
229 free( view );
230 return NULL;
233 /* Insert it in the linked list */
235 if (!VIRTUAL_FirstView || (VIRTUAL_FirstView->base > base))
237 view->next = VIRTUAL_FirstView;
238 view->prev = NULL;
239 if (view->next) view->next->prev = view;
240 VIRTUAL_FirstView = view;
242 else
244 prev = VIRTUAL_FirstView;
245 while (prev->next && (prev->next->base < base)) prev = prev->next;
246 view->next = prev->next;
247 view->prev = prev;
248 if (view->next) view->next->prev = view;
249 prev->next = view;
251 VIRTUAL_DEBUG_DUMP_VIEW( view );
252 return view;
256 /***********************************************************************
257 * VIRTUAL_DeleteView
258 * Deletes a view.
260 * RETURNS
261 * None
263 static void VIRTUAL_DeleteView(
264 FILE_VIEW *view /* [in] View */
266 if (!(view->flags & VFLAG_SYSTEM))
267 FILE_munmap( (void *)view->base, 0, view->size );
268 if (view->next) view->next->prev = view->prev;
269 if (view->prev) view->prev->next = view->next;
270 else VIRTUAL_FirstView = view->next;
271 if (view->mapping) CloseHandle( view->mapping );
272 free( view );
276 /***********************************************************************
277 * VIRTUAL_GetUnixProt
279 * Convert page protections to protection for mmap/mprotect.
281 static int VIRTUAL_GetUnixProt( BYTE vprot )
283 int prot = 0;
284 if ((vprot & VPROT_COMMITTED) && !(vprot & VPROT_GUARD))
286 if (vprot & VPROT_READ) prot |= PROT_READ;
287 if (vprot & VPROT_WRITE) prot |= PROT_WRITE;
288 if (vprot & VPROT_WRITECOPY) prot |= PROT_WRITE;
289 if (vprot & VPROT_EXEC) prot |= PROT_EXEC;
291 return prot;
295 /***********************************************************************
296 * VIRTUAL_GetWin32Prot
298 * Convert page protections to Win32 flags.
300 * RETURNS
301 * None
303 static void VIRTUAL_GetWin32Prot(
304 BYTE vprot, /* [in] Page protection flags */
305 DWORD *protect, /* [out] Location to store Win32 protection flags */
306 DWORD *state /* [out] Location to store mem state flag */
308 if (protect) {
309 *protect = VIRTUAL_Win32Flags[vprot & 0x0f];
310 /* if (vprot & VPROT_GUARD) *protect |= PAGE_GUARD;*/
311 if (vprot & VPROT_NOCACHE) *protect |= PAGE_NOCACHE;
313 if (vprot & VPROT_GUARD) *protect = PAGE_NOACCESS;
316 if (state) *state = (vprot & VPROT_COMMITTED) ? MEM_COMMIT : MEM_RESERVE;
320 /***********************************************************************
321 * VIRTUAL_GetProt
323 * Build page protections from Win32 flags.
325 * RETURNS
326 * Value of page protection flags
328 static BYTE VIRTUAL_GetProt(
329 DWORD protect /* [in] Win32 protection flags */
331 BYTE vprot;
333 switch(protect & 0xff)
335 case PAGE_READONLY:
336 vprot = VPROT_READ;
337 break;
338 case PAGE_READWRITE:
339 vprot = VPROT_READ | VPROT_WRITE;
340 break;
341 case PAGE_WRITECOPY:
342 vprot = VPROT_READ | VPROT_WRITE | VPROT_WRITECOPY;
343 break;
344 case PAGE_EXECUTE:
345 vprot = VPROT_EXEC;
346 break;
347 case PAGE_EXECUTE_READ:
348 vprot = VPROT_EXEC | VPROT_READ;
349 break;
350 case PAGE_EXECUTE_READWRITE:
351 vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITE;
352 break;
353 case PAGE_EXECUTE_WRITECOPY:
354 vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITE | VPROT_WRITECOPY;
355 break;
356 case PAGE_NOACCESS:
357 default:
358 vprot = 0;
359 break;
361 if (protect & PAGE_GUARD) vprot |= VPROT_GUARD;
362 if (protect & PAGE_NOCACHE) vprot |= VPROT_NOCACHE;
363 return vprot;
367 /***********************************************************************
368 * VIRTUAL_SetProt
370 * Change the protection of a range of pages.
372 * RETURNS
373 * TRUE: Success
374 * FALSE: Failure
376 static BOOL VIRTUAL_SetProt(
377 FILE_VIEW *view, /* [in] Pointer to view */
378 UINT base, /* [in] Starting address */
379 UINT size, /* [in] Size in bytes */
380 BYTE vprot /* [in] Protections to use */
382 TRACE("%08x-%08x %s\n",
383 base, base + size - 1, VIRTUAL_GetProtStr( vprot ) );
385 if (mprotect( (void *)base, size, VIRTUAL_GetUnixProt(vprot) ))
386 return FALSE; /* FIXME: last error */
388 memset( view->prot + ((base - view->base) >> page_shift),
389 vprot, size >> page_shift );
390 VIRTUAL_DEBUG_DUMP_VIEW( view );
391 return TRUE;
395 /***********************************************************************
396 * VIRTUAL_Init
398 #ifndef page_mask
399 DECL_GLOBAL_CONSTRUCTOR(VIRTUAL_Init)
401 DWORD page_size;
403 # ifdef HAVE_GETPAGESIZE
404 page_size = getpagesize();
405 # else
406 # ifdef __svr4__
407 page_size = sysconf(_SC_PAGESIZE);
408 # else
409 # error Cannot get the page size on this platform
410 # endif
411 # endif
412 page_mask = page_size - 1;
413 /* Make sure we have a power of 2 */
414 assert( !(page_size & page_mask) );
415 page_shift = 0;
416 while ((1 << page_shift) != page_size) page_shift++;
418 #endif /* page_mask */
421 /***********************************************************************
422 * VIRTUAL_GetPageSize
424 DWORD VIRTUAL_GetPageSize(void)
426 return 1 << page_shift;
430 /***********************************************************************
431 * VIRTUAL_GetGranularity
433 DWORD VIRTUAL_GetGranularity(void)
435 return granularity_mask + 1;
439 /***********************************************************************
440 * VIRTUAL_SetFaultHandler
442 BOOL VIRTUAL_SetFaultHandler( LPCVOID addr, HANDLERPROC proc, LPVOID arg )
444 FILE_VIEW *view;
446 if (!(view = VIRTUAL_FindView((UINT)addr))) return FALSE;
447 view->handlerProc = proc;
448 view->handlerArg = arg;
449 return TRUE;
452 /***********************************************************************
453 * VIRTUAL_HandleFault
455 DWORD VIRTUAL_HandleFault( LPCVOID addr )
457 FILE_VIEW *view = VIRTUAL_FindView((UINT)addr);
458 DWORD ret = EXCEPTION_ACCESS_VIOLATION;
460 if (view)
462 if (view->handlerProc)
464 if (view->handlerProc(view->handlerArg, addr)) ret = 0; /* handled */
466 else
468 BYTE vprot = view->prot[((UINT)addr - view->base) >> page_shift];
469 UINT page = (UINT)addr & ~page_mask;
470 char *stack = (char *)NtCurrentTeb()->stack_base + SIGNAL_STACK_SIZE + page_mask + 1;
471 if (vprot & VPROT_GUARD)
473 VIRTUAL_SetProt( view, page, page_mask + 1, vprot & ~VPROT_GUARD );
474 ret = STATUS_GUARD_PAGE_VIOLATION;
476 /* is it inside the stack guard pages? */
477 if (((char *)addr >= stack) && ((char *)addr < stack + 2*(page_mask+1)))
478 ret = STATUS_STACK_OVERFLOW;
481 return ret;
485 /***********************************************************************
486 * VirtualAlloc (KERNEL32.548)
487 * Reserves or commits a region of pages in virtual address space
489 * RETURNS
490 * Base address of allocated region of pages
491 * NULL: Failure
493 LPVOID WINAPI VirtualAlloc(
494 LPVOID addr, /* [in] Address of region to reserve or commit */
495 DWORD size, /* [in] Size of region */
496 DWORD type, /* [in] Type of allocation */
497 DWORD protect /* [in] Type of access protection */
499 FILE_VIEW *view;
500 UINT base, ptr, view_size;
501 BYTE vprot;
503 TRACE("%08x %08lx %lx %08lx\n",
504 (UINT)addr, size, type, protect );
506 /* Round parameters to a page boundary */
508 if (size > 0x7fc00000) /* 2Gb - 4Mb */
510 SetLastError( ERROR_OUTOFMEMORY );
511 return NULL;
513 if (addr)
515 if (type & MEM_RESERVE) /* Round down to 64k boundary */
516 base = (UINT)addr & ~granularity_mask;
517 else
518 base = ROUND_ADDR( addr );
519 size = (((UINT)addr + size + page_mask) & ~page_mask) - base;
520 if ((base <= granularity_mask) || (base + size < base))
522 /* disallow low 64k and wrap-around */
523 SetLastError( ERROR_INVALID_PARAMETER );
524 return NULL;
527 else
529 base = 0;
530 size = (size + page_mask) & ~page_mask;
533 if (type & MEM_TOP_DOWN) {
534 /* FIXME: MEM_TOP_DOWN allocates the largest possible address.
535 * Is there _ANY_ way to do it with UNIX mmap()?
537 WARN("MEM_TOP_DOWN ignored\n");
538 type &= ~MEM_TOP_DOWN;
540 /* Compute the protection flags */
542 if (!(type & (MEM_COMMIT | MEM_RESERVE | MEM_SYSTEM)) ||
543 (type & ~(MEM_COMMIT | MEM_RESERVE | MEM_SYSTEM)))
545 SetLastError( ERROR_INVALID_PARAMETER );
546 return NULL;
548 if (type & (MEM_COMMIT | MEM_SYSTEM))
549 vprot = VIRTUAL_GetProt( protect ) | VPROT_COMMITTED;
550 else vprot = 0;
552 /* Reserve the memory */
554 if ((type & MEM_RESERVE) || !base)
556 view_size = size + (base ? 0 : granularity_mask + 1);
557 if (type & MEM_SYSTEM)
558 ptr = base;
559 else
560 ptr = (UINT)FILE_dommap( -1, (LPVOID)base, 0, view_size, 0, 0,
561 VIRTUAL_GetUnixProt( vprot ), MAP_PRIVATE );
562 if (ptr == (UINT)-1)
564 SetLastError( ERROR_OUTOFMEMORY );
565 return NULL;
567 if (!base)
569 /* Release the extra memory while keeping the range */
570 /* starting on a 64k boundary. */
572 if (ptr & granularity_mask)
574 UINT extra = granularity_mask + 1 - (ptr & granularity_mask);
575 FILE_munmap( (void *)ptr, 0, extra );
576 ptr += extra;
577 view_size -= extra;
579 if (view_size > size)
580 FILE_munmap( (void *)(ptr + size), 0, view_size - size );
582 else if (ptr != base)
584 /* We couldn't get the address we wanted */
585 FILE_munmap( (void *)ptr, 0, view_size );
586 SetLastError( ERROR_INVALID_ADDRESS );
587 return NULL;
589 if (!(view = VIRTUAL_CreateView( ptr, size, 0, (type & MEM_SYSTEM) ?
590 VFLAG_SYSTEM : 0, vprot, -1 )))
592 FILE_munmap( (void *)ptr, 0, size );
593 SetLastError( ERROR_OUTOFMEMORY );
594 return NULL;
596 VIRTUAL_DEBUG_DUMP_VIEW( view );
597 return (LPVOID)ptr;
600 /* Commit the pages */
602 if (!(view = VIRTUAL_FindView( base )) ||
603 (base + size > view->base + view->size))
605 SetLastError( ERROR_INVALID_ADDRESS );
606 return NULL;
609 if (!VIRTUAL_SetProt( view, base, size, vprot )) return NULL;
610 return (LPVOID)base;
614 /***********************************************************************
615 * VirtualFree (KERNEL32.550)
616 * Release or decommits a region of pages in virtual address space.
618 * RETURNS
619 * TRUE: Success
620 * FALSE: Failure
622 BOOL WINAPI VirtualFree(
623 LPVOID addr, /* [in] Address of region of committed pages */
624 DWORD size, /* [in] Size of region */
625 DWORD type /* [in] Type of operation */
627 FILE_VIEW *view;
628 UINT base;
630 TRACE("%08x %08lx %lx\n",
631 (UINT)addr, size, type );
633 /* Fix the parameters */
635 size = ROUND_SIZE( addr, size );
636 base = ROUND_ADDR( addr );
638 if (!(view = VIRTUAL_FindView( base )) ||
639 (base + size > view->base + view->size))
641 SetLastError( ERROR_INVALID_PARAMETER );
642 return FALSE;
645 /* Compute the protection flags */
647 if ((type != MEM_DECOMMIT) && (type != MEM_RELEASE))
649 SetLastError( ERROR_INVALID_PARAMETER );
650 return FALSE;
653 /* Free the pages */
655 if (type == MEM_RELEASE)
657 if (size || (base != view->base))
659 SetLastError( ERROR_INVALID_PARAMETER );
660 return FALSE;
662 VIRTUAL_DeleteView( view );
663 return TRUE;
666 /* Decommit the pages by remapping zero-pages instead */
668 if (FILE_dommap( -1, (LPVOID)base, 0, size, 0, 0,
669 VIRTUAL_GetUnixProt( 0 ), MAP_PRIVATE|MAP_FIXED )
670 != (LPVOID)base)
671 ERR( "Could not remap pages, expect trouble\n" );
672 return VIRTUAL_SetProt( view, base, size, 0 );
676 /***********************************************************************
677 * VirtualLock (KERNEL32.551)
678 * Locks the specified region of virtual address space
680 * NOTE
681 * Always returns TRUE
683 * RETURNS
684 * TRUE: Success
685 * FALSE: Failure
687 BOOL WINAPI VirtualLock(
688 LPVOID addr, /* [in] Address of first byte of range to lock */
689 DWORD size /* [in] Number of bytes in range to lock */
691 return TRUE;
695 /***********************************************************************
696 * VirtualUnlock (KERNEL32.556)
697 * Unlocks a range of pages in the virtual address space
699 * NOTE
700 * Always returns TRUE
702 * RETURNS
703 * TRUE: Success
704 * FALSE: Failure
706 BOOL WINAPI VirtualUnlock(
707 LPVOID addr, /* [in] Address of first byte of range */
708 DWORD size /* [in] Number of bytes in range */
710 return TRUE;
714 /***********************************************************************
715 * VirtualProtect (KERNEL32.552)
716 * Changes the access protection on a region of committed pages
718 * RETURNS
719 * TRUE: Success
720 * FALSE: Failure
722 BOOL WINAPI VirtualProtect(
723 LPVOID addr, /* [in] Address of region of committed pages */
724 DWORD size, /* [in] Size of region */
725 DWORD new_prot, /* [in] Desired access protection */
726 LPDWORD old_prot /* [out] Address of variable to get old protection */
728 FILE_VIEW *view;
729 UINT base, i;
730 BYTE vprot, *p;
732 TRACE("%08x %08lx %08lx\n",
733 (UINT)addr, size, new_prot );
735 /* Fix the parameters */
737 size = ROUND_SIZE( addr, size );
738 base = ROUND_ADDR( addr );
740 if (!(view = VIRTUAL_FindView( base )) ||
741 (base + size > view->base + view->size))
743 SetLastError( ERROR_INVALID_PARAMETER );
744 return FALSE;
747 /* Make sure all the pages are committed */
749 p = view->prot + ((base - view->base) >> page_shift);
750 for (i = size >> page_shift; i; i--, p++)
752 if (!(*p & VPROT_COMMITTED))
754 SetLastError( ERROR_INVALID_PARAMETER );
755 return FALSE;
759 VIRTUAL_GetWin32Prot( view->prot[0], old_prot, NULL );
760 vprot = VIRTUAL_GetProt( new_prot ) | VPROT_COMMITTED;
761 return VIRTUAL_SetProt( view, base, size, vprot );
765 /***********************************************************************
766 * VirtualProtectEx (KERNEL32.553)
767 * Changes the access protection on a region of committed pages in the
768 * virtual address space of a specified process
770 * RETURNS
771 * TRUE: Success
772 * FALSE: Failure
774 BOOL WINAPI VirtualProtectEx(
775 HANDLE handle, /* [in] Handle of process */
776 LPVOID addr, /* [in] Address of region of committed pages */
777 DWORD size, /* [in] Size of region */
778 DWORD new_prot, /* [in] Desired access protection */
779 LPDWORD old_prot /* [out] Address of variable to get old protection */ )
781 if (MapProcessHandle( handle ) == GetCurrentProcessId())
782 return VirtualProtect( addr, size, new_prot, old_prot );
783 ERR("Unsupported on other process\n");
784 return FALSE;
788 /***********************************************************************
789 * VirtualQuery (KERNEL32.554)
790 * Provides info about a range of pages in virtual address space
792 * RETURNS
793 * Number of bytes returned in information buffer
795 DWORD WINAPI VirtualQuery(
796 LPCVOID addr, /* [in] Address of region */
797 LPMEMORY_BASIC_INFORMATION info, /* [out] Address of info buffer */
798 DWORD len /* [in] Size of buffer */
800 FILE_VIEW *view = VIRTUAL_FirstView;
801 UINT base = ROUND_ADDR( addr );
802 UINT alloc_base = 0;
803 UINT size = 0;
805 /* Find the view containing the address */
807 for (;;)
809 if (!view)
811 size = 0xffff0000 - alloc_base;
812 break;
814 if (view->base > base)
816 size = view->base - alloc_base;
817 view = NULL;
818 break;
820 if (view->base + view->size > base)
822 alloc_base = view->base;
823 size = view->size;
824 break;
826 alloc_base = view->base + view->size;
827 view = view->next;
830 /* Fill the info structure */
832 if (!view)
834 info->State = MEM_FREE;
835 info->Protect = 0;
836 info->AllocationProtect = 0;
837 info->Type = 0;
839 else
841 BYTE vprot = view->prot[(base - alloc_base) >> page_shift];
842 VIRTUAL_GetWin32Prot( vprot, &info->Protect, &info->State );
843 for (size = base - alloc_base; size < view->size; size += page_mask+1)
844 if (view->prot[size >> page_shift] != vprot) break;
845 info->AllocationProtect = view->protect;
846 info->Type = MEM_PRIVATE; /* FIXME */
849 info->BaseAddress = (LPVOID)base;
850 info->AllocationBase = (LPVOID)alloc_base;
851 info->RegionSize = size - (base - alloc_base);
852 return sizeof(*info);
856 /***********************************************************************
857 * VirtualQueryEx (KERNEL32.555)
858 * Provides info about a range of pages in virtual address space of a
859 * specified process
861 * RETURNS
862 * Number of bytes returned in information buffer
864 DWORD WINAPI VirtualQueryEx(
865 HANDLE handle, /* [in] Handle of process */
866 LPCVOID addr, /* [in] Address of region */
867 LPMEMORY_BASIC_INFORMATION info, /* [out] Address of info buffer */
868 DWORD len /* [in] Size of buffer */ )
870 if (MapProcessHandle( handle ) == GetCurrentProcessId())
871 return VirtualQuery( addr, info, len );
872 ERR("Unsupported on other process\n");
873 return 0;
877 /***********************************************************************
878 * IsBadReadPtr (KERNEL32.354)
880 * RETURNS
881 * FALSE: Process has read access to entire block
882 * TRUE: Otherwise
884 BOOL WINAPI IsBadReadPtr(
885 LPCVOID ptr, /* Address of memory block */
886 UINT size ) /* Size of block */
888 __TRY
890 volatile const char *p = ptr;
891 volatile const char *end = p + size - 1;
892 char dummy;
894 while (p < end)
896 dummy = *p;
897 p += page_mask + 1;
899 dummy = *end;
901 __EXCEPT(page_fault) { return TRUE; }
902 __ENDTRY
903 return FALSE;
907 /***********************************************************************
908 * IsBadWritePtr (KERNEL32.357)
910 * RETURNS
911 * FALSE: Process has write access to entire block
912 * TRUE: Otherwise
914 BOOL WINAPI IsBadWritePtr(
915 LPVOID ptr, /* [in] Address of memory block */
916 UINT size ) /* [in] Size of block in bytes */
918 __TRY
920 volatile char *p = ptr;
921 volatile char *end = p + size - 1;
923 while (p < end)
925 *p |= 0;
926 p += page_mask + 1;
928 *end |= 0;
930 __EXCEPT(page_fault) { return TRUE; }
931 __ENDTRY
932 return FALSE;
936 /***********************************************************************
937 * IsBadHugeReadPtr (KERNEL32.352)
938 * RETURNS
939 * FALSE: Process has read access to entire block
940 * TRUE: Otherwise
942 BOOL WINAPI IsBadHugeReadPtr(
943 LPCVOID ptr, /* [in] Address of memory block */
944 UINT size /* [in] Size of block */
946 return IsBadReadPtr( ptr, size );
950 /***********************************************************************
951 * IsBadHugeWritePtr (KERNEL32.353)
952 * RETURNS
953 * FALSE: Process has write access to entire block
954 * TRUE: Otherwise
956 BOOL WINAPI IsBadHugeWritePtr(
957 LPVOID ptr, /* [in] Address of memory block */
958 UINT size /* [in] Size of block */
960 return IsBadWritePtr( ptr, size );
964 /***********************************************************************
965 * IsBadCodePtr (KERNEL32.351)
967 * RETURNS
968 * FALSE: Process has read access to specified memory
969 * TRUE: Otherwise
971 BOOL WINAPI IsBadCodePtr( FARPROC ptr ) /* [in] Address of function */
973 return IsBadReadPtr( ptr, 1 );
977 /***********************************************************************
978 * IsBadStringPtrA (KERNEL32.355)
980 * RETURNS
981 * FALSE: Read access to all bytes in string
982 * TRUE: Else
984 BOOL WINAPI IsBadStringPtrA(
985 LPCSTR str, /* [in] Address of string */
986 UINT max ) /* [in] Maximum size of string */
988 __TRY
990 volatile const char *p = str;
991 while (p < str + max) if (!*p++) break;
993 __EXCEPT(page_fault) { return TRUE; }
994 __ENDTRY
995 return FALSE;
999 /***********************************************************************
1000 * IsBadStringPtrW (KERNEL32.356)
1001 * See IsBadStringPtrA
1003 BOOL WINAPI IsBadStringPtrW( LPCWSTR str, UINT max )
1005 __TRY
1007 volatile const WCHAR *p = str;
1008 while (p < str + max) if (!*p++) break;
1010 __EXCEPT(page_fault) { return TRUE; }
1011 __ENDTRY
1012 return FALSE;
1016 /***********************************************************************
1017 * CreateFileMappingA (KERNEL32.46)
1018 * Creates a named or unnamed file-mapping object for the specified file
1020 * RETURNS
1021 * Handle: Success
1022 * 0: Mapping object does not exist
1023 * NULL: Failure
1025 HANDLE WINAPI CreateFileMappingA(
1026 HFILE hFile, /* [in] Handle of file to map */
1027 SECURITY_ATTRIBUTES *sa, /* [in] Optional security attributes*/
1028 DWORD protect, /* [in] Protection for mapping object */
1029 DWORD size_high, /* [in] High-order 32 bits of object size */
1030 DWORD size_low, /* [in] Low-order 32 bits of object size */
1031 LPCSTR name /* [in] Name of file-mapping object */ )
1033 struct create_mapping_request *req = get_req_buffer();
1034 BYTE vprot;
1036 /* Check parameters */
1038 TRACE("(%x,%p,%08lx,%08lx%08lx,%s)\n",
1039 hFile, sa, protect, size_high, size_low, debugstr_a(name) );
1041 vprot = VIRTUAL_GetProt( protect );
1042 if (protect & SEC_RESERVE)
1044 if (hFile != INVALID_HANDLE_VALUE)
1046 SetLastError( ERROR_INVALID_PARAMETER );
1047 return 0;
1050 else vprot |= VPROT_COMMITTED;
1051 if (protect & SEC_NOCACHE) vprot |= VPROT_NOCACHE;
1053 /* Create the server object */
1055 req->file_handle = hFile;
1056 req->size_high = size_high;
1057 req->size_low = size_low;
1058 req->protect = vprot;
1059 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
1060 server_strcpyAtoW( req->name, name );
1061 SetLastError(0);
1062 server_call( REQ_CREATE_MAPPING );
1063 if (req->handle == -1) return 0;
1064 return req->handle;
1068 /***********************************************************************
1069 * CreateFileMappingW (KERNEL32.47)
1070 * See CreateFileMappingA
1072 HANDLE WINAPI CreateFileMappingW( HFILE hFile, LPSECURITY_ATTRIBUTES sa,
1073 DWORD protect, DWORD size_high,
1074 DWORD size_low, LPCWSTR name )
1076 struct create_mapping_request *req = get_req_buffer();
1077 BYTE vprot;
1079 /* Check parameters */
1081 TRACE("(%x,%p,%08lx,%08lx%08lx,%s)\n",
1082 hFile, sa, protect, size_high, size_low, debugstr_w(name) );
1084 vprot = VIRTUAL_GetProt( protect );
1085 if (protect & SEC_RESERVE)
1087 if (hFile != INVALID_HANDLE_VALUE)
1089 SetLastError( ERROR_INVALID_PARAMETER );
1090 return 0;
1093 else vprot |= VPROT_COMMITTED;
1094 if (protect & SEC_NOCACHE) vprot |= VPROT_NOCACHE;
1096 /* Create the server object */
1098 req->file_handle = hFile;
1099 req->size_high = size_high;
1100 req->size_low = size_low;
1101 req->protect = vprot;
1102 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
1103 server_strcpyW( req->name, name );
1104 SetLastError(0);
1105 server_call( REQ_CREATE_MAPPING );
1106 if (req->handle == -1) return 0;
1107 return req->handle;
1111 /***********************************************************************
1112 * OpenFileMappingA (KERNEL32.397)
1113 * Opens a named file-mapping object.
1115 * RETURNS
1116 * Handle: Success
1117 * NULL: Failure
1119 HANDLE WINAPI OpenFileMappingA(
1120 DWORD access, /* [in] Access mode */
1121 BOOL inherit, /* [in] Inherit flag */
1122 LPCSTR name ) /* [in] Name of file-mapping object */
1124 struct open_mapping_request *req = get_req_buffer();
1126 req->access = access;
1127 req->inherit = inherit;
1128 server_strcpyAtoW( req->name, name );
1129 server_call( REQ_OPEN_MAPPING );
1130 if (req->handle == -1) return 0; /* must return 0 on failure, not -1 */
1131 return req->handle;
1135 /***********************************************************************
1136 * OpenFileMappingW (KERNEL32.398)
1137 * See OpenFileMappingA
1139 HANDLE WINAPI OpenFileMappingW( DWORD access, BOOL inherit, LPCWSTR name)
1141 struct open_mapping_request *req = get_req_buffer();
1143 req->access = access;
1144 req->inherit = inherit;
1145 server_strcpyW( req->name, name );
1146 server_call( REQ_OPEN_MAPPING );
1147 if (req->handle == -1) return 0; /* must return 0 on failure, not -1 */
1148 return req->handle;
1152 /***********************************************************************
1153 * MapViewOfFile (KERNEL32.385)
1154 * Maps a view of a file into the address space
1156 * RETURNS
1157 * Starting address of mapped view
1158 * NULL: Failure
1160 LPVOID WINAPI MapViewOfFile(
1161 HANDLE mapping, /* [in] File-mapping object to map */
1162 DWORD access, /* [in] Access mode */
1163 DWORD offset_high, /* [in] High-order 32 bits of file offset */
1164 DWORD offset_low, /* [in] Low-order 32 bits of file offset */
1165 DWORD count /* [in] Number of bytes to map */
1167 return MapViewOfFileEx( mapping, access, offset_high,
1168 offset_low, count, NULL );
1172 /***********************************************************************
1173 * MapViewOfFileEx (KERNEL32.386)
1174 * Maps a view of a file into the address space
1176 * RETURNS
1177 * Starting address of mapped view
1178 * NULL: Failure
1180 LPVOID WINAPI MapViewOfFileEx(
1181 HANDLE handle, /* [in] File-mapping object to map */
1182 DWORD access, /* [in] Access mode */
1183 DWORD offset_high, /* [in] High-order 32 bits of file offset */
1184 DWORD offset_low, /* [in] Low-order 32 bits of file offset */
1185 DWORD count, /* [in] Number of bytes to map */
1186 LPVOID addr /* [in] Suggested starting address for mapped view */
1188 FILE_VIEW *view;
1189 UINT ptr = (UINT)-1, size = 0;
1190 int flags = MAP_PRIVATE;
1191 int unix_handle = -1;
1192 int prot;
1193 struct get_mapping_info_request *req = get_req_buffer();
1195 /* Check parameters */
1197 if ((offset_low & granularity_mask) ||
1198 (addr && ((UINT)addr & granularity_mask)))
1200 SetLastError( ERROR_INVALID_PARAMETER );
1201 return NULL;
1204 req->handle = handle;
1205 if (server_call_fd( REQ_GET_MAPPING_INFO, -1, &unix_handle )) goto error;
1207 if (req->size_high || offset_high)
1208 ERR("Offsets larger than 4Gb not supported\n");
1210 if ((offset_low >= req->size_low) ||
1211 (count > req->size_low - offset_low))
1213 SetLastError( ERROR_INVALID_PARAMETER );
1214 goto error;
1216 if (count) size = ROUND_SIZE( offset_low, count );
1217 else size = req->size_low - offset_low;
1218 prot = req->protect;
1220 switch(access)
1222 case FILE_MAP_ALL_ACCESS:
1223 case FILE_MAP_WRITE:
1224 case FILE_MAP_WRITE | FILE_MAP_READ:
1225 if (!(prot & VPROT_WRITE))
1227 SetLastError( ERROR_INVALID_PARAMETER );
1228 goto error;
1230 flags = MAP_SHARED;
1231 /* fall through */
1232 case FILE_MAP_READ:
1233 case FILE_MAP_COPY:
1234 case FILE_MAP_COPY | FILE_MAP_READ:
1235 if (prot & VPROT_READ) break;
1236 /* fall through */
1237 default:
1238 SetLastError( ERROR_INVALID_PARAMETER );
1239 goto error;
1242 /* Map the file */
1244 TRACE("handle=%x size=%x offset=%lx\n", handle, size, offset_low );
1246 ptr = (UINT)FILE_dommap( unix_handle, addr, 0, size, 0, offset_low,
1247 VIRTUAL_GetUnixProt( prot ), flags );
1248 if (ptr == (UINT)-1) {
1249 /* KB: Q125713, 25-SEP-1995, "Common File Mapping Problems and
1250 * Platform Differences":
1251 * Windows NT: ERROR_INVALID_PARAMETER
1252 * Windows 95: ERROR_INVALID_ADDRESS.
1253 * FIXME: So should we add a module dependend check here? -MM
1255 if (errno==ENOMEM)
1256 SetLastError( ERROR_OUTOFMEMORY );
1257 else
1258 SetLastError( ERROR_INVALID_PARAMETER );
1259 goto error;
1262 if (!(view = VIRTUAL_CreateView( ptr, size, offset_low, 0, prot, handle )))
1264 SetLastError( ERROR_OUTOFMEMORY );
1265 goto error;
1267 if (unix_handle != -1) close( unix_handle );
1268 return (LPVOID)ptr;
1270 error:
1271 if (unix_handle != -1) close( unix_handle );
1272 if (ptr != (UINT)-1) FILE_munmap( (void *)ptr, 0, size );
1273 return NULL;
1277 /***********************************************************************
1278 * FlushViewOfFile (KERNEL32.262)
1279 * Writes to the disk a byte range within a mapped view of a file
1281 * RETURNS
1282 * TRUE: Success
1283 * FALSE: Failure
1285 BOOL WINAPI FlushViewOfFile(
1286 LPCVOID base, /* [in] Start address of byte range to flush */
1287 DWORD cbFlush /* [in] Number of bytes in range */
1289 FILE_VIEW *view;
1290 UINT addr = ROUND_ADDR( base );
1292 TRACE("FlushViewOfFile at %p for %ld bytes\n",
1293 base, cbFlush );
1295 if (!(view = VIRTUAL_FindView( addr )))
1297 SetLastError( ERROR_INVALID_PARAMETER );
1298 return FALSE;
1300 if (!cbFlush) cbFlush = view->size;
1301 if (!msync( (void *)addr, cbFlush, MS_SYNC )) return TRUE;
1302 SetLastError( ERROR_INVALID_PARAMETER );
1303 return FALSE;
1307 /***********************************************************************
1308 * UnmapViewOfFile (KERNEL32.540)
1309 * Unmaps a mapped view of a file.
1311 * NOTES
1312 * Should addr be an LPCVOID?
1314 * RETURNS
1315 * TRUE: Success
1316 * FALSE: Failure
1318 BOOL WINAPI UnmapViewOfFile(
1319 LPVOID addr /* [in] Address where mapped view begins */
1321 FILE_VIEW *view;
1322 UINT base = ROUND_ADDR( addr );
1323 if (!(view = VIRTUAL_FindView( base )) || (base != view->base))
1325 SetLastError( ERROR_INVALID_PARAMETER );
1326 return FALSE;
1328 VIRTUAL_DeleteView( view );
1329 return TRUE;
1332 /***********************************************************************
1333 * VIRTUAL_MapFileW
1335 * Helper function to map a file to memory:
1336 * name - file name
1337 * [RETURN] ptr - pointer to mapped file
1339 LPVOID VIRTUAL_MapFileW( LPCWSTR name )
1341 HANDLE hFile, hMapping;
1342 LPVOID ptr = NULL;
1344 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
1345 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0);
1346 if (hFile != INVALID_HANDLE_VALUE)
1348 hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
1349 CloseHandle( hFile );
1350 if (hMapping)
1352 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
1353 CloseHandle( hMapping );
1356 return ptr;