FS: Kernel stuff.

This commit is contained in:
Aaron Robinson 2003-01-28 07:28:18 +00:00
parent 972c28a02c
commit 95e11ebc33
7 changed files with 310 additions and 7 deletions

View File

@ -53,7 +53,7 @@ BSC32=bscmake.exe
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386 /out:"Bin/Cxbx.dll"
# ADD LINK32 ntdll.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386 /out:"Bin/Cxbx.dll" /libpath:"Lib"
!ELSEIF "$(CFG)" == "CxbxKrnl - Win32 Debug"
@ -79,7 +79,7 @@ BSC32=bscmake.exe
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /out:"Bin\Debug/Cxbx.dll" /pdbtype:sept
# ADD LINK32 ntdll.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /out:"Bin\Debug/Cxbx.dll" /pdbtype:sept /libpath:"Lib"
!ENDIF
@ -108,6 +108,10 @@ SOURCE=.\Include\Win32\CxbxKrnl\CxbxKrnl.h
# End Source File
# Begin Source File
SOURCE=.\Include\Win32\CxbxKrnl\LDT.h
# End Source File
# Begin Source File
SOURCE=.\Include\Win32\CxbxKrnl\xntdll.h
# End Source File
# End Group
@ -126,6 +130,10 @@ SOURCE=.\Source\Win32\CxbxKrnl\CxbxKrnl.cpp
SOURCE=.\Source\Win32\CxbxKrnl\KernelThunk.cpp
# End Source File
# Begin Source File
SOURCE=.\Source\Win32\CxbxKrnl\LDT.cpp
# End Source File
# End Group
# End Target
# End Project

View File

@ -39,10 +39,11 @@
<Tool
Name="VCLinkerTool"
AdditionalOptions="/MACHINE:I386"
AdditionalDependencies="odbc32.lib odbccp32.lib"
AdditionalDependencies="odbc32.lib odbccp32.lib ntdll.lib"
OutputFile="Bin\Debug/Cxbx.dll"
LinkIncremental="2"
SuppressStartupBanner="TRUE"
AdditionalLibraryDirectories="Lib"
GenerateDebugInformation="TRUE"
ProgramDatabaseFile=".\Bin\Debug/Cxbx.pdb"
ImportLibrary=".\Bin\Debug/Cxbx.lib"/>
@ -97,10 +98,11 @@
<Tool
Name="VCLinkerTool"
AdditionalOptions="/MACHINE:I386"
AdditionalDependencies="odbc32.lib odbccp32.lib"
AdditionalDependencies="odbc32.lib odbccp32.lib ntdll.lib"
OutputFile="Bin/Cxbx.dll"
LinkIncremental="1"
SuppressStartupBanner="TRUE"
AdditionalLibraryDirectories="Lib"
ProgramDatabaseFile=".\Bin/Cxbx.pdb"
ImportLibrary=".\Bin/Cxbx.lib"/>
<Tool
@ -133,6 +135,9 @@
<File
RelativePath=".\Include\Win32\CxbxKrnl\CxbxKrnl.h">
</File>
<File
RelativePath="Include\Win32\CxbxKrnl\LDT.h">
</File>
<File
RelativePath=".\Include\Win32\CxbxKrnl\xntdll.h">
</File>
@ -146,6 +151,9 @@
<File
RelativePath=".\Source\Win32\CxbxKrnl\KernelThunk.cpp">
</File>
<File
RelativePath="Source\Win32\CxbxKrnl\LDT.cpp">
</File>
</Filter>
<Filter
Name="Bin"

View File

@ -0,0 +1,43 @@
// ******************************************************************
// *
// * .,-::::: .,:: .::::::::. .,:: .:
// * ,;;;'````' `;;;, .,;; ;;;'';;' `;;;, .,;;
// * [[[ '[[,,[[' [[[__[[\. '[[,,[['
// * $$$ Y$$$P $$""""Y$$ Y$$$P
// * `88bo,__,o, oP"``"Yo, _88o,,od8P oP"``"Yo,
// * "YUMMMMMP",m" "Mm,""YUMMMP" ,m" "Mm,
// *
// * Cxbx->Win32->CxbxKrnl->LDT.h
// *
// * This file is part of the Cxbx project.
// *
// * Cxbx and Cxbe are free software; you can redistribute them
// * and/or modify them under the terms of the GNU General Public
// * License as published by the Free Software Foundation; either
// * version 2 of the license, or (at your option) any later version.
// *
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU General Public License for more details.
// *
// * You should have recieved a copy of the GNU General Public License
// * along with this program; see the file COPYING.
// * If not, write to the Free Software Foundation, Inc.,
// * 59 Temple Place - Suite 330, Bostom, MA 02111-1307, USA.
// *
// * (c) 2002-2003 Aaron Robinson <caustik@caustik.com>
// *
// * All rights reserved
// *
// ******************************************************************
#ifndef LDT_H
#define LDT_H
void LDTSystemInit();
uint16 LDTAllocate(uint32 dwBaseAddr, uint32 dwLimit);
void LDTDeallocate(uint16 wSelector);
#endif

