2 * Win32 virtual memory functions
4 * Copyright 1997 Alexandre Julliard
11 #ifdef HAVE_SYS_ERRNO_H
12 #include <sys/errno.h>
19 #include <sys/types.h>
20 #ifdef HAVE_SYS_MMAN_H
25 #include "wine/exception.h"
26 #include "wine/unicode.h"
27 #include "wine/library.h"
31 #include "wine/server.h"
32 #include "debugtools.h"
34 DEFAULT_DEBUG_CHANNEL(virtual);
35 DECLARE_DEBUG_CHANNEL(module);
44 struct _FV *next; /* Next view */
45 struct _FV *prev; /* Prev view */
46 void *base; /* Base address */
47 UINT size; /* Size in bytes */
48 UINT flags; /* Allocation flags */
49 HANDLE mapping; /* Handle to the file mapping */
50 HANDLERPROC handlerProc; /* Fault handler */
51 LPVOID handlerArg; /* Fault handler argument */
52 BYTE protect; /* Protection for all pages at allocation time */
53 BYTE prot[1]; /* Protection byte for each page */
57 #define VFLAG_SYSTEM 0x01
58 #define VFLAG_VALLOC 0x02 /* allocated by VirtualAlloc */
60 /* Conversion from VPROT_* to Win32 flags */
61 static const BYTE VIRTUAL_Win32Flags[16] =
63 PAGE_NOACCESS, /* 0 */
64 PAGE_READONLY, /* READ */
65 PAGE_READWRITE, /* WRITE */
66 PAGE_READWRITE, /* READ | WRITE */
67 PAGE_EXECUTE, /* EXEC */
68 PAGE_EXECUTE_READ, /* READ | EXEC */
69 PAGE_EXECUTE_READWRITE, /* WRITE | EXEC */
70 PAGE_EXECUTE_READWRITE, /* READ | WRITE | EXEC */
71 PAGE_WRITECOPY, /* WRITECOPY */
72 PAGE_WRITECOPY, /* READ | WRITECOPY */
73 PAGE_WRITECOPY, /* WRITE | WRITECOPY */
74 PAGE_WRITECOPY, /* READ | WRITE | WRITECOPY */
75 PAGE_EXECUTE_WRITECOPY, /* EXEC | WRITECOPY */
76 PAGE_EXECUTE_WRITECOPY, /* READ | EXEC | WRITECOPY */
77 PAGE_EXECUTE_WRITECOPY, /* WRITE | EXEC | WRITECOPY */
78 PAGE_EXECUTE_WRITECOPY /* READ | WRITE | EXEC | WRITECOPY */
82 static FILE_VIEW *VIRTUAL_FirstView;
83 static CRITICAL_SECTION csVirtual = CRITICAL_SECTION_INIT("csVirtual");
86 /* These are always the same on an i386, and it will be faster this way */
87 # define page_mask 0xfff
88 # define page_shift 12
89 # define page_size 0x1000
91 static UINT page_shift;
92 static UINT page_mask;
93 static UINT page_size;
95 #define granularity_mask 0xffff /* Allocation granularity (usually 64k) */
97 #define ROUND_ADDR(addr,mask) \
98 ((void *)((UINT_PTR)(addr) & ~(mask)))
100 #define ROUND_SIZE(addr,size) \
101 (((UINT)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
103 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
104 if (!TRACE_ON(virtual)); else VIRTUAL_DumpView(view)
106 static LPVOID VIRTUAL_mmap( int fd, LPVOID start, DWORD size, DWORD offset_low,
107 DWORD offset_high, int prot, int flags );
109 /* filter for page-fault exceptions */
110 static WINE_EXCEPTION_FILTER(page_fault)
112 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
113 return EXCEPTION_EXECUTE_HANDLER;
114 return EXCEPTION_CONTINUE_SEARCH;
117 /***********************************************************************
120 static const char *VIRTUAL_GetProtStr( BYTE prot )
122 static char buffer[6];
123 buffer[0] = (prot & VPROT_COMMITTED) ? 'c' : '-';
124 buffer[1] = (prot & VPROT_GUARD) ? 'g' : '-';
125 buffer[2] = (prot & VPROT_READ) ? 'r' : '-';
126 buffer[3] = (prot & VPROT_WRITE) ?
127 ((prot & VPROT_WRITECOPY) ? 'w' : 'W') : '-';
128 buffer[4] = (prot & VPROT_EXEC) ? 'x' : '-';
134 /***********************************************************************
137 static void VIRTUAL_DumpView( FILE_VIEW *view )
140 char *addr = view->base;
141 BYTE prot = view->prot[0];
143 DPRINTF( "View: %p - %p", addr, addr + view->size - 1 );
144 if (view->flags & VFLAG_SYSTEM)
145 DPRINTF( " (system)\n" );
146 else if (view->flags & VFLAG_VALLOC)
147 DPRINTF( " (valloc)\n" );
148 else if (view->mapping)
149 DPRINTF( " %d\n", view->mapping );
151 DPRINTF( " (anonymous)\n");
153 for (count = i = 1; i < view->size >> page_shift; i++, count++)
155 if (view->prot[i] == prot) continue;
156 DPRINTF( " %p - %p %s\n",
157 addr, addr + (count << page_shift) - 1, VIRTUAL_GetProtStr(prot) );
158 addr += (count << page_shift);
159 prot = view->prot[i];
163 DPRINTF( " %p - %p %s\n",
164 addr, addr + (count << page_shift) - 1, VIRTUAL_GetProtStr(prot) );
168 /***********************************************************************
171 void VIRTUAL_Dump(void)
174 DPRINTF( "\nDump of all virtual memory views:\n\n" );
175 EnterCriticalSection(&csVirtual);
176 view = VIRTUAL_FirstView;
179 VIRTUAL_DumpView( view );
182 LeaveCriticalSection(&csVirtual);
186 /***********************************************************************
189 * Find the view containing a given address.
195 static FILE_VIEW *VIRTUAL_FindView( const void *addr ) /* [in] Address */
199 EnterCriticalSection(&csVirtual);
200 view = VIRTUAL_FirstView;
203 if (view->base > addr)
208 if (view->base + view->size > addr) break;
211 LeaveCriticalSection(&csVirtual);
216 /***********************************************************************
219 * Create a new view and add it in the linked list.
221 static FILE_VIEW *VIRTUAL_CreateView( void *base, UINT size, UINT flags,
222 BYTE vprot, HANDLE mapping )
224 FILE_VIEW *view, *prev;
226 /* Create the view structure */
228 assert( !((unsigned int)base & page_mask) );
229 assert( !(size & page_mask) );
231 if (!(view = (FILE_VIEW *)malloc( sizeof(*view) + size - 1 ))) return NULL;
233 view->size = size << page_shift;
235 view->mapping = mapping;
236 view->protect = vprot;
237 view->handlerProc = NULL;
238 memset( view->prot, vprot, size );
240 /* Duplicate the mapping handle */
243 !DuplicateHandle( GetCurrentProcess(), view->mapping,
244 GetCurrentProcess(), &view->mapping,
245 0, FALSE, DUPLICATE_SAME_ACCESS ))
251 /* Insert it in the linked list */
253 EnterCriticalSection(&csVirtual);
254 if (!VIRTUAL_FirstView || (VIRTUAL_FirstView->base > base))
256 view->next = VIRTUAL_FirstView;
258 if (view->next) view->next->prev = view;
259 VIRTUAL_FirstView = view;
263 prev = VIRTUAL_FirstView;
264 while (prev->next && (prev->next->base < base)) prev = prev->next;
265 view->next = prev->next;
267 if (view->next) view->next->prev = view;
270 LeaveCriticalSection(&csVirtual);
271 VIRTUAL_DEBUG_DUMP_VIEW( view );
276 /***********************************************************************
283 static void VIRTUAL_DeleteView(
284 FILE_VIEW *view /* [in] View */
286 if (!(view->flags & VFLAG_SYSTEM))
287 munmap( (void *)view->base, view->size );
288 EnterCriticalSection(&csVirtual);
289 if (view->next) view->next->prev = view->prev;
290 if (view->prev) view->prev->next = view->next;
291 else VIRTUAL_FirstView = view->next;
292 LeaveCriticalSection(&csVirtual);
293 if (view->mapping) NtClose( view->mapping );
298 /***********************************************************************
299 * VIRTUAL_GetUnixProt
301 * Convert page protections to protection for mmap/mprotect.
303 static int VIRTUAL_GetUnixProt( BYTE vprot )
306 if ((vprot & VPROT_COMMITTED) && !(vprot & VPROT_GUARD))
308 if (vprot & VPROT_READ) prot |= PROT_READ;
309 if (vprot & VPROT_WRITE) prot |= PROT_WRITE;
310 if (vprot & VPROT_WRITECOPY) prot |= PROT_WRITE;
311 if (vprot & VPROT_EXEC) prot |= PROT_EXEC;
317 /***********************************************************************
318 * VIRTUAL_GetWin32Prot
320 * Convert page protections to Win32 flags.
325 static void VIRTUAL_GetWin32Prot(
326 BYTE vprot, /* [in] Page protection flags */
327 DWORD *protect, /* [out] Location to store Win32 protection flags */
328 DWORD *state /* [out] Location to store mem state flag */
331 *protect = VIRTUAL_Win32Flags[vprot & 0x0f];
332 /* if (vprot & VPROT_GUARD) *protect |= PAGE_GUARD;*/
333 if (vprot & VPROT_NOCACHE) *protect |= PAGE_NOCACHE;
335 if (vprot & VPROT_GUARD) *protect = PAGE_NOACCESS;
338 if (state) *state = (vprot & VPROT_COMMITTED) ? MEM_COMMIT : MEM_RESERVE;
342 /***********************************************************************
345 * Build page protections from Win32 flags.
348 * Value of page protection flags
350 static BYTE VIRTUAL_GetProt(
351 DWORD protect /* [in] Win32 protection flags */
355 switch(protect & 0xff)
361 vprot = VPROT_READ | VPROT_WRITE;
364 /* MSDN CreateFileMapping() states that if PAGE_WRITECOPY is given,
365 * that the hFile must have been opened with GENERIC_READ and
366 * GENERIC_WRITE access. This is WRONG as tests show that you
367 * only need GENERIC_READ access (at least for Win9x,
368 * FIXME: what about NT?). Thus, we don't put VPROT_WRITE in
369 * PAGE_WRITECOPY and PAGE_EXECUTE_WRITECOPY.
371 vprot = VPROT_READ | VPROT_WRITECOPY;
376 case PAGE_EXECUTE_READ:
377 vprot = VPROT_EXEC | VPROT_READ;
379 case PAGE_EXECUTE_READWRITE:
380 vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITE;
382 case PAGE_EXECUTE_WRITECOPY:
383 /* See comment for PAGE_WRITECOPY above */
384 vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITECOPY;
391 if (protect & PAGE_GUARD) vprot |= VPROT_GUARD;
392 if (protect & PAGE_NOCACHE) vprot |= VPROT_NOCACHE;
397 /***********************************************************************
400 * Change the protection of a range of pages.
406 static BOOL VIRTUAL_SetProt( FILE_VIEW *view, /* [in] Pointer to view */
407 void *base, /* [in] Starting address */
408 UINT size, /* [in] Size in bytes */
409 BYTE vprot ) /* [in] Protections to use */
412 base, (char *)base + size - 1, VIRTUAL_GetProtStr( vprot ) );
414 if (mprotect( base, size, VIRTUAL_GetUnixProt(vprot) ))
415 return FALSE; /* FIXME: last error */
417 memset( view->prot + (((char *)base - (char *)view->base) >> page_shift),
418 vprot, size >> page_shift );
419 VIRTUAL_DEBUG_DUMP_VIEW( view );
424 /***********************************************************************
427 * Create an anonymous mapping aligned to the allocation granularity.
429 static void *anon_mmap_aligned( void *base, unsigned int size, int prot, int flags )
432 unsigned int view_size = size + (base ? 0 : granularity_mask + 1);
434 if ((ptr = wine_anon_mmap( base, view_size, prot, flags )) == (void *)-1)
436 /* KB: Q125713, 25-SEP-1995, "Common File Mapping Problems and
437 * Platform Differences":
438 * Windows NT: ERROR_INVALID_PARAMETER
439 * Windows 95: ERROR_INVALID_ADDRESS.
441 if (errno == ENOMEM) SetLastError( ERROR_OUTOFMEMORY );
444 if (GetVersion() & 0x80000000) /* win95 */
445 SetLastError( ERROR_INVALID_ADDRESS );
447 SetLastError( ERROR_INVALID_PARAMETER );
454 /* Release the extra memory while keeping the range
455 * starting on the granularity boundary. */
456 if ((unsigned int)ptr & granularity_mask)
458 unsigned int extra = granularity_mask + 1 - ((unsigned int)ptr & granularity_mask);
459 munmap( ptr, extra );
460 ptr = (char *)ptr + extra;
463 if (view_size > size)
464 munmap( (char *)ptr + size, view_size - size );
466 else if (ptr != base)
468 /* We couldn't get the address we wanted */
469 munmap( ptr, view_size );
470 SetLastError( ERROR_INVALID_ADDRESS );
477 /***********************************************************************
480 * Map an executable (PE format) image into memory.
482 static LPVOID map_image( HANDLE hmapping, int fd, char *base, DWORD total_size,
483 DWORD header_size, HANDLE shared_file, DWORD shared_size )
485 IMAGE_DOS_HEADER *dos;
486 IMAGE_NT_HEADERS *nt;
487 IMAGE_SECTION_HEADER *sec;
489 DWORD err = GetLastError();
490 FILE_VIEW *view = NULL;
494 SetLastError( ERROR_BAD_EXE_FORMAT ); /* generic error */
496 /* zero-map the whole range */
498 if ((ptr = wine_anon_mmap( base, total_size,
499 PROT_READ | PROT_WRITE | PROT_EXEC, 0 )) == (char *)-1)
501 ptr = wine_anon_mmap( NULL, total_size,
502 PROT_READ | PROT_WRITE | PROT_EXEC, 0 );
503 if (ptr == (char *)-1)
505 ERR_(module)("Not enough memory for module (%ld bytes)\n", total_size);
509 TRACE_(module)( "mapped PE file at %p-%p\n", ptr, ptr + total_size );
511 if (!(view = VIRTUAL_CreateView( ptr, total_size, 0,
512 VPROT_COMMITTED|VPROT_READ|VPROT_WRITE|VPROT_WRITECOPY,
515 munmap( ptr, total_size );
516 SetLastError( ERROR_OUTOFMEMORY );
522 if (VIRTUAL_mmap( fd, ptr, header_size, 0, 0, PROT_READ | PROT_WRITE,
523 MAP_PRIVATE | MAP_FIXED ) == (char *)-1) goto error;
524 dos = (IMAGE_DOS_HEADER *)ptr;
525 nt = (IMAGE_NT_HEADERS *)(ptr + dos->e_lfanew);
526 if ((char *)(nt + 1) > ptr + header_size) goto error;
528 sec = (IMAGE_SECTION_HEADER*)((char*)&nt->OptionalHeader+nt->FileHeader.SizeOfOptionalHeader);
529 if ((char *)(sec + nt->FileHeader.NumberOfSections) > ptr + header_size) goto error;
531 /* check the architecture */
533 if (nt->FileHeader.Machine != IMAGE_FILE_MACHINE_I386)
535 MESSAGE("Trying to load PE image for unsupported architecture (");
536 switch (nt->FileHeader.Machine)
538 case IMAGE_FILE_MACHINE_UNKNOWN: MESSAGE("Unknown"); break;
539 case IMAGE_FILE_MACHINE_I860: MESSAGE("I860"); break;
540 case IMAGE_FILE_MACHINE_R3000: MESSAGE("R3000"); break;
541 case IMAGE_FILE_MACHINE_R4000: MESSAGE("R4000"); break;
542 case IMAGE_FILE_MACHINE_R10000: MESSAGE("R10000"); break;
543 case IMAGE_FILE_MACHINE_ALPHA: MESSAGE("Alpha"); break;
544 case IMAGE_FILE_MACHINE_POWERPC: MESSAGE("PowerPC"); break;
545 default: MESSAGE("Unknown-%04x", nt->FileHeader.Machine); break;
551 /* retrieve the shared sections file */
555 if ((shared_fd = FILE_GetUnixHandle( shared_file, GENERIC_READ )) == -1) goto error;
556 CloseHandle( shared_file ); /* we no longer need it */
560 /* map all the sections */
562 for (i = pos = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
566 /* a few sanity checks */
567 size = sec->VirtualAddress + ROUND_SIZE( sec->VirtualAddress, sec->Misc.VirtualSize );
568 if (sec->VirtualAddress > total_size || size > total_size || size < sec->VirtualAddress)
570 ERR_(module)( "Section %.8s too large (%lx+%lx/%lx)\n",
571 sec->Name, sec->VirtualAddress, sec->Misc.VirtualSize, total_size );
575 if ((sec->Characteristics & IMAGE_SCN_MEM_SHARED) &&
576 (sec->Characteristics & IMAGE_SCN_MEM_WRITE))
578 size = ROUND_SIZE( 0, sec->Misc.VirtualSize );
579 TRACE_(module)( "mapping shared section %.8s at %p off %lx (%x) size %lx (%lx) flags %lx\n",
580 sec->Name, ptr + sec->VirtualAddress,
581 sec->PointerToRawData, pos, sec->SizeOfRawData,
582 size, sec->Characteristics );
583 if (VIRTUAL_mmap( shared_fd, ptr + sec->VirtualAddress, size,
584 pos, 0, PROT_READ|PROT_WRITE|PROT_EXEC,
585 MAP_SHARED|MAP_FIXED ) == (void *)-1)
587 ERR_(module)( "Could not map shared section %.8s\n", sec->Name );
594 if (sec->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA) continue;
595 if (!sec->PointerToRawData || !sec->SizeOfRawData) continue;
597 TRACE_(module)( "mapping section %.8s at %p off %lx size %lx flags %lx\n",
598 sec->Name, ptr + sec->VirtualAddress,
599 sec->PointerToRawData, sec->SizeOfRawData,
600 sec->Characteristics );
602 /* Note: if the section is not aligned properly VIRTUAL_mmap will magically
603 * fall back to read(), so we don't need to check anything here.
605 if (VIRTUAL_mmap( fd, ptr + sec->VirtualAddress, sec->SizeOfRawData,
606 sec->PointerToRawData, 0, PROT_READ|PROT_WRITE|PROT_EXEC,
607 MAP_PRIVATE | MAP_FIXED ) == (void *)-1)
609 ERR_(module)( "Could not map section %.8s, file probably truncated\n", sec->Name );
613 if ((sec->SizeOfRawData < sec->Misc.VirtualSize) && (sec->SizeOfRawData & page_mask))
615 DWORD end = ROUND_SIZE( 0, sec->SizeOfRawData );
616 if (end > sec->Misc.VirtualSize) end = sec->Misc.VirtualSize;
617 TRACE_(module)("clearing %p - %p\n",
618 ptr + sec->VirtualAddress + sec->SizeOfRawData,
619 ptr + sec->VirtualAddress + end );
620 memset( ptr + sec->VirtualAddress + sec->SizeOfRawData, 0,
621 end - sec->SizeOfRawData );
625 SetLastError( err ); /* restore last error */
627 if (shared_fd != -1) close( shared_fd );
631 if (view) VIRTUAL_DeleteView( view );
633 if (shared_fd != -1) close( shared_fd );
634 if (shared_file) CloseHandle( shared_file );
639 /***********************************************************************
643 DECL_GLOBAL_CONSTRUCTOR(VIRTUAL_Init)
645 page_size = getpagesize();
646 page_mask = page_size - 1;
647 /* Make sure we have a power of 2 */
648 assert( !(page_size & page_mask) );
650 while ((1 << page_shift) != page_size) page_shift++;
652 #endif /* page_mask */
655 /***********************************************************************
656 * VIRTUAL_SetFaultHandler
658 BOOL VIRTUAL_SetFaultHandler( LPCVOID addr, HANDLERPROC proc, LPVOID arg )
662 if (!(view = VIRTUAL_FindView( addr ))) return FALSE;
663 view->handlerProc = proc;
664 view->handlerArg = arg;
668 /***********************************************************************
669 * VIRTUAL_HandleFault
671 DWORD VIRTUAL_HandleFault( LPCVOID addr )
673 FILE_VIEW *view = VIRTUAL_FindView( addr );
674 DWORD ret = EXCEPTION_ACCESS_VIOLATION;
678 if (view->handlerProc)
680 if (view->handlerProc(view->handlerArg, addr)) ret = 0; /* handled */
684 BYTE vprot = view->prot[((char *)addr - (char *)view->base) >> page_shift];
685 void *page = (void *)((UINT_PTR)addr & ~page_mask);
686 char *stack = (char *)NtCurrentTeb()->stack_base + SIGNAL_STACK_SIZE + page_mask + 1;
687 if (vprot & VPROT_GUARD)
689 VIRTUAL_SetProt( view, page, page_mask + 1, vprot & ~VPROT_GUARD );
690 ret = STATUS_GUARD_PAGE_VIOLATION;
692 /* is it inside the stack guard pages? */
693 if (((char *)addr >= stack) && ((char *)addr < stack + 2*(page_mask+1)))
694 ret = STATUS_STACK_OVERFLOW;
702 /***********************************************************************
705 * Linux kernels before 2.4.x can support non page-aligned offsets, as
706 * long as the offset is aligned to the filesystem block size. This is
707 * a big performance gain so we want to take advantage of it.
709 * However, when we use 64-bit file support this doesn't work because
710 * glibc rejects unaligned offsets. Also glibc 2.1.3 mmap64 is broken
711 * in that it rounds unaligned offsets down to a page boundary. For
712 * these reasons we do a direct system call here.
714 static void *unaligned_mmap( void *addr, size_t length, unsigned int prot,
715 unsigned int flags, int fd, unsigned int offset_low,
716 unsigned int offset_high )
718 #if defined(linux) && defined(__i386__) && defined(__GNUC__)
719 if (!offset_high && (offset_low & page_mask))
722 __asm__ __volatile__("push %%ebx\n\t"
727 : "0" (90), /* SYS_mmap */
729 if (ret < 0 && ret > -4096)
737 return mmap( addr, length, prot, flags, fd, ((off_t)offset_high << 32) | offset_low );
741 /***********************************************************************
744 * Wrapper for mmap() that handles anonymous mappings portably,
745 * and falls back to read if mmap of a file fails.
747 static LPVOID VIRTUAL_mmap( int fd, LPVOID start, DWORD size,
748 DWORD offset_low, DWORD offset_high, int prot, int flags )
754 if (fd == -1) return wine_anon_mmap( start, size, prot, flags );
756 if ((ret = unaligned_mmap( start, size, prot, flags, fd,
757 offset_low, offset_high )) != (LPVOID)-1) return ret;
759 /* mmap() failed; if this is because the file offset is not */
760 /* page-aligned (EINVAL), or because the underlying filesystem */
761 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
763 if ((errno != ENOEXEC) && (errno != EINVAL) && (errno != ENODEV)) return ret;
764 if (prot & PROT_WRITE)
766 /* We cannot fake shared write mappings */
768 if (flags & MAP_SHARED) return ret;
771 if (!(flags & MAP_PRIVATE)) return ret;
775 /* Reserve the memory with an anonymous mmap */
776 ret = wine_anon_mmap( start, size, PROT_READ | PROT_WRITE, flags );
777 if (ret == (LPVOID)-1) return ret;
778 /* Now read in the file */
779 offset = ((off_t)offset_high << 32) | offset_low;
780 if ((pos = lseek( fd, offset, SEEK_SET )) == -1)
785 read( fd, ret, size );
786 lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
787 mprotect( ret, size, prot ); /* Set the right protection */
792 /***********************************************************************
793 * VirtualAlloc (KERNEL32.@)
794 * Reserves or commits a region of pages in virtual address space
797 * Base address of allocated region of pages
800 LPVOID WINAPI VirtualAlloc(
801 LPVOID addr, /* [in] Address of region to reserve or commit */
802 DWORD size, /* [in] Size of region */
803 DWORD type, /* [in] Type of allocation */
804 DWORD protect)/* [in] Type of access protection */
810 TRACE("%p %08lx %lx %08lx\n", addr, size, type, protect );
812 /* Round parameters to a page boundary */
814 if (size > 0x7fc00000) /* 2Gb - 4Mb */
816 SetLastError( ERROR_OUTOFMEMORY );
821 if (type & MEM_RESERVE) /* Round down to 64k boundary */
822 base = ROUND_ADDR( addr, granularity_mask );
824 base = ROUND_ADDR( addr, page_mask );
825 size = (((UINT_PTR)addr + size + page_mask) & ~page_mask) - (UINT_PTR)base;
826 if ((base <= (char *)granularity_mask) || (base + size < base))
828 /* disallow low 64k and wrap-around */
829 SetLastError( ERROR_INVALID_PARAMETER );
836 size = (size + page_mask) & ~page_mask;
839 if (type & MEM_TOP_DOWN) {
840 /* FIXME: MEM_TOP_DOWN allocates the largest possible address.
841 * Is there _ANY_ way to do it with UNIX mmap()?
843 WARN("MEM_TOP_DOWN ignored\n");
844 type &= ~MEM_TOP_DOWN;
846 /* Compute the alloc type flags */
848 if (!(type & (MEM_COMMIT | MEM_RESERVE | MEM_SYSTEM)) ||
849 (type & ~(MEM_COMMIT | MEM_RESERVE | MEM_SYSTEM)))
851 ERR("called with wrong alloc type flags (%08lx) !\n", type);
852 SetLastError( ERROR_INVALID_PARAMETER );
855 if (type & (MEM_COMMIT | MEM_SYSTEM))
856 vprot = VIRTUAL_GetProt( protect ) | VPROT_COMMITTED;
859 /* Reserve the memory */
861 if ((type & MEM_RESERVE) || !base)
863 if (type & MEM_SYSTEM)
865 if (!(view = VIRTUAL_CreateView( base, size, VFLAG_VALLOC | VFLAG_SYSTEM, vprot, 0 )))
867 SetLastError( ERROR_OUTOFMEMORY );
872 ptr = anon_mmap_aligned( base, size, VIRTUAL_GetUnixProt( vprot ), 0 );
873 if (ptr == (void *)-1) return NULL;
875 if (!(view = VIRTUAL_CreateView( ptr, size, VFLAG_VALLOC, vprot, 0 )))
878 SetLastError( ERROR_OUTOFMEMORY );
884 /* Commit the pages */
886 if (!(view = VIRTUAL_FindView( base )) ||
887 (base + size > (char *)view->base + view->size))
889 SetLastError( ERROR_INVALID_ADDRESS );
893 if (!VIRTUAL_SetProt( view, base, size, vprot )) return NULL;
898 /***********************************************************************
899 * VirtualAllocEx (KERNEL32.@)
901 * Seems to be just as VirtualAlloc, but with process handle.
903 LPVOID WINAPI VirtualAllocEx(
904 HANDLE hProcess, /* [in] Handle of process to do mem operation */
905 LPVOID addr, /* [in] Address of region to reserve or commit */
906 DWORD size, /* [in] Size of region */
907 DWORD type, /* [in] Type of allocation */
908 DWORD protect /* [in] Type of access protection */
910 if (MapProcessHandle( hProcess ) == GetCurrentProcessId())
911 return VirtualAlloc( addr, size, type, protect );
912 ERR("Unsupported on other process\n");
917 /***********************************************************************
918 * VirtualFree (KERNEL32.@)
919 * Release or decommits a region of pages in virtual address space.
925 BOOL WINAPI VirtualFree(
926 LPVOID addr, /* [in] Address of region of committed pages */
927 DWORD size, /* [in] Size of region */
928 DWORD type /* [in] Type of operation */
933 TRACE("%p %08lx %lx\n", addr, size, type );
935 /* Fix the parameters */
937 size = ROUND_SIZE( addr, size );
938 base = ROUND_ADDR( addr, page_mask );
940 if (!(view = VIRTUAL_FindView( base )) ||
941 (base + size > (char *)view->base + view->size) ||
942 !(view->flags & VFLAG_VALLOC))
944 SetLastError( ERROR_INVALID_PARAMETER );
948 /* Compute the protection flags */
950 if ((type != MEM_DECOMMIT) && (type != MEM_RELEASE))
952 ERR("called with wrong free type flags (%08lx) !\n", type);
953 SetLastError( ERROR_INVALID_PARAMETER );
959 if (type == MEM_RELEASE)
961 if (size || (base != view->base))
963 SetLastError( ERROR_INVALID_PARAMETER );
966 VIRTUAL_DeleteView( view );
970 /* Decommit the pages by remapping zero-pages instead */
972 if (wine_anon_mmap( (LPVOID)base, size, VIRTUAL_GetUnixProt(0), MAP_FIXED ) != (LPVOID)base)
973 ERR( "Could not remap pages, expect trouble\n" );
974 return VIRTUAL_SetProt( view, base, size, 0 );
978 /***********************************************************************
979 * VirtualLock (KERNEL32.@)
980 * Locks the specified region of virtual address space
983 * Always returns TRUE
989 BOOL WINAPI VirtualLock(
990 LPVOID addr, /* [in] Address of first byte of range to lock */
991 DWORD size /* [in] Number of bytes in range to lock */
997 /***********************************************************************
998 * VirtualUnlock (KERNEL32.@)
999 * Unlocks a range of pages in the virtual address space
1002 * Always returns TRUE
1008 BOOL WINAPI VirtualUnlock(
1009 LPVOID addr, /* [in] Address of first byte of range */
1010 DWORD size /* [in] Number of bytes in range */
1016 /***********************************************************************
1017 * VirtualProtect (KERNEL32.@)
1018 * Changes the access protection on a region of committed pages
1024 BOOL WINAPI VirtualProtect(
1025 LPVOID addr, /* [in] Address of region of committed pages */
1026 DWORD size, /* [in] Size of region */
1027 DWORD new_prot, /* [in] Desired access protection */
1028 LPDWORD old_prot /* [out] Address of variable to get old protection */
1036 TRACE("%p %08lx %08lx\n", addr, size, new_prot );
1038 /* Fix the parameters */
1040 size = ROUND_SIZE( addr, size );
1041 base = ROUND_ADDR( addr, page_mask );
1043 if (!(view = VIRTUAL_FindView( base )) ||
1044 (base + size > (char *)view->base + view->size))
1046 SetLastError( ERROR_INVALID_PARAMETER );
1050 /* Make sure all the pages are committed */
1052 p = view->prot + ((base - (char *)view->base) >> page_shift);
1053 VIRTUAL_GetWin32Prot( *p, &prot, NULL );
1054 for (i = size >> page_shift; i; i--, p++)
1056 if (!(*p & VPROT_COMMITTED))
1058 SetLastError( ERROR_INVALID_PARAMETER );
1063 if (old_prot) *old_prot = prot;
1064 vprot = VIRTUAL_GetProt( new_prot ) | VPROT_COMMITTED;
1065 return VIRTUAL_SetProt( view, base, size, vprot );
1069 /***********************************************************************
1070 * VirtualProtectEx (KERNEL32.@)
1071 * Changes the access protection on a region of committed pages in the
1072 * virtual address space of a specified process
1078 BOOL WINAPI VirtualProtectEx(
1079 HANDLE handle, /* [in] Handle of process */
1080 LPVOID addr, /* [in] Address of region of committed pages */
1081 DWORD size, /* [in] Size of region */
1082 DWORD new_prot, /* [in] Desired access protection */
1083 LPDWORD old_prot /* [out] Address of variable to get old protection */ )
1085 if (MapProcessHandle( handle ) == GetCurrentProcessId())
1086 return VirtualProtect( addr, size, new_prot, old_prot );
1087 ERR("Unsupported on other process\n");
1092 /***********************************************************************
1093 * VirtualQuery (KERNEL32.@)
1094 * Provides info about a range of pages in virtual address space
1097 * Number of bytes returned in information buffer
1098 * or 0 if addr is >= 0xc0000000 (kernel space).
1100 DWORD WINAPI VirtualQuery(
1101 LPCVOID addr, /* [in] Address of region */
1102 LPMEMORY_BASIC_INFORMATION info, /* [out] Address of info buffer */
1103 DWORD len /* [in] Size of buffer */
1106 char *base, *alloc_base = 0;
1109 if (addr >= (void*)0xc0000000) return 0;
1111 base = ROUND_ADDR( addr, page_mask );
1113 /* Find the view containing the address */
1115 EnterCriticalSection(&csVirtual);
1116 view = VIRTUAL_FirstView;
1121 size = (char *)0xffff0000 - alloc_base;
1124 if ((char *)view->base > base)
1126 size = (char *)view->base - alloc_base;
1130 if ((char *)view->base + view->size > base)
1132 alloc_base = view->base;
1136 alloc_base = (char *)view->base + view->size;
1139 LeaveCriticalSection(&csVirtual);
1141 /* Fill the info structure */
1145 info->State = MEM_FREE;
1147 info->AllocationProtect = 0;
1152 BYTE vprot = view->prot[(base - alloc_base) >> page_shift];
1153 VIRTUAL_GetWin32Prot( vprot, &info->Protect, &info->State );
1154 for (size = base - alloc_base; size < view->size; size += page_mask+1)
1155 if (view->prot[size >> page_shift] != vprot) break;
1156 info->AllocationProtect = view->protect;
1157 info->Type = MEM_PRIVATE; /* FIXME */
1160 info->BaseAddress = (LPVOID)base;
1161 info->AllocationBase = (LPVOID)alloc_base;
1162 info->RegionSize = size - (base - alloc_base);
1163 return sizeof(*info);
1167 /***********************************************************************
1168 * VirtualQueryEx (KERNEL32.@)
1169 * Provides info about a range of pages in virtual address space of a
1173 * Number of bytes returned in information buffer
1175 DWORD WINAPI VirtualQueryEx(
1176 HANDLE handle, /* [in] Handle of process */
1177 LPCVOID addr, /* [in] Address of region */
1178 LPMEMORY_BASIC_INFORMATION info, /* [out] Address of info buffer */
1179 DWORD len /* [in] Size of buffer */ )
1181 if (MapProcessHandle( handle ) == GetCurrentProcessId())
1182 return VirtualQuery( addr, info, len );
1183 ERR("Unsupported on other process\n");
1188 /***********************************************************************
1189 * IsBadReadPtr (KERNEL32.@)
1192 * FALSE: Process has read access to entire block
1195 BOOL WINAPI IsBadReadPtr(
1196 LPCVOID ptr, /* [in] Address of memory block */
1197 UINT size ) /* [in] Size of block */
1199 if (!size) return FALSE; /* handle 0 size case w/o reference */
1202 volatile const char *p = ptr;
1206 while (count > page_size)
1213 dummy = p[count - 1];
1215 __EXCEPT(page_fault) { return TRUE; }
1221 /***********************************************************************
1222 * IsBadWritePtr (KERNEL32.@)
1225 * FALSE: Process has write access to entire block
1228 BOOL WINAPI IsBadWritePtr(
1229 LPVOID ptr, /* [in] Address of memory block */
1230 UINT size ) /* [in] Size of block in bytes */
1232 if (!size) return FALSE; /* handle 0 size case w/o reference */
1235 volatile char *p = ptr;
1238 while (count > page_size)
1247 __EXCEPT(page_fault) { return TRUE; }
1253 /***********************************************************************
1254 * IsBadHugeReadPtr (KERNEL32.@)
1256 * FALSE: Process has read access to entire block
1259 BOOL WINAPI IsBadHugeReadPtr(
1260 LPCVOID ptr, /* [in] Address of memory block */
1261 UINT size /* [in] Size of block */
1263 return IsBadReadPtr( ptr, size );
1267 /***********************************************************************
1268 * IsBadHugeWritePtr (KERNEL32.@)
1270 * FALSE: Process has write access to entire block
1273 BOOL WINAPI IsBadHugeWritePtr(
1274 LPVOID ptr, /* [in] Address of memory block */
1275 UINT size /* [in] Size of block */
1277 return IsBadWritePtr( ptr, size );
1281 /***********************************************************************
1282 * IsBadCodePtr (KERNEL32.@)
1285 * FALSE: Process has read access to specified memory
1288 BOOL WINAPI IsBadCodePtr( FARPROC ptr ) /* [in] Address of function */
1290 return IsBadReadPtr( ptr, 1 );
1294 /***********************************************************************
1295 * IsBadStringPtrA (KERNEL32.@)
1298 * FALSE: Read access to all bytes in string
1301 BOOL WINAPI IsBadStringPtrA(
1302 LPCSTR str, /* [in] Address of string */
1303 UINT max ) /* [in] Maximum size of string */
1307 volatile const char *p = str;
1308 while (p != str + max) if (!*p++) break;
1310 __EXCEPT(page_fault) { return TRUE; }
1316 /***********************************************************************
1317 * IsBadStringPtrW (KERNEL32.@)
1318 * See IsBadStringPtrA
1320 BOOL WINAPI IsBadStringPtrW( LPCWSTR str, UINT max )
1324 volatile const WCHAR *p = str;
1325 while (p != str + max) if (!*p++) break;
1327 __EXCEPT(page_fault) { return TRUE; }
1333 /***********************************************************************
1334 * CreateFileMappingA (KERNEL32.@)
1335 * Creates a named or unnamed file-mapping object for the specified file
1339 * 0: Mapping object does not exist
1342 HANDLE WINAPI CreateFileMappingA(
1343 HANDLE hFile, /* [in] Handle of file to map */
1344 SECURITY_ATTRIBUTES *sa, /* [in] Optional security attributes*/
1345 DWORD protect, /* [in] Protection for mapping object */
1346 DWORD size_high, /* [in] High-order 32 bits of object size */
1347 DWORD size_low, /* [in] Low-order 32 bits of object size */
1348 LPCSTR name /* [in] Name of file-mapping object */ )
1352 DWORD len = name ? MultiByteToWideChar( CP_ACP, 0, name, strlen(name), NULL, 0 ) : 0;
1354 /* Check parameters */
1356 TRACE("(%x,%p,%08lx,%08lx%08lx,%s)\n",
1357 hFile, sa, protect, size_high, size_low, debugstr_a(name) );
1361 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1364 vprot = VIRTUAL_GetProt( protect );
1365 if (protect & SEC_RESERVE)
1367 if (hFile != INVALID_HANDLE_VALUE)
1369 SetLastError( ERROR_INVALID_PARAMETER );
1373 else vprot |= VPROT_COMMITTED;
1374 if (protect & SEC_NOCACHE) vprot |= VPROT_NOCACHE;
1375 if (protect & SEC_IMAGE) vprot |= VPROT_IMAGE;
1377 /* Create the server object */
1379 if (hFile == INVALID_HANDLE_VALUE) hFile = 0;
1380 SERVER_START_VAR_REQ( create_mapping, len * sizeof(WCHAR) )
1382 req->file_handle = hFile;
1383 req->size_high = size_high;
1384 req->size_low = size_low;
1385 req->protect = vprot;
1386 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
1387 if (len) MultiByteToWideChar( CP_ACP, 0, name, strlen(name), server_data_ptr(req), len );
1397 /***********************************************************************
1398 * CreateFileMappingW (KERNEL32.@)
1399 * See CreateFileMappingA
1401 HANDLE WINAPI CreateFileMappingW( HANDLE hFile, LPSECURITY_ATTRIBUTES sa,
1402 DWORD protect, DWORD size_high,
1403 DWORD size_low, LPCWSTR name )
1407 DWORD len = name ? strlenW(name) : 0;
1409 /* Check parameters */
1411 TRACE("(%x,%p,%08lx,%08lx%08lx,%s)\n",
1412 hFile, sa, protect, size_high, size_low, debugstr_w(name) );
1416 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1420 vprot = VIRTUAL_GetProt( protect );
1421 if (protect & SEC_RESERVE)
1423 if (hFile != INVALID_HANDLE_VALUE)
1425 SetLastError( ERROR_INVALID_PARAMETER );
1429 else vprot |= VPROT_COMMITTED;
1430 if (protect & SEC_NOCACHE) vprot |= VPROT_NOCACHE;
1431 if (protect & SEC_IMAGE) vprot |= VPROT_IMAGE;
1433 /* Create the server object */
1435 if (hFile == INVALID_HANDLE_VALUE) hFile = 0;
1436 SERVER_START_VAR_REQ( create_mapping, len * sizeof(WCHAR) )
1438 req->file_handle = hFile;
1439 req->size_high = size_high;
1440 req->size_low = size_low;
1441 req->protect = vprot;
1442 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
1443 memcpy( server_data_ptr(req), name, len * sizeof(WCHAR) );
1453 /***********************************************************************
1454 * OpenFileMappingA (KERNEL32.@)
1455 * Opens a named file-mapping object.
1461 HANDLE WINAPI OpenFileMappingA(
1462 DWORD access, /* [in] Access mode */
1463 BOOL inherit, /* [in] Inherit flag */
1464 LPCSTR name ) /* [in] Name of file-mapping object */
1467 DWORD len = name ? MultiByteToWideChar( CP_ACP, 0, name, strlen(name), NULL, 0 ) : 0;
1470 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1473 SERVER_START_VAR_REQ( open_mapping, len * sizeof(WCHAR) )
1475 req->access = access;
1476 req->inherit = inherit;
1477 if (len) MultiByteToWideChar( CP_ACP, 0, name, strlen(name), server_data_ptr(req), len );
1486 /***********************************************************************
1487 * OpenFileMappingW (KERNEL32.@)
1488 * See OpenFileMappingA
1490 HANDLE WINAPI OpenFileMappingW( DWORD access, BOOL inherit, LPCWSTR name)
1493 DWORD len = name ? strlenW(name) : 0;
1496 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1499 SERVER_START_VAR_REQ( open_mapping, len * sizeof(WCHAR) )
1501 req->access = access;
1502 req->inherit = inherit;
1503 memcpy( server_data_ptr(req), name, len * sizeof(WCHAR) );
1512 /***********************************************************************
1513 * MapViewOfFile (KERNEL32.@)
1514 * Maps a view of a file into the address space
1517 * Starting address of mapped view
1520 LPVOID WINAPI MapViewOfFile(
1521 HANDLE mapping, /* [in] File-mapping object to map */
1522 DWORD access, /* [in] Access mode */
1523 DWORD offset_high, /* [in] High-order 32 bits of file offset */
1524 DWORD offset_low, /* [in] Low-order 32 bits of file offset */
1525 DWORD count /* [in] Number of bytes to map */
1527 return MapViewOfFileEx( mapping, access, offset_high,
1528 offset_low, count, NULL );
1532 /***********************************************************************
1533 * MapViewOfFileEx (KERNEL32.@)
1534 * Maps a view of a file into the address space
1537 * Starting address of mapped view
1540 LPVOID WINAPI MapViewOfFileEx(
1541 HANDLE handle, /* [in] File-mapping object to map */
1542 DWORD access, /* [in] Access mode */
1543 DWORD offset_high, /* [in] High-order 32 bits of file offset */
1544 DWORD offset_low, /* [in] Low-order 32 bits of file offset */
1545 DWORD count, /* [in] Number of bytes to map */
1546 LPVOID addr /* [in] Suggested starting address for mapped view */
1550 int flags = MAP_PRIVATE;
1551 int unix_handle = -1;
1553 void *base, *ptr = (void *)-1, *ret;
1554 DWORD size_low, size_high, header_size, shared_size;
1557 /* Check parameters */
1559 if ((offset_low & granularity_mask) ||
1560 (addr && ((UINT_PTR)addr & granularity_mask)))
1562 SetLastError( ERROR_INVALID_PARAMETER );
1566 SERVER_START_REQ( get_mapping_info )
1568 req->handle = handle;
1569 res = SERVER_CALL_ERR();
1570 prot = req->protect;
1572 size_low = req->size_low;
1573 size_high = req->size_high;
1574 header_size = req->header_size;
1575 shared_file = req->shared_file;
1576 shared_size = req->shared_size;
1579 if (res) goto error;
1581 if ((unix_handle = FILE_GetUnixHandle( handle, 0 )) == -1) goto error;
1583 if (prot & VPROT_IMAGE)
1584 return map_image( handle, unix_handle, base, size_low, header_size,
1585 shared_file, shared_size );
1589 ERR("Sizes larger than 4Gb not supported\n");
1591 if ((offset_low >= size_low) ||
1592 (count > size_low - offset_low))
1594 SetLastError( ERROR_INVALID_PARAMETER );
1597 if (count) size = ROUND_SIZE( offset_low, count );
1598 else size = size_low - offset_low;
1602 case FILE_MAP_ALL_ACCESS:
1603 case FILE_MAP_WRITE:
1604 case FILE_MAP_WRITE | FILE_MAP_READ:
1605 if (!(prot & VPROT_WRITE))
1607 SetLastError( ERROR_INVALID_PARAMETER );
1614 case FILE_MAP_COPY | FILE_MAP_READ:
1615 if (prot & VPROT_READ) break;
1618 SetLastError( ERROR_INVALID_PARAMETER );
1622 /* FIXME: If a mapping is created with SEC_RESERVE and a process,
1623 * which has a view of this mapping commits some pages, they will
1624 * appear commited in all other processes, which have the same
1625 * view created. Since we don`t support this yet, we create the
1626 * whole mapping commited.
1628 prot |= VPROT_COMMITTED;
1630 /* Reserve a properly aligned area */
1632 if ((ptr = anon_mmap_aligned( addr, size, PROT_NONE, 0 )) == (void *)-1) goto error;
1636 TRACE("handle=%x size=%x offset=%lx\n", handle, size, offset_low );
1638 ret = VIRTUAL_mmap( unix_handle, ptr, size, offset_low, offset_high,
1639 VIRTUAL_GetUnixProt( prot ), flags | MAP_FIXED );
1642 ERR( "VIRTUAL_mmap %p %x %lx%08lx failed\n", ptr, size, offset_high, offset_low );
1646 if (!(view = VIRTUAL_CreateView( ptr, size, 0, prot, handle )))
1648 SetLastError( ERROR_OUTOFMEMORY );
1651 if (unix_handle != -1) close( unix_handle );
1655 if (unix_handle != -1) close( unix_handle );
1656 if (ptr != (void *)-1) munmap( ptr, size );
1661 /***********************************************************************
1662 * FlushViewOfFile (KERNEL32.@)
1663 * Writes to the disk a byte range within a mapped view of a file
1669 BOOL WINAPI FlushViewOfFile(
1670 LPCVOID base, /* [in] Start address of byte range to flush */
1671 DWORD cbFlush /* [in] Number of bytes in range */
1674 void *addr = ROUND_ADDR( base, page_mask );
1676 TRACE("FlushViewOfFile at %p for %ld bytes\n",
1679 if (!(view = VIRTUAL_FindView( addr )))
1681 SetLastError( ERROR_INVALID_PARAMETER );
1684 if (!cbFlush) cbFlush = view->size;
1685 if (!msync( addr, cbFlush, MS_SYNC )) return TRUE;
1686 SetLastError( ERROR_INVALID_PARAMETER );
1691 /***********************************************************************
1692 * UnmapViewOfFile (KERNEL32.@)
1693 * Unmaps a mapped view of a file.
1696 * Should addr be an LPCVOID?
1702 BOOL WINAPI UnmapViewOfFile(
1703 LPVOID addr /* [in] Address where mapped view begins */
1706 void *base = ROUND_ADDR( addr, page_mask );
1707 if (!(view = VIRTUAL_FindView( base )) || (base != view->base))
1709 SetLastError( ERROR_INVALID_PARAMETER );
1712 VIRTUAL_DeleteView( view );