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

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

Activated Win32 TIB code and fixes some other things (see ChangeLog)

File size: 11.7 KB
Line 
1/* $Id: os2native.cpp,v 1.4 1999-06-20 12:46:09 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
111 if(rc) dprintf(("Unexpected DosSetMem error %x", rc));
112 }
113 }
114 else {
115 /*knut: flAllocMem */
116 rc = DosAllocMem(&Address, cbSize, flag | flAllocMem );
117 }
118
119//TODO: Set last error in case rc != 0
120 if(rc) {
121 dprintf(("DosSetMem returned %d\n", rc));
122 return(NULL);
123 }
124
125 dprintf(("VirtualAlloc returned %X\n", Address));
126 return(Address);
127}
128//******************************************************************************
129//******************************************************************************
130BOOL WIN32API VirtualFree(LPVOID lpvAddress, DWORD cbSize, DWORD FreeType)
131{
132 APIRET rc;
133
134 dprintf(("VirtualFree at %d; %d bytes, freetype %d\n", (int)lpvAddress, cbSize, FreeType));
135
136 if(FreeType & MEM_DECOMMIT) {
137 rc = DosSetMem(lpvAddress, cbSize, PAG_DECOMMIT);
138 }
139 else rc = DosFreeMem(lpvAddress); //MEM_RELEASE, cbSize == 0 (or should be)
140
141//TODO: Set last error in case rc != 0
142 if(rc) return(FALSE);
143 return(TRUE);
144}
145//******************************************************************************
146//LPVOID lpvAddress; /* address of region of committed pages */
147//DWORD cbSize; /* size of the region */
148//DWORD fdwNewProtect; /* desired access protection */
149//PDWORD pfdwOldProtect; /* address of variable to get old protection */
150//TODO: Not 100% complete
151//TODO: SetLastError on failure
152//******************************************************************************
153BOOL WIN32API VirtualProtect(LPVOID lpvAddress, DWORD cbSize, DWORD fdwNewProtect,
154 DWORD *pfdwOldProtect)
155{
156 APIRET rc;
157 ULONG pageFlags = 0;
158 int npages;
159
160 dprintf(("VirtualProtect %X; %d bytes, new flags %X (%X)\n", (int)lpvAddress, cbSize, fdwNewProtect, pfdwOldProtect));
161// _interrupt(3);
162 if(pfdwOldProtect == NULL)
163 return(FALSE);
164
165 rc = DosQueryMem(lpvAddress, &cbSize, &pageFlags);
166 if(rc) {
167 dprintf(("DosQueryMem returned %d\n", rc));
168 return(FALSE);
169 }
170 dprintf(("Old memory flags %X\n", pageFlags));
171 *pfdwOldProtect = 0;
172 if(pageFlags & PAG_READ && !(pageFlags & PAG_WRITE))
173 *pfdwOldProtect |= PAGE_READONLY;
174 if(pageFlags & (PAG_READ | PAG_WRITE))
175 *pfdwOldProtect |= PAGE_READWRITE;
176 if(pageFlags & PAG_EXECUTE)
177 *pfdwOldProtect |= PAGE_EXECUTE_READ;
178 if(pageFlags & PAG_GUARD)
179 *pfdwOldProtect |= PAGE_GUARD;
180 pageFlags = 0;
181
182 if(fdwNewProtect & PAGE_READONLY) pageFlags |= PAG_READ;
183 if(fdwNewProtect & PAGE_READWRITE) pageFlags |= (PAG_READ | PAG_WRITE);
184 if(fdwNewProtect & PAGE_EXECUTE_READ) pageFlags |= PAG_EXECUTE;
185 if(fdwNewProtect & PAGE_EXECUTE_READWRITE)
186 pageFlags |= (PAG_EXECUTE | PAG_WRITE);
187 if(fdwNewProtect & PAGE_GUARD) pageFlags |= PAG_GUARD;
188//Not supported in OS/2??
189// if(fdwNewProtect & PAGE_NOACCESS)
190
191 dprintf(("New memory flags %X\n", pageFlags));
192 if(pageFlags == 0) {
193 dprintf(("pageFlags == 0\n"));
194 return(TRUE); //nothing to do
195 }
196 npages = ((int)lpvAddress + cbSize >> 12) - ((int)lpvAddress >> 12) + 1;
197
198 lpvAddress = (LPVOID)((int)lpvAddress & ~0xFFF);
199 cbSize = npages*4096;
200 dprintf(("lpvAddress = %X, cbSize = %d\n", lpvAddress, cbSize));
201
202 rc = DosSetMem(lpvAddress, cbSize, pageFlags);
203 if(rc) {
204 dprintf(("DosSetMem returned %d\n", rc));
205 return(FALSE);
206 }
207 return(TRUE);
208}
209//******************************************************************************
210#define PMEMORY_BASIC_INFORMATION void *
211//******************************************************************************
212DWORD WIN32API VirtualQuery(LPVOID lpvAddress, PMEMORY_BASIC_INFORMATION pmbiBuffer,
213 DWORD cbLength)
214{
215 dprintf(("VirtualQuery - stub\n"));
216 return(0);
217}
218//******************************************************************************
219//******************************************************************************
220#ifdef __WATCOMC__ /*PLF Sat 97-06-21 17:12:36*/
221 extern void interrupt3( void );
222 #pragma aux interrupt3= \
223 "int 3"
224#endif
225void WIN32API DebugBreak()
226{
227 dprintf(("DebugBreak\n"));
228#ifdef __WATCOMC__
229 interrupt3();
230#else
231 _interrupt(3);
232#endif
233}
234//******************************************************************************
235//TODO: Implement this??
236//******************************************************************************
237BOOL WIN32API GetThreadContext(HANDLE hThread, LPWINCONTEXT lpContext)
238{
239 dprintf(("GetThreadContext NOT IMPLEMENTED!! (TRUE)\n"));
240 memset(lpContext, 0, sizeof(WINCONTEXT));
241 return TRUE;
242}
243//******************************************************************************
244//TODO: Implement this??
245//******************************************************************************
246BOOL WIN32API SetThreadContext(HANDLE hThread, LPWINCONTEXT lpContext)
247{
248 dprintf(("SetThreadContext NOT IMPLEMENTED!!\n"));
249
250 return FALSE;
251}
252//******************************************************************************
253//******************************************************************************
254BOOL WIN32API VirtualLock( LPVOID lpAddress, DWORD dwSize )
255{
256 dprintf(("VirtualLock at %d; %d bytes - stub (TRUE)\n", (int)lpAddress, dwSize));
257 return TRUE;
258}
259
260//******************************************************************************
261BOOL WIN32API VirtualUnlock( LPVOID lpAddress, DWORD dwSize )
262{
263 dprintf(("VirtualUnlock at %d; %d bytes - stub (TRUE)\n", (int)lpAddress, dwSize));
264 return TRUE;
265}
266
267
268/*****************************************************************************
269 * Name : BOOL VirtualProtectEx
270 * Purpose : The VirtualProtectEx function changes the access protection on
271 * a region of committed pages in the virtual address space of a specified
272 * process. Note that this function differs from VirtualProtect,
273 * which changes the access protection on the calling process only.
274 * Parameters: HANDLE hProcess handle of process
275 * LPVOID lpvAddress address of region of committed pages
276 * DWORD cbSize size of region
277 * DWORD fdwNewProtect desired access protection
278 * PDWORD pfdwOldProtect address of variable to get old protection
279 * Variables :
280 * Result : size of target buffer
281 * Remark :
282 * Status : UNTESTED STUB
283 *
284 * Author : Patrick Haller [Mon, 1998/06/15 08:00]
285 *****************************************************************************/
286
287BOOL WIN32API VirtualProtectEx(HANDLE hProcess,
288 LPVOID lpvAddress,
289 DWORD cbSize,
290 DWORD fdwNewProtect,
291 LPDWORD pfdwOldProtect)
292{
293 dprintf(("KERNEL32: VirtualProtectEx(%08x,%08xh,%08xh,%08xh,%08xh) not implemented.\n",
294 hProcess,
295 lpvAddress,
296 cbSize,
297 fdwNewProtect,
298 pfdwOldProtect));
299
300 return (FALSE);
301}
302
303
304/*****************************************************************************
305 * Name : DWORD VirtualQueryEx
306 * Purpose : The VirtualQueryEx function provides information about a range
307 * of pages within the virtual address space of a specified process.
308 * Parameters: HANDLE hProcess handle of process
309 * LPCVOID lpvAddress address of region
310 * PMEMORY_BASIC_INFORMATION pmbiBuffer address of information buffer
311 * DWORD cbLength size of buffer
312 * Variables :
313 * Result : number of bytes returned in buffer
314 * Remark :
315 * Status : UNTESTED STUB
316 *
317 * Author : Patrick Haller [Mon, 1998/06/15 08:00]
318 *****************************************************************************/
319
320DWORD WIN32API VirtualQueryEx(HANDLE hProcess,
321 LPVOID lpvAddress,
322 PMEMORY_BASIC_INFORMATION pmbiBuffer,
323 DWORD cbLength)
324{
325 dprintf(("KERNEL32: VirtualQueryEx(%08x,%08xh,%08xh,%08xh) not implemented.\n",
326 hProcess,
327 lpvAddress,
328 pmbiBuffer,
329 cbLength));
330
331 return (0);
332}
333
Note: See TracBrowser for help on using the repository browser.