View File

@ -92,6 +92,25 @@ typedef HANDLE *PHANDLE;
typedef long NTSTATUS;
typedef unsigned __int64 ULONGLONG;
#define NT_SUCCESS(Status) ((NTSTATUS) (Status) >= 0)
// ******************************************************************
// * calling conventions
// ******************************************************************
#define NTAPI __stdcall
#define CDECL __cdecl
#define INLINE __inline
#define DECLSPEC_NORETURN __declspec(noreturn)
// ******************************************************************
// * documentation purposes only
// ******************************************************************
#define EXPORTNUM(a)
#define UNALIGNED
#define OPTIONAL
#define IN
#define OUT
// ******************************************************************
// * LDT_ENTRY
// ******************************************************************

BIN
Lib/ntdll.lib Normal file

Binary file not shown.

View File

@ -37,6 +37,8 @@
#define _XBOXKRNL_LOCAL_
#include "CxbxKrnl.h"
#include "LDT.h"
#include <stdio.h>
// ******************************************************************
@ -58,6 +60,44 @@ namespace win32
using namespace win32;
// ******************************************************************
// * func: EmuXGenerateFS
// ******************************************************************
void EmuXGenerateFS()
{
NT_TIB *OrgFS = 0;
uint32 dwFSSize = sizeof(NT_TIB);
// ******************************************************************
// * Retrieve the "old" FS
// ******************************************************************
__asm
{
mov esi, fs:[18h]
mov OrgFS, esi
}
// ******************************************************************
// * Allocate and update the new FS
// ******************************************************************
{
uint32 AllocFS = (uint32)new char[dwFSSize];
memcpy((void*)AllocFS, OrgFS, dwFSSize);
uint16 SelectorFS = LDTAllocate(AllocFS, AllocFS+dwFSSize);
__asm
{
mov ax, SelectorFS
push ax
pop fs
}
}
}
// ******************************************************************
// * func: EmuXInit
// ******************************************************************
@ -106,20 +146,35 @@ CXBXKRNL_API void NTAPI EmuXInit(uint32 DebugConsole, uint08 *XBEHeader, uint32
memcpy((void*)dwCertificateAddr, &XBEHeader[dwCertificateAddr - 0x00010000], sizeof(Xbe::Certificate));
}
// ******************************************************************
// * Initialize LDT system
// ******************************************************************
{
LDTSystemInit();
}
// ******************************************************************
// * Initialize FS:* structure
// ******************************************************************
{
NT_TIB *OriginalTIB = 0;
// Calling this function will overwrite the Win2k/XP FS: structure,
// which will cause an immediate or eventual crash. In order to avoid
// this, it is going to be necessary to store the Win2k/XP FS: in a
// special un-used slot in the XBox FS:* structure, and bring it back
// in whenever we need to use Win2k/XP functions
// EmuXGenerateFS();
NT_TIB *dbgTIB = 0;
__asm
{
mov esi, fs:[18h]
mov OriginalTIB, esi
mov dbgTIB, esi
}
// TODO: Allocate new FS: within LDT, copy OriginalTIB to KPCR.NtTib, load new FS
printf("CxbxKrnl [0x%.08X]: NT_TIB.Self=0x%.08X\n", GetCurrentThreadId(), OriginalTIB->Self);
printf("CxbxKrnl [0x%.08X]: NT_TIB.Self=0x%.08X\n", GetCurrentThreadId(), dbgTIB->Self);
}
printf("CxbxKrnl [0x%.08X]: Initial thread starting.\n", GetCurrentThreadId());

View File

