commit b463527025cd8f4efb61a8f440d760222fa47bb6 Author: CHatingPython Date: Fri Oct 2 06:09:40 2026 +0200 chore: init project diff --git a/.clang-format b/.clang-format new file mode 100755 index 0000000..b64883b --- /dev/null +++ b/.clang-format @@ -0,0 +1,67 @@ +BasedOnStyle: LLVM + +IndentWidth: 4 +TabWidth: 4 +UseTab: Never +ContinuationIndentWidth: 4 + +ColumnLimit: 120 + +BreakBeforeBraces: Attach +BraceWrapping: + BeforeCatch: true + BeforeElse: false + BeforeWhile: false + BeforeLambdaBody: false + +AllowShortFunctionsOnASingleLine: Inline +AllowShortBlocksOnASingleLine: Empty +AllowShortCaseLabelsOnASingleLine: true +AllowShortEnumsOnASingleLine: false +AllowShortIfStatementsOnASingleLine: true +AllowShortLoopsOnASingleLine: false +AllowShortCompoundRequirementOnASingleLine: true + +NamespaceIndentation: None +AccessModifierOffset: -4 +EmptyLineBeforeAccessModifier: Never + +SpaceBeforeParens: ControlStatementsExceptControlMacros +SpaceBeforeInheritanceColon: true +SpacesBeforeTrailingComments: 1 + +AlignTrailingComments: true + +AlignConsecutiveAssignments: + Enabled: true + AlignCompound: true + PadOperators: false + +AlignConsecutiveDeclarations: Consecutive +AlignAfterOpenBracket: DontAlign + +ConstructorInitializerIndentWidth: 2 +PackConstructorInitializers: NextLineOnly +BreakConstructorInitializers: BeforeColon + +BinPackParameters: false +BinPackArguments: false +AllowAllArgumentsOnNextLine: false + +AlwaysBreakTemplateDeclarations: Yes +BreakAfterReturnType: None + +Cpp11BracedListStyle: false +BreakArrays: false + +IncludeBlocks: Regroup +SortIncludes: true + +ReflowComments: Always +KeepEmptyLinesAtTheStartOfBlocks: true + +DerivePointerAlignment: false +PointerAlignment: Left +AllowAllParametersOfDeclarationOnNextLine: false + +PenaltyReturnTypeOnItsOwnLine: 1000 \ No newline at end of file diff --git a/.clang-tidy b/.clang-tidy new file mode 100755 index 0000000..4303f61 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,105 @@ +--- +Checks: > + -*, + + bugprone-*, + performance-*, + readability-*, + portability-*, + cppcoreguidelines-*, + + -bugprone-assignment-in-if-condition, + -bugprone-easily-swappable-parameters, + -bugprone-multi-level-implicit-pointer-conversion, + -bugprone-unintended-char-ostream-output, + -readability-function-cognitive-complexity, + -readability-magic-numbers, + -readability-redundant-access-specifiers, + -readability-use-anyofallof, + -readability-named-parameter, + -readability-convert-member-functions-to-static, + -performance-enum-size, + -cppcoreguidelines-avoid-magic-numbers, + -cppcoreguidelines-pro-bounds-pointer-arithmetic, + -cppcoreguidelines-pro-type-union-access, + -cppcoreguidelines-pro-type-reinterpret-cast, + -cppcoreguidelines-avoid-do-while, + -cppcoreguidelines-avoid-non-const-global-variables, + -cppcoreguidelines-avoid-c-arrays, + -cppcoreguidelines-pro-bounds-constant-array-index, + -cppcoreguidelines-macro-usage, + -cppcoreguidelines-owning-memory, + -cppcoreguidelines-non-private-member-variables-in-classes, + -cppcoreguidelines-pro-bounds-avoid-unchecked-container-access, + -cppcoreguidelines-pro-bounds-array-to-pointer-decay, + -cppcoreguidelines-use-enum-class, + -bugprone-forward-declaration-namespace, + -bugprone-tagged-union-member-count, + -bugprone-unchecked-optional-access + +WarningsAsErrors: "*" + +HeaderFilterRegex: ".*" + +CheckOptions: + - key: readability-braces-around-statements.ShortStatementLines + value: 3 + + - key: readability-identifier-length.MinimumParameterNameLength + value: 2 + + - key: readability-identifier-length.MinimumVariableNameLength + value: 2 + + - key: cppcoreguidelines-rvalue-reference-param-not-moved.AllowPartialMove + value: true + + # Namespaces + - key: readability-identifier-naming.NamespaceCase + value: lower_case + + # Types + - key: readability-identifier-naming.StructCase + value: lower_case + - key: readability-identifier-naming.ClassCase + value: lower_case + - key: readability-identifier-naming.EnumCase + value: lower_case + # - key: readability-identifier-naming.AbstractClassPrefix + # value: I + + # Functions + - key: readability-identifier-naming.FunctionCase + value: lower_case + + # Variables + - key: readability-identifier-naming.VariableCase + value: camelBack + + # Public members + - key: readability-identifier-naming.PublicMemberCase + value: camelBack + + # Static members + - key: readability-identifier-naming.StaticVariableCase + value: camelBack + - key: readability-identifier-naming.StaticVariablePrefix + value: s_ + - key: readability-identifier-naming.ClassMemberCase + value: camelBack + - key: readability-identifier-naming.ClassMemberPrefix + value: s_ + + # Static constexpr + - key: readability-identifier-naming.StaticConstexprVariableCase + value: aNy_CasE + + # Constants + - key: readability-identifier-naming.ConstantCase + value: aNy_CasE + - key: readability-identifier-naming.StaticConstantCase + value: aNy_CasE + - key: readability-identifier-naming.EnumConstantCase + value: CamelCase + - key: readability-identifier-naming.ScopedEnumConstantCase + value: CamelCase diff --git a/.clangd b/.clangd new file mode 100755 index 0000000..500c8c2 --- /dev/null +++ b/.clangd @@ -0,0 +1,25 @@ +CompileFlags: + Add: [-std=c++17, -Wall, -Werror, -Wpedantic] + +InlayHints: + BlockEnd: true + Designators: false + Enabled: true + ParameterNames: true + DeducedTypes: true + TypeNameLimit: 24 + +Completion: + HeaderInsertion: IWYU + +Hover: + ShowAKA: true + +If: + PathMatch: (./.*\.cpp|./.*\.hpp) +Diagnostics: + UnusedIncludes: Strict + MissingIncludes: Strict + +Documentation: + CommentFormat: Doxygen \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3558c56 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin/ +build/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..4877bed --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "deps/grub"] + path = deps/grub + url = https://gitlab.freedesktop.org/gnu-grub/grub.git diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2f85a91 --- /dev/null +++ b/Makefile @@ -0,0 +1,79 @@ +TARGET := towos.elf +ISO := towos.iso + +CC := x86_64-elf-gcc +CXX := x86_64-elf-g++ +LD := x86_64-elf-ld +AS := x86_64-elf-as + +CXXFLAGS := \ + -std=c++17 \ + -ffreestanding \ + -fno-exceptions \ + -fno-rtti \ + -fno-stack-protector \ + -fno-stack-check \ + -fno-pic \ + -fno-pie \ + -fno-use-cxa-atexit \ + -mno-red-zone \ + -Wall \ + -Wextra \ + -Wpedantic \ + -O2 + +ASFLAGS := \ + --64 + +LDFLAGS := \ + -T linker.ld \ + -nostdlib \ + -no-pie \ + --build-id=none + +SRC_DIR := kernel +BIN_DIR := bin +BUILD_DIR := build + +CXXSOURCES := $(shell find $(SRC_DIR) -type f -name '*.cpp') +CXXOBJECTS := $(patsubst $(SRC_DIR)/%.cpp,$(BIN_DIR)/%.o,$(CXXSOURCES)) +SSOURCES := $(shell find $(SRC_DIR) -type f -name '*.S') +SOBJECTS := $(patsubst $(SRC_DIR)/%.S,$(BIN_DIR)/%.o,$(SSOURCES)) + +OBJECTS := $(CXXOBJECTS) $(SOBJECTS) + +.PHONY: all run debug check clean +all: $(BUILD_DIR)/$(ISO) + +$(BIN_DIR)/%.o: $(SRC_DIR)/%.cpp + @mkdir -p $(dir $@) + $(CXX) -x c++ $(CXXFLAGS) -c $< -o $@ + +$(BIN_DIR)/%.o: $(SRC_DIR)/%.S + @mkdir -p $(dir $@) + $(AS) $(ASFLAGS) -c $< -o $@ + +$(BUILD_DIR)/$(TARGET): $(OBJECTS) linker.ld + @mkdir -p $(dir $@) + $(LD) $(LDFLAGS) -o $@ $(OBJECTS) + +$(BUILD_DIR)/$(ISO): $(BUILD_DIR)/$(TARGET) grub.cfg + rm -rf $(BUILD_DIR)/isofiles + mkdir -p $(BUILD_DIR)/isofiles/boot/grub + + cp $(BUILD_DIR)/$(TARGET) $(BUILD_DIR)/isofiles/boot/kernel.elf + cp grub.cfg $(BUILD_DIR)/isofiles/boot/grub/grub.cfg + grub-mkrescue -o $@ $(BUILD_DIR)/isofiles + +run: $(BUILD_DIR)/$(ISO) + qemu-system-x86_64 -m 256M -cdrom $< + +debug: $(BUILD_DIR)/$(ISO) + qemu-system-x86_64 -m 256M -cdrom $< -s -S + +check: $(BUILD_DIR)/$(TARGET) + grub-file --is-x86-multiboot2 $(BUILD_DIR)/$(TARGET) + @echo "Multiboot2 kernel OK" + +clean: + @rm -rf $(BUILD_DIR) $(BIN_DIR) diff --git a/deps/grub b/deps/grub new file mode 160000 index 0000000..023dab0 --- /dev/null +++ b/deps/grub @@ -0,0 +1 @@ +Subproject commit 023dab0b970c1affe768396e3f7b3770c4275d7c diff --git a/grub.cfg b/grub.cfg new file mode 100644 index 0000000..1c73214 --- /dev/null +++ b/grub.cfg @@ -0,0 +1,7 @@ +set timeout=0 +set default=0 + +menuentry "TowOS" { + multiboot2 /boot/kernel.elf + boot +} diff --git a/kernel/boot.S b/kernel/boot.S new file mode 100644 index 0000000..609eb7d --- /dev/null +++ b/kernel/boot.S @@ -0,0 +1,177 @@ +.section .multiboot +.align 8 + +multiboot_header: + .long 0xE85250D6 + .long 0 + .long multiboot_header_end - multiboot_header + .long -(0xE85250D6 + 0 + (multiboot_header_end - multiboot_header)) + + .short 0 + .short 0 + .long 8 +multiboot_header_end: + +.section .text +.code32 + +.global _start +.type _start, @function + +.extern kmain + +_start: + cli + cld + + movl %eax, multiboot_magic + movl %ebx, multiboot_info + + movl $stack_top, %esp + + movl $pdpt_table, %eax + orl $0x3, %eax + movl %eax, pml4_table + + movl $pd_table, %eax + orl $0x3, %eax + movl %eax, pdpt_table + + xorl %ecx, %ecx + movl $pd_table, %edi + +.fill_pd: + movl %ecx, %eax + + shll $21, %eax + + orl $0x83, %eax + + movl %eax, (%edi) + + addl $8, %edi + incl %ecx + + cmpl $512, %ecx + jne .fill_pd + + movl $pml4_table, %eax + movl %eax, %cr3 + + movl %cr4, %eax + orl $0x20, %eax + movl %eax, %cr4 + + movl $0xC0000080, %ecx + rdmsr + + orl $0x100, %eax + + wrmsr + + movl %cr0, %eax + orl $0x80000001, %eax + movl %eax, %cr0 + + lgdt gdt_descriptor + + ljmp $0x08, $long_mode_entry + + +.code64 + +long_mode_entry: + movw $0x10, %ax + + movw %ax, %ds + movw %ax, %es + movw %ax, %ss + movw %ax, %fs + movw %ax, %gs + + movq $stack_top, %rsp + + andq $-16, %rsp + + xorq %rbp, %rbp + + call run_global_constructors + + call kmain +.hang: + cli + hlt + jmp .hang + + +.extern __init_array_start +.extern __init_array_end + +run_global_constructors: + pushq %rbp + movq %rsp, %rbp + + movq $__init_array_start, %rax + +.constructor_loop: + cmpq $__init_array_end, %rax + jae .constructors_done + + movq (%rax), %rdx + call *%rdx + + addq $8, %rax + jmp .constructor_loop + +.constructors_done: + popq %rbp + ret + + +.align 8 + +gdt: + .quad 0x0000000000000000 + .quad 0x00AF9A000000FFFF + .quad 0x00AF92000000FFFF +gdt_end: + +gdt_descriptor: + .word gdt_end - gdt - 1 + .long gdt + + +.section .bss + +.align 8 + +.global multiboot_magic +multiboot_magic: + .quad 0 + +.global multiboot_info +multiboot_info: + .quad 0 + + +.align 4096 + +pml4_table: + .skip 4096 + +.align 4096 + +pdpt_table: + .skip 4096 + +.align 4096 + +pd_table: + .skip 4096 + + +.align 16 + +stack_bottom: + .skip 16384 +stack_top: diff --git a/kernel/kmain.cpp b/kernel/kmain.cpp new file mode 100644 index 0000000..e9980a6 --- /dev/null +++ b/kernel/kmain.cpp @@ -0,0 +1,16 @@ +using u8 = unsigned char; +using u16 = unsigned short; +using u32 = unsigned int; +using u64 = unsigned long long; + +static volatile u16* const s_vga = reinterpret_cast(0xB8000); + +extern "C" void kmain() { + const char* msg = "Zdravstvuyte, tovorisch!"; + + for (int i = 0; msg[i] != '\0'; i++) + s_vga[i] = 0x0700 | msg[i]; + + for (;;) + __asm__ volatile("hlt"); +} diff --git a/linker.ld b/linker.ld new file mode 100644 index 0000000..823e556 --- /dev/null +++ b/linker.ld @@ -0,0 +1,49 @@ +ENTRY(_start) + +SECTIONS +{ + . = 1M; + + .multiboot ALIGN(8) : + { + KEEP(*(.multiboot)) + } + + .text ALIGN(16) : + { + *(.text .text.*) + } + + .rodata ALIGN(16) : + { + *(.rodata .rodata.*) + } + + .data ALIGN(16) : + { + *(.data .data.*) + } + + .init_array ALIGN(8) : + { + __init_array_start = .; + + KEEP(*(SORT(.init_array.*))) + KEEP(*(.init_array)) + + __init_array_end = .; + } + + .bss ALIGN(16) : + { + *(COMMON) + *(.bss .bss.*) + } + + /DISCARD/ : + { + *(.comment) + *(.eh_frame) + *(.note.GNU-stack) + } +}