Integrate xtklib with the kernel

This commit is contained in:
2022-08-30 23:22:38 +02:00
parent 6ef36cbd26
commit a7d07e7ecb
18 changed files with 88 additions and 51 deletions

38
xtoskrnl/CMakeLists.txt Normal file
View File

@@ -0,0 +1,38 @@
# XT Kernel
PROJECT(XTOSKRNL)
# Specify include directories
include_directories(
${EXECTOS_SOURCE_DIR}/sdk/xtdk)
# Specify list of source code files
list(APPEND XTOSKRNL_SOURCE
${XTOSKRNL_SOURCE_DIR}/hl/cport.c
${XTOSKRNL_SOURCE_DIR}/hl/${ARCH}/cpufunc.c
${XTOSKRNL_SOURCE_DIR}/ke/krnlinit.c
${XTOSKRNL_SOURCE_DIR}/rtl/memory.c
${XTOSKRNL_SOURCE_DIR}/rtl/plist.c
${XTOSKRNL_SOURCE_DIR}/rtl/widestr.c)
# Set module definition SPEC file
set_specfile(xtoskrnl.spec)
# Link static XTOS library
add_library(xtoslib ${XTOSKRNL_SOURCE})
# Link kernel executable
add_executable(xtoskrnl
${XTOSKRNL_SOURCE}
${CMAKE_CURRENT_BINARY_DIR}/xtoskrnl.def)
# Add linker libraries
target_link_libraries(xtoskrnl)
# Set instal target
set_install_target(xtoskrnl "exectos/system")
# Set kernel entrypoint, imagebase address, ordinals and subsystem
set_entrypoint(xtoskrnl "KeStartXtSystem")
set_imagebase(xtoskrnl ${BASEADDRESS_XTOSKRNL})
set_ordinals(xtoskrnl TRUE)
set_subsystem(xtoskrnl native)

View File

