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

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

Include os2wrap.h instead of os2.h

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