source: trunk/src/win32k/dev32/d32init.c@ 4181

Last change on this file since 4181 was 4181, checked in by bird, 25 years ago

Added support for comports 3 and 4.

File size: 60.7 KB
Line 
1/* $Id: d32init.c,v 1.21 2000-09-04 02:04:20 bird Exp $
2 *
3 * d32init.c - 32-bits init routines.
4 *
5 * Copyright (c) 1998-1999 knut st. osmundsen
6 *
7 * Project Odin Software License can be found in LICENSE.TXT
8 *
9 */
10
11/*******************************************************************************
12* Defined Constants *
13*******************************************************************************/
14#define MAXSIZE_PROLOG 0x18 /* Note that this must be synced with */
15 /* the one used in calltab.asm. */
16#if 0 /* Enable this to have extra debug logging. */
17 #define kprintf2(a) kprintf
18#else
19 #define kprintf2(a) (void)0
20#endif
21
22#define INCL_DOSERRORS
23#define INCL_NOPMAPI
24#define LDR_INCL_INITONLY
25#define INCL_OS2KRNL_ALL
26
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#include <os2.h>
31
32#include <string.h>
33
34#include "devSegDf.h"
35#include "OS2Krnl.h"
36#include "options.h"
37#include "dev1632.h"
38#include "dev32.h"
39#include "dev32hlp.h"
40#include "probkrnl.h"
41#include "log.h"
42#include "asmutils.h"
43#include "malloc.h"
44#include "ldr.h"
45#include "ldrCalls.h"
46#include "macros.h"
47#include "errors.h"
48
49#ifdef R3TST
50 #include "test.h"
51#endif
52
53
54/*******************************************************************************
55* Global Variables *
56*******************************************************************************/
57#ifdef DEBUG
58static char * apszPE[] = {"FLAGS_PE_NOT", "FLAGS_PE_PE2LX", "FLAGS_PE_PE", "FLAGS_PE_MIXED", "!invalid!"};
59static char * apszInfoLevel[] = {"INFOLEVEL_QUIET", "INFOLEVEL_ERROR", "INFOLEVEL_WARNING", "INFOLEVEL_INFO", "INFOLEVEL_INFOALL", "!invalid!"};
60#endif
61PMTE pKrnlMTE = NULL;
62PSMTE pKrnlSMTE = NULL;
63POTE pKrnlOTE = NULL;
64
65
66/*******************************************************************************
67* Internal Functions *
68*******************************************************************************/
69 ULONG readnum(const char *pszNum);
70_Inline int ModR_M_32bit(char bModRM);
71_Inline int ModR_M_16bit(char bModRM);
72int interpretFunctionProlog32(char *pach, BOOL fOverload);
73int interpretFunctionProlog16(char *pach, BOOL fOverload);
74int importTabInit(void);
75#ifdef R3TST
76PMTE GetOS2KrnlMTETst(void);
77void R3TstFixImportTab(void);
78#endif
79PSZ SECCALL nopSecPathFromSFN(SFN hFile);
80
81
82
83/* externs located in 16-bit data segement in ProbKrnl.c */
84extern ULONG _TKSSBase16;
85extern USHORT _R0FlatCS16;
86extern USHORT _R0FlatDS16;
87
88/* extern(s) located in calltab.asm */
89extern char callTab[NBR_OF_KRNLIMPORTS][MAXSIZE_PROLOG];
90
91/* extern(s) located in mytkExecPgm.asm */
92extern char mytkExecPgm;
93extern char mytkStartProcess;
94
95
96/**
97 * Ring-0, 32-bit, init function.
98 * @returns Status word.
99 * @param pRpInit Pointer init request packet.
100 * @sketch Set TKSSBase32.
101 * Set default parameters.
102 * Parse command line options.
103 * Show (kprint) configuration.
104 * Init heap.
105 * Init ldr.
106 * Init procs. (overloaded ldr procedures)
107 * @status completely implemented.
108 * @author knut st. osmundsen
109 */
110USHORT _loadds _Far32 _Pascal R0Init32(RP32INIT *pRpInit)
111{
112 char * pszTmp2;
113 char * pszTmp;
114 ULONG ul;
115 APIRET rc;
116 LOCKHANDLE lockhandle;
117
118 pulTKSSBase32 = (PULONG)_TKSSBase16;
119
120 /*---------------------*/
121 /* commandline options */
122 /*---------------------*/
123 kprintf(("Options start\n"));
124 pszTmp = strpbrk(pRpInit->InitArgs, "-/");
125 while (pszTmp != NULL)
126 {
127 int cch;
128 pszTmp++; //skip [-/]
129 cch = strlen(pszTmp);
130 switch (*pszTmp)
131 {
132 case 'c':
133 case 'C': /* -C[1|2|3|4] or -Com:[1|2|3|4] - com-port no, def:-C2 */
134 pszTmp2 = strpbrk(pszTmp, ":=/- ");
135 if (pszTmp2 != NULL && (*pszTmp2 == ':' || *pszTmp2 == '='))
136 pszTmp2++;
137 else
138 pszTmp2 = pszTmp + 1;
139 ul = readnum(pszTmp2);
140 switch (ul)
141 {
142 case 1: options.usCom = OUTPUT_COM1; break;
143 case 2: options.usCom = OUTPUT_COM2; break;
144 case 3: options.usCom = OUTPUT_COM3; break;
145 case 4: options.usCom = OUTPUT_COM4; break;
146 }
147 break;
148
149 case 'e':
150 case 'E':/* ELF */
151 pszTmp2 = strpbrk(pszTmp, ":=/- ");
152 if (pszTmp2 != NULL
153 && (pszTmp2[1] == 'N' ||pszTmp2[1] == 'n' || pszTmp2[1] == 'D' || pszTmp2[1] == 'd')
154 )
155 options.fElf = FALSE;
156 else
157 options.fElf = TRUE;
158 break;
159
160 case 'h':
161 case 'H': /* Heap options */
162 pszTmp2 = strpbrk(pszTmp, ":=/- ");
163 if (pszTmp2 != NULL && (*pszTmp2 == ':' || *pszTmp2 == '='))
164 {
165 ul = readnum(pszTmp2 + 1);
166 if (ul > 0x1000UL && ul < 0x2000000UL) /* 4KB < ul < 32MB */
167 {
168 if (strnicmp(pszTmp, "heapm", 5) == 0)
169 options.cbSwpHeapMax = ul;
170 else
171 options.cbSwpHeapInit = ul;
172 }
173 }
174 break;
175
176 case 'j':
177 case 'J': /* -Java:<Yes|No> */
178 pszTmp2 = strpbrk(pszTmp, ":=/- ");
179 options.fJava =
180 pszTmp2 != NULL
181 && (int)(pszTmp2-pszTmp) < cch-1
182 && (*pszTmp2 == ':' || *pszTmp2 == '=')
183 && (pszTmp2[1] == 'Y' || pszTmp2[1] == 'y');
184 break;
185
186 case 'l':
187 case 'L': /* -L[..]<:|=| >[<Y..|E..| > | <N..|D..>] */
188 pszTmp2 = strpbrk(pszTmp, ":=/- ");
189 if (pszTmp2 != NULL
190 && (pszTmp2[1] == 'Y' ||pszTmp2[1] == 'y' || pszTmp2[1] == 'E' || pszTmp2[1] == 'e')
191 )
192 options.fLogging = TRUE;
193 else
194 options.fLogging = FALSE;
195 break;
196
197 case 'n':
198 case 'N': /* NoLoader */
199 options.fNoLoader = TRUE;
200 break;
201
202 case 'p':
203 case 'P': /* PE */
204 pszTmp2 = strpbrk(pszTmp, ":=/- ");
205 if (pszTmp2 != NULL && (*pszTmp2 == ':' || *pszTmp2 == '='))
206 {
207 pszTmp2++;
208 if (strnicmp(pszTmp2, "pe2lx", 5) == 0)
209 options.fPE = FLAGS_PE_PE2LX;
210 else if (strnicmp(pszTmp2, "pe", 2) == 0)
211 options.fPE = FLAGS_PE_PE;
212 else if (strnicmp(pszTmp2, "mixed", 2) == 0)
213 options.fPE = FLAGS_PE_MIXED;
214 else if (strnicmp(pszTmp2, "not", 2) == 0)
215 options.fPE = FLAGS_PE_NOT;
216 else
217 kprintf(("R0Init32: invalid parameter -PE:...\n"));
218 }
219 else
220 kprintf(("R0Init32: invalid parameter -PE...\n"));
221 break;
222
223 case 'q':
224 case 'Q': /* quiet initialization */
225 options.fQuiet = TRUE;
226 break;
227
228 case 'r':
229 case 'R': /* ResHeap options or REXX option */
230 pszTmp2 = strpbrk(pszTmp, ":=/- ");
231 if ( (pszTmp[1] == 'E' || pszTmp[1] == 'e')
232 && (pszTmp[2] == 'X' || pszTmp[2] == 'x'))
233 { /* REXX */
234 options.fREXXScript =
235 pszTmp2 != NULL
236 && (int)(pszTmp2-pszTmp) < cch-1
237 && (*pszTmp2 == ':' || *pszTmp2 == '=')
238 && (pszTmp2[1] == 'Y' || pszTmp2[1] == 'y');
239 }
240 else
241 { /* ResHeap options */
242 if (pszTmp2 != NULL && (*pszTmp2 == ':' || *pszTmp2 == '='))
243 {
244 ul = readnum(pszTmp2 + 1);
245 if (ul > 0x1000UL && ul < 0x700000UL) /* 4KB < ul < 7MB */
246 {
247 if (strnicmp(pszTmp, "resheapm", 8) == 0)
248 options.cbResHeapMax = ul;
249 else
250 options.cbResHeapInit = ul;
251 }
252 }
253 }
254 break;
255
256 case 's':
257 case 'S': /* Sym:<filename> or Script:<Yes|No> or Smp */
258 /* SMP kernel */
259 pszTmp2 = strpbrk(pszTmp, ":=/- ");
260 if (pszTmp[1] == 'c' || pszTmp[1] == 'C')
261 {
262 options.fUNIXScript =
263 pszTmp2 != NULL
264 && (int)(pszTmp2-pszTmp) < cch-1
265 && (*pszTmp2 == ':' || *pszTmp2 == '=')
266 && (pszTmp2[1] == 'Y' || pszTmp2[1] == 'y');
267 }
268 break;
269
270 case 'v':
271 case 'V': /* verbose initialization */
272 options.fQuiet = FALSE;
273 break;
274
275 case 'w':
276 case 'W': /* ModuleBase info level; -W<n> or -Warning:<n> */
277 if (pszTmp[1] >= '0' && pszTmp[1] <= '4')
278 options.ulInfoLevel = pszTmp[1] - '0';
279 else
280 {
281 pszTmp2 = strpbrk(pszTmp, ":=/- ");
282 if (pszTmp2 != NULL && (*pszTmp2 == ':' || *pszTmp2 == '='))
283 pszTmp2++;
284 else
285 pszTmp2 = pszTmp + 1;
286
287 if (*pszTmp2 >= '0' && *pszTmp2 <= '4')
288 options.ulInfoLevel = *pszTmp2 - '0';
289 }
290 break;
291
292 }
293 pszTmp = strpbrk(pszTmp, "-/");
294 }
295
296 /* heap min/max corrections */
297 if (options.cbSwpHeapInit > options.cbSwpHeapMax)
298 options.cbSwpHeapMax = options.cbSwpHeapInit;
299 if (options.cbResHeapInit > options.cbResHeapMax)
300 options.cbResHeapMax = options.cbResHeapInit;
301
302 /* Log option summary */
303 #ifdef DEBUG
304 kprintf(("Options - Summary - Start\n"));
305 if (options.fQuiet)
306 kprintf(("\tQuiet init\n"));
307 else
308 kprintf(("\tVerbose init\n"));
309
310 if (options.fLogging)
311 kprintf(("\tlogging enabled\n"));
312 else
313 kprintf(("\tlogging disabled\n"));
314 kprintf(("\tCom port no.%03xh\n", options.usCom));
315
316 kprintf(("\tKernel: v%d.%d build %d type ",
317 options.usVerMajor,
318 options.usVerMinor,
319 options.ulBuild));
320 if (options.fKernel & KF_SMP)
321 kprintf(("SMP "));
322 else if (options.fKernel & KF_W4)
323 kprintf(("W4 "));
324 else
325 kprintf(("UNI "));
326 if (options.fKernel & KF_DEBUG)
327 kprintf(("DEBUG\n"));
328 else
329 kprintf(("\n"));
330
331 kprintf(("\tfPE=%d (%s)\n", options.fPE, apszPE[MIN(options.fPE, 5)]));
332 kprintf(("\tulInfoLevel=%d (%s)\n", options.ulInfoLevel, apszInfoLevel[MIN(options.ulInfoLevel, 5)]));
333 kprintf(("\tfElf=%d\n", options.fElf));
334 kprintf(("\tfUNIXScript=%d\n", options.fUNIXScript));
335 kprintf(("\tfREXXScript=%d\n", options.fREXXScript));
336 kprintf(("\tfJAVA=%d\n", options.fJava));
337 kprintf(("\tfNoLoader=%d\n", options.fNoLoader));
338 kprintf(("\tcbSwpHeapInit=0x%08x cbSwpHeapMax=0x%08x\n",
339 options.cbSwpHeapInit, options.cbSwpHeapMax));
340 kprintf(("\tcbResHeapInit=0x%08x cbResHeapMax=0x%08x\n",
341 options.cbResHeapInit, options.cbResHeapMax));
342 kprintf(("Options - Summary - End\n"));
343 #endif /* debug */
344 /* end option summary */
345
346
347 /*
348 * init sub-parts
349 */
350 /* heap */
351 if (heapInit(options.cbResHeapInit, options.cbResHeapMax,
352 options.cbSwpHeapInit, options.cbSwpHeapMax) != NO_ERROR)
353 return STATUS_DONE | STERR | ERROR_I24_QUIET_INIT_FAIL;
354
355 /* loader */
356 if (!options.fNoLoader)
357 if (ldrInit() != NO_ERROR)
358 return STATUS_DONE | STERR | ERROR_I24_QUIET_INIT_FAIL;
359
360 /* functionoverrides */
361 if (!options.fNoLoader)
362 if (importTabInit() != NO_ERROR)
363 return STATUS_DONE | STERR | ERROR_I24_QUIET_INIT_FAIL;
364
365 /*
366 * Lock the 32-bit objects/segments and 16-bit datasegment in memory
367 */
368 /* 32-bit code segment */
369 memset(SSToDS(&lockhandle), 0, sizeof(lockhandle));
370 rc = D32Hlp_VMLock2(&CODE32START,
371 ((unsigned)&CODE32END & ~0xFFF) - (unsigned)&CODE32START, /* Round down so we don't overlap with the next request. */
372 VMDHL_LONG,
373 SSToDS(&lockhandle));
374 if (rc != NO_ERROR)
375 kprintf(("code segment lock failed with with rc=%d\n", rc));
376
377 /* 32-bit data segment */
378 memset(SSToDS(&lockhandle), 0, sizeof(lockhandle));
379 rc = D32Hlp_VMLock2(callTab,
380 &CONST32_ROEND - (char*)callTab,
381 VMDHL_LONG | VMDHL_WRITE,
382 SSToDS(&lockhandle));
383 if (rc != NO_ERROR)
384 kprintf(("data segment lock failed with with rc=%d\n", rc));
385
386 /* 16-bit data segment - is this necessary? */
387 memset(SSToDS(&lockhandle), 0, sizeof(lockhandle));
388 rc = D32Hlp_VMLock2(&DATA16START,
389 &DATA16END - &DATA16START,
390 VMDHL_LONG | VMDHL_WRITE,
391 SSToDS(&lockhandle));
392 if (rc != NO_ERROR)
393 kprintf(("16-bit data segment lock failed with with rc=%d\n", rc));
394
395 return STATUS_DONE;
396}
397
398
399/**
400 * Reads a number (unsigned long integer) for a string.
401 * @returns number read, ~0UL on error / no number read.
402 * @param pszNum Pointer to the string containing the number.
403 * @status competely implemented.
404 * @author knut st. osmundsen
405 */
406ULONG readnum(const char *pszNum)
407{
408 ULONG ulRet = 0;
409 ULONG ulBase = 10;
410 int i = 0;
411
412 /* determin ulBase */
413 if (*pszNum == '0')
414 if (pszNum[1] == 'x' || pszNum[1] == 'X')
415 {
416 ulBase = 16;
417 pszNum += 2;
418 }
419 else
420 {
421 ulBase = 8;
422 i = 1;
423 }
424
425 /* read digits */
426 while (ulBase == 16 ? (pszNum[i] >= '0' && pszNum[i] <= '9') || (pszNum[i] >= 'a' && pszNum[i] <= 'f') || (pszNum[i] >= 'A' && pszNum[i] <= 'F')
427 : (pszNum[i] >= '0' && pszNum[i] <= (ulBase == 10 ? '9' : '7'))
428 )
429 {
430 ulRet *= ulBase;
431 if (ulBase <= 10)
432 ulRet += pszNum[i] - '0';
433 else
434 ulRet += pszNum[i] - (pszNum[i] >= 'A' ? 'A' - 10 : (pszNum[i] >= 'a' ? 'a' + 9 : '0'));
435
436 i++;
437 }
438
439 return i > 0 ? ulRet : ~0UL;
440}
441
442
443/**
444 * Get kernel OTEs
445 * This function set pKrnlMTE, pKrnlSMTE and pKrnlOTE.
446 * @returns Strategy return code:
447 * STATUS_DONE on success.
448 * STATUS_DONE | STERR | errorcode on failure.
449 * @param pKrnlInfo Pointer to output buffer.
450 * If NULL only the three global variables are set.
451 * @status completely implemented and tested.
452 * @author knut st. osmundsen
453 * @remark Called from IOCtl.
454 * WARNING! This function is called before the initroutine (R0INIT)!
455 */
456USHORT _loadds _Far32 _Pascal GetKernelInfo32(PKRNLINFO pKrnlInfo)
457{
458 int i;
459 USHORT usRc;
460
461 /* VerifyImporTab32 is called before the initroutine! */
462 pulTKSSBase32 = (PULONG)_TKSSBase16;
463
464 /* Find the kernel OTE table */
465#ifndef R3TST
466 pKrnlMTE = GetOS2KrnlMTE();
467#else
468 pKrnlMTE = GetOS2KrnlMTETst();
469#endif
470 if (pKrnlMTE != NULL)
471 {
472 pKrnlSMTE = pKrnlMTE->mte_swapmte;
473 if (pKrnlSMTE != NULL)
474 {
475 if (pKrnlSMTE->smte_objcnt <= MAXKRNLOBJECTS)
476 {
477 pKrnlOTE = pKrnlSMTE->smte_objtab;
478 if (pKrnlOTE != NULL)
479 {
480 /*
481 * Thats all?
482 */
483 if (pKrnlInfo == NULL)
484 return NO_ERROR;
485
486 pKrnlInfo->cObjects = (unsigned char)pKrnlSMTE->smte_objcnt;
487
488 /*
489 * Copy OTEs
490 */
491 for (i = 0; i < pKrnlInfo->cObjects; i++)
492 {
493 memcpy((void*)&pKrnlInfo->aObjects[i], &pKrnlOTE[i], sizeof(OTE));
494 kprintf2(("GetKernelInfo32: %d base=0x%08x size=0x%08x flags=0x%08x\n",
495 i, pKrnlOTE[i].ote_base, pKrnlOTE[i].ote_size, pKrnlOTE[i].ote_flags));
496 }
497 usRc = 0;
498
499 /*
500 * Search for internal revision stuff in the two first objects.
501 */
502 pKrnlInfo->ulBuild = 0;
503 for (i = 0; i < 2 && pKrnlInfo->ulBuild == 0; i++)
504 {
505 const char *psz = (const char*)pKrnlOTE[i].ote_base;
506 const char *pszEnd = psz + pKrnlOTE[i].ote_size - 50; /* Last possible search position. */
507
508 while (psz < pszEnd)
509 {
510 if (strncmp(psz, "Internal revision ", 18) == 0 && (psz[18] >= '0' && psz[18] <= '9'))
511 {
512 int j;
513 kprintf2(("GetKernelInfo32: found internal revision: '%s'\n", psz));
514
515 /* skip to end of "Internal revision " string. */
516 psz += 18;
517
518 /* Read number*/
519 while ((*psz >= '0' && *psz <= '9') || *psz == '.')
520 {
521 if (*psz != '.')
522 pKrnlInfo->ulBuild = (unsigned short)(pKrnlInfo->ulBuild * 10 + (*psz - '0'));
523 psz++;
524 }
525
526 /* Check if build number seems valid. */
527 if ( !(pKrnlInfo->ulBuild >= 8254 && pKrnlInfo->ulBuild < 8383) /* Warp 3 fp 32 -> fp 60 */
528 && !(pKrnlInfo->ulBuild >= 9023 && pKrnlInfo->ulBuild <= 9036) /* Warp 4 GA -> fp 12 */
529 && !(pKrnlInfo->ulBuild >= 14039 && pKrnlInfo->ulBuild < 14080) /* Warp 4.5 GA -> fp 40 */
530 && !(pKrnlInfo->ulBuild >= 6600 && pKrnlInfo->ulBuild <= 6678) /* Warp 2.1x fix?? (just for fun!) */
531 )
532 {
533 kprintf(("GetKernelInfo32: info summary: Build %d is invalid - invalid fixpack?\n", pKrnlInfo->ulBuild));
534 usRc = ERROR_D32_INVALID_BUILD;
535 break;
536 }
537
538 /* If this is an Aurora/Warp 4.5 or Warp 3 kernel there is more info! */
539 if ((psz[0] != ',' && psz[1] == '_' && (psz[2] == 'S' || psz[2] == 's')) /* F_SMP */
540 || (psz[0] == '_' && (psz[1] == 'S' || psz[1] == 's')) /* _SMP */
541 )
542 pKrnlInfo->fKernel = KF_SMP;
543 else
544 if (*psz != ','
545 && ( (psz[0] == '_' && psz[1] == 'W' && psz[2] == '4') /* _W4 */
546 || (psz[1] == '_' && psz[2] == 'W' && psz[3] == '4') /* A_W4 */
547 || (psz[0] == '_' && psz[1] == 'U' && psz[2] == 'N' && psz[3] == 'I' && psz[4] == '4') /* _UNI4 */
548 || (psz[1] == '_' && psz[2] == 'U' && psz[3] == 'N' && psz[4] == 'I' && psz[5] == '4') /* A_UNI4 */
549 )
550 )
551 pKrnlInfo->fKernel = KF_W4 | KF_UNI;
552 else
553 pKrnlInfo->fKernel = KF_UNI;
554
555
556 /* Check if its a debug kernel (look for DEBUG at start of object 3-5) */
557 j = 3;
558 while (j < 5)
559 {
560 /* There should be no iopl object preceding the debugger data object. */
561 if ((pKrnlOTE[j].ote_flags & OBJIOPL) != 0)
562 break;
563 /* Is this is? */
564 if ((pKrnlOTE[j].ote_flags & OBJINVALID) == 0
565 && (pKrnlOTE[j].ote_flags & (OBJREAD | OBJWRITE)) == (OBJREAD | OBJWRITE)
566 && strncmp((char*)pKrnlOTE[j].ote_base, "DEBUG", 5) == 0)
567 {
568 pKrnlInfo->fKernel |= KF_DEBUG;
569 break;
570 }
571 j++;
572 }
573
574 /* Display info */
575 kprintf(("GetKernelInfo32: info summary: Build %d, fKernel=%d\n",
576 pKrnlInfo->ulBuild, pKrnlInfo->fKernel));
577
578 /* Break out */
579 break;
580 }
581
582 /* next */
583 psz++;
584 } /* while loop searching for "Internal revision " */
585 } /* for loop on objects 0-1. */
586
587 /* Set error code if not found */
588 if (pKrnlInfo->ulBuild == 0)
589 {
590 usRc = ERROR_D32_BUILD_INFO_NOT_FOUND;
591 kprintf(("GetKernelInfo32: Internal revision was not found!\n"));
592 }
593 }
594 else
595 usRc = ERROR_D32_NO_OBJECT_TABLE;
596 }
597 else
598 usRc = ERROR_D32_TOO_MANY_OBJECTS;
599 }
600 else
601 usRc = ERROR_D32_NO_SWAPMTE;
602 }
603 else
604 usRc = ERROR_D32_GETOS2KRNL_FAILED;
605
606 if (usRc != NO_ERROR)
607 kprintf(("GetKernelInfo32: failed. usRc = %d\n", usRc));
608
609 return (USHORT)(usRc | (usRc != NO_ERROR ? STATUS_DONE | STERR : STATUS_DONE));
610}
611
612
613
614/**
615 * Functions which cacluates the instructionsize given a ModR/M byte.
616 * @returns Number of bytes to add to cb and pach.
617 * @param bModRM ModR/M byte.
618 * @status completely implemented.
619 * @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
620 */
621int ModR_M_32bit(char bModRM)
622{
623 if ((bModRM & 0xc0) == 0x80 /* ex. mov ax,[ebp+11145543h] */
624 || ((bModRM & 0xc0) == 0 && (bModRM & 0x07) == 5)) /* ex. mov ebp,[0ff231234h] */
625 { /* 32-bit displacement */
626 return 5 + ((bModRM & 0x7) == 0x4); // + SIB
627 }
628 else if ((bModRM & 0xc0) == 0x40) /* ex. mov ecx,[esi]+4fh */
629 { /* 8-bit displacement */
630 return 2 + ((bModRM & 0x7) == 0x4); // + SIB
631 }
632 /* no displacement (only /r byte) */
633 return 1;
634}
635
636
637/**
638 * Functions which cacluates the instructionsize given a ModR/M byte.
639 * @returns Number of bytes to add to cb and pach.
640 * @param bModRM ModR/M byte.
641 * @status completely implemented.
642 * @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
643 */
644int ModR_M_16bit(char bModRM)
645{
646 if ((bModRM & 0xc0) == 0x80 /* ex. mov ax,[ebp+11145543h] */
647 || ((bModRM & 0xc0) == 0 && (bModRM & 0x07) == 5)) /* ex. mov ebp,[0ff231234h] */
648 { /* 16-bit displacement */
649 return 4;
650 }
651 else if ((bModRM & 0xc0) == 0x40) /* ex. mov ecx,[esi]+4fh */
652 { /* 8-bit displacement */
653 return 2;
654 }
655 /* no displacement (only /r byte) */
656 return 1;
657}
658
659
660
661
662
663/**
664 * 32-bit! Interpret function prolog to find where to jmp back.
665 * @returns Length of prolog need to be copied - which is also the offset of
666 * where the jmp instr should be placed.
667 * On error it returns 0.
668 * @param pach Pointer to prolog.
669 * @param fOverload TRUE: Function is to be overloaded.
670 * FALSE: Function is to be imported.
671 */
672int interpretFunctionProlog32(char *pach, BOOL fOverload)
673{
674 int cb = -3;
675 kprintf2(("interpretFunctionProlog32(0x%08x, %d):\n"
676 "\t%02x %02x %02x %02x - %02x %02x %02x %02x\n"
677 "\t%02x %02x %02x %02x - %02x %02x %02x %02x\n",
678 pach, fOverload,
679 pach[0], pach[1], pach[2], pach[3], pach[4], pach[5], pach[6], pach[7],
680 pach[8], pach[9], pach[10],pach[11],pach[12],pach[13],pach[14],pach[15]));
681
682 /*
683 * check for the well known prolog (the only that is supported now)
684 * which is:
685 * push ebp
686 * mov ebp,esp
687 * or
688 * push ebp
689 * mov eax, dword ptr [xxxxxxxx]
690 *
691 * These are allowed when not overloading:
692 * mov eax, imm32
693 * jmp short
694 * or
695 * mov eax, imm32
696 * push ebp
697 * or
698 * mov ecx, r/m32
699 * or
700 * jmp dword
701 * or
702 * sub esp, imm8
703 * or
704 * call ptr16:32
705 * or
706 * enter imm16, imm8 (2.1x)
707 * or
708 * mov eax, imm32 (2.1x)
709 * <anything>
710 * or
711 * xor r32, r/m32
712 * or
713 * mov eax, msoff32
714 *
715 */
716 if ((pach[0] == 0x55 && (pach[1] == 0x8b || pach[1] == 0xa1)) /* the two first prologs */
717 ||
718 (pach[0] == 0xB8 && (pach[5] == 0xEB || pach[5] == 0x55) && !fOverload) /* the two next prologs */
719 ||
720 (pach[0] == 0x8B && !fOverload) /* the next prolog */
721 ||
722 (pach[0] == 0xFF && !fOverload) /* the next prolog */
723 ||
724 (pach[0] == 0x83 && !fOverload) /* the next prolog */
725 ||
726 (pach[0] == 0x9a && !fOverload) /* the next prolog */
727 ||
728 (pach[0] == 0xc8) /* the next prolog */
729 ||
730 (pach[0] == 0xB8 && !fOverload) /* the next prolog */
731 ||
732 (pach[0] == 0x33 && !fOverload) /* the next prolog */
733 ||
734 (pach[0] == 0xa1 && !fOverload) /* last prolog */
735 )
736 {
737 BOOL fForce = FALSE;
738 int cbWord = 4;
739 cb = 0;
740 while (cb < 5 || fForce) /* 5 is the size of a jump instruction. */
741 {
742 int cb2;
743 if (!fForce && cbWord != 4)
744 cbWord = 4;
745 fForce = FALSE;
746 switch (*pach)
747 {
748 /* simple one byte prefixes */
749 case 0x2e: /* cs segment override */
750 case 0x36: /* ss segment override */
751 case 0x3e: /* ds segment override */
752 case 0x26: /* es segment override */
753 case 0x64: /* fs segment override */
754 case 0x65: /* gs segment override */
755 fForce = TRUE;
756 break;
757
758 case 0x66: /* 16 bit */
759 fForce = TRUE;
760 cbWord = 2;
761 break;
762
763 /* simple one byte instructions */
764 case 0x50: /* push ax */
765 case 0x51: /* push cx */
766 case 0x52: /* push dx */
767 case 0x53: /* push bx */
768 case 0x54: /* push sp */
769 case 0x55: /* push bp */
770 case 0x56: /* push si */
771 case 0x57: /* push di */
772 break;
773
774 /* simple two byte instructions */
775 case 0xb0: /* mov al, imm8 */
776 case 0xb1: /* mov cl, imm8 */
777 case 0xb2: /* mov dl, imm8 */
778 case 0xb3: /* mov bl, imm8 */
779 case 0xb4: /* mov ah, imm8 */
780 case 0xb5: /* mov ch, imm8 */
781 case 0xb6: /* mov dh, imm8 */
782 case 0xb7: /* mov bh, imm8 */
783 case 0x2c: /* sub al, imm8 */
784 case 0x34: /* xor al, imm8 */
785 case 0x3c: /* cmp al, imm8 */
786 case 0x6a: /* push <byte> */
787 case 0xa0: /* mov al, moffs8 */
788 case 0xa2: /* mov moffs8, al */
789 pach++;
790 cb++;
791 break;
792
793 /* simple five byte instructions */
794 case 0xb8: /* mov eax, imm32 */
795 case 0xb9: /* mov ecx, imm32 */
796 case 0xba: /* mov edx, imm32 */
797 case 0xbb: /* mov ebx, imm32 */
798 case 0xbc: /* mov esx, imm32 */
799 case 0xbd: /* mov ebx, imm32 */
800 case 0xbe: /* mov esi, imm32 */
801 case 0xbf: /* mov edi, imm32 */
802 case 0x2d: /* sub eax, imm32 */
803 case 0x35: /* xor eax, imm32 */
804 case 0x3d: /* cmp eax, imm32 */
805 case 0x68: /* push <dword> */
806 case 0xa1: /* mov eax, moffs16 */
807 case 0xa3: /* mov moffs16, eax */
808 pach += cbWord;
809 cb += cbWord;
810 break;
811
812 /* complex sized instructions - "/r" */
813 case 0x30: /* xor r/m8, r8 */
814 case 0x31: /* xor r/m32, r32 */
815 case 0x32: /* xor r8, r/m8 */
816 case 0x33: /* xor r32, r/m32 */
817 case 0x38: /* cmp r/m8, r8 */
818 case 0x39: /* cmp r/m32, r32 */
819 case 0x3a: /* cmp r8, r/m8 */
820 case 0x3b: /* cmp r32, r/m32 */
821 case 0x28: /* sub r/m8, r8 */
822 case 0x29: /* sub r/m32, r32 */
823 case 0x2a: /* sub r8, r/m8 */
824 case 0x2b: /* sub r32, r/m32 */
825 case 0x8b: /* mov /r */
826 case 0x8d: /* lea /r */
827 cb += cb2 = ModR_M_32bit(pach[1]);
828 pach += cb2;
829 break;
830
831 /* complex sized instruction - "/5 ib" */
832 case 0x80: /* 5: sub r/m8, imm8 7: cmp r/m8, imm8 */
833 case 0x83: /* 5: sub r/m32, imm8 7: cmp r/m32, imm8 */
834 if ((pach[1] & 0x38) == (5<<3)
835 || (pach[1] & 0x38) == (7<<3)
836 )
837 {
838 cb += cb2 = 1 + ModR_M_32bit(pach[1]); /* 1 is the size of the imm8 */
839 pach += cb2;
840 }
841 else
842 {
843 kprintf(("interpretFunctionProlog32: unknown instruction (-3) 0x%x 0x%x 0x%x\n", pach[0], pach[1], pach[2]));
844 return -3;
845 }
846 break;
847
848 /* complex sized instruction - "/digit id" */
849 case 0x81: /* sub r/m32, imm32 + more instructions! */
850 if ((pach[1] & 0x38) == (5<<3) /* sub r/m32, imm32 */
851 || (pach[1] & 0x38) == (7<<3) /* cmp r/m32, imm32 */
852 )
853 {
854 cb += cb2 = cbWord + ModR_M_32bit(pach[1]); /* cbWord is the size of the imm32/imm16 */
855 pach += cb2;
856 }
857 else
858 {
859 kprintf(("interpretFunctionProlog32: unknown instruction (-2) 0x%x 0x%x 0x%x\n", pach[0], pach[1], pach[2]));
860 return -2;
861 }
862 break;
863
864 case 0x9a: /* call ptr16:32 */
865 cb += cb2 = 6;
866 pach += cb2;
867 break;
868
869 case 0xc8: /* enter imm16, imm8 */
870 cb += cb = 3;
871 pach += cb2;
872 break;
873
874 /*
875 * jmp /digit
876 */
877 case 0xff:
878 cb += cb2 = cbWord + ModR_M_32bit(pach[1]); /* cbWord is the size of the imm32/imm16 */
879 pach += cb2;
880 break;
881
882 default:
883 kprintf(("interpretFunctionProlog32: unknown instruction 0x%x 0x%x 0x%x\n", pach[0], pach[1], pach[2]));
884 return 0;
885 }
886 pach++;
887 cb++;
888 }
889 }
890 else
891 {
892 kprintf(("interpretFunctionProlog32: unknown prolog start. 0x%x 0x%x 0x%x 0x%x 0x%x\n",
893 pach[0], pach[1], pach[2], pach[3], pach[4]));
894 cb = 0;
895 }
896 return cb;
897}
898
899
900/**
901 * 16-bit! Interpret function prolog to find where to jmp back.
902 * @returns Length of prolog need to be copied - which is also the offset of
903 * where the jmp instr should be placed.
904 * On error it returns 0.
905 * @param pach Pointer to prolog.
906 * @param fOverload TRUE: Function is to be overloaded.
907 * FALSE: Function is to be imported.
908 */
909int interpretFunctionProlog16(char *pach, BOOL fOverload)
910{
911 int cb = -7;
912
913 kprintf2(("interpretFunctionProlog16(0x%08x, %d):\n"
914 "\t%02x %02x %02x %02x - %02x %02x %02x %02x\n"
915 "\t%02x %02x %02x %02x - %02x %02x %02x %02x\n",
916 pach, fOverload,
917 pach[0], pach[1], pach[2], pach[3], pach[4], pach[5], pach[6], pach[7],
918 pach[8], pach[9], pach[10],pach[11],pach[12],pach[13],pach[14],pach[15]));
919 /*
920 * Check for the well known prolog (the only that is supported now)
921 * which is:
922 * push 2
923 */
924 if (*pach == 0x6A) /* push 2 (don't check for the 2) */
925 {
926 BOOL fForce;
927 int cOpPrefix = 0;
928 cb = 0;
929 while (cb < 8 || fForce) /* 8 is the size of a 66h prefixed far jump instruction. */
930 {
931 int cb2;
932 fForce = FALSE;
933 switch (*pach)
934 {
935 case 0x06: /* push es */
936 case 0x0e: /* push cs */
937 case 0x1e: /* push ds */
938 case 0x16: /* push ss */
939 break;
940
941 case 0x0f: /* push gs and push fs */
942 if (pach[1] != 0xA0 && pach[1] != 0xA8)
943 {
944 kprintf(("interpretFunctionProlog16: unknown instruction 0x%x 0x%x 0x%x\n", pach[0], pach[1], pach[2]));
945 return -11;
946 }
947 pach++;
948 cb++;
949 break;
950
951 case 0x50: /* push ax */
952 case 0x51: /* push cx */
953 case 0x52: /* push dx */
954 case 0x53: /* push bx */
955 case 0x54: /* push sp */
956 case 0x55: /* push bp */
957 case 0x56: /* push si */
958 case 0x57: /* push di */
959 break;
960
961 case 0x2e: /* cs segment override */
962 case 0x36: /* ss segment override */
963 case 0x3e: /* ds segment override */
964 case 0x26: /* es segment override */
965 case 0x64: /* fs segment override */
966 case 0x65: /* gs segment override */
967 fForce = TRUE;
968 if (cOpPrefix > 0)
969 cOpPrefix++;
970 break;
971
972 case 0x66:
973 cOpPrefix = 2; /* it's decremented once before it's used. */
974 fForce = TRUE;
975 break;
976
977 case 0x6a: /* push <byte> */
978 pach++;
979 cb++;
980 break;
981
982 case 0x68: /* push <word> */
983 if (cOpPrefix > 0)
984 {
985 pach += 2;
986 cb += 2;
987 }
988 pach += 2;
989 cb += 2;
990 break;
991
992 case 0x8b: /* mov /r */
993 if ((pach[1] & 0xc0) == 0x80 /* ex. mov ax,bp+1114h */
994 || ((pach[1] & 0xc0) == 0 && (pach[1] & 0x7) == 6)) /* ex. mov bp,0ff23h */
995 { /* 16-bit displacement */
996 if (cOpPrefix > 0)
997 {
998 pach += 2;
999 cb += 2;
1000 }
1001 pach += 3;
1002 cb += 3;
1003 }
1004 else
1005 if ((pach[1] & 0xc0) == 0x40) /* ex. mov ax,[si]+4fh */
1006 { /* 8-bit displacement */
1007 pach += 2;
1008 cb += 2;
1009 }
1010 else
1011 { /* no displacement (only /r byte) */
1012 pach++;
1013 cb++;
1014 }
1015 break;
1016
1017 /* complex sized instruction - "/5 ib" */
1018 case 0x80: /* 5: sub r/m8, imm8 7: cmp r/m8, imm8 */
1019 case 0x83: /* 5: sub r/m16, imm8 7: cmp r/m16, imm8 */
1020 if ((pach[1] & 0x38) == (5<<3)
1021 || (pach[1] & 0x38) == (7<<3)
1022 )
1023 {
1024 cb += cb2 = 1 + ModR_M_16bit(pach[1]); /* 1 is the size of the imm8 */
1025 pach += cb2;
1026 }
1027 else
1028 {
1029 kprintf(("interpretFunctionProlog16: unknown instruction (-3) 0x%x 0x%x 0x%x\n", pach[0], pach[1], pach[2]));
1030 return -3;
1031 }
1032 break;
1033
1034
1035 default:
1036 kprintf(("interpretFunctionProlog16: unknown instruction 0x%x 0x%x 0x%x\n", pach[0], pach[1], pach[2]));
1037 return 0;
1038 }
1039 pach++;
1040 cb++;
1041 if (cOpPrefix > 0)
1042 cOpPrefix--;
1043 }
1044 }
1045
1046 fOverload = fOverload;
1047 return cb;
1048}
1049
1050
1051/**
1052 * Verifies the aImportTab.
1053 * @returns 0 if ok. !0 if not ok.
1054 * @remark Called from IOCtl.
1055 * WARNING! This function is called before the initroutine (R0INIT)!
1056 */
1057USHORT _loadds _Far32 _Pascal VerifyImportTab32(void)
1058{
1059 USHORT usRc;
1060 int i;
1061 int cb;
1062 int cbmin;
1063
1064 /* VerifyImporTab32 is called before the initroutine! */
1065 pulTKSSBase32 = (PULONG)_TKSSBase16;
1066
1067 /* Check that pKrnlOTE is set */
1068 usRc = GetKernelInfo32(NULL);
1069 if (usRc != NO_ERROR)
1070 return (USHORT)(STATUS_DONE | STERR | (usRc & STECODE));
1071
1072 /*
1073 * Verify aImportTab.
1074 */
1075 for (i = 0; i < NBR_OF_KRNLIMPORTS; i++)
1076 {
1077 /*
1078 * Debug info
1079 */
1080 kprintf2(("VerifyImportTab32: procedure no.%d is being checked: %s addr=0x%08x iObj=%d offObj=%d\n",
1081 i, &aImportTab[i].achName[0], aImportTab[i].ulAddress,
1082 aImportTab[i].iObject, aImportTab[i].offObject));
1083
1084 /* Verify that it is found */
1085 if (!aImportTab[i].fFound)
1086 {
1087 if (aImportTab[i].fType & EPT_NOT_REQ)
1088 continue;
1089 else
1090 {
1091 kprintf(("VerifyImportTab32: procedure no.%d was not fFound!\n", i));
1092 return STATUS_DONE | STERR | ERROR_D32_PROC_NOT_FOUND;
1093 }
1094 }
1095
1096 /* Verify read/writeable. */
1097 if (aImportTab[i].iObject >= pKrnlSMTE->smte_objcnt /* object index valid? */
1098 || aImportTab[i].ulAddress < pKrnlOTE[aImportTab[i].iObject].ote_base /* address valid? */
1099 || aImportTab[i].ulAddress + 16 > (pKrnlOTE[aImportTab[i].iObject].ote_base +
1100 pKrnlOTE[aImportTab[i].iObject].ote_size) /* address valid? */
1101 || aImportTab[i].ulAddress - aImportTab[i].offObject
1102 != pKrnlOTE[aImportTab[i].iObject].ote_base /* offObject ok? */
1103 )
1104 {
1105 kprintf(("VerifyImportTab32: procedure no.%d has an invalid address or object number.!\n"
1106 " %s addr=0x%08x iObj=%d offObj=%d\n",
1107 i, &aImportTab[i].achName[0], aImportTab[i].ulAddress,
1108 aImportTab[i].iObject, aImportTab[i].offObject));
1109 return STATUS_DONE | STERR | ERROR_D32_INVALID_OBJ_OR_ADDR;
1110 }
1111
1112
1113 #ifndef R3TST
1114 if (aImportTab[i].ulAddress < 0xff400000UL)
1115 {
1116 kprintf(("VerifyImportTab32: procedure no.%d has an invalid address, %#08x!\n",
1117 i, aImportTab[i].ulAddress));
1118 return STATUS_DONE | STERR | ERROR_D32_INVALID_ADDRESS;
1119 }
1120 #endif
1121
1122 switch (aImportTab[i].fType & ~(EPT_BIT_MASK | EPT_NOT_REQ))
1123 {
1124 case EPT_PROC:
1125 case EPT_PROCIMPORT:
1126 /*
1127 * Verify known function prolog.
1128 */
1129 if (EPT32BitEntry(aImportTab[i]))
1130 {
1131 cb = interpretFunctionProlog32((char*)aImportTab[i].ulAddress,
1132 aImportTab[i].fType == EPT_PROC32);
1133 cbmin = 5; /* Size of the jump instruction */
1134 }
1135 else
1136 {
1137 cb = interpretFunctionProlog16((char*)aImportTab[i].ulAddress,
1138 aImportTab[i].fType == EPT_PROC16);
1139 cbmin = 7; /* Size of the far jump instruction */
1140 }
1141
1142 /*
1143 * Check result of the function prolog interpretations.
1144 */
1145 if (cb <= 0 || cb + cbmin >= MAXSIZE_PROLOG)
1146 { /* failed, too small or too large. */
1147 kprintf(("VerifyImportTab32: verify failed for procedure no.%d (cb=%d)\n", i, cb));
1148 return STATUS_DONE | STERR | ERROR_D32_TOO_INVALID_PROLOG;
1149 }
1150 break;
1151
1152 case EPT_VARIMPORT:
1153 /* do nothing! */
1154 break;
1155
1156 default:
1157 kprintf(("VerifyImportTab32: invalid type/type not implemented\n",i));
1158 Int3(); /* temporary fix! */
1159 return STATUS_DONE | STERR | ERROR_D32_NOT_IMPLEMENTED;
1160 }
1161 }
1162
1163 return STATUS_DONE;
1164}
1165
1166
1167/**
1168 * Initiates the overrided functions.
1169 * @returns NO_ERROR on success. !0 on error.
1170 */
1171int importTabInit(void)
1172{
1173 /* This table must be updated with the overloading functions.
1174 * It should also hold NOP functions for functions which are of the
1175 * not required type.
1176 */
1177 static unsigned auFuncs[NBR_OF_KRNLIMPORTS] =
1178 {
1179 (unsigned)myldrRead, /* 0 */
1180 (unsigned)myldrOpen, /* 1 */
1181 (unsigned)myldrClose, /* 2 */
1182 (unsigned)myLDRQAppType, /* 3 */
1183 (unsigned)myldrEnum32bitRelRecs,/* 4 */
1184 0, /* 5 */
1185 0, /* 6 */
1186 0, /* 7 */
1187 0, /* 8 */
1188 0, /* 9 */
1189 0, /* 10 */
1190 0, /* 11 */
1191 0, /* 12 */
1192 (unsigned)&mytkExecPgm, /* 13 */
1193 (unsigned)&mytkStartProcess, /* 14 */
1194 0, /* 15 */
1195 0, /* 16 */
1196 0, /* 17 */
1197 0, /* 18 */
1198 0, /* 19 */
1199 (unsigned)myldrOpenPath, /* 20 */
1200 0, /* 21 */
1201 0, /* 22 */
1202 0, /* 23 */
1203 0, /* 24 */
1204 0, /* 25 */
1205 0, /* 26 */
1206 0, /* 27 */
1207 0, /* 28 */
1208 0, /* 29 */
1209 0, /* 30 */
1210 0, /* 31 */
1211 0, /* 32 */
1212 0, /* 33 */
1213 0, /* 34 */
1214 0, /* 35 */
1215 0, /* 36 */
1216 0, /* 37 */
1217 0, /* 38 */
1218 0, /* 39 */
1219 0, /* 40 */
1220 (unsigned)nopSecPathFromSFN, /* 41 */
1221 };
1222 int i;
1223 int cb;
1224 int cbmin;
1225
1226#ifdef R3TST
1227 R3TstFixImportTab();
1228#endif
1229
1230 /*
1231 * verify proctable
1232 */
1233 for (i = 0; i < NBR_OF_KRNLIMPORTS; i++)
1234 {
1235 /* EPT_VARIMPORTs are skipped */
1236 if ((aImportTab[i].fType & ~EPT_BIT_MASK) == EPT_VARIMPORT)
1237 continue;
1238 /* EPT_NOT_REQ which is not found are set pointing to the nop function provided. */
1239 if (!aImportTab[i].fFound && (aImportTab[i].fType & EPT_NOT_REQ))
1240 {
1241 aImportTab[i].ulAddress = auFuncs[i];
1242 continue;
1243 }
1244
1245 if (EPT32BitEntry(aImportTab[i]))
1246 {
1247 cb = interpretFunctionProlog32((char*)aImportTab[i].ulAddress, aImportTab[i].fType == EPT_PROC32);
1248 cbmin = 5; /* Size of the jump instruction */
1249 }
1250 else
1251 {
1252 cb = interpretFunctionProlog16((char*)aImportTab[i].ulAddress, aImportTab[i].fType == EPT_PROC16);
1253 cbmin = 7; /* Size of the far jump instruction */
1254 }
1255 if (cb <= 0 || cb + cbmin >= MAXSIZE_PROLOG)
1256 {
1257 kprintf(("ImportTabInit: Verify failed for procedure no.%d, cb=%d\n", i, cb));
1258 return ERROR_D32_VERIFY_FAILED;
1259 }
1260 }
1261
1262 /*
1263 * rehook / import
1264 */
1265 for (i = 0; i < NBR_OF_KRNLIMPORTS; i++)
1266 {
1267 switch (aImportTab[i].fType & ~EPT_NOT_REQ)
1268 {
1269 /*
1270 * 32-bit procedure overload.
1271 * The overloading procedure is found in the auFuncs table (at the same index
1272 * as the overloaded procedure has in aImportTab).
1273 * The overloaded procedure is called by issuing a call to the callTab entry.
1274 */
1275 case EPT_PROC32:
1276 {
1277 cb = interpretFunctionProlog32((char*)aImportTab[i].ulAddress, TRUE);
1278 aImportTab[i].cbProlog = (char)cb;
1279 if (cb >= 5 && cb + 5 < MAXSIZE_PROLOG) /* 5(1st): size of jump instruction in the function prolog which jumps to my overloading function */
1280 { /* 5(2nd): size of jump instruction which jumps back to the original function after executing the prolog copied to the callTab entry for this function. */
1281 /*
1282 * Copy function prolog which will be overwritten by the jmp to calltabl.
1283 */
1284 memcpy(callTab[i], (void*)aImportTab[i].ulAddress, (size_t)cb);
1285
1286 /*
1287 * Make jump instruction which jumps from calltab to original function.
1288 * 0xE9 <four bytes displacement>
1289 * Note: the displacement is relative to the next instruction
1290 */
1291 callTab[i][cb] = 0xE9; /* jmp */
1292 *(unsigned long*)(void*)&callTab[i][cb+1] = aImportTab[i].ulAddress + cb - (unsigned long)&callTab[i][cb+5];
1293
1294 /*
1295 * Jump from original function to my function - an cli(?) could be needed here
1296 */
1297 *(char*)aImportTab[i].ulAddress = 0xE9; /* jmp */
1298 *(unsigned long*)(aImportTab[i].ulAddress + 1) = auFuncs[i] - (aImportTab[i].ulAddress + 5);
1299 }
1300 else
1301 { /* !fatal! - this could never happen really... */
1302 kprintf(("ImportTabInit: FATAL verify failed for procedure no.%d when rehooking it!\n", i));
1303 Int3(); /* ipe - later! */
1304 return ERROR_D32_VERIFY_FAILED;
1305 }
1306 break;
1307 }
1308
1309
1310 /*
1311 * 16-bit procedure overload.
1312 * Currently disabled due to expected problems when calltab is a 32-bit segment.
1313 */
1314 case EPT_PROC16:
1315 {
1316 kprintf(("ImportTabInit: Overloading 16-bit procedures are not supported yet!!! Calltable in 32-bit segment!\n", i));
1317 Int3();
1318
1319 cb = interpretFunctionProlog16((char*)aImportTab[i].ulAddress, TRUE);
1320 aImportTab[i].cbProlog = (char)cb;
1321 if (cb >= 8 && cb + 7 < MAXSIZE_PROLOG) /* 8: size of a 16:32 jump which jumps to my overloading function (prefixed with 66h in a 16-bit segment) */
1322 { /* 7: size of a 16:32 jump which is added to the call tab */
1323 /*
1324 * Copy function prolog which is to be overwritten.
1325 */
1326 memcpy(callTab[i], (void*)aImportTab[i].ulAddress, (size_t)cb);
1327
1328 /*
1329 * Create far jump from calltab to original function.
1330 * 0xEA <four byte target address> <two byte target selector>
1331 */
1332 callTab[i][cb] = 0xEA; /* jmp far ptr */
1333 *(unsigned long*)(void*)&callTab[i][cb+1] = aImportTab[i].offObject;
1334 *(unsigned short*)(void*)&callTab[i][cb+5] = aImportTab[i].usSel;
1335
1336 /*
1337 * jump from original function to my function - an cli(?) could be needed here
1338 * 0x66 0xEA <four byte target address> <two byte target selector>
1339 */
1340 *(char*)(aImportTab[i].ulAddress ) = 0x66; /* operandsize prefix */
1341 *(char*)(aImportTab[i].ulAddress + 1) = 0xEA; /* jmp far ptr */
1342 *(unsigned long*)(aImportTab[i].ulAddress + 2) = auFuncs[i]; /* FIXME? */
1343 *(unsigned short*)(aImportTab[i].ulAddress + 6) = _R0FlatCS16; /* FIXME */
1344 }
1345 else
1346 { /* !fatal! - this could never happen really... */
1347 kprintf(("ImportTabInit: FATAL verify failed for procedure no.%d when rehooking it!\n", i));
1348 Int3(); /* ipe - later! */
1349 return ERROR_D32_VERIFY_FAILED;
1350 }
1351 break;
1352 }
1353
1354
1355 /*
1356 * 32-bit imported procedure
1357 * This is called by issuing a near call to the callTab entry.
1358 */
1359 case EPT_PROCIMPORT32:
1360 {
1361 cb = interpretFunctionProlog32((char*)aImportTab[i].ulAddress, FALSE);
1362 aImportTab[i].cbProlog = (char)cb;
1363 if (cb > 0) /* Since no prolog part is copied to the function table, it's ok as long as the prolog has been recognzied. */
1364 {
1365 /*
1366 * Make jump instruction which jumps from calltab to original function.
1367 * 0xE9 <four bytes displacement>
1368 * Note: the displacement is relative to the next instruction
1369 */
1370 callTab[i][0] = 0xE9; /* jmp */
1371 *(unsigned*)(void*)&callTab[i][1] = aImportTab[i].ulAddress - (unsigned)&callTab[i][5];
1372 }
1373 else
1374 { /* !fatal! - this should never really happen... */
1375 kprintf(("ImportTabInit: FATAL verify failed for procedure no.%d when importing it!\n", i));
1376 Int3(); /* ipe - later! */
1377 return ERROR_D32_VERIFY_FAILED;
1378 }
1379 break;
1380 }
1381
1382
1383 /*
1384 * 16-bit imported procedure.
1385 * This is called by issuing a far call to the calltab entry.
1386 */
1387 case EPT_PROCIMPORT16:
1388 {
1389 cb = interpretFunctionProlog16((char*)aImportTab[i].ulAddress, FALSE);
1390 aImportTab[i].cbProlog = (char)cb;
1391 if (cb > 0) /* Since no prolog part is copied to the function table, it's ok as long as the prolog has been recognzied. */
1392 {
1393 /*
1394 * Create far jump from calltab to original function.
1395 * 0xEA <four byte target address> <two byte target selector>
1396 */
1397 callTab[i][0] = 0xEA; /* jmp far ptr */
1398 *(unsigned long*)(void*)&callTab[i][1] = aImportTab[i].offObject;
1399 *(unsigned short*)(void*)&callTab[i][5] = aImportTab[i].usSel;
1400 }
1401 else
1402 { /* !fatal! - this should never really happen... */
1403 kprintf(("ImportTabInit: FATAL verify failed for procedure no.%d when importing it!\n", i));
1404 Int3(); /* ipe - later! */
1405 return ERROR_D32_VERIFY_FAILED;
1406 }
1407 break;
1408 }
1409
1410
1411 /*
1412 * 16/32-bit importe variable.
1413 * This is used by accessing the 32-bit flat address in the callTab.
1414 * callTab-entry + 4 holds the offset of the variable into the object.
1415 * callTab-entry + 8 holds the selector for the object. (These two fields is the 16:32-bit pointer to the variable.)
1416 * callTab-entry + a holds the 16-bit offset for the variable.
1417 * callTab-entry + c holds the selector for the object. (These two fields is the 16:16-bit pointer to the variable.)
1418 */
1419 case EPT_VARIMPORT32:
1420 case EPT_VARIMPORT16:
1421 aImportTab[i].cbProlog = (char)0;
1422 *(unsigned long*)(void*)&callTab[i][0] = aImportTab[i].ulAddress;
1423 *(unsigned long*)(void*)&callTab[i][4] = aImportTab[i].offObject;
1424 *(unsigned short*)(void*)&callTab[i][8] = aImportTab[i].usSel;
1425 *(unsigned short*)(void*)&callTab[i][0xa] = (unsigned short)aImportTab[i].offObject;
1426 *(unsigned short*)(void*)&callTab[i][0xc] = aImportTab[i].usSel;
1427 break;
1428
1429 default:
1430 kprintf(("ImportTabInit: unsupported type. (procedure no.%d, cb=%d)\n", i, cb));
1431 Int3(); /* ipe - later! */
1432 return ERROR_D32_VERIFY_FAILED;
1433 } /* switch - type */
1434 } /* for */
1435
1436 return NO_ERROR;
1437}
1438
1439
1440#ifdef R3TST
1441/**
1442 * Creates a fake kernel MTE, SMTE and OTE for use while testing in Ring3.
1443 * @returns Pointer to the fake kernel MTE.
1444 * @status completely implemented.
1445 * @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
1446 */
1447PMTE GetOS2KrnlMTETst(void)
1448{
1449 static MTE KrnlMTE;
1450 static SMTE KrnlSMTE;
1451
1452 KrnlMTE.mte_swapmte = &KrnlSMTE;
1453 KrnlSMTE.smte_objtab = &aKrnlOTE[0];
1454 KrnlSMTE.smte_objcnt = cObjectsFake;
1455
1456 return &KrnlMTE;
1457}
1458
1459/**
1460 * -Ring-3 testing-
1461 * Changes the entries in aImportTab to point to their fake equivalents.
1462 * @returns void
1463 * @param void
1464 * @status completely implemented.
1465 * @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
1466 * @remark Called before the aImportTab array is used/verified.
1467 */
1468VOID R3TstFixImportTab(VOID)
1469{
1470 struct _TstFaker
1471 {
1472 unsigned uAddress;
1473 int fObj; /* 1 = CODE32, 2 = CODE16, 3 = DATA32, 4 = DATA16 */
1474 }
1475 aTstFakers[NBR_OF_KRNLIMPORTS] =
1476 {
1477 {(unsigned)fakeldrRead, 1},
1478 {(unsigned)fakeldrOpen, 1},
1479 {(unsigned)fakeldrClose, 1},
1480 {(unsigned)fakeLDRQAppType, 1},
1481 {(unsigned)fakeldrEnum32bitRelRecs, 1},
1482 {(unsigned)fakeIOSftOpen, 1},
1483 {(unsigned)fakeIOSftClose, 1},
1484 {(unsigned)fakeIOSftTransPath, 1},
1485 {(unsigned)fakeIOSftReadAt, 1},
1486 {(unsigned)fakeIOSftWriteAt, 1},
1487 {(unsigned)fakeSftFileSize, 1},
1488 {(unsigned)fakeVMAllocMem, 1},
1489 {(unsigned)fakeVMGetOwner, 1},
1490 {(unsigned)fakeg_tkExecPgm, 1},
1491 {(unsigned)fake_tkStartProcess, 1},
1492 {(unsigned)fakef_FuStrLenZ, 2},
1493 {(unsigned)fakef_FuStrLen, 2},
1494 {(unsigned)fakef_FuBuff, 2},
1495 {(unsigned)fakeVMObjHandleInfo, 1},
1496 {(unsigned)fakeldrASMpMTEFromHandle,1},
1497 {(unsigned)fakeldrOpenPath, 1},
1498 {(unsigned)fakeLDRClearSem, 1},
1499 {(unsigned)fakeldrFindModule, 1},
1500 {(unsigned)fakeKSEMRequestMutex, 1},
1501 {(unsigned)fakeKSEMReleaseMutex, 1},
1502 {(unsigned)fakeKSEMQueryMutex, 1},
1503 {(unsigned)fakeKSEMInit, 1},
1504 {(unsigned)&fakeLDRSem, 3},
1505 {(unsigned)&fakeLDRLibPath, 3},
1506 {(unsigned)fakeTKSuBuff, 1},
1507 {(unsigned)fakeTKFuBuff, 1},
1508 {(unsigned)fakeTKFuBufLen, 1},
1509 {(unsigned)fakeldrValidateMteHandle,1},
1510 {(unsigned)&fakepTCBCur, 4},
1511 {(unsigned)&fakepPTDACur, 4},
1512 {(unsigned)&fakeptda_start, 4},
1513 {(unsigned)&fakeptda_environ, 4},
1514 {(unsigned)&fakeptda_ptdasem, 4},
1515 {(unsigned)&fakeptda_module, 4},
1516 {(unsigned)&fakeptda_pBeginLIBPATH, 4},
1517 {(unsigned)&fakeldrpFileNameBuf, 3},
1518 {(unsigned)&fakeSecPathFromSFN, 3}
1519 };
1520 int i;
1521
1522 for (i = 0; i < NBR_OF_KRNLIMPORTS; i++)
1523 {
1524 switch (aImportTab[i].fType)
1525 {
1526 case EPT_PROC32:
1527 if (aTstFakers[i].fObj != 1)
1528 kprintf(("R3TstFixImportTab: invalid segment config for entry %i. (PROC32)\n", i));
1529 break;
1530 case EPT_PROCIMPORT32:
1531 if (aTstFakers[i].fObj != 1)
1532 kprintf(("R3TstFixImportTab: invalid segment config for entry %i. (PROCIMPORT32)\n", i));
1533 break;
1534 case EPT_PROCIMPORT16:
1535 if (aTstFakers[i].fObj != 2)
1536 kprintf(("R3TstFixImportTab: invalid segment config for entry %i. (PROCIMPORT16)\n", i));
1537 break;
1538 case EPT_VARIMPORT32:
1539 case EPT_VARIMPORT16:
1540 if (aTstFakers[i].fObj != 3 && aTstFakers[i].fObj != 4)
1541 kprintf(("R3TstFixImportTab: invalid segment config for entry %i. (VARIMPORT32/16)\n", i));
1542 break;
1543 } /* switch - type */
1544
1545 aImportTab[i].ulAddress = aTstFakers[i].uAddress;
1546 switch (aTstFakers[i].fObj)
1547 {
1548 case 1:
1549 aImportTab[i].usSel = GetSelectorCODE32();
1550 aImportTab[i].offObject = aTstFakers[i].uAddress - (unsigned)&CODE32START;
1551 break;
1552 case 2:
1553 aImportTab[i].usSel = GetSelectorCODE16();
1554 aImportTab[i].offObject = aTstFakers[i].uAddress - (unsigned)&CODE16START;
1555 break;
1556 case 3:
1557 aImportTab[i].usSel = GetSelectorDATA32();
1558 aImportTab[i].offObject = aTstFakers[i].uAddress - (unsigned)&DATA32START;
1559 break;
1560 case 4:
1561 aImportTab[i].usSel = GetSelectorDATA16();
1562 aImportTab[i].offObject = aTstFakers[i].uAddress - (unsigned)&DATA16START;
1563 break;
1564 default:
1565 kprintf(("R3TstFixImportTab: invalid segment config for entry %i.\n", i));
1566 }
1567 } /* for */
1568}
1569#endif
1570
1571/**
1572 * Dummy nop function if SecPathFromSFN isn't found.
1573 */
1574PSZ SECCALL nopSecPathFromSFN(SFN hFile)
1575{
1576 NOREF(hFile);
1577 return NULL;
1578}
Note: See TracBrowser for help on using the repository browser.