2 * Win32 virtual memory functions
4 * Copyright 1997 Alexandre Julliard
11 #ifdef HAVE_SYS_ERRNO_H
12 #include <sys/errno.h>
18 #include <sys/types.h>
19 #ifdef HAVE_SYS_MMAN_H
30 #include "debugtools.h"
32 DEFAULT_DEBUG_CHANNEL(virtual)
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 UINT offset
; /* Offset from start of mapped file */
47 HANDLE mapping
; /* Handle to the file mapping */
48 HANDLERPROC handlerProc
; /* Fault handler */
49 LPVOID handlerArg
; /* Fault handler argument */
50 BYTE protect
; /* Protection for all pages at allocation time */
51 BYTE prot
[1]; /* Protection byte for each page */
55 #define VFLAG_SYSTEM 0x01
57 /* Conversion from VPROT_* to Win32 flags */
58 static const BYTE VIRTUAL_Win32Flags
[16] =
60 PAGE_NOACCESS
, /* 0 */
61 PAGE_READONLY
, /* READ */
62 PAGE_READWRITE
, /* WRITE */
63 PAGE_READWRITE
, /* READ | WRITE */
64 PAGE_EXECUTE
, /* EXEC */
65 PAGE_EXECUTE_READ
, /* READ | EXEC */
66 PAGE_EXECUTE_READWRITE
, /* WRITE | EXEC */
67 PAGE_EXECUTE_READWRITE
, /* READ | WRITE | EXEC */
68 PAGE_WRITECOPY
, /* WRITECOPY */
69 PAGE_WRITECOPY
, /* READ | WRITECOPY */
70 PAGE_WRITECOPY
, /* WRITE | WRITECOPY */
71 PAGE_WRITECOPY
, /* READ | WRITE | WRITECOPY */
72 PAGE_EXECUTE_WRITECOPY
, /* EXEC | WRITECOPY */
73 PAGE_EXECUTE_WRITECOPY
, /* READ | EXEC | WRITECOPY */
74 PAGE_EXECUTE_WRITECOPY
, /* WRITE | EXEC | WRITECOPY */
75 PAGE_EXECUTE_WRITECOPY
/* READ | WRITE | EXEC | WRITECOPY */
79 static FILE_VIEW
*VIRTUAL_FirstView
;
82 /* These are always the same on an i386, and it will be faster this way */
83 # define page_mask 0xfff
84 # define page_shift 12
85 # define granularity_mask 0xffff
87 static UINT page_shift
;
88 static UINT page_mask
;
89 static UINT granularity_mask
; /* Allocation granularity (usually 64k) */
92 #define ROUND_ADDR(addr) \
93 ((UINT)(addr) & ~page_mask)
95 #define ROUND_SIZE(addr,size) \
96 (((UINT)(size) + ((UINT)(addr) & page_mask) + page_mask) & ~page_mask)
98 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
99 if (!TRACE_ON(virtual)); else VIRTUAL_DumpView(view)
101 /***********************************************************************
104 static const char *VIRTUAL_GetProtStr( BYTE prot
)
106 static char buffer
[6];
107 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
108 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : '-';
109 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
110 buffer
[3] = (prot
& VPROT_WRITE
) ?
111 ((prot
& VPROT_WRITECOPY
) ? 'w' : 'W') : '-';
112 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
118 /***********************************************************************
121 static void VIRTUAL_DumpView( FILE_VIEW
*view
)
124 UINT addr
= view
->base
;
125 BYTE prot
= view
->prot
[0];
127 DPRINTF( "View: %08x - %08x%s",
128 view
->base
, view
->base
+ view
->size
- 1,
129 (view
->flags
& VFLAG_SYSTEM
) ? " (system)" : "" );
131 DPRINTF( " %d @ %08x\n", view
->mapping
, view
->offset
);
133 DPRINTF( " (anonymous)\n");
135 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
137 if (view
->prot
[i
] == prot
) continue;
138 DPRINTF( " %08x - %08x %s\n",
139 addr
, addr
+ (count
<< page_shift
) - 1,
140 VIRTUAL_GetProtStr(prot
) );
141 addr
+= (count
<< page_shift
);
142 prot
= view
->prot
[i
];
146 DPRINTF( " %08x - %08x %s\n",
147 addr
, addr
+ (count
<< page_shift
) - 1,
148 VIRTUAL_GetProtStr(prot
) );
152 /***********************************************************************
155 void VIRTUAL_Dump(void)
157 FILE_VIEW
*view
= VIRTUAL_FirstView
;
158 DPRINTF( "\nDump of all virtual memory views:\n\n" );
161 VIRTUAL_DumpView( view
);
167 /***********************************************************************
170 * Find the view containing a given address.
176 static FILE_VIEW
*VIRTUAL_FindView(
177 UINT addr
/* [in] Address */
179 FILE_VIEW
*view
= VIRTUAL_FirstView
;
182 if (view
->base
> addr
) return NULL
;
183 if (view
->base
+ view
->size
> addr
) return view
;
190 /***********************************************************************
193 * Create a new view and add it in the linked list.
195 static FILE_VIEW
*VIRTUAL_CreateView( UINT base
, UINT size
, UINT offset
,
196 UINT flags
, BYTE vprot
,
199 FILE_VIEW
*view
, *prev
;
201 /* Create the view structure */
203 assert( !(base
& page_mask
) );
204 assert( !(size
& page_mask
) );
206 if (!(view
= (FILE_VIEW
*)malloc( sizeof(*view
) + size
- 1 ))) return NULL
;
208 view
->size
= size
<< page_shift
;
210 view
->offset
= offset
;
211 view
->mapping
= mapping
;
212 view
->protect
= vprot
;
213 view
->handlerProc
= NULL
;
214 memset( view
->prot
, vprot
, size
);
216 /* Duplicate the mapping handle */
218 if ((view
->mapping
!= -1) &&
219 !DuplicateHandle( GetCurrentProcess(), view
->mapping
,
220 GetCurrentProcess(), &view
->mapping
,
221 0, FALSE
, DUPLICATE_SAME_ACCESS
))
227 /* Insert it in the linked list */
229 if (!VIRTUAL_FirstView
|| (VIRTUAL_FirstView
->base
> base
))
231 view
->next
= VIRTUAL_FirstView
;
233 if (view
->next
) view
->next
->prev
= view
;
234 VIRTUAL_FirstView
= view
;
238 prev
= VIRTUAL_FirstView
;
239 while (prev
->next
&& (prev
->next
->base
< base
)) prev
= prev
->next
;
240 view
->next
= prev
->next
;
242 if (view
->next
) view
->next
->prev
= view
;
245 VIRTUAL_DEBUG_DUMP_VIEW( view
);
250 /***********************************************************************
257 static void VIRTUAL_DeleteView(
258 FILE_VIEW
*view
/* [in] View */
260 FILE_munmap( (void *)view
->base
, 0, view
->size
);
261 if (view
->next
) view
->next
->prev
= view
->prev
;
262 if (view
->prev
) view
->prev
->next
= view
->next
;
263 else VIRTUAL_FirstView
= view
->next
;
264 if (view
->mapping
) CloseHandle( view
->mapping
);
269 /***********************************************************************
270 * VIRTUAL_GetUnixProt
272 * Convert page protections to protection for mmap/mprotect.
274 static int VIRTUAL_GetUnixProt( BYTE vprot
)
277 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
279 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
280 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
;
281 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
;
287 /***********************************************************************
288 * VIRTUAL_GetWin32Prot
290 * Convert page protections to Win32 flags.
295 static void VIRTUAL_GetWin32Prot(
296 BYTE vprot
, /* [in] Page protection flags */
297 DWORD
*protect
, /* [out] Location to store Win32 protection flags */
298 DWORD
*state
/* [out] Location to store mem state flag */
301 *protect
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
302 /* if (vprot & VPROT_GUARD) *protect |= PAGE_GUARD;*/
303 if (vprot
& VPROT_NOCACHE
) *protect
|= PAGE_NOCACHE
;
305 if (vprot
& VPROT_GUARD
) *protect
= PAGE_NOACCESS
;
308 if (state
) *state
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
312 /***********************************************************************
315 * Build page protections from Win32 flags.
318 * Value of page protection flags
320 static BYTE
VIRTUAL_GetProt(
321 DWORD protect
/* [in] Win32 protection flags */
325 switch(protect
& 0xff)
331 vprot
= VPROT_READ
| VPROT_WRITE
;
334 vprot
= VPROT_READ
| VPROT_WRITE
| VPROT_WRITECOPY
;
339 case PAGE_EXECUTE_READ
:
340 vprot
= VPROT_EXEC
| VPROT_READ
;
342 case PAGE_EXECUTE_READWRITE
:
343 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
| VPROT_WRITECOPY
;
345 case PAGE_EXECUTE_WRITECOPY
:
346 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
353 if (protect
& PAGE_GUARD
) vprot
|= VPROT_GUARD
;
354 if (protect
& PAGE_NOCACHE
) vprot
|= VPROT_NOCACHE
;
359 /***********************************************************************
362 * Change the protection of a range of pages.
368 static BOOL
VIRTUAL_SetProt(
369 FILE_VIEW
*view
, /* [in] Pointer to view */
370 UINT base
, /* [in] Starting address */
371 UINT size
, /* [in] Size in bytes */
372 BYTE vprot
/* [in] Protections to use */
374 TRACE("%08x-%08x %s\n",
375 base
, base
+ size
- 1, VIRTUAL_GetProtStr( vprot
) );
377 if (mprotect( (void *)base
, size
, VIRTUAL_GetUnixProt(vprot
) ))
378 return FALSE
; /* FIXME: last error */
380 memset( view
->prot
+ ((base
- view
->base
) >> page_shift
),
381 vprot
, size
>> page_shift
);
382 VIRTUAL_DEBUG_DUMP_VIEW( view
);
387 /***********************************************************************
390 * Check that all pages in a range have the given flags.
396 static BOOL
VIRTUAL_CheckFlags(
397 UINT base
, /* [in] Starting address */
398 UINT size
, /* [in] Size in bytes */
399 BYTE flags
/* [in] Flags to check for */
404 if (!size
) return TRUE
;
405 if (!(view
= VIRTUAL_FindView( base
))) return FALSE
;
406 if (view
->base
+ view
->size
< base
+ size
) return FALSE
;
407 page
= (base
- view
->base
) >> page_shift
;
408 size
= ROUND_SIZE( base
, size
) >> page_shift
;
409 while (size
--) if ((view
->prot
[page
++] & flags
) != flags
) return FALSE
;
414 /***********************************************************************
417 BOOL
VIRTUAL_Init(void)
422 # ifdef HAVE_GETPAGESIZE
423 page_size
= getpagesize();
426 page_size
= sysconf(_SC_PAGESIZE
);
428 # error Cannot get the page size on this platform
431 page_mask
= page_size
- 1;
432 granularity_mask
= 0xffff; /* hard-coded for now */
433 /* Make sure we have a power of 2 */
434 assert( !(page_size
& page_mask
) );
436 while ((1 << page_shift
) != page_size
) page_shift
++;
437 #endif /* !__i386__ */
441 /* Do not use stdio here since it may temporarily change the size
442 * of some segments (ie libc6 adds 0x1000 per open FILE)
444 int fd
= open ("/proc/self/maps", O_RDONLY
);
447 char buffer
[512]; /* line might be rather long in 2.1 */
451 int start
, end
, offset
;
453 BYTE vprot
= VPROT_COMMITTED
;
456 int count
= sizeof(buffer
);
457 while (1 == read(fd
, ptr
, 1) && *ptr
!= '\n' && --count
> 0)
460 if (*ptr
!= '\n') break;
463 sscanf( buffer
, "%x-%x %c%c%c%c %x",
464 &start
, &end
, &r
, &w
, &x
, &p
, &offset
);
465 if (r
== 'r') vprot
|= VPROT_READ
;
466 if (w
== 'w') vprot
|= VPROT_WRITE
;
467 if (x
== 'x') vprot
|= VPROT_EXEC
;
468 if (p
== 'p') vprot
|= VPROT_WRITECOPY
;
469 VIRTUAL_CreateView( start
, end
- start
, 0,
470 VFLAG_SYSTEM
, vprot
, -1 );
480 /***********************************************************************
481 * VIRTUAL_GetPageSize
483 DWORD
VIRTUAL_GetPageSize(void)
485 return 1 << page_shift
;
489 /***********************************************************************
490 * VIRTUAL_GetGranularity
492 DWORD
VIRTUAL_GetGranularity(void)
494 return granularity_mask
+ 1;
498 /***********************************************************************
499 * VIRTUAL_SetFaultHandler
501 BOOL
VIRTUAL_SetFaultHandler( LPCVOID addr
, HANDLERPROC proc
, LPVOID arg
)
505 if (!(view
= VIRTUAL_FindView((UINT
)addr
))) return FALSE
;
506 view
->handlerProc
= proc
;
507 view
->handlerArg
= arg
;
511 /***********************************************************************
512 * VIRTUAL_HandleFault
514 BOOL
VIRTUAL_HandleFault( LPCVOID addr
)
516 FILE_VIEW
*view
= VIRTUAL_FindView((UINT
)addr
);
518 if (view
&& view
->handlerProc
)
519 return view
->handlerProc(view
->handlerArg
, addr
);
524 /***********************************************************************
525 * VirtualAlloc (KERNEL32.548)
526 * Reserves or commits a region of pages in virtual address space
529 * Base address of allocated region of pages
532 LPVOID WINAPI
VirtualAlloc(
533 LPVOID addr
, /* [in] Address of region to reserve or commit */
534 DWORD size
, /* [in] Size of region */
535 DWORD type
, /* [in] Type of allocation */
536 DWORD protect
/* [in] Type of access protection */
539 UINT base
, ptr
, view_size
;
542 TRACE("%08x %08lx %lx %08lx\n",
543 (UINT
)addr
, size
, type
, protect
);
545 /* Round parameters to a page boundary */
547 if (size
> 0x7fc00000) /* 2Gb - 4Mb */
549 SetLastError( ERROR_OUTOFMEMORY
);
554 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
555 base
= ((UINT
)addr
+ granularity_mask
) & ~granularity_mask
;
557 base
= ROUND_ADDR( addr
);
558 size
= (((UINT
)addr
+ size
+ page_mask
) & ~page_mask
) - base
;
559 if (base
+ size
< base
) /* Disallow wrap-around */
561 SetLastError( ERROR_INVALID_PARAMETER
);
568 size
= (size
+ page_mask
) & ~page_mask
;
571 if (type
& MEM_TOP_DOWN
) {
572 /* FIXME: MEM_TOP_DOWN allocates the largest possible address.
573 * Is there _ANY_ way to do it with UNIX mmap()?
575 WARN("MEM_TOP_DOWN ignored\n");
576 type
&= ~MEM_TOP_DOWN
;
578 /* Compute the protection flags */
580 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
)) ||
581 (type
& ~(MEM_COMMIT
| MEM_RESERVE
)))
583 SetLastError( ERROR_INVALID_PARAMETER
);
586 if (type
& MEM_COMMIT
)
587 vprot
= VIRTUAL_GetProt( protect
) | VPROT_COMMITTED
;
590 /* Reserve the memory */
592 if ((type
& MEM_RESERVE
) || !base
)
594 view_size
= size
+ (base
? 0 : granularity_mask
+ 1);
595 ptr
= (UINT
)FILE_dommap( -1, (LPVOID
)base
, 0, view_size
, 0, 0,
596 VIRTUAL_GetUnixProt( vprot
), MAP_PRIVATE
);
599 SetLastError( ERROR_OUTOFMEMORY
);
604 /* Release the extra memory while keeping the range */
605 /* starting on a 64k boundary. */
607 if (ptr
& granularity_mask
)
609 UINT extra
= granularity_mask
+ 1 - (ptr
& granularity_mask
);
610 FILE_munmap( (void *)ptr
, 0, extra
);
614 if (view_size
> size
)
615 FILE_munmap( (void *)(ptr
+ size
), 0, view_size
- size
);
617 else if (ptr
!= base
)
619 /* We couldn't get the address we wanted */
620 FILE_munmap( (void *)ptr
, 0, view_size
);
621 SetLastError( ERROR_INVALID_ADDRESS
);
624 if (!(view
= VIRTUAL_CreateView( ptr
, size
, 0, 0, vprot
, -1 )))
626 FILE_munmap( (void *)ptr
, 0, size
);
627 SetLastError( ERROR_OUTOFMEMORY
);
630 VIRTUAL_DEBUG_DUMP_VIEW( view
);
634 /* Commit the pages */
636 if (!(view
= VIRTUAL_FindView( base
)) ||
637 (base
+ size
> view
->base
+ view
->size
))
639 SetLastError( ERROR_INVALID_PARAMETER
);
643 if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) return NULL
;
648 /***********************************************************************
649 * VirtualFree (KERNEL32.550)
650 * Release or decommits a region of pages in virtual address space.
656 BOOL WINAPI
VirtualFree(
657 LPVOID addr
, /* [in] Address of region of committed pages */
658 DWORD size
, /* [in] Size of region */
659 DWORD type
/* [in] Type of operation */
664 TRACE("%08x %08lx %lx\n",
665 (UINT
)addr
, size
, type
);
667 /* Fix the parameters */
669 size
= ROUND_SIZE( addr
, size
);
670 base
= ROUND_ADDR( addr
);
672 if (!(view
= VIRTUAL_FindView( base
)) ||
673 (base
+ size
> view
->base
+ view
->size
))
675 SetLastError( ERROR_INVALID_PARAMETER
);
679 /* Compute the protection flags */
681 if ((type
!= MEM_DECOMMIT
) && (type
!= MEM_RELEASE
))
683 SetLastError( ERROR_INVALID_PARAMETER
);
689 if (type
== MEM_RELEASE
)
691 if (size
|| (base
!= view
->base
))
693 SetLastError( ERROR_INVALID_PARAMETER
);
696 VIRTUAL_DeleteView( view
);
700 /* Decommit the pages */
701 return VIRTUAL_SetProt( view
, base
, size
, 0 );
705 /***********************************************************************
706 * VirtualLock (KERNEL32.551)
707 * Locks the specified region of virtual address space
710 * Always returns TRUE
716 BOOL WINAPI
VirtualLock(
717 LPVOID addr
, /* [in] Address of first byte of range to lock */
718 DWORD size
/* [in] Number of bytes in range to lock */
724 /***********************************************************************
725 * VirtualUnlock (KERNEL32.556)
726 * Unlocks a range of pages in the virtual address space
729 * Always returns TRUE
735 BOOL WINAPI
VirtualUnlock(
736 LPVOID addr
, /* [in] Address of first byte of range */
737 DWORD size
/* [in] Number of bytes in range */
743 /***********************************************************************
744 * VirtualProtect (KERNEL32.552)
745 * Changes the access protection on a region of committed pages
751 BOOL WINAPI
VirtualProtect(
752 LPVOID addr
, /* [in] Address of region of committed pages */
753 DWORD size
, /* [in] Size of region */
754 DWORD new_prot
, /* [in] Desired access protection */
755 LPDWORD old_prot
/* [out] Address of variable to get old protection */
761 TRACE("%08x %08lx %08lx\n",
762 (UINT
)addr
, size
, new_prot
);
764 /* Fix the parameters */
766 size
= ROUND_SIZE( addr
, size
);
767 base
= ROUND_ADDR( addr
);
769 if (!(view
= VIRTUAL_FindView( base
)) ||
770 (base
+ size
> view
->base
+ view
->size
))
772 SetLastError( ERROR_INVALID_PARAMETER
);
776 /* Make sure all the pages are committed */
778 p
= view
->prot
+ ((base
- view
->base
) >> page_shift
);
779 for (i
= size
>> page_shift
; i
; i
--, p
++)
781 if (!(*p
& VPROT_COMMITTED
))
783 SetLastError( ERROR_INVALID_PARAMETER
);
788 VIRTUAL_GetWin32Prot( view
->prot
[0], old_prot
, NULL
);
789 vprot
= VIRTUAL_GetProt( new_prot
) | VPROT_COMMITTED
;
790 return VIRTUAL_SetProt( view
, base
, size
, vprot
);
794 /***********************************************************************
795 * VirtualProtectEx (KERNEL32.553)
796 * Changes the access protection on a region of committed pages in the
797 * virtual address space of a specified process
803 BOOL WINAPI
VirtualProtectEx(
804 HANDLE handle
, /* [in] Handle of process */
805 LPVOID addr
, /* [in] Address of region of committed pages */
806 DWORD size
, /* [in] Size of region */
807 DWORD new_prot
, /* [in] Desired access protection */
808 LPDWORD old_prot
/* [out] Address of variable to get old protection */ )
810 if (PROCESS_IsCurrent( handle
))
811 return VirtualProtect( addr
, size
, new_prot
, old_prot
);
812 ERR("Unsupported on other process\n");
817 /***********************************************************************
818 * VirtualQuery (KERNEL32.554)
819 * Provides info about a range of pages in virtual address space
822 * Number of bytes returned in information buffer
824 DWORD WINAPI
VirtualQuery(
825 LPCVOID addr
, /* [in] Address of region */
826 LPMEMORY_BASIC_INFORMATION info
, /* [out] Address of info buffer */
827 DWORD len
/* [in] Size of buffer */
829 FILE_VIEW
*view
= VIRTUAL_FirstView
;
830 UINT base
= ROUND_ADDR( addr
);
834 /* Find the view containing the address */
840 size
= 0xffff0000 - alloc_base
;
843 if (view
->base
> base
)
845 size
= view
->base
- alloc_base
;
849 if (view
->base
+ view
->size
> base
)
851 alloc_base
= view
->base
;
855 alloc_base
= view
->base
+ view
->size
;
859 /* Fill the info structure */
863 info
->State
= MEM_FREE
;
865 info
->AllocationProtect
= 0;
870 BYTE vprot
= view
->prot
[(base
- alloc_base
) >> page_shift
];
871 VIRTUAL_GetWin32Prot( vprot
, &info
->Protect
, &info
->State
);
872 for (size
= base
- alloc_base
; size
< view
->size
; size
+= page_mask
+1)
873 if (view
->prot
[size
>> page_shift
] != vprot
) break;
874 info
->AllocationProtect
= view
->protect
;
875 info
->Type
= MEM_PRIVATE
; /* FIXME */
878 info
->BaseAddress
= (LPVOID
)base
;
879 info
->AllocationBase
= (LPVOID
)alloc_base
;
880 info
->RegionSize
= size
- (base
- alloc_base
);
881 return sizeof(*info
);
885 /***********************************************************************
886 * VirtualQueryEx (KERNEL32.555)
887 * Provides info about a range of pages in virtual address space of a
891 * Number of bytes returned in information buffer
893 DWORD WINAPI
VirtualQueryEx(
894 HANDLE handle
, /* [in] Handle of process */
895 LPCVOID addr
, /* [in] Address of region */
896 LPMEMORY_BASIC_INFORMATION info
, /* [out] Address of info buffer */
897 DWORD len
/* [in] Size of buffer */ )
899 if (PROCESS_IsCurrent( handle
))
900 return VirtualQuery( addr
, info
, len
);
901 ERR("Unsupported on other process\n");
906 /***********************************************************************
907 * IsBadReadPtr32 (KERNEL32.354)
910 * FALSE: Process has read access to entire block
913 BOOL WINAPI
IsBadReadPtr(
914 LPCVOID ptr
, /* Address of memory block */
915 UINT size
/* Size of block */
917 return !VIRTUAL_CheckFlags( (UINT
)ptr
, size
,
918 VPROT_READ
| VPROT_COMMITTED
);
922 /***********************************************************************
923 * IsBadWritePtr32 (KERNEL32.357)
926 * FALSE: Process has write access to entire block
929 BOOL WINAPI
IsBadWritePtr(
930 LPVOID ptr
, /* [in] Address of memory block */
931 UINT size
/* [in] Size of block in bytes */
933 return !VIRTUAL_CheckFlags( (UINT
)ptr
, size
,
934 VPROT_WRITE
| VPROT_COMMITTED
);
938 /***********************************************************************
939 * IsBadHugeReadPtr32 (KERNEL32.352)
941 * FALSE: Process has read access to entire block
944 BOOL WINAPI
IsBadHugeReadPtr(
945 LPCVOID ptr
, /* [in] Address of memory block */
946 UINT size
/* [in] Size of block */
948 return IsBadReadPtr( ptr
, size
);
952 /***********************************************************************
953 * IsBadHugeWritePtr32 (KERNEL32.353)
955 * FALSE: Process has write access to entire block
958 BOOL WINAPI
IsBadHugeWritePtr(
959 LPVOID ptr
, /* [in] Address of memory block */
960 UINT size
/* [in] Size of block */
962 return IsBadWritePtr( ptr
, size
);
966 /***********************************************************************
967 * IsBadCodePtr32 (KERNEL32.351)
970 * FALSE: Process has read access to specified memory
973 BOOL WINAPI
IsBadCodePtr(
974 FARPROC ptr
/* [in] Address of function */
976 return !VIRTUAL_CheckFlags( (UINT
)ptr
, 1, VPROT_EXEC
| VPROT_COMMITTED
);
980 /***********************************************************************
981 * IsBadStringPtr32A (KERNEL32.355)
984 * FALSE: Read access to all bytes in string
987 BOOL WINAPI
IsBadStringPtrA(
988 LPCSTR str
, /* [in] Address of string */
989 UINT max
/* [in] Maximum size of string */
994 if (!max
) return FALSE
;
995 if (!(view
= VIRTUAL_FindView( (UINT
)str
))) return TRUE
;
996 page
= ((UINT
)str
- view
->base
) >> page_shift
;
997 count
= page_mask
+ 1 - ((UINT
)str
& page_mask
);
1001 if ((view
->prot
[page
] & (VPROT_READ
| VPROT_COMMITTED
)) !=
1002 (VPROT_READ
| VPROT_COMMITTED
))
1004 if (count
> max
) count
= max
;
1006 while (count
--) if (!*str
++) return FALSE
;
1007 if (++page
>= view
->size
>> page_shift
) return TRUE
;
1008 count
= page_mask
+ 1;
1014 /***********************************************************************
1015 * IsBadStringPtr32W (KERNEL32.356)
1016 * See IsBadStringPtr32A
1018 BOOL WINAPI
IsBadStringPtrW( LPCWSTR str
, UINT max
)
1023 if (!max
) return FALSE
;
1024 if (!(view
= VIRTUAL_FindView( (UINT
)str
))) return TRUE
;
1025 page
= ((UINT
)str
- view
->base
) >> page_shift
;
1026 count
= (page_mask
+ 1 - ((UINT
)str
& page_mask
)) / sizeof(WCHAR
);
1030 if ((view
->prot
[page
] & (VPROT_READ
| VPROT_COMMITTED
)) !=
1031 (VPROT_READ
| VPROT_COMMITTED
))
1033 if (count
> max
) count
= max
;
1035 while (count
--) if (!*str
++) return FALSE
;
1036 if (++page
>= view
->size
>> page_shift
) return TRUE
;
1037 count
= (page_mask
+ 1) / sizeof(WCHAR
);
1043 /***********************************************************************
1044 * CreateFileMapping32A (KERNEL32.46)
1045 * Creates a named or unnamed file-mapping object for the specified file
1049 * 0: Mapping object does not exist
1052 HANDLE WINAPI
CreateFileMappingA(
1053 HFILE hFile
, /* [in] Handle of file to map */
1054 SECURITY_ATTRIBUTES
*sa
, /* [in] Optional security attributes*/
1055 DWORD protect
, /* [in] Protection for mapping object */
1056 DWORD size_high
, /* [in] High-order 32 bits of object size */
1057 DWORD size_low
, /* [in] Low-order 32 bits of object size */
1058 LPCSTR name
/* [in] Name of file-mapping object */ )
1060 struct create_mapping_request
*req
= get_req_buffer();
1063 /* Check parameters */
1065 TRACE("(%x,%p,%08lx,%08lx%08lx,%s)\n",
1066 hFile
, sa
, protect
, size_high
, size_low
, debugstr_a(name
) );
1068 vprot
= VIRTUAL_GetProt( protect
);
1069 if (protect
& SEC_RESERVE
)
1071 if (hFile
!= INVALID_HANDLE_VALUE
)
1073 SetLastError( ERROR_INVALID_PARAMETER
);
1077 else vprot
|= VPROT_COMMITTED
;
1078 if (protect
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
1080 /* Create the server object */
1082 req
->file_handle
= hFile
;
1083 req
->size_high
= size_high
;
1084 req
->size_low
= size_low
;
1085 req
->protect
= vprot
;
1086 req
->inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
1087 lstrcpynA( req
->name
, name
? name
: "", server_remaining(req
->name
) );
1089 server_call( REQ_CREATE_MAPPING
);
1090 if (req
->handle
== -1) return 0;
1095 /***********************************************************************
1096 * CreateFileMapping32W (KERNEL32.47)
1097 * See CreateFileMapping32A
1099 HANDLE WINAPI
CreateFileMappingW( HFILE hFile
, LPSECURITY_ATTRIBUTES attr
,
1100 DWORD protect
, DWORD size_high
,
1101 DWORD size_low
, LPCWSTR name
)
1103 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
1104 HANDLE ret
= CreateFileMappingA( hFile
, attr
, protect
,
1105 size_high
, size_low
, nameA
);
1106 HeapFree( GetProcessHeap(), 0, nameA
);
1111 /***********************************************************************
1112 * OpenFileMapping32A (KERNEL32.397)
1113 * Opens a named file-mapping object.
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 lstrcpynA( req
->name
, name
? name
: "", server_remaining(req
->name
) );
1129 server_call( REQ_OPEN_MAPPING
);
1130 if (req
->handle
== -1) return 0; /* must return 0 on failure, not -1 */
1135 /***********************************************************************
1136 * OpenFileMapping32W (KERNEL32.398)
1137 * See OpenFileMapping32A
1139 HANDLE WINAPI
OpenFileMappingW( DWORD access
, BOOL inherit
, LPCWSTR name
)
1141 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
1142 HANDLE ret
= OpenFileMappingA( access
, inherit
, nameA
);
1143 HeapFree( GetProcessHeap(), 0, nameA
);
1148 /***********************************************************************
1149 * MapViewOfFile (KERNEL32.385)
1150 * Maps a view of a file into the address space
1153 * Starting address of mapped view
1156 LPVOID WINAPI
MapViewOfFile(
1157 HANDLE mapping
, /* [in] File-mapping object to map */
1158 DWORD access
, /* [in] Access mode */
1159 DWORD offset_high
, /* [in] High-order 32 bits of file offset */
1160 DWORD offset_low
, /* [in] Low-order 32 bits of file offset */
1161 DWORD count
/* [in] Number of bytes to map */
1163 return MapViewOfFileEx( mapping
, access
, offset_high
,
1164 offset_low
, count
, NULL
);
1168 /***********************************************************************
1169 * MapViewOfFileEx (KERNEL32.386)
1170 * Maps a view of a file into the address space
1173 * Starting address of mapped view
1176 LPVOID WINAPI
MapViewOfFileEx(
1177 HANDLE handle
, /* [in] File-mapping object to map */
1178 DWORD access
, /* [in] Access mode */
1179 DWORD offset_high
, /* [in] High-order 32 bits of file offset */
1180 DWORD offset_low
, /* [in] Low-order 32 bits of file offset */
1181 DWORD count
, /* [in] Number of bytes to map */
1182 LPVOID addr
/* [in] Suggested starting address for mapped view */
1185 UINT ptr
= (UINT
)-1, size
= 0;
1186 int flags
= MAP_PRIVATE
;
1187 int unix_handle
= -1;
1189 struct get_mapping_info_request
*req
= get_req_buffer();
1191 /* Check parameters */
1193 if ((offset_low
& granularity_mask
) ||
1194 (addr
&& ((UINT
)addr
& granularity_mask
)))
1196 SetLastError( ERROR_INVALID_PARAMETER
);
1200 req
->handle
= handle
;
1201 if (server_call_fd( REQ_GET_MAPPING_INFO
, -1, &unix_handle
)) goto error
;
1203 if (req
->size_high
|| offset_high
)
1204 ERR("Offsets larger than 4Gb not supported\n");
1206 if ((offset_low
>= req
->size_low
) ||
1207 (count
> req
->size_low
- offset_low
))
1209 SetLastError( ERROR_INVALID_PARAMETER
);
1212 if (count
) size
= ROUND_SIZE( offset_low
, count
);
1213 else size
= req
->size_low
- offset_low
;
1214 prot
= req
->protect
;
1218 case FILE_MAP_ALL_ACCESS
:
1219 case FILE_MAP_WRITE
:
1220 case FILE_MAP_WRITE
| FILE_MAP_READ
:
1221 if (!(prot
& VPROT_WRITE
))
1223 SetLastError( ERROR_INVALID_PARAMETER
);
1230 case FILE_MAP_COPY
| FILE_MAP_READ
:
1231 if (prot
& VPROT_READ
) break;
1234 SetLastError( ERROR_INVALID_PARAMETER
);
1240 TRACE("handle=%x size=%x offset=%lx\n", handle
, size
, offset_low
);
1242 ptr
= (UINT
)FILE_dommap( unix_handle
, addr
, 0, size
, 0, offset_low
,
1243 VIRTUAL_GetUnixProt( prot
), flags
);
1244 if (ptr
== (UINT
)-1) {
1245 /* KB: Q125713, 25-SEP-1995, "Common File Mapping Problems and
1246 * Platform Differences":
1247 * Windows NT: ERROR_INVALID_PARAMETER
1248 * Windows 95: ERROR_INVALID_ADDRESS.
1249 * FIXME: So should we add a module dependend check here? -MM
1252 SetLastError( ERROR_OUTOFMEMORY
);
1254 SetLastError( ERROR_INVALID_PARAMETER
);
1258 if (!(view
= VIRTUAL_CreateView( ptr
, size
, offset_low
, 0, prot
, handle
)))
1260 SetLastError( ERROR_OUTOFMEMORY
);
1263 if (unix_handle
!= -1) close( unix_handle
);
1267 if (unix_handle
!= -1) close( unix_handle
);
1268 if (ptr
!= (UINT
)-1) FILE_munmap( (void *)ptr
, 0, size
);
1273 /***********************************************************************
1274 * FlushViewOfFile (KERNEL32.262)
1275 * Writes to the disk a byte range within a mapped view of a file
1281 BOOL WINAPI
FlushViewOfFile(
1282 LPCVOID base
, /* [in] Start address of byte range to flush */
1283 DWORD cbFlush
/* [in] Number of bytes in range */
1286 UINT addr
= ROUND_ADDR( base
);
1288 TRACE("FlushViewOfFile at %p for %ld bytes\n",
1291 if (!(view
= VIRTUAL_FindView( addr
)))
1293 SetLastError( ERROR_INVALID_PARAMETER
);
1296 if (!cbFlush
) cbFlush
= view
->size
;
1297 if (!msync( (void *)addr
, cbFlush
, MS_SYNC
)) return TRUE
;
1298 SetLastError( ERROR_INVALID_PARAMETER
);
1303 /***********************************************************************
1304 * UnmapViewOfFile (KERNEL32.540)
1305 * Unmaps a mapped view of a file.
1308 * Should addr be an LPCVOID?
1314 BOOL WINAPI
UnmapViewOfFile(
1315 LPVOID addr
/* [in] Address where mapped view begins */
1318 UINT base
= ROUND_ADDR( addr
);
1319 if (!(view
= VIRTUAL_FindView( base
)) || (base
!= view
->base
))
1321 SetLastError( ERROR_INVALID_PARAMETER
);
1324 VIRTUAL_DeleteView( view
);
1328 /***********************************************************************
1331 * Helper function to map a file to memory:
1333 * [RETURN] ptr - pointer to mapped file
1335 LPVOID
VIRTUAL_MapFileW( LPCWSTR name
)
1337 HANDLE hFile
, hMapping
;
1340 hFile
= CreateFileW( name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
1341 OPEN_EXISTING
, FILE_FLAG_RANDOM_ACCESS
, 0);
1342 if (hFile
!= INVALID_HANDLE_VALUE
)
1344 hMapping
= CreateFileMappingA( hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
1345 CloseHandle( hFile
);
1348 ptr
= MapViewOfFile( hMapping
, FILE_MAP_READ
, 0, 0, 0 );
1349 CloseHandle( hMapping
);