Some assorted in-progress work.

This commit is contained in:
Curle 2022-07-08 02:28:20 +01:00
parent acda33948e
commit 3113fb08c3
7 changed files with 224 additions and 28 deletions

View File

@ -11,6 +11,10 @@ set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
SET(CMAKE_SYSTEM_NAME Generic) SET(CMAKE_SYSTEM_NAME Generic)
SET(CMAKE_CROSSCOMPILING 1) SET(CMAKE_CROSSCOMPILING 1)
enable_language(ASM)
enable_language(C)
enable_language(CXX)
project(chroma) project(chroma)
SET(src_files SET(src_files
@ -29,11 +33,13 @@ SET(src_files
${CMAKE_SOURCE_DIR}/src/system/memory/liballoc.cpp ${CMAKE_SOURCE_DIR}/src/system/memory/liballoc.cpp
${CMAKE_SOURCE_DIR}/src/system/memory/physmem.c ${CMAKE_SOURCE_DIR}/src/system/memory/physmem.c
${CMAKE_SOURCE_DIR}/src/system/process/process.cpp ${CMAKE_SOURCE_DIR}/src/system/process/process.cpp
${CMAKE_SOURCE_DIR}/src/system/extern/extern_defs.cpp
${CMAKE_SOURCE_DIR}/src/drivers/elf.cpp ${CMAKE_SOURCE_DIR}/src/drivers/elf.cpp
${CMAKE_SOURCE_DIR}/src/drivers/devices/devices.cpp ${CMAKE_SOURCE_DIR}/src/drivers/devices/devices.cpp
${CMAKE_SOURCE_DIR}/src/drivers/devices/input/keyboard.cpp ${CMAKE_SOURCE_DIR}/src/drivers/devices/input/keyboard.cpp
${CMAKE_SOURCE_DIR}/src/drivers/devices/io/apic.cpp ${CMAKE_SOURCE_DIR}/src/drivers/devices/io/apic.cpp
${CMAKE_SOURCE_DIR}/src/drivers/devices/storage/ata.cpp ${CMAKE_SOURCE_DIR}/src/drivers/devices/storage/ata.cpp
${CMAKE_SOURCE_DIR}/src/global/core-att.s
) )
SET(lib_files SET(lib_files
@ -50,6 +56,9 @@ SET(src_no_sse
${CMAKE_SOURCE_DIR}/src/system/interrupts.cpp ${CMAKE_SOURCE_DIR}/src/system/interrupts.cpp
) )
SET(src_as
)
SET(src_preamble SET(src_preamble
${CMAKE_SOURCE_DIR}/src/global/crt0.o ${CMAKE_SOURCE_DIR}/src/global/crt0.o
${CMAKE_SOURCE_DIR}/src/global/crti.o ${CMAKE_SOURCE_DIR}/src/global/crti.o
@ -69,6 +78,6 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
add_executable(kernel) add_executable(kernel)
target_sources(kernel PUBLIC ${src_preamble} PUBLIC ${src_files} PUBLIC ${src_no_sse} PUBLIC ${lib_files} PUBLIC ${src_epilogue}) target_sources(kernel PUBLIC ${src_preamble} PUBLIC ${src_files} PUBLIC ${src_no_sse} PUBLIC ${lib_files} PUBLIC ${src_as} PUBLIC ${src_epilogue})
target_compile_options(kernel PRIVATE -ffreestanding -O0 -Wall -Wextra -Wall -Werror -fPIC -fno-exceptions -fno-omit-frame-pointer -mno-red-zone -fno-stack-protector -fno-rtti -ggdb3) target_compile_options(kernel PRIVATE -ffreestanding -O0 -Wall -Wextra -Wall -Werror -fPIC -fno-exceptions -fno-omit-frame-pointer -mno-red-zone -fno-stack-protector $<$<COMPILE_LANGUAGE:CXX>:-fno-rtti> -ggdb3)
target_link_options(kernel PRIVATE -T ${CMAKE_SOURCE_DIR}/linker.ld -ffreestanding -O2 -nostdlib -nostartfiles -lgcc) target_link_options(kernel PRIVATE -T ${CMAKE_SOURCE_DIR}/linker.ld -ffreestanding -O2 -nostdlib -nostartfiles -lgcc)

128
src/global/core-att.s Normal file
View File

@ -0,0 +1,128 @@
#************************
#*** Team Kitty, 2021 ***
#*** Chroma ***
#************************
# Initial startup routines for new cores.
# New cores start in 16 bit real mode.
# Because of this, this is the only necessary assembler file in the OS.
# First, bring them up to Protected and Long mode.
# Then enable all necessary auxiliary features.
# Pass off to the CPP code to handle the heavy work, we just want the core running.
.code16
.equ BASE, 0x1000
.global stack
.extern initcpu
.extern coreidt
.extern CoreGDT
# 16-bit startup.
# Initialize registers.
# Load GDT
# Set flags
# Immediately jump to protected mode.
.global startCore
startCore:
cli
mov $0x0, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
mov %ax, %ss
lgdtl gdt_protected
mov %cr0, %eax
or $0x1, %ax
mov %eax, %cr0
ljmpl $0x8, $startCore32
.code32
# Protected mode setup.
# Set page tables
# Set PAE
# Immediately jump to long mode.
.section .text
startCore32:
mov $0x10, %bx
mov %bx, %ds
mov %bx, %es
mov %bx, %ss
mov $0xA000, %eax
mov %eax, %cr3
mov %cr4, %eax # Enable PAE
or $32, %eax # 1 << 5
or $128, %eax # 1 << 7
mov %eax, %cr4
mov $0xC0000080, %ecx
rdmsr
or $256, %eax # 1 << 8
wrmsr
mov %cr0, %eax
or $2147483648, %eax # 1 << 31
mov %eax, %cr0
lgdt gdt_long
ljmp $0x8, $startCore64
# Long mode setup.
# Prepare registers.
# Set flags
# Load the final GDT and IDT
# Jump to the leave function.
.code64
startCore64:
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %ss
mov $0x0, %ax
mov %ax, %ds
mov %ax, %gs
lgdt CoreGDT
lidt coreidt
mov $0x0, %rbp
push $0
popf
mov (leave), %rax
jmp leave
# Final setup.
# Set some flags in registers.
# Jump into C++ code.
leave:
push %rbp
mov %cr0, %rax
btr $2, %eax
bts $1, %eax
mov %rax, %cr0
mov %cr4, %rax
bts $9, %eax
bts $10, %eax
mov %rax, %cr4
call initcpu
.global endCore
endCore:

View File

@ -1,27 +1,29 @@
;************************ .intel_syntax noprefix
;*** Team Kitty, 2021 *** #************************
;*** Chroma *** #*** Team Kitty, 2021 ***
;************************ #*** Chroma ***
#************************
; Initial startup routines for new cores. # Initial startup routines for new cores.
; New cores start in 16 bit real mode. # New cores start in 16 bit real mode.
; Because of this, this is the only necessary assembler file in the OS. # Because of this, this is the only necessary assembler file in the OS.
; First, bring them up to Protected and Long mode. # First, bring them up to Protected and Long mode.
; Then enable all necessary auxiliary features. # Then enable all necessary auxiliary features.
; Pass off to the CPP code to handle the heavy work, we just want the core running. # Pass off to the CPP code to handle the heavy work, we just want the core running.
[bits 16] .intel_syntax
BASE equ 0x1000
global stack .code16
extern initcpu .equ BASE, 0x1000
extern coreidt .global stack
extern coregdt .extern initcpu
global startCore .extern coreidt
startCore: .extern coregdt
.global startCore
cli cli
mov ax, 0x0 mov ax, 0x0
mov ds, ax mov ds, ax
@ -38,9 +40,9 @@ startCore:
jmp 0x8:(startCore32 - startCore + BASE) jmp 0x8:(startCore32 - startCore + BASE)
[bits 32] .code32
section .text .section .text
startCore32: startCore32:
mov bx, 0x10 mov bx, 0x10
mov ds, bx mov ds, bx
@ -67,7 +69,7 @@ startCore32:
lgdt [gdt_long - startCore + BASE] lgdt [gdt_long - startCore + BASE]
jmp 8:(startCore64 - startCore + BASE) jmp 8:(startCore64 - startCore + BASE)
[bits 64] .code64
startCore64: startCore64:
mov ax, 0x10 mov ax, 0x10
mov ds, ax mov ds, ax

View File

@ -25,7 +25,6 @@ extern "C" void initcpu() {
__asm__ __volatile__("mov %%fs, %0" : : "r" (Device::APIC::driver->GetCurrentCore()) : ); __asm__ __volatile__("mov %%fs, %0" : : "r" (Device::APIC::driver->GetCurrentCore()) : );
SerialPrintf("[CORE] Core %d ready.\r\n", Device::APIC::driver->GetCurrentCore()); SerialPrintf("[CORE] Core %d ready.\r\n", Device::APIC::driver->GetCurrentCore());
// TODO: New GDT
__asm__ __volatile__("cli"); __asm__ __volatile__("cli");
Ready = true; Ready = true;
__asm__ __volatile__("sti"); __asm__ __volatile__("sti");

View File

@ -44,6 +44,11 @@ __attribute__((aligned(64))) static volatile size_t InitGDT[5] = {
0 0
}; };
__attribute__((aligned(64))) static DESC_TBL CoreGDT = {
.Limit = sizeof(InitGDT),
.Base = (size_t) InitGDT
};
__attribute__((aligned(64))) static volatile TSS64 TSSEntries; __attribute__((aligned(64))) static volatile TSS64 TSSEntries;
__attribute__((aligned(64))) static volatile IDT_GATE IDTEntries[256]; __attribute__((aligned(64))) static volatile IDT_GATE IDTEntries[256];
@ -111,7 +116,6 @@ void PrepareCPU() {
*/ */
void SetupInitialGDT() { void SetupInitialGDT() {
DESC_TBL GDTData;
size_t TSSBase = (uint64_t) (&TSSEntries); size_t TSSBase = (uint64_t) (&TSSEntries);
uint16_t TSSLower = (uint16_t) TSSBase; uint16_t TSSLower = (uint16_t) TSSBase;
@ -119,15 +123,13 @@ void SetupInitialGDT() {
uint8_t TSSMid2 = (uint8_t) (TSSBase >> 24); uint8_t TSSMid2 = (uint8_t) (TSSBase >> 24);
uint32_t TSSHigher = (uint32_t) (TSSBase >> 32); uint32_t TSSHigher = (uint32_t) (TSSBase >> 32);
GDTData.Limit = sizeof(InitGDT) - 1;
GDTData.Base = (size_t) InitGDT;
((TSS_ENTRY*) (&((GDT_ENTRY*) InitGDT)[3]))->BaseLow = TSSLower; ((TSS_ENTRY*) (&((GDT_ENTRY*) InitGDT)[3]))->BaseLow = TSSLower;
((TSS_ENTRY*) (&((GDT_ENTRY*) InitGDT)[3]))->BaseMid1 = TSSMid1; ((TSS_ENTRY*) (&((GDT_ENTRY*) InitGDT)[3]))->BaseMid1 = TSSMid1;
((TSS_ENTRY*) (&((GDT_ENTRY*) InitGDT)[3]))->BaseMid2 = TSSMid2; ((TSS_ENTRY*) (&((GDT_ENTRY*) InitGDT)[3]))->BaseMid2 = TSSMid2;
((TSS_ENTRY*) (&((GDT_ENTRY*) InitGDT)[3]))->BaseHigh = TSSHigher; ((TSS_ENTRY*) (&((GDT_ENTRY*) InitGDT)[3]))->BaseHigh = TSSHigher;
WriteGDT(GDTData); WriteGDT(CoreGDT);
WriteTSR(3 << 3); WriteTSR(3 << 3);
RefreshCS(); RefreshCS();
} }

55
src/system/extern/extern_defs.cpp vendored Normal file
View File

@ -0,0 +1,55 @@
/************************
*** Team Kitty, 2022 ***
*** Chroma ***
***********************/
#include <kernel/system/descriptors.h>
/**
* Contains definitions that are used by external files.
* That means, independent programs or assembler files.
*/
/**
* The template GDT entry for a newly initialized core.
* Protected Mode.
* TODO: Document what the entries here are.
*/
__attribute__((aligned(64))) size_t ProtectedGDTEntry[3] = {
0,
0x00CF9A000000FFFF,
0x00CF92000000FFFF
};
/**
* The GDT table value to be loaded into each newly initialized core.
* Protected Mode.
*/
DESC_TBL ProtectedGDT = {
.Limit = sizeof(ProtectedGDTEntry) - 1,
.Base = (size_t) &ProtectedGDTEntry
};
/**
* The template GDT entry for a newly initialized core.
* Long Mode.
* TODO: Document what the entries here are.
*/
__attribute__((aligned(64))) size_t LongGDTEntry[3] = {
0,
0x00AF98000000FFFF,
0x00CF92000000FFFF
};
/**
* The GDT table value to be loaded into each newly initialized core.
* Long Mode.
*/
DESC_TBL LongGDT = {
.Limit = sizeof(LongGDTEntry) - 1,
.Base = (size_t) &LongGDTEntry
};

View File

@ -7,6 +7,7 @@
*** Chroma *** *** Chroma ***
***********************/ ***********************/
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif