Release 980628
[wine/multimedia.git] / memory / virtual.c
blob414e9cae0cb5a8b14aa9ab6e93c873e44d44abe2
1 /*
2 * Win32 virtual memory functions
4 * Copyright 1997 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/mman.h>
14 #include "winbase.h"
15 #include "winerror.h"
16 #include "file.h"
17 #include "heap.h"
18 #include "process.h"
19 #include "xmalloc.h"
20 #include "global.h"
21 #include "debug.h"
23 #ifndef MS_SYNC
24 #define MS_SYNC 0
25 #endif
27 /* File mapping */
28 typedef struct
30 K32OBJ header;
31 DWORD size_high;
32 DWORD size_low;
33 FILE_OBJECT *file;
34 BYTE protect;
35 } FILE_MAPPING;
37 /* File view */
38 typedef struct _FV
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 */
51 } FILE_VIEW;
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
62 /* Per-view flags */
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;
89 #ifdef __i386__
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
94 #else
95 static UINT32 page_shift;
96 static UINT32 page_mask;
97 static UINT32 granularity_mask; /* Allocation granularity (usually 64k) */
98 #endif /* __i386__ */
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) */
111 NULL, /* signaled */
112 NULL, /* satisfied */
113 NULL, /* add_wait */
114 NULL, /* remove_wait */
115 NULL, /* read */
116 NULL, /* write */
117 VIRTUAL_DestroyMapping /* destroy */
120 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
121 if (!TRACE_ON(virtual)); else VIRTUAL_DumpView(view)
123 /***********************************************************************
124 * VIRTUAL_GetProtStr
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' : '-';
135 buffer[5] = 0;
136 return buffer;
140 /***********************************************************************
141 * VIRTUAL_DumpView
143 static void VIRTUAL_DumpView( FILE_VIEW *view )
145 UINT32 i, count;
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 );
154 else
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];
165 count = 0;
167 if (count)
168 DUMP( " %08x - %08x %s\n",
169 addr, addr + (count << page_shift) - 1,
170 VIRTUAL_GetProtStr(prot) );
174 /***********************************************************************
175 * VIRTUAL_Dump
177 void VIRTUAL_Dump(void)
179 FILE_VIEW *view = VIRTUAL_FirstView;
180 DUMP( "\nDump of all virtual memory views:\n\n" );
181 while (view)
183 VIRTUAL_DumpView( view );
184 view = view->next;
189 /***********************************************************************
190 * VIRTUAL_FindView
192 * Find the view containing a given address.
194 * RETURNS
195 * View: Success
196 * NULL: Failure
198 static FILE_VIEW *VIRTUAL_FindView(
199 UINT32 addr /* [in] Address */
201 FILE_VIEW *view = VIRTUAL_FirstView;
202 while (view)
204 if (view->base > addr) return NULL;
205 if (view->base + view->size > addr) return view;
206 view = view->next;
208 return NULL;
212 /***********************************************************************
213 * VIRTUAL_CreateView
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) );
227 size >>= page_shift;
228 if (!(view = (FILE_VIEW *)malloc( sizeof(*view) + size - 1 ))) return NULL;
229 view->base = base;
230 view->size = size << page_shift;
231 view->flags = flags;
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;
242 view->prev = NULL;
243 if (view->next) view->next->prev = view;
244 VIRTUAL_FirstView = view;
246 else
248 prev = VIRTUAL_FirstView;
249 while (prev->next && (prev->next->base < base)) prev = prev->next;
250 view->next = prev->next;
251 view->prev = prev;
252 if (view->next) view->next->prev = view;
253 prev->next = view;
255 VIRTUAL_DEBUG_DUMP_VIEW( view );
256 return view;
260 /***********************************************************************
261 * VIRTUAL_DeleteView
262 * Deletes a view.
264 * RETURNS
265 * None
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 );
275 free( view );
279 /***********************************************************************
280 * VIRTUAL_GetUnixProt
282 * Convert page protections to protection for mmap/mprotect.
284 static int VIRTUAL_GetUnixProt( BYTE vprot )
286 int prot = 0;
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;
293 return prot;
297 /***********************************************************************
298 * VIRTUAL_GetWin32Prot
300 * Convert page protections to Win32 flags.
302 * RETURNS
303 * None
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 */
310 if (protect) {
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 /***********************************************************************
321 * VIRTUAL_GetProt
323 * Build page protections from Win32 flags.
325 * RETURNS
326 * Value of page protection flags
328 static BYTE VIRTUAL_GetProt(
329 DWORD protect /* [in] Win32 protection flags */
331 BYTE vprot;
333 switch(protect & 0xff)
335 case PAGE_READONLY:
336 vprot = VPROT_READ;
337 break;
338 case PAGE_READWRITE:
339 vprot = VPROT_READ | VPROT_WRITE;
340 break;
341 case PAGE_WRITECOPY:
342 vprot = VPROT_READ | VPROT_WRITE | VPROT_WRITECOPY;
343 break;
344 case PAGE_EXECUTE:
345 vprot = VPROT_EXEC;
346 break;
347 case PAGE_EXECUTE_READ:
348 vprot = VPROT_EXEC | VPROT_READ;
349 break;
350 case PAGE_EXECUTE_READWRITE:
351 vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITE | VPROT_WRITECOPY;
352 break;
353 case PAGE_EXECUTE_WRITECOPY:
354 vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITE;
355 break;
356 case PAGE_NOACCESS:
357 default:
358 vprot = 0;
359 break;
361 if (protect & PAGE_GUARD) vprot |= VPROT_GUARD;
362 if (protect & PAGE_NOCACHE) vprot |= VPROT_NOCACHE;
363 return vprot;
367 /***********************************************************************
368 * VIRTUAL_SetProt
370 * Change the protection of a range of pages.
372 * RETURNS
373 * TRUE: Success
374 * FALSE: Failure
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 );
391 return TRUE;
395 /***********************************************************************
396 * VIRTUAL_CheckFlags
398 * Check that all pages in a range have the given flags.
400 * RETURNS
401 * TRUE: They do
402 * FALSE: They do not
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 */
409 FILE_VIEW *view;
410 UINT32 page;
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;
418 return TRUE;
422 /***********************************************************************
423 * VIRTUAL_Init
425 BOOL32 VIRTUAL_Init(void)
427 #ifndef __i386__
428 DWORD page_size;
430 # ifdef HAVE_GETPAGESIZE
431 page_size = getpagesize();
432 # else
433 # ifdef __svr4__
434 page_size = sysconf(_SC_PAGESIZE);
435 # else
436 # error Cannot get the page size on this platform
437 # endif
438 # endif
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) );
443 page_shift = 0;
444 while ((1 << page_shift) != page_size) page_shift++;
445 #endif /* !__i386__ */
447 #ifdef linux
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);
453 if (fd >= 0)
455 char buffer[512]; /* line might be rather long in 2.1 */
457 for (;;)
459 int start, end, offset;
460 char r, w, x, p;
461 BYTE vprot = VPROT_COMMITTED;
463 char * ptr = buffer;
464 int count = sizeof(buffer);
465 while (1 == read(fd, ptr, 1) && *ptr != '\n' && --count > 0)
466 ptr++;
468 if (*ptr != '\n') break;
469 *ptr = '\0';
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 );
480 close (fd);
483 #endif /* linux */
484 return TRUE;
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 )
511 FILE_VIEW *view;
513 if (!(view = VIRTUAL_FindView((UINT32)addr))) return FALSE;
514 view->handlerProc = proc;
515 view->handlerArg = arg;
516 return TRUE;
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);
528 return FALSE;
532 /***********************************************************************
533 * VirtualAlloc (KERNEL32.548)
534 * Reserves or commits a region of pages in virtual address space
536 * RETURNS
537 * Base address of allocated region of pages
538 * NULL: Failure
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 */
546 FILE_VIEW *view;
547 UINT32 base, ptr, view_size;
548 BYTE vprot;
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 );
558 return NULL;
560 if (addr)
562 if (type & MEM_RESERVE) /* Round down to 64k boundary */
563 base = ((UINT32)addr + granularity_mask) & ~granularity_mask;
564 else
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 );
570 return NULL;
573 else
575 base = 0;
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 );
592 return NULL;
594 if (type & MEM_COMMIT)
595 vprot = VIRTUAL_GetProt( protect ) | VPROT_COMMITTED;
596 else vprot = 0;
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 );
608 return NULL;
610 if (!base)
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 );
619 ptr += extra;
620 view_size -= 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 );
630 return NULL;
632 if (!(view = VIRTUAL_CreateView( ptr, size, 0, 0, vprot, NULL )))
634 FILE_munmap( (void *)ptr, 0, size );
635 SetLastError( ERROR_OUTOFMEMORY );
636 return NULL;
638 VIRTUAL_DEBUG_DUMP_VIEW( view );
639 return (LPVOID)ptr;
642 /* Commit the pages */
644 if (!(view = VIRTUAL_FindView( base )) ||
645 (base + size > view->base + view->size))
647 SetLastError( ERROR_INVALID_PARAMETER );
648 return NULL;
651 if (!VIRTUAL_SetProt( view, base, size, vprot )) return NULL;
652 return (LPVOID)base;
656 /***********************************************************************
657 * VirtualFree (KERNEL32.550)
658 * Release or decommits a region of pages in virtual address space.
660 * RETURNS
661 * TRUE: Success
662 * FALSE: Failure
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 */
669 FILE_VIEW *view;
670 UINT32 base;
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 );
684 return FALSE;
687 /* Compute the protection flags */
689 if ((type != MEM_DECOMMIT) && (type != MEM_RELEASE))
691 SetLastError( ERROR_INVALID_PARAMETER );
692 return FALSE;
695 /* Free the pages */
697 if (type == MEM_RELEASE)
699 if (size || (base != view->base))
701 SetLastError( ERROR_INVALID_PARAMETER );
702 return FALSE;
704 VIRTUAL_DeleteView( view );
705 return TRUE;
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
717 * NOTE
718 * Always returns TRUE
720 * RETURNS
721 * TRUE: Success
722 * FALSE: Failure
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 */
728 return TRUE;
732 /***********************************************************************
733 * VirtualUnlock (KERNEL32.556)
734 * Unlocks a range of pages in the virtual address space
736 * NOTE
737 * Always returns TRUE
739 * RETURNS
740 * TRUE: Success
741 * FALSE: Failure
743 BOOL32 WINAPI VirtualUnlock(
744 LPVOID addr, /* [in] Address of first byte of range */
745 DWORD size /* [in] Number of bytes in range */
747 return TRUE;
751 /***********************************************************************
752 * VirtualProtect (KERNEL32.552)
753 * Changes the access protection on a region of committed pages
755 * RETURNS
756 * TRUE: Success
757 * FALSE: Failure
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 */
765 FILE_VIEW *view;
766 UINT32 base, i;
767 BYTE vprot, *p;
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 );
781 return FALSE;
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 );
792 return FALSE;
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
807 * RETURNS
808 * TRUE: Success
809 * FALSE: Failure
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 */
818 BOOL32 ret = FALSE;
820 PDB32 *pdb = PROCESS_GetPtr( handle, PROCESS_VM_OPERATION );
821 if (pdb)
823 if (pdb == PROCESS_Current())
824 ret = VirtualProtect( addr, size, new_prot, old_prot );
825 else
826 ERR(virtual,"Unsupported on other process\n");
827 K32OBJ_DecCount( &pdb->header );
829 return ret;
833 /***********************************************************************
834 * VirtualQuery (KERNEL32.554)
835 * Provides info about a range of pages in virtual address space
837 * RETURNS
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;
848 UINT32 size = 0;
850 /* Find the view containing the address */
852 for (;;)
854 if (!view)
856 size = 0xffff0000 - alloc_base;
857 break;
859 if (view->base > base)
861 size = view->base - alloc_base;
862 view = NULL;
863 break;
865 if (view->base + view->size > base)
867 alloc_base = view->base;
868 size = view->size;
869 break;
871 alloc_base = view->base + view->size;
872 view = view->next;
875 /* Fill the info structure */
877 if (!view)
879 info->State = MEM_FREE;
880 info->Protect = 0;
881 info->AllocationProtect = 0;
882 info->Type = 0;
884 else
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
904 * specified process
906 * RETURNS
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 */
915 DWORD ret = len;
917 PDB32 *pdb = PROCESS_GetPtr( handle, PROCESS_QUERY_INFORMATION );
918 if (pdb)
920 if (pdb == PROCESS_Current())
921 ret = VirtualQuery( addr, info, len );
922 else
923 ERR(virtual,"Unsupported on other process\n");
924 K32OBJ_DecCount( &pdb->header );
926 return ret;
930 /***********************************************************************
931 * IsBadReadPtr32 (KERNEL32.354)
933 * RETURNS
934 * FALSE: Process has read access to entire block
935 * TRUE: Otherwise
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)
949 * RETURNS
950 * FALSE: Process has write access to entire block
951 * TRUE: Otherwise
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)
964 * RETURNS
965 * FALSE: Process has read access to entire block
966 * TRUE: Otherwise
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)
978 * RETURNS
979 * FALSE: Process has write access to entire block
980 * TRUE: Otherwise
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)
993 * RETURNS
994 * FALSE: Process has read access to specified memory
995 * TRUE: Otherwise
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)
1007 * RETURNS
1008 * FALSE: Read access to all bytes in string
1009 * TRUE: Else
1011 BOOL32 WINAPI IsBadStringPtr32A(
1012 LPCSTR str, /* [in] Address of string */
1013 UINT32 max /* [in] Maximum size of string */
1015 FILE_VIEW *view;
1016 UINT32 page, count;
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);
1023 while (max)
1025 if ((view->prot[page] & (VPROT_READ | VPROT_COMMITTED)) !=
1026 (VPROT_READ | VPROT_COMMITTED))
1027 return TRUE;
1028 if (count > max) count = max;
1029 max -= count;
1030 while (count--) if (!*str++) return FALSE;
1031 if (++page >= view->size >> page_shift) return TRUE;
1032 count = page_mask + 1;
1034 return FALSE;
1038 /***********************************************************************
1039 * IsBadStringPtr32W (KERNEL32.356)
1040 * See IsBadStringPtr32A
1042 BOOL32 WINAPI IsBadStringPtr32W( LPCWSTR str, UINT32 max )
1044 FILE_VIEW *view;
1045 UINT32 page, count;
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);
1052 while (max)
1054 if ((view->prot[page] & (VPROT_READ | VPROT_COMMITTED)) !=
1055 (VPROT_READ | VPROT_COMMITTED))
1056 return TRUE;
1057 if (count > max) count = max;
1058 max -= count;
1059 while (count--) if (!*str++) return FALSE;
1060 if (++page >= view->size >> page_shift) return TRUE;
1061 count = (page_mask + 1) / sizeof(WCHAR);
1063 return FALSE;
1067 /***********************************************************************
1068 * CreateFileMapping32A (KERNEL32.46)
1069 * Creates a named or unnamed file-mapping object for the specified file
1071 * RETURNS
1072 * Handle: Success
1073 * 0: Mapping object does not exist
1074 * NULL: Failure
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;
1085 HANDLE32 handle;
1086 BYTE vprot;
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 );
1092 if (obj)
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 );
1100 else
1102 SetLastError( ERROR_DUP_NAME );
1103 handle = 0;
1105 K32OBJ_DecCount( obj );
1106 return handle;
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 );
1120 return 0;
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 );
1133 return 0;
1135 obj = NULL;
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 )))
1149 goto error;
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 );
1182 return handle;
1184 error:
1185 if (obj) K32OBJ_DecCount( obj );
1186 if (mapping) HeapFree( SystemHeap, 0, mapping );
1187 return 0;
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 );
1203 return ret;
1207 /***********************************************************************
1208 * OpenFileMapping32A (KERNEL32.397)
1209 * Opens a named file-mapping object.
1211 * RETURNS
1212 * Handle: Success
1213 * NULL: Failure
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;
1221 K32OBJ *obj;
1222 SYSTEM_LOCK();
1223 if ((obj = K32OBJ_FindNameType( name, K32OBJ_MEM_MAPPED_FILE )))
1225 handle = HANDLE_Alloc( PROCESS_Current(), obj, access, inherit );
1226 K32OBJ_DecCount( obj );
1228 SYSTEM_UNLOCK();
1229 return handle;
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 );
1242 return ret;
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
1266 * RETURNS
1267 * Starting address of mapped view
1268 * NULL: Failure
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
1286 * RETURNS
1287 * Starting address of mapped view
1288 * NULL: Failure
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;
1299 FILE_VIEW *view;
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 );
1309 return NULL;
1312 if (!(mapping = (FILE_MAPPING *)HANDLE_GetObjPtr( PROCESS_Current(),
1313 handle,
1314 K32OBJ_MEM_MAPPED_FILE,
1315 0 /* FIXME */ )))
1316 return NULL;
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 );
1325 goto error;
1327 if (count) size = ROUND_SIZE( offset_low, count );
1328 else size = mapping->size_low - offset_low;
1330 switch(access)
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 );
1338 goto error;
1340 flags = MAP_SHARED;
1341 /* fall through */
1342 case FILE_MAP_READ:
1343 case FILE_MAP_COPY:
1344 case FILE_MAP_COPY | FILE_MAP_READ:
1345 if (mapping->protect & VPROT_READ) break;
1346 /* fall through */
1347 default:
1348 SetLastError( ERROR_INVALID_PARAMETER );
1349 goto error;
1352 /* Map the file */
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 ),
1359 flags );
1360 if (ptr == (UINT32)-1)
1362 SetLastError( ERROR_OUTOFMEMORY );
1363 goto error;
1366 if (!(view = VIRTUAL_CreateView( ptr, size, offset_low, 0,
1367 mapping->protect, mapping )))
1369 SetLastError( ERROR_OUTOFMEMORY );
1370 goto error;
1372 return (LPVOID)ptr;
1374 error:
1375 if (ptr != (UINT32)-1) FILE_munmap( (void *)ptr, 0, size );
1376 K32OBJ_DecCount( &mapping->header );
1377 return NULL;
1381 /***********************************************************************
1382 * FlushViewOfFile (KERNEL32.262)
1383 * Writes to the disk a byte range within a mapped view of a file
1385 * RETURNS
1386 * TRUE: Success
1387 * FALSE: Failure
1389 BOOL32 WINAPI FlushViewOfFile(
1390 LPCVOID base, /* [in] Start address of byte range to flush */
1391 DWORD cbFlush /* [in] Number of bytes in range */
1393 FILE_VIEW *view;
1394 UINT32 addr = ROUND_ADDR( base );
1396 TRACE(virtual, "FlushViewOfFile at %p for %ld bytes\n",
1397 base, cbFlush );
1399 if (!(view = VIRTUAL_FindView( addr )))
1401 SetLastError( ERROR_INVALID_PARAMETER );
1402 return FALSE;
1404 if (!cbFlush) cbFlush = view->size;
1405 if (!msync( (void *)addr, cbFlush, MS_SYNC )) return TRUE;
1406 SetLastError( ERROR_INVALID_PARAMETER );
1407 return FALSE;
1411 /***********************************************************************
1412 * UnmapViewOfFile (KERNEL32.540)
1413 * Unmaps a mapped view of a file.
1415 * NOTES
1416 * Should addr be an LPCVOID?
1418 * RETURNS
1419 * TRUE: Success
1420 * FALSE: Failure
1422 BOOL32 WINAPI UnmapViewOfFile(
1423 LPVOID addr /* [in] Address where mapped view begins */
1425 FILE_VIEW *view;
1426 UINT32 base = ROUND_ADDR( addr );
1427 if (!(view = VIRTUAL_FindView( base )) || (base != view->base))
1429 SetLastError( ERROR_INVALID_PARAMETER );
1430 return FALSE;
1432 VIRTUAL_DeleteView( view );
1433 return TRUE;