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

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

Calltable fixes. Handle event. New 14062e kernels.

File size: 63.8 KB
Line 
1/* $Id: d32init.c,v 1.38 2001-02-23 02:57:53 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 /*
536 * Thats all?
537 */
538 if (pKrnlInfo == NULL)
539 return NO_ERROR;
540
541 pKrnlInfo->cObjects = (unsigned char)pKrnlSMTE->smte_objcnt;
542
543 /*
544 * Copy OTEs
545 */
546 for (i = 0; i < pKrnlInfo->cObjects; i++)
547 {
548 memcpy((void*)&pKrnlInfo->aObjects[i], &pKrnlOTE[i], sizeof(OTE));
549 kprintf2(("GetKernelInfo32: %d base=0x%08x size=0x%08x flags=0x%08x\n",
550 i, pKrnlOTE[i].ote_base, pKrnlOTE[i].ote_size, pKrnlOTE[i].ote_flags));
551 }
552 usRc = 0;
553
554 /*
555 * Search for internal revision stuff in the two first objects.
556 */
557 pKrnlInfo->ulBuild = 0;
558 for (i = 0; i < 2 && pKrnlInfo->ulBuild == 0; i++)
559 {
560 const char *psz = (const char*)pKrnlOTE[i].ote_base;
561 const char *pszEnd = psz + pKrnlOTE[i].ote_size - 50; /* Last possible search position. */
562
563 while (psz < pszEnd)
564 {
565 if (strncmp(psz, "Internal revision ", 18) == 0 && (psz[18] >= '0' && psz[18] <= '9'))
566 {
567 int j;
568 kprintf2(("GetKernelInfo32: found internal revision: '%s'\n", psz));
569
570 /* skip to end of "Internal revision " string. */
571 psz += 18;
572
573 /* Read number*/
574 while ((*psz >= '0' && *psz <= '9') || *psz == '.')
575 {
576 if (*psz != '.')
577 pKrnlInfo->ulBuild = (unsigned short)(pKrnlInfo->ulBuild * 10 + (*psz - '0'));
578 psz++;
579 }
580
581 /* Check if build number seems valid. */
582 if ( !(pKrnlInfo->ulBuild >= 8254 && pKrnlInfo->ulBuild < 8383) /* Warp 3 fp 32 -> fp 60 */
583 && !(pKrnlInfo->ulBuild >= 9023 && pKrnlInfo->ulBuild <= 9036) /* Warp 4 GA -> fp 12 */
584 && !(pKrnlInfo->ulBuild >= 14039 && pKrnlInfo->ulBuild < 14100) /* Warp 4.5 GA -> fp 40 */
585 && !(pKrnlInfo->ulBuild >= 6600 && pKrnlInfo->ulBuild <= 6678) /* Warp 2.1x fix?? (just for fun!) */
586 )
587 {
588 kprintf(("GetKernelInfo32: info summary: Build %d is invalid - invalid fixpack?\n", pKrnlInfo->ulBuild));
589 usRc = ERROR_D32_INVALID_BUILD;
590 break;
591 }
592
593 /* Check for any revision flag */
594 pKrnlInfo->fKernel = 0;
595 if ((*psz >= 'A' && *psz <= 'E') || (*psz >= 'a' && *psz <= 'e'))
596 {
597 pKrnlInfo->fKernel = (USHORT)((*psz - (*psz >= 'a' ? 'a'-1 : 'A'-1)) << KF_REV_SHIFT);
598 psz++;
599 }
600 if (*psz == 'F' || *psz == 'f' || *psz == ',') /* These are ignored! */
601 *psz++;
602
603 /* If this is an Aurora/Warp 4.5 or Warp 3 kernel there is more info! */
604 if (psz[0] == '_' && (psz[1] == 'S' || psz[1] == 's')) /* _SMP */
605 pKrnlInfo->fKernel |= KF_SMP;
606 else
607 if (*psz != ','
608 && ( (psz[0] == '_' && psz[1] == 'W' && psz[2] == '4') /* _W4 */
609 || (psz[0] == '_' && psz[1] == 'U' && psz[2] == 'N' && psz[3] == 'I' && psz[4] == '4') /* _UNI4 */
610 )
611 )
612 pKrnlInfo->fKernel |= KF_W4 | KF_UNI;
613 else
614 pKrnlInfo->fKernel |= KF_UNI;
615
616
617 /* Check if its a debug kernel (look for DEBUG at start of object 3-5) */
618 j = 3;
619 while (j < 5)
620 {
621 /* There should be no iopl object preceding the debugger data object. */
622 if ((pKrnlOTE[j].ote_flags & OBJIOPL) != 0)
623 break;
624 /* Is this is? */
625 if ((pKrnlOTE[j].ote_flags & OBJINVALID) == 0
626 && (pKrnlOTE[j].ote_flags & (OBJREAD | OBJWRITE)) == (OBJREAD | OBJWRITE)
627 && strncmp((char*)pKrnlOTE[j].ote_base, "DEBUG", 5) == 0)
628 {
629 pKrnlInfo->fKernel |= KF_DEBUG;
630 break;
631 }
632 j++;
633 }
634
635 /* Display info */
636 kprintf(("GetKernelInfo32: info summary: Build %d, fKernel=0x%x\n",
637 pKrnlInfo->ulBuild, pKrnlInfo->fKernel));
638
639 /* Break out */
640 break;
641 }
642
643 /* next */
644 psz++;
645 } /* while loop searching for "Internal revision " */
646 } /* for loop on objects 0-1. */
647
648 /* Set error code if not found */
649 if (pKrnlInfo->ulBuild == 0)
650 {
651 usRc = ERROR_D32_BUILD_INFO_NOT_FOUND;
652 kprintf(("GetKernelInfo32: Internal revision was not found!\n"));
653 }
654 }
655 else
656 usRc = ERROR_D32_NO_OBJECT_TABLE;
657 }
658 else
659 usRc = ERROR_D32_TOO_MANY_OBJECTS;
660 }
661 else
662 usRc = ERROR_D32_NO_SWAPMTE;
663 }
664 else
665 usRc = ERROR_D32_GETOS2KRNL_FAILED;
666
667 if (usRc != NO_ERROR)
668 kprintf(("GetKernelInfo32: failed. usRc = %d\n", usRc));
669
670 return (USHORT)(usRc | (usRc != NO_ERROR ? STATUS_DONE | STERR : STATUS_DONE));
671}
672
673
674
675/**
676 * Functions which cacluates the instructionsize given a ModR/M byte.
677 * @returns Number of bytes to add to cb and pach.
678 * @param bModRM ModR/M byte.
679 * @status completely implemented.
680 * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
681 */
682int ModR_M_32bit(char bModRM)
683{
684 if ((bModRM & 0xc0) == 0x80 /* ex. mov ax,[ebp+11145543h] */
685 || ((bModRM & 0xc0) == 0 && (bModRM & 0x07) == 5)) /* ex. mov ebp,[0ff231234h] */
686 { /* 32-bit displacement */
687 return 5 + ((bModRM & 0x7) == 0x4); // + SIB
688 }
689 else if ((bModRM & 0xc0) == 0x40) /* ex. mov ecx,[esi]+4fh */
690 { /* 8-bit displacement */
691 return 2 + ((bModRM & 0x7) == 0x4); // + SIB
692 }
693 /* no displacement (only /r byte) */
694 return 1;
695}
696
697
698/**
699 * Functions which cacluates the instructionsize given a ModR/M byte.
700 * @returns Number of bytes to add to cb and pach.
701 * @param bModRM ModR/M byte.
702 * @status completely implemented.
703 * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
704 */
705int ModR_M_16bit(char bModRM)
706{
707 if ((bModRM & 0xc0) == 0x80 /* ex. mov ax,[ebp+11145543h] */
708 || ((bModRM & 0xc0) == 0 && (bModRM & 0x07) == 5)) /* ex. mov ebp,[0ff231234h] */
709 { /* 16-bit displacement */
710 return 4;
711 }
712 else if ((bModRM & 0xc0) == 0x40) /* ex. mov ecx,[esi]+4fh */
713 { /* 8-bit displacement */
714 return 2;
715 }
716 /* no displacement (only /r byte) */
717 return 1;
718}
719
720
721
722
723
724/**
725 * 32-bit! Interpret function prolog to find where to jmp back.
726 * @returns Length of prolog need to be copied - which is also the offset of
727 * where the jmp instr should be placed.
728 * On error it returns 0.
729 * @param pach Pointer to prolog.
730 * @param fOverload TRUE: Function is to be overloaded.
731 * FALSE: Function is to be imported.
732 */
733int interpretFunctionProlog32(char *pach, BOOL fOverload)
734{
735 int cb = -3;
736 kprintf2(("interpretFunctionProlog32(0x%08x, %d):\n"
737 "\t%02x %02x %02x %02x - %02x %02x %02x %02x\n"
738 "\t%02x %02x %02x %02x - %02x %02x %02x %02x\n",
739 pach, fOverload,
740 pach[0], pach[1], pach[2], pach[3], pach[4], pach[5], pach[6], pach[7],
741 pach[8], pach[9], pach[10],pach[11],pach[12],pach[13],pach[14],pach[15]));
742
743 /*
744 * check for the well known prolog (the only that is supported now)
745 * which is:
746 * push ebp
747 * mov ebp,esp
748 * or
749 * push ebp
750 * mov eax, dword ptr [xxxxxxxx]
751 * or
752 * sub esp, imm8
753 * push ebx
754 * push edi
755 *
756 * These are allowed when not overloading:
757 * mov eax, imm32
758 * jmp short
759 * or
760 * mov eax, imm32
761 * push ebp
762 * or
763 * mov ecx, r/m32
764 * or
765 * jmp dword
766 * or
767 * sub esp, imm8
768 * or
769 * call ptr16:32
770 * or
771 * enter imm16, imm8 (2.1x)
772 * or
773 * mov eax, imm32 (2.1x)
774 * <anything>
775 * or
776 * xor r32, r/m32
777 * or
778 * mov eax, msoff32
779 * or
780 * push edi
781 * mov eax, dword ptr [xxxxxxxx]
782 * or
783 * movzx esp, sp
784 * or
785 * call rel32
786 * popf
787 */
788 if ((pach[0] == 0x55 && (pach[1] == 0x8b || pach[1] == 0xa1)) /* the two first prologs */
789 ||
790 (pach[0] == 0x83 && pach[3] == 0x53 && pach[4] == 0x57) /* the third prolog */
791 ||
792 (pach[0] == 0xB8 && (pach[5] == 0xEB || pach[5] == 0x55) && !fOverload) /* the two next prologs */
793 ||
794 (pach[0] == 0x8B && !fOverload) /* the next prolog */
795 ||
796 (pach[0] == 0xFF && !fOverload) /* the next prolog */
797 ||
798 (pach[0] == 0x83 && !fOverload) /* the next prolog */
799 ||
800 (pach[0] == 0x9a && !fOverload) /* the next prolog */
801 ||
802 (pach[0] == 0xc8) /* the next prolog */
803 ||
804 (pach[0] == 0xB8 && !fOverload) /* the next prolog */
805 ||
806 (pach[0] == 0x33 && !fOverload) /* the next prolog */
807 ||
808 (pach[0] == 0xa1 && !fOverload) /* the next prolog */
809 ||
810 (pach[0] == 0x57 && pach[1] == 0x8b && !fOverload) /* the next prolog */
811 ||
812 (pach[0] == 0x0f && pach[1] == 0xb7 && pach[2] == 0xe4 && !fOverload) /* the next prolog */
813 ||
814 (pach[0] == 0xe8 && pach[5] == 0x9d && !fOverload) /* the last prolog */
815 )
816 {
817 BOOL fForce = FALSE;
818 int cbWord = 4;
819 cb = 0;
820 while (cb < 5 || fForce) /* 5 is the size of a jump instruction. */
821 {
822 int cb2;
823 if (!fForce && cbWord != 4)
824 cbWord = 4;
825 fForce = FALSE;
826 switch (*pach)
827 {
828 case 0x0f:
829 if (pach[1] != 0xb7 && pach[2] != 0xe4) /* movzx esp, sp */
830 {
831 kprintf(("interpretFunctionProlog32: unknown instruction 0x%x 0x%x 0x%x\n", pach[0], pach[1], pach[2]));
832 return -11;
833 }
834 pach += 2;
835 cb += 2;
836 break;
837
838
839 /* simple one byte prefixes */
840 case 0x2e: /* cs segment override */
841 case 0x36: /* ss segment override */
842 case 0x3e: /* ds segment override */
843 case 0x26: /* es segment override */
844 case 0x64: /* fs segment override */
845 case 0x65: /* gs segment override */
846 fForce = TRUE;
847 break;
848
849 case 0x66: /* 16 bit */
850 fForce = TRUE;
851 cbWord = 2;
852 break;
853
854 /* simple one byte instructions */
855 case 0x50: /* push ax */
856 case 0x51: /* push cx */
857 case 0x52: /* push dx */
858 case 0x53: /* push bx */
859 case 0x54: /* push sp */
860 case 0x55: /* push bp */
861 case 0x56: /* push si */
862 case 0x57: /* push di */
863 case 0x06: /* push es */
864 case 0x0e: /* push cs */
865 case 0x1e: /* push ds */
866 case 0x16: /* push ss */
867 break;
868
869 /* simple two byte instructions */
870 case 0xb0: /* mov al, imm8 */
871 case 0xb1: /* mov cl, imm8 */
872 case 0xb2: /* mov dl, imm8 */
873 case 0xb3: /* mov bl, imm8 */
874 case 0xb4: /* mov ah, imm8 */
875 case 0xb5: /* mov ch, imm8 */
876 case 0xb6: /* mov dh, imm8 */
877 case 0xb7: /* mov bh, imm8 */
878 case 0x2c: /* sub al, imm8 */
879 case 0x34: /* xor al, imm8 */
880 case 0x3c: /* cmp al, imm8 */
881 case 0x6a: /* push <byte> */
882 case 0xa0: /* mov al, moffs8 */
883 case 0xa2: /* mov moffs8, al */
884 pach++;
885 cb++;
886 break;
887
888 /* simple five byte instructions */
889 case 0xb8: /* mov eax, imm32 */
890 case 0xb9: /* mov ecx, imm32 */
891 case 0xba: /* mov edx, imm32 */
892 case 0xbb: /* mov ebx, imm32 */
893 case 0xbc: /* mov esx, imm32 */
894 case 0xbd: /* mov ebx, imm32 */
895 case 0xbe: /* mov esi, imm32 */
896 case 0xbf: /* mov edi, imm32 */
897 case 0x2d: /* sub eax, imm32 */
898 case 0x35: /* xor eax, imm32 */
899 case 0x3d: /* cmp eax, imm32 */
900 case 0x68: /* push <dword> */
901 case 0xa1: /* mov eax, moffs16 */
902 case 0xa3: /* mov moffs16, eax */
903 pach += cbWord;
904 cb += cbWord;
905 break;
906
907 /* fixed five byte instructions */
908 case 0xe8: /* call imm32 */
909 pach += 4;
910 cb += 4;
911 break;
912
913 /* complex sized instructions - "/r" */
914 case 0x30: /* xor r/m8, r8 */
915 case 0x31: /* xor r/m32, r32 */
916 case 0x32: /* xor r8, r/m8 */
917 case 0x33: /* xor r32, r/m32 */
918 case 0x38: /* cmp r/m8, r8 */
919 case 0x39: /* cmp r/m32, r32 */
920 case 0x3a: /* cmp r8, r/m8 */
921 case 0x3b: /* cmp r32, r/m32 */
922 case 0x28: /* sub r/m8, r8 */
923 case 0x29: /* sub r/m32, r32 */
924 case 0x2a: /* sub r8, r/m8 */
925 case 0x2b: /* sub r32, r/m32 */
926 case 0x8b: /* mov /r */
927 case 0x8d: /* lea /r */
928 cb += cb2 = ModR_M_32bit(pach[1]);
929 pach += cb2;
930 break;
931
932 /* complex sized instruction - "/5 ib" */
933 case 0x80: /* 5: sub r/m8, imm8 7: cmp r/m8, imm8 */
934 case 0x83: /* 5: sub r/m32, imm8 7: cmp r/m32, imm8 */
935 if ((pach[1] & 0x38) == (5<<3)
936 || (pach[1] & 0x38) == (7<<3)
937 )
938 {
939 cb += cb2 = 1 + ModR_M_32bit(pach[1]); /* 1 is the size of the imm8 */
940 pach += cb2;
941 }
942 else
943 {
944 kprintf(("interpretFunctionProlog32: unknown instruction (-3) 0x%x 0x%x 0x%x\n", pach[0], pach[1], pach[2]));
945 return -3;
946 }
947 break;
948
949 /* complex sized instruction - "/digit id" */
950 case 0x81: /* sub r/m32, imm32 + more instructions! */
951 if ((pach[1] & 0x38) == (5<<3) /* sub r/m32, imm32 */
952 || (pach[1] & 0x38) == (7<<3) /* cmp r/m32, imm32 */
953 )
954 {
955 cb += cb2 = cbWord + ModR_M_32bit(pach[1]); /* cbWord is the size of the imm32/imm16 */
956 pach += cb2;
957 }
958 else
959 {
960 kprintf(("interpretFunctionProlog32: unknown instruction (-2) 0x%x 0x%x 0x%x\n", pach[0], pach[1], pach[2]));
961 return -2;
962 }
963 break;
964
965 case 0x9a: /* call ptr16:32 */
966 cb += cb2 = 6;
967 pach += cb2;
968 break;
969
970 case 0xc8: /* enter imm16, imm8 */
971 cb += cb = 3;
972 pach += cb2;
973 break;
974
975 /*
976 * jmp /digit
977 */
978 case 0xff:
979 cb += cb2 = cbWord + ModR_M_32bit(pach[1]); /* cbWord is the size of the imm32/imm16 */
980 pach += cb2;
981 break;
982
983 default:
984 kprintf(("interpretFunctionProlog32: unknown instruction 0x%x 0x%x 0x%x\n", pach[0], pach[1], pach[2]));
985 return 0;
986 }
987 pach++;
988 cb++;
989 }
990 }
991 else
992 {
993 kprintf(("interpretFunctionProlog32: unknown prolog start. 0x%x 0x%x 0x%x 0x%x 0x%x\n",
994 pach[0], pach[1], pach[2], pach[3], pach[4]));
995 cb = 0;
996 }
997 return cb;
998}
999
1000
1001/**
1002 * 16-bit! Interpret function prolog to find where to jmp back.
1003 * @returns Length of prolog need to be copied - which is also the offset of
1004 * where the jmp instr should be placed.
1005 * On error it returns 0.
1006 * @param pach Pointer to prolog.
1007 * @param fOverload TRUE: Function is to be overloaded.
1008 * FALSE: Function is to be imported.
1009 */
1010int interpretFunctionProlog16(char *pach, BOOL fOverload)
1011{
1012 int cb = -7;
1013
1014 kprintf2(("interpretFunctionProlog16(0x%08x, %d):\n"
1015 "\t%02x %02x %02x %02x - %02x %02x %02x %02x\n"
1016 "\t%02x %02x %02x %02x - %02x %02x %02x %02x\n",
1017 pach, fOverload,
1018 pach[0], pach[1], pach[2], pach[3], pach[4], pach[5], pach[6], pach[7],
1019 pach[8], pach[9], pach[10],pach[11],pach[12],pach[13],pach[14],pach[15]));
1020 /*
1021 * Check for the well known prolog (the only that is supported now)
1022 * which is:
1023 */
1024 if ((*pach == 0x6A && !fOverload) /* push 2 (don't check for the 2) */
1025 ||
1026 *pach == 0x60 /* pushf */
1027 ||
1028 (*pach == 0x53 && pach[1] == 0x51) /* push bx, push cx */
1029 ||
1030 (*pach == 0x8c && pach[1] == 0xd8) /* mov ax, ds */
1031 ||
1032 (*pach == 0xb8) /* mov ax, imm16 */
1033 )
1034 {
1035 BOOL fForce;
1036 int cOpPrefix = 0;
1037 cb = 0;
1038 while (cb < 5 || fForce) /* 5 is the size of a 16:16 far jump instruction. */
1039 {
1040 int cb2;
1041 fForce = FALSE;
1042 switch (*pach)
1043 {
1044 case 0x06: /* push es */
1045 case 0x0e: /* push cs */
1046 case 0x1e: /* push ds */
1047 case 0x16: /* push ss */
1048 break;
1049
1050 case 0x0f: /* push gs and push fs */
1051 if (pach[1] != 0xA0 && pach[1] != 0xA8)
1052 {
1053 kprintf(("interpretFunctionProlog16: unknown instruction 0x%x 0x%x 0x%x\n", pach[0], pach[1], pach[2]));
1054 return -11;
1055 }
1056 pach++;
1057 cb++;
1058 break;
1059
1060 case 0x50: /* push ax */
1061 case 0x51: /* push cx */
1062 case 0x52: /* push dx */
1063 case 0x53: /* push bx */
1064 case 0x54: /* push sp */
1065 case 0x55: /* push bp */
1066 case 0x56: /* push si */
1067 case 0x57: /* push di */
1068 case 0x60: /* pusha */
1069 break;
1070
1071 /* simple three byte instructions */
1072 case 0xb8: /* mov eax, imm16 */
1073 case 0xb9: /* mov ecx, imm16 */
1074 case 0xba: /* mov edx, imm16 */
1075 case 0xbb: /* mov ebx, imm16 */
1076 case 0xbc: /* mov esx, imm16 */
1077 case 0xbd: /* mov ebx, imm16 */
1078 case 0xbe: /* mov esi, imm16 */
1079 case 0xbf: /* mov edi, imm16 */
1080 case 0x2d: /* sub eax, imm16 */
1081 case 0x35: /* xor eax, imm16 */
1082 case 0x3d: /* cmp eax, imm16 */
1083 case 0x68: /* push <dword> */
1084 case 0xa1: /* mov eax, moffs16 */
1085 case 0xa3: /* mov moffs16, eax */
1086 if (cOpPrefix > 0) /* FIXME see 32-bit interpreter. */
1087 {
1088 pach += 2;
1089 cb += 2;
1090 }
1091 pach += 2;
1092 cb += 2;
1093 break;
1094
1095 case 0x2e: /* cs segment override */
1096 case 0x36: /* ss segment override */
1097 case 0x3e: /* ds segment override */
1098 case 0x26: /* es segment override */
1099 case 0x64: /* fs segment override */
1100 case 0x65: /* gs segment override */
1101 fForce = TRUE;
1102 if (cOpPrefix > 0)
1103 cOpPrefix++;
1104 break;
1105
1106 case 0x66:
1107 cOpPrefix = 2; /* it's decremented once before it's used. */
1108 fForce = TRUE;
1109 break;
1110
1111 case 0x6a: /* push <byte> */
1112 case 0x3c: /* mov al, imm8 */
1113 pach++;
1114 cb++;
1115 break;
1116
1117 case 0x8b: /* mov /r */
1118 case 0x8c: /* mov r/m16,Sreg (= mov /r) */
1119 case 0x8e: /* mov Sreg, r/m16 (= mov /r) */
1120 if ((pach[1] & 0xc0) == 0x80 /* ex. mov ax,bp+1114h */
1121 || ((pach[1] & 0xc0) == 0 && (pach[1] & 0x7) == 6)) /* ex. mov bp,0ff23h */
1122 { /* 16-bit displacement */
1123 if (cOpPrefix > 0)
1124 {
1125 pach += 2;
1126 cb += 2;
1127 }
1128 pach += 3;
1129 cb += 3;
1130 }
1131 else
1132 if ((pach[1] & 0xc0) == 0x40) /* ex. mov ax,[si]+4fh */
1133 { /* 8-bit displacement */
1134 pach += 2;
1135 cb += 2;
1136 }
1137 else
1138 { /* no displacement (only /r byte) */
1139 pach++;
1140 cb++;
1141 }
1142 break;
1143
1144 /* complex sized instruction - "/5 ib" */
1145 case 0x80: /* 5: sub r/m8, imm8 7: cmp r/m8, imm8 */
1146 case 0x83: /* 5: sub r/m16, imm8 7: cmp r/m16, imm8 */
1147 if ((pach[1] & 0x38) == (5<<3)
1148 || (pach[1] & 0x38) == (7<<3)
1149 )
1150 {
1151 cb += cb2 = 1 + ModR_M_16bit(pach[1]); /* 1 is the size of the imm8 */
1152 pach += cb2;
1153 }
1154 else
1155 {
1156 kprintf(("interpretFunctionProlog16: unknown instruction (-3) 0x%x 0x%x 0x%x\n", pach[0], pach[1], pach[2]));
1157 return -3;
1158 }
1159 break;
1160
1161
1162 default:
1163 kprintf(("interpretFunctionProlog16: unknown instruction 0x%x 0x%x 0x%x\n", pach[0], pach[1], pach[2]));
1164 return 0;
1165 }
1166 pach++;
1167 cb++;
1168 if (cOpPrefix > 0)
1169 cOpPrefix--;
1170 }
1171 }
1172 else
1173 kprintf(("interpretFunctionProlog16: unknown prolog 0x%x 0x%x 0x%x\n", pach[0], pach[1], pach[2]));
1174
1175
1176 fOverload = fOverload;
1177 return cb;
1178}
1179
1180
1181/**
1182 * Verifies the aImportTab.
1183 * @returns 16-bit errorcode where the high byte is the procedure number which
1184 * the error occured on and the low byte the error code.
1185 * @remark Called from IOCtl.
1186 * WARNING! This function is called before the initroutine (R0INIT)!
1187 */
1188USHORT _loadds _Far32 _Pascal VerifyImportTab32(void)
1189{
1190 USHORT usRc;
1191 int i;
1192 int cb;
1193 int cbmax;
1194
1195 /* VerifyImporTab32 is called before the initroutine! */
1196 pulTKSSBase32 = (PULONG)_TKSSBase16;
1197
1198 /* Check that pKrnlOTE is set */
1199 usRc = GetKernelInfo32(NULL);
1200 if (usRc != NO_ERROR)
1201 return usRc;
1202
1203 /*
1204 * Verify aImportTab.
1205 */
1206 for (i = 0; i < NBR_OF_KRNLIMPORTS; i++)
1207 {
1208 /*
1209 * Debug info
1210 */
1211 kprintf2(("VerifyImportTab32: procedure no.%d is being checked: %s addr=0x%08x iObj=%d offObj=%d\n",
1212 i, &aImportTab[i].achName[0], aImportTab[i].ulAddress,
1213 aImportTab[i].iObject, aImportTab[i].offObject));
1214
1215 /* Verify that it is found */
1216 if (!aImportTab[i].fFound)
1217 {
1218 if (EPTNotReq(aImportTab[i]))
1219 continue;
1220 else
1221 {
1222 kprintf(("VerifyImportTab32: procedure no.%d was not fFound!\n", i));
1223 return (USHORT)(ERROR_D32_PROC_NOT_FOUND | (i << ERROR_D32_PROC_SHIFT) | ERROR_D32_PROC_FLAG);
1224 }
1225 }
1226
1227 /* Verify read/writeable. */
1228 if ( aImportTab[i].iObject >= pKrnlSMTE->smte_objcnt /* object index valid? */
1229 || aImportTab[i].ulAddress < pKrnlOTE[aImportTab[i].iObject].ote_base /* address valid? */
1230 || aImportTab[i].ulAddress + 16 > (pKrnlOTE[aImportTab[i].iObject].ote_base +
1231 pKrnlOTE[aImportTab[i].iObject].ote_size) /* address valid? */
1232 || aImportTab[i].ulAddress - aImportTab[i].offObject
1233 != pKrnlOTE[aImportTab[i].iObject].ote_base /* offObject ok? */
1234 )
1235 {
1236 kprintf(("VerifyImportTab32: procedure no.%d has an invalid address or object number.!\n"
1237 " %s addr=0x%08x iObj=%d offObj=%d\n",
1238 i, &aImportTab[i].achName[0], aImportTab[i].ulAddress,
1239 aImportTab[i].iObject, aImportTab[i].offObject));
1240 return (USHORT)(ERROR_D32_INVALID_OBJ_OR_ADDR | (i << ERROR_D32_PROC_SHIFT) | ERROR_D32_PROC_FLAG);
1241 }
1242
1243
1244 #ifndef R3TST
1245 if (aImportTab[i].ulAddress < 0xff400000UL)
1246 {
1247 kprintf(("VerifyImportTab32: procedure no.%d has an invalid address, %#08x!\n",
1248 i, aImportTab[i].ulAddress));
1249 return (USHORT)(ERROR_D32_INVALID_ADDRESS | (i << ERROR_D32_PROC_SHIFT) | ERROR_D32_PROC_FLAG);
1250 }
1251 #endif
1252
1253 switch (aImportTab[i].fType & ~(EPT_BIT_MASK | EPT_NOT_REQ | EPT_WRAPPED))
1254 {
1255 case EPT_PROC:
1256 case EPT_PROCIMPORT:
1257 /*
1258 * Verify known function prolog.
1259 */
1260 if (EPT32BitEntry(aImportTab[i]))
1261 {
1262 cb = interpretFunctionProlog32((char*)aImportTab[i].ulAddress, EPT32Proc(aImportTab[i]));
1263 cbmax = OVERLOAD32_ENTRY - 5; /* 5 = Size of the jump instruction */
1264 }
1265 else
1266 {
1267 cb = interpretFunctionProlog16((char*)aImportTab[i].ulAddress, EPT16Proc(aImportTab[i]));
1268 cbmax = OVERLOAD16_ENTRY - 5; /* 5 = Size of the jump instruction */
1269 }
1270
1271 /*
1272 * Check result of the function prolog interpretations.
1273 */
1274 if (cb <= 0 || cb > cbmax)
1275 { /* failed, too small or too large. */
1276 kprintf(("VerifyImportTab32/16: verify failed for procedure no.%d (cb=%d), %s\n", i, cb, aImportTab[i].achName));
1277 return (USHORT)(ERROR_D32_TOO_INVALID_PROLOG | (i << ERROR_D32_PROC_SHIFT) | ERROR_D32_PROC_FLAG);
1278 }
1279 break;
1280
1281 case EPT_VARIMPORT:
1282 /* do nothing! */
1283 break;
1284
1285 default:
1286 kprintf(("VerifyImportTab32: invalid type/type not implemented. Proc no.%d, %s\n",i, aImportTab[i].achName));
1287 Int3(); /* temporary fix! */
1288 return (USHORT)(ERROR_D32_NOT_IMPLEMENTED | (i << ERROR_D32_PROC_SHIFT) | ERROR_D32_PROC_FLAG);
1289 }
1290 }
1291
1292 return NO_ERROR;
1293}
1294
1295
1296/**
1297 * Initiates the overrided functions.
1298 * @returns 16-bit errorcode where the high byte is the procedure number which
1299 * the error occured on and the low byte the error code.
1300 */
1301int importTabInit(void)
1302{
1303 int i;
1304 int cb;
1305 int cbmax;
1306 char * pchCTEntry; /* Pointer to current 32-bit calltab entry. */
1307 char * pchCTEntry16; /* Pointer to current 16-bit calltab entry. */
1308 ULONG flWP; /* CR0 WP flag restore value. */
1309
1310 /*
1311 * Apply build specific changes to the auFuncs table
1312 */
1313 if (options.ulBuild < 14053)
1314 {
1315 #ifdef DEBUG
1316 if (auFuncs[0] != (unsigned)myldrOpenPath)
1317 {
1318 kprintf(("importTabInit: ASSERTION FAILED auFuncs don't point at myldrOpenPath\n"));
1319 Int3();
1320 }
1321 #endif
1322 auFuncs[0] = (unsigned)myldrOpenPath_old;
1323 }
1324
1325#ifdef R3TST
1326 R3TstFixImportTab();
1327#endif
1328
1329 /*
1330 * verify proctable
1331 */
1332 for (i = 0; i < NBR_OF_KRNLIMPORTS; i++)
1333 {
1334 /* EPT_VARIMPORTs are skipped */
1335 if ((aImportTab[i].fType & ~(EPT_BIT_MASK | EPT_NOT_REQ)) == EPT_VARIMPORT)
1336 continue;
1337 /* EPT_NOT_REQ which is not found are set pointing to the nop function provided. */
1338 if (!aImportTab[i].fFound && EPTNotReq(aImportTab[i]))
1339 continue;
1340
1341 if (EPT32BitEntry(aImportTab[i]))
1342 {
1343 cb = interpretFunctionProlog32((char*)aImportTab[i].ulAddress, EPT32Proc(aImportTab[i]));
1344
1345 cbmax = OVERLOAD16_ENTRY - 5; /* 5 = Size of the jump instruction */
1346 }
1347 else
1348 {
1349 cb = interpretFunctionProlog16((char*)aImportTab[i].ulAddress, EPT16Proc(aImportTab[i]));
1350 cbmax = OVERLOAD16_ENTRY - 5; /* 5 = Size of the jump instruction */
1351 }
1352 if (cb <= 0 || cb > cbmax)
1353 {
1354 kprintf(("ImportTabInit: Verify failed for procedure no.%d, cb=%d\n", i, cb));
1355 return ERROR_D32_VERIFY_FAILED | (i << ERROR_D32_PROC_SHIFT) | ERROR_D32_PROC_FLAG;
1356 }
1357 }
1358
1359 /*
1360 * rehook / import
1361 */
1362 pchCTEntry = &callTab[0];
1363 pchCTEntry16 = &callTab16[0];
1364 flWP = x86DisableWriteProtect();
1365 for (i = 0; i < NBR_OF_KRNLIMPORTS; i++)
1366 {
1367 switch (aImportTab[i].fType & ~EPT_WRAPPED)
1368 {
1369 /*
1370 * 32-bit procedure overload.
1371 * The overloading procedure is found in the auFuncs table (at the same index
1372 * as the overloaded procedure has in aImportTab).
1373 * The overloaded procedure is called by issuing a call to the callTab entry.
1374 */
1375 case EPT_PROC32:
1376 {
1377 cb = interpretFunctionProlog32((char*)aImportTab[i].ulAddress, TRUE);
1378 aImportTab[i].cbProlog = (char)cb;
1379 if (cb >= 5 && cb + 5 < OVERLOAD32_ENTRY) /* 5(1st): size of jump instruction in the function prolog which jumps to my overloading function */
1380 { /* 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. */
1381 /*
1382 * Copy function prolog which will be overwritten by the jmp to calltabl.
1383 */
1384 memcpy(pchCTEntry, (void*)aImportTab[i].ulAddress, (size_t)cb);
1385
1386 /*
1387 * Make jump instruction which jumps from calltab to original function.
1388 * 0xE9 <four bytes displacement>
1389 * Note: the displacement is relative to the next instruction
1390 */
1391 pchCTEntry[cb] = 0xE9; /* jmp */
1392 *(unsigned long*)(void*)&pchCTEntry[cb+1] = aImportTab[i].ulAddress + cb - (unsigned long)&pchCTEntry[cb+5];
1393
1394 /*
1395 * Jump from original function to my function - an cli(?) could be needed here
1396 */
1397 *(char*)aImportTab[i].ulAddress = 0xE9; /* jmp */
1398 *(unsigned long*)(aImportTab[i].ulAddress + 1) = auFuncs[i] - (aImportTab[i].ulAddress + 5);
1399 }
1400 else
1401 { /* !fatal! - this could never happen really... */
1402 kprintf(("ImportTabInit: FATAL verify failed for procedure no.%d when rehooking it!\n", i));
1403 Int3(); /* ipe - later! */
1404 x86RestoreWriteProtect(flWP);
1405 return ERROR_D32_IPE | (i << ERROR_D32_PROC_SHIFT) | ERROR_D32_PROC_FLAG;
1406 }
1407 pchCTEntry += OVERLOAD32_ENTRY;
1408 break;
1409 }
1410
1411
1412 /*
1413 * 16-bit procedure overload.
1414 * Currently disabled due to expected problems when calltab is a 32-bit segment.
1415 */
1416 case EPT_PROC16:
1417 {
1418 cb = interpretFunctionProlog16((char*)aImportTab[i].ulAddress, TRUE);
1419 aImportTab[i].cbProlog = (char)cb;
1420 if (cb >= 5 && cb + 5 < OVERLOAD16_ENTRY) /* 5: size of a 16:16 jump which jumps to my overloading function */
1421 { /* cb+5: size of a 16:16 jump which is added to the call tab */
1422 /*
1423 * Copy function prolog which is to be overwritten.
1424 */
1425 memcpy(pchCTEntry16, (void*)aImportTab[i].ulAddress, (size_t)cb);
1426
1427 /*
1428 * Create far jump from calltab to original function.
1429 * 0xEA <two byte target address> <two byte target selector>
1430 */
1431 pchCTEntry16[cb] = 0xEA; /* jmp far ptr */
1432 *(unsigned short*)(void*)&pchCTEntry16[cb+1] = (unsigned short)aImportTab[i].offObject + cb;
1433 *(unsigned short*)(void*)&pchCTEntry16[cb+3] = aImportTab[i].usSel;
1434
1435 /*
1436 * We store the far 16:16 pointer to the function in the last four
1437 * bytes of the entry. Set them!
1438 */
1439 *(unsigned short*)(void*)&pchCTEntry16[OVERLOAD16_ENTRY-4] = (unsigned short)aImportTab[i].offObject;
1440 *(unsigned short*)(void*)&pchCTEntry16[OVERLOAD16_ENTRY-2] = aImportTab[i].usSel;
1441
1442 /*
1443 * jump from original function to my function - an cli(?) could be needed here
1444 * 0xEA <two byte target address> <two byte target selector>
1445 */
1446 *(char*)(aImportTab[i].ulAddress) = 0xEA; /* jmp far ptr */
1447 *(unsigned long*)(aImportTab[i].ulAddress + 1) = auFuncs[i]; /* The auFuncs entry is a far pointer. */
1448 }
1449 else
1450 { /* !fatal! - this could never happen really... */
1451 kprintf(("ImportTabInit: FATAL verify failed for procedure no.%d when rehooking it!\n", i));
1452 Int3(); /* ipe - later! */
1453 x86RestoreWriteProtect(flWP);
1454 return ERROR_D32_IPE | (i << ERROR_D32_PROC_SHIFT) | ERROR_D32_PROC_FLAG;
1455 }
1456 pchCTEntry16 += OVERLOAD16_ENTRY;
1457 break;
1458 }
1459
1460
1461 /*
1462 * 32-bit imported procedure.
1463 * This is called by issuing a near call to the callTab entry.
1464 */
1465 case EPT_PROCIMPORTNR32: /* Not required */
1466 if (!(pchCTEntry[6] = aImportTab[i].fFound))
1467 aImportTab[i].ulAddress = auFuncs[i];
1468 case EPT_PROCIMPORT32:
1469 {
1470 cb = interpretFunctionProlog32((char*)aImportTab[i].ulAddress, FALSE);
1471 aImportTab[i].cbProlog = (char)cb;
1472 if (cb > 0) /* Since no prolog part is copied to the function table, it's ok as long as the prolog has been recognzied. */
1473 {
1474 /*
1475 * Make jump instruction which jumps from calltab to original function.
1476 * 0xE9 <four bytes displacement>
1477 * Note: the displacement is relative to the next instruction
1478 */
1479 pchCTEntry[0] = 0xE9; /* jmp */
1480 *(unsigned*)(void*)&pchCTEntry[1] = aImportTab[i].ulAddress - (unsigned)&pchCTEntry[5];
1481 }
1482 else
1483 { /* !fatal! - this should never really happen... */
1484 kprintf(("ImportTabInit: FATAL verify failed for procedure no.%d when importing it!\n", i));
1485 Int3(); /* ipe - later! */
1486 x86RestoreWriteProtect(flWP);
1487 return ERROR_D32_IPE | (i << ERROR_D32_PROC_SHIFT) | ERROR_D32_PROC_FLAG;
1488 }
1489 pchCTEntry += IMPORT32_ENTRY;
1490 break;
1491 }
1492
1493
1494 /*
1495 * 16-bit imported procedure.
1496 * This is called by issuing a far call to the calltab entry.
1497 */
1498 case EPT_PROCIMPORTNR16: /* Not required */
1499 if (!(pchCTEntry[7] = aImportTab[i].fFound))
1500 {
1501 aImportTab[i].ulAddress = auFuncs[i];
1502 Int3();
1503 break;
1504 }
1505 case EPT_PROCIMPORT16:
1506 {
1507 cb = interpretFunctionProlog16((char*)aImportTab[i].ulAddress, FALSE);
1508 aImportTab[i].cbProlog = (char)cb;
1509 if (cb > 0) /* Since no prolog part is copied to the function table, it's ok as long as the prolog has been recognzied. */
1510 {
1511 /*
1512 * Create far jump from calltab to original function.
1513 * 0xEA <four byte target address> <two byte target selector>
1514 */
1515 pchCTEntry[0] = 0xEA; /* jmp far ptr */
1516 *(unsigned long*)(void*)&pchCTEntry[1] = aImportTab[i].offObject;
1517 *(unsigned short*)(void*)&pchCTEntry[5] = aImportTab[i].usSel;
1518 }
1519 else
1520 { /* !fatal! - this should never really happen... */
1521 kprintf(("ImportTabInit: FATAL verify failed for procedure no.%d when importing it!\n", i));
1522 Int3(); /* ipe - later! */
1523 x86RestoreWriteProtect(flWP);
1524 return ERROR_D32_IPE | (i << ERROR_D32_PROC_SHIFT) | ERROR_D32_PROC_FLAG;
1525 }
1526 pchCTEntry += IMPORT16_ENTRY;
1527 break;
1528 }
1529
1530
1531 /*
1532 * 16/32-bit importe variable.
1533 * This is used by accessing the 32-bit flat address in the callTab.
1534 * callTab-entry + 4 holds the offset of the variable into the object.
1535 * callTab-entry + 8 holds the selector for the object. (These two fields is the 16:32-bit pointer to the variable.)
1536 * callTab-entry + a holds the 16-bit offset for the variable.
1537 * callTab-entry + c holds the selector for the object. (These two fields is the 16:16-bit pointer to the variable.)
1538 */
1539 case EPT_VARIMPORTNR32:
1540 case EPT_VARIMPORTNR16:
1541 if (!aImportTab[i].fFound)
1542 {
1543 memset(pchCTEntry, 0, VARIMPORT_ENTRY);
1544 pchCTEntry += VARIMPORT_ENTRY;
1545 break;
1546 }
1547 case EPT_VARIMPORT32:
1548 case EPT_VARIMPORT16:
1549 aImportTab[i].cbProlog = (char)0;
1550 *(unsigned long*)(void*)&pchCTEntry[0] = aImportTab[i].ulAddress;
1551 *(unsigned long*)(void*)&pchCTEntry[4] = aImportTab[i].offObject;
1552 *(unsigned short*)(void*)&pchCTEntry[8] = aImportTab[i].usSel;
1553 *(unsigned short*)(void*)&pchCTEntry[0xa] = (unsigned short)aImportTab[i].offObject;
1554 *(unsigned short*)(void*)&pchCTEntry[0xc] = aImportTab[i].usSel;
1555 pchCTEntry += VARIMPORT_ENTRY;
1556 break;
1557
1558 default:
1559 kprintf(("ImportTabInit: unsupported type. (procedure no.%d, cb=%d)\n", i, cb));
1560 Int3(); /* ipe - later! */
1561 x86RestoreWriteProtect(flWP);
1562 return ERROR_D32_IPE | (i << ERROR_D32_PROC_SHIFT) | ERROR_D32_PROC_FLAG;
1563 } /* switch - type */
1564 } /* for */
1565
1566 x86RestoreWriteProtect(flWP);
1567
1568 return NO_ERROR;
1569}
1570
1571
1572#ifdef R3TST
1573/**
1574 * Creates a fake kernel MTE, SMTE and OTE for use while testing in Ring3.
1575 * @returns Pointer to the fake kernel MTE.
1576 * @status completely implemented.
1577 * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
1578 */
1579PMTE GetOS2KrnlMTETst(void)
1580{
1581 static MTE KrnlMTE;
1582 static SMTE KrnlSMTE;
1583
1584 KrnlMTE.mte_swapmte = &KrnlSMTE;
1585 KrnlSMTE.smte_objtab = &aKrnlOTE[0];
1586 KrnlSMTE.smte_objcnt = cObjectsFake;
1587
1588 return &KrnlMTE;
1589}
1590
1591/**
1592 * -Ring-3 testing-
1593 * Changes the entries in aImportTab to point to their fake equivalents.
1594 * @returns void
1595 * @param void
1596 * @status completely implemented.
1597 * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
1598 * @remark Called before the aImportTab array is used/verified.
1599 */
1600VOID R3TstFixImportTab(VOID)
1601{
1602 int i;
1603
1604 for (i = 0; i < NBR_OF_KRNLIMPORTS; i++)
1605 {
1606 switch (aImportTab[i].fType & ~EPT_NOT_REQ)
1607 {
1608 case EPT_PROC32:
1609 if (aTstFakers[i].fObj != 1)
1610 kprintf(("R3TstFixImportTab: invalid segment config for entry %i. (PROC32)\n", i));
1611 break;
1612 case EPT_PROCIMPORT32:
1613 if (aTstFakers[i].fObj != 1)
1614 kprintf(("R3TstFixImportTab: invalid segment config for entry %i. (PROCIMPORT32)\n", i));
1615 break;
1616 case EPT_PROCIMPORT16:
1617 if (aTstFakers[i].fObj != 2)
1618 kprintf(("R3TstFixImportTab: invalid segment config for entry %i. (PROCIMPORT16)\n", i));
1619 break;
1620 case EPT_VARIMPORT32:
1621 case EPT_VARIMPORT16:
1622 if (aTstFakers[i].fObj != 3 && aTstFakers[i].fObj != 4)
1623 kprintf(("R3TstFixImportTab: invalid segment config for entry %i. (VARIMPORT32/16)\n", i));
1624 break;
1625 } /* switch - type */
1626
1627 aImportTab[i].ulAddress = aTstFakers[i].uAddress;
1628 switch (aTstFakers[i].fObj)
1629 {
1630 case 1:
1631 aImportTab[i].usSel = GetSelectorCODE32();
1632 aImportTab[i].offObject = aTstFakers[i].uAddress - (unsigned)&CODE32START;
1633 break;
1634 case 2:
1635 aImportTab[i].usSel = GetSelectorCODE16();
1636 aImportTab[i].offObject = aTstFakers[i].uAddress - (unsigned)&CODE16START;
1637 break;
1638 case 3:
1639 aImportTab[i].usSel = GetSelectorDATA32();
1640 aImportTab[i].offObject = aTstFakers[i].uAddress - (unsigned)&DATA32START;
1641 break;
1642 case 4:
1643 aImportTab[i].usSel = GetSelectorDATA16();
1644 aImportTab[i].offObject = aTstFakers[i].uAddress - (unsigned)&DATA16START;
1645 break;
1646 default:
1647 kprintf(("R3TstFixImportTab: invalid segment config for entry %i.\n", i));
1648 }
1649 } /* for */
1650}
1651#endif
1652
1653/**
1654 * Dummy nop function if SecPathFromSFN isn't found.
1655 */
1656PSZ SECCALL nopSecPathFromSFN(SFN hFile)
1657{
1658 NOREF(hFile);
1659 return NULL;
1660}
Note: See TracBrowser for help on using the repository browser.