@@ -0,0 +1,56 @@
/**
* PROJECT: ExectOS
* COPYRIGHT: See COPYING.md in the top level directory
* FILE: xtoskrnl/hl/amd64/cpufunc.c
* DESCRIPTION: Routines to provide access to special AMD64 CPU instructions
* DEVELOPERS: Rafal Kupiec <belliash@codingworkshop.eu.org>
*/
#include "xtkmapi.h"
/**
* Reads the data from the specified I/O port.
*
* @param Port
* Specifies the port number in the range of 0-0xFFFF.
*
* @return The value read from the port.
*
* @since XT 1.0
*/
UCHAR
XTAPI
HlIoPortInByte(IN USHORT Port)
{
UCHAR Value;
asm volatile("inb %1, %0"
: "=a"(Value)
: "Nd"(Port));
return Value;
}
/**
* Writes the data to the specified I/O port.
*
* @param Port
* Specifies the port number in the range of 0-0xFFFF.
*
* @param Value
* Supplies the value to write.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
VOID
XTAPI
HlIoPortOutByte(IN USHORT Port,
IN UCHAR Value)
{
asm volatile("outb %0, %1"
:
:
"a"(Value),
"Nd"(Port));
}

302
xtoskrnl/hl/cport.c Normal file
View File

@@ -0,0 +1,302 @@
/**
* PROJECT: ExectOS
* COPYRIGHT: See COPYING.md in the top level directory
* FILE: xtoskrnl/hl/cport.c
* DESCRIPTION: Serial (COM) port support
* DEVELOPERS: Rafal Kupiec <belliash@codingworkshop.eu.org>
*/
#include "xtkmapi.h"
/* I/O port addresses for COM1 - COM8 (valid only for ia32 and amd64) */
ULONG ComPortAddress[] = {0x000, 0x3F8, 0x2F8, 0x3E8, 0x2E8, 0x5F8, 0x4F8, 0x5E8, 0x4E8};
/**
* This routine gets a byte from serial port.
*
* @param Port
* Address of port object describing a port settings.
*
* @param Byte
* Address of variable where to store the result.
*
* @param Wait
* Specifies whether to wait for a data or not.
*
* @param Poll
* Indicates whether to only poll, not reading the data.
*
* @return This routine returns a status code.
*
* @since XT 1.0
*/
XTSTATUS
HlComPortGetByte(IN PCPPORT Port,
OUT PUCHAR Byte,
IN BOOLEAN Wait,
IN BOOLEAN Poll)
{
UCHAR Lsr;
ULONG Retry;
/* Make sure the port has been initialized */
if(Port->Address == 0)
{
return STATUS_DEVICE_NOT_READY;
}
/* Retry getting data if allowed to wait */
Retry = Wait ? COMPORT_WAIT_TIMEOUT : 1;
while(Retry--)
{
/* Get LSR for data ready */
Lsr = HlComPortReadLsr(Port, COMPORT_LSR_DR);
if((Lsr & COMPORT_LSR_DR) == COMPORT_LSR_DR)
{
/* Check for errors */
if(Lsr & (COMPORT_LSR_FE | COMPORT_LSR_OE | COMPORT_LSR_PE))
{
/* Framing, parity or overrun error occurred */
*Byte = 0;
return STATUS_IO_DEVICE_ERROR;
}
/* Check if only polling */
if(Poll)
{
/* Only polling, return success */
return STATUS_SUCCESS;
}
/* Read the byte from serial port */
*Byte = HlIoPortInByte(PtrToUshort(Port->Address + (ULONG)COMPORT_REG_RBR));
/* Check if in modem control mode */
if(Port->Flags & COMPORT_FLAG_MC)
{
/* Handle Carrier Detected (CD) */
if((HlIoPortInByte(PtrToShort(Port->Address + (ULONG)COMPORT_REG_MSR)) & COMPORT_MSR_DCD) == 0)
{
/* Skip byte if no CD present */
continue;
}
}
return STATUS_SUCCESS;
}
}
/* Reset LSR and return that no data found */
HlComPortReadLsr(Port, 0);
return STATUS_NOT_FOUND;
}
/**
* Reads LSR from specified serial port.
*
* @param Port
* Address of COM port.
*
* @param Byte
* Value expected from the port.
*
* @return Byte read from COM port.
*
* @since XT 1.0
*/
UCHAR
HlComPortReadLsr(IN PCPPORT Port,
IN UCHAR Byte)
{
UCHAR Lsr, Msr;
STATIC UCHAR RingFlag;
/* Read the Line Status Register (LSR) */
Lsr = HlIoPortInByte(PtrToUshort(Port->Address + (ULONG)COMPORT_REG_LSR));
/* Check if expected byte is present */
if((Lsr & Byte) == 0)
{
/* Check Modem Status Register (MSR) for ring indicator */
Msr = HlIoPortInByte(PtrToUshort(Port->Address + (ULONG)COMPORT_REG_MSR));
RingFlag |= (Msr & COMPORT_MSR_RI) ? 1 : 2;
if(RingFlag == 3)
{
/* Ring indicator toggled, use modem control */
Port->Flags |= COMPORT_FLAG_MC;
}
}
/* Return byte read */
return Lsr;
}
/**
* This routine writes a byte to the serial port.
*
* @param Port
* Address of port object describing a port settings.
*
* @param Byte
* Data to be written.
*
* @return This routine returns a status code.
*
* @since XT 1.0
*/
XTSTATUS
HlComPortPutByte(IN PCPPORT Port,
IN UCHAR Byte)
{
UCHAR Lsr, Msr;
/* Make sure the port has been initialized */
if(Port->Address == 0)
{
return STATUS_DEVICE_NOT_READY;
}
/* Check if port is in modem control */
while(Port->Flags & COMPORT_FLAG_MC)
{
/* Get the Modem Status Register (MSR) */
Msr = HlIoPortInByte(PtrToUshort(Port->Address + (ULONG)COMPORT_REG_MSR)) & COMPORT_MSR_DSRCTSCD;
if(Msr != COMPORT_MSR_DSRCTSCD)
{
/* Take character, if CD is not set */
Lsr = HlComPortReadLsr(Port, 0);
if((Msr & COMPORT_MSR_DCD) == 0 && (Lsr & COMPORT_LSR_DR) == COMPORT_LSR_DR)
{
/* Eat the character */
HlIoPortInByte(PtrToUshort(Port->Address + (ULONG)COMPORT_REG_RBR));
}
}
else
{
/* CD, CTS and DSR are set, we can continue */
break;
}
}
/* Wait for busy port */
while((HlComPortReadLsr(Port, COMPORT_LSR_THRE) & COMPORT_LSR_THRE) == 0);
/* Send byte to the port */
HlIoPortOutByte(PtrToUshort(Port->Address + (ULONG)COMPORT_REG_THR), Byte);
return STATUS_SUCCESS;
}
/**
* This routine initializes the COM port.
*
* @param Port
* Address of port object describing a port settings.
*
* @param PortNumber
* Supplies a port number.
*
* @param BaudRate
* Supplies an optional port baud rate.
*
* @return This routine returns a status code.
*
* @since XT 1.0
*/
XTSTATUS
HlInitializeComPort(IN OUT PCPPORT Port,
IN ULONG PortNumber,
IN ULONG BaudRate)
{
PUCHAR Address;
UCHAR Byte = 0;
USHORT Flags = 0;
ULONG Mode;
/* Check if serial port is set */
if(PortNumber == 0)
{
/* Use COM1 by default */
PortNumber = 1;
}
/* We support only a pre-defined number of ports */
if(PortNumber > ARRAY_SIZE(ComPortAddress))
{
/* Fail if wrong/unsupported port used */
return STATUS_INVALID_PARAMETER;
}
/* Check if baud rate is set */
if(BaudRate == 0)
{
/* Use default baud (clock) rate if not set */
BaudRate = COMPORT_CLOCK_RATE;
Flags |= COMPORT_FLAG_DBR;
}
/* Store COM pointer */
Address = UlongToPtr(ComPortAddress[PortNumber]);
/* Check whether this port is not already initialized */
if((Port->Address == Address) && (Port->Baud == BaudRate))
{
return STATUS_SUCCESS;
}
/* Test if chosen COM port exists */
do
{
/* Check whether the 16450/16550 scratch register exists */
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPROT_REG_SR), Byte);
if(HlIoPortInByte(PtrToUshort(Address + (ULONG)COMPROT_REG_SR)) != Byte)
{
return STATUS_NOT_FOUND;
}
} while(++Byte != 0);
/* Disable interrupts */
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_LCR), COMPORT_LSR_DIS);
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_IER), COMPORT_LSR_DIS);
/* Enable Divisor Latch Access Bit (DLAB) */
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_LCR), COMPORT_LCR_DLAB);
/* Set baud rate */
Mode = COMPORT_CLOCK_RATE / BaudRate;
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_DIV_DLL), (UCHAR)(Mode & 0xFF));
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_DIV_DLM), (UCHAR)((Mode >> 8) & 0xFF));
/* Set 8 data bits, 1 stop bits, no parity (8n1) */
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_LCR),
COMPORT_LCR_8DATA | COMPORT_LCR_1STOP | COMPORT_LCR_PARN);
/* Enable DTR, RTS and OUT2 */
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_MCR),
COMPORT_MCR_DTR | COMPORT_MCR_RTS | COMPORT_MCR_OUT2);
/* Enable FIFO */
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_FCR),
COMPORT_FCR_ENABLE | COMPORT_FCR_RCVR_RESET | COMPORT_FCR_TXMT_RESET);
/* Enable loopback mode and test serial port */
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_MCR), COMPORT_MCR_LOOP);
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_RBR), COMPORT_MSR_TST);
if(HlIoPortInByte(PtrToUshort(Address + (ULONG)COMPORT_REG_RBR)) != COMPORT_MSR_TST)
{
return STATUS_IO_DEVICE_ERROR;
}
/* Mark port as fully initialized */
Flags |= COMPORT_FLAG_INIT;
/* Disable loopback mode and use port normally */
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_MCR), COMPORT_MCR_NOM);
Port->Address = Address;
Port->Baud = BaudRate;
Port->Flags = Flags;
/* Return success */
return STATUS_SUCCESS;
}

