From 6ffe96d1e3f0289c2070e050cf8e1a30288e6d91 Mon Sep 17 00:00:00 2001 From: Alexey Gavrilov Date: Fri, 27 Mar 2026 14:03:55 +0700 Subject: Cmake build generator support: build stage Multi-target architecture implementation: each root source code directory presents the one *INTERFACE* target to it's subdirectory subtargets that link only as *STATIC* library. For instance, *arch* directory presents *Architecture* target then *Architecture* target has *CPUID*, *IO*, *PORT* and etc. dependencies Exact order of linking targets is: boot.o CRTIBUNDLE CRTNBUNDLE. Sequence destruction cause to UB. Please to pay attention to this. TODO - Debug information - Build type - Toolchain file - Image creation - Build by WinGW compiler on Windows platform (optional) Signed-off-by: Alexey Gavrilov --- CMakeLists.txt | 99 +++++++++++++++++++++++++++++++++++++++ arch/CMakeLists.txt | 17 +++++++ arch/SecondaryInit.h | 2 +- arch/x86/CMakeLists.txt | 34 ++++++++++++++ arch/x86/boot.nasm | 16 +++++++ arch/x86/cpuid/CMakeLists.txt | 25 ++++++++++ arch/x86/cpuid/Cpuid.h | 2 +- arch/x86/cpuid/CpuidCheck.nasm | 21 +++++++++ arch/x86/cpuid/CpuidCheck.s | 21 --------- arch/x86/gdt/CMakeLists.txt | 23 +++++++++ arch/x86/gdt/Gdt.h | 2 +- arch/x86/gdt/SwitchToGdt.nasm | 13 +++++ arch/x86/gdt/SwitchToGdt.s | 13 ----- arch/x86/ic/CMakeLists.txt | 16 +++++++ arch/x86/ic/IPic.h | 2 +- arch/x86/idt/CMakeLists.txt | 15 ++++++ arch/x86/io/CMakeLists.txt | 16 +++++++ arch/x86/io/VGADisplayInfo.h | 2 +- arch/x86/msr/CMakeLists.txt | 15 ++++++ arch/x86/port/CMakeLists.txt | 16 +++++++ arch/x86/port/PortOps.h | 2 +- cmake/module/metadata.cmake | 8 ++++ drivers/CMakeLists.txt | 3 ++ drivers/acpi/Acpi.h | 2 +- drivers/acpi/CMakeLists.txt | 17 +++++++ drivers/acpi/Ics.h | 2 +- include/CMakeLists.txt | 27 +++++++++++ include/MemoryOperations.h | 2 +- include/StringOperations.h | 2 +- include/Templates.h | 2 +- include/TypeConverter.h | 2 +- include/Types.h | 29 ++++++++++++ include/containers/CMakeLists.txt | 10 ++++ include/types.h | 29 ------------ kernel/CMakeLists.txt | 14 ++++++ kernel/boot.s | 16 ------- kernel/details/CMakeLists.txt | 8 ++++ kernel/kernel.c | 19 -------- kernel/kernel.cpp | 19 ++++++++ libgcc/CMakeLists.txt | 25 ++++++++++ libgcc/x86/CMakeLists.txt | 15 ++++++ libgcc/x86/crti.nasm | 5 ++ libgcc/x86/crti.s | 5 -- libgcc/x86/crtn.nasm | 3 ++ libgcc/x86/crtn.s | 3 -- makefile | 72 ---------------------------- 46 files changed, 521 insertions(+), 190 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 arch/CMakeLists.txt create mode 100644 arch/x86/CMakeLists.txt create mode 100644 arch/x86/boot.nasm create mode 100644 arch/x86/cpuid/CMakeLists.txt create mode 100644 arch/x86/cpuid/CpuidCheck.nasm delete mode 100644 arch/x86/cpuid/CpuidCheck.s create mode 100644 arch/x86/gdt/CMakeLists.txt create mode 100644 arch/x86/gdt/SwitchToGdt.nasm delete mode 100644 arch/x86/gdt/SwitchToGdt.s create mode 100644 arch/x86/ic/CMakeLists.txt create mode 100644 arch/x86/idt/CMakeLists.txt create mode 100644 arch/x86/io/CMakeLists.txt create mode 100644 arch/x86/msr/CMakeLists.txt create mode 100644 arch/x86/port/CMakeLists.txt create mode 100644 cmake/module/metadata.cmake create mode 100644 drivers/CMakeLists.txt create mode 100644 drivers/acpi/CMakeLists.txt create mode 100644 include/CMakeLists.txt create mode 100644 include/Types.h create mode 100644 include/containers/CMakeLists.txt delete mode 100644 include/types.h create mode 100644 kernel/CMakeLists.txt delete mode 100644 kernel/boot.s create mode 100644 kernel/details/CMakeLists.txt delete mode 100644 kernel/kernel.c create mode 100644 kernel/kernel.cpp create mode 100644 libgcc/CMakeLists.txt create mode 100644 libgcc/x86/CMakeLists.txt create mode 100644 libgcc/x86/crti.nasm delete mode 100644 libgcc/x86/crti.s create mode 100644 libgcc/x86/crtn.nasm delete mode 100644 libgcc/x86/crtn.s delete mode 100644 makefile diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..33ac2dc --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,99 @@ +# Regard that file as root of whole project +# which include farther subprojects source codes + +# Expected input variables: +# ARCH: type of architecture + +set(CXX_FLAGS + -Wall + -ffreestanding + -nostdinc + -nostdlib + -fno-rtti + -mgeneral-regs-only +) + +#---DEV PROPERTIES--- (delete in production) +# message(STATUS ${CMAKE_ASM_NASM_COMPILER}) +set(CMAKE_SYSTEM_NAME Generic) +set(CMAKE_ASM_NASM_OBJECT_FORMAT elf32) +set(CMAKE_CXX_COMPILER i686-elf-g++) +set(CMAKE_CXX_COMPILER_WORKS 1) +# set(CMAKE_CUSTOM_LINKER i686-elf-ld) +# set(CMAKE_CXX_LINK_EXECUTABLE +# "ld -o ") +# set(CMAKE_CXX_FLAGS "-I${CMAKE_CURRENT_LIST_DIR}/include") +# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32 -Wall -ffreestanding -nostdinc -nostdlib -fno-rtti -mgeneral-regs-only") +# message(STATUS ${CMAKE_ASM_NASM_FLAGS}) +# message(STATUS ${CMAKE_ASM_NASM_OBJECT_FORMAT}) +# message(STATUS ${CMAKE_CXX_LINK_EXECUTABLE}) +# message(STATUS ${CMAKE_CXX_LINK_FLAGS}) +#-------------------------------------------- + +cmake_minimum_required(VERSION 3.25) + +project(ObjectiveProject LANGUAGES CXX ASM_NASM) + +list(APPEND CMAKE_MODULE_PATH + "${ObjectiveProject_SOURCE_DIR}/cmake/module" +) + +include(metadata) + +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +add_executable(Objective) + +# Set architecture by default if not present +if(NOT ARCH IN_LIST OBJECTIVE_SUPPORTED_PROCESSORS) + list(GET OBJECTIVE_SUPPORTED_PROCESSORS 0 ARCH) +endif() + +# Create target with compiler requirements (include directories, flags and etc) +add_library(CXX_COMPILER_REQUIREMENTS INTERFACE) + +# Target with linker requirements +add_library(CXX_LINKER_REQUIREMENTS INTERFACE) +target_link_options(CXX_LINKER_REQUIREMENTS INTERFACE + ${CXX_FLAGS} +) +target_link_libraries(CXX_LINKER_REQUIREMENTS INTERFACE + gcc +) + +# Set compiler flags +if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU") + target_compile_options(CXX_COMPILER_REQUIREMENTS INTERFACE + ${CXX_FLAGS} + ) +endif() + +target_include_directories(CXX_COMPILER_REQUIREMENTS + INTERFACE + ${ObjectiveProject_SOURCE_DIR}/include +) + +# Subscribe compiler requirements to Object executable +target_link_libraries(Objective PUBLIC CXX_COMPILER_REQUIREMENTS) + +add_subdirectory(arch) +add_subdirectory(kernel) +add_subdirectory(drivers) +add_subdirectory(include) +add_subdirectory(libgcc) + +target_sources(Objective PRIVATE + $ +) + +target_link_libraries(Objective PRIVATE + CRTI + Architecture + Kernel + Drivers + CRTN +) + +target_link_libraries(Objective PRIVATE CXX_LINKER_REQUIREMENTS) diff --git a/arch/CMakeLists.txt b/arch/CMakeLists.txt new file mode 100644 index 0000000..c8719ce --- /dev/null +++ b/arch/CMakeLists.txt @@ -0,0 +1,17 @@ +add_library(Architecture INTERFACE) + +add_library(Initialization STATIC) +target_link_libraries(Initialization PUBLIC CXX_COMPILER_REQUIREMENTS) + +target_sources(Initialization PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/SecondaryInit.cpp +) + +target_sources(Initialization + PUBLIC + FILE_SET HEADERS + FILES + ${CMAKE_CURRENT_LIST_DIR}/SecondaryInit.h +) + +add_subdirectory(${ARCH}) diff --git a/arch/SecondaryInit.h b/arch/SecondaryInit.h index 53ff191..104f44c 100644 --- a/arch/SecondaryInit.h +++ b/arch/SecondaryInit.h @@ -1,6 +1,6 @@ -#include #include "../kernel/mem/Segments.h" #include "../kernel/mem/Kheap.h" +#include #if defined(__IAPC) #include "x86/PrimaryInit.h" diff --git a/arch/x86/CMakeLists.txt b/arch/x86/CMakeLists.txt new file mode 100644 index 0000000..5601efa --- /dev/null +++ b/arch/x86/CMakeLists.txt @@ -0,0 +1,34 @@ +target_link_options(CXX_LINKER_REQUIREMENTS INTERFACE + LINKER:-L${CMAKE_CURRENT_LIST_DIR} +) + +target_sources(Initialization PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/PrimaryInit.cpp +) + +target_sources(Initialization + PUBLIC + FILE_SET HEADERS + FILES + ${CMAKE_CURRENT_LIST_DIR}/PrimaryInit.h +) + +target_link_libraries(Architecture INTERFACE Initialization) + +# Create library for assembler build +add_library(ArchitectureAssemblerSources STATIC) + +add_library(Boot OBJECT) +target_sources(Boot PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/boot.nasm +) + +add_subdirectory(cpuid) +add_subdirectory(idt) +add_subdirectory(gdt) +add_subdirectory(ic) +add_subdirectory(io) +add_subdirectory(msr) +add_subdirectory(port) + +target_link_libraries(Architecture INTERFACE ArchitectureAssemblerSources) diff --git a/arch/x86/boot.nasm b/arch/x86/boot.nasm new file mode 100644 index 0000000..11175af --- /dev/null +++ b/arch/x86/boot.nasm @@ -0,0 +1,16 @@ +section .text +extern main, _estack, _init +global _start:function +_start: + ; Stack setup + mov eax, _estack + ; Align stack to odd address + and eax, 0xFFFFFFFE + mov esp, eax + + call _init + call main + cli +.hang: hlt + jmp .hang +; .end: diff --git a/arch/x86/cpuid/CMakeLists.txt b/arch/x86/cpuid/CMakeLists.txt new file mode 100644 index 0000000..e8528e8 --- /dev/null +++ b/arch/x86/cpuid/CMakeLists.txt @@ -0,0 +1,25 @@ +add_library(CPUID STATIC) +target_link_libraries(CPUID PUBLIC CXX_COMPILER_REQUIREMENTS) + +target_sources(CPUID PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/Cpuid.cpp +) + +target_sources(CPUID + PUBLIC + FILE_SET HEADERS + BASE_DIRS + ${CMAKE_CURRENT_LIST_DIR} + FILES + ${CMAKE_CURRENT_LIST_DIR}/Cpuid.h +) + +target_link_libraries(Architecture INTERFACE CPUID) + +add_library(CPUIDASM OBJECT) + +target_sources(CPUIDASM PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/CpuidCheck.nasm +) + +target_sources(ArchitectureAssemblerSources PRIVATE $) diff --git a/arch/x86/cpuid/Cpuid.h b/arch/x86/cpuid/Cpuid.h index a65cdc8..c8ad4aa 100644 --- a/arch/x86/cpuid/Cpuid.h +++ b/arch/x86/cpuid/Cpuid.h @@ -1,6 +1,6 @@ #ifndef CPUID_H #define CPUID_H -#include +#include #include // To declare cpuid::Executor::Executor() diff --git a/arch/x86/cpuid/CpuidCheck.nasm b/arch/x86/cpuid/CpuidCheck.nasm new file mode 100644 index 0000000..67bc9eb --- /dev/null +++ b/arch/x86/cpuid/CpuidCheck.nasm @@ -0,0 +1,21 @@ +global CPUIDCHK:function +CPUIDCHK: + push ebx + pushfd + pop eax + mov ebx, eax + xor eax, 0x200000 + push eax + popfd + pushfd + pop eax + xor eax, ebx + je nocpuid + mov eax, 1 + jmp restore +nocpuid: + mov eax, 0 +restore: + pop ebx + ret +.endchk: diff --git a/arch/x86/cpuid/CpuidCheck.s b/arch/x86/cpuid/CpuidCheck.s deleted file mode 100644 index 67bc9eb..0000000 --- a/arch/x86/cpuid/CpuidCheck.s +++ /dev/null @@ -1,21 +0,0 @@ -global CPUIDCHK:function -CPUIDCHK: - push ebx - pushfd - pop eax - mov ebx, eax - xor eax, 0x200000 - push eax - popfd - pushfd - pop eax - xor eax, ebx - je nocpuid - mov eax, 1 - jmp restore -nocpuid: - mov eax, 0 -restore: - pop ebx - ret -.endchk: diff --git a/arch/x86/gdt/CMakeLists.txt b/arch/x86/gdt/CMakeLists.txt new file mode 100644 index 0000000..57e3c26 --- /dev/null +++ b/arch/x86/gdt/CMakeLists.txt @@ -0,0 +1,23 @@ +add_library(GDT STATIC) +target_link_libraries(GDT PUBLIC CXX_COMPILER_REQUIREMENTS) + +target_sources(GDT PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/Gdt.cpp +) + +target_sources(GDT + PUBLIC + FILE_SET HEADERS + FILES + ${CMAKE_CURRENT_LIST_DIR}/Gdt.h +) + +target_link_libraries(Architecture INTERFACE GDT) + +add_library(GDTASM OBJECT) + +target_sources(GDTASM PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/SwitchToGdt.nasm +) + +target_sources(ArchitectureAssemblerSources PRIVATE $) diff --git a/arch/x86/gdt/Gdt.h b/arch/x86/gdt/Gdt.h index 321d67c..8f1d967 100755 --- a/arch/x86/gdt/Gdt.h +++ b/arch/x86/gdt/Gdt.h @@ -1,7 +1,7 @@ #ifndef GDT_H #define GDT_H -#include +#include #include diff --git a/arch/x86/gdt/SwitchToGdt.nasm b/arch/x86/gdt/SwitchToGdt.nasm new file mode 100644 index 0000000..52ebbc7 --- /dev/null +++ b/arch/x86/gdt/SwitchToGdt.nasm @@ -0,0 +1,13 @@ +global SWITCHTOGDT +SWITCHTOGDT: + lgdt [esp + 4] + jmp 0x08:.reloadCS +.reloadCS: + mov ax, 0x10 + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + mov ss, ax + ret +.end: diff --git a/arch/x86/gdt/SwitchToGdt.s b/arch/x86/gdt/SwitchToGdt.s deleted file mode 100644 index 52ebbc7..0000000 --- a/arch/x86/gdt/SwitchToGdt.s +++ /dev/null @@ -1,13 +0,0 @@ -global SWITCHTOGDT -SWITCHTOGDT: - lgdt [esp + 4] - jmp 0x08:.reloadCS -.reloadCS: - mov ax, 0x10 - mov ds, ax - mov es, ax - mov fs, ax - mov gs, ax - mov ss, ax - ret -.end: diff --git a/arch/x86/ic/CMakeLists.txt b/arch/x86/ic/CMakeLists.txt new file mode 100644 index 0000000..e6130d7 --- /dev/null +++ b/arch/x86/ic/CMakeLists.txt @@ -0,0 +1,16 @@ +add_library(IC STATIC) +target_link_libraries(IC PUBLIC CXX_COMPILER_REQUIREMENTS) + +target_sources(IC PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/Pic.cpp +) + +target_sources(IC + PUBLIC + FILE_SET HEADERS + FILES + ${CMAKE_CURRENT_LIST_DIR}/Pic.h + ${CMAKE_CURRENT_LIST_DIR}/IPic.h +) + +target_link_libraries(Architecture INTERFACE IC) diff --git a/arch/x86/ic/IPic.h b/arch/x86/ic/IPic.h index 9bb1b4f..5e0449b 100644 --- a/arch/x86/ic/IPic.h +++ b/arch/x86/ic/IPic.h @@ -1,7 +1,7 @@ #ifndef IPIC_H #define IPIC_H -#include +#include // Defines diff --git a/arch/x86/idt/CMakeLists.txt b/arch/x86/idt/CMakeLists.txt new file mode 100644 index 0000000..da7bdae --- /dev/null +++ b/arch/x86/idt/CMakeLists.txt @@ -0,0 +1,15 @@ +add_library(IDT STATIC) +target_link_libraries(IDT PUBLIC CXX_COMPILER_REQUIREMENTS) + +target_sources(IDT PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/Idt.cpp +) + +target_sources(IDT + PUBLIC + FILE_SET HEADERS + FILES + ${CMAKE_CURRENT_LIST_DIR}/Idt.h +) + +target_link_libraries(Architecture INTERFACE IDT) diff --git a/arch/x86/io/CMakeLists.txt b/arch/x86/io/CMakeLists.txt new file mode 100644 index 0000000..9c2dd33 --- /dev/null +++ b/arch/x86/io/CMakeLists.txt @@ -0,0 +1,16 @@ +add_library(IO STATIC) +target_link_libraries(IO PUBLIC CXX_COMPILER_REQUIREMENTS) + +target_sources(IO PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/VGADisplay.cpp +) + +target_sources(IO + PUBLIC + FILE_SET HEADERS + FILES + ${CMAKE_CURRENT_LIST_DIR}/VGADisplay.h + ${CMAKE_CURRENT_LIST_DIR}/VGADisplayInfo.h +) + +target_link_libraries(Architecture INTERFACE IO) diff --git a/arch/x86/io/VGADisplayInfo.h b/arch/x86/io/VGADisplayInfo.h index 875e7e5..55303dd 100644 --- a/arch/x86/io/VGADisplayInfo.h +++ b/arch/x86/io/VGADisplayInfo.h @@ -1,7 +1,7 @@ #ifndef VGAINFOCLASS_H #define VGAINFOCLASS_H -#include +#include #define TOBG(attr) attr << 4 diff --git a/arch/x86/msr/CMakeLists.txt b/arch/x86/msr/CMakeLists.txt new file mode 100644 index 0000000..86d3330 --- /dev/null +++ b/arch/x86/msr/CMakeLists.txt @@ -0,0 +1,15 @@ +add_library(MSR STATIC) +target_link_libraries(MSR PUBLIC CXX_COMPILER_REQUIREMENTS) + +target_sources(MSR PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/Msr.cpp +) + +target_sources(MSR + PUBLIC + FILE_SET HEADERS + FILES + ${CMAKE_CURRENT_LIST_DIR}/Msr.h +) + +target_link_libraries(Architecture INTERFACE MSR) diff --git a/arch/x86/port/CMakeLists.txt b/arch/x86/port/CMakeLists.txt new file mode 100644 index 0000000..6ad61ef --- /dev/null +++ b/arch/x86/port/CMakeLists.txt @@ -0,0 +1,16 @@ +add_library(Port STATIC) +target_link_libraries(Port PUBLIC CXX_COMPILER_REQUIREMENTS) + +target_sources(Port PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/PortOps.cpp +) + +target_sources(Port + PUBLIC + FILE_SET HEADERS + FILES + ${CMAKE_CURRENT_LIST_DIR}/PortOps.h + ${CMAKE_CURRENT_LIST_DIR}/Operations +) + +target_link_libraries(Architecture INTERFACE Port) diff --git a/arch/x86/port/PortOps.h b/arch/x86/port/PortOps.h index 7db3a5f..f8dac63 100644 --- a/arch/x86/port/PortOps.h +++ b/arch/x86/port/PortOps.h @@ -1,7 +1,7 @@ #ifndef PORT_H #define PORT_H -#include +#include using regb = uint8; diff --git a/cmake/module/metadata.cmake b/cmake/module/metadata.cmake new file mode 100644 index 0000000..fec5bf8 --- /dev/null +++ b/cmake/module/metadata.cmake @@ -0,0 +1,8 @@ +# FIX: get elements from ObjectiveProject_ROOT/arch directory +set(OBJECTIVE_SUPPORTED_PROCESSORS x86;arm/sunxi CACHE STRING "Supported +processors by Objective kernel") + +# FIX: get apps from docs +set(OBJECTIVE_SUPPORTED_ISO_CREATION_APPS grub-mkrescue CACHE STRING "Supported +apps to create ISO image") + diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt new file mode 100644 index 0000000..c11b75f --- /dev/null +++ b/drivers/CMakeLists.txt @@ -0,0 +1,3 @@ +add_library(Drivers INTERFACE) + +add_subdirectory(acpi) diff --git a/drivers/acpi/Acpi.h b/drivers/acpi/Acpi.h index 506ab99..d9dc378 100644 --- a/drivers/acpi/Acpi.h +++ b/drivers/acpi/Acpi.h @@ -1,4 +1,4 @@ -#include +#include #include #include "Ics.h" diff --git a/drivers/acpi/CMakeLists.txt b/drivers/acpi/CMakeLists.txt new file mode 100644 index 0000000..32f2fac --- /dev/null +++ b/drivers/acpi/CMakeLists.txt @@ -0,0 +1,17 @@ +add_library(ACPI STATIC) +target_link_libraries(ACPI PUBLIC CXX_COMPILER_REQUIREMENTS) + +target_sources(ACPI PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/Acpi.cpp + ${CMAKE_CURRENT_LIST_DIR}/Ics.cpp +) + +target_sources(ACPI + PUBLIC + FILE_SET HEADERS + FILES + ${CMAKE_CURRENT_LIST_DIR}/Acpi.h + ${CMAKE_CURRENT_LIST_DIR}/Ics.h +) + +target_link_libraries(Drivers INTERFACE ACPI) diff --git a/drivers/acpi/Ics.h b/drivers/acpi/Ics.h index 8289c90..b4e38d4 100644 --- a/drivers/acpi/Ics.h +++ b/drivers/acpi/Ics.h @@ -1,4 +1,4 @@ -#include +#include class Ics diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt new file mode 100644 index 0000000..1b06451 --- /dev/null +++ b/include/CMakeLists.txt @@ -0,0 +1,27 @@ +add_library(Include STATIC) + +target_sources(Include + PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/MemoryOperations.cpp + ${CMAKE_CURRENT_LIST_DIR}/StringOperations.cpp + ${CMAKE_CURRENT_LIST_DIR}/TypeConverter.cpp +) + +target_sources(Include + PUBLIC + FILE_SET HEADERS + FILES + ${CMAKE_CURRENT_LIST_DIR}/ISingleton.h + ${CMAKE_CURRENT_LIST_DIR}/MemoryOperations.h + ${CMAKE_CURRENT_LIST_DIR}/StringOperations.h + ${CMAKE_CURRENT_LIST_DIR}/Templates.h + ${CMAKE_CURRENT_LIST_DIR}/TypeConverter.h + ${CMAKE_CURRENT_LIST_DIR}/Types.h +) + +add_subdirectory(containers) + +# Link include library to all consumers +target_link_libraries(Architecture INTERFACE Include) +target_link_libraries(Kernel INTERFACE Include) +target_link_libraries(Drivers INTERFACE Include) diff --git a/include/MemoryOperations.h b/include/MemoryOperations.h index 046ca1d..ef116dd 100644 --- a/include/MemoryOperations.h +++ b/include/MemoryOperations.h @@ -1,7 +1,7 @@ #ifndef MEMORYOPERATIONS_H #define MEMORYOPERATIONS_H -#include +#include class MemoryOperations diff --git a/include/StringOperations.h b/include/StringOperations.h index 5d2826c..53519fe 100644 --- a/include/StringOperations.h +++ b/include/StringOperations.h @@ -1,7 +1,7 @@ #ifndef STRINGOPERATIONS_H #define STRINGOPERATIONS_H -#include +#include class MemArea diff --git a/include/Templates.h b/include/Templates.h index 8a1bd95..00b97d3 100644 --- a/include/Templates.h +++ b/include/Templates.h @@ -2,7 +2,7 @@ #define TEMPLATES_H -#include +#include template struct constant diff --git a/include/TypeConverter.h b/include/TypeConverter.h index f9db95c..8b426b9 100644 --- a/include/TypeConverter.h +++ b/include/TypeConverter.h @@ -1,7 +1,7 @@ #ifndef TYPECONVERTER_H #define TYPECONVERTER_H -#include +#include class IntConverter { diff --git a/include/Types.h b/include/Types.h new file mode 100644 index 0000000..9f59063 --- /dev/null +++ b/include/Types.h @@ -0,0 +1,29 @@ +#ifndef TYPES_H +#define TYPES_H + +#define __IAPC + +#define NULL 0 + +#if defined(__IAPC) +typedef unsigned char uint8; +typedef unsigned short int uint16; +typedef unsigned long uint32; +typedef unsigned long long uint64; + +#if defined(__x86_64__) + typedef uint64 uword; +#else + typedef uint32 uword; +#endif + +#if defined(__x86_64) || defined(__i686) + typedef uint64 uintptr; +#else + typedef uint32 uintptr; +#endif +#else + #error IA-PC only support +#endif + +#endif diff --git a/include/containers/CMakeLists.txt b/include/containers/CMakeLists.txt new file mode 100644 index 0000000..aab0387 --- /dev/null +++ b/include/containers/CMakeLists.txt @@ -0,0 +1,10 @@ +add_library(ContainersHeaders INTERFACE) + +target_sources(ContainersHeaders + PUBLIC + FILE_SET HEADERS + FILES + ${CMAKE_CURRENT_LIST_DIR}/Tuple.h +) + +target_link_libraries(Include PUBLIC ContainersHeaders) diff --git a/include/types.h b/include/types.h deleted file mode 100644 index 9f59063..0000000 --- a/include/types.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef TYPES_H -#define TYPES_H - -#define __IAPC - -#define NULL 0 - -#if defined(__IAPC) -typedef unsigned char uint8; -typedef unsigned short int uint16; -typedef unsigned long uint32; -typedef unsigned long long uint64; - -#if defined(__x86_64__) - typedef uint64 uword; -#else - typedef uint32 uword; -#endif - -#if defined(__x86_64) || defined(__i686) - typedef uint64 uintptr; -#else - typedef uint32 uintptr; -#endif -#else - #error IA-PC only support -#endif - -#endif diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt new file mode 100644 index 0000000..cd249d0 --- /dev/null +++ b/kernel/CMakeLists.txt @@ -0,0 +1,14 @@ +target_link_options(CXX_LINKER_REQUIREMENTS INTERFACE + LINKER:-T${CMAKE_CURRENT_LIST_DIR}/linker.ld +) + +add_library(Kernel INTERFACE) + +add_library(Main STATIC) +target_sources(Main PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/kernel.cpp +) +target_link_libraries(Main PUBLIC CXX_COMPILER_REQUIREMENTS Architecture Include) +target_link_libraries(Kernel INTERFACE Main) + +add_subdirectory(details) diff --git a/kernel/boot.s b/kernel/boot.s deleted file mode 100644 index 11175af..0000000 --- a/kernel/boot.s +++ /dev/null @@ -1,16 +0,0 @@ -section .text -extern main, _estack, _init -global _start:function -_start: - ; Stack setup - mov eax, _estack - ; Align stack to odd address - and eax, 0xFFFFFFFE - mov esp, eax - - call _init - call main - cli -.hang: hlt - jmp .hang -; .end: diff --git a/kernel/details/CMakeLists.txt b/kernel/details/CMakeLists.txt new file mode 100644 index 0000000..881d360 --- /dev/null +++ b/kernel/details/CMakeLists.txt @@ -0,0 +1,8 @@ +target_sources(CPUID + PUBLIC + FILE_SET HEADERS + BASE_DIRS + ${CMAKE_CURRENT_LIST_DIR} + FILES + ${CMAKE_CURRENT_LIST_DIR}/Platform.h +) diff --git a/kernel/kernel.c b/kernel/kernel.c deleted file mode 100644 index c280755..0000000 --- a/kernel/kernel.c +++ /dev/null @@ -1,19 +0,0 @@ - -#include "../arch/SecondaryInit.h" -#include "../arch/x86/ic/Pic.h" - - -void empty() {}; - -void main() -{ - SecondaryInitialization init; - - init.processorInitialize(); - init.devicesInitialize(); - - out::display << "Hello, World!\n"; - - asm("sti"); - while(true); -}; diff --git a/kernel/kernel.cpp b/kernel/kernel.cpp new file mode 100644 index 0000000..c280755 --- /dev/null +++ b/kernel/kernel.cpp @@ -0,0 +1,19 @@ + +#include "../arch/SecondaryInit.h" +#include "../arch/x86/ic/Pic.h" + + +void empty() {}; + +void main() +{ + SecondaryInitialization init; + + init.processorInitialize(); + init.devicesInitialize(); + + out::display << "Hello, World!\n"; + + asm("sti"); + while(true); +}; diff --git a/libgcc/CMakeLists.txt b/libgcc/CMakeLists.txt new file mode 100644 index 0000000..b5b2abc --- /dev/null +++ b/libgcc/CMakeLists.txt @@ -0,0 +1,25 @@ +macro(setup_crt_object_file object_file target_name) + execute_process( + COMMAND ${CMAKE_CXX_COMPILER} --print-file-name=${object_file} + OUTPUT_STRIP_TRAILING_WHITESPACE + OUTPUT_VARIABLE PATH + ERROR_VARIABLE ERROR_MESSAGE + RESULT_VARIABLE RESULT + ) + + if(RESULT) + message(FATAL_ERROR ${ERROR_MESSAGE}) + endif() + + add_library(${target_name} OBJECT IMPORTED GLOBAL) + set_target_properties(${target_name} PROPERTIES + IMPORTED_OBJECTS ${PATH} + ) + +endmacro() + +# TODO: set platform specific CRT objects +setup_crt_object_file(crtbegin.o CRTBEGIN) +setup_crt_object_file(crtend.o CRTEND) + +add_subdirectory(${ARCH}) diff --git a/libgcc/x86/CMakeLists.txt b/libgcc/x86/CMakeLists.txt new file mode 100644 index 0000000..2a1bec9 --- /dev/null +++ b/libgcc/x86/CMakeLists.txt @@ -0,0 +1,15 @@ +add_library(CRTI INTERFACE) +add_library(CRTIBUNDLE STATIC) +target_sources(CRTIBUNDLE PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/crti.nasm + $ +) +target_link_libraries(CRTI INTERFACE CRTIBUNDLE) + +add_library(CRTN INTERFACE) +add_library(CRTNBUNDLE STATIC) +target_sources(CRTNBUNDLE PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/crtn.nasm + $ +) +target_link_libraries(CRTN INTERFACE CRTNBUNDLE) diff --git a/libgcc/x86/crti.nasm b/libgcc/x86/crti.nasm new file mode 100644 index 0000000..57cb8c1 --- /dev/null +++ b/libgcc/x86/crti.nasm @@ -0,0 +1,5 @@ +section .init +global _init:function +_init: + push ebp + mov ebp, esp diff --git a/libgcc/x86/crti.s b/libgcc/x86/crti.s deleted file mode 100644 index 57cb8c1..0000000 --- a/libgcc/x86/crti.s +++ /dev/null @@ -1,5 +0,0 @@ -section .init -global _init:function -_init: - push ebp - mov ebp, esp diff --git a/libgcc/x86/crtn.nasm b/libgcc/x86/crtn.nasm new file mode 100644 index 0000000..537fa65 --- /dev/null +++ b/libgcc/x86/crtn.nasm @@ -0,0 +1,3 @@ +section .init + pop ebp + ret diff --git a/libgcc/x86/crtn.s b/libgcc/x86/crtn.s deleted file mode 100644 index 537fa65..0000000 --- a/libgcc/x86/crtn.s +++ /dev/null @@ -1,3 +0,0 @@ -section .init - pop ebp - ret diff --git a/makefile b/makefile deleted file mode 100644 index 79152f8..0000000 --- a/makefile +++ /dev/null @@ -1,72 +0,0 @@ -CC = i686-elf-g++ -LD = ld -ASM = nasm -# test -O2 flag -CFLAGS = -std=c++20 -c -m32 -Wall -ffreestanding -nostdinc -nostdlib -fno-rtti -mgeneral-regs-only -AFLAGS = -f elf32 -LDFLAGS = -m elf_i386 $(patsubst %,-L%,$(subst :, ,$(LIBS_PATH))) -lgcc -QEMUFLAGS = - -CFILES = $(shell find ./ -type f \( -name \*.cpp -o -name \*.c \)) -AFILES = $(shell find ./ -type f \( -iname \*.s -o -name \*.asm \)) -LDFILE = ./kernel/linker.ld -SOURCE_FILES = $(CFILES) $(AFILES) -OBJ_FILES = $(addprefix $(BDIR)/, $(addsuffix .o, $(basename $(SOURCE_FILES)))) -INCLUDE = ./include -# Bad practice! -ARCHDIR = arch/x86 - -ifdef DEBUG - CFLAGS := -g3 $(CFLAGS) - AFLAGS := -g $(AFLAGS) - QEMUFLAGS := -s -S $(QEMUFLAGS) -endif - -OS_BINARY = $(BDIR)/kernel.bin -BDIR = ./build - -BOOTOBJ = $(filter %boot.o, $(OBJ_FILES)) -CRTIOBJ = $(filter %crti.o, $(OBJ_FILES)) -CRTNOBJ = $(filter %crtn.o, $(OBJ_FILES)) -OBJ_REORDERING = $(filter-out $(BOOTOBJ), $(filter-out $(CRTIOBJ), $(filter-out $(CRTNOBJ), $(OBJ_FILES)))) - - -.PHONY: install $(SOURCE_FILES) - -build: startbuild $(SOURCE_FILES) - @echo "Bootfile: " $(BOOTOBJ) $(CRTIOBJ) $(CRTNOBJ) - @echo $(OBJ_FILES) - echo $(AFLAGS) - echo $(CFLAGS) - @echo "Link object files..." - $(LD) -o $(OS_BINARY) -L$(ARCHDIR) -T$(LDFILE) $(BOOTOBJ) $(CRTIOBJ) $(LIBS_PATH)crtbegin.o $(OBJ_REORDERING) $(LIBS_PATH)crtend.o $(CRTNOBJ) $(LDFLAGS) - @echo "Project was built" - -$(CFILES): - mkdir -p $(BDIR)/$(@D) - $(CC) $(CFLAGS) -I $(INCLUDE) -o $(BDIR)/$(addsuffix .o, $(basename $@)) $@ - -$(AFILES): - mkdir -p $(BDIR)/$(@D) - $(ASM) $(AFLAGS) -o $(BDIR)/$(addsuffix .o, $(basename $@)) $@ - -startbuild: - @echo $(OBJ_FILES) - rm -rf $(BDIR) - mkdir $(BDIR) - @echo "Compile source files..." - -install: - @echo "Creating an iso image of OS" - mkdir -p ./iso/os/ - cp ./$(BDIR)/kernel.bin ./iso/os/ - grub-mkrescue -o ./$(BDIR)/kernel.iso ./iso - -clean: - rm -rf $(BDIR) - rm -rf ./iso/os/kernel.bin - -run: - qemu-system-i386 $(QEMUFLAGS) -cdrom ./$(BDIR)/kernel.iso - -all: clean build install run -- cgit v1.2.3