2 * Win32 virtual memory functions
4 * Copyright 1997 Alexandre Julliard
14 #include <sys/types.h>
33 struct _FV
*next
; /* Next view */
34 struct _FV
*prev
; /* Prev view */
35 UINT base
; /* Base address */
36 UINT size
; /* Size in bytes */
37 UINT flags
; /* Allocation flags */
38 UINT offset
; /* Offset from start of mapped file */
39 HANDLE mapping
; /* Handle to the file mapping */
40 HANDLERPROC handlerProc
; /* Fault handler */
41 LPVOID handlerArg
; /* Fault handler argument */
42 BYTE protect
; /* Protection for all pages at allocation time */
43 BYTE prot
[1]; /* Protection byte for each page */
47 #define VFLAG_SYSTEM 0x01
49 /* Conversion from VPROT_* to Win32 flags */
50 static const BYTE VIRTUAL_Win32Flags
[16] =
52 PAGE_NOACCESS
, /* 0 */
53 PAGE_READONLY
, /* READ */
54 PAGE_READWRITE
, /* WRITE */
55 PAGE_READWRITE
, /* READ | WRITE */
56 PAGE_EXECUTE
, /* EXEC */
57 PAGE_EXECUTE_READ
, /* READ | EXEC */
58 PAGE_EXECUTE_READWRITE
, /* WRITE | EXEC */
59 PAGE_EXECUTE_READWRITE
, /* READ | WRITE | EXEC */
60 PAGE_WRITECOPY
, /* WRITECOPY */
61 PAGE_WRITECOPY
, /* READ | WRITECOPY */
62 PAGE_WRITECOPY
, /* WRITE | WRITECOPY */
63 PAGE_WRITECOPY
, /* READ | WRITE | WRITECOPY */
64 PAGE_EXECUTE_WRITECOPY
, /* EXEC | WRITECOPY */
65 PAGE_EXECUTE_WRITECOPY
, /* READ | EXEC | WRITECOPY */
66 PAGE_EXECUTE_WRITECOPY
, /* WRITE | EXEC | WRITECOPY */
67 PAGE_EXECUTE_WRITECOPY
/* READ | WRITE | EXEC | WRITECOPY */
71 static FILE_VIEW
*VIRTUAL_FirstView
;
74 /* These are always the same on an i386, and it will be faster this way */
75 # define page_mask 0xfff
76 # define page_shift 12
77 # define granularity_mask 0xffff
79 static UINT page_shift
;
80 static UINT page_mask
;
81 static UINT granularity_mask
; /* Allocation granularity (usually 64k) */
84 #define ROUND_ADDR(addr) \
85 ((UINT)(addr) & ~page_mask)
87 #define ROUND_SIZE(addr,size) \
88 (((UINT)(size) + ((UINT)(addr) & page_mask) + page_mask) & ~page_mask)
90 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
91 if (!TRACE_ON(virtual)); else VIRTUAL_DumpView(view)
93 /***********************************************************************
96 static const char *VIRTUAL_GetProtStr( BYTE prot
)
98 static char buffer
[6];
99 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
100 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : '-';
101 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
102 buffer
[3] = (prot
& VPROT_WRITE
) ?
103 ((prot
& VPROT_WRITECOPY
) ? 'w' : 'W') : '-';
104 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
110 /***********************************************************************
113 static void VIRTUAL_DumpView( FILE_VIEW
*view
)
116 UINT addr
= view
->base
;
117 BYTE prot
= view
->prot
[0];
119 DUMP( "View: %08x - %08x%s",
120 view
->base
, view
->base
+ view
->size
- 1,
121 (view
->flags
& VFLAG_SYSTEM
) ? " (system)" : "" );
123 DUMP( " %d @ %08x\n", view
->mapping
, view
->offset
);
125 DUMP( " (anonymous)\n");
127 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
129 if (view
->prot
[i
] == prot
) continue;
130 DUMP( " %08x - %08x %s\n",
131 addr
, addr
+ (count
<< page_shift
) - 1,
132 VIRTUAL_GetProtStr(prot
) );
133 addr
+= (count
<< page_shift
);
134 prot
= view
->prot
[i
];
138 DUMP( " %08x - %08x %s\n",
139 addr
, addr
+ (count
<< page_shift
) - 1,
140 VIRTUAL_GetProtStr(prot
) );
144 /***********************************************************************
147 void VIRTUAL_Dump(void)
149 FILE_VIEW
*view
= VIRTUAL_FirstView
;
150 DUMP( "\nDump of all virtual memory views:\n\n" );
153 VIRTUAL_DumpView( view
);
159 /***********************************************************************
162 * Find the view containing a given address.
168 static FILE_VIEW
*VIRTUAL_FindView(
169 UINT addr
/* [in] Address */
171 FILE_VIEW
*view
= VIRTUAL_FirstView
;
174 if (view
->base
> addr
) return NULL
;
175 if (view
->base
+ view
->size
> addr
) return view
;
182 /***********************************************************************
185 * Create a new view and add it in the linked list.
187 static FILE_VIEW
*VIRTUAL_CreateView( UINT base
, UINT size
, UINT offset
,
188 UINT flags
, BYTE vprot
,
191 FILE_VIEW
*view
, *prev
;
193 /* Create the view structure */
195 assert( !(base
& page_mask
) );
196 assert( !(size
& page_mask
) );
198 if (!(view
= (FILE_VIEW
*)malloc( sizeof(*view
) + size
- 1 ))) return NULL
;
200 view
->size
= size
<< page_shift
;
202 view
->offset
= offset
;
203 view
->mapping
= mapping
;
204 view
->protect
= vprot
;
205 view
->handlerProc
= NULL
;
206 memset( view
->prot
, vprot
, size
);
208 /* Duplicate the mapping handle */
210 if ((view
->mapping
!= -1) &&
211 !DuplicateHandle( GetCurrentProcess(), view
->mapping
,
212 GetCurrentProcess(), &view
->mapping
,
213 0, FALSE
, DUPLICATE_SAME_ACCESS
))
219 /* Insert it in the linked list */
221 if (!VIRTUAL_FirstView
|| (VIRTUAL_FirstView
->base
> base
))
223 view
->next
= VIRTUAL_FirstView
;
225 if (view
->next
) view
->next
->prev
= view
;
226 VIRTUAL_FirstView
= view
;
230 prev
= VIRTUAL_FirstView
;
231 while (prev
->next
&& (prev
->next
->base
< base
)) prev
= prev
->next
;
232 view
->next
= prev
->next
;
234 if (view
->next
) view
->next
->prev
= view
;
237 VIRTUAL_DEBUG_DUMP_VIEW( view
);
242 /***********************************************************************
249 static void VIRTUAL_DeleteView(
250 FILE_VIEW
*view
/* [in] View */
252 FILE_munmap( (void *)view
->base
, 0, view
->size
);
253 if (view
->next
) view
->next
->prev
= view
->prev
;
254 if (view
->prev
) view
->prev
->next
= view
->next
;
255 else VIRTUAL_FirstView
= view
->next
;
256 if (view
->mapping
) CloseHandle( view
->mapping
);
261 /***********************************************************************
262 * VIRTUAL_GetUnixProt
264 * Convert page protections to protection for mmap/mprotect.
266 static int VIRTUAL_GetUnixProt( BYTE vprot
)
269 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
271 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
272 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
;
273 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
;
279 /***********************************************************************
280 * VIRTUAL_GetWin32Prot
282 * Convert page protections to Win32 flags.
287 static void VIRTUAL_GetWin32Prot(
288 BYTE vprot
, /* [in] Page protection flags */
289 DWORD
*protect
, /* [out] Location to store Win32 protection flags */
290 DWORD
*state
/* [out] Location to store mem state flag */
293 *protect
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
294 /* if (vprot & VPROT_GUARD) *protect |= PAGE_GUARD;*/
295 if (vprot
& VPROT_NOCACHE
) *protect
|= PAGE_NOCACHE
;
297 if (vprot
& VPROT_GUARD
) *protect
= PAGE_NOACCESS
;
300 if (state
) *state
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
304 /***********************************************************************
307 * Build page protections from Win32 flags.
310 * Value of page protection flags
312 static BYTE
VIRTUAL_GetProt(
313 DWORD protect
/* [in] Win32 protection flags */
317 switch(protect
& 0xff)
323 vprot
= VPROT_READ
| VPROT_WRITE
;
326 vprot
= VPROT_READ
| VPROT_WRITE
| VPROT_WRITECOPY
;
331 case PAGE_EXECUTE_READ
:
332 vprot
= VPROT_EXEC
| VPROT_READ
;
334 case PAGE_EXECUTE_READWRITE
:
335 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
| VPROT_WRITECOPY
;
337 case PAGE_EXECUTE_WRITECOPY
:
338 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
345 if (protect
& PAGE_GUARD
) vprot
|= VPROT_GUARD
;
346 if (protect
& PAGE_NOCACHE
) vprot
|= VPROT_NOCACHE
;
351 /***********************************************************************
354 * Change the protection of a range of pages.
360 static BOOL
VIRTUAL_SetProt(
361 FILE_VIEW
*view
, /* [in] Pointer to view */
362 UINT base
, /* [in] Starting address */
363 UINT size
, /* [in] Size in bytes */
364 BYTE vprot
/* [in] Protections to use */
366 TRACE(virtual, "%08x-%08x %s\n",
367 base
, base
+ size
- 1, VIRTUAL_GetProtStr( vprot
) );
369 if (mprotect( (void *)base
, size
, VIRTUAL_GetUnixProt(vprot
) ))
370 return FALSE
; /* FIXME: last error */
372 memset( view
->prot
+ ((base
- view
->base
) >> page_shift
),
373 vprot
, size
>> page_shift
);
374 VIRTUAL_DEBUG_DUMP_VIEW( view
);
379 /***********************************************************************
382 * Check that all pages in a range have the given flags.
388 static BOOL
VIRTUAL_CheckFlags(
389 UINT base
, /* [in] Starting address */
390 UINT size
, /* [in] Size in bytes */
391 BYTE flags
/* [in] Flags to check for */
396 if (!size
) return TRUE
;
397 if (!(view
= VIRTUAL_FindView( base
))) return FALSE
;
398 if (view
->base
+ view
->size
< base
+ size
) return FALSE
;
399 page
= (base
- view
->base
) >> page_shift
;
400 size
= ROUND_SIZE( base
, size
) >> page_shift
;
401 while (size
--) if ((view
->prot
[page
++] & flags
) != flags
) return FALSE
;
406 /***********************************************************************
409 BOOL
VIRTUAL_Init(void)
414 # ifdef HAVE_GETPAGESIZE
415 page_size
= getpagesize();
418 page_size
= sysconf(_SC_PAGESIZE
);
420 # error Cannot get the page size on this platform
423 page_mask
= page_size
- 1;
424 granularity_mask
= 0xffff; /* hard-coded for now */
425 /* Make sure we have a power of 2 */
426 assert( !(page_size
& page_mask
) );
428 while ((1 << page_shift
) != page_size
) page_shift
++;
429 #endif /* !__i386__ */
433 /* Do not use stdio here since it may temporarily change the size
434 * of some segments (ie libc6 adds 0x1000 per open FILE)
436 int fd
= open ("/proc/self/maps", O_RDONLY
);
439 char buffer
[512]; /* line might be rather long in 2.1 */
443 int start
, end
, offset
;
445 BYTE vprot
= VPROT_COMMITTED
;
448 int count
= sizeof(buffer
);
449 while (1 == read(fd
, ptr
, 1) && *ptr
!= '\n' && --count
> 0)
452 if (*ptr
!= '\n') break;
455 sscanf( buffer
, "%x-%x %c%c%c%c %x",
456 &start
, &end
, &r
, &w
, &x
, &p
, &offset
);
457 if (r
== 'r') vprot
|= VPROT_READ
;
458 if (w
== 'w') vprot
|= VPROT_WRITE
;
459 if (x
== 'x') vprot
|= VPROT_EXEC
;
460 if (p
== 'p') vprot
|= VPROT_WRITECOPY
;
461 VIRTUAL_CreateView( start
, end
- start
, 0,
462 VFLAG_SYSTEM
, vprot
, -1 );
472 /***********************************************************************
473 * VIRTUAL_GetPageSize
475 DWORD
VIRTUAL_GetPageSize(void)
477 return 1 << page_shift
;
481 /***********************************************************************
482 * VIRTUAL_GetGranularity
484 DWORD
VIRTUAL_GetGranularity(void)
486 return granularity_mask
+ 1;
490 /***********************************************************************
491 * VIRTUAL_SetFaultHandler
493 BOOL
VIRTUAL_SetFaultHandler( LPCVOID addr
, HANDLERPROC proc
, LPVOID arg
)
497 if (!(view
= VIRTUAL_FindView((UINT
)addr
))) return FALSE
;
498 view
->handlerProc
= proc
;
499 view
->handlerArg
= arg
;
503 /***********************************************************************
504 * VIRTUAL_HandleFault
506 BOOL
VIRTUAL_HandleFault( LPCVOID addr
)
508 FILE_VIEW
*view
= VIRTUAL_FindView((UINT
)addr
);
510 if (view
&& view
->handlerProc
)
511 return view
->handlerProc(view
->handlerArg
, addr
);
516 /***********************************************************************
517 * VirtualAlloc (KERNEL32.548)
518 * Reserves or commits a region of pages in virtual address space
521 * Base address of allocated region of pages
524 LPVOID WINAPI
VirtualAlloc(
525 LPVOID addr
, /* [in] Address of region to reserve or commit */
526 DWORD size
, /* [in] Size of region */
527 DWORD type
, /* [in] Type of allocation */
528 DWORD protect
/* [in] Type of access protection */
531 UINT base
, ptr
, view_size
;
534 TRACE(virtual, "%08x %08lx %lx %08lx\n",
535 (UINT
)addr
, size
, type
, protect
);
537 /* Round parameters to a page boundary */
539 if (size
> 0x7fc00000) /* 2Gb - 4Mb */
541 SetLastError( ERROR_OUTOFMEMORY
);
546 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
547 base
= ((UINT
)addr
+ granularity_mask
) & ~granularity_mask
;
549 base
= ROUND_ADDR( addr
);
550 size
= (((UINT
)addr
+ size
+ page_mask
) & ~page_mask
) - base
;
551 if (base
+ size
< base
) /* Disallow wrap-around */
553 SetLastError( ERROR_INVALID_PARAMETER
);
560 size
= (size
+ page_mask
) & ~page_mask
;
563 if (type
& MEM_TOP_DOWN
) {
564 /* FIXME: MEM_TOP_DOWN allocates the largest possible address.
565 * Is there _ANY_ way to do it with UNIX mmap()?
567 WARN(virtual,"MEM_TOP_DOWN ignored\n");
568 type
&= ~MEM_TOP_DOWN
;
570 /* Compute the protection flags */
572 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
)) ||
573 (type
& ~(MEM_COMMIT
| MEM_RESERVE
)))
575 SetLastError( ERROR_INVALID_PARAMETER
);
578 if (type
& MEM_COMMIT
)
579 vprot
= VIRTUAL_GetProt( protect
) | VPROT_COMMITTED
;
582 /* Reserve the memory */
584 if ((type
& MEM_RESERVE
) || !base
)
586 view_size
= size
+ (base
? 0 : granularity_mask
+ 1);
587 ptr
= (UINT
)FILE_dommap( -1, (LPVOID
)base
, 0, view_size
, 0, 0,
588 VIRTUAL_GetUnixProt( vprot
), MAP_PRIVATE
);
591 SetLastError( ERROR_OUTOFMEMORY
);
596 /* Release the extra memory while keeping the range */
597 /* starting on a 64k boundary. */
599 if (ptr
& granularity_mask
)
601 UINT extra
= granularity_mask
+ 1 - (ptr
& granularity_mask
);
602 FILE_munmap( (void *)ptr
, 0, extra
);
606 if (view_size
> size
)
607 FILE_munmap( (void *)(ptr
+ size
), 0, view_size
- size
);
609 else if (ptr
!= base
)
611 /* We couldn't get the address we wanted */
612 FILE_munmap( (void *)ptr
, 0, view_size
);
613 SetLastError( ERROR_INVALID_ADDRESS
);
616 if (!(view
= VIRTUAL_CreateView( ptr
, size
, 0, 0, vprot
, -1 )))
618 FILE_munmap( (void *)ptr
, 0, size
);
619 SetLastError( ERROR_OUTOFMEMORY
);
622 VIRTUAL_DEBUG_DUMP_VIEW( view
);
626 /* Commit the pages */
628 if (!(view
= VIRTUAL_FindView( base
)) ||
629 (base
+ size
> view
->base
+ view
->size
))
631 SetLastError( ERROR_INVALID_PARAMETER
);
635 if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) return NULL
;
640 /***********************************************************************
641 * VirtualFree (KERNEL32.550)
642 * Release or decommits a region of pages in virtual address space.
648 BOOL WINAPI
VirtualFree(
649 LPVOID addr
, /* [in] Address of region of committed pages */
650 DWORD size
, /* [in] Size of region */
651 DWORD type
/* [in] Type of operation */
656 TRACE(virtual, "%08x %08lx %lx\n",
657 (UINT
)addr
, size
, type
);
659 /* Fix the parameters */
661 size
= ROUND_SIZE( addr
, size
);
662 base
= ROUND_ADDR( addr
);
664 if (!(view
= VIRTUAL_FindView( base
)) ||
665 (base
+ size
> view
->base
+ view
->size
))
667 SetLastError( ERROR_INVALID_PARAMETER
);
671 /* Compute the protection flags */
673 if ((type
!= MEM_DECOMMIT
) && (type
!= MEM_RELEASE
))
675 SetLastError( ERROR_INVALID_PARAMETER
);
681 if (type
== MEM_RELEASE
)
683 if (size
|| (base
!= view
->base
))
685 SetLastError( ERROR_INVALID_PARAMETER
);
688 VIRTUAL_DeleteView( view
);
692 /* Decommit the pages */
693 return VIRTUAL_SetProt( view
, base
, size
, 0 );
697 /***********************************************************************
698 * VirtualLock (KERNEL32.551)
699 * Locks the specified region of virtual address space
702 * Always returns TRUE
708 BOOL WINAPI
VirtualLock(
709 LPVOID addr
, /* [in] Address of first byte of range to lock */
710 DWORD size
/* [in] Number of bytes in range to lock */
716 /***********************************************************************
717 * VirtualUnlock (KERNEL32.556)
718 * Unlocks a range of pages in the virtual address space
721 * Always returns TRUE
727 BOOL WINAPI
VirtualUnlock(
728 LPVOID addr
, /* [in] Address of first byte of range */
729 DWORD size
/* [in] Number of bytes in range */
735 /***********************************************************************
736 * VirtualProtect (KERNEL32.552)
737 * Changes the access protection on a region of committed pages
743 BOOL WINAPI
VirtualProtect(
744 LPVOID addr
, /* [in] Address of region of committed pages */
745 DWORD size
, /* [in] Size of region */
746 DWORD new_prot
, /* [in] Desired access protection */
747 LPDWORD old_prot
/* [out] Address of variable to get old protection */
753 TRACE(virtual, "%08x %08lx %08lx\n",
754 (UINT
)addr
, size
, new_prot
);
756 /* Fix the parameters */
758 size
= ROUND_SIZE( addr
, size
);
759 base
= ROUND_ADDR( addr
);
761 if (!(view
= VIRTUAL_FindView( base
)) ||
762 (base
+ size
> view
->base
+ view
->size
))
764 SetLastError( ERROR_INVALID_PARAMETER
);
768 /* Make sure all the pages are committed */
770 p
= view
->prot
+ ((base
- view
->base
) >> page_shift
);
771 for (i
= size
>> page_shift
; i
; i
--, p
++)
773 if (!(*p
& VPROT_COMMITTED
))
775 SetLastError( ERROR_INVALID_PARAMETER
);
780 VIRTUAL_GetWin32Prot( view
->prot
[0], old_prot
, NULL
);
781 vprot
= VIRTUAL_GetProt( new_prot
) | VPROT_COMMITTED
;
782 return VIRTUAL_SetProt( view
, base
, size
, vprot
);
786 /***********************************************************************
787 * VirtualProtectEx (KERNEL32.553)
788 * Changes the access protection on a region of committed pages in the
789 * virtual address space of a specified process
795 BOOL WINAPI
VirtualProtectEx(
796 HANDLE handle
, /* [in] Handle of process */
797 LPVOID addr
, /* [in] Address of region of committed pages */
798 DWORD size
, /* [in] Size of region */
799 DWORD new_prot
, /* [in] Desired access protection */
800 LPDWORD old_prot
/* [out] Address of variable to get old protection */ )
802 if (PROCESS_IsCurrent( handle
))
803 return VirtualProtect( addr
, size
, new_prot
, old_prot
);
804 ERR(virtual,"Unsupported on other process\n");
809 /***********************************************************************
810 * VirtualQuery (KERNEL32.554)
811 * Provides info about a range of pages in virtual address space
814 * Number of bytes returned in information buffer
816 DWORD WINAPI
VirtualQuery(
817 LPCVOID addr
, /* [in] Address of region */
818 LPMEMORY_BASIC_INFORMATION info
, /* [out] Address of info buffer */
819 DWORD len
/* [in] Size of buffer */
821 FILE_VIEW
*view
= VIRTUAL_FirstView
;
822 UINT base
= ROUND_ADDR( addr
);
826 /* Find the view containing the address */
832 size
= 0xffff0000 - alloc_base
;
835 if (view
->base
> base
)
837 size
= view
->base
- alloc_base
;
841 if (view
->base
+ view
->size
> base
)
843 alloc_base
= view
->base
;
847 alloc_base
= view
->base
+ view
->size
;
851 /* Fill the info structure */
855 info
->State
= MEM_FREE
;
857 info
->AllocationProtect
= 0;
862 BYTE vprot
= view
->prot
[(base
- alloc_base
) >> page_shift
];
863 VIRTUAL_GetWin32Prot( vprot
, &info
->Protect
, &info
->State
);
864 for (size
= base
- alloc_base
; size
< view
->size
; size
+= page_mask
+1)
865 if (view
->prot
[size
>> page_shift
] != vprot
) break;
866 info
->AllocationProtect
= view
->protect
;
867 info
->Type
= MEM_PRIVATE
; /* FIXME */
870 info
->BaseAddress
= (LPVOID
)base
;
871 info
->AllocationBase
= (LPVOID
)alloc_base
;
872 info
->RegionSize
= size
- (base
- alloc_base
);
873 return sizeof(*info
);
877 /***********************************************************************
878 * VirtualQueryEx (KERNEL32.555)
879 * Provides info about a range of pages in virtual address space of a
883 * Number of bytes returned in information buffer
885 DWORD WINAPI
VirtualQueryEx(
886 HANDLE handle
, /* [in] Handle of process */
887 LPCVOID addr
, /* [in] Address of region */
888 LPMEMORY_BASIC_INFORMATION info
, /* [out] Address of info buffer */
889 DWORD len
/* [in] Size of buffer */ )
891 if (PROCESS_IsCurrent( handle
))
892 return VirtualQuery( addr
, info
, len
);
893 ERR(virtual,"Unsupported on other process\n");
898 /***********************************************************************
899 * IsBadReadPtr32 (KERNEL32.354)
902 * FALSE: Process has read access to entire block
905 BOOL WINAPI
IsBadReadPtr(
906 LPCVOID ptr
, /* Address of memory block */
907 UINT size
/* Size of block */
909 return !VIRTUAL_CheckFlags( (UINT
)ptr
, size
,
910 VPROT_READ
| VPROT_COMMITTED
);
914 /***********************************************************************
915 * IsBadWritePtr32 (KERNEL32.357)
918 * FALSE: Process has write access to entire block
921 BOOL WINAPI
IsBadWritePtr(
922 LPVOID ptr
, /* [in] Address of memory block */
923 UINT size
/* [in] Size of block in bytes */
925 return !VIRTUAL_CheckFlags( (UINT
)ptr
, size
,
926 VPROT_WRITE
| VPROT_COMMITTED
);
930 /***********************************************************************
931 * IsBadHugeReadPtr32 (KERNEL32.352)
933 * FALSE: Process has read access to entire block
936 BOOL WINAPI
IsBadHugeReadPtr(
937 LPCVOID ptr
, /* [in] Address of memory block */
938 UINT size
/* [in] Size of block */
940 return IsBadReadPtr( ptr
, size
);
944 /***********************************************************************
945 * IsBadHugeWritePtr32 (KERNEL32.353)
947 * FALSE: Process has write access to entire block
950 BOOL WINAPI
IsBadHugeWritePtr(
951 LPVOID ptr
, /* [in] Address of memory block */
952 UINT size
/* [in] Size of block */
954 return IsBadWritePtr( ptr
, size
);
958 /***********************************************************************
959 * IsBadCodePtr32 (KERNEL32.351)
962 * FALSE: Process has read access to specified memory
965 BOOL WINAPI
IsBadCodePtr(
966 FARPROC ptr
/* [in] Address of function */
968 return !VIRTUAL_CheckFlags( (UINT
)ptr
, 1, VPROT_EXEC
| VPROT_COMMITTED
);
972 /***********************************************************************
973 * IsBadStringPtr32A (KERNEL32.355)
976 * FALSE: Read access to all bytes in string
979 BOOL WINAPI
IsBadStringPtrA(
980 LPCSTR str
, /* [in] Address of string */
981 UINT max
/* [in] Maximum size of string */
986 if (!max
) return FALSE
;
987 if (!(view
= VIRTUAL_FindView( (UINT
)str
))) return TRUE
;
988 page
= ((UINT
)str
- view
->base
) >> page_shift
;
989 count
= page_mask
+ 1 - ((UINT
)str
& page_mask
);
993 if ((view
->prot
[page
] & (VPROT_READ
| VPROT_COMMITTED
)) !=
994 (VPROT_READ
| VPROT_COMMITTED
))
996 if (count
> max
) count
= max
;
998 while (count
--) if (!*str
++) return FALSE
;
999 if (++page
>= view
->size
>> page_shift
) return TRUE
;
1000 count
= page_mask
+ 1;
1006 /***********************************************************************
1007 * IsBadStringPtr32W (KERNEL32.356)
1008 * See IsBadStringPtr32A
1010 BOOL WINAPI
IsBadStringPtrW( LPCWSTR str
, UINT max
)
1015 if (!max
) return FALSE
;
1016 if (!(view
= VIRTUAL_FindView( (UINT
)str
))) return TRUE
;
1017 page
= ((UINT
)str
- view
->base
) >> page_shift
;
1018 count
= (page_mask
+ 1 - ((UINT
)str
& page_mask
)) / sizeof(WCHAR
);
1022 if ((view
->prot
[page
] & (VPROT_READ
| VPROT_COMMITTED
)) !=
1023 (VPROT_READ
| VPROT_COMMITTED
))
1025 if (count
> max
) count
= max
;
1027 while (count
--) if (!*str
++) return FALSE
;
1028 if (++page
>= view
->size
>> page_shift
) return TRUE
;
1029 count
= (page_mask
+ 1) / sizeof(WCHAR
);
1035 /***********************************************************************
1036 * CreateFileMapping32A (KERNEL32.46)
1037 * Creates a named or unnamed file-mapping object for the specified file
1041 * 0: Mapping object does not exist
1044 HANDLE WINAPI
CreateFileMappingA(
1045 HFILE hFile
, /* [in] Handle of file to map */
1046 SECURITY_ATTRIBUTES
*sa
, /* [in] Optional security attributes*/
1047 DWORD protect
, /* [in] Protection for mapping object */
1048 DWORD size_high
, /* [in] High-order 32 bits of object size */
1049 DWORD size_low
, /* [in] Low-order 32 bits of object size */
1050 LPCSTR name
/* [in] Name of file-mapping object */ )
1052 struct create_mapping_request req
;
1053 struct create_mapping_reply reply
;
1056 /* Check parameters */
1058 TRACE(virtual,"(%x,%p,%08lx,%08lx%08lx,%s)\n",
1059 hFile
, sa
, protect
, size_high
, size_low
, debugstr_a(name
) );
1061 vprot
= VIRTUAL_GetProt( protect
);
1062 if (protect
& SEC_RESERVE
)
1064 if (hFile
!= INVALID_HANDLE_VALUE
)
1066 SetLastError( ERROR_INVALID_PARAMETER
);
1070 else vprot
|= VPROT_COMMITTED
;
1071 if (protect
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
1073 /* Create the server object */
1076 req
.size_high
= size_high
;
1077 req
.size_low
= size_low
;
1078 req
.protect
= vprot
;
1079 req
.inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
1080 CLIENT_SendRequest( REQ_CREATE_MAPPING
, -1, 2,
1082 name
, name
? strlen(name
) + 1 : 0 );
1084 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
1085 if (reply
.handle
== -1) return 0;
1086 return reply
.handle
;
1090 /***********************************************************************
1091 * CreateFileMapping32W (KERNEL32.47)
1092 * See CreateFileMapping32A
1094 HANDLE WINAPI
CreateFileMappingW( HFILE hFile
, LPSECURITY_ATTRIBUTES attr
,
1095 DWORD protect
, DWORD size_high
,
1096 DWORD size_low
, LPCWSTR name
)
1098 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
1099 HANDLE ret
= CreateFileMappingA( hFile
, attr
, protect
,
1100 size_high
, size_low
, nameA
);
1101 HeapFree( GetProcessHeap(), 0, nameA
);
1106 /***********************************************************************
1107 * OpenFileMapping32A (KERNEL32.397)
1108 * Opens a named file-mapping object.
1114 HANDLE WINAPI
OpenFileMappingA(
1115 DWORD access
, /* [in] Access mode */
1116 BOOL inherit
, /* [in] Inherit flag */
1117 LPCSTR name
) /* [in] Name of file-mapping object */
1119 struct open_named_obj_request req
;
1120 struct open_named_obj_reply reply
;
1121 int len
= name
? strlen(name
) + 1 : 0;
1123 req
.type
= OPEN_MAPPING
;
1124 req
.access
= access
;
1125 req
.inherit
= inherit
;
1126 CLIENT_SendRequest( REQ_OPEN_NAMED_OBJ
, -1, 2, &req
, sizeof(req
), name
, len
);
1127 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
1128 if (reply
.handle
== -1) return 0; /* must return 0 on failure, not -1 */
1129 return reply
.handle
;
1133 /***********************************************************************
1134 * OpenFileMapping32W (KERNEL32.398)
1135 * See OpenFileMapping32A
1137 HANDLE WINAPI
OpenFileMappingW( DWORD access
, BOOL inherit
, LPCWSTR name
)
1139 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
1140 HANDLE ret
= OpenFileMappingA( access
, inherit
, nameA
);
1141 HeapFree( GetProcessHeap(), 0, nameA
);
1146 /***********************************************************************
1147 * MapViewOfFile (KERNEL32.385)
1148 * Maps a view of a file into the address space
1151 * Starting address of mapped view
1154 LPVOID WINAPI
MapViewOfFile(
1155 HANDLE mapping
, /* [in] File-mapping object to map */
1156 DWORD access
, /* [in] Access mode */
1157 DWORD offset_high
, /* [in] High-order 32 bits of file offset */
1158 DWORD offset_low
, /* [in] Low-order 32 bits of file offset */
1159 DWORD count
/* [in] Number of bytes to map */
1161 return MapViewOfFileEx( mapping
, access
, offset_high
,
1162 offset_low
, count
, NULL
);
1166 /***********************************************************************
1167 * MapViewOfFileEx (KERNEL32.386)
1168 * Maps a view of a file into the address space
1171 * Starting address of mapped view
1174 LPVOID WINAPI
MapViewOfFileEx(
1175 HANDLE handle
, /* [in] File-mapping object to map */
1176 DWORD access
, /* [in] Access mode */
1177 DWORD offset_high
, /* [in] High-order 32 bits of file offset */
1178 DWORD offset_low
, /* [in] Low-order 32 bits of file offset */
1179 DWORD count
, /* [in] Number of bytes to map */
1180 LPVOID addr
/* [in] Suggested starting address for mapped view */
1183 UINT ptr
= (UINT
)-1, size
= 0;
1184 int flags
= MAP_PRIVATE
;
1185 int unix_handle
= -1;
1186 struct get_mapping_info_request req
;
1187 struct get_mapping_info_reply info
;
1189 /* Check parameters */
1191 if ((offset_low
& granularity_mask
) ||
1192 (addr
&& ((UINT
)addr
& granularity_mask
)))
1194 SetLastError( ERROR_INVALID_PARAMETER
);
1198 req
.handle
= handle
;
1199 CLIENT_SendRequest( REQ_GET_MAPPING_INFO
, -1, 1, &req
, sizeof(req
) );
1200 if (CLIENT_WaitSimpleReply( &info
, sizeof(info
), &unix_handle
))
1203 if (info
.size_high
|| offset_high
)
1204 ERR(virtual, "Offsets larger than 4Gb not supported\n");
1206 if ((offset_low
>= info
.size_low
) ||
1207 (count
> info
.size_low
- offset_low
))
1209 SetLastError( ERROR_INVALID_PARAMETER
);
1212 if (count
) size
= ROUND_SIZE( offset_low
, count
);
1213 else size
= info
.size_low
- offset_low
;
1217 case FILE_MAP_ALL_ACCESS
:
1218 case FILE_MAP_WRITE
:
1219 case FILE_MAP_WRITE
| FILE_MAP_READ
:
1220 if (!(info
.protect
& VPROT_WRITE
))
1222 SetLastError( ERROR_INVALID_PARAMETER
);
1229 case FILE_MAP_COPY
| FILE_MAP_READ
:
1230 if (info
.protect
& VPROT_READ
) break;
1233 SetLastError( ERROR_INVALID_PARAMETER
);
1239 TRACE(virtual, "handle=%x size=%x offset=%lx\n",
1240 handle
, size
, offset_low
);
1242 ptr
= (UINT
)FILE_dommap( unix_handle
,
1243 addr
, 0, size
, 0, offset_low
,
1244 VIRTUAL_GetUnixProt( info
.protect
),
1246 if (ptr
== (UINT
)-1) {
1247 /* KB: Q125713, 25-SEP-1995, "Common File Mapping Problems and
1248 * Platform Differences":
1249 * Windows NT: ERROR_INVALID_PARAMETER
1250 * Windows 95: ERROR_INVALID_ADDRESS.
1251 * FIXME: So should we add a module dependend check here? -MM
1254 SetLastError( ERROR_OUTOFMEMORY
);
1256 SetLastError( ERROR_INVALID_PARAMETER
);
1260 if (!(view
= VIRTUAL_CreateView( ptr
, size
, offset_low
, 0,
1261 info
.protect
, handle
)))
1263 SetLastError( ERROR_OUTOFMEMORY
);
1266 if (unix_handle
!= -1) close( unix_handle
);
1270 if (unix_handle
!= -1) close( unix_handle
);
1271 if (ptr
!= (UINT
)-1) FILE_munmap( (void *)ptr
, 0, size
);
1276 /***********************************************************************
1277 * FlushViewOfFile (KERNEL32.262)
1278 * Writes to the disk a byte range within a mapped view of a file
1284 BOOL WINAPI
FlushViewOfFile(
1285 LPCVOID base
, /* [in] Start address of byte range to flush */
1286 DWORD cbFlush
/* [in] Number of bytes in range */
1289 UINT addr
= ROUND_ADDR( base
);
1291 TRACE(virtual, "FlushViewOfFile at %p for %ld bytes\n",
1294 if (!(view
= VIRTUAL_FindView( addr
)))
1296 SetLastError( ERROR_INVALID_PARAMETER
);
1299 if (!cbFlush
) cbFlush
= view
->size
;
1300 if (!msync( (void *)addr
, cbFlush
, MS_SYNC
)) return TRUE
;
1301 SetLastError( ERROR_INVALID_PARAMETER
);
1306 /***********************************************************************
1307 * UnmapViewOfFile (KERNEL32.540)
1308 * Unmaps a mapped view of a file.
1311 * Should addr be an LPCVOID?
1317 BOOL WINAPI
UnmapViewOfFile(
1318 LPVOID addr
/* [in] Address where mapped view begins */
1321 UINT base
= ROUND_ADDR( addr
);
1322 if (!(view
= VIRTUAL_FindView( base
)) || (base
!= view
->base
))
1324 SetLastError( ERROR_INVALID_PARAMETER
);
1327 VIRTUAL_DeleteView( view
);
1331 /***********************************************************************
1334 * Helper function to map a file to memory:
1336 * [RETURN] ptr - pointer to mapped file
1338 LPVOID
VIRTUAL_MapFileW( LPCWSTR name
)
1340 HANDLE hFile
, hMapping
;
1343 hFile
= CreateFileW( name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
1344 OPEN_EXISTING
, FILE_FLAG_RANDOM_ACCESS
, 0);
1345 if (hFile
!= INVALID_HANDLE_VALUE
)
1347 hMapping
= CreateFileMappingA( hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
1348 CloseHandle( hFile
);
1351 ptr
= MapViewOfFile( hMapping
, FILE_MAP_READ
, 0, 0, 0 );
1352 CloseHandle( hMapping
);