Authors: Gavriel State <gavriels@corel.com>, Ulrich Czekalla <ulrichc@corel.com>
[wine] / memory / virtual.c
1 /*
2  * Win32 virtual memory functions
3  *
4  * Copyright 1997 Alexandre Julliard
5  */
6
7 #include "config.h"
8
9 #include <assert.h>
10 #include <errno.h>
11 #ifdef HAVE_SYS_ERRNO_H
12 #include <sys/errno.h>
13 #endif
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <sys/types.h>
20 #ifdef HAVE_SYS_MMAN_H
21 #include <sys/mman.h>
22 #endif
23 #include "winbase.h"
24 #include "winerror.h"
25 #include "file.h"
26 #include "process.h"
27 #include "global.h"
28 #include "server.h"
29 #include "debugtools.h"
30
31 DEFAULT_DEBUG_CHANNEL(virtual);
32
33 #ifndef MS_SYNC
34 #define MS_SYNC 0
35 #endif
36
37 /* File view */
38 typedef struct _FV
39 {
40     struct _FV   *next;        /* Next view */
41     struct _FV   *prev;        /* Prev view */
42     UINT        base;        /* Base address */
43     UINT        size;        /* Size in bytes */
44     UINT        flags;       /* Allocation flags */
45     UINT        offset;      /* Offset from start of mapped file */
46     HANDLE      mapping;     /* Handle to the 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;
52
53 /* Per-view flags */
54 #define VFLAG_SYSTEM     0x01
55
56 /* Conversion from VPROT_* to Win32 flags */
57 static const BYTE VIRTUAL_Win32Flags[16] =
58 {
59     PAGE_NOACCESS,              /* 0 */
60     PAGE_READONLY,              /* READ */
61     PAGE_READWRITE,             /* WRITE */
62     PAGE_READWRITE,             /* READ | WRITE */
63     PAGE_EXECUTE,               /* EXEC */
64     PAGE_EXECUTE_READ,          /* READ | EXEC */
65     PAGE_EXECUTE_READWRITE,     /* WRITE | EXEC */
66     PAGE_EXECUTE_READWRITE,     /* READ | WRITE | EXEC */
67     PAGE_WRITECOPY,             /* WRITECOPY */
68     PAGE_WRITECOPY,             /* READ | WRITECOPY */
69     PAGE_WRITECOPY,             /* WRITE | WRITECOPY */
70     PAGE_WRITECOPY,             /* READ | WRITE | WRITECOPY */
71     PAGE_EXECUTE_WRITECOPY,     /* EXEC | WRITECOPY */
72     PAGE_EXECUTE_WRITECOPY,     /* READ | EXEC | WRITECOPY */
73     PAGE_EXECUTE_WRITECOPY,     /* WRITE | EXEC | WRITECOPY */
74     PAGE_EXECUTE_WRITECOPY      /* READ | WRITE | EXEC | WRITECOPY */
75 };
76
77
78 static FILE_VIEW *VIRTUAL_FirstView;
79
80 #ifdef __i386__
81 /* These are always the same on an i386, and it will be faster this way */
82 # define page_mask  0xfff
83 # define page_shift 12
84 # define granularity_mask 0xffff
85 #else
86 static UINT page_shift;
87 static UINT page_mask;
88 static UINT granularity_mask;  /* Allocation granularity (usually 64k) */
89 #endif  /* __i386__ */
90
91 #define ROUND_ADDR(addr) \
92    ((UINT)(addr) & ~page_mask)
93
94 #define ROUND_SIZE(addr,size) \
95    (((UINT)(size) + ((UINT)(addr) & page_mask) + page_mask) & ~page_mask)
96
97 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
98    if (!TRACE_ON(virtual)); else VIRTUAL_DumpView(view)
99
100 /***********************************************************************
101  *           VIRTUAL_GetProtStr
102  */
103 static const char *VIRTUAL_GetProtStr( BYTE prot )
104 {
105     static char buffer[6];
106     buffer[0] = (prot & VPROT_COMMITTED) ? 'c' : '-';
107     buffer[1] = (prot & VPROT_GUARD) ? 'g' : '-';
108     buffer[2] = (prot & VPROT_READ) ? 'r' : '-';
109     buffer[3] = (prot & VPROT_WRITE) ?
110                     ((prot & VPROT_WRITECOPY) ? 'w' : 'W') : '-';
111     buffer[4] = (prot & VPROT_EXEC) ? 'x' : '-';
112     buffer[5] = 0;
113     return buffer;
114 }
115
116
117 /***********************************************************************
118  *           VIRTUAL_DumpView
119  */
120 static void VIRTUAL_DumpView( FILE_VIEW *view )
121 {
122     UINT i, count;
123     UINT addr = view->base;
124     BYTE prot = view->prot[0];
125
126     DPRINTF( "View: %08x - %08x%s",
127           view->base, view->base + view->size - 1,
128           (view->flags & VFLAG_SYSTEM) ? " (system)" : "" );
129     if (view->mapping)
130         DPRINTF( " %d @ %08x\n", view->mapping, view->offset );
131     else
132         DPRINTF( " (anonymous)\n");
133
134     for (count = i = 1; i < view->size >> page_shift; i++, count++)
135     {
136         if (view->prot[i] == prot) continue;
137         DPRINTF( "      %08x - %08x %s\n",
138               addr, addr + (count << page_shift) - 1,
139               VIRTUAL_GetProtStr(prot) );
140         addr += (count << page_shift);
141         prot = view->prot[i];
142         count = 0;
143     }
144     if (count)
145         DPRINTF( "      %08x - %08x %s\n",
146               addr, addr + (count << page_shift) - 1,
147               VIRTUAL_GetProtStr(prot) );
148 }
149
150
151 /***********************************************************************
152  *           VIRTUAL_Dump
153  */
154 void VIRTUAL_Dump(void)
155 {
156     FILE_VIEW *view = VIRTUAL_FirstView;
157     DPRINTF( "\nDump of all virtual memory views:\n\n" );
158     while (view)
159     {
160         VIRTUAL_DumpView( view );
161         view = view->next;
162     }
163 }
164
165
166 /***********************************************************************
167  *           VIRTUAL_FindView
168  *
169  * Find the view containing a given address.
170  *
171  * RETURNS
172  *      View: Success
173  *      NULL: Failure
174  */
175 static FILE_VIEW *VIRTUAL_FindView(
176                   UINT addr /* [in] Address */
177 ) {
178     FILE_VIEW *view = VIRTUAL_FirstView;
179     while (view)
180     {
181         if (view->base > addr) return NULL;
182         if (view->base + view->size > addr) return view;
183         view = view->next;
184     }
185     return NULL;
186 }
187
188
189 /***********************************************************************
190  *           VIRTUAL_CreateView
191  *
192  * Create a new view and add it in the linked list.
193  */
194 static FILE_VIEW *VIRTUAL_CreateView( UINT base, UINT size, UINT offset,
195                                       UINT flags, BYTE vprot,
196                                       HANDLE mapping )
197 {
198     FILE_VIEW *view, *prev;
199
200     /* Create the view structure */
201
202     assert( !(base & page_mask) );
203     assert( !(size & page_mask) );
204     size >>= page_shift;
205     if (!(view = (FILE_VIEW *)malloc( sizeof(*view) + size - 1 ))) return NULL;
206     view->base    = base;
207     view->size    = size << page_shift;
208     view->flags   = flags;
209     view->offset  = offset;
210     view->mapping = mapping;
211     view->protect = vprot;
212     view->handlerProc = NULL;
213     memset( view->prot, vprot, size );
214
215     /* Duplicate the mapping handle */
216
217     if ((view->mapping != -1) &&
218         !DuplicateHandle( GetCurrentProcess(), view->mapping,
219                           GetCurrentProcess(), &view->mapping,
220                           0, FALSE, DUPLICATE_SAME_ACCESS ))
221     {
222         free( view );
223         return NULL;
224     }
225
226     /* Insert it in the linked list */
227
228     if (!VIRTUAL_FirstView || (VIRTUAL_FirstView->base > base))
229     {
230         view->next = VIRTUAL_FirstView;
231         view->prev = NULL;
232         if (view->next) view->next->prev = view;
233         VIRTUAL_FirstView = view;
234     }
235     else
236     {
237         prev = VIRTUAL_FirstView;
238         while (prev->next && (prev->next->base < base)) prev = prev->next;
239         view->next = prev->next;
240         view->prev = prev;
241         if (view->next) view->next->prev = view;
242         prev->next  = view;
243     }
244     VIRTUAL_DEBUG_DUMP_VIEW( view );
245     return view;
246 }
247
248
249 /***********************************************************************
250  *           VIRTUAL_DeleteView
251  * Deletes a view.
252  *
253  * RETURNS
254  *      None
255  */
256 static void VIRTUAL_DeleteView(
257             FILE_VIEW *view /* [in] View */
258 ) {
259     FILE_munmap( (void *)view->base, 0, view->size );
260     if (view->next) view->next->prev = view->prev;
261     if (view->prev) view->prev->next = view->next;
262     else VIRTUAL_FirstView = view->next;
263     if (view->mapping) CloseHandle( view->mapping );
264     free( view );
265 }
266
267
268 /***********************************************************************
269  *           VIRTUAL_GetUnixProt
270  *
271  * Convert page protections to protection for mmap/mprotect.
272  */
273 static int VIRTUAL_GetUnixProt( BYTE vprot )
274 {
275     int prot = 0;
276     if ((vprot & VPROT_COMMITTED) && !(vprot & VPROT_GUARD))
277     {
278         if (vprot & VPROT_READ) prot |= PROT_READ;
279         if (vprot & VPROT_WRITE) prot |= PROT_WRITE;
280         if (vprot & VPROT_WRITECOPY) prot |= PROT_WRITE;
281         if (vprot & VPROT_EXEC) prot |= PROT_EXEC;
282     }
283     return prot;
284 }
285
286
287 /***********************************************************************
288  *           VIRTUAL_GetWin32Prot
289  *
290  * Convert page protections to Win32 flags.
291  *
292  * RETURNS
293  *      None
294  */
295 static void VIRTUAL_GetWin32Prot(
296             BYTE vprot,     /* [in] Page protection flags */
297             DWORD *protect, /* [out] Location to store Win32 protection flags */
298             DWORD *state    /* [out] Location to store mem state flag */
299 ) {
300     if (protect) {
301         *protect = VIRTUAL_Win32Flags[vprot & 0x0f];
302 /*      if (vprot & VPROT_GUARD) *protect |= PAGE_GUARD;*/
303         if (vprot & VPROT_NOCACHE) *protect |= PAGE_NOCACHE;
304
305         if (vprot & VPROT_GUARD) *protect = PAGE_NOACCESS;
306     }
307
308     if (state) *state = (vprot & VPROT_COMMITTED) ? MEM_COMMIT : MEM_RESERVE;
309 }
310
311
312 /***********************************************************************
313  *           VIRTUAL_GetProt
314  *
315  * Build page protections from Win32 flags.
316  *
317  * RETURNS
318  *      Value of page protection flags
319  */
320 static BYTE VIRTUAL_GetProt(
321             DWORD protect  /* [in] Win32 protection flags */
322 ) {
323     BYTE vprot;
324
325     switch(protect & 0xff)
326     {
327     case PAGE_READONLY:
328         vprot = VPROT_READ;
329         break;
330     case PAGE_READWRITE:
331         vprot = VPROT_READ | VPROT_WRITE;
332         break;
333     case PAGE_WRITECOPY:
334         vprot = VPROT_READ | VPROT_WRITE | VPROT_WRITECOPY;
335         break;
336     case PAGE_EXECUTE:
337         vprot = VPROT_EXEC;
338         break;
339     case PAGE_EXECUTE_READ:
340         vprot = VPROT_EXEC | VPROT_READ;
341         break;
342     case PAGE_EXECUTE_READWRITE:
343         vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITE;
344         break;
345     case PAGE_EXECUTE_WRITECOPY:
346         vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITE | VPROT_WRITECOPY;
347         break;
348     case PAGE_NOACCESS:
349     default:
350         vprot = 0;
351         break;
352     }
353     if (protect & PAGE_GUARD) vprot |= VPROT_GUARD;
354     if (protect & PAGE_NOCACHE) vprot |= VPROT_NOCACHE;
355     return vprot;
356 }
357
358
359 /***********************************************************************
360  *           VIRTUAL_SetProt
361  *
362  * Change the protection of a range of pages.
363  *
364  * RETURNS
365  *      TRUE: Success
366  *      FALSE: Failure
367  */
368 static BOOL VIRTUAL_SetProt(
369               FILE_VIEW *view, /* [in] Pointer to view */
370               UINT base,     /* [in] Starting address */
371               UINT size,     /* [in] Size in bytes */
372               BYTE vprot       /* [in] Protections to use */
373 ) {
374     TRACE("%08x-%08x %s\n",
375                      base, base + size - 1, VIRTUAL_GetProtStr( vprot ) );
376
377     if (mprotect( (void *)base, size, VIRTUAL_GetUnixProt(vprot) ))
378         return FALSE;  /* FIXME: last error */
379
380     memset( view->prot + ((base - view->base) >> page_shift),
381             vprot, size >> page_shift );
382     VIRTUAL_DEBUG_DUMP_VIEW( view );
383     return TRUE;
384 }
385
386
387 /***********************************************************************
388  *             VIRTUAL_CheckFlags
389  *
390  * Check that all pages in a range have the given flags.
391  *
392  * RETURNS
393  *      TRUE: They do
394  *      FALSE: They do not
395  */
396 static BOOL VIRTUAL_CheckFlags(
397               UINT base, /* [in] Starting address */
398               UINT size, /* [in] Size in bytes */
399               BYTE flags   /* [in] Flags to check for */
400 ) {
401     FILE_VIEW *view;
402     UINT page;
403
404     if (!size) return TRUE;
405     if (!(view = VIRTUAL_FindView( base ))) return FALSE;
406     if (view->base + view->size < base + size) return FALSE;
407     page = (base - view->base) >> page_shift;
408     size = ROUND_SIZE( base, size ) >> page_shift;
409     while (size--) if ((view->prot[page++] & flags) != flags) return FALSE;
410     return TRUE;
411 }
412
413
414 /***********************************************************************
415  *           VIRTUAL_Init
416  */
417 BOOL VIRTUAL_Init(void)
418 {
419 #ifndef __i386__
420     DWORD page_size;
421
422 # ifdef HAVE_GETPAGESIZE
423     page_size = getpagesize();
424 # else
425 #  ifdef __svr4__
426     page_size = sysconf(_SC_PAGESIZE);
427 #  else
428 #   error Cannot get the page size on this platform
429 #  endif
430 # endif
431     page_mask = page_size - 1;
432     granularity_mask = 0xffff;  /* hard-coded for now */
433     /* Make sure we have a power of 2 */
434     assert( !(page_size & page_mask) );
435     page_shift = 0;
436     while ((1 << page_shift) != page_size) page_shift++;
437 #endif  /* !__i386__ */
438
439 #ifdef linux
440     {
441         /* Do not use stdio here since it may temporarily change the size
442          * of some segments (ie libc6 adds 0x1000 per open FILE)
443          */
444         int fd = open ("/proc/self/maps", O_RDONLY);
445         if (fd >= 0)
446         {
447             char buffer[512]; /* line might be rather long in 2.1 */
448
449             for (;;)
450             {
451                 int start, end, offset;
452                 char r, w, x, p;
453                 BYTE vprot = VPROT_COMMITTED;
454
455                 char * ptr = buffer;
456                 int count = sizeof(buffer);
457                 while (1 == read(fd, ptr, 1) && *ptr != '\n' && --count > 0)
458                     ptr++;
459
460                 if (*ptr != '\n') break;
461                 *ptr = '\0';
462
463                 sscanf( buffer, "%x-%x %c%c%c%c %x",
464                         &start, &end, &r, &w, &x, &p, &offset );
465                 if (r == 'r') vprot |= VPROT_READ;
466                 if (w == 'w') vprot |= VPROT_WRITE;
467                 if (x == 'x') vprot |= VPROT_EXEC;
468                 if (p == 'p') vprot |= VPROT_WRITECOPY;
469                 VIRTUAL_CreateView( start, end - start, 0,
470                                     VFLAG_SYSTEM, vprot, -1 );
471             }
472             close (fd);
473         }
474     }
475 #endif  /* linux */
476     return TRUE;
477 }
478
479
480 /***********************************************************************
481  *           VIRTUAL_GetPageSize
482  */
483 DWORD VIRTUAL_GetPageSize(void)
484 {
485     return 1 << page_shift;
486 }
487
488
489 /***********************************************************************
490  *           VIRTUAL_GetGranularity
491  */
492 DWORD VIRTUAL_GetGranularity(void)
493 {
494     return granularity_mask + 1;
495 }
496
497
498 /***********************************************************************
499  *           VIRTUAL_SetFaultHandler
500  */
501 BOOL VIRTUAL_SetFaultHandler( LPCVOID addr, HANDLERPROC proc, LPVOID arg )
502 {
503     FILE_VIEW *view;
504
505     if (!(view = VIRTUAL_FindView((UINT)addr))) return FALSE;
506     view->handlerProc = proc;
507     view->handlerArg  = arg;
508     return TRUE;
509 }
510
511 /***********************************************************************
512  *           VIRTUAL_HandleFault
513  */
514 DWORD VIRTUAL_HandleFault( LPCVOID addr )
515 {
516     FILE_VIEW *view = VIRTUAL_FindView((UINT)addr);
517     DWORD ret = EXCEPTION_ACCESS_VIOLATION;
518
519     if (view)
520     {
521         if (view->handlerProc)
522         {
523             if (view->handlerProc(view->handlerArg, addr)) ret = 0;  /* handled */
524         }
525         else
526         {
527             BYTE vprot = view->prot[((UINT)addr - view->base) >> page_shift];
528             UINT page = (UINT)addr & ~page_mask;
529             char *stack = (char *)NtCurrentTeb()->stack_base + SIGNAL_STACK_SIZE + page_mask + 1;
530             if (vprot & VPROT_GUARD)
531             {
532                 VIRTUAL_SetProt( view, page, page_mask + 1, vprot & ~VPROT_GUARD );
533                 ret = STATUS_GUARD_PAGE_VIOLATION;
534             }
535             /* is it inside the stack guard pages? */
536             if (((char *)addr >= stack) && ((char *)addr < stack + 2*(page_mask+1)))
537                 ret = STATUS_STACK_OVERFLOW;
538         }
539     }
540     return ret;
541 }
542
543
544 /***********************************************************************
545  *             VirtualAlloc   (KERNEL32.548)
546  * Reserves or commits a region of pages in virtual address space
547  *
548  * RETURNS
549  *      Base address of allocated region of pages
550  *      NULL: Failure
551  */
552 LPVOID WINAPI VirtualAlloc(
553               LPVOID addr,  /* [in] Address of region to reserve or commit */
554               DWORD size,   /* [in] Size of region */
555               DWORD type,   /* [in] Type of allocation */
556               DWORD protect /* [in] Type of access protection */
557 ) {
558     FILE_VIEW *view;
559     UINT base, ptr, view_size;
560     BYTE vprot;
561
562     TRACE("%08x %08lx %lx %08lx\n",
563                      (UINT)addr, size, type, protect );
564
565     /* Round parameters to a page boundary */
566
567     if (size > 0x7fc00000)  /* 2Gb - 4Mb */
568     {
569         SetLastError( ERROR_OUTOFMEMORY );
570         return NULL;
571     }
572     if (addr)
573     {
574         if (type & MEM_RESERVE) /* Round down to 64k boundary */
575             base = (UINT)addr & ~granularity_mask;
576         else
577             base = ROUND_ADDR( addr );
578         size = (((UINT)addr + size + page_mask) & ~page_mask) - base;
579         if ((base <= granularity_mask) || (base + size < base))
580         {
581             /* disallow low 64k and wrap-around */
582             SetLastError( ERROR_INVALID_PARAMETER );
583             return NULL;
584         }
585     }
586     else
587     {
588         base = 0;
589         size = (size + page_mask) & ~page_mask;
590     }
591
592     if (type & MEM_TOP_DOWN) {
593         /* FIXME: MEM_TOP_DOWN allocates the largest possible address.
594          *        Is there _ANY_ way to do it with UNIX mmap()?
595          */
596         WARN("MEM_TOP_DOWN ignored\n");
597         type &= ~MEM_TOP_DOWN;
598     }
599     /* Compute the protection flags */
600
601     if (!(type & (MEM_COMMIT | MEM_RESERVE)) ||
602         (type & ~(MEM_COMMIT | MEM_RESERVE)))
603     {
604         SetLastError( ERROR_INVALID_PARAMETER );
605         return NULL;
606     }
607     if (type & MEM_COMMIT)
608         vprot = VIRTUAL_GetProt( protect ) | VPROT_COMMITTED;
609     else vprot = 0;
610
611     /* Reserve the memory */
612
613     if ((type & MEM_RESERVE) || !base)
614     {
615         view_size = size + (base ? 0 : granularity_mask + 1);
616         ptr = (UINT)FILE_dommap( -1, (LPVOID)base, 0, view_size, 0, 0,
617                                    VIRTUAL_GetUnixProt( vprot ), MAP_PRIVATE );
618         if (ptr == (UINT)-1)
619         {
620             SetLastError( ERROR_OUTOFMEMORY );
621             return NULL;
622         }
623         if (!base)
624         {
625             /* Release the extra memory while keeping the range */
626             /* starting on a 64k boundary. */
627
628             if (ptr & granularity_mask)
629             {
630                 UINT extra = granularity_mask + 1 - (ptr & granularity_mask);
631                 FILE_munmap( (void *)ptr, 0, extra );
632                 ptr += extra;
633                 view_size -= extra;
634             }
635             if (view_size > size)
636                 FILE_munmap( (void *)(ptr + size), 0, view_size - size );
637         }
638         else if (ptr != base)
639         {
640             /* We couldn't get the address we wanted */
641             FILE_munmap( (void *)ptr, 0, view_size );
642             SetLastError( ERROR_INVALID_ADDRESS );
643             return NULL;
644         }
645         if (!(view = VIRTUAL_CreateView( ptr, size, 0, 0, vprot, -1 )))
646         {
647             FILE_munmap( (void *)ptr, 0, size );
648             SetLastError( ERROR_OUTOFMEMORY );
649             return NULL;
650         }
651         VIRTUAL_DEBUG_DUMP_VIEW( view );
652         return (LPVOID)ptr;
653     }
654
655     /* Commit the pages */
656
657     if (!(view = VIRTUAL_FindView( base )) ||
658         (base + size > view->base + view->size))
659     {
660         SetLastError( ERROR_INVALID_ADDRESS );
661         return NULL;
662     }
663
664     if (!VIRTUAL_SetProt( view, base, size, vprot )) return NULL;
665     return (LPVOID)base;
666 }
667
668
669 /***********************************************************************
670  *             VirtualFree   (KERNEL32.550)
671  * Release or decommits a region of pages in virtual address space.
672  * 
673  * RETURNS
674  *      TRUE: Success
675  *      FALSE: Failure
676  */
677 BOOL WINAPI VirtualFree(
678               LPVOID addr, /* [in] Address of region of committed pages */
679               DWORD size,  /* [in] Size of region */
680               DWORD type   /* [in] Type of operation */
681 ) {
682     FILE_VIEW *view;
683     UINT base;
684
685     TRACE("%08x %08lx %lx\n",
686                      (UINT)addr, size, type );
687
688     /* Fix the parameters */
689
690     size = ROUND_SIZE( addr, size );
691     base = ROUND_ADDR( addr );
692
693     if (!(view = VIRTUAL_FindView( base )) ||
694         (base + size > view->base + view->size))
695     {
696         SetLastError( ERROR_INVALID_PARAMETER );
697         return FALSE;
698     }
699
700     /* Compute the protection flags */
701
702     if ((type != MEM_DECOMMIT) && (type != MEM_RELEASE))
703     {
704         SetLastError( ERROR_INVALID_PARAMETER );
705         return FALSE;
706     }
707
708     /* Free the pages */
709
710     if (type == MEM_RELEASE)
711     {
712         if (size || (base != view->base))
713         {
714             SetLastError( ERROR_INVALID_PARAMETER );
715             return FALSE;
716         }
717         VIRTUAL_DeleteView( view );
718         return TRUE;
719     }
720
721     /* Decommit the pages by remapping zero-pages instead */
722
723     if (FILE_dommap( -1, (LPVOID)base, 0, size, 0, 0,
724                      VIRTUAL_GetUnixProt( 0 ), MAP_PRIVATE|MAP_FIXED ) 
725         != (LPVOID)base)
726         ERR( "Could not remap pages, expect trouble\n" );
727     return VIRTUAL_SetProt( view, base, size, 0 );
728 }
729
730
731 /***********************************************************************
732  *             VirtualLock   (KERNEL32.551)
733  * Locks the specified region of virtual address space
734  * 
735  * NOTE
736  *      Always returns TRUE
737  *
738  * RETURNS
739  *      TRUE: Success
740  *      FALSE: Failure
741  */
742 BOOL WINAPI VirtualLock(
743               LPVOID addr, /* [in] Address of first byte of range to lock */
744               DWORD size   /* [in] Number of bytes in range to lock */
745 ) {
746     return TRUE;
747 }
748
749
750 /***********************************************************************
751  *             VirtualUnlock   (KERNEL32.556)
752  * Unlocks a range of pages in the virtual address space
753  *
754  * NOTE
755  *      Always returns TRUE
756  *
757  * RETURNS
758  *      TRUE: Success
759  *      FALSE: Failure
760  */
761 BOOL WINAPI VirtualUnlock(
762               LPVOID addr, /* [in] Address of first byte of range */
763               DWORD size   /* [in] Number of bytes in range */
764 ) {
765     return TRUE;
766 }
767
768
769 /***********************************************************************
770  *             VirtualProtect   (KERNEL32.552)
771  * Changes the access protection on a region of committed pages
772  *
773  * RETURNS
774  *      TRUE: Success
775  *      FALSE: Failure
776  */
777 BOOL WINAPI VirtualProtect(
778               LPVOID addr,     /* [in] Address of region of committed pages */
779               DWORD size,      /* [in] Size of region */
780               DWORD new_prot,  /* [in] Desired access protection */
781               LPDWORD old_prot /* [out] Address of variable to get old protection */
782 ) {
783     FILE_VIEW *view;
784     UINT base, i;
785     BYTE vprot, *p;
786
787     TRACE("%08x %08lx %08lx\n",
788                      (UINT)addr, size, new_prot );
789
790     /* Fix the parameters */
791
792     size = ROUND_SIZE( addr, size );
793     base = ROUND_ADDR( addr );
794
795     if (!(view = VIRTUAL_FindView( base )) ||
796         (base + size > view->base + view->size))
797     {
798         SetLastError( ERROR_INVALID_PARAMETER );
799         return FALSE;
800     }
801
802     /* Make sure all the pages are committed */
803
804     p = view->prot + ((base - view->base) >> page_shift);
805     for (i = size >> page_shift; i; i--, p++)
806     {
807         if (!(*p & VPROT_COMMITTED))
808         {
809             SetLastError( ERROR_INVALID_PARAMETER );
810             return FALSE;
811         }
812     }
813
814     VIRTUAL_GetWin32Prot( view->prot[0], old_prot, NULL );
815     vprot = VIRTUAL_GetProt( new_prot ) | VPROT_COMMITTED;
816     return VIRTUAL_SetProt( view, base, size, vprot );
817 }
818
819
820 /***********************************************************************
821  *             VirtualProtectEx   (KERNEL32.553)
822  * Changes the access protection on a region of committed pages in the
823  * virtual address space of a specified process
824  *
825  * RETURNS
826  *      TRUE: Success
827  *      FALSE: Failure
828  */
829 BOOL WINAPI VirtualProtectEx(
830               HANDLE handle, /* [in]  Handle of process */
831               LPVOID addr,     /* [in]  Address of region of committed pages */
832               DWORD size,      /* [in]  Size of region */
833               DWORD new_prot,  /* [in]  Desired access protection */
834               LPDWORD old_prot /* [out] Address of variable to get old protection */ )
835 {
836     if (MapProcessHandle( handle ) == GetCurrentProcessId())
837         return VirtualProtect( addr, size, new_prot, old_prot );
838     ERR("Unsupported on other process\n");
839     return FALSE;
840 }
841
842
843 /***********************************************************************
844  *             VirtualQuery   (KERNEL32.554)
845  * Provides info about a range of pages in virtual address space
846  *
847  * RETURNS
848  *      Number of bytes returned in information buffer
849  */
850 DWORD WINAPI VirtualQuery(
851              LPCVOID addr,                    /* [in]  Address of region */
852              LPMEMORY_BASIC_INFORMATION info, /* [out] Address of info buffer */
853              DWORD len                        /* [in]  Size of buffer */
854 ) {
855     FILE_VIEW *view = VIRTUAL_FirstView;
856     UINT base = ROUND_ADDR( addr );
857     UINT alloc_base = 0;
858     UINT size = 0;
859
860     /* Find the view containing the address */
861
862     for (;;)
863     {
864         if (!view)
865         {
866             size = 0xffff0000 - alloc_base;
867             break;
868         }
869         if (view->base > base)
870         {
871             size = view->base - alloc_base;
872             view = NULL;
873             break;
874         }
875         if (view->base + view->size > base)
876         {
877             alloc_base = view->base;
878             size = view->size;
879             break;
880         }
881         alloc_base = view->base + view->size;
882         view = view->next;
883     }
884
885     /* Fill the info structure */
886
887     if (!view)
888     {
889         info->State             = MEM_FREE;
890         info->Protect           = 0;
891         info->AllocationProtect = 0;
892         info->Type              = 0;
893     }
894     else
895     {
896         BYTE vprot = view->prot[(base - alloc_base) >> page_shift];
897         VIRTUAL_GetWin32Prot( vprot, &info->Protect, &info->State );
898         for (size = base - alloc_base; size < view->size; size += page_mask+1)
899             if (view->prot[size >> page_shift] != vprot) break;
900         info->AllocationProtect = view->protect;
901         info->Type              = MEM_PRIVATE;  /* FIXME */
902     }
903
904     info->BaseAddress    = (LPVOID)base;
905     info->AllocationBase = (LPVOID)alloc_base;
906     info->RegionSize     = size - (base - alloc_base);
907     return sizeof(*info);
908 }
909
910
911 /***********************************************************************
912  *             VirtualQueryEx   (KERNEL32.555)
913  * Provides info about a range of pages in virtual address space of a
914  * specified process
915  *
916  * RETURNS
917  *      Number of bytes returned in information buffer
918  */
919 DWORD WINAPI VirtualQueryEx(
920              HANDLE handle,                 /* [in] Handle of process */
921              LPCVOID addr,                    /* [in] Address of region */
922              LPMEMORY_BASIC_INFORMATION info, /* [out] Address of info buffer */
923              DWORD len                        /* [in] Size of buffer */ )
924 {
925     if (MapProcessHandle( handle ) == GetCurrentProcessId())
926         return VirtualQuery( addr, info, len );
927     ERR("Unsupported on other process\n");
928     return 0;
929 }
930
931
932 /***********************************************************************
933  *             IsBadReadPtr   (KERNEL32.354)
934  *
935  * RETURNS
936  *      FALSE: Process has read access to entire block
937  *      TRUE: Otherwise
938  */
939 BOOL WINAPI IsBadReadPtr(
940               LPCVOID ptr, /* Address of memory block */
941               UINT size  /* Size of block */
942 ) {
943     return !VIRTUAL_CheckFlags( (UINT)ptr, size,
944                                 VPROT_READ | VPROT_COMMITTED );
945 }
946
947
948 /***********************************************************************
949  *             IsBadWritePtr   (KERNEL32.357)
950  *
951  * RETURNS
952  *      FALSE: Process has write access to entire block
953  *      TRUE: Otherwise
954  */
955 BOOL WINAPI IsBadWritePtr(
956               LPVOID ptr, /* [in] Address of memory block */
957               UINT size /* [in] Size of block in bytes */
958 ) {
959     return !VIRTUAL_CheckFlags( (UINT)ptr, size,
960                                 VPROT_WRITE | VPROT_COMMITTED );
961 }
962
963
964 /***********************************************************************
965  *             IsBadHugeReadPtr   (KERNEL32.352)
966  * RETURNS
967  *      FALSE: Process has read access to entire block
968  *      TRUE: Otherwise
969  */
970 BOOL WINAPI IsBadHugeReadPtr(
971               LPCVOID ptr, /* [in] Address of memory block */
972               UINT size  /* [in] Size of block */
973 ) {
974     return IsBadReadPtr( ptr, size );
975 }
976
977
978 /***********************************************************************
979  *             IsBadHugeWritePtr   (KERNEL32.353)
980  * RETURNS
981  *      FALSE: Process has write access to entire block
982  *      TRUE: Otherwise
983  */
984 BOOL WINAPI IsBadHugeWritePtr(
985               LPVOID ptr, /* [in] Address of memory block */
986               UINT size /* [in] Size of block */
987 ) {
988     return IsBadWritePtr( ptr, size );
989 }
990
991
992 /***********************************************************************
993  *             IsBadCodePtr   (KERNEL32.351)
994  *
995  * RETURNS
996  *      FALSE: Process has read access to specified memory
997  *      TRUE: Otherwise
998  */
999 BOOL WINAPI IsBadCodePtr(
1000               FARPROC ptr /* [in] Address of function */
1001 ) {
1002     return !VIRTUAL_CheckFlags( (UINT)ptr, 1, VPROT_EXEC | VPROT_COMMITTED );
1003 }
1004
1005
1006 /***********************************************************************
1007  *             IsBadStringPtrA   (KERNEL32.355)
1008  *
1009  * RETURNS
1010  *      FALSE: Read access to all bytes in string
1011  *      TRUE: Else
1012  */
1013 BOOL WINAPI IsBadStringPtrA(
1014               LPCSTR str, /* [in] Address of string */
1015               UINT max  /* [in] Maximum size of string */
1016 ) {
1017     FILE_VIEW *view;
1018     UINT page, count;
1019
1020     if (!max) return FALSE;
1021     if (!(view = VIRTUAL_FindView( (UINT)str ))) return TRUE;
1022     page  = ((UINT)str - view->base) >> page_shift;
1023     count = page_mask + 1 - ((UINT)str & page_mask);
1024
1025     while (max)
1026     {
1027         if ((view->prot[page] & (VPROT_READ | VPROT_COMMITTED)) != 
1028                                                 (VPROT_READ | VPROT_COMMITTED))
1029             return TRUE;
1030         if (count > max) count = max;
1031         max -= count;
1032         while (count--) if (!*str++) return FALSE;
1033         if (++page >= view->size >> page_shift) return TRUE;
1034         count = page_mask + 1;
1035     }
1036     return FALSE;
1037 }
1038
1039
1040 /***********************************************************************
1041  *             IsBadStringPtrW   (KERNEL32.356)
1042  * See IsBadStringPtrA
1043  */
1044 BOOL WINAPI IsBadStringPtrW( LPCWSTR str, UINT max )
1045 {
1046     FILE_VIEW *view;
1047     UINT page, count;
1048
1049     if (!max) return FALSE;
1050     if (!(view = VIRTUAL_FindView( (UINT)str ))) return TRUE;
1051     page  = ((UINT)str - view->base) >> page_shift;
1052     count = (page_mask + 1 - ((UINT)str & page_mask)) / sizeof(WCHAR);
1053
1054     while (max)
1055     {
1056         if ((view->prot[page] & (VPROT_READ | VPROT_COMMITTED)) != 
1057                                                 (VPROT_READ | VPROT_COMMITTED))
1058             return TRUE;
1059         if (count > max) count = max;
1060         max -= count;
1061         while (count--) if (!*str++) return FALSE;
1062         if (++page >= view->size >> page_shift) return TRUE;
1063         count = (page_mask + 1) / sizeof(WCHAR);
1064     }
1065     return FALSE;
1066 }
1067
1068
1069 /***********************************************************************
1070  *             CreateFileMappingA   (KERNEL32.46)
1071  * Creates a named or unnamed file-mapping object for the specified file
1072  *
1073  * RETURNS
1074  *      Handle: Success
1075  *      0: Mapping object does not exist
1076  *      NULL: Failure
1077  */
1078 HANDLE WINAPI CreateFileMappingA(
1079                 HFILE hFile,   /* [in] Handle of file to map */
1080                 SECURITY_ATTRIBUTES *sa, /* [in] Optional security attributes*/
1081                 DWORD protect,   /* [in] Protection for mapping object */
1082                 DWORD size_high, /* [in] High-order 32 bits of object size */
1083                 DWORD size_low,  /* [in] Low-order 32 bits of object size */
1084                 LPCSTR name      /* [in] Name of file-mapping object */ )
1085 {
1086     struct create_mapping_request *req = get_req_buffer();
1087     BYTE vprot;
1088
1089     /* Check parameters */
1090
1091     TRACE("(%x,%p,%08lx,%08lx%08lx,%s)\n",
1092           hFile, sa, protect, size_high, size_low, debugstr_a(name) );
1093
1094     vprot = VIRTUAL_GetProt( protect );
1095     if (protect & SEC_RESERVE)
1096     {
1097         if (hFile != INVALID_HANDLE_VALUE)
1098         {
1099             SetLastError( ERROR_INVALID_PARAMETER );
1100             return 0;
1101         }
1102     }
1103     else vprot |= VPROT_COMMITTED;
1104     if (protect & SEC_NOCACHE) vprot |= VPROT_NOCACHE;
1105
1106     /* Create the server object */
1107
1108     req->file_handle = hFile;
1109     req->size_high   = size_high;
1110     req->size_low    = size_low;
1111     req->protect     = vprot;
1112     req->inherit     = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
1113     server_strcpyAtoW( req->name, name );
1114     SetLastError(0);
1115     server_call( REQ_CREATE_MAPPING );
1116     if (req->handle == -1) return 0;
1117     return req->handle;
1118 }
1119
1120
1121 /***********************************************************************
1122  *             CreateFileMappingW   (KERNEL32.47)
1123  * See CreateFileMappingA
1124  */
1125 HANDLE WINAPI CreateFileMappingW( HFILE hFile, LPSECURITY_ATTRIBUTES sa, 
1126                                   DWORD protect, DWORD size_high,  
1127                                   DWORD size_low, LPCWSTR name )
1128 {
1129     struct create_mapping_request *req = get_req_buffer();
1130     BYTE vprot;
1131
1132     /* Check parameters */
1133
1134     TRACE("(%x,%p,%08lx,%08lx%08lx,%s)\n",
1135           hFile, sa, protect, size_high, size_low, debugstr_w(name) );
1136
1137     vprot = VIRTUAL_GetProt( protect );
1138     if (protect & SEC_RESERVE)
1139     {
1140         if (hFile != INVALID_HANDLE_VALUE)
1141         {
1142             SetLastError( ERROR_INVALID_PARAMETER );
1143             return 0;
1144         }
1145     }
1146     else vprot |= VPROT_COMMITTED;
1147     if (protect & SEC_NOCACHE) vprot |= VPROT_NOCACHE;
1148
1149     /* Create the server object */
1150
1151     req->file_handle = hFile;
1152     req->size_high   = size_high;
1153     req->size_low    = size_low;
1154     req->protect     = vprot;
1155     req->inherit     = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
1156     server_strcpyW( req->name, name );
1157     SetLastError(0);
1158     server_call( REQ_CREATE_MAPPING );
1159     if (req->handle == -1) return 0;
1160     return req->handle;
1161 }
1162
1163
1164 /***********************************************************************
1165  *             OpenFileMappingA   (KERNEL32.397)
1166  * Opens a named file-mapping object.
1167  *
1168  * RETURNS
1169  *      Handle: Success
1170  *      NULL: Failure
1171  */
1172 HANDLE WINAPI OpenFileMappingA(
1173                 DWORD access,   /* [in] Access mode */
1174                 BOOL inherit, /* [in] Inherit flag */
1175                 LPCSTR name )   /* [in] Name of file-mapping object */
1176 {
1177     struct open_mapping_request *req = get_req_buffer();
1178
1179     req->access  = access;
1180     req->inherit = inherit;
1181     server_strcpyAtoW( req->name, name );
1182     server_call( REQ_OPEN_MAPPING );
1183     if (req->handle == -1) return 0; /* must return 0 on failure, not -1 */
1184     return req->handle;
1185 }
1186
1187
1188 /***********************************************************************
1189  *             OpenFileMappingW   (KERNEL32.398)
1190  * See OpenFileMappingA
1191  */
1192 HANDLE WINAPI OpenFileMappingW( DWORD access, BOOL inherit, LPCWSTR name)
1193 {
1194     struct open_mapping_request *req = get_req_buffer();
1195
1196     req->access  = access;
1197     req->inherit = inherit;
1198     server_strcpyW( req->name, name );
1199     server_call( REQ_OPEN_MAPPING );
1200     if (req->handle == -1) return 0; /* must return 0 on failure, not -1 */
1201     return req->handle;
1202 }
1203
1204
1205 /***********************************************************************
1206  *             MapViewOfFile   (KERNEL32.385)
1207  * Maps a view of a file into the address space
1208  *
1209  * RETURNS
1210  *      Starting address of mapped view
1211  *      NULL: Failure
1212  */
1213 LPVOID WINAPI MapViewOfFile(
1214               HANDLE mapping,  /* [in] File-mapping object to map */
1215               DWORD access,      /* [in] Access mode */
1216               DWORD offset_high, /* [in] High-order 32 bits of file offset */
1217               DWORD offset_low,  /* [in] Low-order 32 bits of file offset */
1218               DWORD count        /* [in] Number of bytes to map */
1219 ) {
1220     return MapViewOfFileEx( mapping, access, offset_high,
1221                             offset_low, count, NULL );
1222 }
1223
1224
1225 /***********************************************************************
1226  *             MapViewOfFileEx   (KERNEL32.386)
1227  * Maps a view of a file into the address space
1228  *
1229  * RETURNS
1230  *      Starting address of mapped view
1231  *      NULL: Failure
1232  */
1233 LPVOID WINAPI MapViewOfFileEx(
1234               HANDLE handle,   /* [in] File-mapping object to map */
1235               DWORD access,      /* [in] Access mode */
1236               DWORD offset_high, /* [in] High-order 32 bits of file offset */
1237               DWORD offset_low,  /* [in] Low-order 32 bits of file offset */
1238               DWORD count,       /* [in] Number of bytes to map */
1239               LPVOID addr        /* [in] Suggested starting address for mapped view */
1240 ) {
1241     FILE_VIEW *view;
1242     UINT ptr = (UINT)-1, size = 0;
1243     int flags = MAP_PRIVATE;
1244     int unix_handle = -1;
1245     int prot;
1246     struct get_mapping_info_request *req = get_req_buffer();
1247
1248     /* Check parameters */
1249
1250     if ((offset_low & granularity_mask) ||
1251         (addr && ((UINT)addr & granularity_mask)))
1252     {
1253         SetLastError( ERROR_INVALID_PARAMETER );
1254         return NULL;
1255     }
1256
1257     req->handle = handle;
1258     if (server_call_fd( REQ_GET_MAPPING_INFO, -1, &unix_handle )) goto error;
1259
1260     if (req->size_high || offset_high)
1261         ERR("Offsets larger than 4Gb not supported\n");
1262
1263     if ((offset_low >= req->size_low) ||
1264         (count > req->size_low - offset_low))
1265     {
1266         SetLastError( ERROR_INVALID_PARAMETER );
1267         goto error;
1268     }
1269     if (count) size = ROUND_SIZE( offset_low, count );
1270     else size = req->size_low - offset_low;
1271     prot = req->protect;
1272
1273     switch(access)
1274     {
1275     case FILE_MAP_ALL_ACCESS:
1276     case FILE_MAP_WRITE:
1277     case FILE_MAP_WRITE | FILE_MAP_READ:
1278         if (!(prot & VPROT_WRITE))
1279         {
1280             SetLastError( ERROR_INVALID_PARAMETER );
1281             goto error;
1282         }
1283         flags = MAP_SHARED;
1284         /* fall through */
1285     case FILE_MAP_READ:
1286     case FILE_MAP_COPY:
1287     case FILE_MAP_COPY | FILE_MAP_READ:
1288         if (prot & VPROT_READ) break;
1289         /* fall through */
1290     default:
1291         SetLastError( ERROR_INVALID_PARAMETER );
1292         goto error;
1293     }
1294
1295     /* Map the file */
1296
1297     TRACE("handle=%x size=%x offset=%lx\n", handle, size, offset_low );
1298
1299     ptr = (UINT)FILE_dommap( unix_handle, addr, 0, size, 0, offset_low,
1300                              VIRTUAL_GetUnixProt( prot ), flags );
1301     if (ptr == (UINT)-1) {
1302         /* KB: Q125713, 25-SEP-1995, "Common File Mapping Problems and
1303          * Platform Differences": 
1304          * Windows NT: ERROR_INVALID_PARAMETER
1305          * Windows 95: ERROR_INVALID_ADDRESS.
1306          * FIXME: So should we add a module dependend check here? -MM
1307          */
1308         if (errno==ENOMEM)
1309             SetLastError( ERROR_OUTOFMEMORY );
1310         else
1311             SetLastError( ERROR_INVALID_PARAMETER );
1312         goto error;
1313     }
1314
1315     if (!(view = VIRTUAL_CreateView( ptr, size, offset_low, 0, prot, handle )))
1316     {
1317         SetLastError( ERROR_OUTOFMEMORY );
1318         goto error;
1319     }
1320     if (unix_handle != -1) close( unix_handle );
1321     return (LPVOID)ptr;
1322
1323 error:
1324     if (unix_handle != -1) close( unix_handle );
1325     if (ptr != (UINT)-1) FILE_munmap( (void *)ptr, 0, size );
1326     return NULL;
1327 }
1328
1329
1330 /***********************************************************************
1331  *             FlushViewOfFile   (KERNEL32.262)
1332  * Writes to the disk a byte range within a mapped view of a file
1333  *
1334  * RETURNS
1335  *      TRUE: Success
1336  *      FALSE: Failure
1337  */
1338 BOOL WINAPI FlushViewOfFile(
1339               LPCVOID base, /* [in] Start address of byte range to flush */
1340               DWORD cbFlush /* [in] Number of bytes in range */
1341 ) {
1342     FILE_VIEW *view;
1343     UINT addr = ROUND_ADDR( base );
1344
1345     TRACE("FlushViewOfFile at %p for %ld bytes\n",
1346                      base, cbFlush );
1347
1348     if (!(view = VIRTUAL_FindView( addr )))
1349     {
1350         SetLastError( ERROR_INVALID_PARAMETER );
1351         return FALSE;
1352     }
1353     if (!cbFlush) cbFlush = view->size;
1354     if (!msync( (void *)addr, cbFlush, MS_SYNC )) return TRUE;
1355     SetLastError( ERROR_INVALID_PARAMETER );
1356     return FALSE;
1357 }
1358
1359
1360 /***********************************************************************
1361  *             UnmapViewOfFile   (KERNEL32.540)
1362  * Unmaps a mapped view of a file.
1363  *
1364  * NOTES
1365  *      Should addr be an LPCVOID?
1366  *
1367  * RETURNS
1368  *      TRUE: Success
1369  *      FALSE: Failure
1370  */
1371 BOOL WINAPI UnmapViewOfFile(
1372               LPVOID addr /* [in] Address where mapped view begins */
1373 ) {
1374     FILE_VIEW *view;
1375     UINT base = ROUND_ADDR( addr );
1376     if (!(view = VIRTUAL_FindView( base )) || (base != view->base))
1377     {
1378         SetLastError( ERROR_INVALID_PARAMETER );
1379         return FALSE;
1380     }
1381     VIRTUAL_DeleteView( view );
1382     return TRUE;
1383 }
1384
1385 /***********************************************************************
1386  *             VIRTUAL_MapFileW
1387  *
1388  * Helper function to map a file to memory:
1389  *  name                        -       file name 
1390  *  [RETURN] ptr                -       pointer to mapped file
1391  */
1392 LPVOID VIRTUAL_MapFileW( LPCWSTR name )
1393 {
1394     HANDLE hFile, hMapping;
1395     LPVOID ptr = NULL;
1396
1397     hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL, 
1398                            OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0);
1399     if (hFile != INVALID_HANDLE_VALUE)
1400     {
1401         hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
1402         CloseHandle( hFile );
1403         if (hMapping)
1404         {
1405             ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
1406             CloseHandle( hMapping );
1407         }
1408     }
1409     return ptr;
1410 }