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

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

Added recognization of enter and exist kernel functions.

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