2 * Win32 virtual memory functions
4 * Copyright 1997 Alexandre Julliard
12 #include <sys/types.h>
40 struct _FV
*next
; /* Next view */
41 struct _FV
*prev
; /* Prev view */
42 UINT32 base
; /* Base address */
43 UINT32 size
; /* Size in bytes */
44 UINT32 flags
; /* Allocation flags */
45 UINT32 offset
; /* Offset from start of mapped file */
46 FILE_MAPPING
*mapping
; /* File mapping */
47 HANDLERPROC handlerProc
; /* Fault handler */
48 LPVOID handlerArg
; /* Fault handler argument */
49 BYTE protect
; /* Protection for all pages at allocation time */
50 BYTE prot
[1]; /* Protection byte for each page */
53 /* Per-page protection byte values */
54 #define VPROT_READ 0x01
55 #define VPROT_WRITE 0x02
56 #define VPROT_EXEC 0x04
57 #define VPROT_WRITECOPY 0x08
58 #define VPROT_GUARD 0x10
59 #define VPROT_NOCACHE 0x20
60 #define VPROT_COMMITTED 0x40
63 #define VFLAG_SYSTEM 0x01
65 /* Conversion from VPROT_* to Win32 flags */
66 static const BYTE VIRTUAL_Win32Flags
[16] =
68 PAGE_NOACCESS
, /* 0 */
69 PAGE_READONLY
, /* READ */
70 PAGE_READWRITE
, /* WRITE */
71 PAGE_READWRITE
, /* READ | WRITE */
72 PAGE_EXECUTE
, /* EXEC */
73 PAGE_EXECUTE_READ
, /* READ | EXEC */
74 PAGE_EXECUTE_READWRITE
, /* WRITE | EXEC */
75 PAGE_EXECUTE_READWRITE
, /* READ | WRITE | EXEC */
76 PAGE_WRITECOPY
, /* WRITECOPY */
77 PAGE_WRITECOPY
, /* READ | WRITECOPY */
78 PAGE_WRITECOPY
, /* WRITE | WRITECOPY */
79 PAGE_WRITECOPY
, /* READ | WRITE | WRITECOPY */
80 PAGE_EXECUTE_WRITECOPY
, /* EXEC | WRITECOPY */
81 PAGE_EXECUTE_WRITECOPY
, /* READ | EXEC | WRITECOPY */
82 PAGE_EXECUTE_WRITECOPY
, /* WRITE | EXEC | WRITECOPY */
83 PAGE_EXECUTE_WRITECOPY
/* READ | WRITE | EXEC | WRITECOPY */
87 static FILE_VIEW
*VIRTUAL_FirstView
;
90 /* These are always the same on an i386, and it will be faster this way */
91 # define page_mask 0xfff
92 # define page_shift 12
93 # define granularity_mask 0xffff
95 static UINT32 page_shift
;
96 static UINT32 page_mask
;
97 static UINT32 granularity_mask
; /* Allocation granularity (usually 64k) */
100 #define ROUND_ADDR(addr) \
101 ((UINT32)(addr) & ~page_mask)
103 #define ROUND_SIZE(addr,size) \
104 (((UINT32)(size) + ((UINT32)(addr) & page_mask) + page_mask) & ~page_mask)
106 static void VIRTUAL_DestroyMapping( K32OBJ
*obj
);
108 const K32OBJ_OPS MEM_MAPPED_FILE_Ops
=
110 /* Object cannot be waited upon, so we don't need these (except destroy) */
112 NULL
, /* satisfied */
114 NULL
, /* remove_wait */
117 VIRTUAL_DestroyMapping
/* destroy */
120 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
121 if (!TRACE_ON(virtual)); else VIRTUAL_DumpView(view)
123 /***********************************************************************
126 static const char *VIRTUAL_GetProtStr( BYTE prot
)
128 static char buffer
[6];
129 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
130 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : '-';
131 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
132 buffer
[3] = (prot
& VPROT_WRITE
) ?
133 ((prot
& VPROT_WRITECOPY
) ? 'w' : 'W') : '-';
134 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
140 /***********************************************************************
143 static void VIRTUAL_DumpView( FILE_VIEW
*view
)
146 UINT32 addr
= view
->base
;
147 BYTE prot
= view
->prot
[0];
149 DUMP( "View: %08x - %08x%s",
150 view
->base
, view
->base
+ view
->size
- 1,
151 (view
->flags
& VFLAG_SYSTEM
) ? " (system)" : "" );
152 if (view
->mapping
&& view
->mapping
->file
)
153 DUMP( " %s @ %08x\n", view
->mapping
->file
->unix_name
, view
->offset
);
155 DUMP( " (anonymous)\n");
157 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
159 if (view
->prot
[i
] == prot
) continue;
160 DUMP( " %08x - %08x %s\n",
161 addr
, addr
+ (count
<< page_shift
) - 1,
162 VIRTUAL_GetProtStr(prot
) );
163 addr
+= (count
<< page_shift
);
164 prot
= view
->prot
[i
];
168 DUMP( " %08x - %08x %s\n",
169 addr
, addr
+ (count
<< page_shift
) - 1,
170 VIRTUAL_GetProtStr(prot
) );
174 /***********************************************************************
177 void VIRTUAL_Dump(void)
179 FILE_VIEW
*view
= VIRTUAL_FirstView
;
180 DUMP( "\nDump of all virtual memory views:\n\n" );
183 VIRTUAL_DumpView( view
);
189 /***********************************************************************
192 * Find the view containing a given address.
198 static FILE_VIEW
*VIRTUAL_FindView(
199 UINT32 addr
/* [in] Address */
201 FILE_VIEW
*view
= VIRTUAL_FirstView
;
204 if (view
->base
> addr
) return NULL
;
205 if (view
->base
+ view
->size
> addr
) return view
;
212 /***********************************************************************
215 * Create a new view and add it in the linked list.
217 static FILE_VIEW
*VIRTUAL_CreateView( UINT32 base
, UINT32 size
, UINT32 offset
,
218 UINT32 flags
, BYTE vprot
,
219 FILE_MAPPING
*mapping
)
221 FILE_VIEW
*view
, *prev
;
223 /* Create the view structure */
225 assert( !(base
& page_mask
) );
226 assert( !(size
& page_mask
) );
228 if (!(view
= (FILE_VIEW
*)malloc( sizeof(*view
) + size
- 1 ))) return NULL
;
230 view
->size
= size
<< page_shift
;
232 view
->offset
= offset
;
233 view
->mapping
= mapping
;
234 view
->protect
= vprot
;
235 memset( view
->prot
, vprot
, size
);
237 /* Insert it in the linked list */
239 if (!VIRTUAL_FirstView
|| (VIRTUAL_FirstView
->base
> base
))
241 view
->next
= VIRTUAL_FirstView
;
243 if (view
->next
) view
->next
->prev
= view
;
244 VIRTUAL_FirstView
= view
;
248 prev
= VIRTUAL_FirstView
;
249 while (prev
->next
&& (prev
->next
->base
< base
)) prev
= prev
->next
;
250 view
->next
= prev
->next
;
252 if (view
->next
) view
->next
->prev
= view
;
255 VIRTUAL_DEBUG_DUMP_VIEW( view
);
260 /***********************************************************************
267 static void VIRTUAL_DeleteView(
268 FILE_VIEW
*view
/* [in] View */
270 FILE_munmap( (void *)view
->base
, 0, view
->size
);
271 if (view
->next
) view
->next
->prev
= view
->prev
;
272 if (view
->prev
) view
->prev
->next
= view
->next
;
273 else VIRTUAL_FirstView
= view
->next
;
274 if (view
->mapping
) K32OBJ_DecCount( &view
->mapping
->header
);
279 /***********************************************************************
280 * VIRTUAL_GetUnixProt
282 * Convert page protections to protection for mmap/mprotect.
284 static int VIRTUAL_GetUnixProt( BYTE vprot
)
287 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
289 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
290 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
;
291 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
;
297 /***********************************************************************
298 * VIRTUAL_GetWin32Prot
300 * Convert page protections to Win32 flags.
305 static void VIRTUAL_GetWin32Prot(
306 BYTE vprot
, /* [in] Page protection flags */
307 DWORD
*protect
, /* [out] Location to store Win32 protection flags */
308 DWORD
*state
/* [out] Location to store mem state flag */
311 *protect
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
312 if (vprot
& VPROT_GUARD
) *protect
|= PAGE_GUARD
;
313 if (vprot
& VPROT_NOCACHE
) *protect
|= PAGE_NOCACHE
;
316 if (state
) *state
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
320 /***********************************************************************
323 * Build page protections from Win32 flags.
326 * Value of page protection flags
328 static BYTE
VIRTUAL_GetProt(
329 DWORD protect
/* [in] Win32 protection flags */
333 switch(protect
& 0xff)
339 vprot
= VPROT_READ
| VPROT_WRITE
;
342 vprot
= VPROT_READ
| VPROT_WRITE
| VPROT_WRITECOPY
;
347 case PAGE_EXECUTE_READ
:
348 vprot
= VPROT_EXEC
| VPROT_READ
;
350 case PAGE_EXECUTE_READWRITE
:
351 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
| VPROT_WRITECOPY
;
353 case PAGE_EXECUTE_WRITECOPY
:
354 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
361 if (protect
& PAGE_GUARD
) vprot
|= VPROT_GUARD
;
362 if (protect
& PAGE_NOCACHE
) vprot
|= VPROT_NOCACHE
;
367 /***********************************************************************
370 * Change the protection of a range of pages.
376 static BOOL32
VIRTUAL_SetProt(
377 FILE_VIEW
*view
, /* [in] Pointer to view */
378 UINT32 base
, /* [in] Starting address */
379 UINT32 size
, /* [in] Size in bytes */
380 BYTE vprot
/* [in] Protections to use */
382 TRACE(virtual, "%08x-%08x %s\n",
383 base
, base
+ size
- 1, VIRTUAL_GetProtStr( vprot
) );
385 if (mprotect( (void *)base
, size
, VIRTUAL_GetUnixProt(vprot
) ))
386 return FALSE
; /* FIXME: last error */
388 memset( view
->prot
+ ((base
- view
->base
) >> page_shift
),
389 vprot
, size
>> page_shift
);
390 VIRTUAL_DEBUG_DUMP_VIEW( view
);
395 /***********************************************************************
398 * Check that all pages in a range have the given flags.
404 static BOOL32
VIRTUAL_CheckFlags(
405 UINT32 base
, /* [in] Starting address */
406 UINT32 size
, /* [in] Size in bytes */
407 BYTE flags
/* [in] Flags to check for */
412 if (!size
) return TRUE
;
413 if (!(view
= VIRTUAL_FindView( base
))) return FALSE
;
414 if (view
->base
+ view
->size
< base
+ size
) return FALSE
;
415 page
= (base
- view
->base
) >> page_shift
;
416 size
= ROUND_SIZE( base
, size
) >> page_shift
;
417 while (size
--) if ((view
->prot
[page
++] & flags
) != flags
) return FALSE
;
422 /***********************************************************************
425 BOOL32
VIRTUAL_Init(void)
430 # ifdef HAVE_GETPAGESIZE
431 page_size
= getpagesize();
434 page_size
= sysconf(_SC_PAGESIZE
);
436 # error Cannot get the page size on this platform
439 page_mask
= page_size
- 1;
440 granularity_mask
= 0xffff; /* hard-coded for now */
441 /* Make sure we have a power of 2 */
442 assert( !(page_size
& page_mask
) );
444 while ((1 << page_shift
) != page_size
) page_shift
++;
445 #endif /* !__i386__ */
449 /* Do not use stdio here since it may temporarily change the size
450 * of some segments (ie libc6 adds 0x1000 per open FILE)
452 int fd
= open ("/proc/self/maps", O_RDONLY
);
455 char buffer
[512]; /* line might be rather long in 2.1 */
459 int start
, end
, offset
;
461 BYTE vprot
= VPROT_COMMITTED
;
464 int count
= sizeof(buffer
);
465 while (1 == read(fd
, ptr
, 1) && *ptr
!= '\n' && --count
> 0)
468 if (*ptr
!= '\n') break;
471 sscanf( buffer
, "%x-%x %c%c%c%c %x",
472 &start
, &end
, &r
, &w
, &x
, &p
, &offset
);
473 if (r
== 'r') vprot
|= VPROT_READ
;
474 if (w
== 'w') vprot
|= VPROT_WRITE
;
475 if (x
== 'x') vprot
|= VPROT_EXEC
;
476 if (p
== 'p') vprot
|= VPROT_WRITECOPY
;
477 VIRTUAL_CreateView( start
, end
- start
, 0,
478 VFLAG_SYSTEM
, vprot
, NULL
);
488 /***********************************************************************
489 * VIRTUAL_GetPageSize
491 DWORD
VIRTUAL_GetPageSize(void)
493 return 1 << page_shift
;
497 /***********************************************************************
498 * VIRTUAL_GetGranularity
500 DWORD
VIRTUAL_GetGranularity(void)
502 return granularity_mask
+ 1;
506 /***********************************************************************
507 * VIRTUAL_SetFaultHandler
509 BOOL32
VIRTUAL_SetFaultHandler( LPVOID addr
, HANDLERPROC proc
, LPVOID arg
)
513 if (!(view
= VIRTUAL_FindView((UINT32
)addr
))) return FALSE
;
514 view
->handlerProc
= proc
;
515 view
->handlerArg
= arg
;
519 /***********************************************************************
520 * VIRTUAL_HandleFault
522 BOOL32
VIRTUAL_HandleFault(LPVOID addr
)
524 FILE_VIEW
*view
= VIRTUAL_FindView((UINT32
)addr
);
526 if (view
&& view
->handlerProc
)
527 return view
->handlerProc(view
->handlerArg
, addr
);
532 /***********************************************************************
533 * VirtualAlloc (KERNEL32.548)
534 * Reserves or commits a region of pages in virtual address space
537 * Base address of allocated region of pages
540 LPVOID WINAPI
VirtualAlloc(
541 LPVOID addr
, /* [in] Address of region to reserve or commit */
542 DWORD size
, /* [in] Size of region */
543 DWORD type
, /* [in] Type of allocation */
544 DWORD protect
/* [in] Type of access protection */
547 UINT32 base
, ptr
, view_size
;
550 TRACE(virtual, "%08x %08lx %lx %08lx\n",
551 (UINT32
)addr
, size
, type
, protect
);
553 /* Round parameters to a page boundary */
555 if (size
> 0x7fc00000) /* 2Gb - 4Mb */
557 SetLastError( ERROR_OUTOFMEMORY
);
562 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
563 base
= ((UINT32
)addr
+ granularity_mask
) & ~granularity_mask
;
565 base
= ROUND_ADDR( addr
);
566 size
= (((UINT32
)addr
+ size
+ page_mask
) & ~page_mask
) - base
;
567 if (base
+ size
< base
) /* Disallow wrap-around */
569 SetLastError( ERROR_INVALID_PARAMETER
);
576 size
= (size
+ page_mask
) & ~page_mask
;
579 if (type
& MEM_TOP_DOWN
) {
580 /* FIXME: MEM_TOP_DOWN allocates the largest possible address.
581 * Is there _ANY_ way to do it with UNIX mmap()?
583 WARN(virtual,"MEM_TOP_DOWN ignored\n");
584 type
&= ~MEM_TOP_DOWN
;
586 /* Compute the protection flags */
588 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
)) ||
589 (type
& ~(MEM_COMMIT
| MEM_RESERVE
)))
591 SetLastError( ERROR_INVALID_PARAMETER
);
594 if (type
& MEM_COMMIT
)
595 vprot
= VIRTUAL_GetProt( protect
) | VPROT_COMMITTED
;
598 /* Reserve the memory */
600 if ((type
& MEM_RESERVE
) || !base
)
602 view_size
= size
+ (base
? 0 : granularity_mask
+ 1);
603 ptr
= (UINT32
)FILE_dommap( NULL
, (LPVOID
)base
, 0, view_size
, 0, 0,
604 VIRTUAL_GetUnixProt( vprot
), MAP_PRIVATE
);
605 if (ptr
== (UINT32
)-1)
607 SetLastError( ERROR_OUTOFMEMORY
);
612 /* Release the extra memory while keeping the range */
613 /* starting on a 64k boundary. */
615 if (ptr
& granularity_mask
)
617 UINT32 extra
= granularity_mask
+ 1 - (ptr
& granularity_mask
);
618 FILE_munmap( (void *)ptr
, 0, extra
);
622 if (view_size
> size
)
623 FILE_munmap( (void *)(ptr
+ size
), 0, view_size
- size
);
625 else if (ptr
!= base
)
627 /* We couldn't get the address we wanted */
628 FILE_munmap( (void *)ptr
, 0, view_size
);
629 SetLastError( ERROR_INVALID_ADDRESS
);
632 if (!(view
= VIRTUAL_CreateView( ptr
, size
, 0, 0, vprot
, NULL
)))
634 FILE_munmap( (void *)ptr
, 0, size
);
635 SetLastError( ERROR_OUTOFMEMORY
);
638 VIRTUAL_DEBUG_DUMP_VIEW( view
);
642 /* Commit the pages */
644 if (!(view
= VIRTUAL_FindView( base
)) ||
645 (base
+ size
> view
->base
+ view
->size
))
647 SetLastError( ERROR_INVALID_PARAMETER
);
651 if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) return NULL
;
656 /***********************************************************************
657 * VirtualFree (KERNEL32.550)
658 * Release or decommits a region of pages in virtual address space.
664 BOOL32 WINAPI
VirtualFree(
665 LPVOID addr
, /* [in] Address of region of committed pages */
666 DWORD size
, /* [in] Size of region */
667 DWORD type
/* [in] Type of operation */
672 TRACE(virtual, "%08x %08lx %lx\n",
673 (UINT32
)addr
, size
, type
);
675 /* Fix the parameters */
677 size
= ROUND_SIZE( addr
, size
);
678 base
= ROUND_ADDR( addr
);
680 if (!(view
= VIRTUAL_FindView( base
)) ||
681 (base
+ size
> view
->base
+ view
->size
))
683 SetLastError( ERROR_INVALID_PARAMETER
);
687 /* Compute the protection flags */
689 if ((type
!= MEM_DECOMMIT
) && (type
!= MEM_RELEASE
))
691 SetLastError( ERROR_INVALID_PARAMETER
);
697 if (type
== MEM_RELEASE
)
699 if (size
|| (base
!= view
->base
))
701 SetLastError( ERROR_INVALID_PARAMETER
);
704 VIRTUAL_DeleteView( view
);
708 /* Decommit the pages */
709 return VIRTUAL_SetProt( view
, base
, size
, 0 );
713 /***********************************************************************
714 * VirtualLock (KERNEL32.551)
715 * Locks the specified region of virtual address space
718 * Always returns TRUE
724 BOOL32 WINAPI
VirtualLock(
725 LPVOID addr
, /* [in] Address of first byte of range to lock */
726 DWORD size
/* [in] Number of bytes in range to lock */
732 /***********************************************************************
733 * VirtualUnlock (KERNEL32.556)
734 * Unlocks a range of pages in the virtual address space
737 * Always returns TRUE
743 BOOL32 WINAPI
VirtualUnlock(
744 LPVOID addr
, /* [in] Address of first byte of range */
745 DWORD size
/* [in] Number of bytes in range */
751 /***********************************************************************
752 * VirtualProtect (KERNEL32.552)
753 * Changes the access protection on a region of committed pages
759 BOOL32 WINAPI
VirtualProtect(
760 LPVOID addr
, /* [in] Address of region of committed pages */
761 DWORD size
, /* [in] Size of region */
762 DWORD new_prot
, /* [in] Desired access protection */
763 LPDWORD old_prot
/* [out] Address of variable to get old protection */
769 TRACE(virtual, "%08x %08lx %08lx\n",
770 (UINT32
)addr
, size
, new_prot
);
772 /* Fix the parameters */
774 size
= ROUND_SIZE( addr
, size
);
775 base
= ROUND_ADDR( addr
);
777 if (!(view
= VIRTUAL_FindView( base
)) ||
778 (base
+ size
> view
->base
+ view
->size
))
780 SetLastError( ERROR_INVALID_PARAMETER
);
784 /* Make sure all the pages are committed */
786 p
= view
->prot
+ ((base
- view
->base
) >> page_shift
);
787 for (i
= size
>> page_shift
; i
; i
--, p
++)
789 if (!(*p
& VPROT_COMMITTED
))
791 SetLastError( ERROR_INVALID_PARAMETER
);
796 VIRTUAL_GetWin32Prot( view
->prot
[0], old_prot
, NULL
);
797 vprot
= VIRTUAL_GetProt( new_prot
) | VPROT_COMMITTED
;
798 return VIRTUAL_SetProt( view
, base
, size
, vprot
);
802 /***********************************************************************
803 * VirtualProtectEx (KERNEL32.553)
804 * Changes the access protection on a region of committed pages in the
805 * virtual address space of a specified process
811 BOOL32 WINAPI
VirtualProtectEx(
812 HANDLE32 handle
, /* [in] Handle of process */
813 LPVOID addr
, /* [in] Address of region of committed pages */
814 DWORD size
, /* [in] Size of region */
815 DWORD new_prot
, /* [in] Desired access protection */
816 LPDWORD old_prot
/* [out] Address of variable to get old protection */
820 PDB32
*pdb
= PROCESS_GetPtr( handle
, PROCESS_VM_OPERATION
);
823 if (pdb
== PROCESS_Current())
824 ret
= VirtualProtect( addr
, size
, new_prot
, old_prot
);
826 ERR(virtual,"Unsupported on other process\n");
827 K32OBJ_DecCount( &pdb
->header
);
833 /***********************************************************************
834 * VirtualQuery (KERNEL32.554)
835 * Provides info about a range of pages in virtual address space
838 * Number of bytes returned in information buffer
840 DWORD WINAPI
VirtualQuery(
841 LPCVOID addr
, /* [in] Address of region */
842 LPMEMORY_BASIC_INFORMATION info
, /* [out] Address of info buffer */
843 DWORD len
/* [in] Size of buffer */
845 FILE_VIEW
*view
= VIRTUAL_FirstView
;
846 UINT32 base
= ROUND_ADDR( addr
);
847 UINT32 alloc_base
= 0;
850 /* Find the view containing the address */
856 size
= 0xffff0000 - alloc_base
;
859 if (view
->base
> base
)
861 size
= view
->base
- alloc_base
;
865 if (view
->base
+ view
->size
> base
)
867 alloc_base
= view
->base
;
871 alloc_base
= view
->base
+ view
->size
;
875 /* Fill the info structure */
879 info
->State
= MEM_FREE
;
881 info
->AllocationProtect
= 0;
886 BYTE vprot
= view
->prot
[(base
- alloc_base
) >> page_shift
];
887 VIRTUAL_GetWin32Prot( vprot
, &info
->Protect
, &info
->State
);
888 for (size
= base
- alloc_base
; size
< view
->size
; size
+= page_mask
+1)
889 if (view
->prot
[size
>> page_shift
] != vprot
) break;
890 info
->AllocationProtect
= view
->protect
;
891 info
->Type
= MEM_PRIVATE
; /* FIXME */
894 info
->BaseAddress
= (LPVOID
)base
;
895 info
->AllocationBase
= (LPVOID
)alloc_base
;
896 info
->RegionSize
= size
- (base
- alloc_base
);
897 return sizeof(*info
);
901 /***********************************************************************
902 * VirtualQueryEx (KERNEL32.555)
903 * Provides info about a range of pages in virtual address space of a
907 * Number of bytes returned in information buffer
909 DWORD WINAPI
VirtualQueryEx(
910 HANDLE32 handle
, /* [in] Handle of process */
911 LPCVOID addr
, /* [in] Address of region */
912 LPMEMORY_BASIC_INFORMATION info
, /* [out] Address of info buffer */
913 DWORD len
/* [in] Size of buffer */
917 PDB32
*pdb
= PROCESS_GetPtr( handle
, PROCESS_QUERY_INFORMATION
);
920 if (pdb
== PROCESS_Current())
921 ret
= VirtualQuery( addr
, info
, len
);
923 ERR(virtual,"Unsupported on other process\n");
924 K32OBJ_DecCount( &pdb
->header
);
930 /***********************************************************************
931 * IsBadReadPtr32 (KERNEL32.354)
934 * FALSE: Process has read access to entire block
937 BOOL32 WINAPI
IsBadReadPtr32(
938 LPCVOID ptr
, /* Address of memory block */
939 UINT32 size
/* Size of block */
941 return !VIRTUAL_CheckFlags( (UINT32
)ptr
, size
,
942 VPROT_READ
| VPROT_COMMITTED
);
946 /***********************************************************************
947 * IsBadWritePtr32 (KERNEL32.357)
950 * FALSE: Process has write access to entire block
953 BOOL32 WINAPI
IsBadWritePtr32(
954 LPVOID ptr
, /* [in] Address of memory block */
955 UINT32 size
/* [in] Size of block in bytes */
957 return !VIRTUAL_CheckFlags( (UINT32
)ptr
, size
,
958 VPROT_WRITE
| VPROT_COMMITTED
);
962 /***********************************************************************
963 * IsBadHugeReadPtr32 (KERNEL32.352)
965 * FALSE: Process has read access to entire block
968 BOOL32 WINAPI
IsBadHugeReadPtr32(
969 LPCVOID ptr
, /* [in] Address of memory block */
970 UINT32 size
/* [in] Size of block */
972 return IsBadReadPtr32( ptr
, size
);
976 /***********************************************************************
977 * IsBadHugeWritePtr32 (KERNEL32.353)
979 * FALSE: Process has write access to entire block
982 BOOL32 WINAPI
IsBadHugeWritePtr32(
983 LPVOID ptr
, /* [in] Address of memory block */
984 UINT32 size
/* [in] Size of block */
986 return IsBadWritePtr32( ptr
, size
);
990 /***********************************************************************
991 * IsBadCodePtr32 (KERNEL32.351)
994 * FALSE: Process has read access to specified memory
997 BOOL32 WINAPI
IsBadCodePtr32(
998 FARPROC32 ptr
/* [in] Address of function */
1000 return !VIRTUAL_CheckFlags( (UINT32
)ptr
, 1, VPROT_EXEC
| VPROT_COMMITTED
);
1004 /***********************************************************************
1005 * IsBadStringPtr32A (KERNEL32.355)
1008 * FALSE: Read access to all bytes in string
1011 BOOL32 WINAPI
IsBadStringPtr32A(
1012 LPCSTR str
, /* [in] Address of string */
1013 UINT32 max
/* [in] Maximum size of string */
1018 if (!max
) return FALSE
;
1019 if (!(view
= VIRTUAL_FindView( (UINT32
)str
))) return TRUE
;
1020 page
= ((UINT32
)str
- view
->base
) >> page_shift
;
1021 count
= page_mask
+ 1 - ((UINT32
)str
& page_mask
);
1025 if ((view
->prot
[page
] & (VPROT_READ
| VPROT_COMMITTED
)) !=
1026 (VPROT_READ
| VPROT_COMMITTED
))
1028 if (count
> max
) count
= max
;
1030 while (count
--) if (!*str
++) return FALSE
;
1031 if (++page
>= view
->size
>> page_shift
) return TRUE
;
1032 count
= page_mask
+ 1;
1038 /***********************************************************************
1039 * IsBadStringPtr32W (KERNEL32.356)
1040 * See IsBadStringPtr32A
1042 BOOL32 WINAPI
IsBadStringPtr32W( LPCWSTR str
, UINT32 max
)
1047 if (!max
) return FALSE
;
1048 if (!(view
= VIRTUAL_FindView( (UINT32
)str
))) return TRUE
;
1049 page
= ((UINT32
)str
- view
->base
) >> page_shift
;
1050 count
= (page_mask
+ 1 - ((UINT32
)str
& page_mask
)) / sizeof(WCHAR
);
1054 if ((view
->prot
[page
] & (VPROT_READ
| VPROT_COMMITTED
)) !=
1055 (VPROT_READ
| VPROT_COMMITTED
))
1057 if (count
> max
) count
= max
;
1059 while (count
--) if (!*str
++) return FALSE
;
1060 if (++page
>= view
->size
>> page_shift
) return TRUE
;
1061 count
= (page_mask
+ 1) / sizeof(WCHAR
);
1067 /***********************************************************************
1068 * CreateFileMapping32A (KERNEL32.46)
1069 * Creates a named or unnamed file-mapping object for the specified file
1073 * 0: Mapping object does not exist
1076 HANDLE32 WINAPI
CreateFileMapping32A(
1077 HFILE32 hFile
, /* [in] Handle of file to map */
1078 SECURITY_ATTRIBUTES
*sa
, /* [in] Optional security attributes*/
1079 DWORD protect
, /* [in] Protection for mapping object */
1080 DWORD size_high
, /* [in] High-order 32 bits of object size */
1081 DWORD size_low
, /* [in] Low-order 32 bits of object size */
1082 LPCSTR name
/* [in] Name of file-mapping object */ )
1084 FILE_MAPPING
*mapping
= NULL
;
1087 BOOL32 inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
1089 /* First search for an object with the same name */
1091 K32OBJ
*obj
= K32OBJ_FindName( name
);
1094 if (obj
->type
== K32OBJ_MEM_MAPPED_FILE
)
1096 SetLastError( ERROR_ALREADY_EXISTS
);
1097 handle
= HANDLE_Alloc( PROCESS_Current(), obj
,
1098 FILE_MAP_ALL_ACCESS
/*FIXME*/, inherit
);
1102 SetLastError( ERROR_DUP_NAME
);
1105 K32OBJ_DecCount( obj
);
1109 /* Check parameters */
1111 TRACE(virtual,"(%x,%p,%08lx,%08lx%08lx,%s)\n",
1112 hFile
, sa
, protect
, size_high
, size_low
, name
);
1114 vprot
= VIRTUAL_GetProt( protect
);
1115 if (protect
& SEC_RESERVE
)
1117 if (hFile
!= INVALID_HANDLE_VALUE32
)
1119 SetLastError( ERROR_INVALID_PARAMETER
);
1123 else vprot
|= VPROT_COMMITTED
;
1124 if (protect
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
1126 /* Compute the size and extend the file if necessary */
1128 if (hFile
== INVALID_HANDLE_VALUE32
)
1130 if (!size_high
&& !size_low
)
1132 SetLastError( ERROR_INVALID_PARAMETER
);
1137 else /* We have a file */
1139 BY_HANDLE_FILE_INFORMATION info
;
1140 DWORD access
= GENERIC_READ
;
1142 if (((protect
& 0xff) == PAGE_READWRITE
) ||
1143 ((protect
& 0xff) == PAGE_WRITECOPY
) ||
1144 ((protect
& 0xff) == PAGE_EXECUTE_READWRITE
) ||
1145 ((protect
& 0xff) == PAGE_EXECUTE_WRITECOPY
))
1146 access
|= GENERIC_WRITE
;
1147 if (!(obj
= HANDLE_GetObjPtr( PROCESS_Current(), hFile
,
1148 K32OBJ_FILE
, access
)))
1151 if (!GetFileInformationByHandle( hFile
, &info
)) goto error
;
1152 if (!size_high
&& !size_low
)
1154 size_high
= info
.nFileSizeHigh
;
1155 size_low
= info
.nFileSizeLow
;
1157 else if ((size_high
> info
.nFileSizeHigh
) ||
1158 ((size_high
== info
.nFileSizeHigh
) &&
1159 (size_low
> info
.nFileSizeLow
)))
1161 /* We have to grow the file */
1162 if (SetFilePointer( hFile
, size_low
, &size_high
,
1163 FILE_BEGIN
) == 0xffffffff) goto error
;
1164 if (!SetEndOfFile( hFile
)) goto error
;
1168 /* Allocate the mapping object */
1170 if (!(mapping
= HeapAlloc( SystemHeap
, 0, sizeof(*mapping
) ))) goto error
;
1171 mapping
->header
.type
= K32OBJ_MEM_MAPPED_FILE
;
1172 mapping
->header
.refcount
= 1;
1173 mapping
->protect
= vprot
;
1174 mapping
->size_high
= size_high
;
1175 mapping
->size_low
= ROUND_SIZE( 0, size_low
);
1176 mapping
->file
= (FILE_OBJECT
*)obj
;
1178 if (!K32OBJ_AddName( &mapping
->header
, name
)) handle
= 0;
1179 else handle
= HANDLE_Alloc( PROCESS_Current(), &mapping
->header
,
1180 FILE_MAP_ALL_ACCESS
/*FIXME*/, inherit
);
1181 K32OBJ_DecCount( &mapping
->header
);
1185 if (obj
) K32OBJ_DecCount( obj
);
1186 if (mapping
) HeapFree( SystemHeap
, 0, mapping
);
1191 /***********************************************************************
1192 * CreateFileMapping32W (KERNEL32.47)
1193 * See CreateFileMapping32A
1195 HANDLE32 WINAPI
CreateFileMapping32W( HFILE32 hFile
, LPSECURITY_ATTRIBUTES attr
,
1196 DWORD protect
, DWORD size_high
,
1197 DWORD size_low
, LPCWSTR name
)
1199 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
1200 HANDLE32 ret
= CreateFileMapping32A( hFile
, attr
, protect
,
1201 size_high
, size_low
, nameA
);
1202 HeapFree( GetProcessHeap(), 0, nameA
);
1207 /***********************************************************************
1208 * OpenFileMapping32A (KERNEL32.397)
1209 * Opens a named file-mapping object.
1215 HANDLE32 WINAPI
OpenFileMapping32A(
1216 DWORD access
, /* [in] Access mode */
1217 BOOL32 inherit
, /* [in] Inherit flag */
1218 LPCSTR name
/* [in] Name of file-mapping object */
1220 HANDLE32 handle
= 0;
1223 if ((obj
= K32OBJ_FindNameType( name
, K32OBJ_MEM_MAPPED_FILE
)))
1225 handle
= HANDLE_Alloc( PROCESS_Current(), obj
, access
, inherit
);
1226 K32OBJ_DecCount( obj
);
1233 /***********************************************************************
1234 * OpenFileMapping32W (KERNEL32.398)
1235 * See OpenFileMapping32A
1237 HANDLE32 WINAPI
OpenFileMapping32W( DWORD access
, BOOL32 inherit
, LPCWSTR name
)
1239 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
1240 HANDLE32 ret
= OpenFileMapping32A( access
, inherit
, nameA
);
1241 HeapFree( GetProcessHeap(), 0, nameA
);
1246 /***********************************************************************
1247 * VIRTUAL_DestroyMapping
1249 * Destroy a FILE_MAPPING object.
1251 static void VIRTUAL_DestroyMapping( K32OBJ
*ptr
)
1253 FILE_MAPPING
*mapping
= (FILE_MAPPING
*)ptr
;
1254 assert( ptr
->type
== K32OBJ_MEM_MAPPED_FILE
);
1256 if (mapping
->file
) K32OBJ_DecCount( &mapping
->file
->header
);
1257 ptr
->type
= K32OBJ_UNKNOWN
;
1258 HeapFree( SystemHeap
, 0, mapping
);
1262 /***********************************************************************
1263 * MapViewOfFile (KERNEL32.385)
1264 * Maps a view of a file into the address space
1267 * Starting address of mapped view
1270 LPVOID WINAPI
MapViewOfFile(
1271 HANDLE32 mapping
, /* [in] File-mapping object to map */
1272 DWORD access
, /* [in] Access mode */
1273 DWORD offset_high
, /* [in] High-order 32 bits of file offset */
1274 DWORD offset_low
, /* [in] Low-order 32 bits of file offset */
1275 DWORD count
/* [in] Number of bytes to map */
1277 return MapViewOfFileEx( mapping
, access
, offset_high
,
1278 offset_low
, count
, NULL
);
1282 /***********************************************************************
1283 * MapViewOfFileEx (KERNEL32.386)
1284 * Maps a view of a file into the address space
1287 * Starting address of mapped view
1290 LPVOID WINAPI
MapViewOfFileEx(
1291 HANDLE32 handle
, /* [in] File-mapping object to map */
1292 DWORD access
, /* [in] Access mode */
1293 DWORD offset_high
, /* [in] High-order 32 bits of file offset */
1294 DWORD offset_low
, /* [in] Low-order 32 bits of file offset */
1295 DWORD count
, /* [in] Number of bytes to map */
1296 LPVOID addr
/* [in] Suggested starting address for mapped view */
1298 FILE_MAPPING
*mapping
;
1300 UINT32 ptr
= (UINT32
)-1, size
= 0;
1301 int flags
= MAP_PRIVATE
;
1303 /* Check parameters */
1305 if ((offset_low
& granularity_mask
) ||
1306 (addr
&& ((UINT32
)addr
& granularity_mask
)))
1308 SetLastError( ERROR_INVALID_PARAMETER
);
1312 if (!(mapping
= (FILE_MAPPING
*)HANDLE_GetObjPtr( PROCESS_Current(),
1314 K32OBJ_MEM_MAPPED_FILE
,
1318 if (mapping
->size_high
|| offset_high
)
1319 ERR(virtual, "Offsets larger than 4Gb not supported\n");
1321 if ((offset_low
>= mapping
->size_low
) ||
1322 (count
> mapping
->size_low
- offset_low
))
1324 SetLastError( ERROR_INVALID_PARAMETER
);
1327 if (count
) size
= ROUND_SIZE( offset_low
, count
);
1328 else size
= mapping
->size_low
- offset_low
;
1332 case FILE_MAP_ALL_ACCESS
:
1333 case FILE_MAP_WRITE
:
1334 case FILE_MAP_WRITE
| FILE_MAP_READ
:
1335 if (!(mapping
->protect
& VPROT_WRITE
))
1337 SetLastError( ERROR_INVALID_PARAMETER
);
1344 case FILE_MAP_COPY
| FILE_MAP_READ
:
1345 if (mapping
->protect
& VPROT_READ
) break;
1348 SetLastError( ERROR_INVALID_PARAMETER
);
1354 TRACE(virtual, "handle=%x size=%x offset=%lx\n",
1355 handle
, size
, offset_low
);
1357 ptr
= (UINT32
)FILE_dommap( mapping
->file
, addr
, 0, size
, 0, offset_low
,
1358 VIRTUAL_GetUnixProt( mapping
->protect
),
1360 if (ptr
== (UINT32
)-1)
1362 SetLastError( ERROR_OUTOFMEMORY
);
1366 if (!(view
= VIRTUAL_CreateView( ptr
, size
, offset_low
, 0,
1367 mapping
->protect
, mapping
)))
1369 SetLastError( ERROR_OUTOFMEMORY
);
1375 if (ptr
!= (UINT32
)-1) FILE_munmap( (void *)ptr
, 0, size
);
1376 K32OBJ_DecCount( &mapping
->header
);
1381 /***********************************************************************
1382 * FlushViewOfFile (KERNEL32.262)
1383 * Writes to the disk a byte range within a mapped view of a file
1389 BOOL32 WINAPI
FlushViewOfFile(
1390 LPCVOID base
, /* [in] Start address of byte range to flush */
1391 DWORD cbFlush
/* [in] Number of bytes in range */
1394 UINT32 addr
= ROUND_ADDR( base
);
1396 TRACE(virtual, "FlushViewOfFile at %p for %ld bytes\n",
1399 if (!(view
= VIRTUAL_FindView( addr
)))
1401 SetLastError( ERROR_INVALID_PARAMETER
);
1404 if (!cbFlush
) cbFlush
= view
->size
;
1405 if (!msync( (void *)addr
, cbFlush
, MS_SYNC
)) return TRUE
;
1406 SetLastError( ERROR_INVALID_PARAMETER
);
1411 /***********************************************************************
1412 * UnmapViewOfFile (KERNEL32.540)
1413 * Unmaps a mapped view of a file.
1416 * Should addr be an LPCVOID?
1422 BOOL32 WINAPI
UnmapViewOfFile(
1423 LPVOID addr
/* [in] Address where mapped view begins */
1426 UINT32 base
= ROUND_ADDR( addr
);
1427 if (!(view
= VIRTUAL_FindView( base
)) || (base
!= view
->base
))
1429 SetLastError( ERROR_INVALID_PARAMETER
);
1432 VIRTUAL_DeleteView( view
);