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

Last change on this file since 6219 was 6219, checked in by bird, 24 years ago

Look for kernel type in the 'SAB KNL?' signature.

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