View File

@@ -0,0 +1,56 @@
/**
* PROJECT: ExectOS
* COPYRIGHT: See COPYING.md in the top level directory
* FILE: xtoskrnl/hl/i686/cpufunc.c
* DESCRIPTION: Routines to provide access to special i686 CPU instructions
* DEVELOPERS: Rafal Kupiec <belliash@codingworkshop.eu.org>
*/
#include "xtkmapi.h"
/**
* Reads the data from the specified I/O port.
*
* @param Port
* Specifies the port number in the range of 0-0xFFFF.
*
* @return The value read from the port.
*
* @since XT 1.0
*/
UCHAR
XTAPI
HlIoPortInByte(IN USHORT Port)
{
UCHAR Value;
asm volatile("inb %1, %0"
: "=a"(Value)
: "Nd"(Port));
return Value;
}
/**
* Writes the data to the specified I/O port.
*
* @param Port
* Specifies the port number in the range of 0-0xFFFF.
*
* @param Value
* Supplies the value to write.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
VOID
XTAPI
HlIoPortOutByte(IN USHORT Port,
IN UCHAR Value)
{
asm volatile("outb %0, %1"
:
:
"a"(Value),
"Nd"(Port));
}

25
xtoskrnl/ke/krnlinit.c Normal file
View File

@@ -0,0 +1,25 @@
/**
* PROJECT: ExectOS
* COPYRIGHT: See COPYING.md in the top level directory
* FILE: xtoskrnl/ke/krnlinit.c
* DESCRIPTION: XT kernel initialization
* DEVELOPERS: Rafal Kupiec <belliash@codingworkshop.eu.org>
*/
#include <xtkmapi.h>
/**
* This routine starts up the XT kernel. It is called by boot loader.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTAPI
VOID
KeStartXtSystem()
{
/* Enter infinite kernel thread loop */
for(;;);
}

