2 * Win32 virtual memory functions
4 * Copyright 1997 Alexandre Julliard
12 #include <sys/types.h>
36 struct _FV
*next
; /* Next view */
37 struct _FV
*prev
; /* Prev view */
38 UINT32 base
; /* Base address */
39 UINT32 size
; /* Size in bytes */
40 UINT32 flags
; /* Allocation flags */
41 UINT32 offset
; /* Offset from start of mapped file */
42 FILE_MAPPING
*mapping
; /* File mapping */
43 BYTE protect
; /* Protection for all pages at allocation time */
44 BYTE prot
[1]; /* Protection byte for each page */
47 /* Per-page protection byte values */
48 #define VPROT_READ 0x01
49 #define VPROT_WRITE 0x02
50 #define VPROT_EXEC 0x04
51 #define VPROT_WRITECOPY 0x08
52 #define VPROT_GUARD 0x10
53 #define VPROT_NOCACHE 0x20
54 #define VPROT_COMMITTED 0x40
57 #define VFLAG_SYSTEM 0x01
59 /* Conversion from VPROT_* to Win32 flags */
60 static const BYTE VIRTUAL_Win32Flags
[16] =
62 PAGE_NOACCESS
, /* 0 */
63 PAGE_READONLY
, /* READ */
64 PAGE_READWRITE
, /* WRITE */
65 PAGE_READWRITE
, /* READ | WRITE */
66 PAGE_EXECUTE
, /* EXEC */
67 PAGE_EXECUTE_READ
, /* READ | EXEC */
68 PAGE_EXECUTE_READWRITE
, /* WRITE | EXEC */
69 PAGE_EXECUTE_READWRITE
, /* READ | WRITE | EXEC */
70 PAGE_WRITECOPY
, /* WRITECOPY */
71 PAGE_WRITECOPY
, /* READ | WRITECOPY */
72 PAGE_WRITECOPY
, /* WRITE | WRITECOPY */
73 PAGE_WRITECOPY
, /* READ | WRITE | WRITECOPY */
74 PAGE_EXECUTE_WRITECOPY
, /* EXEC | WRITECOPY */
75 PAGE_EXECUTE_WRITECOPY
, /* READ | EXEC | WRITECOPY */
76 PAGE_EXECUTE_WRITECOPY
, /* WRITE | EXEC | WRITECOPY */
77 PAGE_EXECUTE_WRITECOPY
/* READ | WRITE | EXEC | WRITECOPY */
81 static FILE_VIEW
*VIRTUAL_FirstView
;
83 static UINT32 page_shift
;
84 static UINT32 page_mask
;
85 static UINT32 granularity_mask
; /* Allocation granularity (usually 64k) */
87 #define ROUND_ADDR(addr) \
88 ((UINT32)(addr) & ~page_mask)
90 #define ROUND_SIZE(addr,size) \
91 (((UINT32)(size) + ((UINT32)(addr) & page_mask) + page_mask) & ~page_mask)
94 /***********************************************************************
97 static const char *VIRTUAL_GetProtStr( BYTE prot
)
99 static char buffer
[6];
100 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
101 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : '-';
102 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
103 buffer
[3] = (prot
& VPROT_WRITE
) ?
104 ((prot
& VPROT_WRITECOPY
) ? 'w' : 'W') : '-';
105 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
111 /***********************************************************************
114 static void VIRTUAL_DumpView( FILE_VIEW
*view
)
117 UINT32 addr
= view
->base
;
118 BYTE prot
= view
->prot
[0];
120 fprintf( stderr
, "View: %08x - %08x%s",
121 view
->base
, view
->base
+ view
->size
- 1,
122 (view
->flags
& VFLAG_SYSTEM
) ? " (system)" : "" );
123 if (view
->mapping
&& view
->mapping
->file
)
124 fprintf( stderr
, " %s @ %08x\n",
125 view
->mapping
->file
->unix_name
, view
->offset
);
127 fprintf( stderr
, " (anonymous)\n");
129 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
131 if (view
->prot
[i
] == prot
) continue;
132 fprintf( stderr
, " %08x - %08x %s\n",
133 addr
, addr
+ (count
<< page_shift
) - 1,
134 VIRTUAL_GetProtStr(prot
) );
135 addr
+= (count
<< page_shift
);
136 prot
= view
->prot
[i
];
140 fprintf( stderr
, " %08x - %08x %s\n",
141 addr
, addr
+ (count
<< page_shift
) - 1,
142 VIRTUAL_GetProtStr(prot
) );
146 /***********************************************************************
149 void VIRTUAL_Dump(void)
151 FILE_VIEW
*view
= VIRTUAL_FirstView
;
152 fprintf( stderr
, "\nDump of all virtual memory views:\n\n" );
155 VIRTUAL_DumpView( view
);
161 /***********************************************************************
164 * Find the view containing a given address.
166 static FILE_VIEW
*VIRTUAL_FindView( UINT32 addr
)
168 FILE_VIEW
*view
= VIRTUAL_FirstView
;
171 if (view
->base
> addr
) return NULL
;
172 if (view
->base
+ view
->size
> addr
) return view
;
179 /***********************************************************************
182 * Create a new view and add it in the linked list.
184 static FILE_VIEW
*VIRTUAL_CreateView( UINT32 base
, UINT32 size
, UINT32 offset
,
185 UINT32 flags
, BYTE vprot
,
186 FILE_MAPPING
*mapping
)
188 FILE_VIEW
*view
, *prev
;
190 /* Create the view structure */
193 if (!(view
= (FILE_VIEW
*)malloc( sizeof(*view
) + size
- 1 ))) return NULL
;
195 view
->size
= size
<< page_shift
;
197 view
->offset
= offset
;
198 view
->mapping
= mapping
;
199 view
->protect
= vprot
;
200 memset( view
->prot
, vprot
, size
);
202 /* Insert it in the linked list */
204 if (!VIRTUAL_FirstView
|| (VIRTUAL_FirstView
->base
> base
))
206 view
->next
= VIRTUAL_FirstView
;
208 if (view
->next
) view
->next
->prev
= view
;
209 VIRTUAL_FirstView
= view
;
213 prev
= VIRTUAL_FirstView
;
214 while (prev
->next
&& (prev
->next
->base
< base
)) prev
= prev
->next
;
215 view
->next
= prev
->next
;
217 if (view
->next
) view
->next
->prev
= view
;
220 if (debugging_virtual
) VIRTUAL_DumpView( view
);
225 /***********************************************************************
230 static void VIRTUAL_DeleteView( FILE_VIEW
*view
)
232 munmap( (void *)view
->base
, view
->size
);
233 if (view
->next
) view
->next
->prev
= view
->prev
;
234 if (view
->prev
) view
->prev
->next
= view
->next
;
235 else VIRTUAL_FirstView
= view
->next
;
236 if (view
->mapping
) K32OBJ_DecCount( &view
->mapping
->header
);
241 /***********************************************************************
242 * VIRTUAL_GetUnixProt
244 * Convert page protections to protection for mmap/mprotect.
246 static int VIRTUAL_GetUnixProt( BYTE vprot
)
249 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
251 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
252 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
;
253 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
;
259 /***********************************************************************
260 * VIRTUAL_GetWin32Prot
262 * Convert page protections to Win32 flags.
264 static void VIRTUAL_GetWin32Prot( BYTE vprot
, DWORD
*protect
, DWORD
*state
)
266 *protect
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
267 if (vprot
& VPROT_GUARD
) *protect
|= PAGE_GUARD
;
268 if (vprot
& VPROT_NOCACHE
) *protect
|= PAGE_NOCACHE
;
270 if (state
) *state
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
274 /***********************************************************************
277 * Build page protections from Win32 flags.
279 static BYTE
VIRTUAL_GetProt( DWORD protect
)
283 switch(protect
& 0xff)
289 vprot
= VPROT_READ
| VPROT_WRITE
;
292 vprot
= VPROT_READ
| VPROT_WRITE
| VPROT_WRITECOPY
;
297 case PAGE_EXECUTE_READ
:
298 vprot
= VPROT_EXEC
| VPROT_READ
;
300 case PAGE_EXECUTE_READWRITE
:
301 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
| VPROT_WRITECOPY
;
303 case PAGE_EXECUTE_WRITECOPY
:
304 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
311 if (protect
& PAGE_GUARD
) vprot
|= VPROT_GUARD
;
312 if (protect
& PAGE_NOCACHE
) vprot
|= VPROT_NOCACHE
;
317 /***********************************************************************
320 * Change the protection of a range of pages.
322 static BOOL32
VIRTUAL_SetProt( FILE_VIEW
*view
, UINT32 base
,
323 UINT32 size
, BYTE vprot
)
325 dprintf_virtual( stddeb
, "VIRTUAL_SetProt: %08x-%08x %s\n",
326 base
, base
+ size
- 1, VIRTUAL_GetProtStr( vprot
) );
328 if (mprotect( (void *)base
, size
, VIRTUAL_GetUnixProt(vprot
) ))
329 return FALSE
; /* FIXME: last error */
331 memset( view
->prot
+ ((base
- view
->base
) >> page_shift
),
332 vprot
, size
>> page_shift
);
333 if (debugging_virtual
) VIRTUAL_DumpView( view
);
338 /***********************************************************************
341 * Check that all pages in a range have the given flags.
343 static BOOL32
VIRTUAL_CheckFlags( UINT32 base
, UINT32 size
, BYTE flags
)
348 if (!size
) return TRUE
;
349 if (!(view
= VIRTUAL_FindView( base
))) return FALSE
;
350 if (view
->base
+ view
->size
< base
+ size
) return FALSE
;
351 page
= (base
- view
->base
) >> page_shift
;
352 size
= ROUND_SIZE( base
, size
) >> page_shift
;
353 while (size
--) if ((view
->prot
[page
++] & flags
) != flags
) return FALSE
;
358 /***********************************************************************
361 BOOL32
VIRTUAL_Init(void)
364 GetSystemInfo( &sysinfo
);
366 page_mask
= sysinfo
.dwPageSize
- 1;
367 granularity_mask
= sysinfo
.dwAllocationGranularity
- 1;
368 /* Make sure we have a power of 2 */
369 assert( !(sysinfo
.dwPageSize
& page_mask
) );
370 assert( !(sysinfo
.dwAllocationGranularity
& granularity_mask
) );
372 while ((1 << page_shift
) != sysinfo
.dwPageSize
) page_shift
++;
376 FILE *f
= fopen( "/proc/self/maps", "r" );
380 while (fgets( buffer
, sizeof(buffer
), f
))
382 int start
, end
, offset
;
384 BYTE vprot
= VPROT_COMMITTED
;
386 sscanf( buffer
, "%x-%x %c%c%c%c %x",
387 &start
, &end
, &r
, &w
, &x
, &p
, &offset
);
388 if (r
== 'r') vprot
|= VPROT_READ
;
389 if (w
== 'w') vprot
|= VPROT_WRITE
;
390 if (x
== 'x') vprot
|= VPROT_EXEC
;
391 if (p
== 'p') vprot
|= VPROT_WRITECOPY
;
392 VIRTUAL_CreateView( start
, end
- start
, 0,
393 VFLAG_SYSTEM
, vprot
, NULL
);
403 /***********************************************************************
404 * VirtualAlloc (KERNEL32.548)
406 LPVOID
VirtualAlloc( LPVOID addr
, DWORD size
, DWORD type
, DWORD protect
)
409 UINT32 base
, ptr
, view_size
;
412 dprintf_virtual( stddeb
, "VirtualAlloc: %08x %08lx %lx %08lx\n",
413 (UINT32
)addr
, size
, type
, protect
);
415 /* Round parameters to a page boundary */
417 if (size
> 0x7fc00000) /* 2Gb - 4Mb */
419 SetLastError( ERROR_OUTOFMEMORY
);
424 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
425 base
= ((UINT32
)addr
+ granularity_mask
) & ~granularity_mask
;
427 base
= ROUND_ADDR( addr
);
428 size
= (((UINT32
)addr
+ size
+ page_mask
) & ~page_mask
) - base
;
429 if (base
+ size
< base
) /* Disallow wrap-around */
431 SetLastError( ERROR_INVALID_PARAMETER
);
438 size
= (size
+ page_mask
) & ~page_mask
;
441 /* Compute the protection flags */
443 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
)) ||
444 (type
& ~(MEM_COMMIT
| MEM_RESERVE
)))
446 SetLastError( ERROR_INVALID_PARAMETER
);
449 if (type
& MEM_COMMIT
)
450 vprot
= VIRTUAL_GetProt( protect
) | VPROT_COMMITTED
;
453 /* Reserve the memory */
455 if ((type
& MEM_RESERVE
) || !base
)
457 view_size
= size
+ (base
? 0 : granularity_mask
+ 1);
458 ptr
= (UINT32
)FILE_mmap( NULL
, (LPVOID
)base
, 0, view_size
, 0, 0,
459 VIRTUAL_GetUnixProt( vprot
), MAP_PRIVATE
);
460 if (ptr
== (UINT32
)-1)
462 SetLastError( ERROR_OUTOFMEMORY
);
467 /* Release the extra memory while keeping the range */
468 /* starting on a 64k boundary. */
470 if (ptr
& granularity_mask
)
472 UINT32 extra
= granularity_mask
+ 1 - (ptr
& granularity_mask
);
473 munmap( (void *)ptr
, extra
);
477 if (view_size
> size
)
478 munmap( (void *)(ptr
+ size
), view_size
- size
);
480 if (!(view
= VIRTUAL_CreateView( ptr
, size
, 0, 0, vprot
, NULL
)))
482 munmap( (void *)ptr
, size
);
483 SetLastError( ERROR_OUTOFMEMORY
);
486 if (debugging_virtual
) VIRTUAL_DumpView( view
);
490 /* Commit the pages */
492 if (!(view
= VIRTUAL_FindView( base
)) ||
493 (base
+ size
> view
->base
+ view
->size
))
495 SetLastError( ERROR_INVALID_PARAMETER
);
499 if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) return NULL
;
504 /***********************************************************************
505 * VirtualFree (KERNEL32.550)
507 BOOL32
VirtualFree( LPVOID addr
, DWORD size
, DWORD type
)
512 dprintf_virtual( stddeb
, "VirtualFree: %08x %08lx %lx\n",
513 (UINT32
)addr
, size
, type
);
515 /* Fix the parameters */
517 size
= ROUND_SIZE( addr
, size
);
518 base
= ROUND_ADDR( addr
);
520 if (!(view
= VIRTUAL_FindView( base
)) ||
521 (base
+ size
> view
->base
+ view
->size
))
523 SetLastError( ERROR_INVALID_PARAMETER
);
527 /* Compute the protection flags */
529 if ((type
!= MEM_DECOMMIT
) && (type
!= MEM_RELEASE
))
531 SetLastError( ERROR_INVALID_PARAMETER
);
537 if (type
== MEM_RELEASE
)
539 if (size
|| (base
!= view
->base
))
541 SetLastError( ERROR_INVALID_PARAMETER
);
544 VIRTUAL_DeleteView( view
);
548 /* Decommit the pages */
550 return VIRTUAL_SetProt( view
, base
, size
, 0 );
554 /***********************************************************************
555 * VirtualLock (KERNEL32.551)
557 BOOL32
VirtualLock( LPVOID addr
, DWORD size
)
563 /***********************************************************************
564 * VirtualUnlock (KERNEL32.556)
566 BOOL32
VirtualUnlock( LPVOID addr
, DWORD size
)
572 /***********************************************************************
573 * VirtualProtect (KERNEL32.552)
575 BOOL32
VirtualProtect( LPVOID addr
, DWORD size
, DWORD new_prot
,
582 dprintf_virtual( stddeb
, "VirtualProtect: %08x %08lx %08lx\n",
583 (UINT32
)addr
, size
, new_prot
);
585 /* Fix the parameters */
587 size
= ROUND_SIZE( addr
, size
);
588 base
= ROUND_ADDR( addr
);
590 if (!(view
= VIRTUAL_FindView( base
)) ||
591 (base
+ size
> view
->base
+ view
->size
))
593 SetLastError( ERROR_INVALID_PARAMETER
);
597 /* Make sure all the pages are committed */
599 p
= view
->prot
+ ((base
- view
->base
) >> page_shift
);
600 for (i
= size
>> page_shift
; i
; i
--, p
++)
602 if (!(*p
& VPROT_COMMITTED
))
604 SetLastError( ERROR_INVALID_PARAMETER
);
609 VIRTUAL_GetWin32Prot( view
->prot
[0], old_prot
, NULL
);
610 vprot
= VIRTUAL_GetProt( new_prot
) | VPROT_COMMITTED
;
611 return VIRTUAL_SetProt( view
, base
, size
, vprot
);
615 /***********************************************************************
616 * VirtualProtectEx (KERNEL32.553)
618 BOOL32
VirtualProtectEx( HANDLE32 handle
, LPVOID addr
, DWORD size
,
619 DWORD new_prot
, LPDWORD old_prot
)
623 PDB32
*pdb
= (PDB32
*)PROCESS_GetObjPtr( handle
, K32OBJ_PROCESS
);
626 if (pdb
== pCurrentProcess
)
627 ret
= VirtualProtect( addr
, size
, new_prot
, old_prot
);
629 fprintf(stderr
,"Unsupported: VirtualProtectEx on other process\n");
630 K32OBJ_DecCount( &pdb
->header
);
636 /***********************************************************************
637 * VirtualQuery (KERNEL32.554)
639 BOOL32
VirtualQuery( LPCVOID addr
, LPMEMORY_BASIC_INFORMATION info
, DWORD len
)
641 FILE_VIEW
*view
= VIRTUAL_FirstView
;
642 UINT32 base
= ROUND_ADDR( addr
);
643 UINT32 alloc_base
= 0;
646 /* Find the view containing the address */
652 size
= 0xffff0000 - alloc_base
;
655 if (view
->base
> base
)
657 size
= view
->base
- alloc_base
;
661 if (view
->base
+ view
->size
> base
)
663 alloc_base
= view
->base
;
667 alloc_base
= view
->base
+ view
->size
;
671 /* Fill the info structure */
675 info
->State
= MEM_FREE
;
677 info
->AllocationProtect
= 0;
682 BYTE vprot
= view
->prot
[(base
- alloc_base
) >> page_shift
];
683 VIRTUAL_GetWin32Prot( vprot
, &info
->Protect
, &info
->State
);
684 for (size
= base
- alloc_base
; size
< view
->size
; size
+= page_mask
+1)
685 if (view
->prot
[size
>> page_shift
] != vprot
) break;
686 info
->AllocationProtect
= view
->protect
;
687 info
->Type
= MEM_PRIVATE
; /* FIXME */
690 info
->BaseAddress
= (LPVOID
)base
;
691 info
->AllocationBase
= (LPVOID
)alloc_base
;
692 info
->RegionSize
= size
- (base
- alloc_base
);
697 /***********************************************************************
698 * VirtualQueryEx (KERNEL32.555)
700 BOOL32
VirtualQueryEx( HANDLE32 handle
, LPCVOID addr
,
701 LPMEMORY_BASIC_INFORMATION info
, DWORD len
)
705 PDB32
*pdb
= (PDB32
*)PROCESS_GetObjPtr( handle
, K32OBJ_PROCESS
);
708 if (pdb
== pCurrentProcess
)
709 ret
= VirtualQuery( addr
, info
, len
);
711 fprintf(stderr
,"Unsupported: VirtualQueryEx on other process\n");
712 K32OBJ_DecCount( &pdb
->header
);
718 /***********************************************************************
719 * IsBadReadPtr32 (KERNEL32.354)
721 BOOL32
IsBadReadPtr32( LPCVOID ptr
, UINT32 size
)
723 return !VIRTUAL_CheckFlags( (UINT32
)ptr
, size
,
724 VPROT_READ
| VPROT_COMMITTED
);
728 /***********************************************************************
729 * IsBadWritePtr32 (KERNEL32.357)
731 BOOL32
IsBadWritePtr32( LPVOID ptr
, UINT32 size
)
733 return !VIRTUAL_CheckFlags( (UINT32
)ptr
, size
,
734 VPROT_WRITE
| VPROT_COMMITTED
);
738 /***********************************************************************
739 * IsBadHugeReadPtr32 (KERNEL32.352)
741 BOOL32
IsBadHugeReadPtr32( LPCVOID ptr
, UINT32 size
)
743 return IsBadReadPtr32( ptr
, size
);
747 /***********************************************************************
748 * IsBadHugeWritePtr32 (KERNEL32.353)
750 BOOL32
IsBadHugeWritePtr32( LPVOID ptr
, UINT32 size
)
752 return IsBadWritePtr32( ptr
, size
);
756 /***********************************************************************
757 * IsBadCodePtr32 (KERNEL32.351)
759 BOOL32
IsBadCodePtr32( FARPROC32 ptr
)
761 return !VIRTUAL_CheckFlags( (UINT32
)ptr
, 1, VPROT_EXEC
| VPROT_COMMITTED
);
765 /***********************************************************************
766 * IsBadStringPtr32A (KERNEL32.355)
768 BOOL32
IsBadStringPtr32A( LPCSTR str
, UINT32 max
)
773 if (!max
) return FALSE
;
774 if (!(view
= VIRTUAL_FindView( (UINT32
)str
))) return TRUE
;
775 page
= ((UINT32
)str
- view
->base
) >> page_shift
;
776 count
= page_mask
+ 1 - ((UINT32
)str
& page_mask
);
780 if ((view
->prot
[page
] & (VPROT_READ
| VPROT_COMMITTED
)) !=
781 (VPROT_READ
| VPROT_COMMITTED
))
783 if (count
> max
) count
= max
;
785 while (count
--) if (!*str
++) return FALSE
;
786 if (++page
>= view
->size
>> page_shift
) return TRUE
;
787 count
= page_mask
+ 1;
793 /***********************************************************************
794 * IsBadStringPtr32W (KERNEL32.356)
796 BOOL32
IsBadStringPtr32W( LPCWSTR str
, UINT32 max
)
801 if (!max
) return FALSE
;
802 if (!(view
= VIRTUAL_FindView( (UINT32
)str
))) return TRUE
;
803 page
= ((UINT32
)str
- view
->base
) >> page_shift
;
804 count
= (page_mask
+ 1 - ((UINT32
)str
& page_mask
)) / sizeof(WCHAR
);
808 if ((view
->prot
[page
] & (VPROT_READ
| VPROT_COMMITTED
)) !=
809 (VPROT_READ
| VPROT_COMMITTED
))
811 if (count
> max
) count
= max
;
813 while (count
--) if (!*str
++) return FALSE
;
814 if (++page
>= view
->size
>> page_shift
) return TRUE
;
815 count
= (page_mask
+ 1) / sizeof(WCHAR
);
821 /***********************************************************************
822 * CreateFileMapping32A (KERNEL32.46)
824 HANDLE32
CreateFileMapping32A( HFILE32 hFile
, LPSECURITY_ATTRIBUTES attr
,
825 DWORD protect
, DWORD size_high
, DWORD size_low
,
828 FILE_MAPPING
*mapping
= NULL
;
832 /* First search for an object with the same name */
834 K32OBJ
*obj
= K32OBJ_FindName( name
);
837 if (obj
->type
== K32OBJ_MEM_MAPPED_FILE
)
839 SetLastError( ERROR_ALREADY_EXISTS
);
840 return PROCESS_AllocHandle( obj
, 0 );
842 SetLastError( ERROR_DUP_NAME
);
846 /* Check parameters */
848 dprintf_virtual(stddeb
,"CreateFileMapping32A(%x,%p,%08lx,%08lx%08lx,%s)\n",
849 hFile
, attr
, protect
, size_high
, size_low
, name
);
851 vprot
= VIRTUAL_GetProt( protect
);
852 if (protect
& SEC_RESERVE
)
854 if (hFile
!= INVALID_HANDLE_VALUE32
)
856 SetLastError( ERROR_INVALID_PARAMETER
);
860 else vprot
|= VPROT_COMMITTED
;
861 if (protect
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
863 /* Compute the size and extend the file if necessary */
865 if (hFile
== INVALID_HANDLE_VALUE32
)
867 if (!size_high
&& !size_low
)
869 SetLastError( ERROR_INVALID_PARAMETER
);
874 else /* We have a file */
876 BY_HANDLE_FILE_INFORMATION info
;
877 if (!(obj
= PROCESS_GetObjPtr( hFile
, K32OBJ_FILE
))) goto error
;
878 /* FIXME: should check if the file permissions agree
879 * with the required protection flags */
880 if (!GetFileInformationByHandle( hFile
, &info
)) goto error
;
881 if (!size_high
&& !size_low
)
883 size_high
= info
.nFileSizeHigh
;
884 size_low
= info
.nFileSizeLow
;
886 else if ((size_high
> info
.nFileSizeHigh
) ||
887 ((size_high
== info
.nFileSizeHigh
) &&
888 (size_low
> info
.nFileSizeLow
)))
890 /* We have to grow the file */
891 if (SetFilePointer( hFile
, size_low
, &size_high
,
892 FILE_BEGIN
) == 0xffffffff) goto error
;
893 if (!SetEndOfFile( hFile
)) goto error
;
897 /* Allocate the mapping object */
899 if (!(mapping
= HeapAlloc( SystemHeap
, 0, sizeof(*mapping
) ))) goto error
;
900 mapping
->header
.type
= K32OBJ_MEM_MAPPED_FILE
;
901 mapping
->header
.refcount
= 1;
902 mapping
->protect
= vprot
;
903 mapping
->size_high
= size_high
;
904 mapping
->size_low
= ROUND_SIZE( 0, size_low
);
905 mapping
->file
= (FILE_OBJECT
*)obj
;
907 handle
= PROCESS_AllocHandle( &mapping
->header
, 0 );
908 if (handle
!= INVALID_HANDLE_VALUE32
) return handle
;
911 if (obj
) K32OBJ_DecCount( obj
);
912 if (mapping
) HeapFree( SystemHeap
, 0, mapping
);
917 /***********************************************************************
918 * CreateFileMapping32W (KERNEL32.47)
920 HANDLE32
CreateFileMapping32W( HFILE32 hFile
, LPSECURITY_ATTRIBUTES attr
,
921 DWORD protect
, DWORD size_high
, DWORD size_low
,
924 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
925 HANDLE32 ret
= CreateFileMapping32A( hFile
, attr
, protect
,
926 size_high
, size_low
, nameA
);
927 HeapFree( GetProcessHeap(), 0, nameA
);
932 /***********************************************************************
933 * OpenFileMapping32A (KERNEL32.397)
935 HANDLE32
OpenFileMapping32A( DWORD access
, BOOL32 inherit
, LPCSTR name
)
937 K32OBJ
*obj
= K32OBJ_FindNameType( name
, K32OBJ_MEM_MAPPED_FILE
);
939 return PROCESS_AllocHandle( obj
, 0 );
943 /***********************************************************************
944 * OpenFileMapping32W (KERNEL32.398)
946 HANDLE32
OpenFileMapping32W( DWORD access
, BOOL32 inherit
, LPCWSTR name
)
948 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
949 HANDLE32 ret
= OpenFileMapping32A( access
, inherit
, nameA
);
950 HeapFree( GetProcessHeap(), 0, nameA
);
955 /***********************************************************************
956 * VIRTUAL_DestroyMapping
958 * Destroy a FILE_MAPPING object.
960 void VIRTUAL_DestroyMapping( K32OBJ
*ptr
)
962 FILE_MAPPING
*mapping
= (FILE_MAPPING
*)ptr
;
963 assert( ptr
->type
== K32OBJ_MEM_MAPPED_FILE
);
965 if (mapping
->file
) K32OBJ_DecCount( &mapping
->file
->header
);
966 ptr
->type
= K32OBJ_UNKNOWN
;
967 HeapFree( SystemHeap
, 0, mapping
);
971 /***********************************************************************
972 * MapViewOfFile (KERNEL32.385)
974 LPVOID
MapViewOfFile( HANDLE32 mapping
, DWORD access
, DWORD offset_high
,
975 DWORD offset_low
, DWORD count
)
977 return MapViewOfFileEx( mapping
, access
, offset_high
,
978 offset_low
, count
, NULL
);
982 /***********************************************************************
983 * MapViewOfFileEx (KERNEL32.386)
985 LPVOID
MapViewOfFileEx( HANDLE32 handle
, DWORD access
, DWORD offset_high
,
986 DWORD offset_low
, DWORD count
, LPVOID addr
)
988 FILE_MAPPING
*mapping
;
990 UINT32 ptr
= (UINT32
)-1, size
= 0;
991 int flags
= MAP_PRIVATE
;
993 /* Check parameters */
995 if ((offset_low
& granularity_mask
) ||
996 (addr
&& ((UINT32
)addr
& granularity_mask
)))
998 SetLastError( ERROR_INVALID_PARAMETER
);
1002 if (!(mapping
= (FILE_MAPPING
*)PROCESS_GetObjPtr( handle
,
1003 K32OBJ_MEM_MAPPED_FILE
)))
1006 if (mapping
->size_high
|| offset_high
)
1007 fprintf( stderr
, "MapViewOfFileEx: offsets larger than 4Gb not supported\n");
1009 if ((offset_low
>= mapping
->size_low
) ||
1010 (count
> mapping
->size_low
- offset_low
))
1012 SetLastError( ERROR_INVALID_PARAMETER
);
1015 if (count
) size
= ROUND_SIZE( offset_low
, count
);
1016 else size
= mapping
->size_low
- offset_low
;
1020 case FILE_MAP_ALL_ACCESS
:
1021 case FILE_MAP_WRITE
:
1022 if (!(mapping
->protect
& VPROT_WRITE
))
1024 SetLastError( ERROR_INVALID_PARAMETER
);
1031 if (mapping
->protect
& VPROT_READ
) break;
1034 SetLastError( ERROR_INVALID_PARAMETER
);
1040 dprintf_virtual( stddeb
, "MapViewOfFile: handle=%x size=%x offset=%lx\n",
1041 handle
, size
, offset_low
);
1043 ptr
= (UINT32
)FILE_mmap( mapping
->file
, addr
, 0, size
, 0, offset_low
,
1044 VIRTUAL_GetUnixProt( mapping
->protect
),
1046 if (ptr
== (UINT32
)-1)
1048 SetLastError( ERROR_OUTOFMEMORY
);
1052 if (!(view
= VIRTUAL_CreateView( ptr
, size
, offset_low
, 0,
1053 mapping
->protect
, mapping
)))
1055 SetLastError( ERROR_OUTOFMEMORY
);
1061 if (ptr
!= (UINT32
)-1) munmap( (void *)ptr
, size
);
1062 K32OBJ_DecCount( &mapping
->header
);
1067 /***********************************************************************
1068 * UnmapViewOfFile (KERNEL32.540)
1070 BOOL32
UnmapViewOfFile( LPVOID addr
)
1073 UINT32 base
= ROUND_ADDR( addr
);
1074 if (!(view
= VIRTUAL_FindView( base
)) || (base
!= view
->base
))
1076 SetLastError( ERROR_INVALID_PARAMETER
);
1079 VIRTUAL_DeleteView( view
);