@ -0,0 +1,170 @@
// ******************************************************************
// *
// * .,-::::: .,:: .::::::::. .,:: .:
// * ,;;;'````' `;;;, .,;; ;;;'';;' `;;;, .,;;
// * [[[ '[[,,[[' [[[__[[\. '[[,,[['
// * $$$ Y$$$P $$""""Y$$ Y$$$P
// * `88bo,__,o, oP"``"Yo, _88o,,od8P oP"``"Yo,
// * "YUMMMMMP",m" "Mm,""YUMMMP" ,m" "Mm,
// *
// * Cxbx->Win32->CxbxKrnl->CxbxKrnl.cpp
// *
// * This file is part of the Cxbx project.
// *
// * Cxbx and Cxbe are free software; you can redistribute them
// * and/or modify them under the terms of the GNU General Public
// * License as published by the Free Software Foundation; either
// * version 2 of the license, or (at your option) any later version.
// *
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU General Public License for more details.
// *
// * You should have recieved a copy of the GNU General Public License
// * along with this program; see the file COPYING.
// * If not, write to the Free Software Foundation, Inc.,
// * 59 Temple Place - Suite 330, Bostom, MA 02111-1307, USA.
// *
// * (c) 2002-2003 Aaron Robinson <caustik@caustik.com>
// *
// * All rights reserved
// *
// ******************************************************************
#include "Cxbx.h"
#include "LDT.h"
// ******************************************************************
// * ntdll wrapped in namespace to avoid collisions
// ******************************************************************
namespace xntdll
{
#include "xntdll.h"
};
#include <windows.h>
// ******************************************************************
// * Maximum number of threads this LDT system will handle
// ******************************************************************
#define MAXIMUM_XBOX_THREADS 32
// ******************************************************************
// * Table of free LDT entries
// ******************************************************************
static uint16 FreeLDTEntries[MAXIMUM_XBOX_THREADS];
// ******************************************************************
// * Critical section lock
// ******************************************************************
static CRITICAL_SECTION LDTLock;
// ******************************************************************
// * func: LDTSystemInit
// ******************************************************************
void LDTSystemInit()
{
InitializeCriticalSection(&LDTLock);
for(uint32 v=0;v<MAXIMUM_XBOX_THREADS;v++)
FreeLDTEntries[v] = (uint16)((v*8) + 7 + 8);
}
// ******************************************************************
// * func: LDTAllocate
// ******************************************************************
uint16 LDTAllocate(uint32 dwBaseAddr, uint32 dwLimit)
{
ULONG_PTR uBase = (ULONG_PTR)dwBaseAddr;
xntdll::LDT_ENTRY LDTEntry;
int x=0;
EnterCriticalSection(&LDTLock);
// ******************************************************************
// * Locate a free LDT entry
// ******************************************************************
{
for(x=0;x<MAXIMUM_XBOX_THREADS;x++)
if(FreeLDTEntries[x])
break;
if(x == MAXIMUM_XBOX_THREADS)
{
LeaveCriticalSection(&LDTLock);
// TODO: cleaner error handling
MessageBox(NULL, "Very strange error: Could not locate free LDT entry!", "CxbxKrnl", MB_OK | MB_ICONEXCLAMATION);
return 0;
}
}
// ******************************************************************
// * Set up selector information
// ******************************************************************
{
LDTEntry.BaseLow = (WORD)(uBase & 0xFFFF);
LDTEntry.HighWord.Bits.BaseMid = (uBase >> 16) & 0xFF;
LDTEntry.HighWord.Bits.BaseHi = (uBase >> 24) & 0xFF;
LDTEntry.HighWord.Bits.Type = 0x13; // RW data segment
LDTEntry.HighWord.Bits.Dpl = 3; // user segment
LDTEntry.HighWord.Bits.Pres = 1; // present
LDTEntry.HighWord.Bits.Sys = 0;
LDTEntry.HighWord.Bits.Reserved_0 = 0;
LDTEntry.HighWord.Bits.Default_Big = 1; // 386 segment
LDTEntry.HighWord.Bits.Granularity = (dwLimit >= 0x00100000) ? 1 : 0;
if(LDTEntry.HighWord.Bits.Granularity)
dwLimit >>= 12;
LDTEntry.LimitLow = (WORD)(dwLimit & 0xFFFF);
LDTEntry.HighWord.Bits.LimitHi = (dwLimit >> 16) & 0xF;
}
// ******************************************************************
// * Allocate selector
// ******************************************************************
{
using namespace xntdll;
if(!NT_SUCCESS(NtSetLdtEntries((x*8)+7+8, LDTEntry, 0, LDTEntry)))
{
LeaveCriticalSection(&LDTLock);
// TODO: cleaner error handling
MessageBox(NULL, "Very strange error: Could not set LDT entry!", "CxbxKrnl", MB_OK | MB_ICONEXCLAMATION);
return 0;
}
}
LeaveCriticalSection(&LDTLock);
FreeLDTEntries[x] = 0;
return (x*8)+7+8;
}
// ******************************************************************
// * func: LDTDeallocate
// ******************************************************************
void LDTDeallocate(uint16 wSelector)
{
xntdll::LDT_ENTRY LDTEntry;
EnterCriticalSection(&LDTLock);
ZeroMemory(&LDTEntry, sizeof(LDTEntry));
xntdll::NtSetLdtEntries(wSelector, LDTEntry, 0, LDTEntry);
FreeLDTEntries[(wSelector >> 3)-1] = wSelector;
LeaveCriticalSection(&LDTLock);
return;
}