source: trunk/src/kernel32/os2native.cpp@ 51

Last change on this file since 51 was 51, checked in by sandervl, 26 years ago

* empty log message *

File size: 11.6 KB
Line 
1/*
2 * Misc procedures
3 *
4 * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
5 * Copyright 1998 Knut St. Osmundsen
6 * Copyright 1998 Peter FitzSimmons
7 *
8 *
9 * Project Odin Software License can be found in LICENSE.TXT
10 *
11 */
12#define INCL_BASE
13#define INCL_DOSEXCEPTIONS
14#define INCL_DOSMEMMGR
15#define INCL_DOSPROCESS
16#include <os2.h>
17#include <stdlib.h>
18#include <stdio.h>
19#include <string.h>
20#ifdef __IBMC__
21#include <builtin.h>
22#endif
23#include <win32type.h>
24#include "misc.h"
25#include "exceptions.h"
26
27#define PAGE_NOACCESS 0x01
28#define PAGE_READONLY 0x02
29#define PAGE_READWRITE 0x04
30#define PAGE_WRITECOPY 0x08
31#define PAGE_EXECUTE 0x10
32#define PAGE_EXECUTE_READ 0x20
33#define PAGE_EXECUTE_READWRITE 0x40
34#define PAGE_EXECUTE_WRITECOPY 0x80
35#define PAGE_GUARD 0x100
36#define PAGE_NOCACHE 0x200
37#define MEM_COMMIT 0x1000
38#define MEM_RESERVE 0x2000
39#define MEM_DECOMMIT 0x4000
40#define MEM_RELEASE 0x8000
41#define MEM_TOP_DOWN 0x100000 //Ignored
42
43extern ULONG flAllocMem; /* Tue 03.03.1998: knut */
44
45#ifdef DEBUG
46ULONG commit = 0, reserve = 0;
47#endif
48
49//******************************************************************************
50//******************************************************************************
51LPVOID WIN32API VirtualAlloc(LPVOID lpvAddress, DWORD cbSize, DWORD fdwAllocationType,
52 DWORD fdwProtect)
53{
54 PVOID Address = lpvAddress;
55 ULONG flag = 0;
56 APIRET rc;
57
58 dprintf(("VirtualAlloc at %X; %d bytes, fAlloc %d, fProtect %d\n", (int)lpvAddress, cbSize, fdwAllocationType, fdwProtect));
59
60 if(fdwAllocationType & MEM_COMMIT) {
61 dprintf(("VirtualAlloc: commit\n"));
62 flag = PAG_COMMIT;
63 }
64 if(fdwProtect & PAGE_READONLY) flag |= PAG_READ;
65 if(fdwProtect & PAGE_READWRITE) flag |= (PAG_READ | PAG_WRITE);
66 if(fdwProtect & PAGE_EXECUTE_READ) flag |= PAG_EXECUTE;
67 if(fdwProtect & PAGE_GUARD) flag |= PAG_GUARD;
68
69 //just do this if other options are used
70 if(!(flag & (PAG_READ | PAG_WRITE | PAG_EXECUTE)) || flag == 0)
71 flag |= PAG_READ | PAG_WRITE;
72
73 if(fdwAllocationType & MEM_COMMIT && lpvAddress != NULL) {
74 Address = lpvAddress;
75
76 rc = DosSetMem(lpvAddress, cbSize, flag);
77 //might try to commit larger part with same base address
78 if(rc == ERROR_ACCESS_DENIED && cbSize > 4096 ) {//knut: AND more than one page
79 char *newbase = (char *)lpvAddress + ((cbSize-1) & 0xFFFFF000); //knut: lets not start after the last page!
80 ULONG size, os2flags;
81
82 while(newbase >= (char *)lpvAddress) { //knut: should check first page to!!
83 size = 4096;
84 os2flags = 0;
85 rc = DosQueryMem(newbase, &size, &os2flags);
86 if(rc) break;
87 if(os2flags & PAG_COMMIT) {
88 newbase += 4096;
89 break;
90 }
91 newbase -= 4096;
92 }
93 if(rc == 0) {
94 //In case it wants to commit bytes that fall into the last
95 //page of the previous commit command
96 if(cbSize > ((int)newbase - (int)lpvAddress)) {
97 rc = DosSetMem(newbase, cbSize - ((int)newbase - (int)lpvAddress), flag);
98 }
99 }
100 else return(NULL);
101 }
102 else
103 {
104 if(rc == ERROR_INVALID_ADDRESS)
105 {
106 rc = DosAllocMem(&Address, cbSize, flag | flAllocMem );
107 }
108 else dprintf(("Unexpected DosSetMem error %x", rc));
109 }
110 }
111 else {
112 /*knut: flAllocMem */
113 rc = DosAllocMem(&Address, cbSize, flag | flAllocMem );
114 }
115
116//TODO: Set last error in case rc != 0
117 if(rc) {
118 dprintf(("DosSetMem returned %d\n", rc));
119 return(NULL);
120 }
121
122 dprintf(("VirtualAlloc returned %X\n", Address));
123 return(Address);
124}
125//******************************************************************************
126//******************************************************************************
127BOOL WIN32API VirtualFree(LPVOID lpvAddress, DWORD cbSize, DWORD FreeType)
128{
129 APIRET rc;
130
131 dprintf(("VirtualFree at %d; %d bytes, freetype %d\n", (int)lpvAddress, cbSize, FreeType));
132
133 if(FreeType & MEM_DECOMMIT) {
134 rc = DosSetMem(lpvAddress, cbSize, PAG_DECOMMIT);
135 }
136 else rc = DosFreeMem(lpvAddress); //MEM_RELEASE, cbSize == 0 (or should be)
137
138//TODO: Set last error in case rc != 0
139 if(rc) return(FALSE);
140 return(TRUE);
141}
142//******************************************************************************
143//LPVOID lpvAddress; /* address of region of committed pages */
144//DWORD cbSize; /* size of the region */
145//DWORD fdwNewProtect; /* desired access protection */
146//PDWORD pfdwOldProtect; /* address of variable to get old protection */
147//TODO: Not 100% complete
148//TODO: SetLastError on failure
149//******************************************************************************
150BOOL WIN32API VirtualProtect(LPVOID lpvAddress, DWORD cbSize, DWORD fdwNewProtect,
151 DWORD *pfdwOldProtect)
152{
153 APIRET rc;
154 ULONG pageFlags = 0;
155 int npages;
156
157 dprintf(("VirtualProtect %X; %d bytes, new flags %X (%X)\n", (int)lpvAddress, cbSize, fdwNewProtect, pfdwOldProtect));
158// _interrupt(3);
159 if(pfdwOldProtect == NULL)
160 return(FALSE);
161
162 rc = DosQueryMem(lpvAddress, &cbSize, &pageFlags);
163 if(rc) {
164 dprintf(("DosQueryMem returned %d\n", rc));
165 return(FALSE);
166 }
167 dprintf(("Old memory flags %X\n", pageFlags));
168 *pfdwOldProtect = 0;
169 if(pageFlags & PAG_READ && !(pageFlags & PAG_WRITE))
170 *pfdwOldProtect |= PAGE_READONLY;
171 if(pageFlags & (PAG_READ | PAG_WRITE))
172 *pfdwOldProtect |= PAGE_READWRITE;
173 if(pageFlags & PAG_EXECUTE)
174 *pfdwOldProtect |= PAGE_EXECUTE_READ;
175 if(pageFlags & PAG_GUARD)
176 *pfdwOldProtect |= PAGE_GUARD;
177 pageFlags = 0;
178
179 if(fdwNewProtect & PAGE_READONLY) pageFlags |= PAG_READ;
180 if(fdwNewProtect & PAGE_READWRITE) pageFlags |= (PAG_READ | PAG_WRITE);
181 if(fdwNewProtect & PAGE_EXECUTE_READ) pageFlags |= PAG_EXECUTE;
182 if(fdwNewProtect & PAGE_EXECUTE_READWRITE)
183 pageFlags |= (PAG_EXECUTE | PAG_WRITE);
184 if(fdwNewProtect & PAGE_GUARD) pageFlags |= PAG_GUARD;
185//Not supported in OS/2??
186// if(fdwNewProtect & PAGE_NOACCESS)
187
188 dprintf(("New memory flags %X\n", pageFlags));
189 if(pageFlags == 0) {
190 dprintf(("pageFlags == 0\n"));
191 return(TRUE); //nothing to do
192 }
193 npages = ((int)lpvAddress + cbSize >> 12) - ((int)lpvAddress >> 12) + 1;
194
195 lpvAddress = (LPVOID)((int)lpvAddress & ~0xFFF);
196 cbSize = npages*4096;
197 dprintf(("lpvAddress = %X, cbSize = %d\n", lpvAddress, cbSize));
198
199 rc = DosSetMem(lpvAddress, cbSize, pageFlags);
200 if(rc) {
201 dprintf(("DosSetMem returned %d\n", rc));
202 return(FALSE);
203 }
204 return(TRUE);
205}
206//******************************************************************************
207#define PMEMORY_BASIC_INFORMATION void *
208//******************************************************************************
209DWORD WIN32API VirtualQuery(LPVOID lpvAddress, PMEMORY_BASIC_INFORMATION pmbiBuffer,
210 DWORD cbLength)
211{
212 dprintf(("VirtualQuery - stub\n"));
213 return(0);
214}
215//******************************************************************************
216//******************************************************************************
217#ifdef __WATCOMC__ /*PLF Sat 97-06-21 17:12:36*/
218 extern void interrupt3( void );
219 #pragma aux interrupt3= \
220 "int 3"
221#endif
222void WIN32API DebugBreak()
223{
224 dprintf(("DebugBreak\n"));
225#ifdef __WATCOMC__
226 interrupt3();
227#else
228 _interrupt(3);
229#endif
230}
231//******************************************************************************
232//TODO: Implement this??
233//******************************************************************************
234BOOL WIN32API GetThreadContext(HANDLE hThread, LPWINCONTEXT lpContext)
235{
236 dprintf(("GetThreadContext NOT IMPLEMENTED!! (TRUE)\n"));
237 memset(lpContext, 0, sizeof(WINCONTEXT));
238 return TRUE;
239}
240//******************************************************************************
241//TODO: Implement this??
242//******************************************************************************
243BOOL WIN32API SetThreadContext(HANDLE hThread, LPWINCONTEXT lpContext)
244{
245 dprintf(("SetThreadContext NOT IMPLEMENTED!!\n"));
246
247 return FALSE;
248}
249//******************************************************************************
250//******************************************************************************
251BOOL WIN32API VirtualLock( LPVOID lpAddress, DWORD dwSize )
252{
253 dprintf(("VirtualLock at %d; %d bytes - stub (TRUE)\n", (int)lpAddress, dwSize));
254 return TRUE;
255}
256
257//******************************************************************************
258BOOL WIN32API VirtualUnlock( LPVOID lpAddress, DWORD dwSize )
259{
260 dprintf(("VirtualUnlock at %d; %d bytes - stub (TRUE)\n", (int)lpAddress, dwSize));
261 return TRUE;
262}
263
264
265/*****************************************************************************
266 * Name : BOOL VirtualProtectEx
267 * Purpose : The VirtualProtectEx function changes the access protection on
268 * a region of committed pages in the virtual address space of a specified
269 * process. Note that this function differs from VirtualProtect,
270 * which changes the access protection on the calling process only.
271 * Parameters: HANDLE hProcess handle of process
272 * LPVOID lpvAddress address of region of committed pages
273 * DWORD cbSize size of region
274 * DWORD fdwNewProtect desired access protection
275 * PDWORD pfdwOldProtect address of variable to get old protection
276 * Variables :
277 * Result : size of target buffer
278 * Remark :
279 * Status : UNTESTED STUB
280 *
281 * Author : Patrick Haller [Mon, 1998/06/15 08:00]
282 *****************************************************************************/
283
284BOOL WIN32API VirtualProtectEx(HANDLE hProcess,
285 LPVOID lpvAddress,
286 DWORD cbSize,
287 DWORD fdwNewProtect,
288 LPDWORD pfdwOldProtect)
289{
290 dprintf(("KERNEL32: VirtualProtectEx(%08x,%08xh,%08xh,%08xh,%08xh) not implemented.\n",
291 hProcess,
292 lpvAddress,
293 cbSize,
294 fdwNewProtect,
295 pfdwOldProtect));
296
297 return (FALSE);
298}
299
300
301/*****************************************************************************
302 * Name : DWORD VirtualQueryEx
303 * Purpose : The VirtualQueryEx function provides information about a range
304 * of pages within the virtual address space of a specified process.
305 * Parameters: HANDLE hProcess handle of process
306 * LPCVOID lpvAddress address of region
307 * PMEMORY_BASIC_INFORMATION pmbiBuffer address of information buffer
308 * DWORD cbLength size of buffer
309 * Variables :
310 * Result : number of bytes returned in buffer
311 * Remark :
312 * Status : UNTESTED STUB
313 *
314 * Author : Patrick Haller [Mon, 1998/06/15 08:00]
315 *****************************************************************************/
316
317DWORD WIN32API VirtualQueryEx(HANDLE hProcess,
318 LPVOID lpvAddress,
319 PMEMORY_BASIC_INFORMATION pmbiBuffer,
320 DWORD cbLength)
321{
322 dprintf(("KERNEL32: VirtualQueryEx(%08x,%08xh,%08xh,%08xh) not implemented.\n",
323 hProcess,
324 lpvAddress,
325 pmbiBuffer,
326 cbLength));
327
328 return (0);
329}
330
Note: See TracBrowser for help on using the repository browser.