2 * Win32 virtual memory functions
4 * Copyright 1997 Alexandre Julliard
14 #include <sys/types.h>
42 struct _FV
*next
; /* Next view */
43 struct _FV
*prev
; /* Prev view */
44 UINT32 base
; /* Base address */
45 UINT32 size
; /* Size in bytes */
46 UINT32 flags
; /* Allocation flags */
47 UINT32 offset
; /* Offset from start of mapped file */
48 FILE_MAPPING
*mapping
; /* File mapping */
49 HANDLERPROC handlerProc
; /* Fault handler */
50 LPVOID handlerArg
; /* Fault handler argument */
51 BYTE protect
; /* Protection for all pages at allocation time */
52 BYTE prot
[1]; /* Protection byte for each page */
55 /* Per-page protection byte values */
56 #define VPROT_READ 0x01
57 #define VPROT_WRITE 0x02
58 #define VPROT_EXEC 0x04
59 #define VPROT_WRITECOPY 0x08
60 #define VPROT_GUARD 0x10
61 #define VPROT_NOCACHE 0x20
62 #define VPROT_COMMITTED 0x40
65 #define VFLAG_SYSTEM 0x01
67 /* Conversion from VPROT_* to Win32 flags */
68 static const BYTE VIRTUAL_Win32Flags
[16] =
70 PAGE_NOACCESS
, /* 0 */
71 PAGE_READONLY
, /* READ */
72 PAGE_READWRITE
, /* WRITE */
73 PAGE_READWRITE
, /* READ | WRITE */
74 PAGE_EXECUTE
, /* EXEC */
75 PAGE_EXECUTE_READ
, /* READ | EXEC */
76 PAGE_EXECUTE_READWRITE
, /* WRITE | EXEC */
77 PAGE_EXECUTE_READWRITE
, /* READ | WRITE | EXEC */
78 PAGE_WRITECOPY
, /* WRITECOPY */
79 PAGE_WRITECOPY
, /* READ | WRITECOPY */
80 PAGE_WRITECOPY
, /* WRITE | WRITECOPY */
81 PAGE_WRITECOPY
, /* READ | WRITE | WRITECOPY */
82 PAGE_EXECUTE_WRITECOPY
, /* EXEC | WRITECOPY */
83 PAGE_EXECUTE_WRITECOPY
, /* READ | EXEC | WRITECOPY */
84 PAGE_EXECUTE_WRITECOPY
, /* WRITE | EXEC | WRITECOPY */
85 PAGE_EXECUTE_WRITECOPY
/* READ | WRITE | EXEC | WRITECOPY */
89 static FILE_VIEW
*VIRTUAL_FirstView
;
92 /* These are always the same on an i386, and it will be faster this way */
93 # define page_mask 0xfff
94 # define page_shift 12
95 # define granularity_mask 0xffff
97 static UINT32 page_shift
;
98 static UINT32 page_mask
;
99 static UINT32 granularity_mask
; /* Allocation granularity (usually 64k) */
100 #endif /* __i386__ */
102 #define ROUND_ADDR(addr) \
103 ((UINT32)(addr) & ~page_mask)
105 #define ROUND_SIZE(addr,size) \
106 (((UINT32)(size) + ((UINT32)(addr) & page_mask) + page_mask) & ~page_mask)
108 static void VIRTUAL_DestroyMapping( K32OBJ
*obj
);
110 const K32OBJ_OPS MEM_MAPPED_FILE_Ops
=
112 /* Object cannot be waited upon, so we don't need these (except destroy) */
114 NULL
, /* satisfied */
116 NULL
, /* remove_wait */
119 VIRTUAL_DestroyMapping
/* destroy */
122 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
123 if (!TRACE_ON(virtual)); else VIRTUAL_DumpView(view)
125 /***********************************************************************
128 static const char *VIRTUAL_GetProtStr( BYTE prot
)
130 static char buffer
[6];
131 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
132 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : '-';
133 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
134 buffer
[3] = (prot
& VPROT_WRITE
) ?
135 ((prot
& VPROT_WRITECOPY
) ? 'w' : 'W') : '-';
136 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
142 /***********************************************************************
145 static void VIRTUAL_DumpView( FILE_VIEW
*view
)
148 UINT32 addr
= view
->base
;
149 BYTE prot
= view
->prot
[0];
151 DUMP( "View: %08x - %08x%s",
152 view
->base
, view
->base
+ view
->size
- 1,
153 (view
->flags
& VFLAG_SYSTEM
) ? " (system)" : "" );
154 if (view
->mapping
&& view
->mapping
->file
)
155 DUMP( " %s @ %08x\n", view
->mapping
->file
->unix_name
, view
->offset
);
157 DUMP( " (anonymous)\n");
159 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
161 if (view
->prot
[i
] == prot
) continue;
162 DUMP( " %08x - %08x %s\n",
163 addr
, addr
+ (count
<< page_shift
) - 1,
164 VIRTUAL_GetProtStr(prot
) );
165 addr
+= (count
<< page_shift
);
166 prot
= view
->prot
[i
];
170 DUMP( " %08x - %08x %s\n",
171 addr
, addr
+ (count
<< page_shift
) - 1,
172 VIRTUAL_GetProtStr(prot
) );
176 /***********************************************************************
179 void VIRTUAL_Dump(void)
181 FILE_VIEW
*view
= VIRTUAL_FirstView
;
182 DUMP( "\nDump of all virtual memory views:\n\n" );
185 VIRTUAL_DumpView( view
);
191 /***********************************************************************
194 * Find the view containing a given address.
200 static FILE_VIEW
*VIRTUAL_FindView(
201 UINT32 addr
/* [in] Address */
203 FILE_VIEW
*view
= VIRTUAL_FirstView
;
206 if (view
->base
> addr
) return NULL
;
207 if (view
->base
+ view
->size
> addr
) return view
;
214 /***********************************************************************
217 * Create a new view and add it in the linked list.
219 static FILE_VIEW
*VIRTUAL_CreateView( UINT32 base
, UINT32 size
, UINT32 offset
,
220 UINT32 flags
, BYTE vprot
,
221 FILE_MAPPING
*mapping
)
223 FILE_VIEW
*view
, *prev
;
225 /* Create the view structure */
227 assert( !(base
& page_mask
) );
228 assert( !(size
& page_mask
) );
230 if (!(view
= (FILE_VIEW
*)malloc( sizeof(*view
) + size
- 1 ))) return NULL
;
232 view
->size
= size
<< page_shift
;
234 view
->offset
= offset
;
235 view
->mapping
= mapping
;
236 view
->protect
= vprot
;
237 memset( view
->prot
, vprot
, size
);
239 /* Insert it in the linked list */
241 if (!VIRTUAL_FirstView
|| (VIRTUAL_FirstView
->base
> base
))
243 view
->next
= VIRTUAL_FirstView
;
245 if (view
->next
) view
->next
->prev
= view
;
246 VIRTUAL_FirstView
= view
;
250 prev
= VIRTUAL_FirstView
;
251 while (prev
->next
&& (prev
->next
->base
< base
)) prev
= prev
->next
;
252 view
->next
= prev
->next
;
254 if (view
->next
) view
->next
->prev
= view
;
257 VIRTUAL_DEBUG_DUMP_VIEW( view
);
262 /***********************************************************************
269 static void VIRTUAL_DeleteView(
270 FILE_VIEW
*view
/* [in] View */
272 FILE_munmap( (void *)view
->base
, 0, view
->size
);
273 if (view
->next
) view
->next
->prev
= view
->prev
;
274 if (view
->prev
) view
->prev
->next
= view
->next
;
275 else VIRTUAL_FirstView
= view
->next
;
276 if (view
->mapping
) K32OBJ_DecCount( &view
->mapping
->header
);
281 /***********************************************************************
282 * VIRTUAL_GetUnixProt
284 * Convert page protections to protection for mmap/mprotect.
286 static int VIRTUAL_GetUnixProt( BYTE vprot
)
289 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
291 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
292 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
;
293 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
;
299 /***********************************************************************
300 * VIRTUAL_GetWin32Prot
302 * Convert page protections to Win32 flags.
307 static void VIRTUAL_GetWin32Prot(
308 BYTE vprot
, /* [in] Page protection flags */
309 DWORD
*protect
, /* [out] Location to store Win32 protection flags */
310 DWORD
*state
/* [out] Location to store mem state flag */
313 *protect
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
314 /* if (vprot & VPROT_GUARD) *protect |= PAGE_GUARD;*/
315 if (vprot
& VPROT_NOCACHE
) *protect
|= PAGE_NOCACHE
;
317 if (vprot
& VPROT_GUARD
) *protect
= PAGE_NOACCESS
;
320 if (state
) *state
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
324 /***********************************************************************
327 * Build page protections from Win32 flags.
330 * Value of page protection flags
332 static BYTE
VIRTUAL_GetProt(
333 DWORD protect
/* [in] Win32 protection flags */
337 switch(protect
& 0xff)
343 vprot
= VPROT_READ
| VPROT_WRITE
;
346 vprot
= VPROT_READ
| VPROT_WRITE
| VPROT_WRITECOPY
;
351 case PAGE_EXECUTE_READ
:
352 vprot
= VPROT_EXEC
| VPROT_READ
;
354 case PAGE_EXECUTE_READWRITE
:
355 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
| VPROT_WRITECOPY
;
357 case PAGE_EXECUTE_WRITECOPY
:
358 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
365 if (protect
& PAGE_GUARD
) vprot
|= VPROT_GUARD
;
366 if (protect
& PAGE_NOCACHE
) vprot
|= VPROT_NOCACHE
;
371 /***********************************************************************
374 * Change the protection of a range of pages.
380 static BOOL32
VIRTUAL_SetProt(
381 FILE_VIEW
*view
, /* [in] Pointer to view */
382 UINT32 base
, /* [in] Starting address */
383 UINT32 size
, /* [in] Size in bytes */
384 BYTE vprot
/* [in] Protections to use */
386 TRACE(virtual, "%08x-%08x %s\n",
387 base
, base
+ size
- 1, VIRTUAL_GetProtStr( vprot
) );
389 if (mprotect( (void *)base
, size
, VIRTUAL_GetUnixProt(vprot
) ))
390 return FALSE
; /* FIXME: last error */
392 memset( view
->prot
+ ((base
- view
->base
) >> page_shift
),
393 vprot
, size
>> page_shift
);
394 VIRTUAL_DEBUG_DUMP_VIEW( view
);
399 /***********************************************************************
402 * Check that all pages in a range have the given flags.
408 static BOOL32
VIRTUAL_CheckFlags(
409 UINT32 base
, /* [in] Starting address */
410 UINT32 size
, /* [in] Size in bytes */
411 BYTE flags
/* [in] Flags to check for */
416 if (!size
) return TRUE
;
417 if (!(view
= VIRTUAL_FindView( base
))) return FALSE
;
418 if (view
->base
+ view
->size
< base
+ size
) return FALSE
;
419 page
= (base
- view
->base
) >> page_shift
;
420 size
= ROUND_SIZE( base
, size
) >> page_shift
;
421 while (size
--) if ((view
->prot
[page
++] & flags
) != flags
) return FALSE
;
426 /***********************************************************************
429 BOOL32
VIRTUAL_Init(void)
434 # ifdef HAVE_GETPAGESIZE
435 page_size
= getpagesize();
438 page_size
= sysconf(_SC_PAGESIZE
);
440 # error Cannot get the page size on this platform
443 page_mask
= page_size
- 1;
444 granularity_mask
= 0xffff; /* hard-coded for now */
445 /* Make sure we have a power of 2 */
446 assert( !(page_size
& page_mask
) );
448 while ((1 << page_shift
) != page_size
) page_shift
++;
449 #endif /* !__i386__ */
453 /* Do not use stdio here since it may temporarily change the size
454 * of some segments (ie libc6 adds 0x1000 per open FILE)
456 int fd
= open ("/proc/self/maps", O_RDONLY
);
459 char buffer
[512]; /* line might be rather long in 2.1 */
463 int start
, end
, offset
;
465 BYTE vprot
= VPROT_COMMITTED
;
468 int count
= sizeof(buffer
);
469 while (1 == read(fd
, ptr
, 1) && *ptr
!= '\n' && --count
> 0)
472 if (*ptr
!= '\n') break;
475 sscanf( buffer
, "%x-%x %c%c%c%c %x",
476 &start
, &end
, &r
, &w
, &x
, &p
, &offset
);
477 if (r
== 'r') vprot
|= VPROT_READ
;
478 if (w
== 'w') vprot
|= VPROT_WRITE
;
479 if (x
== 'x') vprot
|= VPROT_EXEC
;
480 if (p
== 'p') vprot
|= VPROT_WRITECOPY
;
481 VIRTUAL_CreateView( start
, end
- start
, 0,
482 VFLAG_SYSTEM
, vprot
, NULL
);
492 /***********************************************************************
493 * VIRTUAL_GetPageSize
495 DWORD
VIRTUAL_GetPageSize(void)
497 return 1 << page_shift
;
501 /***********************************************************************
502 * VIRTUAL_GetGranularity
504 DWORD
VIRTUAL_GetGranularity(void)
506 return granularity_mask
+ 1;
510 /***********************************************************************
511 * VIRTUAL_SetFaultHandler
513 BOOL32
VIRTUAL_SetFaultHandler( LPVOID addr
, HANDLERPROC proc
, LPVOID arg
)
517 if (!(view
= VIRTUAL_FindView((UINT32
)addr
))) return FALSE
;
518 view
->handlerProc
= proc
;
519 view
->handlerArg
= arg
;
523 /***********************************************************************
524 * VIRTUAL_HandleFault
526 BOOL32
VIRTUAL_HandleFault(LPVOID addr
)
528 FILE_VIEW
*view
= VIRTUAL_FindView((UINT32
)addr
);
530 if (view
&& view
->handlerProc
)
531 return view
->handlerProc(view
->handlerArg
, addr
);
536 /***********************************************************************
537 * VirtualAlloc (KERNEL32.548)
538 * Reserves or commits a region of pages in virtual address space
541 * Base address of allocated region of pages
544 LPVOID WINAPI
VirtualAlloc(
545 LPVOID addr
, /* [in] Address of region to reserve or commit */
546 DWORD size
, /* [in] Size of region */
547 DWORD type
, /* [in] Type of allocation */
548 DWORD protect
/* [in] Type of access protection */
551 UINT32 base
, ptr
, view_size
;
554 TRACE(virtual, "%08x %08lx %lx %08lx\n",
555 (UINT32
)addr
, size
, type
, protect
);
557 /* Round parameters to a page boundary */
559 if (size
> 0x7fc00000) /* 2Gb - 4Mb */
561 SetLastError( ERROR_OUTOFMEMORY
);
566 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
567 base
= ((UINT32
)addr
+ granularity_mask
) & ~granularity_mask
;
569 base
= ROUND_ADDR( addr
);
570 size
= (((UINT32
)addr
+ size
+ page_mask
) & ~page_mask
) - base
;
571 if (base
+ size
< base
) /* Disallow wrap-around */
573 SetLastError( ERROR_INVALID_PARAMETER
);
580 size
= (size
+ page_mask
) & ~page_mask
;
583 if (type
& MEM_TOP_DOWN
) {
584 /* FIXME: MEM_TOP_DOWN allocates the largest possible address.
585 * Is there _ANY_ way to do it with UNIX mmap()?
587 WARN(virtual,"MEM_TOP_DOWN ignored\n");
588 type
&= ~MEM_TOP_DOWN
;
590 /* Compute the protection flags */
592 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
)) ||
593 (type
& ~(MEM_COMMIT
| MEM_RESERVE
)))
595 SetLastError( ERROR_INVALID_PARAMETER
);
598 if (type
& MEM_COMMIT
)
599 vprot
= VIRTUAL_GetProt( protect
) | VPROT_COMMITTED
;
602 /* Reserve the memory */
604 if ((type
& MEM_RESERVE
) || !base
)
606 view_size
= size
+ (base
? 0 : granularity_mask
+ 1);
607 ptr
= (UINT32
)FILE_dommap( NULL
, (LPVOID
)base
, 0, view_size
, 0, 0,
608 VIRTUAL_GetUnixProt( vprot
), MAP_PRIVATE
);
609 if (ptr
== (UINT32
)-1)
611 SetLastError( ERROR_OUTOFMEMORY
);
616 /* Release the extra memory while keeping the range */
617 /* starting on a 64k boundary. */
619 if (ptr
& granularity_mask
)
621 UINT32 extra
= granularity_mask
+ 1 - (ptr
& granularity_mask
);
622 FILE_munmap( (void *)ptr
, 0, extra
);
626 if (view_size
> size
)
627 FILE_munmap( (void *)(ptr
+ size
), 0, view_size
- size
);
629 else if (ptr
!= base
)
631 /* We couldn't get the address we wanted */
632 FILE_munmap( (void *)ptr
, 0, view_size
);
633 SetLastError( ERROR_INVALID_ADDRESS
);
636 if (!(view
= VIRTUAL_CreateView( ptr
, size
, 0, 0, vprot
, NULL
)))
638 FILE_munmap( (void *)ptr
, 0, size
);
639 SetLastError( ERROR_OUTOFMEMORY
);
642 VIRTUAL_DEBUG_DUMP_VIEW( view
);
646 /* Commit the pages */
648 if (!(view
= VIRTUAL_FindView( base
)) ||
649 (base
+ size
> view
->base
+ view
->size
))
651 SetLastError( ERROR_INVALID_PARAMETER
);
655 if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) return NULL
;
660 /***********************************************************************
661 * VirtualFree (KERNEL32.550)
662 * Release or decommits a region of pages in virtual address space.
668 BOOL32 WINAPI
VirtualFree(
669 LPVOID addr
, /* [in] Address of region of committed pages */
670 DWORD size
, /* [in] Size of region */
671 DWORD type
/* [in] Type of operation */
676 TRACE(virtual, "%08x %08lx %lx\n",
677 (UINT32
)addr
, size
, type
);
679 /* Fix the parameters */
681 size
= ROUND_SIZE( addr
, size
);
682 base
= ROUND_ADDR( addr
);
684 if (!(view
= VIRTUAL_FindView( base
)) ||
685 (base
+ size
> view
->base
+ view
->size
))
687 SetLastError( ERROR_INVALID_PARAMETER
);
691 /* Compute the protection flags */
693 if ((type
!= MEM_DECOMMIT
) && (type
!= MEM_RELEASE
))
695 SetLastError( ERROR_INVALID_PARAMETER
);
701 if (type
== MEM_RELEASE
)
703 if (size
|| (base
!= view
->base
))
705 SetLastError( ERROR_INVALID_PARAMETER
);
708 VIRTUAL_DeleteView( view
);
712 /* Decommit the pages */
713 return VIRTUAL_SetProt( view
, base
, size
, 0 );
717 /***********************************************************************
718 * VirtualLock (KERNEL32.551)
719 * Locks the specified region of virtual address space
722 * Always returns TRUE
728 BOOL32 WINAPI
VirtualLock(
729 LPVOID addr
, /* [in] Address of first byte of range to lock */
730 DWORD size
/* [in] Number of bytes in range to lock */
736 /***********************************************************************
737 * VirtualUnlock (KERNEL32.556)
738 * Unlocks a range of pages in the virtual address space
741 * Always returns TRUE
747 BOOL32 WINAPI
VirtualUnlock(
748 LPVOID addr
, /* [in] Address of first byte of range */
749 DWORD size
/* [in] Number of bytes in range */
755 /***********************************************************************
756 * VirtualProtect (KERNEL32.552)
757 * Changes the access protection on a region of committed pages
763 BOOL32 WINAPI
VirtualProtect(
764 LPVOID addr
, /* [in] Address of region of committed pages */
765 DWORD size
, /* [in] Size of region */
766 DWORD new_prot
, /* [in] Desired access protection */
767 LPDWORD old_prot
/* [out] Address of variable to get old protection */
773 TRACE(virtual, "%08x %08lx %08lx\n",
774 (UINT32
)addr
, size
, new_prot
);
776 /* Fix the parameters */
778 size
= ROUND_SIZE( addr
, size
);
779 base
= ROUND_ADDR( addr
);
781 if (!(view
= VIRTUAL_FindView( base
)) ||
782 (base
+ size
> view
->base
+ view
->size
))
784 SetLastError( ERROR_INVALID_PARAMETER
);
788 /* Make sure all the pages are committed */
790 p
= view
->prot
+ ((base
- view
->base
) >> page_shift
);
791 for (i
= size
>> page_shift
; i
; i
--, p
++)
793 if (!(*p
& VPROT_COMMITTED
))
795 SetLastError( ERROR_INVALID_PARAMETER
);
800 VIRTUAL_GetWin32Prot( view
->prot
[0], old_prot
, NULL
);
801 vprot
= VIRTUAL_GetProt( new_prot
) | VPROT_COMMITTED
;
802 return VIRTUAL_SetProt( view
, base
, size
, vprot
);
806 /***********************************************************************
807 * VirtualProtectEx (KERNEL32.553)
808 * Changes the access protection on a region of committed pages in the
809 * virtual address space of a specified process
815 BOOL32 WINAPI
VirtualProtectEx(
816 HANDLE32 handle
, /* [in] Handle of process */
817 LPVOID addr
, /* [in] Address of region of committed pages */
818 DWORD size
, /* [in] Size of region */
819 DWORD new_prot
, /* [in] Desired access protection */
820 LPDWORD old_prot
/* [out] Address of variable to get old protection */
824 PDB32
*pdb
= PROCESS_GetPtr( handle
, PROCESS_VM_OPERATION
, NULL
);
827 if (pdb
== PROCESS_Current())
828 ret
= VirtualProtect( addr
, size
, new_prot
, old_prot
);
830 ERR(virtual,"Unsupported on other process\n");
831 K32OBJ_DecCount( &pdb
->header
);
837 /***********************************************************************
838 * VirtualQuery (KERNEL32.554)
839 * Provides info about a range of pages in virtual address space
842 * Number of bytes returned in information buffer
844 DWORD WINAPI
VirtualQuery(
845 LPCVOID addr
, /* [in] Address of region */
846 LPMEMORY_BASIC_INFORMATION info
, /* [out] Address of info buffer */
847 DWORD len
/* [in] Size of buffer */
849 FILE_VIEW
*view
= VIRTUAL_FirstView
;
850 UINT32 base
= ROUND_ADDR( addr
);
851 UINT32 alloc_base
= 0;
854 /* Find the view containing the address */
860 size
= 0xffff0000 - alloc_base
;
863 if (view
->base
> base
)
865 size
= view
->base
- alloc_base
;
869 if (view
->base
+ view
->size
> base
)
871 alloc_base
= view
->base
;
875 alloc_base
= view
->base
+ view
->size
;
879 /* Fill the info structure */
883 info
->State
= MEM_FREE
;
885 info
->AllocationProtect
= 0;
890 BYTE vprot
= view
->prot
[(base
- alloc_base
) >> page_shift
];
891 VIRTUAL_GetWin32Prot( vprot
, &info
->Protect
, &info
->State
);
892 for (size
= base
- alloc_base
; size
< view
->size
; size
+= page_mask
+1)
893 if (view
->prot
[size
>> page_shift
] != vprot
) break;
894 info
->AllocationProtect
= view
->protect
;
895 info
->Type
= MEM_PRIVATE
; /* FIXME */
898 info
->BaseAddress
= (LPVOID
)base
;
899 info
->AllocationBase
= (LPVOID
)alloc_base
;
900 info
->RegionSize
= size
- (base
- alloc_base
);
901 return sizeof(*info
);
905 /***********************************************************************
906 * VirtualQueryEx (KERNEL32.555)
907 * Provides info about a range of pages in virtual address space of a
911 * Number of bytes returned in information buffer
913 DWORD WINAPI
VirtualQueryEx(
914 HANDLE32 handle
, /* [in] Handle of process */
915 LPCVOID addr
, /* [in] Address of region */
916 LPMEMORY_BASIC_INFORMATION info
, /* [out] Address of info buffer */
917 DWORD len
/* [in] Size of buffer */
921 PDB32
*pdb
= PROCESS_GetPtr( handle
, PROCESS_QUERY_INFORMATION
, NULL
);
924 if (pdb
== PROCESS_Current())
925 ret
= VirtualQuery( addr
, info
, len
);
927 ERR(virtual,"Unsupported on other process\n");
928 K32OBJ_DecCount( &pdb
->header
);
934 /***********************************************************************
935 * IsBadReadPtr32 (KERNEL32.354)
938 * FALSE: Process has read access to entire block
941 BOOL32 WINAPI
IsBadReadPtr32(
942 LPCVOID ptr
, /* Address of memory block */
943 UINT32 size
/* Size of block */
945 return !VIRTUAL_CheckFlags( (UINT32
)ptr
, size
,
946 VPROT_READ
| VPROT_COMMITTED
);
950 /***********************************************************************
951 * IsBadWritePtr32 (KERNEL32.357)
954 * FALSE: Process has write access to entire block
957 BOOL32 WINAPI
IsBadWritePtr32(
958 LPVOID ptr
, /* [in] Address of memory block */
959 UINT32 size
/* [in] Size of block in bytes */
961 return !VIRTUAL_CheckFlags( (UINT32
)ptr
, size
,
962 VPROT_WRITE
| VPROT_COMMITTED
);
966 /***********************************************************************
967 * IsBadHugeReadPtr32 (KERNEL32.352)
969 * FALSE: Process has read access to entire block
972 BOOL32 WINAPI
IsBadHugeReadPtr32(
973 LPCVOID ptr
, /* [in] Address of memory block */
974 UINT32 size
/* [in] Size of block */
976 return IsBadReadPtr32( ptr
, size
);
980 /***********************************************************************
981 * IsBadHugeWritePtr32 (KERNEL32.353)
983 * FALSE: Process has write access to entire block
986 BOOL32 WINAPI
IsBadHugeWritePtr32(
987 LPVOID ptr
, /* [in] Address of memory block */
988 UINT32 size
/* [in] Size of block */
990 return IsBadWritePtr32( ptr
, size
);
994 /***********************************************************************
995 * IsBadCodePtr32 (KERNEL32.351)
998 * FALSE: Process has read access to specified memory
1001 BOOL32 WINAPI
IsBadCodePtr32(
1002 FARPROC32 ptr
/* [in] Address of function */
1004 return !VIRTUAL_CheckFlags( (UINT32
)ptr
, 1, VPROT_EXEC
| VPROT_COMMITTED
);
1008 /***********************************************************************
1009 * IsBadStringPtr32A (KERNEL32.355)
1012 * FALSE: Read access to all bytes in string
1015 BOOL32 WINAPI
IsBadStringPtr32A(
1016 LPCSTR str
, /* [in] Address of string */
1017 UINT32 max
/* [in] Maximum size of string */
1022 if (!max
) return FALSE
;
1023 if (!(view
= VIRTUAL_FindView( (UINT32
)str
))) return TRUE
;
1024 page
= ((UINT32
)str
- view
->base
) >> page_shift
;
1025 count
= page_mask
+ 1 - ((UINT32
)str
& page_mask
);
1029 if ((view
->prot
[page
] & (VPROT_READ
| VPROT_COMMITTED
)) !=
1030 (VPROT_READ
| VPROT_COMMITTED
))
1032 if (count
> max
) count
= max
;
1034 while (count
--) if (!*str
++) return FALSE
;
1035 if (++page
>= view
->size
>> page_shift
) return TRUE
;
1036 count
= page_mask
+ 1;
1042 /***********************************************************************
1043 * IsBadStringPtr32W (KERNEL32.356)
1044 * See IsBadStringPtr32A
1046 BOOL32 WINAPI
IsBadStringPtr32W( LPCWSTR str
, UINT32 max
)
1051 if (!max
) return FALSE
;
1052 if (!(view
= VIRTUAL_FindView( (UINT32
)str
))) return TRUE
;
1053 page
= ((UINT32
)str
- view
->base
) >> page_shift
;
1054 count
= (page_mask
+ 1 - ((UINT32
)str
& page_mask
)) / sizeof(WCHAR
);
1058 if ((view
->prot
[page
] & (VPROT_READ
| VPROT_COMMITTED
)) !=
1059 (VPROT_READ
| VPROT_COMMITTED
))
1061 if (count
> max
) count
= max
;
1063 while (count
--) if (!*str
++) return FALSE
;
1064 if (++page
>= view
->size
>> page_shift
) return TRUE
;
1065 count
= (page_mask
+ 1) / sizeof(WCHAR
);
1071 /***********************************************************************
1072 * CreateFileMapping32A (KERNEL32.46)
1073 * Creates a named or unnamed file-mapping object for the specified file
1077 * 0: Mapping object does not exist
1080 HANDLE32 WINAPI
CreateFileMapping32A(
1081 HFILE32 hFile
, /* [in] Handle of file to map */
1082 SECURITY_ATTRIBUTES
*sa
, /* [in] Optional security attributes*/
1083 DWORD protect
, /* [in] Protection for mapping object */
1084 DWORD size_high
, /* [in] High-order 32 bits of object size */
1085 DWORD size_low
, /* [in] Low-order 32 bits of object size */
1086 LPCSTR name
/* [in] Name of file-mapping object */ )
1088 FILE_MAPPING
*mapping
= NULL
;
1091 BOOL32 inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
1093 /* First search for an object with the same name */
1095 K32OBJ
*obj
= K32OBJ_FindName( name
);
1099 if (obj
->type
== K32OBJ_MEM_MAPPED_FILE
)
1101 SetLastError( ERROR_ALREADY_EXISTS
);
1102 handle
= HANDLE_Alloc( PROCESS_Current(), obj
,
1103 FILE_MAP_ALL_ACCESS
/*FIXME*/, inherit
, -1 );
1107 SetLastError( ERROR_DUP_NAME
);
1110 K32OBJ_DecCount( obj
);
1114 /* Check parameters */
1116 TRACE(virtual,"(%x,%p,%08lx,%08lx%08lx,%s)\n",
1117 hFile
, sa
, protect
, size_high
, size_low
, debugstr_a(name
) );
1119 vprot
= VIRTUAL_GetProt( protect
);
1120 if (protect
& SEC_RESERVE
)
1122 if (hFile
!= INVALID_HANDLE_VALUE32
)
1124 SetLastError( ERROR_INVALID_PARAMETER
);
1128 else vprot
|= VPROT_COMMITTED
;
1129 if (protect
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
1131 /* Compute the size and extend the file if necessary */
1133 if (hFile
== INVALID_HANDLE_VALUE32
)
1135 if (!size_high
&& !size_low
)
1137 SetLastError( ERROR_INVALID_PARAMETER
);
1145 GetTempPath32A(260,buf
);
1146 GetTempFileName32A(buf
,"wine",0,buf
);
1147 hFile
= CreateFile32A(
1149 GENERIC_READ
|GENERIC_WRITE
,
1150 FILE_SHARE_READ
|FILE_SHARE_WRITE
,/* so we can reuse the tmpfn */
1156 /* FIXME: bad hack to avoid lots of leftover tempfiles */
1158 if (hFile
== INVALID_HANDLE_VALUE32
)
1159 FIXME(virtual,"could not create temp. file for anon shared mapping: reason was 0x%08lx\n",GetLastError());
1162 if (hFile
!= INVALID_HANDLE_VALUE32
) /* We have a file */
1164 BY_HANDLE_FILE_INFORMATION info
;
1165 DWORD access
= GENERIC_READ
;
1167 if (((protect
& 0xff) == PAGE_READWRITE
) ||
1168 ((protect
& 0xff) == PAGE_WRITECOPY
) ||
1169 ((protect
& 0xff) == PAGE_EXECUTE_READWRITE
) ||
1170 ((protect
& 0xff) == PAGE_EXECUTE_WRITECOPY
))
1171 access
|= GENERIC_WRITE
;
1172 if (!(obj
= HANDLE_GetObjPtr( PROCESS_Current(), hFile
,
1173 K32OBJ_FILE
, access
, NULL
)))
1176 if (!GetFileInformationByHandle( hFile
, &info
)) goto error
;
1177 if (!size_high
&& !size_low
)
1179 size_high
= info
.nFileSizeHigh
;
1180 size_low
= info
.nFileSizeLow
;
1182 else if ((size_high
> info
.nFileSizeHigh
) ||
1183 ((size_high
== info
.nFileSizeHigh
) &&
1184 (size_low
> info
.nFileSizeLow
)))
1186 /* We have to grow the file */
1187 if (SetFilePointer( hFile
, size_low
, &size_high
,
1188 FILE_BEGIN
) == 0xffffffff) goto error
;
1189 if (!SetEndOfFile( hFile
)) goto error
;
1193 /* Allocate the mapping object */
1195 if (!(mapping
= HeapAlloc( SystemHeap
, 0, sizeof(*mapping
) ))) goto error
;
1196 mapping
->header
.type
= K32OBJ_MEM_MAPPED_FILE
;
1197 mapping
->header
.refcount
= 1;
1198 mapping
->protect
= vprot
;
1199 mapping
->size_high
= size_high
;
1200 mapping
->size_low
= ROUND_SIZE( 0, size_low
);
1201 mapping
->file
= (FILE_OBJECT
*)obj
;
1203 if (!K32OBJ_AddName( &mapping
->header
, name
)) handle
= 0;
1204 else handle
= HANDLE_Alloc( PROCESS_Current(), &mapping
->header
,
1205 FILE_MAP_ALL_ACCESS
/*FIXME*/, inherit
, -1 );
1206 K32OBJ_DecCount( &mapping
->header
);
1207 SetLastError(0); /* Last error value is relevant. (see the start of fun) */
1211 if (obj
) K32OBJ_DecCount( obj
);
1212 if (mapping
) HeapFree( SystemHeap
, 0, mapping
);
1217 /***********************************************************************
1218 * CreateFileMapping32W (KERNEL32.47)
1219 * See CreateFileMapping32A
1221 HANDLE32 WINAPI
CreateFileMapping32W( HFILE32 hFile
, LPSECURITY_ATTRIBUTES attr
,
1222 DWORD protect
, DWORD size_high
,
1223 DWORD size_low
, LPCWSTR name
)
1225 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
1226 HANDLE32 ret
= CreateFileMapping32A( hFile
, attr
, protect
,
1227 size_high
, size_low
, nameA
);
1228 HeapFree( GetProcessHeap(), 0, nameA
);
1233 /***********************************************************************
1234 * OpenFileMapping32A (KERNEL32.397)
1235 * Opens a named file-mapping object.
1241 HANDLE32 WINAPI
OpenFileMapping32A(
1242 DWORD access
, /* [in] Access mode */
1243 BOOL32 inherit
, /* [in] Inherit flag */
1244 LPCSTR name
/* [in] Name of file-mapping object */
1246 HANDLE32 handle
= 0;
1249 if ((obj
= K32OBJ_FindNameType( name
, K32OBJ_MEM_MAPPED_FILE
)))
1251 handle
= HANDLE_Alloc( PROCESS_Current(), obj
, access
, inherit
, -1 );
1252 K32OBJ_DecCount( obj
);
1259 /***********************************************************************
1260 * OpenFileMapping32W (KERNEL32.398)
1261 * See OpenFileMapping32A
1263 HANDLE32 WINAPI
OpenFileMapping32W( DWORD access
, BOOL32 inherit
, LPCWSTR name
)
1265 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
1266 HANDLE32 ret
= OpenFileMapping32A( access
, inherit
, nameA
);
1267 HeapFree( GetProcessHeap(), 0, nameA
);
1272 /***********************************************************************
1273 * VIRTUAL_DestroyMapping
1275 * Destroy a FILE_MAPPING object.
1277 static void VIRTUAL_DestroyMapping( K32OBJ
*ptr
)
1279 FILE_MAPPING
*mapping
= (FILE_MAPPING
*)ptr
;
1280 assert( ptr
->type
== K32OBJ_MEM_MAPPED_FILE
);
1282 if (mapping
->file
) K32OBJ_DecCount( &mapping
->file
->header
);
1283 ptr
->type
= K32OBJ_UNKNOWN
;
1284 HeapFree( SystemHeap
, 0, mapping
);
1288 /***********************************************************************
1289 * MapViewOfFile (KERNEL32.385)
1290 * Maps a view of a file into the address space
1293 * Starting address of mapped view
1296 LPVOID WINAPI
MapViewOfFile(
1297 HANDLE32 mapping
, /* [in] File-mapping object to map */
1298 DWORD access
, /* [in] Access mode */
1299 DWORD offset_high
, /* [in] High-order 32 bits of file offset */
1300 DWORD offset_low
, /* [in] Low-order 32 bits of file offset */
1301 DWORD count
/* [in] Number of bytes to map */
1303 return MapViewOfFileEx( mapping
, access
, offset_high
,
1304 offset_low
, count
, NULL
);
1308 /***********************************************************************
1309 * MapViewOfFileEx (KERNEL32.386)
1310 * Maps a view of a file into the address space
1313 * Starting address of mapped view
1316 LPVOID WINAPI
MapViewOfFileEx(
1317 HANDLE32 handle
, /* [in] File-mapping object to map */
1318 DWORD access
, /* [in] Access mode */
1319 DWORD offset_high
, /* [in] High-order 32 bits of file offset */
1320 DWORD offset_low
, /* [in] Low-order 32 bits of file offset */
1321 DWORD count
, /* [in] Number of bytes to map */
1322 LPVOID addr
/* [in] Suggested starting address for mapped view */
1324 FILE_MAPPING
*mapping
;
1326 UINT32 ptr
= (UINT32
)-1, size
= 0;
1327 int flags
= MAP_PRIVATE
;
1329 /* Check parameters */
1331 if ((offset_low
& granularity_mask
) ||
1332 (addr
&& ((UINT32
)addr
& granularity_mask
)))
1334 SetLastError( ERROR_INVALID_PARAMETER
);
1338 if (!(mapping
= (FILE_MAPPING
*)HANDLE_GetObjPtr( PROCESS_Current(),
1340 K32OBJ_MEM_MAPPED_FILE
,
1341 0 /* FIXME */, NULL
)))
1344 if (mapping
->size_high
|| offset_high
)
1345 ERR(virtual, "Offsets larger than 4Gb not supported\n");
1347 if ((offset_low
>= mapping
->size_low
) ||
1348 (count
> mapping
->size_low
- offset_low
))
1350 SetLastError( ERROR_INVALID_PARAMETER
);
1353 if (count
) size
= ROUND_SIZE( offset_low
, count
);
1354 else size
= mapping
->size_low
- offset_low
;
1358 case FILE_MAP_ALL_ACCESS
:
1359 case FILE_MAP_WRITE
:
1360 case FILE_MAP_WRITE
| FILE_MAP_READ
:
1361 if (!(mapping
->protect
& VPROT_WRITE
))
1363 SetLastError( ERROR_INVALID_PARAMETER
);
1370 case FILE_MAP_COPY
| FILE_MAP_READ
:
1371 if (mapping
->protect
& VPROT_READ
) break;
1374 SetLastError( ERROR_INVALID_PARAMETER
);
1380 TRACE(virtual, "handle=%x size=%x offset=%lx\n",
1381 handle
, size
, offset_low
);
1383 ptr
= (UINT32
)FILE_dommap( mapping
->file
, addr
, 0, size
, 0, offset_low
,
1384 VIRTUAL_GetUnixProt( mapping
->protect
),
1386 if (ptr
== (UINT32
)-1) {
1387 /* KB: Q125713, 25-SEP-1995, "Common File Mapping Problems and
1388 * Platform Differences":
1389 * Windows NT: ERROR_INVALID_PARAMETER
1390 * Windows 95: ERROR_INVALID_ADDRESS.
1391 * FIXME: So should we add a module dependend check here? -MM
1394 SetLastError( ERROR_OUTOFMEMORY
);
1396 SetLastError( ERROR_INVALID_PARAMETER
);
1400 if (!(view
= VIRTUAL_CreateView( ptr
, size
, offset_low
, 0,
1401 mapping
->protect
, mapping
)))
1403 SetLastError( ERROR_OUTOFMEMORY
);
1409 if (ptr
!= (UINT32
)-1) FILE_munmap( (void *)ptr
, 0, size
);
1410 K32OBJ_DecCount( &mapping
->header
);
1415 /***********************************************************************
1416 * FlushViewOfFile (KERNEL32.262)
1417 * Writes to the disk a byte range within a mapped view of a file
1423 BOOL32 WINAPI
FlushViewOfFile(
1424 LPCVOID base
, /* [in] Start address of byte range to flush */
1425 DWORD cbFlush
/* [in] Number of bytes in range */
1428 UINT32 addr
= ROUND_ADDR( base
);
1430 TRACE(virtual, "FlushViewOfFile at %p for %ld bytes\n",
1433 if (!(view
= VIRTUAL_FindView( addr
)))
1435 SetLastError( ERROR_INVALID_PARAMETER
);
1438 if (!cbFlush
) cbFlush
= view
->size
;
1439 if (!msync( (void *)addr
, cbFlush
, MS_SYNC
)) return TRUE
;
1440 SetLastError( ERROR_INVALID_PARAMETER
);
1445 /***********************************************************************
1446 * UnmapViewOfFile (KERNEL32.540)
1447 * Unmaps a mapped view of a file.
1450 * Should addr be an LPCVOID?
1456 BOOL32 WINAPI
UnmapViewOfFile(
1457 LPVOID addr
/* [in] Address where mapped view begins */
1460 UINT32 base
= ROUND_ADDR( addr
);
1461 if (!(view
= VIRTUAL_FindView( base
)) || (base
!= view
->base
))
1463 SetLastError( ERROR_INVALID_PARAMETER
);
1466 VIRTUAL_DeleteView( view
);
1470 /***********************************************************************
1473 * Helper function to map a file to memory:
1475 * [RETURN] ptr - pointer to mapped file
1477 LPVOID
VIRTUAL_MapFileW( LPCWSTR name
)
1479 HANDLE32 hFile
, hMapping
;
1482 hFile
= CreateFile32W( name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
1483 OPEN_EXISTING
, FILE_FLAG_RANDOM_ACCESS
, 0);
1484 if (hFile
!= INVALID_HANDLE_VALUE32
)
1486 hMapping
= CreateFileMapping32A( hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
1487 CloseHandle( hFile
);
1490 ptr
= MapViewOfFile( hMapping
, FILE_MAP_READ
, 0, 0, 0 );
1491 CloseHandle( hMapping
);