95
xtoskrnl/rtl/memory.c Normal file
View File

@@ -0,0 +1,95 @@
/**
* PROJECT: ExectOS
* COPYRIGHT: See COPYING.md in the top level directory
* FILE: xtoskrnl/rtl/memory.c
* DESCRIPTION: Memory related routines
* DEVELOPERS: Rafal Kupiec <belliash@codingworkshop.eu.org>
*/
#include <xtkmapi.h>
/**
* This routine copies a block of memory.
*
* @param Destination
* Supplies a pointer to the buffer where data will be copied to.
*
* @param Source
* Supplies a pointer to the source buffer that will be copied.
*
* @param Length
* Specifies the number of bytes to copy.
*
* @return Returns the destination pointer.
*
* @since NT 3.5
*/
XTAPI
PVOID
RtlCopyMemory(IN PVOID Destination,
IN PCVOID Source,
IN SIZE_T Length)
{
PCHAR DestinationBytes = (PCHAR)Destination;
PCHAR SourceBytes = (PCHAR)Source;
/* Forward buffer copy */
while(Length--)
{
*DestinationBytes++ = *SourceBytes++;
}
/* Return pointer to destination buffer */
return Destination;
}
/**
* This routine compares the first bytes of the specified memory buffers.
*
* @param LeftBuffer
* Supplies a pointer to the first block of memory to compare.
*
* @param RightBuffer
* Supplies a pointer to the second block of memory to compare.
*
* @param Length
* Specifies a number of bytes to compare.
*
* @return A value indicating the relationship between the content of the memory blocks.
* It returns zero (0) if both memory blocks are equal or a value different than
* zero representing which is greater if they do not match.
*
* @since XT 1.0
*/
XTAPI
SIZE_T
RtlSameMemory(IN PCVOID LeftBuffer,
IN PCVOID RightBuffer,
IN SIZE_T Length)
{
CONST UCHAR *Left = (PUCHAR)LeftBuffer;
CONST UCHAR *Right = (PUCHAR)RightBuffer;
/* Check if there is anything to compare */
if(Length)
{
/* Iterate through whole buffer length */
while(Length--)
{
/* Compare bytes from both bufers */
if(*Left != *Right)
{
/* Buffers differ */
return (*Left - *Right);
}
/* Advance to next byte */
Left++;
Right++;
}
}
/* Buffers equal */
return 0;
}

106
xtoskrnl/rtl/plist.c Normal file
View File

