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

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

Added force preload.

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