Compare commits
No commits in common. "f79f04361afeb9aa089f734552fb8981bb6e4ec3" and "927aeb0872d723a92362c607df3d0d898873044b" have entirely different histories.
f79f04361a
...
927aeb0872
|
@ -576,7 +576,11 @@ typedef struct __attribute__((aligned(64), packed)) {
|
|||
} XSAVE_AREA;
|
||||
|
||||
|
||||
static void* IRQ_Handlers[16];
|
||||
static void* IRQ_Handlers[16] = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
|
||||
static const char* ExceptionStrings[] = {
|
||||
"Division by Zero",
|
||||
|
@ -638,12 +642,6 @@ size_t ReadCPUFrequency(size_t* PerformanceDest, uint8_t AvgOrDirect);
|
|||
uint32_t ReadPort(uint16_t Port, int Length);
|
||||
uint32_t WritePort(uint16_t Port, uint32_t Data, int Length);
|
||||
|
||||
/* Serial functions */
|
||||
void serial_write(const char chr);
|
||||
void serial_print(const char* data);
|
||||
void serial_printf(const char* format, ...);
|
||||
void init_serial();
|
||||
|
||||
/* ==================== Registers ==================== */
|
||||
size_t ReadModelSpecificRegister(size_t MSR);
|
||||
size_t WriteModelSpecificRegister(size_t MSR, size_t Data);
|
||||
|
@ -688,7 +686,7 @@ void ScanCPUFeatures(size_t RAX, size_t RCX);
|
|||
|
||||
/* ==================== Interrupts ==================== */
|
||||
|
||||
static uint64_t time;
|
||||
uint64_t time;
|
||||
|
||||
void IRQ_Common(INTERRUPT_FRAME* Frame, size_t Interupt);
|
||||
void ISR_Common(INTERRUPT_FRAME* Frame, size_t Interrupt);
|
||||
|
|
|
@ -79,8 +79,7 @@ __attribute__((aligned(4096))) static size_t FirstPageTable[512] = {0};
|
|||
void timer_wait(int ticks){
|
||||
uint64_t FinalTick = time + ticks;
|
||||
|
||||
int i = 0;
|
||||
while(i < ticks * 1000) { i++; };
|
||||
while(time < FinalTick);
|
||||
}
|
||||
|
||||
|
||||
|
@ -100,15 +99,7 @@ void PrepareSystem(FILELOADER_PARAMS* FLOP) {
|
|||
InstallGDT();
|
||||
InstallIDT();
|
||||
|
||||
// WARNING!
|
||||
// W A R N I N G !
|
||||
// This is L O U D!
|
||||
// Turn your volume down!
|
||||
// Seriously!
|
||||
// LOWER!
|
||||
// IT'S LOUD!
|
||||
|
||||
//beep();
|
||||
beep();
|
||||
|
||||
if(SetIdentityMap(FLOP->RTServices) == NULL) {
|
||||
Memory_Info.MemoryMap = FLOP->MemoryMap;
|
||||
|
@ -579,6 +570,209 @@ size_t ReadCPUFrequency(size_t* Perfs, uint8_t AverageOrDirect) {
|
|||
return Frequency;
|
||||
}
|
||||
|
||||
uint32_t ReadPort(uint16_t Port, int Length) {
|
||||
uint32_t Data;
|
||||
if(Length == 1) { // Read a byte
|
||||
__asm__ __volatile__("inb %[address], %[value]" : : [value] "a" ((uint8_t) Data), [address] "d" (Port) :);
|
||||
} else if (Length == 2) { // Read a word
|
||||
__asm__ __volatile__("inw %[address], %[value]" : : [value] "a" ((uint16_t) Data), [address] "d" (Port) :);
|
||||
} else if (Length == 4) { // Read a long (dword)
|
||||
__asm__ __volatile__("inl %[address], %[value]" : : [value] "a" (Data), [address] "d" (Port) :);
|
||||
} else {
|
||||
printf("ReadPort: Invalid Read Length.\r\n");
|
||||
}
|
||||
|
||||
return Data;
|
||||
}
|
||||
|
||||
uint32_t WritePort(uint16_t Port, uint32_t Data, int Length) {
|
||||
if(Length == 1) { // Write a byte
|
||||
__asm__ __volatile__("outb %[value], %[address]" : : [value] "a" ((uint8_t) Data), [address] "d" (Port) :);
|
||||
} else if (Length == 2) { // Write a word
|
||||
__asm__ __volatile__("outw %[value], %[address]" : : [value] "a" ((uint16_t) Data), [address] "d" (Port) :);
|
||||
} else if (Length == 4) { // Write a long (dword)
|
||||
__asm__ __volatile__("outl %[value], %[address]" : : [value] "a" (Data), [address] "d" (Port) :);
|
||||
} else {
|
||||
printf("WritePort: Invalid Write Length.\r\n");
|
||||
}
|
||||
|
||||
return Data;
|
||||
}
|
||||
|
||||
size_t ReadModelSpecificRegister(size_t MSR) {
|
||||
size_t RegHigh = 0, RegLow = 0;
|
||||
__asm__ __volatile__("rdmsr" : "=a" (RegLow), "=d" (RegHigh) : "c" (MSR) :);
|
||||
return (RegHigh << 32 | RegLow);
|
||||
}
|
||||
|
||||
size_t WriteModelSpecificRegister(size_t MSR, size_t Data) {
|
||||
size_t DataLow = 0, DataHigh = 0;
|
||||
|
||||
DataLow = ((uint32_t* )&Data)[0];
|
||||
DataHigh = ((uint32_t* )&Data)[1];
|
||||
__asm__ __volatile__("wrmsr" : : "a" (DataLow), "c" (MSR), "d" (DataHigh) : );
|
||||
return Data;
|
||||
}
|
||||
|
||||
// VMXCSR - Vex-Encoded MXCSR. These are preferred when AVX is available.
|
||||
uint32_t ReadVexMXCSR() {
|
||||
uint32_t Data;
|
||||
__asm__ __volatile__("vstmxcsr %[dest]" : [dest] "=m" (Data) : :);
|
||||
return Data;
|
||||
}
|
||||
|
||||
uint32_t WriteVexMXCSR(uint32_t Data) {
|
||||
__asm__ __volatile__("vldmxcsr %[src]" : : [src] "m" (Data) :);
|
||||
return Data;
|
||||
}
|
||||
|
||||
// MXCSR - SSE Control Register.
|
||||
uint32_t ReadMXCSR() {
|
||||
uint32_t Data;
|
||||
__asm__ __volatile__("stmxcsr %[dest]" : [dest] "=m" (Data) : :);
|
||||
return Data;
|
||||
}
|
||||
|
||||
uint32_t WriteMXCSR(uint32_t Data) {
|
||||
__asm__ __volatile__("ldmxcsr %[src]" : : [src] "m" (Data) :);
|
||||
return Data;
|
||||
}
|
||||
|
||||
// Control Register : CRX + RFLAGS. Specify 'f' for RFLAGS, X for CRX.
|
||||
|
||||
size_t ReadControlRegister(int CRX) {
|
||||
size_t Data;
|
||||
switch(CRX) {
|
||||
case 0:
|
||||
__asm__ __volatile__("mov %%cr0, %[dest]" : [dest] "=r" (Data) : :);
|
||||
break;
|
||||
case 1:
|
||||
__asm__ __volatile__("mov %%cr1, %[dest]" : [dest] "=r" (Data) : :);
|
||||
break;
|
||||
case 2:
|
||||
__asm__ __volatile__("mov %%cr2, %[dest]" : [dest] "=r" (Data) : :);
|
||||
break;
|
||||
case 3:
|
||||
__asm__ __volatile__("mov %%cr3, %[dest]" : [dest] "=r" (Data) : :);
|
||||
break;
|
||||
case 4:
|
||||
__asm__ __volatile__("mov %%cr4, %[dest]" : [dest] "=r" (Data) : :);
|
||||
break;
|
||||
case 8:
|
||||
__asm__ __volatile__("mov %%cr8, %[dest]" : [dest] "=r" (Data) : :);
|
||||
break;
|
||||
case 'f':
|
||||
// Push flags and pop them into our buffer
|
||||
__asm__ __volatile__("pushfq\n\t" "popq %[dest]" : [dest] "=r" (Data) : :);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return Data;
|
||||
}
|
||||
|
||||
size_t WriteControlRegister(int CRX, size_t Data) {
|
||||
switch(CRX) {
|
||||
case 0:
|
||||
__asm__ __volatile__("mov %[dest], %%cr0" : : [dest] "r" (Data) :);
|
||||
break;
|
||||
case 1:
|
||||
__asm__ __volatile__("mov %[dest], %%cr1" : : [dest] "r" (Data) :);
|
||||
break;
|
||||
case 2:
|
||||
__asm__ __volatile__("mov %[dest], %%cr2" : : [dest] "r" (Data) :);
|
||||
break;
|
||||
case 3:
|
||||
__asm__ __volatile__("mov %[dest], %%cr3" : : [dest] "r" (Data) :);
|
||||
break;
|
||||
case 4:
|
||||
__asm__ __volatile__("mov %[dest], %%cr4" : : [dest] "r" (Data) :);
|
||||
break;
|
||||
case 8:
|
||||
__asm__ __volatile__("mov %[dest], %%cr8" : : [dest] "r" (Data) :);
|
||||
break;
|
||||
case 'f':
|
||||
__asm__ __volatile__("pushq %[dest]\n\t" "popfq" : : [dest] "r" (Data) : "cc");
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return Data;
|
||||
|
||||
}
|
||||
|
||||
// XCR = eXtended Control Register.
|
||||
// XCR0 is used to enable AVX/SSE.
|
||||
|
||||
size_t ReadExtendedControlRegister(size_t XCRX) {
|
||||
size_t RegHigh = 0, RegLow = 0;
|
||||
|
||||
__asm__ __volatile__("xgetbv" : "=a" (RegLow), "=d" (RegHigh) : "c" (XCRX) :);
|
||||
return (RegHigh << 32 | RegLow);
|
||||
}
|
||||
|
||||
size_t WriteExtendedControlRegister(size_t XCRX, size_t Data) {
|
||||
__asm__ __volatile__("xsetbv" : : "a" ( ((uint32_t*)&Data)[0]), "c" (XCRX), "d" ( ((uint32_t*)&Data)[1] ) :);
|
||||
return Data;
|
||||
}
|
||||
|
||||
// The following two functions are utility - for determining whether we're operating in Long Mode.
|
||||
// TODO: Move into DescriptorTables.c
|
||||
|
||||
size_t ReadXCS() {
|
||||
size_t Data = 0;
|
||||
__asm__ __volatile__("mov %%cs, %[dest]" : [dest] "=r" (Data) : :);
|
||||
return Data;
|
||||
}
|
||||
|
||||
DESCRIPTOR_TABLE_POINTER FetchGDT() {
|
||||
DESCRIPTOR_TABLE_POINTER GDTrData = {0};
|
||||
|
||||
__asm__ __volatile__("sgdt %[dest]" : [dest] "=m" (GDTrData) : :);
|
||||
return GDTrData;
|
||||
}
|
||||
|
||||
void SetGDT(DESCRIPTOR_TABLE_POINTER GDTrData) {
|
||||
__asm__ __volatile__("lgdt %[src]" : : [src] "m" (GDTrData) :);
|
||||
}
|
||||
|
||||
DESCRIPTOR_TABLE_POINTER FetchIDT() {
|
||||
DESCRIPTOR_TABLE_POINTER IDTrData = {0};
|
||||
__asm__ __volatile__("sidt %[dest]" : [dest] "=m" (IDTrData) : :);
|
||||
return IDTrData;
|
||||
}
|
||||
|
||||
void SetIDT(DESCRIPTOR_TABLE_POINTER IDTrData) {
|
||||
__asm__ __volatile__("lidt %[src]" : : [src] "m" (IDTrData) :);
|
||||
}
|
||||
|
||||
// LDT = Local Descriptor Table (= GDT entry for current segment)
|
||||
|
||||
uint16_t FetchLDT() {
|
||||
uint16_t LDTrData = 0;
|
||||
__asm__ __volatile__("sldt %[dest]" : [dest] "=m" (LDTrData) : :);
|
||||
return LDTrData;
|
||||
}
|
||||
|
||||
void SetLDT(uint16_t LDTrData) {
|
||||
__asm__ __volatile__("lldt %[src]" : : [src] "m" (LDTrData) :);
|
||||
}
|
||||
|
||||
// TSR - Tast State Register
|
||||
|
||||
uint16_t FetchTSR() {
|
||||
uint16_t TSRData = 0;
|
||||
__asm__ __volatile__ ("str %[dest]" : [dest] "=m" (TSRData) : :);
|
||||
return TSRData;
|
||||
}
|
||||
|
||||
void SetTSR(uint16_t TSRData) {
|
||||
__asm__ __volatile__("ltr %[src]" : : [src] "m" (TSRData) :);
|
||||
}
|
||||
|
||||
void InstallGDT() {
|
||||
DESCRIPTOR_TABLE_POINTER GDTData = {0};
|
||||
|
||||
|
|
|
@ -37,8 +37,6 @@ void SetupPrinting(EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE GPU) {
|
|||
Print_Info.cursorPos = 0;
|
||||
|
||||
Print_Info.scrollMode = 0;
|
||||
|
||||
ClearScreen(GPU);
|
||||
}
|
||||
|
||||
void WriteScaledFormatString(EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE GPU,
|
||||
|
|
|
@ -34,13 +34,11 @@
|
|||
|
||||
#include <kernel.h>
|
||||
|
||||
#ifdef __x86_64__
|
||||
typedef unsigned long long int uword_t;
|
||||
|
||||
|
||||
static void* IRQ_Handlers[16] = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
#else
|
||||
typedef unsigned int uword_t;
|
||||
#endif
|
||||
|
||||
|
||||
/* All of the ISR routines call this function for now.
|
||||
|
@ -50,10 +48,11 @@ static void* IRQ_Handlers[16] = {
|
|||
void ISR_Common(INTERRUPT_FRAME* Frame, size_t Exception) {
|
||||
/* Only the first 32 ISR/IRQs are reserved for exceptions by the CPU. We can handle up to 512 interrupts total, though. */
|
||||
if(Exception < 32) {
|
||||
/* ExceptionStrings is an array of c-strings defined in kernel.h */
|
||||
/* exception_messages is an array of c-strings defined in kernel.h */
|
||||
// TODO: Serial!
|
||||
|
||||
serial_print(ExceptionStrings[Exception]);
|
||||
serial_print(" Exception.\r\n");
|
||||
//serial_print(0x3F8, exception_messages[Exception]);
|
||||
//serial_print(0x3F8, " Exception.\r\n");
|
||||
printf("%s exception!", ExceptionStrings[Exception]);
|
||||
panic();
|
||||
}
|
||||
|
@ -63,8 +62,8 @@ void ISR_Common(INTERRUPT_FRAME* Frame, size_t Exception) {
|
|||
into what went wrong. In pure Curle style, though, we just ignore the error code. */
|
||||
void ISR_Error_Common(EXCEPTION_FRAME* Frame, size_t Exception) {
|
||||
if(Exception < 32) {
|
||||
serial_print(ExceptionStrings[Exception]);
|
||||
serial_printf(" Exception. Context given: %d\r\n", Frame->ErrorCode);
|
||||
//serial_print(0x3F8, ExceptionStrings[Exception]);
|
||||
//serial_printf(0x3F8, " Exception. Context given: %d\r\n", Frame->ErrorCode);
|
||||
printf("%s exception. Context: %x", ExceptionStrings[Exception], Frame->ErrorCode);
|
||||
panic();
|
||||
}
|
||||
|
|
|
@ -20,17 +20,13 @@
|
|||
|
||||
void gdb_end() {} /* GDB Debugging stump */
|
||||
|
||||
static size_t time = 0;
|
||||
|
||||
int kernel_main(FILELOADER_PARAMS* FLOP) {
|
||||
init_serial();
|
||||
serial_printf("Kernel has been given control of the computer.\nStarting bootstrap init.\n");
|
||||
/* The kernel is started in 64-bit Long Mode by Syncboot. */
|
||||
|
||||
/* PrepareSystem just initializes all hardware features and gets the system ready to execute every function.
|
||||
/* Most of this is just preparing the interrupts system, but there is a bit of messing with AVX features, which are used extensively in drawing graphics. */
|
||||
/* Here, we start by drawing a splash, then loading a GDT and IDT into the placeholder UEFI gives us. */
|
||||
|
||||
|
||||
/* Not sure how well serial would work in UEFI. */
|
||||
// TODO: look at this.
|
||||
PrepareSystem(FLOP);
|
||||
|
||||
gdb_end(); /* The first important step. Waypoint it for gdb debugging. */
|
||||
|
|
216
kernel/ports.c
216
kernel/ports.c
|
@ -1,216 +0,0 @@
|
|||
|
||||
/************************
|
||||
*** Team Kitty, 2019 ***
|
||||
*** Sync ***
|
||||
***********************/
|
||||
|
||||
/* This file contains code required for directly communicating with hardware.
|
||||
* This means ASM, most of the time. These are just wrapper functions that make ASM a little bit less daunting.
|
||||
*/
|
||||
|
||||
|
||||
#include <kernel.h>
|
||||
|
||||
|
||||
uint32_t ReadPort(uint16_t Port, int Length) {
|
||||
uint32_t Data;
|
||||
if(Length == 1) { // Read a byte
|
||||
__asm__ __volatile__("inb %[address], %[value]" : : [value] "a" ((uint8_t) Data), [address] "d" (Port) :);
|
||||
} else if (Length == 2) { // Read a word
|
||||
__asm__ __volatile__("inw %[address], %[value]" : : [value] "a" ((uint16_t) Data), [address] "d" (Port) :);
|
||||
} else if (Length == 4) { // Read a long (dword)
|
||||
__asm__ __volatile__("inl %[address], %[value]" : : [value] "a" (Data), [address] "d" (Port) :);
|
||||
} else {
|
||||
printf("ReadPort: Invalid Read Length.\r\n");
|
||||
}
|
||||
|
||||
return Data;
|
||||
}
|
||||
|
||||
uint32_t WritePort(uint16_t Port, uint32_t Data, int Length) {
|
||||
if(Length == 1) { // Write a byte
|
||||
__asm__ __volatile__("outb %[value], %[address]" : : [value] "a" ((uint8_t) Data), [address] "d" (Port) :);
|
||||
} else if (Length == 2) { // Write a word
|
||||
__asm__ __volatile__("outw %[value], %[address]" : : [value] "a" ((uint16_t) Data), [address] "d" (Port) :);
|
||||
} else if (Length == 4) { // Write a long (dword)
|
||||
__asm__ __volatile__("outl %[value], %[address]" : : [value] "a" (Data), [address] "d" (Port) :);
|
||||
} else {
|
||||
printf("WritePort: Invalid Write Length.\r\n");
|
||||
}
|
||||
|
||||
return Data;
|
||||
}
|
||||
|
||||
size_t ReadModelSpecificRegister(size_t MSR) {
|
||||
size_t RegHigh = 0, RegLow = 0;
|
||||
__asm__ __volatile__("rdmsr" : "=a" (RegLow), "=d" (RegHigh) : "c" (MSR) :);
|
||||
return (RegHigh << 32 | RegLow);
|
||||
}
|
||||
|
||||
size_t WriteModelSpecificRegister(size_t MSR, size_t Data) {
|
||||
size_t DataLow = 0, DataHigh = 0;
|
||||
|
||||
DataLow = ((uint32_t* )&Data)[0];
|
||||
DataHigh = ((uint32_t* )&Data)[1];
|
||||
__asm__ __volatile__("wrmsr" : : "a" (DataLow), "c" (MSR), "d" (DataHigh) : );
|
||||
return Data;
|
||||
}
|
||||
|
||||
// VMXCSR - Vex-Encoded MXCSR. These are preferred when AVX is available.
|
||||
uint32_t ReadVexMXCSR() {
|
||||
uint32_t Data;
|
||||
__asm__ __volatile__("vstmxcsr %[dest]" : [dest] "=m" (Data) : :);
|
||||
return Data;
|
||||
}
|
||||
|
||||
uint32_t WriteVexMXCSR(uint32_t Data) {
|
||||
__asm__ __volatile__("vldmxcsr %[src]" : : [src] "m" (Data) :);
|
||||
return Data;
|
||||
}
|
||||
|
||||
// MXCSR - SSE Control Register.
|
||||
uint32_t ReadMXCSR() {
|
||||
uint32_t Data;
|
||||
__asm__ __volatile__("stmxcsr %[dest]" : [dest] "=m" (Data) : :);
|
||||
return Data;
|
||||
}
|
||||
|
||||
uint32_t WriteMXCSR(uint32_t Data) {
|
||||
__asm__ __volatile__("ldmxcsr %[src]" : : [src] "m" (Data) :);
|
||||
return Data;
|
||||
}
|
||||
|
||||
// Control Register : CRX + RFLAGS. Specify 'f' for RFLAGS, X for CRX.
|
||||
|
||||
size_t ReadControlRegister(int CRX) {
|
||||
size_t Data;
|
||||
switch(CRX) {
|
||||
case 0:
|
||||
__asm__ __volatile__("mov %%cr0, %[dest]" : [dest] "=r" (Data) : :);
|
||||
break;
|
||||
case 1:
|
||||
__asm__ __volatile__("mov %%cr1, %[dest]" : [dest] "=r" (Data) : :);
|
||||
break;
|
||||
case 2:
|
||||
__asm__ __volatile__("mov %%cr2, %[dest]" : [dest] "=r" (Data) : :);
|
||||
break;
|
||||
case 3:
|
||||
__asm__ __volatile__("mov %%cr3, %[dest]" : [dest] "=r" (Data) : :);
|
||||
break;
|
||||
case 4:
|
||||
__asm__ __volatile__("mov %%cr4, %[dest]" : [dest] "=r" (Data) : :);
|
||||
break;
|
||||
case 8:
|
||||
__asm__ __volatile__("mov %%cr8, %[dest]" : [dest] "=r" (Data) : :);
|
||||
break;
|
||||
case 'f':
|
||||
// Push flags and pop them into our buffer
|
||||
__asm__ __volatile__("pushfq\n\t" "popq %[dest]" : [dest] "=r" (Data) : :);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return Data;
|
||||
}
|
||||
|
||||
size_t WriteControlRegister(int CRX, size_t Data) {
|
||||
switch(CRX) {
|
||||
case 0:
|
||||
__asm__ __volatile__("mov %[dest], %%cr0" : : [dest] "r" (Data) :);
|
||||
break;
|
||||
case 1:
|
||||
__asm__ __volatile__("mov %[dest], %%cr1" : : [dest] "r" (Data) :);
|
||||
break;
|
||||
case 2:
|
||||
__asm__ __volatile__("mov %[dest], %%cr2" : : [dest] "r" (Data) :);
|
||||
break;
|
||||
case 3:
|
||||
__asm__ __volatile__("mov %[dest], %%cr3" : : [dest] "r" (Data) :);
|
||||
break;
|
||||
case 4:
|
||||
__asm__ __volatile__("mov %[dest], %%cr4" : : [dest] "r" (Data) :);
|
||||
break;
|
||||
case 8:
|
||||
__asm__ __volatile__("mov %[dest], %%cr8" : : [dest] "r" (Data) :);
|
||||
break;
|
||||
case 'f':
|
||||
__asm__ __volatile__("pushq %[dest]\n\t" "popfq" : : [dest] "r" (Data) : "cc");
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return Data;
|
||||
|
||||
}
|
||||
|
||||
// XCR = eXtended Control Register.
|
||||
// XCR0 is used to enable AVX/SSE.
|
||||
|
||||
size_t ReadExtendedControlRegister(size_t XCRX) {
|
||||
size_t RegHigh = 0, RegLow = 0;
|
||||
|
||||
__asm__ __volatile__("xgetbv" : "=a" (RegLow), "=d" (RegHigh) : "c" (XCRX) :);
|
||||
return (RegHigh << 32 | RegLow);
|
||||
}
|
||||
|
||||
size_t WriteExtendedControlRegister(size_t XCRX, size_t Data) {
|
||||
__asm__ __volatile__("xsetbv" : : "a" ( ((uint32_t*)&Data)[0]), "c" (XCRX), "d" ( ((uint32_t*)&Data)[1] ) :);
|
||||
return Data;
|
||||
}
|
||||
|
||||
// The following two functions are utility - for determining whether we're operating in Long Mode.
|
||||
// TODO: Move into DescriptorTables.c
|
||||
|
||||
size_t ReadXCS() {
|
||||
size_t Data = 0;
|
||||
__asm__ __volatile__("mov %%cs, %[dest]" : [dest] "=r" (Data) : :);
|
||||
return Data;
|
||||
}
|
||||
|
||||
DESCRIPTOR_TABLE_POINTER FetchGDT() {
|
||||
DESCRIPTOR_TABLE_POINTER GDTrData = {0};
|
||||
|
||||
__asm__ __volatile__("sgdt %[dest]" : [dest] "=m" (GDTrData) : :);
|
||||
return GDTrData;
|
||||
}
|
||||
|
||||
void SetGDT(DESCRIPTOR_TABLE_POINTER GDTrData) {
|
||||
__asm__ __volatile__("lgdt %[src]" : : [src] "m" (GDTrData) :);
|
||||
}
|
||||
|
||||
DESCRIPTOR_TABLE_POINTER FetchIDT() {
|
||||
DESCRIPTOR_TABLE_POINTER IDTrData = {0};
|
||||
__asm__ __volatile__("sidt %[dest]" : [dest] "=m" (IDTrData) : :);
|
||||
return IDTrData;
|
||||
}
|
||||
|
||||
void SetIDT(DESCRIPTOR_TABLE_POINTER IDTrData) {
|
||||
__asm__ __volatile__("lidt %[src]" : : [src] "m" (IDTrData) :);
|
||||
}
|
||||
|
||||
// LDT = Local Descriptor Table (= GDT entry for current segment)
|
||||
|
||||
uint16_t FetchLDT() {
|
||||
uint16_t LDTrData = 0;
|
||||
__asm__ __volatile__("sldt %[dest]" : [dest] "=m" (LDTrData) : :);
|
||||
return LDTrData;
|
||||
}
|
||||
|
||||
void SetLDT(uint16_t LDTrData) {
|
||||
__asm__ __volatile__("lldt %[src]" : : [src] "m" (LDTrData) :);
|
||||
}
|
||||
|
||||
// TSR - Tast State Register
|
||||
|
||||
uint16_t FetchTSR() {
|
||||
uint16_t TSRData = 0;
|
||||
__asm__ __volatile__ ("str %[dest]" : [dest] "=m" (TSRData) : :);
|
||||
return TSRData;
|
||||
}
|
||||
|
||||
void SetTSR(uint16_t TSRData) {
|
||||
__asm__ __volatile__("ltr %[src]" : : [src] "m" (TSRData) :);
|
||||
}
|
|
@ -457,7 +457,7 @@ static void printchar(int Char, void* Args) {
|
|||
case '\a': // Alert
|
||||
// TODO: Audio alert.
|
||||
break;
|
||||
case '\b': // Back a space - NOT backspace, does not remove previous character.
|
||||
case '\b': // Backspace
|
||||
if(Arg->cursorPos != 0) {
|
||||
Arg->cursorPos--;
|
||||
}
|
||||
|
|
|
@ -35,13 +35,13 @@
|
|||
|
||||
void serial_set_baud_rate(uint16_t com, uint16_t divisor) {
|
||||
WritePort(SERIAL_LINE_COMMAND_PORT(com),
|
||||
SERIAL_LINE_ENABLE_DLAB, 1);
|
||||
SERIAL_LINE_ENABLE_DLAB, 8);
|
||||
|
||||
WritePort(SERIAL_DATA_PORT(com),
|
||||
(divisor >> 8) & 0x00FF, 1);
|
||||
(divisor >> 8) & 0x00FF, 8);
|
||||
|
||||
WritePort(SERIAL_DATA_PORT(com),
|
||||
divisor & 0x00FF, 1);
|
||||
divisor & 0x00FF, 8);
|
||||
|
||||
}
|
||||
|
||||
|
@ -53,12 +53,12 @@ void serial_set_baud_rate(uint16_t com, uint16_t divisor) {
|
|||
*/
|
||||
|
||||
void serial_configure_line(uint16_t com) {
|
||||
/* Bit: | 7 | 6 | 5 4 3 | 2 | 1 0 |
|
||||
* Content: | d | b | parity | s | dl |
|
||||
* Value: | 0 | 0 | 0 0 0 | 0 | 1 1 | = 0x03
|
||||
/* Bit: | 7 | 6 | 5 4 3 | 2 | 1 0 |
|
||||
* Content: | d | b | parity| s | dl |
|
||||
* Value: | 0 | 0 | 0 0 0 | 0 | 1 1 | = 0x03
|
||||
*/
|
||||
|
||||
WritePort(SERIAL_LINE_COMMAND_PORT(com), 0x0B, 1);
|
||||
WritePort(SERIAL_LINE_COMMAND_PORT(com), 0x0B, 8);
|
||||
}
|
||||
|
||||
/** serial_configure_buffers:
|
||||
|
@ -75,7 +75,7 @@ void serial_configure_buffers(uint16_t com) {
|
|||
* Value: | 1 1 | 0 | 0 | 0 | 1 | 1 | 1 | = 0xC7
|
||||
*/
|
||||
|
||||
WritePort(SERIAL_FIFO_COMMAND_PORT(com), 0xC7, 1);
|
||||
WritePort(SERIAL_FIFO_COMMAND_PORT(com), 0xC7, 8);
|
||||
}
|
||||
|
||||
/** serial_configure_modem
|
||||
|
@ -88,7 +88,7 @@ void serial_configure_modem(uint16_t com) {
|
|||
* Value: | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | = 0x03
|
||||
*/
|
||||
|
||||
WritePort(SERIAL_MODEM_COMMAND_PORT(com), 0x3, 1);
|
||||
WritePort(SERIAL_MODEM_COMMAND_PORT(com), 0x3, 8);
|
||||
}
|
||||
|
||||
/** serial_check_tqueue:
|
||||
|
@ -100,7 +100,7 @@ void serial_configure_modem(uint16_t com) {
|
|||
*/
|
||||
|
||||
int serial_check_tqueue(uint16_t com) {
|
||||
return ReadPort(SERIAL_LINE_STATUS_PORT(com), 1) & 0x20;
|
||||
return ReadPort(SERIAL_LINE_STATUS_PORT(com), 8) & 0x20;
|
||||
}
|
||||
|
||||
/** serial_write:
|
||||
|
@ -110,11 +110,10 @@ int serial_check_tqueue(uint16_t com) {
|
|||
* @param data The character to write.
|
||||
*/
|
||||
|
||||
void serial_write(const char chr) {
|
||||
uint16_t com = SERIAL_COM1_BASE;
|
||||
void serial_write(uint16_t com, const char chr) {
|
||||
//Hang until we have access to the COM port.
|
||||
while(serial_check_tqueue(com) == 0);
|
||||
WritePort(com, chr, 1);
|
||||
WritePort(com, chr, 0);
|
||||
}
|
||||
|
||||
/** serial_print:
|
||||
|
@ -125,9 +124,9 @@ void serial_write(const char chr) {
|
|||
* @param data The string to write.
|
||||
*/
|
||||
|
||||
void serial_print(const char* data) {
|
||||
void serial_print(uint16_t com, const char* data) {
|
||||
for(size_t i = 0; i < strlen(data); i++) {
|
||||
serial_write(data[i]);
|
||||
serial_write(com, data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -138,8 +137,7 @@ void serial_print(const char* data) {
|
|||
* @param ... The substitutions.
|
||||
*/
|
||||
|
||||
void serial_printf(const char* format, ...) {
|
||||
uint16_t com = SERIAL_COM1_BASE;
|
||||
void serial_printf(uint16_t com, const char* format, ...) {
|
||||
uint32_t storage; //To hold temporary variables
|
||||
char stringstore[10] = {0}; //To convert ints to strings.
|
||||
va_list list;
|
||||
|
@ -153,7 +151,7 @@ void serial_printf(const char* format, ...) {
|
|||
|
||||
storage = va_arg(list, int);
|
||||
int_to_ascii(storage, stringstore);
|
||||
serial_print(stringstore);
|
||||
serial_print(com, stringstore);
|
||||
empty_string(stringstore);
|
||||
i += 2;
|
||||
|
||||
|
@ -161,21 +159,21 @@ void serial_printf(const char* format, ...) {
|
|||
|
||||
storage = va_arg(list, int);
|
||||
int_to_hex(storage, stringstore);
|
||||
serial_print(stringstore);
|
||||
serial_print(com, stringstore);
|
||||
empty_string(stringstore);
|
||||
i += 2;
|
||||
|
||||
} else if(format[i+1] == 's') {
|
||||
|
||||
serial_print(va_arg(list, char*));
|
||||
serial_print(com, va_arg(list, char*));
|
||||
i += 2;
|
||||
|
||||
} else {
|
||||
serial_print("ERROR: Attempting to parse unknown format string.");
|
||||
serial_print(com, "ERROR: Attempting to parse unknown format string.");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
serial_write(format[i]);
|
||||
serial_write(com, format[i]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
@ -184,7 +182,7 @@ void serial_printf(const char* format, ...) {
|
|||
|
||||
void init_serial() {
|
||||
// Disable interrupts
|
||||
WritePort(SERIAL_COM1_BASE + 1, 0x00, 1);
|
||||
WritePort(SERIAL_COM1_BASE + 1, 0x00, 0);
|
||||
|
||||
// Set baud rate divisor.
|
||||
serial_set_baud_rate(SERIAL_COM1_BASE, 3);
|
||||
|
|
Loading…
Reference in New Issue
Block a user