@@ -0,0 +1,106 @@
/**
* PROJECT: ExectOS
* COPYRIGHT: See COPYING.md in the top level directory
* FILE: xtoskrnl/rtl/plist.c
* DESCRIPTION: Linked list manipulation routines
* DEVELOPERS: Rafal Kupiec <belliash@codingworkshop.eu.org>
*/
#include <xtkmapi.h>
/**
* This routine initializes a structure representing the head of a double-linked list.
*
* @param ListHead
* Pointer to a structure that serves as the list header.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
VOID
RtlInitializeListHead(IN PLIST_ENTRY ListHead)
{
ListHead->Blink = ListHead;
ListHead->Flink = ListHead;
}
/**
* This routine initializes a structure representing the head of a 32bit double-linked list.
*
* @param ListHead
* Pointer to a structure that serves as the list header.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
VOID
RtlInitializeListHead32(IN PLIST_ENTRY32 ListHead)
{
ListHead->Blink = PtrToUlong(ListHead);
ListHead->Flink = PtrToUlong(ListHead);
}
/**
* This routine inserts an entry at the head of a double linked list.
*
* @param ListHead
* Pointer to the head of the list.
*
* @param Entry
* Pointer to the entry that will be inserted in the list.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
VOID
RtlInsertHeadList(IN OUT PLIST_ENTRY ListHead,
IN OUT PLIST_ENTRY Entry)
{
Entry->Flink = ListHead->Flink;
Entry->Blink = ListHead;
ListHead->Flink->Blink = Entry;
ListHead->Flink = Entry;
}
/**
* This routine inserts an entry at the tail of a double linked list.
*
* @param ListHead
* Pointer to the head of the list.
*
* @param Entry
* Pointer to the entry that will be inserted in the list.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
VOID
RtlInsertTailList(IN OUT PLIST_ENTRY ListHead,
IN OUT PLIST_ENTRY Entry)
{
Entry->Flink = ListHead;
Entry->Blink = ListHead->Blink;
ListHead->Blink->Flink = Entry;
ListHead->Blink = Entry;
}
/**
* Indicates whether a double linked list structure is empty.
*
* @param ListHead
* Pointer to a structure that represents the head of the list.
*
* @return TRUE if there are currently no entries in the list or FALSE otherwise.
*
* @since XT 1.0
*/
BOOLEAN
RtlListEmpty(PLIST_ENTRY ListHead)
{
return (ListHead->Flink == ListHead);
}

122
xtoskrnl/rtl/widestr.c Normal file
View File

@@ -0,0 +1,122 @@
/**
* PROJECT: ExectOS
* COPYRIGHT: See COPYING.md in the top level directory
* FILE: xtoskrnl/rtl/widestr.c
* DESCRIPTION: Wide string support
* DEVELOPERS: Rafal Kupiec <belliash@codingworkshop.eu.org>
*/
#include "xtkmapi.h"
/**
* Compares at most specified number of characters of two C wide strings.
*
* @param String1
* Wide string to be compared.
*
* @param String2
* Wide string to be compared.
*
* @param Length
* Maximum number of characters to compare.
*
* @return Integral value indicating the relationship between the wide strings.
*
* @since XT 1.0
*/
INT
RtlWideStringCompare(IN CONST PWCHAR String1,
IN CONST PWCHAR String2,
IN CONST ULONG Length)
{
ULONG Index;
/* Iterate through the strings */
for(Index = 0; Index < Length; Index++) {
/* Check if string characters are equal */
if(String1[Index] != String2[Index])
{
/* Different characters found */
return String1[Index] < String2[Index] ? -1 : 1;
}
/* Check if end of string reached */
if(!String1[Index])
{
/* Equal strings until the end of String1 */
return 0;
}
}
/* Strings are equal */
return 0;
}
/**
* Finds the next token in a null-terminated wide string.
*
* @param String
* Pointer to the null-terminated wide string to tokenize.
*
* @param Delimiter
* Pointer to the null-terminated wide string identifying delimiters.
*
* @param SavePtr
* Pointer to an object used to store routine internal state.
*
* @return Pointer to the beginning of the next token or NULL if there are no more tokens.
*
* @since: XT 1.0
*/
PWCHAR
RtlWideStringTokenize(IN PWCHAR String,
IN CONST PWCHAR Delimiter,
IN OUT PWCHAR *SavePtr)
{
PWCHAR Span, Token;
WCHAR Char, SpanChar;
/* Check if there is anything to tokenize */
if(String == NULL && (String = *SavePtr) == NULL)
{
/* Empty string given */
return (NULL);
}
/* Check non-delimiter characters */
Char = *String++;
if(Char == L'\0')
{
*SavePtr = NULL;
return (NULL);
}
Token = String - 1;
/* Scan token for delimiters */
for(;;)
{
Char = *String++;
Span = (WCHAR *)Delimiter;
do
{
if((SpanChar = *Span++) == Char)
{
if(Char == L'\0')
{
String = NULL;
}
else
{
String[-1] = L'\0';
}
/* Store pointer to the next token */
*SavePtr = String;
/* Return token */
return Token;
}
}
while(SpanChar != L'\0');
}
}

2
xtoskrnl/xtoskrnl.spec Normal file
View File

@@ -0,0 +1,2 @@
@ stdcall HlIoPortInByte(ptr)
@ stdcall HlIoPortOutByte(ptr long)