From 6e2611ab74ed096ee4ac8ae4c59aa6a5dbe22023 Mon Sep 17 00:00:00 2001 From: FyloZ Date: Sat, 21 Feb 2026 20:19:36 -0500 Subject: [PATCH] Setup environment --- .gitignore | 23 ++ .idea/.gitignore | 10 + .idea/.name | 1 + .idea/editor.xml | 345 ++++++++++++++++++++++++++++ .idea/misc.xml | 8 + .idea/modules.xml | 8 + .idea/real_os.iml | 2 + .idea/vcs.xml | 6 + CMakeLists.txt | 7 + src/kernel/CMakeLists.txt | 38 +++ src/kernel/kernel.c | 46 ++++ src/kernel/riscv64/entry.s | 37 +++ src/kernel/riscv64/flags.cmake | 2 + src/kernel/riscv64/qemu/flags.cmake | 0 src/kernel/riscv64/qemu/layout.ld | 26 +++ src/kernel/riscv64/qemu/syscon.c | 13 ++ src/kernel/syscon.h | 7 + src/kernel/uart.c | 3 + 18 files changed, 582 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/.name create mode 100644 .idea/editor.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/real_os.iml create mode 100644 .idea/vcs.xml create mode 100644 CMakeLists.txt create mode 100644 src/kernel/CMakeLists.txt create mode 100644 src/kernel/kernel.c create mode 100644 src/kernel/riscv64/entry.s create mode 100644 src/kernel/riscv64/flags.cmake create mode 100644 src/kernel/riscv64/qemu/flags.cmake create mode 100644 src/kernel/riscv64/qemu/layout.ld create mode 100644 src/kernel/riscv64/qemu/syscon.c create mode 100644 src/kernel/syscon.h create mode 100644 src/kernel/uart.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cb063c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,23 @@ +# Created by https://www.toptal.com/developers/gitignore/api/cmake +# Edit at https://www.toptal.com/developers/gitignore?templates=cmake + +### CMake ### +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps + +### CMake Patch ### +CMakeUserPresets.json + +# External projects +*-prefix/ + +# End of https://www.toptal.com/developers/gitignore/api/cmake diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..ab1f416 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..a82b63c --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +os \ No newline at end of file diff --git a/.idea/editor.xml b/.idea/editor.xml new file mode 100644 index 0000000..8d0e15e --- /dev/null +++ b/.idea/editor.xml @@ -0,0 +1,345 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..e479d63 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,8 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..bd07fc5 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/real_os.iml b/.idea/real_os.iml new file mode 100644 index 0000000..4c94235 --- /dev/null +++ b/.idea/real_os.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..d07caf0 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 4.1.0) +project(os VERSION 0.0.1) + +enable_language(C ASM) + +# Include the sub-projects +add_subdirectory(src/kernel) \ No newline at end of file diff --git a/src/kernel/CMakeLists.txt b/src/kernel/CMakeLists.txt new file mode 100644 index 0000000..ac99f2f --- /dev/null +++ b/src/kernel/CMakeLists.txt @@ -0,0 +1,38 @@ +# Function from osdev wiki: https://wiki.osdev.org/CMake_Build_System +FUNCTION(LOAD_PROFILE ISA PLATFORM) + # Platform specific sources + FILE(GLOB ISA_SRCS "${ISA}/*.c" "${ISA}/*.h" "${ISA}/*.s") + FILE(GLOB PLATFORM_SRCS "${ISA}/${PLATFORM}/*.c" "${ISA}/${PLATFORM}/*.s") + + # Load flags, not sure what these are for yet + INCLUDE("${ISA}/flags.cmake") + INCLUDE("${ISA}/${PLATFORM}/flags.cmake") + + # Export the variables + SET(ISA_SRCS ${ISA_SRCS} PARENT_SCOPE) + SET(PLATFORM_SRCS ${PLATFORM_SRCS} PARENT_SCOPE) + SET(PLATFORM_LAYOUT "${CMAKE_SOURCE_DIR}/src/kernel/${ISA}/${PLATFORM}/layout.ld" PARENT_SCOPE) + SET(ISA_C_FLAGS ${ISA_C_FLAGS} PARENT_SCOPE) + SET(PLATFORM_C_FLAGS ${PLATFORM_C_FLAGS} PARENT_SCOPE) + SET(ISA_ASM_FLAGS ${ISA_ASM_FLAGS} PARENT_SCOPE) + SET(PLATFORM_ASM_FLAGS ${PLATFORM_ASM_FLAGS} PARENT_SCOPE) + SET(ISA_LINKER_FLAGS ${ISA_LINKER_FLAGS} PARENT_SCOPE) + SET(PLATFORM_LINKER_FLAGS ${PLATFORM_LINKER_FLAGS} PARENT_SCOPE) +ENDFUNCTION(LOAD_PROFILE) + +FILE(GLOB GENERIC_SRCS ./*.c) + +# Set the current profile +LOAD_PROFILE("riscv64" "qemu") + +ADD_EXECUTABLE(kernel ${PLATFORM_SRCS} ${ISA_SRCS} ${GENERIC_SRCS}) + +SET(CMAKE_ASM_COMPILE_OBJECT " -c ${ISA_ASM_FLAGS} ${PLATFORM_ASM_FLAGS} -o ") +SET(CMAKE_C_FLAGS "${ISA_C_FLAGS} ${PLATFORM_C_FLAGS}") +SET_TARGET_PROPERTIES(kernel PROPERTIES LINK_FLAGS "-T ${PLATFORM_LAYOUT} -N ${ISA_LINKER_FLAGS} ${PLATFORM_LINKER_FLAGS}") + +# Set the name of the executable to 'kernel.elf' +SET_TARGET_PROPERTIES(kernel PROPERTIES OUTPUT_NAME kernel.elf) + +include(CMakePrintHelpers) +CMAKE_PRINT_VARIABLES(PLATFORM_SRCS) \ No newline at end of file diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c new file mode 100644 index 0000000..4088254 --- /dev/null +++ b/src/kernel/kernel.c @@ -0,0 +1,46 @@ +/* + * ===================================================================================== + * + * Filename: kernel.c + * + * Description: Kernel entry point + * + * Version: 1.0 + * Created: 2026-02-16 10:13:30 PM + * Revision: none + * Compiler: gcc + * + * Author: William Nolin, + * + * ===================================================================================== + */ +#include +#include + +#include "syscon.h" + +unsigned char *uart = (unsigned char *)0x10000000; +void putchar(char c) { + *uart = c; + return; +} + +void print(const char * str) { + while(*str != '\0') { + putchar(*str); + str++; + } + return; +} + +void kmain(void) { + power_off(); + + // print("Hello world!\r\n"); + // while(1) { + // // Read input from the UART + // putchar(*uart); + // } + // return; +} + diff --git a/src/kernel/riscv64/entry.s b/src/kernel/riscv64/entry.s new file mode 100644 index 0000000..e090b4b --- /dev/null +++ b/src/kernel/riscv64/entry.s @@ -0,0 +1,37 @@ +.section .init + +.option norvc + +.type start, @function +.global start +start: + .cfi_startproc + +.option push +.option norelax + la gp, global_pointer +.option pop + + /* Reset satp */ + csrw satp, zero + + /* Setup stack */ + la sp, stack_top + + /* Clear the BSS section */ + la t5, bss_start + la t6, bss_end +bss_clear: + sd zero, (t5) + addi t5, t5, 8 + bltu t5, t6, bss_clear + + la t0, kmain + csrw mepc, t0 + + /* Jump to kernel! */ + tail kmain + + .cfi_endproc + +.end diff --git a/src/kernel/riscv64/flags.cmake b/src/kernel/riscv64/flags.cmake new file mode 100644 index 0000000..dcf9246 --- /dev/null +++ b/src/kernel/riscv64/flags.cmake @@ -0,0 +1,2 @@ +set(ISA_C_FLAGS "-Wall -Wextra -mcmodel=medany -ffreestanding") +set(ISA_LINKER_FLAGS "-lgcc -nostdlib") \ No newline at end of file diff --git a/src/kernel/riscv64/qemu/flags.cmake b/src/kernel/riscv64/qemu/flags.cmake new file mode 100644 index 0000000..e69de29 diff --git a/src/kernel/riscv64/qemu/layout.ld b/src/kernel/riscv64/qemu/layout.ld new file mode 100644 index 0000000..2f42aa2 --- /dev/null +++ b/src/kernel/riscv64/qemu/layout.ld @@ -0,0 +1,26 @@ +ENTRY(start); + +. = 0x80000000; + +SECTIONS { + /* Include entry point at start of binary */ + .text : ALIGN(4K) { + *(.init); + *(.text); + } + .bss : ALIGN(4K) { + PROVIDE(bss_start = .); + *(.bss); + . += 4096; + PROVIDE(stack_top = .); + . += 4096; + PROVIDE(global_pointer = .); + PROVIDE(bss_end = .); + } + .rodata : ALIGN(4K) { + *(.rodata); + } + .data : ALIGN(4K) { + *(.data); + } +} diff --git a/src/kernel/riscv64/qemu/syscon.c b/src/kernel/riscv64/qemu/syscon.c new file mode 100644 index 0000000..2515271 --- /dev/null +++ b/src/kernel/riscv64/qemu/syscon.c @@ -0,0 +1,13 @@ +#include +#include "../../syscon.h" + +// QEMU exposes a syscon-compatible device at address 0x1000000 +#define SYSCON_ADDR 0x100000 + +void power_off() { + *(uint32_t *) SYSCON_ADDR = 0x5555; +} + +void reboot() { + *(uint32_t *) SYSCON_ADDR = 0x7777; +} diff --git a/src/kernel/syscon.h b/src/kernel/syscon.h new file mode 100644 index 0000000..3680146 --- /dev/null +++ b/src/kernel/syscon.h @@ -0,0 +1,7 @@ +#ifndef SYSCON_H +#define SYSCON_H + +void power_off(); +void reboot(); + +#endif \ No newline at end of file diff --git a/src/kernel/uart.c b/src/kernel/uart.c new file mode 100644 index 0000000..21d8258 --- /dev/null +++ b/src/kernel/uart.c @@ -0,0 +1,3 @@ +// +// Created by william on 2026-02-21. +// \ No newline at end of file