wined3d: Do not setup states for unlocking if unlocking is disabled.
[wine] / dlls / vmm.vxd / vmm.c
1 /*
2  * VMM VxD implementation
3  *
4  * Copyright 1998 Marcus Meissner
5  * Copyright 1998 Ulrich Weigand
6  * Copyright 1998 Patrik Stridvall
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdarg.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winreg.h"
31 #include "winnls.h"
32 #include "winternl.h"
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(vxd);
36
37 /*
38  * VMM VxDCall service names are (mostly) taken from Stan Mitchell's
39  * "Inside the Windows 95 File System"
40  */
41
42 #define N_VMM_SERVICE 41
43
44 static const char * const VMM_Service_Name[N_VMM_SERVICE] =
45 {
46     "PageReserve",            /* 0x0000 */
47     "PageCommit",             /* 0x0001 */
48     "PageDecommit",           /* 0x0002 */
49     "PagerRegister",          /* 0x0003 */
50     "PagerQuery",             /* 0x0004 */
51     "HeapAllocate",           /* 0x0005 */
52     "ContextCreate",          /* 0x0006 */
53     "ContextDestroy",         /* 0x0007 */
54     "PageAttach",             /* 0x0008 */
55     "PageFlush",              /* 0x0009 */
56     "PageFree",               /* 0x000A */
57     "ContextSwitch",          /* 0x000B */
58     "HeapReAllocate",         /* 0x000C */
59     "PageModifyPermissions",  /* 0x000D */
60     "PageQuery",              /* 0x000E */
61     "GetCurrentContext",      /* 0x000F */
62     "HeapFree",               /* 0x0010 */
63     "RegOpenKey",             /* 0x0011 */
64     "RegCreateKey",           /* 0x0012 */
65     "RegCloseKey",            /* 0x0013 */
66     "RegDeleteKey",           /* 0x0014 */
67     "RegSetValue",            /* 0x0015 */
68     "RegDeleteValue",         /* 0x0016 */
69     "RegQueryValue",          /* 0x0017 */
70     "RegEnumKey",             /* 0x0018 */
71     "RegEnumValue",           /* 0x0019 */
72     "RegQueryValueEx",        /* 0x001A */
73     "RegSetValueEx",          /* 0x001B */
74     "RegFlushKey",            /* 0x001C */
75     "RegQueryInfoKey",        /* 0x001D */
76     "GetDemandPageInfo",      /* 0x001E */
77     "BlockOnID",              /* 0x001F */
78     "SignalID",               /* 0x0020 */
79     "RegLoadKey",             /* 0x0021 */
80     "RegUnLoadKey",           /* 0x0022 */
81     "RegSaveKey",             /* 0x0023 */
82     "RegRemapPreDefKey",      /* 0x0024 */
83     "PageChangePager",        /* 0x0025 */
84     "RegQueryMultipleValues", /* 0x0026 */
85     "RegReplaceKey",          /* 0x0027 */
86     "<KERNEL32.101>"          /* 0x0028 -- What does this do??? */
87 };
88
89 /* PageReserve arena values */
90 #define PR_PRIVATE    0x80000400 /* anywhere in private arena */
91 #define PR_SHARED     0x80060000 /* anywhere in shared arena */
92 #define PR_SYSTEM     0x80080000 /* anywhere in system arena */
93
94 /* PageReserve flags */
95 #define PR_FIXED      0x00000008 /* don't move during PageReAllocate */
96 #define PR_4MEG       0x00000001 /* allocate on 4mb boundary */
97 #define PR_STATIC     0x00000010 /* see PageReserve documentation */
98
99 /* PageCommit default pager handle values */
100 #define PD_ZEROINIT   0x00000001 /* swappable zero-initialized pages */
101 #define PD_NOINIT     0x00000002 /* swappable uninitialized pages */
102 #define PD_FIXEDZERO  0x00000003 /* fixed zero-initialized pages */
103 #define PD_FIXED      0x00000004 /* fixed uninitialized pages */
104
105 /* PageCommit flags */
106 #define PC_FIXED      0x00000008 /* pages are permanently locked */
107 #define PC_LOCKED     0x00000080 /* pages are made present and locked */
108 #define PC_LOCKEDIFDP 0x00000100 /* pages are locked if swap via DOS */
109 #define PC_WRITABLE   0x00020000 /* make the pages writable */
110 #define PC_USER       0x00040000 /* make the pages ring 3 accessible */
111 #define PC_INCR       0x40000000 /* increment "pagerdata" each page */
112 #define PC_PRESENT    0x80000000 /* make pages initially present */
113 #define PC_STATIC     0x20000000 /* allow commit in PR_STATIC object */
114 #define PC_DIRTY      0x08000000 /* make pages initially dirty */
115 #define PC_CACHEDIS   0x00100000 /* Allocate uncached pages - new for WDM */
116 #define PC_CACHEWT    0x00080000 /* Allocate write through cache pages - new for WDM */
117 #define PC_PAGEFLUSH  0x00008000 /* Touch device mapped pages on alloc - new for WDM */
118
119 /* PageCommitContig additional flags */
120 #define PCC_ZEROINIT  0x00000001 /* zero-initialize new pages */
121 #define PCC_NOLIN     0x10000000 /* don't map to any linear address */
122
123
124 /* Pop a DWORD from the 32-bit stack */
125 static inline DWORD stack32_pop( CONTEXT86 *context )
126 {
127     DWORD ret = *(DWORD *)context->Esp;
128     context->Esp += sizeof(DWORD);
129     return ret;
130 }
131
132
133 /***********************************************************************
134  *           VxDCall   (VMM.VXD.@)
135  */
136 DWORD WINAPI VMM_VxDCall( DWORD service, CONTEXT86 *context )
137 {
138     static int warned;
139
140     switch ( LOWORD(service) )
141     {
142     case 0x0000: /* PageReserve */
143     {
144         LPVOID address;
145         LPVOID ret;
146         DWORD psize = getpagesize();
147         ULONG page   = (ULONG) stack32_pop( context );
148         ULONG npages = (ULONG) stack32_pop( context );
149         ULONG flags  = (ULONG) stack32_pop( context );
150
151         TRACE("PageReserve: page: %08x, npages: %08x, flags: %08x partial stub!\n",
152               page, npages, flags );
153
154         if ( page == PR_SYSTEM ) {
155           ERR("Can't reserve ring 1 memory\n");
156           return -1;
157         }
158         /* FIXME: This has to be handled separately for the separate
159            address-spaces we now have */
160         if ( page == PR_PRIVATE || page == PR_SHARED ) page = 0;
161         /* FIXME: Handle flags in some way */
162         address = (LPVOID )(page * psize);
163         ret = VirtualAlloc ( address, ( npages * psize ), MEM_RESERVE, 0 );
164         TRACE("PageReserve: returning: %p\n", ret );
165         if ( ret == NULL )
166           return -1;
167         else
168           return (DWORD )ret;
169     }
170
171     case 0x0001: /* PageCommit */
172     {
173         LPVOID address;
174         LPVOID ret;
175         DWORD virt_perm;
176         DWORD psize = getpagesize();
177         ULONG page   = (ULONG) stack32_pop( context );
178         ULONG npages = (ULONG) stack32_pop( context );
179         ULONG hpd  = (ULONG) stack32_pop( context );
180         ULONG pagerdata   = (ULONG) stack32_pop( context );
181         ULONG flags  = (ULONG) stack32_pop( context );
182
183         TRACE("PageCommit: page: %08x, npages: %08x, hpd: %08x pagerdata: "
184               "%08x, flags: %08x partial stub\n",
185               page, npages, hpd, pagerdata, flags );
186
187         if ( flags & PC_USER )
188           if ( flags & PC_WRITABLE )
189             virt_perm = PAGE_EXECUTE_READWRITE;
190           else
191             virt_perm = PAGE_EXECUTE_READ;
192         else
193           virt_perm = PAGE_NOACCESS;
194
195         address = (LPVOID )(page * psize);
196         ret = VirtualAlloc ( address, ( npages * psize ), MEM_COMMIT, virt_perm );
197         TRACE("PageCommit: Returning: %p\n", ret );
198         return (DWORD )ret;
199
200     }
201
202     case 0x0002: /* PageDecommit */
203     {
204         LPVOID address;
205         BOOL ret;
206         DWORD psize = getpagesize();
207         ULONG page = (ULONG) stack32_pop( context );
208         ULONG npages = (ULONG) stack32_pop( context );
209         ULONG flags = (ULONG) stack32_pop( context );
210
211         TRACE("PageDecommit: page: %08x, npages: %08x, flags: %08x partial stub\n",
212               page, npages, flags );
213         address = (LPVOID )( page * psize );
214         ret = VirtualFree ( address, ( npages * psize ), MEM_DECOMMIT );
215         TRACE("PageDecommit: Returning: %s\n", ret ? "TRUE" : "FALSE" );
216         return ret;
217     }
218
219     case 0x000d: /* PageModifyPermissions */
220     {
221         DWORD pg_old_perm;
222         DWORD pg_new_perm;
223         DWORD virt_old_perm;
224         DWORD virt_new_perm;
225         MEMORY_BASIC_INFORMATION mbi;
226         LPVOID address;
227         DWORD psize = getpagesize();
228         ULONG page = stack32_pop ( context );
229         ULONG npages = stack32_pop ( context );
230         ULONG permand = stack32_pop ( context );
231         ULONG permor = stack32_pop ( context );
232
233         TRACE("PageModifyPermissions %08x %08x %08x %08x partial stub\n",
234               page, npages, permand, permor );
235         address = (LPVOID )( page * psize );
236
237         VirtualQuery ( address, &mbi, sizeof ( MEMORY_BASIC_INFORMATION ));
238         virt_old_perm = mbi.Protect;
239
240         switch ( virt_old_perm & mbi.Protect ) {
241         case PAGE_READONLY:
242         case PAGE_EXECUTE:
243         case PAGE_EXECUTE_READ:
244           pg_old_perm = PC_USER;
245           break;
246         case PAGE_READWRITE:
247         case PAGE_WRITECOPY:
248         case PAGE_EXECUTE_READWRITE:
249         case PAGE_EXECUTE_WRITECOPY:
250           pg_old_perm = PC_USER | PC_WRITABLE;
251           break;
252         case PAGE_NOACCESS:
253         default:
254           pg_old_perm = 0;
255           break;
256         }
257         pg_new_perm = pg_old_perm;
258         pg_new_perm &= permand & ~PC_STATIC;
259         pg_new_perm |= permor  & ~PC_STATIC;
260
261         virt_new_perm = ( virt_old_perm )  & ~0xff;
262         if ( pg_new_perm & PC_USER )
263         {
264           if ( pg_new_perm & PC_WRITABLE )
265             virt_new_perm |= PAGE_EXECUTE_READWRITE;
266           else
267             virt_new_perm |= PAGE_EXECUTE_READ;
268         }
269
270         if ( ! VirtualProtect ( address, ( npages * psize ), virt_new_perm, &virt_old_perm ) ) {
271           ERR("Can't change page permissions for %p\n", address );
272           return 0xffffffff;
273         }
274         TRACE("Returning: %08x\n", pg_old_perm );
275         return pg_old_perm;
276     }
277
278     case 0x000a: /* PageFree */
279     {
280         BOOL ret;
281         LPVOID hmem = (LPVOID) stack32_pop( context );
282         DWORD flags = (DWORD ) stack32_pop( context );
283
284         TRACE("PageFree: hmem: %p, flags: %08x partial stub\n",
285               hmem, flags );
286
287         ret = VirtualFree ( hmem, 0, MEM_RELEASE );
288         TRACE("Returning: %d\n", ret );
289         return ret;
290     }
291
292     case 0x0011:  /* RegOpenKey */
293         stack32_pop( context ); /* kkey */
294         stack32_pop( context ); /* lpszSubKey */
295         stack32_pop( context ); /* retkey */
296         /* return RegOpenKeyExA( hkey, lpszSubKey, 0, KEY_ALL_ACCESS, retkey ); */
297         if (!warned)
298         {
299             ERR( "Using the native Win95 advapi32.dll is no longer supported.\n" );
300             ERR( "Please configure advapi32 to builtin.\n" );
301             warned++;
302         }
303         return ERROR_CALL_NOT_IMPLEMENTED;
304
305     case 0x0012:  /* RegCreateKey */
306         stack32_pop( context ); /* hkey */
307         stack32_pop( context ); /* lpszSubKey */
308         stack32_pop( context ); /* retkey */
309         /* return RegCreateKeyA( hkey, lpszSubKey, retkey ); */
310         if (!warned)
311         {
312             ERR( "Using the native Win95 advapi32.dll is no longer supported.\n" );
313             ERR( "Please configure advapi32 to builtin.\n" );
314             warned++;
315         }
316         return ERROR_CALL_NOT_IMPLEMENTED;
317
318     case 0x0013:  /* RegCloseKey */
319         stack32_pop( context ); /* hkey */
320         /* return RegCloseKey( hkey ); */
321         return ERROR_CALL_NOT_IMPLEMENTED;
322
323     case 0x0014:  /* RegDeleteKey */
324         stack32_pop( context ); /* hkey */
325         stack32_pop( context ); /* lpszSubKey */
326         /* return RegDeleteKeyA( hkey, lpszSubKey ); */
327         return ERROR_CALL_NOT_IMPLEMENTED;
328
329     case 0x0015:  /* RegSetValue */
330         stack32_pop( context ); /* hkey */
331         stack32_pop( context ); /* lpszSubKey */
332         stack32_pop( context ); /* dwType */
333         stack32_pop( context ); /* lpszData */
334         stack32_pop( context ); /* cbData */
335         /* return RegSetValueA( hkey, lpszSubKey, dwType, lpszData, cbData ); */
336         return ERROR_CALL_NOT_IMPLEMENTED;
337
338     case 0x0016:  /* RegDeleteValue */
339         stack32_pop( context ); /* hkey */
340         stack32_pop( context ); /* lpszValue */
341         /* return RegDeleteValueA( hkey, lpszValue ); */
342         return ERROR_CALL_NOT_IMPLEMENTED;
343
344     case 0x0017:  /* RegQueryValue */
345         stack32_pop( context ); /* hkey */
346         stack32_pop( context ); /* lpszSubKey */
347         stack32_pop( context ); /* lpszData */
348         stack32_pop( context ); /* lpcbData */
349         /* return RegQueryValueA( hkey, lpszSubKey, lpszData, lpcbData ); */
350         return ERROR_CALL_NOT_IMPLEMENTED;
351
352     case 0x0018:  /* RegEnumKey */
353         stack32_pop( context ); /* hkey */
354         stack32_pop( context ); /* iSubkey */
355         stack32_pop( context ); /* lpszName */
356         stack32_pop( context ); /* lpcchName */
357         /* return RegEnumKeyA( hkey, iSubkey, lpszName, lpcchName ); */
358         return ERROR_CALL_NOT_IMPLEMENTED;
359
360     case 0x0019:  /* RegEnumValue */
361         stack32_pop( context ); /* hkey */
362         stack32_pop( context ); /* iValue */
363         stack32_pop( context ); /* lpszValue */
364         stack32_pop( context ); /* lpcchValue */
365         stack32_pop( context ); /* lpReserved */
366         stack32_pop( context ); /* lpdwType */
367         stack32_pop( context ); /* lpbData */
368         stack32_pop( context ); /* lpcbData */
369         /* return RegEnumValueA( hkey, iValue, lpszValue, lpcchValue, lpReserved, lpdwType, lpbData, lpcbData ); */
370         return ERROR_CALL_NOT_IMPLEMENTED;
371
372     case 0x001A:  /* RegQueryValueEx */
373         stack32_pop( context ); /* hkey */
374         stack32_pop( context ); /* lpszValue */
375         stack32_pop( context ); /* lpReserved */
376         stack32_pop( context ); /* lpdwType */
377         stack32_pop( context ); /* lpbData */
378         stack32_pop( context ); /* lpcbData */
379         /* return RegQueryValueExA( hkey, lpszValue, lpReserved, lpdwType, lpbData, lpcbData ); */
380         return ERROR_CALL_NOT_IMPLEMENTED;
381
382     case 0x001B:  /* RegSetValueEx */
383         stack32_pop( context ); /* hkey */
384         stack32_pop( context ); /* lpszValue */
385         stack32_pop( context ); /* dwReserved */
386         stack32_pop( context ); /* dwType */
387         stack32_pop( context ); /* lpbData */
388         stack32_pop( context ); /* cbData */
389         /* return RegSetValueExA( hkey, lpszValue, dwReserved, dwType, lpbData, cbData ); */
390         return ERROR_CALL_NOT_IMPLEMENTED;
391
392     case 0x001C:  /* RegFlushKey */
393         stack32_pop( context ); /* hkey */
394         /* return RegFlushKey( hkey ); */
395         return ERROR_CALL_NOT_IMPLEMENTED;
396
397     case 0x001D:  /* RegQueryInfoKey */
398         /* NOTE: This VxDCall takes only a subset of the parameters that the
399                  corresponding Win32 API call does. The implementation in Win95
400                  ADVAPI32 sets all output parameters not mentioned here to zero. */
401         stack32_pop( context ); /* hkey */
402         stack32_pop( context ); /* lpcSubKeys */
403         stack32_pop( context ); /* lpcchMaxSubKey */
404         stack32_pop( context ); /* lpcValues */
405         stack32_pop( context ); /* lpcchMaxValueName */
406         stack32_pop( context ); /* lpcchMaxValueData */
407         /* return RegQueryInfoKeyA( hkey, lpcSubKeys, lpcchMaxSubKey, lpcValues, lpcchMaxValueName, lpcchMaxValueData ); */
408         return ERROR_CALL_NOT_IMPLEMENTED;
409
410     case 0x001e: /* GetDemandPageInfo */
411     {
412          DWORD dinfo = (DWORD)stack32_pop( context );
413          DWORD flags = (DWORD)stack32_pop( context );
414
415          /* GetDemandPageInfo is supposed to fill out the struct at
416           * "dinfo" with various low-level memory management information.
417           * Apps are certainly not supposed to call this, although it's
418           * demoed and documented by Pietrek on pages 441-443 of "Windows
419           * 95 System Programming Secrets" if any program needs a real
420           * implementation of this.
421           */
422
423          FIXME("GetDemandPageInfo(%08x %08x): stub!\n", dinfo, flags);
424
425          return 0;
426     }
427
428     case 0x0021:  /* RegLoadKey */
429     {
430         HKEY    hkey       = (HKEY)  stack32_pop( context );
431         LPCSTR  lpszSubKey = (LPCSTR)stack32_pop( context );
432         LPCSTR  lpszFile   = (LPCSTR)stack32_pop( context );
433         FIXME("RegLoadKey(%p,%s,%s): stub\n",hkey, debugstr_a(lpszSubKey), debugstr_a(lpszFile));
434         return ERROR_CALL_NOT_IMPLEMENTED;
435     }
436
437     case 0x0022:  /* RegUnLoadKey */
438     {
439         HKEY    hkey       = (HKEY)  stack32_pop( context );
440         LPCSTR  lpszSubKey = (LPCSTR)stack32_pop( context );
441         FIXME("RegUnLoadKey(%p,%s): stub\n",hkey, debugstr_a(lpszSubKey));
442         return ERROR_CALL_NOT_IMPLEMENTED;
443     }
444
445     case 0x0023:  /* RegSaveKey */
446     {
447         HKEY    hkey       = (HKEY)  stack32_pop( context );
448         LPCSTR  lpszFile   = (LPCSTR)stack32_pop( context );
449         LPSECURITY_ATTRIBUTES sa = (LPSECURITY_ATTRIBUTES)stack32_pop( context );
450         FIXME("RegSaveKey(%p,%s,%p): stub\n",hkey, debugstr_a(lpszFile),sa);
451         return ERROR_CALL_NOT_IMPLEMENTED;
452     }
453
454 #if 0 /* Functions are not yet implemented in misc/registry.c */
455     case 0x0024:  /* RegRemapPreDefKey */
456     case 0x0026:  /* RegQueryMultipleValues */
457 #endif
458
459     case 0x0027:  /* RegReplaceKey */
460     {
461         HKEY    hkey       = (HKEY)  stack32_pop( context );
462         LPCSTR  lpszSubKey = (LPCSTR)stack32_pop( context );
463         LPCSTR  lpszNewFile= (LPCSTR)stack32_pop( context );
464         LPCSTR  lpszOldFile= (LPCSTR)stack32_pop( context );
465         FIXME("RegReplaceKey(%p,%s,%s,%s): stub\n", hkey, debugstr_a(lpszSubKey),
466               debugstr_a(lpszNewFile),debugstr_a(lpszOldFile));
467         return ERROR_CALL_NOT_IMPLEMENTED;
468     }
469
470     default:
471         if (LOWORD(service) < N_VMM_SERVICE)
472             FIXME( "Unimplemented service %s (%08x)\n",
473                    VMM_Service_Name[LOWORD(service)], service);
474         else
475             FIXME( "Unknown service %08x\n", service);
476         return 0xffffffff;  /* FIXME */
477     }
478 }