2 * Win32 virtual memory functions
4 * Copyright 1997 Alexandre Julliard
14 #include <sys/types.h>
39 struct _FV *next; /* Next view */
40 struct _FV *prev; /* Prev view */
41 UINT base; /* Base address */
42 UINT size; /* Size in bytes */
43 UINT flags; /* Allocation flags */
44 UINT offset; /* Offset from start of mapped file */
45 HANDLE mapping; /* Handle to the file mapping */
46 HANDLERPROC handlerProc; /* Fault handler */
47 LPVOID handlerArg; /* Fault handler argument */
48 BYTE protect; /* Protection for all pages at allocation time */
49 BYTE prot[1]; /* Protection byte for each page */
53 #define VFLAG_SYSTEM 0x01
55 /* Conversion from VPROT_* to Win32 flags */
56 static const BYTE VIRTUAL_Win32Flags[16] =
58 PAGE_NOACCESS, /* 0 */
59 PAGE_READONLY, /* READ */
60 PAGE_READWRITE, /* WRITE */
61 PAGE_READWRITE, /* READ | WRITE */
62 PAGE_EXECUTE, /* EXEC */
63 PAGE_EXECUTE_READ, /* READ | EXEC */
64 PAGE_EXECUTE_READWRITE, /* WRITE | EXEC */
65 PAGE_EXECUTE_READWRITE, /* READ | WRITE | EXEC */
66 PAGE_WRITECOPY, /* WRITECOPY */
67 PAGE_WRITECOPY, /* READ | WRITECOPY */
68 PAGE_WRITECOPY, /* WRITE | WRITECOPY */
69 PAGE_WRITECOPY, /* READ | WRITE | WRITECOPY */
70 PAGE_EXECUTE_WRITECOPY, /* EXEC | WRITECOPY */
71 PAGE_EXECUTE_WRITECOPY, /* READ | EXEC | WRITECOPY */
72 PAGE_EXECUTE_WRITECOPY, /* WRITE | EXEC | WRITECOPY */
73 PAGE_EXECUTE_WRITECOPY /* READ | WRITE | EXEC | WRITECOPY */
77 static FILE_VIEW *VIRTUAL_FirstView;
80 /* These are always the same on an i386, and it will be faster this way */
81 # define page_mask 0xfff
82 # define page_shift 12
83 # define granularity_mask 0xffff
85 static UINT page_shift;
86 static UINT page_mask;
87 static UINT granularity_mask; /* 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)
99 /***********************************************************************
102 static const char *VIRTUAL_GetProtStr( BYTE prot )
104 static char buffer[6];
105 buffer[0] = (prot & VPROT_COMMITTED) ? 'c' : '-';
106 buffer[1] = (prot & VPROT_GUARD) ? 'g' : '-';
107 buffer[2] = (prot & VPROT_READ) ? 'r' : '-';
108 buffer[3] = (prot & VPROT_WRITE) ?
109 ((prot & VPROT_WRITECOPY) ? 'w' : 'W') : '-';
110 buffer[4] = (prot & VPROT_EXEC) ? 'x' : '-';
116 /***********************************************************************
119 static void VIRTUAL_DumpView( FILE_VIEW *view )
122 UINT addr = view->base;
123 BYTE prot = view->prot[0];
125 DUMP( "View: %08x - %08x%s",
126 view->base, view->base + view->size - 1,
127 (view->flags & VFLAG_SYSTEM) ? " (system)" : "" );
129 DUMP( " %d @ %08x\n", view->mapping, view->offset );
131 DUMP( " (anonymous)\n");
133 for (count = i = 1; i < view->size >> page_shift; i++, count++)
135 if (view->prot[i] == prot) continue;
136 DUMP( " %08x - %08x %s\n",
137 addr, addr + (count << page_shift) - 1,
138 VIRTUAL_GetProtStr(prot) );
139 addr += (count << page_shift);
140 prot = view->prot[i];
144 DUMP( " %08x - %08x %s\n",
145 addr, addr + (count << page_shift) - 1,
146 VIRTUAL_GetProtStr(prot) );
150 /***********************************************************************
153 void VIRTUAL_Dump(void)
155 FILE_VIEW *view = VIRTUAL_FirstView;
156 DUMP( "\nDump of all virtual memory views:\n\n" );
159 VIRTUAL_DumpView( view );
165 /***********************************************************************
168 * Find the view containing a given address.
174 static FILE_VIEW *VIRTUAL_FindView(
175 UINT addr /* [in] Address */
177 FILE_VIEW *view = VIRTUAL_FirstView;
180 if (view->base > addr) return NULL;
181 if (view->base + view->size > addr) return view;
188 /***********************************************************************
191 * Create a new view and add it in the linked list.
193 static FILE_VIEW *VIRTUAL_CreateView( UINT base, UINT size, UINT offset,
194 UINT flags, BYTE vprot,
197 FILE_VIEW *view, *prev;
199 /* Create the view structure */
201 assert( !(base & page_mask) );
202 assert( !(size & page_mask) );
204 if (!(view = (FILE_VIEW *)malloc( sizeof(*view) + size - 1 ))) return NULL;
206 view->size = size << page_shift;
208 view->offset = offset;
209 view->mapping = mapping;
210 view->protect = vprot;
211 view->handlerProc = NULL;
212 memset( view->prot, vprot, size );
214 /* Duplicate the mapping handle */
216 if ((view->mapping != -1) &&
217 !DuplicateHandle( GetCurrentProcess(), view->mapping,
218 GetCurrentProcess(), &view->mapping,
219 0, FALSE, DUPLICATE_SAME_ACCESS ))
225 /* Insert it in the linked list */
227 if (!VIRTUAL_FirstView || (VIRTUAL_FirstView->base > base))
229 view->next = VIRTUAL_FirstView;
231 if (view->next) view->next->prev = view;
232 VIRTUAL_FirstView = view;
236 prev = VIRTUAL_FirstView;
237 while (prev->next && (prev->next->base < base)) prev = prev->next;
238 view->next = prev->next;
240 if (view->next) view->next->prev = view;
243 VIRTUAL_DEBUG_DUMP_VIEW( view );
248 /***********************************************************************
255 static void VIRTUAL_DeleteView(
256 FILE_VIEW *view /* [in] View */
258 FILE_munmap( (void *)view->base, 0, view->size );
259 if (view->next) view->next->prev = view->prev;
260 if (view->prev) view->prev->next = view->next;
261 else VIRTUAL_FirstView = view->next;
262 if (view->mapping) CloseHandle( view->mapping );
267 /***********************************************************************
268 * VIRTUAL_GetUnixProt
270 * Convert page protections to protection for mmap/mprotect.
272 static int VIRTUAL_GetUnixProt( BYTE vprot )
275 if ((vprot & VPROT_COMMITTED) && !(vprot & VPROT_GUARD))
277 if (vprot & VPROT_READ) prot |= PROT_READ;
278 if (vprot & VPROT_WRITE) prot |= PROT_WRITE;
279 if (vprot & VPROT_EXEC) prot |= PROT_EXEC;
285 /***********************************************************************
286 * VIRTUAL_GetWin32Prot
288 * Convert page protections to Win32 flags.
293 static void VIRTUAL_GetWin32Prot(
294 BYTE vprot, /* [in] Page protection flags */
295 DWORD *protect, /* [out] Location to store Win32 protection flags */
296 DWORD *state /* [out] Location to store mem state flag */
299 *protect = VIRTUAL_Win32Flags[vprot & 0x0f];
300 /* if (vprot & VPROT_GUARD) *protect |= PAGE_GUARD;*/
301 if (vprot & VPROT_NOCACHE) *protect |= PAGE_NOCACHE;
303 if (vprot & VPROT_GUARD) *protect = PAGE_NOACCESS;
306 if (state) *state = (vprot & VPROT_COMMITTED) ? MEM_COMMIT : MEM_RESERVE;
310 /***********************************************************************
313 * Build page protections from Win32 flags.
316 * Value of page protection flags
318 static BYTE VIRTUAL_GetProt(
319 DWORD protect /* [in] Win32 protection flags */
323 switch(protect & 0xff)
329 vprot = VPROT_READ | VPROT_WRITE;
332 vprot = VPROT_READ | VPROT_WRITE | VPROT_WRITECOPY;
337 case PAGE_EXECUTE_READ:
338 vprot = VPROT_EXEC | VPROT_READ;
340 case PAGE_EXECUTE_READWRITE:
341 vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITE | VPROT_WRITECOPY;
343 case PAGE_EXECUTE_WRITECOPY:
344 vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITE;
351 if (protect & PAGE_GUARD) vprot |= VPROT_GUARD;
352 if (protect & PAGE_NOCACHE) vprot |= VPROT_NOCACHE;
357 /***********************************************************************
360 * Change the protection of a range of pages.
366 static BOOL VIRTUAL_SetProt(
367 FILE_VIEW *view, /* [in] Pointer to view */
368 UINT base, /* [in] Starting address */
369 UINT size, /* [in] Size in bytes */
370 BYTE vprot /* [in] Protections to use */
372 TRACE(virtual, "%08x-%08x %s\n",
373 base, base + size - 1, VIRTUAL_GetProtStr( vprot ) );
375 if (mprotect( (void *)base, size, VIRTUAL_GetUnixProt(vprot) ))
376 return FALSE; /* FIXME: last error */
378 memset( view->prot + ((base - view->base) >> page_shift),
379 vprot, size >> page_shift );
380 VIRTUAL_DEBUG_DUMP_VIEW( view );
385 /***********************************************************************
388 * Check that all pages in a range have the given flags.
394 static BOOL VIRTUAL_CheckFlags(
395 UINT base, /* [in] Starting address */
396 UINT size, /* [in] Size in bytes */
397 BYTE flags /* [in] Flags to check for */
402 if (!size) return TRUE;
403 if (!(view = VIRTUAL_FindView( base ))) return FALSE;
404 if (view->base + view->size < base + size) return FALSE;
405 page = (base - view->base) >> page_shift;
406 size = ROUND_SIZE( base, size ) >> page_shift;
407 while (size--) if ((view->prot[page++] & flags) != flags) return FALSE;
412 /***********************************************************************
415 BOOL VIRTUAL_Init(void)
420 # ifdef HAVE_GETPAGESIZE
421 page_size = getpagesize();
424 page_size = sysconf(_SC_PAGESIZE);
426 # error Cannot get the page size on this platform
429 page_mask = page_size - 1;
430 granularity_mask = 0xffff; /* hard-coded for now */
431 /* Make sure we have a power of 2 */
432 assert( !(page_size & page_mask) );
434 while ((1 << page_shift) != page_size) page_shift++;
435 #endif /* !__i386__ */
439 /* Do not use stdio here since it may temporarily change the size
440 * of some segments (ie libc6 adds 0x1000 per open FILE)
442 int fd = open ("/proc/self/maps", O_RDONLY);
445 char buffer[512]; /* line might be rather long in 2.1 */
449 int start, end, offset;
451 BYTE vprot = VPROT_COMMITTED;
454 int count = sizeof(buffer);
455 while (1 == read(fd, ptr, 1) && *ptr != '\n' && --count > 0)
458 if (*ptr != '\n') break;
461 sscanf( buffer, "%x-%x %c%c%c%c %x",
462 &start, &end, &r, &w, &x, &p, &offset );
463 if (r == 'r') vprot |= VPROT_READ;
464 if (w == 'w') vprot |= VPROT_WRITE;
465 if (x == 'x') vprot |= VPROT_EXEC;
466 if (p == 'p') vprot |= VPROT_WRITECOPY;
467 VIRTUAL_CreateView( start, end - start, 0,
468 VFLAG_SYSTEM, vprot, -1 );
478 /***********************************************************************
479 * VIRTUAL_GetPageSize
481 DWORD VIRTUAL_GetPageSize(void)
483 return 1 << page_shift;
487 /***********************************************************************
488 * VIRTUAL_GetGranularity
490 DWORD VIRTUAL_GetGranularity(void)
492 return granularity_mask + 1;
496 /***********************************************************************
497 * VIRTUAL_SetFaultHandler
499 BOOL VIRTUAL_SetFaultHandler( LPCVOID addr, HANDLERPROC proc, LPVOID arg )
503 if (!(view = VIRTUAL_FindView((UINT)addr))) return FALSE;
504 view->handlerProc = proc;
505 view->handlerArg = arg;
509 /***********************************************************************
510 * VIRTUAL_HandleFault
512 BOOL VIRTUAL_HandleFault( LPCVOID addr )
514 FILE_VIEW *view = VIRTUAL_FindView((UINT)addr);
516 if (view && view->handlerProc)
517 return view->handlerProc(view->handlerArg, addr);
522 /***********************************************************************
523 * VirtualAlloc (KERNEL32.548)
524 * Reserves or commits a region of pages in virtual address space
527 * Base address of allocated region of pages
530 LPVOID WINAPI VirtualAlloc(
531 LPVOID addr, /* [in] Address of region to reserve or commit */
532 DWORD size, /* [in] Size of region */
533 DWORD type, /* [in] Type of allocation */
534 DWORD protect /* [in] Type of access protection */
537 UINT base, ptr, view_size;
540 TRACE(virtual, "%08x %08lx %lx %08lx\n",
541 (UINT)addr, size, type, protect );
543 /* Round parameters to a page boundary */
545 if (size > 0x7fc00000) /* 2Gb - 4Mb */
547 SetLastError( ERROR_OUTOFMEMORY );
552 if (type & MEM_RESERVE) /* Round down to 64k boundary */
553 base = ((UINT)addr + granularity_mask) & ~granularity_mask;
555 base = ROUND_ADDR( addr );
556 size = (((UINT)addr + size + page_mask) & ~page_mask) - base;
557 if (base + size < base) /* Disallow wrap-around */
559 SetLastError( ERROR_INVALID_PARAMETER );
566 size = (size + page_mask) & ~page_mask;
569 if (type & MEM_TOP_DOWN) {
570 /* FIXME: MEM_TOP_DOWN allocates the largest possible address.
571 * Is there _ANY_ way to do it with UNIX mmap()?
573 WARN(virtual,"MEM_TOP_DOWN ignored\n");
574 type &= ~MEM_TOP_DOWN;
576 /* Compute the protection flags */
578 if (!(type & (MEM_COMMIT | MEM_RESERVE)) ||
579 (type & ~(MEM_COMMIT | MEM_RESERVE)))
581 SetLastError( ERROR_INVALID_PARAMETER );
584 if (type & MEM_COMMIT)
585 vprot = VIRTUAL_GetProt( protect ) | VPROT_COMMITTED;
588 /* Reserve the memory */
590 if ((type & MEM_RESERVE) || !base)
592 view_size = size + (base ? 0 : granularity_mask + 1);
593 ptr = (UINT)FILE_dommap( -1, (LPVOID)base, 0, view_size, 0, 0,
594 VIRTUAL_GetUnixProt( vprot ), MAP_PRIVATE );
597 SetLastError( ERROR_OUTOFMEMORY );
602 /* Release the extra memory while keeping the range */
603 /* starting on a 64k boundary. */
605 if (ptr & granularity_mask)
607 UINT extra = granularity_mask + 1 - (ptr & granularity_mask);
608 FILE_munmap( (void *)ptr, 0, extra );
612 if (view_size > size)
613 FILE_munmap( (void *)(ptr + size), 0, view_size - size );
615 else if (ptr != base)
617 /* We couldn't get the address we wanted */
618 FILE_munmap( (void *)ptr, 0, view_size );
619 SetLastError( ERROR_INVALID_ADDRESS );
622 if (!(view = VIRTUAL_CreateView( ptr, size, 0, 0, vprot, -1 )))
624 FILE_munmap( (void *)ptr, 0, size );
625 SetLastError( ERROR_OUTOFMEMORY );
628 VIRTUAL_DEBUG_DUMP_VIEW( view );
632 /* Commit the pages */
634 if (!(view = VIRTUAL_FindView( base )) ||
635 (base + size > view->base + view->size))
637 SetLastError( ERROR_INVALID_PARAMETER );
641 if (!VIRTUAL_SetProt( view, base, size, vprot )) return NULL;
646 /***********************************************************************
647 * VirtualFree (KERNEL32.550)
648 * Release or decommits a region of pages in virtual address space.
654 BOOL WINAPI VirtualFree(
655 LPVOID addr, /* [in] Address of region of committed pages */
656 DWORD size, /* [in] Size of region */
657 DWORD type /* [in] Type of operation */
662 TRACE(virtual, "%08x %08lx %lx\n",
663 (UINT)addr, size, type );
665 /* Fix the parameters */
667 size = ROUND_SIZE( addr, size );
668 base = ROUND_ADDR( addr );
670 if (!(view = VIRTUAL_FindView( base )) ||
671 (base + size > view->base + view->size))
673 SetLastError( ERROR_INVALID_PARAMETER );
677 /* Compute the protection flags */
679 if ((type != MEM_DECOMMIT) && (type != MEM_RELEASE))
681 SetLastError( ERROR_INVALID_PARAMETER );
687 if (type == MEM_RELEASE)
689 if (size || (base != view->base))
691 SetLastError( ERROR_INVALID_PARAMETER );
694 VIRTUAL_DeleteView( view );
698 /* Decommit the pages */
699 return VIRTUAL_SetProt( view, base, size, 0 );
703 /***********************************************************************
704 * VirtualLock (KERNEL32.551)
705 * Locks the specified region of virtual address space
708 * Always returns TRUE
714 BOOL WINAPI VirtualLock(
715 LPVOID addr, /* [in] Address of first byte of range to lock */
716 DWORD size /* [in] Number of bytes in range to lock */
722 /***********************************************************************
723 * VirtualUnlock (KERNEL32.556)
724 * Unlocks a range of pages in the virtual address space
727 * Always returns TRUE
733 BOOL WINAPI VirtualUnlock(
734 LPVOID addr, /* [in] Address of first byte of range */
735 DWORD size /* [in] Number of bytes in range */
741 /***********************************************************************
742 * VirtualProtect (KERNEL32.552)
743 * Changes the access protection on a region of committed pages
749 BOOL WINAPI VirtualProtect(
750 LPVOID addr, /* [in] Address of region of committed pages */
751 DWORD size, /* [in] Size of region */
752 DWORD new_prot, /* [in] Desired access protection */
753 LPDWORD old_prot /* [out] Address of variable to get old protection */
759 TRACE(virtual, "%08x %08lx %08lx\n",
760 (UINT)addr, size, new_prot );
762 /* Fix the parameters */
764 size = ROUND_SIZE( addr, size );
765 base = ROUND_ADDR( addr );
767 if (!(view = VIRTUAL_FindView( base )) ||
768 (base + size > view->base + view->size))
770 SetLastError( ERROR_INVALID_PARAMETER );
774 /* Make sure all the pages are committed */
776 p = view->prot + ((base - view->base) >> page_shift);
777 for (i = size >> page_shift; i; i--, p++)
779 if (!(*p & VPROT_COMMITTED))
781 SetLastError( ERROR_INVALID_PARAMETER );
786 VIRTUAL_GetWin32Prot( view->prot[0], old_prot, NULL );
787 vprot = VIRTUAL_GetProt( new_prot ) | VPROT_COMMITTED;
788 return VIRTUAL_SetProt( view, base, size, vprot );
792 /***********************************************************************
793 * VirtualProtectEx (KERNEL32.553)
794 * Changes the access protection on a region of committed pages in the
795 * virtual address space of a specified process
801 BOOL WINAPI VirtualProtectEx(
802 HANDLE handle, /* [in] Handle of process */
803 LPVOID addr, /* [in] Address of region of committed pages */
804 DWORD size, /* [in] Size of region */
805 DWORD new_prot, /* [in] Desired access protection */
806 LPDWORD old_prot /* [out] Address of variable to get old protection */ )
808 if (PROCESS_IsCurrent( handle ))
809 return VirtualProtect( addr, size, new_prot, old_prot );
810 ERR(virtual,"Unsupported on other process\n");
815 /***********************************************************************
816 * VirtualQuery (KERNEL32.554)
817 * Provides info about a range of pages in virtual address space
820 * Number of bytes returned in information buffer
822 DWORD WINAPI VirtualQuery(
823 LPCVOID addr, /* [in] Address of region */
824 LPMEMORY_BASIC_INFORMATION info, /* [out] Address of info buffer */
825 DWORD len /* [in] Size of buffer */
827 FILE_VIEW *view = VIRTUAL_FirstView;
828 UINT base = ROUND_ADDR( addr );
832 /* Find the view containing the address */
838 size = 0xffff0000 - alloc_base;
841 if (view->base > base)
843 size = view->base - alloc_base;
847 if (view->base + view->size > base)
849 alloc_base = view->base;
853 alloc_base = view->base + view->size;
857 /* Fill the info structure */
861 info->State = MEM_FREE;
863 info->AllocationProtect = 0;
868 BYTE vprot = view->prot[(base - alloc_base) >> page_shift];
869 VIRTUAL_GetWin32Prot( vprot, &info->Protect, &info->State );
870 for (size = base - alloc_base; size < view->size; size += page_mask+1)
871 if (view->prot[size >> page_shift] != vprot) break;
872 info->AllocationProtect = view->protect;
873 info->Type = MEM_PRIVATE; /* FIXME */
876 info->BaseAddress = (LPVOID)base;
877 info->AllocationBase = (LPVOID)alloc_base;
878 info->RegionSize = size - (base - alloc_base);
879 return sizeof(*info);
883 /***********************************************************************
884 * VirtualQueryEx (KERNEL32.555)
885 * Provides info about a range of pages in virtual address space of a
889 * Number of bytes returned in information buffer
891 DWORD WINAPI VirtualQueryEx(
892 HANDLE handle, /* [in] Handle of process */
893 LPCVOID addr, /* [in] Address of region */
894 LPMEMORY_BASIC_INFORMATION info, /* [out] Address of info buffer */
895 DWORD len /* [in] Size of buffer */ )
897 if (PROCESS_IsCurrent( handle ))
898 return VirtualQuery( addr, info, len );
899 ERR(virtual,"Unsupported on other process\n");
904 /***********************************************************************
905 * IsBadReadPtr32 (KERNEL32.354)
908 * FALSE: Process has read access to entire block
911 BOOL WINAPI IsBadReadPtr(
912 LPCVOID ptr, /* Address of memory block */
913 UINT size /* Size of block */
915 return !VIRTUAL_CheckFlags( (UINT)ptr, size,
916 VPROT_READ | VPROT_COMMITTED );
920 /***********************************************************************
921 * IsBadWritePtr32 (KERNEL32.357)
924 * FALSE: Process has write access to entire block
927 BOOL WINAPI IsBadWritePtr(
928 LPVOID ptr, /* [in] Address of memory block */
929 UINT size /* [in] Size of block in bytes */
931 return !VIRTUAL_CheckFlags( (UINT)ptr, size,
932 VPROT_WRITE | VPROT_COMMITTED );
936 /***********************************************************************
937 * IsBadHugeReadPtr32 (KERNEL32.352)
939 * FALSE: Process has read access to entire block
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 * IsBadHugeWritePtr32 (KERNEL32.353)
953 * FALSE: Process has write access to entire block
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 * IsBadCodePtr32 (KERNEL32.351)
968 * FALSE: Process has read access to specified memory
971 BOOL WINAPI IsBadCodePtr(
972 FARPROC ptr /* [in] Address of function */
974 return !VIRTUAL_CheckFlags( (UINT)ptr, 1, VPROT_EXEC | VPROT_COMMITTED );
978 /***********************************************************************
979 * IsBadStringPtr32A (KERNEL32.355)
982 * FALSE: Read access to all bytes in string
985 BOOL WINAPI IsBadStringPtrA(
986 LPCSTR str, /* [in] Address of string */
987 UINT max /* [in] Maximum size of string */
992 if (!max) return FALSE;
993 if (!(view = VIRTUAL_FindView( (UINT)str ))) return TRUE;
994 page = ((UINT)str - view->base) >> page_shift;
995 count = page_mask + 1 - ((UINT)str & page_mask);
999 if ((view->prot[page] & (VPROT_READ | VPROT_COMMITTED)) !=
1000 (VPROT_READ | VPROT_COMMITTED))
1002 if (count > max) count = max;
1004 while (count--) if (!*str++) return FALSE;
1005 if (++page >= view->size >> page_shift) return TRUE;
1006 count = page_mask + 1;
1012 /***********************************************************************
1013 * IsBadStringPtr32W (KERNEL32.356)
1014 * See IsBadStringPtr32A
1016 BOOL WINAPI IsBadStringPtrW( LPCWSTR str, UINT max )
1021 if (!max) return FALSE;
1022 if (!(view = VIRTUAL_FindView( (UINT)str ))) return TRUE;
1023 page = ((UINT)str - view->base) >> page_shift;
1024 count = (page_mask + 1 - ((UINT)str & page_mask)) / sizeof(WCHAR);
1028 if ((view->prot[page] & (VPROT_READ | VPROT_COMMITTED)) !=
1029 (VPROT_READ | VPROT_COMMITTED))
1031 if (count > max) count = max;
1033 while (count--) if (!*str++) return FALSE;
1034 if (++page >= view->size >> page_shift) return TRUE;
1035 count = (page_mask + 1) / sizeof(WCHAR);
1041 /***********************************************************************
1042 * CreateFileMapping32A (KERNEL32.46)
1043 * Creates a named or unnamed file-mapping object for the specified file
1047 * 0: Mapping object does not exist
1050 HANDLE WINAPI CreateFileMappingA(
1051 HFILE hFile, /* [in] Handle of file to map */
1052 SECURITY_ATTRIBUTES *sa, /* [in] Optional security attributes*/
1053 DWORD protect, /* [in] Protection for mapping object */
1054 DWORD size_high, /* [in] High-order 32 bits of object size */
1055 DWORD size_low, /* [in] Low-order 32 bits of object size */
1056 LPCSTR name /* [in] Name of file-mapping object */ )
1058 FILE_MAPPING *mapping = NULL;
1059 struct create_mapping_request req;
1060 struct create_mapping_reply reply = { -1 };
1063 BOOL inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
1065 /* First search for an object with the same name */
1067 K32OBJ *obj = K32OBJ_FindName( name );
1071 if (obj->type == K32OBJ_MEM_MAPPED_FILE)
1074 CLIENT_SendRequest( REQ_CREATE_MAPPING, -1, 2,
1076 name, name ? strlen(name) + 1 : 0 );
1077 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
1078 if (reply.handle == -1) return 0;
1079 if (GetLastError() != ERROR_ALREADY_EXISTS)
1080 return 0; /* not supposed to happen */
1081 handle = HANDLE_Alloc( PROCESS_Current(), obj,
1082 FILE_MAP_ALL_ACCESS /*FIXME*/, inherit, reply.handle );
1086 SetLastError( ERROR_DUP_NAME );
1089 K32OBJ_DecCount( obj );
1093 /* Check parameters */
1095 TRACE(virtual,"(%x,%p,%08lx,%08lx%08lx,%s)\n",
1096 hFile, sa, protect, size_high, size_low, debugstr_a(name) );
1098 vprot = VIRTUAL_GetProt( protect );
1099 if (protect & SEC_RESERVE)
1101 if (hFile != INVALID_HANDLE_VALUE)
1103 SetLastError( ERROR_INVALID_PARAMETER );
1107 else vprot |= VPROT_COMMITTED;
1108 if (protect & SEC_NOCACHE) vprot |= VPROT_NOCACHE;
1110 /* Compute the size and extend the file if necessary */
1112 if (hFile != INVALID_HANDLE_VALUE) /* We have a file */
1114 DWORD access = GENERIC_READ;
1116 if (((protect & 0xff) == PAGE_READWRITE) ||
1117 ((protect & 0xff) == PAGE_WRITECOPY) ||
1118 ((protect & 0xff) == PAGE_EXECUTE_READWRITE) ||
1119 ((protect & 0xff) == PAGE_EXECUTE_WRITECOPY))
1120 access |= GENERIC_WRITE;
1122 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hFile,
1123 K32OBJ_FILE, access )) == -1) goto error;
1125 else req.handle = -1;
1127 /* Create the server object */
1129 req.size_high = size_high;
1130 req.size_low = ROUND_SIZE( 0, size_low );
1131 req.protect = vprot;
1132 req.inherit = inherit;
1133 CLIENT_SendRequest( REQ_CREATE_MAPPING, -1, 2,
1135 name, name ? strlen(name) + 1 : 0 );
1136 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ))
1139 /* Allocate the mapping object */
1141 if (!(mapping = HeapAlloc( SystemHeap, 0, sizeof(*mapping) ))) goto error;
1142 mapping->header.type = K32OBJ_MEM_MAPPED_FILE;
1143 mapping->header.refcount = 1;
1145 if (!K32OBJ_AddName( &mapping->header, name )) handle = 0;
1146 else handle = HANDLE_Alloc( PROCESS_Current(), &mapping->header,
1147 FILE_MAP_ALL_ACCESS /*FIXME*/, inherit, reply.handle );
1148 K32OBJ_DecCount( &mapping->header );
1149 SetLastError(0); /* Last error value is relevant. (see the start of fun) */
1153 if (reply.handle != -1) CLIENT_CloseHandle( reply.handle );
1154 if (mapping) HeapFree( SystemHeap, 0, mapping );
1159 /***********************************************************************
1160 * CreateFileMapping32W (KERNEL32.47)
1161 * See CreateFileMapping32A
1163 HANDLE WINAPI CreateFileMappingW( HFILE hFile, LPSECURITY_ATTRIBUTES attr,
1164 DWORD protect, DWORD size_high,
1165 DWORD size_low, LPCWSTR name )
1167 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
1168 HANDLE ret = CreateFileMappingA( hFile, attr, protect,
1169 size_high, size_low, nameA );
1170 HeapFree( GetProcessHeap(), 0, nameA );
1175 /***********************************************************************
1176 * OpenFileMapping32A (KERNEL32.397)
1177 * Opens a named file-mapping object.
1183 HANDLE WINAPI OpenFileMappingA(
1184 DWORD access, /* [in] Access mode */
1185 BOOL inherit, /* [in] Inherit flag */
1186 LPCSTR name ) /* [in] Name of file-mapping object */
1190 struct open_named_obj_request req;
1191 struct open_named_obj_reply reply;
1192 int len = name ? strlen(name) + 1 : 0;
1194 req.type = OPEN_MAPPING;
1195 req.access = access;
1196 req.inherit = inherit;
1197 CLIENT_SendRequest( REQ_OPEN_NAMED_OBJ, -1, 2, &req, sizeof(req), name, len );
1198 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
1200 if (reply.handle != -1)
1203 if ((obj = K32OBJ_FindNameType( name, K32OBJ_MEM_MAPPED_FILE )))
1205 handle = HANDLE_Alloc( PROCESS_Current(), obj, access, inherit, reply.handle );
1206 K32OBJ_DecCount( obj );
1207 if (handle == INVALID_HANDLE_VALUE)
1208 handle = 0; /* must return 0 on failure, not -1 */
1210 else CLIENT_CloseHandle( reply.handle );
1217 /***********************************************************************
1218 * OpenFileMapping32W (KERNEL32.398)
1219 * See OpenFileMapping32A
1221 HANDLE WINAPI OpenFileMappingW( DWORD access, BOOL inherit, LPCWSTR name)
1223 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
1224 HANDLE ret = OpenFileMappingA( access, inherit, nameA );
1225 HeapFree( GetProcessHeap(), 0, nameA );
1230 /***********************************************************************
1231 * MapViewOfFile (KERNEL32.385)
1232 * Maps a view of a file into the address space
1235 * Starting address of mapped view
1238 LPVOID WINAPI MapViewOfFile(
1239 HANDLE mapping, /* [in] File-mapping object to map */
1240 DWORD access, /* [in] Access mode */
1241 DWORD offset_high, /* [in] High-order 32 bits of file offset */
1242 DWORD offset_low, /* [in] Low-order 32 bits of file offset */
1243 DWORD count /* [in] Number of bytes to map */
1245 return MapViewOfFileEx( mapping, access, offset_high,
1246 offset_low, count, NULL );
1250 /***********************************************************************
1251 * MapViewOfFileEx (KERNEL32.386)
1252 * Maps a view of a file into the address space
1255 * Starting address of mapped view
1258 LPVOID WINAPI MapViewOfFileEx(
1259 HANDLE handle, /* [in] File-mapping object to map */
1260 DWORD access, /* [in] Access mode */
1261 DWORD offset_high, /* [in] High-order 32 bits of file offset */
1262 DWORD offset_low, /* [in] Low-order 32 bits of file offset */
1263 DWORD count, /* [in] Number of bytes to map */
1264 LPVOID addr /* [in] Suggested starting address for mapped view */
1267 UINT ptr = (UINT)-1, size = 0;
1268 int flags = MAP_PRIVATE;
1269 int unix_handle = -1;
1270 struct get_mapping_info_request req;
1271 struct get_mapping_info_reply info;
1273 /* Check parameters */
1275 if ((offset_low & granularity_mask) ||
1276 (addr && ((UINT)addr & granularity_mask)))
1278 SetLastError( ERROR_INVALID_PARAMETER );
1282 req.handle = HANDLE_GetServerHandle( PROCESS_Current(), handle,
1283 K32OBJ_MEM_MAPPED_FILE, 0 /* FIXME */ );
1284 CLIENT_SendRequest( REQ_GET_MAPPING_INFO, -1, 1, &req, sizeof(req) );
1285 if (CLIENT_WaitSimpleReply( &info, sizeof(info), &unix_handle ))
1288 if (info.size_high || offset_high)
1289 ERR(virtual, "Offsets larger than 4Gb not supported\n");
1291 if ((offset_low >= info.size_low) ||
1292 (count > info.size_low - offset_low))
1294 SetLastError( ERROR_INVALID_PARAMETER );
1297 if (count) size = ROUND_SIZE( offset_low, count );
1298 else size = info.size_low - offset_low;
1302 case FILE_MAP_ALL_ACCESS:
1303 case FILE_MAP_WRITE:
1304 case FILE_MAP_WRITE | FILE_MAP_READ:
1305 if (!(info.protect & VPROT_WRITE))
1307 SetLastError( ERROR_INVALID_PARAMETER );
1314 case FILE_MAP_COPY | FILE_MAP_READ:
1315 if (info.protect & VPROT_READ) break;
1318 SetLastError( ERROR_INVALID_PARAMETER );
1324 TRACE(virtual, "handle=%x size=%x offset=%lx\n",
1325 handle, size, offset_low );
1327 ptr = (UINT)FILE_dommap( unix_handle,
1328 addr, 0, size, 0, offset_low,
1329 VIRTUAL_GetUnixProt( info.protect ),
1331 if (ptr == (UINT)-1) {
1332 /* KB: Q125713, 25-SEP-1995, "Common File Mapping Problems and
1333 * Platform Differences":
1334 * Windows NT: ERROR_INVALID_PARAMETER
1335 * Windows 95: ERROR_INVALID_ADDRESS.
1336 * FIXME: So should we add a module dependend check here? -MM
1339 SetLastError( ERROR_OUTOFMEMORY );
1341 SetLastError( ERROR_INVALID_PARAMETER );
1345 if (!(view = VIRTUAL_CreateView( ptr, size, offset_low, 0,
1346 info.protect, handle )))
1348 SetLastError( ERROR_OUTOFMEMORY );
1351 if (unix_handle != -1) close( unix_handle );
1355 if (unix_handle != -1) close( unix_handle );
1356 if (ptr != (UINT)-1) FILE_munmap( (void *)ptr, 0, size );
1361 /***********************************************************************
1362 * FlushViewOfFile (KERNEL32.262)
1363 * Writes to the disk a byte range within a mapped view of a file
1369 BOOL WINAPI FlushViewOfFile(
1370 LPCVOID base, /* [in] Start address of byte range to flush */
1371 DWORD cbFlush /* [in] Number of bytes in range */
1374 UINT addr = ROUND_ADDR( base );
1376 TRACE(virtual, "FlushViewOfFile at %p for %ld bytes\n",
1379 if (!(view = VIRTUAL_FindView( addr )))
1381 SetLastError( ERROR_INVALID_PARAMETER );
1384 if (!cbFlush) cbFlush = view->size;
1385 if (!msync( (void *)addr, cbFlush, MS_SYNC )) return TRUE;
1386 SetLastError( ERROR_INVALID_PARAMETER );
1391 /***********************************************************************
1392 * UnmapViewOfFile (KERNEL32.540)
1393 * Unmaps a mapped view of a file.
1396 * Should addr be an LPCVOID?
1402 BOOL WINAPI UnmapViewOfFile(
1403 LPVOID addr /* [in] Address where mapped view begins */
1406 UINT base = ROUND_ADDR( addr );
1407 if (!(view = VIRTUAL_FindView( base )) || (base != view->base))
1409 SetLastError( ERROR_INVALID_PARAMETER );
1412 VIRTUAL_DeleteView( view );
1416 /***********************************************************************
1419 * Helper function to map a file to memory:
1421 * [RETURN] ptr - pointer to mapped file
1423 LPVOID VIRTUAL_MapFileW( LPCWSTR name )
1425 HANDLE hFile, hMapping;
1428 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
1429 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0);
1430 if (hFile != INVALID_HANDLE_VALUE)
1432 hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
1433 CloseHandle( hFile );
1436 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
1437 CloseHandle( hMapping );