diff --git a/.github/workflows/obos-build.yml b/.github/workflows/obos-build.yml deleted file mode 100644 index 843cab79..00000000 --- a/.github/workflows/obos-build.yml +++ /dev/null @@ -1,72 +0,0 @@ -# This starter workflow is for a CMake project running on multiple platforms. There is a different starter workflow if you just want a single platform. -# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-single-platform.yml -name: Build OBOS. - -on: - push: - branches: [ "master" ] - pull_request: - branches: [ "master" ] - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v4 - - - name: Set reusable strings - # Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file. - id: strings - shell: bash - run: | - echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT" - - - name: Get libgcc binaries - run: | - mkdir libgcc-binaries - cd libgcc-binaries - curl -LO https://github.com/osdev0/libgcc-binaries/releases/download/2025-08-21/libgcc-x86_64-no-red-zone.a - cd .. - - - name: Install dependencies. - run: | - sudo apt-get update - sudo apt-get install xorriso nasm - wget https://apt.llvm.org/llvm.sh - chmod +x llvm.sh - sudo ./llvm.sh 21 - - - name: Make empty initrd.tar file - run: touch ${{ github.workspace }}/config/initrd.tar - - - name: Configure CMake - run: > - cmake -B ${{ steps.strings.outputs.build-output-dir }}/x86_64 - -DCMAKE_BUILD_TYPE=RelWithDebInfo - -DOBOS_USE_CLANG=ON - -DOBOS_ENABLE_UBSAN=OFF - -DOBOS_CLANG_SUFFIX="-21" - -DLIBGCC=`realpath libgcc-binaries/libgcc-x86_64-no-red-zone.a` - --toolchain ${{ github.workspace }}/src/build/x86_64/toolchain.cmake - -S ${{ github.workspace }} - - - name: Build - working-directory: ${{ github.workspace }}/scripts - run: | - chmod +x ${{ github.workspace }}/dependencies/hyper/hyper_install-linux-x86_64 - cmake --build ${{ steps.strings.outputs.build-output-dir }}/x86_64 - ./generate_initrd-x86_64.sh - cmake --build ${{ steps.strings.outputs.build-output-dir }}/x86_64 - - name: Archive code coverage results - uses: actions/upload-artifact@v4 - with: - name: obos-bin - path: | - ${{ github.workspace }}/out/obos.iso - ${{ github.workspace }}/out/oboskrnl - ${{ github.workspace }}/config/initrd.tar -# TODO: Tests -# - name: Test -# working-directory: ${{ steps.strings.outputs.build-output-dir }} -# run: diff --git a/.gitignore b/.gitignore index 09bce2cb..ad013d6a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,21 +1,19 @@ -/.vs -/.vscode +# Misc +/.vs/* +/qemu_log.txt +/.vscode/ +/gdb_config + +# Dependencies +/dependencies/include +/dependencies/needs_download.cmake +/dependencies/limine +/dependencies/uacpi +/dependencies/ZyDis + +# Build-related +/isodir/ /CMakePresets.json /out -/build -/dependencies/hyper -/dependencies/UltraProtocol -/dependencies/uACPI -/dependencies/uHDA -/dependencies/flanterm/ -/dependencies/freebsd-e1000 -/dependencies/limine -/dependencies/include/ -/dependencies/needs_download.cmake -/qemu_log.txt -/tar -/src/oboskrnl/inc/* -/.cache -/.clangd -/config/initrd.tar -/.gdbinit \ No newline at end of file +/CMakeFiles +*nppBackup* \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 29511733..02fd6225 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,316 +1,71 @@ -# CMakeLists.txt -# -# Copyright (c) 2024-2025 Omar Berrow - -cmake_minimum_required(VERSION 3.10) - -# Boolean options. - -option (OBOS_UP "Make a Uniprocessor-Build of obos.") -option (OBOS_ENABLE_UBSAN "Enables UBSan.") -option (OBOS_ENABLE_KASAN "Enables KAsan.") -option (OBOS_ENABLE_FANALYZER "(Developers only) Enables the compiler's static analyzer") -option (OBOS_ENABLE_WERROR "(Developers only) Enables the -Werror option") -option (OBOS_USE_CLANG "Use clang instead of GCC to build.") -option (OBOS_NVS_SAVE_S3 "Enable compile-time support for saving NVS before S3" ON) -option (OBOS_ENABLE_LOCK_PROFILING "(Experimental) Enable profiling for locks.") -option (OBOS_ENABLE_PROFILER "(Experimental) Enable profiling.") -option (OBOS_LAZY_IRQL "Enables lazy IRQL. Recommended if the main target of the kernel is a hypervisor, where writing to cr8 can be slow.") -option (OBOS_USE_OBOS_GCC "Uses arch-obos-gcc instead of obos-elf-gcc. Ignored if OBOS_USE_CLANG is true.") -option (OBOS_COMPILE_USER_UTILS "Compiles user examples" ${OBOS_USE_OBOS_GCC}) -option (OBOS_ONLY_COMPILE_USER_UTILS "Only compile user utilities" OFF) -option (OBOS_USE_LIMINE "Use Limine instead of Hyper on supported architectures" OFF) -option (OBOS_ENABLE_UHDA "Whether to include uHDA" ON) -option (OBOS_DEBUG_FREE_SIZE "Whether to check for mismatching allocation and free sizes in the allocator" OFF) - -set (OBOS_LAZY_IRQL OFF) - -if (NOT OBOS_USE_OBOS_GCC AND OBOS_COMPILE_USER_UTILS) - message(WARNING "OBOS_COMPILE_USER_UTILS must be OFF if OBOS_USE_OBOS_GCC is OFF") - set(OBOS_COMPILE_USER_UTILS OFF) -endif() - -if (OBOS_USE_CLANG AND OBOS_COMPILE_USER_UTILS) - message(WARNING "OBOS_COMPILE_USER_UTILS must be OFF if OBOS_USE_CLANG is ON") - set(OBOS_COMPILE_USER_UTILS OFF) -endif() - -# String options - -# NOTE: Unless we add FORCE to the end, CMake shouldn't override user values. -set (OUTPUT_DIR ${CMAKE_SOURCE_DIR}/out CACHE STRING "The output directory.") -set (OBOS_PAGE_REPLACEMENT_ALGORITHM "Aging" CACHE STRING "(Kernel developers only) The page replacement algorithm used by the MM.") -set (OBOS_DEV_PREFIX "/dev" CACHE STRING "Directory where the kernel places devices.") -set (OBOS_PERM_PREFIX "/sys/perm" CACHE STRING "Directory where the kernel checks for capabilities") -set (OBOS_CLANG_SUFFIX "" CACHE STRING "Some Linux distributions add a suffix to various clang and LLVM commands, such as the version used. Specify that suffix, if needed") - -file (TO_CMAKE_PATH "${OUTPUT_DIR}" OUTPUT_DIR) -file (TO_NATIVE_PATH "${OUTPUT_DIR}" OUTPUT_DIR) - -set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_DIR}) - -# set(CMAKE_CXX_FLAGS_RELEASE "-O3" CACHE STRING "Flags used by the CXX compiler during RELEASE builds.") -# set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O3 -g" CACHE STRING "Flags used by the CXX compiler during RELWITHDEBINFO builds.") -# set(CMAKE_CXX_FLAGS_MINSIZEREL "-Os" CACHE STRING "Flags used by the CXX compiler during MINSIZEREL builds.") - -project( - OBOS - LANGUAGES C CXX ASM - DESCRIPTION "Hybrid Kernel with driver loading." - HOMEPAGE_URL https://github.com/OBOS-dev/obos - ) - -set (LIBGCC "${LIBGCC_TARGET}" CACHE STRING "A path to a libgcc binary for the target, if one does not exist yet") - -if (OBOS_ARCHITECTURE STREQUAL "x86_64") - enable_language(ASM_NASM) -else() - enable_language(ASM-ATT) -endif() - -if (OBOS_USE_CLANG) - add_compile_options($<$:-Wno-error=unknown-pragmas> - $<$:-Wno-unknown-pragmas> - $<$:-Wno-error=unknown-attributes> - $<$:-Wno-unknown-warning-option> - $<$:-Wno-error=c23-extensions>) - if (OBOS_ENABLE_KASAN) - set (OBOS_ENABLE_KASAN OFF) - message (WARNING "OBOS does not support Clang's ASAN implementation. If you really care about this, make a PR, as I don't.") - endif() -endif() - -# Download dependencies. -# NOTE: As dependencies are added, please add them to the .gitignore file. -# If a dependency cmake file is included, and OBOS_REFRESH_DEPENDENCIES is false, the cmake file shall not download or make any file operations, and only set any cache variables related -# to the dependency (eg. limine_install, zydis_c, uacpi_cmake_file, etc.) -if (EXISTS ${CMAKE_SOURCE_DIR}/dependencies/needs_download.cmake) - include (dependencies/needs_download.cmake) -else() - set (OBOS_REFRESH_DEPENDENCIES true) -endif() -set (OBOSKRNL_EXTERNAL_INCLUDES "${CMAKE_SOURCE_DIR}/dependencies/include") -if (OBOS_REFRESH_DEPENDENCIES) - file(MAKE_DIRECTORY "dependencies/include") - if (OBOS_ARCHITECTURE STREQUAL "x86_64") - message("Fetching Hyper.") - include("dependencies/hyper.cmake") - endif() - if (OBOS_USE_LIMINE) - message("Fetching Limine.") - include("dependencies/limine.cmake") - endif() - message("Fetching uACPI.") - include ("dependencies/uacpi.cmake") - message("Fetching flanterm.") - include ("dependencies/flanterm.cmake") - if (OBOS_ENABLE_UHDA) - message("Fetching uHDA.") - include ("dependencies/uhda.cmake") - endif() - message("Fetching e1000 driver.") - include ("dependencies/freebsd-e1000.cmake") - - file (WRITE "${CMAKE_SOURCE_DIR}/dependencies/needs_download.cmake" - "# This file is auto-generated.\n# Set this variable to true or delete this file to refresh all dependencies.\nset (OBOS_REFRESH_DEPENDENCIES false)" - ) -else() - if (NOT EXISTS hyper_install AND OBOS_ARCHITECTURE STREQUAL "x86_64") - include("dependencies/hyper.cmake") - endif() - if (OBOS_USE_LIMINE AND NOT EXISTS limine_exe) - include("dependencies/limine.cmake") - endif() - if (NOT EXISTS uacpi_cmake_file) - include("dependencies/uacpi.cmake") - endif() - include("dependencies/flanterm.cmake") - include("dependencies/uhda.cmake") - include("dependencies/freebsd-e1000.cmake") -endif() - -if (OBOS_UP) - add_compile_definitions(OBOS_UP=1) -endif() - -if (OBOS_DEBUG_FREE_SIZE) - add_compile_definitions(OBOS_DEBUG_FREE_SIZE=1) -endif() - -if (OBOS_NVS_SAVE_S3) - set (OBOS_NVS_SAVE_S3 "1") -else() - set (OBOS_NVS_SAVE_S3 "0") -endif() -add_compile_definitions(OBOS_NVS_SAVE_S3=${OBOS_NVS_SAVE_S3}) - -if (OBOS_LAZY_IRQL) - set (OBOS_LAZY_IRQL "1") -else() - set (OBOS_LAZY_IRQL "0") -endif() - -add_compile_definitions(OBOS_LAZY_IRQL=${OBOS_LAZY_IRQL}) -if (OBOS_USE_LIMINE) - set (OBOS_USE_LIMINE "1") -else() - set (OBOS_USE_LIMINE "0") -endif() -add_compile_definitions(OBOS_USE_LIMINE=${OBOS_USE_LIMINE}) - -if (OBOS_ENABLE_LOCK_PROFILING) - set (OBOS_ENABLE_LOCK_PROFILING "1") -else() - set (OBOS_ENABLE_LOCK_PROFILING "0") -endif() -add_compile_definitions(OBOS_ENABLE_LOCK_PROFILING=${OBOS_ENABLE_LOCK_PROFILING}) - -if (OBOS_ENABLE_PROFILER) - set (OBOS_ENABLE_PROFILER "1") -else() - set (OBOS_ENABLE_PROFILER "0") -endif() -add_compile_definitions(OBOS_ENABLE_PROFILING=${OBOS_ENABLE_PROFILER}) - -add_compile_definitions(OBOS_BINARY_DIRECTORY="${CMAKE_RUNTIME_OUTPUT_DIRECTORY}") - -add_compile_definitions(OBOS_ARCHITECTURE_BITS=${OBOS_ARCHITECTURE_BITS}) - -add_compile_options($<$:-fno-strict-aliasing>) - -execute_process(COMMAND - "${GIT_EXECUTABLE}" describe --match=NeVeRmAtCh --always --abbrev=40 --dirty - WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" - OUTPUT_VARIABLE GIT_SHA1 - ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) - -# the date of the commit -execute_process(COMMAND - "${GIT_EXECUTABLE}" log -1 --format=%ad --date=local - WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" - OUTPUT_VARIABLE GIT_DATE - ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) - -add_compile_definitions (GIT_SHA1="${GIT_SHA1}" GIT_DATE="${GIT_DATE}") -add_compile_definitions (OBOS_ARCHITECTURE_ENDIANNESS="${OBOS_ARCHITECTURE_ENDIANNESS}") - -#list (APPEND oboskrnl_sources "sanitizers/ubsan.c") -if (OBOS_ENABLE_UBSAN) - add_compile_options( - $<$:-fsanitize=undefined> - ) - set (OBOS_ENABLE_UBSAN "1") -else() - set (OBOS_ENABLE_UBSAN "0") -endif() -add_compile_definitions(OBOS_UBSAN_ENABLED=${OBOS_ENABLE_UBSAN}) - -#list (APPEND oboskrnl_sources "sanitizers/asan.c") -if (OBOS_ENABLE_UHDA) - set (OBOS_ENABLE_UHDA "1") -else() - set (OBOS_ENABLE_UHDA "0") -endif() -add_compile_definitions(OBOS_ENABLE_UHDA=${OBOS_ENABLE_UHDA}) -add_compile_definitions(OBOS_IRQL_COUNT=${OBOS_IRQL_COUNT}) - -add_compile_definitions( - $<$:OBOS_DEBUG> - $<$:OBOS_RELEASE> - $<$:OBOS_RELEASE> - $<$:OBOS_RELEASE> -) - -if (CMAKE_BUILD_TYPE STREQUAL "Debug") - set(OBOS_DEBUG "1") -endif() -if (CMAKE_BUILD_TYPE STREQUAL "Release" OR - CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo" OR - CMAKE_BUILD_TYPE STREQUAL "MinSizeRel") - set(OBOS_RELEASE "1") -endif() - -string(TOUPPER "${OBOS_PAGE_REPLACEMENT_ALGORITHM}" OBOS_PAGE_REPLACEMENT_ALGORITHM) -set(OBOS_PAGE_REPLACEMENT_ALGORITHM_DEFINE "OBOS_PAGE_REPLACEMENT_") -string(APPEND OBOS_PAGE_REPLACEMENT_ALGORITHM_DEFINE "${OBOS_PAGE_REPLACEMENT_ALGORITHM}") -add_compile_definitions(${OBOS_PAGE_REPLACEMENT_ALGORITHM_DEFINE}=1) -string(APPEND OBOS_PAGE_REPLACEMENT_ALGORITHM "\"") -string(PREPEND OBOS_PAGE_REPLACEMENT_ALGORITHM "\"") -add_compile_definitions(OBOS_PAGE_REPLACEMENT_ALGORITHM=${OBOS_PAGE_REPLACEMENT_ALGORITHM}) - -set_property(GLOBAL PROPERTY C_STANDARD 17 - PROPERTY C_STANDARD_REQUIRED true - PROPERTY C_EXTENSIONS ON) - -if (OBOS_ENABLE_WERROR AND NOT OBOS_ENABLE_FANALYZER) - add_compile_options("-Werror" $<$:-Wno-error=unused-function> $<$:-Wno-error=unused-parameter>) -endif() -if (OBOS_ENABLE_FANALYZER) - add_compile_options($<$:-fanalyzer>) -endif() -if (OBOS_ENABLE_FANALYZER AND OBOS_ENABLE_WERROR) - message(WARNING "OBOS_ENABLE_FANALYZER and OBOS_ENABLE_WERROR are mutually exclusive. Preferring OBOS_ENABLE_FANALYZER.") -endif() - -# Now done somewhere else. -# if (NOT DEFINED OBOS_DEV_PREFIX) -# set(OBOS_DEV_PREFIX "/dev/") -# endif() -configure_file(${CMAKE_SOURCE_DIR}/src/inc/dev_prefix.h.in ${CMAKE_SOURCE_DIR}/src/oboskrnl/inc/dev_prefix.h @ONLY) - -add_compile_definitions( - OBOS_ARCHITECTURE_HAS_ACPI=${OBOS_ARCHITECTURE_HAS_ACPI} - OBOS_ARCHITECTURE_HAS_PCI=${OBOS_ARCHITECTURE_HAS_PCI} - OBOS_PERM_PREFIX="${OBOS_PERM_PREFIX}" -) - -if (OBOS_USE_OBOS_GCC AND OBOS_COMPILE_USER_UTILS) - add_subdirectory("src/init") -endif() -if (OBOS_COMPILE_USER_UTILS) - add_subdirectory("src/user-utilities") -endif() - -if (OBOS_ONLY_COMPILE_USER_UTILS) - return() -endif() - -if (OBOS_ENABLE_KASAN) - add_compile_options( - $<$:-fsanitize=kernel-address> - ) - set (OBOS_ENABLE_KASAN "1") -else() - set (OBOS_ENABLE_KASAN "0") -endif() -add_compile_definitions(OBOS_KASAN_ENABLED=${OBOS_ENABLE_KASAN}) - -# Include the kernel -add_subdirectory("src/oboskrnl") -if (OBOS_ARCHITECTURE STREQUAL "m68k") - add_subdirectory("src/oboskrnl/arch/m68k/loader") -endif() -if (OBOS_ARCHITECTURE_HAS_ACPI) - add_subdirectory("src/uACPI") -endif() -if (OBOS_ENABLE_UHDA) - add_subdirectory("src/uHDA") -endif() -add_subdirectory("src/sanitizers") -# Include drivers -if (OBOS_ARCHITECTURE_HAS_ACPI) - get_target_property(uacpi_INCLUDES uacpi INCLUDE_DIRECTORIES) - include_directories(${uacpi_INCLUDES}) -endif() -add_subdirectory("src/drivers/generic/libps2") -add_subdirectory("src/drivers/generic/initrd") -add_subdirectory("src/drivers/generic/extfs") -add_subdirectory("src/drivers/test_driver") -add_subdirectory("src/drivers/generic/ahci") -add_subdirectory("src/drivers/generic/r8169") -add_subdirectory("src/drivers/generic/freebsd-e1000") -if (OBOS_ARCHITECTURE STREQUAL "x86_64") - add_subdirectory("src/drivers/x86/bochs_vbe") - add_subdirectory("src/drivers/x86/uart") - add_subdirectory("src/drivers/x86/i8042") -endif() +# CMakeLists.txt +# +# Copyright (c) 2024 Omar Berrow + +cmake_minimum_required(VERSION 3.7) + +project(OBOS C CXX ASM_NASM) + +# Download dependencies. +# NOTE: As dependencies are added, please add them to the .gitignore file. +# If a dependency cmake file is included, and OBOS_REFRESH_DEPENDENCIES is false, the cmake file shall not download or make any file operations, and only set any cache variables related +# to the dependency (eg. limine_install, zydis_c, uacpi_cmake_file, etc.) +if (EXISTS ${CMAKE_SOURCE_DIR}/dependencies/needs_download.cmake) + include (dependencies/needs_download.cmake) +else() + set (OBOS_REFRESH_DEPENDENCIES true) +endif() +set (OBOSKRNL_EXTERNAL_INCLUDES "${CMAKE_SOURCE_DIR}/dependencies/include") +if (OBOS_REFRESH_DEPENDENCIES) + file(MAKE_DIRECTORY "dependencies/include") + if (OBOS_ARCHITECTURE STREQUAL "x86_64") + message("Fetching Limine.") + include("dependencies/limine.cmake") + if (DEFINED OBOS_ENABLE_KDBG AND DEFINED OBOS_KDBG_ENABLE_ZYDIS) + message("Fetching ZyDis.") + include("dependencies/zydis.cmake") + endif() + endif() + message("Fetching uACPI.") + include ("dependencies/uacpi.cmake") + file (WRITE "${CMAKE_SOURCE_DIR}/dependencies/needs_download.cmake" + "# This file is auto-generated.\n# Set this variable to true or delete this file to refresh all dependencies.\nset (OBOS_REFRESH_DEPENDENCIES false)" + ) +else() + if (OBOS_ARCHITECTURE STREQUAL "x86_64") + if (NOT EXISTS limine_install) + include("dependencies/limine.cmake") + endif() + if (DEFINED OBOS_ENABLE_KDBG AND DEFINED OBOS_KDBG_ENABLE_ZYDIS AND NOT EXISTS zydis_c) + include("dependencies/zydis.cmake") + endif() + endif() + if (NOT EXISTS uacpi_cmake_file) + include("dependencies/uacpi.cmake") + endif() +endif() + +if (NOT DEFINED OUTPUT_DIR) + set (OUTPUT_DIR ${CMAKE_SOURCE_DIR}/out) +endif() + +if (DEFINED OBOS_SLOW_ARTHIMETRIC) + add_compile_definitions(OBOS_SLOW_ARTHIMETRIC=1) +endif() + +set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_DIR}) + +add_compile_definitions(OBOS_ARCHITECTURE_BITS=${OBOS_ARCHITECTURE_BITS}) + +if (OBOS_ARCHITECTURE_ENDIANNESS STREQUAL "Little-Endian") + add_compile_definitions("OBOS_ARCHITECTURE_ENDIANNESS=obos::arch::endianness::LittleEndian") +elseif (OBOS_ARCHITECTURE_ENDIANNESS STREQUAL "Big-Endian") + add_compile_definitions("OBOS_ARCHITECTURE_ENDIANNESS=obos::arch::endianness::BigEndian") +else() + add_compile_definitions("OBOS_ARCHITECTURE_ENDIANNESS=obos::arch::endianness::MixedEndian") +endif() + +add_subdirectory("src/libs/uACPI") +add_subdirectory("src/oboskrnl") +add_subdirectory("src/isogen") +add_subdirectory("src/drivers/testDriver") diff --git a/LICENSE b/LICENSE index 9b9a1db8..f1b1bf92 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,21 @@ -MIT License - -Copyright (c) 2024-2026 Omar Berrow and contributors - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +MIT License + +Copyright (c) 2024 Omar Berrow + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/OVMF/OVMF_CODE_4M.fd b/OVMF/OVMF_CODE_4M.fd new file mode 100644 index 00000000..a180af56 Binary files /dev/null and b/OVMF/OVMF_CODE_4M.fd differ diff --git a/OVMF/OVMF_VARS_4M.fd b/OVMF/OVMF_VARS_4M.fd new file mode 100644 index 00000000..4be7c12c Binary files /dev/null and b/OVMF/OVMF_VARS_4M.fd differ diff --git a/README.md b/README.md index 34a956a8..a8f245f6 100644 --- a/README.md +++ b/README.md @@ -1,93 +1,25 @@ -# OBOS -OBOS uses ***✨ state of the art technology ✨*** to do ***✨ almost nothing ✨*** -## Downloads -- The obos file server can be found at http://obos-dev.ddns.net/ -## Building -### Prerequisites -- (For an x86-64 build) NASM -- (For an x86-64 build): [x86_64-elf gcc](https://github.com/lordmilko/i686-elf-tools/) or an x86_64-obos toolchain (if you use method one, this is done for you) -- CMake -- xorriso -- Ninja -- Git (unoptional, even if the repo was downloaded manually) -- (Optionally): Qemu to run the iso -- (if using method one) libcjson -### Build -#### Method 1 -1. Clone the obos-pkgs repo using git: -```sh -git clone https://github.com/OBOS-dev/obos-pkgs.git -``` -2. Enter the newly clone repo -3. Run the following commands to build an OBOS distribution: -```sh -./bootstrap-repo -./obos-strap setup-env -# Run this for a minimal distribution: -./obos-strap install minimal -# Run this for all packages to be installed -./obos-strap install all -``` -4. Use qemu or any other emulator to run your ISO. -#### Method 2 (for kernel contributors) -1. Clone the repo using git: -```sh -git clone https://github.com/OBOS-dev/obos.git -``` -2. Enter the newly cloned repo -3. Run cmake to build the OS -(x86_64) -```sh -touch config/initrd.tar -cmake -GNinja -DCMAKE_BUILD_TYPE=Debug --toolchain=src/build/x86_64/toolchain.cmake -B build/x86_64 . -chmod +x dependencies/hyper/hyper_install-linux-x86_64 -cmake --build build/x86_64 -scripts/generate_initrd-x86_64.sh -cmake --build build/x86_64 -``` -4. To run the kernel, run the script under scripts/launch_qemu.\[bat,sh\] - -(Linux) -```sh -cd scripts -chmod +x ./launch_qemu.sh -./launch_qemu.sh -``` -(Windows) -```bat -cd scripts -call launch_qemu.bat -``` -## Port status -- x86_64-obos: Working, not all ported programs work -- m68k-obos: Userspace is unstable, but the port works otherwise -- For information on ported programs, go to the obos-pkgs repository -## License Notice -Most code in this repository is licensed under the MIT license.
-Any code from other projects that require a license notice will be put below. -### src/oboskrnl/utils/tree.h -Taken from sys/tree.h from OpenBSD, and is licensed under this: -``` - Copyright 2002 Niels Provos - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -``` +# OBOS +## Building +### Prerequisites +- xorriso +- An x86_64-elf gcc toolchain. Preferably version 13.2.0 for GCC and 2.41 for binutils. +- Nasm +- CMake +- Git (Not optional, even if you download the repository) +- Ninja +- Optionally, qemu to run the ISO generated by the build.

+All these tools are avaliable on windows if you look well enough. Building for windows should always work. +On Linux, use your package manager to install these packages if you don't have them already. +### The build process +1. Clone the repository using git, or download it. +``` +git clone https://github.com/oberrow/obos.git +``` +3. Open a terminal inside the directory the repostory was downloaded into. +4. Run these commands to build:

+For x86_64: +```sh +cmake -GNinja -DCMAKE_BUILD_TYPE=Debug --toolchain=src/toolchains/x86_64/toolchain.cmake . +ninja -j0 +``` +4. To run the newly built iso in qemu, run the appropriate script for your Host OS and Guest OS under the scripts directory. diff --git a/config/hyper.cfg b/config/hyper.cfg deleted file mode 100644 index 5bbf11b7..00000000 --- a/config/hyper.cfg +++ /dev/null @@ -1,34 +0,0 @@ -default-entry = "OBOS" - -[OBOS] -protocol=ultra -higher-half-exclusive=true -kernel-as-module=true -video-mode=auto -cmdline='initrd-module=initrd initrd-driver-module=initrd_driver root-fs-partid initrd load-modules=/libps2,/extfs initial-swap-size=67108864 log-level=1 init-args /usr/bin/bash --login' - -binary: - path="/obos/oboskrnl" - allocate-anywhere=true - -page-table: - levels=4 - constraint=exactly - null-guard=true - -module: - type="file" - path="/obos/initrd.tar" - name="initrd" -module: - type="file" - path="/obos/initrd" - name="initrd_driver" -module: - type="memory" - name="FLANTERM_BUFF" - size=16777216 -module: - type="memory" - name="BUMP_REGION" - size=16777216 diff --git a/config/hyper_uefi_boot.bin b/config/hyper_uefi_boot.bin deleted file mode 100644 index b62d5002..00000000 Binary files a/config/hyper_uefi_boot.bin and /dev/null differ diff --git a/config/limine.cfg b/config/limine.cfg new file mode 100644 index 00000000..0784752b --- /dev/null +++ b/config/limine.cfg @@ -0,0 +1,15 @@ +# Timeout in seconds that Limine will use before automatically booting. +TIMEOUT=1 +QUIET=yes +DEFAULT_ENTRY=1 +RANDOMIZE_MEMORY=no + +:OBOS + PROTOCOL=limine + + KASLR=no + + MODULE_PATH=boot:///obos/testDriver + KERNEL_PATH=boot:///obos/oboskrnl +:Next Bootable Disk + PROTOCOL=chainload_next \ No newline at end of file diff --git a/config/limine.conf b/config/limine.conf deleted file mode 100644 index 6cabd5c7..00000000 --- a/config/limine.conf +++ /dev/null @@ -1,32 +0,0 @@ -timeout: 5 - -/OBOS - protocol: limine - comment: OBOS - - paging_mode: 4level - module_path: boot():/obos/initrd.tar - module_string: initrd - module_path: boot():/obos/initrd - module_string: initrd_driver - module_path: boot():/obos/flanterm_buff - module_string: FLANTERM_BUFF - module_path: boot():/obos/flanterm_buff - module_string: BUMP_REGION - path: boot():/obos/oboskrnl - cmdline: initrd-module=initrd initrd-driver-module=initrd_driver root-fs-partid=initrd load-modules=/libps2,/extfs initial-swap-size=67108864 log-level=1 init-args /usr/bin/bash --login -/OBOS (Login) - protocol: limine - comment: OBOS (Login Prompt) - - paging_mode: 4level - module_path: boot():/obos/initrd.tar - module_string: initrd - module_path: boot():/obos/initrd - module_string: initrd_driver - module_path: boot():/obos/flanterm_buff - module_string: FLANTERM_BUFF - module_path: boot():/obos/flanterm_buff - module_string: BUMP_REGION - path: boot():/obos/oboskrnl - cmdline: initrd-module=initrd initrd-driver-module=initrd_driver root-fs-partid=initrd load-modules=/libps2,/extfs initial-swap-size=67108864 log-level=1 init-args /usr/bin/bash --login -c login \ No newline at end of file diff --git a/CONTRIBUTING.md b/contribution.md similarity index 58% rename from CONTRIBUTING.md rename to contribution.md index 4363d357..f726758b 100644 --- a/CONTRIBUTING.md +++ b/contribution.md @@ -4,7 +4,7 @@ Make sure to make your commits with a couple changes each. Don't forget to describe said changes with a relatively-descriptive commit message. ## Pull Requests Any pull requests are welcome, as long as no Rust, or other Esoteric languages are included in the code, and they include a feature that actually matters. -All commits should be made on a separate local branch, pushed onto the main repo. You should make a pull request describing your changes (unless they're self explanatory because of the branch name).
## Notes -Please don't add backdoors to the kernel or other parts of the OS.
-Don't modify src/oboskrnl/int.h, unless absolutely neccessary (it will cause a rebuild for any other contributors). +Any pushes directly to master will be rejected due to branch protection rules, which is why you might see in the git commit history that this file was on a separate branch for a bit.
+All commits should be made on a separate local branch, pushed onto the main repo, and a pull request you should make a pull request describing your changes (unless they're self explanatory because of the branch name).
+Please don't add backdoors to the kernel or other parts of the OS. \ No newline at end of file diff --git a/dependencies/flanterm.cmake b/dependencies/flanterm.cmake deleted file mode 100644 index ccbfff9f..00000000 --- a/dependencies/flanterm.cmake +++ /dev/null @@ -1,23 +0,0 @@ -# dependencies/flanterm.cmake - -# Copyright (c) 2025 Omar Berrow - -if (OBOS_REFRESH_DEPENDENCIES) - include(FetchContent) - - FetchContent_Declare(flanterm - GIT_REPOSITORY https://codeberg.org/mintsuki/flanterm.git - GIT_TAG v2.0.0 - SOURCE_DIR ${CMAKE_SOURCE_DIR}/dependencies/flanterm - ) - FetchContent_MakeAvailable(flanterm) - file (COPY "${CMAKE_SOURCE_DIR}/dependencies/flanterm/src/flanterm.h" DESTINATION "${CMAKE_SOURCE_DIR}/dependencies/include/") - file (COPY "${CMAKE_SOURCE_DIR}/dependencies/flanterm/src/flanterm_private.h" DESTINATION "${CMAKE_SOURCE_DIR}/dependencies/include/") - file(MAKE_DIRECTORY "${CMAKE_SOURCE_DIR}/dependencies/flanterm/src/flanterm_backends") - file (COPY "${CMAKE_SOURCE_DIR}/dependencies/flanterm/src/flanterm_backends/fb.h" DESTINATION "${CMAKE_SOURCE_DIR}/dependencies/include/flanterm_backends/") - file (COPY "${CMAKE_SOURCE_DIR}/dependencies/flanterm/src/flanterm_backends/fb_private.h" DESTINATION "${CMAKE_SOURCE_DIR}/dependencies/include/flanterm_backends/") -endif() -list (APPEND oboskrnl_sources - "${CMAKE_SOURCE_DIR}/dependencies/flanterm/src/flanterm.c" - "${CMAKE_SOURCE_DIR}/dependencies/flanterm/src/flanterm_backends/fb.c" -) diff --git a/dependencies/freebsd-e1000.cmake b/dependencies/freebsd-e1000.cmake deleted file mode 100644 index aabd92ac..00000000 --- a/dependencies/freebsd-e1000.cmake +++ /dev/null @@ -1,19 +0,0 @@ -# dependencies/freebsd-e1000.cmake - -# Copyright (c) 2025 Omar Berrow - -if (OBOS_REFRESH_DEPENDENCIES) - include(FetchContent) - - FetchContent_Declare(freebsd-e1000 - GIT_REPOSITORY https://github.com/managarm/freebsd-e1000.git - GIT_TAG import - SOURCE_DIR ${CMAKE_SOURCE_DIR}/dependencies/freebsd-e1000 - ) - FetchContent_MakeAvailable(freebsd-e1000) - - file (GLOB E1000_HEADERS SRC_FILES "${CMAKE_SOURCE_DIR}/dependencies/freebsd-e1000/*.h") - file (COPY ${E1000_HEADERS} DESTINATION "${CMAKE_SOURCE_DIR}/dependencies/include/e1000/") -endif() - -file (GLOB E1000_SOURCES SRC_FILES "${CMAKE_SOURCE_DIR}/dependencies/freebsd-e1000/*.c") diff --git a/dependencies/hyper.cmake b/dependencies/hyper.cmake deleted file mode 100644 index 12ee1287..00000000 --- a/dependencies/hyper.cmake +++ /dev/null @@ -1,37 +0,0 @@ -# dependencies/hyper.cmake - -# Copyright (c) 2024 Omar Berrow - -include(FetchContent) - -file(MAKE_DIRECTORY "dependencies/include/UltraProtocol") -file(DOWNLOAD - https://github.com/UltraOS/Hyper/releases/download/v0.12.0/hyper_iso_boot - ${CMAKE_SOURCE_DIR}/dependencies/hyper/hyper_iso_boot -) -file(DOWNLOAD - https://github.com/UltraOS/Hyper/releases/download/v0.12.0/BOOTX64.EFI - ${CMAKE_SOURCE_DIR}/dependencies/hyper/BOOTX64.efi -) -if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows" OR CMAKE_HOST_SYSTEM_NAME STREQUAL "MSYS") - set (hyper_install ${CMAKE_SOURCE_DIR}/dependencies/hyper/hyper_install-win64.exe CACHE INTERNAL "The hyper install binary") - file(DOWNLOAD - https://github.com/UltraOS/Hyper/releases/download/v0.12.0/hyper_install-win64.exe - ${hyper_install} - ) -elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux") - set (hyper_install ${CMAKE_SOURCE_DIR}/dependencies/hyper/hyper_install-linux-x86_64 CACHE INTERNAL "The hyper install binary") - file(DOWNLOAD - https://github.com/UltraOS/Hyper/releases/download/v0.12.0/hyper_install-linux-x86_64 - ${hyper_install} - ) -else() - message(FATAL_ERROR "You must be on windows or linux to compile OBOS.") -endif() -FetchContent_Declare(UltraProtocol - GIT_REPOSITORY https://github.com/UltraOS/UltraProtocol.git - GIT_TAG db30db897d0da7c48d4d630a1367c4a588791191 - SOURCE_DIR ${CMAKE_SOURCE_DIR}/dependencies/UltraProtocol -) -FetchContent_MakeAvailable(UltraProtocol) -file (COPY_FILE "dependencies/UltraProtocol/ultra_protocol.h" "dependencies/include/UltraProtocol/ultra_protocol.h") diff --git a/dependencies/limine.cmake b/dependencies/limine.cmake index 715ca8e6..9d20aa80 100644 --- a/dependencies/limine.cmake +++ b/dependencies/limine.cmake @@ -1,26 +1,31 @@ -# dependencies/limine.cmake - -# Copyright (c) 2025 Omar Berrow - -include(FetchContent) - -if (OBOS_REFRESH_DEPENDENCIES) - FetchContent_Declare(Limine - GIT_REPOSITORY https://github.com/limine-bootloader/limine.git - GIT_TAG v10.3.0-binary - SOURCE_DIR ${CMAKE_SOURCE_DIR}/dependencies/limine - ) - FetchContent_MakeAvailable(Limine) - file (COPY_FILE "${CMAKE_SOURCE_DIR}/dependencies/limine/limine.h" "${CMAKE_SOURCE_DIR}/dependencies/include/limine.h") -endif() -if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux") - execute_process( - COMMAND "make" - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/dependencies/limine - ) - set (limine_exe ${CMAKE_SOURCE_DIR}/dependencies/limine/limine CACHE INTERNAL "x") - # elseif (CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows" OR CMAKE_HOST_SYSTEM_NAME STREQUAL "MSYS") - # set (limine_exe ${CMAKE_SOURCE_DIR}/dependencies/limine/limine.exe CACHE INTERNAL "x") -else() - message(FATAL_ERROR "You must be on Linux to compile OBOS with Limine.") -endif() +# dependencies/limine.cmake + +# Copyright (c) 2024 Omar Berrow + + +if (OBOS_REFRESH_DEPENDENCIES) + include(FetchContent) + file(MAKE_DIRECTORY "dependencies/include/limine") + + FetchContent_Declare(Limine + GIT_REPOSITORY https://github.com/limine-bootloader/limine.git + GIT_TAG 61558c6da9c2b0d23bfb7bc74135345acded0b73 + SOURCE_DIR ${CMAKE_SOURCE_DIR}/dependencies/limine + ) + FetchContent_MakeAvailable(Limine) + file (COPY "${CMAKE_SOURCE_DIR}/dependencies/limine/limine.h" DESTINATION "${CMAKE_SOURCE_DIR}/dependencies/include/limine/") +endif() + +if (CMAKE_HOST_WIN32) + set (limine_install ${CMAKE_SOURCE_DIR}/dependencies/limine/limine.exe CACHE INTERNAL "The limine install binary") +elseif(CMAKE_HOST_UNIX) + set (limine_install ${CMAKE_SOURCE_DIR}/dependencies/limine/limine CACHE INTERNAL "The limine install binary") + if (OBOS_REFRESH_DEPENDENCIES) + execute_process( + COMMAND make + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/dependencies/limine/ + ) + endif() +else() + message(FATAL_ERROR "You must be on windows or linux to compile OBOS.") +endif() \ No newline at end of file diff --git a/dependencies/uacpi.cmake b/dependencies/uacpi.cmake index ec92513b..fca051e5 100644 --- a/dependencies/uacpi.cmake +++ b/dependencies/uacpi.cmake @@ -1,19 +1,16 @@ # dependencies/uacpi.cmake -# Copyright (c) 2024-2025 Omar Berrow +# Copyright (c) 2024 Omar Berrow if (OBOS_REFRESH_DEPENDENCIES) include(FetchContent) FetchContent_Declare(uACPI - # In honor of the old uACPI link - #GIT_REPOSITORY https://github.com/UltraOS/uACPI.git - GIT_REPOSITORY https://github.com/uACPI/uACPI.git - GIT_TAG 6.0.0 + GIT_REPOSITORY https://github.com/UltraOS/uACPI.git SOURCE_DIR ${CMAKE_SOURCE_DIR}/dependencies/uACPI ) FetchContent_MakeAvailable(uACPI) file (COPY "${CMAKE_SOURCE_DIR}/dependencies/uACPI/include/uacpi" DESTINATION "${CMAKE_SOURCE_DIR}/dependencies/include/") endif() -set (uacpi_cmake_file ${CMAKE_SOURCE_DIR}/dependencies/uACPI/uacpi.cmake CACHE INTERNAL "The uACPI CMake file.") +set (uacpi_cmake_file ${CMAKE_SOURCE_DIR}/dependencies/uACPI/uacpi.cmake CACHE INTERNAL "The uACPI CMake file.") \ No newline at end of file diff --git a/dependencies/uhda.cmake b/dependencies/uhda.cmake deleted file mode 100644 index 909e20e6..00000000 --- a/dependencies/uhda.cmake +++ /dev/null @@ -1,17 +0,0 @@ -# dependencies/uhda.cmake - -# Copyright (c) 2025 Omar Berrow - -if (OBOS_REFRESH_DEPENDENCIES) - include(FetchContent) - - FetchContent_Declare(uHDA - GIT_REPOSITORY https://github.com/uDrivers/uHDA.git - GIT_TAG a631f3e7c794a7c8b55b3d54faa6636c224e26e6 - SOURCE_DIR ${CMAKE_SOURCE_DIR}/dependencies/uHDA - ) - FetchContent_MakeAvailable(uHDA) - - file (COPY "${CMAKE_SOURCE_DIR}/dependencies/uHDA/include/uhda" DESTINATION "${CMAKE_SOURCE_DIR}/dependencies/include/") -endif() -set (uhda_cmake_file ${CMAKE_SOURCE_DIR}/dependencies/uHDA/uhda.cmake CACHE INTERNAL "The uHDA CMake file.") diff --git a/dependencies/zydis.cmake b/dependencies/zydis.cmake new file mode 100644 index 00000000..ded5f8d5 --- /dev/null +++ b/dependencies/zydis.cmake @@ -0,0 +1,24 @@ +# dependencies/zydis.cmake + +# Copyright (c) 2024 Omar Berrow + +if (NOT EXISTS ${CMAKE_SOURCE_DIR}/dependencies/ZyDis) + file (MAKE_DIRECTORY ${CMAKE_SOURCE_DIR}/dependencies/ZyDis) +endif() +if (OBOS_REFRESH_DEPENDENCIES) + include(FetchContent) + + FetchContent_Declare( + ZyDis + URL https://github.com/zyantific/zydis/releases/download/v4.1.0/zydis-amalgamated.tar.gz + DOWNLOAD_DIR ${CMAKE_SOURCE_DIR}/dependencies/ZyDis + ) + FetchContent_MakeAvailable(ZyDis) + execute_process( + COMMAND ${CMAKE_COMMAND} -E tar -xzf ${CMAKE_SOURCE_DIR}/dependencies/ZyDis/zydis-amalgamated.tar.gz + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/dependencies/ZyDis/ + ) + file (COPY "${CMAKE_SOURCE_DIR}/dependencies/ZyDis/amalgamated-dist/Zydis.h" DESTINATION "${CMAKE_SOURCE_DIR}/dependencies/include/") + file (REMOVE ${CMAKE_SOURCE_DIR}/dependencies/ZyDis/zydis-amalgamated.tar.gz) +endif() +set (zydis_c ${CMAKE_SOURCE_DIR}/dependencies/ZyDis/amalgamated-dist/Zydis.c CACHE INTERNAL "The amalgamated ZyDis source file.") diff --git a/screenshots/20240314_0749.png b/screenshots/20240314_0749.png new file mode 100644 index 00000000..e5e2c619 Binary files /dev/null and b/screenshots/20240314_0749.png differ diff --git a/screenshots/20240314_0816.png b/screenshots/20240314_0816.png new file mode 100644 index 00000000..25104e60 Binary files /dev/null and b/screenshots/20240314_0816.png differ diff --git a/screenshots/20240322_2149.png b/screenshots/20240322_2149.png new file mode 100644 index 00000000..22627102 Binary files /dev/null and b/screenshots/20240322_2149.png differ diff --git a/screenshots/20240329_1129.png b/screenshots/20240329_1129.png new file mode 100644 index 00000000..420176cb Binary files /dev/null and b/screenshots/20240329_1129.png differ diff --git a/screenshots/20240329_1608.png b/screenshots/20240329_1608.png new file mode 100644 index 00000000..20fa4230 Binary files /dev/null and b/screenshots/20240329_1608.png differ diff --git a/screenshots/20240401_1424.png b/screenshots/20240401_1424.png new file mode 100644 index 00000000..81298aa3 Binary files /dev/null and b/screenshots/20240401_1424.png differ diff --git a/screenshots/20240401_1624.png b/screenshots/20240401_1624.png new file mode 100644 index 00000000..fa599e88 Binary files /dev/null and b/screenshots/20240401_1624.png differ diff --git a/screenshots/20240413_2258.png b/screenshots/20240413_2258.png new file mode 100644 index 00000000..9307a62d Binary files /dev/null and b/screenshots/20240413_2258.png differ diff --git a/screenshots/20240427_1723.png b/screenshots/20240427_1723.png new file mode 100644 index 00000000..0ee1a4b5 Binary files /dev/null and b/screenshots/20240427_1723.png differ diff --git a/screenshots/20240428_1618.png b/screenshots/20240428_1618.png new file mode 100644 index 00000000..43e4e28a Binary files /dev/null and b/screenshots/20240428_1618.png differ diff --git a/screenshots/20240512_2114.png b/screenshots/20240512_2114.png deleted file mode 100644 index 80d2c25a..00000000 Binary files a/screenshots/20240512_2114.png and /dev/null differ diff --git a/screenshots/20240728_2338.png b/screenshots/20240728_2338.png deleted file mode 100644 index 50284718..00000000 Binary files a/screenshots/20240728_2338.png and /dev/null differ diff --git a/screenshots/20240729_2257.png b/screenshots/20240729_2257.png deleted file mode 100644 index 671e6728..00000000 Binary files a/screenshots/20240729_2257.png and /dev/null differ diff --git a/screenshots/20240730_1559.png b/screenshots/20240730_1559.png deleted file mode 100644 index d2ed18ac..00000000 Binary files a/screenshots/20240730_1559.png and /dev/null differ diff --git a/screenshots/20240730_1639.png b/screenshots/20240730_1639.png deleted file mode 100644 index 1aa0ec11..00000000 Binary files a/screenshots/20240730_1639.png and /dev/null differ diff --git a/screenshots/20240806_1401.png b/screenshots/20240806_1401.png deleted file mode 100644 index 5e68d685..00000000 Binary files a/screenshots/20240806_1401.png and /dev/null differ diff --git a/screenshots/20240806_1612.png b/screenshots/20240806_1612.png deleted file mode 100644 index b8df963e..00000000 Binary files a/screenshots/20240806_1612.png and /dev/null differ diff --git a/screenshots/20240807_1158.png b/screenshots/20240807_1158.png deleted file mode 100644 index 3ec6f65c..00000000 Binary files a/screenshots/20240807_1158.png and /dev/null differ diff --git a/screenshots/20240823_1544.png b/screenshots/20240823_1544.png deleted file mode 100644 index 19189d5d..00000000 Binary files a/screenshots/20240823_1544.png and /dev/null differ diff --git a/screenshots/20241017_1948.png b/screenshots/20241017_1948.png deleted file mode 100644 index 77257164..00000000 Binary files a/screenshots/20241017_1948.png and /dev/null differ diff --git a/screenshots/20241213_1753.png b/screenshots/20241213_1753.png deleted file mode 100644 index 5cf02648..00000000 Binary files a/screenshots/20241213_1753.png and /dev/null differ diff --git a/screenshots/20241230_1558.png b/screenshots/20241230_1558.png deleted file mode 100644 index 930b169e..00000000 Binary files a/screenshots/20241230_1558.png and /dev/null differ diff --git a/screenshots/20250719_1658.png b/screenshots/20250719_1658.png deleted file mode 100644 index f34d4a7d..00000000 Binary files a/screenshots/20250719_1658.png and /dev/null differ diff --git a/screenshots/20250821_1428.png b/screenshots/20250821_1428.png deleted file mode 100644 index 45ec99cf..00000000 Binary files a/screenshots/20250821_1428.png and /dev/null differ diff --git a/screenshots/20251202_0930.png b/screenshots/20251202_0930.png deleted file mode 100644 index fa9682cd..00000000 Binary files a/screenshots/20251202_0930.png and /dev/null differ diff --git a/screenshots/20251213_2238.png b/screenshots/20251213_2238.png deleted file mode 100644 index 9ab76f77..00000000 Binary files a/screenshots/20251213_2238.png and /dev/null differ diff --git a/scripts/find_addr.sh b/scripts/find_addr.sh old mode 100755 new mode 100644 index 86808e88..4da8f3a6 --- a/scripts/find_addr.sh +++ b/scripts/find_addr.sh @@ -1,3 +1,5 @@ +clear + cd .. export address=$1 export exe=$2 @@ -5,7 +7,6 @@ if [ -z "$exe" ] then export exe=out/oboskrnl fi -addr2line -e $exe -Cfpira $address -gdb --nx "$exe" -batch -ex "set disassembly-flavor intel" -ex "disassemble/sr $address" | grep --color -i -C 10 -n $address -# objdump -d $exe -C -M intel | grep --color -i -C 10 -n $address +addr2line -e $exe -Cfpira 0x$address +objdump -d $exe -C -M intel | grep --color -C 10 -n $address cd scripts diff --git a/scripts/find_addr_m68k.sh b/scripts/find_addr_m68k.sh deleted file mode 100755 index a33cc6c6..00000000 --- a/scripts/find_addr_m68k.sh +++ /dev/null @@ -1,11 +0,0 @@ -cd .. -export address=$1 -export exe=$2 -if [ -z "$exe" ] -then - export exe=out/oboskrnl -fi -m68k-obos-addr2line -e $exe -Cfpira $address -#m68k-obos-gdb "$exe" -batch -ex "set disassembly-flavor intel" -ex "disassemble/sr $address" | grep --color -i -C 10 -n $address -m68k-obos-objdump -d $exe -C | grep --color -i -C 10 -n ${address##0x} -cd scripts diff --git a/scripts/generate_initrd-m68k.sh b/scripts/generate_initrd-m68k.sh deleted file mode 100755 index 74f64e2c..00000000 --- a/scripts/generate_initrd-m68k.sh +++ /dev/null @@ -1,8 +0,0 @@ -export old_pwd=$PWD -cd $(git rev-parse --show-toplevel) -mkdir -p tar tar/dev tar/sys/perm -# TODO: Drivers? -cp out/init tar/ -cd tar -tar -H ustar -cf ../config/initrd.tar `ls -A` -cd $old_pwd diff --git a/scripts/generate_initrd-x86_64.sh b/scripts/generate_initrd-x86_64.sh deleted file mode 100755 index e738e3ab..00000000 --- a/scripts/generate_initrd-x86_64.sh +++ /dev/null @@ -1,17 +0,0 @@ -export old_pwd=$PWD -cd $(git rev-parse --show-toplevel) -mkdir -p tar -mkdir -p tar/dev -mkdir -p tar/sys/perm -cp out/uart tar -cp out/ahci tar -cp out/extfs tar -cp out/bochs_vbe tar -cp out/r8169 tar -cp out/i8042 tar -cp out/libps2 tar -cp out/e1000 tar -cp out/init tar -cd tar -tar -H ustar -cf ../config/initrd.tar `ls -A` -cd $old_pwd diff --git a/scripts/launch_qemu-m68k.sh b/scripts/launch_qemu-m68k.sh deleted file mode 100755 index 367b78b8..00000000 --- a/scripts/launch_qemu-m68k.sh +++ /dev/null @@ -1,19 +0,0 @@ -old_wd=$PWD -cd `git rev-parse --show-toplevel` - -rm -f qemu_log.txt - -qemu-system-m68k \ --M virt \ --kernel out/m68k_bootloader \ --cpu m68040 \ --monitor stdio \ --s -S \ --m 512M \ --d int \ --D qemu_log.txt \ --serial tcp:0.0.0.0:1534,server,nowait \ --append "--root-fs-uuid=initrd --log-level=2 --initrd-module=initrd --initrd-driver-module=initrd_driver --working-set-cap=8388608 --init-args -cignore /usr/bin/bash --login" \ --initrd "config/initrd.tar" - -cd $old_wd diff --git a/scripts/launch_qemu.bat b/scripts/launch_qemu.bat index 02da37c8..a9868941 100644 --- a/scripts/launch_qemu.bat +++ b/scripts/launch_qemu.bat @@ -1,27 +1,27 @@ -@echo off - -cd ../ - -del qemu_log.txt - -qemu-system-x86_64 ^ --drive file=out/obos.iso,format=raw ^ --m 1G ^ --gdb tcp:0.0.0.0:1234 -S ^ --accel tcg ^ --cpu "Haswell" ^ --M q35 ^ --monitor stdio ^ --debugcon file:CON ^ --serial tcp:0.0.0.0:1534,server,nowait ^ --smp cores=4,threads=1,sockets=1 ^ --d int ^ --D qemu_log.txt -rem -no-reboot ^ -rem -no-shutdown ^ -rem -drive if=pflash,format=raw,unit=1,file=ovmf/OVMF_VARS_4M.fd ^ -rem -drive if=pflash,format=raw,unit=0,file=ovmf/OVMF_CODE_4M.fd,readonly=on ^ -rem -drive id=disk2,file=disk.img,if=none,format=raw -device ide-hd,drive=disk2,bus=ahci1.1 ^ -rem -M smm=off ^ - +@echo off + +cd ../ + +del qemu_log.txt + +qemu-system-x86_64 ^ +-drive file=out/obos.iso,format=raw ^ +-m 512M ^ +-gdb tcp:0.0.0.0:1234 -S ^ +-accel tcg ^ +-cpu "Haswell" ^ +-M q35 ^ +-monitor stdio ^ +-debugcon file:CON ^ +-serial tcp:0.0.0.0:1534,server,nowait ^ +-smp cores=4,threads=1,sockets=1 ^ +-d int ^ +-D qemu_log.txt +rem -drive if=pflash,format=raw,unit=1,file=ovmf/OVMF_VARS_4M.fd ^ +rem -drive if=pflash,format=raw,unit=0,file=ovmf/OVMF_CODE_4M.fd,readonly=on ^ +rem -drive id=disk2,file=disk.img,if=none,format=raw -device ide-hd,drive=disk2,bus=ahci1.1 ^ +rem -M smm=off ^ +REM -no-shutdown ^ +REM -no-reboot ^ + cd scripts \ No newline at end of file diff --git a/scripts/launch_qemu.sh b/scripts/launch_qemu.sh index 4d00e6d1..d37b541a 100755 --- a/scripts/launch_qemu.sh +++ b/scripts/launch_qemu.sh @@ -1,27 +1,25 @@ -old_wd=$PWD -cd `git rev-parse --show-toplevel` +cd ../ -rm -f qemu_log.txt +rm qemu_log.txt -echo $@ qemu-system-x86_64 \ --drive format=raw,file=out/obos.iso,media=disk,index=0 \ --m 2G \ +-drive file=out/obos.iso,format=raw \ +-m 512M \ -gdb tcp:0.0.0.0:1234 -S \ +-cpu "Haswell" \ -M q35 \ --cpu host,+invtsc \ --accel kvm \ --debugcon file:/dev/stdout \ -monitor stdio \ +-debugcon file:/dev/stdout \ -serial tcp:0.0.0.0:1534,server,nowait \ +-smp cores=4,threads=1,sockets=1 \ -d int \ --D qemu_log.txt "$@" - -# -nographic -# -enable-kvm \ +-D qemu_log.txt \ +# -object memory-backend-file,id=mem1,share=on,mem-path=/tmp/memory.img,size=1G +# -drive if=pflash,format=raw,unit=1,file=ovmf/OVMF_VARS_4M.fd \ +# -drive if=pflash,format=raw,unit=0,file=ovmf/OVMF_CODE_4M.fd,readonly=on +# -drive id=disk2,file=disk.img,if=none,format=raw -device ide-hd,drive=disk2,bus=ahci1.1 \ # -M smm=off \ # -no-reboot -# -enable-kvm \ # -no-shutdown cd scripts diff --git a/scripts/launch_qemu.tcg.sh b/scripts/launch_qemu.tcg.sh deleted file mode 100755 index ccea75bf..00000000 --- a/scripts/launch_qemu.tcg.sh +++ /dev/null @@ -1,27 +0,0 @@ -old_wd=$PWD -cd `git rev-parse --show-toplevel` - -rm -f qemu_log.txt - -echo $@ -qemu-system-x86_64 \ --drive format=raw,file=out/obos.iso,media=disk,index=0 \ --m 2G \ --gdb tcp:0.0.0.0:1234 -S \ --M q35 \ --cpu Haswell \ --accel tcg \ --debugcon file:/dev/stdout \ --monitor stdio \ --serial tcp:0.0.0.0:1534,server,nowait \ --d int \ --D qemu_log.txt "$@" - -# -nographic -# -enable-kvm \ -# -M smm=off \ -# -no-reboot -# -enable-kvm \ -# -no-shutdown - -cd scripts diff --git a/scripts/make_bp.sh b/scripts/make_bp.sh deleted file mode 100755 index 613bfe74..00000000 --- a/scripts/make_bp.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/sh -out="for (volatile bool b = ($1); b; );" -which -s xsel -if [ $? -eq 0 ] -then - echo $out | xsel -ibo -else - echo $out -fi diff --git a/scripts/mangle.sh b/scripts/mangle.sh old mode 100755 new mode 100644 index 70edea2a..135430a5 --- a/scripts/mangle.sh +++ b/scripts/mangle.sh @@ -1 +1 @@ -echo -e $1 | x86_64-obos-g++ -ffreestanding -o /dev/stdout -xc++ -S - +echo -e $1 | x86_64-elf-g++ -ffreestanding -o /dev/stdout -xc++ -S - \ No newline at end of file diff --git a/src/build/m68k/driver_link.ld b/src/build/m68k/driver_link.ld deleted file mode 100644 index 5347fdb4..00000000 --- a/src/build/m68k/driver_link.ld +++ /dev/null @@ -1,248 +0,0 @@ -/* Borrowed from binutils */ -/* TODO(oberrow): Make your own linker script that works on the m68k-obos toolchain */ -OUTPUT_FORMAT("elf32-m68k", "elf32-m68k", - "elf32-m68k") -OUTPUT_ARCH(m68k) -ENTRY(OBOS_DriverEntry) -SEARCH_DIR("=/home/serveradmin/m68k-gcc/m68k-obos-elf-tools/m68k-obos/lib"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib"); -PHDRS -{ - headers PT_PHDR PHDRS ; - interp PT_INTERP ; - text PT_LOAD FILEHDR PHDRS ; - data PT_LOAD ; - pageable_text PT_LOAD FLAGS(PF_X|PF_R|0x00010000); - pageable_data PT_LOAD FLAGS(PF_W|PF_R|0x00010000); - dynamic PT_DYNAMIC ; -} -SECTIONS -{ - /* Read-only sections, merged into text segment: */ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0)); . = SEGMENT_START("text-segment", 0) + SIZEOF_HEADERS; - .interp : { *(.interp) } :interp :text - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } :text - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } :text - .dynstr : { *(.dynstr) } :text - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - .rela.dyn : - { - *(.rela.init) - *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) - *(.rela.fini) - *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) - *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) - *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) - *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) - *(.rela.ctors) - *(.rela.dtors) - *(.rela.got) - *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) - *(.rela.iplt) - } :text - .rela.plt : - { - *(.rela.plt) - } :text - .init : - { - KEEP (*(SORT_NONE(.init))) - } =0x4e714e71 - .plt : { *(.plt) } :text - .iplt : { *(.iplt) } :text - .text : - { - *(.text.unlikely .text.*_unlikely .text.unlikely.*) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(SORT(.text.sorted.*)) - *(.text .stub .text.* .gnu.linkonce.t.*) - /* .gnu.warning sections are handled specially by elf.em. */ - *(.gnu.warning) - } :text =0x4e714e71 - .fini : - { - KEEP (*(SORT_NONE(.fini))) - } =0x4e714e71 - PROVIDE (__etext = .); - PROVIDE (_etext = .); - PROVIDE (etext = .); - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } :text - .rodata1 : { *(.rodata1) } :text - .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } - .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .sframe : ONLY_IF_RO { *(.sframe) *(.sframe.*) } - .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } - .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } - /* These sections are generated by the Sun/Oracle C++ compiler. */ - .exception_ranges : ONLY_IF_RO { *(.exception_ranges*) } - /* Adjust the address for the data segment. We want to adjust up to - the same address within the page on the next page up. */ - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - /* Exception handling */ - .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .sframe : ONLY_IF_RW { *(.sframe) *(.sframe.*) } - .gnu_extab : ONLY_IF_RW { *(.gnu_extab) } - .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } - .exception_ranges : ONLY_IF_RW { *(.exception_ranges*) } - /* Thread Local Storage sections */ - .tdata : - { - PROVIDE_HIDDEN (__tdata_start = .); - *(.tdata .tdata.* .gnu.linkonce.td.*) - } :data - .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }:data - .preinit_array : - { - PROVIDE_HIDDEN (__preinit_array_start = .); - KEEP (*(.preinit_array)) - PROVIDE_HIDDEN (__preinit_array_end = .); - }:data - .init_array : - { - PROVIDE_HIDDEN (__init_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) - KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) - PROVIDE_HIDDEN (__init_array_end = .); - }:data - .fini_array : - { - PROVIDE_HIDDEN (__fini_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) - KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) - PROVIDE_HIDDEN (__fini_array_end = .); - }:data - .ctors : - { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) - KEEP (*(SORT(.ctors.*))) - KEEP (*(.ctors)) - } :data - .dtors : - { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } :data - .jcr : { KEEP (*(.jcr)) } - .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) } - .dynamic : { *(.dynamic) } :data :dynamic - . = DATA_SEGMENT_RELRO_END (0, .); - .got : { *(.got.plt) *(.igot.plt) *(.got) *(.igot) } :data - .data : - { - *(.data .data.* .gnu.linkonce.d.*) - SORT(CONSTRUCTORS) - } :data - .data1 : { *(.data1) } :data - _edata = .; PROVIDE (edata = .); - . = ALIGN(ALIGNOF(NEXT_SECTION)); - __bss_start = .; - .bss : - { - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we do not - pad the .data section. */ - . = ALIGN(. != 0 ? 32 / 8 : 1); - } :data - . = ALIGN(32 / 8); - . = SEGMENT_START("ldata-segment", .); - . = ALIGN(32 / 8); - .pageable.text : { - *(.pageable.text .pageable.text.*) - } :pageable_text - .pageable.rodata : { - *(.pageable.rodata .pageable.rodata.*) - } :pageable_text - .pageable.data : { - *(.pageable.data .pageable.data.*) - } :pageable_data - .pageable.bss : - { - *(.pageable.bss .pageable.bss.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we do not - pad the .data section. */ - . = ALIGN(. != 0 ? 32 / 8 : 1); - } :pageable_data - . = ALIGN(32 / 8); - _end = .; PROVIDE (end = .); - . = DATA_SEGMENT_END (.); - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 (INFO) : { *(.comment); LINKER_VERSION; } - .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1. */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions. */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2. */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2. */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions. */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } - /* DWARF 3. */ - .debug_pubtypes 0 : { *(.debug_pubtypes) } - .debug_ranges 0 : { *(.debug_ranges) } - /* DWARF 5. */ - .debug_addr 0 : { *(.debug_addr) } - .debug_line_str 0 : { *(.debug_line_str) } - .debug_loclists 0 : { *(.debug_loclists) } - .debug_macro 0 : { *(.debug_macro) } - .debug_names 0 : { *(.debug_names) } - .debug_rnglists 0 : { *(.debug_rnglists) } - .debug_str_offsets 0 : { *(.debug_str_offsets) } - .debug_sup 0 : { *(.debug_sup) } - .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } - /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } -} \ No newline at end of file diff --git a/src/build/m68k/link.ld b/src/build/m68k/link.ld deleted file mode 100644 index 588191e3..00000000 --- a/src/build/m68k/link.ld +++ /dev/null @@ -1,72 +0,0 @@ -OUTPUT_FORMAT(elf32-m68k) - -ENTRY(Arch_KernelEntryBootstrap) - -PHDRS -{ - text PT_LOAD FLAGS((1 << 0) | (1 << 2)) ; /* Execute + Read */ - rodata PT_LOAD FLAGS((1 << 2)) ; /* Read only */ - data PT_LOAD FLAGS((1 << 1) | (1 << 2)) ; /* Write + Read */ - - pageable_text PT_LOAD FLAGS((1 << 0) | (1 << 2)); /* Read + Execute */ - pageable_data PT_LOAD FLAGS((1 << 1) | (1 << 2)); /* Read + Write */ - pageable_rodata PT_LOAD FLAGS((1 << 2)); /* Read only */ -} - -SECTIONS -{ - . = 0xC0000000; - KERNEL_BASE = .; - - .text : { - *(.text .text.*) - } :text - - . = ALIGN(CONSTANT(MAXPAGESIZE)); - - - .rodata : { - *(.rodata .rodata.*) - } :rodata - - . = ALIGN(CONSTANT(MAXPAGESIZE)); - - .data : { - *(.data .data.*) - } :data - - .bss : { - *(.bss .bss.*) - *(COMMON) - } :data - - . = ALIGN(CONSTANT(MAXPAGESIZE)); - - MmS_MMPageableRangeStart = .; - /* Functions */ - .pageable.text : { - *(.pageable.text) - } :pageable_text - . = ALIGN(CONSTANT(MAXPAGESIZE)); - /* RW Data. */ - .pageable.data : { - *(.pageable.data) - } :pageable_data - . = ALIGN(CONSTANT(MAXPAGESIZE)); - .pageable.bss : { - *(.pageable.bss) - } :pageable_data - /* RO Data */ - .pageable.rodata : { - *(.pageable.rodata) - } :pageable_rodata - . = ALIGN(CONSTANT(MAXPAGESIZE)); - MmS_MMPageableRangeEnd = .; - - /DISCARD/ : { - /* *(.eh_frame*) */ - *(.note .note.*) - *(.comment) - } - KERNEL_TOP = .; -} \ No newline at end of file diff --git a/src/build/m68k/toolchain.cmake b/src/build/m68k/toolchain.cmake deleted file mode 100644 index f16a0d27..00000000 --- a/src/build/m68k/toolchain.cmake +++ /dev/null @@ -1,92 +0,0 @@ -set(CMAKE_SYSTEM_NAME "Generic") -set(CMAKE_SYSTEM_PROCESSOR "m68k") - -find_program(HAS_CROSS_COMPILER "m68k-obos-g++") -if (NOT HAS_CROSS_COMPILER) - message(FATAL_ERROR "No m68k-obos cross compiler in the PATH!") -endif() - -if (OBOS_USE_CLANG) - message(WARNING "OBOS does not support m68k clang") -endif() - -set(CMAKE_C_COMPILER "m68k-obos-gcc") -set(CMAKE_CXX_COMPILER "m68k-obos-g++") -set(CMAKE_ASM-ATT_COMPILER ${CMAKE_C_COMPILER}) -set(CMAKE_C_COMPILER_WORKS true) -set(CMAKE_CXX_COMPILER_WORKS true) - -set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) -set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) - -set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) - -execute_process(COMMAND m68k-obos-gcc -print-file-name=libgcc.a OUTPUT_VARIABLE LIBGCC_TARGET) - -string(STRIP "${LIBGCC_TARGET}" LIBGCC_TARGET) - -find_program(HAS_m68k_elf_objcopy "m68k-obos-objcopy") -find_program(HAS_m68k_elf_nm "m68k-obos-nm") -if (HAS_m68k_objcopy) - set(OBJCOPY "m68k-obos-objcopy") -else() - set(OBJCOPY "objcopy") -endif() -if (HAS_x86_64_elf_nm) - set(NM "m68k-obos-nm") -else() - set(NM "nm") -endif() -set(TARGET_COMPILE_OPTIONS_C -fno-omit-frame-pointer -msoft-float) -set(TARGET_DRIVER_COMPILE_OPTIONS_C -fno-omit-frame-pointer -msoft-float) -set(TARGET_LINKER_OPTIONS -z max-page-size=4096) -set(TARGET_DRIVER_LINKER_OPTIONS -z max-page-size=4096) - -if (OBOS_ENABLE_KASAN) - add_compile_options($<$:-fasan-shadow-offset=0>) -endif() - -add_compile_options("-mcpu=68040") -add_link_options("-mcpu=68040") - -list (APPEND oboskrnl_sources - "arch/m68k/entry.c" "arch/m68k/asm_helpers.S" "arch/m68k/irql.c" "arch/m68k/driver_loader.c" - "arch/m68k/irq.c" "arch/m68k/isr.S" "arch/m68k/thread_ctx.S" "arch/m68k/thread_ctx.c" - "arch/m68k/mmu.c" "arch/m68k/pmm.c" "arch/m68k/exception_handlers.c" "arch/m68k/goldfish_rtc.c" - "arch/m68k/goldfish_pic.c" "arch/m68k/gf_tty.c" "arch/m68k/syscall.c" "arch/m68k/execve.c" -) - -add_compile_definitions( - OBOS_PAGE_SIZE=4096 - # OBOS_HUGE_PAGE_SIZE=8192 - OBOS_HUGE_PAGE_SIZE=4096 - OBOS_KERNEL_ADDRESS_SPACE_BASE=0xc0000000 OBOS_KERNEL_ADDRESS_SPACE_LIMIT=0xfffff000 - OBOS_USER_ADDRESS_SPACE_BASE=0x1000 OBOS_USER_ADDRESS_SPACE_LIMIT=0x80000000 - OBOS_TIMER_IS_DEADLINE= - OBOS_ARCH_USES_SOFT_FLOAT=1 - OBOS_ARCH_EMULATED_IRQL=1 - OBOS_ARCH_HAS_USR_MEMCPY=0 - OBOS_ARCH_HAS_MEMSET=0 - OBOS_ARCH_HAS_MEMZERO=0 - OBOS_ARCH_HAS_MEMCPY=0 - OBOS_ARCH_HAS_MEMCMP=0 - OBOS_ARCH_HAS_MEMCMP_B=0 - OBOS_ARCH_HAS_STRCMP=0 - OBOS_ARCH_HAS_STRNCMP=0 - OBOS_ARCH_HAS_STRLEN=0 - OBOS_ARCH_HAS_STRNLEN=0 - OBOS_ARCH_HAS_STRCHR=0 - OBOS_ARCH_HAS_STRNCHR=0 -) -add_compile_options($<$:-g>) - -set (OBOS_ARCHITECTURE "m68k") -set (OBOS_ARCHITECTURE_BITS 32) # 32-bit architecture. -set (OBOS_ARCHITECTURE_ENDIANNESS "Big-Endian") # Big-Endian architecture. -set (LINKER_SCRIPT "${CMAKE_SOURCE_DIR}/src/build/m68k/link.ld") -set (DRIVER_LINKER_SCRIPT "${CMAKE_SOURCE_DIR}/src/build/m68k/driver_link.ld") -set (OBOS_ARCHITECTURE_HAS_ACPI 0) -set (OBOS_ARCHITECTURE_HAS_PCI 0) -set (OBOS_IRQL_COUNT 8) -set (OBOS_UP 1) # The m68k doesn't support SMP, so we disable it as a [POSSIBLE] optimization -set (OBOS_ENABLE_UHDA 0) \ No newline at end of file diff --git a/src/build/x86_64/driver_link.ld b/src/build/x86_64/driver_link.ld deleted file mode 100644 index d62f3bf4..00000000 --- a/src/build/x86_64/driver_link.ld +++ /dev/null @@ -1,83 +0,0 @@ -OUTPUT_FORMAT(elf64-x86-64) -OUTPUT_ARCH(i386:x86-64) - -ENTRY(OBOS_DriverEntry) - -PHDRS -{ - headers PT_PHDR PHDRS; - text PT_LOAD FILEHDR PHDRS FLAGS((1 << 0) | (1 << 2)) ; /* Execute + Read */ - rodata PT_LOAD FLAGS((1 << 2)) ; /* Read only */ - data PT_LOAD FLAGS((1 << 1) | (1 << 2)) ; /* Write + Read */ - -/* 0x00010000=PF_OBOS_PAGEABLE */ - pageable_text PT_LOAD FLAGS((1 << 0) | (1 << 2) | 0x00010000); /* Read + Execute + Pageable */ - pageable_data PT_LOAD FLAGS((1 << 1) | (1 << 2) | 0x00010000); /* Read + Write + Pageable */ - pageable_rodata PT_LOAD FLAGS((1 << 2) | 0x00010000); /* Read only + Pageable */ - - dynamic PT_DYNAMIC; -} - - -SECTIONS -{ - Drv_Base = .; - . = 0x0000000000000000 + SIZEOF_HEADERS; - - .text : { - *(.text .text.*) - } :text - - . = ALIGN(CONSTANT(MAXPAGESIZE)); - - .rodata : { - *(.rodata .rodata.*) - } :rodata - - . = ALIGN(CONSTANT(MAXPAGESIZE)); - - .data : { - *(.data .data.*) - *(.driverheader) - } :data - - .bss : { - *(.bss .bss.*) - *(COMMON) - } :data - - . = ALIGN(CONSTANT(MAXPAGESIZE)); - - /* Functions */ - .pageable.text : { - *(.pageable.text) - } :pageable_text - . = ALIGN(CONSTANT(MAXPAGESIZE)); - /* RW Data. */ - .pageable.data : { - *(.pageable.data) - } :pageable_data - . = ALIGN(CONSTANT(MAXPAGESIZE)); - .pageable.bss : { - *(.pageable.bss) - } :pageable_data - . = ALIGN(CONSTANT(MAXPAGESIZE)); - /* RO Data */ - .pageable.rodata : { - *(.pageable.rodata) - } :pageable_rodata - . = ALIGN(CONSTANT(MAXPAGESIZE)); - - /DISCARD/ : { - *(.eh_frame) - *(.note .note.*) - *(.comment) - } - - . = ALIGN(CONSTANT(MAXPAGESIZE)); - .dynamic : { - *(.dynamic) - } :data :dynamic - - Drv_Top = .; -} diff --git a/src/build/x86_64/link.ld b/src/build/x86_64/link.ld deleted file mode 100644 index a9f8ec96..00000000 --- a/src/build/x86_64/link.ld +++ /dev/null @@ -1,73 +0,0 @@ -OUTPUT_FORMAT(elf64-x86-64) -OUTPUT_ARCH(i386:x86-64) - -ENTRY(Arch_KernelEntryBootstrap) - -PHDRS -{ - text PT_LOAD FLAGS((1 << 0) | (1 << 2)) ; /* Execute + Read */ - rodata PT_LOAD FLAGS((1 << 2)) ; /* Read only */ - data PT_LOAD FLAGS((1 << 1) | (1 << 2)) ; /* Write + Read */ - - pageable_text PT_LOAD FLAGS((1 << 0) | (1 << 2)); /* Read + Execute */ - pageable_data PT_LOAD FLAGS((1 << 1) | (1 << 2)); /* Read + Write */ - pageable_rodata PT_LOAD FLAGS((1 << 2)); /* Read only */ -} - -SECTIONS -{ - . = 0xffffffff80000000; - KERNEL_BASE = .; - - MmS_MMPageableRangeStart = .; - /* Functions */ - .pageable.text : { - *(.pageable.text) - } :pageable_text - . = ALIGN(CONSTANT(MAXPAGESIZE)); - /* RW Data. */ - .pageable.data : { - *(.pageable.data) - } :pageable_data - . = ALIGN(CONSTANT(MAXPAGESIZE)); - .pageable.bss : { - *(.pageable.bss) - } :pageable_data - /* RO Data */ - .pageable.rodata : { - *(.pageable.rodata) - } :pageable_rodata - . = ALIGN(CONSTANT(MAXPAGESIZE)); - MmS_MMPageableRangeEnd = .; - - .text : { - *(.text .text.*) - } :text - - . = ALIGN(CONSTANT(MAXPAGESIZE)); - - - .rodata : { - *(.rodata .rodata.*) - } :rodata - - . = ALIGN(CONSTANT(MAXPAGESIZE)); - - .data : { - *(.data .data.*) - } :data - - .bss : { - *(.bss .bss.*) - *(COMMON) - } :data - - . = ALIGN(CONSTANT(MAXPAGESIZE)); - - /DISCARD/ : { - *(.eh_frame) - *(.note .note.*) - *(.comment) - } - KERNEL_TOP = .; -} diff --git a/src/build/x86_64/toolchain.cmake b/src/build/x86_64/toolchain.cmake deleted file mode 100644 index 99957fe5..00000000 --- a/src/build/x86_64/toolchain.cmake +++ /dev/null @@ -1,141 +0,0 @@ -set(CMAKE_SYSTEM_NAME "Linux") -set(CMAKE_SYSTEM_PROCESSOR "x86-64") - -if (NOT OBOS_USE_CLANG) - if (NOT OBOS_USE_OBOS_GCC) - set(CMAKE_C_COMPILER "x86_64-elf-gcc") - set(CMAKE_CXX_COMPILER "x86_64-elf-g++") - else() - set(CMAKE_C_COMPILER "x86_64-obos-gcc") - set(CMAKE_CXX_COMPILER "x86_64-obos-g++") - endif() - - find_program(HAS_CROSS_COMPILER ${CMAKE_C_COMPILER}) - if (NOT HAS_CROSS_COMPILER) - message(FATAL_ERROR "No ${CMAKE_C_COMPILER} cross compiler in the PATH!") - endif() -else() - set (CMAKE_C_COMPILER "clang${OBOS_CLANG_SUFFIX}") - set (CMAKE_CXX_COMPILER "clang++${OBOS_CLANG_SUFFIX}") - - add_compile_options( - $<$:-target> - $<$:x86_64-unknown-linux-elf> - $<$:-mstack-protector-guard-offset=552> - $<$:-mstack-protector-guard-reg=gs> - ) - - add_link_options("-static" "-target" "x86_64-unknown-linux-elf" "-ffreestanding") - - if (CMAKE_HOST_UNIX) - set (LLD "ld.lld${OBOS_CLANG_SUFFIX}") - elseif(CMAKE_HOST_WIN32) - set (LLD "lld-link${OBOS_CLANG_SUFFIX}") - endif() - - find_program(HAS_LLD ${LLD}) - if (HAS_LLD) - add_link_options("-fuse-ld=lld") - endif() -endif() - -set(CMAKE_C_COMPILER_FORCED true) -set(CMAKE_C_COMPILER_WORKS true) -set(CMAKE_CXX_COMPILER_FORCED true) -set(CMAKE_CXX_COMPILER_WORKS true) - -set(CMAKE_ASM_NASM_COMPILER "nasm") -set(CMAKE_ASM_NASM_COMPILER_WORKS true) -set(CMAKE_ASM_NASM_COMPILER_FORCED true) - -set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) -set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) - -set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) - -set(CMAKE_ASM_NASM_OBJECT_FORMAT "elf64") - -execute_process(COMMAND ${CMAKE_C_COMPILER} -print-sysroot OUTPUT_VARIABLE CMAKE_FIND_ROOT_PATH) -execute_process(COMMAND ${CMAKE_C_COMPILER} -print-sysroot OUTPUT_VARIABLE CMAKE_MODULE_PATH) - -if (NOT OBOS_USE_CLANG) - execute_process(COMMAND ${CMAKE_C_COMPILER} -print-file-name=no-red-zone/libgcc.a OUTPUT_VARIABLE LIBGCC_TARGET) - string(STRIP "${LIBGCC_TARGET}" LIBGCC_TARGET) - if ("${LIBGCC_TARGET}" STREQUAL "no-red-zone/libgcc.a") - # Use normal libgcc - execute_process(COMMAND ${CMAKE_C_COMPILER} -print-file-name=libgcc.a OUTPUT_VARIABLE LIBGCC_TARGET) - endif() - - string(STRIP "${LIBGCC_TARGET}" LIBGCC_TARGET) - set (OBJCOPY "objcopy") -else() - set (OBJCOPY "llvm-objcopy${OBOS_CLANG_SUFFIX}") - - if (NOT EXISTS libgcc-x86_64.a) - file(DOWNLOAD https://github.com/osdev0/libgcc-binaries/releases/download/2025-12-08/libgcc-x86_64.a libgcc-x86_64.a) - endif() - file(REAL_PATH libgcc-x86_64.a LIBGCC_TARGET BASE_DIRECTORY .) -endif() - - -set(TARGET_COMPILE_OPTIONS_C -mno-red-zone -mgeneral-regs-only -mcmodel=kernel -march=x86-64-v3) -set(TARGET_DRIVER_COMPILE_OPTIONS_C -mno-red-zone -mgeneral-regs-only -march=x86-64-v3) -set(TARGET_LINKER_OPTIONS -mcmodel=kernel) -set(TARGET_DRIVER_LINKER_OPTIONS) - -set(gdbstub_source - "arch/x86_64/gdbstub/connection.c" "arch/x86_64/gdbstub/alloc.c" "arch/x86_64/gdbstub/packet_dispatcher.c" - "arch/x86_64/gdbstub/general_query.c" "arch/x86_64/gdbstub/debug.c" "arch/x86_64/gdbstub/stop_reply.c" "arch/x86_64/gdbstub/bp.c" - "arch/x86_64/gdbstub/gdb_udp_backend.c" "arch/x86_64/gdbstub/vFile.c" -) - -list (APPEND oboskrnl_sources - "arch/x86_64/entry.asm" "arch/x86_64/entry.c" "arch/x86_64/bgdt.asm" "arch/x86_64/idt.c" - "arch/x86_64/asm_helpers.asm" "arch/x86_64/thread_ctx.asm" "arch/x86_64/memmanip.asm" - "arch/x86_64/pmm.c" "arch/x86_64/map.c" "arch/x86_64/isr.asm" "arch/x86_64/lapic.c" - "arch/x86_64/smp.c" "arch/x86_64/smp.asm" "arch/x86_64/lapic_timer_calibration.asm" - "arch/x86_64/ioapic.c" "arch/x86_64/drv_loader.c" "arch/x86_64/ssignal.c" "arch/x86_64/except.c" - "arch/x86_64/pci.c" "arch/x86_64/syscall.c" "arch/x86_64/syscall.asm" "arch/x86_64/wake.c" - "arch/x86_64/mtrr.c" "arch/x86_64/timer.c" "arch/x86_64/execve.c" "arch/x86_64/sse.c" - "arch/x86_64/cmos.c" - ${gdbstub_source} -) - -# set_source_files_properties( -# ${gdbstub_source} -# PROPERTIES -# COMPILE_OPTIONS "-fno-sanitize=undefined" -# COMPILE_OPTIONS "-fno-sanitize=kernel-address" -# ) - -add_compile_definitions( - __x86_64__=1 - OBOS_PAGE_SIZE=4096 OBOS_HUGE_PAGE_SIZE=2097152 - OBOS_KERNEL_ADDRESS_SPACE_BASE=0xffff900000000000 OBOS_KERNEL_ADDRESS_SPACE_LIMIT=0xfffffffffffff000 - # Set this limit to mitigate an intel silicon bug - OBOS_USER_ADDRESS_SPACE_BASE=0x1000 OBOS_USER_ADDRESS_SPACE_LIMIT=0x7FFFFFFFE000 - OBOS_ARCH_USES_SOFT_FLOAT=0 - OBOS_ARCH_EMULATED_IRQL=0 - OBOS_ARCH_HAS_USR_MEMCPY=0 - OBOS_ARCH_HAS_MEMSET=1 - OBOS_ARCH_HAS_MEMZERO=1 - OBOS_ARCH_HAS_MEMCPY=1 - OBOS_ARCH_HAS_MEMCMP=1 - OBOS_ARCH_HAS_MEMCMP_B=1 - OBOS_ARCH_HAS_STRCMP=1 - OBOS_ARCH_HAS_STRNCMP=0 - OBOS_ARCH_HAS_STRLEN=1 - OBOS_ARCH_HAS_STRNLEN=0 - OBOS_ARCH_HAS_STRCHR=1 - OBOS_ARCH_HAS_STRNCHR=0 -) -add_compile_options($<$:-g>) - -set (OBOS_ARCHITECTURE "x86_64") -set (OBOS_ARCHITECTURE_BITS 64) # 64-bit architecture. -set (OBOS_ARCHITECTURE_ENDIANNESS "Little-Endian") # Little-Endian architecture. -set (LINKER_SCRIPT "${CMAKE_SOURCE_DIR}/src/build/x86_64/link.ld") -set (DRIVER_LINKER_SCRIPT "${CMAKE_SOURCE_DIR}/src/build/x86_64/driver_link.ld") -set (OBOS_ARCHITECTURE_HAS_ACPI 1) -set (OBOS_ARCHITECTURE_HAS_PCI 1) -set (OBOS_IRQL_COUNT 16) diff --git a/src/drivers/generic/ahci/CMakeLists.txt b/src/drivers/generic/ahci/CMakeLists.txt deleted file mode 100644 index 5e6f13f3..00000000 --- a/src/drivers/generic/ahci/CMakeLists.txt +++ /dev/null @@ -1,32 +0,0 @@ -# drivers/generic/ahci/CMakeLists.txt -# -# Copyright (c) 2024 Omar Berrow - -add_executable(ahci "main.c" "ahci_irq.c" "command.c" "interface.c") - -target_compile_options(ahci - PRIVATE $<$:${TARGET_DRIVER_COMPILE_OPTIONS_C}> - PRIVATE $<$:-ffreestanding> - PRIVATE $<$:-Wall> - PRIVATE $<$:-Wextra> - PRIVATE $<$:-fstack-protector-all> - PRIVATE $<$:-fno-builtin-memset> - PRIVATE $<$:-fvisibility=hidden> - PRIVATE $<$:-fPIC> -) - -set_property(TARGET ahci PROPERTY link_depends ${DRIVER_LINKER_SCRIPT}) - -target_include_directories(ahci - PRIVATE "${CMAKE_SOURCE_DIR}/src/oboskrnl" - PRIVATE ${OBOSKRNL_EXTERNAL_INCLUDES}) - -target_link_options(ahci - PRIVATE "-nostdlib" - PRIVATE "-fPIC" - PRIVATE "-Wl,-shared" -# PRIVATE "-Wl,--allow-shlib-undefined" - PRIVATE "-T" PRIVATE ${DRIVER_LINKER_SCRIPT} - PRIVATE ${TARGET_DRIVER_LINKER_OPTIONS} -) -target_compile_definitions(ahci PRIVATE OBOS_DRIVER=1) diff --git a/src/drivers/generic/ahci/ahci_irq.c b/src/drivers/generic/ahci/ahci_irq.c deleted file mode 100644 index 20eb57ad..00000000 --- a/src/drivers/generic/ahci/ahci_irq.c +++ /dev/null @@ -1,107 +0,0 @@ -/* - * drivers/generic/ahci/ahci_irq.c - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include -#include -#include - -#include -#include -#include - -#include - -#include -#include - -#include - -#include "ahci_irq.h" -#include "structs.h" -#include "command.h" - -// static dpc ahci_dpc; -static void ahci_dpc_handler(dpc* d, void* userdata) -{ - OBOS_UNUSED(d); - OBOS_UNUSED(userdata); - Port* curr = (Port*)userdata; - for (uint8_t slot = 0; slot < (((HBA->cap >> 8) & 0b11111)+1); slot++) - { - if (!curr->PendingCommands[slot]) - continue; // There was never a command issued in the first place. - if (curr->PendingCommands[slot]->awaitingSignal) - { - Core_SemaphoreRelease(&curr->lock); - Core_EventSet(&curr->PendingCommands[slot]->completionEvent, false); - curr->PendingCommands[slot]->awaitingSignal = false; - curr->CommandBitmask &= ~BIT(slot); - curr->PendingCommands[slot] = nullptr; - } - } -} -OBOS_NO_KASAN OBOS_NO_UBSAN bool ahci_irq_checker(struct irq* i, void* userdata) -{ - OBOS_UNUSED(i); - OBOS_UNUSED(userdata); - // OBOS_Debug("bonjour HBA->is: 0x%08x\n", HBA->is); - return HBA->is != 0; -} -irq HbaIrq; -OBOS_NO_KASAN OBOS_NO_UBSAN void ahci_irq_handler(struct irq* i, interrupt_frame* frame, void* userdata, irql oldIrql) -{ - OBOS_UNUSED(i); - OBOS_UNUSED(frame); - OBOS_UNUSED(userdata); - OBOS_UNUSED(oldIrql); - // OBOS_Debug("hola\n"); - for (uint8_t port = 0; port < PortCount; port++) - { - Port* curr = &Ports[port]; - if (!(HBA->is & BIT(curr->hbaPortIndex))) - continue; - uint32_t portStatus = HBA->ports[curr->hbaPortIndex].is; - if (!curr->works) - { - HBA->ports[curr->hbaPortIndex].is = portStatus; - continue; - } - // Signal each finished event here. - obos_status status = OBOS_STATUS_SUCCESS; - if (portStatus & (0xFD800000)) - { - // Some command failed. - // (How sad) - // Signal all commands with OBOS_STATUS_RETRY. - - status = OBOS_STATUS_RETRY; - } - bool requires_dpc = false; - for (uint8_t slot = 0; slot < (((HBA->cap >> 8) & 0b11111)+1); slot++) - { - if ((HBA->ports[curr->hbaPortIndex].ci & BIT(slot)) && status == OBOS_STATUS_SUCCESS) - continue; - if (!curr->PendingCommands[slot]) - continue; // There was never a command issued in the first place. - if (curr->PendingCommands[slot]->awaitingSignal) - continue; - // // TODO: Is this valid? - // if (curr->PendingCommands[slot]->irp) - // curr->PendingCommands[slot]->irp->nBlkRead = (((HBA_CMD_HEADER*)curr->clBase) + slot)->prdbc / curr->sectorSize; - curr->PendingCommands[slot]->awaitingSignal = true; - requires_dpc = true; - curr->PendingCommands[slot]->commandStatus = status; - } - if (requires_dpc) - { - curr->port_dpc.userdata = curr; - CoreH_InitializeDPC(&curr->port_dpc, ahci_dpc_handler, Core_DefaultThreadAffinity); - } - HBA->ports[curr->hbaPortIndex].is = portStatus; - } - // Set HBA->is to itself to reset it. - HBA->is = HBA->is; -} diff --git a/src/drivers/generic/ahci/ahci_irq.h b/src/drivers/generic/ahci/ahci_irq.h deleted file mode 100644 index 6fadc9e3..00000000 --- a/src/drivers/generic/ahci/ahci_irq.h +++ /dev/null @@ -1,18 +0,0 @@ -/* - * drivers/generic/ahci/ahci_irq.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include - -#include -#include - -#include - -bool ahci_irq_checker(struct irq* i, void* userdata); -void ahci_irq_handler(struct irq* i, interrupt_frame* frame, void* userdata, irql oldIrql); -extern pci_resource* PCIIrqResource; diff --git a/src/drivers/generic/ahci/command.c b/src/drivers/generic/ahci/command.c deleted file mode 100644 index 21e40489..00000000 --- a/src/drivers/generic/ahci/command.c +++ /dev/null @@ -1,168 +0,0 @@ -/* - * drivers/generic/ahci/command.c - * - * Copyright (c) 2024-2026 Omar Berrow -*/ - -#include -#include -#include -#include - -#include -#include - -#include - -#include - -#include "structs.h" -#include "command.h" - -static _Atomic(bool) transactions_halted = false; -obos_status SendCommand(Port* port, struct command_data* data, uint64_t lba, uint8_t device, uint16_t count) -{ - if (!port || !data) - return OBOS_STATUS_INVALID_ARGUMENT; - if (data->physRegionCount > sizeof(((HBA_CMD_TBL*)nullptr))->prdt_entry/sizeof(HBA_PRDT_ENTRY)) - return OBOS_STATUS_INVALID_ARGUMENT; - if (transactions_halted) - return OBOS_STATUS_RETRY; - // StopCommandEngine(&HBA->ports[port->hbaPortIndex]); - Core_SemaphoreAcquire(&port->lock); - HBA->ports[port->hbaPortIndex].is = 0xffffffff; - Core_MutexAcquire(&port->bitmask_lock); - uint32_t cmdSlot = __builtin_ctz(~port->CommandBitmask); - data->internal.cmdSlot = cmdSlot; - port->CommandBitmask |= BIT(cmdSlot); - port->PendingCommands[cmdSlot] = data; - Core_MutexRelease(&port->bitmask_lock); - obos_status status = OBOS_STATUS_SUCCESS; - volatile HBA_CMD_HEADER* cmdHeader = ((HBA_CMD_HEADER*)port->clBase) + cmdSlot; - cmdHeader->b0 |= ((sizeof(FIS_REG_H2D) / sizeof(uint32_t)) & 0x1f) << 0; - if (data->direction == COMMAND_DIRECTION_READ) - cmdHeader->b0 &= ~BIT(6); // Device to Host. - else - cmdHeader->b0 |= BIT(6); // Host to Device. - // cmdHeader->b1 |= BIT(2); -#if OBOS_ARCHITECTURE_BITS == 64 - volatile HBA_CMD_TBL* cmdTBL = - (HBA_CMD_TBL*)(uintptr_t) - ( - (uintptr_t)port->clBase + - ((cmdHeader->ctba | ((uint64_t)cmdHeader->ctbau << 32)) - port->clBasePhys) // The offset of the HBA_CMD_TBL - ); -#else - HBA_CMD_TBL* cmdTBL = - (HBA_CMD_TBL*)(uintptr_t) - ( - (uintptr_t)port->clBase + - ((cmdHeader->ctba - port->clBasePhys)) // The offset of the HBA_CMD_TBL - ); -#endif - memzero((void*)cmdTBL, sizeof(*cmdTBL)); - for (uint16_t i = 0; i < data->physRegionCount; i++) - { - #if OBOS_ARCHITECTURE_BITS == 64 - if (!(HBA->cap & BIT(31))) - OBOS_ASSERT(!(data->phys_regions[i].phys >> 32)); - #endif - memzero((void*)&cmdTBL->prdt_entry[i], sizeof(cmdTBL->prdt_entry[i])); - AHCISetAddress(data->phys_regions[i].phys, cmdTBL->prdt_entry[i].dba); - uint32_t dw4 = ((data->phys_regions[i].sz - 1) & 0x3fffff) << 0; - // cmdTBL->prdt_entry[i].i = (i == (data->physRegionCount - 1)); - // cmdTBL->prdt_entry[i].dw4 &= ~BIT(31); - dw4 |= BIT(31); - cmdTBL->prdt_entry[i].dw4 = dw4; - } - cmdHeader->prdtl = data->physRegionCount; - FIS_REG_H2D* fis = (void*)cmdTBL->cfis; - memzero(fis, sizeof(*fis)); - fis->fis_type = FIS_TYPE_REG_H2D; - fis->b1 |= BIT(7); - fis->command = data->cmd; - - fis->lba0 = (lba & 0xff); - fis->lba1 = (lba >> 8) & 0xff; - fis->lba2 = (lba >> 16) & 0xff; - fis->device = device; - - fis->lba3 = (lba >> 24) & 0xff; - fis->lba4 = (lba >> 32) & 0xff; - fis->lba5 = (lba >> 40) & 0xff; - - fis->countl = count & 0xff; - fis->counth = count >> 8; - // Wait for the port. - // 0x88: ATA_DEV_BUSY | ATA_DEV_DRQ - while ((HBA->ports[port->hbaPortIndex].tfd & 0x88)) - OBOSS_SpinlockHint(); - // Issue the command - data->internal.cmdSlot = cmdSlot; - // HBA->ports[port->hbaPortIndex].sact |= (1 << cmdSlot); - // StartCommandEngine(&HBA->ports[port->hbaPortIndex]); - HBA->ports[port->hbaPortIndex].ci |= (1 << cmdSlot); - // Release the semaphore in the IRQ handler instead. - // Core_SemaphoreRelease(&port->lock); - return status; -} -obos_status ClearCommand(Port* port, struct command_data* data) -{ - uint8_t cmdSlot = data->internal.cmdSlot; - HBA->ports[port->hbaPortIndex].ci &= ~(1 << cmdSlot); - port->PendingCommands[cmdSlot] = nullptr; - return OBOS_STATUS_SUCCESS; -} -void StopCommandEngine(volatile HBA_PORT* hPort) -{ - // PxCMD.ST: Bit 0 - // PxCMD.FRE: Bit 4 - // PxCMD.FR: Bit 14 - // PxCMD.CR: Bit 15 - if (!(hPort->cmd & (BIT(0) | BIT(4) | BIT(14) | BIT(15)))) - return; - // The DMA engine is running. - // Disable it. - hPort->cmd &= ~(1<<0); - timer_tick deadline = CoreS_GetTimerTick() + CoreH_TimeFrameToTick(3000000 /* 1s */); - while(hPort->cmd & (1<<15) /*PxCMD.CR*/ && CoreS_GetTimerTick() < deadline) - OBOSS_SpinlockHint(); - if (hPort->cmd & (1<<15) /*PxCMD.CR*/) - OBOS_Panic(OBOS_PANIC_DRIVER_FAILURE, "Port did not go idle after 3 seconds (PxCMD.CR=1). PxCMD: 0x%08x\n", hPort->cmd); - hPort->cmd &= ~(1<<4); - deadline = CoreS_GetTimerTick() + CoreH_TimeFrameToTick(3000000); - while(hPort->cmd & (1<<14) /*PxCMD.FR*/ && CoreS_GetTimerTick() < deadline) - OBOSS_SpinlockHint(); - if (hPort->cmd & (1<<14) /*PxCMD.FR*/) - OBOS_Panic(OBOS_PANIC_DRIVER_FAILURE, "Port did not go idle after 3 seconds (PxCMD.FR=1). PxCMD: 0x%08x\n", hPort->cmd); -} -void StartCommandEngine(volatile HBA_PORT* port) -{ - port->cmd |= (1<<4); - while (port->cmd & (1<<15)) - OBOSS_SpinlockHint(); - port->cmd |= (1<<0); -} - -void HaltTranscations() -{ - transactions_halted = true; -} -void ResumeTranscations() -{ - transactions_halted = false; -} - -void WaitForTranscations() -{ - for (size_t i = 0; i < PortCount; i++) - { - Port* port = Ports + i; - for (size_t j = 0; j < 32; j++) - if (port->CommandBitmask & BIT(j) && port->PendingCommands[j]) - { - obos_status status = Core_WaitOnObject(WAITABLE_OBJECT(port->PendingCommands[j]->completionEvent)); - OBOS_UNUSED(status); - } - } -} diff --git a/src/drivers/generic/ahci/command.h b/src/drivers/generic/ahci/command.h deleted file mode 100644 index 28445bfc..00000000 --- a/src/drivers/generic/ahci/command.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * drivers/generic/ahci/command.h - * - * Copyright (c) 2024-2026 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include - -#include - -#include "structs.h" - -enum { - COMMAND_DIRECTION_READ, - COMMAND_DIRECTION_WRITE, -}; -struct command_data -{ - struct physical_region* phys_regions; - size_t physRegionCount; - uint8_t direction; - uint8_t cmd; - bool awaitingSignal; - // Set when the command is done. - event completionEvent; - obos_status commandStatus; - struct { - uint8_t cmdSlot; - } internal; -}; -obos_status SendCommand(Port* port, struct command_data* data, uint64_t lba, uint8_t device, uint16_t count); -obos_status ClearCommand(Port* port, struct command_data* data); -void StopCommandEngine(volatile HBA_PORT* port); -void StartCommandEngine(volatile HBA_PORT* port); - -// Prevents any further transactions from happening. -// This causes SendComamnd to return OBOS_STATUS_RETRY, thus making it's callers also return that. -void HaltTranscations(); -// Allows transactions to happen. -void ResumeTranscations(); - -// Wait for transcations to complete. -// If shutting down the ahci driver, or preparing to suspend, -// then call this after halting transactions. -void WaitForTranscations(); diff --git a/src/drivers/generic/ahci/interface.c b/src/drivers/generic/ahci/interface.c deleted file mode 100644 index e5f5a68f..00000000 --- a/src/drivers/generic/ahci/interface.c +++ /dev/null @@ -1,353 +0,0 @@ -/* - * drivers/generic/ahci/interface.c - * - * Copyright (c) 2024-2026 Omar Berrow -*/ - -#include -#include -#include - -#include - -#include -#include -#include -#include -#include - -#include -#include -#include - -#include - -#include - -#include - -#include - -#include - -#include "command.h" -#include "structs.h" - -obos_status get_blk_size(dev_desc desc, size_t* blkSize) -{ - if (!desc || !blkSize) - return OBOS_STATUS_INVALID_ARGUMENT; - Port* port = (Port*)desc; - *blkSize = port->sectorSize; - return OBOS_STATUS_SUCCESS; -} -obos_status get_max_blk_count(dev_desc desc, size_t* count) -{ - if (!desc || !count) - return OBOS_STATUS_INVALID_ARGUMENT; - Port* port = (Port*)desc; - *count = port->nSectors; - return OBOS_STATUS_SUCCESS; -} -static obos_status unpopulate_physical_regions(uintptr_t base, size_t size, struct command_data* data); -// #pragma GCC push_options -// #pragma GCC optimize ("-O0") -static obos_status populate_physical_regions(uintptr_t base, size_t size, struct command_data* data) -{ - context* ctx = CoreS_GetCPULocalPtr()->currentContext; -#ifndef __x86_64__ - if (base >= OBOS_KERNEL_ADDRESS_SPACE_BASE && (base + size) < OBOS_KERNEL_ADDRESS_SPACE_LIMIT) - ctx = &Mm_KernelContext; -#else - if (base >= 0xffff800000000000 && (base + size) < OBOS_KERNEL_ADDRESS_SPACE_LIMIT) - ctx = &Mm_KernelContext; -#endif - const size_t MAX_PRDT_COUNT = sizeof(((HBA_CMD_TBL*)nullptr))->prdt_entry/sizeof(HBA_PRDT_ENTRY); - - return DrvH_ScatterGather( - ctx, - (void*)base, size, - &data->phys_regions, &data->physRegionCount, - MAX_PRDT_COUNT, - data->direction == COMMAND_DIRECTION_READ ? true : false); -} -static obos_status unpopulate_physical_regions(uintptr_t base, size_t size, struct command_data* data) -{ - OBOS_ASSERT(data); - OBOS_ASSERT(base); - OBOS_ASSERT(size); - context* ctx = CoreS_GetCPULocalPtr()->currentContext; -#ifndef __x86_64__ - if (base >= OBOS_KERNEL_ADDRESS_SPACE_BASE && (base + size) < OBOS_KERNEL_ADDRESS_SPACE_LIMIT) - ctx = &Mm_KernelContext; -#else - if (base >= 0xffff800000000000 && (base + size) < OBOS_KERNEL_ADDRESS_SPACE_LIMIT) - ctx = &Mm_KernelContext; -#endif - - obos_status status = DrvH_FreeScatterGatherList(ctx, (void*)base, size, data->phys_regions, data->physRegionCount); - data->phys_regions = nullptr; - - return status; -} -// #pragma GCC pop_options -obos_status read_sync(dev_desc desc, void* buf, size_t blkCount, size_t blkOffset, size_t* nBlkRead) -{ - if (!desc || !buf) - return OBOS_STATUS_INVALID_ARGUMENT; - if (blkCount > 0x10000) - { - size_t nSectorBlocks = blkCount / 0x10000; - size_t leftOver = blkCount % 0x10000; - obos_status status = OBOS_STATUS_SUCCESS; - if (nBlkRead) - *nBlkRead = 0; - for (size_t i = 0; i < nSectorBlocks; i++) - { - size_t read = 0; - if (obos_is_error(status = read_sync(desc, (void*)((uintptr_t)buf + i*0x10000), 0x10000, i*0x10000, &read))) - return status; - if (nBlkRead) - *nBlkRead += read; - } - if (leftOver) - { - size_t read = 0; - if (obos_is_error(status = read_sync(desc, (void*)((uintptr_t)buf + nSectorBlocks*0x10000), 0x10000, leftOver, &read))) - return status; - if (nBlkRead) - *nBlkRead += read; - } - return status; - } - Port* port = (Port*)desc; - if (!port->works) - { - if (nBlkRead) - *nBlkRead = 0; - return OBOS_STATUS_ABORTED; - } - if (blkCount > port->nSectors) - { - if (nBlkRead) - *nBlkRead = 0; - return OBOS_STATUS_SUCCESS; - } - if (blkOffset > port->nSectors) - return OBOS_STATUS_INVALID_ARGUMENT; - if ((blkOffset + blkCount) > port->nSectors) - blkCount = blkOffset - port->nSectors; - if (!blkCount) - { - if (nBlkRead) - *nBlkRead = 0; - return OBOS_STATUS_SUCCESS; - } - obos_status status = OBOS_STATUS_SUCCESS; - struct command_data data = { .direction=COMMAND_DIRECTION_READ, .cmd=(port->supports48bitLBA ? ATA_READ_DMA_EXT : ATA_READ_DMA ) }; - data.completionEvent = EVENT_INITIALIZE(EVENT_NOTIFICATION); - status = populate_physical_regions((uintptr_t)buf, blkCount*port->sectorSize, &data); - if (obos_is_error(status)) - return status; - // for (size_t i = 0; i < data.physRegionCount; i++) - // printf("%d: 0x%p 0x%08x\n", i, data.phys_regions[i].phys, data.phys_regions[i].sz); - for (uint8_t try = 0; try < 5; try++) - { - HBA->ghc |= BIT(1); - // irql oldIrql = Core_RaiseIrql(IRQL_AHCI); - if (obos_is_error(status = SendCommand(port, &data, blkOffset, 0x40, blkCount == 0x10000 ? 0 : blkCount))) - { - Core_SemaphoreRelease(&port->lock); - unpopulate_physical_regions((uintptr_t)buf, blkCount*port->sectorSize, &data); - break; - } - // Core_LowerIrql(oldIrql); - if (obos_is_error(status = Core_WaitOnObject(WAITABLE_OBJECT(data.completionEvent)))) - { - Core_SemaphoreRelease(&port->lock); - unpopulate_physical_regions((uintptr_t)buf, blkCount*port->sectorSize, &data); - break; - } - Core_EventClear(&data.completionEvent); - if (!port->works) - { - Core_SemaphoreRelease(&port->lock); - unpopulate_physical_regions((uintptr_t)buf, blkCount*port->sectorSize, &data); - status = OBOS_STATUS_ABORTED; // oops - break; - } - if (obos_is_success(data.commandStatus)) - break; - if (obos_is_error(data.commandStatus) && data.commandStatus != OBOS_STATUS_RETRY) - { - status = data.commandStatus; - break; - } - } - unpopulate_physical_regions((uintptr_t)buf, blkCount*port->sectorSize, &data); - if (nBlkRead) - *nBlkRead = blkCount; - return status; -} -obos_status write_sync(dev_desc desc, const void* buf, size_t blkCount, size_t blkOffset, size_t* nBlkWritten) -{ - if (!desc || !buf) - return OBOS_STATUS_INVALID_ARGUMENT; - // FIXME: Allow for writes greater than 0x10000 sectors. - if (blkCount > 0x10000) - blkCount = 0x10000; - Port* port = (Port*)desc; - if (!port->works) - { - if (nBlkWritten) - *nBlkWritten = 0; - return OBOS_STATUS_ABORTED; - } - if (blkCount > port->nSectors) - { - if (nBlkWritten) - *nBlkWritten = 0; - return OBOS_STATUS_SUCCESS; - } - if ((blkOffset + blkCount) > port->nSectors) - blkCount = (blkOffset + blkCount) - port->nSectors; - if (!blkCount) - { - if (nBlkWritten) - *nBlkWritten = 0; - return OBOS_STATUS_SUCCESS; - } - obos_status status = OBOS_STATUS_SUCCESS; - struct command_data data = { .direction=COMMAND_DIRECTION_WRITE, .cmd=(port->supports48bitLBA ? ATA_WRITE_DMA_EXT : ATA_WRITE_DMA ) }; - data.completionEvent = EVENT_INITIALIZE(EVENT_NOTIFICATION); - status = populate_physical_regions((uintptr_t)buf, blkCount*port->sectorSize, &data); - if (obos_is_error(status)) - return status; - for (uint8_t try = 0; try < 5; try++) - { - SendCommand(port, &data, blkOffset, 0x40, blkCount == 0x10000 ? 0 : blkCount); - HBA->ghc |= BIT(1) /* GhcIE */; - if (obos_is_error(status = Core_WaitOnObject(WAITABLE_OBJECT(data.completionEvent)))) - { - Core_SemaphoreRelease(&port->lock); - unpopulate_physical_regions((uintptr_t)buf, blkCount*port->sectorSize, &data); - break; - } - Core_EventClear(&data.completionEvent); - if (!port->works) - { - Core_SemaphoreRelease(&port->lock); - unpopulate_physical_regions((uintptr_t)buf, blkCount*port->sectorSize, &data); - status = OBOS_STATUS_ABORTED; // oops - break; - } - if (obos_is_success(data.commandStatus)) - break; - if (obos_is_error(data.commandStatus) && data.commandStatus != OBOS_STATUS_RETRY) - { - status = data.commandStatus; - break; - } - } - if (nBlkWritten) - *nBlkWritten = blkCount; - return status; -} -obos_status foreach_device(iterate_decision(*cb)(dev_desc desc, size_t blkSize, size_t blkCount, void* u), void* u) -{ - if (!cb) - return OBOS_STATUS_INVALID_ARGUMENT; - for (uint8_t port = 0; port < 32; port++) - cb((dev_desc)(Ports + port), Ports[port].sectorSize, Ports[port].nSectors, u); - return OBOS_STATUS_SUCCESS; -} -obos_status query_user_readable_name(dev_desc desc, const char** name) -{ - if (!desc || !name) - return OBOS_STATUS_INVALID_ARGUMENT; - Port* port = (Port*)desc; - *name = port->dev_name; - return OBOS_STATUS_SUCCESS; -} - -obos_status submit_irp(void* request_) -{ - if (!request_) - return OBOS_STATUS_INVALID_ARGUMENT; - irp* request = request_; - Port* port = (Port*)request->desc; - if (!port || !request->buff || !request->refs) - return OBOS_STATUS_INVALID_ARGUMENT; - if (!port->works) - { - VfsH_IRPSignal(request, OBOS_STATUS_ABORTED); - return OBOS_STATUS_SUCCESS; - } - if (request->blkCount > port->nSectors) - { - request->nBlkRead = 0; - VfsH_IRPSignal(request, OBOS_STATUS_SUCCESS); - return OBOS_STATUS_SUCCESS; - } - if (request->blkOffset > port->nSectors) - return OBOS_STATUS_INVALID_ARGUMENT; - if ((request->blkOffset + request->blkCount) > port->nSectors) - request->blkCount = request->blkOffset - port->nSectors; - if (!request->blkCount) - { - request->nBlkRead = 0; - VfsH_IRPSignal(request, OBOS_STATUS_SUCCESS); - return OBOS_STATUS_SUCCESS; - } - if (request->dryOp) - { - // Assume the AHCI driver can always do I/O. - // This is probably true, considering there are 32 command slots - // and not enough disk I/O done in the kernel to exhaust that. - VfsH_IRPSignal(request, OBOS_STATUS_SUCCESS); - return OBOS_STATUS_SUCCESS; - } - obos_status status = OBOS_STATUS_SUCCESS; - struct command_data *data = ZeroAllocate(OBOS_NonPagedPoolAllocator, 1, sizeof(struct command_data), nullptr); - switch (request->op) { - case IRP_READ: - data->cmd = port->supports48bitLBA ? ATA_READ_DMA_EXT : ATA_READ_DMA; - data->direction = COMMAND_DIRECTION_READ; - break; - case IRP_WRITE: - data->cmd = port->supports48bitLBA ? ATA_WRITE_DMA_EXT : ATA_WRITE_DMA; - data->direction = COMMAND_DIRECTION_WRITE; - break; - } - data->completionEvent = EVENT_INITIALIZE(EVENT_NOTIFICATION); - request->evnt = &data->completionEvent; - request->drvData = data; - status = populate_physical_regions((uintptr_t)request->buff, request->blkCount*port->sectorSize, data); - if (obos_is_error(status)) - { - Free(OBOS_NonPagedPoolAllocator, data, sizeof(struct command_data)); - request->drvData = nullptr; - VfsH_IRPSignal(request, status); - return OBOS_STATUS_SUCCESS; - } - SendCommand(port, data, request->blkOffset, 0x40, request->blkCount == 0x10000 ? 0 : request->blkCount); - HBA->ghc |= BIT(1) /* GhcIE */; - return OBOS_STATUS_SUCCESS; -} -obos_status finalize_irp(void* request_) -{ - if (!request_) - return OBOS_STATUS_INVALID_ARGUMENT; - irp* request = request_; - if (!request->drvData) - return OBOS_STATUS_INVALID_ARGUMENT; - struct command_data* data = request->drvData; - if (obos_is_success(data->commandStatus)) - request->nBlkRead = request->blkCount; - else - request->nBlkRead = 0; - unpopulate_physical_regions((uintptr_t)request->buff, request->blkCount, data); - Free(OBOS_NonPagedPoolAllocator, data, sizeof(struct command_data)); - request->drvData = nullptr; - return OBOS_STATUS_SUCCESS; -} \ No newline at end of file diff --git a/src/drivers/generic/ahci/main.c b/src/drivers/generic/ahci/main.c deleted file mode 100644 index 5a8f4537..00000000 --- a/src/drivers/generic/ahci/main.c +++ /dev/null @@ -1,558 +0,0 @@ -/* - * drivers/generic/ahci/main.c - * - * Copyright (c) 2024-2025 Omar Berrow -*/ - -#include -#include -#include -#include - -#include - -#include -#include -#include - -#include -#include -#include - -#include -#include - -#include - -#include -#include -#include - -#include - -#include "command.h" -#include "ahci_irq.h" -#include "mm/page.h" -#include "structs.h" - -#if OBOS_ARCHITECTURE_HAS_ACPI -#include -#include -#include -#include -#endif - -#include -#include -#include - -#include - -#if defined(__x86_64__) -#include -#endif - -pci_resource* PCIIrqResource = nullptr; - -OBOS_WEAK obos_status get_blk_size(dev_desc desc, size_t* blkSize); -OBOS_WEAK obos_status get_max_blk_count(dev_desc desc, size_t* count); -OBOS_WEAK obos_status read_sync(dev_desc desc, void* buf, size_t blkCount, size_t blkOffset, size_t* nBlkRead); -OBOS_WEAK obos_status write_sync(dev_desc desc, const void* buf, size_t blkCount, size_t blkOffset, size_t* nBlkWritten); -OBOS_WEAK obos_status foreach_device(iterate_decision(*cb)(dev_desc desc, size_t blkSize, size_t blkCount, void* u), void* u); -OBOS_WEAK obos_status query_user_readable_name(dev_desc what, const char** name); // unrequired for fs drivers. -OBOS_WEAK obos_status submit_irp(void*); -OBOS_WEAK obos_status finalize_irp(void*); -OBOS_PAGEABLE_FUNCTION obos_status ioctl(dev_desc what, uint32_t request, void* argp) -{ - OBOS_UNUSED(what); - OBOS_UNUSED(request); - OBOS_UNUSED(argp); - return OBOS_STATUS_INVALID_IOCTL; -} -void driver_cleanup_callback() -{ - HaltTranscations(); - WaitForTranscations(); - if (PCIIrqResource) - { - PCIIrqResource->irq->masked = true; - Drv_PCISetResource(PCIIrqResource); - } - Core_IrqObjectFree(&HbaIrq); - // TODO: Free HBA, port clb, and fb - for (size_t i = 0; i < PortCount; i++) - { - Port* port = &Ports[i]; - if (!port->vn) - continue; - Vfs_UnlinkNode(port->ent, false); - port->vn->flags |= VFLAGS_DRIVER_DEAD; - } -} - -driver_id* this_driver; - -void on_wake(); -void on_suspend(); - -__attribute__((section(OBOS_DRIVER_HEADER_SECTION))) driver_header drv_hdr = { - .magic = OBOS_DRIVER_MAGIC, - .flags = DRIVER_HEADER_HAS_STANDARD_INTERFACES|DRIVER_HEADER_FLAGS_DETECT_VIA_PCI|DRIVER_HEADER_HAS_VERSION_FIELD, - .acpiId.nPnpIds = 0, - .pciId.indiv = { - .classCode = 0x01, // mass storage controller - .subClass = 0x06, // SATA Controller - .progIf = 0x01, // AHCI 1.0 - }, - .ftable = { - .driver_cleanup_callback = driver_cleanup_callback, - .ioctl = ioctl, - .get_blk_size = get_blk_size, - .get_max_blk_count = get_max_blk_count, - .query_user_readable_name = query_user_readable_name, - .foreach_device = foreach_device, - .read_sync = read_sync, - .write_sync = write_sync, - .on_wake = on_wake, - .on_suspend = on_suspend, - .submit_irp = submit_irp, - .finalize_irp = finalize_irp, - }, - .driverName = "AHCI Driver", - .version=1, - .uacpi_init_level_required = PCI_IRQ_UACPI_INIT_LEVEL -}; - -volatile HBA_MEM* HBA; -// Arch-specific. -uint32_t HbaIrqNumber; -Port Ports[32]; -size_t PortCount; -// TODO: Make AHCI driver support multiple controllers. -pci_device* PCINode; -bool FoundPCINode; - -static void* map_registers(uintptr_t phys, size_t size, bool uc) -{ - size_t phys_page_offset = (phys % OBOS_PAGE_SIZE); - phys -= phys_page_offset; - size = size + (OBOS_PAGE_SIZE - (size % OBOS_PAGE_SIZE)); - size += phys_page_offset; - obos_status status = OBOS_STATUS_SUCCESS; - void* virt = Mm_VirtualMemoryAlloc( - &Mm_KernelContext, - nullptr, size, - uc ? OBOS_PROTECTION_CACHE_DISABLE : 0, VMA_FLAGS_NON_PAGED, - nullptr, - &status); - if (obos_is_error(status)) - { - OBOS_Error("%s: Status %d\n", __func__, status); - OBOS_ENSURE(virt); - } - for (uintptr_t offset = 0; offset < size; offset += OBOS_PAGE_SIZE) - { - page_info page = {.virt=offset+(uintptr_t)virt}; - MmS_QueryPageInfo(Mm_KernelContext.pt, page.virt, &page, nullptr); - page.prot.uc = uc; - page.phys = phys+offset; - MmS_SetPageMapping(Mm_KernelContext.pt, &page, phys + offset, false); - } - Drv_TLBShootdown(Mm_KernelContext.pt, (uintptr_t)virt, size); - return virt+phys_page_offset; -} - -uintptr_t HBAAllocate(size_t size, size_t alignment) -{ - size = size + (OBOS_PAGE_SIZE - (size % OBOS_PAGE_SIZE)); - alignment = alignment + (OBOS_PAGE_SIZE - (alignment % OBOS_PAGE_SIZE)); - alignment /= OBOS_PAGE_SIZE; - size /= OBOS_PAGE_SIZE; - if (HBA->cap & BIT(31)) - return Mm_AllocatePhysicalPages(size, alignment, nullptr); - else - return Mm_AllocatePhysicalPages32(size, alignment, nullptr); - OBOS_UNREACHABLE; -} - -const char* const DeviceNames[32] = { - "sda", "sdb", "sdc", "sdd", - "sde", "sdf", "sdg", "sdh", - "sdi", "sdj", "sdk", "sdl", - "sdm", "sdn", "sdo", "sdp", - "sdq", "sdr", "sds", "sdt", - "sdu", "sdv", "sdw", "sdx", - "sdy", "sdz", "sd1", "sd2", - "sd3", "sd4", "sd5", "sd6", -}; - -static void search_bus(pci_bus* bus) -{ - for (pci_device* dev = LIST_GET_HEAD(pci_device_list, &bus->devices); dev; ) - { - if ((dev->hid.id & 0xffffffff) == (drv_hdr.pciId.id & 0xffffffff)) - { - PCINode = dev; - FoundPCINode = true; - break; - } - - dev = LIST_GET_NEXT(pci_device_list, &bus->devices, dev); - } -} -// https://forum.osdev.org/viewtopic.php?t=40969 -driver_init_status OBOS_DriverEntry(driver_id* this) -{ - this_driver = this; - for (size_t i = 0; i < Drv_PCIBusCount; i++) - search_bus(&Drv_PCIBuses[i]); - if (!FoundPCINode) - return (driver_init_status){.status=OBOS_STATUS_NOT_FOUND,.fatal=true,.context="Could not find PCI Device."}; - //uintptr_t bar = PCINode.bars.indiv32.bar5; - // Look for BAR 5. - pci_resource* bar = nullptr; - for (pci_resource* curr = LIST_GET_HEAD(pci_resource_list, &PCINode->resources); curr; ) - { - if (curr->type == PCI_RESOURCE_BAR) - { - if (curr->bar->idx == 5) - bar = curr; - } else if (curr->type == PCI_RESOURCE_IRQ) - PCIIrqResource = curr; - - if (bar && PCIIrqResource) - break; - - curr = LIST_GET_NEXT(pci_resource_list, &PCINode->resources, curr); - } - - size_t barlen = bar->bar->size; - OBOS_Log("%*s: Initializing AHCI controller at %02x:%02x:%02x. HBA at 0x%p-0x%p.\n", - strnlen(drv_hdr.driverName, 64), drv_hdr.driverName, - PCINode->location.bus, PCINode->location.slot, PCINode->location.function, - bar, bar+barlen); - - OBOS_Debug("Enabling bus master and memory space access in PCI command.\n"); - PCINode->resource_cmd_register->cmd_register |= 6; // memory space + bus master - Drv_PCISetResource(PCINode->resource_cmd_register); - OBOS_Debug("Mapping HBA memory.\n"); - HBA = map_registers(bar->bar->phys, barlen, true); - // OBOS_Log("Mapped HBA memory at 0x%p-0x%p.\n", HBA, ((uintptr_t)HBA)+barlen); - obos_status status = Core_IrqObjectInitializeIRQL(&HbaIrq, IRQL_AHCI, true, true); - if (obos_is_error(status)) - return (driver_init_status){.status=status,.fatal=true,.context="Could not initialize IRQ object."}; - OBOS_Debug("Enabling IRQs...\n"); -/* status = Drv_RegisterPCIIrq(&HbaIrq, &PCINode, &PCIIrqHandle); - if (obos_is_error(status)) - return (driver_init_status){.status=status,.fatal=true,.context="Could not initialize HBA Irq."}; - status = Drv_MaskPCIIrq(&PCIIrqHandle, false); - if (obos_is_error(status)) - return (driver_init_status){.status=status,.fatal=true,.context="Could not unmask HBA Irq."};*/ - PCIIrqResource->irq->irq = &HbaIrq; - PCIIrqResource->irq->masked = false; - Drv_PCISetResource(PCIIrqResource); - HbaIrq.irqChecker = ahci_irq_checker; - HbaIrq.handler = ahci_irq_handler; - OBOS_Debug("Enabled IRQs.\n"); - HBA->ghc |= BIT(31); - while (!(HBA->ghc & BIT(31))) - OBOSS_SpinlockHint(); - if (HBA->cap2 & BIT(0) /* Bios/OS Handoff (BOH) */) - { - OBOS_Debug("Performing Bios/OS handoff. This might take a couple seconds.\n"); - do { - /* - 1. Sets the OS Ownership (BOHC.OOS) bit to ’1’. - 2. This will cause an SMI so that the BIOS can cleanup and pass control if needed. - 3. Spin on the BIOS Ownership (BOHC.BOS) bit, waiting for it to be cleared to ‘0’. - 4. If the BIOS Busy (BOHC.BB) has been set to ‘1’ within 25 milliseconds, then the OS - driver shall provide the BIOS a minimum of two seconds for finishing outstanding - commands on the HBA. - 5. If after 25 milliseconds the BIOS Busy (BOHC.BB) bit has not been set to ‘1’, then the OS - can assume control and perform whatever cleanup is necessary. - */ - HBA->bohc |= BIT(1); // BOHC.OOS - while (HBA->bohc & BIT(0) /* BOHC.BOS */) - OBOSS_SpinlockHint(); - timer_tick deadline = CoreS_GetTimerTick() + CoreH_TimeFrameToTick(25000 /* 25 ms */); - while (!(HBA->bohc & BIT(4)) /* BOHC.BB */ && CoreS_GetTimerTick() < deadline) - OBOSS_SpinlockHint(); - if (!(HBA->bohc & BIT(4))) - break; - // It is set... - // Spin on it for two seconds, otherwise assume control. - deadline = CoreS_GetTimerTick() + CoreH_TimeFrameToTick(2*1000000 /* 2s */); - while (HBA->bohc & BIT(4) /* BOHC.BB */ && CoreS_GetTimerTick() < deadline) - OBOSS_SpinlockHint(); - } while(0); - } - for (uint8_t i = 0; i < 32; i++) - { - if (!(HBA->pi & BIT(i))) - continue; - HBA_PORT* hPort = HBA->ports + i; - // StopCommandEngine(hPort); - hPort->cmd &= ~(1<<0); - while(hPort->cmd & (1<<15) /*PxCMD.CR*/) - OBOSS_SpinlockHint(); - hPort->cmd &= ~(1<<4); - while(hPort->cmd & (1<<14) /*PxCMD.FR*/) - OBOSS_SpinlockHint(); - } - HBA->ghc |= BIT(0); - while (HBA->ghc & BIT(0)) - OBOSS_SpinlockHint(); - HBA->ghc |= BIT(31); - while (!(HBA->ghc & BIT(31))) - OBOSS_SpinlockHint(); - HBA->ghc |= BIT(1); - for (uint8_t port = 0; port < 32; port++) - { - if (!(HBA->pi & BIT(port))) - continue; - HBA_PORT* hPort = HBA->ports + port; - Port* curr = &Ports[PortCount++]; - curr->clBasePhys = HBAAllocate(sizeof(HBA_CMD_HEADER)*32+sizeof(HBA_CMD_TBL)*32, 0); - curr->clBase = map_registers(curr->clBasePhys, sizeof(HBA_CMD_HEADER)*32+sizeof(HBA_CMD_TBL)*32, true); - curr->fisBasePhys = HBAAllocate(4096, 0); - curr->fisBase = map_registers(curr->fisBasePhys, 4096, true); - memzero((void*)curr->fisBase, 4096); - memzero((void*)curr->clBase, sizeof(HBA_CMD_HEADER)*32+sizeof(HBA_CMD_TBL)*32); - for (uint8_t slot = 0; slot < (((HBA->cap >> 8) & 0b11111)+1); slot++) - { - HBA_CMD_HEADER* cmdHeader = (HBA_CMD_HEADER*)curr->clBase + slot; - uintptr_t ctba = curr->clBasePhys + sizeof(HBA_CMD_HEADER) * 32 + slot * sizeof(HBA_CMD_TBL); - AHCISetAddress(ctba, cmdHeader->ctba); - } - AHCISetAddress(curr->clBasePhys, hPort->clb); - AHCISetAddress(curr->fisBasePhys, hPort->fb); - hPort->cmd |= BIT(4); - if (HBA->cap & BIT(27) /* CapSSS */) - hPort->cmd |= BIT(1); - timer_tick deadline = CoreS_GetTimerTick() + CoreH_TimeFrameToTick(1000); - while ((hPort->ssts & 0xf) != 0x3 && CoreS_GetTimerTick() < deadline) - OBOSS_SpinlockHint(); - curr->hbaPortIndex = port; - if ((hPort->ssts & 0xf) != 0x3) - continue; - hPort->serr = 0xffffffff; - deadline = CoreS_GetTimerTick() + CoreH_TimeFrameToTick(10000); - while (hPort->tfd & 0x80 && hPort->tfd & 0x8 && CoreS_GetTimerTick() < deadline) - OBOSS_SpinlockHint(); - if ((hPort->tfd & 0x88) != 0) - continue; - OBOS_Debug("Done port init for port %d.\n", port); - StartCommandEngine(hPort); - curr->works = true; - } - OBOS_Log("%*s: Initialized %d ports.\n", strnlen(drv_hdr.driverName, 64), drv_hdr.driverName, PortCount); - for (uint8_t i = 0; i < PortCount; i++) - { - Port* port = Ports + i; - if (!port->works) - continue; - OBOS_Log("%*s: Sending IDENTIFY_ATA to port %d.\n", strnlen(drv_hdr.driverName, 64), drv_hdr.driverName, i); - HBA->ports[port->hbaPortIndex].is = 0xffffffff; - HBA->ports[port->hbaPortIndex].ie = 0xffffffff; - HBA->ports[port->hbaPortIndex].serr = 0xffffffff; - // Send Identify ATA. - struct physical_region reg = { - .phys = HBAAllocate(512, 0), - .sz = 512 - }; - void* response = map_registers(reg.phys, reg.sz, false); - memzero(response, reg.sz); - uint16_t* res_data = (uint16_t*)response; - struct command_data data = {}; - data.phys_regions = ® - data.physRegionCount = 1; - data.cmd = ATA_IDENTIFY_DEVICE; - data.direction = COMMAND_DIRECTION_READ; - data.completionEvent = EVENT_INITIALIZE(EVENT_NOTIFICATION); - port->dev_name = DeviceNames[i]; - port->lock = SEMAPHORE_INITIALIZE((((HBA->cap >> 8) & 0b11111)+1)); - port->bitmask_lock = MUTEX_INITIALIZE(); - size_t tries = 0; - retry: - HBA->ghc &= ~BIT(1); - // irql oldIrql = Core_RaiseIrql(IRQL_AHCI); - SendCommand(port, &data, 0, 0, 0); - // Core_LowerIrql(oldIrql); - while (!(HBA->ports[port->hbaPortIndex].is)) - OBOSS_SpinlockHint(); - HBA->ghc |= BIT(1); - Core_EventClear(&data.completionEvent); - port->type = HBA->ports[port->hbaPortIndex].sig == SATA_SIG_ATA ? DRIVE_TYPE_SATA : DRIVE_TYPE_SATAPI; - if (port->type == DRIVE_TYPE_SATAPI) - { - OBOS_Log("%*s: Cannot send IDENTIFY_ATA to a SATAPI port.\n", strnlen(drv_hdr.driverName, 64), drv_hdr.driverName); - ClearCommand(port, &data); - continue; - } - if (data.commandStatus != OBOS_STATUS_SUCCESS) - { - if (tries++ >= 3) - { - Mm_FreePhysicalPages(reg.phys, reg.sz/OBOS_PAGE_SIZE); - Mm_VirtualMemoryFree(&Mm_KernelContext, (void*)port->clBase, OBOS_PAGE_SIZE); - port->clBase = 0; - Mm_VirtualMemoryFree(&Mm_KernelContext, (void*)port->fisBase, OBOS_PAGE_SIZE*7); - port->fisBase = 0; - continue; - } - OBOS_Debug("Command failed. Retrying...\n"); - data.commandStatus = OBOS_STATUS_SUCCESS; - goto retry; - } - ClearCommand(port, &data); - uint16_t cmd_feature_set = res_data[83]; - uint16_t cmd_feature_set2 = res_data[86]; - if ((cmd_feature_set & BIT(14)) && !(cmd_feature_set & BIT(15))) - port->supports48bitLBA = cmd_feature_set & BIT(10); - if (!port->supports48bitLBA) - { - if ((res_data[87] & BIT(14)) && !(res_data[87] & BIT(15))) - { - cmd_feature_set = cmd_feature_set2; - port->supports48bitLBA = cmd_feature_set & BIT(10); - } - } - if (port->supports48bitLBA) - port->nSectors = *(uint64_t*)(&res_data[100]); - else - port->nSectors = *(uint32_t*)(&res_data[60]); - uint16_t sectorInfo = res_data[106]; - port->sectorSize = 512; // Assume one sector = 512 bytes. - if ((sectorInfo & BIT(14)) && !(sectorInfo & BIT(15))) - if (sectorInfo & BIT(12)) - port->sectorSize = *((uint32_t*)&res_data[117]); - OBOS_Log("AHCI: Found %s drive at port %s. Sector count: 0x%016X, sector size 0x%08X.\n", - port->type == DRIVE_TYPE_SATA ? "SATA" : "SATAPI", - port->dev_name, - port->nSectors, - port->sectorSize); - port->vn = Drv_AllocateVNode(this, (dev_desc)port, port->nSectors*port->sectorSize, nullptr, VNODE_TYPE_BLK); - port->ent = Drv_RegisterVNode(port->vn, port->dev_name); - } - OBOS_Log("%*s: Finished initialization of the HBA.\n", strnlen(drv_hdr.driverName, 64), drv_hdr.driverName); - return (driver_init_status){.status=OBOS_STATUS_SUCCESS,.fatal=false,.context=nullptr}; -} - -void on_wake() -{ - HbaIrq.irqChecker = ahci_irq_checker; - HbaIrq.handler = ahci_irq_handler; - - // Set GHC.AE (AHCI Enable) - // Set GHC.HR (HBA Reset) - // Set GHC.AE (AHCI Enable) again - - HBA->ghc |= BIT(31); - while (!(HBA->ghc & BIT(31))) - OBOSS_SpinlockHint(); - HBA->ghc |= BIT(0); - while (HBA->ghc & BIT(0)) - OBOSS_SpinlockHint(); - HBA->ghc |= BIT(31); - while (!(HBA->ghc & BIT(31))) - OBOSS_SpinlockHint(); - - Port* curr = &Ports[0]; - for (uint8_t port = 0; port < 32; port++, curr++) - { - HBA_PORT* hPort = HBA->ports + port; - if (!curr->works) - continue; - curr->works = false; - memzero((void*)curr->fisBase, 4096); - memzero((void*)curr->clBase, sizeof(HBA_CMD_HEADER)*32+sizeof(HBA_CMD_TBL)*32); - uint32_t max_slots = (((HBA->cap >> 8) & 0b11111)+1); - for (uint8_t slot = 0; slot < max_slots; slot++) - { - HBA_CMD_HEADER* cmdHeader = (HBA_CMD_HEADER*)curr->clBase + slot; - uintptr_t ctba = curr->clBasePhys + sizeof(HBA_CMD_HEADER) * 32 + slot * sizeof(HBA_CMD_TBL); - AHCISetAddress(ctba, cmdHeader->ctba); - } - AHCISetAddress(curr->clBasePhys, hPort->clb); - AHCISetAddress(curr->fisBasePhys, hPort->fb); - hPort->cmd |= BIT(4); - if (HBA->cap & BIT(27) /* CapSSS */) - hPort->cmd |= BIT(1); - timer_tick deadline = CoreS_GetTimerTick() + CoreH_TimeFrameToTick(1000); - while ((hPort->ssts & 0xf) != 0x3 && CoreS_GetTimerTick() < deadline) - OBOSS_SpinlockHint(); - curr->hbaPortIndex = port; - if ((hPort->ssts & 0xf) != 0x3) - continue; - hPort->serr = 0xffffffff; - deadline = CoreS_GetTimerTick() + CoreH_TimeFrameToTick(10000); - while (hPort->tfd & 0x80 && hPort->tfd & 0x8 && CoreS_GetTimerTick() < deadline) - OBOSS_SpinlockHint(); - if ((hPort->tfd & 0x88) != 0) - continue; - hPort->is = 0xffffffff; - hPort->ie = 0xffffffff; - hPort->serr = 0xffffffff; - StartCommandEngine(hPort); - curr->works = true; - } - - HBA->ghc |= BIT(1); - while (!(HBA->ghc & BIT(1))) - OBOSS_SpinlockHint(); - - ResumeTranscations(); -} -void on_suspend() -{ - HaltTranscations(); - WaitForTranscations(); - HBA->ghc &= ~BIT(1); // IE (interrupt enable) -} - -// hexdump clone lol -// static OBOS_NO_KASAN void hexdump(void* _buff, size_t nBytes, const size_t width) -// { -// bool printCh = false; -// uint8_t* buff = (uint8_t*)_buff; -// printf(" Address: "); -// for(uint8_t i = 0; i < ((uint8_t)width) + 1; i++) -// printf("%02x ", i); -// printf("\n%016lx: ", buff); -// for (size_t i = 0, chI = 0; i < nBytes; i++, chI++) -// { -// if (printCh) -// { -// char ch = buff[i]; -// switch (ch) -// { -// case '\n': -// case '\t': -// case '\r': -// case '\b': -// case '\a': -// case '\0': -// { -// ch = '.'; -// break; -// } -// default: -// break; -// } -// printf("%c", ch); -// } -// else -// printf("%02x ", buff[i]); -// if (chI == (size_t)(width + (!(i < (width + 1)) || printCh))) -// { -// chI = 0; -// if (!printCh) -// i -= (width + 1); -// else -// printf(" |\n%016lx: ", &buff[i + 1]); -// printCh = !printCh; -// if (printCh) -// printf("\t| "); -// } -// } -// } diff --git a/src/drivers/generic/ahci/structs.h b/src/drivers/generic/ahci/structs.h deleted file mode 100644 index 86198398..00000000 --- a/src/drivers/generic/ahci/structs.h +++ /dev/null @@ -1,367 +0,0 @@ -/* - drivers/x86_64/sata/structs.h - - Copyright (c) 2023-2024 Omar Berrow -*/ - -#pragma once - -#include "irq/dpc.h" -#include - -#include -#include - -#include - -#define SATA_SIG_ATA 0x00000101 // SATA drive -#define SATA_SIG_ATAPI 0xEB140101 // SATAPI drive -#define SATA_SIG_SEMB 0xC33C0101 // Enclosure management bridge -#define SATA_SIG_PM 0x96690101 // Port multiplier - -#define HBA_PORT_IPM_ACTIVE 1 -#define HBA_PORT_DET_PRESENT 3 - -typedef volatile struct HBA_PORT -{ - volatile uint32_t clb; // 0x00, command list base address, 1K-byte aligned - volatile uint32_t clbu; // 0x04, command list base address upper 32 bits - volatile uint32_t fb; // 0x08, FIS base address, 256-byte aligned - volatile uint32_t fbu; // 0x0C, FIS base address upper 32 bits - volatile uint32_t is; // 0x10, interrupt status - volatile uint32_t ie; // 0x14, interrupt enable - volatile uint32_t cmd; // 0x18, command and status - volatile uint32_t rsv0; // 0x1C, Reserved - volatile uint32_t tfd; // 0x20, task file data - volatile uint32_t sig; // 0x24, signature - volatile uint32_t ssts; // 0x28, SATA status (SCR0:SStatus) - volatile uint32_t sctl; // 0x2C, SATA control (SCR2:SControl) - volatile uint32_t serr; // 0x30, SATA error (SCR1:SError) - volatile uint32_t sact; // 0x34, SATA active (SCR3:SActive) - volatile uint32_t ci; // 0x38, command issue - volatile uint32_t sntf; // 0x3C, SATA notification (SCR4:SNotification) - volatile uint32_t fbs; // 0x40, FIS-based switch control - volatile uint32_t rsv1[11]; // 0x44 ~ 0x6F, Reserved - volatile uint32_t vendor[4]; // 0x70 ~ 0x7F, vendor specific -} HBA_PORT; -typedef struct HBA_MEM -{ - // 0x00 - 0x2B, Generic Host Control - // volatile struct - // { - // const volatile uint8_t np : 5; - // const volatile bool sxs : 1; - // const volatile bool ems : 1; - // const volatile bool ccsc : 1; - // const volatile uint8_t nsc : 5; - // const volatile bool psc : 1; - // const volatile bool ssc : 1; - // const volatile bool pmd : 1; - // const volatile bool fbss : 1; - // const volatile bool spm : 1; - // const volatile bool sam : 1; - // const volatile bool resv1 : 1; - // const volatile uint8_t iss : 4; - // const volatile bool sclo : 1; - // const volatile bool sal : 1; - // const volatile bool salp : 1; - // const volatile bool sss : 1; - // const volatile bool smps : 1; - // const volatile bool ssntf : 1; - // const volatile bool sncq : 1; - // const volatile bool s64a : 1; - // } cap; // 0x00, Host capability - uint32_t cap; - // volatile struct - // { - // // HBA Reset. - // volatile bool hr : 1; - // // Interrupt Enable. - // volatile bool ie : 1; - // // MSI Revert to Single Message - // volatile bool mrsm : 1; - // // Reserved. - // volatile uint32_t revs : 28; - // // AHCI Enable. - // volatile bool ae : 1; - // } __attribute__((packed)) ghc; // 0x04, Global host control - volatile uint32_t ghc; - volatile uint32_t is; // 0x08, Interrupt status - volatile uint32_t pi; // 0x0C, Port implemented - volatile uint32_t vs; // 0x10, Version - volatile uint32_t ccc_ctl; // 0x14, Command completion coalescing control - volatile uint32_t ccc_pts; // 0x18, Command completion coalescing ports - volatile uint32_t em_loc; // 0x1C, Enclosure management location - volatile uint32_t em_ctl; // 0x20, Enclosure management control - volatile uint32_t cap2; // 0x24, Host capabilities extended - volatile uint32_t bohc; // 0x28, BIOS/OS handoff control and status - - // 0x2C - 0x9F, Reserved - volatile uint8_t rsv[0xA0 - 0x2C]; - - // 0xA0 - 0xFF, Vendor specific registers - volatile uint8_t vendor[0x100 - 0xA0]; - - // 0x100 - 0x10FF, Port control registers - volatile HBA_PORT ports[32]; // 1 ~ 32 -} HBA_MEM; - -typedef enum FIS_TYPE -{ - FIS_TYPE_REG_H2D = 0x27, // Register FIS - host to device - FIS_TYPE_REG_D2H = 0x34, // Register FIS - device to host - FIS_TYPE_DMA_ACT = 0x39, // DMA activate FIS - device to host - FIS_TYPE_DMA_SETUP = 0x41, // DMA setup FIS - bidirectional - FIS_TYPE_DATA = 0x46, // Data FIS - bidirectional - FIS_TYPE_BIST = 0x58, // BIST activate FIS - bidirectional - FIS_TYPE_PIO_SETUP = 0x5F, // PIO setup FIS - device to host - FIS_TYPE_DEV_BITS = 0xA1, // Set device bits FIS - device to host -} FIS_TYPE; - -typedef struct FIS_REG_H2D -{ - volatile uint8_t fis_type; // FIS_TYPE_REG_H2D - - // volatile uint8_t pmport : 4; - // volatile uint8_t rsv0 : 3; - // volatile uint8_t c : 1; - volatile uint8_t b1; - - volatile uint8_t command; - volatile uint8_t featurel; - - volatile uint8_t lba0; - volatile uint8_t lba1; - volatile uint8_t lba2; - volatile uint8_t device; - - volatile uint8_t lba3; - volatile uint8_t lba4; - volatile uint8_t lba5; - volatile uint8_t featureh; - - volatile uint8_t countl; - volatile uint8_t counth; - volatile uint8_t icc; - volatile uint8_t control; - - volatile uint8_t rsv1[4]; -} FIS_REG_H2D; - -typedef struct FIS_REG_D2H -{ - volatile uint8_t fis_type; // FIS_TYPE_REG_D2H - - // volatile uint8_t pmport : 4; - // volatile uint8_t rsv0 : 2; - // volatile uint8_t i : 1; - // volatile uint8_t resv1 : 1; - volatile uint8_t b1; - - volatile uint8_t status; - volatile uint8_t error; - - volatile uint8_t lba0; - volatile uint8_t lba1; - volatile uint8_t lba2; - volatile uint8_t device; - - volatile uint8_t lba3; - volatile uint8_t lba4; - volatile uint8_t lba5; - volatile uint8_t resv2; - - volatile uint8_t countl; - volatile uint8_t counth; - volatile uint8_t resv3[2]; - - volatile uint8_t resv4[4]; -} FIS_REG_D2H; - -typedef struct FIS_DATA -{ - volatile uint8_t fis_type; // FIS_TYPE_DATA - - // volatile uint8_t pmport : 4; - // volatile uint8_t rsv0 : 4; - volatile uint8_t b1; - - volatile uint8_t rsv1[2]; - - // DWORD 1 ~ N - volatile uint8_t data[1]; -} FIS_DATA; - -typedef struct FIS_PIO_SETUP -{ - volatile uint8_t fis_type; // FIS_TYPE_PIO_SETUP - - // volatile uint8_t pmport : 4; - // volatile uint8_t rsv0 : 1; - // volatile uint8_t d : 1; - // volatile uint8_t i : 1; - // volatile uint8_t resv1 : 1; - volatile uint8_t b1; - - volatile uint8_t status; - volatile uint8_t error; - - volatile uint8_t lba0; - volatile uint8_t lba1; - volatile uint8_t lba2; - volatile uint8_t device; - - volatile uint8_t lba3; - volatile uint8_t lba4; - volatile uint8_t lba5; - volatile uint8_t resv2; - - volatile uint8_t countl; - volatile uint8_t counth; - volatile uint8_t resv3; - volatile uint8_t e_status; - - volatile uint16_t tc; - volatile uint8_t resv4[2]; -} FIS_PIO_SETUP; - -typedef struct FIS_DMA_SETUP -{ - volatile uint8_t fis_type; // FIS_TYPE_DMA_SETUP - volatile uint8_t b1; - - // volatile uint8_t pmport : 4; - // volatile uint8_t resv0 : 1; - // volatile uint8_t d : 1; - // volatile uint8_t i : 1; - // volatile uint8_t a : 1; - - volatile uint8_t resv1[2]; - - volatile uint64_t DMAbufferID; - - volatile uint32_t resv2; - - volatile uint32_t DMAbufOffset; - - volatile uint32_t TransferCount; - - volatile uint32_t resv3; -} FIS_DMA_SETUP; - -typedef volatile struct HBA_CMD_HEADER -{ - // volatile uint8_t cfl:5; - // volatile uint8_t a:1; - // volatile uint8_t w:1; - // volatile uint8_t p:1; - - // volatile uint8_t r:1; - // volatile uint8_t b:1; - // volatile uint8_t c:1; - // volatile uint8_t rsv0:1; - // volatile uint8_t pmp:4; - volatile uint8_t b0; - volatile uint8_t b1; - - volatile uint16_t prdtl; - - volatile uint32_t prdbc; - - volatile uint32_t ctba; - volatile uint32_t ctbau; - - volatile uint32_t rsv1[4]; // Reserved -} HBA_CMD_HEADER; - -typedef struct HBA_PRDT_ENTRY -{ - volatile uint32_t dba; - volatile uint32_t dbau; - volatile uint32_t rsv0; - - // volatile uint32_t dbc:22; - // volatile uint32_t rsv1:9; - // volatile uint32_t i:1; - volatile uint32_t dw4; -} HBA_PRDT_ENTRY; - -typedef volatile struct HBA_CMD_TBL -{ - volatile uint8_t cfis[64]; - - volatile uint8_t acmd[16]; - - volatile uint8_t rsv[48]; - - volatile HBA_PRDT_ENTRY prdt_entry[1024]; -} HBA_CMD_TBL; - -typedef enum drive_type -{ - DRIVE_TYPE_INVALID = 0, - DRIVE_TYPE_SATA, - DRIVE_TYPE_SATAPI, -} drive_type; -typedef struct Port -{ - struct command_data* PendingCommands[32]; - mutex bitmask_lock; - semaphore lock; // can have a maximum of 32 slots - dpc port_dpc; - volatile void* clBase; - volatile void* fisBase; - struct vnode* vn; - struct dirent* ent; - uintptr_t clBasePhys, fisBasePhys; - const char* dev_name; - uint64_t nSectors; // used in get_max_blk_count - uint32_t sectorSize; // used in get_blk_size - uint32_t CommandBitmask; // should be an exact representation of hbaPort->ci - drive_type type; - uint8_t hbaPortIndex; - bool works : 1; - bool supports48bitLBA : 1; -} Port; - -enum -{ - ATA_READ_DMA_EXT = 0x25, - ATA_READ_DMA = 0xC8, - ATA_WRITE_DMA_EXT = 0x35, - ATA_WRITE_DMA = 0xCA, - ATA_IDENTIFY_DEVICE = 0xEC, -}; - -extern volatile HBA_MEM* HBA; -// Arch-specific. -extern uint32_t HbaIrqNumber; -extern Port Ports[32]; -extern size_t PortCount; -extern irq HbaIrq; - -#if OBOS_IRQL_COUNT == 16 -# define IRQL_AHCI (7) -#elif OBOS_IRQL_COUNT == 8 -# define IRQL_AHCI (3) -#elif OBOS_IRQL_COUNT == 4 -# define IRQL_AHCI (2) -#elif OBOS_IRQL_COUNT == 2 -# define IRQL_AHCI (0) -#else -# error Funny business. -#endif - -#if OBOS_ARCHITECTURE_BITS == 64 -#define AHCISetAddress(phys, field) \ -do {\ - field = (phys & 0xffffffff);\ - if (HBA->cap & BIT(31))\ - field##u = (phys >> 32);\ -} while(0) -#else -#define AHCISetAddress(phys, field) \ -do {\ - field = (phys & 0xffffffff);\ -} while(0) -#endif \ No newline at end of file diff --git a/src/drivers/generic/extfs/CMakeLists.txt b/src/drivers/generic/extfs/CMakeLists.txt deleted file mode 100644 index ef8730d0..00000000 --- a/src/drivers/generic/extfs/CMakeLists.txt +++ /dev/null @@ -1,33 +0,0 @@ -# drivers/generic/extfs/CMakeLists.txt -# -# Copyright (c) 2025 Omar Berrow - -add_executable(extfs "main.c" "probe.c" "helper.c" "dirent.c" - "interface.c" "create.c") - -target_compile_options(extfs - PRIVATE $<$:${TARGET_DRIVER_COMPILE_OPTIONS_C}> - PRIVATE $<$:-ffreestanding> - PRIVATE $<$:-Wall> - PRIVATE $<$:-Wextra> - PRIVATE $<$:-fstack-protector-all> - PRIVATE $<$:-fno-builtin-memset> - PRIVATE $<$:-fvisibility=hidden> - PRIVATE $<$:-fPIC> -) - -set_property(TARGET extfs PROPERTY link_depends ${DRIVER_LINKER_SCRIPT}) - -target_include_directories(extfs - PRIVATE "${CMAKE_SOURCE_DIR}/src/oboskrnl" - PRIVATE ${OBOSKRNL_EXTERNAL_INCLUDES}) - -target_link_options(extfs - PRIVATE "-nostdlib" - PRIVATE "-fPIC" - PRIVATE "-T${DRIVER_LINKER_SCRIPT}" - PRIVATE "-Wl,-shared" -# PRIVATE "-Wl,--allow-shlib-undefined" - PRIVATE ${TARGET_DRIVER_LINKER_OPTIONS} -) -target_compile_definitions(extfs PRIVATE OBOS_DRIVER=1) diff --git a/src/drivers/generic/extfs/create.c b/src/drivers/generic/extfs/create.c deleted file mode 100644 index aa971b14..00000000 --- a/src/drivers/generic/extfs/create.c +++ /dev/null @@ -1,622 +0,0 @@ -/* - * drivers/generic/extfs/create.c - * - * Copyright (c) 2026 Omar Berrow - * - * Abandon all hope, ye who enter here. - */ - -#include -#include -#include -#include - -#include - -#include - -#include - -#include "structs.h" - -static obos_status expand_directory(ext_cache* cache, ext_dirent_cache* parent, size_t* new_ent_offset, size_t* prev_ent_offset) -{ - size_t block = 0, old_size = ext_ino_filesize(cache, parent->inode); - - obos_status status = ext_ino_resize(cache, parent->ent.ino, old_size + block, true); - if (obos_is_error(status)) - return status; - status = ext_ino_commit_blocks(cache, parent->ent.ino, old_size, block); - if (obos_is_error(status)) - return status; - - size_t nToRead = le32_to_host(parent->inode->blocks) * 512; - uint8_t* buffer = Allocate(EXT_Allocator, nToRead, nullptr); - ext_ino_read_blocks(cache, parent->ent.ino, 0, nToRead, buffer, nullptr); - - ext_dirent* iter = nullptr; - size_t offset = 0; - for (; offset < old_size; ) - { - iter = (void*)(buffer+offset); - offset += iter->rec_len; - } - - *prev_ent_offset = offset - iter->rec_len; - // the last entry should already point to the new entry. - *new_ent_offset = old_size; - - return OBOS_STATUS_SUCCESS; -} - -static obos_status make_dirent( - ext_dirent_cache** out, - ext_cache* cache, - ext_dirent_cache* parent, - const char* name, - uint32_t ino, bool ino_new, - file_type type) -{ - page* pg = nullptr; - ext_inode* inode = ext_read_inode_pg(cache, ino, &pg); - if (!inode) - return OBOS_STATUS_INVALID_ARGUMENT; - MmH_RefPage(pg); - if (ino_new) - memzero(inode, sizeof(*inode)); - - // look for space for the dirent - - size_t nToRead = le32_to_host(parent->inode->blocks) * 512; - uint8_t* buffer = Allocate(EXT_Allocator, nToRead, nullptr); - ext_ino_read_blocks(cache, parent->ent.ino, 0, nToRead, buffer, nullptr); - - size_t hole_begin = 0, hole_end = 0; - uint16_t ent_len = strlen(name) + (sizeof(ext_dirent)-255); - size_t ent_offset = 0, prev_ent_offset = 0; - - ext_dirent* iter = nullptr; - size_t offset = 0; - for (; offset < nToRead; ) - { - iter = (void*)(buffer+offset); - - if (!hole_begin) - { - size_t real_begin = offset + (sizeof(ext_dirent)-255) + iter->name_len; - if (!iter->ino) - real_begin = offset; - hole_begin = real_begin; - hole_begin += 3; - hole_begin &= ~3; - if ((hole_begin - real_begin) > (iter->rec_len-real_begin)) - { - hole_begin = 0; - goto next; - } - prev_ent_offset = offset; - } - else - { - hole_end = offset; - size_t hole_size = hole_end - hole_begin; - if (hole_size >= ent_len) - { - ent_offset = hole_begin; - ent_len += (hole_size-ent_len); - break; - } - hole_end = 0; - prev_ent_offset = offset; - size_t real_begin = offset + (sizeof(ext_dirent)-255) + iter->name_len; - if (!iter->ino) - real_begin = offset; - hole_begin = real_begin; - hole_begin += 3; - hole_begin &= ~3; - } - - next: - offset += iter->rec_len; - } - - Free(OBOS_KernelAllocator, buffer, nToRead); - buffer = nullptr; - - if (!hole_begin) - { - obos_status status = expand_directory(cache, parent, &ent_offset, &prev_ent_offset); - if (obos_is_error(status)) - return status; - } - - if (hole_begin && !hole_end) - { - hole_end = offset; - size_t hole_size = hole_end - hole_begin; - if (hole_size < ent_len) - { - obos_status status = expand_directory(cache, parent, &ent_offset, &prev_ent_offset); - if (obos_is_error(status)) - return status; - } - else - { - ent_offset = hole_begin; - ent_len += (hole_size-ent_len); - } - } - - ext_dirent_cache* ent = ZeroAllocate(EXT_Allocator, 1, sizeof(ext_dirent_cache), nullptr); - ent->rel_offset = ent_offset; - ent->ent_block = ext_get_block_at_index(cache, parent->ent.ino, ext_get_blk_index_from_offset(cache, ent_offset)); - OBOS_ENSURE(ent->ent_block != 0); - ent->ent_offset = ent_offset % cache->block_size; - ent->parent = parent; - ent->inode = inode; - ent->pg = pg; - ent->ent.ino = ino; - ent->ent.rec_len = ent_len; - ent->ent.name_len = strlen(name); - ent->cache = cache; - memcpy(ent->ent.name, name, ent->ent.name_len); - - ext_dirent_cache* emplace_at = nullptr; - bool found = false; - for (ext_dirent_cache* node = parent->children.head; node; node = node->next) - { - if (node->rel_offset == prev_ent_offset) - { - // We have closed the hole, change the rec_len of the previous entry. - node->ent.rec_len = ent_offset - prev_ent_offset; - ext_dirent_flush(cache, node); - if (node->next) - emplace_at = node; - found = true; - break; - } - } - - OBOS_ENSURE(found); - - if (emplace_at) - ext_dirent_emplace_at(parent, ent, emplace_at); - else - ext_dirent_adopt(parent, ent); - - if (cache->superblock.revision > 0) - { - switch (type) { - case FILE_TYPE_DIRECTORY: - ent->ent.file_type = EXT2_FT_DIR; - break; - case FILE_TYPE_REGULAR_FILE: - ent->ent.file_type = EXT2_FT_REG_FILE; - break; - case FILE_TYPE_SYMBOLIC_LINK: - ent->ent.file_type = EXT2_FT_SYMLINK; - break; - default: OBOS_UNREACHABLE; - } - } - - ext_dirent_flush(cache, ent); - - *out = ent; - return OBOS_STATUS_SUCCESS; -} - -static void directory_finalize(ext_cache* cache, ext_inode* inode, uint32_t ino, uint32_t parent_ino) -{ - const size_t dir_size = cache->block_size * 4; - ext_dirent* ents = Allocate(EXT_Allocator, dir_size, nullptr); - ext_dirent* end = (ext_dirent*)(((uintptr_t)ents) + dir_size); - inode->size = dir_size; -#if ext_sb_supports_64bit_filesize - inode->dir_acl = (dir_size >> 32); -#endif - inode->blocks = dir_size / 512; - obos_status status = ext_ino_commit_blocks(cache, ino, 0, inode->size); - OBOS_ASSERT(obos_is_success(status)); - if (obos_is_error(status)) - return; - ext_dirent* dot = ents; - dot->rec_len = (sizeof(ext_dirent)-255) + 4; - dot->name_len = 1; - dot->name[0] = '.'; - if (cache->revision > 0) - dot->file_type = EXT2_FT_DIR; - dot->ino = ino; - ext_dirent* dotdot = (ext_dirent*)(((uintptr_t)dot) + dot->rec_len); - size_t rec_len = dir_size - ((sizeof(ext_dirent)-255) + 4); - if (rec_len > cache->block_size) - { - ext_dirent* current_entry = dotdot; - rec_len += (cache->block_size-1); - rec_len &= ~(cache->block_size-1); - while (rec_len != 0 && (current_entry < end)) - { - memzero(current_entry, sizeof(*current_entry)); - if (current_entry == dotdot) - current_entry->rec_len = cache->block_size - ((sizeof(ext_dirent)-255) + 4); - else - current_entry->rec_len = cache->block_size; - current_entry = (ext_dirent*)(((uintptr_t)current_entry) + current_entry->rec_len); - rec_len -= cache->block_size; - } - current_entry->rec_len = cache->block_size; - } - else - dotdot->rec_len = rec_len; - dotdot->name_len = 2; - dotdot->name[0] = '.'; - dotdot->name[1] = '.'; - if (cache->revision > 0) - dotdot->file_type = EXT2_FT_DIR; - dotdot->ino = parent_ino; - inode->link_count++; - do { - page* pg = nullptr; - ext_inode* parent = ext_read_inode_pg(cache, parent_ino, &pg); - parent->link_count++; - } while(0); - - cache->bgdt[ext_ino_get_block_group(cache, ino)].used_directories++; - ext_writeback_bgd(cache, ext_ino_get_block_group(cache, ino)); - ext_ino_write_blocks(cache, ino, 0, dir_size, ents, nullptr); - - Free(EXT_Allocator, ents, dir_size); -} - -obos_status pmk_file(dev_desc* newDesc, - const char* parent_path, - void* vn, - const char* name, - file_type type, - driver_file_perm perm) -{ - if (!newDesc || !parent_path || !vn || !name || (type > FILE_TYPE_SYMBOLIC_LINK)) - return OBOS_STATUS_INVALID_ARGUMENT; - - Mm_PageWriterOperation |= PAGE_WRITER_SYNC_FILE; - Mm_WakePageWriter(true); - Mm_WakePageWriter(true); - - ext_cache *cache = nullptr; - for (ext_cache* curr = LIST_GET_HEAD(ext_cache_list, &EXT_CacheList); curr && !cache; ) - { - if (curr->vn == vn) - cache = curr; - - curr = LIST_GET_NEXT(ext_cache_list, &EXT_CacheList, curr); - } - - if (!cache) - return OBOS_STATUS_NOT_FOUND; - - if (cache->read_only) - return OBOS_STATUS_READ_ONLY; - - if ((strlen(name)) > 255) - return OBOS_STATUS_INVALID_ARGUMENT; // TODO ENAMETOOLONG - if (name[strchr(name, '/')] == '/') - return OBOS_STATUS_INVALID_ARGUMENT; - - ext_dirent_cache* parent = ext_dirent_lookup_from(parent_path, cache->root); - if (!parent) - return OBOS_STATUS_NOT_FOUND; - if (parent != cache->root) - { - char name[256] = {}; - memcpy(name, parent->ent.name, parent->ent.name_len); - ext_dirent_populate(cache, parent->ent.ino, name, false, parent); - } - - ext_dirent_cache* ent = nullptr; - - uint32_t newino = ext_ino_allocate(cache, nullptr); - if (!newino) - return OBOS_STATUS_NO_SPACE; - - obos_status status = make_dirent(&ent, cache, parent, name, newino, true, type); - if (obos_is_error(status)) - { - ext_ino_free(cache, newino); - return status; - } - - if (cache->revision == 1) - ent->inode->dir_acl = 0; - ent->inode->size = 0; - ent->inode->gid = ROOT_GID; - ent->inode->uid = ROOT_UID; - if (perm.set_uid) - ent->inode->mode |= EXT_SETUID; - if (perm.set_gid) - ent->inode->mode |= EXT_SETGID; - if (perm.owner_read) - ent->inode->mode |= EXT_OWNER_READ; - if (perm.owner_write) - ent->inode->mode |= EXT_OWNER_WRITE; - if (perm.owner_exec) - ent->inode->mode |= EXT_OWNER_EXEC; - if (perm.group_read) - ent->inode->mode |= EXT_GROUP_READ; - if (perm.group_write) - ent->inode->mode |= EXT_GROUP_WRITE; - if (perm.group_exec) - ent->inode->mode |= EXT_GROUP_EXEC; - if (perm.other_read) - ent->inode->mode |= EXT_OTHER_READ; - if (perm.other_write) - ent->inode->mode |= EXT_OTHER_WRITE; - if (perm.other_exec) - ent->inode->mode |= EXT_OTHER_EXEC; - ent->inode->link_count++; - - switch (type) - { - case FILE_TYPE_SYMBOLIC_LINK: - ent->inode->mode |= EXT2_S_IFLNK; - break; - case FILE_TYPE_DIRECTORY: - ent->inode->mode |= EXT2_S_IFDIR; - directory_finalize(cache, ent->inode, newino, parent->ent.ino); - break; - case FILE_TYPE_REGULAR_FILE: - ent->inode->mode |= EXT2_S_IFREG; - break; - default: - OBOS_ENSURE(!"unimplemented"); - } - - Mm_MarkAsDirtyPhys(ent->pg); - - ext_inode_handle* hnd = ZeroAllocate(EXT_Allocator, 1, sizeof(*hnd), nullptr); - hnd->cache = cache; - hnd->ino = newino; - hnd->lock = MUTEX_INITIALIZE(); - *newDesc = (dev_desc)hnd; - - return OBOS_STATUS_SUCCESS; -} - - -obos_status phardlink_file(dev_desc desc, const char* parent_path, void* vn, const char* name) -{ - ext_inode_handle* to_link = (void*)desc; - - Mm_PageWriterOperation |= PAGE_WRITER_SYNC_FILE; - Mm_WakePageWriter(true); - Mm_WakePageWriter(true); - - if (!parent_path || !to_link || !name) - return OBOS_STATUS_INVALID_ARGUMENT; - - ext_cache *cache = nullptr; - for (ext_cache* curr = LIST_GET_HEAD(ext_cache_list, &EXT_CacheList); curr && !cache; ) - { - if (curr->vn == vn) - cache = curr; - - curr = LIST_GET_NEXT(ext_cache_list, &EXT_CacheList, curr); - } - - if (!cache) - return OBOS_STATUS_NOT_FOUND; - - if (cache != to_link->cache) - return OBOS_STATUS_ACCESS_DENIED; - - ext_dirent_cache* parent = ext_dirent_lookup_from(parent_path, cache->root); - if (!parent) - return OBOS_STATUS_NOT_FOUND; - if (parent != cache->root) - { - char name[256] = {}; - memcpy(name, parent->ent.name, parent->ent.name_len); - ext_dirent_populate(cache, parent->ent.ino, name, false, parent); - } - - ext_dirent_cache* ent = nullptr; - - file_type type = 0; - ext_inode* ino = ext_read_inode(cache, to_link->ino); - if (ext_ino_test_type(ino, EXT2_S_IFDIR)) - { - Free(EXT_Allocator, ino, sizeof(*ino)); - return OBOS_STATUS_ACCESS_DENIED; - } - else if (ext_ino_test_type(ino, EXT2_S_IFREG)) - type = FILE_TYPE_REGULAR_FILE; - else if (ext_ino_test_type(ino, EXT2_S_IFLNK)) - type = FILE_TYPE_SYMBOLIC_LINK; - else - { - Free(EXT_Allocator, ino, sizeof(*ino)); - return OBOS_STATUS_UNIMPLEMENTED; - } - - Free(EXT_Allocator, ino, sizeof(*ino)); - - obos_status status = make_dirent(&ent, cache, parent, name, to_link->ino, false, type); - if (obos_is_error(status)) - return status; - - ent->inode->link_count++; - Mm_MarkAsDirtyPhys(ent->pg); - - return OBOS_STATUS_SUCCESS; -} - -obos_status symlink_set_path(dev_desc desc, const char* to) -{ - ext_inode_handle* hnd = (void*)desc; - if (!hnd) - return OBOS_STATUS_INVALID_ARGUMENT; - - page* pg = nullptr; - ext_inode* inode = ext_read_inode_pg(hnd->cache, hnd->ino, &pg); - OBOS_ASSERT(inode); - if (!inode) - return OBOS_STATUS_INVALID_ARGUMENT; - - MmH_RefPage(pg); - - if (!ext_ino_test_type(inode, EXT2_S_IFLNK)) - { - MmH_DerefPage(pg); - return OBOS_STATUS_INVALID_ARGUMENT; - } - - bool dirty = false; - size_t path_len = strlen(to); - bool fast_symlink = path_len <= 60; - - if (fast_symlink) - memcpy((char*)&inode->direct_blocks, to, path_len); - else - { - obos_status status = ext_ino_resize(hnd->cache, hnd->ino, path_len, false); - if (obos_is_error(status)) - { - MmH_DerefPage(pg); - return status; - } - - status = ext_ino_commit_blocks(hnd->cache, hnd->ino, 0, path_len); - if (obos_is_error(status)) - { - MmH_DerefPage(pg); - return status; - } - - status = ext_ino_write_blocks(hnd->cache, hnd->ino, 0, path_len, to, nullptr); - if (obos_is_error(status)) - { - MmH_DerefPage(pg); - return status; - } - } - - inode->size = path_len; - - if (dirty) - Mm_MarkAsDirtyPhys(pg); - MmH_DerefPage(pg); - - return OBOS_STATUS_SUCCESS; -} - -static obos_status premove_file_impl(ext_cache *cache, const char* path, bool unlink) -{ - ext_dirent_cache* dent = ext_dirent_lookup_from(path, cache->root); - if (!dent) - return OBOS_STATUS_NOT_FOUND; - if (strncmp(dent->ent.name, ".", dent->ent.name_len) || strncmp(dent->ent.name, "..", dent->ent.name_len)) - return OBOS_STATUS_ACCESS_DENIED; - - ext_dirent_cache* prev = dent->prev; - ext_dirent_cache* next = dent->next; - if (prev) - { - prev->ent.rec_len = (next ? next->rel_offset : dent->parent->inode->blocks*512) - prev->rel_offset; - ext_dirent_flush(cache, prev); - } - - dent->ent.ino = 0; - dent->ent.file_type = 0; - ext_dirent_flush(cache, dent); - - ext_dirent_disown(dent->parent, dent); - if (unlink) - if (!(--dent->inode->link_count)) - ext_ino_free(cache, dent->ent.ino); - MmH_DerefPage(dent->pg); - - Free(EXT_Allocator, dent, sizeof(*dent)); - - return OBOS_STATUS_SUCCESS; -} - -obos_status premove_file(void* vn, const char* path) -{ - ext_cache *cache = nullptr; - for (ext_cache* curr = LIST_GET_HEAD(ext_cache_list, &EXT_CacheList); curr && !cache; ) - { - if (curr->vn == vn) - cache = curr; - - curr = LIST_GET_NEXT(ext_cache_list, &EXT_CacheList, curr); - } - - if (!cache) - return OBOS_STATUS_NOT_FOUND; - - return premove_file_impl(cache, path, true); -} - -OBOS_WEAK obos_status pmove_desc_to(void* vn, const char* path, const char* newpath, const char* name) -{ - if (strcmp(name, ".")) - return OBOS_STATUS_INVALID_ARGUMENT; - if (strcmp(name, "..")) - return OBOS_STATUS_INVALID_ARGUMENT; - - ext_cache *cache = nullptr; - for (ext_cache* curr = LIST_GET_HEAD(ext_cache_list, &EXT_CacheList); curr && !cache; ) - { - if (curr->vn == vn) - cache = curr; - - curr = LIST_GET_NEXT(ext_cache_list, &EXT_CacheList, curr); - } - - if (!cache) - return OBOS_STATUS_NOT_FOUND; - - ext_dirent_cache* ent = nullptr; - - ext_dirent_cache* parent = ext_dirent_lookup_from(newpath, cache->root); - if (!parent) - return OBOS_STATUS_NOT_FOUND; - - ext_dirent_cache* dent = ext_dirent_lookup_from(path, cache->root); - if (!dent) - return OBOS_STATUS_NOT_FOUND; - - if (!newpath) - parent = dent->parent; - - if (strncmp(dent->ent.name, ".", dent->ent.name_len) || strncmp(dent->ent.name, "..", dent->ent.name_len)) - return OBOS_STATUS_ACCESS_DENIED; - - uint32_t ino = dent->ent.ino; - dent = nullptr; - - obos_status status = premove_file_impl(cache, path, false); - if (obos_is_error(status)) - return status; - - file_type type = 0; - ext_inode* inode = ext_read_inode(cache, ino); - if (ext_ino_test_type(inode, EXT2_S_IFDIR)) - type = FILE_TYPE_DIRECTORY; - else if (ext_ino_test_type(inode, EXT2_S_IFREG)) - type = FILE_TYPE_REGULAR_FILE; - else if (ext_ino_test_type(inode, EXT2_S_IFLNK)) - type = FILE_TYPE_SYMBOLIC_LINK; - else - { - Free(EXT_Allocator, inode, sizeof(*inode)); - return OBOS_STATUS_UNIMPLEMENTED; - } - - Free(EXT_Allocator, inode, sizeof(*inode)); - - Mm_PageWriterOperation |= PAGE_WRITER_SYNC_FILE; - Mm_WakePageWriter(true); - Mm_WakePageWriter(true); - - return make_dirent(&ent, cache, parent, name, ino, false, type); -} \ No newline at end of file diff --git a/src/drivers/generic/extfs/dirent.c b/src/drivers/generic/extfs/dirent.c deleted file mode 100644 index 84c6e018..00000000 --- a/src/drivers/generic/extfs/dirent.c +++ /dev/null @@ -1,334 +0,0 @@ -/* - * drivers/generic/extfs/dirent.c - * - * Copyright (c) 2025 Omar Berrow - * - * Abandon all hope, ye who enter here. - */ - -#include -#include -#include -#include - -#include - -#include - -#include - -#include "structs.h" - -uint32_t ext_get_block_at_index(ext_cache* cache, uint32_t ino, struct inode_offset_location loc) -{ - ext_inode* inode = ext_read_inode(cache, ino); - OBOS_ASSERT(inode); - uint32_t block = 0; -#define idx_blocks 0 -#define idx_indirect_block 1 -#define idx_doubly_indirect_block 2 -#define idx_triply_indirect_block 3 - uint8_t what = - (loc.idx[3] == UINT32_MAX ? - (loc.idx[2] == UINT32_MAX ? - (loc.idx[1] != UINT32_MAX ? - idx_indirect_block - : idx_blocks) // loc.idx[1] != UINT32_MAX - : idx_doubly_indirect_block) // loc.idx[2] != UINT32_MAX - : idx_triply_indirect_block); // loc.idx[3] != UINT32_MAX - switch (what) { - case idx_blocks: - block = inode->direct_blocks[loc.idx[0]]; - break; - case idx_indirect_block: - { - if (!inode->indirect_block) - { - block = 0; - break; - } - page* pg2 = nullptr; - uint32_t* indirect_block = ext_read_block(cache, inode->indirect_block, &pg2); - MmH_RefPage(pg2); - block = indirect_block[loc.idx[1]]; - MmH_DerefPage(pg2); - break; - } - case idx_doubly_indirect_block: - { - if (!inode->doubly_indirect_block) - { - block = 0; - break; - } - page* pg2 = nullptr; - uint32_t* doubly_indirect_block = ext_read_block(cache, inode->doubly_indirect_block, &pg2); - MmH_RefPage(pg2); - if (!doubly_indirect_block[loc.idx[2]]) - { - block = 0; - MmH_DerefPage(pg2); - break; - } - uint32_t indirect_block_number = doubly_indirect_block[loc.idx[2]]; - MmH_DerefPage(pg2); - uint32_t* indirect_block = ext_read_block(cache, indirect_block_number, &pg2); - MmH_RefPage(pg2); - block = indirect_block[loc.idx[1]]; - MmH_DerefPage(pg2); - break; - } - case idx_triply_indirect_block: - { - if (!inode->triply_indirect_block) - { - block = 0; - break; - } - page* pg2 = nullptr; - uint32_t* triply_indirect_block = ext_read_block(cache, inode->triply_indirect_block, &pg2); - MmH_RefPage(pg2); - if (!triply_indirect_block[loc.idx[3]]) - { - block = 0; - MmH_DerefPage(pg2); - break; - } - uint32_t doubly_indirect_block_number = triply_indirect_block[loc.idx[2]]; - MmH_DerefPage(pg2); - uint32_t* doubly_indirect_block = ext_read_block(cache, doubly_indirect_block_number, &pg2); - MmH_RefPage(pg2); - if (!doubly_indirect_block[loc.idx[2]]) - { - block = 0; - MmH_DerefPage(pg2); - break; - } - uint32_t indirect_block_number = doubly_indirect_block[loc.idx[2]]; - MmH_DerefPage(pg2); - uint32_t* indirect_block = ext_read_block(cache, indirect_block_number, &pg2); - MmH_RefPage(pg2); - block = indirect_block[loc.idx[1]]; - MmH_DerefPage(pg2); - break; - } - } - Free(EXT_Allocator, inode, sizeof(*inode)); - return block; -} -#undef idx_blocks -#undef idx_indirect_block -#undef idx_doubly_indirect_block -#undef idx_triply_indirect_block - -ext_dirent_cache* ext_dirent_populate(ext_cache* cache, uint32_t ino, const char* parent_name, bool recurse_directories, ext_dirent_cache* parent) -{ - if (!cache || !ino) - return nullptr; - - if (strlen(parent_name) > 255) - return nullptr; - - page* pg = nullptr; - ext_inode* inode = ext_read_inode_pg(cache, ino, &pg); - if (!inode) - return nullptr; - MmH_RefPage(pg); - if (!ext_ino_test_type(inode, EXT2_S_IFDIR)) - { - MmH_DerefPage(pg); - return parent; - } - - if (!parent) - { - parent = ZeroAllocate(EXT_Allocator, 1, sizeof(ext_dirent_cache) + strlen(parent_name), nullptr); - parent->ent.ino = ino; - parent->inode = inode; - MmH_RefPage(pg); - parent->pg = pg; - parent->ent.name_len = strlen(parent_name); - memcpy(parent->ent.name, parent_name, parent->ent.name_len); - } - else if (parent->populated) - { - MmH_DerefPage(pg); - return parent; - } - - size_t nToRead = le32_to_host(inode->blocks) * 512; - uint8_t* buffer = Allocate(EXT_Allocator, nToRead, nullptr); - ext_ino_read_blocks(cache, ino, 0, nToRead, buffer, nullptr); - ext_dirent* ent = nullptr; - for (size_t offset = 0; offset < nToRead; ) - { - ent = (void*)(buffer+offset); - if (!ent->ino) - goto down; - // if (strcmp(ent->name, ".") || strcmp(ent->name, "..")) - // goto down; - - if (!recurse_directories) - { - ext_dirent_cache* ent_cache = ZeroAllocate(EXT_Allocator, 1, sizeof(ext_dirent_cache), nullptr); - ent_cache->ent = *ent; - ent_cache->cache = cache; - ent_cache->ent_block = ext_get_block_at_index(cache, parent->ent.ino, ext_get_blk_index_from_offset(cache, offset)); - ent_cache->ent_offset = offset % cache->block_size; - ent_cache->rel_offset = offset; - ent_cache->inode = ext_read_inode_pg(cache, ent->ino, &ent_cache->pg); - memcpy(ent_cache->ent.name, ent->name, ent->name_len); - ext_dirent_adopt(parent, ent_cache); - } - else - { - bool is_directory = false; - if (cache->revision == 1) - is_directory = ent->file_type == EXT2_FT_DIR; - else - { - ext_inode* child_inode = ext_read_inode(cache, ent->ino); - is_directory = ext_ino_test_type(inode, EXT2_S_IFDIR); - Free(EXT_Allocator, child_inode, sizeof(ext_inode)); - } - - ext_dirent_cache* ent_cache = nullptr; - - if (is_directory) - ent_cache = ext_dirent_populate(cache, ent->ino, ent->name, true, nullptr); - else - { - ent_cache = ZeroAllocate(EXT_Allocator, 1, sizeof(ext_dirent_cache), nullptr); - ent_cache->ent = *ent; - ent_cache->inode = ext_read_inode_pg(cache, ent->ino, &ent_cache->pg); - MmH_RefPage(ent_cache->pg); - memcpy(ent_cache->ent.name, ent->name, ent->name_len); - } - - ent_cache->ent.file_type = ent->file_type; - ent_cache->ent.name_len = ent->name_len; - ent_cache->ent.rec_len = ent->rec_len; - ent_cache->ent_block = ext_get_block_at_index(cache, parent->ent.ino, ext_get_blk_index_from_offset(cache, offset)); - ent_cache->ent_offset = offset % cache->block_size; - ent_cache->rel_offset = offset; - ent_cache->cache = cache; - - ext_dirent_adopt(parent, ent_cache); - } - - down: - offset += ent->rec_len; - if (!ent->rec_len) - { - MmH_DerefPage(pg); - OBOS_Error("extfs: %s: directory corrupted, returning nullptr\n", __func__); - return nullptr; - } - } - - parent->populated = true; - MmH_DerefPage(pg); - return parent; -} - -// Adapated from vfs/dirent.c - -static size_t str_search(const char* str, char ch) -{ - size_t ret = strchr(str, ch); - for (; str[ret] == ch && str[ret] != 0; ret++) - ; - return ret; -} -static ext_dirent_cache* on_match(ext_dirent_cache** const curr_, ext_dirent_cache** const root, const char** const tok, size_t* const tok_len, const char** const path, - size_t* const path_len) -{ - ext_dirent_cache *curr = *curr_; - curr = ext_dirent_populate(curr->cache, curr->ent.ino, curr->ent.name, false, curr); - *root = curr; - const char *newtok = (*tok) + str_search(*tok, '/'); - if (newtok >= (*path + *path_len)) - return curr; - if (!curr->children.nChildren) - return nullptr; // could not find node. - *tok = newtok; - size_t currentPathLen = strlen(*tok)-1; - if ((*tok)[currentPathLen] != '/') - currentPathLen++; - while ((*tok)[currentPathLen] == '/') - currentPathLen--; - *tok_len = strchr(*tok, '/'); - if (*tok_len != currentPathLen) - (*tok_len)--; - while ((*tok)[(*tok_len) - 1] == '/') - (*tok_len)--; - return nullptr; -} -ext_dirent_cache* ext_dirent_lookup_from(const char* path, ext_dirent_cache* root) -{ - if (!path) - return root; - size_t path_len = strlen(path); - if (!path_len) - return root; - for (; *path == '/'; path++, path_len--) - ; - const char* tok = path; - size_t tok_len = strchr(tok, '/'); - if (tok_len != path_len) - tok_len--; - while (tok[tok_len - 1] == '/') - tok_len--; - if (!tok_len) - return nullptr; - while(root) - { - ext_dirent_cache* curr = root; - { - string name = {}; - name.cap = 33; - name.ls = root->ent.name; - name.len = root->ent.name_len; - if (OBOS_CompareStringNC(&name, tok, tok_len)) - { - root = curr->children.head; - continue; - } - } - for (curr = root->children.head; curr;) - { - string name = {}; - name.cap = 33; - name.ls = curr->ent.name; - name.len = curr->ent.name_len; - if (OBOS_CompareStringNC(&name, tok, tok_len)) - { - // Match! - ext_dirent_cache* what = - on_match(&curr, &root, &tok, &tok_len, &path, &path_len); - if (what) - return what; - curr = curr->children.head ? curr->children.head : curr; - break; - } - - // root = curr->children.head ? curr->children.head : root; - curr = curr->next; - } - if (!curr) - root = root->parent; - } - return nullptr; -} - -void ext_dirent_flush(ext_cache* cache, ext_dirent_cache* ent) -{ - if (!cache || !ent) - return; - page* pg = nullptr; - void* block = ext_read_block(cache, ent->ent_block, &pg); - MmH_RefPage(pg); - memcpy((char*)block + ent->ent_offset, &ent->ent, sizeof(ent->ent)); - Mm_MarkAsDirtyPhys(pg); - MmH_DerefPage(pg); -} \ No newline at end of file diff --git a/src/drivers/generic/extfs/helper.c b/src/drivers/generic/extfs/helper.c deleted file mode 100644 index 63a71ffe..00000000 --- a/src/drivers/generic/extfs/helper.c +++ /dev/null @@ -1,962 +0,0 @@ -/* - * drivers/generic/extfs/helper.c - * - * Copyright (c) 2025 Omar Berrow - * - * Abandon all hope, ye who enter here. -*/ - -#include -#include -#include -#include -#include - -#include -#include - -#include - -#include -#include - -#include - -#include "structs.h" - -ext_inode* ext_read_inode_pg(ext_cache* cache, uint32_t ino, page** pg) -{ - if (!cache || !ino || !pg) - return nullptr; - if (ext_ino_get_block_group(cache, ino) > cache->block_group_count) - return nullptr; - ext_bgd* bgd = &cache->bgdt[ext_ino_get_block_group(cache, ino)]; - uint32_t local_inode_index = ext_ino_get_local_index(cache, ino); - uint32_t inode_table_block = le32_to_host(bgd->inode_table) + local_inode_index / cache->inodes_per_block; - uint32_t real_inode_index = (local_inode_index % cache->inodes_per_block); - uint32_t inode_bitmap_block = le32_to_host(bgd->inode_bitmap) + (local_inode_index / 8) / (cache->inodes_per_group/8); - uint32_t bmp_idx = (local_inode_index / 8) % (cache->inodes_per_group/8); - - bool free = false; - page* pg2 = nullptr; - uint8_t* inode_bitmap = ext_read_block(cache, inode_bitmap_block, &pg2); - MmH_RefPage(pg2); - if (~inode_bitmap[bmp_idx] & BIT(local_inode_index % 8)) - free = true; - MmH_DerefPage(pg2); - if (free) - return nullptr; - - ext_inode* inodes = ext_read_block(cache, inode_table_block, pg); - return (ext_inode*)((uintptr_t)inodes + real_inode_index * cache->inode_size); -} - -ext_inode* ext_read_inode(ext_cache* cache, uint32_t ino) -{ - page* pg = nullptr; - ext_inode* read = ext_read_inode_pg(cache, ino, &pg); - if (!read) - return read; - MmH_RefPage(pg); - ext_inode* ret = ZeroAllocate(EXT_Allocator, 1, sizeof(ext_inode), nullptr); - memcpy(ret, read, sizeof(ext_inode)); - MmH_DerefPage(pg); - return ret; -} - -static iterate_decision ext_ino_foreach_indirect_block(ext_cache* cache, - ext_inode* inode, - iterate_decision(*cb)(ext_cache* cache, ext_inode* inode, uint32_t *block, void* userdata), - void* userdata, - uint32_t *blocks, size_t *curr_index, bool *dirty) -{ - if (!blocks) return ITERATE_DECISION_STOP; - size_t nEntriesPerBlock = cache->block_size / 4; - size_t i = 0; - for (; i < OBOS_MIN(ext_ino_max_block_index(cache, inode) - 12, nEntriesPerBlock); i++) - { - uint32_t blk = blocks ? blocks[i] : 0; - if (cb(cache, inode, blocks ? &blocks[i] : 0, userdata) == ITERATE_DECISION_STOP) - { - if (!(*dirty)) - *dirty = blocks ? blocks[i] != blk : 0; - break; - } - if (!(*dirty)) - *dirty = blocks ? blocks[i] != blk : 0; - (*curr_index)++; - if ((*curr_index) >= ext_ino_max_block_index(cache, inode)) - break; - } - return ITERATE_DECISION_CONTINUE; -} - -static iterate_decision ext_ino_foreach_doubly_indirect_block(ext_cache* cache, - ext_inode* inode, - iterate_decision(*cb)(ext_cache* cache, ext_inode* inode, uint32_t *block, void* userdata), - void* userdata, - uint32_t *blocks, size_t *curr_index) -{ - if (!blocks) return ITERATE_DECISION_STOP; - size_t nEntriesPerBlock = cache->block_size / 4, i = 0; - size_t maxBlockIndexDouble = ext_ino_max_block_index(cache, inode) / nEntriesPerBlock; - if (ext_ino_max_block_index(cache, inode) % nEntriesPerBlock) - maxBlockIndexDouble++; - for (; i < OBOS_MIN(maxBlockIndexDouble, nEntriesPerBlock); i++) - { - page* pg = nullptr; - uint32_t* indirect_blocks = nullptr; - if (blocks) - indirect_blocks = blocks[i] ? ext_read_block(cache, le32_to_host(blocks[i]), &pg) : nullptr; - else - indirect_blocks = nullptr; - MmH_RefPage(pg); - bool dirty = false; - if (ext_ino_foreach_indirect_block(cache,inode,cb,userdata,indirect_blocks,curr_index, &dirty) == ITERATE_DECISION_STOP) - { - MmH_DerefPage(pg); - return ITERATE_DECISION_STOP; - } - if (dirty) - Mm_MarkAsDirtyPhys(pg); - MmH_DerefPage(pg); - if (*curr_index >= ext_ino_max_block_index(cache, inode)) - return ITERATE_DECISION_STOP; - } - return ITERATE_DECISION_CONTINUE; -} - -static void ext_ino_foreach_triply_indirect_block(ext_cache* cache, - ext_inode* inode, - iterate_decision(*cb)(ext_cache* cache, ext_inode* inode, uint32_t *block, void* userdata), - void* userdata, - uint32_t *blocks, size_t *curr_index) -{ - if (!blocks) return; - size_t nEntriesPerBlock = cache->block_size / 4, i = 0; - size_t maxBlockIndexTriple = ext_ino_max_block_index(cache, inode) / nEntriesPerBlock / nEntriesPerBlock; - if ((ext_ino_max_block_index(cache, inode) / nEntriesPerBlock) % nEntriesPerBlock) - maxBlockIndexTriple++; - for (; i < OBOS_MIN(maxBlockIndexTriple, nEntriesPerBlock); i++) - { - page* pg = nullptr; - uint32_t* doubly_indirect_blocks = blocks[i] ? ext_read_block(cache, le32_to_host(blocks[i]), &pg) : nullptr; - MmH_RefPage(pg); - if (ext_ino_foreach_doubly_indirect_block(cache,inode,cb,userdata,doubly_indirect_blocks,curr_index) == ITERATE_DECISION_STOP) - { - MmH_DerefPage(pg); - return; - } - MmH_DerefPage(pg); - if (*curr_index >= ext_ino_max_block_index(cache, inode)) - return; - } -} - -void ext_ino_foreach_block(ext_cache* cache, - uint32_t ino, - iterate_decision(*cb)(ext_cache* cache, ext_inode* inode, uint32_t *block, void* userdata), - void* userdata) -{ - if (!cache || !ino || !cb) - return; - - page* pg = nullptr; - - ext_inode* inode = ext_read_inode_pg(cache, ino, &pg); - MmH_RefPage(pg); - - size_t i = 0; - for (; i < OBOS_MIN(ext_ino_max_block_index(cache, inode), (size_t)12); i++) - { - uint32_t blk = inode->direct_blocks[i]; - bool exit = (cb(cache, inode, &inode->direct_blocks[i], userdata) == ITERATE_DECISION_STOP); - if (blk != inode->direct_blocks[i]) - Mm_MarkAsDirtyPhys(pg); - if (exit) - { - MmH_DerefPage(pg); - return; // We done here - } - } - - if (i >= ext_ino_max_block_index(cache, inode)) - { - MmH_DerefPage(pg); - return; // We done here - } - - page* pg2 = nullptr; - uint32_t* indirect_blocks = inode->indirect_block == 0 ? nullptr : ext_read_block(cache, le32_to_host(inode->indirect_block), &pg2); - MmH_RefPage(pg2); - bool dirty = false; - if (ext_ino_foreach_indirect_block(cache,inode,cb,userdata,indirect_blocks,&i,&dirty) == ITERATE_DECISION_STOP) - { - if (dirty) - Mm_MarkAsDirtyPhys(pg2); - MmH_DerefPage(pg2); - MmH_DerefPage(pg); - return; // We done here - } - if (dirty) - Mm_MarkAsDirtyPhys(pg2); - MmH_DerefPage(pg2); - - if (i >= ext_ino_max_block_index(cache, inode)) - { - MmH_DerefPage(pg); - return; // We done here - } - - pg2 = nullptr; - uint32_t* doubly_indirect_blocks = - inode->doubly_indirect_block ? ext_read_block(cache, le32_to_host(inode->doubly_indirect_block), &pg2) : - nullptr; - MmH_RefPage(pg2); - if (ext_ino_foreach_doubly_indirect_block(cache,inode,cb,userdata,doubly_indirect_blocks,&i) == ITERATE_DECISION_STOP) - { - MmH_DerefPage(pg2); - MmH_DerefPage(pg); - return; // We done here - } - MmH_DerefPage(pg2); - - if (i >= ext_ino_max_block_index(cache, inode)) - { - MmH_DerefPage(pg); - return; // We done here - } - - pg2 = nullptr; - uint32_t* triply_indirect_blocks = inode->triply_indirect_block ? - ext_read_block(cache, le32_to_host(inode->triply_indirect_block), &pg2) : - nullptr; - MmH_RefPage(pg2); - ext_ino_foreach_triply_indirect_block(cache,inode,cb,userdata,triply_indirect_blocks,&i); - MmH_DerefPage(pg2); -} - -struct read_block_packet { - uint32_t* blocks; - size_t count; - size_t start_offset; - size_t current_offset; - obos_status status; - void* buffer; - size_t buffer_offset; -}; - -static iterate_decision read_cb(ext_cache* cache, ext_inode* inode, uint32_t *block, void* userdata) -{ - OBOS_UNUSED(inode); - struct read_block_packet *packet = userdata; - iterate_decision decision = ITERATE_DECISION_CONTINUE; - if (packet->current_offset < packet->start_offset) - goto out1; - if (packet->current_offset > (packet->start_offset + packet->count)) - { - decision = ITERATE_DECISION_STOP; - goto out1; - } - packet->blocks[(packet->current_offset - packet->start_offset) / cache->block_size] = block ? *block : UINT32_MAX; - if (!packet->blocks[(packet->current_offset - packet->start_offset) / cache->block_size]) - packet->blocks[(packet->current_offset - packet->start_offset) / cache->block_size] = UINT32_MAX; - - out1: - packet->current_offset += cache->block_size; - return decision; -} - -static iterate_decision sync_read_cb(ext_cache* cache, ext_inode* inode, uint32_t *block, void* userdata) -{ - OBOS_UNUSED(inode); - struct read_block_packet *packet = userdata; - iterate_decision decision = ITERATE_DECISION_CONTINUE; - if (packet->current_offset < packet->start_offset) - goto out1; - if (packet->current_offset > (packet->start_offset + packet->count)) - { - decision = ITERATE_DECISION_STOP; - goto out1; - } - void* const buff = (void*)((uintptr_t)packet->buffer + packet->buffer_offset); - if (!block || !(*block)) - memzero(buff, cache->block_size); // sparse block - else - { - page* pg = nullptr; - void* data = ext_read_block(cache, *block, &pg); - MmH_RefPage(pg); - memcpy(buff, data, OBOS_MIN(packet->count - packet->buffer_offset, cache->block_size)); - MmH_DerefPage(pg); - } - packet->buffer_offset += cache->block_size; - - out1: - packet->current_offset += cache->block_size; - return decision; -} - -obos_status ext_ino_read_blocks(ext_cache* cache, uint32_t ino, size_t offset, size_t count, void* buffer, size_t *nRead) -{ - if (!cache || !ino || !buffer) - return OBOS_STATUS_INVALID_ARGUMENT; - page* pg = nullptr; - ext_inode* inode = ext_read_inode_pg(cache, ino, &pg); - MmH_RefPage(pg); - if (!inode) - return OBOS_STATUS_INVALID_ARGUMENT; - if ((offset + count) > inode->blocks*512) - count = (offset + count) - inode->blocks*512; - // qemu segfaults with async block reads - if (OBOS_GetOPTF("extfs-disable-async-block-read")) - { - struct read_block_packet userdata = {.buffer=buffer,.start_offset=offset,.count=count,.buffer_offset=0}; - ext_ino_foreach_block(cache, ino, sync_read_cb, &userdata); - MmH_DerefPage(pg); - return userdata.status; - } - uint32_t nBlocks = count / cache->block_size; - uint32_t* blocks = ZeroAllocate(OBOS_KernelAllocator, nBlocks, sizeof(uint32_t), nullptr); - struct read_block_packet userdata = {.blocks=blocks,.start_offset=offset,.count=count}; - ext_ino_foreach_block(cache, ino, read_cb, &userdata); - MmH_DerefPage(pg); - uint32_t current_block_base = 0; - uint32_t current_block_index = 0; - uint32_t current_block_count = 0; - bool read_current = false; - irp** irps = nullptr; - size_t nIrps = 0; - bool refresh_next = true; - for (uint32_t i = 0; i < nBlocks; i++) - { - if (blocks[i] == UINT32_MAX) - { - // TODO: Make that asynchronous? - memzero((void*)((uintptr_t)buffer + i*cache->block_size), cache->block_size); - refresh_next = true; - continue; - } - if (refresh_next) - { - read_current = false; - refresh_next = false; - current_block_base = blocks[i]; - ++current_block_count; - current_block_index = i; - continue; - } - if (blocks[i] == (current_block_base + current_block_count)) - { - read_current = false; - ++current_block_count; - continue; - } - irp* req = VfsH_IRPAllocate(); - VfsH_IRPBytesToBlockCount(cache->vn, current_block_count*cache->block_size, &req->blkCount); - VfsH_IRPBytesToBlockCount(cache->vn, current_block_base*cache->block_size, &req->blkOffset); - req->vn = cache->vn; - req->buff = (void*)((uintptr_t)buffer + current_block_index*cache->block_size); - req->op = IRP_READ; - VfsH_IRPSubmit(req, nullptr); - read_current = true; - if (req->evnt && !Core_EventGetState(req->evnt)) - { - size_t oldsize = nIrps*sizeof(irp*); - nIrps++; - irps = Reallocate(OBOS_KernelAllocator, irps, nIrps*sizeof(irp*), oldsize, nullptr); - irps[nIrps-1] = req; - } - else - { - VfsH_IRPWait(req); - VfsH_IRPUnref(req); - } - if (blocks[i] == UINT32_MAX) - continue; - current_block_index = i; - current_block_base = blocks[i]; - current_block_count++; - } - if (!read_current) - { - irp* req = VfsH_IRPAllocate(); - VfsH_IRPBytesToBlockCount(cache->vn, current_block_count*cache->block_size, &req->blkCount); - VfsH_IRPBytesToBlockCount(cache->vn, current_block_base*cache->block_size, &req->blkOffset); - req->vn = cache->vn; - req->buff = (void*)((uintptr_t)buffer + current_block_index*cache->block_size); - req->op = IRP_READ; - VfsH_IRPSubmit(req, nullptr); - read_current = true; - if (req->evnt && !Core_EventGetState(req->evnt)) - { - size_t oldsize = nIrps*sizeof(irp*); - nIrps++; - irps = Reallocate(OBOS_KernelAllocator, irps, nIrps*sizeof(irp*), oldsize, nullptr); - irps[nIrps-1] = req; - } - else - { - VfsH_IRPWait(req); - VfsH_IRPUnref(req); - } - } - for (size_t i = 0; i < nIrps; i++) - { - VfsH_IRPWait(irps[i]); - VfsH_IRPUnref(irps[i]); - irps[i] = nullptr; - } - Free(OBOS_KernelAllocator, irps, nIrps*sizeof(irp*)); - - Free(OBOS_KernelAllocator, blocks, nBlocks*sizeof(uint32_t)); - if (nRead) - *nRead = count; - return userdata.status; -} - -struct commit_blks_packet -{ - // in blocks - uint32_t minimum_offset; - uint32_t maximum_offset; - uint32_t current_offset; - struct { - uint32_t* arr; - size_t cnt; - } offsets_to_commit; -}; - - -struct write_blks_packet -{ - // in blocks - uint32_t minimum_offset; - uint32_t maximum_offset; - uint32_t current_offset; - uint32_t block_group; - const void* buff; - size_t buff_sz; - size_t buff_offset; - uint32_t initial_offset; -}; - -iterate_decision write_blks_cb(ext_cache* cache, ext_inode* inode, uint32_t* block, void* userdata) -{ - OBOS_UNUSED(cache && inode); - struct write_blks_packet* packet = userdata; - uint32_t current_offset = packet->current_offset++; - if (current_offset < packet->minimum_offset) - return ITERATE_DECISION_CONTINUE; - if (current_offset >= packet->maximum_offset) - return ITERATE_DECISION_STOP; - if (!block || !(*block)) - { - OBOS_Warning("extfs: skipping write of uncommitted block\n"); - return ITERATE_DECISION_CONTINUE; - } - - page* pg = nullptr; - char* data = ext_read_block(cache, *block, &pg); - MmH_RefPage(pg); - size_t copy_count = OBOS_MIN(packet->buff_sz-packet->buff_offset, cache->block_size); -#if OBOS_DEBUG - for (size_t i = 0; i < cache->block_group_count; i++) - { - OBOS_ASSERT(cache->bgdt[i].inode_bitmap != *block); - OBOS_ASSERT(cache->bgdt[i].block_bitmap != *block); - } -#endif - memcpy(data+(current_offset == packet->minimum_offset ? packet->initial_offset : 0), - (char*)packet->buff+packet->buff_offset, - copy_count); - packet->buff_offset += copy_count; - Mm_MarkAsDirtyPhys(pg); - MmH_DerefPage(pg); - - return ITERATE_DECISION_CONTINUE; -} - -obos_status ext_ino_write_blocks(ext_cache* cache, uint32_t ino, size_t offset, size_t count, const void* buffer, size_t *nWritten) -{ - if (!cache || !ino || !buffer) - return OBOS_STATUS_INVALID_ARGUMENT; - if (cache->read_only) - return OBOS_STATUS_READ_ONLY; - - page* pg = nullptr; - ext_inode* inode = ext_read_inode_pg(cache, ino, &pg); - MmH_RefPage(pg); - size_t inode_size = inode->size; -#if ext_sb_supports_64bit_filesize - inode_size |= ((uint64_t)inode->dir_acl << 32); -#endif - MmH_DerefPage(pg); - if ((offset+count) > inode_size) - return OBOS_STATUS_NO_SPACE; - - struct write_blks_packet packet = { - .block_group = ext_ino_get_block_group(cache, ino), - .minimum_offset=offset/cache->block_size, - .maximum_offset=offset/cache->block_size+(count/cache->block_size + (count%cache->block_size ? 1 : 0)), - .buff = buffer, - .buff_sz = count, - .initial_offset = offset % cache->block_size - }; - ext_ino_foreach_block(cache, ino, write_blks_cb, &packet); - - if (nWritten) - *nWritten = packet.buff_offset; - - return OBOS_STATUS_SUCCESS; -} - -iterate_decision commit_blks_cb(ext_cache* cache, ext_inode* inode, uint32_t* block, void* userdata) -{ - OBOS_UNUSED(cache && inode); - struct commit_blks_packet* packet = userdata; - uint32_t current_offset = packet->current_offset++; - if (current_offset < packet->minimum_offset) - return ITERATE_DECISION_CONTINUE; - if (current_offset >= packet->maximum_offset) - return ITERATE_DECISION_STOP; - if (block && *block) - return ITERATE_DECISION_CONTINUE; - size_t old_size = packet->offsets_to_commit.cnt++ * sizeof(uint32_t); - size_t new_size = packet->offsets_to_commit.cnt * sizeof(uint32_t); - packet->offsets_to_commit.arr = Reallocate(EXT_Allocator, packet->offsets_to_commit.arr, new_size, old_size, nullptr); - packet->offsets_to_commit.arr[packet->offsets_to_commit.cnt-1] = current_offset; - return ITERATE_DECISION_CONTINUE; -} - -obos_status ext_ino_commit_blocks(ext_cache* cache, uint32_t ino, size_t offset, size_t size) -{ - if (!cache || !ino) - return OBOS_STATUS_INVALID_ARGUMENT; - if (!size) - return OBOS_STATUS_SUCCESS; - if (cache->read_only) - return OBOS_STATUS_READ_ONLY; - - page* pg = nullptr; - ext_inode* inode = ext_read_inode_pg(cache, ino, &pg); - MmH_RefPage(pg); - bool inode_dirty = false; - - uint32_t block_group = ext_ino_get_block_group(cache, ino); - struct commit_blks_packet packet = { - .minimum_offset=offset/cache->block_size, - .maximum_offset=offset/cache->block_size+(size/cache->block_size + (size%cache->block_size ? 1 : 0)) - }; - ext_ino_foreach_block(cache, ino, commit_blks_cb, &packet); - for (size_t i = 0; i < packet.offsets_to_commit.cnt; i++) - { - struct inode_offset_location loc = ext_get_blk_index_from_offset(cache, packet.offsets_to_commit.arr[i] * cache->block_size); - uint32_t block = ext_blk_allocate(cache, &block_group); -#define idx_blocks 0 -#define idx_indirect_block 1 -#define idx_doubly_indirect_block 2 -#define idx_triply_indirect_block 3 - uint8_t what = - (loc.idx[3] == UINT32_MAX ? - (loc.idx[2] == UINT32_MAX ? - (loc.idx[1] != UINT32_MAX ? - idx_indirect_block - : idx_blocks) // loc.idx[1] != UINT32_MAX - : idx_doubly_indirect_block) // loc.idx[2] == UINT32_MAX - : idx_triply_indirect_block); // loc.idx[3] == UINT32_MAX - switch (what) { - case idx_blocks: - inode->direct_blocks[loc.idx[0]] = block; - inode_dirty = true; - break; - case idx_indirect_block: - { - if (!inode->indirect_block) - { - inode->indirect_block = ext_blk_allocate(cache, &block_group); - inode->blocks += (cache->block_size/512); - inode_dirty = true; - } - page* pg2 = nullptr; - uint32_t* indirect_block = ext_read_block(cache, inode->indirect_block, &pg2); - MmH_RefPage(pg2); - indirect_block[loc.idx[1]] = block; - Mm_MarkAsDirtyPhys(pg2); - MmH_DerefPage(pg2); - break; - } - case idx_doubly_indirect_block: - { - if (!inode->doubly_indirect_block) - { - inode->doubly_indirect_block = ext_blk_allocate(cache, &block_group); - inode->blocks += (cache->block_size/512); - inode_dirty = true; - } - page* pg2 = nullptr; - uint32_t* doubly_indirect_block = ext_read_block(cache, inode->doubly_indirect_block, &pg2); - MmH_RefPage(pg2); - if (!doubly_indirect_block[loc.idx[2]]) - { - doubly_indirect_block[loc.idx[2]] = ext_blk_allocate(cache, &block_group); - inode->blocks += (cache->block_size/512); - inode_dirty = true; - Mm_MarkAsDirtyPhys(pg2); - } - uint32_t indirect_block_number = doubly_indirect_block[loc.idx[2]]; - MmH_DerefPage(pg2); - uint32_t* indirect_block = ext_read_block(cache, indirect_block_number, &pg2); - indirect_block[loc.idx[1]] = block; - Mm_MarkAsDirtyPhys(pg2); - MmH_DerefPage(pg2); - break; - } - case idx_triply_indirect_block: - { - if (!inode->triply_indirect_block) - { - inode->triply_indirect_block = ext_blk_allocate(cache, &block_group); - inode->blocks += (cache->block_size/512); - inode_dirty = true; - } - page* pg2 = nullptr; - uint32_t* triply_indirect_block = ext_read_block(cache, inode->triply_indirect_block, &pg2); - MmH_RefPage(pg2); - if (!triply_indirect_block[loc.idx[3]]) - { - triply_indirect_block[loc.idx[3]] = ext_blk_allocate(cache, &block_group); - inode->blocks += (cache->block_size/512); - inode_dirty = true; - Mm_MarkAsDirtyPhys(pg2); - } - uint32_t doubly_indirect_block_number = triply_indirect_block[loc.idx[2]]; - MmH_DerefPage(pg2); - uint32_t* doubly_indirect_block = ext_read_block(cache, doubly_indirect_block_number, &pg2); - MmH_RefPage(pg2); - if (!doubly_indirect_block[loc.idx[2]]) - { - doubly_indirect_block[loc.idx[2]] = ext_blk_allocate(cache, &block_group); - inode->blocks += (cache->block_size/512); - inode_dirty = true; - Mm_MarkAsDirtyPhys(pg2); - } - uint32_t indirect_block_number = doubly_indirect_block[loc.idx[2]]; - MmH_DerefPage(pg2); - uint32_t* indirect_block = ext_read_block(cache, indirect_block_number, &pg2); - MmH_RefPage(pg2); - indirect_block[loc.idx[1]] = block; - Mm_MarkAsDirtyPhys(pg2); - MmH_DerefPage(pg2); - break; - } - } - } - - if (inode_dirty) - Mm_MarkAsDirtyPhys(pg); - MmH_DerefPage(pg); - - return OBOS_STATUS_SUCCESS; -} - -#undef idx_blocks -#undef idx_indirect_block -#undef idx_doubly_indirect_block -#undef idx_triply_indirect_block - -obos_status ext_ino_resize(ext_cache* cache, uint32_t ino, size_t new_size, bool expand_only) -{ - if (!cache || !ino) - return OBOS_STATUS_INVALID_ARGUMENT; - if (cache->read_only) - return OBOS_STATUS_READ_ONLY; - - page* pg = nullptr; - ext_inode* inode = ext_read_inode_pg(cache, ino, &pg); - MmH_RefPage(pg); - OBOS_ASSERT(inode); - if (!inode) - return OBOS_STATUS_INTERNAL_ERROR; - size_t inode_size = inode->size; -#if ext_sb_supports_64bit_filesize - inode_size |= ((uint64_t)inode->dir_acl << 32); -#endif - - int32_t blocks_diff = 0; - do { - size_t old_size = inode_size, rnew_size = new_size; - old_size += (cache->block_size-1); - old_size &= ~(cache->block_size-1); - rnew_size += (cache->block_size-1); - rnew_size &= ~(cache->block_size-1); - blocks_diff = rnew_size - old_size; - blocks_diff /= 512; - } while(0); - - if (new_size == inode_size) - { - MmH_DerefPage(pg); - return OBOS_STATUS_SUCCESS; - } - if (expand_only && new_size < inode_size) - { - MmH_DerefPage(pg); - return OBOS_STATUS_SUCCESS; - } - - inode->blocks += blocks_diff; - inode->size = new_size & 0xffffffff; -#if ext_sb_supports_64bit_filesize - inode->dir_acl = new_size >> 32; -#endif - - Mm_MarkAsDirtyPhys(pg); - MmH_DerefPage(pg); - - return OBOS_STATUS_SUCCESS; -} - -char* ext_ino_get_linked(ext_cache* cache, ext_inode* inode, uint32_t ino_num) -{ - if (!cache || !inode || !ino_num) - return nullptr; - int ea_blocks = le32_to_host(inode->file_acl) ? (cache->block_size>>9) : 0; - bool fast_symlink = !(le32_to_host(inode->blocks) - ea_blocks); - char* ret = nullptr; - if (fast_symlink) - { - size_t str_len = strnlen((char*)&inode->direct_blocks, 60); - ret = Allocate(EXT_Allocator, str_len+1, nullptr); - memcpy(ret, (char*)&inode->direct_blocks, str_len); - ret[str_len] = 0; - } - else - { - // Read blocks - void* buff = Allocate(EXT_Allocator, le32_to_host(inode->blocks) * 512, nullptr); - ext_ino_read_blocks(cache, ino_num, 0, le32_to_host(inode->blocks) * 512, buff, nullptr); - size_t str_len = strnlen(buff, le32_to_host(inode->blocks) * 512); - ret = Reallocate(EXT_Allocator, buff, str_len+1, le32_to_host(inode->blocks) * 512, nullptr); - ret[str_len] = 0; - } - return ret; -} - -struct inode_offset_location ext_get_blk_index_from_offset(ext_cache* cache, size_t offset) -{ - struct inode_offset_location loc = {.offset=offset,.idx={}}; - memset(loc.idx, 0xff, sizeof(loc.idx)); - if (offset < (cache->block_size*12)) - { - loc.idx[0] = offset / cache->block_size; - return loc; - } - offset -= (cache->block_size*12); - const size_t nBlocksPerIndirectBlock = cache->block_size / 4; - loc.idx[1] = (offset % (cache->block_size*nBlocksPerIndirectBlock)) / cache->block_size; - if (offset < (cache->block_size*nBlocksPerIndirectBlock)) - return loc; - offset -= (cache->block_size*nBlocksPerIndirectBlock); - const size_t nBlocksPerDoublyIndirectBlock = nBlocksPerIndirectBlock*nBlocksPerIndirectBlock; - loc.idx[2] = (offset % (cache->block_size*nBlocksPerDoublyIndirectBlock)) / cache->block_size; - if (offset < (cache->block_size*nBlocksPerDoublyIndirectBlock)) - return loc; - offset -= (cache->block_size*nBlocksPerDoublyIndirectBlock); - const size_t nBlocksPerTriplyIndirectBlock = nBlocksPerDoublyIndirectBlock*nBlocksPerIndirectBlock; - loc.idx[3] = (offset % (cache->block_size*nBlocksPerTriplyIndirectBlock)) / cache->block_size; - return loc; -} - -enum { - ALLOC_INODE = false, - ALLOC_BLOCK = true, -}; - -uint32_t allocate_bmp(ext_cache* cache, const uint32_t* block_group_ptr, int what) -{ - if (!cache) - return 0; - uint32_t block_group = block_group_ptr ? *block_group_ptr : 0; - bool checked_bg_zero = block_group == 0; - if (block_group > cache->block_group_count) - return 0; - uint32_t res = 0; - while (!res) - { - if (!(what == ALLOC_INODE ? cache->bgdt[block_group].free_inodes : cache->bgdt[block_group].free_blocks)) - goto down; - - uint32_t local_index = 0; - size_t bitmap_size = what == ALLOC_INODE ? cache->inodes_per_block / 8 : cache->blocks_per_group / 8; - uint8_t* buffer = nullptr; - uint32_t block = 0; - page* pg = nullptr; - bool found = false; - for (size_t i = 0; i < bitmap_size; i++) - { - if ((i % cache->block_size) == 0) - { - if (pg) - MmH_DerefPage(pg); - uint32_t bmp = what == ALLOC_INODE ? cache->bgdt[block_group].inode_bitmap : cache->bgdt[block_group].block_bitmap; - block = bmp + (i / cache->block_size); - buffer = ext_read_block(cache, block, &pg); - MmH_RefPage(pg); - } - OBOS_ENSURE(buffer); - if (buffer[i] != 0xff) - { - uint8_t bit = __builtin_ctz(~buffer[i]); - local_index = bit + i*8; - found = true; - buffer[i] |= BIT(bit); - Mm_MarkAsDirtyPhys(pg); - MmH_DerefPage(pg); - break; - } - } - if (found) - { - res = local_index + (what==ALLOC_INODE ? cache->inodes_per_group : cache->blocks_per_group) * block_group; - if (what == ALLOC_INODE) - res++; - continue; - } - - down: - if (++block_group >= cache->block_group_count) - { - if (checked_bg_zero) - break; - else - { - block_group = 0; - checked_bg_zero = true; - } - } - } - - return res; -} - -uint32_t ext_ino_allocate(ext_cache* cache, const uint32_t* block_group_ptr) -{ - uint32_t ret = allocate_bmp(cache, block_group_ptr, ALLOC_INODE); - if (ret) - { - ext_bgd* bgd = &cache->bgdt[ext_ino_get_block_group(cache, ret)]; - bgd->free_inodes--; - cache->superblock.free_inode_count--; - ext_writeback_sb(cache); - ext_writeback_bgd(cache, ext_ino_get_block_group(cache, ret)); - } - return ret; -} - -#ifdef __x86_64__ -# include -#endif - -static long get_current_epoch_time() -{ - long current_time = 0; -#ifdef __x86_64__ - Arch_CMOSGetEpochTime(¤t_time); -#endif - return current_time; -} - -void ext_ino_free(ext_cache* cache, uint32_t ino) -{ - if (!cache || !ino) - return; - - page* pg = nullptr; - ext_inode* inode = ext_read_inode_pg(cache, ino, &pg); - MmH_RefPage(pg); - memzero(inode, sizeof(*inode)); - inode->delete_time = get_current_epoch_time(); - Mm_MarkAsDirtyPhys(pg); - MmH_DerefPage(pg); - - pg = nullptr; - - ext_bgd* bgd = &cache->bgdt[ext_ino_get_block_group(cache, ino)]; - uint32_t local_inode_index = ext_ino_get_local_index(cache, ino); - uint32_t inode_bitmap_block = le32_to_host(bgd->inode_bitmap) + (local_inode_index / 8) / (cache->inodes_per_group/8); - uint32_t idx = (local_inode_index / 8) % (cache->inodes_per_group/8); - - bool free = false; - uint8_t* inode_bitmap = ext_read_block(cache, inode_bitmap_block, &pg); - MmH_RefPage(pg); - if (~inode_bitmap[idx] & BIT(local_inode_index % 8)) - free = true; - else - { - Mm_MarkAsDirtyPhys(pg); - inode_bitmap[idx] &= ~BIT(local_inode_index % 8); - } - MmH_DerefPage(pg); - if (free) - return; - - bgd->free_inodes++; - cache->superblock.free_inode_count++; - ext_writeback_sb(cache); - ext_writeback_bgd(cache, ext_ino_get_block_group(cache, ino)); -} - -uint32_t ext_blk_allocate(ext_cache* cache, const uint32_t* block_group_ptr) -{ - uint32_t blk = allocate_bmp(cache, block_group_ptr, ALLOC_BLOCK); - ext_bgd* bgd = &cache->bgdt[blk/cache->blocks_per_group]; - bgd->free_blocks--; - cache->superblock.free_block_count--; - ext_writeback_sb(cache); - ext_writeback_bgd(cache, blk/cache->blocks_per_group); - return blk; -} - -void ext_blk_free(ext_cache* cache, uint32_t blk) -{ - ext_bgd* bgd = &cache->bgdt[blk/cache->blocks_per_group]; - uint32_t local_index = blk%cache->blocks_per_group; - uint32_t bmp_block = le32_to_host(bgd->block_bitmap) + local_index / (cache->block_size * 8); - uint32_t bit = (local_index % (cache->block_size * 8)) % 8; - uint32_t idx = (local_index % (cache->block_size * 8)) / 8; - - page* pg = nullptr; - uint8_t* bmp = ext_read_block(cache, bmp_block, &pg); - MmH_RefPage(pg); - if (bmp[idx] & BIT(bit)) - { - bmp[idx] &= ~BIT(bit); - Mm_MarkAsDirtyPhys(pg); - } - MmH_DerefPage(pg); - - bgd->free_blocks++; - cache->superblock.free_block_count++; - ext_writeback_sb(cache); - ext_writeback_bgd(cache, blk/cache->blocks_per_group); -} - -void ext_writeback_bgd(ext_cache* cache, uint32_t bgd_idx) -{ - page* pg = nullptr; - ext_bgdt bgdt_section = ext_read_block(cache, (cache->block_size == 1024 ? 2 : 1) + (bgd_idx / (cache->block_size / sizeof(ext_bgd))), &pg); - MmH_RefPage(pg); - bgdt_section[bgd_idx % (cache->block_size / sizeof(ext_bgd))] = cache->bgdt[bgd_idx]; - Mm_MarkAsDirtyPhys(pg); - MmH_DerefPage(pg); -} -void ext_writeback_sb(ext_cache* cache) -{ - page* pg = nullptr; - void* sb = ext_read_block(cache, (cache->block_size == 1024 ? 1 : 0), &pg); - MmH_RefPage(pg); - memcpy(sb+(cache->block_size == 1024 ? 0 : 1024), &cache->superblock, sizeof(cache->superblock)); - Mm_MarkAsDirtyPhys(pg); - MmH_DerefPage(pg); -} \ No newline at end of file diff --git a/src/drivers/generic/extfs/interface.c b/src/drivers/generic/extfs/interface.c deleted file mode 100644 index 9c6c7d2d..00000000 --- a/src/drivers/generic/extfs/interface.c +++ /dev/null @@ -1,571 +0,0 @@ -/* - * drivers/generic/extfs/interface.c - * - * Copyright (c) 2025 Omar Berrow - * - * Abandon all hope ye who enter here -*/ - -#include -#include -#include -#include - -#include -#include - -#include - -#include - -#include - -#include "structs.h" - -#define get_handle(desc) ({\ - if (!desc) return OBOS_STATUS_INVALID_ARGUMENT;\ - (ext_inode_handle*)desc;\ -}) - -obos_status set_file_perms(dev_desc desc, driver_file_perm newperm) -{ - ext_inode_handle* hnd = get_handle(desc); - Core_MutexAcquire(&hnd->lock); - page* pg = nullptr; - ext_inode* ino = ext_read_inode_pg(hnd->cache, hnd->ino, &pg); - MmH_RefPage(pg); - - uint32_t new_mode = ino->mode & ~0777; - - if (newperm.other_exec) - new_mode |= EXT_OTHER_EXEC; - if (newperm.owner_exec) - new_mode |= EXT_OWNER_EXEC; - if (newperm.group_exec) - new_mode |= EXT_GROUP_EXEC; - - if (newperm.other_write) - new_mode |= EXT_OTHER_WRITE; - if (newperm.owner_write) - new_mode |= EXT_OWNER_WRITE; - if (newperm.group_write) - new_mode |= EXT_GROUP_WRITE; - - if (newperm.other_read) - new_mode |= EXT_OTHER_READ; - if (newperm.owner_read) - new_mode |= EXT_OWNER_READ; - if (newperm.group_read) - new_mode |= EXT_GROUP_READ; - - ino->mode = new_mode; - Mm_MarkAsDirtyPhys(pg); - MmH_DerefPage(pg); - Core_MutexRelease(&hnd->lock); - return OBOS_STATUS_SUCCESS; -} - -obos_status set_file_owner(dev_desc desc, uid owner_uid, gid group_uid) -{ - ext_inode_handle* hnd = get_handle(desc); - Core_MutexAcquire(&hnd->lock); - page* pg = nullptr; - ext_inode* ino = ext_read_inode_pg(hnd->cache, hnd->ino, &pg); - OBOS_ASSERT(ino); - if (!ino) - return OBOS_STATUS_INTERNAL_ERROR; - MmH_RefPage(pg); - - if (owner_uid != -1) - ino->uid = owner_uid & 0xffff; - if (group_uid != -1) - ino->gid = group_uid & 0xffff; - - Mm_MarkAsDirtyPhys(pg); - MmH_DerefPage(pg); - Core_MutexRelease(&hnd->lock); - return OBOS_STATUS_SUCCESS; -} - -obos_status get_max_blk_count(dev_desc desc, size_t* count) -{ - ext_inode_handle* hnd = (void*)desc; - if (!hnd || !count) - return OBOS_STATUS_INVALID_ARGUMENT; - if (desc == UINTPTR_MAX) - return OBOS_STATUS_INVALID_ARGUMENT; - ext_inode* node = ext_read_inode(hnd->cache, hnd->ino); - if (!node) - return OBOS_STATUS_INVALID_ARGUMENT; // uh oh :D - *count = node->size; - Free(EXT_Allocator, node, sizeof(*node)); - return OBOS_STATUS_SUCCESS; -} - -obos_status stat_fs_info(void *vn, drv_fs_info *info) -{ - if (!info) - return OBOS_STATUS_INVALID_ARGUMENT; - - ext_cache *cache = nullptr; - for (ext_cache* curr = LIST_GET_HEAD(ext_cache_list, &EXT_CacheList); curr && !cache; ) - { - if (curr->vn == vn) - cache = curr; - - curr = LIST_GET_NEXT(ext_cache_list, &EXT_CacheList, curr); - } - - if (!cache) - return OBOS_STATUS_NOT_FOUND; - - info->fsBlockSize = cache->block_size; - info->freeBlocks = le32_to_host(cache->superblock.block_count) - le32_to_host(cache->superblock.free_block_count); - - info->availableFiles = le32_to_host(cache->superblock.free_inode_count); - info->fileCount = le32_to_host(cache->superblock.inode_count) - le32_to_host(cache->superblock.free_inode_count); - - info->nameMax = 255; - - if (cache->read_only) - info->flags |= FS_FLAGS_RDONLY; - - info->partBlockSize = cache->vn->blkSize; - info->szFs = cache->vn->filesize / info->partBlockSize; - - return OBOS_STATUS_SUCCESS; -} - - -obos_status set_file_times(dev_desc desc, void* times_) -{ - struct file_times* times = times_; - ext_inode_handle* hnd = (void*)desc; - - if (!hnd || !times) - return OBOS_STATUS_INVALID_ARGUMENT; - - page* pg = nullptr; - Core_MutexAcquire(&hnd->lock); - - ext_inode* ino = ext_read_inode_pg(hnd->cache, hnd->ino, &pg); - - ino->access_time = times->access; - ino->creation_time = times->birth; - ino->modification_time = times->change; - ino->delete_time = 0; - - Mm_MarkAsDirtyPhys(pg); - MmH_DerefPage(pg); - - Core_MutexRelease(&hnd->lock); - - return OBOS_STATUS_SUCCESS; -} - -obos_status get_file_times(dev_desc desc, void *ptimes) -{ - struct file_times* times = ptimes; - ext_inode_handle* hnd = (void*)desc; - if (!hnd || !times) - return OBOS_STATUS_INVALID_ARGUMENT; - if (desc == UINTPTR_MAX) - return OBOS_STATUS_INVALID_ARGUMENT; - ext_inode* node = ext_read_inode(hnd->cache, hnd->ino); - if (!node) - return OBOS_STATUS_INVALID_ARGUMENT; - times->access = le32_to_host(node->access_time); - times->change = le32_to_host(node->modification_time); - times->birth = le32_to_host(node->creation_time); - Free(EXT_Allocator, node, sizeof(*node)); - return OBOS_STATUS_SUCCESS; -} - -static dev_desc get_desc(ext_cache* cache, uint32_t ino) -{ - if (cache->inode_vnode_table[ino-1]) - return cache->inode_vnode_table[ino-1]->desc; - else - { - ext_inode_handle* hnd = ZeroAllocate(EXT_Allocator, 1, sizeof(ext_inode_handle), nullptr); - hnd->cache = cache; - hnd->ino = ino; - hnd->lock = MUTEX_INITIALIZE(); - return (dev_desc)hnd; - } -} - -obos_status vnode_search(void** vn_found, dev_desc desc, void* dev_vn) -{ - uint32_t ino = 0; - ext_cache* cache = nullptr; - if (desc == UINTPTR_MAX) - { - for (ext_cache* curr = LIST_GET_HEAD(ext_cache_list, &EXT_CacheList); curr && !cache; ) - { - if (curr->vn == dev_vn) - cache = curr; - - curr = LIST_GET_NEXT(ext_cache_list, &EXT_CacheList, curr); - } - if (!cache) - return OBOS_STATUS_INVALID_ARGUMENT; - ino = 2; - } - else - { - ext_inode_handle* hnd = (void*)desc; - cache = hnd->cache; - ino = hnd->ino; - OBOS_ENSURE(cache); - } - - *vn_found = ext_make_vnode(cache, ino, nullptr); - return OBOS_STATUS_SUCCESS; -} - -obos_status list_dir(dev_desc dir, void* vn, iterate_decision(*cb)(dev_desc desc, size_t blkSize, size_t blkCount, void* userdata, const char* name), void* userdata) -{ - Mm_PageWriterOperation |= PAGE_WRITER_SYNC_FILE; - Mm_WakePageWriter(true); - Mm_WakePageWriter(true); - - uint32_t ino = 0; - ext_cache* cache = nullptr; - if (dir == UINTPTR_MAX) - { - for (ext_cache* curr = LIST_GET_HEAD(ext_cache_list, &EXT_CacheList); curr && !cache; ) - { - if (curr->vn == vn) - cache = curr; - - curr = LIST_GET_NEXT(ext_cache_list, &EXT_CacheList, curr); - } - if (!cache) - return OBOS_STATUS_INVALID_ARGUMENT; - ino = 2; - } - else - { - ext_inode_handle* hnd = (void*)dir; - cache = hnd->cache; - ino = hnd->ino; - } - - page* pg = nullptr; - ext_inode* inode = ext_read_inode_pg(cache, ino, &pg); - if (!inode) - return OBOS_STATUS_INVALID_ARGUMENT; - MmH_RefPage(pg); - if (!ext_ino_test_type(inode, EXT2_S_IFDIR)) - { - MmH_DerefPage(pg); - return OBOS_STATUS_INVALID_ARGUMENT; - } - - size_t nToRead = le32_to_host(inode->blocks) * 512; - uint8_t* buffer = Allocate(EXT_Allocator, nToRead, nullptr); - ext_ino_read_blocks(cache, ino, 0, nToRead, buffer, nullptr); - - ext_dirent* ent = nullptr; - char name[256] = {}; - for (size_t offset = 0; offset < nToRead; ) - { - ent = (void*)(buffer+offset); - if (!ent->ino) - goto down; - if (strcmp(ent->name, ".") || strcmp(ent->name, "..")) - goto down; - - ext_inode* ent_ino = ext_read_inode(cache, ent->ino); - OBOS_ASSERT(ent_ino); - if (!ent_ino) - goto down; - - memcpy(name, ent->name, ent->name_len); - if (cb(get_desc(cache, ent->ino), 1, ext_ino_filesize(cache, ent_ino), userdata, name) == ITERATE_DECISION_STOP) - { - Free(EXT_Allocator, ent_ino, sizeof(ext_inode)); - break; - } - memzero(name, ent->name_len); - - Free(EXT_Allocator, ent_ino, sizeof(ext_inode)); - - down: - offset += ent->rec_len; - if (!ent->rec_len) - { - // Corrupted directory. - MmH_DerefPage(pg); - return OBOS_STATUS_INTERNAL_ERROR; - } - } - - MmH_DerefPage(pg); - inode = nullptr; - - return OBOS_STATUS_SUCCESS; -} - -obos_status get_file_perms(dev_desc desc, driver_file_perm *perm) -{ - ext_inode_handle* hnd = (void*)desc; - if (!hnd || !perm) - return OBOS_STATUS_INVALID_ARGUMENT; - if (desc == UINTPTR_MAX) - return OBOS_STATUS_INVALID_ARGUMENT; - ext_inode* node = ext_read_inode(hnd->cache, hnd->ino); - if (!node) - return OBOS_STATUS_INVALID_ARGUMENT; // uh oh :D - perm->mode = node->mode & 0777; - Free(EXT_Allocator, node, sizeof(*node)); - return OBOS_STATUS_SUCCESS; -} - -obos_status get_file_type(dev_desc desc, file_type *type) -{ - ext_inode_handle* hnd = (void*)desc; - if (!hnd || !type) - return OBOS_STATUS_INVALID_ARGUMENT; - if (desc == UINTPTR_MAX) - return OBOS_STATUS_INVALID_ARGUMENT; - ext_inode* node = ext_read_inode(hnd->cache, hnd->ino); - if (!node) - return OBOS_STATUS_INVALID_ARGUMENT; // uh oh :D - if (ext_ino_test_type(node, EXT2_S_IFDIR)) *type = FILE_TYPE_DIRECTORY; - else if (ext_ino_test_type(node, EXT2_S_IFREG)) *type = FILE_TYPE_REGULAR_FILE; - else if (ext_ino_test_type(node, EXT2_S_IFLNK)) *type = FILE_TYPE_SYMBOLIC_LINK; - else - { - Free(EXT_Allocator, node, sizeof(*node)); - return OBOS_STATUS_INVALID_ARGUMENT; - } - Free(EXT_Allocator, node, sizeof(*node)); - return OBOS_STATUS_SUCCESS; -} - -obos_status get_file_owner(dev_desc desc, uid* uid, gid* gid) -{ - ext_inode_handle* hnd = (void*)desc; - if (!hnd) - return OBOS_STATUS_INVALID_ARGUMENT; - if (desc == UINTPTR_MAX) - return OBOS_STATUS_INVALID_ARGUMENT; - ext_inode* node = ext_read_inode(hnd->cache, hnd->ino); - if (!node) - return OBOS_STATUS_INVALID_ARGUMENT; // uh oh :D - if (uid) *uid = le16_to_host(node->uid); - if (gid) *gid = le16_to_host(node->gid); - Free(EXT_Allocator, node, sizeof(*node)); - return OBOS_STATUS_SUCCESS; -} - -static size_t str_search(const char* str, char ch) -{ - size_t ret = strchr(str, ch); - for (; str[ret] == ch && str[ret] != 0; ret++) - ; - return ret; -} - -#define get_next_tok() do {\ - const char *newtok = tok + str_search(tok, '/');\ - tok = newtok;\ - size_t currentPathLen = strlen(tok);\ - if (currentPathLen)\ - currentPathLen--;\ - else { tok_len = 0; break; }\ - if (tok[currentPathLen] != '/')\ - currentPathLen++;\ - while (tok[currentPathLen] == '/')\ - currentPathLen--;\ - tok_len = strchr(tok, '/');\ - if (tok_len != currentPathLen)\ - tok_len--;\ - while (tok[tok_len - 1] == '/')\ - tok_len--;\ -} while(0) - -static void on_match(ext_inode** const inode, - page** const pg, - uint8_t** const buffer, - size_t* const nToRead, - size_t* const offset, - const ext_dirent* const dent, - ext_cache* const cache) -{ - uint32_t ino = dent->ino; - if (dent->file_type == EXT2_FT_DIR) - { - size_t old_size = *nToRead; - *nToRead = le32_to_host((*inode)->blocks) * 512; - *buffer = Reallocate(EXT_Allocator, *buffer, *nToRead, old_size, nullptr); - ext_ino_read_blocks(cache, ino, 0, *nToRead, *buffer, nullptr); - *offset = 0; - } - MmH_DerefPage(*pg); - *inode = ext_read_inode_pg(cache, ino, pg); - MmH_RefPage(*pg); -} - -obos_status path_search(dev_desc* found, void* vn, const char* path, dev_desc parent) -{ - if (!found || (!vn && parent == UINTPTR_MAX) || !path) - return OBOS_STATUS_INVALID_ARGUMENT; - - Mm_PageWriterOperation |= PAGE_WRITER_SYNC_FILE; - Mm_WakePageWriter(true); - Mm_WakePageWriter(true); - - uint32_t parent_ino = 0; - ext_cache* cache = nullptr; - if (parent == UINTPTR_MAX) - { - for (ext_cache* curr = LIST_GET_HEAD(ext_cache_list, &EXT_CacheList); curr && !cache; ) - { - if (curr->vn == vn) - cache = curr; - - curr = LIST_GET_NEXT(ext_cache_list, &EXT_CacheList, curr); - } - if (!cache) - return OBOS_STATUS_INVALID_ARGUMENT; - - parent_ino = 2; - } - else - { - ext_inode_handle* hnd = (void*)parent; - if (!parent) - return OBOS_STATUS_INVALID_ARGUMENT; - cache = hnd->cache; - parent_ino = hnd->ino; - } - - *found = 0; - - size_t path_len = strlen(path); - for (; *path == '/'; path++, path_len--) - ; - if (!path_len) - return OBOS_STATUS_NOT_FOUND; - const char* tok = path; - size_t tok_len = strchr(tok, '/'); - if (tok_len != path_len) - tok_len--; - while (tok[tok_len - 1] == '/') - tok_len--; - if (!tok_len) - return OBOS_STATUS_NOT_FOUND; - - page* pg = nullptr; - ext_inode* inode = ext_read_inode_pg(cache, parent_ino, &pg); - if (!inode) - return OBOS_STATUS_INVALID_ARGUMENT; - MmH_RefPage(pg); - if (!ext_ino_test_type(inode, EXT2_S_IFDIR)) - { - MmH_DerefPage(pg); - return OBOS_STATUS_INVALID_ARGUMENT; - } - - size_t nToRead = le32_to_host(inode->blocks) * 512; - uint8_t* buffer = Allocate(EXT_Allocator, nToRead, nullptr); - ext_ino_read_blocks(cache, parent_ino, 0, nToRead, buffer, nullptr); - - ext_dirent* ent = nullptr; - for (size_t offset = 0; offset < nToRead; ) - { - ent = (void*)(buffer+offset); - if (!ent->ino) - goto down; - if (strcmp(ent->name, ".") || strcmp(ent->name, "..")) - goto down; - - ext_inode* ent_ino = ext_read_inode(cache, ent->ino); - - if (ent->name_len == tok_len) - { - if (memcmp(ent->name, tok, tok_len)) - { - get_next_tok(); - if (tok == (path + path_len)) - { - *found = get_desc(cache, ent->ino); - break; - } - on_match(&inode, - &pg, - &buffer, - &nToRead, - &offset, - ent, - cache); - } - } - - Free(EXT_Allocator, ent_ino, sizeof(ext_inode)); - - down: - offset += ent->rec_len; - if (!ent->rec_len) - { - // Directory corruption. - OBOS_Error("extfs: %s: directory corrupted, returning OBOS_STATUS_INTERNAL_ERROR (EIO)\n", __func__); - MmH_DerefPage(pg); - Free(EXT_Allocator, buffer, nToRead); - return OBOS_STATUS_INTERNAL_ERROR; - } - } - - MmH_DerefPage(pg); - Free(EXT_Allocator, buffer, nToRead); - - return *found ? OBOS_STATUS_SUCCESS : OBOS_STATUS_NOT_FOUND; -} - -obos_status mountp(void* vnp, void* targetp) -{ - vnode* vn = vnp; - OBOS_MAYBE_UNUSED dirent* target = targetp; - - ext_cache* cache = nullptr; - - for (ext_cache* curr = LIST_GET_HEAD(ext_cache_list, &EXT_CacheList); curr && !cache; ) - { - if (curr->vn == vn) - cache = curr; - - curr = LIST_GET_NEXT(ext_cache_list, &EXT_CacheList, curr); - } - if (!cache) - return OBOS_STATUS_INVALID_ARGUMENT; - - cache->superblock.state = EXT_ERROR_FS; - ext_writeback_sb(cache); - - return OBOS_STATUS_SUCCESS; -} - -obos_status umount(void* vnp) -{ - vnode* vn = vnp; - - ext_cache* cache = nullptr; - - for (ext_cache* curr = LIST_GET_HEAD(ext_cache_list, &EXT_CacheList); curr && !cache; ) - { - if (curr->vn == vn) - cache = curr; - - curr = LIST_GET_NEXT(ext_cache_list, &EXT_CacheList, curr); - } - if (!cache) - return OBOS_STATUS_INVALID_ARGUMENT; - - cache->superblock.state = EXT_VALID_FS; - ext_writeback_sb(cache); - - return OBOS_STATUS_SUCCESS; -} \ No newline at end of file diff --git a/src/drivers/generic/extfs/main.c b/src/drivers/generic/extfs/main.c deleted file mode 100644 index 2a10afd4..00000000 --- a/src/drivers/generic/extfs/main.c +++ /dev/null @@ -1,178 +0,0 @@ -/* - * drivers/generic/extfs/main.c - * - * Copyright (c) 2025 Omar Berrow - * - * Abandon all hope, ye who enter here -*/ - -#include -#include - -#include -#include - -#include - -#include - -#include - -#include "structs.h" - -LIST_GENERATE(ext_cache_list, ext_cache, node); -ext_cache_list EXT_CacheList; - -OBOS_PAGEABLE_FUNCTION obos_status get_blk_size(dev_desc desc, size_t* blkSize) -{ - OBOS_UNUSED(desc); - if (!blkSize) - return OBOS_STATUS_INVALID_ARGUMENT; - *blkSize = 1; - return OBOS_STATUS_SUCCESS; -} -OBOS_WEAK obos_status get_max_blk_count(dev_desc desc, size_t* count); - -obos_status read_sync(dev_desc desc, void* buf, size_t blkCount, size_t blkOffset, size_t* nBlkRead) -{ - ext_inode_handle *hnd = (void*)desc; - if (!hnd || !buf) - return OBOS_STATUS_INVALID_ARGUMENT; - Mm_PageWriterOperation |= PAGE_WRITER_SYNC_FILE; - Mm_WakePageWriter(true); - Mm_WakePageWriter(true); - Core_MutexAcquire(&hnd->lock); - obos_status status = ext_ino_read_blocks(hnd->cache, hnd->ino, blkOffset, blkCount, buf, nBlkRead); - Core_MutexRelease(&hnd->lock); - return status; -} - -obos_status write_sync(dev_desc desc, const void* buf, size_t blkCount, size_t blkOffset, size_t* nBlkWritten) -{ - ext_inode_handle *hnd = (void*)desc; - if (!hnd || !buf) - return OBOS_STATUS_INVALID_ARGUMENT; - - Core_MutexAcquire(&hnd->lock); - - obos_status status = OBOS_STATUS_SUCCESS; - - size_t new_size = blkOffset+blkCount; - status = ext_ino_resize(hnd->cache, hnd->ino, new_size, true); - if (obos_is_error(status)) - { - Core_MutexRelease(&hnd->lock); - return status; - } - - status = ext_ino_commit_blocks(hnd->cache, hnd->ino, blkOffset, blkCount); - if (obos_is_error(status)) - { - Core_MutexRelease(&hnd->lock); - return status; - } - - status = ext_ino_write_blocks(hnd->cache, hnd->ino, blkOffset, blkCount, buf, nBlkWritten); - Core_MutexRelease(&hnd->lock); - return status; -} - -OBOS_WEAK obos_status foreach_device(iterate_decision(*cb)(dev_desc desc, size_t blkSize, size_t blkCount, void* u), void* u); -OBOS_WEAK obos_status query_user_readable_name(dev_desc what, const char** name); // unrequired for fs drivers. -OBOS_PAGEABLE_FUNCTION obos_status ioctl(dev_desc what, uint32_t request, void* argp) -{ - OBOS_UNUSED(what); - OBOS_UNUSED(request); - OBOS_UNUSED(argp); - return OBOS_STATUS_INVALID_IOCTL; -} -void driver_cleanup_callback() -{ -} -OBOS_WEAK obos_status symlink_set_path(dev_desc desc, const char* to);; -OBOS_WEAK obos_status phardlink_file(dev_desc desc, const char* parent_path, void* vn, const char* name); -OBOS_WEAK obos_status query_path(dev_desc desc, const char** path); -OBOS_WEAK obos_status path_search(dev_desc* found, void*, const char* what, dev_desc parent); -OBOS_WEAK obos_status get_linked_path(dev_desc desc, const char** found); -OBOS_WEAK obos_status pmove_desc_to(void* vn, const char* path, const char* newpath, const char* name); -OBOS_WEAK obos_status pmk_file(dev_desc* newDesc, const char* parent_path, void* vn, const char* name, file_type type, driver_file_perm perm); -OBOS_WEAK obos_status premove_file(void* vn, const char* path); -OBOS_WEAK obos_status trunc_file(dev_desc desc, size_t newsize); -OBOS_WEAK obos_status set_file_perms(dev_desc desc, driver_file_perm newperm); -OBOS_WEAK obos_status set_file_owner(dev_desc desc, uid owner_uid, gid group_uid); -OBOS_WEAK obos_status get_file_owner(dev_desc desc, uid* uid, gid* gid); -OBOS_WEAK obos_status get_file_perms(dev_desc desc, driver_file_perm *perm); -OBOS_WEAK obos_status get_file_type(dev_desc desc, file_type *type); -OBOS_WEAK obos_status list_dir(dev_desc dir, void* vn, iterate_decision(*cb)(dev_desc desc, size_t blkSize, size_t blkCount, void* userdata, const char* name), void* userdata); -OBOS_WEAK bool probe(void* vn); -OBOS_WEAK obos_status submit_irp(void* request); -OBOS_WEAK obos_status finalize_irp(void* request); -OBOS_WEAK obos_status ext_mount(void* vn, void* at); -OBOS_WEAK obos_status set_file_times(dev_desc desc, const void* times_); -OBOS_WEAK obos_status get_file_times(dev_desc desc, void *ptimes); -OBOS_WEAK obos_status stat_fs_info(void *vn, drv_fs_info *info); -OBOS_WEAK obos_status vnode_search(void** vn_found, dev_desc desc, void* dev_vn); -obos_status get_file_inode(dev_desc desc, uint32_t *out) -{ - ext_inode_handle* hnd = (void*)desc; - if (!hnd) - return OBOS_STATUS_INVALID_ARGUMENT; - *out = hnd->ino; - return OBOS_STATUS_SUCCESS; -} - -OBOS_WEAK obos_status mountp(void* vnp, void* targetp); -OBOS_WEAK obos_status umount(void* vnp); - -__attribute__((section(OBOS_DRIVER_HEADER_SECTION))) driver_header drv_hdr = { - .magic = OBOS_DRIVER_MAGIC, - .flags = DRIVER_HEADER_HAS_STANDARD_INTERFACES|DRIVER_HEADER_DIRENT_CB_PATHS, - .ftable = { - .driver_cleanup_callback = driver_cleanup_callback, - .ioctl = ioctl, - .get_blk_size = get_blk_size, - .get_max_blk_count = get_max_blk_count, - .query_user_readable_name = query_user_readable_name, - .foreach_device = foreach_device, - .read_sync = read_sync, - .write_sync = write_sync, - .submit_irp = submit_irp, - .finalize_irp = finalize_irp, - - .query_path = query_path, - .path_search = path_search, - .get_linked_path = get_linked_path, - .pmove_desc_to = pmove_desc_to, - .set_file_times = set_file_times, - .get_file_times = get_file_times, - .pmk_file = pmk_file, - .premove_file = premove_file, - .trunc_file = trunc_file, // TODO: Implement - .get_file_perms = get_file_perms, - .set_file_perms = set_file_perms, - .set_file_owner = set_file_owner, - .get_file_owner = get_file_owner, - .get_file_type = get_file_type, - .get_file_inode = get_file_inode, - .list_dir = list_dir, - .vnode_search = vnode_search, - .stat_fs_info = stat_fs_info, - .probe = probe, - .phardlink_file = phardlink_file, - .symlink_set_path = symlink_set_path, - .mount = mountp, - .umount = umount, - }, - .driverName = "EXT Driver", - .version = 1, -}; - -allocator_info* EXT_Allocator; -static basic_allocator ext_alloc = {}; - -driver_init_status OBOS_DriverEntry(driver_id* this) -{ - OBOSH_ConstructBasicAllocator(&ext_alloc); - EXT_Allocator = (allocator_info*)&ext_alloc; - return (driver_init_status){.status=OBOS_STATUS_SUCCESS}; -} \ No newline at end of file diff --git a/src/drivers/generic/extfs/probe.c b/src/drivers/generic/extfs/probe.c deleted file mode 100644 index 3e223f00..00000000 --- a/src/drivers/generic/extfs/probe.c +++ /dev/null @@ -1,254 +0,0 @@ -/* - * drivers/generic/extfs/probe.c - * - * Copyright (c) 2025 Omar Berrow - * - * Abandon all hope ye who enter here -*/ - -#include -#include -#include -#include - -#include -#include -#include -#include - -#include - -#include - -#include -#include - -#include - -#include "structs.h" - -bool probe(void* vn_) -{ - OBOS_ASSERT(vn_); - if (!vn_) - return false; - vnode* vn = vn_; - ext_cache* cache = ZeroAllocate(EXT_Allocator, 1, sizeof(ext_cache), nullptr); - cache->block_size = 1024; - cache->vn = vn; - page* pg = nullptr; - ext_superblock* sb = ext_read_block(cache, 1, &pg); - MmH_RefPage(pg); - if (!sb) - { - MmH_DerefPage(pg); - Free(EXT_Allocator, cache, sizeof(*cache)); - return false; - } - if (sb->magic != EXT_MAGIC) - { - MmH_DerefPage(pg); - Free(EXT_Allocator, cache, sizeof(*cache)); - return false; - } - memcpy(&cache->superblock, sb, sizeof(*sb)); - MmH_DerefPage(pg); - sb = &cache->superblock; - pg = nullptr; - cache->revision = le32_to_host(sb->revision); - if (cache->revision) - { - uint32_t mask = EXT2_FEATURE_INCOMPAT_FILETYPE|EXT2_FEATURE_INCOMPAT_META_BG; - bool incompatible = le32_to_host(sb->dynamic_rev.incompat_features) & ~mask; - if (incompatible) - { - Free(EXT_Allocator, cache, sizeof(*cache)); - return false; - } - mask = (ext_sb_supports_64bit_filesize ? EXT2_FEATURE_RO_COMPAT_LARGE_FILE : 0) | EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; - cache->read_only = sb->dynamic_rev.ro_only_features & ~mask; - } - - cache->block_size = ext_sb_block_size(sb); - cache->blocks_per_group = ext_sb_blocks_per_group(sb); - cache->inodes_per_group = ext_sb_inodes_per_group(sb); - cache->inode_size = ext_sb_inode_size(sb); - uint32_t block_count = le32_to_host(cache->superblock.block_count); - cache->block_group_count = block_count / cache->blocks_per_group + !!(block_count % cache->blocks_per_group); - cache->vn = vn; - cache->inodes_per_block = cache->block_size/cache->inode_size; - cache->inode_blocks_per_group = cache->inodes_per_group/cache->inodes_per_block; - cache->bgdt = Allocate(EXT_Allocator, cache->block_group_count * sizeof(ext_bgd), nullptr); - - // Populate the in-memory BGDT - do { - uint32_t bgdt_blocks = (cache->block_group_count * sizeof(ext_bgd)) / cache->block_size; - if ((cache->block_group_count * sizeof(ext_bgd)) % cache->block_size) - bgdt_blocks++; - for (size_t i = 0; i < bgdt_blocks; i++) - { - void* bgdt_section = ext_read_block(cache, (cache->block_size == 1024 ? 2 : 1) + i, &pg); - MmH_RefPage(pg); - size_t copy_count = i == (bgdt_blocks - 1) ? (cache->block_group_count % (cache->block_size / sizeof(ext_bgd))) : (cache->block_size / sizeof(ext_bgd)); - memcpy(cache->bgdt + (i*4096/sizeof(ext_bgd)), bgdt_section, copy_count*sizeof(ext_bgd)); - MmH_DerefPage(pg); - } - } while(0); - - OBOS_Debug("extfs: Block size: 0x%x\n", cache->block_size); - OBOS_Debug("extfs: Blocks per group: 0x%x\n", cache->blocks_per_group); - OBOS_Debug("extfs: Inodes per group: 0x%x\n", cache->inodes_per_group); - OBOS_Debug("extfs: Inodes per block: 0x%x\n", cache->inodes_per_block); - OBOS_Debug("extfs: Inode blocks per group: 0x%x\n", cache->inode_blocks_per_group); - OBOS_Debug("extfs: Inode size: 0x%x\n", cache->inode_size); - OBOS_Debug("extfs: Block group count: 0x%d\n", cache->block_group_count); - OBOS_Debug("extfs: Revision: %d\n", cache->revision); - - if (cache->read_only) - OBOS_Warning("extfs: Probed partition is read-only at probe. Likely due to unsupported ext features\n"); - -// for (volatile bool b = true; b;) -// ; - - ext_inode* root = ext_read_inode(cache, 2); - if (!root) - { - OBOS_Error("extfs: No root inode in filesystem. Aborting probe\n"); - Free(EXT_Allocator, cache, sizeof(*cache)); - return false; - } - // We don't need the root inode except to check for its existence - Free(EXT_Allocator, root, sizeof(*root)); - root = nullptr; - cache->root = ext_dirent_populate(cache, 2, "/", false, nullptr); - - cache->inode_vnode_table_size = cache->inodes_per_group*cache->block_group_count*sizeof(vnode*); - cache->inode_vnode_table = Mm_VirtualMemoryAlloc(&Mm_KernelContext, - nullptr, cache->inode_vnode_table_size, - 0, VMA_FLAGS_HUGE_PAGE, - nullptr, nullptr); - - LIST_APPEND(ext_cache_list, &EXT_CacheList, cache); - - return true; -} - -vnode* ext_make_vnode(ext_cache* cache, uint32_t ino, mount* mnt) -{ - if (cache->inode_vnode_table[ino-1]) - { - cache->inode_vnode_table[ino-1]->refs++; - return cache->inode_vnode_table[ino-1]; - } - - Mm_PageWriterOperation |= PAGE_WRITER_SYNC_FILE; - Mm_WakePageWriter(true); - Mm_WakePageWriter(true); - - ext_inode* inode = ext_read_inode(cache, ino); - OBOS_ASSERT(inode); - if (!inode) - return nullptr; - - uint32_t vtype = 0; - if (ext_ino_test_type(inode, EXT2_S_IFDIR)) - vtype = VNODE_TYPE_DIR; - else if (ext_ino_test_type(inode, EXT2_S_IFREG)) - vtype = VNODE_TYPE_REG; - else if (ext_ino_test_type(inode, EXT2_S_IFLNK)) - vtype = VNODE_TYPE_LNK; - else - { - Free(EXT_Allocator, inode, sizeof(*inode)); - return nullptr; - } - - vnode* vn = Vfs_Calloc(1, sizeof(vnode)); - cache->inode_vnode_table[ino-1] = vn; - ext_inode_handle* handle = ZeroAllocate(EXT_Allocator, 1, sizeof(ext_inode_handle), nullptr); - handle->ino = ino; - handle->cache = cache; - handle->lock = MUTEX_INITIALIZE(); - vn->desc = (dev_desc)handle; - vn->vtype = vtype; - vn->blkSize = 1; - vn->uid = inode->uid; - vn->gid = inode->gid; - vn->filesize = (uint64_t)inode->size | (ext_sb_supports_64bit_filesize ? (uint64_t)inode->dir_acl << 32 : 0); - vn->times.access = inode->access_time; - vn->times.birth = inode->creation_time; - vn->times.access = inode->access_time; - - vn->perm.other_exec = inode->mode & EXT_OTHER_EXEC; - vn->perm.other_write = inode->mode & EXT_OTHER_WRITE; - vn->perm.other_read = inode->mode & EXT_OTHER_READ && !cache->read_only; - - vn->perm.owner_exec = inode->mode & EXT_OWNER_EXEC; - vn->perm.owner_write = inode->mode & EXT_OWNER_WRITE; - vn->perm.owner_read = inode->mode & EXT_OWNER_READ && !cache->read_only; - - vn->perm.group_exec = inode->mode & EXT_GROUP_EXEC; - vn->perm.group_write = inode->mode & EXT_GROUP_WRITE; - vn->perm.group_read = inode->mode & EXT_GROUP_READ && !cache->read_only; - - vn->perm.set_uid = inode->mode & EXT_SETUID; - vn->perm.set_gid = inode->mode & EXT_SETGID; - - vn->mount_point = mnt; - - vn->inode = ino; - - if (vn->vtype == VNODE_TYPE_LNK) - vn->un.linked = ext_ino_get_linked(cache, inode, ino); - - Free(EXT_Allocator, inode, sizeof(*inode)); - - return vn; -} - -static void mount_recursive(ext_cache* cache, ext_dirent_cache* parent, dirent* dparent, mount* mnt) -{ - OBOS_ENSURE(mnt); - for (ext_dirent_cache* ent = parent->children.head; ent; ) - { - vnode* vn = ext_make_vnode(cache, ent->ent.ino, mnt); - if (!vn) - goto down; - - dirent* dent = Vfs_Calloc(1, sizeof(dirent)); - OBOS_InitStringLen(&dent->name, ent->ent.name, ent->ent.name_len); - dent->vnode = vn; - VfsH_DirentAppendChild(dparent, dent); - LIST_APPEND(dirent_list, &mnt->dirent_list, dent); - dent->vnode->refs++; - - if (ent->ent.file_type == EXT2_FT_DIR) - mount_recursive(cache, ent, dent, mnt); - - down: - ent = ent->next; - } -} - -obos_status ext_mount(void* vn_, void* at_) -{ - if (!vn_ || !at_) - return OBOS_STATUS_INVALID_ARGUMENT; - vnode* vn = vn_; - dirent* at = at_; - ext_cache *cache = nullptr; - for (ext_cache* curr = LIST_GET_HEAD(ext_cache_list, &EXT_CacheList); curr && !cache; ) - { - if (curr->vn == vn) - cache = curr; - - curr = LIST_GET_NEXT(ext_cache_list, &EXT_CacheList, curr); - } - - if (!cache) - return OBOS_STATUS_NOT_FOUND; - - mount_recursive(cache, cache->root, at, at->vnode->un.mounted); - - return OBOS_STATUS_SUCCESS; -} \ No newline at end of file diff --git a/src/drivers/generic/extfs/structs.h b/src/drivers/generic/extfs/structs.h deleted file mode 100644 index 0c5a9b7e..00000000 --- a/src/drivers/generic/extfs/structs.h +++ /dev/null @@ -1,404 +0,0 @@ -/* - * drivers/generic/extfs/structs.h - * - * Copyright (c) 2025 Omar Berrow - * - * Abandon all hope ye who enter here -*/ - -#include -#include -#include - -#include - -#include - -#include - -#include - -#include - -#if defined(__x86_64__) -# define host_to_be16(val) __builtin_bswap16(val) -# define host_to_be32(val) __builtin_bswap32(val) -# define host_to_be64(val) __builtin_bswap64(val) -# define be16_to_host(val) __builtin_bswap16(val) -# define be32_to_host(val) __builtin_bswap32(val) -# define be64_to_host(val) __builtin_bswap64(val) -# define host_to_le16(val) (uint16_t)(val) -# define host_to_le32(val) (uint32_t)(val) -# define host_to_le64(val) (uint32_t)(val) -# define le16_to_host(val) (uint16_t)(val) -# define le32_to_host(val) (uint32_t)(val) -# define le64_to_host(val) (uint64_t)(val) -#elif defined(__m68k__) -# define host_to_be16(val) (uint16_t)(val) -# define host_to_be32(val) (uint32_t)(val) -# define host_to_be64(val) (uint32_t)(val) -# define be16_to_host(val) (uint16_t)(val) -# define be32_to_host(val) (uint32_t)(val) -# define be64_to_host(val) (uint64_t)(val) -# define host_to_le16(val) __builtin_bswap16(val) -# define host_to_le32(val) __builtin_bswap32(val) -# define host_to_le64(val) __builtin_bswap64(val) -# define le16_to_host(val) __builtin_bswap16(val) -# define le32_to_host(val) __builtin_bswap32(val) -# define le64_to_host(val) __builtin_bswap64(val) -#else -# error Define required macros. -#endif - -enum { - EXT_MAGIC = 0xEF53, -}; -enum { - EXT2_FEATURE_INCOMPAT_COMPRESSION = BIT(0), - EXT2_FEATURE_INCOMPAT_FILETYPE = BIT(1), - EXT3_FEATURE_INCOMPAT_RECOVER = BIT(2), - EXT3_FEATURE_INCOMPAT_JOURNAL_DEV = BIT(3), - EXT2_FEATURE_INCOMPAT_META_BG = BIT(4), -}; -enum { - EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER = BIT(0), - EXT2_FEATURE_RO_COMPAT_LARGE_FILE = BIT(1), - EXT2_FEATURE_RO_COMPAT_BTREE_DIR = BIT(2), -}; -enum { - EXT_VALID_FS = 1, - EXT_ERROR_FS = 2, -}; -enum { - EXT_ERROR_CONTINUE, - EXT_ERROR_RO, // remount as RO - EXT_ERROR_PANIC, // kernel panic -}; -enum { - EXT_GOOD_OLD_REV = 0, - EXT_DYNAMIC_REV = 1, -}; - -typedef struct ext_superblock -{ - uint32_t inode_count; - uint32_t block_count; - uint32_t resv_block_count; - uint32_t free_block_count; - uint32_t free_inode_count; - uint32_t first_data_block; - uint32_t log_block_size; // 1024<mode) >> 12) -#define ext_ino_test_type(inode, type) ((le32_to_host((inode)->mode) >> 12) == ((type) >> 12)) - -#undef EXEC -#undef WRITE -#undef READ - -typedef struct ext_inode { - uint16_t mode; - uint16_t uid; - uint32_t size; - uint32_t access_time; - uint32_t creation_time; - uint32_t modification_time; - uint32_t delete_time; - uint16_t gid; - uint16_t link_count; // does not include soft links, acts as a refcount - uint32_t blocks; // in 512-byte blocks, not ext blocks - uint32_t flags; - uint32_t os1; - uint32_t direct_blocks[12]; - uint32_t indirect_block; - uint32_t doubly_indirect_block; - uint32_t triply_indirect_block; - uint32_t generation; - uint32_t file_acl; - uint32_t dir_acl; // revision one, contains the top 32-bits of the file size for regular files - uint32_t fragment; - uint8_t os2[12]; -} OBOS_ALIGN(1) ext_inode; - -enum { - EXT2_FT_UNKNOWN = 0, - EXT2_FT_REG_FILE = 1, - EXT2_FT_DIR = 2, - EXT2_FT_CHRDEV = 3, - EXT2_FT_BLKDEV = 4, - EXT2_FT_FIFO = 5, - EXT2_FT_SOCK = 6, - EXT2_FT_SYMLINK = 7, -}; - -typedef struct ext_dirent { - uint32_t ino; - uint16_t rec_len; - uint8_t name_len; - uint8_t file_type; // ignore, use inode->mode. also invalid in revision 0 - char name[255]; // 255 bytes max -} OBOS_PACK ext_dirent; - -typedef struct ext_dirent_cache { - struct { - struct ext_dirent_cache *head, *tail; - size_t nChildren; - } children; - struct ext_dirent_cache *next, *prev; - struct ext_dirent_cache* parent; - ext_inode* inode; - page* pg; - uint32_t ent_block, ent_offset, rel_offset; - ext_dirent ent; - struct ext_cache* cache; - bool populated; -} ext_dirent_cache; - -#define ext_dirent_adopt(parent_, child_) \ -do {\ - ext_dirent_cache* _parent = (parent_);\ - ext_dirent_cache* _child = (child_);\ - if(!_parent->children.head)\ - _parent->children.head = _child;\ - if (_parent->children.tail)\ - _parent->children.tail->next = _child;\ - _child->prev = _parent->children.tail;\ - _parent->children.tail = _child;\ - _parent->children.nChildren++;\ - _child->parent = _parent;\ -} while(0) -#define ext_dirent_emplace_at(parent_, child_, after_) \ -do {\ - ext_dirent_cache* _parent = (parent_);\ - ext_dirent_cache* _child = (child_);\ - ext_dirent_cache* _after = (after_);\ - if (_after->next)\ - _after->next->prev = _child;\ - _child->next = _after->next;\ - _after->next = _child;\ - _child->prev = _after;\ - if (_parent->children.tail == _after)\ - _parent->children.tail = _child;\ - _parent->children.nChildren++;\ - _child->parent = _parent;\ -} while(0) - -#define ext_dirent_disown(parent_, child_) \ -do {\ - ext_dirent_cache* _parent = (parent_);\ - ext_dirent_cache* _child = (child_);\ - if (_parent->children.head == _child)\ - _parent->children.head = _child->next;\ - if (_parent->children.tail == _child)\ - _parent->children.tail = _child->prev;\ - if (_child->prev)\ - _child->prev->next = _child->next;\ - if (_child->next)\ - _child->next->prev = _child->prev;\ - _parent->children.nChildren--;\ -} while(0) - -typedef struct ext_cache { - ext_superblock superblock; - vnode* vn; - bool read_only; - ext_bgdt bgdt; - uint32_t block_size; - uint32_t revision; - uint32_t block_group_count; - uint32_t inode_blocks_per_group; - uint32_t inodes_per_block; - uint16_t inodes_per_group; - uint16_t blocks_per_group; - uint16_t inode_size; - ext_dirent_cache* root; - LIST_NODE(ext_cache_list, struct ext_cache) node; - vnode** inode_vnode_table; - size_t inode_vnode_table_size; -} ext_cache; - -typedef LIST_HEAD(ext_cache_list, ext_cache) ext_cache_list; -LIST_PROTOTYPE(ext_cache_list, ext_cache, node); - -extern ext_cache_list EXT_CacheList; - -typedef struct ext_inode_handle -{ - uint32_t ino; - ext_cache* cache; - mutex lock; -} ext_inode_handle; - -// Gets an inode straight from the pagecache, returns it along the page* of the pagecache entry. -ext_inode* ext_read_inode_pg(ext_cache* cache, uint32_t ino, page **pg); -// Wrapper for ext_read_inode_pg that copies it into a separate buffer and returns it. -ext_inode* ext_read_inode(ext_cache* cache, uint32_t ino); - -void ext_ino_foreach_block(ext_cache* cache, - uint32_t ino, - iterate_decision(*cb)(ext_cache* cache, ext_inode* inode, uint32_t *block, void* userdata), - void* userdata); -obos_status ext_ino_read_blocks(ext_cache* cache, uint32_t ino, size_t offset, size_t count, void* buffer, size_t *nRead); -obos_status ext_ino_write_blocks(ext_cache* cache, uint32_t ino, size_t offset, size_t count, const void* buffer, size_t *nWritten); -obos_status ext_ino_commit_blocks(ext_cache* cache, uint32_t ino, size_t offset, size_t size); -obos_status ext_ino_resize(ext_cache* cache, uint32_t ino, size_t new_size, bool expand_only); -char* ext_ino_get_linked(ext_cache* cache, ext_inode* inode, uint32_t ino_num); - -struct inode_offset_location { - size_t offset; - /* [0]: Direct block index (valid if idx[1-3] are all zero) - [1]: Indirect block index - [2]: Doubly indirect block index - [3]: Triply indirect block index - The search should start at: - - if idx[3] != UINT32_MAX - - triply indirect blocks - - else - - if idx[2] != UINT32_MAX - - doubly indirect blocks - - else - - if idx[1] != UINT32_MAX - - indirect blocks - - else - - direct blocks - (idx[3] == UINT32_MAX ? - (idx[2] == UINT32_MAX ? - (idx[1] != UINT32_MAX ? - indirect_block - : blocks) // idx[1] != UINT32_MAX - : doubly_indirect_block) // idx[2] == UINT32_MAX - : triply_indirect_block) // idx[3] == UINT32_MAX - */ - uint32_t idx[4]; -}; -struct inode_offset_location ext_get_blk_index_from_offset(ext_cache* cache, size_t offset); - -// block_group can be nullptr -uint32_t ext_ino_allocate(ext_cache* cache, const uint32_t* block_group); -void ext_ino_free(ext_cache* cache, uint32_t ino); - -uint32_t ext_blk_allocate(ext_cache* cache, const uint32_t* block_group); -void ext_blk_free(ext_cache* cache, uint32_t blk); - -void ext_writeback_bgd(ext_cache* cache, uint32_t bgd_idx); -void ext_writeback_sb(ext_cache* cache); - -// Creates and populates a dirent -// If ino is not a directory, this fails. -// cache: The ext cache to use -// ino: The directory inode -// parent_name: The directory's name -// recurse_directories: Whether to recursively populate child directories. -ext_dirent_cache *ext_dirent_populate(ext_cache* cache, uint32_t ino, const char* parent_name, bool recurse_directories, ext_dirent_cache* parent); -ext_dirent_cache *ext_dirent_lookup_from(const char* path, ext_dirent_cache* root); -void ext_dirent_flush(ext_cache* cache, ext_dirent_cache* ent); - -uint32_t ext_get_block_at_index(ext_cache* cache, uint32_t ino, struct inode_offset_location loc); - -#define ext_read_block(cache, block_number, pg) (VfsH_PageCacheGetEntry((cache)->vn, (block_number)*(cache->block_size), (pg))) -#define ext_block_group_from_block(cache, block_number) ((block_number) / (cache)->blocks_per_group) - -#define ext_ino_max_block_index(cache, inode) (le32_to_host(inode->blocks) / ((cache->block_size) / 512)) -#define ext_ino_get_block_group(cache, inode_number) ((inode_number - 1) / (cache)->inodes_per_group) -#define ext_ino_get_local_index(cache, inode_number) ((inode_number - 1) % (cache)->inodes_per_group) - -vnode* ext_make_vnode(ext_cache* cache, uint32_t ino, mount* mnt); - -#ifdef __UINT64_TYPE__ -# if SIZE_MAX==UINT64_MAX -# define ext_sb_supports_64bit_filesize (true) -# define ext_ino_filesize(cache, inode) (le32_to_host((inode)->size) | ((cache)->revision > 1 ? le32_to_host((inode)->dir_acl) : 0)) -# else -# define ext_sb_supports_64bit_filesize (false) -# define ext_ino_filesize(cache, inode) (le32_to_host((inode)->size)) -# endif -#else -# define ext_sb_supports_64bit_filesize (false) -# define ext_ino_filesize(cache, inode) (le32_to_host((inode)->size)) -#endif - -#define ext_sb_block_size(superblock) (1024<log_block_size)) -#define ext_sb_blocks_per_group(superblock) (le16_to_host((superblock)->blocks_per_group)) -#define ext_sb_inodes_per_group(superblock) (le16_to_host((superblock)->inodes_per_group)) -#define ext_sb_inode_size(superblock) (le32_to_host((superblock)->revision) == EXT_DYNAMIC_REV ? le16_to_host((superblock)->dynamic_rev.inode_size) : 128) - -extern allocator_info* EXT_Allocator; \ No newline at end of file diff --git a/src/drivers/generic/freebsd-e1000/CMakeLists.txt b/src/drivers/generic/freebsd-e1000/CMakeLists.txt deleted file mode 100644 index f5c7fa8e..00000000 --- a/src/drivers/generic/freebsd-e1000/CMakeLists.txt +++ /dev/null @@ -1,39 +0,0 @@ -# drivers/generic/freebsd-e1000/CMakeLists.txt -# -# Copyright (c) 2025 Omar Berrow - -add_executable(e1000 "main.c" "io.c" ${E1000_SOURCES}) - -target_compile_options(e1000 - PRIVATE $<$:${TARGET_DRIVER_COMPILE_OPTIONS_C}> - PRIVATE $<$:-ffreestanding> - PRIVATE $<$:-Wall> - PRIVATE $<$:-Wextra> - PRIVATE $<$:-fstack-protector-all> - PRIVATE $<$:-fno-builtin-memset> - PRIVATE $<$:-fvisibility=hidden> - PRIVATE $<$:-fPIC> - PRIVATE $<$:-fno-sanitize=kernel-address> - PRIVATE $<$:-Wno-error> -) - -if (OBOS_ENABLE_FANALYZER) - target_compile_options(e1000 PRIVATE "-fno-analyzer") -endif() - -set_property(TARGET e1000 PROPERTY link_depends ${DRIVER_LINKER_SCRIPT}) - -target_include_directories(e1000 - PRIVATE "${CMAKE_SOURCE_DIR}/src/oboskrnl" - PRIVATE "${CMAKE_SOURCE_DIR}/src/drivers/generic/freebsd-e1000" - PRIVATE ${OBOSKRNL_EXTERNAL_INCLUDES}) - -target_link_options(e1000 - PRIVATE "-nostdlib" - PRIVATE "-fPIC" - PRIVATE "-Wl,-shared" -# PRIVATE "-Wl,--allow-shlib-undefined" - PRIVATE "-T" PRIVATE ${DRIVER_LINKER_SCRIPT} - PRIVATE ${TARGET_DRIVER_LINKER_OPTIONS} -) -target_compile_definitions(e1000 PRIVATE OBOS_DRIVER=1) diff --git a/src/drivers/generic/freebsd-e1000/dev.h b/src/drivers/generic/freebsd-e1000/dev.h deleted file mode 100644 index b99200bf..00000000 --- a/src/drivers/generic/freebsd-e1000/dev.h +++ /dev/null @@ -1,90 +0,0 @@ -#pragma once - -#include -#include - -#include - -#include - -#include -#include -#include - -#include - -#include - -#include - -#include - -#define RX_QUEUE_SIZE (OBOS_PAGE_SIZE / sizeof(union e1000_rx_desc_extended)) -#define TX_QUEUE_SIZE (OBOS_PAGE_SIZE / sizeof(struct e1000_tx_desc)) -#define TX_BUFFER_PAGES (16) - -typedef struct e1000_frame { - void* buff; - size_t size; - size_t refs; - LIST_NODE(e1000_frame_list, struct e1000_frame) node; -} e1000_frame; -typedef LIST_HEAD(e1000_frame_list, e1000_frame) e1000_frame_list; -LIST_PROTOTYPE(e1000_frame_list, e1000_frame, node); - -typedef struct e1000_device { - struct e1000_hw hw; - struct e1000_osdep osdep; - char* interface_name; - vnode* vn; - - pci_resource* irq_res; - irq irq; - uint32_t icr; - dpc dpc; - dpc dpc_tx; - - uintptr_t rx_ring; - uintptr_t rx_ring_buffers[RX_QUEUE_SIZE]; - page* rx_ring_phys_pg; - event rx_evnt; - uint32_t rx_idx; - - uintptr_t tx_ring; - page* tx_ring_phys_pg; - size_t tx_index; - event tx_done_evnt; - uintptr_t tx_buffers[TX_QUEUE_SIZE]; - - size_t refs; - - e1000_frame_list rx_frames; -} e1000_device; - -#define E1000_HANDLE_MAGIC 0xe100070d -typedef struct e1000_handle { - uint32_t magic; - e1000_device* dev; - e1000_frame* rx_curr; - e1000_frame* last_rx; - size_t rx_off; -} e1000_handle; - -#if OBOS_IRQL_COUNT == 16 -# define IRQL_E1000 (9) -#elif OBOS_IRQL_COUNT == 8 -# define IRQL_E1000 (3) -#elif OBOS_IRQL_COUNT == 4 -# define IRQL_E1000 (2) -#elif OBOS_IRQL_COUNT == 2 -# define IRQL_E1000 (0) -#else -# error Funny business. -#endif - -void e1000_init_rx(e1000_device* dev); -void e1000_init_tx(e1000_device* dev); -event* e1000_tx_packet(e1000_device* dev, const void* buffer, size_t size, bool dry, obos_status* status); - -void e1000_irq_handler(struct irq* i, interrupt_frame* frame, void* userdata, irql oldIrql); -bool e1000_check_irq_callback(struct irq* i, void* userdata); \ No newline at end of file diff --git a/src/drivers/generic/freebsd-e1000/device-ids.h b/src/drivers/generic/freebsd-e1000/device-ids.h deleted file mode 100644 index d430312e..00000000 --- a/src/drivers/generic/freebsd-e1000/device-ids.h +++ /dev/null @@ -1,197 +0,0 @@ -#pragma once -#include -#include -static uint16_t device_ids[] = { - E1000_DEV_ID_82542, - E1000_DEV_ID_82543GC_FIBER, - E1000_DEV_ID_82543GC_COPPER, - E1000_DEV_ID_82544EI_COPPER, - E1000_DEV_ID_82544EI_FIBER, - E1000_DEV_ID_82544GC_COPPER, - E1000_DEV_ID_82544GC_LOM, - E1000_DEV_ID_82540EM, - E1000_DEV_ID_82540EM_LOM, - E1000_DEV_ID_82540EP_LOM, - E1000_DEV_ID_82540EP, - E1000_DEV_ID_82540EP_LP, - E1000_DEV_ID_82545EM_COPPER, - E1000_DEV_ID_82545EM_FIBER, - E1000_DEV_ID_82545GM_COPPER, - E1000_DEV_ID_82545GM_FIBER, - E1000_DEV_ID_82545GM_SERDES, - E1000_DEV_ID_82546EB_COPPER, - E1000_DEV_ID_82546EB_FIBER, - E1000_DEV_ID_82546EB_QUAD_COPPER, - E1000_DEV_ID_82546GB_COPPER, - E1000_DEV_ID_82546GB_FIBER, - E1000_DEV_ID_82546GB_SERDES, - E1000_DEV_ID_82546GB_PCIE, - E1000_DEV_ID_82546GB_QUAD_COPPER, - E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3, - E1000_DEV_ID_82541EI, - E1000_DEV_ID_82541EI_MOBILE, - E1000_DEV_ID_82541ER_LOM, - E1000_DEV_ID_82541ER, - E1000_DEV_ID_82541GI, - E1000_DEV_ID_82541GI_LF, - E1000_DEV_ID_82541GI_MOBILE, - E1000_DEV_ID_82547EI, - E1000_DEV_ID_82547EI_MOBILE, - E1000_DEV_ID_82547GI, - E1000_DEV_ID_82571EB_COPPER, - E1000_DEV_ID_82571EB_FIBER, - E1000_DEV_ID_82571EB_SERDES, - E1000_DEV_ID_82571EB_SERDES_DUAL, - E1000_DEV_ID_82571EB_SERDES_QUAD, - E1000_DEV_ID_82571EB_QUAD_COPPER, - E1000_DEV_ID_82571PT_QUAD_COPPER, - E1000_DEV_ID_82571EB_QUAD_FIBER, - E1000_DEV_ID_82571EB_QUAD_COPPER_LP, - E1000_DEV_ID_82572EI_COPPER, - E1000_DEV_ID_82572EI_FIBER, - E1000_DEV_ID_82572EI_SERDES, - E1000_DEV_ID_82572EI, - E1000_DEV_ID_82573E, - E1000_DEV_ID_82573E_IAMT, - E1000_DEV_ID_82573L, - E1000_DEV_ID_82574L, - E1000_DEV_ID_82574LA, - E1000_DEV_ID_82583V, - E1000_DEV_ID_80003ES2LAN_COPPER_DPT, - E1000_DEV_ID_80003ES2LAN_SERDES_DPT, - E1000_DEV_ID_80003ES2LAN_COPPER_SPT, - E1000_DEV_ID_80003ES2LAN_SERDES_SPT, - E1000_DEV_ID_ICH8_82567V_3, - E1000_DEV_ID_ICH8_IGP_M_AMT, - E1000_DEV_ID_ICH8_IGP_AMT, - E1000_DEV_ID_ICH8_IGP_C, - E1000_DEV_ID_ICH8_IFE, - E1000_DEV_ID_ICH8_IFE_GT, - E1000_DEV_ID_ICH8_IFE_G, - E1000_DEV_ID_ICH8_IGP_M, - E1000_DEV_ID_ICH9_IGP_M, - E1000_DEV_ID_ICH9_IGP_M_AMT, - E1000_DEV_ID_ICH9_IGP_M_V, - E1000_DEV_ID_ICH9_IGP_AMT, - E1000_DEV_ID_ICH9_BM, - E1000_DEV_ID_ICH9_IGP_C, - E1000_DEV_ID_ICH9_IFE, - E1000_DEV_ID_ICH9_IFE_GT, - E1000_DEV_ID_ICH9_IFE_G, - E1000_DEV_ID_ICH10_R_BM_LM, - E1000_DEV_ID_ICH10_R_BM_LF, - E1000_DEV_ID_ICH10_R_BM_V, - E1000_DEV_ID_ICH10_D_BM_LM, - E1000_DEV_ID_ICH10_D_BM_LF, - E1000_DEV_ID_ICH10_D_BM_V, - E1000_DEV_ID_PCH_M_HV_LM, - E1000_DEV_ID_PCH_M_HV_LC, - E1000_DEV_ID_PCH_D_HV_DM, - E1000_DEV_ID_PCH_D_HV_DC, - E1000_DEV_ID_PCH2_LV_LM, - E1000_DEV_ID_PCH2_LV_V, - E1000_DEV_ID_PCH_LPT_I217_LM, - E1000_DEV_ID_PCH_LPT_I217_V, - E1000_DEV_ID_PCH_LPTLP_I218_LM, - E1000_DEV_ID_PCH_LPTLP_I218_V, - E1000_DEV_ID_PCH_I218_LM2, - E1000_DEV_ID_PCH_I218_V2, - E1000_DEV_ID_PCH_I218_LM3, /* Wildcat Point PCH */ - E1000_DEV_ID_PCH_I218_V3, /* Wildcat Point PCH */ - E1000_DEV_ID_PCH_SPT_I219_LM, /* Sunrise Point PCH */ - E1000_DEV_ID_PCH_SPT_I219_V, /* Sunrise Point PCH */ - E1000_DEV_ID_PCH_SPT_I219_LM2, /* Sunrise Point-H PCH */ - E1000_DEV_ID_PCH_SPT_I219_V2, /* Sunrise Point-H PCH */ - E1000_DEV_ID_PCH_LBG_I219_LM3, /* LEWISBURG PCH */ - E1000_DEV_ID_PCH_SPT_I219_LM4, - E1000_DEV_ID_PCH_SPT_I219_V4, - E1000_DEV_ID_PCH_SPT_I219_LM5, - E1000_DEV_ID_PCH_SPT_I219_V5, - E1000_DEV_ID_PCH_CNP_I219_LM6, - E1000_DEV_ID_PCH_CNP_I219_V6, - E1000_DEV_ID_PCH_CNP_I219_LM7, - E1000_DEV_ID_PCH_CNP_I219_V7, - E1000_DEV_ID_PCH_ICP_I219_LM8, - E1000_DEV_ID_PCH_ICP_I219_V8, - E1000_DEV_ID_PCH_ICP_I219_LM9, - E1000_DEV_ID_PCH_ICP_I219_V9, - E1000_DEV_ID_PCH_CMP_I219_LM10, - E1000_DEV_ID_PCH_CMP_I219_V10, - E1000_DEV_ID_PCH_CMP_I219_LM11, - E1000_DEV_ID_PCH_CMP_I219_V11, - E1000_DEV_ID_PCH_CMP_I219_LM12, - E1000_DEV_ID_PCH_CMP_I219_V12, - E1000_DEV_ID_PCH_TGP_I219_LM13, - E1000_DEV_ID_PCH_TGP_I219_V13, - E1000_DEV_ID_PCH_TGP_I219_LM14, - E1000_DEV_ID_PCH_TGP_I219_V14, - E1000_DEV_ID_PCH_TGP_I219_LM15, - E1000_DEV_ID_PCH_TGP_I219_V15, - E1000_DEV_ID_PCH_ADL_I219_LM16, - E1000_DEV_ID_PCH_ADL_I219_V16, - E1000_DEV_ID_PCH_ADL_I219_LM17, - E1000_DEV_ID_PCH_ADL_I219_V17, - E1000_DEV_ID_PCH_MTP_I219_LM18, - E1000_DEV_ID_PCH_MTP_I219_V18, - E1000_DEV_ID_PCH_ADL_I219_LM19, - E1000_DEV_ID_PCH_ADL_I219_V19, - E1000_DEV_ID_PCH_LNL_I219_LM20, - E1000_DEV_ID_PCH_LNL_I219_V20, - E1000_DEV_ID_PCH_LNL_I219_LM21, - E1000_DEV_ID_PCH_LNL_I219_V21, - E1000_DEV_ID_PCH_RPL_I219_LM22, - E1000_DEV_ID_PCH_RPL_I219_V22, - E1000_DEV_ID_PCH_RPL_I219_LM23, - E1000_DEV_ID_PCH_RPL_I219_V23, - E1000_DEV_ID_PCH_ARL_I219_LM24, - E1000_DEV_ID_PCH_ARL_I219_V24, - E1000_DEV_ID_PCH_PTP_I219_LM25, - E1000_DEV_ID_PCH_PTP_I219_V25, - E1000_DEV_ID_PCH_PTP_I219_LM26, - E1000_DEV_ID_PCH_PTP_I219_V26, - E1000_DEV_ID_PCH_PTP_I219_LM27, - E1000_DEV_ID_PCH_PTP_I219_V27, - E1000_DEV_ID_82576, - E1000_DEV_ID_82576_FIBER, - E1000_DEV_ID_82576_SERDES, - E1000_DEV_ID_82576_QUAD_COPPER, - E1000_DEV_ID_82576_QUAD_COPPER_ET2, - E1000_DEV_ID_82576_NS, - E1000_DEV_ID_82576_NS_SERDES, - E1000_DEV_ID_82576_SERDES_QUAD, - E1000_DEV_ID_82576_VF, - E1000_DEV_ID_82576_VF_HV, - E1000_DEV_ID_I350_VF, - E1000_DEV_ID_I350_VF_HV, - E1000_DEV_ID_82575EB_COPPER, - E1000_DEV_ID_82575EB_FIBER_SERDES, - E1000_DEV_ID_82575GB_QUAD_COPPER, - E1000_DEV_ID_82580_COPPER, - E1000_DEV_ID_82580_FIBER, - E1000_DEV_ID_82580_SERDES, - E1000_DEV_ID_82580_SGMII, - E1000_DEV_ID_82580_COPPER_DUAL, - E1000_DEV_ID_82580_QUAD_FIBER, - E1000_DEV_ID_I350_COPPER, - E1000_DEV_ID_I350_FIBER, - E1000_DEV_ID_I350_SERDES, - E1000_DEV_ID_I350_SGMII, - E1000_DEV_ID_I350_DA4, - E1000_DEV_ID_I210_COPPER, - E1000_DEV_ID_I210_COPPER_OEM1, - E1000_DEV_ID_I210_COPPER_IT, - E1000_DEV_ID_I210_FIBER, - E1000_DEV_ID_I210_SERDES, - E1000_DEV_ID_I210_SGMII, - E1000_DEV_ID_I210_COPPER_FLASHLESS, - E1000_DEV_ID_I210_SERDES_FLASHLESS, - E1000_DEV_ID_I210_SGMII_FLASHLESS, - E1000_DEV_ID_I211_COPPER, - E1000_DEV_ID_I354_BACKPLANE_1GBPS, - E1000_DEV_ID_I354_SGMII, - E1000_DEV_ID_I354_BACKPLANE_2_5GBPS, - E1000_DEV_ID_DH89XXCC_SGMII, - E1000_DEV_ID_DH89XXCC_SERDES, - E1000_DEV_ID_DH89XXCC_BACKPLANE, - E1000_DEV_ID_DH89XXCC_SFP, -}; \ No newline at end of file diff --git a/src/drivers/generic/freebsd-e1000/e1000_osdep.h b/src/drivers/generic/freebsd-e1000/e1000_osdep.h deleted file mode 100644 index 5dceccfa..00000000 --- a/src/drivers/generic/freebsd-e1000/e1000_osdep.h +++ /dev/null @@ -1,149 +0,0 @@ -/* This file is derived from FreeBSD's e1000_osdep.h and has been adapted for use in managarm, - then taken from managarm to be used in OBOS. -*/ - -/****************************************************************************** - SPDX-License-Identifier: BSD-3-Clause - - Copyright (c) 2001-2020, Intel Corporation - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - 3. Neither the name of the Intel Corporation nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. - -******************************************************************************/ -/*$FreeBSD$*/ - - -#ifndef _FREEBSD_E1000_OSDEP_H_ -#define _FREEBSD_E1000_OSDEP_H_ - -#include -#include -#include -#include -#include -#include -#define ETHER_ADDR_LEN (sizeof(mac_address)) -#define NULL ((void*)0) - -// #define usec_delay(x) usleep(x) -// #define usec_delay_irq(x) usleep(x) -// #define msec_delay(x) usleep(x * 1000) -// #define msec_delay_irq(x) usleep(x * 1000) -extern void e1000_sleep_us(uint64_t us); -#define usec_delay(x) e1000_sleep_us(x) -#define usec_delay_irq(x) e1000_sleep_us(x) -#define msec_delay(x) e1000_sleep_us(x*1000) -#define msec_delay_irq(x) e1000_sleep_us(x*1000) - -#define DEBUGOUT(format, ...) OBOS_Debug("driver/freebsd-e1000: %s %d: " format, __FUNCTION__, __LINE__, ##__VA_ARGS__) -#define DEBUGOUT1(...) DEBUGOUT(__VA_ARGS__) -#define DEBUGOUT2(...) DEBUGOUT(__VA_ARGS__) -#define DEBUGOUT3(...) DEBUGOUT(__VA_ARGS__) -#define DEBUGFUNC(F) - -#define FALSE 0 -#define TRUE 1 - -#define CMD_MEM_WRT_INVALIDATE 0x0010 /* BIT_4 */ - -typedef uint64_t u64; -typedef uint32_t u32; -typedef uint16_t u16; -typedef uint8_t u8; -typedef int64_t s64; -typedef int32_t s32; -typedef int16_t s16; -typedef int8_t s8; -#define __le16 u16 -#define __le32 u32 -#define __le64 u64 - -#define ASSERT_CTX_LOCK_HELD(hw) - -struct e1000_osdep { - pci_device *pci; - pci_capability* cached_cap; - uintptr_t membase; - uintptr_t iobase; - uintptr_t flashbase; -}; - -#define hw2pci(hw) (((struct e1000_osdep*)(hw)->back)->pci) -#define hw2pcicap(hw) (((struct e1000_osdep*)(hw)->back)->cached_cap) -#define hw2membase(hw) (((struct e1000_osdep*)(hw)->back)->membase) -#define hw2iobase(hw) (((struct e1000_osdep*)(hw)->back)->iobase) -#define hw2flashbase(hw) (((struct e1000_osdep*)(hw)->back)->flashbase) - -#define REG64(addr) ((volatile uint64_t *)(uintptr_t)(addr)) -#define REG32(addr) ((volatile uint32_t *)(uintptr_t)(addr)) -#define REG16(addr) ((volatile uint16_t *)(uintptr_t)(addr)) -#define REG8(addr) ((volatile uint8_t *)(uintptr_t)(addr)) - -#define writel(v, a) (*REG32(a) = (v)) -#define readl(a) (*REG32(a)) -#define writew(v, a) (*REG16(a) = (v)) -#define readw(a) (*REG16(a)) - -struct e1000_hw; - -void e1000_io_write(struct e1000_hw *hw, u16 reg, u32 data); - -#define E1000_REGISTER(hw, reg) \ - (((hw)->mac.type >= e1000_82543) ? (u32)(reg) : e1000_translate_register_82542(reg)) - -#define E1000_WRITE_FLUSH(a) E1000_READ_REG(a, E1000_STATUS) - -/* Read from an absolute offset in the adapter's memory space */ -#define E1000_READ_OFFSET(hw, offset) readl((const volatile void*)(uintptr_t)(hw2membase(hw) + (offset))) - -/* Write to an absolute offset in the adapter's memory space */ -#define E1000_WRITE_OFFSET(hw, offset, value) writel((value), (volatile void*)(uintptr_t)(hw2membase(hw) + (offset))) - -/* Register READ/WRITE macros */ -#define E1000_READ_REG(hw, reg) E1000_READ_OFFSET((hw), E1000_REGISTER((hw), (reg))) -#define E1000_WRITE_REG(hw, reg, value) \ - E1000_WRITE_OFFSET((hw), E1000_REGISTER((hw), (reg)), (value)) - -#define E1000_READ_REG_ARRAY(hw, reg, index) \ - E1000_READ_OFFSET((hw), E1000_REGISTER((hw), (reg)) + ((index) << 2)) -#define E1000_WRITE_REG_ARRAY(hw, reg, index, value) \ - E1000_WRITE_OFFSET((hw), E1000_REGISTER((hw), (reg)) + ((index) << 2), (value)) - -#define E1000_READ_REG_ARRAY_DWORD E1000_READ_REG_ARRAY -#define E1000_WRITE_REG_ARRAY_DWORD E1000_WRITE_REG_ARRAY - -#define E1000_WRITE_REG_IO(hw, reg, value) e1000_io_write(hw, reg, value) - -#define E1000_READ_FLASH_REG(hw, reg) readl((const volatile void*)(uintptr_t)(hw2flashbase(hw) + (reg))) -#define E1000_READ_FLASH_REG16(hw, reg) readw((const volatile void*)(uintptr_t)(hw2flashbase(hw) + (reg))) -#define E1000_WRITE_FLASH_REG(hw, reg, value) writel((value), (volatile void*)(uintptr_t)(hw2flashbase(hw) + (reg))) -#define E1000_WRITE_FLASH_REG16(hw, reg, value) writew((value), (volatile void*)(uintptr_t)(hw2flashbase(hw) + (reg))) - -#define ASSERT_NO_LOCKS() - -#endif /* _FREEBSD_E1000_OSDEP_H_ */ \ No newline at end of file diff --git a/src/drivers/generic/freebsd-e1000/io.c b/src/drivers/generic/freebsd-e1000/io.c deleted file mode 100644 index fabd8522..00000000 --- a/src/drivers/generic/freebsd-e1000/io.c +++ /dev/null @@ -1,478 +0,0 @@ -/* - * drivers/generic/freebsd-e1000/io.c - * - * Copyright (c) 2025-2026 Omar Berrow - * - * RX/TX function for the E1000 driver -*/ - -#include -#include - -#include - -#include "e1000_osdep.h" - -#include -#include -#include - -#include -#include - -#include - -#include - -#include - -#include -#include - -#include - -#include "dev.h" - -DefineNetFreeSharedPtr - -LIST_GENERATE(e1000_frame_list, e1000_frame, node); - -#define EM_RADV 64 -#define EM_RDTR 0 - -#define IGB_RX_PTHRESH \ - ((dev->hw.mac.type == e1000_i354) ? 12 : ((dev->hw.mac.type <= e1000_82576) ? 16 : 8)) -#define IGB_RX_HTHRESH 8 -#define IGB_RX_WTHRESH ((dev->hw.mac.type == e1000_82576) ? 1 : 4) -#define IGB_TX_PTHRESH ((dev->hw.mac.type == e1000_i354) ? 20 : 8) -#define IGB_TX_HTHRESH 1 -#define IGB_TX_WTHRESH ((dev->hw.mac.type != e1000_82575) ? 1 : 16) - -#define MAX_INTS_PER_SEC 8000 -#define DEFAULT_ITR (1000000000 / (MAX_INTS_PER_SEC * 256)) - -static void e1000_init_rx_desc(e1000_device* dev) -{ - OBOS_STATIC_ASSERT((RX_QUEUE_SIZE * sizeof(union e1000_rx_desc_extended)) <= OBOS_PAGE_SIZE, "RX_QUEUE_SIZE is too large!"); - dev->rx_ring_phys_pg = MmH_PgAllocatePhysical(false, false); - dev->rx_ring = dev->rx_ring_phys_pg->phys; - union e1000_rx_desc_extended* desc = MmS_MapVirtFromPhys(dev->rx_ring); - memzero(desc, sizeof(*desc) * RX_QUEUE_SIZE); - for (size_t i = 0; i < RX_QUEUE_SIZE; i++) - { - dev->rx_ring_buffers[i] = Mm_AllocatePhysicalPages(1, 1, nullptr); - desc[i].read.buffer_addr = dev->rx_ring_buffers[i]; - desc[i].wb.upper.status_error = 0; - } -} - -void e1000_init_rx(e1000_device* dev) -{ - e1000_init_rx_desc(dev); - - uint32_t rctl = E1000_READ_REG(&dev->hw, E1000_RCTL); - - // inspired by managarm - - if (dev->hw.mac.type != e1000_82574 && dev->hw.mac.type != e1000_82583) - E1000_WRITE_REG(&dev->hw, E1000_RCTL, rctl & ~E1000_RCTL_EN); - - rctl &= ~(3 << E1000_RCTL_MO_SHIFT); - rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF | (dev->hw.mac.mc_filter_type << E1000_RCTL_MO_SHIFT); - rctl &= ~E1000_RCTL_SBP; - rctl &= ~E1000_RCTL_UPE; - rctl &= ~E1000_RCTL_MPE; - rctl &= ~E1000_RCTL_LPE; - rctl &= ~E1000_RCTL_SECRC; - - // taken from managarm - if(dev->hw.mac.type >= e1000_82540) { - E1000_WRITE_REG(&dev->hw, E1000_RADV, EM_RADV); - /* - * Set the interrupt throttling rate. Value is calculated - * as DEFAULT_ITR = 1/(MAX_INTS_PER_SEC * 256ns) - */ - E1000_WRITE_REG(&dev->hw, E1000_ITR, DEFAULT_ITR); - } - - E1000_WRITE_REG(&dev->hw, E1000_RDTR, EM_RDTR); - - /* Use extended rx descriptor formats */ - u32 rfctl = E1000_READ_REG(&dev->hw, E1000_RFCTL); - rfctl |= E1000_RFCTL_EXTEN; - - /* - * When using MSIX interrupts we need to throttle - * using the EITR register (82574 only) - */ - if (dev->hw.mac.type == e1000_82574) { - for (int i = 0; i < 4; i++) { - E1000_WRITE_REG(&dev->hw, E1000_EITR_82574(i), DEFAULT_ITR); - } - /* Disable accelerated acknowledge */ - rfctl |= E1000_RFCTL_ACK_DIS; - } - - E1000_WRITE_REG(&dev->hw, E1000_RFCTL, rfctl); - // u32 rxcsum = E1000_READ_REG(&dev->hw, E1000_RXCSUM); - // rxcsum &= ~E1000_RXCSUM_TUOFL; - // rxcsum &= ~E1000_RXCSUM_IPOFL; - E1000_WRITE_REG(&dev->hw, E1000_RXCSUM, 0); - - /* - * XXX TEMPORARY WORKAROUND: on some systems with 82573 - * long latencies are observed, like Lenovo X60. This - * change eliminates the problem, but since having positive - * values in RDTR is a known source of problems on other - * platforms another solution is being sought. - */ - if (dev->hw.mac.type == e1000_82573) { - E1000_WRITE_REG(&dev->hw, E1000_RDTR, 0x20); - } - - // Ring slightly modified. - E1000_WRITE_REG(&dev->hw, E1000_RDLEN(0), RX_QUEUE_SIZE * sizeof(union e1000_rx_desc_extended)); - E1000_WRITE_REG(&dev->hw, E1000_RDBAH(0), (u32)(dev->rx_ring >> 32)); - E1000_WRITE_REG(&dev->hw, E1000_RDBAL(0), (u32)dev->rx_ring); - - /* - * Set PTHRESH for improved jumbo performance - * According to 10.2.5.11 of Intel 82574 Datasheet, - * RXDCTL(1) is written whenever RXDCTL(0) is written. - * Only write to RXDCTL(1) if there is a need for different - * settings. - */ - if (dev->hw.mac.type == e1000_82574) { - u32 rxdctl = E1000_READ_REG(&dev->hw, E1000_RXDCTL(0)); - - rxdctl |= 0x20; /* PTHRESH */ - rxdctl |= 4 << 8; /* HTHRESH */ - rxdctl |= 4 << 16; /* WTHRESH */ - rxdctl |= 1 << 24; /* Switch to granularity */ - - E1000_WRITE_REG(&dev->hw, E1000_RXDCTL(0), rxdctl); - } else if (dev->hw.mac.type >= e1000_82575) { - u32 srrctl = 2048 >> E1000_SRRCTL_BSIZEPKT_SHIFT; - rctl |= E1000_RCTL_SZ_2048; - - /* Setup the Base and Length of the Rx Descriptor Rings */ - - u32 rxdctl; - srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF; - E1000_WRITE_REG(&dev->hw, E1000_RDLEN(0), RX_QUEUE_SIZE * sizeof(struct e1000_rx_desc)); - E1000_WRITE_REG(&dev->hw, E1000_RDBAH(0), (uint32_t)(dev->rx_ring >> 32)); - E1000_WRITE_REG(&dev->hw, E1000_RDBAL(0), (uint32_t)dev->rx_ring); - E1000_WRITE_REG(&dev->hw, E1000_SRRCTL(0), srrctl); - - /* Enable this Queue */ - rxdctl = E1000_READ_REG(&dev->hw, E1000_RXDCTL(0)); - rxdctl |= E1000_RXDCTL_QUEUE_ENABLE; - rxdctl &= 0xFFF00000; - rxdctl |= IGB_RX_PTHRESH; - rxdctl |= IGB_RX_HTHRESH << 8; - rxdctl |= IGB_RX_WTHRESH << 16; - - E1000_WRITE_REG(&dev->hw, E1000_RXDCTL(0), rxdctl); - - /* poll for enable completion */ - do { - rxdctl = E1000_READ_REG(&dev->hw, E1000_RXDCTL(0)); - } while (!(rxdctl & E1000_RXDCTL_QUEUE_ENABLE)); - } else if (dev->hw.mac.type >= e1000_pch2lan) { - e1000_lv_jumbo_workaround_ich8lan(&dev->hw, false); - } - - /* Make sure VLAN Filters are off */ - rctl &= ~E1000_RCTL_VFE; - - if (dev->hw.mac.type < e1000_82575) { - rctl |= E1000_RCTL_SZ_2048; - /* ensure we clear use DTYPE of 00 here */ - rctl &= ~0x00000C00; - } - - /* Setup the Head and Tail Descriptor Pointers */ - E1000_WRITE_REG(&dev->hw, E1000_RDH(0), 0); - E1000_WRITE_REG(&dev->hw, E1000_RDT(0), RX_QUEUE_SIZE - 1); - - /* Write out the settings */ - E1000_WRITE_REG(&dev->hw, E1000_RCTL, rctl); -} - -void e1000_init_tx(e1000_device* dev) -{ - dev->tx_ring_phys_pg = MmH_PgAllocatePhysical(false, false); - dev->tx_ring = dev->tx_ring_phys_pg->phys; - - // Copied from managarm - E1000_WRITE_REG(&dev->hw, E1000_TDLEN(0), TX_QUEUE_SIZE * sizeof(struct e1000_tx_desc)); - E1000_WRITE_REG(&dev->hw, E1000_TDBAH(0), (u32)(dev->tx_ring >> 32)); - E1000_WRITE_REG(&dev->hw, E1000_TDBAL(0), (u32)dev->tx_ring); - - /* Init the HEAD/TAIL indices */ - E1000_WRITE_REG(&dev->hw, E1000_TDH(0), 0); - E1000_WRITE_REG(&dev->hw, E1000_TDT(0), 0); - - u32 txdctl = 0; /* clear txdctl */ - txdctl |= 0x1f; /* PTHRESH */ - txdctl |= 1 << 8; /* HTHRESH */ - txdctl |= 1 << 16; /* WTHRESH */ - txdctl |= 1 << 22; /* Reserved bit 22 must always be 1 */ - txdctl |= E1000_TXDCTL_GRAN; - txdctl |= 1 << 25; /* LWTHRESH */ - - E1000_WRITE_REG(&dev->hw, E1000_TXDCTL(0), txdctl); - - u32 tipg = 0; - - /* Set the default values for the Tx Inter Packet Gap timer */ - switch (dev->hw.mac.type) { - case e1000_80003es2lan: - tipg = DEFAULT_82543_TIPG_IPGR1; - tipg |= DEFAULT_80003ES2LAN_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT; - break; - case e1000_82542: - tipg = DEFAULT_82542_TIPG_IPGT; - tipg |= DEFAULT_82542_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT; - tipg |= DEFAULT_82542_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT; - break; - default: - if ((dev->hw.phy.media_type == e1000_media_type_fiber) || - (dev->hw.phy.media_type == e1000_media_type_internal_serdes)) - tipg = DEFAULT_82543_TIPG_IPGT_FIBER; - else - tipg = DEFAULT_82543_TIPG_IPGT_COPPER; - tipg |= DEFAULT_82543_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT; - tipg |= DEFAULT_82543_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT; - } - - E1000_WRITE_REG(&dev->hw, E1000_TIPG, tipg); - E1000_WRITE_REG(&dev->hw, E1000_TIDV, 0); - - if (dev->hw.mac.type >= e1000_82540) - E1000_WRITE_REG(&dev->hw, E1000_TADV, 0); - - u32 tarc = 0; - -#define TARC_SPEED_MODE_BIT (1 << 21) /* On PCI-E MACs only */ -#define TARC_ERRATA_BIT (1 << 26) /* Note from errata on 82574 */ - - if ((dev->hw.mac.type == e1000_82571) || (dev->hw.mac.type == e1000_82572)) { - tarc = E1000_READ_REG(&dev->hw, E1000_TARC(0)); - tarc |= TARC_SPEED_MODE_BIT; - E1000_WRITE_REG(&dev->hw, E1000_TARC(0), tarc); - } else if (dev->hw.mac.type == e1000_80003es2lan) { - /* errata: program both queues to unweighted RR */ - tarc = E1000_READ_REG(&dev->hw, E1000_TARC(0)); - tarc |= 1; - E1000_WRITE_REG(&dev->hw, E1000_TARC(0), tarc); - tarc = E1000_READ_REG(&dev->hw, E1000_TARC(1)); - tarc |= 1; - E1000_WRITE_REG(&dev->hw, E1000_TARC(1), tarc); - } else if (dev->hw.mac.type == e1000_82574) { - tarc = E1000_READ_REG(&dev->hw, E1000_TARC(0)); - tarc |= TARC_ERRATA_BIT; - E1000_WRITE_REG(&dev->hw, E1000_TARC(0), tarc); - } - - /* Program the Transmit Control Register */ - u32 tctl = E1000_READ_REG(&dev->hw, E1000_TCTL); - tctl &= ~E1000_TCTL_CT; - tctl |= (E1000_TCTL_RTLC | E1000_TCTL_EN | E1000_TCTL_PSP | (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT)); - - if (dev->hw.mac.type >= e1000_82571) - tctl |= E1000_TCTL_MULR; - - /* This write will effectively turn on the transmit unit. */ - E1000_WRITE_REG(&dev->hw, E1000_TCTL, tctl); - - /* SPT and KBL errata workarounds */ - if (dev->hw.mac.type == e1000_pch_spt) { - u32 reg; - reg = E1000_READ_REG(&dev->hw, E1000_IOSFPC); - reg |= E1000_RCTL_RDMTS_HEX; - E1000_WRITE_REG(&dev->hw, E1000_IOSFPC, reg); - /* i218-i219 Specification Update 1.5.4.5 */ - reg = E1000_READ_REG(&dev->hw, E1000_TARC(0)); - reg &= ~E1000_TARC0_CB_MULTIQ_3_REQ; - reg |= E1000_TARC0_CB_MULTIQ_2_REQ; - E1000_WRITE_REG(&dev->hw, E1000_TARC(0), reg); - } - - struct e1000_tx_desc* desc = ((struct e1000_tx_desc*)MmS_MapVirtFromPhys(dev->tx_ring)); - for (size_t i = 0; i < TX_QUEUE_SIZE; i++) - { - dev->tx_buffers[i] = Mm_AllocatePhysicalPages(TX_BUFFER_PAGES, 1, nullptr); - desc[i].buffer_addr = dev->tx_buffers[i]; - desc[i].lower.data = 0; - desc[i].upper.data = 0; - } - OBOS_ENSURE(memcmp_b(desc, 0, sizeof(*desc)*TX_QUEUE_SIZE) == false); -} - -void e1000_tx_reap(e1000_device* dev); - -event* e1000_tx_packet(e1000_device* dev, const void* buffer, size_t size, bool dry, obos_status* status) -{ - e1000_tx_reap(dev); - volatile struct e1000_tx_desc* desc = &((volatile struct e1000_tx_desc*)MmS_MapVirtFromPhys(dev->tx_ring))[dev->tx_index % TX_QUEUE_SIZE]; - if (dry) - return nullptr; - irql oldIrql = Core_RaiseIrql(IRQL_E1000); - - size_t nPages = size / OBOS_PAGE_SIZE; - if (size % OBOS_PAGE_SIZE) - nPages++; - if (nPages > TX_BUFFER_PAGES) - { - if (status) - *status = OBOS_STATUS_MESSAGE_TOO_BIG; - Core_LowerIrql(oldIrql); - return nullptr; - } - - uintptr_t buff = dev->tx_buffers[dev->tx_index % TX_QUEUE_SIZE]; - memcpy(MmS_MapVirtFromPhys(buff), buffer, size); - desc->buffer_addr = buff; - desc->upper.data = 0; - desc->lower.data = size | E1000_TXD_CMD_EOP | E1000_TXD_CMD_RS; - E1000_WRITE_REG(&dev->hw, E1000_TDBAH(0), (u32)(dev->tx_ring >> 32)); - E1000_WRITE_REG(&dev->hw, E1000_TDBAL(0), (u32)dev->tx_ring); - E1000_WRITE_REG(&dev->hw, E1000_TDT(0), ++dev->tx_index % TX_QUEUE_SIZE); - - Core_LowerIrql(oldIrql); - - uintptr_t deadline = CoreS_GetTimerTick() + CoreH_TimeFrameToTick(1*1000*1000); - while (~desc->upper.data & E1000_TXD_STAT_DD && CoreS_GetTimerTick() < deadline) - Core_Yield(); - if (~desc->upper.data & E1000_TXD_STAT_DD) - *status = OBOS_STATUS_TIMED_OUT; - - return nullptr; -} - - -static void rx_dpc(dpc* d, void* udata) -{ - OBOS_UNUSED(d); - e1000_device* dev = udata; - - vnode* const nic = dev->vn; - - e1000_frame* current_frame = nullptr; - size_t offset = 0; - - while (true) - { - uint32_t length = 0; - bool eop = false; - if(dev->hw.mac.type >= e1000_82547) - { - union e1000_rx_desc_extended* desc = &((union e1000_rx_desc_extended*)MmS_MapVirtFromPhys(dev->rx_ring))[(dev->rx_idx % RX_QUEUE_SIZE)]; - if (~desc->wb.upper.status_error & E1000_RXD_STAT_DD) - break; - length = desc->wb.upper.length; - eop = desc->wb.upper.status_error & E1000_RXD_STAT_EOP; - desc->wb.upper.status_error = 0; - desc->read.buffer_addr = dev->rx_ring_buffers[(dev->rx_idx % RX_QUEUE_SIZE)]; - } - else - { - struct e1000_rx_desc* desc = &((struct e1000_rx_desc*)MmS_MapVirtFromPhys(dev->rx_ring))[(dev->rx_idx % RX_QUEUE_SIZE)]; - if (~desc->status & E1000_RXD_STAT_DD) - break; - eop = desc->status & E1000_RXD_STAT_EOP; - length = desc->length; - desc->status = 0; - } - if (!current_frame) - current_frame = ZeroAllocate(OBOS_NonPagedPoolAllocator, 1, sizeof(e1000_frame), nullptr); - current_frame->size += length; - current_frame->buff = Reallocate(OBOS_NonPagedPoolAllocator, current_frame->buff, current_frame->size, current_frame->size - length, nullptr); - current_frame->refs = dev->refs; - memcpy((char*)current_frame->buff + offset, MmS_MapVirtFromPhys(dev->rx_ring_buffers[(dev->rx_idx % RX_QUEUE_SIZE)]), length); - if (eop) - { - if (current_frame->size < 14) - { - NetError("e1000: dropping misplaced runt!\n"); - Free(OBOS_NonPagedPoolAllocator, current_frame->buff, current_frame->size); - Free(OBOS_NonPagedPoolAllocator, current_frame, sizeof(e1000_frame)); - current_frame = nullptr; - offset = 0; - ++dev->rx_idx; - continue; - } - - if (nic->net_tables) - { - shared_ptr* buf = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(shared_ptr), nullptr); - OBOS_SharedPtrConstructSz(buf, current_frame->buff, current_frame->size); - buf->free = OBOS_SharedPtrDefaultFree; - buf->onDeref = NetFreeSharedPtr; - buf->freeUdata = OBOS_NonPagedPoolAllocator; - current_frame->refs--; - if (!current_frame->refs) - Free(OBOS_NonPagedPoolAllocator, current_frame, sizeof(e1000_frame)); - - Net_EthernetProcess(nic, 0, OBOS_SharedPtrCopy(buf), buf->obj, buf->szObj, nullptr); - } - - LIST_APPEND(e1000_frame_list, &dev->rx_frames, current_frame); - current_frame = nullptr; - offset = 0; - } - else - offset += length; - - ++dev->rx_idx; - } - E1000_WRITE_REG(&dev->hw, E1000_RDT(0), ((dev->rx_idx-1) % RX_QUEUE_SIZE)); - if (nic->net_tables) - Net_TCPFlushACKs(nic->net_tables); - Core_EventSet(&dev->rx_evnt, false); -} -void e1000_rx(e1000_device* dev) -{ - dev->dpc.userdata = dev; - CoreH_InitializeDPC(&dev->dpc, rx_dpc, Core_DefaultThreadAffinity); -} - -static void tx_event_set_dpc(dpc* d, void* udata) -{ - OBOS_UNUSED(d); - e1000_device* dev = udata; - Core_EventSet(&dev->tx_done_evnt, false); -} -void e1000_tx_reap(e1000_device* dev) -{ - volatile struct e1000_tx_desc* desc = &((struct e1000_tx_desc*)MmS_MapVirtFromPhys(dev->tx_ring))[0]; - size_t i = dev->tx_index; - for (; ; ) - { - if (~desc[i].upper.data & E1000_TXD_STAT_DD) - break; - desc[i].upper.fields.status = 0; - desc[i].lower.data = 0; - desc[i].buffer_addr = 0; - i++; - } - dev->tx_index = i; -} - -void e1000_irq_handler(struct irq* i, interrupt_frame* frame, void* userdata, irql oldIrql) -{ - OBOS_UNUSED(i && frame && oldIrql); - e1000_device* dev = userdata; - if (dev->icr & E1000_ICR_RXT0) - e1000_rx(dev); - dev->icr = 0; -} - -bool e1000_check_irq_callback(struct irq* i, void* userdata) -{ - OBOS_UNUSED(i); - e1000_device* dev = userdata; - dev->icr |= E1000_READ_REG(&dev->hw, E1000_ICR); - return dev->icr != 0; -} \ No newline at end of file diff --git a/src/drivers/generic/freebsd-e1000/main.c b/src/drivers/generic/freebsd-e1000/main.c deleted file mode 100644 index 59bf1a66..00000000 --- a/src/drivers/generic/freebsd-e1000/main.c +++ /dev/null @@ -1,589 +0,0 @@ -/* - * drivers/generic/freebsd-1000/main.c - * - * Copyright (c) 2025-2026 Omar Berrow -*/ - -#include -#include -#include -#include - -#include - -#include - -#include -#include - -#include -#include - -#include -#include -#include -#include - -#include - -#include -#include -#include - -#include "dev.h" -#include "e1000/e1000_defines.h" - -OBOS_PAGEABLE_FUNCTION obos_status get_blk_size(dev_desc desc, size_t* blkSize) -{ - OBOS_UNUSED(desc); - if (!blkSize) - return OBOS_STATUS_INVALID_ARGUMENT; - *blkSize = 1; - return OBOS_STATUS_SUCCESS; -} -obos_status get_max_blk_count(dev_desc desc, size_t* count) -{ - OBOS_UNUSED(desc && count); - return OBOS_STATUS_INVALID_OPERATION; -} -obos_status ioctl(dev_desc what, uint32_t request, void* argp) -{ - e1000_handle* hnd = (void*)what; - if (hnd->magic != E1000_HANDLE_MAGIC) - return OBOS_STATUS_INVALID_ARGUMENT; - switch (request) { - case IOCTL_IFACE_MAC_REQUEST: - { - obos_status status = OBOS_CapabilityCheck("net/mac-query", true); - if (obos_is_error(status)) - break; - e1000_read_mac_addr(&hnd->dev->hw); - memcpy(argp, hnd->dev->hw.mac.addr, sizeof(mac_address)); - break; - } - default: - return Net_InterfaceIoctl(hnd->dev->vn, request, argp); - } - return OBOS_STATUS_SUCCESS; -} -obos_status ioctl_argp_size(uint32_t request, size_t* out) -{ - switch (request) { - case IOCTL_IFACE_MAC_REQUEST: - { - *out = sizeof(mac_address); - break; - } - default: - return Net_InterfaceIoctlArgpSize(request, out); - } - return OBOS_STATUS_SUCCESS; -} -void driver_cleanup_callback() -{ - // TODO: cleanup -} - -static void irp_on_rx_event_set(irp* req) -{ - e1000_handle* hnd = (void*)req->desc; - e1000_device* dev = hnd->dev; - if (req->evnt) - Core_EventClear(req->evnt); - if (!hnd->rx_curr) - hnd->rx_curr = LIST_GET_TAIL(e1000_frame_list, &hnd->dev->rx_frames); - if (!hnd->rx_curr) - { - req->status = OBOS_STATUS_IRP_RETRY; - return; - } - req->status = OBOS_STATUS_SUCCESS; - if (req->dryOp) - { - if (hnd->rx_curr) - req->nBlkRead = hnd->rx_curr->size; - return; - } - - size_t szRead = OBOS_MIN(req->blkCount, hnd->rx_curr->size - hnd->rx_off); - memcpy(req->buff, hnd->rx_curr->buff + hnd->rx_off, szRead); - hnd->rx_off += szRead; - if (hnd->rx_off >= hnd->rx_curr->size) - { - e1000_frame* next = LIST_GET_NEXT(e1000_frame_list, &dev->rx_frames, hnd->rx_curr); - hnd->last_rx = hnd->rx_curr; - if (!(--hnd->rx_curr->refs)) - { - hnd->last_rx = nullptr; - LIST_REMOVE(e1000_frame_list, &dev->rx_frames, hnd->rx_curr); - Free(OBOS_NonPagedPoolAllocator, hnd->rx_curr->buff, hnd->rx_curr->size); - Free(OBOS_NonPagedPoolAllocator, hnd->rx_curr, sizeof(*hnd->rx_curr)); - // hnd->rx_curr = 0; - } - hnd->rx_curr = next; - hnd->rx_off = 0; - } - else - OBOS_ASSERT(!"what"); - req->nBlkRead = szRead; -} - -static void irp_on_tx_event_set(irp* req) -{ - e1000_handle* hnd = (void*)req->desc; - if (req->evnt) - Core_EventClear(req->evnt); - req->status = OBOS_STATUS_SUCCESS; - req->evnt = e1000_tx_packet(hnd->dev, req->cbuff,req->blkCount, req->dryOp, &req->status); - req->on_event_set = nullptr; - if (obos_is_success(req->status)) - req->status = req->evnt ? OBOS_STATUS_IRP_RETRY : OBOS_STATUS_SUCCESS; - if (req->status) - req->nBlkWritten = req->blkCount; -} - -obos_status submit_irp(void* request) -{ - irp* req = request; - e1000_handle* hnd = (void*)req->desc; - if (!hnd || hnd->magic != E1000_HANDLE_MAGIC) - return OBOS_STATUS_INVALID_ARGUMENT; - if (req->op == IRP_READ) - { - if (!hnd->rx_curr) - hnd->rx_curr = LIST_GET_HEAD(e1000_frame_list, &hnd->dev->rx_frames); - if (!hnd->rx_curr || hnd->last_rx == hnd->rx_curr) - { - hnd->rx_curr = nullptr; - req->evnt = &hnd->dev->rx_evnt; - req->on_event_set = irp_on_rx_event_set; - } - else - irp_on_rx_event_set(req); - } - else - { - req->status = OBOS_STATUS_SUCCESS; - req->evnt = e1000_tx_packet(hnd->dev, req->cbuff, req->blkCount, req->dryOp, &req->status); - req->on_event_set = irp_on_tx_event_set; - if (obos_is_success(req->status)) - req->nBlkWritten = req->blkCount; - } - return OBOS_STATUS_SUCCESS; -} -obos_status finalize_irp(void* request) -{ - irp* req = request; - if (!req) return OBOS_STATUS_INVALID_ARGUMENT; - // if (req->evnt) - // Core_EventClear(req->evnt); - return OBOS_STATUS_SUCCESS; -} - -obos_status reference_device(dev_desc *pdesc) -{ - if (!pdesc || !*pdesc) - return OBOS_STATUS_INVALID_ARGUMENT; - e1000_device* dev = (void*)*pdesc; - e1000_handle* hnd = ZeroAllocate(OBOS_NonPagedPoolAllocator, 1, sizeof(e1000_handle),nullptr); - hnd->rx_curr = LIST_GET_TAIL(e1000_frame_list, &dev->rx_frames); - hnd->rx_off = 0; - hnd->dev = dev; - hnd->magic = E1000_HANDLE_MAGIC; - dev->refs++; - *pdesc = (dev_desc)hnd; - return OBOS_STATUS_SUCCESS; -} -obos_status unreference_device(dev_desc desc) -{ - if (!desc) - return OBOS_STATUS_INVALID_ARGUMENT; - e1000_handle* hnd = (void*)desc; - hnd->dev->refs--; - Free(OBOS_NonPagedPoolAllocator, hnd, sizeof(*hnd)); - return OBOS_STATUS_SUCCESS; -} - -__attribute__((section(OBOS_DRIVER_HEADER_SECTION))) driver_header drv_hdr = { - .magic = OBOS_DRIVER_MAGIC, - .flags = DRIVER_HEADER_HAS_STANDARD_INTERFACES | - DRIVER_HEADER_FLAGS_DETECT_VIA_PCI | - DRIVER_HEADER_PCI_IGNORE_PROG_IF | - DRIVER_HEADER_HAS_VERSION_FIELD, - .pciId = { - .indiv = { - .classCode = 0x02 , // Network Controller - .subClass = 0x00 , // Ethernet Controller - .progIf = 0x00 , // Ignored - } - }, - .ftable = { - .driver_cleanup_callback = driver_cleanup_callback, - .ioctl = ioctl, - .ioctl_argp_size = ioctl_argp_size, - .get_blk_size = get_blk_size, - .get_max_blk_count = get_max_blk_count, - .query_user_readable_name = nullptr, - .foreach_device = nullptr, - .read_sync = nullptr, - .write_sync = nullptr, - .submit_irp = submit_irp, - .finalize_irp = finalize_irp, - .reference_device = reference_device, - .unreference_device = unreference_device, - }, - .driverName = "E1000 Driver", - .version = 1, - .uacpi_init_level_required = PCI_IRQ_UACPI_INIT_LEVEL -}; - -#include "device-ids.h" - -#include - -void e1000_sleep_us(uint64_t us) -{ - timer_tick deadline = CoreS_GetTimerTick() + CoreH_TimeFrameToTick(us); - while (deadline > CoreS_GetTimerTick()) - OBOSS_SpinlockHint(); -} - -static driver_id* this_driver; - -e1000_device *Devices = nullptr; -size_t nDevices = 0, nInitializedDevices = 0; - -static void* map_registers(uintptr_t phys, size_t size, bool uc, bool mmio, bool ref_twice) -{ - size_t phys_page_offset = (phys % OBOS_PAGE_SIZE); - phys -= phys_page_offset; - size = size + (OBOS_PAGE_SIZE - (size % OBOS_PAGE_SIZE)); - size += phys_page_offset; - void* virt = Mm_VirtualMemoryAlloc( - &Mm_KernelContext, - nullptr, size, - uc ? OBOS_PROTECTION_CACHE_DISABLE : 0, VMA_FLAGS_NON_PAGED, - nullptr, - nullptr); - for (uintptr_t offset = 0; offset < size; offset += OBOS_PAGE_SIZE) - { - page_info page = {.virt=offset+(uintptr_t)virt}; - MmS_QueryPageInfo(Mm_KernelContext.pt, page.virt, &page, nullptr); - do { - struct page what = {.phys=page.phys}; - Core_MutexAcquire(&Mm_PhysicalPagesLock); - struct page* pg = RB_FIND(phys_page_tree, &Mm_PhysicalPages, &what); - Core_MutexRelease(&Mm_PhysicalPagesLock); - MmH_DerefPage(pg); - } while(0); - page.prot.uc = uc; - page.phys = phys+offset; - struct page* pg = MmH_AllocatePage(page.phys, false); - if (mmio) - pg->flags |= PHYS_PAGE_MMIO; - if (ref_twice) - MmH_RefPage(pg); - pg->pagedCount++; - MmS_SetPageMapping(Mm_KernelContext.pt, &page, phys + offset, false); - } - Drv_TLBShootdown(Mm_KernelContext.pt, (uintptr_t)virt, size); - return virt+phys_page_offset; -} - -static void init_e1000(pci_bus* bus, pci_device* dev, size_t dev_index) -{ - pci_resource* bar0 = nullptr; - pci_resource* bar1 = nullptr; - pci_resource* io_bar = nullptr; - pci_resource* irq_res = nullptr; - for (pci_resource* res = LIST_GET_HEAD(pci_resource_list, &dev->resources); res; ) - { - if (res->type == PCI_RESOURCE_BAR && res->bar->idx == 0) - bar0 = res; - if (res->type == PCI_RESOURCE_BAR && res->bar->idx == 1) - bar1 = res; - if (res->type == PCI_RESOURCE_BAR && res->bar->type == PCI_BARIO) - io_bar = res; - if (res->type == PCI_RESOURCE_IRQ) - irq_res = res; - - if (bar0 && irq_res && io_bar && bar1) - break; - - res = LIST_GET_NEXT(pci_resource_list, &dev->resources, res); - } - - if (!bar0 || !irq_res) - { - OBOS_Warning("%02x:%02x:%02x: Bogus E1000 PCI node.\n", dev->location.bus, dev->location.slot, dev->location.function); - dev = LIST_GET_NEXT(pci_device_list, &bus->devices, dev); - return; - } - - // for (volatile bool b = true; b;) - // OBOSS_SpinlockHint(); - - dev->resource_cmd_register->cmd_register |= 0x7; - Drv_PCISetResource(dev->resource_cmd_register); - - Devices[dev_index].hw.back = &Devices[dev_index].osdep; - Devices[dev_index].osdep.pci = dev; - Devices[dev_index].osdep.iobase = io_bar ? io_bar->bar->iospace : 0; - Devices[dev_index].osdep.membase = (uintptr_t)map_registers(bar0->bar->phys, bar0->bar->size, true, true, false); - Devices[dev_index].hw.io_base = Devices[dev_index].osdep.iobase; - Devices[dev_index].hw.hw_addr = (void*)Devices[dev_index].osdep.membase; - Devices[dev_index].hw.vendor_id = dev->hid.indiv.vendorId; - Devices[dev_index].hw.device_id = dev->hid.indiv.deviceId; - Devices[dev_index].hw.revision_id = 0; - e1000_read_pci_cfg(&Devices[dev_index].hw, 0x2c, &Devices[dev_index].hw.subsystem_vendor_id); - e1000_read_pci_cfg(&Devices[dev_index].hw, 0x2e, &Devices[dev_index].hw.subsystem_device_id); - if (e1000_set_mac_type(&Devices[dev_index].hw) != E1000_SUCCESS) - { - Mm_VirtualMemoryFree(&Mm_KernelContext, (void*)Devices[dev_index].osdep.membase, bar0->bar->size); - nDevices--; - Devices = Reallocate(OBOS_NonPagedPoolAllocator, Devices, nDevices*sizeof(e1000_device), (nDevices+1)*sizeof(e1000_device), nullptr); - OBOS_Warning("%02x:%02x:%02x: Bogus E1000 PCI node.\n", dev->location.bus, dev->location.slot, dev->location.function); - dev = LIST_GET_NEXT(pci_device_list, &bus->devices, dev); - return; - } - if (!io_bar && (Devices[dev_index].hw.mac.type < e1000_82547 && Devices[dev_index].hw.mac.type > e1000_82543)) - { - Mm_VirtualMemoryFree(&Mm_KernelContext, (void*)Devices[dev_index].osdep.membase, bar0->bar->size); - nDevices--; - Devices = Reallocate(OBOS_NonPagedPoolAllocator, Devices, nDevices*sizeof(e1000_device), (nDevices+1)*sizeof(e1000_device), nullptr); - OBOS_Warning("%02x:%02x:%02x: Bogus E1000 PCI node.\n", dev->location.bus, dev->location.slot, dev->location.function); - dev = LIST_GET_NEXT(pci_device_list, &bus->devices, dev); - return; - } - - // Taken from managarm - if((Devices[dev_index].hw.mac.type == e1000_ich8lan) || (Devices[dev_index].hw.mac.type == e1000_ich9lan) || - (Devices[dev_index].hw.mac.type == e1000_ich10lan) || (Devices[dev_index].hw.mac.type == e1000_pchlan) || - (Devices[dev_index].hw.mac.type == e1000_pch2lan) || (Devices[dev_index].hw.mac.type == e1000_pch_lpt)) - { - // (not taken from managarm) - if (!bar1 || bar1->bar->type == PCI_BARIO) - { - OBOS_Warning("%02x:%02x:%02x: Cannot map flash: unimplemented.\n", dev->location.bus, dev->location.slot, dev->location.function); - Mm_VirtualMemoryFree(&Mm_KernelContext, (void*)Devices[dev_index].osdep.membase, bar0->bar->size); - nDevices--; - Devices = Reallocate(OBOS_NonPagedPoolAllocator, Devices, nDevices*sizeof(e1000_device), (nDevices+1)*sizeof(e1000_device), nullptr); - OBOS_Warning("%02x:%02x:%02x: Bogus E1000 PCI node.\n", dev->location.bus, dev->location.slot, dev->location.function); - dev = LIST_GET_NEXT(pci_device_list, &bus->devices, dev); - return; - } - - hw2flashbase(&Devices[dev_index].hw) = (uintptr_t)map_registers(bar1->bar->phys, bar1->bar->size, true, true, false); - } else if (Devices[dev_index].hw.mac.type >= e1000_pch_spt) { - /** - * In the new SPT device flash is not a separate BAR, rather it is also in BAR0, - * so use the same tag and an offset handle for the FLASH read/write macros in the shared code. - */ - - hw2flashbase(&Devices[dev_index].hw) = Devices[dev_index].osdep.membase + E1000_FLASH_BASE_ADDR; - } - Devices[dev_index].hw.flash_address = (void*)Devices[dev_index].osdep.flashbase; - - if (e1000_setup_init_funcs(&Devices[dev_index].hw, true) != E1000_SUCCESS) - { - Mm_VirtualMemoryFree(&Mm_KernelContext, (void*)Devices[dev_index].osdep.membase, bar0->bar->size); - nDevices--; - Devices = Reallocate(OBOS_NonPagedPoolAllocator, Devices, nDevices*sizeof(e1000_device), (nDevices+1)*sizeof(e1000_device), nullptr); - OBOS_Warning("%02x:%02x:%02x: Bogus E1000 PCI node.\n", dev->location.bus, dev->location.slot, dev->location.function); - dev = LIST_GET_NEXT(pci_device_list, &bus->devices, dev); - return; - } - - e1000_get_bus_info(&Devices[dev_index].hw); - - Devices[dev_index].hw.mac.autoneg = 1; - Devices[dev_index].hw.phy.autoneg_wait_to_complete = false; - Devices[dev_index].hw.phy.autoneg_advertised = (ADVERTISE_10_HALF | ADVERTISE_10_FULL | ADVERTISE_100_HALF | ADVERTISE_100_FULL | ADVERTISE_1000_FULL); - - if (Devices[dev_index].hw.phy.media_type == e1000_media_type_copper) { - Devices[dev_index].hw.phy.mdix = 0; - Devices[dev_index].hw.phy.disable_polarity_correction = false; - Devices[dev_index].hw.phy.ms_type = e1000_ms_hw_default; - } - - Devices[dev_index].hw.mac.report_tx_early = true; - - if (e1000_reset_hw(&Devices[dev_index].hw) != E1000_SUCCESS) - { - Mm_VirtualMemoryFree(&Mm_KernelContext, (void*)Devices[dev_index].osdep.membase, bar0->bar->size); - nDevices--; - Devices = Reallocate(OBOS_NonPagedPoolAllocator, Devices, nDevices*sizeof(e1000_device), (nDevices+1)*sizeof(e1000_device), nullptr); - OBOS_Warning("%02x:%02x:%02x: Bogus E1000 PCI node.\n", dev->location.bus, dev->location.slot, dev->location.function); - dev = LIST_GET_NEXT(pci_device_list, &bus->devices, dev); - return; - } - e1000_power_up_phy(&Devices[dev_index].hw); - - if (e1000_validate_nvm_checksum(&Devices[dev_index].hw) != E1000_SUCCESS) - { - Mm_VirtualMemoryFree(&Mm_KernelContext, (void*)Devices[dev_index].osdep.membase, bar0->bar->size); - nDevices--; - Devices = Reallocate(OBOS_NonPagedPoolAllocator, Devices, nDevices*sizeof(e1000_device), (nDevices+1)*sizeof(e1000_device), nullptr); - OBOS_Warning("%02x:%02x:%02x: Bogus E1000 PCI node.\n", dev->location.bus, dev->location.slot, dev->location.function); - dev = LIST_GET_NEXT(pci_device_list, &bus->devices, dev); - return; - } - - e1000_disable_ulp_lpt_lp(&Devices[dev_index].hw, true); - - Devices[dev_index].rx_evnt = EVENT_INITIALIZE(EVENT_NOTIFICATION); - Devices[dev_index].tx_done_evnt = EVENT_INITIALIZE(EVENT_NOTIFICATION); - Devices[dev_index].irq_res = irq_res; - - e1000_init_rx(&Devices[dev_index]); - e1000_init_tx(&Devices[dev_index]); - - Core_IrqObjectInitializeIRQL(&Devices[dev_index].irq, IRQL_E1000, true, true); - Devices[dev_index].irq.handler = e1000_irq_handler; - Devices[dev_index].irq.irqChecker = e1000_check_irq_callback; - Devices[dev_index].irq.irqCheckerUserdata = Devices + dev_index; - Devices[dev_index].irq.handlerUserdata = Devices + dev_index; - Devices[dev_index].irq_res->irq->irq = &Devices[dev_index].irq; - Devices[dev_index].irq_res->irq->masked = false; - Drv_PCISetResource(Devices[dev_index].irq_res); - Devices[dev_index].irq.handler = e1000_irq_handler; - Devices[dev_index].irq.irqChecker = e1000_check_irq_callback; - Devices[dev_index].irq.irqCheckerUserdata = Devices + dev_index; - Devices[dev_index].irq.handlerUserdata = Devices + dev_index; - - e1000_clear_hw_cntrs_base_generic(&Devices[dev_index].hw); - E1000_WRITE_REG(&Devices[dev_index].hw, E1000_IMS, IMS_ENABLE_MASK); -} -static void search_bus(pci_bus* bus, bool enumerate) -{ - for (pci_device* dev = LIST_GET_HEAD(pci_device_list, &bus->devices); dev; ) - { - if ((dev->hid.id & 0xffffffff) == (drv_hdr.pciId.id & 0xffffffff)) - { - // Compare Device IDs. - bool found = false; - for (size_t i = 0; i < sizeof(device_ids)/sizeof(device_ids[0]); i++) - { - if (dev->hid.indiv.deviceId == device_ids[i]) - { - found = true; - break; - } - } - if (!found) - { - dev = LIST_GET_NEXT(pci_device_list, &bus->devices, dev); - continue; - } - if (enumerate) - nDevices++; - else - init_e1000(bus, dev, nInitializedDevices++); - } - - dev = LIST_GET_NEXT(pci_device_list, &bus->devices, dev); - } -} - -driver_init_status OBOS_DriverEntry(driver_id* this) -{ - this_driver = this; - for (uint16_t bus = 0; bus < Drv_PCIBusCount; bus++) - search_bus(&Drv_PCIBuses[bus], true); - Devices = ZeroAllocate(OBOS_KernelAllocator, nDevices, sizeof(*Devices), nullptr); - nInitializedDevices = 0; - for (uint16_t bus = 0; bus < Drv_PCIBusCount; bus++) - search_bus(&Drv_PCIBuses[bus], false); - for (size_t i = 0; i < nDevices; i++) - { - Devices[i].interface_name = DrvH_MakePCIDeviceName(Devices[i].osdep.pci->location, ETHERNET_DEVICE_PREFIX); - Devices[i].vn = Drv_AllocateVNode(this, (dev_desc)&Devices[i], 0, nullptr, VNODE_TYPE_CHR); - Devices[i].vn->flags |= VFLAGS_NIC_NO_FCS; - Devices[i].vn->flags |= VFLAGS_NIC_PACKET_INJECT; - Drv_RegisterVNode(Devices[i].vn, Devices[i].interface_name); - } - return (driver_init_status){.status=OBOS_STATUS_SUCCESS,.fatal=false}; -} - -void e1000_pci_clear_mwi(struct e1000_hw *hw) -{ - OBOS_UNUSED(hw); - OBOS_Debug("%s unimplemented\n", __func__); -} -void e1000_pci_set_mwi(struct e1000_hw *hw) -{ - OBOS_UNUSED(hw); - OBOS_Debug("%s unimplemented\n", __func__); -} - -#define PCIY_EXPRESS 0x10 -s32 e1000_read_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value) -{ - pci_capability* cached_cap = hw2pcicap(hw); - if (!cached_cap) - { - for (pci_capability* cap = hw2pci(hw)->first_capability; cap; cap = cap->next_cap) - { - if (cap->id == PCIY_EXPRESS) - { - cached_cap = cap; - break; - } - } - hw2pcicap(hw) = cached_cap; - } - if (!cached_cap) - cached_cap = hw2pcicap(hw) = (void*)0x1; - uint32_t offset = cached_cap == (void*)0x1 ? 0 : cached_cap->offset; - uint64_t res = 0; - *value = DrvS_ReadPCIRegister(hw2pci(hw)->location, offset+reg, 2, &res); - *value = res; - return E1000_SUCCESS; -} - -s32 e1000_write_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value) -{ - pci_capability* cached_cap = hw2pcicap(hw); - if (!cached_cap) - { - for (pci_capability* cap = hw2pci(hw)->first_capability; cap; cap = cap->next_cap) - { - if (cap->id == PCIY_EXPRESS) - { - cached_cap = cap; - break; - } - } - hw2pcicap(hw) = cached_cap; - } - if (!cached_cap) - cached_cap = hw2pcicap(hw) = (void*)0x1; - uint32_t offset = cached_cap == (void*)0x1 ? 0 : cached_cap->offset; - *value = DrvS_WritePCIRegister(hw2pci(hw)->location, offset+reg, 2, *value); - return E1000_SUCCESS; -} - -void e1000_read_pci_cfg(struct e1000_hw *hw, u32 reg, u16 *value) -{ - pci_device* dev = hw2pci(hw); - uint64_t val = 0; - DrvS_ReadPCIRegister(dev->location, reg, 2, &val); - *value = val; -} - -void e1000_write_pci_cfg(struct e1000_hw *hw, u32 reg, u16 *value) -{ - pci_device* dev = hw2pci(hw); - DrvS_WritePCIRegister(dev->location, reg, 2, *value); -} - -void e1000_io_write(struct e1000_hw *hw, u16 reg, u32 data) -{ - pci_bar fake_bar = { - .idx = 0, - .iospace = hw2iobase(hw), - .size = reg+4, - .type = PCI_BARIO, - }; - DrvS_WriteIOSpaceBar(&fake_bar, reg, data, 4); -} \ No newline at end of file diff --git a/src/drivers/generic/freebsd-e1000/managarm-LICENSE b/src/drivers/generic/freebsd-e1000/managarm-LICENSE deleted file mode 100644 index 96c335f2..00000000 --- a/src/drivers/generic/freebsd-e1000/managarm-LICENSE +++ /dev/null @@ -1,9 +0,0 @@ -io.c contains many code copied directly from managarm, but slightly modified, thus this copyright notice - -Copyright 2014-2020 Managarm Contributors - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/src/drivers/generic/initrd/CMakeLists.txt b/src/drivers/generic/initrd/CMakeLists.txt deleted file mode 100644 index 80dd5984..00000000 --- a/src/drivers/generic/initrd/CMakeLists.txt +++ /dev/null @@ -1,32 +0,0 @@ -# drivers/generic/initrd/CMakeLists.txt -# -# Copyright (c) 2024 Omar Berrow - -add_executable(initrd "main.c" "parse.c") - -target_compile_options(initrd - PRIVATE $<$:${TARGET_DRIVER_COMPILE_OPTIONS_C}> - PRIVATE $<$:-ffreestanding> - PRIVATE $<$:-Wall> - PRIVATE $<$:-Wextra> - PRIVATE $<$:-fstack-protector-all> - PRIVATE $<$:-fno-builtin-memset> - PRIVATE $<$:-fvisibility=hidden> - PRIVATE $<$:-fPIC> -) - -set_property(TARGET initrd PROPERTY link_depends ${DRIVER_LINKER_SCRIPT}) - -target_include_directories(initrd - PRIVATE "${CMAKE_SOURCE_DIR}/src/oboskrnl" - PRIVATE ${OBOSKRNL_EXTERNAL_INCLUDES}) - -target_link_options(initrd - PRIVATE "-nostdlib" - PRIVATE "-fPIC" - PRIVATE "-T${DRIVER_LINKER_SCRIPT}" - PRIVATE "-Wl,-shared" -# PRIVATE "-Wl,--allow-shlib-undefined" - PRIVATE ${TARGET_DRIVER_LINKER_OPTIONS} -) -target_compile_definitions(initrd PRIVATE OBOS_DRIVER=1) diff --git a/src/drivers/generic/initrd/main.c b/src/drivers/generic/initrd/main.c deleted file mode 100644 index 267f3547..00000000 --- a/src/drivers/generic/initrd/main.c +++ /dev/null @@ -1,707 +0,0 @@ -/* - * drivers/generic/initrd/main.c - * - * Copyright (c) 2024-2026 Omar Berrow -*/ - -#include -#include -#include -#include -#include - -#include - -#include -#include -#include - -#include - -#include -#include - -#include - -#include - -#include "name.h" -#include "ustar_hdr.h" -#include "parse.h" - -OBOS_PAGEABLE_FUNCTION obos_status get_blk_size(dev_desc desc, size_t* blkSize) -{ - OBOS_UNUSED(desc); - if (!blkSize) - return OBOS_STATUS_INVALID_ARGUMENT; - *blkSize = 1; - return OBOS_STATUS_SUCCESS; -} -OBOS_WEAK obos_status get_max_blk_count(dev_desc desc, size_t* count); -OBOS_WEAK obos_status read_sync(dev_desc desc, void* buf, size_t blkCount, size_t blkOffset, size_t* nBlkRead); -OBOS_WEAK obos_status write_sync(dev_desc desc, const void* buf, size_t blkCount, size_t blkOffset, size_t* nBlkWritten); -OBOS_WEAK obos_status foreach_device(iterate_decision(*cb)(dev_desc desc, size_t blkSize, size_t blkCount, void* u), void* u); -OBOS_WEAK obos_status query_user_readable_name(dev_desc what, const char** name); // unrequired for fs drivers. -OBOS_PAGEABLE_FUNCTION obos_status ioctl(dev_desc what, uint32_t request, void* argp) -{ - OBOS_UNUSED(what); - OBOS_UNUSED(request); - OBOS_UNUSED(argp); - return OBOS_STATUS_INVALID_IOCTL; -} - -void driver_cleanup_callback() -{} - -OBOS_WEAK obos_status symlink_set_path(dev_desc desc, const char* to); -OBOS_WEAK obos_status query_path(dev_desc desc, const char** path); -OBOS_WEAK obos_status path_search(dev_desc* found, void*, const char* what, dev_desc parent); -OBOS_WEAK obos_status get_linked_path(dev_desc desc, const char** found); -OBOS_WEAK obos_status move_desc_to(dev_desc desc, dev_desc new_parent, const char* name); -OBOS_WEAK obos_status mk_file(dev_desc* newDesc, dev_desc parent, void* vn, const char* name, file_type type, driver_file_perm perm); -OBOS_WEAK obos_status remove_file(dev_desc desc); -OBOS_WEAK obos_status set_file_perms(dev_desc desc, driver_file_perm newperm); -OBOS_WEAK obos_status get_file_perms(dev_desc desc, driver_file_perm *perm); -OBOS_WEAK obos_status set_file_owner(dev_desc desc, uid owner_uid, gid group_uid); -OBOS_WEAK obos_status get_file_type(dev_desc desc, file_type *type); -OBOS_WEAK obos_status get_file_times(dev_desc desc, void *times); -OBOS_WEAK obos_status list_dir(dev_desc dir, void* unused, iterate_decision(*cb)(dev_desc desc, size_t blkSize, size_t blkCount, void* userdata, const char* name), void* userdata); -OBOS_WEAK obos_status stat_fs_info(void *vn, drv_fs_info *info); - -obos_status irp_process_dryop(irp* req) -{ - if (req->op == IRP_WRITE) return OBOS_STATUS_READ_ONLY; - - dev_desc desc = req->desc; - size_t blkCount = req->blkCount; - size_t blkOffset = req->blkOffset; - initrd_inode* ino = (void*)desc; - if (!ino) - return OBOS_STATUS_INVALID_ARGUMENT; - if (!blkCount) - return OBOS_STATUS_SUCCESS; - if (ino->type != FILE_TYPE_REGULAR_FILE) - return OBOS_STATUS_NOT_A_FILE; - size_t filesize = ino->filesize; - if (blkOffset >= filesize) - { - req->nBlkRead = 0; - return OBOS_STATUS_SUCCESS; - } - return OBOS_STATUS_SUCCESS; -} - -obos_status vnode_search(void** vn_found, dev_desc desc, void* dev_vn) -{ - OBOS_UNUSED(dev_vn); - initrd_inode* ino = (void*)desc; - if (!ino) - return OBOS_STATUS_INVALID_ARGUMENT; - if (ino->dead) - return OBOS_STATUS_INVALID_ARGUMENT; // confusing... - if (!ino->vnode) - { - ino->vnode = Vfs_Calloc(1, sizeof(vnode)); - ino->vnode->desc = (uintptr_t)ino; - ino->vnode->filesize = ino->filesize; - ino->vnode->blkSize = 1; - ino->vnode->uid = 0; - ino->vnode->gid = 0; - ino->vnode->inode = ino->ino; - ino->vnode->perm = ino->perm; - if (ino->hdr) - ino->vnode->times.change = oct2bin(ino->hdr->last_mod, strnlen(ino->hdr->last_mod, 12)); - ino->vnode->times.birth = ino->vnode->times.change; - ino->vnode->times.access = ino->vnode->times.change; - switch (ino->type) { - case FILE_TYPE_REGULAR_FILE: - ino->vnode->vtype = VNODE_TYPE_REG; - break; - case FILE_TYPE_DIRECTORY: - ino->vnode->vtype = VNODE_TYPE_DIR; - break; - case FILE_TYPE_SYMBOLIC_LINK: - ino->vnode->vtype = VNODE_TYPE_LNK; - ino->vnode->un.linked = ino->linked_path; - break; - default: - OBOS_UNREACHABLE; - } - } - *vn_found = ino->vnode; - return OBOS_STATUS_SUCCESS; -} - -OBOS_WEAK obos_status submit_irp(void* /* irp* */ request_) -{ - if (!request_) - return OBOS_STATUS_INVALID_ARGUMENT; - irp* request = request_; - if (request->dryOp) - request->status = irp_process_dryop(request); - else - request->status = request->op == IRP_READ ? - read_sync(request->desc, request->buff, request->blkCount, request->blkOffset, &request->nBlkRead) : - OBOS_STATUS_READ_ONLY; - request->evnt = nullptr; - return OBOS_STATUS_SUCCESS; -} -obos_status get_file_inode(dev_desc desc, uint32_t *out) -{ - initrd_inode* ino = (void*)desc; - if (!ino) - return OBOS_STATUS_INVALID_ARGUMENT; - *out = ino->ino; - return OBOS_STATUS_SUCCESS; -} -OBOS_WEAK obos_status trunc_file(dev_desc desc, size_t newsize /* note, newsize must be less than the filesize */); -OBOS_WEAK obos_status finalize_irp(void* /* irp* */ request_); - -__attribute__((section(OBOS_DRIVER_HEADER_SECTION))) driver_header drv_hdr = { - .magic = OBOS_DRIVER_MAGIC, - .flags = DRIVER_HEADER_HAS_STANDARD_INTERFACES, - .ftable = { - .driver_cleanup_callback = driver_cleanup_callback, - .ioctl = ioctl, - .get_blk_size = get_blk_size, - .get_max_blk_count = get_max_blk_count, - .query_user_readable_name = query_user_readable_name, - .foreach_device = foreach_device, - .read_sync = read_sync, - .write_sync = write_sync, - .submit_irp = submit_irp, - .finalize_irp = finalize_irp, - - .query_path = query_path, - .path_search = path_search, - .get_linked_path = get_linked_path, - .vnode_search = vnode_search, - .move_desc_to = move_desc_to, - .mk_file = mk_file, - .remove_file = remove_file, - .get_file_perms = get_file_perms, - .set_file_perms = set_file_perms, - .set_file_owner = set_file_owner, - .get_file_type = get_file_type, - .get_file_inode = get_file_inode, - .get_file_times = get_file_times, - .list_dir = list_dir, - .stat_fs_info = stat_fs_info, - .symlink_set_path = symlink_set_path, - .trunc_file = trunc_file, - }, - .driverName = INITRD_DRIVER_NAME, - .version = 1, -}; - -initrd_inode* InitrdRoot; -size_t CurrentInodeNumber = 1; - -static int64_t strrfind(const char* str, char ch) -{ - int64_t i = strlen(str); - for (; i >= 0; i--) - if (str[i] == ch) - return i; - return -1; -} - -initrd_inode* create_inode_boot(const ustar_hdr* hdr) -{ - initrd_inode* ino = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(initrd_inode), nullptr); - - bool path_needs_slash = hdr->filename[0] != '/' && (*hdr->prefix && hdr->prefix[strnlen(hdr->prefix, 155)-1] != '/'); - ino->path_len = strnlen(hdr->filename, 100) + strnlen(hdr->prefix, 155) + path_needs_slash; - ino->path_size = ino->path_len + 1; - ino->path = Allocate(OBOS_KernelAllocator, ino->path_len+1, nullptr); - - memcpy(ino->path, hdr->prefix, strnlen(hdr->prefix, 155)); - if (path_needs_slash) - ino->path[strnlen(hdr->prefix, 155)] = '/'; - memcpy(ino->path + strnlen(hdr->prefix, 155) + path_needs_slash, hdr->filename, strnlen(hdr->filename, 100)); - - ino->path[ino->path_len] = 0; - ino->persistent = true; - // Get our parent. - // Also, seperate the path from the basename. - while (ino->path[ino->path_len - 1] == '/') - ino->path[--ino->path_len] = 0; - int64_t index = strrfind(ino->path, '/')+1; - ino->name_len = ino->path_len - index+1; - ino->name_size = ino->name_len + 1; - ino->name = Allocate(OBOS_KernelAllocator, ino->name_len+1, nullptr); - memcpy(ino->name, ino->path+index, ino->name_len); - ino->name[ino->name_len] = 0; - // ino->data = Allocate(OBOS_NonPagedPoolAllocator, ino->filesize, nullptr); - // if (!ino->data) - // { - // OBOS_Error("initrd: OOM while allocating node!\n"); - // Free(OBOS_KernelAllocator, ino->name, ino->name_size); - // Free(OBOS_KernelAllocator, ino->path, ino->path_size); - // Free(OBOS_KernelAllocator, ino, sizeof(*ino)); - // return nullptr; - // } - // memcpy(ino->data, (char*)hdr + 512, ino->filesize); - back: - switch (hdr->type) { - case AREGTYPE: - case REGTYPE: - ino->type = FILE_TYPE_REGULAR_FILE; - break; - case DIRTYPE: - ino->type = FILE_TYPE_DIRECTORY; - break; - case SYMTYPE: - ino->type = FILE_TYPE_SYMBOLIC_LINK; - break; - case LNKTYPE: - { - obos_status status = OBOS_STATUS_SUCCESS; - hdr = GetFile(hdr->linked, &status); - if (obos_is_error(status)) - { - Free(OBOS_KernelAllocator, ino->name, ino->name_size); - Free(OBOS_KernelAllocator, ino->path, ino->path_size); - Free(OBOS_KernelAllocator, ino, sizeof(*ino)); - return nullptr; - } - goto back; - break; - } - default: - OBOS_ENSURE(!"Unrecognized header type"); - } - ino->filesize = oct2bin(hdr->filesize, strnlen(hdr->filesize, 12)); - ino->data = (char*)hdr + 512; - - uint16_t filemode = oct2bin(hdr->filemode, strnlen(hdr->filemode, 8)); - ino->perm.group_read = filemode & FILEMODE_GROUP_READ; - ino->perm.owner_read = filemode & FILEMODE_OWNER_READ; - ino->perm.other_read = filemode & FILEMODE_OTHER_READ; - ino->perm.group_write = filemode & FILEMODE_GROUP_WRITE; - ino->perm.owner_write = filemode & FILEMODE_OWNER_WRITE; - ino->perm.other_write = filemode & FILEMODE_OTHER_WRITE; - ino->perm.group_exec = filemode & FILEMODE_GROUP_EXEC; - ino->perm.owner_exec = filemode & FILEMODE_OWNER_EXEC; - ino->perm.other_exec = filemode & FILEMODE_OTHER_EXEC; - ino->perm.set_uid = filemode & 04000; - ino->perm.set_gid = filemode & 02000; - ino->linked_path = hdr->linked; - - ino->vnode = Vfs_Calloc(1, sizeof(vnode)); - ino->vnode->desc = (uintptr_t)ino; - ino->vnode->filesize = ino->filesize; - ino->vnode->blkSize = 1; - ino->vnode->uid = 0; - ino->vnode->gid = 0; - ino->vnode->inode = ino->ino; - ino->vnode->perm = ino->perm; - ino->vnode->times.change = oct2bin(hdr->last_mod, strnlen(hdr->last_mod, 12)); - ino->vnode->times.birth = ino->vnode->times.change; - ino->vnode->times.access = ino->vnode->times.change; - switch (ino->type) { - case FILE_TYPE_REGULAR_FILE: - ino->vnode->vtype = VNODE_TYPE_REG; - break; - case FILE_TYPE_DIRECTORY: - ino->vnode->vtype = VNODE_TYPE_DIR; - break; - case FILE_TYPE_SYMBOLIC_LINK: - ino->vnode->vtype = VNODE_TYPE_LNK; - ino->vnode->un.linked = ino->linked_path; - break; - default: - OBOS_UNREACHABLE; - } - - ino->hdr = hdr; - ino->ino = CurrentInodeNumber++; - - return ino; -} - -driver_init_status OBOS_DriverEntry(driver_id* this) -{ - (void)this; - - // for (volatile bool b = true; b; ) - // ; - - // Parse the initrd, and make the initrd_inode tree. - - InitrdRoot = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(initrd_inode), nullptr); - InitrdRoot->filesize = 0; - InitrdRoot->name = ""; - InitrdRoot->path = ""; - InitrdRoot->type = FILE_TYPE_DIRECTORY; - InitrdRoot->persistent = false; - - InitrdRoot->perm.owner_exec = true; - InitrdRoot->perm.group_exec = true; - InitrdRoot->perm.other_exec = false; - - InitrdRoot->perm.owner_read = true; - InitrdRoot->perm.group_read = true; - InitrdRoot->perm.other_read = false; - - InitrdRoot->perm.owner_write = true; - InitrdRoot->perm.group_write = true; - InitrdRoot->perm.other_write = false; - - InitrdRoot->ino = CurrentInodeNumber++; - - const ustar_hdr* hdr = (ustar_hdr*)OBOS_InitrdBinary; - if (OBOS_InitrdSize < sizeof(ustar_hdr)) - return (driver_init_status){.status=OBOS_STATUS_SUCCESS,.fatal=false}; - - while (memcmp(hdr->magic, USTAR_MAGIC, 6)) - { - size_t hdr_filename_len = strnlen(hdr->filename, 100); - size_t hdr_prefix_len = strnlen(hdr->prefix, 155); - size_t filesize = oct2bin(hdr->filesize, strnlen(hdr->filesize, 12)); - if (strchr(hdr->filename, '/') != hdr_filename_len || strchr(hdr->prefix, '/') != hdr_prefix_len) - goto down; - - initrd_inode* ino = create_inode_boot(hdr); - if (!ino) - return (driver_init_status){.status=OBOS_STATUS_INTERNAL_ERROR,.fatal=false}; - ino->parent = InitrdRoot; - if (!ino->parent->children.head) - ino->parent->children.head = ino; - if (ino->parent->children.tail) - ino->parent->children.tail->next = ino; - ino->prev = ino->parent->children.tail; - ino->parent->children.tail = ino; - ino->parent->children.nChildren++; - - down: - (void)0; - size_t filesize_rounded = (filesize + 0x1ff) & ~0x1ff; - hdr = (ustar_hdr*)(((uintptr_t)hdr) + filesize_rounded + 512); - } - - return (driver_init_status){.status=OBOS_STATUS_SUCCESS,.fatal=false}; -} - -// dev_desc is simply a pointer to a ustar_hdr - -OBOS_PAGEABLE_FUNCTION obos_status get_max_blk_count(dev_desc desc, size_t* count) -{ - initrd_inode* inode = (void*)desc; - if (!inode || !count) - return OBOS_STATUS_INVALID_ARGUMENT; - if (desc == UINTPTR_MAX) - return OBOS_STATUS_NOT_A_FILE; - if (inode->type != FILE_TYPE_REGULAR_FILE) - return OBOS_STATUS_NOT_A_FILE; - *count = inode->filesize; - return OBOS_STATUS_SUCCESS; -} -OBOS_PAGEABLE_FUNCTION obos_status read_sync(dev_desc desc, void* buf, size_t blkCount, size_t blkOffset, size_t* nBlkRead) -{ - initrd_inode* inode = (void*)desc; - if (!inode || !buf) - return OBOS_STATUS_INVALID_ARGUMENT; - if (!blkCount) - return OBOS_STATUS_SUCCESS; - if (inode->type != FILE_TYPE_REGULAR_FILE) - return OBOS_STATUS_NOT_A_FILE; - if (blkOffset >= inode->filesize) - { - if (nBlkRead) *nBlkRead = 0; - return OBOS_STATUS_SUCCESS; - } - size_t nToRead = blkCount; - if ((blkOffset + blkCount) >= inode->filesize) - nToRead = inode->filesize - blkOffset; - const char* iter = inode->data+blkOffset; - memcpy(buf, iter, nToRead); - return OBOS_STATUS_SUCCESS; -} - -OBOS_PAGEABLE_FUNCTION obos_status query_path(dev_desc desc, const char** path) -{ - initrd_inode* inode = (void*)desc; - if (!inode || !path) - return OBOS_STATUS_INVALID_ARGUMENT; - *path = inode->path; - return OBOS_STATUS_SUCCESS; -} - -OBOS_PAGEABLE_FUNCTION obos_status get_linked_path(dev_desc desc, const char** found) -{ - initrd_inode* ino = (void*)desc; - if (ino->type != FILE_TYPE_SYMBOLIC_LINK) - return OBOS_STATUS_INVALID_ARGUMENT; - *found = ino->linked_path; - return OBOS_STATUS_SUCCESS; -} - -static char* fullpath(dev_desc parent, const char* what) -{ - char *ret = nullptr; - if (parent != UINTPTR_MAX) - { - size_t ppath_len = strlen(((initrd_inode*)parent)->path); - const char* format = (((initrd_inode*)parent)->path)[ppath_len-1] == '/' ? - "%s%s" : "%s/%s"; - size_t len = snprintf(nullptr, 0, format, ((initrd_inode*)parent)->path, what); - ret = Allocate(OBOS_KernelAllocator, len+1, nullptr); - snprintf(ret, len+1, format, ((initrd_inode*)parent)->path, what); - } - else - { - size_t len = strlen(what); - ret = memcpy(Allocate(OBOS_KernelAllocator, len+1, nullptr), what, len+1); - } - OBOS_ENSURE(ret); - return ret; -} - -initrd_inode* create_inode_with_parents(const char* path, const ustar_hdr* hdr) -{ - if (!hdr) - hdr = GetFile(path, nullptr); - if (!hdr) - return nullptr; - - initrd_inode* ino = create_inode_boot(hdr); - if (!ino) - return nullptr; - // Get our parent. - int64_t index = strrfind(ino->path, '/'); - if (index == -1) - index = 0; - char presv = ino->path[index]; - ino->path[index] = 0; - ino->parent = DirentLookupFrom(ino->path /* really the 'dirname' */, InitrdRoot); - ino->path[index] = presv; - - if (!ino->parent) - { - ino->parent = InitrdRoot; - // Create all parent directories - char* iter = ino->path; - char* end = ino->path + ino->path_len; - while (iter < end) - { - size_t off = strchr(iter, '/'); - presv = iter[off]; - iter[off] = 0; - initrd_inode* found = DirentLookupFrom(ino->path, InitrdRoot); - if (found) - { - if (found->dead) - found->dead = false; - ino->parent = found; - goto down; - } - - const ustar_hdr *sub_hdr = GetFile(ino->path, nullptr); - if (!sub_hdr && ((iter + off) >= end)) - goto down; // NOTE: buggy? - if (sub_hdr == hdr) - goto down; - - initrd_inode* sub_ino = create_inode_boot(sub_hdr); - if (!sub_ino) - { - Free(OBOS_KernelAllocator, ino->name, ino->name_size); - Free(OBOS_KernelAllocator, ino->path, ino->path_size); - Free(OBOS_KernelAllocator, ino, sizeof(*ino)); - return nullptr; - } - sub_ino->parent = ino->parent; - // printf("%s %s\n", iter, sub_ino->parent->name); - if (!sub_ino->parent->children.head) - sub_ino->parent->children.head = sub_ino; - if (sub_ino->parent->children.tail) - sub_ino->parent->children.tail->next = sub_ino; - sub_ino->prev = sub_ino->parent->children.tail; - sub_ino->parent->children.tail = sub_ino; - sub_ino->parent->children.nChildren++; - - ino->parent = sub_ino; - - down: - iter[off] = presv; - iter += off; - } - } - if (!ino->parent) - ino->parent = InitrdRoot; - if (!ino->parent->children.head) - ino->parent->children.head = ino; - if (ino->parent->children.tail) - ino->parent->children.tail->next = ino; - ino->prev = ino->parent->children.tail; - ino->parent->children.tail = ino; - ino->parent->children.nChildren++; - - return ino; -} - -OBOS_PAGEABLE_FUNCTION obos_status path_search(dev_desc* found, void* unused, const char* what, dev_desc parent) -{ - OBOS_UNUSED(unused); - if (!found || !what) - return OBOS_STATUS_INVALID_ARGUMENT; - initrd_inode* ino = DirentLookupFrom(what, parent == UINTPTR_MAX ? InitrdRoot : (initrd_inode*)parent); - if (ino) - { - if (ino->dead) - { - *found = 0; - return OBOS_STATUS_NOT_FOUND; - } - OBOS_ENSURE(ino->parent == (parent == UINTPTR_MAX ? InitrdRoot : (initrd_inode*)parent)); - *found = (dev_desc)ino; - return OBOS_STATUS_SUCCESS; - } - char* path = fullpath(parent, what); - ino = create_inode_with_parents(path, nullptr); - Free(OBOS_KernelAllocator, path, strlen(path)+1); - - *found = (dev_desc)ino; - return ino ? OBOS_STATUS_SUCCESS : OBOS_STATUS_NOT_FOUND; -} - -OBOS_PAGEABLE_FUNCTION obos_status get_file_perms(dev_desc desc, driver_file_perm *perm) -{ - initrd_inode* inode = (void*)desc; - if (!inode || !perm) - return OBOS_STATUS_INVALID_ARGUMENT; - *perm = inode->perm; - return OBOS_STATUS_SUCCESS; -} - -OBOS_PAGEABLE_FUNCTION obos_status get_file_type(dev_desc desc, file_type *type) -{ - if (!type || !desc) - return OBOS_STATUS_INVALID_ARGUMENT; - initrd_inode* inode = (void*)desc; - *type = inode->type; - return OBOS_STATUS_SUCCESS; -} - -OBOS_PAGEABLE_FUNCTION obos_status list_dir(dev_desc dir_, void* unused, iterate_decision(*cb)(dev_desc desc, size_t blkSize, size_t blkCount, void* userdata, const char* name), void* userdata) -{ - OBOS_UNUSED(unused); - if (!dir_) - return OBOS_STATUS_INVALID_ARGUMENT; - initrd_inode* dir = dir_ == UINTPTR_MAX ? InitrdRoot : (void*)dir_; - const ustar_hdr* hdr = (ustar_hdr*)OBOS_InitrdBinary; - // FIXME: Non persistent entries won't appear here. - if (dir->persistent) - { - while (memcmp(hdr->magic, USTAR_MAGIC, 6)) - { - size_t filesize = oct2bin(hdr->filesize, strnlen(hdr->filesize, 12)); - if (hdr == dir->hdr) - goto down; - char* hdr_path = nullptr; - size_t hdr_path_len = strnlen(hdr->filename, 100) + strnlen(hdr->prefix, 155); - do { - size_t len_hdr_prefix = strnlen(hdr->prefix, 155); - bool prefix_has_slash = true; - if (*hdr->prefix && hdr->prefix[len_hdr_prefix-1] != '/' && hdr->filename[0] != '/') - { - hdr_path_len++; - prefix_has_slash = false; - } - hdr_path = Allocate(OBOS_KernelAllocator, hdr_path_len+1, nullptr); - snprintf(hdr_path, hdr_path_len+1, prefix_has_slash ? "%s%s" : "%s/%s", hdr->prefix, hdr->filename); - } while(0); - - if (!dir->path_len || (strncmp(dir->path, hdr_path, dir->path_len) && dir->path_len != hdr_path_len)) - { - int addend = 0; - if (dir->path_len) - addend = dir->path[dir->path_len-1] != '/'; - if (strchr(hdr_path+dir->path_len+addend, '/') == hdr_path_len-(dir->path_len+addend)) - { - initrd_inode *ino = DirentLookupFrom(hdr_path, dir); - if (!ino) - ino = create_inode_with_parents(hdr_path, hdr); - if (!ino || ino->dead) - goto down; - if (cb((dev_desc)ino, 1, ino->filesize, userdata, ino->name) == ITERATE_DECISION_STOP) - { - Free(OBOS_KernelAllocator, hdr_path, hdr_path_len+1); - hdr_path = nullptr; - break; - } - } - } - Free(OBOS_KernelAllocator, hdr_path, hdr_path_len+1); - hdr_path = nullptr; - - down: - (void)0; - size_t filesize_rounded = (filesize + 0x1ff) & ~0x1ff; - hdr = (ustar_hdr*)(((uintptr_t)hdr) + filesize_rounded + 512); - } - } - else - { - for (initrd_inode* ino = dir->children.head; ino; ) - { - if (ino->dead) - goto next; - if (cb((dev_desc)ino, 1, ino->filesize, userdata, ino->name) == ITERATE_DECISION_STOP) - break; - next: - ino = ino->next; - } - } - return OBOS_STATUS_SUCCESS; -} - -static iterate_decision cb(dev_desc desc, size_t blkSize, size_t blkCount, void* userdata, const char* name) -{ - OBOS_UNUSED(blkSize); - OBOS_UNUSED(blkCount); - OBOS_UNUSED(name); - - size_t* const fileCount = userdata; - (*fileCount)++; - - initrd_inode* ino = (void*)desc; - if (ino->type == FILE_TYPE_DIRECTORY) - list_dir(desc, nullptr, cb, userdata); - return ITERATE_DECISION_CONTINUE; -} - -obos_status stat_fs_info(void *vn, drv_fs_info *info) -{ - OBOS_UNUSED(vn); - static size_t fileCount = SIZE_MAX; - if (fileCount == SIZE_MAX) - { - fileCount = 0; - list_dir(UINTPTR_MAX, vn, cb, &fileCount); - } - info->partBlockSize = 1; - info->fsBlockSize = 1; - info->availableFiles = SIZE_MAX; - info->fileCount = fileCount; - info->szFs = (Mm_TotalPhysicalPages - Mm_TotalPhysicalPagesUsed) * OBOS_PAGE_SIZE; - info->freeBlocks = info->szFs; - info->flags = FS_FLAGS_RDONLY; - // TODO: Is there a proper value for this? - info->nameMax = 100; - return OBOS_STATUS_SUCCESS; -} - -obos_status get_file_times(dev_desc desc, void *ptimes) -{ - struct file_times* times = ptimes; - initrd_inode* ino = (void*)desc; - if (!ino) - return OBOS_STATUS_INVALID_ARGUMENT; - if (ino->hdr) - times->change = oct2bin(ino->hdr->last_mod, strnlen(ino->hdr->last_mod, 12)); - times->birth = ino->vnode->times.change; - times->access = ino->vnode->times.change; - return OBOS_STATUS_SUCCESS; -} diff --git a/src/drivers/generic/initrd/name.h b/src/drivers/generic/initrd/name.h deleted file mode 100644 index b16586fa..00000000 --- a/src/drivers/generic/initrd/name.h +++ /dev/null @@ -1,2 +0,0 @@ -#pragma once -#define INITRD_DRIVER_NAME "Initial Ramdisk (InitRD) Driver" \ No newline at end of file diff --git a/src/drivers/generic/initrd/parse.c b/src/drivers/generic/initrd/parse.c deleted file mode 100644 index 91786729..00000000 --- a/src/drivers/generic/initrd/parse.c +++ /dev/null @@ -1,177 +0,0 @@ -/* - * drivers/generic/initrd/parse.c - * - * Copyright (c) 2024-2025 Omar Berrow -*/ - -#include -#include -#include -#include -#include - -#include - -#include "parse.h" -#include "ustar_hdr.h" - -#include - -#define set_status(to) status ? (*status = to) : (void)0 -const ustar_hdr* GetFile(const char* path, obos_status* status) -{ - if (!OBOS_InitrdBinary || (OBOS_InitrdSize < sizeof(ustar_hdr))) - { - set_status(OBOS_STATUS_NOT_FOUND); - return nullptr; - } - if (strlen(path) > 255) - { - set_status(OBOS_STATUS_INVALID_ARGUMENT); - return nullptr; - } - set_status(OBOS_STATUS_SUCCESS); - const ustar_hdr* hdr = (ustar_hdr*)OBOS_InitrdBinary; - size_t pathlen = strnlen(path, 255); - char path_slash[256]; - memcpy(path_slash, path, pathlen); - if (path_slash[pathlen-1] != '/') - { - path_slash[pathlen] = '/'; - path_slash[++pathlen] = '\0'; - } - while (memcmp(hdr->magic, USTAR_MAGIC, 6)) - { - if (!(*hdr->prefix)) - { - if (pathlen >= 100) - OBOSS_SpinlockHint(); - if (strncmp(path_slash, hdr->filename, pathlen) || - strncmp(path, hdr->filename, pathlen) - ) - return hdr; - } - else - { - char hdr_filename[256] = {}; - size_t prefix_len = strnlen(hdr->prefix, 155); - size_t filename_len = strnlen(hdr->filename, 100); - memcpy(hdr_filename, hdr->prefix, prefix_len); - if (hdr->prefix[prefix_len-1] != '/') - hdr_filename[prefix_len] = '/'; - memcpy(hdr_filename + prefix_len + (hdr->prefix[prefix_len-1] != '/'), hdr->filename, filename_len); - if (strcmp(hdr_filename, path)) - return hdr; - } - size_t filesize = oct2bin(hdr->filesize, strnlen(hdr->filesize, 12)); - size_t filesize_rounded = (filesize + 0x1ff) & ~0x1ff; - hdr = (ustar_hdr*)(((uintptr_t)hdr) + filesize_rounded + 512); - } - return nullptr; -} - - -// Adapated from vfs/dirent.c - -static size_t str_search(const char* str, char ch) -{ - size_t ret = strchr(str, ch); - for (; str[ret] == ch && str[ret] != 0; ret++) - ; - return ret; -} -static initrd_inode* on_match(initrd_inode** const curr_, initrd_inode** const root, const char** const tok, size_t* const tok_len, const char** const path, - size_t* const path_len) -{ - initrd_inode *curr = *curr_; - *root = curr; - const char *newtok = (*tok) + str_search(*tok, '/'); - if (newtok >= (*path + *path_len)) - return curr; - if (!curr->children.nChildren) - return nullptr; // could not find node. - *tok = newtok; - size_t currentPathLen = strlen(*tok)-1; - if ((*tok)[currentPathLen] != '/') - currentPathLen++; - while ((*tok)[currentPathLen] == '/') - currentPathLen--; - *tok_len = strchr(*tok, '/'); - if (*tok_len != currentPathLen) - (*tok_len)--; - while ((*tok)[(*tok_len) - 1] == '/') - (*tok_len)--; - return nullptr; -} -initrd_inode* DirentLookupFrom(const char* path, initrd_inode* root) -{ - if (!path) - return nullptr; - initrd_inode* const root_par = root; - size_t path_len = strlen(path); - if (!path_len) - return root; - for (; *path == '/'; path++, path_len--) - ; - const char* tok = path; - size_t tok_len = strchr(tok, '/'); - if (tok_len != path_len) - tok_len--; - while (tok[tok_len - 1] == '/') - tok_len--; - if (!tok_len) - return nullptr; - while(root) - { - initrd_inode* curr = root; - - if (root != root_par) - { - string name = {}; - name.cap = 33; - name.ls = root->name; - name.len = strlen(root->name); - if (OBOS_CompareStringNC(&name, tok, tok_len)) - { - // Match! - initrd_inode* what = - on_match(&curr, &root, &tok, &tok_len, &path, &path_len); - if (what) - return what; - root = curr->children.head; - continue; - } - } - // down: - - for (curr = root->children.head; curr;) - { - string name = {}; - name.cap = 33; - name.ls = curr->name; - name.len = strlen(curr->name); - if (OBOS_CompareStringNC(&name, tok, tok_len)) - { - // Match! - initrd_inode* what = - on_match(&curr, &root, &tok, &tok_len, &path, &path_len); - if (what) - return what; - else if ((tok+tok_len) >= (path+path_len) && !curr->next) - return nullptr; - root = curr->children.head; - break; - } - - // root = curr->children.head ? curr->children.head : root; - curr = curr->next; - } - if (!curr) - { - if ((tok+tok_len) >= (path+path_len)) - break; - root = root->parent; - } - } - return nullptr; -} \ No newline at end of file diff --git a/src/drivers/generic/initrd/parse.h b/src/drivers/generic/initrd/parse.h deleted file mode 100644 index 4796183a..00000000 --- a/src/drivers/generic/initrd/parse.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * drivers/generic/initrd/parse.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include "ustar_hdr.h" - -extern initrd_inode* InitrdRoot; -extern size_t CurrentInodeNumber; - -const ustar_hdr* GetFile(const char* path, obos_status* status); -initrd_inode* DirentLookupFrom(const char* path, initrd_inode* root); -inline static uint64_t oct2bin(const char* str, size_t size) -{ - uint64_t n = 0; - const char* c = str; - while (size-- > 0) - n = n * 8 + (uint64_t)(*c++ - '0'); - return n; -} -void* malloc(size_t sz); -void* realloc(void* buf, size_t sz); -void free(void* buf); diff --git a/src/drivers/generic/initrd/ustar_hdr.h b/src/drivers/generic/initrd/ustar_hdr.h deleted file mode 100644 index af317aee..00000000 --- a/src/drivers/generic/initrd/ustar_hdr.h +++ /dev/null @@ -1,85 +0,0 @@ -/* - * drivers/generic/initrd/ustar_hdr.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include - -#include - -typedef struct ustar_hdr -{ - char filename[100]; - char filemode[8]; - char owner_uid[8]; - char group_uid[8]; - char filesize[12]; // In octal! - char last_mod[12]; // In octal! - char chksum[8]; - char type; - char linked[100]; - char magic[6]; // should be ustar\0 - char version[2]; - char owner_uname[32]; - char group_uname[32]; - char unused[16]; - char prefix[155]; -} OBOS_PACK ustar_hdr; - -typedef struct initrd_inode { - struct { - struct initrd_inode *head, *tail; - size_t nChildren; - } children; - struct initrd_inode *next, *prev, *parent; - char* name; - char* path; - char* data; - const char* linked_path; - const ustar_hdr* hdr; - size_t filesize; - size_t path_len; - size_t path_size; - size_t name_len; - size_t name_size; - file_type type; - uint32_t ino; - bool persistent : 1; - bool dead : 1; - driver_file_perm perm; - vnode* vnode; -} initrd_inode; - -enum { - FILEMODE_EXEC = BIT(0), - FILEMODE_WRITE = BIT(1), - FILEMODE_READ = BIT(2), - FILEMODE_OTHER_EXEC = FILEMODE_EXEC << 0, - FILEMODE_OTHER_WRITE = FILEMODE_WRITE << 0, - FILEMODE_OTHER_READ = FILEMODE_READ << 0, - FILEMODE_GROUP_EXEC = FILEMODE_EXEC << 3, - FILEMODE_GROUP_WRITE = FILEMODE_WRITE << 3, - FILEMODE_GROUP_READ = FILEMODE_READ << 3, - FILEMODE_OWNER_EXEC = FILEMODE_EXEC << 6, - FILEMODE_OWNER_WRITE = FILEMODE_WRITE << 6, - FILEMODE_OWNER_READ = FILEMODE_READ << 6, -}; -enum -{ - AREGTYPE = '\0', - REGTYPE = '0', - LNKTYPE = '1', - SYMTYPE = '2', - CHRTYPE = '3', - BLKTYPE = '4', - DIRTYPE = '5', - FIFOTYPE = '6', - CONTYPE = '7', -}; -#define USTAR_MAGIC "ustar\0" diff --git a/src/drivers/generic/libps2/CMakeLists.txt b/src/drivers/generic/libps2/CMakeLists.txt deleted file mode 100644 index 5929db4b..00000000 --- a/src/drivers/generic/libps2/CMakeLists.txt +++ /dev/null @@ -1,45 +0,0 @@ -# drivers/generic/libps2/CMakeLists.txt - -# Copyright (c) 2025 Omar Berrow - -# The motive behind having this instead of directly intergrating PS/2 drivers -# into a file is to allow PS/2 keyboard/mouse drivers to be shared between -# platforms with different controllers. - -add_executable(libps2 "keyboard.c" "detect.c" "main.c" "mouse.c") - -target_compile_options(libps2 - PRIVATE $<$:${TARGET_DRIVER_COMPILE_OPTIONS_C}> - PRIVATE $<$:-ffreestanding> - PRIVATE $<$:-Wall> - PRIVATE $<$:-Wextra> - PRIVATE $<$:-fstack-protector-all> - PRIVATE $<$:-fno-builtin-memset> - PRIVATE $<$:-fvisibility=hidden> - PRIVATE $<$:-fPIC> -) - -set_property(TARGET libps2 PROPERTY link_depends ${DRIVER_LINKER_SCRIPT}) - -target_include_directories(libps2 - PRIVATE "${CMAKE_SOURCE_DIR}/src/oboskrnl" - PRIVATE "${CMAKE_SOURCE_DIR}/src/drivers" - PRIVATE ${OBOSKRNL_EXTERNAL_INCLUDES}) - -target_link_options(libps2 - PRIVATE "-nostdlib" - PRIVATE "-fPIC" - PRIVATE "-Wl,-shared" -# PRIVATE "-Wl,--allow-shlib-undefined" - PRIVATE "-T" PRIVATE ${DRIVER_LINKER_SCRIPT} - PRIVATE ${TARGET_DRIVER_LINKER_OPTIONS} -) -target_compile_definitions(libps2 PRIVATE OBOS_DRIVER=1) - -target_compile_definitions(libps2 - PRIVATE $<$:OBOS_DEBUG> - PRIVATE $<$:OBOS_RELEASE> - PRIVATE $<$:OBOS_RELEASE> - PRIVATE $<$:OBOS_RELEASE> - PUBLIC OBOS_DRIVER=1 -) diff --git a/src/drivers/generic/libps2/controller.h b/src/drivers/generic/libps2/controller.h deleted file mode 100644 index 82d956b9..00000000 --- a/src/drivers/generic/libps2/controller.h +++ /dev/null @@ -1,91 +0,0 @@ -/* - * drivers/generic/libps2/controller.h - * - * Copyright (c) 2025 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include - -#include -#include - -#if defined(__x86_64__) || defined(__i686__) -# include -#else -#define IRQL_PS2 -1 -#endif - -enum {PS2_PORT_MAGIC=0x1BADBEEF}; - -enum { - PS2_DEV_TYPE_UNKNOWN = 'u', - PS2_DEV_TYPE_KEYBOARD = 'k', - PS2_DEV_TYPE_MOUSE = 'm', -}; - -typedef struct ps2_port { - union { - uint64_t data; - void* pdata; - }; - union { - uint64_t udata; - void* pudata; - }; - - union { - obos_status(*read_code)(void* handle, keycode* out, bool block); - obos_status(*read_mouse_packet)(void* handle, mouse_packet* out, bool block); - obos_status(*read_raw)(void* handle, void* buf, bool block); - }; - // Gets the amount of readable objects for that handle. - obos_status(*get_readable_count)(void* handle, size_t* nReadable); - obos_status(*make_handle)(struct ps2_port* port, void** handle); - obos_status(*close_handle)(struct ps2_port* port, void* handle); - // To be set by the driver when there is at least one object ready to be read by read_* - // Should be a 'EVENT_NOTIFICATION' - event* data_ready_event; - - // NOTE: Remove this and make it correct when the net-stack branch is - // merged, and add the (un)reference_interface callbacks. - void* default_handle; - - struct irq* irq; - void(*data_ready)(struct ps2_port* channel, uint8_t data); - - size_t blk_size; - - union { - OBOS_PACK struct { - char id[4]; - uint8_t padding; - }; - char str_id[5]; - }; - - uint32_t magic; - uint32_t gsi; - - uint16_t model; - - char type; - - bool works : 1; - bool suppress_irqs : 1; - bool second : 1; - - struct vnode* vn; - struct dirent* ent; -} ps2_port; - -DRV_EXPORT void PS2_DeviceWrite(bool channel_two, uint8_t val); -DRV_EXPORT uint8_t PS2_DeviceRead(uint32_t spin_timeout, obos_status* status); -DRV_EXPORT obos_status PS2_EnableChannel(bool channel_two, bool status); -DRV_EXPORT obos_status PS2_MaskChannelIRQs(bool channel_two, bool mask); -DRV_EXPORT obos_status PS2_FlushInput(); -DRV_EXPORT ps2_port* PS2_GetPort(bool channel_two); \ No newline at end of file diff --git a/src/drivers/generic/libps2/detect.c b/src/drivers/generic/libps2/detect.c deleted file mode 100644 index b8ef17fd..00000000 --- a/src/drivers/generic/libps2/detect.c +++ /dev/null @@ -1,170 +0,0 @@ -/* - * generic/libps2/detect.c - * - * Copyright (c) 2025 Omar Berrow -*/ - -#include -#include -#include -#include - -#include "controller.h" -#include "detect.h" -#include "keyboard.h" -#include "mouse.h" - -static OBOS_PAGEABLE_FUNCTION uint8_t send_command_impl(ps2_port* port, obos_status* status, uint8_t cmd, size_t nArgs, va_list list) -{ - PS2_DeviceWrite(port->second, cmd); - uint8_t ack = PS2_DeviceRead(0x10000, status); - if (ack != PS2_ACK) - return ack; - for (size_t i = 0; i < nArgs; i++) - { - PS2_DeviceWrite(port->second, va_arg(list, uint32_t) & 0xff); - ack = PS2_DeviceRead(0x10000, status); - if (ack != PS2_ACK) - return ack; - } - return PS2_ACK; -} - -OBOS_PAGEABLE_FUNCTION uint8_t PS2_SendCommand(ps2_port* port, uint8_t cmd, size_t nArgs, ...) -{ - obos_status status = OBOS_STATUS_SUCCESS; - va_list list; - va_start(list, nArgs); - uint8_t res = send_command_impl(port, &status, cmd, nArgs, list); - if (obos_is_error(status)) - { - OBOS_Warning("Timeout while waiting for a response from the PS/2 Device on channel %c. Aborting\n", port->second ? '2' : '1'); - res = PS2_INVALID_RESPONSE; - goto done; - } - if (res == PS2_ACK) - goto ack; - ack: - done: - va_end(list); - return res; -} - -void identify_device(ps2_port* port, uint16_t* const model, uint8_t* const type) -{ - irql oldIrql = Core_RaiseIrql(IRQL_PS2); - bool success = false; - for (size_t i = 0; i < 2; i++) - { - if ((success = (PS2_SendCommand(port, 0xf2, 0) == PS2_ACK))) - break; - } - if (!success) - { - OBOS_Warning("Failed to identify device on channel %c: Failed to receive ACK\n", port->second ? '2' : '1'); - Core_LowerIrql(oldIrql); - return; - } - uint16_t byte_one = PS2_DeviceRead(0x10000, nullptr); - uint16_t byte_two = PS2_DeviceRead(0x10000, nullptr); - bool byte_one_timed_out = byte_one == 0xff; - bool byte_two_timed_out = byte_two == 0xff; - if (byte_one_timed_out && byte_two_timed_out) - { - // An old device, assume it's a keyboard. - *type = PS2_DEV_TYPE_KEYBOARD; - goto done; - } - if (byte_two_timed_out) - byte_two = 0; - *model = byte_two | (byte_one<<8); - if (byte_one == 0xab || byte_one == 0xac) - { - *type = PS2_DEV_TYPE_KEYBOARD; - goto done; - } - if (byte_two_timed_out) - { - // One-byte IDs should always be mice. - *type = PS2_DEV_TYPE_MOUSE; - goto done; - } - done: - Core_LowerIrql(oldIrql); -} - -void PS2_DetectDevice(ps2_port* port) -{ - uint8_t type = 0; - - // bool retried = false; - // retry_echo: - // PS2_DeviceWrite(port->second, 0xee); - // uint8_t echo_response = PS2_DeviceRead(0xffff, nullptr); - // if (echo_response == 0xfe) - // { - // if (retried) - // return; // No device. - // retried = true; - // goto retry_echo; - // } - - irql oldIrql = Core_RaiseIrql(IRQL_PS2); - PS2_SendCommand(port, 0xf5, 0); - Core_LowerIrql(oldIrql); - - uint16_t model = 0; - identify_device(port, &model, &type); - if (type == 0) - return; - if ((type == PS2_DEV_TYPE_KEYBOARD && port->second) || (type == PS2_DEV_TYPE_MOUSE && !port->second)) - { - // Try this again... - uint16_t model2 = 0; - uint8_t type2 = 0; - identify_device(port, &model2, &type2); - if (type2 == type && model2 == model) - goto done; - // Otherwise, choose the new ones. - type = type2; - model = model2; - } - - done: - oldIrql = Core_RaiseIrql(IRQL_PS2); - PS2_SendCommand(port, 0xf4, 0); - Core_LowerIrql(oldIrql); - port->model = model; - - if (type != PS2_DEV_TYPE_UNKNOWN) - OBOS_Log("PS/2: Found a %s on channel %c (model id: 0x%04x).\n", type == PS2_DEV_TYPE_KEYBOARD ? "keyboard" : "mouse", port->second ? '2' : '1', model); - - switch (type) { - case PS2_DEV_TYPE_KEYBOARD: - PS2_InitializeKeyboard(port); - break; - case PS2_DEV_TYPE_MOUSE: - PS2_InitializeMouse(port); - break; - default: - break; - } -} - -void PS2_EnableDevices() -{ - ps2_port* port_one = PS2_GetPort(false); - ps2_port* port_two = PS2_GetPort(true); - for (int i = 0; i < 2; i++) - { - ps2_port* port = (i == 1) ? port_two : port_one; - if (!port) - continue; - if (!port->works) - continue; - if (port->type == PS2_DEV_TYPE_KEYBOARD) - PS2_StartKeyboard(port); - else if (port->type == PS2_DEV_TYPE_MOUSE) - PS2_StartMouse(port); - } -} diff --git a/src/drivers/generic/libps2/detect.h b/src/drivers/generic/libps2/detect.h deleted file mode 100644 index a31e00aa..00000000 --- a/src/drivers/generic/libps2/detect.h +++ /dev/null @@ -1,16 +0,0 @@ -/* - * generic/libps2/detect.h - * - * Copyright (c) 2025 Omar Berrow -*/ - -#pragma once - -#include - -#include "controller.h" - -void PS2_DetectDevice(ps2_port* port); -void PS2_EnableDevices(); - -uint8_t PS2_SendCommand(ps2_port* port, uint8_t cmd, size_t nArgs, ...); \ No newline at end of file diff --git a/src/drivers/generic/libps2/keyboard.c b/src/drivers/generic/libps2/keyboard.c deleted file mode 100644 index 63a9b9cf..00000000 --- a/src/drivers/generic/libps2/keyboard.c +++ /dev/null @@ -1,395 +0,0 @@ -/* - * drivers/generic/libps2/keyboard.c - * - * Copyright (c) 2025 Omar Berrow -*/ - -#include -#include -#include -#include -#include - -#include - -#include -#include - -#include - -#include -#include - -#include - -#include - -#include "keyboard.h" -#include "scancode_tables.h" -#include "controller.h" -#include "detect.h" - -static void signal_ring_buffer_dpc(dpc* d, void* userdata) -{ - OBOS_UNUSED(d); - ps2_ringbuffer* buff = userdata; - Core_EventSet(&buff->e, true); -} - -static void keyboard_ready(ps2_port* port, uint8_t scancode) -{ - ps2k_data* data = port->pudata; - if (!data->initialized) - return; - - // A command response. - if (scancode == 0xfa || scancode == 0xfe) - return; - - if (scancode == 0xe0) - { - data->processing_extended = true; - return; - } - if (scancode == 0xf0 && data->set == 2) - { - data->processing_release = true; - return; - } - - const uint8_t scancode_raw = (data->set == 1 ? (scancode & 0x7f) : scancode) - (data->processing_extended ? 0x10 : 0); - const keycode *raw_code = nullptr; - bool released = data->set == 1 ? (scancode & BIT(7)) : data->processing_release; - data->processing_release = false; - - if (data->processing_extended) - { - data->processing_extended = false; - if (scancode_raw >= ((data->set == 2) ? sizeof(set2_keycode_extended)/sizeof(keycode) : sizeof(set1_keycode_extended)/sizeof(keycode))) - return; // funy - raw_code = data->set == 1 ? &set1_keycode_extended[scancode_raw] : &set2_keycode_extended[scancode_raw]; - } - else - { - if (scancode_raw >= ((data->set == 2) ? sizeof(set2_keycode_normal)/sizeof(keycode) : sizeof(set1_keycode_normal)/sizeof(keycode))) - return; // funy - raw_code = data->set == 1 ? &set1_keycode_normal[scancode_raw] : &set2_keycode_normal[scancode_raw]; - } - - switch (SCANCODE_FROM_KEYCODE(*raw_code)) - { - case SCANCODE_CTRL: - data->ctrl = !released; - break; - case SCANCODE_ALT: - data->alt = !released; - break; - case SCANCODE_SHIFT: - data->shift = !released; - break; - case SCANCODE_FN: - data->fn = !released; - break; - case SCANCODE_SUPER_KEY: - data->super_key = !released; - break; - default: break; - } - - bool changed_status = false; - - if (MODIFIERS_FROM_KEYCODE(*raw_code) & CAPS_LOCK && released) - { - data->caps_lock = !data->caps_lock; - changed_status = true; - } - if (MODIFIERS_FROM_KEYCODE(*raw_code) & NUM_LOCK && released) - { - data->num_lock = !data->num_lock; - changed_status = true; - } - - if (changed_status) - { - uint8_t led_state = 0; - if (data->num_lock) - led_state |= BIT(1); - if (data->caps_lock) - led_state |= BIT(2); - // TODO: Scroll lock. - PS2_SendCommand(port, 0xed, 1, led_state); - } - - if (SCANCODE_FROM_KEYCODE(*raw_code) == SCANCODE_UNKNOWN) - return; - - keycode code = *raw_code; - KEYCODE_ADD_MODIFIER(code, released ? KEY_RELEASED : 0); - KEYCODE_ADD_MODIFIER(code, data->ctrl ? CTRL : 0); - KEYCODE_ADD_MODIFIER(code, data->alt ? ALT : 0); - KEYCODE_ADD_MODIFIER(code, data->fn ? FN : 0); - KEYCODE_ADD_MODIFIER(code, data->shift ? SHIFT : 0); - KEYCODE_ADD_MODIFIER(code, data->caps_lock ? CAPS_LOCK : 0); - KEYCODE_ADD_MODIFIER(code, data->num_lock ? NUM_LOCK : 0); - KEYCODE_ADD_MODIFIER(code, data->super_key ? SUPER_KEY : 0); - - // OBOS_Debug("Got key %s (0x%02x), modifiers 0x%02x\n", OBOS_ScancodeToString[SCANCODE_FROM_KEYCODE(code)], SCANCODE_FROM_KEYCODE(code), MODIFIERS_FROM_KEYCODE(code)); - PS2_RingbufferAppendKeycode(&data->input, code, false); - - data->dpc.userdata = &data->input; - CoreH_InitializeDPC(&data->dpc, signal_ring_buffer_dpc, 0); -} - -static ps2k_data keyboard_data_buf[2]; - -static obos_status read_code(void* handle, keycode* out, bool block) -{ - ps2k_handle* hnd = handle; - if (hnd->magic != PS2K_HND_MAGIC_VALUE) - return OBOS_STATUS_INVALID_ARGUMENT; - ps2_port* port = hnd->port; - ps2k_data* data = port->pudata; - - if (hnd->in_ptr == data->input.out_ptr) - { - if (!Core_EventGetState(port->data_ready_event) && !block) - return OBOS_STATUS_WOULD_BLOCK; - - obos_status status = Core_WaitOnObject(WAITABLE_OBJECT(*port->data_ready_event)); - if (obos_is_error(status)) - return status; - } - - return PS2_RingbufferFetchKeycode(&data->input, &hnd->in_ptr, out); -} - -static obos_status get_readable_count(void* handle, size_t* nReadable) -{ - ps2k_handle* hnd = handle; - if (hnd->magic != PS2K_HND_MAGIC_VALUE) - return OBOS_STATUS_INVALID_ARGUMENT; - ps2_port* port = hnd->port; - ps2k_data* data = port->pudata; - *nReadable = (data->input.out_ptr - hnd->in_ptr); - return OBOS_STATUS_SUCCESS; -} - -static obos_status make_handle(struct ps2_port* port, void** handle) -{ - ps2k_data* data = port->pudata; - if (data->ps2k_magic != PS2K_MAGIC_VALUE) - return OBOS_STATUS_INVALID_ARGUMENT; - ps2k_handle* hnd = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(ps2k_handle), nullptr); - hnd->magic = PS2K_HND_MAGIC_VALUE; - hnd->port = port; - hnd->in_ptr = data->input.out_ptr; - *handle = hnd; - return OBOS_STATUS_SUCCESS; -} -static obos_status close_handle(struct ps2_port* port, void* handle) -{ - ps2k_data* data = port->pudata; - if (data->ps2k_magic != PS2K_MAGIC_VALUE || ((ps2k_handle*)handle)->magic != PS2K_HND_MAGIC_VALUE) - return OBOS_STATUS_INVALID_ARGUMENT; - Free(OBOS_KernelAllocator, handle, sizeof(ps2k_handle)); - return OBOS_STATUS_SUCCESS; -} - -OBOS_PAGEABLE_FUNCTION void PS2_InitializeKeyboard(ps2_port* port) -{ - OBOS_ASSERT(Core_GetIrql() < IRQL_DISPATCH); - OBOS_Log("PS/2: Initializing PS/2 Keyboard on channel %c\n", port->second ? '2' : '1'); - // PS2_MaskChannelIRQs(port->second, true); - irql oldIrql = Core_RaiseIrql(IRQL_PS2); - - // port->suppress_irqs = true; - - port->pudata = &keyboard_data_buf[port->second]; - ps2k_data* data = port->pudata; - data->ps2k_magic = PS2K_MAGIC_VALUE; - data->port = port; - data->initialized = false; - - port->data_ready = keyboard_ready; - - uint8_t res = PS2_SendCommand(port, 0xff, 0); - if (res != 0xfa) - { - Core_LowerIrql(oldIrql); - return; - } - int retries = 0; - up: - res = PS2_DeviceRead(1024, nullptr); - if (res == 0xff && retries++ < 5) - goto up; - - if (res != 0xAA) - { - OBOS_Warning("PS/2: While resetting PS/2 keyboard: Got 0x%02x instead of 0xaa (test success code). Aborting initialization\n", res); - Core_LowerIrql(oldIrql); - return; - } - - PS2_SendCommand(port, 0xf5, 0); - - // Keys need to held for 250 ms before repeating, and they repeat at a rate of 30 hz (33.33333 ms). - res = PS2_SendCommand(port, 0xf3, 1, 0x00); - if (res != PS2_ACK) - { - Core_LowerIrql(oldIrql); - return; - } - - // Clear keyboard LEDs. - res = PS2_SendCommand(port, 0xed, 1, 0x0); - if (res != PS2_ACK) - { - Core_LowerIrql(oldIrql); - return; - } - - Core_LowerIrql(oldIrql); - - PS2_FlushInput(); - - port->suppress_irqs = true; - - uint8_t set = 0; -#define DEFAULT_SET 2 -#define ALTERNATE_SET 1 - // Try putting the keyboard into scancode set #2 by default, if that doesn't work (it sends RESEND), put it into scancode set #1 - set = DEFAULT_SET; - bool found = false; - while (!found) - { - bool retried = false; - res = PS2_SendCommand(port, 0xf0, 1, set); - if (res == PS2_RESEND && !retried) - { - retried = true; - continue; - } - if (set == ALTERNATE_SET && res == PS2_RESEND) - { - set = 0; - break; - } - if (res == PS2_RESEND) - set = ALTERNATE_SET; - else - found = true; - } - if (!set) - { - OBOS_Error("PS/2: Could not put the keyboard into a defined scancode set (tried sets one and two, neither were recognized).\n"); - return; - } - - // Enable scanning. - // res = PS2_SendCommand(port, 0xf4, 0); - // if (res != PS2_ACK) - // { - // Core_LowerIrql(oldIrql); - // return; - // } - - // 'true' is not an error, see PS2_StartKeyboard - port->suppress_irqs = true; - - data->set = set; - - data->input.e = EVENT_INITIALIZE(EVENT_NOTIFICATION); - port->data_ready_event = &data->input.e; - port->read_code = read_code; - port->make_handle = make_handle; - port->close_handle = close_handle; - port->get_readable_count = get_readable_count; - - OBOS_Log("PS/2: Successfully initialized keyboard on channel %c\n", port->second ? '2' : '1'); - OBOS_Debug("PS/2 Keyboard is using scancode set %d\n", data->set); - data->initialized = true; - PS2_RingbufferInitialize(&data->input, false); - - port->type = PS2_DEV_TYPE_KEYBOARD; - port->id[3] = port->type; - make_handle(port, &port->default_handle); - port->blk_size = sizeof(keycode); -} - -void PS2_StartKeyboard(ps2_port* port) -{ - uint8_t res = PS2_SendCommand(port, 0xf4, 0); - if (res != PS2_ACK) - return; - - port->suppress_irqs = false; -} - -obos_status PS2_RingbufferInitialize(ps2_ringbuffer* buff, bool mouse) -{ - memzero(buff, sizeof(*buff)); - - buff->e = EVENT_INITIALIZE(EVENT_NOTIFICATION); - buff->size = OBOS_PAGE_SIZE; - if (!mouse) - buff->nElements = buff->size/sizeof(keycode); - else - buff->nElements = buff->size/sizeof(mouse_packet); - - page* phys = MmH_PgAllocatePhysical(false, false); - if (!phys) - return OBOS_STATUS_NOT_ENOUGH_MEMORY; - buff->buff = MmS_MapVirtFromPhys(phys->phys); - buff->out_ptr = 0; - buff->handle_count = 0; - - return OBOS_STATUS_SUCCESS; -} - -obos_status PS2_RingbufferAppendKeycode(ps2_ringbuffer* buff, keycode code, bool signal) -{ - buff->keycodes[buff->out_ptr++ % buff->nElements] = code; - if (signal) - Core_EventSet(&buff->e, true); - return OBOS_STATUS_SUCCESS; -} - -obos_status PS2_RingbufferFetchKeycode(const ps2_ringbuffer* buff, size_t* in_ptr, keycode* code) -{ - if (*in_ptr == buff->out_ptr) - return OBOS_STATUS_EOF; - *code = buff->keycodes[(*in_ptr)++ % buff->nElements]; - // *in_ptr = *in_ptr % buff->nElemen.ts; // sanitize it for the user - return OBOS_STATUS_SUCCESS; -} - -obos_status PS2_RingbufferAppendMousePacket(ps2_ringbuffer* buff, mouse_packet pckt, bool signal) -{ - buff->mouse_packets[buff->out_ptr++ % buff->nElements] = pckt; - if (signal) - Core_EventSet(&buff->e, true); - return OBOS_STATUS_SUCCESS; -} - -obos_status PS2_RingbufferFetchMousePacket(const ps2_ringbuffer* buff, size_t* in_ptr, mouse_packet* pckt) -{ - if (*in_ptr == buff->out_ptr) - return OBOS_STATUS_EOF; - *pckt = buff->mouse_packets[(*in_ptr)++ % buff->nElements]; - // *in_ptr = *in_ptr % buff->nElements; // sanitize it for the user - return OBOS_STATUS_SUCCESS; -} - -obos_status PS2_RingbufferFree(ps2_ringbuffer* buff) -{ - memset(buff->buff, 0xcc, buff->size); - uintptr_t phys = MmS_UnmapVirtFromPhys(buff->buff); - page what = {.phys=phys}; - Core_MutexAcquire(&Mm_PhysicalPagesLock); - page* pg = RB_FIND(phys_page_tree, &Mm_PhysicalPages, &what); - Core_MutexRelease(&Mm_PhysicalPagesLock); - MmH_DerefPage(pg); - memset(buff, 0xcc, sizeof(*buff)); - return OBOS_STATUS_SUCCESS; -} \ No newline at end of file diff --git a/src/drivers/generic/libps2/keyboard.h b/src/drivers/generic/libps2/keyboard.h deleted file mode 100644 index cb215482..00000000 --- a/src/drivers/generic/libps2/keyboard.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * drivers/generic/libps2/keyboard.h - * - * Copyright (c) 2025 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include - -#include - -#include - -#include "controller.h" -#include "ringbuffer.h" - -#define PS2_ACK 0xfa -#define PS2_RESEND 0xfe -#define PS2_INVALID_RESPONSE 0xff - -enum { - PS2K_MAGIC_VALUE = 0xFEE1DEAD, - PS2K_HND_MAGIC_VALUE = 0xFEE1DEAE -}; - -typedef struct ps2k_handle { - uint32_t magic; // PS2K_HND_MAGIC_VALUE - ps2_port* port; - size_t in_ptr; -} ps2k_handle; - -typedef struct ps2k_data { - struct ps2_ringbuffer input; - ps2_port* port; - uint32_t ps2k_magic; - dpc dpc; - uint8_t set; - bool initialized : 1; - bool processing_extended : 1; - bool processing_release : 1; // only valid if set == 2 - bool super_key : 1; - bool caps_lock : 1; - bool num_lock : 1; - bool ctrl : 1; - bool shift : 1; - bool alt : 1; - bool fn : 1; -} ps2k_data; - -void PS2_InitializeKeyboard(ps2_port* port); -void PS2_StartKeyboard(ps2_port* port); -void PS2_FreeKeyboard(ps2_port* port); \ No newline at end of file diff --git a/src/drivers/generic/libps2/main.c b/src/drivers/generic/libps2/main.c deleted file mode 100644 index aa3b535e..00000000 --- a/src/drivers/generic/libps2/main.c +++ /dev/null @@ -1,280 +0,0 @@ -/* - * generic/libps2/main.c - * - * Copyright (c) 2025 Omar Berrow -*/ - -#include -#include -#include -#include - -#include -#include - -#include -#include -#include -#include - -#include - -#include "controller.h" -#include "keyboard.h" -#include "mouse.h" -#include "detect.h" - -typedef struct ps2_handle { - uint32_t magic; - ps2_port* port; - char drv_data[]; -} ps2_handle; - -obos_status get_blk_size(dev_desc desc, size_t* blk_size) -{ - if (!desc || !blk_size) - return OBOS_STATUS_INVALID_ARGUMENT; - ps2_handle* handle = (void*)desc; - if (handle->magic != PS2M_HND_MAGIC_VALUE && handle->magic != PS2K_HND_MAGIC_VALUE) - { - ps2_port* port = (void*)desc; - if (port->magic != PS2_PORT_MAGIC) - return OBOS_STATUS_INVALID_ARGUMENT; - *blk_size = port->blk_size; - return OBOS_STATUS_SUCCESS; - } - ps2_port* port = handle->port; - if (port->magic != PS2_PORT_MAGIC) - return OBOS_STATUS_INVALID_ARGUMENT; - *blk_size = port->blk_size; - return OBOS_STATUS_SUCCESS; -} - -obos_status get_max_blk_count(dev_desc desc, size_t* count) -{ - OBOS_UNUSED(desc && count); - return OBOS_STATUS_INVALID_OPERATION; -} - -obos_status read_sync(dev_desc desc, void* buf, size_t blkCount, size_t blkOffset, size_t* nBlkRead) -{ - OBOS_UNUSED(blkOffset); - - if (!desc || !buf) - return OBOS_STATUS_INVALID_ARGUMENT; - ps2_handle* handle = (void*)desc; - if (handle->magic != PS2M_HND_MAGIC_VALUE && handle->magic != PS2K_HND_MAGIC_VALUE) - return OBOS_STATUS_INVALID_ARGUMENT; - ps2_port* port = handle->port; - if (port->magic != PS2_PORT_MAGIC) - return OBOS_STATUS_INVALID_ARGUMENT; - - uint8_t *out = buf; - for (size_t i = 0; i < blkCount; i++) - port->read_raw(handle, &out[i*port->blk_size], true); - - if (nBlkRead) - *nBlkRead = blkCount; - - return OBOS_STATUS_SUCCESS; -} - -obos_status write_sync(dev_desc desc, const void* buf, size_t blkCount, size_t blkOffset, size_t* nBlkWritten) -{ - OBOS_UNUSED(desc && buf && blkCount && blkOffset && nBlkWritten); - return OBOS_STATUS_INVALID_OPERATION; -} - -obos_status foreach_device(iterate_decision(*cb)(dev_desc desc, size_t blkSize, size_t blkCount, void* userdata), void* userdata) -{ - if (PS2_GetPort(false) && PS2_GetPort(true)->read_raw) - cb((dev_desc)PS2_GetPort(false), PS2_GetPort(false)->blk_size, 0, userdata); - if (PS2_GetPort(true) && PS2_GetPort(true)->read_raw) - cb((dev_desc)PS2_GetPort(true), PS2_GetPort(true)->blk_size, 0, userdata); - return OBOS_STATUS_SUCCESS; -} - -obos_status query_user_readable_name(dev_desc what, const char** name) -{ - if (!what || !name) - return OBOS_STATUS_INVALID_ARGUMENT; - ps2_handle* handle = (void*)what; - if (handle->magic != PS2M_HND_MAGIC_VALUE && handle->magic != PS2K_HND_MAGIC_VALUE) - return OBOS_STATUS_INVALID_ARGUMENT; - ps2_port* port = handle->port; - if (port->magic != PS2_PORT_MAGIC) - return OBOS_STATUS_INVALID_ARGUMENT; - *name = port->str_id; - return OBOS_STATUS_SUCCESS; -} - -obos_status ioctl(dev_desc what, uint32_t request, void* argp) -{ - if (!argp || !what) - return OBOS_STATUS_INVALID_ARGUMENT; - ps2_handle* handle = (void*)what; - if (handle->magic != PS2M_HND_MAGIC_VALUE && handle->magic != PS2K_HND_MAGIC_VALUE) - return OBOS_STATUS_INVALID_ARGUMENT; - ps2_port* port = handle->port; - if (port->magic != PS2_PORT_MAGIC) - return OBOS_STATUS_INVALID_ARGUMENT; - obos_status st = OBOS_STATUS_SUCCESS; - switch (request) - { - case 1: - st = port->get_readable_count(handle, argp); - break; - default: - st = OBOS_STATUS_INVALID_IOCTL; - break; - } - return st; -} - -obos_status ioctl_argp_size(uint32_t request, size_t* res) -{ - obos_status st = OBOS_STATUS_SUCCESS; - switch (request) - { - case 1: - *res = sizeof(size_t); - break; - default: - st = OBOS_STATUS_INVALID_IOCTL; - break; - } - return st; -} - -static void cleanup_port_vn(ps2_port* port) -{ - Vfs_UnlinkNode(port->ent, false); - port->vn->flags |= VFLAGS_DRIVER_DEAD; -} - -void cleanup() -{ - ps2_port* port1 = PS2_GetPort(false); - ps2_port* port2 = PS2_GetPort(false); - if (port1) - cleanup_port_vn(port1); - if (port2) - cleanup_port_vn(port2); -} - -static void irp_event_set(irp* req) -{ - ps2_handle* handle = (void*)req->desc; - ps2_port* port = handle->port; - size_t nReady = 0; - port->get_readable_count(handle, &nReady); - if (nReady >= req->blkCount) - req->status = !req->dryOp ? read_sync(req->desc, req->buff, req->blkCount, 0, &req->nBlkRead) : OBOS_STATUS_SUCCESS; - else - req->status = OBOS_STATUS_IRP_RETRY; - Core_EventClear(req->evnt); -} -obos_status submit_irp(void* req_) -{ - irp* req = req_; - if (!req) - return OBOS_STATUS_INVALID_ARGUMENT; - if ((!req->buff && !req->dryOp) || !req->refs || !req->desc) - return OBOS_STATUS_INVALID_ARGUMENT; - - if (req->op == IRP_WRITE) - return OBOS_STATUS_INVALID_OPERATION; - - ps2_handle* handle = (void*)req->desc; - if (handle->magic != PS2M_HND_MAGIC_VALUE && handle->magic != PS2K_HND_MAGIC_VALUE) - return OBOS_STATUS_INVALID_ARGUMENT; - ps2_port* port = handle->port; - if (port->magic != PS2_PORT_MAGIC) - return OBOS_STATUS_INVALID_ARGUMENT; - size_t nReady = 0; - port->get_readable_count(handle, &nReady); - if (nReady >= req->blkCount) - { - req->evnt = nullptr; - req->status = req->dryOp ? OBOS_STATUS_SUCCESS : read_sync(req->desc, req->buff, req->blkCount, 0, &req->nBlkRead); - } - else - { - req->on_event_set = irp_event_set; - req->evnt = port->data_ready_event; - } - - return OBOS_STATUS_SUCCESS; -} - -OBOS_WEAK void on_suspend(); -OBOS_WEAK void on_wake(); - -obos_status reference_device(dev_desc* desc) -{ - if (!desc || !*desc) - return OBOS_STATUS_INVALID_ARGUMENT; - ps2_port* port = (void*)*desc; - return port->make_handle(port, (void**)desc); -} - -obos_status unreference_device(dev_desc desc) -{ - if (!desc) - return OBOS_STATUS_INVALID_ARGUMENT; - ps2_handle* handle = (void*)desc; - if (handle->magic != PS2M_HND_MAGIC_VALUE && handle->magic != PS2K_HND_MAGIC_VALUE) - return OBOS_STATUS_INVALID_ARGUMENT; - return handle->port->close_handle(handle->port, handle); -} - -__attribute__((section(OBOS_DRIVER_HEADER_SECTION))) driver_header drv_hdr = { - .magic = OBOS_DRIVER_MAGIC, - .flags = DRIVER_HEADER_HAS_VERSION_FIELD|DRIVER_HEADER_HAS_STANDARD_INTERFACES, - .ftable = { - .driver_cleanup_callback = cleanup, - .ioctl = ioctl, - .ioctl_argp_size = ioctl_argp_size, - .get_blk_size = get_blk_size, - .get_max_blk_count = get_max_blk_count, - .query_user_readable_name = query_user_readable_name, - .foreach_device = foreach_device, - .read_sync = read_sync, - .write_sync = write_sync, - .on_suspend = on_suspend, - .on_wake = on_wake, - .submit_irp = submit_irp, - .reference_device = reference_device, - .unreference_device = unreference_device, - }, - .driverName = "PS/2 Driver", - .version = 1, - .mainThreadAffinity = 0b1, -}; - -OBOS_PAGEABLE_FUNCTION driver_init_status OBOS_DriverEntry(driver_id* this) -{ - ps2_port* port_one = PS2_GetPort(false); - ps2_port* port_two = PS2_GetPort(true); - for (size_t i = 0; i < 2; i++) - { - ps2_port* port = (i == 1) ? port_two : port_one; - if (!port) - continue; - if (!port->works) - continue; - PS2_DetectDevice(port); - if (port->type == PS2_DEV_TYPE_UNKNOWN) - continue; - vnode* vn = Drv_AllocateVNode(this, (uintptr_t)port, 0, nullptr, VNODE_TYPE_CHR); - port->vn = vn; - char dev_name[6] = {}; - memcpy(dev_name, port->str_id, 5); - dev_name[4] = i == 0 ? '1' : '2'; - dev_name[5] = 0; - OBOS_Debug("%*s: Registering PS/2 Device at %s%c%s\n", strnlen(this->header.driverName, 64), this->header.driverName, OBOS_DEV_PREFIX, OBOS_DEV_PREFIX[sizeof(OBOS_DEV_PREFIX)-1] == '/' ? 0 : '/', dev_name); - port->ent = Drv_RegisterVNode(vn, dev_name); - } - PS2_EnableDevices(); - return (driver_init_status){.status=OBOS_STATUS_SUCCESS,.fatal=false,.context=nullptr}; -} \ No newline at end of file diff --git a/src/drivers/generic/libps2/mouse.c b/src/drivers/generic/libps2/mouse.c deleted file mode 100644 index c8072e48..00000000 --- a/src/drivers/generic/libps2/mouse.c +++ /dev/null @@ -1,233 +0,0 @@ -/* - * drivers/generic/libps2/mouse.h - * - * Copyright (c) 2025 Omar Berrow -*/ - -#include -#include -#include - -#include - -#include - -#include - -#include - -#include "controller.h" -#include "ringbuffer.h" -#include "mouse.h" -#include "detect.h" - -static ps2m_data mouse_data_buf[2]; - -static obos_status read_pckt(void* handle, mouse_packet* out, bool block) -{ - ps2m_handle* hnd = handle; - if (hnd->magic != PS2M_HND_MAGIC_VALUE) - return OBOS_STATUS_INVALID_ARGUMENT; - ps2_port* port = hnd->port; - ps2m_data* data = port->pudata; - - if (hnd->in_ptr == data->packets.out_ptr) - { - if (!Core_EventGetState(port->data_ready_event) && !block) - return OBOS_STATUS_WOULD_BLOCK; - - obos_status status = Core_WaitOnObject(WAITABLE_OBJECT(*port->data_ready_event)); - if (obos_is_error(status)) - return status; - } - - return PS2_RingbufferFetchMousePacket(&data->packets, &hnd->in_ptr, out); -} - -static obos_status get_readable_count(void* handle, size_t* nReadable) -{ - ps2m_handle* hnd = handle; - if (hnd->magic != PS2M_HND_MAGIC_VALUE) - return OBOS_STATUS_INVALID_ARGUMENT; - ps2_port* port = hnd->port; - ps2m_data* data = port->pudata; - if ((hnd->in_ptr / data->packets.nElements) < (data->packets.out_ptr / data->packets.nElements)) - { - *nReadable = data->packets.nElements; - return OBOS_STATUS_SUCCESS; - } - *nReadable = (data->packets.out_ptr - hnd->in_ptr); - OBOS_ENSURE(*nReadable <= data->packets.nElements); - return OBOS_STATUS_SUCCESS; -} - -static obos_status make_handle(struct ps2_port* port, void** handle) -{ - ps2m_data* data = port->pudata; - if (data->magic != PS2M_MAGIC_VALUE) - return OBOS_STATUS_INVALID_ARGUMENT; - ps2m_handle* hnd = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(ps2m_handle), nullptr); - hnd->magic = PS2M_HND_MAGIC_VALUE; - hnd->port = port; - hnd->in_ptr = data->packets.out_ptr; - *handle = hnd; - return OBOS_STATUS_SUCCESS; -} -static obos_status close_handle(struct ps2_port* port, void* handle) -{ - ps2m_data* data = port->pudata; - if (data->magic != PS2M_MAGIC_VALUE || ((ps2m_handle*)handle)->magic != PS2M_HND_MAGIC_VALUE) - return OBOS_STATUS_INVALID_ARGUMENT; - Free(OBOS_KernelAllocator, handle, sizeof(ps2m_handle)); - return OBOS_STATUS_SUCCESS; -} - -static void set_mouse_rate(ps2_port* port, uint8_t rate) -{ - uint8_t res = 0; - if ((res = PS2_SendCommand(port, 0xf3, 1, rate)) != 0xfa) - OBOS_Warning("set_mouse_rate(%d) got 0x%02x instead of ACK from mouse.\n", rate, res); -} - -static uint8_t get_id(ps2_port* port) -{ - uint8_t res = PS2_SendCommand(port, 0xf2, 0); - if (res != 0xfa) - { - OBOS_Warning("get_id() got 0x%02x instead of ACK from mouse.\n", res); - return res; - } - return PS2_DeviceRead(0x20000, nullptr); -} - -static void dpc_hnd(dpc* d, void* udata) -{ - OBOS_UNUSED(d); - Core_EventSet((event*)udata, false); -} -static void mouse_ready(ps2_port* port, uint8_t b) -{ - ps2m_data* data = port->pudata; - data->raw_pckt[data->nReady++] = b; - if (!ps2m_enough_data(data)) - return; - data->nReady = 0; - - mouse_packet pckt = {}; - - pckt.mb = data->basic_pckt.flags & PS2M_BM; - pckt.lb = data->basic_pckt.flags & PS2M_BL; - pckt.rb = data->basic_pckt.flags & PS2M_BR; - if (data->b4b5_extension_enabled) - { - pckt.b4 = data->b5_pckt.flags2 & PS2M_FLAGS2_B4; - pckt.b5 = data->b5_pckt.flags2 & PS2M_FLAGS2_B5; - pckt.x = data->b5_pckt.x - (data->b5_pckt.flags & PS2M_XS ? 0x100 : 0); - pckt.y = data->b5_pckt.y - (data->b5_pckt.flags & PS2M_YS ? 0x100 : 0); - pckt.z = data->b5_pckt.flags2 & PS2M_FLAGS2_Z_MASK; - } - else if (data->z_axis_extension_enabled) - { - pckt.b4 = 0; - pckt.b5 = 0; - pckt.x = data->z_pckt.x - (data->z_pckt.flags & PS2M_XS ? 0x100 : 0); - pckt.y = data->z_pckt.y - (data->z_pckt.flags & PS2M_YS ? 0x100 : 0); - pckt.z = (data->z_pckt.z & 0x7) * (data->z_pckt.z & 0x8 ? -1 : 1); - } - else - { - pckt.b4 = 0; - pckt.b5 = 0; - pckt.z = 0; - pckt.x = data->basic_pckt.x - (data->basic_pckt.flags & PS2M_XS ? 0x100 : 0); - pckt.y = data->basic_pckt.y - (data->basic_pckt.flags & PS2M_YS ? 0x100 : 0); - } - - PS2_RingbufferAppendMousePacket(&data->packets, pckt, false); - data->dpc.userdata = (struct event*)port->data_ready_event; - CoreH_InitializeDPC(&data->dpc, dpc_hnd, Core_DefaultThreadAffinity); -} - -void PS2_InitializeMouse(ps2_port* port) -{ - OBOS_Log("PS/2: Initializing PS/2 Mouse on channel %c\n", port->second ? '2' : '1'); - - port->pudata = &mouse_data_buf[port->second]; - ps2m_data* data = port->pudata; - data->magic = PS2M_MAGIC_VALUE; - data->port = port; - data->initialized = false; - - port->suppress_irqs = true; - // irql oldIrql = Core_RaiseIrql(IRQL_PS2); - - uint8_t res = PS2_SendCommand(port, 0xff, 0); - if (res != 0xfa) - return; - - res = PS2_DeviceRead(0xffff, nullptr); - if (res != 0xAA) - { - OBOS_Warning("PS/2: While resetting PS/2 Mouse: Got 0x%02x instead of 0xaa (test success code). Aborting initialization\n", res); - return; - } - // Discard the next byte (mouse id) - PS2_DeviceRead(0xffff, nullptr); - - // for (volatile bool b = true; b; ) - // ; - - PS2_SendCommand(port, 0xf5, 0); - - // Do fnuy shit to tryn get a z axis, and buttons 4 and 5 - set_mouse_rate(port, 200); - set_mouse_rate(port, 100); - set_mouse_rate(port, 80); - uint8_t id = get_id(port); - if (id != 3) - goto premature_finish; - data->z_axis_extension_enabled = true; - set_mouse_rate(port, 200); - set_mouse_rate(port, 200); - set_mouse_rate(port, 80); - id = get_id(port); - if (id != 4) - goto premature_finish; - data->b4b5_extension_enabled = true; - - premature_finish: - - set_mouse_rate(port, 60); - - // Core_LowerIrql(oldIrql); - - // the lack of suppress_irqs=false is not a bug, - // see PS2_StartMouse - - port->data_ready_event = &data->packets.e; - port->read_mouse_packet = read_pckt; - port->make_handle = make_handle; - port->close_handle = close_handle; - port->get_readable_count = get_readable_count; - port->type = PS2_DEV_TYPE_MOUSE; - port->id[3] = port->type; - make_handle(port, &port->default_handle); - port->blk_size = sizeof(mouse_packet); - data->initialized = true; - PS2_RingbufferInitialize(&data->packets, true); - - OBOS_Log("PS/2: Successfully initialized mouse on channel %c\n", port->second ? '2' : '1'); - - if (data->z_axis_extension_enabled) - OBOS_Log("PS/2: Z Axis Extension Enabled\n"); - if (data->b4b5_extension_enabled) - OBOS_Log("PS/2: Buttons 4&5 Extension Enabled\n"); - port->data_ready = mouse_ready; -} -void PS2_StartMouse(ps2_port* port) -{ - PS2_SendCommand(port, 0xf4, 0); - port->suppress_irqs = false; -} - -void PS2_FreeMouse(ps2_port* port); \ No newline at end of file diff --git a/src/drivers/generic/libps2/mouse.h b/src/drivers/generic/libps2/mouse.h deleted file mode 100644 index c4c5e38f..00000000 --- a/src/drivers/generic/libps2/mouse.h +++ /dev/null @@ -1,91 +0,0 @@ -/* - * drivers/generic/libps2/mouse.h - * - * Copyright (c) 2025 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include - -#include - -#include - -#include "controller.h" -#include "ringbuffer.h" - -enum { - PS2M_YOF = BIT(7), - PS2M_XOF = BIT(6), - PS2M_YS = BIT(5), - PS2M_XS = BIT(4), - PS2M_BM = BIT(2), - PS2M_BR = BIT(1), - PS2M_BL = BIT(0), -}; - -typedef struct ps2m_basic_pckt { - uint8_t flags; - uint8_t x; - uint8_t y; -} ps2m_basic_pckt; - -typedef struct ps2m_z_exten_pckt { - uint8_t flags; - uint8_t x; - uint8_t y; - int8_t z; -} ps2m_z_exten_pckt; - -enum { - PS2M_FLAGS2_Z_MASK = 0xf, - PS2M_FLAGS2_B4 = BIT(4), - PS2M_FLAGS2_B5 = BIT(5), -}; - -typedef struct ps2m_b4b5_exten_pckt { - uint8_t flags; - uint8_t x; - uint8_t y; - uint8_t flags2; -} ps2m_b4b5_exten_pckt; - -enum { - PS2M_MAGIC_VALUE = 0xBEEDDEAD, // BEAD DEAD? - PS2M_HND_MAGIC_VALUE = 0xBADDA600 // BAD DAY? (TIME??) ((SANS REFERENCE??)) -}; - -typedef struct ps2m_data { - uint32_t magic; // PS2M_MAGIC_VALUE - - ps2_port* port; - ps2_ringbuffer packets; - dpc dpc; - - bool initialized : 1; - bool z_axis_extension_enabled : 1; - bool b4b5_extension_enabled : 1; - - union { - struct ps2m_basic_pckt basic_pckt; - struct ps2m_b4b5_exten_pckt b5_pckt; - struct ps2m_z_exten_pckt z_pckt; - uint8_t raw_pckt[4]; - }; - uint8_t nReady; -} ps2m_data; -#define ps2m_enough_data(m) ((m)->nReady >= ((m)->z_axis_extension_enabled ? 4 : 3)) - -typedef struct ps2m_handle { - uint32_t magic; // PS2M_HND_MAGIC_VALUE - ps2_port* port; - size_t in_ptr; -} ps2m_handle; - -void PS2_InitializeMouse(ps2_port* port); -void PS2_StartMouse(ps2_port* port); -void PS2_FreeMouse(ps2_port* port); \ No newline at end of file diff --git a/src/drivers/generic/libps2/ringbuffer.h b/src/drivers/generic/libps2/ringbuffer.h deleted file mode 100644 index e814bc2b..00000000 --- a/src/drivers/generic/libps2/ringbuffer.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * drivers/generic/libps2/ringbuffer.h - * - * Copyright (c) 2025 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include -#include - -#include - -typedef struct ps2_ringbuffer -{ - event e; - union { - void* buff; - keycode* keycodes; - mouse_packet* mouse_packets; - }; - size_t size; - size_t nElements; - size_t out_ptr; - size_t handle_count; -} ps2_ringbuffer; - -obos_status PS2_RingbufferInitialize(ps2_ringbuffer* buff, bool mouse); - -obos_status PS2_RingbufferAppendKeycode(ps2_ringbuffer* buff, keycode code, bool signal); -obos_status PS2_RingbufferFetchKeycode(const ps2_ringbuffer* buff, size_t* in_ptr, keycode* code); - -obos_status PS2_RingbufferAppendMousePacket(ps2_ringbuffer* buff, mouse_packet pckt, bool signal); -obos_status PS2_RingbufferFetchMousePacket(const ps2_ringbuffer* buff, size_t* in_ptr, mouse_packet* pckt); - -obos_status PS2_RingbufferFree(ps2_ringbuffer* buff); \ No newline at end of file diff --git a/src/drivers/generic/libps2/scancode_tables.h b/src/drivers/generic/libps2/scancode_tables.h deleted file mode 100644 index 716a30e4..00000000 --- a/src/drivers/generic/libps2/scancode_tables.h +++ /dev/null @@ -1,449 +0,0 @@ -/* - * generic/libps2/scancode_tables.h - * - * Copyright (c) 2025 Omar Berrow -*/ - -#include - -#include - -// May God have Mercy on the one to have to write a bug fix for this... - -static keycode set1_keycode_normal[0x59] = { - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_ESC,0x00), - KEYCODE(SCANCODE_1,0x00), - KEYCODE(SCANCODE_2,0x00), - KEYCODE(SCANCODE_3,0x00), - KEYCODE(SCANCODE_4,0x00), - KEYCODE(SCANCODE_5,0x00), - KEYCODE(SCANCODE_6,0x00), - KEYCODE(SCANCODE_7,0x00), - KEYCODE(SCANCODE_8,0x00), - KEYCODE(SCANCODE_9,0x00), - KEYCODE(SCANCODE_0,0x00), - KEYCODE(SCANCODE_DASH,0x00), - KEYCODE(SCANCODE_EQUAL,0x00), - KEYCODE(SCANCODE_BACKSPACE,0x00), - KEYCODE(SCANCODE_TAB,0x00), - KEYCODE(SCANCODE_Q,0x00), - KEYCODE(SCANCODE_W,0x00), - KEYCODE(SCANCODE_E,0x00), - KEYCODE(SCANCODE_R,0x00), - KEYCODE(SCANCODE_T,0x00), - KEYCODE(SCANCODE_Y,0x00), - KEYCODE(SCANCODE_U,0x00), - KEYCODE(SCANCODE_I,0x00), - KEYCODE(SCANCODE_O,0x00), - KEYCODE(SCANCODE_P,0x00), - KEYCODE(SCANCODE_SQUARE_BRACKET_LEFT,0x00), - KEYCODE(SCANCODE_SQUARE_BRACKET_RIGHT,0x00), - KEYCODE(SCANCODE_ENTER,0x00), - KEYCODE(SCANCODE_CTRL,CTRL), - KEYCODE(SCANCODE_A,0), - KEYCODE(SCANCODE_S,0), - KEYCODE(SCANCODE_D,0), - KEYCODE(SCANCODE_F,0), - KEYCODE(SCANCODE_G,0), - KEYCODE(SCANCODE_H,0), - KEYCODE(SCANCODE_J,0), - KEYCODE(SCANCODE_K,0), - KEYCODE(SCANCODE_L,0), - KEYCODE(SCANCODE_SEMICOLON,0), - KEYCODE(SCANCODE_APOSTROPHE,0), - KEYCODE(SCANCODE_BACKTICK,0), - KEYCODE(SCANCODE_SHIFT,SHIFT), - KEYCODE(SCANCODE_BACKSLASH,0), - KEYCODE(SCANCODE_Z,0), - KEYCODE(SCANCODE_X,0), - KEYCODE(SCANCODE_C,0), - KEYCODE(SCANCODE_V,0), - KEYCODE(SCANCODE_B,0), - KEYCODE(SCANCODE_N,0), - KEYCODE(SCANCODE_M,0), - KEYCODE(SCANCODE_COMMA,0), - KEYCODE(SCANCODE_DOT,0), - KEYCODE(SCANCODE_FORWARD_SLASH,0), - KEYCODE(SCANCODE_SHIFT,SHIFT), - KEYCODE(SCANCODE_STAR,NUMPAD), - KEYCODE(SCANCODE_ALT,ALT), - KEYCODE(SCANCODE_SPACE,0x00), - KEYCODE(SCANCODE_UNKNOWN,CAPS_LOCK), - KEYCODE(SCANCODE_F1,0x00), - KEYCODE(SCANCODE_F2,0x00), - KEYCODE(SCANCODE_F3,0x00), - KEYCODE(SCANCODE_F4,0x00), - KEYCODE(SCANCODE_F5,0x00), - KEYCODE(SCANCODE_F6,0x00), - KEYCODE(SCANCODE_F7,0x00), - KEYCODE(SCANCODE_F8,0x00), - KEYCODE(SCANCODE_F9,0x00), - KEYCODE(SCANCODE_F10,0x00), - KEYCODE(SCANCODE_UNKNOWN,NUM_LOCK), - KEYCODE(SCANCODE_UNKNOWN,/*SCROLL_LOCK*/0x00), /* scroll lock */ - KEYCODE(SCANCODE_7,NUMPAD), - KEYCODE(SCANCODE_8,NUMPAD), - KEYCODE(SCANCODE_9,NUMPAD), - KEYCODE(SCANCODE_DASH,NUMPAD), - KEYCODE(SCANCODE_4,NUMPAD), - KEYCODE(SCANCODE_5,NUMPAD), - KEYCODE(SCANCODE_6,NUMPAD), - KEYCODE(SCANCODE_PLUS,NUMPAD), - KEYCODE(SCANCODE_1,NUMPAD), - KEYCODE(SCANCODE_2,NUMPAD), - KEYCODE(SCANCODE_3,NUMPAD), - KEYCODE(SCANCODE_0,NUMPAD), - KEYCODE(SCANCODE_DOT,NUMPAD), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_F11,0), - KEYCODE(SCANCODE_F12,0), -}; - -// NOTE: To index, subtract 0x10 from the extended scancode. -static keycode set1_keycode_extended[0x6E - 0x10] = { - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_ENTER,NUMPAD), - KEYCODE(SCANCODE_CTRL,CTRL), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_FORWARD_SLASH,NUMPAD), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_ALT,ALT), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_HOME,0), - KEYCODE(SCANCODE_UP_ARROW,0), - KEYCODE(SCANCODE_PGUP,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_LEFT_ARROW,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_RIGHT_ARROW,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_END,0), - KEYCODE(SCANCODE_DOWN_ARROW,0), - KEYCODE(SCANCODE_PGDOWN,0), - KEYCODE(SCANCODE_INSERT,0), - KEYCODE(SCANCODE_DELETE,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_SUPER_KEY,0), - KEYCODE(SCANCODE_SUPER_KEY,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), -}; - -static keycode set2_keycode_normal[0x84] = { - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_F9,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_F5,0), - KEYCODE(SCANCODE_F3,0), - KEYCODE(SCANCODE_F1,0), - KEYCODE(SCANCODE_F2,0), - KEYCODE(SCANCODE_F12,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_F10,0), - KEYCODE(SCANCODE_F8,0), - KEYCODE(SCANCODE_F6,0), - KEYCODE(SCANCODE_F4,0), - KEYCODE(SCANCODE_TAB,0), - KEYCODE(SCANCODE_BACKTICK,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_ALT,ALT), - KEYCODE(SCANCODE_SHIFT,SHIFT), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_CTRL,CTRL), - KEYCODE(SCANCODE_Q,0), - KEYCODE(SCANCODE_1,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_Z,0), - KEYCODE(SCANCODE_S,0), - KEYCODE(SCANCODE_A,0), - KEYCODE(SCANCODE_W,0), - KEYCODE(SCANCODE_2,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_C,0), - KEYCODE(SCANCODE_X,0), - KEYCODE(SCANCODE_D,0), - KEYCODE(SCANCODE_E,0), - KEYCODE(SCANCODE_4,0), - KEYCODE(SCANCODE_3,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_SPACE,0), - KEYCODE(SCANCODE_V,0), - KEYCODE(SCANCODE_F,0), - KEYCODE(SCANCODE_T,0), - KEYCODE(SCANCODE_R,0), - KEYCODE(SCANCODE_5,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_N,0), - KEYCODE(SCANCODE_B,0), - KEYCODE(SCANCODE_H,0), - KEYCODE(SCANCODE_G,0), - KEYCODE(SCANCODE_Y,0), - KEYCODE(SCANCODE_6,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_M,0), - KEYCODE(SCANCODE_J,0), - KEYCODE(SCANCODE_U,0), - KEYCODE(SCANCODE_7,0), - KEYCODE(SCANCODE_8,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_COMMA,0), - KEYCODE(SCANCODE_K,0), - KEYCODE(SCANCODE_I,0), - KEYCODE(SCANCODE_O,0), - KEYCODE(SCANCODE_0,0), - KEYCODE(SCANCODE_9,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_DOT,0), - KEYCODE(SCANCODE_FORWARD_SLASH,0), - KEYCODE(SCANCODE_L,0), - KEYCODE(SCANCODE_SEMICOLON,0), - KEYCODE(SCANCODE_P,0), - KEYCODE(SCANCODE_DASH,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_APOSTROPHE,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_SQUARE_BRACKET_LEFT,0), - KEYCODE(SCANCODE_EQUAL,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,CAPS_LOCK), - KEYCODE(SCANCODE_SHIFT,SHIFT), - KEYCODE(SCANCODE_ENTER,0), - KEYCODE(SCANCODE_SQUARE_BRACKET_RIGHT,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_BACKSLASH,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_BACKSPACE,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_1,NUMPAD), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_4,NUMPAD), - KEYCODE(SCANCODE_7,NUMPAD), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_UNKNOWN,0), - KEYCODE(SCANCODE_0,NUMPAD), - KEYCODE(SCANCODE_DOT,NUMPAD), - KEYCODE(SCANCODE_2,NUMPAD), - KEYCODE(SCANCODE_5,NUMPAD), - KEYCODE(SCANCODE_6,NUMPAD), - KEYCODE(SCANCODE_8,NUMPAD), - KEYCODE(SCANCODE_ESC,0), - KEYCODE(SCANCODE_UNKNOWN,NUM_LOCK), - KEYCODE(SCANCODE_F11,0), - KEYCODE(SCANCODE_PLUS,NUMPAD), - KEYCODE(SCANCODE_3,NUMPAD), - KEYCODE(SCANCODE_DASH,NUMPAD), - KEYCODE(SCANCODE_STAR,NUMPAD), - KEYCODE(SCANCODE_9,NUMPAD), - KEYCODE(SCANCODE_UNKNOWN,/*SCROLL_LOCK*/0x00), // scroll lock - KEYCODE(SCANCODE_UNKNOWN, 0x0), - KEYCODE(SCANCODE_UNKNOWN, 0x0), - KEYCODE(SCANCODE_UNKNOWN, 0x0), - KEYCODE(SCANCODE_UNKNOWN, 0x0), - KEYCODE(SCANCODE_F7, 0x0), -}; - -// NOTE: To index, subtract 0x10 from the extended scancode. -static keycode set2_keycode_extended[0x7e - 0x10] = { - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_ALT,ALT), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_CTRL,CTRL), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_SUPER_KEY,SUPER_KEY), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_SUPER_KEY,SUPER_KEY), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_FORWARD_SLASH,NUMPAD), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_ENTER,NUMPAD), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_END,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_LEFT_ARROW,0x00), - KEYCODE(SCANCODE_HOME,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_INSERT,0x00), - KEYCODE(SCANCODE_DELETE,0x00), - KEYCODE(SCANCODE_DOWN_ARROW,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_RIGHT_ARROW,0x00), - KEYCODE(SCANCODE_UP_ARROW,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_PGDOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_UNKNOWN,0x00), - KEYCODE(SCANCODE_PGUP,0x00), -}; \ No newline at end of file diff --git a/src/drivers/generic/r8169/CMakeLists.txt b/src/drivers/generic/r8169/CMakeLists.txt deleted file mode 100644 index 90790e45..00000000 --- a/src/drivers/generic/r8169/CMakeLists.txt +++ /dev/null @@ -1,32 +0,0 @@ -# drivers/generic/r8169/CMakeLists.txt -# -# Copyright (c) 2025 Omar Berrow - -add_executable(r8169 "main.c" "device.c") - -target_compile_options(r8169 - PRIVATE $<$:${TARGET_DRIVER_COMPILE_OPTIONS_C}> - PRIVATE $<$:-ffreestanding> - PRIVATE $<$:-Wall> - PRIVATE $<$:-Wextra> - PRIVATE $<$:-fstack-protector-all> - PRIVATE $<$:-fno-builtin-memset> - PRIVATE $<$:-fvisibility=hidden> - PRIVATE $<$:-fPIC> -) - -set_property(TARGET r8169 PROPERTY link_depends ${DRIVER_LINKER_SCRIPT}) - -target_include_directories(r8169 - PRIVATE "${CMAKE_SOURCE_DIR}/src/oboskrnl" - PRIVATE ${OBOSKRNL_EXTERNAL_INCLUDES}) - -target_link_options(r8169 - PRIVATE "-nostdlib" - PRIVATE "-fPIC" - PRIVATE "-Wl,-shared" -# PRIVATE "-Wl,--allow-shlib-undefined" - PRIVATE "-T" PRIVATE ${DRIVER_LINKER_SCRIPT} - PRIVATE ${TARGET_DRIVER_LINKER_OPTIONS} -) -target_compile_definitions(r8169 PRIVATE OBOS_DRIVER=1) diff --git a/src/drivers/generic/r8169/device.c b/src/drivers/generic/r8169/device.c deleted file mode 100644 index 5d2bb070..00000000 --- a/src/drivers/generic/r8169/device.c +++ /dev/null @@ -1,625 +0,0 @@ -/* - * drivers/generic/r8169/device.c - * - * Copyright (c) 2025 Omar Berrow - */ - -#include -#include -#include -#include - -#include - -#include -#include -#include -#include - -#include -#include - -#include -#include -#include - -#include - -#include - -#include - -#include - -#include "structs.h" - -static void write_reg64(r8169_device* dev, uint8_t off, uint64_t val) -{ - DrvS_WriteIOSpaceBar(dev->bar->bar, off+4, val>>32, 4); - DrvS_WriteIOSpaceBar(dev->bar->bar, off, val&0xffffffff, 4); -} - -static void write_or_register(r8169_device* dev, uint8_t off, uint32_t mask, uint8_t size) -{ - uint32_t tmp = 0; - DrvS_ReadIOSpaceBar(dev->bar->bar, off, &tmp, size); - tmp |= mask; - DrvS_WriteIOSpaceBar(dev->bar->bar, off, tmp, size); -} - -static void dpc_handler(dpc* obj, void* userdata); -static void* map_registers(uintptr_t phys, size_t size, bool uc, bool mmio, bool ref_twice); - -bool r8169_irq_checker(struct irq* i, void* userdata) -{ - OBOS_UNUSED(i); - r8169_device* dev = userdata; - uint32_t isr = 0; - DrvS_ReadIOSpaceBar(dev->bar->bar, IntrStatus, &isr, 2); - return isr != 0; -} - -void r8169_irq_handler(struct irq* i, interrupt_frame* frame, void* userdata, irql oldIrql) -{ - OBOS_UNUSED(i); - OBOS_UNUSED(frame); - OBOS_UNUSED(oldIrql); - - r8169_device* dev = userdata; - uint32_t isr = 0; - DrvS_ReadIOSpaceBar(dev->bar->bar, IntrStatus, &isr, 2); - dev->isr = isr; - DrvS_WriteIOSpaceBar(dev->bar->bar, IntrStatus, isr, 2); - DrvS_ReadIOSpaceBar(dev->bar->bar, IntrStatus, &isr, 2); - - CoreH_InitializeDPC(&dev->dpc, dpc_handler, Core_DefaultThreadAffinity); -} - -void r8169_rx(r8169_device* dev) -{ - OBOS_ENSURE(~dev->isr & RxOverflow); - OBOS_ENSURE(~dev->isr & TxErr); - irql oldIrql = Core_SpinlockAcquire(&dev->rx_buffer_lock); - for (size_t i = 0; i < DESCS_IN_SET; i++) - { - r8169_descriptor* desc = &dev->sets[Rx_Set][i]; - if (desc->command & NIC_OWN) - continue; - - dev->rx_count++; - - if (desc->command & RES_ERR) - { - // Uh oh - dev->rx_errors++; - if (desc->command & CRC_ERR) - dev->rx_crc_errors++; - if (desc->command & (RUNT_ERR|RWT_ERR)) - dev->rx_length_errors++; - goto done; - } - - // TODO: Support fragmented packets. - if ((desc->command & (LS|FS)) != (LS|FS)) - { - dev->rx_dropped++; - dev->rx_length_errors++; - goto done; - } - - size_t packet_len = desc->command & PACKET_LEN_MASK; - - r8169_frame frame = {}; - void* buff = map_registers(desc->buf, packet_len, false, true, false); - r8169_frame_generate(dev, &frame, buff, packet_len, FRAME_PURPOSE_RX /* We're receiving a packet */); - r8169_buffer_add_frame(&dev->rx_buffer, &frame); - // if (memcmp(buff, "OBOS_Shutdown", 13)) - // OBOS_Shutdown(); - Mm_VirtualMemoryFree(&Mm_KernelContext, buff, packet_len); - buff = nullptr; - - dev->rx_bytes += packet_len; - Core_EventPulse(&dev->rx_buffer.envt, false); - - done: - r8169_release_desc(dev, desc, Rx_Set); - } - Core_SpinlockRelease(&dev->rx_buffer_lock, oldIrql); -} - -static void tx_set(r8169_device* dev, uint8_t set) -{ - if (!dev->sets[set]) - return; - irql oldIrql = Core_SpinlockAcquire(&dev->tx_buffer_lock); - for (size_t i = 0; i < DESCS_IN_SET; i++) - { - r8169_descriptor* desc = &dev->sets[set][i]; - if (desc->command & NIC_OWN) - continue; - if (!desc->buf) - continue; - - dev->tx_count++; - - size_t sz = TX_PACKET_SIZE*128; - if (sz % OBOS_PAGE_SIZE) - sz += (OBOS_PAGE_SIZE-(sz%OBOS_PAGE_SIZE)); - for (uintptr_t phys = desc->buf; phys < (desc->buf + sz); phys += OBOS_PAGE_SIZE) - { - page what = {.phys=phys}; - Core_MutexAcquire(&Mm_PhysicalPagesLock); - page* pg = RB_FIND(phys_page_tree, &Mm_PhysicalPages, &what); - Core_MutexRelease(&Mm_PhysicalPagesLock); - MmH_DerefPage(pg); - } - // Mm_FreePhysicalPages(desc->buf, TX_PACKET_SIZE*128); - - Core_EventPulse(&dev->tx_buffer.envt, false); - - r8169_release_desc(dev, desc, set); - // OBOS_Debug("%d: desc->command: %08x\n", i, desc->command); - } - uint32_t tx_poll = 0; - DrvS_ReadIOSpaceBar(dev->bar->bar, TxPoll, &tx_poll, 1); - if (set == TxH_Set && ~tx_poll & BIT(7)) - { - dev->tx_bytes += dev->tx_high_priority_awaiting_transfer; - dev->tx_high_priority_awaiting_transfer = 0; - } - if (set == Tx_Set && ~tx_poll & BIT(6)) - { - dev->tx_bytes += dev->tx_awaiting_transfer; - dev->tx_awaiting_transfer = 0; - } - Core_SpinlockRelease(&dev->tx_buffer_lock, oldIrql); -} -void r8169_tx(r8169_device* dev) -{ - tx_set(dev, Tx_Set); - tx_set(dev, TxH_Set); -} - -static void dpc_handler(dpc* obj, void* userdata) -{ - OBOS_UNUSED(obj); - - r8169_device* dev = userdata; - r8169_rx(dev); - r8169_tx(dev); -} - -void r8169_set_irq_mask(r8169_device* dev, uint16_t mask) -{ - DrvS_WriteIOSpaceBar(dev->bar->bar, IntrMask, mask, 2); -} - -void r8169_read_mac(r8169_device* dev) -{ - memzero(&dev->mac_readable, sizeof(dev->mac_readable)); - - uint32_t tmp = 0; - for (uint8_t i = 0; i < 6; i++) - { - DrvS_ReadIOSpaceBar(dev->bar->bar, MAC0+i, &tmp, 1); - dev->mac[i] = tmp & 0xff; - } - - snprintf(dev->mac_readable, 19, "%02x:%02x:%02x:%02x:%02x:%02x", dev->mac[0], dev->mac[1], dev->mac[2], dev->mac[3], dev->mac[4], dev->mac[5]); - OBOS_Debug("RTL8169: %02x:%02x:%02x: MAC Address is %s\n", - dev->dev->location.bus,dev->dev->location.slot,dev->dev->location.function, - dev->mac_readable - ); -} - -void r8169_hw_reset(r8169_device* dev) -{ - DrvS_WriteIOSpaceBar(dev->bar->bar, ChipCmd, BIT(4) /* Reset */, 1); - uint32_t tmp = 0; - do { - DrvS_ReadIOSpaceBar(dev->bar->bar, ChipCmd, &tmp, 1); - } while(tmp & BIT(4)); -} - -void r8169_init_rxcfg(r8169_device *dev) -{ - DrvS_WriteIOSpaceBar(dev->bar->bar, RxConfig, 0xe700, 4); -} - -void r8169_set_rxcfg_mode(r8169_device *dev) -{ - uint64_t mc_filter = 0xffffffffffffffff; - write_reg64(dev, MAR0, mc_filter); - // AcceptBroadcast | AcceptMyPhys | AcceptMulticast - write_or_register(dev, RxConfig, 0b1110, 4); -} - -void r8169_set_txcfg(r8169_device *dev) -{ - // TODO: Auto FIFO enable? - DrvS_WriteIOSpaceBar(dev->bar->bar, TxConfig, 0x0700|0x3000000, 4); -} - -void r8169_lock_config(r8169_device* dev) -{ - DrvS_WriteIOSpaceBar(dev->bar->bar, Cfg9346, Cfg9346_Lock, 1); -} - -void r8169_unlock_config(r8169_device* dev) -{ - DrvS_WriteIOSpaceBar(dev->bar->bar, Cfg9346, Cfg9346_Unlock, 1); -} - -static uint16_t read_phy(r8169_device* dev, uint8_t offset) -{ - offset &= ~0x1f; - uint32_t phyar_value = BIT(31); - phyar_value |= (offset<<16); - DrvS_WriteIOSpaceBar(dev->bar->bar, PhyAr, phyar_value, 4); - do { - DrvS_ReadIOSpaceBar(dev->bar->bar, PhyAr, &phyar_value, 4); - } while(phyar_value & BIT(31)); - return phyar_value & 0xffff; -} - -static void write_phy(r8169_device* dev, uint8_t offset, uint16_t data) -{ - offset &= ~0x1f; - uint32_t phyar_value = 0; - phyar_value |= (offset<<16); - phyar_value |= (data&0xffff); - DrvS_WriteIOSpaceBar(dev->bar->bar, PhyAr, phyar_value, 4); - do { - DrvS_ReadIOSpaceBar(dev->bar->bar, PhyAr, &phyar_value, 4); - } while(~phyar_value & BIT(31)); -} - -void r8169_save_phy(r8169_device* dev) -{ - for (uint8_t offset = 0; offset < 0x20; offset++) - dev->saved_phy_state[offset] = read_phy(dev, offset); -} - -void r8169_resume_phy(r8169_device* dev) -{ - for (uint8_t offset = 0; offset < 0x20; offset++) - write_phy(dev, offset, dev->saved_phy_state[offset]); -} - -void r8169_reset(r8169_device* dev) -{ - if (!dev->suspended) - { - // This is a clean reinit, so we must initialize the IRQ object and DPC. - - Core_IrqObjectInitializeIRQL(&dev->irq, IRQL_R8169, true, true); - dev->irq_res->irq->irq = &dev->irq; - dev->irq_res->irq->masked = false; - Drv_PCISetResource(dev->irq_res); - dev->irq_res->irq->irq->irqChecker = r8169_irq_checker; - dev->irq_res->irq->irq->handler = r8169_irq_handler; - dev->irq_res->irq->irq->irqCheckerUserdata = dev; - dev->irq_res->irq->irq->handlerUserdata = dev; - - dev->dpc.userdata = dev; - - dev->rx_buffer.envt = EVENT_INITIALIZE(EVENT_NOTIFICATION); - dev->rx_buffer_lock = Core_SpinlockCreate(); - dev->tx_buffer_lock = Core_SpinlockCreate(); - - dev->magic = R8169_DEVICE_MAGIC; - - dev->interface_name = DrvH_MakePCIDeviceName(dev->dev->location, ETHERNET_DEVICE_PREFIX); - - dev->dev->resource_cmd_register->cmd_register |= 0x3; // io space + memspace - Drv_PCISetResource(dev->dev->resource_cmd_register); - - r8169_alloc_set(dev, Rx_Set); - r8169_alloc_set(dev, Tx_Set); - // TODO: Test high-priority TX packets. - r8169_alloc_set(dev, TxH_Set); - } - - r8169_init_rxcfg(dev); - - DrvS_WriteIOSpaceBar(dev->bar->bar, IntrStatus, 0xffffffff, 2); - DrvS_WriteIOSpaceBar(dev->bar->bar, IntrMask, 0x0, 2); - - r8169_hw_reset(dev); - r8169_read_mac(dev); - - r8169_unlock_config(dev); - - DrvS_WriteIOSpaceBar(dev->bar->bar, MaxTxPacketSize, TX_PACKET_SIZE, 2); - - write_or_register(dev, CPlusCmd, BIT(3) /*PCIMulRW*/, 2); - - // Linux sets a "magic register" here - // TODO: Should we do that too? - - DrvS_WriteIOSpaceBar(dev->bar->bar, IntrMitigate, 0x0, 2); - - DrvS_WriteIOSpaceBar(dev->bar->bar, RxMaxSize, RX_PACKET_SIZE, 4); - - write_reg64(dev, RxDescAddrLow, dev->sets_phys[Rx_Set]); - write_reg64(dev, TxDescStartAddrLow, dev->sets_phys[Tx_Set]); - write_reg64(dev, TxHDescStartAddrLow, dev->sets_phys[TxH_Set]); - - r8169_lock_config(dev); - - DrvS_WriteIOSpaceBar(dev->bar->bar, ChipCmd, TX_ENABLE|RX_ENABLE, 1); - r8169_init_rxcfg(dev); - r8169_set_txcfg(dev); - r8169_set_rxcfg_mode(dev); - DrvS_WriteIOSpaceBar(dev->bar->bar, IntrMask, ENABLED_IRQS, 2); - DrvS_WriteIOSpaceBar(dev->bar->bar, TimerInt, 0, 2); -} - -static void* map_registers(uintptr_t phys, size_t size, bool uc, bool mmio, bool ref_twice) -{ - size_t phys_page_offset = (phys % OBOS_PAGE_SIZE); - phys -= phys_page_offset; - size = size + (OBOS_PAGE_SIZE - (size % OBOS_PAGE_SIZE)); - size += phys_page_offset; - void* virt = Mm_VirtualMemoryAlloc( - &Mm_KernelContext, - nullptr, size, - uc ? OBOS_PROTECTION_CACHE_DISABLE : 0, VMA_FLAGS_NON_PAGED, - nullptr, - nullptr); - for (uintptr_t offset = 0; offset < size; offset += OBOS_PAGE_SIZE) - { - page_info page = {.virt=offset+(uintptr_t)virt}; - MmS_QueryPageInfo(Mm_KernelContext.pt, page.virt, &page, nullptr); - do { - struct page what = {.phys=page.phys}; - Core_MutexAcquire(&Mm_PhysicalPagesLock); - struct page* pg = RB_FIND(phys_page_tree, &Mm_PhysicalPages, &what); - Core_MutexRelease(&Mm_PhysicalPagesLock); - MmH_DerefPage(pg); - } while(0); - page.prot.uc = uc; - page.phys = phys+offset; - struct page* pg = MmH_AllocatePage(page.phys, false); - if (mmio) - pg->flags |= PHYS_PAGE_MMIO; - if (ref_twice) - MmH_RefPage(pg); - pg->pagedCount++; - MmS_SetPageMapping(Mm_KernelContext.pt, &page, phys + offset, false); - } - Drv_TLBShootdown(Mm_KernelContext.pt, (uintptr_t)virt, size); - return virt+phys_page_offset; -} - -void r8169_alloc_set(r8169_device* dev, uint8_t set) -{ - OBOS_ENSURE(set <= 2); - if (dev->sets[set]) - { - printf("info before assert: set=%d, dev->sets[%d]=0x%p", set, dev->sets[set]); - OBOS_ASSERT(dev->sets[set] && "RTL8169: Attempt to reallocate set %d denied."); - return; - } - - OBOS_STATIC_ASSERT(DESCS_IN_SET <= MAX_DESCS_IN_SET, "DESCS_IN_SET is greater than MAX_DESCS_IN_SET"); - - size_t nPages = DESCS_IN_SET*sizeof(r8169_descriptor); - if (nPages % OBOS_PAGE_SIZE) - nPages += (OBOS_PAGE_SIZE-(nPages%OBOS_PAGE_SIZE)); - nPages /= OBOS_PAGE_SIZE; - -#if OBOS_PAGE_SIZE >= 256 - const size_t alignment = 1; -#else - size_t alignment = 256; - if (alignment % OBOS_PAGE_SIZE) - alignment += (OBOS_PAGE_SIZE-(alignment%OBOS_PAGE_SIZE)); - alignment /= OBOS_PAGE_SIZE; -#endif - - uintptr_t phys = Mm_AllocatePhysicalPages(nPages, alignment, nullptr); - - // TODO: Do any platforms require this to be mapped as UC? - dev->sets[set] = map_registers(phys, nPages*OBOS_PAGE_SIZE, false, true, false); - dev->sets_phys[set] = phys; - memzero(dev->sets[set], nPages*OBOS_PAGE_SIZE); - - phys = 0; - - for (size_t i = 0; i < DESCS_IN_SET && (set == Rx_Set); i++) - { - dev->sets[set][i].vlan = 0; // we don't use this, so we zero it. - dev->sets[set][i].command = (RX_PACKET_SIZE & PACKET_LEN_MASK) & ~0x7; - - nPages = RX_PACKET_SIZE; - if (nPages % OBOS_PAGE_SIZE) - nPages += (OBOS_PAGE_SIZE-(nPages%OBOS_PAGE_SIZE)); - nPages /= OBOS_PAGE_SIZE; - - phys = Mm_AllocatePhysicalPages(nPages, 1, nullptr); - dev->sets[set][i].buf = phys; - - r8169_release_desc(dev, &dev->sets[set][i], set); - } - dev->sets[set][DESCS_IN_SET - 1].command |= EOR; -} - -void r8169_release_desc(r8169_device* dev, r8169_descriptor* desc, uint8_t set) -{ - OBOS_UNUSED(dev); - if (set == Rx_Set) - { - desc->command |= NIC_OWN; - return; // We're done here. - } - - desc->command &= EOR; - desc->buf = 0; - desc->vlan = 0; -} - -r8169_descriptor* r8169_alloc_desc(r8169_device* dev, uint8_t set) -{ - if (set == Rx_Set) - return nullptr; // We're done here. - return &dev->sets[set][(*(set == TxH_Set ? &dev->tx_priority_idx : &dev->tx_idx))++ % DESCS_IN_SET]; -} - -obos_status r8169_tx_queue_flush(r8169_device* dev, bool wait) -{ - OBOS_UNUSED(wait); - - irql oldIrql = Core_SpinlockAcquireExplicit(&dev->tx_buffer_lock, IRQL_R8169, false); - - r8169_frame* tx_frame = LIST_GET_HEAD(r8169_frame_list, &dev->tx_buffer.frames); - - // TODO: Do we need to synchronize this? - uint8_t tx_poll = 0x00; - while (tx_frame) - { - r8169_frame* next = LIST_GET_NEXT(r8169_frame_list, &dev->tx_buffer.frames, tx_frame); - - r8169_descriptor* desc = r8169_alloc_desc(dev, tx_frame->tx_priority_high ? TxH_Set : Tx_Set); - OBOS_ENSURE(desc); - - uintptr_t phys = 0; - MmS_QueryPageInfo(Mm_KernelContext.pt, (uintptr_t)tx_frame->buf, nullptr, &phys); - OBOS_ENSURE(phys); - desc->buf = phys; - desc->command = (desc->command & EOR) | (tx_frame->sz & TX_PACKET_LEN_MASK); - desc->command |= FS; - desc->command |= LS; - if (dev->ip_checksum_offload) - desc->command |= IPCS; - if (dev->udp_checksum_offload) - desc->command |= UDPCS; - if (dev->tcp_checksum_offload) - desc->command |= TCPCS; - desc->command |= NIC_OWN; - if (tx_frame->tx_priority_high) - { - tx_poll |= BIT(7); // High priority frame. - dev->tx_high_priority_awaiting_transfer += tx_frame->sz; - } - else - { - tx_poll |= BIT(6); // Normal priority frame. - dev->tx_awaiting_transfer += tx_frame->sz; - } - Mm_VirtualMemoryFree(&Mm_KernelContext, tx_frame->buf, ((TX_PACKET_SIZE*128) + (OBOS_PAGE_SIZE-((TX_PACKET_SIZE*128)%OBOS_PAGE_SIZE)))); - tx_frame->buf = nullptr; - - r8169_buffer_remove_frame(&dev->tx_buffer, tx_frame); - - tx_frame = next; - } - DrvS_WriteIOSpaceBar(dev->bar->bar, TxPoll, tx_poll, 1); - - Core_SpinlockRelease(&dev->tx_buffer_lock, oldIrql); - return OBOS_STATUS_SUCCESS; -} - -obos_status r8169_frame_tx_high_priority(r8169_frame* frame, bool priority) -{ - frame->tx_priority_high = !!priority; - return OBOS_STATUS_SUCCESS; -} - -LIST_GENERATE(r8169_frame_list, r8169_frame, node); -LIST_GENERATE(r8169_descriptor_list, r8169_descriptor_node, node); - -obos_status r8169_frame_generate(r8169_device* dev, r8169_frame* frame, const void* data, size_t sz_, uint32_t purpose) -{ - const size_t sz = sz_; - if (!dev->refcount) - { - dev->rx_dropped++; - return OBOS_STATUS_SUCCESS; - } - switch (purpose) - { - case FRAME_PURPOSE_GENERAL: break; - case FRAME_PURPOSE_RX: - { - if (obos_expect(sz > RX_PACKET_SIZE, false)) - { - dev->rx_dropped++; - return OBOS_STATUS_INVALID_ARGUMENT; - } - frame->refcount = dev->refcount; - frame->idx = dev->rx_count; - break; - } - case FRAME_PURPOSE_TX: - { - if (obos_expect(sz > (TX_PACKET_SIZE*128), false)) - { - dev->tx_dropped++; - return OBOS_STATUS_INVALID_ARGUMENT; - } - frame->refcount = 1; - frame->idx = dev->tx_count; - break; - } - default: - return OBOS_STATUS_INVALID_ARGUMENT; - } - if (purpose == FRAME_PURPOSE_RX) - frame->buf = OBOS_KernelAllocator->Allocate(OBOS_KernelAllocator, sz, nullptr); - else - frame->buf = map_registers(Mm_AllocatePhysicalPages(((TX_PACKET_SIZE*128) + (OBOS_PAGE_SIZE-((TX_PACKET_SIZE*128)%OBOS_PAGE_SIZE))) / OBOS_PAGE_SIZE, 1, nullptr), ((TX_PACKET_SIZE*128) + (OBOS_PAGE_SIZE-((TX_PACKET_SIZE*128)%OBOS_PAGE_SIZE))), false, true, true); - memcpy(frame->buf, data, sz); - frame->sz = sz; - frame->purpose = purpose; - return OBOS_STATUS_SUCCESS; -} - -obos_status r8169_buffer_add_frame(r8169_buffer* buff, r8169_frame* frame) -{ - if (!frame->buf) - return OBOS_STATUS_INVALID_ARGUMENT; - r8169_frame* new_frame = OBOS_NonPagedPoolAllocator->ZeroAllocate(OBOS_NonPagedPoolAllocator, 1, sizeof(*frame), nullptr); - *new_frame = *frame; - LIST_APPEND(r8169_frame_list, &buff->frames, new_frame); - return OBOS_STATUS_SUCCESS; -} - -obos_status r8169_buffer_remove_frame(r8169_buffer* buff, r8169_frame* frame) -{ - // printf("refcount of %p is now %d\n", frame, frame->refcount - 1); - if (!(--frame->refcount)) - { - LIST_REMOVE(r8169_frame_list, &buff->frames, frame); - if (frame->purpose == FRAME_PURPOSE_RX) - OBOS_KernelAllocator->Free(OBOS_KernelAllocator, frame->buf, frame->sz); - OBOS_NonPagedPoolAllocator->Free(OBOS_NonPagedPoolAllocator, frame, sizeof(*frame)); - } - return OBOS_STATUS_SUCCESS; -} - -obos_status r8169_buffer_read_next_frame(r8169_buffer* buff, r8169_frame** frame) -{ - if (!(*frame)) - *frame = LIST_GET_TAIL(r8169_frame_list, &buff->frames); - else - *frame = LIST_GET_NEXT(r8169_frame_list, &buff->frames, *frame); - return OBOS_STATUS_SUCCESS; - -} - -obos_status r8169_buffer_poll(r8169_buffer* buff) -{ - if (LIST_GET_NODE_COUNT(r8169_frame_list, &buff->frames)) - return OBOS_STATUS_SUCCESS; - while (!buff->envt.hdr.signaled) - OBOSS_SpinlockHint(); - return OBOS_STATUS_SUCCESS; -} - -obos_status r8169_buffer_block(r8169_buffer* buff) -{ - if (LIST_GET_NODE_COUNT(r8169_frame_list, &buff->frames)) - return OBOS_STATUS_SUCCESS; - return Core_WaitOnObject(WAITABLE_OBJECT(buff->envt)); -} diff --git a/src/drivers/generic/r8169/main.c b/src/drivers/generic/r8169/main.c deleted file mode 100644 index 796b5c0f..00000000 --- a/src/drivers/generic/r8169/main.c +++ /dev/null @@ -1,563 +0,0 @@ -/* - * drivers/generic/r8169/main.c - * - * Copyright (c) 2025-2026 Omar Berrow - */ - -#include -#include -#include -#include -#include - -#include -#include -#include - -#include - -#include -#include -#include -#include - -#include -#include -#include - -#include -#include - -#include -#include - -#include -#include - -#include "structs.h" - -r8169_device* Devices; -size_t nDevices; -driver_id* this_driver; - -void on_wake() -{ - for (size_t i = 0; i < nDevices; i++) - { - r8169_reset(&Devices[i]); - r8169_resume_phy(&Devices[i]); - Devices[i].suspended = false; - } -} - -void on_suspend() -{ - for (size_t i = 0; i < nDevices; i++) - { - r8169_save_phy(&Devices[i]); - Devices[i].suspended = true; - } -} - -obos_status get_blk_size(dev_desc desc, size_t* blkSize) -{ - OBOS_UNUSED(desc); - if (!blkSize) - return OBOS_STATUS_INVALID_ARGUMENT; - *blkSize = 1; - return OBOS_STATUS_SUCCESS; -} - -obos_status get_max_blk_count(dev_desc desc, size_t* count) -{ - OBOS_UNUSED(desc); - OBOS_UNUSED(count); - return OBOS_STATUS_INVALID_OPERATION; -} - -obos_status write_sync(dev_desc desc, const void* buf, size_t blkCount, size_t blkOffset, size_t* nBlkWritten) -{ - OBOS_UNUSED(blkOffset); - if (!desc) - return OBOS_STATUS_INVALID_ARGUMENT; - uint32_t* magic = (void*)desc; - if (*magic != R8169_HANDLE_MAGIC) - return OBOS_STATUS_INVALID_ARGUMENT; - if (blkCount > (TX_PACKET_SIZE*128)) - return OBOS_STATUS_INVALID_ARGUMENT; - r8169_device_handle* hnd = (void*)desc; - r8169_device* dev = hnd->dev; - - r8169_frame frame = {}; - r8169_frame_generate(dev, &frame, buf, blkCount, FRAME_PURPOSE_TX); - r8169_buffer_add_frame(&dev->tx_buffer, &frame); - r8169_tx_queue_flush(dev, true); - - if (nBlkWritten) - *nBlkWritten = blkCount; - - return OBOS_STATUS_SUCCESS; -} - -obos_status read_sync(dev_desc desc, void* buf, size_t blkCount, size_t blkOffset, size_t* nBlkRead) -{ - OBOS_UNUSED(blkOffset); - if (!desc) - return OBOS_STATUS_INVALID_ARGUMENT; - uint32_t* magic = (void*)desc; - OBOS_ENSURE(*magic == R8169_HANDLE_MAGIC); - if (*magic != R8169_HANDLE_MAGIC) - return OBOS_STATUS_INVALID_ARGUMENT; - if (blkCount > RX_PACKET_SIZE) - return OBOS_STATUS_INVALID_ARGUMENT; - if (Core_GetIrql() > IRQL_DISPATCH) - return OBOS_STATUS_INVALID_IRQL; - r8169_device_handle* hnd = (void*)desc; - r8169_device* dev = hnd->dev; - - // Wait for the buffer to receive a frame. - r8169_buffer_block(&dev->rx_buffer); - - irql oldIrql = Core_SpinlockAcquireExplicit(&dev->rx_buffer_lock, IRQL_R8169, false); - - // printf("rx_curr: %p, rx_off: %d\n", hnd->rx_curr, hnd->rx_off); - - if (!hnd->rx_curr) - r8169_buffer_read_next_frame(&dev->rx_buffer, &hnd->rx_curr); - - // printf("rx_curr: %p, rx_off: %d\n", hnd->rx_curr, hnd->rx_off); - - if (!hnd->rx_curr) - { - if (nBlkRead) - *nBlkRead = 0; - Core_SpinlockRelease(&dev->rx_buffer_lock, oldIrql); - return OBOS_STATUS_SUCCESS; - } - - size_t szRead = OBOS_MIN(blkCount, hnd->rx_curr->sz - hnd->rx_off); - Core_SpinlockRelease(&dev->rx_buffer_lock, oldIrql); - - // Lower the IRQL temporarily. - // This is because there is a chance of a page fault here, which is invalid at IRQL > IRQL_DISPATCH - memcpy(buf, hnd->rx_curr->buf + hnd->rx_off, szRead); - hnd->rx_off += szRead; - - // printf("rx_curr: %p, rx_off: %d\n", hnd->rx_curr, hnd->rx_off); - - if (hnd->rx_off >= hnd->rx_curr->sz) - { - // printf(__FILE__ ":%d\n", __LINE__); - oldIrql = Core_SpinlockAcquireExplicit(&dev->rx_buffer_lock, IRQL_R8169, false); - r8169_frame* next = LIST_GET_NEXT(r8169_frame_list, &dev->rx_buffer.frames, hnd->rx_curr); - r8169_buffer_remove_frame(&dev->rx_buffer, hnd->rx_curr); - hnd->rx_curr = next; - hnd->rx_off = 0; - Core_SpinlockRelease(&dev->rx_buffer_lock, oldIrql); - } - - // printf("rx_curr: %p, rx_off: %d\n", hnd->rx_curr, hnd->rx_off); - - if (nBlkRead) - *nBlkRead = szRead; - - return OBOS_STATUS_SUCCESS; -} - -OBOS_WEAK obos_status foreach_device(iterate_decision(*cb)(dev_desc desc, size_t blkSize, size_t blkCount, void* u), void* u); - -obos_status reference_interface(dev_desc *desc) -{ - if (!desc) - return OBOS_STATUS_INVALID_ARGUMENT; - r8169_device* dev = (void*)(*desc); - if (dev->magic != R8169_DEVICE_MAGIC && dev->magic != R8169_HANDLE_MAGIC) - return OBOS_STATUS_INVALID_ARGUMENT; - if (dev->magic == R8169_HANDLE_MAGIC) - dev = ((r8169_device_handle*)(*desc))->dev; - - r8169_device_handle* hnd = OBOS_KernelAllocator->ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(r8169_device_handle), nullptr); - *desc = (uintptr_t)hnd; - hnd->magic = R8169_HANDLE_MAGIC; - - dev->refcount++; - - hnd->dev = dev; - - irql oldIrql = Core_SpinlockAcquireExplicit(&dev->rx_buffer_lock, IRQL_R8169, false); - hnd->rx_curr = LIST_GET_TAIL(r8169_frame_list, &dev->rx_buffer.frames); - if (hnd->rx_curr) - hnd->rx_curr->refcount++; - Core_SpinlockRelease(&dev->rx_buffer_lock, oldIrql); - hnd->rx_off = 0; - - return OBOS_STATUS_SUCCESS; -} - -obos_status unreference_interface(dev_desc desc) -{ - if (!desc) - return OBOS_STATUS_INVALID_ARGUMENT; - r8169_device_handle* hnd = (void*)desc; - if (hnd->magic != R8169_HANDLE_MAGIC) - return OBOS_STATUS_INVALID_ARGUMENT; - irql oldIrql = Core_SpinlockAcquireExplicit(&hnd->dev->rx_buffer_lock, IRQL_R8169, false); - for (r8169_frame* frame = hnd->rx_curr; frame; ) - { - r8169_frame* next = LIST_GET_NEXT(r8169_frame_list, &hnd->dev->rx_buffer.frames, frame); - r8169_buffer_remove_frame(&hnd->dev->rx_buffer, frame); - frame = next; - } - hnd->dev->refcount--; - Core_SpinlockRelease(&hnd->dev->rx_buffer_lock, oldIrql); - return OBOS_STATUS_SUCCESS; -} - -obos_status query_user_readable_name(dev_desc what, const char** name) -{ - if (!what) - return OBOS_STATUS_INVALID_ARGUMENT; - r8169_device_handle* hnd = (void*)what; - if (hnd->magic != R8169_HANDLE_MAGIC) - return OBOS_STATUS_INVALID_ARGUMENT; - *name = hnd->dev->interface_name; - return OBOS_STATUS_SUCCESS; -} - -obos_status ioctl_argp_size(uint32_t request, size_t *ret) -{ - switch (request) { - case IOCTL_IFACE_MAC_REQUEST: - *ret = sizeof(mac_address); - return OBOS_STATUS_SUCCESS; - default: - return Net_InterfaceIoctlArgpSize(request, ret); - } - return OBOS_STATUS_INVALID_IOCTL; -} -obos_status ioctl(dev_desc what, uint32_t request, void* argp) -{ - if (!what) return OBOS_STATUS_INVALID_ARGUMENT; - r8169_device_handle* hnd = (void*)what; - r8169_device* dev = nullptr; - if (hnd->magic == R8169_DEVICE_MAGIC) - dev = (void*)what; - else if (hnd->magic == R8169_HANDLE_MAGIC) - dev = hnd->dev; - else - return OBOS_STATUS_INVALID_ARGUMENT; - switch (request) { - case IOCTL_IFACE_MAC_REQUEST: - { - obos_status status = OBOS_CapabilityCheck("net/mac-query", true); - if (obos_is_error(status)) - break; - memcpy(argp, dev->mac, sizeof(mac_address)); - return OBOS_STATUS_SUCCESS; - } - default: - return Net_InterfaceIoctl(dev->vn, request, argp); - } - return OBOS_STATUS_INVALID_IOCTL; -} - -void irp_on_rx_event_set(irp* req) -{ - r8169_device_handle* hnd = (void*)req->desc; - r8169_device* dev = hnd->dev; - if (!hnd->rx_curr) - r8169_buffer_read_next_frame(&dev->rx_buffer, &hnd->rx_curr); - if (!hnd->rx_curr) - { - req->status = OBOS_STATUS_IRP_RETRY; - return; - } - req->status = OBOS_STATUS_SUCCESS; - if (req->dryOp) - { - req->nBlkRead = hnd->rx_curr->sz; - return; - } - - size_t szRead = OBOS_MIN(req->blkCount, hnd->rx_curr->sz - hnd->rx_off); - memcpy(req->buff, hnd->rx_curr->buf + hnd->rx_off, szRead); - hnd->rx_off += szRead; - if (hnd->rx_off >= hnd->rx_curr->sz) - { - // printf(__FILE__ ":%d\n", __LINE__); - irql oldIrql = Core_SpinlockAcquireExplicit(&dev->rx_buffer_lock, IRQL_R8169, false); - r8169_frame* next = LIST_GET_NEXT(r8169_frame_list, &dev->rx_buffer.frames, hnd->rx_curr); - r8169_buffer_remove_frame(&dev->rx_buffer, hnd->rx_curr); - hnd->rx_curr = next; - hnd->rx_off = 0; - Core_SpinlockRelease(&dev->rx_buffer_lock, oldIrql); - } - - req->nBlkRead = szRead; -} - -obos_status finalize_irp(void* req_) -{ - irp* req = req_; - if (req->evnt) - Core_EventClear(req->evnt); - return OBOS_STATUS_SUCCESS; -} -obos_status submit_irp(void* req_) -{ - irp* req = req_; - if (!req) - return OBOS_STATUS_INVALID_ARGUMENT; - - uint32_t* magic = (void*)req->desc; - OBOS_ENSURE(*magic == R8169_HANDLE_MAGIC); - if (*magic != R8169_HANDLE_MAGIC) - return OBOS_STATUS_INVALID_ARGUMENT; - if (req->blkCount > RX_PACKET_SIZE) - return OBOS_STATUS_INVALID_ARGUMENT; - if (Core_GetIrql() > IRQL_DISPATCH) - return OBOS_STATUS_INVALID_IRQL; - if (!req->buff && !req->dryOp) - { - req->status = OBOS_STATUS_INVALID_ARGUMENT; - return OBOS_STATUS_SUCCESS; - } - r8169_device_handle* hnd = (void*)req->desc; - r8169_device* dev = hnd->dev; - - if (req->op == IRP_READ) - { - if (!hnd->rx_curr) - r8169_buffer_read_next_frame(&dev->rx_buffer, &hnd->rx_curr); - if (!hnd->rx_curr) - { - req->evnt = &dev->rx_buffer.envt; - req->on_event_set = irp_on_rx_event_set; - } - else - irp_on_rx_event_set(req); - } - else if (!req->dryOp) - { - r8169_frame frame = {}; - /* - "Padding: The RTL8169S/RTL8110S will automatically pad any packets less than 64 bytes (including 4 bytes CRC) to 64-byte - long (including 4-byte CRC) before transmitting that packet onto network medium. The padded data are all 0x00" - Because of this, we need to pad the packets ourselves so that the FCS doesn't appear as 0x00 for the peer. - */ - if (req->blkCount < 60) - { - uint8_t *copied_buffer = ZeroAllocate(OBOS_NonPagedPoolAllocator, 64, sizeof(char), nullptr); - memcpy(copied_buffer, req->cbuff, req->blkCount-4); - *(uint32_t*)(copied_buffer+60) = NetH_CRC32Bytes(copied_buffer, 60); - r8169_frame_generate(dev, &frame, copied_buffer, 64, FRAME_PURPOSE_TX); - r8169_buffer_add_frame(&dev->tx_buffer, &frame); - r8169_tx_queue_flush(dev, true); - Free(OBOS_NonPagedPoolAllocator, copied_buffer, 64); - } - else - { - r8169_frame_generate(dev, &frame, req->cbuff, req->blkCount, FRAME_PURPOSE_TX); - r8169_buffer_add_frame(&dev->tx_buffer, &frame); - r8169_tx_queue_flush(dev, true); - } - req->evnt = nullptr; - } - return OBOS_STATUS_SUCCESS; -} - -// TODO: driver cleanup -void driver_cleanup_callback() {} - -__attribute__((section(OBOS_DRIVER_HEADER_SECTION))) driver_header drv_hdr = { - .magic = OBOS_DRIVER_MAGIC, - .flags = DRIVER_HEADER_HAS_STANDARD_INTERFACES | - DRIVER_HEADER_FLAGS_DETECT_VIA_PCI | - DRIVER_HEADER_HAS_VERSION_FIELD | - DRIVER_HEADER_PCI_HAS_VENDOR_ID | - DRIVER_HEADER_PCI_IGNORE_PROG_IF | - DRIVER_HEADER_PIPE_STYLE_DEVICE, - .acpiId.nPnpIds = 0, - .pciId.indiv = { - .classCode = 0x02 , // Network Controller - .subClass = 0x00 , // Ethernet Controller - .progIf = 0x00 , // Ignored - .vendorId = 0x10ec, // Realtek Semiconductor. Co., Ltd. - // Verify device IDs at runtime. - }, - .ftable = { - .driver_cleanup_callback = driver_cleanup_callback, - .ioctl = ioctl, - .ioctl_argp_size = ioctl_argp_size, - .get_blk_size = get_blk_size, - .get_max_blk_count = get_max_blk_count, - .query_user_readable_name = query_user_readable_name, - .foreach_device = foreach_device, - .reference_device = reference_interface, - .unreference_device = unreference_interface, - .read_sync = read_sync, - .write_sync = write_sync, - .on_wake = on_wake, - .on_suspend = on_suspend, - .submit_irp = submit_irp, - .finalize_irp = finalize_irp, - }, - .driverName = "RTL8169 Driver", - .version = 1, - .uacpi_init_level_required = PCI_IRQ_UACPI_INIT_LEVEL -}; - -static uint16_t device_ids[] = { - 0x8161, - 0x8168, - 0x8169, - 0x8136, - 0x8125, -}; - -static void search_bus(pci_bus* bus) -{ - for (pci_device* dev = LIST_GET_HEAD(pci_device_list, &bus->devices); dev; ) - { - if ((dev->hid.id & 0xffffffff) == (drv_hdr.pciId.id & 0xffffffff)) - { - // Compare Device IDs. - bool found = false; - for (size_t i = 0; i < sizeof(device_ids)/sizeof(device_ids[0]); i++) - { - if (dev->hid.indiv.deviceId == device_ids[i]) - { - found = true; - break; - } - } - if (!found) - { - dev = LIST_GET_NEXT(pci_device_list, &bus->devices, dev); - continue; - } - - pci_resource* bar0 = nullptr; - pci_resource* irq_res = nullptr; - for (pci_resource* res = LIST_GET_HEAD(pci_resource_list, &dev->resources); res; ) - { - if (res->type == PCI_RESOURCE_BAR && res->bar->idx == 0) - bar0 = res; - if (res->type == PCI_RESOURCE_IRQ) - irq_res = res; - - if (bar0 && irq_res) - break; - - res = LIST_GET_NEXT(pci_resource_list, &dev->resources, res); - } - - if (!bar0 || !irq_res) - { - OBOS_Warning("%02x:%02x:%02x: Bogus RTL8169 PCI node.", dev->location.bus, dev->location.slot, dev->location.function); - dev = LIST_GET_NEXT(pci_device_list, &bus->devices, dev); - continue; - } - - nDevices++; - Devices = OBOS_NonPagedPoolAllocator->Reallocate(OBOS_NonPagedPoolAllocator, Devices, nDevices*sizeof(r8169_device), (nDevices-1)*sizeof(r8169_device), nullptr); - memzero(&Devices[nDevices-1], sizeof(Devices[nDevices-1])); - Devices[nDevices-1].dev = dev; - Devices[nDevices-1].bar = bar0; - Devices[nDevices-1].irq_res = irq_res; - Devices[nDevices-1].idx = nDevices-1; - - Devices[nDevices-1].tx_count = 0; - Devices[nDevices-1].tx_bytes = 0; - Devices[nDevices-1].tx_dropped = 0; - Devices[nDevices-1].tx_high_priority_awaiting_transfer = 0; - Devices[nDevices-1].tx_awaiting_transfer = 0; - - Devices[nDevices-1].rx_bytes = 0; - Devices[nDevices-1].rx_count = 0; - Devices[nDevices-1].rx_crc_errors = 0; - Devices[nDevices-1].rx_errors = 0; - Devices[nDevices-1].rx_length_errors = 0; - Devices[nDevices-1].rx_dropped = 0; - - Devices[nDevices-1].isr = 0; - - Devices[nDevices-1].suspended = false; - } - - dev = LIST_GET_NEXT(pci_device_list, &bus->devices, dev); - } -} - -OBOS_NO_KASAN void hexdump(void* _buff, size_t nBytes, const size_t width); - -OBOS_PAGEABLE_FUNCTION driver_init_status OBOS_DriverEntry(driver_id* this) -{ - this_driver = this; - for (size_t i = 0; i < Drv_PCIBusCount; i++) - search_bus(&Drv_PCIBuses[i]); - if (!nDevices) - return (driver_init_status){.status=OBOS_STATUS_NOT_FOUND,.fatal=true,.context="Could not find PCI Devices."}; - - // Reset the devices. - for (size_t i = 0; i < nDevices; i++) - { - r8169_reset(Devices+i); - OBOS_ENSURE(this_driver); - vnode* vn = Drv_AllocateVNode(this_driver, (uintptr_t)&Devices[i], 0, nullptr, VNODE_TYPE_CHR); - const char* dev_name = Devices[i].interface_name; - OBOS_Debug("%*s: Registering r8169 NIC card at %s%c%s\n", strnlen(this_driver->header.driverName, 64), this_driver->header.driverName, OBOS_DEV_PREFIX, OBOS_DEV_PREFIX[sizeof(OBOS_DEV_PREFIX)-1] == '/' ? 0 : '/', dev_name); - Drv_RegisterVNode(vn, dev_name); - Devices[0].vn = vn; - } - - return (driver_init_status){.status=OBOS_STATUS_SUCCESS}; -} - -// hexdump clone lol -// OBOS_NO_KASAN void hexdump(void* _buff, size_t nBytes, const size_t width) -// { -// bool printCh = false; -// uint8_t* buff = (uint8_t*)_buff; -// printf(" Address: "); -// for(uint8_t i = 0; i < ((uint8_t)width) + 1; i++) -// printf("%02x ", i); -// printf("\n%016lx: ", buff); -// for (size_t i = 0, chI = 0; i < ((nBytes + 0xf) & ~0xf); i++, chI++) -// { -// if (printCh) -// { -// char ch = (i < nBytes) ? buff[i] : 0; -// switch (ch) -// { -// case '\n': -// case '\t': -// case '\r': -// case '\b': -// case '\a': -// case '\0': -// { -// ch = '.'; -// break; -// } -// default: -// break; -// } -// printf("%c", ch); -// } -// else -// printf("%02x ", (i < nBytes) ? buff[i] : 0); -// if (chI == (size_t)(width + (!(i < (width + 1)) || printCh))) -// { -// chI = 0; -// if (!printCh) -// i -= (width + 1); -// else -// printf(" |\n%016lx: ", &buff[i + 1]); -// printCh = !printCh; -// if (printCh) -// printf("\t| "); -// } -// } -// printf("\n"); -// } diff --git a/src/drivers/generic/r8169/structs.h b/src/drivers/generic/r8169/structs.h deleted file mode 100644 index 45df4eed..00000000 --- a/src/drivers/generic/r8169/structs.h +++ /dev/null @@ -1,276 +0,0 @@ -/* - * drivers/generic/r8169/structs.h - * - * Copyright (c) 2025 Omar Berrow - */ - -#pragma once - -#include -#include - -#include - -#include -#include - -#include -#include - -#include - -#include - -#if OBOS_IRQL_COUNT == 16 -# define IRQL_R8169 (7) -#elif OBOS_IRQL_COUNT == 8 -# define IRQL_R8169 (3) -#elif OBOS_IRQL_COUNT == 4 -# define IRQL_R8169 (2) -#elif OBOS_IRQL_COUNT == 2 -# define IRQL_R8169 (0) -#else -# error Funny business. -#endif - -typedef LIST_HEAD(r8169_frame_list, struct r8169_frame) r8169_frame_list; -LIST_PROTOTYPE(r8169_frame_list, struct r8169_frame, node); - -enum { - FRAME_PURPOSE_TX, - FRAME_PURPOSE_RX, - FRAME_PURPOSE_GENERAL, -}; - -typedef struct r8169_frame { - void* buf; - size_t sz; - size_t idx; - uint32_t purpose; - bool tx_priority_high; - _Atomic(size_t) refcount; - LIST_NODE(r8169_frame_list, struct r8169_frame) node; -} r8169_frame; -typedef struct r8169_buffer { - r8169_frame_list frames; - event envt; // EVENT_NOTIFICATION -} r8169_buffer; -typedef struct r8169_descriptor { - uint32_t command; - uint32_t vlan; - uint64_t buf; -} r8169_descriptor; - -typedef LIST_HEAD(r8169_descriptor_list, struct r8169_descriptor_node) r8169_descriptor_list; -LIST_PROTOTYPE(r8169_descriptor_list, struct r8169_descriptor_node, node); -typedef struct r8169_descriptor_node { - r8169_descriptor* desc; - LIST_NODE(r8169_descriptor_list, struct r8169_descriptor_node) node; -} r8169_descriptor_node; - -#define R8169_DEVICE_MAGIC 0x7186941C -#define R8169_HANDLE_MAGIC 0x7186941D - -typedef struct r8169_device -{ - uint32_t magic; - - vnode* vn; - - pci_device *dev; - pci_resource* bar; // BAR0 - pci_resource* irq_res; - - size_t idx; - - mac_address mac; - char mac_readable[6*3+1]; // XX:XX:XX:XX:XX:XX\0 - - bool ip_checksum_offload : 1; - bool udp_checksum_offload : 1; - bool tcp_checksum_offload : 1; - - r8169_descriptor* sets[3]; - uintptr_t sets_phys[3]; - - bool suspended; - - _Atomic(size_t) refcount; - - irq irq; - dpc dpc; - uint16_t isr; - - char* interface_name; - - // Total number of received packets (both dropped, and undropped) - size_t rx_count; - // Total number of dropped packets - size_t rx_dropped; - // Total number of packet errors - size_t rx_errors; - // Total number of length errors - size_t rx_length_errors; - // Total number of CRC errors - size_t rx_crc_errors; - // Total number of bytes received. - size_t rx_bytes; - // received frames - r8169_buffer rx_buffer; - spinlock rx_buffer_lock; - - size_t tx_idx; - size_t tx_priority_idx; - - // Total number of transmitted packets. - size_t tx_count; - // Total number of dropped packets that were to be transmitted. - size_t tx_dropped; - // Total number of bytes transmitted. - size_t tx_bytes; - // Total number of bytes waiting to be transferred. - size_t tx_awaiting_transfer; - // Total number of bytes from high priority packets waiting to be transferred. - size_t tx_high_priority_awaiting_transfer; - // Frames to transmit. - r8169_buffer tx_buffer; - spinlock tx_buffer_lock; - - uint16_t saved_phy_state[0x20]; - - void(*data_ready)(void *userdata, void* vn, size_t bytes_ready); - void* data_ready_userdata; - thread* data_ready_thread; -} r8169_device; - -typedef struct r8169_device_handle { - uint32_t magic; // R8169_HANDLE_MAGIC - - r8169_device* dev; - r8169_frame* rx_curr; - size_t rx_off; -} r8169_device_handle; - -enum { - MAC0 = 0x0, - MAC1 = 0x4, - MAR0 = 0x8, - TxDescStartAddrLow = 0x20, - TxDescStartAddrHigh = 0x24, - TxHDescStartAddrLow = 0x28, - TxHDescStartAddrHigh = 0x2c, - ChipCmd = 0x37, - TxPoll = 0x38, - IntrMask = 0x3c, - IntrStatus = 0x3e, - TxConfig = 0x40, - RxConfig = 0x44, - TimerCount = 0x48, - MissedPacketCount = 0x4c, - Cfg9346 = 0x50, - TimerInt = 0x58, - PhyAr = 0x60, // dword - RxMaxSize = 0xda, - CPlusCmd = 0xe0, - IntrMitigate = 0xe2, - RxDescAddrLow = 0xe4, - RxDescAddrHigh = 0xe8, - MaxTxPacketSize = 0xec, -}; - -#define TX_ENABLE BIT(2) -#define RX_ENABLE BIT(3) - -enum { - EOR = BIT(30), - NIC_OWN = BIT(31), - // Only valid if NIC_OWN is set - FS = BIT(29), - LS = BIT(28), - MAR = BIT(27), // rx packet - PAM = BIT(25), // rx packet - BAR = BIT(24), // rx packet - RWT_ERR = BIT(22), // rx packet - RES_ERR = BIT(21), // rx packet - RUNT_ERR = BIT(20), // rx packet - CRC_ERR = BIT(19), // rx packet - PID1 = BIT(18), // rx packet - PID0 = BIT(17), // rx packet - PID = PID1|PID0, // rx packet - IPF = BIT(16), // rx packet - UDPF = BIT(15), // rx packet - TCPF = BIT(14), // rx packet - // bits 0-13 - PACKET_LEN_MASK = 0x1fff, - - // bits 0-15 - TX_PACKET_LEN_MASK = 0xffff, - LGSEND = BIT(27), // tx packet - IPCS = BIT(18), // tx packet - UDPCS = BIT(17), // tx packet - TCPCS = BIT(16), // tx packet -}; - -enum { - // Transimission set. - Tx_Set, - // Priority Transimission set. - TxH_Set, - // Receiving set. - Rx_Set, -}; - -// Multiply by 128 to get the real size. -// NOTE: Do not do this when setting MaxTxPacketSize. -#define TX_PACKET_SIZE 0x3b - -#define RX_PACKET_SIZE 0x1fff - -// The maximum amount of descriptors to allocate per set. -#define MAX_DESCS_IN_SET 0x400 /* 1024 */ - -// The amount of descriptors to allocate per set -// TODO: Should we set this to something that would make this <= OBOS_PAGE_SIZE to facilitate -// allocation? -#define DESCS_IN_SET MAX_DESCS_IN_SET - -#define RxOk BIT(0) -#define RxErr BIT(1) -#define TxOk BIT(2) -#define TxErr BIT(3) -#define RxOverflow BIT(4) -#define LinkStatus BIT(5) - -#define Cfg9346_Lock 0x00 -#define Cfg9346_Unlock 0xc0 - -#define ENABLED_IRQS RxOk|RxErr|TxOk|TxErr|LinkStatus|RxOverflow - -void r8169_reset(r8169_device* dev); -void r8169_save_phy(r8169_device* dev); -void r8169_resume_phy(r8169_device* dev); -void r8169_read_mac(r8169_device* dev); -void r8169_hw_reset(r8169_device* dev); -void r8169_init_rxcfg(r8169_device *dev); -void r8169_set_rxcfg_mode(r8169_device *dev); -void r8169_set_txcfg(r8169_device *dev); -void r8169_lock_config(r8169_device* dev); -void r8169_unlock_config(r8169_device* dev); -void r8169_rx(r8169_device* dev); -void r8169_tx(r8169_device* dev); -void r8169_set_irq_mask(r8169_device* dev, uint16_t mask); - -void r8169_alloc_set(r8169_device* dev, uint8_t set); -void r8169_free_set(r8169_device* dev, uint8_t set); -r8169_descriptor* r8169_alloc_desc(r8169_device* dev, uint8_t set); -void r8169_release_desc(r8169_device* dev, r8169_descriptor* desc, uint8_t set); - -obos_status r8169_frame_generate(r8169_device* dev, r8169_frame* frame, const void* data, size_t sz, uint32_t purpose); -obos_status r8169_frame_tx_high_priority(r8169_frame* frame, bool priority); -// *frame is copied into a local buffer. -obos_status r8169_buffer_add_frame(r8169_buffer* buff, r8169_frame* frame); -obos_status r8169_buffer_remove_frame(r8169_buffer* buff, r8169_frame* frame); -obos_status r8169_buffer_read_next_frame(r8169_buffer* buff, r8169_frame** frame); -obos_status r8169_buffer_poll(r8169_buffer* buff); -obos_status r8169_buffer_block(r8169_buffer* buff); -obos_status r8169_tx_queue_flush(r8169_device* dev, bool wait); diff --git a/src/drivers/generic/slowfat/CMakeLists.txt b/src/drivers/generic/slowfat/CMakeLists.txt deleted file mode 100644 index c8a767db..00000000 --- a/src/drivers/generic/slowfat/CMakeLists.txt +++ /dev/null @@ -1,34 +0,0 @@ -# drivers/generic/slowfat/CMakeLists.txt -# -# Copyright (c) 2024 Omar Berrow - -add_executable(slowfat "main.c" "probe.c" "lookup.c" "interface.c" - "cls_alloc.c" "io.c" "create.c" "fat_irp.c" -) - -target_compile_options(slowfat - PRIVATE $<$:${TARGET_DRIVER_COMPILE_OPTIONS_C}> - PRIVATE $<$:-ffreestanding> - PRIVATE $<$:-Wall> - PRIVATE $<$:-Wextra> - PRIVATE $<$:-fstack-protector-all> - PRIVATE $<$:-fno-builtin-memset> - PRIVATE $<$:-fvisibility=hidden> - PRIVATE $<$:-fPIC> -) - -set_property(TARGET slowfat PROPERTY link_depends ${DRIVER_LINKER_SCRIPT}) - -target_include_directories(slowfat - PRIVATE "${CMAKE_SOURCE_DIR}/src/oboskrnl" - PRIVATE ${OBOSKRNL_EXTERNAL_INCLUDES}) - -target_link_options(slowfat - PRIVATE "-nostdlib" - PRIVATE "-fPIC" - PRIVATE "-T${DRIVER_LINKER_SCRIPT}" - PRIVATE "-Wl,-shared" -# PRIVATE "-Wl,--allow-shlib-undefined" - PRIVATE ${TARGET_DRIVER_LINKER_OPTIONS} -) -target_compile_definitions(slowfat PRIVATE OBOS_DRIVER=1) diff --git a/src/drivers/generic/slowfat/alloc.h b/src/drivers/generic/slowfat/alloc.h deleted file mode 100644 index 52cf673d..00000000 --- a/src/drivers/generic/slowfat/alloc.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * drivers/generic/slowfat/alloc.h - * - * Copyright (c) 2024-2025 Omar Berrow -*/ - -#pragma once - -#include - -#include - -#include "structs.h" - -// All functions in this file, unless otherwise specified, should take the fat lock in the fat_cache struct. -// returns UINT32_MAX if no sector was found -uint32_t AllocateClusters(fat_cache* volume, size_t nClusters); -// Returns true if the cluster region was extended, otherwise you need to reallocate the clusters. -bool ExtendClusters(fat_cache* volume, uint32_t cluster, size_t nClusters, size_t oldClusterCount); -void TruncateClusters(fat_cache* volume, uint32_t cluster, size_t newClusterCount, size_t oldClusterCount); -void FreeClusters(fat_cache* volume, uint32_t cluster, size_t nClusters); -void InitializeCacheFreelist(fat_cache* volume); - -// if status is passed as OBOS_STATUS_SUCCESS, the cluster passed is valid. -// if status is passed as OBOS_STATUS_EOF, the cluster passed is valid, and is the last cluster of the chain. -// if status is passed as OBOS_STATUS_ABORTED, the cluster passed is not valid, as an error has occurred following the chain. -typedef iterate_decision(*clus_chain_cb)(uint32_t cluster, obos_status status, void* userdata); -obos_status NextCluster(fat_cache* cache, uint32_t cluster, uint8_t* sec_buf, uint32_t* ret); -uint32_t ClusterSeek(fat_cache* cache, uint32_t cluster, uint32_t nClusters); -void FollowClusterChain(fat_cache* volume, uint32_t clus, clus_chain_cb callback, void* userdata); \ No newline at end of file diff --git a/src/drivers/generic/slowfat/cls_alloc.c b/src/drivers/generic/slowfat/cls_alloc.c deleted file mode 100644 index 29284511..00000000 --- a/src/drivers/generic/slowfat/cls_alloc.c +++ /dev/null @@ -1,407 +0,0 @@ -/* - * drivers/generic/slowfat/cls_alloc.c - * - * Copyright (c) 2024-2025 Omar Berrow - * - * Abandon all hope ye who enter here -*/ - -#include -#include -#include -#include - -#include - -#include - -#include - -#include - -#include - -#include "structs.h" -#include "alloc.h" - -#include -#include - -static OBOS_NO_UBSAN fat12_entry readFat12Entry(const uint8_t* sector, uint32_t cluster, fat_entry_addr addr) -{ - return GetFat12Entry(*(uint16_t*)(sector + addr.offset), cluster); -} -static bool isClusterFree(fat_cache* volume, uint32_t cluster) -{ - bool res = false; - fat_entry_addr addr = {}; - GetFatEntryAddrForCluster(volume, cluster, &addr); - const uint8_t* sector = VfsH_PageCacheGetEntry(volume->volume->vn, addr.lba*volume->blkSize, nullptr); - switch (volume->fatType) - { - case FAT32_VOLUME: - { - fat32_entry ent = {}; - memcpy(&ent, sector + addr.offset, sizeof(ent)); - res = !ent.ent; - break; - } - case FAT16_VOLUME: - { - fat16_entry ent = {}; - memcpy(&ent, sector + addr.offset, sizeof(ent)); - res = !ent.ent; - break; - } - case FAT12_VOLUME: - { - fat12_entry ent = readFat12Entry(sector, cluster, addr); - res = !ent.ent; - break; - } - } - return res; -} -static bool isLastCluster(fat_cache* volume, uint32_t cluster) -{ - // return GetFatEntryAddrForCluster(volume, cluster).lba >= volume->fatSz; - return cluster >= volume->CountofClusters; -} - -static void sync_fat(fat_cache* volume, fat_entry_addr addr, uint8_t* sector) -{ - page* pg = nullptr; - // Start at the 2nd FAT. - for (size_t i = 1; i < volume->bpb->nFATs; i++) - { - memcpy(VfsH_PageCacheGetEntry(volume->volume->vn, (addr.lba+volume->fatSz*i)*volume->blkSize, &pg), sector, volume->blkSize); - Mm_MarkAsDirtyPhys(pg); - } -} - -static void markAllocated(fat_cache* volume, uint32_t cluster) -{ - fat_entry_addr addr = {}; - GetFatEntryAddrForCluster(volume, cluster, &addr); - OBOS_ASSERT(volume->blkSize <= OBOS_PAGE_SIZE); - page* pg = nullptr; - uint8_t* sector = VfsH_PageCacheGetEntry(volume->volume->vn, addr.lba*volume->blkSize, &pg); - switch (volume->fatType) - { - case FAT32_VOLUME: - { - fat32_entry ent = {}; - memcpy(&ent, sector+addr.offset, sizeof(ent)); - // note: +1 is intentional - ent.ent = cluster+1; - memcpy(sector+addr.offset, &ent, sizeof(ent)); - break; - } - case FAT16_VOLUME: - { - fat16_entry ent = {}; - memcpy(&ent, sector+addr.offset, sizeof(ent)); - // note: +1 is intentional - ent.ent = cluster+1; - memcpy(sector+addr.offset, &ent, sizeof(ent)); - break; - } - case FAT12_VOLUME: - { - fat12_entry ent = GetFat12Entry(*(uint16_t*)(sector+addr.offset), cluster); - // note: +1 is intentional - ent.ent = cluster+1; - memcpy(sector+addr.offset, &ent, sizeof(ent)); - break; - } - } - Mm_MarkAsDirtyPhys(pg); - sync_fat(volume, addr, sector); -} -static void markFree(fat_cache* volume, uint32_t cluster) -{ - fat_entry_addr addr = {}; - GetFatEntryAddrForCluster(volume, cluster, &addr); - page* pg = nullptr; - uint8_t* sector = VfsH_PageCacheGetEntry(volume->volume->vn, addr.lba*volume->blkSize, &pg); - switch (volume->fatType) - { - case FAT32_VOLUME: - { - fat32_entry ent = {}; - memcpy(&ent, sector+addr.offset, sizeof(ent)); - ent.ent = 0; - memcpy(sector+addr.offset, &ent, sizeof(ent)); - break; - } - case FAT16_VOLUME: - { - fat16_entry ent = {}; - memcpy(&ent, sector+addr.offset, sizeof(ent)); - ent.ent = 0; - memcpy(sector+addr.offset, &ent, sizeof(ent)); - break; - } - case FAT12_VOLUME: - { - fat12_entry ent = GetFat12Entry(*(uint16_t*)(sector+addr.offset), cluster); - ent.ent = 0; - memcpy(sector+addr.offset, &ent, sizeof(ent)); - break; - } - } - Mm_MarkAsDirtyPhys(pg); - sync_fat(volume, addr, sector); -} -static void markEnd(fat_cache* volume, uint32_t cluster) -{ - fat_entry_addr addr = {}; - GetFatEntryAddrForCluster(volume, cluster, &addr); - page* pg = nullptr; - uint8_t* sector = VfsH_PageCacheGetEntry(volume->volume->vn, addr.lba*volume->blkSize, &pg); - switch (volume->fatType) - { - case FAT32_VOLUME: - { - uint32_t free = 0x0FFFFFF8; - fat32_entry ent = {}; - memcpy(&ent, sector+addr.offset, sizeof(ent)); - ent.ent = free; - memcpy(sector+addr.offset, &ent, sizeof(ent)); - break; - } - case FAT16_VOLUME: - { - uint16_t free = 0xFFF8; - fat16_entry ent = {}; - memcpy(&ent, sector+addr.offset, sizeof(ent)); - ent.ent = free; - memcpy(sector+addr.offset, &ent, sizeof(ent)); - break; - } - case FAT12_VOLUME: - { - uint16_t free = 0x0FF8; - fat12_entry ent = GetFat12Entry(*(uint16_t*)(sector+addr.offset), cluster); - ent.ent = free; - memcpy(sector+addr.offset, &ent, sizeof(ent)); - break; - } - } - Mm_MarkAsDirtyPhys(pg); - sync_fat(volume, addr, sector); -} -uint32_t AllocateClusters(fat_cache* volume, size_t nClusters) -{ - // TODO: Use fsinfo structure. - // if (volume->fatType == FAT32_VOLUME) - // {} - if (volume->freelist.freeClusterCount < nClusters) - return UINT32_MAX; - fat_freenode* node = volume->freelist.tail; - while (node && node->nClusters < nClusters) - node = node->prev; - if (!node) - return UINT32_MAX; - node->nClusters -= nClusters; - uint32_t cluster = node->cluster+node->nClusters; - if (!node->nClusters) - { - if (node->prev) - node->prev->next = node->prev; - if (node->next) - node->next->prev = node->prev; - if (volume->freelist.head == node) - volume->freelist.head = node->next; - if (volume->freelist.tail == node) - volume->freelist.tail = node->prev; - volume->freelist.nNodes--; - FATAllocator->Free(FATAllocator, node, sizeof(*node)); - } - for (size_t i = 0; i < nClusters; i++) - markAllocated(volume, cluster+i); - markEnd(volume, cluster+(nClusters-1)); - volume->freelist.freeClusterCount -= nClusters; - return cluster; -} -// Returns true if the cluster region was extended, otherwise you need to reallocate the clusters. -bool ExtendClusters(fat_cache* volume, uint32_t cluster, size_t nClusters, size_t oldClusterCount) -{ - // if (nClusters < oldClusterCount) - // return false; - // if (nClusters == oldClusterCount) - // return true; - // size_t diff = nClusters-oldClusterCount; - // size_t i = 0; - // markFree(volume, cluster); - // for (i = 0; i < diff && !isLastCluster(volume, cluster+i); i++) - // { - // if (!isClusterFree(volume, cluster+i)) - // return false; - // }; - // for (i = 0; i < diff; i++) - // markAllocated(volume, cluster+i); - // markEnd(volume, cluster+diff); - // return true; - OBOS_UNUSED(volume); - OBOS_UNUSED(cluster); - OBOS_UNUSED(nClusters); - OBOS_UNUSED(oldClusterCount); - return false; // TODO: Implement extending clusters. -} -void TruncateClusters(fat_cache* volume, uint32_t cluster, size_t newClusterCount, size_t oldClusterCount) -{ - if (newClusterCount >= oldClusterCount) - return; - FreeClusters(volume, cluster+oldClusterCount, oldClusterCount-newClusterCount); - if (newClusterCount) - markEnd(volume, cluster+newClusterCount-1); -} -void FreeClusters(fat_cache* volume, uint32_t cluster, size_t nClusters) -{ - for (size_t i = 0; i < nClusters && !isLastCluster(volume, cluster); i++) - markFree(volume, cluster+i); - fat_freenode *curr = FATAllocator->ZeroAllocate(FATAllocator, 1, sizeof(*curr), nullptr); - curr->cluster = cluster; - curr->nClusters = nClusters; - if (!volume->freelist.head) - volume->freelist.head = curr; - if (volume->freelist.tail) - volume->freelist.tail->next = curr; - curr->prev = volume->freelist.tail; - volume->freelist.tail = curr; - volume->freelist.nNodes++; - volume->freelist.freeClusterCount += curr->nClusters; -} -void InitializeCacheFreelist(fat_cache* volume) -{ - uint32_t cluster = 0; - struct fat_freenode* curr = nullptr; - for (; !isLastCluster(volume, cluster); cluster++) - { - if (isClusterFree(volume, cluster)) - { - if (!curr) - { - curr = FATAllocator->ZeroAllocate(FATAllocator, 1, sizeof(*curr), nullptr); - curr->cluster = cluster; - } - curr->nClusters++; - } - else - { - if (!curr) - continue; - while (isLastCluster(volume, curr->cluster + curr->nClusters)) - curr->nClusters--; - if (!volume->freelist.head) - volume->freelist.head = curr; - if (volume->freelist.tail) - volume->freelist.tail->next = curr; - curr->prev = volume->freelist.tail; - volume->freelist.tail = curr; - volume->freelist.nNodes++; - volume->freelist.freeClusterCount += curr->nClusters; - curr = nullptr; - } - } - if (curr) - { - if (!volume->freelist.head) - volume->freelist.head = curr; - if (volume->freelist.tail) - volume->freelist.tail->next = curr; - curr->prev = volume->freelist.tail; - volume->freelist.tail = curr; - volume->freelist.nNodes++; - volume->freelist.freeClusterCount += curr->nClusters; - } -} -obos_status NextCluster(fat_cache* cache, uint32_t cluster, uint8_t* sec_buf, uint32_t* ret) -{ - uint32_t res = 0; - uint32_t last_clus_val = 0x0ffffff8; - fat_entry_addr addr = {}; - GetFatEntryAddrForCluster(cache, cluster, &addr); - uint32_t offset = addr.offset; - switch (cache->fatType) { - case FAT32_VOLUME: - { - fat32_entry ent = {}; - memcpy(&ent, sec_buf + offset, sizeof(ent)); - res = ent.ent; - last_clus_val = 0x0ffffff8; - break; - } - case FAT16_VOLUME: - { - fat16_entry ent = {}; - memcpy(&ent, sec_buf + offset, sizeof(ent)); - res = ent.ent; - last_clus_val = 0xfff8; - break; - } - case FAT12_VOLUME: - { - fat12_entry ent = GetFat12Entry(*(uint16_t*)(sec_buf + offset), cluster); - res = ent.ent; - last_clus_val = 0xff8; - break; - } - } - *ret = res; - return res >= last_clus_val ? OBOS_STATUS_EOF : OBOS_STATUS_SUCCESS; -} - -static iterate_decision cluster_seek_cb(uint32_t cluster, obos_status status, void* userdata) -{ - uint32_t* data = userdata; - uint32_t* const nClusters = data+1; - uint32_t* const res = data; - if (!(--(*nClusters))) - { - *res = cluster; - return ITERATE_DECISION_STOP; - } - return ITERATE_DECISION_CONTINUE; -} -uint32_t ClusterSeek(fat_cache* cache, uint32_t cluster, uint32_t nClusters) -{ - if (!nClusters) - return cluster; - uint32_t udata[2] = {UINT32_MAX,nClusters}; - FollowClusterChain(cache, cluster, cluster_seek_cb, udata); - return udata[0]; // If UINT32_MAX, then nClusters is out of bounds. -} - -void FollowClusterChain(fat_cache* volume, uint32_t clus, clus_chain_cb callback, void* userdata) -{ - fat_entry_addr addr = {}; - GetFatEntryAddrForCluster(volume, clus, &addr); - uint8_t* sector = VfsH_PageCacheGetEntry(volume->volume->vn, addr.lba*volume->blkSize, nullptr); - uint32_t curr = clus; - obos_status status = OBOS_STATUS_SUCCESS; - do { - if (callback(curr, status, userdata) == ITERATE_DECISION_STOP) - break; - status = NextCluster(volume, curr, sector, &curr); - if (status == OBOS_STATUS_EOF) - break; - if (curr == 0) - { - OBOS_Error("FAT: Error following cluster chain: Unexpected free cluster. Aborting.\n"); - callback(0, OBOS_STATUS_ABORTED, userdata); - break; - } - if (curr >= volume->CountofClusters) - { - OBOS_Error("FAT: Error following cluster chain: Cluster is over disk boundaries. Aborting.\n"); - callback(0, OBOS_STATUS_ABORTED, userdata); - break; - } - uint64_t prev_lba = addr.lba; - GetFatEntryAddrForCluster(volume, curr, &addr); - if (addr.lba != prev_lba) - sector = VfsH_PageCacheGetEntry(volume->volume->vn, addr.lba*volume->blkSize, nullptr); - } while(status != OBOS_STATUS_EOF); -} diff --git a/src/drivers/generic/slowfat/create.c b/src/drivers/generic/slowfat/create.c deleted file mode 100644 index a1ceedd8..00000000 --- a/src/drivers/generic/slowfat/create.c +++ /dev/null @@ -1,679 +0,0 @@ -/* - * drivers/generic/slowfat/create.c - * - * Copyright (c) 2024 Omar Berrow - * - * Abandon all hope ye who enter here -*/ - -#include -#include -#include -#include - -#include - -#include -#include -#include - -#include - -#include - -#include - -#include "alloc.h" -#include "structs.h" - -bool valid_filename(const char* filename, bool isPath) -{ - for (size_t i = 0; filename[i]; i++) - { - switch (filename[i]) { - case '<': - case '>': - case ':': - case '\"': - case '\\': - case '|': - case '?': - case '*': - return false; - case '/': - { - if (!isPath) - return false; - break; - } - } - } - return true; -} -static void ref_dirent(fat_dirent_cache* cache_entry); -static void gen_short_name(const char* long_name, char* name, fat_dirent_cache* parent, fat_dirent_cache* dirent); -obos_status mk_file(dev_desc* newDesc, dev_desc parent_desc, void* vn_, const char* name, file_type type, driver_file_perm perm) -{ - if (!parent_desc || !newDesc || !name) - return OBOS_STATUS_INVALID_ARGUMENT; - if (type == FILE_TYPE_SYMBOLIC_LINK) - return OBOS_STATUS_INTERNAL_ERROR; - if (!valid_filename(name, false)) - return OBOS_STATUS_INVALID_ARGUMENT; - fat_dirent_cache* parent = (fat_dirent_cache*)parent_desc; - fat_cache* cache = nullptr; - if (parent_desc == UINTPTR_MAX) - { - if (!vn_) - return OBOS_STATUS_INVALID_ARGUMENT; - for (cache = LIST_GET_HEAD(fat_cache_list, &FATVolumes); cache; ) - { - if (cache->vn == vn_) - break; - - cache = LIST_GET_NEXT(fat_cache_list, &FATVolumes, cache); - } - if (!cache) - return OBOS_STATUS_INVALID_OPERATION; // not a fat volume we have probed - parent = cache->root; - } - if (!cache) - cache = parent->owner; - if (!(parent->data.attribs & DIRECTORY)) - return OBOS_STATUS_INVALID_ARGUMENT; - for (fat_dirent_cache* curr = parent->fdc_children.head; curr; ) - { - if (OBOS_CompareStringC(&curr->name, name)) - return OBOS_STATUS_ALREADY_INITIALIZED; - curr = curr->fdc_next_child; - } - fat_dirent_cache* new = FATAllocator->ZeroAllocate(FATAllocator, 1, sizeof(fat_dirent_cache), nullptr); - *newDesc = (dev_desc)new; - cache->fileCount++; - new->data.creation_date = (fat_date){}; - new->data.last_mod_date = new->data.creation_date; - new->data.creation_time = (fat_time){}; - new->data.last_mod_time = new->data.creation_time; - new->data.access_date = new->data.creation_date; - new->data.filesize = 0; - new->data.attribs |= (type == FILE_TYPE_DIRECTORY) ? DIRECTORY : 0; - if (!perm.owner_write) - new->data.attribs |= READ_ONLY; - - if (type == FILE_TYPE_DIRECTORY) - { - uint32_t cluster = AllocateClusters(cache, 1); - new->data.first_cluster_low = cluster & 0xffff; - if (cache->fatType == FAT32_VOLUME) - new->data.first_cluster_high = cluster >> 16; - fat_dirent dot_entries[2] = {}; - dot_entries[0].filename_83[0] = '.'; - memset(dot_entries[0].filename_83+1, ' ', 10); - dot_entries[0].attribs = new->data.attribs; - dot_entries[0].first_cluster_low = new->data.first_cluster_low; - dot_entries[0].first_cluster_high = new->data.first_cluster_high; - dot_entries[0].last_mod_time = new->data.last_mod_time; - dot_entries[0].last_mod_date = new->data.last_mod_date; - dot_entries[0].creation_time = new->data.creation_time; - dot_entries[0].creation_date = new->data.creation_date; - memcpy(dot_entries+1,dot_entries,sizeof(*dot_entries)); - dot_entries[1].first_cluster_low = parent->data.first_cluster_low; - dot_entries[1].first_cluster_high = parent->data.first_cluster_high; - dot_entries[1].filename_83[1] = '.'; - page* pg = nullptr; - void* buff = VfsH_PageCacheGetEntry(cache->volume->vn, ClusterToSector(cache, cluster)*cache->blkSize, &pg); - memcpy(buff, dot_entries, sizeof(dot_entries)); - Mm_MarkAsDirtyPhys(pg); - } - else - { - new->data.first_cluster_low = 0; - new->data.first_cluster_high = 0; - } - - - gen_short_name(name, new->data.filename_83, parent, new); - new->owner = parent->owner; - OBOS_InitString(&new->name, name); - OBOS_InitStringLen(&new->path, OBOS_GetStringCPtr(&parent->path), OBOS_GetStringSize(&parent->path)); - if (OBOS_GetStringSize(&new->path)) - if (OBOS_GetStringCPtr(&new->path)[OBOS_GetStringSize(&new->path) - 1] != '/') - OBOS_AppendStringC(&new->path, "/"); - OBOS_AppendStringS(&new->path, &new->name); - CacheAppendChild(parent, new); - Core_MutexAcquire(&new->owner->fd_lock); - ref_dirent(new); - Vfs_FdFlush(new->owner->volume); - Core_MutexRelease(&new->owner->fd_lock); - return OBOS_STATUS_SUCCESS; -} -static size_t strrfind(const char* str, char ch) -{ - int64_t i = strlen(str); - for (; i >= 0; i--) - if (str[i] == ch) - return i; - return SIZE_MAX; -} -static char toupper(char ch) -{ - if (ch >= 'a' && ch <= 'z') - return (ch - 'a') + 'A'; - return ch; -} -static void gen_short_name_impl(const char* long_name, char* name) -{ - // memcpy(name, long_name, 8); - while(*long_name == ' ') - long_name++; - uint8_t short_i = 0; - size_t i = 0; - for (; short_i < 8 && i < strlen(long_name); i++) - { - if (long_name[i] == ' ') - continue; - if (long_name[i] == '.') - break; - name[short_i++] = toupper(long_name[i]); - } - for (; short_i < 8; short_i++) - name[short_i] = ' '; - if (strrfind(long_name, '.') != SIZE_MAX) - { - size_t off = strrfind(long_name, '.')+1; - for (uint8_t i = 0; i < 3 && i < strlen(long_name+off); i++) - name[i+8] = toupper(long_name[off+i]); - } - else - memset(name+short_i, ' ', 11-short_i); -} -static void gen_short_name(const char* long_name, char* name, fat_dirent_cache* parent, fat_dirent_cache* dirent) -{ - memset(name, 0, 11); - if (strlen(long_name) <= 11) - { - // memcpy(name, long_name, 11); - gen_short_name_impl(long_name, name); - return; - } - uint32_t n = 1; - char raw[11] = {}; - gen_short_name_impl(long_name, raw); - for (fat_dirent_cache* curr = parent->fdc_children.head; curr; ) - { - if (curr == dirent) - { - curr = curr->fdc_next_child; - continue; - } - char curr_name[11] = {}; - gen_short_name_impl(OBOS_GetStringCPtr(&curr->name), curr_name); - if (memcmp(raw, curr_name, 11)) - n++; - curr = curr->fdc_next_child; - } - OBOS_ASSERT(n <= 999999); - uint8_t basename_len = 0; - for (; raw[basename_len] != ' ' && basename_len < 8; basename_len++) - ; - size_t sz = snprintf(nullptr, 0, "%d", n); - raw[basename_len-sz-1] = '~'; - char buf[7] = {}; - snprintf(buf, sz+1, "%d", n); - memcpy(&raw[basename_len-sz], buf, sz); - memcpy(name, raw, 11); -} -static const char* basename(const char* path) -{ - size_t base = strrfind(path, '/'); - if (base == SIZE_MAX) - return path; - return path + base + 1; -} -#define DIRENT_FREE (char)0xe5 -static void deref_dirent(fat_dirent_cache* cache_entry) -{ - fat_cache* cache = cache_entry->owner; - int64_t clusterSize = cache->blkSize*cache->bpb->sectorsPerCluster; - off_t sector_offset = cache_entry->dirent_offset; - // Vfs_FdSeek(cache->volume, cache_entry->dirent_fileoff, SEEK_SET); - // Vfs_FdRead(cache->volume, sector, clusterSize, nullptr); - OBOS_ASSERT(clusterSize < OBOS_PAGE_SIZE); - page* pg = nullptr; - uint8_t* sector = VfsH_PageCacheGetEntry(cache->vn, cache_entry->dirent_fileoff, &pg); - fat_dirent* curr = (fat_dirent*)(sector + cache_entry->dirent_offset); - curr--; - while (curr->attribs & LFN) - { - curr->filename_83[0] = DIRENT_FREE; - curr--; - sector_offset -= sizeof(fat_dirent); - if (sector_offset == 0) - { - // // TODO: Follow cluster chain. - // sector_offset = clusterSize - sizeof(fat_dirent); - // Vfs_FdSeek(cache->volume, -clusterSize, SEEK_CUR); - // Vfs_FdWrite(cache->volume, sector, clusterSize, nullptr); - // Vfs_FdSeek(cache->volume, -clusterSize, SEEK_CUR); - // Vfs_FdRead(cache->volume, sector, clusterSize, nullptr); - // wroteback = true; - // curr = (fat_dirent*)(sector + cache_entry->dirent_offset); - // curr--; - // lmao just let someone else free the orphaned lfn entries - break; - } - } - cache_entry->data.filename_83[0] = DIRENT_FREE; - memcpy(sector + cache_entry->dirent_offset, &cache_entry->data, sizeof(cache_entry->data)); - Mm_MarkAsDirtyPhys(pg); - // WriteFatDirent(cache, cache_entry); -} -static uint8_t checksum(const char *pFcbName) -{ - uint16_t FcbNameLen; - uint8_t Sum; - Sum = 0; - for (FcbNameLen=11; FcbNameLen!=0; FcbNameLen--) - Sum = ((Sum & 1) ? 0x80 : 0) + (Sum >> 1) + *pFcbName++; - return Sum; -} -static void process_dirent(fat_cache* cache, const fat_dirent* curr, fat_dirent_cache* parent, uint32_t off, uint32_t* const fileoff, uint32_t* const nFree) -{ - if (curr->filename_83[0] == DIRENT_FREE) - { - if (!(*nFree)++) - *fileoff = off; - } - else - { - *fileoff = 0; - *nFree = 0; - } - OBOS_UNUSED(cache); - OBOS_UNUSED(parent); - OBOS_UNUSED(parent); -} -iterate_decision find_free_entry(uint32_t cluster, obos_status status, void* userdata) -{ - if (status == OBOS_STATUS_ABORTED) - return ITERATE_DECISION_STOP; - fat_cache* cache = (void*)((uintptr_t*)userdata)[0]; - fat_dirent_cache* parent = (void*)((uintptr_t*)userdata)[1]; - uint32_t nEntries = ((uintptr_t*)userdata)[2]; - // void* buff = (void*)((uintptr_t*)userdata)[3]; - uint32_t* nFree = (void*)((uintptr_t*)userdata)[4]; - uint32_t* fileoff = (void*)((uintptr_t*)userdata)[5]; - (*(size_t*)((uintptr_t*)userdata)[6])++; - uint32_t* entry_cluster = (void*)((uintptr_t*)userdata)[7]; - const size_t clusterSize = cache->blkSize*cache->bpb->sectorsPerCluster; - // Vfs_FdSeek(cache->volume, ClusterToSector(cache, cluster)*cache->blkSize, SEEK_SET); - // Vfs_FdRead(cache->volume, buff, clusterSize, nullptr); - const void* buff = VfsH_PageCacheGetEntry(cache->vn, ClusterToSector(cache, cluster)*cache->blkSize, nullptr); - const fat_dirent* curr = buff; - for (size_t j = 0; j < (clusterSize/sizeof(fat_dirent)); j++) - { - if (curr->filename_83[0] == (char)0) - { - // TODO: Account for the other clusters. - *entry_cluster = cluster; - *nFree = (clusterSize/sizeof(fat_dirent))-j; - *fileoff = ClusterToSector(cache, cluster)*cache->blkSize+(j*sizeof(fat_dirent)); - break; - } - process_dirent(cache, curr, parent, ClusterToSector(cache, cluster)*cache->blkSize+(j*sizeof(fat_dirent)), fileoff, nFree); - if ((*nFree) == 1) - *entry_cluster = cluster; - if ((*nFree) >= nEntries) - { - curr = nullptr; - break; - } - curr++; - } - return ITERATE_DECISION_CONTINUE; -} -static void ref_dirent(fat_dirent_cache* cache_entry) -{ - fat_cache* cache = cache_entry->owner; - lfn_dirent* lfnEntries = nullptr; - uint8_t nLfnEntries = 0; - if (OBOS_GetStringSize(&cache_entry->name) > 11) - { - // we need to generate lfn entries. - nLfnEntries = (OBOS_GetStringSize(&cache_entry->name) / 13) + ((OBOS_GetStringSize(&cache_entry->name) % 13) != 0); - lfnEntries = FATAllocator->ZeroAllocate(FATAllocator, nLfnEntries, sizeof(lfn_dirent), nullptr); - size_t nBytesLeft = OBOS_GetStringSize(&cache_entry->name); - uint8_t chksum = checksum((char*)&cache_entry->data.filename_83); - for (uint8_t order = 0; order < nLfnEntries; order++) - { - lfnEntries[order].order = order+1; - lfnEntries[order].checksum = chksum; - lfnEntries[order].mustBeZero = 0; - if (order == (nLfnEntries - 1)) - lfnEntries[order].order |= 0x40; - lfnEntries[order].attrib |= LFN; - for (size_t i = 0, limit = nBytesLeft >= 13 ? 13 : nBytesLeft; i < limit; i++) - { - if (i < 5) - lfnEntries[order].name1[i] = OBOS_GetStringCPtr(&cache_entry->name)[i]; - else if (i >= 5 && i < 11) - lfnEntries[order].name2[i - 5] = OBOS_GetStringCPtr(&cache_entry->name)[i]; - else if (i == 11 || i == 12) - lfnEntries[order].name3[i - 11] = OBOS_GetStringCPtr(&cache_entry->name)[i]; - } - } - } - size_t nEntries = nLfnEntries + 1; - const size_t bytesPerCluster = (cache->bpb->sectorsPerCluster*cache->blkSize); - size_t nClusters = nEntries*sizeof(fat_dirent); - nClusters /= bytesPerCluster; - if ((nEntries*sizeof(fat_dirent)) % bytesPerCluster) - nClusters++; - // Search for enough free entries - bool inRoot = cache->root == cache_entry->fdc_parent; - uint32_t nFree = 0; - uint32_t fileoff = 0; - uint32_t entry_cluster = 0; - size_t szClusters = 0; - size_t blkSize = 0; - if (inRoot && cache->fatType != FAT32_VOLUME) - { - // We need to iterate over each sector in the root directory. - blkSize = cache->blkSize; - // void* buff = FATAllocator->ZeroAllocate(FATAllocator, 1, cache->blkSize, nullptr); - Vfs_FdSeek(cache->volume, cache->root_sector*cache->blkSize, SEEK_SET); - for (size_t i = cache->root_sector; i < (cache->root_sector+cache->RootDirSectors); i++) - { - const void* buff = VfsH_PageCacheGetEntry(cache->volume->vn, i*cache->blkSize, nullptr); - const fat_dirent* curr = buff; - for (size_t j = 0; j < (cache->blkSize/sizeof(fat_dirent)); j++) - { - if (curr->filename_83[0] == (char)0) - { - nFree = (cache->blkSize/sizeof(fat_dirent))-j; - // fnuy business - nFree += (cache->RootDirSectors-(i-cache->root_sector))*(cache->blkSize/sizeof(fat_dirent)); - fileoff = (i*cache->blkSize)+(j*sizeof(fat_dirent)); - curr = nullptr; - break; - } - process_dirent(cache, curr, cache_entry->fdc_parent, (i*cache->blkSize)+(j*sizeof(fat_dirent)), &fileoff, &nFree); - if (nFree >= nEntries) - { - curr = nullptr; - break; - } - curr++; - } - if (!curr) - break; - } - } - else - { - // We need to iterate over each cluster in the directory. - blkSize = bytesPerCluster; - uintptr_t udata[] = { - (uintptr_t)cache, - (uintptr_t)cache_entry, - nEntries, - (uintptr_t)0, - (uintptr_t)&nFree, - (uintptr_t)&fileoff, - (uintptr_t)&szClusters, - (uintptr_t)&entry_cluster - }; - uint32_t first_cluster = cache_entry->fdc_parent->data.first_cluster_low; - first_cluster |= ((uint32_t)cache_entry->fdc_parent->data.first_cluster_high << 16); - FollowClusterChain(cache, first_cluster, find_free_entry, udata); - } - // void *buf = FATAllocator->ZeroAllocate(FATAllocator, 1, bytesPerCluster, nullptr); - page* pg = nullptr; - if (nFree < nEntries) - { - // We need MOREEEEEEEEEEEE clusters - if (inRoot && cache->fatType != FAT32_VOLUME) - return; // nothing we can do about this... - size_t newSizeCls = szClusters + nClusters; - uint32_t cluster = cache_entry->fdc_parent->data.first_cluster_low; - cluster |= ((uint32_t)cache_entry->fdc_parent->data.first_cluster_high << 16); - if (!ExtendClusters(cache, cluster, newSizeCls, szClusters)) - { - uint32_t newCluster = AllocateClusters(cache, newSizeCls); - if (newCluster == UINT32_MAX) - { - Core_MutexRelease(&cache->fat_lock); - return; - } - for (size_t i = 0; i < szClusters; i++) - { - const void* buf = VfsH_PageCacheGetEntry(cache->vn, ClusterToSector(cache, cluster+i)*cache->blkSize, nullptr); - void* out = VfsH_PageCacheGetEntry(cache->vn, ClusterToSector(cache, newCluster+i)*cache->blkSize, &pg); - memcpy(out, buf, bytesPerCluster); - Mm_MarkAsDirtyPhys(pg); - } - if (cluster) - FreeClusters(cache, cluster, szClusters); - cluster = newCluster; - cache_entry->fdc_parent->data.first_cluster_high = cluster >> 16; - cache_entry->fdc_parent->data.first_cluster_low = cluster & 0xffff; - if (cache_entry != cache->root) - WriteFatDirent(cache, cache_entry->fdc_parent, false); - else - { - cache->bpb->ebpb.fat32.rootCluster = cluster; - WriteFatDirent(cache, cache_entry->fdc_parent, false); - Vfs_FdSeek(cache->volume, 0, SEEK_SET); - // memzero(buf, cache->blkSize); - // memcpy(buf, cache->bpb, sizeof(*cache->bpb)); - // Vfs_FdWrite(cache->volume, buf, cache->blkSize, nullptr); - memcpy(VfsH_PageCacheGetEntry(cache->vn, 0, &pg), cache->bpb, sizeof(*cache->bpb)); - Mm_MarkAsDirtyPhys(pg); - } - } - fileoff = ClusterToSector(cache, cluster)*cache->blkSize; - entry_cluster = cluster; - } - Vfs_FdSeek(cache->volume, fileoff, SEEK_SET); - void* buf = VfsH_PageCacheGetEntry(cache->vn, fileoff, &pg); - fileoff = Vfs_FdTellOff(cache->volume); - fat_dirent* curr = buf; - for (size_t i = 0; i < nEntries; i++) - { - fat_dirent* curr_dirent = &cache_entry->data; - if (i < nLfnEntries) - curr_dirent = (fat_dirent*)&lfnEntries[i]; - memcpy(curr, curr_dirent, sizeof(*curr)); - curr++; - if ((uintptr_t)curr >= ((uintptr_t)buf+blkSize)) - { - if (inRoot && cache->fatType != FAT32_VOLUME) - { - // Simply read the next sector. - Mm_MarkAsDirtyPhys(pg); - fileoff += cache->blkSize; - buf = VfsH_PageCacheGetEntry(cache->vn, fileoff, &pg); - curr = buf; - } - else - { - Mm_MarkAsDirtyPhys(pg); - // Read the next cluster. - Vfs_FdSeek(cache->volume, fileoff, SEEK_SET); - uint32_t next = 0; - fat_entry_addr addr = {}; - GetFatEntryAddrForCluster(cache, entry_cluster, &addr); - uint8_t* sector = VfsH_PageCacheGetEntry(cache->vn, addr.lba*cache->blkSize, nullptr); - obos_status status = NextCluster(cache, entry_cluster, sector, &next); - if (status == OBOS_STATUS_EOF) - break; - if (next == 0) - { - OBOS_Error("FAT: Error following cluster chain: Unexpected free cluster. Aborting.\n"); - FATAllocator->Free(FATAllocator, buf, bytesPerCluster); - break; - } - if (next >= cache->CountofClusters) - { - OBOS_Error("FAT: Error following cluster chain: Cluster is over disk boundaries. Aborting.\n"); - FATAllocator->Free(FATAllocator, buf, bytesPerCluster); - break; - } - Vfs_FdSeek(cache->volume, ClusterToSector(cache, next)*cache->blkSize, SEEK_SET); - fileoff = Vfs_FdTellOff(cache->volume); - buf = VfsH_PageCacheGetEntry(cache->vn, fileoff, &pg); - curr = buf; - } - } - } -} -// obos_status move_desc_to(dev_desc desc, const char* where) -// { -// if (!desc || !where) -// return OBOS_STATUS_INVALID_ARGUMENT; -// if (!valid_filename(where, true)) -// return OBOS_STATUS_INVALID_ARGUMENT; -// fat_dirent_cache* cache_entry = (fat_dirent_cache*)desc; -// fat_cache* cache = cache_entry->owner; -// /* -// To move a entry: -// - IF the paths are the same -// - return SUCCESS -// - IF the new path already exists -// - return ALREADY_EXISTS -// - IF the new parent doesn't exist -// - return NOT_FOUND -// - Change it's name to the new name -// - Change it's path (and all its childrens' path) to the new path -// - IF the paths differ, and the parent directory of both paths differ -// - Dereference the cache node from its parent, and add it to the new parent. -// - Dereference the dirent from its parent, and add it to the new parent -// - else -// - Dereference the dirent from its parent, and add it with the new basename -// - exit -// */ -// do { -// fat_dirent_cache* found = DirentLookupFrom(where, cache->root); -// if (found == cache_entry) -// return OBOS_STATUS_SUCCESS; -// if (found) -// return OBOS_STATUS_ALREADY_INITIALIZED; -// } while(0); -// string parent_path = {}; -// OBOS_InitStringLen(&parent_path, where, strrfind(where, '/') == SIZE_MAX ? strlen(where) : strrfind(where, '/')); -// fat_dirent_cache* parent = DirentLookupFrom(OBOS_GetStringCPtr(&parent_path), cache->root); -// if (!parent && OBOS_GetStringSize(&parent_path) == strlen(where)) -// { -// parent = cache->root; -// parent_path = cache->root->path; -// } -// if (!parent) -// return OBOS_STATUS_NOT_FOUND; -// OBOS_FreeString(&cache_entry->name); -// OBOS_FreeString(&cache_entry->path); -// cache_entry->path = parent_path; -// if (OBOS_GetStringSize(&cache_entry->path)) -// if (OBOS_GetStringCPtr(&cache_entry->path)[OBOS_GetStringSize(&cache_entry->path) - 1] != '/') -// OBOS_AppendStringC(&cache_entry->path, "/"); -// OBOS_InitString(&cache_entry->name, basename(where)); -// OBOS_AppendStringS(&cache_entry->path, &cache_entry->name); -// if (parent != cache_entry->fdc_parent) -// { -// Core_MutexAcquire(&cache->fat_lock); -// Core_MutexAcquire(&cache->fd_lock); -// deref_dirent(cache_entry); -// CacheRemoveChild(cache_entry->fdc_parent, cache_entry); -// CacheAppendChild(parent, cache_entry); -// gen_short_name(OBOS_GetStringCPtr(&cache_entry->name), &cache_entry->data.filename_83[0], parent, cache_entry); -// ref_dirent(cache_entry); -// Core_MutexRelease(&cache->fd_lock); -// Core_MutexRelease(&cache->fat_lock); -// } -// else -// { -// deref_dirent(cache_entry); -// Core_MutexAcquire(&cache->fat_lock); -// Core_MutexAcquire(&cache->fd_lock); -// gen_short_name(OBOS_GetStringCPtr(&cache_entry->name), &cache_entry->data.filename_83[0], parent, cache_entry); -// ref_dirent(cache_entry); -// Core_MutexRelease(&cache->fd_lock); -// Core_MutexRelease(&cache->fat_lock); -// } -// Vfs_FdFlush(cache->volume); -// return OBOS_STATUS_SUCCESS; -// } -obos_status move_desc_to(dev_desc desc, dev_desc new_parent_desc, const char* name) -{ - if (!desc) - return OBOS_STATUS_INVALID_ARGUMENT; - if (name && !valid_filename(name, true)) - return OBOS_STATUS_INVALID_ARGUMENT; - if (!new_parent_desc && !name) - return OBOS_STATUS_SUCCESS; // Nothing to do. - - // If !new_parent && name, then we need to rename the file. - // If new_parent && !name, then we need to move the file to the new parent, and keep the filename. - // If new_parent && name, then we need to move the file to new parent and rename the file. - - fat_dirent_cache* cache_entry = (fat_dirent_cache*)desc; - fat_dirent_cache* new_parent = (fat_dirent_cache*)new_parent_desc; - fat_cache* cache = cache_entry->owner; - - if (!new_parent || new_parent == cache_entry->fdc_parent) - { - deref_dirent(cache_entry); - OBOS_FreeString(&cache_entry->name); - OBOS_InitString(&cache_entry->name, name); - gen_short_name(OBOS_GetStringCPtr(&cache_entry->name), &cache_entry->data.filename_83[0], cache_entry->fdc_parent, cache_entry); - Core_MutexAcquire(&cache->fat_lock); - Core_MutexAcquire(&cache->fd_lock); - ref_dirent(cache_entry); - Core_MutexRelease(&cache->fd_lock); - Core_MutexRelease(&cache->fat_lock); - } - else - { - deref_dirent(cache_entry); - if (name) - { - OBOS_FreeString(&cache_entry->name); - OBOS_InitString(&cache_entry->name, name); - gen_short_name(OBOS_GetStringCPtr(&cache_entry->name), &cache_entry->data.filename_83[0], cache_entry->fdc_parent, cache_entry); - } - CacheRemoveChild(cache_entry->fdc_parent, cache_entry); - CacheAppendChild(new_parent, cache_entry); - Core_MutexAcquire(&cache->fat_lock); - Core_MutexAcquire(&cache->fd_lock); - ref_dirent(cache_entry); - Core_MutexRelease(&cache->fd_lock); - Core_MutexRelease(&cache->fat_lock); - } - - Vfs_FdFlush(cache->volume); - return OBOS_STATUS_SUCCESS; -} -obos_status remove_file(dev_desc desc) -{ - if (!desc) - return OBOS_STATUS_INVALID_ARGUMENT; - fat_dirent_cache* cache_entry = (fat_dirent_cache*)desc; - if (cache_entry->fdc_children.nChildren) - return OBOS_STATUS_IN_USE; // we cannot remove a directory with children. - fat_cache* cache = cache_entry->owner; - /* - To remove a file we need to: - - remove the dirent - - remove all LFN entries associated with it - - remove the cache entry associated with it - - free any clusters used by it - */ - deref_dirent(cache_entry); - const size_t bytesPerCluster = (cache->bpb->sectorsPerCluster*cache->blkSize); - const uint32_t szClusters = ((cache_entry->data.filesize / bytesPerCluster) + ((cache_entry->data.filesize % bytesPerCluster) != 0)); - FreeClusters(cache, (uint32_t)cache_entry->data.first_cluster_low|((uint32_t)cache_entry->data.first_cluster_high<<16), szClusters); - CacheRemoveChild(cache_entry->fdc_parent, cache_entry); - cache->fileCount--; - FATAllocator->Free(FATAllocator, cache_entry, sizeof(*cache_entry)); - Vfs_FdFlush(cache->volume); - return OBOS_STATUS_SUCCESS; -} diff --git a/src/drivers/generic/slowfat/fat_irp.c b/src/drivers/generic/slowfat/fat_irp.c deleted file mode 100644 index 0c45da26..00000000 --- a/src/drivers/generic/slowfat/fat_irp.c +++ /dev/null @@ -1,224 +0,0 @@ -/* - * drivers/generic/slowfat/fat_irp.c - * - * Copyright (c) 2025 Omar Berrow -*/ - -#include -#include -#include -#include - -#include - -#include - -#include "structs.h" -#include "alloc.h" - -struct irp_clus_node { - uint32_t cluster; - size_t nClusters; - size_t ioOffset; - size_t ioLimit; - struct irp_clus_node *next, *prev; -}; -struct slowfat_irp_data { - fat_cache* cache; - uint8_t *iter; - irp *disk_irp, *parent_irp; - struct irp_clus_node *clus_head; - struct irp_clus_node *clus_tail; -}; - -#define bytesPerCluster(cache) ((cache)->bpb->sectorsPerCluster*(cache)->blkSize) - -iterate_decision cluster_list_populate_cb(uint32_t cluster, obos_status status, void* userdata) -{ - OBOS_UNUSED(status); - - uintptr_t* udata = userdata; - struct slowfat_irp_data* const irp_data = (void*)udata[0]; - int64_t* const blkCount = (int64_t*)udata[1]; - fat_cache* cache = (void*)udata[2]; - uintptr_t const realBlkCount = udata[3]; - uintptr_t const blkOffset = udata[4]; - - struct irp_clus_node *node = nullptr; - if (irp_data->clus_tail && (irp_data->clus_tail->cluster + irp_data->clus_tail->nClusters) == cluster) - { - node = irp_data->clus_tail; - node->nClusters++; - goto down; - } - node = ZeroAllocate(FATAllocator, 1, sizeof(struct irp_clus_node), nullptr); - node->cluster = cluster; - node->nClusters++; - - node->ioLimit = bytesPerCluster(cache)*node->nClusters; - if (!irp_data->clus_head) - { - node->ioOffset = blkOffset % bytesPerCluster(cache); - node->ioLimit -= node->ioOffset; - } - - if (!irp_data->clus_head) - irp_data->clus_head = node; - if (irp_data->clus_tail) - irp_data->clus_tail->next = node; - node->prev = irp_data->clus_tail; - irp_data->clus_tail = node; - - down: - (*blkCount) -= bytesPerCluster(cache); - if ((*blkCount) <= 0) - { - node->ioLimit = (bytesPerCluster(cache)*(node->nClusters-1)) + (realBlkCount % bytesPerCluster(cache)); - return ITERATE_DECISION_STOP; - } - return ITERATE_DECISION_CONTINUE; -} - -void read_irp_event_set_cb(irp* req) -{ - // Before destroying the IRP, check if we need to copy any data, - // or if status == IRP_RETRY, or if the IRP failed (in which case, propagate the error). - - struct slowfat_irp_data* irp_data = req->drvData; - OBOS_ENSURE(irp_data); - - req->status = OBOS_STATUS_IRP_RETRY; - - if (irp_data->disk_irp) - { - if (irp_data->disk_irp->on_event_set) - irp_data->disk_irp->on_event_set(irp_data->disk_irp); - if (irp_data->disk_irp->status == OBOS_STATUS_IRP_RETRY) - return; - if (obos_is_error(irp_data->disk_irp->status)) - { - req->status = irp_data->disk_irp->status; - return; - } - } - - fat_cache* cache = irp_data->cache; - - if (irp_data->disk_irp) - { - if (((irp_data->clus_head->ioLimit % bytesPerCluster(cache)) || irp_data->clus_head->ioOffset)) - { - memcpy(irp_data->iter, irp_data->disk_irp->buff + irp_data->clus_head->ioOffset, irp_data->clus_head->ioLimit); - Free(FATAllocator, irp_data->disk_irp->buff, irp_data->clus_head->nClusters * bytesPerCluster(cache)); - } - irp_data->iter += irp_data->clus_head->ioLimit; - req->nBlkRead += irp_data->clus_head->ioLimit; - - if (irp_data->clus_head->next) - irp_data->clus_head->next->prev = irp_data->clus_head->prev; - if (irp_data->clus_head->prev) - irp_data->clus_head->prev->next = irp_data->clus_head->next; - irp_data->clus_head = irp_data->clus_head->next; - if (irp_data->clus_tail == irp_data->clus_head) - irp_data->clus_tail = nullptr; - if (!irp_data->clus_head) - { - // The read has finished. - req->status = OBOS_STATUS_SUCCESS; - return; - } - } - - if (!irp_data->disk_irp) - irp_data->disk_irp = VfsH_IRPAllocate(); - else - { - memzero(irp_data->disk_irp, sizeof(irp)); - irp_data->disk_irp->refs = 1; - } - irp* new_irp = irp_data->disk_irp; - /* - * If the clus_head->ioLimit != nClusters*bytesPerCluster, then we should use a separate side buffer to read the entire cluster(s), - * and then copy it into irp_data->iter, but only copy ioLimit. - * If clus_head->ioOffset != 0, then we should also use a separate side buffer, but instead of copying ioLimit bytes from - * tmpBuff+0, copy ioLimit bytes from tmpBuff + ioOffset - */ - VfsH_IRPBytesToBlockCount(cache->vn, irp_data->clus_head->nClusters*bytesPerCluster(cache), &new_irp->blkCount); - VfsH_IRPBytesToBlockCount(cache->vn, ClusterToSector(cache, irp_data->clus_head->cluster)*cache->blkSize, &new_irp->blkOffset); - new_irp->op = IRP_READ; - new_irp->vn = cache->vn; - new_irp->dryOp = req->dryOp; - if ((irp_data->clus_head->ioLimit % bytesPerCluster(cache)) || irp_data->clus_head->ioOffset) - { - size_t buffSz = irp_data->clus_head->nClusters * bytesPerCluster(cache); - new_irp->buff = Allocate(FATAllocator, buffSz, nullptr); - } - else - new_irp->buff = irp_data->iter; - VfsH_IRPSubmit(new_irp, nullptr); - req->evnt = new_irp->evnt; - - req->status = OBOS_STATUS_IRP_RETRY; -} -obos_status submit_irp(void* request) -{ - irp* req = request; - if (!req) - return OBOS_STATUS_INVALID_ARGUMENT; - - if (!req->desc || !req->buff) - return OBOS_STATUS_INVALID_ARGUMENT; - - if (!req->blkCount) - { - req->evnt = nullptr; - req->status = OBOS_STATUS_SUCCESS; - return OBOS_STATUS_SUCCESS; - } - - fat_dirent_cache* cache_entry = (fat_dirent_cache*)req->desc; - fat_cache* cache = cache_entry->owner; - if (cache_entry->data.attribs & DIRECTORY) - return OBOS_STATUS_NOT_A_FILE; - - struct slowfat_irp_data* irp_data = ZeroAllocate(FATAllocator, 1, sizeof(struct slowfat_irp_data), nullptr); - irp_data->parent_irp = req; - irp_data->cache = cache; - req->drvData = irp_data; - // Form a cluster list, if reading. - if (req->op == IRP_READ) - { - if (req->blkOffset >= cache_entry->data.filesize) - { - req->nBlkRead = 0; - return OBOS_STATUS_SUCCESS; - } - if ((req->blkOffset + req->blkCount) >= cache_entry->data.filesize) - req->blkCount = cache_entry->data.filesize - req->blkOffset; - uint32_t cluster = cache_entry->data.first_cluster_low; - if (cache->fatType == FAT32_VOLUME) - cluster |= ((uint32_t)cache_entry->data.first_cluster_high << 16); - cluster = ClusterSeek(cache, cluster, req->blkOffset/bytesPerCluster(cache)); - if (cluster == UINT32_MAX) - { - req->status = OBOS_STATUS_INVALID_ARGUMENT; // TODO: Handle properly. - Free(FATAllocator, irp_data, sizeof(*irp_data)); - req->drvData = nullptr; - return OBOS_STATUS_SUCCESS; - } - int64_t tmpBlkCount = req->blkCount; - if (tmpBlkCount % bytesPerCluster(cache)) - tmpBlkCount += (bytesPerCluster(cache) - (tmpBlkCount % bytesPerCluster(cache))); - uintptr_t udata[] = { (uintptr_t)irp_data, (uintptr_t)&tmpBlkCount, (uintptr_t)cache, req->blkCount, req->blkOffset }; - FollowClusterChain(cache, cluster, cluster_list_populate_cb, &udata); - irp_data->iter = req->buff; - req->on_event_set = read_irp_event_set_cb; - read_irp_event_set_cb(req); - } - else - return OBOS_STATUS_UNIMPLEMENTED; - - return OBOS_STATUS_SUCCESS; -} - -OBOS_WEAK obos_status finalize_irp(void* request); \ No newline at end of file diff --git a/src/drivers/generic/slowfat/interface.c b/src/drivers/generic/slowfat/interface.c deleted file mode 100644 index efd91ac7..00000000 --- a/src/drivers/generic/slowfat/interface.c +++ /dev/null @@ -1,133 +0,0 @@ -/* - * drivers/generic/slowfat/interface.c - * - * Copyright (c) 2024 Omar Berrow - * - * Abandon all hope ye who enter here -*/ - -#include -#include -#include -#include - -#include - -#include "structs.h" - -obos_status get_max_blk_count(dev_desc desc, size_t* count) -{ - if (!desc || !count) - return OBOS_STATUS_INVALID_ARGUMENT; - fat_dirent_cache* cache_entry = (fat_dirent_cache*)desc; - *count = cache_entry->data.filesize; - return OBOS_STATUS_SUCCESS; -} -obos_status query_path(dev_desc desc, const char** path) -{ - if (!desc || !path) - return OBOS_STATUS_INVALID_ARGUMENT; - fat_dirent_cache* cache_entry = (fat_dirent_cache*)desc; - *path = OBOS_GetStringCPtr(&cache_entry->path); - return OBOS_STATUS_SUCCESS; -} -obos_status path_search(dev_desc* found, void* vn_, const char* what, dev_desc parent) -{ - if (!found || !vn_ || !what) - return OBOS_STATUS_INVALID_ARGUMENT; - fat_cache* cache = nullptr; - for (cache = LIST_GET_HEAD(fat_cache_list, &FATVolumes); cache; ) - { - if (cache->vn == vn_) - break; - - cache = LIST_GET_NEXT(fat_cache_list, &FATVolumes, cache); - } - if (!cache && parent != UINTPTR_MAX) - return OBOS_STATUS_INVALID_OPERATION; // not a fat volume we have probed - *found = (dev_desc)DirentLookupFrom(what, parent != UINTPTR_MAX ? (fat_dirent_cache*)parent : cache->root); - if (*found) - return OBOS_STATUS_SUCCESS; - return OBOS_STATUS_NOT_FOUND; -} -obos_status get_linked_path(dev_desc desc, const char** found) -{ - OBOS_UNUSED(desc); - OBOS_UNUSED(found); - return OBOS_STATUS_INTERNAL_ERROR; // Impossible, as we don't know of such thing. -} -obos_status set_file_perms(dev_desc desc, driver_file_perm newperm) -{ - if (!desc) - return OBOS_STATUS_INVALID_ARGUMENT; - fat_dirent_cache* cache_entry = (fat_dirent_cache*)desc; - if (!newperm.owner_write || !newperm.group_write) - cache_entry->data.attribs |= READ_ONLY; - return WriteFatDirent(cache_entry->owner, cache_entry, true); -} -obos_status get_file_perms(dev_desc desc, driver_file_perm *perm) -{ - if (!desc || !perm) - return OBOS_STATUS_INVALID_ARGUMENT; - fat_dirent_cache* cache_entry = (fat_dirent_cache*)desc; - static const driver_file_perm base_perm = { - .owner_read=true, - .group_read=true, - .other_read=false, - .owner_write=true, - .group_write=true, - .other_write=false, - .owner_exec=true, - .group_exec=true, - .other_exec=true, - }; - *perm = base_perm; - if (cache_entry->data.attribs & READ_ONLY) - { - perm->owner_write = false; - perm->other_write = false; - perm->group_write = false; - } - return OBOS_STATUS_SUCCESS; -} -obos_status get_file_type(dev_desc desc, file_type *type) -{ - if (!desc || !type) - return OBOS_STATUS_INVALID_ARGUMENT; - fat_dirent_cache* cache_entry = (fat_dirent_cache*)desc; - if (cache_entry->data.attribs & DIRECTORY) - *type = FILE_TYPE_DIRECTORY; - else - *type = FILE_TYPE_REGULAR_FILE; - return OBOS_STATUS_SUCCESS; -} -obos_status list_dir(dev_desc dir, void* vn, iterate_decision(*cb)(dev_desc desc, size_t blkSize, size_t blkCount, void* userdata, const char* name), void* userdata) -{ - if (!dir || !vn || !cb) - return OBOS_STATUS_INVALID_ARGUMENT; - fat_cache* cache = nullptr; - for (cache = LIST_GET_HEAD(fat_cache_list, &FATVolumes); cache; ) - { - if (cache->vn == vn) - break; - - cache = LIST_GET_NEXT(fat_cache_list, &FATVolumes, cache); - } - if (!cache) - return OBOS_STATUS_INVALID_OPERATION; // not a fat volume we have probed - if (dir == UINTPTR_MAX) - dir = (dev_desc)cache->root; - for (fat_dirent_cache* cache_entry = ((fat_dirent_cache*)dir)->fdc_children.head; cache_entry; ) - { - if (cache_entry->data.attribs & VOLUME_ID) - { - cache_entry = cache_entry->fdc_next_child; - continue; - } - OBOS_ASSERT(cache_entry->data.attribs != LFN); - if (cb((dev_desc)cache_entry, 1, cache_entry->data.filesize, userdata, OBOS_GetStringCPtr(&cache_entry->name)) == ITERATE_DECISION_STOP) - break; - cache_entry = cache_entry->fdc_next_child; - } - return OBOS_STATUS_SUCCESS; -} diff --git a/src/drivers/generic/slowfat/io.c b/src/drivers/generic/slowfat/io.c deleted file mode 100644 index 83514668..00000000 --- a/src/drivers/generic/slowfat/io.c +++ /dev/null @@ -1,258 +0,0 @@ -/* - * drivers/generic/slowfat/io.c - * - * Copyright (c) 2024-2025 Omar Berrow - * - * Abandon all hope ye who enter here -*/ - -#include -#include -#include -#include - -#include -#include -#include -#include - -#include - -#include -#include - -#include - -#include - -#include - -#include "structs.h" -#include "alloc.h" - -static iterate_decision read_callback(uint32_t cluster, obos_status stat, void* udata_) -{ - uintptr_t* udata = udata_; - uint8_t* buf = (void*)udata[0]; - size_t i = udata[1]++; - size_t nClustersToRead = udata[2]; - int64_t bytesLeft = udata[3]; - size_t current_offset = udata[4]; - size_t cluster_offset = udata[5]; - fat_cache* cache = (void*)udata[6]; - obos_status* status = (void*)udata[7]; - const uint8_t* cluster_buf = nullptr; - const size_t bytesPerCluster = (cache->bpb->sectorsPerCluster*cache->blkSize); - if (stat != OBOS_STATUS_EOF && stat != OBOS_STATUS_SUCCESS) - { - *status = stat; - return ITERATE_DECISION_STOP; - } - cluster_buf = VfsH_PageCacheGetEntry(cache->volume->vn, ClusterToSector(cache, cluster)*cache->blkSize, false); - if (obos_is_error(*status)) - { - Core_MutexRelease(&cache->fd_lock); - return ITERATE_DECISION_STOP; - } - memcpy(buf+current_offset, cluster_buf + cluster_offset, (size_t)bytesLeft <= bytesPerCluster ? (size_t)bytesLeft : bytesPerCluster); - - current_offset += bytesPerCluster; - // cluster_offset += bytesPerCluster; - cluster_offset = 0; - bytesLeft -= bytesPerCluster; - if (i >= nClustersToRead) - return ITERATE_DECISION_STOP; - if (bytesLeft < 0) - return ITERATE_DECISION_STOP; - return ITERATE_DECISION_CONTINUE; -} -obos_status read_sync(dev_desc desc, void* buf_, size_t blkCount, size_t blkOffset, size_t* nBlkRead) -{ - if (!desc || !buf_) - return OBOS_STATUS_INVALID_ARGUMENT; - if (!blkCount) - return OBOS_STATUS_SUCCESS; - fat_dirent_cache* cache_entry = (fat_dirent_cache*)desc; - fat_cache* cache = cache_entry->owner; - if (cache_entry->data.attribs & DIRECTORY) - return OBOS_STATUS_NOT_A_FILE; - if (blkOffset >= cache_entry->data.filesize) - { - if (nBlkRead) - *nBlkRead = 0; - return OBOS_STATUS_SUCCESS; - } - size_t nToRead = blkCount; - if ((blkOffset + blkCount) >= cache_entry->data.filesize) - nToRead = cache_entry->data.filesize - blkOffset; - const size_t bytesPerCluster = (cache->bpb->sectorsPerCluster*cache->blkSize); - size_t nClustersToRead = (nToRead / bytesPerCluster) + (((nToRead % bytesPerCluster) != 0) ? 1 : 0); - uint32_t cluster = cache_entry->data.first_cluster_low; - if (cache->fatType == FAT32_VOLUME) - cluster |= ((uint32_t)cache_entry->data.first_cluster_high << 16); - cluster = ClusterSeek(cache, cluster, blkOffset/bytesPerCluster); - if (cluster == UINT32_MAX) - return OBOS_STATUS_INVALID_ARGUMENT; // TODO: Handle properly. - size_t current_offset = 0; - size_t cluster_offset = blkOffset % bytesPerCluster; - int64_t bytesLeft = blkCount; - uint8_t *buf = buf_; - Core_MutexAcquire(&cache->fd_lock); - obos_status status = OBOS_STATUS_SUCCESS; - uintptr_t udata[8] = { - (uintptr_t)buf, - (uintptr_t)0, /* i */ - (uintptr_t)nClustersToRead, - (uintptr_t)bytesLeft, - (uintptr_t)current_offset, - (uintptr_t)cluster_offset, - (uintptr_t)cache, - (uintptr_t)&status, - }; - FollowClusterChain(cache, cluster, read_callback, udata); - Core_MutexRelease(&cache->fd_lock); - if (nBlkRead) - *nBlkRead = (blkCount-(bytesLeft >= 0 ? bytesLeft : 0)); - return status; -} -obos_status write_sync(dev_desc desc, const void* buf_, size_t blkCount, size_t blkOffset, size_t* nBlkWritten) -{ - if (!desc || !buf_) - return OBOS_STATUS_INVALID_ARGUMENT; - if (!blkCount) - return OBOS_STATUS_SUCCESS; - fat_dirent_cache* cache_entry = (fat_dirent_cache*)desc; - fat_cache* cache = cache_entry->owner; - if (cache_entry->data.attribs & DIRECTORY) - return OBOS_STATUS_INVALID_ARGUMENT; - bool requiresExpand = false; - bool expandClusters = false; - // TODO: Use block chain length. - const size_t bytesPerCluster = (cache->bpb->sectorsPerCluster*cache->blkSize); - if ((blkOffset+blkCount) > cache_entry->data.filesize) - requiresExpand = true; - size_t filesz_cluster_aligned = cache_entry->data.filesize; - if (filesz_cluster_aligned % bytesPerCluster) - filesz_cluster_aligned += bytesPerCluster-(filesz_cluster_aligned%bytesPerCluster); - if ((blkOffset+blkCount) >= filesz_cluster_aligned) - requiresExpand = expandClusters = true; - size_t nToWrite = blkCount; - size_t nClustersToWrite = (nToWrite / bytesPerCluster) + (((nToWrite % bytesPerCluster) != 0) ? 1 : 0); - uint32_t cluster = cache_entry->data.first_cluster_low; - if (cache->fatType == FAT32_VOLUME) - cluster |= ((uint32_t)cache_entry->data.first_cluster_high << 16); - cluster = ClusterSeek(cache, cluster, blkOffset/bytesPerCluster); - if (cluster == UINT32_MAX) - return OBOS_STATUS_INVALID_ARGUMENT; // TODO: Handle properly. - page* pg = nullptr; - if (requiresExpand) - { - uint32_t szClusters = ((cache_entry->data.filesize / bytesPerCluster) + ((cache_entry->data.filesize % bytesPerCluster) != 0)); - cache_entry->data.filesize += blkCount; - uint32_t newSizeCls = ((cache_entry->data.filesize / bytesPerCluster) + ((cache_entry->data.filesize % bytesPerCluster) != 0)); - Core_MutexAcquire(&cache->fat_lock); - if (expandClusters) - if (!ExtendClusters(cache, cluster, newSizeCls, szClusters)) - { - uint32_t newCluster = AllocateClusters(cache, newSizeCls); - if (newCluster == UINT32_MAX) - { - Core_MutexRelease(&cache->fat_lock); - return OBOS_STATUS_NOT_ENOUGH_MEMORY; - } - Core_MutexAcquire(&cache->fd_lock); - for (size_t i = 0; i < szClusters; i++) - { - Vfs_FdSeek(cache->volume, ClusterToSector(cache, cluster+i)*cache->blkSize, SEEK_SET); - const void* src = VfsH_PageCacheGetEntry(cache->volume->vn, ClusterToSector(cache, cluster+i)*cache->blkSize, nullptr); - void* dest = VfsH_PageCacheGetEntry(cache->volume->vn, ClusterToSector(cache, newCluster+i)*cache->blkSize, &pg); - memcpy(dest, src, bytesPerCluster); - Mm_MarkAsDirtyPhys(pg); - } - if (cluster) - FreeClusters(cache, cluster, szClusters); - cluster = newCluster; - cache_entry->data.first_cluster_high = cluster >> 16; - cache_entry->data.first_cluster_low = cluster & 0xffff; - Core_MutexRelease(&cache->fd_lock); - } - Core_MutexRelease(&cache->fat_lock); - WriteFatDirent(cache, cache_entry, true); - } - size_t current_offset = 0; - size_t cluster_offset = blkOffset % bytesPerCluster; - int64_t bytesLeft = blkCount; - Core_MutexAcquire(&cache->fd_lock); - const uint8_t *buf = buf_; - for (size_t i = 0; i < nClustersToWrite && bytesLeft > 0; i++) - { - void* cluster_buf = VfsH_PageCacheGetEntry(cache->volume->vn, ClusterToSector(cache, cluster)*cache->blkSize, &pg); - memcpy(cluster_buf + cluster_offset, buf+current_offset, ((size_t)bytesLeft <= bytesPerCluster ? (size_t)bytesLeft : bytesPerCluster-cluster_offset)); - Mm_MarkAsDirtyPhys(pg); - - current_offset += bytesPerCluster; - cluster_offset = 0; - - // Get the next cluster to write. - uint32_t next = 0; - fat_entry_addr addr = {}; - GetFatEntryAddrForCluster(cache, cluster, &addr); - uint8_t* sector = VfsH_PageCacheGetEntry(cache->vn, addr.lba*cache->blkSize, nullptr); - NextCluster(cache, cluster, sector, &next); - - cluster = next; - bytesLeft -= bytesPerCluster; - } - Vfs_FdFlush(cache->volume); - Core_MutexRelease(&cache->fd_lock); - if (nBlkWritten) - *nBlkWritten = blkCount; - return OBOS_STATUS_SUCCESS; -} -obos_status trunc_file(dev_desc desc, size_t blkCount) -{ - if (!desc) - return OBOS_STATUS_INVALID_ARGUMENT; - fat_dirent_cache* cache_entry = (fat_dirent_cache*)desc; - fat_cache* cache = cache_entry->owner; - if (cache_entry->data.filesize == blkCount) - return OBOS_STATUS_SUCCESS; - OBOS_ASSERT(cache_entry->data.filesize >= blkCount); - if (blkCount > cache_entry->data.filesize) - return OBOS_STATUS_INVALID_ARGUMENT; - const size_t bytesPerCluster = (cache->bpb->sectorsPerCluster*cache->blkSize); - uint32_t szClusters = ((cache_entry->data.filesize / bytesPerCluster) + ((cache_entry->data.filesize % bytesPerCluster) != 0)); - cache_entry->data.filesize = blkCount; - uint32_t newSizeCls = ((cache_entry->data.filesize / bytesPerCluster) + ((cache_entry->data.filesize % bytesPerCluster) != 0)); - uint32_t cluster = cache_entry->data.first_cluster_low; - if (cache->fatType == FAT32_VOLUME) - cluster |= ((uint32_t)cache_entry->data.first_cluster_high << 16); - Core_MutexAcquire(&cache->fat_lock); - TruncateClusters(cache, cluster, newSizeCls, szClusters); - if (!newSizeCls) - { - FreeClusters(cache, cluster, szClusters); - cache_entry->data.first_cluster_low = 0; - cache_entry->data.first_cluster_high = 0; - } - Core_MutexRelease(&cache->fat_lock); - WriteFatDirent(cache, cache_entry, true); - Vfs_FdFlush(cache->volume); - return OBOS_STATUS_SUCCESS; -} -obos_status WriteFatDirent(fat_cache* cache, fat_dirent_cache* cache_entry, bool lock) -{ - const size_t bytesPerCluster = (cache->bpb->sectorsPerCluster*cache->blkSize); - if (lock) - Core_MutexAcquire(&cache->fd_lock); - Vfs_FdSeek(cache->volume, cache_entry->dirent_fileoff, SEEK_SET); - page* pg = nullptr; - uint8_t* cluster_buf = VfsH_PageCacheGetEntry(cache->volume->vn, cache_entry->dirent_fileoff, &pg); - memcpy(cluster_buf+cache_entry->dirent_offset, &cache_entry->data, sizeof(cache_entry->data)); - Mm_MarkAsDirtyPhys(pg); - if (lock) - Core_MutexRelease(&cache->fd_lock); - FATAllocator->Free(FATAllocator, cluster_buf, bytesPerCluster); - Vfs_FdFlush(cache->volume); - return OBOS_STATUS_SUCCESS; -} \ No newline at end of file diff --git a/src/drivers/generic/slowfat/lookup.c b/src/drivers/generic/slowfat/lookup.c deleted file mode 100644 index ec5391ac..00000000 --- a/src/drivers/generic/slowfat/lookup.c +++ /dev/null @@ -1,98 +0,0 @@ -/* - * drivers/generic/slowfat/lookup.c - * - * Copyright (c) 2024 Omar Berrow - * - * Abandon all hope ye who enter here -*/ - -#include -#include - -#include "structs.h" - -// Adapated from vfs/dirent.c - -static size_t str_search(const char* str, char ch) -{ - size_t ret = strchr(str, ch); - for (; str[ret] == ch && str[ret] != 0; ret++) - ; - return ret; -} -static fat_dirent_cache* on_match(fat_dirent_cache** const curr_, fat_dirent_cache** const root, const char** const tok, size_t* const tok_len, const char** const path, - size_t* const path_len) -{ - fat_dirent_cache *curr = *curr_; - *root = curr; - const char *newtok = (*tok) + str_search(*tok, '/'); - if (newtok >= (*path + *path_len)) - return curr; - if (!curr->fdc_children.nChildren) - return nullptr; // could not find node. - *tok = newtok; - size_t currentPathLen = strlen(*tok)-1; - if ((*tok)[currentPathLen] != '/') - currentPathLen++; - while ((*tok)[currentPathLen] == '/') - currentPathLen--; - *tok_len = strchr(*tok, '/'); - if (*tok_len != currentPathLen) - (*tok_len)--; - while ((*tok)[(*tok_len) - 1] == '/') - (*tok_len)--; - return nullptr; -} -fat_dirent_cache* DirentLookupFrom(const char* path, fat_dirent_cache* root) -{ - if (!path) - return nullptr; - size_t path_len = strlen(path); - if (!path_len) - return nullptr; - for (; *path == '/'; path++, path_len--) - ; - const char* tok = path; - size_t tok_len = strchr(tok, '/'); - if (tok_len != path_len) - tok_len--; - while (tok[tok_len - 1] == '/') - tok_len--; - if (!tok_len) - return nullptr; - while(root) - { - fat_dirent_cache* curr = root; - if (OBOS_CompareStringNC(&root->name, tok, tok_len)) - { - root = curr->fdc_children.head; - continue; - } - - for (curr = root->fdc_children.head; curr;) - { - if (OBOS_CompareStringNC(&curr->name, tok, tok_len)) - { - // Match! - fat_dirent_cache* what = - on_match(&curr, &root, &tok, &tok_len, &path, &path_len); - if (what) - return what; - else if ((tok+tok_len) >= (path+path_len) && !curr->fdc_next_child) - return nullptr; - curr = curr->fdc_children.head ? curr->fdc_children.head : curr; - break; - } - - // root = curr->fdc_children.head ? curr->fdc_children.head : root; - curr = curr->fdc_next_child; - } - if (!curr) - { - if ((tok+tok_len) >= (path+path_len)) - break; - root = root->fdc_parent; - } - } - return nullptr; -} \ No newline at end of file diff --git a/src/drivers/generic/slowfat/main.c b/src/drivers/generic/slowfat/main.c deleted file mode 100644 index b17b7321..00000000 --- a/src/drivers/generic/slowfat/main.c +++ /dev/null @@ -1,93 +0,0 @@ -/* - * drivers/generic/slowfat/main.c - * - * Copyright (c) 2024 Omar Berrow - * - * Abandon all hope ye who enter here -*/ - -#include -#include - -#include - -#include - -#include - -OBOS_PAGEABLE_FUNCTION obos_status get_blk_size(dev_desc desc, size_t* blkSize) -{ - OBOS_UNUSED(desc); - if (!blkSize) - return OBOS_STATUS_INVALID_ARGUMENT; - *blkSize = 1; - return OBOS_STATUS_SUCCESS; -} -OBOS_WEAK obos_status get_max_blk_count(dev_desc desc, size_t* count); -OBOS_WEAK obos_status read_sync(dev_desc desc, void* buf, size_t blkCount, size_t blkOffset, size_t* nBlkRead); -OBOS_WEAK obos_status write_sync(dev_desc desc, const void* buf, size_t blkCount, size_t blkOffset, size_t* nBlkWritten); -OBOS_WEAK obos_status foreach_device(iterate_decision(*cb)(dev_desc desc, size_t blkSize, size_t blkCount, void* u), void* u); -OBOS_WEAK obos_status query_user_readable_name(dev_desc what, const char** name); // unrequired for fs drivers. -OBOS_PAGEABLE_FUNCTION obos_status ioctl(dev_desc what, uint32_t request, void* argp) -{ - OBOS_UNUSED(what); - OBOS_UNUSED(request); - OBOS_UNUSED(argp); - return OBOS_STATUS_INVALID_IOCTL; -} -void driver_cleanup_callback() -{ -} -OBOS_WEAK obos_status query_path(dev_desc desc, const char** path); -OBOS_WEAK obos_status path_search(dev_desc* found, void*, const char* what, dev_desc parent); -OBOS_WEAK obos_status get_linked_path(dev_desc desc, const char** found); -OBOS_WEAK obos_status move_desc_to(dev_desc desc, dev_desc new_parent, const char* name); -OBOS_WEAK obos_status mk_file(dev_desc* newDesc, dev_desc parent, void* vn, const char* name, file_type type, driver_file_perm perm); -OBOS_WEAK obos_status remove_file(dev_desc desc); -OBOS_WEAK obos_status trunc_file(dev_desc desc, size_t newsize); -OBOS_WEAK obos_status set_file_perms(dev_desc desc, driver_file_perm newperm); -OBOS_WEAK obos_status get_file_perms(dev_desc desc, driver_file_perm *perm); -OBOS_WEAK obos_status get_file_type(dev_desc desc, file_type *type); -OBOS_WEAK obos_status list_dir(dev_desc dir, void* vn, iterate_decision(*cb)(dev_desc desc, size_t blkSize, size_t blkCount, void* userdata, const char* name), void* userdata); -OBOS_WEAK bool probe(void* vn); -OBOS_WEAK obos_status stat_fs_info(void *vn, drv_fs_info *info); -OBOS_WEAK obos_status submit_irp(void* request); -OBOS_WEAK obos_status finalize_irp(void* request); - -__attribute__((section(OBOS_DRIVER_HEADER_SECTION))) driver_header drv_hdr = { - .magic = OBOS_DRIVER_MAGIC, - .flags = DRIVER_HEADER_HAS_STANDARD_INTERFACES|DRIVER_HEADER_FLAGS_NO_ENTRY, - .ftable = { - .driver_cleanup_callback = driver_cleanup_callback, - .ioctl = ioctl, - .get_blk_size = get_blk_size, - .get_max_blk_count = get_max_blk_count, - .query_user_readable_name = query_user_readable_name, - .foreach_device = foreach_device, - .read_sync = read_sync, - .write_sync = write_sync, - .submit_irp = submit_irp, - .finalize_irp = finalize_irp, - - .query_path = query_path, - .path_search = path_search, - .get_linked_path = get_linked_path, - .move_desc_to = move_desc_to, - .mk_file = mk_file, - .remove_file = remove_file, - .trunc_file = trunc_file, - .get_file_perms = get_file_perms, - .set_file_perms = set_file_perms, - .get_file_type = get_file_type, - .list_dir = list_dir, - .stat_fs_info = stat_fs_info, - .probe = probe, - }, - .driverName = "FAT Driver", -}; -// allocator_info* FATAllocator = nullptr; -// void OBOS_DriverEntry() -// { -// FATAllocator = OBOS_KernelAllocator; -// Core_ExitCurrentThread(); -// } diff --git a/src/drivers/generic/slowfat/probe.c b/src/drivers/generic/slowfat/probe.c deleted file mode 100644 index 0cf61917..00000000 --- a/src/drivers/generic/slowfat/probe.c +++ /dev/null @@ -1,383 +0,0 @@ -/* - * drivers/generic/slowfat/probe.c - * - * Copyright (c) 2024 Omar Berrow - * - * Abandon all hope ye who enter here -*/ - -#include -#include -#include -#include - -#include -#include -#include - -#include - -#include -#include - -#include - -#include - -#include - -#include "structs.h" -#include "alloc.h" - -static char lfn_at(const lfn_dirent* lfn, size_t i) -{ - char ret = 0; - if (i < 5) - ret = lfn->name1[i]; - else if (i >= 5 && i < 11) - ret = lfn->name2[i - 5]; - else if (i == 11 || i == 12) - ret = lfn->name3[i - 11]; - if (ret < 0) - ret = 0; - return ret; -} -static size_t lfn_strlen(const lfn_dirent* lfn) -{ - size_t ret = 0; - for (; lfn_at(lfn, ret) && ret < 13; ret++); - return ret; -} -static void dir_iterate(fat_cache* cache, fat_dirent_cache* parent, uint32_t cluster); -static void process_dirent(fat_cache* cache, fat_dirent_cache* const parent, uint32_t cluster, const void* buff, const fat_dirent* curr, lfn_dirent*** const lfn_entries, size_t* const lfn_entry_count, string* current_filename) -{ - OBOS_UNUSED(cluster); - if ((uint8_t)curr->filename_83[0] == 0xE5) - return; - if (curr->attribs & LFN) - { - lfn_dirent* lfn = (lfn_dirent*)curr; - // NOTE(oberrow): Do not check if there was already a chain before, just truncate the list. - if ((lfn->order & 0x40)) - { - // Allocate all the memory we'll need for this LFN chain. - size_t old_entry_count = *lfn_entry_count; - *lfn_entry_count = (lfn->order & ~0x40 /* last entry */); - *lfn_entries = FATAllocator->Reallocate(FATAllocator, *lfn_entries, sizeof(lfn_dirent)*(*lfn_entry_count), old_entry_count*sizeof(lfn_dirent), nullptr); - } - (*lfn_entries)[(lfn->order & ~0x40) - 1] = lfn; - return; - } - char filename_83[11]; - memcpy(filename_83, curr->filename_83, 11); - if (filename_83[0] == 0x5) - filename_83[0] = 0xE5; - - if (memcmp_b(&filename_83[0], '.', 2)) - return; - if (filename_83[0] == '.') - return; - if (*lfn_entry_count) - { - for (size_t i = 0; i < (*lfn_entry_count); i++) - { - lfn_dirent* lfn = (*lfn_entries)[i]; - if (!lfn) - continue; - size_t len = lfn_strlen(lfn); - char ch[2] = {}; - for (size_t j = 0; j < len; j++) - { - ch[0] = lfn_at(lfn, j); - if (!ch[0]) - continue; - OBOS_AppendStringC(current_filename, ch); - } - } - FATAllocator->Free(FATAllocator, *lfn_entries, (*lfn_entry_count) * sizeof(lfn_dirent*)); - *lfn_entry_count = 0; - *lfn_entries = nullptr; - } - else - { - char ch[2] = {}; - size_t len = 0; - for (len = 0; len < 8 && filename_83[len] != ' ' && filename_83[len]; len++) - ; - for (size_t i = 0; i < len; i++) - { - ch[0] = filename_83[i]; - OBOS_AppendStringC(current_filename, ch); - } - for (len = 0; len < 3 && filename_83[len+8] != ' ' && filename_83[len+8]; len++) - ; - if (len != 0) - { - ch[0] = '.'; - OBOS_AppendStringC(current_filename, ch); - for (size_t i = 0; i < len; i++) - { - ch[0] = filename_83[i+8]; - OBOS_AppendStringC(current_filename, ch); - } - } - } - fat_dirent_cache* dir_cache = FATAllocator->ZeroAllocate(FATAllocator, 1, sizeof(fat_dirent_cache), nullptr); - dir_cache->data = *curr; - dir_cache->name = *current_filename; - dir_cache->owner = cache; - dir_cache->dirent_fileoff = Vfs_FdTellOff(cache->volume); - if (cache->fatType != FAT32_VOLUME && cache->root == parent) - dir_cache->dirent_fileoff -= cache->blkSize; - else - dir_cache->dirent_fileoff -= (cache->blkSize*cache->bpb->sectorsPerCluster); - dir_cache->dirent_offset = ((uintptr_t)curr-(uintptr_t)buff); - OBOS_InitStringLen(&dir_cache->path, OBOS_GetStringCPtr(&parent->path), OBOS_GetStringSize(&parent->path)); - if (OBOS_GetStringSize(&dir_cache->path)) - OBOS_AppendStringC(&dir_cache->path, "/"); - OBOS_AppendStringS(&dir_cache->path, &dir_cache->name); - *current_filename = (string){}; - CacheAppendChild(parent, dir_cache); - if (curr->attribs & DIRECTORY) - { - uint32_t cluster = curr->first_cluster_low; - if (cache->fatType == FAT32_VOLUME) - cluster |= ((uint32_t)curr->first_cluster_high << 16); - dir_iterate(cache, dir_cache, cluster); - } - - cache->fileCount++; -} -static iterate_decision dir_iterate_impl(uint32_t current_cluster, obos_status stat, void* udata) -{ - if (stat == OBOS_STATUS_ABORTED) - return ITERATE_DECISION_STOP; - fat_cache* cache = (void*)((uintptr_t*)udata)[0]; - fat_dirent_cache* parent = (void*)((uintptr_t*)udata)[1]; - const void* buff = VfsH_PageCacheGetEntry(cache->volume->vn, ClusterToSector(cache, current_cluster)*cache->blkSize, nullptr); - const fat_dirent* curr = buff; - string current_filename = {}; - lfn_dirent **lfn_entries = nullptr; - size_t lfn_entry_count = 0; - OBOS_InitString(¤t_filename, ""); - while(curr->filename_83[0] != 0) - { - process_dirent( - cache, parent, current_cluster, buff, - curr, &lfn_entries, &lfn_entry_count, ¤t_filename); - curr += 1; - if ((uintptr_t)curr >= ((uintptr_t)buff + cache->blkSize)) - return ITERATE_DECISION_CONTINUE; - } - return ITERATE_DECISION_STOP; -} -static void dir_iterate(fat_cache* cache, fat_dirent_cache* parent, uint32_t cluster) -{ - uintptr_t udata[2] = { - (uintptr_t)cache, - (uintptr_t)parent, - }; - FollowClusterChain(cache, cluster, dir_iterate_impl, udata); -} -#undef read_next_sector - -bool probe(void* vn_) -{ - OBOS_ASSERT(vn_); - if (!vn_) - return false; - vnode* vn = (vnode*)vn_; - fd *volume = FATAllocator->ZeroAllocate(FATAllocator, 1, sizeof(fd), nullptr); - // *volume = (fd){ .vn=vn, .flags=FD_FLAGS_READ|FD_FLAGS_WRITE|FD_FLAGS_OPEN, .offset=0 }; - // LIST_APPEND(fd_list, &vn->opened, volume); - Vfs_FdOpenVnode(volume, vn, FD_OFLAGS_READ|FD_OFLAGS_WRITE); - if (!(volume->flags & FD_FLAGS_READ)) - return false; - size_t blkSize = Vfs_FdGetBlkSz(volume); - if (blkSize != 1) - OBOS_ASSERT(blkSize >= sizeof(bpb)); - bpb* bpb = FATAllocator->ZeroAllocate(FATAllocator, 1, blkSize == 1 ? sizeof(struct bpb) : blkSize, nullptr); - OBOS_ENSURE(bpb); - obos_status status = Vfs_FdRead(volume, bpb, blkSize == 1 ? sizeof(struct bpb) : blkSize, nullptr); - if (obos_is_error(status)) - { - FATAllocator->Free(FATAllocator, bpb, blkSize == 1 ? sizeof(struct bpb) : blkSize); - Vfs_FdClose(volume); - FATAllocator->Free(FATAllocator, volume, sizeof(*volume)); - return false; - } - bool ret = - memcmp((uint8_t*)bpb + 0x36, "FAT", 3) ^ - memcmp((uint8_t*)bpb + 0x52, "FAT", 3); - if (bpb->totalSectors16 > 0 && bpb->totalSectors32 > 0) - ret = false; // fnuy business - if (!ret) - { - FATAllocator->Free(FATAllocator, bpb, blkSize == 1 ? sizeof(struct bpb) : blkSize); - Vfs_FdClose(volume); - FATAllocator->Free(FATAllocator, volume, sizeof(*volume)); - return ret; - } - fat_cache* cache = FATAllocator->ZeroAllocate(FATAllocator, 1, sizeof(fat_cache), nullptr); - cache->vn = vn; - cache->volume = volume; - uint32_t RootDirSectors = ((bpb->rootEntryCount * 32) + (bpb->bytesPerSector - 1)) / bpb->bytesPerSector; - uint32_t fatSz = bpb->fatSz16 ? bpb->fatSz16 : bpb->ebpb.fat32.fatSz32; - size_t totalSectors = bpb->totalSectors16 ? bpb->totalSectors16 : bpb->totalSectors32; - uint32_t FirstDataSector = bpb->reservedSectorCount + (bpb->nFATs * fatSz) + RootDirSectors; - cache->FirstDataSector = FirstDataSector; - cache->RootDirSectors = RootDirSectors; - uint32_t DataSec = totalSectors - FirstDataSector; - size_t CountofClusters = DataSec / bpb->sectorsPerCluster; - if (CountofClusters < 4085) - cache->fatType = FAT12_VOLUME; - else if (CountofClusters < 65525) - cache->fatType = FAT16_VOLUME; - else - cache->fatType = FAT32_VOLUME; - cache->bpb = bpb; - cache->fatSz = fatSz; - cache->blkSize = Vfs_FdGetBlkSz(volume); - if (cache->blkSize == 1) - cache->blkSize = bpb->bytesPerSector; - cache->CountofClusters = CountofClusters; - cache->root_cluster = cache->fatType == FAT32_VOLUME ? bpb->ebpb.fat32.rootCluster : 0; - cache->root_sector = cache->fatType == FAT32_VOLUME ? 0 : FirstDataSector-RootDirSectors; - cache->root = FATAllocator->ZeroAllocate(FATAllocator, 1, sizeof(*cache->root), nullptr); - cache->root->data = (fat_dirent){}; // simply has nothing - cache->root->owner = cache; - cache->root->data.attribs |= DIRECTORY; - cache->root->dirent_fileoff = - cache->fatType == FAT32_VOLUME ? - ClusterToSector(cache, cache->root_cluster)*cache->blkSize : - cache->root_sector*cache->blkSize; - cache->root->dirent_offset = 0; - OBOS_InitString(&cache->root->path, ""); - OBOS_InitString(&cache->root->name, ""); - if (cache->fatType == FAT32_VOLUME) - dir_iterate(cache, cache->root, cache->root_cluster); - else - { - lfn_dirent** lfn_entries = nullptr; - size_t lfn_entry_count = 0; - string current_filename = {}; - Vfs_FdSeek(cache->volume, cache->root_sector*cache->blkSize, SEEK_SET); - for (size_t i = cache->root_sector; i < (cache->root_sector+cache->RootDirSectors); i++) - { - const void* buff = VfsH_PageCacheGetEntry(volume->vn, i*cache->blkSize, nullptr); - const fat_dirent* curr = buff; - for (size_t j = 0; j < (cache->blkSize/sizeof(lfn_dirent)); j++, curr++) - { - if (curr->filename_83[0] == (char)0xe5) - continue; - if (curr->filename_83[0] == (char)0) - { - curr = nullptr; - break; - } - process_dirent( - cache, cache->root, 0, buff, curr, - &lfn_entries, &lfn_entry_count, ¤t_filename); - } - if (!curr) - break; - } - } - InitializeCacheFreelist(cache); - LIST_APPEND(fat_cache_list, &FATVolumes, cache); - OBOS_Debug("FAT: CountofClusters: 0x%08x\n", CountofClusters); - OBOS_Debug("FAT: blkSize: 0x%08x\n", blkSize); - OBOS_Debug("FAT: fatSz: 0x%08x\n", fatSz); - OBOS_Debug("FAT: nFats: 0x%08x\n", cache->bpb->nFATs); - OBOS_Debug("FAT: FAT Type: %s\n", (cache->fatType == FAT32_VOLUME) ? "FAT32" : ((cache->fatType == FAT16_VOLUME)) ? "FAT16" : "FAT12"); - return true; -} - -obos_status stat_fs_info(void *vn, drv_fs_info *info) -{ - if (!vn || !info) - return OBOS_STATUS_INVALID_ARGUMENT; - fat_cache* cache = nullptr; - for (cache = LIST_GET_HEAD(fat_cache_list, &FATVolumes); cache; ) - { - if (cache->vn == vn) - break; - - cache = LIST_GET_NEXT(fat_cache_list, &FATVolumes, cache); - } - if (!cache) - return OBOS_STATUS_INVALID_OPERATION; // not a fat volume we have probed - - info->fsBlockSize = cache->blkSize*cache->bpb->sectorsPerCluster; - info->partBlockSize = cache->blkSize; - info->szFs = (cache->CountofClusters*info->fsBlockSize)/info->partBlockSize; - - info->nameMax = 255; - info->flags = FS_FLAGS_NOEXEC; - - info->fileCount = cache->fileCount; - info->freeBlocks = cache->freelist.freeClusterCount; - // TODO: Avaliable file count. - info->availableFiles = SIZE_MAX; - - return OBOS_STATUS_SUCCESS; -} - -LIST_GENERATE(fat_cache_list, struct fat_cache, node); -fat_cache_list FATVolumes; -void GetFatEntryAddrForCluster(fat_cache* cache, uint32_t cluster, fat_entry_addr* out) -{ - uint32_t fatOffset = 0; - switch (cache->fatType) { - case FAT32_VOLUME: - fatOffset = cluster*4; - break; - case FAT16_VOLUME: - fatOffset = cluster*2; - break; - case FAT12_VOLUME: - fatOffset = cluster + (cluster / 2); - break; - default: - OBOS_ASSERT(false && "Invalid fat type."); - break; - } - fat_entry_addr addr = {}; - addr.lba = (cache->bpb->reservedSectorCount + fatOffset / cache->bpb->bytesPerSector); - addr.offset = fatOffset % cache->bpb->bytesPerSector; - *out = addr; -} -uint32_t GetClusterFromFatEntryAddr(fat_cache* cache, fat_entry_addr addr) -{ - return (addr.lba * cache->bpb->bytesPerSector - cache->bpb->reservedSectorCount) + addr.offset; -} -fat12_entry GetFat12Entry(uint16_t val, uint32_t valCluster) -{ - if (valCluster % 2) - return (fat12_entry){ .ent=(val & 0xfff) }; - return (fat12_entry){ .ent=((val >> 4) & 0xfff) }; -} -void CacheAppendChild(fat_dirent_cache* parent, fat_dirent_cache* child) -{ - if(!parent->fdc_children.head) - parent->fdc_children.head = child; - if (parent->fdc_children.tail) - parent->fdc_children.tail->fdc_next_child = child; - child->fdc_prev_child = parent->fdc_children.tail; - parent->fdc_children.tail = child; - parent->fdc_children.nChildren++; - child->d_parent = parent; -} -void CacheRemoveChild(fat_dirent_cache* parent, fat_dirent_cache* what) -{ - if (what->fdc_prev_child) - what->fdc_prev_child->fdc_next_child = what->fdc_next_child; - if (what->fdc_next_child) - what->fdc_next_child->fdc_prev_child = what->fdc_prev_child; - if (parent->fdc_children.head == what) - parent->fdc_children.head = what->fdc_next_child; - if (parent->fdc_children.tail == what) - parent->fdc_children.tail = what->fdc_prev_child; - parent->fdc_children.nChildren--; - what->d_parent = nullptr; // we're now an orphan :( -} diff --git a/src/drivers/generic/slowfat/structs.h b/src/drivers/generic/slowfat/structs.h deleted file mode 100644 index ee1c98a5..00000000 --- a/src/drivers/generic/slowfat/structs.h +++ /dev/null @@ -1,217 +0,0 @@ -/* - * drivers/generic/slowfat/structs.h - * - * Copyright (c) 2024 Omar Berrow - * - * Abandon all hope ye who enter here -*/ - -#pragma once - -#include -#include - -#include - -#include - -#include -#include - -typedef struct fsinfo -{ - uint32_t leadSignature; // 0x41615252 - char resv1[480]; - uint32_t other_signature; // 0x61417272 - uint32_t lastFreeCluster; - uint32_t firstAvailableCluster; // If 0xffffffff, start at two - char resv2[12]; - uint32_t trailSignature; // 0xAA550000 -} OBOS_PACK fsinfo; -typedef struct ebpb32 { - uint32_t fatSz32; - uint16_t extendedFlags; - uint16_t fsVersion; - uint32_t rootCluster; - uint16_t fsInfoOffset; - uint16_t bkBootSector; - char resv1[12]; - uint8_t driveNumber; - char resv2[1]; - uint8_t bootSignature; - uint32_t volumeId; - char volumeLabel[11]; - char doNotUse[8]; -} OBOS_PACK ebpb32; -typedef struct ebpb { - uint8_t driveNumber; - uint8_t reversed; - uint8_t bootSignature; // 0x29 - uint32_t volumeId; - char volumeLabel[11]; - char doNotUse[8]; -} OBOS_PACK ebpb; -typedef struct bpb { - char jmpboot[3]; - char oem_name[8]; - uint16_t bytesPerSector; - uint8_t sectorsPerCluster; - uint16_t reservedSectorCount; - uint8_t nFATs; - uint16_t rootEntryCount; - uint16_t totalSectors16; - uint8_t media; - uint16_t fatSz16; - uint16_t sectorsPerTrack; - uint16_t nHeads; - uint32_t nHiddenSectors; - uint32_t totalSectors32; - union { - ebpb32 fat32; - ebpb fat; - } ebpb; -} OBOS_PACK bpb; -enum { - READ_ONLY=0x01, - HIDDEN=0x02, - SYSTEM=0x04, - VOLUME_ID=0x08, - DIRECTORY=0x10, - ARCHIVE=0x20, - LFN=READ_ONLY|HIDDEN|SYSTEM|VOLUME_ID, -}; -// typedef struct fat_date -// { -// uint8_t day : 5; -// uint8_t month : 4; -// uint8_t year1980 : 7; -// } OBOS_PACK fat_date; -// typedef struct fat_time -// { -// uint8_t seconds : 5; // multiply by two -// uint8_t minutes : 6; -// uint8_t hour : 5; -// } OBOS_PACK fat_time; -typedef uint16_t fat_date; -typedef uint16_t fat_time; -typedef struct fat_dirent -{ - char filename_83[11]; - uint8_t attribs; - uint8_t resv; - uint8_t unused; // creation time in hundredths of a second. unused, since it's pretty obscure, and useless to us - fat_time creation_time; - fat_date creation_date; - fat_date access_date; - uint16_t first_cluster_high; // only valid on FAT32 - fat_time last_mod_time; - fat_date last_mod_date; - uint16_t first_cluster_low; - uint32_t filesize; -} OBOS_PACK fat_dirent; -// Mustn't exceed 255 characters. -typedef struct lfn_dirent -{ - // if bit 6 is set, this is the last entry. this is always set in the first lfn entry of a set - uint8_t order; - uint16_t name1[5]; - uint8_t attrib; // must be LFN - uint8_t type; // TODO: what is this? - uint8_t checksum; - uint16_t name2[6]; - uint16_t mustBeZero; // fstClusLO - uint16_t name3[2]; -} OBOS_PACK lfn_dirent; -OBOS_STATIC_ASSERT(sizeof(fat_dirent) == 32, "sizeof(fat_dirent) isn't 32 bytes."); -OBOS_STATIC_ASSERT(sizeof(lfn_dirent) == 32, "sizeof(lfn_dirent) isn't 32 bytes."); -typedef struct fat_dirent_cache -{ - fat_dirent data; - string name; - string path; - // The offset in bytes of the cluster or sector this dirent is on. - uint64_t dirent_fileoff; - // The offset into the dirent sector in which this dirent is at. - uint32_t dirent_offset; - struct fat_cache* owner; - struct - { - struct fat_dirent_cache* parent; - struct - { - struct fat_dirent_cache* head; - struct fat_dirent_cache* tail; - size_t nChildren; - } children; - struct fat_dirent_cache* next_child; - struct fat_dirent_cache* prev_child; - } tree_info; -} fat_dirent_cache; -#define fdc_children tree_info.children -#define fdc_next_child tree_info.next_child -#define fdc_prev_child tree_info.prev_child -#define fdc_parent tree_info.parent -enum { - FAT32_VOLUME, - FAT16_VOLUME, - FAT12_VOLUME, -}; -typedef LIST_HEAD(fat_cache_list, struct fat_cache) fat_cache_list; -LIST_PROTOTYPE(fat_cache_list, struct fat_cache, node); -typedef struct fat_freenode -{ - uint32_t cluster; - uint32_t nClusters; - struct fat_freenode *next, *prev; -} fat_freenode; -typedef struct fat_cache { - fat_dirent_cache* root; - uint8_t fatType; - bpb* bpb; - struct fd* volume; - mutex fd_lock; - struct vnode* vn; - LIST_NODE(fat_cache_list, struct fat_cache) node; - uint32_t FirstDataSector; - uint32_t RootDirSectors; - uint32_t root_cluster; - uint64_t root_sector; - uint32_t CountofClusters; - size_t blkSize; - size_t fileCount; - uint32_t fatSz; - mutex fat_lock; - struct { - fat_freenode *head, *tail; - size_t nNodes; - size_t freeClusterCount; - mutex lock; - } freelist; -} fat_cache; -extern fat_cache_list FATVolumes; -void CacheAppendChild(fat_dirent_cache* parent, fat_dirent_cache* child); -void CacheRemoveChild(fat_dirent_cache* parent, fat_dirent_cache* what); -typedef struct fat_entry_addr -{ - uint64_t lba; - uint32_t offset; -} fat_entry_addr; -typedef struct { - uint32_t ent : 28; -} OBOS_ALIGN(4) fat32_entry; -typedef struct { - uint16_t ent; -} OBOS_ALIGN(2) fat16_entry; -// FAT12 is funny. -typedef struct { - uint16_t ent : 12; -} OBOS_ALIGN(2) fat12_entry; -void GetFatEntryAddrForCluster(fat_cache* cache, uint32_t cluster, fat_entry_addr* out); -uint32_t GetClusterFromFatEntryAddr(fat_cache* cache, fat_entry_addr addr); -fat12_entry GetFat12Entry(uint16_t val, uint32_t valCluster); -fat_dirent_cache* DirentLookupFrom(const char* path, fat_dirent_cache* root); -#define ClusterToSector(cache, n) (((n) - 2) * (cache)->bpb->sectorsPerCluster + (cache)->FirstDataSector) -#define SectorToCluster(cache, n) (((int64_t)(n)-(int64_t)(cache)->FirstDataSector+(int64_t)(cache)->bpb->sectorsPerCluster*2)/(int64_t)(cache)->bpb->sectorsPerCluster) -//extern allocator_info* FATAllocator; -#define FATAllocator OBOS_KernelAllocator -obos_status WriteFatDirent(fat_cache* cache, fat_dirent_cache* cache_entry, bool lock); diff --git a/src/drivers/testDriver/CMakeLists.txt b/src/drivers/testDriver/CMakeLists.txt new file mode 100644 index 00000000..a95fd1cb --- /dev/null +++ b/src/drivers/testDriver/CMakeLists.txt @@ -0,0 +1,45 @@ +# drivers/testDriver/CMakeLists.txt + +# Copyright (c) 2024 Omar Berrow + +add_executable(testDriver "main.cpp") + +set_target_properties(testDriver PROPERTIES CXX_STANDARD 20) +set_target_properties(testDriver PROPERTIES CXX_STANDARD_REQUIRED 20) + +set_target_properties(testDriver PROPERTIES LINK_DEPENDS "${LINKER_SCRIPT}") + +target_include_directories(testDriver PRIVATE "${CMAKE_SOURCE_DIR}/src/oboskrnl") +target_include_directories(testDriver PRIVATE ${OBOSKRNL_EXTERNAL_INCLUDES}) + +target_compile_definitions(testDriver PRIVATE OBOS_DRIVER=1) + +target_compile_options(testDriver + PRIVATE $<$:${TARGET_DRIVER_COMPILE_OPTIONS_CPP}> + PRIVATE $<$:${TARGET_DRIVER_COMPILE_OPTIONS_C}> + PRIVATE $<$:-fno-rtti> + PRIVATE $<$:-fno-exceptions> + PRIVATE $<$:-fno-stack-protector> + PRIVATE $<$:-fno-stack-check> + PRIVATE $<$:-fno-use-cxa-atexit> + PRIVATE $<$:-fno-threadsafe-statics> + PRIVATE $<$:-std=gnu++20> + PRIVATE $<$:-ffreestanding> + PRIVATE $<$:-fstack-protector-strong> + PRIVATE $<$:-Wstack-usage=65536> + PRIVATE $<$:-Wall> + PRIVATE $<$:-Wextra> + PRIVATE $<$:-Wno-missing-field-initializers> + PRIVATE $<$:-fPIE> + PRIVATE $<$:-g> +) +target_compile_definitions(testDriver + PRIVATE $<$:OBOS_DEBUG> + PRIVATE $<$:OBOS_RELEASE>) +target_link_options(testDriver + PUBLIC "-nostdlib" + PUBLIC "-pie" + PUBLIC ${TARGET_DRIVER_LINKER_OPTIONS} +) + +target_link_libraries(testDriver PRIVATE ${LIBGCC}) \ No newline at end of file diff --git a/src/drivers/testDriver/main.cpp b/src/drivers/testDriver/main.cpp new file mode 100644 index 00000000..1395784d --- /dev/null +++ b/src/drivers/testDriver/main.cpp @@ -0,0 +1,29 @@ +/* + drivers/testDriver/main.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include +#include + +#include + +#include + +using namespace obos; +[[maybe_unused]] volatile +driverInterface::driverHeader OBOS_DEFINE_IN_SECTION(OBOS_DRIVER_HEADER_SECTION) g_driverHeader = { + .magic = driverInterface::g_driverHeaderMagic, + .type = driverInterface::driverType::KernelExtension, + .friendlyName = "Test driver", + .requestedLoader = {0}, + .loaderPacket = nullptr +}; + +extern "C" +void _start() +{ + logger::log("In test driver!\n"); + scheduler::ExitCurrentThread(); +} \ No newline at end of file diff --git a/src/drivers/test_driver/CMakeLists.txt b/src/drivers/test_driver/CMakeLists.txt deleted file mode 100644 index 808d0a16..00000000 --- a/src/drivers/test_driver/CMakeLists.txt +++ /dev/null @@ -1,33 +0,0 @@ -# drivers/test_driver/CMakeLists.txt -# -# Copyright (c) 2024 Omar Berrow - -add_executable(test_driver "main.c" "fireworks.c" "rand.c") - -target_compile_options(test_driver - PRIVATE $<$:${TARGET_DRIVER_COMPILE_OPTIONS_C}> - PRIVATE $<$:-ffreestanding> - PRIVATE $<$:-Wall> - PRIVATE $<$:-Wextra> - PRIVATE $<$:-fstack-protector-all> - PRIVATE $<$:-fno-builtin-memset> - PRIVATE $<$:-fvisibility=hidden> - PRIVATE $<$:-fPIC> -) - -set_target_properties(test_driver PROPERTIES link_depends ${DRIVER_LINKER_SCRIPT}) - -target_include_directories(test_driver - PRIVATE "${CMAKE_SOURCE_DIR}/src/oboskrnl" - PRIVATE ${OBOSKRNL_EXTERNAL_INCLUDES}) - -target_link_options(test_driver - PRIVATE "-nostdlib" - PRIVATE "-fPIC" - PRIVATE "-T${DRIVER_LINKER_SCRIPT}" - PRIVATE "-Wl,-shared" -# PRIVATE "-Wl,--allow-shlib-undefined" - PRIVATE ${TARGET_DRIVER_LINKER_OPTIONS} -) - -target_compile_definitions(test_driver PRIVATE OBOS_DRIVER=1) diff --git a/src/drivers/test_driver/fireworks.c b/src/drivers/test_driver/fireworks.c deleted file mode 100644 index 9bfa1612..00000000 --- a/src/drivers/test_driver/fireworks.c +++ /dev/null @@ -1,356 +0,0 @@ -/* - * drivers/test_driver/fireworks.c - * - * Copyright (c) 2024 Omar Berrow - * - * Inspired from BORON's fireworks test. -*/ - -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include -#include - -#include - -#include -#include -#include - -#include - -#include - -#include "rand.h" - -#undef OBOS_TEXT_BACKGROUND - -#ifndef OBOS_TEXT_BACKGROUND -# define OBOS_TEXT_BACKGROUND 0x00000000 -#endif - -void delay_impl(void* userdata) -{ - Core_EventSet((event*)userdata, true); -} -static timer* delay(timer_tick ms, timer* cached_t) -{ - // event e = EVENT_INITIALIZE(EVENT_NOTIFICATION); - // timer* t = cached_t ? cached_t : Core_TimerObjectAllocate(nullptr); - // memzero(t, sizeof(*t)); - // t->handler = delay_impl; - // t->userdata = (void*)&e; - // // printf("wait for %d ms\n", ms); - // obos_status status = Core_TimerObjectInitialize(t, TIMER_MODE_DEADLINE, ms*1000); - // OBOS_ASSERT(status == OBOS_STATUS_SUCCESS && "Core_TimerObjectInitialize"); - // status = Core_WaitOnObject(WAITABLE_OBJECT(e)); - // OBOS_ASSERT(status == OBOS_STATUS_SUCCESS && "Core_WaitOnObject"); - // return t; - timer_tick deadline = CoreS_GetTimerTick() + CoreH_TimeFrameToTick(ms*1000); - while(CoreS_GetTimerTick() < deadline) - Core_Yield(); - return nullptr; -} -#define FRAMEBUFFER_WIDTH (OBOS_TextRendererState.fb.width) -#define FRAMEBUFFER_HEIGHT (OBOS_TextRendererState.fb.height) - -thread* create_thread(void* entry, uintptr_t udata, thread_priority priority, thread** out) -{ - thread* new = CoreH_ThreadAllocate(nullptr); - thread_ctx ctx = {}; - void* stack = nullptr; - stack = Mm_VirtualMemoryAlloc(&Mm_KernelContext, - nullptr, 0x8000, - 0, VMA_FLAGS_KERNEL_STACK, - nullptr, nullptr); - CoreS_SetupThreadContext(&ctx, (uintptr_t)entry, udata, false, stack, 0x8000); - CoreH_ThreadInitialize(new, priority, Core_DefaultThreadAffinity, &ctx); - new->references++; - new->stackFree = CoreH_VMAStackFree; - new->stackFreeUserdata = &Mm_KernelContext; - if (out) - { - new->references++; - *out = new; - } - Core_ProcessAppendThread(OBOS_KernelProcess, new); - CoreH_ThreadReady(new); - return new; -} -void plot_pixel(uint32_t rgbx, int32_t x, int32_t y) -{ - if (x < 0) - x = 0; - if (y < 0) - y = 0; - OBOS_ASSERT (x < (int32_t)FRAMEBUFFER_WIDTH); - OBOS_ASSERT(y < (int32_t)FRAMEBUFFER_HEIGHT); - if (x > (int32_t)FRAMEBUFFER_WIDTH || y > (int32_t)FRAMEBUFFER_HEIGHT) - return; - // uint8_t* fb = OBOS_TextRendererState.fb.backbuffer_base ? OBOS_TextRendererState.fb.backbuffer_base : OBOS_TextRendererState.fb.base; - uint8_t* fb = OBOS_TextRendererState.fb.base; - fb += (y*OBOS_TextRendererState.fb.pitch+x*OBOS_TextRendererState.fb.bpp/8); - OBOS_PlotPixel(rgbx, fb, OBOS_TextRendererState.fb.format); -} -uint32_t random_pixel() -{ - return ((mt_random() + 0x808080) & 0xffffff) << 8; -} - -typedef struct firework_data -{ - int32_t x,y; - uint32_t rgbx; - fixedptd act_x, act_y; - fixedptd vel_x, vel_y; - int explosion_range; - _Atomic(size_t) refcount; - bool can_free; - bool stress_test; - fixedptd direction; -} firework_data; - -OBOS_NO_UBSAN fixedptd fp_rand_sign() -{ - if (mt_random() % 2) - return -fixedpt_fromint(mt_random() % (FIXEDPT_FBITS+1)); - return fixedpt_fromint(mt_random() % (FIXEDPT_FBITS+1)); -} - -#include "sin_table.h" -OBOS_NO_UBSAN fixedptd sin(int angle) -{ - return fixedpt_xdiv(fixedpt_fromint(sin_table[angle % 65536]), fixedpt_fromint(32768)); -} -OBOS_NO_UBSAN fixedptd cos(int angle) -{ - return sin(angle+16384); -} - -static void particle_update(void* udata_) -{ - uint64_t *udata = udata_; - firework_data *data = (void*)(uintptr_t)udata[0]; - event* e = (void*)(uintptr_t)udata[1]; - timer* t = (void*)(uintptr_t)udata[2]; - int expires_in = (int)(uintptr_t)udata[3]; - timer_tick startedAt = (timer_tick)(uintptr_t)udata[4]; - if ((expires_in + startedAt) >= t->lastTimeTicked) - { - Core_CancelTimer(t); - Core_EventSet(e, true); - return; - } - if (data->y < 0) - { - Core_CancelTimer(t); - Core_EventSet(e, true); - return; - } - if (data->x < 0) - { - Core_CancelTimer(t); - Core_EventSet(e, true); - return; - } - if (data->y >= (int32_t)FRAMEBUFFER_HEIGHT) - { - Core_CancelTimer(t); - Core_EventSet(e, true); - return; - } - if (data->x >= (int32_t)FRAMEBUFFER_WIDTH) - { - Core_CancelTimer(t); - Core_EventSet(e, true); - return; - } - plot_pixel(OBOS_TEXT_BACKGROUND, data->x, data->y); - Core_Yield(); - fixedptd temp_pt = fixedpt_div(fixedpt_fromint(17), fixedpt_fromint(1000)); - data->act_x = fixedpt_add(fixedpt_mul(data->vel_x, temp_pt), data->act_x); - data->act_y = fixedpt_add(data->act_y, fixedpt_mul(data->vel_y, temp_pt)); - data->x = fixedpt_toint(data->act_x); - data->y = fixedpt_toint(data->act_y); - if (data->y < 0) - { - Core_CancelTimer(t); - Core_EventSet(e, true); - return; - } - if (data->x < 0) - { - Core_CancelTimer(t); - Core_EventSet(e, true); - return; - } - if (data->y >= (int32_t)FRAMEBUFFER_HEIGHT) - { - Core_CancelTimer(t); - Core_EventSet(e, true); - return; - } - if (data->x >= (int32_t)FRAMEBUFFER_WIDTH) - { - Core_CancelTimer(t); - Core_EventSet(e, true); - return; - } - data->vel_y += fixedpt_fromint(10)*temp_pt; - plot_pixel(data->rgbx, data->x, data->y); - Core_Yield(); -} -_Atomic(size_t) nParticlesLeft = 0; -void particle_handler(void* udata) -{ - firework_data* parent = udata; - firework_data data = {.x=parent->x,.y=parent->y,.act_x=parent->act_x,.act_y=parent->act_y,.direction=parent->direction,.stress_test = parent->stress_test}; - int ExplosionRange = parent->explosion_range; - if (!(--parent->refcount) && parent->can_free) - { - Free(OBOS_NonPagedPoolAllocator, parent, sizeof(*parent)); - parent = nullptr; - } - int Angle = mt_random() % 65536; - data.vel_x = fixedpt_mul(cos(Angle), fp_rand_sign())*ExplosionRange; - data.vel_y = fixedpt_mul(sin(Angle), fp_rand_sign())*ExplosionRange; - const int expires_in = 2000 + mt_random() % 2000; - data.rgbx = random_pixel(); - if (!data.stress_test) - { - timer t = {}; - event e = EVENT_INITIALIZE(EVENT_NOTIFICATION); - uint64_t udata[] = { - (uintptr_t)&data, - (uintptr_t)&e, - (uintptr_t)&t, - expires_in, 0 - }; - t.handler = particle_update; - t.userdata = udata; - obos_status status = Core_TimerObjectInitialize(&t, TIMER_MODE_INTERVAL, 17*1000 /* 17 ms */); - udata[3] = t.lastTimeTicked; - if (obos_is_error(status)) - goto die; - status = Core_WaitOnObject(WAITABLE_OBJECT(e)); - goto die; - } - int t = 0; - timer* timer = nullptr; - for (int i = 0; i < expires_in; ) - { - plot_pixel(data.rgbx, data.x, data.y); - int curr_delay = 8+(t!=0); - timer = delay(curr_delay, timer); - i += curr_delay; - if (++t == 3) - t = 0; - plot_pixel(OBOS_TEXT_BACKGROUND, data.x, data.y); - fixedptd temp_pt = fixedpt_div(fixedpt_fromint(curr_delay), fixedpt_fromint(1000)); - data.act_x = fixedpt_add(fixedpt_mul(data.vel_x, temp_pt), data.act_x); - data.act_y = fixedpt_add(data.act_y, fixedpt_mul(data.vel_y, temp_pt)); - data.x = fixedpt_toint(data.act_x); - data.y = fixedpt_toint(data.act_y); - if (data.y < 0) - break; - if (data.x < 0) - break; - if (data.y >= (int32_t)FRAMEBUFFER_HEIGHT) - break; - if (data.x >= (int32_t)FRAMEBUFFER_WIDTH) - break; - data.vel_y += (-fixedpt_fromint(10))*temp_pt; - } - die: - nParticlesLeft--; - Core_ExitCurrentThread(); -} -static void explodeable_handler(bool stress_test) -{ - firework_data data = {}; - int x_offset = (FRAMEBUFFER_WIDTH*400)/1024; - data.x = FRAMEBUFFER_WIDTH / 2; - data.y = FRAMEBUFFER_HEIGHT - 1; - data.stress_test = stress_test; - data.act_x = fixedpt_fromint(data.x); - data.act_y = fixedpt_fromint(data.y); - data.vel_y = -fixedpt_fromint((400+mt_random()%400)); - data.direction = mt_random() % 2 ? -fixedpt_fromint(1) : fixedpt_fromint(1); - data.vel_x = fixedpt_mul(fixedpt_fromint(x_offset), data.direction); - data.rgbx = random_pixel(); - data.explosion_range = mt_random() % 100 + 100; - int expires_in = 250 + (mt_random() % 500); - int t = 0; - timer* timer = nullptr; - for (int i = 0; i < expires_in; ) - { - plot_pixel(data.rgbx, data.x, data.y); - int curr_delay = 16+(t!=0); - timer = delay(curr_delay, timer); - i += curr_delay; - if (++t == 3) - t = 0; - plot_pixel(OBOS_TEXT_BACKGROUND, data.x, data.y); - fixedptd temp_pt = fixedpt_div(fixedpt_fromint(curr_delay), fixedpt_fromint(1000)); - data.act_x = fixedpt_add(fixedpt_mul(data.vel_x, temp_pt), data.act_x); - data.act_y = fixedpt_add(data.act_y, fixedpt_mul(data.vel_y, temp_pt)); - data.x = fixedpt_toint(data.act_x); - data.y = fixedpt_toint(data.act_y); - if (data.y < 0) - break; - if (data.x < 0) - break; - if (data.y >= (int32_t)FRAMEBUFFER_HEIGHT) - break; - if (data.x >= (int32_t)(FRAMEBUFFER_WIDTH)) - break; - data.vel_y += -fixedpt_fromint(10)*temp_pt; - } - int nParticles = mt_random() % 100 + 100; - firework_data* fw_clone = ZeroAllocate(OBOS_NonPagedPoolAllocator, 1, sizeof(firework_data), nullptr); - memcpy(fw_clone, &data, sizeof(data)); - nParticlesLeft += nParticles; - for (int i = 0; i < nParticles; i++) - { - fw_clone->can_free = false; - fw_clone->refcount++; - create_thread((void*)particle_handler, (uintptr_t)fw_clone, THREAD_PRIORITY_IDLE, nullptr); - } - fw_clone->can_free = true; - Core_ExitCurrentThread(); -} -static void SpawnNewExplodable(bool stress_test, thread** out) -{ - create_thread((void*)explodeable_handler, stress_test, THREAD_PRIORITY_NORMAL, out); -} - -DRV_EXPORT void TestDriver_Fireworks(uint32_t max_iterations, int spawn_min, int spawn_max, bool stress_test) -{ - OBOS_UNUSED(max_iterations); - memset(OBOS_TextRendererState.fb.base, 0, OBOS_TextRendererState.fb.pitch*OBOS_TextRendererState.fb.height); - mt_seed(random_seed()); - timer* t = nullptr; - log_level old = OBOS_GetLogLevel(); - OBOS_SetLogLevel(LOG_LEVEL_ERROR); - for (uint32_t i = 0; i < max_iterations; i++) - { - int nToSpawn = mt_random() % spawn_max + spawn_min; - for (int i = 0; i < nToSpawn; i++) - SpawnNewExplodable(stress_test, nullptr); - // Wait 1-2 seconds to spawn new fireworks. - t = delay(mt_random() % 2000 + 2000, t); - // while (threadCounts[last_direction]) - // OBOSS_SpinlockHint(); - } - OBOS_SetLogLevel(old); - Core_TimerObjectFree(t); -} diff --git a/src/drivers/test_driver/main.c b/src/drivers/test_driver/main.c deleted file mode 100644 index 42c9a028..00000000 --- a/src/drivers/test_driver/main.c +++ /dev/null @@ -1,116 +0,0 @@ -/* - * drivers/test_driver/main.c - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include -#include -#include - -#include - -#include -#include - -#include - -#include - -#include "rand.h" - -#define IOCTL_TESTDRIVER_FIREWORKS 1 -#define IOCTL_TESTDRIVER_ALLOCATOR 2 /* TODO */ -void cleanup() -{ /* Nothing to do */ } -DRV_EXPORT void TestDriver_Fireworks(uint32_t max_iterations, int spawn_min, int spawn_max, bool stress_test); -/*obos_status ioctl_var(size_t nParameters, uint64_t request, va_list list) -{ - switch(request) - { - case IOCTL_TESTDRIVER_FIREWORKS: - if (nParameters < 4) - return OBOS_STATUS_INVALID_ARGUMENT; - volatile uint32_t arg1 = va_arg(list, uint32_t); - volatile int arg2 = va_arg(list, int); - volatile int arg3 = va_arg(list, int); - volatile bool arg4 = (bool)va_arg(list, uint32_t); - TestDriver_Fireworks(arg1, arg2, arg3, arg4); - return OBOS_STATUS_SUCCESS; - case IOCTL_TESTDRIVER_ALLOCATOR: - return OBOS_STATUS_UNIMPLEMENTED; - } - return OBOS_STATUS_INVALID_IOCTL; -} -obos_status ioctl(size_t nParameters, uint64_t request, ...) -{ - va_list list; - va_start(list, request); - obos_status status = ioctl_var(nParameters, request, list); - va_end(list); - return status; -}*/ -__attribute__((section(OBOS_DRIVER_HEADER_SECTION))) volatile driver_header drv_hdr = { - .magic = OBOS_DRIVER_MAGIC, - .flags = 0, - .ftable.driver_cleanup_callback = cleanup, - //.ftable.ioctl = ioctl, - .driverName = "Test driver" -}; - -driver_id* this_driver; - -OBOS_PAGEABLE_FUNCTION DRV_EXPORT void TestDriver_Test(driver_id* caller) -{ - OBOS_Log("Function in driver %d called from driver %d.\n", this_driver->id, caller->id); -} -extern char Drv_Base[]; - -OBOS_PAGEABLE_FUNCTION void OBOS_DriverEntry(driver_id* this) -{ - this_driver = this; - OBOS_Log("%s: Hello from test driver #1. Driver base: %p. Driver id: %d.\n", __func__, this->base, this->id); - TestDriver_Test(this); - TestDriver_Fireworks(UINT32_MAX, 1, 3, true); - OBOS_Log("Exiting from main thread.\n"); - Core_ExitCurrentThread(); -} - -#ifdef __x86_64__ -extern uint64_t random_seed_x86_64(); -__attribute__((optimize("-O0"))) uintptr_t random_seed() -{ - uint64_t seed = random_seed_x86_64(); - if (!seed) - seed = CoreS_GetNativeTimerTick(); - return seed; -} -asm ( - "\ - .intel_syntax noprefix;\ - .global random_seed_x86_64;\ - random_seed_x86_64:;\ - push rbp; mov rbp, rsp; push rbx;\ - .rdrand:;\ - mov eax, 1;\ - xor ecx,ecx;\ - cpuid;\ - bt ecx, 30;\ - jnc .rdseed;\ - rdrand rax;\ - jnc .rdrand;\ - jmp .done;\ - .rdseed:;\ - mov eax, 7;\ - xor ecx,ecx;\ - cpuid;\ - bt ebx, 18;\ - jnc .done;\ - rdseed rax;\ - jnc .rdseed;\ - .done:;\ - pop rbx; leave; ret;\ - .att_syntax prefix;\ - " -); -#endif diff --git a/src/drivers/test_driver/rand.c b/src/drivers/test_driver/rand.c deleted file mode 100644 index 47aedbac..00000000 --- a/src/drivers/test_driver/rand.c +++ /dev/null @@ -1,83 +0,0 @@ -#include -#include - -#include - -#include "rand.h" - -// shamelessly stolen from https://osdev.wiki/wiki/Random_Number_Generator#Mersenne_Twister - -#if OBOS_ARCHITECTURE_BITS == 32 -#define STATE_SIZE 624 -#define MIDDLE 397 -#define INIT_SHIFT 30 -#define INIT_FACT 1812433253 -#define TWIST_MASK 0x9908b0df -#define SHIFT1 11 -#define MASK1 0xffffffff -#define SHIFT2 7 -#define MASK2 0x9d2c5680 -#define SHIFT3 15 -#define MASK3 0xefc60000 -#define SHIFT4 18 -#else -#define STATE_SIZE 312 -#define MIDDLE 156 -#define INIT_SHIFT 62 -#define TWIST_MASK 0xb5026f5aa96619e9 -#define INIT_FACT 6364136223846793005 -#define SHIFT1 29 -#define MASK1 0x5555555555555555 -#define SHIFT2 17 -#define MASK2 0x71d67fffeda60000 -#define SHIFT3 37 -#define MASK3 0xfff7eee000000000 -#define SHIFT4 43 -#endif - -#define LOWER_MASK 0x7fffffff -#define UPPER_MASK (~(uintptr_t)LOWER_MASK) -static uintptr_t state[STATE_SIZE]; -static size_t index = STATE_SIZE + 1; - -void mt_seed(uintptr_t s) -{ - index = STATE_SIZE; - state[0] = s; - for (size_t i = 1; i < STATE_SIZE; i++) - state[i] = (INIT_FACT * (state[i - 1] ^ (state[i - 1] >> INIT_SHIFT))) + i; -} - -static void twist(void) -{ - for (size_t i = 0; i < STATE_SIZE; i++) - { - uintptr_t x = (state[i] & UPPER_MASK) | (state[(i + 1) % STATE_SIZE] & LOWER_MASK); - x = (x >> 1) ^ (x & 1? TWIST_MASK : 0); - state[i] = state[(i + MIDDLE) % STATE_SIZE] ^ x; - } - index = 0; -} - -uintptr_t mt_random(void) -{ - if (index >= STATE_SIZE) - { - OBOS_ASSERT(index == STATE_SIZE || !"Generator never seeded"); - twist(); - } - - uintptr_t y = state[index]; - y ^= (y >> SHIFT1) & MASK1; - y ^= (y << SHIFT2) & MASK2; - y ^= (y << SHIFT3) & MASK3; - y ^= y >> SHIFT4; - - index++; - return y; -} - -OBOS_WEAK uintptr_t random_seed() -{ - return CoreS_GetNativeTimerTick(); -} \ No newline at end of file diff --git a/src/drivers/test_driver/rand.h b/src/drivers/test_driver/rand.h deleted file mode 100644 index 504fce58..00000000 --- a/src/drivers/test_driver/rand.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include - -uintptr_t mt_random(void); -void mt_seed(uintptr_t seed); -uintptr_t random_seed(); \ No newline at end of file diff --git a/src/drivers/test_driver/sin_table.h b/src/drivers/test_driver/sin_table.h deleted file mode 100644 index 35dd43b4..00000000 --- a/src/drivers/test_driver/sin_table.h +++ /dev/null @@ -1,3 +0,0 @@ -int sin_table[] = { - 0,3,6,9,13,16,19,22,25,28,31,35,38,41,44,47,50,53,57,60,63,66,69,72,75,79,82,85,88,91,94,97,101,104,107,110,113,116,119,123,126,129,132,135,138,141,145,148,151,154,157,160,163,166,170,173,176,179,182,185,188,192,195,198,201,204,207,210,214,217,220,223,226,229,232,236,239,242,245,248,251,254,258,261,264,267,270,273,276,280,283,286,289,292,295,298,302,305,308,311,314,317,320,324,327,330,333,336,339,342,346,349,352,355,358,361,364,368,371,374,377,380,383,386,390,393,396,399,402,405,408,412,415,418,421,424,427,430,434,437,440,443,446,449,452,456,459,462,465,468,471,474,477,481,484,487,490,493,496,499,503,506,509,512,515,518,521,525,528,531,534,537,540,543,547,550,553,556,559,562,565,569,572,575,578,581,584,587,591,594,597,600,603,606,609,613,616,619,622,625,628,631,635,638,641,644,647,650,653,657,660,663,666,669,672,675,679,682,685,688,691,694,697,701,704,707,710,713,716,719,722,726,729,732,735,738,741,744,748,751,754,757,760,763,766,770,773,776,779,782,785,788,792,795,798,801,804,807,810,814,817,820,823,826,829,832,836,839,842,845,848,851,854,858,861,864,867,870,873,876,880,883,886,889,892,895,898,901,905,908,911,914,917,920,923,927,930,933,936,939,942,945,949,952,955,958,961,964,967,971,974,977,980,983,986,989,993,996,999,1002,1005,1008,1011,1015,1018,1021,1024,1027,1030,1033,1037,1040,1043,1046,1049,1052,1055,1059,1062,1065,1068,1071,1074,1077,1080,1084,1087,1090,1093,1096,1099,1102,1106,1109,1112,1115,1118,1121,1124,1128,1131,1134,1137,1140,1143,1146,1150,1153,1156,1159,1162,1165,1168,1172,1175,1178,1181,1184,1187,1190,1194,1197,1200,1203,1206,1209,1212,1215,1219,1222,1225,1228,1231,1234,1237,1241,1244,1247,1250,1253,1256,1259,1263,1266,1269,1272,1275,1278,1281,1285,1288,1291,1294,1297,1300,1303,1307,1310,1313,1316,1319,1322,1325,1328,1332,1335,1338,1341,1344,1347,1350,1354,1357,1360,1363,1366,1369,1372,1376,1379,1382,1385,1388,1391,1394,1398,1401,1404,1407,1410,1413,1416,1420,1423,1426,1429,1432,1435,1438,1441,1445,1448,1451,1454,1457,1460,1463,1467,1470,1473,1476,1479,1482,1485,1489,1492,1495,1498,1501,1504,1507,1511,1514,1517,1520,1523,1526,1529,1532,1536,1539,1542,1545,1548,1551,1554,1558,1561,1564,1567,1570,1573,1576,1580,1583,1586,1589,1592,1595,1598,1602,1605,1608,1611,1614,1617,1620,1623,1627,1630,1633,1636,1639,1642,1645,1649,1652,1655,1658,1661,1664,1667,1671,1674,1677,1680,1683,1686,1689,1693,1696,1699,1702,1705,1708,1711,1714,1718,1721,1724,1727,1730,1733,1736,1740,1743,1746,1749,1752,1755,1758,1762,1765,1768,1771,1774,1777,1780,1783,1787,1790,1793,1796,1799,1802,1805,1809,1812,1815,1818,1821,1824,1827,1831,1834,1837,1840,1843,1846,1849,1852,1856,1859,1862,1865,1868,1871,1874,1878,1881,1884,1887,1890,1893,1896,1900,1903,1906,1909,1912,1915,1918,1921,1925,1928,1931,1934,1937,1940,1943,1947,1950,1953,1956,1959,1962,1965,1969,1972,1975,1978,1981,1984,1987,1990,1994,1997,2000,2003,2006,2009,2012,2016,2019,2022,2025,2028,2031,2034,2038,2041,2044,2047,2050,2053,2056,2059,2063,2066,2069,2072,2075,2078,2081,2085,2088,2091,2094,2097,2100,2103,2106,2110,2113,2116,2119,2122,2125,2128,2132,2135,2138,2141,2144,2147,2150,2154,2157,2160,2163,2166,2169,2172,2175,2179,2182,2185,2188,2191,2194,2197,2201,2204,2207,2210,2213,2216,2219,2222,2226,2229,2232,2235,2238,2241,2244,2248,2251,2254,2257,2260,2263,2266,2269,2273,2276,2279,2282,2285,2288,2291,2295,2298,2301,2304,2307,2310,2313,2316,2320,2323,2326,2329,2332,2335,2338,2342,2345,2348,2351,2354,2357,2360,2363,2367,2370,2373,2376,2379,2382,2385,2389,2392,2395,2398,2401,2404,2407,2410,2414,2417,2420,2423,2426,2429,2432,2436,2439,2442,2445,2448,2451,2454,2457,2461,2464,2467,2470,2473,2476,2479,2483,2486,2489,2492,2495,2498,2501,2504,2508,2511,2514,2517,2520,2523,2526,2530,2533,2536,2539,2542,2545,2548,2551,2555,2558,2561,2564,2567,2570,2573,2577,2580,2583,2586,2589,2592,2595,2598,2602,2605,2608,2611,2614,2617,2620,2623,2627,2630,2633,2636,2639,2642,2645,2649,2652,2655,2658,2661,2664,2667,2670,2674,2677,2680,2683,2686,2689,2692,2695,2699,2702,2705,2708,2711,2714,2717,2721,2724,2727,2730,2733,2736,2739,2742,2746,2749,2752,2755,2758,2761,2764,2767,2771,2774,2777,2780,2783,2786,2789,2793,2796,2799,2802,2805,2808,2811,2814,2818,2821,2824,2827,2830,2833,2836,2839,2843,2846,2849,2852,2855,2858,2861,2865,2868,2871,2874,2877,2880,2883,2886,2890,2893,2896,2899,2902,2905,2908,2911,2915,2918,2921,2924,2927,2930,2933,2936,2940,2943,2946,2949,2952,2955,2958,2962,2965,2968,2971,2974,2977,2980,2983,2987,2990,2993,2996,2999,3002,3005,3008,3012,3015,3018,3021,3024,3027,3030,3033,3037,3040,3043,3046,3049,3052,3055,3059,3062,3065,3068,3071,3074,3077,3080,3084,3087,3090,3093,3096,3099,3102,3105,3109,3112,3115,3118,3121,3124,3127,3130,3134,3137,3140,3143,3146,3149,3152,3155,3159,3162,3165,3168,3171,3174,3177,3180,3184,3187,3190,3193,3196,3199,3202,3205,3209,3212,3215,3218,3221,3224,3227,3230,3234,3237,3240,3243,3246,3249,3252,3255,3259,3262,3265,3268,3271,3274,3277,3281,3284,3287,3290,3293,3296,3299,3302,3306,3309,3312,3315,3318,3321,3324,3327,3331,3334,3337,3340,3343,3346,3349,3352,3356,3359,3362,3365,3368,3371,3374,3377,3381,3384,3387,3390,3393,3396,3399,3402,3406,3409,3412,3415,3418,3421,3424,3427,3430,3434,3437,3440,3443,3446,3449,3452,3455,3459,3462,3465,3468,3471,3474,3477,3480,3484,3487,3490,3493,3496,3499,3502,3505,3509,3512,3515,3518,3521,3524,3527,3530,3534,3537,3540,3543,3546,3549,3552,3555,3559,3562,3565,3568,3571,3574,3577,3580,3584,3587,3590,3593,3596,3599,3602,3605,3609,3612,3615,3618,3621,3624,3627,3630,3634,3637,3640,3643,3646,3649,3652,3655,3658,3662,3665,3668,3671,3674,3677,3680,3683,3687,3690,3693,3696,3699,3702,3705,3708,3712,3715,3718,3721,3724,3727,3730,3733,3737,3740,3743,3746,3749,3752,3755,3758,3761,3765,3768,3771,3774,3777,3780,3783,3786,3790,3793,3796,3799,3802,3805,3808,3811,3815,3818,3821,3824,3827,3830,3833,3836,3839,3843,3846,3849,3852,3855,3858,3861,3864,3868,3871,3874,3877,3880,3883,3886,3889,3893,3896,3899,3902,3905,3908,3911,3914,3917,3921,3924,3927,3930,3933,3936,3939,3942,3946,3949,3952,3955,3958,3961,3964,3967,3970,3974,3977,3980,3983,3986,3989,3992,3995,3999,4002,4005,4008,4011,4014,4017,4020,4024,4027,4030,4033,4036,4039,4042,4045,4048,4052,4055,4058,4061,4064,4067,4070,4073,4076,4080,4083,4086,4089,4092,4095,4098,4101,4105,4108,4111,4114,4117,4120,4123,4126,4129,4133,4136,4139,4142,4145,4148,4151,4154,4158,4161,4164,4167,4170,4173,4176,4179,4182,4186,4189,4192,4195,4198,4201,4204,4207,4210,4214,4217,4220,4223,4226,4229,4232,4235,4239,4242,4245,4248,4251,4254,4257,4260,4263,4267,4270,4273,4276,4279,4282,4285,4288,4291,4295,4298,4301,4304,4307,4310,4313,4316,4320,4323,4326,4329,4332,4335,4338,4341,4344,4348,4351,4354,4357,4360,4363,4366,4369,4372,4376,4379,4382,4385,4388,4391,4394,4397,4400,4404,4407,4410,4413,4416,4419,4422,4425,4428,4432,4435,4438,4441,4444,4447,4450,4453,4456,4460,4463,4466,4469,4472,4475,4478,4481,4485,4488,4491,4494,4497,4500,4503,4506,4509,4513,4516,4519,4522,4525,4528,4531,4534,4537,4541,4544,4547,4550,4553,4556,4559,4562,4565,4569,4572,4575,4578,4581,4584,4587,4590,4593,4597,4600,4603,4606,4609,4612,4615,4618,4621,4624,4628,4631,4634,4637,4640,4643,4646,4649,4652,4656,4659,4662,4665,4668,4671,4674,4677,4680,4684,4687,4690,4693,4696,4699,4702,4705,4708,4712,4715,4718,4721,4724,4727,4730,4733,4736,4740,4743,4746,4749,4752,4755,4758,4761,4764,4768,4771,4774,4777,4780,4783,4786,4789,4792,4795,4799,4802,4805,4808,4811,4814,4817,4820,4823,4827,4830,4833,4836,4839,4842,4845,4848,4851,4855,4858,4861,4864,4867,4870,4873,4876,4879,4882,4886,4889,4892,4895,4898,4901,4904,4907,4910,4914,4917,4920,4923,4926,4929,4932,4935,4938,4941,4945,4948,4951,4954,4957,4960,4963,4966,4969,4973,4976,4979,4982,4985,4988,4991,4994,4997,5000,5004,5007,5010,5013,5016,5019,5022,5025,5028,5032,5035,5038,5041,5044,5047,5050,5053,5056,5059,5063,5066,5069,5072,5075,5078,5081,5084,5087,5091,5094,5097,5100,5103,5106,5109,5112,5115,5118,5122,5125,5128,5131,5134,5137,5140,5143,5146,5149,5153,5156,5159,5162,5165,5168,5171,5174,5177,5180,5184,5187,5190,5193,5196,5199,5202,5205,5208,5212,5215,5218,5221,5224,5227,5230,5233,5236,5239,5243,5246,5249,5252,5255,5258,5261,5264,5267,5270,5274,5277,5280,5283,5286,5289,5292,5295,5298,5301,5305,5308,5311,5314,5317,5320,5323,5326,5329,5332,5336,5339,5342,5345,5348,5351,5354,5357,5360,5363,5367,5370,5373,5376,5379,5382,5385,5388,5391,5394,5398,5401,5404,5407,5410,5413,5416,5419,5422,5425,5428,5432,5435,5438,5441,5444,5447,5450,5453,5456,5459,5463,5466,5469,5472,5475,5478,5481,5484,5487,5490,5494,5497,5500,5503,5506,5509,5512,5515,5518,5521,5525,5528,5531,5534,5537,5540,5543,5546,5549,5552,5555,5559,5562,5565,5568,5571,5574,5577,5580,5583,5586,5590,5593,5596,5599,5602,5605,5608,5611,5614,5617,5620,5624,5627,5630,5633,5636,5639,5642,5645,5648,5651,5655,5658,5661,5664,5667,5670,5673,5676,5679,5682,5685,5689,5692,5695,5698,5701,5704,5707,5710,5713,5716,5719,5723,5726,5729,5732,5735,5738,5741,5744,5747,5750,5754,5757,5760,5763,5766,5769,5772,5775,5778,5781,5784,5788,5791,5794,5797,5800,5803,5806,5809,5812,5815,5818,5822,5825,5828,5831,5834,5837,5840,5843,5846,5849,5852,5856,5859,5862,5865,5868,5871,5874,5877,5880,5883,5886,5890,5893,5896,5899,5902,5905,5908,5911,5914,5917,5920,5924,5927,5930,5933,5936,5939,5942,5945,5948,5951,5954,5958,5961,5964,5967,5970,5973,5976,5979,5982,5985,5988,5991,5995,5998,6001,6004,6007,6010,6013,6016,6019,6022,6025,6029,6032,6035,6038,6041,6044,6047,6050,6053,6056,6059,6063,6066,6069,6072,6075,6078,6081,6084,6087,6090,6093,6096,6100,6103,6106,6109,6112,6115,6118,6121,6124,6127,6130,6134,6137,6140,6143,6146,6149,6152,6155,6158,6161,6164,6167,6171,6174,6177,6180,6183,6186,6189,6192,6195,6198,6201,6204,6208,6211,6214,6217,6220,6223,6226,6229,6232,6235,6238,6241,6245,6248,6251,6254,6257,6260,6263,6266,6269,6272,6275,6278,6282,6285,6288,6291,6294,6297,6300,6303,6306,6309,6312,6315,6319,6322,6325,6328,6331,6334,6337,6340,6343,6346,6349,6352,6356,6359,6362,6365,6368,6371,6374,6377,6380,6383,6386,6389,6393,6396,6399,6402,6405,6408,6411,6414,6417,6420,6423,6426,6429,6433,6436,6439,6442,6445,6448,6451,6454,6457,6460,6463,6466,6470,6473,6476,6479,6482,6485,6488,6491,6494,6497,6500,6503,6506,6510,6513,6516,6519,6522,6525,6528,6531,6534,6537,6540,6543,6547,6550,6553,6556,6559,6562,6565,6568,6571,6574,6577,6580,6583,6587,6590,6593,6596,6599,6602,6605,6608,6611,6614,6617,6620,6623,6627,6630,6633,6636,6639,6642,6645,6648,6651,6654,6657,6660,6663,6667,6670,6673,6676,6679,6682,6685,6688,6691,6694,6697,6700,6703,6706,6710,6713,6716,6719,6722,6725,6728,6731,6734,6737,6740,6743,6746,6750,6753,6756,6759,6762,6765,6768,6771,6774,6777,6780,6783,6786,6789,6793,6796,6799,6802,6805,6808,6811,6814,6817,6820,6823,6826,6829,6833,6836,6839,6842,6845,6848,6851,6854,6857,6860,6863,6866,6869,6872,6876,6879,6882,6885,6888,6891,6894,6897,6900,6903,6906,6909,6912,6915,6919,6922,6925,6928,6931,6934,6937,6940,6943,6946,6949,6952,6955,6958,6961,6965,6968,6971,6974,6977,6980,6983,6986,6989,6992,6995,6998,7001,7004,7008,7011,7014,7017,7020,7023,7026,7029,7032,7035,7038,7041,7044,7047,7050,7054,7057,7060,7063,7066,7069,7072,7075,7078,7081,7084,7087,7090,7093,7097,7100,7103,7106,7109,7112,7115,7118,7121,7124,7127,7130,7133,7136,7139,7143,7146,7149,7152,7155,7158,7161,7164,7167,7170,7173,7176,7179,7182,7185,7188,7192,7195,7198,7201,7204,7207,7210,7213,7216,7219,7222,7225,7228,7231,7234,7238,7241,7244,7247,7250,7253,7256,7259,7262,7265,7268,7271,7274,7277,7280,7283,7287,7290,7293,7296,7299,7302,7305,7308,7311,7314,7317,7320,7323,7326,7329,7332,7336,7339,7342,7345,7348,7351,7354,7357,7360,7363,7366,7369,7372,7375,7378,7381,7385,7388,7391,7394,7397,7400,7403,7406,7409,7412,7415,7418,7421,7424,7427,7430,7433,7437,7440,7443,7446,7449,7452,7455,7458,7461,7464,7467,7470,7473,7476,7479,7482,7485,7489,7492,7495,7498,7501,7504,7507,7510,7513,7516,7519,7522,7525,7528,7531,7534,7537,7541,7544,7547,7550,7553,7556,7559,7562,7565,7568,7571,7574,7577,7580,7583,7586,7589,7592,7596,7599,7602,7605,7608,7611,7614,7617,7620,7623,7626,7629,7632,7635,7638,7641,7644,7647,7651,7654,7657,7660,7663,7666,7669,7672,7675,7678,7681,7684,7687,7690,7693,7696,7699,7702,7705,7709,7712,7715,7718,7721,7724,7727,7730,7733,7736,7739,7742,7745,7748,7751,7754,7757,7760,7764,7767,7770,7773,7776,7779,7782,7785,7788,7791,7794,7797,7800,7803,7806,7809,7812,7815,7818,7821,7825,7828,7831,7834,7837,7840,7843,7846,7849,7852,7855,7858,7861,7864,7867,7870,7873,7876,7879,7882,7886,7889,7892,7895,7898,7901,7904,7907,7910,7913,7916,7919,7922,7925,7928,7931,7934,7937,7940,7943,7946,7950,7953,7956,7959,7962,7965,7968,7971,7974,7977,7980,7983,7986,7989,7992,7995,7998,8001,8004,8007,8010,8014,8017,8020,8023,8026,8029,8032,8035,8038,8041,8044,8047,8050,8053,8056,8059,8062,8065,8068,8071,8074,8077,8081,8084,8087,8090,8093,8096,8099,8102,8105,8108,8111,8114,8117,8120,8123,8126,8129,8132,8135,8138,8141,8144,8147,8151,8154,8157,8160,8163,8166,8169,8172,8175,8178,8181,8184,8187,8190,8193,8196,8199,8202,8205,8208,8211,8214,8217,8220,8224,8227,8230,8233,8236,8239,8242,8245,8248,8251,8254,8257,8260,8263,8266,8269,8272,8275,8278,8281,8284,8287,8290,8293,8296,8300,8303,8306,8309,8312,8315,8318,8321,8324,8327,8330,8333,8336,8339,8342,8345,8348,8351,8354,8357,8360,8363,8366,8369,8372,8375,8379,8382,8385,8388,8391,8394,8397,8400,8403,8406,8409,8412,8415,8418,8421,8424,8427,8430,8433,8436,8439,8442,8445,8448,8451,8454,8457,8460,8464,8467,8470,8473,8476,8479,8482,8485,8488,8491,8494,8497,8500,8503,8506,8509,8512,8515,8518,8521,8524,8527,8530,8533,8536,8539,8542,8545,8548,8552,8555,8558,8561,8564,8567,8570,8573,8576,8579,8582,8585,8588,8591,8594,8597,8600,8603,8606,8609,8612,8615,8618,8621,8624,8627,8630,8633,8636,8639,8642,8645,8649,8652,8655,8658,8661,8664,8667,8670,8673,8676,8679,8682,8685,8688,8691,8694,8697,8700,8703,8706,8709,8712,8715,8718,8721,8724,8727,8730,8733,8736,8739,8742,8745,8748,8751,8755,8758,8761,8764,8767,8770,8773,8776,8779,8782,8785,8788,8791,8794,8797,8800,8803,8806,8809,8812,8815,8818,8821,8824,8827,8830,8833,8836,8839,8842,8845,8848,8851,8854,8857,8860,8863,8866,8869,8873,8876,8879,8882,8885,8888,8891,8894,8897,8900,8903,8906,8909,8912,8915,8918,8921,8924,8927,8930,8933,8936,8939,8942,8945,8948,8951,8954,8957,8960,8963,8966,8969,8972,8975,8978,8981,8984,8987,8990,8993,8996,8999,9002,9006,9009,9012,9015,9018,9021,9024,9027,9030,9033,9036,9039,9042,9045,9048,9051,9054,9057,9060,9063,9066,9069,9072,9075,9078,9081,9084,9087,9090,9093,9096,9099,9102,9105,9108,9111,9114,9117,9120,9123,9126,9129,9132,9135,9138,9141,9144,9147,9150,9153,9156,9159,9162,9165,9168,9172,9175,9178,9181,9184,9187,9190,9193,9196,9199,9202,9205,9208,9211,9214,9217,9220,9223,9226,9229,9232,9235,9238,9241,9244,9247,9250,9253,9256,9259,9262,9265,9268,9271,9274,9277,9280,9283,9286,9289,9292,9295,9298,9301,9304,9307,9310,9313,9316,9319,9322,9325,9328,9331,9334,9337,9340,9343,9346,9349,9352,9355,9358,9361,9364,9367,9370,9373,9376,9379,9382,9385,9388,9391,9394,9397,9400,9403,9406,9409,9413,9416,9419,9422,9425,9428,9431,9434,9437,9440,9443,9446,9449,9452,9455,9458,9461,9464,9467,9470,9473,9476,9479,9482,9485,9488,9491,9494,9497,9500,9503,9506,9509,9512,9515,9518,9521,9524,9527,9530,9533,9536,9539,9542,9545,9548,9551,9554,9557,9560,9563,9566,9569,9572,9575,9578,9581,9584,9587,9590,9593,9596,9599,9602,9605,9608,9611,9614,9617,9620,9623,9626,9629,9632,9635,9638,9641,9644,9647,9650,9653,9656,9659,9662,9665,9668,9671,9674,9677,9680,9683,9686,9689,9692,9695,9698,9701,9704,9707,9710,9713,9716,9719,9722,9725,9728,9731,9734,9737,9740,9743,9746,9749,9752,9755,9758,9761,9764,9767,9770,9773,9776,9779,9782,9785,9788,9791,9794,9797,9800,9803,9806,9809,9812,9815,9818,9821,9824,9827,9830,9833,9836,9839,9842,9845,9848,9851,9854,9857,9860,9863,9866,9869,9872,9875,9878,9881,9884,9887,9890,9893,9896,9899,9902,9905,9908,9911,9914,9917,9920,9923,9926,9929,9932,9935,9938,9941,9944,9947,9950,9953,9956,9959,9962,9965,9968,9971,9974,9977,9980,9983,9986,9989,9992,9995,9998,10001,10004,10007,10010,10013,10016,10019,10022,10025,10028,10031,10033,10036,10039,10042,10045,10048,10051,10054,10057,10060,10063,10066,10069,10072,10075,10078,10081,10084,10087,10090,10093,10096,10099,10102,10105,10108,10111,10114,10117,10120,10123,10126,10129,10132,10135,10138,10141,10144,10147,10150,10153,10156,10159,10162,10165,10168,10171,10174,10177,10180,10183,10186,10189,10192,10195,10198,10201,10204,10207,10210,10213,10216,10219,10222,10225,10228,10231,10234,10237,10240,10243,10246,10249,10252,10255,10258,10261,10263,10266,10269,10272,10275,10278,10281,10284,10287,10290,10293,10296,10299,10302,10305,10308,10311,10314,10317,10320,10323,10326,10329,10332,10335,10338,10341,10344,10347,10350,10353,10356,10359,10362,10365,10368,10371,10374,10377,10380,10383,10386,10389,10392,10395,10398,10401,10404,10407,10410,10413,10416,10419,10421,10424,10427,10430,10433,10436,10439,10442,10445,10448,10451,10454,10457,10460,10463,10466,10469,10472,10475,10478,10481,10484,10487,10490,10493,10496,10499,10502,10505,10508,10511,10514,10517,10520,10523,10526,10529,10532,10535,10538,10541,10544,10546,10549,10552,10555,10558,10561,10564,10567,10570,10573,10576,10579,10582,10585,10588,10591,10594,10597,10600,10603,10606,10609,10612,10615,10618,10621,10624,10627,10630,10633,10636,10639,10642,10645,10648,10651,10654,10656,10659,10662,10665,10668,10671,10674,10677,10680,10683,10686,10689,10692,10695,10698,10701,10704,10707,10710,10713,10716,10719,10722,10725,10728,10731,10734,10737,10740,10743,10746,10749,10751,10754,10757,10760,10763,10766,10769,10772,10775,10778,10781,10784,10787,10790,10793,10796,10799,10802,10805,10808,10811,10814,10817,10820,10823,10826,10829,10832,10835,10838,10840,10843,10846,10849,10852,10855,10858,10861,10864,10867,10870,10873,10876,10879,10882,10885,10888,10891,10894,10897,10900,10903,10906,10909,10912,10915,10918,10920,10923,10926,10929,10932,10935,10938,10941,10944,10947,10950,10953,10956,10959,10962,10965,10968,10971,10974,10977,10980,10983,10986,10989,10992,10994,10997,11000,11003,11006,11009,11012,11015,11018,11021,11024,11027,11030,11033,11036,11039,11042,11045,11048,11051,11054,11057,11060,11063,11065,11068,11071,11074,11077,11080,11083,11086,11089,11092,11095,11098,11101,11104,11107,11110,11113,11116,11119,11122,11125,11128,11131,11133,11136,11139,11142,11145,11148,11151,11154,11157,11160,11163,11166,11169,11172,11175,11178,11181,11184,11187,11190,11193,11195,11198,11201,11204,11207,11210,11213,11216,11219,11222,11225,11228,11231,11234,11237,11240,11243,11246,11249,11252,11255,11257,11260,11263,11266,11269,11272,11275,11278,11281,11284,11287,11290,11293,11296,11299,11302,11305,11308,11311,11314,11316,11319,11322,11325,11328,11331,11334,11337,11340,11343,11346,11349,11352,11355,11358,11361,11364,11367,11370,11372,11375,11378,11381,11384,11387,11390,11393,11396,11399,11402,11405,11408,11411,11414,11417,11420,11423,11425,11428,11431,11434,11437,11440,11443,11446,11449,11452,11455,11458,11461,11464,11467,11470,11473,11476,11478,11481,11484,11487,11490,11493,11496,11499,11502,11505,11508,11511,11514,11517,11520,11523,11526,11528,11531,11534,11537,11540,11543,11546,11549,11552,11555,11558,11561,11564,11567,11570,11573,11575,11578,11581,11584,11587,11590,11593,11596,11599,11602,11605,11608,11611,11614,11617,11620,11623,11625,11628,11631,11634,11637,11640,11643,11646,11649,11652,11655,11658,11661,11664,11667,11669,11672,11675,11678,11681,11684,11687,11690,11693,11696,11699,11702,11705,11708,11711,11714,11716,11719,11722,11725,11728,11731,11734,11737,11740,11743,11746,11749,11752,11755,11758,11760,11763,11766,11769,11772,11775,11778,11781,11784,11787,11790,11793,11796,11799,11801,11804,11807,11810,11813,11816,11819,11822,11825,11828,11831,11834,11837,11840,11842,11845,11848,11851,11854,11857,11860,11863,11866,11869,11872,11875,11878,11881,11883,11886,11889,11892,11895,11898,11901,11904,11907,11910,11913,11916,11919,11922,11924,11927,11930,11933,11936,11939,11942,11945,11948,11951,11954,11957,11960,11962,11965,11968,11971,11974,11977,11980,11983,11986,11989,11992,11995,11998,12001,12003,12006,12009,12012,12015,12018,12021,12024,12027,12030,12033,12036,12038,12041,12044,12047,12050,12053,12056,12059,12062,12065,12068,12071,12074,12076,12079,12082,12085,12088,12091,12094,12097,12100,12103,12106,12109,12112,12114,12117,12120,12123,12126,12129,12132,12135,12138,12141,12144,12147,12149,12152,12155,12158,12161,12164,12167,12170,12173,12176,12179,12182,12184,12187,12190,12193,12196,12199,12202,12205,12208,12211,12214,12217,12219,12222,12225,12228,12231,12234,12237,12240,12243,12246,12249,12251,12254,12257,12260,12263,12266,12269,12272,12275,12278,12281,12284,12286,12289,12292,12295,12298,12301,12304,12307,12310,12313,12316,12318,12321,12324,12327,12330,12333,12336,12339,12342,12345,12348,12350,12353,12356,12359,12362,12365,12368,12371,12374,12377,12380,12382,12385,12388,12391,12394,12397,12400,12403,12406,12409,12412,12414,12417,12420,12423,12426,12429,12432,12435,12438,12441,12444,12446,12449,12452,12455,12458,12461,12464,12467,12470,12473,12476,12478,12481,12484,12487,12490,12493,12496,12499,12502,12505,12507,12510,12513,12516,12519,12522,12525,12528,12531,12534,12536,12539,12542,12545,12548,12551,12554,12557,12560,12563,12566,12568,12571,12574,12577,12580,12583,12586,12589,12592,12595,12597,12600,12603,12606,12609,12612,12615,12618,12621,12624,12626,12629,12632,12635,12638,12641,12644,12647,12650,12652,12655,12658,12661,12664,12667,12670,12673,12676,12679,12681,12684,12687,12690,12693,12696,12699,12702,12705,12708,12710,12713,12716,12719,12722,12725,12728,12731,12734,12736,12739,12742,12745,12748,12751,12754,12757,12760,12763,12765,12768,12771,12774,12777,12780,12783,12786,12789,12791,12794,12797,12800,12803,12806,12809,12812,12815,12817,12820,12823,12826,12829,12832,12835,12838,12841,12843,12846,12849,12852,12855,12858,12861,12864,12867,12870,12872,12875,12878,12881,12884,12887,12890,12893,12895,12898,12901,12904,12907,12910,12913,12916,12919,12921,12924,12927,12930,12933,12936,12939,12942,12945,12947,12950,12953,12956,12959,12962,12965,12968,12971,12973,12976,12979,12982,12985,12988,12991,12994,12997,12999,13002,13005,13008,13011,13014,13017,13020,13022,13025,13028,13031,13034,13037,13040,13043,13046,13048,13051,13054,13057,13060,13063,13066,13069,13071,13074,13077,13080,13083,13086,13089,13092,13094,13097,13100,13103,13106,13109,13112,13115,13118,13120,13123,13126,13129,13132,13135,13138,13141,13143,13146,13149,13152,13155,13158,13161,13164,13166,13169,13172,13175,13178,13181,13184,13187,13189,13192,13195,13198,13201,13204,13207,13210,13212,13215,13218,13221,13224,13227,13230,13233,13235,13238,13241,13244,13247,13250,13253,13256,13258,13261,13264,13267,13270,13273,13276,13279,13281,13284,13287,13290,13293,13296,13299,13302,13304,13307,13310,13313,13316,13319,13322,13324,13327,13330,13333,13336,13339,13342,13345,13347,13350,13353,13356,13359,13362,13365,13368,13370,13373,13376,13379,13382,13385,13388,13390,13393,13396,13399,13402,13405,13408,13411,13413,13416,13419,13422,13425,13428,13431,13433,13436,13439,13442,13445,13448,13451,13454,13456,13459,13462,13465,13468,13471,13474,13476,13479,13482,13485,13488,13491,13494,13496,13499,13502,13505,13508,13511,13514,13516,13519,13522,13525,13528,13531,13534,13537,13539,13542,13545,13548,13551,13554,13557,13559,13562,13565,13568,13571,13574,13577,13579,13582,13585,13588,13591,13594,13597,13599,13602,13605,13608,13611,13614,13617,13619,13622,13625,13628,13631,13634,13637,13639,13642,13645,13648,13651,13654,13657,13659,13662,13665,13668,13671,13674,13677,13679,13682,13685,13688,13691,13694,13697,13699,13702,13705,13708,13711,13714,13717,13719,13722,13725,13728,13731,13734,13736,13739,13742,13745,13748,13751,13754,13756,13759,13762,13765,13768,13771,13774,13776,13779,13782,13785,13788,13791,13793,13796,13799,13802,13805,13808,13811,13813,13816,13819,13822,13825,13828,13831,13833,13836,13839,13842,13845,13848,13850,13853,13856,13859,13862,13865,13868,13870,13873,13876,13879,13882,13885,13887,13890,13893,13896,13899,13902,13905,13907,13910,13913,13916,13919,13922,13924,13927,13930,13933,13936,13939,13942,13944,13947,13950,13953,13956,13959,13961,13964,13967,13970,13973,13976,13978,13981,13984,13987,13990,13993,13995,13998,14001,14004,14007,14010,14013,14015,14018,14021,14024,14027,14030,14032,14035,14038,14041,14044,14047,14049,14052,14055,14058,14061,14064,14066,14069,14072,14075,14078,14081,14083,14086,14089,14092,14095,14098,14101,14103,14106,14109,14112,14115,14118,14120,14123,14126,14129,14132,14135,14137,14140,14143,14146,14149,14152,14154,14157,14160,14163,14166,14169,14171,14174,14177,14180,14183,14186,14188,14191,14194,14197,14200,14203,14205,14208,14211,14214,14217,14219,14222,14225,14228,14231,14234,14236,14239,14242,14245,14248,14251,14253,14256,14259,14262,14265,14268,14270,14273,14276,14279,14282,14285,14287,14290,14293,14296,14299,14302,14304,14307,14310,14313,14316,14318,14321,14324,14327,14330,14333,14335,14338,14341,14344,14347,14350,14352,14355,14358,14361,14364,14366,14369,14372,14375,14378,14381,14383,14386,14389,14392,14395,14398,14400,14403,14406,14409,14412,14414,14417,14420,14423,14426,14429,14431,14434,14437,14440,14443,14445,14448,14451,14454,14457,14460,14462,14465,14468,14471,14474,14477,14479,14482,14485,14488,14491,14493,14496,14499,14502,14505,14507,14510,14513,14516,14519,14522,14524,14527,14530,14533,14536,14538,14541,14544,14547,14550,14553,14555,14558,14561,14564,14567,14569,14572,14575,14578,14581,14584,14586,14589,14592,14595,14598,14600,14603,14606,14609,14612,14614,14617,14620,14623,14626,14628,14631,14634,14637,14640,14643,14645,14648,14651,14654,14657,14659,14662,14665,14668,14671,14673,14676,14679,14682,14685,14688,14690,14693,14696,14699,14702,14704,14707,14710,14713,14716,14718,14721,14724,14727,14730,14732,14735,14738,14741,14744,14746,14749,14752,14755,14758,14760,14763,14766,14769,14772,14774,14777,14780,14783,14786,14789,14791,14794,14797,14800,14803,14805,14808,14811,14814,14817,14819,14822,14825,14828,14831,14833,14836,14839,14842,14845,14847,14850,14853,14856,14859,14861,14864,14867,14870,14873,14875,14878,14881,14884,14887,14889,14892,14895,14898,14901,14903,14906,14909,14912,14915,14917,14920,14923,14926,14929,14931,14934,14937,14940,14942,14945,14948,14951,14954,14956,14959,14962,14965,14968,14970,14973,14976,14979,14982,14984,14987,14990,14993,14996,14998,15001,15004,15007,15010,15012,15015,15018,15021,15024,15026,15029,15032,15035,15037,15040,15043,15046,15049,15051,15054,15057,15060,15063,15065,15068,15071,15074,15077,15079,15082,15085,15088,15090,15093,15096,15099,15102,15104,15107,15110,15113,15116,15118,15121,15124,15127,15129,15132,15135,15138,15141,15143,15146,15149,15152,15155,15157,15160,15163,15166,15168,15171,15174,15177,15180,15182,15185,15188,15191,15194,15196,15199,15202,15205,15207,15210,15213,15216,15219,15221,15224,15227,15230,15233,15235,15238,15241,15244,15246,15249,15252,15255,15258,15260,15263,15266,15269,15271,15274,15277,15280,15283,15285,15288,15291,15294,15296,15299,15302,15305,15308,15310,15313,15316,15319,15321,15324,15327,15330,15333,15335,15338,15341,15344,15346,15349,15352,15355,15358,15360,15363,15366,15369,15371,15374,15377,15380,15382,15385,15388,15391,15394,15396,15399,15402,15405,15407,15410,15413,15416,15419,15421,15424,15427,15430,15432,15435,15438,15441,15443,15446,15449,15452,15455,15457,15460,15463,15466,15468,15471,15474,15477,15479,15482,15485,15488,15491,15493,15496,15499,15502,15504,15507,15510,15513,15515,15518,15521,15524,15527,15529,15532,15535,15538,15540,15543,15546,15549,15551,15554,15557,15560,15562,15565,15568,15571,15574,15576,15579,15582,15585,15587,15590,15593,15596,15598,15601,15604,15607,15609,15612,15615,15618,15621,15623,15626,15629,15632,15634,15637,15640,15643,15645,15648,15651,15654,15656,15659,15662,15665,15667,15670,15673,15676,15678,15681,15684,15687,15690,15692,15695,15698,15701,15703,15706,15709,15712,15714,15717,15720,15723,15725,15728,15731,15734,15736,15739,15742,15745,15747,15750,15753,15756,15758,15761,15764,15767,15769,15772,15775,15778,15780,15783,15786,15789,15791,15794,15797,15800,15802,15805,15808,15811,15813,15816,15819,15822,15824,15827,15830,15833,15835,15838,15841,15844,15846,15849,15852,15855,15857,15860,15863,15866,15868,15871,15874,15877,15879,15882,15885,15888,15890,15893,15896,15899,15901,15904,15907,15910,15912,15915,15918,15921,15923,15926,15929,15932,15934,15937,15940,15943,15945,15948,15951,15954,15956,15959,15962,15965,15967,15970,15973,15976,15978,15981,15984,15987,15989,15992,15995,15997,16000,16003,16006,16008,16011,16014,16017,16019,16022,16025,16028,16030,16033,16036,16039,16041,16044,16047,16050,16052,16055,16058,16061,16063,16066,16069,16071,16074,16077,16080,16082,16085,16088,16091,16093,16096,16099,16102,16104,16107,16110,16113,16115,16118,16121,16123,16126,16129,16132,16134,16137,16140,16143,16145,16148,16151,16154,16156,16159,16162,16164,16167,16170,16173,16175,16178,16181,16184,16186,16189,16192,16195,16197,16200,16203,16205,16208,16211,16214,16216,16219,16222,16225,16227,16230,16233,16235,16238,16241,16244,16246,16249,16252,16255,16257,16260,16263,16265,16268,16271,16274,16276,16279,16282,16285,16287,16290,16293,16295,16298,16301,16304,16306,16309,16312,16315,16317,16320,16323,16325,16328,16331,16334,16336,16339,16342,16344,16347,16350,16353,16355,16358,16361,16364,16366,16369,16372,16374,16377,16380,16383,16385,16388,16391,16393,16396,16399,16402,16404,16407,16410,16413,16415,16418,16421,16423,16426,16429,16432,16434,16437,16440,16442,16445,16448,16451,16453,16456,16459,16461,16464,16467,16470,16472,16475,16478,16480,16483,16486,16489,16491,16494,16497,16499,16502,16505,16508,16510,16513,16516,16518,16521,16524,16527,16529,16532,16535,16537,16540,16543,16546,16548,16551,16554,16556,16559,16562,16565,16567,16570,16573,16575,16578,16581,16584,16586,16589,16592,16594,16597,16600,16602,16605,16608,16611,16613,16616,16619,16621,16624,16627,16630,16632,16635,16638,16640,16643,16646,16648,16651,16654,16657,16659,16662,16665,16667,16670,16673,16676,16678,16681,16684,16686,16689,16692,16694,16697,16700,16703,16705,16708,16711,16713,16716,16719,16721,16724,16727,16730,16732,16735,16738,16740,16743,16746,16749,16751,16754,16757,16759,16762,16765,16767,16770,16773,16775,16778,16781,16784,16786,16789,16792,16794,16797,16800,16802,16805,16808,16811,16813,16816,16819,16821,16824,16827,16829,16832,16835,16838,16840,16843,16846,16848,16851,16854,16856,16859,16862,16864,16867,16870,16873,16875,16878,16881,16883,16886,16889,16891,16894,16897,16899,16902,16905,16908,16910,16913,16916,16918,16921,16924,16926,16929,16932,16934,16937,16940,16943,16945,16948,16951,16953,16956,16959,16961,16964,16967,16969,16972,16975,16977,16980,16983,16986,16988,16991,16994,16996,16999,17002,17004,17007,17010,17012,17015,17018,17020,17023,17026,17028,17031,17034,17037,17039,17042,17045,17047,17050,17053,17055,17058,17061,17063,17066,17069,17071,17074,17077,17079,17082,17085,17087,17090,17093,17096,17098,17101,17104,17106,17109,17112,17114,17117,17120,17122,17125,17128,17130,17133,17136,17138,17141,17144,17146,17149,17152,17154,17157,17160,17162,17165,17168,17171,17173,17176,17179,17181,17184,17187,17189,17192,17195,17197,17200,17203,17205,17208,17211,17213,17216,17219,17221,17224,17227,17229,17232,17235,17237,17240,17243,17245,17248,17251,17253,17256,17259,17261,17264,17267,17269,17272,17275,17277,17280,17283,17285,17288,17291,17293,17296,17299,17301,17304,17307,17309,17312,17315,17317,17320,17323,17325,17328,17331,17333,17336,17339,17341,17344,17347,17349,17352,17355,17357,17360,17363,17365,17368,17371,17373,17376,17379,17381,17384,17387,17389,17392,17395,17397,17400,17403,17405,17408,17411,17413,17416,17419,17421,17424,17427,17429,17432,17435,17437,17440,17443,17445,17448,17451,17453,17456,17459,17461,17464,17467,17469,17472,17474,17477,17480,17482,17485,17488,17490,17493,17496,17498,17501,17504,17506,17509,17512,17514,17517,17520,17522,17525,17528,17530,17533,17536,17538,17541,17544,17546,17549,17551,17554,17557,17559,17562,17565,17567,17570,17573,17575,17578,17581,17583,17586,17589,17591,17594,17597,17599,17602,17605,17607,17610,17612,17615,17618,17620,17623,17626,17628,17631,17634,17636,17639,17642,17644,17647,17650,17652,17655,17657,17660,17663,17665,17668,17671,17673,17676,17679,17681,17684,17687,17689,17692,17695,17697,17700,17702,17705,17708,17710,17713,17716,17718,17721,17724,17726,17729,17732,17734,17737,17739,17742,17745,17747,17750,17753,17755,17758,17761,17763,17766,17768,17771,17774,17776,17779,17782,17784,17787,17790,17792,17795,17798,17800,17803,17805,17808,17811,17813,17816,17819,17821,17824,17827,17829,17832,17834,17837,17840,17842,17845,17848,17850,17853,17855,17858,17861,17863,17866,17869,17871,17874,17877,17879,17882,17884,17887,17890,17892,17895,17898,17900,17903,17906,17908,17911,17913,17916,17919,17921,17924,17927,17929,17932,17934,17937,17940,17942,17945,17948,17950,17953,17955,17958,17961,17963,17966,17969,17971,17974,17976,17979,17982,17984,17987,17990,17992,17995,17997,18000,18003,18005,18008,18011,18013,18016,18018,18021,18024,18026,18029,18032,18034,18037,18039,18042,18045,18047,18050,18053,18055,18058,18060,18063,18066,18068,18071,18074,18076,18079,18081,18084,18087,18089,18092,18095,18097,18100,18102,18105,18108,18110,18113,18115,18118,18121,18123,18126,18129,18131,18134,18136,18139,18142,18144,18147,18149,18152,18155,18157,18160,18163,18165,18168,18170,18173,18176,18178,18181,18183,18186,18189,18191,18194,18197,18199,18202,18204,18207,18210,18212,18215,18217,18220,18223,18225,18228,18230,18233,18236,18238,18241,18244,18246,18249,18251,18254,18257,18259,18262,18264,18267,18270,18272,18275,18277,18280,18283,18285,18288,18290,18293,18296,18298,18301,18304,18306,18309,18311,18314,18317,18319,18322,18324,18327,18330,18332,18335,18337,18340,18343,18345,18348,18350,18353,18356,18358,18361,18363,18366,18369,18371,18374,18376,18379,18382,18384,18387,18389,18392,18395,18397,18400,18402,18405,18408,18410,18413,18415,18418,18421,18423,18426,18428,18431,18434,18436,18439,18441,18444,18447,18449,18452,18454,18457,18460,18462,18465,18467,18470,18473,18475,18478,18480,18483,18485,18488,18491,18493,18496,18498,18501,18504,18506,18509,18511,18514,18517,18519,18522,18524,18527,18530,18532,18535,18537,18540,18543,18545,18548,18550,18553,18555,18558,18561,18563,18566,18568,18571,18574,18576,18579,18581,18584,18587,18589,18592,18594,18597,18599,18602,18605,18607,18610,18612,18615,18618,18620,18623,18625,18628,18630,18633,18636,18638,18641,18643,18646,18649,18651,18654,18656,18659,18661,18664,18667,18669,18672,18674,18677,18680,18682,18685,18687,18690,18692,18695,18698,18700,18703,18705,18708,18711,18713,18716,18718,18721,18723,18726,18729,18731,18734,18736,18739,18741,18744,18747,18749,18752,18754,18757,18759,18762,18765,18767,18770,18772,18775,18778,18780,18783,18785,18788,18790,18793,18796,18798,18801,18803,18806,18808,18811,18814,18816,18819,18821,18824,18826,18829,18832,18834,18837,18839,18842,18844,18847,18850,18852,18855,18857,18860,18862,18865,18868,18870,18873,18875,18878,18880,18883,18885,18888,18891,18893,18896,18898,18901,18903,18906,18909,18911,18914,18916,18919,18921,18924,18927,18929,18932,18934,18937,18939,18942,18944,18947,18950,18952,18955,18957,18960,18962,18965,18968,18970,18973,18975,18978,18980,18983,18985,18988,18991,18993,18996,18998,19001,19003,19006,19009,19011,19014,19016,19019,19021,19024,19026,19029,19032,19034,19037,19039,19042,19044,19047,19049,19052,19055,19057,19060,19062,19065,19067,19070,19072,19075,19078,19080,19083,19085,19088,19090,19093,19095,19098,19101,19103,19106,19108,19111,19113,19116,19118,19121,19123,19126,19129,19131,19134,19136,19139,19141,19144,19146,19149,19152,19154,19157,19159,19162,19164,19167,19169,19172,19174,19177,19180,19182,19185,19187,19190,19192,19195,19197,19200,19202,19205,19208,19210,19213,19215,19218,19220,19223,19225,19228,19230,19233,19236,19238,19241,19243,19246,19248,19251,19253,19256,19258,19261,19264,19266,19269,19271,19274,19276,19279,19281,19284,19286,19289,19291,19294,19297,19299,19302,19304,19307,19309,19312,19314,19317,19319,19322,19324,19327,19330,19332,19335,19337,19340,19342,19345,19347,19350,19352,19355,19357,19360,19362,19365,19368,19370,19373,19375,19378,19380,19383,19385,19388,19390,19393,19395,19398,19400,19403,19406,19408,19411,19413,19416,19418,19421,19423,19426,19428,19431,19433,19436,19438,19441,19444,19446,19449,19451,19454,19456,19459,19461,19464,19466,19469,19471,19474,19476,19479,19481,19484,19486,19489,19492,19494,19497,19499,19502,19504,19507,19509,19512,19514,19517,19519,19522,19524,19527,19529,19532,19534,19537,19539,19542,19545,19547,19550,19552,19555,19557,19560,19562,19565,19567,19570,19572,19575,19577,19580,19582,19585,19587,19590,19592,19595,19597,19600,19602,19605,19607,19610,19613,19615,19618,19620,19623,19625,19628,19630,19633,19635,19638,19640,19643,19645,19648,19650,19653,19655,19658,19660,19663,19665,19668,19670,19673,19675,19678,19680,19683,19685,19688,19690,19693,19695,19698,19700,19703,19706,19708,19711,19713,19716,19718,19721,19723,19726,19728,19731,19733,19736,19738,19741,19743,19746,19748,19751,19753,19756,19758,19761,19763,19766,19768,19771,19773,19776,19778,19781,19783,19786,19788,19791,19793,19796,19798,19801,19803,19806,19808,19811,19813,19816,19818,19821,19823,19826,19828,19831,19833,19836,19838,19841,19843,19846,19848,19851,19853,19856,19858,19861,19863,19866,19868,19871,19873,19876,19878,19881,19883,19886,19888,19891,19893,19896,19898,19901,19903,19906,19908,19911,19913,19916,19918,19921,19923,19926,19928,19931,19933,19936,19938,19941,19943,19946,19948,19951,19953,19956,19958,19961,19963,19966,19968,19971,19973,19976,19978,19981,19983,19985,19988,19990,19993,19995,19998,20000,20003,20005,20008,20010,20013,20015,20018,20020,20023,20025,20028,20030,20033,20035,20038,20040,20043,20045,20048,20050,20053,20055,20058,20060,20063,20065,20068,20070,20072,20075,20077,20080,20082,20085,20087,20090,20092,20095,20097,20100,20102,20105,20107,20110,20112,20115,20117,20120,20122,20125,20127,20130,20132,20135,20137,20139,20142,20144,20147,20149,20152,20154,20157,20159,20162,20164,20167,20169,20172,20174,20177,20179,20182,20184,20187,20189,20191,20194,20196,20199,20201,20204,20206,20209,20211,20214,20216,20219,20221,20224,20226,20229,20231,20234,20236,20238,20241,20243,20246,20248,20251,20253,20256,20258,20261,20263,20266,20268,20271,20273,20275,20278,20280,20283,20285,20288,20290,20293,20295,20298,20300,20303,20305,20308,20310,20312,20315,20317,20320,20322,20325,20327,20330,20332,20335,20337,20340,20342,20345,20347,20349,20352,20354,20357,20359,20362,20364,20367,20369,20372,20374,20377,20379,20381,20384,20386,20389,20391,20394,20396,20399,20401,20404,20406,20408,20411,20413,20416,20418,20421,20423,20426,20428,20431,20433,20436,20438,20440,20443,20445,20448,20450,20453,20455,20458,20460,20463,20465,20467,20470,20472,20475,20477,20480,20482,20485,20487,20489,20492,20494,20497,20499,20502,20504,20507,20509,20512,20514,20516,20519,20521,20524,20526,20529,20531,20534,20536,20538,20541,20543,20546,20548,20551,20553,20556,20558,20560,20563,20565,20568,20570,20573,20575,20578,20580,20583,20585,20587,20590,20592,20595,20597,20600,20602,20604,20607,20609,20612,20614,20617,20619,20622,20624,20626,20629,20631,20634,20636,20639,20641,20644,20646,20648,20651,20653,20656,20658,20661,20663,20666,20668,20670,20673,20675,20678,20680,20683,20685,20687,20690,20692,20695,20697,20700,20702,20704,20707,20709,20712,20714,20717,20719,20722,20724,20726,20729,20731,20734,20736,20739,20741,20743,20746,20748,20751,20753,20756,20758,20760,20763,20765,20768,20770,20773,20775,20777,20780,20782,20785,20787,20790,20792,20794,20797,20799,20802,20804,20807,20809,20811,20814,20816,20819,20821,20824,20826,20828,20831,20833,20836,20838,20841,20843,20845,20848,20850,20853,20855,20858,20860,20862,20865,20867,20870,20872,20874,20877,20879,20882,20884,20887,20889,20891,20894,20896,20899,20901,20904,20906,20908,20911,20913,20916,20918,20920,20923,20925,20928,20930,20933,20935,20937,20940,20942,20945,20947,20949,20952,20954,20957,20959,20962,20964,20966,20969,20971,20974,20976,20978,20981,20983,20986,20988,20990,20993,20995,20998,21000,21003,21005,21007,21010,21012,21015,21017,21019,21022,21024,21027,21029,21031,21034,21036,21039,21041,21043,21046,21048,21051,21053,21056,21058,21060,21063,21065,21068,21070,21072,21075,21077,21080,21082,21084,21087,21089,21092,21094,21096,21099,21101,21104,21106,21108,21111,21113,21116,21118,21120,21123,21125,21128,21130,21132,21135,21137,21140,21142,21144,21147,21149,21152,21154,21156,21159,21161,21164,21166,21168,21171,21173,21176,21178,21180,21183,21185,21188,21190,21192,21195,21197,21200,21202,21204,21207,21209,21212,21214,21216,21219,21221,21224,21226,21228,21231,21233,21236,21238,21240,21243,21245,21247,21250,21252,21255,21257,21259,21262,21264,21267,21269,21271,21274,21276,21279,21281,21283,21286,21288,21290,21293,21295,21298,21300,21302,21305,21307,21310,21312,21314,21317,21319,21322,21324,21326,21329,21331,21333,21336,21338,21341,21343,21345,21348,21350,21353,21355,21357,21360,21362,21364,21367,21369,21372,21374,21376,21379,21381,21383,21386,21388,21391,21393,21395,21398,21400,21403,21405,21407,21410,21412,21414,21417,21419,21422,21424,21426,21429,21431,21433,21436,21438,21441,21443,21445,21448,21450,21452,21455,21457,21460,21462,21464,21467,21469,21471,21474,21476,21479,21481,21483,21486,21488,21490,21493,21495,21498,21500,21502,21505,21507,21509,21512,21514,21516,21519,21521,21524,21526,21528,21531,21533,21535,21538,21540,21543,21545,21547,21550,21552,21554,21557,21559,21561,21564,21566,21569,21571,21573,21576,21578,21580,21583,21585,21587,21590,21592,21595,21597,21599,21602,21604,21606,21609,21611,21613,21616,21618,21621,21623,21625,21628,21630,21632,21635,21637,21639,21642,21644,21646,21649,21651,21654,21656,21658,21661,21663,21665,21668,21670,21672,21675,21677,21679,21682,21684,21687,21689,21691,21694,21696,21698,21701,21703,21705,21708,21710,21712,21715,21717,21719,21722,21724,21727,21729,21731,21734,21736,21738,21741,21743,21745,21748,21750,21752,21755,21757,21759,21762,21764,21766,21769,21771,21774,21776,21778,21781,21783,21785,21788,21790,21792,21795,21797,21799,21802,21804,21806,21809,21811,21813,21816,21818,21820,21823,21825,21827,21830,21832,21835,21837,21839,21842,21844,21846,21849,21851,21853,21856,21858,21860,21863,21865,21867,21870,21872,21874,21877,21879,21881,21884,21886,21888,21891,21893,21895,21898,21900,21902,21905,21907,21909,21912,21914,21916,21919,21921,21923,21926,21928,21930,21933,21935,21937,21940,21942,21944,21947,21949,21951,21954,21956,21958,21961,21963,21965,21968,21970,21972,21975,21977,21979,21982,21984,21986,21989,21991,21993,21996,21998,22000,22003,22005,22007,22010,22012,22014,22017,22019,22021,22024,22026,22028,22031,22033,22035,22038,22040,22042,22045,22047,22049,22051,22054,22056,22058,22061,22063,22065,22068,22070,22072,22075,22077,22079,22082,22084,22086,22089,22091,22093,22096,22098,22100,22103,22105,22107,22110,22112,22114,22116,22119,22121,22123,22126,22128,22130,22133,22135,22137,22140,22142,22144,22147,22149,22151,22154,22156,22158,22160,22163,22165,22167,22170,22172,22174,22177,22179,22181,22184,22186,22188,22191,22193,22195,22197,22200,22202,22204,22207,22209,22211,22214,22216,22218,22221,22223,22225,22227,22230,22232,22234,22237,22239,22241,22244,22246,22248,22251,22253,22255,22257,22260,22262,22264,22267,22269,22271,22274,22276,22278,22281,22283,22285,22287,22290,22292,22294,22297,22299,22301,22304,22306,22308,22310,22313,22315,22317,22320,22322,22324,22327,22329,22331,22333,22336,22338,22340,22343,22345,22347,22350,22352,22354,22356,22359,22361,22363,22366,22368,22370,22373,22375,22377,22379,22382,22384,22386,22389,22391,22393,22395,22398,22400,22402,22405,22407,22409,22411,22414,22416,22418,22421,22423,22425,22428,22430,22432,22434,22437,22439,22441,22444,22446,22448,22450,22453,22455,22457,22460,22462,22464,22466,22469,22471,22473,22476,22478,22480,22482,22485,22487,22489,22492,22494,22496,22498,22501,22503,22505,22508,22510,22512,22514,22517,22519,22521,22524,22526,22528,22530,22533,22535,22537,22540,22542,22544,22546,22549,22551,22553,22555,22558,22560,22562,22565,22567,22569,22571,22574,22576,22578,22581,22583,22585,22587,22590,22592,22594,22596,22599,22601,22603,22606,22608,22610,22612,22615,22617,22619,22621,22624,22626,22628,22631,22633,22635,22637,22640,22642,22644,22646,22649,22651,22653,22656,22658,22660,22662,22665,22667,22669,22671,22674,22676,22678,22680,22683,22685,22687,22690,22692,22694,22696,22699,22701,22703,22705,22708,22710,22712,22714,22717,22719,22721,22724,22726,22728,22730,22733,22735,22737,22739,22742,22744,22746,22748,22751,22753,22755,22757,22760,22762,22764,22766,22769,22771,22773,22776,22778,22780,22782,22785,22787,22789,22791,22794,22796,22798,22800,22803,22805,22807,22809,22812,22814,22816,22818,22821,22823,22825,22827,22830,22832,22834,22836,22839,22841,22843,22845,22848,22850,22852,22854,22857,22859,22861,22863,22866,22868,22870,22872,22875,22877,22879,22881,22884,22886,22888,22890,22893,22895,22897,22899,22902,22904,22906,22908,22911,22913,22915,22917,22920,22922,22924,22926,22929,22931,22933,22935,22938,22940,22942,22944,22947,22949,22951,22953,22956,22958,22960,22962,22965,22967,22969,22971,22973,22976,22978,22980,22982,22985,22987,22989,22991,22994,22996,22998,23000,23003,23005,23007,23009,23012,23014,23016,23018,23020,23023,23025,23027,23029,23032,23034,23036,23038,23041,23043,23045,23047,23050,23052,23054,23056,23058,23061,23063,23065,23067,23070,23072,23074,23076,23079,23081,23083,23085,23087,23090,23092,23094,23096,23099,23101,23103,23105,23107,23110,23112,23114,23116,23119,23121,23123,23125,23128,23130,23132,23134,23136,23139,23141,23143,23145,23148,23150,23152,23154,23156,23159,23161,23163,23165,23168,23170,23172,23174,23176,23179,23181,23183,23185,23188,23190,23192,23194,23196,23199,23201,23203,23205,23208,23210,23212,23214,23216,23219,23221,23223,23225,23227,23230,23232,23234,23236,23239,23241,23243,23245,23247,23250,23252,23254,23256,23258,23261,23263,23265,23267,23270,23272,23274,23276,23278,23281,23283,23285,23287,23289,23292,23294,23296,23298,23300,23303,23305,23307,23309,23311,23314,23316,23318,23320,23323,23325,23327,23329,23331,23334,23336,23338,23340,23342,23345,23347,23349,23351,23353,23356,23358,23360,23362,23364,23367,23369,23371,23373,23375,23378,23380,23382,23384,23386,23389,23391,23393,23395,23397,23400,23402,23404,23406,23408,23411,23413,23415,23417,23419,23422,23424,23426,23428,23430,23433,23435,23437,23439,23441,23444,23446,23448,23450,23452,23455,23457,23459,23461,23463,23466,23468,23470,23472,23474,23476,23479,23481,23483,23485,23487,23490,23492,23494,23496,23498,23501,23503,23505,23507,23509,23512,23514,23516,23518,23520,23522,23525,23527,23529,23531,23533,23536,23538,23540,23542,23544,23546,23549,23551,23553,23555,23557,23560,23562,23564,23566,23568,23571,23573,23575,23577,23579,23581,23584,23586,23588,23590,23592,23595,23597,23599,23601,23603,23605,23608,23610,23612,23614,23616,23618,23621,23623,23625,23627,23629,23632,23634,23636,23638,23640,23642,23645,23647,23649,23651,23653,23655,23658,23660,23662,23664,23666,23668,23671,23673,23675,23677,23679,23682,23684,23686,23688,23690,23692,23695,23697,23699,23701,23703,23705,23708,23710,23712,23714,23716,23718,23721,23723,23725,23727,23729,23731,23734,23736,23738,23740,23742,23744,23747,23749,23751,23753,23755,23757,23760,23762,23764,23766,23768,23770,23773,23775,23777,23779,23781,23783,23785,23788,23790,23792,23794,23796,23798,23801,23803,23805,23807,23809,23811,23814,23816,23818,23820,23822,23824,23827,23829,23831,23833,23835,23837,23839,23842,23844,23846,23848,23850,23852,23855,23857,23859,23861,23863,23865,23867,23870,23872,23874,23876,23878,23880,23883,23885,23887,23889,23891,23893,23895,23898,23900,23902,23904,23906,23908,23910,23913,23915,23917,23919,23921,23923,23925,23928,23930,23932,23934,23936,23938,23940,23943,23945,23947,23949,23951,23953,23956,23958,23960,23962,23964,23966,23968,23971,23973,23975,23977,23979,23981,23983,23985,23988,23990,23992,23994,23996,23998,24000,24003,24005,24007,24009,24011,24013,24015,24018,24020,24022,24024,24026,24028,24030,24033,24035,24037,24039,24041,24043,24045,24047,24050,24052,24054,24056,24058,24060,24062,24065,24067,24069,24071,24073,24075,24077,24079,24082,24084,24086,24088,24090,24092,24094,24096,24099,24101,24103,24105,24107,24109,24111,24114,24116,24118,24120,24122,24124,24126,24128,24131,24133,24135,24137,24139,24141,24143,24145,24148,24150,24152,24154,24156,24158,24160,24162,24164,24167,24169,24171,24173,24175,24177,24179,24181,24184,24186,24188,24190,24192,24194,24196,24198,24201,24203,24205,24207,24209,24211,24213,24215,24217,24220,24222,24224,24226,24228,24230,24232,24234,24237,24239,24241,24243,24245,24247,24249,24251,24253,24256,24258,24260,24262,24264,24266,24268,24270,24272,24275,24277,24279,24281,24283,24285,24287,24289,24291,24294,24296,24298,24300,24302,24304,24306,24308,24310,24312,24315,24317,24319,24321,24323,24325,24327,24329,24331,24334,24336,24338,24340,24342,24344,24346,24348,24350,24352,24355,24357,24359,24361,24363,24365,24367,24369,24371,24373,24376,24378,24380,24382,24384,24386,24388,24390,24392,24394,24397,24399,24401,24403,24405,24407,24409,24411,24413,24415,24417,24420,24422,24424,24426,24428,24430,24432,24434,24436,24438,24441,24443,24445,24447,24449,24451,24453,24455,24457,24459,24461,24464,24466,24468,24470,24472,24474,24476,24478,24480,24482,24484,24487,24489,24491,24493,24495,24497,24499,24501,24503,24505,24507,24509,24512,24514,24516,24518,24520,24522,24524,24526,24528,24530,24532,24534,24537,24539,24541,24543,24545,24547,24549,24551,24553,24555,24557,24559,24562,24564,24566,24568,24570,24572,24574,24576,24578,24580,24582,24584,24586,24589,24591,24593,24595,24597,24599,24601,24603,24605,24607,24609,24611,24613,24616,24618,24620,24622,24624,24626,24628,24630,24632,24634,24636,24638,24640,24642,24645,24647,24649,24651,24653,24655,24657,24659,24661,24663,24665,24667,24669,24671,24673,24676,24678,24680,24682,24684,24686,24688,24690,24692,24694,24696,24698,24700,24702,24704,24707,24709,24711,24713,24715,24717,24719,24721,24723,24725,24727,24729,24731,24733,24735,24737,24740,24742,24744,24746,24748,24750,24752,24754,24756,24758,24760,24762,24764,24766,24768,24770,24772,24774,24777,24779,24781,24783,24785,24787,24789,24791,24793,24795,24797,24799,24801,24803,24805,24807,24809,24811,24814,24816,24818,24820,24822,24824,24826,24828,24830,24832,24834,24836,24838,24840,24842,24844,24846,24848,24850,24852,24855,24857,24859,24861,24863,24865,24867,24869,24871,24873,24875,24877,24879,24881,24883,24885,24887,24889,24891,24893,24895,24897,24899,24902,24904,24906,24908,24910,24912,24914,24916,24918,24920,24922,24924,24926,24928,24930,24932,24934,24936,24938,24940,24942,24944,24946,24948,24950,24953,24955,24957,24959,24961,24963,24965,24967,24969,24971,24973,24975,24977,24979,24981,24983,24985,24987,24989,24991,24993,24995,24997,24999,25001,25003,25005,25007,25009,25011,25013,25016,25018,25020,25022,25024,25026,25028,25030,25032,25034,25036,25038,25040,25042,25044,25046,25048,25050,25052,25054,25056,25058,25060,25062,25064,25066,25068,25070,25072,25074,25076,25078,25080,25082,25084,25086,25088,25090,25092,25094,25096,25099,25101,25103,25105,25107,25109,25111,25113,25115,25117,25119,25121,25123,25125,25127,25129,25131,25133,25135,25137,25139,25141,25143,25145,25147,25149,25151,25153,25155,25157,25159,25161,25163,25165,25167,25169,25171,25173,25175,25177,25179,25181,25183,25185,25187,25189,25191,25193,25195,25197,25199,25201,25203,25205,25207,25209,25211,25213,25215,25217,25219,25221,25223,25225,25227,25229,25231,25233,25235,25237,25239,25241,25243,25245,25247,25249,25251,25253,25255,25257,25259,25261,25263,25265,25267,25269,25271,25273,25275,25277,25279,25281,25283,25285,25287,25289,25291,25293,25295,25297,25299,25301,25303,25305,25307,25309,25311,25313,25315,25317,25319,25321,25323,25325,25327,25329,25331,25333,25335,25337,25339,25341,25343,25345,25347,25349,25351,25353,25355,25357,25359,25361,25363,25365,25367,25369,25371,25373,25375,25377,25379,25381,25383,25385,25387,25389,25391,25393,25395,25397,25399,25401,25403,25405,25407,25409,25411,25413,25415,25417,25419,25421,25423,25425,25427,25429,25431,25433,25435,25437,25438,25440,25442,25444,25446,25448,25450,25452,25454,25456,25458,25460,25462,25464,25466,25468,25470,25472,25474,25476,25478,25480,25482,25484,25486,25488,25490,25492,25494,25496,25498,25500,25502,25504,25506,25508,25510,25512,25514,25516,25518,25519,25521,25523,25525,25527,25529,25531,25533,25535,25537,25539,25541,25543,25545,25547,25549,25551,25553,25555,25557,25559,25561,25563,25565,25567,25569,25571,25573,25575,25577,25578,25580,25582,25584,25586,25588,25590,25592,25594,25596,25598,25600,25602,25604,25606,25608,25610,25612,25614,25616,25618,25620,25622,25624,25626,25628,25629,25631,25633,25635,25637,25639,25641,25643,25645,25647,25649,25651,25653,25655,25657,25659,25661,25663,25665,25667,25669,25671,25672,25674,25676,25678,25680,25682,25684,25686,25688,25690,25692,25694,25696,25698,25700,25702,25704,25706,25708,25710,25711,25713,25715,25717,25719,25721,25723,25725,25727,25729,25731,25733,25735,25737,25739,25741,25743,25745,25746,25748,25750,25752,25754,25756,25758,25760,25762,25764,25766,25768,25770,25772,25774,25776,25778,25779,25781,25783,25785,25787,25789,25791,25793,25795,25797,25799,25801,25803,25805,25807,25809,25810,25812,25814,25816,25818,25820,25822,25824,25826,25828,25830,25832,25834,25836,25838,25839,25841,25843,25845,25847,25849,25851,25853,25855,25857,25859,25861,25863,25865,25866,25868,25870,25872,25874,25876,25878,25880,25882,25884,25886,25888,25890,25892,25893,25895,25897,25899,25901,25903,25905,25907,25909,25911,25913,25915,25917,25918,25920,25922,25924,25926,25928,25930,25932,25934,25936,25938,25940,25942,25943,25945,25947,25949,25951,25953,25955,25957,25959,25961,25963,25965,25966,25968,25970,25972,25974,25976,25978,25980,25982,25984,25986,25988,25989,25991,25993,25995,25997,25999,26001,26003,26005,26007,26009,26010,26012,26014,26016,26018,26020,26022,26024,26026,26028,26030,26031,26033,26035,26037,26039,26041,26043,26045,26047,26049,26051,26052,26054,26056,26058,26060,26062,26064,26066,26068,26070,26071,26073,26075,26077,26079,26081,26083,26085,26087,26089,26090,26092,26094,26096,26098,26100,26102,26104,26106,26108,26109,26111,26113,26115,26117,26119,26121,26123,26125,26127,26128,26130,26132,26134,26136,26138,26140,26142,26144,26146,26147,26149,26151,26153,26155,26157,26159,26161,26163,26164,26166,26168,26170,26172,26174,26176,26178,26180,26181,26183,26185,26187,26189,26191,26193,26195,26197,26198,26200,26202,26204,26206,26208,26210,26212,26214,26215,26217,26219,26221,26223,26225,26227,26229,26230,26232,26234,26236,26238,26240,26242,26244,26246,26247,26249,26251,26253,26255,26257,26259,26261,26262,26264,26266,26268,26270,26272,26274,26276,26277,26279,26281,26283,26285,26287,26289,26291,26292,26294,26296,26298,26300,26302,26304,26306,26307,26309,26311,26313,26315,26317,26319,26321,26322,26324,26326,26328,26330,26332,26334,26336,26337,26339,26341,26343,26345,26347,26349,26350,26352,26354,26356,26358,26360,26362,26364,26365,26367,26369,26371,26373,26375,26377,26378,26380,26382,26384,26386,26388,26390,26392,26393,26395,26397,26399,26401,26403,26405,26406,26408,26410,26412,26414,26416,26418,26419,26421,26423,26425,26427,26429,26431,26432,26434,26436,26438,26440,26442,26444,26445,26447,26449,26451,26453,26455,26457,26458,26460,26462,26464,26466,26468,26469,26471,26473,26475,26477,26479,26481,26482,26484,26486,26488,26490,26492,26494,26495,26497,26499,26501,26503,26505,26506,26508,26510,26512,26514,26516,26518,26519,26521,26523,26525,26527,26529,26530,26532,26534,26536,26538,26540,26542,26543,26545,26547,26549,26551,26553,26554,26556,26558,26560,26562,26564,26565,26567,26569,26571,26573,26575,26576,26578,26580,26582,26584,26586,26588,26589,26591,26593,26595,26597,26599,26600,26602,26604,26606,26608,26610,26611,26613,26615,26617,26619,26621,26622,26624,26626,26628,26630,26631,26633,26635,26637,26639,26641,26642,26644,26646,26648,26650,26652,26653,26655,26657,26659,26661,26663,26664,26666,26668,26670,26672,26674,26675,26677,26679,26681,26683,26684,26686,26688,26690,26692,26694,26695,26697,26699,26701,26703,26705,26706,26708,26710,26712,26714,26715,26717,26719,26721,26723,26725,26726,26728,26730,26732,26734,26735,26737,26739,26741,26743,26745,26746,26748,26750,26752,26754,26755,26757,26759,26761,26763,26764,26766,26768,26770,26772,26774,26775,26777,26779,26781,26783,26784,26786,26788,26790,26792,26793,26795,26797,26799,26801,26802,26804,26806,26808,26810,26811,26813,26815,26817,26819,26821,26822,26824,26826,26828,26830,26831,26833,26835,26837,26839,26840,26842,26844,26846,26848,26849,26851,26853,26855,26857,26858,26860,26862,26864,26866,26867,26869,26871,26873,26875,26876,26878,26880,26882,26884,26885,26887,26889,26891,26893,26894,26896,26898,26900,26901,26903,26905,26907,26909,26910,26912,26914,26916,26918,26919,26921,26923,26925,26927,26928,26930,26932,26934,26936,26937,26939,26941,26943,26944,26946,26948,26950,26952,26953,26955,26957,26959,26961,26962,26964,26966,26968,26969,26971,26973,26975,26977,26978,26980,26982,26984,26986,26987,26989,26991,26993,26994,26996,26998,27000,27002,27003,27005,27007,27009,27010,27012,27014,27016,27018,27019,27021,27023,27025,27026,27028,27030,27032,27034,27035,27037,27039,27041,27042,27044,27046,27048,27049,27051,27053,27055,27057,27058,27060,27062,27064,27065,27067,27069,27071,27073,27074,27076,27078,27080,27081,27083,27085,27087,27088,27090,27092,27094,27096,27097,27099,27101,27103,27104,27106,27108,27110,27111,27113,27115,27117,27118,27120,27122,27124,27126,27127,27129,27131,27133,27134,27136,27138,27140,27141,27143,27145,27147,27148,27150,27152,27154,27155,27157,27159,27161,27162,27164,27166,27168,27169,27171,27173,27175,27177,27178,27180,27182,27184,27185,27187,27189,27191,27192,27194,27196,27198,27199,27201,27203,27205,27206,27208,27210,27212,27213,27215,27217,27219,27220,27222,27224,27226,27227,27229,27231,27233,27234,27236,27238,27240,27241,27243,27245,27247,27248,27250,27252,27253,27255,27257,27259,27260,27262,27264,27266,27267,27269,27271,27273,27274,27276,27278,27280,27281,27283,27285,27287,27288,27290,27292,27294,27295,27297,27299,27300,27302,27304,27306,27307,27309,27311,27313,27314,27316,27318,27320,27321,27323,27325,27327,27328,27330,27332,27333,27335,27337,27339,27340,27342,27344,27346,27347,27349,27351,27352,27354,27356,27358,27359,27361,27363,27365,27366,27368,27370,27372,27373,27375,27377,27378,27380,27382,27384,27385,27387,27389,27390,27392,27394,27396,27397,27399,27401,27403,27404,27406,27408,27409,27411,27413,27415,27416,27418,27420,27421,27423,27425,27427,27428,27430,27432,27434,27435,27437,27439,27440,27442,27444,27446,27447,27449,27451,27452,27454,27456,27458,27459,27461,27463,27464,27466,27468,27470,27471,27473,27475,27476,27478,27480,27482,27483,27485,27487,27488,27490,27492,27493,27495,27497,27499,27500,27502,27504,27505,27507,27509,27511,27512,27514,27516,27517,27519,27521,27523,27524,27526,27528,27529,27531,27533,27534,27536,27538,27540,27541,27543,27545,27546,27548,27550,27551,27553,27555,27557,27558,27560,27562,27563,27565,27567,27568,27570,27572,27574,27575,27577,27579,27580,27582,27584,27585,27587,27589,27590,27592,27594,27596,27597,27599,27601,27602,27604,27606,27607,27609,27611,27613,27614,27616,27618,27619,27621,27623,27624,27626,27628,27629,27631,27633,27634,27636,27638,27640,27641,27643,27645,27646,27648,27650,27651,27653,27655,27656,27658,27660,27661,27663,27665,27666,27668,27670,27672,27673,27675,27677,27678,27680,27682,27683,27685,27687,27688,27690,27692,27693,27695,27697,27698,27700,27702,27703,27705,27707,27708,27710,27712,27714,27715,27717,27719,27720,27722,27724,27725,27727,27729,27730,27732,27734,27735,27737,27739,27740,27742,27744,27745,27747,27749,27750,27752,27754,27755,27757,27759,27760,27762,27764,27765,27767,27769,27770,27772,27774,27775,27777,27779,27780,27782,27784,27785,27787,27789,27790,27792,27794,27795,27797,27799,27800,27802,27804,27805,27807,27809,27810,27812,27814,27815,27817,27819,27820,27822,27824,27825,27827,27829,27830,27832,27834,27835,27837,27839,27840,27842,27843,27845,27847,27848,27850,27852,27853,27855,27857,27858,27860,27862,27863,27865,27867,27868,27870,27872,27873,27875,27877,27878,27880,27882,27883,27885,27886,27888,27890,27891,27893,27895,27896,27898,27900,27901,27903,27905,27906,27908,27910,27911,27913,27914,27916,27918,27919,27921,27923,27924,27926,27928,27929,27931,27933,27934,27936,27937,27939,27941,27942,27944,27946,27947,27949,27951,27952,27954,27956,27957,27959,27960,27962,27964,27965,27967,27969,27970,27972,27974,27975,27977,27978,27980,27982,27983,27985,27987,27988,27990,27992,27993,27995,27996,27998,28000,28001,28003,28005,28006,28008,28009,28011,28013,28014,28016,28018,28019,28021,28022,28024,28026,28027,28029,28031,28032,28034,28036,28037,28039,28040,28042,28044,28045,28047,28049,28050,28052,28053,28055,28057,28058,28060,28061,28063,28065,28066,28068,28070,28071,28073,28074,28076,28078,28079,28081,28083,28084,28086,28087,28089,28091,28092,28094,28095,28097,28099,28100,28102,28104,28105,28107,28108,28110,28112,28113,28115,28116,28118,28120,28121,28123,28125,28126,28128,28129,28131,28133,28134,28136,28137,28139,28141,28142,28144,28145,28147,28149,28150,28152,28154,28155,28157,28158,28160,28162,28163,28165,28166,28168,28170,28171,28173,28174,28176,28178,28179,28181,28182,28184,28186,28187,28189,28190,28192,28194,28195,28197,28198,28200,28202,28203,28205,28206,28208,28210,28211,28213,28214,28216,28218,28219,28221,28222,28224,28226,28227,28229,28230,28232,28234,28235,28237,28238,28240,28242,28243,28245,28246,28248,28249,28251,28253,28254,28256,28257,28259,28261,28262,28264,28265,28267,28269,28270,28272,28273,28275,28277,28278,28280,28281,28283,28284,28286,28288,28289,28291,28292,28294,28296,28297,28299,28300,28302,28303,28305,28307,28308,28310,28311,28313,28315,28316,28318,28319,28321,28322,28324,28326,28327,28329,28330,28332,28333,28335,28337,28338,28340,28341,28343,28345,28346,28348,28349,28351,28352,28354,28356,28357,28359,28360,28362,28363,28365,28367,28368,28370,28371,28373,28374,28376,28378,28379,28381,28382,28384,28385,28387,28389,28390,28392,28393,28395,28396,28398,28400,28401,28403,28404,28406,28407,28409,28411,28412,28414,28415,28417,28418,28420,28421,28423,28425,28426,28428,28429,28431,28432,28434,28436,28437,28439,28440,28442,28443,28445,28446,28448,28450,28451,28453,28454,28456,28457,28459,28460,28462,28464,28465,28467,28468,28470,28471,28473,28474,28476,28478,28479,28481,28482,28484,28485,28487,28488,28490,28492,28493,28495,28496,28498,28499,28501,28502,28504,28505,28507,28509,28510,28512,28513,28515,28516,28518,28519,28521,28523,28524,28526,28527,28529,28530,28532,28533,28535,28536,28538,28540,28541,28543,28544,28546,28547,28549,28550,28552,28553,28555,28556,28558,28560,28561,28563,28564,28566,28567,28569,28570,28572,28573,28575,28576,28578,28580,28581,28583,28584,28586,28587,28589,28590,28592,28593,28595,28596,28598,28600,28601,28603,28604,28606,28607,28609,28610,28612,28613,28615,28616,28618,28619,28621,28622,28624,28626,28627,28629,28630,28632,28633,28635,28636,28638,28639,28641,28642,28644,28645,28647,28648,28650,28651,28653,28655,28656,28658,28659,28661,28662,28664,28665,28667,28668,28670,28671,28673,28674,28676,28677,28679,28680,28682,28683,28685,28686,28688,28690,28691,28693,28694,28696,28697,28699,28700,28702,28703,28705,28706,28708,28709,28711,28712,28714,28715,28717,28718,28720,28721,28723,28724,28726,28727,28729,28730,28732,28733,28735,28736,28738,28739,28741,28742,28744,28745,28747,28748,28750,28752,28753,28755,28756,28758,28759,28761,28762,28764,28765,28767,28768,28770,28771,28773,28774,28776,28777,28779,28780,28782,28783,28785,28786,28788,28789,28791,28792,28794,28795,28797,28798,28800,28801,28803,28804,28806,28807,28809,28810,28812,28813,28815,28816,28818,28819,28821,28822,28824,28825,28827,28828,28830,28831,28832,28834,28835,28837,28838,28840,28841,28843,28844,28846,28847,28849,28850,28852,28853,28855,28856,28858,28859,28861,28862,28864,28865,28867,28868,28870,28871,28873,28874,28876,28877,28879,28880,28882,28883,28885,28886,28888,28889,28891,28892,28893,28895,28896,28898,28899,28901,28902,28904,28905,28907,28908,28910,28911,28913,28914,28916,28917,28919,28920,28922,28923,28925,28926,28927,28929,28930,28932,28933,28935,28936,28938,28939,28941,28942,28944,28945,28947,28948,28950,28951,28953,28954,28955,28957,28958,28960,28961,28963,28964,28966,28967,28969,28970,28972,28973,28975,28976,28977,28979,28980,28982,28983,28985,28986,28988,28989,28991,28992,28994,28995,28997,28998,28999,29001,29002,29004,29005,29007,29008,29010,29011,29013,29014,29016,29017,29018,29020,29021,29023,29024,29026,29027,29029,29030,29032,29033,29034,29036,29037,29039,29040,29042,29043,29045,29046,29048,29049,29050,29052,29053,29055,29056,29058,29059,29061,29062,29064,29065,29066,29068,29069,29071,29072,29074,29075,29077,29078,29079,29081,29082,29084,29085,29087,29088,29090,29091,29093,29094,29095,29097,29098,29100,29101,29103,29104,29106,29107,29108,29110,29111,29113,29114,29116,29117,29118,29120,29121,29123,29124,29126,29127,29129,29130,29131,29133,29134,29136,29137,29139,29140,29142,29143,29144,29146,29147,29149,29150,29152,29153,29154,29156,29157,29159,29160,29162,29163,29164,29166,29167,29169,29170,29172,29173,29174,29176,29177,29179,29180,29182,29183,29184,29186,29187,29189,29190,29192,29193,29194,29196,29197,29199,29200,29202,29203,29204,29206,29207,29209,29210,29212,29213,29214,29216,29217,29219,29220,29222,29223,29224,29226,29227,29229,29230,29231,29233,29234,29236,29237,29239,29240,29241,29243,29244,29246,29247,29248,29250,29251,29253,29254,29256,29257,29258,29260,29261,29263,29264,29265,29267,29268,29270,29271,29273,29274,29275,29277,29278,29280,29281,29282,29284,29285,29287,29288,29289,29291,29292,29294,29295,29296,29298,29299,29301,29302,29304,29305,29306,29308,29309,29311,29312,29313,29315,29316,29318,29319,29320,29322,29323,29325,29326,29327,29329,29330,29332,29333,29334,29336,29337,29339,29340,29341,29343,29344,29346,29347,29348,29350,29351,29353,29354,29355,29357,29358,29360,29361,29362,29364,29365,29366,29368,29369,29371,29372,29373,29375,29376,29378,29379,29380,29382,29383,29385,29386,29387,29389,29390,29392,29393,29394,29396,29397,29398,29400,29401,29403,29404,29405,29407,29408,29410,29411,29412,29414,29415,29416,29418,29419,29421,29422,29423,29425,29426,29428,29429,29430,29432,29433,29434,29436,29437,29439,29440,29441,29443,29444,29445,29447,29448,29450,29451,29452,29454,29455,29457,29458,29459,29461,29462,29463,29465,29466,29468,29469,29470,29472,29473,29474,29476,29477,29478,29480,29481,29483,29484,29485,29487,29488,29489,29491,29492,29494,29495,29496,29498,29499,29500,29502,29503,29504,29506,29507,29509,29510,29511,29513,29514,29515,29517,29518,29520,29521,29522,29524,29525,29526,29528,29529,29530,29532,29533,29534,29536,29537,29539,29540,29541,29543,29544,29545,29547,29548,29549,29551,29552,29554,29555,29556,29558,29559,29560,29562,29563,29564,29566,29567,29568,29570,29571,29572,29574,29575,29577,29578,29579,29581,29582,29583,29585,29586,29587,29589,29590,29591,29593,29594,29595,29597,29598,29599,29601,29602,29604,29605,29606,29608,29609,29610,29612,29613,29614,29616,29617,29618,29620,29621,29622,29624,29625,29626,29628,29629,29630,29632,29633,29634,29636,29637,29638,29640,29641,29642,29644,29645,29646,29648,29649,29651,29652,29653,29655,29656,29657,29659,29660,29661,29663,29664,29665,29667,29668,29669,29671,29672,29673,29675,29676,29677,29679,29680,29681,29683,29684,29685,29687,29688,29689,29690,29692,29693,29694,29696,29697,29698,29700,29701,29702,29704,29705,29706,29708,29709,29710,29712,29713,29714,29716,29717,29718,29720,29721,29722,29724,29725,29726,29728,29729,29730,29732,29733,29734,29736,29737,29738,29739,29741,29742,29743,29745,29746,29747,29749,29750,29751,29753,29754,29755,29757,29758,29759,29761,29762,29763,29764,29766,29767,29768,29770,29771,29772,29774,29775,29776,29778,29779,29780,29782,29783,29784,29785,29787,29788,29789,29791,29792,29793,29795,29796,29797,29799,29800,29801,29802,29804,29805,29806,29808,29809,29810,29812,29813,29814,29816,29817,29818,29819,29821,29822,29823,29825,29826,29827,29829,29830,29831,29832,29834,29835,29836,29838,29839,29840,29842,29843,29844,29845,29847,29848,29849,29851,29852,29853,29854,29856,29857,29858,29860,29861,29862,29864,29865,29866,29867,29869,29870,29871,29873,29874,29875,29876,29878,29879,29880,29882,29883,29884,29885,29887,29888,29889,29891,29892,29893,29894,29896,29897,29898,29900,29901,29902,29903,29905,29906,29907,29909,29910,29911,29912,29914,29915,29916,29918,29919,29920,29921,29923,29924,29925,29927,29928,29929,29930,29932,29933,29934,29936,29937,29938,29939,29941,29942,29943,29944,29946,29947,29948,29950,29951,29952,29953,29955,29956,29957,29958,29960,29961,29962,29964,29965,29966,29967,29969,29970,29971,29972,29974,29975,29976,29978,29979,29980,29981,29983,29984,29985,29986,29988,29989,29990,29991,29993,29994,29995,29997,29998,29999,30000,30002,30003,30004,30005,30007,30008,30009,30010,30012,30013,30014,30015,30017,30018,30019,30020,30022,30023,30024,30026,30027,30028,30029,30031,30032,30033,30034,30036,30037,30038,30039,30041,30042,30043,30044,30046,30047,30048,30049,30051,30052,30053,30054,30056,30057,30058,30059,30061,30062,30063,30064,30066,30067,30068,30069,30071,30072,30073,30074,30076,30077,30078,30079,30081,30082,30083,30084,30086,30087,30088,30089,30091,30092,30093,30094,30096,30097,30098,30099,30100,30102,30103,30104,30105,30107,30108,30109,30110,30112,30113,30114,30115,30117,30118,30119,30120,30122,30123,30124,30125,30126,30128,30129,30130,30131,30133,30134,30135,30136,30138,30139,30140,30141,30143,30144,30145,30146,30147,30149,30150,30151,30152,30154,30155,30156,30157,30159,30160,30161,30162,30163,30165,30166,30167,30168,30170,30171,30172,30173,30174,30176,30177,30178,30179,30181,30182,30183,30184,30185,30187,30188,30189,30190,30192,30193,30194,30195,30196,30198,30199,30200,30201,30203,30204,30205,30206,30207,30209,30210,30211,30212,30214,30215,30216,30217,30218,30220,30221,30222,30223,30224,30226,30227,30228,30229,30231,30232,30233,30234,30235,30237,30238,30239,30240,30241,30243,30244,30245,30246,30247,30249,30250,30251,30252,30253,30255,30256,30257,30258,30260,30261,30262,30263,30264,30266,30267,30268,30269,30270,30272,30273,30274,30275,30276,30278,30279,30280,30281,30282,30284,30285,30286,30287,30288,30290,30291,30292,30293,30294,30296,30297,30298,30299,30300,30302,30303,30304,30305,30306,30308,30309,30310,30311,30312,30313,30315,30316,30317,30318,30319,30321,30322,30323,30324,30325,30327,30328,30329,30330,30331,30333,30334,30335,30336,30337,30338,30340,30341,30342,30343,30344,30346,30347,30348,30349,30350,30351,30353,30354,30355,30356,30357,30359,30360,30361,30362,30363,30365,30366,30367,30368,30369,30370,30372,30373,30374,30375,30376,30377,30379,30380,30381,30382,30383,30385,30386,30387,30388,30389,30390,30392,30393,30394,30395,30396,30397,30399,30400,30401,30402,30403,30404,30406,30407,30408,30409,30410,30412,30413,30414,30415,30416,30417,30419,30420,30421,30422,30423,30424,30426,30427,30428,30429,30430,30431,30433,30434,30435,30436,30437,30438,30439,30441,30442,30443,30444,30445,30446,30448,30449,30450,30451,30452,30453,30455,30456,30457,30458,30459,30460,30462,30463,30464,30465,30466,30467,30468,30470,30471,30472,30473,30474,30475,30477,30478,30479,30480,30481,30482,30483,30485,30486,30487,30488,30489,30490,30492,30493,30494,30495,30496,30497,30498,30500,30501,30502,30503,30504,30505,30506,30508,30509,30510,30511,30512,30513,30514,30516,30517,30518,30519,30520,30521,30522,30524,30525,30526,30527,30528,30529,30530,30532,30533,30534,30535,30536,30537,30538,30540,30541,30542,30543,30544,30545,30546,30548,30549,30550,30551,30552,30553,30554,30556,30557,30558,30559,30560,30561,30562,30563,30565,30566,30567,30568,30569,30570,30571,30573,30574,30575,30576,30577,30578,30579,30580,30582,30583,30584,30585,30586,30587,30588,30589,30591,30592,30593,30594,30595,30596,30597,30598,30600,30601,30602,30603,30604,30605,30606,30607,30609,30610,30611,30612,30613,30614,30615,30616,30617,30619,30620,30621,30622,30623,30624,30625,30626,30628,30629,30630,30631,30632,30633,30634,30635,30636,30638,30639,30640,30641,30642,30643,30644,30645,30646,30648,30649,30650,30651,30652,30653,30654,30655,30656,30658,30659,30660,30661,30662,30663,30664,30665,30666,30668,30669,30670,30671,30672,30673,30674,30675,30676,30678,30679,30680,30681,30682,30683,30684,30685,30686,30687,30689,30690,30691,30692,30693,30694,30695,30696,30697,30698,30700,30701,30702,30703,30704,30705,30706,30707,30708,30709,30711,30712,30713,30714,30715,30716,30717,30718,30719,30720,30721,30723,30724,30725,30726,30727,30728,30729,30730,30731,30732,30733,30735,30736,30737,30738,30739,30740,30741,30742,30743,30744,30745,30746,30748,30749,30750,30751,30752,30753,30754,30755,30756,30757,30758,30760,30761,30762,30763,30764,30765,30766,30767,30768,30769,30770,30771,30772,30774,30775,30776,30777,30778,30779,30780,30781,30782,30783,30784,30785,30786,30788,30789,30790,30791,30792,30793,30794,30795,30796,30797,30798,30799,30800,30802,30803,30804,30805,30806,30807,30808,30809,30810,30811,30812,30813,30814,30815,30816,30818,30819,30820,30821,30822,30823,30824,30825,30826,30827,30828,30829,30830,30831,30832,30834,30835,30836,30837,30838,30839,30840,30841,30842,30843,30844,30845,30846,30847,30848,30849,30851,30852,30853,30854,30855,30856,30857,30858,30859,30860,30861,30862,30863,30864,30865,30866,30867,30868,30870,30871,30872,30873,30874,30875,30876,30877,30878,30879,30880,30881,30882,30883,30884,30885,30886,30887,30888,30889,30891,30892,30893,30894,30895,30896,30897,30898,30899,30900,30901,30902,30903,30904,30905,30906,30907,30908,30909,30910,30911,30912,30914,30915,30916,30917,30918,30919,30920,30921,30922,30923,30924,30925,30926,30927,30928,30929,30930,30931,30932,30933,30934,30935,30936,30937,30938,30939,30941,30942,30943,30944,30945,30946,30947,30948,30949,30950,30951,30952,30953,30954,30955,30956,30957,30958,30959,30960,30961,30962,30963,30964,30965,30966,30967,30968,30969,30970,30971,30972,30973,30974,30976,30977,30978,30979,30980,30981,30982,30983,30984,30985,30986,30987,30988,30989,30990,30991,30992,30993,30994,30995,30996,30997,30998,30999,31000,31001,31002,31003,31004,31005,31006,31007,31008,31009,31010,31011,31012,31013,31014,31015,31016,31017,31018,31019,31020,31021,31022,31023,31024,31025,31026,31027,31028,31029,31030,31031,31032,31033,31034,31035,31036,31037,31038,31039,31040,31041,31043,31044,31045,31046,31047,31048,31049,31050,31051,31052,31053,31054,31055,31056,31057,31058,31059,31060,31061,31062,31063,31064,31065,31066,31067,31068,31069,31070,31071,31072,31073,31074,31075,31076,31077,31078,31079,31080,31081,31082,31083,31083,31084,31085,31086,31087,31088,31089,31090,31091,31092,31093,31094,31095,31096,31097,31098,31099,31100,31101,31102,31103,31104,31105,31106,31107,31108,31109,31110,31111,31112,31113,31114,31115,31116,31117,31118,31119,31120,31121,31122,31123,31124,31125,31126,31127,31128,31129,31130,31131,31132,31133,31134,31135,31136,31137,31138,31139,31140,31141,31142,31143,31144,31145,31146,31147,31148,31148,31149,31150,31151,31152,31153,31154,31155,31156,31157,31158,31159,31160,31161,31162,31163,31164,31165,31166,31167,31168,31169,31170,31171,31172,31173,31174,31175,31176,31177,31178,31179,31180,31181,31181,31182,31183,31184,31185,31186,31187,31188,31189,31190,31191,31192,31193,31194,31195,31196,31197,31198,31199,31200,31201,31202,31203,31204,31205,31206,31206,31207,31208,31209,31210,31211,31212,31213,31214,31215,31216,31217,31218,31219,31220,31221,31222,31223,31224,31225,31226,31227,31227,31228,31229,31230,31231,31232,31233,31234,31235,31236,31237,31238,31239,31240,31241,31242,31243,31244,31245,31246,31246,31247,31248,31249,31250,31251,31252,31253,31254,31255,31256,31257,31258,31259,31260,31261,31262,31262,31263,31264,31265,31266,31267,31268,31269,31270,31271,31272,31273,31274,31275,31276,31277,31278,31278,31279,31280,31281,31282,31283,31284,31285,31286,31287,31288,31289,31290,31291,31292,31292,31293,31294,31295,31296,31297,31298,31299,31300,31301,31302,31303,31304,31305,31305,31306,31307,31308,31309,31310,31311,31312,31313,31314,31315,31316,31317,31318,31318,31319,31320,31321,31322,31323,31324,31325,31326,31327,31328,31329,31329,31330,31331,31332,31333,31334,31335,31336,31337,31338,31339,31340,31341,31341,31342,31343,31344,31345,31346,31347,31348,31349,31350,31351,31352,31352,31353,31354,31355,31356,31357,31358,31359,31360,31361,31362,31362,31363,31364,31365,31366,31367,31368,31369,31370,31371,31372,31372,31373,31374,31375,31376,31377,31378,31379,31380,31381,31381,31382,31383,31384,31385,31386,31387,31388,31389,31390,31391,31391,31392,31393,31394,31395,31396,31397,31398,31399,31400,31400,31401,31402,31403,31404,31405,31406,31407,31408,31408,31409,31410,31411,31412,31413,31414,31415,31416,31417,31417,31418,31419,31420,31421,31422,31423,31424,31425,31425,31426,31427,31428,31429,31430,31431,31432,31433,31433,31434,31435,31436,31437,31438,31439,31440,31441,31441,31442,31443,31444,31445,31446,31447,31448,31448,31449,31450,31451,31452,31453,31454,31455,31456,31456,31457,31458,31459,31460,31461,31462,31463,31463,31464,31465,31466,31467,31468,31469,31470,31470,31471,31472,31473,31474,31475,31476,31477,31477,31478,31479,31480,31481,31482,31483,31484,31484,31485,31486,31487,31488,31489,31490,31490,31491,31492,31493,31494,31495,31496,31497,31497,31498,31499,31500,31501,31502,31503,31503,31504,31505,31506,31507,31508,31509,31510,31510,31511,31512,31513,31514,31515,31516,31516,31517,31518,31519,31520,31521,31522,31522,31523,31524,31525,31526,31527,31528,31528,31529,31530,31531,31532,31533,31534,31534,31535,31536,31537,31538,31539,31539,31540,31541,31542,31543,31544,31545,31545,31546,31547,31548,31549,31550,31551,31551,31552,31553,31554,31555,31556,31556,31557,31558,31559,31560,31561,31562,31562,31563,31564,31565,31566,31567,31567,31568,31569,31570,31571,31572,31572,31573,31574,31575,31576,31577,31578,31578,31579,31580,31581,31582,31583,31583,31584,31585,31586,31587,31588,31588,31589,31590,31591,31592,31593,31593,31594,31595,31596,31597,31598,31598,31599,31600,31601,31602,31603,31603,31604,31605,31606,31607,31608,31608,31609,31610,31611,31612,31613,31613,31614,31615,31616,31617,31617,31618,31619,31620,31621,31622,31622,31623,31624,31625,31626,31627,31627,31628,31629,31630,31631,31631,31632,31633,31634,31635,31636,31636,31637,31638,31639,31640,31640,31641,31642,31643,31644,31645,31645,31646,31647,31648,31649,31649,31650,31651,31652,31653,31653,31654,31655,31656,31657,31658,31658,31659,31660,31661,31662,31662,31663,31664,31665,31666,31666,31667,31668,31669,31670,31670,31671,31672,31673,31674,31674,31675,31676,31677,31678,31679,31679,31680,31681,31682,31683,31683,31684,31685,31686,31687,31687,31688,31689,31690,31691,31691,31692,31693,31694,31695,31695,31696,31697,31698,31698,31699,31700,31701,31702,31702,31703,31704,31705,31706,31706,31707,31708,31709,31710,31710,31711,31712,31713,31714,31714,31715,31716,31717,31718,31718,31719,31720,31721,31721,31722,31723,31724,31725,31725,31726,31727,31728,31729,31729,31730,31731,31732,31732,31733,31734,31735,31736,31736,31737,31738,31739,31739,31740,31741,31742,31743,31743,31744,31745,31746,31746,31747,31748,31749,31750,31750,31751,31752,31753,31753,31754,31755,31756,31757,31757,31758,31759,31760,31760,31761,31762,31763,31764,31764,31765,31766,31767,31767,31768,31769,31770,31770,31771,31772,31773,31774,31774,31775,31776,31777,31777,31778,31779,31780,31780,31781,31782,31783,31783,31784,31785,31786,31787,31787,31788,31789,31790,31790,31791,31792,31793,31793,31794,31795,31796,31796,31797,31798,31799,31799,31800,31801,31802,31802,31803,31804,31805,31806,31806,31807,31808,31809,31809,31810,31811,31812,31812,31813,31814,31815,31815,31816,31817,31818,31818,31819,31820,31821,31821,31822,31823,31824,31824,31825,31826,31827,31827,31828,31829,31830,31830,31831,31832,31833,31833,31834,31835,31836,31836,31837,31838,31838,31839,31840,31841,31841,31842,31843,31844,31844,31845,31846,31847,31847,31848,31849,31850,31850,31851,31852,31853,31853,31854,31855,31855,31856,31857,31858,31858,31859,31860,31861,31861,31862,31863,31864,31864,31865,31866,31866,31867,31868,31869,31869,31870,31871,31872,31872,31873,31874,31875,31875,31876,31877,31877,31878,31879,31880,31880,31881,31882,31882,31883,31884,31885,31885,31886,31887,31888,31888,31889,31890,31890,31891,31892,31893,31893,31894,31895,31896,31896,31897,31898,31898,31899,31900,31901,31901,31902,31903,31903,31904,31905,31906,31906,31907,31908,31908,31909,31910,31911,31911,31912,31913,31913,31914,31915,31916,31916,31917,31918,31918,31919,31920,31921,31921,31922,31923,31923,31924,31925,31925,31926,31927,31928,31928,31929,31930,31930,31931,31932,31933,31933,31934,31935,31935,31936,31937,31937,31938,31939,31940,31940,31941,31942,31942,31943,31944,31944,31945,31946,31947,31947,31948,31949,31949,31950,31951,31951,31952,31953,31954,31954,31955,31956,31956,31957,31958,31958,31959,31960,31960,31961,31962,31963,31963,31964,31965,31965,31966,31967,31967,31968,31969,31969,31970,31971,31972,31972,31973,31974,31974,31975,31976,31976,31977,31978,31978,31979,31980,31980,31981,31982,31982,31983,31984,31985,31985,31986,31987,31987,31988,31989,31989,31990,31991,31991,31992,31993,31993,31994,31995,31995,31996,31997,31997,31998,31999,31999,32000,32001,32002,32002,32003,32004,32004,32005,32006,32006,32007,32008,32008,32009,32010,32010,32011,32012,32012,32013,32014,32014,32015,32016,32016,32017,32018,32018,32019,32020,32020,32021,32022,32022,32023,32024,32024,32025,32026,32026,32027,32028,32028,32029,32030,32030,32031,32032,32032,32033,32034,32034,32035,32036,32036,32037,32038,32038,32039,32040,32040,32041,32041,32042,32043,32043,32044,32045,32045,32046,32047,32047,32048,32049,32049,32050,32051,32051,32052,32053,32053,32054,32055,32055,32056,32057,32057,32058,32058,32059,32060,32060,32061,32062,32062,32063,32064,32064,32065,32066,32066,32067,32068,32068,32069,32069,32070,32071,32071,32072,32073,32073,32074,32075,32075,32076,32077,32077,32078,32078,32079,32080,32080,32081,32082,32082,32083,32084,32084,32085,32086,32086,32087,32087,32088,32089,32089,32090,32091,32091,32092,32092,32093,32094,32094,32095,32096,32096,32097,32098,32098,32099,32099,32100,32101,32101,32102,32103,32103,32104,32104,32105,32106,32106,32107,32108,32108,32109,32110,32110,32111,32111,32112,32113,32113,32114,32115,32115,32116,32116,32117,32118,32118,32119,32119,32120,32121,32121,32122,32123,32123,32124,32124,32125,32126,32126,32127,32128,32128,32129,32129,32130,32131,32131,32132,32132,32133,32134,32134,32135,32136,32136,32137,32137,32138,32139,32139,32140,32140,32141,32142,32142,32143,32144,32144,32145,32145,32146,32147,32147,32148,32148,32149,32150,32150,32151,32151,32152,32153,32153,32154,32154,32155,32156,32156,32157,32157,32158,32159,32159,32160,32160,32161,32162,32162,32163,32163,32164,32165,32165,32166,32166,32167,32168,32168,32169,32169,32170,32171,32171,32172,32172,32173,32174,32174,32175,32175,32176,32177,32177,32178,32178,32179,32180,32180,32181,32181,32182,32183,32183,32184,32184,32185,32185,32186,32187,32187,32188,32188,32189,32190,32190,32191,32191,32192,32193,32193,32194,32194,32195,32195,32196,32197,32197,32198,32198,32199,32200,32200,32201,32201,32202,32202,32203,32204,32204,32205,32205,32206,32206,32207,32208,32208,32209,32209,32210,32211,32211,32212,32212,32213,32213,32214,32215,32215,32216,32216,32217,32217,32218,32219,32219,32220,32220,32221,32221,32222,32223,32223,32224,32224,32225,32225,32226,32227,32227,32228,32228,32229,32229,32230,32231,32231,32232,32232,32233,32233,32234,32234,32235,32236,32236,32237,32237,32238,32238,32239,32240,32240,32241,32241,32242,32242,32243,32243,32244,32245,32245,32246,32246,32247,32247,32248,32248,32249,32250,32250,32251,32251,32252,32252,32253,32253,32254,32255,32255,32256,32256,32257,32257,32258,32258,32259,32260,32260,32261,32261,32262,32262,32263,32263,32264,32265,32265,32266,32266,32267,32267,32268,32268,32269,32269,32270,32271,32271,32272,32272,32273,32273,32274,32274,32275,32275,32276,32277,32277,32278,32278,32279,32279,32280,32280,32281,32281,32282,32282,32283,32284,32284,32285,32285,32286,32286,32287,32287,32288,32288,32289,32289,32290,32290,32291,32292,32292,32293,32293,32294,32294,32295,32295,32296,32296,32297,32297,32298,32298,32299,32300,32300,32301,32301,32302,32302,32303,32303,32304,32304,32305,32305,32306,32306,32307,32307,32308,32308,32309,32310,32310,32311,32311,32312,32312,32313,32313,32314,32314,32315,32315,32316,32316,32317,32317,32318,32318,32319,32319,32320,32320,32321,32321,32322,32322,32323,32324,32324,32325,32325,32326,32326,32327,32327,32328,32328,32329,32329,32330,32330,32331,32331,32332,32332,32333,32333,32334,32334,32335,32335,32336,32336,32337,32337,32338,32338,32339,32339,32340,32340,32341,32341,32342,32342,32343,32343,32344,32344,32345,32345,32346,32346,32347,32347,32348,32348,32349,32349,32350,32350,32351,32351,32352,32352,32353,32353,32354,32354,32355,32355,32356,32356,32357,32357,32358,32358,32359,32359,32360,32360,32361,32361,32362,32362,32363,32363,32364,32364,32365,32365,32366,32366,32367,32367,32368,32368,32369,32369,32370,32370,32371,32371,32372,32372,32373,32373,32374,32374,32375,32375,32375,32376,32376,32377,32377,32378,32378,32379,32379,32380,32380,32381,32381,32382,32382,32383,32383,32384,32384,32385,32385,32386,32386,32387,32387,32387,32388,32388,32389,32389,32390,32390,32391,32391,32392,32392,32393,32393,32394,32394,32395,32395,32396,32396,32397,32397,32397,32398,32398,32399,32399,32400,32400,32401,32401,32402,32402,32403,32403,32404,32404,32404,32405,32405,32406,32406,32407,32407,32408,32408,32409,32409,32410,32410,32411,32411,32411,32412,32412,32413,32413,32414,32414,32415,32415,32416,32416,32416,32417,32417,32418,32418,32419,32419,32420,32420,32421,32421,32422,32422,32422,32423,32423,32424,32424,32425,32425,32426,32426,32426,32427,32427,32428,32428,32429,32429,32430,32430,32431,32431,32431,32432,32432,32433,32433,32434,32434,32435,32435,32435,32436,32436,32437,32437,32438,32438,32439,32439,32439,32440,32440,32441,32441,32442,32442,32443,32443,32443,32444,32444,32445,32445,32446,32446,32447,32447,32447,32448,32448,32449,32449,32450,32450,32450,32451,32451,32452,32452,32453,32453,32453,32454,32454,32455,32455,32456,32456,32457,32457,32457,32458,32458,32459,32459,32460,32460,32460,32461,32461,32462,32462,32463,32463,32463,32464,32464,32465,32465,32466,32466,32466,32467,32467,32468,32468,32468,32469,32469,32470,32470,32471,32471,32471,32472,32472,32473,32473,32474,32474,32474,32475,32475,32476,32476,32476,32477,32477,32478,32478,32479,32479,32479,32480,32480,32481,32481,32481,32482,32482,32483,32483,32484,32484,32484,32485,32485,32486,32486,32486,32487,32487,32488,32488,32488,32489,32489,32490,32490,32490,32491,32491,32492,32492,32493,32493,32493,32494,32494,32495,32495,32495,32496,32496,32497,32497,32497,32498,32498,32499,32499,32499,32500,32500,32501,32501,32501,32502,32502,32503,32503,32503,32504,32504,32505,32505,32505,32506,32506,32507,32507,32507,32508,32508,32509,32509,32509,32510,32510,32510,32511,32511,32512,32512,32512,32513,32513,32514,32514,32514,32515,32515,32516,32516,32516,32517,32517,32517,32518,32518,32519,32519,32519,32520,32520,32521,32521,32521,32522,32522,32522,32523,32523,32524,32524,32524,32525,32525,32526,32526,32526,32527,32527,32527,32528,32528,32529,32529,32529,32530,32530,32530,32531,32531,32532,32532,32532,32533,32533,32533,32534,32534,32535,32535,32535,32536,32536,32536,32537,32537,32538,32538,32538,32539,32539,32539,32540,32540,32541,32541,32541,32542,32542,32542,32543,32543,32543,32544,32544,32545,32545,32545,32546,32546,32546,32547,32547,32547,32548,32548,32549,32549,32549,32550,32550,32550,32551,32551,32551,32552,32552,32553,32553,32553,32554,32554,32554,32555,32555,32555,32556,32556,32556,32557,32557,32558,32558,32558,32559,32559,32559,32560,32560,32560,32561,32561,32561,32562,32562,32562,32563,32563,32564,32564,32564,32565,32565,32565,32566,32566,32566,32567,32567,32567,32568,32568,32568,32569,32569,32569,32570,32570,32570,32571,32571,32571,32572,32572,32573,32573,32573,32574,32574,32574,32575,32575,32575,32576,32576,32576,32577,32577,32577,32578,32578,32578,32579,32579,32579,32580,32580,32580,32581,32581,32581,32582,32582,32582,32583,32583,32583,32584,32584,32584,32585,32585,32585,32586,32586,32586,32587,32587,32587,32588,32588,32588,32589,32589,32589,32590,32590,32590,32591,32591,32591,32592,32592,32592,32592,32593,32593,32593,32594,32594,32594,32595,32595,32595,32596,32596,32596,32597,32597,32597,32598,32598,32598,32599,32599,32599,32600,32600,32600,32600,32601,32601,32601,32602,32602,32602,32603,32603,32603,32604,32604,32604,32605,32605,32605,32606,32606,32606,32606,32607,32607,32607,32608,32608,32608,32609,32609,32609,32610,32610,32610,32610,32611,32611,32611,32612,32612,32612,32613,32613,32613,32613,32614,32614,32614,32615,32615,32615,32616,32616,32616,32617,32617,32617,32617,32618,32618,32618,32619,32619,32619,32620,32620,32620,32620,32621,32621,32621,32622,32622,32622,32622,32623,32623,32623,32624,32624,32624,32625,32625,32625,32625,32626,32626,32626,32627,32627,32627,32627,32628,32628,32628,32629,32629,32629,32629,32630,32630,32630,32631,32631,32631,32631,32632,32632,32632,32633,32633,32633,32633,32634,32634,32634,32635,32635,32635,32635,32636,32636,32636,32637,32637,32637,32637,32638,32638,32638,32639,32639,32639,32639,32640,32640,32640,32640,32641,32641,32641,32642,32642,32642,32642,32643,32643,32643,32643,32644,32644,32644,32645,32645,32645,32645,32646,32646,32646,32646,32647,32647,32647,32648,32648,32648,32648,32649,32649,32649,32649,32650,32650,32650,32650,32651,32651,32651,32652,32652,32652,32652,32653,32653,32653,32653,32654,32654,32654,32654,32655,32655,32655,32655,32656,32656,32656,32656,32657,32657,32657,32657,32658,32658,32658,32659,32659,32659,32659,32660,32660,32660,32660,32661,32661,32661,32661,32662,32662,32662,32662,32663,32663,32663,32663,32664,32664,32664,32664,32665,32665,32665,32665,32666,32666,32666,32666,32667,32667,32667,32667,32668,32668,32668,32668,32668,32669,32669,32669,32669,32670,32670,32670,32670,32671,32671,32671,32671,32672,32672,32672,32672,32673,32673,32673,32673,32674,32674,32674,32674,32674,32675,32675,32675,32675,32676,32676,32676,32676,32677,32677,32677,32677,32678,32678,32678,32678,32678,32679,32679,32679,32679,32680,32680,32680,32680,32681,32681,32681,32681,32681,32682,32682,32682,32682,32683,32683,32683,32683,32683,32684,32684,32684,32684,32685,32685,32685,32685,32685,32686,32686,32686,32686,32687,32687,32687,32687,32687,32688,32688,32688,32688,32689,32689,32689,32689,32689,32690,32690,32690,32690,32690,32691,32691,32691,32691,32692,32692,32692,32692,32692,32693,32693,32693,32693,32693,32694,32694,32694,32694,32694,32695,32695,32695,32695,32696,32696,32696,32696,32696,32697,32697,32697,32697,32697,32698,32698,32698,32698,32698,32699,32699,32699,32699,32699,32700,32700,32700,32700,32700,32701,32701,32701,32701,32701,32702,32702,32702,32702,32702,32703,32703,32703,32703,32703,32704,32704,32704,32704,32704,32705,32705,32705,32705,32705,32706,32706,32706,32706,32706,32706,32707,32707,32707,32707,32707,32708,32708,32708,32708,32708,32709,32709,32709,32709,32709,32710,32710,32710,32710,32710,32710,32711,32711,32711,32711,32711,32712,32712,32712,32712,32712,32712,32713,32713,32713,32713,32713,32714,32714,32714,32714,32714,32714,32715,32715,32715,32715,32715,32715,32716,32716,32716,32716,32716,32717,32717,32717,32717,32717,32717,32718,32718,32718,32718,32718,32718,32719,32719,32719,32719,32719,32719,32720,32720,32720,32720,32720,32720,32721,32721,32721,32721,32721,32721,32722,32722,32722,32722,32722,32722,32723,32723,32723,32723,32723,32723,32724,32724,32724,32724,32724,32724,32725,32725,32725,32725,32725,32725,32726,32726,32726,32726,32726,32726,32726,32727,32727,32727,32727,32727,32727,32728,32728,32728,32728,32728,32728,32728,32729,32729,32729,32729,32729,32729,32730,32730,32730,32730,32730,32730,32730,32731,32731,32731,32731,32731,32731,32731,32732,32732,32732,32732,32732,32732,32732,32733,32733,32733,32733,32733,32733,32733,32734,32734,32734,32734,32734,32734,32734,32735,32735,32735,32735,32735,32735,32735,32736,32736,32736,32736,32736,32736,32736,32737,32737,32737,32737,32737,32737,32737,32737,32738,32738,32738,32738,32738,32738,32738,32739,32739,32739,32739,32739,32739,32739,32739,32740,32740,32740,32740,32740,32740,32740,32740,32741,32741,32741,32741,32741,32741,32741,32741,32742,32742,32742,32742,32742,32742,32742,32742,32743,32743,32743,32743,32743,32743,32743,32743,32744,32744,32744,32744,32744,32744,32744,32744,32744,32745,32745,32745,32745,32745,32745,32745,32745,32745,32746,32746,32746,32746,32746,32746,32746,32746,32746,32747,32747,32747,32747,32747,32747,32747,32747,32747,32748,32748,32748,32748,32748,32748,32748,32748,32748,32749,32749,32749,32749,32749,32749,32749,32749,32749,32749,32750,32750,32750,32750,32750,32750,32750,32750,32750,32751,32751,32751,32751,32751,32751,32751,32751,32751,32751,32751,32752,32752,32752,32752,32752,32752,32752,32752,32752,32752,32753,32753,32753,32753,32753,32753,32753,32753,32753,32753,32753,32754,32754,32754,32754,32754,32754,32754,32754,32754,32754,32754,32755,32755,32755,32755,32755,32755,32755,32755,32755,32755,32755,32755,32756,32756,32756,32756,32756,32756,32756,32756,32756,32756,32756,32756,32757,32757,32757,32757,32757,32757,32757,32757,32757,32757,32757,32757,32757,32758,32758,32758,32758,32758,32758,32758,32758,32758,32758,32758,32758,32758,32758,32759,32759,32759,32759,32759,32759,32759,32759,32759,32759,32759,32759,32759,32759,32760,32760,32760,32760,32760,32760,32760,32760,32760,32760,32760,32760,32760,32760,32760,32760,32761,32761,32761,32761,32761,32761,32761,32761,32761,32761,32761,32761,32761,32761,32761,32761,32762,32762,32762,32762,32762,32762,32762,32762,32762,32762,32762,32762,32762,32762,32762,32762,32762,32762,32762,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32767,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32766,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32765,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32764,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32763,32762,32762,32762,32762,32762,32762,32762,32762,32762,32762,32762,32762,32762,32762,32762,32762,32762,32762,32762,32761,32761,32761,32761,32761,32761,32761,32761,32761,32761,32761,32761,32761,32761,32761,32761,32760,32760,32760,32760,32760,32760,32760,32760,32760,32760,32760,32760,32760,32760,32760,32760,32759,32759,32759,32759,32759,32759,32759,32759,32759,32759,32759,32759,32759,32759,32758,32758,32758,32758,32758,32758,32758,32758,32758,32758,32758,32758,32758,32758,32757,32757,32757,32757,32757,32757,32757,32757,32757,32757,32757,32757,32757,32756,32756,32756,32756,32756,32756,32756,32756,32756,32756,32756,32756,32755,32755,32755,32755,32755,32755,32755,32755,32755,32755,32755,32755,32754,32754,32754,32754,32754,32754,32754,32754,32754,32754,32754,32753,32753,32753,32753,32753,32753,32753,32753,32753,32753,32753,32752,32752,32752,32752,32752,32752,32752,32752,32752,32752,32751,32751,32751,32751,32751,32751,32751,32751,32751,32751,32751,32750,32750,32750,32750,32750,32750,32750,32750,32750,32749,32749,32749,32749,32749,32749,32749,32749,32749,32749,32748,32748,32748,32748,32748,32748,32748,32748,32748,32747,32747,32747,32747,32747,32747,32747,32747,32747,32746,32746,32746,32746,32746,32746,32746,32746,32746,32745,32745,32745,32745,32745,32745,32745,32745,32745,32744,32744,32744,32744,32744,32744,32744,32744,32744,32743,32743,32743,32743,32743,32743,32743,32743,32742,32742,32742,32742,32742,32742,32742,32742,32741,32741,32741,32741,32741,32741,32741,32741,32740,32740,32740,32740,32740,32740,32740,32740,32739,32739,32739,32739,32739,32739,32739,32739,32738,32738,32738,32738,32738,32738,32738,32737,32737,32737,32737,32737,32737,32737,32737,32736,32736,32736,32736,32736,32736,32736,32735,32735,32735,32735,32735,32735,32735,32734,32734,32734,32734,32734,32734,32734,32733,32733,32733,32733,32733,32733,32733,32732,32732,32732,32732,32732,32732,32732,32731,32731,32731,32731,32731,32731,32731,32730,32730,32730,32730,32730,32730,32730,32729,32729,32729,32729,32729,32729,32728,32728,32728,32728,32728,32728,32728,32727,32727,32727,32727,32727,32727,32726,32726,32726,32726,32726,32726,32726,32725,32725,32725,32725,32725,32725,32724,32724,32724,32724,32724,32724,32723,32723,32723,32723,32723,32723,32722,32722,32722,32722,32722,32722,32721,32721,32721,32721,32721,32721,32720,32720,32720,32720,32720,32720,32719,32719,32719,32719,32719,32719,32718,32718,32718,32718,32718,32718,32717,32717,32717,32717,32717,32717,32716,32716,32716,32716,32716,32715,32715,32715,32715,32715,32715,32714,32714,32714,32714,32714,32714,32713,32713,32713,32713,32713,32712,32712,32712,32712,32712,32712,32711,32711,32711,32711,32711,32710,32710,32710,32710,32710,32710,32709,32709,32709,32709,32709,32708,32708,32708,32708,32708,32707,32707,32707,32707,32707,32706,32706,32706,32706,32706,32706,32705,32705,32705,32705,32705,32704,32704,32704,32704,32704,32703,32703,32703,32703,32703,32702,32702,32702,32702,32702,32701,32701,32701,32701,32701,32700,32700,32700,32700,32700,32699,32699,32699,32699,32699,32698,32698,32698,32698,32698,32697,32697,32697,32697,32697,32696,32696,32696,32696,32696,32695,32695,32695,32695,32694,32694,32694,32694,32694,32693,32693,32693,32693,32693,32692,32692,32692,32692,32692,32691,32691,32691,32691,32690,32690,32690,32690,32690,32689,32689,32689,32689,32689,32688,32688,32688,32688,32687,32687,32687,32687,32687,32686,32686,32686,32686,32685,32685,32685,32685,32685,32684,32684,32684,32684,32683,32683,32683,32683,32683,32682,32682,32682,32682,32681,32681,32681,32681,32681,32680,32680,32680,32680,32679,32679,32679,32679,32678,32678,32678,32678,32678,32677,32677,32677,32677,32676,32676,32676,32676,32675,32675,32675,32675,32674,32674,32674,32674,32674,32673,32673,32673,32673,32672,32672,32672,32672,32671,32671,32671,32671,32670,32670,32670,32670,32669,32669,32669,32669,32668,32668,32668,32668,32668,32667,32667,32667,32667,32666,32666,32666,32666,32665,32665,32665,32665,32664,32664,32664,32664,32663,32663,32663,32663,32662,32662,32662,32662,32661,32661,32661,32661,32660,32660,32660,32660,32659,32659,32659,32659,32658,32658,32658,32657,32657,32657,32657,32656,32656,32656,32656,32655,32655,32655,32655,32654,32654,32654,32654,32653,32653,32653,32653,32652,32652,32652,32652,32651,32651,32651,32650,32650,32650,32650,32649,32649,32649,32649,32648,32648,32648,32648,32647,32647,32647,32646,32646,32646,32646,32645,32645,32645,32645,32644,32644,32644,32643,32643,32643,32643,32642,32642,32642,32642,32641,32641,32641,32640,32640,32640,32640,32639,32639,32639,32639,32638,32638,32638,32637,32637,32637,32637,32636,32636,32636,32635,32635,32635,32635,32634,32634,32634,32633,32633,32633,32633,32632,32632,32632,32631,32631,32631,32631,32630,32630,32630,32629,32629,32629,32629,32628,32628,32628,32627,32627,32627,32627,32626,32626,32626,32625,32625,32625,32625,32624,32624,32624,32623,32623,32623,32622,32622,32622,32622,32621,32621,32621,32620,32620,32620,32620,32619,32619,32619,32618,32618,32618,32617,32617,32617,32617,32616,32616,32616,32615,32615,32615,32614,32614,32614,32613,32613,32613,32613,32612,32612,32612,32611,32611,32611,32610,32610,32610,32610,32609,32609,32609,32608,32608,32608,32607,32607,32607,32606,32606,32606,32606,32605,32605,32605,32604,32604,32604,32603,32603,32603,32602,32602,32602,32601,32601,32601,32600,32600,32600,32600,32599,32599,32599,32598,32598,32598,32597,32597,32597,32596,32596,32596,32595,32595,32595,32594,32594,32594,32593,32593,32593,32592,32592,32592,32592,32591,32591,32591,32590,32590,32590,32589,32589,32589,32588,32588,32588,32587,32587,32587,32586,32586,32586,32585,32585,32585,32584,32584,32584,32583,32583,32583,32582,32582,32582,32581,32581,32581,32580,32580,32580,32579,32579,32579,32578,32578,32578,32577,32577,32577,32576,32576,32576,32575,32575,32575,32574,32574,32574,32573,32573,32573,32572,32572,32571,32571,32571,32570,32570,32570,32569,32569,32569,32568,32568,32568,32567,32567,32567,32566,32566,32566,32565,32565,32565,32564,32564,32564,32563,32563,32562,32562,32562,32561,32561,32561,32560,32560,32560,32559,32559,32559,32558,32558,32558,32557,32557,32556,32556,32556,32555,32555,32555,32554,32554,32554,32553,32553,32553,32552,32552,32551,32551,32551,32550,32550,32550,32549,32549,32549,32548,32548,32547,32547,32547,32546,32546,32546,32545,32545,32545,32544,32544,32543,32543,32543,32542,32542,32542,32541,32541,32541,32540,32540,32539,32539,32539,32538,32538,32538,32537,32537,32536,32536,32536,32535,32535,32535,32534,32534,32533,32533,32533,32532,32532,32532,32531,32531,32530,32530,32530,32529,32529,32529,32528,32528,32527,32527,32527,32526,32526,32526,32525,32525,32524,32524,32524,32523,32523,32522,32522,32522,32521,32521,32521,32520,32520,32519,32519,32519,32518,32518,32517,32517,32517,32516,32516,32516,32515,32515,32514,32514,32514,32513,32513,32512,32512,32512,32511,32511,32510,32510,32510,32509,32509,32509,32508,32508,32507,32507,32507,32506,32506,32505,32505,32505,32504,32504,32503,32503,32503,32502,32502,32501,32501,32501,32500,32500,32499,32499,32499,32498,32498,32497,32497,32497,32496,32496,32495,32495,32495,32494,32494,32493,32493,32493,32492,32492,32491,32491,32490,32490,32490,32489,32489,32488,32488,32488,32487,32487,32486,32486,32486,32485,32485,32484,32484,32484,32483,32483,32482,32482,32481,32481,32481,32480,32480,32479,32479,32479,32478,32478,32477,32477,32476,32476,32476,32475,32475,32474,32474,32474,32473,32473,32472,32472,32471,32471,32471,32470,32470,32469,32469,32468,32468,32468,32467,32467,32466,32466,32466,32465,32465,32464,32464,32463,32463,32463,32462,32462,32461,32461,32460,32460,32460,32459,32459,32458,32458,32457,32457,32457,32456,32456,32455,32455,32454,32454,32453,32453,32453,32452,32452,32451,32451,32450,32450,32450,32449,32449,32448,32448,32447,32447,32447,32446,32446,32445,32445,32444,32444,32443,32443,32443,32442,32442,32441,32441,32440,32440,32439,32439,32439,32438,32438,32437,32437,32436,32436,32435,32435,32435,32434,32434,32433,32433,32432,32432,32431,32431,32431,32430,32430,32429,32429,32428,32428,32427,32427,32426,32426,32426,32425,32425,32424,32424,32423,32423,32422,32422,32422,32421,32421,32420,32420,32419,32419,32418,32418,32417,32417,32416,32416,32416,32415,32415,32414,32414,32413,32413,32412,32412,32411,32411,32411,32410,32410,32409,32409,32408,32408,32407,32407,32406,32406,32405,32405,32404,32404,32404,32403,32403,32402,32402,32401,32401,32400,32400,32399,32399,32398,32398,32397,32397,32397,32396,32396,32395,32395,32394,32394,32393,32393,32392,32392,32391,32391,32390,32390,32389,32389,32388,32388,32387,32387,32387,32386,32386,32385,32385,32384,32384,32383,32383,32382,32382,32381,32381,32380,32380,32379,32379,32378,32378,32377,32377,32376,32376,32375,32375,32375,32374,32374,32373,32373,32372,32372,32371,32371,32370,32370,32369,32369,32368,32368,32367,32367,32366,32366,32365,32365,32364,32364,32363,32363,32362,32362,32361,32361,32360,32360,32359,32359,32358,32358,32357,32357,32356,32356,32355,32355,32354,32354,32353,32353,32352,32352,32351,32351,32350,32350,32349,32349,32348,32348,32347,32347,32346,32346,32345,32345,32344,32344,32343,32343,32342,32342,32341,32341,32340,32340,32339,32339,32338,32338,32337,32337,32336,32336,32335,32335,32334,32334,32333,32333,32332,32332,32331,32331,32330,32330,32329,32329,32328,32328,32327,32327,32326,32326,32325,32325,32324,32324,32323,32322,32322,32321,32321,32320,32320,32319,32319,32318,32318,32317,32317,32316,32316,32315,32315,32314,32314,32313,32313,32312,32312,32311,32311,32310,32310,32309,32308,32308,32307,32307,32306,32306,32305,32305,32304,32304,32303,32303,32302,32302,32301,32301,32300,32300,32299,32298,32298,32297,32297,32296,32296,32295,32295,32294,32294,32293,32293,32292,32292,32291,32290,32290,32289,32289,32288,32288,32287,32287,32286,32286,32285,32285,32284,32284,32283,32282,32282,32281,32281,32280,32280,32279,32279,32278,32278,32277,32277,32276,32275,32275,32274,32274,32273,32273,32272,32272,32271,32271,32270,32269,32269,32268,32268,32267,32267,32266,32266,32265,32265,32264,32263,32263,32262,32262,32261,32261,32260,32260,32259,32258,32258,32257,32257,32256,32256,32255,32255,32254,32253,32253,32252,32252,32251,32251,32250,32250,32249,32248,32248,32247,32247,32246,32246,32245,32245,32244,32243,32243,32242,32242,32241,32241,32240,32240,32239,32238,32238,32237,32237,32236,32236,32235,32234,32234,32233,32233,32232,32232,32231,32231,32230,32229,32229,32228,32228,32227,32227,32226,32225,32225,32224,32224,32223,32223,32222,32221,32221,32220,32220,32219,32219,32218,32217,32217,32216,32216,32215,32215,32214,32213,32213,32212,32212,32211,32211,32210,32209,32209,32208,32208,32207,32206,32206,32205,32205,32204,32204,32203,32202,32202,32201,32201,32200,32200,32199,32198,32198,32197,32197,32196,32195,32195,32194,32194,32193,32193,32192,32191,32191,32190,32190,32189,32188,32188,32187,32187,32186,32185,32185,32184,32184,32183,32183,32182,32181,32181,32180,32180,32179,32178,32178,32177,32177,32176,32175,32175,32174,32174,32173,32172,32172,32171,32171,32170,32169,32169,32168,32168,32167,32166,32166,32165,32165,32164,32163,32163,32162,32162,32161,32160,32160,32159,32159,32158,32157,32157,32156,32156,32155,32154,32154,32153,32153,32152,32151,32151,32150,32150,32149,32148,32148,32147,32147,32146,32145,32145,32144,32144,32143,32142,32142,32141,32140,32140,32139,32139,32138,32137,32137,32136,32136,32135,32134,32134,32133,32132,32132,32131,32131,32130,32129,32129,32128,32128,32127,32126,32126,32125,32124,32124,32123,32123,32122,32121,32121,32120,32119,32119,32118,32118,32117,32116,32116,32115,32115,32114,32113,32113,32112,32111,32111,32110,32110,32109,32108,32108,32107,32106,32106,32105,32104,32104,32103,32103,32102,32101,32101,32100,32099,32099,32098,32098,32097,32096,32096,32095,32094,32094,32093,32092,32092,32091,32091,32090,32089,32089,32088,32087,32087,32086,32086,32085,32084,32084,32083,32082,32082,32081,32080,32080,32079,32078,32078,32077,32077,32076,32075,32075,32074,32073,32073,32072,32071,32071,32070,32069,32069,32068,32068,32067,32066,32066,32065,32064,32064,32063,32062,32062,32061,32060,32060,32059,32058,32058,32057,32057,32056,32055,32055,32054,32053,32053,32052,32051,32051,32050,32049,32049,32048,32047,32047,32046,32045,32045,32044,32043,32043,32042,32041,32041,32040,32040,32039,32038,32038,32037,32036,32036,32035,32034,32034,32033,32032,32032,32031,32030,32030,32029,32028,32028,32027,32026,32026,32025,32024,32024,32023,32022,32022,32021,32020,32020,32019,32018,32018,32017,32016,32016,32015,32014,32014,32013,32012,32012,32011,32010,32010,32009,32008,32008,32007,32006,32006,32005,32004,32004,32003,32002,32002,32001,32000,31999,31999,31998,31997,31997,31996,31995,31995,31994,31993,31993,31992,31991,31991,31990,31989,31989,31988,31987,31987,31986,31985,31985,31984,31983,31982,31982,31981,31980,31980,31979,31978,31978,31977,31976,31976,31975,31974,31974,31973,31972,31972,31971,31970,31969,31969,31968,31967,31967,31966,31965,31965,31964,31963,31963,31962,31961,31960,31960,31959,31958,31958,31957,31956,31956,31955,31954,31954,31953,31952,31951,31951,31950,31949,31949,31948,31947,31947,31946,31945,31944,31944,31943,31942,31942,31941,31940,31940,31939,31938,31937,31937,31936,31935,31935,31934,31933,31933,31932,31931,31930,31930,31929,31928,31928,31927,31926,31925,31925,31924,31923,31923,31922,31921,31921,31920,31919,31918,31918,31917,31916,31916,31915,31914,31913,31913,31912,31911,31911,31910,31909,31908,31908,31907,31906,31906,31905,31904,31903,31903,31902,31901,31901,31900,31899,31898,31898,31897,31896,31896,31895,31894,31893,31893,31892,31891,31890,31890,31889,31888,31888,31887,31886,31885,31885,31884,31883,31882,31882,31881,31880,31880,31879,31878,31877,31877,31876,31875,31875,31874,31873,31872,31872,31871,31870,31869,31869,31868,31867,31866,31866,31865,31864,31864,31863,31862,31861,31861,31860,31859,31858,31858,31857,31856,31855,31855,31854,31853,31853,31852,31851,31850,31850,31849,31848,31847,31847,31846,31845,31844,31844,31843,31842,31841,31841,31840,31839,31838,31838,31837,31836,31836,31835,31834,31833,31833,31832,31831,31830,31830,31829,31828,31827,31827,31826,31825,31824,31824,31823,31822,31821,31821,31820,31819,31818,31818,31817,31816,31815,31815,31814,31813,31812,31812,31811,31810,31809,31809,31808,31807,31806,31806,31805,31804,31803,31802,31802,31801,31800,31799,31799,31798,31797,31796,31796,31795,31794,31793,31793,31792,31791,31790,31790,31789,31788,31787,31787,31786,31785,31784,31783,31783,31782,31781,31780,31780,31779,31778,31777,31777,31776,31775,31774,31774,31773,31772,31771,31770,31770,31769,31768,31767,31767,31766,31765,31764,31764,31763,31762,31761,31760,31760,31759,31758,31757,31757,31756,31755,31754,31753,31753,31752,31751,31750,31750,31749,31748,31747,31746,31746,31745,31744,31743,31743,31742,31741,31740,31739,31739,31738,31737,31736,31736,31735,31734,31733,31732,31732,31731,31730,31729,31729,31728,31727,31726,31725,31725,31724,31723,31722,31721,31721,31720,31719,31718,31718,31717,31716,31715,31714,31714,31713,31712,31711,31710,31710,31709,31708,31707,31706,31706,31705,31704,31703,31702,31702,31701,31700,31699,31698,31698,31697,31696,31695,31695,31694,31693,31692,31691,31691,31690,31689,31688,31687,31687,31686,31685,31684,31683,31683,31682,31681,31680,31679,31679,31678,31677,31676,31675,31674,31674,31673,31672,31671,31670,31670,31669,31668,31667,31666,31666,31665,31664,31663,31662,31662,31661,31660,31659,31658,31658,31657,31656,31655,31654,31653,31653,31652,31651,31650,31649,31649,31648,31647,31646,31645,31645,31644,31643,31642,31641,31640,31640,31639,31638,31637,31636,31636,31635,31634,31633,31632,31631,31631,31630,31629,31628,31627,31627,31626,31625,31624,31623,31622,31622,31621,31620,31619,31618,31617,31617,31616,31615,31614,31613,31613,31612,31611,31610,31609,31608,31608,31607,31606,31605,31604,31603,31603,31602,31601,31600,31599,31598,31598,31597,31596,31595,31594,31593,31593,31592,31591,31590,31589,31588,31588,31587,31586,31585,31584,31583,31583,31582,31581,31580,31579,31578,31578,31577,31576,31575,31574,31573,31572,31572,31571,31570,31569,31568,31567,31567,31566,31565,31564,31563,31562,31562,31561,31560,31559,31558,31557,31556,31556,31555,31554,31553,31552,31551,31551,31550,31549,31548,31547,31546,31545,31545,31544,31543,31542,31541,31540,31539,31539,31538,31537,31536,31535,31534,31534,31533,31532,31531,31530,31529,31528,31528,31527,31526,31525,31524,31523,31522,31522,31521,31520,31519,31518,31517,31516,31516,31515,31514,31513,31512,31511,31510,31510,31509,31508,31507,31506,31505,31504,31503,31503,31502,31501,31500,31499,31498,31497,31497,31496,31495,31494,31493,31492,31491,31490,31490,31489,31488,31487,31486,31485,31484,31484,31483,31482,31481,31480,31479,31478,31477,31477,31476,31475,31474,31473,31472,31471,31470,31470,31469,31468,31467,31466,31465,31464,31463,31463,31462,31461,31460,31459,31458,31457,31456,31456,31455,31454,31453,31452,31451,31450,31449,31448,31448,31447,31446,31445,31444,31443,31442,31441,31441,31440,31439,31438,31437,31436,31435,31434,31433,31433,31432,31431,31430,31429,31428,31427,31426,31425,31425,31424,31423,31422,31421,31420,31419,31418,31417,31417,31416,31415,31414,31413,31412,31411,31410,31409,31408,31408,31407,31406,31405,31404,31403,31402,31401,31400,31400,31399,31398,31397,31396,31395,31394,31393,31392,31391,31391,31390,31389,31388,31387,31386,31385,31384,31383,31382,31381,31381,31380,31379,31378,31377,31376,31375,31374,31373,31372,31372,31371,31370,31369,31368,31367,31366,31365,31364,31363,31362,31362,31361,31360,31359,31358,31357,31356,31355,31354,31353,31352,31352,31351,31350,31349,31348,31347,31346,31345,31344,31343,31342,31341,31341,31340,31339,31338,31337,31336,31335,31334,31333,31332,31331,31330,31329,31329,31328,31327,31326,31325,31324,31323,31322,31321,31320,31319,31318,31318,31317,31316,31315,31314,31313,31312,31311,31310,31309,31308,31307,31306,31305,31305,31304,31303,31302,31301,31300,31299,31298,31297,31296,31295,31294,31293,31292,31292,31291,31290,31289,31288,31287,31286,31285,31284,31283,31282,31281,31280,31279,31278,31278,31277,31276,31275,31274,31273,31272,31271,31270,31269,31268,31267,31266,31265,31264,31263,31262,31262,31261,31260,31259,31258,31257,31256,31255,31254,31253,31252,31251,31250,31249,31248,31247,31246,31246,31245,31244,31243,31242,31241,31240,31239,31238,31237,31236,31235,31234,31233,31232,31231,31230,31229,31228,31227,31227,31226,31225,31224,31223,31222,31221,31220,31219,31218,31217,31216,31215,31214,31213,31212,31211,31210,31209,31208,31207,31206,31206,31205,31204,31203,31202,31201,31200,31199,31198,31197,31196,31195,31194,31193,31192,31191,31190,31189,31188,31187,31186,31185,31184,31183,31182,31181,31181,31180,31179,31178,31177,31176,31175,31174,31173,31172,31171,31170,31169,31168,31167,31166,31165,31164,31163,31162,31161,31160,31159,31158,31157,31156,31155,31154,31153,31152,31151,31150,31149,31148,31148,31147,31146,31145,31144,31143,31142,31141,31140,31139,31138,31137,31136,31135,31134,31133,31132,31131,31130,31129,31128,31127,31126,31125,31124,31123,31122,31121,31120,31119,31118,31117,31116,31115,31114,31113,31112,31111,31110,31109,31108,31107,31106,31105,31104,31103,31102,31101,31100,31099,31098,31097,31096,31095,31094,31093,31092,31091,31090,31089,31088,31087,31086,31085,31084,31083,31083,31082,31081,31080,31079,31078,31077,31076,31075,31074,31073,31072,31071,31070,31069,31068,31067,31066,31065,31064,31063,31062,31061,31060,31059,31058,31057,31056,31055,31054,31053,31052,31051,31050,31049,31048,31047,31046,31045,31044,31043,31041,31040,31039,31038,31037,31036,31035,31034,31033,31032,31031,31030,31029,31028,31027,31026,31025,31024,31023,31022,31021,31020,31019,31018,31017,31016,31015,31014,31013,31012,31011,31010,31009,31008,31007,31006,31005,31004,31003,31002,31001,31000,30999,30998,30997,30996,30995,30994,30993,30992,30991,30990,30989,30988,30987,30986,30985,30984,30983,30982,30981,30980,30979,30978,30977,30976,30974,30973,30972,30971,30970,30969,30968,30967,30966,30965,30964,30963,30962,30961,30960,30959,30958,30957,30956,30955,30954,30953,30952,30951,30950,30949,30948,30947,30946,30945,30944,30943,30942,30941,30939,30938,30937,30936,30935,30934,30933,30932,30931,30930,30929,30928,30927,30926,30925,30924,30923,30922,30921,30920,30919,30918,30917,30916,30915,30914,30912,30911,30910,30909,30908,30907,30906,30905,30904,30903,30902,30901,30900,30899,30898,30897,30896,30895,30894,30893,30892,30891,30889,30888,30887,30886,30885,30884,30883,30882,30881,30880,30879,30878,30877,30876,30875,30874,30873,30872,30871,30870,30868,30867,30866,30865,30864,30863,30862,30861,30860,30859,30858,30857,30856,30855,30854,30853,30852,30851,30849,30848,30847,30846,30845,30844,30843,30842,30841,30840,30839,30838,30837,30836,30835,30834,30832,30831,30830,30829,30828,30827,30826,30825,30824,30823,30822,30821,30820,30819,30818,30816,30815,30814,30813,30812,30811,30810,30809,30808,30807,30806,30805,30804,30803,30802,30800,30799,30798,30797,30796,30795,30794,30793,30792,30791,30790,30789,30788,30786,30785,30784,30783,30782,30781,30780,30779,30778,30777,30776,30775,30774,30772,30771,30770,30769,30768,30767,30766,30765,30764,30763,30762,30761,30760,30758,30757,30756,30755,30754,30753,30752,30751,30750,30749,30748,30746,30745,30744,30743,30742,30741,30740,30739,30738,30737,30736,30735,30733,30732,30731,30730,30729,30728,30727,30726,30725,30724,30723,30721,30720,30719,30718,30717,30716,30715,30714,30713,30712,30711,30709,30708,30707,30706,30705,30704,30703,30702,30701,30700,30698,30697,30696,30695,30694,30693,30692,30691,30690,30689,30687,30686,30685,30684,30683,30682,30681,30680,30679,30678,30676,30675,30674,30673,30672,30671,30670,30669,30668,30666,30665,30664,30663,30662,30661,30660,30659,30658,30656,30655,30654,30653,30652,30651,30650,30649,30648,30646,30645,30644,30643,30642,30641,30640,30639,30638,30636,30635,30634,30633,30632,30631,30630,30629,30628,30626,30625,30624,30623,30622,30621,30620,30619,30617,30616,30615,30614,30613,30612,30611,30610,30609,30607,30606,30605,30604,30603,30602,30601,30600,30598,30597,30596,30595,30594,30593,30592,30591,30589,30588,30587,30586,30585,30584,30583,30582,30580,30579,30578,30577,30576,30575,30574,30573,30571,30570,30569,30568,30567,30566,30565,30563,30562,30561,30560,30559,30558,30557,30556,30554,30553,30552,30551,30550,30549,30548,30546,30545,30544,30543,30542,30541,30540,30538,30537,30536,30535,30534,30533,30532,30530,30529,30528,30527,30526,30525,30524,30522,30521,30520,30519,30518,30517,30516,30514,30513,30512,30511,30510,30509,30508,30506,30505,30504,30503,30502,30501,30500,30498,30497,30496,30495,30494,30493,30492,30490,30489,30488,30487,30486,30485,30483,30482,30481,30480,30479,30478,30477,30475,30474,30473,30472,30471,30470,30468,30467,30466,30465,30464,30463,30462,30460,30459,30458,30457,30456,30455,30453,30452,30451,30450,30449,30448,30446,30445,30444,30443,30442,30441,30439,30438,30437,30436,30435,30434,30433,30431,30430,30429,30428,30427,30426,30424,30423,30422,30421,30420,30419,30417,30416,30415,30414,30413,30412,30410,30409,30408,30407,30406,30404,30403,30402,30401,30400,30399,30397,30396,30395,30394,30393,30392,30390,30389,30388,30387,30386,30385,30383,30382,30381,30380,30379,30377,30376,30375,30374,30373,30372,30370,30369,30368,30367,30366,30365,30363,30362,30361,30360,30359,30357,30356,30355,30354,30353,30351,30350,30349,30348,30347,30346,30344,30343,30342,30341,30340,30338,30337,30336,30335,30334,30333,30331,30330,30329,30328,30327,30325,30324,30323,30322,30321,30319,30318,30317,30316,30315,30313,30312,30311,30310,30309,30308,30306,30305,30304,30303,30302,30300,30299,30298,30297,30296,30294,30293,30292,30291,30290,30288,30287,30286,30285,30284,30282,30281,30280,30279,30278,30276,30275,30274,30273,30272,30270,30269,30268,30267,30266,30264,30263,30262,30261,30260,30258,30257,30256,30255,30253,30252,30251,30250,30249,30247,30246,30245,30244,30243,30241,30240,30239,30238,30237,30235,30234,30233,30232,30231,30229,30228,30227,30226,30224,30223,30222,30221,30220,30218,30217,30216,30215,30214,30212,30211,30210,30209,30207,30206,30205,30204,30203,30201,30200,30199,30198,30196,30195,30194,30193,30192,30190,30189,30188,30187,30185,30184,30183,30182,30181,30179,30178,30177,30176,30174,30173,30172,30171,30170,30168,30167,30166,30165,30163,30162,30161,30160,30159,30157,30156,30155,30154,30152,30151,30150,30149,30147,30146,30145,30144,30143,30141,30140,30139,30138,30136,30135,30134,30133,30131,30130,30129,30128,30126,30125,30124,30123,30122,30120,30119,30118,30117,30115,30114,30113,30112,30110,30109,30108,30107,30105,30104,30103,30102,30100,30099,30098,30097,30096,30094,30093,30092,30091,30089,30088,30087,30086,30084,30083,30082,30081,30079,30078,30077,30076,30074,30073,30072,30071,30069,30068,30067,30066,30064,30063,30062,30061,30059,30058,30057,30056,30054,30053,30052,30051,30049,30048,30047,30046,30044,30043,30042,30041,30039,30038,30037,30036,30034,30033,30032,30031,30029,30028,30027,30026,30024,30023,30022,30020,30019,30018,30017,30015,30014,30013,30012,30010,30009,30008,30007,30005,30004,30003,30002,30000,29999,29998,29997,29995,29994,29993,29991,29990,29989,29988,29986,29985,29984,29983,29981,29980,29979,29978,29976,29975,29974,29972,29971,29970,29969,29967,29966,29965,29964,29962,29961,29960,29958,29957,29956,29955,29953,29952,29951,29950,29948,29947,29946,29944,29943,29942,29941,29939,29938,29937,29936,29934,29933,29932,29930,29929,29928,29927,29925,29924,29923,29921,29920,29919,29918,29916,29915,29914,29912,29911,29910,29909,29907,29906,29905,29903,29902,29901,29900,29898,29897,29896,29894,29893,29892,29891,29889,29888,29887,29885,29884,29883,29882,29880,29879,29878,29876,29875,29874,29873,29871,29870,29869,29867,29866,29865,29864,29862,29861,29860,29858,29857,29856,29854,29853,29852,29851,29849,29848,29847,29845,29844,29843,29842,29840,29839,29838,29836,29835,29834,29832,29831,29830,29829,29827,29826,29825,29823,29822,29821,29819,29818,29817,29816,29814,29813,29812,29810,29809,29808,29806,29805,29804,29802,29801,29800,29799,29797,29796,29795,29793,29792,29791,29789,29788,29787,29785,29784,29783,29782,29780,29779,29778,29776,29775,29774,29772,29771,29770,29768,29767,29766,29764,29763,29762,29761,29759,29758,29757,29755,29754,29753,29751,29750,29749,29747,29746,29745,29743,29742,29741,29739,29738,29737,29736,29734,29733,29732,29730,29729,29728,29726,29725,29724,29722,29721,29720,29718,29717,29716,29714,29713,29712,29710,29709,29708,29706,29705,29704,29702,29701,29700,29698,29697,29696,29694,29693,29692,29690,29689,29688,29687,29685,29684,29683,29681,29680,29679,29677,29676,29675,29673,29672,29671,29669,29668,29667,29665,29664,29663,29661,29660,29659,29657,29656,29655,29653,29652,29651,29649,29648,29646,29645,29644,29642,29641,29640,29638,29637,29636,29634,29633,29632,29630,29629,29628,29626,29625,29624,29622,29621,29620,29618,29617,29616,29614,29613,29612,29610,29609,29608,29606,29605,29604,29602,29601,29599,29598,29597,29595,29594,29593,29591,29590,29589,29587,29586,29585,29583,29582,29581,29579,29578,29577,29575,29574,29572,29571,29570,29568,29567,29566,29564,29563,29562,29560,29559,29558,29556,29555,29554,29552,29551,29549,29548,29547,29545,29544,29543,29541,29540,29539,29537,29536,29534,29533,29532,29530,29529,29528,29526,29525,29524,29522,29521,29520,29518,29517,29515,29514,29513,29511,29510,29509,29507,29506,29504,29503,29502,29500,29499,29498,29496,29495,29494,29492,29491,29489,29488,29487,29485,29484,29483,29481,29480,29478,29477,29476,29474,29473,29472,29470,29469,29468,29466,29465,29463,29462,29461,29459,29458,29457,29455,29454,29452,29451,29450,29448,29447,29445,29444,29443,29441,29440,29439,29437,29436,29434,29433,29432,29430,29429,29428,29426,29425,29423,29422,29421,29419,29418,29416,29415,29414,29412,29411,29410,29408,29407,29405,29404,29403,29401,29400,29398,29397,29396,29394,29393,29392,29390,29389,29387,29386,29385,29383,29382,29380,29379,29378,29376,29375,29373,29372,29371,29369,29368,29366,29365,29364,29362,29361,29360,29358,29357,29355,29354,29353,29351,29350,29348,29347,29346,29344,29343,29341,29340,29339,29337,29336,29334,29333,29332,29330,29329,29327,29326,29325,29323,29322,29320,29319,29318,29316,29315,29313,29312,29311,29309,29308,29306,29305,29304,29302,29301,29299,29298,29296,29295,29294,29292,29291,29289,29288,29287,29285,29284,29282,29281,29280,29278,29277,29275,29274,29273,29271,29270,29268,29267,29265,29264,29263,29261,29260,29258,29257,29256,29254,29253,29251,29250,29248,29247,29246,29244,29243,29241,29240,29239,29237,29236,29234,29233,29231,29230,29229,29227,29226,29224,29223,29222,29220,29219,29217,29216,29214,29213,29212,29210,29209,29207,29206,29204,29203,29202,29200,29199,29197,29196,29194,29193,29192,29190,29189,29187,29186,29184,29183,29182,29180,29179,29177,29176,29174,29173,29172,29170,29169,29167,29166,29164,29163,29162,29160,29159,29157,29156,29154,29153,29152,29150,29149,29147,29146,29144,29143,29142,29140,29139,29137,29136,29134,29133,29131,29130,29129,29127,29126,29124,29123,29121,29120,29118,29117,29116,29114,29113,29111,29110,29108,29107,29106,29104,29103,29101,29100,29098,29097,29095,29094,29093,29091,29090,29088,29087,29085,29084,29082,29081,29079,29078,29077,29075,29074,29072,29071,29069,29068,29066,29065,29064,29062,29061,29059,29058,29056,29055,29053,29052,29050,29049,29048,29046,29045,29043,29042,29040,29039,29037,29036,29034,29033,29032,29030,29029,29027,29026,29024,29023,29021,29020,29018,29017,29016,29014,29013,29011,29010,29008,29007,29005,29004,29002,29001,28999,28998,28997,28995,28994,28992,28991,28989,28988,28986,28985,28983,28982,28980,28979,28977,28976,28975,28973,28972,28970,28969,28967,28966,28964,28963,28961,28960,28958,28957,28955,28954,28953,28951,28950,28948,28947,28945,28944,28942,28941,28939,28938,28936,28935,28933,28932,28930,28929,28927,28926,28925,28923,28922,28920,28919,28917,28916,28914,28913,28911,28910,28908,28907,28905,28904,28902,28901,28899,28898,28896,28895,28893,28892,28891,28889,28888,28886,28885,28883,28882,28880,28879,28877,28876,28874,28873,28871,28870,28868,28867,28865,28864,28862,28861,28859,28858,28856,28855,28853,28852,28850,28849,28847,28846,28844,28843,28841,28840,28838,28837,28835,28834,28832,28831,28830,28828,28827,28825,28824,28822,28821,28819,28818,28816,28815,28813,28812,28810,28809,28807,28806,28804,28803,28801,28800,28798,28797,28795,28794,28792,28791,28789,28788,28786,28785,28783,28782,28780,28779,28777,28776,28774,28773,28771,28770,28768,28767,28765,28764,28762,28761,28759,28758,28756,28755,28753,28752,28750,28748,28747,28745,28744,28742,28741,28739,28738,28736,28735,28733,28732,28730,28729,28727,28726,28724,28723,28721,28720,28718,28717,28715,28714,28712,28711,28709,28708,28706,28705,28703,28702,28700,28699,28697,28696,28694,28693,28691,28690,28688,28686,28685,28683,28682,28680,28679,28677,28676,28674,28673,28671,28670,28668,28667,28665,28664,28662,28661,28659,28658,28656,28655,28653,28651,28650,28648,28647,28645,28644,28642,28641,28639,28638,28636,28635,28633,28632,28630,28629,28627,28626,28624,28622,28621,28619,28618,28616,28615,28613,28612,28610,28609,28607,28606,28604,28603,28601,28600,28598,28596,28595,28593,28592,28590,28589,28587,28586,28584,28583,28581,28580,28578,28576,28575,28573,28572,28570,28569,28567,28566,28564,28563,28561,28560,28558,28556,28555,28553,28552,28550,28549,28547,28546,28544,28543,28541,28540,28538,28536,28535,28533,28532,28530,28529,28527,28526,28524,28523,28521,28519,28518,28516,28515,28513,28512,28510,28509,28507,28505,28504,28502,28501,28499,28498,28496,28495,28493,28492,28490,28488,28487,28485,28484,28482,28481,28479,28478,28476,28474,28473,28471,28470,28468,28467,28465,28464,28462,28460,28459,28457,28456,28454,28453,28451,28450,28448,28446,28445,28443,28442,28440,28439,28437,28436,28434,28432,28431,28429,28428,28426,28425,28423,28421,28420,28418,28417,28415,28414,28412,28411,28409,28407,28406,28404,28403,28401,28400,28398,28396,28395,28393,28392,28390,28389,28387,28385,28384,28382,28381,28379,28378,28376,28374,28373,28371,28370,28368,28367,28365,28363,28362,28360,28359,28357,28356,28354,28352,28351,28349,28348,28346,28345,28343,28341,28340,28338,28337,28335,28333,28332,28330,28329,28327,28326,28324,28322,28321,28319,28318,28316,28315,28313,28311,28310,28308,28307,28305,28303,28302,28300,28299,28297,28296,28294,28292,28291,28289,28288,28286,28284,28283,28281,28280,28278,28277,28275,28273,28272,28270,28269,28267,28265,28264,28262,28261,28259,28257,28256,28254,28253,28251,28249,28248,28246,28245,28243,28242,28240,28238,28237,28235,28234,28232,28230,28229,28227,28226,28224,28222,28221,28219,28218,28216,28214,28213,28211,28210,28208,28206,28205,28203,28202,28200,28198,28197,28195,28194,28192,28190,28189,28187,28186,28184,28182,28181,28179,28178,28176,28174,28173,28171,28170,28168,28166,28165,28163,28162,28160,28158,28157,28155,28154,28152,28150,28149,28147,28145,28144,28142,28141,28139,28137,28136,28134,28133,28131,28129,28128,28126,28125,28123,28121,28120,28118,28116,28115,28113,28112,28110,28108,28107,28105,28104,28102,28100,28099,28097,28095,28094,28092,28091,28089,28087,28086,28084,28083,28081,28079,28078,28076,28074,28073,28071,28070,28068,28066,28065,28063,28061,28060,28058,28057,28055,28053,28052,28050,28049,28047,28045,28044,28042,28040,28039,28037,28036,28034,28032,28031,28029,28027,28026,28024,28022,28021,28019,28018,28016,28014,28013,28011,28009,28008,28006,28005,28003,28001,28000,27998,27996,27995,27993,27992,27990,27988,27987,27985,27983,27982,27980,27978,27977,27975,27974,27972,27970,27969,27967,27965,27964,27962,27960,27959,27957,27956,27954,27952,27951,27949,27947,27946,27944,27942,27941,27939,27937,27936,27934,27933,27931,27929,27928,27926,27924,27923,27921,27919,27918,27916,27914,27913,27911,27910,27908,27906,27905,27903,27901,27900,27898,27896,27895,27893,27891,27890,27888,27886,27885,27883,27882,27880,27878,27877,27875,27873,27872,27870,27868,27867,27865,27863,27862,27860,27858,27857,27855,27853,27852,27850,27848,27847,27845,27843,27842,27840,27839,27837,27835,27834,27832,27830,27829,27827,27825,27824,27822,27820,27819,27817,27815,27814,27812,27810,27809,27807,27805,27804,27802,27800,27799,27797,27795,27794,27792,27790,27789,27787,27785,27784,27782,27780,27779,27777,27775,27774,27772,27770,27769,27767,27765,27764,27762,27760,27759,27757,27755,27754,27752,27750,27749,27747,27745,27744,27742,27740,27739,27737,27735,27734,27732,27730,27729,27727,27725,27724,27722,27720,27719,27717,27715,27714,27712,27710,27708,27707,27705,27703,27702,27700,27698,27697,27695,27693,27692,27690,27688,27687,27685,27683,27682,27680,27678,27677,27675,27673,27672,27670,27668,27666,27665,27663,27661,27660,27658,27656,27655,27653,27651,27650,27648,27646,27645,27643,27641,27640,27638,27636,27634,27633,27631,27629,27628,27626,27624,27623,27621,27619,27618,27616,27614,27613,27611,27609,27607,27606,27604,27602,27601,27599,27597,27596,27594,27592,27590,27589,27587,27585,27584,27582,27580,27579,27577,27575,27574,27572,27570,27568,27567,27565,27563,27562,27560,27558,27557,27555,27553,27551,27550,27548,27546,27545,27543,27541,27540,27538,27536,27534,27533,27531,27529,27528,27526,27524,27523,27521,27519,27517,27516,27514,27512,27511,27509,27507,27505,27504,27502,27500,27499,27497,27495,27493,27492,27490,27488,27487,27485,27483,27482,27480,27478,27476,27475,27473,27471,27470,27468,27466,27464,27463,27461,27459,27458,27456,27454,27452,27451,27449,27447,27446,27444,27442,27440,27439,27437,27435,27434,27432,27430,27428,27427,27425,27423,27421,27420,27418,27416,27415,27413,27411,27409,27408,27406,27404,27403,27401,27399,27397,27396,27394,27392,27390,27389,27387,27385,27384,27382,27380,27378,27377,27375,27373,27372,27370,27368,27366,27365,27363,27361,27359,27358,27356,27354,27352,27351,27349,27347,27346,27344,27342,27340,27339,27337,27335,27333,27332,27330,27328,27327,27325,27323,27321,27320,27318,27316,27314,27313,27311,27309,27307,27306,27304,27302,27300,27299,27297,27295,27294,27292,27290,27288,27287,27285,27283,27281,27280,27278,27276,27274,27273,27271,27269,27267,27266,27264,27262,27260,27259,27257,27255,27253,27252,27250,27248,27247,27245,27243,27241,27240,27238,27236,27234,27233,27231,27229,27227,27226,27224,27222,27220,27219,27217,27215,27213,27212,27210,27208,27206,27205,27203,27201,27199,27198,27196,27194,27192,27191,27189,27187,27185,27184,27182,27180,27178,27177,27175,27173,27171,27169,27168,27166,27164,27162,27161,27159,27157,27155,27154,27152,27150,27148,27147,27145,27143,27141,27140,27138,27136,27134,27133,27131,27129,27127,27126,27124,27122,27120,27118,27117,27115,27113,27111,27110,27108,27106,27104,27103,27101,27099,27097,27096,27094,27092,27090,27088,27087,27085,27083,27081,27080,27078,27076,27074,27073,27071,27069,27067,27065,27064,27062,27060,27058,27057,27055,27053,27051,27049,27048,27046,27044,27042,27041,27039,27037,27035,27034,27032,27030,27028,27026,27025,27023,27021,27019,27018,27016,27014,27012,27010,27009,27007,27005,27003,27002,27000,26998,26996,26994,26993,26991,26989,26987,26986,26984,26982,26980,26978,26977,26975,26973,26971,26969,26968,26966,26964,26962,26961,26959,26957,26955,26953,26952,26950,26948,26946,26944,26943,26941,26939,26937,26936,26934,26932,26930,26928,26927,26925,26923,26921,26919,26918,26916,26914,26912,26910,26909,26907,26905,26903,26901,26900,26898,26896,26894,26893,26891,26889,26887,26885,26884,26882,26880,26878,26876,26875,26873,26871,26869,26867,26866,26864,26862,26860,26858,26857,26855,26853,26851,26849,26848,26846,26844,26842,26840,26839,26837,26835,26833,26831,26830,26828,26826,26824,26822,26821,26819,26817,26815,26813,26811,26810,26808,26806,26804,26802,26801,26799,26797,26795,26793,26792,26790,26788,26786,26784,26783,26781,26779,26777,26775,26774,26772,26770,26768,26766,26764,26763,26761,26759,26757,26755,26754,26752,26750,26748,26746,26745,26743,26741,26739,26737,26735,26734,26732,26730,26728,26726,26725,26723,26721,26719,26717,26715,26714,26712,26710,26708,26706,26705,26703,26701,26699,26697,26695,26694,26692,26690,26688,26686,26684,26683,26681,26679,26677,26675,26674,26672,26670,26668,26666,26664,26663,26661,26659,26657,26655,26653,26652,26650,26648,26646,26644,26642,26641,26639,26637,26635,26633,26631,26630,26628,26626,26624,26622,26621,26619,26617,26615,26613,26611,26610,26608,26606,26604,26602,26600,26599,26597,26595,26593,26591,26589,26588,26586,26584,26582,26580,26578,26576,26575,26573,26571,26569,26567,26565,26564,26562,26560,26558,26556,26554,26553,26551,26549,26547,26545,26543,26542,26540,26538,26536,26534,26532,26530,26529,26527,26525,26523,26521,26519,26518,26516,26514,26512,26510,26508,26506,26505,26503,26501,26499,26497,26495,26494,26492,26490,26488,26486,26484,26482,26481,26479,26477,26475,26473,26471,26469,26468,26466,26464,26462,26460,26458,26457,26455,26453,26451,26449,26447,26445,26444,26442,26440,26438,26436,26434,26432,26431,26429,26427,26425,26423,26421,26419,26418,26416,26414,26412,26410,26408,26406,26405,26403,26401,26399,26397,26395,26393,26392,26390,26388,26386,26384,26382,26380,26378,26377,26375,26373,26371,26369,26367,26365,26364,26362,26360,26358,26356,26354,26352,26350,26349,26347,26345,26343,26341,26339,26337,26336,26334,26332,26330,26328,26326,26324,26322,26321,26319,26317,26315,26313,26311,26309,26307,26306,26304,26302,26300,26298,26296,26294,26292,26291,26289,26287,26285,26283,26281,26279,26277,26276,26274,26272,26270,26268,26266,26264,26262,26261,26259,26257,26255,26253,26251,26249,26247,26246,26244,26242,26240,26238,26236,26234,26232,26230,26229,26227,26225,26223,26221,26219,26217,26215,26214,26212,26210,26208,26206,26204,26202,26200,26198,26197,26195,26193,26191,26189,26187,26185,26183,26181,26180,26178,26176,26174,26172,26170,26168,26166,26164,26163,26161,26159,26157,26155,26153,26151,26149,26147,26146,26144,26142,26140,26138,26136,26134,26132,26130,26128,26127,26125,26123,26121,26119,26117,26115,26113,26111,26109,26108,26106,26104,26102,26100,26098,26096,26094,26092,26090,26089,26087,26085,26083,26081,26079,26077,26075,26073,26071,26070,26068,26066,26064,26062,26060,26058,26056,26054,26052,26051,26049,26047,26045,26043,26041,26039,26037,26035,26033,26031,26030,26028,26026,26024,26022,26020,26018,26016,26014,26012,26010,26009,26007,26005,26003,26001,25999,25997,25995,25993,25991,25989,25988,25986,25984,25982,25980,25978,25976,25974,25972,25970,25968,25966,25965,25963,25961,25959,25957,25955,25953,25951,25949,25947,25945,25943,25942,25940,25938,25936,25934,25932,25930,25928,25926,25924,25922,25920,25918,25917,25915,25913,25911,25909,25907,25905,25903,25901,25899,25897,25895,25893,25892,25890,25888,25886,25884,25882,25880,25878,25876,25874,25872,25870,25868,25866,25865,25863,25861,25859,25857,25855,25853,25851,25849,25847,25845,25843,25841,25839,25838,25836,25834,25832,25830,25828,25826,25824,25822,25820,25818,25816,25814,25812,25810,25809,25807,25805,25803,25801,25799,25797,25795,25793,25791,25789,25787,25785,25783,25781,25779,25778,25776,25774,25772,25770,25768,25766,25764,25762,25760,25758,25756,25754,25752,25750,25748,25746,25745,25743,25741,25739,25737,25735,25733,25731,25729,25727,25725,25723,25721,25719,25717,25715,25713,25711,25710,25708,25706,25704,25702,25700,25698,25696,25694,25692,25690,25688,25686,25684,25682,25680,25678,25676,25674,25672,25671,25669,25667,25665,25663,25661,25659,25657,25655,25653,25651,25649,25647,25645,25643,25641,25639,25637,25635,25633,25631,25629,25628,25626,25624,25622,25620,25618,25616,25614,25612,25610,25608,25606,25604,25602,25600,25598,25596,25594,25592,25590,25588,25586,25584,25582,25580,25578,25577,25575,25573,25571,25569,25567,25565,25563,25561,25559,25557,25555,25553,25551,25549,25547,25545,25543,25541,25539,25537,25535,25533,25531,25529,25527,25525,25523,25521,25519,25518,25516,25514,25512,25510,25508,25506,25504,25502,25500,25498,25496,25494,25492,25490,25488,25486,25484,25482,25480,25478,25476,25474,25472,25470,25468,25466,25464,25462,25460,25458,25456,25454,25452,25450,25448,25446,25444,25442,25440,25438,25437,25435,25433,25431,25429,25427,25425,25423,25421,25419,25417,25415,25413,25411,25409,25407,25405,25403,25401,25399,25397,25395,25393,25391,25389,25387,25385,25383,25381,25379,25377,25375,25373,25371,25369,25367,25365,25363,25361,25359,25357,25355,25353,25351,25349,25347,25345,25343,25341,25339,25337,25335,25333,25331,25329,25327,25325,25323,25321,25319,25317,25315,25313,25311,25309,25307,25305,25303,25301,25299,25297,25295,25293,25291,25289,25287,25285,25283,25281,25279,25277,25275,25273,25271,25269,25267,25265,25263,25261,25259,25257,25255,25253,25251,25249,25247,25245,25243,25241,25239,25237,25235,25233,25231,25229,25227,25225,25223,25221,25219,25217,25215,25213,25211,25209,25207,25205,25203,25201,25199,25197,25195,25193,25191,25189,25187,25185,25183,25181,25179,25177,25175,25173,25171,25169,25167,25165,25163,25161,25159,25157,25155,25153,25151,25149,25147,25145,25143,25141,25139,25137,25135,25133,25131,25129,25127,25125,25123,25121,25119,25117,25115,25113,25111,25109,25107,25105,25103,25101,25099,25096,25094,25092,25090,25088,25086,25084,25082,25080,25078,25076,25074,25072,25070,25068,25066,25064,25062,25060,25058,25056,25054,25052,25050,25048,25046,25044,25042,25040,25038,25036,25034,25032,25030,25028,25026,25024,25022,25020,25018,25016,25013,25011,25009,25007,25005,25003,25001,24999,24997,24995,24993,24991,24989,24987,24985,24983,24981,24979,24977,24975,24973,24971,24969,24967,24965,24963,24961,24959,24957,24955,24953,24950,24948,24946,24944,24942,24940,24938,24936,24934,24932,24930,24928,24926,24924,24922,24920,24918,24916,24914,24912,24910,24908,24906,24904,24902,24899,24897,24895,24893,24891,24889,24887,24885,24883,24881,24879,24877,24875,24873,24871,24869,24867,24865,24863,24861,24859,24857,24855,24852,24850,24848,24846,24844,24842,24840,24838,24836,24834,24832,24830,24828,24826,24824,24822,24820,24818,24816,24814,24811,24809,24807,24805,24803,24801,24799,24797,24795,24793,24791,24789,24787,24785,24783,24781,24779,24777,24774,24772,24770,24768,24766,24764,24762,24760,24758,24756,24754,24752,24750,24748,24746,24744,24742,24740,24737,24735,24733,24731,24729,24727,24725,24723,24721,24719,24717,24715,24713,24711,24709,24707,24704,24702,24700,24698,24696,24694,24692,24690,24688,24686,24684,24682,24680,24678,24676,24673,24671,24669,24667,24665,24663,24661,24659,24657,24655,24653,24651,24649,24647,24645,24642,24640,24638,24636,24634,24632,24630,24628,24626,24624,24622,24620,24618,24616,24613,24611,24609,24607,24605,24603,24601,24599,24597,24595,24593,24591,24589,24586,24584,24582,24580,24578,24576,24574,24572,24570,24568,24566,24564,24562,24559,24557,24555,24553,24551,24549,24547,24545,24543,24541,24539,24537,24534,24532,24530,24528,24526,24524,24522,24520,24518,24516,24514,24512,24509,24507,24505,24503,24501,24499,24497,24495,24493,24491,24489,24487,24484,24482,24480,24478,24476,24474,24472,24470,24468,24466,24464,24461,24459,24457,24455,24453,24451,24449,24447,24445,24443,24441,24438,24436,24434,24432,24430,24428,24426,24424,24422,24420,24417,24415,24413,24411,24409,24407,24405,24403,24401,24399,24397,24394,24392,24390,24388,24386,24384,24382,24380,24378,24376,24373,24371,24369,24367,24365,24363,24361,24359,24357,24355,24352,24350,24348,24346,24344,24342,24340,24338,24336,24334,24331,24329,24327,24325,24323,24321,24319,24317,24315,24312,24310,24308,24306,24304,24302,24300,24298,24296,24294,24291,24289,24287,24285,24283,24281,24279,24277,24275,24272,24270,24268,24266,24264,24262,24260,24258,24256,24253,24251,24249,24247,24245,24243,24241,24239,24237,24234,24232,24230,24228,24226,24224,24222,24220,24217,24215,24213,24211,24209,24207,24205,24203,24201,24198,24196,24194,24192,24190,24188,24186,24184,24181,24179,24177,24175,24173,24171,24169,24167,24164,24162,24160,24158,24156,24154,24152,24150,24148,24145,24143,24141,24139,24137,24135,24133,24131,24128,24126,24124,24122,24120,24118,24116,24114,24111,24109,24107,24105,24103,24101,24099,24096,24094,24092,24090,24088,24086,24084,24082,24079,24077,24075,24073,24071,24069,24067,24065,24062,24060,24058,24056,24054,24052,24050,24047,24045,24043,24041,24039,24037,24035,24033,24030,24028,24026,24024,24022,24020,24018,24015,24013,24011,24009,24007,24005,24003,24000,23998,23996,23994,23992,23990,23988,23985,23983,23981,23979,23977,23975,23973,23971,23968,23966,23964,23962,23960,23958,23956,23953,23951,23949,23947,23945,23943,23940,23938,23936,23934,23932,23930,23928,23925,23923,23921,23919,23917,23915,23913,23910,23908,23906,23904,23902,23900,23898,23895,23893,23891,23889,23887,23885,23883,23880,23878,23876,23874,23872,23870,23867,23865,23863,23861,23859,23857,23855,23852,23850,23848,23846,23844,23842,23839,23837,23835,23833,23831,23829,23827,23824,23822,23820,23818,23816,23814,23811,23809,23807,23805,23803,23801,23798,23796,23794,23792,23790,23788,23785,23783,23781,23779,23777,23775,23773,23770,23768,23766,23764,23762,23760,23757,23755,23753,23751,23749,23747,23744,23742,23740,23738,23736,23734,23731,23729,23727,23725,23723,23721,23718,23716,23714,23712,23710,23708,23705,23703,23701,23699,23697,23695,23692,23690,23688,23686,23684,23682,23679,23677,23675,23673,23671,23668,23666,23664,23662,23660,23658,23655,23653,23651,23649,23647,23645,23642,23640,23638,23636,23634,23632,23629,23627,23625,23623,23621,23618,23616,23614,23612,23610,23608,23605,23603,23601,23599,23597,23595,23592,23590,23588,23586,23584,23581,23579,23577,23575,23573,23571,23568,23566,23564,23562,23560,23557,23555,23553,23551,23549,23546,23544,23542,23540,23538,23536,23533,23531,23529,23527,23525,23522,23520,23518,23516,23514,23512,23509,23507,23505,23503,23501,23498,23496,23494,23492,23490,23487,23485,23483,23481,23479,23476,23474,23472,23470,23468,23466,23463,23461,23459,23457,23455,23452,23450,23448,23446,23444,23441,23439,23437,23435,23433,23430,23428,23426,23424,23422,23419,23417,23415,23413,23411,23408,23406,23404,23402,23400,23397,23395,23393,23391,23389,23386,23384,23382,23380,23378,23375,23373,23371,23369,23367,23364,23362,23360,23358,23356,23353,23351,23349,23347,23345,23342,23340,23338,23336,23334,23331,23329,23327,23325,23323,23320,23318,23316,23314,23311,23309,23307,23305,23303,23300,23298,23296,23294,23292,23289,23287,23285,23283,23281,23278,23276,23274,23272,23270,23267,23265,23263,23261,23258,23256,23254,23252,23250,23247,23245,23243,23241,23239,23236,23234,23232,23230,23227,23225,23223,23221,23219,23216,23214,23212,23210,23208,23205,23203,23201,23199,23196,23194,23192,23190,23188,23185,23183,23181,23179,23176,23174,23172,23170,23168,23165,23163,23161,23159,23156,23154,23152,23150,23148,23145,23143,23141,23139,23136,23134,23132,23130,23128,23125,23123,23121,23119,23116,23114,23112,23110,23107,23105,23103,23101,23099,23096,23094,23092,23090,23087,23085,23083,23081,23079,23076,23074,23072,23070,23067,23065,23063,23061,23058,23056,23054,23052,23050,23047,23045,23043,23041,23038,23036,23034,23032,23029,23027,23025,23023,23020,23018,23016,23014,23012,23009,23007,23005,23003,23000,22998,22996,22994,22991,22989,22987,22985,22982,22980,22978,22976,22973,22971,22969,22967,22965,22962,22960,22958,22956,22953,22951,22949,22947,22944,22942,22940,22938,22935,22933,22931,22929,22926,22924,22922,22920,22917,22915,22913,22911,22908,22906,22904,22902,22899,22897,22895,22893,22890,22888,22886,22884,22881,22879,22877,22875,22872,22870,22868,22866,22863,22861,22859,22857,22854,22852,22850,22848,22845,22843,22841,22839,22836,22834,22832,22830,22827,22825,22823,22821,22818,22816,22814,22812,22809,22807,22805,22803,22800,22798,22796,22794,22791,22789,22787,22785,22782,22780,22778,22776,22773,22771,22769,22766,22764,22762,22760,22757,22755,22753,22751,22748,22746,22744,22742,22739,22737,22735,22733,22730,22728,22726,22724,22721,22719,22717,22714,22712,22710,22708,22705,22703,22701,22699,22696,22694,22692,22690,22687,22685,22683,22680,22678,22676,22674,22671,22669,22667,22665,22662,22660,22658,22656,22653,22651,22649,22646,22644,22642,22640,22637,22635,22633,22631,22628,22626,22624,22621,22619,22617,22615,22612,22610,22608,22606,22603,22601,22599,22596,22594,22592,22590,22587,22585,22583,22581,22578,22576,22574,22571,22569,22567,22565,22562,22560,22558,22555,22553,22551,22549,22546,22544,22542,22540,22537,22535,22533,22530,22528,22526,22524,22521,22519,22517,22514,22512,22510,22508,22505,22503,22501,22498,22496,22494,22492,22489,22487,22485,22482,22480,22478,22476,22473,22471,22469,22466,22464,22462,22460,22457,22455,22453,22450,22448,22446,22444,22441,22439,22437,22434,22432,22430,22428,22425,22423,22421,22418,22416,22414,22411,22409,22407,22405,22402,22400,22398,22395,22393,22391,22389,22386,22384,22382,22379,22377,22375,22373,22370,22368,22366,22363,22361,22359,22356,22354,22352,22350,22347,22345,22343,22340,22338,22336,22333,22331,22329,22327,22324,22322,22320,22317,22315,22313,22310,22308,22306,22304,22301,22299,22297,22294,22292,22290,22287,22285,22283,22281,22278,22276,22274,22271,22269,22267,22264,22262,22260,22257,22255,22253,22251,22248,22246,22244,22241,22239,22237,22234,22232,22230,22227,22225,22223,22221,22218,22216,22214,22211,22209,22207,22204,22202,22200,22197,22195,22193,22191,22188,22186,22184,22181,22179,22177,22174,22172,22170,22167,22165,22163,22160,22158,22156,22154,22151,22149,22147,22144,22142,22140,22137,22135,22133,22130,22128,22126,22123,22121,22119,22116,22114,22112,22110,22107,22105,22103,22100,22098,22096,22093,22091,22089,22086,22084,22082,22079,22077,22075,22072,22070,22068,22065,22063,22061,22058,22056,22054,22051,22049,22047,22045,22042,22040,22038,22035,22033,22031,22028,22026,22024,22021,22019,22017,22014,22012,22010,22007,22005,22003,22000,21998,21996,21993,21991,21989,21986,21984,21982,21979,21977,21975,21972,21970,21968,21965,21963,21961,21958,21956,21954,21951,21949,21947,21944,21942,21940,21937,21935,21933,21930,21928,21926,21923,21921,21919,21916,21914,21912,21909,21907,21905,21902,21900,21898,21895,21893,21891,21888,21886,21884,21881,21879,21877,21874,21872,21870,21867,21865,21863,21860,21858,21856,21853,21851,21849,21846,21844,21842,21839,21837,21835,21832,21830,21827,21825,21823,21820,21818,21816,21813,21811,21809,21806,21804,21802,21799,21797,21795,21792,21790,21788,21785,21783,21781,21778,21776,21774,21771,21769,21766,21764,21762,21759,21757,21755,21752,21750,21748,21745,21743,21741,21738,21736,21734,21731,21729,21727,21724,21722,21719,21717,21715,21712,21710,21708,21705,21703,21701,21698,21696,21694,21691,21689,21687,21684,21682,21679,21677,21675,21672,21670,21668,21665,21663,21661,21658,21656,21654,21651,21649,21646,21644,21642,21639,21637,21635,21632,21630,21628,21625,21623,21621,21618,21616,21613,21611,21609,21606,21604,21602,21599,21597,21595,21592,21590,21587,21585,21583,21580,21578,21576,21573,21571,21569,21566,21564,21561,21559,21557,21554,21552,21550,21547,21545,21543,21540,21538,21535,21533,21531,21528,21526,21524,21521,21519,21516,21514,21512,21509,21507,21505,21502,21500,21498,21495,21493,21490,21488,21486,21483,21481,21479,21476,21474,21471,21469,21467,21464,21462,21460,21457,21455,21452,21450,21448,21445,21443,21441,21438,21436,21433,21431,21429,21426,21424,21422,21419,21417,21414,21412,21410,21407,21405,21403,21400,21398,21395,21393,21391,21388,21386,21383,21381,21379,21376,21374,21372,21369,21367,21364,21362,21360,21357,21355,21353,21350,21348,21345,21343,21341,21338,21336,21333,21331,21329,21326,21324,21322,21319,21317,21314,21312,21310,21307,21305,21302,21300,21298,21295,21293,21290,21288,21286,21283,21281,21279,21276,21274,21271,21269,21267,21264,21262,21259,21257,21255,21252,21250,21247,21245,21243,21240,21238,21236,21233,21231,21228,21226,21224,21221,21219,21216,21214,21212,21209,21207,21204,21202,21200,21197,21195,21192,21190,21188,21185,21183,21180,21178,21176,21173,21171,21168,21166,21164,21161,21159,21156,21154,21152,21149,21147,21144,21142,21140,21137,21135,21132,21130,21128,21125,21123,21120,21118,21116,21113,21111,21108,21106,21104,21101,21099,21096,21094,21092,21089,21087,21084,21082,21080,21077,21075,21072,21070,21068,21065,21063,21060,21058,21056,21053,21051,21048,21046,21043,21041,21039,21036,21034,21031,21029,21027,21024,21022,21019,21017,21015,21012,21010,21007,21005,21003,21000,20998,20995,20993,20990,20988,20986,20983,20981,20978,20976,20974,20971,20969,20966,20964,20962,20959,20957,20954,20952,20949,20947,20945,20942,20940,20937,20935,20933,20930,20928,20925,20923,20920,20918,20916,20913,20911,20908,20906,20904,20901,20899,20896,20894,20891,20889,20887,20884,20882,20879,20877,20874,20872,20870,20867,20865,20862,20860,20858,20855,20853,20850,20848,20845,20843,20841,20838,20836,20833,20831,20828,20826,20824,20821,20819,20816,20814,20811,20809,20807,20804,20802,20799,20797,20794,20792,20790,20787,20785,20782,20780,20777,20775,20773,20770,20768,20765,20763,20760,20758,20756,20753,20751,20748,20746,20743,20741,20739,20736,20734,20731,20729,20726,20724,20722,20719,20717,20714,20712,20709,20707,20704,20702,20700,20697,20695,20692,20690,20687,20685,20683,20680,20678,20675,20673,20670,20668,20666,20663,20661,20658,20656,20653,20651,20648,20646,20644,20641,20639,20636,20634,20631,20629,20626,20624,20622,20619,20617,20614,20612,20609,20607,20604,20602,20600,20597,20595,20592,20590,20587,20585,20583,20580,20578,20575,20573,20570,20568,20565,20563,20560,20558,20556,20553,20551,20548,20546,20543,20541,20538,20536,20534,20531,20529,20526,20524,20521,20519,20516,20514,20512,20509,20507,20504,20502,20499,20497,20494,20492,20489,20487,20485,20482,20480,20477,20475,20472,20470,20467,20465,20463,20460,20458,20455,20453,20450,20448,20445,20443,20440,20438,20436,20433,20431,20428,20426,20423,20421,20418,20416,20413,20411,20408,20406,20404,20401,20399,20396,20394,20391,20389,20386,20384,20381,20379,20377,20374,20372,20369,20367,20364,20362,20359,20357,20354,20352,20349,20347,20345,20342,20340,20337,20335,20332,20330,20327,20325,20322,20320,20317,20315,20312,20310,20308,20305,20303,20300,20298,20295,20293,20290,20288,20285,20283,20280,20278,20275,20273,20271,20268,20266,20263,20261,20258,20256,20253,20251,20248,20246,20243,20241,20238,20236,20234,20231,20229,20226,20224,20221,20219,20216,20214,20211,20209,20206,20204,20201,20199,20196,20194,20191,20189,20187,20184,20182,20179,20177,20174,20172,20169,20167,20164,20162,20159,20157,20154,20152,20149,20147,20144,20142,20139,20137,20135,20132,20130,20127,20125,20122,20120,20117,20115,20112,20110,20107,20105,20102,20100,20097,20095,20092,20090,20087,20085,20082,20080,20077,20075,20072,20070,20068,20065,20063,20060,20058,20055,20053,20050,20048,20045,20043,20040,20038,20035,20033,20030,20028,20025,20023,20020,20018,20015,20013,20010,20008,20005,20003,20000,19998,19995,19993,19990,19988,19985,19983,19981,19978,19976,19973,19971,19968,19966,19963,19961,19958,19956,19953,19951,19948,19946,19943,19941,19938,19936,19933,19931,19928,19926,19923,19921,19918,19916,19913,19911,19908,19906,19903,19901,19898,19896,19893,19891,19888,19886,19883,19881,19878,19876,19873,19871,19868,19866,19863,19861,19858,19856,19853,19851,19848,19846,19843,19841,19838,19836,19833,19831,19828,19826,19823,19821,19818,19816,19813,19811,19808,19806,19803,19801,19798,19796,19793,19791,19788,19786,19783,19781,19778,19776,19773,19771,19768,19766,19763,19761,19758,19756,19753,19751,19748,19746,19743,19741,19738,19736,19733,19731,19728,19726,19723,19721,19718,19716,19713,19711,19708,19706,19703,19700,19698,19695,19693,19690,19688,19685,19683,19680,19678,19675,19673,19670,19668,19665,19663,19660,19658,19655,19653,19650,19648,19645,19643,19640,19638,19635,19633,19630,19628,19625,19623,19620,19618,19615,19613,19610,19607,19605,19602,19600,19597,19595,19592,19590,19587,19585,19582,19580,19577,19575,19572,19570,19567,19565,19562,19560,19557,19555,19552,19550,19547,19545,19542,19539,19537,19534,19532,19529,19527,19524,19522,19519,19517,19514,19512,19509,19507,19504,19502,19499,19497,19494,19492,19489,19486,19484,19481,19479,19476,19474,19471,19469,19466,19464,19461,19459,19456,19454,19451,19449,19446,19444,19441,19438,19436,19433,19431,19428,19426,19423,19421,19418,19416,19413,19411,19408,19406,19403,19400,19398,19395,19393,19390,19388,19385,19383,19380,19378,19375,19373,19370,19368,19365,19362,19360,19357,19355,19352,19350,19347,19345,19342,19340,19337,19335,19332,19330,19327,19324,19322,19319,19317,19314,19312,19309,19307,19304,19302,19299,19297,19294,19291,19289,19286,19284,19281,19279,19276,19274,19271,19269,19266,19264,19261,19258,19256,19253,19251,19248,19246,19243,19241,19238,19236,19233,19230,19228,19225,19223,19220,19218,19215,19213,19210,19208,19205,19202,19200,19197,19195,19192,19190,19187,19185,19182,19180,19177,19174,19172,19169,19167,19164,19162,19159,19157,19154,19152,19149,19146,19144,19141,19139,19136,19134,19131,19129,19126,19123,19121,19118,19116,19113,19111,19108,19106,19103,19101,19098,19095,19093,19090,19088,19085,19083,19080,19078,19075,19072,19070,19067,19065,19062,19060,19057,19055,19052,19049,19047,19044,19042,19039,19037,19034,19032,19029,19026,19024,19021,19019,19016,19014,19011,19009,19006,19003,19001,18998,18996,18993,18991,18988,18985,18983,18980,18978,18975,18973,18970,18968,18965,18962,18960,18957,18955,18952,18950,18947,18944,18942,18939,18937,18934,18932,18929,18927,18924,18921,18919,18916,18914,18911,18909,18906,18903,18901,18898,18896,18893,18891,18888,18885,18883,18880,18878,18875,18873,18870,18868,18865,18862,18860,18857,18855,18852,18850,18847,18844,18842,18839,18837,18834,18832,18829,18826,18824,18821,18819,18816,18814,18811,18808,18806,18803,18801,18798,18796,18793,18790,18788,18785,18783,18780,18778,18775,18772,18770,18767,18765,18762,18759,18757,18754,18752,18749,18747,18744,18741,18739,18736,18734,18731,18729,18726,18723,18721,18718,18716,18713,18711,18708,18705,18703,18700,18698,18695,18692,18690,18687,18685,18682,18680,18677,18674,18672,18669,18667,18664,18661,18659,18656,18654,18651,18649,18646,18643,18641,18638,18636,18633,18630,18628,18625,18623,18620,18618,18615,18612,18610,18607,18605,18602,18599,18597,18594,18592,18589,18587,18584,18581,18579,18576,18574,18571,18568,18566,18563,18561,18558,18555,18553,18550,18548,18545,18543,18540,18537,18535,18532,18530,18527,18524,18522,18519,18517,18514,18511,18509,18506,18504,18501,18498,18496,18493,18491,18488,18485,18483,18480,18478,18475,18473,18470,18467,18465,18462,18460,18457,18454,18452,18449,18447,18444,18441,18439,18436,18434,18431,18428,18426,18423,18421,18418,18415,18413,18410,18408,18405,18402,18400,18397,18395,18392,18389,18387,18384,18382,18379,18376,18374,18371,18369,18366,18363,18361,18358,18356,18353,18350,18348,18345,18343,18340,18337,18335,18332,18330,18327,18324,18322,18319,18317,18314,18311,18309,18306,18304,18301,18298,18296,18293,18290,18288,18285,18283,18280,18277,18275,18272,18270,18267,18264,18262,18259,18257,18254,18251,18249,18246,18244,18241,18238,18236,18233,18230,18228,18225,18223,18220,18217,18215,18212,18210,18207,18204,18202,18199,18197,18194,18191,18189,18186,18183,18181,18178,18176,18173,18170,18168,18165,18163,18160,18157,18155,18152,18149,18147,18144,18142,18139,18136,18134,18131,18129,18126,18123,18121,18118,18115,18113,18110,18108,18105,18102,18100,18097,18095,18092,18089,18087,18084,18081,18079,18076,18074,18071,18068,18066,18063,18060,18058,18055,18053,18050,18047,18045,18042,18039,18037,18034,18032,18029,18026,18024,18021,18018,18016,18013,18011,18008,18005,18003,18000,17997,17995,17992,17990,17987,17984,17982,17979,17976,17974,17971,17969,17966,17963,17961,17958,17955,17953,17950,17948,17945,17942,17940,17937,17934,17932,17929,17927,17924,17921,17919,17916,17913,17911,17908,17906,17903,17900,17898,17895,17892,17890,17887,17884,17882,17879,17877,17874,17871,17869,17866,17863,17861,17858,17855,17853,17850,17848,17845,17842,17840,17837,17834,17832,17829,17827,17824,17821,17819,17816,17813,17811,17808,17805,17803,17800,17798,17795,17792,17790,17787,17784,17782,17779,17776,17774,17771,17768,17766,17763,17761,17758,17755,17753,17750,17747,17745,17742,17739,17737,17734,17732,17729,17726,17724,17721,17718,17716,17713,17710,17708,17705,17702,17700,17697,17695,17692,17689,17687,17684,17681,17679,17676,17673,17671,17668,17665,17663,17660,17657,17655,17652,17650,17647,17644,17642,17639,17636,17634,17631,17628,17626,17623,17620,17618,17615,17612,17610,17607,17605,17602,17599,17597,17594,17591,17589,17586,17583,17581,17578,17575,17573,17570,17567,17565,17562,17559,17557,17554,17551,17549,17546,17544,17541,17538,17536,17533,17530,17528,17525,17522,17520,17517,17514,17512,17509,17506,17504,17501,17498,17496,17493,17490,17488,17485,17482,17480,17477,17474,17472,17469,17467,17464,17461,17459,17456,17453,17451,17448,17445,17443,17440,17437,17435,17432,17429,17427,17424,17421,17419,17416,17413,17411,17408,17405,17403,17400,17397,17395,17392,17389,17387,17384,17381,17379,17376,17373,17371,17368,17365,17363,17360,17357,17355,17352,17349,17347,17344,17341,17339,17336,17333,17331,17328,17325,17323,17320,17317,17315,17312,17309,17307,17304,17301,17299,17296,17293,17291,17288,17285,17283,17280,17277,17275,17272,17269,17267,17264,17261,17259,17256,17253,17251,17248,17245,17243,17240,17237,17235,17232,17229,17227,17224,17221,17219,17216,17213,17211,17208,17205,17203,17200,17197,17195,17192,17189,17187,17184,17181,17179,17176,17173,17171,17168,17165,17162,17160,17157,17154,17152,17149,17146,17144,17141,17138,17136,17133,17130,17128,17125,17122,17120,17117,17114,17112,17109,17106,17104,17101,17098,17096,17093,17090,17087,17085,17082,17079,17077,17074,17071,17069,17066,17063,17061,17058,17055,17053,17050,17047,17045,17042,17039,17037,17034,17031,17028,17026,17023,17020,17018,17015,17012,17010,17007,17004,17002,16999,16996,16994,16991,16988,16986,16983,16980,16977,16975,16972,16969,16967,16964,16961,16959,16956,16953,16951,16948,16945,16943,16940,16937,16934,16932,16929,16926,16924,16921,16918,16916,16913,16910,16908,16905,16902,16899,16897,16894,16891,16889,16886,16883,16881,16878,16875,16873,16870,16867,16864,16862,16859,16856,16854,16851,16848,16846,16843,16840,16838,16835,16832,16829,16827,16824,16821,16819,16816,16813,16811,16808,16805,16802,16800,16797,16794,16792,16789,16786,16784,16781,16778,16775,16773,16770,16767,16765,16762,16759,16757,16754,16751,16749,16746,16743,16740,16738,16735,16732,16730,16727,16724,16721,16719,16716,16713,16711,16708,16705,16703,16700,16697,16694,16692,16689,16686,16684,16681,16678,16676,16673,16670,16667,16665,16662,16659,16657,16654,16651,16648,16646,16643,16640,16638,16635,16632,16630,16627,16624,16621,16619,16616,16613,16611,16608,16605,16602,16600,16597,16594,16592,16589,16586,16584,16581,16578,16575,16573,16570,16567,16565,16562,16559,16556,16554,16551,16548,16546,16543,16540,16537,16535,16532,16529,16527,16524,16521,16518,16516,16513,16510,16508,16505,16502,16499,16497,16494,16491,16489,16486,16483,16480,16478,16475,16472,16470,16467,16464,16461,16459,16456,16453,16451,16448,16445,16442,16440,16437,16434,16432,16429,16426,16423,16421,16418,16415,16413,16410,16407,16404,16402,16399,16396,16393,16391,16388,16385,16383,16380,16377,16374,16372,16369,16366,16364,16361,16358,16355,16353,16350,16347,16344,16342,16339,16336,16334,16331,16328,16325,16323,16320,16317,16315,16312,16309,16306,16304,16301,16298,16295,16293,16290,16287,16285,16282,16279,16276,16274,16271,16268,16265,16263,16260,16257,16255,16252,16249,16246,16244,16241,16238,16235,16233,16230,16227,16225,16222,16219,16216,16214,16211,16208,16205,16203,16200,16197,16195,16192,16189,16186,16184,16181,16178,16175,16173,16170,16167,16164,16162,16159,16156,16154,16151,16148,16145,16143,16140,16137,16134,16132,16129,16126,16123,16121,16118,16115,16113,16110,16107,16104,16102,16099,16096,16093,16091,16088,16085,16082,16080,16077,16074,16071,16069,16066,16063,16061,16058,16055,16052,16050,16047,16044,16041,16039,16036,16033,16030,16028,16025,16022,16019,16017,16014,16011,16008,16006,16003,16000,15997,15995,15992,15989,15987,15984,15981,15978,15976,15973,15970,15967,15965,15962,15959,15956,15954,15951,15948,15945,15943,15940,15937,15934,15932,15929,15926,15923,15921,15918,15915,15912,15910,15907,15904,15901,15899,15896,15893,15890,15888,15885,15882,15879,15877,15874,15871,15868,15866,15863,15860,15857,15855,15852,15849,15846,15844,15841,15838,15835,15833,15830,15827,15824,15822,15819,15816,15813,15811,15808,15805,15802,15800,15797,15794,15791,15789,15786,15783,15780,15778,15775,15772,15769,15767,15764,15761,15758,15756,15753,15750,15747,15745,15742,15739,15736,15734,15731,15728,15725,15723,15720,15717,15714,15712,15709,15706,15703,15701,15698,15695,15692,15690,15687,15684,15681,15678,15676,15673,15670,15667,15665,15662,15659,15656,15654,15651,15648,15645,15643,15640,15637,15634,15632,15629,15626,15623,15621,15618,15615,15612,15609,15607,15604,15601,15598,15596,15593,15590,15587,15585,15582,15579,15576,15574,15571,15568,15565,15562,15560,15557,15554,15551,15549,15546,15543,15540,15538,15535,15532,15529,15527,15524,15521,15518,15515,15513,15510,15507,15504,15502,15499,15496,15493,15491,15488,15485,15482,15479,15477,15474,15471,15468,15466,15463,15460,15457,15455,15452,15449,15446,15443,15441,15438,15435,15432,15430,15427,15424,15421,15419,15416,15413,15410,15407,15405,15402,15399,15396,15394,15391,15388,15385,15382,15380,15377,15374,15371,15369,15366,15363,15360,15358,15355,15352,15349,15346,15344,15341,15338,15335,15333,15330,15327,15324,15321,15319,15316,15313,15310,15308,15305,15302,15299,15296,15294,15291,15288,15285,15283,15280,15277,15274,15271,15269,15266,15263,15260,15258,15255,15252,15249,15246,15244,15241,15238,15235,15233,15230,15227,15224,15221,15219,15216,15213,15210,15207,15205,15202,15199,15196,15194,15191,15188,15185,15182,15180,15177,15174,15171,15168,15166,15163,15160,15157,15155,15152,15149,15146,15143,15141,15138,15135,15132,15129,15127,15124,15121,15118,15116,15113,15110,15107,15104,15102,15099,15096,15093,15090,15088,15085,15082,15079,15077,15074,15071,15068,15065,15063,15060,15057,15054,15051,15049,15046,15043,15040,15037,15035,15032,15029,15026,15024,15021,15018,15015,15012,15010,15007,15004,15001,14998,14996,14993,14990,14987,14984,14982,14979,14976,14973,14970,14968,14965,14962,14959,14956,14954,14951,14948,14945,14942,14940,14937,14934,14931,14929,14926,14923,14920,14917,14915,14912,14909,14906,14903,14901,14898,14895,14892,14889,14887,14884,14881,14878,14875,14873,14870,14867,14864,14861,14859,14856,14853,14850,14847,14845,14842,14839,14836,14833,14831,14828,14825,14822,14819,14817,14814,14811,14808,14805,14803,14800,14797,14794,14791,14789,14786,14783,14780,14777,14774,14772,14769,14766,14763,14760,14758,14755,14752,14749,14746,14744,14741,14738,14735,14732,14730,14727,14724,14721,14718,14716,14713,14710,14707,14704,14702,14699,14696,14693,14690,14688,14685,14682,14679,14676,14673,14671,14668,14665,14662,14659,14657,14654,14651,14648,14645,14643,14640,14637,14634,14631,14628,14626,14623,14620,14617,14614,14612,14609,14606,14603,14600,14598,14595,14592,14589,14586,14584,14581,14578,14575,14572,14569,14567,14564,14561,14558,14555,14553,14550,14547,14544,14541,14538,14536,14533,14530,14527,14524,14522,14519,14516,14513,14510,14507,14505,14502,14499,14496,14493,14491,14488,14485,14482,14479,14477,14474,14471,14468,14465,14462,14460,14457,14454,14451,14448,14445,14443,14440,14437,14434,14431,14429,14426,14423,14420,14417,14414,14412,14409,14406,14403,14400,14398,14395,14392,14389,14386,14383,14381,14378,14375,14372,14369,14366,14364,14361,14358,14355,14352,14350,14347,14344,14341,14338,14335,14333,14330,14327,14324,14321,14318,14316,14313,14310,14307,14304,14302,14299,14296,14293,14290,14287,14285,14282,14279,14276,14273,14270,14268,14265,14262,14259,14256,14253,14251,14248,14245,14242,14239,14236,14234,14231,14228,14225,14222,14219,14217,14214,14211,14208,14205,14203,14200,14197,14194,14191,14188,14186,14183,14180,14177,14174,14171,14169,14166,14163,14160,14157,14154,14152,14149,14146,14143,14140,14137,14135,14132,14129,14126,14123,14120,14118,14115,14112,14109,14106,14103,14101,14098,14095,14092,14089,14086,14083,14081,14078,14075,14072,14069,14066,14064,14061,14058,14055,14052,14049,14047,14044,14041,14038,14035,14032,14030,14027,14024,14021,14018,14015,14013,14010,14007,14004,14001,13998,13995,13993,13990,13987,13984,13981,13978,13976,13973,13970,13967,13964,13961,13959,13956,13953,13950,13947,13944,13942,13939,13936,13933,13930,13927,13924,13922,13919,13916,13913,13910,13907,13905,13902,13899,13896,13893,13890,13887,13885,13882,13879,13876,13873,13870,13868,13865,13862,13859,13856,13853,13850,13848,13845,13842,13839,13836,13833,13831,13828,13825,13822,13819,13816,13813,13811,13808,13805,13802,13799,13796,13793,13791,13788,13785,13782,13779,13776,13774,13771,13768,13765,13762,13759,13756,13754,13751,13748,13745,13742,13739,13736,13734,13731,13728,13725,13722,13719,13717,13714,13711,13708,13705,13702,13699,13697,13694,13691,13688,13685,13682,13679,13677,13674,13671,13668,13665,13662,13659,13657,13654,13651,13648,13645,13642,13639,13637,13634,13631,13628,13625,13622,13619,13617,13614,13611,13608,13605,13602,13599,13597,13594,13591,13588,13585,13582,13579,13577,13574,13571,13568,13565,13562,13559,13557,13554,13551,13548,13545,13542,13539,13537,13534,13531,13528,13525,13522,13519,13516,13514,13511,13508,13505,13502,13499,13496,13494,13491,13488,13485,13482,13479,13476,13474,13471,13468,13465,13462,13459,13456,13454,13451,13448,13445,13442,13439,13436,13433,13431,13428,13425,13422,13419,13416,13413,13411,13408,13405,13402,13399,13396,13393,13390,13388,13385,13382,13379,13376,13373,13370,13368,13365,13362,13359,13356,13353,13350,13347,13345,13342,13339,13336,13333,13330,13327,13324,13322,13319,13316,13313,13310,13307,13304,13302,13299,13296,13293,13290,13287,13284,13281,13279,13276,13273,13270,13267,13264,13261,13258,13256,13253,13250,13247,13244,13241,13238,13235,13233,13230,13227,13224,13221,13218,13215,13212,13210,13207,13204,13201,13198,13195,13192,13189,13187,13184,13181,13178,13175,13172,13169,13166,13164,13161,13158,13155,13152,13149,13146,13143,13141,13138,13135,13132,13129,13126,13123,13120,13118,13115,13112,13109,13106,13103,13100,13097,13094,13092,13089,13086,13083,13080,13077,13074,13071,13069,13066,13063,13060,13057,13054,13051,13048,13046,13043,13040,13037,13034,13031,13028,13025,13022,13020,13017,13014,13011,13008,13005,13002,12999,12997,12994,12991,12988,12985,12982,12979,12976,12973,12971,12968,12965,12962,12959,12956,12953,12950,12947,12945,12942,12939,12936,12933,12930,12927,12924,12921,12919,12916,12913,12910,12907,12904,12901,12898,12895,12893,12890,12887,12884,12881,12878,12875,12872,12870,12867,12864,12861,12858,12855,12852,12849,12846,12843,12841,12838,12835,12832,12829,12826,12823,12820,12817,12815,12812,12809,12806,12803,12800,12797,12794,12791,12789,12786,12783,12780,12777,12774,12771,12768,12765,12763,12760,12757,12754,12751,12748,12745,12742,12739,12736,12734,12731,12728,12725,12722,12719,12716,12713,12710,12708,12705,12702,12699,12696,12693,12690,12687,12684,12681,12679,12676,12673,12670,12667,12664,12661,12658,12655,12652,12650,12647,12644,12641,12638,12635,12632,12629,12626,12624,12621,12618,12615,12612,12609,12606,12603,12600,12597,12595,12592,12589,12586,12583,12580,12577,12574,12571,12568,12566,12563,12560,12557,12554,12551,12548,12545,12542,12539,12536,12534,12531,12528,12525,12522,12519,12516,12513,12510,12507,12505,12502,12499,12496,12493,12490,12487,12484,12481,12478,12476,12473,12470,12467,12464,12461,12458,12455,12452,12449,12446,12444,12441,12438,12435,12432,12429,12426,12423,12420,12417,12414,12412,12409,12406,12403,12400,12397,12394,12391,12388,12385,12382,12380,12377,12374,12371,12368,12365,12362,12359,12356,12353,12350,12348,12345,12342,12339,12336,12333,12330,12327,12324,12321,12318,12316,12313,12310,12307,12304,12301,12298,12295,12292,12289,12286,12284,12281,12278,12275,12272,12269,12266,12263,12260,12257,12254,12251,12249,12246,12243,12240,12237,12234,12231,12228,12225,12222,12219,12217,12214,12211,12208,12205,12202,12199,12196,12193,12190,12187,12184,12182,12179,12176,12173,12170,12167,12164,12161,12158,12155,12152,12149,12147,12144,12141,12138,12135,12132,12129,12126,12123,12120,12117,12114,12112,12109,12106,12103,12100,12097,12094,12091,12088,12085,12082,12079,12076,12074,12071,12068,12065,12062,12059,12056,12053,12050,12047,12044,12041,12038,12036,12033,12030,12027,12024,12021,12018,12015,12012,12009,12006,12003,12001,11998,11995,11992,11989,11986,11983,11980,11977,11974,11971,11968,11965,11962,11960,11957,11954,11951,11948,11945,11942,11939,11936,11933,11930,11927,11924,11922,11919,11916,11913,11910,11907,11904,11901,11898,11895,11892,11889,11886,11883,11881,11878,11875,11872,11869,11866,11863,11860,11857,11854,11851,11848,11845,11842,11840,11837,11834,11831,11828,11825,11822,11819,11816,11813,11810,11807,11804,11801,11799,11796,11793,11790,11787,11784,11781,11778,11775,11772,11769,11766,11763,11760,11758,11755,11752,11749,11746,11743,11740,11737,11734,11731,11728,11725,11722,11719,11716,11714,11711,11708,11705,11702,11699,11696,11693,11690,11687,11684,11681,11678,11675,11672,11669,11667,11664,11661,11658,11655,11652,11649,11646,11643,11640,11637,11634,11631,11628,11625,11623,11620,11617,11614,11611,11608,11605,11602,11599,11596,11593,11590,11587,11584,11581,11578,11575,11573,11570,11567,11564,11561,11558,11555,11552,11549,11546,11543,11540,11537,11534,11531,11528,11526,11523,11520,11517,11514,11511,11508,11505,11502,11499,11496,11493,11490,11487,11484,11481,11478,11476,11473,11470,11467,11464,11461,11458,11455,11452,11449,11446,11443,11440,11437,11434,11431,11428,11425,11423,11420,11417,11414,11411,11408,11405,11402,11399,11396,11393,11390,11387,11384,11381,11378,11375,11372,11370,11367,11364,11361,11358,11355,11352,11349,11346,11343,11340,11337,11334,11331,11328,11325,11322,11319,11316,11314,11311,11308,11305,11302,11299,11296,11293,11290,11287,11284,11281,11278,11275,11272,11269,11266,11263,11260,11257,11255,11252,11249,11246,11243,11240,11237,11234,11231,11228,11225,11222,11219,11216,11213,11210,11207,11204,11201,11198,11195,11193,11190,11187,11184,11181,11178,11175,11172,11169,11166,11163,11160,11157,11154,11151,11148,11145,11142,11139,11136,11133,11131,11128,11125,11122,11119,11116,11113,11110,11107,11104,11101,11098,11095,11092,11089,11086,11083,11080,11077,11074,11071,11068,11065,11063,11060,11057,11054,11051,11048,11045,11042,11039,11036,11033,11030,11027,11024,11021,11018,11015,11012,11009,11006,11003,11000,10997,10994,10992,10989,10986,10983,10980,10977,10974,10971,10968,10965,10962,10959,10956,10953,10950,10947,10944,10941,10938,10935,10932,10929,10926,10923,10920,10918,10915,10912,10909,10906,10903,10900,10897,10894,10891,10888,10885,10882,10879,10876,10873,10870,10867,10864,10861,10858,10855,10852,10849,10846,10843,10840,10838,10835,10832,10829,10826,10823,10820,10817,10814,10811,10808,10805,10802,10799,10796,10793,10790,10787,10784,10781,10778,10775,10772,10769,10766,10763,10760,10757,10754,10751,10749,10746,10743,10740,10737,10734,10731,10728,10725,10722,10719,10716,10713,10710,10707,10704,10701,10698,10695,10692,10689,10686,10683,10680,10677,10674,10671,10668,10665,10662,10659,10656,10654,10651,10648,10645,10642,10639,10636,10633,10630,10627,10624,10621,10618,10615,10612,10609,10606,10603,10600,10597,10594,10591,10588,10585,10582,10579,10576,10573,10570,10567,10564,10561,10558,10555,10552,10549,10546,10544,10541,10538,10535,10532,10529,10526,10523,10520,10517,10514,10511,10508,10505,10502,10499,10496,10493,10490,10487,10484,10481,10478,10475,10472,10469,10466,10463,10460,10457,10454,10451,10448,10445,10442,10439,10436,10433,10430,10427,10424,10421,10419,10416,10413,10410,10407,10404,10401,10398,10395,10392,10389,10386,10383,10380,10377,10374,10371,10368,10365,10362,10359,10356,10353,10350,10347,10344,10341,10338,10335,10332,10329,10326,10323,10320,10317,10314,10311,10308,10305,10302,10299,10296,10293,10290,10287,10284,10281,10278,10275,10272,10269,10266,10263,10261,10258,10255,10252,10249,10246,10243,10240,10237,10234,10231,10228,10225,10222,10219,10216,10213,10210,10207,10204,10201,10198,10195,10192,10189,10186,10183,10180,10177,10174,10171,10168,10165,10162,10159,10156,10153,10150,10147,10144,10141,10138,10135,10132,10129,10126,10123,10120,10117,10114,10111,10108,10105,10102,10099,10096,10093,10090,10087,10084,10081,10078,10075,10072,10069,10066,10063,10060,10057,10054,10051,10048,10045,10042,10039,10036,10033,10031,10028,10025,10022,10019,10016,10013,10010,10007,10004,10001,9998,9995,9992,9989,9986,9983,9980,9977,9974,9971,9968,9965,9962,9959,9956,9953,9950,9947,9944,9941,9938,9935,9932,9929,9926,9923,9920,9917,9914,9911,9908,9905,9902,9899,9896,9893,9890,9887,9884,9881,9878,9875,9872,9869,9866,9863,9860,9857,9854,9851,9848,9845,9842,9839,9836,9833,9830,9827,9824,9821,9818,9815,9812,9809,9806,9803,9800,9797,9794,9791,9788,9785,9782,9779,9776,9773,9770,9767,9764,9761,9758,9755,9752,9749,9746,9743,9740,9737,9734,9731,9728,9725,9722,9719,9716,9713,9710,9707,9704,9701,9698,9695,9692,9689,9686,9683,9680,9677,9674,9671,9668,9665,9662,9659,9656,9653,9650,9647,9644,9641,9638,9635,9632,9629,9626,9623,9620,9617,9614,9611,9608,9605,9602,9599,9596,9593,9590,9587,9584,9581,9578,9575,9572,9569,9566,9563,9560,9557,9554,9551,9548,9545,9542,9539,9536,9533,9530,9527,9524,9521,9518,9515,9512,9509,9506,9503,9500,9497,9494,9491,9488,9485,9482,9479,9476,9473,9470,9467,9464,9461,9458,9455,9452,9449,9446,9443,9440,9437,9434,9431,9428,9425,9422,9419,9416,9413,9409,9406,9403,9400,9397,9394,9391,9388,9385,9382,9379,9376,9373,9370,9367,9364,9361,9358,9355,9352,9349,9346,9343,9340,9337,9334,9331,9328,9325,9322,9319,9316,9313,9310,9307,9304,9301,9298,9295,9292,9289,9286,9283,9280,9277,9274,9271,9268,9265,9262,9259,9256,9253,9250,9247,9244,9241,9238,9235,9232,9229,9226,9223,9220,9217,9214,9211,9208,9205,9202,9199,9196,9193,9190,9187,9184,9181,9178,9175,9172,9168,9165,9162,9159,9156,9153,9150,9147,9144,9141,9138,9135,9132,9129,9126,9123,9120,9117,9114,9111,9108,9105,9102,9099,9096,9093,9090,9087,9084,9081,9078,9075,9072,9069,9066,9063,9060,9057,9054,9051,9048,9045,9042,9039,9036,9033,9030,9027,9024,9021,9018,9015,9012,9009,9006,9002,8999,8996,8993,8990,8987,8984,8981,8978,8975,8972,8969,8966,8963,8960,8957,8954,8951,8948,8945,8942,8939,8936,8933,8930,8927,8924,8921,8918,8915,8912,8909,8906,8903,8900,8897,8894,8891,8888,8885,8882,8879,8876,8873,8869,8866,8863,8860,8857,8854,8851,8848,8845,8842,8839,8836,8833,8830,8827,8824,8821,8818,8815,8812,8809,8806,8803,8800,8797,8794,8791,8788,8785,8782,8779,8776,8773,8770,8767,8764,8761,8758,8755,8751,8748,8745,8742,8739,8736,8733,8730,8727,8724,8721,8718,8715,8712,8709,8706,8703,8700,8697,8694,8691,8688,8685,8682,8679,8676,8673,8670,8667,8664,8661,8658,8655,8652,8649,8645,8642,8639,8636,8633,8630,8627,8624,8621,8618,8615,8612,8609,8606,8603,8600,8597,8594,8591,8588,8585,8582,8579,8576,8573,8570,8567,8564,8561,8558,8555,8552,8548,8545,8542,8539,8536,8533,8530,8527,8524,8521,8518,8515,8512,8509,8506,8503,8500,8497,8494,8491,8488,8485,8482,8479,8476,8473,8470,8467,8464,8460,8457,8454,8451,8448,8445,8442,8439,8436,8433,8430,8427,8424,8421,8418,8415,8412,8409,8406,8403,8400,8397,8394,8391,8388,8385,8382,8379,8375,8372,8369,8366,8363,8360,8357,8354,8351,8348,8345,8342,8339,8336,8333,8330,8327,8324,8321,8318,8315,8312,8309,8306,8303,8300,8296,8293,8290,8287,8284,8281,8278,8275,8272,8269,8266,8263,8260,8257,8254,8251,8248,8245,8242,8239,8236,8233,8230,8227,8224,8220,8217,8214,8211,8208,8205,8202,8199,8196,8193,8190,8187,8184,8181,8178,8175,8172,8169,8166,8163,8160,8157,8154,8151,8147,8144,8141,8138,8135,8132,8129,8126,8123,8120,8117,8114,8111,8108,8105,8102,8099,8096,8093,8090,8087,8084,8081,8077,8074,8071,8068,8065,8062,8059,8056,8053,8050,8047,8044,8041,8038,8035,8032,8029,8026,8023,8020,8017,8014,8010,8007,8004,8001,7998,7995,7992,7989,7986,7983,7980,7977,7974,7971,7968,7965,7962,7959,7956,7953,7950,7946,7943,7940,7937,7934,7931,7928,7925,7922,7919,7916,7913,7910,7907,7904,7901,7898,7895,7892,7889,7886,7882,7879,7876,7873,7870,7867,7864,7861,7858,7855,7852,7849,7846,7843,7840,7837,7834,7831,7828,7825,7821,7818,7815,7812,7809,7806,7803,7800,7797,7794,7791,7788,7785,7782,7779,7776,7773,7770,7767,7764,7760,7757,7754,7751,7748,7745,7742,7739,7736,7733,7730,7727,7724,7721,7718,7715,7712,7709,7705,7702,7699,7696,7693,7690,7687,7684,7681,7678,7675,7672,7669,7666,7663,7660,7657,7654,7651,7647,7644,7641,7638,7635,7632,7629,7626,7623,7620,7617,7614,7611,7608,7605,7602,7599,7596,7592,7589,7586,7583,7580,7577,7574,7571,7568,7565,7562,7559,7556,7553,7550,7547,7544,7541,7537,7534,7531,7528,7525,7522,7519,7516,7513,7510,7507,7504,7501,7498,7495,7492,7489,7485,7482,7479,7476,7473,7470,7467,7464,7461,7458,7455,7452,7449,7446,7443,7440,7437,7433,7430,7427,7424,7421,7418,7415,7412,7409,7406,7403,7400,7397,7394,7391,7388,7385,7381,7378,7375,7372,7369,7366,7363,7360,7357,7354,7351,7348,7345,7342,7339,7336,7332,7329,7326,7323,7320,7317,7314,7311,7308,7305,7302,7299,7296,7293,7290,7287,7283,7280,7277,7274,7271,7268,7265,7262,7259,7256,7253,7250,7247,7244,7241,7238,7234,7231,7228,7225,7222,7219,7216,7213,7210,7207,7204,7201,7198,7195,7192,7188,7185,7182,7179,7176,7173,7170,7167,7164,7161,7158,7155,7152,7149,7146,7143,7139,7136,7133,7130,7127,7124,7121,7118,7115,7112,7109,7106,7103,7100,7097,7093,7090,7087,7084,7081,7078,7075,7072,7069,7066,7063,7060,7057,7054,7050,7047,7044,7041,7038,7035,7032,7029,7026,7023,7020,7017,7014,7011,7008,7004,7001,6998,6995,6992,6989,6986,6983,6980,6977,6974,6971,6968,6965,6961,6958,6955,6952,6949,6946,6943,6940,6937,6934,6931,6928,6925,6922,6919,6915,6912,6909,6906,6903,6900,6897,6894,6891,6888,6885,6882,6879,6876,6872,6869,6866,6863,6860,6857,6854,6851,6848,6845,6842,6839,6836,6833,6829,6826,6823,6820,6817,6814,6811,6808,6805,6802,6799,6796,6793,6789,6786,6783,6780,6777,6774,6771,6768,6765,6762,6759,6756,6753,6750,6746,6743,6740,6737,6734,6731,6728,6725,6722,6719,6716,6713,6710,6706,6703,6700,6697,6694,6691,6688,6685,6682,6679,6676,6673,6670,6667,6663,6660,6657,6654,6651,6648,6645,6642,6639,6636,6633,6630,6627,6623,6620,6617,6614,6611,6608,6605,6602,6599,6596,6593,6590,6587,6583,6580,6577,6574,6571,6568,6565,6562,6559,6556,6553,6550,6547,6543,6540,6537,6534,6531,6528,6525,6522,6519,6516,6513,6510,6506,6503,6500,6497,6494,6491,6488,6485,6482,6479,6476,6473,6470,6466,6463,6460,6457,6454,6451,6448,6445,6442,6439,6436,6433,6429,6426,6423,6420,6417,6414,6411,6408,6405,6402,6399,6396,6393,6389,6386,6383,6380,6377,6374,6371,6368,6365,6362,6359,6356,6352,6349,6346,6343,6340,6337,6334,6331,6328,6325,6322,6319,6315,6312,6309,6306,6303,6300,6297,6294,6291,6288,6285,6282,6278,6275,6272,6269,6266,6263,6260,6257,6254,6251,6248,6245,6241,6238,6235,6232,6229,6226,6223,6220,6217,6214,6211,6208,6204,6201,6198,6195,6192,6189,6186,6183,6180,6177,6174,6171,6167,6164,6161,6158,6155,6152,6149,6146,6143,6140,6137,6134,6130,6127,6124,6121,6118,6115,6112,6109,6106,6103,6100,6096,6093,6090,6087,6084,6081,6078,6075,6072,6069,6066,6063,6059,6056,6053,6050,6047,6044,6041,6038,6035,6032,6029,6025,6022,6019,6016,6013,6010,6007,6004,6001,5998,5995,5991,5988,5985,5982,5979,5976,5973,5970,5967,5964,5961,5958,5954,5951,5948,5945,5942,5939,5936,5933,5930,5927,5924,5920,5917,5914,5911,5908,5905,5902,5899,5896,5893,5890,5886,5883,5880,5877,5874,5871,5868,5865,5862,5859,5856,5852,5849,5846,5843,5840,5837,5834,5831,5828,5825,5822,5818,5815,5812,5809,5806,5803,5800,5797,5794,5791,5788,5784,5781,5778,5775,5772,5769,5766,5763,5760,5757,5754,5750,5747,5744,5741,5738,5735,5732,5729,5726,5723,5719,5716,5713,5710,5707,5704,5701,5698,5695,5692,5689,5685,5682,5679,5676,5673,5670,5667,5664,5661,5658,5655,5651,5648,5645,5642,5639,5636,5633,5630,5627,5624,5620,5617,5614,5611,5608,5605,5602,5599,5596,5593,5590,5586,5583,5580,5577,5574,5571,5568,5565,5562,5559,5555,5552,5549,5546,5543,5540,5537,5534,5531,5528,5525,5521,5518,5515,5512,5509,5506,5503,5500,5497,5494,5490,5487,5484,5481,5478,5475,5472,5469,5466,5463,5459,5456,5453,5450,5447,5444,5441,5438,5435,5432,5428,5425,5422,5419,5416,5413,5410,5407,5404,5401,5398,5394,5391,5388,5385,5382,5379,5376,5373,5370,5367,5363,5360,5357,5354,5351,5348,5345,5342,5339,5336,5332,5329,5326,5323,5320,5317,5314,5311,5308,5305,5301,5298,5295,5292,5289,5286,5283,5280,5277,5274,5270,5267,5264,5261,5258,5255,5252,5249,5246,5243,5239,5236,5233,5230,5227,5224,5221,5218,5215,5212,5208,5205,5202,5199,5196,5193,5190,5187,5184,5180,5177,5174,5171,5168,5165,5162,5159,5156,5153,5149,5146,5143,5140,5137,5134,5131,5128,5125,5122,5118,5115,5112,5109,5106,5103,5100,5097,5094,5091,5087,5084,5081,5078,5075,5072,5069,5066,5063,5059,5056,5053,5050,5047,5044,5041,5038,5035,5032,5028,5025,5022,5019,5016,5013,5010,5007,5004,5000,4997,4994,4991,4988,4985,4982,4979,4976,4973,4969,4966,4963,4960,4957,4954,4951,4948,4945,4941,4938,4935,4932,4929,4926,4923,4920,4917,4914,4910,4907,4904,4901,4898,4895,4892,4889,4886,4882,4879,4876,4873,4870,4867,4864,4861,4858,4855,4851,4848,4845,4842,4839,4836,4833,4830,4827,4823,4820,4817,4814,4811,4808,4805,4802,4799,4795,4792,4789,4786,4783,4780,4777,4774,4771,4768,4764,4761,4758,4755,4752,4749,4746,4743,4740,4736,4733,4730,4727,4724,4721,4718,4715,4712,4708,4705,4702,4699,4696,4693,4690,4687,4684,4680,4677,4674,4671,4668,4665,4662,4659,4656,4652,4649,4646,4643,4640,4637,4634,4631,4628,4624,4621,4618,4615,4612,4609,4606,4603,4600,4597,4593,4590,4587,4584,4581,4578,4575,4572,4569,4565,4562,4559,4556,4553,4550,4547,4544,4541,4537,4534,4531,4528,4525,4522,4519,4516,4513,4509,4506,4503,4500,4497,4494,4491,4488,4485,4481,4478,4475,4472,4469,4466,4463,4460,4456,4453,4450,4447,4444,4441,4438,4435,4432,4428,4425,4422,4419,4416,4413,4410,4407,4404,4400,4397,4394,4391,4388,4385,4382,4379,4376,4372,4369,4366,4363,4360,4357,4354,4351,4348,4344,4341,4338,4335,4332,4329,4326,4323,4320,4316,4313,4310,4307,4304,4301,4298,4295,4291,4288,4285,4282,4279,4276,4273,4270,4267,4263,4260,4257,4254,4251,4248,4245,4242,4239,4235,4232,4229,4226,4223,4220,4217,4214,4210,4207,4204,4201,4198,4195,4192,4189,4186,4182,4179,4176,4173,4170,4167,4164,4161,4158,4154,4151,4148,4145,4142,4139,4136,4133,4129,4126,4123,4120,4117,4114,4111,4108,4105,4101,4098,4095,4092,4089,4086,4083,4080,4076,4073,4070,4067,4064,4061,4058,4055,4052,4048,4045,4042,4039,4036,4033,4030,4027,4024,4020,4017,4014,4011,4008,4005,4002,3999,3995,3992,3989,3986,3983,3980,3977,3974,3970,3967,3964,3961,3958,3955,3952,3949,3946,3942,3939,3936,3933,3930,3927,3924,3921,3917,3914,3911,3908,3905,3902,3899,3896,3893,3889,3886,3883,3880,3877,3874,3871,3868,3864,3861,3858,3855,3852,3849,3846,3843,3839,3836,3833,3830,3827,3824,3821,3818,3815,3811,3808,3805,3802,3799,3796,3793,3790,3786,3783,3780,3777,3774,3771,3768,3765,3761,3758,3755,3752,3749,3746,3743,3740,3737,3733,3730,3727,3724,3721,3718,3715,3712,3708,3705,3702,3699,3696,3693,3690,3687,3683,3680,3677,3674,3671,3668,3665,3662,3658,3655,3652,3649,3646,3643,3640,3637,3634,3630,3627,3624,3621,3618,3615,3612,3609,3605,3602,3599,3596,3593,3590,3587,3584,3580,3577,3574,3571,3568,3565,3562,3559,3555,3552,3549,3546,3543,3540,3537,3534,3530,3527,3524,3521,3518,3515,3512,3509,3505,3502,3499,3496,3493,3490,3487,3484,3480,3477,3474,3471,3468,3465,3462,3459,3455,3452,3449,3446,3443,3440,3437,3434,3430,3427,3424,3421,3418,3415,3412,3409,3406,3402,3399,3396,3393,3390,3387,3384,3381,3377,3374,3371,3368,3365,3362,3359,3356,3352,3349,3346,3343,3340,3337,3334,3331,3327,3324,3321,3318,3315,3312,3309,3306,3302,3299,3296,3293,3290,3287,3284,3281,3277,3274,3271,3268,3265,3262,3259,3255,3252,3249,3246,3243,3240,3237,3234,3230,3227,3224,3221,3218,3215,3212,3209,3205,3202,3199,3196,3193,3190,3187,3184,3180,3177,3174,3171,3168,3165,3162,3159,3155,3152,3149,3146,3143,3140,3137,3134,3130,3127,3124,3121,3118,3115,3112,3109,3105,3102,3099,3096,3093,3090,3087,3084,3080,3077,3074,3071,3068,3065,3062,3059,3055,3052,3049,3046,3043,3040,3037,3033,3030,3027,3024,3021,3018,3015,3012,3008,3005,3002,2999,2996,2993,2990,2987,2983,2980,2977,2974,2971,2968,2965,2962,2958,2955,2952,2949,2946,2943,2940,2936,2933,2930,2927,2924,2921,2918,2915,2911,2908,2905,2902,2899,2896,2893,2890,2886,2883,2880,2877,2874,2871,2868,2865,2861,2858,2855,2852,2849,2846,2843,2839,2836,2833,2830,2827,2824,2821,2818,2814,2811,2808,2805,2802,2799,2796,2793,2789,2786,2783,2780,2777,2774,2771,2767,2764,2761,2758,2755,2752,2749,2746,2742,2739,2736,2733,2730,2727,2724,2721,2717,2714,2711,2708,2705,2702,2699,2695,2692,2689,2686,2683,2680,2677,2674,2670,2667,2664,2661,2658,2655,2652,2649,2645,2642,2639,2636,2633,2630,2627,2623,2620,2617,2614,2611,2608,2605,2602,2598,2595,2592,2589,2586,2583,2580,2577,2573,2570,2567,2564,2561,2558,2555,2551,2548,2545,2542,2539,2536,2533,2530,2526,2523,2520,2517,2514,2511,2508,2504,2501,2498,2495,2492,2489,2486,2483,2479,2476,2473,2470,2467,2464,2461,2457,2454,2451,2448,2445,2442,2439,2436,2432,2429,2426,2423,2420,2417,2414,2410,2407,2404,2401,2398,2395,2392,2389,2385,2382,2379,2376,2373,2370,2367,2363,2360,2357,2354,2351,2348,2345,2342,2338,2335,2332,2329,2326,2323,2320,2316,2313,2310,2307,2304,2301,2298,2295,2291,2288,2285,2282,2279,2276,2273,2269,2266,2263,2260,2257,2254,2251,2248,2244,2241,2238,2235,2232,2229,2226,2222,2219,2216,2213,2210,2207,2204,2201,2197,2194,2191,2188,2185,2182,2179,2175,2172,2169,2166,2163,2160,2157,2154,2150,2147,2144,2141,2138,2135,2132,2128,2125,2122,2119,2116,2113,2110,2106,2103,2100,2097,2094,2091,2088,2085,2081,2078,2075,2072,2069,2066,2063,2059,2056,2053,2050,2047,2044,2041,2038,2034,2031,2028,2025,2022,2019,2016,2012,2009,2006,2003,2000,1997,1994,1990,1987,1984,1981,1978,1975,1972,1969,1965,1962,1959,1956,1953,1950,1947,1943,1940,1937,1934,1931,1928,1925,1921,1918,1915,1912,1909,1906,1903,1900,1896,1893,1890,1887,1884,1881,1878,1874,1871,1868,1865,1862,1859,1856,1852,1849,1846,1843,1840,1837,1834,1831,1827,1824,1821,1818,1815,1812,1809,1805,1802,1799,1796,1793,1790,1787,1783,1780,1777,1774,1771,1768,1765,1762,1758,1755,1752,1749,1746,1743,1740,1736,1733,1730,1727,1724,1721,1718,1714,1711,1708,1705,1702,1699,1696,1693,1689,1686,1683,1680,1677,1674,1671,1667,1664,1661,1658,1655,1652,1649,1645,1642,1639,1636,1633,1630,1627,1623,1620,1617,1614,1611,1608,1605,1602,1598,1595,1592,1589,1586,1583,1580,1576,1573,1570,1567,1564,1561,1558,1554,1551,1548,1545,1542,1539,1536,1532,1529,1526,1523,1520,1517,1514,1511,1507,1504,1501,1498,1495,1492,1489,1485,1482,1479,1476,1473,1470,1467,1463,1460,1457,1454,1451,1448,1445,1441,1438,1435,1432,1429,1426,1423,1420,1416,1413,1410,1407,1404,1401,1398,1394,1391,1388,1385,1382,1379,1376,1372,1369,1366,1363,1360,1357,1354,1350,1347,1344,1341,1338,1335,1332,1328,1325,1322,1319,1316,1313,1310,1307,1303,1300,1297,1294,1291,1288,1285,1281,1278,1275,1272,1269,1266,1263,1259,1256,1253,1250,1247,1244,1241,1237,1234,1231,1228,1225,1222,1219,1215,1212,1209,1206,1203,1200,1197,1194,1190,1187,1184,1181,1178,1175,1172,1168,1165,1162,1159,1156,1153,1150,1146,1143,1140,1137,1134,1131,1128,1124,1121,1118,1115,1112,1109,1106,1102,1099,1096,1093,1090,1087,1084,1080,1077,1074,1071,1068,1065,1062,1059,1055,1052,1049,1046,1043,1040,1037,1033,1030,1027,1024,1021,1018,1015,1011,1008,1005,1002,999,996,993,989,986,983,980,977,974,971,967,964,961,958,955,952,949,945,942,939,936,933,930,927,923,920,917,914,911,908,905,901,898,895,892,889,886,883,880,876,873,870,867,864,861,858,854,851,848,845,842,839,836,832,829,826,823,820,817,814,810,807,804,801,798,795,792,788,785,782,779,776,773,770,766,763,760,757,754,751,748,744,741,738,735,732,729,726,722,719,716,713,710,707,704,701,697,694,691,688,685,682,679,675,672,669,666,663,660,657,653,650,647,644,641,638,635,631,628,625,622,619,616,613,609,606,603,600,597,594,591,587,584,581,578,575,572,569,565,562,559,556,553,550,547,543,540,537,534,531,528,525,521,518,515,512,509,506,503,499,496,493,490,487,484,481,477,474,471,468,465,462,459,456,452,449,446,443,440,437,434,430,427,424,421,418,415,412,408,405,402,399,396,393,390,386,383,380,377,374,371,368,364,361,358,355,352,349,346,342,339,336,333,330,327,324,320,317,314,311,308,305,302,298,295,292,289,286,283,280,276,273,270,267,264,261,258,254,251,248,245,242,239,236,232,229,226,223,220,217,214,210,207,204,201,198,195,192,188,185,182,179,176,173,170,166,163,160,157,154,151,148,145,141,138,135,132,129,126,123,119,116,113,110,107,104,101,97,94,91,88,85,82,79,75,72,69,66,63,60,57,53,50,47,44,41,38,35,31,28,25,22,19,16,13,9,6,3,0,-3,-6,-9,-13,-16,-19,-22,-25,-28,-31,-35,-38,-41,-44,-47,-50,-53,-57,-60,-63,-66,-69,-72,-75,-79,-82,-85,-88,-91,-94,-97,-101,-104,-107,-110,-113,-116,-119,-123,-126,-129,-132,-135,-138,-141,-145,-148,-151,-154,-157,-160,-163,-166,-170,-173,-176,-179,-182,-185,-188,-192,-195,-198,-201,-204,-207,-210,-214,-217,-220,-223,-226,-229,-232,-236,-239,-242,-245,-248,-251,-254,-258,-261,-264,-267,-270,-273,-276,-280,-283,-286,-289,-292,-295,-298,-302,-305,-308,-311,-314,-317,-320,-324,-327,-330,-333,-336,-339,-342,-346,-349,-352,-355,-358,-361,-364,-368,-371,-374,-377,-380,-383,-386,-390,-393,-396,-399,-402,-405,-408,-412,-415,-418,-421,-424,-427,-430,-434,-437,-440,-443,-446,-449,-452,-456,-459,-462,-465,-468,-471,-474,-477,-481,-484,-487,-490,-493,-496,-499,-503,-506,-509,-512,-515,-518,-521,-525,-528,-531,-534,-537,-540,-543,-547,-550,-553,-556,-559,-562,-565,-569,-572,-575,-578,-581,-584,-587,-591,-594,-597,-600,-603,-606,-609,-613,-616,-619,-622,-625,-628,-631,-635,-638,-641,-644,-647,-650,-653,-657,-660,-663,-666,-669,-672,-675,-679,-682,-685,-688,-691,-694,-697,-701,-704,-707,-710,-713,-716,-719,-722,-726,-729,-732,-735,-738,-741,-744,-748,-751,-754,-757,-760,-763,-766,-770,-773,-776,-779,-782,-785,-788,-792,-795,-798,-801,-804,-807,-810,-814,-817,-820,-823,-826,-829,-832,-836,-839,-842,-845,-848,-851,-854,-858,-861,-864,-867,-870,-873,-876,-880,-883,-886,-889,-892,-895,-898,-901,-905,-908,-911,-914,-917,-920,-923,-927,-930,-933,-936,-939,-942,-945,-949,-952,-955,-958,-961,-964,-967,-971,-974,-977,-980,-983,-986,-989,-993,-996,-999,-1002,-1005,-1008,-1011,-1015,-1018,-1021,-1024,-1027,-1030,-1033,-1037,-1040,-1043,-1046,-1049,-1052,-1055,-1059,-1062,-1065,-1068,-1071,-1074,-1077,-1080,-1084,-1087,-1090,-1093,-1096,-1099,-1102,-1106,-1109,-1112,-1115,-1118,-1121,-1124,-1128,-1131,-1134,-1137,-1140,-1143,-1146,-1150,-1153,-1156,-1159,-1162,-1165,-1168,-1172,-1175,-1178,-1181,-1184,-1187,-1190,-1194,-1197,-1200,-1203,-1206,-1209,-1212,-1215,-1219,-1222,-1225,-1228,-1231,-1234,-1237,-1241,-1244,-1247,-1250,-1253,-1256,-1259,-1263,-1266,-1269,-1272,-1275,-1278,-1281,-1285,-1288,-1291,-1294,-1297,-1300,-1303,-1307,-1310,-1313,-1316,-1319,-1322,-1325,-1328,-1332,-1335,-1338,-1341,-1344,-1347,-1350,-1354,-1357,-1360,-1363,-1366,-1369,-1372,-1376,-1379,-1382,-1385,-1388,-1391,-1394,-1398,-1401,-1404,-1407,-1410,-1413,-1416,-1420,-1423,-1426,-1429,-1432,-1435,-1438,-1441,-1445,-1448,-1451,-1454,-1457,-1460,-1463,-1467,-1470,-1473,-1476,-1479,-1482,-1485,-1489,-1492,-1495,-1498,-1501,-1504,-1507,-1511,-1514,-1517,-1520,-1523,-1526,-1529,-1532,-1536,-1539,-1542,-1545,-1548,-1551,-1554,-1558,-1561,-1564,-1567,-1570,-1573,-1576,-1580,-1583,-1586,-1589,-1592,-1595,-1598,-1602,-1605,-1608,-1611,-1614,-1617,-1620,-1623,-1627,-1630,-1633,-1636,-1639,-1642,-1645,-1649,-1652,-1655,-1658,-1661,-1664,-1667,-1671,-1674,-1677,-1680,-1683,-1686,-1689,-1693,-1696,-1699,-1702,-1705,-1708,-1711,-1714,-1718,-1721,-1724,-1727,-1730,-1733,-1736,-1740,-1743,-1746,-1749,-1752,-1755,-1758,-1762,-1765,-1768,-1771,-1774,-1777,-1780,-1783,-1787,-1790,-1793,-1796,-1799,-1802,-1805,-1809,-1812,-1815,-1818,-1821,-1824,-1827,-1831,-1834,-1837,-1840,-1843,-1846,-1849,-1852,-1856,-1859,-1862,-1865,-1868,-1871,-1874,-1878,-1881,-1884,-1887,-1890,-1893,-1896,-1900,-1903,-1906,-1909,-1912,-1915,-1918,-1921,-1925,-1928,-1931,-1934,-1937,-1940,-1943,-1947,-1950,-1953,-1956,-1959,-1962,-1965,-1969,-1972,-1975,-1978,-1981,-1984,-1987,-1990,-1994,-1997,-2000,-2003,-2006,-2009,-2012,-2016,-2019,-2022,-2025,-2028,-2031,-2034,-2038,-2041,-2044,-2047,-2050,-2053,-2056,-2059,-2063,-2066,-2069,-2072,-2075,-2078,-2081,-2085,-2088,-2091,-2094,-2097,-2100,-2103,-2106,-2110,-2113,-2116,-2119,-2122,-2125,-2128,-2132,-2135,-2138,-2141,-2144,-2147,-2150,-2154,-2157,-2160,-2163,-2166,-2169,-2172,-2175,-2179,-2182,-2185,-2188,-2191,-2194,-2197,-2201,-2204,-2207,-2210,-2213,-2216,-2219,-2222,-2226,-2229,-2232,-2235,-2238,-2241,-2244,-2248,-2251,-2254,-2257,-2260,-2263,-2266,-2269,-2273,-2276,-2279,-2282,-2285,-2288,-2291,-2295,-2298,-2301,-2304,-2307,-2310,-2313,-2316,-2320,-2323,-2326,-2329,-2332,-2335,-2338,-2342,-2345,-2348,-2351,-2354,-2357,-2360,-2363,-2367,-2370,-2373,-2376,-2379,-2382,-2385,-2389,-2392,-2395,-2398,-2401,-2404,-2407,-2410,-2414,-2417,-2420,-2423,-2426,-2429,-2432,-2436,-2439,-2442,-2445,-2448,-2451,-2454,-2457,-2461,-2464,-2467,-2470,-2473,-2476,-2479,-2483,-2486,-2489,-2492,-2495,-2498,-2501,-2504,-2508,-2511,-2514,-2517,-2520,-2523,-2526,-2530,-2533,-2536,-2539,-2542,-2545,-2548,-2551,-2555,-2558,-2561,-2564,-2567,-2570,-2573,-2577,-2580,-2583,-2586,-2589,-2592,-2595,-2598,-2602,-2605,-2608,-2611,-2614,-2617,-2620,-2623,-2627,-2630,-2633,-2636,-2639,-2642,-2645,-2649,-2652,-2655,-2658,-2661,-2664,-2667,-2670,-2674,-2677,-2680,-2683,-2686,-2689,-2692,-2695,-2699,-2702,-2705,-2708,-2711,-2714,-2717,-2721,-2724,-2727,-2730,-2733,-2736,-2739,-2742,-2746,-2749,-2752,-2755,-2758,-2761,-2764,-2767,-2771,-2774,-2777,-2780,-2783,-2786,-2789,-2793,-2796,-2799,-2802,-2805,-2808,-2811,-2814,-2818,-2821,-2824,-2827,-2830,-2833,-2836,-2839,-2843,-2846,-2849,-2852,-2855,-2858,-2861,-2865,-2868,-2871,-2874,-2877,-2880,-2883,-2886,-2890,-2893,-2896,-2899,-2902,-2905,-2908,-2911,-2915,-2918,-2921,-2924,-2927,-2930,-2933,-2936,-2940,-2943,-2946,-2949,-2952,-2955,-2958,-2962,-2965,-2968,-2971,-2974,-2977,-2980,-2983,-2987,-2990,-2993,-2996,-2999,-3002,-3005,-3008,-3012,-3015,-3018,-3021,-3024,-3027,-3030,-3033,-3037,-3040,-3043,-3046,-3049,-3052,-3055,-3059,-3062,-3065,-3068,-3071,-3074,-3077,-3080,-3084,-3087,-3090,-3093,-3096,-3099,-3102,-3105,-3109,-3112,-3115,-3118,-3121,-3124,-3127,-3130,-3134,-3137,-3140,-3143,-3146,-3149,-3152,-3155,-3159,-3162,-3165,-3168,-3171,-3174,-3177,-3180,-3184,-3187,-3190,-3193,-3196,-3199,-3202,-3205,-3209,-3212,-3215,-3218,-3221,-3224,-3227,-3230,-3234,-3237,-3240,-3243,-3246,-3249,-3252,-3255,-3259,-3262,-3265,-3268,-3271,-3274,-3277,-3281,-3284,-3287,-3290,-3293,-3296,-3299,-3302,-3306,-3309,-3312,-3315,-3318,-3321,-3324,-3327,-3331,-3334,-3337,-3340,-3343,-3346,-3349,-3352,-3356,-3359,-3362,-3365,-3368,-3371,-3374,-3377,-3381,-3384,-3387,-3390,-3393,-3396,-3399,-3402,-3406,-3409,-3412,-3415,-3418,-3421,-3424,-3427,-3430,-3434,-3437,-3440,-3443,-3446,-3449,-3452,-3455,-3459,-3462,-3465,-3468,-3471,-3474,-3477,-3480,-3484,-3487,-3490,-3493,-3496,-3499,-3502,-3505,-3509,-3512,-3515,-3518,-3521,-3524,-3527,-3530,-3534,-3537,-3540,-3543,-3546,-3549,-3552,-3555,-3559,-3562,-3565,-3568,-3571,-3574,-3577,-3580,-3584,-3587,-3590,-3593,-3596,-3599,-3602,-3605,-3609,-3612,-3615,-3618,-3621,-3624,-3627,-3630,-3634,-3637,-3640,-3643,-3646,-3649,-3652,-3655,-3658,-3662,-3665,-3668,-3671,-3674,-3677,-3680,-3683,-3687,-3690,-3693,-3696,-3699,-3702,-3705,-3708,-3712,-3715,-3718,-3721,-3724,-3727,-3730,-3733,-3737,-3740,-3743,-3746,-3749,-3752,-3755,-3758,-3761,-3765,-3768,-3771,-3774,-3777,-3780,-3783,-3786,-3790,-3793,-3796,-3799,-3802,-3805,-3808,-3811,-3815,-3818,-3821,-3824,-3827,-3830,-3833,-3836,-3839,-3843,-3846,-3849,-3852,-3855,-3858,-3861,-3864,-3868,-3871,-3874,-3877,-3880,-3883,-3886,-3889,-3893,-3896,-3899,-3902,-3905,-3908,-3911,-3914,-3917,-3921,-3924,-3927,-3930,-3933,-3936,-3939,-3942,-3946,-3949,-3952,-3955,-3958,-3961,-3964,-3967,-3970,-3974,-3977,-3980,-3983,-3986,-3989,-3992,-3995,-3999,-4002,-4005,-4008,-4011,-4014,-4017,-4020,-4024,-4027,-4030,-4033,-4036,-4039,-4042,-4045,-4048,-4052,-4055,-4058,-4061,-4064,-4067,-4070,-4073,-4076,-4080,-4083,-4086,-4089,-4092,-4095,-4098,-4101,-4105,-4108,-4111,-4114,-4117,-4120,-4123,-4126,-4129,-4133,-4136,-4139,-4142,-4145,-4148,-4151,-4154,-4158,-4161,-4164,-4167,-4170,-4173,-4176,-4179,-4182,-4186,-4189,-4192,-4195,-4198,-4201,-4204,-4207,-4210,-4214,-4217,-4220,-4223,-4226,-4229,-4232,-4235,-4239,-4242,-4245,-4248,-4251,-4254,-4257,-4260,-4263,-4267,-4270,-4273,-4276,-4279,-4282,-4285,-4288,-4291,-4295,-4298,-4301,-4304,-4307,-4310,-4313,-4316,-4320,-4323,-4326,-4329,-4332,-4335,-4338,-4341,-4344,-4348,-4351,-4354,-4357,-4360,-4363,-4366,-4369,-4372,-4376,-4379,-4382,-4385,-4388,-4391,-4394,-4397,-4400,-4404,-4407,-4410,-4413,-4416,-4419,-4422,-4425,-4428,-4432,-4435,-4438,-4441,-4444,-4447,-4450,-4453,-4456,-4460,-4463,-4466,-4469,-4472,-4475,-4478,-4481,-4485,-4488,-4491,-4494,-4497,-4500,-4503,-4506,-4509,-4513,-4516,-4519,-4522,-4525,-4528,-4531,-4534,-4537,-4541,-4544,-4547,-4550,-4553,-4556,-4559,-4562,-4565,-4569,-4572,-4575,-4578,-4581,-4584,-4587,-4590,-4593,-4597,-4600,-4603,-4606,-4609,-4612,-4615,-4618,-4621,-4624,-4628,-4631,-4634,-4637,-4640,-4643,-4646,-4649,-4652,-4656,-4659,-4662,-4665,-4668,-4671,-4674,-4677,-4680,-4684,-4687,-4690,-4693,-4696,-4699,-4702,-4705,-4708,-4712,-4715,-4718,-4721,-4724,-4727,-4730,-4733,-4736,-4740,-4743,-4746,-4749,-4752,-4755,-4758,-4761,-4764,-4768,-4771,-4774,-4777,-4780,-4783,-4786,-4789,-4792,-4795,-4799,-4802,-4805,-4808,-4811,-4814,-4817,-4820,-4823,-4827,-4830,-4833,-4836,-4839,-4842,-4845,-4848,-4851,-4855,-4858,-4861,-4864,-4867,-4870,-4873,-4876,-4879,-4882,-4886,-4889,-4892,-4895,-4898,-4901,-4904,-4907,-4910,-4914,-4917,-4920,-4923,-4926,-4929,-4932,-4935,-4938,-4941,-4945,-4948,-4951,-4954,-4957,-4960,-4963,-4966,-4969,-4973,-4976,-4979,-4982,-4985,-4988,-4991,-4994,-4997,-5000,-5004,-5007,-5010,-5013,-5016,-5019,-5022,-5025,-5028,-5032,-5035,-5038,-5041,-5044,-5047,-5050,-5053,-5056,-5059,-5063,-5066,-5069,-5072,-5075,-5078,-5081,-5084,-5087,-5091,-5094,-5097,-5100,-5103,-5106,-5109,-5112,-5115,-5118,-5122,-5125,-5128,-5131,-5134,-5137,-5140,-5143,-5146,-5149,-5153,-5156,-5159,-5162,-5165,-5168,-5171,-5174,-5177,-5180,-5184,-5187,-5190,-5193,-5196,-5199,-5202,-5205,-5208,-5212,-5215,-5218,-5221,-5224,-5227,-5230,-5233,-5236,-5239,-5243,-5246,-5249,-5252,-5255,-5258,-5261,-5264,-5267,-5270,-5274,-5277,-5280,-5283,-5286,-5289,-5292,-5295,-5298,-5301,-5305,-5308,-5311,-5314,-5317,-5320,-5323,-5326,-5329,-5332,-5336,-5339,-5342,-5345,-5348,-5351,-5354,-5357,-5360,-5363,-5367,-5370,-5373,-5376,-5379,-5382,-5385,-5388,-5391,-5394,-5398,-5401,-5404,-5407,-5410,-5413,-5416,-5419,-5422,-5425,-5428,-5432,-5435,-5438,-5441,-5444,-5447,-5450,-5453,-5456,-5459,-5463,-5466,-5469,-5472,-5475,-5478,-5481,-5484,-5487,-5490,-5494,-5497,-5500,-5503,-5506,-5509,-5512,-5515,-5518,-5521,-5525,-5528,-5531,-5534,-5537,-5540,-5543,-5546,-5549,-5552,-5555,-5559,-5562,-5565,-5568,-5571,-5574,-5577,-5580,-5583,-5586,-5590,-5593,-5596,-5599,-5602,-5605,-5608,-5611,-5614,-5617,-5620,-5624,-5627,-5630,-5633,-5636,-5639,-5642,-5645,-5648,-5651,-5655,-5658,-5661,-5664,-5667,-5670,-5673,-5676,-5679,-5682,-5685,-5689,-5692,-5695,-5698,-5701,-5704,-5707,-5710,-5713,-5716,-5719,-5723,-5726,-5729,-5732,-5735,-5738,-5741,-5744,-5747,-5750,-5754,-5757,-5760,-5763,-5766,-5769,-5772,-5775,-5778,-5781,-5784,-5788,-5791,-5794,-5797,-5800,-5803,-5806,-5809,-5812,-5815,-5818,-5822,-5825,-5828,-5831,-5834,-5837,-5840,-5843,-5846,-5849,-5852,-5856,-5859,-5862,-5865,-5868,-5871,-5874,-5877,-5880,-5883,-5886,-5890,-5893,-5896,-5899,-5902,-5905,-5908,-5911,-5914,-5917,-5920,-5924,-5927,-5930,-5933,-5936,-5939,-5942,-5945,-5948,-5951,-5954,-5958,-5961,-5964,-5967,-5970,-5973,-5976,-5979,-5982,-5985,-5988,-5991,-5995,-5998,-6001,-6004,-6007,-6010,-6013,-6016,-6019,-6022,-6025,-6029,-6032,-6035,-6038,-6041,-6044,-6047,-6050,-6053,-6056,-6059,-6063,-6066,-6069,-6072,-6075,-6078,-6081,-6084,-6087,-6090,-6093,-6096,-6100,-6103,-6106,-6109,-6112,-6115,-6118,-6121,-6124,-6127,-6130,-6134,-6137,-6140,-6143,-6146,-6149,-6152,-6155,-6158,-6161,-6164,-6167,-6171,-6174,-6177,-6180,-6183,-6186,-6189,-6192,-6195,-6198,-6201,-6204,-6208,-6211,-6214,-6217,-6220,-6223,-6226,-6229,-6232,-6235,-6238,-6241,-6245,-6248,-6251,-6254,-6257,-6260,-6263,-6266,-6269,-6272,-6275,-6278,-6282,-6285,-6288,-6291,-6294,-6297,-6300,-6303,-6306,-6309,-6312,-6315,-6319,-6322,-6325,-6328,-6331,-6334,-6337,-6340,-6343,-6346,-6349,-6352,-6356,-6359,-6362,-6365,-6368,-6371,-6374,-6377,-6380,-6383,-6386,-6389,-6393,-6396,-6399,-6402,-6405,-6408,-6411,-6414,-6417,-6420,-6423,-6426,-6429,-6433,-6436,-6439,-6442,-6445,-6448,-6451,-6454,-6457,-6460,-6463,-6466,-6470,-6473,-6476,-6479,-6482,-6485,-6488,-6491,-6494,-6497,-6500,-6503,-6506,-6510,-6513,-6516,-6519,-6522,-6525,-6528,-6531,-6534,-6537,-6540,-6543,-6547,-6550,-6553,-6556,-6559,-6562,-6565,-6568,-6571,-6574,-6577,-6580,-6583,-6587,-6590,-6593,-6596,-6599,-6602,-6605,-6608,-6611,-6614,-6617,-6620,-6623,-6627,-6630,-6633,-6636,-6639,-6642,-6645,-6648,-6651,-6654,-6657,-6660,-6663,-6667,-6670,-6673,-6676,-6679,-6682,-6685,-6688,-6691,-6694,-6697,-6700,-6703,-6706,-6710,-6713,-6716,-6719,-6722,-6725,-6728,-6731,-6734,-6737,-6740,-6743,-6746,-6750,-6753,-6756,-6759,-6762,-6765,-6768,-6771,-6774,-6777,-6780,-6783,-6786,-6789,-6793,-6796,-6799,-6802,-6805,-6808,-6811,-6814,-6817,-6820,-6823,-6826,-6829,-6833,-6836,-6839,-6842,-6845,-6848,-6851,-6854,-6857,-6860,-6863,-6866,-6869,-6872,-6876,-6879,-6882,-6885,-6888,-6891,-6894,-6897,-6900,-6903,-6906,-6909,-6912,-6915,-6919,-6922,-6925,-6928,-6931,-6934,-6937,-6940,-6943,-6946,-6949,-6952,-6955,-6958,-6961,-6965,-6968,-6971,-6974,-6977,-6980,-6983,-6986,-6989,-6992,-6995,-6998,-7001,-7004,-7008,-7011,-7014,-7017,-7020,-7023,-7026,-7029,-7032,-7035,-7038,-7041,-7044,-7047,-7050,-7054,-7057,-7060,-7063,-7066,-7069,-7072,-7075,-7078,-7081,-7084,-7087,-7090,-7093,-7097,-7100,-7103,-7106,-7109,-7112,-7115,-7118,-7121,-7124,-7127,-7130,-7133,-7136,-7139,-7143,-7146,-7149,-7152,-7155,-7158,-7161,-7164,-7167,-7170,-7173,-7176,-7179,-7182,-7185,-7188,-7192,-7195,-7198,-7201,-7204,-7207,-7210,-7213,-7216,-7219,-7222,-7225,-7228,-7231,-7234,-7238,-7241,-7244,-7247,-7250,-7253,-7256,-7259,-7262,-7265,-7268,-7271,-7274,-7277,-7280,-7283,-7287,-7290,-7293,-7296,-7299,-7302,-7305,-7308,-7311,-7314,-7317,-7320,-7323,-7326,-7329,-7332,-7336,-7339,-7342,-7345,-7348,-7351,-7354,-7357,-7360,-7363,-7366,-7369,-7372,-7375,-7378,-7381,-7385,-7388,-7391,-7394,-7397,-7400,-7403,-7406,-7409,-7412,-7415,-7418,-7421,-7424,-7427,-7430,-7433,-7437,-7440,-7443,-7446,-7449,-7452,-7455,-7458,-7461,-7464,-7467,-7470,-7473,-7476,-7479,-7482,-7485,-7489,-7492,-7495,-7498,-7501,-7504,-7507,-7510,-7513,-7516,-7519,-7522,-7525,-7528,-7531,-7534,-7537,-7541,-7544,-7547,-7550,-7553,-7556,-7559,-7562,-7565,-7568,-7571,-7574,-7577,-7580,-7583,-7586,-7589,-7592,-7596,-7599,-7602,-7605,-7608,-7611,-7614,-7617,-7620,-7623,-7626,-7629,-7632,-7635,-7638,-7641,-7644,-7647,-7651,-7654,-7657,-7660,-7663,-7666,-7669,-7672,-7675,-7678,-7681,-7684,-7687,-7690,-7693,-7696,-7699,-7702,-7705,-7709,-7712,-7715,-7718,-7721,-7724,-7727,-7730,-7733,-7736,-7739,-7742,-7745,-7748,-7751,-7754,-7757,-7760,-7764,-7767,-7770,-7773,-7776,-7779,-7782,-7785,-7788,-7791,-7794,-7797,-7800,-7803,-7806,-7809,-7812,-7815,-7818,-7821,-7825,-7828,-7831,-7834,-7837,-7840,-7843,-7846,-7849,-7852,-7855,-7858,-7861,-7864,-7867,-7870,-7873,-7876,-7879,-7882,-7886,-7889,-7892,-7895,-7898,-7901,-7904,-7907,-7910,-7913,-7916,-7919,-7922,-7925,-7928,-7931,-7934,-7937,-7940,-7943,-7946,-7950,-7953,-7956,-7959,-7962,-7965,-7968,-7971,-7974,-7977,-7980,-7983,-7986,-7989,-7992,-7995,-7998,-8001,-8004,-8007,-8010,-8014,-8017,-8020,-8023,-8026,-8029,-8032,-8035,-8038,-8041,-8044,-8047,-8050,-8053,-8056,-8059,-8062,-8065,-8068,-8071,-8074,-8077,-8081,-8084,-8087,-8090,-8093,-8096,-8099,-8102,-8105,-8108,-8111,-8114,-8117,-8120,-8123,-8126,-8129,-8132,-8135,-8138,-8141,-8144,-8147,-8151,-8154,-8157,-8160,-8163,-8166,-8169,-8172,-8175,-8178,-8181,-8184,-8187,-8190,-8193,-8196,-8199,-8202,-8205,-8208,-8211,-8214,-8217,-8220,-8224,-8227,-8230,-8233,-8236,-8239,-8242,-8245,-8248,-8251,-8254,-8257,-8260,-8263,-8266,-8269,-8272,-8275,-8278,-8281,-8284,-8287,-8290,-8293,-8296,-8300,-8303,-8306,-8309,-8312,-8315,-8318,-8321,-8324,-8327,-8330,-8333,-8336,-8339,-8342,-8345,-8348,-8351,-8354,-8357,-8360,-8363,-8366,-8369,-8372,-8375,-8379,-8382,-8385,-8388,-8391,-8394,-8397,-8400,-8403,-8406,-8409,-8412,-8415,-8418,-8421,-8424,-8427,-8430,-8433,-8436,-8439,-8442,-8445,-8448,-8451,-8454,-8457,-8460,-8464,-8467,-8470,-8473,-8476,-8479,-8482,-8485,-8488,-8491,-8494,-8497,-8500,-8503,-8506,-8509,-8512,-8515,-8518,-8521,-8524,-8527,-8530,-8533,-8536,-8539,-8542,-8545,-8548,-8552,-8555,-8558,-8561,-8564,-8567,-8570,-8573,-8576,-8579,-8582,-8585,-8588,-8591,-8594,-8597,-8600,-8603,-8606,-8609,-8612,-8615,-8618,-8621,-8624,-8627,-8630,-8633,-8636,-8639,-8642,-8645,-8649,-8652,-8655,-8658,-8661,-8664,-8667,-8670,-8673,-8676,-8679,-8682,-8685,-8688,-8691,-8694,-8697,-8700,-8703,-8706,-8709,-8712,-8715,-8718,-8721,-8724,-8727,-8730,-8733,-8736,-8739,-8742,-8745,-8748,-8751,-8755,-8758,-8761,-8764,-8767,-8770,-8773,-8776,-8779,-8782,-8785,-8788,-8791,-8794,-8797,-8800,-8803,-8806,-8809,-8812,-8815,-8818,-8821,-8824,-8827,-8830,-8833,-8836,-8839,-8842,-8845,-8848,-8851,-8854,-8857,-8860,-8863,-8866,-8869,-8873,-8876,-8879,-8882,-8885,-8888,-8891,-8894,-8897,-8900,-8903,-8906,-8909,-8912,-8915,-8918,-8921,-8924,-8927,-8930,-8933,-8936,-8939,-8942,-8945,-8948,-8951,-8954,-8957,-8960,-8963,-8966,-8969,-8972,-8975,-8978,-8981,-8984,-8987,-8990,-8993,-8996,-8999,-9002,-9006,-9009,-9012,-9015,-9018,-9021,-9024,-9027,-9030,-9033,-9036,-9039,-9042,-9045,-9048,-9051,-9054,-9057,-9060,-9063,-9066,-9069,-9072,-9075,-9078,-9081,-9084,-9087,-9090,-9093,-9096,-9099,-9102,-9105,-9108,-9111,-9114,-9117,-9120,-9123,-9126,-9129,-9132,-9135,-9138,-9141,-9144,-9147,-9150,-9153,-9156,-9159,-9162,-9165,-9168,-9172,-9175,-9178,-9181,-9184,-9187,-9190,-9193,-9196,-9199,-9202,-9205,-9208,-9211,-9214,-9217,-9220,-9223,-9226,-9229,-9232,-9235,-9238,-9241,-9244,-9247,-9250,-9253,-9256,-9259,-9262,-9265,-9268,-9271,-9274,-9277,-9280,-9283,-9286,-9289,-9292,-9295,-9298,-9301,-9304,-9307,-9310,-9313,-9316,-9319,-9322,-9325,-9328,-9331,-9334,-9337,-9340,-9343,-9346,-9349,-9352,-9355,-9358,-9361,-9364,-9367,-9370,-9373,-9376,-9379,-9382,-9385,-9388,-9391,-9394,-9397,-9400,-9403,-9406,-9409,-9413,-9416,-9419,-9422,-9425,-9428,-9431,-9434,-9437,-9440,-9443,-9446,-9449,-9452,-9455,-9458,-9461,-9464,-9467,-9470,-9473,-9476,-9479,-9482,-9485,-9488,-9491,-9494,-9497,-9500,-9503,-9506,-9509,-9512,-9515,-9518,-9521,-9524,-9527,-9530,-9533,-9536,-9539,-9542,-9545,-9548,-9551,-9554,-9557,-9560,-9563,-9566,-9569,-9572,-9575,-9578,-9581,-9584,-9587,-9590,-9593,-9596,-9599,-9602,-9605,-9608,-9611,-9614,-9617,-9620,-9623,-9626,-9629,-9632,-9635,-9638,-9641,-9644,-9647,-9650,-9653,-9656,-9659,-9662,-9665,-9668,-9671,-9674,-9677,-9680,-9683,-9686,-9689,-9692,-9695,-9698,-9701,-9704,-9707,-9710,-9713,-9716,-9719,-9722,-9725,-9728,-9731,-9734,-9737,-9740,-9743,-9746,-9749,-9752,-9755,-9758,-9761,-9764,-9767,-9770,-9773,-9776,-9779,-9782,-9785,-9788,-9791,-9794,-9797,-9800,-9803,-9806,-9809,-9812,-9815,-9818,-9821,-9824,-9827,-9830,-9833,-9836,-9839,-9842,-9845,-9848,-9851,-9854,-9857,-9860,-9863,-9866,-9869,-9872,-9875,-9878,-9881,-9884,-9887,-9890,-9893,-9896,-9899,-9902,-9905,-9908,-9911,-9914,-9917,-9920,-9923,-9926,-9929,-9932,-9935,-9938,-9941,-9944,-9947,-9950,-9953,-9956,-9959,-9962,-9965,-9968,-9971,-9974,-9977,-9980,-9983,-9986,-9989,-9992,-9995,-9998,-10001,-10004,-10007,-10010,-10013,-10016,-10019,-10022,-10025,-10028,-10031,-10033,-10036,-10039,-10042,-10045,-10048,-10051,-10054,-10057,-10060,-10063,-10066,-10069,-10072,-10075,-10078,-10081,-10084,-10087,-10090,-10093,-10096,-10099,-10102,-10105,-10108,-10111,-10114,-10117,-10120,-10123,-10126,-10129,-10132,-10135,-10138,-10141,-10144,-10147,-10150,-10153,-10156,-10159,-10162,-10165,-10168,-10171,-10174,-10177,-10180,-10183,-10186,-10189,-10192,-10195,-10198,-10201,-10204,-10207,-10210,-10213,-10216,-10219,-10222,-10225,-10228,-10231,-10234,-10237,-10240,-10243,-10246,-10249,-10252,-10255,-10258,-10261,-10263,-10266,-10269,-10272,-10275,-10278,-10281,-10284,-10287,-10290,-10293,-10296,-10299,-10302,-10305,-10308,-10311,-10314,-10317,-10320,-10323,-10326,-10329,-10332,-10335,-10338,-10341,-10344,-10347,-10350,-10353,-10356,-10359,-10362,-10365,-10368,-10371,-10374,-10377,-10380,-10383,-10386,-10389,-10392,-10395,-10398,-10401,-10404,-10407,-10410,-10413,-10416,-10419,-10421,-10424,-10427,-10430,-10433,-10436,-10439,-10442,-10445,-10448,-10451,-10454,-10457,-10460,-10463,-10466,-10469,-10472,-10475,-10478,-10481,-10484,-10487,-10490,-10493,-10496,-10499,-10502,-10505,-10508,-10511,-10514,-10517,-10520,-10523,-10526,-10529,-10532,-10535,-10538,-10541,-10544,-10546,-10549,-10552,-10555,-10558,-10561,-10564,-10567,-10570,-10573,-10576,-10579,-10582,-10585,-10588,-10591,-10594,-10597,-10600,-10603,-10606,-10609,-10612,-10615,-10618,-10621,-10624,-10627,-10630,-10633,-10636,-10639,-10642,-10645,-10648,-10651,-10654,-10656,-10659,-10662,-10665,-10668,-10671,-10674,-10677,-10680,-10683,-10686,-10689,-10692,-10695,-10698,-10701,-10704,-10707,-10710,-10713,-10716,-10719,-10722,-10725,-10728,-10731,-10734,-10737,-10740,-10743,-10746,-10749,-10751,-10754,-10757,-10760,-10763,-10766,-10769,-10772,-10775,-10778,-10781,-10784,-10787,-10790,-10793,-10796,-10799,-10802,-10805,-10808,-10811,-10814,-10817,-10820,-10823,-10826,-10829,-10832,-10835,-10838,-10840,-10843,-10846,-10849,-10852,-10855,-10858,-10861,-10864,-10867,-10870,-10873,-10876,-10879,-10882,-10885,-10888,-10891,-10894,-10897,-10900,-10903,-10906,-10909,-10912,-10915,-10918,-10920,-10923,-10926,-10929,-10932,-10935,-10938,-10941,-10944,-10947,-10950,-10953,-10956,-10959,-10962,-10965,-10968,-10971,-10974,-10977,-10980,-10983,-10986,-10989,-10992,-10994,-10997,-11000,-11003,-11006,-11009,-11012,-11015,-11018,-11021,-11024,-11027,-11030,-11033,-11036,-11039,-11042,-11045,-11048,-11051,-11054,-11057,-11060,-11063,-11065,-11068,-11071,-11074,-11077,-11080,-11083,-11086,-11089,-11092,-11095,-11098,-11101,-11104,-11107,-11110,-11113,-11116,-11119,-11122,-11125,-11128,-11131,-11133,-11136,-11139,-11142,-11145,-11148,-11151,-11154,-11157,-11160,-11163,-11166,-11169,-11172,-11175,-11178,-11181,-11184,-11187,-11190,-11193,-11195,-11198,-11201,-11204,-11207,-11210,-11213,-11216,-11219,-11222,-11225,-11228,-11231,-11234,-11237,-11240,-11243,-11246,-11249,-11252,-11255,-11257,-11260,-11263,-11266,-11269,-11272,-11275,-11278,-11281,-11284,-11287,-11290,-11293,-11296,-11299,-11302,-11305,-11308,-11311,-11314,-11316,-11319,-11322,-11325,-11328,-11331,-11334,-11337,-11340,-11343,-11346,-11349,-11352,-11355,-11358,-11361,-11364,-11367,-11370,-11372,-11375,-11378,-11381,-11384,-11387,-11390,-11393,-11396,-11399,-11402,-11405,-11408,-11411,-11414,-11417,-11420,-11423,-11425,-11428,-11431,-11434,-11437,-11440,-11443,-11446,-11449,-11452,-11455,-11458,-11461,-11464,-11467,-11470,-11473,-11476,-11478,-11481,-11484,-11487,-11490,-11493,-11496,-11499,-11502,-11505,-11508,-11511,-11514,-11517,-11520,-11523,-11526,-11528,-11531,-11534,-11537,-11540,-11543,-11546,-11549,-11552,-11555,-11558,-11561,-11564,-11567,-11570,-11573,-11575,-11578,-11581,-11584,-11587,-11590,-11593,-11596,-11599,-11602,-11605,-11608,-11611,-11614,-11617,-11620,-11623,-11625,-11628,-11631,-11634,-11637,-11640,-11643,-11646,-11649,-11652,-11655,-11658,-11661,-11664,-11667,-11669,-11672,-11675,-11678,-11681,-11684,-11687,-11690,-11693,-11696,-11699,-11702,-11705,-11708,-11711,-11714,-11716,-11719,-11722,-11725,-11728,-11731,-11734,-11737,-11740,-11743,-11746,-11749,-11752,-11755,-11758,-11760,-11763,-11766,-11769,-11772,-11775,-11778,-11781,-11784,-11787,-11790,-11793,-11796,-11799,-11801,-11804,-11807,-11810,-11813,-11816,-11819,-11822,-11825,-11828,-11831,-11834,-11837,-11840,-11842,-11845,-11848,-11851,-11854,-11857,-11860,-11863,-11866,-11869,-11872,-11875,-11878,-11881,-11883,-11886,-11889,-11892,-11895,-11898,-11901,-11904,-11907,-11910,-11913,-11916,-11919,-11922,-11924,-11927,-11930,-11933,-11936,-11939,-11942,-11945,-11948,-11951,-11954,-11957,-11960,-11962,-11965,-11968,-11971,-11974,-11977,-11980,-11983,-11986,-11989,-11992,-11995,-11998,-12001,-12003,-12006,-12009,-12012,-12015,-12018,-12021,-12024,-12027,-12030,-12033,-12036,-12038,-12041,-12044,-12047,-12050,-12053,-12056,-12059,-12062,-12065,-12068,-12071,-12074,-12076,-12079,-12082,-12085,-12088,-12091,-12094,-12097,-12100,-12103,-12106,-12109,-12112,-12114,-12117,-12120,-12123,-12126,-12129,-12132,-12135,-12138,-12141,-12144,-12147,-12149,-12152,-12155,-12158,-12161,-12164,-12167,-12170,-12173,-12176,-12179,-12182,-12184,-12187,-12190,-12193,-12196,-12199,-12202,-12205,-12208,-12211,-12214,-12217,-12219,-12222,-12225,-12228,-12231,-12234,-12237,-12240,-12243,-12246,-12249,-12251,-12254,-12257,-12260,-12263,-12266,-12269,-12272,-12275,-12278,-12281,-12284,-12286,-12289,-12292,-12295,-12298,-12301,-12304,-12307,-12310,-12313,-12316,-12318,-12321,-12324,-12327,-12330,-12333,-12336,-12339,-12342,-12345,-12348,-12350,-12353,-12356,-12359,-12362,-12365,-12368,-12371,-12374,-12377,-12380,-12382,-12385,-12388,-12391,-12394,-12397,-12400,-12403,-12406,-12409,-12412,-12414,-12417,-12420,-12423,-12426,-12429,-12432,-12435,-12438,-12441,-12444,-12446,-12449,-12452,-12455,-12458,-12461,-12464,-12467,-12470,-12473,-12476,-12478,-12481,-12484,-12487,-12490,-12493,-12496,-12499,-12502,-12505,-12507,-12510,-12513,-12516,-12519,-12522,-12525,-12528,-12531,-12534,-12536,-12539,-12542,-12545,-12548,-12551,-12554,-12557,-12560,-12563,-12566,-12568,-12571,-12574,-12577,-12580,-12583,-12586,-12589,-12592,-12595,-12597,-12600,-12603,-12606,-12609,-12612,-12615,-12618,-12621,-12624,-12626,-12629,-12632,-12635,-12638,-12641,-12644,-12647,-12650,-12652,-12655,-12658,-12661,-12664,-12667,-12670,-12673,-12676,-12679,-12681,-12684,-12687,-12690,-12693,-12696,-12699,-12702,-12705,-12708,-12710,-12713,-12716,-12719,-12722,-12725,-12728,-12731,-12734,-12736,-12739,-12742,-12745,-12748,-12751,-12754,-12757,-12760,-12763,-12765,-12768,-12771,-12774,-12777,-12780,-12783,-12786,-12789,-12791,-12794,-12797,-12800,-12803,-12806,-12809,-12812,-12815,-12817,-12820,-12823,-12826,-12829,-12832,-12835,-12838,-12841,-12843,-12846,-12849,-12852,-12855,-12858,-12861,-12864,-12867,-12870,-12872,-12875,-12878,-12881,-12884,-12887,-12890,-12893,-12895,-12898,-12901,-12904,-12907,-12910,-12913,-12916,-12919,-12921,-12924,-12927,-12930,-12933,-12936,-12939,-12942,-12945,-12947,-12950,-12953,-12956,-12959,-12962,-12965,-12968,-12971,-12973,-12976,-12979,-12982,-12985,-12988,-12991,-12994,-12997,-12999,-13002,-13005,-13008,-13011,-13014,-13017,-13020,-13022,-13025,-13028,-13031,-13034,-13037,-13040,-13043,-13046,-13048,-13051,-13054,-13057,-13060,-13063,-13066,-13069,-13071,-13074,-13077,-13080,-13083,-13086,-13089,-13092,-13094,-13097,-13100,-13103,-13106,-13109,-13112,-13115,-13118,-13120,-13123,-13126,-13129,-13132,-13135,-13138,-13141,-13143,-13146,-13149,-13152,-13155,-13158,-13161,-13164,-13166,-13169,-13172,-13175,-13178,-13181,-13184,-13187,-13189,-13192,-13195,-13198,-13201,-13204,-13207,-13210,-13212,-13215,-13218,-13221,-13224,-13227,-13230,-13233,-13235,-13238,-13241,-13244,-13247,-13250,-13253,-13256,-13258,-13261,-13264,-13267,-13270,-13273,-13276,-13279,-13281,-13284,-13287,-13290,-13293,-13296,-13299,-13302,-13304,-13307,-13310,-13313,-13316,-13319,-13322,-13324,-13327,-13330,-13333,-13336,-13339,-13342,-13345,-13347,-13350,-13353,-13356,-13359,-13362,-13365,-13368,-13370,-13373,-13376,-13379,-13382,-13385,-13388,-13390,-13393,-13396,-13399,-13402,-13405,-13408,-13411,-13413,-13416,-13419,-13422,-13425,-13428,-13431,-13433,-13436,-13439,-13442,-13445,-13448,-13451,-13454,-13456,-13459,-13462,-13465,-13468,-13471,-13474,-13476,-13479,-13482,-13485,-13488,-13491,-13494,-13496,-13499,-13502,-13505,-13508,-13511,-13514,-13516,-13519,-13522,-13525,-13528,-13531,-13534,-13537,-13539,-13542,-13545,-13548,-13551,-13554,-13557,-13559,-13562,-13565,-13568,-13571,-13574,-13577,-13579,-13582,-13585,-13588,-13591,-13594,-13597,-13599,-13602,-13605,-13608,-13611,-13614,-13617,-13619,-13622,-13625,-13628,-13631,-13634,-13637,-13639,-13642,-13645,-13648,-13651,-13654,-13657,-13659,-13662,-13665,-13668,-13671,-13674,-13677,-13679,-13682,-13685,-13688,-13691,-13694,-13697,-13699,-13702,-13705,-13708,-13711,-13714,-13717,-13719,-13722,-13725,-13728,-13731,-13734,-13736,-13739,-13742,-13745,-13748,-13751,-13754,-13756,-13759,-13762,-13765,-13768,-13771,-13774,-13776,-13779,-13782,-13785,-13788,-13791,-13793,-13796,-13799,-13802,-13805,-13808,-13811,-13813,-13816,-13819,-13822,-13825,-13828,-13831,-13833,-13836,-13839,-13842,-13845,-13848,-13850,-13853,-13856,-13859,-13862,-13865,-13868,-13870,-13873,-13876,-13879,-13882,-13885,-13887,-13890,-13893,-13896,-13899,-13902,-13905,-13907,-13910,-13913,-13916,-13919,-13922,-13924,-13927,-13930,-13933,-13936,-13939,-13942,-13944,-13947,-13950,-13953,-13956,-13959,-13961,-13964,-13967,-13970,-13973,-13976,-13978,-13981,-13984,-13987,-13990,-13993,-13995,-13998,-14001,-14004,-14007,-14010,-14013,-14015,-14018,-14021,-14024,-14027,-14030,-14032,-14035,-14038,-14041,-14044,-14047,-14049,-14052,-14055,-14058,-14061,-14064,-14066,-14069,-14072,-14075,-14078,-14081,-14083,-14086,-14089,-14092,-14095,-14098,-14101,-14103,-14106,-14109,-14112,-14115,-14118,-14120,-14123,-14126,-14129,-14132,-14135,-14137,-14140,-14143,-14146,-14149,-14152,-14154,-14157,-14160,-14163,-14166,-14169,-14171,-14174,-14177,-14180,-14183,-14186,-14188,-14191,-14194,-14197,-14200,-14203,-14205,-14208,-14211,-14214,-14217,-14219,-14222,-14225,-14228,-14231,-14234,-14236,-14239,-14242,-14245,-14248,-14251,-14253,-14256,-14259,-14262,-14265,-14268,-14270,-14273,-14276,-14279,-14282,-14285,-14287,-14290,-14293,-14296,-14299,-14302,-14304,-14307,-14310,-14313,-14316,-14318,-14321,-14324,-14327,-14330,-14333,-14335,-14338,-14341,-14344,-14347,-14350,-14352,-14355,-14358,-14361,-14364,-14366,-14369,-14372,-14375,-14378,-14381,-14383,-14386,-14389,-14392,-14395,-14398,-14400,-14403,-14406,-14409,-14412,-14414,-14417,-14420,-14423,-14426,-14429,-14431,-14434,-14437,-14440,-14443,-14445,-14448,-14451,-14454,-14457,-14460,-14462,-14465,-14468,-14471,-14474,-14477,-14479,-14482,-14485,-14488,-14491,-14493,-14496,-14499,-14502,-14505,-14507,-14510,-14513,-14516,-14519,-14522,-14524,-14527,-14530,-14533,-14536,-14538,-14541,-14544,-14547,-14550,-14553,-14555,-14558,-14561,-14564,-14567,-14569,-14572,-14575,-14578,-14581,-14584,-14586,-14589,-14592,-14595,-14598,-14600,-14603,-14606,-14609,-14612,-14614,-14617,-14620,-14623,-14626,-14628,-14631,-14634,-14637,-14640,-14643,-14645,-14648,-14651,-14654,-14657,-14659,-14662,-14665,-14668,-14671,-14673,-14676,-14679,-14682,-14685,-14688,-14690,-14693,-14696,-14699,-14702,-14704,-14707,-14710,-14713,-14716,-14718,-14721,-14724,-14727,-14730,-14732,-14735,-14738,-14741,-14744,-14746,-14749,-14752,-14755,-14758,-14760,-14763,-14766,-14769,-14772,-14774,-14777,-14780,-14783,-14786,-14789,-14791,-14794,-14797,-14800,-14803,-14805,-14808,-14811,-14814,-14817,-14819,-14822,-14825,-14828,-14831,-14833,-14836,-14839,-14842,-14845,-14847,-14850,-14853,-14856,-14859,-14861,-14864,-14867,-14870,-14873,-14875,-14878,-14881,-14884,-14887,-14889,-14892,-14895,-14898,-14901,-14903,-14906,-14909,-14912,-14915,-14917,-14920,-14923,-14926,-14929,-14931,-14934,-14937,-14940,-14942,-14945,-14948,-14951,-14954,-14956,-14959,-14962,-14965,-14968,-14970,-14973,-14976,-14979,-14982,-14984,-14987,-14990,-14993,-14996,-14998,-15001,-15004,-15007,-15010,-15012,-15015,-15018,-15021,-15024,-15026,-15029,-15032,-15035,-15037,-15040,-15043,-15046,-15049,-15051,-15054,-15057,-15060,-15063,-15065,-15068,-15071,-15074,-15077,-15079,-15082,-15085,-15088,-15090,-15093,-15096,-15099,-15102,-15104,-15107,-15110,-15113,-15116,-15118,-15121,-15124,-15127,-15129,-15132,-15135,-15138,-15141,-15143,-15146,-15149,-15152,-15155,-15157,-15160,-15163,-15166,-15168,-15171,-15174,-15177,-15180,-15182,-15185,-15188,-15191,-15194,-15196,-15199,-15202,-15205,-15207,-15210,-15213,-15216,-15219,-15221,-15224,-15227,-15230,-15233,-15235,-15238,-15241,-15244,-15246,-15249,-15252,-15255,-15258,-15260,-15263,-15266,-15269,-15271,-15274,-15277,-15280,-15283,-15285,-15288,-15291,-15294,-15296,-15299,-15302,-15305,-15308,-15310,-15313,-15316,-15319,-15321,-15324,-15327,-15330,-15333,-15335,-15338,-15341,-15344,-15346,-15349,-15352,-15355,-15358,-15360,-15363,-15366,-15369,-15371,-15374,-15377,-15380,-15382,-15385,-15388,-15391,-15394,-15396,-15399,-15402,-15405,-15407,-15410,-15413,-15416,-15419,-15421,-15424,-15427,-15430,-15432,-15435,-15438,-15441,-15443,-15446,-15449,-15452,-15455,-15457,-15460,-15463,-15466,-15468,-15471,-15474,-15477,-15479,-15482,-15485,-15488,-15491,-15493,-15496,-15499,-15502,-15504,-15507,-15510,-15513,-15515,-15518,-15521,-15524,-15527,-15529,-15532,-15535,-15538,-15540,-15543,-15546,-15549,-15551,-15554,-15557,-15560,-15562,-15565,-15568,-15571,-15574,-15576,-15579,-15582,-15585,-15587,-15590,-15593,-15596,-15598,-15601,-15604,-15607,-15609,-15612,-15615,-15618,-15621,-15623,-15626,-15629,-15632,-15634,-15637,-15640,-15643,-15645,-15648,-15651,-15654,-15656,-15659,-15662,-15665,-15667,-15670,-15673,-15676,-15678,-15681,-15684,-15687,-15690,-15692,-15695,-15698,-15701,-15703,-15706,-15709,-15712,-15714,-15717,-15720,-15723,-15725,-15728,-15731,-15734,-15736,-15739,-15742,-15745,-15747,-15750,-15753,-15756,-15758,-15761,-15764,-15767,-15769,-15772,-15775,-15778,-15780,-15783,-15786,-15789,-15791,-15794,-15797,-15800,-15802,-15805,-15808,-15811,-15813,-15816,-15819,-15822,-15824,-15827,-15830,-15833,-15835,-15838,-15841,-15844,-15846,-15849,-15852,-15855,-15857,-15860,-15863,-15866,-15868,-15871,-15874,-15877,-15879,-15882,-15885,-15888,-15890,-15893,-15896,-15899,-15901,-15904,-15907,-15910,-15912,-15915,-15918,-15921,-15923,-15926,-15929,-15932,-15934,-15937,-15940,-15943,-15945,-15948,-15951,-15954,-15956,-15959,-15962,-15965,-15967,-15970,-15973,-15976,-15978,-15981,-15984,-15987,-15989,-15992,-15995,-15997,-16000,-16003,-16006,-16008,-16011,-16014,-16017,-16019,-16022,-16025,-16028,-16030,-16033,-16036,-16039,-16041,-16044,-16047,-16050,-16052,-16055,-16058,-16061,-16063,-16066,-16069,-16071,-16074,-16077,-16080,-16082,-16085,-16088,-16091,-16093,-16096,-16099,-16102,-16104,-16107,-16110,-16113,-16115,-16118,-16121,-16123,-16126,-16129,-16132,-16134,-16137,-16140,-16143,-16145,-16148,-16151,-16154,-16156,-16159,-16162,-16164,-16167,-16170,-16173,-16175,-16178,-16181,-16184,-16186,-16189,-16192,-16195,-16197,-16200,-16203,-16205,-16208,-16211,-16214,-16216,-16219,-16222,-16225,-16227,-16230,-16233,-16235,-16238,-16241,-16244,-16246,-16249,-16252,-16255,-16257,-16260,-16263,-16265,-16268,-16271,-16274,-16276,-16279,-16282,-16285,-16287,-16290,-16293,-16295,-16298,-16301,-16304,-16306,-16309,-16312,-16315,-16317,-16320,-16323,-16325,-16328,-16331,-16334,-16336,-16339,-16342,-16344,-16347,-16350,-16353,-16355,-16358,-16361,-16364,-16366,-16369,-16372,-16374,-16377,-16380,-16383,-16385,-16388,-16391,-16393,-16396,-16399,-16402,-16404,-16407,-16410,-16413,-16415,-16418,-16421,-16423,-16426,-16429,-16432,-16434,-16437,-16440,-16442,-16445,-16448,-16451,-16453,-16456,-16459,-16461,-16464,-16467,-16470,-16472,-16475,-16478,-16480,-16483,-16486,-16489,-16491,-16494,-16497,-16499,-16502,-16505,-16508,-16510,-16513,-16516,-16518,-16521,-16524,-16527,-16529,-16532,-16535,-16537,-16540,-16543,-16546,-16548,-16551,-16554,-16556,-16559,-16562,-16565,-16567,-16570,-16573,-16575,-16578,-16581,-16584,-16586,-16589,-16592,-16594,-16597,-16600,-16602,-16605,-16608,-16611,-16613,-16616,-16619,-16621,-16624,-16627,-16630,-16632,-16635,-16638,-16640,-16643,-16646,-16648,-16651,-16654,-16657,-16659,-16662,-16665,-16667,-16670,-16673,-16676,-16678,-16681,-16684,-16686,-16689,-16692,-16694,-16697,-16700,-16703,-16705,-16708,-16711,-16713,-16716,-16719,-16721,-16724,-16727,-16730,-16732,-16735,-16738,-16740,-16743,-16746,-16749,-16751,-16754,-16757,-16759,-16762,-16765,-16767,-16770,-16773,-16775,-16778,-16781,-16784,-16786,-16789,-16792,-16794,-16797,-16800,-16802,-16805,-16808,-16811,-16813,-16816,-16819,-16821,-16824,-16827,-16829,-16832,-16835,-16838,-16840,-16843,-16846,-16848,-16851,-16854,-16856,-16859,-16862,-16864,-16867,-16870,-16873,-16875,-16878,-16881,-16883,-16886,-16889,-16891,-16894,-16897,-16899,-16902,-16905,-16908,-16910,-16913,-16916,-16918,-16921,-16924,-16926,-16929,-16932,-16934,-16937,-16940,-16943,-16945,-16948,-16951,-16953,-16956,-16959,-16961,-16964,-16967,-16969,-16972,-16975,-16977,-16980,-16983,-16986,-16988,-16991,-16994,-16996,-16999,-17002,-17004,-17007,-17010,-17012,-17015,-17018,-17020,-17023,-17026,-17028,-17031,-17034,-17037,-17039,-17042,-17045,-17047,-17050,-17053,-17055,-17058,-17061,-17063,-17066,-17069,-17071,-17074,-17077,-17079,-17082,-17085,-17087,-17090,-17093,-17096,-17098,-17101,-17104,-17106,-17109,-17112,-17114,-17117,-17120,-17122,-17125,-17128,-17130,-17133,-17136,-17138,-17141,-17144,-17146,-17149,-17152,-17154,-17157,-17160,-17162,-17165,-17168,-17171,-17173,-17176,-17179,-17181,-17184,-17187,-17189,-17192,-17195,-17197,-17200,-17203,-17205,-17208,-17211,-17213,-17216,-17219,-17221,-17224,-17227,-17229,-17232,-17235,-17237,-17240,-17243,-17245,-17248,-17251,-17253,-17256,-17259,-17261,-17264,-17267,-17269,-17272,-17275,-17277,-17280,-17283,-17285,-17288,-17291,-17293,-17296,-17299,-17301,-17304,-17307,-17309,-17312,-17315,-17317,-17320,-17323,-17325,-17328,-17331,-17333,-17336,-17339,-17341,-17344,-17347,-17349,-17352,-17355,-17357,-17360,-17363,-17365,-17368,-17371,-17373,-17376,-17379,-17381,-17384,-17387,-17389,-17392,-17395,-17397,-17400,-17403,-17405,-17408,-17411,-17413,-17416,-17419,-17421,-17424,-17427,-17429,-17432,-17435,-17437,-17440,-17443,-17445,-17448,-17451,-17453,-17456,-17459,-17461,-17464,-17467,-17469,-17472,-17474,-17477,-17480,-17482,-17485,-17488,-17490,-17493,-17496,-17498,-17501,-17504,-17506,-17509,-17512,-17514,-17517,-17520,-17522,-17525,-17528,-17530,-17533,-17536,-17538,-17541,-17544,-17546,-17549,-17551,-17554,-17557,-17559,-17562,-17565,-17567,-17570,-17573,-17575,-17578,-17581,-17583,-17586,-17589,-17591,-17594,-17597,-17599,-17602,-17605,-17607,-17610,-17612,-17615,-17618,-17620,-17623,-17626,-17628,-17631,-17634,-17636,-17639,-17642,-17644,-17647,-17650,-17652,-17655,-17657,-17660,-17663,-17665,-17668,-17671,-17673,-17676,-17679,-17681,-17684,-17687,-17689,-17692,-17695,-17697,-17700,-17702,-17705,-17708,-17710,-17713,-17716,-17718,-17721,-17724,-17726,-17729,-17732,-17734,-17737,-17739,-17742,-17745,-17747,-17750,-17753,-17755,-17758,-17761,-17763,-17766,-17768,-17771,-17774,-17776,-17779,-17782,-17784,-17787,-17790,-17792,-17795,-17798,-17800,-17803,-17805,-17808,-17811,-17813,-17816,-17819,-17821,-17824,-17827,-17829,-17832,-17834,-17837,-17840,-17842,-17845,-17848,-17850,-17853,-17855,-17858,-17861,-17863,-17866,-17869,-17871,-17874,-17877,-17879,-17882,-17884,-17887,-17890,-17892,-17895,-17898,-17900,-17903,-17906,-17908,-17911,-17913,-17916,-17919,-17921,-17924,-17927,-17929,-17932,-17934,-17937,-17940,-17942,-17945,-17948,-17950,-17953,-17955,-17958,-17961,-17963,-17966,-17969,-17971,-17974,-17976,-17979,-17982,-17984,-17987,-17990,-17992,-17995,-17997,-18000,-18003,-18005,-18008,-18011,-18013,-18016,-18018,-18021,-18024,-18026,-18029,-18032,-18034,-18037,-18039,-18042,-18045,-18047,-18050,-18053,-18055,-18058,-18060,-18063,-18066,-18068,-18071,-18074,-18076,-18079,-18081,-18084,-18087,-18089,-18092,-18095,-18097,-18100,-18102,-18105,-18108,-18110,-18113,-18115,-18118,-18121,-18123,-18126,-18129,-18131,-18134,-18136,-18139,-18142,-18144,-18147,-18149,-18152,-18155,-18157,-18160,-18163,-18165,-18168,-18170,-18173,-18176,-18178,-18181,-18183,-18186,-18189,-18191,-18194,-18197,-18199,-18202,-18204,-18207,-18210,-18212,-18215,-18217,-18220,-18223,-18225,-18228,-18230,-18233,-18236,-18238,-18241,-18244,-18246,-18249,-18251,-18254,-18257,-18259,-18262,-18264,-18267,-18270,-18272,-18275,-18277,-18280,-18283,-18285,-18288,-18290,-18293,-18296,-18298,-18301,-18304,-18306,-18309,-18311,-18314,-18317,-18319,-18322,-18324,-18327,-18330,-18332,-18335,-18337,-18340,-18343,-18345,-18348,-18350,-18353,-18356,-18358,-18361,-18363,-18366,-18369,-18371,-18374,-18376,-18379,-18382,-18384,-18387,-18389,-18392,-18395,-18397,-18400,-18402,-18405,-18408,-18410,-18413,-18415,-18418,-18421,-18423,-18426,-18428,-18431,-18434,-18436,-18439,-18441,-18444,-18447,-18449,-18452,-18454,-18457,-18460,-18462,-18465,-18467,-18470,-18473,-18475,-18478,-18480,-18483,-18485,-18488,-18491,-18493,-18496,-18498,-18501,-18504,-18506,-18509,-18511,-18514,-18517,-18519,-18522,-18524,-18527,-18530,-18532,-18535,-18537,-18540,-18543,-18545,-18548,-18550,-18553,-18555,-18558,-18561,-18563,-18566,-18568,-18571,-18574,-18576,-18579,-18581,-18584,-18587,-18589,-18592,-18594,-18597,-18599,-18602,-18605,-18607,-18610,-18612,-18615,-18618,-18620,-18623,-18625,-18628,-18630,-18633,-18636,-18638,-18641,-18643,-18646,-18649,-18651,-18654,-18656,-18659,-18661,-18664,-18667,-18669,-18672,-18674,-18677,-18680,-18682,-18685,-18687,-18690,-18692,-18695,-18698,-18700,-18703,-18705,-18708,-18711,-18713,-18716,-18718,-18721,-18723,-18726,-18729,-18731,-18734,-18736,-18739,-18741,-18744,-18747,-18749,-18752,-18754,-18757,-18759,-18762,-18765,-18767,-18770,-18772,-18775,-18778,-18780,-18783,-18785,-18788,-18790,-18793,-18796,-18798,-18801,-18803,-18806,-18808,-18811,-18814,-18816,-18819,-18821,-18824,-18826,-18829,-18832,-18834,-18837,-18839,-18842,-18844,-18847,-18850,-18852,-18855,-18857,-18860,-18862,-18865,-18868,-18870,-18873,-18875,-18878,-18880,-18883,-18885,-18888,-18891,-18893,-18896,-18898,-18901,-18903,-18906,-18909,-18911,-18914,-18916,-18919,-18921,-18924,-18927,-18929,-18932,-18934,-18937,-18939,-18942,-18944,-18947,-18950,-18952,-18955,-18957,-18960,-18962,-18965,-18968,-18970,-18973,-18975,-18978,-18980,-18983,-18985,-18988,-18991,-18993,-18996,-18998,-19001,-19003,-19006,-19009,-19011,-19014,-19016,-19019,-19021,-19024,-19026,-19029,-19032,-19034,-19037,-19039,-19042,-19044,-19047,-19049,-19052,-19055,-19057,-19060,-19062,-19065,-19067,-19070,-19072,-19075,-19078,-19080,-19083,-19085,-19088,-19090,-19093,-19095,-19098,-19101,-19103,-19106,-19108,-19111,-19113,-19116,-19118,-19121,-19123,-19126,-19129,-19131,-19134,-19136,-19139,-19141,-19144,-19146,-19149,-19152,-19154,-19157,-19159,-19162,-19164,-19167,-19169,-19172,-19174,-19177,-19180,-19182,-19185,-19187,-19190,-19192,-19195,-19197,-19200,-19202,-19205,-19208,-19210,-19213,-19215,-19218,-19220,-19223,-19225,-19228,-19230,-19233,-19236,-19238,-19241,-19243,-19246,-19248,-19251,-19253,-19256,-19258,-19261,-19264,-19266,-19269,-19271,-19274,-19276,-19279,-19281,-19284,-19286,-19289,-19291,-19294,-19297,-19299,-19302,-19304,-19307,-19309,-19312,-19314,-19317,-19319,-19322,-19324,-19327,-19330,-19332,-19335,-19337,-19340,-19342,-19345,-19347,-19350,-19352,-19355,-19357,-19360,-19362,-19365,-19368,-19370,-19373,-19375,-19378,-19380,-19383,-19385,-19388,-19390,-19393,-19395,-19398,-19400,-19403,-19406,-19408,-19411,-19413,-19416,-19418,-19421,-19423,-19426,-19428,-19431,-19433,-19436,-19438,-19441,-19444,-19446,-19449,-19451,-19454,-19456,-19459,-19461,-19464,-19466,-19469,-19471,-19474,-19476,-19479,-19481,-19484,-19486,-19489,-19492,-19494,-19497,-19499,-19502,-19504,-19507,-19509,-19512,-19514,-19517,-19519,-19522,-19524,-19527,-19529,-19532,-19534,-19537,-19539,-19542,-19545,-19547,-19550,-19552,-19555,-19557,-19560,-19562,-19565,-19567,-19570,-19572,-19575,-19577,-19580,-19582,-19585,-19587,-19590,-19592,-19595,-19597,-19600,-19602,-19605,-19607,-19610,-19613,-19615,-19618,-19620,-19623,-19625,-19628,-19630,-19633,-19635,-19638,-19640,-19643,-19645,-19648,-19650,-19653,-19655,-19658,-19660,-19663,-19665,-19668,-19670,-19673,-19675,-19678,-19680,-19683,-19685,-19688,-19690,-19693,-19695,-19698,-19700,-19703,-19706,-19708,-19711,-19713,-19716,-19718,-19721,-19723,-19726,-19728,-19731,-19733,-19736,-19738,-19741,-19743,-19746,-19748,-19751,-19753,-19756,-19758,-19761,-19763,-19766,-19768,-19771,-19773,-19776,-19778,-19781,-19783,-19786,-19788,-19791,-19793,-19796,-19798,-19801,-19803,-19806,-19808,-19811,-19813,-19816,-19818,-19821,-19823,-19826,-19828,-19831,-19833,-19836,-19838,-19841,-19843,-19846,-19848,-19851,-19853,-19856,-19858,-19861,-19863,-19866,-19868,-19871,-19873,-19876,-19878,-19881,-19883,-19886,-19888,-19891,-19893,-19896,-19898,-19901,-19903,-19906,-19908,-19911,-19913,-19916,-19918,-19921,-19923,-19926,-19928,-19931,-19933,-19936,-19938,-19941,-19943,-19946,-19948,-19951,-19953,-19956,-19958,-19961,-19963,-19966,-19968,-19971,-19973,-19976,-19978,-19981,-19983,-19985,-19988,-19990,-19993,-19995,-19998,-20000,-20003,-20005,-20008,-20010,-20013,-20015,-20018,-20020,-20023,-20025,-20028,-20030,-20033,-20035,-20038,-20040,-20043,-20045,-20048,-20050,-20053,-20055,-20058,-20060,-20063,-20065,-20068,-20070,-20072,-20075,-20077,-20080,-20082,-20085,-20087,-20090,-20092,-20095,-20097,-20100,-20102,-20105,-20107,-20110,-20112,-20115,-20117,-20120,-20122,-20125,-20127,-20130,-20132,-20135,-20137,-20139,-20142,-20144,-20147,-20149,-20152,-20154,-20157,-20159,-20162,-20164,-20167,-20169,-20172,-20174,-20177,-20179,-20182,-20184,-20187,-20189,-20191,-20194,-20196,-20199,-20201,-20204,-20206,-20209,-20211,-20214,-20216,-20219,-20221,-20224,-20226,-20229,-20231,-20234,-20236,-20238,-20241,-20243,-20246,-20248,-20251,-20253,-20256,-20258,-20261,-20263,-20266,-20268,-20271,-20273,-20275,-20278,-20280,-20283,-20285,-20288,-20290,-20293,-20295,-20298,-20300,-20303,-20305,-20308,-20310,-20312,-20315,-20317,-20320,-20322,-20325,-20327,-20330,-20332,-20335,-20337,-20340,-20342,-20345,-20347,-20349,-20352,-20354,-20357,-20359,-20362,-20364,-20367,-20369,-20372,-20374,-20377,-20379,-20381,-20384,-20386,-20389,-20391,-20394,-20396,-20399,-20401,-20404,-20406,-20408,-20411,-20413,-20416,-20418,-20421,-20423,-20426,-20428,-20431,-20433,-20436,-20438,-20440,-20443,-20445,-20448,-20450,-20453,-20455,-20458,-20460,-20463,-20465,-20467,-20470,-20472,-20475,-20477,-20480,-20482,-20485,-20487,-20489,-20492,-20494,-20497,-20499,-20502,-20504,-20507,-20509,-20512,-20514,-20516,-20519,-20521,-20524,-20526,-20529,-20531,-20534,-20536,-20538,-20541,-20543,-20546,-20548,-20551,-20553,-20556,-20558,-20560,-20563,-20565,-20568,-20570,-20573,-20575,-20578,-20580,-20583,-20585,-20587,-20590,-20592,-20595,-20597,-20600,-20602,-20604,-20607,-20609,-20612,-20614,-20617,-20619,-20622,-20624,-20626,-20629,-20631,-20634,-20636,-20639,-20641,-20644,-20646,-20648,-20651,-20653,-20656,-20658,-20661,-20663,-20666,-20668,-20670,-20673,-20675,-20678,-20680,-20683,-20685,-20687,-20690,-20692,-20695,-20697,-20700,-20702,-20704,-20707,-20709,-20712,-20714,-20717,-20719,-20722,-20724,-20726,-20729,-20731,-20734,-20736,-20739,-20741,-20743,-20746,-20748,-20751,-20753,-20756,-20758,-20760,-20763,-20765,-20768,-20770,-20773,-20775,-20777,-20780,-20782,-20785,-20787,-20790,-20792,-20794,-20797,-20799,-20802,-20804,-20807,-20809,-20811,-20814,-20816,-20819,-20821,-20824,-20826,-20828,-20831,-20833,-20836,-20838,-20841,-20843,-20845,-20848,-20850,-20853,-20855,-20858,-20860,-20862,-20865,-20867,-20870,-20872,-20874,-20877,-20879,-20882,-20884,-20887,-20889,-20891,-20894,-20896,-20899,-20901,-20904,-20906,-20908,-20911,-20913,-20916,-20918,-20920,-20923,-20925,-20928,-20930,-20933,-20935,-20937,-20940,-20942,-20945,-20947,-20949,-20952,-20954,-20957,-20959,-20962,-20964,-20966,-20969,-20971,-20974,-20976,-20978,-20981,-20983,-20986,-20988,-20990,-20993,-20995,-20998,-21000,-21003,-21005,-21007,-21010,-21012,-21015,-21017,-21019,-21022,-21024,-21027,-21029,-21031,-21034,-21036,-21039,-21041,-21043,-21046,-21048,-21051,-21053,-21056,-21058,-21060,-21063,-21065,-21068,-21070,-21072,-21075,-21077,-21080,-21082,-21084,-21087,-21089,-21092,-21094,-21096,-21099,-21101,-21104,-21106,-21108,-21111,-21113,-21116,-21118,-21120,-21123,-21125,-21128,-21130,-21132,-21135,-21137,-21140,-21142,-21144,-21147,-21149,-21152,-21154,-21156,-21159,-21161,-21164,-21166,-21168,-21171,-21173,-21176,-21178,-21180,-21183,-21185,-21188,-21190,-21192,-21195,-21197,-21200,-21202,-21204,-21207,-21209,-21212,-21214,-21216,-21219,-21221,-21224,-21226,-21228,-21231,-21233,-21236,-21238,-21240,-21243,-21245,-21247,-21250,-21252,-21255,-21257,-21259,-21262,-21264,-21267,-21269,-21271,-21274,-21276,-21279,-21281,-21283,-21286,-21288,-21290,-21293,-21295,-21298,-21300,-21302,-21305,-21307,-21310,-21312,-21314,-21317,-21319,-21322,-21324,-21326,-21329,-21331,-21333,-21336,-21338,-21341,-21343,-21345,-21348,-21350,-21353,-21355,-21357,-21360,-21362,-21364,-21367,-21369,-21372,-21374,-21376,-21379,-21381,-21383,-21386,-21388,-21391,-21393,-21395,-21398,-21400,-21403,-21405,-21407,-21410,-21412,-21414,-21417,-21419,-21422,-21424,-21426,-21429,-21431,-21433,-21436,-21438,-21441,-21443,-21445,-21448,-21450,-21452,-21455,-21457,-21460,-21462,-21464,-21467,-21469,-21471,-21474,-21476,-21479,-21481,-21483,-21486,-21488,-21490,-21493,-21495,-21498,-21500,-21502,-21505,-21507,-21509,-21512,-21514,-21516,-21519,-21521,-21524,-21526,-21528,-21531,-21533,-21535,-21538,-21540,-21543,-21545,-21547,-21550,-21552,-21554,-21557,-21559,-21561,-21564,-21566,-21569,-21571,-21573,-21576,-21578,-21580,-21583,-21585,-21587,-21590,-21592,-21595,-21597,-21599,-21602,-21604,-21606,-21609,-21611,-21613,-21616,-21618,-21621,-21623,-21625,-21628,-21630,-21632,-21635,-21637,-21639,-21642,-21644,-21646,-21649,-21651,-21654,-21656,-21658,-21661,-21663,-21665,-21668,-21670,-21672,-21675,-21677,-21679,-21682,-21684,-21687,-21689,-21691,-21694,-21696,-21698,-21701,-21703,-21705,-21708,-21710,-21712,-21715,-21717,-21719,-21722,-21724,-21727,-21729,-21731,-21734,-21736,-21738,-21741,-21743,-21745,-21748,-21750,-21752,-21755,-21757,-21759,-21762,-21764,-21766,-21769,-21771,-21774,-21776,-21778,-21781,-21783,-21785,-21788,-21790,-21792,-21795,-21797,-21799,-21802,-21804,-21806,-21809,-21811,-21813,-21816,-21818,-21820,-21823,-21825,-21827,-21830,-21832,-21835,-21837,-21839,-21842,-21844,-21846,-21849,-21851,-21853,-21856,-21858,-21860,-21863,-21865,-21867,-21870,-21872,-21874,-21877,-21879,-21881,-21884,-21886,-21888,-21891,-21893,-21895,-21898,-21900,-21902,-21905,-21907,-21909,-21912,-21914,-21916,-21919,-21921,-21923,-21926,-21928,-21930,-21933,-21935,-21937,-21940,-21942,-21944,-21947,-21949,-21951,-21954,-21956,-21958,-21961,-21963,-21965,-21968,-21970,-21972,-21975,-21977,-21979,-21982,-21984,-21986,-21989,-21991,-21993,-21996,-21998,-22000,-22003,-22005,-22007,-22010,-22012,-22014,-22017,-22019,-22021,-22024,-22026,-22028,-22031,-22033,-22035,-22038,-22040,-22042,-22045,-22047,-22049,-22051,-22054,-22056,-22058,-22061,-22063,-22065,-22068,-22070,-22072,-22075,-22077,-22079,-22082,-22084,-22086,-22089,-22091,-22093,-22096,-22098,-22100,-22103,-22105,-22107,-22110,-22112,-22114,-22116,-22119,-22121,-22123,-22126,-22128,-22130,-22133,-22135,-22137,-22140,-22142,-22144,-22147,-22149,-22151,-22154,-22156,-22158,-22160,-22163,-22165,-22167,-22170,-22172,-22174,-22177,-22179,-22181,-22184,-22186,-22188,-22191,-22193,-22195,-22197,-22200,-22202,-22204,-22207,-22209,-22211,-22214,-22216,-22218,-22221,-22223,-22225,-22227,-22230,-22232,-22234,-22237,-22239,-22241,-22244,-22246,-22248,-22251,-22253,-22255,-22257,-22260,-22262,-22264,-22267,-22269,-22271,-22274,-22276,-22278,-22281,-22283,-22285,-22287,-22290,-22292,-22294,-22297,-22299,-22301,-22304,-22306,-22308,-22310,-22313,-22315,-22317,-22320,-22322,-22324,-22327,-22329,-22331,-22333,-22336,-22338,-22340,-22343,-22345,-22347,-22350,-22352,-22354,-22356,-22359,-22361,-22363,-22366,-22368,-22370,-22373,-22375,-22377,-22379,-22382,-22384,-22386,-22389,-22391,-22393,-22395,-22398,-22400,-22402,-22405,-22407,-22409,-22411,-22414,-22416,-22418,-22421,-22423,-22425,-22428,-22430,-22432,-22434,-22437,-22439,-22441,-22444,-22446,-22448,-22450,-22453,-22455,-22457,-22460,-22462,-22464,-22466,-22469,-22471,-22473,-22476,-22478,-22480,-22482,-22485,-22487,-22489,-22492,-22494,-22496,-22498,-22501,-22503,-22505,-22508,-22510,-22512,-22514,-22517,-22519,-22521,-22524,-22526,-22528,-22530,-22533,-22535,-22537,-22540,-22542,-22544,-22546,-22549,-22551,-22553,-22555,-22558,-22560,-22562,-22565,-22567,-22569,-22571,-22574,-22576,-22578,-22581,-22583,-22585,-22587,-22590,-22592,-22594,-22596,-22599,-22601,-22603,-22606,-22608,-22610,-22612,-22615,-22617,-22619,-22621,-22624,-22626,-22628,-22631,-22633,-22635,-22637,-22640,-22642,-22644,-22646,-22649,-22651,-22653,-22656,-22658,-22660,-22662,-22665,-22667,-22669,-22671,-22674,-22676,-22678,-22680,-22683,-22685,-22687,-22690,-22692,-22694,-22696,-22699,-22701,-22703,-22705,-22708,-22710,-22712,-22714,-22717,-22719,-22721,-22724,-22726,-22728,-22730,-22733,-22735,-22737,-22739,-22742,-22744,-22746,-22748,-22751,-22753,-22755,-22757,-22760,-22762,-22764,-22766,-22769,-22771,-22773,-22776,-22778,-22780,-22782,-22785,-22787,-22789,-22791,-22794,-22796,-22798,-22800,-22803,-22805,-22807,-22809,-22812,-22814,-22816,-22818,-22821,-22823,-22825,-22827,-22830,-22832,-22834,-22836,-22839,-22841,-22843,-22845,-22848,-22850,-22852,-22854,-22857,-22859,-22861,-22863,-22866,-22868,-22870,-22872,-22875,-22877,-22879,-22881,-22884,-22886,-22888,-22890,-22893,-22895,-22897,-22899,-22902,-22904,-22906,-22908,-22911,-22913,-22915,-22917,-22920,-22922,-22924,-22926,-22929,-22931,-22933,-22935,-22938,-22940,-22942,-22944,-22947,-22949,-22951,-22953,-22956,-22958,-22960,-22962,-22965,-22967,-22969,-22971,-22973,-22976,-22978,-22980,-22982,-22985,-22987,-22989,-22991,-22994,-22996,-22998,-23000,-23003,-23005,-23007,-23009,-23012,-23014,-23016,-23018,-23020,-23023,-23025,-23027,-23029,-23032,-23034,-23036,-23038,-23041,-23043,-23045,-23047,-23050,-23052,-23054,-23056,-23058,-23061,-23063,-23065,-23067,-23070,-23072,-23074,-23076,-23079,-23081,-23083,-23085,-23087,-23090,-23092,-23094,-23096,-23099,-23101,-23103,-23105,-23107,-23110,-23112,-23114,-23116,-23119,-23121,-23123,-23125,-23128,-23130,-23132,-23134,-23136,-23139,-23141,-23143,-23145,-23148,-23150,-23152,-23154,-23156,-23159,-23161,-23163,-23165,-23168,-23170,-23172,-23174,-23176,-23179,-23181,-23183,-23185,-23188,-23190,-23192,-23194,-23196,-23199,-23201,-23203,-23205,-23208,-23210,-23212,-23214,-23216,-23219,-23221,-23223,-23225,-23227,-23230,-23232,-23234,-23236,-23239,-23241,-23243,-23245,-23247,-23250,-23252,-23254,-23256,-23258,-23261,-23263,-23265,-23267,-23270,-23272,-23274,-23276,-23278,-23281,-23283,-23285,-23287,-23289,-23292,-23294,-23296,-23298,-23300,-23303,-23305,-23307,-23309,-23311,-23314,-23316,-23318,-23320,-23323,-23325,-23327,-23329,-23331,-23334,-23336,-23338,-23340,-23342,-23345,-23347,-23349,-23351,-23353,-23356,-23358,-23360,-23362,-23364,-23367,-23369,-23371,-23373,-23375,-23378,-23380,-23382,-23384,-23386,-23389,-23391,-23393,-23395,-23397,-23400,-23402,-23404,-23406,-23408,-23411,-23413,-23415,-23417,-23419,-23422,-23424,-23426,-23428,-23430,-23433,-23435,-23437,-23439,-23441,-23444,-23446,-23448,-23450,-23452,-23455,-23457,-23459,-23461,-23463,-23466,-23468,-23470,-23472,-23474,-23476,-23479,-23481,-23483,-23485,-23487,-23490,-23492,-23494,-23496,-23498,-23501,-23503,-23505,-23507,-23509,-23512,-23514,-23516,-23518,-23520,-23522,-23525,-23527,-23529,-23531,-23533,-23536,-23538,-23540,-23542,-23544,-23546,-23549,-23551,-23553,-23555,-23557,-23560,-23562,-23564,-23566,-23568,-23571,-23573,-23575,-23577,-23579,-23581,-23584,-23586,-23588,-23590,-23592,-23595,-23597,-23599,-23601,-23603,-23605,-23608,-23610,-23612,-23614,-23616,-23618,-23621,-23623,-23625,-23627,-23629,-23632,-23634,-23636,-23638,-23640,-23642,-23645,-23647,-23649,-23651,-23653,-23655,-23658,-23660,-23662,-23664,-23666,-23668,-23671,-23673,-23675,-23677,-23679,-23682,-23684,-23686,-23688,-23690,-23692,-23695,-23697,-23699,-23701,-23703,-23705,-23708,-23710,-23712,-23714,-23716,-23718,-23721,-23723,-23725,-23727,-23729,-23731,-23734,-23736,-23738,-23740,-23742,-23744,-23747,-23749,-23751,-23753,-23755,-23757,-23760,-23762,-23764,-23766,-23768,-23770,-23773,-23775,-23777,-23779,-23781,-23783,-23785,-23788,-23790,-23792,-23794,-23796,-23798,-23801,-23803,-23805,-23807,-23809,-23811,-23814,-23816,-23818,-23820,-23822,-23824,-23827,-23829,-23831,-23833,-23835,-23837,-23839,-23842,-23844,-23846,-23848,-23850,-23852,-23855,-23857,-23859,-23861,-23863,-23865,-23867,-23870,-23872,-23874,-23876,-23878,-23880,-23883,-23885,-23887,-23889,-23891,-23893,-23895,-23898,-23900,-23902,-23904,-23906,-23908,-23910,-23913,-23915,-23917,-23919,-23921,-23923,-23925,-23928,-23930,-23932,-23934,-23936,-23938,-23940,-23943,-23945,-23947,-23949,-23951,-23953,-23956,-23958,-23960,-23962,-23964,-23966,-23968,-23971,-23973,-23975,-23977,-23979,-23981,-23983,-23985,-23988,-23990,-23992,-23994,-23996,-23998,-24000,-24003,-24005,-24007,-24009,-24011,-24013,-24015,-24018,-24020,-24022,-24024,-24026,-24028,-24030,-24033,-24035,-24037,-24039,-24041,-24043,-24045,-24047,-24050,-24052,-24054,-24056,-24058,-24060,-24062,-24065,-24067,-24069,-24071,-24073,-24075,-24077,-24079,-24082,-24084,-24086,-24088,-24090,-24092,-24094,-24096,-24099,-24101,-24103,-24105,-24107,-24109,-24111,-24114,-24116,-24118,-24120,-24122,-24124,-24126,-24128,-24131,-24133,-24135,-24137,-24139,-24141,-24143,-24145,-24148,-24150,-24152,-24154,-24156,-24158,-24160,-24162,-24164,-24167,-24169,-24171,-24173,-24175,-24177,-24179,-24181,-24184,-24186,-24188,-24190,-24192,-24194,-24196,-24198,-24201,-24203,-24205,-24207,-24209,-24211,-24213,-24215,-24217,-24220,-24222,-24224,-24226,-24228,-24230,-24232,-24234,-24237,-24239,-24241,-24243,-24245,-24247,-24249,-24251,-24253,-24256,-24258,-24260,-24262,-24264,-24266,-24268,-24270,-24272,-24275,-24277,-24279,-24281,-24283,-24285,-24287,-24289,-24291,-24294,-24296,-24298,-24300,-24302,-24304,-24306,-24308,-24310,-24312,-24315,-24317,-24319,-24321,-24323,-24325,-24327,-24329,-24331,-24334,-24336,-24338,-24340,-24342,-24344,-24346,-24348,-24350,-24352,-24355,-24357,-24359,-24361,-24363,-24365,-24367,-24369,-24371,-24373,-24376,-24378,-24380,-24382,-24384,-24386,-24388,-24390,-24392,-24394,-24397,-24399,-24401,-24403,-24405,-24407,-24409,-24411,-24413,-24415,-24417,-24420,-24422,-24424,-24426,-24428,-24430,-24432,-24434,-24436,-24438,-24441,-24443,-24445,-24447,-24449,-24451,-24453,-24455,-24457,-24459,-24461,-24464,-24466,-24468,-24470,-24472,-24474,-24476,-24478,-24480,-24482,-24484,-24487,-24489,-24491,-24493,-24495,-24497,-24499,-24501,-24503,-24505,-24507,-24509,-24512,-24514,-24516,-24518,-24520,-24522,-24524,-24526,-24528,-24530,-24532,-24534,-24537,-24539,-24541,-24543,-24545,-24547,-24549,-24551,-24553,-24555,-24557,-24559,-24562,-24564,-24566,-24568,-24570,-24572,-24574,-24576,-24578,-24580,-24582,-24584,-24586,-24589,-24591,-24593,-24595,-24597,-24599,-24601,-24603,-24605,-24607,-24609,-24611,-24613,-24616,-24618,-24620,-24622,-24624,-24626,-24628,-24630,-24632,-24634,-24636,-24638,-24640,-24642,-24645,-24647,-24649,-24651,-24653,-24655,-24657,-24659,-24661,-24663,-24665,-24667,-24669,-24671,-24673,-24676,-24678,-24680,-24682,-24684,-24686,-24688,-24690,-24692,-24694,-24696,-24698,-24700,-24702,-24704,-24707,-24709,-24711,-24713,-24715,-24717,-24719,-24721,-24723,-24725,-24727,-24729,-24731,-24733,-24735,-24737,-24740,-24742,-24744,-24746,-24748,-24750,-24752,-24754,-24756,-24758,-24760,-24762,-24764,-24766,-24768,-24770,-24772,-24774,-24777,-24779,-24781,-24783,-24785,-24787,-24789,-24791,-24793,-24795,-24797,-24799,-24801,-24803,-24805,-24807,-24809,-24811,-24814,-24816,-24818,-24820,-24822,-24824,-24826,-24828,-24830,-24832,-24834,-24836,-24838,-24840,-24842,-24844,-24846,-24848,-24850,-24852,-24855,-24857,-24859,-24861,-24863,-24865,-24867,-24869,-24871,-24873,-24875,-24877,-24879,-24881,-24883,-24885,-24887,-24889,-24891,-24893,-24895,-24897,-24899,-24902,-24904,-24906,-24908,-24910,-24912,-24914,-24916,-24918,-24920,-24922,-24924,-24926,-24928,-24930,-24932,-24934,-24936,-24938,-24940,-24942,-24944,-24946,-24948,-24950,-24953,-24955,-24957,-24959,-24961,-24963,-24965,-24967,-24969,-24971,-24973,-24975,-24977,-24979,-24981,-24983,-24985,-24987,-24989,-24991,-24993,-24995,-24997,-24999,-25001,-25003,-25005,-25007,-25009,-25011,-25013,-25016,-25018,-25020,-25022,-25024,-25026,-25028,-25030,-25032,-25034,-25036,-25038,-25040,-25042,-25044,-25046,-25048,-25050,-25052,-25054,-25056,-25058,-25060,-25062,-25064,-25066,-25068,-25070,-25072,-25074,-25076,-25078,-25080,-25082,-25084,-25086,-25088,-25090,-25092,-25094,-25096,-25099,-25101,-25103,-25105,-25107,-25109,-25111,-25113,-25115,-25117,-25119,-25121,-25123,-25125,-25127,-25129,-25131,-25133,-25135,-25137,-25139,-25141,-25143,-25145,-25147,-25149,-25151,-25153,-25155,-25157,-25159,-25161,-25163,-25165,-25167,-25169,-25171,-25173,-25175,-25177,-25179,-25181,-25183,-25185,-25187,-25189,-25191,-25193,-25195,-25197,-25199,-25201,-25203,-25205,-25207,-25209,-25211,-25213,-25215,-25217,-25219,-25221,-25223,-25225,-25227,-25229,-25231,-25233,-25235,-25237,-25239,-25241,-25243,-25245,-25247,-25249,-25251,-25253,-25255,-25257,-25259,-25261,-25263,-25265,-25267,-25269,-25271,-25273,-25275,-25277,-25279,-25281,-25283,-25285,-25287,-25289,-25291,-25293,-25295,-25297,-25299,-25301,-25303,-25305,-25307,-25309,-25311,-25313,-25315,-25317,-25319,-25321,-25323,-25325,-25327,-25329,-25331,-25333,-25335,-25337,-25339,-25341,-25343,-25345,-25347,-25349,-25351,-25353,-25355,-25357,-25359,-25361,-25363,-25365,-25367,-25369,-25371,-25373,-25375,-25377,-25379,-25381,-25383,-25385,-25387,-25389,-25391,-25393,-25395,-25397,-25399,-25401,-25403,-25405,-25407,-25409,-25411,-25413,-25415,-25417,-25419,-25421,-25423,-25425,-25427,-25429,-25431,-25433,-25435,-25437,-25438,-25440,-25442,-25444,-25446,-25448,-25450,-25452,-25454,-25456,-25458,-25460,-25462,-25464,-25466,-25468,-25470,-25472,-25474,-25476,-25478,-25480,-25482,-25484,-25486,-25488,-25490,-25492,-25494,-25496,-25498,-25500,-25502,-25504,-25506,-25508,-25510,-25512,-25514,-25516,-25518,-25519,-25521,-25523,-25525,-25527,-25529,-25531,-25533,-25535,-25537,-25539,-25541,-25543,-25545,-25547,-25549,-25551,-25553,-25555,-25557,-25559,-25561,-25563,-25565,-25567,-25569,-25571,-25573,-25575,-25577,-25578,-25580,-25582,-25584,-25586,-25588,-25590,-25592,-25594,-25596,-25598,-25600,-25602,-25604,-25606,-25608,-25610,-25612,-25614,-25616,-25618,-25620,-25622,-25624,-25626,-25628,-25629,-25631,-25633,-25635,-25637,-25639,-25641,-25643,-25645,-25647,-25649,-25651,-25653,-25655,-25657,-25659,-25661,-25663,-25665,-25667,-25669,-25671,-25672,-25674,-25676,-25678,-25680,-25682,-25684,-25686,-25688,-25690,-25692,-25694,-25696,-25698,-25700,-25702,-25704,-25706,-25708,-25710,-25711,-25713,-25715,-25717,-25719,-25721,-25723,-25725,-25727,-25729,-25731,-25733,-25735,-25737,-25739,-25741,-25743,-25745,-25746,-25748,-25750,-25752,-25754,-25756,-25758,-25760,-25762,-25764,-25766,-25768,-25770,-25772,-25774,-25776,-25778,-25779,-25781,-25783,-25785,-25787,-25789,-25791,-25793,-25795,-25797,-25799,-25801,-25803,-25805,-25807,-25809,-25810,-25812,-25814,-25816,-25818,-25820,-25822,-25824,-25826,-25828,-25830,-25832,-25834,-25836,-25838,-25839,-25841,-25843,-25845,-25847,-25849,-25851,-25853,-25855,-25857,-25859,-25861,-25863,-25865,-25866,-25868,-25870,-25872,-25874,-25876,-25878,-25880,-25882,-25884,-25886,-25888,-25890,-25892,-25893,-25895,-25897,-25899,-25901,-25903,-25905,-25907,-25909,-25911,-25913,-25915,-25917,-25918,-25920,-25922,-25924,-25926,-25928,-25930,-25932,-25934,-25936,-25938,-25940,-25942,-25943,-25945,-25947,-25949,-25951,-25953,-25955,-25957,-25959,-25961,-25963,-25965,-25966,-25968,-25970,-25972,-25974,-25976,-25978,-25980,-25982,-25984,-25986,-25988,-25989,-25991,-25993,-25995,-25997,-25999,-26001,-26003,-26005,-26007,-26009,-26010,-26012,-26014,-26016,-26018,-26020,-26022,-26024,-26026,-26028,-26030,-26031,-26033,-26035,-26037,-26039,-26041,-26043,-26045,-26047,-26049,-26051,-26052,-26054,-26056,-26058,-26060,-26062,-26064,-26066,-26068,-26070,-26071,-26073,-26075,-26077,-26079,-26081,-26083,-26085,-26087,-26089,-26090,-26092,-26094,-26096,-26098,-26100,-26102,-26104,-26106,-26108,-26109,-26111,-26113,-26115,-26117,-26119,-26121,-26123,-26125,-26127,-26128,-26130,-26132,-26134,-26136,-26138,-26140,-26142,-26144,-26146,-26147,-26149,-26151,-26153,-26155,-26157,-26159,-26161,-26163,-26164,-26166,-26168,-26170,-26172,-26174,-26176,-26178,-26180,-26181,-26183,-26185,-26187,-26189,-26191,-26193,-26195,-26197,-26198,-26200,-26202,-26204,-26206,-26208,-26210,-26212,-26214,-26215,-26217,-26219,-26221,-26223,-26225,-26227,-26229,-26230,-26232,-26234,-26236,-26238,-26240,-26242,-26244,-26246,-26247,-26249,-26251,-26253,-26255,-26257,-26259,-26261,-26262,-26264,-26266,-26268,-26270,-26272,-26274,-26276,-26277,-26279,-26281,-26283,-26285,-26287,-26289,-26291,-26292,-26294,-26296,-26298,-26300,-26302,-26304,-26306,-26307,-26309,-26311,-26313,-26315,-26317,-26319,-26321,-26322,-26324,-26326,-26328,-26330,-26332,-26334,-26336,-26337,-26339,-26341,-26343,-26345,-26347,-26349,-26350,-26352,-26354,-26356,-26358,-26360,-26362,-26364,-26365,-26367,-26369,-26371,-26373,-26375,-26377,-26378,-26380,-26382,-26384,-26386,-26388,-26390,-26392,-26393,-26395,-26397,-26399,-26401,-26403,-26405,-26406,-26408,-26410,-26412,-26414,-26416,-26418,-26419,-26421,-26423,-26425,-26427,-26429,-26431,-26432,-26434,-26436,-26438,-26440,-26442,-26444,-26445,-26447,-26449,-26451,-26453,-26455,-26457,-26458,-26460,-26462,-26464,-26466,-26468,-26469,-26471,-26473,-26475,-26477,-26479,-26481,-26482,-26484,-26486,-26488,-26490,-26492,-26494,-26495,-26497,-26499,-26501,-26503,-26505,-26506,-26508,-26510,-26512,-26514,-26516,-26518,-26519,-26521,-26523,-26525,-26527,-26529,-26530,-26532,-26534,-26536,-26538,-26540,-26542,-26543,-26545,-26547,-26549,-26551,-26553,-26554,-26556,-26558,-26560,-26562,-26564,-26565,-26567,-26569,-26571,-26573,-26575,-26576,-26578,-26580,-26582,-26584,-26586,-26588,-26589,-26591,-26593,-26595,-26597,-26599,-26600,-26602,-26604,-26606,-26608,-26610,-26611,-26613,-26615,-26617,-26619,-26621,-26622,-26624,-26626,-26628,-26630,-26631,-26633,-26635,-26637,-26639,-26641,-26642,-26644,-26646,-26648,-26650,-26652,-26653,-26655,-26657,-26659,-26661,-26663,-26664,-26666,-26668,-26670,-26672,-26674,-26675,-26677,-26679,-26681,-26683,-26684,-26686,-26688,-26690,-26692,-26694,-26695,-26697,-26699,-26701,-26703,-26705,-26706,-26708,-26710,-26712,-26714,-26715,-26717,-26719,-26721,-26723,-26725,-26726,-26728,-26730,-26732,-26734,-26735,-26737,-26739,-26741,-26743,-26745,-26746,-26748,-26750,-26752,-26754,-26755,-26757,-26759,-26761,-26763,-26764,-26766,-26768,-26770,-26772,-26774,-26775,-26777,-26779,-26781,-26783,-26784,-26786,-26788,-26790,-26792,-26793,-26795,-26797,-26799,-26801,-26802,-26804,-26806,-26808,-26810,-26811,-26813,-26815,-26817,-26819,-26821,-26822,-26824,-26826,-26828,-26830,-26831,-26833,-26835,-26837,-26839,-26840,-26842,-26844,-26846,-26848,-26849,-26851,-26853,-26855,-26857,-26858,-26860,-26862,-26864,-26866,-26867,-26869,-26871,-26873,-26875,-26876,-26878,-26880,-26882,-26884,-26885,-26887,-26889,-26891,-26893,-26894,-26896,-26898,-26900,-26901,-26903,-26905,-26907,-26909,-26910,-26912,-26914,-26916,-26918,-26919,-26921,-26923,-26925,-26927,-26928,-26930,-26932,-26934,-26936,-26937,-26939,-26941,-26943,-26944,-26946,-26948,-26950,-26952,-26953,-26955,-26957,-26959,-26961,-26962,-26964,-26966,-26968,-26969,-26971,-26973,-26975,-26977,-26978,-26980,-26982,-26984,-26986,-26987,-26989,-26991,-26993,-26994,-26996,-26998,-27000,-27002,-27003,-27005,-27007,-27009,-27010,-27012,-27014,-27016,-27018,-27019,-27021,-27023,-27025,-27026,-27028,-27030,-27032,-27034,-27035,-27037,-27039,-27041,-27042,-27044,-27046,-27048,-27049,-27051,-27053,-27055,-27057,-27058,-27060,-27062,-27064,-27065,-27067,-27069,-27071,-27073,-27074,-27076,-27078,-27080,-27081,-27083,-27085,-27087,-27088,-27090,-27092,-27094,-27096,-27097,-27099,-27101,-27103,-27104,-27106,-27108,-27110,-27111,-27113,-27115,-27117,-27118,-27120,-27122,-27124,-27126,-27127,-27129,-27131,-27133,-27134,-27136,-27138,-27140,-27141,-27143,-27145,-27147,-27148,-27150,-27152,-27154,-27155,-27157,-27159,-27161,-27162,-27164,-27166,-27168,-27169,-27171,-27173,-27175,-27177,-27178,-27180,-27182,-27184,-27185,-27187,-27189,-27191,-27192,-27194,-27196,-27198,-27199,-27201,-27203,-27205,-27206,-27208,-27210,-27212,-27213,-27215,-27217,-27219,-27220,-27222,-27224,-27226,-27227,-27229,-27231,-27233,-27234,-27236,-27238,-27240,-27241,-27243,-27245,-27247,-27248,-27250,-27252,-27253,-27255,-27257,-27259,-27260,-27262,-27264,-27266,-27267,-27269,-27271,-27273,-27274,-27276,-27278,-27280,-27281,-27283,-27285,-27287,-27288,-27290,-27292,-27294,-27295,-27297,-27299,-27300,-27302,-27304,-27306,-27307,-27309,-27311,-27313,-27314,-27316,-27318,-27320,-27321,-27323,-27325,-27327,-27328,-27330,-27332,-27333,-27335,-27337,-27339,-27340,-27342,-27344,-27346,-27347,-27349,-27351,-27352,-27354,-27356,-27358,-27359,-27361,-27363,-27365,-27366,-27368,-27370,-27372,-27373,-27375,-27377,-27378,-27380,-27382,-27384,-27385,-27387,-27389,-27390,-27392,-27394,-27396,-27397,-27399,-27401,-27403,-27404,-27406,-27408,-27409,-27411,-27413,-27415,-27416,-27418,-27420,-27421,-27423,-27425,-27427,-27428,-27430,-27432,-27434,-27435,-27437,-27439,-27440,-27442,-27444,-27446,-27447,-27449,-27451,-27452,-27454,-27456,-27458,-27459,-27461,-27463,-27464,-27466,-27468,-27470,-27471,-27473,-27475,-27476,-27478,-27480,-27482,-27483,-27485,-27487,-27488,-27490,-27492,-27493,-27495,-27497,-27499,-27500,-27502,-27504,-27505,-27507,-27509,-27511,-27512,-27514,-27516,-27517,-27519,-27521,-27523,-27524,-27526,-27528,-27529,-27531,-27533,-27534,-27536,-27538,-27540,-27541,-27543,-27545,-27546,-27548,-27550,-27551,-27553,-27555,-27557,-27558,-27560,-27562,-27563,-27565,-27567,-27568,-27570,-27572,-27574,-27575,-27577,-27579,-27580,-27582,-27584,-27585,-27587,-27589,-27590,-27592,-27594,-27596,-27597,-27599,-27601,-27602,-27604,-27606,-27607,-27609,-27611,-27613,-27614,-27616,-27618,-27619,-27621,-27623,-27624,-27626,-27628,-27629,-27631,-27633,-27634,-27636,-27638,-27640,-27641,-27643,-27645,-27646,-27648,-27650,-27651,-27653,-27655,-27656,-27658,-27660,-27661,-27663,-27665,-27666,-27668,-27670,-27672,-27673,-27675,-27677,-27678,-27680,-27682,-27683,-27685,-27687,-27688,-27690,-27692,-27693,-27695,-27697,-27698,-27700,-27702,-27703,-27705,-27707,-27708,-27710,-27712,-27714,-27715,-27717,-27719,-27720,-27722,-27724,-27725,-27727,-27729,-27730,-27732,-27734,-27735,-27737,-27739,-27740,-27742,-27744,-27745,-27747,-27749,-27750,-27752,-27754,-27755,-27757,-27759,-27760,-27762,-27764,-27765,-27767,-27769,-27770,-27772,-27774,-27775,-27777,-27779,-27780,-27782,-27784,-27785,-27787,-27789,-27790,-27792,-27794,-27795,-27797,-27799,-27800,-27802,-27804,-27805,-27807,-27809,-27810,-27812,-27814,-27815,-27817,-27819,-27820,-27822,-27824,-27825,-27827,-27829,-27830,-27832,-27834,-27835,-27837,-27839,-27840,-27842,-27843,-27845,-27847,-27848,-27850,-27852,-27853,-27855,-27857,-27858,-27860,-27862,-27863,-27865,-27867,-27868,-27870,-27872,-27873,-27875,-27877,-27878,-27880,-27882,-27883,-27885,-27886,-27888,-27890,-27891,-27893,-27895,-27896,-27898,-27900,-27901,-27903,-27905,-27906,-27908,-27910,-27911,-27913,-27914,-27916,-27918,-27919,-27921,-27923,-27924,-27926,-27928,-27929,-27931,-27933,-27934,-27936,-27937,-27939,-27941,-27942,-27944,-27946,-27947,-27949,-27951,-27952,-27954,-27956,-27957,-27959,-27960,-27962,-27964,-27965,-27967,-27969,-27970,-27972,-27974,-27975,-27977,-27978,-27980,-27982,-27983,-27985,-27987,-27988,-27990,-27992,-27993,-27995,-27996,-27998,-28000,-28001,-28003,-28005,-28006,-28008,-28009,-28011,-28013,-28014,-28016,-28018,-28019,-28021,-28022,-28024,-28026,-28027,-28029,-28031,-28032,-28034,-28036,-28037,-28039,-28040,-28042,-28044,-28045,-28047,-28049,-28050,-28052,-28053,-28055,-28057,-28058,-28060,-28061,-28063,-28065,-28066,-28068,-28070,-28071,-28073,-28074,-28076,-28078,-28079,-28081,-28083,-28084,-28086,-28087,-28089,-28091,-28092,-28094,-28095,-28097,-28099,-28100,-28102,-28104,-28105,-28107,-28108,-28110,-28112,-28113,-28115,-28116,-28118,-28120,-28121,-28123,-28125,-28126,-28128,-28129,-28131,-28133,-28134,-28136,-28137,-28139,-28141,-28142,-28144,-28145,-28147,-28149,-28150,-28152,-28154,-28155,-28157,-28158,-28160,-28162,-28163,-28165,-28166,-28168,-28170,-28171,-28173,-28174,-28176,-28178,-28179,-28181,-28182,-28184,-28186,-28187,-28189,-28190,-28192,-28194,-28195,-28197,-28198,-28200,-28202,-28203,-28205,-28206,-28208,-28210,-28211,-28213,-28214,-28216,-28218,-28219,-28221,-28222,-28224,-28226,-28227,-28229,-28230,-28232,-28234,-28235,-28237,-28238,-28240,-28242,-28243,-28245,-28246,-28248,-28249,-28251,-28253,-28254,-28256,-28257,-28259,-28261,-28262,-28264,-28265,-28267,-28269,-28270,-28272,-28273,-28275,-28277,-28278,-28280,-28281,-28283,-28284,-28286,-28288,-28289,-28291,-28292,-28294,-28296,-28297,-28299,-28300,-28302,-28303,-28305,-28307,-28308,-28310,-28311,-28313,-28315,-28316,-28318,-28319,-28321,-28322,-28324,-28326,-28327,-28329,-28330,-28332,-28333,-28335,-28337,-28338,-28340,-28341,-28343,-28345,-28346,-28348,-28349,-28351,-28352,-28354,-28356,-28357,-28359,-28360,-28362,-28363,-28365,-28367,-28368,-28370,-28371,-28373,-28374,-28376,-28378,-28379,-28381,-28382,-28384,-28385,-28387,-28389,-28390,-28392,-28393,-28395,-28396,-28398,-28400,-28401,-28403,-28404,-28406,-28407,-28409,-28411,-28412,-28414,-28415,-28417,-28418,-28420,-28421,-28423,-28425,-28426,-28428,-28429,-28431,-28432,-28434,-28436,-28437,-28439,-28440,-28442,-28443,-28445,-28446,-28448,-28450,-28451,-28453,-28454,-28456,-28457,-28459,-28460,-28462,-28464,-28465,-28467,-28468,-28470,-28471,-28473,-28474,-28476,-28478,-28479,-28481,-28482,-28484,-28485,-28487,-28488,-28490,-28492,-28493,-28495,-28496,-28498,-28499,-28501,-28502,-28504,-28505,-28507,-28509,-28510,-28512,-28513,-28515,-28516,-28518,-28519,-28521,-28523,-28524,-28526,-28527,-28529,-28530,-28532,-28533,-28535,-28536,-28538,-28540,-28541,-28543,-28544,-28546,-28547,-28549,-28550,-28552,-28553,-28555,-28556,-28558,-28560,-28561,-28563,-28564,-28566,-28567,-28569,-28570,-28572,-28573,-28575,-28576,-28578,-28580,-28581,-28583,-28584,-28586,-28587,-28589,-28590,-28592,-28593,-28595,-28596,-28598,-28600,-28601,-28603,-28604,-28606,-28607,-28609,-28610,-28612,-28613,-28615,-28616,-28618,-28619,-28621,-28622,-28624,-28626,-28627,-28629,-28630,-28632,-28633,-28635,-28636,-28638,-28639,-28641,-28642,-28644,-28645,-28647,-28648,-28650,-28651,-28653,-28655,-28656,-28658,-28659,-28661,-28662,-28664,-28665,-28667,-28668,-28670,-28671,-28673,-28674,-28676,-28677,-28679,-28680,-28682,-28683,-28685,-28686,-28688,-28690,-28691,-28693,-28694,-28696,-28697,-28699,-28700,-28702,-28703,-28705,-28706,-28708,-28709,-28711,-28712,-28714,-28715,-28717,-28718,-28720,-28721,-28723,-28724,-28726,-28727,-28729,-28730,-28732,-28733,-28735,-28736,-28738,-28739,-28741,-28742,-28744,-28745,-28747,-28748,-28750,-28752,-28753,-28755,-28756,-28758,-28759,-28761,-28762,-28764,-28765,-28767,-28768,-28770,-28771,-28773,-28774,-28776,-28777,-28779,-28780,-28782,-28783,-28785,-28786,-28788,-28789,-28791,-28792,-28794,-28795,-28797,-28798,-28800,-28801,-28803,-28804,-28806,-28807,-28809,-28810,-28812,-28813,-28815,-28816,-28818,-28819,-28821,-28822,-28824,-28825,-28827,-28828,-28830,-28831,-28832,-28834,-28835,-28837,-28838,-28840,-28841,-28843,-28844,-28846,-28847,-28849,-28850,-28852,-28853,-28855,-28856,-28858,-28859,-28861,-28862,-28864,-28865,-28867,-28868,-28870,-28871,-28873,-28874,-28876,-28877,-28879,-28880,-28882,-28883,-28885,-28886,-28888,-28889,-28891,-28892,-28893,-28895,-28896,-28898,-28899,-28901,-28902,-28904,-28905,-28907,-28908,-28910,-28911,-28913,-28914,-28916,-28917,-28919,-28920,-28922,-28923,-28925,-28926,-28927,-28929,-28930,-28932,-28933,-28935,-28936,-28938,-28939,-28941,-28942,-28944,-28945,-28947,-28948,-28950,-28951,-28953,-28954,-28955,-28957,-28958,-28960,-28961,-28963,-28964,-28966,-28967,-28969,-28970,-28972,-28973,-28975,-28976,-28977,-28979,-28980,-28982,-28983,-28985,-28986,-28988,-28989,-28991,-28992,-28994,-28995,-28997,-28998,-28999,-29001,-29002,-29004,-29005,-29007,-29008,-29010,-29011,-29013,-29014,-29016,-29017,-29018,-29020,-29021,-29023,-29024,-29026,-29027,-29029,-29030,-29032,-29033,-29034,-29036,-29037,-29039,-29040,-29042,-29043,-29045,-29046,-29048,-29049,-29050,-29052,-29053,-29055,-29056,-29058,-29059,-29061,-29062,-29064,-29065,-29066,-29068,-29069,-29071,-29072,-29074,-29075,-29077,-29078,-29079,-29081,-29082,-29084,-29085,-29087,-29088,-29090,-29091,-29093,-29094,-29095,-29097,-29098,-29100,-29101,-29103,-29104,-29106,-29107,-29108,-29110,-29111,-29113,-29114,-29116,-29117,-29118,-29120,-29121,-29123,-29124,-29126,-29127,-29129,-29130,-29131,-29133,-29134,-29136,-29137,-29139,-29140,-29142,-29143,-29144,-29146,-29147,-29149,-29150,-29152,-29153,-29154,-29156,-29157,-29159,-29160,-29162,-29163,-29164,-29166,-29167,-29169,-29170,-29172,-29173,-29174,-29176,-29177,-29179,-29180,-29182,-29183,-29184,-29186,-29187,-29189,-29190,-29192,-29193,-29194,-29196,-29197,-29199,-29200,-29202,-29203,-29204,-29206,-29207,-29209,-29210,-29212,-29213,-29214,-29216,-29217,-29219,-29220,-29222,-29223,-29224,-29226,-29227,-29229,-29230,-29231,-29233,-29234,-29236,-29237,-29239,-29240,-29241,-29243,-29244,-29246,-29247,-29248,-29250,-29251,-29253,-29254,-29256,-29257,-29258,-29260,-29261,-29263,-29264,-29265,-29267,-29268,-29270,-29271,-29273,-29274,-29275,-29277,-29278,-29280,-29281,-29282,-29284,-29285,-29287,-29288,-29289,-29291,-29292,-29294,-29295,-29296,-29298,-29299,-29301,-29302,-29304,-29305,-29306,-29308,-29309,-29311,-29312,-29313,-29315,-29316,-29318,-29319,-29320,-29322,-29323,-29325,-29326,-29327,-29329,-29330,-29332,-29333,-29334,-29336,-29337,-29339,-29340,-29341,-29343,-29344,-29346,-29347,-29348,-29350,-29351,-29353,-29354,-29355,-29357,-29358,-29360,-29361,-29362,-29364,-29365,-29366,-29368,-29369,-29371,-29372,-29373,-29375,-29376,-29378,-29379,-29380,-29382,-29383,-29385,-29386,-29387,-29389,-29390,-29392,-29393,-29394,-29396,-29397,-29398,-29400,-29401,-29403,-29404,-29405,-29407,-29408,-29410,-29411,-29412,-29414,-29415,-29416,-29418,-29419,-29421,-29422,-29423,-29425,-29426,-29428,-29429,-29430,-29432,-29433,-29434,-29436,-29437,-29439,-29440,-29441,-29443,-29444,-29445,-29447,-29448,-29450,-29451,-29452,-29454,-29455,-29457,-29458,-29459,-29461,-29462,-29463,-29465,-29466,-29468,-29469,-29470,-29472,-29473,-29474,-29476,-29477,-29478,-29480,-29481,-29483,-29484,-29485,-29487,-29488,-29489,-29491,-29492,-29494,-29495,-29496,-29498,-29499,-29500,-29502,-29503,-29504,-29506,-29507,-29509,-29510,-29511,-29513,-29514,-29515,-29517,-29518,-29520,-29521,-29522,-29524,-29525,-29526,-29528,-29529,-29530,-29532,-29533,-29534,-29536,-29537,-29539,-29540,-29541,-29543,-29544,-29545,-29547,-29548,-29549,-29551,-29552,-29554,-29555,-29556,-29558,-29559,-29560,-29562,-29563,-29564,-29566,-29567,-29568,-29570,-29571,-29572,-29574,-29575,-29577,-29578,-29579,-29581,-29582,-29583,-29585,-29586,-29587,-29589,-29590,-29591,-29593,-29594,-29595,-29597,-29598,-29599,-29601,-29602,-29604,-29605,-29606,-29608,-29609,-29610,-29612,-29613,-29614,-29616,-29617,-29618,-29620,-29621,-29622,-29624,-29625,-29626,-29628,-29629,-29630,-29632,-29633,-29634,-29636,-29637,-29638,-29640,-29641,-29642,-29644,-29645,-29646,-29648,-29649,-29651,-29652,-29653,-29655,-29656,-29657,-29659,-29660,-29661,-29663,-29664,-29665,-29667,-29668,-29669,-29671,-29672,-29673,-29675,-29676,-29677,-29679,-29680,-29681,-29683,-29684,-29685,-29687,-29688,-29689,-29690,-29692,-29693,-29694,-29696,-29697,-29698,-29700,-29701,-29702,-29704,-29705,-29706,-29708,-29709,-29710,-29712,-29713,-29714,-29716,-29717,-29718,-29720,-29721,-29722,-29724,-29725,-29726,-29728,-29729,-29730,-29732,-29733,-29734,-29736,-29737,-29738,-29739,-29741,-29742,-29743,-29745,-29746,-29747,-29749,-29750,-29751,-29753,-29754,-29755,-29757,-29758,-29759,-29761,-29762,-29763,-29764,-29766,-29767,-29768,-29770,-29771,-29772,-29774,-29775,-29776,-29778,-29779,-29780,-29782,-29783,-29784,-29785,-29787,-29788,-29789,-29791,-29792,-29793,-29795,-29796,-29797,-29799,-29800,-29801,-29802,-29804,-29805,-29806,-29808,-29809,-29810,-29812,-29813,-29814,-29816,-29817,-29818,-29819,-29821,-29822,-29823,-29825,-29826,-29827,-29829,-29830,-29831,-29832,-29834,-29835,-29836,-29838,-29839,-29840,-29842,-29843,-29844,-29845,-29847,-29848,-29849,-29851,-29852,-29853,-29854,-29856,-29857,-29858,-29860,-29861,-29862,-29864,-29865,-29866,-29867,-29869,-29870,-29871,-29873,-29874,-29875,-29876,-29878,-29879,-29880,-29882,-29883,-29884,-29885,-29887,-29888,-29889,-29891,-29892,-29893,-29894,-29896,-29897,-29898,-29900,-29901,-29902,-29903,-29905,-29906,-29907,-29909,-29910,-29911,-29912,-29914,-29915,-29916,-29918,-29919,-29920,-29921,-29923,-29924,-29925,-29927,-29928,-29929,-29930,-29932,-29933,-29934,-29936,-29937,-29938,-29939,-29941,-29942,-29943,-29944,-29946,-29947,-29948,-29950,-29951,-29952,-29953,-29955,-29956,-29957,-29958,-29960,-29961,-29962,-29964,-29965,-29966,-29967,-29969,-29970,-29971,-29972,-29974,-29975,-29976,-29978,-29979,-29980,-29981,-29983,-29984,-29985,-29986,-29988,-29989,-29990,-29991,-29993,-29994,-29995,-29997,-29998,-29999,-30000,-30002,-30003,-30004,-30005,-30007,-30008,-30009,-30010,-30012,-30013,-30014,-30015,-30017,-30018,-30019,-30020,-30022,-30023,-30024,-30026,-30027,-30028,-30029,-30031,-30032,-30033,-30034,-30036,-30037,-30038,-30039,-30041,-30042,-30043,-30044,-30046,-30047,-30048,-30049,-30051,-30052,-30053,-30054,-30056,-30057,-30058,-30059,-30061,-30062,-30063,-30064,-30066,-30067,-30068,-30069,-30071,-30072,-30073,-30074,-30076,-30077,-30078,-30079,-30081,-30082,-30083,-30084,-30086,-30087,-30088,-30089,-30091,-30092,-30093,-30094,-30096,-30097,-30098,-30099,-30100,-30102,-30103,-30104,-30105,-30107,-30108,-30109,-30110,-30112,-30113,-30114,-30115,-30117,-30118,-30119,-30120,-30122,-30123,-30124,-30125,-30126,-30128,-30129,-30130,-30131,-30133,-30134,-30135,-30136,-30138,-30139,-30140,-30141,-30143,-30144,-30145,-30146,-30147,-30149,-30150,-30151,-30152,-30154,-30155,-30156,-30157,-30159,-30160,-30161,-30162,-30163,-30165,-30166,-30167,-30168,-30170,-30171,-30172,-30173,-30174,-30176,-30177,-30178,-30179,-30181,-30182,-30183,-30184,-30185,-30187,-30188,-30189,-30190,-30192,-30193,-30194,-30195,-30196,-30198,-30199,-30200,-30201,-30203,-30204,-30205,-30206,-30207,-30209,-30210,-30211,-30212,-30214,-30215,-30216,-30217,-30218,-30220,-30221,-30222,-30223,-30224,-30226,-30227,-30228,-30229,-30231,-30232,-30233,-30234,-30235,-30237,-30238,-30239,-30240,-30241,-30243,-30244,-30245,-30246,-30247,-30249,-30250,-30251,-30252,-30253,-30255,-30256,-30257,-30258,-30260,-30261,-30262,-30263,-30264,-30266,-30267,-30268,-30269,-30270,-30272,-30273,-30274,-30275,-30276,-30278,-30279,-30280,-30281,-30282,-30284,-30285,-30286,-30287,-30288,-30290,-30291,-30292,-30293,-30294,-30296,-30297,-30298,-30299,-30300,-30302,-30303,-30304,-30305,-30306,-30308,-30309,-30310,-30311,-30312,-30313,-30315,-30316,-30317,-30318,-30319,-30321,-30322,-30323,-30324,-30325,-30327,-30328,-30329,-30330,-30331,-30333,-30334,-30335,-30336,-30337,-30338,-30340,-30341,-30342,-30343,-30344,-30346,-30347,-30348,-30349,-30350,-30351,-30353,-30354,-30355,-30356,-30357,-30359,-30360,-30361,-30362,-30363,-30365,-30366,-30367,-30368,-30369,-30370,-30372,-30373,-30374,-30375,-30376,-30377,-30379,-30380,-30381,-30382,-30383,-30385,-30386,-30387,-30388,-30389,-30390,-30392,-30393,-30394,-30395,-30396,-30397,-30399,-30400,-30401,-30402,-30403,-30404,-30406,-30407,-30408,-30409,-30410,-30412,-30413,-30414,-30415,-30416,-30417,-30419,-30420,-30421,-30422,-30423,-30424,-30426,-30427,-30428,-30429,-30430,-30431,-30433,-30434,-30435,-30436,-30437,-30438,-30439,-30441,-30442,-30443,-30444,-30445,-30446,-30448,-30449,-30450,-30451,-30452,-30453,-30455,-30456,-30457,-30458,-30459,-30460,-30462,-30463,-30464,-30465,-30466,-30467,-30468,-30470,-30471,-30472,-30473,-30474,-30475,-30477,-30478,-30479,-30480,-30481,-30482,-30483,-30485,-30486,-30487,-30488,-30489,-30490,-30492,-30493,-30494,-30495,-30496,-30497,-30498,-30500,-30501,-30502,-30503,-30504,-30505,-30506,-30508,-30509,-30510,-30511,-30512,-30513,-30514,-30516,-30517,-30518,-30519,-30520,-30521,-30522,-30524,-30525,-30526,-30527,-30528,-30529,-30530,-30532,-30533,-30534,-30535,-30536,-30537,-30538,-30540,-30541,-30542,-30543,-30544,-30545,-30546,-30548,-30549,-30550,-30551,-30552,-30553,-30554,-30556,-30557,-30558,-30559,-30560,-30561,-30562,-30563,-30565,-30566,-30567,-30568,-30569,-30570,-30571,-30573,-30574,-30575,-30576,-30577,-30578,-30579,-30580,-30582,-30583,-30584,-30585,-30586,-30587,-30588,-30589,-30591,-30592,-30593,-30594,-30595,-30596,-30597,-30598,-30600,-30601,-30602,-30603,-30604,-30605,-30606,-30607,-30609,-30610,-30611,-30612,-30613,-30614,-30615,-30616,-30617,-30619,-30620,-30621,-30622,-30623,-30624,-30625,-30626,-30628,-30629,-30630,-30631,-30632,-30633,-30634,-30635,-30636,-30638,-30639,-30640,-30641,-30642,-30643,-30644,-30645,-30646,-30648,-30649,-30650,-30651,-30652,-30653,-30654,-30655,-30656,-30658,-30659,-30660,-30661,-30662,-30663,-30664,-30665,-30666,-30668,-30669,-30670,-30671,-30672,-30673,-30674,-30675,-30676,-30678,-30679,-30680,-30681,-30682,-30683,-30684,-30685,-30686,-30687,-30689,-30690,-30691,-30692,-30693,-30694,-30695,-30696,-30697,-30698,-30700,-30701,-30702,-30703,-30704,-30705,-30706,-30707,-30708,-30709,-30711,-30712,-30713,-30714,-30715,-30716,-30717,-30718,-30719,-30720,-30721,-30723,-30724,-30725,-30726,-30727,-30728,-30729,-30730,-30731,-30732,-30733,-30735,-30736,-30737,-30738,-30739,-30740,-30741,-30742,-30743,-30744,-30745,-30746,-30748,-30749,-30750,-30751,-30752,-30753,-30754,-30755,-30756,-30757,-30758,-30760,-30761,-30762,-30763,-30764,-30765,-30766,-30767,-30768,-30769,-30770,-30771,-30772,-30774,-30775,-30776,-30777,-30778,-30779,-30780,-30781,-30782,-30783,-30784,-30785,-30786,-30788,-30789,-30790,-30791,-30792,-30793,-30794,-30795,-30796,-30797,-30798,-30799,-30800,-30802,-30803,-30804,-30805,-30806,-30807,-30808,-30809,-30810,-30811,-30812,-30813,-30814,-30815,-30816,-30818,-30819,-30820,-30821,-30822,-30823,-30824,-30825,-30826,-30827,-30828,-30829,-30830,-30831,-30832,-30834,-30835,-30836,-30837,-30838,-30839,-30840,-30841,-30842,-30843,-30844,-30845,-30846,-30847,-30848,-30849,-30851,-30852,-30853,-30854,-30855,-30856,-30857,-30858,-30859,-30860,-30861,-30862,-30863,-30864,-30865,-30866,-30867,-30868,-30870,-30871,-30872,-30873,-30874,-30875,-30876,-30877,-30878,-30879,-30880,-30881,-30882,-30883,-30884,-30885,-30886,-30887,-30888,-30889,-30891,-30892,-30893,-30894,-30895,-30896,-30897,-30898,-30899,-30900,-30901,-30902,-30903,-30904,-30905,-30906,-30907,-30908,-30909,-30910,-30911,-30912,-30914,-30915,-30916,-30917,-30918,-30919,-30920,-30921,-30922,-30923,-30924,-30925,-30926,-30927,-30928,-30929,-30930,-30931,-30932,-30933,-30934,-30935,-30936,-30937,-30938,-30939,-30941,-30942,-30943,-30944,-30945,-30946,-30947,-30948,-30949,-30950,-30951,-30952,-30953,-30954,-30955,-30956,-30957,-30958,-30959,-30960,-30961,-30962,-30963,-30964,-30965,-30966,-30967,-30968,-30969,-30970,-30971,-30972,-30973,-30974,-30976,-30977,-30978,-30979,-30980,-30981,-30982,-30983,-30984,-30985,-30986,-30987,-30988,-30989,-30990,-30991,-30992,-30993,-30994,-30995,-30996,-30997,-30998,-30999,-31000,-31001,-31002,-31003,-31004,-31005,-31006,-31007,-31008,-31009,-31010,-31011,-31012,-31013,-31014,-31015,-31016,-31017,-31018,-31019,-31020,-31021,-31022,-31023,-31024,-31025,-31026,-31027,-31028,-31029,-31030,-31031,-31032,-31033,-31034,-31035,-31036,-31037,-31038,-31039,-31040,-31041,-31043,-31044,-31045,-31046,-31047,-31048,-31049,-31050,-31051,-31052,-31053,-31054,-31055,-31056,-31057,-31058,-31059,-31060,-31061,-31062,-31063,-31064,-31065,-31066,-31067,-31068,-31069,-31070,-31071,-31072,-31073,-31074,-31075,-31076,-31077,-31078,-31079,-31080,-31081,-31082,-31083,-31083,-31084,-31085,-31086,-31087,-31088,-31089,-31090,-31091,-31092,-31093,-31094,-31095,-31096,-31097,-31098,-31099,-31100,-31101,-31102,-31103,-31104,-31105,-31106,-31107,-31108,-31109,-31110,-31111,-31112,-31113,-31114,-31115,-31116,-31117,-31118,-31119,-31120,-31121,-31122,-31123,-31124,-31125,-31126,-31127,-31128,-31129,-31130,-31131,-31132,-31133,-31134,-31135,-31136,-31137,-31138,-31139,-31140,-31141,-31142,-31143,-31144,-31145,-31146,-31147,-31148,-31148,-31149,-31150,-31151,-31152,-31153,-31154,-31155,-31156,-31157,-31158,-31159,-31160,-31161,-31162,-31163,-31164,-31165,-31166,-31167,-31168,-31169,-31170,-31171,-31172,-31173,-31174,-31175,-31176,-31177,-31178,-31179,-31180,-31181,-31181,-31182,-31183,-31184,-31185,-31186,-31187,-31188,-31189,-31190,-31191,-31192,-31193,-31194,-31195,-31196,-31197,-31198,-31199,-31200,-31201,-31202,-31203,-31204,-31205,-31206,-31206,-31207,-31208,-31209,-31210,-31211,-31212,-31213,-31214,-31215,-31216,-31217,-31218,-31219,-31220,-31221,-31222,-31223,-31224,-31225,-31226,-31227,-31227,-31228,-31229,-31230,-31231,-31232,-31233,-31234,-31235,-31236,-31237,-31238,-31239,-31240,-31241,-31242,-31243,-31244,-31245,-31246,-31246,-31247,-31248,-31249,-31250,-31251,-31252,-31253,-31254,-31255,-31256,-31257,-31258,-31259,-31260,-31261,-31262,-31262,-31263,-31264,-31265,-31266,-31267,-31268,-31269,-31270,-31271,-31272,-31273,-31274,-31275,-31276,-31277,-31278,-31278,-31279,-31280,-31281,-31282,-31283,-31284,-31285,-31286,-31287,-31288,-31289,-31290,-31291,-31292,-31292,-31293,-31294,-31295,-31296,-31297,-31298,-31299,-31300,-31301,-31302,-31303,-31304,-31305,-31305,-31306,-31307,-31308,-31309,-31310,-31311,-31312,-31313,-31314,-31315,-31316,-31317,-31318,-31318,-31319,-31320,-31321,-31322,-31323,-31324,-31325,-31326,-31327,-31328,-31329,-31329,-31330,-31331,-31332,-31333,-31334,-31335,-31336,-31337,-31338,-31339,-31340,-31341,-31341,-31342,-31343,-31344,-31345,-31346,-31347,-31348,-31349,-31350,-31351,-31352,-31352,-31353,-31354,-31355,-31356,-31357,-31358,-31359,-31360,-31361,-31362,-31362,-31363,-31364,-31365,-31366,-31367,-31368,-31369,-31370,-31371,-31372,-31372,-31373,-31374,-31375,-31376,-31377,-31378,-31379,-31380,-31381,-31381,-31382,-31383,-31384,-31385,-31386,-31387,-31388,-31389,-31390,-31391,-31391,-31392,-31393,-31394,-31395,-31396,-31397,-31398,-31399,-31400,-31400,-31401,-31402,-31403,-31404,-31405,-31406,-31407,-31408,-31408,-31409,-31410,-31411,-31412,-31413,-31414,-31415,-31416,-31417,-31417,-31418,-31419,-31420,-31421,-31422,-31423,-31424,-31425,-31425,-31426,-31427,-31428,-31429,-31430,-31431,-31432,-31433,-31433,-31434,-31435,-31436,-31437,-31438,-31439,-31440,-31441,-31441,-31442,-31443,-31444,-31445,-31446,-31447,-31448,-31448,-31449,-31450,-31451,-31452,-31453,-31454,-31455,-31456,-31456,-31457,-31458,-31459,-31460,-31461,-31462,-31463,-31463,-31464,-31465,-31466,-31467,-31468,-31469,-31470,-31470,-31471,-31472,-31473,-31474,-31475,-31476,-31477,-31477,-31478,-31479,-31480,-31481,-31482,-31483,-31484,-31484,-31485,-31486,-31487,-31488,-31489,-31490,-31490,-31491,-31492,-31493,-31494,-31495,-31496,-31497,-31497,-31498,-31499,-31500,-31501,-31502,-31503,-31503,-31504,-31505,-31506,-31507,-31508,-31509,-31510,-31510,-31511,-31512,-31513,-31514,-31515,-31516,-31516,-31517,-31518,-31519,-31520,-31521,-31522,-31522,-31523,-31524,-31525,-31526,-31527,-31528,-31528,-31529,-31530,-31531,-31532,-31533,-31534,-31534,-31535,-31536,-31537,-31538,-31539,-31539,-31540,-31541,-31542,-31543,-31544,-31545,-31545,-31546,-31547,-31548,-31549,-31550,-31551,-31551,-31552,-31553,-31554,-31555,-31556,-31556,-31557,-31558,-31559,-31560,-31561,-31562,-31562,-31563,-31564,-31565,-31566,-31567,-31567,-31568,-31569,-31570,-31571,-31572,-31572,-31573,-31574,-31575,-31576,-31577,-31578,-31578,-31579,-31580,-31581,-31582,-31583,-31583,-31584,-31585,-31586,-31587,-31588,-31588,-31589,-31590,-31591,-31592,-31593,-31593,-31594,-31595,-31596,-31597,-31598,-31598,-31599,-31600,-31601,-31602,-31603,-31603,-31604,-31605,-31606,-31607,-31608,-31608,-31609,-31610,-31611,-31612,-31613,-31613,-31614,-31615,-31616,-31617,-31617,-31618,-31619,-31620,-31621,-31622,-31622,-31623,-31624,-31625,-31626,-31627,-31627,-31628,-31629,-31630,-31631,-31631,-31632,-31633,-31634,-31635,-31636,-31636,-31637,-31638,-31639,-31640,-31640,-31641,-31642,-31643,-31644,-31645,-31645,-31646,-31647,-31648,-31649,-31649,-31650,-31651,-31652,-31653,-31653,-31654,-31655,-31656,-31657,-31658,-31658,-31659,-31660,-31661,-31662,-31662,-31663,-31664,-31665,-31666,-31666,-31667,-31668,-31669,-31670,-31670,-31671,-31672,-31673,-31674,-31674,-31675,-31676,-31677,-31678,-31679,-31679,-31680,-31681,-31682,-31683,-31683,-31684,-31685,-31686,-31687,-31687,-31688,-31689,-31690,-31691,-31691,-31692,-31693,-31694,-31695,-31695,-31696,-31697,-31698,-31698,-31699,-31700,-31701,-31702,-31702,-31703,-31704,-31705,-31706,-31706,-31707,-31708,-31709,-31710,-31710,-31711,-31712,-31713,-31714,-31714,-31715,-31716,-31717,-31718,-31718,-31719,-31720,-31721,-31721,-31722,-31723,-31724,-31725,-31725,-31726,-31727,-31728,-31729,-31729,-31730,-31731,-31732,-31732,-31733,-31734,-31735,-31736,-31736,-31737,-31738,-31739,-31739,-31740,-31741,-31742,-31743,-31743,-31744,-31745,-31746,-31746,-31747,-31748,-31749,-31750,-31750,-31751,-31752,-31753,-31753,-31754,-31755,-31756,-31757,-31757,-31758,-31759,-31760,-31760,-31761,-31762,-31763,-31764,-31764,-31765,-31766,-31767,-31767,-31768,-31769,-31770,-31770,-31771,-31772,-31773,-31774,-31774,-31775,-31776,-31777,-31777,-31778,-31779,-31780,-31780,-31781,-31782,-31783,-31783,-31784,-31785,-31786,-31787,-31787,-31788,-31789,-31790,-31790,-31791,-31792,-31793,-31793,-31794,-31795,-31796,-31796,-31797,-31798,-31799,-31799,-31800,-31801,-31802,-31802,-31803,-31804,-31805,-31806,-31806,-31807,-31808,-31809,-31809,-31810,-31811,-31812,-31812,-31813,-31814,-31815,-31815,-31816,-31817,-31818,-31818,-31819,-31820,-31821,-31821,-31822,-31823,-31824,-31824,-31825,-31826,-31827,-31827,-31828,-31829,-31830,-31830,-31831,-31832,-31833,-31833,-31834,-31835,-31836,-31836,-31837,-31838,-31838,-31839,-31840,-31841,-31841,-31842,-31843,-31844,-31844,-31845,-31846,-31847,-31847,-31848,-31849,-31850,-31850,-31851,-31852,-31853,-31853,-31854,-31855,-31855,-31856,-31857,-31858,-31858,-31859,-31860,-31861,-31861,-31862,-31863,-31864,-31864,-31865,-31866,-31866,-31867,-31868,-31869,-31869,-31870,-31871,-31872,-31872,-31873,-31874,-31875,-31875,-31876,-31877,-31877,-31878,-31879,-31880,-31880,-31881,-31882,-31882,-31883,-31884,-31885,-31885,-31886,-31887,-31888,-31888,-31889,-31890,-31890,-31891,-31892,-31893,-31893,-31894,-31895,-31896,-31896,-31897,-31898,-31898,-31899,-31900,-31901,-31901,-31902,-31903,-31903,-31904,-31905,-31906,-31906,-31907,-31908,-31908,-31909,-31910,-31911,-31911,-31912,-31913,-31913,-31914,-31915,-31916,-31916,-31917,-31918,-31918,-31919,-31920,-31921,-31921,-31922,-31923,-31923,-31924,-31925,-31925,-31926,-31927,-31928,-31928,-31929,-31930,-31930,-31931,-31932,-31933,-31933,-31934,-31935,-31935,-31936,-31937,-31937,-31938,-31939,-31940,-31940,-31941,-31942,-31942,-31943,-31944,-31944,-31945,-31946,-31947,-31947,-31948,-31949,-31949,-31950,-31951,-31951,-31952,-31953,-31954,-31954,-31955,-31956,-31956,-31957,-31958,-31958,-31959,-31960,-31960,-31961,-31962,-31963,-31963,-31964,-31965,-31965,-31966,-31967,-31967,-31968,-31969,-31969,-31970,-31971,-31972,-31972,-31973,-31974,-31974,-31975,-31976,-31976,-31977,-31978,-31978,-31979,-31980,-31980,-31981,-31982,-31982,-31983,-31984,-31985,-31985,-31986,-31987,-31987,-31988,-31989,-31989,-31990,-31991,-31991,-31992,-31993,-31993,-31994,-31995,-31995,-31996,-31997,-31997,-31998,-31999,-31999,-32000,-32001,-32002,-32002,-32003,-32004,-32004,-32005,-32006,-32006,-32007,-32008,-32008,-32009,-32010,-32010,-32011,-32012,-32012,-32013,-32014,-32014,-32015,-32016,-32016,-32017,-32018,-32018,-32019,-32020,-32020,-32021,-32022,-32022,-32023,-32024,-32024,-32025,-32026,-32026,-32027,-32028,-32028,-32029,-32030,-32030,-32031,-32032,-32032,-32033,-32034,-32034,-32035,-32036,-32036,-32037,-32038,-32038,-32039,-32040,-32040,-32041,-32041,-32042,-32043,-32043,-32044,-32045,-32045,-32046,-32047,-32047,-32048,-32049,-32049,-32050,-32051,-32051,-32052,-32053,-32053,-32054,-32055,-32055,-32056,-32057,-32057,-32058,-32058,-32059,-32060,-32060,-32061,-32062,-32062,-32063,-32064,-32064,-32065,-32066,-32066,-32067,-32068,-32068,-32069,-32069,-32070,-32071,-32071,-32072,-32073,-32073,-32074,-32075,-32075,-32076,-32077,-32077,-32078,-32078,-32079,-32080,-32080,-32081,-32082,-32082,-32083,-32084,-32084,-32085,-32086,-32086,-32087,-32087,-32088,-32089,-32089,-32090,-32091,-32091,-32092,-32092,-32093,-32094,-32094,-32095,-32096,-32096,-32097,-32098,-32098,-32099,-32099,-32100,-32101,-32101,-32102,-32103,-32103,-32104,-32104,-32105,-32106,-32106,-32107,-32108,-32108,-32109,-32110,-32110,-32111,-32111,-32112,-32113,-32113,-32114,-32115,-32115,-32116,-32116,-32117,-32118,-32118,-32119,-32119,-32120,-32121,-32121,-32122,-32123,-32123,-32124,-32124,-32125,-32126,-32126,-32127,-32128,-32128,-32129,-32129,-32130,-32131,-32131,-32132,-32132,-32133,-32134,-32134,-32135,-32136,-32136,-32137,-32137,-32138,-32139,-32139,-32140,-32140,-32141,-32142,-32142,-32143,-32144,-32144,-32145,-32145,-32146,-32147,-32147,-32148,-32148,-32149,-32150,-32150,-32151,-32151,-32152,-32153,-32153,-32154,-32154,-32155,-32156,-32156,-32157,-32157,-32158,-32159,-32159,-32160,-32160,-32161,-32162,-32162,-32163,-32163,-32164,-32165,-32165,-32166,-32166,-32167,-32168,-32168,-32169,-32169,-32170,-32171,-32171,-32172,-32172,-32173,-32174,-32174,-32175,-32175,-32176,-32177,-32177,-32178,-32178,-32179,-32180,-32180,-32181,-32181,-32182,-32183,-32183,-32184,-32184,-32185,-32185,-32186,-32187,-32187,-32188,-32188,-32189,-32190,-32190,-32191,-32191,-32192,-32193,-32193,-32194,-32194,-32195,-32195,-32196,-32197,-32197,-32198,-32198,-32199,-32200,-32200,-32201,-32201,-32202,-32202,-32203,-32204,-32204,-32205,-32205,-32206,-32206,-32207,-32208,-32208,-32209,-32209,-32210,-32211,-32211,-32212,-32212,-32213,-32213,-32214,-32215,-32215,-32216,-32216,-32217,-32217,-32218,-32219,-32219,-32220,-32220,-32221,-32221,-32222,-32223,-32223,-32224,-32224,-32225,-32225,-32226,-32227,-32227,-32228,-32228,-32229,-32229,-32230,-32231,-32231,-32232,-32232,-32233,-32233,-32234,-32234,-32235,-32236,-32236,-32237,-32237,-32238,-32238,-32239,-32240,-32240,-32241,-32241,-32242,-32242,-32243,-32243,-32244,-32245,-32245,-32246,-32246,-32247,-32247,-32248,-32248,-32249,-32250,-32250,-32251,-32251,-32252,-32252,-32253,-32253,-32254,-32255,-32255,-32256,-32256,-32257,-32257,-32258,-32258,-32259,-32260,-32260,-32261,-32261,-32262,-32262,-32263,-32263,-32264,-32265,-32265,-32266,-32266,-32267,-32267,-32268,-32268,-32269,-32269,-32270,-32271,-32271,-32272,-32272,-32273,-32273,-32274,-32274,-32275,-32275,-32276,-32277,-32277,-32278,-32278,-32279,-32279,-32280,-32280,-32281,-32281,-32282,-32282,-32283,-32284,-32284,-32285,-32285,-32286,-32286,-32287,-32287,-32288,-32288,-32289,-32289,-32290,-32290,-32291,-32292,-32292,-32293,-32293,-32294,-32294,-32295,-32295,-32296,-32296,-32297,-32297,-32298,-32298,-32299,-32300,-32300,-32301,-32301,-32302,-32302,-32303,-32303,-32304,-32304,-32305,-32305,-32306,-32306,-32307,-32307,-32308,-32308,-32309,-32310,-32310,-32311,-32311,-32312,-32312,-32313,-32313,-32314,-32314,-32315,-32315,-32316,-32316,-32317,-32317,-32318,-32318,-32319,-32319,-32320,-32320,-32321,-32321,-32322,-32322,-32323,-32324,-32324,-32325,-32325,-32326,-32326,-32327,-32327,-32328,-32328,-32329,-32329,-32330,-32330,-32331,-32331,-32332,-32332,-32333,-32333,-32334,-32334,-32335,-32335,-32336,-32336,-32337,-32337,-32338,-32338,-32339,-32339,-32340,-32340,-32341,-32341,-32342,-32342,-32343,-32343,-32344,-32344,-32345,-32345,-32346,-32346,-32347,-32347,-32348,-32348,-32349,-32349,-32350,-32350,-32351,-32351,-32352,-32352,-32353,-32353,-32354,-32354,-32355,-32355,-32356,-32356,-32357,-32357,-32358,-32358,-32359,-32359,-32360,-32360,-32361,-32361,-32362,-32362,-32363,-32363,-32364,-32364,-32365,-32365,-32366,-32366,-32367,-32367,-32368,-32368,-32369,-32369,-32370,-32370,-32371,-32371,-32372,-32372,-32373,-32373,-32374,-32374,-32375,-32375,-32375,-32376,-32376,-32377,-32377,-32378,-32378,-32379,-32379,-32380,-32380,-32381,-32381,-32382,-32382,-32383,-32383,-32384,-32384,-32385,-32385,-32386,-32386,-32387,-32387,-32387,-32388,-32388,-32389,-32389,-32390,-32390,-32391,-32391,-32392,-32392,-32393,-32393,-32394,-32394,-32395,-32395,-32396,-32396,-32397,-32397,-32397,-32398,-32398,-32399,-32399,-32400,-32400,-32401,-32401,-32402,-32402,-32403,-32403,-32404,-32404,-32404,-32405,-32405,-32406,-32406,-32407,-32407,-32408,-32408,-32409,-32409,-32410,-32410,-32411,-32411,-32411,-32412,-32412,-32413,-32413,-32414,-32414,-32415,-32415,-32416,-32416,-32416,-32417,-32417,-32418,-32418,-32419,-32419,-32420,-32420,-32421,-32421,-32422,-32422,-32422,-32423,-32423,-32424,-32424,-32425,-32425,-32426,-32426,-32426,-32427,-32427,-32428,-32428,-32429,-32429,-32430,-32430,-32431,-32431,-32431,-32432,-32432,-32433,-32433,-32434,-32434,-32435,-32435,-32435,-32436,-32436,-32437,-32437,-32438,-32438,-32439,-32439,-32439,-32440,-32440,-32441,-32441,-32442,-32442,-32443,-32443,-32443,-32444,-32444,-32445,-32445,-32446,-32446,-32447,-32447,-32447,-32448,-32448,-32449,-32449,-32450,-32450,-32450,-32451,-32451,-32452,-32452,-32453,-32453,-32453,-32454,-32454,-32455,-32455,-32456,-32456,-32457,-32457,-32457,-32458,-32458,-32459,-32459,-32460,-32460,-32460,-32461,-32461,-32462,-32462,-32463,-32463,-32463,-32464,-32464,-32465,-32465,-32466,-32466,-32466,-32467,-32467,-32468,-32468,-32468,-32469,-32469,-32470,-32470,-32471,-32471,-32471,-32472,-32472,-32473,-32473,-32474,-32474,-32474,-32475,-32475,-32476,-32476,-32476,-32477,-32477,-32478,-32478,-32479,-32479,-32479,-32480,-32480,-32481,-32481,-32481,-32482,-32482,-32483,-32483,-32484,-32484,-32484,-32485,-32485,-32486,-32486,-32486,-32487,-32487,-32488,-32488,-32488,-32489,-32489,-32490,-32490,-32490,-32491,-32491,-32492,-32492,-32493,-32493,-32493,-32494,-32494,-32495,-32495,-32495,-32496,-32496,-32497,-32497,-32497,-32498,-32498,-32499,-32499,-32499,-32500,-32500,-32501,-32501,-32501,-32502,-32502,-32503,-32503,-32503,-32504,-32504,-32505,-32505,-32505,-32506,-32506,-32507,-32507,-32507,-32508,-32508,-32509,-32509,-32509,-32510,-32510,-32510,-32511,-32511,-32512,-32512,-32512,-32513,-32513,-32514,-32514,-32514,-32515,-32515,-32516,-32516,-32516,-32517,-32517,-32517,-32518,-32518,-32519,-32519,-32519,-32520,-32520,-32521,-32521,-32521,-32522,-32522,-32522,-32523,-32523,-32524,-32524,-32524,-32525,-32525,-32526,-32526,-32526,-32527,-32527,-32527,-32528,-32528,-32529,-32529,-32529,-32530,-32530,-32530,-32531,-32531,-32532,-32532,-32532,-32533,-32533,-32533,-32534,-32534,-32535,-32535,-32535,-32536,-32536,-32536,-32537,-32537,-32538,-32538,-32538,-32539,-32539,-32539,-32540,-32540,-32541,-32541,-32541,-32542,-32542,-32542,-32543,-32543,-32543,-32544,-32544,-32545,-32545,-32545,-32546,-32546,-32546,-32547,-32547,-32547,-32548,-32548,-32549,-32549,-32549,-32550,-32550,-32550,-32551,-32551,-32551,-32552,-32552,-32553,-32553,-32553,-32554,-32554,-32554,-32555,-32555,-32555,-32556,-32556,-32556,-32557,-32557,-32558,-32558,-32558,-32559,-32559,-32559,-32560,-32560,-32560,-32561,-32561,-32561,-32562,-32562,-32562,-32563,-32563,-32564,-32564,-32564,-32565,-32565,-32565,-32566,-32566,-32566,-32567,-32567,-32567,-32568,-32568,-32568,-32569,-32569,-32569,-32570,-32570,-32570,-32571,-32571,-32571,-32572,-32572,-32573,-32573,-32573,-32574,-32574,-32574,-32575,-32575,-32575,-32576,-32576,-32576,-32577,-32577,-32577,-32578,-32578,-32578,-32579,-32579,-32579,-32580,-32580,-32580,-32581,-32581,-32581,-32582,-32582,-32582,-32583,-32583,-32583,-32584,-32584,-32584,-32585,-32585,-32585,-32586,-32586,-32586,-32587,-32587,-32587,-32588,-32588,-32588,-32589,-32589,-32589,-32590,-32590,-32590,-32591,-32591,-32591,-32592,-32592,-32592,-32592,-32593,-32593,-32593,-32594,-32594,-32594,-32595,-32595,-32595,-32596,-32596,-32596,-32597,-32597,-32597,-32598,-32598,-32598,-32599,-32599,-32599,-32600,-32600,-32600,-32600,-32601,-32601,-32601,-32602,-32602,-32602,-32603,-32603,-32603,-32604,-32604,-32604,-32605,-32605,-32605,-32606,-32606,-32606,-32606,-32607,-32607,-32607,-32608,-32608,-32608,-32609,-32609,-32609,-32610,-32610,-32610,-32610,-32611,-32611,-32611,-32612,-32612,-32612,-32613,-32613,-32613,-32613,-32614,-32614,-32614,-32615,-32615,-32615,-32616,-32616,-32616,-32617,-32617,-32617,-32617,-32618,-32618,-32618,-32619,-32619,-32619,-32620,-32620,-32620,-32620,-32621,-32621,-32621,-32622,-32622,-32622,-32622,-32623,-32623,-32623,-32624,-32624,-32624,-32625,-32625,-32625,-32625,-32626,-32626,-32626,-32627,-32627,-32627,-32627,-32628,-32628,-32628,-32629,-32629,-32629,-32629,-32630,-32630,-32630,-32631,-32631,-32631,-32631,-32632,-32632,-32632,-32633,-32633,-32633,-32633,-32634,-32634,-32634,-32635,-32635,-32635,-32635,-32636,-32636,-32636,-32637,-32637,-32637,-32637,-32638,-32638,-32638,-32639,-32639,-32639,-32639,-32640,-32640,-32640,-32640,-32641,-32641,-32641,-32642,-32642,-32642,-32642,-32643,-32643,-32643,-32643,-32644,-32644,-32644,-32645,-32645,-32645,-32645,-32646,-32646,-32646,-32646,-32647,-32647,-32647,-32648,-32648,-32648,-32648,-32649,-32649,-32649,-32649,-32650,-32650,-32650,-32650,-32651,-32651,-32651,-32652,-32652,-32652,-32652,-32653,-32653,-32653,-32653,-32654,-32654,-32654,-32654,-32655,-32655,-32655,-32655,-32656,-32656,-32656,-32656,-32657,-32657,-32657,-32657,-32658,-32658,-32658,-32659,-32659,-32659,-32659,-32660,-32660,-32660,-32660,-32661,-32661,-32661,-32661,-32662,-32662,-32662,-32662,-32663,-32663,-32663,-32663,-32664,-32664,-32664,-32664,-32665,-32665,-32665,-32665,-32666,-32666,-32666,-32666,-32667,-32667,-32667,-32667,-32668,-32668,-32668,-32668,-32668,-32669,-32669,-32669,-32669,-32670,-32670,-32670,-32670,-32671,-32671,-32671,-32671,-32672,-32672,-32672,-32672,-32673,-32673,-32673,-32673,-32674,-32674,-32674,-32674,-32674,-32675,-32675,-32675,-32675,-32676,-32676,-32676,-32676,-32677,-32677,-32677,-32677,-32678,-32678,-32678,-32678,-32678,-32679,-32679,-32679,-32679,-32680,-32680,-32680,-32680,-32681,-32681,-32681,-32681,-32681,-32682,-32682,-32682,-32682,-32683,-32683,-32683,-32683,-32683,-32684,-32684,-32684,-32684,-32685,-32685,-32685,-32685,-32685,-32686,-32686,-32686,-32686,-32687,-32687,-32687,-32687,-32687,-32688,-32688,-32688,-32688,-32689,-32689,-32689,-32689,-32689,-32690,-32690,-32690,-32690,-32690,-32691,-32691,-32691,-32691,-32692,-32692,-32692,-32692,-32692,-32693,-32693,-32693,-32693,-32693,-32694,-32694,-32694,-32694,-32694,-32695,-32695,-32695,-32695,-32696,-32696,-32696,-32696,-32696,-32697,-32697,-32697,-32697,-32697,-32698,-32698,-32698,-32698,-32698,-32699,-32699,-32699,-32699,-32699,-32700,-32700,-32700,-32700,-32700,-32701,-32701,-32701,-32701,-32701,-32702,-32702,-32702,-32702,-32702,-32703,-32703,-32703,-32703,-32703,-32704,-32704,-32704,-32704,-32704,-32705,-32705,-32705,-32705,-32705,-32706,-32706,-32706,-32706,-32706,-32706,-32707,-32707,-32707,-32707,-32707,-32708,-32708,-32708,-32708,-32708,-32709,-32709,-32709,-32709,-32709,-32710,-32710,-32710,-32710,-32710,-32710,-32711,-32711,-32711,-32711,-32711,-32712,-32712,-32712,-32712,-32712,-32712,-32713,-32713,-32713,-32713,-32713,-32714,-32714,-32714,-32714,-32714,-32714,-32715,-32715,-32715,-32715,-32715,-32715,-32716,-32716,-32716,-32716,-32716,-32717,-32717,-32717,-32717,-32717,-32717,-32718,-32718,-32718,-32718,-32718,-32718,-32719,-32719,-32719,-32719,-32719,-32719,-32720,-32720,-32720,-32720,-32720,-32720,-32721,-32721,-32721,-32721,-32721,-32721,-32722,-32722,-32722,-32722,-32722,-32722,-32723,-32723,-32723,-32723,-32723,-32723,-32724,-32724,-32724,-32724,-32724,-32724,-32725,-32725,-32725,-32725,-32725,-32725,-32726,-32726,-32726,-32726,-32726,-32726,-32726,-32727,-32727,-32727,-32727,-32727,-32727,-32728,-32728,-32728,-32728,-32728,-32728,-32728,-32729,-32729,-32729,-32729,-32729,-32729,-32730,-32730,-32730,-32730,-32730,-32730,-32730,-32731,-32731,-32731,-32731,-32731,-32731,-32731,-32732,-32732,-32732,-32732,-32732,-32732,-32732,-32733,-32733,-32733,-32733,-32733,-32733,-32733,-32734,-32734,-32734,-32734,-32734,-32734,-32734,-32735,-32735,-32735,-32735,-32735,-32735,-32735,-32736,-32736,-32736,-32736,-32736,-32736,-32736,-32737,-32737,-32737,-32737,-32737,-32737,-32737,-32737,-32738,-32738,-32738,-32738,-32738,-32738,-32738,-32739,-32739,-32739,-32739,-32739,-32739,-32739,-32739,-32740,-32740,-32740,-32740,-32740,-32740,-32740,-32740,-32741,-32741,-32741,-32741,-32741,-32741,-32741,-32741,-32742,-32742,-32742,-32742,-32742,-32742,-32742,-32742,-32743,-32743,-32743,-32743,-32743,-32743,-32743,-32743,-32744,-32744,-32744,-32744,-32744,-32744,-32744,-32744,-32744,-32745,-32745,-32745,-32745,-32745,-32745,-32745,-32745,-32745,-32746,-32746,-32746,-32746,-32746,-32746,-32746,-32746,-32746,-32747,-32747,-32747,-32747,-32747,-32747,-32747,-32747,-32747,-32748,-32748,-32748,-32748,-32748,-32748,-32748,-32748,-32748,-32749,-32749,-32749,-32749,-32749,-32749,-32749,-32749,-32749,-32749,-32750,-32750,-32750,-32750,-32750,-32750,-32750,-32750,-32750,-32751,-32751,-32751,-32751,-32751,-32751,-32751,-32751,-32751,-32751,-32751,-32752,-32752,-32752,-32752,-32752,-32752,-32752,-32752,-32752,-32752,-32753,-32753,-32753,-32753,-32753,-32753,-32753,-32753,-32753,-32753,-32753,-32754,-32754,-32754,-32754,-32754,-32754,-32754,-32754,-32754,-32754,-32754,-32755,-32755,-32755,-32755,-32755,-32755,-32755,-32755,-32755,-32755,-32755,-32755,-32756,-32756,-32756,-32756,-32756,-32756,-32756,-32756,-32756,-32756,-32756,-32756,-32757,-32757,-32757,-32757,-32757,-32757,-32757,-32757,-32757,-32757,-32757,-32757,-32757,-32758,-32758,-32758,-32758,-32758,-32758,-32758,-32758,-32758,-32758,-32758,-32758,-32758,-32758,-32759,-32759,-32759,-32759,-32759,-32759,-32759,-32759,-32759,-32759,-32759,-32759,-32759,-32759,-32760,-32760,-32760,-32760,-32760,-32760,-32760,-32760,-32760,-32760,-32760,-32760,-32760,-32760,-32760,-32760,-32761,-32761,-32761,-32761,-32761,-32761,-32761,-32761,-32761,-32761,-32761,-32761,-32761,-32761,-32761,-32761,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32766,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32765,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32764,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32763,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32762,-32761,-32761,-32761,-32761,-32761,-32761,-32761,-32761,-32761,-32761,-32761,-32761,-32761,-32761,-32761,-32761,-32760,-32760,-32760,-32760,-32760,-32760,-32760,-32760,-32760,-32760,-32760,-32760,-32760,-32760,-32760,-32760,-32759,-32759,-32759,-32759,-32759,-32759,-32759,-32759,-32759,-32759,-32759,-32759,-32759,-32759,-32758,-32758,-32758,-32758,-32758,-32758,-32758,-32758,-32758,-32758,-32758,-32758,-32758,-32758,-32757,-32757,-32757,-32757,-32757,-32757,-32757,-32757,-32757,-32757,-32757,-32757,-32757,-32756,-32756,-32756,-32756,-32756,-32756,-32756,-32756,-32756,-32756,-32756,-32756,-32755,-32755,-32755,-32755,-32755,-32755,-32755,-32755,-32755,-32755,-32755,-32755,-32754,-32754,-32754,-32754,-32754,-32754,-32754,-32754,-32754,-32754,-32754,-32753,-32753,-32753,-32753,-32753,-32753,-32753,-32753,-32753,-32753,-32753,-32752,-32752,-32752,-32752,-32752,-32752,-32752,-32752,-32752,-32752,-32751,-32751,-32751,-32751,-32751,-32751,-32751,-32751,-32751,-32751,-32751,-32750,-32750,-32750,-32750,-32750,-32750,-32750,-32750,-32750,-32749,-32749,-32749,-32749,-32749,-32749,-32749,-32749,-32749,-32749,-32748,-32748,-32748,-32748,-32748,-32748,-32748,-32748,-32748,-32747,-32747,-32747,-32747,-32747,-32747,-32747,-32747,-32747,-32746,-32746,-32746,-32746,-32746,-32746,-32746,-32746,-32746,-32745,-32745,-32745,-32745,-32745,-32745,-32745,-32745,-32745,-32744,-32744,-32744,-32744,-32744,-32744,-32744,-32744,-32744,-32743,-32743,-32743,-32743,-32743,-32743,-32743,-32743,-32742,-32742,-32742,-32742,-32742,-32742,-32742,-32742,-32741,-32741,-32741,-32741,-32741,-32741,-32741,-32741,-32740,-32740,-32740,-32740,-32740,-32740,-32740,-32740,-32739,-32739,-32739,-32739,-32739,-32739,-32739,-32739,-32738,-32738,-32738,-32738,-32738,-32738,-32738,-32737,-32737,-32737,-32737,-32737,-32737,-32737,-32737,-32736,-32736,-32736,-32736,-32736,-32736,-32736,-32735,-32735,-32735,-32735,-32735,-32735,-32735,-32734,-32734,-32734,-32734,-32734,-32734,-32734,-32733,-32733,-32733,-32733,-32733,-32733,-32733,-32732,-32732,-32732,-32732,-32732,-32732,-32732,-32731,-32731,-32731,-32731,-32731,-32731,-32731,-32730,-32730,-32730,-32730,-32730,-32730,-32730,-32729,-32729,-32729,-32729,-32729,-32729,-32728,-32728,-32728,-32728,-32728,-32728,-32728,-32727,-32727,-32727,-32727,-32727,-32727,-32726,-32726,-32726,-32726,-32726,-32726,-32726,-32725,-32725,-32725,-32725,-32725,-32725,-32724,-32724,-32724,-32724,-32724,-32724,-32723,-32723,-32723,-32723,-32723,-32723,-32722,-32722,-32722,-32722,-32722,-32722,-32721,-32721,-32721,-32721,-32721,-32721,-32720,-32720,-32720,-32720,-32720,-32720,-32719,-32719,-32719,-32719,-32719,-32719,-32718,-32718,-32718,-32718,-32718,-32718,-32717,-32717,-32717,-32717,-32717,-32717,-32716,-32716,-32716,-32716,-32716,-32715,-32715,-32715,-32715,-32715,-32715,-32714,-32714,-32714,-32714,-32714,-32714,-32713,-32713,-32713,-32713,-32713,-32712,-32712,-32712,-32712,-32712,-32712,-32711,-32711,-32711,-32711,-32711,-32710,-32710,-32710,-32710,-32710,-32710,-32709,-32709,-32709,-32709,-32709,-32708,-32708,-32708,-32708,-32708,-32707,-32707,-32707,-32707,-32707,-32706,-32706,-32706,-32706,-32706,-32706,-32705,-32705,-32705,-32705,-32705,-32704,-32704,-32704,-32704,-32704,-32703,-32703,-32703,-32703,-32703,-32702,-32702,-32702,-32702,-32702,-32701,-32701,-32701,-32701,-32701,-32700,-32700,-32700,-32700,-32700,-32699,-32699,-32699,-32699,-32699,-32698,-32698,-32698,-32698,-32698,-32697,-32697,-32697,-32697,-32697,-32696,-32696,-32696,-32696,-32696,-32695,-32695,-32695,-32695,-32694,-32694,-32694,-32694,-32694,-32693,-32693,-32693,-32693,-32693,-32692,-32692,-32692,-32692,-32692,-32691,-32691,-32691,-32691,-32690,-32690,-32690,-32690,-32690,-32689,-32689,-32689,-32689,-32689,-32688,-32688,-32688,-32688,-32687,-32687,-32687,-32687,-32687,-32686,-32686,-32686,-32686,-32685,-32685,-32685,-32685,-32685,-32684,-32684,-32684,-32684,-32683,-32683,-32683,-32683,-32683,-32682,-32682,-32682,-32682,-32681,-32681,-32681,-32681,-32681,-32680,-32680,-32680,-32680,-32679,-32679,-32679,-32679,-32678,-32678,-32678,-32678,-32678,-32677,-32677,-32677,-32677,-32676,-32676,-32676,-32676,-32675,-32675,-32675,-32675,-32674,-32674,-32674,-32674,-32674,-32673,-32673,-32673,-32673,-32672,-32672,-32672,-32672,-32671,-32671,-32671,-32671,-32670,-32670,-32670,-32670,-32669,-32669,-32669,-32669,-32668,-32668,-32668,-32668,-32668,-32667,-32667,-32667,-32667,-32666,-32666,-32666,-32666,-32665,-32665,-32665,-32665,-32664,-32664,-32664,-32664,-32663,-32663,-32663,-32663,-32662,-32662,-32662,-32662,-32661,-32661,-32661,-32661,-32660,-32660,-32660,-32660,-32659,-32659,-32659,-32659,-32658,-32658,-32658,-32657,-32657,-32657,-32657,-32656,-32656,-32656,-32656,-32655,-32655,-32655,-32655,-32654,-32654,-32654,-32654,-32653,-32653,-32653,-32653,-32652,-32652,-32652,-32652,-32651,-32651,-32651,-32650,-32650,-32650,-32650,-32649,-32649,-32649,-32649,-32648,-32648,-32648,-32648,-32647,-32647,-32647,-32646,-32646,-32646,-32646,-32645,-32645,-32645,-32645,-32644,-32644,-32644,-32643,-32643,-32643,-32643,-32642,-32642,-32642,-32642,-32641,-32641,-32641,-32640,-32640,-32640,-32640,-32639,-32639,-32639,-32639,-32638,-32638,-32638,-32637,-32637,-32637,-32637,-32636,-32636,-32636,-32635,-32635,-32635,-32635,-32634,-32634,-32634,-32633,-32633,-32633,-32633,-32632,-32632,-32632,-32631,-32631,-32631,-32631,-32630,-32630,-32630,-32629,-32629,-32629,-32629,-32628,-32628,-32628,-32627,-32627,-32627,-32627,-32626,-32626,-32626,-32625,-32625,-32625,-32625,-32624,-32624,-32624,-32623,-32623,-32623,-32622,-32622,-32622,-32622,-32621,-32621,-32621,-32620,-32620,-32620,-32620,-32619,-32619,-32619,-32618,-32618,-32618,-32617,-32617,-32617,-32617,-32616,-32616,-32616,-32615,-32615,-32615,-32614,-32614,-32614,-32613,-32613,-32613,-32613,-32612,-32612,-32612,-32611,-32611,-32611,-32610,-32610,-32610,-32610,-32609,-32609,-32609,-32608,-32608,-32608,-32607,-32607,-32607,-32606,-32606,-32606,-32606,-32605,-32605,-32605,-32604,-32604,-32604,-32603,-32603,-32603,-32602,-32602,-32602,-32601,-32601,-32601,-32600,-32600,-32600,-32600,-32599,-32599,-32599,-32598,-32598,-32598,-32597,-32597,-32597,-32596,-32596,-32596,-32595,-32595,-32595,-32594,-32594,-32594,-32593,-32593,-32593,-32592,-32592,-32592,-32592,-32591,-32591,-32591,-32590,-32590,-32590,-32589,-32589,-32589,-32588,-32588,-32588,-32587,-32587,-32587,-32586,-32586,-32586,-32585,-32585,-32585,-32584,-32584,-32584,-32583,-32583,-32583,-32582,-32582,-32582,-32581,-32581,-32581,-32580,-32580,-32580,-32579,-32579,-32579,-32578,-32578,-32578,-32577,-32577,-32577,-32576,-32576,-32576,-32575,-32575,-32575,-32574,-32574,-32574,-32573,-32573,-32573,-32572,-32572,-32571,-32571,-32571,-32570,-32570,-32570,-32569,-32569,-32569,-32568,-32568,-32568,-32567,-32567,-32567,-32566,-32566,-32566,-32565,-32565,-32565,-32564,-32564,-32564,-32563,-32563,-32562,-32562,-32562,-32561,-32561,-32561,-32560,-32560,-32560,-32559,-32559,-32559,-32558,-32558,-32558,-32557,-32557,-32556,-32556,-32556,-32555,-32555,-32555,-32554,-32554,-32554,-32553,-32553,-32553,-32552,-32552,-32551,-32551,-32551,-32550,-32550,-32550,-32549,-32549,-32549,-32548,-32548,-32547,-32547,-32547,-32546,-32546,-32546,-32545,-32545,-32545,-32544,-32544,-32543,-32543,-32543,-32542,-32542,-32542,-32541,-32541,-32541,-32540,-32540,-32539,-32539,-32539,-32538,-32538,-32538,-32537,-32537,-32536,-32536,-32536,-32535,-32535,-32535,-32534,-32534,-32533,-32533,-32533,-32532,-32532,-32532,-32531,-32531,-32530,-32530,-32530,-32529,-32529,-32529,-32528,-32528,-32527,-32527,-32527,-32526,-32526,-32526,-32525,-32525,-32524,-32524,-32524,-32523,-32523,-32522,-32522,-32522,-32521,-32521,-32521,-32520,-32520,-32519,-32519,-32519,-32518,-32518,-32517,-32517,-32517,-32516,-32516,-32516,-32515,-32515,-32514,-32514,-32514,-32513,-32513,-32512,-32512,-32512,-32511,-32511,-32510,-32510,-32510,-32509,-32509,-32509,-32508,-32508,-32507,-32507,-32507,-32506,-32506,-32505,-32505,-32505,-32504,-32504,-32503,-32503,-32503,-32502,-32502,-32501,-32501,-32501,-32500,-32500,-32499,-32499,-32499,-32498,-32498,-32497,-32497,-32497,-32496,-32496,-32495,-32495,-32495,-32494,-32494,-32493,-32493,-32493,-32492,-32492,-32491,-32491,-32490,-32490,-32490,-32489,-32489,-32488,-32488,-32488,-32487,-32487,-32486,-32486,-32486,-32485,-32485,-32484,-32484,-32484,-32483,-32483,-32482,-32482,-32481,-32481,-32481,-32480,-32480,-32479,-32479,-32479,-32478,-32478,-32477,-32477,-32476,-32476,-32476,-32475,-32475,-32474,-32474,-32474,-32473,-32473,-32472,-32472,-32471,-32471,-32471,-32470,-32470,-32469,-32469,-32468,-32468,-32468,-32467,-32467,-32466,-32466,-32466,-32465,-32465,-32464,-32464,-32463,-32463,-32463,-32462,-32462,-32461,-32461,-32460,-32460,-32460,-32459,-32459,-32458,-32458,-32457,-32457,-32457,-32456,-32456,-32455,-32455,-32454,-32454,-32453,-32453,-32453,-32452,-32452,-32451,-32451,-32450,-32450,-32450,-32449,-32449,-32448,-32448,-32447,-32447,-32447,-32446,-32446,-32445,-32445,-32444,-32444,-32443,-32443,-32443,-32442,-32442,-32441,-32441,-32440,-32440,-32439,-32439,-32439,-32438,-32438,-32437,-32437,-32436,-32436,-32435,-32435,-32435,-32434,-32434,-32433,-32433,-32432,-32432,-32431,-32431,-32431,-32430,-32430,-32429,-32429,-32428,-32428,-32427,-32427,-32426,-32426,-32426,-32425,-32425,-32424,-32424,-32423,-32423,-32422,-32422,-32422,-32421,-32421,-32420,-32420,-32419,-32419,-32418,-32418,-32417,-32417,-32416,-32416,-32416,-32415,-32415,-32414,-32414,-32413,-32413,-32412,-32412,-32411,-32411,-32411,-32410,-32410,-32409,-32409,-32408,-32408,-32407,-32407,-32406,-32406,-32405,-32405,-32404,-32404,-32404,-32403,-32403,-32402,-32402,-32401,-32401,-32400,-32400,-32399,-32399,-32398,-32398,-32397,-32397,-32397,-32396,-32396,-32395,-32395,-32394,-32394,-32393,-32393,-32392,-32392,-32391,-32391,-32390,-32390,-32389,-32389,-32388,-32388,-32387,-32387,-32387,-32386,-32386,-32385,-32385,-32384,-32384,-32383,-32383,-32382,-32382,-32381,-32381,-32380,-32380,-32379,-32379,-32378,-32378,-32377,-32377,-32376,-32376,-32375,-32375,-32375,-32374,-32374,-32373,-32373,-32372,-32372,-32371,-32371,-32370,-32370,-32369,-32369,-32368,-32368,-32367,-32367,-32366,-32366,-32365,-32365,-32364,-32364,-32363,-32363,-32362,-32362,-32361,-32361,-32360,-32360,-32359,-32359,-32358,-32358,-32357,-32357,-32356,-32356,-32355,-32355,-32354,-32354,-32353,-32353,-32352,-32352,-32351,-32351,-32350,-32350,-32349,-32349,-32348,-32348,-32347,-32347,-32346,-32346,-32345,-32345,-32344,-32344,-32343,-32343,-32342,-32342,-32341,-32341,-32340,-32340,-32339,-32339,-32338,-32338,-32337,-32337,-32336,-32336,-32335,-32335,-32334,-32334,-32333,-32333,-32332,-32332,-32331,-32331,-32330,-32330,-32329,-32329,-32328,-32328,-32327,-32327,-32326,-32326,-32325,-32325,-32324,-32324,-32323,-32322,-32322,-32321,-32321,-32320,-32320,-32319,-32319,-32318,-32318,-32317,-32317,-32316,-32316,-32315,-32315,-32314,-32314,-32313,-32313,-32312,-32312,-32311,-32311,-32310,-32310,-32309,-32308,-32308,-32307,-32307,-32306,-32306,-32305,-32305,-32304,-32304,-32303,-32303,-32302,-32302,-32301,-32301,-32300,-32300,-32299,-32298,-32298,-32297,-32297,-32296,-32296,-32295,-32295,-32294,-32294,-32293,-32293,-32292,-32292,-32291,-32290,-32290,-32289,-32289,-32288,-32288,-32287,-32287,-32286,-32286,-32285,-32285,-32284,-32284,-32283,-32282,-32282,-32281,-32281,-32280,-32280,-32279,-32279,-32278,-32278,-32277,-32277,-32276,-32275,-32275,-32274,-32274,-32273,-32273,-32272,-32272,-32271,-32271,-32270,-32269,-32269,-32268,-32268,-32267,-32267,-32266,-32266,-32265,-32265,-32264,-32263,-32263,-32262,-32262,-32261,-32261,-32260,-32260,-32259,-32258,-32258,-32257,-32257,-32256,-32256,-32255,-32255,-32254,-32253,-32253,-32252,-32252,-32251,-32251,-32250,-32250,-32249,-32248,-32248,-32247,-32247,-32246,-32246,-32245,-32245,-32244,-32243,-32243,-32242,-32242,-32241,-32241,-32240,-32240,-32239,-32238,-32238,-32237,-32237,-32236,-32236,-32235,-32234,-32234,-32233,-32233,-32232,-32232,-32231,-32231,-32230,-32229,-32229,-32228,-32228,-32227,-32227,-32226,-32225,-32225,-32224,-32224,-32223,-32223,-32222,-32221,-32221,-32220,-32220,-32219,-32219,-32218,-32217,-32217,-32216,-32216,-32215,-32215,-32214,-32213,-32213,-32212,-32212,-32211,-32211,-32210,-32209,-32209,-32208,-32208,-32207,-32206,-32206,-32205,-32205,-32204,-32204,-32203,-32202,-32202,-32201,-32201,-32200,-32200,-32199,-32198,-32198,-32197,-32197,-32196,-32195,-32195,-32194,-32194,-32193,-32193,-32192,-32191,-32191,-32190,-32190,-32189,-32188,-32188,-32187,-32187,-32186,-32185,-32185,-32184,-32184,-32183,-32183,-32182,-32181,-32181,-32180,-32180,-32179,-32178,-32178,-32177,-32177,-32176,-32175,-32175,-32174,-32174,-32173,-32172,-32172,-32171,-32171,-32170,-32169,-32169,-32168,-32168,-32167,-32166,-32166,-32165,-32165,-32164,-32163,-32163,-32162,-32162,-32161,-32160,-32160,-32159,-32159,-32158,-32157,-32157,-32156,-32156,-32155,-32154,-32154,-32153,-32153,-32152,-32151,-32151,-32150,-32150,-32149,-32148,-32148,-32147,-32147,-32146,-32145,-32145,-32144,-32144,-32143,-32142,-32142,-32141,-32140,-32140,-32139,-32139,-32138,-32137,-32137,-32136,-32136,-32135,-32134,-32134,-32133,-32132,-32132,-32131,-32131,-32130,-32129,-32129,-32128,-32128,-32127,-32126,-32126,-32125,-32124,-32124,-32123,-32123,-32122,-32121,-32121,-32120,-32119,-32119,-32118,-32118,-32117,-32116,-32116,-32115,-32115,-32114,-32113,-32113,-32112,-32111,-32111,-32110,-32110,-32109,-32108,-32108,-32107,-32106,-32106,-32105,-32104,-32104,-32103,-32103,-32102,-32101,-32101,-32100,-32099,-32099,-32098,-32098,-32097,-32096,-32096,-32095,-32094,-32094,-32093,-32092,-32092,-32091,-32091,-32090,-32089,-32089,-32088,-32087,-32087,-32086,-32086,-32085,-32084,-32084,-32083,-32082,-32082,-32081,-32080,-32080,-32079,-32078,-32078,-32077,-32077,-32076,-32075,-32075,-32074,-32073,-32073,-32072,-32071,-32071,-32070,-32069,-32069,-32068,-32068,-32067,-32066,-32066,-32065,-32064,-32064,-32063,-32062,-32062,-32061,-32060,-32060,-32059,-32058,-32058,-32057,-32057,-32056,-32055,-32055,-32054,-32053,-32053,-32052,-32051,-32051,-32050,-32049,-32049,-32048,-32047,-32047,-32046,-32045,-32045,-32044,-32043,-32043,-32042,-32041,-32041,-32040,-32040,-32039,-32038,-32038,-32037,-32036,-32036,-32035,-32034,-32034,-32033,-32032,-32032,-32031,-32030,-32030,-32029,-32028,-32028,-32027,-32026,-32026,-32025,-32024,-32024,-32023,-32022,-32022,-32021,-32020,-32020,-32019,-32018,-32018,-32017,-32016,-32016,-32015,-32014,-32014,-32013,-32012,-32012,-32011,-32010,-32010,-32009,-32008,-32008,-32007,-32006,-32006,-32005,-32004,-32004,-32003,-32002,-32002,-32001,-32000,-31999,-31999,-31998,-31997,-31997,-31996,-31995,-31995,-31994,-31993,-31993,-31992,-31991,-31991,-31990,-31989,-31989,-31988,-31987,-31987,-31986,-31985,-31985,-31984,-31983,-31982,-31982,-31981,-31980,-31980,-31979,-31978,-31978,-31977,-31976,-31976,-31975,-31974,-31974,-31973,-31972,-31972,-31971,-31970,-31969,-31969,-31968,-31967,-31967,-31966,-31965,-31965,-31964,-31963,-31963,-31962,-31961,-31960,-31960,-31959,-31958,-31958,-31957,-31956,-31956,-31955,-31954,-31954,-31953,-31952,-31951,-31951,-31950,-31949,-31949,-31948,-31947,-31947,-31946,-31945,-31944,-31944,-31943,-31942,-31942,-31941,-31940,-31940,-31939,-31938,-31937,-31937,-31936,-31935,-31935,-31934,-31933,-31933,-31932,-31931,-31930,-31930,-31929,-31928,-31928,-31927,-31926,-31925,-31925,-31924,-31923,-31923,-31922,-31921,-31921,-31920,-31919,-31918,-31918,-31917,-31916,-31916,-31915,-31914,-31913,-31913,-31912,-31911,-31911,-31910,-31909,-31908,-31908,-31907,-31906,-31906,-31905,-31904,-31903,-31903,-31902,-31901,-31901,-31900,-31899,-31898,-31898,-31897,-31896,-31896,-31895,-31894,-31893,-31893,-31892,-31891,-31890,-31890,-31889,-31888,-31888,-31887,-31886,-31885,-31885,-31884,-31883,-31882,-31882,-31881,-31880,-31880,-31879,-31878,-31877,-31877,-31876,-31875,-31875,-31874,-31873,-31872,-31872,-31871,-31870,-31869,-31869,-31868,-31867,-31866,-31866,-31865,-31864,-31864,-31863,-31862,-31861,-31861,-31860,-31859,-31858,-31858,-31857,-31856,-31855,-31855,-31854,-31853,-31853,-31852,-31851,-31850,-31850,-31849,-31848,-31847,-31847,-31846,-31845,-31844,-31844,-31843,-31842,-31841,-31841,-31840,-31839,-31838,-31838,-31837,-31836,-31836,-31835,-31834,-31833,-31833,-31832,-31831,-31830,-31830,-31829,-31828,-31827,-31827,-31826,-31825,-31824,-31824,-31823,-31822,-31821,-31821,-31820,-31819,-31818,-31818,-31817,-31816,-31815,-31815,-31814,-31813,-31812,-31812,-31811,-31810,-31809,-31809,-31808,-31807,-31806,-31806,-31805,-31804,-31803,-31802,-31802,-31801,-31800,-31799,-31799,-31798,-31797,-31796,-31796,-31795,-31794,-31793,-31793,-31792,-31791,-31790,-31790,-31789,-31788,-31787,-31787,-31786,-31785,-31784,-31783,-31783,-31782,-31781,-31780,-31780,-31779,-31778,-31777,-31777,-31776,-31775,-31774,-31774,-31773,-31772,-31771,-31770,-31770,-31769,-31768,-31767,-31767,-31766,-31765,-31764,-31764,-31763,-31762,-31761,-31760,-31760,-31759,-31758,-31757,-31757,-31756,-31755,-31754,-31753,-31753,-31752,-31751,-31750,-31750,-31749,-31748,-31747,-31746,-31746,-31745,-31744,-31743,-31743,-31742,-31741,-31740,-31739,-31739,-31738,-31737,-31736,-31736,-31735,-31734,-31733,-31732,-31732,-31731,-31730,-31729,-31729,-31728,-31727,-31726,-31725,-31725,-31724,-31723,-31722,-31721,-31721,-31720,-31719,-31718,-31718,-31717,-31716,-31715,-31714,-31714,-31713,-31712,-31711,-31710,-31710,-31709,-31708,-31707,-31706,-31706,-31705,-31704,-31703,-31702,-31702,-31701,-31700,-31699,-31698,-31698,-31697,-31696,-31695,-31695,-31694,-31693,-31692,-31691,-31691,-31690,-31689,-31688,-31687,-31687,-31686,-31685,-31684,-31683,-31683,-31682,-31681,-31680,-31679,-31679,-31678,-31677,-31676,-31675,-31674,-31674,-31673,-31672,-31671,-31670,-31670,-31669,-31668,-31667,-31666,-31666,-31665,-31664,-31663,-31662,-31662,-31661,-31660,-31659,-31658,-31658,-31657,-31656,-31655,-31654,-31653,-31653,-31652,-31651,-31650,-31649,-31649,-31648,-31647,-31646,-31645,-31645,-31644,-31643,-31642,-31641,-31640,-31640,-31639,-31638,-31637,-31636,-31636,-31635,-31634,-31633,-31632,-31631,-31631,-31630,-31629,-31628,-31627,-31627,-31626,-31625,-31624,-31623,-31622,-31622,-31621,-31620,-31619,-31618,-31617,-31617,-31616,-31615,-31614,-31613,-31613,-31612,-31611,-31610,-31609,-31608,-31608,-31607,-31606,-31605,-31604,-31603,-31603,-31602,-31601,-31600,-31599,-31598,-31598,-31597,-31596,-31595,-31594,-31593,-31593,-31592,-31591,-31590,-31589,-31588,-31588,-31587,-31586,-31585,-31584,-31583,-31583,-31582,-31581,-31580,-31579,-31578,-31578,-31577,-31576,-31575,-31574,-31573,-31572,-31572,-31571,-31570,-31569,-31568,-31567,-31567,-31566,-31565,-31564,-31563,-31562,-31562,-31561,-31560,-31559,-31558,-31557,-31556,-31556,-31555,-31554,-31553,-31552,-31551,-31551,-31550,-31549,-31548,-31547,-31546,-31545,-31545,-31544,-31543,-31542,-31541,-31540,-31539,-31539,-31538,-31537,-31536,-31535,-31534,-31534,-31533,-31532,-31531,-31530,-31529,-31528,-31528,-31527,-31526,-31525,-31524,-31523,-31522,-31522,-31521,-31520,-31519,-31518,-31517,-31516,-31516,-31515,-31514,-31513,-31512,-31511,-31510,-31510,-31509,-31508,-31507,-31506,-31505,-31504,-31503,-31503,-31502,-31501,-31500,-31499,-31498,-31497,-31497,-31496,-31495,-31494,-31493,-31492,-31491,-31490,-31490,-31489,-31488,-31487,-31486,-31485,-31484,-31484,-31483,-31482,-31481,-31480,-31479,-31478,-31477,-31477,-31476,-31475,-31474,-31473,-31472,-31471,-31470,-31470,-31469,-31468,-31467,-31466,-31465,-31464,-31463,-31463,-31462,-31461,-31460,-31459,-31458,-31457,-31456,-31456,-31455,-31454,-31453,-31452,-31451,-31450,-31449,-31448,-31448,-31447,-31446,-31445,-31444,-31443,-31442,-31441,-31441,-31440,-31439,-31438,-31437,-31436,-31435,-31434,-31433,-31433,-31432,-31431,-31430,-31429,-31428,-31427,-31426,-31425,-31425,-31424,-31423,-31422,-31421,-31420,-31419,-31418,-31417,-31417,-31416,-31415,-31414,-31413,-31412,-31411,-31410,-31409,-31408,-31408,-31407,-31406,-31405,-31404,-31403,-31402,-31401,-31400,-31400,-31399,-31398,-31397,-31396,-31395,-31394,-31393,-31392,-31391,-31391,-31390,-31389,-31388,-31387,-31386,-31385,-31384,-31383,-31382,-31381,-31381,-31380,-31379,-31378,-31377,-31376,-31375,-31374,-31373,-31372,-31372,-31371,-31370,-31369,-31368,-31367,-31366,-31365,-31364,-31363,-31362,-31362,-31361,-31360,-31359,-31358,-31357,-31356,-31355,-31354,-31353,-31352,-31352,-31351,-31350,-31349,-31348,-31347,-31346,-31345,-31344,-31343,-31342,-31341,-31341,-31340,-31339,-31338,-31337,-31336,-31335,-31334,-31333,-31332,-31331,-31330,-31329,-31329,-31328,-31327,-31326,-31325,-31324,-31323,-31322,-31321,-31320,-31319,-31318,-31318,-31317,-31316,-31315,-31314,-31313,-31312,-31311,-31310,-31309,-31308,-31307,-31306,-31305,-31305,-31304,-31303,-31302,-31301,-31300,-31299,-31298,-31297,-31296,-31295,-31294,-31293,-31292,-31292,-31291,-31290,-31289,-31288,-31287,-31286,-31285,-31284,-31283,-31282,-31281,-31280,-31279,-31278,-31278,-31277,-31276,-31275,-31274,-31273,-31272,-31271,-31270,-31269,-31268,-31267,-31266,-31265,-31264,-31263,-31262,-31262,-31261,-31260,-31259,-31258,-31257,-31256,-31255,-31254,-31253,-31252,-31251,-31250,-31249,-31248,-31247,-31246,-31246,-31245,-31244,-31243,-31242,-31241,-31240,-31239,-31238,-31237,-31236,-31235,-31234,-31233,-31232,-31231,-31230,-31229,-31228,-31227,-31227,-31226,-31225,-31224,-31223,-31222,-31221,-31220,-31219,-31218,-31217,-31216,-31215,-31214,-31213,-31212,-31211,-31210,-31209,-31208,-31207,-31206,-31206,-31205,-31204,-31203,-31202,-31201,-31200,-31199,-31198,-31197,-31196,-31195,-31194,-31193,-31192,-31191,-31190,-31189,-31188,-31187,-31186,-31185,-31184,-31183,-31182,-31181,-31181,-31180,-31179,-31178,-31177,-31176,-31175,-31174,-31173,-31172,-31171,-31170,-31169,-31168,-31167,-31166,-31165,-31164,-31163,-31162,-31161,-31160,-31159,-31158,-31157,-31156,-31155,-31154,-31153,-31152,-31151,-31150,-31149,-31148,-31148,-31147,-31146,-31145,-31144,-31143,-31142,-31141,-31140,-31139,-31138,-31137,-31136,-31135,-31134,-31133,-31132,-31131,-31130,-31129,-31128,-31127,-31126,-31125,-31124,-31123,-31122,-31121,-31120,-31119,-31118,-31117,-31116,-31115,-31114,-31113,-31112,-31111,-31110,-31109,-31108,-31107,-31106,-31105,-31104,-31103,-31102,-31101,-31100,-31099,-31098,-31097,-31096,-31095,-31094,-31093,-31092,-31091,-31090,-31089,-31088,-31087,-31086,-31085,-31084,-31083,-31083,-31082,-31081,-31080,-31079,-31078,-31077,-31076,-31075,-31074,-31073,-31072,-31071,-31070,-31069,-31068,-31067,-31066,-31065,-31064,-31063,-31062,-31061,-31060,-31059,-31058,-31057,-31056,-31055,-31054,-31053,-31052,-31051,-31050,-31049,-31048,-31047,-31046,-31045,-31044,-31043,-31041,-31040,-31039,-31038,-31037,-31036,-31035,-31034,-31033,-31032,-31031,-31030,-31029,-31028,-31027,-31026,-31025,-31024,-31023,-31022,-31021,-31020,-31019,-31018,-31017,-31016,-31015,-31014,-31013,-31012,-31011,-31010,-31009,-31008,-31007,-31006,-31005,-31004,-31003,-31002,-31001,-31000,-30999,-30998,-30997,-30996,-30995,-30994,-30993,-30992,-30991,-30990,-30989,-30988,-30987,-30986,-30985,-30984,-30983,-30982,-30981,-30980,-30979,-30978,-30977,-30976,-30974,-30973,-30972,-30971,-30970,-30969,-30968,-30967,-30966,-30965,-30964,-30963,-30962,-30961,-30960,-30959,-30958,-30957,-30956,-30955,-30954,-30953,-30952,-30951,-30950,-30949,-30948,-30947,-30946,-30945,-30944,-30943,-30942,-30941,-30939,-30938,-30937,-30936,-30935,-30934,-30933,-30932,-30931,-30930,-30929,-30928,-30927,-30926,-30925,-30924,-30923,-30922,-30921,-30920,-30919,-30918,-30917,-30916,-30915,-30914,-30912,-30911,-30910,-30909,-30908,-30907,-30906,-30905,-30904,-30903,-30902,-30901,-30900,-30899,-30898,-30897,-30896,-30895,-30894,-30893,-30892,-30891,-30889,-30888,-30887,-30886,-30885,-30884,-30883,-30882,-30881,-30880,-30879,-30878,-30877,-30876,-30875,-30874,-30873,-30872,-30871,-30870,-30868,-30867,-30866,-30865,-30864,-30863,-30862,-30861,-30860,-30859,-30858,-30857,-30856,-30855,-30854,-30853,-30852,-30851,-30849,-30848,-30847,-30846,-30845,-30844,-30843,-30842,-30841,-30840,-30839,-30838,-30837,-30836,-30835,-30834,-30832,-30831,-30830,-30829,-30828,-30827,-30826,-30825,-30824,-30823,-30822,-30821,-30820,-30819,-30818,-30816,-30815,-30814,-30813,-30812,-30811,-30810,-30809,-30808,-30807,-30806,-30805,-30804,-30803,-30802,-30800,-30799,-30798,-30797,-30796,-30795,-30794,-30793,-30792,-30791,-30790,-30789,-30788,-30786,-30785,-30784,-30783,-30782,-30781,-30780,-30779,-30778,-30777,-30776,-30775,-30774,-30772,-30771,-30770,-30769,-30768,-30767,-30766,-30765,-30764,-30763,-30762,-30761,-30760,-30758,-30757,-30756,-30755,-30754,-30753,-30752,-30751,-30750,-30749,-30748,-30746,-30745,-30744,-30743,-30742,-30741,-30740,-30739,-30738,-30737,-30736,-30735,-30733,-30732,-30731,-30730,-30729,-30728,-30727,-30726,-30725,-30724,-30723,-30721,-30720,-30719,-30718,-30717,-30716,-30715,-30714,-30713,-30712,-30711,-30709,-30708,-30707,-30706,-30705,-30704,-30703,-30702,-30701,-30700,-30698,-30697,-30696,-30695,-30694,-30693,-30692,-30691,-30690,-30689,-30687,-30686,-30685,-30684,-30683,-30682,-30681,-30680,-30679,-30678,-30676,-30675,-30674,-30673,-30672,-30671,-30670,-30669,-30668,-30666,-30665,-30664,-30663,-30662,-30661,-30660,-30659,-30658,-30656,-30655,-30654,-30653,-30652,-30651,-30650,-30649,-30648,-30646,-30645,-30644,-30643,-30642,-30641,-30640,-30639,-30638,-30636,-30635,-30634,-30633,-30632,-30631,-30630,-30629,-30628,-30626,-30625,-30624,-30623,-30622,-30621,-30620,-30619,-30617,-30616,-30615,-30614,-30613,-30612,-30611,-30610,-30609,-30607,-30606,-30605,-30604,-30603,-30602,-30601,-30600,-30598,-30597,-30596,-30595,-30594,-30593,-30592,-30591,-30589,-30588,-30587,-30586,-30585,-30584,-30583,-30582,-30580,-30579,-30578,-30577,-30576,-30575,-30574,-30573,-30571,-30570,-30569,-30568,-30567,-30566,-30565,-30563,-30562,-30561,-30560,-30559,-30558,-30557,-30556,-30554,-30553,-30552,-30551,-30550,-30549,-30548,-30546,-30545,-30544,-30543,-30542,-30541,-30540,-30538,-30537,-30536,-30535,-30534,-30533,-30532,-30530,-30529,-30528,-30527,-30526,-30525,-30524,-30522,-30521,-30520,-30519,-30518,-30517,-30516,-30514,-30513,-30512,-30511,-30510,-30509,-30508,-30506,-30505,-30504,-30503,-30502,-30501,-30500,-30498,-30497,-30496,-30495,-30494,-30493,-30492,-30490,-30489,-30488,-30487,-30486,-30485,-30483,-30482,-30481,-30480,-30479,-30478,-30477,-30475,-30474,-30473,-30472,-30471,-30470,-30468,-30467,-30466,-30465,-30464,-30463,-30462,-30460,-30459,-30458,-30457,-30456,-30455,-30453,-30452,-30451,-30450,-30449,-30448,-30446,-30445,-30444,-30443,-30442,-30441,-30439,-30438,-30437,-30436,-30435,-30434,-30433,-30431,-30430,-30429,-30428,-30427,-30426,-30424,-30423,-30422,-30421,-30420,-30419,-30417,-30416,-30415,-30414,-30413,-30412,-30410,-30409,-30408,-30407,-30406,-30404,-30403,-30402,-30401,-30400,-30399,-30397,-30396,-30395,-30394,-30393,-30392,-30390,-30389,-30388,-30387,-30386,-30385,-30383,-30382,-30381,-30380,-30379,-30377,-30376,-30375,-30374,-30373,-30372,-30370,-30369,-30368,-30367,-30366,-30365,-30363,-30362,-30361,-30360,-30359,-30357,-30356,-30355,-30354,-30353,-30351,-30350,-30349,-30348,-30347,-30346,-30344,-30343,-30342,-30341,-30340,-30338,-30337,-30336,-30335,-30334,-30333,-30331,-30330,-30329,-30328,-30327,-30325,-30324,-30323,-30322,-30321,-30319,-30318,-30317,-30316,-30315,-30313,-30312,-30311,-30310,-30309,-30308,-30306,-30305,-30304,-30303,-30302,-30300,-30299,-30298,-30297,-30296,-30294,-30293,-30292,-30291,-30290,-30288,-30287,-30286,-30285,-30284,-30282,-30281,-30280,-30279,-30278,-30276,-30275,-30274,-30273,-30272,-30270,-30269,-30268,-30267,-30266,-30264,-30263,-30262,-30261,-30260,-30258,-30257,-30256,-30255,-30253,-30252,-30251,-30250,-30249,-30247,-30246,-30245,-30244,-30243,-30241,-30240,-30239,-30238,-30237,-30235,-30234,-30233,-30232,-30231,-30229,-30228,-30227,-30226,-30224,-30223,-30222,-30221,-30220,-30218,-30217,-30216,-30215,-30214,-30212,-30211,-30210,-30209,-30207,-30206,-30205,-30204,-30203,-30201,-30200,-30199,-30198,-30196,-30195,-30194,-30193,-30192,-30190,-30189,-30188,-30187,-30185,-30184,-30183,-30182,-30181,-30179,-30178,-30177,-30176,-30174,-30173,-30172,-30171,-30170,-30168,-30167,-30166,-30165,-30163,-30162,-30161,-30160,-30159,-30157,-30156,-30155,-30154,-30152,-30151,-30150,-30149,-30147,-30146,-30145,-30144,-30143,-30141,-30140,-30139,-30138,-30136,-30135,-30134,-30133,-30131,-30130,-30129,-30128,-30126,-30125,-30124,-30123,-30122,-30120,-30119,-30118,-30117,-30115,-30114,-30113,-30112,-30110,-30109,-30108,-30107,-30105,-30104,-30103,-30102,-30100,-30099,-30098,-30097,-30096,-30094,-30093,-30092,-30091,-30089,-30088,-30087,-30086,-30084,-30083,-30082,-30081,-30079,-30078,-30077,-30076,-30074,-30073,-30072,-30071,-30069,-30068,-30067,-30066,-30064,-30063,-30062,-30061,-30059,-30058,-30057,-30056,-30054,-30053,-30052,-30051,-30049,-30048,-30047,-30046,-30044,-30043,-30042,-30041,-30039,-30038,-30037,-30036,-30034,-30033,-30032,-30031,-30029,-30028,-30027,-30026,-30024,-30023,-30022,-30020,-30019,-30018,-30017,-30015,-30014,-30013,-30012,-30010,-30009,-30008,-30007,-30005,-30004,-30003,-30002,-30000,-29999,-29998,-29997,-29995,-29994,-29993,-29991,-29990,-29989,-29988,-29986,-29985,-29984,-29983,-29981,-29980,-29979,-29978,-29976,-29975,-29974,-29972,-29971,-29970,-29969,-29967,-29966,-29965,-29964,-29962,-29961,-29960,-29958,-29957,-29956,-29955,-29953,-29952,-29951,-29950,-29948,-29947,-29946,-29944,-29943,-29942,-29941,-29939,-29938,-29937,-29936,-29934,-29933,-29932,-29930,-29929,-29928,-29927,-29925,-29924,-29923,-29921,-29920,-29919,-29918,-29916,-29915,-29914,-29912,-29911,-29910,-29909,-29907,-29906,-29905,-29903,-29902,-29901,-29900,-29898,-29897,-29896,-29894,-29893,-29892,-29891,-29889,-29888,-29887,-29885,-29884,-29883,-29882,-29880,-29879,-29878,-29876,-29875,-29874,-29873,-29871,-29870,-29869,-29867,-29866,-29865,-29864,-29862,-29861,-29860,-29858,-29857,-29856,-29854,-29853,-29852,-29851,-29849,-29848,-29847,-29845,-29844,-29843,-29842,-29840,-29839,-29838,-29836,-29835,-29834,-29832,-29831,-29830,-29829,-29827,-29826,-29825,-29823,-29822,-29821,-29819,-29818,-29817,-29816,-29814,-29813,-29812,-29810,-29809,-29808,-29806,-29805,-29804,-29802,-29801,-29800,-29799,-29797,-29796,-29795,-29793,-29792,-29791,-29789,-29788,-29787,-29785,-29784,-29783,-29782,-29780,-29779,-29778,-29776,-29775,-29774,-29772,-29771,-29770,-29768,-29767,-29766,-29764,-29763,-29762,-29761,-29759,-29758,-29757,-29755,-29754,-29753,-29751,-29750,-29749,-29747,-29746,-29745,-29743,-29742,-29741,-29739,-29738,-29737,-29736,-29734,-29733,-29732,-29730,-29729,-29728,-29726,-29725,-29724,-29722,-29721,-29720,-29718,-29717,-29716,-29714,-29713,-29712,-29710,-29709,-29708,-29706,-29705,-29704,-29702,-29701,-29700,-29698,-29697,-29696,-29694,-29693,-29692,-29690,-29689,-29688,-29687,-29685,-29684,-29683,-29681,-29680,-29679,-29677,-29676,-29675,-29673,-29672,-29671,-29669,-29668,-29667,-29665,-29664,-29663,-29661,-29660,-29659,-29657,-29656,-29655,-29653,-29652,-29651,-29649,-29648,-29646,-29645,-29644,-29642,-29641,-29640,-29638,-29637,-29636,-29634,-29633,-29632,-29630,-29629,-29628,-29626,-29625,-29624,-29622,-29621,-29620,-29618,-29617,-29616,-29614,-29613,-29612,-29610,-29609,-29608,-29606,-29605,-29604,-29602,-29601,-29599,-29598,-29597,-29595,-29594,-29593,-29591,-29590,-29589,-29587,-29586,-29585,-29583,-29582,-29581,-29579,-29578,-29577,-29575,-29574,-29572,-29571,-29570,-29568,-29567,-29566,-29564,-29563,-29562,-29560,-29559,-29558,-29556,-29555,-29554,-29552,-29551,-29549,-29548,-29547,-29545,-29544,-29543,-29541,-29540,-29539,-29537,-29536,-29534,-29533,-29532,-29530,-29529,-29528,-29526,-29525,-29524,-29522,-29521,-29520,-29518,-29517,-29515,-29514,-29513,-29511,-29510,-29509,-29507,-29506,-29504,-29503,-29502,-29500,-29499,-29498,-29496,-29495,-29494,-29492,-29491,-29489,-29488,-29487,-29485,-29484,-29483,-29481,-29480,-29478,-29477,-29476,-29474,-29473,-29472,-29470,-29469,-29468,-29466,-29465,-29463,-29462,-29461,-29459,-29458,-29457,-29455,-29454,-29452,-29451,-29450,-29448,-29447,-29445,-29444,-29443,-29441,-29440,-29439,-29437,-29436,-29434,-29433,-29432,-29430,-29429,-29428,-29426,-29425,-29423,-29422,-29421,-29419,-29418,-29416,-29415,-29414,-29412,-29411,-29410,-29408,-29407,-29405,-29404,-29403,-29401,-29400,-29398,-29397,-29396,-29394,-29393,-29392,-29390,-29389,-29387,-29386,-29385,-29383,-29382,-29380,-29379,-29378,-29376,-29375,-29373,-29372,-29371,-29369,-29368,-29366,-29365,-29364,-29362,-29361,-29360,-29358,-29357,-29355,-29354,-29353,-29351,-29350,-29348,-29347,-29346,-29344,-29343,-29341,-29340,-29339,-29337,-29336,-29334,-29333,-29332,-29330,-29329,-29327,-29326,-29325,-29323,-29322,-29320,-29319,-29318,-29316,-29315,-29313,-29312,-29311,-29309,-29308,-29306,-29305,-29304,-29302,-29301,-29299,-29298,-29296,-29295,-29294,-29292,-29291,-29289,-29288,-29287,-29285,-29284,-29282,-29281,-29280,-29278,-29277,-29275,-29274,-29273,-29271,-29270,-29268,-29267,-29265,-29264,-29263,-29261,-29260,-29258,-29257,-29256,-29254,-29253,-29251,-29250,-29248,-29247,-29246,-29244,-29243,-29241,-29240,-29239,-29237,-29236,-29234,-29233,-29231,-29230,-29229,-29227,-29226,-29224,-29223,-29222,-29220,-29219,-29217,-29216,-29214,-29213,-29212,-29210,-29209,-29207,-29206,-29204,-29203,-29202,-29200,-29199,-29197,-29196,-29194,-29193,-29192,-29190,-29189,-29187,-29186,-29184,-29183,-29182,-29180,-29179,-29177,-29176,-29174,-29173,-29172,-29170,-29169,-29167,-29166,-29164,-29163,-29162,-29160,-29159,-29157,-29156,-29154,-29153,-29152,-29150,-29149,-29147,-29146,-29144,-29143,-29142,-29140,-29139,-29137,-29136,-29134,-29133,-29131,-29130,-29129,-29127,-29126,-29124,-29123,-29121,-29120,-29118,-29117,-29116,-29114,-29113,-29111,-29110,-29108,-29107,-29106,-29104,-29103,-29101,-29100,-29098,-29097,-29095,-29094,-29093,-29091,-29090,-29088,-29087,-29085,-29084,-29082,-29081,-29079,-29078,-29077,-29075,-29074,-29072,-29071,-29069,-29068,-29066,-29065,-29064,-29062,-29061,-29059,-29058,-29056,-29055,-29053,-29052,-29050,-29049,-29048,-29046,-29045,-29043,-29042,-29040,-29039,-29037,-29036,-29034,-29033,-29032,-29030,-29029,-29027,-29026,-29024,-29023,-29021,-29020,-29018,-29017,-29016,-29014,-29013,-29011,-29010,-29008,-29007,-29005,-29004,-29002,-29001,-28999,-28998,-28997,-28995,-28994,-28992,-28991,-28989,-28988,-28986,-28985,-28983,-28982,-28980,-28979,-28977,-28976,-28975,-28973,-28972,-28970,-28969,-28967,-28966,-28964,-28963,-28961,-28960,-28958,-28957,-28955,-28954,-28953,-28951,-28950,-28948,-28947,-28945,-28944,-28942,-28941,-28939,-28938,-28936,-28935,-28933,-28932,-28930,-28929,-28927,-28926,-28925,-28923,-28922,-28920,-28919,-28917,-28916,-28914,-28913,-28911,-28910,-28908,-28907,-28905,-28904,-28902,-28901,-28899,-28898,-28896,-28895,-28893,-28892,-28891,-28889,-28888,-28886,-28885,-28883,-28882,-28880,-28879,-28877,-28876,-28874,-28873,-28871,-28870,-28868,-28867,-28865,-28864,-28862,-28861,-28859,-28858,-28856,-28855,-28853,-28852,-28850,-28849,-28847,-28846,-28844,-28843,-28841,-28840,-28838,-28837,-28835,-28834,-28832,-28831,-28830,-28828,-28827,-28825,-28824,-28822,-28821,-28819,-28818,-28816,-28815,-28813,-28812,-28810,-28809,-28807,-28806,-28804,-28803,-28801,-28800,-28798,-28797,-28795,-28794,-28792,-28791,-28789,-28788,-28786,-28785,-28783,-28782,-28780,-28779,-28777,-28776,-28774,-28773,-28771,-28770,-28768,-28767,-28765,-28764,-28762,-28761,-28759,-28758,-28756,-28755,-28753,-28752,-28750,-28748,-28747,-28745,-28744,-28742,-28741,-28739,-28738,-28736,-28735,-28733,-28732,-28730,-28729,-28727,-28726,-28724,-28723,-28721,-28720,-28718,-28717,-28715,-28714,-28712,-28711,-28709,-28708,-28706,-28705,-28703,-28702,-28700,-28699,-28697,-28696,-28694,-28693,-28691,-28690,-28688,-28686,-28685,-28683,-28682,-28680,-28679,-28677,-28676,-28674,-28673,-28671,-28670,-28668,-28667,-28665,-28664,-28662,-28661,-28659,-28658,-28656,-28655,-28653,-28651,-28650,-28648,-28647,-28645,-28644,-28642,-28641,-28639,-28638,-28636,-28635,-28633,-28632,-28630,-28629,-28627,-28626,-28624,-28622,-28621,-28619,-28618,-28616,-28615,-28613,-28612,-28610,-28609,-28607,-28606,-28604,-28603,-28601,-28600,-28598,-28596,-28595,-28593,-28592,-28590,-28589,-28587,-28586,-28584,-28583,-28581,-28580,-28578,-28576,-28575,-28573,-28572,-28570,-28569,-28567,-28566,-28564,-28563,-28561,-28560,-28558,-28556,-28555,-28553,-28552,-28550,-28549,-28547,-28546,-28544,-28543,-28541,-28540,-28538,-28536,-28535,-28533,-28532,-28530,-28529,-28527,-28526,-28524,-28523,-28521,-28519,-28518,-28516,-28515,-28513,-28512,-28510,-28509,-28507,-28505,-28504,-28502,-28501,-28499,-28498,-28496,-28495,-28493,-28492,-28490,-28488,-28487,-28485,-28484,-28482,-28481,-28479,-28478,-28476,-28474,-28473,-28471,-28470,-28468,-28467,-28465,-28464,-28462,-28460,-28459,-28457,-28456,-28454,-28453,-28451,-28450,-28448,-28446,-28445,-28443,-28442,-28440,-28439,-28437,-28436,-28434,-28432,-28431,-28429,-28428,-28426,-28425,-28423,-28421,-28420,-28418,-28417,-28415,-28414,-28412,-28411,-28409,-28407,-28406,-28404,-28403,-28401,-28400,-28398,-28396,-28395,-28393,-28392,-28390,-28389,-28387,-28385,-28384,-28382,-28381,-28379,-28378,-28376,-28374,-28373,-28371,-28370,-28368,-28367,-28365,-28363,-28362,-28360,-28359,-28357,-28356,-28354,-28352,-28351,-28349,-28348,-28346,-28345,-28343,-28341,-28340,-28338,-28337,-28335,-28333,-28332,-28330,-28329,-28327,-28326,-28324,-28322,-28321,-28319,-28318,-28316,-28315,-28313,-28311,-28310,-28308,-28307,-28305,-28303,-28302,-28300,-28299,-28297,-28296,-28294,-28292,-28291,-28289,-28288,-28286,-28284,-28283,-28281,-28280,-28278,-28277,-28275,-28273,-28272,-28270,-28269,-28267,-28265,-28264,-28262,-28261,-28259,-28257,-28256,-28254,-28253,-28251,-28249,-28248,-28246,-28245,-28243,-28242,-28240,-28238,-28237,-28235,-28234,-28232,-28230,-28229,-28227,-28226,-28224,-28222,-28221,-28219,-28218,-28216,-28214,-28213,-28211,-28210,-28208,-28206,-28205,-28203,-28202,-28200,-28198,-28197,-28195,-28194,-28192,-28190,-28189,-28187,-28186,-28184,-28182,-28181,-28179,-28178,-28176,-28174,-28173,-28171,-28170,-28168,-28166,-28165,-28163,-28162,-28160,-28158,-28157,-28155,-28154,-28152,-28150,-28149,-28147,-28145,-28144,-28142,-28141,-28139,-28137,-28136,-28134,-28133,-28131,-28129,-28128,-28126,-28125,-28123,-28121,-28120,-28118,-28116,-28115,-28113,-28112,-28110,-28108,-28107,-28105,-28104,-28102,-28100,-28099,-28097,-28095,-28094,-28092,-28091,-28089,-28087,-28086,-28084,-28083,-28081,-28079,-28078,-28076,-28074,-28073,-28071,-28070,-28068,-28066,-28065,-28063,-28061,-28060,-28058,-28057,-28055,-28053,-28052,-28050,-28049,-28047,-28045,-28044,-28042,-28040,-28039,-28037,-28036,-28034,-28032,-28031,-28029,-28027,-28026,-28024,-28022,-28021,-28019,-28018,-28016,-28014,-28013,-28011,-28009,-28008,-28006,-28005,-28003,-28001,-28000,-27998,-27996,-27995,-27993,-27992,-27990,-27988,-27987,-27985,-27983,-27982,-27980,-27978,-27977,-27975,-27974,-27972,-27970,-27969,-27967,-27965,-27964,-27962,-27960,-27959,-27957,-27956,-27954,-27952,-27951,-27949,-27947,-27946,-27944,-27942,-27941,-27939,-27937,-27936,-27934,-27933,-27931,-27929,-27928,-27926,-27924,-27923,-27921,-27919,-27918,-27916,-27914,-27913,-27911,-27910,-27908,-27906,-27905,-27903,-27901,-27900,-27898,-27896,-27895,-27893,-27891,-27890,-27888,-27886,-27885,-27883,-27882,-27880,-27878,-27877,-27875,-27873,-27872,-27870,-27868,-27867,-27865,-27863,-27862,-27860,-27858,-27857,-27855,-27853,-27852,-27850,-27848,-27847,-27845,-27843,-27842,-27840,-27839,-27837,-27835,-27834,-27832,-27830,-27829,-27827,-27825,-27824,-27822,-27820,-27819,-27817,-27815,-27814,-27812,-27810,-27809,-27807,-27805,-27804,-27802,-27800,-27799,-27797,-27795,-27794,-27792,-27790,-27789,-27787,-27785,-27784,-27782,-27780,-27779,-27777,-27775,-27774,-27772,-27770,-27769,-27767,-27765,-27764,-27762,-27760,-27759,-27757,-27755,-27754,-27752,-27750,-27749,-27747,-27745,-27744,-27742,-27740,-27739,-27737,-27735,-27734,-27732,-27730,-27729,-27727,-27725,-27724,-27722,-27720,-27719,-27717,-27715,-27714,-27712,-27710,-27708,-27707,-27705,-27703,-27702,-27700,-27698,-27697,-27695,-27693,-27692,-27690,-27688,-27687,-27685,-27683,-27682,-27680,-27678,-27677,-27675,-27673,-27672,-27670,-27668,-27666,-27665,-27663,-27661,-27660,-27658,-27656,-27655,-27653,-27651,-27650,-27648,-27646,-27645,-27643,-27641,-27640,-27638,-27636,-27634,-27633,-27631,-27629,-27628,-27626,-27624,-27623,-27621,-27619,-27618,-27616,-27614,-27613,-27611,-27609,-27607,-27606,-27604,-27602,-27601,-27599,-27597,-27596,-27594,-27592,-27590,-27589,-27587,-27585,-27584,-27582,-27580,-27579,-27577,-27575,-27574,-27572,-27570,-27568,-27567,-27565,-27563,-27562,-27560,-27558,-27557,-27555,-27553,-27551,-27550,-27548,-27546,-27545,-27543,-27541,-27540,-27538,-27536,-27534,-27533,-27531,-27529,-27528,-27526,-27524,-27523,-27521,-27519,-27517,-27516,-27514,-27512,-27511,-27509,-27507,-27505,-27504,-27502,-27500,-27499,-27497,-27495,-27493,-27492,-27490,-27488,-27487,-27485,-27483,-27482,-27480,-27478,-27476,-27475,-27473,-27471,-27470,-27468,-27466,-27464,-27463,-27461,-27459,-27458,-27456,-27454,-27452,-27451,-27449,-27447,-27446,-27444,-27442,-27440,-27439,-27437,-27435,-27434,-27432,-27430,-27428,-27427,-27425,-27423,-27421,-27420,-27418,-27416,-27415,-27413,-27411,-27409,-27408,-27406,-27404,-27403,-27401,-27399,-27397,-27396,-27394,-27392,-27390,-27389,-27387,-27385,-27384,-27382,-27380,-27378,-27377,-27375,-27373,-27372,-27370,-27368,-27366,-27365,-27363,-27361,-27359,-27358,-27356,-27354,-27352,-27351,-27349,-27347,-27346,-27344,-27342,-27340,-27339,-27337,-27335,-27333,-27332,-27330,-27328,-27327,-27325,-27323,-27321,-27320,-27318,-27316,-27314,-27313,-27311,-27309,-27307,-27306,-27304,-27302,-27300,-27299,-27297,-27295,-27294,-27292,-27290,-27288,-27287,-27285,-27283,-27281,-27280,-27278,-27276,-27274,-27273,-27271,-27269,-27267,-27266,-27264,-27262,-27260,-27259,-27257,-27255,-27253,-27252,-27250,-27248,-27247,-27245,-27243,-27241,-27240,-27238,-27236,-27234,-27233,-27231,-27229,-27227,-27226,-27224,-27222,-27220,-27219,-27217,-27215,-27213,-27212,-27210,-27208,-27206,-27205,-27203,-27201,-27199,-27198,-27196,-27194,-27192,-27191,-27189,-27187,-27185,-27184,-27182,-27180,-27178,-27177,-27175,-27173,-27171,-27169,-27168,-27166,-27164,-27162,-27161,-27159,-27157,-27155,-27154,-27152,-27150,-27148,-27147,-27145,-27143,-27141,-27140,-27138,-27136,-27134,-27133,-27131,-27129,-27127,-27126,-27124,-27122,-27120,-27118,-27117,-27115,-27113,-27111,-27110,-27108,-27106,-27104,-27103,-27101,-27099,-27097,-27096,-27094,-27092,-27090,-27088,-27087,-27085,-27083,-27081,-27080,-27078,-27076,-27074,-27073,-27071,-27069,-27067,-27065,-27064,-27062,-27060,-27058,-27057,-27055,-27053,-27051,-27049,-27048,-27046,-27044,-27042,-27041,-27039,-27037,-27035,-27034,-27032,-27030,-27028,-27026,-27025,-27023,-27021,-27019,-27018,-27016,-27014,-27012,-27010,-27009,-27007,-27005,-27003,-27002,-27000,-26998,-26996,-26994,-26993,-26991,-26989,-26987,-26986,-26984,-26982,-26980,-26978,-26977,-26975,-26973,-26971,-26969,-26968,-26966,-26964,-26962,-26961,-26959,-26957,-26955,-26953,-26952,-26950,-26948,-26946,-26944,-26943,-26941,-26939,-26937,-26936,-26934,-26932,-26930,-26928,-26927,-26925,-26923,-26921,-26919,-26918,-26916,-26914,-26912,-26910,-26909,-26907,-26905,-26903,-26901,-26900,-26898,-26896,-26894,-26893,-26891,-26889,-26887,-26885,-26884,-26882,-26880,-26878,-26876,-26875,-26873,-26871,-26869,-26867,-26866,-26864,-26862,-26860,-26858,-26857,-26855,-26853,-26851,-26849,-26848,-26846,-26844,-26842,-26840,-26839,-26837,-26835,-26833,-26831,-26830,-26828,-26826,-26824,-26822,-26821,-26819,-26817,-26815,-26813,-26811,-26810,-26808,-26806,-26804,-26802,-26801,-26799,-26797,-26795,-26793,-26792,-26790,-26788,-26786,-26784,-26783,-26781,-26779,-26777,-26775,-26774,-26772,-26770,-26768,-26766,-26764,-26763,-26761,-26759,-26757,-26755,-26754,-26752,-26750,-26748,-26746,-26745,-26743,-26741,-26739,-26737,-26735,-26734,-26732,-26730,-26728,-26726,-26725,-26723,-26721,-26719,-26717,-26715,-26714,-26712,-26710,-26708,-26706,-26705,-26703,-26701,-26699,-26697,-26695,-26694,-26692,-26690,-26688,-26686,-26684,-26683,-26681,-26679,-26677,-26675,-26674,-26672,-26670,-26668,-26666,-26664,-26663,-26661,-26659,-26657,-26655,-26653,-26652,-26650,-26648,-26646,-26644,-26642,-26641,-26639,-26637,-26635,-26633,-26631,-26630,-26628,-26626,-26624,-26622,-26621,-26619,-26617,-26615,-26613,-26611,-26610,-26608,-26606,-26604,-26602,-26600,-26599,-26597,-26595,-26593,-26591,-26589,-26588,-26586,-26584,-26582,-26580,-26578,-26576,-26575,-26573,-26571,-26569,-26567,-26565,-26564,-26562,-26560,-26558,-26556,-26554,-26553,-26551,-26549,-26547,-26545,-26543,-26542,-26540,-26538,-26536,-26534,-26532,-26530,-26529,-26527,-26525,-26523,-26521,-26519,-26518,-26516,-26514,-26512,-26510,-26508,-26506,-26505,-26503,-26501,-26499,-26497,-26495,-26494,-26492,-26490,-26488,-26486,-26484,-26482,-26481,-26479,-26477,-26475,-26473,-26471,-26469,-26468,-26466,-26464,-26462,-26460,-26458,-26457,-26455,-26453,-26451,-26449,-26447,-26445,-26444,-26442,-26440,-26438,-26436,-26434,-26432,-26431,-26429,-26427,-26425,-26423,-26421,-26419,-26418,-26416,-26414,-26412,-26410,-26408,-26406,-26405,-26403,-26401,-26399,-26397,-26395,-26393,-26392,-26390,-26388,-26386,-26384,-26382,-26380,-26378,-26377,-26375,-26373,-26371,-26369,-26367,-26365,-26364,-26362,-26360,-26358,-26356,-26354,-26352,-26350,-26349,-26347,-26345,-26343,-26341,-26339,-26337,-26336,-26334,-26332,-26330,-26328,-26326,-26324,-26322,-26321,-26319,-26317,-26315,-26313,-26311,-26309,-26307,-26306,-26304,-26302,-26300,-26298,-26296,-26294,-26292,-26291,-26289,-26287,-26285,-26283,-26281,-26279,-26277,-26276,-26274,-26272,-26270,-26268,-26266,-26264,-26262,-26261,-26259,-26257,-26255,-26253,-26251,-26249,-26247,-26246,-26244,-26242,-26240,-26238,-26236,-26234,-26232,-26230,-26229,-26227,-26225,-26223,-26221,-26219,-26217,-26215,-26214,-26212,-26210,-26208,-26206,-26204,-26202,-26200,-26198,-26197,-26195,-26193,-26191,-26189,-26187,-26185,-26183,-26181,-26180,-26178,-26176,-26174,-26172,-26170,-26168,-26166,-26164,-26163,-26161,-26159,-26157,-26155,-26153,-26151,-26149,-26147,-26146,-26144,-26142,-26140,-26138,-26136,-26134,-26132,-26130,-26128,-26127,-26125,-26123,-26121,-26119,-26117,-26115,-26113,-26111,-26109,-26108,-26106,-26104,-26102,-26100,-26098,-26096,-26094,-26092,-26090,-26089,-26087,-26085,-26083,-26081,-26079,-26077,-26075,-26073,-26071,-26070,-26068,-26066,-26064,-26062,-26060,-26058,-26056,-26054,-26052,-26051,-26049,-26047,-26045,-26043,-26041,-26039,-26037,-26035,-26033,-26031,-26030,-26028,-26026,-26024,-26022,-26020,-26018,-26016,-26014,-26012,-26010,-26009,-26007,-26005,-26003,-26001,-25999,-25997,-25995,-25993,-25991,-25989,-25988,-25986,-25984,-25982,-25980,-25978,-25976,-25974,-25972,-25970,-25968,-25966,-25965,-25963,-25961,-25959,-25957,-25955,-25953,-25951,-25949,-25947,-25945,-25943,-25942,-25940,-25938,-25936,-25934,-25932,-25930,-25928,-25926,-25924,-25922,-25920,-25918,-25917,-25915,-25913,-25911,-25909,-25907,-25905,-25903,-25901,-25899,-25897,-25895,-25893,-25892,-25890,-25888,-25886,-25884,-25882,-25880,-25878,-25876,-25874,-25872,-25870,-25868,-25866,-25865,-25863,-25861,-25859,-25857,-25855,-25853,-25851,-25849,-25847,-25845,-25843,-25841,-25839,-25838,-25836,-25834,-25832,-25830,-25828,-25826,-25824,-25822,-25820,-25818,-25816,-25814,-25812,-25810,-25809,-25807,-25805,-25803,-25801,-25799,-25797,-25795,-25793,-25791,-25789,-25787,-25785,-25783,-25781,-25779,-25778,-25776,-25774,-25772,-25770,-25768,-25766,-25764,-25762,-25760,-25758,-25756,-25754,-25752,-25750,-25748,-25746,-25745,-25743,-25741,-25739,-25737,-25735,-25733,-25731,-25729,-25727,-25725,-25723,-25721,-25719,-25717,-25715,-25713,-25711,-25710,-25708,-25706,-25704,-25702,-25700,-25698,-25696,-25694,-25692,-25690,-25688,-25686,-25684,-25682,-25680,-25678,-25676,-25674,-25672,-25671,-25669,-25667,-25665,-25663,-25661,-25659,-25657,-25655,-25653,-25651,-25649,-25647,-25645,-25643,-25641,-25639,-25637,-25635,-25633,-25631,-25629,-25628,-25626,-25624,-25622,-25620,-25618,-25616,-25614,-25612,-25610,-25608,-25606,-25604,-25602,-25600,-25598,-25596,-25594,-25592,-25590,-25588,-25586,-25584,-25582,-25580,-25578,-25577,-25575,-25573,-25571,-25569,-25567,-25565,-25563,-25561,-25559,-25557,-25555,-25553,-25551,-25549,-25547,-25545,-25543,-25541,-25539,-25537,-25535,-25533,-25531,-25529,-25527,-25525,-25523,-25521,-25519,-25518,-25516,-25514,-25512,-25510,-25508,-25506,-25504,-25502,-25500,-25498,-25496,-25494,-25492,-25490,-25488,-25486,-25484,-25482,-25480,-25478,-25476,-25474,-25472,-25470,-25468,-25466,-25464,-25462,-25460,-25458,-25456,-25454,-25452,-25450,-25448,-25446,-25444,-25442,-25440,-25438,-25437,-25435,-25433,-25431,-25429,-25427,-25425,-25423,-25421,-25419,-25417,-25415,-25413,-25411,-25409,-25407,-25405,-25403,-25401,-25399,-25397,-25395,-25393,-25391,-25389,-25387,-25385,-25383,-25381,-25379,-25377,-25375,-25373,-25371,-25369,-25367,-25365,-25363,-25361,-25359,-25357,-25355,-25353,-25351,-25349,-25347,-25345,-25343,-25341,-25339,-25337,-25335,-25333,-25331,-25329,-25327,-25325,-25323,-25321,-25319,-25317,-25315,-25313,-25311,-25309,-25307,-25305,-25303,-25301,-25299,-25297,-25295,-25293,-25291,-25289,-25287,-25285,-25283,-25281,-25279,-25277,-25275,-25273,-25271,-25269,-25267,-25265,-25263,-25261,-25259,-25257,-25255,-25253,-25251,-25249,-25247,-25245,-25243,-25241,-25239,-25237,-25235,-25233,-25231,-25229,-25227,-25225,-25223,-25221,-25219,-25217,-25215,-25213,-25211,-25209,-25207,-25205,-25203,-25201,-25199,-25197,-25195,-25193,-25191,-25189,-25187,-25185,-25183,-25181,-25179,-25177,-25175,-25173,-25171,-25169,-25167,-25165,-25163,-25161,-25159,-25157,-25155,-25153,-25151,-25149,-25147,-25145,-25143,-25141,-25139,-25137,-25135,-25133,-25131,-25129,-25127,-25125,-25123,-25121,-25119,-25117,-25115,-25113,-25111,-25109,-25107,-25105,-25103,-25101,-25099,-25096,-25094,-25092,-25090,-25088,-25086,-25084,-25082,-25080,-25078,-25076,-25074,-25072,-25070,-25068,-25066,-25064,-25062,-25060,-25058,-25056,-25054,-25052,-25050,-25048,-25046,-25044,-25042,-25040,-25038,-25036,-25034,-25032,-25030,-25028,-25026,-25024,-25022,-25020,-25018,-25016,-25013,-25011,-25009,-25007,-25005,-25003,-25001,-24999,-24997,-24995,-24993,-24991,-24989,-24987,-24985,-24983,-24981,-24979,-24977,-24975,-24973,-24971,-24969,-24967,-24965,-24963,-24961,-24959,-24957,-24955,-24953,-24950,-24948,-24946,-24944,-24942,-24940,-24938,-24936,-24934,-24932,-24930,-24928,-24926,-24924,-24922,-24920,-24918,-24916,-24914,-24912,-24910,-24908,-24906,-24904,-24902,-24899,-24897,-24895,-24893,-24891,-24889,-24887,-24885,-24883,-24881,-24879,-24877,-24875,-24873,-24871,-24869,-24867,-24865,-24863,-24861,-24859,-24857,-24855,-24852,-24850,-24848,-24846,-24844,-24842,-24840,-24838,-24836,-24834,-24832,-24830,-24828,-24826,-24824,-24822,-24820,-24818,-24816,-24814,-24811,-24809,-24807,-24805,-24803,-24801,-24799,-24797,-24795,-24793,-24791,-24789,-24787,-24785,-24783,-24781,-24779,-24777,-24774,-24772,-24770,-24768,-24766,-24764,-24762,-24760,-24758,-24756,-24754,-24752,-24750,-24748,-24746,-24744,-24742,-24740,-24737,-24735,-24733,-24731,-24729,-24727,-24725,-24723,-24721,-24719,-24717,-24715,-24713,-24711,-24709,-24707,-24704,-24702,-24700,-24698,-24696,-24694,-24692,-24690,-24688,-24686,-24684,-24682,-24680,-24678,-24676,-24673,-24671,-24669,-24667,-24665,-24663,-24661,-24659,-24657,-24655,-24653,-24651,-24649,-24647,-24645,-24642,-24640,-24638,-24636,-24634,-24632,-24630,-24628,-24626,-24624,-24622,-24620,-24618,-24616,-24613,-24611,-24609,-24607,-24605,-24603,-24601,-24599,-24597,-24595,-24593,-24591,-24589,-24586,-24584,-24582,-24580,-24578,-24576,-24574,-24572,-24570,-24568,-24566,-24564,-24562,-24559,-24557,-24555,-24553,-24551,-24549,-24547,-24545,-24543,-24541,-24539,-24537,-24534,-24532,-24530,-24528,-24526,-24524,-24522,-24520,-24518,-24516,-24514,-24512,-24509,-24507,-24505,-24503,-24501,-24499,-24497,-24495,-24493,-24491,-24489,-24487,-24484,-24482,-24480,-24478,-24476,-24474,-24472,-24470,-24468,-24466,-24464,-24461,-24459,-24457,-24455,-24453,-24451,-24449,-24447,-24445,-24443,-24441,-24438,-24436,-24434,-24432,-24430,-24428,-24426,-24424,-24422,-24420,-24417,-24415,-24413,-24411,-24409,-24407,-24405,-24403,-24401,-24399,-24397,-24394,-24392,-24390,-24388,-24386,-24384,-24382,-24380,-24378,-24376,-24373,-24371,-24369,-24367,-24365,-24363,-24361,-24359,-24357,-24355,-24352,-24350,-24348,-24346,-24344,-24342,-24340,-24338,-24336,-24334,-24331,-24329,-24327,-24325,-24323,-24321,-24319,-24317,-24315,-24312,-24310,-24308,-24306,-24304,-24302,-24300,-24298,-24296,-24294,-24291,-24289,-24287,-24285,-24283,-24281,-24279,-24277,-24275,-24272,-24270,-24268,-24266,-24264,-24262,-24260,-24258,-24256,-24253,-24251,-24249,-24247,-24245,-24243,-24241,-24239,-24237,-24234,-24232,-24230,-24228,-24226,-24224,-24222,-24220,-24217,-24215,-24213,-24211,-24209,-24207,-24205,-24203,-24201,-24198,-24196,-24194,-24192,-24190,-24188,-24186,-24184,-24181,-24179,-24177,-24175,-24173,-24171,-24169,-24167,-24164,-24162,-24160,-24158,-24156,-24154,-24152,-24150,-24148,-24145,-24143,-24141,-24139,-24137,-24135,-24133,-24131,-24128,-24126,-24124,-24122,-24120,-24118,-24116,-24114,-24111,-24109,-24107,-24105,-24103,-24101,-24099,-24096,-24094,-24092,-24090,-24088,-24086,-24084,-24082,-24079,-24077,-24075,-24073,-24071,-24069,-24067,-24065,-24062,-24060,-24058,-24056,-24054,-24052,-24050,-24047,-24045,-24043,-24041,-24039,-24037,-24035,-24033,-24030,-24028,-24026,-24024,-24022,-24020,-24018,-24015,-24013,-24011,-24009,-24007,-24005,-24003,-24000,-23998,-23996,-23994,-23992,-23990,-23988,-23985,-23983,-23981,-23979,-23977,-23975,-23973,-23971,-23968,-23966,-23964,-23962,-23960,-23958,-23956,-23953,-23951,-23949,-23947,-23945,-23943,-23940,-23938,-23936,-23934,-23932,-23930,-23928,-23925,-23923,-23921,-23919,-23917,-23915,-23913,-23910,-23908,-23906,-23904,-23902,-23900,-23898,-23895,-23893,-23891,-23889,-23887,-23885,-23883,-23880,-23878,-23876,-23874,-23872,-23870,-23867,-23865,-23863,-23861,-23859,-23857,-23855,-23852,-23850,-23848,-23846,-23844,-23842,-23839,-23837,-23835,-23833,-23831,-23829,-23827,-23824,-23822,-23820,-23818,-23816,-23814,-23811,-23809,-23807,-23805,-23803,-23801,-23798,-23796,-23794,-23792,-23790,-23788,-23785,-23783,-23781,-23779,-23777,-23775,-23773,-23770,-23768,-23766,-23764,-23762,-23760,-23757,-23755,-23753,-23751,-23749,-23747,-23744,-23742,-23740,-23738,-23736,-23734,-23731,-23729,-23727,-23725,-23723,-23721,-23718,-23716,-23714,-23712,-23710,-23708,-23705,-23703,-23701,-23699,-23697,-23695,-23692,-23690,-23688,-23686,-23684,-23682,-23679,-23677,-23675,-23673,-23671,-23668,-23666,-23664,-23662,-23660,-23658,-23655,-23653,-23651,-23649,-23647,-23645,-23642,-23640,-23638,-23636,-23634,-23632,-23629,-23627,-23625,-23623,-23621,-23618,-23616,-23614,-23612,-23610,-23608,-23605,-23603,-23601,-23599,-23597,-23595,-23592,-23590,-23588,-23586,-23584,-23581,-23579,-23577,-23575,-23573,-23571,-23568,-23566,-23564,-23562,-23560,-23557,-23555,-23553,-23551,-23549,-23546,-23544,-23542,-23540,-23538,-23536,-23533,-23531,-23529,-23527,-23525,-23522,-23520,-23518,-23516,-23514,-23512,-23509,-23507,-23505,-23503,-23501,-23498,-23496,-23494,-23492,-23490,-23487,-23485,-23483,-23481,-23479,-23476,-23474,-23472,-23470,-23468,-23466,-23463,-23461,-23459,-23457,-23455,-23452,-23450,-23448,-23446,-23444,-23441,-23439,-23437,-23435,-23433,-23430,-23428,-23426,-23424,-23422,-23419,-23417,-23415,-23413,-23411,-23408,-23406,-23404,-23402,-23400,-23397,-23395,-23393,-23391,-23389,-23386,-23384,-23382,-23380,-23378,-23375,-23373,-23371,-23369,-23367,-23364,-23362,-23360,-23358,-23356,-23353,-23351,-23349,-23347,-23345,-23342,-23340,-23338,-23336,-23334,-23331,-23329,-23327,-23325,-23323,-23320,-23318,-23316,-23314,-23311,-23309,-23307,-23305,-23303,-23300,-23298,-23296,-23294,-23292,-23289,-23287,-23285,-23283,-23281,-23278,-23276,-23274,-23272,-23270,-23267,-23265,-23263,-23261,-23258,-23256,-23254,-23252,-23250,-23247,-23245,-23243,-23241,-23239,-23236,-23234,-23232,-23230,-23227,-23225,-23223,-23221,-23219,-23216,-23214,-23212,-23210,-23208,-23205,-23203,-23201,-23199,-23196,-23194,-23192,-23190,-23188,-23185,-23183,-23181,-23179,-23176,-23174,-23172,-23170,-23168,-23165,-23163,-23161,-23159,-23156,-23154,-23152,-23150,-23148,-23145,-23143,-23141,-23139,-23136,-23134,-23132,-23130,-23128,-23125,-23123,-23121,-23119,-23116,-23114,-23112,-23110,-23107,-23105,-23103,-23101,-23099,-23096,-23094,-23092,-23090,-23087,-23085,-23083,-23081,-23079,-23076,-23074,-23072,-23070,-23067,-23065,-23063,-23061,-23058,-23056,-23054,-23052,-23050,-23047,-23045,-23043,-23041,-23038,-23036,-23034,-23032,-23029,-23027,-23025,-23023,-23020,-23018,-23016,-23014,-23012,-23009,-23007,-23005,-23003,-23000,-22998,-22996,-22994,-22991,-22989,-22987,-22985,-22982,-22980,-22978,-22976,-22973,-22971,-22969,-22967,-22965,-22962,-22960,-22958,-22956,-22953,-22951,-22949,-22947,-22944,-22942,-22940,-22938,-22935,-22933,-22931,-22929,-22926,-22924,-22922,-22920,-22917,-22915,-22913,-22911,-22908,-22906,-22904,-22902,-22899,-22897,-22895,-22893,-22890,-22888,-22886,-22884,-22881,-22879,-22877,-22875,-22872,-22870,-22868,-22866,-22863,-22861,-22859,-22857,-22854,-22852,-22850,-22848,-22845,-22843,-22841,-22839,-22836,-22834,-22832,-22830,-22827,-22825,-22823,-22821,-22818,-22816,-22814,-22812,-22809,-22807,-22805,-22803,-22800,-22798,-22796,-22794,-22791,-22789,-22787,-22785,-22782,-22780,-22778,-22776,-22773,-22771,-22769,-22766,-22764,-22762,-22760,-22757,-22755,-22753,-22751,-22748,-22746,-22744,-22742,-22739,-22737,-22735,-22733,-22730,-22728,-22726,-22724,-22721,-22719,-22717,-22714,-22712,-22710,-22708,-22705,-22703,-22701,-22699,-22696,-22694,-22692,-22690,-22687,-22685,-22683,-22680,-22678,-22676,-22674,-22671,-22669,-22667,-22665,-22662,-22660,-22658,-22656,-22653,-22651,-22649,-22646,-22644,-22642,-22640,-22637,-22635,-22633,-22631,-22628,-22626,-22624,-22621,-22619,-22617,-22615,-22612,-22610,-22608,-22606,-22603,-22601,-22599,-22596,-22594,-22592,-22590,-22587,-22585,-22583,-22581,-22578,-22576,-22574,-22571,-22569,-22567,-22565,-22562,-22560,-22558,-22555,-22553,-22551,-22549,-22546,-22544,-22542,-22540,-22537,-22535,-22533,-22530,-22528,-22526,-22524,-22521,-22519,-22517,-22514,-22512,-22510,-22508,-22505,-22503,-22501,-22498,-22496,-22494,-22492,-22489,-22487,-22485,-22482,-22480,-22478,-22476,-22473,-22471,-22469,-22466,-22464,-22462,-22460,-22457,-22455,-22453,-22450,-22448,-22446,-22444,-22441,-22439,-22437,-22434,-22432,-22430,-22428,-22425,-22423,-22421,-22418,-22416,-22414,-22411,-22409,-22407,-22405,-22402,-22400,-22398,-22395,-22393,-22391,-22389,-22386,-22384,-22382,-22379,-22377,-22375,-22373,-22370,-22368,-22366,-22363,-22361,-22359,-22356,-22354,-22352,-22350,-22347,-22345,-22343,-22340,-22338,-22336,-22333,-22331,-22329,-22327,-22324,-22322,-22320,-22317,-22315,-22313,-22310,-22308,-22306,-22304,-22301,-22299,-22297,-22294,-22292,-22290,-22287,-22285,-22283,-22281,-22278,-22276,-22274,-22271,-22269,-22267,-22264,-22262,-22260,-22257,-22255,-22253,-22251,-22248,-22246,-22244,-22241,-22239,-22237,-22234,-22232,-22230,-22227,-22225,-22223,-22221,-22218,-22216,-22214,-22211,-22209,-22207,-22204,-22202,-22200,-22197,-22195,-22193,-22191,-22188,-22186,-22184,-22181,-22179,-22177,-22174,-22172,-22170,-22167,-22165,-22163,-22160,-22158,-22156,-22154,-22151,-22149,-22147,-22144,-22142,-22140,-22137,-22135,-22133,-22130,-22128,-22126,-22123,-22121,-22119,-22116,-22114,-22112,-22110,-22107,-22105,-22103,-22100,-22098,-22096,-22093,-22091,-22089,-22086,-22084,-22082,-22079,-22077,-22075,-22072,-22070,-22068,-22065,-22063,-22061,-22058,-22056,-22054,-22051,-22049,-22047,-22045,-22042,-22040,-22038,-22035,-22033,-22031,-22028,-22026,-22024,-22021,-22019,-22017,-22014,-22012,-22010,-22007,-22005,-22003,-22000,-21998,-21996,-21993,-21991,-21989,-21986,-21984,-21982,-21979,-21977,-21975,-21972,-21970,-21968,-21965,-21963,-21961,-21958,-21956,-21954,-21951,-21949,-21947,-21944,-21942,-21940,-21937,-21935,-21933,-21930,-21928,-21926,-21923,-21921,-21919,-21916,-21914,-21912,-21909,-21907,-21905,-21902,-21900,-21898,-21895,-21893,-21891,-21888,-21886,-21884,-21881,-21879,-21877,-21874,-21872,-21870,-21867,-21865,-21863,-21860,-21858,-21856,-21853,-21851,-21849,-21846,-21844,-21842,-21839,-21837,-21835,-21832,-21830,-21827,-21825,-21823,-21820,-21818,-21816,-21813,-21811,-21809,-21806,-21804,-21802,-21799,-21797,-21795,-21792,-21790,-21788,-21785,-21783,-21781,-21778,-21776,-21774,-21771,-21769,-21766,-21764,-21762,-21759,-21757,-21755,-21752,-21750,-21748,-21745,-21743,-21741,-21738,-21736,-21734,-21731,-21729,-21727,-21724,-21722,-21719,-21717,-21715,-21712,-21710,-21708,-21705,-21703,-21701,-21698,-21696,-21694,-21691,-21689,-21687,-21684,-21682,-21679,-21677,-21675,-21672,-21670,-21668,-21665,-21663,-21661,-21658,-21656,-21654,-21651,-21649,-21646,-21644,-21642,-21639,-21637,-21635,-21632,-21630,-21628,-21625,-21623,-21621,-21618,-21616,-21613,-21611,-21609,-21606,-21604,-21602,-21599,-21597,-21595,-21592,-21590,-21587,-21585,-21583,-21580,-21578,-21576,-21573,-21571,-21569,-21566,-21564,-21561,-21559,-21557,-21554,-21552,-21550,-21547,-21545,-21543,-21540,-21538,-21535,-21533,-21531,-21528,-21526,-21524,-21521,-21519,-21516,-21514,-21512,-21509,-21507,-21505,-21502,-21500,-21498,-21495,-21493,-21490,-21488,-21486,-21483,-21481,-21479,-21476,-21474,-21471,-21469,-21467,-21464,-21462,-21460,-21457,-21455,-21452,-21450,-21448,-21445,-21443,-21441,-21438,-21436,-21433,-21431,-21429,-21426,-21424,-21422,-21419,-21417,-21414,-21412,-21410,-21407,-21405,-21403,-21400,-21398,-21395,-21393,-21391,-21388,-21386,-21383,-21381,-21379,-21376,-21374,-21372,-21369,-21367,-21364,-21362,-21360,-21357,-21355,-21353,-21350,-21348,-21345,-21343,-21341,-21338,-21336,-21333,-21331,-21329,-21326,-21324,-21322,-21319,-21317,-21314,-21312,-21310,-21307,-21305,-21302,-21300,-21298,-21295,-21293,-21290,-21288,-21286,-21283,-21281,-21279,-21276,-21274,-21271,-21269,-21267,-21264,-21262,-21259,-21257,-21255,-21252,-21250,-21247,-21245,-21243,-21240,-21238,-21236,-21233,-21231,-21228,-21226,-21224,-21221,-21219,-21216,-21214,-21212,-21209,-21207,-21204,-21202,-21200,-21197,-21195,-21192,-21190,-21188,-21185,-21183,-21180,-21178,-21176,-21173,-21171,-21168,-21166,-21164,-21161,-21159,-21156,-21154,-21152,-21149,-21147,-21144,-21142,-21140,-21137,-21135,-21132,-21130,-21128,-21125,-21123,-21120,-21118,-21116,-21113,-21111,-21108,-21106,-21104,-21101,-21099,-21096,-21094,-21092,-21089,-21087,-21084,-21082,-21080,-21077,-21075,-21072,-21070,-21068,-21065,-21063,-21060,-21058,-21056,-21053,-21051,-21048,-21046,-21043,-21041,-21039,-21036,-21034,-21031,-21029,-21027,-21024,-21022,-21019,-21017,-21015,-21012,-21010,-21007,-21005,-21003,-21000,-20998,-20995,-20993,-20990,-20988,-20986,-20983,-20981,-20978,-20976,-20974,-20971,-20969,-20966,-20964,-20962,-20959,-20957,-20954,-20952,-20949,-20947,-20945,-20942,-20940,-20937,-20935,-20933,-20930,-20928,-20925,-20923,-20920,-20918,-20916,-20913,-20911,-20908,-20906,-20904,-20901,-20899,-20896,-20894,-20891,-20889,-20887,-20884,-20882,-20879,-20877,-20874,-20872,-20870,-20867,-20865,-20862,-20860,-20858,-20855,-20853,-20850,-20848,-20845,-20843,-20841,-20838,-20836,-20833,-20831,-20828,-20826,-20824,-20821,-20819,-20816,-20814,-20811,-20809,-20807,-20804,-20802,-20799,-20797,-20794,-20792,-20790,-20787,-20785,-20782,-20780,-20777,-20775,-20773,-20770,-20768,-20765,-20763,-20760,-20758,-20756,-20753,-20751,-20748,-20746,-20743,-20741,-20739,-20736,-20734,-20731,-20729,-20726,-20724,-20722,-20719,-20717,-20714,-20712,-20709,-20707,-20704,-20702,-20700,-20697,-20695,-20692,-20690,-20687,-20685,-20683,-20680,-20678,-20675,-20673,-20670,-20668,-20666,-20663,-20661,-20658,-20656,-20653,-20651,-20648,-20646,-20644,-20641,-20639,-20636,-20634,-20631,-20629,-20626,-20624,-20622,-20619,-20617,-20614,-20612,-20609,-20607,-20604,-20602,-20600,-20597,-20595,-20592,-20590,-20587,-20585,-20583,-20580,-20578,-20575,-20573,-20570,-20568,-20565,-20563,-20560,-20558,-20556,-20553,-20551,-20548,-20546,-20543,-20541,-20538,-20536,-20534,-20531,-20529,-20526,-20524,-20521,-20519,-20516,-20514,-20512,-20509,-20507,-20504,-20502,-20499,-20497,-20494,-20492,-20489,-20487,-20485,-20482,-20480,-20477,-20475,-20472,-20470,-20467,-20465,-20463,-20460,-20458,-20455,-20453,-20450,-20448,-20445,-20443,-20440,-20438,-20436,-20433,-20431,-20428,-20426,-20423,-20421,-20418,-20416,-20413,-20411,-20408,-20406,-20404,-20401,-20399,-20396,-20394,-20391,-20389,-20386,-20384,-20381,-20379,-20377,-20374,-20372,-20369,-20367,-20364,-20362,-20359,-20357,-20354,-20352,-20349,-20347,-20345,-20342,-20340,-20337,-20335,-20332,-20330,-20327,-20325,-20322,-20320,-20317,-20315,-20312,-20310,-20308,-20305,-20303,-20300,-20298,-20295,-20293,-20290,-20288,-20285,-20283,-20280,-20278,-20275,-20273,-20271,-20268,-20266,-20263,-20261,-20258,-20256,-20253,-20251,-20248,-20246,-20243,-20241,-20238,-20236,-20234,-20231,-20229,-20226,-20224,-20221,-20219,-20216,-20214,-20211,-20209,-20206,-20204,-20201,-20199,-20196,-20194,-20191,-20189,-20187,-20184,-20182,-20179,-20177,-20174,-20172,-20169,-20167,-20164,-20162,-20159,-20157,-20154,-20152,-20149,-20147,-20144,-20142,-20139,-20137,-20135,-20132,-20130,-20127,-20125,-20122,-20120,-20117,-20115,-20112,-20110,-20107,-20105,-20102,-20100,-20097,-20095,-20092,-20090,-20087,-20085,-20082,-20080,-20077,-20075,-20072,-20070,-20068,-20065,-20063,-20060,-20058,-20055,-20053,-20050,-20048,-20045,-20043,-20040,-20038,-20035,-20033,-20030,-20028,-20025,-20023,-20020,-20018,-20015,-20013,-20010,-20008,-20005,-20003,-20000,-19998,-19995,-19993,-19990,-19988,-19985,-19983,-19981,-19978,-19976,-19973,-19971,-19968,-19966,-19963,-19961,-19958,-19956,-19953,-19951,-19948,-19946,-19943,-19941,-19938,-19936,-19933,-19931,-19928,-19926,-19923,-19921,-19918,-19916,-19913,-19911,-19908,-19906,-19903,-19901,-19898,-19896,-19893,-19891,-19888,-19886,-19883,-19881,-19878,-19876,-19873,-19871,-19868,-19866,-19863,-19861,-19858,-19856,-19853,-19851,-19848,-19846,-19843,-19841,-19838,-19836,-19833,-19831,-19828,-19826,-19823,-19821,-19818,-19816,-19813,-19811,-19808,-19806,-19803,-19801,-19798,-19796,-19793,-19791,-19788,-19786,-19783,-19781,-19778,-19776,-19773,-19771,-19768,-19766,-19763,-19761,-19758,-19756,-19753,-19751,-19748,-19746,-19743,-19741,-19738,-19736,-19733,-19731,-19728,-19726,-19723,-19721,-19718,-19716,-19713,-19711,-19708,-19706,-19703,-19700,-19698,-19695,-19693,-19690,-19688,-19685,-19683,-19680,-19678,-19675,-19673,-19670,-19668,-19665,-19663,-19660,-19658,-19655,-19653,-19650,-19648,-19645,-19643,-19640,-19638,-19635,-19633,-19630,-19628,-19625,-19623,-19620,-19618,-19615,-19613,-19610,-19607,-19605,-19602,-19600,-19597,-19595,-19592,-19590,-19587,-19585,-19582,-19580,-19577,-19575,-19572,-19570,-19567,-19565,-19562,-19560,-19557,-19555,-19552,-19550,-19547,-19545,-19542,-19539,-19537,-19534,-19532,-19529,-19527,-19524,-19522,-19519,-19517,-19514,-19512,-19509,-19507,-19504,-19502,-19499,-19497,-19494,-19492,-19489,-19486,-19484,-19481,-19479,-19476,-19474,-19471,-19469,-19466,-19464,-19461,-19459,-19456,-19454,-19451,-19449,-19446,-19444,-19441,-19438,-19436,-19433,-19431,-19428,-19426,-19423,-19421,-19418,-19416,-19413,-19411,-19408,-19406,-19403,-19400,-19398,-19395,-19393,-19390,-19388,-19385,-19383,-19380,-19378,-19375,-19373,-19370,-19368,-19365,-19362,-19360,-19357,-19355,-19352,-19350,-19347,-19345,-19342,-19340,-19337,-19335,-19332,-19330,-19327,-19324,-19322,-19319,-19317,-19314,-19312,-19309,-19307,-19304,-19302,-19299,-19297,-19294,-19291,-19289,-19286,-19284,-19281,-19279,-19276,-19274,-19271,-19269,-19266,-19264,-19261,-19258,-19256,-19253,-19251,-19248,-19246,-19243,-19241,-19238,-19236,-19233,-19230,-19228,-19225,-19223,-19220,-19218,-19215,-19213,-19210,-19208,-19205,-19202,-19200,-19197,-19195,-19192,-19190,-19187,-19185,-19182,-19180,-19177,-19174,-19172,-19169,-19167,-19164,-19162,-19159,-19157,-19154,-19152,-19149,-19146,-19144,-19141,-19139,-19136,-19134,-19131,-19129,-19126,-19123,-19121,-19118,-19116,-19113,-19111,-19108,-19106,-19103,-19101,-19098,-19095,-19093,-19090,-19088,-19085,-19083,-19080,-19078,-19075,-19072,-19070,-19067,-19065,-19062,-19060,-19057,-19055,-19052,-19049,-19047,-19044,-19042,-19039,-19037,-19034,-19032,-19029,-19026,-19024,-19021,-19019,-19016,-19014,-19011,-19009,-19006,-19003,-19001,-18998,-18996,-18993,-18991,-18988,-18985,-18983,-18980,-18978,-18975,-18973,-18970,-18968,-18965,-18962,-18960,-18957,-18955,-18952,-18950,-18947,-18944,-18942,-18939,-18937,-18934,-18932,-18929,-18927,-18924,-18921,-18919,-18916,-18914,-18911,-18909,-18906,-18903,-18901,-18898,-18896,-18893,-18891,-18888,-18885,-18883,-18880,-18878,-18875,-18873,-18870,-18868,-18865,-18862,-18860,-18857,-18855,-18852,-18850,-18847,-18844,-18842,-18839,-18837,-18834,-18832,-18829,-18826,-18824,-18821,-18819,-18816,-18814,-18811,-18808,-18806,-18803,-18801,-18798,-18796,-18793,-18790,-18788,-18785,-18783,-18780,-18778,-18775,-18772,-18770,-18767,-18765,-18762,-18759,-18757,-18754,-18752,-18749,-18747,-18744,-18741,-18739,-18736,-18734,-18731,-18729,-18726,-18723,-18721,-18718,-18716,-18713,-18711,-18708,-18705,-18703,-18700,-18698,-18695,-18692,-18690,-18687,-18685,-18682,-18680,-18677,-18674,-18672,-18669,-18667,-18664,-18661,-18659,-18656,-18654,-18651,-18649,-18646,-18643,-18641,-18638,-18636,-18633,-18630,-18628,-18625,-18623,-18620,-18618,-18615,-18612,-18610,-18607,-18605,-18602,-18599,-18597,-18594,-18592,-18589,-18587,-18584,-18581,-18579,-18576,-18574,-18571,-18568,-18566,-18563,-18561,-18558,-18555,-18553,-18550,-18548,-18545,-18543,-18540,-18537,-18535,-18532,-18530,-18527,-18524,-18522,-18519,-18517,-18514,-18511,-18509,-18506,-18504,-18501,-18498,-18496,-18493,-18491,-18488,-18485,-18483,-18480,-18478,-18475,-18473,-18470,-18467,-18465,-18462,-18460,-18457,-18454,-18452,-18449,-18447,-18444,-18441,-18439,-18436,-18434,-18431,-18428,-18426,-18423,-18421,-18418,-18415,-18413,-18410,-18408,-18405,-18402,-18400,-18397,-18395,-18392,-18389,-18387,-18384,-18382,-18379,-18376,-18374,-18371,-18369,-18366,-18363,-18361,-18358,-18356,-18353,-18350,-18348,-18345,-18343,-18340,-18337,-18335,-18332,-18330,-18327,-18324,-18322,-18319,-18317,-18314,-18311,-18309,-18306,-18304,-18301,-18298,-18296,-18293,-18290,-18288,-18285,-18283,-18280,-18277,-18275,-18272,-18270,-18267,-18264,-18262,-18259,-18257,-18254,-18251,-18249,-18246,-18244,-18241,-18238,-18236,-18233,-18230,-18228,-18225,-18223,-18220,-18217,-18215,-18212,-18210,-18207,-18204,-18202,-18199,-18197,-18194,-18191,-18189,-18186,-18183,-18181,-18178,-18176,-18173,-18170,-18168,-18165,-18163,-18160,-18157,-18155,-18152,-18149,-18147,-18144,-18142,-18139,-18136,-18134,-18131,-18129,-18126,-18123,-18121,-18118,-18115,-18113,-18110,-18108,-18105,-18102,-18100,-18097,-18095,-18092,-18089,-18087,-18084,-18081,-18079,-18076,-18074,-18071,-18068,-18066,-18063,-18060,-18058,-18055,-18053,-18050,-18047,-18045,-18042,-18039,-18037,-18034,-18032,-18029,-18026,-18024,-18021,-18018,-18016,-18013,-18011,-18008,-18005,-18003,-18000,-17997,-17995,-17992,-17990,-17987,-17984,-17982,-17979,-17976,-17974,-17971,-17969,-17966,-17963,-17961,-17958,-17955,-17953,-17950,-17948,-17945,-17942,-17940,-17937,-17934,-17932,-17929,-17927,-17924,-17921,-17919,-17916,-17913,-17911,-17908,-17906,-17903,-17900,-17898,-17895,-17892,-17890,-17887,-17884,-17882,-17879,-17877,-17874,-17871,-17869,-17866,-17863,-17861,-17858,-17855,-17853,-17850,-17848,-17845,-17842,-17840,-17837,-17834,-17832,-17829,-17827,-17824,-17821,-17819,-17816,-17813,-17811,-17808,-17805,-17803,-17800,-17798,-17795,-17792,-17790,-17787,-17784,-17782,-17779,-17776,-17774,-17771,-17768,-17766,-17763,-17761,-17758,-17755,-17753,-17750,-17747,-17745,-17742,-17739,-17737,-17734,-17732,-17729,-17726,-17724,-17721,-17718,-17716,-17713,-17710,-17708,-17705,-17702,-17700,-17697,-17695,-17692,-17689,-17687,-17684,-17681,-17679,-17676,-17673,-17671,-17668,-17665,-17663,-17660,-17657,-17655,-17652,-17650,-17647,-17644,-17642,-17639,-17636,-17634,-17631,-17628,-17626,-17623,-17620,-17618,-17615,-17612,-17610,-17607,-17605,-17602,-17599,-17597,-17594,-17591,-17589,-17586,-17583,-17581,-17578,-17575,-17573,-17570,-17567,-17565,-17562,-17559,-17557,-17554,-17551,-17549,-17546,-17544,-17541,-17538,-17536,-17533,-17530,-17528,-17525,-17522,-17520,-17517,-17514,-17512,-17509,-17506,-17504,-17501,-17498,-17496,-17493,-17490,-17488,-17485,-17482,-17480,-17477,-17474,-17472,-17469,-17467,-17464,-17461,-17459,-17456,-17453,-17451,-17448,-17445,-17443,-17440,-17437,-17435,-17432,-17429,-17427,-17424,-17421,-17419,-17416,-17413,-17411,-17408,-17405,-17403,-17400,-17397,-17395,-17392,-17389,-17387,-17384,-17381,-17379,-17376,-17373,-17371,-17368,-17365,-17363,-17360,-17357,-17355,-17352,-17349,-17347,-17344,-17341,-17339,-17336,-17333,-17331,-17328,-17325,-17323,-17320,-17317,-17315,-17312,-17309,-17307,-17304,-17301,-17299,-17296,-17293,-17291,-17288,-17285,-17283,-17280,-17277,-17275,-17272,-17269,-17267,-17264,-17261,-17259,-17256,-17253,-17251,-17248,-17245,-17243,-17240,-17237,-17235,-17232,-17229,-17227,-17224,-17221,-17219,-17216,-17213,-17211,-17208,-17205,-17203,-17200,-17197,-17195,-17192,-17189,-17187,-17184,-17181,-17179,-17176,-17173,-17171,-17168,-17165,-17162,-17160,-17157,-17154,-17152,-17149,-17146,-17144,-17141,-17138,-17136,-17133,-17130,-17128,-17125,-17122,-17120,-17117,-17114,-17112,-17109,-17106,-17104,-17101,-17098,-17096,-17093,-17090,-17087,-17085,-17082,-17079,-17077,-17074,-17071,-17069,-17066,-17063,-17061,-17058,-17055,-17053,-17050,-17047,-17045,-17042,-17039,-17037,-17034,-17031,-17028,-17026,-17023,-17020,-17018,-17015,-17012,-17010,-17007,-17004,-17002,-16999,-16996,-16994,-16991,-16988,-16986,-16983,-16980,-16977,-16975,-16972,-16969,-16967,-16964,-16961,-16959,-16956,-16953,-16951,-16948,-16945,-16943,-16940,-16937,-16934,-16932,-16929,-16926,-16924,-16921,-16918,-16916,-16913,-16910,-16908,-16905,-16902,-16899,-16897,-16894,-16891,-16889,-16886,-16883,-16881,-16878,-16875,-16873,-16870,-16867,-16864,-16862,-16859,-16856,-16854,-16851,-16848,-16846,-16843,-16840,-16838,-16835,-16832,-16829,-16827,-16824,-16821,-16819,-16816,-16813,-16811,-16808,-16805,-16802,-16800,-16797,-16794,-16792,-16789,-16786,-16784,-16781,-16778,-16775,-16773,-16770,-16767,-16765,-16762,-16759,-16757,-16754,-16751,-16749,-16746,-16743,-16740,-16738,-16735,-16732,-16730,-16727,-16724,-16721,-16719,-16716,-16713,-16711,-16708,-16705,-16703,-16700,-16697,-16694,-16692,-16689,-16686,-16684,-16681,-16678,-16676,-16673,-16670,-16667,-16665,-16662,-16659,-16657,-16654,-16651,-16648,-16646,-16643,-16640,-16638,-16635,-16632,-16630,-16627,-16624,-16621,-16619,-16616,-16613,-16611,-16608,-16605,-16602,-16600,-16597,-16594,-16592,-16589,-16586,-16584,-16581,-16578,-16575,-16573,-16570,-16567,-16565,-16562,-16559,-16556,-16554,-16551,-16548,-16546,-16543,-16540,-16537,-16535,-16532,-16529,-16527,-16524,-16521,-16518,-16516,-16513,-16510,-16508,-16505,-16502,-16499,-16497,-16494,-16491,-16489,-16486,-16483,-16480,-16478,-16475,-16472,-16470,-16467,-16464,-16461,-16459,-16456,-16453,-16451,-16448,-16445,-16442,-16440,-16437,-16434,-16432,-16429,-16426,-16423,-16421,-16418,-16415,-16413,-16410,-16407,-16404,-16402,-16399,-16396,-16393,-16391,-16388,-16385,-16383,-16380,-16377,-16374,-16372,-16369,-16366,-16364,-16361,-16358,-16355,-16353,-16350,-16347,-16344,-16342,-16339,-16336,-16334,-16331,-16328,-16325,-16323,-16320,-16317,-16315,-16312,-16309,-16306,-16304,-16301,-16298,-16295,-16293,-16290,-16287,-16285,-16282,-16279,-16276,-16274,-16271,-16268,-16265,-16263,-16260,-16257,-16255,-16252,-16249,-16246,-16244,-16241,-16238,-16235,-16233,-16230,-16227,-16225,-16222,-16219,-16216,-16214,-16211,-16208,-16205,-16203,-16200,-16197,-16195,-16192,-16189,-16186,-16184,-16181,-16178,-16175,-16173,-16170,-16167,-16164,-16162,-16159,-16156,-16154,-16151,-16148,-16145,-16143,-16140,-16137,-16134,-16132,-16129,-16126,-16123,-16121,-16118,-16115,-16113,-16110,-16107,-16104,-16102,-16099,-16096,-16093,-16091,-16088,-16085,-16082,-16080,-16077,-16074,-16071,-16069,-16066,-16063,-16061,-16058,-16055,-16052,-16050,-16047,-16044,-16041,-16039,-16036,-16033,-16030,-16028,-16025,-16022,-16019,-16017,-16014,-16011,-16008,-16006,-16003,-16000,-15997,-15995,-15992,-15989,-15987,-15984,-15981,-15978,-15976,-15973,-15970,-15967,-15965,-15962,-15959,-15956,-15954,-15951,-15948,-15945,-15943,-15940,-15937,-15934,-15932,-15929,-15926,-15923,-15921,-15918,-15915,-15912,-15910,-15907,-15904,-15901,-15899,-15896,-15893,-15890,-15888,-15885,-15882,-15879,-15877,-15874,-15871,-15868,-15866,-15863,-15860,-15857,-15855,-15852,-15849,-15846,-15844,-15841,-15838,-15835,-15833,-15830,-15827,-15824,-15822,-15819,-15816,-15813,-15811,-15808,-15805,-15802,-15800,-15797,-15794,-15791,-15789,-15786,-15783,-15780,-15778,-15775,-15772,-15769,-15767,-15764,-15761,-15758,-15756,-15753,-15750,-15747,-15745,-15742,-15739,-15736,-15734,-15731,-15728,-15725,-15723,-15720,-15717,-15714,-15712,-15709,-15706,-15703,-15701,-15698,-15695,-15692,-15690,-15687,-15684,-15681,-15678,-15676,-15673,-15670,-15667,-15665,-15662,-15659,-15656,-15654,-15651,-15648,-15645,-15643,-15640,-15637,-15634,-15632,-15629,-15626,-15623,-15621,-15618,-15615,-15612,-15609,-15607,-15604,-15601,-15598,-15596,-15593,-15590,-15587,-15585,-15582,-15579,-15576,-15574,-15571,-15568,-15565,-15562,-15560,-15557,-15554,-15551,-15549,-15546,-15543,-15540,-15538,-15535,-15532,-15529,-15527,-15524,-15521,-15518,-15515,-15513,-15510,-15507,-15504,-15502,-15499,-15496,-15493,-15491,-15488,-15485,-15482,-15479,-15477,-15474,-15471,-15468,-15466,-15463,-15460,-15457,-15455,-15452,-15449,-15446,-15443,-15441,-15438,-15435,-15432,-15430,-15427,-15424,-15421,-15419,-15416,-15413,-15410,-15407,-15405,-15402,-15399,-15396,-15394,-15391,-15388,-15385,-15382,-15380,-15377,-15374,-15371,-15369,-15366,-15363,-15360,-15358,-15355,-15352,-15349,-15346,-15344,-15341,-15338,-15335,-15333,-15330,-15327,-15324,-15321,-15319,-15316,-15313,-15310,-15308,-15305,-15302,-15299,-15296,-15294,-15291,-15288,-15285,-15283,-15280,-15277,-15274,-15271,-15269,-15266,-15263,-15260,-15258,-15255,-15252,-15249,-15246,-15244,-15241,-15238,-15235,-15233,-15230,-15227,-15224,-15221,-15219,-15216,-15213,-15210,-15207,-15205,-15202,-15199,-15196,-15194,-15191,-15188,-15185,-15182,-15180,-15177,-15174,-15171,-15168,-15166,-15163,-15160,-15157,-15155,-15152,-15149,-15146,-15143,-15141,-15138,-15135,-15132,-15129,-15127,-15124,-15121,-15118,-15116,-15113,-15110,-15107,-15104,-15102,-15099,-15096,-15093,-15090,-15088,-15085,-15082,-15079,-15077,-15074,-15071,-15068,-15065,-15063,-15060,-15057,-15054,-15051,-15049,-15046,-15043,-15040,-15037,-15035,-15032,-15029,-15026,-15024,-15021,-15018,-15015,-15012,-15010,-15007,-15004,-15001,-14998,-14996,-14993,-14990,-14987,-14984,-14982,-14979,-14976,-14973,-14970,-14968,-14965,-14962,-14959,-14956,-14954,-14951,-14948,-14945,-14942,-14940,-14937,-14934,-14931,-14929,-14926,-14923,-14920,-14917,-14915,-14912,-14909,-14906,-14903,-14901,-14898,-14895,-14892,-14889,-14887,-14884,-14881,-14878,-14875,-14873,-14870,-14867,-14864,-14861,-14859,-14856,-14853,-14850,-14847,-14845,-14842,-14839,-14836,-14833,-14831,-14828,-14825,-14822,-14819,-14817,-14814,-14811,-14808,-14805,-14803,-14800,-14797,-14794,-14791,-14789,-14786,-14783,-14780,-14777,-14774,-14772,-14769,-14766,-14763,-14760,-14758,-14755,-14752,-14749,-14746,-14744,-14741,-14738,-14735,-14732,-14730,-14727,-14724,-14721,-14718,-14716,-14713,-14710,-14707,-14704,-14702,-14699,-14696,-14693,-14690,-14688,-14685,-14682,-14679,-14676,-14673,-14671,-14668,-14665,-14662,-14659,-14657,-14654,-14651,-14648,-14645,-14643,-14640,-14637,-14634,-14631,-14628,-14626,-14623,-14620,-14617,-14614,-14612,-14609,-14606,-14603,-14600,-14598,-14595,-14592,-14589,-14586,-14584,-14581,-14578,-14575,-14572,-14569,-14567,-14564,-14561,-14558,-14555,-14553,-14550,-14547,-14544,-14541,-14538,-14536,-14533,-14530,-14527,-14524,-14522,-14519,-14516,-14513,-14510,-14507,-14505,-14502,-14499,-14496,-14493,-14491,-14488,-14485,-14482,-14479,-14477,-14474,-14471,-14468,-14465,-14462,-14460,-14457,-14454,-14451,-14448,-14445,-14443,-14440,-14437,-14434,-14431,-14429,-14426,-14423,-14420,-14417,-14414,-14412,-14409,-14406,-14403,-14400,-14398,-14395,-14392,-14389,-14386,-14383,-14381,-14378,-14375,-14372,-14369,-14366,-14364,-14361,-14358,-14355,-14352,-14350,-14347,-14344,-14341,-14338,-14335,-14333,-14330,-14327,-14324,-14321,-14318,-14316,-14313,-14310,-14307,-14304,-14302,-14299,-14296,-14293,-14290,-14287,-14285,-14282,-14279,-14276,-14273,-14270,-14268,-14265,-14262,-14259,-14256,-14253,-14251,-14248,-14245,-14242,-14239,-14236,-14234,-14231,-14228,-14225,-14222,-14219,-14217,-14214,-14211,-14208,-14205,-14203,-14200,-14197,-14194,-14191,-14188,-14186,-14183,-14180,-14177,-14174,-14171,-14169,-14166,-14163,-14160,-14157,-14154,-14152,-14149,-14146,-14143,-14140,-14137,-14135,-14132,-14129,-14126,-14123,-14120,-14118,-14115,-14112,-14109,-14106,-14103,-14101,-14098,-14095,-14092,-14089,-14086,-14083,-14081,-14078,-14075,-14072,-14069,-14066,-14064,-14061,-14058,-14055,-14052,-14049,-14047,-14044,-14041,-14038,-14035,-14032,-14030,-14027,-14024,-14021,-14018,-14015,-14013,-14010,-14007,-14004,-14001,-13998,-13995,-13993,-13990,-13987,-13984,-13981,-13978,-13976,-13973,-13970,-13967,-13964,-13961,-13959,-13956,-13953,-13950,-13947,-13944,-13942,-13939,-13936,-13933,-13930,-13927,-13924,-13922,-13919,-13916,-13913,-13910,-13907,-13905,-13902,-13899,-13896,-13893,-13890,-13887,-13885,-13882,-13879,-13876,-13873,-13870,-13868,-13865,-13862,-13859,-13856,-13853,-13850,-13848,-13845,-13842,-13839,-13836,-13833,-13831,-13828,-13825,-13822,-13819,-13816,-13813,-13811,-13808,-13805,-13802,-13799,-13796,-13793,-13791,-13788,-13785,-13782,-13779,-13776,-13774,-13771,-13768,-13765,-13762,-13759,-13756,-13754,-13751,-13748,-13745,-13742,-13739,-13736,-13734,-13731,-13728,-13725,-13722,-13719,-13717,-13714,-13711,-13708,-13705,-13702,-13699,-13697,-13694,-13691,-13688,-13685,-13682,-13679,-13677,-13674,-13671,-13668,-13665,-13662,-13659,-13657,-13654,-13651,-13648,-13645,-13642,-13639,-13637,-13634,-13631,-13628,-13625,-13622,-13619,-13617,-13614,-13611,-13608,-13605,-13602,-13599,-13597,-13594,-13591,-13588,-13585,-13582,-13579,-13577,-13574,-13571,-13568,-13565,-13562,-13559,-13557,-13554,-13551,-13548,-13545,-13542,-13539,-13537,-13534,-13531,-13528,-13525,-13522,-13519,-13516,-13514,-13511,-13508,-13505,-13502,-13499,-13496,-13494,-13491,-13488,-13485,-13482,-13479,-13476,-13474,-13471,-13468,-13465,-13462,-13459,-13456,-13454,-13451,-13448,-13445,-13442,-13439,-13436,-13433,-13431,-13428,-13425,-13422,-13419,-13416,-13413,-13411,-13408,-13405,-13402,-13399,-13396,-13393,-13390,-13388,-13385,-13382,-13379,-13376,-13373,-13370,-13368,-13365,-13362,-13359,-13356,-13353,-13350,-13347,-13345,-13342,-13339,-13336,-13333,-13330,-13327,-13324,-13322,-13319,-13316,-13313,-13310,-13307,-13304,-13302,-13299,-13296,-13293,-13290,-13287,-13284,-13281,-13279,-13276,-13273,-13270,-13267,-13264,-13261,-13258,-13256,-13253,-13250,-13247,-13244,-13241,-13238,-13235,-13233,-13230,-13227,-13224,-13221,-13218,-13215,-13212,-13210,-13207,-13204,-13201,-13198,-13195,-13192,-13189,-13187,-13184,-13181,-13178,-13175,-13172,-13169,-13166,-13164,-13161,-13158,-13155,-13152,-13149,-13146,-13143,-13141,-13138,-13135,-13132,-13129,-13126,-13123,-13120,-13118,-13115,-13112,-13109,-13106,-13103,-13100,-13097,-13094,-13092,-13089,-13086,-13083,-13080,-13077,-13074,-13071,-13069,-13066,-13063,-13060,-13057,-13054,-13051,-13048,-13046,-13043,-13040,-13037,-13034,-13031,-13028,-13025,-13022,-13020,-13017,-13014,-13011,-13008,-13005,-13002,-12999,-12997,-12994,-12991,-12988,-12985,-12982,-12979,-12976,-12973,-12971,-12968,-12965,-12962,-12959,-12956,-12953,-12950,-12947,-12945,-12942,-12939,-12936,-12933,-12930,-12927,-12924,-12921,-12919,-12916,-12913,-12910,-12907,-12904,-12901,-12898,-12895,-12893,-12890,-12887,-12884,-12881,-12878,-12875,-12872,-12870,-12867,-12864,-12861,-12858,-12855,-12852,-12849,-12846,-12843,-12841,-12838,-12835,-12832,-12829,-12826,-12823,-12820,-12817,-12815,-12812,-12809,-12806,-12803,-12800,-12797,-12794,-12791,-12789,-12786,-12783,-12780,-12777,-12774,-12771,-12768,-12765,-12763,-12760,-12757,-12754,-12751,-12748,-12745,-12742,-12739,-12736,-12734,-12731,-12728,-12725,-12722,-12719,-12716,-12713,-12710,-12708,-12705,-12702,-12699,-12696,-12693,-12690,-12687,-12684,-12681,-12679,-12676,-12673,-12670,-12667,-12664,-12661,-12658,-12655,-12652,-12650,-12647,-12644,-12641,-12638,-12635,-12632,-12629,-12626,-12624,-12621,-12618,-12615,-12612,-12609,-12606,-12603,-12600,-12597,-12595,-12592,-12589,-12586,-12583,-12580,-12577,-12574,-12571,-12568,-12566,-12563,-12560,-12557,-12554,-12551,-12548,-12545,-12542,-12539,-12536,-12534,-12531,-12528,-12525,-12522,-12519,-12516,-12513,-12510,-12507,-12505,-12502,-12499,-12496,-12493,-12490,-12487,-12484,-12481,-12478,-12476,-12473,-12470,-12467,-12464,-12461,-12458,-12455,-12452,-12449,-12446,-12444,-12441,-12438,-12435,-12432,-12429,-12426,-12423,-12420,-12417,-12414,-12412,-12409,-12406,-12403,-12400,-12397,-12394,-12391,-12388,-12385,-12382,-12380,-12377,-12374,-12371,-12368,-12365,-12362,-12359,-12356,-12353,-12350,-12348,-12345,-12342,-12339,-12336,-12333,-12330,-12327,-12324,-12321,-12318,-12316,-12313,-12310,-12307,-12304,-12301,-12298,-12295,-12292,-12289,-12286,-12284,-12281,-12278,-12275,-12272,-12269,-12266,-12263,-12260,-12257,-12254,-12251,-12249,-12246,-12243,-12240,-12237,-12234,-12231,-12228,-12225,-12222,-12219,-12217,-12214,-12211,-12208,-12205,-12202,-12199,-12196,-12193,-12190,-12187,-12184,-12182,-12179,-12176,-12173,-12170,-12167,-12164,-12161,-12158,-12155,-12152,-12149,-12147,-12144,-12141,-12138,-12135,-12132,-12129,-12126,-12123,-12120,-12117,-12114,-12112,-12109,-12106,-12103,-12100,-12097,-12094,-12091,-12088,-12085,-12082,-12079,-12076,-12074,-12071,-12068,-12065,-12062,-12059,-12056,-12053,-12050,-12047,-12044,-12041,-12038,-12036,-12033,-12030,-12027,-12024,-12021,-12018,-12015,-12012,-12009,-12006,-12003,-12001,-11998,-11995,-11992,-11989,-11986,-11983,-11980,-11977,-11974,-11971,-11968,-11965,-11962,-11960,-11957,-11954,-11951,-11948,-11945,-11942,-11939,-11936,-11933,-11930,-11927,-11924,-11922,-11919,-11916,-11913,-11910,-11907,-11904,-11901,-11898,-11895,-11892,-11889,-11886,-11883,-11881,-11878,-11875,-11872,-11869,-11866,-11863,-11860,-11857,-11854,-11851,-11848,-11845,-11842,-11840,-11837,-11834,-11831,-11828,-11825,-11822,-11819,-11816,-11813,-11810,-11807,-11804,-11801,-11799,-11796,-11793,-11790,-11787,-11784,-11781,-11778,-11775,-11772,-11769,-11766,-11763,-11760,-11758,-11755,-11752,-11749,-11746,-11743,-11740,-11737,-11734,-11731,-11728,-11725,-11722,-11719,-11716,-11714,-11711,-11708,-11705,-11702,-11699,-11696,-11693,-11690,-11687,-11684,-11681,-11678,-11675,-11672,-11669,-11667,-11664,-11661,-11658,-11655,-11652,-11649,-11646,-11643,-11640,-11637,-11634,-11631,-11628,-11625,-11623,-11620,-11617,-11614,-11611,-11608,-11605,-11602,-11599,-11596,-11593,-11590,-11587,-11584,-11581,-11578,-11575,-11573,-11570,-11567,-11564,-11561,-11558,-11555,-11552,-11549,-11546,-11543,-11540,-11537,-11534,-11531,-11528,-11526,-11523,-11520,-11517,-11514,-11511,-11508,-11505,-11502,-11499,-11496,-11493,-11490,-11487,-11484,-11481,-11478,-11476,-11473,-11470,-11467,-11464,-11461,-11458,-11455,-11452,-11449,-11446,-11443,-11440,-11437,-11434,-11431,-11428,-11425,-11423,-11420,-11417,-11414,-11411,-11408,-11405,-11402,-11399,-11396,-11393,-11390,-11387,-11384,-11381,-11378,-11375,-11372,-11370,-11367,-11364,-11361,-11358,-11355,-11352,-11349,-11346,-11343,-11340,-11337,-11334,-11331,-11328,-11325,-11322,-11319,-11316,-11314,-11311,-11308,-11305,-11302,-11299,-11296,-11293,-11290,-11287,-11284,-11281,-11278,-11275,-11272,-11269,-11266,-11263,-11260,-11257,-11255,-11252,-11249,-11246,-11243,-11240,-11237,-11234,-11231,-11228,-11225,-11222,-11219,-11216,-11213,-11210,-11207,-11204,-11201,-11198,-11195,-11193,-11190,-11187,-11184,-11181,-11178,-11175,-11172,-11169,-11166,-11163,-11160,-11157,-11154,-11151,-11148,-11145,-11142,-11139,-11136,-11133,-11131,-11128,-11125,-11122,-11119,-11116,-11113,-11110,-11107,-11104,-11101,-11098,-11095,-11092,-11089,-11086,-11083,-11080,-11077,-11074,-11071,-11068,-11065,-11063,-11060,-11057,-11054,-11051,-11048,-11045,-11042,-11039,-11036,-11033,-11030,-11027,-11024,-11021,-11018,-11015,-11012,-11009,-11006,-11003,-11000,-10997,-10994,-10992,-10989,-10986,-10983,-10980,-10977,-10974,-10971,-10968,-10965,-10962,-10959,-10956,-10953,-10950,-10947,-10944,-10941,-10938,-10935,-10932,-10929,-10926,-10923,-10920,-10918,-10915,-10912,-10909,-10906,-10903,-10900,-10897,-10894,-10891,-10888,-10885,-10882,-10879,-10876,-10873,-10870,-10867,-10864,-10861,-10858,-10855,-10852,-10849,-10846,-10843,-10840,-10838,-10835,-10832,-10829,-10826,-10823,-10820,-10817,-10814,-10811,-10808,-10805,-10802,-10799,-10796,-10793,-10790,-10787,-10784,-10781,-10778,-10775,-10772,-10769,-10766,-10763,-10760,-10757,-10754,-10751,-10749,-10746,-10743,-10740,-10737,-10734,-10731,-10728,-10725,-10722,-10719,-10716,-10713,-10710,-10707,-10704,-10701,-10698,-10695,-10692,-10689,-10686,-10683,-10680,-10677,-10674,-10671,-10668,-10665,-10662,-10659,-10656,-10654,-10651,-10648,-10645,-10642,-10639,-10636,-10633,-10630,-10627,-10624,-10621,-10618,-10615,-10612,-10609,-10606,-10603,-10600,-10597,-10594,-10591,-10588,-10585,-10582,-10579,-10576,-10573,-10570,-10567,-10564,-10561,-10558,-10555,-10552,-10549,-10546,-10544,-10541,-10538,-10535,-10532,-10529,-10526,-10523,-10520,-10517,-10514,-10511,-10508,-10505,-10502,-10499,-10496,-10493,-10490,-10487,-10484,-10481,-10478,-10475,-10472,-10469,-10466,-10463,-10460,-10457,-10454,-10451,-10448,-10445,-10442,-10439,-10436,-10433,-10430,-10427,-10424,-10421,-10419,-10416,-10413,-10410,-10407,-10404,-10401,-10398,-10395,-10392,-10389,-10386,-10383,-10380,-10377,-10374,-10371,-10368,-10365,-10362,-10359,-10356,-10353,-10350,-10347,-10344,-10341,-10338,-10335,-10332,-10329,-10326,-10323,-10320,-10317,-10314,-10311,-10308,-10305,-10302,-10299,-10296,-10293,-10290,-10287,-10284,-10281,-10278,-10275,-10272,-10269,-10266,-10263,-10261,-10258,-10255,-10252,-10249,-10246,-10243,-10240,-10237,-10234,-10231,-10228,-10225,-10222,-10219,-10216,-10213,-10210,-10207,-10204,-10201,-10198,-10195,-10192,-10189,-10186,-10183,-10180,-10177,-10174,-10171,-10168,-10165,-10162,-10159,-10156,-10153,-10150,-10147,-10144,-10141,-10138,-10135,-10132,-10129,-10126,-10123,-10120,-10117,-10114,-10111,-10108,-10105,-10102,-10099,-10096,-10093,-10090,-10087,-10084,-10081,-10078,-10075,-10072,-10069,-10066,-10063,-10060,-10057,-10054,-10051,-10048,-10045,-10042,-10039,-10036,-10033,-10031,-10028,-10025,-10022,-10019,-10016,-10013,-10010,-10007,-10004,-10001,-9998,-9995,-9992,-9989,-9986,-9983,-9980,-9977,-9974,-9971,-9968,-9965,-9962,-9959,-9956,-9953,-9950,-9947,-9944,-9941,-9938,-9935,-9932,-9929,-9926,-9923,-9920,-9917,-9914,-9911,-9908,-9905,-9902,-9899,-9896,-9893,-9890,-9887,-9884,-9881,-9878,-9875,-9872,-9869,-9866,-9863,-9860,-9857,-9854,-9851,-9848,-9845,-9842,-9839,-9836,-9833,-9830,-9827,-9824,-9821,-9818,-9815,-9812,-9809,-9806,-9803,-9800,-9797,-9794,-9791,-9788,-9785,-9782,-9779,-9776,-9773,-9770,-9767,-9764,-9761,-9758,-9755,-9752,-9749,-9746,-9743,-9740,-9737,-9734,-9731,-9728,-9725,-9722,-9719,-9716,-9713,-9710,-9707,-9704,-9701,-9698,-9695,-9692,-9689,-9686,-9683,-9680,-9677,-9674,-9671,-9668,-9665,-9662,-9659,-9656,-9653,-9650,-9647,-9644,-9641,-9638,-9635,-9632,-9629,-9626,-9623,-9620,-9617,-9614,-9611,-9608,-9605,-9602,-9599,-9596,-9593,-9590,-9587,-9584,-9581,-9578,-9575,-9572,-9569,-9566,-9563,-9560,-9557,-9554,-9551,-9548,-9545,-9542,-9539,-9536,-9533,-9530,-9527,-9524,-9521,-9518,-9515,-9512,-9509,-9506,-9503,-9500,-9497,-9494,-9491,-9488,-9485,-9482,-9479,-9476,-9473,-9470,-9467,-9464,-9461,-9458,-9455,-9452,-9449,-9446,-9443,-9440,-9437,-9434,-9431,-9428,-9425,-9422,-9419,-9416,-9413,-9409,-9406,-9403,-9400,-9397,-9394,-9391,-9388,-9385,-9382,-9379,-9376,-9373,-9370,-9367,-9364,-9361,-9358,-9355,-9352,-9349,-9346,-9343,-9340,-9337,-9334,-9331,-9328,-9325,-9322,-9319,-9316,-9313,-9310,-9307,-9304,-9301,-9298,-9295,-9292,-9289,-9286,-9283,-9280,-9277,-9274,-9271,-9268,-9265,-9262,-9259,-9256,-9253,-9250,-9247,-9244,-9241,-9238,-9235,-9232,-9229,-9226,-9223,-9220,-9217,-9214,-9211,-9208,-9205,-9202,-9199,-9196,-9193,-9190,-9187,-9184,-9181,-9178,-9175,-9172,-9168,-9165,-9162,-9159,-9156,-9153,-9150,-9147,-9144,-9141,-9138,-9135,-9132,-9129,-9126,-9123,-9120,-9117,-9114,-9111,-9108,-9105,-9102,-9099,-9096,-9093,-9090,-9087,-9084,-9081,-9078,-9075,-9072,-9069,-9066,-9063,-9060,-9057,-9054,-9051,-9048,-9045,-9042,-9039,-9036,-9033,-9030,-9027,-9024,-9021,-9018,-9015,-9012,-9009,-9006,-9002,-8999,-8996,-8993,-8990,-8987,-8984,-8981,-8978,-8975,-8972,-8969,-8966,-8963,-8960,-8957,-8954,-8951,-8948,-8945,-8942,-8939,-8936,-8933,-8930,-8927,-8924,-8921,-8918,-8915,-8912,-8909,-8906,-8903,-8900,-8897,-8894,-8891,-8888,-8885,-8882,-8879,-8876,-8873,-8869,-8866,-8863,-8860,-8857,-8854,-8851,-8848,-8845,-8842,-8839,-8836,-8833,-8830,-8827,-8824,-8821,-8818,-8815,-8812,-8809,-8806,-8803,-8800,-8797,-8794,-8791,-8788,-8785,-8782,-8779,-8776,-8773,-8770,-8767,-8764,-8761,-8758,-8755,-8751,-8748,-8745,-8742,-8739,-8736,-8733,-8730,-8727,-8724,-8721,-8718,-8715,-8712,-8709,-8706,-8703,-8700,-8697,-8694,-8691,-8688,-8685,-8682,-8679,-8676,-8673,-8670,-8667,-8664,-8661,-8658,-8655,-8652,-8649,-8645,-8642,-8639,-8636,-8633,-8630,-8627,-8624,-8621,-8618,-8615,-8612,-8609,-8606,-8603,-8600,-8597,-8594,-8591,-8588,-8585,-8582,-8579,-8576,-8573,-8570,-8567,-8564,-8561,-8558,-8555,-8552,-8548,-8545,-8542,-8539,-8536,-8533,-8530,-8527,-8524,-8521,-8518,-8515,-8512,-8509,-8506,-8503,-8500,-8497,-8494,-8491,-8488,-8485,-8482,-8479,-8476,-8473,-8470,-8467,-8464,-8460,-8457,-8454,-8451,-8448,-8445,-8442,-8439,-8436,-8433,-8430,-8427,-8424,-8421,-8418,-8415,-8412,-8409,-8406,-8403,-8400,-8397,-8394,-8391,-8388,-8385,-8382,-8379,-8375,-8372,-8369,-8366,-8363,-8360,-8357,-8354,-8351,-8348,-8345,-8342,-8339,-8336,-8333,-8330,-8327,-8324,-8321,-8318,-8315,-8312,-8309,-8306,-8303,-8300,-8296,-8293,-8290,-8287,-8284,-8281,-8278,-8275,-8272,-8269,-8266,-8263,-8260,-8257,-8254,-8251,-8248,-8245,-8242,-8239,-8236,-8233,-8230,-8227,-8224,-8220,-8217,-8214,-8211,-8208,-8205,-8202,-8199,-8196,-8193,-8190,-8187,-8184,-8181,-8178,-8175,-8172,-8169,-8166,-8163,-8160,-8157,-8154,-8151,-8147,-8144,-8141,-8138,-8135,-8132,-8129,-8126,-8123,-8120,-8117,-8114,-8111,-8108,-8105,-8102,-8099,-8096,-8093,-8090,-8087,-8084,-8081,-8077,-8074,-8071,-8068,-8065,-8062,-8059,-8056,-8053,-8050,-8047,-8044,-8041,-8038,-8035,-8032,-8029,-8026,-8023,-8020,-8017,-8014,-8010,-8007,-8004,-8001,-7998,-7995,-7992,-7989,-7986,-7983,-7980,-7977,-7974,-7971,-7968,-7965,-7962,-7959,-7956,-7953,-7950,-7946,-7943,-7940,-7937,-7934,-7931,-7928,-7925,-7922,-7919,-7916,-7913,-7910,-7907,-7904,-7901,-7898,-7895,-7892,-7889,-7886,-7882,-7879,-7876,-7873,-7870,-7867,-7864,-7861,-7858,-7855,-7852,-7849,-7846,-7843,-7840,-7837,-7834,-7831,-7828,-7825,-7821,-7818,-7815,-7812,-7809,-7806,-7803,-7800,-7797,-7794,-7791,-7788,-7785,-7782,-7779,-7776,-7773,-7770,-7767,-7764,-7760,-7757,-7754,-7751,-7748,-7745,-7742,-7739,-7736,-7733,-7730,-7727,-7724,-7721,-7718,-7715,-7712,-7709,-7705,-7702,-7699,-7696,-7693,-7690,-7687,-7684,-7681,-7678,-7675,-7672,-7669,-7666,-7663,-7660,-7657,-7654,-7651,-7647,-7644,-7641,-7638,-7635,-7632,-7629,-7626,-7623,-7620,-7617,-7614,-7611,-7608,-7605,-7602,-7599,-7596,-7592,-7589,-7586,-7583,-7580,-7577,-7574,-7571,-7568,-7565,-7562,-7559,-7556,-7553,-7550,-7547,-7544,-7541,-7537,-7534,-7531,-7528,-7525,-7522,-7519,-7516,-7513,-7510,-7507,-7504,-7501,-7498,-7495,-7492,-7489,-7485,-7482,-7479,-7476,-7473,-7470,-7467,-7464,-7461,-7458,-7455,-7452,-7449,-7446,-7443,-7440,-7437,-7433,-7430,-7427,-7424,-7421,-7418,-7415,-7412,-7409,-7406,-7403,-7400,-7397,-7394,-7391,-7388,-7385,-7381,-7378,-7375,-7372,-7369,-7366,-7363,-7360,-7357,-7354,-7351,-7348,-7345,-7342,-7339,-7336,-7332,-7329,-7326,-7323,-7320,-7317,-7314,-7311,-7308,-7305,-7302,-7299,-7296,-7293,-7290,-7287,-7283,-7280,-7277,-7274,-7271,-7268,-7265,-7262,-7259,-7256,-7253,-7250,-7247,-7244,-7241,-7238,-7234,-7231,-7228,-7225,-7222,-7219,-7216,-7213,-7210,-7207,-7204,-7201,-7198,-7195,-7192,-7188,-7185,-7182,-7179,-7176,-7173,-7170,-7167,-7164,-7161,-7158,-7155,-7152,-7149,-7146,-7143,-7139,-7136,-7133,-7130,-7127,-7124,-7121,-7118,-7115,-7112,-7109,-7106,-7103,-7100,-7097,-7093,-7090,-7087,-7084,-7081,-7078,-7075,-7072,-7069,-7066,-7063,-7060,-7057,-7054,-7050,-7047,-7044,-7041,-7038,-7035,-7032,-7029,-7026,-7023,-7020,-7017,-7014,-7011,-7008,-7004,-7001,-6998,-6995,-6992,-6989,-6986,-6983,-6980,-6977,-6974,-6971,-6968,-6965,-6961,-6958,-6955,-6952,-6949,-6946,-6943,-6940,-6937,-6934,-6931,-6928,-6925,-6922,-6919,-6915,-6912,-6909,-6906,-6903,-6900,-6897,-6894,-6891,-6888,-6885,-6882,-6879,-6876,-6872,-6869,-6866,-6863,-6860,-6857,-6854,-6851,-6848,-6845,-6842,-6839,-6836,-6833,-6829,-6826,-6823,-6820,-6817,-6814,-6811,-6808,-6805,-6802,-6799,-6796,-6793,-6789,-6786,-6783,-6780,-6777,-6774,-6771,-6768,-6765,-6762,-6759,-6756,-6753,-6750,-6746,-6743,-6740,-6737,-6734,-6731,-6728,-6725,-6722,-6719,-6716,-6713,-6710,-6706,-6703,-6700,-6697,-6694,-6691,-6688,-6685,-6682,-6679,-6676,-6673,-6670,-6667,-6663,-6660,-6657,-6654,-6651,-6648,-6645,-6642,-6639,-6636,-6633,-6630,-6627,-6623,-6620,-6617,-6614,-6611,-6608,-6605,-6602,-6599,-6596,-6593,-6590,-6587,-6583,-6580,-6577,-6574,-6571,-6568,-6565,-6562,-6559,-6556,-6553,-6550,-6547,-6543,-6540,-6537,-6534,-6531,-6528,-6525,-6522,-6519,-6516,-6513,-6510,-6506,-6503,-6500,-6497,-6494,-6491,-6488,-6485,-6482,-6479,-6476,-6473,-6470,-6466,-6463,-6460,-6457,-6454,-6451,-6448,-6445,-6442,-6439,-6436,-6433,-6429,-6426,-6423,-6420,-6417,-6414,-6411,-6408,-6405,-6402,-6399,-6396,-6393,-6389,-6386,-6383,-6380,-6377,-6374,-6371,-6368,-6365,-6362,-6359,-6356,-6352,-6349,-6346,-6343,-6340,-6337,-6334,-6331,-6328,-6325,-6322,-6319,-6315,-6312,-6309,-6306,-6303,-6300,-6297,-6294,-6291,-6288,-6285,-6282,-6278,-6275,-6272,-6269,-6266,-6263,-6260,-6257,-6254,-6251,-6248,-6245,-6241,-6238,-6235,-6232,-6229,-6226,-6223,-6220,-6217,-6214,-6211,-6208,-6204,-6201,-6198,-6195,-6192,-6189,-6186,-6183,-6180,-6177,-6174,-6171,-6167,-6164,-6161,-6158,-6155,-6152,-6149,-6146,-6143,-6140,-6137,-6134,-6130,-6127,-6124,-6121,-6118,-6115,-6112,-6109,-6106,-6103,-6100,-6096,-6093,-6090,-6087,-6084,-6081,-6078,-6075,-6072,-6069,-6066,-6063,-6059,-6056,-6053,-6050,-6047,-6044,-6041,-6038,-6035,-6032,-6029,-6025,-6022,-6019,-6016,-6013,-6010,-6007,-6004,-6001,-5998,-5995,-5991,-5988,-5985,-5982,-5979,-5976,-5973,-5970,-5967,-5964,-5961,-5958,-5954,-5951,-5948,-5945,-5942,-5939,-5936,-5933,-5930,-5927,-5924,-5920,-5917,-5914,-5911,-5908,-5905,-5902,-5899,-5896,-5893,-5890,-5886,-5883,-5880,-5877,-5874,-5871,-5868,-5865,-5862,-5859,-5856,-5852,-5849,-5846,-5843,-5840,-5837,-5834,-5831,-5828,-5825,-5822,-5818,-5815,-5812,-5809,-5806,-5803,-5800,-5797,-5794,-5791,-5788,-5784,-5781,-5778,-5775,-5772,-5769,-5766,-5763,-5760,-5757,-5754,-5750,-5747,-5744,-5741,-5738,-5735,-5732,-5729,-5726,-5723,-5719,-5716,-5713,-5710,-5707,-5704,-5701,-5698,-5695,-5692,-5689,-5685,-5682,-5679,-5676,-5673,-5670,-5667,-5664,-5661,-5658,-5655,-5651,-5648,-5645,-5642,-5639,-5636,-5633,-5630,-5627,-5624,-5620,-5617,-5614,-5611,-5608,-5605,-5602,-5599,-5596,-5593,-5590,-5586,-5583,-5580,-5577,-5574,-5571,-5568,-5565,-5562,-5559,-5555,-5552,-5549,-5546,-5543,-5540,-5537,-5534,-5531,-5528,-5525,-5521,-5518,-5515,-5512,-5509,-5506,-5503,-5500,-5497,-5494,-5490,-5487,-5484,-5481,-5478,-5475,-5472,-5469,-5466,-5463,-5459,-5456,-5453,-5450,-5447,-5444,-5441,-5438,-5435,-5432,-5428,-5425,-5422,-5419,-5416,-5413,-5410,-5407,-5404,-5401,-5398,-5394,-5391,-5388,-5385,-5382,-5379,-5376,-5373,-5370,-5367,-5363,-5360,-5357,-5354,-5351,-5348,-5345,-5342,-5339,-5336,-5332,-5329,-5326,-5323,-5320,-5317,-5314,-5311,-5308,-5305,-5301,-5298,-5295,-5292,-5289,-5286,-5283,-5280,-5277,-5274,-5270,-5267,-5264,-5261,-5258,-5255,-5252,-5249,-5246,-5243,-5239,-5236,-5233,-5230,-5227,-5224,-5221,-5218,-5215,-5212,-5208,-5205,-5202,-5199,-5196,-5193,-5190,-5187,-5184,-5180,-5177,-5174,-5171,-5168,-5165,-5162,-5159,-5156,-5153,-5149,-5146,-5143,-5140,-5137,-5134,-5131,-5128,-5125,-5122,-5118,-5115,-5112,-5109,-5106,-5103,-5100,-5097,-5094,-5091,-5087,-5084,-5081,-5078,-5075,-5072,-5069,-5066,-5063,-5059,-5056,-5053,-5050,-5047,-5044,-5041,-5038,-5035,-5032,-5028,-5025,-5022,-5019,-5016,-5013,-5010,-5007,-5004,-5000,-4997,-4994,-4991,-4988,-4985,-4982,-4979,-4976,-4973,-4969,-4966,-4963,-4960,-4957,-4954,-4951,-4948,-4945,-4941,-4938,-4935,-4932,-4929,-4926,-4923,-4920,-4917,-4914,-4910,-4907,-4904,-4901,-4898,-4895,-4892,-4889,-4886,-4882,-4879,-4876,-4873,-4870,-4867,-4864,-4861,-4858,-4855,-4851,-4848,-4845,-4842,-4839,-4836,-4833,-4830,-4827,-4823,-4820,-4817,-4814,-4811,-4808,-4805,-4802,-4799,-4795,-4792,-4789,-4786,-4783,-4780,-4777,-4774,-4771,-4768,-4764,-4761,-4758,-4755,-4752,-4749,-4746,-4743,-4740,-4736,-4733,-4730,-4727,-4724,-4721,-4718,-4715,-4712,-4708,-4705,-4702,-4699,-4696,-4693,-4690,-4687,-4684,-4680,-4677,-4674,-4671,-4668,-4665,-4662,-4659,-4656,-4652,-4649,-4646,-4643,-4640,-4637,-4634,-4631,-4628,-4624,-4621,-4618,-4615,-4612,-4609,-4606,-4603,-4600,-4597,-4593,-4590,-4587,-4584,-4581,-4578,-4575,-4572,-4569,-4565,-4562,-4559,-4556,-4553,-4550,-4547,-4544,-4541,-4537,-4534,-4531,-4528,-4525,-4522,-4519,-4516,-4513,-4509,-4506,-4503,-4500,-4497,-4494,-4491,-4488,-4485,-4481,-4478,-4475,-4472,-4469,-4466,-4463,-4460,-4456,-4453,-4450,-4447,-4444,-4441,-4438,-4435,-4432,-4428,-4425,-4422,-4419,-4416,-4413,-4410,-4407,-4404,-4400,-4397,-4394,-4391,-4388,-4385,-4382,-4379,-4376,-4372,-4369,-4366,-4363,-4360,-4357,-4354,-4351,-4348,-4344,-4341,-4338,-4335,-4332,-4329,-4326,-4323,-4320,-4316,-4313,-4310,-4307,-4304,-4301,-4298,-4295,-4291,-4288,-4285,-4282,-4279,-4276,-4273,-4270,-4267,-4263,-4260,-4257,-4254,-4251,-4248,-4245,-4242,-4239,-4235,-4232,-4229,-4226,-4223,-4220,-4217,-4214,-4210,-4207,-4204,-4201,-4198,-4195,-4192,-4189,-4186,-4182,-4179,-4176,-4173,-4170,-4167,-4164,-4161,-4158,-4154,-4151,-4148,-4145,-4142,-4139,-4136,-4133,-4129,-4126,-4123,-4120,-4117,-4114,-4111,-4108,-4105,-4101,-4098,-4095,-4092,-4089,-4086,-4083,-4080,-4076,-4073,-4070,-4067,-4064,-4061,-4058,-4055,-4052,-4048,-4045,-4042,-4039,-4036,-4033,-4030,-4027,-4024,-4020,-4017,-4014,-4011,-4008,-4005,-4002,-3999,-3995,-3992,-3989,-3986,-3983,-3980,-3977,-3974,-3970,-3967,-3964,-3961,-3958,-3955,-3952,-3949,-3946,-3942,-3939,-3936,-3933,-3930,-3927,-3924,-3921,-3917,-3914,-3911,-3908,-3905,-3902,-3899,-3896,-3893,-3889,-3886,-3883,-3880,-3877,-3874,-3871,-3868,-3864,-3861,-3858,-3855,-3852,-3849,-3846,-3843,-3839,-3836,-3833,-3830,-3827,-3824,-3821,-3818,-3815,-3811,-3808,-3805,-3802,-3799,-3796,-3793,-3790,-3786,-3783,-3780,-3777,-3774,-3771,-3768,-3765,-3761,-3758,-3755,-3752,-3749,-3746,-3743,-3740,-3737,-3733,-3730,-3727,-3724,-3721,-3718,-3715,-3712,-3708,-3705,-3702,-3699,-3696,-3693,-3690,-3687,-3683,-3680,-3677,-3674,-3671,-3668,-3665,-3662,-3658,-3655,-3652,-3649,-3646,-3643,-3640,-3637,-3634,-3630,-3627,-3624,-3621,-3618,-3615,-3612,-3609,-3605,-3602,-3599,-3596,-3593,-3590,-3587,-3584,-3580,-3577,-3574,-3571,-3568,-3565,-3562,-3559,-3555,-3552,-3549,-3546,-3543,-3540,-3537,-3534,-3530,-3527,-3524,-3521,-3518,-3515,-3512,-3509,-3505,-3502,-3499,-3496,-3493,-3490,-3487,-3484,-3480,-3477,-3474,-3471,-3468,-3465,-3462,-3459,-3455,-3452,-3449,-3446,-3443,-3440,-3437,-3434,-3430,-3427,-3424,-3421,-3418,-3415,-3412,-3409,-3406,-3402,-3399,-3396,-3393,-3390,-3387,-3384,-3381,-3377,-3374,-3371,-3368,-3365,-3362,-3359,-3356,-3352,-3349,-3346,-3343,-3340,-3337,-3334,-3331,-3327,-3324,-3321,-3318,-3315,-3312,-3309,-3306,-3302,-3299,-3296,-3293,-3290,-3287,-3284,-3281,-3277,-3274,-3271,-3268,-3265,-3262,-3259,-3255,-3252,-3249,-3246,-3243,-3240,-3237,-3234,-3230,-3227,-3224,-3221,-3218,-3215,-3212,-3209,-3205,-3202,-3199,-3196,-3193,-3190,-3187,-3184,-3180,-3177,-3174,-3171,-3168,-3165,-3162,-3159,-3155,-3152,-3149,-3146,-3143,-3140,-3137,-3134,-3130,-3127,-3124,-3121,-3118,-3115,-3112,-3109,-3105,-3102,-3099,-3096,-3093,-3090,-3087,-3084,-3080,-3077,-3074,-3071,-3068,-3065,-3062,-3059,-3055,-3052,-3049,-3046,-3043,-3040,-3037,-3033,-3030,-3027,-3024,-3021,-3018,-3015,-3012,-3008,-3005,-3002,-2999,-2996,-2993,-2990,-2987,-2983,-2980,-2977,-2974,-2971,-2968,-2965,-2962,-2958,-2955,-2952,-2949,-2946,-2943,-2940,-2936,-2933,-2930,-2927,-2924,-2921,-2918,-2915,-2911,-2908,-2905,-2902,-2899,-2896,-2893,-2890,-2886,-2883,-2880,-2877,-2874,-2871,-2868,-2865,-2861,-2858,-2855,-2852,-2849,-2846,-2843,-2839,-2836,-2833,-2830,-2827,-2824,-2821,-2818,-2814,-2811,-2808,-2805,-2802,-2799,-2796,-2793,-2789,-2786,-2783,-2780,-2777,-2774,-2771,-2767,-2764,-2761,-2758,-2755,-2752,-2749,-2746,-2742,-2739,-2736,-2733,-2730,-2727,-2724,-2721,-2717,-2714,-2711,-2708,-2705,-2702,-2699,-2695,-2692,-2689,-2686,-2683,-2680,-2677,-2674,-2670,-2667,-2664,-2661,-2658,-2655,-2652,-2649,-2645,-2642,-2639,-2636,-2633,-2630,-2627,-2623,-2620,-2617,-2614,-2611,-2608,-2605,-2602,-2598,-2595,-2592,-2589,-2586,-2583,-2580,-2577,-2573,-2570,-2567,-2564,-2561,-2558,-2555,-2551,-2548,-2545,-2542,-2539,-2536,-2533,-2530,-2526,-2523,-2520,-2517,-2514,-2511,-2508,-2504,-2501,-2498,-2495,-2492,-2489,-2486,-2483,-2479,-2476,-2473,-2470,-2467,-2464,-2461,-2457,-2454,-2451,-2448,-2445,-2442,-2439,-2436,-2432,-2429,-2426,-2423,-2420,-2417,-2414,-2410,-2407,-2404,-2401,-2398,-2395,-2392,-2389,-2385,-2382,-2379,-2376,-2373,-2370,-2367,-2363,-2360,-2357,-2354,-2351,-2348,-2345,-2342,-2338,-2335,-2332,-2329,-2326,-2323,-2320,-2316,-2313,-2310,-2307,-2304,-2301,-2298,-2295,-2291,-2288,-2285,-2282,-2279,-2276,-2273,-2269,-2266,-2263,-2260,-2257,-2254,-2251,-2248,-2244,-2241,-2238,-2235,-2232,-2229,-2226,-2222,-2219,-2216,-2213,-2210,-2207,-2204,-2201,-2197,-2194,-2191,-2188,-2185,-2182,-2179,-2175,-2172,-2169,-2166,-2163,-2160,-2157,-2154,-2150,-2147,-2144,-2141,-2138,-2135,-2132,-2128,-2125,-2122,-2119,-2116,-2113,-2110,-2106,-2103,-2100,-2097,-2094,-2091,-2088,-2085,-2081,-2078,-2075,-2072,-2069,-2066,-2063,-2059,-2056,-2053,-2050,-2047,-2044,-2041,-2038,-2034,-2031,-2028,-2025,-2022,-2019,-2016,-2012,-2009,-2006,-2003,-2000,-1997,-1994,-1990,-1987,-1984,-1981,-1978,-1975,-1972,-1969,-1965,-1962,-1959,-1956,-1953,-1950,-1947,-1943,-1940,-1937,-1934,-1931,-1928,-1925,-1921,-1918,-1915,-1912,-1909,-1906,-1903,-1900,-1896,-1893,-1890,-1887,-1884,-1881,-1878,-1874,-1871,-1868,-1865,-1862,-1859,-1856,-1852,-1849,-1846,-1843,-1840,-1837,-1834,-1831,-1827,-1824,-1821,-1818,-1815,-1812,-1809,-1805,-1802,-1799,-1796,-1793,-1790,-1787,-1783,-1780,-1777,-1774,-1771,-1768,-1765,-1762,-1758,-1755,-1752,-1749,-1746,-1743,-1740,-1736,-1733,-1730,-1727,-1724,-1721,-1718,-1714,-1711,-1708,-1705,-1702,-1699,-1696,-1693,-1689,-1686,-1683,-1680,-1677,-1674,-1671,-1667,-1664,-1661,-1658,-1655,-1652,-1649,-1645,-1642,-1639,-1636,-1633,-1630,-1627,-1623,-1620,-1617,-1614,-1611,-1608,-1605,-1602,-1598,-1595,-1592,-1589,-1586,-1583,-1580,-1576,-1573,-1570,-1567,-1564,-1561,-1558,-1554,-1551,-1548,-1545,-1542,-1539,-1536,-1532,-1529,-1526,-1523,-1520,-1517,-1514,-1511,-1507,-1504,-1501,-1498,-1495,-1492,-1489,-1485,-1482,-1479,-1476,-1473,-1470,-1467,-1463,-1460,-1457,-1454,-1451,-1448,-1445,-1441,-1438,-1435,-1432,-1429,-1426,-1423,-1420,-1416,-1413,-1410,-1407,-1404,-1401,-1398,-1394,-1391,-1388,-1385,-1382,-1379,-1376,-1372,-1369,-1366,-1363,-1360,-1357,-1354,-1350,-1347,-1344,-1341,-1338,-1335,-1332,-1328,-1325,-1322,-1319,-1316,-1313,-1310,-1307,-1303,-1300,-1297,-1294,-1291,-1288,-1285,-1281,-1278,-1275,-1272,-1269,-1266,-1263,-1259,-1256,-1253,-1250,-1247,-1244,-1241,-1237,-1234,-1231,-1228,-1225,-1222,-1219,-1215,-1212,-1209,-1206,-1203,-1200,-1197,-1194,-1190,-1187,-1184,-1181,-1178,-1175,-1172,-1168,-1165,-1162,-1159,-1156,-1153,-1150,-1146,-1143,-1140,-1137,-1134,-1131,-1128,-1124,-1121,-1118,-1115,-1112,-1109,-1106,-1102,-1099,-1096,-1093,-1090,-1087,-1084,-1080,-1077,-1074,-1071,-1068,-1065,-1062,-1059,-1055,-1052,-1049,-1046,-1043,-1040,-1037,-1033,-1030,-1027,-1024,-1021,-1018,-1015,-1011,-1008,-1005,-1002,-999,-996,-993,-989,-986,-983,-980,-977,-974,-971,-967,-964,-961,-958,-955,-952,-949,-945,-942,-939,-936,-933,-930,-927,-923,-920,-917,-914,-911,-908,-905,-901,-898,-895,-892,-889,-886,-883,-880,-876,-873,-870,-867,-864,-861,-858,-854,-851,-848,-845,-842,-839,-836,-832,-829,-826,-823,-820,-817,-814,-810,-807,-804,-801,-798,-795,-792,-788,-785,-782,-779,-776,-773,-770,-766,-763,-760,-757,-754,-751,-748,-744,-741,-738,-735,-732,-729,-726,-722,-719,-716,-713,-710,-707,-704,-701,-697,-694,-691,-688,-685,-682,-679,-675,-672,-669,-666,-663,-660,-657,-653,-650,-647,-644,-641,-638,-635,-631,-628,-625,-622,-619,-616,-613,-609,-606,-603,-600,-597,-594,-591,-587,-584,-581,-578,-575,-572,-569,-565,-562,-559,-556,-553,-550,-547,-543,-540,-537,-534,-531,-528,-525,-521,-518,-515,-512,-509,-506,-503,-499,-496,-493,-490,-487,-484,-481,-477,-474,-471,-468,-465,-462,-459,-456,-452,-449,-446,-443,-440,-437,-434,-430,-427,-424,-421,-418,-415,-412,-408,-405,-402,-399,-396,-393,-390,-386,-383,-380,-377,-374,-371,-368,-364,-361,-358,-355,-352,-349,-346,-342,-339,-336,-333,-330,-327,-324,-320,-317,-314,-311,-308,-305,-302,-298,-295,-292,-289,-286,-283,-280,-276,-273,-270,-267,-264,-261,-258,-254,-251,-248,-245,-242,-239,-236,-232,-229,-226,-223,-220,-217,-214,-210,-207,-204,-201,-198,-195,-192,-188,-185,-182,-179,-176,-173,-170,-166,-163,-160,-157,-154,-151,-148,-145,-141,-138,-135,-132,-129,-126,-123,-119,-116,-113,-110,-107,-104,-101,-97,-94,-91,-88,-85,-82,-79,-75,-72,-69,-66,-63,-60,-57,-53,-50,-47,-44,-41,-38,-35,-31,-28,-25,-22,-19,-16,-13,-9,-6,-3, -}; \ No newline at end of file diff --git a/src/drivers/x86/bochs_vbe/CMakeLists.txt b/src/drivers/x86/bochs_vbe/CMakeLists.txt deleted file mode 100644 index 64bda801..00000000 --- a/src/drivers/x86/bochs_vbe/CMakeLists.txt +++ /dev/null @@ -1,32 +0,0 @@ -# drivers/x86/bochs_vbe/CMakeLists.txt -# -# Copyright (c) 2024 Omar Berrow - -add_executable(bochs_vbe "main.c" "suspend.c" "io.c") - -target_compile_options(bochs_vbe - PRIVATE $<$:${TARGET_DRIVER_COMPILE_OPTIONS_C}> - PRIVATE $<$:-ffreestanding> - PRIVATE $<$:-Wall> - PRIVATE $<$:-Wextra> - PRIVATE $<$:-fstack-protector-all> - PRIVATE $<$:-fno-builtin-memset> - PRIVATE $<$:-fvisibility=hidden> - PRIVATE $<$:-fPIC> -) - -set_property(TARGET bochs_vbe PROPERTY link_depends ${DRIVER_LINKER_SCRIPT}) - -target_include_directories(bochs_vbe - PRIVATE "${CMAKE_SOURCE_DIR}/src/oboskrnl" - PRIVATE ${OBOSKRNL_EXTERNAL_INCLUDES}) - -target_link_options(bochs_vbe - PRIVATE "-nostdlib" - PRIVATE "-fPIC" - PRIVATE "-Wl,-shared" -# PRIVATE "-Wl,--allow-shlib-undefined" - PRIVATE "-T" PRIVATE ${DRIVER_LINKER_SCRIPT} - PRIVATE ${TARGET_DRIVER_LINKER_OPTIONS} -) -target_compile_definitions(bochs_vbe PRIVATE OBOS_DRIVER=1) diff --git a/src/drivers/x86/bochs_vbe/io.c b/src/drivers/x86/bochs_vbe/io.c deleted file mode 100644 index c377ffea..00000000 --- a/src/drivers/x86/bochs_vbe/io.c +++ /dev/null @@ -1,25 +0,0 @@ -/* - * drivers/x86/bochs_vbe/io.c - * - * Copyright (c) 2024 Omar Berrow - */ - -#include - -#include - -#include "io.h" - -#define INDEX_REG 0x1ce -#define DATA_REG 0x1cf - -void WriteRegister(uint16_t index, uint16_t val) -{ - outw(INDEX_REG, index); - outw(DATA_REG, val); -} -uint16_t ReadRegister(uint16_t index) -{ - outw(INDEX_REG, index); - return inw(DATA_REG); -} diff --git a/src/drivers/x86/bochs_vbe/io.h b/src/drivers/x86/bochs_vbe/io.h deleted file mode 100644 index eb6770a7..00000000 --- a/src/drivers/x86/bochs_vbe/io.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * drivers/x86/bochs_vbe/io.h - * - * Copyright (c) 2024 Omar Berrow - */ - -#pragma once - -#include - -#define INDEX_ID 0 -#define INDEX_XRES 1 -#define INDEX_YRES 2 -#define INDEX_BPP 3 -#define INDEX_ENABLE 4 -#define INDEX_BANK 5 -#define INDEX_VIRT_WIDTH 6 -#define INDEX_VIRT_HEIGHT 7 -#define INDEX_X_OFFSET 8 -#define INDEX_Y_OFFSET 9 - -void WriteRegister(uint16_t index, uint16_t val); -uint16_t ReadRegister(uint16_t index); diff --git a/src/drivers/x86/bochs_vbe/main.c b/src/drivers/x86/bochs_vbe/main.c deleted file mode 100644 index e83e08d6..00000000 --- a/src/drivers/x86/bochs_vbe/main.c +++ /dev/null @@ -1,94 +0,0 @@ -/* - * drivers/x86/boches_vbe/main.c - * - * Copyright (c) 2024-2025 Omar Berrow - */ - -#include -#include -#include -#include - -#include - -#include - -#include -#include - -#include -#include -#include - -#include - -#if !defined(__x86_64__) && !defined(__i686__) -# error Invalid target for Bochs VBE driver. -#endif - -// Defined in suspend.c -void on_wake(); -// Defined in suspend.c -void on_suspend(); - -void cleanup() {} - -__attribute__((section(OBOS_DRIVER_HEADER_SECTION))) driver_header drv_hdr = { - .magic = OBOS_DRIVER_MAGIC, - .flags = - DRIVER_HEADER_FLAGS_DETECT_VIA_PCI | - DRIVER_HEADER_HAS_VERSION_FIELD | - DRIVER_HEADER_PCI_HAS_DEVICE_ID | - DRIVER_HEADER_PCI_HAS_VENDOR_ID, - .pciId.indiv = { - // VGA-Compatible - .classCode = 0x0003, - .subClass = 0x0000, - .progIf = 0x0000, - // Bochs VBE - .vendorId = 0x1234, - .deviceId = 0x1111, - }, - .ftable = { - .driver_cleanup_callback = cleanup, - // .ioctl = ioctl, - // .ioctl_var = ioctl_var, - // .get_blk_size = get_blk_size, - // .get_max_blk_count = get_max_blk_count, - // .query_user_readable_name = query_user_readable_name, - // .foreach_device = foreach_device, - // .read_sync = read_sync, - // .write_sync = write_sync, - .on_suspend = on_suspend, - .on_wake = on_wake, - }, - .driverName = "Bochs VBE Driver", - .version = 1, - .uacpi_init_level_required = UACPI_INIT_LEVEL_EARLY -}; - -pci_device* PCIDevice; - -static void search_bus(pci_bus* bus) -{ - for (pci_device* dev = LIST_GET_HEAD(pci_device_list, &bus->devices); dev; ) - { - if (memcmp(&dev->hid, &drv_hdr.pciId, sizeof(pci_hid))) - { - PCIDevice = dev; - break; - } - - dev = LIST_GET_NEXT(pci_device_list, &bus->devices, dev); - } -} - -OBOS_PAGEABLE_FUNCTION driver_init_status OBOS_DriverEntry(driver_id* this) -{ - for (size_t i = 0; i < Drv_PCIBusCount; i++) - search_bus(&Drv_PCIBuses[i]); - if (!PCIDevice) - return (driver_init_status){.status=OBOS_STATUS_NOT_FOUND,.context="Could not find Bochs VBE device.",.fatal=true}; - // Maybe we can add stuff here eventually... - return (driver_init_status){.status=OBOS_STATUS_SUCCESS}; -} diff --git a/src/drivers/x86/bochs_vbe/suspend.c b/src/drivers/x86/bochs_vbe/suspend.c deleted file mode 100644 index 6118e015..00000000 --- a/src/drivers/x86/bochs_vbe/suspend.c +++ /dev/null @@ -1,78 +0,0 @@ -/* - * drivers/x86/bochs_vbe/suspend.c - * - * Copyright (c) 2024 Omar Berrow - */ - -#include - -#include - -#include "io.h" - -/* - * We need to save: - * xres - * yres - * virtual width - * virtual height - * bpp - * the bank - * x offset - * y offset - * the value of the enable register -*/ - -struct { - uint32_t xres, yres; - uint32_t virt_width, virt_height; - uint32_t bpp; - uint32_t bank; - uint32_t xoffset, yoffset; - uint32_t enable; -} saved_vals; - -void on_suspend() -{ - saved_vals.xres = ReadRegister(INDEX_XRES); - saved_vals.yres = ReadRegister(INDEX_YRES); - saved_vals.virt_height = ReadRegister(INDEX_VIRT_HEIGHT); - saved_vals.virt_width = ReadRegister(INDEX_VIRT_WIDTH); - saved_vals.bpp = ReadRegister(INDEX_BPP); - saved_vals.bank = ReadRegister(INDEX_BANK); - saved_vals.xoffset = ReadRegister(INDEX_X_OFFSET); - saved_vals.yoffset = ReadRegister(INDEX_Y_OFFSET); - saved_vals.enable = ReadRegister(INDEX_ENABLE); -} - -#define VGA_ATT_W 0x3C0 -#define VGA_MIS_W 0x3C2 -#define VGA_IS1_RC 0x3DA -#define VGA_MIS_COLOR 0x01 - -void on_wake() -{ - // for (volatile bool b = true; b; ) - // asm volatile ("" : :"r"(b) :"memory"); - - // NOTE: PCI stuff is already restored by now. - - outb(VGA_MIS_W, VGA_MIS_COLOR); - inb(VGA_IS1_RC); - outb(VGA_ATT_W, 0); - - WriteRegister(INDEX_ENABLE, 0); - WriteRegister(INDEX_BPP, saved_vals.bpp); - WriteRegister(INDEX_XRES, saved_vals.xres); - WriteRegister(INDEX_YRES, saved_vals.yres); - WriteRegister(INDEX_BANK, saved_vals.bank); - WriteRegister(INDEX_VIRT_WIDTH, saved_vals.virt_width); - WriteRegister(INDEX_VIRT_HEIGHT, saved_vals.virt_height); - WriteRegister(INDEX_X_OFFSET, saved_vals.xoffset); - WriteRegister(INDEX_Y_OFFSET, saved_vals.yoffset); - WriteRegister(INDEX_ENABLE, saved_vals.enable | BIT(7)); - - outb(VGA_MIS_W, VGA_MIS_COLOR); - inb(VGA_IS1_RC); - outb(VGA_ATT_W, 0x20); -} diff --git a/src/drivers/x86/i8042/CMakeLists.txt b/src/drivers/x86/i8042/CMakeLists.txt deleted file mode 100644 index 1479de4e..00000000 --- a/src/drivers/x86/i8042/CMakeLists.txt +++ /dev/null @@ -1,33 +0,0 @@ -# drivers/x86/i8042/CMakeLists.txt -# -# Copyright (c) 2025 Omar Berrow - -add_executable(i8042 "main.c" "ctlr.c") - -target_compile_options(i8042 - PRIVATE $<$:${TARGET_DRIVER_COMPILE_OPTIONS_C}> - PRIVATE $<$:-ffreestanding> - PRIVATE $<$:-Wall> - PRIVATE $<$:-Wextra> - PRIVATE $<$:-fstack-protector-all> - PRIVATE $<$:-fno-builtin-memset> - PRIVATE $<$:-fvisibility=hidden> - PRIVATE $<$:-fPIC> -) - -set_property(TARGET i8042 PROPERTY link_depends ${DRIVER_LINKER_SCRIPT}) - -target_include_directories(i8042 - PRIVATE "${CMAKE_SOURCE_DIR}/src/oboskrnl" - PRIVATE "${CMAKE_SOURCE_DIR}/src/drivers" - PRIVATE ${OBOSKRNL_EXTERNAL_INCLUDES}) - -target_link_options(i8042 - PRIVATE "-nostdlib" - PRIVATE "-fPIC" - PRIVATE "-Wl,-shared" -# PRIVATE "-Wl,--allow-shlib-undefined" - PRIVATE "-T" PRIVATE ${DRIVER_LINKER_SCRIPT} - PRIVATE ${TARGET_DRIVER_LINKER_OPTIONS} -) -target_compile_definitions(i8042 PRIVATE OBOS_DRIVER=1) diff --git a/src/drivers/x86/i8042/ctlr.c b/src/drivers/x86/i8042/ctlr.c deleted file mode 100644 index b4de7f2a..00000000 --- a/src/drivers/x86/i8042/ctlr.c +++ /dev/null @@ -1,257 +0,0 @@ -/* - * drivers/x86/i8042/ctlr.c - * - * Copyright (c) 2025 Omar Berrow -*/ - -#include -#include -#include - -#include - -#include -#include - -#include -#include - -#include - -#include "ps2_structs.h" - -struct ps2_ctlr_data PS2_CtlrData; - -static void poll_status(uint8_t mask, uint8_t expected) -{ - while ((inb(PS2_CMD_STATUS) & mask) != expected) - pause(); -} -static uint8_t read_ctlr_status() -{ - irql oldIrql = Core_RaiseIrql(IRQL_PS2); - poll_status(PS2_INPUT_BUFFER_FULL, 0); - outb(PS2_CMD_STATUS, PS2_CTLR_READ_RAM_CMD(0)); - poll_status(PS2_OUTPUT_BUFFER_FULL, PS2_OUTPUT_BUFFER_FULL); - uint8_t res = inb(PS2_DATA); - Core_LowerIrql(oldIrql); - return res; -} -static void write_ctlr_status(uint8_t ctlr_config) -{ - irql oldIrql = Core_RaiseIrql(IRQL_PS2); - poll_status(PS2_INPUT_BUFFER_FULL, 0); - outb(PS2_CMD_STATUS, PS2_CTLR_WRITE_RAM_CMD(0)); - poll_status(PS2_INPUT_BUFFER_FULL, 0); - outb(PS2_DATA, ctlr_config); - Core_LowerIrql(oldIrql); -} - -void ps2_irq_move_callback(struct irq* i, struct irq_vector* from, struct irq_vector* to, void* userdata) -{ - OBOS_UNUSED(i); - OBOS_UNUSED(from); - ps2_port* port = (ps2_port*)userdata; - obos_status status = Arch_IOAPICMapIRQToVector(port->gsi, 0, PolarityActiveHigh, TriggerModeEdgeSensitive); - if (obos_is_error(status)) - OBOS_Panic(OBOS_PANIC_DRIVER_FAILURE, "IOAPIC: Could not unmap GSI %d. Status: %d\n", port->gsi, status); - status = Arch_IOAPICMapIRQToVector(port->gsi, to->id+0x20, PolarityActiveHigh, TriggerModeEdgeSensitive); - if (obos_is_error(status)) - OBOS_Panic(OBOS_PANIC_DRIVER_FAILURE, "IOAPIC: Could not map GSI %d. Status: %d\n", port->gsi, status); - Arch_IOAPICMaskIRQ(port->gsi, false); -} -void ps2_irq_handler(struct irq* i, interrupt_frame* frame, void* userdata, irql oldIrql) -{ - OBOS_UNUSED(i && frame && oldIrql); - ps2_port* port = userdata; - if (port->suppress_irqs || !port->data_ready) - return; - while (inb(PS2_CMD_STATUS) & PS2_OUTPUT_BUFFER_FULL) - { - uint8_t read = inb(PS2_DATA); - if (port->data_ready) - port->data_ready(port, read); - else - OBOS_Error("i8042: Possibly read command response! inb(PS2_DATA)=0x%02x\n", read); - } -} - -obos_status PS2_InitializeController() -{ - irql oldIrql = Core_RaiseIrql(IRQL_PS2); - - PS2_CtlrData.lock = Core_SpinlockCreate(); - - PS2_FlushInput(); - - // Write the controller configuration byte. - uint8_t ctlr_config = read_ctlr_status(); - ctlr_config &= ~(PS2_CTLR_CONFIG_PORT_ONE_IRQ|PS2_CTLR_CONFIG_PORT_TWO_IRQ|PS2_CTLR_CONFIG_PORT_ONE_TRANSLATION); - write_ctlr_status(ctlr_config); - - // Run the PS/2 Controller self test. - poll_status(PS2_INPUT_BUFFER_FULL, 0); - outb(PS2_CMD_STATUS, PS2_CTLR_TEST); - poll_status(PS2_OUTPUT_BUFFER_FULL, PS2_OUTPUT_BUFFER_FULL); - uint8_t test_result = inb(PS2_DATA); - if (test_result != 0x55 /* test passed */) - { - Core_LowerIrql(oldIrql); - return OBOS_STATUS_INTERNAL_ERROR; - } - - // Rewrite the configuration, in case. - write_ctlr_status(ctlr_config); - - // Determine if the controller is dual-channel. - - poll_status(PS2_INPUT_BUFFER_FULL, 0); - outb(PS2_CMD_STATUS, PS2_CTLR_ENABLE_PORT_TWO); - ctlr_config = read_ctlr_status(); - PS2_CtlrData.dual_channel = ~ctlr_config & PS2_CTLR_CONFIG_PORT_TWO_CLOCK; - if (PS2_CtlrData.dual_channel) - { - poll_status(PS2_INPUT_BUFFER_FULL, 0); - outb(PS2_CMD_STATUS, PS2_CTLR_DISABLE_PORT_TWO); - } - - // Run device tests. - poll_status(PS2_INPUT_BUFFER_FULL, 0); - outb(PS2_CMD_STATUS, PS2_CTLR_TEST_PORT_ONE); - poll_status(PS2_OUTPUT_BUFFER_FULL, PS2_OUTPUT_BUFFER_FULL); - test_result = inb(PS2_DATA); - if (!test_result) - PS2_CtlrData.ports[0].works = true; - if (!PS2_CtlrData.dual_channel) - PS2_CtlrData.ports[1].works = false; - if (PS2_CtlrData.dual_channel) - { - poll_status(PS2_INPUT_BUFFER_FULL, 0); - outb(PS2_CMD_STATUS, PS2_CTLR_TEST_PORT_TWO); - poll_status(PS2_OUTPUT_BUFFER_FULL, PS2_OUTPUT_BUFFER_FULL); - test_result = inb(PS2_DATA); - if (!test_result) - PS2_CtlrData.ports[1].works = true; - } - - if (!PS2_CtlrData.ports[0].works && !PS2_CtlrData.ports[1].works) - { - OBOS_Log("Found %d PS/2 ports, but all self-tests failed. Aborting.\n", PS2_CtlrData.dual_channel ? 2 : 1); - Core_LowerIrql(oldIrql); - return OBOS_STATUS_NOT_FOUND; // No devices. - } - - // Enable devices. - obos_status status = OBOS_STATUS_SUCCESS; - if (obos_is_error(status = PS2_EnableChannel(false, true))) - OBOS_Warning("PS2_EnableChannel(channel_one, true): %d\n", status); - if (obos_is_error(status = PS2_EnableChannel(true, true))) - OBOS_Warning("PS2_EnableChannel(channel_two, true): %d\n", status); - - - Core_LowerIrql(oldIrql); - - PS2_CtlrData.ports[1].second = true; - // Initialize the structs. - for (int i = 0; i < 2; i++) - { - if (!PS2_CtlrData.ports[i].works) - continue; - PS2_CtlrData.ports[i].gsi = (i==0) ? 1 : 12 /* TODO: Use ACPI namespace? */; - PS2_CtlrData.ports[i].irq = Core_IrqObjectAllocate(nullptr); - PS2_CtlrData.ports[i].irq->irqMoveCallbackUserdata = PS2_CtlrData.ports+i; - PS2_CtlrData.ports[i].irq->irqCheckerUserdata = PS2_CtlrData.ports+i; - PS2_CtlrData.ports[i].irq->handlerUserdata = PS2_CtlrData.ports+i; - PS2_CtlrData.ports[i].irq->moveCallback = ps2_irq_move_callback; - PS2_CtlrData.ports[i].irq->handler = ps2_irq_handler; - PS2_CtlrData.ports[i].type = PS2_DEV_TYPE_UNKNOWN; - PS2_CtlrData.ports[i].id[0] = 'p'; - PS2_CtlrData.ports[i].id[1] = 's'; - PS2_CtlrData.ports[i].id[2] = '2'; - PS2_CtlrData.ports[i].id[3] = PS2_CtlrData.ports[i].type; - PS2_CtlrData.ports[i].padding = 0; - PS2_CtlrData.ports[i].magic = PS2_PORT_MAGIC; - Core_IrqObjectInitializeIRQL(PS2_CtlrData.ports[i].irq, IRQL_PS2, false, true); - Arch_IOAPICMapIRQToVector(PS2_CtlrData.ports[i].gsi, PS2_CtlrData.ports[i].irq->vector->id+0x20, PolarityActiveHigh, TriggerModeEdgeSensitive); - Arch_IOAPICMaskIRQ(PS2_CtlrData.ports[i].gsi, false); - } - - // Enable port IRQs - ctlr_config = read_ctlr_status(); - if (PS2_CtlrData.ports[0].works) - ctlr_config |= PS2_CTLR_CONFIG_PORT_ONE_IRQ; - if (PS2_CtlrData.ports[1].works) - ctlr_config |= PS2_CTLR_CONFIG_PORT_TWO_IRQ; - ctlr_config &= ~(BIT(4)|BIT(5)|BIT(6)); - write_ctlr_status(ctlr_config); - - return OBOS_STATUS_SUCCESS; -} - -void PS2_DeviceWrite(bool port_two, uint8_t val) -{ - irql oldIrql = Core_SpinlockAcquireExplicit(&PS2_CtlrData.lock, IRQL_PS2, false); - poll_status(PS2_INPUT_BUFFER_FULL, 0); - if (port_two) - outb(PS2_CMD_STATUS, PS2_CTLR_WRITE_PORT_TWO); - poll_status(PS2_INPUT_BUFFER_FULL, 0); - outb(PS2_DATA, val); - Core_SpinlockRelease(&PS2_CtlrData.lock, oldIrql); -} -uint8_t PS2_DeviceRead(uint32_t spin_timeout, obos_status* status) -{ - size_t i = 0; - uint8_t last_status = 0; - do { - i++; - last_status = inb(PS2_CMD_STATUS); - } while(i < spin_timeout && !(last_status & PS2_OUTPUT_BUFFER_FULL)); - if (status) *status = OBOS_STATUS_SUCCESS; - if (~last_status & PS2_OUTPUT_BUFFER_FULL) - { - if (status) *status = OBOS_STATUS_TIMED_OUT; - return 0xff; - } - return inb(PS2_DATA); -} - -obos_status PS2_EnableChannel(bool channel_two, bool status) -{ - if (channel_two && !PS2_CtlrData.dual_channel) - return OBOS_STATUS_INVALID_ARGUMENT; - status = !!status; - poll_status(PS2_INPUT_BUFFER_FULL, 0); - if (!channel_two) - outb(PS2_CMD_STATUS, status ? PS2_CTLR_ENABLE_PORT_ONE : PS2_CTLR_DISABLE_PORT_ONE); - else - outb(PS2_CMD_STATUS, status ? PS2_CTLR_ENABLE_PORT_TWO : PS2_CTLR_DISABLE_PORT_TWO); - return OBOS_STATUS_SUCCESS; -} - -obos_status PS2_MaskChannelIRQs(bool channel_two, bool mask) -{ - if (channel_two && !PS2_CtlrData.dual_channel) - return OBOS_STATUS_INVALID_ARGUMENT; - mask = !!mask; - uint8_t ctlr_config = read_ctlr_status(); - uint8_t bitmask = channel_two ? PS2_CTLR_CONFIG_PORT_TWO_IRQ : PS2_CTLR_CONFIG_PORT_ONE_IRQ; - if (mask) - ctlr_config &= ~bitmask; - else - ctlr_config |= bitmask; - write_ctlr_status(ctlr_config); - return OBOS_STATUS_SUCCESS; -} -obos_status PS2_FlushInput() -{ - while (inb(PS2_CMD_STATUS) & PS2_INPUT_BUFFER_FULL) - inb(PS2_DATA); - return OBOS_STATUS_SUCCESS; -} - -ps2_port* PS2_GetPort(bool channel_two) -{ - if (!PS2_CtlrData.ports[channel_two ? 1 : 0].works) - return nullptr; - return &PS2_CtlrData.ports[channel_two ? 1 : 0]; -} \ No newline at end of file diff --git a/src/drivers/x86/i8042/main.c b/src/drivers/x86/i8042/main.c deleted file mode 100644 index 0d24b68c..00000000 --- a/src/drivers/x86/i8042/main.c +++ /dev/null @@ -1,87 +0,0 @@ -/* - * drivers/x86/i8042/main.c - * - * Copyright (c) 2025 Omar Berrow -*/ - -#include -#include -#include -#include - -#include - -#include -#include - -#include - -#include -#include - -#include - -#include -#include - -#include - -#include -#include - -#include "ps2_structs.h" - -#include -#include - -OBOS_WEAK void cleanup() -{ - -} - -__attribute__((section(OBOS_DRIVER_HEADER_SECTION))) driver_header drv_hdr = { - .magic = OBOS_DRIVER_MAGIC, - .flags = DRIVER_HEADER_HAS_VERSION_FIELD|DRIVER_HEADER_FLAGS_DETECT_VIA_ACPI, - .acpiId = { - .nPnpIds = 3, - .pnpIds = { - "PNP0303", - "PNP0F13", - "PNP0F03", - } - }, - .ftable = { - .driver_cleanup_callback = cleanup - }, - .driverName = "PS/2 Controller Driver", - .version = 1, - .uacpi_init_level_required = UACPI_INIT_LEVEL_NAMESPACE_LOADED -}; - -driver_id* this_driver; - -// static bool found_ps2_device; - -// static uacpi_iteration_decision match ( -// void *user, uacpi_namespace_node *node, uacpi_u32 node_depth -// ) -// { -// OBOS_UNUSED(user && node && node_depth); -// found_ps2_device = true; -// return UACPI_ITERATION_DECISION_BREAK; -// } - -OBOS_PAGEABLE_FUNCTION driver_init_status OBOS_DriverEntry(driver_id* this) -{ - this_driver = this; - - // uacpi_find_devices_at(uacpi_namespace_root(), pnp_ids, match, nullptr); - // if (!found_ps2_device) - // return (driver_init_status){.status=OBOS_STATUS_NOT_FOUND,.fatal=true,.context="Could not find a PS/2 Controller."}; - - obos_status status = PS2_InitializeController(); - if (obos_is_error(status)) - return (driver_init_status){.status=status,.fatal=true,.context="Could not initialize the PS/2 Controller."}; - - return (driver_init_status){.status=OBOS_STATUS_SUCCESS,.fatal=false,.context=nullptr}; -} diff --git a/src/drivers/x86/i8042/ps2_irql.h b/src/drivers/x86/i8042/ps2_irql.h deleted file mode 100644 index 52ff9b56..00000000 --- a/src/drivers/x86/i8042/ps2_irql.h +++ /dev/null @@ -1,3 +0,0 @@ -#ifndef IRQL_PS2 -# define IRQL_PS2 3 -#endif \ No newline at end of file diff --git a/src/drivers/x86/i8042/ps2_structs.h b/src/drivers/x86/i8042/ps2_structs.h deleted file mode 100644 index 2ea94beb..00000000 --- a/src/drivers/x86/i8042/ps2_structs.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * drivers/x86/uart/ps2_structs.h - * - * Copyright (c) 2025 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include - -#include -#include - -#define PS2_DATA 0x60 -#define PS2_CMD_STATUS 0x64 - -enum { - PS2_OUTPUT_BUFFER_FULL = BIT(0), - PS2_INPUT_BUFFER_FULL = BIT(1), - PS2_SYSTEM_FLAG = BIT(2), - // "0 = data written to input buffer is data for PS/2 device, 1 = data written to input buffer is data for PS/2 controller command" - PS2_CMD_DATA = BIT(3), - PS2_TIMEOUT = BIT(6), - PS2_PARITY_ERROR = BIT(7), -}; - -#define PS2_CTLR_READ_RAM_CMD(n) (0x20+((n)&0x1f)) -#define PS2_CTLR_WRITE_RAM_CMD(n) (0x60+((n)&0x1f)) -#define PS2_CTLR_DISABLE_PORT_TWO 0xA7 -#define PS2_CTLR_ENABLE_PORT_TWO 0xA8 -#define PS2_CTLR_TEST_PORT_TWO 0xA9 -#define PS2_CTLR_TEST 0xAA -#define PS2_CTLR_TEST_PORT_ONE 0xAB -#define PS2_CTLR_DUMP_RAM 0xAC -#define PS2_CTLR_DISABLE_PORT_ONE 0xAD -#define PS2_CTLR_ENABLE_PORT_ONE 0xAE -#define PS2_CTLR_READ_CTLR_OUT_BUFFER 0xD0 -#define PS2_CTLR_WRITE_CTLR_OUT_BUFFER 0xD1 -#define PS2_CTLR_WRITE_PORT_TWO 0xD4 - -enum { - PS2_CTLR_CONFIG_PORT_ONE_IRQ = BIT(0), - PS2_CTLR_CONFIG_PORT_TWO_IRQ = BIT(1), - PS2_CTLR_CONFIG_SYSTEM_FLAG = BIT(2), - PS2_CTLR_CONFIG_PORT_ONE_CLOCK = BIT(4), - PS2_CTLR_CONFIG_PORT_TWO_CLOCK = BIT(5), - PS2_CTLR_CONFIG_PORT_ONE_TRANSLATION = BIT(6), -}; - -#include - -extern struct ps2_ctlr_data { - bool dual_channel : 1; - ps2_port ports[2]; - spinlock lock; -} PS2_CtlrData; - -obos_status PS2_InitializeController(); \ No newline at end of file diff --git a/src/drivers/x86/uart/CMakeLists.txt b/src/drivers/x86/uart/CMakeLists.txt deleted file mode 100644 index 2543e9ab..00000000 --- a/src/drivers/x86/uart/CMakeLists.txt +++ /dev/null @@ -1,32 +0,0 @@ -# drivers/x86/uart/CMakeLists.txt -# -# Copyright (c) 2024 Omar Berrow - -add_executable(uart "main.c" "serial_port.c") - -target_compile_options(uart - PRIVATE $<$:${TARGET_DRIVER_COMPILE_OPTIONS_C}> - PRIVATE $<$:-ffreestanding> - PRIVATE $<$:-Wall> - PRIVATE $<$:-Wextra> - PRIVATE $<$:-fstack-protector-all> - PRIVATE $<$:-fno-builtin-memset> - PRIVATE $<$:-fvisibility=hidden> - PRIVATE $<$:-fPIC> -) - -set_property(TARGET uart PROPERTY link_depends ${DRIVER_LINKER_SCRIPT}) - -target_include_directories(uart - PRIVATE "${CMAKE_SOURCE_DIR}/src/oboskrnl" - PRIVATE ${OBOSKRNL_EXTERNAL_INCLUDES}) - -target_link_options(uart - PRIVATE "-nostdlib" - PRIVATE "-fPIC" - PRIVATE "-Wl,-shared" -# PRIVATE "-Wl,--allow-shlib-undefined" - PRIVATE "-T" PRIVATE ${DRIVER_LINKER_SCRIPT} - PRIVATE ${TARGET_DRIVER_LINKER_OPTIONS} -) -target_compile_definitions(uart PRIVATE OBOS_DRIVER=1) diff --git a/src/drivers/x86/uart/main.c b/src/drivers/x86/uart/main.c deleted file mode 100644 index b65ee798..00000000 --- a/src/drivers/x86/uart/main.c +++ /dev/null @@ -1,398 +0,0 @@ -/* - * drivers/x86/uart/main.c - * - * Copyright (c) 2024-2025 Omar Berrow -*/ - -#include -#include -#include -#include - -#include - -#include -#include - -#include - -#include -#include - -#include - -#include -#include - -#include - -#include - -#include - -#include -#include -#include - -#include "serial_port.h" - -#include -#include - -driver_id* this_driver; -serial_port* serialPorts = nullptr; -size_t nSerialPorts = 0; - -void cleanup() -{ - for (size_t i = 0; i < nSerialPorts; i++) - { - Free(OBOS_KernelAllocator, serialPorts[i].user_name, strlen(serialPorts[i].user_name)); - free_buffer(&serialPorts[i].in_buffer); - free_buffer(&serialPorts[i].out_buffer); - } - Free(OBOS_KernelAllocator, serialPorts, sizeof(*serialPorts)*nSerialPorts); -} -obos_status get_blk_size(dev_desc ign, size_t* sz) -{ - OBOS_UNUSED(ign); - if (!sz) - return OBOS_STATUS_INVALID_ARGUMENT; - *sz = 1; - return OBOS_STATUS_SUCCESS; -} -obos_status get_max_blk_count(dev_desc ign1, size_t* ign2) -{ - OBOS_UNUSED(ign1); - OBOS_UNUSED(ign2); - return OBOS_STATUS_INVALID_OPERATION; -} -obos_status query_user_readable_name(dev_desc what, const char** name) -{ - if (!what || !name) - return OBOS_STATUS_INVALID_ARGUMENT; - serial_port* port = (serial_port*)what; - if (port->user_name) - *name = port->user_name; - else - { - size_t sz = snprintf(nullptr, 0, "COM%d", port->com_port); - char* buf = ZeroAllocate(OBOS_KernelAllocator, 1, sz+1, nullptr); - buf[sz] = 0; - snprintf(buf, sz+1, "COM%d", port->com_port); - port->user_name = buf; - *name = port->user_name; - } - return OBOS_STATUS_SUCCESS; -} -obos_status foreach_device(iterate_decision(*cb)(dev_desc desc, size_t blkSize, size_t blkCount, void* udata), void* udata) -{ - for (size_t i = 0; i < nSerialPorts; i++) - { - switch (cb((dev_desc)&serialPorts[i], 1, -1, udata)) - { - case ITERATE_DECISION_CONTINUE: - continue; - case ITERATE_DECISION_STOP: - return OBOS_STATUS_SUCCESS; - } - } - return OBOS_STATUS_SUCCESS; -} -obos_status read_sync(dev_desc desc, void* buf, size_t blkCount, size_t blkOffset, size_t* nBlkRead) -{ - OBOS_UNUSED(blkOffset); - serial_port* port = (serial_port*)desc; - if (!port || !buf || !blkCount) - return OBOS_STATUS_INVALID_ARGUMENT; - size_t i = 0; - while(((volatile serial_port*)port)->in_buffer.szBuf < blkCount) - OBOSS_SpinlockHint(); - irql oldIrql = Core_SpinlockAcquireExplicit(&port->in_buffer.lock, IRQL_COM_IRQ, false); - const size_t initialBufSize = port->in_buffer.szBuf; - for (; i < blkCount && i < initialBufSize; i++) - ((char*)buf)[i] = pop_from_buffer(&port->in_buffer); - Core_SpinlockRelease(&port->in_buffer.lock, oldIrql); - if (nBlkRead) - *nBlkRead = i; - return OBOS_STATUS_SUCCESS; -} -obos_status write_sync(dev_desc desc, const void* buf, size_t blkCount, size_t blkOffset, size_t* nBlkWritten) -{ - OBOS_UNUSED(blkOffset); - serial_port* port = (serial_port*)desc; - if (!port || !buf || !blkCount) - return OBOS_STATUS_INVALID_ARGUMENT; - irql oldIrql = Core_SpinlockAcquireExplicit(&port->out_buffer.lock, IRQL_COM_IRQ, false); - for (size_t i = 0; i < blkCount; i++) - { - size_t spin = 0; - const size_t threshold = 100000; - while(spin++ < threshold && !(inb(port->port_base + LINE_STATUS) & BIT(5))) - pause(); - if (spin >= threshold) - { - // buffer the writes after it hangs for too long. - append_to_buffer_str_len(&port->out_buffer, &((char*)buf)[i], blkCount); - break; - } - outb(port->port_base + IO_BUFFER, ((char*)buf)[i]); - } - if (nBlkWritten) - *nBlkWritten = blkCount; - Core_SpinlockRelease(&port->out_buffer.lock, oldIrql); - return OBOS_STATUS_SUCCESS; -} - -void on_wake() -{ - for (size_t i = 0; i < nSerialPorts; i++) - { - serial_port *port = serialPorts + i; - if (!port->opened) - continue; - open_serial_connection(port, port->baudRate, port->dataBits, port->stopbits, port->parityBit); - } -} - -void on_suspend() -{} - -obos_status ioctl_argp_size(uint32_t request, size_t* out); - -obos_status ioctl(dev_desc what, uint32_t request, void* argp); -__attribute__((section(OBOS_DRIVER_HEADER_SECTION))) driver_header drv_hdr = { - .magic = OBOS_DRIVER_MAGIC, - .flags = DRIVER_HEADER_PIPE_STYLE_DEVICE|DRIVER_HEADER_HAS_STANDARD_INTERFACES|DRIVER_HEADER_FLAGS_DETECT_VIA_ACPI|DRIVER_HEADER_HAS_VERSION_FIELD, - .acpiId.nPnpIds = 1, - .acpiId.pnpIds = { - "PNP0501", // 16550A-compatible COM port - }, - .ftable = { - .driver_cleanup_callback = cleanup, - .ioctl = ioctl, - .ioctl_argp_size = ioctl_argp_size, - .get_blk_size = get_blk_size, - .get_max_blk_count = get_max_blk_count, - .query_user_readable_name = query_user_readable_name, - .foreach_device = foreach_device, - .read_sync = read_sync, - .write_sync = write_sync, - .on_suspend = on_suspend, - .on_wake = on_wake, - }, - .driverName = "COM Driver", - .version = 1, - .uacpi_init_level_required = UACPI_INIT_LEVEL_NAMESPACE_INITIALIZED -}; - -uacpi_iteration_decision resource_iterator(void *user, uacpi_resource *resource) -{ - serial_port* curr = (serial_port*)user; - switch (resource->type) - { - case UACPI_RESOURCE_TYPE_IRQ: - { - curr->gsi = resource->irq.irqs[0]; - break; - } - case UACPI_RESOURCE_TYPE_IO: - { - curr->port_base = resource->io.minimum; - curr->port_top = curr->port_base + resource->io.length; - break; - } - case UACPI_RESOURCE_TYPE_FIXED_IO: - { - curr->port_base = resource->fixed_io.address; - curr->port_top = curr->port_base + resource->fixed_io.length; - break; - } - default: break; - } - return UACPI_ITERATION_DECISION_CONTINUE; -} -static uacpi_iteration_decision match_uart(void *user, uacpi_namespace_node *node, uint32_t max_depth) -{ - OBOS_UNUSED(user); - OBOS_UNUSED(max_depth); - - uacpi_resources* resources = nullptr; - uacpi_status ret = uacpi_get_current_resources(node, &resources); - if (uacpi_unlikely_error(ret)) - { - OBOS_Error("Could not retrieve resources! Status: %s\n", uacpi_status_to_string(ret)); - return UACPI_ITERATION_DECISION_NEXT_PEER; - } - size_t old_sz = nSerialPorts*sizeof(serial_port); - size_t new_sz = (++nSerialPorts)*sizeof(serial_port); - serialPorts = Reallocate(OBOS_KernelAllocator, serialPorts, new_sz, old_sz, nullptr); - memzero(&serialPorts[nSerialPorts - 1], sizeof(*serialPorts)); - serialPorts[nSerialPorts - 1].com_port = nSerialPorts; - uacpi_for_each_resource(resources, resource_iterator, &serialPorts[nSerialPorts - 1]); - uacpi_free_resources(resources); - - // OBOS_Debug("COM%d is from IO address 0x%x - 0x%x, and is on GSI %d.\n", - // serialPorts[nSerialPorts-1].com_port, - // serialPorts[nSerialPorts-1].port_base, - // serialPorts[nSerialPorts-1].port_top, - // serialPorts[nSerialPorts-1].gsi - // ); - - return UACPI_ITERATION_DECISION_CONTINUE; -} - -obos_status ioctl(dev_desc what, uint32_t request, void* argp) -{ - OBOS_UNUSED(what); - obos_status status = OBOS_STATUS_INVALID_IOCTL; - switch (request) - { - case IOCTL_OPEN_SERIAL_CONNECTION: - { - OBOS_ALIGNAS(sizeof(uintptr_t)) struct { - uint32_t baudRate; - uint32_t dataBits; - uint32_t stopBits; - uint32_t parityBit; - } *args = argp; - - serial_port* port = (serial_port*)what; - status = open_serial_connection( - port, - args->baudRate, - args->dataBits, - args->stopBits, - args->parityBit); - break; - } - default: break; - } - return status; -} - -OBOS_ALIGNAS(sizeof(uintptr_t)) struct open_serial_con { - uint32_t baudRate; - uint32_t dataBits; - uint32_t stopBits; - uint32_t parityBit; -} args; - -obos_status ioctl_argp_size(uint32_t request, size_t* out) -{ - obos_status status = OBOS_STATUS_INVALID_IOCTL; - switch (request) - { - case IOCTL_OPEN_SERIAL_CONNECTION: - { - status = OBOS_STATUS_SUCCESS; - *out = sizeof(struct open_serial_con); - break; - } - default: break; - } - return status; -} - -static void set_data_ready_cb(void* tty, void(*cb)(void* tty, const void* buf, size_t nBytesReady)) -{ - struct tty* t = tty; - serial_port* port = t->interface.userdata; - port->data_ready = cb; - port->tty = t; -} -static obos_status write(void* tty, const char* buf, size_t szBuf) -{ - struct tty* t = tty; - serial_port* port = t->interface.userdata; - write_sync((dev_desc)port, buf, szBuf, 0, nullptr); - return OBOS_STATUS_SUCCESS; -} - -static void on_termios_set(void* tty, void* new_termiosp, void* old_termios) -{ - OBOS_UNUSED(old_termios); - struct tty* t = tty; - serial_port* port = t->interface.userdata; - - struct termios* new_termios = new_termiosp; - - // ibaud is ignored. - - uint32_t baudRate = speed_t_to_baud_rate(new_termios->cflag & CBAUD); - data_bits dataBits = SEVEN_DATABITS; - stop_bits stopbits = ONE_STOPBIT; - parity_bit parityBit = PARITYBIT_NONE; - - switch (new_termios->cflag & CSIZE) { - case CS5: dataBits = FIVE_DATABITS; break; - case CS6: dataBits = SIX_DATABITS; break; - case CS7: dataBits = SEVEN_DATABITS; break; - case CS8: dataBits = EIGHT_DATABITS; break; - default: return; - } - - if (new_termios->cflag & PARENB) - parityBit = !!(new_termios->cflag & PARODD) ? PARITYBIT_ODD : PARITYBIT_EVEN; - - if (new_termios->cflag & CSTOPB) - stopbits = TWO_STOPBITS; - - open_serial_connection(port, baudRate, dataBits, stopbits, parityBit); -} - -OBOS_PAGEABLE_FUNCTION driver_init_status OBOS_DriverEntry(driver_id* this) -{ - this_driver = this; - // Find the keyboard driver. - uacpi_find_devices("PNP0500", match_uart, nullptr); - uacpi_find_devices("PNP0501", match_uart, nullptr); - // For each COM port, make an IRQ object. - for (size_t i = 0; i < nSerialPorts; i++) - { - serial_port* port = &serialPorts[i]; - obos_status status = OBOS_STATUS_SUCCESS; - port->irq_obj = Core_IrqObjectAllocate(&status); - if (obos_is_error(status)) - { - OBOS_Warning("Could not allocate irq object for COM%d. Status: %d.\n", port->com_port, status); - continue; - } - port->irq_obj->handler = com_irq_handler; - port->irq_obj->irqChecker = com_check_irq_callback; - port->irq_obj->moveCallback = com_irq_move_callback; - port->irq_obj->handlerUserdata = port; - port->irq_obj->irqCheckerUserdata = port; - port->irq_obj->irqMoveCallbackUserdata = port; - status = Core_IrqObjectInitializeIRQL(port->irq_obj, IRQL_COM_IRQ, true, true); - if (obos_is_error(status)) - { - OBOS_Warning("Could not initialize irq object for COM%d. Status: %d.\n", port->com_port, status); - continue; - } - - // Register the GSI. - status = Arch_IOAPICMapIRQToVector(port->gsi, port->irq_obj->vector->id+0x20, PolarityActiveHigh, TriggerModeEdgeSensitive); - if (obos_is_error(status)) - { - OBOS_Warning("Could not initialize GSI for COM%d. Status: %d.\n", port->com_port, status); - continue; - } - - vnode* vn = Drv_AllocateVNode(this_driver, (uintptr_t)port, 0, nullptr, VNODE_TYPE_CHR); - const char* dev_name = nullptr; - query_user_readable_name(vn->desc, &dev_name); - OBOS_Debug("%*s: Registering serial port at %s%c%s\n", uacpi_strnlen(this_driver->header.driverName, 64), this_driver->header.driverName, OBOS_DEV_PREFIX, OBOS_DEV_PREFIX[sizeof(OBOS_DEV_PREFIX)-1] == '/' ? 0 : '/', dev_name); - Drv_RegisterVNode(vn, dev_name); - tty_interface ttyi = { - .userdata = port, - .on_termios_set = on_termios_set, - .write = write, - .set_data_ready_cb = set_data_ready_cb, - .size.row=30, - .size.col=95, - }; - Vfs_RegisterTTY(&ttyi, nullptr, TTY_SERIAL); - } - return (driver_init_status){.status=OBOS_STATUS_SUCCESS,.fatal=false,.context=nullptr}; -} diff --git a/src/drivers/x86/uart/serial_port.c b/src/drivers/x86/uart/serial_port.c deleted file mode 100644 index b380732e..00000000 --- a/src/drivers/x86/uart/serial_port.c +++ /dev/null @@ -1,207 +0,0 @@ -/* - * drivers/x86/uart/serial_port.c - * - * Copyright (c) 2024-2025 Omar Berrow -*/ - -#include -#include -#include -#include -#include - -#include - -#include - -#include - -#include - -#include -#include - -#include - -#include - -#include - -#include "serial_port.h" - - -void append_to_buffer_char(buffer* buf, char what) -{ - size_t index = buf->szBuf++; - if (index >= buf->nAllocated) - { - buf->nAllocated += 4; // Reserve 4 more bytes. - buf->buf = Reallocate(OBOS_NonPagedPoolAllocator, buf->buf, buf->nAllocated*sizeof(*buf->buf), index*sizeof(*buf->buf), nullptr); - OBOS_ASSERT(buf->buf); - } - buf->buf[index+buf->offset] = what; -} -void append_to_buffer_str_len(buffer* buf, const char* what, size_t strlen) -{ - // Reserve enough bytes to prevent a bunch of allocations from being made. - if (buf->nAllocated < (buf->szBuf + strlen)) - { - size_t old_sz = buf->nAllocated; - buf->nAllocated += ((strlen + 3) & ~3); // Reserve strlen more bytes. - buf->buf = Reallocate(OBOS_NonPagedPoolAllocator, buf->buf, buf->nAllocated*sizeof(*buf->buf), old_sz*sizeof(*buf->buf), nullptr); - OBOS_ASSERT(buf->buf); - } - char ch = what[0]; - for (size_t i = 0; i < strlen; ch = what[++i]) - append_to_buffer_char(buf, ch); -} -void append_to_buffer_str(buffer* buf, const char* what) -{ - append_to_buffer_str_len(buf, what, strlen(what)); -} -char pop_from_buffer(buffer* buf) -{ - if (!buf->buf || !buf->szBuf) - return 0; - char ret = buf->buf[buf->offset]; - buf->szBuf--; - buf->offset++; - if ((buf->nAllocated - buf->szBuf) >= 4) - { - buf->nAllocated -= (buf->nAllocated - buf->szBuf); - OBOS_ASSERT(buf->nAllocated == buf->szBuf); - char* newbuf = ZeroAllocate(OBOS_NonPagedPoolAllocator, buf->nAllocated, sizeof(*buf->buf), nullptr); - memcpy(newbuf, buf->buf + buf->offset, buf->szBuf); - Free(OBOS_NonPagedPoolAllocator, buf->buf, buf->nAllocated + 4); - buf->buf = newbuf; - buf->offset = 0; - } - return ret; -} -void free_buffer(buffer* buf) -{ - Free(OBOS_NonPagedPoolAllocator, buf->buf, buf->nAllocated*sizeof(*buf->buf)); - buf->buf = nullptr; - buf->szBuf = buf->nAllocated = 0; -} -void flush_out_buffer(serial_port* port) -{ - while((inb(port->port_base + LINE_STATUS) & BIT(5)) && port->out_buffer.szBuf) - outb(port->port_base + IO_BUFFER, pop_from_buffer(&port->out_buffer)); -} - -const size_t irq_rate = 1; -obos_status open_serial_connection(serial_port* port, uint32_t baudRate, data_bits dataBits, stop_bits stopbits, parity_bit parityBit) -{ - if (!port) - return OBOS_STATUS_INVALID_ARGUMENT; - if (!baudRate) - { - // Disable FIFOs, clear FIFO buffers. - outb(port->port_base + FIFO_CTRL, 0b110); - outb(port->port_base + IRQ_ENABLE, 0); - port->opened = false; - return OBOS_STATUS_SUCCESS; - } - uint16_t divisor = 115200 / baudRate; - if (divisor == 0) - return OBOS_STATUS_INTERNAL_ERROR; - // NOTE: You still should allow initialization of the port, even if it was deduced to be faulty, - // as it seems as if disconnected serial ports fail in the same way. - irql oldIrql = Core_RaiseIrql(IRQL_COM_IRQ); - // Disable serial IRQs (temporarily) - outb(port->port_base + IRQ_ENABLE, 0); - outb(port->port_base + LINE_CTRL, 0x80 /* LINE_CTRL.DLAB */); - outb(port->port_base + DIVISOR_LOW_BYTE, divisor & 0xff); - outb(port->port_base + DIVISOR_HIGH_BYTE, divisor >> 8); - outb(port->port_base + LINE_CTRL, dataBits | stopbits | parityBit); - while (inb(port->port_base + LINE_STATUS) & BIT(0)) - inb(port->port_base + IO_BUFFER); - // Enter loop back mode. - outb(port->port_base + MODEM_CTRL, 0x1B /* RTS+Out1+Out2+Loop */); - outb(port->port_base + IO_BUFFER, 0xde); - if (inb(port->port_base + IO_BUFFER) != 0xde) - { - OBOS_Log("Port COM%d is faulty or disconnected.", port->com_port); - port->isFaulty = true; // bruh - Core_LowerIrql(oldIrql); - return OBOS_STATUS_INTERNAL_ERROR; - } - // Enter normal transmission mode. - port->isFaulty = false; - port->opened = true; - outb(port->port_base + FIFO_CTRL, 0x07 /* FIFO Enabled, IRQ when one byte is received, other flags */); - outb(port->port_base + MODEM_CTRL, 0xF /* DTR+RTS+OUT2+OUT1*/); - outb(port->port_base + IRQ_ENABLE, 1); - Arch_IOAPICMaskIRQ(port->gsi, false); - port->stopbits = stopbits; - port->baudRate = baudRate; - port->dataBits = dataBits; - port->parityBit = parityBit; - Core_LowerIrql(oldIrql); - return OBOS_STATUS_SUCCESS; -} - -extern __attribute__((section(OBOS_DRIVER_HEADER_SECTION))) driver_header drv_hdr; -void com_irq_handler(struct irq* i, interrupt_frame* frame, void* userdata, irql oldIrql_) -{ - OBOS_UNUSED(i); - OBOS_UNUSED(frame); - serial_port *port = userdata; - uint8_t lineStatusRegister = inb(port->port_base + LINE_STATUS); - if (lineStatusRegister & BIT(5)) - { - // Send all the data in the output buffer. - irql oldIrql = Core_SpinlockAcquireExplicit(&port->out_buffer.lock, IRQL_COM_IRQ, false); - flush_out_buffer(port); - Core_SpinlockRelease(&port->out_buffer.lock, oldIrql); - } - // Receive all the avaliable data. - bool received_break = false; - irql oldIrql = Core_SpinlockAcquireExplicit(&port->in_buffer.lock, IRQL_COM_IRQ, false); - while (inb(port->port_base + LINE_STATUS) & BIT(0)) - { - char ch = inb(port->port_base+IO_BUFFER); - append_to_buffer_char(&port->in_buffer, ch); - port->data_ready(port->tty, &ch, 1); - if (ch == '\x03') - received_break = true; - } - Core_SpinlockRelease(&port->in_buffer.lock, oldIrql); - bool should_break = received_break && (OBOS_GetOPTF("enable-kdbg") || Kdbg_CurrentConnection); - if (should_break && (!Kdbg_CurrentConnection || Kdbg_CurrentConnection->pipe == (dev_desc)port)) - { - oldIrql = Core_GetIrql(); - Core_LowerIrqlNoDPCDispatch(oldIrql_); - if (!Kdbg_CurrentConnection) - { - OBOS_Debug("UART: Received 0x03 break. Enabling kernel debugger.\n"); - static gdb_connection con = {}; - Kdbg_ConnectionInitialize(&con, &drv_hdr.ftable, (dev_desc)port); - Kdbg_CurrentConnection = &con; - Kdbg_InitializeHandlers(); - Kdbg_CurrentConnection->connection_active = true; - } - Kdbg_CallDebugExceptionHandler(frame, true); - oldIrql = Core_RaiseIrqlNoThread(oldIrql); - } -} -bool com_check_irq_callback(struct irq* i, void* userdata) -{ - OBOS_UNUSED(i); - serial_port* port = (serial_port*)userdata; - return inb(port->port_base + LINE_STATUS) != 0 && !port->isFaulty; -} -void com_irq_move_callback(struct irq* i, struct irq_vector* from, struct irq_vector* to, void* userdata) -{ - OBOS_UNUSED(i); - OBOS_UNUSED(from); - serial_port* port = (serial_port*)userdata; - obos_status status = Arch_IOAPICMapIRQToVector(port->gsi, 0, PolarityActiveHigh, TriggerModeEdgeSensitive); - if (obos_is_error(status)) - OBOS_Panic(OBOS_PANIC_DRIVER_FAILURE, "IOAPIC: Could not unmap GSI %d. Status: %d\n", port->gsi, status); - status = Arch_IOAPICMapIRQToVector(port->gsi, to->id+0x20, PolarityActiveHigh, TriggerModeEdgeSensitive); - if (obos_is_error(status)) - OBOS_Panic(OBOS_PANIC_DRIVER_FAILURE, "IOAPIC: Could not map GSI %d. Status: %d\n", port->gsi, status); - Arch_IOAPICMaskIRQ(port->gsi, false); -} diff --git a/src/drivers/x86/uart/serial_port.h b/src/drivers/x86/uart/serial_port.h deleted file mode 100644 index cf74ee0e..00000000 --- a/src/drivers/x86/uart/serial_port.h +++ /dev/null @@ -1,118 +0,0 @@ -/* - * drivers/x86/uart/serial_port.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include - -#include -#include - -#include - -typedef struct buffer -{ - char* buf; - size_t offset; - size_t szBuf; - size_t nAllocated; - spinlock lock; -} buffer; -void append_to_buffer_char(buffer* buf, char what); -void append_to_buffer_str(buffer* buf,const char* what); -void append_to_buffer_str_len(buffer* buf, const char* what, size_t strlen); -char pop_from_buffer(buffer* buf); // NOTE: Pops from beginning of buffer. -void free_buffer(buffer* buf); - -enum uart_registers -{ - IO_BUFFER = 0, - DIVISOR_LOW_BYTE = 0, // When FIFO_CTRL.DLAB = 1 - IRQ_ENABLE = 1, - DIVISOR_HIGH_BYTE = 1, // When FIFO_CTRL.DLAB = 1 - INTERRUPT_IDENTIFICATION = 2, - FIFO_CTRL = 2, - LINE_CTRL = 3, - MODEM_CTRL = 4, - LINE_STATUS = 5, - MODEM_STATUS = 6, - SCRATCH = 7, -}; -typedef enum parity_bit -{ - PARITYBIT_NONE, - PARITYBIT_ODD = 0b10000, - PARITYBIT_EVEN = 0b11000, - PARITYBIT_MARK = 0b10100, - PARITYBIT_SPACE = 0b11100, -} parity_bit; -typedef enum data_bits -{ - FIVE_DATABITS, - SIX_DATABITS, - SEVEN_DATABITS, - EIGHT_DATABITS, -} data_bits; -typedef enum stop_bits -{ - ONE_STOPBIT, - ONE_HALF_STOPBIT = 0b0100, - TWO_STOPBITS = 0b0100, -} stop_bits; - -typedef struct serial_port -{ - uint8_t com_port; - char* user_name; - - uint16_t port_base; - uint16_t port_top; - - uint8_t gsi; - irq* irq_obj; - - buffer in_buffer; - buffer out_buffer; - - bool isFaulty; - - void(*data_ready)(void* tty, const void* buf, size_t nBytesReady); - void* tty; - - dpc com_dpc; - - uint32_t baudRate; - data_bits dataBits; - stop_bits stopbits; - parity_bit parityBit; - - bool opened; -} serial_port; -void flush_out_buffer(serial_port* port); - -#define IRQL_COM_IRQ (IRQL_DISPATCH) - -enum -{ - /* - struct open_serial_con_args { - uint32_t baudRate; - uint32_t dataBits; - uint32_t stopBits; - uint32_t parityBit; - }; - */ - IOCTL_OPEN_SERIAL_CONNECTION = 0x5e01, -}; - -obos_status open_serial_connection(serial_port* port, uint32_t baudRate, data_bits dataBits, stop_bits stopbits, parity_bit parityBit); - -void com_irq_handler(struct irq* i, interrupt_frame* frame, void* userdata, irql oldIrql); -bool com_check_irq_callback(struct irq* i, void* userdata); -void com_irq_move_callback(struct irq* i, struct irq_vector* from, struct irq_vector* to, void* userdata); diff --git a/src/inc/dev_prefix.h.in b/src/inc/dev_prefix.h.in deleted file mode 100644 index 1877a3b6..00000000 --- a/src/inc/dev_prefix.h.in +++ /dev/null @@ -1 +0,0 @@ -#cmakedefine OBOS_DEV_PREFIX "@OBOS_DEV_PREFIX@" \ No newline at end of file diff --git a/src/init/CMakeLists.txt b/src/init/CMakeLists.txt deleted file mode 100644 index d99e85a9..00000000 --- a/src/init/CMakeLists.txt +++ /dev/null @@ -1,26 +0,0 @@ -# init/CMakeLists.txt -# -# Copyright (c) 2025 Omar Berrow - -if (${OBOS_ARCHITECTURE} STREQUAL "x86_64") - add_library(syscall_invoke STATIC "x86_64-syscall.S") - elseif (${OBOS_ARCHITECTURE} STREQUAL "m68k") - add_library(syscall_invoke STATIC "m68k-syscall.S") -endif() - -if (OBOS_ENABLE_UBSAN) - add_compile_options("-fno-sanitize=undefined") -endif() -if (OBOS_ENABLE_KASAN) - add_compile_options("-fno-sanitize=address") -endif() -if (NOT CMAKE_SYSTEM_PROCESSOR STREQUAL "x86-64") - add_compile_options("-fPIE") - add_link_options("-pie") -endif() - -link_libraries(syscall_invoke) - -add_executable (init "main.c" "motd.c" "nm.c") - -target_link_libraries(init PRIVATE cjson) diff --git a/src/init/m68k-syscall.S b/src/init/m68k-syscall.S deleted file mode 100644 index afb8713c..00000000 --- a/src/init/m68k-syscall.S +++ /dev/null @@ -1,31 +0,0 @@ -// This helper library allows one to use syscalln(...) in a user-program, since obos' -// sysdep marks it as hidden - -.global syscall - -syscall: - move.l %fp, %sp@- - move.l %sp, %fp - move.l %d2, %sp@- - move.l %d3, %sp@- - move.l %d4, %sp@- - move.l %d5, %sp@- - - move.l (+8,%fp), %d5 - move.l (+12,%fp), %d0 - move.l (+16,%fp), %d1 - move.l (+20,%fp), %d2 - move.l (+24,%fp), %d3 - move.l (+28,%fp), %d4 - trap #0 - - move.l %sp@+, %d5 - move.l %sp@+, %d4 - move.l %sp@+, %d3 - move.l %sp@+, %d2 - move.l %sp@+, %fp - rts - -.hidden syscall - -.section .note.GNU-stack,"",%progbits diff --git a/src/init/main.c b/src/init/main.c deleted file mode 100644 index 9e4e94b2..00000000 --- a/src/init/main.c +++ /dev/null @@ -1,265 +0,0 @@ -/* - * init/main.c - * - * Copyright (c) 2025-2026 Omar Berrow -*/ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include -#include - -#include "nm.h" - -int print_motd(); - -static int parse_file_status(obos_status status) -{ - switch (status) - { - case OBOS_STATUS_SUCCESS: return 0; - case OBOS_STATUS_NOT_FOUND: return ENOENT; - case OBOS_STATUS_INVALID_ARGUMENT: return EINVAL; - case OBOS_STATUS_PAGE_FAULT: return EFAULT; - case OBOS_STATUS_NOT_A_FILE: return EISDIR; - case OBOS_STATUS_UNINITIALIZED: return EBADF; - case OBOS_STATUS_EOF: return EIO; - case OBOS_STATUS_ACCESS_DENIED: return EACCES; - case OBOS_STATUS_NO_SYSCALL: return ENOSYS; - case OBOS_STATUS_NOT_ENOUGH_MEMORY: return ENOSPC; - case OBOS_STATUS_PIPE_CLOSED: return EPIPE; - default: abort(); - } -} - -const char* sigchld_action = "shutdown"; - -bool is_power_button_handler = false; - -// NOTE: This might need to be changed if init starts "adopting" processes when their parents die (as of this commit, the kernel adopts them). -void sigchld_handler(int num) -{ - (void)(num); - if (!is_power_button_handler) - printf("init: Child process died. Performing sigchld action \"%s\"\n", sigchld_action); - - setutxent(); - - do { - struct utmpx dead_proc_entry = {}; - struct timespec ts; - clock_gettime(0, &ts); - - dead_proc_entry.ut_tv.tv_sec = ts.tv_sec; - dead_proc_entry.ut_tv.tv_usec = ts.tv_nsec/1000; - dead_proc_entry.ut_type = DEAD_PROCESS; - dead_proc_entry.ut_pid = 1; - - char* tty_name = ttyname(STDIN_FILENO); - if (tty_name) - { - strncpy(dead_proc_entry.ut_line, tty_name + 5 /* remove /dev/ */, sizeof(dead_proc_entry.ut_line) - 1); - strncpy(dead_proc_entry.ut_id, tty_name + strlen(tty_name) - 4, sizeof(dead_proc_entry.ut_id) - 1); - } - - if (pututxline(&dead_proc_entry) == NULL) - perror("pututxline"); - updwtmpx(_PATH_WTMP, &dead_proc_entry); - } while(0); - - endutxent(); - - sync(); - if (strcasecmp(sigchld_action, "shutdown") == 0) - syscall0(Sys_Shutdown); - else if (strcasecmp(sigchld_action, "reboot") == 0) - syscall0(Sys_Reboot); - else if (strcasecmp(sigchld_action, "suspend") == 0) - syscall0(Sys_Suspend); - else if (strcasecmp(sigchld_action, "ignore") == 0) - return; - else - abort(); // bug - - exit(0); -} - -int main(int argc, char** argv) -{ - if (getpid() != 1) - return -1; - if (getuid() != 0) - return -1; - - const char* swap_file = NULL; - char* handoff_process = NULL; - int opt = 0; - long final_log_level = 2; - while ((opt = getopt(argc, argv, "+s:c:l:h")) != -1) - { - switch (opt) - { - case 'c': - { - sigchld_action = optarg; - // what the hell is this? - if (!sigchld_action || - (strcasecmp(sigchld_action, "help") == 0 && - ( - strcasecmp(sigchld_action, "shutdown") != 0 && - strcasecmp(sigchld_action, "reboot") != 0 && - strcasecmp(sigchld_action, "suspend") != 0 && - strcasecmp(sigchld_action, "ignore") != 0) - ) - ) - { - fprintf(stderr, "-c valid values: 'shutdown', 'reboot', 'suspend', 'ignore', and 'help' (for this menu).\n"); - return !sigchld_action ? 1 : strcasecmp(sigchld_action, "help") != 0; - } - break; - } - case 's': - { - swap_file = optarg; - break; - } - case 'l': - { - errno = 0; - final_log_level = strtol(optarg, NULL, 0); - if (errno != 0 || final_log_level < 0 || final_log_level > 4) - { - fprintf(stderr, "Expected integer within [0...4] for -l option, got %s instead\n", optarg); - return -1; - } - break; - } - case 'h': - default: - fprintf(stderr, "Usage: %s [-c sigchld/powerbutton_action] [-s swap_dev] [-l kernel_log_level] handoff_path [handoff program arguments]", argv[0]); - return opt != 'h'; - } - } - if (optind >= argc) - { - fprintf(stderr, "Usage: %s [-c sigchld/powerbutton_action] [-s swap_dev] [-l kernel_log_level] handoff_path [handoff program arguments]", argv[0]); - return 1; - } - // if (strcasecmp(sigchld_action, "ignore") != 0) - - struct timespec ts = {}; - clock_gettime(0, &ts); - - setutxent(); - do { - struct utmpx boot_entry = {}; - - boot_entry.ut_tv.tv_sec = ts.tv_sec; - boot_entry.ut_tv.tv_usec = ts.tv_nsec/1000; - boot_entry.ut_type = BOOT_TIME; - boot_entry.ut_pid = 1; - memcpy(boot_entry.ut_line, "reboot", 6); - - if (pututxline(&boot_entry) == NULL) - perror("pututxline"); - updwtmpx(_PATH_WTMP, &boot_entry); - } while(0); - do { - struct utmpx init_proc_entry = {}; - struct timespec ts; - - clock_gettime(0, &ts); - - init_proc_entry.ut_tv.tv_sec = ts.tv_sec; - init_proc_entry.ut_tv.tv_usec = ts.tv_nsec/1000; - init_proc_entry.ut_type = INIT_PROCESS; - init_proc_entry.ut_pid = 1; - - char* tty_name = ttyname(STDIN_FILENO); - if (tty_name) - { - strncpy(init_proc_entry.ut_line, tty_name + 5 /* remove /dev/ */, sizeof(init_proc_entry.ut_line) - 1); - strncpy(init_proc_entry.ut_id, tty_name + strlen(tty_name) - 4, sizeof(init_proc_entry.ut_id) - 1); - } - - if (pututxline(&init_proc_entry) == NULL) - perror("pututxline"); - updwtmpx(_PATH_WTMP, &init_proc_entry); - } while(0); - endutxent(); - - if (0) - signal(SIGCHLD, sigchld_handler); - if (swap_file) - { - printf("init: Switching swap to %s\n", swap_file); - obos_status st = (obos_status)syscall1(Sys_SwitchSwap, swap_file); - if (obos_is_error(st)) - { - errno = parse_file_status(st); - perror("Could not switch swap"); - } - } - handoff_process = argv[optind]; - - setenv("PATH", "/usr/bin:/bin:/usr/sbin:/sbin", 1); - - nm_initialize_hostname(); - nm_initialize_interfaces("/etc/interfaces.json"); - - int ret = print_motd(); - if (ret != 0) - return ret; - - syscall1(Sys_SetKLogLevel, final_log_level); - - // Start a shell, I guess. - pid_t pid = fork(); - if (pid == 0) - { - setpgrp(); - tcsetpgrp(0, getpgrp()); - execvp(handoff_process, &argv[optind]); - perror("execvp"); - exit(EXIT_FAILURE); - } - int power_button = open("/dev/power_button", O_RDONLY); - if (power_button != -1 && fork() == 0) - { - // ACPI Event Handler. - is_power_button_handler = true; - fd_set set = {}; - FD_ZERO(&set); - FD_SET(power_button, &set); - select(power_button+1, &set, NULL, NULL, NULL); - syscall1(Sys_LibCLog, "init: Received power button event\n"); - sigchld_handler(SIGCHLD); - } - int status; - top: - waitpid(pid, &status, 0); - if (WIFSIGNALED(status)) - printf("Handoff process exited due to signal %d\n", WTERMSIG(status)); - if (!WIFEXITED(status) && !WIFSIGNALED(status)) - goto top; - sigchld_handler(SIGCHLD); - while (1) - asm volatile ("" :::"memory"); -} diff --git a/src/init/motd.c b/src/init/motd.c deleted file mode 100644 index 9c0578c3..00000000 --- a/src/init/motd.c +++ /dev/null @@ -1,33 +0,0 @@ -/* - * init/motd.c - * - * Copyright (c) 2025 Omar Berrow -*/ - -#include -#include -#include -#include -#include - -int print_motd() -{ - struct stat st = {}; - if (stat("/etc/motd", &st) != 0) - { - perror("stat(\"/etc/motd\")"); - return -1; - } - int motd_fd = open("/etc/motd", O_RDONLY); - if (motd_fd < 0) - { - perror("open(\"/etc/motd\", O_RDONLY)"); - return -1; - } - void* motd = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, motd_fd, 0); - write(STDOUT_FILENO, motd, st.st_size); - write(STDOUT_FILENO, "\n", 1); - close(motd_fd); - munmap(motd, st.st_size); - return 0; -} \ No newline at end of file diff --git a/src/init/nm.c b/src/init/nm.c deleted file mode 100644 index 9a2546d4..00000000 --- a/src/init/nm.c +++ /dev/null @@ -1,310 +0,0 @@ -/* - * init/nm.c - * - * Copyright (c) 2025 Omar Berrow -*/ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include - -#include "nm.h" - -static ip_addr get_ip_addr_field(cJSON* parent, const char* field) -{ - cJSON* child = cJSON_GetObjectItem(parent, field); - if (!child) return (ip_addr){.addr=0}; - const char* str = cJSON_GetStringValue(child); - if (!str) return (ip_addr){.addr=0}; - return (ip_addr){.addr=inet_addr(str)}; -} -static const char* get_str_field(cJSON* parent, const char* field) -{ - cJSON* child = cJSON_GetObjectItem(parent, field); - if (!child) return NULL; - const char* str = cJSON_GetStringValue(child); - return str; -} -static bool get_boolean_field(cJSON* parent, const char* field, bool def) -{ - cJSON* obj = cJSON_GetObjectItem(parent, field); - if (!obj) return def; - return cJSON_IsTrue(obj); -} - -/* - * Format: -{ - "address": "x.x.x.x", - "broadcast-address": "x.x.x.x", - "subnet-mask": "x.x.x.x", - // Default: true - "ipv4-forwarding": boolean, - // Default: true - "arp-reply": boolean, - // Default: true - "icmp-echo-reply": boolean -} -*/ -ip_table_entry_user nm_parse_ip_table_entry(cJSON* obj) -{ - ip_table_entry_user ret = {}; - ret.address = get_ip_addr_field(obj, "address"); - ret.broadcast = get_ip_addr_field(obj, "broadcast-address"); - ret.subnet = get_ip_addr_field(obj, "subnet-mask").addr; - if (get_boolean_field(obj, "ipv4-forwarding", true)) - ret.ip_entry_flags |= IP_ENTRY_IPv4_FORWARDING; - if (get_boolean_field(obj, "arp-reply", true)) - ret.ip_entry_flags |= IP_ENTRY_ENABLE_ARP_REPLY; - if (get_boolean_field(obj, "icmp-echo-reply", true)) - ret.ip_entry_flags |= IP_ENTRY_ENABLE_ICMP_ECHO_REPLY; - return ret; -} - -/* - * Format: -{ - "source": "x.x.x.x", - "router": "x.x.x.x" -} -*/ -gateway_user nm_parse_gateway(cJSON* obj) -{ - gateway_user ret = {}; - ret.src = get_ip_addr_field(obj, "source"); - ret.dest = get_ip_addr_field(obj, "router"); - return ret; -} - -ip_table_entry_user* nm_get_ip_table(cJSON* top_level, size_t* size_array) -{ - *size_array = 0; - cJSON* obj = NULL; - cJSON* json_table = cJSON_GetObjectItem(top_level, "ip-table"); - if (!json_table) return NULL; - ip_table_entry_user* table = calloc(cJSON_GetArraySize(json_table), sizeof(ip_table_entry_user)); - *size_array = cJSON_GetArraySize(json_table); - size_t i = 0; - cJSON_ArrayForEach(obj, json_table) - table[i++] = nm_parse_ip_table_entry(obj); - return table; -} -gateway_user* nm_get_gateways(cJSON* top_level, size_t* size_array) -{ - *size_array = 0; - cJSON* obj = NULL; - cJSON* json_table = cJSON_GetObjectItem(top_level, "static-routes"); - if (!json_table) return NULL; - gateway_user* table = calloc(cJSON_GetArraySize(json_table), sizeof(gateway_user)); - *size_array = cJSON_GetArraySize(json_table); - size_t i = 0; - cJSON_ArrayForEach(obj, json_table) - table[i++] = nm_parse_gateway(obj); - return table; -} - -/* - * Format: -{ - "interface": "(interface name)", -// Default: false - "dynamic-config": (boolean), - // Ignored if dynamic-config = true - "ip-table": [ - // See nm_parse_ip_table_entry - ], - "default-router": "x.x.x.x", - "static-routes": [ - // See nm_parse_gateway - ] -} -*/ - -bool nm_initialize_interface(cJSON* top_level) -{ - const char* interface_name = get_str_field(top_level, "interface"); - if (!interface_name) - { - fprintf(stderr, "NM: No interface name specified\n"); - return false; - } - if (get_boolean_field(top_level, "dynamic-config", false)) - { - pid_t pid = 0; - if ((pid = fork()) == 0) - { - setpgrp(); - tcsetpgrp(0, getpgrp()); - char *argv[3] = { - "obos_dhcpc", - (char*)interface_name, - NULL - }; - execvp("/usr/sbin/obos_dhcpc", argv); - exit(-errno); - } - int status = 0; - waitpid(pid, &status, 0); - if (status < 0) - { - fprintf(stderr, "executing obos_dhcpc: %s\n", strerror(-status)); - return false; - } - if (status > 0) - { - fprintf(stderr, "obos_dhcpc returned status %d\n", status); - return false; - } - return true; - } - char* interface_path = malloc(strlen(interface_name)+sizeof("/dev/")); - snprintf(interface_path, strlen(interface_name)+sizeof("/dev/"), "/dev/%s", interface_name); - printf("NM: Initializing interface %s at %s\n", interface_name, interface_path); - - int fd = open(interface_path, O_RDWR); - if (fd < 0) - { - perror("NM: Could not initialize interface: open"); - return false; - } - free(interface_path); - - size_t ip_table_len = 0, nGateways = 0; - ip_table_entry_user* ip_table = nm_get_ip_table(top_level, &ip_table_len); - gateway_user* gateways = nm_get_gateways(top_level, &nGateways); - ip_addr default_gateway = get_ip_addr_field(top_level, "default-router"); - - ioctl(fd, IOCTL_IFACE_INITIALIZE); - for (size_t i = 0; i < ip_table_len; i++) - { - if (ip_table[i].address.addr && ip_table[i].broadcast.addr && ip_table[i].subnet) - { - if (ioctl(fd, IOCTL_IFACE_ADD_IP_TABLE_ENTRY, &ip_table[i]) != 0) - { - fprintf(stderr, "ioctl(IOCTL_IFACE_ADD_IP_TABLE_ENTRY): %s\nIs the interface already configured?\n", strerror(errno)); - return false; - } - } - else - fprintf(stderr, "NM: %s: Skipping invalid ip table entry\n", interface_name); - } - for (size_t i = 0; i < nGateways; i++) - { - if (gateways[i].src.addr && gateways[i].dest.addr) - { - if (ioctl(fd, IOCTL_IFACE_ADD_ROUTING_TABLE_ENTRY, &gateways[i]) != 0) - { - fprintf(stderr, "ioctl(IOCTL_IFACE_ADD_ROUTING_TABLE_ENTRY): %s\nIs the router up, or is the interface already configured?\n", strerror(errno)); - return false; - } - } - else - fprintf(stderr, "NM: %s: Skipping invalid static route\n", interface_name); - } - if (default_gateway.addr) - { - if (ioctl(fd, IOCTL_IFACE_SET_DEFAULT_GATEWAY, &default_gateway) != 0) - { - fprintf(stderr, "ioctl(IOCTL_IFACE_SET_DEFAULT_GATEWAY): %s\nIs the router up, or is the interface already configured?\n", strerror(errno)); - return false; - } - } - else - fprintf(stderr, "NM: %s: Skipping default gateway\n", interface_name); - - free(gateways); - free(ip_table); - - close(fd); - - return true; -} - -void nm_initialize_interfaces(const char* config_file) -{ - int fd = open(config_file, O_RDONLY); - if (fd < 0) - { - perror("open"); - return; - } - struct stat st = {}; - fstat(fd, &st); - char* buf = mmap(NULL, st.st_size, PROT_READ, 0, fd, 0); - cJSON* top_level = cJSON_ParseWithLength(buf, st.st_size); - if (!top_level) - { - const char* errptr = cJSON_GetErrorPtr(); - int dec = 0; - for (; dec <= 15; dec++) - { - if (errptr > buf) - errptr--; - else - break; - } - int inc = 0; - for (; inc <= 15; inc++) - { - if (*(errptr+inc)) - continue; - break; - } - int nToPrint = dec+inc; - fprintf(stderr, "NM: JSON parsing error in file %s\n%.*s\n", config_file, nToPrint, errptr); - return; - } - munmap(buf, st.st_size); - close(fd); - - cJSON* obj = NULL; - cJSON* json_table = cJSON_GetObjectItem(top_level, "interfaces"); - if (!json_table) - { - fprintf(stderr, "NM: No interfaces array in %s\n", config_file); - return; - } - size_t nInterfacesInitialized = 0, nInterfaces = 0; - cJSON_ArrayForEach(obj, json_table) - { - nInterfaces++; - if (nm_initialize_interface(obj)) - nInterfacesInitialized++; - } - printf("NM: Initialized %ld interfaces (%ld failed)\n\n", nInterfacesInitialized, nInterfaces-nInterfacesInitialized); - - cJSON_Delete(top_level); -} - -void nm_initialize_hostname() -{ - int fd = open("/etc/hostname", O_RDONLY); - if (fd < 0) - { - perror("open(\"/etc/hostname\", O_RDONLY)"); - return; - } - struct stat st = {}; - fstat(fd, &st); - - char* buf = mmap(NULL, st.st_size, PROT_READ, 0, fd, 0); - - if (sethostname(buf, st.st_size) != 0) - perror("sethostname"); - - munmap(buf, st.st_size); - close(fd); -} diff --git a/src/init/nm.h b/src/init/nm.h deleted file mode 100644 index d0467798..00000000 --- a/src/init/nm.h +++ /dev/null @@ -1,70 +0,0 @@ -/* - * init/nm.h - * - * Copyright (c) 2025 Omar Berrow -*/ - -#pragma once - -#include - -void nm_initialize_interfaces(const char* config_file); -void nm_initialize_hostname(); - -typedef union ip_addr { -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - struct { - uint8_t comp1; - uint8_t comp2; - uint8_t comp3; - uint8_t comp4; - }; -#else - struct { - uint8_t comp4; - uint8_t comp3; - uint8_t comp2; - uint8_t comp1; - }; -#endif - uint32_t addr; -} ip_addr; -_Static_assert(sizeof(ip_addr) == 4, "Invalid ip_addr size"); - -typedef struct gateway_user { - ip_addr src; - ip_addr dest; -} gateway_user; - -enum { - IP_ENTRY_ENABLE_ICMP_ECHO_REPLY = 1<<0, - IP_ENTRY_ENABLE_ARP_REPLY = 1<<1, - IP_ENTRY_IPv4_FORWARDING = 1<<2, -}; - -typedef struct ip_table_entry_user { - ip_addr address; - ip_addr broadcast; - uint32_t subnet; - uint32_t ip_entry_flags; -} ip_table_entry_user; - -enum { - // Each ethernet driver should define this - // argp points to a `mac_address` - IOCTL_IFACE_MAC_REQUEST = 0xe100, - // implementations of the following ioctls are in - // tables.h - IOCTL_IFACE_ADD_IP_TABLE_ENTRY, - IOCTL_IFACE_REMOVE_IP_TABLE_ENTRY, - IOCTL_IFACE_ADD_ROUTING_TABLE_ENTRY, - IOCTL_IFACE_REMOVE_ROUTING_TABLE_ENTRY, - IOCTL_IFACE_SET_IP_TABLE_ENTRY, - IOCTL_IFACE_CLEAR_ARP_CACHE, - IOCTL_IFACE_CLEAR_ROUTE_CACHE, - IOCTL_IFACE_GET_IP_TABLE, - IOCTL_IFACE_GET_ROUTING_TABLE, - IOCTL_IFACE_SET_DEFAULT_GATEWAY, - IOCTL_IFACE_UNSET_DEFAULT_GATEWAY, - IOCTL_IFACE_INITIALIZE, -}; \ No newline at end of file diff --git a/src/init/x86_64-syscall.S b/src/init/x86_64-syscall.S deleted file mode 100644 index c4b9f305..00000000 --- a/src/init/x86_64-syscall.S +++ /dev/null @@ -1,22 +0,0 @@ -// This helper library allows one to use syscalln(...) in a user-program, since obos' -// sysdep marks it as hidden - -.intel_syntax noprefix - -.global syscall - -syscall: - push rbp - mov rbp, rsp - - mov eax, edi - mov rdi, rsi - mov rsi, rdx - mov rdx, rcx - - syscall - - leave - ret - -.att_syntax prefix \ No newline at end of file diff --git a/src/isogen/CMakeLists.txt b/src/isogen/CMakeLists.txt new file mode 100644 index 00000000..a7d3eeb1 --- /dev/null +++ b/src/isogen/CMakeLists.txt @@ -0,0 +1,65 @@ +# isogen/CMakeLists.txt + +# Copyright (c) 2024 Omar Berrow + +# The only point of this subdirectory is to provide a target which builds the iso. + +set (SOURCE_DIR ${CMAKE_SOURCE_DIR}) +if (CMAKE_HOST_WIN32) + set (SUPPRESS_OUTPUT > NUL 2>&1) +elseif(CMAKE_HOST_UNIX) + set (SUPPRESS_OUTPUT > /dev/null 2>&1) +endif() + +set (ISODIR ${CMAKE_SOURCE_DIR}/isodir/) + +if (NOT EXISTS ${ISODIR}) + file (MAKE_DIRECTORY ${ISODIR}) +endif() +if (NOT EXISTS ${ISODIR}/obos) + file (MAKE_DIRECTORY ${ISODIR}/obos) +endif() +if (NOT EXISTS ${ISODIR}/limine) + file (MAKE_DIRECTORY ${ISODIR}/limine) +endif() +if (NOT EXISTS ${ISODIR}/EFI) + file (MAKE_DIRECTORY ${ISODIR}/EFI) +endif() +if (NOT EXISTS ${ISODIR}/EFI/BOOT) + file (MAKE_DIRECTORY ${ISODIR}/EFI/BOOT) +endif() +if (NOT EXISTS ${CMAKE_SOURCE_DIR}/config/limine.cfg) + message(FATAL_ERROR "No hyper configuration file detected!") +endif() +add_custom_target(isogen ALL + COMMAND ${OBJCOPY} -g ${OUTPUT_DIR}/oboskrnl ${ISODIR}/obos/oboskrnl + COMMAND ${OBJCOPY} -g ${OUTPUT_DIR}/testDriver ${ISODIR}/obos/testDriver + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/config/limine.cfg ${ISODIR}/ ${SUPPRESS_OUTPUT} + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/dependencies/limine/limine-uefi-cd.bin ${ISODIR}/limine ${SUPPRESS_OUTPUT} + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/dependencies/limine/limine-bios-cd.bin ${ISODIR}/limine ${SUPPRESS_OUTPUT} + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/dependencies/limine/limine-bios-pxe.bin ${ISODIR}/limine ${SUPPRESS_OUTPUT} + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/dependencies/limine/limine-bios.sys ${ISODIR}/limine ${SUPPRESS_OUTPUT} + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/dependencies/limine/BOOTX64.EFI ${ISODIR}/EFI/BOOT ${SUPPRESS_OUTPUT} + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/dependencies/limine/BOOTIA32.EFI ${ISODIR}/EFI/BOOT ${SUPPRESS_OUTPUT} + COMMAND xorriso -as mkisofs -b limine/limine-bios-cd.bin -no-emul-boot -boot-load-size 4 -boot-info-table --efi-boot limine/limine-uefi-cd.bin -efi-boot-part --efi-boot-image --protective-msdos-label isodir -o ${OUTPUT_DIR}/obos.iso ${SUPPRESS_OUTPUT} + COMMAND ${limine_install} bios-install ${OUTPUT_DIR}/obos.iso ${SUPPRESS_OUTPUT} + SOURCES ${CMAKE_SOURCE_DIR}/config/limine.cfg ${CMAKE_SOURCE_DIR}/dependencies/limine/limine-uefi-cd.bin ${CMAKE_SOURCE_DIR}/dependencies/limine/limine-bios-cd.bin + ${CMAKE_SOURCE_DIR}/dependencies/limine/limine-bios-pxe.bin ${CMAKE_SOURCE_DIR}/dependencies/limine/limine-bios.sys ${CMAKE_SOURCE_DIR}/dependencies/limine/BOOTX64.EFI + ${CMAKE_SOURCE_DIR}/dependencies/limine/BOOTIA32.EFI + DEPENDS oboskrnl + DEPENDS uacpi + DEPENDS testDriver + BYPRODUCTS ${ISODIR}/limine.cfg + BYPRODUCTS ${ISODIR}/EFI/BOOT/BOOTX64.efi + BYPRODUCTS ${ISODIR}/EFI/BOOT/BOOTIA32.efi + BYPRODUCTS ${ISODIR}/limine/limine-uefi-cd.bin + BYPRODUCTS ${ISODIR}/limine/limine-bios-cd.bin + BYPRODUCTS ${ISODIR}/limine/limine-bios-pxe.bin + BYPRODUCTS ${ISODIR}/limine/limine-bios.sys + BYPRODUCTS ${ISODIR}/obos/oboskrnl + BYPRODUCTS ${ISODIR}/obos/testDriver + BYPRODUCTS ${OUTPUT_DIR}/obos.iso + # DEPENDS oboskrnl + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + COMMENT "Generating ${OUTPUT_DIR}obos.iso" +) \ No newline at end of file diff --git a/src/isogen/isogen.cmake b/src/isogen/isogen.cmake deleted file mode 100644 index 85027121..00000000 --- a/src/isogen/isogen.cmake +++ /dev/null @@ -1,69 +0,0 @@ -# isogen/isogen.cmake - -# Copyright (c) 2024-2025 Omar Berrow - -set (ISODIR ${CMAKE_SOURCE_DIR}/out/isodir/) - -if (OBOS_ARCHITECTURE STREQUAL "m68k") - return() -endif() - -if (NOT EXISTS ${ISODIR}) - file (MAKE_DIRECTORY ${ISODIR}) -endif() -if (NOT EXISTS ${ISODIR}/obos) - file (MAKE_DIRECTORY ${ISODIR}/obos) -endif() - -if (CMAKE_HOST_WIN32) - set(SUPPRESS_OUTPUT > NUL 2>&1) -elseif(CMAKE_HOST_LINUX) - set(SUPPRESS_OUTPUT > /dev/null 2>&1) -endif() - -if (OBOS_USE_LIMINE) - set (LIMINE_ENABLED "true") - set (LIMINE_NOT_ENABLED "false") - set(BIOS_INSTALL ${limine_exe} bios-install ${OUTPUT_DIR}/obos.iso ${SUPPRESS_OUTPUT}) - set(BIOS_PRE_XORRISO fallocate -l16M ${ISODIR}/obos/flanterm_buff && cmake -E make_directory ${ISODIR}/limine/ && cmake -E copy ${CMAKE_SOURCE_DIR}/dependencies/limine/limine-bios.sys ${ISODIR}/limine/) - set(BTLDR_CONF "limine.conf") - set(xorriso_bios_cd_path "dependencies/limine/limine-bios-cd.bin") - set(xorriso_uefi_cd_path "dependencies/limine/limine-uefi-cd.bin") - set(xorriso_uefi_cd_name "limine-uefi-cd.bin") - set(xorriso_bios_cd_name "limine-bios-cd.bin") -else() - set(BIOS_INSTALL ${hyper_install} ${OUTPUT_DIR}/obos.iso ${SUPPRESS_OUTPUT}) - set(BIOS_PRE_XORRISO true) - set(BTLDR_CONF "hyper.cfg") - set(xorriso_bios_cd_path "dependencies/hyper/hyper_iso_boot") - set(xorriso_uefi_cd_path "config/hyper_uefi_boot.bin") - set(xorriso_uefi_cd_name "hyper_uefi_boot.bin") - set(xorriso_bios_cd_name "hyper_iso_boot") -endif() - -if (NOT EXISTS ${CMAKE_SOURCE_DIR}/config/${BTLDR_CONF}) - message(FATAL_ERROR "No bootloader configuration file detected!") -endif() - -add_custom_command(OUTPUT ${OUTPUT_DIR}/obos.iso - COMMAND ${OBJCOPY} -g ${OUTPUT_DIR}/oboskrnl ${ISODIR}/obos/oboskrnl ${SUPPRESS_OUTPUT} - COMMAND cmake -E copy ${CMAKE_SOURCE_DIR}/config/${BTLDR_CONF} ${ISODIR}/ ${SUPPRESS_OUTPUT} - COMMAND cmake -E copy ${CMAKE_SOURCE_DIR}/${xorriso_bios_cd_path} ${ISODIR} ${SUPPRESS_OUTPUT} - COMMAND cmake -E copy ${CMAKE_SOURCE_DIR}/${xorriso_uefi_cd_path} ${ISODIR} ${SUPPRESS_OUTPUT} - COMMAND cmake -E copy ${CMAKE_SOURCE_DIR}/out/initrd ${ISODIR}/obos ${SUPPRESS_OUTPUT} - COMMAND cmake -E copy ${CMAKE_SOURCE_DIR}/config/initrd.tar ${ISODIR}/obos ${SUPPRESS_OUTPUT} - COMMAND ${BIOS_PRE_XORRISO} - COMMAND xorriso -as mkisofs -b ${xorriso_bios_cd_name} -no-emul-boot -boot-load-size 4 -boot-info-table --efi-boot ${xorriso_uefi_cd_name} -efi-boot-part --efi-boot-image --protective-msdos-label out/isodir -o ${OUTPUT_DIR}/obos.iso ${SUPPRESS_OUTPUT} - COMMAND ${BIOS_INSTALL} - BYPRODUCTS ${ISODIR}/${BTLDR_CONF} - ${ISODIR}/${xorriso_bios_cd_name} - ${ISODIR}/${xorriso_uefi_cd_name} - ${ISODIR}/obos/oboskrnl - ${ISODIR}/obos/initrd - ${ISODIR}/obos/initrd.tar - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} - DEPENDS ${CMAKE_SOURCE_DIR}/config/initrd.tar ${CMAKE_SOURCE_DIR}/config/${BTLDR_CONF} oboskrnl initrd -) -add_custom_target (isogen ALL - DEPENDS oboskrnl initrd ${OUTPUT_DIR}/obos.iso -) diff --git a/src/libs/uACPI/CMakeLists.txt b/src/libs/uACPI/CMakeLists.txt new file mode 100644 index 00000000..709c7554 --- /dev/null +++ b/src/libs/uACPI/CMakeLists.txt @@ -0,0 +1,56 @@ +# libs/uACPI/CMakeLists.txt + +# Copyright (c) 2024 Omar Berrow + +include (${uacpi_cmake_file}) + +uacpi_add_sources() + +add_library(uacpi STATIC ${UACPI_SOURCES} "kernel_api.cpp") + +target_compile_options(uacpi + PRIVATE $<$:${TARGET_COMPILE_OPTIONS_CPP}> + PRIVATE $<$:${TARGET_COMPILE_OPTIONS_C}> + PRIVATE $<$:-fno-rtti> + PRIVATE $<$:-fno-exceptions> + PRIVATE $<$:-fno-stack-protector> + PRIVATE $<$:-fno-stack-check> + PRIVATE $<$:-fno-use-cxa-atexit> + PRIVATE $<$:-fno-threadsafe-statics> + PRIVATE $<$:-std=gnu++20> + PRIVATE $<$:-ffreestanding> + PRIVATE $<$:-fstack-protector-strong> +# PRIVATE $<$:-nostdlib> +# We don't enable -Werror in case future compiler versions introduce new warnings that the kernel triggers. + PRIVATE $<$:-Wall> + PRIVATE $<$:-Wextra> + PRIVATE $<$:-Wno-missing-field-initializers> + PRIVATE $<$:-g> +) + +set_target_properties(uacpi PROPERTIES CXX_STANDARD 20) +set_target_properties(uacpi PROPERTIES CXX_STANDARD_REQUIRED 20) + +target_include_directories(uacpi PRIVATE "${CMAKE_SOURCE_DIR}/src/oboskrnl") +target_include_directories(uacpi PRIVATE ${OBOSKRNL_EXTERNAL_INCLUDES}) +target_include_directories(uacpi PUBLIC "${CMAKE_SOURCE_DIR}/src/libs/uACPI") + +target_link_libraries(uacpi PRIVATE ${LIBGCC}) + +target_compile_definitions(uacpi + PRIVATE $<$:OBOS_DEBUG> + PRIVATE $<$:OBOS_RELEASE> + PUBLIC UACPI_OVERRIDE_STDLIB=1 + PUBLIC UACPI_OVERRIDE_ARCH_HELPERS=1 + ) +target_link_options(uacpi + PRIVATE "-nostdlib" +) + +if (DEFINED OBOS_ENABLE_KASAN) + target_compile_options(uacpi PRIVATE $<$:-fsanitize=kernel-address>) + set (OBOS_ENABLE_KASAN "1") +else() + set (OBOS_ENABLE_KASAN "0") +endif() +target_compile_definitions(uacpi PRIVATE OBOS_KASAN_ENABLED=${OBOS_ENABLE_KASAN}) \ No newline at end of file diff --git a/src/libs/uACPI/kernel_api.cpp b/src/libs/uACPI/kernel_api.cpp new file mode 100644 index 00000000..895658ba --- /dev/null +++ b/src/libs/uACPI/kernel_api.cpp @@ -0,0 +1,752 @@ +/* + libs/uACPI/kernel_api.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include + +#include +#include +#include + +#include + +#include +#include +#include + +#include + +#include +#include + +#include +#include + +#include + +#ifdef __x86_64__ +#include +#include +#include +#include +#endif + +using namespace obos; + +static bool isPower2(uint64_t num) +{ + auto firstBitPos = __builtin_ctzll(num); + num &= ~(1< uint8_t { return inb(address); }; + readW = [](uacpi_io_addr address) -> uint16_t { return inw(address); }; + readD = [](uacpi_io_addr address) -> uint32_t { return ind(address); }; + readQ = nullptr; // Not supported. +#endif + if (!isPower2(byteWidth)) + return UACPI_STATUS_INVALID_ARGUMENT; + if (byteWidth > 8) + return UACPI_STATUS_INVALID_ARGUMENT; + uacpi_status status = UACPI_STATUS_OK; + if (byteWidth == 1) + { + if (readB) + *out_value = readB(address); + else + status = UACPI_STATUS_INVALID_ARGUMENT; + } + if (byteWidth == 2) + { + if (readW) + *out_value = readW(address); + else + status = UACPI_STATUS_INVALID_ARGUMENT; + } + if (byteWidth == 4) + { + if (readD) + *out_value = readD(address); + else + status = UACPI_STATUS_INVALID_ARGUMENT; + } + if (byteWidth == 8) + { + if (readQ) + *out_value = readQ(address); + else + status = UACPI_STATUS_INVALID_ARGUMENT; + } + return status; + } + uacpi_status uacpi_kernel_raw_io_write(uacpi_io_addr address, uacpi_u8 byteWidth, uacpi_u64 in_value) + { + void(*writeB)(uacpi_io_addr address, uint8_t val) = nullptr; + void(*writeW)(uacpi_io_addr address, uint16_t val) = nullptr; + void(*writeD)(uacpi_io_addr address, uint32_t val) = nullptr; + void(*writeQ)(uacpi_io_addr address, uint64_t val) = nullptr; +#ifdef __x86_64__ + writeB = [](uacpi_io_addr address, uint8_t val) -> void { outb(address, val); }; + writeW = [](uacpi_io_addr address, uint16_t val) -> void { outw(address, val); }; + writeD = [](uacpi_io_addr address, uint32_t val) -> void { outd(address, val); }; + writeQ = nullptr; // Not supported. +#endif + if (!isPower2(byteWidth)) + return UACPI_STATUS_INVALID_ARGUMENT; + if (byteWidth > 8) + return UACPI_STATUS_INVALID_ARGUMENT; + uacpi_status status = UACPI_STATUS_OK; + if (byteWidth == 1) + { + if (writeB) + writeB(address, in_value); + else + status = UACPI_STATUS_INVALID_ARGUMENT; + } + if (byteWidth == 2) + { + if (writeW) + writeW(address, in_value); + else + status = UACPI_STATUS_INVALID_ARGUMENT; + } + if (byteWidth == 4) + { + if (writeD) + writeD(address, in_value); + else + status = UACPI_STATUS_INVALID_ARGUMENT; + } + if (byteWidth == 8) + { + if (writeQ) + writeQ(address, in_value); + else + status = UACPI_STATUS_INVALID_ARGUMENT; + } + return status; + } +#ifdef __x86_64__ + void pciWriteByteRegister(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint8_t data) + { + uint32_t address; + uint32_t lbus = (uint32_t)bus; + uint32_t lslot = (uint32_t)slot; + uint32_t lfunc = (uint32_t)func; + + address = (uint32_t)((lbus << 16) | (lslot << 11) | + (lfunc << 8) | (offset & 0xFC) | ((uint32_t)0x80000000)); + + outd(0xCF8, address); + outb(0xCFC, data); + } + void pciWriteWordRegister(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint16_t data) + { + uint32_t address; + uint32_t lbus = (uint32_t)bus; + uint32_t lslot = (uint32_t)slot; + uint32_t lfunc = (uint32_t)func; + + address = (uint32_t)((lbus << 16) | (lslot << 11) | + (lfunc << 8) | (offset & 0xFC) | ((uint32_t)0x80000000)); + + outd(0xCF8, address); + outw(0xCFC, data); + } + void pciWriteDwordRegister(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint32_t data) + { + uint32_t address; + uint32_t lbus = (uint32_t)bus; + uint32_t lslot = (uint32_t)slot; + uint32_t lfunc = (uint32_t)func; + + address = (uint32_t)((lbus << 16) | (lslot << 11) | + (lfunc << 8) | (offset & 0xFC) | ((uint32_t)0x80000000)); + + outd(0xCF8, address); + outd(0xCFC, data); + } + uint8_t pciReadByteRegister(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) + { + uint32_t address; + uint32_t lbus = (uint32_t)bus; + uint32_t lslot = (uint32_t)slot; + uint32_t lfunc = (uint32_t)func; + + address = (uint32_t)((lbus << 16) | (lslot << 11) | + (lfunc << 8) | (offset & 0xFC) | ((uint32_t)0x80000000)); + + outd(0xCF8, address); + + uint8_t ret = (uint16_t)((ind(0xCFC) >> ((offset & 2) * 8)) & 0xFFFFFF); + return ret; + } + uint16_t pciReadWordRegister(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) + { + uint32_t address; + uint32_t lbus = (uint32_t)bus; + uint32_t lslot = (uint32_t)slot; + uint32_t lfunc = (uint32_t)func; + + address = (uint32_t)((lbus << 16) | (lslot << 11) | + (lfunc << 8) | (offset & 0xFC) | ((uint32_t)0x80000000)); + + outd(0xCF8, address); + + uint16_t ret = (uint16_t)((ind(0xCFC) >> ((offset & 2) * 8)) & 0xFFFF); + return ret; + } + uint32_t pciReadDwordRegister(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) + { + uint32_t address; + uint32_t lbus = (uint32_t)bus; + uint32_t lslot = (uint32_t)slot; + uint32_t lfunc = (uint32_t)func; + + address = (uint32_t)((lbus << 16) | (lslot << 11) | + (lfunc << 8) | (offset & 0xFC) | ((uint32_t)0x80000000)); + + outd(0xCF8, address); + + return ((ind(0xCFC) >> ((offset & 2) * 8))); + } +#endif + uacpi_status uacpi_kernel_pci_read( + uacpi_pci_address *address, uacpi_size offset, + uacpi_u8 byte_width, uacpi_u64 *value + ) + { + if (address->segment) + return UACPI_STATUS_UNIMPLEMENTED; + switch (byte_width) + { + case 1: *value = pciReadByteRegister(address->bus, address->device, address->function, offset); break; + case 2: *value = pciReadWordRegister(address->bus, address->device, address->function, offset); break; + case 4: *value = pciReadDwordRegister(address->bus, address->device, address->function, offset); break; + default: return UACPI_STATUS_INVALID_ARGUMENT; + } + return UACPI_STATUS_OK; + } + uacpi_status uacpi_kernel_pci_write( + uacpi_pci_address *address, uacpi_size offset, + uacpi_u8 byte_width, uacpi_u64 value + ) + { + if (address->segment) + return UACPI_STATUS_UNIMPLEMENTED; + switch (byte_width) + { + case 1: pciWriteByteRegister(address->bus, address->device, address->function, offset, value & 0xff); break; + case 2: pciWriteWordRegister(address->bus, address->device, address->function, offset, value & 0xffff); break; + case 4: pciWriteDwordRegister(address->bus, address->device, address->function, offset, value & 0xffff'ffff); break; + default: return UACPI_STATUS_INVALID_ARGUMENT; + } + return UACPI_STATUS_OK; + } + // static allocators::BasicAllocator s_uACPIAllocator; + // static bool s_uACPIAllocatorInitialized = false; + void* uacpi_kernel_alloc(uacpi_size size) + { + // logger::debug("Attempting allocation of %lu bytes.\n", size); + // if (!s_uACPIAllocatorInitialized) + // { + // new (&s_uACPIAllocator) allocators::BasicAllocator{}; + // s_uACPIAllocatorInitialized = true; + // } + void* ret = new uint8_t[size]; + if (!ret) + logger::warning("%s: Allocation of 0x%lx bytes failed.\n", __func__, size); + /*else + logger::debug("Allocated %lu bytes at 0x%p\n", size, ret);*/ + return ret; + } + void* uacpi_kernel_calloc(uacpi_size count, uacpi_size size) + { + return memzero(uacpi_kernel_alloc(count * size), count * size); + } + void uacpi_kernel_free(void* mem) + { + if (!mem) + return; + if (mem == (void*)0xffff8000feed50f0) + asm volatile("nop"); + // logger::debug("Attempt free of 0x%p\n", mem); + // if (!s_uACPIAllocatorInitialized) + // logger::panic(nullptr, "Function %s, line %d: free before uACPI allocator is initialized detected. This is a bug, please report in some way.\n", + // __func__, __LINE__); + size_t sz = allocators::g_kAllocator->QueryObjectSize(mem); + if (sz == SIZE_MAX) + logger::panic(nullptr, "Function %s, line %d: free of object by uACPI not allocated by uACPI allocator. This is a bug, please report in some way.\n", + __func__, __LINE__); + delete[] (uint8_t*)mem; + // s_uACPIAllocator.Free(mem, sz); + //logger::debug("Freed 0x%p.\n", mem); + } + void uacpi_kernel_log(enum uacpi_log_level level, const char* format, ...) + { + va_list list; + va_start(list, format); + uacpi_kernel_vlog(level, format, list); + va_end(list); + } + void uacpi_kernel_vlog(enum uacpi_log_level level, const char* format, uacpi_va_list list) + { + const char* prefix = "UNKNOWN"; + switch (level) + { + case UACPI_LOG_TRACE: + prefix = "TRACE"; + break; + case UACPI_LOG_INFO: + prefix = "INFO"; + break; + case UACPI_LOG_WARN: + prefix = "WARN"; + break; + case UACPI_LOG_ERROR: + prefix = "ERROR"; + break; + default: + break; + } + logger::printf("uACPI, %s: ", prefix); + logger::vprintf(format, list); + } + // 2500 + uacpi_u64 uacpi_kernel_get_ticks(void) + { + return scheduler::g_ticks * 2500; // 4000 hz -> 250000 ns = 2500 ticks. + } + void *uacpi_kernel_map(uacpi_phys_addr addr, uacpi_size) + { +#ifdef __x86_64__ + return MapToHHDM(addr); +#endif + } + void uacpi_kernel_unmap(void *, uacpi_size ) + { /* Does nothing. */} + uacpi_handle uacpi_kernel_create_spinlock(void) + { + return new bool{}; + } + void uacpi_kernel_free_spinlock(uacpi_handle hnd) + { + delete (bool*)hnd; + } + uacpi_cpu_flags uacpi_kernel_spinlock_lock(uacpi_handle hnd) + { + volatile bool* lock = (volatile bool*)hnd; + uacpi_cpu_flags irql = 0; + RaiseIRQL(0xf, &irql); + const bool excepted = false; + while (__atomic_load_n(lock, __ATOMIC_SEQ_CST)) + spinlock_delay(); + while (__atomic_compare_exchange_n(lock, (bool*)&excepted, true, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) + spinlock_delay(); + return irql; + } + void uacpi_kernel_spinlock_unlock(uacpi_handle hnd, uacpi_cpu_flags oldIrql) + { + volatile bool* lock = (bool*)hnd; + __atomic_store_n(&lock, false, __ATOMIC_SEQ_CST); + LowerIRQL(oldIrql); + } + uacpi_handle uacpi_kernel_create_event(void) + { + return new size_t{}; + } + void uacpi_kernel_free_event(uacpi_handle e) + { + delete (size_t*)e; + } + + uacpi_bool uacpi_kernel_wait_for_event(uacpi_handle _e, uacpi_u16 t) + { + volatile size_t* e = (size_t*)_e; + if (t == 0xffff) + { + while (*e > 0); + return UACPI_TRUE; + } + t *= 4; + uint64_t wakeTime = scheduler::g_ticks + t; + while (*e > 0 && scheduler::g_ticks >= wakeTime); + bool ret = *e > 0; + *e -= ret; + return ret; + } + void uacpi_kernel_signal_event(uacpi_handle _e) + { + volatile size_t* e = (size_t*)_e; + __atomic_fetch_add(e, 1, __ATOMIC_SEQ_CST); + } + void uacpi_kernel_reset_event(uacpi_handle _e) + { + volatile size_t* e = (size_t*)_e; + __atomic_store_n(e, 0, __ATOMIC_SEQ_CST); + } + struct io_range + { + uacpi_io_addr base; + uacpi_size len; + }; + uacpi_status uacpi_kernel_io_map(uacpi_io_addr base, uacpi_size len, uacpi_handle *out_handle) + { + if (base > 0xffff) + return UACPI_STATUS_INVALID_ARGUMENT; + io_range* rng = new io_range{ base, len }; + *out_handle = (uacpi_handle)rng; + return UACPI_STATUS_OK; + } + void uacpi_kernel_io_unmap(uacpi_handle handle) + { + delete (io_range*)handle; + } + uacpi_status uacpi_kernel_io_read( + uacpi_handle hnd, uacpi_size offset, + uacpi_u8 byte_width, uacpi_u64 *value + ) + { + io_range* rng = (io_range*)hnd; + if (offset >= rng->len) + return UACPI_STATUS_INVALID_ARGUMENT; + return uacpi_kernel_raw_io_read(rng->base + offset, byte_width, value); + } + uacpi_status uacpi_kernel_io_write( + uacpi_handle hnd, uacpi_size offset, + uacpi_u8 byte_width, uacpi_u64 value + ) + { + io_range* rng = (io_range*)hnd; + if (offset >= rng->len) + return UACPI_STATUS_INVALID_ARGUMENT; + return uacpi_kernel_raw_io_write(rng->base + offset, byte_width, value); + } + struct mutex + { + volatile bool locked = false; + // If nullptr, locked. + scheduler::Thread* owner = nullptr; + }; + uacpi_handle uacpi_kernel_create_mutex(void) + { + return new mutex{}; + } + void uacpi_kernel_free_mutex(uacpi_handle hnd) + { + delete (mutex*)hnd; + } + uacpi_bool uacpi_kernel_acquire_mutex(uacpi_handle hnd, uacpi_u16 t) + { + mutex *mut = (mutex*)hnd; + uint64_t wakeTime = 0; + if (t != 0xffff) + wakeTime = scheduler::g_ticks + t * 4; + else + wakeTime = 0xffff'ffff'ffff'ffff; + const bool expected = false; + while (__atomic_load_n(&mut->locked, __ATOMIC_SEQ_CST)) + spinlock_delay(); + while (__atomic_compare_exchange_n(&mut->locked, (bool*)&expected, true, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) && scheduler::g_ticks < wakeTime) + spinlock_delay(); + mut->owner = scheduler::GetCPUPtr()->currentThread; + return UACPI_TRUE; + } + void uacpi_kernel_release_mutex(uacpi_handle hnd) + { + mutex *mut = (mutex*)hnd; + if (mut->owner != scheduler::GetCPUPtr()->currentThread) + { + logger::debug("Failed release of mutex %p. Owner != currentThread\n", hnd); + return; + } + mut->locked = false; + } + uacpi_status uacpi_kernel_handle_firmware_request(uacpi_firmware_request* req) + { + switch (req->type) + { + case UACPI_FIRMWARE_REQUEST_TYPE_BREAKPOINT: + break; + case UACPI_FIRMWARE_REQUEST_TYPE_FATAL: + logger::panic(nullptr, + "Your bios fucked up, so now you have to deal with the consequences, also known as possible data loss. Firmware Error Code: 0x%016x, argument: %016lx\n", + req->fatal.code, req->fatal.arg); + break; + default: + break; + } + return UACPI_STATUS_OK; + } + #if defined(__x86_64__) || defined(_WIN64) +#pragma GCC push_options +#pragma GCC target("sse") + uint64_t calibrateHPET(uint64_t freq); + void uacpi_kernel_stall(uacpi_u8 usec) + { + // This (hopefully) should be enough context saved. + volatile uint8_t fpuState[512] = {}; + asm volatile("fxsave (%0)" : : "r"(fpuState):); + for (uint64_t comparatorValue = calibrateHPET(1/(usec/1000000.f)); arch::g_hpetAddress->mainCounterValue < comparatorValue;) + pause(); + asm volatile("fxrstor (%0)" : : "r"(fpuState):); + } + void uacpi_kernel_sleep(uacpi_u64 msec) + { + // This (hopefully) should be enough context saved. + volatile uint8_t fpuState[512] = {}; + asm volatile("fxsave (%0)" : : "r"(fpuState):); + for (uint64_t comparatorValue = calibrateHPET(1.f/((float)msec)*1000.f); arch::g_hpetAddress->mainCounterValue < comparatorValue;) + pause(); + asm volatile("fxrstor (%0)" : : "r"(fpuState):); + } +#pragma GCC pop_options +#else +#error Implement uacpi_kernel_stall for the current architecture! +#endif + uacpi_status uacpi_kernel_install_interrupt_handler( + uacpi_u32 irq, uacpi_interrupt_handler handler, uacpi_handle ctx, + uacpi_handle *out_irq_handle + ) + { + Irq *irqHnd = new Irq{ IRQL_GPE, false, true }; + // Ask the IOAPIC (nicely) to register the irq to our vector. +#if defined(__x86_64__) + if (!IOAPIC_MapIRQToVector(irq, irqHnd->GetVector() + 0x20, true, triggerMode::LevelSensitive)) + return UACPI_STATUS_INVALID_ARGUMENT; +#endif + uintptr_t *udata = new uintptr_t[2]; + udata[0] = (uintptr_t)ctx; + udata[1] = (uintptr_t)handler; + irqHnd->SetHandler([](const Irq*, const struct IrqVector*, void* udata, interrupt_frame*) { + uacpi_handle ctx = *((void**)udata); + uacpi_interrupt_handler handler = (uacpi_interrupt_handler)((void**)udata)[1]; + handler(ctx); + }, udata); + irqHnd->SetIRQChecker([](const Irq*, const struct IrqVector*, void*) { return true; /* Assume this is a GPE (see comment for IRQL_GPE in irql.h */ }, nullptr); + *out_irq_handle = irqHnd; + return UACPI_STATUS_OK; + } + uacpi_status uacpi_kernel_uninstall_interrupt_handler( + uacpi_interrupt_handler, uacpi_handle irq_handle + ) + { + Irq* irqHnd = (Irq*)irq_handle; + delete[] (uintptr_t*)irqHnd->GetHandlerUserdata(); + delete irqHnd; + return UACPI_STATUS_OK; + } + struct uacpi_work + { + uacpi_work_type type; + uacpi_work_handler cb; + uacpi_handle ctx; + scheduler::Thread* dpc; + + uacpi_work *next, *prev; + }; + static uacpi_work *s_workHead = nullptr, *s_workTail = nullptr; + static size_t s_nWork = 0; + static locks::SpinLock s_workQueueLock; + static bool s_isWorkQueueLockInit = false; + uacpi_status uacpi_kernel_schedule_work(uacpi_work_type type, uacpi_work_handler cb, uacpi_handle ctx) + { + if (!s_isWorkQueueLockInit) + new (&s_workQueueLock) locks::SpinLock{}; + // Make the work object. + uacpi_work* work = new uacpi_work{ type, cb, ctx }; + s_workQueueLock.Lock(); + if(!s_workHead) + s_workHead = work; + if (s_workTail) + s_workTail->next = work; + work->prev = s_workTail; + s_workTail = work; + s_nWork++; + s_workQueueLock.Unlock(); + // Make the DPC. + using scheduler::Thread; + scheduler::cpu_local *on = nullptr; + if (type == UACPI_WORK_GPE_EXECUTION) + { + // Find the BSP in the cpu info list. + for (size_t i = 0; i < scheduler::g_nCPUs && !on; i++) + if (scheduler::g_cpuInfo[i].isBSP) + on = &scheduler::g_cpuInfo[i]; + } + else + on = scheduler::GetCPUPtr(); + Thread* dpc = new Thread{}; + work->dpc = dpc; + dpc->tid = scheduler::g_nextTID++; + dpc->status = scheduler::ThreadStatus::CanRun; + dpc->flags = scheduler::ThreadFlags::IsDeferredProcedureCall; + dpc->priority = scheduler::ThreadPriority::High; + dpc->affinity = dpc->ogAffinity = (1 << scheduler::GetCPUPtr()->cpuId); + dpc->addressSpace = &vmm::g_kernelContext; + arch::SetupThreadContext(&dpc->context, /* The thread's context */ + &dpc->thread_stack, /* The thread's stack info */ + (uintptr_t)(void(*)(uacpi_work*))[](uacpi_work* work) { + // Run the work. + work->cb(work->ctx); + // Remove the work from the queue. + s_workQueueLock.Lock(); + if (work->next) + work->next->prev = work->prev; + if (work->prev) + work->prev->next = work->next; + if (s_workTail == work) + s_workTail = work->prev; + if (s_workHead == work) + s_workHead = work->next; + s_nWork--; + s_workQueueLock.Unlock(); + // Kill ourself. + scheduler::ExitCurrentThread(); + while(1); + }, /* The thread's entry */ + (uintptr_t)work, /* The thread's first parameter */ + false, /* The thread's ring level */ + 0x8000, /* The thread's stack size */ + dpc->addressSpace /* The thread's address space */ + ); + on->dpcList.Append(dpc); + return UACPI_STATUS_OK; + } + uacpi_status uacpi_kernel_wait_for_work_completion(void) + { + while (s_nWork > 0) + spinlock_delay(); + return UACPI_STATUS_OK; + } + + + // uacpi_stdlib + void *uacpi_memcpy(void *dest, const void* src, size_t sz) + { + return memcpy(dest,src,sz); + } + void *uacpi_memset(void *dest, int src, size_t cnt) + { + return memset(dest, src, cnt); + } + int uacpi_memcmp(const void *src1, const void *src2, size_t cnt) + { + const uint8_t* b1 = (const uint8_t*)src1; + const uint8_t* b2 = (const uint8_t*)src2; + for (size_t i = 0; i < cnt; i++) + if (b1[i] < b2[i]) + return -1; + else if (b1[i] > b2[i]) + return 1; + else + continue; + return 0; + } + int uacpi_strncmp(const char *src1, const char *src2, size_t maxcnt) + { + size_t len1 = uacpi_strnlen(src1, maxcnt); + size_t len2 = uacpi_strnlen(src2, maxcnt); + if (len1 < len2) + return -1; + else if (len1 > len2) + return 1; + return uacpi_memcmp(src1, src2, len1); + } + int uacpi_strcmp(const char *src1, const char *src2) + { + size_t len1 = uacpi_strlen(src1); + size_t len2 = uacpi_strlen(src2); + if (len1 < len2) + return -1; + else if (len1 > len2) + return 1; + return uacpi_memcmp(src1, src2, len1); + } + void *uacpi_memmove(void *dest, const void* src, size_t len) + { + if (src == dest) + return dest; + // Refactored from https://stackoverflow.com/a/65822606 + uint8_t *dp = (uint8_t *)dest; + const uint8_t *sp = (uint8_t *)src; + if(sp < dp && sp + len > dp) + { + sp += len; + dp += len; + while(len-- > 0) + *--dp = *--sp; + } + else + while(len-- > 0) + *dp++ = *sp++; + return dest; + } + size_t uacpi_strnlen(const char *src, size_t maxcnt) + { + size_t i = 0; + for (; i < maxcnt && src[i]; i++); + return i; + } + size_t uacpi_strlen(const char *src) + { + return strlen(src); + } + int uacpi_snprintf(char* dest, size_t n, const char* format, ...) + { + va_list list; + va_start(list, format); + int ret = logger::vsnprintf(dest, n, format, list); + va_end(list); + return ret; + } +} \ No newline at end of file diff --git a/src/libs/uACPI/uacpi_arch_helpers.h b/src/libs/uACPI/uacpi_arch_helpers.h new file mode 100644 index 00000000..52b8f152 --- /dev/null +++ b/src/libs/uACPI/uacpi_arch_helpers.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +#ifdef __x86_64__ +#define UACPI_ARCH_FLUSH_CPU_CACHE() asm volatile ("wbinvd") +#define UACPI_ARCH_DISABLE_INTERRUPTS() asm volatile ("cli") +#else +#error Invalid architecture. +#endif + +typedef uint8_t uacpi_cpu_flags; \ No newline at end of file diff --git a/src/libs/uACPI/uacpi_stdlib.h b/src/libs/uACPI/uacpi_stdlib.h new file mode 100644 index 00000000..ca16f0e3 --- /dev/null +++ b/src/libs/uACPI/uacpi_stdlib.h @@ -0,0 +1,35 @@ +/* + libs/uACPI/uacpi_stdlib.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include + +#define UACPI_PRIx64 "lx" +#define UACPI_PRIX64 "lX" +#define UACPI_PRIu64 "lu" + +#define PRIx64 UACPI_PRIx64 +#define PRIX64 UACPI_PRIX64 +#define PRIu64 UACPI_PRIu64 + +#ifdef __cplusplus +extern "C" { +#endif + void *uacpi_memcpy(void *dest, const void* src, size_t sz); + void *uacpi_memset(void *dest, int src, size_t cnt); + int uacpi_memcmp(const void *src1, const void *src2, size_t cnt); + int uacpi_strncmp(const char *src1, const char *src2, size_t maxcnt); + int uacpi_strcmp(const char *src1, const char *src2); + void *uacpi_memmove(void *dest, const void* src, size_t sz); + size_t uacpi_strnlen(const char *src, size_t maxcnt); + size_t uacpi_strlen(const char *src); + int uacpi_snprintf(char* dest, size_t n, const char* format, ...); +#ifdef __cplusplus +} +#endif + +#define uacpi_offsetof(t, m) ((uintptr_t)(&((t*)0)->m)) \ No newline at end of file diff --git a/src/oboskrnl/CMakeLists.txt b/src/oboskrnl/CMakeLists.txt index dc86bf2c..527fb79e 100644 --- a/src/oboskrnl/CMakeLists.txt +++ b/src/oboskrnl/CMakeLists.txt @@ -1,108 +1,94 @@ -# oboskrnl/CMakeLists.txt - -# Copyright (c) 2024-2026 Omar Berrow - -list (APPEND oboskrnl_sources - "klog.c" "locks/spinlock.c" "irq/irql.c" "scheduler/schedule.c" - "scheduler/thread.c" "mm/bare_map.c" "allocators/basic_allocator.c" - "text.c" "irq/irq.c" "scheduler/process.c" "power/shutdown.c" - "irq/timer.c" "mm/context.c" "mm/init.c" "mm/swap.c" - "mm/handler.c" "mm/alloc.c" "driver_interface/loader.c" "power/event.c" - "driver_interface/pnp.c" "irq/dpc.c" "locks/mutex.c" "locks/semaphore.c" - "locks/event.c" "locks/wait.c" "cmdline.c" "vfs/init.c" - "vfs/alloc.c" "utils/string.c" "vfs/mount.c" "vfs/dirent.c" - "vfs/fd.c" "vfs/dummy_devices.c" "vfs/tty.c" "mm/pmm.c" - "driver_interface/pci_irq.c" "mbr.c" "gpt.c" "partition.c" - "utils/uuid.c" "mm/disk_swap.c" "memmanip.c" "signal.c" - "mm/aging.c" "mm/initial_swap.c" "locks/pushlock.c" "vfs/pipe.c" - "syscall.c" "handle.c" "scheduler/sched_sys.c" "mm/mm_sys.c" - "mm/fork.c" "power/suspend.c" "power/device.c" "power/init.c" - "driver_interface/pci.c" "utils/shared_ptr.c" "locks/sys_futex.c" "vfs/fd_sys.c" - "elf/load.c" "driver_interface/drv_sys.c" "sig_sys.c" "execve.c" - "init_proc.c" "vfs/create.c" "net/route.c" "net/eth.c" - "net/ip.c" "net/arp.c" "power/event.c" "net/udp.c" - "net/icmp.c" "net/tcp.c" "vfs/socket.c" "net/lo.c" - "kinit.c" "vfs/local_socket.c" "perm.c" "contrib/tjec.c" - "contrib/csprng.c" "vfs/pty.c" "vfs/tmpfs.c" -) - -add_executable(oboskrnl) - -set_source_files_properties("sanitizers/ubsan.c" PROPERTIES COMPILE_OPTIONS "-fvisibility=default") -set_source_files_properties("klog.c" PROPERTIES COMPILE_OPTIONS "-Wno-error") -set_source_files_properties("vfs/mount.c" PROPERTIES COMPILE_OPTIONS "-Wno-unused-function") -set_source_files_properties("${CMAKE_SOURCE_DIR}/dependencies/flanterm/src/flanterm_backends/fb.c" PROPERTIES COMPILE_OPTIONS "-fno-sanitize=kernel-address") -set_source_files_properties("${CMAKE_SOURCE_DIR}/dependencies/flanterm/src/flanterm.c" PROPERTIES COMPILE_OPTIONS "-fno-sanitize=kernel-address") - -target_compile_options(oboskrnl - PRIVATE $<$:${TARGET_COMPILE_OPTIONS_C}> - PRIVATE $<$:-ffreestanding> - PRIVATE $<$:-Wall> - PRIVATE $<$:-Wextra> - PRIVATE $<$:-fstack-protector> - PRIVATE $<$:-fvisibility=hidden> - PRIVATE $<$:-fno-pie> - # https://discord.com/channels/440442961147199490/1141057599584878645/1269032825579901069 - PRIVATE $<$:-Werror=incompatible-pointer-types> - PRIVATE $<$:-Wno-error=address> -) - -if (OBOS_ENABLE_PROFILER) - target_compile_options(oboskrnl PRIVATE $<$:-finstrument-functions>) -endif() - -target_link_libraries(oboskrnl - PRIVATE ${LIBGCC} - PRIVATE sanitizers -) -if (OBOS_ARCHITECTURE_HAS_ACPI) - target_link_libraries(oboskrnl - PRIVATE uacpi - ) -endif() - -set_property(TARGET oboskrnl PROPERTY link_depends ${LINKER_SCRIPT}) - -if (DEFINED OBOS_RELEASE) - set_property(TARGET oboskrnl PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) -endif() - -target_include_directories(oboskrnl - PRIVATE "${CMAKE_SOURCE_DIR}/src/oboskrnl" - PRIVATE "${CMAKE_SOURCE_DIR}/src/drivers" - PRIVATE "${CMAKE_SOURCE_DIR}/src/sanitizers" - PRIVATE ${OBOSKRNL_EXTERNAL_INCLUDES} -) - -target_link_options(oboskrnl - PRIVATE "-nostdlib" - PRIVATE "-fno-pie" - PRIVATE "-Wl,--export-dynamic" - PRIVATE "-T" PRIVATE ${LINKER_SCRIPT} - PRIVATE ${TARGET_LINKER_OPTIONS} -) - -target_compile_definitions(oboskrnl - PRIVATE FIXEDPT_BITS=${OBOS_ARCHITECTURE_BITS} - PRIVATE OBOS_KERNEL=1 - PRIVATE "FIXEDPT_WBITS=(${OBOS_ARCHITECTURE_BITS}-8)" -) - -target_sources(oboskrnl PRIVATE ${oboskrnl_sources}) - -set_source_files_properties( - "arch/m68k/entry.c" - PROPERTIES - OBJECT_DEPENDS "${OUTPUT_DIR}/initrd" -) - -if (OBOS_ENABLE_UHDA) - target_link_libraries(oboskrnl PRIVATE uhda) -endif() - -include("${CMAKE_SOURCE_DIR}/src/isogen/isogen.cmake") - -include(GNUInstallDirs) - -install(FILES ${CMAKE_SOURCE_DIR}/src/oboskrnl/vfs/keycode.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/obos/) -install(FILES ${CMAKE_SOURCE_DIR}/src/oboskrnl/vfs/mouse.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/obos/) \ No newline at end of file +# oboskrnl/CMakeLists.txt + +# Copyright (c) 2024 Omar Berrow + +set (oboskrnl_sources ${oboskrnl_sources} + "console.cpp" "locks/spinlock.cpp" "klog.cpp" "irq/irql.cpp" + "vmm/pg_context.cpp" "vmm/map.cpp" "allocators/allocator.cpp" "allocators/basic_allocator.cpp" + "vmm/init.cpp" "vmm/demand_paging.cpp" "vmm/mprot.cpp" "irq/irq.cpp" + "scheduler/init.cpp" "scheduler/thread.cpp" "scheduler/scheduler.cpp" "kmain.cpp" + "vfs/open.cpp" "vfs/cwd.cpp" +) + +add_executable(oboskrnl) + +set_target_properties(oboskrnl PROPERTIES CXX_STANDARD 20) +set_target_properties(oboskrnl PROPERTIES CXX_STANDARD_REQUIRED 20) + +set_target_properties(oboskrnl PROPERTIES LINK_DEPENDS "${LINKER_SCRIPT}") + +target_include_directories(oboskrnl PRIVATE "${CMAKE_SOURCE_DIR}/src/oboskrnl") +target_include_directories(oboskrnl PRIVATE ${OBOSKRNL_EXTERNAL_INCLUDES}) + +target_compile_options(oboskrnl + PRIVATE $<$:${TARGET_COMPILE_OPTIONS_CPP}> + PRIVATE $<$:${TARGET_COMPILE_OPTIONS_C}> + PRIVATE $<$:-fno-rtti> + PRIVATE $<$:-fno-exceptions> + PRIVATE $<$:-fno-stack-protector> + PRIVATE $<$:-fno-stack-check> + # PRIVATE $<$:-flto> + PRIVATE $<$:-fno-use-cxa-atexit> + PRIVATE $<$:-fno-threadsafe-statics> + PRIVATE $<$:-std=gnu++20> + PRIVATE $<$:-ffreestanding> + PRIVATE $<$:-fstack-protector-strong> + PRIVATE $<$:-Wstack-usage=65536> +# PRIVATE $<$:-nostdlib> +# We don't enable -Werror in case future compiler versions introduce new warnings that the kernel triggers. + PRIVATE $<$:-Wall> + PRIVATE $<$:-Wextra> + PRIVATE $<$:-Wno-missing-field-initializers> + PRIVATE $<$:-g> +) +target_compile_definitions(oboskrnl + PRIVATE $<$:OBOS_DEBUG> + PRIVATE $<$:OBOS_RELEASE>) +target_link_options(oboskrnl + PUBLIC "-T" PUBLIC "${LINKER_SCRIPT}" + PUBLIC "-nostdlib" + PUBLIC ${TARGET_LINKER_OPTIONS} +) + +if (DEFINED OBOS_ENABLE_KASAN) + target_compile_options(oboskrnl PRIVATE $<$:-fsanitize=kernel-address>) + list(APPEND oboskrnl_sources "sanitizers/asan.cpp") + set (OBOS_ENABLE_KASAN "1") +else() + set (OBOS_ENABLE_KASAN "0") +endif() +target_compile_definitions(oboskrnl PRIVATE OBOS_KASAN_ENABLED=${OBOS_ENABLE_KASAN}) +if (DEFINED OBOS_ENABLE_UBSAN) + target_compile_options(oboskrnl PRIVATE $<$:-fsanitize=undefined>) + list(APPEND oboskrnl_sources "sanitizers/ubsan.cpp") + set (OBOS_ENABLE_UBSAN "1") +else() + set (OBOS_ENABLE_UBSAN "0") +endif() +target_compile_definitions(oboskrnl PRIVATE OBOS_UBSAN_ENABLED=${OBOS_ENABLE_UBSAN}) + +target_link_libraries(oboskrnl PRIVATE uacpi PRIVATE ${LIBGCC}) + +if (DEFINED OBOS_SLOW_ARTHIMETRIC) + target_compile_definitions(oboskrnl PUBLIC OBOS_SLOW_ARTHIMETRIC=1) +endif() +if (OBOS_ARCHITECTURE STREQUAL "x86_64") + if (DEFINED OBOS_ENABLE_KDBG) + target_compile_definitions(oboskrnl PUBLIC OBOS_KDBG_ENABLED=1 PUBLIC ZYAN_NO_LIBC=1) + if (DEFINED OBOS_KDBG_ENABLE_ZYDIS) + list (APPEND oboskrnl_sources ${zydis_c}) + target_compile_definitions(oboskrnl PUBLIC OBOS_KDBG_ENABLE_ZYDIS=1) + else() + target_compile_definitions(oboskrnl PUBLIC OBOS_KDBG_ENABLE_ZYDIS=0) + endif() + else() + target_compile_definitions(oboskrnl PUBLIC OBOS_KDBG_ENABLED=0) + endif() +endif() +if (OBOS_ARCHITECTURE_BITS EQUAL 64) + list(APPEND oboskrnl_sources "driver_interface/loader64.cpp") +else() + message(SEND_ERROR "Unimplemented: OBOS_ARCHITECTURE_BITS=32") +endif() + +target_sources(oboskrnl PRIVATE ${oboskrnl_sources}) \ No newline at end of file diff --git a/src/oboskrnl/allocators/allocator.cpp b/src/oboskrnl/allocators/allocator.cpp new file mode 100644 index 00000000..aa65ab2c --- /dev/null +++ b/src/oboskrnl/allocators/allocator.cpp @@ -0,0 +1,96 @@ +/* + oboskrnl/allocators/allocator.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include +#include +#include + +#include + +namespace obos +{ + namespace allocators + { + Allocator* g_kAllocator; + void* Allocator::ZeroAllocate(size_t size) + { + if (!size) + return nullptr; + size_t sizeBytes = GetAllocationSize(), nBytes = size; + if (sizeBytes) + nBytes *= sizeBytes; + void* ptr = Allocate(size); + if (!ptr) + return nullptr; + return memzero(ptr, nBytes); + } + Allocator::~Allocator() + { + OBOS_ASSERTP(g_kAllocator != this, "General kernel allocator object was destroyed. If this is expected, set g_kAllocator to something else, or nullptr for no default allocator (breaks all operator new/delete calls) before destruction.\n"); + } + } +} +using obos::allocators::g_kAllocator; +void* operator new(size_t count) noexcept +{ + if (!g_kAllocator) + return nullptr; + if (!g_kAllocator->GetAllocationSize()) + return g_kAllocator->Allocate(count); + const size_t allocSize = g_kAllocator->GetAllocationSize(); + count = (count / allocSize) + ((count % allocSize) != 0); + return g_kAllocator->Allocate(count); +} +void* operator new[](size_t count) noexcept +{ + return operator new(count); +} +void operator delete (void* ptr) noexcept +{ + if (!g_kAllocator) + return; + if (!ptr) + return; + const size_t count = g_kAllocator->QueryObjectSize(ptr); + if (!count) + return; + g_kAllocator->Free(ptr, count); +} +void operator delete[](void* ptr) noexcept +{ + if (!g_kAllocator) + return; + if (!ptr) + return; + const size_t size = g_kAllocator->QueryObjectSize(ptr); + if (size == SIZE_MAX) + return; + g_kAllocator->Free(ptr, size); +} +void operator delete (void* ptr, size_t count) noexcept +{ + if (!g_kAllocator) + return; + const size_t allocSize = g_kAllocator->GetAllocationSize(); + if (!allocSize) + { + g_kAllocator->Free(ptr, count); + return; + } + count = (count / allocSize) + ((count % allocSize) != 0); + g_kAllocator->Free(ptr, count); +} +void operator delete[](void* ptr, size_t size) noexcept +{ + if (!g_kAllocator) + return; + operator delete(ptr, size); +} + +[[nodiscard]] void* operator new(size_t, void* ptr) noexcept { return ptr; } +[[nodiscard]] void* operator new[](size_t, void* ptr) noexcept { return ptr; } +void operator delete(void*, void*) noexcept {} +void operator delete[](void*, void*) noexcept {} \ No newline at end of file diff --git a/src/oboskrnl/allocators/allocator.h b/src/oboskrnl/allocators/allocator.h new file mode 100644 index 00000000..0a930e3a --- /dev/null +++ b/src/oboskrnl/allocators/allocator.h @@ -0,0 +1,74 @@ +/* + oboskrnl/allocators/allocator.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include + +namespace obos +{ + namespace allocators + { + // Defines the general structure of an allocator. + class Allocator + { + public: + Allocator() = default; + + /// + /// Allocates memory. + /// + /// The size of the region. If the allocator is fixed-size, this should be treated as the count of objects to allocate. + /// A pointer to the block of memory allocated, or nullptr on failure. + virtual void* Allocate(size_t size) = 0; + /// + /// Allocates memory, then zeroes it. + /// + /// The size of the region. Refer to Allocate for more information. + /// A pointer to the block of memory allocated, or nullptr on failure. + virtual void* ZeroAllocate(size_t size); + /// + /// Reallocates a region of memory. The allocator is not required to support this. + /// If newSize == 0, the allocator shall Free() the region returning nullptr. + /// If base == nullptr, the allocator shall Allocate() the region. + /// + /// The base of the region. + /// The new size. Refer to Allocate() for more information. + /// A pointer to the new block. The function returns nullptr on failure, UINTPTR_MAX if ReAllocate is unsupported. Any other pointer should be assumed to be the new block, even if it is the same passed to the function. + virtual void* ReAllocate(void* base, size_t newSize) { (base = base); (newSize = newSize); return nullptr; }; + /// + /// Frees a region of memory. It is undefined behaviour to use the region after it is freed, and should not be done at all. + /// + /// The base of the region to free. + /// The size of the region to free. Refer to Allocate() for more information. This can be zero if the allocator doesn't need the size to free objects, but it would be better if you always pass in the size. If you don't know the size, use QueryObjectSize. + virtual void Free(void* base, size_t size) = 0; + + /// + /// Queries the size of an object. This does not need to be the same size passed to Allocate, as the allocator might add padding. + /// + /// The object. + /// On success, see the size parameter of Allocate() for information on the return value. On failure, it returns SIZE_MAX. + virtual size_t QueryObjectSize(const void* base) = 0; + + /// + /// Gets the allocation size of the allocator. + /// + /// The allocation size, or zero if the allocator does not use fixed-sized allocations. + virtual size_t GetAllocationSize() = 0; + + /// + /// Optimizes the allocator's data structures, so that it runs faster, takes less memory, or has a higher success rate of providing a free block of memory. + /// This can, for example, in a free list allocator, make it so that all continuous free regions are continuous in the free list. + /// + virtual void OptimizeAllocator() {}; + + virtual void* operator()(size_t size = 1) { return Allocate(size); } + + virtual ~Allocator(); + }; + extern Allocator* g_kAllocator; + } +} \ No newline at end of file diff --git a/src/oboskrnl/allocators/base.h b/src/oboskrnl/allocators/base.h deleted file mode 100644 index 4645996f..00000000 --- a/src/oboskrnl/allocators/base.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - * oboskrnl/allocators/base.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include - -// Defines a struct that defines the base info and interfaces for an allocator. - -typedef struct allocator_info -{ - /// - /// Identifies the allocator used. - /// - uint64_t magic; - /// - /// Allocates memory. - /// - /// The allocator object used. - /// The amount of bytes to allocate. - /// [out,optional] The status code of the function. - /// A pointer to the block allocated, or nullptr on failure. - void* (*Allocate)(struct allocator_info* This, size_t nBytes, obos_status* status); - /// - /// Does the same as Allocate, except it also zeroes the memory block returned. - /// - /// The allocator object used. - /// The amount of objects to allocate. - /// The size of each object. - /// [out,optional] The status code of the function. - /// A pointer to the block allocated, or nullptr on failure. - void* (*ZeroAllocate)(struct allocator_info* This, size_t nObjects, size_t bytesPerObject, obos_status* status); - /// - /// Reallocates a previously allocated block of memory. - /// If nBytes==0, the function shall call Free() on the block. - /// If base==nullptr, the function shall Allocate() the byte count passed. - /// - /// The allocator object used. - /// The base of the other allocated block of memory. - /// The new byte count. - /// The old size, if required by the allocator. - /// [out,optional] The status code of the function. - /// The new block, or nullptr on failure. This function is allowed to return the same base passed. - void* (*Reallocate)(struct allocator_info* This, void* base, size_t nBytes, size_t old_size, obos_status* status); - /// - /// Frees a previously allocated block of memory. - /// - /// The allocator object used. - /// The base of the memory - /// The size of the memory block (could be optional depending on the allocator) - /// The status of the function. - obos_status (*Free)(struct allocator_info* This, void* base, size_t nBytes); - /// - /// Queries the size of an allocated block. - /// - /// The allocator object used. - /// The base of the region. - /// [out] The size of the block. - /// The status of the function. - obos_status(*QueryBlockSize)(struct allocator_info* This, void* base, size_t* nBytes); -} allocator_info; - -OBOS_EXPORT obos_status Free(struct allocator_info* This, void* base, size_t nBytes); -#ifndef __clang__ -OBOS_EXPORT __attribute__((malloc, malloc(Free, 2), returns_nonnull)) void* Allocate(struct allocator_info* This, size_t nBytes, obos_status* status); -OBOS_EXPORT __attribute__((malloc, malloc(Free, 2), returns_nonnull)) void* ZeroAllocate(struct allocator_info* This, size_t nObjects, size_t bytesPerObject, obos_status* status); -#else -OBOS_EXPORT void* Allocate(struct allocator_info* This, size_t nBytes, obos_status* status); -OBOS_EXPORT void* ZeroAllocate(struct allocator_info* This, size_t nObjects, size_t bytesPerObject, obos_status* status); -#endif -OBOS_EXPORT void* Reallocate(struct allocator_info* This, void* base, size_t nBytes, size_t old_size, obos_status* status); -OBOS_EXPORT obos_status QueryBlockSize(struct allocator_info* This, void* base, size_t* nBytes); - -extern OBOS_EXPORT allocator_info* OBOS_KernelAllocator; -extern OBOS_EXPORT allocator_info* OBOS_NonPagedPoolAllocator; diff --git a/src/oboskrnl/allocators/basic_allocator.c b/src/oboskrnl/allocators/basic_allocator.c deleted file mode 100644 index 7f972581..00000000 --- a/src/oboskrnl/allocators/basic_allocator.c +++ /dev/null @@ -1,474 +0,0 @@ -/* - * oboskrnl/allocators/basic_allocator.c - * - * Copyright (c) 2024-2025 Omar Berrow - */ - -#include -#include -#include -#include - -#include -#include - -#include -#include -#include -#include -#include - -#include - -#include - -#include - -#if __x86_64__ -# include -#elif defined(__m68k__) -# include -#endif - -// static OBOS_NO_KASAN uintptr_t round_up(uintptr_t x, size_t to) -// { -// if (x % to) -// return x + (to - (x % to)); -// return x; -// } - -static OBOS_NO_KASAN void set_status(obos_status* p, obos_status to) -{ - if (p) - *p = to; -} - -#define makeSafeLock(name, This) struct safe_spinlock name; name.lock = &((This)->lock); Lock(&name); -static OBOS_NO_KASAN void* init_mmap(size_t size, basic_allocator* This, enum blockSource* blockSource) -{ - OBOS_ASSERT(blockSource); - *blockSource = BLOCK_SOURCE_INVALID; - if (obos_expect(Mm_IsInitialized(), true)) - { - // Use the VMA, unless this is the vmm allocator. - if ((allocator_info*)This == Mm_Allocator) - { - // If this calculation changes, update freeRegion - size_t nPages = size / OBOS_PAGE_SIZE; - if (size % OBOS_PAGE_SIZE) - nPages++; - uintptr_t phys = Mm_AllocatePhysicalPages(nPages, 1, nullptr); - if (!phys) - return nullptr; - // Arch-specific: - // Map the physical address to virtual addresses without the need of page nodes. - // On x86-64, this is done using the HHDM. - void* ret = nullptr; -#ifdef __x86_64__ - *blockSource = BLOCK_SOURCE_PHYSICAL_MEMORY; - ret = Arch_MapToHHDM(phys); -#elif defined(__m68k__) - *blockSource = BLOCK_SOURCE_PHYSICAL_MEMORY; - ret = Arch_MapToHHDM(phys); -#else -# error Unknown architecture -#endif - memzero(ret, size); - return ret; - } - - void* ret = nullptr; - // if (size < OBOS_HUGE_PAGE_SIZE) - ret = Mm_QuickVMAllocate(size, (void*)This == (void*)OBOS_NonPagedPoolAllocator); - // else - // ret = Mm_VirtualMemoryAlloc(&Mm_KernelContext, - // nullptr, size, - // 0, - // (((void*)This == (void*)OBOS_NonPagedPoolAllocator) ? VMA_FLAGS_NON_PAGED : 0) | VMA_FLAGS_HUGE_PAGE, - // nullptr, - // nullptr); - if (!ret) - return nullptr; - memzero(ret, size); - *blockSource = BLOCK_SOURCE_VMA; - return ret; - } - void* blk = OBOS_BasicMMAllocatePages(size, nullptr); - if (!blk) - return nullptr; - *blockSource = BLOCK_SOURCE_BASICMM; - blk = (void*)(((uintptr_t)blk + 0xf) & ~0xf); - memzero(blk, size); - return blk; -} - -#define init_pgsize() OBOS_PAGE_SIZE - -static OBOS_NO_KASAN void init_munmap(enum blockSource blockSource, void* block, size_t size) -{ - switch (blockSource) - { - case BLOCK_SOURCE_BASICMM: - { - OBOS_BasicMMFreePages(block, size); - break; - } - case BLOCK_SOURCE_VMA: - { - Mm_VirtualMemoryFree(&Mm_KernelContext, block, size); - break; - } - case BLOCK_SOURCE_PHYSICAL_MEMORY: - { - uintptr_t phys = 0; -#ifdef __x86_64__ - phys = Arch_UnmapFromHHDM(block); -#elif defined(__m68k__) - phys = Arch_UnmapFromHHDM(block); -#else -# error Unknown architecture -#endif - size_t nPages = size / OBOS_PAGE_SIZE; - if (size % OBOS_PAGE_SIZE) - nPages++; -// memset(block, 0xcc, size); - Mm_FreePhysicalPages(phys, nPages); - break; - } - default: - OBOS_Panic(OBOS_PANIC_ALLOCATOR_ERROR, "(possible?) Region corruption in region %p. Invalid block source: %d.\n", block, blockSource); - } -} - -#pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak" - -#define ALLOCATOR_ALIGNMENT 0x10 - -#define append_node(list, node) do {\ - if (!(list).head)\ - (list).head = (node);\ - if ((list).tail)\ - (list).tail->next = (node);\ - (node)->prev = (list).tail;\ - (list).tail = (node);\ - (list).nNodes++; \ -} while(0) - -#define remove_node(list, node) do {\ - if ((node)->next)\ - (node)->next->prev = (node)->prev;\ - if ((node)->prev)\ - (node)->prev->next = (node)->next;\ - if ((list).head == (node))\ - (list).head = (node)->next;\ - if ((list).tail == (node))\ - (list).tail = (node)->prev;\ - (list).nNodes--;\ -} while(0) - -#if 1 -OBOS_NO_KASAN irql lock(cache* c) -{ - return Core_SpinlockAcquire(&c->lock); -} -OBOS_NO_KASAN void unlock(cache* c, irql oldIrql) -{ - Core_SpinlockRelease(&c->lock, oldIrql); -} -#else -irql lock(cache* c) -{ - // return Core_SpinlockAcquire(&c->lock); - return 0; -} -void unlock(cache* c, irql oldIrql) -{ - // Core_SpinlockRelease(&c->lock, oldIrql); -} -#endif - -static OBOS_NO_KASAN int allocate_region(basic_allocator* alloc, cache* c, size_t cache_index) -{ - size_t sz = 1 << (cache_index+4); - size_t sz_node = sz; - if (sz < init_pgsize()) - sz = init_pgsize()*8; - void* reg = init_mmap(sz, alloc, &alloc->blkSource); - if (!reg) - return 0; -#if OBOS_KASAN_ENABLED - memzero(reg, sz); -#endif - - if (sz_node == sz) - append_node(c->clean, (freelist_node*)reg); - else - { - size_t nBlocks = (sz/sz_node); - for (size_t i = 0; i < nBlocks; i++) - { - freelist_node* node = (freelist_node*)((uintptr_t)reg + i*sz_node); - append_node(c->clean, node); - } - } - - return 1; -} - -// OBOS_NO_UBSAN OBOS_NO_KASAN void* Allocate(allocator_info* This_, size_t nBytes, obos_status* status) -// { -// void* blk = _Allocate(This_, nBytes, status, false); -// // if (s_enable_alloc_logs) -// // printf("kalloc alloc 0x%p %d 0x%p\n", blk, nBytes, __builtin_return_address(0)); -// return blk; -// } -static OBOS_NO_UBSAN OBOS_NO_KASAN void* DoAllocation(allocator_info* This_, size_t nBytes, obos_status* status, bool zero_out, OBOS_MAYBE_UNUSED void* return_address) -{ - basic_allocator* This = (basic_allocator*)This_; - -#if OBOS_KASAN_ENABLED - nBytes += 32; -#endif - -#if OBOS_DEBUG_FREE_SIZE - nBytes += (sizeof(uintptr_t)*2); -#endif - - OBOS_MAYBE_UNUSED size_t unrounded_nBytes = nBytes; - - if (nBytes <= 16) - nBytes = 16; - else - nBytes = (size_t)1 << (64-__builtin_clzll(nBytes)); - - OBOS_ASSERT(nBytes >= unrounded_nBytes); - -#if __SIZE_MAX__ > __UINT32_MAX__ - if (obos_expect(nBytes > 0x100000000, false)) - { - if (status) *status = OBOS_STATUS_INVALID_ARGUMENT; - return NULL; // invalid argument - } -#endif - - size_t cache_index = __builtin_ctzll(nBytes)-4; - cache* c = &This->caches[cache_index]; - - irql oldIrql = lock((cache*)c); - - const bool izero_out = zero_out; - freelist* list = &c->clean; - void* ret = c->clean.tail; - zero_out = false; - - if (obos_expect(!ret, true)) - { - ret = c->free.tail; - list = &c->free; - zero_out = izero_out; - } - - if (obos_expect(!ret, false)) - { - if (!allocate_region(This, (cache*)c, cache_index)) - { - unlock((cache*)c, oldIrql); - if (status) *status = OBOS_STATUS_NOT_ENOUGH_MEMORY; - return NULL; // OOM - } - - ret = c->clean.tail; - list = &c->clean; - zero_out = false; - } - - remove_node(*list, list->tail); - - unlock((cache*)c, oldIrql); - -#if OBOS_KASAN_ENABLED - nBytes -= 32; - unrounded_nBytes -= 32; - memset((void*)((uintptr_t)ret+nBytes-32), OBOS_ASANPoisonValues[ASAN_POISON_ALLOCATED], 32); -#endif - -#if OBOS_DEBUG_FREE_SIZE - nBytes -= sizeof(uintptr_t)*2; - unrounded_nBytes -= sizeof(uintptr_t)*2; - ((uintptr_t*)ret)[0] = unrounded_nBytes; - ((void**)ret)[1] = return_address; - ret = (void*)((uintptr_t)ret + (sizeof(uintptr_t)*2)); -#endif - - // if (log_alloc && s_enable_alloc_logs) - // printf("kalloc alloc 0x%p %d 0x%p\n", ret, nBytes, __builtin_return_address(0)); - -#if OBOS_KASAN_ENABLED - zero_out = true; -#endif - if (zero_out) - return memzero(ret, nBytes); - freelist_node* node = ret; - node->next = nullptr; - node->prev = nullptr; - return ret; -} - -#if OBOS_DEBUG_FREE_SIZE -# define get_return_address() __builtin_return_address(0) -#else -# define get_return_address() nullptr -#endif - -OBOS_NO_UBSAN OBOS_NO_KASAN void* Allocate(allocator_info* This_, size_t nBytes, obos_status* status) -{ - return DoAllocation(This_, nBytes, status, false, get_return_address()); -} -OBOS_NO_KASAN void* ZeroAllocate(allocator_info* This, size_t nObjects, size_t bytesPerObject, obos_status* status) -{ - if (!This || This->magic != OBOS_BASIC_ALLOCATOR_MAGIC) - { - set_status(status, OBOS_STATUS_INVALID_ARGUMENT); - return nullptr; - } - return DoAllocation(This, bytesPerObject * nObjects, status, true, get_return_address()); -} - -OBOS_NO_KASAN OBOS_NO_UBSAN void* Reallocate(allocator_info* This_, void* blk, size_t new_size, size_t old_size, obos_status* status) -{ - if (!This_ || This_->magic != OBOS_BASIC_ALLOCATOR_MAGIC) - { - set_status(status, OBOS_STATUS_INVALID_ARGUMENT); - return nullptr; - } - // basic_allocator* alloc = (basic_allocator*)This_; - if (!blk) - return Allocate(This_, new_size, status); - if (!new_size) - { - This_->Free(This_, blk, old_size); - return NULL; - } - do { - size_t nBytes = new_size; - size_t nBytes_old = old_size; - if (nBytes <= 16) - nBytes = 16; - else - nBytes = (size_t)1 << (64-__builtin_clzll(nBytes)); - if (nBytes_old <= 16) - nBytes_old = 16; - else - nBytes_old = (size_t)1 << (64-__builtin_clzll(nBytes_old)); - if (nBytes == nBytes_old) - { -#if OBOS_DEBUG_FREE_SIZE - *((uintptr_t*)((uintptr_t)blk - sizeof(uintptr_t)*2)) = new_size; -#endif - return blk; - } - } while(0); - void* newblk = Allocate(This_, new_size, status); - if (!newblk) - return newblk; - memcpy(newblk, blk, OBOS_MIN(new_size, old_size)); - This_->Free(This_, blk, old_size); - return newblk; -} - -OBOS_NO_KASAN OBOS_NO_UBSAN obos_status Free(allocator_info* This_, void* blk, size_t nBytes) -{ - if (!This_ || This_->magic != OBOS_BASIC_ALLOCATOR_MAGIC) - return OBOS_STATUS_INVALID_ARGUMENT; - - if (!blk || !nBytes) - return OBOS_STATUS_SUCCESS; - - OBOS_MAYBE_UNUSED void* initial_blk = blk; - OBOS_MAYBE_UNUSED size_t initial_nBytes = nBytes; - - OBOS_ASSERT(!((uintptr_t)blk & 0xf)); - -#ifdef OBOS_DEBUG_FREE_SIZE - blk = (void*)((uintptr_t)blk - (sizeof(uintptr_t)*2)); - do { - uintptr_t *debug_info = blk; - if (debug_info[0] != nBytes) - OBOS_Panic(OBOS_PANIC_ALLOCATOR_ERROR, "MISMATCHED ALLOCATION/FREE SIZES! nBytes on alloc: %d, nBytes on free: %d. Block 0x%p allocated by 0x%p\n", debug_info[0], nBytes, blk, debug_info[1]); - } while(0); - nBytes += (sizeof(uint64_t)*2); -#endif - -#if OBOS_DEBUG - if (This_ != Mm_Allocator) - { - page_range key = {.virt=(uintptr_t)blk,.size=nBytes}; - page_range* rng = RB_FIND(page_tree, &Mm_KernelContext.pages, &key); - if (rng) - OBOS_ENSURE(rng->user_view == false && "Attempted Free() of a view of user memory."); - } -#endif - -#if OBOS_KASAN_ENABLED - nBytes += 32; -#endif - - if (nBytes <= 16) - nBytes = 16; - else - nBytes = (size_t)1 << (64-__builtin_clzll(nBytes-1)); - - // if (s_enable_alloc_logs) - // printf("kalloc free 0x%p %d 0x%p\n", initial_blk, initial_nBytes, __builtin_return_address(0)); - -#if __SIZE_MAX__ > __UINT32_MAX__ - if (nBytes > (4UL*1024*1024*1024)) - return OBOS_STATUS_INVALID_ARGUMENT; // invalid argument -#endif - - basic_allocator* alloc = (basic_allocator*)This_; - - size_t cache_index = __builtin_ctzll(nBytes)-4; - cache* c = &alloc->caches[cache_index]; - - memzero(blk, sizeof(freelist_node)); - - // if (obos_expect(nBytes >= init_pgsize(), false)) - // { - // init_munmap(alloc->blkSource, blk, nBytes); - // return OBOS_STATUS_SUCCESS; - // } - - irql oldIrql = lock(c); - append_node(c->free, (freelist_node*)blk); -#if OBOS_KASAN_ENABLED - memset(((freelist_node*)blk)+1, OBOS_ASANPoisonValues[ASAN_POISON_FREED], nBytes-sizeof(freelist_node)); -#elif OBOS_DEBUG - memset(((freelist_node*)blk)+1, 0xde, nBytes-sizeof(freelist_node)); -#endif - unlock(c, oldIrql); - - return OBOS_STATUS_SUCCESS; -} - -OBOS_NO_KASAN OBOS_NO_UBSAN obos_status QueryBlockSize(allocator_info* This, void* base, size_t* nBytes) -{ - if (!This || This->magic != OBOS_BASIC_ALLOCATOR_MAGIC || !nBytes || !base) - return OBOS_STATUS_INVALID_ARGUMENT; - // We don't have the capability to do such things. - *nBytes = 0; - return OBOS_STATUS_SUCCESS; -} - -OBOS_PAGEABLE_FUNCTION obos_status OBOSH_ConstructBasicAllocator(basic_allocator* This) -{ - if (!This) - return OBOS_STATUS_INVALID_ARGUMENT; - This->header.magic = OBOS_BASIC_ALLOCATOR_MAGIC; - This->header.Allocate = Allocate; - This->header.ZeroAllocate = ZeroAllocate; - This->header.Reallocate = Reallocate; - This->header.Free = Free; - This->header.QueryBlockSize = QueryBlockSize; - return OBOS_STATUS_SUCCESS; -} diff --git a/src/oboskrnl/allocators/basic_allocator.cpp b/src/oboskrnl/allocators/basic_allocator.cpp new file mode 100644 index 00000000..5ff21ebe --- /dev/null +++ b/src/oboskrnl/allocators/basic_allocator.cpp @@ -0,0 +1,363 @@ +/* + oboskrnl/allocators/basic_allocator.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include +#include +#include + +#include + +#include + +#include + +#include +#include +#include +#include + +#include +#include + +#if __x86_64__ +# include +#endif + +struct safe_lock +{ + safe_lock() = delete; + safe_lock(obos::locks::SpinLock* lock) + { + m_lock = lock; + } + bool Lock() + { + if (m_lock) + return m_lock->Lock(); + return false; + } + bool IsLocked() + { + if (m_lock) + return m_lock->Locked(); + return false; + } + void Unlock() + { + if (m_lock) + m_lock->Unlock(); + } + ~safe_lock() + { + Unlock(); + } +private: + obos::locks::SpinLock* m_lock = nullptr; +}; + +#define makeSafeLock(vName) safe_lock vName{ &m_lock }; vName.Lock() + +namespace obos +{ + extern uint8_t asan_poison; + namespace allocators + { + constexpr uintptr_t round_up(uintptr_t x, size_t to) + { + if (x % to) + return x + (to-(x%to)); + return x; + } + void *BasicAllocator::Allocate(size_t size) + { + size = round_up(size, 0x10); +#if OBOS_KASAN_ENABLED + size += 0x100 /* add 256 byte shadow space */; +#endif + // First, find a region. + BasicAllocator::region* from = nullptr; + BasicAllocator::region* start = m_regionHead; + tryAgain: + for (auto r = start; r; ) + { + if (!r->free.nNodes || !r->biggestFreeNode) + goto end; + if (r->biggestFreeNode->size >= size) + { + from = r; + break; + } + + end: + r = r->next; + } + if (!from) + from = allocateNewRegion(size); + if (!from) + return nullptr; // uh-oh. + // Then, use that region's first free node with a size big enough to handle this allocation. + BasicAllocator::node *freeNode = nullptr; + for (auto n = from->free.head; n; ) + { + if (n->magic != MEMBLOCK_MAGIC || n->size > from->size || n->_containingRegion != from) + logger::panic(nullptr, "Memory corruption detected for block %p. Dumping node contents.\nn->magic: 0x%08x, n->size: %ld, n->_containingRegion: 0x%p, n->next: 0x%p, n->prev: 0x%p, allocAddr: 0x%p\n", n, n->magic, n->size, n->_containingRegion, n->next, n->prev, n->getAllocAddr()); + if (n->size == size) + { + makeSafeLock(lock); + freeNode = n; + if (freeNode->prev) + freeNode->prev->next = freeNode->next; + if (freeNode->next) + freeNode->next->prev = freeNode->prev; + if (from->free.head == n) + from->free.head = n->next; + if (from->free.tail == n) + from->free.tail = n->prev; + from->free.nNodes--; + if (from->biggestFreeNode == freeNode) + { + node* res = nullptr; + for (auto n = from->free.head; n; ) + { + if (!res || n->size > res->size) + res = n; + + n = n->next; + } + from->biggestFreeNode = res; + } + break; + } + if (n->size >= (size + sizeof(node))) + { + // This'll work as long as we suballocate within the node. + makeSafeLock(lock); + n->size -= size + sizeof(node); + if (!n->size) + { + if (n->prev) + n->prev->next = n->next; + if (n->next) + n->next->prev = n->prev; + if (from->free.head == n) + from->free.head = n->next; + if (from->free.tail == n) + from->free.tail = n->prev; + from->free.nNodes--; + if (from->biggestFreeNode == freeNode) + { + node* res = nullptr; + for (auto n = from->free.head; n; ) + { + if (!res || n->size > res->size) + res = n; + + n = n->next; + } + from->biggestFreeNode = res; + } + } + node* newNode = (node*)((char*)n->getAllocAddr() + n->size); + freeNode = newNode; + break; + } + + n = n->next; + } + if (!freeNode) + { + start = from->next; + from = nullptr; + freeNode = nullptr; + goto tryAgain; + } + makeSafeLock(lock); + freeNode->next = freeNode->prev = nullptr; + freeNode->magic = MEMBLOCK_MAGIC; + freeNode->_containingRegion = from; + freeNode->size = size; + if (from->allocated.tail) + from->allocated.tail->next = freeNode; + if(!from->allocated.head) + from->allocated.head = freeNode; + freeNode->prev = from->allocated.tail; + from->allocated.tail = freeNode; + from->allocated.nNodes++; + from->nFreeBytes -= size; +#if OBOS_KASAN_ENABLED + memset((uint8_t*)freeNode->getAllocAddr() + size - 256, asan_poison, 256); +#endif + return freeNode->getAllocAddr(); + } + void* BasicAllocator::ReAllocate(void* base, size_t newSize) + { + if (!newSize) + { + Free(base, QueryObjectSize(base)); + return nullptr; + } + if (!base) + return Allocate(newSize); + newSize = round_up(newSize, 0x10); + // NOTE: Memory corruption can happen with KASAN enabled if QueryObjectSize() doesn't remove the shadow space size from the size returned. + // Because of this, make sure to subtract the shadow space size if you ever decide to change this code to use the node directly. + size_t objSize = QueryObjectSize(base); + if (objSize == SIZE_MAX) + return nullptr; + if (objSize == newSize) + return base; + if (newSize < objSize) + { + BasicAllocator::node* n = ((node*)base - 1); + memzero((char*)base + n->size, newSize-objSize); + n->size = newSize; + return base; + } + // The block is bigger. + void* newBlock = Allocate(newSize); + memcpy(newBlock, base, objSize); + Free(base, objSize); + return newBlock; + } + void BasicAllocator::Free(void* base, size_t) + { + if (!base) + return; + BasicAllocator::node* n = ((node*)base - 1); + if (n->magic != MEMBLOCK_MAGIC) + return; + auto r = (region*)n->_containingRegion; + if (!r) + return; + if (n->prev) + n->prev->next = n->next; + if (n->next) + n->next->prev = n->prev; + if (r->allocated.head == n) + r->allocated.head = n->next; + if (r->allocated.tail == n) + r->allocated.tail = n->prev; + if (!(--r->allocated.nNodes)) + { + freeRegion(r); + return; + } + n->next = n->prev = nullptr; + if (r->free.tail) + r->free.tail->next = n; + if(!r->free.head) + r->free.head = n; + n->prev = r->free.tail; + r->free.tail = n; + r->free.nNodes++; + r->nFreeBytes += n->size; + if (!r->biggestFreeNode) + return; + if (n->size > r->biggestFreeNode->size) + r->biggestFreeNode = n; +#ifdef OBOS_DEBUG + memset(base, 0xAA, n->size); +#endif + } + size_t BasicAllocator::QueryObjectSize(const void* base) + { + if (!base) + return SIZE_MAX; + auto n = ((const BasicAllocator::node*)base - 1); + if (n->magic != MEMBLOCK_MAGIC) + return SIZE_MAX; + if (!n->_containingRegion) + return SIZE_MAX; +#if OBOS_KASAN_ENABLED + return n->size - 256; +#else + return n->size; +#endif + } + OBOS_NO_KASAN BasicAllocator::region* BasicAllocator::allocateNewRegion(size_t size) + { + size = round_up(size, OBOS_PAGE_SIZE*4); + size += sizeof(region) + sizeof(node); + size_t initialSize = size; + makeSafeLock(lock); + BasicAllocator::region* blk = nullptr; + if (this == &vmm::g_vmmAllocator) + { +#ifdef __x86_64__ + const size_t nPages = (size / OBOS_PAGE_SIZE) + ((size % OBOS_PAGE_SIZE) != 0); + uintptr_t phys = AllocatePhysicalPages(nPages); + if (!phys) + return nullptr; + blk = (region*)MapToHHDM(phys); +#else +# error Unknown architecture. +#endif + memzero(blk, size); + } + else + { + m_lock.Unlock(); + blk = (region*)vmm::Allocate( + &vmm::g_kernelContext, + nullptr, + size, + vmm::FLAGS_GUARD_PAGE_LEFT|vmm::FLAGS_GUARD_PAGE_RIGHT, + 0 + ); + m_lock.Lock(); + } + new (blk) region{}; + blk->size = initialSize+sizeof(node); + BasicAllocator::node *n = (node*)(blk + 1); + new (n) node{}; + n->size = initialSize; + n->next = n->prev = nullptr; + n->_containingRegion = blk; + blk->biggestFreeNode = n; + blk->free.tail = blk->free.head = n; + blk->free.nNodes++; + blk->nFreeBytes += n->size; + if (m_regionTail) + m_regionTail->next = blk; + if(!m_regionHead) + m_regionHead = blk; + blk->prev = m_regionTail; + m_regionTail = blk; + m_nRegions++; + return blk; + } + void BasicAllocator::freeRegion(BasicAllocator::region* block) + { + if (block->prev) + block->prev->next = block->next; + if (block->next) + block->next->prev = block->prev; + if (m_regionHead == block) + m_regionHead = block->next; + if (m_regionTail == block) + m_regionTail = block->prev; + m_nRegions--; + if (this != &vmm::g_vmmAllocator) + vmm::Free(&vmm::g_kernelContext, block, block->size); + else + { +#if __x86_64__ + const size_t nPages = (block->size / OBOS_PAGE_SIZE) + ((block->size % OBOS_PAGE_SIZE) != 0); + FreePhysicalPages((uintptr_t)block-(uintptr_t)MapToHHDM(0), nPages); +#endif + } + } + BasicAllocator::~BasicAllocator() + { + for (region* pb = m_regionHead; pb; ) + { + auto next = pb->next; + freeRegion(pb); + pb = next; + } + } + } +} \ No newline at end of file diff --git a/src/oboskrnl/allocators/basic_allocator.h b/src/oboskrnl/allocators/basic_allocator.h index fdcd3b64..d6366df2 100644 --- a/src/oboskrnl/allocators/basic_allocator.h +++ b/src/oboskrnl/allocators/basic_allocator.h @@ -1,59 +1,116 @@ /* - * oboskrnl/allocators/basic_allocator.c - * - * Copyright (c) 2024 Omar Berrow + oboskrnl/allocators/basic_allocator.h + + Copyright (c) 2024 Omar Berrow */ #pragma once #include -#include -#include +#include #include #define MEMBLOCK_MAGIC 0x6AB450AA #define PAGEBLOCK_MAGIC 0x768AADFC #define MEMBLOCK_DEAD 0x3D793CCD -#define OBOS_BASIC_ALLOCATOR_MAGIC (0x7E046A92E7735) - -#define OBOS_NODE_ADDR(n) ((void*)(n + 1)) -enum blockSource +namespace obos { - BLOCK_SOURCE_INVALID = -1, // It is an error to get this. - BLOCK_SOURCE_PHYSICAL_MEMORY, // See allocateBlock "if ((allocator_info*)This == Mm_Allocator)" - BLOCK_SOURCE_BASICMM, // OBOS_BasicMMAllocatePages - BLOCK_SOURCE_VMA, // Mm_AllocateVirtualMemory -}; - -typedef struct freelist_node { - struct freelist_node *next, *prev; -} freelist_node; - -_Static_assert(sizeof(freelist_node) <= 16, "Internal bug, report this."); - -typedef struct freelist { - freelist_node *head, *tail; - size_t nNodes; -} freelist; - -enum { - REGION_MAGIC = 0xb49ad907c56c8 -}; - -typedef struct cache { - freelist free; - freelist clean; - spinlock lock; -} cache; - -typedef struct basic_allocator -{ - allocator_info header; - cache caches[28]; - enum blockSource blkSource; -} basic_allocator; +#if OBOS_KASAN_ENABLED + void asan_verify(uintptr_t at, size_t size, uintptr_t ip, bool rw, bool abort); +#endif + namespace allocators + { + // TODO: Test in userspace. + // struct memBlock + // { + // alignas (0x10) uint32_t magic = MEMBLOCK_MAGIC; + + // alignas (0x10) size_t size = 0; + // alignas (0x10) void* allocAddr = 0; + + // alignas (0x10) memBlock* next = nullptr; + // alignas (0x10) memBlock* prev = nullptr; + + // alignas (0x10) struct pageBlock* pageBlock = nullptr; + + // alignas (0x10) void* whoAllocated = nullptr; + // }; + // struct pageBlock + // { + // alignas (0x10) uint32_t magic = PAGEBLOCK_MAGIC; + + // alignas (0x10) pageBlock* next = nullptr; + // alignas (0x10) pageBlock* prev = nullptr; + + // alignas (0x10) memBlock* firstBlock = nullptr; + // alignas (0x10) memBlock* lastBlock = nullptr; + // alignas (0x10) size_t nMemBlocks = 0; + + // struct + // { + // alignas (0x10) memBlock* head = nullptr; + // alignas (0x10) memBlock* tail = nullptr; + // alignas (0x10) size_t nBlocks = 0; + // } freeList{}; + + // alignas (0x10) size_t nBytesUsed = 0; + // alignas (0x10) size_t nPagesAllocated = 0; + // }; + class BasicAllocator final : public Allocator + { + public: + struct node + { + alignas(0x10) uint32_t magic = MEMBLOCK_MAGIC; + alignas(0x10) size_t size; + alignas(0x10) void* _containingRegion; + alignas(0x10) node *next; + alignas(0x10) node *prev; + constexpr void *getAllocAddr() const { return (void*)(this + 1); } + }; + struct node_list + { + node *head, *tail; + size_t nNodes; + }; + struct region + { + alignas(0x10) uint32_t magic = PAGEBLOCK_MAGIC; + alignas(0x10) size_t size; + alignas(0x10) size_t nFreeBytes; + alignas(0x10) node_list free, allocated; + alignas(0x10) node* biggestFreeNode; + + alignas(0x10) region *next, *prev; + }; + public: + BasicAllocator() {} + + void* Allocate(size_t size) override; + void* ReAllocate(void* base, size_t newSize) override; + void Free(void* base, size_t size) override; + + size_t QueryObjectSize(const void* base) override; + + size_t GetAllocationSize() override { return 0; }; + + void OptimizeAllocator() override {}; + + ~BasicAllocator(); -OBOS_EXPORT obos_status OBOSH_ConstructBasicAllocator(basic_allocator* This); +#if OBOS_KASAN_ENABLED + friend void obos::asan_verify(uintptr_t at, size_t size, uintptr_t ip, bool rw, bool abort); +#endif + private: + locks::SpinLock m_lock; + region *m_regionHead = nullptr, *m_regionTail = nullptr; + size_t m_nRegions = 0; + size_t m_totalPagesAllocated = 0; + region* allocateNewRegion(size_t size); + void freeRegion(region* block); + }; + } +} \ No newline at end of file diff --git a/src/oboskrnl/allocators/liballoc.cpp b/src/oboskrnl/allocators/liballoc.cpp new file mode 100644 index 00000000..0dfcfacb --- /dev/null +++ b/src/oboskrnl/allocators/liballoc.cpp @@ -0,0 +1,445 @@ +/* + oboskrnl/allocators/liballoc.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include +#include + +#include + +#include + +#include + +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include +#include + +#define GET_FUNC_ADDR(addr) reinterpret_cast(addr) + +#define MIN_PAGES_ALLOCATED 8 +#define PTR_ALIGNMENT 0x10 +#define ROUND_PTR_UP(ptr) (((ptr) + PTR_ALIGNMENT) & ~(PTR_ALIGNMENT - 1)) +#define ROUND_PTR_DOWN(ptr) ((ptr) & ~(PTR_ALIGNMENT - 1)) + +struct safe_lock +{ + safe_lock() = delete; + safe_lock(obos::locks::SpinLock* lock) + { + m_lock = lock; + } + bool Lock() + { + if (m_lock) + return m_lock->Lock(); + return false; + } + bool IsLocked() + { + if (m_lock) + return m_lock->Locked(); + return false; + } + void Unlock() + { + if (m_lock) + m_lock->Unlock(); + } + ~safe_lock() + { + Unlock(); + } +private: + obos::locks::SpinLock* m_lock = nullptr; +}; + +#define makeSafeLock(vName) safe_lock vName{ &m_lock }; vName.Lock(); + +namespace obos +{ + namespace allocators + { + pageBlock* BasicAllocator::allocateNewPageBlock(size_t nPages) + { + nPages += (MIN_PAGES_ALLOCATED - (nPages % MIN_PAGES_ALLOCATED)); + pageBlock* blk = nullptr; + bool isFirstBlock = false; + if (!m_pageBlockHead && this == allocators::g_kAllocator) + { + if (nPages < 16) + nPages = 16; // Guarantee that there is enough space for any initial page nodes/descriptors. + isFirstBlock = true; + blk = (pageBlock*)vmm::RawAllocate( + (void*)vmm::FindBase(&vmm::g_kernelContext, + OBOS_KERNEL_ADDRESS_SPACE_USABLE_BASE, + OBOS_KERNEL_ADDRESS_SPACE_LIMIT, + nPages * OBOS_PAGE_SIZE + ), + nPages * OBOS_PAGE_SIZE, + vmm::FLAGS_GUARD_PAGE_LEFT|vmm::FLAGS_GUARD_PAGE_RIGHT, + 0 + ); + memzero(blk, nPages * OBOS_PAGE_SIZE); + } + else + { + m_lock.Unlock(); + blk = (pageBlock*)vmm::Allocate( + &vmm::g_kernelContext, + nullptr, + nPages * OBOS_PAGE_SIZE, + vmm::FLAGS_GUARD_PAGE_LEFT|vmm::FLAGS_GUARD_PAGE_RIGHT, + 0 + ); + m_lock.Lock(); + } + if(!blk) + obos::logger::panic(nullptr, "Could not allocate a pageBlock.\n"); + new (blk) pageBlock{}; + blk->magic = PAGEBLOCK_MAGIC; + blk->nPagesAllocated = nPages; + blk->nBytesUsed += sizeof(*blk); + memBlock* firstBlock = (memBlock*)(blk + 1); + firstBlock->magic = MEMBLOCK_MAGIC; + firstBlock->allocAddr = firstBlock + 1; + firstBlock->pageBlock = blk; + firstBlock->size = blk->nPagesAllocated*OBOS_PAGE_SIZE-((uintptr_t)firstBlock->allocAddr-(uintptr_t)blk); + blk->freeList.head = blk->freeList.tail = firstBlock; + blk->freeList.nBlocks++; + m_totalPagesAllocated += nPages; + if (m_pageBlockTail) + m_pageBlockTail->next = blk; + if(!m_pageBlockHead) + m_pageBlockHead = blk; + blk->prev = m_pageBlockTail; + m_pageBlockTail = blk; + m_nPageBlocks++; + if (isFirstBlock) + { + m_lock.Unlock(); + void *res = vmm::Allocate( + &vmm::g_kernelContext, + blk, + nPages * OBOS_PAGE_SIZE, + vmm::FLAGS_RESERVE|vmm::FLAGS_GUARD_PAGE_LEFT|vmm::FLAGS_GUARD_PAGE_RIGHT, + 0 + ); + OBOS_ASSERTP(res, "Could not reserve page block.\n"); + auto pgNode = vmm::g_kernelContext.GetPageNode(res); + OBOS_ASSERTP(pgNode, "No page node found"); + for (size_t i = 0; i < pgNode->nPageDescriptors; i++) + arch::get_page_descriptor(&vmm::g_kernelContext, (void*)pgNode->pageDescriptors[i].virt, pgNode->pageDescriptors[i]); + m_lock.Lock(); + } + return blk; + } + void BasicAllocator::freePageBlock(pageBlock* block) + { + if (block->next) + block->next->prev = block->prev; + if (block->prev) + block->prev->next = block->next; + if (m_pageBlockTail == block) + m_pageBlockTail = block->prev; + m_nPageBlocks--; + m_totalPagesAllocated -= block->nPagesAllocated; + vmm::Free(&vmm::g_kernelContext, block, block->nPagesAllocated * OBOS_PAGE_SIZE); + } + void* BasicAllocator::Allocate(size_t amount) + { + if (!amount) + return nullptr; + + makeSafeLock(lock); + + amount = ROUND_PTR_UP(amount); + + pageBlock* currentPageBlock = nullptr; + + // Choose a pageBlock that fits a block with "amount" + + size_t amountNeeded = amount + sizeof(memBlock); + + if (m_nPageBlocks == 0) + { + size_t nPages = (amount / OBOS_PAGE_SIZE) + ((amount % OBOS_PAGE_SIZE) != 0); + if ((nPages*OBOS_PAGE_SIZE-sizeof(memBlock)-sizeof(pageBlock)) < amount) + nPages++; + currentPageBlock = allocateNewPageBlock(nPages); + if (!currentPageBlock) + return nullptr; + goto foundPageBlock; + } + + { + pageBlock* current = m_pageBlockHead; + + // We need to look for pageBlock structures. + while (current) + { + //if (((current->nPagesAllocated * OBOS_PAGE_SIZE) - current->nuint8_tsUsed) >= amountNeeded) + /*if ((GET_FUNC_ADDR(current->lastBlock->allocAddr) + current->lastBlock->size + sizeof(memBlock) + amountNeeded) < + (GET_FUNC_ADDR(current) + current->nPagesAllocated * OBOS_PAGE_SIZE))*/ + if (!OBOS_IS_VIRT_ADDR_CANONICAL(current)) + break; + if (!OBOS_IS_VIRT_ADDR_CANONICAL(current->lastBlock)) + break; + if (!current->firstBlock || !current->lastBlock) + { + // Simply check that. + if ((current->nPagesAllocated * OBOS_PAGE_SIZE) > amountNeeded) + { + currentPageBlock = current; + break; + } + goto end; + } + // if ((GET_FUNC_ADDR(current->highestBlock->allocAddr) + current->highestBlock->size + amountNeeded) <= + // (GET_FUNC_ADDR(current) + current->nPagesAllocated * OBOS_PAGE_SIZE) + // ) + if (current->freeList.nBlocks) + { + currentPageBlock = current; + break; + } + + end: + current = current->next; + }; + } + + foundPageBlock: + + // We couldn't find a page block big enough :( + if (!currentPageBlock) + { + size_t nPages = (amount / OBOS_PAGE_SIZE) + ((amount % OBOS_PAGE_SIZE) != 0); + if ((nPages*OBOS_PAGE_SIZE-sizeof(memBlock)-sizeof(pageBlock)) < amount) + nPages++; + currentPageBlock = allocateNewPageBlock(nPages); + } + if (!currentPageBlock) + return nullptr; + + memBlock* block = nullptr; + for (auto cur = currentPageBlock->freeList.head; cur; ) + { + if (cur->size == amount || cur->size >= amountNeeded) + { + block = cur; + break; + } + + cur = cur->next; + } + if (!block) + { + currentPageBlock = nullptr; + goto foundPageBlock; + } + bool blockRequiresSetup = true; + if (block->size == amount) + { + // Repurpose this block to an allocated block. + if (currentPageBlock->freeList.head == block) + currentPageBlock->freeList.head = block->next; + if (currentPageBlock->freeList.tail == block) + currentPageBlock->freeList.tail = block->prev; + if (block->next) + block->next->prev = block->prev; + if (block->prev) + block->prev->next = block->next; + currentPageBlock->freeList.nBlocks--; + block->magic = MEMBLOCK_MAGIC; + blockRequiresSetup = false; + } + else if (block->size >= amountNeeded) + { + block->size -= amountNeeded; + if (!block->size) + { + if (currentPageBlock->freeList.head == block) + currentPageBlock->freeList.head = block->next; + if (currentPageBlock->freeList.tail == block) + currentPageBlock->freeList.tail = block->prev; + if (block->next) + block->next->prev = block->prev; + if (block->prev) + block->prev->next = block->next; + currentPageBlock->freeList.nBlocks--; + } + void* ptr = (void*)((uintptr_t)block->allocAddr + block->size); + block = (memBlock*)ptr; + } + else + { + currentPageBlock = nullptr; + goto foundPageBlock; + } + foundMemBlock: + block->next = nullptr; + block->prev = nullptr; + if (blockRequiresSetup) + { + block->magic = MEMBLOCK_MAGIC; + block->allocAddr = (void*)(block + 1); + block->size = amount; + block->pageBlock = currentPageBlock; + } + + if (currentPageBlock->lastBlock) + currentPageBlock->lastBlock->next = block; + if(!currentPageBlock->firstBlock) + currentPageBlock->firstBlock = block; + block->prev = currentPageBlock->lastBlock; + currentPageBlock->lastBlock = block; + currentPageBlock->nMemBlocks++; + + currentPageBlock->nBytesUsed += amountNeeded; +#ifdef OBOS_DEBUG + block->whoAllocated = (void*)__builtin_extract_return_addr(__builtin_return_address(0)); +#endif + return block->allocAddr; + } + void* BasicAllocator::ReAllocate(void* ptr, size_t newSize) + { + if (!newSize) + return nullptr; + + newSize = ROUND_PTR_UP(newSize); + + if (!ptr) + return ZeroAllocate(newSize); + + size_t oldSize = 0; + + memBlock* block = (memBlock*)ptr; + block--; + if (block->magic != MEMBLOCK_MAGIC) + return nullptr; + oldSize = block->size; + if (oldSize == newSize) + return ptr; // The block can be kept in the same state. + if (newSize < oldSize) + { + // If the new size is less than the old size. + // Truncate the block to the right size. + block->size = newSize; + memzero((uint8_t*)ptr + oldSize, oldSize - newSize); + return ptr; + } + // if (block->pageBlock->highestBlock == block && + // ((GET_FUNC_ADDR(current) + current->nPagesAllocated * OBOS_PAGE_SIZE) - current->nBytesUsed) >= amountNeeded) + // { + // // If we're the highest block in the page block, and there's still space in the page block, then expand the block size. + // block->size = newSize; + // memzero((uint8_t*)ptr + oldSize, newSize - oldSize); + // return ptr; + // } + // If the next block crosses page block boundaries, skip checking for that. + if (((uint8_t*)(block + 1) + oldSize) >= ((uint8_t*)block->pageBlock + block->pageBlock->nPagesAllocated * OBOS_PAGE_SIZE)) + goto newBlock; + if (((memBlock*)((uint8_t*)(block + 1) + oldSize))->magic != MEMBLOCK_MAGIC) + { + // This is rather a corrupted block or there is free space after the block. + void* blkAfter = ((uint8_t*)(block + 1) + oldSize); + size_t increment = PTR_ALIGNMENT; + void* endBlock = nullptr; + for (endBlock = blkAfter; ((memBlock*)endBlock)->magic != MEMBLOCK_MAGIC && + ((uint8_t*)endBlock) >= ((uint8_t*)block->pageBlock + block->pageBlock->nPagesAllocated * OBOS_PAGE_SIZE); + endBlock = (uint8_t*)endBlock + increment); + size_t nFreeSpace = (uint8_t*)endBlock - (uint8_t*)blkAfter; + if ((oldSize + nFreeSpace) >= newSize) + { + // If we have enough space after the block. + block->size = newSize; + memzero((uint8_t*)ptr + oldSize, newSize - oldSize); + return ptr; + } + // Otherwise we'll need a new block. + } + newBlock: + // We need a new block. + void* newBlock = ZeroAllocate(newSize); + memcpy(newBlock, ptr, oldSize); + Free(ptr, 0); + return newBlock; + } + void BasicAllocator::Free(void* ptr, size_t) + { + if (!ptr) + return; + + makeSafeLock(lock); + + memBlock* block = (memBlock*)ptr; + block--; + if (block->magic != MEMBLOCK_MAGIC) + return; + + pageBlock* currentPageBlock = (pageBlock*)block->pageBlock; + + const size_t totalSize = sizeof(memBlock) + block->size; + + currentPageBlock->nBytesUsed -= totalSize; + + if (--currentPageBlock->nMemBlocks) + { + if (block->next) + block->next->prev = block->prev; + if (block->prev) + block->prev->next = block->next; + if (currentPageBlock->lastBlock == block) + currentPageBlock->lastBlock = block->prev; + if (currentPageBlock->firstBlock == block) + currentPageBlock->firstBlock = block->next; + + block->next = nullptr; + block->prev = nullptr; + + block->magic = MEMBLOCK_DEAD; + if (currentPageBlock->freeList.tail) + currentPageBlock->freeList.tail->next = block; + if(!currentPageBlock->freeList.head) + currentPageBlock->freeList.head = block; + block->prev = currentPageBlock->freeList.tail; + currentPageBlock->freeList.tail = block; + currentPageBlock->freeList.nBlocks++; + + memzero(block->allocAddr, block->size); + } + else + freePageBlock(currentPageBlock); + } + size_t BasicAllocator::QueryObjectSize(const void* ptr) + { + memBlock* block = (memBlock*)ptr; + block--; + if (block->magic != MEMBLOCK_MAGIC) + return SIZE_MAX; + return block->size; + } + BasicAllocator::~BasicAllocator() + { + for (pageBlock* pb = m_pageBlockHead; pb; ) + { + auto next = pb->next; + freePageBlock(pb); + pb = next; + } + } + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/driver_interface_load.h b/src/oboskrnl/arch/driver_interface_load.h new file mode 100644 index 00000000..ef056ac3 --- /dev/null +++ b/src/oboskrnl/arch/driver_interface_load.h @@ -0,0 +1,5 @@ +#pragma once + +#if defined(__x86_64__) || defined(_WIN64) +#include +#endif \ No newline at end of file diff --git a/src/oboskrnl/arch/irq_register.h b/src/oboskrnl/arch/irq_register.h new file mode 100644 index 00000000..12c7a271 --- /dev/null +++ b/src/oboskrnl/arch/irq_register.h @@ -0,0 +1,32 @@ +#pragma once + +#if defined(__x86_64__) || defined(_WIN64) +#include +#include +namespace obos +{ + namespace arch + { + extern void SendEOI(interrupt_frame*); + inline bool IsSpuriousInterrupt(interrupt_frame*) { return false; } // We can assume no, as spurious interrupts are sent through the spurious interrupt vector of the LAPIC. + } +} +#endif + +#ifndef OBOS_MAX_INTERRUPT_VECTORS +#error OBOS_MAX_INTERRUPT_VECTORS is not defined by the architecture +#endif +#ifndef OBOS_MAX_INTERRUPT_VECTORS_PER_IRQL +#error OBOS_MAX_INTERRUPT_VECTORS_PER_IRQL is not defined by the architecture +#endif +#ifndef OBOS_IRQL_TO_VECTOR +#include +COMPILE_MESSAGE("OBOS_IRQL_TO_VECTOR is not defined. Defining automatically...\n"); +#define OBOS_IRQL_TO_VECTOR(irql) ((irql) >= 2 ? ((irql) * OBOS_MAX_INTERRUPT_VECTORS_PER_IRQL) : 0) +#endif +#ifndef OBOS_NO_EOI_ON_SPURIOUS_INTERRUPT +COMPILE_MESSAGE("OBOS_NO_EOI_ON_SPURIOUS_INTERRUPT is not defined. Setting to true.\n"); +#define OBOS_NO_EOI_ON_SPURIOUS_INTERRUPT (true) +#endif +static_assert(OBOS_MAX_INTERRUPT_VECTORS_PER_IRQL < OBOS_MAX_INTERRUPT_VECTORS, ""); +static_assert((OBOS_MAX_INTERRUPT_VECTORS / OBOS_MAX_INTERRUPT_VECTORS_PER_IRQL) != 0, ""); \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/asm_helpers.S b/src/oboskrnl/arch/m68k/asm_helpers.S deleted file mode 100644 index 4cad78a6..00000000 --- a/src/oboskrnl/arch/m68k/asm_helpers.S +++ /dev/null @@ -1,26 +0,0 @@ -/* - * oboskrnl/arch/m68k/asm_helpers.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -.cpu 68040 - -// void setSR(uint32_t to); -// uint32_t getSR(); -.global setSR -.global getSR -.global pflush -setSR: - move.l (+4,%a7), %d0 - move.w %d0, %sr - rts -getSR: - move.w %sr, %d0 - rts -pflush: - move.l (4, %sp), %a0 - pflush %a0 - rts - -.section .note.GNU-stack,"",%progbits diff --git a/src/oboskrnl/arch/m68k/asm_helpers.h b/src/oboskrnl/arch/m68k/asm_helpers.h deleted file mode 100644 index 60846b0b..00000000 --- a/src/oboskrnl/arch/m68k/asm_helpers.h +++ /dev/null @@ -1,15 +0,0 @@ -/* - * oboskrnl/arch/m68k/asm_helpers.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include - -void setSR(uint16_t to); -uint16_t getSR(); -void pflush(uintptr_t virt); -// Calls the interrupt vector at 'vector' -void Arch_SimulateIRQ(uint8_t vector); \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/boot_info.h b/src/oboskrnl/arch/m68k/boot_info.h deleted file mode 100644 index 1781ad91..00000000 --- a/src/oboskrnl/arch/m68k/boot_info.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * oboskrnl/arch/m68k/boot_info.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include -#include - -typedef enum BootInfoType -{ - BootInfoType_Last = 0, - BootInfoType_MachType = 1, - BootInfoType_CpuType = 2, - BootInfoType_FpuType = 3, - BootInfoType_MmuType = 4, - BootInfoType_MemChunk = 5, - BootInfoType_InitRd = 6, - BootInfoType_CommandLine = 7, - BootInfoType_RngSeed = 8, - - BootInfoType_QemuVersion = 0x8000, - BootInfoType_GoldfishPicBase = 0x8001, - BootInfoType_GoldfishRtcBase = 0x8002, - BootInfoType_GoldfishTtyBase = 0x8003, - BootInfoType_VirtioBase = 0x8004, - BootInfoType_ControlBase = 0x8005 -} BootInfoType; - -typedef struct BootDeviceBase -{ - uint32_t base; - uint32_t irq; -} BootDeviceBase; -typedef struct BootInfoTag -{ - uint16_t type; - uint16_t size; -} OBOS_PACK BootInfoTag; - -OBOS_EXPORT BootInfoTag* Arch_GetBootInfo(BootInfoType type); -OBOS_EXPORT BootInfoTag* Arch_GetBootInfoFrom(BootInfoType type, BootInfoTag* tag); \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/cpu_local_arch.h b/src/oboskrnl/arch/m68k/cpu_local_arch.h deleted file mode 100644 index f6484f55..00000000 --- a/src/oboskrnl/arch/m68k/cpu_local_arch.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * oboskrnl/arch/m68k/cpu_local_arch.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include - -// deferred irq -typedef struct m68k_dirq -{ - struct m68k_dirq *next, *prev; - size_t nDefers; - // Is called after the defer happens - void(*on_defer_callback)(void* udata); // used by the PIC code. - void* udata; - uint8_t irql; -} m68k_dirq; -typedef struct m68k_dirq_list -{ - m68k_dirq *head, *tail; - size_t nNodes; -} m68k_dirq_list; -typedef struct cpu_local_arch -{ - m68k_dirq irqs[256]; - m68k_dirq_list deferred; -} cpu_local_arch; \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/driver_loader.c b/src/oboskrnl/arch/m68k/driver_loader.c deleted file mode 100644 index 7c94aeec..00000000 --- a/src/oboskrnl/arch/m68k/driver_loader.c +++ /dev/null @@ -1,646 +0,0 @@ -/* - * oboskrnl/arch/m68k/driver_loader.c - * - * Copyright (c) 2024-2025 Omar Berrow -*/ - -#include -#include -#include -#include - -#include - -#include -#include - -#include - -#include - -#include -#include - -#define OffsetPtr(ptr, off, type) (type)(((uintptr_t)ptr) + ((intptr_t)off)) -// Please forgive me for this -#define Cast(what, to) ((to)what) - -// Helpers. -struct relocation_table -{ - Elf_Dyn* table; - size_t nEntries; - bool rel; // false if rela, true if rel. -}; -struct relocation -{ - uint32_t symbolTableOffset; - uintptr_t virtualAddress; - uint16_t relocationType; - int64_t addend; -}; -struct relocation_array -{ - struct relocation_table* buf; - size_t nRelocations; -}; -struct copy_reloc -{ - void* src, *dest; - size_t size; -}; -struct copy_reloc_array -{ - struct copy_reloc* buf; - size_t nRelocations; -}; -static void append_relocation_table(struct relocation_array* arr, const struct relocation_table* what) -{ - size_t old_sz = arr->nRelocations*sizeof(*arr->buf); - size_t new_sz = (++arr->nRelocations)*sizeof(*arr->buf); - arr->buf = Reallocate(OBOS_KernelAllocator, arr->buf, new_sz, old_sz, nullptr); - OBOS_ASSERT(arr->buf); // oopsies - arr->buf[arr->nRelocations - 1] = *what; - // Note: - // Since you'll probably forget what you were doing: - // You were making relocation tables in the array be stored as the struct relocation_table instead of Elf64_Dyn. - // You're done adapting the array, you only need to adapt the code. - // 5 Hours later: - // thank god I wrote this comment. -} -static void free_reloc_array(struct relocation_array* arr) -{ - Free(OBOS_KernelAllocator, arr->buf, arr->nRelocations*sizeof(*arr->buf)); - memzero(arr, sizeof(*arr)); -} -static void append_copy_reloc(struct copy_reloc_array* arr, const struct copy_reloc* what) -{ - size_t old_sz = arr->nRelocations*sizeof(*arr->buf); - size_t new_sz = (++arr->nRelocations)*sizeof(*arr->buf); - arr->buf = Reallocate(OBOS_KernelAllocator, arr->buf, new_sz, old_sz, nullptr); - OBOS_ASSERT(arr->buf); // oopsies - arr->buf[arr->nRelocations - 1] = *what; -} -static void free_copy_reloc_array(struct copy_reloc_array* arr) -{ - Free(OBOS_KernelAllocator, arr->buf, arr->nRelocations*sizeof(*arr->buf)); - memzero(arr, sizeof(*arr)); -} - - -// From https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-48031.html#scrolltoc -static uint32_t ElfHash(const char* name) -{ - uint32_t h = 0, g = 0; - - while (*name) - { - h = (h << 4) + *name++; - if ((g = h & 0xf0000000)) - h ^= g >> 24; - h &= ~g; - } - return h; -} -static Elf32_Sym* GetSymbolFromTable( - const uint8_t* fileStart, - uint8_t* baseAddress, - Elf32_Sym* symbolTable, - uintptr_t hashTableOff, - Elf32_Off stringTable, - const char* _symbol) -{ - Elf32_Word* hashTableBase = (Elf32_Word*)(baseAddress + hashTableOff); - Elf32_Word nBuckets = hashTableBase[0]; - uint32_t currentBucket = ElfHash(_symbol) % nBuckets; - Elf32_Word* firstBucket = hashTableBase + 2; - Elf32_Word* firstChain = firstBucket + nBuckets; - size_t index = firstBucket[currentBucket]; - while (index) - { - Elf32_Sym* symbol = &symbolTable[index]; - const char* symbolName = (char*)(fileStart + stringTable + symbol->st_name); - if (strcmp(symbolName, _symbol)) - return symbol; - - index = firstChain[index]; - } - return nullptr; -} - -static void add_dependency(driver_id* depends, driver_id* dependency) -{ - if (!dependency || !depends) - return; - // depends is the driver that depends on the dependency. - for (driver_node* cur = depends->dependencies.head; cur; ) - { - if (cur->data == dependency) - return; // don't add an already added dependency to the list - cur = cur->next; - } - driver_node* node = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(driver_node), nullptr); - node->data = dependency; - driver_list* list = &depends->dependencies; - if (!list->head) - list->head = node; - if (list->tail) - list->tail->next = node; - node->prev = list->tail; - list->tail = node; - list->nNodes++; - dependency->refCnt++; -} -static bool calculate_relocation(obos_status* status, driver_id* drv, Elf_Sym* symbolTable, Elf32_Off stringTable, const void* file, struct relocation i, void* base, size_t szProgram, Elf32_Addr* GOT, struct copy_reloc_array* copy_relocations, uintptr_t hashTableOffset) -{ - if (status) - *status = OBOS_STATUS_SUCCESS; - driver_symbol* Symbol = nullptr; - driver_symbol internal_symbol = {}; - if (i.symbolTableOffset) - { - Elf32_Sym* Unresolved_Symbol = &symbolTable[i.symbolTableOffset]; - driver_id* dependency = nullptr; - Symbol = DrvH_ResolveSymbol( - OffsetPtr(base, stringTable + Unresolved_Symbol->st_name, const char*), - &dependency - ); - if (Symbol && Symbol->visibility != SYMBOL_VISIBILITY_DEFAULT) - { - if (status) - *status = OBOS_STATUS_DRIVER_REFERENCED_UNRESOLVED_SYMBOL; - OBOS_Debug("Could not resolve symbol '%s' (symbol is hidden) referenced within a driver.\n", OffsetPtr(base, stringTable + Unresolved_Symbol->st_name, const char*)); - Mm_VirtualMemoryFree(&Mm_KernelContext, base, szProgram); - return false; - } - if (!Symbol) - { - Elf32_Sym* sym = GetSymbolFromTable( - file, base, symbolTable, hashTableOffset, stringTable, - OffsetPtr(base, stringTable + Unresolved_Symbol->st_name, const char*) - ); - if (sym->st_shndx == 0) - goto unresolved; - internal_symbol.address = OffsetPtr(base, sym->st_value, uintptr_t); - internal_symbol.size = sym->st_size; - internal_symbol.visibility = SYMBOL_VISIBILITY_DEFAULT; - switch (ELF32_ST_TYPE(sym->st_info)) - { - case STT_FUNC: - internal_symbol.type = SYMBOL_TYPE_FUNCTION; - break; - case STT_FILE: - internal_symbol.type = SYMBOL_TYPE_FILE; - break; - case STT_OBJECT: - internal_symbol.type = SYMBOL_TYPE_VARIABLE; - break; - default: - break; - } - Symbol = &internal_symbol; - } - unresolved: - if (!Symbol && ELF32_ST_BIND(Unresolved_Symbol->st_info) != STB_WEAK) - { - if (status) - *status = OBOS_STATUS_DRIVER_REFERENCED_UNRESOLVED_SYMBOL; - OBOS_Debug("Could not resolve symbol '%s' referenced within a driver.\n", OffsetPtr(base, stringTable + Unresolved_Symbol->st_name, const char*)); - Mm_VirtualMemoryFree(&Mm_KernelContext, base, szProgram); - return false; - } - add_dependency(drv, dependency); - if (ELF32_ST_BIND(Unresolved_Symbol->st_info) == STB_WEAK) - { - internal_symbol.size = Unresolved_Symbol->st_size; - internal_symbol.address = 0; - switch (ELF32_ST_TYPE(Unresolved_Symbol->st_info)) - { - case STT_FUNC: - internal_symbol.type = SYMBOL_TYPE_FUNCTION; - break; - case STT_FILE: - internal_symbol.type = SYMBOL_TYPE_FILE; - break; - case STT_OBJECT: - internal_symbol.type = SYMBOL_TYPE_VARIABLE; - break; - default: - break; - } - internal_symbol.visibility = SYMBOL_VISIBILITY_DEFAULT; - Symbol = &internal_symbol; - } - if (Unresolved_Symbol->st_size != Symbol->size && i.relocationType == R_68K_COPY) - { - // Oh no! - if (status) - *status = OBOS_STATUS_DRIVER_SYMBOL_MISMATCH; - Mm_VirtualMemoryFree(&Mm_KernelContext, base, szProgram); - return false; - } - } - uintptr_t relocAddr = (uintptr_t)base + i.virtualAddress; - uint32_t relocResult = 0; - uint8_t relocSize = 0; - switch (i.relocationType) - { - case R_68K_NONE: - break; - case R_68K_32: - relocResult = Symbol->address + i.addend; - relocSize = 4; - break; - case R_68K_16: - relocResult = Symbol->address + i.addend; - relocSize = 2; - break; - case R_68K_8: - relocResult = Symbol->address + i.addend; - relocSize = 1; - break; - case R_68K_PC32: - relocResult = Symbol->address + i.addend - relocAddr; - relocSize = 1; - break; - case R_68K_PC16: - relocResult = Symbol->address + i.addend - relocAddr; - relocSize = 2; - break; - case R_68K_PC8: - relocResult = Symbol->address + i.addend - relocAddr; - relocSize = 1; - break; - case R_68K_GOT32: - relocResult = (uintptr_t)GOT + i.addend - relocAddr; - relocSize = 4; - break; - case R_68K_GOT16: - relocResult = (uintptr_t)GOT + i.addend - relocAddr; - relocSize = 2; - break; - case R_68K_GOT8: - relocResult = (uintptr_t)GOT + i.addend - relocAddr; - relocSize = 1; - break; - case R_68K_GOT320: - relocResult = 0; // (uintptr_t)GOT - (uintptr_t)GOT - relocSize = 4; - break; - case R_68K_GOT160: - relocResult = 0; // (uintptr_t)GOT - (uintptr_t)GOT - relocSize = 2; - break; - case R_68K_GOT80: - relocResult = 0; // (uintptr_t)GOT - (uintptr_t)GOT - relocSize = 1; - break; - case R_68K_PLT32: - // TODO: Get PLT address. - if (status) - *status = OBOS_STATUS_UNIMPLEMENTED; - return false; - relocResult = 0; - relocSize = 4; - break; - case R_68K_PLT16: - // TODO: Get PLT address. - if (status) - *status = OBOS_STATUS_UNIMPLEMENTED; - return false; - relocResult = 0; - relocSize = 2; - break; - case R_68K_PLT8: - // TODO: Get PLT address. - if (status) - *status = OBOS_STATUS_UNIMPLEMENTED; - return false; - relocResult = 0; - relocSize = 1; - break; - case R_68K_PLT320: - relocResult = 0; // PLT-PLT - relocSize = 4; - break; - case R_68K_PLT160: - relocResult = 0; // PLT-PLT - relocSize = 2; - break; - case R_68K_PLT80: - relocResult = 0; // PLT-PLT - relocSize = 2; - break; - case R_68K_COPY: - { - // Save copy relocations for the end because if we don't, it might contain unresolved addresses. - struct copy_reloc reloc = { (void*)relocAddr, (void*)Symbol->address, Symbol->size }; - append_copy_reloc(copy_relocations, &reloc); - relocSize = 0; - break; - } - case R_68K_GLOB_DAT: - case R_68K_JUMP_SLOT: - relocResult = Symbol->address; - relocSize = 4; - break; - case R_68K_RELATIVE: - relocResult = (uint32_t)base + i.addend; - relocSize = 4; - break; - default: - break; - } - switch (relocSize) - { - case 0: - break; // The relocation type is rather unsupported or special. - case 1: // word8 - *(uint8_t*)(relocAddr) = (uint8_t)(relocResult & 0xff); - break; - case 2: // word16 - *(uint16_t*)(relocAddr) = (uint16_t)(relocResult & 0xffff); - break; - case 4: // word32 - *(uint32_t*)(relocAddr) = (uint32_t)(relocResult & 0xffffffff); - break; - default: - break; - } - // memcpy((void*)relocAddr, &relocResult, relocSize); - return true; -} -void dyn_loader_dummy() { return; } -void* DrvS_LoadRelocatableElf(driver_id* driver, const void* file, size_t szFile, Elf_Sym** dynamicSymbolTable, size_t* nEntriesDynamicSymbolTable, const char** dynstrtab, void** top, obos_status* status) -{ - OBOS_UNUSED(driver); - OBOS_UNUSED(file); - OBOS_UNUSED(szFile); - OBOS_UNUSED(dynamicSymbolTable); - OBOS_UNUSED(nEntriesDynamicSymbolTable); - OBOS_UNUSED(dynstrtab); - OBOS_UNUSED(top); - OBOS_UNUSED(status); - Elf_Ehdr* ehdr = Cast(file, Elf_Ehdr*); - Elf_Phdr* phdr_table = OffsetPtr(ehdr, ehdr->e_phoff, Elf_Phdr*); - Elf_Phdr* dynamic = nullptr; - size_t szProgram = 0; - char* end = nullptr; - for (size_t i = 0; i < ehdr->e_phnum; i++) - { - if (phdr_table[i].p_type == PT_DYNAMIC) - dynamic = &phdr_table[i]; - if (phdr_table[i].p_type != PT_LOAD && phdr_table[i].p_type != PT_DYNAMIC) - continue; - Elf_Phdr* curr = &phdr_table[i]; - if (!curr->p_memsz) - continue; - if (!end || curr->p_vaddr > (uintptr_t)end) - end = (void*)(curr->p_vaddr+curr->p_memsz); - } - end = (void*)(((uintptr_t)end + 0xfff) & ~0xfff); - szProgram = (size_t)end; - void* base = Mm_VirtualMemoryAlloc(&Mm_KernelContext, nullptr, szProgram, 0, VMA_FLAGS_GUARD_PAGE, nullptr, status); - if (!base) - return nullptr; - // The pages are mapped in. - // Copy the phdr data into them. - for (size_t i = 0; i < ehdr->e_phnum; i++) - { - if (phdr_table[i].p_type != PT_LOAD && phdr_table[i].p_type != PT_DYNAMIC) - continue; - Elf_Phdr* curr = &phdr_table[i]; - if (!curr->p_memsz) - continue; - memcpy(OffsetPtr(base, curr->p_vaddr, void*), OffsetPtr(file, curr->p_offset, void*), curr->p_filesz); - // NOTE: Possible buffer overflow - memzero(OffsetPtr(base, curr->p_vaddr + curr->p_filesz, void*), curr->p_memsz - curr->p_filesz); - } - // Find the relocations in the dynamic header. - struct relocation_array relocations = {}; - Elf32_Sym* symbolTable = 0; - Elf32_Off stringTable = 0; - Elf32_Addr hashTableOffset = 0; - Elf32_Addr* GOT = nullptr; - size_t last_dtrelasz = 0, last_dtrelsz = 0, last_dtpltrelsz = 0; - bool awaitingRelaSz = false, foundRelaSz = false; - Elf_Dyn* dynEntryAwaitingRelaSz = nullptr; - bool awaitingRelSz = false, foundRelSz = false; - Elf_Dyn* dynEntryAwaitingRelSz = nullptr; - uint64_t last_dlpltrel = 0; - void* dyn_base = OffsetPtr(base, dynamic->p_vaddr, Elf32_Dyn*); - struct relocation_table current = {}; - struct relocation_table *last_found_dtrel = {}; - struct relocation_table *last_found_dtrela = {}; - struct relocation_table *last_found_dtjmprel = {}; - OBOS_UNUSED(last_found_dtrel); - OBOS_UNUSED(last_found_dtrela); - OBOS_UNUSED(last_found_dtjmprel); - for (Elf_Dyn* currentDynamicHeader = dyn_base; currentDynamicHeader->d_tag != DT_NULL; currentDynamicHeader++) - { - switch (currentDynamicHeader->d_tag) - { - case DT_HASH: - hashTableOffset = currentDynamicHeader->d_un.d_ptr; - break; - case DT_PLTGOT: - // TODO: Find out whether this is the PLT or GOT (if possible). - GOT = (Elf32_Addr*)(base + currentDynamicHeader->d_un.d_ptr); - break; - case DT_REL: - if (!foundRelSz) - { - awaitingRelSz = true; - dynEntryAwaitingRelSz = currentDynamicHeader; - break; - } - current.rel = true; - current.table = currentDynamicHeader; - current.nEntries = last_dtrelsz/sizeof(Elf32_Rel); - append_relocation_table(&relocations, ¤t); - foundRelSz = false; - last_dtrelsz = 0; - last_found_dtrel = &relocations.buf[relocations.nRelocations - 1]; - break; - case DT_RELA: - if (!foundRelaSz) - { - awaitingRelaSz = true; - dynEntryAwaitingRelaSz = currentDynamicHeader; - break; - } - current.rel = false; - current.table = currentDynamicHeader; - current.nEntries = last_dtrelasz/sizeof(Elf32_Rela); - append_relocation_table(&relocations, ¤t); - foundRelaSz = false; - last_dtrelasz = 0; - last_found_dtrela = &relocations.buf[relocations.nRelocations - 1]; - break; - case DT_JMPREL: - switch (last_dlpltrel) - { - case DT_REL: - current.rel = true; - current.table = currentDynamicHeader; - current.nEntries = last_dtpltrelsz/sizeof(Elf32_Rel); - append_relocation_table(&relocations, ¤t); - break; - case DT_RELA: - current.rel = false; - current.table = currentDynamicHeader; - current.nEntries = last_dtpltrelsz/sizeof(Elf32_Rela); - append_relocation_table(&relocations, ¤t); - break; - default: - break; - } - last_found_dtjmprel = &relocations.buf[relocations.nRelocations - 1]; - break; - case DT_RELSZ: - last_dtrelsz = currentDynamicHeader->d_un.d_val; - foundRelSz = !awaitingRelSz; - if (awaitingRelSz) - { - current.rel = true; - current.table = dynEntryAwaitingRelSz; - current.nEntries = last_dtrelsz/sizeof(Elf32_Rel); - append_relocation_table(&relocations, ¤t); - last_found_dtrel = &relocations.buf[relocations.nRelocations - 1]; - awaitingRelSz = false; - } - break; - case DT_RELASZ: - last_dtrelasz = currentDynamicHeader->d_un.d_val; - foundRelaSz = !awaitingRelaSz; - if (awaitingRelaSz) - { - current.rel = false; - current.table = dynEntryAwaitingRelaSz; - current.nEntries = last_dtrelasz/sizeof(Elf32_Rela); - append_relocation_table(&relocations, ¤t); - last_found_dtrela = &relocations.buf[relocations.nRelocations - 1]; - awaitingRelaSz = false; - } - break; - case DT_PLTREL: - last_dlpltrel = currentDynamicHeader->d_un.d_val; - break; - case DT_PLTRELSZ: - last_dtpltrelsz = currentDynamicHeader->d_un.d_val; - break; - case DT_STRTAB: - stringTable = currentDynamicHeader->d_un.d_ptr; - break; - case DT_SYMTAB: - symbolTable = (Elf32_Sym*)(base + currentDynamicHeader->d_un.d_ptr); - break; - // case DT_RELACOUNT: - // last_found_dtrela->nEntries = currentDynamicHeader->d_un.d_val; - // break; - // case DT_RELCOUNT: - // last_found_dtrel->nEntries = currentDynamicHeader->d_un.d_val; - // break; - default: - break; - } - } - struct copy_reloc_array copy_relocations = {0,0}; - // For each relocation table, calculate its relocations. - for (size_t index = 0; index < relocations.nRelocations; index++) - { - struct relocation_table table = relocations.buf[index]; - Elf32_Rel* relTable = (Elf32_Rel*)(base + table.table->d_un.d_ptr); - Elf32_Rela* relaTable = (Elf32_Rela*)(base + table.table->d_un.d_ptr); - for (size_t i = 0; i < table.nEntries; i++) - { - struct relocation cur; - if (!table.rel) - { - cur.symbolTableOffset = (uint32_t)(relaTable[i].r_info >> 8), - cur.virtualAddress = relaTable[i].r_offset, - cur.relocationType = (uint16_t)(relaTable[i].r_info & 0xff), - cur.addend = relaTable[i].r_addend; - } - else - { - cur.symbolTableOffset = (uint32_t)(relTable[i].r_info >> 8), - cur.virtualAddress = relTable[i].r_offset, - cur.relocationType = (uint16_t)(relTable[i].r_info & 0xff), - cur.addend = 0; - } - if (!calculate_relocation(status, - driver, - symbolTable, - stringTable, - file, - cur, - base, - szProgram, - GOT, - ©_relocations, - hashTableOffset)) - return nullptr; - } - } - // TODO: Should I do this or not? - GOT[0] += (uintptr_t)base; - // GOT[0] = (uintptr_t)base + dynamic->p_vaddr; - static uint32_t dummy; - GOT[1] = (uintptr_t)&dummy; - GOT[2] = (uintptr_t)dyn_loader_dummy; - for (size_t i = 0; i < copy_relocations.nRelocations; i++) - { - struct copy_reloc reloc = copy_relocations.buf[i]; - memcpy(reloc.dest, reloc.src, reloc.size); - } - - free_copy_reloc_array(©_relocations); - free_reloc_array(&relocations); - - // Set protection. - for (size_t i = 0; i < ehdr->e_phnum; i++) - { - if (phdr_table[i].p_type != PT_LOAD) - continue; - if (!phdr_table[i].p_memsz) - continue; - void* phdrBase = OffsetPtr(base, phdr_table[i].p_vaddr, void*); - phdrBase = (void*)((uintptr_t)phdrBase & ~0xfff); - prot_flags prot = 0; - bool isPageable = false; - if (phdr_table[i].p_flags & PF_X) - prot |= OBOS_PROTECTION_EXECUTABLE; - if ((phdr_table[i].p_flags & PF_R) && !(phdr_table[i].p_flags & PF_W)) - prot |= OBOS_PROTECTION_READ_ONLY; - if (phdr_table[i].p_flags & PF_OBOS_PAGEABLE) - isPageable = true; - else - isPageable = false; - Mm_VirtualMemoryProtect(&Mm_KernelContext, phdrBase, phdr_table[i].p_memsz, prot, isPageable); - } - if (status) - *status = OBOS_STATUS_SUCCESS; - if (dynamicSymbolTable) - *dynamicSymbolTable = symbolTable; - if (nEntriesDynamicSymbolTable) - { - if (!hashTableOffset) - { - // There is no possible way to know the size without a hash table. - - *nEntriesDynamicSymbolTable = 0; - } - else - { - Elf32_Word* hashTable = OffsetPtr(base, hashTableOffset, Elf32_Word*); - *nEntriesDynamicSymbolTable = hashTable[1]; - } - } - if (dynstrtab) - *dynstrtab = OffsetPtr(base, stringTable, const char*); - if (top) - *top = OffsetPtr(base, end, void*); - // Yes, most (nearly all) of the code in this file is stolen from the x86-64 implementation. - return base; -} diff --git a/src/oboskrnl/arch/m68k/entry.c b/src/oboskrnl/arch/m68k/entry.c deleted file mode 100644 index baea8904..00000000 --- a/src/oboskrnl/arch/m68k/entry.c +++ /dev/null @@ -1,381 +0,0 @@ -/* - * oboskrnl/arch/m68k/entry.c - * - * Copyright (c) 2024-2025 Omar Berrow -*/ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include -#include - -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include - -#include -#include - -#include - -timer_frequency CoreS_TimerFrequency; - -OBOS_ALIGNAS(0x10) volatile struct limine_memmap_request Arch_MemmapRequest = { - .id = LIMINE_MEMMAP_REQUEST, - .revision = 0 -}; -OBOS_ALIGNAS(0x10) volatile struct limine_kernel_address_request Arch_KernelAddressRequest = { - .id = LIMINE_KERNEL_ADDRESS_REQUEST, - .revision = 0, -}; -OBOS_ALIGNAS(0x10) volatile struct limine_hhdm_request Arch_HHDMRequest = { - .id = LIMINE_HHDM_REQUEST, - .revision = 0 -}; -OBOS_ALIGNAS(0x10) volatile struct limine_kernel_file_request Arch_KernelFile = { - .id = LIMINE_KERNEL_FILE_REQUEST, - .revision = 0, -}; -OBOS_ALIGNAS(0x10) volatile struct limine_boot_info_request Arch_BootInfo = { - .id = LIMINE_BOOT_INFO_REQUEST, - .revision = 0, -}; -OBOS_ALIGNAS(0x10) volatile struct limine_module_request Arch_InitrdRequest = { - .id = LIMINE_MODULE_REQUEST, - .revision = 0, -}; -cpu_local bsp_cpu; -thread kmain_thread; -thread idle_thread; -static thread_node kmain_node; -static thread_node idle_thread_node; -void Arch_InitializeVectorTable(); -void Arch_RawRegisterInterrupt(uint8_t vec, uintptr_t f); - -struct stack_frame -{ - struct stack_frame* down; - uintptr_t rip; -}; - -stack_frame OBOSS_StackFrameNext(stack_frame curr) -{ - if (!curr) - { - curr = __builtin_frame_address(0); - if (curr->down) - curr = curr->down; // use caller's stack frame, if available - return curr; - } - if (!KASAN_IsAllocated((uintptr_t)&curr->down, sizeof(*curr), false)) - return nullptr; - return curr->down; -} -uintptr_t OBOSS_StackFrameGetPC(stack_frame curr) -{ - if (!curr) - { - curr = __builtin_frame_address(0); - if (curr->down) - curr = curr->down; // use caller's stack frame, if available - } - if (!KASAN_IsAllocated((uintptr_t)&curr->rip, sizeof(*curr), false)) - return 0; - return curr->rip; -} - -// Makeshift frame buffer lol. -// BPP=32 -// Width=1024P -// Height=768P -// Pitch=4096B -// Format=XRGB8888 -// char Arch_Framebuffer[1024*768*4]; -void Arch_KernelEntry(); -uintptr_t Arch_TTYBase = 0; -static char idle_task_stack[0x10000]; -static char kernel_main_stack[0x10000]; -void Arch_IdleTask(); -obos_status Arch_MapPage(page_table pt_root, uintptr_t virt, uintptr_t to, uintptr_t ptFlags); -void Arch_KernelEntryBootstrap() -{ - for (uint16_t irq = 0; irq <= 255; irq++) - { - bsp_cpu.arch_specific.irqs[irq].irql = irq / 32 - 1; - bsp_cpu.arch_specific.irqs[irq].nDefers = 0; - bsp_cpu.arch_specific.irqs[irq].next = bsp_cpu.arch_specific.irqs[irq].prev = nullptr; - } - Core_CpuInfo = &bsp_cpu; - Core_CpuCount = 1; - bsp_cpu.isBSP = true; - bsp_cpu.initialized = true; - bsp_cpu.id = 0; - bsp_cpu.idleThread = &idle_thread; - for (thread_priority i = 0; i <= THREAD_PRIORITY_MAX_VALUE; i++) - Core_CpuInfo[0].priorityLists[i].priority = i; - irql oldIrql = Core_RaiseIrql(IRQL_MASKED); - // OBOS_TextRendererState.fb.base = Arch_Framebuffer; - // OBOS_TextRendererState.fb.bpp = 32; - // OBOS_TextRendererState.fb.height = 768; - // OBOS_TextRendererState.fb.width = 1024; - // OBOS_TextRendererState.fb.pitch = 1024*4; - // OBOS_TextRendererState.fb.format = OBOS_FB_FORMAT_RGBX8888; - - // memzero(Arch_Framebuffer, 1024*768*4); - // OBOS_TextRendererState.font = font_bin; - OBOS_KernelCmdLine = Arch_KernelFile.response->kernel_file->cmdline; - OBOS_ParseCMDLine(); - - uint64_t log_level = OBOS_GetOPTD_Ex("log-level", LOG_LEVEL_DEBUG); - if (log_level > 4) - log_level = LOG_LEVEL_DEBUG; - OBOS_SetLogLevel(log_level); - - OBOS_Debug("Initializing Vector Base Register.\n"); - Arch_InitializeVectorTable(); - OBOS_Debug("Initializing scheduler.\n"); - Core_DefaultThreadAffinity = 1; // we will always only have one cpu - thread_ctx ctx = {}; - memzero(&ctx, sizeof(ctx)); - CoreS_SetupThreadContext(&ctx, (uintptr_t)Arch_KernelEntry, 0, false, kernel_main_stack, 0x10000); - CoreH_ThreadInitialize(&kmain_thread, THREAD_PRIORITY_NORMAL, Core_DefaultThreadAffinity, &ctx); - memzero(&ctx, sizeof(ctx)); - CoreS_SetupThreadContext(&ctx, (uintptr_t)Arch_IdleTask, 0, false, idle_task_stack, 0x10000); - CoreH_ThreadInitialize(&idle_thread, THREAD_PRIORITY_IDLE, Core_DefaultThreadAffinity, &ctx); - CoreH_ThreadReadyNode(&kmain_thread, &kmain_node); - CoreH_ThreadReadyNode(&idle_thread, &idle_thread_node); - bsp_cpu.nReadyThreads--; - Core_LowerIrql(oldIrql); - - // Finally, yield. - OBOS_Debug("Yielding into kernel main thread.\n"); - Core_Yield(); -} -void Arch_InitializePageTables(); -void Arch_PageFaultHandler(interrupt_frame* frame); -void Arch_AddressErrorHandler(interrupt_frame* frame); -extern BootDeviceBase Arch_RTCBase; -void timer_yield(dpc* on, void* udata) -{ - OBOS_UNUSED(on); - OBOS_UNUSED(udata); - Arch_PICMaskIRQ(Arch_RTCBase.irq, false); - Core_Yield(); -} -void sched_timer_hnd(void* unused) -{ - static dpc sched_dpc; - OBOS_UNUSED(unused); - // Turns out you can't do that without breaking things [because the timer needs to be restarted] - // Core_Yield(); - CoreH_InitializeDPC(&sched_dpc, timer_yield, CoreH_CPUIdToAffinity(CoreS_GetCPULocalPtr()->id)); -} - -asm ( - ".section .rodata;" - ".global Arch_ModuleStart;" - ".global Arch_ModuleEnd;" - ".align 8;" - "Arch_ModuleStart:;" - ".incbin \"" OBOS_BINARY_DIRECTORY "/initrd\";" - "Arch_ModuleEnd:;" - ".section .text;" -); -extern const char Arch_ModuleStart[]; -extern const char Arch_ModuleEnd[]; - -static const char* color_to_ansi[] = { - "\x1b[30m", - "\x1b[34m", - "\x1b[32m", - "\x1b[36m", - "\x1b[31m", - "\x1b[35m", - "\x1b[38;5;52m", - "\x1b[38;5;7m", - "\x1b[38;5;8m", - "\x1b[38;5;75m", - "\x1b[38;5;10m", - "\x1b[38;5;14m", - "\x1b[38;5;9m", - "\x1b[38;5;13m", - "\x1b[38;5;11m", - "\x1b[38;5;15m", -}; -/* -typedef struct log_backend { - void* userdata; - void(*write)(const char* buf, size_t sz, void* userdata); - // Can be nullptr. - void(*set_color)(color c, void* userdata); - // Can be nullptr if set_color is nullptr. - void(*reset_color)(void* userdata); -} log_backend; -*/ - -OBOS_NO_UBSAN void tty_print(const char* buf, size_t sz, void *data) -{ - OBOS_UNUSED(data); - for (size_t i = 0; i < sz; i++) - ((volatile uint32_t*)Arch_TTYBase)[0] = buf[i]; // Enable device through CMD register -} -void tty_set_color(color c, void* udata) -{ - tty_print(color_to_ansi[c], strlen(color_to_ansi[c]), udata); -} -void tty_reset_color(void *udata) -{ - tty_print("\x1b[0m", 4, udata); -} -static log_backend tty_backend = { .write=tty_print, .set_color=tty_set_color, .reset_color=tty_reset_color }; - -void OBOSS_KernelPostTmInit() -{ - OBOS_Debug("%s: Initializing scheduler timer.\n", __func__); - static timer sched_timer; - sched_timer.handler = sched_timer_hnd; - sched_timer.userdata = nullptr; - Core_TimerObjectInitialize(&sched_timer, TIMER_MODE_INTERVAL, 4000); -} -void OBOSS_KernelPostPMMInit() -{ - OBOS_Debug("%s: Initializing page tables.\n", __func__); - Arch_InitializePageTables(); - Arch_TTYBase = Arch_BootInfo.response->uart_phys_base; - if (Arch_TTYBase) - { - uintptr_t pt_root = 0; - asm ("movec.l %%srp, %0" :"=r"(pt_root) :); - Arch_MapPage(pt_root, 0xffffe000, Arch_TTYBase, 0b11|(1<<7)|(0b11 << 5)); - Arch_TTYBase = 0xffffe000; - ((uint32_t*)Arch_TTYBase)[2] = 0; // disable IRQs. - static basicmm_region tty_region = {}; - OBOSH_BasicMMAddRegion(&tty_region, (void*)Arch_TTYBase, 0x1000); - tty_region.mmioRange = true; - OBOS_AddLogSource(&tty_backend); - } -} - -void OBOSS_GetModule(struct boot_module *module, const char* name) -{ - OBOSS_GetModuleLen(module, name, strlen(name)); -} - -void OBOSS_GetModuleLen(struct boot_module *module, const char* name, size_t name_len) -{ - if (strncmp(name, "initrd", name_len)) - { - module->address = (void*)Arch_InitrdRequest.response->modules[0]->address; - module->size = Arch_InitrdRequest.response->modules[0]->size; - module->is_kernel = false; - module->is_memory = false; - module->name = "initrd"; - } - else if (strncmp(name, "__KERNEL__", name_len)) - { - module->address = (void*)Arch_KernelFile.response->kernel_file->address; - module->size = Arch_KernelFile.response->kernel_file->size; - module->is_kernel = true; - module->is_memory = false; - module->name = "__KERNEL__"; - } - else if (strncmp(name, "initrd_driver", name_len)) - { - module->address = (void*)Arch_ModuleStart; - module->size = (uintptr_t)Arch_ModuleEnd - (uintptr_t)Arch_ModuleStart; - module->is_kernel = false; - module->is_memory = false; - module->name = "initrd_driver"; - } -} - -void OBOSS_GetKernelModule(struct boot_module *module) -{ - OBOSS_GetModuleLen(module, "__KERNEL__", sizeof("__KERNEL__")); -} - -void OBOSS_KernelPostKProcInit() -{ - Core_ProcessAppendThread(OBOS_KernelProcess, &kmain_thread); - Core_ProcessAppendThread(OBOS_KernelProcess, &idle_thread); -} -void OBOSS_KernelPostIRQInit() -{ - OBOSS_InitializeSyscallInterface(); -} - -void Arch_KernelEntry() -{ - Arch_RawRegisterInterrupt(0x2, (uintptr_t)Arch_PageFaultHandler); - Arch_RawRegisterInterrupt(0x3, (uintptr_t)Arch_AddressErrorHandler); - Arch_RawRegisterInterrupt(24, (uintptr_t)Arch_PICHandleSpurious); - for (uint8_t vec = 25; vec < 32; vec++) - Arch_RawRegisterInterrupt(vec, (uintptr_t)Arch_PICHandleIRQ); - - OBOS_KernelInit(); - - Core_ExitCurrentThread(); -} - -void Arch_IdleTask() -{ - while(1); -} - -cpu_local* CoreS_GetCPULocalPtr() -{ - return &bsp_cpu; -} - -BootInfoTag* Arch_GetBootInfo(BootInfoType type) -{ - return Arch_GetBootInfoFrom(type, nullptr); -} -BootInfoTag* Arch_GetBootInfoFrom(BootInfoType type, BootInfoTag* tag) -{ - if (!tag) - tag = (BootInfoTag*)Arch_BootInfo.response->base; - else - tag = (BootInfoTag*)((uintptr_t)tag + tag->size); - BootInfoTag* found = tag; - while(found) - { - if (found->type == type) - return found; - found = (BootInfoTag*)((uintptr_t)found + found->size); - if (found->type == BootInfoType_Last) - found = nullptr; - } - return nullptr; -} diff --git a/src/oboskrnl/arch/m68k/exception_handlers.c b/src/oboskrnl/arch/m68k/exception_handlers.c deleted file mode 100644 index 039beb55..00000000 --- a/src/oboskrnl/arch/m68k/exception_handlers.c +++ /dev/null @@ -1,87 +0,0 @@ -/* - * oboskrnl/arch/m68k/exception_handlers.c - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include -#include -#include - -#include -#include - -#include - -#include - -// well technically it's an access fault but whatever -void Arch_PageFaultHandler(interrupt_frame* frame) -{ - uint32_t mm_ec = 0; - if (frame->format_7.ssw.atc) - mm_ec &= ~PF_EC_PRESENT; - if (!frame->format_7.ssw.rw) - mm_ec |= PF_EC_RW; - if (!(frame->sr & (1<<13) /* Supervisor */)) - mm_ec |= PF_EC_UM; - if (Mm_IsInitialized()) - { - mm_ec &= ~PF_EC_PRESENT; - page_info curr = {}; - context* ctx = CoreS_GetCPULocalPtr()->currentContext; - MmS_QueryPageInfo(ctx->pt, frame->format_7.fa & ~0xfff, &curr, nullptr); - if (curr.prot.present) - mm_ec |= PF_EC_PRESENT; - obos_status status = Mm_HandlePageFault(ctx, frame->format_7.fa & ~0xfff, mm_ec); - switch (status) - { - case OBOS_STATUS_SUCCESS: - return; - case OBOS_STATUS_UNHANDLED: - break; - default: - { - static const char format[] = "Handling page fault with error code 0x%x on address %08x failed.\n"; - OBOS_Warning(format, mm_ec, frame->format_7.fa); - break; - } - } - } - OBOS_Panic(OBOS_PANIC_EXCEPTION, - "Access fault in %s-mode at 0x%08x while trying to %s the %spresent page at 0x%08x.\nRegister dump:\n" - "d0: 0x%08x, d1: 0x%08x, d2: 0x%08x, d3: 0x%08x\n" - "d1: 0x%08x, d5: 0x%08x, d6: 0x%08x, d7: 0x%08x\n" - "a0: 0x%08x, a1: 0x%08x, a2: 0x%08x, a3: 0x%08x\n" - "a4: 0x%08x, a5: 0x%08x, a6: 0x%08x, sp: 0x%08x\n" - "pc: 0x%08x, sr: 0x%08x\n", - (mm_ec & PF_EC_UM) ? "user" : "kernel", - frame->pc, - (mm_ec & PF_EC_RW) ? "write" : "read", - (mm_ec & PF_EC_PRESENT) ? "" : "non-", - frame->format_7.fa, - frame->d0, frame->d1, frame->d2, frame->d3, - frame->d4, frame->d5, frame->d6, frame->d7, - frame->a0, frame->a1, frame->a2, frame->a3, - frame->a4, frame->a5, frame->a6, frame->usp, - frame->pc, frame->sr - ); -} - -void Arch_AddressErrorHandler(interrupt_frame* frame) -{ - OBOS_Panic(OBOS_PANIC_EXCEPTION, - "address fault at 0x%08x.\nRegister dump:\n" - "d0: 0x%08x, d1: 0x%08x, d2: 0x%08x, d3: 0x%08x\n" - "d1: 0x%08x, d5: 0x%08x, d6: 0x%08x, d7: 0x%08x\n" - "a0: 0x%08x, a1: 0x%08x, a2: 0x%08x, a3: 0x%08x\n" - "a4: 0x%08x, a5: 0x%08x, a6: 0x%08x, sp: 0x%08x\n" - "pc: 0x%08x, sr: 0x%08x\n", - frame->pc, - frame->d0, frame->d1, frame->d2, frame->d3, - frame->d4, frame->d5, frame->d6, frame->d7, - frame->a0, frame->a1, frame->a2, frame->a3, - frame->a4, frame->a5, frame->a6, frame->usp, - frame->pc, frame->sr - ); -} \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/execve.c b/src/oboskrnl/arch/m68k/execve.c deleted file mode 100644 index 582ea7d9..00000000 --- a/src/oboskrnl/arch/m68k/execve.c +++ /dev/null @@ -1,148 +0,0 @@ -/* - * oboskrnl/arch/m68k/execve.c - * - * Copyright (c) 2025 Omar Berrow - */ - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include -#include - -#include - -#include - -typedef struct -{ - int a_type; - union { - long a_val; - void *a_ptr; - void (*a_fnc)(); - } a_un; -} auxv_t; - -enum { - AT_NULL, - AT_IGNORE, - AT_EXECFD, - AT_PHDR, - AT_PHENT, - AT_PHNUM, - AT_PAGESZ, - AT_BASE, - AT_FLAGS, - AT_ENTRY, -}; - -static void allocate_string_vector_on_stack(char** vec, size_t cnt) -{ - OBOS_ENSURE(CoreS_ThreadAlloca != nullptr); - for (size_t i = 0; i < cnt; i++) - { - size_t str_len = strlen(vec[i]); - char* str = CoreS_ThreadAlloca(&Core_GetCurrentThread()->context, str_len+1, nullptr); - if (!str) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "obos shat itself: your stack is not big enough to hold all these arguments"); - memcpy_k_to_usr(str, vec[i], str_len+1); - // stck_buf[i] = str; - vec[i] = str; - } -} - -static void write_vector_to_stack(char** vec, char** stck_buf, size_t cnt) -{ - for (size_t i = 0; i < cnt; i++) - stck_buf[i] = vec[i]; - stck_buf[cnt] = 0; -} - -static void reset_extended_state() -{ - // TODO: Implement - OBOS_Debug("m68k execve: %s unimplemented", __func__); -} - -OBOS_NORETURN void OBOSS_HandControlTo(struct context* ctx, struct exec_aux_values* aux) -{ - if (Core_GetIrql() < IRQL_DISPATCH) - { - irql oldIrql = Core_RaiseIrql(IRQL_DISPATCH); - OBOS_UNUSED(oldIrql); - } - - Core_GetCurrentThread()->context.usp = (uintptr_t)Core_GetCurrentThread()->context.stackBase + Core_GetCurrentThread()->context.stackSize; - // 1+argc+char*[argc]+NULL+char*[envpc]+NULL+4(aux)+1 - // 1+1+argc+1+envpc+1+8+1=9+envpc+argc - size_t szAllocation = (13+aux->envpc+aux->argc)*sizeof(uint64_t); - allocate_string_vector_on_stack(aux->argv, aux->argc); - allocate_string_vector_on_stack(aux->envp, aux->envpc); - Core_GetCurrentThread()->context.usp &= ~0xf; - if (!((aux->argc+aux->envpc) % 2)) - Core_GetCurrentThread()->context.usp -= 8; - - void* uinit_vals = CoreS_ThreadAlloca(&Core_GetCurrentThread()->context, szAllocation, nullptr); - uint32_t* init_vals = Mm_MapViewOfUserMemory(ctx, uinit_vals, nullptr, szAllocation, 0, false, nullptr); - init_vals[0] = aux->argc; - - auxv_t* auxv = (auxv_t*)&init_vals[1+aux->argc+1+aux->envpc+1]; - - (*auxv).a_type = AT_PHDR; - (*auxv).a_un.a_ptr = aux->phdr.ptr; - auxv++; - - (*auxv).a_type = AT_PHENT; - (*auxv).a_un.a_val = aux->phdr.phent; - auxv++; - - (*auxv).a_type = AT_PHNUM; - (*auxv).a_un.a_val = aux->phdr.phnum; - auxv++; - - (*auxv).a_type = AT_ENTRY; - (*auxv).a_un.a_ptr = (void*)aux->elf.entry; - auxv++; - - (*auxv).a_type = AT_NULL; - - write_vector_to_stack(aux->argv, (char**)(init_vals+1), aux->argc); - write_vector_to_stack(aux->envp, (char**)(init_vals+1+aux->argc+1), aux->envpc); - - OBOS_Debug("Handing off control to user program.\n"); - OBOS_Debug("NOTE: SP=0x%p.\n", Core_GetCurrentThread()->context.usp); - - uintptr_t udata[2] = { aux->elf.real_entry, ctx->pt }; - CoreS_SetKernelStack(Core_GetCurrentThread()->kernelStack); - Core_GetCurrentThread()->context.pc = udata[0]; - Core_GetCurrentThread()->context.urp = udata[1]; - Core_GetCurrentThread()->context.a6 = Core_GetCurrentThread()->context.usp; - Core_GetCurrentThread()->context.sr &= ~BIT(13); - CoreS_SwitchToThreadContext(&Core_GetCurrentThread()->context); - OBOS_UNREACHABLE; -} - -void OBOSS_HandOffToInit(struct exec_aux_values* aux) -{ - OBOS_OpenStandardFDs(OBOS_CurrentHandleTable()); - - irql oldIrql = Core_RaiseIrql(IRQL_DISPATCH); - OBOS_UNUSED(oldIrql); - context* ctx = CoreS_GetCPULocalPtr()->currentContext; - - Core_GetCurrentThread()->context.stackBase = Mm_VirtualMemoryAlloc(ctx, nullptr, 4*1024*1024, OBOS_PROTECTION_USER_PAGE, VMA_FLAGS_GUARD_PAGE, nullptr, nullptr); - Core_GetCurrentThread()->context.stackSize = 4*1024*1024; - - CoreS_SetThreadPageTable(&Core_GetCurrentThread()->context, ctx->pt); - - OBOSS_HandControlTo(ctx, aux); -} diff --git a/src/oboskrnl/arch/m68k/gf_tty.c b/src/oboskrnl/arch/m68k/gf_tty.c deleted file mode 100644 index 411cbb8a..00000000 --- a/src/oboskrnl/arch/m68k/gf_tty.c +++ /dev/null @@ -1,191 +0,0 @@ -/* - * oboskrnl/arch/m68k/gf_tty.c - * - * Copyright (c) 2025-2026 Omar Berrow -*/ - -#include -#include -#include -#include - -#include -#include -#include -#include - -#include -#include -#include - -#include -#include - -#include - -#include - -#include -#include - -extern uintptr_t Arch_TTYBase; - -struct gf_tty { - volatile uint32_t put_char; - volatile const uint32_t bytes_ready; - volatile uint32_t command; - volatile uint32_t resv; - volatile uint32_t data_ptr; - volatile uint32_t data_len; - volatile uint32_t data_ptr_high; -}; - -enum { - CMD_INT_DISABLE, - CMD_INT_ENABLE, - CMD_WRITE_BUFFER, - CMD_READ_BUFFER, -}; - -static struct gf_tty_iface { - tty* tty; - event data_ready_evnt; - struct { - char buffer[512]; - uintptr_t buffer_phys; - size_t out_ptr; - size_t in_ptr; - } ring_buffer; - size_t bytes_count; // total amount of bytes ever read from this tty - void(*data_ready)(void* tty, const void* buf, size_t nBytesReady); - thread* data_ready_thread; -} tty_iface_obj = { - .data_ready_evnt = EVENT_INITIALIZE(EVENT_NOTIFICATION) -}; - -static void set_data_ready_cb(void* tty, void(*cb)(void* tty, const void* buf, size_t nBytesReady)) -{ - struct tty* t = tty; - struct gf_tty_iface* iface = t->interface.userdata; - iface->data_ready = cb; -} -static obos_status write(void* tty, const char* buf, size_t szBuf) -{ - OBOS_UNUSED(tty); - struct gf_tty* dev = (void*)Arch_TTYBase; - for (size_t i = 0; i < szBuf; i++) - dev->put_char = buf[i]; - return OBOS_STATUS_SUCCESS; -} - -tty_interface tty_iface = { - .userdata = &tty_iface_obj, - .set_data_ready_cb = set_data_ready_cb, - .write = write, - .tcdrain = nullptr, - .size.row=30, - .size.col=95, -}; - -static void poll_gf_tty(struct gf_tty_iface* iface) -{ - while (!iface->tty) - OBOSS_SpinlockHint(); - // struct gf_tty* dev = (void*)Arch_TTYBase; - while (1) - { - if (obos_is_error(Core_WaitOnObject(WAITABLE_OBJECT(iface->data_ready_evnt)))) - continue; - irql oldIrql = Core_RaiseIrql(IRQL_DISPATCH); - if (iface->data_ready) - { - iface->data_ready(iface->tty, iface->ring_buffer.buffer, iface->ring_buffer.out_ptr); - iface->ring_buffer.out_ptr -= iface->ring_buffer.out_ptr; - } - Core_LowerIrql(oldIrql); - Core_EventClear(&iface->data_ready_evnt); - } - Core_ExitCurrentThread(); -} - -void gf_irq_hnd(struct irq* i, interrupt_frame* frame, void* userdata, irql oldIrql) -{ - OBOS_UNUSED(i && frame && userdata && oldIrql); - struct gf_tty* dev = (void*)Arch_TTYBase; - Core_EventSet(&tty_iface_obj.data_ready_evnt, false); - tty_iface_obj.bytes_count += dev->bytes_ready; - size_t nToRead = OBOS_MIN(dev->bytes_ready, sizeof(tty_iface_obj.ring_buffer.buffer) - tty_iface_obj.ring_buffer.out_ptr); - if (!tty_iface_obj.ring_buffer.buffer_phys) - { - MmS_QueryPageInfo(Mm_KernelContext.pt, (uintptr_t)tty_iface_obj.ring_buffer.buffer, nullptr, &tty_iface_obj.ring_buffer.buffer_phys); - tty_iface_obj.ring_buffer.buffer_phys += ((uintptr_t)tty_iface_obj.ring_buffer.buffer) % OBOS_PAGE_SIZE; - } - dev->data_len = nToRead; - dev->data_ptr_high = 0; - dev->data_ptr = tty_iface_obj.ring_buffer.buffer_phys + tty_iface_obj.ring_buffer.out_ptr; - dev->command = CMD_READ_BUFFER; - tty_iface_obj.ring_buffer.out_ptr += nToRead; - while (dev->bytes_ready) - OBOSS_SpinlockHint(); - // printf("%d bytes read\n", nToRead); - // printf("%d bytes ever read\n", tty_iface_obj.bytes_count); - // dev->command = CMD_INT_DISABLE; -} - -static BootDeviceBase gf_tty_info; - -void tty_irq_move_callback(struct irq* i, struct irq_vector* from, struct irq_vector* to, void* userdata) -{ - OBOS_UNUSED(i); - OBOS_UNUSED(from); - OBOS_UNUSED(userdata); - Arch_PICRegisterIRQ(gf_tty_info.irq, to->id + 0x40); -} - -static irq gf_tty_irq; - -void OBOSS_MakeTTY() -{ - // boot-tty is ignored. - - char* boot_tty = OBOS_GetOPTS("boot-tty"); - if (boot_tty) OBOS_Warning("boot-tty option is ignored for the m68k\n"); - Free(OBOS_KernelAllocator, boot_tty, strlen(boot_tty)+1); - - gf_tty_info = *(BootDeviceBase*)(Arch_GetBootInfo(BootInfoType_GoldfishTtyBase)+1); - - tty_iface_obj.data_ready_thread = CoreH_ThreadAllocate(nullptr); - thread_ctx ctx = {}; - void* stack = Mm_VirtualMemoryAlloc( - &Mm_KernelContext, - nullptr, 0x4000, - 0, VMA_FLAGS_KERNEL_STACK, - nullptr,nullptr); - CoreS_SetupThreadContext(&ctx, (uintptr_t)poll_gf_tty, (uintptr_t)&tty_iface_obj, false, stack, 0x4000); - CoreH_ThreadInitialize(tty_iface_obj.data_ready_thread, THREAD_PRIORITY_HIGH, Core_DefaultThreadAffinity, &ctx); - Core_ProcessAppendThread(OBOS_KernelProcess, tty_iface_obj.data_ready_thread); - CoreH_ThreadReady(tty_iface_obj.data_ready_thread); - - gf_tty_info.irq = 31; - - gf_tty_irq.handler = gf_irq_hnd; - gf_tty_irq.moveCallback = tty_irq_move_callback; - gf_tty_irq.handlerUserdata = nullptr; - Core_IrqObjectInitializeIRQL(&gf_tty_irq, IRQL_DISPATCH, false, true); - - Arch_PICMaskIRQ(gf_tty_info.irq, true); - Arch_PICRegisterIRQ(gf_tty_info.irq, gf_tty_irq.vector->id + 0x40); - - struct gf_tty* dev = (void*)Arch_TTYBase; - dev->command = CMD_INT_ENABLE; - - dirent* ent = nullptr; - Vfs_RegisterTTY(&tty_iface, &ent, TTY_SERIAL); - tty_iface_obj.tty = ent->vnode->data; - - Core_MakeSession(OBOS_KernelProcess, nullptr); - OBOS_KernelProcess->session->controlling_tty = ent->vnode->data; - ((struct tty*)ent->vnode->data)->fg_job = OBOS_KernelProcess->pgrp; - - Arch_PICMaskIRQ(gf_tty_info.irq, false); -} \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/goldfish_pic.c b/src/oboskrnl/arch/m68k/goldfish_pic.c deleted file mode 100644 index 503d4f8b..00000000 --- a/src/oboskrnl/arch/m68k/goldfish_pic.c +++ /dev/null @@ -1,185 +0,0 @@ -/* - * oboskrnl/arch/m68k/goldfish_pic.c - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include -#include -#include - -#include -#include -#include -#include - -#include -#include -#include -#include - -#include - -#include - -#include - -obos_status Arch_MapPage(page_table pt_root, uintptr_t virt, uintptr_t to, uintptr_t ptFlags); -pic* Arch_PICBases; -size_t Arch_PICCount; -static void initialize() -{ - uintptr_t pt_root = 0; - asm ("movec.l %%srp, %0" :"=r"(pt_root) :); - // Detect all PICs. - BootInfoTag* cur = Arch_GetBootInfoFrom(BootInfoType_GoldfishPicBase, nullptr); - Arch_PICCount = 6; - Arch_PICBases = ZeroAllocate(OBOS_NonPagedPoolAllocator, Arch_PICCount, sizeof(*Arch_PICBases), nullptr); - for (size_t i = 0; i < Arch_PICCount; i++) - { - memzero(&Arch_PICBases[i], sizeof(Arch_PICBases[0])); - Arch_PICBases[i].phys_base = i ? Arch_PICBases[i - 1].phys_base+0x1000 : ((BootDeviceBase*)(cur+1))->base; - uintptr_t virt_base = - (uintptr_t)Mm_VirtualMemoryAlloc( - &Mm_KernelContext, - nullptr, - 0x1000, - OBOS_PROTECTION_CACHE_DISABLE, - VMA_FLAGS_NON_PAGED, - nullptr, nullptr); - uintptr_t oldPhys = 0; - MmS_QueryPageInfo(MmS_GetCurrentPageTable(), virt_base, nullptr, &oldPhys); - // Map as RW, Cache Disabled, and Supervisor - Arch_MapPage(MmS_GetCurrentPageTable(), virt_base, Arch_PICBases[i].phys_base, (0b11|(0b11<<5)|(1<<7))); - Mm_FreePhysicalPages(oldPhys, 1); - Arch_PICBases[i].base = virt_base; - } -} -static OBOS_NO_KASAN uint32_t read_register(pic* pic, uint8_t offset) -{ - if (!Arch_PICBases) - initialize(); - return ((volatile uint32_t*)pic->base)[offset/4]; -} -static OBOS_NO_KASAN void write_register(pic* pic, uint8_t offset, uint32_t val) -{ - if (!Arch_PICBases) - initialize(); - ((volatile uint32_t*)pic->base)[offset/4] = val; -} -#define PENDING 0x00 -#define NUMBER 0x04 -#define DISABLE_ALL 0x08 -#define DISABLE 0x0C -#define ENABLE 0x10 -void Arch_PICClearPending(pic* on) -{ - write_register(on, DISABLE_ALL, 0); -} -uint8_t Arch_PICGetPendingCount(pic* on) -{ - return read_register(on, PENDING); -} -void Arch_PICDisable(pic* on, uint32_t line) -{ - irql oldIrql = Core_RaiseIrql(IRQL_MASKED); - write_register(on, DISABLE, 1U<irqs[line].masked = true; - Core_LowerIrql(oldIrql); -} -void Arch_PICEnable(pic* on, uint32_t line) -{ - irql oldIrql = Core_RaiseIrql(IRQL_MASKED); - write_register(on, ENABLE, 1U<irqs[line].masked = false; - Core_LowerIrql(oldIrql); -} -uint32_t Arch_PICGetPending(pic* on) -{ - return read_register(on, NUMBER); -} -void Arch_PICRegisterIRQ(uint32_t line_number, uint8_t irq) -{ - if (!Arch_PICBases) - initialize(); - uint8_t pic_index = line_number/32; // see qemu/hw/m68k/virt.c:58 - uint8_t line = line_number%32; - Arch_PICBases[pic_index].irqs[line].vector = irq; -} -void Arch_PICUnregisterIRQ(uint32_t line_number) -{ - if (!Arch_PICBases) - initialize(); - uint8_t pic_index = line_number/32; // see qemu/hw/m68k/virt.c:58 - uint8_t line = line_number%32; - Arch_PICBases[pic_index].irqs[line].vector = 0; - Arch_PICDisable(&Arch_PICBases[pic_index], line); -} -void Arch_PICMaskIRQ(uint32_t line_number, bool mask) -{ - if (!Arch_PICBases) - initialize(); - uint8_t pic_index = line_number/32; // see qemu/hw/m68k/virt.c:58 - uint8_t line = line_number%32; - Arch_PICBases[pic_index].irqs[line].masked = mask; - if (!mask) - Arch_PICEnable(&Arch_PICBases[pic_index], line); - else - Arch_PICDisable(&Arch_PICBases[pic_index], line); -} -extern uintptr_t Arch_IRQHandlers[256]; -static void on_defer_1(void* udata) -{ - uint32_t line_number = (uint32_t)udata; - uint8_t pic_index = line_number/32; // see qemu/hw/m68k/virt.c:58 - uint8_t line = line_number%32; - Arch_PICBases[pic_index].irqs[line].masked = false; - Arch_PICEnable(&Arch_PICBases[pic_index], line); -} -void CoreS_SendEOI(interrupt_frame* frame) -{ - OBOS_UNUSED(frame); - // for (size_t i = 0; i < Arch_PICCount; i++) - // Arch_PICClearPending(&Arch_PICBases[i]); -} -void Arch_SetHardwareIPL(uint8_t to); -void Arch_PICHandleIRQ(interrupt_frame* frame) -{ - pic* cur = Arch_PICBases; - for (size_t i = 0; i < Arch_PICCount && cur; i++, cur++) - { - int32_t pending = Arch_PICGetPendingCount(cur); - uint32_t line = 0; - while (--pending >= 0) - { - line = __builtin_ctz(Arch_PICGetPending(cur)); - if (!cur->irqs[line].masked) - { - interrupt_frame iframe = {}; - memcpy(&iframe, frame, sizeof(*frame)-sizeof(frame->format_7)); - iframe.intNumber = cur->irqs[line].vector; - iframe.vector = cur->irqs[line].vector - 0x40; - cur->irqs[line].masked = true; - Arch_PICDisable(cur, line); - if (Arch_IRQHandlers[cur->irqs[line].vector]) - ((void(*)(interrupt_frame*))Arch_IRQHandlers[cur->irqs[line].vector])(&iframe); - Arch_SetHardwareIPL(7); - if (CoreS_GetCPULocalPtr()->arch_specific.irqs[cur->irqs[line].vector].nDefers) - { - uint32_t line_pic = ((i * 32)) + line; - CoreS_GetCPULocalPtr()->arch_specific.irqs[cur->irqs[line].vector].on_defer_callback = on_defer_1; - CoreS_GetCPULocalPtr()->arch_specific.irqs[cur->irqs[line].vector].udata = (void*)line_pic; - } - else - { - cur->irqs[line].masked = false; - Arch_PICEnable(cur, line); - } - } - } - } -} -void Arch_PICHandleSpurious(interrupt_frame* unused) -{ - OBOS_UNUSED(unused); -} \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/goldfish_pic.h b/src/oboskrnl/arch/m68k/goldfish_pic.h deleted file mode 100644 index ffd4c430..00000000 --- a/src/oboskrnl/arch/m68k/goldfish_pic.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * oboskrnl/arch/m68k/goldfish_pic.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include - -#include - -typedef struct pic_irq { - uint8_t vector; - bool masked; -} pic_irq; -typedef struct pic -{ - uintptr_t base; - uintptr_t phys_base; - pic_irq irqs[32]; -} pic; -extern pic* Arch_PICBases; -extern size_t Arch_PICCount; -void Arch_PICClearPending(pic* on); -void Arch_PICDisable(pic*, uint32_t line); -void Arch_PICEnable(pic*, uint32_t line); -uint8_t Arch_PICGetPendingCount(pic*); -uint32_t Arch_PICGetPending(pic*); -void Arch_PICRegisterIRQ(uint32_t line, uint8_t irq); -void Arch_PICUnregisterIRQ(uint32_t line); -void Arch_PICMaskIRQ(uint32_t line, bool mask); -void Arch_PICHandleIRQ(interrupt_frame*); -void Arch_PICHandleSpurious(interrupt_frame*); \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/goldfish_rtc.c b/src/oboskrnl/arch/m68k/goldfish_rtc.c deleted file mode 100644 index f19fa675..00000000 --- a/src/oboskrnl/arch/m68k/goldfish_rtc.c +++ /dev/null @@ -1,175 +0,0 @@ -/* - * oboskrnl/arch/m68k/goldfish_rtc.c - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include -#include -#include - -#include -#include -#include - -#include -#include -#include - -#include -#include -#include -#include - -obos_status Arch_MapPage(page_table pt_root, uintptr_t virt, uintptr_t to, uintptr_t ptFlags); - -// Because of qemu weirdness, this can be used as a timer. -BootDeviceBase Arch_RTCBase; -// typedef struct gf_rtc -// { -// const uint32_t time_low; // read first -// const uint32_t time_high; // read second -// uint32_t alarm_low; // write second to arm -// uint32_t alarm_high; // write first -// uint32_t enable_irq; -// uint32_t clear_alarm; -// const uint32_t alarm_status; -// uint32_t clear_irq; -// } OBOS_PACK gf_rtc; -#define TIME_LOW 0x00 /* The low dword of the time register */ -#define TIME_HIGH 0x04 /* The high dword of the time register */ -#define ALARM_LOW 0x08 /* The low dword of the alarm register */ -#define ALARM_HIGH 0x0C /* The high dword of the alarm register */ -#define ENABLE_IRQ 0x10 /* Write one to enable IRQs, or zero to disable IRQs. */ -#define CLEAR_ALARM 0x14 /* Clears the alarm register */ -#define ALARM_STATUS 0x18 /* On read, returns whether the alarm is running (non zero) or not (zero)*/ -#define CLEAR_IRQ 0x1c /* Clears pending IRQs on write */ -typedef uint32_t gf_rtc; -static const uint64_t ns_period = 4000000; -static OBOS_NO_KASAN uint32_t read_register32(volatile gf_rtc* rtc, uint8_t reg) -{ - return rtc[reg/4]; -} -static OBOS_NO_KASAN uint64_t read_register64(volatile gf_rtc* rtc, uint8_t reg) -{ - uint32_t half1 = (uint32_t)(read_register32(rtc, reg)); - uint32_t half2 = (uint32_t)(read_register32(rtc, reg+4)); - return (uint64_t)half1|((uint64_t)half2 << 32); -} -static OBOS_NO_KASAN void write_register32(volatile gf_rtc* rtc, uint8_t reg, uint32_t val) -{ - if (reg <= TIME_HIGH) - return; // Drop the read - rtc[reg/4] = val; -} - -static OBOS_NO_KASAN void write_register64(volatile gf_rtc* rtc, uint8_t reg, uint64_t val) -{ - write_register32(rtc, reg+4, val>>32); - write_register32(rtc, reg, val); -} - -static void set_alarm(volatile gf_rtc* rtc) -{ - write_register64(rtc, ALARM_LOW, read_register64(rtc, TIME_LOW) + ns_period); - write_register32(rtc, ENABLE_IRQ, 1); -} - -timer_tick CoreS_GetTimerTick() -{ - volatile gf_rtc* rtc = (volatile gf_rtc*)(Arch_RTCBase.base); - return read_register64(rtc,TIME_LOW); -} - -uint64_t CoreS_GetNativeTimerFrequency() -{ - return CoreS_TimerFrequency; -} -timer_tick CoreS_GetNativeTimerTick() -{ - return CoreS_GetTimerTick(); -} - -void rtc_irq_move_callback(struct irq* i, struct irq_vector* from, struct irq_vector* to, void* userdata) -{ - OBOS_UNUSED(i); - OBOS_UNUSED(from); - OBOS_UNUSED(to); - OBOS_UNUSED(userdata); - Arch_PICRegisterIRQ(Arch_RTCBase.irq, to->id + 0x40); -} - -void rtc_irq_handler(struct irq* i, interrupt_frame* frame, void* userdata, irql oldIrql) -{ - OBOS_UNUSED(i); - OBOS_UNUSED(frame); - OBOS_UNUSED(userdata); - OBOS_UNUSED(oldIrql); - write_register32((volatile gf_rtc*)Arch_RTCBase.base, CLEAR_IRQ, 1); - ((irq_handler)userdata)(i, frame, nullptr, oldIrql); - // set_alarm((volatile gf_rtc*)(Arch_RTCBase.base)); -} - -// TODO: Implement -// bool rtc_check_irq_callback(struct irq* i, void* userdata) -// { -// OBOS_UNUSED(i); -// OBOS_UNUSED(userdata); -// return ((volatile gf_rtc*)(Arch_RTCBase.base))->alarm_status; -// } -OBOS_PAGEABLE_FUNCTION OBOS_NO_UBSAN obos_status CoreS_InitializeTimer(irq_handler handler) -{ - if (Arch_RTCBase.base) - return OBOS_STATUS_ALREADY_INITIALIZED; - if (!handler) - return OBOS_STATUS_INVALID_ARGUMENT; - obos_status status = Core_IrqObjectInitializeIRQL(Core_TimerIRQ, IRQL_TIMER, false, true); - if (obos_is_error(status)) - return status; - Core_TimerIRQ->moveCallback = rtc_irq_move_callback; - // Core_TimerIRQ->irqChecker = rtc_check_irq_callback; - Core_TimerIRQ->handler = rtc_irq_handler; - Core_TimerIRQ->handlerUserdata = handler; - BootInfoTag* rtc_tag = Arch_GetBootInfo(BootInfoType_GoldfishRtcBase); - if (!rtc_tag) - return OBOS_STATUS_INTERNAL_ERROR; - Arch_RTCBase = *(BootDeviceBase*)(rtc_tag + 1); - Arch_RTCBase.irq -= 8; // weirdness - // Map it. - uintptr_t virt_base = - (uintptr_t)Mm_VirtualMemoryAlloc( - &Mm_KernelContext, - nullptr, - 0x1000, - OBOS_PROTECTION_CACHE_DISABLE, - VMA_FLAGS_NON_PAGED, - nullptr, nullptr); - uintptr_t oldPhys = 0; - MmS_QueryPageInfo(MmS_GetCurrentPageTable(), virt_base, nullptr, &oldPhys); - // Map as RW, Cache Disabled, and Supervisor - Arch_MapPage(MmS_GetCurrentPageTable(), virt_base, Arch_RTCBase.base, (0b11|(0b11<<5)|(1<<7))); - Mm_FreePhysicalPages(oldPhys, 1); - Arch_RTCBase.base = virt_base; - irql oldIrql = Core_RaiseIrql(IRQL_TIMER); - volatile gf_rtc* rtc = (volatile gf_rtc*)(Arch_RTCBase.base); - Arch_PICMaskIRQ(Arch_RTCBase.irq, true); - Arch_PICRegisterIRQ(Arch_RTCBase.irq, Core_TimerIRQ->vector->id + 0x40); - // Reset the alarm by clearing it and clearing the irq status. - write_register32(rtc, CLEAR_ALARM, 1); - write_register32(rtc, CLEAR_IRQ, 1); - // Enable the alarm IRQ. - CoreS_TimerFrequency = 250; - write_register64(rtc, ALARM_LOW, read_register64(rtc, TIME_LOW) + ns_period); - write_register32(rtc, ENABLE_IRQ, 1); - Arch_PICMaskIRQ(Arch_RTCBase.irq, false); - Core_LowerIrql(oldIrql); - return OBOS_STATUS_SUCCESS; -} - -obos_status CoreS_ResetTimer() -{ - if (!Arch_RTCBase.base) - return OBOS_STATUS_UNINITIALIZED; - set_alarm((volatile gf_rtc*)(Arch_RTCBase.base)); - return OBOS_STATUS_SUCCESS; -} diff --git a/src/oboskrnl/arch/m68k/interrupt_frame.h b/src/oboskrnl/arch/m68k/interrupt_frame.h deleted file mode 100644 index eb2ac37d..00000000 --- a/src/oboskrnl/arch/m68k/interrupt_frame.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * oboskrnl/arch/m68k/interrupt_frame.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include - -struct OBOS_PACK ssw_68040 { - uint16_t cp : 1; - uint16_t cu : 1; - uint16_t ct : 1; - uint16_t cm : 1; - uint16_t ma : 1; - uint16_t atc : 1; - uint16_t lk : 1; - uint16_t rw : 1; - uint16_t x : 1; - uint16_t size : 2; - uint16_t tt : 2; - uint16_t tm : 3; -}; -typedef struct interrupt_frame -{ - uint32_t intNumber; - uint32_t vector; // intNumber-64 - uintptr_t usp; - uintptr_t d0,d1,d2,d3,d4,d5,d6,d7; - uintptr_t a0,a1,a2,a3,a4,a5,a6; - uint16_t padding; - uint16_t sr; - uintptr_t pc; - uint16_t unused; - struct format_7 - { - // Effective address (whatever that means) - uint32_t ea; - struct ssw_68040 ssw; - uint16_t wb3s, wb2s, wb1s; - // Fault address - uint32_t fa; - uint32_t wb3a, wb3d, wb2a, fb2d, wb1a, wb1d; - uint32_t pd1, pd2, pd3; - } OBOS_PACK format_7; -} OBOS_PACK interrupt_frame; \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/irq.c b/src/oboskrnl/arch/m68k/irq.c deleted file mode 100644 index 36b6d311..00000000 --- a/src/oboskrnl/arch/m68k/irq.c +++ /dev/null @@ -1,49 +0,0 @@ -/* - * oboskrnl/arch/m68k/irq.c - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include -#include -#include -#include - -#include - -#include -#include - -#include -#include - -uint32_t vector_base[256]; -uintptr_t Arch_IRQHandlers[256]; -extern void isr_stub(); - -void Arch_InitializeVectorTable() -{ - for (size_t i = 0; i < 256; i++) - vector_base[i] = (uintptr_t)isr_stub; - asm volatile("movec.l %0, %%vbr;" : :"r"(vector_base) :); -} -void Arch_RawRegisterInterrupt(uint8_t vec, uintptr_t f) -{ - Arch_IRQHandlers[vec] = f; -} -obos_status CoreS_RegisterIRQHandler(irq_vector_id vector, void(*handler)(interrupt_frame* frame)) -{ - obos_status s = OBOS_STATUS_SUCCESS; - if ((s = CoreS_IsIRQVectorInUse(vector)) && handler) - return s; - if ((uintptr_t)handler < OBOS_KERNEL_ADDRESS_SPACE_BASE && handler) - return OBOS_STATUS_INVALID_ARGUMENT; - Arch_IRQHandlers[vector + 0x40] = (uintptr_t)handler; - return OBOS_STATUS_SUCCESS; -} -OBOS_PAGEABLE_FUNCTION obos_status CoreS_IsIRQVectorInUse(irq_vector_id vector) -{ - if (vector > OBOS_MAX_INTERRUPT_VECTORS) - return OBOS_STATUS_INVALID_ARGUMENT; - return Arch_IRQHandlers[vector+0x40] ? OBOS_STATUS_IN_USE : OBOS_STATUS_SUCCESS; -} \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/irql.c b/src/oboskrnl/arch/m68k/irql.c deleted file mode 100644 index 67931fe3..00000000 --- a/src/oboskrnl/arch/m68k/irql.c +++ /dev/null @@ -1,174 +0,0 @@ -/* - * oboskrnl/arch/m68k/irql.c - * - * Copyright (c) 2024 Omar Berrow -*/ - -// Emulates IRQLs for the m68k. - -#include - -#include - -#include -#include - -#include -#include - -irql* Core_GetIRQLVar(); - -void Arch_SetHardwareIPL(uint8_t to) -{ - to &= 0b111; - uint16_t sr = (getSR() & ~(7<<8)) | (to<<8); - setSR(sr); -} -uint8_t Arch_GetHardwareIPL() -{ - return (getSR() << 8) & 0b111; -} -bool CoreS_EnterIRQHandler(interrupt_frame *frame) -{ - m68k_dirq* irqs = CoreS_GetCPULocalPtr()->arch_specific.irqs; - m68k_dirq_list* deferred = &CoreS_GetCPULocalPtr()->arch_specific.deferred; - if (*Core_GetIRQLVar() >= (frame->intNumber / 32 - 1)) - { - // IRQL >= irq means that the IRQ should be defferred - if (!irqs[frame->intNumber].nDefers) - { - // Add it to the list - Arch_SetHardwareIPL(7); // 7=masked - m68k_dirq *node = &irqs[frame->intNumber]; - if (deferred->tail && deferred->tail->irql < node->irql) - { - // Append it - if (deferred->tail) - deferred->tail->next = node; - if (!deferred->head) - deferred->head = node; - node->prev = deferred->tail; - deferred->tail = node; - deferred->nNodes++; - } - else if (deferred->tail && deferred->tail->irql > node->irql) - { - // Prepend it - if (deferred->head) - deferred->head->next = node; - if (!deferred->tail) - deferred->tail = node; - node->prev = deferred->head; - deferred->head = node; - deferred->nNodes++; - } - else - { - // Find the node that this node should go after. - m68k_dirq* found = nullptr, * n = deferred->tail; - while (n && n->next) - { - if (n->irql < node->irql && - n->next->irql > node->irql) - { - found = n; - break; - } - - n = n->next; - } - if (!found) - { - // Append it - if (deferred->tail) - deferred->tail->next = node; - if (!deferred->head) - deferred->head = node; - node->prev = deferred->tail; - deferred->tail = node; - deferred->nNodes++; - goto done; - } - if (found->next) - found->next->prev = node; - if (deferred->tail == found) - deferred->tail = node; - node->next = found->next; - found->next = node; - node->prev = found; - deferred->nNodes++; - } - done: - (void)0; - } - irqs[frame->intNumber].nDefers++; // the amount of times the IRQ has occurred. - return false; - } - if (irqs[frame->intNumber].nDefers && !(--irqs[frame->intNumber].nDefers)) - { - // Last defer. - if (&irqs[frame->intNumber] == deferred->head) - deferred->head = irqs[frame->intNumber].next; - if (&irqs[frame->intNumber] == deferred->tail) - deferred->tail = irqs[frame->intNumber].prev; - if (irqs[frame->intNumber].prev) - irqs[frame->intNumber].prev->next = irqs[frame->intNumber].next; - if (irqs[frame->intNumber].next) - irqs[frame->intNumber].next->prev = irqs[frame->intNumber].prev; - deferred->nNodes--; - } - Arch_SetHardwareIPL(0); - return true; // it can run. -} -void CoreS_ExitIRQHandler(interrupt_frame *frame) -{ - OBOS_UNUSED(frame); - Arch_SetHardwareIPL(7); - return; -} -void Arch_CallDeferredIrq(m68k_dirq* irq, m68k_dirq* table) -{ - uint8_t vector = irq-table; - Arch_SetHardwareIPL(7); - Arch_SimulateIRQ(vector); - Arch_SetHardwareIPL(0); - irq->on_defer_callback(irq->udata); -} -void CoreS_SetIRQL(uint8_t to, uint8_t old) -{ - if (to == IRQL_MASKED) - Arch_SetHardwareIPL(7); // avoid a lot of needless deferring. - else - Arch_SetHardwareIPL(0); - if (to >= old) - return; - CoreS_GetCPULocalPtr()->currentIrql = to; - m68k_dirq_list* deferred = &CoreS_GetCPULocalPtr()->arch_specific.deferred; - // If to < current irql, run all defferred IRQs. - // Start at the highest priority IRQs. - for (m68k_dirq* curr = deferred->tail; curr; ) - { - if (to >= curr->irql) - { - curr = curr->prev; - continue; // it's not time yet - } - m68k_dirq* prev = curr->prev; - while (curr->nDefers != 0) - Arch_CallDeferredIrq(curr, CoreS_GetCPULocalPtr()->arch_specific.irqs); - if (curr->prev) - curr->prev->next = curr->next; - if (curr->next) - curr->next->prev = curr->prev; - if (deferred->tail == curr) - deferred->tail = curr->prev; - if (deferred->head == curr) - deferred->head = curr->next; - deferred->nNodes--; - curr = prev; - } -} -uint8_t CoreS_GetIRQL() -{ - return *Core_GetIRQLVar(); -} \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/isr.S b/src/oboskrnl/arch/m68k/isr.S deleted file mode 100644 index 17c7431c..00000000 --- a/src/oboskrnl/arch/m68k/isr.S +++ /dev/null @@ -1,93 +0,0 @@ -/* - * oboskrnl/arch/m68k/isr.S - * - * Copyright (c) 2024 Omar Berrow -*/ - -.global isr_stub -.global Arch_SimulateIRQ -.extern Arch_IrqHandlers - -isr_stub: - .cfi_startproc simple - .cfi_signal_frame - - // Disable IRQs - or #0x700, %sr - -// Align the stack - move.w #0, -(%sp) - - .cfi_def_cfa %sp, 0 - .cfi_offset %pc, 4 - -// Push the GPRs d0-d7 and a0-a6. -// Functionally equivalent to a pushad on x86 - movem.l %d0-%d7/%a0-%a6, %sp@- - .cfi_adjust_cfa_offset 60 - -// Push the user stack pointer - move.l %usp, %a0 - move.l %a0, %sp@- - .cfi_adjust_cfa_offset 4 - -// Push 'vector' - move.l #0, %d0 - move.l #0, %d1 - move.w (0x48,%sp), %d0 - and #0xfff, %d0 - divu #4, %d0 - move.w %d0, %d1 - sub.l #64, %d1 - move.l %d1, -(%sp) - move.l %d0, -(%sp) - .cfi_adjust_cfa_offset 8 - -// Call the handler - mulu #4, %d0 - move.l (%d0,Arch_IRQHandlers), %a0 - - move.l %sp, -(%sp) - .cfi_adjust_cfa_offset 4 - - cmp #0, %a0 - beq .L1 - - jsr (%a0) - -.L1: -// Restore GPRs - - add #12, %sp - .cfi_adjust_cfa_offset -12 - - move.l %sp@+, %a0 - move.l %a0, %usp - .cfi_adjust_cfa_offset -4 - - movem.l %sp@+, %d0-%d7/%a0-%a6 - .cfi_adjust_cfa_offset -60 - - add #2, %sp - - rte - .cfi_endproc -// Simulates an incoming IRQ on the vector passed. -Arch_SimulateIRQ: -// d0: Contains the VBR. -// d1: Contains the vector. -// a0: Contains the vector handler. - move.l (4,%sp), %d1 - movec.l %vbr, %d0 - mulu #4, %d1 - move.l %d1, %a0 - move.w %d1, %sp@- - move.l #ret, %sp@- - move.w %sr, %sp@- - adda.l %d0, %a0 - move.l (%a0), %a0 - jmp (%a0) -ret: - rts - -.section .note.GNU-stack,"",%progbits diff --git a/src/oboskrnl/arch/m68k/loader/Allocator.h b/src/oboskrnl/arch/m68k/loader/Allocator.h deleted file mode 100644 index b72b6e91..00000000 --- a/src/oboskrnl/arch/m68k/loader/Allocator.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include - -extern "C" -{ - void* malloc(size_t length); - void free(void* ptr, size_t length); -} - -namespace sl -{ - struct DefaultAllocator - { - constexpr DefaultAllocator() - {} - - [[nodiscard]] - inline void* Allocate(size_t length) - { - return malloc(length); - } - - inline void Deallocate(void* ptr, size_t length) - { - free(ptr, length); - } - }; -} \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/loader/CMakeLists.txt b/src/oboskrnl/arch/m68k/loader/CMakeLists.txt deleted file mode 100644 index f035839e..00000000 --- a/src/oboskrnl/arch/m68k/loader/CMakeLists.txt +++ /dev/null @@ -1,43 +0,0 @@ -# oboskrnl/arch/m68k/loader/CMakeLists.txt -# -# Copyright (c) 2024 Omar Berrow - -# CMake file for the m68k loader "borrowed" from northport - -add_executable(m68k_bootloader) - -target_sources(m68k_bootloader PRIVATE - "Entry.S" "Main.cpp" "Loader.cpp" "Memory.cpp" - "Syslib.cpp" "Util.cpp") - -add_dependencies(m68k_bootloader oboskrnl) - -set_source_files_properties( - "Loader.cpp" - PROPERTIES - OBJECT_DEPENDS "${OUTPUT_DIR}/oboskrnl" -) - -target_compile_options(m68k_bootloader - PRIVATE "-w" - PRIVATE "-fstack-protector-strong" - PRIVATE "-ffreestanding" - PRIVATE "-std=c++17" - PRIVATE "-fno-rtti" - PRIVATE "-fno-exceptions" - PRIVATE "-fno-unwind-tables" - PRIVATE "-fno-asynchronous-unwind-tables" - PRIVATE "-g" - PRIVATE "-m68040" -) -target_link_options(m68k_bootloader - PRIVATE "-nostdlib" - PRIVATE "-static" - PRIVATE "-T" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/Linker.lds" -) -target_compile_definitions(m68k_bootloader - PRIVATE NPL_ENABLE_LOGGING=1 - PRIVATE OUTPUT_DIRECTORY="${OUTPUT_DIR}" -) - -target_include_directories(m68k_bootloader PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/loader/CppUtils.h b/src/oboskrnl/arch/m68k/loader/CppUtils.h deleted file mode 100644 index 1d5d6cb5..00000000 --- a/src/oboskrnl/arch/m68k/loader/CppUtils.h +++ /dev/null @@ -1,126 +0,0 @@ -#pragma once - -namespace sl -{ - template - struct _RemoveReference - { using Type = T; }; - - template - struct _RemoveReference - { using Type = T; }; - - template - struct _RemoveReference - { using Type = T; }; - - template - using RemoveReferenceType = typename _RemoveReference::Type; - - template - constexpr inline bool IsLValueReference = false; - - template - constexpr inline bool IsLValueReference = true; - - template - struct EnableIf - {}; - - template - struct EnableIf - { using Type = T; }; - - template - constexpr inline bool IsSame = false; - - template - constexpr inline bool IsSame = true; - - template - struct _MakeUnsigned - { using Type = void; }; - - template<> - struct _MakeUnsigned - { using Type = unsigned char; }; - - template<> - struct _MakeUnsigned - { using Type = unsigned short; }; - - template<> - struct _MakeUnsigned - { using Type = unsigned int; }; - - template<> - struct _MakeUnsigned - { using Type = unsigned long; }; - - template<> - struct _MakeUnsigned - { using Type = unsigned long long; }; - - template<> - struct _MakeUnsigned - { using Type = unsigned char; }; - - template<> - struct _MakeUnsigned - { using Type = unsigned short; }; - - template<> - struct _MakeUnsigned - { using Type = unsigned int; }; - - template<> - struct _MakeUnsigned - { using Type = unsigned long; }; - - template<> - struct _MakeUnsigned - { using Type = unsigned long long; }; - - template<> - struct _MakeUnsigned - { using Type = unsigned char; }; - - template<> - struct _MakeUnsigned - { using Type = bool; }; - - template - using MakeUnsigned = typename _MakeUnsigned::Type; - - template - constexpr inline bool IsUnsigned = IsSame>; - - template - constexpr RemoveReferenceType&& Move(T&& t) noexcept - { return static_cast&&>(t); } - - template - void Swap(T& a, T& b) - { - T temp = Move(a); - a = Move(b); - b = Move(temp); - } - - template - constexpr T&& Forward(RemoveReferenceType& param) - { return static_cast(param); } - - template - constexpr T&& Forward(RemoveReferenceType&& param) - { - static_assert(!IsLValueReference, "Cant forward rvalue as lvalue."); - return static_cast(param); - } - - template - constexpr T* Launder(T* p) - { - return __builtin_launder(p); - } -} \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/loader/Elf.h b/src/oboskrnl/arch/m68k/loader/Elf.h deleted file mode 100644 index 9a75e0da..00000000 --- a/src/oboskrnl/arch/m68k/loader/Elf.h +++ /dev/null @@ -1,52 +0,0 @@ -#pragma once - -#if __SIZEOF_POINTER__ == 8 -# error what are you doing? how the hell did you end up here? -#elif __SIZEOF_POINTER__ == 4 - #include - - #define Elf_Addr Elf32_addr - #define Elf_Off Elf32_Off - #define Elf_Half Elf32_Half - #define Elf_Word Elf32_Word - #define Elf_Sword Elf32_Sword - #define Elf_Xword Elf32_Xword - #define Elf_Sxword Elf32_Sxword - #define Elf_UnsignedChar Elf32_UnsignedChar - #define Elf_Ehdr Elf32_Ehdr - #define Elf_Shdr Elf32_Shdr - #define Elf_Sym Elf32_Sym - #define Elf_Rel Elf32_Rel - #define Elf_Rela Elf32_Rela - #define Elf_Phdr Elf32_Phdr - #define Elf_Dyn Elf32_Dyn - - #define ELF_ST_BIND ELF32_ST_BIND - #define ELF_ST_TYPE ELF32_ST_TYPE - #define ELF_ST_INFO ELF32_ST_INFO - #define ELF_ST_VISIBILITY ELF32_ST_VISIBILITY - #define ELF_R_TYPE ELF32_R_TYPE - #define ELF_R_SYM ELF32_R_SYM - #define ELF_R_INFO ELF32_R_INFO -#else - #error "Unsupported ELF spec" -#endif -#include - -namespace sl -{ - bool ValidateElfHeader(const void* file, Elf_Half type); - - struct ComputedReloc - { - uintptr_t value; - size_t length; - bool usedSymbol; - }; - - ComputedReloc ComputeRelocation(Elf_Word type, uintptr_t a, uintptr_t b, uintptr_t s, uintptr_t p); - - sl::Vector FindPhdrs(const Elf_Ehdr* hdr, Elf_Word type); - const Elf_Shdr* FindShdr(const Elf_Ehdr* hdr, const char* name); - sl::Vector FindShdrs(const Elf_Ehdr* hdr, Elf_Word type); -} \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/loader/Elf32.h b/src/oboskrnl/arch/m68k/loader/Elf32.h deleted file mode 100644 index 7dd5da8e..00000000 --- a/src/oboskrnl/arch/m68k/loader/Elf32.h +++ /dev/null @@ -1,253 +0,0 @@ -#pragma once - -#include - -namespace sl -{ - using Elf32_Addr = uint32_t; - using Elf32_Off = uint32_t; - using Elf32_Half = uint16_t; - using Elf32_Word = uint32_t; - using Elf32_Sword = int32_t; - using Elf32_UnsignedChar = uint8_t; - - struct [[gnu::packed]] Elf32_Ehdr - { - Elf32_UnsignedChar e_ident[16]; - Elf32_Half e_type; - Elf32_Half e_machine; - Elf32_Word e_version; - Elf32_Addr e_entry; - Elf32_Off e_phoff; - Elf32_Off e_shoff; - Elf32_Word e_flags; - Elf32_Half e_ehsize; - Elf32_Half e_phentsize; - Elf32_Half e_phnum; - Elf32_Half e_shentsize; - Elf32_Half e_shnum; - Elf32_Half e_shstrndx; - }; - - constexpr static Elf32_UnsignedChar ExpectedMagic[] = { 0x7F, 'E', 'L', 'F' }; - - constexpr static Elf32_UnsignedChar EI_MAG0 = 0; - constexpr static Elf32_UnsignedChar EI_MAG1 = 1; - constexpr static Elf32_UnsignedChar EI_MAG2 = 2; - constexpr static Elf32_UnsignedChar EI_MAG3 = 3; - constexpr static Elf32_UnsignedChar EI_CLASS = 4; - constexpr static Elf32_UnsignedChar EI_DATA = 5; - constexpr static Elf32_UnsignedChar EI_VERSION = 6; - constexpr static Elf32_UnsignedChar EI_PAD = 7; - constexpr static Elf32_UnsignedChar EI_NIDENT = 16; - - constexpr static Elf32_UnsignedChar ELFCLASSNONE = 0; - constexpr static Elf32_UnsignedChar ELFCLASS32 = 1; - constexpr static Elf32_UnsignedChar ELFCLASS64 = 2; - - constexpr static Elf32_UnsignedChar ELFDATANONE = 0; - constexpr static Elf32_UnsignedChar ELFDATA2LSB = 1; - constexpr static Elf32_UnsignedChar ELFDATA2MSB = 2; - - constexpr static Elf32_Half ET_NONE = 0; - constexpr static Elf32_Half ET_REL = 1; - constexpr static Elf32_Half ET_EXEC = 2; - constexpr static Elf32_Half ET_DYN = 3; - constexpr static Elf32_Half ET_CORE = 4; - constexpr static Elf32_Half ET_LOPROC = 0xFF00; - constexpr static Elf32_Half ET_HIPROC = 0xFFFF; - - constexpr static Elf32_Half EM_NONE = 0; - constexpr static Elf32_Half EM_M32 = 1; - constexpr static Elf32_Half EM_SPARC = 2; - constexpr static Elf32_Half EM_386 = 3; - constexpr static Elf32_Half EM_68K = 4; - constexpr static Elf32_Half EM_88K = 5; - constexpr static Elf32_Half EM_860 = 7; - constexpr static Elf32_Half EM_MIPS = 8; - - constexpr static Elf32_Word EV_CURRENT = 1; - - constexpr static Elf32_Half SHN_UNDEF = 0; - constexpr static Elf32_Half SHN_LORESERVE = 0xFF00; - constexpr static Elf32_Half SHN_LOPROC = 0xFF00; - constexpr static Elf32_Half SHN_HIPROC = 0xFF1F; - constexpr static Elf32_Half SHN_ABS = 0xFFF1; - constexpr static Elf32_Half SHN_COMMON = 0xFFF2; - constexpr static Elf32_Half SHN_HIRESERVE = 0xFFFF; - - struct [[gnu::packed]] Elf32_Shdr - { - Elf32_Word sh_name; - Elf32_Word sh_type; - Elf32_Word sh_flags; - Elf32_Addr sh_addr; - Elf32_Off sh_offset; - Elf32_Word sh_size; - Elf32_Word sh_link; - Elf32_Word sh_info; - Elf32_Word sh_addralign; - Elf32_Word sh_entsize; - }; - - constexpr static Elf32_Word SHT_NULL = 0; - constexpr static Elf32_Word SHT_PROGBITS = 1; - constexpr static Elf32_Word SHT_SYMTAB = 2; - constexpr static Elf32_Word SHT_STRTAB = 3; - constexpr static Elf32_Word SHT_RELA = 4; - constexpr static Elf32_Word SHT_HASH = 5; - constexpr static Elf32_Word SHT_DYNAMIC = 6; - constexpr static Elf32_Word SHT_NOTE = 7; - constexpr static Elf32_Word SHT_NOBITS = 8; - constexpr static Elf32_Word SHT_REL = 9; - constexpr static Elf32_Word SHT_SHLIB = 10; - constexpr static Elf32_Word SHT_DYNSYM = 11; - constexpr static Elf32_Word SHT_LOPROC = 0x7000'0000; - constexpr static Elf32_Word SHT_HIPROC = 0x7FFF'FFFF; - constexpr static Elf32_Word SHT_LOUSER = 0x8000'0000; - constexpr static Elf32_Word SHT_HIUSER = 0xFFFF'FFFF; - - constexpr static Elf32_Word SHF_WRITE = 1 << 0; - constexpr static Elf32_Word SHF_ALLOC = 1 << 1; - constexpr static Elf32_Word SHF_EXECINSTR = 1 << 2; - constexpr static Elf32_Word SHF_MASKPROC = 0xF000'0000; - - struct [[gnu::packed]] Elf32_Sym - { - Elf32_Word st_name; - Elf32_Addr st_value; - Elf32_Word st_size; - Elf32_UnsignedChar st_info; - Elf32_UnsignedChar st_other; - Elf32_Half st_shndx; - }; - -#define ELF32_ST_BIND(i) ((i) >> 4) -#define ELF32_ST_TYPE(i) ((i) & 0xF) -#define ELF32_ST_INFO (b, t) (((b) << 4) + ((t) & 0xF)) -#define ELF32_ST_VISIBILITY(o) ((o) & 0x3) - - constexpr static Elf32_UnsignedChar STB_LOCAL = 0; - constexpr static Elf32_UnsignedChar STB_GLOBAL = 1; - constexpr static Elf32_UnsignedChar STB_WEAK = 2; - constexpr static Elf32_UnsignedChar STB_LOPROC = 13; - constexpr static Elf32_UnsignedChar STB_HIPROC = 15; - - constexpr static Elf32_UnsignedChar STT_NOTYPE = 0; - constexpr static Elf32_UnsignedChar STT_OBJECT = 1; - constexpr static Elf32_UnsignedChar STT_FUNC = 2; - constexpr static Elf32_UnsignedChar STT_SECTION = 3; - constexpr static Elf32_UnsignedChar STT_FILE = 4; - constexpr static Elf32_UnsignedChar STT_LOPROC = 13; - constexpr static Elf32_UnsignedChar STT_HIPROC = 15; - - constexpr static Elf32_UnsignedChar STV_DEFAULT = 0; - constexpr static Elf32_UnsignedChar STV_INTERNAL = 1; - constexpr static Elf32_UnsignedChar STV_HIDDEN = 2; - constexpr static Elf32_UnsignedChar STV_PROTECTED = 3; - - struct [[gnu::packed]] Elf32_Rel - { - Elf32_Addr r_offset; - Elf32_Word r_info; - }; - - struct [[gnu::packed]] Elf32_Rela - { - Elf32_Addr r_offset; - Elf32_Word r_info; - Elf32_Sword r_addend; - }; - -#define ELF32_R_SYM(i) ((i) >> 8) -#define ELF32_R_TYPE(i) ((uint8_t)(i)) -#define ELF32_R_INFO(s, t) (((s) << 8) + (uint8_t)(t)) - - constexpr static Elf32_Word R_68K_NONE = 0; - constexpr static Elf32_Word R_68K_32 = 1; - constexpr static Elf32_Word R_68K_16 = 2; - constexpr static Elf32_Word R_68K_8 = 3; - constexpr static Elf32_Word R_68K_PC32 = 4; - constexpr static Elf32_Word R_68K_PC16 = 5; - constexpr static Elf32_Word R_68K_PC8 = 6; - constexpr static Elf32_Word R_68K_GOT32 = 7; - constexpr static Elf32_Word R_68K_GOT16 = 8; - constexpr static Elf32_Word R_68K_GOT8 = 9; - constexpr static Elf32_Word R_68K_GOT32O = 10; - constexpr static Elf32_Word R_68K_GOT16O = 11; - constexpr static Elf32_Word R_68K_GOT8O = 12; - constexpr static Elf32_Word R_68K_PLT32 = 13; - constexpr static Elf32_Word R_68K_PLT16 = 14; - constexpr static Elf32_Word R_68K_PLT8 = 15; - constexpr static Elf32_Word R_68K_PLT32O = 16; - constexpr static Elf32_Word R_68K_PLT16O = 17; - constexpr static Elf32_Word R_68K_PLT8O = 18; - constexpr static Elf32_Word R_68K_COPY = 19; - constexpr static Elf32_Word R_68K_GLOB_DAT = 20; - constexpr static Elf32_Word R_68K_JMP_SLOT = 21; - constexpr static Elf32_Word R_68K_RELATIVE = 22; - - struct [[gnu::packed]] Elf32_Phdr - { - Elf32_Word p_type; - Elf32_Off p_offset; - Elf32_Addr p_vaddr; - Elf32_Addr p_paddr; - Elf32_Word p_filesz; - Elf32_Word p_memsz; - Elf32_Word p_flags; - Elf32_Word p_align; - }; - - constexpr static Elf32_Word PT_NULL = 0; - constexpr static Elf32_Word PT_LOAD = 1; - constexpr static Elf32_Word PT_DYNAMIC = 2; - constexpr static Elf32_Word PT_INTERP = 3; - constexpr static Elf32_Word PT_NOTE = 4; - constexpr static Elf32_Word PT_SHLIB = 5; - constexpr static Elf32_Word PT_PHDR = 6; - constexpr static Elf32_Word PT_LOPROC = 0x7000'0000; - constexpr static Elf32_Word PT_HIPROC = 0x7FFF'FFFF; - - constexpr static Elf32_Word PF_X = 1 << 0; - constexpr static Elf32_Word PF_W = 1 << 1; - constexpr static Elf32_Word PF_R = 1 << 2; - constexpr static Elf32_Word PF_MASKPROC = 0xFF00'0000; - - struct [[gnu::packed]] Elf32_Dyn - { - Elf32_Sword d_tag; - union - { - Elf32_Word d_val; - Elf32_Addr d_ptr; - }; - }; - - constexpr static Elf32_Sword DT_NULL = 0; - constexpr static Elf32_Sword DT_NEEDED = 1; - constexpr static Elf32_Sword DT_PLTRELSZ = 2; - constexpr static Elf32_Sword DT_PLTGOT = 3; - constexpr static Elf32_Sword DT_HASH = 4; - constexpr static Elf32_Sword DT_STRTAB = 5; - constexpr static Elf32_Sword DT_SYMTAB = 6; - constexpr static Elf32_Sword DT_RELA = 7; - constexpr static Elf32_Sword DT_RELASZ = 8; - constexpr static Elf32_Sword DT_RELAENT = 9; - constexpr static Elf32_Sword DT_STRSZ = 10; - constexpr static Elf32_Sword DT_SYMENT = 11; - constexpr static Elf32_Sword DT_INIT = 12; - constexpr static Elf32_Sword DT_FINI = 13; - constexpr static Elf32_Sword DT_SONAME = 14; - constexpr static Elf32_Sword DT_RPATH = 15; - constexpr static Elf32_Sword DT_SYMBOLIC = 16; - constexpr static Elf32_Sword DT_REL = 17; - constexpr static Elf32_Sword DT_RELSZ = 18; - constexpr static Elf32_Sword DT_RELENT = 19; - constexpr static Elf32_Sword DT_PLTREL = 20; - constexpr static Elf32_Sword DT_DEBUG = 21; - constexpr static Elf32_Sword DT_TEXTREL = 22; - constexpr static Elf32_Sword DT_JMPREL = 23; - constexpr static Elf32_Sword DT_LPROC = 0x7000'0000; - constexpr static Elf32_Sword DT_HPROC = 0x7FFF'FFFF; -} \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/loader/Entry.S b/src/oboskrnl/arch/m68k/loader/Entry.S deleted file mode 100644 index 9cb3555b..00000000 --- a/src/oboskrnl/arch/m68k/loader/Entry.S +++ /dev/null @@ -1,28 +0,0 @@ -.global LoaderEntry -.global LoaderExit - -.extern LoaderEntryNext - -LoaderEntry: - lea StackTop, %a7 - jmp LoaderEntryNext - -# void LoaderExit(uintptr_t hhdmbase, uintptr_t jumpTarget) -LoaderExit: - move.l 8(%a7), %a0 - - lea StackTop, %a1 - add.l 4(%a7), %a1 - exg %a1, %a7 - - move.l #0, -(%a7) - move.l #0, -(%a7) - jmp (%a0) - -.section .bss -.align 16 -StackBase: -.zero 0x4000 -StackTop: - -.section .note.GNU-stack,"",%progbits diff --git a/src/oboskrnl/arch/m68k/loader/LICENSE.txt b/src/oboskrnl/arch/m68k/loader/LICENSE.txt deleted file mode 100644 index 8fe66148..00000000 --- a/src/oboskrnl/arch/m68k/loader/LICENSE.txt +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) Dean T. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/loader/Limine.h b/src/oboskrnl/arch/m68k/loader/Limine.h deleted file mode 100644 index 2f2b047e..00000000 --- a/src/oboskrnl/arch/m68k/loader/Limine.h +++ /dev/null @@ -1,596 +0,0 @@ -/* BSD Zero Clause License */ - -/* Copyright (C) 2022-2023 mintsuki and contributors. - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION - * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN - * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef _LIMINE_H -#define _LIMINE_H 1 - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -/* Misc */ - -#ifdef LIMINE_NO_POINTERS -# define LIMINE_PTR(TYPE) uint64_t -#else -# define LIMINE_PTR(TYPE) TYPE -#endif - -#ifdef __GNUC__ -# define LIMINE_DEPRECATED __attribute__((__deprecated__)) -# define LIMINE_DEPRECATED_IGNORE_START \ - _Pragma("GCC diagnostic push") \ - _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") -# define LIMINE_DEPRECATED_IGNORE_END \ - _Pragma("GCC diagnostic pop") -#else -# define LIMINE_DEPRECATED -# define LIMINE_DEPRECATED_IGNORE_START -# define LIMINE_DEPRECATED_IGNORE_END -#endif - -#define LIMINE_COMMON_MAGIC 0xc7b1dd30df4c8b88, 0x0a82e883a194f07b - -struct limine_uuid { - uint32_t a; - uint16_t b; - uint16_t c; - uint8_t d[8]; -}; - -#define LIMINE_MEDIA_TYPE_GENERIC 0 -#define LIMINE_MEDIA_TYPE_OPTICAL 1 -#define LIMINE_MEDIA_TYPE_TFTP 2 - -struct limine_file { - uint64_t revision; - LIMINE_PTR(void *) address; - uint64_t size; - LIMINE_PTR(const char *) path; - LIMINE_PTR(const char *) cmdline; - uint32_t media_type; - uint32_t unused; - uint32_t tftp_ip; - uint32_t tftp_port; - uint32_t partition_index; - uint32_t mbr_disk_id; - struct limine_uuid gpt_disk_uuid; - struct limine_uuid gpt_part_uuid; - struct limine_uuid part_uuid; -}; - -/* Boot info */ - -#define LIMINE_BOOTLOADER_INFO_REQUEST { LIMINE_COMMON_MAGIC, 0xf55038d8e2a1202f, 0x279426fcf5f59740 } - -struct limine_bootloader_info_response { - uint64_t revision; - LIMINE_PTR(const char *) name; - LIMINE_PTR(const char *) version; -}; - -struct limine_bootloader_info_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_bootloader_info_response *) response; -}; - -/* Stack size */ - -#define LIMINE_STACK_SIZE_REQUEST { LIMINE_COMMON_MAGIC, 0x224ef0460a8e8926, 0xe1cb0fc25f46ea3d } - -struct limine_stack_size_response { - uint64_t revision; -}; - -struct limine_stack_size_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_stack_size_response *) response; - uint64_t stack_size; -}; - -/* HHDM */ - -#define LIMINE_HHDM_REQUEST { LIMINE_COMMON_MAGIC, 0x48dcf1cb8ad2b852, 0x63984e959a98244b } - -struct limine_hhdm_response { - uint64_t revision; - uint64_t offset; -}; - -struct limine_hhdm_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_hhdm_response *) response; -}; - -/* Framebuffer */ - -#define LIMINE_FRAMEBUFFER_REQUEST { LIMINE_COMMON_MAGIC, 0x9d5827dcd881dd75, 0xa3148604f6fab11b } - -#define LIMINE_FRAMEBUFFER_RGB 1 - -struct limine_video_mode { - uint64_t pitch; - uint64_t width; - uint64_t height; - uint16_t bpp; - uint8_t memory_model; - uint8_t red_mask_size; - uint8_t red_mask_shift; - uint8_t green_mask_size; - uint8_t green_mask_shift; - uint8_t blue_mask_size; - uint8_t blue_mask_shift; -}; - -struct limine_framebuffer { - LIMINE_PTR(void *) address; - uint64_t width; - uint64_t height; - uint64_t pitch; - uint16_t bpp; - uint8_t memory_model; - uint8_t red_mask_size; - uint8_t red_mask_shift; - uint8_t green_mask_size; - uint8_t green_mask_shift; - uint8_t blue_mask_size; - uint8_t blue_mask_shift; - uint8_t unused[7]; - uint64_t edid_size; - LIMINE_PTR(void *) edid; - /* Response revision 1 */ - uint64_t mode_count; - LIMINE_PTR(struct limine_video_mode **) modes; -}; - -struct limine_framebuffer_response { - uint64_t revision; - uint64_t framebuffer_count; - LIMINE_PTR(struct limine_framebuffer **) framebuffers; -}; - -struct limine_framebuffer_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_framebuffer_response *) response; -}; - -/* Terminal */ - -#define LIMINE_TERMINAL_REQUEST { LIMINE_COMMON_MAGIC, 0xc8ac59310c2b0844, 0xa68d0c7265d38878 } - -#define LIMINE_TERMINAL_CB_DEC 10 -#define LIMINE_TERMINAL_CB_BELL 20 -#define LIMINE_TERMINAL_CB_PRIVATE_ID 30 -#define LIMINE_TERMINAL_CB_STATUS_REPORT 40 -#define LIMINE_TERMINAL_CB_POS_REPORT 50 -#define LIMINE_TERMINAL_CB_KBD_LEDS 60 -#define LIMINE_TERMINAL_CB_MODE 70 -#define LIMINE_TERMINAL_CB_LINUX 80 - -#define LIMINE_TERMINAL_CTX_SIZE ((uint64_t)(-1)) -#define LIMINE_TERMINAL_CTX_SAVE ((uint64_t)(-2)) -#define LIMINE_TERMINAL_CTX_RESTORE ((uint64_t)(-3)) -#define LIMINE_TERMINAL_FULL_REFRESH ((uint64_t)(-4)) - -/* Response revision 1 */ -#define LIMINE_TERMINAL_OOB_OUTPUT_GET ((uint64_t)(-10)) -#define LIMINE_TERMINAL_OOB_OUTPUT_SET ((uint64_t)(-11)) - -#define LIMINE_TERMINAL_OOB_OUTPUT_OCRNL (1 << 0) -#define LIMINE_TERMINAL_OOB_OUTPUT_OFDEL (1 << 1) -#define LIMINE_TERMINAL_OOB_OUTPUT_OFILL (1 << 2) -#define LIMINE_TERMINAL_OOB_OUTPUT_OLCUC (1 << 3) -#define LIMINE_TERMINAL_OOB_OUTPUT_ONLCR (1 << 4) -#define LIMINE_TERMINAL_OOB_OUTPUT_ONLRET (1 << 5) -#define LIMINE_TERMINAL_OOB_OUTPUT_ONOCR (1 << 6) -#define LIMINE_TERMINAL_OOB_OUTPUT_OPOST (1 << 7) - -LIMINE_DEPRECATED_IGNORE_START - -struct LIMINE_DEPRECATED limine_terminal; - -typedef void (*limine_terminal_write)(struct limine_terminal *, const char *, uint64_t); -typedef void (*limine_terminal_callback)(struct limine_terminal *, uint64_t, uint64_t, uint64_t, uint64_t); - -struct LIMINE_DEPRECATED limine_terminal { - uint64_t columns; - uint64_t rows; - LIMINE_PTR(struct limine_framebuffer *) framebuffer; -}; - -struct LIMINE_DEPRECATED limine_terminal_response { - uint64_t revision; - uint64_t terminal_count; - LIMINE_PTR(struct limine_terminal **) terminals; - LIMINE_PTR(limine_terminal_write) write; -}; - -struct LIMINE_DEPRECATED limine_terminal_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_terminal_response *) response; - LIMINE_PTR(limine_terminal_callback) callback; -}; - -LIMINE_DEPRECATED_IGNORE_END - -/* Paging mode */ - -#define LIMINE_PAGING_MODE_REQUEST { LIMINE_COMMON_MAGIC, 0x95c1a0edab0944cb, 0xa4e5cb3842f7488a } - -#if defined (__x86_64__) || defined (__i386__) -#define LIMINE_PAGING_MODE_X86_64_4LVL 0 -#define LIMINE_PAGING_MODE_X86_64_5LVL 1 -#define LIMINE_PAGING_MODE_MAX LIMINE_PAGING_MODE_X86_64_5LVL -#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_X86_64_4LVL -#elif defined (__aarch64__) -#define LIMINE_PAGING_MODE_AARCH64_4LVL 0 -#define LIMINE_PAGING_MODE_AARCH64_5LVL 1 -#define LIMINE_PAGING_MODE_MAX LIMINE_PAGING_MODE_AARCH64_5LVL -#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_AARCH64_4LVL -#elif defined (__riscv) && (__riscv_xlen == 64) -#define LIMINE_PAGING_MODE_RISCV_SV39 0 -#define LIMINE_PAGING_MODE_RISCV_SV48 1 -#define LIMINE_PAGING_MODE_RISCV_SV57 2 -#define LIMINE_PAGING_MODE_MAX LIMINE_PAGING_MODE_RISCV_SV57 -#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_RISCV_SV48 -#elif defined (__m68k__) -#define LIMINE_PAGING_MODE_M68K_4K 0 -#define LIMINE_PAGING_MODE_M68K_8K 1 -#define LIMINE_PAGING_MODE_MAX LIMINE_PAGING_MODE_M68K_8K -#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_M68K_4K -#else -#warning Unknown Architecture -#endif - -struct limine_paging_mode_response { - uint64_t revision; - uint64_t mode; - uint64_t flags; -}; - -struct limine_paging_mode_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_paging_mode_response *) response; - uint64_t mode; - uint64_t flags; -}; - -/* 5-level paging */ - -#define LIMINE_5_LEVEL_PAGING_REQUEST { LIMINE_COMMON_MAGIC, 0x94469551da9b3192, 0xebe5e86db7382888 } - -LIMINE_DEPRECATED_IGNORE_START - -struct LIMINE_DEPRECATED limine_5_level_paging_response { - uint64_t revision; -}; - -struct LIMINE_DEPRECATED limine_5_level_paging_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_5_level_paging_response *) response; -}; - -LIMINE_DEPRECATED_IGNORE_END - -/* SMP */ - -#define LIMINE_SMP_REQUEST { LIMINE_COMMON_MAGIC, 0x95a67b819a1b857e, 0xa0b61b723b6a73e0 } - -struct limine_smp_info; - -typedef void (*limine_goto_address)(struct limine_smp_info *); - -#if defined (__x86_64__) || defined (__i386__) - -#define LIMINE_SMP_X2APIC (1 << 0) - -struct limine_smp_info { - uint32_t processor_id; - uint32_t lapic_id; - uint64_t reserved; - LIMINE_PTR(limine_goto_address) goto_address; - uint64_t extra_argument; -}; - -struct limine_smp_response { - uint64_t revision; - uint32_t flags; - uint32_t bsp_lapic_id; - uint64_t cpu_count; - LIMINE_PTR(struct limine_smp_info **) cpus; -}; - -#elif defined (__aarch64__) - -struct limine_smp_info { - uint32_t processor_id; - uint32_t gic_iface_no; - uint64_t mpidr; - uint64_t reserved; - LIMINE_PTR(limine_goto_address) goto_address; - uint64_t extra_argument; -}; - -struct limine_smp_response { - uint64_t revision; - uint32_t flags; - uint64_t bsp_mpidr; - uint64_t cpu_count; - LIMINE_PTR(struct limine_smp_info **) cpus; -}; - -#elif defined (__riscv) && (__riscv_xlen == 64) - -struct limine_smp_info { - uint32_t processor_id; - uint64_t hartid; - uint64_t reserved; - LIMINE_PTR(limine_goto_address) goto_address; - uint64_t extra_argument; -}; - -struct limine_smp_response { - uint64_t revision; - uint32_t flags; - uint64_t bsp_hartid; - uint64_t cpu_count; - LIMINE_PTR(struct limine_smp_info **) cpus; -}; - -#elif defined (__m68k__) - -struct limine_smp_info { - uint32_t processor_id; - uint64_t id; - LIMINE_PTR(limine_goto_address) goto_address; -}; - -struct limine_smp_response { - uint64_t revision; - uint32_t flags; - uint64_t bsp_id; - uint64_t cpu_count; - LIMINE_PTR(struct limine_smp_info **) cpus; -}; -#else -#warning Unknown Architecture -#endif - -struct limine_smp_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_smp_response *) response; - uint64_t flags; -}; - -/* Memory map */ - -#define LIMINE_MEMMAP_REQUEST { LIMINE_COMMON_MAGIC, 0x67cf3d9d378a806f, 0xe304acdfc50c3c62 } - -#define LIMINE_MEMMAP_USABLE 0 -#define LIMINE_MEMMAP_RESERVED 1 -#define LIMINE_MEMMAP_ACPI_RECLAIMABLE 2 -#define LIMINE_MEMMAP_ACPI_NVS 3 -#define LIMINE_MEMMAP_BAD_MEMORY 4 -#define LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE 5 -#define LIMINE_MEMMAP_KERNEL_AND_MODULES 6 -#define LIMINE_MEMMAP_FRAMEBUFFER 7 - -struct limine_memmap_entry { - uint64_t base; - uint64_t length; - uint64_t type; -}; - -struct limine_memmap_response { - uint64_t revision; - uint64_t entry_count; - LIMINE_PTR(struct limine_memmap_entry **) entries; -}; - -struct limine_memmap_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_memmap_response *) response; -}; - -/* Entry point */ - -#define LIMINE_ENTRY_POINT_REQUEST { LIMINE_COMMON_MAGIC, 0x13d86c035a1cd3e1, 0x2b0caa89d8f3026a } - -typedef void (*limine_entry_point)(void); - -struct limine_entry_point_response { - uint64_t revision; -}; - -struct limine_entry_point_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_entry_point_response *) response; - LIMINE_PTR(limine_entry_point) entry; -}; - -/* Kernel File */ - -#define LIMINE_KERNEL_FILE_REQUEST { LIMINE_COMMON_MAGIC, 0xad97e90e83f1ed67, 0x31eb5d1c5ff23b69 } - -struct limine_kernel_file_response { - uint64_t revision; - LIMINE_PTR(struct limine_file *) kernel_file; -}; - -struct limine_kernel_file_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_kernel_file_response *) response; -}; - -/* Module */ - -#define LIMINE_MODULE_REQUEST { LIMINE_COMMON_MAGIC, 0x3e7e279702be32af, 0xca1c4f3bd1280cee } - -#define LIMINE_INTERNAL_MODULE_REQUIRED (1 << 0) - -struct limine_internal_module { - LIMINE_PTR(const char *) path; - LIMINE_PTR(const char *) cmdline; - uint64_t flags; -}; - -struct limine_module_response { - uint64_t revision; - uint64_t module_count; - LIMINE_PTR(struct limine_file **) modules; -}; - -struct limine_module_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_module_response *) response; - - /* Request revision 1 */ - uint64_t internal_module_count; - LIMINE_PTR(struct limine_internal_module **) internal_modules; -}; - -/* RSDP */ - -#define LIMINE_RSDP_REQUEST { LIMINE_COMMON_MAGIC, 0xc5e77b6b397e7b43, 0x27637845accdcf3c } - -struct limine_rsdp_response { - uint64_t revision; - LIMINE_PTR(void *) address; -}; - -struct limine_rsdp_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_rsdp_response *) response; -}; - -/* SMBIOS */ - -#define LIMINE_SMBIOS_REQUEST { LIMINE_COMMON_MAGIC, 0x9e9046f11e095391, 0xaa4a520fefbde5ee } - -struct limine_smbios_response { - uint64_t revision; - LIMINE_PTR(void *) entry_32; - LIMINE_PTR(void *) entry_64; -}; - -struct limine_smbios_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_smbios_response *) response; -}; - -/* EFI system table */ - -#define LIMINE_EFI_SYSTEM_TABLE_REQUEST { LIMINE_COMMON_MAGIC, 0x5ceba5163eaaf6d6, 0x0a6981610cf65fcc } - -struct limine_efi_system_table_response { - uint64_t revision; - LIMINE_PTR(void *) address; -}; - -struct limine_efi_system_table_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_efi_system_table_response *) response; -}; - -/* Boot time */ - -#define LIMINE_BOOT_TIME_REQUEST { LIMINE_COMMON_MAGIC, 0x502746e184c088aa, 0xfbc5ec83e6327893 } - -struct limine_boot_time_response { - uint64_t revision; - int64_t boot_time; -}; - -struct limine_boot_time_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_boot_time_response *) response; -}; - -/* Kernel address */ - -#define LIMINE_KERNEL_ADDRESS_REQUEST { LIMINE_COMMON_MAGIC, 0x71ba76863cc55f63, 0xb2644a48c516a487 } - -struct limine_kernel_address_response { - uint64_t revision; - uint64_t physical_base; - uint64_t virtual_base; -}; - -struct limine_kernel_address_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_kernel_address_response *) response; -}; - -/* Device Tree Blob */ - -#define LIMINE_DTB_REQUEST { LIMINE_COMMON_MAGIC, 0xb40ddb48fb54bac7, 0x545081493f81ffb7 } - -struct limine_dtb_response { - uint64_t revision; - LIMINE_PTR(void *) dtb_ptr; -}; - -struct limine_dtb_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_dtb_response *) response; -}; - -#ifdef __m68k__ - -/* Address of the boot info */ - -#define LIMINE_BOOT_INFO_REQUEST { LIMINE_COMMON_MAGIC, 0x53aa5a53e788b, 0x1dbf43fc4704ea } - -struct limine_boot_info_response -{ - uint64_t revision; - void* base; - uintptr_t uart_phys_base; -}; - -struct limine_boot_info_request { - uint64_t id[4]; - uint64_t revision; - LIMINE_PTR(struct limine_boot_info_response *) response; -}; - -#endif - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/loader/Linker.lds b/src/oboskrnl/arch/m68k/loader/Linker.lds deleted file mode 100644 index 45301be9..00000000 --- a/src/oboskrnl/arch/m68k/loader/Linker.lds +++ /dev/null @@ -1,51 +0,0 @@ -OUTPUT_FORMAT(elf32-m68k) - -ENTRY(LoaderEntry) - -PHDRS -{ - text PT_LOAD FLAGS((1 << 0) | (1 << 2)); - rodata PT_LOAD FLAGS((1 << 2)); - data PT_LOAD FLAGS((1 << 1) | (1 << 2)); -} - -SECTIONS -{ - . = 0x1000; - - LOADER_BLOB_BEGIN = .; - .text : - { - *(.text) - *(.text.*) - } :text - - . += CONSTANT(MAXPAGESIZE); - - .rodata : - { - *(.rodata) - *(.rodata.*) - } :rodata - - . += CONSTANT(MAXPAGESIZE); - - .data : - { - *(.data) - *(.data.*) - } :data - - .bss : - { - *(COMMON) - *(.bss) - } :data - - /DISCARD/ : - { - *(.note) - *(.note.*) - } - LOADER_BLOB_END = .; -} diff --git a/src/oboskrnl/arch/m68k/loader/List.h b/src/oboskrnl/arch/m68k/loader/List.h deleted file mode 100644 index 5b2e162f..00000000 --- a/src/oboskrnl/arch/m68k/loader/List.h +++ /dev/null @@ -1,153 +0,0 @@ -#pragma once - -#include -#include - -namespace sl -{ - namespace Intrusive - { - template - class FwdList - { - private: - T* head; - T* tail; - - public: - using Iterator = T*; - - constexpr FwdList() : head(nullptr), tail(nullptr) - {} - - FwdList(const FwdList&) = delete; - FwdList& operator=(const FwdList&) = delete; - FwdList(FwdList&&) = delete; - FwdList& operator=(FwdList&&) = delete; - - T& Front() - { return *head; } - - T& Back() - { return *tail; } - - Iterator Begin() - { return head; } - - Iterator End() - { return nullptr; } - - bool Empty() - { return head == nullptr; } - - void PushFront(T* value) - { - value->next = head; - head = value; - if (tail == nullptr) - tail = head; - } - - void PushBack(T* value) - { - value->next = nullptr; - if (Empty()) - head = value; - else - tail->next = value; - tail = value; - } - - T* PopFront() - { - if (head == nullptr) - return nullptr; - - T* temp = head; - head = head->next; - - if (Empty()) - tail = nullptr; - return temp; - } - - void InsertAfter(Iterator it, T* value) - { - if (it == End()) - return PushBack(value); - - value->next = it->next; - it->next = value; - - if (it == tail) - tail = value; - } - - T* EraseAfter(Iterator it) - { - if (it == End()) - return nullptr; - - T* temp = it->next; - if (temp != nullptr) - it->next = temp->next; - else - it->next = nullptr; - if (temp == tail) - tail = temp->next; - return temp; - } - - T* Remove(T* value) - { - if (value == nullptr || head == nullptr) - return nullptr; - - if (head == value) - { - PopFront(); - return head; - } - - T* scan = head; - while (scan->next != nullptr) - { - if (scan->next == value) - return EraseAfter(scan); - scan = scan->next; - } - - return nullptr; - } - - template - void Sort(Comparison comp) - { - for (auto i = head; i != nullptr; i = i->next) - { - for (auto j = i->next; j != nullptr; j = j->next) - { - if (comp(*i, *j)) - { - sl::Swap(*i, *j); - sl::Swap(i->next, j->next); - } - } - } - } - }; - - template - class List - { - private: - //TODO: impl, not copying other list - }; - } - - template - using IntrFwdList = Intrusive::FwdList; - - template - using IntrList = Intrusive::List; -} \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/loader/Loader.cpp b/src/oboskrnl/arch/m68k/loader/Loader.cpp deleted file mode 100644 index 3ef04439..00000000 --- a/src/oboskrnl/arch/m68k/loader/Loader.cpp +++ /dev/null @@ -1,290 +0,0 @@ -#include "Loader.h" -#include "Memory.h" -#include "Util.h" -#include -#include - -//bit of assembly in the global scope - you love to see it -asm("\ -.global KERNEL_BLOB_BEGIN \n\ -.global KERNEL_BLOB_END \n\ -.section .rodata \n\ -.balign 0x10 \n\ -KERNEL_BLOB_BEGIN: \n\ - .incbin \"" OUTPUT_DIRECTORY "/oboskrnl\" \n\ -KERNEL_BLOB_END: \n\ -.previous \n\ -"); - -extern "C" -{ - extern void LoaderExit(uintptr_t hhdmBase, uintptr_t jumpTarget); //defined in Entry.S -} - -namespace Npl -{ - uintptr_t kernelPhysBase; - uintptr_t kernelBase; - uintptr_t kernelTop; - uintptr_t kernelSlide; - - static bool IsKernelValid() - { - sl::CNativePtr blob(KERNEL_BLOB_BEGIN); - auto ehdr = blob.As(); - - if (sl::memcmp(&ehdr->e_ident[sl::EI_MAG0], sl::ExpectedMagic, 4) != 0) - return false; - if (ehdr->e_machine != sl::EM_68K) - return false; - if (ehdr->e_type != sl::ET_EXEC && ehdr->e_type != sl::ET_DYN) - return false; - if (ehdr->e_ident[sl::EI_CLASS] != sl::ELFCLASS32) - return false; - if (ehdr->e_ident[sl::EI_DATA] != sl::ELFDATA2MSB) - return false; - if (ehdr->e_phentsize != sizeof(sl::Elf_Phdr)) - return false; - - return true; - } - - static void SetKernelSlide() - { - kernelSlide = 0; //TODO: KASLR - NPL_LOG("Setting kernel slide: %u, effective load window 0x%x->0x%x\r\n", - kernelSlide, kernelBase + kernelSlide, kernelTop + kernelSlide); - } - - static bool DoRelocation(bool isRela, const sl::Elf_Rela* r, const sl::Elf_Sym* symTable) - { - const auto type = ELF_R_TYPE(r->r_info); - const uintptr_t b = kernelSlide; - const uintptr_t p = b + r->r_offset; - const uintptr_t s = symTable[ELF_R_SYM(r->r_info)].st_value; - - uintptr_t a = 0; - if (isRela) - a = r->r_addend; - else - { - const auto op = sl::ComputeRelocation(type, 0, b, s, p); - if (op.length == 0) - return false; - sl::memcopy(reinterpret_cast(p), &a, op.length); - } - - const auto op = sl::ComputeRelocation(type, a, b, s, p); - if (op.usedSymbol && s == 0) - return false; //tried to reference an external symbol - if (op.length == 0) - return false; //unknown relocation type. *sigh* time to crack open _that_ version of the m68k elf psABI. - - sl::memcopy(&op.value, reinterpret_cast(p), op.length); - return true; - } - - static bool DoKernelRelocations(sl::CNativePtr blob) - { - auto ehdr = blob.As(); - const sl::Elf_Dyn* dyn = [=]() -> const sl::Elf_Dyn* - { - auto phdrs = blob.As(ehdr->e_phoff); - for (size_t i = 0; i < ehdr->e_phnum; i++) - { - if (phdrs[i].p_type != sl::PT_DYNAMIC) - continue; - if (phdrs[i].p_filesz == 0) - return nullptr; - return blob.Offset(phdrs[i].p_offset).As(); - } - return nullptr; - }(); - - if (dyn == nullptr) - { - NPL_LOG("Kernel has no valid dynamic segment.\r\n"); - return true; - } - NPL_LOG("Kernel dynamic segment at %p\r\n", dyn); - - const sl::Elf_Sym* symTable; - const uint8_t* pltRelocs; - size_t pltRelocSize = 0; - bool pltUsesRela = false; - const sl::Elf_Rela* relas; - const sl::Elf_Rel* rels; - size_t relaCount = 0; - size_t relCount = 0; - - for (auto scan = dyn; scan->d_tag != sl::DT_NULL; scan++) - { - switch (scan->d_tag) - { - case sl::DT_NEEDED: - return false; - case sl::DT_SYMTAB: - symTable = reinterpret_cast(scan->d_ptr - kernelBase + blob.raw); - break; - case sl::DT_JMPREL: - pltRelocs = reinterpret_cast(scan->d_ptr - kernelBase + blob.raw); - break; - case sl::DT_PLTRELSZ: - pltRelocSize = scan->d_val; - break; - case sl::DT_PLTREL: - pltUsesRela = (scan->d_val == sl::DT_RELA); - break; - case sl::DT_RELA: - relas = reinterpret_cast(scan->d_ptr - kernelBase + blob.raw); - break; - case sl::DT_REL: - rels = reinterpret_cast(scan->d_ptr - kernelBase + blob.raw); - break; - case sl::DT_RELASZ: - relaCount = scan->d_val / sizeof(sl::Elf_Rela); - break; - case sl::DT_RELSZ: - relCount = scan->d_val / sizeof(sl::Elf_Rel); - break; - default: - continue; - } - } - - for (size_t i = 0; i < relCount; i++) - { - if (!DoRelocation(false, reinterpret_cast(&rels[i]), symTable)) - return false; - } - for (size_t i = 0; i < relaCount; i++) - { - if (!DoRelocation(true, &relas[i], symTable)) - return false; - } - - size_t pltCount = 0; - for (size_t off = 0; off < pltRelocSize;) - { - auto rela = reinterpret_cast(pltRelocs + off); - if (!DoRelocation(pltUsesRela, rela, symTable)) - return false; - - off += pltUsesRela ? sizeof(sl::Elf_Rela) : sizeof(sl::Elf_Rel); - pltCount++; - } - - NPL_LOG("Kernel relocations done: rel=%u, rela=%u, plt=%u\r\n", relCount, relaCount, pltCount); - return true; - } - - bool LoadKernel() - { - NPL_LOG("Loading kernel ...\r\n"); - if (!IsKernelValid()) - { - NPL_LOG("Aborting load: kernel image invalid.\r\n"); - return false; - } - - kernelBase = ~0; - kernelTop = 0; - sl::CNativePtr blob(KERNEL_BLOB_BEGIN); - auto ehdr = blob.As(); - - //gather some initial info about memory required for the kernel - const size_t phdrCount = ehdr->e_phnum; - auto phdrs = blob.Offset(ehdr->e_phoff).As(); - for (size_t i = 0; i < phdrCount; i++) - { - if (phdrs[i].p_type != sl::PT_LOAD) - continue; - if (!phdrs[i].p_vaddr) - continue; - - if (phdrs[i].p_vaddr < kernelBase) - kernelBase = phdrs[i].p_vaddr; - if (phdrs[i].p_vaddr + phdrs[i].p_memsz > kernelTop) - kernelTop = phdrs[i].p_vaddr + phdrs[i].p_memsz; - } - NPL_LOG("Kernel image requests load window: 0x%x->0x%x\r\n", kernelBase, kernelTop); - - SetKernelSlide(); - - kernelPhysBase = []() - { - const size_t allocSize = sl::AlignUp(kernelTop, PageSize) - sl::AlignDown(kernelBase, PageSize); - return AllocPages(allocSize / PageSize, MemoryType::KernelModules); - }(); - if (kernelPhysBase == 0) - Panic(PanicReason::LoadAllocFailure); - NPL_LOG("Kernel physical base: 0x%x\r\n", kernelPhysBase); - - sl::NativePtr slate = MapMemory(kernelTop - kernelBase, kernelBase + kernelSlide, kernelPhysBase); - for (size_t i = 0; i < phdrCount; i++) - { - const sl::Elf_Phdr phdr = phdrs[i]; - if (phdr.p_type != sl::PT_LOAD) - continue; - if (!phdrs[i].p_vaddr) - continue; - - void* dest = slate.Offset(phdr.p_vaddr - kernelBase).ptr; - sl::memcopy(blob.Offset(phdr.p_offset).ptr, dest, phdr.p_filesz); - const size_t zeroes = phdr.p_memsz - phdr.p_filesz; - if (zeroes != 0) - sl::memset(sl::NativePtr(dest).Offset(phdr.p_filesz).ptr, 0, zeroes); - - NPL_LOG("Segment %u loaded at %p, 0x%x bytes, 0x%x zeroes appended.\r\n", i, - dest, phdr.p_filesz, zeroes); - } - - return DoKernelRelocations(blob); - } - - void ExecuteKernel() - { - NPL_LOG("Loader finished, jumping to kernel. Bye!\r\n"); - - sl::CNativePtr blob(KERNEL_BLOB_BEGIN); - LoaderExit(HhdmBase, blob.As()->e_entry + kernelSlide); - } - - void GetKernelBases(uint64_t* phys, uint64_t* virt) - { - *phys = kernelPhysBase; - *virt = kernelBase + kernelSlide; - } - - LbpRequest* LbpNextRequest(LbpRequest* current) - { - constexpr uint64_t CommonMagic[] = { LIMINE_COMMON_MAGIC }; - - // sl::NativePtr scan = current; - // if (current == nullptr) - // scan = kernelBase + kernelSlide; - // scan = sl::AlignUp(scan.raw+1, 8); - - // for (; scan.raw < kernelTop; scan = scan.raw + 8) - // { - // if (*scan.As() != CommonMagic[0]) - // continue; - - // LbpRequest* req = scan.As(); - // if (req->id[1] != CommonMagic[1]) - // continue; - - // return req; - // } - uintptr_t* iter = (uintptr_t*)current; - if (!iter) - iter = (uintptr_t*)(kernelBase + kernelSlide); - else - iter++; - for (; (uintptr_t)iter < kernelTop; iter++) - if (sl::memcmp(iter, CommonMagic, sizeof(uint64_t)*2) == 0) - return (LbpRequest*)iter; - - return nullptr; - } -} diff --git a/src/oboskrnl/arch/m68k/loader/Loader.h b/src/oboskrnl/arch/m68k/loader/Loader.h deleted file mode 100644 index 2c57b45d..00000000 --- a/src/oboskrnl/arch/m68k/loader/Loader.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include -#include - -namespace Npl -{ - bool LoadKernel(); - void ExecuteKernel(); - void GetKernelBases(uint64_t* phys, uint64_t* virt); - - struct LbpRequest - { - uint64_t id[4]; - uint64_t revision; - union - { - uint64_t pad; - void* response; - }; - }; - - LbpRequest* LbpNextRequest(LbpRequest* current = nullptr); -} diff --git a/src/oboskrnl/arch/m68k/loader/Main.cpp b/src/oboskrnl/arch/m68k/loader/Main.cpp deleted file mode 100644 index b22d84ec..00000000 --- a/src/oboskrnl/arch/m68k/loader/Main.cpp +++ /dev/null @@ -1,284 +0,0 @@ -#include "Memory.h" -#include "Util.h" -#include "Loader.h" -#include -#include -#include - -#ifndef NPL_KERNEL_CMDLINE - #define NPL_KERNEL_CMDLINE "" -#endif - -#ifndef NPL_INITRD_CMDLINE - #define NPL_INITRD_CMDLINE "" -#endif - -namespace Npl -{ - const char* EmptyStr = "" + HhdmBase; - - struct LbpRequestHandler - { - uint64_t id[4]; - void (*Handle)(LbpRequest* req); - }; - - constexpr LbpRequestHandler RequestHandlers[] = - { - { - .id = LIMINE_BOOTLOADER_INFO_REQUEST, - .Handle = [](LbpRequest* req) - { - NPL_LOG("Populating bootloader info response.\r\n"); - auto resp = new limine_bootloader_info_response(); - resp->revision = 0; - resp->name = "northport m68k loader"; - resp->version = "1"; - - resp->name += HhdmBase; - resp->version += HhdmBase; - - req->response = resp; - } - }, - { - .id = LIMINE_HHDM_REQUEST, - .Handle = [](LbpRequest* req) - { - NPL_LOG("Populating HHDM response.\r\n"); - auto resp = new limine_hhdm_response(); - resp->revision = 0; - resp->offset = HhdmBase; - - req->response = resp; - } - }, - { - .id = LIMINE_SMP_REQUEST, - .Handle = [](LbpRequest* req) - { - NPL_LOG("Populating SMP response.\r\n"); - auto resp = new limine_smp_response(); - resp->revision = 0; - resp->flags = 0; - resp->bsp_id = 0; - resp->cpu_count = 1; - - auto info = new limine_smp_info(); - info->id = 0; - info->processor_id = 0; - - auto infoBlock = new limine_smp_info*[1]; //lol - infoBlock[0] = info; - resp->cpus = infoBlock; - - req->response = resp; - } - }, - { - .id = LIMINE_KERNEL_FILE_REQUEST, - .Handle = [](LbpRequest* req) - { - NPL_LOG("Populating kernel file response.\r\n"); - auto fileDesc = new limine_file(); - fileDesc->revision = 0; - fileDesc->address = reinterpret_cast((uintptr_t)KERNEL_BLOB_BEGIN + HhdmBase); - fileDesc->size = reinterpret_cast(KERNEL_BLOB_END); - fileDesc->size -= reinterpret_cast(KERNEL_BLOB_BEGIN); - fileDesc->path = EmptyStr; - - const char bakedCmdline[] = NPL_KERNEL_CMDLINE; - const char* liveCmdline = nullptr; - size_t liveLen = 0; - if (auto found = FindBootInfoTag(BootInfoType::CommandLine); found.ptr != nullptr) - { - liveCmdline = found.Offset(sizeof(BootInfoTag)).As(); - liveLen = found.As()->size; - } - - char* cmdline = static_cast(AllocGeneral(sizeof(bakedCmdline) + liveLen)); - sl::memcopy(bakedCmdline, cmdline, sizeof(bakedCmdline)); - sl::memcopy(liveCmdline, cmdline + (sizeof(bakedCmdline) - 1), liveLen); - fileDesc->cmdline = cmdline; - - auto resp = new limine_kernel_file_response(); - resp->revision = 0; - resp->kernel_file = fileDesc; - - req->response = resp; - } - }, - { - .id = LIMINE_MODULE_REQUEST, - .Handle = [](LbpRequest* req) - { - const auto maybeInitRd = FindBootInfoTag(BootInfoType::InitRd); - if (maybeInitRd.ptr == nullptr) - { - NPL_LOG("No initrd passed to loader, ignoring modules request.\r\n"); - return; - } - - NPL_LOG("Populating modules response (with initrd).\r\n"); - const auto initrd = maybeInitRd.Offset(sizeof(BootInfoTag)).As(); - auto file = new limine_file(); - file->revision = 0; - file->address = reinterpret_cast(initrd->addr + HhdmBase); - file->size = initrd->size; - file->path = EmptyStr; - - const char initrdCmdline[] = NPL_INITRD_CMDLINE; - char* cmdline = new char[sizeof(initrdCmdline)]; - sl::memcopy(initrdCmdline, cmdline, sizeof(initrdCmdline)); - file->cmdline = cmdline; - - auto infoBlock = new limine_file*[1]; - infoBlock[0] = file; - - auto resp = new limine_module_response(); - resp->revision = 0; - resp->module_count = 1; - resp->modules = infoBlock; - - req->response = resp; - } - }, - { - .id = LIMINE_KERNEL_ADDRESS_REQUEST, - .Handle = [](LbpRequest* req) - { - NPL_LOG("Populating kernel address response.\r\n"); - auto resp = new limine_kernel_address_response(); - resp->revision = 0; - GetKernelBases(&resp->physical_base, &resp->virtual_base); - - req->response = resp; - } - }, - { - .id = LIMINE_BOOT_INFO_REQUEST, - .Handle = [](LbpRequest* req) - { - NPL_LOG("Populating boot info response.\r\n"); - auto resp = new limine_boot_info_response{}; - resp->revision = 0; - resp->base = (void*)(HhdmBase + sl::AlignUp((uintptr_t)LOADER_BLOB_END, 2)); - auto maybeUart = FindBootInfoTag(BootInfoType::GoldfishTtyBase); - if (maybeUart.ptr) - resp->uart_phys_base = (uintptr_t)maybeUart.Offset(sizeof(BootInfoTag)).Read(); // provide for convinience - - req->response = resp; - } - } - }; -} - -extern "C" -{ - uintptr_t __stack_chk_guard = static_cast(0x57656C2C6675636B); - - void __stack_chk_fail() - { Npl::Panic(Npl::PanicReason::StackCheckFail); } - - void LoaderEntryNext() - { - using namespace Npl; - -#ifdef NPL_ENABLE_LOGGING - auto maybeUart = FindBootInfoTag(BootInfoType::GoldfishTtyBase); - if (maybeUart.ptr != nullptr) - { - uart = maybeUart.Offset(sizeof(BootInfoTag)).Read(); - uart.Offset(8).Write(0); //disable interrupts - NPL_LOG("\r\nNorthport m68k loader starting ...\r\n"); - NPL_LOG("UART enabled, mapped at %p\r\n", uart.ptr); - } -#endif - InitMemoryManager(); - - const size_t hhdmLimit = HhdmLimit(); - for (size_t i = PageSize; i < hhdmLimit; i += PageSize) - { - if (!MapMemory(PageSize, i, i)) - Panic(PanicReason::HhdmSetupFail); - if (!MapMemory(PageSize, i + HhdmBase, i)) - Panic(PanicReason::HhdmSetupFail); - } - - EnableMmu(); - -#ifdef NPL_ENABLE_LOGGING - uart = MapMemory(PageSize, hhdmLimit, maybeUart.Offset(sizeof(BootInfoTag)).Read()); - NPL_LOG("MMU enabled, UART re-mapped at %p\r\n", uart.ptr); -#endif - - LoadKernel(); - - //memory map request gets special treatment: we want to handle it last since we - //may modify the memory map while handling other responses. - const uint64_t mmapId[4] = LIMINE_MEMMAP_REQUEST; - LbpRequest* mmapRequest = nullptr; - - NPL_LOG("Handling LBP requests ...\r\n"); - for (LbpRequest* request = LbpNextRequest(); request != nullptr; request = LbpNextRequest(request)) - { - if (sl::memcmp(mmapId, request->id, 32) == 0) - { - mmapRequest = request; - continue; - } - - for (size_t i = 0; i < sizeof(RequestHandlers) / sizeof(LbpRequestHandler); i++) - { - if (sl::memcmp(RequestHandlers[i].id, request->id, 32) != 0) - continue; - - NPL_LOG("Found request at %p\r\n", request); - RequestHandlers[i].Handle(request); - break; - } - } - - if (mmapRequest != nullptr) - { - NPL_LOG("Handling memory map request ...\r\n"); - constexpr size_t MaxMemmapAllocTries = 3; - - limine_memmap_response* resp = new limine_memmap_response(); - limine_memmap_entry* entries = nullptr; - limine_memmap_entry** entryPtrs = nullptr; - size_t entryCount = 0; - - for (size_t i = 0; i < MaxMemmapAllocTries; i++) - { - const size_t tentativeCount = GenerateLbpMemoryMap(nullptr, 0); - const size_t storeCount = tentativeCount + tentativeCount / 2; - entries = new limine_memmap_entry[storeCount]; - entryPtrs = new limine_memmap_entry*[storeCount]; - - entryCount = GenerateLbpMemoryMap(entries, storeCount); - NPL_LOG("Memory map finalize attempt: store=%u, actual=%u\r\n", storeCount, entryCount); - if (entryCount <= storeCount) - break; - - NPL_LOG("Finalize attempt failed, retrying\r\n"); - delete[] entries; - delete[] entryPtrs; - entries = nullptr; - entryPtrs = nullptr; - } - NPL_LOG("Finalized memory map, populating protocol response.\r\n"); - - for (size_t i = 0; i < entryCount; i++) - entryPtrs[i] = &entries[i]; - resp->revision = 0; - resp->entry_count = entryCount; - resp->entries = entryPtrs; - - mmapRequest->response = resp; - } - - ExecuteKernel(); - Panic(PanicReason::KernelReturned); - } -} diff --git a/src/oboskrnl/arch/m68k/loader/Maths.h b/src/oboskrnl/arch/m68k/loader/Maths.h deleted file mode 100644 index 86700ea9..00000000 --- a/src/oboskrnl/arch/m68k/loader/Maths.h +++ /dev/null @@ -1,217 +0,0 @@ -#pragma once - -#include -#include - -constexpr inline size_t KiB = 1024; -constexpr inline size_t MiB = KiB * KiB; -constexpr inline size_t GiB = MiB * KiB; -constexpr inline size_t TiB = GiB * KiB; -constexpr inline size_t PiB = TiB * KiB; - -namespace sl -{ - template - constexpr inline T AlignDown(T addr, size_t alignment) - { - return (addr / alignment) * alignment; - } - - template - constexpr inline T AlignUp(T addr, size_t alignment) - { - return ((addr + alignment - 1) / alignment) * alignment; - } - - template - constexpr inline T* AlignDown(T* addr, size_t alignment) - { - return reinterpret_cast(((uintptr_t)addr / alignment) * alignment); - } - - template - constexpr inline T* AlignUp(T* addr, size_t alignment) - { - return reinterpret_cast(((uintptr_t)(addr + alignment - 1) / alignment) * alignment); - } - - template - constexpr inline T Min(T a, T b) - { - return ((a < b) ? a : b); - } - - template - constexpr inline T Max(T a, T b) - { - return ((a > b) ? a : b); - } - - template - constexpr inline T Clamp(T value, T low, T high) - { - if (value < low) - return low; - if (value > high) - return high; - return value; - } - - constexpr inline uint8_t ByteSwap(uint8_t value) - { return value; } - - constexpr inline uint16_t ByteSwap(uint16_t value) - { - return (value << 8) | (value >> 8); - } - - constexpr inline uint32_t ByteSwap(uint32_t value) - { - uint32_t result = 0; - result |= (value & 0x0000'00FF) << 24; - result |= (value & 0x0000'FF00) << 8; - result |= (value & 0x00FF'0000) >> 8; - result |= (value & 0xFF00'0000) >> 24; - return result; - } - - constexpr inline uint64_t ByteSwap(uint64_t value) - { - uint64_t result = 0; - result |= (value & 0x0000'0000'0000'00FF) << 56; - result |= (value & 0x0000'0000'0000'FF00) << 40; - result |= (value & 0x0000'0000'00FF'0000) << 24; - result |= (value & 0x0000'0000'FF00'0000) << 8; - result |= (value & 0x0000'00FF'0000'0000) >> 8; - result |= (value & 0x0000'FF00'0000'0000) >> 24; - result |= (value & 0x00FF'0000'0000'0000) >> 40; - result |= (value & 0xFF00'0000'0000'0000) >> 56; - return result; - } - - constexpr inline uint8_t LeToHost(uint8_t value) - { - return value; - } - - constexpr inline uint16_t LeToHost(uint16_t value) - { -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - return value; -#else - return ByteSwap(value); -#endif - } - - constexpr inline uint32_t LeToHost(uint32_t value) - { -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - return value; -#else - return ByteSwap(value); -#endif - } - - constexpr inline uint64_t LeToHost(uint64_t value) - { -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - return value; -#else - return ByteSwap(value); -#endif - } - - constexpr inline uint8_t BeToHost(uint8_t value) - { - return value; - } - - constexpr inline uint16_t BeToHost(uint16_t value) - { -#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - return value; -#else - return ByteSwap(value); -#endif - } - - constexpr inline uint32_t BeToHost(uint32_t value) - { -#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - return value; -#else - return ByteSwap(value); -#endif - } - - constexpr inline uint64_t BeToHost(uint64_t value) - { -#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - return value; -#else - return ByteSwap(value); -#endif - } - - template - constexpr inline T SquareRoot(T value) - { - if (value == 0) - return 0; - T left = 1; - T right = value / 2 + 1; - T res = 0; - - while (left <= right) - { - T mid = left + ((right - left) / 2); - if (mid <= value / mid) - { - left = mid + 1; - res = mid; - } - else - right = mid - 1; - } - - return res; - } - - template //mm yes, very safe. - constexpr inline T StandardDeviation(T* data, size_t dataCount) - { - T mean = 0; - for (size_t i = 0; i < dataCount; i++) - mean += data[i]; - mean = mean / dataCount; - - T variance = 0; - for (size_t i = 0; i < dataCount; i++) - { - variance += ((data[i] - mean) * (data[i] - mean) / (dataCount - 1)); - } - - return SquareRoot(variance); - } - - template - constexpr inline bool IsPowerOfTwo(T test) - { - return test && !(test & (test - 1)); - } - - constexpr inline size_t PopCount(size_t test) - { - /* This function was taken from https://github.com/mintsuki/cc-runtime, which is a rip - * of the LLVM compiler runtime library (different flavour of libgcc). - * See https://llvm.org/LICENSE.txt for the full license and more info. - */ - uint64_t x2 = (uint64_t)test; - x2 = x2 - ((x2 >> 1) & 0x5555555555555555uLL); - x2 = ((x2 >> 2) & 0x3333333333333333uLL) + (x2 & 0x3333333333333333uLL); - x2 = (x2 + (x2 >> 4)) & 0x0F0F0F0F0F0F0F0FuLL; - uint32_t x = (uint32_t)(x2 + (x2 >> 32)); - x = x + (x >> 16); - return (x + (x >> 8)) & 0x0000007F; - } -} \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/loader/Memory.cpp b/src/oboskrnl/arch/m68k/loader/Memory.cpp deleted file mode 100644 index c9091e0b..00000000 --- a/src/oboskrnl/arch/m68k/loader/Memory.cpp +++ /dev/null @@ -1,351 +0,0 @@ -#include "Memory.h" -#include "Util.h" -#include -#include -#include -#include -#include - -namespace Npl -{ - struct MemoryBlock - { - MemoryBlock* next; - uintptr_t base; - size_t length; - MemoryType type; - }; - - constexpr uint32_t PteResident = 3 << 0; - constexpr uint32_t PteWrite = 1 << 2; - constexpr uint32_t PteCopyback = 1 << 5; - constexpr uint32_t PteSupervisor = 1 << 7; - - struct PageTable - { - uint32_t entries[128]; - }; - - constexpr size_t FreeHeaderCount = 256; - MemoryBlock freeHeaders[FreeHeaderCount]; - size_t freeHeadersUsed; - - sl::IntrFwdList physList {}; - uintptr_t ptRoot; - - struct MemoryBlockComparer - { - bool operator()(const MemoryBlock& a, const MemoryBlock& b) - { return a.base > b.base; } - }; - - static MemoryBlock* AllocMemoryBlock() - { - MemoryBlock* block = &freeHeaders[freeHeadersUsed++]; - if (freeHeadersUsed == FreeHeaderCount) - Panic(PanicReason::InternalAllocFailure); - return block; - } - - static void ReservePhysicalRange(uintptr_t base, size_t top, MemoryType type) - { - base = sl::AlignDown(base, PageSize); - top = sl::AlignUp(top, PageSize); - NPL_LOG("Physical range reserved: 0x%x->0x%x (0x%x bytes).\r\n", base, top, top - base); - - for (MemoryBlock* scan = physList.Begin(); scan != nullptr; scan = scan->next) - { - if (scan->type != MemoryType::Usable) - continue; //we only care about reserving usable memory - - const uintptr_t scanTop = scan->base + scan->length; - if (scan->length == 0 || top < scan->base || base >= scanTop) - continue; //no overlap, ignore this block - - if (base <= scan->base) //reserved overlaps beginning of scan - { - if (top >= scanTop) - scan->length = 0; - else - { - scan->base = top; - scan->length = scanTop - scan->base; - } - } - else if (top > scanTop) //reserved overlaps end of scan - scan->length = base - scan->base; - else //reserved is in the middle of scan - { - scan->length = base - scan->base; - - MemoryBlock* latest = AllocMemoryBlock(); - latest->type = MemoryType::Usable; - latest->base = top; - latest->length = scanTop - top; - physList.PushBack(latest); - break; - } - } - - MemoryBlock* reserved = AllocMemoryBlock(); - reserved->type = type; - reserved->base = base; - reserved->length = top - base; - physList.PushBack(reserved); - - physList.Sort(MemoryBlockComparer{}); - } - - static void MapPage(uintptr_t vaddr, uintptr_t paddr, bool write) - { - const size_t pml3Idx = (vaddr >> 25) & 0x7F; - const size_t pml2Idx = (vaddr >> 18) & 0x7F; - const size_t pml1Idx = (vaddr >> 12) & 0x3F; - - PageTable* pml3 = reinterpret_cast(ptRoot); - PageTable* pml2 = nullptr; - if ((pml3->entries[pml3Idx] & PteResident) == 0) - { - uintptr_t page = AllocPages(1); - if (page == 0) - Panic(PanicReason::InternalAllocFailure); - sl::memset(sl::NativePtr(page).ptr, 0, sizeof(PageTable)); - - page |= PteResident; - pml3->entries[pml3Idx] = page; - } - pml2 = reinterpret_cast(pml3->entries[pml3Idx] & ~0x1FF); - - PageTable* pml1 = nullptr; - if ((pml2->entries[pml2Idx] & PteResident) == 0) - { - uintptr_t page = AllocPages(1); - if (page == 0) - Panic(PanicReason::InternalAllocFailure); - sl::memset(sl::NativePtr(page).ptr, 0, sizeof(PageTable)); - - page |= PteResident; - pml2->entries[pml2Idx] = page; - } - pml1 = reinterpret_cast(pml2->entries[pml2Idx] & ~0xFF); - - uint32_t* pte = &pml1->entries[pml1Idx]; - *pte = paddr | PteResident | PteSupervisor | PteCopyback; - if (!write) - *pte |= PteWrite; - } - - void InitMemoryManager() - { - freeHeadersUsed = 0; - - //create a map of physical memory - sl::CNativePtr chunkScan = FindBootInfoTag(BootInfoType::MemChunk); - while (chunkScan.ptr != nullptr) - { - auto chunk = chunkScan.Offset(sizeof(BootInfoTag)).As(); - uintptr_t base = chunk->addr; - size_t length = chunk->size; - - if (base == 0) - { - base += PageSize; - length -= sl::Min(length, PageSize); - } - - if (length != 0) - { - MemoryBlock* header = AllocMemoryBlock(); - header->type = MemoryType::Usable; - header->base = base; - header->length = length; - physList.PushBack(header); - NPL_LOG("Usable memory chunk: 0x%x->0x%x (0x%x bytes).\r\n", base, length, length - base); - } - - chunkScan = chunkScan.Offset(chunkScan.As()->size); - chunkScan = FindBootInfoTag(BootInfoType::MemChunk, chunkScan); - } - - const uintptr_t loaderBase = (uintptr_t)LOADER_BLOB_BEGIN; - const uintptr_t loaderTop = ((uintptr_t)LOADER_BLOB_END) + PageSize * 2 /* be mindful of the boot tags, save a few pages after this */; - ReservePhysicalRange(loaderBase, loaderTop, MemoryType::Reclaimable); - - if (auto initrdPtr = FindBootInfoTag(BootInfoType::InitRd); initrdPtr.ptr != nullptr) - { - auto desc = initrdPtr.Offset(sizeof(BootInfoTag)).As(); - ReservePhysicalRange(desc->addr, desc->addr + desc->size, MemoryType::KernelModules); - } - - ptRoot = AllocPages(1); - if (ptRoot == 0) - Panic(PanicReason::InternalAllocFailure); - sl::memset(reinterpret_cast(ptRoot), 0, sizeof(PageTable)); - } - - void EnableMmu() - { - asm("movec %0, %%srp" :: "d"(ptRoot)); - asm("movec %0, %%dtt0" :: "d"(0)); //we dont use the transparent translation regs - asm("movec %0, %%dtt1" :: "d"(0)); //so clear them, make sure they dont interfere. - asm("movec %0, %%itt0" :: "d"(0)); - asm("movec %0, %%itt1" :: "d"(0)); - - asm("movec %0, %%tcr" :: "d"(1 << 15)); //set bit 15 to enable translation - } - - size_t HhdmLimit() - { - return physList.Back().base + physList.Back().length; - } - - size_t GenerateLbpMemoryMap(void* store, size_t count) - { - size_t accum = 0; - for (auto it = physList.Begin(); it != physList.End(); it = it->next) - accum++; - - if (store == nullptr || accum > count) - return accum; - - auto entries = static_cast(store); - for (auto it = physList.Begin(); it != physList.End(); it = it->next) - { - limine_memmap_entry* entry = entries++; - entry->base = it->base; - entry->length = it->length; - if ((uintptr_t)LOADER_BLOB_END >= entry->base && (uintptr_t)LOADER_BLOB_END < entry->base+entry->length && it->type == MemoryType::Usable) - Panic(PanicReason::InternalAllocFailure); - - switch (it->type) - { - case MemoryType::Usable: - entry->type = LIMINE_MEMMAP_USABLE; - break; - case MemoryType::Reclaimable: - entry->type = LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE; - break; - case MemoryType::KernelModules: - entry->type = LIMINE_MEMMAP_KERNEL_AND_MODULES; - break; - } - } - - return accum; - } - - uintptr_t AllocPages(size_t count, MemoryType type) - { - const size_t length = count * PageSize; - - MemoryBlock* firstUsable = nullptr; - for (MemoryBlock* scan = physList.Begin(); scan != nullptr; scan = scan->next) - { - if (firstUsable == nullptr && scan->type == MemoryType::Usable) - firstUsable = scan; - if (type != MemoryType::Reclaimable) - continue; - - if (scan->type != MemoryType::Reclaimable || scan->next == nullptr - || scan->next->type != MemoryType::Usable || scan->length < length) - continue; - - //we found a pair of regions ([reclaimable][usable]), move memory from usable to reclaimable - const uintptr_t found = scan->base + scan->length; - scan->length += length; - scan->next->length -= length; - scan->next->base += length; - return found; - } - - if (firstUsable == nullptr || firstUsable->length < length) - return 0; //abort, no memory for us - - //no nicely arranged regions found, make one - const uintptr_t found = firstUsable->base; - ReservePhysicalRange(firstUsable->base, firstUsable->base + length, type); - return found; - } - - void* AllocGeneral(size_t size) - { - const size_t pages = sl::AlignUp(size, PageSize) / PageSize; - const uintptr_t found = AllocPages(pages); //TODO: we can do better than this surely - - if (found == 0) - return nullptr; - return reinterpret_cast(found + HhdmBase); - } - - void* MapMemory(size_t length, uintptr_t vaddr, uintptr_t paddr) - { - const uintptr_t top = sl::AlignUp(vaddr + length, PageSize); - const size_t addend = vaddr % PageSize; - vaddr = sl::AlignDown(vaddr, PageSize); - length = top - vaddr; - - for (size_t i = 0; i < length; i += PageSize) - { - const uintptr_t mappedPaddr = (paddr == 0 ? AllocPages(1) : paddr + i); - if (mappedPaddr == 0) - return nullptr; - - MapPage(vaddr + i, mappedPaddr, true); - } - - return reinterpret_cast(vaddr + addend); - } - - uintptr_t GetMap(uintptr_t vaddr) - { - uint32_t mmusr = 0; - asm("ptestw (%1); movec %%mmusr, %0" : "=d"(mmusr) : "a"(vaddr)); - - if (mmusr & PteResident) - return (mmusr & ~0xFFF) | (vaddr & 0xFFF); - return 0; - } -} - -void* operator new(size_t size) -{ - return Npl::AllocGeneral(size); -} - -void* operator new[](size_t size) -{ - return Npl::AllocGeneral(size); -} - -void operator delete(void*) noexcept -{ - Panic(Npl::PanicReason::DeleteCalled); -} - -void operator delete(void*, size_t) noexcept -{ - Panic(Npl::PanicReason::DeleteCalled); -} - -void operator delete[](void*) noexcept -{ - Panic(Npl::PanicReason::DeleteCalled); -} - -void operator delete[](void*, size_t) noexcept -{ - Panic(Npl::PanicReason::DeleteCalled); -} - -extern "C" -{ - void* malloc(size_t size) - { - return Npl::AllocGeneral(size); - } - - void free(void*, size_t) - { - Npl::Panic(Npl::PanicReason::DeleteCalled); - } -} diff --git a/src/oboskrnl/arch/m68k/loader/Memory.h b/src/oboskrnl/arch/m68k/loader/Memory.h deleted file mode 100644 index db33f0fa..00000000 --- a/src/oboskrnl/arch/m68k/loader/Memory.h +++ /dev/null @@ -1,64 +0,0 @@ -#pragma once - -#include -#include - -namespace Npl -{ - constexpr uintptr_t DontCare = 0; - constexpr uintptr_t HhdmBase = 0x8000'0000; - - enum class MemoryType - { - Usable, - Reclaimable, - KernelModules, - }; - - void InitMemoryManager(); - void EnableMmu(); - size_t HhdmLimit(); - size_t GenerateLbpMemoryMap(void* store, size_t count); - - uintptr_t AllocPages(size_t count, MemoryType type = MemoryType::Reclaimable); - void* AllocGeneral(size_t size); - void* MapMemory(size_t length, uintptr_t vaddr, uintptr_t paddr = DontCare); - uintptr_t GetMap(uintptr_t vaddr); -} - -// NOTE(oberrow): -// Taken from https://github.com/DeanoBurrito/northport/blob/730f3c531f6190c238801f1abe180c1e0c8c61de/libs/np-syslib/include/Memory.h -namespace sl -{ - template - void memsetT(void* const start, T value, size_t valueCount) - { - T* const si = reinterpret_cast(start); - - for (size_t i = 0; i < valueCount; i++) - si[i] = value; - } - - void* memset(void* const start, uint8_t val, size_t count); - - void* memcopy(const void* const source, void* const destination, size_t count); - void* memcopy(const void* const source, size_t sourceOffset, void* const destination, size_t destOffset, size_t count); - - int memcmp(const void* const a, const void* const b, size_t count); - int memcmp(const void* const a, size_t offsetA, const void* const b, size_t offsetB, size_t count); - - size_t memfirst(const void* const buff, uint8_t target, size_t upperLimit); - size_t memfirst(const void* const buff, size_t offset, uint8_t target, size_t upperLimit); -} - -//These MUST be provided by the program, we'll forward declare them here to make them available. -extern "C" -{ - void* malloc(size_t length); - void free(void* ptr, size_t length); - - //clang requires these to exist for __builtin_xyz, while GCC provides its own. - void* memcpy(void* dest, const void* src, size_t len); - void* memset(void* dest, int value, size_t len); - void* memmove(void* dest, const void* src, size_t len); -} \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/loader/NanoPrintf.h b/src/oboskrnl/arch/m68k/loader/NanoPrintf.h deleted file mode 100644 index e8326d30..00000000 --- a/src/oboskrnl/arch/m68k/loader/NanoPrintf.h +++ /dev/null @@ -1,1138 +0,0 @@ -/* nanoprintf: a tiny embeddable printf replacement written in C. - https://github.com/charlesnicholson/nanoprintf - charles.nicholson+nanoprintf@gmail.com - dual-licensed under 0bsd and unlicense, take your pick. see eof for details. */ - -#ifndef NANOPRINTF_H_INCLUDED -#define NANOPRINTF_H_INCLUDED - -#include -#include - -// Define this to fully sandbox nanoprintf inside of a translation unit. -#ifdef NANOPRINTF_VISIBILITY_STATIC - #define NPF_VISIBILITY static -#else - #define NPF_VISIBILITY extern -#endif - -#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) - #define NPF_PRINTF_ATTR(FORMAT_INDEX, VARGS_INDEX) \ - __attribute__((format(printf, FORMAT_INDEX, VARGS_INDEX))) -#else - #define NPF_PRINTF_ATTR(FORMAT_INDEX, VARGS_INDEX) -#endif - -// Public API - -#ifdef __cplusplus -extern "C" { -#endif - -// The npf_ functions all return the number of bytes required to express the -// fully-formatted string, not including the null terminator character. -// The npf_ functions do not return negative values, since the lack of 'l' length -// modifier support makes encoding errors impossible. - -NPF_VISIBILITY int npf_snprintf( - char *buffer, size_t bufsz, const char *format, ...) NPF_PRINTF_ATTR(3, 4); - -NPF_VISIBILITY int npf_vsnprintf( - char *buffer, size_t bufsz, char const *format, va_list vlist) NPF_PRINTF_ATTR(3, 0); - -typedef void (*npf_putc)(int c, void *ctx); -NPF_VISIBILITY int npf_pprintf( - npf_putc pc, void *pc_ctx, char const *format, ...) NPF_PRINTF_ATTR(3, 4); - -NPF_VISIBILITY int npf_vpprintf( - npf_putc pc, void *pc_ctx, char const *format, va_list vlist) NPF_PRINTF_ATTR(3, 0); - -#ifdef __cplusplus -} -#endif - -#endif // NANOPRINTF_H_INCLUDED - -/* The implementation of nanoprintf begins here, to be compiled only if - NANOPRINTF_IMPLEMENTATION is defined. In a multi-file library what follows would - be nanoprintf.c. */ - -#ifdef NANOPRINTF_IMPLEMENTATION - -#ifndef NANOPRINTF_IMPLEMENTATION_INCLUDED -#define NANOPRINTF_IMPLEMENTATION_INCLUDED - -#include -#include - -// The conversion buffer must fit at least UINT64_MAX in octal format with the leading '0'. -#ifndef NANOPRINTF_CONVERSION_BUFFER_SIZE - #define NANOPRINTF_CONVERSION_BUFFER_SIZE 23 -#endif -#if NANOPRINTF_CONVERSION_BUFFER_SIZE < 23 - #error The size of the conversion buffer must be at least 23 bytes. -#endif - -// Pick reasonable defaults if nothing's been configured. -#if !defined(NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS) && \ - !defined(NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS) && \ - !defined(NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS) && \ - !defined(NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS) && \ - !defined(NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS) && \ - !defined(NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS) - #define NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS 1 - #define NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS 1 - #define NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS 1 - #define NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS 0 - #define NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS 0 - #define NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS 0 -#endif - -// If anything's been configured, everything must be configured. -#ifndef NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS - #error NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS must be #defined to 0 or 1 -#endif -#ifndef NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS - #error NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS must be #defined to 0 or 1 -#endif -#ifndef NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS - #error NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS must be #defined to 0 or 1 -#endif -#ifndef NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS - #error NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS must be #defined to 0 or 1 -#endif -#ifndef NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS - #error NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS must be #defined to 0 or 1 -#endif -#ifndef NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS - #error NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS must be #defined to 0 or 1 -#endif - -// Ensure flags are compatible. -#if (NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1) && \ - (NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 0) - #error Precision format specifiers must be enabled if float support is enabled. -#endif - -// intmax_t / uintmax_t require stdint from c99 / c++11 -#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1 - #ifndef _MSC_VER - #ifdef __cplusplus - #if __cplusplus < 201103L - #error large format specifier support requires C++11 or later. - #endif - #else - #if __STDC_VERSION__ < 199409L - #error nanoprintf requires C99 or later. - #endif - #endif - #endif -#endif - -// Figure out if we can disable warnings with pragmas. -#ifdef __clang__ - #define NANOPRINTF_CLANG 1 - #define NANOPRINTF_GCC_PAST_4_6 0 -#else - #define NANOPRINTF_CLANG 0 - #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 6))) - #define NANOPRINTF_GCC_PAST_4_6 1 - #else - #define NANOPRINTF_GCC_PAST_4_6 0 - #endif -#endif - -#if NANOPRINTF_CLANG || NANOPRINTF_GCC_PAST_4_6 - #define NANOPRINTF_HAVE_GCC_WARNING_PRAGMAS 1 -#else - #define NANOPRINTF_HAVE_GCC_WARNING_PRAGMAS 0 -#endif - -#if NANOPRINTF_HAVE_GCC_WARNING_PRAGMAS - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Wunused-function" - #pragma GCC diagnostic ignored "-Wimplicit-fallthrough" - #ifdef __cplusplus - #pragma GCC diagnostic ignored "-Wold-style-cast" - #endif - #pragma GCC diagnostic ignored "-Wpadded" - #pragma GCC diagnostic ignored "-Wfloat-equal" - #if NANOPRINTF_CLANG - #pragma GCC diagnostic ignored "-Wc++98-compat-pedantic" - #pragma GCC diagnostic ignored "-Wcovered-switch-default" - #pragma GCC diagnostic ignored "-Wdeclaration-after-statement" - #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant" - #ifndef __APPLE__ - #pragma GCC diagnostic ignored "-Wunsafe-buffer-usage" - #endif - #elif NANOPRINTF_GCC_PAST_4_6 - #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" - #endif -#endif - -#ifdef _MSC_VER - #pragma warning(push) - #pragma warning(disable:4619) // there is no warning number 'number' - // C4619 has to be disabled first! - #pragma warning(disable:4127) // conditional expression is constant - #pragma warning(disable:4505) // unreferenced local function has been removed - #pragma warning(disable:4514) // unreferenced inline function has been removed - #pragma warning(disable:4701) // potentially uninitialized local variable used - #pragma warning(disable:4706) // assignment within conditional expression - #pragma warning(disable:4710) // function not inlined - #pragma warning(disable:4711) // function selected for inline expansion - #pragma warning(disable:4820) // padding added after struct member - #pragma warning(disable:5039) // potentially throwing function passed to extern C function - #pragma warning(disable:5045) // compiler will insert Spectre mitigation for memory load - #pragma warning(disable:5262) // implicit switch fall-through - #pragma warning(disable:26812) // enum type is unscoped -#endif - -#if (NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1) || \ - (NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1) -typedef enum { - NPF_FMT_SPEC_OPT_NONE, - NPF_FMT_SPEC_OPT_LITERAL, - NPF_FMT_SPEC_OPT_STAR, -} npf_fmt_spec_opt_t; -#endif - -typedef enum { - NPF_FMT_SPEC_LEN_MOD_NONE, - NPF_FMT_SPEC_LEN_MOD_SHORT, // 'h' - NPF_FMT_SPEC_LEN_MOD_LONG_DOUBLE, // 'L' - NPF_FMT_SPEC_LEN_MOD_CHAR, // 'hh' - NPF_FMT_SPEC_LEN_MOD_LONG, // 'l' -#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1 - NPF_FMT_SPEC_LEN_MOD_LARGE_LONG_LONG, // 'll' - NPF_FMT_SPEC_LEN_MOD_LARGE_INTMAX, // 'j' - NPF_FMT_SPEC_LEN_MOD_LARGE_SIZET, // 'z' - NPF_FMT_SPEC_LEN_MOD_LARGE_PTRDIFFT, // 't' -#endif -} npf_format_spec_length_modifier_t; - -typedef enum { - NPF_FMT_SPEC_CONV_PERCENT, // '%' - NPF_FMT_SPEC_CONV_CHAR, // 'c' - NPF_FMT_SPEC_CONV_STRING, // 's' - NPF_FMT_SPEC_CONV_SIGNED_INT, // 'i', 'd' -#if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1 - NPF_FMT_SPEC_CONV_BINARY, // 'b' -#endif - NPF_FMT_SPEC_CONV_OCTAL, // 'o' - NPF_FMT_SPEC_CONV_HEX_INT, // 'x', 'X' - NPF_FMT_SPEC_CONV_UNSIGNED_INT, // 'u' - NPF_FMT_SPEC_CONV_POINTER, // 'p' -#if NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS == 1 - NPF_FMT_SPEC_CONV_WRITEBACK, // 'n' -#endif -#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1 - NPF_FMT_SPEC_CONV_FLOAT_DEC, // 'f', 'F' - NPF_FMT_SPEC_CONV_FLOAT_SCI, // 'e', 'E' - NPF_FMT_SPEC_CONV_FLOAT_SHORTEST, // 'g', 'G' - NPF_FMT_SPEC_CONV_FLOAT_HEX, // 'a', 'A' -#endif -} npf_format_spec_conversion_t; - -typedef struct npf_format_spec { - char prepend; // ' ' or '+' - char alt_form; // '#' - -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - npf_fmt_spec_opt_t field_width_opt; - int field_width; - char left_justified; // '-' - char leading_zero_pad; // '0' -#endif - -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - npf_fmt_spec_opt_t prec_opt; - int prec; -#endif - - npf_format_spec_length_modifier_t length_modifier; - npf_format_spec_conversion_t conv_spec; - char case_adjust; -} npf_format_spec_t; - -#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 0 - typedef long npf_int_t; - typedef unsigned long npf_uint_t; -#else - typedef intmax_t npf_int_t; - typedef uintmax_t npf_uint_t; -#endif - -typedef struct npf_bufputc_ctx { - char *dst; - size_t len; - size_t cur; -} npf_bufputc_ctx_t; - -static int npf_parse_format_spec(char const *format, npf_format_spec_t *out_spec); -static void npf_bufputc(int c, void *ctx); -static void npf_bufputc_nop(int c, void *ctx); -static int npf_itoa_rev(char *buf, npf_int_t i); -static int npf_utoa_rev(char *buf, npf_uint_t i, unsigned base, unsigned case_adjust); - -#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1 -static int npf_ftoa_rev(char *buf, npf_format_spec_t const *spec, double f); -#endif - -#if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1 -static int npf_bin_len(npf_uint_t i); -#endif - -#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1 - #ifdef _MSC_VER - #include - typedef SSIZE_T ssize_t; - #endif -#endif - -#ifdef _MSC_VER - #include -#endif - -static int npf_max(int x, int y) { return (x > y) ? x : y; } - -int npf_parse_format_spec(char const *format, npf_format_spec_t *out_spec) { - char const *cur = format; - -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - out_spec->left_justified = 0; - out_spec->leading_zero_pad = 0; -#endif - out_spec->case_adjust = 'a'-'A'; // lowercase - out_spec->prepend = 0; - out_spec->alt_form = 0; - - while (*++cur) { // cur points at the leading '%' character - switch (*cur) { // Optional flags -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - case '-': out_spec->left_justified = '-'; out_spec->leading_zero_pad = 0; continue; - case '0': out_spec->leading_zero_pad = !out_spec->left_justified; continue; -#endif - case '+': out_spec->prepend = '+'; continue; - case ' ': if (out_spec->prepend == 0) { out_spec->prepend = ' '; } continue; - case '#': out_spec->alt_form = '#'; continue; - default: break; - } - break; - } - -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - out_spec->field_width_opt = NPF_FMT_SPEC_OPT_NONE; - if (*cur == '*') { - out_spec->field_width_opt = NPF_FMT_SPEC_OPT_STAR; - ++cur; - } else { - out_spec->field_width = 0; - while ((*cur >= '0') && (*cur <= '9')) { - out_spec->field_width_opt = NPF_FMT_SPEC_OPT_LITERAL; - out_spec->field_width = (out_spec->field_width * 10) + (*cur++ - '0'); - } - } -#endif - -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - out_spec->prec = 0; - out_spec->prec_opt = NPF_FMT_SPEC_OPT_NONE; - if (*cur == '.') { - ++cur; - if (*cur == '*') { - out_spec->prec_opt = NPF_FMT_SPEC_OPT_STAR; - ++cur; - } else { - if (*cur == '-') { - ++cur; - out_spec->prec_opt = NPF_FMT_SPEC_OPT_NONE; - } else { - out_spec->prec_opt = NPF_FMT_SPEC_OPT_LITERAL; - } - while ((*cur >= '0') && (*cur <= '9')) { - out_spec->prec = (out_spec->prec * 10) + (*cur++ - '0'); - } - } - } -#endif - - int tmp_conv = -1; - out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_NONE; - switch (*cur++) { // Length modifier - case 'h': - out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_SHORT; - if (*cur == 'h') { - out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_CHAR; - ++cur; - } - break; - case 'l': - out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LONG; -#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1 - if (*cur == 'l') { - out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LARGE_LONG_LONG; - ++cur; - } -#endif - break; -#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1 - case 'L': out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LONG_DOUBLE; break; -#endif -#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1 - case 'j': out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LARGE_INTMAX; break; - case 'z': out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LARGE_SIZET; break; - case 't': out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LARGE_PTRDIFFT; break; -#endif - default: --cur; break; - } - - switch (*cur++) { // Conversion specifier - case '%': out_spec->conv_spec = NPF_FMT_SPEC_CONV_PERCENT; -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - out_spec->prec_opt = NPF_FMT_SPEC_OPT_NONE; -#endif - break; - - case 'c': out_spec->conv_spec = NPF_FMT_SPEC_CONV_CHAR; -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - out_spec->prec_opt = NPF_FMT_SPEC_OPT_NONE; -#endif - break; - - case 's': out_spec->conv_spec = NPF_FMT_SPEC_CONV_STRING; -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - out_spec->leading_zero_pad = 0; -#endif - break; - - case 'i': - case 'd': tmp_conv = NPF_FMT_SPEC_CONV_SIGNED_INT; - case 'o': if (tmp_conv == -1) { tmp_conv = NPF_FMT_SPEC_CONV_OCTAL; } - case 'u': if (tmp_conv == -1) { tmp_conv = NPF_FMT_SPEC_CONV_UNSIGNED_INT; } - case 'X': if (tmp_conv == -1) { out_spec->case_adjust = 0; } - case 'x': if (tmp_conv == -1) { tmp_conv = NPF_FMT_SPEC_CONV_HEX_INT; } - out_spec->conv_spec = (npf_format_spec_conversion_t)tmp_conv; -#if (NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1) && \ - (NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1) - if (out_spec->prec_opt != NPF_FMT_SPEC_OPT_NONE) { out_spec->leading_zero_pad = 0; } -#endif - break; - -#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1 - case 'F': out_spec->case_adjust = 0; - case 'f': - out_spec->conv_spec = NPF_FMT_SPEC_CONV_FLOAT_DEC; - if (out_spec->prec_opt == NPF_FMT_SPEC_OPT_NONE) { out_spec->prec = 6; } - break; - - case 'E': out_spec->case_adjust = 0; - case 'e': - out_spec->conv_spec = NPF_FMT_SPEC_CONV_FLOAT_SCI; - if (out_spec->prec_opt == NPF_FMT_SPEC_OPT_NONE) { out_spec->prec = 6; } - break; - - case 'G': out_spec->case_adjust = 0; - case 'g': - out_spec->conv_spec = NPF_FMT_SPEC_CONV_FLOAT_SHORTEST; - if (out_spec->prec_opt == NPF_FMT_SPEC_OPT_NONE) { out_spec->prec = 6; } - break; - - case 'A': out_spec->case_adjust = 0; - case 'a': - out_spec->conv_spec = NPF_FMT_SPEC_CONV_FLOAT_HEX; - if (out_spec->prec_opt == NPF_FMT_SPEC_OPT_NONE) { out_spec->prec = 6; } - break; -#endif - -#if NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS == 1 - case 'n': - // todo: reject string if flags or width or precision exist - out_spec->conv_spec = NPF_FMT_SPEC_CONV_WRITEBACK; -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - out_spec->prec_opt = NPF_FMT_SPEC_OPT_NONE; -#endif - break; -#endif - - case 'p': - out_spec->conv_spec = NPF_FMT_SPEC_CONV_POINTER; -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - out_spec->prec_opt = NPF_FMT_SPEC_OPT_NONE; -#endif - break; - -#if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1 - case 'B': - out_spec->case_adjust = 0; - case 'b': - out_spec->conv_spec = NPF_FMT_SPEC_CONV_BINARY; - break; -#endif - - default: return 0; - } - - return (int)(cur - format); -} - -int npf_itoa_rev(char *buf, npf_int_t i) { - int n = 0; - int const sign = (i >= 0) ? 1 : -1; - do { *buf++ = (char)('0' + (sign * (i % 10))); i /= 10; ++n; } while (i); - return n; -} - -int npf_utoa_rev(char *buf, npf_uint_t i, unsigned base, unsigned case_adj) { - int n = 0; - do { - unsigned const d = (unsigned)(i % base); - *buf++ = (char)((d < 10) ? ('0' + d) : ('A' + case_adj + (d - 10))); - i /= base; - ++n; - } while (i); - return n; -} - -#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1 - -#include - -#if (DBL_MANT_DIG <= 11) && (DBL_MAX_EXP <= 16) - typedef uint_fast16_t npf_double_bin_t; - typedef int_fast8_t npf_ftoa_exp_t; -#elif (DBL_MANT_DIG <= 24) && (DBL_MAX_EXP <= 128) - typedef uint_fast32_t npf_double_bin_t; - typedef int_fast8_t npf_ftoa_exp_t; -#elif (DBL_MANT_DIG <= 53) && (DBL_MAX_EXP <= 1024) - typedef uint_fast64_t npf_double_bin_t; - typedef int_fast16_t npf_ftoa_exp_t; -#else - #error Unsupported width of the double type. -#endif - -// The floating point conversion code works with an unsigned integer type of any size. -#ifndef NANOPRINTF_CONVERSION_FLOAT_TYPE - #define NANOPRINTF_CONVERSION_FLOAT_TYPE unsigned int -#endif -typedef NANOPRINTF_CONVERSION_FLOAT_TYPE npf_ftoa_man_t; - -#if (NANOPRINTF_CONVERSION_BUFFER_SIZE <= UINT_FAST8_MAX) && (UINT_FAST8_MAX <= INT_MAX) - typedef uint_fast8_t npf_ftoa_dec_t; -#else - typedef int npf_ftoa_dec_t; -#endif - -enum { - NPF_DOUBLE_EXP_MASK = DBL_MAX_EXP * 2 - 1, - NPF_DOUBLE_EXP_BIAS = DBL_MAX_EXP - 1, - NPF_DOUBLE_MAN_BITS = DBL_MANT_DIG - 1, - NPF_DOUBLE_BIN_BITS = sizeof(npf_double_bin_t) * CHAR_BIT, - NPF_FTOA_MAN_BITS = sizeof(npf_ftoa_man_t) * CHAR_BIT, - NPF_FTOA_SHIFT_BITS = ((NPF_FTOA_MAN_BITS < DBL_MANT_DIG) ? NPF_FTOA_MAN_BITS : DBL_MANT_DIG) - 1 -}; - -/* Generally floating-point conversion implementations use - grisu2 (https://bit.ly/2JgMggX) and ryu (https://bit.ly/2RLXSg0) algorithms, - which are mathematically exact and fast, but require large lookup tables. - - This implementation was inspired by Wojciech Muła's (zdjęcia@garnek.pl) - algorithm (http://0x80.pl/notesen/2015-12-29-float-to-string.html) and - extended further by adding dynamic scaling and configurable integer width by - Oskars Rubenis (https://github.com/Okarss). -*/ -static int npf_ftoa_rev(char *buf, npf_format_spec_t const *spec, double f) { - char const *ret = NULL; - npf_double_bin_t bin; { // Union-cast is UB, let compiler optimize byte-copy loop. - char const *src = (char const *)&f; - char *dst = (char *)&bin; - for (uint_fast8_t i = 0; i < sizeof(f); ++i) { dst[i] = src[i]; } - } - - // Unsigned to signed integer casting is UB, but it works for two's complement implementations. - npf_ftoa_exp_t exp = (npf_ftoa_exp_t)((npf_ftoa_exp_t)(bin >> NPF_DOUBLE_MAN_BITS) & NPF_DOUBLE_EXP_MASK); - bin &= ((npf_double_bin_t)0x1 << NPF_DOUBLE_MAN_BITS) - 1; - if (exp == (npf_ftoa_exp_t)NPF_DOUBLE_EXP_MASK) { // special value - ret = (bin) ? "NAN" : "FNI"; - goto exit; - } - if (spec->prec > (NANOPRINTF_CONVERSION_BUFFER_SIZE - 2)) { goto exit; } - if (exp) { // normal number - bin |= (npf_double_bin_t)0x1 << NPF_DOUBLE_MAN_BITS; - } else { // subnormal number - ++exp; - } - exp = (npf_ftoa_exp_t)(exp - NPF_DOUBLE_EXP_BIAS); - - uint_fast8_t carry; carry = 0; - npf_ftoa_dec_t end, dec; dec = (npf_ftoa_dec_t)spec->prec; - if (dec || spec->alt_form) { - buf[dec++] = '.'; - } - - { // Integer part - npf_ftoa_man_t man_i; - - if (exp >= 0) { - int_fast8_t shift_i = (int_fast8_t)((exp > NPF_FTOA_SHIFT_BITS) ? (int)NPF_FTOA_SHIFT_BITS : exp); - npf_ftoa_exp_t exp_i = (npf_ftoa_exp_t)(exp - shift_i); - shift_i = (int_fast8_t)(NPF_DOUBLE_MAN_BITS - shift_i); - man_i = (npf_ftoa_man_t)(bin >> shift_i); - - if (exp_i) { - if (shift_i) { - carry = (bin >> (shift_i - 1)) & 0x1; - } - exp = NPF_DOUBLE_MAN_BITS; // invalidate the fraction part - } - - // Scale the exponent from base-2 to base-10. - for (; exp_i; --exp_i) { - if (!(man_i & ((npf_ftoa_man_t)0x1 << (NPF_FTOA_MAN_BITS - 1)))) { - man_i = (npf_ftoa_man_t)(man_i << 1); - man_i = (npf_ftoa_man_t)(man_i | carry); carry = 0; - } else { - if (dec >= NANOPRINTF_CONVERSION_BUFFER_SIZE) { goto exit; } - buf[dec++] = '0'; - carry = (((uint_fast8_t)(man_i % 5) + carry) > 2); - man_i /= 5; - } - } - } else { - man_i = 0; - } - end = dec; - - // Print the integer - do { - if (end >= NANOPRINTF_CONVERSION_BUFFER_SIZE) { goto exit; } - buf[end++] = (char)('0' + (char)(man_i % 10)); - man_i /= 10; - } while (man_i); - } - - { // Fraction part - npf_ftoa_man_t man_f; - npf_ftoa_dec_t dec_f = (npf_ftoa_dec_t)spec->prec; - - if (exp < NPF_DOUBLE_MAN_BITS) { - int_fast8_t shift_f = (int_fast8_t)((exp < 0) ? -1 : exp); - npf_ftoa_exp_t exp_f = (npf_ftoa_exp_t)(exp - shift_f); - npf_double_bin_t bin_f = bin << ((NPF_DOUBLE_BIN_BITS - NPF_DOUBLE_MAN_BITS) + shift_f); - - // This if-else statement can be completely optimized at compile time. - if (NPF_DOUBLE_BIN_BITS > NPF_FTOA_MAN_BITS) { - man_f = (npf_ftoa_man_t)(bin_f >> ((unsigned)(NPF_DOUBLE_BIN_BITS - NPF_FTOA_MAN_BITS) % NPF_DOUBLE_BIN_BITS)); - carry = (uint_fast8_t)((bin_f >> ((unsigned)(NPF_DOUBLE_BIN_BITS - NPF_FTOA_MAN_BITS - 1) % NPF_DOUBLE_BIN_BITS)) & 0x1); - } else { - man_f = (npf_ftoa_man_t)((npf_ftoa_man_t)bin_f << ((unsigned)(NPF_FTOA_MAN_BITS - NPF_DOUBLE_BIN_BITS) % NPF_FTOA_MAN_BITS)); - carry = 0; - } - - // Scale the exponent from base-2 to base-10 and prepare the first digit. - for (uint_fast8_t digit = 0; dec_f && (exp_f < 4); ++exp_f) { - if ((man_f > ((npf_ftoa_man_t)-4 / 5)) || digit) { - carry = (uint_fast8_t)(man_f & 0x1); - man_f = (npf_ftoa_man_t)(man_f >> 1); - } else { - man_f = (npf_ftoa_man_t)(man_f * 5); - if (carry) { man_f = (npf_ftoa_man_t)(man_f + 3); carry = 0; } - if (exp_f < 0) { - buf[--dec_f] = '0'; - } else { - ++digit; - } - } - } - man_f = (npf_ftoa_man_t)(man_f + carry); - carry = (exp_f >= 0); - dec = 0; - } else { - man_f = 0; - } - - if (dec_f) { - // Print the fraction - for (;;) { - buf[--dec_f] = (char)('0' + (char)(man_f >> (NPF_FTOA_MAN_BITS - 4))); - man_f = (npf_ftoa_man_t)(man_f & ~((npf_ftoa_man_t)0xF << (NPF_FTOA_MAN_BITS - 4))); - if (!dec_f) { break; } - man_f = (npf_ftoa_man_t)(man_f * 10); - } - man_f = (npf_ftoa_man_t)(man_f << 4); - } - if (exp < NPF_DOUBLE_MAN_BITS) { - carry &= (uint_fast8_t)(man_f >> (NPF_FTOA_MAN_BITS - 1)); - } - } - - // Round the number - for (; carry; ++dec) { - if (dec >= NANOPRINTF_CONVERSION_BUFFER_SIZE) { goto exit; } - if (dec >= end) { buf[end++] = '0'; } - if (buf[dec] == '.') { continue; } - carry = (buf[dec] == '9'); - buf[dec] = (char)(carry ? '0' : (buf[dec] + 1)); - } - - return (int)end; -exit: - if (!ret) { ret = "RRE"; } - uint_fast8_t i; - for (i = 0; ret[i]; ++i) { buf[i] = (char)(ret[i] + spec->case_adjust); } - return (int)i; -} - -#endif // NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS - -#if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1 -int npf_bin_len(npf_uint_t u) { - // Return the length of the binary string format of 'u', preferring intrinsics. - if (!u) { return 1; } - -#ifdef _MSC_VER // Win64, use _BSR64 for everything. If x86, use _BSR when non-large. - #ifdef _M_X64 - #define NPF_HAVE_BUILTIN_CLZ - #define NPF_CLZ _BitScanReverse64 - #elif NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 0 - #define NPF_HAVE_BUILTIN_CLZ - #define NPF_CLZ _BitScanReverse - #endif - #ifdef NPF_HAVE_BUILTIN_CLZ - unsigned long idx; - NPF_CLZ(&idx, u); - return (int)(idx + 1); - #endif -#elif defined(NANOPRINTF_CLANG) || defined(NANOPRINTF_GCC_PAST_4_6) - #define NPF_HAVE_BUILTIN_CLZ - #if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1 - #define NPF_CLZ(X) ((sizeof(long long) * CHAR_BIT) - (size_t)__builtin_clzll(X)) - #else - #define NPF_CLZ(X) ((sizeof(long) * CHAR_BIT) - (size_t)__builtin_clzl(X)) - #endif - return (int)NPF_CLZ(u); -#endif - -#ifndef NPF_HAVE_BUILTIN_CLZ - int n; - for (n = 0; u; ++n, u >>= 1); // slow but small software fallback - return n; -#else - #undef NPF_HAVE_BUILTIN_CLZ - #undef NPF_CLZ -#endif -} -#endif - -void npf_bufputc(int c, void *ctx) { - npf_bufputc_ctx_t *bpc = (npf_bufputc_ctx_t *)ctx; - if (bpc->cur < bpc->len) { bpc->dst[bpc->cur++] = (char)c; } -} - -void npf_bufputc_nop(int c, void *ctx) { (void)c; (void)ctx; } - -typedef struct npf_cnt_putc_ctx { - npf_putc pc; - void *ctx; - int n; -} npf_cnt_putc_ctx_t; - -static void npf_putc_cnt(int c, void *ctx) { - npf_cnt_putc_ctx_t *pc_cnt = (npf_cnt_putc_ctx_t *)ctx; - ++pc_cnt->n; - pc_cnt->pc(c, pc_cnt->ctx); // sibling-call optimization -} - -#define NPF_PUTC(VAL) do { npf_putc_cnt((int)(VAL), &pc_cnt); } while (0) - -#define NPF_EXTRACT(MOD, CAST_TO, EXTRACT_AS) \ - case NPF_FMT_SPEC_LEN_MOD_##MOD: val = (CAST_TO)va_arg(args, EXTRACT_AS); break - -#define NPF_WRITEBACK(MOD, TYPE) \ - case NPF_FMT_SPEC_LEN_MOD_##MOD: *(va_arg(args, TYPE *)) = (TYPE)pc_cnt.n; break - -int npf_vpprintf(npf_putc pc, void *pc_ctx, char const *format, va_list args) { - npf_format_spec_t fs; - char const *cur = format; - npf_cnt_putc_ctx_t pc_cnt; - pc_cnt.pc = pc; - pc_cnt.ctx = pc_ctx; - pc_cnt.n = 0; - - while (*cur) { - int const fs_len = (*cur != '%') ? 0 : npf_parse_format_spec(cur, &fs); - if (!fs_len) { NPF_PUTC(*cur++); continue; } - cur += fs_len; - - // Extract star-args immediately -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - if (fs.field_width_opt == NPF_FMT_SPEC_OPT_STAR) { - fs.field_width_opt = NPF_FMT_SPEC_OPT_LITERAL; - fs.field_width = va_arg(args, int); - if (fs.field_width < 0) { - fs.field_width = -fs.field_width; - fs.left_justified = 1; - } - } -#endif -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - if (fs.prec_opt == NPF_FMT_SPEC_OPT_STAR) { - fs.prec_opt = NPF_FMT_SPEC_OPT_NONE; - fs.prec = va_arg(args, int); - if (fs.prec >= 0) { fs.prec_opt = NPF_FMT_SPEC_OPT_LITERAL; } - } -#endif - - union { char cbuf_mem[NANOPRINTF_CONVERSION_BUFFER_SIZE]; npf_uint_t binval; } u; - char *cbuf = u.cbuf_mem, sign_c = 0; - int cbuf_len = 0, need_0x = 0; -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - int field_pad = 0; - char pad_c = 0; -#endif -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - int prec_pad = 0; -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - int zero = 0; -#endif -#endif - - // Extract and convert the argument to string, point cbuf at the text. - switch (fs.conv_spec) { - case NPF_FMT_SPEC_CONV_PERCENT: - *cbuf = '%'; - cbuf_len = 1; - break; - - case NPF_FMT_SPEC_CONV_CHAR: - *cbuf = (char)va_arg(args, int); - cbuf_len = 1; - break; - - case NPF_FMT_SPEC_CONV_STRING: { - cbuf = va_arg(args, char *); -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - for (char const *s = cbuf; - ((fs.prec_opt == NPF_FMT_SPEC_OPT_NONE) || (cbuf_len < fs.prec)) && *s; - ++s, ++cbuf_len); -#else - for (char const *s = cbuf; *s; ++s, ++cbuf_len); // strlen -#endif - } break; - - case NPF_FMT_SPEC_CONV_SIGNED_INT: { - npf_int_t val = 0; - switch (fs.length_modifier) { - NPF_EXTRACT(NONE, int, int); - NPF_EXTRACT(SHORT, short, int); - NPF_EXTRACT(LONG_DOUBLE, int, int); - NPF_EXTRACT(CHAR, char, int); - NPF_EXTRACT(LONG, long, long); -#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1 - NPF_EXTRACT(LARGE_LONG_LONG, long long, long long); - NPF_EXTRACT(LARGE_INTMAX, intmax_t, intmax_t); - NPF_EXTRACT(LARGE_SIZET, ssize_t, ssize_t); - NPF_EXTRACT(LARGE_PTRDIFFT, ptrdiff_t, ptrdiff_t); -#endif - default: break; - } - - sign_c = (val < 0) ? '-' : fs.prepend; - -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - zero = !val; -#endif - // special case, if prec and value are 0, skip - if (!val && (fs.prec_opt == NPF_FMT_SPEC_OPT_LITERAL) && !fs.prec) { - cbuf_len = 0; - } else -#endif - { cbuf_len = npf_itoa_rev(cbuf, val); } - } break; - -#if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1 - case NPF_FMT_SPEC_CONV_BINARY: -#endif - case NPF_FMT_SPEC_CONV_OCTAL: - case NPF_FMT_SPEC_CONV_HEX_INT: - case NPF_FMT_SPEC_CONV_UNSIGNED_INT: { - npf_uint_t val = 0; - - switch (fs.length_modifier) { - NPF_EXTRACT(NONE, unsigned, unsigned); - NPF_EXTRACT(SHORT, unsigned short, unsigned); - NPF_EXTRACT(LONG_DOUBLE, unsigned, unsigned); - NPF_EXTRACT(CHAR, unsigned char, unsigned); - NPF_EXTRACT(LONG, unsigned long, unsigned long); -#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1 - NPF_EXTRACT(LARGE_LONG_LONG, unsigned long long, unsigned long long); - NPF_EXTRACT(LARGE_INTMAX, uintmax_t, uintmax_t); - NPF_EXTRACT(LARGE_SIZET, size_t, size_t); - NPF_EXTRACT(LARGE_PTRDIFFT, size_t, size_t); -#endif - default: break; - } - -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - zero = !val; -#endif - if (!val && (fs.prec_opt == NPF_FMT_SPEC_OPT_LITERAL) && !fs.prec) { - // Zero value and explicitly-requested zero precision means "print nothing". - if ((fs.conv_spec == NPF_FMT_SPEC_CONV_OCTAL) && fs.alt_form) { - fs.prec = 1; // octal special case, print a single '0' - } - } else -#endif -#if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1 - if (fs.conv_spec == NPF_FMT_SPEC_CONV_BINARY) { - cbuf_len = npf_bin_len(val); u.binval = val; - } else -#endif - { - unsigned const base = (fs.conv_spec == NPF_FMT_SPEC_CONV_OCTAL) ? - 8u : ((fs.conv_spec == NPF_FMT_SPEC_CONV_HEX_INT) ? 16u : 10u); - cbuf_len = npf_utoa_rev(cbuf, val, base, (unsigned)fs.case_adjust); - } - - if (val && fs.alt_form && (fs.conv_spec == NPF_FMT_SPEC_CONV_OCTAL)) { - cbuf[cbuf_len++] = '0'; // OK to add leading octal '0' immediately. - } - - if (val && fs.alt_form) { // 0x or 0b but can't write it yet. - if (fs.conv_spec == NPF_FMT_SPEC_CONV_HEX_INT) { need_0x = 'X'; } -#if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1 - else if (fs.conv_spec == NPF_FMT_SPEC_CONV_BINARY) { need_0x = 'B'; } -#endif - if (need_0x) { need_0x += fs.case_adjust; } - } - } break; - - case NPF_FMT_SPEC_CONV_POINTER: { - cbuf_len = - npf_utoa_rev(cbuf, (npf_uint_t)(uintptr_t)va_arg(args, void *), 16, 'a'-'A'); - need_0x = 'x'; - } break; - -#if NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS == 1 - case NPF_FMT_SPEC_CONV_WRITEBACK: - switch (fs.length_modifier) { - NPF_WRITEBACK(NONE, int); - NPF_WRITEBACK(SHORT, short); - NPF_WRITEBACK(LONG, long); - NPF_WRITEBACK(LONG_DOUBLE, double); - NPF_WRITEBACK(CHAR, signed char); -#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1 - NPF_WRITEBACK(LARGE_LONG_LONG, long long); - NPF_WRITEBACK(LARGE_INTMAX, intmax_t); - NPF_WRITEBACK(LARGE_SIZET, size_t); - NPF_WRITEBACK(LARGE_PTRDIFFT, ptrdiff_t); -#endif - default: break; - } break; -#endif - -#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1 - case NPF_FMT_SPEC_CONV_FLOAT_DEC: - case NPF_FMT_SPEC_CONV_FLOAT_SCI: - case NPF_FMT_SPEC_CONV_FLOAT_SHORTEST: - case NPF_FMT_SPEC_CONV_FLOAT_HEX: { - double val; - if (fs.length_modifier == NPF_FMT_SPEC_LEN_MOD_LONG_DOUBLE) { - val = (double)va_arg(args, long double); - } else { - val = va_arg(args, double); - } - - sign_c = (val < 0.) ? '-' : fs.prepend; -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - zero = (val == 0.); -#endif - cbuf_len = npf_ftoa_rev(cbuf, &fs, val); - } break; -#endif - default: break; - } - -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - // Compute the field width pad character - if (fs.field_width_opt == NPF_FMT_SPEC_OPT_LITERAL) { - if (fs.leading_zero_pad) { // '0' flag is only legal with numeric types - if ((fs.conv_spec != NPF_FMT_SPEC_CONV_STRING) && - (fs.conv_spec != NPF_FMT_SPEC_CONV_CHAR) && - (fs.conv_spec != NPF_FMT_SPEC_CONV_PERCENT)) { -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - if ((fs.prec_opt == NPF_FMT_SPEC_OPT_LITERAL) && !fs.prec && zero) { - pad_c = ' '; - } else -#endif - { pad_c = '0'; } - } - } else { pad_c = ' '; } - } -#endif - - // Compute the number of bytes to truncate or '0'-pad. -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - if (fs.conv_spec != NPF_FMT_SPEC_CONV_STRING) { -#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1 - // float precision is after the decimal point - if (fs.conv_spec != NPF_FMT_SPEC_CONV_FLOAT_DEC) -#endif - { prec_pad = npf_max(0, fs.prec - cbuf_len); } - } -#endif - -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - // Given the full converted length, how many pad bytes? - field_pad = fs.field_width - cbuf_len - !!sign_c; - if (need_0x) { field_pad -= 2; } -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - field_pad -= prec_pad; -#endif - field_pad = npf_max(0, field_pad); - - // Apply right-justified field width if requested - if (!fs.left_justified && pad_c) { // If leading zeros pad, sign goes first. - if (pad_c == '0') { - if (sign_c) { NPF_PUTC(sign_c); sign_c = 0; } - // Pad byte is '0', write '0x' before '0' pad chars. - if (need_0x) { NPF_PUTC('0'); NPF_PUTC(need_0x); } - } - while (field_pad-- > 0) { NPF_PUTC(pad_c); } - // Pad byte is ' ', write '0x' after ' ' pad chars but before number. - if ((pad_c != '0') && need_0x) { NPF_PUTC('0'); NPF_PUTC(need_0x); } - } else -#endif - { if (need_0x) { NPF_PUTC('0'); NPF_PUTC(need_0x); } } // no pad, '0x' requested. - - // Write the converted payload - if (fs.conv_spec == NPF_FMT_SPEC_CONV_STRING) { - for (int i = 0; i < cbuf_len; ++i) { NPF_PUTC(cbuf[i]); } - } else { - if (sign_c) { NPF_PUTC(sign_c); } -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - while (prec_pad-- > 0) { NPF_PUTC('0'); } // int precision leads. -#endif -#if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1 - if (fs.conv_spec == NPF_FMT_SPEC_CONV_BINARY) { - while (cbuf_len) { NPF_PUTC('0' + ((u.binval >> --cbuf_len) & 1)); } - } else -#endif - { while (cbuf_len-- > 0) { NPF_PUTC(cbuf[cbuf_len]); } } // payload is reversed - } - -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - if (fs.left_justified && pad_c) { // Apply left-justified field width - while (field_pad-- > 0) { NPF_PUTC(pad_c); } - } -#endif - } - - return pc_cnt.n; -} - -#undef NPF_PUTC -#undef NPF_EXTRACT -#undef NPF_WRITEBACK - -int npf_pprintf(npf_putc pc, void *pc_ctx, char const *format, ...) { - va_list val; - va_start(val, format); - int const rv = npf_vpprintf(pc, pc_ctx, format, val); - va_end(val); - return rv; -} - -int npf_snprintf(char *buffer, size_t bufsz, const char *format, ...) { - va_list val; - va_start(val, format); - int const rv = npf_vsnprintf(buffer, bufsz, format, val); - va_end(val); - return rv; -} - -int npf_vsnprintf(char *buffer, size_t bufsz, char const *format, va_list vlist) { - npf_bufputc_ctx_t bufputc_ctx; - bufputc_ctx.dst = buffer; - bufputc_ctx.len = bufsz; - bufputc_ctx.cur = 0; - - npf_putc const pc = buffer ? npf_bufputc : npf_bufputc_nop; - int const n = npf_vpprintf(pc, &bufputc_ctx, format, vlist); - pc('\0', &bufputc_ctx); - - if (buffer && bufsz) { -#ifdef NANOPRINTF_SNPRINTF_SAFE_EMPTY_STRING_ON_OVERFLOW - if (n >= (int)bufsz) { buffer[0] = '\0'; } -#else - buffer[bufsz - 1] = '\0'; -#endif - } - - return n; -} - -#if NANOPRINTF_HAVE_GCC_WARNING_PRAGMAS - #pragma GCC diagnostic pop -#endif - -#ifdef _MSC_VER - #pragma warning(pop) -#endif - -#endif // NANOPRINTF_IMPLEMENTATION_INCLUDED -#endif // NANOPRINTF_IMPLEMENTATION - -/* - nanoprintf is dual-licensed under both the "Unlicense" and the - "Zero-Clause BSD" (0BSD) licenses. The intent of this dual-licensing - structure is to make nanoprintf as consumable as possible in as many - environments / countries / companies as possible without any - encumberances. - - The text of the two licenses follows below: - - ============================== UNLICENSE ============================== - - This is free and unencumbered software released into the public domain. - - Anyone is free to copy, modify, publish, use, compile, sell, or - distribute this software, either in source code form or as a compiled - binary, for any purpose, commercial or non-commercial, and by any - means. - - In jurisdictions that recognize copyright laws, the author or authors - of this software dedicate any and all copyright interest in the - software to the public domain. We make this dedication for the benefit - of the public at large and to the detriment of our heirs and - successors. We intend this dedication to be an overt act of - relinquishment in perpetuity of all present and future rights to this - software under copyright law. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR - OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - OTHER DEALINGS IN THE SOFTWARE. - - For more information, please refer to - - ================================ 0BSD ================================= - - Copyright (C) 2019- by Charles Nicholson - - Permission to use, copy, modify, and/or distribute this software for - any purpose with or without fee is hereby granted. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/loader/NativePtr.h b/src/oboskrnl/arch/m68k/loader/NativePtr.h deleted file mode 100644 index 9a0d0a6a..00000000 --- a/src/oboskrnl/arch/m68k/loader/NativePtr.h +++ /dev/null @@ -1,104 +0,0 @@ -#pragma once - -#include -#include - -namespace sl -{ - template - union IntPtr - { - BackingType raw; - void* ptr; - - constexpr IntPtr() : raw(0) {} - constexpr IntPtr(BackingType r) : raw(r) {} - constexpr IntPtr(void* p) : ptr(p) {} - constexpr IntPtr(const void* p) = delete; - constexpr IntPtr(decltype(nullptr)) : ptr(nullptr) {} - - template - constexpr AsType* As() const - { - return reinterpret_cast(ptr); - } - - template - constexpr AsType* As(const size_t byteOffset) const - { - return reinterpret_cast((void*)(raw + byteOffset)); - } - - constexpr IntPtr Offset(size_t offset) const - { return IntPtr(raw + offset); } - - template - void Write(Word value) const - { - *reinterpret_cast(ptr) = value; - } - - template - Word Read() const - { - return *reinterpret_cast(ptr); - } - - template - void Push(Word value) - { - if (growDown) - raw -= sizeof(Word); - *reinterpret_cast(ptr) = value; - if (!growDown) - raw += sizeof(Word); - } - - template - Word Pop() - { - if (growDown) - raw -= sizeof(Word); - Word value = *reinterpret_cast(ptr); - if (!growDown) - raw += sizeof(Word); - return value; - } - }; - - template - union ConstIntPtr - { - BackingType raw; - const void* ptr; - - constexpr ConstIntPtr() : raw(0) {} - constexpr ConstIntPtr(BackingType r) : raw(r) {} - constexpr ConstIntPtr(const void* p) : ptr(p) {} - constexpr ConstIntPtr(decltype(nullptr)) : ptr(nullptr) {} - - template - constexpr const AsType* As() const - { - return reinterpret_cast(ptr); - } - - template - constexpr const AsType* As(const size_t byteOffset) const - { - return reinterpret_cast((void*)(raw + byteOffset)); - } - - constexpr ConstIntPtr Offset(size_t offset) const - { return ConstIntPtr(raw + offset); } - - template - Word Read() const - { - return *reinterpret_cast(ptr); - } - }; - - using NativePtr = IntPtr; - using CNativePtr = ConstIntPtr; -} \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/loader/README.md b/src/oboskrnl/arch/m68k/loader/README.md deleted file mode 100644 index fce929d6..00000000 --- a/src/oboskrnl/arch/m68k/loader/README.md +++ /dev/null @@ -1,33 +0,0 @@ -# Limine Bootshim - M68k series -I made this as part of a challenge with another member of the osdev discord server, to see who could port their project to the qemu virt m68k machine first. It's been tested in that single scenario, and likely only on my system. If you're unfamiliar with the limine protocol, its intended to be exclusively 64-bit, so I've made some modifications to the spec (see below) in order for it to be usable here. - -Having said that, this does what you'd expect: it allows the northport kernel to boot natively (via limine) on the qemu m68k virt board. It provides most of the responses the northport kernel needs, and there are some that it should provide but doesnt right now. Naturally there are some requests it cant repond to (dtb, rsdp, efi runtime services table). In theory this should work with any limine-compliant kernel, keeping in mind the changes below. - -## Bootloader Config - -The shim is extremely primitive, but it does allow some configuration at compile-time. The following macros can be defined when compiling the shim: -- `NPL_INITRD_CMDLINE`: The shim can only provide a single module to the kernel (passed via the `-initrd` arg to qemu), this macro can be used to set the cmdline field for the module. -- `NPL_KERNEL_CMDLINE`: If defined, this macro is used as a string to append to the kernel command line, passed as the cmdline field of the kernel file response. If the firmware also passes a command line to the loader (for example, via qemu's `-append` flag) these strings are concatenated and the result is passed to the kernel. - -## Limine Boot Protocol Modifications -Please note: these changes are my own, and not endorsed by the original authors of the limine protocol. If you're using this shim for your own kernel, do not ask for help upstream or bother actual limine devs. - -- All pointers are natively sized: 64-bits on 64-bit systems (existing targets of the limine protocol), and 32-bits on 32-bit systems (m68k). While this could have remain unchanged, it's more convinient for the kernel and loader this way. -- The kernel is still required to be loaded in the upper 2GiB of virtual memory, however it's recommended the kernel is loaded higher than, as this is the entire higher half on a 32-bit system. -- The kernel **must** be a 32-bit ELF, no support for boot anchors is provided. - -- The transparent translation registers are cleared to zero (disabled). - -### SMP Feature - -### Paging Mode Feature -The following values are added for the m68k implementation: - -```c -#define LIMINE_PAGING_MODE_M68K_4K 0 -#define LIMINE_PAGING_MODE_M68k_8K 1 - -#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_M68K_4K -``` - -No flags are currently defined. The default (if no request is present) is `LIMINE_PAGING_MODE_M68K_4K`. diff --git a/src/oboskrnl/arch/m68k/loader/Syslib.cpp b/src/oboskrnl/arch/m68k/loader/Syslib.cpp deleted file mode 100644 index c5ff858f..00000000 --- a/src/oboskrnl/arch/m68k/loader/Syslib.cpp +++ /dev/null @@ -1,100 +0,0 @@ -#include -#include - -//Syslib does have a version compiled for kernel use, but that uses the got/plt, -//which I dont want to have in the loader. So the functions we need from syslib -//are implemented here instead, and compiled-in (insteading of being linked-in). -namespace sl -{ - void* memset(void* const start, uint8_t value, size_t count) - { - uint8_t* const si = static_cast(start); - - for (size_t i = 0; i < count; i++) - si[i] = value; - return start; - } - - void* memcopy(const void* const source, void* const dest, size_t count) - { - const uint8_t* const si = static_cast(source); - uint8_t* const di = static_cast(dest); - - for (size_t i = 0; i < count; i++) - di[i] = si[i]; - return dest; - } - - void* memcopy(const void* const source, size_t sourceOffset, void* const dest, size_t destOffset, size_t count) - { - const uint8_t* const si = static_cast(source); - uint8_t* const di = static_cast(dest); - - for (size_t i = 0; i < count; i++) - di[destOffset + i] = si[sourceOffset + i]; - return dest; - } - - int memcmp(const void* const a, const void* const b, size_t count) - { - const uint8_t* const ai = static_cast(a); - const uint8_t* const bi = static_cast(b); - - for (size_t i = 0; i < count; i++) - { - if (ai[i] > bi[i]) - return 1; - else if (bi[i] > ai[i]) - return -1; - } - return 0; - } - - int memcmp(const void* const a, size_t offsetA, const void* const b, size_t offsetB, size_t count) - { - const uint8_t* ai = static_cast(a); - const uint8_t* bi = static_cast(b); - ai += offsetA; - bi += offsetB; - - for (size_t i = 0; i < count; i++) - { - if (ai[i] > bi[i]) - return 1; - else if (bi[i] > ai[i]) - return -1; - } - return 0; - } - - ComputedReloc ComputeRelocation(Elf_Word type, uintptr_t a, uintptr_t b, uintptr_t s, uintptr_t p) - { - switch (type) - { - case R_68K_GLOB_DAT: return { .value = s, .length = 4, .usedSymbol = true }; - case R_68K_JMP_SLOT: return { .value = s, .length = 4, .usedSymbol = true }; - case R_68K_RELATIVE: return { .value = b + a, .length = 4, .usedSymbol = false }; - } - return { .value = 0, .length = 0, .usedSymbol = false }; - } -} - -extern "C" -{ - void* memcpy(void* dest, const void* src, size_t len) - { return sl::memcopy(src, dest, len); } - - void* memset(void* dest, int value, size_t len) - { return sl::memset(dest, value, len); } - - void* memmove(void* dest, const void* src, size_t len) - { - uint8_t* di = static_cast(dest); - const uint8_t* si = static_cast(src); - - for (size_t i = len; i > 0; i--) - di[i - 1] = si[i - 1]; - - return dest; - } -} diff --git a/src/oboskrnl/arch/m68k/loader/Util.cpp b/src/oboskrnl/arch/m68k/loader/Util.cpp deleted file mode 100644 index 8f12b849..00000000 --- a/src/oboskrnl/arch/m68k/loader/Util.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include "Util.h" -#include - -#ifdef NPL_ENABLE_LOGGING -#define NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS 1 -#define NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS 1 -#define NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS 0 -#define NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS 0 -#define NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS 0 -#define NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS 0 - -#define NANOPRINTF_IMPLEMENTATION -#include -#endif - -namespace Npl -{ - void Panic(PanicReason r) - { - NPL_LOG("LOADER PANIC: %u", (unsigned)r); - asm("clr %d1; add #0xDEAD, %d1"); - asm("clr %d2; add #0xDEAD, %d2"); - asm("clr %d3; add #0xDEAD, %d3"); - while (true) - asm("stop #0x2700"); - __builtin_unreachable(); - } - - sl::CNativePtr FindBootInfoTag(BootInfoType type, sl::CNativePtr begin) - { - constexpr size_t ReasonableSearchCount = 50; - - if (begin.ptr == nullptr) - begin = sl::AlignUp((uintptr_t)LOADER_BLOB_END, 2); - - for (size_t i = 0; i < ReasonableSearchCount; i++) - { - auto tag = begin.As(); - if (tag->type == BootInfoType::Last) - return nullptr; - if (tag->type == type) - return begin; - begin = begin.Offset(tag->size); - } - - return nullptr; - } - -#ifdef NPL_ENABLE_LOGGING - sl::NativePtr uart = nullptr; - - void UartWrite(int c, void* ignored) - { - (void)ignored; - if (uart.ptr != nullptr) - uart.Write(c); //registers are 32-bits wide - } -#endif -} diff --git a/src/oboskrnl/arch/m68k/loader/Util.h b/src/oboskrnl/arch/m68k/loader/Util.h deleted file mode 100644 index a77e3cf6..00000000 --- a/src/oboskrnl/arch/m68k/loader/Util.h +++ /dev/null @@ -1,73 +0,0 @@ -#pragma once - -#include "NativePtr.h" -#include - -extern "C" -{ - extern char LOADER_BLOB_BEGIN[]; - extern char LOADER_BLOB_END[]; - extern char KERNEL_BLOB_BEGIN[]; - extern char KERNEL_BLOB_END[]; -} - -namespace Npl -{ - constexpr size_t PageSize = 0x1000; - - enum class PanicReason - { - KernelReturned = 1, - InternalAllocFailure = 2, - LoadAllocFailure = 3, - StackCheckFail = 4, - HhdmSetupFail = 5, - BadLbpRevision = 6, - DeleteCalled = 7, - }; - - enum class BootInfoType : uint16_t - { - Last = 0, - MachType = 1, - CpuType = 2, - FpuType = 3, - MmuType = 4, - MemChunk = 5, - InitRd = 6, - CommandLine = 7, - RngSeed = 8, - - QemuVersion = 0x8000, - GoldfishPicBase = 0x8001, - GoldfishRtcBase = 0x8002, - GoldfishTtyBase = 0x8003, - VirtioBase = 0x8004, - ControlBase = 0x8005 - }; - - struct [[gnu::packed]] BootInfoTag - { - BootInfoType type; - uint16_t size; - }; - - struct [[gnu::packed]] BootInfoMemChunk - { - uint32_t addr; - uint32_t size; - }; - - void Panic(PanicReason reason); - sl::CNativePtr FindBootInfoTag(BootInfoType type, sl::CNativePtr begin = nullptr); - -#ifdef NPL_ENABLE_LOGGING - extern sl::NativePtr uart; - - void UartWrite(int c, void* ignored); - - #define NPL_LOG(...) npf_pprintf(UartWrite, nullptr, __VA_ARGS__) -#else - #define NPL_LOG(msg, ...) do {} while(false) -#endif -} diff --git a/src/oboskrnl/arch/m68k/loader/Vector.h b/src/oboskrnl/arch/m68k/loader/Vector.h deleted file mode 100644 index 3eafddd6..00000000 --- a/src/oboskrnl/arch/m68k/loader/Vector.h +++ /dev/null @@ -1,283 +0,0 @@ -#pragma once -/* -This vector implementation is based on frigg's implementation (https://github.com/managarm/frigg). -As required, the license text is below. - -MIT License -Copyright 2014-2021 Frigg Contributors - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*/ - - -#include -#include -#include -#include - -namespace sl -{ - template - class Vector - { - private: - T* elements = nullptr; - size_t size = 0; - size_t capacity = 0; - Allocator alloc; - - public: - friend void Swap(Vector& a, Vector& b) - { - sl::Swap(a.elements, b.elements); - sl::Swap(a.size, b.size); - sl::Swap(a.capacity, b.capacity); - } - - void EnsureCapacity(size_t neededCapacity) - { - if (neededCapacity <= capacity) - return; - - const size_t newCapacity = sl::Max(capacity * 2, neededCapacity); - - T* newElements = (T*)alloc.Allocate(sizeof(T) * newCapacity); - for (size_t i = 0; i < capacity; i++) - new(&newElements[i]) T(sl::Move(elements[i])); - - for (size_t i = 0; i < size; i++) - elements[i].~T(); - if (elements != nullptr) - alloc.Deallocate(elements, sizeof(T) * capacity); - - elements = newElements; - capacity = newCapacity; - } - - constexpr Vector() : elements(nullptr), size(0), capacity(0), alloc() - {} - - Vector(size_t initialCapacity) : Vector() - { EnsureCapacity(initialCapacity); } - - Vector(T* takingBuffer, size_t bufferLength) - { - elements = takingBuffer; - capacity = bufferLength; - size = bufferLength; - } - - ~Vector() - { - Clear(); - if (elements) - alloc.Deallocate(elements, sizeof(T) * capacity); - } - - Vector(const Vector& other) - { - EnsureCapacity(other.size); - for (size_t i = 0; i < other.size; i++) - new(&elements[i]) T(other[i]); - size = other.size; - } - - Vector& operator=(const Vector& other) - { - Clear(); - EnsureCapacity(other.size); - for (size_t i = 0; i < other.size; i++) - new(&elements[i]) T(other[i]); - size = other.size; - return *this; - } - - Vector(Vector&& from) - { - Swap(*this, from); - } - - Vector& operator=(Vector&& from) - { - Swap(*this, from); - return *this; - } - - T& PushBack(const T& elem) - { - EnsureCapacity(size + 1); - - T* latest = new(&elements[size]) T(elem); - size++; - return *latest; - } - - T& PushBack(T&& elem) - { - EnsureCapacity(size + 1); - - T* latest = new(&elements[size]) T(sl::Move(elem)); - size++; - return *latest; - } - - template - T& EmplaceBack(Args&&... args) - { - EnsureCapacity(size + 1); - - T* latest = new(&elements[size]) T(sl::Forward(args)...); - size++; - return *latest; - } - - template - T& EmplaceAt(size_t index, Args&&... args) - { - EnsureCapacity(index + 1); - while (size < index) - { - new(&elements[size]) T(); - size++; - } - - T* latest = new(&elements[index]) T(sl::Forward(args)...); - if (index >= size) - size++; - return *latest; - } - - T PopBack() - { - size--; - T elem = sl::Move(elements[size]); - elements[size].~T(); - return elem; - } - - void Clear() - { - for (size_t i = 0; i < size; i++) - elements[i].~T(); - size = 0; - } - - void Erase(size_t index) - { - if (index >= size) - return; - - Erase(index, 1); - } - - void Erase(size_t index, size_t count) - { - if (count == 0 || index >= size) - return; - - if (count > index) - count -= index; - for (size_t i = index; i < index + count; i++) - At(i).~T(); - for (size_t i = index + count; i < size; i++) - { - new (&elements[i - count]) T(sl::Move(At(i))); - At(i).~T(); - } - - size -= count; - } - - template - T& Emplace(size_t index, Args&&... args) - { - if (index >= size) - return EmplaceAt(index, sl::Forward(args)...); - - EnsureCapacity(size + 1); - for (size_t i = size; i > index; i--) - new (&elements[i]) T(sl::Move(elements[i - 1])); - - elements[index].~T(); - new (&elements[index]) T(sl::Forward(args)...); - size++; - - return elements[index]; - } - - T& Insert(size_t index, const T& elem) - { - return Emplace(index, elem); - } - - [[nodiscard]] - T* DetachData() - { - size = capacity = 0; - elements = nullptr; - } - - T* Data() - { return elements; } - - const T* Data() const - { return elements; } - - size_t Size() const - { return size; } - - size_t Capacity() const - { return capacity; } - - bool Empty() const - { return size == 0; } - - T* Begin() - { return elements; } - - const T* Begin() const - { return elements; } - - T* End() - { return elements + size; } - - const T* End() const - { return elements + size; } - - T& Front() - { return elements[0]; } - - const T& Front() const - { return elements[0]; } - - T& Back() - { return elements[size - 1]; } - - const T& Back() const - { return elements[size - 1]; } - - T& At(size_t index) - { - if (index > size) - index = size; - return elements[index]; - } - - const T& At(size_t index) const - { - if (index > size) - index = size; - return elements[index]; - } - - T& operator[](size_t index) - { return At(index); } - - const T& operator[](size_t index) const - { return At(index); } - }; -} \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/mmu.c b/src/oboskrnl/arch/m68k/mmu.c deleted file mode 100644 index 45291a6f..00000000 --- a/src/oboskrnl/arch/m68k/mmu.c +++ /dev/null @@ -1,333 +0,0 @@ -/* - * oboskrnl/arch/m68k/mmu.c - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include "mm/page_table.h" -#include -#include -#include -#include - -#include -#include - -#include - -#include -#include -#include -#include - -#include -#include - -extern volatile struct limine_kernel_file_request Arch_KernelFile; -extern volatile struct limine_kernel_address_request Arch_KernelAddressRequest; -extern volatile struct limine_hhdm_request Arch_HHDMRequest; - -#define PT_FLAGS_RESIDENT (0b11 << 0) -#define PT_FLAGS_SUPERVISOR (0b1 << 7) -#define PT_FLAGS_U0 (0b1 << 8) /* user-bits */ -#define PT_FLAGS_U1 (0b1 << 9) /* user-bits */ -#define PT_FLAGS_READONLY (0b1 << 2) -#define PT_FLAGS_CACHE_COPYBACK (0b01 << 5) -#define PT_FLAGS_CACHE_WRITETHROUGH (0b00 << 5) -#define PT_FLAGS_CACHE_DISABLE (0b11 << 5) -#define PT_FLAGS_CACHE_DISABLE_SERALIZED (0b10 << 5) -#define PT_FLAGS_USED (1<<3) -#define PT_FLAGS_MODIFIED (1<<4) - -#define MASK_PTE(pte) (((uintptr_t)(pte)) & 0xffffff00) - -OBOS_NO_UBSAN OBOS_NO_KASAN obos_status Arch_MapPage(page_table pt_root, uintptr_t virt, uintptr_t to, uintptr_t ptFlags, bool free_pte) -{ - if (!pt_root) - return OBOS_STATUS_INVALID_ARGUMENT; - bool is_swap_phys = ptFlags & PT_FLAGS_U0; - if (is_swap_phys) - ptFlags &= ~PT_FLAGS_U0; - size_t pte3Index = (virt >> 25) & ((1<<7)-1); - size_t pte2Index = (virt >> 18) & ((1<<7)-1); - size_t pte1Index = (virt >> 12) & ((1<<6)-1); - uintptr_t *pte3 = nullptr, *pte2 = nullptr, *pte1 = nullptr; - pte3 = (uintptr_t*)Arch_MapToHHDM(pt_root); - if (!(pte3[pte3Index] & PT_FLAGS_RESIDENT)) - { - obos_status status = OBOS_STATUS_SUCCESS; - uintptr_t phys = Mm_AllocatePhysicalPages(1, 1, &status); - if (obos_is_error(status)) - return status; - memzero(Arch_MapToHHDM(phys), OBOS_PAGE_SIZE); - pte3[pte3Index] = ((uintptr_t)phys) | ptFlags | PT_FLAGS_RESIDENT; - } - else - { - if (!(ptFlags & PT_FLAGS_READONLY)) - pte3[pte3Index] &= ~PT_FLAGS_READONLY; - if (!(ptFlags & PT_FLAGS_SUPERVISOR)) - pte3[pte3Index] &= ~PT_FLAGS_SUPERVISOR; - } - pte2 = Arch_MapToHHDM(MASK_PTE(pte3[pte3Index])); - if (!((uintptr_t)pte2[pte2Index] & PT_FLAGS_RESIDENT)) - { - obos_status status = OBOS_STATUS_SUCCESS; - uintptr_t phys = Mm_AllocatePhysicalPages(1, 1, &status); - if (obos_is_error(status)) - return status; - memzero(Arch_MapToHHDM(phys), OBOS_PAGE_SIZE); - ((uintptr_t*)pte2)[pte2Index] = phys | ptFlags | PT_FLAGS_RESIDENT; - } - else - { - if (!(ptFlags & PT_FLAGS_READONLY)) - pte2[pte2Index] &= ~PT_FLAGS_READONLY; - if (!(ptFlags & PT_FLAGS_SUPERVISOR)) - pte2[pte2Index] &= ~PT_FLAGS_SUPERVISOR; - } - pte1 = (uintptr_t*)Arch_MapToHHDM(MASK_PTE(((uintptr_t*)pte2)[pte2Index])); - pte1[pte1Index] = to | ptFlags | (is_swap_phys ? PT_FLAGS_U0 : 0); - if (!(ptFlags & PT_FLAGS_RESIDENT) && free_pte) - { - if (memcmp_b(pte1, 0, 256) && free_pte) - { - pte2[pte2Index] = 0; - Mm_FreePhysicalPages((uintptr_t)Arch_UnmapFromHHDM(pte1), 1); - if (memcmp_b(pte2, 0, 1024)) - { - pte3[pte3Index] = 0; - Mm_FreePhysicalPages((uintptr_t)Arch_UnmapFromHHDM(pte2), 1); - // Don't do this since pte3=pt_root, and we shouldn't be freeing that, or bad stuff might happen. - // if (memcmp_b(pte3, 0, 1024)) - // Mm_FreePhysicalPages((uintptr_t)pte3, 1); - } - } - } - pflush(virt); - return OBOS_STATUS_SUCCESS; -} - -OBOS_NO_UBSAN OBOS_NO_KASAN obos_status Arch_UnmapPage(page_table pt_root, uintptr_t virt, bool free_pte) -{ - if (!pt_root) - return OBOS_STATUS_INVALID_ARGUMENT; - size_t pte3Index = (virt >> 25) & ((1<<7)-1); - size_t pte2Index = (virt >> 18) & ((1<<7)-1); - size_t pte1Index = (virt >> 12) & ((1<<6)-1); - uintptr_t *pte3 = nullptr, *pte2 = nullptr, *pte1 = nullptr; - pte3 = (uintptr_t*)Arch_MapToHHDM(pt_root); - if (!(pte3[pte3Index] & PT_FLAGS_RESIDENT)) - return OBOS_STATUS_NOT_FOUND; - pte2 = Arch_MapToHHDM(MASK_PTE(pte3[pte3Index])); - if (!((uintptr_t)pte2[pte2Index] & PT_FLAGS_RESIDENT)) - return OBOS_STATUS_NOT_FOUND; - pte1 = (uintptr_t*)Arch_MapToHHDM(MASK_PTE(((uintptr_t*)pte2)[pte2Index])); - if (!(pte1[pte1Index] & PT_FLAGS_RESIDENT)) - return OBOS_STATUS_NOT_FOUND; - pte1[pte1Index] &= ~BIT(0); - pflush(virt); - if (memcmp_b(pte1, 0, 256) && free_pte) - { - pte2[pte2Index] = 0; - Mm_FreePhysicalPages((uintptr_t)Arch_UnmapFromHHDM(pte1), 1); - if (memcmp_b(pte2, 0, 1024)) - { - pte3[pte3Index] = 0; - Mm_FreePhysicalPages((uintptr_t)Arch_UnmapFromHHDM(pte2), 1); - // Don't do this since pte3=pt_root, and we shouldn't be freeing that, or bad stuff might happen. - // if (memcmp_b(pte3, 0, 1024)) - // Mm_FreePhysicalPages((uintptr_t)pte3, 1); - } - } - return OBOS_STATUS_SUCCESS; -} - -OBOS_NO_UBSAN OBOS_NO_KASAN obos_status Arch_GetPagePTE(page_table pt_root, uintptr_t virt, uint32_t* out) -{ - if (!out) - return OBOS_STATUS_INVALID_ARGUMENT; - *out = 0; - size_t pte3Index = (virt >> 25) & ((1<<7)-1); - size_t pte2Index = (virt >> 18) & ((1<<7)-1); - size_t pte1Index = (virt >> 12) & ((1<<6)-1); - uintptr_t *pte3 = nullptr, *pte2 = nullptr, *pte1 = nullptr; - pte3 = (uintptr_t*)Arch_MapToHHDM(pt_root); - if (!(pte3[pte3Index] & PT_FLAGS_RESIDENT)) - return OBOS_STATUS_NOT_FOUND; - pte2 = Arch_MapToHHDM(MASK_PTE(pte3[pte3Index])); - if (!((uintptr_t)pte2[pte2Index] & PT_FLAGS_RESIDENT)) - return OBOS_STATUS_NOT_FOUND; - pte1 = (uintptr_t*)Arch_MapToHHDM(MASK_PTE(((uintptr_t*)pte2)[pte2Index])); - // if (!(pte1[pte1Index] & PT_FLAGS_RESIDENT)) - // return OBOS_STATUS_NOT_FOUND; - *out = pte1[pte1Index]; - return OBOS_STATUS_SUCCESS; -} -OBOS_NO_UBSAN OBOS_NO_KASAN obos_status OBOSS_GetPagePhysicalAddress(void* virt_, uintptr_t* oPhys) -{ - if (!oPhys) - return OBOS_STATUS_INVALID_ARGUMENT; - uintptr_t virt = (uintptr_t)virt_; - uintptr_t pt_root = 0; - asm("movec.l %%srp, %0" :"=r"(pt_root) :); - uintptr_t entry = 0; - obos_status status = Arch_GetPagePTE(pt_root, virt, &entry); - *oPhys = MASK_PTE(entry); - return status; -} -OBOS_NO_UBSAN OBOS_NO_KASAN obos_status OBOSS_MapPage_RW_XD(void* at_, uintptr_t phys) -{ - page_table cur = 0; - asm("movec.l %%srp, %0" :"=a"(cur) :); - return Arch_MapPage(cur, (uintptr_t)at_, phys, PT_FLAGS_SUPERVISOR|PT_FLAGS_RESIDENT|PT_FLAGS_CACHE_COPYBACK, false); -} -OBOS_NO_UBSAN OBOS_NO_KASAN obos_status OBOSS_UnmapPage(void* at) -{ - page_table cur = 0; - asm("movec.l %%srp, %0" :"=a"(cur) :); - return Arch_UnmapPage(cur, (uintptr_t)at, true); -} -static basicmm_region kernel_region; -static basicmm_region hhdm_region; -OBOS_NO_UBSAN OBOS_NO_KASAN void Arch_InitializePageTables() -{ - page_table newPt = Mm_AllocatePhysicalPages(1, 1, nullptr); - memzero(Arch_MapToHHDM(newPt), 4096); - page_table oldPt = 0; - asm("movec.l %%srp, %0" :"=a"(oldPt) :); - // Map the HHDM. - uintptr_t hhdm_base = Arch_HHDMRequest.response->offset; - for (uintptr_t addr = 0; addr < Mm_PhysicalMemoryBoundaries; addr += OBOS_PAGE_SIZE) - Arch_MapPage(newPt, hhdm_base+addr, addr, PT_FLAGS_RESIDENT|PT_FLAGS_CACHE_COPYBACK|PT_FLAGS_SUPERVISOR, false); - // Map the kernel. - Elf_Ehdr* ehdr = (Elf_Ehdr*)Arch_KernelFile.response->kernel_file->address; - Elf_Phdr* phdrs = Arch_KernelFile.response->kernel_file->address + ehdr->e_phoff; - // NOTE(oberrow): If the kernel shits it self, look here first. - size_t kernelSize = 0; - uintptr_t top = 0; - for (size_t i = 0; i < ehdr->e_phnum; i++) - { - Elf_Phdr* phdr = &phdrs[i]; - if (phdr->p_type != PT_LOAD || !phdr->p_vaddr) - continue; - if (phdr->p_vaddr < Arch_KernelAddressRequest.response->virtual_base) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "Bootloader made a whoopsie!\n"); - uintptr_t ptFlags = PT_FLAGS_RESIDENT; - if ((phdr->p_flags & PF_R) && !(phdr->p_flags & PF_W)) - ptFlags |= PT_FLAGS_READONLY; - ptFlags |= PT_FLAGS_CACHE_COPYBACK; - ptFlags |= PT_FLAGS_SUPERVISOR; - uintptr_t phys = 0; - for (uintptr_t addr = phdr->p_vaddr & ~0xfff; addr < ((phdr->p_vaddr & ~0xfff) + ((phdr->p_memsz + 0xfff) & ~0xfff)); addr += 4096) - { - Arch_GetPagePTE(oldPt, addr, &phys); - phys = MASK_PTE(phys); - Arch_MapPage(newPt, addr, phys, ptFlags, false); - top = addr + 0x1000; - } - } - kernelSize = top-Arch_KernelAddressRequest.response->virtual_base; - asm volatile ("movec %0, %%srp" : :"r"(newPt)); - Mm_FreePhysicalPages(oldPt, 1); - OBOSH_BasicMMAddRegion(&kernel_region, (void*)(uintptr_t)Arch_KernelAddressRequest.response->virtual_base, kernelSize); - OBOSH_BasicMMAddRegion(&hhdm_region, (void*)Arch_MapToHHDM(0), Mm_PhysicalMemoryBoundaries); -} -OBOS_NO_UBSAN OBOS_NO_KASAN obos_status MmS_QueryPageInfo(page_table pt, uintptr_t addr, page_info* ppage, uintptr_t* phys) -{ - if (!pt) - return OBOS_STATUS_INVALID_ARGUMENT; - if (!ppage && !phys) - return OBOS_STATUS_SUCCESS; - addr &= ~0xfff; - uintptr_t entry = 0; - obos_status status = Arch_GetPagePTE(pt, addr, &entry); - page_info page; - memzero(&page, sizeof(page)); - if (obos_is_error(status) && status != OBOS_STATUS_NOT_FOUND) - { - if (ppage) - { - memcpy(&ppage->prot, &page.prot, sizeof(page.prot)); - ppage->virt = page.virt; - ppage->phys = MASK_PTE(entry); - } - if (phys) - *phys = MASK_PTE(entry); - return status; - } - page.virt = addr; - page.prot.present = entry & PT_FLAGS_RESIDENT; - page.prot.is_swap_phys = entry & PT_FLAGS_U0; - page.prot.huge_page = false; - page.prot.rw = !(entry & PT_FLAGS_READONLY); - page.prot.executable = true; - page.accessed = entry & PT_FLAGS_USED; - page.dirty = entry & PT_FLAGS_MODIFIED; - page.prot.user = !(entry & PT_FLAGS_SUPERVISOR); - page.prot.uc = ((entry >> 5) & 0b11) == (PT_FLAGS_CACHE_DISABLE >> 5); - if (page.accessed || page.dirty) - { - // Unset the bit(s). - // NOTE(oberrow): I just realized I forgot to do this on x86-64 - // Oops - entry &= ~(PT_FLAGS_USED | PT_FLAGS_MODIFIED); - Arch_MapPage(pt, addr, MASK_PTE(entry), entry & ~0xffffff00, false); - } - if (page.prot.present) - OBOS_ENSURE(MASK_PTE(entry) != 0); - else if (!entry) - page.prot.user = false; - if (phys) - *phys = MASK_PTE(entry); - if (ppage) - { - memcpy(&ppage->prot, &page.prot, sizeof(page.prot)); - ppage->phys = MASK_PTE(entry); - ppage->virt = page.virt; - } - return OBOS_STATUS_SUCCESS; -} -OBOS_NO_UBSAN OBOS_NO_KASAN obos_status MmS_SetPageMapping(page_table pt, const page_info* page, uintptr_t phys, bool free_pte) -{ - if (!pt || !page) - return OBOS_STATUS_INVALID_ARGUMENT; - // if (!page->prot.present) - // return Arch_UnmapPage(pt, page->virt, free_pte); - uintptr_t flags = 0; - if (!page->prot.rw) - flags |= PT_FLAGS_READONLY; - if (!page->prot.user) - flags |= PT_FLAGS_SUPERVISOR; - if (page->prot.present) - flags |= PT_FLAGS_RESIDENT; - if (!page->prot.uc) - flags |= PT_FLAGS_CACHE_COPYBACK; - else - flags |= PT_FLAGS_CACHE_DISABLE; - if (page->prot.is_swap_phys) - flags |= PT_FLAGS_U0; - return !page->prot.huge_page ? - Arch_MapPage(pt, (page->virt & ~0xfff), phys, flags, free_pte) : - OBOS_STATUS_UNIMPLEMENTED; -} - -OBOS_NO_UBSAN OBOS_NO_KASAN page_table MmS_GetCurrentPageTable() -{ - page_table pt; - asm ("movec.l %%srp, %0" :"=r"(pt) :); - return pt; -} - -page_table MmS_AllocatePageTable() -{ - page_table pt = Mm_AllocatePhysicalPages(1, 1, nullptr); - memzero(Arch_MapToHHDM(pt), OBOS_PAGE_SIZE); - return pt; -} - -void MmS_FreePageTable(page_table pt) -{ - // TODO: Free the hierarchy? - Mm_FreePhysicalPages(pt, 1); -} \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/pmm.c b/src/oboskrnl/arch/m68k/pmm.c deleted file mode 100644 index 12598897..00000000 --- a/src/oboskrnl/arch/m68k/pmm.c +++ /dev/null @@ -1,55 +0,0 @@ -/* - oboskrnl/arch/x86_64/pmm.c - - Copyright (c) 2024 Omar Berrow -*/ - -#include -#include -#include -#include - -#include - -#include -#include - -extern volatile struct limine_memmap_request Arch_MemmapRequest; -extern volatile struct limine_hhdm_request Arch_HHDMRequest; -extern volatile struct limine_memmap_request Arch_MemmapRequest; -extern volatile struct limine_hhdm_request Arch_HHDMRequest; -obos_pmem_map_entry* MmS_GetFirstPMemMapEntry(uintptr_t* index) -{ - *index = 0; - return Arch_MemmapRequest.response->entries[0]; -} -// returns nullptr at the end of the list. -obos_pmem_map_entry* MmS_GetNextPMemMapEntry(obos_pmem_map_entry* current, uintptr_t* index) -{ - OBOS_UNUSED(current); - size_t nEntries = (Arch_MemmapRequest.response->entry_count); - if (!nEntries) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "No memory map entries.\n"); - if ((*index) > nEntries) - return nullptr; - return Arch_MemmapRequest.response->entries[++(*index)]; -} -#define MAP_TO_HHDM(addr, type) ((type*)((uintptr_t)Arch_HHDMRequest.response->offset + (uintptr_t)(addr))) -#define UNMAP_FROM_HHDM(addr) ((uintptr_t)(addr) - (uintptr_t)Arch_HHDMRequest.response->offset) - -OBOS_NO_UBSAN OBOS_NO_KASAN void* Arch_MapToHHDM(uintptr_t phys) -{ - return MAP_TO_HHDM(phys, void); -} -OBOS_NO_UBSAN OBOS_NO_KASAN uintptr_t Arch_UnmapFromHHDM(void* virt) -{ - return UNMAP_FROM_HHDM(virt); -} -void* MmS_MapVirtFromPhys(uintptr_t addr) -{ - return Arch_MapToHHDM(addr); -} -uintptr_t MmS_UnmapVirtFromPhys(void* virt) -{ - return Arch_UnmapFromHHDM(virt); -} \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/pmm.h b/src/oboskrnl/arch/m68k/pmm.h deleted file mode 100644 index 5bea3a4f..00000000 --- a/src/oboskrnl/arch/m68k/pmm.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - oboskrnl/arch/x86_64/pmm.h - - Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include - -typedef struct limine_memmap_entry obos_pmem_map_entry; -#define pmem_map_base base -#define pmem_map_size length -#define pmem_map_type type -#define PHYSICAL_MEMORY_TYPE_USABLE LIMINE_MEMMAP_USABLE -#define PHYSICAL_MEMORY_TYPE_RECLAIMABLE LIMINE_MEMMAP_ACPI_RECLAIMABLE -#define PHYSICAL_MEMORY_TYPE_LOADER_RECLAIMABLE LIMINE_MEMMAP_LOADER_RECLAIMABLE - -void* Arch_MapToHHDM(uintptr_t phys); -uintptr_t Arch_UnmapFromHHDM(void* virt); \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/syscall.c b/src/oboskrnl/arch/m68k/syscall.c deleted file mode 100644 index 7b3738d3..00000000 --- a/src/oboskrnl/arch/m68k/syscall.c +++ /dev/null @@ -1,354 +0,0 @@ -/* - * oboskrnl/arch/m68k/syscall.c - * - * Copyright (c) 2025-2026 Omar Berrow -*/ - -#include -#include -#include -#include -#include -#include - -#include - -#include - -#include -#include -#include -#include -#include - -#include - -const char *syscall_to_string[] = { - "Core_ExitCurrentThread/Sys_SetTCB", - "Core_Yield/Sys_GetTCB", - "OBOS_Reboot/Sys_ThreadContextCreateFork", - "OBOS_Shutdown", - "Sys_HandleClose", - "Sys_HandleClone", - "Sys_ThreadContextCreate", // 6 - "OBOS_Suspend", - "Sys_ThreadOpen", - "Sys_ThreadCreate", - "Sys_ThreadReady", - "Sys_ThreadBlock", - "Sys_ThreadBoostPriority", - "Sys_ThreadPriority", - "Sys_ThreadAffinity", - "Sys_ThreadSetOwner", - "Sys_ThreadGetTid", // 16 - "Sys_WaitOnObject", - "Sys_Fcntl", - "Sys_ProcessOpen", // Unimplemented - "Sys_ProcessStart", - "Sys_KillProcess", // signal-related - "Sys_VirtualMemoryAlloc", // 22 - "Sys_VirtualMemoryFree", - "Sys_VirtualMemoryProtect", - "Sys_VirtualMemoryLock", // Unimplemented - "Sys_VirtualMemoryUnlock", // Unimplemented - "Sys_MakeNewContext", - "Sys_ContextExpandWSCapacity", - "Sys_ContextGetStat", - "Sys_GetUsedPhysicalMemoryCount", - "Sys_QueryPageInfo", - "Sys_FutexWake", // 32 - "Sys_FutexWait", - "Sys_FdAlloc", // 34 - "Sys_FdOpen", - "Sys_FdOpenDirent", - "Sys_FdWrite", - "Sys_IRPCreate", - "Sys_FdRead", - "Sys_IRPWait", - "Sys_FdSeek", - "Sys_FdTellOff", - "Sys_FdEOF", - "Sys_FdIoctl", - "Sys_FdFlush", // 45 - "OBOS_PartProbeAllDrives", - "Sys_PartProbeDrive", // 47 - "OBOSS_SigReturn", - "Sys_Kill", - "Sys_SigAction", - "OBOS_SigSuspend", - "Sys_SigProcMask", - "Sys_SigAltStack", // 53 - "Sys_OpenDir", - "Sys_ReadEntries", // 55 - "Sys_ExecVE", // 56 - "Sys_LibCLog", // 57 - "Sys_ProcessGetPID", // 58 - "Sys_ProcessGetPPID", // 59 - "Sys_FdOpenAt", - "Sys_MmFork", - "Sys_ExitCurrentProcess", - "Sys_ProcessGetStatus", - "Sys_WaitProcess", - "Sys_Stat", - "Sys_StatFSInfo", - "Sys_SysConf", - "Sys_SetKLogLevel", - "Sys_LoadDriver", - "Sys_StartDriver", - "Sys_UnloadDriver", - "Sys_PnpLoadDriversAt", - "Sys_FindDriverByName", - "Sys_EnumerateLoadedDrivers", - "Sys_QueryDriverName", - "Sys_Sync", - "Sys_SleepMS", - "Sys_Mount", - "Sys_Unmount", - "Sys_FdCreat", - "Sys_FdOpenEx", - "Sys_FdOpenAtEx", - "Sys_Mkdir", - "Sys_MkdirAt", - "Sys_Chdir", - "Sys_ChdirEnt", - "Sys_GetCWD", - "Sys_SetControllingTTY", - "Sys_GetControllingTTY", - "Sys_TTYName", - "Sys_IsATTY", - "Sys_IRPWait", - "Sys_IRPQueryState", - "Sys_IRPGetBuffer", - "Sys_IRPGetStatus", - "Sys_CreatePipe", - "Sys_PSelect", - "Sys_ReadLinkAt", - "Sys_SetUid", - "Sys_SetGid", - "Sys_GetUid", - "Sys_GetGid", - "Sys_UnlinkAt", - "Sys_MakeDiskSwap", - "Sys_SwitchSwap", - "Sys_SyncAnonPages", - "Sys_FdPWrite", - "Sys_FdPRead", - "Sys_SymLink", - "Sys_SymLinkAt", - "Sys_CreateNamedPipe", - "Sys_PPoll", - "Sys_Socket", - "Sys_SendTo", - "Sys_RecvFrom", - "Sys_Listen", - "Sys_Accept", - "Sys_Bind", - "Sys_Connect", - "Sys_SockName", - "Sys_PeerName", - "Sys_GetSockOpt", - "Sys_SetSockOpt", - "Sys_ShutdownSocket", - "Sys_GetHostname", - "Sys_SetHostname", - [127]="Sys_KillProcessGroup", - "Sys_SetProcessGroup", - "Sys_GetProcessGroup", - "Sys_LinkAt", - "Sys_FChmodAt", - "Sys_FChownAt", - "Sys_UMask", - "Sys_RenameAt", - "Sys_UTimeNSAt", - "Sys_ThreadGetStack", - "Sys_SetRESUid", - "Sys_SetRESGid", - "Sys_GetRESUid", - "Sys_GetRESGid", - "Sys_SigPending", - "Sys_SetGroups", - "Sys_GetGroups", - "Sys_GetCachedByteCount", - "Sys_GetHDADevices", - "Sys_SetSid", - "Sys_GetSid", - "Sys_Chroot", - "Sys_OpenTmpFS", - "Sys_Mount2", -}; - -const char* status_to_string[] = { - "OBOS_STATUS_SUCCESS", - "OBOS_STATUS_INVALID_IRQL", - "OBOS_STATUS_INVALID_ARGUMENT", - "OBOS_STATUS_UNIMPLEMENTED", - "OBOS_STATUS_INVALID_INIT_PHASE", - "OBOS_STATUS_INVALID_AFFINITY", - "OBOS_STATUS_NOT_ENOUGH_MEMORY", - "OBOS_STATUS_MISMATCH", - "OBOS_STATUS_INTERNAL_ERROR", - "OBOS_STATUS_RETRY", - "OBOS_STATUS_ALREADY_INITIALIZED", - "OBOS_STATUS_NOT_FOUND", - "OBOS_STATUS_IN_USE", - "OBOS_STATUS_ACCESS_DENIED", - "OBOS_STATUS_UNINITIALIZED", - "OBOS_STATUS_UNHANDLED", - "OBOS_STATUS_UNPAGED_POOL", - "OBOS_STATUS_INVALID_FILE", - "OBOS_STATUS_INVALID_HEADER", - "OBOS_STATUS_DRIVER_REFERENCED_UNRESOLVED_SYMBOL", - "OBOS_STATUS_DRIVER_SYMBOL_MISMATCH", - "OBOS_STATUS_NO_ENTRY_POINT", - "OBOS_STATUS_INVALID_IOCTL", - "OBOS_STATUS_INVALID_OPERATION", - "OBOS_STATUS_DPC_ALREADY_ENQUEUED", - "OBOS_STATUS_RECURSIVE_LOCK", - "OBOS_STATUS_READ_ONLY", - "OBOS_STATUS_NOT_A_FILE", - "OBOS_STATUS_ALREADY_MOUNTED", - "OBOS_STATUS_EOF", - "OBOS_STATUS_ABORTED", - "OBOS_STATUS_PAGE_FAULT", - "OBOS_STATUS_TIMED_OUT", - "OBOS_STATUS_PIPE_CLOSED", - "OBOS_STATUS_NO_SPACE", - "OBOS_STATUS_NO_SYSCALL", - "OBOS_STATUS_WAKE_INCAPABLE", - "OBOS_STATUS_INVALID_ELF_TYPE", - "OBOS_STATUS_WOULD_BLOCK", - "OBOS_STATUS_NOT_A_TTY", - "OBOS_STATUS_IRP_RETRY", - "OBOS_STATUS_PORT_IN_USE", - "OBOS_STATUS_ADDRESS_IN_USE", - "OBOS_STATUS_ADDRESS_NOT_AVAILABLE", - "OBOS_STATUS_NO_ROUTE_TO_HOST", - "OBOS_STATUS_MESSAGE_TOO_BIG", - "OBOS_STATUS_CONNECTION_REFUSED", - "OBOS_STATUS_SEEK_UNALLOWED", - "OBOS_STATUS_NETWORK_UNREACHABLE", -}; - -void Arch_RawRegisterInterrupt(uint8_t vec, uintptr_t f); - -void Sys_SetTCB(void* tcb) -{ - Core_GetCurrentThread()->context.tcb = tcb; -} -void *Sys_GetTCB() -{ - return Core_GetCurrentThread()->context.tcb; -} -handle Sys_ThreadContextCreateFork(uintptr_t entry, uintptr_t stack_pointer, handle vmm_context); - -uintptr_t OBOS_ArchSyscallTable[] = { - (uintptr_t)Sys_SetTCB, - (uintptr_t)Sys_GetTCB, - (uintptr_t)Sys_ThreadContextCreateFork, -}; - -void Arch_LogSyscall(uintptr_t p1, uintptr_t p2, uintptr_t p3, uintptr_t p4, uintptr_t p5, uint32_t sysnum); -void Arch_LogSyscallRet(uint32_t ret, uint32_t sysnum); - -handle Sys_ThreadContextCreateFork(uintptr_t entry, uintptr_t stack_pointer, handle vmm_context) -{ - context* vmm_ctx = - HANDLE_TYPE(vmm_context) == HANDLE_TYPE_CURRENT ? - Core_GetCurrentThread()->proc->ctx : nullptr; - if (!vmm_ctx) - { - OBOS_LockHandleTable(OBOS_CurrentHandleTable()); - obos_status status = OBOS_STATUS_SUCCESS; - handle_desc* vmm_ctx_desc = OBOS_HandleLookup(OBOS_CurrentHandleTable(), vmm_context, HANDLE_TYPE_VMM_CONTEXT, false, &status); - OBOS_UNUSED(status); - if (!vmm_ctx_desc) - { - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - return HANDLE_INVALID; - } - vmm_ctx = vmm_ctx_desc->un.vmm_context; - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - } - - handle_desc* desc = nullptr; - OBOS_LockHandleTable(OBOS_CurrentHandleTable()); - handle hnd = OBOS_HandleAllocate(OBOS_CurrentHandleTable(), HANDLE_TYPE_THREAD_CTX, &desc); - thread_ctx_handle *ctx = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(thread_ctx_handle), nullptr); - desc->un.thread_ctx = ctx; - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - ctx->ctx = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(thread_ctx), nullptr); - ctx->canFree = true; - ctx->lock = PUSHLOCK_INITIALIZE(); - - ctx->ctx->urp = vmm_ctx->pt; - ctx->ctx->pc = entry; - ctx->ctx->usp = stack_pointer; - ctx->ctx->sr = 0; - ctx->ctx->tcb = Core_GetCurrentThread()->context.tcb; - ctx->vmm_ctx = vmm_ctx; - ctx->ctx->stackBase = Core_GetCurrentThread()->context.stackBase; - ctx->ctx->stackSize = Core_GetCurrentThread()->context.stackSize; - - return hnd; -} - -// parameters 1-5 are in d0-d4, respectively -// syscall number is in d5 -// return value is in d0 -void Arch_SyscallTrapHandler(interrupt_frame* frame) -{ - uint32_t syscall_number = frame->d5; - uintptr_t* table = IS_ARCH_SYSCALL(syscall_number) ? OBOS_ArchSyscallTable : OBOS_SyscallTable; - if (IS_ARCH_SYSCALL(syscall_number)) - syscall_number -= ARCH_SYSCALL_BEGIN; - if (frame->d5 != (ARCH_SYSCALL_BEGIN+1) /* Sys_GetTCB */) - Arch_LogSyscall(frame->d0, frame->d1, frame->d2, frame->d3, frame->d4, syscall_number); - if (syscall_number >= SYSCALL_END) - { - frame->a1 = OBOS_STATUS_UNIMPLEMENTED; - return; - } - uintptr_t(*hnd)(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4, uint32_t p5) = (void*)table[syscall_number]; - if (hnd) - frame->d0 = hnd(frame->d0, frame->d1, frame->d2, frame->d3, frame->d4); - else - frame->d0 = OBOS_STATUS_UNIMPLEMENTED; - if (frame->d5 != (ARCH_SYSCALL_BEGIN+1) /* Sys_GetTCB */) - Arch_LogSyscallRet(frame->d0, syscall_number); -} - -void OBOSS_InitializeSyscallInterface() -{ - Arch_RawRegisterInterrupt(32 /* trap 0 */, (uintptr_t)Arch_SyscallTrapHandler); -} - -void Arch_LogSyscall(uintptr_t d0, uintptr_t d1, uintptr_t d2, uintptr_t d3, uintptr_t d4, uint32_t sysnum) -{ - if (sysnum >= sizeof(syscall_to_string)/sizeof(const char*) || !syscall_to_string[sysnum]) - { - OBOS_Warning("(thread %d, process %d) invalid syscall %d(0x%p, 0x%p, 0x%p, 0x%p, 0x%p)\n", (uint32_t)Core_GetCurrentThread()->tid, Core_GetCurrentThread()->proc->pid, sysnum, d0,d1,d2,d3,d4); - return; - } - OBOS_Debug("(thread %d, process %d) syscall %s(0x%p, 0x%p, 0x%p, 0x%p, 0x%p)\n", (uint32_t)Core_GetCurrentThread()->tid, Core_GetCurrentThread()->proc->pid, syscall_to_string[sysnum], d0,d1,d2,d3,d4); -} -void Arch_LogSyscallRet(uint32_t ret, uint32_t sysnum) -{ - OBOS_UNUSED(ret); - OBOS_UNUSED(sysnum); - if (sysnum >= sizeof(syscall_to_string)/sizeof(const char*) || !syscall_to_string[sysnum]) - return; - static bool cached_opt = false, opt = false; - if (!cached_opt) - { - opt = OBOS_GetOPTF("disable-syscall-error-log"); - cached_opt = true; - } - if (opt || - (ret == 0 || sysnum == 22 || sysnum == 42 || sysnum == 58 || sysnum == 34 || sysnum == 0 - || sysnum == 20 || sysnum == 59 || sysnum == 61 || sysnum == 9 || sysnum == 1 || sysnum == 19 - || sysnum == 2 || sysnum == 6 || sysnum == 16 || (sysnum == 91 || ret == OBOS_STATUS_NOT_A_TTY))) - OBOS_Debug("(thread %d, process %d) syscall %s returned 0x%x (%s)\n", (uint32_t)Core_GetCurrentThread()->tid, Core_GetCurrentThread()->proc->pid, syscall_to_string[sysnum], ret, (ret < sizeof(status_to_string)/sizeof(status_to_string[0])) ? status_to_string[ret] : "no status string"); - else - OBOS_Log("(thread %d, process %d) syscall %s returned 0x%x (%s)\n", (uint32_t)Core_GetCurrentThread()->tid, Core_GetCurrentThread()->proc->pid, syscall_to_string[sysnum], ret, (ret < sizeof(status_to_string)/sizeof(status_to_string[0])) ? status_to_string[ret] : "no status string"); - -} \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/thread_ctx.S b/src/oboskrnl/arch/m68k/thread_ctx.S deleted file mode 100644 index 42d305b1..00000000 --- a/src/oboskrnl/arch/m68k/thread_ctx.S +++ /dev/null @@ -1,112 +0,0 @@ -/* - * oboskrnl/arch/m68k/thread_ctx.S - * - * Copyright (c) 2024 Omar Berrow -*/ - -.section .bss -// top of stack -.skip 0x10000 -Arch_TempStack: -// bottom of stack -.section .text - -.global CoreS_SwitchToThreadContext -.global CoreS_SaveRegisterContextAndYield -.global CoreS_CallFunctionOnStack -.extern Core_GetIRQLVar -.extern CoreS_SetIRQL - -CoreS_SwitchToThreadContext: - or #0x2700, %sr // Disable all IRQs - - jsr Core_GetIRQLVar - sub.l %d2, %d2 - move.b (%d0), %d2 - move.l (+4,%sp), %a0 - move.b (+0x52,%a0), (%d0) - - move.l (+4,%sp), %a0 - move.l %d2, %sp@- - sub.l %d1, %d1 - move.b (+0x4e,%a0), %d1 - move.l %d1, %sp@- - jsr CoreS_SetIRQL - add #8, %sp - - move.l (+4,%sp), %a0 - - move.l %a0@+, %d1 - movec %d1, %urp - - move.l %a0@+, %a1 - move.l %a1, %sp - - move.l %a0@+, %a1 - move.l %a1, %usp - - movem.l %a0@+, %d0-%d7/%a0-%a6 - - add #2, %a0 - - move.w #0x20, %sp@- - move.w %a0@+, %d0 - move.l %a0@+, %d1 - move.l %d1, %sp@- - move.w %d0, %sp@- - rte -.extern Core_Schedule -CoreS_SaveRegisterContextAndYield: - move.l %sp@+, %d1 // save the return address into d1 - move.l %d1, %sp@- - move.l (+4,%sp), %a0 - // Save URP - movec %urp, %d0 - move.l %d0, %a0@+ - // Save SP - move.l %sp, %a1 - move.l %a1, %a0@+ - move.l %usp, %a1 - move.l %a1, %a0@+ - // movem.l %d0-%d7/%a0-%a6, %a0@+ - // Save GPRs - move.l %d0, %a0@+ - move.l %d1, %a0@+ - move.l %d2, %a0@+ - move.l %d3, %a0@+ - move.l %d4, %a0@+ - move.l %d5, %a0@+ - move.l %d6, %a0@+ - move.l %d7, %a0@+ - move.l %a0, %a0@+ - move.l %a1, %a0@+ - move.l %a2, %a0@+ - move.l %a3, %a0@+ - move.l %a4, %a0@+ - move.l %a5, %a0@+ - move.l %a6, %a0@+ - // Padding - move.w #0, %a0@+ - // Save SR - move.w %sr, %a0@+ - // Save PC - move.l %d1, %a0@+ - jsr Core_Schedule -// should be noreturn, but return just in case - rts -CoreS_CallFunctionOnStack: -// uintptr_t(*func)(uintptr_t), uintptr_t userdata - move.l %d2, %sp@- - move.l %sp, %d2 // save sp into d2 - move.l #Arch_TempStack, %sp - - move.l (8, %d2), %a0 - move.l (12, %d2), %d0 - move.l %d0, %sp@- - jsr (%a0) - - move.l %d2, %sp - add #4, %sp - rts - -.section .note.GNU-stack,"",%progbits diff --git a/src/oboskrnl/arch/m68k/thread_ctx.c b/src/oboskrnl/arch/m68k/thread_ctx.c deleted file mode 100644 index ebf3795f..00000000 --- a/src/oboskrnl/arch/m68k/thread_ctx.c +++ /dev/null @@ -1,94 +0,0 @@ -/* - * oboskrnl/arch/m68k/thread_ctx.c - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include -#include -#include - -#include -#include - -// Thread context manipulation functions. - -void CoreS_SetThreadIRQL(thread_ctx* ctx, irql newIRQL) -{ - ctx->irql = newIRQL; -} -irql CoreS_GetThreadIRQL(const thread_ctx* ctx) -{ - return ctx->irql; -} -void* CoreS_GetThreadStack(const thread_ctx* ctx) -{ - return ctx->stackBase; -} -size_t CoreS_GetThreadStackSize(const thread_ctx* ctx) -{ - return ctx->stackSize; -} -obos_status CoreS_SetupThreadContext(thread_ctx* ctx, uintptr_t entry, uintptr_t arg1, bool makeUserMode, void* stackBase, size_t stackSize) -{ - if (!ctx) - return OBOS_STATUS_INVALID_ARGUMENT; - ctx->pc = entry; - if (!makeUserMode) - { - ctx->sr |= (1<<13); - ctx->sp = (uintptr_t)stackBase+stackSize; - ctx->sp -= 4; - *(uintptr_t*)ctx->sp = arg1; - ctx->sp -= 4; - *(uintptr_t*)ctx->sp = 0; - } - else - { - ctx->usp = (uintptr_t)stackBase+stackSize; - uint32_t stack_frame[2] = { - 0, - arg1, - }; - ctx->usp -= 8; - memcpy_k_to_usr((void*)ctx->usp, stack_frame, sizeof(stack_frame)); - // ctx->usp -= 4; - // *(uintptr_t*)ctx->usp = arg1; - // ctx->usp -= 4; - // *(uintptr_t*)ctx->usp = 0; - } - ctx->stackBase = stackBase; - ctx->stackSize = stackSize; - return OBOS_STATUS_SUCCESS; -} -obos_status CoreS_FreeThreadContext(thread_ctx* ctx) -{ - OBOS_UNUSED(ctx); - return OBOS_STATUS_SUCCESS; -} -void CoreS_SetThreadPageTable(thread_ctx* ctx, page_table pt) -{ - if (!pt || !ctx) - return; - ctx->urp = pt; -} - -void CoreS_SetKernelStack(void* stck) -{ - // TODO: Set kernel stack - OBOS_UNUSED(stck); -} - -void* CoreS_ThreadAlloca(const thread_ctx* ctx_, size_t size, obos_status *status) -{ - thread_ctx* ctx = (void*)ctx_; - if (!ctx) - { - if (status) *status = OBOS_STATUS_INVALID_ARGUMENT; - return nullptr; - } - ctx->usp -= size; - uintptr_t ret = ctx->usp; - if (status) *status = OBOS_STATUS_SUCCESS; - return (void*)ret; -} \ No newline at end of file diff --git a/src/oboskrnl/arch/m68k/thread_ctx.h b/src/oboskrnl/arch/m68k/thread_ctx.h deleted file mode 100644 index 41bc8046..00000000 --- a/src/oboskrnl/arch/m68k/thread_ctx.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * oboskrnl/arch/m68k/thread_ctx.asm - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include - -#include - -struct thread_context_info -{ - uintptr_t urp; - uintptr_t sp; - uintptr_t usp; - uintptr_t d0,d1,d2,d3,d4,d5,d6,d7; - uintptr_t a0,a1,a2,a3,a4,a5,a6; - uint16_t padding; - uint16_t sr; - uintptr_t pc; - uint16_t unused; - irql irql; - void* stackBase; - size_t stackSize; - void* tcb; -} OBOS_ALIGN(4); \ No newline at end of file diff --git a/src/oboskrnl/arch/sched_timer.h b/src/oboskrnl/arch/sched_timer.h new file mode 100644 index 00000000..c6c17bca --- /dev/null +++ b/src/oboskrnl/arch/sched_timer.h @@ -0,0 +1,5 @@ +#pragma once + +#if defined(__x86_64__) || defined(_WIN64) +#include +#endif \ No newline at end of file diff --git a/src/oboskrnl/arch/smp_cpu_func.h b/src/oboskrnl/arch/smp_cpu_func.h new file mode 100644 index 00000000..61a7faa4 --- /dev/null +++ b/src/oboskrnl/arch/smp_cpu_func.h @@ -0,0 +1,5 @@ +#pragma once + +#if defined(__x86_64__) || defined(_WIN64) +#include +#endif \ No newline at end of file diff --git a/src/oboskrnl/arch/smp_cpu_local.h b/src/oboskrnl/arch/smp_cpu_local.h new file mode 100644 index 00000000..532680ec --- /dev/null +++ b/src/oboskrnl/arch/smp_cpu_local.h @@ -0,0 +1,5 @@ +#pragma once + +#if defined(__x86_64__) || defined(_WIN64) +#include +#endif \ No newline at end of file diff --git a/src/oboskrnl/arch/thr_context_info.h b/src/oboskrnl/arch/thr_context_info.h new file mode 100644 index 00000000..c6c546a2 --- /dev/null +++ b/src/oboskrnl/arch/thr_context_info.h @@ -0,0 +1,15 @@ +#pragma once + +// This must define (at least) one structure: obos::arch::ThreadContextInfo. +// This structure must contain at least a member called 'irql' that it uint8_t. +// It must contain any information to restore the state of a thread after it gets preempted. +// It must also define a function called obos::arch::SwitchToThrContext() that takes in a pointer to that structure, and switches to that context. +// This function must not return. +// The function is free to modify this structure. +// Another function it should define is obos::arch::SetupThreadContext(). +// Look in the x86_64 implementation for parameters and return value. +// For more information on how to correctly implement header, look at the x86_64 implementation. + +#if defined(__x86_64__) +#include +#endif \ No newline at end of file diff --git a/src/oboskrnl/arch/vmm_context.h b/src/oboskrnl/arch/vmm_context.h new file mode 100644 index 00000000..3d0f81de --- /dev/null +++ b/src/oboskrnl/arch/vmm_context.h @@ -0,0 +1,5 @@ +#pragma once + +#if defined(__x86_64__) || defined(_WIN64) +#include +#endif \ No newline at end of file diff --git a/src/oboskrnl/arch/vmm_defines.h b/src/oboskrnl/arch/vmm_defines.h new file mode 100644 index 00000000..3947e0df --- /dev/null +++ b/src/oboskrnl/arch/vmm_defines.h @@ -0,0 +1,66 @@ +#pragma once + +// Include platform defines. + +#if defined(__x86_64__) || defined(_WIN64) +#include +#endif + +// Check defines. +#ifndef OBOS_CHILDREN_PER_PT +# error "Platform does not define OBOS_CHILDREN_PER_PT" +#endif +#ifndef OBOS_LEVELS_PER_PAGEMAP +# error "Platform does not define OBOS_LEVELS_PER_PAGEMAP" +#endif +#ifndef OBOS_PAGE_SIZE +# error "Platform does not define OBOS_PAGE_SIZE" +#endif +#ifndef OBOS_VIRT_ADDR_BITWIDTH +# error "Platform does not define OBOS_VIRT_ADDR_BITWIDTH" +#endif +#ifndef OBOS_ADDR_BITWIDTH +# error "Platform does not define OBOS_ADDR_BITWIDTH" +#endif +#ifndef OBOS_MAX_PAGE_FAULT_HANDLERS +# error "Platform does not define OBOS_MAX_PAGE_FAULT_HANDLERS" +#endif +#ifndef OBOS_KERNEL_ADDRESS_SPACE_BASE +# error "Platform does not define OBOS_KERNEL_ADDRESS_SPACE_BASE" +#endif +#ifndef OBOS_KERNEL_ADDRESS_SPACE_LIMIT +# error "Platform does not define OBOS_KERNEL_ADDRESS_SPACE_LIMIT" +#endif +#ifndef OBOS_KERNEL_BASE +# error "Platform does not define OBOS_KERNEL_BASE" +#endif +#ifndef OBOS_KERNEL_TOP +# error "Platform does not define OBOS_KERNEL_TOP" +#endif +// We treat OBOS_HUGE_PAGE_SIZE specially, as support for huge pages is optional. +#ifdef OBOS_HUGE_PAGE_SIZE +# define OBOS_HAS_HUGE_PAGE_SUPPORT 1 +static_assert(OBOS_HUGE_PAGE_SIZE > OBOS_PAGE_SIZE, "Huge page size is less than the normal page size."); +static_assert(OBOS_HUGE_PAGE_SIZE != 0, "Huge page size is zero."); +#else +# define OBOS_HAS_HUGE_PAGE_SUPPORT 0 +#endif +// We can define some macros on our own if they're obvious. +#ifndef OBOS_IS_VIRT_ADDR_CANONICAL +# include +COMPILE_MESSAGE("OBOS_IS_VIRT_ADDR_CANONICAL is not defined.\nConsider defining the macro. Default value is true."); +# define OBOS_IS_VIRT_ADDR_CANONICAL(addr) (true) +#endif +#ifndef OBOS_ZERO_PAGE_PHYSICAL +# define OBOS_ZERO_PAGE_PHYSICAL ((uintptr_t)0) +#endif +#ifndef OBOS_ADDRESS_SPACE_LIMIT +# include +# define OBOS_ADDRESS_SPACE_LIMIT UINTPTR_MAX +#endif +#ifndef OBOS_KERNEL_ADDRESS_SPACE_USABLE_BASE +# include +COMPILE_MESSAGE("OBOS_KERNEL_ADDRESS_SPACE_USABLE_BASE is not defined.\nConsider defining the macro. Default value is OBOS_KERNEL_ADDRESS_SPACE_BASE."); +# define OBOS_KERNEL_ADDRESS_SPACE_USABLE_BASE OBOS_KERNEL_ADDRESS_SPACE_BASE +#endif +static_assert(OBOS_PAGE_SIZE != 0, "Page size is zero."); \ No newline at end of file diff --git a/src/oboskrnl/arch/vmm_map.h b/src/oboskrnl/arch/vmm_map.h new file mode 100644 index 00000000..9f8bb85e --- /dev/null +++ b/src/oboskrnl/arch/vmm_map.h @@ -0,0 +1,5 @@ +#pragma once + +#if defined(__x86_64__) || defined(_WIN64) +#include +#endif \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/asm_helpers.asm b/src/oboskrnl/arch/x86_64/asm_helpers.asm index c32d509c..033f7418 100644 --- a/src/oboskrnl/arch/x86_64/asm_helpers.asm +++ b/src/oboskrnl/arch/x86_64/asm_helpers.asm @@ -1,229 +1,188 @@ -; oboskrnl/arch/x86_64/asm_helpers.asm -; -; Copyright (c) 2024-2026 Omar Berrow - -[BITS 64] -[DEFAULT ABS] - -global getCR0:function default -global getCR2:function default -global getCR3:function default -global getCR4:function default -global getCR4:function default -global getCR8:function default -global getDR6:function default -global pause:function default -global getEFER:function default -global rdmsr:function default -global wrmsr:function default -global outb:function default -global outw:function default -global outd:function default -global inb:function default -global inw:function default -global ind:function default -global __cpuid__:function default -global invlpg:function default -global wbinvd:function default -global xsave:function default -global cli:function default -global sti:function default -global hlt:function default -global MmS_GetCurrentPageTable: function default - -section .text - -getCR0: - push rbp - mov rbp, rsp - - mov rax, cr0 - - leave - ret -getCR2: - push rbp - mov rbp, rsp - - mov rax, cr2 - - leave - ret -getCR3: - push rbp - mov rbp, rsp - - mov rax, cr3 - - leave - ret -getCR4: - push rbp - mov rbp, rsp - - mov rax, cr4 - - leave - ret -getCR8: - push rbp - mov rbp, rsp - - mov rax, cr8 - - leave - ret -getDR6: - push rbp - mov rbp, rsp - - mov rax, dr6 - - leave - ret -getEFER: - push rbp - mov rbp, rsp - - mov ecx, 0xc0000080 ; EFER - rdmsr - shl rdx, 32 - or rax, rdx - - leave - ret -rdmsr: - push rbp - mov rbp, rsp - - mov ecx, edi - rdmsr - shl rdx, 32 - or rax, rdx - - leave - ret -wrmsr: - push rbp - mov rbp, rsp - - mov ecx, edi - mov eax, esi - mov rdx, rsi - shr rdx, 32 - wrmsr - - leave - ret -pause: - pause - ret -outb: - mov dx, di - mov al, sil - out dx, al - ret -outw: - mov dx, di - mov ax, si - out dx, ax - ret -outd: - mov dx, di - mov eax, esi - out dx, eax - ret -inb: - mov eax, 0 - mov dx, di - in al, dx - ret -inw: - mov eax, 0 - mov dx, di - in ax, dx - ret -ind: - mov dx, di - in eax, dx - ret -__cpuid__: - push rbp - mov rbp, rsp - sub rsp, 16 - push rbx - - mov [rbp-0x8], rdx - mov [rbp-0x10], rcx - - mov eax, edi - mov ecx, esi - cpuid - - cmp qword [rbp-0x8],0 - jz .no_rdx - mov r11, [rbp-0x8] - mov dword [r11], eax -.no_rdx: - cmp qword [rbp-0x10],0 - jz .no_rcx - mov r11, [rbp-0x10] - mov dword [r11], ebx -.no_rcx: - cmp r8,0 - jz .no_r8 - mov dword [r8], ecx -.no_r8: - cmp r9,0 - jz .no_r9 - mov dword [r9], edx -.no_r9: - pop rbx - leave - ret - -wbinvd: - wbinvd - ret -invlpg: - invlpg [rdi] - ret -xsave: - xor rcx,rcx - xgetbv - xsave [rdi] - ret -global CoreS_GetCPULocalPtr:function default -extern Arch_cpu_local_curr_offset -extern Arch_SMPInitialized -CoreS_GetCPULocalPtr: - mov rax, [Arch_cpu_local_curr_offset] - mov rax, [gs:rax] - ret -cli: - cli - ret -sti: - sti - ret -hlt: - hlt - ret -global rdtsc -rdtsc: - xor eax,eax - xor edx,edx - rdtsc - shl rdx, 32 - or rax, rdx - ret -MmS_GetCurrentPageTable: - jmp getCR3 - -extern Arch_cpu_local_currentIrql_offset -global Core_RaiseIrqlNoThread: function weak -Core_RaiseIrqlNoThread: - mov rax, [Arch_cpu_local_currentIrql_offset] - xchg [gs:rax], rdi - mov rax, rdi +; oboskrnl/arch/x86_64/asm_helpers.asm +; +; Copyright (c) 2024 Omar Berrow + +[BITS 64] + +global _ZN4obos6getCR0Ev +global _ZN4obos6getCR2Ev +global _ZN4obos6getCR3Ev +global _ZN4obos6getCR4Ev +global _ZN4obos6getCR4Ev +global _ZN4obos6getCR8Ev +global _ZN4obos6getDR6Ev +global _ZN4obos5pauseEv +global _ZN4obos7getEFEREv +global _ZN4obos5rdmsrEj +global _ZN4obos5wrmsrEjm +global _ZN4obos4outbEth +global _ZN4obos4outwEtt +global _ZN4obos4outdEtj +global _ZN4obos3inbEt +global _ZN4obos3inwEt +global _ZN4obos3indEt +global _ZN4obos9__cpuid__EmmPjS0_S0_S0_ +global _ZN4obos6invlpgEm +global _ZN4obos6wbinvdEv +global _ZN4obos5xsaveEPv + +_ZN4obos6getCR0Ev: + push rbp + mov rbp, rsp + + mov rax, cr0 + + leave + ret +_ZN4obos6getCR2Ev: + push rbp + mov rbp, rsp + + mov rax, cr2 + + leave + ret +_ZN4obos6getCR3Ev: + push rbp + mov rbp, rsp + + mov rax, cr3 + + leave + ret +_ZN4obos6getCR4Ev: + push rbp + mov rbp, rsp + + mov rax, cr4 + + leave + ret +_ZN4obos6getCR8Ev: + push rbp + mov rbp, rsp + + mov rax, cr8 + + leave + ret +_ZN4obos6getDR6Ev: + push rbp + mov rbp, rsp + + mov rax, dr6 + + leave + ret +_ZN4obos7getEFEREv: + push rbp + mov rbp, rsp + + mov ecx, 0xc0000080 ; EFER + rdmsr + shl rdx, 32 + or rax, rdx + + leave + ret +_ZN4obos5rdmsrEj: + push rbp + mov rbp, rsp + + mov ecx, edi + rdmsr + shl rdx, 32 + or rax, rdx + + leave + ret +_ZN4obos5wrmsrEjm: + push rbp + mov rbp, rsp + + mov ecx, edi + mov eax, esi + mov rdx, rsi + shr rdx, 32 + wrmsr + + leave + ret +_ZN4obos5pauseEv: + pause + ret +_ZN4obos4outbEth: + mov dx, di + mov al, sil + out dx, al + ret +_ZN4obos4outwEtt: + mov dx, di + mov ax, si + out dx, ax + ret +_ZN4obos4outdEtj: + mov dx, di + mov eax, esi + out dx, eax + ret +_ZN4obos3inbEt: + mov eax, 0 + mov dx, di + in al, dx + ret +_ZN4obos3inwEt: + mov eax, 0 + mov dx, di + in ax, dx + ret +_ZN4obos3indEt: + mov dx, di + in eax, dx + ret +_ZN4obos9__cpuid__EmmPjS0_S0_S0_: + push rbp + mov rbp, rsp + sub rsp, 16 + push rbx + + mov [rbp-0x8], rdx + mov [rbp-0x10], rcx + + mov eax, edi + mov ecx, esi + cpuid + + cmp qword [rbp-0x8],0 + jz .no_rdx + mov r11, [rbp-0x8] + mov dword [r11], eax +.no_rdx: + cmp qword [rbp-0x10],0 + jz .no_rcx + mov r11, [rbp-0x10] + mov dword [r11], ebx +.no_rcx: + cmp r8,0 + jz .no_r8 + mov dword [r8], ecx +.no_r8: + cmp r9,0 + jz .no_r9 + mov dword [r9], edx +.no_r9: + pop rbx + leave + ret + +_ZN4obos6wbinvdEv: + wbinvd + ret +_ZN4obos6invlpgEm: + invlpg [rdi] + ret +_ZN4obos5xsaveEPv: + xor rcx,rcx + xgetbv + xsave [rdi] ret \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/asm_helpers.h b/src/oboskrnl/arch/x86_64/asm_helpers.h index 3e45fc6f..67a7f76c 100644 --- a/src/oboskrnl/arch/x86_64/asm_helpers.h +++ b/src/oboskrnl/arch/x86_64/asm_helpers.h @@ -1,43 +1,40 @@ -/* - oboskrnl/arch/x86_64/asm_helpers.h - - Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include - -void outb(uint16_t port, uint8_t data); -void outw(uint16_t port, uint16_t data); -void outd(uint16_t port, uint32_t data); -uint8_t inb(uint16_t port); -uint16_t inw(uint16_t port); -uint32_t ind(uint16_t port); - -uintptr_t getCR0(); -uintptr_t getCR2(); -uintptr_t getCR3(); -uintptr_t getCR4(); -uintptr_t getCR8(); -uintptr_t getEFER(); - -uintptr_t getDR6(); - -uint64_t rdtsc(); - -void __cpuid__(uint64_t initialEax, uint64_t initialEcx, uint32_t* eax, uint32_t* ebx, uint32_t* ecx, uint32_t* edx); - -uint64_t rdmsr(uint32_t msr); -void wrmsr(uint32_t msr, uint64_t val); - -void pause(); - -void invlpg(uintptr_t addr); -void wbinvd(); - -void xsave(void* region); - -void cli(); -void sti(); -void hlt(); \ No newline at end of file +/* + oboskrnl/arch/x86_64/asm_helpers.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include + +namespace obos +{ + void outb(uint16_t port, uint8_t data); + void outw(uint16_t port, uint16_t data); + void outd(uint16_t port, uint32_t data); + uint8_t inb(uint16_t port); + uint16_t inw(uint16_t port); + uint32_t ind(uint16_t port); + + uintptr_t getCR0(); + uintptr_t getCR2(); + uintptr_t getCR3(); + uintptr_t getCR4(); + uintptr_t getCR8(); + uintptr_t getEFER(); + + uintptr_t getDR6(); + + void __cpuid__(uint64_t initialEax, uint64_t initialEcx, uint32_t* eax, uint32_t* ebx, uint32_t* ecx, uint32_t* edx); + + uint64_t rdmsr(uint32_t msr); + void wrmsr(uint32_t msr, uint64_t val); + + void pause(); + + void invlpg(uintptr_t addr); + void wbinvd(); + + void xsave(void* region); +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/bgdt.asm b/src/oboskrnl/arch/x86_64/bgdt.asm index fe768f1f..510f1e6a 100644 --- a/src/oboskrnl/arch/x86_64/bgdt.asm +++ b/src/oboskrnl/arch/x86_64/bgdt.asm @@ -1,111 +1,87 @@ -; oboskrnl/arch/x86_64/bgdt.asm - -; Copyright (c) 2024 Omar Berrow - -[BITS 64] -[DEFAULT ABS] - -section .bss -Arch_InitialISTStack: - resb 0x20000 -global Arch_InitialISTStack:data hidden -section .data -align 1 -TSS: - .rsv1: dd 0 - .rsp0: dq 0 - .rsp1: dq 0 - .rsp2: dq 0 - .rsv2: dq 0 - .ist0: dq 0 - .ist1: dq 0 - .ist2: dq 0 - .ist3: dq 0 - .ist4: dq 0 - .ist5: dq 0 - .ist6: dq 0 - .ist7: dq 0 - .rsv3: dq 0 - .rsv4: dw 0 - .iopb: dw 103 -.end: -TSS_Len equ TSS.end-TSS -global GDT:data hidden -GDT: - .null: dq 0 - .kcode: dq 0x00af9b000000ffff - .kdata: dq 0x00af93000000ffff - .tss_limitLow: dw 0 - .tss_baseLow: dw 0 - .tss_baseMiddle1: db 0 - .tss_access: db 0x89 - .tss_gran: db 0x40 - .tss_baseMiddle2: db 0 - .tss_baseHigh: dd 0 - .tss_resv1: dd 0 -.end: -GDTPtr: - .len: dw GDT.end-GDT-1 - .base: dq GDT -section .text - -global Arch_InitBootGDT:function hidden - -Arch_InitBootGDT: - push rbp - mov rbp, rsp - sub rsp, 10 - - mov rax, TSS - mov word [GDT.tss_limitLow], TSS_Len - mov [GDT.tss_baseLow], ax - mov rdx, rax - shr rdx, 16 - mov [GDT.tss_baseMiddle1], dl - shr rdx, 8 - mov [GDT.tss_baseMiddle2], dl - shr rdx, 8 - mov [GDT.tss_baseHigh], edx - - lea rax, [Arch_InitialISTStack+0x20000] - mov [TSS.ist0], rax - - lgdt [GDTPtr] - - mov ax, 0x18 - ltr ax - - mov ax, 0x10 - mov ds, ax - mov ss, ax - mov es, ax - mov fs, ax - mov gs, ax - - leave - pop rax - push 0x8 - push rax - retfq -global Arch_FlushGDT:function hidden -Arch_FlushGDT: - push rbp - mov rbp, rsp - - lgdt [rdi] - - mov ax, 0x10 - mov ds, ax - mov ss, ax - mov es, ax - mov fs, ax - mov gs, ax - - mov ax, 0x28 - ltr ax - - leave - pop rax - push 0x8 - push rax - retfq +; oboskrnl/arch/x86_64/bgdt.asm + +; Copyright (c) 2024 Omar Berrow + +[BITS 64] + +section .bss +initial_ist: + resb 0x4000 +section .data +align 1 +TSS: + .rsv1: dd 0 + .rsp0: dq 0 + .rsp1: dq 0 + .rsp2: dq 0 + .rsv2: dq 0 + .ist0: dq 0 + .ist1: dq 0 + .ist2: dq 0 + .ist3: dq 0 + .ist4: dq 0 + .ist5: dq 0 + .ist6: dq 0 + .ist7: dq 0 + .rsv3: dq 0 + .rsv4: dw 0 + .iopb: dw 103 +.end: +TSS_Len equ TSS.end-TSS +global GDT +GDT: + .null: dq 0 + .kcode: dq 0x00af9b000000ffff + .kdata: dq 0x00af93000000ffff + .tss_limitLow: dw 0 + .tss_baseLow: dw 0 + .tss_baseMiddle1: db 0 + .tss_access: db 0x89 + .tss_gran: db 0x40 + .tss_baseMiddle2: db 0 + .tss_baseHigh: dd 0 + .tss_resv1: dd 0 +.end: +GDTPtr: + .len: dw GDT.end-GDT-1 + .base: dq GDT +section .text + +global InitBootGDT + +InitBootGDT: + push rbp + mov rbp, rsp + sub rsp, 10 + + mov rax, TSS + mov word [GDT.tss_limitLow], TSS_Len + mov [GDT.tss_baseLow], ax + mov rdx, rax + shr rdx, 16 + mov [GDT.tss_baseMiddle1], dl + shr rdx, 8 + mov [GDT.tss_baseMiddle2], dl + shr rdx, 8 + mov [GDT.tss_baseHigh], edx + + lea rax, [initial_ist+0x4000] + mov [TSS.ist0], rax + + lgdt [GDTPtr] + + mov ax, 0x18 + ltr ax + + mov ax, 0x10 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + + leave + pop rax + push 0x8 + push rax + retfq \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/boot_info.h b/src/oboskrnl/arch/x86_64/boot_info.h deleted file mode 100644 index eebfe263..00000000 --- a/src/oboskrnl/arch/x86_64/boot_info.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * oboskrnl/arch/x86_64/boot_info.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#if !OBOS_USE_LIMINE - -#include - -extern volatile struct ultra_memory_map_attribute* Arch_MemoryMap; -extern volatile struct ultra_platform_info_attribute* Arch_LdrPlatformInfo; -extern volatile struct ultra_kernel_info_attribute* Arch_KernelInfo; -extern volatile struct ultra_module_info_attribute* Arch_KernelBinary; -extern volatile struct ultra_module_info_attribute* Arch_InitialSwapBuffer; -extern volatile struct ultra_framebuffer* Arch_Framebuffer; -extern volatile struct ultra_boot_context* Arch_BootContext; -#else - -#include -#include - -extern volatile struct limine_framebuffer_request Arch_LimineFBRequest; -extern volatile struct limine_memmap_request Arch_LimineMemmapRequest; -extern volatile struct limine_module_request Arch_LimineModuleRequest; -extern volatile struct limine_hhdm_request Arch_LimineHHDMRequest; -extern volatile struct limine_executable_file_request Arch_LimineKernelInfoRequest; -extern volatile struct limine_executable_address_request Arch_LimineKernelAddressRequest; -extern volatile struct limine_executable_cmdline_request Arch_LimineKernelCmdlineRequest; -extern volatile struct limine_bootloader_info_request Arch_LimineBtldrInfoRequest; -extern volatile struct ultra_framebuffer* Arch_Framebuffer; // Artificially created -extern struct limine_file* Arch_KernelBinary; -#endif -extern uintptr_t Arch_RSDPBase; \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/cmos.c b/src/oboskrnl/arch/x86_64/cmos.c deleted file mode 100644 index 689dc8f6..00000000 --- a/src/oboskrnl/arch/x86_64/cmos.c +++ /dev/null @@ -1,136 +0,0 @@ -/* - * oboskrnl/arch/x86_64/cmos.c - * - * Copyright (c) 2025 Omar Berrow -*/ - -#include -#include -#include - -#include -#include - -#include - -#include -#include - -static bool has_cmos = false; -static bool initialized_cmos = false; -static unsigned CMOS_REGISTER_CENTURY = 0; -static uint8_t cmos_flags = 0; - -static uint8_t read_cmos8_no_bcd(uint8_t offset) -{ - cli(); - outb(CMOS_SELECT, offset); - uint8_t val = inb(CMOS_DATA); - sti(); - return val; -} -static uint8_t read_cmos8(uint8_t offset) -{ - uint8_t val = read_cmos8_no_bcd(offset); - if (cmos_flags & 0x4) - return val; - uint8_t ret = val & 0xf; - ret += ((val >> 4) * 10); - return ret; -} - -obos_status Arch_CMOSInitialize() -{ - if (initialized_cmos) - return OBOS_STATUS_ALREADY_INITIALIZED; - struct uacpi_table tbl = {}; - uacpi_table_find_by_signature(ACPI_FADT_SIGNATURE, &tbl); - struct acpi_fadt* fadt = (void*)tbl.hdr; - if (fadt->iapc_boot_arch & ACPI_IA_PC_NO_CMOS_RTC) - return OBOS_STATUS_NOT_FOUND; - has_cmos = true; - initialized_cmos = true; - cmos_flags = read_cmos8_no_bcd(CMOS_REGISTER_STATUS_B); - CMOS_REGISTER_CENTURY = fadt->century; - return OBOS_STATUS_SUCCESS; -} - -obos_status Arch_CMOSGetTimeOfDay(cmos_timeofday* time) -{ - if (!has_cmos) - return OBOS_STATUS_NOT_FOUND; - if (!time) - return OBOS_STATUS_INVALID_ARGUMENT; - uint8_t val = 0; - do { - val = read_cmos8_no_bcd(CMOS_REGISTER_STATUS_A); - } while(val & BIT(7)); - time->seconds = read_cmos8(CMOS_REGISTER_SECONDS); - time->minutes = read_cmos8(CMOS_REGISTER_MINUTES); - time->hours = read_cmos8(CMOS_REGISTER_HOURS); - time->day_of_month = read_cmos8(CMOS_REGISTER_DAY_OF_MONTH); - time->month = read_cmos8(CMOS_REGISTER_MONTH); - time->year = read_cmos8(CMOS_REGISTER_YEAR); - uint16_t century = CMOS_REGISTER_CENTURY ? read_cmos8(CMOS_REGISTER_CENTURY) : 20; - century *= 100; - time->year += century; - return OBOS_STATUS_SUCCESS; -} - -static int days_from_civil(int y, unsigned m, unsigned d) -{ - y -= m <= 2; - const int era = (y >= 0 ? y : y - 399) / 400; - const unsigned yoe = (unsigned)(y - era * 400); // [0, 399] - const unsigned doy = (153 * (m > 2 ? m - 3 : m + 9) + 2) / 5 + d - 1; // [0, 365] - const unsigned doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; // [0, 146096] - return era * 146097 + (int)(doe) - 719468; -} - -obos_status Arch_CMOSGetEpochTime(long* out) -{ - cmos_timeofday tm = {}; - obos_status status = Arch_CMOSGetTimeOfDay(&tm); - if (obos_is_error(status)) return status; - - long days = days_from_civil(tm.year, tm.month, tm.day_of_month); - *out = (days * 86400) + (tm.hours*60*60) + (tm.minutes * 60) + tm.seconds; - return OBOS_STATUS_SUCCESS; -} - -obos_status SysS_ClockGet(int clock, long *secs, long *nsecs) -{ - if (!secs && !nsecs) - return OBOS_STATUS_SUCCESS; - - if (clock == 1) - { - static uint64_t cached_rate = 0; - if (!cached_rate) - cached_rate = 1000000000/CoreS_GetNativeTimerFrequency(); - long nsecs_res = CoreS_GetNativeTimerTick()*cached_rate; - obos_status status = memcpy_k_to_usr(nsecs, &nsecs_res, sizeof(long)); - if (obos_is_error(status) && nsecs) - return status; - long res = nsecs_res / 1000000000; - status = memcpy_k_to_usr(secs, &res, sizeof(long)); - if (obos_is_error(status) && secs) - return status; - return OBOS_STATUS_SUCCESS; - } - - cmos_timeofday tm = {}; - Arch_CMOSGetTimeOfDay(&tm); - - long days = days_from_civil(tm.year, tm.month, tm.day_of_month); - long res = (days * 86400) + (tm.hours*60*60) + (tm.minutes * 60) + tm.seconds; - obos_status status = secs ? memcpy_k_to_usr(secs, &res, sizeof(long)) : OBOS_STATUS_SUCCESS; - if (obos_is_error(status)) - return status; - res *= 1000000000; - status = nsecs ? memcpy_k_to_usr(nsecs, &res, sizeof(long)) : OBOS_STATUS_SUCCESS; - if (obos_is_error(status)) - return status; - - return OBOS_STATUS_SUCCESS; -} diff --git a/src/oboskrnl/arch/x86_64/cmos.h b/src/oboskrnl/arch/x86_64/cmos.h deleted file mode 100644 index 09f67eaa..00000000 --- a/src/oboskrnl/arch/x86_64/cmos.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * oboskrnl/arch/x86_64/cmos.h - * - * Copyright (c) 2025 Omar Berrow -*/ - -#pragma once - -#include -#include - -enum { - CMOS_SELECT = 0x70, - CMOS_DATA = 0x71, -}; - -// Century register should be fetched from the FADT -enum { - // 1-byte, range 0-60 - CMOS_REGISTER_SECONDS = 0x00, - // 1-byte, range 0-60 - CMOS_REGISTER_MINUTES = 0x02, - // 1-byte, range 0-23 in 24-hour mode, 0-12 in 12-hour mode - CMOS_REGISTER_HOURS = 0x04, - // 1-byte, range 1-7 - CMOS_REGISTER_WEEKDAY = 0x06, - // 1-byte, range 0-31 - CMOS_REGISTER_DAY_OF_MONTH = 0x07, - // 1-byte, range 1-12 - CMOS_REGISTER_MONTH = 0x08, - // 1-byte, range 0-99 - CMOS_REGISTER_YEAR = 0x09, - // 1-byte - CMOS_REGISTER_STATUS_A = 0x0A, - // 1-byte - CMOS_REGISTER_STATUS_B = 0x0B, -}; - -enum { - CMOS_SUNDAY = 1, - CMOS_MONDAY, - CMOS_TUESDAY, - CMOS_WEDNESDAY, - CMOS_THURSDAY, - CMOS_FRIDAY, - CMOS_SATURDAY, -}; - -typedef struct cmos_timeofday -{ - uint8_t seconds; - uint8_t minutes; - uint8_t hours; - uint8_t day_of_month; - uint8_t month; - uint16_t year; -} cmos_timeofday; - -obos_status Arch_CMOSInitialize(); -obos_status Arch_CMOSGetTimeOfDay(cmos_timeofday* time); -OBOS_EXPORT obos_status Arch_CMOSGetEpochTime(long* out); - -obos_status SysS_ClockGet(int clock, long *secs, long *nsecs); \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/cpu_local_arch.h b/src/oboskrnl/arch/x86_64/cpu_local_arch.h index ccc7b479..800c3890 100644 --- a/src/oboskrnl/arch/x86_64/cpu_local_arch.h +++ b/src/oboskrnl/arch/x86_64/cpu_local_arch.h @@ -1,51 +1,168 @@ -/* - * oboskrnl/arch/x86_64/cpu_local_arch.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include - -#include - -#include - -#include - -#include - -typedef struct cpu_local_arch -{ - uint64_t gdtEntries[7]; - struct - { - uint32_t rsv1; - uint64_t rsp0; - uint64_t rsp1; - uint64_t rsp2; - uint64_t rsv2; - uint64_t ist0; - uint64_t ist1; - uint64_t ist2; - uint64_t ist3; - uint64_t ist4; - uint64_t ist5; - uint64_t ist6; - uint64_t ist7; - uint64_t rsv3; - uint16_t rsv4; - uint16_t iopb; - } OBOS_PACK tss; - // Size: 0x20000 bytes, divided into the IST1 stack (offset 0 to 0x10000), and the cpu temp stack (offset 0x10000 to 0x20000) - void* ist_stack; - void* startup_stack; // Size: 0x4000 bytes, freed after smp initialization. - bool initializedSchedulerTimer; - bool pf_handler_running; - gdb_ctx dbg_ctx; - dpc dbg_dpc; - uint64_t stack_check_guard; - uint8_t lapicId; - struct tlb_shootdown_packet* curr_pckt; -} cpu_local_arch; +/* + oboskrnl/arch/x86_64/cpu_local_arch.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include +#include + +#include + +#if OBOS_KDBG_ENABLED +# include +#endif + +namespace obos +{ + class Irq; + struct interrupt_frame; + namespace arch + { + struct base_ipi + { + base_ipi() = default; + base_ipi(void(*hnd)(base_ipi*, interrupt_frame*)) : handler {hnd} {}; + virtual ~base_ipi() {} + void(*handler)(base_ipi* _this, interrupt_frame*); + }; + // IPI to start the LAPIC timer. + struct timer_ipi : public base_ipi + { + private: + static void _handler(base_ipi* _this, interrupt_frame*); + public: + timer_ipi() : base_ipi{ _handler } {} + const Irq* irq = nullptr; + uint64_t freq = 0; + bool singleShoot = false; + virtual ~timer_ipi() {} + }; + // TLB Shootdown + struct shootdown_ipi : public base_ipi + { + private: + static void _handler(base_ipi* _this, interrupt_frame*); + public: + shootdown_ipi() : base_ipi{ _handler } {} + // If the current PageMap (cr3) is the same as the specified page map, then the address at 'virt' will 'invlpg'ed by the IPI handler. + const PageMap* pm; + uintptr_t virt; + virtual ~shootdown_ipi() {} + }; + // IPI to set the debug registers. + struct dbg_reg_ipi : public base_ipi + { + private: + static void _handler(base_ipi* _this, interrupt_frame*); + public: + dbg_reg_ipi() : base_ipi{ _handler } {} + uint8_t regIdx; + uint64_t* val; + // false to read from DRn into *val, true to write the value at *val into DRn. + bool rw; + virtual ~dbg_reg_ipi() {} + }; + struct ipi final + { + enum + { + IPI_INVALID = 0, + IPI_TIMER, + IPI_SHOOTDOWN, + IPI_DEBUG_REGISTER, + } type = IPI_INVALID; + union + { + base_ipi* base; + timer_ipi* timer; + shootdown_ipi* tlb_shootdown; + dbg_reg_ipi* dbg_reg; + } data{}; + bool processed = false; + ipi *next, *prev; + }; + struct cpu_local_arch + { + cpu_local_arch() + { + gdt[0] = 0; + gdt[1] = 0x00af9b000000ffff; + gdt[2] = 0x00af93000000ffff; + gdt[3] = 0x00aff3000000ffff; + gdt[4] = 0x00affb000000ffff; + gdt[5] = 0x0000000000000000; + gdt[6] = 0x0000000000000000; + } + // Layout: + // 0x00: Null descriptor. + // 0x08: Kernel code descriptor. + // 0x10: Kernel data descriptor. + // 0x18: User data descriptor. + // 0x20: User code descriptor. + // 0x28: TSS descriptor. + uint64_t gdt[7] = { + 0, + 0x00af9b000000ffff, + 0x00af93000000ffff, + 0x00aff3000000ffff, + 0x00affb000000ffff, + // This is to be filled in the gdt initialize function of the SMP code. + 0x0000000000000000, + 0x0000000000000000, + }; + struct + { + uint32_t rsv1; + uint64_t rsp0; + uint64_t rsp1; + uint64_t rsp2; + uint64_t rsv2; + uint64_t ist0; + uint64_t ist1; + uint64_t ist2; + uint64_t ist3; + uint64_t ist4; + uint64_t ist5; + uint64_t ist6; + uint64_t ist7; + uint64_t rsv3; + uint16_t rsv4; + uint16_t iopb; + } OBOS_PACK tss; + struct + { + ipi *head, *tail; + size_t nNodes; + ipi *pop() + { + if (!nNodes) + return nullptr; + ipi* ret = head; + if (tail == ret) + tail = ret->prev; + if (ret->next) + ret->next->prev = nullptr; + head = head->next; + nNodes--; + return ret; + } + void push(ipi* i) + { + if (!head) + head = i; + if (tail) + tail->next = i; + i->prev = tail; + tail = i; + nNodes++; + } + } ipi_queue; +#if OBOS_KDBG_ENABLED + kdbg::cpu_local_debugger_state debugger_state; +#endif + }; + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/cpu_utils.h b/src/oboskrnl/arch/x86_64/cpu_utils.h new file mode 100644 index 00000000..f2c01c8f --- /dev/null +++ b/src/oboskrnl/arch/x86_64/cpu_utils.h @@ -0,0 +1,33 @@ +/* + oboskrnl/arch/x86_64/cpu_utils.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include + +namespace obos +{ + namespace arch + { + // x86-64 specific. + extern uint8_t g_lapicIDs[256]; + + // All architectures should define the following symbols. + + extern bool g_initializedAllCPUs; + + /// + /// Starts all processors. This cannot be used to restart CPUs after a call to StopCPUs(). + /// + /// The amount of processors on the system. + size_t StartProcessors(); + /// + /// Stops all CPUs. + /// + /// Whether to stop the current cpu as well. If true, this function doesn't return. + void StopCPUs(bool includingSelf = false); + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/driver_interface_load.cpp b/src/oboskrnl/arch/x86_64/driver_interface_load.cpp new file mode 100644 index 00000000..96339d07 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/driver_interface_load.cpp @@ -0,0 +1,500 @@ +/* + oboskrnl/arch/x86_64/driver_interface_load.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include + +#include + +#define Ehdr(ptr) ((const Elf64_Ehdr*)ptr) +#define OffsetPtr(ptr, off) ((decltype(ptr))(((char*)(ptr)) + (off))) +#define OffsetOf(type, member) ((uintptr_t)&(((type*)nullptr)->member)) + +namespace obos +{ + extern volatile limine_kernel_file_request kernel_file; + namespace arch + { + using namespace elf; + + struct relocation + { + uint32_t symbolTableOffset = 0; + uintptr_t virtualAddress = 0; + uint16_t relocationType = 0; + int64_t addend = 0; + }; + struct copy_reloc + { + void* src = nullptr, * dest = nullptr; + size_t size = 0; + }; + static const char* getElfString(const Elf64_Ehdr* elfHeader, uintptr_t index) + { + const char* startAddress = reinterpret_cast(elfHeader); + Elf64_Shdr* stringTable = reinterpret_cast(const_cast(startAddress) + elfHeader->e_shoff); + stringTable += elfHeader->e_shstrndx; + return startAddress + stringTable->sh_offset + index; + } + static uint32_t ElfHash(const char* name) + { + uint32_t h = 0, g = 0; + + while (*name) + { + h = (h << 4) + *name++; + if ((g = h & 0xf0000000)) + h ^= g >> 24; + h &= ~g; + } + return h; + } + Elf64_Sym* GetSymbolFromTable( + uint8_t* fileStart, + uint8_t* baseAddress, + Elf64_Sym* symbolTable, + uintptr_t hashTableOff, + Elf64_Off stringTable, + const char* _symbol) + { + Elf64_Word* hashTableBase = (Elf64_Word*)(baseAddress + hashTableOff); + auto& nBuckets = hashTableBase[0]; + auto currentBucket = ElfHash(_symbol) % nBuckets; + Elf64_Word* firstBucket = hashTableBase + 2; + Elf64_Word* firstChain = firstBucket + nBuckets; + size_t index = firstBucket[currentBucket]; + while (index) + { + auto& symbol = symbolTable[index]; + const char* symbolName = (char*)(fileStart + stringTable + symbol.st_name); + if (strcmp(symbolName, _symbol)) + return &symbol; + + index = firstChain[index]; + } + return nullptr; + } + static Elf64_Sym* GetSymbolFromTable( + uint8_t* fileStart, + Elf64_Sym* symbolTable, + size_t szSymbolTable, + Elf64_Off stringTable, + const char* _symbol, + uint8_t requiredBinding = STB_GLOBAL) + { + size_t countEntries = szSymbolTable / sizeof(Elf64_Sym); + for (size_t i = 0; i < countEntries; i++) + { + auto& symbol = symbolTable[i]; + if ((symbol.st_info >> 4) != requiredBinding) + continue; + const char* symbolName = (char*)(fileStart + stringTable + symbol.st_name); + if (strcmp(symbolName, _symbol)) + return &symbol; + } + return nullptr; + } + struct tables + { + const Elf64_Shdr* symtab_section; + const Elf64_Shdr* strtab_section; + }; + static tables GetKernelSymbolStringTables() + { + uintptr_t kernelFileStart = (uintptr_t)kernel_file.response->kernel_file->address; + Elf64_Ehdr* eheader = (Elf64_Ehdr*)kernelFileStart; + Elf64_Shdr* iter = reinterpret_cast(kernelFileStart + eheader->e_shoff); + Elf64_Shdr* symtab_section = nullptr; + Elf64_Shdr* strtab_section = nullptr; + + for (size_t i = 0; i < eheader->e_shnum; i++, iter++) + { + const char* section_name = getElfString(eheader, iter->sh_name); + if (strcmp(".symtab", section_name)) + { + symtab_section = iter; + continue; + } + if (strcmp(".strtab", section_name)) + { + strtab_section = iter; + continue; + } + if (strtab_section != nullptr && symtab_section != nullptr) + break; + } + if (!symtab_section) + return { nullptr,nullptr }; + if (!strtab_section) + return { nullptr,nullptr }; + return { symtab_section,strtab_section }; + } + static tables GetDriverSymbolStringTables(const uint8_t* file) + { + const Elf64_Ehdr* eheader = (Elf64_Ehdr*)file; + const Elf64_Shdr* iter = reinterpret_cast(file + eheader->e_shoff); + const Elf64_Shdr* symtab_section = nullptr; + const Elf64_Shdr* strtab_section = nullptr; + + for (size_t i = 0; i < eheader->e_shnum; i++, iter++) + { + const char* section_name = getElfString(eheader, iter->sh_name); + if (strcmp(".symtab", section_name)) + { + symtab_section = iter; + continue; + } + if (strcmp(".strtab", section_name)) + { + strtab_section = iter; + continue; + } + if (strtab_section != nullptr && symtab_section != nullptr) + break; + } + if (!symtab_section) + return { nullptr,nullptr }; + if (!strtab_section) + return { nullptr,nullptr }; + return { symtab_section,strtab_section }; + } + static Elf64_Sym* GetSymbolFromIndex( + Elf64_Sym* symbolTable, + size_t index) + { + return symbolTable + index; + } + static bool ApplyRelocations(const Elf64_Ehdr* ehdr, const Elf64_Dyn* dynamicHeader, void* baseAddress) + { + // Most of the code here is copied from the previous kernel. + void* file = (void*)ehdr; + utils::Vector required_relocations{}; + const Elf64_Dyn* currentDynamicHeader = dynamicHeader; + size_t last_dtrelasz = 0, last_dtrelsz = 0, last_dtpltrelsz = 0; + bool awaitingRelaSz = false, foundRelaSz = false; + const Elf64_Dyn* dynEntryAwaitingRelaSz = nullptr; + bool awaitingRelSz = false, foundRelSz = false; + const Elf64_Dyn* dynEntryAwaitingRelSz = nullptr; + uint64_t last_dlpltrel = 0; + auto handleDtRel = [&](const Elf64_Dyn* dynamicHeader, size_t sz) { + const Elf64_Rela* relTable = (Elf64_Rela*)OffsetPtr(file, dynamicHeader->d_un.d_ptr); + for (size_t i = 0; i < sz / sizeof(Elf64_Rela); i++) + required_relocations.push_back({ + (uint32_t)(relTable[i].r_info >> 32), + relTable[i].r_offset, + (uint16_t)(relTable[i].r_info & 0xffff), + relTable[i].r_addend, + }); + }; + auto handleDtRela = [&](const Elf64_Dyn* dynamicHeader, size_t sz) { + const Elf64_Rela* relTable = (Elf64_Rela*)OffsetPtr(file, dynamicHeader->d_un.d_ptr); + for (size_t i = 0; i < (sz / sizeof(Elf64_Rela)); i++) + required_relocations.push_back({ + (uint32_t)(relTable[i].r_info >> 32), + relTable[i].r_offset, + (uint16_t)(relTable[i].r_info & 0xffff), + relTable[i].r_addend, + }); + }; + [[maybe_unused]] Elf64_Sym* symbolTable = 0; + [[maybe_unused]] Elf64_Off stringTable = 0; + [[maybe_unused]] uintptr_t hashTableOffset = 0; + [[maybe_unused]] Elf64_Addr* GOT = nullptr; + for (size_t i = 0; currentDynamicHeader->d_tag != DT_NULL; i++, currentDynamicHeader++) + { + switch (currentDynamicHeader->d_tag) + { + case DT_HASH: + hashTableOffset = currentDynamicHeader->d_un.d_ptr; + break; + case DT_PLTGOT: + // TODO: Find out whether this is the PLT or GOT (if possible). + GOT = (Elf64_Addr*)OffsetPtr(baseAddress, currentDynamicHeader->d_un.d_ptr); + break; + case DT_REL: + if (!foundRelSz) + { + awaitingRelSz = true; + dynEntryAwaitingRelSz = currentDynamicHeader; + break; + } + handleDtRel(currentDynamicHeader, last_dtrelsz); + foundRelSz = false; + last_dtrelsz = 0; + break; + case DT_RELA: + if (!foundRelaSz) + { + awaitingRelaSz = true; + dynEntryAwaitingRelaSz = currentDynamicHeader; + break; + } + handleDtRela(currentDynamicHeader, last_dtrelasz); + foundRelaSz = false; + last_dtrelasz = 0; + break; + case DT_JMPREL: + switch (last_dlpltrel) + { + case DT_REL: + handleDtRel(currentDynamicHeader, last_dtpltrelsz); + break; + case DT_RELA: + handleDtRela(currentDynamicHeader, last_dtpltrelsz); + break; + default: + break; + } + break; + case DT_RELSZ: + last_dtrelsz = currentDynamicHeader->d_un.d_val; + foundRelSz = !awaitingRelSz; + if (awaitingRelSz) + { + handleDtRel(dynEntryAwaitingRelSz, last_dtrelsz); + awaitingRelSz = false; + } + break; + case DT_RELASZ: + last_dtrelasz = currentDynamicHeader->d_un.d_val; + foundRelaSz = !awaitingRelaSz; + if (awaitingRelaSz) + { + handleDtRel(dynEntryAwaitingRelaSz, last_dtrelasz); + awaitingRelaSz = false; + } + break; + case DT_PLTREL: + last_dlpltrel = currentDynamicHeader->d_un.d_val; + break; + case DT_PLTRELSZ: + last_dtpltrelsz = currentDynamicHeader->d_un.d_val; + break; + case DT_STRTAB: + stringTable = currentDynamicHeader->d_un.d_ptr; + break; + case DT_SYMTAB: + symbolTable = (Elf64_Sym*)OffsetPtr(baseAddress, currentDynamicHeader->d_un.d_ptr); + break; + default: + break; + } + } + utils::Vector copy_relocations; + uint8_t* kFileBase = (uint8_t*)kernel_file.response->kernel_file->address; + tables kernel_tables = GetKernelSymbolStringTables(); + for (size_t j = 0; j < required_relocations.length(); j++) + { + auto& i = required_relocations.at(j); + Elf64_Sym* Symbol = nullptr; + if (i.symbolTableOffset) + { + auto& Unresolved_Symbol = *GetSymbolFromIndex(symbolTable, i.symbolTableOffset); + Symbol = GetSymbolFromTable( + kFileBase, + (Elf64_Sym*)(kFileBase + kernel_tables.symtab_section->sh_offset), + kernel_tables.symtab_section->sh_size, + kernel_tables.strtab_section->sh_offset, + (const char*)OffsetPtr(baseAddress, stringTable + Unresolved_Symbol.st_name) + ); + if (!Symbol) + return false; + if (Unresolved_Symbol.st_size != Symbol->st_size && i.relocationType == R_AMD64_COPY) + { + // Oh no! + return false; + } + } + auto& type = i.relocationType; + uintptr_t relocAddr = (uintptr_t)baseAddress + i.virtualAddress; + uint64_t relocResult = 0; + uint8_t relocSize = 0; + switch (type) + { + case R_AMD64_NONE: + continue; + case R_AMD64_64: + relocResult = Symbol->st_value + i.addend; + relocSize = 8; + break; + case R_AMD64_PC32: + relocResult = Symbol->st_value + i.addend - relocAddr; + relocSize = 4; + break; + case R_AMD64_GOT32: + logger::warning("%s: Unimplemented: R_AMD64_GOT32 Relocation.\n", __func__); + return false; + // TODO: Replace the zero in the calculation with "G" (see elf spec for more info). + relocResult = 0 + i.addend; + relocSize = 4; + break; + case R_AMD64_PLT32: + logger::warning("%s: Unimplemented: R_AMD64_PLT32 Relocation.\n", __func__); + return false; + // TODO: Replace the zero in the calculation with "L" (see elf spec for more info). + relocResult = 0 + i.addend - relocAddr; + relocSize = 4; + break; + case R_AMD64_COPY: + // Save copy relocations for the end because if we don't, it might contain unresolved addresses. + copy_relocations.push_back({ (void*)relocAddr, (void*)Symbol->st_value, Symbol->st_size }); + relocSize = 0; + break; + case R_AMD64_JUMP_SLOT: + case R_AMD64_GLOB_DAT: + relocResult = Symbol->st_value; + relocSize = 8; + break; + case R_AMD64_RELATIVE: + relocResult = (uint64_t)baseAddress + i.addend; + relocSize = 8; + break; + case R_AMD64_GOTPCREL: + return false; + // TODO: Replace the zero in the calculation with "G" (see elf spec for more info). + relocResult = 0 + (uint64_t)baseAddress + i.addend - relocAddr; + relocSize = 8; + break; + case R_AMD64_32: + relocResult = Symbol->st_value + i.addend; + relocSize = 4; + break; + case R_AMD64_32S: + relocResult = Symbol->st_value + i.addend; + relocSize = 4; + break; + case R_AMD64_16: + relocResult = Symbol->st_value + i.addend; + relocSize = 2; + break; + case R_AMD64_PC16: + relocResult = Symbol->st_value + i.addend - relocAddr; + relocSize = 2; + break; + case R_AMD64_8: + relocResult = Symbol->st_value + i.addend; + relocSize = 1; + break; + case R_AMD64_PC8: + relocResult = Symbol->st_value + i.addend - relocAddr; + relocSize = 1; + break; + case R_AMD64_PC64: + relocResult = Symbol->st_value + i.addend - relocAddr; + relocSize = 8; + break; + case R_AMD64_GOTOFF64: + relocResult = Symbol->st_value + i.addend - ((uint64_t)GOT); + relocSize = 8; + break; + case R_AMD64_GOTPC32: + relocResult = (uint64_t)GOT + i.addend + relocAddr; + relocSize = 8; + break; + case R_AMD64_SIZE32: + relocSize = 4; + relocResult = Symbol->st_size + i.addend; + break; + case R_AMD64_SIZE64: + relocSize = 8; + relocResult = Symbol->st_size + i.addend; + break; + default: + break; // Ignore. + } + switch (relocSize) + { + case 0: + break; // The relocation type is rather unsupported or special. + case 1: // word8 + *(uint8_t*)(relocAddr) = (uint8_t)(relocResult & 0xff); + break; + case 2: // word16 + *(uint16_t*)(relocAddr) = (uint16_t)(relocResult & 0xffff); + break; + case 4: // word32 + *(uint32_t*)(relocAddr) = (uint16_t)(relocResult & 0xffffffff); + break; + case 8: // word64 + *(uint64_t*)(relocAddr) = relocResult; + break; + default: + break; + } + } + // Apply copy relocations. + for (size_t i = 0; i < copy_relocations.length(); i++) + { + auto& reloc = copy_relocations.at(i); + memcpy(reloc.src, reloc.dest, reloc.size); + } + return true; + } + void* LoadDynamicElfFile(const void* data, size_t size) + { + // First, figure out the size of all the program headers. + void* base = nullptr, *end = nullptr; + const Elf64_Ehdr* ehdr = Ehdr(data); + const Elf64_Phdr* programHeaders = OffsetPtr((const Elf64_Phdr*)data, ehdr->e_phoff); + const Elf64_Phdr* dynamicPhdr = nullptr; + const Elf64_Dyn* dynamicHdr = nullptr; + for (size_t i = 0; i < ehdr->e_phnum; i++) + { + if (programHeaders[i].p_type == PT_DYNAMIC) + dynamicPhdr = &programHeaders[i]; + if (programHeaders[i].p_type != PT_LOAD) + continue; + if (!end || programHeaders[i].p_vaddr > (uintptr_t)end) + end = (void*)programHeaders[i].p_vaddr; + } + if (!dynamicPhdr || !end) + return nullptr; + size_t progSize = (((size_t)end) + 0xfff) & ~0xfff; + base = vmm::Allocate(&vmm::g_kernelContext, nullptr, progSize, vmm::FLAGS_GUARD_PAGE_LEFT|vmm::FLAGS_GUARD_PAGE_RIGHT, 0); + end = OffsetPtr(base, progSize); + for (size_t i = 0; i < ehdr->e_phnum; i++) + { + if (programHeaders[i].p_type != PT_LOAD) + continue; + void* phdrBase = (void*)OffsetPtr(base, programHeaders[i].p_vaddr); + memcpy(phdrBase, OffsetPtr(data, programHeaders[i].p_offset), programHeaders[i].p_filesz); + if (programHeaders[i].p_memsz - programHeaders[i].p_filesz) + memzero(OffsetPtr(phdrBase, programHeaders[i].p_filesz), programHeaders[i].p_memsz - programHeaders[i].p_filesz); + } + dynamicHdr = OffsetPtr((const Elf64_Dyn*)data, dynamicPhdr->p_offset); + if (!ApplyRelocations(ehdr, dynamicHdr, base)) + { + // Uh oh... + // We couldn't apply relocations on this driver. + vmm::Free(&vmm::g_kernelContext, base, progSize); + return nullptr; + } + for (size_t i = 0; i < ehdr->e_phnum; i++) + { + if (programHeaders[i].p_type != PT_LOAD) + continue; + void* phdrBase = (void*)OffsetPtr(base, programHeaders[i].p_vaddr); + vmm::prot_t prot = 0; + if (programHeaders[i].p_flags & PF_X) + prot |= vmm::PROT_EXECUTE; + if ((programHeaders[i].p_flags & PF_R) && !(programHeaders[i].p_flags & PF_W)) + prot |= vmm::PROT_READ_ONLY; + vmm::SetProtection(&vmm::g_kernelContext, phdrBase, programHeaders[i].p_memsz, prot); + } + return base; + } + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/driver_interface_load.h b/src/oboskrnl/arch/x86_64/driver_interface_load.h new file mode 100644 index 00000000..6b44dd41 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/driver_interface_load.h @@ -0,0 +1,23 @@ +/* + oboskrnl/arch/x86_64/driver_interface_load.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include + +namespace obos +{ + namespace arch + { + /// + /// Loads a dynamic elf file, applying any relocations. + /// + /// The elf file's data. + /// The elf file's size. + /// The module's base on success, otherwise nullptr. + void* LoadDynamicElfFile(const void* data, size_t size); + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/drv_loader.c b/src/oboskrnl/arch/x86_64/drv_loader.c deleted file mode 100644 index 5252052f..00000000 --- a/src/oboskrnl/arch/x86_64/drv_loader.c +++ /dev/null @@ -1,814 +0,0 @@ -/* - * oboskrnl/arch/x86_64/drv_loader.c - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include -#include -#include -#include - -#include - -#include - -#include -#include - -#include -#include -#include -#include - -#include - -#include - -#include -#include -#include -#include - -// Abandon all hope, ye who enter here. - -#define OffsetPtr(ptr, off, type) (type)(((uintptr_t)ptr) + ((intptr_t)off)) -// Please forgive me for this -#define Cast(what, to) ((to)what) - -// Helpers. -struct relocation_table -{ - Elf64_Dyn* table; - size_t sz; - bool rel; // false if rela, true if rel. -}; -struct relocation -{ - uint32_t symbolTableOffset; - uintptr_t virtualAddress; - uint16_t relocationType; - int64_t addend; -}; -struct relocation_array -{ - struct relocation_table* buf; - size_t nRelocations; -}; -struct copy_reloc -{ - void* src, *dest; - size_t size; -}; -struct copy_reloc_array -{ - struct copy_reloc* buf; - size_t nRelocations; -}; -static void append_relocation_table(struct relocation_array* arr, const struct relocation_table* what) -{ - size_t old_sz = arr->nRelocations*sizeof(*arr->buf); - size_t new_sz = (++arr->nRelocations)*sizeof(*arr->buf); - arr->buf = Reallocate(OBOS_KernelAllocator, arr->buf, new_sz, old_sz, nullptr); - OBOS_ASSERT(arr->buf); // oopsies - arr->buf[arr->nRelocations - 1] = *what; - // Note: - // Since you'll probably forget what you were doing: - // You were making relocation tables in the array be stored as the struct relocation_table instead of Elf64_Dyn. - // You're done adapting the array, you only need to adapt the code. - // 5 Hours later: - // thank god I wrote this comment. -} -static void free_reloc_array(struct relocation_array* arr) -{ - Free(OBOS_KernelAllocator, arr->buf, arr->nRelocations*sizeof(*arr->buf)); - memzero(arr, sizeof(*arr)); -} -static void append_copy_reloc(struct copy_reloc_array* arr, const struct copy_reloc* what) -{ - size_t old_sz = arr->nRelocations*sizeof(*arr->buf); - size_t new_sz = (++arr->nRelocations)*sizeof(*arr->buf); - arr->buf = Reallocate(OBOS_KernelAllocator, arr->buf, new_sz, old_sz, nullptr); - OBOS_ASSERT(arr->buf); // oopsies - arr->buf[arr->nRelocations - 1] = *what; -} -static void free_copy_reloc_array(struct copy_reloc_array* arr) -{ - Free(OBOS_KernelAllocator, arr->buf, arr->nRelocations*sizeof(*arr->buf)); - memzero(arr, sizeof(*arr)); -} - -static uint32_t GnuHash(const char* name) -{ - uint32_t h = 5381 /* oooo magic number */; - - while (*name) - h = ((h<<5)+h) + *(name++); // h*33 + *name++ - - return h; -} - -#define getEntryOffset(ptr, type, offset) (*((type*)((uintptr_t)(ptr) + (offset)))) -static OBOS_NO_UBSAN Elf64_Sym* GetSymbolFromGnuTable( - uint8_t* fileStart, - uint8_t* baseAddress, - Elf64_Sym* symbolTable, - uintptr_t gnuHashTableOffset, - Elf64_Off stringTable, - const char* _symbol) -{ - void* base = baseAddress+gnuHashTableOffset; - - uint32_t nbuckets = getEntryOffset(base, uint32_t, 0); - uint32_t symoffset = getEntryOffset(base, uint32_t, 4); - uint32_t bloom_size = getEntryOffset(base, uint32_t, 8); - uint32_t bloom_shift = getEntryOffset(base, uint32_t, 12); - // NOTE: 32-bits on a 32-bit arch, if this code is ever copied. - uint64_t* bloom = &getEntryOffset(base, uint64_t, 16); - uint32_t* buckets = &getEntryOffset(base, uint32_t, 16+bloom_size*sizeof(*bloom)); - uint32_t* chain = &getEntryOffset(base, uint32_t, 16+bloom_size*sizeof(*bloom)+nbuckets*sizeof(uint32_t)); - - uint32_t hash = GnuHash(_symbol); - - // Look at the bloom table (ooo fancy) - uint64_t bloom_value = bloom[hash/64 % bloom_size]; - - // OpenBSD, you confuse me greatly. - uint32_t h1 = hash % 64; - uint32_t h2 = (hash >> bloom_shift) % 64; - - if (((bloom_value >> h1) & (bloom_value >> h2) & 1) == 0) - return nullptr; // The symbol definitely does not exist. - - // First, fetch the bucket. - uint32_t bucket = buckets[hash % nbuckets]; - if (bucket < symoffset) - return nullptr; // bruh - - - for (uint32_t currhash = chain[bucket-symoffset]; true; currhash = chain[++bucket-symoffset]) - { - Elf64_Sym* symbol = &symbolTable[bucket]; - const char* name = stringTable + (char*)fileStart + symbol->st_name; - - if (((hash|1) == (currhash|1)) && strcmp(name, _symbol)) - return symbol; - - if (currhash & 1) - return nullptr; - } - - return nullptr; -} -// From https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-48031.html#scrolltoc -static uint32_t ElfHash(const char* name) -{ - uint32_t h = 0, g = 0; - - while (*name) - { - h = (h << 4) + *name++; - if ((g = h & 0xf0000000)) - h ^= g >> 24; - h &= ~g; - } - return h; -} -static Elf64_Sym* GetSymbolFromTable( - uint8_t* fileStart, - uint8_t* baseAddress, - Elf64_Sym* symbolTable, - uintptr_t hashTableOff, - uintptr_t gnuHashTableOffset, - Elf64_Off stringTable, - const char* _symbol) -{ - if (gnuHashTableOffset) - return GetSymbolFromGnuTable(fileStart, baseAddress, symbolTable, gnuHashTableOffset, stringTable, _symbol); - Elf64_Word* hashTableBase = (Elf64_Word*)(baseAddress + hashTableOff); - Elf64_Word nBuckets = hashTableBase[0]; - uint32_t currentBucket = ElfHash(_symbol) % nBuckets; - Elf64_Word* firstBucket = hashTableBase + 2; - Elf64_Word* firstChain = firstBucket + nBuckets; - size_t index = firstBucket[currentBucket]; - while (index) - { - Elf64_Sym* symbol = &symbolTable[index]; - const char* symbolName = (char*)(fileStart + stringTable + symbol->st_name); - if (strcmp(symbolName, _symbol)) - return symbol; - - index = firstChain[index]; - } - return nullptr; -} - -static void add_dependency(driver_id* depends, driver_id* dependency) -{ - if (!dependency || !depends) - return; - // depends is the driver that depends on the dependency. - for (driver_node* cur = depends->dependencies.head; cur; ) - { - if (cur->data == dependency) - return; // don't add an already added dependency to the list - cur = cur->next; - } - driver_node* node = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(driver_node), nullptr); - node->data = dependency; - driver_list* list = &depends->dependencies; - if (!list->head) - list->head = node; - if (list->tail) - list->tail->next = node; - node->prev = list->tail; - list->tail = node; - list->nNodes++; - dependency->refCnt++; -} -static bool calculate_relocation(obos_status* status, driver_id* drv, Elf64_Sym* symbolTable, Elf64_Off stringTable, const void* file, struct relocation i, void* base, size_t szProgram, Elf64_Addr* GOT, struct copy_reloc_array* copy_relocations, uintptr_t hashTableOffset, uintptr_t gnuHashTableOffset, bool *usesUacpiSymbol) -{ - driver_symbol* Symbol = nullptr; - driver_symbol internal_symbol = {}; - if (i.symbolTableOffset) - { - Elf64_Sym* Unresolved_Symbol = &symbolTable[i.symbolTableOffset]; - driver_id* dependency = nullptr; - Symbol = DrvH_ResolveSymbol( - OffsetPtr(base, stringTable + Unresolved_Symbol->st_name, const char*), - &dependency - ); - if (Symbol && Symbol->visibility != SYMBOL_VISIBILITY_DEFAULT) - { - if (status) - *status = OBOS_STATUS_DRIVER_REFERENCED_UNRESOLVED_SYMBOL; - OBOS_Log("Could not resolve symbol '%s' (symbol is hidden) referenced within a driver.\n", OffsetPtr(base, stringTable + Unresolved_Symbol->st_name, const char*)); - Mm_VirtualMemoryFree(&Mm_KernelContext, base, szProgram); - return false; - } - if (!Symbol) - { - Elf64_Sym* sym = GetSymbolFromTable( - (uint8_t*)file, base, symbolTable, hashTableOffset, gnuHashTableOffset, stringTable, - OffsetPtr(base, stringTable + Unresolved_Symbol->st_name, const char*) - ); - if (!obos_expect((uintptr_t)sym != 0, 1)) - goto unresolved; - if (sym->st_shndx == 0) - goto unresolved; - internal_symbol.address = OffsetPtr(base, sym->st_value, uintptr_t); - internal_symbol.size = sym->st_size; - internal_symbol.visibility = SYMBOL_VISIBILITY_DEFAULT; - switch (ELF64_ST_TYPE(sym->st_info)) - { - case STT_FUNC: - internal_symbol.type = SYMBOL_TYPE_FUNCTION; - break; - case STT_FILE: - internal_symbol.type = SYMBOL_TYPE_FILE; - break; - case STT_OBJECT: - internal_symbol.type = SYMBOL_TYPE_VARIABLE; - break; - default: - break; - } - Symbol = &internal_symbol; - } - unresolved: - if (!Symbol && ELF64_ST_BIND(Unresolved_Symbol->st_info) != STB_WEAK) - { - if (status) - *status = OBOS_STATUS_DRIVER_REFERENCED_UNRESOLVED_SYMBOL; - OBOS_Log("Could not resolve symbol '%s' referenced within a driver.\n", OffsetPtr(base, stringTable + Unresolved_Symbol->st_name, const char*)); - Mm_VirtualMemoryFree(&Mm_KernelContext, base, szProgram); - return false; - } - // If this is a uacpi symbol, then we want to check against the init level, if valid. - if (!(*usesUacpiSymbol) && (Symbol && (uacpi_strncmp(Symbol->name, "uacpi_", 6) == 0))) - { - static const char* const uacpi_stdlib_symbols[] = { - "uacpi_memcpy", - "uacpi_memset", - "uacpi_memmove", - "uacpi_memcmp", - "uacpi_strcmp", - "uacpi_strncmp", - "uacpi_strnlen", - "uacpi_strlen", - "uacpi_snprintf", - }; - for (size_t i = 0; i < sizeof(uacpi_stdlib_symbols)/sizeof(uacpi_stdlib_symbols[0]); i++) - if (strcmp(Symbol->name, uacpi_stdlib_symbols[i])) - goto cont; - // Zeroed [by the kernel] if it does not exist. - if (drv->header.uacpi_init_level_required && drv->header.uacpi_init_level_required > uacpi_get_current_init_level()) - { - if (status) - *status = OBOS_STATUS_INVALID_INIT_PHASE; - OBOS_Debug("Driver attempted to use uacpi symbol %s. Note: Requested init level is %s.\n", Symbol->name, uacpi_init_level_to_string(drv->header.uacpi_init_level_required)); - Mm_VirtualMemoryFree(&Mm_KernelContext, base, szProgram); - return false; - } - *usesUacpiSymbol = true; - } - cont: - add_dependency(drv, dependency); - if (ELF64_ST_BIND(Unresolved_Symbol->st_info) == STB_WEAK) - { - internal_symbol.size = Unresolved_Symbol->st_size; - internal_symbol.address = 0; - switch (ELF64_ST_TYPE(Unresolved_Symbol->st_info)) - { - case STT_FUNC: - internal_symbol.type = SYMBOL_TYPE_FUNCTION; - break; - case STT_FILE: - internal_symbol.type = SYMBOL_TYPE_FILE; - break; - case STT_OBJECT: - internal_symbol.type = SYMBOL_TYPE_VARIABLE; - break; - default: - break; - } - internal_symbol.visibility = SYMBOL_VISIBILITY_DEFAULT; - Symbol = &internal_symbol; - } - if (Unresolved_Symbol->st_size != Symbol->size && i.relocationType == R_AMD64_COPY) - { - // Oh no! - if (status) - *status = OBOS_STATUS_DRIVER_SYMBOL_MISMATCH; - Mm_VirtualMemoryFree(&Mm_KernelContext, base, szProgram); - return false; - } - } - int type = i.relocationType; - uintptr_t relocAddr = (uintptr_t)base + i.virtualAddress; - uint64_t relocResult = 0; - uint8_t relocSize = 0; - switch (type) - { - case R_AMD64_NONE: - return true; - case R_AMD64_64: - OBOS_ASSERT(Symbol); - relocResult = Symbol->address + i.addend; - relocSize = 8; - break; - case R_AMD64_PC32: - OBOS_ASSERT(Symbol); - relocResult = Symbol->address + i.addend - relocAddr; - relocSize = 4; - break; - case R_AMD64_GOT32: - if (status) - *status = OBOS_STATUS_UNIMPLEMENTED; - return false; - // TODO: Replace the zero in the calculation with "G" (see elf spec for more info). - relocResult = 0 + i.addend; - relocSize = 4; - break; - case R_AMD64_PLT32: - if (status) - *status = OBOS_STATUS_UNIMPLEMENTED; - Mm_VirtualMemoryFree(&Mm_KernelContext, base, szProgram); - return false; - // TODO: Replace the zero in the calculation with "L" (see elf spec for more info). - relocResult = 0 + i.addend - relocAddr; - relocSize = 4; - break; - case R_AMD64_COPY: - { - // Save copy relocations for the end because if we don't, it might contain unresolved addresses. - // copy_relocations.push_back({ (void*)relocAddr, (void*)Symbol->address, Symbol->size }); - OBOS_ASSERT(Symbol); - struct copy_reloc reloc = { (void*)relocAddr, (void*)Symbol->address, Symbol->size }; - append_copy_reloc(copy_relocations, &reloc); - relocSize = 0; - break; - } - case R_AMD64_JUMP_SLOT: - case R_AMD64_GLOB_DAT: - OBOS_ASSERT(Symbol); - relocResult = Symbol->address; - relocSize = 8; - break; - case R_AMD64_RELATIVE: - relocResult = (uint64_t)base + i.addend; - relocSize = 8; - break; - case R_AMD64_GOTPCREL: - if (status) - *status = OBOS_STATUS_UNIMPLEMENTED; - Mm_VirtualMemoryFree(&Mm_KernelContext, base, szProgram); - return false; - // TODO: Replace the zero in the calculation with "G" (see elf spec for more info). - relocResult = 0 + (uint64_t)base + i.addend - relocAddr; - relocSize = 8; - break; - case R_AMD64_32: - OBOS_ASSERT(Symbol); - relocResult = Symbol->address + i.addend; - relocSize = 4; - break; - case R_AMD64_32S: - OBOS_ASSERT(Symbol); - relocResult = Symbol->address + i.addend; - relocSize = 4; - break; - case R_AMD64_16: - OBOS_ASSERT(Symbol); - relocResult = Symbol->address + i.addend; - relocSize = 2; - break; - case R_AMD64_PC16: - OBOS_ASSERT(Symbol); - relocResult = Symbol->address + i.addend - relocAddr; - relocSize = 2; - break; - case R_AMD64_8: - OBOS_ASSERT(Symbol); - relocResult = Symbol->address + i.addend; - relocSize = 1; - break; - case R_AMD64_PC8: - OBOS_ASSERT(Symbol); - relocResult = Symbol->address + i.addend - relocAddr; - relocSize = 1; - break; - case R_AMD64_PC64: - OBOS_ASSERT(Symbol); - relocResult = Symbol->address + i.addend - relocAddr; - relocSize = 8; - break; - case R_AMD64_GOTOFF64: - OBOS_ASSERT(Symbol); - relocResult = Symbol->address + i.addend - ((uint64_t)GOT); - relocSize = 8; - break; - case R_AMD64_GOTPC32: - relocResult = (uint64_t)GOT + i.addend + relocAddr; - relocSize = 8; - break; - case R_AMD64_SIZE32: - relocSize = 4; - OBOS_ASSERT(Symbol); - relocResult = Symbol->size + i.addend; - break; - case R_AMD64_SIZE64: - relocSize = 8; - OBOS_ASSERT(Symbol); - relocResult = Symbol->size + i.addend; - break; - default: - break; // Ignore. - } - switch (relocSize) - { - case 0: - break; // The relocation type is rather unsupported or special. - case 1: // word8 - *(uint8_t*)(relocAddr) = (uint8_t)(relocResult & 0xff); - break; - case 2: // word16 - *(uint16_t*)(relocAddr) = (uint16_t)(relocResult & 0xffff); - break; - case 4: // word32 - *(uint32_t*)(relocAddr) = (uint32_t)(relocResult & 0xffffffff); - break; - case 8: // word64 - *(uint64_t*)(relocAddr) = relocResult; - break; - default: - break; - } - return true; -} - -static OBOS_NO_UBSAN size_t get_dynsym_entry_count(Elf64_Word* hashTable) -{ - size_t nEntriesDynamicSymbolTable = 0; - - uint32_t nbuckets = getEntryOffset(hashTable, uint32_t, 0); - uint32_t symoffset = getEntryOffset(hashTable, uint32_t, 4); - uint32_t bloom_size = getEntryOffset(hashTable, uint32_t, 8); - // NOTE: 32-bits on a 32-bit arch, if this code is ever copied. - uint64_t* bloom = &getEntryOffset(hashTable, uint64_t, 16); - uint32_t* buckets = &getEntryOffset(hashTable, uint32_t, 16+bloom_size*sizeof(*bloom)); - uint32_t* chain = &getEntryOffset(hashTable, uint32_t, 16+bloom_size*sizeof(*bloom)+nbuckets*sizeof(uint32_t)); - - // Credit to mlibc - for (uint32_t i = 0; i < nbuckets; i++) - { - if (nEntriesDynamicSymbolTable < buckets[i]) - nEntriesDynamicSymbolTable = buckets[i]; - while (!(chain[nEntriesDynamicSymbolTable - symoffset] & 1)) - nEntriesDynamicSymbolTable++; - } - return nEntriesDynamicSymbolTable + 1; -} - -void* DrvS_LoadRelocatableElf(driver_id* driver, const void* file, size_t szFile, Elf_Sym** dynamicSymbolTable, size_t* nEntriesDynamicSymbolTable, const char** dynstrtab, void** top, obos_status* status) -{ - OBOS_UNUSED(szFile); - Elf64_Ehdr* ehdr = Cast(file, Elf64_Ehdr*); - Elf64_Phdr* phdr_table = OffsetPtr(ehdr, ehdr->e_phoff, Elf64_Phdr*); - Elf64_Phdr* dynamic = nullptr; - size_t szProgram = 0; - void* end = nullptr; - for (size_t i = 0; i < ehdr->e_phnum; i++) - { - if (phdr_table[i].p_type == PT_DYNAMIC) - dynamic = &phdr_table[i]; - if (phdr_table[i].p_type != PT_LOAD && phdr_table[i].p_type != PT_DYNAMIC) - continue; - Elf64_Phdr* curr = &phdr_table[i]; - if (!end || curr->p_vaddr > (uintptr_t)end) - end = (void*)(curr->p_vaddr+curr->p_memsz); - } - szProgram = (size_t)end; - szProgram = (szProgram + 0xfff) & ~0xfff; - void* base = Mm_VirtualMemoryAlloc(&Mm_KernelContext, nullptr, szProgram, 0, 0, nullptr, status); - if (!base) - return nullptr; - // The pages are mapped in. - // Copy the phdr data into them. - for (size_t i = 0; i < ehdr->e_phnum; i++) - { - if (phdr_table[i].p_type != PT_LOAD) - continue; - Elf64_Phdr* curr = &phdr_table[i]; - memcpy(OffsetPtr(base, curr->p_vaddr, void*), OffsetPtr(file, curr->p_offset, void*), curr->p_filesz); - // NOTE: Possible buffer overflow - memzero(OffsetPtr(base, curr->p_vaddr + curr->p_offset, void*), curr->p_memsz - curr->p_filesz); - } - OBOS_ASSERT(obos_expect((uintptr_t)dynamic != 0, 1)); - // Apply relocations. - struct relocation_array relocations = {.buf=nullptr,.nRelocations=0}; - Elf64_Sym* symbolTable = 0; - Elf64_Off stringTable = 0; - uintptr_t hashTableOffset = 0; - uintptr_t gnuHashTableOffset = 0; - Elf64_Addr* GOT = nullptr; - Elf64_Dyn* currentDynamicHeader = OffsetPtr(file, dynamic->p_offset, Elf64_Dyn*); - size_t last_dtrelasz = 0, last_dtrelsz = 0, last_dtpltrelsz = 0; - bool awaitingRelaSz = false, foundRelaSz = false; - Elf64_Dyn* dynEntryAwaitingRelaSz = nullptr; - bool awaitingRelSz = false, foundRelSz = false; - Elf64_Dyn* dynEntryAwaitingRelSz = nullptr; - bool awaitingPltRelSz = false, foundPltRelSz = false; - Elf64_Dyn* dynEntryAwaitingPltRelSz = nullptr; - uint64_t last_dlpltrel = 0xffffffff; - struct relocation_table current = {}; - for (size_t i = 0; currentDynamicHeader->d_tag != DT_NULL; i++, currentDynamicHeader++) - { - OBOS_UNUSED(i); - switch (currentDynamicHeader->d_tag) - { - case DT_HASH: - hashTableOffset = currentDynamicHeader->d_un.d_ptr; - break; - case DT_GNU_HASH: - gnuHashTableOffset = currentDynamicHeader->d_un.d_ptr; - break; - case DT_PLTGOT: - // TODO: Find out whether this is the PLT or GOT (if possible). - GOT = (Elf64_Addr*)(base + currentDynamicHeader->d_un.d_ptr); - break; - case DT_REL: - if (!foundRelSz) - { - awaitingRelSz = true; - dynEntryAwaitingRelSz = currentDynamicHeader; - break; - } - current.rel = true; - current.table = currentDynamicHeader; - current.sz = last_dtrelsz; - append_relocation_table(&relocations, ¤t); - foundRelSz = false; - last_dtrelsz = 0; - break; - case DT_RELA: - if (!foundRelaSz) - { - awaitingRelaSz = true; - dynEntryAwaitingRelaSz = currentDynamicHeader; - break; - } - current.rel = false; - current.table = currentDynamicHeader; - current.sz = last_dtrelasz; - append_relocation_table(&relocations, ¤t); - foundRelaSz = false; - last_dtrelasz = 0; - break; - case DT_JMPREL: - if (!foundPltRelSz) - { - awaitingPltRelSz = true; - dynEntryAwaitingPltRelSz = currentDynamicHeader; - break; - } - switch (last_dlpltrel) - { - case DT_REL: - current.rel = true; - current.table = currentDynamicHeader; - current.sz = last_dtpltrelsz; - append_relocation_table(&relocations, ¤t); - break; - case DT_RELA: - current.rel = false; - current.table = currentDynamicHeader; - current.sz = last_dtpltrelsz; - append_relocation_table(&relocations, ¤t); - break; - default: - break; - } - break; - case DT_RELSZ: - last_dtrelsz = currentDynamicHeader->d_un.d_val; - foundRelSz = !awaitingRelSz; - if (awaitingRelSz) - { - current.rel = true; - current.table = dynEntryAwaitingRelSz; - current.sz = last_dtrelsz; - append_relocation_table(&relocations, ¤t); - awaitingRelSz = false; - } - break; - case DT_RELASZ: - last_dtrelasz = currentDynamicHeader->d_un.d_val; - foundRelaSz = !awaitingRelaSz; - if (awaitingRelaSz) - { - current.rel = false; - current.table = dynEntryAwaitingRelaSz; - current.sz = last_dtrelasz; - append_relocation_table(&relocations, ¤t); - awaitingRelaSz = false; - } - break; - case DT_PLTREL: - last_dlpltrel = currentDynamicHeader->d_un.d_val; - if (awaitingPltRelSz) - { - switch (last_dlpltrel) - { - case DT_REL: - current.rel = true; - current.table = dynEntryAwaitingPltRelSz; - current.sz = last_dtpltrelsz; - append_relocation_table(&relocations, ¤t); - break; - case DT_RELA: - current.rel = false; - current.table = dynEntryAwaitingPltRelSz; - current.sz = last_dtpltrelsz; - append_relocation_table(&relocations, ¤t); - break; - default: - break; - } - awaitingPltRelSz = false; - } - foundPltRelSz = true; - break; - case DT_PLTRELSZ: - last_dtpltrelsz = currentDynamicHeader->d_un.d_val; - if (awaitingPltRelSz && (last_dlpltrel != UINT32_MAX)) - { - switch (last_dlpltrel) - { - case DT_REL: - current.rel = true; - current.table = dynEntryAwaitingPltRelSz; - current.sz = last_dtpltrelsz; - append_relocation_table(&relocations, ¤t); - break; - case DT_RELA: - current.rel = false; - current.table = dynEntryAwaitingPltRelSz; - current.sz = last_dtpltrelsz; - append_relocation_table(&relocations, ¤t); - break; - default: - break; - } - awaitingPltRelSz = false; - } - foundPltRelSz = (last_dlpltrel != UINT32_MAX); - break; - case DT_STRTAB: - stringTable = currentDynamicHeader->d_un.d_ptr; - break; - case DT_SYMTAB: - symbolTable = (Elf64_Sym*)(base + currentDynamicHeader->d_un.d_ptr); - break; - default: - break; - } - } - - struct copy_reloc_array copy_relocations = {0,0}; - bool usesUacpiSymbol = false; - for (size_t index = 0; index < relocations.nRelocations; index++) - { - struct relocation_table table = relocations.buf[index]; - Elf64_Rel* relTable = (Elf64_Rel*)(base + table.table->d_un.d_ptr); - Elf64_Rela* relaTable = (Elf64_Rela*)(base + table.table->d_un.d_ptr); - for (size_t i = 0; i < table.sz / (table.rel ? sizeof(Elf64_Rel) : sizeof(Elf64_Rela)); i++) - { - struct relocation cur; - if (!table.rel) - { - cur.symbolTableOffset = (uint32_t)(relaTable[i].r_info >> 32), - cur.virtualAddress = relaTable[i].r_offset, - cur.relocationType = (uint16_t)(relaTable[i].r_info & 0xffff), - cur.addend = relaTable[i].r_addend; - } - else - { - cur.symbolTableOffset = (uint32_t)(relTable[i].r_info >> 32), - cur.virtualAddress = relTable[i].r_offset, - cur.relocationType = (uint16_t)(relTable[i].r_info & 0xffff), - cur.addend = 0; - } - if (!calculate_relocation(status, - driver, - symbolTable, - stringTable, - file, - cur, - base, - szProgram, - GOT, - ©_relocations, - hashTableOffset, - gnuHashTableOffset, - &usesUacpiSymbol)) - return nullptr; - } - } - for (size_t i = 0; i < copy_relocations.nRelocations; i++) - { - struct copy_reloc reloc = copy_relocations.buf[i]; - memcpy(reloc.dest, reloc.src, reloc.size); - } - - free_copy_reloc_array(©_relocations); - free_reloc_array(&relocations); - - // Set protection. - for (size_t i = 0; i < ehdr->e_phnum; i++) - { - if (phdr_table[i].p_type != PT_LOAD) - continue; - void* phdrBase = OffsetPtr(base, phdr_table[i].p_vaddr, void*); - phdrBase = (void*)((uintptr_t)phdrBase & ~0xfff); - prot_flags prot = 0; - bool isPageable = false; - if (phdr_table[i].p_flags & PF_X) - prot |= OBOS_PROTECTION_EXECUTABLE; - if ((phdr_table[i].p_flags & PF_R) && !(phdr_table[i].p_flags & PF_W)) - prot |= OBOS_PROTECTION_READ_ONLY; - if (phdr_table[i].p_flags & PF_OBOS_PAGEABLE) - isPageable = true; - else - isPageable = false; - Mm_VirtualMemoryProtect(&Mm_KernelContext, phdrBase, phdr_table[i].p_memsz, prot, isPageable); - } - if (status) - *status = OBOS_STATUS_SUCCESS; - if (dynamicSymbolTable) - *dynamicSymbolTable = symbolTable; - if (nEntriesDynamicSymbolTable) - { - if (gnuHashTableOffset) - { - Elf64_Word* hashTable = OffsetPtr(file, gnuHashTableOffset, Elf64_Word*); - *nEntriesDynamicSymbolTable = get_dynsym_entry_count(hashTable); - } - else if (hashTableOffset) - { - Elf64_Word* hashTable = OffsetPtr(base, hashTableOffset, Elf64_Word*); - *nEntriesDynamicSymbolTable = hashTable[1]; - } - else - { - // There is no possible way to know the size without a hash table. - - *nEntriesDynamicSymbolTable = 0; - } - } - if (dynstrtab) - *dynstrtab = OffsetPtr(base, stringTable, const char*); - if (top) - *top = OffsetPtr(base, end, void*); - return base; -} diff --git a/src/oboskrnl/arch/x86_64/entry.asm b/src/oboskrnl/arch/x86_64/entry.asm deleted file mode 100644 index 79176293..00000000 --- a/src/oboskrnl/arch/x86_64/entry.asm +++ /dev/null @@ -1,75 +0,0 @@ -; oboskrnl/arch/x86_64/entry.asm - -; Copyright (c) 2024 Omar Berrow - -[BITS 64] -[DEFAULT ABS] - -default rel - -; Bootstrap code to initialize the stack guard. - -extern __stack_chk_guard -extern Arch_disablePIC -extern Arch_KernelEntry -global Arch_KernelEntryBootstrap:function hidden -Arch_KernelEntryBootstrap: -%if !OBOS_USE_LIMINE - cmp rsi, 0x554c5442 - je .ok ; should triple fault on failure - xor rax,rax - jmp rax ; All hope is lost. -%endif -.ok: - push rdi - push rsi -; Get a hardware-generated random value. -; If both methods are unsupported, it will fallback to using the default value used. -.rdrand: - mov eax, 1 - xor ecx,ecx - cpuid - bt ecx, 30 - jnc .rdseed - rdrand rax - jnc .rdrand - jmp .move -.rdseed: - mov eax, 7 - xor ecx,ecx - cpuid - bt ebx, 18 - jnc .done - rdseed rax - jnc .rdseed -.move: - ; Move the (likely) random value into the stack guard variable. - mov [__stack_chk_guard], rax -.done: - call Arch_disablePIC - ; Turn on cr0.WP (write protect) - mov rax, cr0 - or rax, (1<<16) ; WP - mov cr0, rax - ; Restore rdi and rsi - pop rsi - pop rdi - ; Call into the kernel entry. - push 0 ; Make sure if the kernel entry returns, it triple faults and doesn't do goofy things. - jmp Arch_KernelEntry -global Arch_IdleTask:function hidden -section .data -global Arch_MakeIdleTaskSleep:data hidden -Arch_MakeIdleTaskSleep: db 0 -section .text -Arch_IdleTask: - hlt - cmp byte [Arch_MakeIdleTaskSleep], 1 - jne Arch_IdleTask - cli -.slp: - pause - cmp byte [Arch_MakeIdleTaskSleep], 1 - je .slp - sti - jmp Arch_IdleTask diff --git a/src/oboskrnl/arch/x86_64/entry.c b/src/oboskrnl/arch/x86_64/entry.c deleted file mode 100644 index 8ceff5c5..00000000 --- a/src/oboskrnl/arch/x86_64/entry.c +++ /dev/null @@ -1,773 +0,0 @@ -/* - * oboskrnl/arch/x86_64/entry.c - * - * Copyright (c) 2024-2026 Omar Berrow -*/ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define FLANTERM_IN_FLANTERM -#include -#include - -#include - -#include -#include -#include -#include - -#include -#include - -#include -#include -#include - -#include - -#include - -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include - -#include - -#if OBOS_USE_LIMINE -# include -#endif - -extern void Arch_InitBootGDT(); - -static char thr_stack[0x4000]; -static char kmain_thr_stack[0x40000]; -extern char Arch_InitialISTStack[0x20000]; -static thread bsp_idleThread; - -static thread kernelMainThread; -volatile struct ultra_boot_context* Arch_BootContext; - -obos_status Arch_MapHugePage(uintptr_t cr3, void* at_, uintptr_t phys, uintptr_t flags, bool free_pte); - -static cpu_local bsp_cpu; -extern void Arch_IdleTask(); -void Arch_CPUInitializeGDT(cpu_local* info, uintptr_t istStack, size_t istStackSize); -void Arch_KernelMainBootstrap(); -static void ParseBootContext(struct ultra_boot_context* bcontext); -void Arch_InstallExceptionHandlers(); - -struct stack_frame -{ - struct stack_frame* down; - uintptr_t rip; -}; - -static const char* color_to_ansi[] = { - "\x1b[30m", - "\x1b[34m", - "\x1b[32m", - "\x1b[36m", - "\x1b[31m", - "\x1b[35m", - "\x1b[38;5;52m", - "\x1b[38;5;7m", - "\x1b[38;5;8m", - "\x1b[38;5;12m", - "\x1b[38;5;10m", - "\x1b[38;5;14m", - "\x1b[38;5;9m", - "\x1b[38;5;13m", - "\x1b[38;5;11m", - "\x1b[38;5;15m", -}; - -OBOS_NO_UBSAN stack_frame OBOSS_StackFrameNext(stack_frame curr) -{ - if (!curr) - { - curr = __builtin_frame_address(0); - if (curr->down) - curr = curr->down; // use caller's stack frame, if available - return curr; - } - if (!KASAN_IsAllocated((uintptr_t)&curr->down, sizeof(*curr), false)) - return nullptr; - return curr->down; -} -OBOS_NO_UBSAN uintptr_t OBOSS_StackFrameGetPC(stack_frame curr) -{ - if (!curr) - { - curr = __builtin_frame_address(0); - if (curr->down) - curr = curr->down; // use caller's stack frame, if available - } - if (!KASAN_IsAllocated((uintptr_t)&curr->rip, sizeof(*curr), false)) - return 0; - return curr->rip; -} - -static void e9_out(const char *str, size_t sz, void* userdata) -{ - OBOS_UNUSED(userdata); - for (size_t i = 0; i < sz; i++) - outb(0xe9, str[i]); -} - -static void e9_set_color(color c, void* unused) -{ - e9_out(color_to_ansi[c], strlen(color_to_ansi[c]), unused); -} -static void e9_reset_color(void* unused) -{ - e9_out("\x1b[0m", 4, unused); -} - -const uintptr_t Arch_cpu_local_curr_offset = offsetof(cpu_local, curr); -const uintptr_t Arch_cpu_local_currentIrql_offset = offsetof(cpu_local, currentIrql); - -struct flanterm_context* OBOS_FlantermContext = nullptr; -static bool disable_flanterm_backend = false; -static void flanterm_cb_set_color(color c, void*) -{ - if (disable_flanterm_backend) return; - flanterm_write(OBOS_FlantermContext, color_to_ansi[c], strlen(color_to_ansi[c])); -} -static void flanterm_cb_reset_color(void*) -{ - if (disable_flanterm_backend) return; - flanterm_write(OBOS_FlantermContext, "\x1b[0m", 4); -} -static void flanterm_cb_out(const char* str, size_t sz, void*) -{ - if (disable_flanterm_backend) return; - flanterm_write(OBOS_FlantermContext, str, sz); -} -static void init_flanterm_backend() -{ - log_backend backend = {}; - backend.reset_color = flanterm_cb_reset_color; - backend.set_color = flanterm_cb_set_color; - backend.write = flanterm_cb_out; - OBOS_AddLogSource(&backend); -} - -static struct { - uintptr_t address; - uintptr_t size; -} Arch_FlantermBumpAllocator = {}; -static void *flanterm_malloc(size_t sz) -{ - static size_t bump_off = 0; - if ((bump_off + sz) > Arch_FlantermBumpAllocator.size) - return nullptr; - void* ret = (void*)(Arch_FlantermBumpAllocator.address + bump_off); - bump_off += sz; - return ret; -} -static void flanterm_free(void* blk, size_t sz) -{ - OBOS_UNUSED(blk && sz); - return; -} - -#if OBOS_USE_LIMINE -volatile struct limine_framebuffer_request Arch_LimineFBRequest = { - .id = LIMINE_FRAMEBUFFER_REQUEST_ID, - .revision = 1, -}; -volatile struct limine_memmap_request Arch_LimineMemmapRequest = { - .id = LIMINE_MEMMAP_REQUEST_ID, - .revision = 0, -}; -volatile struct limine_module_request Arch_LimineModuleRequest = { - .id = LIMINE_MODULE_REQUEST_ID, - .revision = 1, -}; -volatile struct limine_hhdm_request Arch_LimineHHDMRequest = { - .id = LIMINE_HHDM_REQUEST_ID, - .revision = 0, -}; -volatile struct limine_executable_file_request Arch_LimineKernelInfoRequest = { - .id = LIMINE_EXECUTABLE_FILE_REQUEST_ID, - .revision = 0, -}; -volatile struct limine_executable_address_request Arch_LimineKernelAddressRequest = { - .id = LIMINE_EXECUTABLE_ADDRESS_REQUEST_ID, - .revision = 0, -}; -volatile struct limine_executable_cmdline_request Arch_LimineKernelCmdlineRequest = { - .id = LIMINE_EXECUTABLE_CMDLINE_REQUEST_ID, - .revision = 0, -}; -volatile struct limine_rsdp_request Arch_LimineRSDPRequest = { - .id = LIMINE_RSDP_REQUEST_ID, - .revision = 0, -}; -volatile struct limine_bootloader_info_request Arch_LimineBtldrInfoRequest = { - .id = LIMINE_BOOTLOADER_INFO_REQUEST_ID, - .revision = 0, -}; -volatile struct ultra_framebuffer limine_fb0 = {}; -volatile struct ultra_framebuffer* Arch_Framebuffer = &limine_fb0; -struct limine_file* Arch_KernelBinary = nullptr; -#endif - -uintptr_t Arch_RSDPBase = 0; - -#if !OBOS_USE_LIMINE -OBOS_PAGEABLE_FUNCTION void __attribute__((no_stack_protector)) Arch_KernelEntry(struct ultra_boot_context* bcontext) -#else -OBOS_PAGEABLE_FUNCTION void __attribute__((no_stack_protector)) Arch_KernelEntry() -#endif -{ - bsp_cpu.id = 0; - bsp_cpu.isBSP = true; - Core_CpuCount = 1; - Core_CpuInfo = &bsp_cpu; - Core_CpuInfo->curr = Core_CpuInfo; - Core_CpuInfo->currentIrql = 0xf; - - extern uint64_t __stack_chk_guard; - Core_CpuInfo->arch_specific.stack_check_guard = __stack_chk_guard; - - wrmsr(0xC0000101, (uintptr_t)&Core_CpuInfo[0]); - -#if !OBOS_ENABLE_PROFILING - { - uint32_t ecx = 0; - __cpuid__(1, 0, nullptr, nullptr, &ecx, nullptr); - bool isHypervisor = ecx & BIT_TYPE(31, UL) /* Hypervisor bit: Always 0 on physical CPUs. */; - if (isHypervisor) - { - log_backend e9_out_cb = {.write=e9_out,.set_color=e9_set_color,.reset_color=e9_reset_color}; - OBOS_AddLogSource(&e9_out_cb); - } - } -#endif - -#if !OBOS_USE_LIMINE - ParseBootContext(bcontext); - Arch_BootContext = bcontext; -#else - OBOS_KernelCmdLine = Arch_LimineKernelCmdlineRequest.response->cmdline; - Arch_KernelBinary = Arch_LimineKernelInfoRequest.response->executable_file; - Arch_RSDPBase = Arch_UnmapFromHHDM(Arch_LimineRSDPRequest.response->address); -#endif - - basicmm_region bump_region = {}; - struct boot_module bump_region_module = {}; - OBOSS_GetModule(&bump_region_module, "BUMP_REGION"); - if (!bump_region_module.address) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "Could not find BUMP_REGION kernel module\n"); - bump_region.addr = (uintptr_t)bump_region_module.address; - bump_region.size = bump_region_module.size; - OBOSH_BasicMMSetBumpRegion(&bump_region); - - struct boot_module flanterm_buff = {}; - OBOSS_GetModule(&flanterm_buff, "FLANTERM_BUFF"); - if (!flanterm_buff.address) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "Could not find FLANTERM_BUFF kernel module\n"); - Arch_FlantermBumpAllocator.address = (uintptr_t)flanterm_buff.address; - Arch_FlantermBumpAllocator.size = flanterm_buff.size; - - OBOS_ParseCMDLine(); - asm("sti"); - - uint64_t log_level = OBOS_GetOPTD_Ex("log-level", LOG_LEVEL_LOG); - if (log_level > 4) - log_level = LOG_LEVEL_DEBUG; - OBOS_SetLogLevel(log_level); - extern uint64_t Arch_KernelCR3; - Arch_KernelCR3 = getCR3(); - -#if 0 - init_serial_log_backend(); -#endif - - static uint32_t ansi_colors[8] = { - __builtin_bswap32(0x000000) >> 8, - __builtin_bswap32(0xbb0000) >> 8, - __builtin_bswap32(0x00bb00) >> 8, - __builtin_bswap32(0xbbbb00) >> 8, - __builtin_bswap32(0x0000bb) >> 8, - __builtin_bswap32(0xbb00bb) >> 8, - __builtin_bswap32(0x00bbbb) >> 8, - __builtin_bswap32(0xbbbbbb) >> 8, - }; - static uint32_t ansi_bright_colors[8] = { - __builtin_bswap32(0x555555) >> 8, - __builtin_bswap32(0xff5555) >> 8, - __builtin_bswap32(0x55ff55) >> 8, - __builtin_bswap32(0xffff55) >> 8, - __builtin_bswap32(0x5555aa) >> 8, - __builtin_bswap32(0xff55ff) >> 8, - __builtin_bswap32(0x55ffff) >> 8, - __builtin_bswap32(0xffffff) >> 8, - }; - OBOS_UNUSED(ansi_bright_colors && ansi_colors); -#if !OBOS_USE_LIMINE - if (!Arch_Framebuffer) - OBOS_Warning("No framebuffer passed by the bootloader. All kernel logs will be on port 0xE9.\n"); - else - { - uint8_t red_mask_size = 8, red_mask_shift = 0; - uint8_t green_mask_size = 8, green_mask_shift = 0; - uint8_t blue_mask_size = 8, blue_mask_shift = 0; - uint32_t bg = OBOS_TEXT_BACKGROUND; - switch (Arch_Framebuffer->format) { - case ULTRA_FB_FORMAT_BGR888: - red_mask_shift = 16; - green_mask_shift = 8; - blue_mask_shift = 0; - bg = __builtin_bswap32(bg) >> 8; - break; - case ULTRA_FB_FORMAT_RGB888: - red_mask_shift = 0; - green_mask_shift = 8; - blue_mask_shift = 16; - break; - case ULTRA_FB_FORMAT_RGBX8888: - red_mask_shift = 8; - green_mask_shift = 16; - blue_mask_shift = 24; - break; - case ULTRA_FB_FORMAT_XRGB8888: - red_mask_shift = 0; - green_mask_shift = 8; - blue_mask_shift = 16; - bg = bg >> 8; - break; - } - OBOS_FlantermContext = flanterm_fb_init( - flanterm_malloc, flanterm_free, - Arch_MapToHHDM(Arch_Framebuffer->physical_address), Arch_Framebuffer->width, Arch_Framebuffer->height, Arch_Framebuffer->pitch, - red_mask_size, red_mask_shift, - green_mask_size, green_mask_shift, - blue_mask_size, blue_mask_shift, - nullptr, ansi_colors, - ansi_bright_colors, nullptr, - nullptr, nullptr, - nullptr, - (void*)font_bin, 8, 16, 0, - 0,0, - 0); - // while(1); - init_flanterm_backend(); - } - - if (Arch_LdrPlatformInfo->page_table_depth != 4) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "5-level paging is unsupported by oboskrnl.\n"); -#else - if (Arch_LimineFBRequest.response->framebuffer_count) - { - struct limine_framebuffer *fb = Arch_LimineFBRequest.response->framebuffers[0]; - OBOS_FlantermContext = flanterm_fb_init( - flanterm_malloc, flanterm_free, - fb->address, fb->width, fb->height, fb->pitch, - fb->red_mask_size, fb->red_mask_shift, - fb->green_mask_size, fb->green_mask_shift, - fb->blue_mask_size, fb->blue_mask_shift, - nullptr, nullptr, - nullptr, nullptr, - nullptr, nullptr, - nullptr, - (void*)font_bin, 8, 16, 0, - // nullptr, 8, 16, 0, - 0,0, - 0); - init_flanterm_backend(); - limine_fb0.physical_address = Arch_UnmapFromHHDM(fb->address); - limine_fb0.bpp = fb->bpp; - limine_fb0.width = fb->width; - limine_fb0.height = fb->height; - limine_fb0.pitch = fb->pitch; - do { - if (limine_fb0.bpp == 24) - { - if (fb->blue_mask_shift == 0 && fb->green_mask_shift == 8 && fb->red_mask_shift == 16) - limine_fb0.format = ULTRA_FB_FORMAT_RGB888; - if (fb->red_mask_shift == 0 && fb->green_mask_shift == 8 && fb->blue_mask_shift == 16) - limine_fb0.format = ULTRA_FB_FORMAT_BGR888; - break; - } - else if (limine_fb0.bpp == 32) - { - uint8_t x_shift = 0; - if (fb->red_mask_shift == 16) x_shift = 24; - else if (fb->red_mask_shift == 24) x_shift = 0; - if (x_shift == 0 && fb->blue_mask_shift == 8 && fb->green_mask_shift == 16 && fb->red_mask_shift == 24) - limine_fb0.format = ULTRA_FB_FORMAT_RGBX8888; - if (fb->blue_mask_shift == 0 && fb->green_mask_shift == 8 && fb->red_mask_shift == 16 && x_shift == 24) - limine_fb0.format = ULTRA_FB_FORMAT_XRGB8888; - break; - } - } while(0); - } -#endif - - -#if OBOS_RELEASE - OBOS_Log("Booting OBOS %s committed on %s. Build time: %s.\n", GIT_SHA1, GIT_DATE, __DATE__ " " __TIME__); - char cpu_vendor[13] = {0}; - memset(cpu_vendor, 0, 13); - __cpuid__(0, 0, nullptr, (uint32_t*)&cpu_vendor[0],(uint32_t*)&cpu_vendor[8], (uint32_t*)&cpu_vendor[4]); - uint32_t ecx = 0; - __cpuid__(1, 0, nullptr, nullptr, &ecx, nullptr); - bool isHypervisor = ecx & BIT_TYPE(31, UL) /* Hypervisor bit: Always 0 on physical CPUs. */; - char brand_string[49]; - memset(brand_string, 0, sizeof(brand_string)); - __cpuid__(0x80000002, 0, (uint32_t*)&brand_string[0], (uint32_t*)&brand_string[4], (uint32_t*)&brand_string[8], (uint32_t*)&brand_string[12]); - __cpuid__(0x80000003, 0, (uint32_t*)&brand_string[16], (uint32_t*)&brand_string[20], (uint32_t*)&brand_string[24], (uint32_t*)&brand_string[28]); - __cpuid__(0x80000004, 0, (uint32_t*)&brand_string[32], (uint32_t*)&brand_string[36], (uint32_t*)&brand_string[40], (uint32_t*)&brand_string[44]); - OBOS_Log("Running on a %s processor, cpu brand string, %s. We are currently %srunning on a hypervisor\n", cpu_vendor, brand_string, isHypervisor ? "" : "not "); - -#if OBOS_USE_LIMINE - OBOS_Log("Bootloader is %s-%s\n", Arch_LimineBtldrInfoRequest.response->name, Arch_LimineBtldrInfoRequest.response->version); -#else - OBOS_Log("Bootloader is %.32s\n", Arch_LdrPlatformInfo->loader_name, Arch_LdrPlatformInfo->loader_major, Arch_LdrPlatformInfo->loader_minor); -#endif - -#endif - - OBOS_Debug("%s: Initializing the Boot GDT.\n", __func__); - Arch_InitBootGDT(); - - wrmsr(0xC0000101, (uintptr_t)&Core_CpuInfo[0]); - - OBOS_Debug("%s: Initializing the Boot IDT.\n", __func__); - Arch_InitializeIDT(true); - Arch_InstallExceptionHandlers(); - - OBOS_Debug("Enabling XD bit in IA32_EFER.\n"); - do - { - uint32_t edx = 0; - __cpuid__(0x80000001, 0, nullptr, nullptr, nullptr, &edx); - if (edx & (1 << 20)) - wrmsr(0xC0000080 /* IA32_EFER */, rdmsr(0xC0000080) | (1<<11) /* XD Enable */); - } while(0); - do { - uint32_t ecx = 0; - __cpuid__(0x1, 0x0, nullptr, nullptr, &ecx, nullptr); - if (~ecx & (1<<23)) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "OBOS requires popcnt instruction (CPUID.01H:ECX.POPCNT [Bit 23] = 0)\n"); - } while(0); - - OBOS_Debug("%s: Initializing scheduler.\n", __func__); - - thread_ctx ctx1, ctx2; - memzero(&ctx1, sizeof(ctx1)); - memzero(&ctx2, sizeof(ctx2)); - CoreS_SetupThreadContext(&ctx2, (uintptr_t)Arch_KernelMainBootstrap, 0, false, kmain_thr_stack, 0x10000); - CoreS_SetupThreadContext(&ctx1, (uintptr_t)Arch_IdleTask, 0, false, thr_stack, 0x4000); - CoreH_ThreadInitialize(&kernelMainThread, THREAD_PRIORITY_NORMAL, 1, &ctx2); - CoreH_ThreadInitialize(&bsp_idleThread, THREAD_PRIORITY_IDLE, 1, &ctx1); - kernelMainThread.context.gs_base = (uintptr_t)&bsp_cpu; - bsp_idleThread.context.gs_base = (uintptr_t)&bsp_cpu; - CoreH_ThreadReady(&kernelMainThread); - CoreH_ThreadReady(&bsp_idleThread); - Core_CpuInfo->idleThread = &bsp_idleThread; - Core_CpuInfo->nReadyThreads--; - Core_ReadyThreadCount--; - - // Initialize the CPU's GDT. - Arch_CPUInitializeGDT(&Core_CpuInfo[0], (uintptr_t)Arch_InitialISTStack, sizeof(Arch_InitialISTStack)); - Core_CpuInfo[0].currentIrql = Core_GetIrql(); - Core_CpuInfo[0].arch_specific.ist_stack = Arch_InitialISTStack; - for (thread_priority i = 0; i <= THREAD_PRIORITY_MAX_VALUE; i++) - Core_CpuInfo[0].priorityLists[i].priority = i; - Core_CpuInfo->initialized = true; - - // Finally yield into the scheduler. - Core_LowerIrql(IRQL_DISPATCH); - Core_Yield(); - - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "Scheduler did not switch to a new thread.\n"); - while (1) - asm volatile("nop" : : :); -} - -#if !OBOS_USE_LIMINE -volatile struct ultra_memory_map_attribute* Arch_MemoryMap; -volatile struct ultra_platform_info_attribute* Arch_LdrPlatformInfo; -volatile struct ultra_kernel_info_attribute* Arch_KernelInfo; -volatile struct ultra_module_info_attribute* Arch_KernelBinary; -volatile struct ultra_module_info_attribute* Arch_InitRDDriver; -volatile struct ultra_framebuffer* Arch_Framebuffer; - -static OBOS_PAGEABLE_FUNCTION OBOS_NO_UBSAN struct ultra_module_info_attribute* FindBootModule(volatile struct ultra_boot_context* bcontext, const char* name, size_t nameLen) -{ - if (!nameLen) - nameLen = strlen(name); - volatile struct ultra_attribute_header* header = bcontext->attributes; - for (size_t i = 0; i < bcontext->attribute_count; i++, header = ULTRA_NEXT_ATTRIBUTE(header)) - { - if (header->type == ULTRA_ATTRIBUTE_MODULE_INFO) - { - struct ultra_module_info_attribute* module = (struct ultra_module_info_attribute*)header; - if (uacpi_strncmp(module->name, name, nameLen) == 0) - return module; - } - } - return nullptr; -} - -static OBOS_PAGEABLE_FUNCTION OBOS_NO_KASAN OBOS_NO_UBSAN void ParseBootContext(struct ultra_boot_context* bcontext) -{ - struct ultra_attribute_header* header = bcontext->attributes; - for (size_t i = 0; i < bcontext->attribute_count; i++, header = ULTRA_NEXT_ATTRIBUTE(header)) - { - switch (header->type) - { - case ULTRA_ATTRIBUTE_PLATFORM_INFO: Arch_LdrPlatformInfo = (struct ultra_platform_info_attribute*)header; break; - case ULTRA_ATTRIBUTE_KERNEL_INFO: Arch_KernelInfo = (struct ultra_kernel_info_attribute*)header; break; - case ULTRA_ATTRIBUTE_MEMORY_MAP: Arch_MemoryMap = (struct ultra_memory_map_attribute*)header; break; - case ULTRA_ATTRIBUTE_COMMAND_LINE: OBOS_KernelCmdLine = (const char*)(header + 1); break; - case ULTRA_ATTRIBUTE_FRAMEBUFFER_INFO: - { - struct ultra_framebuffer_attribute* fb = (struct ultra_framebuffer_attribute*)header; - Arch_Framebuffer = &fb->fb; - break; - } - case ULTRA_ATTRIBUTE_MODULE_INFO: - { - struct ultra_module_info_attribute* module = (struct ultra_module_info_attribute*)header; - if (strcmp(module->name, "__KERNEL__")) - Arch_KernelBinary = module; - break; - } - case ULTRA_ATTRIBUTE_INVALID: - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "Invalid UltraProtocol attribute type %d.\n", header->type); - break; - default: - break; - } - } - if (!Arch_LdrPlatformInfo) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "Could not find platform info from bootloader.\n"); - if (Arch_LdrPlatformInfo->platform_type == ULTRA_PLATFORM_INVALID) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "Invalid platform type %d.\n", Arch_LdrPlatformInfo->platform_type); - if (!Arch_KernelInfo) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "Could not find kernel info from bootloader.\n"); - if (Arch_KernelInfo->partition_type == ULTRA_PARTITION_TYPE_INVALID) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "Invalid partition type %d.\n", Arch_KernelInfo->partition_type); - if (!Arch_KernelBinary) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "Could not find the kernel module in boot context!\nDo you set kernel-as-module to true in the hyper.cfg?\n"); - Arch_RSDPBase = Arch_LdrPlatformInfo->acpi_rsdp_address; -} -#endif - -extern obos_status Arch_InitializeKernelPageTable(); - -// uint64_t random_number(); -// uint8_t random_number8(); -// __asm__( -// "random_number:; rdrand %rax; ret; " -// "random_number8:; rdrand %ax; mov $0, %ah; ret; " -// ); - -void Arch_SMPStartup(); - -void OBOSS_KernelPostIRQInit() -{ - obos_status status = OBOS_STATUS_SUCCESS; - - OBOS_Debug("%s: Initializing CMOS RTC\n", __func__); - Arch_CMOSInitialize(); - OBOS_Debug("%s: Initializing scheduler timer.\n", __func__); - Arch_InitializeSchedulerTimer(); - OBOS_Debug("%s: Initializing IOAPICs.\n", __func__); - if (obos_is_error(status = Arch_InitializeIOAPICs())) - OBOS_Panic(OBOS_PANIC_DRIVER_FAILURE, "Could not initialize I/O APICs. Status: %d\n", status); -} - -void OBOSS_KernelPostVMMInit() -{ - if (Arch_Framebuffer->physical_address) - { - OBOS_Debug("Mapping framebuffer as Write-Combining.\n"); - size_t size = (Arch_Framebuffer->height*Arch_Framebuffer->pitch + OBOS_HUGE_PAGE_SIZE - 1) & ~(OBOS_HUGE_PAGE_SIZE - 1); - void* base_ = Mm_VirtualMemoryAlloc(&Mm_KernelContext, (void*)0xffffa00000000000, size, 0, VMA_FLAGS_NON_PAGED | VMA_FLAGS_HINT | VMA_FLAGS_HUGE_PAGE, nullptr, nullptr); - uintptr_t base = (uintptr_t)base_; - if (base) - { - // We got memory for the framebuffer. - // Now modify the physical pages - uintptr_t offset = 0; - for (uintptr_t addr = base; addr < (base + size); addr += offset) - { - uintptr_t oldPhys = 0, phys = Arch_Framebuffer->physical_address + (addr-base); - page_info info = {}; - MmS_QueryPageInfo(MmS_GetCurrentPageTable(), addr, &info, &oldPhys); - // Present,Write,XD,Write-Combining (PAT: 0b110) - Arch_MapHugePage(Mm_KernelContext.pt, (void*)addr, phys, BIT_TYPE(0, UL)|BIT_TYPE(1, UL)|BIT_TYPE(63, UL)|BIT_TYPE(4, UL)|BIT_TYPE(12, UL), false); - offset = info.prot.huge_page ? OBOS_HUGE_PAGE_SIZE : OBOS_PAGE_SIZE; - page what = {.phys=oldPhys}; - Core_MutexAcquire(&Mm_PhysicalPagesLock); - page* pg = RB_FIND(phys_page_tree, &Mm_PhysicalPages, &what); - Core_MutexRelease(&Mm_PhysicalPagesLock); - MmH_DerefPage(pg); - } - } - ((struct flanterm_fb_context*)OBOS_FlantermContext)->framebuffer = base_; - OBOS_TextRendererState.fb.base = base_; - OBOS_TextRendererState.fb.height = Arch_Framebuffer->height; - OBOS_TextRendererState.fb.pitch = Arch_Framebuffer->pitch; - OBOS_TextRendererState.fb.width = Arch_Framebuffer->width; - OBOS_TextRendererState.fb.bpp = Arch_Framebuffer->bpp; - OBOS_TextRendererState.fb.format = Arch_Framebuffer->format; - } -} - -void OBOSS_KernelPostPMMInit() -{ - OBOS_Debug("%s: Initializing page tables.\n", __func__); - obos_status status = Arch_InitializeKernelPageTable(); - if (obos_is_error(status)) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "Could not initialize page tables. Status: %d.\n", status); - bsp_idleThread.context.cr3 = getCR3(); -} - -void OBOSS_KernelPostKProcInit() -{ - Core_ProcessAppendThread(OBOS_KernelProcess, &kernelMainThread); - Core_ProcessAppendThread(OBOS_KernelProcess, &bsp_idleThread); -} - -void OBOSS_InitializeSMP() -{ - OBOS_Debug("%s: Initializing LAPIC.\n", __func__); - Arch_LAPICInitialize(true); - OBOS_Debug("%s: Initializing SMP.\n", __func__); - Arch_SMPStartup(); - bsp_idleThread.context.gs_base = rdmsr(0xC0000101 /* GS_BASE */); - bsp_idleThread.masterCPU = CoreS_GetCPULocalPtr(); - Core_GetCurrentThread()->masterCPU = CoreS_GetCPULocalPtr(); -} - -#if !OBOS_USE_LIMINE -static void ultra_module_to_boot_module(const struct ultra_module_info_attribute *ultra_module, struct boot_module* out_module) -{ - if (!ultra_module || !out_module) - return; - out_module->address = (void*)ultra_module->address; - out_module->size = ultra_module->size; - out_module->name = ultra_module->name; - out_module->is_memory = ultra_module->type == ULTRA_MODULE_TYPE_MEMORY; - out_module->is_kernel = strcmp(ultra_module->name, "__KERNEL__"); - return; -} - -void OBOSS_GetModule(struct boot_module *module, const char* name) -{ - ultra_module_to_boot_module(FindBootModule(Arch_BootContext, name, strlen(name)), module); -} -void OBOSS_GetModuleLen(struct boot_module *module, const char* name, size_t name_len) -{ - ultra_module_to_boot_module(FindBootModule(Arch_BootContext, name, name_len), module); -} -void OBOSS_GetKernelModule(struct boot_module *module) -{ - ultra_module_to_boot_module((void*)Arch_KernelBinary, module); -} -#else -static void limine_file_to_boot_module(const struct limine_file *file, struct boot_module* out_module) -{ - if (!file || !out_module) - return; - out_module->address = file->address; - out_module->size = file->size; - out_module->name = file->string; - out_module->is_memory = false; - out_module->is_kernel = file == Arch_LimineKernelInfoRequest.response->executable_file; - return; -} -static struct limine_file* find_module(const char* name, size_t name_len) -{ - for (size_t i = 0; i < Arch_LimineModuleRequest.response->module_count; i++) - { - struct limine_file* cur = Arch_LimineModuleRequest.response->modules[i]; - if (strncmp(cur->string, name, name_len)) - return cur; - } - return nullptr; -} - -void OBOSS_GetModule(struct boot_module *module, const char* name) -{ - limine_file_to_boot_module(find_module(name, strlen(name)), module); -} -void OBOSS_GetModuleLen(struct boot_module *module, const char* name, size_t name_len) -{ - limine_file_to_boot_module(find_module(name, name_len), module); -} -void OBOSS_GetKernelModule(struct boot_module *module) -{ - limine_file_to_boot_module(Arch_LimineKernelInfoRequest.response->executable_file, module); -} -#endif - -void OBOSS_MakeTTY() -{ - session* session = nullptr; - OBOS_ENSURE(obos_is_success(Core_MakeSession(Core_GetCurrentThread()->proc, &session))); - - const char* boot_tty = OBOS_GetOPTS("boot-tty"); - const char* tty0_keyboard = OBOS_GetOPTS("x86-tty0-keyboard"); - if (!tty0_keyboard) tty0_keyboard = "ps2k1"; - if (!boot_tty) boot_tty = "tty0"; - - dirent* keyboard = VfsH_DirentLookupFrom(tty0_keyboard, Vfs_DevRoot); - tty_interface i = {}; - VfsH_MakeScreenTTY(&i, keyboard ? keyboard->vnode : nullptr, nullptr, OBOS_FlantermContext); - Vfs_RegisterTTY(&i, nullptr, TTY_SCREEN); - dirent* tty = VfsH_DirentLookupFrom(boot_tty, Vfs_DevRoot); - if (!tty || !tty->vnode) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "Could not find %s specified in boot-tty option!\n", boot_tty); - if (~tty->vnode->flags & VFLAGS_IS_TTY) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "%s specified in boot-tty option is not a tty!\n", boot_tty); - - process_group* pgrp = Core_GetCurrentThread()->proc->pgrp; - ((struct tty*)tty->vnode->desc)->fg_job = pgrp; - session->controlling_tty = (void*)tty->vnode->desc; -} - -static bool isnum(char ch) -{ - return (ch - '0') >= 0 && (ch - '0') < 10; -} - -void Arch_KernelMainBootstrap() -{ - OBOS_KernelInit(); - // No longer receive logs on the TTY, if the option is set - // Do __not__ document this option. - if (OBOS_GetOPTF("disable-flanterm-logging")) - disable_flanterm_backend = true; - Core_ExitCurrentThread(); -} diff --git a/src/oboskrnl/arch/x86_64/entry.cpp b/src/oboskrnl/arch/x86_64/entry.cpp new file mode 100644 index 00000000..3729451c --- /dev/null +++ b/src/oboskrnl/arch/x86_64/entry.cpp @@ -0,0 +1,220 @@ +/* + oboskrnl/arch/x86_64/entry.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include + +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include +#include + +#include + +#include +#include + +#include +#include +#include +#include + +#include +#include + +#include + +#include +#include + +#include + +#include + +#include +#include + +extern "C" void InitBootGDT(); +extern "C" void disablePIC(); +extern "C" void enableSSE(); +extern "C" void enableXSAVE(); +extern "C" void enableAVX(); +extern "C" void enableAVX512(); + +namespace obos +{ + void InitializePMM(); + void RegisterExceptionHandlers(); + volatile limine_framebuffer_request framebuffer_request = { + .id = LIMINE_FRAMEBUFFER_REQUEST, + .revision = 0 + }; + extern volatile limine_rsdp_request rsdp_request; + extern volatile limine_hhdm_request hhdm_offset; + namespace arch + { + size_t ThreadContextInfo::xsave_size = 0; + HPET* g_hpetAddress; + } + extern void kmain(); +} + +using namespace obos; + +LIMINE_BASE_REVISION(1); + +extern "C" uint64_t calibrateHPET(uint64_t freq); + +extern "C" void KernelArchInit() +{ + g_consoleFont = (void*)font_bin; + Framebuffer initFB; + memzero(&initFB, sizeof(initFB)); + if (framebuffer_request.response->framebuffer_count) + { + initFB.address = framebuffer_request.response->framebuffers[0]->address; + initFB.bpp = framebuffer_request.response->framebuffers[0]->bpp; + initFB.height = framebuffer_request.response->framebuffers[0]->height; + initFB.width = framebuffer_request.response->framebuffers[0]->width; + initFB.pitch = framebuffer_request.response->framebuffers[0]->pitch; + initFB.format = FramebufferFormat::FB_FORMAT_RGBX8888; + } + new (&g_kernelConsole) Console{}; + g_kernelConsole.Initialize(&initFB, nullptr, true); + logger::log("%s: Initializing boot GDT.\n", __func__); + InitBootGDT(); + logger::log("%s: Initializing IDT.\n", __func__); + InitializeIDT(); + logger::log("%s: Registering exception handlers.\n", __func__); + RegisterExceptionHandlers(); + disablePIC(); + logger::log("%s: Initializing LAPIC.\n", __func__); + InitializeLAPIC(GetLAPICAddress()); + logger::log("%s: Initializing IOAPIC.\n", __func__); + uintptr_t ioapic = 0; + ACPISDTHeader* sdt = nullptr; + size_t nEntries = 0; + bool t32 = false; + GetSDTFromRSDP((ACPIRSDPHeader*)rsdp_request.response->address, &sdt, &t32, &nEntries); + char sign[4] = { 'A', 'P', 'I', 'C' }; + auto madt = (MADTTable*)GetTableWithSignature(sdt, t32, nEntries, &sign); + if (!madt) + logger::panic(nullptr, "Could not find MADT table in the system.\n"); + if (ParseMADTForIOAPICAddresses(madt, &ioapic, 1)) + logger::warning("%s: There are multiple I/O APICs, but multiple I/O APICs are not supported by oboskrnl.\n", __func__); + if (ioapic) + InitializeIOAPIC((IOAPIC*)(hhdm_offset.response->offset + ioapic)); + else + logger::warning("%s: Could not find an I/O APIC on this computer.\n", __func__); + sign[0] = 'H'; + sign[1] = 'P'; + sign[2] = 'E'; + sign[3] = 'T'; + auto hpet_table = (arch::HPET_Table*)GetTableWithSignature(sdt, t32, nEntries, &sign); + if (hpet_table->baseAddress.address) + arch::g_hpetAddress = (arch::HPET*)(hhdm_offset.response->offset + hpet_table->baseAddress.address); + else + logger::panic(nullptr, "%s: No HPET Table provided by the firmware.\n", __func__); + uint8_t oldIRQL = 0; + RaiseIRQL(0xf /* Mask All. */, &oldIRQL); + asm("sti"); + logger::log("%s: Initializing PMM.\n", __func__); + InitializePMM(); + logger::log("%s: Enabling SSE.\n", __func__); + enableSSE(); + logger::log("%s: Enabling XSAVE (if supported).\n", __func__); + enableXSAVE(); + logger::log("%s: Enabling AVX (if supported).\n", __func__); + enableAVX(); + logger::log("%s: Enabling AVX-512 (if supported).\n", __func__); + enableAVX512(); + logger::log("%s: Zeroing zero-page.\n", __func__); + memzero(MapToHHDM(0), 4096); + logger::log("%s: Initializing page tables.\n", __func__); + arch::InitializePageTables(); + logger::log("%s: Initializing VMM.\n", __func__); + vmm::InitializeVMM(); + { + vmm::page_descriptor lapicPD{}; + lapicPD.virt = 0xffff'ffff'ffff'f000; + lapicPD.phys = (uintptr_t)g_localAPICAddress - hhdm_offset.response->offset; + lapicPD.protFlags = vmm::PROT_CACHE_DISABLE | vmm::PROT_NO_DEMAND_PAGE; + lapicPD.present = true; + lapicPD.isHugePage = false; + vmm::MapPageDescriptor(&vmm::g_kernelContext, lapicPD); + g_localAPICAddress = (LAPIC*)lapicPD.virt; + vmm::page_descriptor ioapicPD{}; + ioapicPD.virt = lapicPD.virt - 0x1000; + ioapicPD.phys = (uintptr_t)g_IOAPICAddress - hhdm_offset.response->offset; + ioapicPD.protFlags = vmm::PROT_CACHE_DISABLE | vmm::PROT_NO_DEMAND_PAGE; + ioapicPD.present = true; + ioapicPD.isHugePage = false; + vmm::MapPageDescriptor(&vmm::g_kernelContext, ioapicPD); + g_IOAPICAddress = (IOAPIC*)ioapicPD.virt; + vmm::page_descriptor hpetPD{}; + hpetPD.virt = ioapicPD.virt - 0x1000; + hpetPD.phys = (uintptr_t)arch::g_hpetAddress - hhdm_offset.response->offset; + hpetPD.protFlags = vmm::PROT_CACHE_DISABLE | vmm::PROT_NO_DEMAND_PAGE; + hpetPD.present = true; + hpetPD.isHugePage = false; + vmm::MapPageDescriptor(&vmm::g_kernelContext, hpetPD); + arch::g_hpetAddress = (arch::HPET*)hpetPD.virt; + } + logger::log("%s: Registering IPI handler\n", __func__); + arch::RegisterIPIHandler(); +#if OBOS_KDBG_ENABLED + logger::log("%s: Enabling kernel debugger.\n", __func__); + kdbg::init_kdbg(kdbg::input_format::PS2_KEYBOARD, kdbg::output_format::CONSOLE); +#endif + logger::log("%s: Starting processors.\n", __func__); + size_t nCpus = arch::StartProcessors(); + logger::debug("%s: Started %ld cores.\n", __func__, nCpus); + // sign[0] = 'F'; + // sign[1] = 'A'; + // sign[2] = 'C'; + // sign[3] = 'P'; + // auto fadt = GetTableWithSignature(sdt, t32, nEntries, &sign); + // ACPISDTHeader* dsdt = (ACPISDTHeader*)MapToHHDM((((uint32_t*)(fadt + 1))[1])); + // size_t dsdt_size = dsdt->Length; + // char* dsdt_data = (char*)dsdt; + // for (size_t i = 0; i < dsdt_size; i++) + // outb(0x3f8, dsdt_data[i]); + // while(1); + logger::log("%s: Initializing scheduler.\n", __func__); + __cpuid__(0xd, 0, nullptr, nullptr, (uint32_t*)&arch::ThreadContextInfo::xsave_size, nullptr); + scheduler::InitializeScheduler(); + scheduler::StartKernelMainThread(kmain); + LowerIRQL(oldIRQL, false); + // Configure watchdog timer to wait for the LAPIC timer for one second. + // If it fails, assume something messed up and forgot to send an EOI. + uint64_t timer = calibrateHPET(scheduler::g_schedulerFrequency/2); + arch::g_hpetAddress->generalConfig |= 1; + while (arch::g_hpetAddress->mainCounterValue < timer) + pause(); + logger::warning("Watchdog timer ran out!\n"); + LAPIC_SendEOI(); + asm("sti"); + LowerIRQL(0); + // Spin for a couple iterations. + timer = calibrateHPET(scheduler::g_schedulerFrequency/2); + arch::g_hpetAddress->generalConfig |= 1; + while (arch::g_hpetAddress->mainCounterValue < timer) + pause(); + // We're still here. Panic. + logger::panic(nullptr, "Watchdog timer ran out while waiting for LAPIC timer for CPU %d!\n", scheduler::GetCPUPtr()->cpuId); + // Hang waiting for an interrupt. + while(1) + asm volatile("hlt"); +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/except.c b/src/oboskrnl/arch/x86_64/except.c deleted file mode 100644 index 3cd60bc0..00000000 --- a/src/oboskrnl/arch/x86_64/except.c +++ /dev/null @@ -1,364 +0,0 @@ -/* - * oboskrnl/arch/x86_64/except.c - * - * Copyright (c) 2024-2025 Omar Berrow -*/ - -#include -#include -#include - -#include -#include -#include - -#include -#include -#include -#include - -#include -#include -#include - -#include "gdbstub/debug.h" - -uintptr_t Arch_GetPML2Entry(uintptr_t pml4Base, uintptr_t addr); -__attribute__((no_instrument_function)) OBOS_NO_UBSAN OBOS_NO_KASAN void Arch_PageFaultHandler(interrupt_frame* frame) -{ - sti(); - uintptr_t virt = getCR2(); - // for (volatile bool b = (virt == 0x412134fa); b; ) - // asm volatile ("" : "=r"(b) : "r"(b) :"memory"); - virt &= ~0xfff; - if (!CoreS_GetCPULocalPtr()) - goto down; - if (!CoreS_GetCPULocalPtr()->currentContext) - goto down; - if (Core_GetIrql() > IRQL_DISPATCH) - OBOS_Error("Page fault at > IRQL_DISPATCH\n"); - if (Mm_IsInitialized() && Core_GetIrql() <= IRQL_DISPATCH) - { - if (Arch_GetPML2Entry(CoreS_GetCPULocalPtr()->currentContext->pt, virt) & (1<<7)) - virt &= ~0x1fffff; - CoreS_GetCPULocalPtr()->arch_specific.pf_handler_running = true; - uint32_t mm_ec = 0; - if (frame->errorCode & BIT(0)) - mm_ec |= PF_EC_PRESENT; - if (frame->errorCode & BIT(1)) - mm_ec |= PF_EC_RW; - if (frame->errorCode & BIT(2)) - mm_ec |= PF_EC_UM; - if (frame->errorCode & BIT(3)) - mm_ec |= PF_EC_INV_PTE; - if (frame->errorCode & BIT(4)) - mm_ec |= PF_EC_EXEC; - // TODO: Find out why CoreS_GetCPULocalPtr()->currentContext is nullptr in the first place - if (!CoreS_GetCPULocalPtr()->currentContext) - { - if (CoreS_GetCPULocalPtr()->currentThread->proc->pid != 0 && mm_ec & PF_EC_UM) - CoreS_GetCPULocalPtr()->currentContext = CoreS_GetCPULocalPtr()->currentThread->proc->ctx; - else - CoreS_GetCPULocalPtr()->currentContext = &Mm_KernelContext; - } - irql oldIrql = Core_RaiseIrql(IRQL_DISPATCH); - obos_status status = Mm_HandlePageFault(CoreS_GetCPULocalPtr()->currentContext, virt, mm_ec); - Core_LowerIrql(oldIrql); - CoreS_GetCPULocalPtr()->arch_specific.pf_handler_running = false; - switch (status) - { - case OBOS_STATUS_SUCCESS: - OBOS_ASSERT(frame->rsp != 0); - cli(); - return; - case OBOS_STATUS_UNHANDLED: - break; - default: - { - OBOS_Warning("Handling page fault with error code 0x%x on address %p failed with status %d.\n", mm_ec, getCR2(), status); - break; - } - } - } - if (Kdbg_CurrentConnection && !Kdbg_Paused && Kdbg_CurrentConnection->connection_active) - { - asm("sti"); - irql oldIrql = Core_GetIrql(); - Core_LowerIrqlNoThread(IRQL_PASSIVE); - Kdbg_NotifyGDB(Kdbg_CurrentConnection, 11 /* SIGSEGV */); - Kdbg_CallDebugExceptionHandler(frame, true); - irql discardedlol = Core_RaiseIrqlNoThread(oldIrql); - (void)discardedlol; - asm("cli"); - } - if (frame->cs & 3) - { - OBOS_Log("User thread %d SIGSEGV (rip 0x%p, cr2 0x%p, error code 0x%08x)\n", Core_GetCurrentThread()->tid, frame->rip, getCR2(), frame->errorCode); - Core_GetCurrentThread()->proc->signal_handlers[SIGSEGV].addr = (void*)getCR2(); - OBOS_Kill(Core_GetCurrentThread(), Core_GetCurrentThread(), SIGSEGV); - // OBOS_SyncPendingSignal(frame); - OBOS_RunSignal(SIGSEGV, frame); // Ensure SIGSEGV runs. - return; - } - down: - asm("cli"); - OBOS_Panic(OBOS_PANIC_EXCEPTION, - "Page fault at 0x%p in %s-mode while trying to %s page at 0x%p, which is %s. Error code: %d\n" - "Register dump:\n" - "\tRDI: 0x%016lx, RSI: 0x%016lx, RBP: 0x%016lx\n" - "\tRSP: 0x%016lx, RBX: 0x%016lx, RDX: 0x%016lx\n" - "\tRCX: 0x%016lx, RAX: 0x%016lx, RIP: 0x%016lx\n" - "\t R8: 0x%016lx, R9: 0x%016lx, R10: 0x%016lx\n" - "\tR11: 0x%016lx, R12: 0x%016lx, R13: 0x%016lx\n" - "\tR14: 0x%016lx, R15: 0x%016lx, RFL: 0x%016lx\n" - "\t SS: 0x%016lx, DS: 0x%016lx, CS: 0x%016lx\n" - "\tCR0: 0x%016lx, CR2: 0x%016lx, CR3: 0x%016lx\n" - "\tCR4: 0x%016lx, CR8: 0x%016x, EFER: 0x%016lx\n", - (void*)frame->rip, - frame->cs == 0x8 ? "kernel" : "user", - (frame->errorCode & 2) ? "write" : ((frame->errorCode & 0x10) ? "execute" : "read"), - getCR2(), - frame->errorCode & 1 ? "present" : "unpresent", - frame->errorCode, - frame->rdi, frame->rsi, frame->rbp, - frame->rsp, frame->rbx, frame->rdx, - frame->rcx, frame->rax, frame->rip, - frame->r8, frame->r9, frame->r10, - frame->r11, frame->r12, frame->r13, - frame->r14, frame->r15, frame->rflags, - frame->ss, frame->ds, frame->cs, - getCR0(), getCR2(), frame->cr3, - getCR4(), Core_GetIrql(), getEFER() - ); -} - -__attribute__((no_instrument_function)) OBOS_NO_UBSAN OBOS_NO_KASAN OBOS_NO_KASAN void Arch_DoubleFaultHandler(interrupt_frame* frame) -{ - static const char format[] = "Double fault!\n" - "Register dump:\n" - "\tRDI: 0x%016lx, RSI: 0x%016lx, RBP: 0x%016lx\n" - "\tRSP: 0x%016lx, RBX: 0x%016lx, RDX: 0x%016lx\n" - "\tRCX: 0x%016lx, RAX: 0x%016lx, RIP: 0x%016lx\n" - "\t R8: 0x%016lx, R9: 0x%016lx, R10: 0x%016lx\n" - "\tR11: 0x%016lx, R12: 0x%016lx, R13: 0x%016lx\n" - "\tR14: 0x%016lx, R15: 0x%016lx, RFL: 0x%016lx\n" - "\t SS: 0x%016lx, DS: 0x%016lx, CS: 0x%016lx\n" - "\tCR0: 0x%016lx, CR2: 0x%016lx, CR3: 0x%016lx\n" - "\tCR4: 0x%016lx, CR8: 0x%016x, EFER: 0x%016lx\n"; - OBOS_Panic(OBOS_PANIC_EXCEPTION, - format, - frame->rdi, frame->rsi, frame->rbp, - frame->rsp, frame->rbx, frame->rdx, - frame->rcx, frame->rax, frame->rip, - frame->r8, frame->r9, frame->r10, - frame->r11, frame->r12, frame->r13, - frame->r14, frame->r15, frame->rflags, - frame->ss, frame->ds, frame->cs, - getCR0(), getCR2(), frame->cr3, - getCR4(), Core_GetIrql(), getEFER() - ); -} - -__attribute__((no_instrument_function)) OBOS_NO_UBSAN OBOS_NO_KASAN OBOS_NO_KASAN void Arch_SegvHandler(interrupt_frame* frame) -{ - if (frame->cs & 3) - { - OBOS_Log("User thread %d SIGSEGV\n", Core_GetCurrentThread()->tid); - if (Kdbg_CurrentConnection && !Kdbg_Paused && Kdbg_CurrentConnection->connection_active) - { - asm("sti"); - irql oldIrql = Core_GetIrql(); - Core_LowerIrqlNoThread(IRQL_PASSIVE); - Kdbg_NotifyGDB(Kdbg_CurrentConnection, 11 /* SIGSEGV */); - Kdbg_CallDebugExceptionHandler(frame, true); - irql discardedlol = Core_RaiseIrqlNoThread(oldIrql); - (void)discardedlol; - asm("cli"); - } - OBOS_Kill(Core_GetCurrentThread(), Core_GetCurrentThread(), SIGSEGV); - OBOS_SyncPendingSignal(frame); - OBOS_RunSignal(SIGSEGV, frame); // Ensure SIGSEGV runs. - return; - } - OBOS_Panic(OBOS_PANIC_EXCEPTION, - "Kernel segmentation fault! Exception code: %d. Error code: 0x%08x\n" - "Register dump:\n" - "\tRDI: 0x%016lx, RSI: 0x%016lx, RBP: 0x%016lx\n" - "\tRSP: 0x%016lx, RBX: 0x%016lx, RDX: 0x%016lx\n" - "\tRCX: 0x%016lx, RAX: 0x%016lx, RIP: 0x%016lx\n" - "\t R8: 0x%016lx, R9: 0x%016lx, R10: 0x%016lx\n" - "\tR11: 0x%016lx, R12: 0x%016lx, R13: 0x%016lx\n" - "\tR14: 0x%016lx, R15: 0x%016lx, RFL: 0x%016lx\n" - "\t SS: 0x%016lx, DS: 0x%016lx, CS: 0x%016lx\n" - "\tCR0: 0x%016lx, CR2: 0x%016lx, CR3: 0x%016lx\n" - "\tCR4: 0x%016lx, CR8: 0x%016x, EFER: 0x%016lx\n", - frame->intNumber, frame->errorCode, - frame->rdi, frame->rsi, frame->rbp, - frame->rsp, frame->rbx, frame->rdx, - frame->rcx, frame->rax, frame->rip, - frame->r8, frame->r9, frame->r10, - frame->r11, frame->r12, frame->r13, - frame->r14, frame->r15, frame->rflags, - frame->ss, frame->ds, frame->cs, - getCR0(), getCR2(), frame->cr3, - getCR4(), Core_GetIrql(), getEFER() - ); -} - -__attribute__((no_instrument_function)) OBOS_NO_UBSAN OBOS_NO_KASAN OBOS_NO_KASAN void Arch_UndefinedOpcodeHandler(interrupt_frame* frame) -{ - if (frame->cs & 3) - { - OBOS_Log("User thread %d SIGILL\n", Core_GetCurrentThread()->tid); - OBOS_Kill(Core_GetCurrentThread(), Core_GetCurrentThread(), SIGILL); - // OBOS_SyncPendingSignal(frame); - OBOS_RunSignal(SIGILL, frame); // Ensure SIGILL runs. - return; - } - OBOS_Panic(OBOS_PANIC_EXCEPTION, - "Kernel illegal instruction! Exception code: %d\n" - "Register dump:\n" - "\tRDI: 0x%016lx, RSI: 0x%016lx, RBP: 0x%016lx\n" - "\tRSP: 0x%016lx, RBX: 0x%016lx, RDX: 0x%016lx\n" - "\tRCX: 0x%016lx, RAX: 0x%016lx, RIP: 0x%016lx\n" - "\t R8: 0x%016lx, R9: 0x%016lx, R10: 0x%016lx\n" - "\tR11: 0x%016lx, R12: 0x%016lx, R13: 0x%016lx\n" - "\tR14: 0x%016lx, R15: 0x%016lx, RFL: 0x%016lx\n" - "\t SS: 0x%016lx, DS: 0x%016lx, CS: 0x%016lx\n" - "\tCR0: 0x%016lx, CR2: 0x%016lx, CR3: 0x%016lx\n" - "\tCR4: 0x%016lx, CR8: 0x%016x, EFER: 0x%016lx\n", - frame->intNumber, - frame->rdi, frame->rsi, frame->rbp, - frame->rsp, frame->rbx, frame->rdx, - frame->rcx, frame->rax, frame->rip, - frame->r8, frame->r9, frame->r10, - frame->r11, frame->r12, frame->r13, - frame->r14, frame->r15, frame->rflags, - frame->ss, frame->ds, frame->cs, - getCR0(), getCR2(), frame->cr3, - getCR4(), Core_GetIrql(), getEFER() - ); -} - -__attribute__((no_instrument_function)) OBOS_NO_UBSAN OBOS_NO_KASAN void Arch_FPEHandler(interrupt_frame* frame) -{ - if (frame->cs & 3) - { - OBOS_Log("User thread %d SIGFPE (rip 0x%p)\n", Core_GetCurrentThread()->tid, frame->rip); - if (Kdbg_CurrentConnection && !Kdbg_Paused && Kdbg_CurrentConnection->connection_active) - { - asm("sti"); - irql oldIrql = Core_GetIrql(); - Core_LowerIrqlNoThread(IRQL_PASSIVE); - Kdbg_NotifyGDB(Kdbg_CurrentConnection, SIGFPE); - Kdbg_CallDebugExceptionHandler(frame, true); - irql discardedlol = Core_RaiseIrqlNoThread(oldIrql); - (void)discardedlol; - asm("cli"); - } - OBOS_Kill(Core_GetCurrentThread(), Core_GetCurrentThread(), SIGFPE); - // OBOS_SyncPendingSignal(frame); - OBOS_RunSignal(SIGFPE, frame); // Ensure SIGFPE runs. - return; - } - OBOS_Panic(OBOS_PANIC_EXCEPTION, - "Kernel floating point error! Exception code: %d\n" - "Register dump:\n" - "\tRDI: 0x%016lx, RSI: 0x%016lx, RBP: 0x%016lx\n" - "\tRSP: 0x%016lx, RBX: 0x%016lx, RDX: 0x%016lx\n" - "\tRCX: 0x%016lx, RAX: 0x%016lx, RIP: 0x%016lx\n" - "\t R8: 0x%016lx, R9: 0x%016lx, R10: 0x%016lx\n" - "\tR11: 0x%016lx, R12: 0x%016lx, R13: 0x%016lx\n" - "\tR14: 0x%016lx, R15: 0x%016lx, RFL: 0x%016lx\n" - "\t SS: 0x%016lx, DS: 0x%016lx, CS: 0x%016lx\n" - "\tCR0: 0x%016lx, CR2: 0x%016lx, CR3: 0x%016lx\n" - "\tCR4: 0x%016lx, CR8: 0x%016x, EFER: 0x%016lx\n", - frame->intNumber, - frame->rdi, frame->rsi, frame->rbp, - frame->rsp, frame->rbx, frame->rdx, - frame->rcx, frame->rax, frame->rip, - frame->r8, frame->r9, frame->r10, - frame->r11, frame->r12, frame->r13, - frame->r14, frame->r15, frame->rflags, - frame->ss, frame->ds, frame->cs, - getCR0(), getCR2(), frame->cr3, - getCR4(), Core_GetIrql(), getEFER() - ); -} - -__attribute__((no_instrument_function)) OBOS_NO_UBSAN OBOS_NO_KASAN OBOS_NO_KASAN void Arch_DbgExceptHandler(interrupt_frame* frame) -{ - // if (Kdbg_CurrentConnection->pipe_interface && Kdbg_CurrentConnection->pipe_interface->read_sync) - // { - // switch (frame->intNumber) { - // case 1: - // Kdbg_int1_handler(frame); - // break; - // case 3: - // Kdbg_int3_handler(frame); - // break; - // default: - // OBOS_UNREACHABLE; - // } - // return; - // } - if (frame->cs & 3) - { - OBOS_Log("User thread %d SIGTRAP\n", Core_GetCurrentThread()->tid); - // Doesn't really apply. - // if (Kdbg_CurrentConnection && !Kdbg_Paused && Kdbg_CurrentConnection->connection_active) - // { - // asm("sti"); - // irql oldIrql = Core_GetIrql(); - // Core_LowerIrqlNoThread(IRQL_PASSIVE); - // Kdbg_NotifyGDB(Kdbg_CurrentConnection, SIGTRAP); - // Kdbg_CallDebugExceptionHandler(frame, true); - // (void)Core_RaiseIrqlNoThread(oldIrql); - // asm("cli"); - // } - OBOS_Kill(Core_GetCurrentThread(), Core_GetCurrentThread(), SIGTRAP); - // OBOS_SyncPendingSignal(frame); - OBOS_RunSignal(SIGTRAP, frame); // Ensure SIGTRAP runs. - return; - } - OBOS_Panic(OBOS_PANIC_EXCEPTION, - "Unexpected kernel-mode debug exception! Exception code: %d\n" - "Register dump:\n" - "\tRDI: 0x%016lx, RSI: 0x%016lx, RBP: 0x%016lx\n" - "\tRSP: 0x%016lx, RBX: 0x%016lx, RDX: 0x%016lx\n" - "\tRCX: 0x%016lx, RAX: 0x%016lx, RIP: 0x%016lx\n" - "\t R8: 0x%016lx, R9: 0x%016lx, R10: 0x%016lx\n" - "\tR11: 0x%016lx, R12: 0x%016lx, R13: 0x%016lx\n" - "\tR14: 0x%016lx, R15: 0x%016lx, RFL: 0x%016lx\n" - "\t SS: 0x%016lx, DS: 0x%016lx, CS: 0x%016lx\n" - "\tCR0: 0x%016lx, CR2: 0x%016lx, CR3: 0x%016lx\n" - "\tCR4: 0x%016lx, CR8: 0x%016x, EFER: 0x%016lx\n", - frame->intNumber, - frame->rdi, frame->rsi, frame->rbp, - frame->rsp, frame->rbx, frame->rdx, - frame->rcx, frame->rax, frame->rip, - frame->r8, frame->r9, frame->r10, - frame->r11, frame->r12, frame->r13, - frame->r14, frame->r15, frame->rflags, - frame->ss, frame->ds, frame->cs, - getCR0(), getCR2(), frame->cr3, - getCR4(), Core_GetIrql(), getEFER() - ); -} - - -void Arch_InstallExceptionHandlers() -{ - Arch_RawRegisterInterrupt(0x00, (uintptr_t)Arch_FPEHandler); - Arch_RawRegisterInterrupt(0x01, (uintptr_t)Arch_DbgExceptHandler); - Arch_RawRegisterInterrupt(0x03, (uintptr_t)Arch_DbgExceptHandler); - Arch_RawRegisterInterrupt(0x06, (uintptr_t)Arch_UndefinedOpcodeHandler); - Arch_RawRegisterInterrupt(0x08, (uintptr_t)Arch_DoubleFaultHandler); - Arch_RawRegisterInterrupt(0x0b, (uintptr_t)Arch_SegvHandler); - Arch_RawRegisterInterrupt(0x0c, (uintptr_t)Arch_SegvHandler); - Arch_RawRegisterInterrupt(0x0d, (uintptr_t)Arch_SegvHandler); - Arch_RawRegisterInterrupt(0x0e, (uintptr_t)Arch_PageFaultHandler); - Arch_RawRegisterInterrupt(0x10, (uintptr_t)Arch_FPEHandler); - Arch_RawRegisterInterrupt(0x11, (uintptr_t)Arch_SegvHandler); - Arch_RawRegisterInterrupt(0x13, (uintptr_t)Arch_FPEHandler); -} diff --git a/src/oboskrnl/arch/x86_64/exception_handlers.cpp b/src/oboskrnl/arch/x86_64/exception_handlers.cpp new file mode 100644 index 00000000..b4bcb383 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/exception_handlers.cpp @@ -0,0 +1,332 @@ +/* + oboskrnl/arch/x86_64/exception_handlers.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include +#include +#include + +#include + +#include +#include + +#include + +#include +#include + +#include +#include + +#include + +#include + +#include + +#if OBOS_KDBG_ENABLED +# include +#endif + +namespace obos +{ + namespace arch + { + struct page_fault_handler + { + void(*callback)(void* on, vmm::PageFaultErrorCode errorCode, const vmm::page_descriptor& pd); + vmm::PageFaultReason reason; + bool hasToBeInUserMode; + bool inUse; + }; + struct page_fault_handler_node + { + page_fault_handler_node *next, *prev; + page_fault_handler* data; + bool inUse; + }; + static page_fault_handler s_handlers[OBOS_MAX_PAGE_FAULT_HANDLERS]; + static locks::SpinLock s_handlerAllocatorLock; + static page_fault_handler *allocate_handler() + { + s_handlerAllocatorLock.Lock(); + page_fault_handler* ret = nullptr; + for (size_t i = 0; i < OBOS_MAX_PAGE_FAULT_HANDLERS; i++) + if (!s_handlers[i].inUse) + ret = &s_handlers[i]; + if (!ret) + return nullptr; + ret->inUse = true; + s_handlerAllocatorLock.Unlock(); + return ret; + } + static void free_handler(page_fault_handler* handler) + { + if (!(handler >= s_handlers && handler < s_handlers + OBOS_MAX_PAGE_FAULT_HANDLERS)) + return; + handler->inUse = false; + memzero(handler, sizeof(*handler)); + } + struct + { + page_fault_handler_node *head, *tail; + size_t nHandlers; + } s_pfHandlers; + static page_fault_handler_node s_nodes[OBOS_MAX_PAGE_FAULT_HANDLERS]; + static locks::SpinLock s_nodeAllocatorLock; + static page_fault_handler_node* allocate_node() + { + s_nodeAllocatorLock.Lock(); + page_fault_handler_node* ret = nullptr; + for (size_t i = 0; i < OBOS_MAX_PAGE_FAULT_HANDLERS; i++) + if (!s_nodes[i].inUse) + ret = &s_nodes[i]; + if (!ret) + return nullptr; + ret->inUse = true; + s_nodeAllocatorLock.Unlock(); + return ret; + } + static void free_node(page_fault_handler_node* node) + { + if (!(node >= s_nodes && node < s_nodes + OBOS_MAX_PAGE_FAULT_HANDLERS)) + return; + node->inUse = false; + memzero(node, sizeof(*node)); + } + + bool register_page_fault_handler(vmm::PageFaultReason reason, bool hasToBeInUserMode, void(*callback)(void* on, vmm::PageFaultErrorCode errorCode, const vmm::page_descriptor& pd)) + { + if (s_pfHandlers.nHandlers == OBOS_MAX_PAGE_FAULT_HANDLERS) + return false; + page_fault_handler* handler = allocate_handler(); + page_fault_handler_node* node = allocate_node(); + if (!handler || !node) + { + if (handler) + free_handler(handler); + if (node) + free_node(node); + return false; + } + handler->hasToBeInUserMode = hasToBeInUserMode; + handler->callback = callback; + handler->reason = reason; + node->data = handler; + if (!s_pfHandlers.head) + s_pfHandlers.head = node; + if (s_pfHandlers.tail) + s_pfHandlers.tail->next = node; + node->prev = s_pfHandlers.tail; + s_pfHandlers.tail = node; + s_pfHandlers.nHandlers++; + return true; + } + extern bool g_halt; + } + OBOS_NO_KASAN uint32_t getCPUId() + { + if (!scheduler::GetCPUPtr()) + return 0; // We're on the BSP + return scheduler::GetCPUPtr()->cpuId; + } + OBOS_NO_KASAN uint32_t getTID() + { + if (scheduler::GetCPUPtr() && scheduler::GetCPUPtr()->currentThread) + return scheduler::GetCPUPtr()->currentThread->tid; + return (uint32_t)-1; + } + OBOS_NO_KASAN OBOS_NO_UBSAN uint32_t getPID() + { + return 0; + } + OBOS_NO_KASAN OBOS_NO_UBSAN void defaultExceptionHandler(interrupt_frame* frame) + { + uint8_t oldIRQL = 0; + RaiseIRQL(IRQL_MASK_ALL, &oldIRQL); + uint32_t cpuId = getCPUId(), pid = getPID(), tid = getTID(); + bool whileInScheduler = false; +#if OBOS_KDBG_ENABLED + if (!kdbg::exceptionHandler(frame)) + { + LowerIRQL(oldIRQL); + return; + } +#endif + logger::panic( + nullptr, + "Exception %ld in %s-mode at 0x%016lx (cpu %d, pid %d, tid %d). IRQL: %d. Error code: %ld. whileInScheduler = %s\nDumping registers:\n" + "\tRDI: 0x%016lx, RSI: 0x%016lx, RBP: 0x%016lx\n" + "\tRSP: 0x%016lx, RBX: 0x%016lx, RDX: 0x%016lx\n" + "\tRCX: 0x%016lx, RAX: 0x%016lx, RIP: 0x%016lx\n" + "\t R8: 0x%016lx, R9: 0x%016lx, R10: 0x%016lx\n" + "\tR11: 0x%016lx, R12: 0x%016lx, R13: 0x%016lx\n" + "\tR14: 0x%016lx, R15: 0x%016lx, RFL: 0x%016lx\n" + "\t SS: 0x%016lx, DS: 0x%016lx, CS: 0x%016lx\n" + "\tCR0: 0x%016lx, CR2: 0x%016lx, CR3: 0x%016lx\n" + "\tCR4: 0x%016lx, CR8: 0x%016x, EFER: 0x%016lx\n", + frame->intNumber, + (frame->cs != 0x8) ? "user" : "kernel", + frame->rip, + cpuId, + pid, tid, + GetIRQL(), + frame->errorCode, + whileInScheduler ? "true" : "false", + frame->rdi, frame->rsi, frame->rbp, + frame->rsp, frame->rbx, frame->rdx, + frame->rcx, frame->rax, frame->rip, + frame->r8, frame->r9, frame->r10, + frame->r11, frame->r12, frame->r13, + frame->r14, frame->r15, frame->rflags, + frame->ss, frame->ds, frame->cs, + getCR0(), getCR2(), getCR3(), + getCR4(), oldIRQL , getEFER() + ); + asm volatile("nop"); + } + OBOS_NO_KASAN OBOS_NO_UBSAN bool callPageFaultHandlers(vmm::PageFaultReason reason, uintptr_t at, const vmm::page_descriptor &pd, uint32_t ec) + { + arch::page_fault_handler_node* node = arch::s_pfHandlers.head; + while (node) + { + if (!node->data) + goto down1; + if (node->data->hasToBeInUserMode && !(ec & vmm::PageFault_InUserMode)) + goto down1; + if (node->data->reason == reason) + node->data->callback((void*)at, (vmm::PageFaultErrorCode)ec, pd); + down1: + node = node->next; + } + if (ec & vmm::PageFault_InUserMode && reason == vmm::PageFaultReason::PageFault_AccessViolation) + return false; + if (ec & vmm::PageFault_DemandPage || reason == vmm::PageFaultReason::PageFault_DemandPaging) + return false; + return true; + } + OBOS_NO_KASAN OBOS_NO_UBSAN uint32_t DecodePFErrorCode(uintptr_t ec) + { + uintptr_t ret = 0; + if (ec & 1) + ret |= vmm::PageFault_IsPresent; + if (ec & 2) + ret |= vmm::PageFault_Write; + else + ret |= vmm::PageFault_Read; + if (ec & 4) + ret |= vmm::PageFault_InUserMode; + if (ec & 16) + ret |= vmm::PageFault_Execution; + return ret; + } + OBOS_NO_KASAN OBOS_NO_UBSAN void pageFaultHandler(interrupt_frame* frame) + { + uint8_t oldIRQL = 0; + RaiseIRQL(15, &oldIRQL); + arch::PageMap* pm = (arch::PageMap*)getCR3(); + uintptr_t at = getCR2(); + if (frame->errorCode & 1) + { + vmm::page_descriptor pd = {}; + arch::get_page_descriptor(pm, (void*)at, pd); + uintptr_t entry = 0; + if (pd.isHugePage) + entry = pm->GetL2PageMapEntryAt(at); + else + entry = pm->GetL1PageMapEntryAt(at); + if (!(entry & ((uintptr_t)1 << 9))) + goto fault; + uint32_t ec = DecodePFErrorCode(frame->errorCode); + ec |= vmm::PageFault_DemandPage; + if (!callPageFaultHandlers(vmm::PageFaultReason::PageFault_DemandPaging, at, pd, ec)) + { + LowerIRQL(oldIRQL); + return; + } + } + fault: + + // Call page fault handlers. + vmm::page_descriptor pd = {}; + arch::get_page_descriptor(pm, (void*)at, pd); + uint32_t ec = DecodePFErrorCode(frame->errorCode); + if (!callPageFaultHandlers(vmm::PageFaultReason::PageFault_AccessViolation, at, pd, ec)) + { + LowerIRQL(oldIRQL); + return; + } +#if OBOS_KDBG_ENABLED + if (!kdbg::exceptionHandler(frame)) + { + LowerIRQL(oldIRQL); + return; + } +#endif + uint32_t cpuId = getCPUId(), pid = getPID(), tid = getTID(); + bool whileInScheduler = false; + const char* action = (frame->errorCode & ((uintptr_t)1 << 1)) ? "write" : "read"; + if (frame->errorCode & ((uintptr_t)1 << 4)) + action = "execute"; + logger::panic( + nullptr, + "Page fault in %s-mode at 0x%016lx (cpu %d, pid %d, tid %d) while trying to %s a %s page. The address of this page is 0x%016lx. IRQL: %d. Error code: %ld. whileInScheduler = %s\nDumping registers:\n" + "\tRDI: 0x%016lx, RSI: 0x%016lx, RBP: 0x%016lx\n" + "\tRSP: 0x%016lx, RBX: 0x%016lx, RDX: 0x%016lx\n" + "\tRCX: 0x%016lx, RAX: 0x%016lx, RIP: 0x%016lx\n" + "\t R8: 0x%016lx, R9: 0x%016lx, R10: 0x%016lx\n" + "\tR11: 0x%016lx, R12: 0x%016lx, R13: 0x%016lx\n" + "\tR14: 0x%016lx, R15: 0x%016lx, RFL: 0x%016lx\n" + "\t SS: 0x%016lx, DS: 0x%016lx, CS: 0x%016lx\n" + "\tCR0: 0x%016lx, CR2: 0x%016lx, CR3: 0x%016lx\n" + "\tCR4: 0x%016lx, CR8: 0x%016x, EFER: 0x%016lx\n", + (frame->errorCode & ((uintptr_t)1 << 2)) ? "user" : "kernel", + frame->rip, + cpuId, + pid, tid, + action, + (frame->errorCode & ((uintptr_t)1 << 0)) ? "present" : "non-present", + getCR2(), + oldIRQL, + frame->errorCode, + whileInScheduler ? "true" : "false", + frame->rdi, frame->rsi, frame->rbp, + frame->rsp, frame->rbx, frame->rdx, + frame->rcx, frame->rax, frame->rip, + frame->r8, frame->r9, frame->r10, + frame->r11, frame->r12, frame->r13, + frame->r14, frame->r15, frame->rflags, + frame->ss, frame->ds, frame->cs, + getCR0(), getCR2(), getCR3(), + getCR4(), oldIRQL , getEFER() + ); + asm volatile("nop"); + } + OBOS_NO_KASAN OBOS_NO_UBSAN void nmiHandler(interrupt_frame* frame) + { + uint8_t oldIrql = 0; + RaiseIRQL(0xf, &oldIrql); + if (arch::g_halt) + while (1) + asm volatile("hlt"); +#if OBOS_KDBG_ENABLED + kdbg::exceptionHandler(frame); +#endif + logger::panic(nullptr, "NMI thrown by hardware! System Control Port: 0x%x.\n", (uint16_t)inb(0x92) | ((uint16_t)inb(0x61) << 8)); + } + + void RegisterExceptionHandlers() + { + RawRegisterInterrupt(0, (uintptr_t)defaultExceptionHandler); + RawRegisterInterrupt(1, (uintptr_t)defaultExceptionHandler); + RawRegisterInterrupt(2, (uintptr_t)nmiHandler); + for (uint8_t vec = 3; vec < 14; vec++) + RawRegisterInterrupt(vec, (uintptr_t)defaultExceptionHandler); + RawRegisterInterrupt(14, (uintptr_t)pageFaultHandler); + for (uint8_t vec = 15; vec < 32; vec++) + RawRegisterInterrupt(vec, (uintptr_t)defaultExceptionHandler); + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/execve.c b/src/oboskrnl/arch/x86_64/execve.c deleted file mode 100644 index dc9dface..00000000 --- a/src/oboskrnl/arch/x86_64/execve.c +++ /dev/null @@ -1,200 +0,0 @@ -/* - * oboskrnl/arch/x86_64/execve.c - * - * Copyright (c) 2024-2026 Omar Berrow - */ - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include -#include - -#include - -#include - -#include - -#include - -typedef struct -{ - OBOS_ALIGNAS(0x8) uint64_t a_type; - union { - long a_val; - void *a_ptr; - void (*a_fnc)(); - } a_un; -} auxv_t; - -enum { - AT_NULL, - AT_IGNORE, - AT_EXECFD, - AT_PHDR, - AT_PHENT, - AT_PHNUM, - AT_PAGESZ, - AT_BASE, - AT_FLAGS, - AT_ENTRY, - AT_NOTELF, - AT_UID, - AT_EUID, - AT_GID, - AT_EGID, - AT_PLATFORM, - AT_HWCAP, - AT_CLKTCK, - AT_SECURE = 23, - AT_BASE_PLATFORM, - AT_RANDOM, - AT_HWCAP2, - AT_EXECFN = 31, -}; - -static OBOS_NO_KASAN void allocate_string_vector_on_stack(char** vec, size_t cnt) -{ - for (size_t i = 0; i < cnt; i++) - { - size_t str_len = strlen(vec[i]); - char* str = CoreS_ThreadAlloca(&Core_GetCurrentThread()->context, str_len+1, nullptr); - if (!str) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "obos shat itself: your stack is not big enough to hold all these arguments"); - memcpy_k_to_usr(str, vec[i], str_len+1); - Free(OBOS_KernelAllocator, vec[i], str_len+1); - // stck_buf[i] = str; - vec[i] = str; - } -} - -static OBOS_NO_KASAN void write_vector_to_stack(char** vec, char** stck_buf, size_t cnt) -{ - for (size_t i = 0; i < cnt; i++) - stck_buf[i] = vec[i]; - stck_buf[cnt] = 0; - Free(OBOS_KernelAllocator, vec, (cnt+1)*sizeof(char*)); -} - -OBOS_NORETURN void Arch_GotoUser(uintptr_t rip, uintptr_t cr3, uintptr_t rsp); - -static __attribute__((target("xsave"))) __attribute__((target("avx"))) void reset_extended_state() -{ - asm volatile("fninit"); - memzero(Core_GetCurrentThread()->context.extended_ctx_ptr, Arch_GetXSaveRegionSize()); - if (obos_expect(Arch_HasXSAVE, true)) - __builtin_ia32_xrstor(Core_GetCurrentThread()->context.extended_ctx_ptr, __builtin_ia32_xgetbv(0)); - else - __builtin_ia32_fxrstor(Core_GetCurrentThread()->context.extended_ctx_ptr); - static const uint32_t mxcsr = 0x1f80; - asm volatile ("ldmxcsr (%0)" : :"r"(&mxcsr)); -} - -uintptr_t Arch_GotoUserBootstrap(uintptr_t udata) -{ - reset_extended_state(); - uintptr_t* user = (void*)udata; - Arch_GotoUser(user[0], user[1], user[2]); - return -1; -} - -OBOS_NORETURN OBOS_NO_KASAN void OBOSS_HandControlTo(struct context* ctx, struct exec_aux_values* aux) -{ - if (Core_GetIrql() < IRQL_DISPATCH) - { - irql oldIrql = Core_RaiseIrql(IRQL_DISPATCH); - OBOS_UNUSED(oldIrql); - } - Core_GetCurrentThread()->context.extended_ctx_ptr = Arch_AllocateXSAVERegion(); - - Core_GetCurrentThread()->context.frame.rsp = (uintptr_t)Core_GetCurrentThread()->context.stackBase + Core_GetCurrentThread()->context.stackSize; - size_t szAllocation = (15+aux->envpc+aux->argc)*sizeof(uint64_t); - allocate_string_vector_on_stack(aux->argv, aux->argc); - allocate_string_vector_on_stack(aux->envp, aux->envpc); - Core_GetCurrentThread()->context.frame.rsp &= ~0xf; - if (!((aux->argc+aux->envpc) % 2)) - Core_GetCurrentThread()->context.frame.rsp -= 8; - - void* uinit_vals = CoreS_ThreadAlloca(&Core_GetCurrentThread()->context, szAllocation, nullptr); - uint64_t* init_vals = Mm_MapViewOfUserMemory(ctx, uinit_vals, nullptr, szAllocation, 0, false, nullptr); - init_vals[0] = aux->argc; - - auxv_t* auxv = (auxv_t*)&init_vals[1+aux->argc+1+aux->envpc+1]; - - (*auxv).a_type = AT_PHDR; - (*auxv).a_un.a_ptr = aux->phdr.ptr; - auxv++; - - (*auxv).a_type = AT_PHENT; - (*auxv).a_un.a_val = aux->phdr.phent; - auxv++; - - (*auxv).a_type = AT_PHNUM; - (*auxv).a_un.a_val = aux->phdr.phnum; - auxv++; - - (*auxv).a_type = AT_ENTRY; - (*auxv).a_un.a_ptr = (void*)aux->elf.entry; - auxv++; - - (*auxv).a_type = AT_SECURE; - (*auxv).a_un.a_val = aux->at_secure; - auxv++; - - (*auxv).a_type = AT_NULL; - - write_vector_to_stack(aux->argv, (char**)(init_vals+1), aux->argc); - write_vector_to_stack(aux->envp, (char**)(init_vals+1+aux->argc+1), aux->envpc); - aux->argv = 0; - aux->envp = 0; - - // Core_GetCurrentThread()->context.frame.cr3 = ctx->pt; - // Core_GetCurrentThread()->context.frame.cs = 0x20|3; - // Core_GetCurrentThread()->context.frame.ss = 0x18|3; - // Core_GetCurrentThread()->context.frame.rip = aux->elf.real_entry; - // Core_GetCurrentThread()->context.frame.rflags = 0x200202; // CPUID,IF - // Core_GetCurrentThread()->context.irql = 0; - // Core_GetCurrentThread()->context.fs_base = 0; - // Core_GetCurrentThread()->context.gs_base = 0; - - Mm_VirtualMemoryFree(&Mm_KernelContext, init_vals, szAllocation); - init_vals = 0; - - OBOS_Debug("Handing off control to user program.\n"); - OBOS_Debug("NOTE: RSP=0x%p.\n", Core_GetCurrentThread()->context.frame.rsp); - - OBOS_ENSURE(!(Core_GetCurrentThread()->context.frame.rsp & 0xf)); - - Core_GetCurrentThread()->context.frame.rbp = 0; - uintptr_t udata[3] = { aux->elf.real_entry, ctx->pt, Core_GetCurrentThread()->context.frame.rsp }; - CoreS_SetKernelStack(Core_GetCurrentThread()->kernelStack); - CoreS_CallFunctionOnStack(Arch_GotoUserBootstrap, (uintptr_t)&udata); - OBOS_UNREACHABLE; -} - -void OBOSS_HandOffToInit(struct exec_aux_values* aux) -{ - OBOS_OpenStandardFDs(OBOS_CurrentHandleTable()); - - irql oldIrql = Core_RaiseIrql(IRQL_DISPATCH); - OBOS_UNUSED(oldIrql); - context* ctx = CoreS_GetCPULocalPtr()->currentContext; - - Core_GetCurrentThread()->context.stackBase = Mm_VirtualMemoryAlloc(ctx, nullptr, 4*1024*1024, OBOS_PROTECTION_USER_PAGE, VMA_FLAGS_GUARD_PAGE, nullptr, nullptr); - Core_GetCurrentThread()->context.stackSize = 4*1024*1024; - Core_GetCurrentThread()->stackFree = CoreH_VMAStackFree; - Core_GetCurrentThread()->stackFreeUserdata = ctx; - - CoreS_SetThreadPageTable(&Core_GetCurrentThread()->context, ctx->pt); - - OBOSS_HandControlTo(ctx, aux); -} diff --git a/src/oboskrnl/font.h b/src/oboskrnl/arch/x86_64/font.h similarity index 100% rename from src/oboskrnl/font.h rename to src/oboskrnl/arch/x86_64/font.h diff --git a/src/oboskrnl/arch/x86_64/gdbstub/alloc.c b/src/oboskrnl/arch/x86_64/gdbstub/alloc.c deleted file mode 100644 index 27f9195d..00000000 --- a/src/oboskrnl/arch/x86_64/gdbstub/alloc.c +++ /dev/null @@ -1,54 +0,0 @@ -/* - * oboskrnl/arch/x86_64/gdbstub/alloc.c - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include -#include -#include - -#include - -struct cmd_allocation_header -{ - size_t alloc_size; -}; - -void* Kdbg_Malloc(size_t sz) -{ - struct cmd_allocation_header* blk = nullptr; - blk = OBOS_NonPagedPoolAllocator->Allocate(OBOS_NonPagedPoolAllocator, sz+sizeof(*blk), nullptr); - blk->alloc_size = sz; - return blk + 1; -} - -void* Kdbg_Calloc(size_t nobj, size_t szobj) -{ - size_t sz = nobj * szobj; - return memzero(Kdbg_Malloc(sz), sz); -} - -void Kdbg_Free(void* buf) -{ - struct cmd_allocation_header* hdr = buf; - hdr--; - OBOS_NonPagedPoolAllocator->Free(OBOS_NonPagedPoolAllocator, hdr, hdr->alloc_size+sizeof(*hdr)); -} - -void* Kdbg_Realloc(void* buf, size_t newsize) -{ - if (!buf) - return Kdbg_Malloc(newsize); - if (!newsize) - { - Kdbg_Free(buf); - return nullptr; - } - struct cmd_allocation_header* hdr = buf; - hdr--; - size_t oldsz = hdr->alloc_size; - hdr->alloc_size = newsize; - hdr = OBOS_NonPagedPoolAllocator->Reallocate(OBOS_NonPagedPoolAllocator, hdr, sizeof(*hdr)+newsize, sizeof(*hdr)+oldsz, nullptr); - return hdr + 1; -} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/gdbstub/alloc.h b/src/oboskrnl/arch/x86_64/gdbstub/alloc.h deleted file mode 100644 index a47d3af6..00000000 --- a/src/oboskrnl/arch/x86_64/gdbstub/alloc.h +++ /dev/null @@ -1,17 +0,0 @@ -/* - * oboskrnl/arch/x86_64/gdbstub/alloc.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include - -// All memory for the gdb stub is allocated using the non-paged pool allocator - -void* Kdbg_Malloc(size_t sz); -void* Kdbg_Calloc(size_t nObjs, size_t szObj); -void* Kdbg_Realloc(void* ptr, size_t newSz); -void Kdbg_Free(void* ptr); \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/gdbstub/bp.c b/src/oboskrnl/arch/x86_64/gdbstub/bp.c deleted file mode 100644 index b8325959..00000000 --- a/src/oboskrnl/arch/x86_64/gdbstub/bp.c +++ /dev/null @@ -1,143 +0,0 @@ -/* - * oboskrnl/arch/x86_64/gdbstub/bp.c - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include -#include -#include -#include - -#include -#include -#include -#include - -#include - -#include -#include -#include -#include - -#include -#include - -#include -#include - -#define NO_ARGUMENTS \ -do {\ - OBOS_UNUSED(arguments);\ - OBOS_UNUSED(argumentsLen);\ -} while(0) -#define NO_RESPONSE \ -do {\ - OBOS_UNUSED(con);\ -} while(0) -#define NO_USERDATA \ -do {\ - OBOS_UNUSED(userdata);\ -} while(0) -#define NO_CTX \ -do {\ - OBOS_UNUSED(dbg_ctx);\ -} while(0) - -LIST_GENERATE(sw_breakpoint_list, struct sw_breakpoint, node); - -struct args -{ - uintptr_t address; - uintptr_t kind; -}; - -static struct args parse_arguments(const char* arguments, size_t argumentsLen) -{ - struct args ret = {}; - size_t addrLen = strnchr(arguments, ',', argumentsLen)-1; - size_t kindLen = strnchr(arguments+addrLen, ';', argumentsLen-addrLen); - if (kindLen != argumentsLen) - kindLen--; - ret.address = KdbgH_hex2bin(arguments, addrLen); - ret.kind = KdbgH_hex2bin(arguments+addrLen+1, kindLen); - return ret; -} - -#define X86_INT3 0xCC - -// Adds a software breakpoint. -obos_status Kdbg_GDB_Z0(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* dbg_ctx, void* userdata) -{ - NO_CTX; - NO_USERDATA; - struct args args = parse_arguments(arguments, argumentsLen); - char* response = nullptr; - sw_breakpoint *bp = Kdbg_Malloc(sizeof(*bp)); - bp->addr = args.address; - - uintptr_t phys = 0; - MmS_QueryPageInfo(dbg_ctx->interrupted_thread && dbg_ctx->interrupted_thread->proc ? - dbg_ctx->interrupted_thread->proc->ctx->pt : - dbg_ctx->interrupt_ctx.frame.cr3, bp->addr, nullptr, &phys); - if (!phys) - { - response = "E.Page fault"; - goto respond; - } - - phys += (bp->addr & 0xfff); - - bp->at = *(uint8_t*)Arch_MapToHHDM(phys); - *(uint8_t*)Arch_MapToHHDM(phys) = X86_INT3; - - LIST_APPEND(sw_breakpoint_list, &con->sw_breakpoints, bp); - response = "OK"; - respond: - return Kdbg_ConnectionSendPacket(con, response); -} -// Removes a software breakpoint. -obos_status Kdbg_GDB_z0(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* dbg_ctx, void* userdata) -{ - NO_CTX; - NO_USERDATA; - struct args args = parse_arguments(arguments, argumentsLen); - char* response = nullptr; - sw_breakpoint *bp = nullptr; - for (sw_breakpoint* node = LIST_GET_HEAD(sw_breakpoint_list, &con->sw_breakpoints); node;) - { - if (node->addr == args.address) - { - bp = node; - break; - } - - node = LIST_GET_NEXT(sw_breakpoint_list, &con->sw_breakpoints, node); - } - if (!bp) - { - response = "E.No such breakpoint at address"; - goto respond; - } - - uintptr_t phys = 0; - MmS_QueryPageInfo(dbg_ctx->interrupted_thread && dbg_ctx->interrupted_thread->proc ? - dbg_ctx->interrupted_thread->proc->ctx->pt : - dbg_ctx->interrupt_ctx.frame.cr3, bp->addr, nullptr, &phys); - if (!phys) - { - response = "E.Page fault"; - goto respond; - } - - phys += (bp->addr & 0xfff); - - *(uint8_t*)Arch_MapToHHDM(phys) = bp->at; - LIST_APPEND(sw_breakpoint_list, &con->sw_breakpoints, bp); - response = "OK"; - respond: - (void)0; - obos_status status = Kdbg_ConnectionSendPacket(con, response); - return status; -} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/gdbstub/bp.h b/src/oboskrnl/arch/x86_64/gdbstub/bp.h deleted file mode 100644 index b375b567..00000000 --- a/src/oboskrnl/arch/x86_64/gdbstub/bp.h +++ /dev/null @@ -1,17 +0,0 @@ -/* - * oboskrnl/arch/x86_64/gdbstub/bp.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include - -// Removes a software breakpoint. -obos_status Kdbg_GDB_z0(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* dbg_ctx, void* userdata); -// Adds a software breakpoint. -obos_status Kdbg_GDB_Z0(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* dbg_ctx, void* userdata); \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/gdbstub/breakpoint.h b/src/oboskrnl/arch/x86_64/gdbstub/breakpoint.h deleted file mode 100644 index 3de57c94..00000000 --- a/src/oboskrnl/arch/x86_64/gdbstub/breakpoint.h +++ /dev/null @@ -1,22 +0,0 @@ -/* - * oboskrnl/arch/x86_64/gdbstub/breakpoint.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include - -typedef LIST_HEAD(sw_breakpoint_list, struct sw_breakpoint) sw_breakpoint_list; -LIST_PROTOTYPE(sw_breakpoint_list, struct sw_breakpoint, node); - -typedef struct sw_breakpoint -{ - uintptr_t addr; - uint8_t at; // the byte at addr before setting it to a breakpoint instruction - LIST_NODE(sw_breakpoint_list, struct sw_breakpoint) node; -} sw_breakpoint; \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/gdbstub/connection.c b/src/oboskrnl/arch/x86_64/gdbstub/connection.c deleted file mode 100644 index 96b8ec2e..00000000 --- a/src/oboskrnl/arch/x86_64/gdbstub/connection.c +++ /dev/null @@ -1,441 +0,0 @@ -/* - * oboskrnl/arch/x86_64/gdbstub/connection.c - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include -#include -#include -#include -#include - -#include - -#include - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include - -enum -{ - FLAGS_ENABLE_ACK = 0x1, -}; - -static uint8_t mod256(const char* data, size_t len) -{ - uint8_t checksum = 0; - for (size_t i = 0; i < len; i++) - checksum += data[i]; - return checksum; -} -uintptr_t KdbgH_hex2bin(const char* str, unsigned size) -{ - uintptr_t ret = 0; - str += *str == '\n'; - for (int i = size - 1, j = 0; i > -1; i--, j++) - { - char c = str[i]; - uintptr_t digit = 0; - switch (c) - { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - digit = c - '0'; - break; - case 'A': - case 'B': - case 'C': - case 'D': - case 'E': - case 'F': - digit = (c - 'A') + 10; - break; - case 'a': - case 'b': - case 'c': - case 'd': - case 'e': - case 'f': - digit = (c - 'a') + 10; - break; - default: - break; - } - /*if (!j) - { - ret = digit; - continue; - }*/ - ret |= digit << (j * 4); - } - return ret; -} -// Must be a pipe-style driver, or stuff will go wrong. -obos_status Kdbg_ConnectionInitialize(gdb_connection* conn, const driver_ftable* pipe_interface, dev_desc pipe) -{ - if (!conn || !pipe_interface) - return OBOS_STATUS_INVALID_ARGUMENT; - conn->pipe_interface = pipe_interface; - conn->pipe = pipe; - conn->flags |= FLAGS_ENABLE_ACK; - return OBOS_STATUS_SUCCESS; -} -obos_status Kdbg_ConnectionSendPacket(gdb_connection* conn, const char* packet) -{ - if (!conn) - return OBOS_STATUS_INVALID_ARGUMENT; - if (!conn->pipe_interface) - return OBOS_STATUS_UNINITIALIZED; - // printf(">%s\n", packet); - uint8_t checksum = mod256(packet, strlen(packet)); - const char* format = "$%s#%02x"; - size_t szBuf = snprintf(nullptr, 0, format, packet, checksum); - char* buf = Kdbg_Calloc(szBuf+1, sizeof(char)); - snprintf(buf, szBuf+1, format, packet, checksum); - if (!(conn->flags & FLAGS_ENABLE_ACK)) - { - obos_status status = conn->pipe_interface->write_sync( - conn->pipe, - buf, szBuf, - 0,nullptr - ); - Kdbg_Free(buf); - return status; - } - for (size_t i = 0; i < 5; i++) - { - obos_status status = conn->pipe_interface->write_sync( - conn->pipe, - buf, szBuf, - 0,nullptr - ); - if (obos_is_error(status)) - { - Kdbg_Free(buf); - return status; - } - size_t nRead = 0; - char ack = 0; - size_t spin = 0; - try_again: - status = conn->pipe_interface->read_sync( - conn->pipe, - &ack, 1, - 0,&nRead - ); - if ((spin++) >= 1000) - { - Kdbg_Free(buf); - return OBOS_STATUS_SUCCESS; - } - if (nRead != 1) - goto try_again; - if (obos_is_error(status)) - { - Kdbg_Free(buf); - return status; - } - if (ack == '+') - { - Kdbg_Free(buf); - return OBOS_STATUS_SUCCESS; - } - else if (ack == '-') - continue; - else - { - Kdbg_Free(buf); - return OBOS_STATUS_INTERNAL_ERROR; // something stupid has happened. the gdb stub's ack enabled value and gdb's ack enabled value are probably out of sync - } - } - Kdbg_Free(buf); - return OBOS_STATUS_RETRY; -} -static char recv_char(gdb_connection* conn) -{ - size_t nRead = 0; - const size_t nToRead = 1; - char ret = 0; - size_t spin = 0; - while(nRead < nToRead && (spin++ < 100000)) - conn->pipe_interface->read_sync(conn->pipe, &ret, nToRead, 0, &nRead); - return ret; -} -obos_status Kdbg_ConnectionRecvPacket(gdb_connection* conn, char** packet, size_t* szPacket_) -{ - if (!conn || !packet || !szPacket_) - return OBOS_STATUS_INVALID_ARGUMENT; - retry: - (void)0; - char* rawPacket = nullptr; - size_t rawPacketLen = 0; - char ch = 0; - uint8_t calculatedChecksum = 0; - uint8_t remoteChecksum = 0; - char checksum[2] = {}; - bool ack = true; - size_t spin = 0; - while((ch = recv_char(conn)) != '$' && spin++ < 500000) - __builtin_ia32_pause(); - if (ch != '$') - { - ack = false; - goto acknowledge; - } - bool isEscaped = false; - while((ch = recv_char(conn)) != '#') - { - calculatedChecksum += ch; - if (ch == '}' && !isEscaped) - isEscaped = true; - else - { - if (isEscaped) - { - isEscaped = false; - ch ^= 0x20; // unescape the character. - } - rawPacketLen++; - rawPacket = Kdbg_Realloc(rawPacket, rawPacketLen); - rawPacket[rawPacketLen - 1] = ch; - } - } - rawPacket = Kdbg_Realloc(rawPacket, rawPacketLen+1); - rawPacket[rawPacketLen] = 0; - checksum[0] = recv_char(conn); - checksum[1] = recv_char(conn); - remoteChecksum = KdbgH_hex2bin(checksum, 2); - ack = remoteChecksum == calculatedChecksum; - acknowledge: - (void)0; - char ackCh = ack ? '+' : '-'; - conn->pipe_interface->write_sync(conn->pipe, &ackCh, 1, 0, nullptr); - if (!ack) - { - Kdbg_Free(rawPacket); - rawPacket = 0; - rawPacketLen = 0; - goto retry; - } - *packet = rawPacket; - *szPacket_ = rawPacketLen; - return OBOS_STATUS_SUCCESS; -} -obos_status Kdbg_ConnectionSetAck(gdb_connection* conn, bool ack) -{ - if (!conn) - return OBOS_STATUS_INVALID_ARGUMENT; - if (ack) - conn->flags |= FLAGS_ENABLE_ACK; - else - conn->flags &= ~FLAGS_ENABLE_ACK; - return OBOS_STATUS_SUCCESS; -} - -char* KdbgH_FormatResponse(const char* format, ...) -{ - va_list list; - va_list list2; - va_start(list, format); - va_copy(list2, list); - size_t bufSize = vsnprintf(nullptr, 0, format, list); - va_end(list); - char* buf = Kdbg_Calloc(bufSize+1, sizeof(char)); - vsnprintf(buf, bufSize+1, format, list2); - va_end(list2); - return buf; -} -char* KdbgH_FormatResponseSized(size_t bufSize, const char* format, ...) -{ - va_list list; - va_start(list, format); - char* buf = Kdbg_Calloc(bufSize+1, sizeof(char)); - vsnprintf(buf, bufSize+1, format, list); - va_end(list); - return buf; -} - -static bool initialized_handlers; -obos_status Kdbg_InitializeHandlers() -{ - if (initialized_handlers) - return OBOS_STATUS_ALREADY_INITIALIZED; - Kdbg_AddPacketHandler("qC", Kdbg_GDB_qC, nullptr); - Kdbg_AddPacketHandler("qfThreadInfo", Kdbg_GDB_q_ThreadInfo, nullptr); - Kdbg_AddPacketHandler("qsThreadInfo", Kdbg_GDB_q_ThreadInfo, nullptr); - Kdbg_AddPacketHandler("qAttached", Kdbg_GDB_qAttached, nullptr); - Kdbg_AddPacketHandler("qSupported", Kdbg_GDB_qSupported, nullptr); - Kdbg_AddPacketHandler("?", Kdbg_GDB_query_halt, nullptr); - Kdbg_AddPacketHandler("g", Kdbg_GDB_g, nullptr); - Kdbg_AddPacketHandler("G", Kdbg_GDB_G, nullptr); - Kdbg_AddPacketHandler("k", Kdbg_GDB_k, nullptr); - Kdbg_AddPacketHandler("vKill", Kdbg_GDB_k, nullptr); - Kdbg_AddPacketHandler("H", Kdbg_GDB_H, nullptr); - Kdbg_AddPacketHandler("T", Kdbg_GDB_T, nullptr); - Kdbg_AddPacketHandler("qRcmd", Kdbg_GDB_qRcmd, nullptr); - Kdbg_AddPacketHandler("m", Kdbg_GDB_m, nullptr); - Kdbg_AddPacketHandler("M", Kdbg_GDB_M, nullptr); - Kdbg_AddPacketHandler("c", Kdbg_GDB_c, nullptr); - Kdbg_AddPacketHandler("C", Kdbg_GDB_C, nullptr); - Kdbg_AddPacketHandler("s", Kdbg_GDB_s, nullptr); - Kdbg_AddPacketHandler("Z0", Kdbg_GDB_Z0, nullptr); - Kdbg_AddPacketHandler("z0", Kdbg_GDB_z0, nullptr); - Kdbg_AddPacketHandler("D", Kdbg_GDB_D, nullptr); - Kdbg_AddPacketHandler("vFile", Kdbg_GDB_vFile, nullptr); - Kdbg_AddPacketHandler("qXfer", Kdbg_GDB_qXfer, nullptr); - Arch_RawRegisterInterrupt(0x3, (uintptr_t)(void*)Kdbg_int3_handler); - Arch_RawRegisterInterrupt(0x1, (uintptr_t)(void*)Kdbg_int1_handler); - initialized_handlers = true; - return OBOS_STATUS_SUCCESS; -} - -#include -#include -#include -#include -#include -#include -#include -#include -#include "gdb_udp_backend.h" - -static gdb_connection current_connection; -static bool bound_gdb_stub = false; -static bool started_gdb_stub = false; - -obos_status SysS_GDBStubBindInet(struct sockaddr_in* uaddr, int proto) -{ - if (Sys_GetUid() != 0) - return OBOS_STATUS_ACCESS_DENIED; - if (bound_gdb_stub) - return OBOS_STATUS_IN_USE; - - struct sockaddr_in addr = {}; - obos_status status = memcpy_usr_to_k(&addr, uaddr, sizeof(addr)); - if (obos_is_error(status)) - return status; - - switch (proto) { - case IPPROTO_UDP: - { - net_tables* iface = nullptr; - status = NetH_GetLocalAddressInterface(&iface, addr.addr); - if (obos_is_error(status)) - break; - status = Kdbg_ConnectionInitializeUDP(¤t_connection, be16_to_host(addr.port), iface->interface); - if (obos_is_error(status)) - break; - OBOS_Log("Bound GDB Stub to interface '" MAC_ADDRESS_FORMAT "', port %d\n", MAC_ADDRESS_ARGS(iface->mac), be16_to_host(addr.port)); - break; - } - case IPPROTO_TCP: - OBOS_Warning("Cannot bind GDB Stub to TCP. Unimplemented.\n"); - return OBOS_STATUS_UNIMPLEMENTED; - case IPPROTO_IP: - default: - return OBOS_STATUS_INVALID_ARGUMENT; - } - bound_gdb_stub = obos_is_success(status); - return status; -} - -obos_status SysS_GDBStubBindDevice(handle desc) -{ - if (Sys_GetUid() != 0) - return OBOS_STATUS_ACCESS_DENIED; - if (bound_gdb_stub) - return OBOS_STATUS_IN_USE; - - OBOS_LockHandleTable(OBOS_CurrentHandleTable()); - obos_status status = OBOS_STATUS_SUCCESS; - handle_desc* fd = OBOS_HandleLookup(OBOS_CurrentHandleTable(), desc, HANDLE_TYPE_FD, false, &status); - if (!fd) - { - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - return status; - } - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - if (!fd->un.fd || ~fd->un.fd->flags & FD_FLAGS_OPEN) - return OBOS_STATUS_UNINITIALIZED; - - driver_header* header = Vfs_GetVnodeDriver(fd->un.fd->vn); - if (!header) - return OBOS_STATUS_INVALID_ARGUMENT; - - dev_desc new_desc = fd->un.fd->vn->desc; - if (header->ftable.reference_device) - status = header->ftable.reference_device(&new_desc); - if (obos_is_error(status)) - return status; - - status = Kdbg_ConnectionInitialize(¤t_connection, &header->ftable, new_desc); - bound_gdb_stub = obos_is_success(status); - - return status; -} - -static thread gdb_thread = {}; -static event gdb_connected = EVENT_INITIALIZE(EVENT_NOTIFICATION); - -static void gdb_defer_thread() -{ - Kdbg_Break(); - Core_EventSet(&gdb_connected, false); - Core_ExitCurrentThread(); -} - -obos_status SysS_GDBStubStart() -{ - if (Sys_GetUid() != 0) - return OBOS_STATUS_ACCESS_DENIED; - if (!bound_gdb_stub) - return OBOS_STATUS_UNINITIALIZED; - if (started_gdb_stub) - return OBOS_STATUS_ALREADY_INITIALIZED; - - current_connection.connection_active = true; - Kdbg_CurrentConnection = ¤t_connection; - Kdbg_InitializeHandlers(); - - thread_ctx ctx = {}; - const size_t stackSize = 0x4000; - void* stackBase = Mm_VirtualMemoryAlloc( - &Mm_KernelContext, - nullptr, stackSize, - 0, VMA_FLAGS_KERNEL_STACK, - nullptr, - nullptr - ); - - CoreS_SetupThreadContext(&ctx, (uintptr_t)gdb_defer_thread, 0, false, stackBase, stackSize); - - CoreH_ThreadInitialize(&gdb_thread, THREAD_PRIORITY_NORMAL, Core_DefaultThreadAffinity, &ctx); - Core_ProcessAppendThread(OBOS_KernelProcess, &gdb_thread); - CoreH_ThreadReady(&gdb_thread); - obos_status status = Core_WaitOnObject(WAITABLE_OBJECT(gdb_connected)); - if (CoreS_ForceYieldOnSyscallReturn) - CoreS_ForceYieldOnSyscallReturn(); - return status; -} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/gdbstub/connection.h b/src/oboskrnl/arch/x86_64/gdbstub/connection.h deleted file mode 100644 index 2e0d732a..00000000 --- a/src/oboskrnl/arch/x86_64/gdbstub/connection.h +++ /dev/null @@ -1,77 +0,0 @@ -/* - * oboskrnl/arch/x86_64/gdbstub/connection.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include -#include - -#include -#include - -#include - -#include - -#include - -typedef struct gdb_connection -{ - // Communication context - const driver_ftable* pipe_interface; - dev_desc pipe; - uint32_t flags; - bool connection_active; - - // Connection context. - struct - { - bool received_first; - thread_node* last_thread; - } q_ThreadInfo_ctx; - // bool swbreak_supported; - // bool hwbreak_supported; - // bool multiprocess_supported; - // bool vCont_supported; - // bool errormessage_supported; - // Bitfield: - // bit 0: swbreak - // bit 1: hwbreak - // bit 2: multiprocess - // bit 3: vCont - // bit 4: error-message - uint32_t gdb_supported; - sw_breakpoint_list sw_breakpoints; -} gdb_connection; -typedef struct gdb_ctx -{ - thread* interrupted_thread; - thread_ctx interrupt_ctx; - bool wake; -} gdb_ctx; -extern OBOS_EXPORT gdb_connection* Kdbg_CurrentConnection; - -uintptr_t KdbgH_hex2bin(const char* str, unsigned size); - -OBOS_EXPORT obos_status Kdbg_InitializeHandlers(); - -// Must be a pipe-style driver, or stuff will go wrong. -OBOS_EXPORT obos_status Kdbg_ConnectionInitialize(gdb_connection* conn, const driver_ftable* pipe_interface, dev_desc pipe); -obos_status Kdbg_ConnectionSendPacket(gdb_connection* conn, const char* packet); -// *packet is guaranteed to be nul-terminated. -obos_status Kdbg_ConnectionRecvPacket(gdb_connection* conn, char** packet, size_t* szPacket); -// NOTE: Does not send the packet to change the ack status -// You must do that yourself. -obos_status Kdbg_ConnectionSetAck(gdb_connection* conn, bool ack); - -char* KdbgH_FormatResponse(const char* format, ...); -// size needs to be the exact size of the output, or bad things will happen. -char* KdbgH_FormatResponseSized(size_t size, const char* format, ...); - -obos_status SysS_GDBStubBindInet(struct sockaddr_in* addr, int proto); -obos_status SysS_GDBStubBindDevice(handle devfd); -obos_status SysS_GDBStubStart(); \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/gdbstub/debug.c b/src/oboskrnl/arch/x86_64/gdbstub/debug.c deleted file mode 100644 index ff99c052..00000000 --- a/src/oboskrnl/arch/x86_64/gdbstub/debug.c +++ /dev/null @@ -1,150 +0,0 @@ -/* - * oboskrnl/arch/x86_64/gdbstub/debug.c - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include "allocators/base.h" -#include -#include -#include -#include - -#include -#include -#include - -#include -#include -#include -#include - -#include - -#include - -#include -#include -#include -#include - -gdb_connection* Kdbg_CurrentConnection; - -bool Kdbg_Paused = false; - -#define get_dbg_ctx() (CoreS_GetCPULocalPtr()->arch_specific.dbg_ctx) - -void Kdbg_Break() -{ - asm("int3"); -} -static mutex lock = MUTEX_INITIALIZE(); -void Kdbg_CallDebugExceptionHandler(interrupt_frame* frame, bool isSource) -{ - if (!Kdbg_CurrentConnection) - return; - if (isSource) - { - Kdbg_Paused = true; - ipi_lapic_info lapic = { - .isShorthand=true, - .info.shorthand = LAPIC_DESTINATION_SHORTHAND_ALL_BUT_SELF, - }; - ipi_vector_info vec = { - .deliveryMode = LAPIC_DELIVERY_MODE_NMI, - }; - Arch_LAPICSendIPI(lapic, vec); - } - get_dbg_ctx().interrupted_thread = Core_GetCurrentThread(); - get_dbg_ctx().interrupted_thread->flags |= THREAD_FLAGS_DEBUGGER_BLOCKED; - get_dbg_ctx().interrupt_ctx.cr3 = frame->cr3; - get_dbg_ctx().interrupt_ctx.irql = 0; - memcpy(&get_dbg_ctx().interrupt_ctx.frame, frame, sizeof(*frame)); - get_dbg_ctx().interrupt_ctx.gs_base = frame->cs & 0x3 ? rdmsr(0xc0000102) : (uintptr_t)CoreS_GetCPULocalPtr(); - get_dbg_ctx().interrupt_ctx.fs_base = rdmsr(0xc0000100); - Kdbg_GeneralDebugExceptionHandler(Kdbg_CurrentConnection, &get_dbg_ctx(), isSource); - memcpy(frame, &get_dbg_ctx().interrupt_ctx.frame, sizeof(*frame)); -} -void Kdbg_NotifyGDB(gdb_connection* con, uint8_t signal) -{ - char* packet = KdbgH_FormatResponse("T%02xthread:p%x.%x;", signal, Core_GetCurrentThread()->proc->pid+1, Core_GetCurrentThread()->tid); - Kdbg_ConnectionSendPacket(con, packet); -} - -void Kdbg_int3_handler(interrupt_frame* frame) -{ - asm("sti"); - static bool initialized = false; - obos_status status = Core_MutexTryAcquire(&lock); - if (obos_is_error(status)) - return; - if (initialized) - Kdbg_NotifyGDB(Kdbg_CurrentConnection, 0x05); // trap - // Adjust rip properly - uint8_t offset = 0; - if (*(uint8_t*)(frame->rip-1) == 0xcc /*int3*/) - offset = 1; // ... int3 (0xcc xx) - if (*(uint8_t*)(frame->rip-1) == 0xcd) - offset = 2; // ... int 3 (0xcd, 0x03, xx) - uintptr_t old_rip = frame->rip; - frame->rip -= offset; - sw_breakpoint *bp = nullptr; - for (sw_breakpoint* node = LIST_GET_HEAD(sw_breakpoint_list, &Kdbg_CurrentConnection->sw_breakpoints); node;) - { - if (node->addr == frame->rip) - { - bp = node; - break; - } - - node = LIST_GET_NEXT(sw_breakpoint_list, &Kdbg_CurrentConnection->sw_breakpoints, node); - } - Kdbg_CallDebugExceptionHandler(frame, true); - if ((old_rip - offset) == frame->rip) - { - if (!bp) - frame->rip = old_rip; // if the rip wasn't artifically modified, set rip back to the instruction it was before - // else - // frame->rip = frame->rip; // if the rip wasn't artifically modified, set rip back to the instruction it was before - } - Core_MutexRelease(&lock); - if (!initialized) - Kdbg_CurrentConnection->connection_active = true; - initialized = Kdbg_CurrentConnection->connection_active; - asm("cli"); -} -void Kdbg_int1_handler(interrupt_frame* frame) -{ - asm("sti"); - Core_MutexAcquire(&lock); - if (getDR6() & BIT(14)) - { - Kdbg_NotifyGDB(Kdbg_CurrentConnection, 0x05); // trap - frame->rflags &= ~RFLAGS_TRAP; - } - Kdbg_CallDebugExceptionHandler(frame, true); - Core_MutexRelease(&lock); - asm("cli"); -} - -void Kdbg_GeneralDebugExceptionHandler(gdb_connection* conn, gdb_ctx* dbg_ctx, bool isSource) -{ - if (!isSource) - { - while (Kdbg_Paused && !dbg_ctx->wake) - CoreH_DispatchDPCs(); - return; - } - while(dbg_ctx->interrupted_thread->flags & THREAD_FLAGS_DEBUGGER_BLOCKED && Kdbg_CurrentConnection->connection_active) - { - char* packet = nullptr; - size_t packetLen = 0; - Kdbg_ConnectionRecvPacket(conn, &packet, &packetLen); - // printf("<%s\n", packet); - obos_status st = Kdbg_DispatchPacket(conn, packet, packetLen, dbg_ctx); - if (obos_is_error(st)) - OBOS_Debug("Kdbg: While dispatching packet: Got status %d\n", st); - Kdbg_Free(packet); - } - Kdbg_Paused = false; -} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/gdbstub/debug.h b/src/oboskrnl/arch/x86_64/gdbstub/debug.h deleted file mode 100644 index 862099c8..00000000 --- a/src/oboskrnl/arch/x86_64/gdbstub/debug.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * oboskrnl/arch/x86_64/gdbstub/debug.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include - -#include - -#include - -extern bool Kdbg_Paused; - -OBOS_EXPORT void Kdbg_Break(); - -OBOS_EXPORT void Kdbg_CallDebugExceptionHandler(interrupt_frame* frame, bool isSource); -void Kdbg_NotifyGDB(gdb_connection* con, uint8_t signal); - -void Kdbg_int3_handler(interrupt_frame* frame); -void Kdbg_int1_handler(interrupt_frame* frame); - -void Kdbg_GeneralDebugExceptionHandler(gdb_connection* conn, gdb_ctx* dbg_ctx, bool isSource); \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/gdbstub/gdb_udp_backend.c b/src/oboskrnl/arch/x86_64/gdbstub/gdb_udp_backend.c deleted file mode 100644 index f2a46ec0..00000000 --- a/src/oboskrnl/arch/x86_64/gdbstub/gdb_udp_backend.c +++ /dev/null @@ -1,179 +0,0 @@ -/* - * oboskrnl/arch/x86_64/gdbstub/gdb_udp_backend.c - * - * Copyright (c) 2025 Omar Berrow -*/ - -#include -#include -#include -#include - -#include - -#include -#include -#include -#include - -#include - -#include - -#include - -#include - -#include -#include -#include - -#include - -#include -#include -#include - -static obos_status get_blk_size(dev_desc desc, size_t* blkSize) -{ - OBOS_UNUSED(desc); - if (!blkSize) - return OBOS_STATUS_INVALID_ARGUMENT; - *blkSize = 1; - return OBOS_STATUS_SUCCESS; -} - -static obos_status get_max_blk_count(dev_desc desc, size_t* count) -{ - OBOS_UNUSED(desc); - OBOS_UNUSED(count); - return OBOS_STATUS_INVALID_OPERATION; -} - -struct udp_handle { - #define UDP_HANDLE_MAGIC 0xAD70C43F - uint32_t magic; - - vnode* interface; - - ip_addr client; - uint16_t client_port; - - udp_port* bound_port; - ip_table_entry* table_ent; - - udp_recv_packet* curr_rx; - size_t rx_off; - - gdb_connection* con; -}; - -static obos_status read_sync(dev_desc desc, void* buf, size_t blkCount, size_t blkOffset, size_t* nBlkRead) -{ - OBOS_UNUSED(blkOffset); - if (!buf || !desc) - return OBOS_STATUS_INVALID_ARGUMENT; - struct udp_handle* hnd = (struct udp_handle*)desc; - if (hnd->magic != UDP_HANDLE_MAGIC) - return OBOS_STATUS_INVALID_ARGUMENT; - - while (!hnd->curr_rx) - { - obos_status status = Core_WaitOnObject(WAITABLE_OBJECT(hnd->bound_port->recv_event)); - if (obos_is_error(status)) - return status; - Core_EventClear(&hnd->bound_port->recv_event); - udp_recv_packet* recv_packet = LIST_GET_HEAD(frame_queue, &hnd->bound_port->packets); - if (!recv_packet) - continue; - if (!hnd->client.addr) - hnd->client.addr = recv_packet->src.addr.addr; - if (hnd->client.addr != recv_packet->src.addr.addr) - continue; // Discard packets from other clients. - hnd->client_port = recv_packet->src.port; - hnd->curr_rx = recv_packet; - } - - const size_t nRead = OBOS_MIN(blkCount, hnd->curr_rx->buffer_ptr.szObj); - memcpy(buf, hnd->curr_rx->buffer_ptr.obj + hnd->rx_off, nRead); - hnd->rx_off += blkCount; - - if (hnd->rx_off >= hnd->curr_rx->buffer_ptr.szObj) - { - udp_recv_packet* next = LIST_GET_NEXT(udp_recv_packet_list, &hnd->bound_port->packets, hnd->curr_rx); - OBOS_SharedPtrUnref(&hnd->curr_rx->packet_ptr); - hnd->curr_rx = next; - hnd->rx_off = 0; - } - - if (nBlkRead) - *nBlkRead = nRead; - - return OBOS_STATUS_SUCCESS; -} - -DefineNetFreeSharedPtr - -static obos_status write_sync(dev_desc desc, const void* buf, size_t blkCount, size_t blkOffset, size_t* nBlkWritten) -{ - OBOS_UNUSED(blkOffset); - if (!buf || !desc) - return OBOS_STATUS_INVALID_ARGUMENT; - struct udp_handle* hnd = (struct udp_handle*)desc; - if (hnd->magic != UDP_HANDLE_MAGIC) - return OBOS_STATUS_INVALID_ARGUMENT; - - udp_header* hdr = Allocate(OBOS_KernelAllocator, sizeof(udp_header)+blkCount, nullptr); - hdr->dest_port = host_to_be16(hnd->client_port); - hdr->src_port = host_to_be16(hnd->bound_port->port); - hdr->length = host_to_be16(sizeof(udp_header)+blkCount); - hdr->chksum = 0; - memcpy(hdr+1, buf, blkCount); - - shared_ptr *data = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(shared_ptr), nullptr); - OBOS_SharedPtrConstructSz(data, hdr, sizeof(*hdr)+blkCount); - data->free = OBOS_SharedPtrDefaultFree; - data->freeUdata = OBOS_KernelAllocator; - data->onDeref = NetFreeSharedPtr; - NetH_SendIPv4Packet(hnd->interface, hnd->table_ent, hnd->client, 0x11, 128, 0, OBOS_SharedPtrCopy(data)); - - if (nBlkWritten) - *nBlkWritten = blkCount; - - return OBOS_STATUS_SUCCESS; -} - -static void driver_cleanup_callback(){}; -static obos_status ioctl(dev_desc what, uint32_t request, void* argp) { OBOS_UNUSED(what); OBOS_UNUSED(request); OBOS_UNUSED(argp); return OBOS_STATUS_INVALID_IOCTL; } - -static driver_ftable ftable = { - .get_blk_size = get_blk_size, - .get_max_blk_count = get_max_blk_count, - .write_sync = write_sync, - .read_sync = read_sync, - .ioctl = ioctl, - .driver_cleanup_callback = driver_cleanup_callback, -}; - -obos_status Kdbg_ConnectionInitializeUDP(gdb_connection* conn, uint16_t bind_port, vnode* interface) -{ - if (!interface || !bind_port || !conn) - return OBOS_STATUS_INVALID_ARGUMENT; - struct udp_handle* handle = Kdbg_Calloc(1, sizeof(struct udp_handle)); - handle->interface = interface; - handle->table_ent = LIST_GET_HEAD(ip_table, &interface->net_tables->table); - handle->magic = UDP_HANDLE_MAGIC; - handle->con = conn; - - Core_PushlockAcquire(&interface->net_tables->udp_ports_lock, true); - - udp_port *bound_port = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(udp_port), nullptr); - bound_port->port = bind_port; - bound_port->recv_event = EVENT_INITIALIZE(EVENT_NOTIFICATION); - handle->bound_port = bound_port; - RB_INSERT(udp_port_tree, &interface->net_tables->udp_ports, bound_port); - - Core_PushlockRelease(&interface->net_tables->udp_ports_lock, true); - - return Kdbg_ConnectionInitialize(conn, &ftable, (dev_desc)handle); -} diff --git a/src/oboskrnl/arch/x86_64/gdbstub/gdb_udp_backend.h b/src/oboskrnl/arch/x86_64/gdbstub/gdb_udp_backend.h deleted file mode 100644 index 56efd5cb..00000000 --- a/src/oboskrnl/arch/x86_64/gdbstub/gdb_udp_backend.h +++ /dev/null @@ -1,16 +0,0 @@ -/* - * oboskrnl/arch/x86_64/gdbstub/gdb_udp_backend.h - * - * Copyright (c) 2025 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include - -#include - -obos_status Kdbg_ConnectionInitializeUDP(gdb_connection* conn, uint16_t bind_port, vnode* interface); diff --git a/src/oboskrnl/arch/x86_64/gdbstub/general_query.c b/src/oboskrnl/arch/x86_64/gdbstub/general_query.c deleted file mode 100644 index afc6acdf..00000000 --- a/src/oboskrnl/arch/x86_64/gdbstub/general_query.c +++ /dev/null @@ -1,422 +0,0 @@ -/* - * oboskrnl/arch/x86_64/gdbstub/general_query.c - * - * Copyright (c) 2024-2025 Omar Berrow -*/ - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include - -#include -#include -#include - -#include - -#include - -#define NO_ARGUMENTS \ -do {\ - OBOS_UNUSED(arguments);\ - OBOS_UNUSED(argumentsLen);\ -} while(0) -#define NO_RESPONSE \ -do {\ - OBOS_UNUSED(con);\ -} while(0) -#define NO_USERDATA \ -do {\ - OBOS_UNUSED(userdata);\ -} while(0) -#define NO_CTX \ -do {\ - OBOS_UNUSED(dbg_ctx);\ -} while(0) - -obos_status Kdbg_GDB_qC(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* dbg_ctx, void* userdata) -{ - NO_ARGUMENTS; - NO_USERDATA; - uint32_t tid = dbg_ctx->interrupted_thread->tid > 255 ? __builtin_bswap32(dbg_ctx->interrupted_thread->tid+1) : dbg_ctx->interrupted_thread->tid; - uint32_t pid = (dbg_ctx->interrupted_thread->proc->pid+1) > 255 ? __builtin_bswap32(dbg_ctx->interrupted_thread->proc->pid+1) : dbg_ctx->interrupted_thread->proc->pid+1; - char* response = KdbgH_FormatResponse("QCp%x.%x", pid, tid); - Kdbg_ConnectionSendPacket(con, response); - Kdbg_Free(response); - return OBOS_STATUS_SUCCESS; -} -obos_status Kdbg_GDB_q_ThreadInfo(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* dbg_ctx, void* userdata) -{ - NO_ARGUMENTS; - NO_RESPONSE; - NO_USERDATA; - NO_CTX; - char* response = nullptr; - bool shouldFreeResponse = true; - retry: - if (!con->q_ThreadInfo_ctx.last_thread && con->q_ThreadInfo_ctx.received_first) - { - // Respond with an l. - response = "l"; - shouldFreeResponse = false; - con->q_ThreadInfo_ctx.received_first = false; - goto respond; - } - if (!con->q_ThreadInfo_ctx.received_first) - { - con->q_ThreadInfo_ctx.received_first = true; - con->q_ThreadInfo_ctx.last_thread = OBOS_KernelProcess->threads.head; - } - while (con->q_ThreadInfo_ctx.last_thread && (con->q_ThreadInfo_ctx.last_thread->data->flags & THREAD_FLAGS_DIED)) - con->q_ThreadInfo_ctx.last_thread = con->q_ThreadInfo_ctx.last_thread->next; - if (!con->q_ThreadInfo_ctx.last_thread) - goto retry; - response = KdbgH_FormatResponse("mp01.%x", con->q_ThreadInfo_ctx.last_thread->data->tid); - con->q_ThreadInfo_ctx.last_thread = con->q_ThreadInfo_ctx.last_thread->next; - respond: - (void)0; - obos_status status = Kdbg_ConnectionSendPacket(con, response); - if (shouldFreeResponse) - Kdbg_Free(response); - return status; -} -obos_status Kdbg_GDB_QStartNoAckMode(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* dbg_ctx, void* userdata) -{ - NO_ARGUMENTS; - NO_RESPONSE; - NO_USERDATA; - NO_CTX; - return OBOS_STATUS_SUCCESS; -} -obos_status Kdbg_GDB_qSupported(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* dbg_ctx, void* userdata) -{ - NO_RESPONSE; - NO_USERDATA; - NO_CTX; - OBOS_UNUSED(argumentsLen); - // We support: - // swbreak - // hwbreak - // multiprocess - // vCont - // error-message - // NOTE: We don't have a limit, so we just set it to this. - // Our PacketSize is 4096 - // NOTE: Despite supporting QStartNoAckMode, we will not send it as it will also tell gdb we prefer it, which we don't, - // as serial connections aren't reliable (and neither is our driver). - const char* const supported[] = { - "swbreak", - "hwbreak", - "multiprocess", - // "vContSupported", - "error-message", - }; - const size_t packet_size = 4096; - size_t responseLen = 0; - char* response = nullptr; - for (size_t i = 0; arguments[i]; ) - { - size_t feature_len = strnchr(arguments + i, ';', argumentsLen-i)-1; - if (arguments[i+feature_len-1] == '+') - feature_len--; - bool isSupported = false; - size_t j = 0; - for (; j < sizeof(supported)/sizeof(supported[0]) && !isSupported; j++) - if (uacpi_strncmp(arguments + i, supported[j], feature_len) == 0) - isSupported = true; - if (isSupported) - { - // This bitfield was made specifically to match the indices of the supported array and have the same meaning. - con->gdb_supported |= (1<= argumentsLen) - return "496E636F7272656374206E756D626572206F6620617267756D656E74730a"; // Incorrect number of arguments - const char* endptr = nullptr; - uint16_t io_addr = OBOSH_StrToULL(arguments+commandLen+1, &endptr, 0); - if (!endptr) - return "496E76616C696420617267756D656E740A"; // Invald argument - printf("endptr=%s\n", endptr); - endptr++; - printf("endptr=%s\n", endptr); - if (endptr >= (argumentsLen+arguments)) - return "496E76616C696420617267756D656E740A"; // Invald argument - uint32_t data = OBOSH_StrToULL(endptr, &endptr, 0); - if (!endptr) - return "496E636F7272656374206E756D626572206F6620617267756D656E74730a"; // Incorrect number of arguments - printf("io addr = 0x%04x, data = 0x%x\n", io_addr, data); - *out_ioaddr = io_addr; - *out_iodata = data; - return "537563636573730A"; -} -// void OBOS_TestLocks(); -obos_status Kdbg_GDB_qRcmd(gdb_connection* con, const char* arguments_, size_t argumentsLen_, gdb_ctx* dbg_ctx, void* userdata) -{ - NO_CTX; - NO_USERDATA; - size_t argumentsLen = argumentsLen_ / 2; - char* arguments = Kdbg_Calloc(argumentsLen + 1, sizeof(char)); - for (size_t i = 0; i < argumentsLen; i++) - arguments[i] = KdbgH_hex2bin(arguments_ + (i*2), 2); - // printf("%s\n%s\n", arguments, arguments_); - size_t commandLen = strnchr(arguments, ' ', argumentsLen); - char* response = "556E6B6E6F776E20636F6D6D616E640A"; - if (!commandLen) - return Kdbg_ConnectionSendPacket(con, response); - if (commandLen != argumentsLen) - commandLen -= 1; - char* command = - (char*)memcpy(Kdbg_Calloc(commandLen+1, sizeof(char)), arguments, commandLen); - bool freeResponse = false; - if (strcmp(command, "ping")) - response = "706F6E670A"; - else if (strcmp(command, "io8_read")) - { - if ((commandLen+1) >= argumentsLen) - { - // Incorrect number of arguments - response = "496E636F7272656374206E756D626572206F6620617267756D656E74730a"; - goto down; - } - const char* endptr = nullptr; - uint16_t io_addr = OBOSH_StrToULL(arguments+commandLen+1, &endptr, 0); - if (!endptr) - { - response = "496E76616C696420617267756D656E740A"; - goto down; - } - char* read = KdbgH_FormatResponse("%02x", inb(io_addr)); - response = KdbgH_FormatResponse("3078%02x%02x0a", read[0], read[1]); - Kdbg_Free(read); - freeResponse = true; - } - else if (strcmp(command, "io16_read")) - { - if ((commandLen+1) >= argumentsLen) - { - // Incorrect number of arguments - response = "496E636F7272656374206E756D626572206F6620617267756D656E74730a"; - goto down; - } - const char* endptr = nullptr; - uint16_t io_addr = OBOSH_StrToULL(arguments+commandLen+1, &endptr, 0); - if (!endptr) - { - response = "496E76616C696420617267756D656E740A"; - goto down; - } - char* read = KdbgH_FormatResponse("%04x", inw(io_addr)); - response = KdbgH_FormatResponse("3078%02x%02x%02x%02x0a", read[0], read[1], read[2], read[3]); - Kdbg_Free(read); - freeResponse = true; - } - else if (strcmp(command, "io32_read")) - { - if ((commandLen+1) >= argumentsLen) - { - // Incorrect number of arguments - response = "496E636F7272656374206E756D626572206F6620617267756D656E74730a"; - goto down; - } - const char* endptr = nullptr; - uint16_t io_addr = OBOSH_StrToULL(arguments+commandLen+1, &endptr, 0); - if (!endptr) - { - response = "496E76616C696420617267756D656E740A"; - goto down; - } - char* read = KdbgH_FormatResponse("%08x", ind(io_addr)); - response = KdbgH_FormatResponse("3078%02x%02x%02x%02x%02x%02x%02x%02x0a", read[0], read[1], read[2], read[3], read[4], read[5], read[6], read[7]); - Kdbg_Free(read); - freeResponse = true; - } - else if (strcmp(command, "io8_write")) - { - uint16_t io_addr = 0; - uint32_t data = 0; - response = common_io_out(arguments, argumentsLen, commandLen, &io_addr, &data); - if (strcmp(response, "537563636573730A")) - { - printf("outb(0x%x, 0x%x)\n", io_addr, data & 0xff); - outb(io_addr, data & 0xff); - } - freeResponse = false; - } - else if (strcmp(command, "io16_write")) - { - uint16_t io_addr = 0; - uint32_t data = 0; - response = common_io_out(arguments, argumentsLen, commandLen, &io_addr, &data); - if (strcmp(response, "537563636573730A")) - { - printf("outw(0x%x, 0x%x)\n", io_addr, data & 0xffff); - outw(io_addr, data & 0xffff); - } - freeResponse = false; - } - else if (strcmp(command, "io32_write")) - { - uint16_t io_addr = 0; - uint32_t data = 0; - response = common_io_out(arguments, argumentsLen, commandLen, &io_addr, &data); - if (strcmp(response, "537563636573730A")) - { - printf("outd(0x%x, 0x%x)\n", io_addr, data & 0xff); - outd(io_addr, data & 0xff); - } - freeResponse = false; - } - down: - Kdbg_Free(arguments); - Kdbg_Free(command); - obos_status st = Kdbg_ConnectionSendPacket(con, response); - if (freeResponse) - Kdbg_Free(response); - return st; -} - -#define do_bounds_check(ptr, pstart, len) \ -({\ - uintptr_t _start = (uintptr_t)pstart;\ - uintptr_t _p = (uintptr_t)ptr;\ - (_p >= _start) && (_p < (_start+len));\ -}) - -obos_status Kdbg_GDB_qXfer(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* dbg_ctx, void* userdata) -{ - NO_USERDATA; - char* resp = "E.Unrecognized operation"; - bool free_resp = false; - - const char* op = arguments; - size_t op_len = strnchr(arguments, ':', argumentsLen)-1; - - const char* op_type = nullptr; - size_t len_op_type = 0; - - uint64_t annex = 0, offset = 0, length = 0; - OBOS_MAYBE_UNUSED bool have_annex = false, have_offset = false, have_length = false; - - - const char* op_args = op + (op_len+1); - size_t op_args_len = argumentsLen - (op_len+1); - - const char* args_iter = op_args; - size_t rel_args_size = op_args_len; - do { - const char* ptr = args_iter; - size_t len_ptr = strnchr(op_args, ':', rel_args_size)-1; - if (!do_bounds_check(ptr, op_args, op_args_len)) - return Kdbg_ConnectionSendPacket(con, "E.Not enough arguments"); - op_type = ptr; - len_op_type = len_ptr; - rel_args_size -= len_ptr + 1; - args_iter += len_ptr + 1; - } while(0); - do { - const char* ptr = args_iter; - size_t len_ptr = strnchr(ptr, ':', rel_args_size)-1; - if (!do_bounds_check(ptr, op_args, op_args_len)) - return Kdbg_ConnectionSendPacket(con, "E.Not enough arguments"); - annex = OBOSH_StrToULL(ptr, nullptr, 16); - have_annex = len_ptr > 0; - rel_args_size -= len_ptr + 1; - args_iter += len_ptr + 1; - } while(0); - do { - const char* ptr = args_iter; - size_t len_ptr = strnchr(ptr, ',', rel_args_size)-1; - if (!do_bounds_check(ptr, op_args, op_args_len)) - return Kdbg_ConnectionSendPacket(con, "E.Not enough arguments"); - offset = OBOSH_StrToULL(ptr, nullptr, 16); - have_offset = len_ptr > 0; - rel_args_size -= len_ptr + 1; - args_iter += len_ptr + 1; - } while(0); - do { - const char* ptr = args_iter; - size_t len_ptr = rel_args_size; - if (!do_bounds_check(ptr, op_args, op_args_len)) - return Kdbg_ConnectionSendPacket(con, "E.Not enough arguments"); - length = OBOSH_StrToULL(ptr, nullptr, 16); - have_length = len_ptr > 0; - rel_args_size -= len_ptr + 1; - args_iter += len_ptr + 1; - } while(0); - - if (uacpi_strncmp(op_type, "read", len_op_type) == 0) - { - const char* resp_buffer = nullptr; - size_t len_resp_buffer = 0; - bool resp_binary = false; - - if (uacpi_strncmp(op, "exec-file", op_len) == 0) - { - process* proc = have_annex ? Core_LookupProc(annex-1) : dbg_ctx->interrupted_thread->proc; - if (!proc) - return Kdbg_ConnectionSendPacket(con, "E.Could not find process"); - resp_binary = false; - if (proc->pid != 0) - resp_buffer = proc->exec_file; - else - resp_buffer = "oboskrnl"; - - len_resp_buffer = strlen(resp_buffer); - } - - if (!resp_binary) - { - if (offset < len_resp_buffer) - resp = KdbgH_FormatResponse("l%.*s", OBOS_MIN(len_resp_buffer-offset, length), resp_buffer + OBOS_MAX(offset, 0UL)); - else - resp = KdbgH_FormatResponse("l"); - } - } - - obos_status status = Kdbg_ConnectionSendPacket(con, resp); - if (free_resp) - Kdbg_Free(resp); - return status; -} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/gdbstub/general_query.h b/src/oboskrnl/arch/x86_64/gdbstub/general_query.h deleted file mode 100644 index f5033cb4..00000000 --- a/src/oboskrnl/arch/x86_64/gdbstub/general_query.h +++ /dev/null @@ -1,21 +0,0 @@ -/* - * oboskrnl/arch/x86_64/gdbstub/general_query.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include - -obos_status Kdbg_GDB_qC(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata); -obos_status Kdbg_GDB_q_ThreadInfo(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata); -obos_status Kdbg_GDB_QStartNoAckMode(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata); -obos_status Kdbg_GDB_qSupported(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata); -obos_status Kdbg_GDB_qAttached(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata); -obos_status Kdbg_GDB_qRcmd(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata); -obos_status Kdbg_GDB_qXfer(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata); -obos_status Kdbg_GDB_vMustReplyEmpty(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata); \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/gdbstub/packet_dispatcher.c b/src/oboskrnl/arch/x86_64/gdbstub/packet_dispatcher.c deleted file mode 100644 index 0c1d9930..00000000 --- a/src/oboskrnl/arch/x86_64/gdbstub/packet_dispatcher.c +++ /dev/null @@ -1,120 +0,0 @@ -/* - * oboskrnl/arch/x86_64/gdbstub/packet_dispatcher.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include -#include -#include -#include - -#include -#include -#include - -#include - -#include - -typedef struct gdb_packet -{ - char* packet; - size_t strlen_packet; - packet_handler handler; - void* userdata; - RB_ENTRY(gdb_packet) node; -} gdb_packet; -RB_HEAD(gdb_packet_tree, gdb_packet); -static struct gdb_packet_tree packet_handlers; - -static int cmp_packet(const gdb_packet *lhs, const gdb_packet *rhs) -{ - if (lhs->strlen_packet < rhs->strlen_packet) - return -1; - if (lhs->strlen_packet > rhs->strlen_packet) - return 1; - return uacpi_memcmp(lhs->packet, rhs->packet, lhs->strlen_packet); -} - -RB_PROTOTYPE_STATIC(gdb_packet_tree, gdb_packet, node, cmp_packet); - -void Kdbg_AddPacketHandler(const char* name, packet_handler handler, void* userdata) -{ - if (!name || !handler) - return; - size_t nameLen = strlen(name); - gdb_packet *packet = Kdbg_Calloc(1, sizeof(gdb_packet)); - *packet = (gdb_packet){ - .packet = memcpy(Kdbg_Calloc(nameLen + 1, sizeof(char)), name, nameLen), - .strlen_packet = nameLen, - .handler = handler, - .userdata = userdata - }; - RB_INSERT(gdb_packet_tree, &packet_handlers, packet); -} -obos_status Kdbg_DispatchPacket(gdb_connection* con, const char* packet, size_t packetLen, gdb_ctx* ctx) -{ - if (!con || !packet || !packetLen) - return OBOS_STATUS_INVALID_ARGUMENT; - const char* name = nullptr; - size_t nameLen = 1; - size_t argsOffset = 0; - switch (*packet) { - case 'v': - { - nameLen = strnchr(packet, ';', packetLen) == packetLen ? - strnchr(packet, '?', packetLen) : - strnchr(packet, ';', packetLen); - if (nameLen == packetLen) - nameLen = strnchr(packet, ':', packetLen); - if (nameLen != packetLen) - nameLen--; - name = packet; - argsOffset = nameLen + (nameLen!=packetLen); - break; - } - case 'Q': - case 'q': - { - size_t comma_offset = strnchr(packet, ',', packetLen); - size_t colon_offset = strnchr(packet, ':', packetLen); - nameLen = colon_offset == packetLen ? ((comma_offset == packetLen) ? packetLen : comma_offset-1) : colon_offset-1; - name = packet; - argsOffset = nameLen + (nameLen!=packetLen); - break; - } - case 'z': - case 'Z': - { - nameLen = 2; - name = packet; - argsOffset = nameLen+(nameLen!=packetLen); - break; - } - default: - { - argsOffset = 1; - name = packet; - nameLen = 1; - break; - } - } - const char* arguments = packet + argsOffset; - size_t szArguments = packetLen - argsOffset; - gdb_packet key = { - .packet = (char*)name, - .strlen_packet = nameLen, - }; - const gdb_packet *found_packet = RB_FIND(gdb_packet_tree, &packet_handlers, &key); - if (!found_packet) - { - Kdbg_ConnectionSendPacket(con, ""); - return OBOS_STATUS_UNHANDLED; - } - if (name != packet) - Kdbg_Free((char*)name); - return found_packet->handler(con, arguments, szArguments, ctx, found_packet->userdata); -} - -RB_GENERATE_STATIC(gdb_packet_tree, gdb_packet, node, cmp_packet); diff --git a/src/oboskrnl/arch/x86_64/gdbstub/packet_dispatcher.h b/src/oboskrnl/arch/x86_64/gdbstub/packet_dispatcher.h deleted file mode 100644 index 7dec457b..00000000 --- a/src/oboskrnl/arch/x86_64/gdbstub/packet_dispatcher.h +++ /dev/null @@ -1,18 +0,0 @@ -/* - * oboskrnl/arch/x86_64/gdbstub/packet_dispatcher.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include - -typedef obos_status(*packet_handler)(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata); -void Kdbg_AddPacketHandler(const char* name, packet_handler handler, void* userdata); -// packet must be nul-terminated -// or bad stuff might happen. -obos_status Kdbg_DispatchPacket(gdb_connection* con, const char* packet, size_t packetLen, gdb_ctx* ctx); \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/gdbstub/stop_reply.c b/src/oboskrnl/arch/x86_64/gdbstub/stop_reply.c deleted file mode 100644 index 1fee992b..00000000 --- a/src/oboskrnl/arch/x86_64/gdbstub/stop_reply.c +++ /dev/null @@ -1,516 +0,0 @@ -/* - * oboskrnl/arch/x86_64/gdbstub/stop_reply.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include "arch/x86_64/pmm.h" -#include "power/shutdown.h" -#include -#include -#include -#include - -#include -#include -#include -#include - -#include -#include - -#include -#include -#include -#include - -#include -#include - -#include -#include - -#include - -#include - -#define NO_ARGUMENTS \ -do {\ - OBOS_UNUSED(arguments);\ - OBOS_UNUSED(argumentsLen);\ -} while(0) -#define NO_RESPONSE \ -do {\ - OBOS_UNUSED(con);\ -} while(0) -#define NO_USERDATA \ -do {\ - OBOS_UNUSED(userdata);\ -} while(0) -#define NO_CTX \ -do {\ - OBOS_UNUSED(dbg_ctx);\ -} while(0) -// 0xffffffff means all -// 0xfffffffe means current -static size_t parse_gdb_thread_id(const char* id, size_t len, uint32_t* pid, uint32_t* tid); - -// '?' packet -// Queries the stop reason. -obos_status Kdbg_GDB_query_halt(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* dbg_ctx, void* userdata) -{ - NO_USERDATA; - NO_CTX; - NO_ARGUMENTS; - char* response = KdbgH_FormatResponse("T05thread:p%x.%x;%s", - (dbg_ctx->interrupted_thread->proc->pid+1) >= 256 ? __builtin_bswap32(dbg_ctx->interrupted_thread->proc->pid + 1) : dbg_ctx->interrupted_thread->proc->pid + 1, - dbg_ctx->interrupted_thread->tid >= 256 ? __builtin_bswap32(dbg_ctx->interrupted_thread->tid) : dbg_ctx->interrupted_thread->tid, - con->gdb_supported & BIT(0) ? - ";swbreak:;" : "" - ); - obos_status status = Kdbg_ConnectionSendPacket(con, response); - Kdbg_Free(response); - return status; -} -thread* current_g_thread = nullptr; -thread* current_c_thread = nullptr; -bool c_all_threads = false; -// static void strreverse(char* begin, int size) -// { -// int i = 0; -// char tmp = 0; - -// for (i = 0; i < size / 2; i++) -// { -// tmp = begin[i]; -// begin[i] = begin[size - i - 1]; -// begin[size - i - 1] = tmp; -// } -// } -// Credit: http://www.strudel.org.uk/itoa/ -// static char* itoa_unsigned(uintptr_t value, char* result, int base) -// { -// // check that the base if valid - -// if (base < 2 || base > 16) -// { -// *result = 0; -// return result; -// } - -// char* out = result; - -// uintptr_t quotient = value; - -// do -// { - -// uintptr_t abs = /*(quotient % base) < 0 ? (-(quotient % base)) : */(quotient % base); - -// *out = "0123456789abcdef"[abs]; - -// ++out; - -// quotient /= base; - -// } while (quotient); - -// strreverse(result, out - result); - -// return result; - -// } -static void format_register64(char** str, uintptr_t reg) -{ - reg = __builtin_bswap64(reg); - (*str) += snprintf(*str, 17, "%016lx", reg); -} -static void format_register32(char** str, uintptr_t reg) -{ - reg = __builtin_bswap32(reg); - (*str) += snprintf(*str, 9, "%08x", reg); -} -// Read Registers -obos_status Kdbg_GDB_g(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata) -{ - NO_USERDATA; - OBOS_UNUSED(ctx); - NO_ARGUMENTS; - // This'll be horrid.... - // const char* format = - // // rax,rbx,rcx,rdx,rsi,rdi,rbp,rsp - // "%016lx%016lx%016lx%016lx%016lx%016lx%016lx%016lx" // that's 8 - // // r8-r15 - // "%016lx%016lx%016lx%016lx%016lx%016lx%016lx%016lx" // that's 8 - // // rip, eflags, cc, ss, ds, es, fs, gs - // "%016lx%08x%08x%08x%08x%08x%08x%08x"; // that's 8 - // st0-st7 (ignore, set to zero) - // "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" - // // a bunch of fpu ctrl registers - // "0000000000000000000000000000000000000000000000000000000000000000" - if (!current_g_thread || !current_g_thread->masterCPU) - current_g_thread = Core_GetCurrentThread(); - gdb_ctx dbg_ctx = current_g_thread->masterCPU->arch_specific.dbg_ctx; - if (current_g_thread->masterCPU == CoreS_GetCPULocalPtr() && current_g_thread != Core_GetCurrentThread()) - dbg_ctx.interrupt_ctx = current_g_thread->context; - interrupt_frame* frame = &dbg_ctx.interrupt_ctx.frame; - // char* response = KdbgH_FormatResponseSized(320, format, - // __builtin_bswap64(frame->rax),__builtin_bswap64(frame->rbx),__builtin_bswap64(frame->rcx),__builtin_bswap64(frame->rdx),__builtin_bswap64(frame->rsi),__builtin_bswap64(frame->rdi),__builtin_bswap64(frame->rbp),__builtin_bswap64(frame->rsp), - // __builtin_bswap64(frame->r8) ,__builtin_bswap64(frame->r9) ,__builtin_bswap64(frame->r10),__builtin_bswap64(frame->r11),__builtin_bswap64(frame->r12),__builtin_bswap64(frame->r13),__builtin_bswap64(frame->r14),__builtin_bswap64(frame->r15), - // __builtin_bswap64(frame->rip),__builtin_bswap32(frame->rflags),__builtin_bswap32(frame->cs) ,__builtin_bswap32(frame->ss) ,__builtin_bswap32(frame->ds) , 0,0,0 - // ); - char* response = Kdbg_Calloc(329, sizeof(char)); - char* iter = response; - format_register64(&iter, frame->rax); - format_register64(&iter, frame->rbx); - format_register64(&iter, frame->rcx); - format_register64(&iter, frame->rdx); - format_register64(&iter, frame->rsi); - format_register64(&iter, frame->rdi); - format_register64(&iter, frame->rbp); - format_register64(&iter, frame->rsp); - format_register64(&iter, frame->r8); - format_register64(&iter, frame->r9); - format_register64(&iter, frame->r10); - format_register64(&iter, frame->r11); - format_register64(&iter, frame->r12); - format_register64(&iter, frame->r13); - format_register64(&iter, frame->r14); - format_register64(&iter, frame->r15); - format_register64(&iter, frame->rip); - format_register32(&iter, frame->rflags); - format_register32(&iter, frame->cs); - format_register32(&iter, frame->ss); - format_register32(&iter, frame->ds); - format_register32(&iter, 0); - format_register32(&iter, 0); - format_register32(&iter, 0); - obos_status status = Kdbg_ConnectionSendPacket(con, response); - Kdbg_Free(response); - return status; -} -static uintptr_t get_register64(const char** str) -{ - uintptr_t reg = KdbgH_hex2bin(*str, 16); - (*str) += 16; - return __builtin_bswap64(reg); -} -static uintptr_t get_register32(const char** str) -{ - uintptr_t reg = KdbgH_hex2bin(*str, 8); - (*str) += 8; - return __builtin_bswap32(reg & 0xffffffff); -} -// Write Registers -obos_status Kdbg_GDB_G(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata) -{ - NO_USERDATA; - OBOS_UNUSED(argumentsLen); - OBOS_UNUSED(ctx); - if (!current_g_thread || !current_g_thread->masterCPU) - current_g_thread = Core_GetCurrentThread(); - gdb_ctx *dbg_ctx = ¤t_g_thread->masterCPU->arch_specific.dbg_ctx; - interrupt_frame* frame = &dbg_ctx->interrupt_ctx.frame; - if (current_g_thread->masterCPU == CoreS_GetCPULocalPtr() && current_g_thread != Core_GetCurrentThread()) - frame = ¤t_g_thread->context.frame; - // rax,rbx,rcx,rdx,rsi,rdi,rbp,rsp - // r8-r15 - // rip, eflags, cc, ss, ds, es, fs, gs - const char* iter = arguments; - frame->rax = get_register64(&iter); - frame->rbx = get_register64(&iter); - frame->rcx = get_register64(&iter); - frame->rdx = get_register64(&iter); - frame->rsi = get_register64(&iter); - frame->rdi = get_register64(&iter); - frame->rbp = get_register64(&iter); - frame->rsp = get_register64(&iter); - frame->r8 = get_register64(&iter); - frame->r9 = get_register64(&iter); - frame->r10 = get_register64(&iter); - frame->r11 = get_register64(&iter); - frame->r12 = get_register64(&iter); - frame->r13 = get_register64(&iter); - frame->r14 = get_register64(&iter); - frame->r15 = get_register64(&iter); - frame->rip = get_register64(&iter); - frame->rflags = get_register32(&iter); - frame->cs = get_register32(&iter); - frame->ss = get_register32(&iter); - frame->cs = get_register32(&iter); - get_register32(&iter); - get_register32(&iter); - get_register32(&iter); - return Kdbg_ConnectionSendPacket(con, "OK"); -} -// Kill the kernel (shutdown) -obos_status Kdbg_GDB_k(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* dbg_ctx, void* userdata) -{ - NO_CTX; - NO_RESPONSE; - NO_ARGUMENTS; - NO_USERDATA; - // OBOS_Log("%s: GDB sent kill command. Requesting system shutdown...\n", __func__); - OBOS_Shutdown(); - OBOS_UNREACHABLE; - return OBOS_STATUS_SUCCESS; -} -obos_status Kdbg_GDB_D(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* dbg_ctx, void* userdata) -{ - NO_CTX; - NO_RESPONSE; - NO_ARGUMENTS; - NO_USERDATA; - Kdbg_Paused = false; - Kdbg_CurrentConnection->connection_active = false; - Kdbg_CurrentConnection->gdb_supported = 0; - Kdbg_CurrentConnection->flags = 0; - return Kdbg_ConnectionSendPacket(con, "OK"); -} -// Read Memory -OBOS_NO_KASAN obos_status Kdbg_GDB_m(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* dbg_ctx, void* userdata) -{ - NO_USERDATA; - dbg_ctx = &CoreS_GetCPULocalPtr()->arch_specific.dbg_ctx; - size_t addrLen = strnchr(arguments, ',', argumentsLen)-1; - if (addrLen > 16) - return Kdbg_ConnectionSendPacket(con, ""); - uintptr_t address = KdbgH_hex2bin(arguments, addrLen); - if ((argumentsLen-addrLen-1) > 16) - return Kdbg_ConnectionSendPacket(con, ""); - size_t memoryLen = KdbgH_hex2bin(arguments+addrLen+1, argumentsLen-addrLen-1); - // printf("GDB requested %d bytes of memory at %p\n", memoryLen, address); - uintptr_t top = address + memoryLen; - // Fun fact! - // This buffer takes more bytes than the actual memory takes! - char* response = Kdbg_Calloc(memoryLen*2+1, sizeof(char)); - size_t i = 0; - size_t nRead = 0; - static __attribute__((nonstring)) const char hexmask[16] = "0123456789abcdef"; - uintptr_t curr_phys = 0; - uintptr_t last_virt = 0; - for (uintptr_t addr = address; addr < top && nRead < memoryLen; addr++, i += 2, nRead++) - { - if ((addr & ~0xfff) != last_virt) - MmS_QueryPageInfo(dbg_ctx->interrupt_ctx.cr3, addr & ~0xfff, nullptr, &curr_phys); - if (!curr_phys) - break; - uint8_t byte = *(uint8_t*)Arch_MapToHHDM(curr_phys + (addr & 0xfff)); - response[i+1] = hexmask[byte&0xf]; - response[i] = hexmask[byte>>4]; - // printf("%d\n", i); - last_virt = addr & ~0xfff; - } - obos_status status = Kdbg_ConnectionSendPacket(con, response); - Kdbg_Free(response); - return status; -} -// Write Memory -obos_status Kdbg_GDB_M(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* dbg_ctx, void* userdata) -{ - NO_USERDATA; - dbg_ctx = &CoreS_GetCPULocalPtr()->arch_specific.dbg_ctx; - size_t addrLen = strnchr(arguments, ',', argumentsLen)-1; - if (addrLen > 16) - return Kdbg_ConnectionSendPacket(con, "E.Invalid address."); - uintptr_t address = KdbgH_hex2bin(arguments, addrLen); - size_t argv2_len = strnchr(arguments+addrLen+1, ':', argumentsLen-addrLen-1)-1; - if (argv2_len > 16) - return Kdbg_ConnectionSendPacket(con, "E.Invalid size."); - size_t memoryLen = KdbgH_hex2bin(arguments+addrLen+1, argumentsLen-addrLen-1); - uintptr_t top = address + memoryLen; - size_t i = 0; - size_t nRead = 0; - // static const char hexmask[16] = "0123456789abcdef"; - const char* iter = arguments+strnchr(arguments, ':', argumentsLen); - uintptr_t curr_phys = 0; - uintptr_t last_virt = address & ~0xfff; - const char* response = "OK"; - for (uintptr_t addr = address; addr < top && nRead < memoryLen; addr++, i += 2, nRead++, iter += 2) - { - if ((addr & ~0xfff) != last_virt) - MmS_QueryPageInfo(dbg_ctx->interrupt_ctx.cr3, addr & ~0xfff, nullptr, &curr_phys); - if (!curr_phys) - { - // TODO: Check this before hand? - response = "E.Memory not mapped."; - break; - } - *(uint8_t*)Arch_MapToHHDM(curr_phys + (addr & 0xfff)) = KdbgH_hex2bin(iter, 2); - } - obos_status status = Kdbg_ConnectionSendPacket(con, response); - return status; -} -// Step -obos_status Kdbg_GDB_c(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* dbg_ctx, void* userdata) -{ - NO_ARGUMENTS; - NO_RESPONSE; - NO_CTX; - NO_USERDATA; - if ((!current_c_thread || !current_c_thread->masterCPU) && !c_all_threads) - current_c_thread = Core_GetCurrentThread(); - if (!c_all_threads) - { - current_c_thread->flags &= ~THREAD_FLAGS_DEBUGGER_BLOCKED; - current_c_thread->masterCPU->arch_specific.dbg_ctx.interrupt_ctx.frame.rflags &= ~RFLAGS_TRAP; - } - else - { - for (size_t i = 0; i < Core_CpuCount; i++) - { - Core_CpuInfo[i].arch_specific.dbg_ctx.interrupted_thread->flags &= ~THREAD_FLAGS_DEBUGGER_BLOCKED; - Core_CpuInfo[i].arch_specific.dbg_ctx.interrupt_ctx.frame.rflags &= ~RFLAGS_TRAP; - } - // Kdbg_Paused = false; - } - return OBOS_STATUS_SUCCESS; -} -obos_status Kdbg_GDB_C(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* dbg_ctx, void* userdata) -{ - return Kdbg_GDB_c(con, arguments, argumentsLen, dbg_ctx, userdata); -} -obos_status Kdbg_GDB_s(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* dbg_ctx, void* userdata) -{ - NO_ARGUMENTS; - NO_RESPONSE; - NO_CTX; - NO_USERDATA; - // TODO: Support arguments. - // c_all_threads = false; - if ((!current_c_thread || !current_c_thread->masterCPU) && !c_all_threads) - current_c_thread = Core_GetCurrentThread(); - // FIXME: Stepping all threads breaks everything - if (!c_all_threads) - { - current_c_thread->flags &= ~THREAD_FLAGS_DEBUGGER_BLOCKED; - current_c_thread->masterCPU->arch_specific.dbg_ctx.interrupt_ctx.frame.rflags |= RFLAGS_TRAP; - } - else - { - for (size_t i = 0; i < Core_CpuCount; i++) - { - Core_CpuInfo[i].arch_specific.dbg_ctx.interrupt_ctx.frame.rflags |= RFLAGS_TRAP; - Core_CpuInfo[i].arch_specific.dbg_ctx.interrupted_thread->flags &= ~THREAD_FLAGS_DEBUGGER_BLOCKED; - } - } - // Kdbg_Paused = false; - return OBOS_STATUS_SUCCESS; -} -// Query thread status. -obos_status Kdbg_GDB_T(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* dbg_ctx, void* userdata) -{ - NO_USERDATA; - NO_CTX; - NO_ARGUMENTS; - uint32_t tid = 0; - uint32_t pid = 0; // ignored - parse_gdb_thread_id(arguments, argumentsLen, &pid, &tid); - if (pid == 0xfffffffe) - pid = Core_GetCurrentThread()->proc->pid+1; - thread* thr = nullptr; - if (tid == 0xfffffffe) - thr = Core_GetCurrentThread(); - else if (tid == (uint32_t)-1) - { - // TODO: Implement. - return Kdbg_ConnectionSendPacket(con, "E.Could not find thread"); - } - else - { - for(thread_node* node = OBOS_KernelProcess->threads.head; node && !thr; ) - { - if (node->data->tid == tid) - thr = node->data; - node = node->next; - } - } - if (!thr) - return Kdbg_ConnectionSendPacket(con, ""); - if (thr->flags & THREAD_FLAGS_DIED) - return Kdbg_ConnectionSendPacket(con, ""); - return Kdbg_ConnectionSendPacket(con, "OK"); -} -// These packets: g,G,k,m,M -// But multithreaded -obos_status Kdbg_GDB_H(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* dbg_ctx, void* userdata) -{ - NO_CTX; - NO_USERDATA; - // Forward the arguments. - OBOS_ASSERT(argumentsLen); - uint32_t tid = 0; - uint32_t pid = 0; // ignored - parse_gdb_thread_id(arguments+1, argumentsLen-1, &pid, &tid); - if (pid == 0xfffffffe) - pid = Core_GetCurrentThread()->proc->pid+1; - thread** pThr = arguments[0] == 'c' ? ¤t_c_thread : ¤t_g_thread; - if (tid == 0xfffffffe) - *pThr = Core_GetCurrentThread(); - else if (tid == (uint32_t)-1) - { - // TODO: Implement g argument for all. - if (arguments[0] == 'c') - { - c_all_threads = true; - return Kdbg_ConnectionSendPacket(con, "OK"); - } - else - { - return Kdbg_ConnectionSendPacket(con, "E.Unsupported"); - } - } - else - { - for(thread_node* node = OBOS_KernelProcess->threads.head; node; ) - { - if (node->data->tid == tid) - { - *pThr = node->data; - break; - } - node = node->next; - } - } - if (*pThr) - return Kdbg_ConnectionSendPacket(con, "OK"); - return Kdbg_ConnectionSendPacket(con, "E.Could not find thread"); -} - -static size_t parse_gdb_thread_id(const char* id, size_t len, uint32_t* pid, uint32_t* tid) -{ - if (!len || !pid || !tid) - return 0; - bool hasPid = *id == 'p'; - // Minimum contents of a thread id: - // 0 - // With a pid: - // p0.0 - if (hasPid && len < 4) - return 0; - // size_t idSize = 0; - if (hasPid) - { - id++; - size_t pidLen = strnchr(id, '.', len-1)-1; - // idSize += pidLen + 1; - if (id[0] == '-' && id[1] == '1') - *pid = 0xffffffff; - else - *pid = KdbgH_hex2bin(id, pidLen); - if (!(*pid)) - *pid = 0xfffffffe; - id += pidLen + 1; - } - if (!(*id)) - return 0; - size_t tidLen = strnchr(id, ';', len); - // idSize += tidLen; - if (id[0] == '-' && id[1] == '1') - *tid = 0xffffffff; - else - *tid = KdbgH_hex2bin(id, tidLen); - if (!(*tid)) - *tid = 0xfffffffe; - return tidLen; -} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/gdbstub/stop_reply.h b/src/oboskrnl/arch/x86_64/gdbstub/stop_reply.h deleted file mode 100644 index e0c62737..00000000 --- a/src/oboskrnl/arch/x86_64/gdbstub/stop_reply.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * oboskrnl/arch/x86_64/gdbstub/stop_reply.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include - -// '?' packet -// Queries the stop reason. -obos_status Kdbg_GDB_query_halt(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata); -// Read Registers -obos_status Kdbg_GDB_g(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata); -// Write Registers -obos_status Kdbg_GDB_G(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata); -// Kill the kernel (shutdown) -obos_status Kdbg_GDB_k(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata); -// Detach from gdb. -obos_status Kdbg_GDB_D(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata); -// Read Memory -obos_status Kdbg_GDB_m(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata); -// Write Memory -obos_status Kdbg_GDB_M(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata); -// Queries thread status. -obos_status Kdbg_GDB_T(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata); -// Step -obos_status Kdbg_GDB_s(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata); -// Continue [at address] -obos_status Kdbg_GDB_c(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata); -obos_status Kdbg_GDB_C(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* dbg_ctx, void* userdata); -// These packets: g,G,k,m,M -// But multithreaded -obos_status Kdbg_GDB_H(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata); \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/gdbstub/vFile.c b/src/oboskrnl/arch/x86_64/gdbstub/vFile.c deleted file mode 100644 index e5b4adf2..00000000 --- a/src/oboskrnl/arch/x86_64/gdbstub/vFile.c +++ /dev/null @@ -1,545 +0,0 @@ -/* - * oboskrnl/arch/x86_64/gdbstub/vFile.c - * - * Copyright (c) 2025 Omar Berrow -*/ - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include -#include -#include - -#include - -#define GDB_EPERM 1 -#define GDB_ENOENT 2 -#define GDB_EINTR 4 -#define GDB_EBADF 9 -#define GDB_EACCES 13 -#define GDB_EFAULT 14 -#define GDB_EBUSY 16 -#define GDB_EEXIST 17 -#define GDB_ENODEV 19 -#define GDB_ENOTDIR 20 -#define GDB_EISDIR 21 -#define GDB_EINVAL 22 -#define GDB_ENFILE 23 -#define GDB_EMFILE 24 -#define GDB_EFBIG 27 -#define GDB_ENOSPC 28 -#define GDB_ESPIPE 29 -#define GDB_EROFS 30 -#define GDB_ENOSYS 38 /* this actually isn't listed in the remote protocol lol */ -#define GDB_ENAMETOOLONG 91 -#define GDB_EUNKNOWN 9999 - -static int obos_status_to_gdb_errno(obos_status status) -{ - switch (status) { - case OBOS_STATUS_SUCCESS: return 0; // success :) - case OBOS_STATUS_ACCESS_DENIED: return GDB_EACCES; - case OBOS_STATUS_NOT_FOUND: return GDB_ENOENT; - case OBOS_STATUS_RETRY: return GDB_EINTR; - case OBOS_STATUS_TIMED_OUT: return GDB_EINTR; - case OBOS_STATUS_UNINITIALIZED: return GDB_EBADF; - case OBOS_STATUS_PAGE_FAULT: return GDB_EFAULT; - case OBOS_STATUS_WOULD_BLOCK: return GDB_EBUSY; - case OBOS_STATUS_ALREADY_MOUNTED: return GDB_EBUSY; - case OBOS_STATUS_ALREADY_INITIALIZED: return GDB_EEXIST; - case OBOS_STATUS_NOT_A_FILE: return GDB_EISDIR; - case OBOS_STATUS_INVALID_ARGUMENT: return GDB_EINVAL; - case OBOS_STATUS_NO_SPACE: return GDB_ENOSPC; - case OBOS_STATUS_READ_ONLY: return GDB_EROFS; - case OBOS_STATUS_UNIMPLEMENTED: return GDB_ENOSYS; - default: OBOS_Log("Kdbg: vFile: Function returned status %d which cannot be translated to an errno.\n", status); return GDB_EUNKNOWN; - } -} - -handle_table Kdbg_GDBHandleTable; - -#define O_RDONLY 0x0 -#define O_WRONLY 0x1 -#define O_RDWR 0x2 -#define O_APPEND 0x8 -#define O_CREAT 0x200 -#define O_TRUNC 0x400 -#define O_EXCL 0x800 - -#define S_IFLNK 0120000 -#define S_IFREG 0100000 -#define S_IFBLK 0060000 -#define S_IFDIR 0040000 -#define S_IFCHR 0020000 -#define S_IFIFO 0010000 -#define S_IRUSR 0400 -#define S_IWUSR 0200 -#define S_IXUSR 0100 -#define S_IRGRP 040 -#define S_IWGRP 020 -#define S_IXGRP 010 -#define S_IROTH 04 -#define S_IWOTH 02 -#define S_IXOTH 01 - - -static char* hex2str(const char* hex, size_t len) -{ - char* ret = Kdbg_Malloc(len/2+1); - ret[len/2] = 0; - for (size_t i = 0; i < len; i += 2) - ret[i/2] = (char)KdbgH_hex2bin(hex+i, 2); - return ret; -} - -#define do_bounds_check(ptr, pstart, len) \ -({\ - uintptr_t _start = (uintptr_t)pstart;\ - uintptr_t _p = (uintptr_t)ptr;\ - (_p >= _start) && (_p < (_start+len));\ -}) - -static const char* next_escaped_char(const char* iter, char* end) -{ - while (iter++ < end) - { - switch (*iter) { - case '#': - case '$': - case '}': - return iter; - default: continue; - } - } - return end; -} -static char* format_binary_response(const char* buf, char* resp, size_t initial_resp_offset, size_t* const resp_len, size_t sz_buf) -{ - const char* iter = buf; - char* resp_iter = resp+initial_resp_offset; - while (iter < (char*)buf+sz_buf) - { - size_t nCopied = 0; - if (*iter == '#' || *iter == '$' || *iter == '}') - { - (*resp_len)++; - char* prev_resp = resp; - resp = Kdbg_Realloc(resp, (*resp_len)); - resp_iter = resp + (resp_iter - prev_resp); - *resp_iter++ = '}'; - *resp_iter++ = *iter ^ 0x20; - iter++; - continue; - } - else - { - const char* next_ch = next_escaped_char(iter, (char*)buf+sz_buf); - nCopied = next_ch - iter; - } - memcpy(resp_iter, buf, nCopied); - iter += nCopied; - resp_iter += nCopied; - } - return resp; -} - -struct gdb_stat { - unsigned int st_dev; /* device */ - unsigned int st_ino; /* inode */ - unsigned int st_mode; /* protection */ - unsigned int st_nlink; /* number of hard links */ - unsigned int st_uid; /* user ID of owner */ - unsigned int st_gid; /* group ID of owner */ - unsigned int st_rdev; /* device type (if inode device) */ - unsigned long st_size; /* total size, in bytes */ - unsigned long st_blksize; /* blocksize for filesystem I/O */ - unsigned long st_blocks; /* number of blocks allocated */ - long st_atime; /* time of last access */ - long st_mtime; /* time of last modification */ - long st_ctime; /* time of last change */ -}; - -static obos_status common_stat(vnode* vn, struct gdb_stat* out) -{ - OBOS_ENSURE(vn && out); - switch (vn->vtype) { - case VNODE_TYPE_DIR: - out->st_mode |= S_IFDIR; - break; - case VNODE_TYPE_FIFO: - out->st_mode |= S_IFIFO; - break; - case VNODE_TYPE_CHR: - out->st_mode |= S_IFCHR; - break; - case VNODE_TYPE_BLK: - out->st_mode |= S_IFBLK; - break; - case VNODE_TYPE_REG: - out->st_mode |= S_IFREG; - break; - case VNODE_TYPE_LNK: - out->st_mode |= S_IFLNK; - break; - default: - return OBOS_STATUS_UNIMPLEMENTED; - } - out->st_mode |= vn->perm.mode; - out->st_dev = 0; - out->st_ino = vn->inode; - out->st_nlink = vn->refs; - out->st_uid = vn->uid; - out->st_gid = vn->gid; - out->st_rdev = 0; - out->st_size = vn->filesize; - out->st_blksize = vn->blkSize; - out->st_blocks = vn->filesize/vn->blkSize; - out->st_atime = 0; - out->st_mtime = 0; - out->st_ctime = 0; - return OBOS_STATUS_SUCCESS; -} - -obos_status Kdbg_GDB_vFile(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata) -{ - OBOS_UNUSED(userdata); - OBOS_UNUSED(ctx); - - if (!Kdbg_GDBHandleTable.arr) - OBOS_InitializeHandleTable(&Kdbg_GDBHandleTable); - - const char* op = arguments; - size_t op_len = strnchr(op, ':', argumentsLen)-1; - if (op_len == argumentsLen) - { - Kdbg_ConnectionSendPacket(con, ""); - return OBOS_STATUS_SUCCESS; - } - const char* op_args = op+op_len+1; - size_t op_args_len = argumentsLen - (op_len+1); - // NOTE(oberrow): Intentionally don't support pwrite and unlink - - if (uacpi_strncmp(op, "open", op_len) == 0) - { - char* filename = nullptr; - size_t filename_len = 0; - int flags = 0; - // NOTE: There is a mode, but we're going to ignore that. - - if (!do_bounds_check(op_args, op_args, op_args_len)) - { - Kdbg_ConnectionSendPacket(con, "F-1,16"); // EINVAL - return OBOS_STATUS_SUCCESS; - } - - filename_len = strnchr(op_args, ',', op_args_len) / 2; - filename = hex2str(op_args, filename_len*2); - - const char* flags_ptr = op_args + (filename_len*2)+1; - OBOS_MAYBE_UNUSED size_t flags_ptr_len = strnchr(op_args+((filename_len*2)+1), ',', op_args_len-((filename_len*2)+1)); - - if (!do_bounds_check(flags_ptr, op_args, op_args_len)) - { - Kdbg_Free(filename); - Kdbg_ConnectionSendPacket(con, "F-1,16"); // EINVAL - return OBOS_STATUS_SUCCESS; - } - - flags = OBOSH_StrToULL(flags_ptr, nullptr, 16); - - uint32_t decoded_flags = 0; - if ((flags & 3) == O_RDONLY) - decoded_flags |= FD_OFLAGS_READ; - else if ((flags & 3) == O_WRONLY) - decoded_flags |= FD_OFLAGS_WRITE; - else if ((flags & 3) == O_RDWR) - decoded_flags |= FD_OFLAGS_READ|FD_OFLAGS_WRITE; - - handle_desc *desc = {}; - OBOS_LockHandleTable(&Kdbg_GDBHandleTable); - handle fd = OBOS_HandleAllocate(&Kdbg_GDBHandleTable, HANDLE_TYPE_FD, &desc); - desc->un.fd = Kdbg_Calloc(1, sizeof(fd)); - int errno = obos_status_to_gdb_errno(Vfs_FdOpen(desc->un.fd, filename, decoded_flags)); - if (flags & O_APPEND && !errno) - Vfs_FdSeek(desc->un.fd, 0, SEEK_END); - OBOS_UnlockHandleTable(&Kdbg_GDBHandleTable); - - char* resp = nullptr; - - if (errno != 0) - resp = KdbgH_FormatResponse("F-1,%x", errno); - else - resp = KdbgH_FormatResponse("F%x", fd); - - Kdbg_ConnectionSendPacket(con, resp); - - Kdbg_Free(filename); - Kdbg_Free(resp); - } - else if (uacpi_strncmp(op, "close", op_len) == 0) - { - if (!op_args_len) - return Kdbg_ConnectionSendPacket(con, ""); - handle fd = OBOSH_StrToULL(op_args, nullptr, 0); - - int errno = obos_status_to_gdb_errno(Sys_HandleClose(fd)); - - char* resp = nullptr; - if (errno != 0) - resp = KdbgH_FormatResponse("F-1,%x", errno); - else - resp = KdbgH_FormatResponse("F0"); - - Kdbg_ConnectionSendPacket(con, resp); - Kdbg_Free(resp); - } - else if (uacpi_strncmp(op, "pread", op_len) == 0) - { - handle desc = 0; - size_t count = 0, offset = 0; - - const char* args_iter = op_args; - do { - const char* ptr = args_iter; - if (!do_bounds_check(ptr, op_args, op_args_len)) - return Kdbg_ConnectionSendPacket(con, "F-1,16"); // EINVAL - desc = OBOSH_StrToULL(ptr, &args_iter, 16); - args_iter++; - } while(0); - do { - const char* ptr = args_iter; - if (!do_bounds_check(ptr, op_args, op_args_len)) - return Kdbg_ConnectionSendPacket(con, "F-1,16"); // EINVAL - count = OBOSH_StrToULL(ptr, &args_iter, 16); - args_iter++; - } while(0); - do { - const char* ptr = args_iter; - if (!do_bounds_check(ptr, op_args, op_args_len)) - return Kdbg_ConnectionSendPacket(con, "F-1,16"); // EINVAL - offset = OBOSH_StrToULL(ptr, &args_iter, 16); - args_iter++; - } while(0); - - OBOS_LockHandleTable(&Kdbg_GDBHandleTable); - handle_desc *fd = nullptr; - obos_status status = OBOS_STATUS_SUCCESS; - fd = OBOS_HandleLookup(&Kdbg_GDBHandleTable, desc, HANDLE_TYPE_FD, false, &status); - int errno = 0; - if (obos_is_error(status)) - { - errno = obos_status_to_gdb_errno(status); - char* resp = KdbgH_FormatResponse("F%d,%x", errno == 0 ? 0 : -1, errno); - Kdbg_ConnectionSendPacket(con, resp); - Kdbg_Free(resp); - goto down; - } - OBOS_UnlockHandleTable(&Kdbg_GDBHandleTable); - - void* buf = Kdbg_Malloc(count); - size_t nRead = 0; - errno = obos_status_to_gdb_errno(Vfs_FdPRead(fd->un.fd, buf, offset, count, &nRead)); - - char* resp = nullptr; - if (errno == 0) - { - resp = KdbgH_FormatResponse("F%x;", nRead); - size_t resp_len = strlen(resp); - resp = Kdbg_Realloc(resp, resp_len+nRead); - resp_len += nRead; - resp = format_binary_response(buf, resp, resp_len-nRead, &resp_len, nRead); - } - else - resp = KdbgH_FormatResponse("F-1,%d;", errno); - Kdbg_ConnectionSendPacket(con, resp); - Kdbg_Free(resp); - } - else if (uacpi_strncmp(op, "fstat", op_len) == 0) - { - handle desc = 0; - const char* args_iter = op_args; - size_t rel_args_size = op_args_len; - do { - const char* ptr = args_iter; - size_t len_ptr = rel_args_size; - if (!do_bounds_check(ptr, op_args, op_args_len)) - return Kdbg_ConnectionSendPacket(con, "F-1,16"); // EINVAL - desc = OBOSH_StrToULL(ptr, nullptr, 0); - rel_args_size -= len_ptr - 1; - args_iter += len_ptr + 1; - } while(0); - - OBOS_LockHandleTable(&Kdbg_GDBHandleTable); - handle_desc *fd = nullptr; - obos_status status = OBOS_STATUS_SUCCESS; - fd = OBOS_HandleLookup(&Kdbg_GDBHandleTable, desc, HANDLE_TYPE_FD, false, &status); - int errno = 0; - if (obos_is_error(status)) - { - errno = obos_status_to_gdb_errno(status); - char* resp = KdbgH_FormatResponse("F-1,%x", errno); - Kdbg_ConnectionSendPacket(con, resp); - Kdbg_Free(resp); - goto down; - } - OBOS_UnlockHandleTable(&Kdbg_GDBHandleTable); - - struct gdb_stat st = {}; - errno = obos_status_to_gdb_errno(common_stat(fd->un.fd->vn, &st)); - - char* resp = nullptr; - if (errno == 0) - { - resp = KdbgH_FormatResponse("F%x;", sizeof(st)); - size_t resp_len = strlen(resp); - resp = Kdbg_Realloc(resp, resp_len+sizeof(st)); - resp_len += sizeof(st); - resp = format_binary_response((void*)&st, resp, resp_len-sizeof(st), &resp_len, sizeof(st)); - } - else - resp = KdbgH_FormatResponse("F-1,%x;", errno); - Kdbg_ConnectionSendPacket(con, resp); - Kdbg_Free(resp); - } - else if (uacpi_strncmp(op, "stat", op_len) == 0) - { - vnode* vn = nullptr; - char* filename = nullptr; - size_t filename_len = 0; - - if (!do_bounds_check(op_args, op_args, op_args_len)) - { - Kdbg_ConnectionSendPacket(con, "F-1,16"); // EINVAL - return OBOS_STATUS_SUCCESS; - } - - filename_len = op_args_len / 2; - filename = hex2str(op_args, filename_len*2); - - dirent* ent = VfsH_DirentLookup(filename); - if (!ent) - { - Kdbg_Free(filename); - Kdbg_ConnectionSendPacket(con, "F-1,2"); // ENOENT - return OBOS_STATUS_SUCCESS; - } - ent = VfsH_FollowLink(ent); - vn = ent->vnode; - - struct gdb_stat st = {}; - int errno = obos_status_to_gdb_errno(common_stat(vn, &st)); - - char* resp = nullptr; - if (errno == 0) - { - resp = KdbgH_FormatResponse("F%d;", sizeof(st)); - size_t resp_len = strlen(resp); - resp = Kdbg_Realloc(resp, resp_len+sizeof(st)); - resp_len += sizeof(st); - resp = format_binary_response((void*)&st, resp, resp_len-sizeof(st), &resp_len, sizeof(st)); - } - else - resp = KdbgH_FormatResponse("F-1,%d;", errno); - Kdbg_ConnectionSendPacket(con, resp); - Kdbg_Free(resp); - Kdbg_Free(filename); - } - else if (uacpi_strncmp(op, "lstat", op_len) == 0) - { - vnode* vn = nullptr; - char* filename = nullptr; - size_t filename_len = 0; - - if (!do_bounds_check(op_args, op_args, op_args_len)) - { - Kdbg_ConnectionSendPacket(con, "F-1,16"); // EINVAL - return OBOS_STATUS_SUCCESS; - } - - filename_len = op_args_len / 2; - filename = hex2str(op_args, filename_len*2); - - dirent* ent = VfsH_DirentLookup(filename); - if (!ent) - { - Kdbg_Free(filename); - Kdbg_ConnectionSendPacket(con, "F-1,2"); // ENOENT - return OBOS_STATUS_SUCCESS; - } - vn = ent->vnode; - - struct gdb_stat st = {}; - int errno = obos_status_to_gdb_errno(common_stat(vn, &st)); - - char* resp = nullptr; - if (errno == 0) - { - resp = KdbgH_FormatResponse("F%x;", sizeof(st)); - size_t resp_len = strlen(resp); - resp = Kdbg_Realloc(resp, resp_len+sizeof(st)); - resp_len += sizeof(st); - resp = format_binary_response((void*)&st, resp, resp_len-sizeof(st), &resp_len, sizeof(st)); - } - else - resp = KdbgH_FormatResponse("F-1,%x;", errno); - Kdbg_ConnectionSendPacket(con, resp); - Kdbg_Free(resp); - Kdbg_Free(filename); - } - else if (uacpi_strncmp(op, "readlink", op_len) == 0) - { - char* filename = nullptr; - size_t filename_len = 0; - - if (!do_bounds_check(op_args, op_args, op_args_len)) - { - Kdbg_ConnectionSendPacket(con, "F-1,16"); // EINVAL - return OBOS_STATUS_SUCCESS; - } - - filename_len = op_args_len / 2; - filename = hex2str(op_args, filename_len*2); - - dirent* ent = VfsH_DirentLookup(filename); - if (!ent) - { - Kdbg_Free(filename); - Kdbg_ConnectionSendPacket(con, "F-1,2"); // ENOENT - return OBOS_STATUS_SUCCESS; - } - - int errno = 0; - const char* lnk_dat = ent->vnode->un.linked; - size_t len_lnk_dat = strlen(lnk_dat); - - char* resp = nullptr; - if (errno == 0) - { - resp = KdbgH_FormatResponse("F%x;", len_lnk_dat); - size_t resp_len = strlen(resp); - resp = Kdbg_Realloc(resp, resp_len+len_lnk_dat); - resp_len += len_lnk_dat; - resp = format_binary_response(lnk_dat, resp, resp_len-len_lnk_dat, &resp_len, len_lnk_dat); - } - else - resp = KdbgH_FormatResponse("F-1,%x;", errno); - Kdbg_ConnectionSendPacket(con, resp); - Kdbg_Free(resp); - Kdbg_Free(filename); - } - else if (uacpi_strncmp(op, "setfs", op_len) == 0) - return Kdbg_ConnectionSendPacket(con, "F0"); - else - Kdbg_ConnectionSendPacket(con, ""); - down: - return OBOS_STATUS_SUCCESS; -} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/gdbstub/vFile.h b/src/oboskrnl/arch/x86_64/gdbstub/vFile.h deleted file mode 100644 index 0af2f855..00000000 --- a/src/oboskrnl/arch/x86_64/gdbstub/vFile.h +++ /dev/null @@ -1,17 +0,0 @@ -/* - * oboskrnl/arch/x86_64/gdbstub/vFile.h - * - * Copyright (c) 2025 Omar Berrow -*/ - -#pragma once - -#include -#include -#include - -#include - -extern handle_table Kdbg_GDBHandleTable; - -obos_status Kdbg_GDB_vFile(gdb_connection* con, const char* arguments, size_t argumentsLen, gdb_ctx* ctx, void* userdata); \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/hpet_table.h b/src/oboskrnl/arch/x86_64/hpet_table.h index 01f0a92a..8997a8db 100644 --- a/src/oboskrnl/arch/x86_64/hpet_table.h +++ b/src/oboskrnl/arch/x86_64/hpet_table.h @@ -1,64 +1,69 @@ -/* - * oboskrnl/arch/x86_64/hpet_table.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include - -typedef struct OBOS_PACK HPET_Addr -{ - uint8_t addressSpaceId; - uint8_t registerBitWidth; - uint8_t registerBitOffset; - uint8_t resv; - uintptr_t address; -} HPET_Addr; -typedef struct OBOS_PACK HPET_Table -{ - ACPISDTHeader sdtHeader; - uint32_t eventTimerBlockID; - HPET_Addr baseAddress; - uint8_t hpetNumber; - uint16_t mainCounterMinimum; - uint8_t pageProtectionAndOEMAttrib; -} HPET_Table; -typedef struct OBOS_PACK HPET_Timer -{ - volatile uint64_t timerConfigAndCapabilities; - volatile uint64_t timerComparatorValue; - volatile struct - { - uint32_t fsbIntVal; - uint32_t fsbIntAddr; - } timerFSBInterruptRoute; - const volatile uint64_t resv; -} HPET_Timer; -typedef struct OBOS_PACK HPET -{ - volatile const struct { - uint8_t revisionId; - uint8_t numTimCap : 4; - bool countSizeCap : 1; - bool resv1 : 1; - bool legRouteCap : 1; - uint16_t vendorID; - uint32_t counterCLKPeriod; - } OBOS_PACK generalCapabilitiesAndID; - volatile const uint64_t resv1; - volatile uint64_t generalConfig; - volatile const uint64_t resv2; - volatile uint64_t generalInterruptStatus; - volatile const uint64_t resv3[0x19]; - volatile uint64_t mainCounterValue; - volatile const uint64_t resv4; - volatile HPET_Timer timer0, timer1, timer2; - // 0x160-0x400 are for timers 0-31 -} HPET; -extern HPET* Arch_HPETAddress; -extern uint64_t Arch_HPETFrequency; +/* + oboskrnl/arch/x86_64/hpet_table.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include + +#include + +namespace obos +{ + namespace arch + { + struct HPET_Addr + { + uint8_t addressSpaceId; + uint8_t registerBitWidth; + uint8_t registerBitOffset; + uint8_t resv; + uintptr_t address; + } __attribute__((packed)); + struct HPET_Table + { + ACPISDTHeader sdtHeader; + uint32_t eventTimerBlockID; + HPET_Addr baseAddress; + uint8_t hpetNumber; + uint16_t mainCounterMinimum; + uint8_t pageProtectionAndOEMAttrib; + } __attribute__((packed)); + struct HPET_Timer + { + uint64_t timerConfigAndCapabilities; + uint64_t timerComparatorValue; + struct + { + uint32_t fsbIntVal; + uint32_t fsbIntAddr; + } timerFSBInterruptRoute; + const uint64_t resv; + }; + struct HPET + { + const struct { + uint8_t revisionId; + uint8_t numTimCap : 4; + bool countSizeCap : 1; + bool resv1 : 1; + bool legRouteCap : 1; + uint16_t vendorID; + uint32_t counterCLKPeriod; + } __attribute__((packed)) generalCapabilitiesAndID; + const uint64_t resv1; + uint64_t generalConfig; + const uint64_t resv2; + uint64_t generalInterruptStatus; + const uint64_t resv3[0x19]; + uint64_t mainCounterValue; + const uint64_t resv4; + HPET_Timer timer0, timer1, timer2; + // 0x160-0x400 are for timers 0-31 + }; + extern HPET* g_hpetAddress; + extern uint64_t g_hpetFrequency; + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/idt.c b/src/oboskrnl/arch/x86_64/idt.c deleted file mode 100644 index 26a06540..00000000 --- a/src/oboskrnl/arch/x86_64/idt.c +++ /dev/null @@ -1,125 +0,0 @@ -/* - * oboskrnl/arch/x86_64/idt.c - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include -#include -#include -#include - -#include - -#include -#include - -#include -#include -#include - -#include - -#include -#include -#include -#include - -extern char Arch_b_isr_handler; -extern char Arch_e_isr_handler; - -struct idtEntry g_idtEntries[256]; -struct idtPointer -{ - uint16_t size; - uintptr_t idt; -} OBOS_PACK; -enum -{ - // Present, Interrupt Gate - DEFAULT_TYPE_ATTRIBUTE = 0x8E, - // Max DPL: 3 - TYPE_ATTRIBUTE_USER_MODE = 0x60 -}; -uintptr_t Arch_IRQHandlers[256]; -extern void Arch_FlushIDT(struct idtPointer* idtptr); -static OBOS_PAGEABLE_FUNCTION void RegisterISRInIDT(uint8_t vec, uintptr_t addr, bool canUsermodeCall, uint8_t ist) -{ - struct idtEntry idtEntry = g_idtEntries[vec]; - idtEntry.ist = ist & 0x7; - idtEntry.offset1 = addr & 0xffff; - idtEntry.selector = 0x8; - idtEntry.typeAttributes = DEFAULT_TYPE_ATTRIBUTE | (canUsermodeCall ? TYPE_ATTRIBUTE_USER_MODE : 0); - idtEntry.offset2 = (addr >> 16) & 0xffff; - idtEntry.offset3 = (addr >> 32) & 0xffffffff; - g_idtEntries[vec] = idtEntry; -} -static OBOS_PAGEABLE_FUNCTION int getIntIST(int i) -{ - if (i > 32) - return 0; - return (i == 8) ? 1 : 0; -} -OBOS_PAGEABLE_FUNCTION void Arch_InitializeIDT(bool isBSP) -{ - if (isBSP) - { - for (int i = 0; i < 256; i++) - RegisterISRInIDT(i, - (uintptr_t)(&Arch_b_isr_handler + (i * 32)), - i == 3 /* The only interrupt that can be called from user mode is the breakpoint interrupt */, - getIntIST(i)); - } - struct idtPointer idtPtr; - idtPtr.size = sizeof(g_idtEntries) - 1; - idtPtr.idt = (uintptr_t)g_idtEntries; - Arch_FlushIDT(&idtPtr); -} -void Arch_RawRegisterInterrupt(uint8_t vec, uintptr_t f) -{ - Arch_IRQHandlers[vec] = f; -} -void Arch_PutInterruptOnIST(uint8_t vec, uint8_t ist) -{ - if (ist > 8) - return; - g_idtEntries[vec].ist = ist; -} -obos_status CoreS_RegisterIRQHandler(irq_vector_id vector, void(*handler)(interrupt_frame* frame)) -{ - obos_status s = OBOS_STATUS_SUCCESS; - if ((s = CoreS_IsIRQVectorInUse(vector)) && handler) - return s; - if(!(((uintptr_t)(handler) >> 47) == 0 || ((uintptr_t)(handler) >> 47) == 0x1ffff)) - return OBOS_STATUS_INVALID_ARGUMENT; - if ((uintptr_t)handler < OBOS_KERNEL_ADDRESS_SPACE_BASE && handler) - return OBOS_STATUS_INVALID_ARGUMENT; - Arch_IRQHandlers[vector + 32] = (uintptr_t)handler; - return OBOS_STATUS_SUCCESS; -} -obos_status CoreS_IsIRQVectorInUse(irq_vector_id vector) -{ - if (vector > 223) - return OBOS_STATUS_INVALID_ARGUMENT; - return Arch_IRQHandlers[vector+32] ? OBOS_STATUS_IN_USE : OBOS_STATUS_SUCCESS; -} -void CoreS_SendEOI(interrupt_frame* unused) -{ - OBOS_UNUSED(unused); - Arch_LAPICSendEOI(); -} -bool CoreS_EnterIRQHandler(interrupt_frame* frame) -{ - sti(); - if (CoreS_GetCPULocalPtr() && Mm_IsInitialized() && ~frame->cs & 0x3 /* kernel mode */) - CoreS_GetCPULocalPtr()->currentContext = &Mm_KernelContext; - return true; -} -void CoreS_ExitIRQHandler(interrupt_frame* frame) -{ - if (~frame->cs & 0x3 && CoreS_GetCPULocalPtr()->currentThread) - CoreS_GetCPULocalPtr()->currentContext = CoreS_GetCPULocalPtr()->currentThread->proc ? CoreS_GetCPULocalPtr()->currentThread->proc->ctx : &Mm_KernelContext; - else if (Core_GetIrql() <= IRQL_DISPATCH) - OBOS_SyncPendingSignal(frame); - cli(); -} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/idt.h b/src/oboskrnl/arch/x86_64/idt.h deleted file mode 100644 index 25183b27..00000000 --- a/src/oboskrnl/arch/x86_64/idt.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - oboskrnl/arch/x86_64/idt.h - - Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include - -#include - -struct idtEntry -{ - uint16_t offset1; - uint16_t selector; - uint8_t ist; - uint8_t typeAttributes; - uint16_t offset2; - uint32_t offset3; - uint32_t resv1; -}; - -void Arch_InitializeIDT(bool isBSP); -void Arch_RawRegisterInterrupt(uint8_t vec, uintptr_t f); -void Arch_PutInterruptOnIST(uint8_t vec, uint8_t ist); \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/interrupt_frame.h b/src/oboskrnl/arch/x86_64/interrupt_frame.h deleted file mode 100644 index f339ece3..00000000 --- a/src/oboskrnl/arch/x86_64/interrupt_frame.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - oboskrnl/arch/x86_64/interrupt_frame.h - - Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include - -enum -{ - RFLAGS_CARRY = BIT(0), - RFLAGS_PARITY = BIT(2), - RFLAGS_AUXILLARY_CARRY = BIT(4), - RFLAGS_ZERO = BIT(6), - RFLAGS_SIGN = BIT(7), - RFLAGS_TRAP = BIT(8), - RFLAGS_INTERRUPT_ENABLE = BIT(9), - RFLAGS_DIRECTION = BIT(10), - RFLAGS_OVERFLOW = BIT(11), - RFLAGS_IOPL_3 = BIT(12) | BIT(13), - RFLAGS_NESTED_TASK = BIT(14), - RFLAGS_RESUME = BIT(16), - RFLAGS_VIRTUAL8086 = BIT(17), - RFLAGS_ALIGN_CHECK = BIT(18), - RFLAGS_VINTERRUPT_FLAG = BIT(19), - RFLAGS_VINTERRUPT_PENDING = BIT(20), - RFLAGS_CPUID = BIT(21), -}; -typedef struct __interrupt_frame -{ - // 0x0 - uintptr_t cr3; - // 0x8 - uintptr_t ds; - // 0x10, 0x18, 0x20, 0x28, 0x30, 0x38, 0x40, 0x48, 0x50, 0x58 - // 0x60, 0x68, 0x70, 0x78, 0x80, 0x88, 0x90 - uintptr_t rbp, ignored1, r15, r14, r13, r12, r11, r10, r9, r8, - rdi, rsi, ignored2, rbx, rdx, rcx, rax; - // 0x98, 0xA0, 0xA8 - uintptr_t vector, intNumber, errorCode; - // 0xB0, 0xB8 - uintptr_t rip, cs; - // 0xC0 - uintptr_t rflags; - // 0xC8, 0xD0 - uintptr_t rsp, ss; -} interrupt_frame; - -typedef struct syscall_frame -{ - // ds=0x00, ss=0x1b, cs=0x23 - uintptr_t unused; - uintptr_t orig_rax; - uintptr_t rbp, rip, r15, r14, r13, r12; - union { - uintptr_t rflags; - uintptr_t r11; - }; - union { - // the syscall trap handler saves user rsp in r10 - uintptr_t rsp; - uintptr_t r10; - }; - uintptr_t r9, r8, rdi, rsi, rbx, rdx, rcx, cr3; -} syscall_frame; \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/ioapic.c b/src/oboskrnl/arch/x86_64/ioapic.c deleted file mode 100644 index 4c877898..00000000 --- a/src/oboskrnl/arch/x86_64/ioapic.c +++ /dev/null @@ -1,257 +0,0 @@ -/* - * oboskrnl/arch/x86_64/ioapic.c - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include -#include -#include -#include - -#include - -#include -#include - -#include -#include -#include - -ioapic_irq_redirection_entry* Arch_IRQRedirectionEntries; -size_t Arch_SizeofIRQRedirectionEntries; -ioapic_descriptor* Arch_IOAPICs; -size_t Arch_IOAPICCount; - -#define OffsetPtr(ptr, off, t) ((t*)(((uintptr_t)(ptr)) + (off))) -#define NextMADTEntry(cur) OffsetPtr(cur, cur->length, MADT_EntryHeader) -#define MADTEntryStruct(entry_type) MADT_EntryType ##entry_type -// #define OffsetOfReg(reg) ((uintptr_t)&(((ioapic_registers*)nullptr)->reg)) -#define OffsetOfReg(reg) offsetof(ioapic_registers, reg) - -static OBOS_PAGEABLE_FUNCTION OBOS_NO_UBSAN obos_status ParseMADT() -{ - // Find the MADT in the ACPI tables. - ACPIRSDPHeader* rsdp = (ACPIRSDPHeader*)Arch_MapToHHDM(Arch_RSDPBase); - bool tables32 = rsdp->Revision < 2; - ACPISDTHeader* xsdt = tables32 ? (ACPISDTHeader*)(uintptr_t)rsdp->RsdtAddress : (ACPISDTHeader*)rsdp->XsdtAddress; - xsdt = (ACPISDTHeader*)Arch_MapToHHDM((uintptr_t)xsdt); - size_t nEntries = (xsdt->Length - sizeof(*xsdt)) / (tables32 ? 4 : 8); - MADTTable* madt = nullptr; - for (size_t i = 0; i < nEntries; i++) - { - uintptr_t phys = tables32 ? OffsetPtr(xsdt, sizeof(*xsdt), uint32_t)[i] : OffsetPtr(xsdt, sizeof(*xsdt), uint64_t)[i]; - ACPISDTHeader* header = (ACPISDTHeader*)Arch_MapToHHDM(phys); - if (memcmp(header->Signature, "APIC", 4)) - { - madt = (MADTTable*)header; - break; - } - } - void* end = OffsetPtr(madt, madt->sdtHeader.Length, void); - for (MADT_EntryHeader* cur = OffsetPtr(madt, sizeof(*madt), MADT_EntryHeader); (uintptr_t)cur < (uintptr_t)end; cur = NextMADTEntry(cur)) - { - if (cur->type == 1) - Arch_IOAPICCount++; - if (cur->type == 2) - { - if (((MADTEntryStruct(2)*)cur)->busSource != 0 /* ISA */) - continue; - if ((((MADTEntryStruct(2)*)cur)->flags & 0b11) == 0b10) - continue; - if (((((MADTEntryStruct(2)*)cur)->flags >> 2) & 0b11) == 0b10) - continue; - Arch_SizeofIRQRedirectionEntries++; - } - } - if (Arch_IOAPICCount > 16) - return OBOS_STATUS_INTERNAL_ERROR; - obos_status status = OBOS_STATUS_SUCCESS; - Arch_IRQRedirectionEntries = ZeroAllocate(OBOS_KernelAllocator, Arch_SizeofIRQRedirectionEntries, sizeof(ioapic_irq_redirection_entry), &status); - if (obos_is_error(status)) - return status; - Arch_IOAPICs = ZeroAllocate(OBOS_KernelAllocator, Arch_IOAPICCount, sizeof(ioapic_descriptor), &status); - if (obos_is_error(status)) - return status; - size_t ioapic_index = 0; - size_t ioapic_redir_index = 0; - for (MADT_EntryHeader* cur = OffsetPtr(madt, sizeof(*madt), MADT_EntryHeader); (uintptr_t)cur < (uintptr_t)end; cur = NextMADTEntry(cur)) - { - if (cur->type == 1) - { - // IOAPIC - MADTEntryStruct(1)* ent = (MADTEntryStruct(1)*)cur; - Arch_IOAPICs[ioapic_index].id = ent->ioApicID; - Arch_IOAPICs[ioapic_index].phys = ent->ioapicAddress; - Arch_IOAPICs[ioapic_index].gsi = ent->globalSystemInterruptBase; - Arch_IOAPICs[ioapic_index].address = Arch_MapToHHDM(ent->ioapicAddress); - Arch_IOAPICs[ioapic_index].maxRedirectionEntries = - ArchH_IOAPICReadRegister(Arch_IOAPICs[ioapic_index].address, OffsetOfReg(ioapicVersion.maximumRedirectionEntries)) & 0xff; - ioapic_index++; - } - if (cur->type == 2) - { - // IOAPIC Redirection entry - MADTEntryStruct(2)* ent = (MADTEntryStruct(2)*)cur; - if (ent->busSource != 0 /* ISA */) - continue; - uint8_t polarity = ent->flags & 0b11; - OBOS_ASSERT(polarity != 0b10); - bool res = false; - if (polarity == 0b1 || polarity == 0) - res = PolarityActiveHigh; - else if (polarity == 0b11) - res = PolarityActiveLow; - ioapic_trigger_mode tm = TriggerModeLevelSensitive; - uint8_t tmFlg = (ent->flags >> 2) & 0b11; - OBOS_ASSERT(tmFlg != 0b10); - if (tmFlg <= 0b1) - tm = TriggerModeEdgeSensitive; - else if (tmFlg == 0b11) - tm = TriggerModeLevelSensitive; - OBOS_ENSURE(ioapic_redir_index < Arch_SizeofIRQRedirectionEntries); - Arch_IRQRedirectionEntries[ioapic_redir_index].globalSystemInterrupt = ent->globalSystemInterrupt; - Arch_IRQRedirectionEntries[ioapic_redir_index].source = ent->irqSource; - Arch_IRQRedirectionEntries[ioapic_redir_index].polarity = res; - Arch_IRQRedirectionEntries[ioapic_redir_index].tm = tm; - ioapic_redir_index++; - } - } - return OBOS_STATUS_SUCCESS; -} - -// FIXME: Optimizations break things for some reason. -#pragma GCC optimize("-O0") -OBOS_PAGEABLE_FUNCTION obos_status Arch_InitializeIOAPICs() -{ - obos_status status = ParseMADT(); - if (obos_is_error(status)) - return status; - OBOS_ASSERT(Arch_IOAPICCount <= 16); - // Initialize each I/O APIC. - for (size_t i = 0; i < Arch_IOAPICCount; i++) - { - ioapic_descriptor* ioapic = &Arch_IOAPICs[i]; - ArchH_IOAPICWriteRegister(ioapic->address, 0, i<<24); - for (uint32_t gsi = ioapic->gsi; gsi <= ioapic->gsi+ioapic->maxRedirectionEntries; gsi++) - OBOS_ASSERT(obos_is_success(Arch_IOAPICMapIRQToVector(gsi, 0, PolarityActiveHigh, TriggerModeEdgeSensitive))); - } - return status; -} -static OBOS_PAGEABLE_FUNCTION size_t redirection_entry_index(uint32_t gsi) -{ - size_t i = 0; - for (; i < Arch_SizeofIRQRedirectionEntries; i++) - if (Arch_IRQRedirectionEntries[i].source == gsi) - return i; - return SIZE_MAX; -} -static OBOS_PAGEABLE_FUNCTION ioapic_descriptor* find_ioapic(uint32_t gsi) -{ - for (size_t i = 0; i < Arch_IOAPICCount; i++) - if (gsi >= Arch_IOAPICs[i].gsi && gsi <= (Arch_IOAPICs[i].gsi + Arch_IOAPICs[i].maxRedirectionEntries)) - return &Arch_IOAPICs[i]; - return nullptr; -} -OBOS_PAGEABLE_FUNCTION obos_status Arch_IOAPICMaskIRQ(uint32_t gsi, bool mask) -{ - size_t redir_entry = redirection_entry_index(gsi); - if (redir_entry != SIZE_MAX) - { - ioapic_irq_redirection_entry* ent = Arch_IRQRedirectionEntries + redir_entry; - OBOS_ASSERT(ent->source == gsi); - gsi = ent->globalSystemInterrupt; - } - ioapic_descriptor* ioapic = find_ioapic(gsi); - if (!ioapic) - return OBOS_STATUS_NOT_FOUND; - // Gets the redirection entry. - uint32_t ent[2]; - ent[0] = ((uint64_t)ArchH_IOAPICReadRegister(ioapic->address, OffsetOfReg(redirectionEntries[gsi-ioapic->gsi]))); - ent[1] = ((uint64_t)ArchH_IOAPICReadRegister(ioapic->address, OffsetOfReg(redirectionEntries[gsi-ioapic->gsi]) + 4)); - ioapic_redirection_entry *entry = (ioapic_redirection_entry*)&ent; - if (!entry->vector) - return OBOS_STATUS_UNINITIALIZED; - entry->mask = mask; - ArchH_IOAPICWriteRegister(ioapic->address, OffsetOfReg(redirectionEntries[gsi-ioapic->gsi]) + 4, ent[1]); - ArchH_IOAPICWriteRegister(ioapic->address, OffsetOfReg(redirectionEntries[gsi-ioapic->gsi]), ent[0]); - return OBOS_STATUS_SUCCESS; -} -OBOS_PAGEABLE_FUNCTION obos_status Arch_IOAPICMapIRQToVector(uint32_t gsi, uint8_t vector, ioapic_polarity polarity, ioapic_trigger_mode tm) -{ - if (tm < 0 || tm > 1) - return OBOS_STATUS_INVALID_ARGUMENT; - if (vector < 0x20 && vector != 0) - return OBOS_STATUS_INVALID_ARGUMENT; - size_t redir_entry = redirection_entry_index(gsi); - if (redir_entry != SIZE_MAX) - { - ioapic_irq_redirection_entry* ent = Arch_IRQRedirectionEntries + redir_entry; - OBOS_ASSERT(ent->source == gsi); - polarity = ent->polarity; - tm = ent->tm; - gsi = ent->globalSystemInterrupt; - } - ioapic_descriptor* ioapic = find_ioapic(gsi); - if (!ioapic) - return OBOS_STATUS_NOT_FOUND; - // Gets the redirection entry. - uint32_t ent[2]; - ent[0] = ((uint64_t)ArchH_IOAPICReadRegister(ioapic->address, OffsetOfReg(redirectionEntries[gsi-ioapic->gsi]))); - ent[1] = ((uint64_t)ArchH_IOAPICReadRegister(ioapic->address, OffsetOfReg(redirectionEntries[gsi-ioapic->gsi]) + 4)); - ioapic_redirection_entry *entry = (ioapic_redirection_entry*)&ent; - // Set vector info. - if (vector) - { - entry->vector = vector; - entry->delMod = 0b000 /* FIXED */; - // Set polarity info. - entry->intPol = polarity; - entry->triggerMode = tm; - // Set LAPIC info. - entry->destMode = 0 /* Physical */; - entry->destination.physical.lapicId = 0 /* BSP */; - } - else - { - memzero(entry, sizeof(*entry)); - } - // Mask the IRQ. - entry->mask = true; - ArchH_IOAPICWriteRegister(ioapic->address, OffsetOfReg(redirectionEntries[gsi-ioapic->gsi]) + 4, ent[1]); - ArchH_IOAPICWriteRegister(ioapic->address, OffsetOfReg(redirectionEntries[gsi-ioapic->gsi]), ent[0]); - return OBOS_STATUS_SUCCESS; -} -OBOS_PAGEABLE_FUNCTION obos_status Arch_IOAPICGSIUsed(uint32_t gsi) -{ - size_t redir_entry = redirection_entry_index(gsi); - if (redir_entry != SIZE_MAX) - { - ioapic_irq_redirection_entry* ent = Arch_IRQRedirectionEntries + redir_entry; - OBOS_ASSERT(ent->source == gsi); - gsi = ent->globalSystemInterrupt; - } - ioapic_descriptor* ioapic = find_ioapic(gsi); - if (!ioapic) - return OBOS_STATUS_NOT_FOUND; - volatile uint32_t ent[2]; - ent[0] = ((uint64_t)ArchH_IOAPICReadRegister(ioapic->address, OffsetOfReg(redirectionEntries[gsi-ioapic->gsi]))); - ent[1] = ((uint64_t)ArchH_IOAPICReadRegister(ioapic->address, OffsetOfReg(redirectionEntries[gsi-ioapic->gsi]) + 4)); - volatile ioapic_redirection_entry *entry = (ioapic_redirection_entry*)&ent; - return entry->vector == 0 ? OBOS_STATUS_SUCCESS : OBOS_STATUS_IN_USE; -} -OBOS_PAGEABLE_FUNCTION void ArchH_IOAPICWriteRegister(ioapic* ioapic_, uint32_t offset, uint32_t value) -{ - volatile ioapic* const ioapic = ioapic_; - OBOS_ASSERT(!(offset % 4)); - if (offset % 4) - return; - ioapic->ioregsel = offset/4; - ioapic->iowin = value; -} -OBOS_PAGEABLE_FUNCTION uint32_t ArchH_IOAPICReadRegister(ioapic* ioapic_, uint32_t offset) -{ - volatile ioapic* const ioapic = ioapic_; - ioapic->ioregsel = offset/4; - return ioapic->iowin >> ((offset % 4)*8); -} diff --git a/src/oboskrnl/arch/x86_64/ioapic.h b/src/oboskrnl/arch/x86_64/ioapic.h deleted file mode 100644 index ba9f56d8..00000000 --- a/src/oboskrnl/arch/x86_64/ioapic.h +++ /dev/null @@ -1,135 +0,0 @@ -/* - * oboskrnl/arch/x86_64/ioapic.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include -#include - -typedef struct ioapic -{ - OBOS_ALIGNAS(0x10) volatile uint8_t ioregsel; - OBOS_ALIGNAS(0x10) volatile uint32_t iowin; -} ioapic; -typedef struct OBOS_PACK ioapic_redirection_entry -{ - uint8_t vector; // the vector - uint8_t delMod : 3; // delivery mode (1:1 as the lapic's delivery mode) - bool destMode : 1; // the destination mode (logical: 1, physical: 0) - const bool delivStatus : 1; // 1: send pending, 0: idle line - bool intPol : 1; // polarity (0: active-high, 1: active-low) - const bool remoteIRR : 1; // (level-triggered only): set to one when a lapic accepts the irq from the ioapic, and set to zero on EOI in the lapic. - bool triggerMode : 1; // the irq trigger mode - bool mask : 1; // whether the irq is masked - const uint64_t padding : 39; // resv. - union - { - struct - { - uint8_t setOfProcessors; // unused in obos - } OBOS_PACK logical; - struct - { - const uint8_t resv1 : 4; // resv. - uint8_t lapicId : 4; // the lapic id. - } OBOS_PACK physical; - } OBOS_PACK destination; // the destination -} ioapic_redirection_entry; -OBOS_STATIC_ASSERT(sizeof(ioapic_redirection_entry) == 8, "sizeof(ioapic_redirection_entry) must be 8!"); -typedef struct OBOS_PACK ioapic_registers -{ - struct { - const uint32_t resv1 : 24; - uint8_t ioapicID : 4; - const uint8_t resv2 : 4; - } OBOS_PACK ioapicId; - struct - { - const uint8_t version; - const uint8_t resv1; - const uint8_t maximumRedirectionEntries; - const uint8_t resv2; - } OBOS_PACK ioapicVersion; - struct - { - const uint32_t resv1 : 24; - const uint8_t ioapicID : 4; - const uint8_t resv2 : 4; - } OBOS_PACK ioapicArbitrationID; - uint32_t resv1[13]; - ioapic_redirection_entry redirectionEntries[]; -} ioapic_registers; -typedef enum ioapic_trigger_mode -{ - TriggerModeEdgeSensitive = 0, - TriggerModeLevelSensitive = 1, -} ioapic_trigger_mode; -typedef enum ioapic_polarity -{ - PolarityActiveHigh, - PolarityActiveLow, -} ioapic_polarity; -typedef struct ioapic_irq_redirection_entry -{ - uint8_t source; - uint32_t globalSystemInterrupt; - ioapic_polarity polarity; // polarity (0: active-high, 1: active-low) - ioapic_trigger_mode tm; -} ioapic_irq_redirection_entry; -typedef struct ioapic_descriptor -{ - uint8_t id; - uintptr_t phys; - ioapic* address; - uint8_t maxRedirectionEntries; - uint32_t gsi; -} ioapic_descriptor; -extern ioapic_irq_redirection_entry* Arch_IRQRedirectionEntries; -extern size_t Arch_SizeofIRQRedirectionEntries; -extern ioapic_descriptor* Arch_IOAPICs; -extern size_t Arch_IOAPICCount; -/// -/// Initialized all the I/O APICs of the system. -/// -/// The status of the function. -obos_status Arch_InitializeIOAPICs(); -/// -/// Masks an IRQ on the I/O APIC. -/// -/// The gsi to mask. -/// false to unmask, true to mask. -/// The status of the function. -OBOS_EXPORT obos_status Arch_IOAPICMaskIRQ(uint32_t gsi, bool mask); -/// -/// Registers a GSI, redirecting it to a specific vector. -/// -/// The gsi to register. -/// The CPU vector to use. If this is zero, the IRQ is unregistered. -/// The polarity of the IRQ. -/// The trigger mode of the IRQ. -/// The status of the function. -OBOS_EXPORT obos_status Arch_IOAPICMapIRQToVector(uint32_t gsi, uint8_t vector, ioapic_polarity polarity, ioapic_trigger_mode tm); -/// -/// Checks if a GSI is in use or not. -/// -/// The gsi to check. -/// OBOS_STATUS_SUCCESS if it is unused, OBOS_STATUS_IN_USE if it is used, otherwise the status is an error. -OBOS_EXPORT obos_status Arch_IOAPICGSIUsed(uint32_t gsi); -/// -/// Writes an I/O APIC register. -/// -/// The target ioapic. -/// The offset (in bytes) of the register. Must be divisible by 4 -/// The value to write. -void ArchH_IOAPICWriteRegister(ioapic* ioapic, uint32_t offset, uint32_t value); -/// -/// Reads from an I/O APIC register. -/// -/// The target ioapic. -/// The offset (in bytes) of the register. Doesn't have to be divisible by 4 -/// The value at the register offset. -uint32_t ArchH_IOAPICReadRegister(ioapic* ioapic, uint32_t offset); diff --git a/src/oboskrnl/arch/x86_64/irq/apic.cpp b/src/oboskrnl/arch/x86_64/irq/apic.cpp new file mode 100644 index 00000000..da4cbdd8 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/irq/apic.cpp @@ -0,0 +1,190 @@ +/* + oboskrnl/arch/x86_64/irq/apic.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include +#include + +#include + +#include + +#include +#include +#include +#include + +#include + +#define IA32_APIC_BASE 0x1b + +namespace obos +{ + extern volatile limine_hhdm_request hhdm_offset; + volatile limine_rsdp_request rsdp_request = { + .id = LIMINE_RSDP_REQUEST, + .revision = 1, + }; + LAPIC* g_localAPICAddress; + IOAPIC* g_IOAPICAddress; + size_t g_szIoapicRedirectionEntries; + IOAPIC_IRQRedirectionEntry g_ioapicRedirectionEntries[256]; + static bool s_initializedIOAPICRedirectionEntries; + void IOAPIC::WriteRegister(uint8_t offset, uint32_t val) + { + ioregsel = offset; + iowin = val; + } + uint32_t IOAPIC::ReadRegister(uint8_t offset) const + { + ioregsel = offset; + return iowin; + } + + LAPIC* GetLAPICAddress() + { + return (LAPIC*)(hhdm_offset.response->offset + (rdmsr(IA32_APIC_BASE) & ~0xfff)); + } + void DefaultInterruptHandler(interrupt_frame* frame) + { + if (frame->intNumber != 0xff) + LAPIC_SendEOI(); + else + logger::debug("Spurious interrupt received!\n"); + } + void InitializeLAPIC(LAPIC* lapicAddress) + { + if (!g_localAPICAddress) + g_localAPICAddress = lapicAddress; + wrmsr(IA32_APIC_BASE, rdmsr(IA32_APIC_BASE) | (1 << 11)); + lapicAddress->errorStatus = 0; + lapicAddress->lvtLINT0 |= 0xf8; + lapicAddress->lvtLINT1 |= 0xf9; + lapicAddress->lvtError = 0xfa; + lapicAddress->lvtCMCI = 0xfb; + lapicAddress->lvtPerformanceMonitoringCounters = 0xfc; + lapicAddress->lvtThermalSensor = 0xfd; + lapicAddress->lvtTimer = 0xfe; + lapicAddress->spuriousInterruptVector = 0xff; + + for (uint8_t i = 0xf8; i > 0; i++) + RawRegisterInterrupt(i, (uintptr_t)DefaultInterruptHandler); + lapicAddress->spuriousInterruptVector |= (1 << 8); + } + void InitializeIOAPIC(IOAPIC* ioapicAddress) + { + OBOS_ASSERTP(g_IOAPICAddress == nullptr, ""); + logger::debug("%s: Initializing I/O APIC at address 0x%p.\n", __func__, ioapicAddress); + g_IOAPICAddress = ioapicAddress; + // Write zero the IOAPIC id register to initialize it. + ioapicAddress->WriteRegister(0, 0); + if (!s_initializedIOAPICRedirectionEntries) + { + ACPISDTHeader* sdt = nullptr; + size_t nEntries = 0; + bool t32 = false; + GetSDTFromRSDP((ACPIRSDPHeader*)rsdp_request.response->address, &sdt, &t32, &nEntries); + char sign[4] = { 'A', 'P', 'I', 'C' }; + auto madt = (MADTTable*)GetTableWithSignature(sdt, t32, nEntries, &sign); + ParseMADTForIOAPICRedirectionEntries(madt, g_ioapicRedirectionEntries, sizeof(g_ioapicRedirectionEntries) / sizeof(*g_ioapicRedirectionEntries)); + s_initializedIOAPICRedirectionEntries = true; + } + } + void LAPIC_SendEOI() + { + g_localAPICAddress->eoi = 0; + } + namespace arch + { + void SendEOI(interrupt_frame*) + { + g_localAPICAddress->eoi = 0; + } + } + void LAPIC_SendIPI(DestinationShorthand shorthand, DeliveryMode deliveryMode, uint8_t vector, uint8_t _destination) + { + if (!g_localAPICAddress) + return; + while ((g_localAPICAddress->interruptCommand0_31 >> 12 & 0b1)) pause(); + uint32_t icr1 = 0; + uint32_t icr2 = 0; + switch (shorthand) + { + case obos::DestinationShorthand::None: + icr2 |= _destination << (56 - 32); + break; + default: + break; + } + switch (deliveryMode) + { + case obos::DeliveryMode::SMI: + vector = 0; + break; + case obos::DeliveryMode::NMI: + vector = 0; + break; + case obos::DeliveryMode::INIT: + vector = 0; + break; + default: + break; + } + icr1 |= vector; + icr1 |= ((uint32_t)deliveryMode & 0b111) << 8; + icr1 |= (uint32_t)shorthand << 18; + g_localAPICAddress->interruptCommand32_63 = icr2; + g_localAPICAddress->interruptCommand0_31 = icr1; + while ((g_localAPICAddress->interruptCommand0_31 >> 12 & 0b1)) pause(); + } + static uint8_t getRedirectionEntryIndex(uint8_t irq) + { + for (size_t i = 0; i < g_szIoapicRedirectionEntries; i++) + if (g_ioapicRedirectionEntries[i].source == irq) + return g_ioapicRedirectionEntries[i].globalSystemInterrupt; + return irq; + } +#define GetIOAPICRegisterOffset(reg) ((uint8_t)(uintptr_t)(&((IOAPIC_Registers*)nullptr)->reg) / 4) + bool IOAPIC_MaskIRQ(uint8_t irq, bool mask) + { + if (!g_IOAPICAddress) + return false; + uint32_t maximumRedirectionEntries = GetIOAPICRegisterOffset(ioapicVersion); + maximumRedirectionEntries = (g_IOAPICAddress->ReadRegister(maximumRedirectionEntries) >> 16) & 0xff; + uint8_t redirectionEntryIndex = getRedirectionEntryIndex(irq); + if (redirectionEntryIndex > (maximumRedirectionEntries + 1)) + return false; + uint64_t redirectionEntryOffset = GetIOAPICRegisterOffset(redirectionEntries[redirectionEntryIndex]); + uint64_t _entry = (uint64_t)g_IOAPICAddress->ReadRegister(redirectionEntryOffset) | ((uint64_t)g_IOAPICAddress->ReadRegister(redirectionEntryOffset + 1) << 32); + IOAPIC_RedirectionEntry* redirectionEntry = (IOAPIC_RedirectionEntry*)&_entry; + redirectionEntry->mask = mask; + g_IOAPICAddress->WriteRegister(redirectionEntryOffset, _entry); + g_IOAPICAddress->WriteRegister(redirectionEntryOffset + 1, _entry >> 32); + return true; + } + bool IOAPIC_MapIRQToVector(uint8_t irq, uint8_t vector, bool activeLow, triggerMode tm) + { + if (!g_IOAPICAddress) + return false; + uint32_t maximumRedirectionEntries = GetIOAPICRegisterOffset(ioapicVersion); + maximumRedirectionEntries = (g_IOAPICAddress->ReadRegister(maximumRedirectionEntries) >> 16) & 0xff; + uint8_t redirectionEntryIndex = getRedirectionEntryIndex(irq); + if (redirectionEntryIndex >= maximumRedirectionEntries) + return false; + uint64_t redirectionEntryOffset = GetIOAPICRegisterOffset(redirectionEntries[redirectionEntryIndex]); + uint64_t _entry = (uint64_t)g_IOAPICAddress->ReadRegister(redirectionEntryOffset) | ((uint64_t)g_IOAPICAddress->ReadRegister(redirectionEntryOffset + 1) << 32); + IOAPIC_RedirectionEntry* redirectionEntry = (IOAPIC_RedirectionEntry*)&_entry; + redirectionEntry->delMod = 0b000 /* Fixed */; + redirectionEntry->destMode = false /* Physical Mode */; + redirectionEntry->mask = false /* Unmasked */; + redirectionEntry->intPol = activeLow; + redirectionEntry->triggerMode = (bool)tm; + redirectionEntry->vector = vector; + redirectionEntry->destination.physical.lapicId = 0; + g_IOAPICAddress->WriteRegister(redirectionEntryOffset, _entry); + g_IOAPICAddress->WriteRegister(redirectionEntryOffset + 1, _entry >> 32); + return true; + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/irq/apic.h b/src/oboskrnl/arch/x86_64/irq/apic.h new file mode 100644 index 00000000..b1e18718 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/irq/apic.h @@ -0,0 +1,163 @@ +/* + oboskrnl/arch/x86_64/irq/apic.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include +#include + +namespace obos +{ + struct LAPIC + { + alignas(0x10) volatile const uint8_t resv1[0x20]; + alignas(0x10) volatile uint32_t lapicID; + alignas(0x10) volatile uint32_t lapicVersion; + alignas(0x10) volatile const uint8_t resv2[0x40]; + alignas(0x10) volatile uint32_t taskPriority; + alignas(0x10) volatile const uint32_t arbitrationPriority; + alignas(0x10) volatile const uint32_t processorPriority; + alignas(0x10) volatile uint32_t eoi; // write zero to send eoi. + alignas(0x10) volatile uint32_t remoteRead; + alignas(0x10) volatile uint32_t logicalDestination; + alignas(0x10) volatile uint32_t destinationFormat; + alignas(0x10) volatile uint32_t spuriousInterruptVector; + alignas(0x10) volatile const uint32_t inService0_31; + alignas(0x10) volatile const uint32_t inService32_63; + alignas(0x10) volatile const uint32_t inService64_95; + alignas(0x10) volatile const uint32_t inService96_127; + alignas(0x10) volatile const uint32_t inService128_159; + alignas(0x10) volatile const uint32_t inService160_191; + alignas(0x10) volatile const uint32_t inService192_223; + alignas(0x10) volatile const uint32_t inService224_255; + alignas(0x10) volatile const uint32_t triggerMode0_31; + alignas(0x10) volatile const uint32_t triggerMode32_63; + alignas(0x10) volatile const uint32_t triggerMode64_95; + alignas(0x10) volatile const uint32_t triggerMode96_127; + alignas(0x10) volatile const uint32_t triggerMode128_159; + alignas(0x10) volatile const uint32_t triggerMode160_191; + alignas(0x10) volatile const uint32_t triggerMode192_223; + alignas(0x10) volatile const uint32_t triggerMode224_255; + alignas(0x10) volatile const uint32_t interruptRequest0_31; + alignas(0x10) volatile const uint32_t interruptRequest32_63; + alignas(0x10) volatile const uint32_t interruptRequest64_95; + alignas(0x10) volatile const uint32_t interruptRequest96_127; + alignas(0x10) volatile const uint32_t interruptRequest128_159; + alignas(0x10) volatile const uint32_t interruptRequest160_191; + alignas(0x10) volatile const uint32_t interruptRequest192_223; + alignas(0x10) volatile const uint32_t interruptRequest224_255; + alignas(0x10) volatile uint32_t errorStatus; + alignas(0x10) volatile const uint8_t resv3[0x60]; + alignas(0x10) volatile uint32_t lvtCMCI; + alignas(0x10) volatile uint32_t interruptCommand0_31; + alignas(0x10) volatile uint32_t interruptCommand32_63; + alignas(0x10) volatile uint32_t lvtTimer; + alignas(0x10) volatile uint32_t lvtThermalSensor; + alignas(0x10) volatile uint32_t lvtPerformanceMonitoringCounters; + alignas(0x10) volatile uint32_t lvtLINT0; + alignas(0x10) volatile uint32_t lvtLINT1; + alignas(0x10) volatile uint32_t lvtError; + alignas(0x10) volatile uint32_t initialCount; + alignas(0x10) volatile const uint32_t currentCount; + alignas(0x10) volatile const uint8_t resv4[0x40]; + alignas(0x10) volatile uint32_t divideConfig; + alignas(0x10) volatile const uint8_t resv5[0x10]; + }; + struct IOAPIC + { + alignas(0x10) volatile mutable uint8_t ioregsel; + alignas(0x10) volatile uint32_t iowin; + void WriteRegister(uint8_t offset, uint32_t val); + uint32_t ReadRegister(uint8_t offset) const; + }; + struct IOAPIC_RedirectionEntry + { + uint8_t vector; + uint8_t delMod : 3; + bool destMode : 1; + const bool delivStatus : 1; + bool intPol : 1; + const bool remoteIRR : 1; + bool triggerMode : 1; + bool mask : 1; + const uint64_t padding : 39; + union + { + struct + { + uint8_t setOfProcessors; + } OBOS_PACK logical; + struct + { + uint8_t resv1 : 4; + uint8_t lapicId : 4; + } OBOS_PACK physical; + } OBOS_PACK destination; + } OBOS_PACK; + struct IOAPIC_Registers + { + struct { + const uint32_t resv1 : 24; + uint8_t ioapicID : 4; + const uint8_t resv2 : 4; + } OBOS_PACK ioapicId; + struct + { + const uint8_t version; + const uint8_t resv1; + const uint8_t maximumRedirectionEntries; + const uint8_t resv2; + } OBOS_PACK ioapicVersion; + struct + { + const uint32_t resv1 : 24; + const uint8_t ioapicID : 4; + const uint8_t resv2 : 4; + } OBOS_PACK ioapicArbitrationID; + uint32_t resv1[13]; + IOAPIC_RedirectionEntry redirectionEntries[]; + } OBOS_PACK; + struct IOAPIC_IRQRedirectionEntry + { + uint8_t source; + uint32_t globalSystemInterrupt; + }; + extern LAPIC* g_localAPICAddress; + extern IOAPIC* g_IOAPICAddress; + LAPIC* GetLAPICAddress(); + void InitializeLAPIC(LAPIC* lapicAddress); + void InitializeIOAPIC(IOAPIC* ioapicAddress); + enum class DestinationShorthand + { + None, + Self, + All, + All_Except_Self, + HighestValue = All_Except_Self + }; + enum class DeliveryMode + { + Fixed, Default = Fixed, + Fixed_LowestPriority, + SMI, + NMI = 4, + INIT, + SIPI, + }; + void LAPIC_SendEOI(); + void LAPIC_SendIPI(DestinationShorthand shorthand, DeliveryMode deliveryMode, uint8_t vector = 0, uint8_t _destination = 0); + bool IOAPIC_MaskIRQ(uint8_t irq, bool mask = false); + enum class triggerMode : bool + { + EdgeSensitive = 0, + LevelSensitive = 1, + }; + bool IOAPIC_MapIRQToVector(uint8_t irq, uint8_t vector, bool activeLow = false, triggerMode tm = triggerMode::EdgeSensitive); + namespace arch + { + void RegisterIPIHandler(); + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/irq/idt.cpp b/src/oboskrnl/arch/x86_64/irq/idt.cpp new file mode 100644 index 00000000..cf356ca6 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/irq/idt.cpp @@ -0,0 +1,81 @@ +/* + oboskrnl/arch/x86_64/idt.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include +#include + +#include + +extern "C" char __b_isr_handler; +extern "C" char __e_isr_handler; + +namespace obos +{ + struct idtEntry + { + uint16_t offset1; + uint16_t selector; + uint8_t ist; + uint8_t typeAttributes; + uint16_t offset2; + uint32_t offset3; + uint32_t resv1; + } g_idtEntries[256]; + struct idtPointer + { + uint16_t size; + uintptr_t idt; + } OBOS_PACK; + enum + { + // Present, Trap Gate + DEFAULT_TYPE_ATTRIBUTE = 0x8E, + // Max DPL: 3 + TYPE_ATTRIBUTE_USER_MODE = 0x60 + }; + uintptr_t g_handlers[256]; + extern "C" void idtFlush(idtPointer* idtptr); + static void RegisterISRInIDT(uint8_t vec, uintptr_t addr, bool canUsermodeCall, uint8_t ist = 0) + { + auto& idtEntry = g_idtEntries[vec]; + idtEntry.ist = ist & 0x7; + idtEntry.offset1 = addr & 0xffff; + idtEntry.selector = 0x8; + idtEntry.typeAttributes = DEFAULT_TYPE_ATTRIBUTE | (canUsermodeCall ? TYPE_ATTRIBUTE_USER_MODE : 0); + idtEntry.offset2 = (addr >> 16) & 0xffff; + idtEntry.offset3 = (addr >> 32) & 0xffffffff; + } + static int getIntIST(int i) + { + if (i >= 32) + return 0; + if (i == 8) + return 2; + return (i == 14 || i == 2 || i == 8 || i == 18 || i == 13) ? 1 : 0; + } + void InitializeIDT() + { + for (int i = 0; i < 256; i++) + RegisterISRInIDT(i, (uintptr_t)(&__b_isr_handler + (i * 32)), true, getIntIST(i)); + idtPointer idtPtr{ sizeof(g_idtEntries) - 1, (uintptr_t)g_idtEntries }; + idtFlush(&idtPtr); + } + void RawRegisterInterrupt(uint8_t vec, uintptr_t f) + { + g_handlers[vec] = f; + } + namespace arch + { + void RegisterInterrupt(uint8_t vec, void(*f)(interrupt_frame*)) + { + RawRegisterInterrupt(vec + 0x20, (uintptr_t)f); + } + bool InterruptRegistered(uint8_t vec) + { + return g_handlers[vec + 0x20] == 0; + } + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/irq/idt.h b/src/oboskrnl/arch/x86_64/irq/idt.h new file mode 100644 index 00000000..dc71fb7b --- /dev/null +++ b/src/oboskrnl/arch/x86_64/irq/idt.h @@ -0,0 +1,37 @@ +/* + oboskrnl/arch/x86_64/idt.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include + +#include + +#define OBOS_MAX_INTERRUPT_VECTORS (224) +#define OBOS_MAX_INTERRUPT_VECTORS_PER_IRQL (0x10) +#define OBOS_IRQL_TO_VECTOR(irql) ((irql) >= 2 ? ((irql) * 0x10 - 0x20) : 0) +#define OBOS_NO_EOI_ON_SPURIOUS_INTERRUPT (true) + +namespace obos +{ + void InitializeIDT(); + void RawRegisterInterrupt(uint8_t vec, uintptr_t f); + namespace arch + { + /// + /// Registers an interrupt handler. + /// + /// The vector. If the interrupt at this vector has already been registered, the previous handler will be overwritten. + /// The handler. This can be nullptr to unregister the interrupt handler. + void RegisterInterrupt(uint8_t vec, void(*f)(interrupt_frame*)); + /// + /// Queries whether an interrupt has been registered. + /// + /// The interrupt's vector. + /// Whether the interrupt has been registered. + bool InterruptRegistered(uint8_t vec); + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/irq/interrupt_frame.h b/src/oboskrnl/arch/x86_64/irq/interrupt_frame.h new file mode 100644 index 00000000..8c2f70bc --- /dev/null +++ b/src/oboskrnl/arch/x86_64/irq/interrupt_frame.h @@ -0,0 +1,53 @@ +/* + oboskrnl/arch/x86_64/irq/interrupt_frame.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include + +#define BITFIELD_FROM_BIT(n) (1< + +#include +#include + +#include +#include + +#include +#include + +#include + +#include +#include + +extern "C" uint64_t FindCounter(uint64_t hz); + +namespace obos +{ + namespace arch + { + Irq g_ipiIrq{ 3,false }; + void timer_ipi::_handler(base_ipi* _this, interrupt_frame*) + { + timer_ipi* obj = (timer_ipi*)_this; + g_localAPICAddress->divideConfig = 0b1101; + g_localAPICAddress->lvtTimer = (obj->irq->GetVector() + 0x20) | (((uint32_t)!obj->singleShoot) << 17); + g_localAPICAddress->initialCount = FindCounter(obj->freq); + } + void shootdown_ipi::_handler(base_ipi* _this, interrupt_frame*) + { + shootdown_ipi* obj = (shootdown_ipi*)_this; + if (getCR3() == (uintptr_t)obj->pm) + invlpg(obj->virt); + } + void dbg_reg_ipi::_handler(base_ipi* _this, interrupt_frame*) + { + // uint8_t regIdx; + // uint64_t* val; + // // false to read from DRn into *val, true to write the value at *val into DRn. + // bool rw; + dbg_reg_ipi* obj = (dbg_reg_ipi*)_this; + if (!obj->rw) + { + uint64_t ret = 0; + switch (obj->regIdx) + { + case 0: asm volatile("mov %%dr0, %0" :"=r"(ret)::); break; + case 1: asm volatile("mov %%dr1, %0" :"=r"(ret)::); break; + case 2: asm volatile("mov %%dr2, %0" :"=r"(ret)::); break; + case 3: asm volatile("mov %%dr3, %0" :"=r"(ret)::); break; + case 6: + [[fallthrough]]; case 4: asm volatile("mov %%dr6, %0" :"=r"(ret)::); break; + case 7: + [[fallthrough]]; case 5: asm volatile("mov %%dr7, %0" :"=r"(ret)::); break; + default: ret = 0xffff'ffff'ffff'ffff; + } + *obj->val = ret; + } + else + { + uint64_t val = *obj->val; + switch (obj->regIdx) + { + case 0: asm volatile("mov %0, %%dr0" ::"r"(val):); break; + case 1: asm volatile("mov %0, %%dr1" ::"r"(val):); break; + case 2: asm volatile("mov %0, %%dr2" ::"r"(val):); break; + case 3: asm volatile("mov %0, %%dr3" ::"r"(val):); break; + case 6: + [[fallthrough]]; case 4: asm volatile("mov %0, %%dr6" ::"r"(val):); break; + case 7: + [[fallthrough]]; case 5: asm volatile("mov %0, %%dr7" ::"r"(val):); break; + } + } + } + // Required IRQL: 0x3 + void IpiHandler(const Irq*, const IrqVector*, void*, interrupt_frame* frame) + { + uint8_t oldIRQL = 0xff; + if (GetIRQL() < 3) + RaiseIRQL(0x3, &oldIRQL); + ipi* cur = scheduler::GetCPUPtr()->archSpecific.ipi_queue.pop(); + if (!cur) + return; // No IPI to handle. + OBOS_ASSERTP(cur->type != ipi::IPI_INVALID, "IPI called with invalid type."); + OBOS_ASSERTP(cur->data.base->handler != nullptr, "IPI called with null handler.\n"); + if (cur->data.base->handler == nullptr) + { + if (oldIRQL != 0xff) + LowerIRQL(oldIRQL); + return; + } + if (cur->type == ipi::IPI_INVALID) + { + if (oldIRQL != 0xff) + LowerIRQL(oldIRQL); + return; + } + cur->data.base->handler(cur->data.base, frame); + cur->processed = true; + if (oldIRQL != 0xff) + LowerIRQL(oldIRQL); + } + bool IpiChecker(const Irq*, const struct IrqVector*, void*) + { + return scheduler::GetCPUPtr()->archSpecific.ipi_queue.nNodes > 0 /* If there are any pending IPIs */; + } + void RegisterIPIHandler() + { + new (&g_ipiIrq) Irq{ 3, false }; + g_ipiIrq.SetIRQChecker(IpiChecker, nullptr); + g_ipiIrq.SetHandler(IpiHandler, nullptr); + } + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/isr.asm b/src/oboskrnl/arch/x86_64/irq/isr.asm similarity index 55% rename from src/oboskrnl/arch/x86_64/isr.asm rename to src/oboskrnl/arch/x86_64/irq/isr.asm index 60290416..f7b70683 100644 --- a/src/oboskrnl/arch/x86_64/isr.asm +++ b/src/oboskrnl/arch/x86_64/irq/isr.asm @@ -1,232 +1,170 @@ -; oboskrnl/arch/x86_64/isr.asm -; -; Copyright (c) 2024 Omar Berrow - -bits 64 -[DEFAULT ABS] - -section .data -global Arch_KernelCR3:data hidden -Arch_KernelCR3: - dq 0 - -section .text -default rel - -global Arch_StartISRHandlersText: data hidden -global Arch_EndISRHandlersText: data hidden -Arch_StartISRHandlersText: -align 32 -global Arch_b_isr_handler:function hidden -global Arch_e_isr_handler:function hidden -global Arch_FlushIDT:function hidden -Arch_b_isr_handler: -%macro isr_handler_no_ec 1 -align 32 -isr%1: - cld - push 0 - push %1 - push %1-0x20 - jmp int_handler_common -%endmacro -%macro isr_handler_ec 1 -align 0x20 -isr%1: - cld - push %1 - push %1-0x20 - jmp int_handler_common -%endmacro -isr_handler_no_ec 0 -isr_handler_no_ec 1 -isr_handler_no_ec 2 -isr_handler_no_ec 3 -isr_handler_no_ec 4 -isr_handler_no_ec 5 -isr_handler_no_ec 6 -isr_handler_no_ec 7 -isr_handler_ec 8 -isr_handler_no_ec 9 -isr_handler_ec 10 -isr_handler_ec 11 -isr_handler_ec 12 -isr_handler_ec 13 -isr_handler_ec 14 -isr_handler_no_ec 15 -isr_handler_no_ec 16 -isr_handler_ec 17 -isr_handler_no_ec 18 -isr_handler_no_ec 19 -isr_handler_no_ec 20 -isr_handler_ec 21 -isr_handler_no_ec 22 -isr_handler_no_ec 23 -isr_handler_no_ec 24 -isr_handler_no_ec 25 -isr_handler_no_ec 26 -isr_handler_no_ec 27 -isr_handler_no_ec 28 -isr_handler_no_ec 29 -isr_handler_no_ec 30 -isr_handler_no_ec 31 -%assign current_int_number 32 -%rep 224 -isr_handler_no_ec current_int_number -%assign current_int_number current_int_number+1 -%endrep -Arch_e_isr_handler: - -%macro pushaq 0 -push rax -; rax has rsp. -mov rax, rsp -add rax, 8 -push rcx -push rdx -push rbx -push rax ; Push rsp -push rsi -push rdi -push r8 -push r9 -push r10 -push r11 -push r12 -push r13 -push r14 -push r15 -push qword [rsp+0x90] -push rbp -%endmacro - -; Cleans up after pushaq. - -%macro popaq 0 -pop rbp -add rsp, 8 -pop r15 -pop r14 -pop r13 -pop r12 -pop r11 -pop r10 -pop r9 -pop r8 -pop rdi -pop rsi -add rsp, 8 -pop rbx -pop rdx -pop rcx -pop rax -%endmacro - -extern Arch_IRQHandlers - -int_handler_common: - pushaq - mov rbp, rsp - - mov rax, ds - push rax - - mov rax, cr3 - push rax - -; If we are not using the kernel's CR3 - mov rax, [Arch_KernelCR3] - mov rdx, cr3 - cmp rdx, rax - je .no_switch_cr3 - -; Then switch to the kernel cr3 - mov cr3, rax -.no_switch_cr3: - -; If we were interrupted in user code - test qword [rsp+0xB8], 0x3 ; User code - jz .no_swapgs1 -; Then swapgs - swapgs -.no_swapgs1: - mov rax, [rsp+0xA0] - cmp rax, 255 - ja .finished - mov rax, [Arch_IRQHandlers+rax*8] - - test rax,rax - jz .finished - - mov rdi, rsp - call rax - -.finished: - - pop rax - cmp rax, [Arch_KernelCR3] - je .no_switch_cr3_2 - ; Switch to the old cr3 - mov cr3, rax - -.no_switch_cr3_2: - - test qword [rsp+0xB0], 0x3 ; User code - je .no_swapgs2 - swapgs -.no_swapgs2: - - mov rsp, rbp - popaq - - add rsp, 0x18 - - iretq -Arch_EndISRHandlersText: -section .text -Arch_FlushIDT: - lidt [rdi] - ret -global CoreS_GetIRQL:function hidden -global CoreS_SetIRQL:function hidden -extern OBOS_Panic -section .rodata -panic_format1: db "Invalid IRQL %d passed to CoreS_GetIRQL.", 0xa, 0x0 -section .text -; Takes no registers as input. -; Sets rax to the current IRQL (cr8). -CoreS_GetIRQL: - push rbp - mov rbp, rsp - - mov rax, cr8 - - leave - ret -; Input: -; rdi: New IRQL. -CoreS_SetIRQL: - push rbp - mov rbp, rsp - - cmp rdi, 1 - jne .success -; Panic! - mov rax, 1 - push rdi - mov rdi, 1 ; OBOS_PANIC_FATAL_ERROR - mov rsi, panic_format1 - pop rdx - call OBOS_Panic -.success: - mov cr8, rdi - - leave - ret -section .text -global Arch_disablePIC:function hidden -Arch_disablePIC: - mov al, 0xff - out 0x21, al - out 0xA1, al - ret +; oboskrnl/arch/x86_64/isr.asm +; +; Copyright (c) 2024 Omar Berrow + +section .text + +align 32 +global __b_isr_handler +global __e_isr_handler +global idtFlush +__b_isr_handler: +%macro isr_handler_no_ec 1 +align 32 +isr%1: + cld + push 0 + push %1 + push %1-0x20 + jmp int_handler_common +%endmacro +%macro isr_handler_ec 1 +align 0x20 +isr%1: + cld + push %1 + push %1-0x20 + jmp int_handler_common +%endmacro +isr_handler_no_ec 0 +isr_handler_no_ec 1 +isr_handler_no_ec 2 +isr_handler_no_ec 3 +isr_handler_no_ec 4 +isr_handler_no_ec 5 +isr_handler_no_ec 6 +isr_handler_no_ec 7 +isr_handler_ec 8 +isr_handler_no_ec 9 +isr_handler_ec 10 +isr_handler_ec 11 +isr_handler_ec 12 +isr_handler_ec 13 +isr_handler_ec 14 +isr_handler_no_ec 15 +isr_handler_no_ec 16 +isr_handler_ec 17 +isr_handler_no_ec 18 +isr_handler_no_ec 19 +isr_handler_no_ec 20 +isr_handler_ec 21 +isr_handler_no_ec 22 +isr_handler_no_ec 23 +isr_handler_no_ec 24 +isr_handler_no_ec 25 +isr_handler_no_ec 26 +isr_handler_no_ec 27 +isr_handler_no_ec 28 +isr_handler_no_ec 29 +isr_handler_no_ec 30 +isr_handler_no_ec 31 +%assign current_int_number 32 +%rep 224 +isr_handler_no_ec current_int_number +%assign current_int_number current_int_number+1 +%endrep +__e_isr_handler: + +%macro pushaq 0 +push rax +; rax has rsp. +mov rax, rsp +add rax, 8 +push rcx +push rdx +push rbx +push rax ; Push rsp +push rsi +push rdi +push r8 +push r9 +push r10 +push r11 +push r12 +push r13 +push r14 +push r15 +push qword [rsp+0x90] +push rbp +%endmacro + +; Cleans up after pushaq. + +%macro popaq 0 +pop rbp +add rsp, 8 +pop r15 +pop r14 +pop r13 +pop r12 +pop r11 +pop r10 +pop r9 +pop r8 +pop rdi +pop rsi +add rsp, 8 +pop rbx +pop rdx +pop rcx +pop rax +%endmacro + +extern _ZN4obos10g_handlersE + +int_handler_common: + pushaq + mov rbp, rsp + + mov rax, ds + push rax + + mov rax, [rsp+0x98] + cmp rax, 255 + ja .finished + mov rax, [_ZN4obos10g_handlersE+rax*8] + + swapgs + + test rax,rax + jz .finished + + mov rdi, rsp + call rax + +.finished: + swapgs + mov rsp, rbp + popaq + + add rsp, 0x18 + + iretq + +idtFlush: + lidt [rdi] + ret +global _ZN4obos4arch7SetIRQLEh +global _ZN4obos4arch7GetIRQLEv +_ZN4obos4arch7GetIRQLEv: + push rbp + mov rbp, rsp + + mov rax, cr8 + + leave + ret +_ZN4obos4arch7SetIRQLEh: + push rbp + mov rbp, rsp + + mov cr8, rdi + + leave + ret +global disablePIC +disablePIC: + mov al, 0xff + out 0x21, al + out 0xA1, al + ret \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/irq/madt.cpp b/src/oboskrnl/arch/x86_64/irq/madt.cpp new file mode 100644 index 00000000..98a3d641 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/irq/madt.cpp @@ -0,0 +1,75 @@ +/* + oboskrnl/arch/x86_64/irq/madt.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include +#include + +#include + +#include + +namespace obos +{ + void ScanMADT(MADTTable* madt, void(*callback)(MADT_EntryHeader* header, void* userdata), void* userdata, uint8_t entryType) + { + MADT_EntryHeader* entryHeader = (MADT_EntryHeader*)(madt + 1); + void* end = reinterpret_cast(reinterpret_cast(madt) + madt->sdtHeader.Length); + for (; entryHeader < end; entryHeader = reinterpret_cast(reinterpret_cast(entryHeader) + entryHeader->length)) + if (entryHeader->type == entryType) + callback(entryHeader, userdata); + } + size_t ParseMADTForIOAPICAddresses(MADTTable* madt, uintptr_t* addresses, size_t maxEntries) + { + uintptr_t userdata[] = { (uintptr_t)addresses, (uintptr_t)maxEntries, 0 }; + ScanMADT(madt, [](MADT_EntryHeader* _header, void* udata) + { + uintptr_t *userdata = (uintptr_t*)udata; + MADT_EntryType1* header = (MADT_EntryType1*)_header; + uintptr_t* addresses = (uintptr_t*)userdata[0]; + size_t maxEntries = (size_t)userdata[1]; + if (userdata[2]++ < maxEntries) + addresses[userdata[2] - 1] = header->ioapicAddress; + }, userdata, 1); + if (maxEntries > userdata[2]) + maxEntries = userdata[2]; + return userdata[2] - maxEntries; + } + size_t ParseMADTForIOAPICRedirectionEntries(MADTTable* madt, IOAPIC_IRQRedirectionEntry* entries, size_t maxEntries) + { + uintptr_t userdata[] = { (uintptr_t)entries, (uintptr_t)maxEntries, 0 }; + ScanMADT(madt, [](MADT_EntryHeader* _header, void* udata) + { + uintptr_t* userdata = (uintptr_t*)udata; + MADT_EntryType2* header = (MADT_EntryType2*)_header; + IOAPIC_IRQRedirectionEntry* addresses = (IOAPIC_IRQRedirectionEntry*)userdata[0]; + size_t maxEntries = (size_t)userdata[1]; + if (userdata[2]++ < maxEntries) + { + addresses[userdata[2] - 1].globalSystemInterrupt = header->globalSystemInterrupt; + addresses[userdata[2] - 1].source = header->irqSource; + } + }, userdata, 2); + if (maxEntries > userdata[2]) + maxEntries = userdata[2]; + return userdata[2] - maxEntries; + } + size_t ParseMADTForLAPICIds(MADTTable* madt, uint8_t* lapicIds, size_t maxEntries) + { + uintptr_t userdata[] = { (uintptr_t)lapicIds, (uintptr_t)maxEntries, 0 }; + ScanMADT(madt, [](MADT_EntryHeader* _header, void* udata) + { + uintptr_t* userdata = (uintptr_t*)udata; + MADT_EntryType0* header = (MADT_EntryType0*)_header; + uint8_t* addresses = (uint8_t*)userdata[0]; + size_t maxEntries = (size_t)userdata[1]; + if (userdata[2]++ < maxEntries) + addresses[userdata[2] - 1] = header->apicID; + }, userdata, 0); + if (maxEntries > userdata[2]) + maxEntries = userdata[2]; + return userdata[2] - maxEntries; + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/irq/madt.h b/src/oboskrnl/arch/x86_64/irq/madt.h new file mode 100644 index 00000000..e1b0669e --- /dev/null +++ b/src/oboskrnl/arch/x86_64/irq/madt.h @@ -0,0 +1,105 @@ +/* + oboskrnl/arch/x86_64/irq/madt.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include + +#include + +#include + +namespace obos +{ + struct MADTTable + { + ACPISDTHeader sdtHeader; + uint32_t lapicAddress; + uint32_t unwanted; + // There are more entries. + } OBOS_PACK; + struct MADT_EntryHeader + { + uint8_t type; + uint8_t length; + } OBOS_PACK; + struct MADT_EntryType0 + { + MADT_EntryHeader entryHeader; + uint8_t processorID; + uint8_t apicID; + uint32_t flags; + } OBOS_PACK; + struct MADT_EntryType1 + { + MADT_EntryHeader entryHeader; + uint8_t ioApicID; + uint8_t resv1; + uint32_t ioapicAddress; + uint32_t globalSystemInterruptBase; + } OBOS_PACK; + struct MADT_EntryType2 + { + MADT_EntryHeader entryHeader; + uint8_t busSource; + uint8_t irqSource; + uint32_t globalSystemInterrupt; + uint16_t flags; + } OBOS_PACK; + struct MADT_EntryType3 + { + MADT_EntryHeader entryHeader; + uint8_t nmiSource; + uint8_t resv; + uint16_t flags; + uint32_t globalSystemInterrupt; + } OBOS_PACK; + struct MADT_EntryType4 + { + MADT_EntryHeader entryHeader; + uint8_t processorID; + uint16_t flags; + uint8_t lINT; + } OBOS_PACK; + struct MADT_EntryType5 + { + MADT_EntryHeader entryHeader; + uint8_t resv1[2]; + uintptr_t lapic_address; + } OBOS_PACK; + struct MADT_EntryType9 + { + MADT_EntryHeader entryHeader; + uint8_t resv1[2]; + uint32_t x2APIC_ID; + uint32_t flags; + uint32_t acpiID; + } OBOS_PACK; + /// + /// Parses the MADT for I/O APIC Addresses. + /// + /// The MADT table. + /// [out] The addresses of the I/O APICs on the computer. + /// The max entries that the 'addresses' parameter can hold. + /// How many entries are left to store. + size_t ParseMADTForIOAPICAddresses(MADTTable* table, uintptr_t* addresses, size_t maxEntries); + /// + /// Parses the MADT for I/O APIC Redirection Entries. + /// + /// The MADT table. + /// [out] The redirection entries. + /// The maximum entries that the 'entries' parameter can hold. + /// How many entries are left to store. + size_t ParseMADTForIOAPICRedirectionEntries(MADTTable* table, IOAPIC_IRQRedirectionEntry* entries, size_t maxEntries); + /// + /// Parses the MADT for LAPIC ids. + /// + /// The MADT table. + /// [out] The lapic ids. + /// The maximum entries that the 'lapicIds' parameter can hold. + /// How many entries are left to store. + size_t ParseMADTForLAPICIds(MADTTable* table, uint8_t* lapicIds, size_t maxEntries); +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/irq_vector.h b/src/oboskrnl/arch/x86_64/irq_vector.h deleted file mode 100644 index d6f46f7d..00000000 --- a/src/oboskrnl/arch/x86_64/irq_vector.h +++ /dev/null @@ -1,17 +0,0 @@ -/* - * oboskrnl/arch/x86_64/irq_vector.h - * - * Copyright (c) 2024 Omar Berrow - */ - -#pragma once - -#include - -#include - -typedef uint8_t irq_vector_id; -#define OBOS_IRQ_VECTOR_ID_MAX (224 /* 256 vectors - the 32 reserved vectors */) -#define OBOS_IRQ_VECTOR_ID_TO_IRQL(x) ((irql)(((x)>>4)+2)) -#define OBOS_IRQL_TO_IRQ_VECTOR_ID(x) ((irq_vector_id)(((x)<<4)-0x20)) -#define OBOS_IRQ_VECTOR_ID_COUNT_PER_IRQL (16) \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/kdbg/bp.h b/src/oboskrnl/arch/x86_64/kdbg/bp.h new file mode 100644 index 00000000..a8a5dd09 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/kdbg/bp.h @@ -0,0 +1,49 @@ +/* + oboskrnl/arch/x86_64/kdbg/bp.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include + +namespace obos +{ + namespace kdbg + { + struct bp + { + bp() = default; + bp(uintptr_t rip); + uint32_t idx; + void setStatus(bool to); + uintptr_t rip = 0; + size_t hitCount = 0; + bool enabled = false; + bool awaitingSmpRefresh = false; // Set for breakpoints that are initialized before SMP initialization. + struct + { + const char* name = nullptr; + uintptr_t base = 0; + } funcInfo{}; + ~bp(); + }; + // struct bp_node + // { + // bp_node *next, *prev; + // bp *data; + // }; + // struct bp_list + // { + // bp_node *head, *tail; + // size_t nBreakpoints; + // void append(bp* breakpoint); + // void remove(uint32_t idx); + // bool contains(size_t idx); + // bp& operator[](size_t idx); + // bp_node* getNode(size_t idx); + // }; + } + +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/kdbg/debugger_state.h b/src/oboskrnl/arch/x86_64/kdbg/debugger_state.h new file mode 100644 index 00000000..864a3336 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/kdbg/debugger_state.h @@ -0,0 +1,41 @@ +/* + oboskrnl/arch/x86_64/kdbg/debugger_state.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include + +#include + +#include + +#include + +namespace obos +{ + namespace kdbg + { + struct debugger_state + { + bp* breakpoints[4]; + size_t nBreakpointsInUse; + size_t nextBpIndex = 0; + // bool watchdogSet = false; + // size_t nSecondsForWatchdog = 0; + // Irq watchdogIRQ{0,0,0}; + // bool watchdogIRQInitialized = false; + }; + extern debugger_state g_kdbgState; + struct cpu_local_debugger_state + { + arch::ThreadContextInfo context; + bool shouldStopAtNextInst : 1; + bool isFinishingFunction : 1; + size_t nCallsSinceFinishCommand = 0; + }; + } + +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/kdbg/disasm.cpp b/src/oboskrnl/arch/x86_64/kdbg/disasm.cpp new file mode 100644 index 00000000..62b14277 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/kdbg/disasm.cpp @@ -0,0 +1,136 @@ +/* + oboskrnl/arch/x86_64/kdbg/disasm.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#if OBOS_KDBG_ENABLED +#include + +#include + +#if OBOS_KDBG_ENABLE_ZYDIS +# include +#endif + +#include +#include + +#include + +#include + +#define TO_EHDR(obj) ((elf::Elf64_Ehdr*)obj) +#define TO_STRING_TABLE(ehdr, stable, off) ((const char*)ehdr + stable + off) + +namespace obos +{ + const elf::Elf64_Shdr* getSectionHeader(const elf::Elf64_Ehdr* ehdr, const char* name); + extern volatile limine_kernel_file_request kernel_file; + extern volatile limine_kernel_address_request kernel_addr; + namespace kdbg + { +#if OBOS_KDBG_ENABLE_ZYDIS + static void addr2sym(uintptr_t rip, const char** const symbolName, uintptr_t* symbolBase, size_t* symbolSize, uint8_t symType) + { + if (rip < kernel_addr.response->virtual_base) + { + *symbolName = nullptr; + *symbolBase = 0; + *symbolSize = 0; + return; + } + uintptr_t base = (uintptr_t)kernel_file.response->kernel_file->address; + elf::Elf64_Ehdr* ehdr = (elf::Elf64_Ehdr*)base; + size_t stable = 0; + if (getSectionHeader(ehdr, ".strtab")) + stable = getSectionHeader(ehdr, ".strtab")->sh_offset; + const elf::Elf64_Shdr* symtab = getSectionHeader(ehdr, ".symtab"); + if (!symtab) + { + *symbolName = nullptr; + *symbolBase = 0; + return; + } + size_t nEntries = symtab->sh_size / symtab->sh_entsize; + for (size_t i = 0; i < nEntries; i++) + { + elf::Elf64_Sym* symbol = (elf::Elf64_Sym*)(base + symtab->sh_offset + i*symtab->sh_entsize); + if (symType != elf::STT_NOTYPE) + if ((symbol->st_info & 0xf) != symType) + continue; + if (rip >= symbol->st_value && rip < (symbol->st_value + symbol->st_size)) + { + if (stable) + *symbolName = TO_STRING_TABLE(ehdr, stable, symbol->st_name); + else + *symbolName = "no strtab"; + *symbolBase = symbol->st_value; + *symbolSize = symbol->st_size; + return; + } + } + } + bool disasm(void* at, size_t nInstructions) + { + ZyanU64 eip = reinterpret_cast(at); + + uint8_t* data = (uint8_t*)at; + + ZyanUSize offset = 0; + size_t bytesSinceLastSymbolCheck = 0; + ZydisDisassembledInstruction instruction; + + size_t i = 0; + size_t symSize = 0; + uintptr_t symBase = 0; + const char* symName = nullptr; + + printf("Disassembly of address 0x%p.\n", at); + addr2sym((uintptr_t)at, &symName, &symBase, &symSize, elf::STT_FUNC); + bytesSinceLastSymbolCheck = (uintptr_t)at-symBase; + if (symName) + printf("%p <%s>:\n", at, symName); + while (i < nInstructions) + { + if (bytesSinceLastSymbolCheck >= symSize) + { + addr2sym((uintptr_t)eip, &symName, &symBase, &symSize, elf::STT_FUNC); + if (symName) + printf("\n%p <%s>:\n", (void*)symBase, symName); + bytesSinceLastSymbolCheck = 0; + } + ZyanStatus st = ZydisDisassembleIntel( + ZYDIS_MACHINE_MODE_LONG_64, + eip, + data + offset, + 32, + &instruction + ); + // TODO: Implement translating memory addresses to symbol names. + if (ZYAN_SUCCESS(st)) + printf("%p: %s\n", eip, instruction.text); + else + { + printf("%p: ???\n", eip); + instruction.info.length = 1; + } + offset += instruction.info.length; + bytesSinceLastSymbolCheck += instruction.info.length; + eip += instruction.info.length; + i++; + } + return true; + } +#else + bool disasm(void* at, size_t nInstructions) + { + printf("Disassembly is disabled in kernel configuration (OBOS_KDBG_ENABLE_ZYDIS=0).\n"); + return true; + } +#endif + } +} +#else +// Nothing to do +#endif diff --git a/src/oboskrnl/arch/x86_64/kdbg/exception_handlers.h b/src/oboskrnl/arch/x86_64/kdbg/exception_handlers.h new file mode 100644 index 00000000..ea9151fa --- /dev/null +++ b/src/oboskrnl/arch/x86_64/kdbg/exception_handlers.h @@ -0,0 +1,22 @@ +/* + oboskrnl/arch/x86_64/kdbg/exception_handlers.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include + +#include + +namespace obos +{ + namespace kdbg + { + // Returns false if the exception handler should be returned from. + // Otherwise, true. + bool exceptionHandler(interrupt_frame* frame); + } + +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/kdbg/init.cpp b/src/oboskrnl/arch/x86_64/kdbg/init.cpp new file mode 100644 index 00000000..20b9d6a2 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/kdbg/init.cpp @@ -0,0 +1,30 @@ +/* + oboskrnl/arch/x86_64/kdbg/init.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include + +#include +#include + +#include + +namespace obos +{ + namespace kdbg + { + bool g_initialized; + void init_kdbg(input_format inputDev, output_format outputDev) + { +#if OBOS_KDBG_ENABLED + g_inputDev = inputDev; + g_outputDev = outputDev; + g_initialized = true; + printf("oboskrnl: Kernel debugger is on.\n"); + breakpoint(); +#endif + } + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/kdbg/init.h b/src/oboskrnl/arch/x86_64/kdbg/init.h new file mode 100644 index 00000000..505bcc1f --- /dev/null +++ b/src/oboskrnl/arch/x86_64/kdbg/init.h @@ -0,0 +1,27 @@ +/* + oboskrnl/arch/x86_64/kdbg/init.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include + +#include + +#if OBOS_KDBG_ENABLED +#define breakpoint() asm volatile("int3") +#else +#define breakpoint() do {} while(0) +#endif + +namespace obos +{ + namespace kdbg + { + extern bool g_initialized; + void init_kdbg(input_format inputDev, output_format outputDev); + } + +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/kdbg/io.cpp b/src/oboskrnl/arch/x86_64/kdbg/io.cpp new file mode 100644 index 00000000..2e5cb0c9 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/kdbg/io.cpp @@ -0,0 +1,341 @@ +/* + oboskrnl/arch/x86_64/kdbg/io.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include +#include + +#include + +#include + +#define STB_SPRINTF_NOFLOAT 1 +#define STB_SPRINTF_MIN 8 +#include + +#include + +#include + +namespace obos +{ + namespace kdbg + { + output_format g_outputDev; + input_format g_inputDev; +#if OBOS_KDBG_ENABLED + static bool s_com1Initialized = false; + static bool s_ps2kInitialized = false; + constexpr uint16_t COM1 = 0x3F8; + static void common_initialize_serial(const uint16_t port) + { + constexpr uint8_t EIGHT_DATABITS = 0b11; + constexpr uint8_t ONE_STOPBIT = 0b0; + constexpr uint8_t PARITY_EVEN = 0b11000; + outb(port + 1, 0x01); + // Enable DLAB + outb(port + 3, 0x80); + // Set the baud rate divisor to 1 (115200 ticks). + outb(port, 1); + outb(port + 1, 0); + // Set the data bits, stop bits, and parity bit + outb(port + 3, EIGHT_DATABITS | ONE_STOPBIT | PARITY_EVEN); + + // RTS/DSR are set. + outb(port + 4, 0x03); + // Set the serial chip to "loopback mode." + outb(port + 4, 0x1E); + + // Check if the serial port is working correctly. + outb(port, 0xAE); + if (inb(port) != 0xAE) + return; + + // Disable all interrupts for the serial port. + outb(port + 2, 0x00); + + // Exit loopback mode. + outb(port + 4, 0x03); + } + static char getchar_serial(bool async) + { + if (!s_com1Initialized) + { + common_initialize_serial(COM1); + s_com1Initialized = true; + } + if (!(inb(COM1 + 5) & (1<<0)) && async) + return EOF; + while (!(inb(COM1 + 5) & (1<<0))) + pause(); + return inb(COM1); + } + static uint8_t ps2k_sendCommand(uint32_t nCommands, ...); + static void common_initialize_ps2k() + { + // Keys need to be held for 250 ms before repeating, and they repeat at a rate of 30 hz (33.33333 ms). + ps2k_sendCommand(2, 0xF3, 0); + + // Set scancode set 1. + ps2k_sendCommand(2, 0xF0, 0); + + // Enable scanning. + ps2k_sendCommand(1, 0xF4); + + // Clear all keyboard LEDs. + ps2k_sendCommand(2, 0xED, 0b000); + } + static uint8_t ps2k_sendCommand(uint32_t nCommands, ...) + { + uint8_t ret = 0x00; + va_list list; + uint8_t *commands = new uint8_t[nCommands]; + va_start(list, nCommands); + for (uint32_t x = 0; x < nCommands; x++) + commands[x] = va_arg(list, int); + va_end(list); + for (int i = 0; i < 5; i++) + { + for (size_t x = 0; x < nCommands; x++) + { + uint8_t& c = commands[x]; + while ((inb(0x64) & 0b10) == 0b10); + outb(0x60, c); + } + ret = inb(0x60); + if (ret == 0xFA /*ACK*/) + break; + if (ret == 0xFE /*RESEND*/) + continue; + // Assume abort. + break; + } + delete[] commands; + return ret; + } + static bool isNumber(char ch) + { + char temp = ch - '0'; + return temp >= 0 && temp < 10; + } + static bool isAlpha(char ch) + { + char tmp1 = ch - 'A', tmp2 = ch - 'a'; + return (tmp1 > 0 && tmp1 < 26) || (tmp2 > 0 && tmp2 < 26); + } + static size_t getLetterIndex(char ch) + { + size_t tmp1 = ch - 'A', tmp2 = ch - 'a'; + if (tmp1 > 0 && tmp1 < 26) + return tmp1; + if (tmp2 > 0 && tmp2 < 26) + return tmp2; + return SIZE_MAX; + } + static char getchar_keyboard(bool async) + { + if (!s_ps2kInitialized) + { + common_initialize_ps2k(); + s_ps2kInitialized = true; + } + static struct + { + bool capsLock : 1; + bool shiftPressed : 1; + } flags; + static char scancode_table[] = + { + /*0x00*/ '\0' , /*0x01*/ '\x1f', /*0x02*/ '1' , /*0x03*/ '2' , + /*0x04*/ '3' , /*0x05*/ '4' , /*0x06*/ '5' , /*0x07*/ '6' , + /*0x08*/ '7' , /*0x09*/ '8' , /*0x0A*/ '9' , /*0x0B*/ '0' , + /*0x0C*/ '-' , /*0x0D*/ '=' , /*0x0E*/ '\b', /*0x0F*/ '\t', + /*0x10*/ 'q' , /*0x11*/ 'w' , /*0x12*/ 'e' , /*0x13*/ 'r' , + /*0x14*/ 't' , /*0x15*/ 'y' , /*0x16*/ 'u' , /*0x17*/ 'i' , + /*0x18*/ 'o' , /*0x19*/ 'p' , /*0x1A*/ '[' , /*0x1B*/ ']' , + /*0x1C*/ '\n' , /*0x1D*/ '\0' , /*0x1E*/ 'a' , /*0x1F*/ 's' , + /*0x20*/ 'd' , /*0x21*/ 'f' , /*0x22*/ 'g' , /*0x23*/ 'h' , + /*0x24*/ 'j' , /*0x25*/ 'k' , /*0x26*/ 'l' , /*0x27*/ ';' , + /*0x28*/ '\'' , /*0x29*/ '`' , /*0x2A*/ '\0', /*0x2B*/ '\\', + /*0x2C*/ 'z' , /*0x2D*/ 'x' , /*0x2E*/ 'c' , /*0x2F*/ 'v' , + /*0x30*/ 'b' , /*0x31*/ 'n' , /*0x32*/ 'm' , /*0x33*/ ',' , + /*0x34*/ '.' , /*0x35*/ '/' , /*0x36*/ '\0', /*0x37*/ '*' , + /*0x38*/ '\0' , /*0x39*/ ' ' , /*0x3A*/ '\0', /*0x3B*/ '\0', + /*0x3C*/ '\0' , /*0x3D*/ '\0' , /*0x3E*/ '\0', /*0x3F*/ '\0', + /*0x40*/ '\0' , /*0x41*/ '\0' , /*0x42*/ '\0', /*0x43*/ '\0', + /*0x44*/ '\0' , /*0x45*/ '\0' , /*0x46*/ '\0', /*0x47*/ '7' , + /*0x48*/ '8' , /*0x49*/ '9' , /*0x4A*/ '-' , /*0x4B*/ '4' , + /*0x4C*/ '5' , /*0x4D*/ '6' , /*0x4E*/ '+' , /*0x4F*/ '1' , + /*0x50*/ '2' , /*0x51*/ '3' , /*0x52*/ '0' , /*0x53*/ '.' , + }; + while (1) + { + if (async && !(inb(0x64) & 1<<0)) + return EOF; + while (!(inb(0x64) & 1<<0)) + pause(); + uint8_t scancode = inb(0x60); + bool wasRelease = scancode & 0x80; + scancode &= ~0x80; + char ch = 0; + ch = wasRelease ? EOF : scancode_table[scancode]; + // If shift was pressed. + if (scancode == 0x2A || scancode == 0x36) + flags.shiftPressed = !wasRelease; + if (scancode == 0x3A) + flags.capsLock = !flags.capsLock; + if (!async && !ch) + continue; + if (flags.shiftPressed) + { + const char *shiftAlias = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "!@#$%^&*()" "<>?:\"{}|_+~"; + size_t index = SIZE_MAX; + if (isAlpha(ch)) + index = getLetterIndex(ch); + else if (isNumber(ch)) + index = (ch - '0') + 25 + (ch == '0' ? 10 : 0); + else + { + switch(ch) + { + case ',' : index = 36+0; break; + case '.' : index = 36+1; break; + case '/' : index = 36+2; break; + case ';' : index = 36+3; break; + case '\'': index = 36+4; break; + case '[' : index = 36+5; break; + case ']' : index = 36+6; break; + case '\\': index = 36+7; break; + case '-' : index = 36+8; break; + case '=' : index = 36+9; break; + case '`' : index = 36+10; break; + default: break; + } + } + if (index != SIZE_MAX) + ch = shiftAlias[index]; + } + if (flags.capsLock) + { + const char *capsAlias = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + if (isAlpha(ch)) + ch = capsAlias[ch-'a']; + } + return ch == 0 ? EOF : ch; + } + } + static bool putchar_console(char ch, bool) + { + g_kernelConsole.ConsoleOutput(ch); + return true; + } + static bool putchar_serial(char ch, bool async) + { + if (!s_com1Initialized) + { + common_initialize_serial(COM1); + s_com1Initialized = true; + } + if (!(inb(COM1 + 5) & (1<<5)) && async) + return false; + while (!(inb(COM1 + 5) & (1<<5))) + pause(); + outb(COM1, ch); + return true; + } + static char(*s_inputHandlers[2])(bool) = { getchar_keyboard, getchar_serial }; + static bool(*s_outputHandlers[2])(char, bool) = { putchar_console, putchar_serial }; + char getchar(bool async, bool echo) + { + if ((int)g_inputDev > 2 || (int)g_inputDev < 0) + return EOF; + char ch = s_inputHandlers[(int)g_inputDev - 1](async); + if (echo && ch != EOF) + putchar(ch, async); + return ch; + } + bool putchar(char ch, bool async) + { + if ((int)g_outputDev > 2 || (int)g_outputDev < 0) + return false; + if (ch == '\n') + { + bool r1 = s_outputHandlers[(int)g_outputDev - 1]('\r', async); + if (!r1) + return false; + bool r2 = s_outputHandlers[(int)g_outputDev - 1]('\n', async); + return r2; + } + return s_outputHandlers[(int)g_outputDev - 1](ch, async); + } + static char* outputCallback(const char* buf, void*, int len) + { + for (size_t i = 0; i < len; i++) + putchar(buf[i], false); + return (char*)buf; + } + size_t printf(const char* format, ...) + { + static locks::SpinLock kdbg_printf_lock; + kdbg_printf_lock.Lock(); + va_list list; + va_start(list, format); + char ch[8]; + size_t ret = stbsp_vsprintfcb(outputCallback, nullptr, ch, format, list); + va_end(list); + kdbg_printf_lock.Unlock(); + return ret; + } + char* getline() + { + char *ret = nullptr; + size_t stringSize = 0; + char ch = 0; + while((ch = getchar(false, false)) != '\n') + { + if (ch == EOF) + continue; + if (ch != '\b') + { + ret = (char*)allocators::g_kAllocator->ReAllocate(ret, ++stringSize + 1); + ret[stringSize - 1] = ch; + putchar(ch); + } + else + { + if (stringSize != 0) + { + ret = (char*)allocators::g_kAllocator->ReAllocate(ret, --stringSize); + putchar(ch); + } + else + continue; + } + if (ret) + ret[stringSize] = 0; + } + putchar('\n'); + return ret; + } +#else + char getchar(bool async, bool echo) + { + return EOF; + } + bool putchar(char ch, bool async) + { + return false; + } + size_t printf(const char* format, ...) + { + return 0; + } + char* getline() + { + return nullptr; + } +#endif + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/kdbg/io.h b/src/oboskrnl/arch/x86_64/kdbg/io.h new file mode 100644 index 00000000..fc9e0d32 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/kdbg/io.h @@ -0,0 +1,40 @@ +/* + oboskrnl/arch/x86_64/kdbg/io.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include +#include + +namespace obos +{ + namespace kdbg + { + enum class input_format + { + INVALID, + PS2_KEYBOARD, + SERIAL, + }; + enum class output_format + { + INVALID, + CONSOLE, + SERIAL, + }; + constexpr char EOF = -1; + extern input_format g_inputDev; + extern output_format g_outputDev; + extern bool g_echoKernelLogsToDbgConsole; + // Returns EOF when no character was inputted, and async is true. + char getchar(bool async = false, bool echo = true); + bool putchar(char ch, bool async = false); + + FORMAT(printf, 1) size_t printf(const char* format, ...); + char* getline(); + } + +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/kdbg/kdbg.cpp b/src/oboskrnl/arch/x86_64/kdbg/kdbg.cpp new file mode 100644 index 00000000..373df454 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/kdbg/kdbg.cpp @@ -0,0 +1,1256 @@ +/* + oboskrnl/arch/x86_64/kdbg/kdbg.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include + +#include + +#include + +#include + +#include + +#include + +#include +#include + +#include +#include + +#include + +#include + +#define TO_EHDR(obj) ((elf::Elf64_Ehdr*)obj) +#define TO_STRING_TABLE(ehdr, stable, off) ((const char*)ehdr + stable + off) + +namespace obos +{ + static kdbg::cpu_local_debugger_state s_dbgState; + extern volatile limine_kernel_file_request kernel_file; + void addr2sym(uintptr_t rip, const char** const symbolName, uintptr_t* symbolBase, uint8_t symType); + const elf::Elf64_Shdr* getSectionHeader(const elf::Elf64_Ehdr* ehdr, const char* name); + namespace arch + { + extern Irq g_ipiIrq; + bool LAPICTimerIRQChecker(const Irq*, const struct IrqVector*, void* _udata); + } + namespace scheduler + { + extern Irq g_schedulerIRQ; + } + namespace kdbg + { + debugger_state g_kdbgState; + bool g_echoKernelLogsToDbgConsole = false; +#if OBOS_KDBG_ENABLED + bp::bp(uintptr_t rip) + { + this->rip = rip; + addr2sym(rip, &funcInfo.name, &funcInfo.base, elf::STT_FUNC); + } + void bp::setStatus(bool to) + { + if (enabled == to) + return; // Nothing to do. + enabled = to; + // if (to) + // g_kdbgState.activeBreakpoints.append(this); + // else + // g_kdbgState.activeBreakpoints.remove(this->idx); + } + bp::~bp() + { + // if (g_kdbgState.activeBreakpoints.contains(this->idx)) + // g_kdbgState.activeBreakpoints.remove(this->idx); + // if (g_kdbgState.breakpoints.contains(this->idx)) + // g_kdbgState.breakpoints.remove(this->idx); + } + // bp_node* bp_list::getNode(size_t idx) + // { + // for (auto b = head; b; ) + // { + // if (b->data->idx == idx) + // return b; + // b = b->next; + // } + // return nullptr; + // } + // void bp_list::append(bp* _breakpoint) + // { + // bp_node *breakpoint = new bp_node{}; + // breakpoint->data = _breakpoint; + // if (tail) + // tail->next = breakpoint; + // if(!head) + // head = breakpoint; + // breakpoint->next = breakpoint; + // tail = breakpoint; + // _breakpoint->idx = nBreakpoints++; + // } + // void bp_list::remove(uint32_t idx) + // { + // bp_node *breakpoint = getNode(idx); + // if (!breakpoint) + // return; + // if (breakpoint->next) + // breakpoint->next->prev = breakpoint->prev; + // if (breakpoint->prev) + // breakpoint->prev->next = breakpoint->next; + // if (breakpoint == head) + // head = breakpoint->next; + // if (breakpoint == tail) + // tail = breakpoint->prev; + // nBreakpoints--; + // delete breakpoint; + // } + // bool bp_list::contains(size_t idx) + // { + // return getNode(idx) != nullptr; + // } + // bp& bp_list::operator[](size_t idx) + // { + // auto node = getNode(idx); + // if (!node) + // return *(bp*)nullptr; + // return *node->data; + // } + static bool isWhitespace(char ch) + { + return + ch == ' ' || + ch == '\t' || + ch == '\n' || + ch == '\v' || + ch == '\f' || + ch == '\r'; + } + static const char *help_message = + "Usage:\n" + "break [func]\n" + "\tSets a breakpoint at func, or at the current position if no argument is specified.\n" + "\tReturns the breakpoint number.\n" + "break_at addr\n" + "\tSets a breakpoint at addr.\n" + "\tReturns the breakpoint number.\n" + "delete breakpoint_idx\n" + "\tDeletes the breakpoint with the index specified.\n" + "\tReturns nothing on sucess.\n" + "list\n" + "\tLists breakpoints\n" + "\tReturns all the active breakpoints.\n" + "step\n" + "\tSteps one instruction.\n" + "\tReturns nothing.\n" + "finish\n" + "\tContinues until after the next ret instruction/\n" + "\tReturns nothing.\n" + "continue\n" + "\tContinues until an exception or breakpoint occurs.\n" + "\tReturns nothing.\n" + "x/hex addr count\n" + "\tPrints 'count' bytes at 'addr' as hexadecimal.\n" + "\tReturns the bytes.\n" + "x/dec addr count\n" + "\tPrints 'count' bytes at 'addr' as decimal.\n" + "\tReturns the bytes.\n" + "x/i count [addr]\n" + "\tDisassembles 'count' instructions at addr, or at rip if addr isn't specified.\n" + "\tReturns the instructions disassembled.\n" + "dreg\n" + "\tDumps all the registers and their values.\n" + "\tReturns the register's values and names.\n" + "print register\n" + "\tPrints a register as hexadecimal.\n" + "\tReturns the register's value.\n" + "set register=value\n" + "\tSets the value of a register.\n" + "\tReturns nothing.\n" + "wb,ww,wd,wq address=value\n" + "\tWrites a value in memory at a granularity of a byte, word, dword, or qword depending on the command overload.\n" + "\tReturns nothing.\n" + "where_addr\n" + "\tConverts an address in the kernel to it's respective symbol.\n" + "\tReturns the respective symbol name and address.\n" + "where\n" + "\tConverts a symbol to it's respective address.\n" + "\tReturns the symbol name's address in hexadecimal.\n" + // "watchdog seconds\n" + // "\tSets a watchdog timer to go off after 'seconds' if no debug event happens. This implicitly continues.\n" + // "\tReturns nothing.\n" + "stack_trace\n" + "\tPrints a stack trace.\n" + "\tReturns the stack trace.\n" + "ping\n" + "\tPrints pong.\n" + "\tReturns 'pong'\n" + "echo on/off\n" + "\tChanges whether kernel logs should be outputted on the debug console.\n" + "\tReturns nothing\n" + "echo ...\n" + "\tEchoes a message onto the debug console.\n" + "\tReturns the message\n" + "echo\n" + "\tPrints whether echoing kernel logs onto the debug console is enabled or not.\n" + "\tReturns whether echoing kernel logs onto the debug console is enabled or not\n" + "help\n" + "\tPrints this help message.\n"; + bool step(cpu_local_debugger_state& dbg_state) + { + // Step one instruction. + dbg_state.shouldStopAtNextInst = true; + dbg_state.isFinishingFunction = false; + dbg_state.context.frame.rflags |= RFLAGS_TRAP; + return false; + } + bool cont(cpu_local_debugger_state& dbg_state) + { + dbg_state.shouldStopAtNextInst = false; + dbg_state.isFinishingFunction = false; + dbg_state.context.frame.rflags &= ~RFLAGS_TRAP; + return false; + } + bool finish(cpu_local_debugger_state& dbg_state) + { + // Step one instruction. + dbg_state.shouldStopAtNextInst = false; + dbg_state.isFinishingFunction = true; + dbg_state.nCallsSinceFinishCommand = 1; + dbg_state.context.frame.rflags |= RFLAGS_TRAP; + return false; + } + bool dreg(cpu_local_debugger_state& dbg_state) + { + auto frame = &dbg_state.context.frame; + printf("Dumping registers:\n" + "\tRDI: 0x%016lx, RSI: 0x%016lx, RBP: 0x%016lx\n" + "\tRSP: 0x%016lx, RBX: 0x%016lx, RDX: 0x%016lx\n" + "\tRCX: 0x%016lx, RAX: 0x%016lx, RIP: 0x%016lx\n" + "\t R8: 0x%016lx, R9: 0x%016lx, R10: 0x%016lx\n" + "\tR11: 0x%016lx, R12: 0x%016lx, R13: 0x%016lx\n" + "\tR14: 0x%016lx, R15: 0x%016lx, RFL: 0x%016lx\n" + "\t SS: 0x%016lx, DS: 0x%016lx, CS: 0x%016lx\n" + "\tCR0: 0x%016lx, CR2: 0x%016lx, CR3: 0x%016lx\n" + "\tCR4: 0x%016lx, CR8: 0x%016lx, EFER: 0x%016lx\n" + "\tGS_BASE: 0x%016lx, FS_BASE: 0x%016lx\n", + frame->rdi, frame->rsi, frame->rbp, + frame->rsp, frame->rbx, frame->rdx, + frame->rcx, frame->rax, frame->rip, + frame->r8, frame->r9, frame->r10, + frame->r11, frame->r12, frame->r13, + frame->r14, frame->r15, frame->rflags, + frame->ss, frame->ds, frame->cs, + getCR0(), getCR2(), getCR3(), + getCR4(), dbg_state.context.irql, getEFER(), + dbg_state.context.gs_base, dbg_state.context.fs_base + ); + return true; + } + static bool isNumber(char ch) + { + char temp = ch - '0'; + return temp >= 0 && temp < 10; + } + static bool isHexNumber(char ch) + { + char temp = ch - '0'; + if (temp >= 0 && temp < 10) + return true; + temp = ch - 'A'; + if (temp >= 0 && temp < 6) + return true; + temp = ch - 'a'; + if (temp >= 0 && temp < 6) + return true; + return false; + } + static uint64_t dec2bin(const char* str, size_t size) + { + uint64_t ret = 0; + for (size_t i = 0; i < size; i++) + { + if ((str[i] - '0') < 0 || (str[i] - '0') > 9) + continue; + ret *= 10; + ret += str[i] - '0'; + } + return ret; + } + static uint64_t hex2bin(const char* str, size_t size) + { + uint64_t ret = 0; + str += *str == '\n'; + for (int i = size - 1, j = 0; i > -1; i--, j++) + { + char c = str[i]; + uintptr_t digit = 0; + switch (c) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + digit = c - '0'; + break; + case 'A': + case 'B': + case 'C': + case 'D': + case 'E': + case 'F': + digit = (c - 'A') + 10; + break; + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + digit = (c - 'a') + 10; + break; + default: + break; + } + ret |= digit << (j * 4); + } + return ret; + } + static uint64_t oct2bin(const char* str, size_t size) + { + uint64_t n = 0; + const char* c = str; + while (size-- > 0) + n = n * 8 + (uint64_t)(*c++ - '0'); + return n; + } + static uint64_t strtoull(const char* str, char** endptr, int base) + { + for (; !isNumber(*str) && *str; str++); + if (!(*str)) + return UINT64_MAX; + if (!base) + { + base = 10; + if ((*(str + 1) == 'x' || *(str + 1) == 'X') && *str == '0') + { + base = 16; + str += 2; + } + else if (*str == '0') + { + base = 8; + str++; + } + } + size_t sz = 0; + for (; base == 16 ? isHexNumber(*str) : isNumber(*str) && *str; sz++, str++); + str -= sz; + if (!(*str)) + return UINT64_MAX; + if (endptr) + *endptr = (char*)str + sz; + switch (base) + { + case 10: + return dec2bin(str, sz); + case 16: + return hex2bin(str, sz); + case 8: + return oct2bin(str, sz); + default: + break; + } + return 0xffff'ffff'ffff'ffff; + } + bool examine_memory(bool asHexadecimal, void* at, size_t nBytes) + { + uint8_t* buf = (uint8_t*)at; + vmm::page_descriptor pd; + memzero(&pd, sizeof(pd) ); + printf("Dumping %lu bytes at address 0x%p as %s.\n", nBytes, at, asHexadecimal ? "hexadecimal" : "decimal"); + size_t lastPdCheckIndex = (size_t)-4096; + size_t nBytesPrintedOnLine = 0; + if (asHexadecimal) + printf("00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n"); + else + printf(" 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15\n"); + for (size_t i = 0; i < nBytes; i++) + { + if ((lastPdCheckIndex + 4096) == i) + { + lastPdCheckIndex = i; + OBOS_ASSERTP(vmm::GetPageDescriptor(&vmm::g_kernelContext, (void*)((uintptr_t)(buf + i) & ~0xfff), 4096, &pd, 1) != SIZE_MAX, "Could not retrieve page descriptors for page %p.",, buf); + } + if (!pd.present) + { + printf("%s ", asHexadecimal ? "??" : "???"); + continue; + } + if (nBytesPrintedOnLine++ == 16) + printf("\n"); + if (!asHexadecimal) + printf("%3d ", buf[i]); + else + printf("%02x ", buf[i]); + } + printf("\n"); + return true; + } + bool write_memory(void* at, uintptr_t what, uint8_t granularity) + { + vmm::page_descriptor pd[2]; + memzero(pd, sizeof(pd)); + OBOS_ASSERTP(vmm::GetPageDescriptor(&vmm::g_kernelContext, at, granularity, pd, 2) != SIZE_MAX, "Could not retrieve page descriptors for page %p.",, at); + if (!pd[0].present) + { + printf("Cannot write %d bytes at 0x%p\n", granularity, at); + return true; + } + if (OBOS_CROSSES_PAGE_BOUNDARY(at, granularity) && !pd[1].present) + { + printf("Cannot write %d bytes at 0x%p\n", granularity, at); + return true; + } + switch(granularity) + { + case 1: *( uint8_t*)at = (what & 0xff); break; + case 2: *(uint16_t*)at = (what & 0xffff); break; + case 4: *(uint32_t*)at = (what & 0xffff'ffff); break; + case 8: *(uint64_t*)at = what; break; + default: return true; + }; + return true; + } + uintptr_t& getRegister(cpu_local_debugger_state& dbg_state, const char* regName) + { + auto frame = &dbg_state.context.frame; + uintptr_t *regVal = nullptr; + if (strcmp(regName, "rbp")) regVal = &frame->rbp; + else if ( strcmp(regName, "r8")) regVal = &frame->r8; + else if ( strcmp(regName, "r9")) regVal = &frame->r9; + else if (strcmp(regName, "r10")) regVal = &frame->r10; + else if (strcmp(regName, "r11")) regVal = &frame->r11; + else if (strcmp(regName, "r12")) regVal = &frame->r12; + else if (strcmp(regName, "r13")) regVal = &frame->r13; + else if (strcmp(regName, "r14")) regVal = &frame->r14; + else if (strcmp(regName, "r15")) regVal = &frame->r15; + else if (strcmp(regName, "rdi")) regVal = &frame->rdi; + else if (strcmp(regName, "rsi")) regVal = &frame->rsi; + else if (strcmp(regName, "rbx")) regVal = &frame->rbx; + else if (strcmp(regName, "rdx")) regVal = &frame->rdx; + else if (strcmp(regName, "rcx")) regVal = &frame->rcx; + else if (strcmp(regName, "rax")) regVal = &frame->rax; + else if (strcmp(regName, "rip")) regVal = &frame->rip; + else if (strcmp(regName, "rsp")) regVal = &frame->rsp; + else if ( strcmp(regName, "ss")) regVal = &frame->ss; + else if ( strcmp(regName, "ds")) regVal = &frame->ds; + else if ( strcmp(regName, "cs")) regVal = &frame->cs; + else if (strcmp(regName, "rflags")) regVal = &frame->rflags; + else if (strcmp(regName, "irql") || strcmp(regName, "cr8")) regVal = &dbg_state.context.irql; + else if (strcmp(regName, "cr3")) regVal = (uintptr_t*)dbg_state.context.pm; + else if (strcmp(regName, "gs_base")) regVal = &dbg_state.context.gs_base; + else if (strcmp(regName, "fs_base")) regVal = &dbg_state.context.fs_base; + return *regVal; + } + bool print_reg(cpu_local_debugger_state& dbg_state, const char* regName) + { + uintptr_t tmp = 0; + uintptr_t* regVal = &getRegister(dbg_state, regName); + if (!regVal) + { + // Rather a special register or an invalid register. + if (strcmp(regName, "cr0")) { tmp = getCR0(); } + else if (strcmp(regName, "cr2")) { tmp = getCR2(); } + else if (strcmp(regName, "cr4")) { tmp = getCR4(); } + else + return true; + regVal = &tmp; + } + printf("%s=0x%016lx", regName, *regVal); + if (strcmp(regName, "rip")) + { + const char* fName = nullptr; + uintptr_t fBase = 0; + addr2sym(*regVal, &fName, &fBase, elf::STT_FUNC); + printf(" (%s+%ld)\n", fName ? fName : "External code", fBase ? *regVal-fBase : 0); + } + else + printf("\n"); + return true; + } + bool write_reg(cpu_local_debugger_state& dbg_state, const char* regName, uintptr_t val) + { + uintptr_t* regVal = &getRegister(dbg_state, regName); + if (!regVal) + { + // Rather a special register or an invalid register. + if (strcmp(regName, "cr0")) asm volatile("mov %0, %%cr0;" : :"r"(val) :); + else if (strcmp(regName, "cr2")) asm volatile("mov %0, %%cr2;" : :"r"(val) :); + else if (strcmp(regName, "cr4")) asm volatile("mov %0, %%cr4;" : :"r"(val) :); + return true; + } + *regVal = val; + return true; + } + void set_drn_on_cpu(scheduler::cpu_local* cpu, uint8_t dbgIdx, uint64_t value) + { + // Setup the IPI. + arch::ipi* drIPI = new arch::ipi{}; + arch::dbg_reg_ipi* payload = new arch::dbg_reg_ipi{}; + drIPI->data.dbg_reg = payload; + drIPI->type = arch::ipi::IPI_DEBUG_REGISTER; + payload->regIdx = dbgIdx; + payload->val = new uint64_t{ value }; + payload->rw = true /* write */; + // Add the IPI to the queue and call it. + cpu->archSpecific.ipi_queue.push(drIPI); + uint8_t oldIRQL = IRQL_INVALID; + if (cpu == scheduler::GetCPUPtr() && GetIRQL() >= IRQL_IPI_DISPATCH) + { + // We need to temporarily lower the IRQL for self-ipis, as otherwise we would deadlock while waiting. + oldIRQL = GetIRQL(); + LowerIRQL(IRQL_DISPATCH); + } + LAPIC_SendIPI(DestinationShorthand::None, DeliveryMode::Fixed, arch::g_ipiIrq.GetVector() + 0x20, cpu->cpuId); + // Wait for the IPI to be processed. + // We do this as we need to free the structures, and we don't want to cause a race condition. + while(!drIPI->processed) + pause(); + if (oldIRQL != IRQL_INVALID) + { + uint8_t _oldIRQL = 0; + RaiseIRQL(oldIRQL, &_oldIRQL); + } + // After the IPI has been processed, we must clean up after ourselves. + // The IPI isn't in the queue anymore, so we just have to free the payload and ipi object. + delete drIPI; + delete payload->val; + delete payload; + } + uint64_t get_drn_on_cpu(scheduler::cpu_local* cpu, uint8_t dbgIdx) + { + arch::ipi* drIPI = new arch::ipi{}; + arch::dbg_reg_ipi* payload = new arch::dbg_reg_ipi{}; + drIPI->data.dbg_reg = payload; + drIPI->type = arch::ipi::IPI_DEBUG_REGISTER; + payload->regIdx = dbgIdx; + payload->val = new uint64_t{ 0 }; + payload->rw = false /* write */; + // Add the IPI to the queue and call it. + cpu->archSpecific.ipi_queue.push(drIPI); + uint8_t oldIRQL = IRQL_INVALID; + if (cpu == scheduler::GetCPUPtr() && GetIRQL() >= IRQL_IPI_DISPATCH) + { + // We need to temporarily lower the IRQL for self-ipis, as otherwise we would deadlock while waiting. + oldIRQL = GetIRQL(); + LowerIRQL(IRQL_DISPATCH); + } + LAPIC_SendIPI(DestinationShorthand::None, DeliveryMode::Fixed, arch::g_ipiIrq.GetVector() + 0x20, cpu->cpuId); + // Wait for the IPI to be processed. + // We do this as we need to free the structures, and we don't want to cause a race condition. + while(!drIPI->processed) + pause(); + if (oldIRQL != IRQL_INVALID) + { + uint8_t _oldIRQL = 0; + RaiseIRQL(oldIRQL, &_oldIRQL); + } + // After the IPI has been processed, we must clean up after ourselves. + // The IPI isn't in the queue anymore, so we just have to free the payload and ipi object. + uint64_t val = *payload->val; + delete drIPI; + delete payload->val; + delete payload; + return val; + } + void setupDRsForBreakpoint(bp* _breakpoint) + { + switch (_breakpoint->idx) + { + case 0: asm volatile("mov %0, %%dr0" : : "r"(_breakpoint->rip) : ); break; + case 1: asm volatile("mov %0, %%dr1" : : "r"(_breakpoint->rip) : ); break; + case 2: asm volatile("mov %0, %%dr2" : : "r"(_breakpoint->rip) : ); break; + case 3: asm volatile("mov %0, %%dr3" : : "r"(_breakpoint->rip) : ); break; + default: break; + } + uintptr_t dr7 = 0; + asm volatile("mov %%dr7, %0" :"=r"(dr7) ::); + dr7 |= (1 << (_breakpoint->idx * 2 + 1)); + asm volatile("mov %0, %%dr7" ::"r"(dr7) : ); + for (size_t i = 0; i < scheduler::g_nCPUs; i++) + { + auto cpu = &scheduler::g_cpuInfo[i]; + if (cpu == scheduler::GetCPUPtr()) + continue; + if (!cpu->initialized) + continue; + set_drn_on_cpu(cpu, _breakpoint->idx, _breakpoint->rip); + dr7 = get_drn_on_cpu(cpu, 7); + dr7 |= (1 << (_breakpoint->idx * 2 + 1)); + set_drn_on_cpu(cpu, 7, dr7); + } + } + bool set_breakpoint(uintptr_t rip) + { + if (g_kdbgState.nBreakpointsInUse == 4) + { + printf("Breakpoint limit of four breakpoints has been hit.\n"); + return true; + } + auto _breakpoint = new bp{ rip }; + _breakpoint->idx = g_kdbgState.nextBpIndex; + g_kdbgState.breakpoints[_breakpoint->idx] = _breakpoint; + if (!g_kdbgState.breakpoints[g_kdbgState.nextBpIndex + 1]) + g_kdbgState.nextBpIndex++; + else + { + g_kdbgState.nextBpIndex = 4; + for (size_t i = 0; i < g_kdbgState.nBreakpointsInUse; i++) + { + if (!g_kdbgState.breakpoints[i]) + { + g_kdbgState.nextBpIndex = i; + break; + } + } + } + // Set the appropriate debug registers. + _breakpoint->awaitingSmpRefresh = arch::g_initializedAllCPUs; + setupDRsForBreakpoint(_breakpoint); + printf("Created breakpoint %d at rip=0x%p (%s+%ld).\n", _breakpoint->idx, (void*)rip, _breakpoint->funcInfo.name, rip-_breakpoint->funcInfo.base); + return true; + } + bool delete_breakpoint(size_t idx) + { + if (idx > 4) + return true; + uintptr_t dr7 = 0; + asm volatile("mov %%dr7, %0" :"=r"(dr7) ::); + dr7 &= ~(1<<(idx*2+1)); + asm volatile("mov %0, %%dr7" ::"r"(dr7):); + delete g_kdbgState.breakpoints[idx]; + g_kdbgState.breakpoints[idx] = nullptr; + g_kdbgState.nextBpIndex = idx; + return true; + } + bool list_breakpoints() + { + for (size_t i = 0; i < 4; i++) + { + if (!g_kdbgState.breakpoints[i]) + continue; + printf("Breakpoint %d: 0x%p (%s+%ld)\nHit %d times\n", + g_kdbgState.breakpoints[i]->idx, + (void*)g_kdbgState.breakpoints[i]->rip, + g_kdbgState.breakpoints[i]->funcInfo.name, g_kdbgState.breakpoints[i]->rip-g_kdbgState.breakpoints[i]->funcInfo.base, + g_kdbgState.breakpoints[i]->hitCount + ); + // Example output: + // Breakpoint 0: 0xffffffff80000000 (KernelArchInit+0) + // Hit 0 times. + } + return true; + } + bool where_addr(uintptr_t addr) + { + const char *symName = nullptr; + uintptr_t symBase = 0; + addr2sym(addr, &symName, &symBase, elf::STT_NOTYPE); + printf("0x%p: %s+%ld\n", (void*)addr, symName ? symName : "External code", symBase ? addr-symBase : 0); + return true; + } + uintptr_t symToAddr(const char* symName, size_t* symSz, uint8_t symType) + { + uintptr_t base = (uintptr_t)kernel_file.response->kernel_file->address; + elf::Elf64_Ehdr* ehdr = (elf::Elf64_Ehdr*)base; + size_t stable = 0; + if (!getSectionHeader(ehdr, ".strtab")) + return UINTPTR_MAX; + stable = getSectionHeader(ehdr, ".strtab")->sh_offset; + const elf::Elf64_Shdr* symtab = getSectionHeader(ehdr, ".symtab"); + if (!symtab) + return UINTPTR_MAX; + size_t nEntries = symtab->sh_size / symtab->sh_entsize; + for (size_t i = 0; i < nEntries; i++) + { + elf::Elf64_Sym* symbol = (elf::Elf64_Sym*)(base + symtab->sh_offset + i*symtab->sh_entsize); + if (symType != elf::STT_NOTYPE) + if ((symbol->st_info & 0xf) != symType) + continue; + if (strcmp(TO_STRING_TABLE(ehdr, stable, symbol->st_name), symName)) + { + if (symSz) + *symSz = symbol->st_size; + return symbol->st_value; + } + } + return UINTPTR_MAX; + } + bool where(const char* cmdline) + { + char* symName = nullptr; + size_t symNameSz = 0; + for(; !isWhitespace(cmdline[symNameSz]) && cmdline[symNameSz] != 0; symNameSz++); + symName = new char[symNameSz + 1]; + memcpy(symName, cmdline, symNameSz); + symName[symNameSz] = 0; + size_t symSz = 0; + uintptr_t addr = symToAddr(symName, &symSz, elf::STT_NOTYPE); + delete[] symName; + printf("%s is at 0x%p and ends at 0x%p.\n", symName, (void*)addr, (void*)(addr+symSz)); + return true; + } + bool _break(cpu_local_debugger_state& dbg_state, const char* cmdline) + { + if (!(*cmdline)) + return set_breakpoint(dbg_state.context.frame.rip); + char* symName = nullptr; + size_t symNameSz = 0; + for(; !isWhitespace(cmdline[symNameSz]) && cmdline[symNameSz] != 0; symNameSz++); + symName = new char[symNameSz + 1]; + memcpy(symName, cmdline, symNameSz); + symName[symNameSz] = 0; + size_t symSz = 0; + uintptr_t addr = symToAddr(symName, &symSz, elf::STT_NOTYPE); + delete[] symName; + return set_breakpoint(addr); + } + bool stack_trace(cpu_local_debugger_state& dbg_state) + { + const char* fName = nullptr; + uintptr_t fBase = 0; + addr2sym(dbg_state.context.frame.rip, &fName, &fBase, elf::STT_FUNC); + printf("Stack trace:\n"); + printf("\t0x%016lx: %s+%ld\n", dbg_state.context.frame.rip, fName ? fName : "External code", fBase ? dbg_state.context.frame.rip - fBase : 0); + logger::stackTrace((void*)dbg_state.context.frame.rbp, "\t", printf); + return true; + } + bool dbg_terminal(); + // TODO: Fix + // void watchdog_irq_handler(const Irq*, const IrqVector*, void* udata, interrupt_frame* frame) + // { + // uint64_t *userdata = (uint64_t*)udata; + // const uint64_t& nSeconds = userdata[0]; + // uint64_t& nSecondsPassed = userdata[1]; + // cpu_local_debugger_state& dbg_state = scheduler::GetCPUPtr()->archSpecific.debugger_state; + // dbg_state.context.frame = *frame; + // if (++nSecondsPassed >= nSeconds) + // { + // // The watchdog timer went off! + // uint8_t oldIRQL = 0; + // RaiseIRQL(0xf, &oldIRQL); + // printf("Watchdog timer went off!\nCalling debug terminal...\n"); + // bool ret = dbg_terminal(); + // *frame = dbg_state.context.frame; + // LowerIRQL(oldIRQL); + // return; + // } + // } + // bool set_watchdog(cpu_local_debugger_state& dbg_state, uint64_t nSeconds) + // { + // if (!g_kdbgState.watchdogIRQInitialized) + // { + // new (&g_kdbgState.watchdogIRQ) Irq{ scheduler::g_schedulerIRQ.GetVector(), false, false }; + // g_kdbgState.watchdogIRQInitialized = true; + // } + // constexpr uint64_t freqHz = 1; + // if (g_kdbgState.watchdogIRQ.GetHandlerUserdata()) + // delete[] (uint64_t*)g_kdbgState.watchdogIRQ.GetHandlerUserdata(); + // if (g_kdbgState.watchdogIRQ.GetIrqCheckerUserdata()) + // delete[] (uint64_t*)g_kdbgState.watchdogIRQ.GetIrqCheckerUserdata(); + // auto udata = new uint64_t[3]; + // arch::g_hpetAddress->generalConfig &= ~(1<<0); + // udata[0] = freqHz; + // udata[1] = arch::g_hpetAddress->mainCounterValue; + // udata[2] = udata[1] + (arch::g_hpetFrequency/freqHz); + // arch::g_hpetAddress->generalConfig |= (1<<0); + // g_kdbgState.watchdogIRQ.SetIRQChecker(arch::LAPICTimerIRQChecker, udata); + // udata = new uint64_t[2]; + // udata[0] = nSeconds; + // udata[1] = 0; + // g_kdbgState.watchdogIRQ.SetHandler(watchdog_irq_handler, udata); + // dbg_state.shouldStopAtNextInst = false; + // dbg_state.isFinishingFunction = false; + // return false; + // } + bool disasm(void* at, size_t nInstructions); + struct colour_changer + { + colour_changer() = default; + colour_changer(Pixel fore) + { + g_kernelConsole.GetColour(oldForeground, oldBackground); + g_kernelConsole.SetColour(fore, oldBackground); + } + ~colour_changer() + { + g_kernelConsole.SetColour(oldForeground, oldBackground); + } + Pixel oldForeground, oldBackground; + }; + struct unique_ptr + { + unique_ptr() = delete; + unique_ptr(colour_changer* ptr) + :obj{ptr} + {} + ~unique_ptr() + { + if (obj) + delete obj; + } + colour_changer* obj; + }; + bool dbg_terminal() + { + colour_changer *c; + if (g_outputDev == output_format::CONSOLE) + c = new colour_changer{ logger::GREY }; + else + c = new colour_changer{}; + unique_ptr pC = c; + cpu_local_debugger_state& dbg_state = scheduler::GetCPUPtr() ? scheduler::GetCPUPtr()->archSpecific.debugger_state : s_dbgState; + bool shouldRun = true; + while(shouldRun) + { + printf("> "); + char* buf = getline(); + if (!buf) + { + putchar('\n'); + continue; + } + char* input = buf; + for(; isWhitespace(*input); input++); + size_t szInput = strlen(input); + if (input[szInput - 1] == '\r') + input[szInput - 1] = 0; + char* command = input; + if (!*command) + continue; + size_t commandSz = 0; + for(; !isWhitespace(command[commandSz]) && command[commandSz] != 0; commandSz++); + command = new char[commandSz + 1]; + memcpy(command, input, commandSz); + command[commandSz] = 0; + char* cmdline = input + commandSz; + for(; isWhitespace(*cmdline) && *cmdline; cmdline++); + if (strcmp(command, "help")) + printf("%s", help_message); + else if (strcmp(command, "ping")) + printf("pong\n"); + else if (strcmp(command, "step")) + shouldRun = step(dbg_state); + else if (strcmp(command, "finish")) + shouldRun = finish(dbg_state); + else if (strcmp(command, "continue")) + shouldRun = cont(dbg_state); + else if (strcmp(command, "dreg")) + shouldRun = dreg(dbg_state); + else if (strcmp(command, "list")) + shouldRun = list_breakpoints(); + else if (strcmp(command, "break")) + shouldRun = _break(dbg_state, cmdline); + else if (strcmp(command, "stack_trace")) + shouldRun = stack_trace(dbg_state); + else if (strcmp(command, "echo")) + { + if (!(*cmdline)) + { + printf("Echo is %s\n", g_echoKernelLogsToDbgConsole ? "on" : "off"); + goto end; + } + if (strcmp(cmdline, "on")) + g_echoKernelLogsToDbgConsole = true; + else if (strcmp(cmdline, "off")) + g_echoKernelLogsToDbgConsole = false; + else + printf("%s\n", cmdline); + } + else if (strcmp(command, "print")) + { + if (!(*cmdline)) + { + printf("Insufficient parameters to %s.\n", command); + goto end; + } + shouldRun = print_reg(dbg_state, cmdline); + } + // else if (strcmp(command, "watchdog")) + // { + // if (!(*cmdline)) + // { + // printf("Insufficient parameters to %s.\n", command); + // goto end; + // } + // shouldRun = set_watchdog(dbg_state, strtoull(cmdline, nullptr, 0)); + // } + else if (strcmp(command, "break_at")) + { + if (!(*cmdline)) + { + printf("Insufficient parameters to %s.\n", command); + goto end; + } + + shouldRun = set_breakpoint(strtoull(cmdline, nullptr, 0)); + } + else if (strcmp(command, "where_addr")) + { + if (!(*cmdline)) + { + printf("Insufficient parameters to %s.\n", command); + goto end; + } + shouldRun = where_addr(strtoull(cmdline, nullptr, 0)); + } + else if (strcmp(command, "delete")) + { + if (!(*cmdline)) + { + printf("Insufficient parameters to %s.\n", command); + goto end; + } + shouldRun = delete_breakpoint(strtoull(cmdline, nullptr, 0)); + } + else if (strcmp(command, "where")) + { + if (!(*cmdline)) + { + printf("Insufficient parameters to %s.\n", command); + goto end; + } + shouldRun = where(cmdline); + } + else if (strcmp(command, "x/hex")) + { + if (!(*cmdline)) + { + printf("Insufficient parameters to %s.\n", command); + goto end; + } + void* at = nullptr; + size_t nBytes = 0; + char* cmdline = input + commandSz + 1; + char* endptr = nullptr; + at = (void*)strtoull(cmdline, &endptr, 0); + if (!(*endptr)) + { + printf("Insufficient parameters to %s.\n", command); + goto end; + } + endptr++; + if (!(*endptr)) + { + printf("Insufficient parameters to %s.\n", command); + goto end; + } + nBytes = strtoull(endptr, nullptr, 0); + shouldRun = examine_memory(true, at, nBytes); + } + else if (strcmp(command, "x/dec")) + { + if (!(*cmdline)) + { + printf("Insufficient parameters to %s.\n", command); + goto end; + } + void* at = nullptr; + size_t nBytes = 0; + char* cmdline = input + commandSz + 1; + if (!(*cmdline)) + { + printf("Insufficient parameters to %s.\n", command); + goto end; + } + char* endptr = nullptr; + at = (void*)strtoull(cmdline, &endptr, 0); + if (!(*endptr)) + { + printf("Insufficient parameters to %s.\n", command); + goto end; + } + endptr++; + if (!(*endptr)) + { + printf("Insufficient parameters to %s.\n", command); + goto end; + } + nBytes = strtoull(endptr, nullptr, 0); + shouldRun = examine_memory(false, at, nBytes); + } + else if (strcmp(command, "x/i")) + { + if (!(*cmdline)) + { + printf("Insufficient parameters to %s.\n", command); + goto end; + } + void* at = nullptr; + size_t nInstructions = 0; + char* cmdline = input + commandSz + 1; + if (!(*cmdline)) + { + printf("Insufficient parameters to %s.\n", command); + goto end; + } + char* endptr = nullptr; + nInstructions = strtoull(cmdline, &endptr, 0); + if (!(*endptr)) + { + at = (void*)dbg_state.context.frame.rip; + goto disassemble; + } + endptr++; + at = (void*)strtoull(endptr, nullptr, 0); + disassemble: + shouldRun = disasm(at, nInstructions); + } + else if (strcmp(command, "set")) + { + if (!(*cmdline)) + { + printf("Insufficient parameters to %s.\n", command); + goto end; + } + size_t szPar1 = 0; + for(; cmdline[szPar1] != '=' && cmdline[szPar1] != 0; szPar1++); + if (!cmdline[szPar1]) + { + printf("Insufficient parameters to %s.\n", command); + goto end; + } + uintptr_t val = strtoull(cmdline + szPar1 + 1, nullptr, 0); + char* regName = (char*)memcpy(new char[szPar1 + 1], cmdline, szPar1); + regName[szPar1] = 0; + shouldRun = write_reg(dbg_state, regName, val); + delete[] regName; + } + else if (strcmp(command, "wb") || + strcmp(command, "ww") || + strcmp(command, "wd") || + strcmp(command, "wq") + ) + { + uint8_t gran = 0; + switch (command[1]) + { + case 'b': gran = 1; break; + case 'w': gran = 2; break; + case 'd': gran = 4; break; + case 'q': gran = 8; break; + default: break; + } + if (!(*cmdline)) + { + printf("Insufficient parameters to %s.\n", command); + goto end; + } + void* at = (void*)strtoull(cmdline, &cmdline, 0); + if (!(*cmdline)) + { + printf("Insufficient parameters to %s.\n", command); + goto end; + } + uintptr_t val = strtoull(cmdline, nullptr, 0); + shouldRun = write_memory(at, val, gran); + } + else + printf("Invalid command '%s'.\nUse 'help' for a list of valid commands.\n", command); + end: + delete[] command; + allocators::g_kAllocator->Free(buf, allocators::g_kAllocator->QueryObjectSize(buf)); + } + return false; + } + bool processDebugException(interrupt_frame* frame, bool isBpInstruction) + { + if (!g_initialized) + return false; // We shouldn't be handling anything. + cpu_local_debugger_state& dbg_state = scheduler::GetCPUPtr() ? scheduler::GetCPUPtr()->archSpecific.debugger_state : s_dbgState; + dbg_state.context.frame = *frame; + if (isBpInstruction) + { + const char* funcName = nullptr; + uintptr_t funcBase = 0; + addr2sym(frame->rip, &funcName, &funcBase, elf::STT_FUNC); + printf("Trap instruction into kernel debugger at rip %p (%s+%d).\n", frame->rip, funcName ? funcName : "External Code", funcBase ? frame->rip-funcBase : funcBase); + bool ret = dbg_terminal(); + *frame = dbg_state.context.frame; + return ret; + } + uint64_t dr6 = getDR6(); + uint64_t dr6_bitmask = (1<<14)|(1<<0)|(1<<1)|(1<<2)|(1<<3); + if (!(dr6 & dr6_bitmask)) + return true; // This debug exception isn't ours, as it didn't occur as a result of the trap flag or of any breakpoints. + if (dr6 & (1<<14)) + { + if (dbg_state.shouldStopAtNextInst) + { + dbg_state.shouldStopAtNextInst = false; + printf("Opening debug terminal...\n"); + bool ret = dbg_terminal(); + *frame = dbg_state.context.frame; + return ret; + } + if (dbg_state.isFinishingFunction) + { + // Our mini disassembler. + constexpr uint8_t NEAR_RETURN1 = 0xc3; + constexpr uint8_t NEAR_RETURN2 = 0xc2; + constexpr uint8_t FAR_RETURN1 = 0xcb; + constexpr uint8_t FAR_RETURN2 = 0xca; + constexpr uint8_t CALL_REL = 0xe8; + constexpr uint8_t CALL_ABS = 0xff; + constexpr uint8_t CALL_FAR = 0x9a; + constexpr uint8_t REX_W = 0x48; + uint8_t instruction = *(uint8_t*)frame->rip; + if (instruction == REX_W) + instruction = *((uint8_t*)frame->rip + 1); + switch(instruction) + { + case NEAR_RETURN1: + case NEAR_RETURN2: + case FAR_RETURN1: + case FAR_RETURN2: + { + if (!(--dbg_state.nCallsSinceFinishCommand)) + { + dbg_state.shouldStopAtNextInst = true; + dbg_state.isFinishingFunction = false; + } + return false; + } + case CALL_REL: + case CALL_ABS: + case CALL_FAR: + { + dbg_state.nCallsSinceFinishCommand++; + return false; + } + } + } + } + else + { + // We hit a breakpoint... + // Find the breakpoint(s) that were hit, and issue a message for each of them. + // Then, call the debugger terminal. + dr6 &= 0b01111; + dr6 |= 0b10000; + for (uint32_t idx = __builtin_ctzll(dr6); dr6 != 0b10000; ) + { + if (!g_kdbgState.breakpoints[idx]) + goto end; + { + const char* funcName = nullptr; + uintptr_t funcBase = 0; + addr2sym(frame->rip, &funcName, &funcBase, elf::STT_FUNC); + printf("Hit breakpoint %d at rip 0x%p (%s+%d).\n", idx, (void*)frame->rip, funcName ? funcName : "External Code", funcBase ? frame->rip-funcBase : funcBase); + g_kdbgState.breakpoints[idx]->hitCount++; + } + end: + dr6 &= ~(1<archSpecific.debugger_state : s_dbgState; + dbg_state.context.frame = *frame; + dbg_state.context.irql = oldIRQL; + dbg_state.context.gs_base = rdmsr(0xC0000101); + dbg_state.context.fs_base = rdmsr(0xC0000100); + const char* exception_messages[32] = { + "Division Error", + "Debug", + "Non-maskable Interrupt", + "Breakpoint", + "Overflow", + "Bound Range Exceeded", + "Invalid Opcode", + "Device Not Available", + "Double Fault", + "Coprocessor Segment Overrun", + "Invalid TSS", + "Segment Not Present", + "Stack-Segment Fault", + "General Protection Fault", + "Page Fault", + "Reserved", + "x87 Floating-Point Exception", + "Alignment Check", + "Machine Check", + "SIMD Floating-Point Exception", + "Virtualization Exception", + "Control Protection Exception" + "Reserved", + "Reserved", + "Reserved", + "Reserved", + "Reserved", + "Hypervisor Injection Exception", + "VMM Communication Exception", + "Security Exception", + "Reserved", + }; + if (frame->intNumber == 3 || frame->intNumber == 1) + { + bool ret = processDebugException(frame, frame->intNumber == 3); + LowerIRQL(oldIRQL); + return ret; + } + const char* funcName = nullptr; + uintptr_t funcBase = 0; + addr2sym(frame->rip, &funcName, &funcBase, elf::STT_FUNC); + printf("** EXCEPTION **\n" + "%s (%ld) exception occurred at rip 0x%p (%s+%d).\n" + "Opening debug terminal...\n", + exception_messages[frame->intNumber], frame->intNumber, + (void*)frame->rip, funcName ? funcName : "External Code", funcBase ? frame->rip-funcBase : funcBase); + bool ret = dbg_terminal(); + *frame = dbg_state.context.frame; + LowerIRQL(oldIRQL); + return ret; + } +#else + bool exceptionHandler(interrupt_frame* ) + { return false; } +#endif + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/lapic.c b/src/oboskrnl/arch/x86_64/lapic.c deleted file mode 100644 index fa78db11..00000000 --- a/src/oboskrnl/arch/x86_64/lapic.c +++ /dev/null @@ -1,192 +0,0 @@ -/* - * oboskrnl/arch/x86_64/lapic.c - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include -#include - -#include -#include -#include - -#include - -#include - -#include - -#define IA32_APIC_BASE 0x1B -#define APIC_BSP 0x100 -#define APIC_ENABLE 0x800 -#define IA32_X2APIC_REGISTERS 0x800 - -obos_status Arch_MapPage(uintptr_t cr3, void* at_, uintptr_t phys, uintptr_t flags, bool e); - -lapic* Arch_LAPICAddress; -static bool has_x2apic; -static basicmm_region lapic_region; -static void LAPIC_DefaultIrqHandler(interrupt_frame* frame) -{ - OBOS_UNUSED(frame); - Arch_LAPICSendEOI(); -} -OBOS_PAGEABLE_FUNCTION void Arch_LAPICInitialize(bool isBSP) -{ - if (isBSP) - { - uint32_t ecx = 0; - __cpuid__(1, 0, nullptr, nullptr, &ecx, nullptr); - has_x2apic = !!(ecx & BIT(21)); - } - - if (has_x2apic) - { - uintptr_t lapic_msr = rdmsr(IA32_APIC_BASE); - lapic_msr |= APIC_ENABLE; - lapic_msr |= BIT(10); - if (isBSP) - { - lapic_msr |= APIC_BSP; - Arch_RawRegisterInterrupt(0xfe, (uintptr_t)LAPIC_DefaultIrqHandler); - } - wrmsr(IA32_APIC_BASE, lapic_msr); - - wrmsr(IA32_X2APIC_REGISTERS + 0x0F, 0x1ff); - wrmsr(IA32_X2APIC_REGISTERS + 0x35, (isBSP ? 0x700:0) | 0xfe); - wrmsr(IA32_X2APIC_REGISTERS + 0x36, isBSP ? 0x400 /* NMI, Unmasked */ : 0xfe); - // wrmsr(IA32_X2APIC_REGISTERS + 0x2f, 0x200FE); - wrmsr(IA32_X2APIC_REGISTERS + 0x34, 0xfe); - wrmsr(IA32_X2APIC_REGISTERS + 0x33, 0xfe); - wrmsr(IA32_X2APIC_REGISTERS + 0x32, 0xfe); - - return; - } - - uintptr_t lapic_msr = rdmsr(IA32_APIC_BASE); - if (!Arch_LAPICAddress) - { - uintptr_t phys = lapic_msr & ~0xfff; - Arch_LAPICAddress = (lapic*)(0xffffffffffffe000); - Arch_MapPage(getCR3(), Arch_LAPICAddress, phys, 0x8000000000000013, false); - lapic_region.mmioRange = true; - OBOSH_BasicMMAddRegion(&lapic_region, Arch_LAPICAddress, 0x1000); - } - lapic_msr |= APIC_ENABLE; - if (isBSP) - lapic_msr |= APIC_BSP; - wrmsr(IA32_APIC_BASE, lapic_msr); - if (isBSP) - Arch_RawRegisterInterrupt(0xfe, (uintptr_t)LAPIC_DefaultIrqHandler); - Arch_LAPICAddress->spuriousInterruptVector = 0x1ff /* LAPIC Enabled, spurious vector 0xff */; - Arch_LAPICAddress->lvtLINT0 = 0xfe /* Vector 0xFE, Fixed, Unmasked */; - if (isBSP) - Arch_LAPICAddress->lvtLINT0 |= 0x700; - Arch_LAPICAddress->lvtLINT1 = isBSP ? 0x400 /* NMI, Unmasked */ : 0xfe; - Arch_LAPICAddress->lvtCMCI = 0xfe /* Vector 0xFE, Fixed, Unmasked */; - Arch_LAPICAddress->lvtError = 0xfe /* Vector 0xFE, Fixed, Unmasked */; - Arch_LAPICAddress->lvtPerformanceMonitoringCounters = 0xfe /* Vector 0xFE, Fixed, Unmasked */; - Arch_LAPICAddress->lvtThermalSensor = 0xfe /* Vector 0xFE, Fixed, Unmasked */; - Arch_LAPICAddress->lvtTimer = 0xfe /* Vector 0xFE, Fixed, Unmasked */; -} - -void Arch_LAPICSendEOI() -{ - if (has_x2apic) - { - wrmsr(IA32_X2APIC_REGISTERS + 0xb, 0); - return; - } - - OBOS_ASSERT(Arch_LAPICAddress); - if (Arch_LAPICAddress) - Arch_LAPICAddress->eoi = 0; -} - -uint8_t Arch_LAPICReadID() -{ - if (!has_x2apic) - return Arch_LAPICAddress->lapicID; - else - return rdmsr(IA32_X2APIC_REGISTERS + 0x2); -} - -void Arch_WriteLAPICOffset(uint32_t offset, uint32_t value) -{ - if (has_x2apic) - wrmsr(offset/0x10 + IA32_X2APIC_REGISTERS, value); - else - *((uint32_t*)Arch_LAPICAddress + offset/4) = value; -} - -uint32_t Arch_ReadLAPICOffset(uint32_t offset) -{ - if (has_x2apic) - return rdmsr(offset/0x10 + IA32_X2APIC_REGISTERS); - else - return *((uint32_t*)Arch_LAPICAddress + offset/4); -} - -OBOS_NO_KASAN OBOS_NO_UBSAN obos_status Arch_LAPICSendIPI(ipi_lapic_info lapic, ipi_vector_info vector) -{ - if (!Arch_LAPICAddress && !has_x2apic) - return OBOS_STATUS_INVALID_INIT_PHASE; - - uint64_t icr = 0x4000 /* Level: Assert */; - if (lapic.isShorthand) - { - uint64_t shorthand = lapic.info.shorthand; - if (shorthand & ~LAPIC_DESTINATION_SHORTHAND_MASK) - return OBOS_STATUS_INVALID_ARGUMENT; - icr |= (shorthand << 18); - } - else - icr |= ((uint64_t)lapic.info.lapicId << (has_x2apic ? 32 : 56)); - if (vector.deliveryMode == LAPIC_DELIVERY_MODE_FIXED) - icr |= vector.info.vector; - else if (vector.deliveryMode == LAPIC_DELIVERY_MODE_SIPI) - icr |= (vector.info.address >> 12); - icr |= ((uint64_t)vector.deliveryMode << 8); - - if (!has_x2apic) - { - Arch_LAPICAddress->interruptCommand32_63 = icr >> 32; - Arch_LAPICAddress->interruptCommand0_31 = icr & UINT32_MAX; - } - else - wrmsr(IA32_X2APIC_REGISTERS + 0x30, icr); - - if (!has_x2apic) - { - while (Arch_LAPICAddress->interruptCommand0_31 & (1<<12)) - pause(); - } - else { - do { - icr = rdmsr(IA32_X2APIC_REGISTERS+0x30); - pause(); - } while (icr & BIT(12)); - } - return OBOS_STATUS_SUCCESS; -} - -void CoreS_DeferIRQ(interrupt_frame* frame) -{ - Arch_LAPICSendIPI((ipi_lapic_info){.isShorthand=true,.info.shorthand=LAPIC_DESTINATION_SHORTHAND_SELF}, - (ipi_vector_info){.deliveryMode=LAPIC_DELIVERY_MODE_FIXED,.info={.vector=frame->intNumber}}); -} - -void Arch_LAPICSetTimerConfiguration(uint32_t lvtTimer, uint32_t counter, uint32_t divideConfig) -{ - if (has_x2apic) - { - wrmsr(IA32_X2APIC_REGISTERS + 0x32, lvtTimer); - wrmsr(IA32_X2APIC_REGISTERS + 0x38, counter); - wrmsr(IA32_X2APIC_REGISTERS + 0x3e, divideConfig); - return; - } - Arch_LAPICAddress->lvtTimer = lvtTimer; - Arch_LAPICAddress->initialCount = counter; - Arch_LAPICAddress->divideConfig = divideConfig; -} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/lapic.h b/src/oboskrnl/arch/x86_64/lapic.h deleted file mode 100644 index 036abf9e..00000000 --- a/src/oboskrnl/arch/x86_64/lapic.h +++ /dev/null @@ -1,115 +0,0 @@ -/* - * oboskrnl/arch/x86_64/lapic.h - * - * Copyright (c) 2024-2026 Omar Berrow -*/ - -#pragma once - -#include -#include - -typedef struct lapic -{ - OBOS_ALIGNAS(0x10) volatile const uint8_t resv1[0x20]; - OBOS_ALIGNAS(0x10) volatile uint32_t lapicID; - OBOS_ALIGNAS(0x10) volatile uint32_t lapicVersion; - OBOS_ALIGNAS(0x10) volatile const uint8_t resv2[0x40]; - OBOS_ALIGNAS(0x10) volatile uint32_t taskPriority; - OBOS_ALIGNAS(0x10) volatile const uint32_t arbitrationPriority; - OBOS_ALIGNAS(0x10) volatile const uint32_t processorPriority; - OBOS_ALIGNAS(0x10) volatile uint32_t eoi; // write zero to send eoi. - OBOS_ALIGNAS(0x10) volatile uint32_t remoteRead; - OBOS_ALIGNAS(0x10) volatile uint32_t logicalDestination; - OBOS_ALIGNAS(0x10) volatile uint32_t destinationFormat; - OBOS_ALIGNAS(0x10) volatile uint32_t spuriousInterruptVector; - OBOS_ALIGNAS(0x10) volatile const uint32_t inService0_31; - OBOS_ALIGNAS(0x10) volatile const uint32_t inService32_63; - OBOS_ALIGNAS(0x10) volatile const uint32_t inService64_95; - OBOS_ALIGNAS(0x10) volatile const uint32_t inService96_127; - OBOS_ALIGNAS(0x10) volatile const uint32_t inService128_159; - OBOS_ALIGNAS(0x10) volatile const uint32_t inService160_191; - OBOS_ALIGNAS(0x10) volatile const uint32_t inService192_223; - OBOS_ALIGNAS(0x10) volatile const uint32_t inService224_255; - OBOS_ALIGNAS(0x10) volatile const uint32_t triggerMode0_31; - OBOS_ALIGNAS(0x10) volatile const uint32_t triggerMode32_63; - OBOS_ALIGNAS(0x10) volatile const uint32_t triggerMode64_95; - OBOS_ALIGNAS(0x10) volatile const uint32_t triggerMode96_127; - OBOS_ALIGNAS(0x10) volatile const uint32_t triggerMode128_159; - OBOS_ALIGNAS(0x10) volatile const uint32_t triggerMode160_191; - OBOS_ALIGNAS(0x10) volatile const uint32_t triggerMode192_223; - OBOS_ALIGNAS(0x10) volatile const uint32_t triggerMode224_255; - OBOS_ALIGNAS(0x10) volatile const uint32_t interruptRequest0_31; - OBOS_ALIGNAS(0x10) volatile const uint32_t interruptRequest32_63; - OBOS_ALIGNAS(0x10) volatile const uint32_t interruptRequest64_95; - OBOS_ALIGNAS(0x10) volatile const uint32_t interruptRequest96_127; - OBOS_ALIGNAS(0x10) volatile const uint32_t interruptRequest128_159; - OBOS_ALIGNAS(0x10) volatile const uint32_t interruptRequest160_191; - OBOS_ALIGNAS(0x10) volatile const uint32_t interruptRequest192_223; - OBOS_ALIGNAS(0x10) volatile const uint32_t interruptRequest224_255; - OBOS_ALIGNAS(0x10) volatile uint32_t errorStatus; - OBOS_ALIGNAS(0x10) volatile const uint8_t resv3[0x60]; - OBOS_ALIGNAS(0x10) volatile uint32_t lvtCMCI; - OBOS_ALIGNAS(0x10) volatile uint32_t interruptCommand0_31; - OBOS_ALIGNAS(0x10) volatile uint32_t interruptCommand32_63; - OBOS_ALIGNAS(0x10) volatile uint32_t lvtTimer; - OBOS_ALIGNAS(0x10) volatile uint32_t lvtThermalSensor; - OBOS_ALIGNAS(0x10) volatile uint32_t lvtPerformanceMonitoringCounters; - OBOS_ALIGNAS(0x10) volatile uint32_t lvtLINT0; - OBOS_ALIGNAS(0x10) volatile uint32_t lvtLINT1; - OBOS_ALIGNAS(0x10) volatile uint32_t lvtError; - OBOS_ALIGNAS(0x10) volatile uint32_t initialCount; - OBOS_ALIGNAS(0x10) volatile const uint32_t currentCount; - OBOS_ALIGNAS(0x10) volatile const uint8_t resv4[0x40]; - OBOS_ALIGNAS(0x10) volatile uint32_t divideConfig; - OBOS_ALIGNAS(0x10) volatile const uint8_t resv5[0x10]; -} lapic; - -extern lapic* Arch_LAPICAddress; -void Arch_LAPICInitialize(bool isBSP); -void Arch_LAPICSendEOI(); -uint8_t Arch_LAPICReadID(); - -void Arch_WriteLAPICOffset(uint32_t offset, uint32_t value); -uint32_t Arch_ReadLAPICOffset(uint32_t offset); - -typedef enum -{ - LAPIC_DESTINATION_SHORTHAND_NONE, - LAPIC_DESTINATION_SHORTHAND_SELF, - LAPIC_DESTINATION_SHORTHAND_ALL, - LAPIC_DESTINATION_SHORTHAND_ALL_BUT_SELF, - LAPIC_DESTINATION_SHORTHAND_MASK = 0b11, - LAPIC_DESTINATION_SHORTHAND_DEFAULT = LAPIC_DESTINATION_SHORTHAND_NONE, -} lapic_destination_shorthand; -typedef enum -{ - LAPIC_DELIVERY_MODE_FIXED = 0b000, - LAPIC_DELIVERY_MODE_SMI = 0b010, - LAPIC_DELIVERY_MODE_NMI = 0b100, - LAPIC_DELIVERY_MODE_INIT = 0b101, - LAPIC_DELIVERY_MODE_SIPI = 0b110, -} lapic_delivery_mode; -typedef struct ipi_lapic_info -{ - bool isShorthand; - union - { - uint8_t lapicId; - lapic_destination_shorthand shorthand; - } info; -} ipi_lapic_info; -typedef struct ipi_vector_info -{ - lapic_delivery_mode deliveryMode; - union - { - // Ignored for anything but fixed delivery mode. - uint8_t vector; - // Ignored for anything but SIPIs. - uint16_t address; - } info; -} ipi_vector_info; -obos_status Arch_LAPICSendIPI(ipi_lapic_info lapic, ipi_vector_info vector); - -void Arch_LAPICSetTimerConfiguration(uint32_t lvtTimer, uint32_t counter, uint32_t divideConfig); \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/lapic_timer_calibration.asm b/src/oboskrnl/arch/x86_64/lapic_timer_calibration.asm index 4c9823d6..cdcbf658 100644 --- a/src/oboskrnl/arch/x86_64/lapic_timer_calibration.asm +++ b/src/oboskrnl/arch/x86_64/lapic_timer_calibration.asm @@ -1,99 +1,52 @@ -; oboskrnl/arch/x86_64/lapic_timer_calibration.asm -; -; Copyright (c) 2024-2026 Omar Berrow - -[BITS 64] -[DEFAULT ABS] - -global Arch_FindCounter:function hidden -extern Arch_LAPICAddress -extern Arch_HPETAddress -extern Arch_CalibrateHPET -extern Arch_ReadLAPICOffset -extern Arch_WriteLAPICOffset - -; uint64_t Arch_FindCounter(uint64_t hz); -; (input) rdi: The expected frequency of the LAPIC count. -; (output) rax: The timer count for the LAPIC, assuming the divisor is LAPIC_TIMER_DIVISIOR_ONE (0b1101) -Arch_FindCounter: - push rbp - mov rbp, rsp - push r15 - - call Arch_CalibrateHPET - mov r15, rax - - mov edi, 0x3e0 - mov esi, 0xb - call Arch_WriteLAPICOffset - mov edi, 0x380 - mov esi, 0xffffffff - call Arch_WriteLAPICOffset - - mov r11, [Arch_HPETAddress] - - ; Start the HPET timer. - mov rax, [r11+0x10] - or rax, (1<<0) - mov [r11+0x10], rax - - add r11, 0xf0 - -.loop: - mov r9, [r11] - cmp r9, r15 - jnge .loop - - mov edi, 0x390 - call Arch_ReadLAPICOffset - - push rax - - mov edi, 0x380 - xor esi, esi - call Arch_WriteLAPICOffset - - pop r9 - - mov rax, 0xffffffff - sub rax, r9 - -.end: - pop r15 - leave - ret -extern rdtsc -global Arch_FindTSCChangeRate -Arch_FindTSCChangeRate: - push rbp - mov rbp, rsp - push r15 - push r13 - - mov r15, rdi - - call rdtsc - mov r13, rax - - mov r11, [Arch_HPETAddress] - - ; Start the HPET timer. - mov rax, [r11+0x10] - or rax, (1<<0) - mov [r11+0x10], rax - - lea r11, [r11+0xf0] - -.loop: - cmp [r11], r15 - jnge .loop - -.end: - - call rdtsc - sub rax, r13 - - pop r13 - pop r15 - leave +; oboskrnl/arch/x86_64/lapic_timer_calibration.asm +; +; Copyright (c) 2024 Omar Berrow + +[BITS 64] + +global FindCounter +extern _ZN4obos18g_localAPICAddressE +extern _ZN4obos4arch13g_hpetAddressE +extern calibrateHPET + +; uint64_t FindCounter(uint64_t hz); +; (input) rdi: The expected frequency of the LAPIC count. +; (output) rax: The timer count for the LAPIC, assuming the divisor is LAPIC_TIMER_DIVISIOR_ONE (0b1101) +FindCounter: + push rbp + mov rbp, rsp + push r15 + push r13 + + call calibrateHPET + mov r15, rax + mov r13, [_ZN4obos18g_localAPICAddressE] + mov r11, [_ZN4obos4arch13g_hpetAddressE] + + mov r13, [_ZN4obos18g_localAPICAddressE] + mov dword [r13+0x3E0], 0xD ; DIVISOR_ONE + mov dword [r13+0x380], 0xffffffff + + ; Start the HPET timer. + mov rax, [r11+0x10] + or rax, (1<<0) + mov [r11+0x10], rax + + add r11, 0xf0 + +.loop: + mov r9, [r11] + cmp r9, r15 + jnge .loop + + xor r9,r9 + mov r9d, [r13+0x390] + mov dword [r13+0x380], 0 + mov rax, 0xffffffff + sub rax, r9 + +.end: + pop r13 + pop r15 + leave ret \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/madt.h b/src/oboskrnl/arch/x86_64/madt.h deleted file mode 100644 index 0f6de876..00000000 --- a/src/oboskrnl/arch/x86_64/madt.h +++ /dev/null @@ -1,76 +0,0 @@ -/* - * oboskrnl/arch/x86_64/irq/madt.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include - -#include - -typedef struct OBOS_PACK MADTTable -{ - ACPISDTHeader sdtHeader; - uint32_t lapicAddress; - uint32_t unwanted; - // There are more entries. -} MADTTable; -typedef struct OBOS_PACK MADT_EntryHeader -{ - uint8_t type; - uint8_t length; -} MADT_EntryHeader; -typedef struct OBOS_PACK MADT_EntryType0 -{ - MADT_EntryHeader entryHeader; - uint8_t processorID; - uint8_t apicID; - uint32_t flags; -} MADT_EntryType0; -typedef struct OBOS_PACK MADT_EntryType1 -{ - MADT_EntryHeader entryHeader; - uint8_t ioApicID; - uint8_t resv1; - uint32_t ioapicAddress; - uint32_t globalSystemInterruptBase; -} MADT_EntryType1; -typedef struct OBOS_PACK MADT_EntryType2 -{ - MADT_EntryHeader entryHeader; - uint8_t busSource; - uint8_t irqSource; - uint32_t globalSystemInterrupt; - uint16_t flags; -} MADT_EntryType2; -typedef struct OBOS_PACK MADT_EntryType3 -{ - MADT_EntryHeader entryHeader; - uint8_t nmiSource; - uint8_t resv; - uint16_t flags; - uint32_t globalSystemInterrupt; -} MADT_EntryType3; -typedef struct OBOS_PACK MADT_EntryType4 -{ - MADT_EntryHeader entryHeader; - uint8_t processorID; - uint16_t flags; - uint8_t lINT; -} MADT_EntryType4; -typedef struct OBOS_PACK MADT_EntryType5 -{ - MADT_EntryHeader entryHeader; - uint8_t resv1[2]; - uintptr_t lapic_address; -} MADT_EntryType5; -typedef struct OBOS_PACK MADT_EntryType9 -{ - MADT_EntryHeader entryHeader; - uint8_t resv1[2]; - uint32_t x2APIC_ID; - uint32_t flags; - uint32_t acpiID; -} MADT_EntryType9; \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/map.c b/src/oboskrnl/arch/x86_64/map.c deleted file mode 100644 index 8569dc0e..00000000 --- a/src/oboskrnl/arch/x86_64/map.c +++ /dev/null @@ -1,635 +0,0 @@ -/* - * oboskrnl/arch/x86_64/map.c - * - * Copyright (c) 2024-2026 Omar Berrow -*/ - -#include -#include -#include -#include -#include - -#include - -#include -#include -#include -#include - -#include - -#include - -#include - -#include -#include -#include -#include -#include - -#include - -#include - -#include - -#include -#include - -// Abandon all hope, ye who enter here. - -static __attribute__((no_instrument_function)) OBOS_NO_KASAN size_t AddressToIndex(uintptr_t address, uint8_t level) { return (address >> (9 * level + 12)) & 0x1FF; } - -OBOS_NO_KASAN __attribute__((no_instrument_function)) uintptr_t Arch_MaskPhysicalAddressFromEntry(uintptr_t phys) -{ - return phys & 0xffffffffff000; -} -OBOS_NO_KASAN __attribute__((no_instrument_function)) uintptr_t Arch_GetPML4Entry(uintptr_t pml4Base, uintptr_t addr) -{ - if (!pml4Base) - return 0; - uintptr_t* arr = (uintptr_t*)MmS_MapVirtFromPhys(Arch_MaskPhysicalAddressFromEntry(pml4Base)); - return arr[AddressToIndex(addr, 3)]; -} -OBOS_NO_KASAN __attribute__((no_instrument_function)) uintptr_t Arch_GetPML3Entry(uintptr_t pml4Base, uintptr_t addr) -{ - uintptr_t phys = Arch_MaskPhysicalAddressFromEntry(Arch_GetPML4Entry(pml4Base, addr)); - if (!phys) - return 0; - uintptr_t* arr = (uintptr_t*)MmS_MapVirtFromPhys(phys); - return arr[AddressToIndex(addr, 2)]; -} -OBOS_NO_KASAN __attribute__((no_instrument_function)) uintptr_t Arch_GetPML2Entry(uintptr_t pml4Base, uintptr_t addr) -{ - uintptr_t phys = Arch_MaskPhysicalAddressFromEntry(Arch_GetPML3Entry(pml4Base, addr)); - if (!phys) - return 0; - uintptr_t* arr = (uintptr_t*)MmS_MapVirtFromPhys(phys); - return arr[AddressToIndex(addr, 1)]; -} -OBOS_NO_KASAN __attribute__((no_instrument_function)) uintptr_t Arch_GetPML1Entry(uintptr_t pml4Base, uintptr_t addr) -{ - uintptr_t phys = Arch_MaskPhysicalAddressFromEntry(Arch_GetPML2Entry(pml4Base, addr)); - if (!phys) - return 0; - uintptr_t* arr = (uintptr_t*)MmS_MapVirtFromPhys(phys); - return arr[AddressToIndex(addr, 0)]; -} - -static __attribute__((no_instrument_function)) uintptr_t GetPageMapEntryForDepth(uintptr_t pml4Base, uintptr_t addr, uint8_t depth) -{ - switch (depth) - { - case 1: - return Arch_GetPML2Entry(pml4Base, addr); - case 2: - return Arch_GetPML3Entry(pml4Base, addr); - case 3: - return Arch_GetPML4Entry(pml4Base, addr); - default: - break; - } - return 0; -} - -uintptr_t* Arch_AllocatePageMapAt(uintptr_t pml4Base, uintptr_t at, uintptr_t cpuFlags, uint8_t depth) -{ - if (depth > 3 || depth == 0) - return nullptr; - cpuFlags &= ~0xfffffffff0000; - cpuFlags |= 1; - // Clear the caching flags. - cpuFlags &= ~(1 << 3) & ~(1 << 4) & ~(1 << 7); - // Clear the avaliable bits in the flags. - cpuFlags &= ~0x07F0000000000E00; - for (uint8_t i = 3; i > (3 - depth); i--) - { - uintptr_t* pageMap = (uintptr_t*)MmS_MapVirtFromPhys((i + 1) == 4 ? pml4Base : Arch_MaskPhysicalAddressFromEntry(GetPageMapEntryForDepth(pml4Base, at, i + 1))); - if (!pageMap[AddressToIndex(at, i)]) - { - uintptr_t newTable = Mm_AllocatePhysicalPages(1,1, nullptr); - memzero(MmS_MapVirtFromPhys(newTable), 4096); - pageMap[AddressToIndex(at, i)] = newTable | cpuFlags; - } - else - { - uintptr_t entry = (uintptr_t)pageMap[AddressToIndex(at, i)]; - if ((entry & ((uintptr_t)1 << 63)) && !(cpuFlags & ((uintptr_t)1 << 63))) - entry &= ~((uintptr_t)1 << 63); - if (!(entry & ((uintptr_t)1 << 2)) && (cpuFlags & ((uintptr_t)1 << 2))) - entry |= ((uintptr_t)1 << 2); - if (!(entry & ((uintptr_t)1 << 1)) && (cpuFlags & ((uintptr_t)1 << 1))) - entry |= ((uintptr_t)1 << 1); - pageMap[AddressToIndex(at, i)] = entry; - } - } - return (uintptr_t*)MmS_MapVirtFromPhys(Arch_MaskPhysicalAddressFromEntry(GetPageMapEntryForDepth(pml4Base, at, (4 - depth)))); -} -bool Arch_FreePageMapAt(uintptr_t pml4Base, uintptr_t at, uint8_t maxDepth) -{ - if (maxDepth > 3 || maxDepth == 0) - return false; - for (uint8_t i = (4 - maxDepth); i < 4; i++) - { - if (!(GetPageMapEntryForDepth(pml4Base, at, i + 1) & 1)) - continue; - uintptr_t* pageMap = (uintptr_t*)MmS_MapVirtFromPhys(Arch_MaskPhysicalAddressFromEntry(GetPageMapEntryForDepth(pml4Base, at, i + 1))); - uintptr_t phys = Arch_MaskPhysicalAddressFromEntry(pageMap[AddressToIndex(at, i)]); - uintptr_t* subPageMap = (uintptr_t*)MmS_MapVirtFromPhys(phys); - if (memcmp_b(subPageMap, (int)0, 4096)) - { - pageMap[AddressToIndex(at, i)] = 0; - Mm_FreePhysicalPages(phys, 1); - continue; - } - } - return true; -} - -static bool has_xd() -{ - static bool ret = false, init = false; - if (!init) - { - ret = (rdmsr(0xC0000080) & (1 << 11)); - init = true; - } - return ret; -} - -obos_status Arch_MapPage(uintptr_t cr3, void* at_, uintptr_t phys, uintptr_t flags, bool free_pte) -{ - if (!(((uintptr_t)(at_) >> 47) == 0 || ((uintptr_t)(at_) >> 47) == 0x1ffff)) - return OBOS_STATUS_INVALID_ARGUMENT; - uintptr_t at = (uintptr_t)at_; - if (phys & 0xfff || at & 0xfff) - return OBOS_STATUS_INVALID_ARGUMENT; - if (obos_expect(has_xd(), true)) - flags &= ~0x8000000000000000; // If XD is disabled in IA32_EFER (0xC0000080), disable the bit here. - if (phys & ~0xffffffffff000) - return OBOS_STATUS_INVALID_ARGUMENT; - phys = Arch_MaskPhysicalAddressFromEntry(phys); - uintptr_t* pm = Arch_AllocatePageMapAt(cr3, at, flags & ~(BIT(9) | BIT_TYPE(52, UL)), 3); - uintptr_t entry = phys | flags; - // for (volatile bool b = !(flags & 1); b; ) - // ; - pm[AddressToIndex(at, 0)] = entry; - if (free_pte && ~flags & BIT_TYPE(0, UL)) - Arch_FreePageMapAt(cr3, at, 3); - return OBOS_STATUS_SUCCESS; -} -obos_status Arch_MapHugePage(uintptr_t cr3, void* at_, uintptr_t phys, uintptr_t flags, bool free_pte) -{ - if (!(((uintptr_t)(at_) >> 47) == 0 || ((uintptr_t)(at_) >> 47) == 0x1ffff)) - return OBOS_STATUS_INVALID_ARGUMENT; - // flags |= 1; - uintptr_t at = (uintptr_t)at_; - if (phys & 0x1fffff || at & 0x1fffff) - return OBOS_STATUS_INVALID_ARGUMENT; - if (phys & ~0xffffffffff000) - return OBOS_STATUS_INVALID_ARGUMENT; - if (obos_expect(!has_xd(), true)) - flags &= ~0x8000000000000000; // If XD is disabled in IA32_EFER (0xC0000080), disable the bit here. - if (flags & ((uintptr_t)1 << 7)) - flags |= ((uintptr_t)1 << 12); - phys = Arch_MaskPhysicalAddressFromEntry(phys); - uintptr_t* pm = Arch_AllocatePageMapAt(cr3, at, flags & ~(BIT(9) | BIT_TYPE(52, UL)), 2); - uintptr_t entry = phys | flags | ((uintptr_t)1 << 7); - pm[AddressToIndex(at, 1)] = entry; - if (free_pte && ~flags & BIT_TYPE(0, UL)) - Arch_FreePageMapAt(cr3, at, 2); - return OBOS_STATUS_SUCCESS; -} - -extern bool Arch_SMPInitialized; -obos_status Arch_UnmapPage(uintptr_t cr3, void* at_, bool free_pte) -{ - if (!(((uintptr_t)(at_) >> 47) == 0 || ((uintptr_t)(at_) >> 47) == 0x1ffff)) - return OBOS_STATUS_INVALID_ARGUMENT; - uintptr_t at = (uintptr_t)at_; - if (at & 0xfff) - return OBOS_STATUS_INVALID_ARGUMENT; - uintptr_t entry = Arch_GetPML2Entry(cr3, at); - if (!(entry & (1 << 0))) - return OBOS_STATUS_SUCCESS; - bool isHugePage = (entry & (1ULL<<7)); - if (isHugePage) - entry = Arch_GetPML3Entry(cr3, at); - if (!(entry & (1<<0))) - return OBOS_STATUS_SUCCESS; - uintptr_t phys = Arch_MaskPhysicalAddressFromEntry(entry); - uintptr_t* pt = (uintptr_t*)MmS_MapVirtFromPhys(phys); - pt[AddressToIndex(at, (uint8_t)isHugePage)] &= ~BIT(0); - if (free_pte) - Arch_FreePageMapAt(cr3, at, 3 - (uint8_t)isHugePage); - return OBOS_STATUS_SUCCESS; -} - -static irq* invlpg_irq; -typedef struct tlb_shootdown_packet { - uintptr_t base; - size_t size; - atomic_size_t refcount; - LIST_NODE(tlb_shootdown_queue, struct tlb_shootdown_packet) node; -} tlb_shootdown_packet; -typedef LIST_HEAD(tlb_shootdown_queue, tlb_shootdown_packet) tlb_shootdown_queue; -LIST_GENERATE_STATIC(tlb_shootdown_queue, tlb_shootdown_packet, node); -tlb_shootdown_queue g_tlb_shootdown_queue; -spinlock g_tlb_shootdown_queue_lock; - -static void deref_tlb_shootdown_pckt(tlb_shootdown_packet* pckt) -{ - if (!--pckt->refcount) - { - irql oldIrql = Core_SpinlockAcquire(&g_tlb_shootdown_queue_lock); - LIST_REMOVE(tlb_shootdown_queue, &g_tlb_shootdown_queue, pckt); - Core_SpinlockRelease(&g_tlb_shootdown_queue_lock, oldIrql); - Free(Mm_Allocator, pckt, sizeof(*pckt)); - } -} - -bool Arch_InvlpgIPI(interrupt_frame* frame) -{ - OBOS_UNUSED(frame); - if (!LIST_GET_NODE_COUNT(tlb_shootdown_queue, &g_tlb_shootdown_queue)) - return false; - tlb_shootdown_packet* curr = CoreS_GetCPULocalPtr()->arch_specific.curr_pckt; - - irql oldIrql = Core_SpinlockAcquire(&g_tlb_shootdown_queue_lock); - if (!curr) - curr = LIST_GET_TAIL(tlb_shootdown_queue, &g_tlb_shootdown_queue); - else - curr = LIST_GET_NEXT(tlb_shootdown_queue, &g_tlb_shootdown_queue, curr); - Core_SpinlockRelease(&g_tlb_shootdown_queue_lock, oldIrql); - - while (curr) - { - for (uintptr_t addr = curr->base; addr <= (curr->base+curr->size); addr += OBOS_PAGE_SIZE) - { - OBOS_ASSERT(addr >= curr->base); - invlpg(addr); - } - - if (curr != LIST_GET_TAIL(tlb_shootdown_queue, &g_tlb_shootdown_queue)) - deref_tlb_shootdown_pckt(curr); - else - CoreS_GetCPULocalPtr()->arch_specific.curr_pckt = curr; - oldIrql = Core_SpinlockAcquire(&g_tlb_shootdown_queue_lock); - curr = LIST_GET_NEXT(tlb_shootdown_queue, &g_tlb_shootdown_queue, curr); - Core_SpinlockRelease(&g_tlb_shootdown_queue_lock, oldIrql); - } - - return true; -} - -static void invlpg_ipi_bootstrap(struct irq* i, interrupt_frame* frame, void* userdata, irql oldIrql) -{ - OBOS_UNUSED(i); - OBOS_UNUSED(userdata); - OBOS_UNUSED(oldIrql); - Arch_InvlpgIPI(frame); -} - -#define issue_nmi() \ - Arch_LAPICSendIPI((ipi_lapic_info){.isShorthand=true,.info.shorthand=LAPIC_DESTINATION_SHORTHAND_ALL}, \ - (ipi_vector_info){.deliveryMode=LAPIC_DELIVERY_MODE_NMI}) - - -extern uintptr_t Arch_KernelCR3; - -enum { IRQL_INVLPG_IPI=15 }; - -obos_status MmS_TLBShootdown(page_table pt, uintptr_t base, size_t size) -{ - if (Core_CpuCount == 1 || !Arch_SMPInitialized) - { - for (uintptr_t addr = base; addr < (base + size); addr += OBOS_PAGE_SIZE) - invlpg(addr); - return OBOS_STATUS_SUCCESS; - } - -#if OBOS_UP - return OBOS_STATUS_SUCCESS; -#endif - - if (pt != Arch_KernelCR3) - goto ipi; - - // Can this allocation cause problems? - // i sure do hope not... - tlb_shootdown_packet* pckt = ZeroAllocate(Mm_Allocator, 1, sizeof(tlb_shootdown_packet), nullptr); - pckt->base = base; - pckt->size = size; - pckt->refcount = Core_CpuCount; - irql oldIrql = Core_SpinlockAcquire(&g_tlb_shootdown_queue_lock); - LIST_APPEND(tlb_shootdown_queue, &g_tlb_shootdown_queue, pckt); - Core_SpinlockRelease(&g_tlb_shootdown_queue_lock, oldIrql); - - ipi: - // Issue the IPI. - - if (Core_IrqInterfaceInitialized()) - { - static ipi_vector_info vec = {}; - static ipi_lapic_info dest = {.isShorthand=true,.info.shorthand=LAPIC_DESTINATION_SHORTHAND_ALL}; - if (!invlpg_irq) - { - static irq irq; - invlpg_irq = &irq; - Core_IrqObjectInitializeIRQL(&irq, IRQL_INVLPG_IPI, false, true); - irq.handler = invlpg_ipi_bootstrap; - irq.handlerUserdata = nullptr; - - vec.deliveryMode = LAPIC_DELIVERY_MODE_FIXED; - vec.info.vector = irq.vector->id + 0x20; - } - Arch_LAPICSendIPI(dest, vec); - } - else if (pt != Arch_KernelCR3) - issue_nmi(); - - return OBOS_STATUS_SUCCESS; -} - -obos_status OBOSS_MapPage_RW_XD(void* at_, uintptr_t phys) -{ - return Arch_MapPage(getCR3(), at_, phys, 0x8000000000000003, false); -} -obos_status OBOSS_UnmapPage(void* at_) -{ - return Arch_UnmapPage(getCR3(), at_, true); - -} -obos_status OBOSS_GetPagePhysicalAddress(void* at_, uintptr_t* oPhys) -{ - if (!(((uintptr_t)(at_) >> 47) == 0 || ((uintptr_t)(at_) >> 47) == 0x1ffff)) - return OBOS_STATUS_INVALID_ARGUMENT; - if (!oPhys) - return OBOS_STATUS_INVALID_ARGUMENT; - uintptr_t at = (uintptr_t)at_; - *oPhys = 0; - uintptr_t entry = Arch_GetPML2Entry(getCR3(), at); - if (!(entry & (1 << 0))) - return OBOS_STATUS_SUCCESS; - bool isHugePage = (entry & (1ULL << 7)); - if (isHugePage) - entry = Arch_GetPML3Entry(getCR3(), at); - if (!(entry & (1 << 0))) - return OBOS_STATUS_SUCCESS; - *oPhys = Arch_MaskPhysicalAddressFromEntry(((uintptr_t*)MmS_MapVirtFromPhys(Arch_MaskPhysicalAddressFromEntry(entry)))[AddressToIndex(at, (uint8_t)isHugePage)]); - return OBOS_STATUS_SUCCESS; -} - -static basicmm_region kernel_region; -static basicmm_region hhdm_region; -static void FreePageTables(uintptr_t* pm, uint8_t level, uint32_t beginIndex, bool user, uint32_t* indices) -{ - if (!pm) - return; - pm = (uintptr_t*)MmS_MapVirtFromPhys((uintptr_t)pm); - for (indices[level] = beginIndex; indices[level] < (user ? 256 : 512); indices[level]++) - { - if (!pm[indices[level]]) - continue; - if (pm[indices[level]] & ((uintptr_t)1<<7) || level == 0) - continue; - FreePageTables((uintptr_t*)Arch_MaskPhysicalAddressFromEntry(pm[indices[level]]), level - 1, 0, false, indices); - Mm_FreePhysicalPages(Arch_MaskPhysicalAddressFromEntry(pm[indices[level]]), 1); - } -} -uintptr_t MmS_KernelBaseAddress; -uintptr_t MmS_KernelEndAddress; -obos_status Arch_InitializeKernelPageTable() -{ - obos_status status = OBOS_STATUS_SUCCESS; - uintptr_t newCR3 = Mm_AllocatePhysicalPages32(1,1, &status); - uintptr_t oldCR3 = getCR3(); - memzero(MmS_MapVirtFromPhys(newCR3), 4096); - if (status != OBOS_STATUS_SUCCESS) - return status; - OBOS_Debug("%s: Mapping kernel.\n", __func__); - Elf64_Ehdr* ehdr = (Elf64_Ehdr*)Arch_KernelBinary->address; - Elf64_Phdr* phdrs = (Elf64_Phdr*)(Arch_KernelBinary->address + ehdr->e_phoff); - size_t i = 0; - uintptr_t kernel_limit = 0; - for (; i < ehdr->e_phnum; i++) - { - Elf64_Phdr* phdr = phdrs + i; - if (phdr->p_type != PT_LOAD) - continue; - if (!phdr->p_memsz) - continue; - uintptr_t flags = 1; - if (!(phdr->p_flags & PF_X)) - flags |= 0x8000000000000000 /* XD */; - if (phdr->p_flags & PF_W) - flags |= 2; - uintptr_t base = phdr->p_vaddr & ~0xfff; -#if !OBOS_USE_LIMINE - if (base < Arch_KernelInfo->virtual_base) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "Fatal error. Bootloader made a whoopsie! (line %d in file %s). Expression: base < Arch_KernelInfo->virtual_base.\n", __LINE__, __FILE__); -#endif - uintptr_t limit = phdr->p_vaddr + phdr->p_memsz; - if (limit & 0xfff) - limit = (limit + 0xfff) & ~0xfff; - for (uintptr_t virt = base; virt < limit; virt += OBOS_PAGE_SIZE) - { - uintptr_t phys = Arch_MaskPhysicalAddressFromEntry(Arch_GetPML1Entry(oldCR3, virt)); - OBOS_ASSERT(phys); - Arch_MapPage(newCR3, (void*)virt, phys, flags, false); - } - kernel_limit = kernel_limit > limit ? kernel_limit : limit; - } - OBOS_Debug("%s: Mapping HHDM.\n", __func__); - for (uintptr_t off = 0; off < Mm_PhysicalMemoryBoundaries; off += 0x200000) - Arch_MapHugePage(newCR3, MmS_MapVirtFromPhys(off), off, 0x8000000000000003 /* XD, Write, Present */, false); - asm volatile("mov %0, %%cr3;" : :"r"(newCR3)); - // Reclaim old page tables. - uint32_t indices[4] = { 0,0,0,0 }; - FreePageTables((uintptr_t*)oldCR3, 3, AddressToIndex(0xffff800000000000, 3), false, indices); - Mm_FreePhysicalPages((uintptr_t)oldCR3, 1); -#if OBOS_USE_LIMINE - OBOSH_BasicMMAddRegion( - &kernel_region, - (void*)Arch_LimineKernelAddressRequest.response->virtual_base, - kernel_limit - Arch_LimineKernelAddressRequest.response->virtual_base); - OBOSH_BasicMMAddRegion(&hhdm_region, (void*)Arch_LimineHHDMRequest.response->offset, Mm_PhysicalMemoryBoundaries); -#else - OBOSH_BasicMMAddRegion(&kernel_region, (void*)Arch_KernelInfo->virtual_base, Arch_KernelInfo->size); - OBOSH_BasicMMAddRegion(&hhdm_region, (void*)Arch_LdrPlatformInfo->higher_half_base, Mm_PhysicalMemoryBoundaries); -#endif - Arch_KernelCR3 = newCR3; - return OBOS_STATUS_SUCCESS; -} -obos_status MmS_QueryPageInfo(page_table pt, uintptr_t addr, page_info* ppage, uintptr_t* phys) -{ - if (!pt) - return OBOS_STATUS_INVALID_ARGUMENT; - if (!ppage && !phys) - return OBOS_STATUS_SUCCESS; - page_info page; - memzero(&page, sizeof(page)); - uintptr_t pml2Entry = Arch_GetPML2Entry(pt, addr); - uintptr_t pml1Entry = Arch_GetPML1Entry(pt, addr); - page.prot.present = pml2Entry & BIT_TYPE(0, UL); - page.prot.huge_page = pml2Entry & BIT_TYPE(7, UL) /* Huge page */; - // if (!page.prot.present) - // { - // if (ppage) - // { - // ppage->prot = page.prot; - // ppage->virt = addr & ~0xfff; - // ppage->phys = Arch_MaskPhysicalAddressFromEntry(pml2Entry); - // } - // if (phys) - // *phys = Arch_MaskPhysicalAddressFromEntry(pml2Entry); - // return OBOS_STATUS_SUCCESS; - // } - uintptr_t entry = 0; - if (page.prot.huge_page) - { - addr &= ~0x1fffff; - entry = pml2Entry; - } - else - { - addr &= ~0xfff; - page.prot.present = pml1Entry & BIT_TYPE(0, UL); - entry = pml1Entry; - } - page.virt = addr; - page.prot.rw = entry & BIT_TYPE(1, UL); - page.prot.user = entry & BIT_TYPE(2, UL); - page.accessed = entry & BIT_TYPE(5, UL); - page.dirty = entry & BIT_TYPE(6, UL); - page.prot.lck = entry & BIT_TYPE(52, UL); - page.prot.executable = !(entry & BIT_TYPE(63, UL)); - page.prot.is_swap_phys = entry & BIT_TYPE(9, UL); - // page.prot.uc = (entry & BIT_TYPE(4, UL)); - if (page.prot.huge_page) - { - uintptr_t pml3Entry = Arch_MaskPhysicalAddressFromEntry(Arch_GetPML3Entry(pt, addr)); - pml3Entry = (uintptr_t)MmS_MapVirtFromPhys(pml3Entry); - ((uintptr_t*)pml3Entry)[AddressToIndex(addr, 1)] &= ~(BIT_TYPE(5, UL) | BIT_TYPE(6, UL)); - } - else - { - pml2Entry = Arch_MaskPhysicalAddressFromEntry(pml2Entry); - pml2Entry = (uintptr_t)MmS_MapVirtFromPhys(pml2Entry); - ((uintptr_t*)pml2Entry)[AddressToIndex(addr, 0)] &= ~(BIT_TYPE(5, UL) | BIT_TYPE(6, UL)); - } - if (ppage) - { - ppage->virt = addr; - ppage->phys = page.prot.huge_page ? (entry & 0xFFFFFFFE00000) : Arch_MaskPhysicalAddressFromEntry(entry); - memcpy(&ppage->prot, &page.prot, sizeof(page.prot)); - } - if (phys) - *phys = page.prot.huge_page ? (entry & 0xFFFFFFFE00000) : Arch_MaskPhysicalAddressFromEntry(entry); - return OBOS_STATUS_SUCCESS; -} -obos_status MmS_SetPageMapping(page_table pt, const page_info* page, uintptr_t phys, bool free_pte) -{ - if (!page || !pt) - return OBOS_STATUS_INVALID_ARGUMENT; - // if (!page->prot.present) - // return Arch_UnmapPage(pt, (void*)page->virt, free_pte); - uintptr_t flags = 0; - if (page->prot.rw) - flags |= BIT_TYPE(1, UL); - if (page->prot.user) - flags |= BIT_TYPE(2, UL); - if (!page->prot.executable) - flags |= BIT_TYPE(63, UL); - if (page->prot.present) - flags |= BIT_TYPE(0, UL); - if (page->prot.is_swap_phys) - flags |= BIT_TYPE(9, UL); /* Available bit */ - if (page->prot.lck) - flags |= BIT_TYPE(52, UL); /* Available bit */ - if (page->prot.uc && !page->prot.uc) - flags |= BIT_TYPE(4, UL); - if (page->prot.fb) - flags |= BIT_TYPE(4, UL)|BIT_TYPE(7, UL) /* write-Combining */; - // if (page->prot.present && !page->prot.is_swap_phys) - // OBOS_ENSURE(!Mm_PhysicalPageFree(phys)); - // if (page->prot.is_swap_phys) - // OBOS_ENSURE(!page->prot.present); - if (pt != Arch_KernelCR3) - { - // printf("map 0x%p 0x%p (present: %d, is_swap_phys: %d, rw: %d, exec: %d)\n", page->virt, phys, - // page->prot.present,page->prot.is_swap_phys,page->prot.rw,page->prot.executable); - // for (volatile bool b = (page->virt == 0x4100c000); b;) - // ; - } - obos_status status = !page->prot.huge_page ? - Arch_MapPage(pt, (void*)(page->virt & ~0xfff), phys, flags, free_pte) : - Arch_MapHugePage(pt, (void*)(page->virt & ~0x1fffff), phys, flags, free_pte); - OBOS_ASSERT(obos_is_success(status)); - return status; -} - -page_table cached_root = {}; -extern char Arch_StartISRHandlersText; -extern char Arch_EndISRHandlersText; -extern char CoreS_SwitchToThreadContextEnd; -static void map_range(uintptr_t cached_root, uintptr_t base, uintptr_t top, uintptr_t flags) -{ - base &= ~0xfff; - top += 0xfff; - top &= ~0xfff; - for (uintptr_t addr = base; addr < top; addr += 0x1000) - { - uintptr_t phys = 0; - uintptr_t pml2ent = Arch_GetPML2Entry(Arch_KernelCR3, addr); - if (pml2ent & BIT_TYPE(7, UL)) - phys = Arch_MaskPhysicalAddressFromEntry(pml2ent) + (addr & (OBOS_HUGE_PAGE_SIZE-1)); - else - phys = Arch_MaskPhysicalAddressFromEntry(Arch_GetPML1Entry(Arch_KernelCR3, addr)); - OBOS_ENSURE(phys != 0); - Arch_MapPage(cached_root, (void*)addr, phys, flags, false); - } -} -page_table MmS_AllocatePageTable() -{ - page_table root = Mm_AllocatePhysicalPages(1, 1, nullptr); - if (!cached_root) - { - cached_root = Mm_AllocatePhysicalPages(1, 1, nullptr); - memzero(Arch_MapToHHDM(cached_root), OBOS_PAGE_SIZE); - // Map the ISR handlers. - map_range(cached_root, (uintptr_t)&Arch_StartISRHandlersText, (uintptr_t)&Arch_EndISRHandlersText, BIT(0)); - // Map Arch_KernelCR3 - map_range(cached_root, (uintptr_t)&Arch_KernelCR3, (uintptr_t)(&Arch_KernelCR3 + 1), BIT(0) | BIT_TYPE(63, UL)); - // Map CoreS_SwitchToThreadContext - map_range(cached_root, (uintptr_t)&CoreS_SwitchToThreadContext, (uintptr_t)&CoreS_SwitchToThreadContextEnd, BIT(0)); - // Map kernel stacks. - for (size_t i = 0; i < Core_CpuCount; i++) - { - cpu_local* const cpu = Core_CpuInfo + i; - map_range(cached_root, (uintptr_t)cpu->arch_specific.ist_stack, (uintptr_t)cpu->arch_specific.ist_stack + 0x20000, 1|BIT_TYPE(63, UL)|2); - } - // Map cpu local structs. - map_range(cached_root, (uintptr_t)Core_CpuInfo, (uintptr_t)(Core_CpuInfo + Core_CpuCount), 1|BIT_TYPE(63, UL)|2); - // Map IDT - extern struct idtEntry g_idtEntries[256]; - map_range(cached_root, (uintptr_t)&g_idtEntries, ((uintptr_t)&g_idtEntries) + 0x1000, 1|BIT_TYPE(63, UL)|2); - extern uintptr_t Arch_IRQHandlers[256]; - map_range(cached_root, (uintptr_t)&Arch_IRQHandlers, ((uintptr_t)&Arch_IRQHandlers) + sizeof(Arch_IRQHandlers), 1|BIT_TYPE(63, UL)|2); - // Map syscall trap handler. - extern char Arch_SyscallTrapHandlerEnd; - extern char Arch_SyscallTrapHandler; - map_range(cached_root, (uintptr_t)&Arch_SyscallTrapHandler, (uintptr_t)&Arch_SyscallTrapHandlerEnd, 1|2); - } - memcpy(Arch_MapToHHDM(root), Arch_MapToHHDM(cached_root), OBOS_PAGE_SIZE); - return root; -} -void MmS_FreePageTable(page_table pt) -{ - OBOS_ENSURE(pt != Arch_KernelCR3); - uint32_t indices[4] = {}; - FreePageTables((uintptr_t*)pt, 3, 0, true, indices); - Mm_FreePhysicalPages(pt, 1); -} diff --git a/src/oboskrnl/arch/x86_64/memmanip.asm b/src/oboskrnl/arch/x86_64/memmanip.asm index ba93154f..3bd2f12b 100644 --- a/src/oboskrnl/arch/x86_64/memmanip.asm +++ b/src/oboskrnl/arch/x86_64/memmanip.asm @@ -1,126 +1,128 @@ -; oboskrnl/arch/x86_64/memmanip.asm -; -; Copyright (c) 2024 Omar Berrow - -[BITS 64] -[DEFAULT ABS] - -global memset:function default -global memzero:function default -global memcpy:function default -global memcmp:function default -global memcmp_b:function default -global strcmp:function default -global strlen:function default -global strchr:function default - -section .text - -memset: - push rbp - mov rbp, rsp - - push rdi - mov al, sil - mov rcx, rdx - rep stosb - pop rax - - leave - ret -memzero: - push rbp - mov rbp, rsp - - push rdi - xor eax, eax - mov rcx, rsi - rep stosb - pop rax - - leave - ret -memcpy: - push rbp - mov rbp, rsp - - mov rax, rdi - mov rcx, rdx - rep movsb - - leave - ret -memcmp: - push rbp - mov rbp, rsp - - mov rcx, rdx - repe cmpsb - sete al - - leave - ret -memcmp_b: - push rbp - mov rbp, rsp - - mov al, sil - mov rcx, rdx - repe scasb - sete al - - leave - ret -strcmp: - push rbp - mov rbp, rsp - sub rsp, 0x8 - - push rdi - push rsi - call strlen - mov [rbp-8], rax - mov rdi, rsi - call strlen - pop rsi - pop rdi - cmp rax, [rbp-8] - sete al - jne .end - - mov rdx, [rbp-8] - call memcmp - -.end: - leave - ret -strlen: - push rbp - mov rbp, rsp - - xor rcx, rcx - not rcx - xor eax,eax - repne scasb - sub rax, rcx - sub rax, 2 - - leave - ret -strchr: - push rbp - mov rbp, rsp - - push rdi - call strlen - pop rdi - mov r8, rax - cld - mov rcx, rax - mov rax, rsi - repne scasb - sub r8, rcx - mov rax, r8 - - leave - ret +; oboskrnl/arch/x86_64/memmanip.asm +; +; Copyright (c) 2024 Omar Berrow + +[BITS 64] + +global _ZN4obos6memsetEPvjm +global _ZN4obos7memzeroEPvm +global _ZN4obos6memcpyEPvPKvm +global _ZN4obos6memcmpEPKvS1_m ; blk1,blk2,size +global _ZN4obos6memcmpEPKvim ; blk1, against, size +global _ZN4obos6strcmpEPKcS1_ +global _ZN4obos6strlenEPKc +global _ZN4obos6strchrEPKcc + +global memcmp +global memcpy +global memset + +memset: +_ZN4obos6memsetEPvjm: + push rbp + mov rbp, rsp + + push rdi + mov al, sil + mov rcx, rdx + rep stosb + pop rax + + leave + ret +_ZN4obos7memzeroEPvm: + push rbp + mov rbp, rsp + + push rdi + xor rax, rax + mov rcx, rsi + rep stosb + pop rax + + leave + ret +memcpy: +_ZN4obos6memcpyEPvPKvm: + push rbp + mov rbp, rsp + + mov rax, rdi + mov rcx, rdx + rep movsb + + leave + ret +memcmp: +_ZN4obos6memcmpEPKvS1_m: + push rbp + mov rbp, rsp + + mov rcx, rdx + repe cmpsb + sete al + + leave + ret +_ZN4obos6memcmpEPKvim: + push rbp + mov rbp, rsp + + mov al, sil + mov rcx, rdx + repe scasb + sete al + + leave + ret +_ZN4obos6strcmpEPKcS1_: + push rbp + mov rbp, rsp + sub rsp, 0x8 + + push rdi + push rsi + call _ZN4obos6strlenEPKc + mov [rbp-8], rax + mov rdi, rsi + call _ZN4obos6strlenEPKc + pop rsi + pop rdi + cmp rax, [rbp-8] + sete al + jne .end + + mov rdx, [rbp-8] + call _ZN4obos6memcmpEPKvS1_m + +.end: + leave + ret +_ZN4obos6strlenEPKc: + push rbp + mov rbp, rsp + + xor rcx, rcx + not rcx + xor rax,rax + repne scasb + sub rax, rcx + sub rax, 2 + + leave + ret +_ZN4obos6strchrEPKcc: + push rbp + mov rbp, rsp + + push rdi + call _ZN4obos6strlenEPKc + pop rdi + mov rcx, rax + mov al, sil + repne scasb + mov rax, rdi + dec rax + + leave + ret \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/mm/map.cpp b/src/oboskrnl/arch/x86_64/mm/map.cpp new file mode 100644 index 00000000..dee2c7c1 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/mm/map.cpp @@ -0,0 +1,382 @@ +/* + oboskrnl/arch/x86_64/mm/map.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include + +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include + +#include +#include +#include +#include + +#include + +#include + +#define GetCurrentPageMap() (PageMap*)getCR3() + +#define BIT(n) ((uintptr_t)1 << n) + +extern "C" uintptr_t KERNEL_BASE; +extern "C" uintptr_t KERNEL_TOP; + +namespace obos +{ + extern volatile limine_hhdm_request hhdm_offset; + extern uintptr_t hhdm_limit; + volatile limine_kernel_file_request kernel_file = { + .id = LIMINE_KERNEL_FILE_REQUEST, + .revision=0 + }; + volatile limine_kernel_address_request kernel_addr = { + .id = LIMINE_KERNEL_ADDRESS_REQUEST, + .revision=0 + }; + uintptr_t g_kernelBase = 0; + uintptr_t g_kernelTop = 0; + namespace arch + { + uintptr_t getKernelBase() + { + return ((uintptr_t)&KERNEL_BASE) & ~0xfff; + } + uintptr_t getKernelTop() + { + return (((uintptr_t)&KERNEL_TOP) + 0xfff) & ~0xfff; + } + OBOS_NO_KASAN uintptr_t DecodeProt(uintptr_t prot) + { + uintptr_t ret = 0; + if (!(prot & vmm::PROT_NO_DEMAND_PAGE)) + { + ret = BIT(9) | BIT(63); + if (prot & vmm::PROT_x86_64_WRITE_COMBINING_CACHE) + ret |= BIT(3) | BIT(7) /* Use PAT5 */; + if (prot & vmm::PROT_x86_64_WRITE_THROUGH_CACHE) + ret |= BIT(3) /* Use PAT1 */; + ret |= (prot & 0x7f) << 52; + return ret; + } + if (!(prot & vmm::PROT_READ_ONLY)) + ret |= BIT(1); + if (prot & vmm::PROT_USER) + ret |= BIT(2); + if (prot & vmm::PROT_CACHE_DISABLE) + ret |= BIT(4); + if (prot & vmm::PROT_x86_64_WRITE_COMBINING_CACHE) + ret |= BIT(3) | BIT(7) /* Use PAT5 */; + if (prot & vmm::PROT_x86_64_WRITE_THROUGH_CACHE) + ret |= BIT(3) /* Use PAT1 */; + if (!(prot & vmm::PROT_EXECUTE)) + ret |= BIT(63); + return ret; + } + OBOS_NO_KASAN uintptr_t DecodeEntry(uintptr_t entry) + { + uintptr_t ret = 0, flags = entry; + flags &= ~((((uintptr_t)1 << GetPhysicalAddressBits()) - 1) << 12); + if (flags & BIT(9)) + { + ret = (flags >> 52) & 0x7f; + if ((flags & BIT(3)) && (flags & BIT(7)) /* PAT5 */) + ret |= vmm::PROT_x86_64_WRITE_COMBINING_CACHE; + if ((flags & BIT(3)) /* PAT1 */) + ret |= vmm::PROT_x86_64_WRITE_THROUGH_CACHE; + return ret; + } + if (!(flags & BIT(1))) + ret |= vmm::PROT_READ_ONLY; + if (flags & BIT(2)) + ret |= vmm::PROT_USER; + if (flags & BIT(4)) + ret |= vmm::PROT_CACHE_DISABLE; + if ((flags & BIT(3)) && (flags & BIT(7))) + ret |= vmm::PROT_x86_64_WRITE_COMBINING_CACHE; + if ((flags & BIT(3))) + ret |= vmm::PROT_x86_64_WRITE_THROUGH_CACHE; + if (!(flags & BIT(63))) + ret |= vmm::PROT_EXECUTE; + return ret; + } + + OBOS_NO_KASAN void* map_page_to(vmm::Context* ctx, uintptr_t virt, uintptr_t phys, vmm::prot_t prot) + { + return map_page_to(ctx ? ctx->GetContext()->getCR3() : GetCurrentPageMap(), virt, phys, prot); + } + OBOS_NO_KASAN void* map_hugepage_to(vmm::Context* ctx, uintptr_t virt, uintptr_t phys, vmm::prot_t prot) + { + return map_hugepage_to(ctx ? ctx->GetContext()->getCR3() : GetCurrentPageMap(), virt, phys, prot); + } + + OBOS_NO_KASAN void unmap(vmm::Context* ctx, void* addr) + { + unmap(ctx ? ctx->GetContext()->getCR3() : GetCurrentPageMap(), addr); + } + + OBOS_NO_KASAN void get_page_descriptor(vmm::Context* ctx, void* addr, vmm::page_descriptor& out) + { + get_page_descriptor(ctx ? ctx->GetContext()->getCR3() : GetCurrentPageMap(), addr, out); + } + + OBOS_NO_KASAN void* map_page_to(PageMap* pm, uintptr_t virt, uintptr_t phys, vmm::prot_t prot) + { + if (!OBOS_IS_VIRT_ADDR_CANONICAL(virt)) + return nullptr; + if (phys > ((uintptr_t)1 << GetPhysicalAddressBits())) + return nullptr; + virt &= ~0xfff; + phys &= ~0xfff; + uintptr_t flags = DecodeProt(prot) | 1; + if (!(prot & vmm::PROT_NO_DEMAND_PAGE)) + flags |= (((uintptr_t)prot & 0x7f) << 52); + uintptr_t* pt = pm->AllocatePageMapAt(virt, flags); + pt[PageMap::AddressToIndex(virt, 0)] = phys | flags; + if (pm == GetCurrentPageMap()) + invlpg(virt); + return (void*)virt; + } + OBOS_NO_KASAN void* map_hugepage_to(PageMap* pm, uintptr_t virt, uintptr_t phys, vmm::prot_t prot) + { + if (!OBOS_IS_VIRT_ADDR_CANONICAL(virt)) + return nullptr; + if (phys & 0x1fffff) + return nullptr; + // Align to 2mib + virt &= ~0x1fffff; + uintptr_t flags = DecodeProt(prot) | 1; + // No need to clear the flag, as it'll just get added again. + if (flags & ((uintptr_t)1 << 7)) + flags |= ((uintptr_t)1<<12); + if (!(prot & vmm::PROT_NO_DEMAND_PAGE)) + flags |= (((uintptr_t)prot & 0x7f) << 52); + uintptr_t* pt = pm->AllocatePageMapAt(virt, flags, 2); + pt[PageMap::AddressToIndex(virt, 1)] = phys | flags | ((uintptr_t)1<<7); + if (pm == GetCurrentPageMap()) + invlpg(virt); + return (void*)virt; + } + OBOS_NO_KASAN void unmap(PageMap* pm, void* addr) + { + uintptr_t virt = (uintptr_t)addr; + if (!OBOS_IS_VIRT_ADDR_CANONICAL(addr)) + return; + uintptr_t l2Entry = pm->GetL2PageMapEntryAt(virt); + uintptr_t l1Entry = pm->GetL1PageMapEntryAt(virt); + bool isHugePage = l2Entry & BIT(7); + if (!l2Entry) + return; + if (!l1Entry && !isHugePage) + return; + uintptr_t* pt = (uintptr_t*)MapToHHDM(PageMap::MaskPhysicalAddressFromEntry(isHugePage ? pm->GetL3PageMapEntryAt(virt) : l2Entry)); + pt[PageMap::AddressToIndex(virt, (uint8_t)isHugePage)] = 0; + pm->FreePageMapAt(virt, 3 - (uint8_t)isHugePage); + invlpg(virt); + } + OBOS_NO_KASAN void get_page_descriptor(class PageMap* pm, void* addr, vmm::page_descriptor& out) + { + out.virt = ((uintptr_t)addr) & ~0xfff; + uintptr_t l2Entry = pm->GetL2PageMapEntryAt(out.virt); + uintptr_t l1Entry = pm->GetL1PageMapEntryAt(out.virt); + if (l2Entry & BIT(7)) + { + out.isHugePage = true; + out.present = true; + out.awaitingDemandPagingFault = (l2Entry & BIT(9)); + out.virt = ((uintptr_t)addr) & ~0x1f'ffff; + } + else + { + out.isHugePage = false; + out.present = (bool)(l1Entry & 1); + out.awaitingDemandPagingFault = (l1Entry & BIT(9)); + } + if (!out.present) + { + out.phys = 0; + out.awaitingDemandPagingFault = false; + out.protFlags = 0; + out.isHugePage = false; + return; + } + if (out.isHugePage) + { + bool patFlag = (l2Entry & BIT(12)); + if (!patFlag) + l2Entry &= ~BIT(7); + else + l2Entry &= ~BIT(12); // Bit 7 is already set, but bit 12 being set could cause some confusion, so we clear it. + } + out.protFlags = DecodeEntry(out.isHugePage ? l2Entry : l1Entry); + out.phys = PageMap::MaskPhysicalAddressFromEntry(out.isHugePage ? l2Entry : l1Entry) + ((uintptr_t)addr & (out.isHugePage ? 0x1f'ffff : 0xfff)); + } + + OBOS_NO_KASAN uintptr_t AllocatePhysicalPages(size_t nPages, bool alignToHugePageSize) + { + return ::obos::AllocatePhysicalPages(nPages, alignToHugePageSize); + } + OBOS_NO_KASAN void FreePhysicalPages(uintptr_t base, size_t nPages) + { + base &= ~(OBOS_PAGE_SIZE-1); + ::obos::FreePhysicalPages(base, nPages); + } + static void FreePageTables(uintptr_t* pm, uint8_t level, uint32_t beginIndex, uint32_t *indices) + { + if (!pm) + return; + pm = (uintptr_t*)MapToHHDM((uintptr_t)pm); + for (indices[level] = beginIndex; indices[level] < 512; indices[level]++) + { + if (!pm[indices[level]]) + continue; + if (pm[indices[level]] & BIT(7) || level == 0) + continue; + FreePageTables((uintptr_t*)PageMap::MaskPhysicalAddressFromEntry(pm[indices[level]]), level - 1, 0, indices); + FreePhysicalPages(PageMap::MaskPhysicalAddressFromEntry(pm[indices[level]]), 1); + } + } + static uintptr_t s_FBAddr = 0; + static void MapFramebuffer(PageMap* pm) + { + Framebuffer fb{}; + g_kernelConsole.GetFramebuffer(&fb, nullptr, nullptr); + uintptr_t fbPhys = (uintptr_t)fb.address - hhdm_offset.response->offset; + size_t fbSize = ((size_t)fb.height * (size_t)fb.pitch); + size_t nHugePagesInFB = fbSize / OBOS_HUGE_PAGE_SIZE; + size_t nLeftOverPagesInFb = (fbSize - nHugePagesInFB * OBOS_HUGE_PAGE_SIZE) / OBOS_PAGE_SIZE; + uintptr_t s_FBAddr = 0xffff'ff00'0000'0000; + uintptr_t addr = s_FBAddr; + s_FBAddr = s_FBAddr; + for (; addr < (s_FBAddr + nHugePagesInFB * OBOS_HUGE_PAGE_SIZE); addr += OBOS_HUGE_PAGE_SIZE) + map_hugepage_to(pm, addr, fbPhys + (addr - s_FBAddr), (uintptr_t)vmm::PROT_x86_64_WRITE_COMBINING_CACHE | (uintptr_t)vmm::PROT_NO_DEMAND_PAGE); + for (; addr < (s_FBAddr + nHugePagesInFB * OBOS_HUGE_PAGE_SIZE + nLeftOverPagesInFb * OBOS_PAGE_SIZE); addr += OBOS_PAGE_SIZE) + map_page_to(pm, addr, fbPhys + (addr - s_FBAddr), (uintptr_t)vmm::PROT_x86_64_WRITE_COMBINING_CACHE | (uintptr_t)vmm::PROT_NO_DEMAND_PAGE); + fb.address = (void*)s_FBAddr; + g_kernelConsole.SetFramebuffer(&fb, nullptr, true); + } + static void MapKernel(PageMap* pm) + { + uint8_t* kfile = (uint8_t*)kernel_file.response->kernel_file->address; + [[maybe_unused]] size_t kfileSize = kernel_file.response->kernel_file->size; + elf::Elf64_Ehdr* ehdr = (elf::Elf64_Ehdr*)kfile; + elf::Elf64_Phdr* firstPhdr = (elf::Elf64_Phdr*)(kfile + ehdr->e_phoff); + uintptr_t kernelBase = 0, kernelTop = 0; + for (size_t i = 0; i < ehdr->e_phnum; i++) + { + if (firstPhdr[i].p_type != elf::PT_LOAD) + continue; + if (firstPhdr[i].p_vaddr < kernelBase || !kernelBase) + kernelBase = firstPhdr[i].p_vaddr; + } + kernelTop = kernelBase; + for (size_t i = 0; i < ehdr->e_phnum; i++) + { + if (firstPhdr[i].p_type != elf::PT_LOAD) + continue; + uintptr_t prot = vmm::PROT_NO_DEMAND_PAGE; + if (firstPhdr[i].p_flags & elf::PF_X) + prot |= vmm::PROT_EXECUTE; + if (!(firstPhdr[i].p_flags & elf::PF_W)) + prot |= vmm::PROT_READ_ONLY; + uint32_t nPages = firstPhdr[i].p_memsz >> 12; + if ((firstPhdr[i].p_memsz % 4096) != 0) + nPages++; + uintptr_t base = firstPhdr[i].p_vaddr & ~0xfff; + if (((firstPhdr[i - 1].p_paddr + firstPhdr[i - 1].p_memsz) & ~0xfff) == base) + base += 0x1000; + kernelTop = (firstPhdr[i].p_vaddr + firstPhdr[i].p_memsz + 0xfff) & ~0xfff; + vmm::page_descriptor pd; + for (uintptr_t kBase = base; kBase < kernelTop; kBase += 4096) + { + get_page_descriptor((PageMap*)getCR3(), (void*)kBase, pd); + map_page_to(pm, kBase, pd.phys, prot); + } + } + g_kernelBase = kernelBase; + g_kernelTop = kernelTop; + } + static pg_context s_internalKernelContext; + static internal_context s_kernelCr3; + void InitializePageTables() + { + uintptr_t newPageMap = obos::AllocatePhysicalPages(1); + memzero(MapToHHDM(newPageMap), 4096); + PageMap* pm = (PageMap*)newPageMap; + for (uintptr_t i = hhdm_offset.response->offset; i < hhdm_limit; i += 0x200000) + map_hugepage_to(pm, i, i - hhdm_offset.response->offset, vmm::PROT_NO_DEMAND_PAGE); + MapKernel(pm); + PageMap* oldPageMap = nullptr; + __asm__ __volatile__("mov %%cr3, %0" : "=r"(oldPageMap) : : "memory"); + __asm__ __volatile__("mov %0, %%cr3" : :"r"(newPageMap) : "memory"); + // Reclaim old page tables. + uint32_t indices[4] = {}; + FreePageTables((uintptr_t*)oldPageMap, 3, PageMap::AddressToIndex(0xffff'8000'0000'0000, 3), indices); + FreePhysicalPages((uintptr_t)oldPageMap, 1); + MapFramebuffer(pm); + OptimizePMMFreeList(); + new (&s_internalKernelContext) pg_context{}; + new (&s_kernelCr3) internal_context{}; + s_kernelCr3.cr3 = pm; + s_kernelCr3.references = 1; + s_internalKernelContext.set(&s_kernelCr3); + new (&vmm::g_kernelContext) vmm::Context{ &s_internalKernelContext }; + } + OBOS_NO_KASAN void register_allocated_pages_in_context(vmm::Context* ctx) + { + // Register the HHDM in the context. + vmm::page_node node{}; + node.ctx = ctx; + node.nPageDescriptors = (hhdm_limit - hhdm_offset.response->offset) / OBOS_HUGE_PAGE_SIZE; + node.pageDescriptors = new vmm::page_descriptor[node.nPageDescriptors]; + size_t i = 0; + for (uintptr_t addr = hhdm_offset.response->offset; addr < hhdm_limit; addr += OBOS_HUGE_PAGE_SIZE, i++) + get_page_descriptor(ctx, (void*)addr, node.pageDescriptors[i]); + ctx->AppendPageNode(node); + // NOTE(oberrow,20/04/2024): I just realized that the framebuffer is being mapped but never marked as mapped. + // Whoopsie. + Framebuffer fb{}; + g_kernelConsole.GetFramebuffer(&fb, nullptr, nullptr); + uintptr_t fbPhys = (uintptr_t)fb.address - hhdm_offset.response->offset; + size_t fbSize = ((size_t)fb.height * (size_t)fb.pitch); + size_t nHugePagesInFB = fbSize / OBOS_HUGE_PAGE_SIZE; + size_t nLeftOverPagesInFb = (fbSize - nHugePagesInFB * OBOS_HUGE_PAGE_SIZE) / OBOS_PAGE_SIZE; + uintptr_t addr = s_FBAddr; + node.nPageDescriptors = nHugePagesInFB + nLeftOverPagesInFb; + node.pageDescriptors = new vmm::page_descriptor[node.nPageDescriptors]; + memzero(node.pageDescriptors, sizeof(vmm::page_descriptor) * node.nPageDescriptors); + for (i = 0; addr < (s_FBAddr + nHugePagesInFB * OBOS_HUGE_PAGE_SIZE); addr += OBOS_HUGE_PAGE_SIZE, i++) + { + node.pageDescriptors[i].protFlags = (uintptr_t)vmm::PROT_x86_64_WRITE_COMBINING_CACHE | (uintptr_t)vmm::PROT_NO_DEMAND_PAGE; + node.pageDescriptors[i].isHugePage = true; + node.pageDescriptors[i].phys = fbPhys + (addr-s_FBAddr); + node.pageDescriptors[i].virt = addr; + node.pageDescriptors[i].present = true; + node.pageDescriptors[i].awaitingDemandPagingFault = false; + } + for (; addr < (s_FBAddr + nHugePagesInFB * OBOS_HUGE_PAGE_SIZE + nLeftOverPagesInFb * OBOS_PAGE_SIZE); addr += OBOS_PAGE_SIZE, i++) + { + node.pageDescriptors[i].protFlags = (uintptr_t)vmm::PROT_x86_64_WRITE_COMBINING_CACHE | (uintptr_t)vmm::PROT_NO_DEMAND_PAGE; + node.pageDescriptors[i].isHugePage = false; + node.pageDescriptors[i].phys = fbPhys + (addr - s_FBAddr); + node.pageDescriptors[i].virt = addr; + node.pageDescriptors[i].present = true; + node.pageDescriptors[i].awaitingDemandPagingFault = false; + } + ctx->AppendPageNode(node); + } + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/mm/map.h b/src/oboskrnl/arch/x86_64/mm/map.h new file mode 100644 index 00000000..5cd50bff --- /dev/null +++ b/src/oboskrnl/arch/x86_64/mm/map.h @@ -0,0 +1,105 @@ +/* + oboskrnl/arch/x86_64/mm/map.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include +#include + +#include +#include +#include +#include + +namespace obos +{ + namespace arch + { + // External interface. + + /// + /// Maps a physical page to a virtual address. + /// + /// The context. This can be nullptr to specify the current context. + /// The virtual address to map to. + /// The physical page to map. + /// The protection flags. + /// The virtual address. + OBOS_EXPORT void* map_page_to(vmm::Context* ctx, uintptr_t virt, uintptr_t phys, vmm::prot_t prot); + /// + /// Maps a physical page to a virtual address as a huge page. + /// + /// The context. This can be nullptr to specify the current context. + /// The virtual address to map to. + /// The physical page to map. + /// The protection flags. + /// The virtual address. + OBOS_EXPORT void* map_hugepage_to(vmm::Context* ctx, uintptr_t virt, uintptr_t phys, vmm::prot_t prot); + + /// + /// Un-maps a page. + /// + /// The context. This can be nullptr to specify the current context. + /// The address to un-map. The underlying physical page is kept allocated. + OBOS_EXPORT void unmap(vmm::Context* ctx, void* addr); + /// + /// Makes a page descriptor from a virtual page. + /// + /// The context. This can be nullptr to specify the current context. + /// The virtual address to make a page descriptor from. + /// [out] The page descriptor to store the result in. + OBOS_EXPORT void get_page_descriptor(vmm::Context* ctx, void* addr, vmm::page_descriptor& out); + + /// + /// Registers any allocated pages that InitializeVMM() might not know about in the kernel context.
+ /// This is probably only to be called once in InitializeVMM(). + ///
+ /// The context. + void register_allocated_pages_in_context(vmm::Context* ctx); + + /// + /// Registers a page fault handler. + /// + /// The reason that triggers the callback. + /// Whether the fault has to be from user-mode thread. + /// The handler. + /// Whether the handler was registered (true) or not (false). + OBOS_EXPORT bool register_page_fault_handler(vmm::PageFaultReason reason, bool hasToBeInUserMode, void(*callback)(void* on, vmm::PageFaultErrorCode errorCode, const vmm::page_descriptor& pd)); + + // nPages is in units of OBOS_PAGE_SIZE. + // So to allocate/free one OBOS_PAGE_SIZE, you would pass one as nPages. + // As a note for anyone using x86-64 as a reference, these rules must be followed, or you'll have a bad time. + + /// + /// Allocates physical pages. + /// + /// The number of pages to allocate. + /// Whether to align to the huge page size (true), or not (false). + /// The physical address of the pages allocated. + OBOS_EXPORT uintptr_t AllocatePhysicalPages(size_t nPages, bool alignToHugePageSize); + /// + /// Frees physical pages. + /// + /// The base of the pages. + /// The amount of pages to free. + OBOS_EXPORT void FreePhysicalPages(uintptr_t base, size_t nPages); + + // Internal interface. + + void* map_page_to(class PageMap* pm, uintptr_t virt, uintptr_t phys, vmm::prot_t prot); + void* map_hugepage_to(class PageMap* pm, uintptr_t virt, uintptr_t phys, vmm::prot_t prot); + // This does not free the underlying physical page, but it does free the page structures. + // Un-maps a page mapped with map_* + void unmap(class PageMap* pm, void* addr); + void get_page_descriptor(class PageMap* pm, void* addr, vmm::page_descriptor& out); + + /// + /// Initializes the page tables of the kernel. + /// This sets up the HHDM, kernel memory, and framebuffer. + /// + void InitializePageTables(); + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/mm/palloc.cpp b/src/oboskrnl/arch/x86_64/mm/palloc.cpp new file mode 100644 index 00000000..ff977ada --- /dev/null +++ b/src/oboskrnl/arch/x86_64/mm/palloc.cpp @@ -0,0 +1,306 @@ +/* + oboskrnl/arch/x86_64/mm/palloc.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include + +#include +#include + +#include +#include + +#include + +#include + +#include + +#include + +namespace obos +{ +#define MAP_TO_HHDM(addr) (hhdm_offset.response->offset + (uintptr_t)(addr)) +#define CMAP_TO_HHDM(addr) reinterpret_cast(hhdm_offset.response->offset + (uintptr_t)(addr)) +#define REMOVE_FROM_HHDM(addr) ((uintptr_t)(addr) - hhdm_offset.response->offset) + volatile limine_memmap_request mmap_request = { + .id = LIMINE_MEMMAP_REQUEST, + .revision = 1 + }; + extern volatile limine_hhdm_request hhdm_offset; + uintptr_t hhdm_limit = 0; + struct MemoryNode + { + MemoryNode *next, *prev; + size_t nPages; + }; + MemoryNode *g_memoryHead, *g_memoryTail; + size_t g_nMemoryNodes; + size_t g_nPhysPagesUsed = 0; + bool g_pmmInitialized = false; + locks::SpinLock g_pmmLock; + static OBOS_NO_KASAN uintptr_t CalculateHHDMLimit() + { + auto lastMMAPEntry = mmap_request.response->entries[mmap_request.response->entry_count - 1]; + uintptr_t limit = hhdm_offset.response->offset + (uintptr_t)lastMMAPEntry->base + (lastMMAPEntry->length / 4096) * 4096; + vmm::page_descriptor pd{}; + arch::get_page_descriptor((vmm::Context*)nullptr, (void*)limit, pd); + size_t i = 1; + while (!pd.present) + { + lastMMAPEntry = mmap_request.response->entries[mmap_request.response->entry_count - ++i]; + if (lastMMAPEntry < *mmap_request.response->entries) + { + limit = hhdm_offset.response->offset; + break; + } + limit = hhdm_offset.response->offset + (uintptr_t)lastMMAPEntry->base + (lastMMAPEntry->length / 4096) * 4096; + arch::get_page_descriptor((vmm::Context*)nullptr, (void*)(limit - 0x1000), pd); + } + // Round the limit up. + limit += 0x200000; + limit &= ~(0x200000 - 1); + // Add one last big page to it... + limit += OBOS_HUGE_PAGE_SIZE; + return limit; + } + namespace arch + { + OBOS_NO_KASAN uintptr_t getHHDMLimit() + { + if (g_pmmInitialized) + return hhdm_limit; + return CalculateHHDMLimit(); + } + } + OBOS_NO_KASAN void InitializePMM() + { + if (g_pmmInitialized) + return; + new (&g_pmmLock) locks::SpinLock{ 0xf }; + g_pmmLock.Lock(); + for (size_t i = 0; i < mmap_request.response->entry_count; i++) + { + if (mmap_request.response->entries[i]->type == LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE || mmap_request.response->entries[i]->type == LIMINE_MEMMAP_KERNEL_AND_MODULES) + g_nPhysPagesUsed += ((mmap_request.response->entries[i]->length + 0xfff) & ~0xfff) >> 12; + if (mmap_request.response->entries[i]->type != LIMINE_MEMMAP_USABLE) + continue; + uintptr_t base = mmap_request.response->entries[i]->base; + if (base < 0x1000) + base = 0x1000; + if (base & 0xfff) + base = (base + 0xfff) & 0xfff; + size_t size = mmap_request.response->entries[i]->length & ~0xfff; + g_nMemoryNodes++; + MemoryNode* newNode = (MemoryNode*)MAP_TO_HHDM(base); + MemoryNode* newNodePhys = (MemoryNode*)base; + if (g_memoryTail) + ((MemoryNode*)MAP_TO_HHDM(g_memoryTail))->next = newNodePhys; + if (!g_memoryHead) + g_memoryHead = newNodePhys; + newNode->prev = g_memoryTail; + newNode->next = nullptr; + newNode->nPages = size / 0x1000; + g_memoryTail = newNodePhys; + } + g_pmmLock.Unlock(); + hhdm_limit = CalculateHHDMLimit(); + g_pmmInitialized = true; + } + + OBOS_NO_KASAN uintptr_t AllocatePhysicalPages(size_t nPages, bool align2MIB) + { + if (!g_nMemoryNodes) + logger::panic(nullptr, "No more available physical memory left.\n"); + g_pmmLock.Lock(); + MemoryNode* node = (MemoryNode*)MAP_TO_HHDM((uintptr_t*)g_memoryHead); + MemoryNode* nodePhys = g_memoryHead; + uintptr_t ret = (uintptr_t)g_memoryHead; + if (align2MIB && nPages & 0x1ff) + nPages = (nPages + 0x1ff) & ~0x1ff; + size_t nPagesRequired = nPages + (align2MIB ? (node->nPages & 0x1ff) : 0); + if (align2MIB && ((uintptr_t)nodePhys & 0x1f'ffff)) + nPagesRequired += ((uintptr_t)nodePhys & 0x1f'ffff) / 4096; + while (node->nPages < nPagesRequired) + { + node = node->next; + if (!node) + { + g_pmmLock.Unlock(); + return 0; // Not enough physical memory to satisfy request of nPages. + } + nodePhys = node; + node = (MemoryNode*)MAP_TO_HHDM((uintptr_t*)node); + nPagesRequired = nPages; + if (align2MIB) + { + nPagesRequired += (node->nPages & 0x1ff); + nPagesRequired += ((uintptr_t)nodePhys & 0x1f'ffff) / 4096; + } + } + node->nPages -= nPagesRequired; + if (!node->nPages) + { + // This node has no free pages after this allocation, so it should be removed. + MemoryNode* next = node->next, *prev = node->prev; + if (next) + ((MemoryNode*)MAP_TO_HHDM((uintptr_t*)next))->prev = prev; + if (prev) + ((MemoryNode*)MAP_TO_HHDM((uintptr_t*)prev))->next = next; + if (g_memoryHead == nodePhys) + g_memoryHead = next; + if (g_memoryTail == nodePhys) + g_memoryTail = prev; + node->next = nullptr; + node->prev = nullptr; + } + ret = (uintptr_t)nodePhys + node->nPages * 4096; + g_pmmLock.Unlock(); + __atomic_add_fetch(&g_nPhysPagesUsed, nPages, __ATOMIC_SEQ_CST); + return ret; + } + OBOS_NO_KASAN void FreePhysicalPages(uintptr_t addr, size_t nPages) + { + OBOS_ASSERTP(addr != 0, "Attempt free of physical address zero.\n"); + addr &= ~0xfff; + OBOS_ASSERTP((uintptr_t)addr != hhdm_limit, ""); + g_pmmLock.Lock(); + MemoryNode* node = (MemoryNode*)MAP_TO_HHDM((uintptr_t*)addr); + MemoryNode* nodePhys = (MemoryNode*)addr; + OBOS_ASSERTP((uintptr_t)g_memoryTail != hhdm_limit, ""); + if (g_memoryTail) + CMAP_TO_HHDM(g_memoryTail)->next = nodePhys; + if (!g_memoryHead) + g_memoryHead = nodePhys; + node->next = nullptr; + node->prev = g_memoryTail; + g_memoryTail = nodePhys; + node->nPages = nPages; + g_nMemoryNodes++; + g_pmmLock.Unlock(); + __atomic_sub_fetch(&g_nPhysPagesUsed, nPages, __ATOMIC_SEQ_CST); + } + static void swapNodes(MemoryNode* node, MemoryNode* nodePhys, MemoryNode* with, MemoryNode* withPhys) + { + if (!node || !with) + return; + struct MemoryNode* aPrev = node->prev; + struct MemoryNode* aNext = node->next; + struct MemoryNode* bPrev = with->prev; + struct MemoryNode* bNext = with->next; + if (aPrev == withPhys) + { + // Assuming the nodes are valid, bNext == node + node->prev = bPrev; + node->next = withPhys; + with->prev = nodePhys; + with->next = aNext; + if (bPrev) CMAP_TO_HHDM(bPrev)->next = nodePhys; + if (aNext) CMAP_TO_HHDM(aNext)->prev = withPhys; + } + else if (aNext == withPhys) + { + // Assuming the nodes are valid, bPrev == node + node->prev = withPhys; + node->next = bNext; + with->prev = aPrev; + with->next = nodePhys; + if (bNext) CMAP_TO_HHDM(bNext)->prev = nodePhys; + if (aPrev) CMAP_TO_HHDM(aPrev)->next = withPhys; + } + else + { + node->prev = bPrev; + node->next = bNext; + with->prev = aPrev; + with->next = aNext; + if (aPrev) CMAP_TO_HHDM(aPrev)->next = withPhys; + if (aNext) CMAP_TO_HHDM(aPrev)->prev = withPhys; + if (bPrev) CMAP_TO_HHDM(bPrev)->next = nodePhys; + if (bNext) CMAP_TO_HHDM(bNext)->prev = nodePhys; + } + if (g_memoryHead == withPhys) + g_memoryHead = nodePhys; + else if (g_memoryHead == nodePhys) + g_memoryHead = withPhys; + if (g_memoryTail == withPhys) + g_memoryTail = nodePhys; + else if (g_memoryTail == nodePhys) + g_memoryTail = withPhys; + } + OBOS_NO_KASAN void OptimizePMMFreeList() + { + g_pmmLock.Lock(); + MemoryNode* currentNode = CMAP_TO_HHDM(g_memoryHead); + uintptr_t currentNodePhys = (uintptr_t)currentNode - hhdm_offset.response->offset; + uintptr_t stepNodePhys = 0; + bool swapped = false; + // Sort the nodes into lowest address to highest address using bubble sort. + do + { + swapped = false; + currentNode = CMAP_TO_HHDM(g_memoryHead); + currentNodePhys = (uintptr_t)currentNode - hhdm_offset.response->offset; + if (!currentNodePhys) + break; + + while ((uintptr_t)currentNode->next != stepNodePhys) + { + if (!currentNodePhys) + break; + if (currentNodePhys > (uintptr_t)currentNode->next) + { + // Swap the nodes. + if (!currentNode->next) + break; + MemoryNode* nextNode = CMAP_TO_HHDM(currentNode->next); + swapNodes(currentNode, (MemoryNode*)currentNodePhys, nextNode, currentNode->next); + swapped = true; + } + currentNode = (MemoryNode*)MAP_TO_HHDM(currentNodePhys = (uintptr_t)currentNode->next); + if (!currentNodePhys) + break; + } + + if (!currentNodePhys) + break; + stepNodePhys = currentNodePhys; + } while (swapped); + currentNode = (MemoryNode*)MAP_TO_HHDM(((MemoryNode*)MAP_TO_HHDM(g_memoryHead))->next); + currentNodePhys = (uintptr_t)currentNode - hhdm_offset.response->offset; + while (currentNodePhys) + { + uintptr_t previousNodePhys = (uintptr_t)currentNode->prev; + MemoryNode* previousNode = (MemoryNode*)MAP_TO_HHDM(currentNode->prev); + OBOS_ASSERTP(previousNodePhys != 0, ""); + uintptr_t nextNodePhys = (uintptr_t)currentNode->next; + MemoryNode* nextNode = (MemoryNode*)MAP_TO_HHDM(currentNode->next); + // Two blocks that are continuous but in separate nodes. + if ((previousNodePhys + previousNode->nPages * 4096) == currentNodePhys) + { + // Combine them. + previousNode->nPages += currentNode->nPages; + if (nextNodePhys) + nextNode->prev = (MemoryNode*)previousNodePhys; + // It's guaranteed that previousNodePhys != 0 + previousNode->next = (MemoryNode*)nextNodePhys; + // Because of that guarantee, currentNode cannot be equal to g_memoryHead + /*if (currentNode == g_memoryHead) + g_memoryHead = currentNode->next;*/ + if (currentNodePhys == (uintptr_t)g_memoryTail) + g_memoryTail = (MemoryNode*)previousNodePhys; + g_nMemoryNodes--; + } + + currentNode = (MemoryNode*)MAP_TO_HHDM(currentNodePhys = (uintptr_t)currentNode->next); + } + g_pmmLock.Unlock(); + } + + OBOS_NO_KASAN void* MapToHHDM(uintptr_t phys) + { + return (void*)MAP_TO_HHDM(phys); + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/mm/palloc.h b/src/oboskrnl/arch/x86_64/mm/palloc.h new file mode 100644 index 00000000..4ac42ad0 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/mm/palloc.h @@ -0,0 +1,19 @@ +/* + oboskrnl/arch/x86_64/mm/palloc.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include +#include + +namespace obos +{ + OBOS_EXPORT uintptr_t AllocatePhysicalPages(size_t nPages, bool align2MIB = false); + OBOS_EXPORT void FreePhysicalPages(uintptr_t addr, size_t nPages); + OBOS_EXPORT void OptimizePMMFreeList(); + + OBOS_EXPORT void* MapToHHDM(uintptr_t phys); +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/mm/pmap_l4.cpp b/src/oboskrnl/arch/x86_64/mm/pmap_l4.cpp new file mode 100644 index 00000000..8b2cadd4 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/mm/pmap_l4.cpp @@ -0,0 +1,166 @@ +/* + oboskrnl/arch/x86_64/mm/pmap_l4.h + + Copyright (c) 2024 Omar Berrow +*/ + +#include +#include + +#include + +#include +#include + +#include + +namespace obos +{ + namespace arch + { + uintptr_t PageMap::m_physAddrMask = 0; + + size_t GetPhysicalAddressBits() + { + uint32_t eax = 0, unused = 0; + __cpuid__(0x80000008, 0, &eax, &unused, &unused, &unused); + return eax & 0xff; + } + size_t GetVirtualAddressBits() + { + uint32_t eax = 0, unused = 0; + __cpuid__(0x8000008, 0, &eax, &unused, &unused, &unused); + return (eax >> 8) & 0xff; + } + + OBOS_NO_KASAN uintptr_t PageMap::GetL4PageMapEntryAt(uintptr_t at) + { + uintptr_t* arr = (uintptr_t*)MapToHHDM(GetPageMap()); + return GetEntryAt(arr, at, 3); + } + OBOS_NO_KASAN uintptr_t PageMap::GetL3PageMapEntryAt(uintptr_t at) + { + uintptr_t* arr = (uintptr_t*)MapToHHDM(MaskPhysicalAddressFromEntry(GetL4PageMapEntryAt(at))); + if (arr == MapToHHDM(0)) + return 0; + return GetEntryAt(arr, at, 2); + } + OBOS_NO_KASAN uintptr_t PageMap::GetL2PageMapEntryAt(uintptr_t at) + { + uintptr_t* arr = (uintptr_t*)MapToHHDM(MaskPhysicalAddressFromEntry(GetL3PageMapEntryAt(at))); + if (arr == MapToHHDM(0)) + return 0; + return GetEntryAt(arr, at, 1); + } + OBOS_NO_KASAN uintptr_t PageMap::GetL1PageMapEntryAt(uintptr_t at) + { + if (GetL2PageMapEntryAt(at) & ((uintptr_t)1 << 7)) + return 0; + uintptr_t* arr = (uintptr_t*)MapToHHDM(MaskPhysicalAddressFromEntry(GetL2PageMapEntryAt(at))); + if (arr == MapToHHDM(0)) + return 0; + return GetEntryAt(arr, at, 0); + } + + OBOS_NO_KASAN uintptr_t* PageMap::AllocatePageMapAt(uintptr_t at, uintptr_t cpuFlags, uint8_t depth) + { + if (depth > 3 || depth == 0) + return nullptr; + if (!OBOS_IS_VIRT_ADDR_CANONICAL(at)) + return nullptr; + if (!m_physAddrMask) + m_physAddrMask = (((uint64_t)1 << GetPhysicalAddressBits()) - 1) << 12; + cpuFlags &= ~m_physAddrMask; + cpuFlags |= 1; + // Clear the caching flags. + cpuFlags &= ~(1<<3) & ~(1<<4) & ~(1<<7); + // Clear the avaliable bits in the flags. + cpuFlags &= ~0x07F0'0000'0000'0E00; + auto GetPageMapEntryForDepth = [&](uintptr_t addr, uint8_t depth)->uintptr_t + { + switch (depth) + { + case 1: + return GetL2PageMapEntryAt(addr); + case 2: + return GetL3PageMapEntryAt(addr); + case 3: + return GetL4PageMapEntryAt(addr); + default: + break; + } + return 0; + }; + for (uint8_t i = 3; i > (3 - depth); i--) + { + uintptr_t* pageMap = (uintptr_t*)MapToHHDM((i + 1) == 4 ? GetPageMap() : MaskPhysicalAddressFromEntry(GetPageMapEntryForDepth(at, i + 1))); + if (!pageMap[AddressToIndex(at, i)]) + { + uintptr_t newTable = AllocatePhysicalPages(1); + memzero(MapToHHDM(newTable), 4096); + pageMap[AddressToIndex(at, i)] = newTable | cpuFlags; + } + else + { + uintptr_t entry = (uintptr_t)pageMap[AddressToIndex(at, i)]; + if ((entry & ((uintptr_t)1 << 63)) && !(cpuFlags & ((uintptr_t)1 << 63))) + entry &= ~((uintptr_t)1 << 63); + if (!(entry & ((uintptr_t)1 << 2)) && (cpuFlags & ((uintptr_t)1 << 2))) + entry |= ((uintptr_t)1 << 2); + if (!(entry & ((uintptr_t)1 << 1)) && (cpuFlags & ((uintptr_t)1 << 1))) + entry |= ((uintptr_t)1 << 1); + pageMap[AddressToIndex(at, i)] = entry; + } + } + return (uintptr_t*)MapToHHDM(MaskPhysicalAddressFromEntry(GetPageMapEntryForDepth(at, (4-depth)))); + } + OBOS_NO_KASAN bool PageMap::FreePageMapAt(uintptr_t at, uint8_t maxDepth) + { + if (!OBOS_IS_VIRT_ADDR_CANONICAL(at)) + return false; + if (maxDepth > 3 || maxDepth == 0) + return false; + auto GetPageMapEntryForDepth = [&](uintptr_t addr, uint8_t depth)->uintptr_t + { + switch (depth) + { + case 1: + return GetL2PageMapEntryAt(addr); + case 2: + return GetL3PageMapEntryAt(addr); + case 3: + return GetL4PageMapEntryAt(addr); + case 4: + return GetPageMap(); + default: + break; + } + return 0; + }; + for (uint8_t i = (4 - maxDepth); i < 4; i++) + { + if (!(GetPageMapEntryForDepth(at, i + 1) & 1)) + continue; + uintptr_t* pageMap = (uintptr_t*)MapToHHDM(MaskPhysicalAddressFromEntry(GetPageMapEntryForDepth(at, i + 1))); + uintptr_t phys = MaskPhysicalAddressFromEntry(pageMap[AddressToIndex(at, i)]); + uintptr_t* subPageMap = (uintptr_t*)MapToHHDM(phys); + if (memcmp(subPageMap, (int)0, 4096)) + { + pageMap[AddressToIndex(at, i)] = 0; + FreePhysicalPages(phys, 1); + continue; + } + } + return true; + } + + OBOS_NO_KASAN uintptr_t PageMap::GetEntryAt(uintptr_t* arr, uintptr_t virt, uint8_t level) + { + if (!OBOS_IS_VIRT_ADDR_CANONICAL(arr)) + return 0; + if (!OBOS_IS_VIRT_ADDR_CANONICAL(&arr[AddressToIndex(virt, level)])) + return 0; + return arr[AddressToIndex(virt, level)]; + } + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/mm/pmap_l4.h b/src/oboskrnl/arch/x86_64/mm/pmap_l4.h new file mode 100644 index 00000000..aff45dae --- /dev/null +++ b/src/oboskrnl/arch/x86_64/mm/pmap_l4.h @@ -0,0 +1,51 @@ +/* + oboskrnl/arch/x86_64/mm/pmap_l4.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include +#include + +namespace obos +{ + namespace arch + { + OBOS_EXPORT size_t GetPhysicalAddressBits(); + OBOS_EXPORT size_t GetVirtualAddressBits(); + + class PageMap + { + public: + PageMap() = delete; + + // L4 -> Page Map + // L3 -> Page Directory Pointer + // L2 -> Page Directory + // L1 -> Page Table + // L0 -> Page Table Entry + + uintptr_t GetPageMap() { return (uintptr_t)this; } + uintptr_t GetL4PageMapEntryAt(uintptr_t at); // pageMap[addressToIndex(at, 3)]; + uintptr_t GetL3PageMapEntryAt(uintptr_t at); // getL4PageMapEntryAt()[addressToIndex(at,2)]; + uintptr_t GetL2PageMapEntryAt(uintptr_t at); // getL3PageMapEntryAt()[addressToIndex(at,1)]; + uintptr_t GetL1PageMapEntryAt(uintptr_t at); // getL2PageMapEntryAt()[addressToIndex(at,0)]; + + uintptr_t* AllocatePageMapAt(uintptr_t at, uintptr_t cpuFlags, uint8_t depth = 3); + bool FreePageMapAt(uintptr_t at, uint8_t maxDepth = 3); + + static uintptr_t MaskPhysicalAddressFromEntry(uintptr_t entry) + { + if (!m_physAddrMask) + m_physAddrMask = (((uint64_t)1 << GetPhysicalAddressBits()) - 1) << 12; + return entry & m_physAddrMask; + } + static size_t AddressToIndex(uintptr_t address, uint8_t level) { return (address >> (9 * level + 12)) & 0x1FF; } + private: + static uintptr_t m_physAddrMask; + static uintptr_t GetEntryAt(uintptr_t* arr, uintptr_t virt, uint8_t level); + }; + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/mm/vmm_context.cpp b/src/oboskrnl/arch/x86_64/mm/vmm_context.cpp new file mode 100644 index 00000000..4664ea9b --- /dev/null +++ b/src/oboskrnl/arch/x86_64/mm/vmm_context.cpp @@ -0,0 +1,41 @@ +/* + oboskrnl/arch/x86_64/mm/vmm_context.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include +#include + +#include +#include + +namespace obos +{ + namespace arch + { + bool pg_context::alloc() + { + m_context = new internal_context{}; + m_context->cr3 = (PageMap*)AllocatePhysicalPages(1, false); + m_context->references = 1; + return false; + } + bool pg_context::free() + { + if (!m_context) + return false; + if (!(--m_context->references)) + { + FreePhysicalPages((uintptr_t)m_context->cr3, 1); + delete m_context; + } + m_context = nullptr; + return true; + } + pg_context::~pg_context() + { + free(); + } + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/mm/vmm_context.h b/src/oboskrnl/arch/x86_64/mm/vmm_context.h new file mode 100644 index 00000000..0ef153c8 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/mm/vmm_context.h @@ -0,0 +1,40 @@ +/* + oboskrnl/arch/x86_64/mm/vmm_context.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include + +#include + +namespace obos +{ + namespace arch + { + struct internal_context + { + PageMap* cr3; + size_t references; + }; + class pg_context + { + public: + pg_context() = default; + + void set(internal_context* ctx) + { m_context = ctx; } + + bool alloc(); + bool free(); + + PageMap* getCR3() const { if (!m_context) return nullptr; return m_context->cr3; } + + ~pg_context(); + private: + internal_context* m_context = nullptr; + }; + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/mtrr.c b/src/oboskrnl/arch/x86_64/mtrr.c deleted file mode 100644 index 7cb0777a..00000000 --- a/src/oboskrnl/arch/x86_64/mtrr.c +++ /dev/null @@ -1,91 +0,0 @@ -/* - * oboskrnl/arch/x86_64/mtrr.c - * - * Copyright (c) 2024 Omar Berrow - */ - -#include -#include - -#include - -#include -#include - -#include - -#define IA32_MTRRCAP (0xFE) -#define IA32_MTRRdefType (0x2FF) -#define IA32_MTRRphysBase(n) (0x200 + (n) * 2) -#define IA32_MTRRphysMask(n) (0x201 + (n) * 2) -#define MTRRfix64K_00000 (0x250) -#define MTRRfix16K_80000 (0x258) -#define MTRRfix16K_A0000 (0x259) -#define MTRRfix4K_C0000 (0x268) -#define MTRRfix4K_C8000 (0x269) -#define MTRRfix4K_D0000 (0x26A) -#define MTRRfix4K_D8000 (0x26B) -#define MTRRfix4K_E0000 (0x26C) -#define MTRRfix4K_E8000 (0x26D) -#define MTRRfix4K_F0000 (0x26E) -#define MTRRfix4K_F8000 (0x26F) - -static bool savedMtrrs = false; -static uint8_t nVariableMTRRs = 0; -static uint64_t *variableMTRRsBase; -static uint64_t *variableMTRRsMask; -static uint64_t deftype = 0; -static uint64_t fix64K = 0; -static uint64_t fix16K[2] = {}; -static uint64_t fix4K[8] = {}; -// Only to be called once on the BSP -void Arch_SaveMTRRs() -{ - OBOS_ASSERT(CoreS_GetCPULocalPtr()->isBSP); - OBOS_ASSERT(!savedMtrrs); - savedMtrrs = true; - uint64_t cap = rdmsr(IA32_MTRRCAP); - deftype = rdmsr(IA32_MTRRdefType); - if (~deftype & BIT(11) /* enabled? */) - return; // nope, abort. - nVariableMTRRs = cap & 0xff; - variableMTRRsBase = ZeroAllocate(OBOS_KernelAllocator, nVariableMTRRs, sizeof(uint64_t), nullptr); - variableMTRRsMask = ZeroAllocate(OBOS_KernelAllocator, nVariableMTRRs, sizeof(uint64_t), nullptr); - - for (size_t i = 0; i < nVariableMTRRs; i++) - { - variableMTRRsBase[i] = rdmsr(IA32_MTRRphysBase(i)); - variableMTRRsMask[i] = rdmsr(IA32_MTRRphysMask(i)); - } - - if (deftype & BIT(8) /* fixed avaliable? */) - { - fix64K = rdmsr(MTRRfix64K_00000); - fix16K[0] = rdmsr(MTRRfix16K_80000); - fix16K[1] = rdmsr(MTRRfix16K_A0000); - for (size_t i = 0; i < 8; i++) - fix4K[i] = rdmsr(MTRRfix4K_C0000 + i); - } -} -// Can be called on any CPU any amount of times. -void Arch_RestoreMTRRs() -{ - OBOS_ASSERT(savedMtrrs); - if (~deftype & BIT(11) /* enabled? */) - return; // nope, abort. - - for (size_t i = 0; i < nVariableMTRRs; i++) - { - wrmsr(IA32_MTRRphysBase(i), variableMTRRsBase[i]); - wrmsr(IA32_MTRRphysMask(i), variableMTRRsMask[i] ); - } - - if (deftype & BIT(8) /* fixed avaliable? */) - { - wrmsr(MTRRfix64K_00000, fix64K ); - wrmsr(MTRRfix16K_80000, fix16K[0]); - wrmsr(MTRRfix16K_A0000, fix16K[1]); - for (size_t i = 0; i < 8; i++) - wrmsr(MTRRfix4K_C0000 + i, fix4K[i]); - } -} diff --git a/src/oboskrnl/arch/x86_64/mtrr.h b/src/oboskrnl/arch/x86_64/mtrr.h deleted file mode 100644 index b1c5cadd..00000000 --- a/src/oboskrnl/arch/x86_64/mtrr.h +++ /dev/null @@ -1,14 +0,0 @@ -/* - * oboskrnl/arch/x86_64/mtrr.h - * - * Copyright (c) 2024 Omar Berrow - */ - -#pragma once - -#include - -// Only to be called once on the BSP -void Arch_SaveMTRRs(); -// Can be called on any CPU any amount of times. -void Arch_RestoreMTRRs(); diff --git a/src/oboskrnl/arch/x86_64/pci.c b/src/oboskrnl/arch/x86_64/pci.c deleted file mode 100644 index b0203cdf..00000000 --- a/src/oboskrnl/arch/x86_64/pci.c +++ /dev/null @@ -1,313 +0,0 @@ -/* - * oboskrnl/arch/x86_64/pci.h - * - * Copyright (c) 2024-2025 Omar Berrow -*/ - -#include -#include -#include - -#include - -#include -#include - -#include -#include -#include -#include -#include - -// Funny how these same functions for io from PCI registers have been in use since OBOS Rewrite #2 -// (Branch old_code2 in OBOS-dev/obos-old) - -static uint32_t pciAddress(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) -{ - func &= 7; - slot &= 31; - uint32_t address = 0; - uint32_t lbus = (uint32_t)bus; - uint32_t lslot = (uint32_t)slot; - uint32_t lfunc = (uint32_t)func; - address |= (offset & ~0b11); - address |= (lfunc<<8); - address |= (lslot<<11); - address |= (lbus<<16); - address |= BIT(31); - return address; -} - -void pciWriteByteRegister(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint8_t data) -{ - uint32_t address = pciAddress(bus, slot, func, offset); - outd(0xCF8, address); - outb(0xCFC, data); -} -void pciWriteWordRegister(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint16_t data) -{ - uint32_t address = pciAddress(bus, slot, func, offset); - outd(0xCF8, address); - outw(0xCFC, data); -} -void pciWriteDwordRegister(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint32_t data) -{ - uint32_t address = pciAddress(bus, slot, func, offset); - outd(0xCF8, address); - outd(0xCFC, data); -} -uint8_t pciReadByteRegister(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) -{ - uint32_t address = pciAddress(bus, slot, func, offset); - outd(0xCF8, address); - - uint8_t ret = (uint8_t)((ind(0xCFC) >> ((offset & 2) * 8)) & 0xFFFFFF); - return ret; -} -uint16_t pciReadWordRegister(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) -{ - uint32_t address = pciAddress(bus, slot, func, offset); - - outd(0xCF8, address); - - uint16_t ret = (uint16_t)((ind(0xCFC) >> ((offset & 2) * 8)) & 0xFFFF); - return ret; -} -uint32_t pciReadDwordRegister(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) -{ - uint32_t address = pciAddress(bus, slot, func, offset); - outd(0xCF8, address); - - return ((ind(0xCFC) >> ((offset & 2) * 8))); -} -OBOS_EXPORT obos_status DrvS_EnumeratePCI(uint8_t bus, pci_iteration_decision(*cb)(void* udata, pci_device_location device), void *cb_udata) -{ - int16_t currentSlot = 0; - for (; currentSlot < 32; currentSlot++) - { - for (int function = 0; function < 8; function++) - { - if (pciReadWordRegister(bus, currentSlot, function, 0) == 0xFFFF) - { - if (!function) - break; - continue; // There is no device here. - } - pci_device_location loc = { - .bus = bus, - .slot = currentSlot, - .function = function, - }; - if (!cb(cb_udata, loc)) - break; - if(!function) - if ((pciReadByteRegister(bus, currentSlot, 0, 15) & 0x80) != 0x80) - break; - } - } - return OBOS_STATUS_SUCCESS; -} -OBOS_EXPORT obos_status DrvS_ReadPCIRegister(pci_device_location loc, uint8_t offset, size_t accessSize, uint64_t* val) -{ - if (!val) - return OBOS_STATUS_INVALID_ARGUMENT; - if (loc.function > 7) - return OBOS_STATUS_INVALID_ARGUMENT; - if (loc.slot > 31) - return OBOS_STATUS_INVALID_ARGUMENT; - switch (accessSize) - { - case 1: - *val = pciReadByteRegister(loc.bus, loc.slot, loc.function, offset); - break; - case 2: - *val = pciReadWordRegister(loc.bus, loc.slot, loc.function, offset); - break; - case 4: - *val = pciReadDwordRegister(loc.bus, loc.slot, loc.function, offset); - break; - default: - return OBOS_STATUS_INVALID_ARGUMENT; - } - return OBOS_STATUS_SUCCESS; -} -OBOS_EXPORT obos_status DrvS_WritePCIRegister(pci_device_location loc, uint8_t offset, size_t accessSize, uint64_t val) -{ - if (loc.function > 7) - return OBOS_STATUS_INVALID_ARGUMENT; - if (loc.slot > 31) - return OBOS_STATUS_INVALID_ARGUMENT; - switch (accessSize) - { - case 1: - pciWriteByteRegister(loc.bus, loc.slot, loc.function, offset, val); - break; - case 2: - pciWriteWordRegister(loc.bus, loc.slot, loc.function, offset, val); - break; - case 4: - pciWriteDwordRegister(loc.bus, loc.slot, loc.function, offset, val); - break; - default: - return OBOS_STATUS_INVALID_ARGUMENT; - } - return OBOS_STATUS_SUCCESS; -} -OBOS_EXPORT size_t DrvS_GetBarSize(pci_device_location loc, uint8_t bar_index, bool is64bit, obos_status* status) -{ - const uint8_t bus = loc.bus; - const uint8_t slot = loc.slot; - const uint8_t function = loc.function; - if (status) - *status = OBOS_STATUS_SUCCESS; - if (bar_index > 5+(!is64bit)) - { - if (status) - *status = OBOS_STATUS_INVALID_ARGUMENT; - return (size_t)-1; - } - uint32_t bar = pciReadDwordRegister(bus, slot, function, (4+bar_index)*4) & 0xfffffff0; - pciWriteDwordRegister(bus, slot, function, (4+bar_index)*4, 0xFFFFFFFF); - size_t size = (~pciReadDwordRegister(bus, slot, function, (4+bar_index)*4) & 0xfffffff0) + 1; - // size = ((size >> 12) + 1) << 12; - pciWriteDwordRegister(bus, slot, function, (4+bar_index)*4, bar); - return size; -} -uint64_t DrvS_MSIAddressAndData(uint64_t* data, irq_vector_id vec, uint32_t processor, bool edgetrigger, bool deassert) -{ - if (!data) - return 0; - vec += 0x20; - // Shamelessly stolen from the osdev wiki. - *data = (vec & 0xFF) | (edgetrigger == 1 ? 0 : (1 << 15)) | (deassert == 1 ? 0 : (1 << 14)); - return (0xFEE00000 | (processor << 12)); -} -uacpi_iteration_decision pci_bus_match(void *user, uacpi_namespace_node *node, uint32_t max_depth) -{ - OBOS_UNUSED(max_depth); - uint64_t curr_bus = 0; - // Evaluate _BBN - uint64_t obj = 0; - uacpi_status status = uacpi_eval_simple_integer(node, "_BBN", &obj); - if (uacpi_unlikely(status == UACPI_STATUS_NOT_FOUND)) - curr_bus = 0; - else - return UACPI_ITERATION_DECISION_CONTINUE; - uintptr_t* udata = user; - if (udata[0] != curr_bus) - return UACPI_ITERATION_DECISION_CONTINUE; - uacpi_namespace_node** pNode = (void*)udata[1]; - *pNode = node; - return UACPI_ITERATION_DECISION_BREAK; -} -obos_status DrvS_RegisterIRQPin(const pci_device_location* dev, uint32_t* handle, irq_vector_id vector) -{ - if (!dev || !handle) - return OBOS_STATUS_INVALID_ARGUMENT; - uint8_t int_pin = pciReadByteRegister(dev->bus, dev->slot, dev->function, 0x3c+1); - if (!int_pin) - return OBOS_STATUS_NOT_FOUND; // No INT Pin :( - // Get PCI bus. - uacpi_namespace_node* pciBus = nullptr; - uintptr_t udata[2] = { - (uintptr_t)dev->bus, - (uintptr_t)&pciBus - }; - uacpi_find_devices("PNP0A03", pci_bus_match, udata); - if (!pciBus) - return OBOS_STATUS_NOT_FOUND; - uacpi_pci_routing_table *pci_routing_table = nullptr; - uacpi_status ustat = uacpi_get_pci_routing_table(pciBus, &pci_routing_table); - if (uacpi_unlikely_error(ustat)) - return OBOS_STATUS_NOT_FOUND; - bool wasGoodMatch = false; - ioapic_trigger_mode triggerMode = 0; - bool polarity = false; - uint32_t gsi = 0; - for (size_t i = 0, pi = 0; i < pci_routing_table->num_entries; i++, pi++) - { - if (pci_routing_table->entries[i].pin != (int_pin - 1)) - continue; - // OBOS_Debug("Found PIN%c.\n", (int_pin - 1) + 'A'); - uint16_t function = (pci_routing_table->entries[i].address & 0xffff); - uint16_t slot = (pci_routing_table->entries[i].address >> 16); - // OBOS_Debug("slot: 0x%04x\n", slot); - // OBOS_Debug("function: 0x%04x\n", function); - if (slot != dev->slot && (function != dev->function || function == 0xffff)) - continue; - if (pci_routing_table->entries[i].source == 0) - gsi = pci_routing_table->entries[i].index; - else - { - uacpi_resources* resources = nullptr; - uacpi_get_current_resources(pci_routing_table->entries[i].source, &resources); - - switch (resources->entries[pci_routing_table->entries[i].index].type) - { - case UACPI_RESOURCE_TYPE_IRQ: - gsi = resources->entries[pci_routing_table->entries[i].index].irq.irqs[0]; - polarity = resources->entries[pci_routing_table->entries[i].index].irq.polarity == UACPI_POLARITY_ACTIVE_LOW ? - true : false; - triggerMode = resources->entries[pci_routing_table->entries[i].index].irq.triggering == UACPI_TRIGGERING_EDGE ? - TriggerModeEdgeSensitive : TriggerModeLevelSensitive; - break; - case UACPI_RESOURCE_TYPE_EXTENDED_IRQ: - gsi = resources->entries[pci_routing_table->entries[i].index].extended_irq.irqs[0]; - polarity = resources->entries[pci_routing_table->entries[i].index].extended_irq.polarity == UACPI_POLARITY_ACTIVE_LOW ? - true : false; - triggerMode = resources->entries[pci_routing_table->entries[i].index].extended_irq.triggering == UACPI_TRIGGERING_EDGE ? - TriggerModeEdgeSensitive : TriggerModeLevelSensitive; - break; - default: - OBOS_ASSERT(false && "Invalid resource type"); - break; - } - wasGoodMatch = (function != 0xffff); - uacpi_free_resources(resources); - if (wasGoodMatch) - break; - } - break; - } - uacpi_free_pci_routing_table(pci_routing_table); - Arch_IOAPICMapIRQToVector(gsi, vector+0x20, polarity, triggerMode); - *handle = gsi; - return OBOS_STATUS_SUCCESS; -} -obos_status DrvS_MaskIRQPin(uint32_t handle, bool mask) -{ - // handle is the GSI. - return Arch_IOAPICMaskIRQ(handle & 0xffffffff, mask); -} - -OBOS_EXPORT obos_status DrvS_WriteIOSpaceBar(pci_bar* bar, uint16_t offset, uint32_t val, uint8_t byte_width) -{ - if (!bar) - return OBOS_STATUS_INVALID_ARGUMENT; - if (bar->type != PCI_BARIO) - return OBOS_STATUS_INVALID_ARGUMENT; - switch (byte_width) - { - case 1: outb(bar->iospace+offset, val&0xff); break; - case 2: outw(bar->iospace+offset, val&0xffff); break; - case 4: outd(bar->iospace+offset, val&0xffffffff); break; - default: return OBOS_STATUS_INVALID_ARGUMENT; - } - return OBOS_STATUS_SUCCESS; -} - -OBOS_EXPORT obos_status DrvS_ReadIOSpaceBar(pci_bar* bar, uint16_t offset, uint32_t *val, uint8_t byte_width) -{ - if (!bar || !val) - return OBOS_STATUS_INVALID_ARGUMENT; - if (bar->type != PCI_BARIO) - return OBOS_STATUS_INVALID_ARGUMENT; - switch (byte_width) - { - case 1: *val = inb(bar->iospace+offset); break; - case 2: *val = inw(bar->iospace+offset); break; - case 4: *val = ind(bar->iospace+offset); break; - default: return OBOS_STATUS_INVALID_ARGUMENT; - } - return OBOS_STATUS_SUCCESS; -} diff --git a/src/oboskrnl/arch/x86_64/pmm.c b/src/oboskrnl/arch/x86_64/pmm.c deleted file mode 100644 index 5654d4e1..00000000 --- a/src/oboskrnl/arch/x86_64/pmm.c +++ /dev/null @@ -1,81 +0,0 @@ -/* - oboskrnl/arch/x86_64/pmm.c - - Copyright (c) 2024 Omar Berrow -*/ - -#include -#include -#include -#include - -#include - -#if !OBOS_USE_LIMINE -#include - -extern struct ultra_memory_map_attribute* Arch_MemoryMap; -extern struct ultra_platform_info_attribute* Arch_LdrPlatformInfo; -obos_pmem_map_entry* MmS_GetFirstPMemMapEntry(uintptr_t* index) -{ - *index = 0; - return &Arch_MemoryMap->entries[0]; -} -// returns nullptr at the end of the list. -obos_pmem_map_entry* MmS_GetNextPMemMapEntry(obos_pmem_map_entry* current, uintptr_t* index) -{ - OBOS_UNUSED(current); - size_t nEntries = (Arch_MemoryMap->header.size - sizeof(Arch_MemoryMap->header)) / sizeof(struct ultra_memory_map_entry); - if (!nEntries) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "No memory map entries.\n"); - if ((*index) > nEntries) - return nullptr; - return &Arch_MemoryMap->entries[++(*index)]; -} -#define MAP_TO_HHDM(addr, type) ((type*)(Arch_LdrPlatformInfo->higher_half_base + (uintptr_t)(addr))) -#define UNMAP_FROM_HHDM(addr) ((uintptr_t)(addr) - Arch_LdrPlatformInfo->higher_half_base) - -#else - -#include - -obos_pmem_map_entry* MmS_GetFirstPMemMapEntry(uintptr_t* index) -{ - *index = 0; - return Arch_LimineMemmapRequest.response->entries[0]; -} -// returns nullptr at the end of the list. -obos_pmem_map_entry* MmS_GetNextPMemMapEntry(obos_pmem_map_entry* current, uintptr_t* index) -{ - OBOS_UNUSED(current); - size_t nEntries = Arch_LimineMemmapRequest.response->entry_count; - if (!nEntries) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "No memory map entries.\n"); - if ((*index) >= nEntries) - return nullptr; - return Arch_LimineMemmapRequest.response->entries[++(*index)]; -} - -#define MAP_TO_HHDM(addr, type) ((type*)(Arch_LimineHHDMRequest.response->offset + (uintptr_t)(addr))) -#define UNMAP_FROM_HHDM(addr) ((uintptr_t)(addr) - Arch_LimineHHDMRequest.response->offset) - -#endif - -OBOS_NO_KASAN OBOS_NO_UBSAN __attribute__((no_instrument_function)) void* Arch_MapToHHDM(uintptr_t phys) -{ - return MAP_TO_HHDM(phys, void); -} -OBOS_NO_KASAN OBOS_NO_UBSAN __attribute__((no_instrument_function)) uintptr_t Arch_UnmapFromHHDM(void* virt) -{ - return UNMAP_FROM_HHDM(virt); -} - -__attribute__((no_instrument_function)) OBOS_EXPORT void* MmS_MapVirtFromPhys(uintptr_t addr) -{ - return Arch_MapToHHDM(addr); -} - -__attribute__((no_instrument_function)) OBOS_EXPORT uintptr_t MmS_UnmapVirtFromPhys(void* virt) -{ - return Arch_UnmapFromHHDM(virt); -} diff --git a/src/oboskrnl/arch/x86_64/pmm.h b/src/oboskrnl/arch/x86_64/pmm.h deleted file mode 100644 index cec0032f..00000000 --- a/src/oboskrnl/arch/x86_64/pmm.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - oboskrnl/arch/x86_64/pmm.h - - Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include - -#if !OBOS_USE_LIMINE - -#include - -typedef struct ultra_memory_map_entry obos_pmem_map_entry; -#define pmem_map_base physical_address -#define pmem_map_size size -#define pmem_map_type type -#define PHYSICAL_MEMORY_TYPE_USABLE ULTRA_MEMORY_TYPE_FREE -#define PHYSICAL_MEMORY_TYPE_RECLAIMABLE ULTRA_MEMORY_TYPE_RECLAIMABLE -#define PHYSICAL_MEMORY_TYPE_LOADER_RECLAIMABLE ULTRA_MEMORY_TYPE_LOADER_RECLAIMABLE - -#else - -#include - -typedef struct limine_memmap_entry obos_pmem_map_entry; -#define pmem_map_base base -#define pmem_map_size length -#define pmem_map_type type -#define PHYSICAL_MEMORY_TYPE_USABLE LIMINE_MEMMAP_USABLE -#define PHYSICAL_MEMORY_TYPE_RECLAIMABLE LIMINE_MEMMAP_ACPI_RECLAIMABLE -#define PHYSICAL_MEMORY_TYPE_LOADER_RECLAIMABLE LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE - -#endif - -void* Arch_MapToHHDM(uintptr_t phys); -uintptr_t Arch_UnmapFromHHDM(void* virt); \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/sched_timer.cpp b/src/oboskrnl/arch/x86_64/sched_timer.cpp new file mode 100644 index 00000000..79d42b0d --- /dev/null +++ b/src/oboskrnl/arch/x86_64/sched_timer.cpp @@ -0,0 +1,109 @@ +/* + oboskrnl/arch/x86_64/sched_timer.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include +#include + +#include + +#include +#include + +#include +#include + +#include + +#include + +namespace obos +{ + namespace arch + { + extern Irq g_ipiIrq; + uint64_t g_hpetFrequency = 0; + extern "C" uint64_t calibrateHPET(uint64_t freq) + { + if (!g_hpetFrequency) + g_hpetFrequency = 1000000000000000/g_hpetAddress->generalCapabilitiesAndID.counterCLKPeriod; + g_hpetAddress->generalConfig &= ~(1<<0); + uint64_t compValue = g_hpetAddress->mainCounterValue + (g_hpetFrequency/freq); + g_hpetAddress->timer0.timerConfigAndCapabilities &= ~(1<<2); + g_hpetAddress->timer0.timerConfigAndCapabilities &= ~(1<<3); + return compValue; + } + // Userdata format: + // uint64_t[3]: + // [0]: Frequency in hertz + // [1]: The initial hpet counter value. + // [2]: The expected hpet counter value. + bool LAPICTimerIRQChecker(const Irq*, const struct IrqVector*, void* _udata) + { + uintptr_t* udata = (uintptr_t*)_udata; + const uint64_t &freq = udata[0]; + uint64_t &lastHPETCounter = udata[1]; + uint64_t &expectedHPETCounter = udata[2]; + g_hpetAddress->generalConfig &= ~(1<<0); + bool status = false; + lastHPETCounter = g_hpetAddress->mainCounterValue; + if (lastHPETCounter >= expectedHPETCounter) + { + // It is likely that this is a LAPIC timer interrupt, as the amount of time passed since the last interrupt on the IRQ + // is equal or greater to the frequency of the LAPIC timer. + status = expectedHPETCounter != 0; + expectedHPETCounter = lastHPETCounter + (g_hpetFrequency/freq) - (g_hpetFrequency/(freq*2)); + } + g_hpetAddress->generalConfig |= (1<<0); + return status; + } + void StartTimerOnCPU(scheduler::cpu_local* cpu, uint64_t freqHz, Irq& irq) + { + if (!g_hpetFrequency) + g_hpetFrequency = 1000000000000000/g_hpetAddress->generalCapabilitiesAndID.counterCLKPeriod; + auto udata = new uint64_t[3]; + g_hpetAddress->generalConfig &= ~(1<<0); + udata[0] = freqHz; + udata[1] = g_hpetAddress->mainCounterValue; + udata[2] = udata[1] + (g_hpetFrequency/freqHz); + g_hpetAddress->generalConfig |= (1<<0); + irq.SetIRQChecker(LAPICTimerIRQChecker, udata); + // Setup the IPI. + ipi* tIPI = new ipi{}; + timer_ipi* payload = new timer_ipi{}; + tIPI->data.timer = payload; + tIPI->type = ipi::IPI_TIMER; + // Set the IRQ that the timer shall happen on. + payload->irq = &irq; + // Set the target CPU's timer frequency. + payload->freq = freqHz; + // The scheduler expects the timer to be periodic. + payload->singleShoot = false; + // Add the IPI to the queue and call it. + cpu->archSpecific.ipi_queue.push(tIPI); + uint8_t oldIRQL = IRQL_INVALID; + if (cpu == scheduler::GetCPUPtr() && GetIRQL() >= IRQL_IPI_DISPATCH) + { + // We need to temporarily lower the IRQL for self-ipis, as otherwise we would deadlock while waiting. + oldIRQL = GetIRQL(); + LowerIRQL(IRQL_DISPATCH); + } + LAPIC_SendIPI(DestinationShorthand::None, DeliveryMode::Fixed, g_ipiIrq.GetVector() + 0x20, cpu->cpuId); + // Wait for the IPI to be processed. + // We do this as we need to free the structures, and we don't want to cause a race condition. + while(!tIPI->processed) + pause(); + if (oldIRQL != IRQL_INVALID) + { + uint8_t _oldIRQL = 0; + RaiseIRQL(oldIRQL, &_oldIRQL); + } + // After the IPI has been processed, we must clean up after ourselves. + // The IPI isn't in the queue anymore, so we just have to free the payload and ipi object. + delete tIPI; + delete payload; + } + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/sched_timer.h b/src/oboskrnl/arch/x86_64/sched_timer.h new file mode 100644 index 00000000..d8a8f76d --- /dev/null +++ b/src/oboskrnl/arch/x86_64/sched_timer.h @@ -0,0 +1,21 @@ +/* + oboskrnl/arch/x86_64/sched_timer.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include + +#include + +#include + +namespace obos +{ + namespace arch + { + void StartTimerOnCPU(scheduler::cpu_local* cpu, uint64_t freqHz, Irq& irq); + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/sdt.cpp b/src/oboskrnl/arch/x86_64/sdt.cpp new file mode 100644 index 00000000..0226a419 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/sdt.cpp @@ -0,0 +1,61 @@ +/* + oboskrnl/arch/x86_64/sdt.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include +#include + +#include + +#include + +namespace obos +{ + volatile limine_hhdm_request hhdm_offset = { + .id = LIMINE_HHDM_REQUEST, + .revision = 0, + }; + template + T mapToHHDM(T addr) + { + uintptr_t _addr = (uintptr_t)addr; + return (T)(hhdm_offset.response->offset + _addr); + } + OBOS_NO_UBSAN_FOR(alignment) ACPISDTHeader* GetTableWithSignature(ACPISDTHeader* sdt, bool t32, size_t nEntries, char(*signature)[4]) + { + uint64_t* tableAddresses64 = reinterpret_cast(sdt + 1); + uint32_t* tableAddresses32 = reinterpret_cast(sdt + 1); + for (size_t i = 0; i < nEntries; i++) + { + ACPISDTHeader* currentSDT = nullptr; + if (t32) + currentSDT = (ACPISDTHeader*)(uintptr_t)tableAddresses32[i]; + else + currentSDT = (ACPISDTHeader*)tableAddresses64[i]; + currentSDT = mapToHHDM(currentSDT); + if (memcmp(currentSDT->Signature, signature, 4)) + return currentSDT; + } + return nullptr; + } + void GetSDTFromRSDP(ACPIRSDPHeader* rsdp, ACPISDTHeader** oHeader, bool* oT32, size_t* oNEntries) + { + bool is32BitTables = false; + ACPISDTHeader* xsdt = nullptr; + if ((is32BitTables = (rsdp->Revision == 0))) + xsdt = mapToHHDM((ACPISDTHeader*)(uintptr_t)rsdp->RsdtAddress); + else + xsdt = mapToHHDM((ACPISDTHeader*)(uintptr_t)rsdp->XsdtAddress); + if (oHeader) + *oHeader = xsdt; + if (oT32) + *oT32 = is32BitTables; + if (oNEntries) + { + // The purpose of "(((uintptr_t)!is32BitTables + 1) * 4)" is to determine whether to use sizeof(uint32_t) or sizeof(uint64_t) + *oNEntries = (xsdt->Length - sizeof(*xsdt)) / (((uintptr_t)!is32BitTables + 1) * 4); + } + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/sdt.h b/src/oboskrnl/arch/x86_64/sdt.h index 1923e564..6b0ccec0 100644 --- a/src/oboskrnl/arch/x86_64/sdt.h +++ b/src/oboskrnl/arch/x86_64/sdt.h @@ -1,35 +1,49 @@ -/* - * oboskrnl/arch/x86_64/sdt.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include - -typedef struct OBOS_PACK ACPIRSDPHeader { - char Signature[8]; - uint8_t Checksum; - char OEMID[6]; - uint8_t Revision; - uint32_t RsdtAddress; // Deprecated - - // Fields only valid if Revision != 0 - uint32_t Length; - uint64_t XsdtAddress; - uint8_t ExtendedChecksum; - uint8_t reserved[3]; -} ACPIRSDPHeader; -typedef struct OBOS_PACK ACPISDTHeader { - char Signature[4]; - uint32_t Length; - uint8_t Revision; - uint8_t Checksum; - char OEMID[6]; - char OEMTableID[8]; - uint32_t OEMRevision; - uint32_t CreatorID; - uint32_t CreatorRevision; -} ACPISDTHeader; \ No newline at end of file +/* + oboskrnl/arch/x86_64/sdt.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include +#include +#include + +namespace obos +{ + struct ACPIRSDPHeader { + char Signature[8]; + uint8_t Checksum; + char OEMID[6]; + uint8_t Revision; + uint32_t RsdtAddress; // Deprecated + + // Fields only valid if Revision != 0 + uint32_t Length; + uint64_t XsdtAddress; + uint8_t ExtendedChecksum; + uint8_t reserved[3]; + } OBOS_PACK; + struct ACPISDTHeader { + char Signature[4]; + uint32_t Length; + uint8_t Revision; + uint8_t Checksum; + char OEMID[6]; + char OEMTableID[8]; + uint32_t OEMRevision; + uint32_t CreatorID; + uint32_t CreatorRevision; + } OBOS_PACK; + /// + /// Gets a table using the signature. + /// + /// A pointer to the rsdt/xsdt. + /// Whether the tables are 32-bit (true) or not (false). + /// The amount of entries to search. + /// The signature of the table to look for. + /// The table, or nullptr on failure. + OBOS_EXPORT ACPISDTHeader* GetTableWithSignature(ACPISDTHeader* sdt, bool t32, size_t nEntries, char(*signature)[4]); + OBOS_EXPORT void GetSDTFromRSDP(ACPIRSDPHeader* rsdp, ACPISDTHeader** oHeader, bool *oT32, size_t *oNEntries); +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/smp.asm b/src/oboskrnl/arch/x86_64/smp.asm deleted file mode 100644 index 5c6694d7..00000000 --- a/src/oboskrnl/arch/x86_64/smp.asm +++ /dev/null @@ -1,208 +0,0 @@ -; oboskrnl/arch/x86_64/smp.asm -; -; Copyright (c) 2024 Omar Berrow - -[BITS 64] -[DEFAULT ABS] - -global Arch_SMPTrampolineStart:data hidden -global Arch_SMPTrampolineEnd:data hidden -global Arch_SMPTrampolineCR3:data hidden -global Arch_SMPTrampolineRSP:data hidden -global Arch_SMPTrampolineCPULocalPtr:data hidden - -section .pageable.data - -extern Arch_APEntry -global Arch_SMPTrampolineWakeLocation -Arch_SMPTrampolineWakeLocation: dq Arch_APEntry - -Arch_SMPTrampolineStart: -bits 16 -real_mode: - jmp 0x0:start_addr -align 16 -gdt: -trampoline_base: equ 0x1000 -gdt_addr: equ $-Arch_SMPTrampolineStart+trampoline_base - dq 0 - dq 0x00af9b000000ffff ; code segment, 64-bit - dq 0x00cf93000000ffff ; data segment, 64-bit -gdtr_addr: equ $-Arch_SMPTrampolineStart+trampoline_base -gdtr: - dw gdtr_addr-gdt_addr - dd gdt_addr -cr3_loc: equ $-Arch_SMPTrampolineStart+trampoline_base -Arch_SMPTrampolineCR3: - dq 0 -rsp_loc: equ $-Arch_SMPTrampolineStart+trampoline_base -Arch_SMPTrampolineRSP: - dq 0 -cpu_local_loc: equ $-Arch_SMPTrampolineStart+trampoline_base -Arch_SMPTrampolineCPULocalPtr: - dq 0 -align 1 -start: -start_addr: equ $-Arch_SMPTrampolineStart+trampoline_base -; Load GDT. - lgdt [gdtr_addr] - -; Enter long mode. - xor ax,ax - mov ds, ax - mov ss, ax - mov es, ax - mov fs, ax - mov gs, ax - - mov eax, cr4 - or eax, (1<<5) - mov cr4, eax - - mov eax, [ds:cr3_loc] - mov cr3, eax - - mov eax, 0x80000001 - xor ecx,ecx - cpuid - xor eax, eax - test edx, (1<<20) - mov esi, (1<<11) - cmovnz eax, esi ; If bit 20 is set - mov ecx, 0xC0000080 - or eax, (1 << 8)|(1 << 10)|(1 << 0) - xor edx,edx - wrmsr - - mov eax, 0x80010001 - mov cr0, eax - - mov eax, [gdt_addr+0x8+4] - test eax, 0x00af9b00 - jz .down1 - ; weird... - ; TODO: Find the real cause of this bug. - mov dword [gdt_addr+0x8], 0x0000ffff - mov dword [gdt_addr+0x8+4], 0x00af9b00 - -.down1: - - mov eax, [gdt_addr+0x10+4] - test eax, 0x00cf9300 - jz .down2 - ; weird... - ; TODO: Find the real cause of this bug. - mov dword [gdt_addr+0x10], 0x0000ffff - mov dword [gdt_addr+0x10+4], 0x00cf9300 - -.down2: - - mov word [gdtr_addr+0], gdt_addr-gdtr_addr - mov dword [gdtr_addr+0x2], gdt_addr - lgdt [gdtr_addr] - -; Reload segment registers. - jmp 0x8:reload_cs_addr -.reload_cs: -reload_cs_addr: equ $-Arch_SMPTrampolineStart+trampoline_base - mov ax, 0x10 - mov ss, ax - mov ds, ax - mov gs, ax - mov fs, ax - mov es, ax -[BITS 64] - -; Reload cr3. - mov rax, cr3 - mov cr3, rax - -; Load RSP. - mov rsp, [rsp_loc] - -; Call AP initialization code. - mov rdi, [cpu_local_loc] - mov rax, [Arch_SMPTrampolineWakeLocation] - call rax -Arch_SMPTrampolineEnd: -;global Arch_ACPIWakeTrampoline -;global Arch_ACPIWakeTrampoline_data -;global Arch_ACPIWakeTrampoline_end -;Arch_ACPIWakeTrampoline: -;bits 16 -; xor ax,ax -; mov es, ax -; mov ds, ax - -; mov si, Arch_ACPIWakeTrampoline_data-Arch_ACPIWakeTrampoline+0x1000 -; xor di,di - -; mov cx, Arch_SMPTrampolineEnd-Arch_SMPTrampolineStart -; rep movsb - -; jmp 0x00:0x0000PolarityActiveHigh -;Arch_ACPIWakeTrampoline_data: -;times Arch_SMPTrampolineEnd-Arch_SMPTrampolineStart db 0 -;Arch_ACPIWakeTrampoline_end: -;bits 64 -section .text -; All of these CPUID bits are in CPUID.07H.0H:EBX -%define CPUID_FSGSBASE (1<<0) -%define CPUID_SMEP (1<<7) -%define CPUID_SMAP (1<<20) -%define CR4_FSGSBASE (1<<16) -%define CR4_SMEP (1<<20) -%define CR4_SMAP (1<<21) -global Arch_InitializeMiscFeatures -Arch_InitializeMiscFeatures: - push rbp - mov rbp, rsp - sub rsp, 8 - mov [rbp-8], rbx - - mov eax, 7 - xor ecx, ecx - cpuid - - mov rax, cr4 - -; NOTE: We don't use this in the kernel, but userspace might like it. - test ebx, CPUID_FSGSBASE - jz .next1 - or rax, CR4_FSGSBASE -.next1: - test ebx, CPUID_SMEP - jz .next2 - or rax, CR4_SMEP -.next2: - test ebx, CPUID_SMAP - jz .next3 - or rax, CR4_SMAP -.next3: - - mov cr4, rax - - mov rbx, [rbp-8] - leave - ret -global Arch_APYield:function hidden -extern Core_Yield -extern OBOS_BasicMMFreePages -extern OBOS_Panic -section .rodata -panic_str: db "Arch_APYield: Core_Yield returned.", 0xa, 0x0 -section .text -Arch_APYield: - lea rsp, [rsi+0x10000] - push rdi - push rsi - mov rsi, 0x4000 - call OBOS_BasicMMFreePages - pop rsi - pop rdi - call Core_Yield - mov rdi, 1 ; OBOS_PANIC_REASON_FATAL_ERROR - mov rsi, panic_str - xor rax, rax - call OBOS_Panic - jmp $ diff --git a/src/oboskrnl/arch/x86_64/smp.c b/src/oboskrnl/arch/x86_64/smp.c deleted file mode 100644 index 149e6960..00000000 --- a/src/oboskrnl/arch/x86_64/smp.c +++ /dev/null @@ -1,374 +0,0 @@ -/* - * oboskrnl/arch/x86_64/smp.c - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include -#include -#include -#include -#include -#include - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include -#include -#include -#include - -#include - -#include - -#include - -#include -#include - -static uint8_t s_lapicIDs[256]; -static uint8_t s_nLAPICIDs = 0; -#define OffsetPtr(ptr, off, t) ((t*)(((uintptr_t)(ptr)) + (off))) -#define NextMADTEntry(cur) OffsetPtr(cur, cur->length, MADT_EntryHeader) -obos_status Arch_MapPage(uintptr_t cr3, void* at_, uintptr_t phys, uintptr_t flags, bool); -static OBOS_NO_UBSAN void ParseMADT() -{ - // Find the MADT in the ACPI tables. - ACPIRSDPHeader* rsdp = (ACPIRSDPHeader*)Arch_MapToHHDM(Arch_RSDPBase); - bool tables32 = rsdp->Revision < 2; - ACPISDTHeader* xsdt = tables32 ? (ACPISDTHeader*)(uintptr_t)rsdp->RsdtAddress : (ACPISDTHeader*)rsdp->XsdtAddress; - xsdt = (ACPISDTHeader*)Arch_MapToHHDM((uintptr_t)xsdt); - size_t nEntries = (xsdt->Length - sizeof(*xsdt)) / (tables32 ? 4 : 8); - MADTTable* madt = nullptr; - for (size_t i = 0; i < nEntries; i++) - { - uintptr_t phys = tables32 ? OffsetPtr(xsdt, sizeof(*xsdt), uint32_t)[i] : OffsetPtr(xsdt, sizeof(*xsdt), uint64_t)[i]; - ACPISDTHeader* header = (ACPISDTHeader*)Arch_MapToHHDM(phys); - if (memcmp(header->Signature, "APIC", 4)) - { - madt = (MADTTable*)header; - break; - } - } - void* end = OffsetPtr(madt, madt->sdtHeader.Length, void); - for (MADT_EntryHeader* cur = OffsetPtr(madt, sizeof(*madt), MADT_EntryHeader); (uintptr_t)cur < (uintptr_t)end; cur = NextMADTEntry(cur)) - { - if (cur->type == 0) - { - MADT_EntryType0* mLapicId = (MADT_EntryType0*)cur; - if (s_nLAPICIDs == 255) - break; // make continue if more types are parsed - if (~mLapicId->flags & BIT(0)) - continue; - s_lapicIDs[s_nLAPICIDs++] = mLapicId->apicID; - } - } -} -extern uint8_t Arch_SMPTrampolineStart[]; -extern uint8_t Arch_SMPTrampolineEnd[]; -extern uint64_t Arch_SMPTrampolineCR3; -extern uint64_t Arch_SMPTrampolineRSP; -extern uint64_t Arch_SMPTrampolineCPULocalPtr; -static _Atomic(bool) ap_initialized; -static void nmiHandler(interrupt_frame* frame); -extern void Arch_FlushGDT(uintptr_t gdtr); -OBOS_NO_UBSAN void Arch_CPUInitializeGDT(cpu_local *info, uintptr_t istStack, size_t istStackSize) -{ - memzero(info->arch_specific.gdtEntries, sizeof(info->arch_specific.gdtEntries)); - memzero(&info->arch_specific.tss, sizeof(info->arch_specific.tss)); - info->arch_specific.gdtEntries[0] = 0; - info->arch_specific.gdtEntries[1] = 0x00af9b000000ffff; // 64-bit code - info->arch_specific.gdtEntries[2] = 0x00cf93000000ffff; // 64-bit data - info->arch_specific.gdtEntries[3] = 0x00cff3000000ffff; // 64-bit user-mode data - info->arch_specific.gdtEntries[4] = 0x00affb000000ffff; // 64-bit user-mode code - struct - { - uint16_t limitLow; - uint16_t baseLow; - uint8_t baseMiddle1; - uint8_t access; - uint8_t gran; - uint8_t baseMiddle2; - uint32_t baseHigh; - uint32_t resv1; - } tss_entry; - memzero(&tss_entry, sizeof(tss_entry)); - uintptr_t tss = (uintptr_t)&info->arch_specific.tss; - tss_entry.limitLow = sizeof(info->arch_specific.tss)-1; - tss_entry.baseLow = tss & 0xffff; - tss_entry.baseMiddle1 = (tss >> 16) & 0xff; - tss_entry.baseMiddle2 = (tss >> 24) & 0xff; - tss_entry.baseHigh = (tss >> 32) & 0xffffffff; - tss_entry.access = 0x89; - tss_entry.gran = 0x40; - info->arch_specific.gdtEntries[5] = *((uint64_t*)&tss_entry + 0); - info->arch_specific.gdtEntries[6] = *((uint64_t*)&tss_entry + 1); - - info->arch_specific.tss.ist0 = istStack + 0x10000 /* see comment as to why we do this */; - info->arch_specific.tss.rsp0 = istStack + 0x20000 /* see comment as to why we do this */; - info->arch_specific.tss.iopb = sizeof(info->arch_specific.tss) - 1; - struct - { - uint16_t limit; - uintptr_t base; - } OBOS_PACK gdtr; - gdtr.limit = sizeof(info->arch_specific.gdtEntries) - 1; - gdtr.base = (uintptr_t)&info->arch_specific.gdtEntries; - Arch_FlushGDT((uintptr_t)&gdtr); - wrmsr(0xC0000101, (uintptr_t)info); -} -extern void Arch_IdleTask(); -OBOS_NORETURN extern void Arch_APYield(void* startup_stack, void* temp_stack); -static void idleTaskBootstrap() -{ - ap_initialized = true; - CoreS_GetCPULocalPtr()->initialized = true; - Arch_IdleTask(); -} - -extern void Arch_InitializeMiscFeatures(); -void __attribute__((no_stack_protector)) Arch_APEntry(cpu_local* info) -{ - wrmsr(0xC0000101 /* GS_BASE */, (uint64_t)info); - Arch_CPUInitializeGDT(info, (uintptr_t)info->arch_specific.ist_stack, 0x20000); - Arch_InitializeIDT(false); - irql oldIrql = Core_RaiseIrql(0xf); - OBOS_UNUSED(oldIrql); - // Setup the idle thread. - thread_ctx ctx; - memzero(&ctx, sizeof(ctx)); - void* thr_stack = OBOS_BasicMMAllocatePages(0x10000, nullptr); - CoreS_SetupThreadContext(&ctx, (uintptr_t)idleTaskBootstrap, 0, false, thr_stack, 0x10000); - thread* idleThread = CoreH_ThreadAllocate(nullptr); - CoreH_ThreadInitialize(idleThread, THREAD_PRIORITY_IDLE, ((thread_affinity)1<id), &ctx); - CoreH_ThreadReady(idleThread); - info->nReadyThreads--; - Core_ReadyThreadCount--; - Core_ProcessAppendThread(OBOS_KernelProcess, idleThread); - info->idleThread = idleThread; - Arch_LAPICInitialize(false); - Arch_InitializeMiscFeatures(); - Arch_EnableSIMDFeatures(); - // UC UC- WT WB UC WC WT WB - wrmsr(0x277, 0x0001040600070406); - Arch_RestoreMTRRs(); - asm volatile("mov %0, %%cr3" : :"r"(getCR3())); - wbinvd(); - wrmsr(0xC0000080 /* IA32_EFER */, rdmsr(0xC0000080)|BIT(0)); - OBOSS_InitializeSyscallInterface(); - Arch_APYield(info->arch_specific.startup_stack, info->arch_specific.ist_stack); -} -static OBOS_NO_UBSAN void SetMemberInSMPTrampoline(uint8_t off, uint64_t val) -{ - *OffsetPtr(0x1000, off, uint64_t) = val; -} -// static OBOS_NO_UBSAN uint64_t GetMemberInSMPTrampoline(uint8_t off) -// { -// return *OffsetPtr(nullptr, off, uint64_t); -// } -bool Arch_SMPInitialized = false; -void Arch_SMPStartup() -{ - Arch_SaveMTRRs(); - Arch_RawRegisterInterrupt(0x2, (uintptr_t)nmiHandler); - ParseMADT(); - bool up = false; -#ifdef OBOS_UP - OBOS_Log("Uniprocessor-build of OBOS. No other cores will be initialized.\n"); - // s_nLAPICIDs = 1; - up = true; -#else - if (OBOS_GetOPTF("no-smp")) - { - OBOS_Log("Running OBOS as Uniprocessor. No other cores will be initialized.\n"); - up = true; - // s_nLAPICIDs = 1; - } -#endif - // if (s_nLAPICIDs == 1) - // return; // No work to do. - // ^ is not true - cpu_local* cpu_info = (cpu_local*)ZeroAllocate(OBOS_KernelAllocator, s_nLAPICIDs, sizeof(cpu_local), nullptr); - memzero(cpu_info, s_nLAPICIDs * sizeof(cpu_local)); - OBOS_STATIC_ASSERT(sizeof(*cpu_info) == sizeof(*Core_CpuInfo), "Size mismatch for Core_CpuInfo and cpu_info."); - cpu_info[0] = Core_CpuInfo[0]; - cpu_info[0].currentPriorityList = cpu_info[0].priorityLists + (Core_CpuInfo[0].currentPriorityList - Core_CpuInfo[0].priorityLists); - Arch_MapPage(getCR3(), (void*)0x1000, 0x1000, 0x3, false); - Arch_SMPTrampolineCR3 = getCR3(); - Core_CpuInfo = cpu_info; - Core_CpuCount = up ? 1 : s_nLAPICIDs; - // irql oldIrql = Core_RaiseIrql(0xf); - for (size_t i = 0; i < s_nLAPICIDs; i++) - { - if (s_lapicIDs[i] == Arch_LAPICReadID()) - { - cpu_info[i].curr = cpu_info + i; - Arch_CPUInitializeGDT(&cpu_info[i], (uintptr_t)(cpu_info[i].arch_specific.ist_stack = OBOS_BasicMMAllocatePages(0x20000, nullptr)), 0x20000); - wrmsr(0xC0000101 /* GS_BASE */, (uintptr_t)&cpu_info[0]); - // UC UC- WT WB UC WC WT WB - wrmsr(0x277, 0x0001040600070406); - asm volatile("mov %0, %%cr3" : :"r"(getCR3())); - wbinvd(); - wrmsr(0xC0000080 /* IA32_EFER */, rdmsr(0xC0000080)|BIT(0)); - OBOSS_InitializeSyscallInterface(); - Arch_InitializeMiscFeatures(); - Arch_EnableSIMDFeatures(); - continue; - } - memcpy((void*)0x1000, Arch_SMPTrampolineStart, Arch_SMPTrampolineEnd - Arch_SMPTrampolineStart); - for (thread_priority j = 0; j <= THREAD_PRIORITY_MAX_VALUE; j++) - cpu_info[i].priorityLists[j].priority = j; - cpu_info[i].id = i; - cpu_info[i].arch_specific.lapicId = s_lapicIDs[i]; - cpu_info[i].currentIrql = 0; - cpu_info[i].isBSP = false; - cpu_info[i].schedulerTicks = 0; - cpu_info[i].curr = cpu_info + i; - cpu_info[i].arch_specific.ist_stack = OBOS_BasicMMAllocatePages(0x20000, nullptr); - cpu_info[i].arch_specific.startup_stack = OBOS_BasicMMAllocatePages(0x4000, nullptr); - Core_DefaultThreadAffinity |= CoreH_CPUIdToAffinity(cpu_info[i].id); - SetMemberInSMPTrampoline((uintptr_t)&Arch_SMPTrampolineRSP - (uintptr_t)Arch_SMPTrampolineStart, (uint64_t)cpu_info[i].arch_specific.startup_stack + 0x4000); - SetMemberInSMPTrampoline((uintptr_t)&Arch_SMPTrampolineCPULocalPtr - (uintptr_t)Arch_SMPTrampolineStart, (uint64_t)&cpu_info[i]); - ipi_lapic_info lapic = { - .isShorthand = false, - .info = { - .lapicId = s_lapicIDs[i] - }, - }; - ipi_vector_info vector = { - .deliveryMode = LAPIC_DELIVERY_MODE_INIT, - .info = { - .vector = 0, - }, - }; - obos_status status = 0; - if ((status = Arch_LAPICSendIPI(lapic, vector)) != OBOS_STATUS_SUCCESS) - { - OBOS_Error("%s: Could not send IPI. Status: %d.\n", status); - continue; - } - vector.deliveryMode = LAPIC_DELIVERY_MODE_SIPI; - vector.info.address = 0x1000; - if ((status = Arch_LAPICSendIPI(lapic, vector)) != OBOS_STATUS_SUCCESS) - { - OBOS_Error("%s: Could not send IPI. Status: %d.\n", status); - continue; - } - while (!atomic_load(&ap_initialized)) - pause(); - atomic_store(&ap_initialized, false); - } - // Core_LowerIrql(oldIrql); - Arch_SMPInitialized = true; - OBOSS_UnmapPage((void*)0x1000); - Arch_InitializeMiscFeatures(); -} -_Atomic(bool) Arch_HaltCPUs = false; -_Atomic(uint8_t) Arch_CPUsHalted = 0; -bool Arch_InvlpgIPI(interrupt_frame* frame); -OBOS_NO_KASAN static void nmiHandler(interrupt_frame* frame) -{ - if (Arch_HaltCPUs) - { - atomic_fetch_add(&Arch_CPUsHalted, 1); - cli(); - while (1) - hlt(); - } - if (Arch_InvlpgIPI(frame)) - return; - if (Kdbg_Paused) - { - asm("sti"); - // TODO: Fix - Arch_LAPICSendEOI(); - Kdbg_CallDebugExceptionHandler(frame, false); - asm("cli"); - return; - } - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "Unhandled NMI!\n"); -} -OBOS_NO_KASAN static void HaltInitializedCPUs() -{ - Arch_HaltCPUs = true; - for (size_t i = 0; i < Core_CpuCount; i++) - { - if (!Core_CpuInfo[i].initialized) - continue; - ipi_lapic_info lapic = { - .isShorthand = false, - .info = { - .lapicId = Core_CpuInfo[i].id, - }, - }; - ipi_vector_info vector = { - .deliveryMode = LAPIC_DELIVERY_MODE_NMI, - }; - Arch_LAPICSendIPI(lapic, vector); - } -} -static spinlock halt_cpu_lock; -OBOS_NO_KASAN void OBOSS_HaltCPUs() -{ - if (Core_CpuCount == 1) - return; - Core_SpinlockAcquire(&halt_cpu_lock); - if (!Arch_SMPInitialized) - { - HaltInitializedCPUs(); - return; - } - ipi_lapic_info lapic = { - .isShorthand = true, - .info = { - .shorthand = LAPIC_DESTINATION_SHORTHAND_ALL_BUT_SELF, - }, - }; - ipi_vector_info vector = { - .deliveryMode = LAPIC_DELIVERY_MODE_NMI, - }; - Arch_HaltCPUs = true; - Arch_LAPICSendIPI(lapic, vector); - // Wait for all CPUs to halt. - while (atomic_load(&Arch_CPUsHalted) != (Core_CpuCount - 1)) - pause(); -} -uintptr_t Arch_GetCPUTempStack() -{ - return (uintptr_t)CoreS_GetCPULocalPtr()->arch_specific.ist_stack; -} - -void CoreS_SetKernelStack(void* stck) -{ - if (!stck) return; - CoreS_GetCPULocalPtr()->arch_specific.tss.rsp0 = (uintptr_t)stck+0x10000; -} - -void CoreS_ForceYieldOnSyscallReturn() -{ - ipi_vector_info vector = { - .deliveryMode = LAPIC_DELIVERY_MODE_FIXED, - .info.vector = Core_SchedulerIRQ->vector->id + 0x20 - }; - asm volatile ("cli"); - static ipi_lapic_info self = { - .isShorthand=true, - .info.shorthand=LAPIC_DESTINATION_SHORTHAND_SELF - }; - Arch_LAPICSendIPI(self, vector); -} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/smp.cpp b/src/oboskrnl/arch/x86_64/smp.cpp new file mode 100644 index 00000000..bd72a6fd --- /dev/null +++ b/src/oboskrnl/arch/x86_64/smp.cpp @@ -0,0 +1,273 @@ +/* + oboskrnl/arch/x86_64/smp.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include + +#include + +#include +#include +#include + +#if OBOS_KDBG_ENABLED +#include +#include +#endif + +extern "C" char smp_trampoline_start[]; +extern "C" char smp_trampoline_end[]; +extern "C" uintptr_t smp_trampoline_cr3_loc; +extern "C" uintptr_t smp_trampoline_cpu_local_ptr; +extern "C" uintptr_t smp_trampoline_pat; +extern "C" void reload_gdt(uintptr_t gdtr); +extern "C" void enableSSE(); +extern "C" void enableXSAVE(); +extern "C" void enableAVX(); +extern "C" void enableAVX512(); + +#define IA32_PAT 0x277 +#define GS_BASE 0xC0000101 +#define KERNEL_GS_BASE 0xC0000102 + +namespace obos +{ + extern volatile limine_rsdp_request rsdp_request; + namespace scheduler + { + cpu_local* g_cpuInfo; + size_t g_nCPUs; + } +#ifdef OBOS_KDBG_ENABLED + namespace kdbg + { + extern void setupDRsForBreakpoint(bp*); + } +#endif + using namespace scheduler; + extern struct idtEntry g_idtEntries[256]; + struct idtPointer; + extern "C" void idtFlush(idtPointer* idtr); + namespace arch + { + uint8_t g_lapicIDs[256]; + static volatile bool jumped_to_bootstrap = false; + bool g_initializedAllCPUs = false; + OBOS_NO_KASAN OBOS_NO_UBSAN void InitializeGDTCPU(cpu_local* info) + { + // Initialize the TSS entry in the GDT. + struct + { + uint16_t limitLow; + uint16_t baseLow; + uint8_t baseMiddle1; + uint8_t access; + uint8_t gran; + uint8_t baseMiddle2; + uint32_t baseHigh; + uint32_t resv1; + } tss_entry{}; + uintptr_t tss = (uintptr_t)&info->archSpecific.tss; + tss_entry.limitLow = sizeof(info->archSpecific.tss); + tss_entry.baseLow = tss & 0xffff; + tss_entry.baseMiddle1 = (tss >> 16) & 0xff; + tss_entry.baseMiddle2 = (tss >> 24) & 0xff; + tss_entry.baseHigh = (tss >> 32) & 0xffff'ffff; + tss_entry.access = 0x89; + tss_entry.gran = 0x40; + info->archSpecific.gdt[5] = *((uint64_t*)&tss_entry + 0); + info->archSpecific.gdt[6] = *((uint64_t*)&tss_entry + 1); + // Initialize the TSS stacks. + info->archSpecific.tss.ist0 = info->tempStack.base + info->tempStack.size; + info->archSpecific.tss.ist1 = info->tempStack.base + info->tempStack.size; + info->archSpecific.tss.rsp0 = info->tempStack.base + info->tempStack.size; + info->archSpecific.tss.iopb = sizeof(cpu_local::archSpecific.tss)-1; + // Load the GDT and reset segment values. + struct OBOS_PACK + { + uint16_t limit; + uint64_t base; + } gdtr{}; + gdtr.limit = sizeof(info->archSpecific.gdt) - 1; + gdtr.base = (uintptr_t)info->archSpecific.gdt; + reload_gdt((uintptr_t)&gdtr); + } + [[noreturn]] OBOS_NO_KASAN OBOS_NO_UBSAN void ProcStart(cpu_local* info) + { + InitializeGDTCPU(info); + // We must set GS_BASE before anything else, or we'll have problems (such as IRQL mismatching). + wrmsr(GS_BASE, (uint64_t)info); + wrmsr(KERNEL_GS_BASE, (uint64_t)info); + jumped_to_bootstrap = true; + struct + { + uint16_t size; + uintptr_t idt; + } OBOS_PACK idtPtr{ 0xfff, (uintptr_t)g_idtEntries }; + idtFlush((idtPointer*)&idtPtr); + enableSSE(); + enableXSAVE(); + enableAVX(); + enableAVX512(); + InitializeLAPIC(g_localAPICAddress); + info->initialized = true; + // Enable interrupt. + asm("sti"); + // Ensure the IRQL of the current processor is zero. + LowerIRQL(0); + // Hang waiting for an interrupt. + while(1) + asm volatile("hlt"); + } + template + bool hasDuplicateEntry(T* arr, size_t szArr, signed long at, bool direction = false /* back */) + { + if (at > szArr) + return false; + // arr (uint8_t*): 0, 1, 2, 3, 0, 0, 0, 0 + // szArr: 8 + // at: 1 + // direction: false (back) + for (signed long i = at + (direction ? 1 : -1); direction ? i < szArr : i >= 0; direction ? i++ : i--) + if (arr[i] == arr[at]) + return true; + return false; + } + size_t StartProcessors() + { + // Get all CPU ids. + ACPISDTHeader* sdt = nullptr; + size_t nEntries = 0; + bool t32 = false; + GetSDTFromRSDP((ACPIRSDPHeader*)rsdp_request.response->address, &sdt, &t32, &nEntries); + char sign[4] = { 'A', 'P', 'I', 'C' }; + auto madt = (MADTTable*)GetTableWithSignature(sdt, t32, nEntries, &sign); + size_t nCPUs = ParseMADTForLAPICIds(madt, g_lapicIDs, 0); + ParseMADTForLAPICIds(madt, g_lapicIDs, sizeof(g_lapicIDs) / sizeof(*g_lapicIDs)); + if (nCPUs > sizeof(g_lapicIDs) / sizeof(*g_lapicIDs)) + nCPUs = sizeof(g_lapicIDs) / sizeof(*g_lapicIDs); + g_nCPUs = nCPUs; + g_cpuInfo = new cpu_local[g_nCPUs]; + OBOS_ASSERTP(g_cpuInfo, "Could not allocate cpu info array."); + // Copy the trampoline to physical address zero. + arch::map_page_to(&vmm::g_kernelContext, 0,0, vmm::PROT_EXECUTE | vmm::PROT_NO_DEMAND_PAGE); + smp_trampoline_cr3_loc = getCR3(); + smp_trampoline_pat = rdmsr(IA32_PAT); + memcpy(nullptr, smp_trampoline_start, smp_trampoline_end - smp_trampoline_start); + size_t nCPUsStarted = 0; + for (size_t i = 0; i < nCPUs; i++) + { + uint8_t lAPIC = g_lapicIDs[i]; + if (hasDuplicateEntry(g_lapicIDs, g_nCPUs, i, false)) + { + logger::warning("%s: CPU %d is a duplicate in the LAPIC ID list. Possibly a Hyper-Thread on the CPU?\n", __func__, lAPIC); + logger::info("%s: Skipping CPU...\n", __func__); + continue; + } + g_cpuInfo[i].tempStack.size = 0x1'0000; + g_cpuInfo[i].tempStack.base = (uintptr_t)vmm::Allocate( + &vmm::g_kernelContext, + nullptr, + g_cpuInfo[i].tempStack.size, + vmm::FLAGS_COMMIT | vmm::FLAGS_GUARD_PAGE_LEFT, + vmm::PROT_NO_DEMAND_PAGE); + g_cpuInfo[i].cpuId = lAPIC; + if (lAPIC == g_localAPICAddress->lapicID) + { + g_cpuInfo[i].irql = GetIRQL(); + g_cpuInfo[i].isBSP = true; + InitializeGDTCPU(&g_cpuInfo[i]); + wrmsr(GS_BASE, (uint64_t)&g_cpuInfo[i]); + wrmsr(KERNEL_GS_BASE, (uint64_t)&g_cpuInfo[i]); + g_cpuInfo[i].initialized = true; + continue; + } + g_cpuInfo[i].startupStack.size = 0x8000; + g_cpuInfo[i].startupStack.base = (uintptr_t)vmm::Allocate( + &vmm::g_kernelContext, + nullptr, + g_cpuInfo[i].startupStack.size, + vmm::FLAGS_COMMIT | vmm::FLAGS_GUARD_PAGE_LEFT, + vmm::PROT_NO_DEMAND_PAGE); + g_cpuInfo[i].initialized = false; + g_cpuInfo[i].isBSP = false; + *(cpu_local**)((uintptr_t)&smp_trampoline_cpu_local_ptr - (uintptr_t)&smp_trampoline_start) = &g_cpuInfo[i]; + LAPIC_SendIPI(DestinationShorthand::None, DeliveryMode::INIT, 0, lAPIC); + LAPIC_SendIPI(DestinationShorthand::None, DeliveryMode::SIPI, 0, lAPIC); + LAPIC_SendIPI(DestinationShorthand::None, DeliveryMode::SIPI, 0, lAPIC); + while (!jumped_to_bootstrap); + jumped_to_bootstrap = false; + nCPUsStarted++; + } + g_nCPUs = nCPUsStarted + 1; + bool allInitialized = false; + while (!allInitialized) + { + allInitialized = true; + for (size_t i = 0; i < g_nCPUs; i++) + if (!g_cpuInfo[i].initialized) + allInitialized = false; + } + g_initializedAllCPUs = true; + memzero(0, 0x1000); + arch::unmap(&vmm::g_kernelContext, 0); +#ifdef OBOS_KDBG_ENABLED + for (size_t i = 0; i < 4; i++) + { + auto b = kdbg::g_kdbgState.breakpoints[i]; + if (!b) + continue; + if (!b->awaitingSmpRefresh) + continue; + kdbg::setupDRsForBreakpoint(b); + } +#endif + return nCPUsStarted; + } + bool g_halt; + static void StopAllInitializedCPUs(bool includingSelf) + { + uint8_t oldIRQL = 0; + RaiseIRQL(0xf, &oldIRQL); + uint32_t currentCID = GetCPUPtr() ? GetCPUPtr()->cpuId : 0; + for (size_t cpu = 0; cpu < g_nCPUs; cpu++) + { + if (!g_cpuInfo[cpu].initialized) + continue; + if (g_cpuInfo[cpu].cpuId == currentCID) + continue; + LAPIC_SendIPI(DestinationShorthand::None, DeliveryMode::NMI, 0, g_cpuInfo[cpu].cpuId); + } + if (includingSelf) + { + LAPIC_SendIPI(DestinationShorthand::Self, DeliveryMode::NMI); + while (1); + } + LowerIRQL(oldIRQL); + } + void StopCPUs(bool includingSelf) + { + g_halt = true; + if (!g_initializedAllCPUs) + { + StopAllInitializedCPUs(includingSelf); + return; + } + LAPIC_SendIPI(includingSelf ? DestinationShorthand::All : DestinationShorthand::All_Except_Self, DeliveryMode::NMI); + } + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/smp_trampoline.asm b/src/oboskrnl/arch/x86_64/smp_trampoline.asm new file mode 100644 index 00000000..096b3e67 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/smp_trampoline.asm @@ -0,0 +1,132 @@ +; oboskrnl/arch/x86_64/smp_trampoline.asm + +; Copyright (c) 2024 Omar Berrow + +[DEFAULT REL] + +section .data + +[BITS 16] +global smp_trampoline_start +global smp_trampoline_end +global smp_trampoline_cr3_loc +global smp_trampoline_cpu_local_ptr +global smp_trampoline_pat + +extern _ZN4obos4arch9ProcStartEPNS_9scheduler9cpu_localE + + +smp_trampoline_start: + jmp .real_mode +align 8 +.gdt: + dq 0 +; 64-bit Code segment. + dq 0x00209A0000000000 +; 64-bit Data segment. + dq 0x0000920000000000 +.gdt_end: +.gdtr: equ 0x8+.gdt_end-.gdt +.cpu_local_ptr: equ smp_trampoline_cpu_local_ptr-smp_trampoline_start +.cr3_loc: equ smp_trampoline_cr3_loc-smp_trampoline_start +.reload_cs_addr: equ .reload_cs-smp_trampoline_start +.pat: equ smp_trampoline_pat-smp_trampoline_start +dw .gdt_end-.gdt-1 +dq 0x8 +align 1 +.real_mode: + ; Use a method to get from real mode->long mode. + lgdt [.gdtr] + + mov ax, 0 + mov ds, ax + + mov eax, cr4 + or eax, (1<<5) + mov cr4, eax + + mov eax, [ds:.cr3_loc] + mov cr3, eax + + mov eax, 0x80000001 + xor ecx,ecx + cpuid + xor eax, eax + test edx, (1<<20) + mov esi, (1<<11) + cmovnz eax, esi ; If bit 20 is set + mov ecx, 0xC0000080 + or eax, (1 << 8)|(1<<10) + xor edx,edx + wrmsr + + mov eax, 0x80010001 + mov cr0, eax + +; We're in long mode, we just need to a couple things before jumping to the bootstrap code. + mov ax, 0x10 + mov ds, ax + mov es, ax + mov ss, ax + mov fs, ax + mov gs, ax + + jmp 0x8:.reload_cs_addr +.reload_cs: +[BITS 64] +; Set the PAT properly. + mov eax, dword [abs .pat] + mov edx, dword [abs .pat+4] + mov ecx, 0x277 ; IA32_PAT + wrmsr +; Reload cr3 to reload any caching info in the TLB. + mov rax, cr3 + mov cr3, rax + +; Load the startup stack. + mov rax, [abs .cpu_local_ptr] + mov rdx, [rax] + add rdx, [rax+0x10] + mov rsp, rdx + +; Jump to the bootstrap function. + mov rdi, [abs .cpu_local_ptr] + mov rax, _ZN4obos4arch9ProcStartEPNS_9scheduler9cpu_localE + jmp rax + +align 8 +smp_trampoline_cr3_loc: + dq 0 +smp_trampoline_cpu_local_ptr: + dq 0 +smp_trampoline_pat: + dq 0 +smp_trampoline_end: +align 1 +section .text +global _ZN4obos9scheduler9GetCPUPtrEv +extern _ZN4obos5rdmsrEj +_ZN4obos9scheduler9GetCPUPtrEv: + mov rdi, 0xc0000101 + jmp _ZN4obos5rdmsrEj +global reload_gdt +reload_gdt: + push rbp + mov rbp, rsp + lgdt [rdi] + + mov ax, 0x10 + mov ds, ax + mov es, ax + mov ss, ax + mov fs, ax + mov gs, ax + + mov ax, 0x28 + ltr ax + + leave + pop rax + push 0x8 + push rax + o64 retf \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/sse.asm b/src/oboskrnl/arch/x86_64/sse.asm new file mode 100644 index 00000000..88d95c4a --- /dev/null +++ b/src/oboskrnl/arch/x86_64/sse.asm @@ -0,0 +1,82 @@ +; oboskrnl/arch/x86_64/sse.asm + +; Copyright (c) 2024 Omar Berrow + +[BITS 64] + +[global enableSSE] +[global enableXSAVE] +[global enableAVX] +[global enableAVX512] + +enableSSE: + push rbp + mov rbp, rsp + + mov rax, cr0 + and rax, ~(1<<2) + or rax, (1<<2) + mov cr0, rax + mov rax, cr4 + or rax, (1<<9)|(1<<10) + mov cr4, rax + + leave + ret +enableXSAVE: + push rbp + mov rbp, rsp + push rbx + + mov eax, 1 + xor ecx, ecx + cpuid + test ecx, (1<<26) + jz .done + ; Set CR4.OSXSAVE + mov rax, cr4 + or rax, (1<<18) + mov cr4, rax + +.done: + pop rbx + leave + ret +enableAVX: + push rbp + mov rbp, rsp + push rbx + + mov eax, 1 + xor ecx, ecx + cpuid + test ecx, (1<<28) + jz .done + xor rcx,rcx + xgetbv + or eax, (1<<0)|(1<<1)|(1<<2) + xsetbv + +.done: + pop rbx + leave + ret +enableAVX512: + push rbp + mov rbp, rsp + push rbx + + mov eax, 0xD + xor ecx, ecx + cpuid + test eax, (1<<5)|(1<<6)|(1<<7) + jz .done + xor rcx,rcx + xgetbv + or eax, (1<<5)|(1<<6)|(1<<7) + xsetbv + +.done: + pop rbx + leave + ret \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/sse.c b/src/oboskrnl/arch/x86_64/sse.c deleted file mode 100644 index 117281f3..00000000 --- a/src/oboskrnl/arch/x86_64/sse.c +++ /dev/null @@ -1,74 +0,0 @@ -/* - * oboskrnl/arch/x86_64/sse.c - * - * Copyright (c) 2024-2025 Omar Berrow - */ - -#include -#include -#include - -#include - -#include - -static size_t xsave_size = 512; -bool Arch_HasXSAVE = false; - -void* Arch_AllocateXSAVERegion() -{ -#ifndef OBOS_DEBUG - void* base = ZeroAllocate(OBOS_NonPagedPoolAllocator, 1, xsave_size, nullptr); -#else - void* base = ZeroAllocate(OBOS_NonPagedPoolAllocator, 1, xsave_size+64, nullptr); - base = (void*)(((uintptr_t)base + 63) & ~63); -#endif - uint32_t* base32 = base; - base32[0x18/4] = 0x1f80; // mxcsr - return base; - -} - -void Arch_FreeXSAVERegion(void* buf) -{ -#ifndef OBOS_DEBUG_FREE_SIZE - Free(OBOS_NonPagedPoolAllocator, buf, xsave_size); -#else - OBOS_Debug("XSave Region 0x%p leaked!\n", buf); -#endif -} - -// Enables stuff such as XSAVE, SSE(2), AVX, AVX512, etc. -__attribute__((target("xsave"))) void Arch_EnableSIMDFeatures() -{ - // Enable SSE. - - // Clear CR0.EM, set CR0.MP - asm ("mov %0, %%cr0" : :"r"(getCR0() & ~BIT(2))); - asm ("mov %0, %%cr0" : :"r"(getCR0() | BIT(1))); - // Set CR4.OSFXSR, CR4.OSXMMEXCPT - asm ("mov %0, %%cr4" : :"r"(getCR4() | BIT(9) | BIT(10))); - - // Enable XSAVE, if supported - uint32_t ecx = 0; - __cpuid__(0x1, 0x0, nullptr, nullptr, &ecx, nullptr); - if (ecx & BIT(26)) - { - asm ("mov %0, %%cr4" : :"r"(getCR4() | BIT(18))); - Arch_HasXSAVE = true; - __cpuid__(0xd, 0, nullptr, nullptr, (uint32_t*)&xsave_size, nullptr); - } - - // Enable AVX. - if (ecx & BIT(28)) - __builtin_ia32_xsetbv(0, __builtin_ia32_xgetbv(0)|0b111); - - // Enable AVX512. - uint32_t eax = 0; - __cpuid__(0xd, 0, &eax, nullptr,nullptr,nullptr); - if (eax & (0x7<<5)) - __builtin_ia32_xsetbv(0, __builtin_ia32_xgetbv(0) | (0x7<<5)); -} - -size_t Arch_GetXSaveRegionSize() -{return xsave_size;} diff --git a/src/oboskrnl/arch/x86_64/sse.h b/src/oboskrnl/arch/x86_64/sse.h deleted file mode 100644 index dbd7dbda..00000000 --- a/src/oboskrnl/arch/x86_64/sse.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * oboskrnl/arch/x86_64/sse.h - * - * Copyright (c) 2024 Omar Berrow - */ - -#pragma once - -#include - -void* Arch_AllocateXSAVERegion(); -void Arch_FreeXSAVERegion(void* reg); -// Enables stuff such as XSAVE, SSE(2), AVX, AVX512, etc. -void Arch_EnableSIMDFeatures(); -size_t Arch_GetXSaveRegionSize(); - -// Set to false if the thread context code should fallback to fxsave/fxrstor, -// otherwise, the thread context code can and should use xsave/xrstor -extern bool Arch_HasXSAVE; diff --git a/src/oboskrnl/arch/x86_64/ssignal.c b/src/oboskrnl/arch/x86_64/ssignal.c deleted file mode 100644 index cb8671ef..00000000 --- a/src/oboskrnl/arch/x86_64/ssignal.c +++ /dev/null @@ -1,165 +0,0 @@ -/* - * oboskrnl/arch/x86_64/ssignal.c - * - * Copyright (c) 2024-2026 Omar Berrow -*/ - -#include -#include -#include -#include -#include - -#include -#include -#include - -#include - -#include - -#include - -#include - -#include -#include -#include -#include -#include - -#include - -// Abandon all hope, ye who enter here. - -#define FS_BASE (0xC0000100) -#define GS_BASE (0xC0000101) -#define KERNEL_GS_BASE (0xC0000102) - -#define ALLOWED_FLAGS (0b1000011000110111111111) -void OBOSS_SigReturn(ucontext_t* uctx) -{ - if (Core_GetCurrentThread()->signal_info->is_siginfo) - uctx = (void*)((uintptr_t)uctx + sizeof(siginfo_t)); - ucontext_t* ctx = Mm_MapViewOfUserMemory(Core_GetCurrentThread()->proc->ctx, uctx, nullptr, sizeof(ucontext_t), OBOS_PROTECTION_READ_ONLY, true, nullptr); - if (!ctx) - return; - Core_EventClear(&Core_GetCurrentThread()->signal_info->event); - irql oldIrql = Core_RaiseIrqlNoThread(IRQL_DISPATCH); - OBOS_UNUSED(oldIrql); - thread_ctx* thread_ctx = &Core_GetCurrentThread()->context; - memcpy(&thread_ctx->frame, &ctx->frame, sizeof(interrupt_frame)); - thread_ctx->cr3 = ctx->cr3; - thread_ctx->fs_base = ctx->fs_base; - thread_ctx->gs_base = ctx->gs_base; - if (thread_ctx->signal_extended_ctx_ptr && thread_ctx->extended_ctx_ptr) - memcpy(thread_ctx->extended_ctx_ptr, thread_ctx->signal_extended_ctx_ptr, Arch_GetXSaveRegionSize()); - thread_ctx->irql = ctx->irql; - Mm_VirtualMemoryFree(&Mm_KernelContext, ctx, sizeof(*ctx)); - CoreS_SwitchToThreadContext(thread_ctx); -} -void OBOSS_RunSignalImpl(int sigval, interrupt_frame* frame) -{ - // if (!(frame->cs & 0x3)) - // return; - sigaction* sig = &Core_GetCurrentThread()->proc->signal_handlers[sigval-1]; - if (sig->un.handler == SIG_IGN) - return; - ucontext_t ctx = { .frame=*frame,.gs_base=rdmsr(!(frame->cs & 0x3) ? GS_BASE : KERNEL_GS_BASE),.fs_base=rdmsr(FS_BASE),.cr3=frame->cr3 }; - if (!Core_GetCurrentThread()->context.signal_extended_ctx_ptr) - Core_GetCurrentThread()->context.signal_extended_ctx_ptr = Arch_AllocateXSAVERegion(); - if (Arch_HasXSAVE) - xsave(Core_GetCurrentThread()->context.signal_extended_ctx_ptr); - else - __builtin_ia32_fxsave(Core_GetCurrentThread()->context.signal_extended_ctx_ptr); - bool is_kernel_stack = !(frame->cs & 0x3); - if (sig->flags & SA_ONSTACK && Core_GetCurrentThread()->signal_info->sp) - frame->rsp = Core_GetCurrentThread()->signal_info->sp; - if (is_kernel_stack) - { - if (!Core_GetCurrentThread()->userStack) - Core_GetCurrentThread()->userStack = Mm_VirtualMemoryAlloc(Core_GetCurrentThread()->proc->ctx, nullptr, 0x10000, 0, VMA_FLAGS_GUARD_PAGE, nullptr, nullptr); - OBOS_ENSURE(Core_GetCurrentThread()->userStack); - frame->rsp = (uintptr_t)Core_GetCurrentThread()->userStack + 0x10000; - } - - ctx.irql = 0; - - frame->rsp -= sizeof(ctx); - uint8_t *rsp = Mm_MapViewOfUserMemory(Core_GetCurrentThread()->proc->ctx, (void*)frame->rsp, nullptr, sizeof(ucontext_t), 0, true, nullptr); - if (!rsp) - Core_ExitCurrentThread(); - memcpy(rsp, &ctx, sizeof(ctx)); - Mm_VirtualMemoryFree(&Mm_KernelContext, rsp, sizeof(ucontext_t)); - - uintptr_t ucontext_loc = frame->rsp; - if (!sig->un.handler || sigval == SIGKILL || sigval == SIGSTOP) - { - switch (OBOS_SignalDefaultActions[sigval]) { - case SIGNAL_DEFAULT_TERMINATE_PROC: - break; - case SIGNAL_DEFAULT_IGNORE: - return; - case SIGNAL_DEFAULT_STOP: - CoreH_ThreadBlock(Core_GetCurrentThread(), true); - return; - case SIGNAL_DEFAULT_CONTINUE: - return; - default: - OBOS_ASSERT(!"unknown signal default action"); - } - // bruh - siginfo_t siginfo = {}; - siginfo.sender = sig->sender; - siginfo.sigcode = sig->sigcode; - siginfo.status = sig->status; - siginfo.udata.integer = sig->udata; - siginfo.signum = sigval; - frame->rdi = sigval; - frame->rsi = (uintptr_t)ZeroAllocate(OBOS_NonPagedPoolAllocator, 1, sizeof(siginfo), nullptr); - memcpy((void*)frame->rsi, &siginfo, sizeof(siginfo)); - frame->rdx = (uintptr_t)ZeroAllocate(OBOS_NonPagedPoolAllocator, 1, sizeof(ctx), nullptr); - memcpy((void*)frame->rdx, &ctx, sizeof(ctx)); - frame->rip = (uintptr_t)OBOS_DefaultSignalHandler; - frame->cs = 0x8; - frame->ss = 0x10; - frame->ds = 0x10; - frame->cr3 = MmS_GetCurrentPageTable(); - frame->rsp = (uintptr_t)Core_GetCurrentThread()->kernelStack + 0x10000; - return; - } - frame->cr3 = Core_GetCurrentThread()->proc->ctx->pt; - frame->rflags = 0x200202; - frame->cs = 0x23; - frame->ss = 0x1b; - frame->ds = 0x1b; - if (sig->flags & SA_SIGINFO) - { - siginfo_t siginfo = {}; - siginfo.sender = sig->sender; - siginfo.sigcode = sig->sigcode; - siginfo.status = sig->status; - siginfo.udata.integer = sig->udata; - siginfo.signum = sigval; - frame->rsp -= sizeof(siginfo); - memcpy_k_to_usr((void*)frame->rsp, &siginfo, sizeof(siginfo)); - frame->rsp -= sizeof(sig->trampoline_base); - Core_GetCurrentThread()->signal_info->is_siginfo = true; - memcpy_k_to_usr((void*)frame->rsp, &sig->trampoline_base, sizeof(sig->trampoline_base)); - frame->rdi = sigval; - frame->rsi = frame->rsp; - frame->rdx = ucontext_loc; - frame->rip = (uintptr_t)sig->un.sa_sigaction; - } - else - { - frame->rdi = sigval; - frame->rip = (uintptr_t)sig->un.handler; - frame->rsp -= sizeof(sig->trampoline_base); - memcpy_k_to_usr((void*)frame->rsp, &sig->trampoline_base, sizeof(sig->trampoline_base)); - } - // When the irq handler returns it will be in the user signal handler. - // NOTTODO: Test. - // NOTE(oberrow): HOW DID THIS WORK FIRST TRY!!!! - // LESGOOOOOOOOOOOOOOOOOOoooo -} diff --git a/src/oboskrnl/arch/x86_64/syscall.asm b/src/oboskrnl/arch/x86_64/syscall.asm deleted file mode 100644 index a2acb4e0..00000000 --- a/src/oboskrnl/arch/x86_64/syscall.asm +++ /dev/null @@ -1,154 +0,0 @@ -; oboskrnl/arch/x86_64/syscall.asm -; -; Copyright (c) 2024 Omar Berrow - -BITS 64 -[DEFAULT ABS] - -extern OBOS_SyscallTable -extern OBOS_ArchSyscallTable -extern Core_RaiseIrql -extern Core_LowerIrql -extern Arch_KernelCR3 -extern Sys_InvalidSyscall -extern Arch_LogSyscall -extern Arch_LogSyscallRet - -section .data -global Arch_cpu_local_currentKernelStack_offset -Arch_cpu_local_currentKernelStack_offset: - dq 0 -section .text - -global Arch_SyscallTrapHandler - -; NOTE: Clobbers r10, rcx, and r11 -; Return value in rax -; Parameter registers (in order): -; rdi, rsi, rdx, r8, r9 -; Syscall number is in eax. -; FIXME: Return OBOS_STATUS_UNIMPLEMENTED if eax > highest syscall number -Arch_SyscallTrapHandler: - swapgs - - mov r10, [Arch_KernelCR3] - mov cr3, r10 - - mov r10, rsp - mov rsp, [Arch_cpu_local_currentKernelStack_offset] - mov rsp, [gs:rsp] - add rsp, 0x10000 - - sti - -; Only save SysV callee-saved registers, rcx (return address), r10 (return stack), and r11 (return eflags) - push rcx - push r10 - push r11 - push rbp - push rbx - push r12 - push r13 - push r14 - push r15 - - ; Use r11 as the syscall table address - mov r11, OBOS_SyscallTable - cmp eax, 0x80000000 - jb .not_arch_syscall - - mov r11, OBOS_ArchSyscallTable - sub eax, 0x80000000 - -.not_arch_syscall: - - ; Conserve caller-saved "scratch" registers before logging the syscall - push rax - push r11 - push rdi - push rsi - push rdx - push rcx - push r8 - push r9 - - mov rcx, r8 - mov r8, r9 - mov r9, rax - call Arch_LogSyscall - - pop r9 - pop r8 - pop rcx - pop rdx - pop rsi - pop rdi - pop r11 - pop rax - - push rax - - mov rcx, r8 - mov r8, r9 - - cmp qword [r11+rax*8], 0 - jz .invalid_syscall - - push .finished - jmp [r11+rax*8] - -.invalid_syscall: - call Sys_InvalidSyscall -.finished: - - pop rsi - push rax - mov rdi, rax - call Arch_LogSyscallRet - pop rax - - mov r9, gs:0x18 - mov r9, [r9] ; currentContext->pt - pop r15 - pop r14 - pop r13 - pop r12 - pop rbx - pop rbp - pop r11 - pop r10 - pop rcx - mov cr3, r9 - - cli - mov rsp, r10 - swapgs - o64 sysret -global Arch_SyscallTrapHandlerEnd -; OBOS_NORETURN void Arch_GotoUser(uintptr_t rip, uintptr_t cr3, uintptr_t rsp); -global Arch_GotoUser -extern Core_GetIRQLVar -Arch_GotoUser: - cli - - mov rax, 0 - mov cr8, rax - push rdi - push rsi - push rdx - call Core_GetIRQLVar - pop rdx - pop rsi - pop rdi - mov qword [rax], 0 - - mov cr3, rsi - - swapgs - - mov rcx, rdi - mov r11, 0x200202 ; rflags=IF,ID - mov rsp, rdx - o64 sysret -Arch_SyscallTrapHandlerEnd: - diff --git a/src/oboskrnl/arch/x86_64/syscall.c b/src/oboskrnl/arch/x86_64/syscall.c deleted file mode 100644 index 3ab373f6..00000000 --- a/src/oboskrnl/arch/x86_64/syscall.c +++ /dev/null @@ -1,383 +0,0 @@ -/* - * oboskrnl/arch/x86_64/syscall.c - * - * Copyright (c) 2024-2026 Omar Berrow - */ - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include - -#include -#include -#include -#include - -#include - -#include - -#include -#include - -#define IA32_EFER 0xC0000080 -#define IA32_STAR 0xC0000081 -#define IA32_LSTAR 0xC0000082 -#define IA32_CSTAR 0xC0000083 -#define IA32_FSTAR 0xC0000084 - -extern uint64_t Arch_cpu_local_currentKernelStack_offset; -extern char Arch_SyscallTrapHandler[]; -void OBOSS_InitializeSyscallInterface() -{ - // Enable IA32_EFER.SCE - // This is done in CPU initialization - // wrmsr(IA32_EFER, rdmsr(IA32_EFER) | BIT(0) /* SCE */); - wrmsr(IA32_STAR, 0x0013000800000000); // CS: 0x08, SS: 0x10, User CS: 0x1b, User SS: 0x23 - wrmsr(IA32_FSTAR, 0x43700); // Clear IF,TF,AC, and DF - wrmsr(IA32_LSTAR, (uintptr_t)Arch_SyscallTrapHandler); - Arch_cpu_local_currentKernelStack_offset = offsetof(cpu_local, currentKernelStack); -} - -void SysS_SetFSBase(uintptr_t to) -{ - wrmsr(0xC0000100, to); - Core_GetCurrentThread()->context.fs_base = to; -} - -// Creates a new thread context, but takes in a stack pointer instead of a stack and stack size -handle SysS_ThreadContextCreateFork(uintptr_t entry, uintptr_t stack_pointer, handle vmm_context) -{ - context* vmm_ctx = - HANDLE_TYPE(vmm_context) == HANDLE_TYPE_CURRENT ? - Core_GetCurrentThread()->proc->ctx : nullptr; - if (!vmm_ctx) - { - OBOS_LockHandleTable(OBOS_CurrentHandleTable()); - obos_status status = OBOS_STATUS_SUCCESS; - handle_desc* vmm_ctx_desc = OBOS_HandleLookup(OBOS_CurrentHandleTable(), vmm_context, HANDLE_TYPE_VMM_CONTEXT, false, &status); - OBOS_UNUSED(status); - if (!vmm_ctx_desc) - { - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - return HANDLE_INVALID; - } - vmm_ctx = vmm_ctx_desc->un.vmm_context; - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - } - - handle_desc* desc = nullptr; - OBOS_LockHandleTable(OBOS_CurrentHandleTable()); - handle hnd = OBOS_HandleAllocate(OBOS_CurrentHandleTable(), HANDLE_TYPE_THREAD_CTX, &desc); - thread_ctx_handle *ctx = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(thread_ctx_handle), nullptr); - desc->un.thread_ctx = ctx; - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - ctx->ctx = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(thread_ctx), nullptr); - ctx->usable = true; - ctx->lock = PUSHLOCK_INITIALIZE(); - - ctx->ctx->cr3 = vmm_ctx->pt; - ctx->ctx->frame.cr3 = vmm_ctx->pt; - ctx->ctx->frame.rip = entry; - ctx->ctx->frame.rsp = stack_pointer; - ctx->ctx->frame.ss = 0x1b; - ctx->ctx->frame.cs = 0x23; - ctx->ctx->frame.rflags = 0x200202; - ctx->ctx->fs_base = rdmsr(0xC0000100); - ctx->ctx->extended_ctx_ptr = Arch_AllocateXSAVERegion(); - ctx->vmm_ctx = vmm_ctx; - ctx->ctx->stackBase = Core_GetCurrentThread()->context.stackBase; - ctx->ctx->stackSize = Core_GetCurrentThread()->context.stackSize; - - return hnd; -} - -bool Arch_UsingInvTSCFrequency(); -uint64_t SysS_QueryTSCFrequency() -{ - if (Arch_UsingInvTSCFrequency()) - return CoreS_GetNativeTimerFrequency(); - return UINT64_MAX; -} - -uintptr_t OBOS_ArchSyscallTable[] = { - (uintptr_t)SysS_SetFSBase, - (uintptr_t)SysS_ThreadContextCreateFork, - (uintptr_t)SysS_ClockGet, - (uintptr_t)SysS_GDBStubBindInet, - (uintptr_t)SysS_GDBStubBindDevice, - (uintptr_t)SysS_GDBStubStart, - (uintptr_t)SysS_QueryTSCFrequency, -}; - -const char* syscall_to_string[] = { - "Core_ExitCurrentThread/Sys_SetFSBase", - "Core_Yield/Sys_ThreadContextCreateFork", - "OBOS_Reboot/SysS_ClockGet", - "OBOS_Shutdown/SysS_GDBStubBindInet", - "Sys_HandleClose/SysS_GDBStubBindDevice", - "Sys_HandleClone/SysS_GDBStubStart", - "Sys_ThreadContextCreate/SysS_QueryTSCFrequency", // 6 - "OBOS_Suspend", - "Sys_ThreadOpen", - "Sys_ThreadCreate", - "Sys_ThreadReady", - "Sys_ThreadBlock", - "Sys_ThreadBoostPriority", - "Sys_ThreadPriority", - "Sys_ThreadAffinity", - "Sys_ThreadSetOwner", - "Sys_ThreadGetTid", // 16 - "Sys_WaitOnObject", - "Sys_Fcntl", - "Sys_ProcessOpen", // Unimplemented - "Sys_ProcessStart", - "Sys_KillProcess", // signal-related - "Sys_VirtualMemoryAlloc", // 22 - "Sys_VirtualMemoryFree", - "Sys_VirtualMemoryProtect", - "Sys_VirtualMemoryLock", // Unimplemented - "Sys_VirtualMemoryUnlock", // Unimplemented - "Sys_MakeNewContext", - "Sys_ContextExpandWSCapacity", - "Sys_ContextGetStat", - "Sys_GetUsedPhysicalMemoryCount", - "Sys_QueryPageInfo", - "Sys_FutexWake", // 32 - "Sys_FutexWait", - "Sys_FdAlloc", // 34 - "Sys_FdOpen", - "Sys_FdOpenDirent", - "Sys_FdWrite", - "Sys_IRPCreate", - "Sys_FdRead", - "Sys_IRPWait", - "Sys_FdSeek", - "Sys_FdTellOff", - "Sys_FdEOF", - "Sys_FdIoctl", - "Sys_FdFlush", // 45 - "OBOS_PartProbeAllDrives", - "Sys_PartProbeDrive", // 47 - "OBOSS_SigReturn", - "Sys_Kill", - "Sys_SigAction", - "OBOS_SigSuspend", - "Sys_SigProcMask", - "Sys_SigAltStack", // 53 - "Sys_OpenDir", - "Sys_ReadEntries", // 55 - "Sys_ExecVE", // 56 - "Sys_LibCLog", // 57 - "Sys_ProcessGetPID", // 58 - "Sys_ProcessGetPPID", // 59 - "Sys_FdOpenAt", - "Sys_MmFork", - "Sys_ExitCurrentProcess", - "Sys_ProcessGetStatus", - "Sys_WaitProcess", - "Sys_Stat", - "Sys_StatFSInfo", - "Sys_SysConf", - "Sys_SetKLogLevel", - "Sys_LoadDriver", - "Sys_StartDriver", - "Sys_UnloadDriver", - "Sys_PnpLoadDriversAt", - "Sys_FindDriverByName", - "Sys_EnumerateLoadedDrivers", - "Sys_QueryDriverName", - "Sys_Sync", - "Sys_SleepMS", - "Sys_Mount", - "Sys_Unmount", - "Sys_FdCreat", - "Sys_FdOpenEx", - "Sys_FdOpenAtEx", - "Sys_Mkdir", - "Sys_MkdirAt", - "Sys_Chdir", - "Sys_ChdirEnt", - "Sys_GetCWD", - "Sys_SetControllingTTY", - "Sys_GetControllingTTY", - "Sys_TTYName", - "Sys_IsATTY", - "Sys_IRPWait", - "Sys_IRPQueryState", - "Sys_IRPGetBuffer", - "Sys_IRPGetStatus", - "Sys_CreatePipe", - "Sys_PSelect", - "Sys_ReadLinkAt", - "Sys_SetUid", - "Sys_SetGid", - "Sys_GetUid", - "Sys_GetGid", - "Sys_UnlinkAt", - "Sys_MakeDiskSwap", - "Sys_SwitchSwap", - "Sys_SyncAnonPages", - "Sys_FdPWrite", - "Sys_FdPRead", - "Sys_SymLink", - "Sys_SymLinkAt", - "Sys_CreateNamedPipe", - "Sys_PPoll", - "Sys_Socket", - "Sys_SendTo", - "Sys_RecvFrom", - "Sys_Listen", - "Sys_Accept", - "Sys_Bind", - "Sys_Connect", - "Sys_SockName", - "Sys_PeerName", - "Sys_GetSockOpt", - "Sys_SetSockOpt", - "Sys_ShutdownSocket", - "Sys_GetHostname", - "Sys_SetHostname", - [127]="Sys_KillProcessGroup", - "Sys_SetProcessGroup", - "Sys_GetProcessGroup", - "Sys_LinkAt", - "Sys_FChmodAt", - "Sys_FChownAt", - "Sys_UMask", - "Sys_RenameAt", - "Sys_UTimeNSAt", - "Sys_ThreadGetStack", - "Sys_SetRESUid", - "Sys_SetRESGid", - "Sys_GetRESUid", - "Sys_GetRESGid", - "Sys_SigPending", - "Sys_SetGroups", - "Sys_GetGroups", - "Sys_GetCachedByteCount", - "Sys_GetHDADevices", - "Sys_SetSid", - "Sys_GetSid", - "Sys_Chroot", - "Sys_OpenTmpFS", - "Sys_Mount2", -}; - -const char* status_to_string[] = { - "OBOS_STATUS_SUCCESS", - "OBOS_STATUS_INVALID_IRQL", - "OBOS_STATUS_INVALID_ARGUMENT", - "OBOS_STATUS_UNIMPLEMENTED", - "OBOS_STATUS_INVALID_INIT_PHASE", - "OBOS_STATUS_INVALID_AFFINITY", - "OBOS_STATUS_NOT_ENOUGH_MEMORY", - "OBOS_STATUS_MISMATCH", - "OBOS_STATUS_INTERNAL_ERROR", - "OBOS_STATUS_RETRY", - "OBOS_STATUS_ALREADY_INITIALIZED", - "OBOS_STATUS_NOT_FOUND", - "OBOS_STATUS_IN_USE", - "OBOS_STATUS_ACCESS_DENIED", - "OBOS_STATUS_UNINITIALIZED", - "OBOS_STATUS_UNHANDLED", - "OBOS_STATUS_UNPAGED_POOL", - "OBOS_STATUS_INVALID_FILE", - "OBOS_STATUS_INVALID_HEADER", - "OBOS_STATUS_DRIVER_REFERENCED_UNRESOLVED_SYMBOL", - "OBOS_STATUS_DRIVER_SYMBOL_MISMATCH", - "OBOS_STATUS_NO_ENTRY_POINT", - "OBOS_STATUS_INVALID_IOCTL", - "OBOS_STATUS_INVALID_OPERATION", - "OBOS_STATUS_DPC_ALREADY_ENQUEUED", - "OBOS_STATUS_RECURSIVE_LOCK", - "OBOS_STATUS_READ_ONLY", - "OBOS_STATUS_NOT_A_FILE", - "OBOS_STATUS_ALREADY_MOUNTED", - "OBOS_STATUS_EOF", - "OBOS_STATUS_ABORTED", - "OBOS_STATUS_PAGE_FAULT", - "OBOS_STATUS_TIMED_OUT", - "OBOS_STATUS_PIPE_CLOSED", - "OBOS_STATUS_NO_SPACE", - "OBOS_STATUS_NO_SYSCALL", - "OBOS_STATUS_WAKE_INCAPABLE", - "OBOS_STATUS_INVALID_ELF_TYPE", - "OBOS_STATUS_WOULD_BLOCK", - "OBOS_STATUS_NOT_A_TTY", - "OBOS_STATUS_IRP_RETRY", - "OBOS_STATUS_PORT_IN_USE", - "OBOS_STATUS_ADDRESS_IN_USE", - "OBOS_STATUS_ADDRESS_NOT_AVAILABLE", - "OBOS_STATUS_NO_ROUTE_TO_HOST", - "OBOS_STATUS_MESSAGE_TOO_BIG", - "OBOS_STATUS_CONNECTION_REFUSED", - "OBOS_STATUS_SEEK_UNALLOWED", - "OBOS_STATUS_NETWORK_UNREACHABLE", -}; - -void Arch_LogSyscall(uintptr_t rdi, uintptr_t rsi, uintptr_t rdx, uintptr_t r8, uintptr_t r9, uint32_t eax) -{ - OBOS_UNUSED(rdi); - OBOS_UNUSED(rsi); - OBOS_UNUSED(rdx); - OBOS_UNUSED(r8); - OBOS_UNUSED(r9); - OBOS_UNUSED(eax); - if (eax >= sizeof(syscall_to_string)/sizeof(const char*) || !syscall_to_string[eax]) - { - OBOS_Warning("(thread %ld, process %ld) invalid syscall %d(0x%p, 0x%p, 0x%p, 0x%p, 0x%p)\n", Core_GetCurrentThread()->tid, Core_GetCurrentThread()->proc->pid, eax, rdi,rsi,rdx,r8,r9); - return; - } - static bool cached_opt = false, opt = false; - if (!cached_opt) - { - opt = OBOS_GetOPTF("disable-syscall-logs"); - cached_opt = true; - } - if (opt) - return; - OBOS_Debug("(thread %ld, process %ld) syscall %s(0x%p, 0x%p, 0x%p, 0x%p, 0x%p)\n", Core_GetCurrentThread()->tid, Core_GetCurrentThread()->proc->pid, syscall_to_string[eax], rdi,rsi,rdx,r8,r9); -} -void Arch_LogSyscallRet(uint64_t ret, uint32_t eax) -{ - OBOS_UNUSED(ret); - OBOS_UNUSED(eax); - if (eax >= sizeof(syscall_to_string)/sizeof(const char*) || !syscall_to_string[eax]) - return; - static bool cached_opt = false, opt = false; - if (!cached_opt) - { - opt = OBOS_GetOPTF("disable-syscall-error-log"); - cached_opt = true; - } - static bool cached_opt2 = false, opt2 = false; - if (!cached_opt2) - { - opt2 = OBOS_GetOPTF("disable-syscall-logs"); - cached_opt2 = true; - } - if (opt2) - return; - if (opt || - (ret == 0 || eax == 22 || eax == 42 || eax == 58 || eax == 34 || eax == 0 - || eax == 20 || eax == 59 || eax == 61 || eax == 9 || eax == 1 || eax == 19 - || eax == 2 || eax == 54 || eax == 6 || eax == 16 || (eax == 91 || ret == OBOS_STATUS_NOT_A_TTY))) - OBOS_Debug("(thread %ld, process %ld) syscall %s returned 0x%x (%s)\n", Core_GetCurrentThread()->tid, Core_GetCurrentThread()->proc->pid, syscall_to_string[eax], ret, (ret < sizeof(status_to_string)/sizeof(status_to_string[0])) ? status_to_string[ret] : "no status string"); - else - OBOS_Log("(thread %ld, process %ld) syscall %s returned 0x%x (%s)\n", Core_GetCurrentThread()->tid, Core_GetCurrentThread()->proc->pid, syscall_to_string[eax], ret, (ret < sizeof(status_to_string)/sizeof(status_to_string[0])) ? status_to_string[ret] : "no status string"); - if (Core_GetCurrentThread()->signal_info->pending) - Arch_LAPICSendIPI( - (ipi_lapic_info){.isShorthand=true,.info={.shorthand=LAPIC_DESTINATION_SHORTHAND_SELF}}, - (ipi_vector_info){.deliveryMode=LAPIC_DELIVERY_MODE_FIXED,.info.vector=Core_SchedulerIRQ->vector->id+0x20} - ); -} diff --git a/src/oboskrnl/arch/x86_64/thr_context_info.cpp b/src/oboskrnl/arch/x86_64/thr_context_info.cpp new file mode 100644 index 00000000..4344b60a --- /dev/null +++ b/src/oboskrnl/arch/x86_64/thr_context_info.cpp @@ -0,0 +1,92 @@ +/* + oboskrnl/arch/x86_64/thr_context_info.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include + +#include +#include + +#include + +#include + +#include + +#include +#include +#include + +#include +#include + +#include + +#include + +#include + +#define FS_BASE 0xC0000100 +#define GS_BASE 0xC0000101 +#define KERNEL_GS_BASE 0xC0000102 + +extern "C" OBOS_NO_KASAN uintptr_t GetContextInfoOffset(obos::scheduler::Thread*) +{ + return (uintptr_t)&((obos::scheduler::Thread*)nullptr)->context; +} +extern "C" OBOS_NO_KASAN uintptr_t GetLastPreemptTimeOffset(obos::scheduler::Thread*) +{ + return (uintptr_t)&((obos::scheduler::Thread*)nullptr)->lastPreemptTime; +} +extern "C" OBOS_NO_KASAN void RaiseIRQLForScheduler() +{ + uint8_t oldIRQL = 0; + if (obos::GetIRQL() < 2) + obos::RaiseIRQL(2, &oldIRQL, false); +} + +namespace obos +{ + extern uint8_t asan_stack_poison; + namespace arch + { + OBOS_NO_KASAN void SetupThreadContext(ThreadContextInfo* info, + scheduler::thr_stack* stack, + uintptr_t entry, uintptr_t arg1, + bool isUsermode, + size_t stackSize, + vmm::Context* ctx) + { + info->frame.rip = entry; + info->frame.rdi = arg1; + info->frame.rflags = RFLAGS_INTERRUPT_ENABLE | RFLAGS_CPUID | (isUsermode ? RFLAGS_IOPL_3 : 0) | (1<<1); + info->frame.cs = isUsermode ? 0x20 : 0x08; + info->frame.ss = info->frame.ds = isUsermode ? 0x18 : 0x10; + info->frame.rbp = 0; + info->pm = ctx->GetContext()->getCR3(); + info->xsave_context = !isUsermode ? nullptr : new uint8_t[info->xsave_size]; + info->fs_base = 0; + info->gs_base = isUsermode ? 0 : rdmsr(GS_BASE); + stack->base = (uintptr_t)vmm::Allocate(ctx, nullptr, stackSize, vmm::FLAGS_GUARD_PAGE_LEFT | vmm::FLAGS_RESERVE | vmm::FLAGS_COMMIT, isUsermode ? vmm::PROT_USER : 0); + stack->size = stackSize; + info->frame.rsp = stack->base + stack->size - 8; + info->irql = 0; +//#if OBOS_KASAN_ENABLED +// memset((void*)stack->base, asan_stack_poison, stack->size); +//#endif + } + OBOS_NO_KASAN void SaveThreadContext(ThreadContextInfo* dest, interrupt_frame* frame) + { + memcpy(&dest->frame, frame, sizeof(*frame)); + dest->pm = (PageMap*)getCR3(); + dest->gs_base = rdmsr(KERNEL_GS_BASE); + dest->fs_base = rdmsr(FS_BASE); + dest->pm = (PageMap*)getCR3(); + if (dest->xsave_context) + xsave(dest->xsave_context); + } + + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/thr_context_info.h b/src/oboskrnl/arch/x86_64/thr_context_info.h new file mode 100644 index 00000000..d97b8a86 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/thr_context_info.h @@ -0,0 +1,50 @@ +/* + oboskrnl/arch/x86_64/thr_context_info.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include +#include + +#include + +#include + +#include + +#include + +namespace obos +{ + namespace scheduler + { + struct Thread; + } + namespace arch + { + struct ThreadContextInfo + { + static size_t xsave_size; + // Must be at least 576 bytes and aligned to 64 bytes, or nullptr for kernel-mode threads. + uint8_t* xsave_context; + PageMap *pm; + uint64_t irql; + uintptr_t gs_base, fs_base; + interrupt_frame frame; + }; + OBOS_EXPORT [[noreturn]] void SwitchToThrContext(ThreadContextInfo* info); + OBOS_EXPORT void SetupThreadContext(ThreadContextInfo* info, + scheduler::thr_stack* stack, + uintptr_t entry, uintptr_t arg1, + bool isUsermode, + size_t stackSize, + vmm::Context* ctx); + OBOS_EXPORT void SaveThreadContext(ThreadContextInfo* dest, interrupt_frame* frame, bool saveIRQL = true); + // Saves the thread context with the CPU's current context and calls the scheduler. + OBOS_EXPORT void YieldThread(scheduler::Thread *thr); + OBOS_EXPORT void JumpToFunctionWithCPUTempStack(void(*func)(uintptr_t userdata), uintptr_t userdata); + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/thr_context_save.asm b/src/oboskrnl/arch/x86_64/thr_context_save.asm new file mode 100644 index 00000000..93760f8a --- /dev/null +++ b/src/oboskrnl/arch/x86_64/thr_context_save.asm @@ -0,0 +1,174 @@ +; oboskrnl/arch/x86_64/thr_context_save.asm + +; Copyright (c) 2024 Omar Berrow + +[BITS 64] + +; Exported functions +global _ZN4obos4arch18SwitchToThrContextEPNS0_17ThreadContextInfoE +global _ZN4obos4arch11YieldThreadEPNS_9scheduler6ThreadE +global idleTask +global _ZN4obos4arch30JumpToFunctionWithCPUTempStackEPFvmEm +; Exported variables + +; External functions +extern _ZN4obos4arch17SaveThreadContextEPNS0_17ThreadContextInfoEPNS_15interrupt_frameE +extern _ZN4obos9scheduler8scheduleEv +extern _ZN4obos10getIRQLVarEv +extern GetContextInfoOffset +extern GetLastPreemptTimeOffset +extern RaiseIRQLForScheduler + +; External variables +extern _ZN4obos4arch17ThreadContextInfo10xsave_sizeE +extern _ZN4obos9scheduler7g_ticksE + +%macro popaq 0 +pop rbp +add rsp, 8 +pop r15 +pop r14 +pop r13 +pop r12 +pop r11 +pop r10 +pop r9 +pop r8 +pop rdi +pop rsi +add rsp, 8 +pop rbx +pop rdx +pop rcx +pop rax +%endmacro +%macro pushaq 0 +push rax +; rax has rsp. +mov rax, rsp +add rax, 8 +push rcx +push rdx +push rbx +push rax ; Push rsp +push rsi +push rdi +push r8 +push r9 +push r10 +push r11 +push r12 +push r13 +push r14 +push r15 +push 0 ; unused +push rbp +%endmacro + +_ZN4obos4arch18SwitchToThrContextEPNS0_17ThreadContextInfoE: + mov rax, [rdi] + cmp rax, 0 + je .no_xstate + ; Restore the extended state. + xrstor [rax] +.no_xstate: + cli ; Disable interrupts to avoid getting an interrupt upon setting the IRQL. + add rdi, 8 + + ; Restore the previous page map. + mov rax, [rdi] + mov cr3, rax + add rdi, 8 + ; Restore the IRQL. + push rdi + mov rdi, [rdi] + push rdi + call _ZN4obos10getIRQLVarEv + pop rdi + mov [rax], rdi + mov cr8, rdi + pop rdi + add rdi, 8 + ; Restore GS/FS base. + mov rcx, 0xC0000101 ; GS_BASE + mov eax, [rdi] + mov edx, [rdi+4] + wrmsr + add rdi, 8 + dec rcx + mov eax, [rdi] + mov edx, [rdi+4] + wrmsr + add rdi, 8 + ; Restore all GPRs. + mov rsp, rdi + add rsp, 8 ; Skip the saved DS + popaq + ; Restore RSP, RFLAGS, CS, SS, and RIP. + add rsp, 0x18 + iretq +_ZN4obos4arch11YieldThreadEPNS_9scheduler6ThreadE: +; Reserve the stack space. + push 0 + push 0 + push 0 + push 0 + push 0 + push 0 + push 0 + push 0 +; Instead of reserving it, just write it directly. + pushaq + cli + ; Just not here though + mov rax, ds + push rax + lea rax, [rsp+0xd0] ; The rsp before the call + mov [rsp+0xc0], rax ; The "interrupt" frame's saved RSP. + mov rax, [rsp+0xd0] ; The real saved RIP. + mov [rsp+0xa8], rax ; The "interrupt" frame's saved RIP. + mov rax, cs ; Current CS + mov [rsp+0xb0], rax ; The "interrupt" frame's saved CS. + pushfq + pop rax ; RFLAGS at the time of the call. This is guaranteed to be the same, as the none of the instructions used up to now modify RFLAGS. + mov [rsp+0xb8], rax ; The "interrupt" frame's saved RFLAGS. + mov rax, ss ; The current SS + mov [rsp+0xc8], rax ; The "interrupt" frame's saved SS. + mov rax, ds + mov [rsp+0], rax + + push rdi + call GetLastPreemptTimeOffset + pop rdi + mov rdx, [_ZN4obos9scheduler7g_ticksE] + mov qword [rdi+rax], rdx + + ; rdi = &thread->context + push rdi + call GetContextInfoOffset + pop rdi + add rdi, rax + mov rsi, rsp + push rdi + call _ZN4obos4arch17SaveThreadContextEPNS0_17ThreadContextInfoEPNS_15interrupt_frameE + call RaiseIRQLForScheduler + pop rdi + ; We've (finally) saved the thread context, we can yield. + call _ZN4obos9scheduler8scheduleEv +; If we got rescheduled, we would've already returned to the caller, as while setting up the interrupt frame, the RIP was the return address pushed by call. +; If not, hang + jmp $ +idleTask: + hlt + jmp idleTask +_ZN4obos4arch30JumpToFunctionWithCPUTempStackEPFvmEm: + push rbp + mov rbp, rsp + + mov rsp, [gs:0x30] + add rsp, [gs:0x40] + xchg rdi, rsi + call rsi + + leave + ret \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/thread_ctx.asm b/src/oboskrnl/arch/x86_64/thread_ctx.asm deleted file mode 100644 index 3b27a717..00000000 --- a/src/oboskrnl/arch/x86_64/thread_ctx.asm +++ /dev/null @@ -1,396 +0,0 @@ -; oboskrnl/arch/x86_64/thread_ctx.asm -; -; Copyright (c) 2024 Omar Berrow - -[BITS 64] -[DEFAULT ABS] - -; obos_status CoreS_SetupThreadContext(thread_ctx* ctx, uintptr_t entry, uintptr_t arg1, bool makeUserMode, void* stackBase, size_t stackSize); -; OBOS_NORETURN void CoreS_SwitchToThreadContext(const thread_ctx* ctx); -; void CoreS_SaveRegisterContextAndYield(thread_ctx* ctx); -; obos_status CoreS_FreeThreadContext(thread_ctx* ctx); -; uintptr_t CoreS_CallFunctionOnStack(uintptr_t(*func)(uintptr_t), uintptr_t userdata); -; -; void CoreS_SetThreadIRQL(thread_ctx* ctx, irql newIRQL); -; irql CoreS_GetThreadIRQL(const thread_ctx* ctx); - -global CoreS_SetupThreadContext:function default -global CoreS_SwitchToThreadContext:function hidden -global CoreS_SaveRegisterContextAndYield:function hidden -global CoreS_FreeThreadContext:function hidden -global CoreS_CallFunctionOnStack:function default -global CoreS_CallFunctionOnThreadStack:function hidden -global CoreS_SetThreadIRQL:function hidden -global CoreS_GetThreadIRQL:function hidden -global CoreS_ThreadAlloca:function hidden -global Arch_UserYield:function hidden - -%macro popaq 0 -pop rbp -add rsp, 8 -pop r15 -pop r14 -pop r13 -pop r12 -pop r11 -pop r10 -pop r9 -pop r8 -pop rdi -pop rsi -add rsp, 8 -pop rbx -pop rdx -pop rcx -pop rax -%endmacro - -struc thread_ctx -align 8 -.extended_ctx_ptr: resq 1 -.irql: resq 1 -.cr3: resq 1 -.gs_base: resq 1 -.fs_base: resq 1 -.frame: resq 0x1b -.stackBase: resq 1 -.stackSize: resq 1 -.signal_extended_ctx_ptr: resq 1 -endstruc - -extern Core_GetIRQLVar - -extern Arch_HasXSAVE - -section .text -CoreS_SwitchToThreadContext: - ; Disable interrupts, getting an interrupt in the middle of execution of this function can be deadly. - cli - mov rbx, [rdi] - cmp rbx, 0 - je .no_xstate - - mov rax, [Arch_HasXSAVE] - cmp rax, 0 - je .no_xsave - - ; Restore the extended state. - xor rcx,rcx - xgetbv - xrstor [rbx] - jmp .no_xstate - -.no_xsave: - fxrstor [rbx] - -.no_xstate: - add rdi, 8 - -; Restore IRQL. - mov rax, [rdi] - mov cr8, rax - push rdi - call Core_GetIRQLVar - pop rdi - mov rcx, [rdi] - mov [rax], rcx - add rdi, 8 - - ; Restore CR3 (address space) - mov rax, [rdi] - mov rdx, cr3 - cmp rax, rdx - je .kernel_cr3 - -; We won't be able to use kernel memory anymore, so copy the context to the stack. - mov rsi, rdi - sub rsp, 272 - 8 ; push that many bytes - mov rdi, rsp - mov rcx, 272 - 8 ; sizeof(thread_ctx) - 8 (the amount of bytes already accessed) - rep movsb - sub rdi, 272 - 8 - -.kernel_cr3: - mov cr3, rax - add rdi, 8 - - ; Restore GS_BASE - test qword [rdi+16+0xB8], 0x3 - jz .restore_fs_base - swapgs - mov eax, [rdi] - mov edx, [rdi+4] - mov ecx, 0xC0000101 - wrmsr - -.restore_fs_base: - - add rdi, 0x8 - ; Restore FS_BASE - mov eax, [rdi] - mov edx, [rdi+4] - mov ecx, 0xC0000100 - wrmsr - add rdi, 8 - - ; Restore thread GPRs. - mov rsp, rdi - add rsp, 0x10 ; Skip the saved DS and CR3 - popaq - add rsp, 0x18 - iretq -global CoreS_SwitchToThreadContextEnd: data hidden -CoreS_SwitchToThreadContextEnd: -section .pageable.text -extern Arch_FreeXSAVERegion -CoreS_FreeThreadContext: - push rbp - mov rbp, rsp - - push rdi - mov rdi, [rdi+thread_ctx.extended_ctx_ptr] - call Arch_FreeXSAVERegion - pop rdi - push rdi - mov rdi, [rdi+thread_ctx.signal_extended_ctx_ptr] - call Arch_FreeXSAVERegion - pop rdi - - xor rax,rax ; OBOS_STATUS_SUCCESS - - leave - ret - -extern Arch_AllocateXSAVERegion - -CoreS_SetupThreadContext: - push rbp - mov rbp, rsp - - ; rdi, rsi, rdx, rcx, r8, r9 - ; thread_ctx* ctx, uintptr_t entry, uintptr_t arg1, bool makeUserMode, void* stackBase, size_t stackSize - - cmp rdi, 0 - jnz .L1 - mov rax, 2 ; OBOS_STATUS_INVALID_ARGUMENT - jmp .finish - -.L1: - ; Setup the registers. - mov [rdi+thread_ctx.frame+0xB0], rsi ; ctx->frame.rip - mov [rdi+thread_ctx.frame+0x60], rdx ; ctx->frame.rdi - mov qword [rdi+thread_ctx.frame+0xB8], 0x8 ; ctx->frame.cs - mov qword [rdi+thread_ctx.frame+0xD0], 0x10 ; ctx->frame.ss - mov qword [rdi+thread_ctx.frame+0xC0], 0x200202 ; ctx->frame.rflags - lea rax, [r8+r9] - mov qword [rdi+thread_ctx.frame+0xC8], rax ; ctx->frame.rsp - mov qword [rdi+thread_ctx.extended_ctx_ptr], 0 ; ctx->extended_ctx_ptr - mov [rdi+thread_ctx.stackBase], r8 - mov [rdi+thread_ctx.stackSize], r9 - - cmp rcx, 1 - jne .kmode - -.userspace: - mov qword [rdi+thread_ctx.frame+0xB8], 0x20|3 ; ctx->frame.cs=user (CPL3) code segment - mov qword [rdi+thread_ctx.frame+0xD0], 0x18|3 ; ctx->frame.ss=user (CPL3) data segment - - push rdi - call Arch_AllocateXSAVERegion - pop rdi - mov qword [rdi+thread_ctx.extended_ctx_ptr], rax - jmp .finish - -.kmode: - - ; Setup the IRQL. - mov byte [rdi+thread_ctx.irql], 0 ; Unmasked - ; Setup the page map. - mov rax, cr3 - mov qword [rdi+thread_ctx.cr3], rax - ; Setup GS_BASE. - ;mov rcx, 0xC0000101 ; GS.Base - ;rdmsr - ;shl rdx, 32 - ;or rax, rdx - ;mov qword [rdi+thread_ctx.gs_base], rax - -%if OBOS_ENABLE_PROFILING - mov rax, [rdi+thread_ctx.frame+0xC8] - sub rax, 8 - mov [rdi+thread_ctx.frame+0xC8], rax -%endif - - xor rax, rax ; OBOS_STATUS_SUCCESS -.finish: - leave - ret -global CoreS_SetThreadPageTable -CoreS_SetThreadPageTable: - mov qword [rdi+thread_ctx.cr3], rsi - ret -section .text -extern Arch_GetCPUTempStack -extern Arch_GetIST1Stack -CoreS_CallFunctionOnStack: - push rbp - mov rbp, rsp - - push rdi - push rsi - call Arch_GetCPUTempStack - pop rsi - pop rdi - lea rsp, [rax+0x20000] - - xchg rdi, rsi - call rsi - - leave - ret -extern Core_Schedule -extern Core_GetIRQLVar -extern Arch_cpu_local_curr_offset -CoreS_SaveRegisterContextAndYield: - pushfq - cli - ; Save the current GPRs. - mov [rdi+thread_ctx.frame+0x10] , rbp - mov qword [rdi+thread_ctx.frame+0x18], 0 - mov [rdi+thread_ctx.frame+0x40], r12 - mov [rdi+thread_ctx.frame+0x48], r13 - mov [rdi+thread_ctx.frame+0x50], r14 - mov [rdi+thread_ctx.frame+0x58], r15 - mov qword [rdi+thread_ctx.frame+0x70], 0 - mov [rdi+thread_ctx.frame+0x78], rbx - mov rax, ds - mov [rdi+thread_ctx.frame+0x08], rax ; ds - mov rax, [rsp+8] - mov [rdi+thread_ctx.frame+0xB0], rax ; return address - mov qword [rdi+thread_ctx.frame+0xB8], 0x08 ; cs - pop rax ; see pushfq at the beginning of the function - push rax - mov [rdi+thread_ctx.frame+0xC0], rax ; rflags - lea rax, [rsp+0x10] ; skip the return address - mov [rdi+thread_ctx.frame+0xC8], rax ; rsp - mov qword [rdi+thread_ctx.frame+0xD0], 0x10 ; ss - mov rax, [Arch_cpu_local_curr_offset] - mov rax, [gs:rax] - mov [rdi+thread_ctx.gs_base], rax - mov ecx, 0xC0000100 - rdmsr - shl rdx, 32 - or rax, rdx - mov [rdi+thread_ctx.fs_base], rax - mov rax, cr3 - mov [rdi+thread_ctx.cr3], rax - mov [rdi+thread_ctx.frame+0x00], rax ; cr3 - - cmp qword [rdi+thread_ctx.extended_ctx_ptr], 0 - jz .call_scheduler - - mov rbx, [Arch_HasXSAVE] - cmp rbx, 0 - je .no_xsave - - xor rcx,rcx - xgetbv - - mov rbx, [rdi+thread_ctx.extended_ctx_ptr] - xsave [rbx] - jmp .call_scheduler - -.no_xsave: - mov rbx, [rdi+thread_ctx.extended_ctx_ptr] - fxsave [rax] - -.call_scheduler: - popfq ; see the rflags saving code at the beginning of the function - - mov rdi, Core_Schedule - xor rsi,rsi - call CoreS_CallFunctionOnStack - ret ; There is the chance that the scheduler returned because the threads quantum has not been finished yet. -; When the scheduler switches to the current thread context, the rip will be at the return address, as we set rip to the return address passed on the stack -CoreS_SetThreadIRQL: - push rbp - mov rbp, rsp - - mov [rdi+thread_ctx.irql], sil - - leave - ret -CoreS_GetThreadIRQL: - push rbp - mov rbp, rsp - - mov rax, [rdi+thread_ctx.irql] - - leave - ret -global CoreS_GetThreadStack -global CoreS_GetThreadStackSize -CoreS_GetThreadStackSize: - push rbp - mov rbp, rsp - - mov rax, [rdi+thread_ctx.stackSize] - - leave - ret -CoreS_GetThreadStack: - push rbp - mov rbp, rsp - - mov rax, [rdi+thread_ctx.stackBase] - - leave - ret - -CoreS_ThreadAlloca: - push rbp - mov rbp, rsp - - cmp rdx, 0 - jz .status_nullptr1 - mov dword [rdx], 0 ; OBOS_STATUS_SUCCESS -.status_nullptr1: - - ; ret = ctx->frame.rsp -= size - mov rax, [rdi+thread_ctx.frame+0xc8] - sub rax, rsi - mov [rdi+thread_ctx.frame+0xc8], rax - ; if (ret < ctx->stackBase) - cmp rax, [rdi+thread_ctx.stackBase] - jae .done - - cmp rdx, 0 - jz .status_nullptr2 - ; if (status) *status = OBOS_STATUS_NOT_ENOUGH_MEMORY - mov dword [rdx], 6 ; OBOS_STATUS_NOT_ENOUGH_MEMORY -.status_nullptr2: - ; ret = nullptr - xor rax,rax -.done: - - ; return ret; - leave - ret - -extern Core_Yield -Arch_UserYield: - push rbp - mov rbp, rsp - -; lea rsp, [rdi+0x10000] - call Core_Yield - - leave - ret - -global Core_GetCurrentThread -Core_GetCurrentThread: - mov rax, [gs:0x8] - ret - -section .text diff --git a/src/oboskrnl/arch/x86_64/thread_ctx.h b/src/oboskrnl/arch/x86_64/thread_ctx.h deleted file mode 100644 index 80e556d9..00000000 --- a/src/oboskrnl/arch/x86_64/thread_ctx.h +++ /dev/null @@ -1,27 +0,0 @@ -/* -* oboskrnl/arch/x86_64/thread_ctx.asm -* -* Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include - -// Don't forget to update this in the assembly code (thread_ctx.asm) -struct thread_context_info -{ - void* extended_ctx_ptr; - uint8_t irql; - uintptr_t cr3; - uint64_t gs_base, fs_base; - interrupt_frame frame; - void* stackBase; - size_t stackSize; - void* signal_extended_ctx_ptr; -} OBOS_ALIGN(8); - -extern void Arch_UserYield(void* kstck); diff --git a/src/oboskrnl/arch/x86_64/timer.c b/src/oboskrnl/arch/x86_64/timer.c deleted file mode 100644 index e1a0b042..00000000 --- a/src/oboskrnl/arch/x86_64/timer.c +++ /dev/null @@ -1,310 +0,0 @@ -/* - * oboskrnl/arch/x86_64/timer.c - * - * Copyright (c) 2024-2026 Omar Berrow - */ - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include - -extern uint64_t Arch_FindCounter(uint64_t hz); -atomic_size_t nCPUsWithInitializedTimer; - -static irq_handler s_timer_cb; - -void Arch_SchedulerIRQHandlerEntry(irq* obj, interrupt_frame* frame, void* userdata, irql oldIrql) -{ - if (!CoreS_GetCPULocalPtr()->arch_specific.initializedSchedulerTimer) - { - Arch_LAPICSetTimerConfiguration(0x20000 | (Core_SchedulerIRQ->vector->id + 0x20), - Arch_FindCounter(Core_SchedulerTimerFrequency), - 0xb); - OBOS_Debug("Initialized timer for CPU %d.\n", CoreS_GetCPULocalPtr()->id); - CoreS_GetCPULocalPtr()->arch_specific.initializedSchedulerTimer = true; - nCPUsWithInitializedTimer++; - } - else - { - if (s_timer_cb && CoreS_GetCPULocalPtr() == Core_CpuInfo) - s_timer_cb(obj, frame, userdata, oldIrql); - if (frame->cs & 0x3) - Arch_UserYield(Core_GetCurrentThread()->kernelStack); // switches to the kernel stack passed, then yields, then returns. - else - Core_Yield(); - } -} - -HPET* Arch_HPETAddress; -uint64_t Arch_HPETFrequency; -timer_frequency CoreS_TimerFrequency; -uint64_t Arch_CalibrateHPET(uint64_t freq) -{ - if (!Arch_HPETFrequency) - Arch_HPETFrequency = 1000000000000000 / Arch_HPETAddress->generalCapabilitiesAndID.counterCLKPeriod; - Arch_HPETAddress->generalConfig &= ~(1 << 0); - uint64_t compValue = Arch_HPETAddress->mainCounterValue + (Arch_HPETFrequency / freq); - Arch_HPETAddress->timer0.timerConfigAndCapabilities &= ~(1 << 2); - Arch_HPETAddress->timer0.timerConfigAndCapabilities &= ~(1 << 3); - return compValue; -} -#define OffsetPtr(ptr, off, t) ((t*)(((uintptr_t)(ptr)) + (off))) -extern obos_status Arch_MapPage(uintptr_t cr3, void* at_, uintptr_t phys, uintptr_t flags, bool e); -static OBOS_PAGEABLE_FUNCTION OBOS_NO_UBSAN void InitializeHPET() -{ - static basicmm_region hpet_region; - hpet_region.mmioRange = true; - ACPIRSDPHeader* rsdp = (ACPIRSDPHeader*)Arch_MapToHHDM(Arch_RSDPBase); - bool tables32 = rsdp->Revision < 2; - ACPISDTHeader* xsdt = tables32 ? (ACPISDTHeader*)(uintptr_t)rsdp->RsdtAddress : (ACPISDTHeader*)rsdp->XsdtAddress; - xsdt = (ACPISDTHeader*)Arch_MapToHHDM((uintptr_t)xsdt); - size_t nEntries = (xsdt->Length - sizeof(*xsdt)) / (tables32 ? 4 : 8); - HPET_Table* hpet_table = nullptr; - for (size_t i = 0; i < nEntries; i++) - { - uintptr_t phys = tables32 ? OffsetPtr(xsdt, sizeof(*xsdt), uint32_t)[i] : OffsetPtr(xsdt, sizeof(*xsdt), uint64_t)[i]; - ACPISDTHeader* header = (ACPISDTHeader*)Arch_MapToHHDM(phys); - if (memcmp(header->Signature, "HPET", 4)) - { - hpet_table = (HPET_Table*)header; - break; - } - } - if (!hpet_table) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "No HPET!\n"); - uintptr_t phys = hpet_table->baseAddress.address; - Arch_HPETAddress = (HPET*)0xffffffffffffd000; - Arch_MapPage(getCR3(), Arch_HPETAddress, phys, 0x8000000000000013, false); - OBOSH_BasicMMAddRegion(&hpet_region, Arch_HPETAddress, 0x1000); -} -static void hpet_irq_move_callback(irq* i, irq_vector* from, irq_vector* to, void* userdata) -{ - OBOS_UNUSED(i); - OBOS_UNUSED(from); - HPET_Timer* timer = (HPET_Timer*)userdata; - OBOS_ASSERT(timer); - uint32_t gsi = (timer->timerConfigAndCapabilities >> 9) & 0b11111; - Arch_IOAPICMapIRQToVector(gsi, to->id+0x20, false, TriggerModeLevelSensitive); - Arch_IOAPICMaskIRQ(gsi, false); -} -OBOS_NO_KASAN OBOS_NO_UBSAN void hpet_irq_handler(struct irq* i, interrupt_frame* frame, void* userdata, irql oldIrql) -{ - // HPET_Timer* timer = (HPET_Timer*)i->irqCheckerUserdata; - // OBOS_ASSERT(timer); - // size_t timerIndex = ((uintptr_t)timer-(uintptr_t)Arch_HPETAddress-offsetof(HPET, timer0))/sizeof(HPET_Timer); - // Arch_HPETAddress->generalInterruptStatus &= ~(1<mainCounterValue + (fixedpt_toint(tp)+1)); - - s_invariant_tsc_frequency = (change_rate / n) * 100000; - // s_invariant_tsc_frequency = 2380800000UL; - } while(0); - - down: - (void)0; - - OBOS_Debug("Using invariant TSC. TSC Frequency is %zu Hz\n", s_invariant_tsc_frequency); - - return OBOS_STATUS_SUCCESS; - } - - obos_status status = Core_IrqObjectInitializeIRQL(Core_TimerIRQ, IRQL_TIMER, false, true); - if (obos_is_error(status)) - return status; - Core_TimerIRQ->moveCallback = hpet_irq_move_callback; - Core_TimerIRQ->handler = hpet_irq_handler; - Core_TimerIRQ->handlerUserdata = handler; - volatile HPET_Timer* timer = &Arch_HPETAddress->timer0; - // TODO: Make this support choosing a different timer. - if (!(timer->timerConfigAndCapabilities & (1<<4))) - OBOS_Panic(OBOS_PANIC_DRIVER_FAILURE, "HPET Timer does not support periodic mode."); - if (!(timer->timerConfigAndCapabilities & (1<<5))) - OBOS_Panic(OBOS_PANIC_DRIVER_FAILURE, "HPET Timer is not a 64-bit timer."); - Core_TimerIRQ->irqCheckerUserdata = (void*)timer; - Core_TimerIRQ->irqMoveCallbackUserdata = (void*)timer; - uint32_t irqRouting = timer->timerConfigAndCapabilities >> 32; - if (!irqRouting) - OBOS_Panic(OBOS_PANIC_DRIVER_FAILURE, "HPET Timer does not support irq routing through the I/O APIC."); - volatile uint32_t gsi = UINT32_MAX; - do { - uint32_t cgsi = __builtin_ctz(irqRouting); - if (Arch_IOAPICGSIUsed(cgsi) == OBOS_STATUS_SUCCESS) - { - gsi = cgsi; - break; - } - irqRouting &= (1timerConfigAndCapabilities |= (1<6)|(1<<3)|((uint8_t)gsi<<9); // Edge-triggered IRQs, set GSI, Periodic timer - CoreS_TimerFrequency = 1000; - OBOS_Debug("HPET frequency: %ld, configured HPET frequency: %ld\n", Arch_HPETFrequency, CoreS_TimerFrequency); - const uint64_t value = Arch_HPETFrequency/CoreS_TimerFrequency; - timer->timerComparatorValue = Arch_HPETAddress->mainCounterValue + value; - timer->timerComparatorValue = value; - timer->timerConfigAndCapabilities |= (1<<1); // Enable IRQs - Arch_IOAPICMapIRQToVector(gsi, Core_TimerIRQ->vector->id+0x20, true, TriggerModeEdgeSensitive); - Arch_IOAPICMaskIRQ(gsi, false); - Arch_HPETAddress->generalConfig = 0b01; - initialized = true; - return OBOS_STATUS_SUCCESS; -} - -__attribute__((no_instrument_function)) timer_tick CoreS_GetTimerTick() -{ - static volatile uint64_t cached_divisor = 0; - if (!cached_divisor) - cached_divisor = CoreS_GetNativeTimerFrequency() / CoreS_TimerFrequency; - if (obos_expect(s_use_invariant_tsc, true)) - return rdtsc() / cached_divisor; - return Arch_HPETAddress->mainCounterValue / cached_divisor; -} - -__attribute__((no_instrument_function)) timer_tick CoreS_GetNativeTimerTick() -{ - if (obos_expect(s_use_invariant_tsc, true)) - return rdtsc(); - if (obos_expect(!Arch_HPETAddress, false)) - return 0; - return Arch_HPETAddress->mainCounterValue; -} - -__attribute__((no_instrument_function)) uint64_t CoreS_GetNativeTimerFrequency() -{ - if (obos_expect(s_use_invariant_tsc, true)) - return s_invariant_tsc_frequency; - return Arch_HPETFrequency; -} - -uint64_t CoreS_TimerTickToNS(timer_tick tp) -{ - // 1000000000/freq*tp - static uint64_t cached_rate = 0; - if (!cached_rate) - cached_rate = 1000000000/CoreS_TimerFrequency; - return cached_rate * tp; -} - -void Arch_InitializeSchedulerTimer() -{ - obos_status status = OBOS_STATUS_SUCCESS; - InitializeHPET(); - Core_SchedulerIRQ = Core_IrqObjectAllocate(&status); - if (obos_is_error(status)) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "Could not initialize the scheduler IRQ. Status: %d.\n", status); - status = Core_IrqObjectInitializeIRQL(Core_SchedulerIRQ, IRQL_DISPATCH, false, true); - if (obos_is_error(status)) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "Could not initialize the scheduler IRQ. Status: %d.\n", status); - Core_SchedulerIRQ->handler = Arch_SchedulerIRQHandlerEntry; - Core_SchedulerIRQ->handlerUserdata = nullptr; - Core_SchedulerIRQ->irqChecker = nullptr; - Core_SchedulerIRQ->irqCheckerUserdata = nullptr; - // Hopefully this won't cause trouble. - Core_SchedulerIRQ->choseVector = true; - Core_SchedulerIRQ->vector->nIRQsWithChosenID = 1; - ipi_lapic_info target = { - .isShorthand = true, - .info = { - .shorthand = LAPIC_DESTINATION_SHORTHAND_ALL, - } - }; - ipi_vector_info vector = { - .deliveryMode = LAPIC_DELIVERY_MODE_FIXED, - .info.vector = Core_SchedulerIRQ->vector->id + 0x20 - }; - Core_LowerIrql(IRQL_PASSIVE); - Arch_LAPICSendIPI(target, vector); - while (nCPUsWithInitializedTimer != Core_CpuCount) - pause(); - OBOS_Debug("%s: Scheduler timer is running at %d hz.\n", __func__, Core_SchedulerTimerFrequency); -} diff --git a/src/oboskrnl/arch/x86_64/timer.h b/src/oboskrnl/arch/x86_64/timer.h deleted file mode 100644 index b0ba0412..00000000 --- a/src/oboskrnl/arch/x86_64/timer.h +++ /dev/null @@ -1,12 +0,0 @@ -/* - * oboskrnl/arch/x86_64/timer.h - * - * Copyright (c) 2024 Omar Berrow - */ - -#pragma once - -#include - -// NOTE: To be called at > IRQL_PASSIVE. -void Arch_InitializeSchedulerTimer(); diff --git a/src/oboskrnl/arch/x86_64/trace.cpp b/src/oboskrnl/arch/x86_64/trace.cpp new file mode 100644 index 00000000..5b7000f5 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/trace.cpp @@ -0,0 +1,140 @@ +/* + oboskrnl/arch/x86_64/trace.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include + +#include + +namespace obos +{ + extern volatile limine_kernel_file_request kernel_file; + extern volatile limine_kernel_address_request kernel_addr; +#define TO_EHDR(obj) ((elf::Elf64_Ehdr*)obj) +#define TO_STRING_TABLE(ehdr, stable, off) ((const char*)ehdr + stable + off) + const elf::Elf64_Shdr* getSectionHeader(const elf::Elf64_Ehdr* ehdr, const char* name) + { + uintptr_t base = (uintptr_t)ehdr; + const elf::Elf64_Shdr* sections = (elf::Elf64_Shdr*)(base + ehdr->e_shoff); + size_t shdr_stable = sections[ehdr->e_shstrndx].sh_offset; + for (size_t i = 0; i < ehdr->e_shnum; i++) + if (strcmp(TO_STRING_TABLE(ehdr, shdr_stable, sections[i].sh_name), name)) + return §ions[i]; + return nullptr; + } + void addr2sym(uintptr_t rip, const char** const symbolName, uintptr_t* symbolBase, uint8_t symType) + { + if (rip < kernel_addr.response->virtual_base) + { + *symbolName = nullptr; + *symbolBase = 0; + return; + } + uintptr_t base = (uintptr_t)kernel_file.response->kernel_file->address; + elf::Elf64_Ehdr* ehdr = (elf::Elf64_Ehdr*)base; + size_t stable = 0; + if (getSectionHeader(ehdr, ".strtab")) + stable = getSectionHeader(ehdr, ".strtab")->sh_offset; + const elf::Elf64_Shdr* symtab = getSectionHeader(ehdr, ".symtab"); + if (!symtab) + { + *symbolName = nullptr; + *symbolBase = 0; + return; + } + size_t nEntries = symtab->sh_size / symtab->sh_entsize; + for (size_t i = 0; i < nEntries; i++) + { + elf::Elf64_Sym* symbol = (elf::Elf64_Sym*)(base + symtab->sh_offset + i*symtab->sh_entsize); + if (symType != elf::STT_NOTYPE) + if ((symbol->st_info & 0xf) != symType) + continue; + if (rip >= symbol->st_value && rip < (symbol->st_value + symbol->st_size)) + { + if (stable) + *symbolName = TO_STRING_TABLE(ehdr, stable, symbol->st_name); + else + *symbolName = "no strtab"; + *symbolBase = symbol->st_value; + return; + } + } + } + namespace logger + { + struct stack_frame + { + stack_frame* down; + uintptr_t rip; + }; + static void stackTraceNoFuncName(stack_frame* frame, const char* prefix) + { + while (frame) + { + printf("\t0x%016lx: Cannot get function name\n", frame->rip); + frame = frame->down; + } + } + void stackTrace(void* parameter, const char* prefix, size_t(*outputCallback)(const char* format, ...)) + { + stack_frame* frame = parameter ? (stack_frame*)parameter : (stack_frame*)__builtin_frame_address(0); + if (!vmm::g_initialized) + { + stackTraceNoFuncName(frame, prefix); + return; + } + int32_t nFrames = 0; + vmm::page_descriptor pd[2]; + memzero(pd, sizeof(pd)); + vmm::Context* ctx = &vmm::g_kernelContext; + if (scheduler::GetCPUPtr() && scheduler::GetCPUPtr()->currentThread) + ctx = scheduler::GetCPUPtr()->currentThread->addressSpace; + do + { + OBOS_ASSERTP(vmm::GetPageDescriptor(ctx, frame, sizeof(*frame), pd, 2) != SIZE_MAX, "Could not retrieve page descriptors for page %p.",, frame); + for (size_t i = 0; i < (1 + (size_t)OBOS_CROSSES_PAGE_BOUNDARY(frame, sizeof *frame)); i++) + { + if (!pd[i].present) + { + frame = nullptr; + if (nFrames) + nFrames--; + break; + } + } + + if (!frame) + break; + frame = frame->down; + nFrames++; + } while (frame); + nFrames--; + frame = parameter ? (stack_frame*)parameter : (stack_frame*)__builtin_frame_address(0); + while (nFrames-- > 0) + { + const char* functionName = nullptr; + uintptr_t functionStart = 0; + addr2sym(frame->rip, &functionName, &functionStart, elf::STT_FUNC); + if (functionName) + outputCallback("%s0x%016lx: %s+%ld\n", prefix, frame->rip, functionName, (uintptr_t)frame->rip - functionStart); + else + outputCallback("%s0x%016lx: External Code\n", prefix, frame->rip); + frame = frame->down; + } + } + } +} \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/vmm_defines.h b/src/oboskrnl/arch/x86_64/vmm_defines.h new file mode 100644 index 00000000..441ee2d4 --- /dev/null +++ b/src/oboskrnl/arch/x86_64/vmm_defines.h @@ -0,0 +1,43 @@ +/* + oboskrnl/arch/x86_64/vmm_defines.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include +#include + +namespace obos +{ + namespace vmm + { + enum + { + PROT_x86_64_WRITE_COMBINING_CACHE = 0x0100'0000, + PROT_x86_64_WRITE_THROUGH_CACHE = 0x0200'0000, + }; + } + namespace arch + { + OBOS_EXPORT extern uintptr_t getHHDMLimit(); + OBOS_EXPORT extern uintptr_t getKernelBase(); + OBOS_EXPORT extern uintptr_t getKernelTop(); + } +} +#define OBOS_LEVELS_PER_PAGEMAP (4) +#define OBOS_CHILDREN_PER_PT (512) +#define OBOS_PAGE_SIZE (4096) +#define OBOS_HUGE_PAGE_SIZE (2097152) +#define OBOS_IS_VIRT_ADDR_CANONICAL(addr) (((uintptr_t)(addr) >> 47) == 0 || ((uintptr_t)(addr) >> 47) == 0x1ffff) +#define OBOS_VIRT_ADDR_BITWIDTH (48) +#define OBOS_ADDR_BITWIDTH (64) +#define OBOS_ZERO_PAGE_PHYSICAL ((uintptr_t)0) +#define OBOS_MAX_PAGE_FAULT_HANDLERS (32) +#define OBOS_ADDRESS_SPACE_LIMIT (0xffff'ffff'ffff'ffff) +#define OBOS_KERNEL_ADDRESS_SPACE_BASE (0xffff'8000'0000'0000) +#define OBOS_KERNEL_ADDRESS_SPACE_USABLE_BASE (obos::arch::getHHDMLimit()) +#define OBOS_KERNEL_ADDRESS_SPACE_LIMIT (0xffff'ffff'ffff'f000) +#define OBOS_KERNEL_BASE (obos::arch::getKernelBase()) +#define OBOS_KERNEL_TOP (obos::arch::getKernelTop()) \ No newline at end of file diff --git a/src/oboskrnl/arch/x86_64/wake.c b/src/oboskrnl/arch/x86_64/wake.c deleted file mode 100644 index 4e85ef6f..00000000 --- a/src/oboskrnl/arch/x86_64/wake.c +++ /dev/null @@ -1,365 +0,0 @@ -/* - * oboskrnl/arch/x86_64/wake.c - * - * Copyright (c) 2024 Omar Berrow - */ - -#include -#include -#include -#include -#include - -#include - -#include - -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include -#include - -#include - -#include -#include - -#include - -#include - -#if OBOS_USE_LIMINE -# include -#endif - -struct { - HPET_Timer timer0; - uint64_t mainCounterValue; - uint64_t generalConfig; -} hpet_state; -struct ioapic_state -{ - uint64_t* redirection_entries; - uint8_t arbitrationId; - uint8_t id; -}; -struct ioapic_state* saved_ioapics; // one to one indexing with Arch_IOAPICs -#if OBOS_NVS_SAVE_S3 -struct acpi_nvs { - void* saved_region; - void* region_address; - size_t size; -}; -struct acpi_nvs* saved_nvs; -size_t saved_nvs_count; -#endif -void OBOSS_SuspendSavePlatformState() -{ - Arch_HPETAddress->generalConfig &= ~BIT(0); - hpet_state.mainCounterValue = Arch_HPETAddress->mainCounterValue; - hpet_state.generalConfig = Arch_HPETAddress->generalConfig | 1; - hpet_state.timer0.timerComparatorValue = Arch_HPETAddress->timer0.timerComparatorValue; - hpet_state.timer0.timerFSBInterruptRoute = Arch_HPETAddress->timer0.timerFSBInterruptRoute; - hpet_state.timer0.timerConfigAndCapabilities = Arch_HPETAddress->timer0.timerConfigAndCapabilities; - Arch_HPETAddress->generalConfig |= BIT(0); - - saved_ioapics = ZeroAllocate(OBOS_NonPagedPoolAllocator, Arch_IOAPICCount, sizeof(struct ioapic_state), nullptr); - for (size_t i = 0; i < Arch_IOAPICCount; i++) - { - saved_ioapics[i].arbitrationId = (ArchH_IOAPICReadRegister(Arch_IOAPICs[i].address, 8) >> 24) & 0xf; - saved_ioapics[i].id = (ArchH_IOAPICReadRegister(Arch_IOAPICs[i].address, 0) >> 24) & 0xf; - saved_ioapics[i].redirection_entries = ZeroAllocate(OBOS_NonPagedPoolAllocator, - Arch_IOAPICs[i].maxRedirectionEntries, - sizeof(uint64_t), - nullptr); - for (uint8_t entry = 0; entry < Arch_IOAPICs[i].maxRedirectionEntries; entry++) - { - uint64_t val = 0; - val = ArchH_IOAPICReadRegister(Arch_IOAPICs[i].address, 0x40+entry*8); - val |= ((uint64_t)ArchH_IOAPICReadRegister(Arch_IOAPICs[i].address, 0x44+entry*8)) << 32; - saved_ioapics[i].redirection_entries[entry] = val; - } - } - - // Save NVS, if requested. -#if OBOS_NVS_SAVE_S3 - if (!OBOS_GetOPTF("nvs-nosave-s3")) - { - // Save ACPI NVS regions. - // Why? Some versions of windows do, so I guess we have to :) - - // Read the memory map. - uintptr_t index = 0; - for (obos_pmem_map_entry* entry = MmS_GetFirstPMemMapEntry(&index); entry; ) - { - if (entry->type == ULTRA_MEMORY_TYPE_ACPI_NVS) - saved_nvs_count++; - - entry = MmS_GetNextPMemMapEntry(entry, &index); - } - - saved_nvs = ZeroAllocate(OBOS_NonPagedPoolAllocator, saved_nvs_count, sizeof(struct acpi_nvs), nullptr); - - index = 0; - size_t nvs_index = 0; -# if OBOS_USE_LIMINE - for (obos_pmem_map_entry* entry = MmS_GetFirstPMemMapEntry(&index); entry; ) - { - if (entry->type == LIMINE_MEMMAP_ACPI_NVS) - { - saved_nvs[nvs_index].region_address = Arch_MapToHHDM(entry->base); - saved_nvs[nvs_index].size = entry->length; - saved_nvs[nvs_index].saved_region = Allocate(OBOS_NonPagedPoolAllocator, - saved_nvs[nvs_index].size, - nullptr); - memcpy(saved_nvs[nvs_index].saved_region, saved_nvs[nvs_index].region_address, saved_nvs[nvs_index].size); - nvs_index++; - } - - entry = MmS_GetNextPMemMapEntry(entry, &index); - } -# else - for (obos_pmem_map_entry* entry = MmS_GetFirstPMemMapEntry(&index); entry; ) - { - if (entry->type == ULTRA_MEMORY_TYPE_ACPI_NVS) - { - saved_nvs[nvs_index].region_address = Arch_MapToHHDM(entry->physical_address); - saved_nvs[nvs_index].size = entry->size; - saved_nvs[nvs_index].saved_region = Allocate(OBOS_NonPagedPoolAllocator, - saved_nvs[nvs_index].size, - nullptr); - memcpy(saved_nvs[nvs_index].saved_region, saved_nvs[nvs_index].region_address, saved_nvs[nvs_index].size); - nvs_index++; - } - - entry = MmS_GetNextPMemMapEntry(entry, &index); - } -# endif - } -#endif -} - -static void restore_nvs() -{ -#if OBOS_NVS_SAVE_S3 - for (size_t i = 0; i < saved_nvs_count; i++) - { - memcpy(saved_nvs[i].region_address, saved_nvs[i].saved_region, saved_nvs[i].size); - Free(OBOS_NonPagedPoolAllocator, saved_nvs[i].saved_region, saved_nvs[i].size); - } - Free(OBOS_NonPagedPoolAllocator, saved_nvs, saved_nvs_count*sizeof(struct acpi_nvs)); - saved_nvs_count = 0; -#endif -} - -static void restore_hpet() -{ - Arch_HPETAddress->mainCounterValue = hpet_state.mainCounterValue; - Arch_HPETAddress->timer0.timerComparatorValue = hpet_state.timer0.timerComparatorValue; - Arch_HPETAddress->timer0.timerFSBInterruptRoute = hpet_state.timer0.timerFSBInterruptRoute; - Arch_HPETAddress->timer0.timerConfigAndCapabilities = hpet_state.timer0.timerConfigAndCapabilities; - Arch_HPETAddress->generalConfig = hpet_state.generalConfig; -} - -static void restore_ioapics() -{ - for (size_t i = 0; i < Arch_IOAPICCount; i++) - { - uint32_t tmp = 0; - - tmp = ((uint32_t)saved_ioapics[i].id) << 24; - ArchH_IOAPICWriteRegister(Arch_IOAPICs[i].address, 0x0, tmp); - - tmp = ((uint32_t)saved_ioapics[i].arbitrationId) << 24; - ArchH_IOAPICWriteRegister(Arch_IOAPICs[i].address, 0x8, tmp); - - for (uint8_t entry = 0; entry < Arch_IOAPICs[i].maxRedirectionEntries; entry++) - { - tmp = saved_ioapics[i].redirection_entries[entry] >> 32; - ArchH_IOAPICWriteRegister(Arch_IOAPICs[i].address, 0x44+entry*8, tmp); - tmp = saved_ioapics[i].redirection_entries[entry] & 0xffffffff; - ArchH_IOAPICWriteRegister(Arch_IOAPICs[i].address, 0x40+entry*8, tmp); - } - Free(OBOS_NonPagedPoolAllocator, saved_ioapics[i].redirection_entries, Arch_IOAPICs[i].maxRedirectionEntries*sizeof(uint64_t)); - } - Free(OBOS_NonPagedPoolAllocator, saved_ioapics, Arch_IOAPICCount*sizeof(struct ioapic_state)); -} - -extern uint8_t Arch_SMPTrampolineStart[]; -extern uint8_t Arch_SMPTrampolineEnd[]; -extern uint64_t Arch_SMPTrampolineCR3; -extern uint64_t Arch_SMPTrampolineRSP; -extern uint64_t Arch_SMPTrampolineCPULocalPtr; - -#define OffsetPtr(ptr, off, t) ((t*)(((uintptr_t)(ptr)) + (off))) -static OBOS_NO_UBSAN void SetMemberInSMPTrampoline(uint8_t off, uint64_t val) -{ - *OffsetPtr(0x1000, off, uint64_t) = val; -} - -obos_status Arch_MapPage(uintptr_t cr3, void* at_, uintptr_t phys, uintptr_t flags, bool free_pte); -uint32_t OBOSS_WakeVector = 0; -extern char Arch_ACPIWakeTrampoline; -extern char Arch_ACPIWakeTrampoline_data; -extern char Arch_ACPIWakeTrampoline_end; -extern uintptr_t Arch_SMPTrampolineWakeLocation; -extern void Arch_FlushGDT(uintptr_t gdtr); -static void reinit_tss_segment(cpu_local* info) -{ - struct - { - uint16_t limitLow; - uint16_t baseLow; - uint8_t baseMiddle1; - uint8_t access; - uint8_t gran; - uint8_t baseMiddle2; - uint32_t baseHigh; - uint32_t resv1; - } tss_entry; - memzero(&tss_entry, sizeof(tss_entry)); - uintptr_t tss = (uintptr_t)&info->arch_specific.tss; - tss_entry.limitLow = sizeof(info->arch_specific.tss); - tss_entry.baseLow = tss & 0xffff; - tss_entry.baseMiddle1 = (tss >> 16) & 0xff; - tss_entry.baseMiddle2 = (tss >> 24) & 0xff; - tss_entry.baseHigh = (tss >> 32) & 0xffffffff; - tss_entry.access = 0x89; - tss_entry.gran = 0x40; - info->arch_specific.gdtEntries[5] = *((uint64_t*)&tss_entry + 0); - info->arch_specific.gdtEntries[6] = *((uint64_t*)&tss_entry + 1); -} -extern void Arch_InitializeMiscFeatures(); -atomic_bool ap_initialized; -#ifdef OBOS_UP -static void restart_cpus() -{} -#else -static void restart_cpus() -{ - for (size_t i = 0; i < Core_CpuCount; i++) - { - if (Core_CpuInfo[i].isBSP) - continue; - memcpy((void*)0x1000, Arch_SMPTrampolineStart, Arch_SMPTrampolineEnd - Arch_SMPTrampolineStart); - Core_CpuInfo[i].arch_specific.startup_stack = Mm_VirtualMemoryAlloc(&Mm_KernelContext, nullptr, 0x4000, 0, VMA_FLAGS_KERNEL_STACK, nullptr, nullptr); - SetMemberInSMPTrampoline((uintptr_t)&Arch_SMPTrampolineRSP - (uintptr_t)Arch_SMPTrampolineStart, (uint64_t)Core_CpuInfo[i].arch_specific.startup_stack + 0x4000); - SetMemberInSMPTrampoline((uintptr_t)&Arch_SMPTrampolineCPULocalPtr - (uintptr_t)Arch_SMPTrampolineStart, (uint64_t)&Core_CpuInfo[i]); - ipi_lapic_info lapic = { - .isShorthand = false, - .info = { - .lapicId = Core_CpuInfo[i].id - }, - }; - ipi_vector_info vector = { - .deliveryMode = LAPIC_DELIVERY_MODE_INIT, - .info = { - .vector = 0, - }, - }; - obos_status status = 0; - if ((status = Arch_LAPICSendIPI(lapic, vector)) != OBOS_STATUS_SUCCESS) - { - OBOS_Error("%s: Could not send IPI. Status: %d.\n", status); - continue; - } - vector.deliveryMode = LAPIC_DELIVERY_MODE_SIPI; - vector.info.address = 0x1000; - if ((status = Arch_LAPICSendIPI(lapic, vector)) != OBOS_STATUS_SUCCESS) - { - OBOS_Error("%s: Could not send IPI. Status: %d.\n", status); - continue; - } - while (!atomic_load(&ap_initialized)) - pause(); - atomic_store(&ap_initialized, false); - } -} -#endif - -extern void Arch_disablePIC(); - -extern uint64_t Arch_FindCounter(uint64_t hz); -static void __attribute__((no_stack_protector)) on_wake(cpu_local* info) -{ - static uint64_t cached_counter; - struct - { - uint16_t limit; - uintptr_t base; - } OBOS_PACK gdtr; - gdtr.limit = sizeof(info->arch_specific.gdtEntries) - 1; - gdtr.base = (uintptr_t)&info->arch_specific.gdtEntries; - wrmsr(0xC0000101 /* GS_BASE */, (uint64_t)info); - reinit_tss_segment(info); - Arch_FlushGDT((uintptr_t)&gdtr); - wrmsr(0xC0000101 /* GS_BASE */, (uint64_t)info); - irql oldIrql = Core_RaiseIrqlNoThread(0xf); - OBOS_UNUSED(oldIrql); - Arch_InitializeMiscFeatures(); - Arch_EnableSIMDFeatures(); - Arch_RestoreMTRRs(); - // UC UC- WT WB UC WC WT WB - wrmsr(0x277, 0x0001040600070406); - asm volatile("mov %0, %%cr3" : :"r"(getCR3())); - wbinvd(); - Arch_LAPICInitialize(info->isBSP); - wrmsr(0xC0000080 /* IA32_EFER */, rdmsr(0xC0000080)|BIT(0)); - Arch_InitializeIDT(false); - OBOSS_InitializeSyscallInterface(); - if (info->isBSP) - { - Arch_disablePIC(); - // Restore NVS - restore_nvs(); - // Restore IOAPICs. - restore_ioapics(); - // Restore the HPET. - restore_hpet(); - // Restart CPUs. - restart_cpus(); - cached_counter = Arch_FindCounter(Core_SchedulerTimerFrequency); - } - else - atomic_store(&ap_initialized, true); - while (!cached_counter) - OBOSS_SpinlockHint(); - - Arch_LAPICSetTimerConfiguration(0x20000 | (Core_SchedulerIRQ->vector->id + 0x20), - cached_counter, - 0xb); - - if (info->isBSP) - { - OBOS_WokeFromSuspend = true; - Core_SuspendScheduler(false); - } - if (CoreS_GetCPULocalPtr()->currentThread == OBOS_SuspendWorkerThread) - OBOS_SuspendWorkerThread->context.frame.rflags &= 0x200; // enter with interrupts disabled - CoreS_SwitchToThreadContext(&CoreS_GetCPULocalPtr()->currentThread->context); -} -obos_status OBOSS_PrepareWakeVector() -{ - OBOSS_WakeVector = 0x1000; - Arch_MapPage(getCR3(), (void*)(uintptr_t)OBOSS_WakeVector, OBOSS_WakeVector, 0x3, false); - Arch_SMPTrampolineCR3 = getCR3(); - Arch_SMPTrampolineCPULocalPtr = (uintptr_t)&Core_CpuInfo[0]; - Arch_SMPTrampolineRSP = (uintptr_t)Mm_VirtualMemoryAlloc(&Mm_KernelContext, nullptr, 0x4000, 0, VMA_FLAGS_KERNEL_STACK, nullptr, nullptr); - Arch_SMPTrampolineRSP += 0x4000; - memcpy((void*)(uintptr_t)OBOSS_WakeVector, &Arch_SMPTrampolineStart, Arch_SMPTrampolineEnd - Arch_SMPTrampolineStart); - Arch_SMPTrampolineWakeLocation = (uintptr_t)on_wake; - return OBOS_STATUS_SUCCESS; -} diff --git a/src/oboskrnl/cmdline.c b/src/oboskrnl/cmdline.c deleted file mode 100644 index 2a4e5e18..00000000 --- a/src/oboskrnl/cmdline.c +++ /dev/null @@ -1,384 +0,0 @@ -/* - * oboskrnl/cmdline.c - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include -#include -#include -#include - -#include - -#include - -OBOS_PAGEABLE_VARIABLE const char* OBOS_KernelCmdLine; -OBOS_PAGEABLE_VARIABLE const char* volatile OBOS_InitrdBinary; -OBOS_PAGEABLE_VARIABLE size_t volatile OBOS_InitrdSize; -OBOS_PAGEABLE_VARIABLE char** OBOS_argv; -OBOS_PAGEABLE_VARIABLE size_t OBOS_argc; -OBOS_PAGEABLE_VARIABLE size_t OBOS_InitArgumentsStart = SIZE_MAX; -OBOS_PAGEABLE_VARIABLE size_t OBOS_InitArgumentsCount; - -static const char* const help_message = -"OBOSKRNL usage:\n" -"NOTE: Any amount of dashes ('-') can be used at the beginning of the option or flag.\n" -"--initrd-module=name: The name or path of the initrd module.\n" -"--initrd-driver-module=name: The name or path of the initrd driver module.\n" -"--load-modules=name[,name]: If an initrd driver is specified, then 'name' is an absolute path\n" -" in the initrd, otherwise it is the name of a module to load as a driver.\n" -"--mount-initrd=pathspec: Mounts the InitRD at pathspec if specified, otherwise the initrd is left unmounted\n" -" when 'init' is called.\n" -"--root-fs-uuid=uuid: Specifies the partition to mount as root. If set to 'initrd', the initrd\n" -" is used as root.\n" -"--root-fs-partid=partid: Specifies the partition to mount as root. If set to 'initrd', the initrd\n" -" is used as root.\n" -"--working-set-cap=bytes: Specifies the kernel's working-set size in bytes.\n" -"--initial-swap-size=bytes: Specifies the size (in bytes) of the initial, in-ram swap.\n" -"--log-level=integer: Specifies the log level of the kernel, 0 meaning all, 4 meaning none.\n" -"--disable-network-error-logs: Disable error logs from the network stack\n" -"--init-path=path: Specifies the path of init. If not present, assumes /init.\n" -"--init-args: Special argument, makes the kernel assume all following arguments are to be passed to the init process.\n" -"--no-init: Disables loading the init process.\n" -"--acpi-no-osi: Don't create the _OSI method when building the namespace. For more info, see documenation for UACPI_FLAG_NO_OSI.\n" -"--acpi-bad-xsdt: Use the RSDT, even if the XSDT is present. For more info, see documenation for UACPI_FLAG_BAD_XSDT.\n" -"--no-smp: Disables SMP. Has the equivalent effect of passing OBOS_UP at build-time.\n" -"--pnp-module-path=pathspec: Where to find kernel modules for PnP during kernel init.\n" -"--disable-libc-log: Disables the logs from the C library (see Sys_LibcLog) .\n" -"--disable-syscall-error-log: Makes all syscall logs happen at DEBUG level.\n" -"--disable-syscall-logs: Disables all syscall logs.\n" -"--boot-tty=devspec: TTY to use on boot. Default is usually tty0\n" -"--tjec-random-access: Makes the underlying TJEC memory accessing randomized.\n" -"--tjec-max-memory-size=bytes: Specifies the maximum amount of memory TJEC is allowed to allocate.\n" -"--tjec-no-fips: Tells TJEC to not over sample per block of bits generated.\n" -"--tjec-no-lag-predictor: Disables TJEC LAG Predictor health checks.\n" -"--tjec-max-acc-loop-bits=<1-8>: Specifies a maximum number of random additional memory accesses TJEC makes per block in 2^k, default k=7 or 128.\n" -"--tjec-max-hash-loop-bits=<1-8>: Specifies a maximum number of random additional hash iterations TJEC makes per block in 2^k, default k=3 or 8.\n" -"--tjec-osr=<1-255>: Specifies the over sampling ratio for TJEC, in other words, how many blocks to collect per block generated.\n" -"--x86-disable-tsc: (x86 only) Disables use of the TSC.\n" -"--x86-tty0-keyboard=devspec: (x86 only) Device to use as the keyboard input to tty0. Default is ps2k1\n" -"--help: Displays this help message.\n"; - -struct cmd_allocation_header -{ - size_t alloc_size; - // set to true if the bump allocator (OBOS_BasicMMAllocatePages) was used. - bool basicmm; -}; - -static void* cmd_malloc(size_t sz) -{ - struct cmd_allocation_header* blk = nullptr; - if (OBOS_KernelAllocator) - { - blk = Allocate(OBOS_KernelAllocator, sz+sizeof(*blk), nullptr); - blk->basicmm = false; - blk->alloc_size = sz; - return blk + 1; - } - sz += sizeof(*blk); - if (sz % OBOS_PAGE_SIZE) - sz += (OBOS_PAGE_SIZE-(sz%OBOS_PAGE_SIZE)); - blk = OBOS_BasicMMAllocatePages(sz, nullptr); - blk->basicmm = true; - blk->alloc_size = sz; - return blk + 1; -} - -static void* cmd_calloc(size_t nobj, size_t szobj) -{ - size_t sz = nobj * szobj; - return memzero(cmd_malloc(sz), sz); -} - -static void cmd_free(void* buf) -{ - struct cmd_allocation_header* hdr = buf; - hdr--; - if (hdr->basicmm) - { - memset(buf, 0x11, hdr->alloc_size); - return; - } - Free(OBOS_KernelAllocator, hdr, hdr->alloc_size+sizeof(*hdr)); -} - -static void* cmd_realloc(void* buf, size_t newsize) -{ - if (!buf) - return cmd_malloc(newsize); - if (!newsize) - { - cmd_free(buf); - return nullptr; - } - struct cmd_allocation_header* hdr = buf; - hdr--; - if (hdr->basicmm) - { - newsize += sizeof(*hdr); - if (newsize % OBOS_PAGE_SIZE) - newsize += (OBOS_PAGE_SIZE-(newsize%OBOS_PAGE_SIZE)); - if (newsize == hdr->alloc_size) - return buf; - hdr->alloc_size = newsize; - void* newbuf = cmd_malloc(newsize); - if (newsize < hdr->alloc_size) - memcpy(newbuf, hdr, newsize); - else - memcpy(newbuf, hdr, hdr->alloc_size+sizeof(*hdr)); - cmd_free(buf); // probably a no-op, but do it anyway. - return newbuf; - } - size_t oldsz = hdr->alloc_size; - hdr->alloc_size = newsize; - hdr = Reallocate(OBOS_KernelAllocator, hdr, sizeof(*hdr)+newsize, sizeof(*hdr)+oldsz, nullptr); - return hdr + 1; -} - -// Parses the command line into argv and argc -void OBOS_ParseCMDLine() -{ - size_t cmdlinelen = OBOS_KernelCmdLine ? strlen(OBOS_KernelCmdLine) : 0; - if (!cmdlinelen) - return; - - for (const char* iter = OBOS_KernelCmdLine; iter < (OBOS_KernelCmdLine + cmdlinelen); ) - { - OBOS_argv = cmd_realloc(OBOS_argv, (++OBOS_argc)*sizeof(const char* const)); - size_t arg_len = 0; - if (*iter == '\"' || *iter == '\'') - { - char delim = *iter; - size_t off = 0; - off = strchr(iter+1, delim); - while (iter[off - 1] == '\\' && off < cmdlinelen) - off = strchr(iter + off, delim); - if (off == cmdlinelen - (iter-OBOS_KernelCmdLine)) - arg_len = off; - else - arg_len = off - 1; - iter++; - } - else - { - arg_len = strchr(iter, ' '); - if (arg_len != cmdlinelen-(iter-OBOS_KernelCmdLine)) - arg_len--; - } - OBOS_argv[OBOS_argc - 1] = memcpy(cmd_calloc(arg_len + 1, sizeof(char)), iter, arg_len); - iter += arg_len+1; - } - for (size_t i = 0; i < OBOS_argc; i++) - { - // *-(opt) - const char* arg = OBOS_argv[i]; - while (*arg == '-') - arg++; - size_t arglen = strlen(arg); - if (!arglen) - continue; - size_t optlen = strchr(arg, '='); - if (arglen != optlen || arg[arglen - 1] == '=') - continue; - if (strncmp("init-args", arg, optlen)) - { - if ((i + 1) == OBOS_argc) - break; - OBOS_InitArgumentsStart = i+1; - OBOS_InitArgumentsCount = OBOS_argc - OBOS_InitArgumentsStart; - OBOS_argc = i; - break; - } - } - if (OBOS_GetOPTF("help")) - printf("%s", help_message); -} - -#define user_alloc(sz) \ -(OBOS_KernelAllocator ? \ - ZeroAllocate(OBOS_KernelAllocator, sz, 1, nullptr) : \ - OBOS_BasicMMAllocatePages(sz, nullptr)\ -) - -char* OBOS_GetOPTS(const char* opt) -{ - for (size_t i = 0; i < OBOS_argc; i++) - { - // *-(opt)=[value] - const char* arg = OBOS_argv[i]; - while (*arg == '-') - arg++; - size_t arglen = strlen(arg); - if (!arglen) - continue; - size_t optlen = strchr(arg, '='); - if (arglen != optlen || arg[arglen - 1] == '=') - optlen--; - // Wut? - // if (arglen == optlen && arg[arglen - 1] != '=') - // continue; - if (strncmp(arg, opt, optlen)) - { - if (i == (OBOS_argc - 1) && optlen == arglen) - return nullptr; - if (optlen == arglen) - { - size_t valuelen = strlen(OBOS_argv[i+1]); - return memcpy(user_alloc(valuelen+1), OBOS_argv[i+1], valuelen); - } - size_t valuelen = -1+arglen-optlen; - arg += optlen+1; - return memcpy(user_alloc(valuelen+1), arg, valuelen); - } - } - return nullptr; -} -static uint64_t dec2bin(const char* str, size_t size) -{ - uint64_t ret = 0; - for (size_t i = 0; i < size; i++) - { - if ((str[i] - '0') < 0 || (str[i] - '0') > 9) - continue; - ret *= 10; - ret += str[i] - '0'; - } - return ret; -} -static uint64_t hex2bin(const char* str, size_t size) -{ - uint64_t ret = 0; - str += *str == '\n'; - for (int i = size - 1, j = 0; i > -1; i--, j++) - { - char c = str[i]; - uintptr_t digit = 0; - switch (c) - { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - digit = c - '0'; - break; - case 'A': - case 'B': - case 'C': - case 'D': - case 'E': - case 'F': - digit = (c - 'A') + 10; - break; - case 'a': - case 'b': - case 'c': - case 'd': - case 'e': - case 'f': - digit = (c - 'a') + 10; - break; - default: - break; - } - ret |= digit << (j * 4); - } - return ret; -} -static uint64_t oct2bin(const char* str, size_t size) -{ - uint64_t n = 0; - const char* c = str; - while (size-- > 0) - n = n * 8 + (uint64_t)(*c++ - '0'); - return n; -} -static bool isNumber(char ch) -{ - char temp = ch - '0'; - return temp >= 0 && temp < 10; -} -static uint64_t strtoull(const char* str, const char** endptr, int base) -{ - while (!isNumber(*str) && *str) - str++; - if (!(*str)) - { - if (endptr) - *endptr = nullptr; - return 0; - } - if (!base) - { - base = 10; - if (*(str+1) == 'x' || *(str+1) == 'X') - { - base = 16; - str += 2; - } - else if (*str == '0') - { - base = 8; - str++; - } - } - size_t sz = 0; - while (isNumber(*(str + sz))) - sz++; - if (endptr) - *endptr = (str + sz); - switch (base) - { - case 10: - return dec2bin(str, sz); - case 16: - return hex2bin(str, sz); - case 8: - return oct2bin(str, sz); - default: - break; - } - return 0xffffffffffffffff; -} -__attribute__((alias("strtoull"))) uint64_t OBOSH_StrToULL(const char* str, const char** endptr, int base); - -uint64_t OBOS_GetOPTD(const char* opt) -{ - return OBOS_GetOPTD_Ex(opt, UINT64_MAX); -} -uint64_t OBOS_GetOPTD_Ex(const char* opt, uint64_t default_value) -{ - char* val = OBOS_GetOPTS(opt); - if (!val) - return default_value; - uint64_t dec = strtoull(val, nullptr, 0); - Free(OBOS_KernelAllocator, val, strlen(val)+1); - return dec; -} - -bool OBOS_GetOPTF(const char* opt) -{ - for (size_t i = 0; i < OBOS_argc; i++) - { - // *-(opt) - const char* arg = OBOS_argv[i]; - while (*arg == '-') - arg++; - size_t arglen = strlen(arg); - if (!arglen) - continue; - size_t optlen = strchr(arg, '='); - if (arglen != optlen || arg[arglen - 1] == '=') - continue; - if (strncmp(opt, arg, optlen)) - return true; - } - return false; -} diff --git a/src/oboskrnl/cmdline.h b/src/oboskrnl/cmdline.h deleted file mode 100644 index 8b007f71..00000000 --- a/src/oboskrnl/cmdline.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * oboskrnl/cmdline.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include - -extern OBOS_EXPORT const char* OBOS_KernelCmdLine; -extern OBOS_EXPORT const char* volatile OBOS_InitrdBinary; -extern OBOS_EXPORT size_t volatile OBOS_InitrdSize; -extern OBOS_EXPORT char** OBOS_argv; -extern OBOS_EXPORT size_t OBOS_argc; -extern size_t OBOS_InitArgumentsStart; // if SIZE_MAX, assume it doesn't exist -extern size_t OBOS_InitArgumentsCount; - -uint64_t OBOSH_StrToULL(const char* str, const char** endptr, int base); - -// Parses the command line into OBOS_argv and OBOS_argc -void OBOS_ParseCMDLine(); -// Gets the value of a string command line option. -OBOS_EXPORT char* OBOS_GetOPTS(const char* opt); -// Gets the value of an integer command line option. -OBOS_EXPORT uint64_t OBOS_GetOPTD(const char* opt); -OBOS_EXPORT uint64_t OBOS_GetOPTD_Ex(const char* opt, uint64_t default_value); -// Gets the value of a flag command line option. -// true if the flag was found on the command line, otherwise false. -OBOS_EXPORT bool OBOS_GetOPTF(const char* opt); - -OBOS_EXPORT uint64_t OBOSH_StrToULL(const char* str, const char** endptr, int base); \ No newline at end of file diff --git a/src/oboskrnl/console.cpp b/src/oboskrnl/console.cpp new file mode 100644 index 00000000..af86dfe9 --- /dev/null +++ b/src/oboskrnl/console.cpp @@ -0,0 +1,226 @@ +/* + oboskrnl/console.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include +#include +#include +#include + +namespace obos +{ + static void PlotPixel(Pixel colour, uint8_t* fb, FramebufferFormat fbFmt); + bool Console::Initialize(const Framebuffer* fb, const Framebuffer* bb, bool drawToFB) + { + SetFramebuffer(fb, bb, drawToFB); + return true; + } + + void Console::ConsoleOutput(const char* string) + { + m_lock.Lock(); + for (size_t i = 0; string[i]; __ImplOutputCharacter(string[i++])); + m_lock.Unlock(); + } + void Console::ConsoleOutput(const char* string, size_t size) + { + m_lock.Lock(); + for (size_t i = 0; i < size; __ImplOutputCharacter(string[i++])); + m_lock.Unlock(); + } + void Console::ConsoleOutput(char ch) + { + m_lock.Lock(); + __ImplOutputCharacter(ch); + m_lock.Unlock(); + } + + static OBOS_NO_KASAN void fillBackgroundTransparent(Framebuffer& framebuffer, Pixel backgroundColour, Pixel initialBackgroundColour) + { + for (uint32_t i = 0; i < framebuffer.height * framebuffer.width * framebuffer.bpp / 8; i += (framebuffer.bpp / 8)) + { + Pixel pixel = 0; + switch (framebuffer.format) + { + case FramebufferFormat::FB_FORMAT_RGB888: + pixel.ele.r = ((uint8_t*)framebuffer.address)[i + 0]; + pixel.ele.g = ((uint8_t*)framebuffer.address)[i + 1]; + pixel.ele.b = ((uint8_t*)framebuffer.address)[i + 2]; + break; + case FramebufferFormat::FB_FORMAT_BGR888: + pixel.ele.b = ((uint8_t*)framebuffer.address)[i + 0]; + pixel.ele.g = ((uint8_t*)framebuffer.address)[i + 1]; + pixel.ele.r = ((uint8_t*)framebuffer.address)[i + 2]; + break; + case FramebufferFormat::FB_FORMAT_RGBX8888: + pixel.raw.rgbx = *(uint32_t*)((uint8_t*)framebuffer.address + i); + break; + case FramebufferFormat::FB_FORMAT_XRGB8888: + pixel.ele.r = ((uint8_t*)framebuffer.address)[i + 1]; + pixel.ele.g = ((uint8_t*)framebuffer.address)[i + 2]; + pixel.ele.b = ((uint8_t*)framebuffer.address)[i + 3]; + break; + default: + break; + } + PlotPixel(pixel == initialBackgroundColour ? backgroundColour : pixel, ((uint8_t*)framebuffer.address) + i, framebuffer.format); + } + } + OBOS_NO_KASAN void Console::SetColour(Pixel fg, Pixel bg, bool fillBg) + { + if (m_backgroundColour != bg && fillBg) + fillBackgroundTransparent(*m_drawBuffer, bg, m_backgroundColour); + m_foregroundColour = fg; + m_backgroundColour = bg; + } + void Console::GetColour(Pixel& fg, Pixel& bg) const + { + fg = m_foregroundColour; + bg = m_backgroundColour; + } + void Console::SetPosition(uint32_t x, uint32_t y) + { + x = x % m_maxX; + y = y % m_maxY; + m_x = x; + m_y = y; + } + void Console::GetPosition(uint32_t* x, uint32_t* y) const + { + if (x) + *x = m_x; + if (y) + *y = m_y; + } + void Console::SetFramebuffer(const Framebuffer* fb, const Framebuffer* bb, bool drawToFB) + { + if (fb) + m_framebuffer = *fb; + if (bb) + m_backbuffer = *bb; + if (drawToFB && !fb) + drawToFB = false; + if (!drawToFB && !bb) + drawToFB = true; + if (drawToFB) + m_drawBuffer = &m_framebuffer; + else + m_drawBuffer = &m_backbuffer; + if (m_drawBuffer) + { + m_maxX = m_drawBuffer->width / 8; + m_maxY = m_drawBuffer->height / 16; + } + } + void Console::GetFramebuffer(Framebuffer* fb, Framebuffer* bb, bool* drawToFB) + { + if (fb) + *fb = m_framebuffer; + if (bb) + *bb = m_backbuffer; + if (drawToFB) + *drawToFB = (m_drawBuffer == &m_framebuffer); + } + + OBOS_NO_KASAN void Console::ClearConsole(Pixel bg) + { + if (!m_drawBuffer) + return; + for (uint32_t i = 0; i < m_drawBuffer->height * m_drawBuffer->pitch; i += (m_drawBuffer->bpp / 8)) + PlotPixel(bg, ((uint8_t*)m_drawBuffer->address) + i, m_drawBuffer->format); + } + + void Console::__ImplOutputCharacter(char ch) + { + switch (ch) + { + case '\n': + newlineHandler(); + break; + case '\r': + m_x = 0; + break; + case '\t': + m_x += 4 - (m_x % 4); + break; + case '\b': + if (!m_x) + break; + __ImplPutChar(' ', --m_x, m_y, m_foregroundColour, m_backgroundColour); + break; + default: + if (m_x >= m_maxX) + newlineHandler(); + __ImplPutChar(ch, m_x++, m_y, m_foregroundColour, m_backgroundColour); + break; + } + } + OBOS_NO_KASAN void Console::newlineHandler() + { + m_x = 0; + if (++m_y == m_maxY) + { + memset(m_drawBuffer->address, 0, (size_t)m_drawBuffer->pitch / 4 * 16); + memcpy(m_drawBuffer->address, (uint8_t*)m_drawBuffer->address + (m_drawBuffer->pitch * 16), (size_t)m_drawBuffer->pitch * ((size_t)m_drawBuffer->height - 16)); + memset((uint8_t*)m_drawBuffer->address + (size_t)m_drawBuffer->pitch * ((size_t)m_drawBuffer->height - 16), 0, (size_t)m_drawBuffer->pitch * 16); + m_y--; + } + } + OBOS_NO_KASAN static void PlotPixel(Pixel colour, uint8_t* fb, FramebufferFormat fbFmt) + { + switch (fbFmt) + { + case obos::FramebufferFormat::FB_FORMAT_RGB888: + *fb++ = colour.ele.r; + *fb++ = colour.ele.g; + *fb++ = colour.ele.b; + break; + case obos::FramebufferFormat::FB_FORMAT_BGR888: + *fb++ = colour.ele.b; + *fb++ = colour.ele.g; + *fb++ = colour.ele.r; + break; + case obos::FramebufferFormat::FB_FORMAT_RGBX8888: + *(uint32_t*)fb = colour.raw.rgbx; + break; + case obos::FramebufferFormat::FB_FORMAT_XRGB8888: + *(uint32_t*)fb = colour.ToFormat(FramebufferFormat::FB_FORMAT_XRGB8888); + break; + default: + break; + } + } + OBOS_NO_KASAN void Console::__ImplPutChar(char ch, uint32_t x, uint32_t y, Pixel fc, Pixel bc) + { + if (!m_drawBuffer) + return; + int cy; + int mask[8] = { 128,64,32,16,8,4,2,1 }; + const uint8_t* glyph = (uint8_t*)g_consoleFont + (int)ch * 16; + y = y * 16 + 12; + x <<= 3; + if (x > m_drawBuffer->width) + x = 0; + if (y >= m_drawBuffer->height) + y = m_drawBuffer->height - 16; + const uint32_t bytesPerPixel = m_drawBuffer->bpp / 8; + for (cy = 0; cy < 16; cy++) + { + uint32_t realY = y + cy - 12; + uint8_t* framebuffer = (uint8_t*)m_drawBuffer->address + realY * m_drawBuffer->pitch; + PlotPixel((glyph[cy] & mask[0]) ? fc : bc, framebuffer + ((x + 0) * bytesPerPixel), m_drawBuffer->format); + PlotPixel((glyph[cy] & mask[1]) ? fc : bc, framebuffer + ((x + 1) * bytesPerPixel), m_drawBuffer->format); + PlotPixel((glyph[cy] & mask[2]) ? fc : bc, framebuffer + ((x + 2) * bytesPerPixel), m_drawBuffer->format); + PlotPixel((glyph[cy] & mask[3]) ? fc : bc, framebuffer + ((x + 3) * bytesPerPixel), m_drawBuffer->format); + PlotPixel((glyph[cy] & mask[4]) ? fc : bc, framebuffer + ((x + 4) * bytesPerPixel), m_drawBuffer->format); + PlotPixel((glyph[cy] & mask[5]) ? fc : bc, framebuffer + ((x + 5) * bytesPerPixel), m_drawBuffer->format); + PlotPixel((glyph[cy] & mask[6]) ? fc : bc, framebuffer + ((x + 6) * bytesPerPixel), m_drawBuffer->format); + PlotPixel((glyph[cy] & mask[7]) ? fc : bc, framebuffer + ((x + 7) * bytesPerPixel), m_drawBuffer->format); + } + } + + void* g_consoleFont; + Console g_kernelConsole; +} \ No newline at end of file diff --git a/src/oboskrnl/console.h b/src/oboskrnl/console.h new file mode 100644 index 00000000..49333d30 --- /dev/null +++ b/src/oboskrnl/console.h @@ -0,0 +1,83 @@ +/* + oboskrnl/console.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include +#include +#include + +#include + +namespace obos +{ + class Console + { + public: + Console() = default; + + /// + /// Initializes the console. + /// If both buffers are nullptr, the draw buffer is set to nullptr and all writes to the console are dropped. + /// + /// The framebuffer to use. + /// The backbuffer to use. + /// Whether to set the draw buffer to the framebuffer (true) or the backbuffer (false). If the framebuffer is nullptr and this is set, + /// the parameter is treated as if it were false. If the backbuffer is nullptr and this is false, the parameter is treated as if it were true. + /// + OBOS_EXPORT bool Initialize(const Framebuffer *fb, const Framebuffer *bb, bool drawToFB); + + /// + /// Prints a string. + /// + /// The string to print. + OBOS_EXPORT void ConsoleOutput(const char* string); + /// + /// Prints a string. + /// + /// The string to print. + /// The amount of characters to print. + OBOS_EXPORT void ConsoleOutput(const char* string, size_t size); + /// + /// Prints a single character. + /// + /// The character to print. + OBOS_EXPORT void ConsoleOutput(char ch); + + OBOS_EXPORT void SetColour(Pixel fg, Pixel bg, bool fillBg = false); + OBOS_EXPORT void GetColour(Pixel &fg, Pixel &bg) const; + OBOS_EXPORT void SetPosition(uint32_t x, uint32_t y); + OBOS_EXPORT void GetPosition(uint32_t* x, uint32_t* y) const; + /// + /// Sets the console's frame buffer. + /// If both buffers are nullptr, the draw buffer is set to nullptr and all writes to the console are dropped. + /// + /// The framebuffer to use. + /// The back buffer to use. + /// Whether to set the draw buffer to the framebuffer (true) or the backbuffer (false). If the framebuffer is nullptr and this is set, + /// the parameter is treated as if it were false. If the backbuffer is nullptr and this is false, the parameter is treated as if it were true. + OBOS_EXPORT void SetFramebuffer(const Framebuffer* fb, const Framebuffer* bb, bool drawToFB); + OBOS_EXPORT void GetFramebuffer(Framebuffer* fb, Framebuffer* bb, bool* drawToFB); + + OBOS_EXPORT void ClearConsole(Pixel bg); + private: + void __ImplPutChar(char ch, uint32_t x, uint32_t y, Pixel fc, Pixel bc); + void __ImplOutputCharacter(char ch); + void newlineHandler(); + Framebuffer* m_drawBuffer; + Framebuffer m_framebuffer; + Framebuffer m_backbuffer; + uint32_t m_x, m_y; + uint32_t m_maxX = 0, m_maxY = 0; + Pixel m_foregroundColour{ 0xCC, 0xCC, 0xCC }; + Pixel m_backgroundColour{ 0 }; + locks::SpinLock m_lock; + }; + + // Must be an 8x16 font. + extern void* g_consoleFont; + extern Console g_kernelConsole; +} \ No newline at end of file diff --git a/src/oboskrnl/contrib/csprng.c b/src/oboskrnl/contrib/csprng.c deleted file mode 100644 index 08db1749..00000000 --- a/src/oboskrnl/contrib/csprng.c +++ /dev/null @@ -1,280 +0,0 @@ -/* - * oboskrnl/contrib/csprng.c - * - * Copyright (c) 2026 MarcasRealAccount - */ - -#include - -#include - -/* Helper functions: */ - -static inline uint32_t rotr32(uint32_t v, uint8_t s) -{ - return (v >> s) | (v << (32 - s)); -} - -static void ctr_drbg_instantiate(csprng* ctx); -static void ctr_drbg_reseed(csprng* ctx); -static void ctr_drbg_generate(csprng* ctx, void* output, size_t size); -static void ctr_drbg_update(const void* provided_data, void* key, void* v); - -static void aes128_key_expand(const void* key, void* keys); -static void aes_cipher(const void* keys, const void* in, void* out); - -size_t csprng_tjec_read_entropy(void* userdata, void* data, size_t size) -{ - int64_t res = tjec_read_entropy_safe((tjec*) userdata, data, size); - return res > 0 ? (size_t) res : 0; -} - -uint32_t csprng_init(csprng* ctx, const csprng_callbacks* callbacks, uint64_t flags) -{ - if (!ctx || !callbacks || !callbacks->read_entropy) - return CSPRNG_EINVAL; - - memset(ctx, 0, sizeof(csprng)); - - ctx->mtx = MUTEX_INITIALIZE(); - memcpy(&ctx->callbacks, callbacks, sizeof(csprng_callbacks)); - ctx->flags = flags; - - ctr_drbg_instantiate(ctx); - - return CSPRNG_ENOERR; -} - -void csprng_destroy(csprng* ctx) -{ - if (!ctx) - return; - - memset(ctx, 0, sizeof(csprng)); -} - -int64_t csprng_read_random(csprng* ctx, void* data, size_t size) -{ - if (!ctx || !data) - return 0; - - size_t orig_size = size; - uint8_t* ptr = (uint8_t*) data; - - while (size > 0) - { - size_t to_generate = size < 4096 ? size : 4096; - Core_MutexAcquire(&ctx->mtx); - ctr_drbg_generate(ctx, ptr, to_generate); - Core_MutexRelease(&ctx->mtx); - ptr += to_generate; - size -= to_generate; - } - return (int64_t) orig_size; -} - -// CTR DRBG - -void ctr_drbg_instantiate(csprng* ctx) -{ - memset(ctx->v, 0, sizeof(ctx->v)); - memset(ctx->key, 0, sizeof(ctx->key)); - ctr_drbg_reseed(ctx); -} - -void ctr_drbg_reseed(csprng* ctx) -{ - uint8_t seed_material[32]; - size_t read = ctx->callbacks.read_entropy(ctx->callbacks.userdata, seed_material, sizeof(seed_material)); - (void) read; - - ctr_drbg_update(seed_material, ctx->key, ctx->v); - ctx->reseed_counter = 1; - - memset(seed_material, 0, sizeof(seed_material)); -} - -void ctr_drbg_generate(csprng* ctx, void* output, size_t size) -{ - const uint64_t reseed_interval = 16; - - uint8_t temp[32]; - - if (ctx->reseed_counter > reseed_interval) - ctr_drbg_reseed(ctx); - - uint8_t keys[16*11]; - aes128_key_expand(ctx->key, keys); - uint8_t* p_out = (uint8_t*) output; - - while (size > 0) - { - if (*(uint64_t*) ctx->v == ~((uint64_t)0)) - { - ((uint64_t*) ctx->v)[0] = 0; - ++((uint64_t*) ctx->v)[1]; - } - else - { - ++((uint64_t*) ctx->v)[0]; - } - aes_cipher(keys, ctx->v, temp); - size_t to_copy = size < 16 ? size : 16; - memcpy(p_out, temp, to_copy); - p_out += to_copy; - size -= to_copy; - } - memset(keys, 0, sizeof(keys)); - memset(temp, 0, sizeof(temp)); - ctr_drbg_update(temp, ctx->key, ctx->v); - ++ctx->reseed_counter; -} - -void ctr_drbg_update(const void* provided_data, void* key, void* v) -{ - uint8_t keys[16*11]; - aes128_key_expand(key, keys); - aes_cipher(keys, v, key); - if (*(uint64_t*) v == ~((uint64_t)0)) - { - ((uint64_t*) v)[0] = 0; - ++((uint64_t*) v)[1]; - } - else - { - ++((uint64_t*) v)[0]; - } - aes_cipher(keys, v, v); - ((uint64_t*) key)[0] ^= ((uint64_t*) provided_data)[0]; - ((uint64_t*) key)[1] ^= ((uint64_t*) provided_data)[1]; - ((uint64_t*) v)[0] ^= ((uint64_t*) provided_data)[2]; - ((uint64_t*) v)[1] ^= ((uint64_t*) provided_data)[3]; - memset(keys, 0, sizeof(keys)); -} - -// AES-128 - -static const _Alignas(64) uint8_t SBOX_MAT[16][16] = { - { 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76 }, - { 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0 }, - { 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15 }, - { 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75 }, - { 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84 }, - { 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF }, - { 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8 }, - { 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2 }, - { 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73 }, - { 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB }, - { 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79 }, - { 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08 }, - { 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A }, - { 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E }, - { 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF }, - { 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16 } -}; -static const uint8_t* SBOX = (const uint8_t*) SBOX_MAT; - -typedef union aes_block { - uint8_t bytes[16]; - uint32_t words[4]; - uint64_t quads[2]; -} aes_block; - -void aes128_key_expand(const void* key, void* keys) -{ - const uint8_t Rcon[10] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36 }; - uint8_t* p_bytes = (uint8_t*) keys; - memcpy(p_bytes, key, 16); - for (uint8_t i = 0; i < 10; ++i) - { - p_bytes[16 + i * 16] = p_bytes[0 + i * 16] ^ SBOX[p_bytes[13 + i * 16]] ^ Rcon[i]; - p_bytes[17 + i * 16] = p_bytes[1 + i * 16] ^ SBOX[p_bytes[14 + i * 16]]; - p_bytes[18 + i * 16] = p_bytes[2 + i * 16] ^ SBOX[p_bytes[15 + i * 16]]; - p_bytes[19 + i * 16] = p_bytes[3 + i * 16] ^ SBOX[p_bytes[12 + i * 16]]; - - p_bytes[20 + i * 16] = p_bytes[4 + i * 16] ^ p_bytes[16 + i * 16]; - p_bytes[21 + i * 16] = p_bytes[5 + i * 16] ^ p_bytes[17 + i * 16]; - p_bytes[22 + i * 16] = p_bytes[6 + i * 16] ^ p_bytes[18 + i * 16]; - p_bytes[23 + i * 16] = p_bytes[7 + i * 16] ^ p_bytes[19 + i * 16]; - - p_bytes[24 + i * 16] = p_bytes[8 + i * 16] ^ p_bytes[20 + i * 16]; - p_bytes[25 + i * 16] = p_bytes[9 + i * 16] ^ p_bytes[21 + i * 16]; - p_bytes[26 + i * 16] = p_bytes[10 + i * 16] ^ p_bytes[22 + i * 16]; - p_bytes[27 + i * 16] = p_bytes[11 + i * 16] ^ p_bytes[23 + i * 16]; - - p_bytes[28 + i * 16] = p_bytes[12 + i * 16] ^ p_bytes[24 + i * 16]; - p_bytes[29 + i * 16] = p_bytes[13 + i * 16] ^ p_bytes[25 + i * 16]; - p_bytes[30 + i * 16] = p_bytes[14 + i * 16] ^ p_bytes[26 + i * 16]; - p_bytes[31 + i * 16] = p_bytes[15 + i * 16] ^ p_bytes[27 + i * 16]; - } -} - -void aes_cipher(const void* keys, const void* in, void* out) -{ - const aes_block* key_blocks = (const aes_block*) keys; - aes_block* block_out = (aes_block*) out; - memcpy(block_out, in, 16); - - block_out->quads[0] ^= key_blocks[0].quads[0]; - block_out->quads[1] ^= key_blocks[0].quads[1]; - for (size_t i = 1; i < 10; ++i) - { - for (size_t j = 0; j < 16; ++j) - block_out->bytes[j] = SBOX[block_out->bytes[j]]; - - uint8_t tmp = block_out->bytes[1]; - block_out->bytes[1] = block_out->bytes[5]; - block_out->bytes[5] = block_out->bytes[9]; - block_out->bytes[9] = block_out->bytes[13]; - block_out->bytes[13] = tmp; - tmp = block_out->bytes[2]; - block_out->bytes[2] = block_out->bytes[10]; - block_out->bytes[10] = tmp; - tmp = block_out->bytes[6]; - block_out->bytes[6] = block_out->bytes[14]; - block_out->bytes[14] = tmp; - tmp = block_out->bytes[15]; - block_out->bytes[15] = block_out->bytes[11]; - block_out->bytes[11] = block_out->bytes[7]; - block_out->bytes[7] = block_out->bytes[3]; - block_out->bytes[3] = tmp; - - for (uint8_t c = 0; c < 4; ++c) - { - uint32_t column = block_out->words[c]; - uint32_t column2 = ((column & 0x7F7F7F7F) << 1) ^ (((column >> 7) & 0x01010101) * 0x1B); - uint32_t column3 = rotr32(column, 8); - uint32_t column4 = rotr32(column2, 8); - uint32_t column5 = rotr32(column, 16); - uint32_t column6 = rotr32(column, 24); - - block_out->words[c] = column2 ^ column3 ^ column4 ^ column5 ^ column6; - } - - block_out->quads[0] ^= key_blocks[i].quads[0]; - block_out->quads[1] ^= key_blocks[i].quads[1]; - } - - for (size_t j = 0; j < 16; ++j) - block_out->bytes[j] = SBOX[block_out->bytes[j]]; - - uint8_t tmp = block_out->bytes[1]; - block_out->bytes[1] = block_out->bytes[5]; - block_out->bytes[5] = block_out->bytes[9]; - block_out->bytes[9] = block_out->bytes[13]; - block_out->bytes[13] = tmp; - tmp = block_out->bytes[2]; - block_out->bytes[2] = block_out->bytes[10]; - block_out->bytes[10] = tmp; - tmp = block_out->bytes[6]; - block_out->bytes[6] = block_out->bytes[14]; - block_out->bytes[14] = tmp; - tmp = block_out->bytes[15]; - block_out->bytes[15] = block_out->bytes[11]; - block_out->bytes[11] = block_out->bytes[7]; - block_out->bytes[7] = block_out->bytes[3]; - block_out->bytes[3] = tmp; - - block_out->quads[0] ^= key_blocks[10].quads[0]; - block_out->quads[1] ^= key_blocks[10].quads[1]; -} \ No newline at end of file diff --git a/src/oboskrnl/contrib/random.h b/src/oboskrnl/contrib/random.h deleted file mode 100644 index 86e5f385..00000000 --- a/src/oboskrnl/contrib/random.h +++ /dev/null @@ -1,174 +0,0 @@ -/* - * oboskrnl/contrib/random.h - * - * Copyright (c) 2026 MarcasRealAccount - */ - -#pragma once - -#include -#include - -#include - -typedef struct tjec_sha3 -{ - uint16_t r; - uint16_t rword; - uint16_t digest_size; - uint8_t partial[136]; - uint64_t state[25]; - uint64_t msg_len; -} tjec_sha3; - -typedef struct tjec_memory -{ - atomic_uchar* memory; - size_t cell_size; - size_t size; - uint64_t flags; -} tjec_memory; - -typedef struct tjec -{ - mutex mtx; - - uint32_t health_failure; - uint16_t base_acc_count; - uint8_t osr; - uint8_t apt_base_set : 1; - uint8_t fips_enabled : 1; - uint8_t random_access : 1; - uint8_t lag_predictor : 1; - uint64_t flags; - - uint64_t prev_time; - uint64_t prev_delta; - uint64_t prev_delta2; - - uint64_t common_time_gcd; - - const tjec_memory* memory; - size_t location; - - uint64_t apt_base; - uint32_t apt_count; - uint32_t apt_cutoff; - uint32_t apt_cutoff_permanent; - uint32_t apt_observations; - - uint32_t rct_count; - - uint32_t lag_global_cutoff; - uint32_t lag_local_cutoff; - uint32_t lag_prediction_success_count; - uint32_t lag_prediction_success_run; - uint32_t lag_best_predictor; - uint32_t lag_observations; - uint64_t lag_scoreboard[8]; - uint64_t lag_delta_history[8]; - - tjec_sha3 sha3; -} tjec; - -typedef struct csprng_callbacks -{ - void* userdata; - size_t(* read_entropy)(void* userdata, void* data, size_t size); -} csprng_callbacks; - -typedef struct csprng -{ - mutex mtx; - - csprng_callbacks callbacks; - - uint64_t flags; - - uint64_t reseed_counter; - uint8_t v[16]; - uint8_t key[16]; -} csprng; - -static const uint64_t TJEC_MEM_32KIB = 1; -static const uint64_t TJEC_MEM_64KIB = 2; -static const uint64_t TJEC_MEM_128KIB = 3; -static const uint64_t TJEC_MEM_256KIB = 4; -static const uint64_t TJEC_MEM_512KIB = 5; -static const uint64_t TJEC_MEM_1MIB = 6; -static const uint64_t TJEC_MEM_2MIB = 7; -static const uint64_t TJEC_MEM_4MIB = 8; -static const uint64_t TJEC_MEM_8MIB = 9; -static const uint64_t TJEC_MEM_16MIB = 10; -static const uint64_t TJEC_MEM_32MIB = 11; -static const uint64_t TJEC_MEM_64MIB = 12; -static const uint64_t TJEC_MEM_128MIB = 13; -static const uint64_t TJEC_MEM_256MIB = 14; -static const uint64_t TJEC_MEM_512MIB = 15; -static const uint64_t TJEC_MEM_RANDOM_ACCESS = 16; - -static const uint64_t TJEC_USE_FIPS = 1; -static const uint64_t TJEC_USE_LAG_PREDICTOR = 2; -static const uint64_t TJEC_MAX_ACC_LOOP_BITS_1 = UINT64_C(0) << 8; -static const uint64_t TJEC_MAX_ACC_LOOP_BITS_2 = UINT64_C(1) << 8; -static const uint64_t TJEC_MAX_ACC_LOOP_BITS_3 = UINT64_C(2) << 8; -static const uint64_t TJEC_MAX_ACC_LOOP_BITS_4 = UINT64_C(3) << 8; -static const uint64_t TJEC_MAX_ACC_LOOP_BITS_5 = UINT64_C(4) << 8; -static const uint64_t TJEC_MAX_ACC_LOOP_BITS_6 = UINT64_C(5) << 8; -static const uint64_t TJEC_MAX_ACC_LOOP_BITS_7 = UINT64_C(6) << 8; -static const uint64_t TJEC_MAX_ACC_LOOP_BITS_8 = UINT64_C(7) << 8; -static const uint64_t TJEC_MAX_HASH_LOOP_BITS_1 = UINT64_C(0) << 11; -static const uint64_t TJEC_MAX_HASH_LOOP_BITS_2 = UINT64_C(1) << 11; -static const uint64_t TJEC_MAX_HASH_LOOP_BITS_3 = UINT64_C(2) << 11; -static const uint64_t TJEC_MAX_HASH_LOOP_BITS_4 = UINT64_C(3) << 11; -static const uint64_t TJEC_MAX_HASH_LOOP_BITS_5 = UINT64_C(4) << 11; -static const uint64_t TJEC_MAX_HASH_LOOP_BITS_6 = UINT64_C(5) << 11; -static const uint64_t TJEC_MAX_HASH_LOOP_BITS_7 = UINT64_C(6) << 11; -static const uint64_t TJEC_MAX_HASH_LOOP_BITS_8 = UINT64_C(7) << 11; - -#define TJEC_ENOERR 0 -#define TJEC_EINVAL 1 -#define TJEC_ENOMEM 2 -#define TJEC_ENOTIME 3 -#define TJEC_ECOARSETIME 4 -#define TJEC_ENOMONOTONIC 5 -#define TJEC_ERCT 6 -#define TJEC_EHEALTH 7 -#define TJEC_ESTUCK 8 -#define TJEC_EMINVARVAR 9 - -#define TJEC_OSR_FAILURE -1 -#define TJEC_RCT_FAILURE -2 -#define TJEC_APT_FAILURE -3 -#define TJEC_LAG_FAILURE -4 -#define TJEC_UNKNOWN_FAILURE -5 -#define TJEC_RCT_FAILURE_PERMANENT -6 -#define TJEC_APT_FAILURE_PERMANENT -7 -#define TJEC_LAG_FAILURE_PERMANENT -8 - -uint32_t tjec_memory_init(tjec_memory* mem, uint64_t flags); -void tjec_memory_destroy(tjec_memory* mem); -size_t tjec_memory_get_size(const tjec_memory* mem); - -uint32_t tjec_pre_init(tjec* ec, const tjec_memory* mem, uint64_t flags); -uint32_t tjec_pre_init_ex(tjec* ec, const tjec_memory* mem, uint64_t flags, uint8_t osr); -uint32_t tjec_init(tjec* ec, const tjec_memory* mem, uint64_t flags); -uint32_t tjec_init_ex(tjec* ec, const tjec_memory* mem, uint64_t flags, uint8_t osr); -void tjec_destroy(tjec* ec); -int64_t tjec_read_entropy(tjec* ec, void* data, size_t size); -int64_t tjec_read_entropy_safe(tjec* ec, void* data, size_t size); - -#define CSPRNG_ENOERR 0 -#define CSPRNG_EINVAL 1 - -size_t csprng_tjec_read_entropy(void* userdata, void* data, size_t size); - -uint32_t csprng_init(csprng* ctx, const csprng_callbacks* callbacks, uint64_t flags); -void csprng_destroy(csprng* ctx); -int64_t csprng_read_random(csprng* ctx, void* data, size_t size); - -uint8_t random8(void); -uint16_t random16(void); -uint32_t random32(void); -uint64_t random64(void); -bool random_buffer(void* buffer, size_t size); \ No newline at end of file diff --git a/src/oboskrnl/contrib/tjec.c b/src/oboskrnl/contrib/tjec.c deleted file mode 100644 index e915b946..00000000 --- a/src/oboskrnl/contrib/tjec.c +++ /dev/null @@ -1,1081 +0,0 @@ -/* - * oboskrnl/contrib/tjec.c - * - * Copyright (c) 2026 MarcasRealAccount - */ - -#include - -#include - -#include - -#include -#include - -#define ENTROPY_SAFETY_FACTOR 64 -#define APT_MASK (UINT64_C(0xFFFFFFFFFFFFFFFF)) -#define APT_WINDOW_SIZE 512 -#define LAG_HISTORY_SIZE (sizeof(((tjec*) 0)->lag_delta_history) / sizeof(*((tjec*) 0)->lag_delta_history)) -#define LAG_MASK (LAG_HISTORY_SIZE - 1) -#define LAG_WINDOW_SIZE (1 << 10) -#define SHA3_UNROLLED 0 - -#define CELL_SIZE 128 -#define CELL_COUNT 512 - -#define APT_FAILURE_PERMANENT 1 -#define RCT_FAILURE_PERMANENT 2 -#define LAG_FAILURE_PERMANENT 4 -#define APT_FAILURE 8 -#define RCT_FAILURE 16 -#define LAG_FAILURE 32 - -/* Helper functions: */ - -#if __x86_64__ -# include -#endif - -static uint64_t temp_l1_cache_size() -{ -#if __x86_64__ - uint32_t ecx; - uint32_t edx; - __cpuid__(0x80000005, 0, NULL, NULL, &ecx, &edx); - uint64_t cachesize = ((uint64_t)((ecx >> 24) & 7) + ((edx >>24 ) & 7)) << 10; - return cachesize; -#else - return 0; -#endif -} - -static inline uint64_t rotl64(uint64_t v, uint8_t s) -{ - return (v << s) | (v >> (64 - s)); -} - -static inline uint32_t rotl32(uint32_t v, uint8_t s) -{ - return (v << s) | (v >> (32 - s)); -} - -static inline uint64_t gcd64(uint64_t a, uint64_t b) -{ - if (a < b) - { - uint64_t tmp = a; - a = b; - b = tmp; - } - - while (b != 0) - { - uint64_t r = a % b; - a = b; - b = r; - } - return a; -} - -static void sha3_init(tjec_sha3* ctx); -static void sha3_256_init(tjec_sha3* ctx); -static void sha3_update(tjec_sha3* ctx, const void* data, size_t size); -static void sha3_final(tjec_sha3* ctx, void* digest); - -static void apt_init(tjec* ec, uint8_t osr); -static void apt_reinit(tjec* ec, uint64_t delta, uint32_t apt_count, uint32_t apt_observations); -static void apt_reset(tjec* ec); -static void apt_insert(tjec* ec, uint64_t delta); - -static void lag_init(tjec* ec, uint8_t osr); -static void lag_reset(tjec* ec); -static void lag_insert(tjec* ec, uint64_t delta); -static uint64_t lag_delta2(tjec* ec, uint64_t delta); -static uint64_t lag_delta3(tjec* ec, uint64_t delta2); - -static uint32_t rct_permanent_cutoff(uint8_t osr); -static uint32_t rct_intermittent_cutoff(uint8_t osr); -static void rct_insert(tjec* ec, int stuck); - -static int tjec_is_stuck(tjec* ec, uint64_t delta); -static uint32_t tjec_health_failure(tjec* ec); -static uint64_t tjec_loop_shuffle(tjec* ec, uint32_t bits, uint32_t min); -static void tjec_hash_time(tjec* ec, uint64_t delta, int stuck); -static void tjec_random_memory_access(tjec* ec); -static void tjec_memory_access(tjec* ec); -static int tjec_measure_jitter(tjec* ec, uint64_t* current_delta); -static void tjec_random_data(tjec* ec); -static void tjec_read_random_block(tjec* ec, void* dst, size_t size); -static uint32_t tjec_find_common_time_gcd(tjec* ec); -static uint64_t tjec_memory_size(uint64_t flags); - -static uint64_t tjec_common_time_gcd = 1; - -uint32_t tjec_memory_init(tjec_memory* mem, uint64_t flags) -{ - if (!mem) - return TJEC_EINVAL; - - memset(mem, 0, sizeof(tjec_memory)); - - uint64_t memory_size = tjec_memory_size(flags); - - mem->memory = (atomic_uchar*) Mm_QuickVMAllocate(memory_size, false); - mem->cell_size = CELL_SIZE; - mem->size = memory_size; - mem->flags = flags; - if (!mem->memory) - return TJEC_ENOMEM; - - return TJEC_ENOERR; -} - -void tjec_memory_destroy(tjec_memory* mem) -{ - if (!mem) - return; - - Mm_VirtualMemoryFree(&Mm_KernelContext, mem->memory, mem->size); - memset(mem, 0, sizeof(tjec_memory)); -} - -size_t tjec_memory_get_size(const tjec_memory* mem) -{ - return mem ? mem->size : 0; -} - -uint32_t tjec_pre_init(tjec* ec, const tjec_memory* mem, uint64_t flags) -{ - return tjec_pre_init_ex(ec, mem, flags, 1); -} - -uint32_t tjec_pre_init_ex(tjec* ec, const tjec_memory* mem, uint64_t flags, uint8_t osr) -{ - uint32_t err = tjec_init_ex(ec, mem, flags, osr); - if (err) - return err; - - err = tjec_find_common_time_gcd(ec); - if (err) - return err; - - tjec_common_time_gcd = ec->common_time_gcd; - return TJEC_ENOERR; -} - -uint32_t tjec_init(tjec* ec, const tjec_memory* mem, uint64_t flags) -{ - return tjec_init_ex(ec, mem, flags, 1); -} - -uint32_t tjec_init_ex(tjec* ec, const tjec_memory* mem, uint64_t flags, uint8_t osr) -{ - if (!ec || !mem || !osr) - return TJEC_EINVAL; - - memset(ec, 0, sizeof(tjec)); - - ec->mtx = MUTEX_INITIALIZE(); - ec->base_acc_count = 64; - ec->osr = osr; - ec->fips_enabled = !!(flags & TJEC_USE_FIPS); - ec->random_access = !!(mem->flags & TJEC_MEM_RANDOM_ACCESS); - ec->lag_predictor = !!(flags & TJEC_USE_LAG_PREDICTOR); - ec->flags = flags; - ec->common_time_gcd = tjec_common_time_gcd; - ec->memory = mem; - - sha3_256_init(&ec->sha3); - apt_init(ec, osr); - if (ec->lag_predictor) - lag_init(ec, osr); - - tjec_measure_jitter(ec, NULL); - - return TJEC_ENOERR; -} - -void tjec_destroy(tjec* ec) -{ - if (!ec) - return; - - memset(ec, 0, sizeof(tjec)); -} - -int64_t tjec_read_entropy(tjec* ec, void* data, size_t size) -{ - if (!ec || !data) - return 0; - - size_t orig_size = size; - int32_t ret = 0; - - uint8_t* ptr = (uint8_t*) data; - - uint8_t reacquire_counter = 0; - Core_MutexAcquire(&ec->mtx); - - while (size > 0) - { - tjec_random_data(ec); - - uint32_t health_test = tjec_health_failure(ec); - if (health_test) - { - if (health_test & RCT_FAILURE_PERMANENT) - ret = TJEC_RCT_FAILURE_PERMANENT; - else if (health_test & APT_FAILURE_PERMANENT) - ret = TJEC_APT_FAILURE_PERMANENT; - else if (health_test & LAG_FAILURE_PERMANENT) - ret = TJEC_LAG_FAILURE_PERMANENT; - else if (health_test & RCT_FAILURE) - ret = TJEC_RCT_FAILURE; - else if (health_test & APT_FAILURE) - ret = TJEC_APT_FAILURE; - else if (health_test & LAG_FAILURE) - ret = TJEC_LAG_FAILURE; - else - ret = TJEC_UNKNOWN_FAILURE; - break; - } - - size_t to_copy = size >= 32 ? 32 : size; - tjec_read_random_block(ec, ptr, to_copy); - size -= to_copy; - ptr += to_copy; - - if (++reacquire_counter >= 128) - { - reacquire_counter = 0; - Core_MutexRelease(&ec->mtx); - Core_MutexAcquire(&ec->mtx); - } - } - - tjec_read_random_block(ec, NULL, 0); - - Core_MutexRelease(&ec->mtx); - - return ret ? ret : (int64_t) orig_size; -} - -int64_t tjec_read_entropy_safe(tjec* ec, void* data, size_t size) -{ - if (!ec) - return 0; - - size_t orig_size = size; - uint8_t* ptr = (uint8_t*) data; - - while (size > 0) - { - int64_t ret = tjec_read_entropy(ec, ptr, size); - - switch (ret) - { - case TJEC_OSR_FAILURE: - case TJEC_RCT_FAILURE_PERMANENT: - case TJEC_APT_FAILURE_PERMANENT: - case TJEC_LAG_FAILURE_PERMANENT: - return ret; - - case TJEC_UNKNOWN_FAILURE: - case TJEC_RCT_FAILURE: - case TJEC_APT_FAILURE: - case TJEC_LAG_FAILURE: - { - Core_MutexAcquire(&ec->mtx); - uint32_t apt_observations = ec->apt_observations; - uint64_t current_delta = ec->apt_base; - uint32_t lag_prediction_success_run = ec->lag_prediction_success_run; - uint32_t lag_prediction_success_count = ec->lag_prediction_success_count; - - const tjec_memory* mem = ec->memory; - - uint16_t osr = (uint16_t) ec->osr + 1; - uint64_t flags = ec->flags; - - if (osr > 20) - { - Core_MutexRelease(&ec->mtx); - return ret; - } - - while (tjec_init_ex(ec, mem, flags, (uint8_t) osr)) - { - ++osr; - if (osr > 20) - { - Core_MutexRelease(&ec->mtx); - return TJEC_OSR_FAILURE; - } - } - - if (apt_observations) - { - apt_reinit(ec, current_delta, 0, apt_observations); - ec->rct_count = rct_intermittent_cutoff(osr); - ec->lag_prediction_success_run = lag_prediction_success_run; - ec->lag_prediction_success_count = lag_prediction_success_count; - } - Core_MutexRelease(&ec->mtx); - break; - } - default: - size -= (size_t) ret; - ptr += (size_t) ret; - break; - } - } - - return (int64_t) orig_size; -} - -// APT - -void apt_init(tjec* ec, uint8_t osr) -{ - const uint32_t cutoff_lut[] = { 325, 422, 459, 477, 488, 494, 499, 502, 505, 507, 508, 509, 510, 511, 512 }; - const uint32_t cutoff_permanent_lut[] = { 355, 447, 479, 494, 502, 507, 510, 512, 512, 512, 512, 512, 512, 512, 512 }; - - osr = osr < sizeof(cutoff_lut) / sizeof(*cutoff_lut) ? osr : sizeof(cutoff_lut) / sizeof(*cutoff_lut); - - ec->apt_cutoff = cutoff_lut[osr]; - ec->apt_cutoff_permanent = cutoff_permanent_lut[osr]; -} - -void apt_reinit(tjec* ec, uint64_t delta, uint32_t apt_count, uint32_t apt_observations) -{ - ec->apt_base = delta; - ec->apt_base_set = 1; - if (apt_count) - ec->apt_count = apt_count; - else - ec->apt_count = ec->apt_cutoff; - ec->apt_observations = apt_observations; -} - -void apt_reset(tjec* ec) -{ - ec->apt_base_set = 0; -} - -void apt_insert(tjec* ec, uint64_t delta) -{ - delta &= APT_MASK; - - if (!ec->apt_base_set) - { - apt_reinit(ec, delta, 1, 1); - return; - } - - if (delta == ec->apt_base) - { - ++ec->apt_count; - - if (ec->apt_count >= ec->apt_cutoff_permanent) - ec->health_failure |= APT_FAILURE_PERMANENT; - else if (ec->apt_count == ec->apt_cutoff) - ec->health_failure |= APT_FAILURE; - } - - ++ec->apt_observations; - - if (ec->apt_observations >= APT_WINDOW_SIZE) - apt_reset(ec); -} - -// LAG - -void lag_init(tjec* ec, uint8_t osr) -{ - const uint32_t global_cutoff_lut[] = { 66443, 93504, 104761, 110875, 114707, 117330, 119237, 120686, 121823, 122739, 123493, 124124, 124660, 125120, 125520, 125871, 126181, 126457, 126704, 126926 }; - const uint32_t local_cutoff_lut[] = { 38, 75, 111, 146, 181, 215, 250, 284, 318, 351, 385, 419, 452, 485, 518, 551, 584, 617, 650, 683 }; - - osr = osr < sizeof(global_cutoff_lut) / sizeof(*global_cutoff_lut) ? osr : sizeof(global_cutoff_lut) / sizeof(*global_cutoff_lut); - - ec->lag_global_cutoff = global_cutoff_lut[osr - 1]; - ec->lag_local_cutoff = local_cutoff_lut[osr - 1]; -} - -void lag_reset(tjec* ec) -{ - ec->lag_prediction_success_count = 0; - ec->lag_prediction_success_run = 0; - ec->lag_best_predictor = 0; - ec->lag_observations = 0; - - for (uint32_t i = 0; i < LAG_HISTORY_SIZE; ++i) - { - ec->lag_scoreboard[i] = 0; - ec->lag_delta_history[i] = 0; - } -} - -#define LAG_HISTORY(ec, loc) (ec->lag_delta_history[(ec->lag_observations - loc - 1) & LAG_MASK]) - -void lag_insert(tjec* ec, uint64_t delta) -{ - if (ec->lag_observations < LAG_HISTORY_SIZE) - { - ec->lag_delta_history[ec->lag_observations++] = delta; - return; - } - - uint64_t prediction = LAG_HISTORY(ec, ec->lag_best_predictor); - if (prediction == delta) - { - ++ec->lag_prediction_success_count; - ++ec->lag_prediction_success_run; - - if ((ec->lag_prediction_success_run >= ec->lag_local_cutoff) || - (ec->lag_prediction_success_count >= ec->lag_global_cutoff)) - ec->health_failure |= LAG_FAILURE; - } - else - { - ec->lag_prediction_success_run = 0; - } - - for (uint32_t i = 0; i < LAG_HISTORY_SIZE; ++i) - { - if (LAG_HISTORY(ec, i) == delta) - { - ++ec->lag_scoreboard[i]; - - if (ec->lag_scoreboard[i] > ec->lag_scoreboard[ec->lag_best_predictor]) - ec->lag_best_predictor = i; - } - } - - ec->lag_delta_history[(ec->lag_observations++) & LAG_MASK] = delta; - if (ec->lag_observations >= LAG_WINDOW_SIZE) - lag_reset(ec); -} - -uint64_t lag_delta2(tjec* ec, uint64_t delta) -{ - return delta - LAG_HISTORY(ec, 0); -} - -uint64_t lag_delta3(tjec* ec, uint64_t delta2) -{ - return delta2 - (LAG_HISTORY(ec, 0) - LAG_HISTORY(ec, 1)); -} - -// RCT - -uint32_t rct_permanent_cutoff(uint8_t osr) -{ - return osr * 60; -} - -uint32_t rct_intermittent_cutoff(uint8_t osr) -{ - return osr * 30; -} - -void rct_insert(tjec* ec, int stuck) -{ - if (stuck) - { - ++ec->rct_count; - - if (ec->rct_count >= rct_permanent_cutoff(ec->osr)) - ec->health_failure |= RCT_FAILURE_PERMANENT; - else if (ec->rct_count >= rct_intermittent_cutoff(ec->osr)) - ec->health_failure |= RCT_FAILURE; - } - else - { - ec->rct_count = 0; - } -} - -// TJEC - -int tjec_is_stuck(tjec* ec, uint64_t delta) -{ - uint64_t delta2; - uint64_t delta3; - if (ec->lag_predictor) - { - delta2 = lag_delta2(ec, delta); - delta3 = lag_delta3(ec, delta2); - } - else - { - delta2 = delta - ec->prev_delta; - ec->prev_delta = delta; - delta3 = delta2 - ec->prev_delta2; - ec->prev_delta2 = delta2; - } - - apt_insert(ec, delta); - if (ec->lag_predictor) - lag_insert(ec, delta); - - if (!delta || !delta2 || !delta3) - { - rct_insert(ec, 1); - return 1; - } - - rct_insert(ec, 0); - return 0; -} - -uint32_t tjec_health_failure(tjec* ec) -{ - if (!ec->fips_enabled) - return 0; - return ec->health_failure; -} - -uint64_t tjec_loop_shuffle(tjec* ec, uint32_t bits, uint32_t min) -{ - (void) ec; - uint64_t time_now = (uint64_t) CoreS_GetNativeTimerTick(); - - uint64_t mask = (UINT64_C(1) << bits) - 1; - uint64_t shuffle = 0; - for (uint32_t i = 0; i < (((sizeof(time_now) << 3) + bits - 1) / bits); ++i) - { - shuffle ^= time_now & mask; - time_now = time_now >> bits; - } - return shuffle + (UINT64_C(1) << min); -} - -void tjec_hash_time(tjec* ec, uint64_t delta, int stuck) -{ - tjec_sha3 sha3; - - uint8_t temp[sizeof(((tjec_sha3*) 0)->partial)]; - memset(temp, 0, sizeof(temp)); - - uint64_t loop_count = tjec_loop_shuffle(ec, 1 + ((ec->flags >> 11) & 7), 0); - - sha3_256_init(&sha3); - for (uint64_t j = 0; j < loop_count; ++j) - { - sha3_update(&sha3, temp, 32); - sha3_update(&sha3, &ec->rct_count, sizeof(ec->rct_count)); - sha3_update(&sha3, &ec->apt_cutoff, sizeof(ec->apt_cutoff)); - sha3_update(&sha3, &ec->apt_observations, sizeof(ec->apt_observations)); - sha3_update(&sha3, &ec->apt_count, sizeof(ec->apt_count)); - sha3_update(&sha3, &ec->apt_base, sizeof(ec->apt_base)); - sha3_update(&sha3, &j, sizeof(j)); - sha3_final(&sha3, temp); - } - - if (stuck) - delta = 0; - - memcpy(temp + 32, &delta, sizeof(delta)); - sha3_update(&ec->sha3, temp, sizeof(temp)); - - memset(&sha3, 0, sizeof(sha3)); - memset(temp, 0, sizeof(temp)); -} - -static uint32_t xoshiro128starstar(uint32_t* s) -{ - const uint32_t result = rotl32(s[1] * 5, 7) * 9; - const uint32_t t = s[1] << 9; - - s[2] ^= s[0]; - s[3] ^= s[1]; - s[1] ^= s[2]; - s[0] ^= s[3]; - s[2] ^= t; - s[3] = rotl32(s[3], 11); - return result; -} - -void tjec_random_memory_access(tjec* ec) -{ - uint32_t prng_state[4] = { 0x8E93EEC0, 0xCE65608A, 0xA8D46B46, 0xE83CEF69 }; - - uint64_t loop_count = ec->base_acc_count + tjec_loop_shuffle(ec, 1 + ((ec->flags >> 8) & 7), 0); - - for (size_t i = 0; i < sizeof(prng_state) / sizeof(*prng_state); ++i) - { - // It is very likely prng_state[i] = v & 0xFF | ((v & 0xFF) << 8) | ((v & 0xFF) << 16) | ((v & 0xFF) << 24), i.e. same byte repeated, because taking the time and doing bitwise and + shift left shouldn't generally cost enough to cause timer increments. - // for that reason, the following code is commented out, and replaced with a broadcast instead. - // prng_state[i] = (CoreS_GetNativeTimerTick() & 0xFF) | ((CoreS_GetTimerTick() & 0xFF) << 8) | ((CoreS_GetTimerTick() & 0xFF) << 16) | ((CoreS_GetTimerTick() & 0xFF) << 24); - prng_state[i] = (CoreS_GetNativeTimerTick() & 0xFF) * 0x01010101; - } - - for (uint64_t i = 0; i < loop_count; ++i) - { - atomic_uchar* pMem = ec->memory->memory + (xoshiro128starstar(prng_state) % ec->memory->size); - *pMem = *pMem + 1; - } -} - -void tjec_memory_access(tjec* ec) -{ - uint64_t loop_count = ec->base_acc_count + tjec_loop_shuffle(ec, 1 + ((ec->flags >> 8) & 7), 0); - - for (uint64_t i = 0; i < loop_count; ++i) - { - atomic_uchar* pMem = ec->memory->memory + ec->location; - *pMem = *pMem + 1; - - ec->location = ec->location + ec->memory->cell_size - 1; - ec->location = ec->location % ec->memory->size; - } -} - -int tjec_measure_jitter(tjec* ec, uint64_t* current_delta) -{ - if (ec->random_access) - tjec_random_memory_access(ec); - else - tjec_memory_access(ec); - - uint64_t time_now = (uint64_t) CoreS_GetNativeTimerTick(); - uint64_t delta = (time_now - ec->prev_time) / ec->common_time_gcd; - ec->prev_time = time_now; - - int stuck = tjec_is_stuck(ec, delta); - tjec_hash_time(ec, delta, stuck); - - if (current_delta) - *current_delta = delta; - return stuck; -} - -void tjec_random_data(tjec* ec) -{ - uint32_t bits_to_read = (256 + (ec->fips_enabled ? ENTROPY_SAFETY_FACTOR : 0)) * ec->osr; - - tjec_measure_jitter(ec, NULL); - - uint32_t k = 0; - while (!tjec_health_failure(ec)) - { - if (tjec_measure_jitter(ec, NULL)) - continue; - - if (++k >= bits_to_read) - break; - } -} - -void tjec_read_random_block(tjec* ec, void* dst, size_t size) -{ - uint8_t block[32]; - sha3_final(&ec->sha3, block); - if (size) - memcpy(dst, block, size); - sha3_update(&ec->sha3, block, sizeof(block)); - memset(block, 0, sizeof(block)); -} - -uint32_t tjec_find_common_time_gcd(tjec* ec) -{ - uint64_t gcd_history[1024]; - uint32_t time_backwards = 0; - uint32_t count_stuck = 0; - - const int CLEARCACHE = 100; - for (int i = -CLEARCACHE; i < 1024; ++i) - { - uint64_t delta = 0; - int stuck = tjec_measure_jitter(ec, &delta); - uint64_t end_time = ec->prev_time; - uint64_t start_time = end_time - delta; - if (!start_time || !end_time) - return TJEC_ENOTIME; - - if (!delta || (end_time == start_time)) - return TJEC_ECOARSETIME; - - if (i < 0) - continue; - - if (stuck) - ++count_stuck; - - if (end_time < start_time) - ++time_backwards; - - gcd_history[i] = delta; - } - - if (time_backwards > 3) - return TJEC_ENOMONOTONIC; - - uint32_t health_test = tjec_health_failure(ec); - if (health_test) - return (health_test & RCT_FAILURE) ? TJEC_ERCT : TJEC_EHEALTH; - - if (count_stuck > (1024 * 9) / 10) - return TJEC_ESTUCK; - - { - uint64_t running_gcd = gcd_history[0]; - uint64_t delta_sum = 0; - - for (size_t i = 1; i < 1024; ++i) - { - if (gcd_history[i] >= gcd_history[i - 1]) - delta_sum += gcd_history[i] - gcd_history[i - 1]; - else - delta_sum += gcd_history[i - 1] - gcd_history[i]; - running_gcd = gcd64(gcd_history[i], running_gcd); - } - - if ((delta_sum * ec->osr) < 1024) - return TJEC_EMINVARVAR; - if (running_gcd >= UINT32_MAX / 2) - return TJEC_ECOARSETIME; - - ec->common_time_gcd = running_gcd; - } - - return TJEC_ENOERR; -} - -uint64_t tjec_memory_size(uint64_t flags) -{ - uint64_t max_memory_size; - if ((flags & 0xF) == 0) - { - if (flags & TJEC_MEM_RANDOM_ACCESS) - max_memory_size = UINT64_C(1) << 17; - else - max_memory_size = CELL_SIZE * CELL_COUNT; - } - else - { - max_memory_size = UINT64_C(32) << (9 + (flags & 0xF)); - } - - uint64_t cache_memory_size = temp_l1_cache_size(); - uint64_t memory_size = (!cache_memory_size || max_memory_size < cache_memory_size) ? max_memory_size : cache_memory_size; - return memory_size; -} - -// SHA-3 - -#if SHA3_UNROLLED - -static void keccakp_1600(uint64_t* s) -{ - const uint8_t rols[24] = { 1, 62, 28, 27, 36, 44, 6, 55, 20, 3, 10, 43, 25, 39, 41, 45, 15, 21, 8, 18, 2, 61, 56, 14 }; - const uint64_t iota_values[] = { 0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL, 0x8000000080008000ULL, 0x000000000000808bULL, 0x0000000080000001ULL, 0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008aULL, 0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL, 0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL, 0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL, 0x000000000000800aULL, 0x800000008000000aULL, 0x8000000080008081ULL, 0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL }; - - uint64_t C[5], D[5]; - for (uint8_t round = 0; round < 24; ++round) - { - C[0] = s[0] ^ s[5] ^ s[10] ^ s[15] ^ s[20]; - C[1] = s[1] ^ s[6] ^ s[11] ^ s[16] ^ s[21]; - C[2] = s[2] ^ s[7] ^ s[12] ^ s[17] ^ s[22]; - C[3] = s[3] ^ s[8] ^ s[13] ^ s[18] ^ s[23]; - C[4] = s[4] ^ s[9] ^ s[14] ^ s[19] ^ s[24]; - D[0] = C[4] ^ rotl64(C[1], 1); - D[1] = C[0] ^ rotl64(C[2], 1); - D[2] = C[1] ^ rotl64(C[3], 1); - D[3] = C[2] ^ rotl64(C[4], 1); - D[4] = C[3] ^ rotl64(C[0], 1); - - uint64_t tmp = s[7]; - - s[0] = s[0] ^ D[0]; - s[7] = rotl64(s[10] ^ D[0], rols[9]); - s[10] = rotl64(s[1] ^ D[1], rols[0]); - s[1] = rotl64(s[6] ^ D[1], rols[5]); - s[6] = rotl64(s[9] ^ D[4], rols[8]); - s[9] = rotl64(s[22] ^ D[2], rols[21]); - s[22] = rotl64(s[14] ^ D[4], rols[13]); - s[14] = rotl64(s[20] ^ D[0], rols[19]); - s[20] = rotl64(s[2] ^ D[2], rols[1]); - s[2] = rotl64(s[12] ^ D[2], rols[11]); - s[12] = rotl64(s[13] ^ D[3], rols[12]); - s[13] = rotl64(s[19] ^ D[4], rols[18]); - s[19] = rotl64(s[23] ^ D[3], rols[22]); - s[23] = rotl64(s[15] ^ D[0], rols[14]); - s[15] = rotl64(s[4] ^ D[4], rols[3]); - s[4] = rotl64(s[24] ^ D[4], rols[23]); - s[24] = rotl64(s[21] ^ D[1], rols[20]); - s[21] = rotl64(s[8] ^ D[3], rols[7]); - s[8] = rotl64(s[16] ^ D[1], rols[15]); - s[16] = rotl64(s[5] ^ D[0], rols[4]); - s[5] = rotl64(s[3] ^ D[3], rols[2]); - s[3] = rotl64(s[18] ^ D[3], rols[17]); - s[18] = rotl64(s[17] ^ D[2], rols[16]); - s[17] = rotl64(s[11] ^ D[1], rols[10]); - s[11] = rotl64(tmp ^ D[2], rols[6]); - - C[0] = s[0]; - C[1] = s[5]; - C[2] = s[10]; - C[3] = s[15]; - C[4] = s[20]; - D[0] = s[1]; - D[1] = s[6]; - D[2] = s[11]; - D[3] = s[16]; - D[4] = s[21]; - s[0] ^= ~s[1] & s[2]; - s[1] ^= ~s[2] & s[3]; - s[2] ^= ~s[3] & s[4]; - s[3] ^= ~s[4] & C[0]; - s[4] ^= ~C[0] & D[0]; - s[5] ^= ~s[6] & s[7]; - s[6] ^= ~s[7] & s[8]; - s[7] ^= ~s[8] & s[9]; - s[8] ^= ~s[9] & C[1]; - s[9] ^= ~C[1] & D[1]; - s[10] ^= ~s[11] & s[12]; - s[11] ^= ~s[12] & s[13]; - s[12] ^= ~s[13] & s[14]; - s[13] ^= ~s[14] & C[2]; - s[14] ^= ~C[2] & D[2]; - s[15] ^= ~s[16] & s[17]; - s[16] ^= ~s[17] & s[18]; - s[17] ^= ~s[18] & s[19]; - s[18] ^= ~s[19] & C[3]; - s[19] ^= ~C[3] & D[3]; - s[20] ^= ~s[21] & s[22]; - s[21] ^= ~s[22] & s[23]; - s[22] ^= ~s[23] & s[24]; - s[23] ^= ~s[24] & C[4]; - s[24] ^= ~C[4] & D[4]; - - s[0] ^= iota_values[round]; - } -} - -#else - -static inline void keccakp_theta(uint64_t* s) -{ - uint64_t C[5], D[5]; - C[0] = s[0] ^ s[5] ^ s[10] ^ s[15] ^ s[20]; - C[1] = s[1] ^ s[6] ^ s[11] ^ s[16] ^ s[21]; - C[2] = s[2] ^ s[7] ^ s[12] ^ s[17] ^ s[22]; - C[3] = s[3] ^ s[8] ^ s[13] ^ s[18] ^ s[23]; - C[4] = s[4] ^ s[9] ^ s[14] ^ s[19] ^ s[24]; - D[0] = C[4] ^ rotl64(C[1], 1); - D[1] = C[0] ^ rotl64(C[2], 1); - D[2] = C[1] ^ rotl64(C[3], 1); - D[3] = C[2] ^ rotl64(C[4], 1); - D[4] = C[3] ^ rotl64(C[0], 1); - s[0] ^= D[0]; - s[1] ^= D[1]; - s[2] ^= D[2]; - s[3] ^= D[3]; - s[4] ^= D[4]; - s[5] ^= D[0]; - s[6] ^= D[1]; - s[7] ^= D[2]; - s[8] ^= D[3]; - s[9] ^= D[4]; - s[10] ^= D[0]; - s[11] ^= D[1]; - s[12] ^= D[2]; - s[13] ^= D[3]; - s[14] ^= D[4]; - s[15] ^= D[0]; - s[16] ^= D[1]; - s[17] ^= D[2]; - s[18] ^= D[3]; - s[19] ^= D[4]; - s[20] ^= D[0]; - s[21] ^= D[1]; - s[22] ^= D[2]; - s[23] ^= D[3]; - s[24] ^= D[4]; -} - -static inline void keccakp_rho(uint64_t* s) -{ - const uint8_t rols[24] = { 1, 62, 28, 27, 36, 44, 6, 55, 20, 3, 10, 43, 25, 39, 41, 45, 15, 21, 8, 18, 2, 61, 56, 14 }; - s[1] = rotl64(s[1], rols[0]); - s[2] = rotl64(s[2], rols[1]); - s[3] = rotl64(s[3], rols[2]); - s[4] = rotl64(s[4], rols[3]); - s[5] = rotl64(s[5], rols[4]); - s[6] = rotl64(s[6], rols[5]); - s[7] = rotl64(s[7], rols[6]); - s[8] = rotl64(s[8], rols[7]); - s[9] = rotl64(s[9], rols[8]); - s[10] = rotl64(s[10], rols[9]); - s[11] = rotl64(s[11], rols[10]); - s[12] = rotl64(s[12], rols[11]); - s[13] = rotl64(s[13], rols[12]); - s[14] = rotl64(s[14], rols[13]); - s[15] = rotl64(s[15], rols[14]); - s[16] = rotl64(s[16], rols[15]); - s[17] = rotl64(s[17], rols[16]); - s[18] = rotl64(s[18], rols[17]); - s[19] = rotl64(s[19], rols[18]); - s[20] = rotl64(s[20], rols[19]); - s[21] = rotl64(s[21], rols[20]); - s[22] = rotl64(s[22], rols[21]); - s[23] = rotl64(s[23], rols[22]); - s[24] = rotl64(s[24], rols[23]); -} - -static inline void keccakp_pi(uint64_t* s) -{ - uint64_t tmp = s[21]; - s[0] = s[0]; - s[1] = s[6]; - s[2] = s[12]; - s[3] = s[18]; - s[4] = s[24]; - s[5] = s[3]; - s[6] = s[9]; - s[7] = s[10]; - s[8] = s[16]; - s[9] = s[22]; - s[10] = s[1]; - s[11] = s[7]; - s[12] = s[13]; - s[13] = s[19]; - s[14] = s[20]; - s[15] = s[4]; - s[16] = s[5]; - s[17] = s[11]; - s[18] = s[17]; - s[19] = s[23]; - s[20] = s[2]; - s[21] = s[8]; - s[22] = s[14]; - s[23] = s[15]; - s[24] = tmp; -} - -static inline void keccakp_chi(uint64_t* s) -{ - uint64_t t0[5], t1[5]; - t0[0] = s[0]; - t0[1] = s[5]; - t0[2] = s[10]; - t0[3] = s[15]; - t0[4] = s[20]; - t1[0] = s[1]; - t1[1] = s[6]; - t1[2] = s[11]; - t1[3] = s[16]; - t1[4] = s[21]; - s[0] ^= ~s[1] & s[2]; - s[5] ^= ~s[6] & s[7]; - s[10] ^= ~s[11] & s[12]; - s[15] ^= ~s[16] & s[17]; - s[20] ^= ~s[21] & s[22]; - s[1] ^= ~s[2] & s[3]; - s[6] ^= ~s[7] & s[8]; - s[11] ^= ~s[12] & s[13]; - s[16] ^= ~s[17] & s[18]; - s[21] ^= ~s[22] & s[23]; - s[2] ^= ~s[3] & s[4]; - s[7] ^= ~s[8] & s[9]; - s[12] ^= ~s[13] & s[14]; - s[17] ^= ~s[18] & s[19]; - s[22] ^= ~s[23] & s[24]; - s[3] ^= ~s[4] & t0[0]; - s[8] ^= ~s[9] & t0[1]; - s[13] ^= ~s[14] & t0[2]; - s[18] ^= ~s[19] & t0[3]; - s[23] ^= ~s[24] & t0[4]; - s[4] ^= ~t0[0] & t1[0]; - s[9] ^= ~t0[1] & t1[1]; - s[14] ^= ~t0[2] & t1[2]; - s[19] ^= ~t0[3] & t1[3]; - s[24] ^= ~t0[4] & t1[4]; -} - -static inline void keccakp_iota(uint64_t* s, uint8_t round) -{ - const uint64_t iota_values[] = { 0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL, 0x8000000080008000ULL, 0x000000000000808bULL, 0x0000000080000001ULL, 0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008aULL, 0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL, 0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL, 0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL, 0x000000000000800aULL, 0x800000008000000aULL, 0x8000000080008081ULL, 0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL }; - s[0] ^= iota_values[round]; -} - -static void keccakp_1600(uint64_t* s) -{ - for (uint8_t round = 0; round < 24; ++round) - { - keccakp_theta(s); - keccakp_rho(s); - keccakp_pi(s); - keccakp_chi(s); - keccakp_iota(s, round); - } -} - -#endif - -void sha3_init(tjec_sha3* ctx) -{ - memset(ctx->state, 0, sizeof(ctx->state)); - ctx->msg_len = 0; -} - -void sha3_256_init(tjec_sha3* ctx) -{ - sha3_init(ctx); - ctx->r = 136; - ctx->rword = 136 / sizeof(uint64_t); - ctx->digest_size = 32; -} - -void sha3_update(tjec_sha3* ctx, const void* data, size_t size) -{ - const uint8_t* ptr = (const uint8_t*) data; - - size_t partial = ctx->msg_len % ctx->r; - ctx->msg_len += size; - if (partial) - { - size_t to_copy = ctx->r - partial; - if (size < to_copy) - { - memcpy(ctx->partial + partial, ptr, size); - return; - } - - memcpy(ctx->partial + partial, ptr, to_copy); - size -= to_copy; - ptr += to_copy; - - for (size_t i = 0; i < ctx->rword; ++i) - ctx->state[i] ^= ((uint64_t*) ctx->partial)[i]; - keccakp_1600(ctx->state); - } - - for (; size >= ctx->r; size -= ctx->r, ptr += ctx->r) - { - for (size_t i = 0; i < ctx->rword; ++i) - ctx->state[i] ^= ((uint64_t*) ptr)[i]; - keccakp_1600(ctx->state); - } - - if (size) - memcpy(ctx->partial, ptr, size); -} - -void sha3_final(tjec_sha3* ctx, void* digest) -{ - size_t partial = ctx->msg_len % ctx->r; - memset(ctx->partial + partial, 0, ctx->r - partial); - - ctx->partial[partial] = 0x06; - ctx->partial[ctx->r - 1] |= 0x80; - - for (size_t i = 0; i < ctx->rword; ++i) - ctx->state[i] ^= ((uint64_t*) ctx->partial)[i]; - keccakp_1600(ctx->state); - - memcpy(digest, ctx->state, ctx->digest_size); - memset(ctx->partial, 0, ctx->r); - sha3_init(ctx); -} \ No newline at end of file diff --git a/src/oboskrnl/driver_interface/driverId.h b/src/oboskrnl/driver_interface/driverId.h index f744f86c..ed549390 100644 --- a/src/oboskrnl/driver_interface/driverId.h +++ b/src/oboskrnl/driver_interface/driverId.h @@ -1,111 +1,63 @@ /* - * oboskrnl/driver_interface/driverId.h - * - * Copyright (c) 2024-2025 Omar Berrow + oboskrnl/driver_interface/driverId.h + + Copyright (c) 2024 Omar Berrow */ #pragma once #include -#include - #include -#include - -enum -{ - SYMBOL_TYPE_FUNCTION, - SYMBOL_TYPE_VARIABLE, - SYMBOL_TYPE_FILE, -}; -enum -{ - SYMBOL_VISIBILITY_DEFAULT, - SYMBOL_VISIBILITY_HIDDEN, -}; -typedef struct driver_symbol -{ - RB_ENTRY(driver_symbol) rb_entry; - const char* name; - uintptr_t address; - uintptr_t size; - int8_t type : 4; - int8_t visibility : 4; -} driver_symbol; -typedef RB_HEAD(symbol_table, driver_symbol) symbol_table; -RB_PROTOTYPE(symbol_table, driver_symbol, rb_entry, cmp_symbols); -typedef struct driver_node -{ - struct driver_node *next, *prev; - struct driver_id* data; -} driver_node; -typedef struct driver_list -{ - driver_node *head, *tail; - size_t nNodes; -} driver_list; - -// TODO: Lock? - -#define APPEND_DRIVER_NODE(list, node) do {\ - (node)->next = nullptr;\ - (node)->prev = nullptr;\ - if ((list).tail)\ - (list).tail->next = (node);\ - if (!(list).head)\ - (list).head = (node);\ - (node)->prev = ((list).tail);\ - (list).tail = (node);\ - (list).nNodes++;\ -} while(0) -#define REMOVE_DRIVER_NODE(list, node) do {\ - if ((list).tail == (node))\ - (list).tail = (node)->prev;\ - if ((list).head == (node))\ - (list).head = (node)->next;\ - if ((node)->prev)\ - (node)->prev->next = (node)->next;\ - if ((node)->next)\ - (node)->next->prev = (node)->prev;\ - (list).nNodes--;\ -} while(0) - -typedef struct driver_id -{ - uint32_t id; - void* base; - void* top; - symbol_table symbols; - // A copy of the driver's header. - driver_header header; - uintptr_t entryAddr; // If zero, there is no entry point. - // The amount of loaded drivers that depend on this driver. - // Also incremented whenever a user thread opens a handle to this driver. - // This is set to one (the kernel) on driver load. - size_t refCnt; - // The driver's dependencies. - driver_list dependencies; - // The node in Drv_LoadedDrivers - driver_node node; - // The node in Drv_LoadedFsDrivers - driver_node other_node; - thread* main_thread; - bool started; -} driver_id; -// To be filled out by the arch-specific code of the kernel before the driver interface is called. -extern driver_list Drv_LoadedDrivers; -extern driver_list Drv_LoadedFsDrivers; -extern symbol_table OBOS_KernelSymbolTable; +#include -typedef struct driver_init_status +namespace obos { - obos_status status; - // An additional message to print describing the failure. can be nullptr - const char* context; - // If true, the driver will be unloaded. Ignored if obos_is_success(status) == true. - bool fatal; -} driver_init_status; -// id->main must be the current thread, otherwise this function silently fails. -OBOS_EXPORT void Drv_ExitDriver(struct driver_id* id, const driver_init_status* status); + namespace driverInterface + { + struct driverSymbol + { + const char* name = nullptr; + uintptr_t address = 0; + enum + { + SYMBOL_INVALID, + SYMBOL_FUNC, + SYMBOL_VARIABLE, + } type = SYMBOL_INVALID; + }; + struct driverId + { + static uint32_t nextDriverId; + uint32_t id = 0; + // This member is set to the header section of the driver, thus it can be modified to set driver header values. + driverHeader* header = nullptr; + // Please don't modify this after loading. + utils::Vector symbols{}; + void* driverBaseAddress = nullptr; + void(*driverEntry)() = nullptr; + // Should be set by the driver when it is done initializing itself. + // If the driver's entry is never called, this will likely stay false. + // Make sure this is true before calling any driver functions. + bool isDriverInitialized = false; + }; + struct driverIdNode + { + driverIdNode *next = nullptr, *prev = nullptr; + driverId* data = nullptr; + }; + struct driverIdList + { + driverIdNode *head = nullptr, *tail = nullptr; + size_t nNodes = 0; + void Append(driverId*); + void Remove(driverId*); + void Remove(uint32_t); + driverIdNode* Find(driverId*); + driverIdNode* Find(uint32_t); + }; + extern driverIdList g_driverTable[(int)driverType::MaxValue]; + extern driverIdList g_drivers; + } +} \ No newline at end of file diff --git a/src/oboskrnl/driver_interface/drv_sys.c b/src/oboskrnl/driver_interface/drv_sys.c deleted file mode 100644 index 5a727852..00000000 --- a/src/oboskrnl/driver_interface/drv_sys.c +++ /dev/null @@ -1,257 +0,0 @@ -/* - * oboskrnl/driver_interface/drv_sys.c - * - * Copyright (c) 2024-2025 Omar Berrow - */ - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include - -#include -#include -#include - -#include - -handle Sys_LoadDriver(const void* file, size_t szFile, obos_status* ustatus) -{ - obos_status status = OBOS_CapabilityCheck("drv/load", false); - if (obos_is_error(status)) - return status; - - if (!szFile) - { - status = OBOS_STATUS_INVALID_ARGUMENT; - if (ustatus) memcpy_k_to_usr(ustatus, &status, sizeof(obos_status)); - return HANDLE_INVALID; - } - const void* buf = Mm_MapViewOfUserMemory(CoreS_GetCPULocalPtr()->currentContext, (void*)file, nullptr, szFile, OBOS_PROTECTION_READ_ONLY, true, &status); - if (!buf) - { - if (ustatus) memcpy_k_to_usr(ustatus, &status, sizeof(obos_status)); - return HANDLE_INVALID; - } - driver_id* id = Drv_LoadDriver(buf, szFile, &status); - Mm_VirtualMemoryFree(&Mm_KernelContext, (void*)buf, szFile); - if (!id) - { - if (ustatus) memcpy_k_to_usr(ustatus, &status, sizeof(obos_status)); - return HANDLE_INVALID; - } - - handle_desc* desc = nullptr; - OBOS_LockHandleTable(OBOS_CurrentHandleTable()); - handle ret = OBOS_HandleAllocate(OBOS_CurrentHandleTable(), HANDLE_TYPE_DRIVER_ID, &desc); - id->refCnt++; - desc->un.driver_id = id; - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - - return ret; -} - -obos_status Sys_StartDriver(handle driver, handle* mainThread) -{ - obos_status status = OBOS_CapabilityCheck("drv/start", false); - if (obos_is_error(status)) - return status; - - OBOS_LockHandleTable(OBOS_CurrentHandleTable()); - handle_desc* drv = OBOS_HandleLookup(OBOS_CurrentHandleTable(), driver, HANDLE_TYPE_DRIVER_ID, false, &status); - if (!drv) - { - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - return status; - } - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - - handle tmp = HANDLE_INVALID; - status = memcpy_k_to_usr(mainThread, &tmp, sizeof(handle)); - if (obos_is_error(status) && mainThread != nullptr) - return status; - - thread* mainThreadPtr = nullptr; - status = Drv_StartDriver(drv->un.driver_id, mainThread ? &mainThreadPtr : nullptr); - if (mainThread && obos_is_success(status)) - { - OBOS_LockHandleTable(OBOS_CurrentHandleTable()); - handle_desc* desc = nullptr; - handle hnd = OBOS_HandleAllocate(OBOS_CurrentHandleTable(), HANDLE_TYPE_THREAD, &desc); - desc->un.thread = mainThreadPtr; - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - status = memcpy_k_to_usr(mainThread, &hnd, sizeof(handle)); - } - - return status; -} - -obos_status Sys_UnloadDriver(handle driver) -{ - obos_status status = OBOS_CapabilityCheck("drv/unload", false); - if (obos_is_error(status)) - return status; - - OBOS_LockHandleTable(OBOS_CurrentHandleTable()); - handle_desc* drv = OBOS_HandleLookup(OBOS_CurrentHandleTable(), driver, HANDLE_TYPE_DRIVER_ID, false, &status); - if (!drv) - { - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - return status; - } - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - - driver_id* id = drv->un.driver_id; - - // Otherwise, refcount is too high for the driver to be unloaded - Sys_HandleClose(driver); - - return Drv_UnloadDriver(id); -} - -obos_status Sys_PnpLoadDriversAt(handle dent, bool wait) -{ - obos_status status = OBOS_CapabilityCheck("drv/load-pnp", false); - if (obos_is_error(status)) - return status; - - OBOS_LockHandleTable(OBOS_CurrentHandleTable()); - handle_desc* dirent = OBOS_HandleLookup(OBOS_CurrentHandleTable(), dent, HANDLE_TYPE_DIRENT, false, &status); - if (!dirent) - { - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - return status; - } - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - - return Drv_PnpLoadDriversAt(dirent->un.dirent->parent, wait); -} - -// Finds a loaded driver by its name, and returns a handle to it. -handle Sys_FindDriverByName(const char* uname /* assumed to be 64-bytes at max */) -{ - obos_status status = OBOS_CapabilityCheck("drv/open-name", false); - if (obos_is_error(status)) - return HANDLE_INVALID; - - char* name = nullptr; - size_t sz_name = 0; - status = OBOSH_ReadUserString(uname, nullptr, &sz_name); - if (obos_is_error(status)) - return HANDLE_INVALID; - if (sz_name >= 64) - return HANDLE_INVALID; - name = ZeroAllocate(OBOS_KernelAllocator, sz_name+1, sizeof(char), nullptr); - OBOSH_ReadUserString(uname, name, nullptr); - - driver_id *id = nullptr; - for (driver_node* curr = Drv_LoadedDrivers.head; curr; ) - { - if (strncmp(name, curr->data->header.driverName, 64)) - { - id = curr->data; - break; - } - - curr = curr->next; - } - Free(OBOS_KernelAllocator, name, sz_name+1); - if (!id) - return HANDLE_INVALID; - - handle_desc* desc = nullptr; - OBOS_LockHandleTable(OBOS_CurrentHandleTable()); - handle ret = OBOS_HandleAllocate(OBOS_CurrentHandleTable(), HANDLE_TYPE_DRIVER_ID, &desc); - id->refCnt++; - desc->un.driver_id = id; - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - - return ret; -} - -// Returns the next driver in the list -// The handle 'curr' is not automatically closed. -// If 'curr' is HANDLE_INVALID, the first item in the list is returned. -handle Sys_EnumerateLoadedDrivers(handle curr) -{ - obos_status status = OBOS_CapabilityCheck("drv/enumerate", false); - if (obos_is_error(status)) - return HANDLE_INVALID; - - driver_id* id = nullptr; - - if (curr != HANDLE_INVALID) - { - OBOS_LockHandleTable(OBOS_CurrentHandleTable()); - handle_desc* drv = OBOS_HandleLookup(OBOS_CurrentHandleTable(), curr, HANDLE_TYPE_DRIVER_ID, false, nullptr); - if (!drv) - { - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - return HANDLE_INVALID; - } - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - id = drv->un.driver_id; - if (!id->node.next) - return HANDLE_INVALID; - id = id->node.next->data; - } - else - id = Drv_LoadedDrivers.head->data; - - handle_desc* desc = nullptr; - OBOS_LockHandleTable(OBOS_CurrentHandleTable()); - handle ret = OBOS_HandleAllocate(OBOS_CurrentHandleTable(), HANDLE_TYPE_DRIVER_ID, &desc); - id->refCnt++; - desc->un.driver_id = id; - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - - return ret; -} - -// Queries the name of the driver in 'drv'. -obos_status Sys_QueryDriverName(handle driver, char* namebuf, size_t *sznamebuf /* need not be over 64 */) -{ - obos_status status = OBOS_CapabilityCheck("drv/query-name", false); - if (obos_is_error(status)) - return status; - - if (!sznamebuf) - return OBOS_STATUS_INVALID_ARGUMENT; - - OBOS_LockHandleTable(OBOS_CurrentHandleTable()); - handle_desc* drv = OBOS_HandleLookup(OBOS_CurrentHandleTable(), driver, HANDLE_TYPE_DRIVER_ID, false, &status); - if (!drv) - { - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - return status; - } - OBOS_UnlockHandleTable(OBOS_CurrentHandleTable()); - - size_t strlen_name = strnlen(drv->un.driver_id->header.driverName, 64); - - size_t sznamebuf_deref = 0; - status = memcpy_usr_to_k(&sznamebuf_deref, sznamebuf, sizeof(size_t)); - if (obos_is_error(status)) - return status; - - if (namebuf) - { - status = memcpy_k_to_usr(namebuf, drv->un.driver_id->header.driverName, OBOS_MIN(sznamebuf_deref, strlen_name)); - if (obos_is_error(status)) - return status; - } - - status = memcpy_k_to_usr(sznamebuf, &strlen_name, sizeof(size_t)); - if (obos_is_error(status)) - return status; - - return OBOS_STATUS_SUCCESS; -} diff --git a/src/oboskrnl/driver_interface/drv_sys.h b/src/oboskrnl/driver_interface/drv_sys.h deleted file mode 100644 index de803370..00000000 --- a/src/oboskrnl/driver_interface/drv_sys.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * oboskrnl/driver_interface/drv_sys.h - * - * Copyright (c) 2024 Omar Berrow - */ - -#pragma once - -#include -#include -#include - -handle Sys_LoadDriver(const void* file, size_t szFile, obos_status* status); -obos_status Sys_StartDriver(handle driver, handle* mainThread); -obos_status Sys_UnloadDriver(handle driver); - -obos_status Sys_PnpLoadDriversAt(handle dent, bool wait); - -// Finds a loaded driver by its name, and returns a handle to it. -// If the calling process has insufficient permissions, HANDLE_INVALID is returned. -handle Sys_FindDriverByName(const char* name /* assumed to be 64-bytes at max */); - -// Returns the next driver in the list -// The handle 'curr' is not automatically closed. -// If 'curr' is HANDLE_INVALID, the first item in the list is returned. -// If the calling process has insufficient permissions, HANDLE_INVALID is returned. -handle Sys_EnumerateLoadedDrivers(handle curr); - -// Queries the name of the driver in 'drv'. -// sznamebuf shall never be NULL. -// sznamebuf is the size of the buffer passed, and *sznamebuf is set to the actual length of the string. -// When copying the buffer, MIN(*sznamebuf, strlen(driverName)) is chosen. -obos_status Sys_QueryDriverName(handle drv, char* namebuf, size_t *sznamebuf /* need not be over 64 */); - -obos_status Sys_GetHDADevices(handle* arr, size_t* count, uint32_t oflags); \ No newline at end of file diff --git a/src/oboskrnl/driver_interface/header.h b/src/oboskrnl/driver_interface/header.h index 945553f8..ceac8c10 100644 --- a/src/oboskrnl/driver_interface/header.h +++ b/src/oboskrnl/driver_interface/header.h @@ -1,307 +1,88 @@ /* - * oboskrnl/driver_interface/header.h - * - * Copyright (c) 2024-2025 Omar Berrow + oboskrnl/driver_interface/header.h + + Copyright (c) 2024 Omar Berrow */ #pragma once #include -#include -#include -#include - -#include - -#include - -enum { OBOS_DRIVER_MAGIC = 0x00116d868ac84e59 }; -// Not required, but can speed up loading times if the driver header is put in here. -#define OBOS_DRIVER_HEADER_SECTION ".driverheader" - -typedef enum driver_header_flags -{ - /// - /// Should the driver be detected through ACPI? - /// - DRIVER_HEADER_FLAGS_DETECT_VIA_ACPI = 0x1, - /// - /// Should the driver be detected through PCI? - /// - DRIVER_HEADER_FLAGS_DETECT_VIA_PCI = 0x2, - /// - /// If the driver does not have an entry point, specify this flag. - /// - DRIVER_HEADER_FLAGS_NO_ENTRY = 0x4, - /// - /// If set, the driver chooses its entry point's stack size. - /// Ignored if DRIVER_HEADER_FLAGS_NO_ENTRY is set. - /// - DRIVER_HEADER_FLAGS_REQUEST_STACK_SIZE = 0x8, - /// - /// Whether the driver exposes the standard driver interfaces to the kernel via the function table in the driver header. - /// If unset, the driver needs to expose its own interfaces using DRV_EXPORT. - /// NOTE: Every driver needs to have an ioctl callback in the function table, despite the state of this flag. - /// - DRIVER_HEADER_HAS_STANDARD_INTERFACES = 0x10, - /// - /// This flag should be set if the device is read from pipe-style. - /// If this flag is set, any blkOffset parameter should be ignored. - /// - DRIVER_HEADER_PIPE_STYLE_DEVICE = 0x20, - /// - /// Set if PnP should use the vendor id in the pciId field of the header. - /// - DRIVER_HEADER_PCI_HAS_VENDOR_ID = 0x40, - /// - /// Set if PnP should use the device id in the pciId field of the header. - /// - DRIVER_HEADER_PCI_HAS_DEVICE_ID = 0x80, - /// - /// Set if the driver header has the version field. - /// - DRIVER_HEADER_HAS_VERSION_FIELD = 0x100, - /// - /// Set to tell PnP to ignore the driver. - /// - DRIVER_HEADER_PNP_IGNORE = 0x200, - /// - /// Set if PnP should ignore the Prog IF in the the pciId field of the header. - /// - DRIVER_HEADER_PCI_IGNORE_PROG_IF = 0x400, - /// - /// Set if the filesystem driver wants paths for mk_file, move_desc_to, and remove_file - /// - DRIVER_HEADER_DIRENT_CB_PATHS = 0x800, -} driver_header_flags; -typedef enum iterate_decision -{ - ITERATE_DECISION_CONTINUE, - ITERATE_DECISION_STOP, -} iterate_decision; -typedef union driver_file_perm -{ - struct { - bool other_exec : 1; - bool other_write : 1; - bool other_read : 1; - bool group_exec : 1; - bool group_write : 1; - bool group_read : 1; - bool owner_exec : 1; - bool owner_write : 1; - bool owner_read : 1; - bool set_uid : 1; - bool set_gid : 1; - } OBOS_PACK; - uint16_t mode; -} OBOS_PACK driver_file_perm; -typedef enum file_type -{ - FILE_TYPE_DIRECTORY, - FILE_TYPE_REGULAR_FILE, - FILE_TYPE_SYMBOLIC_LINK, -} file_type; -// Represents a driver-specific object. -// This could be a disk, partition, file, etc. -// This must be unique per-driver. -typedef uintptr_t dev_desc; - -enum { - FS_FLAGS_NOEXEC = BIT(0), - FS_FLAGS_RDONLY = BIT(1), -}; - -typedef struct drv_fs_info { - size_t fsBlockSize; - size_t freeBlocks; // in units of 'fsBlockSize' - - size_t partBlockSize; - size_t szFs; // in units of 'partBlockSize' - - size_t fileCount; - size_t availableFiles; // the count of files that can be made until the partition cannot hold anymore - - size_t nameMax; - - uint32_t flags; -} drv_fs_info; - -typedef struct driver_ftable -{ - // Note: If there is not an OBOS_STATUS for an error that a driver needs to return, choose the error closest to the error that you want to report, - // or return OBOS_STATUS_INTERNAL_ERROR. - - // --------------------------------------- - // ------- START GENERIC FUNCTIONS ------- - - // NOTE: Every driver should have these functions implemented. - - obos_status(*get_blk_size)(dev_desc desc, size_t* blkSize); - obos_status(*get_max_blk_count)(dev_desc desc, size_t* count); - obos_status(*read_sync)(dev_desc desc, void* buf, size_t blkCount, size_t blkOffset, size_t* nBlkRead); - obos_status(*write_sync)(dev_desc desc, const void* buf, size_t blkCount, size_t blkOffset, size_t* nBlkWritten); - obos_status(*submit_irp)(void* /* irp* */ request); - obos_status(*finalize_irp)(void* /* irp* */ request); // optional, can be nullptr - // Optional, can be nullptr - // Note, *desc is subject to change by the driver - obos_status(*reference_device)(dev_desc* desc); - // Required, if reference_device exists. - obos_status(*unreference_device)(dev_desc); - obos_status(*foreach_device)(iterate_decision(*cb)(dev_desc desc, size_t blkSize, size_t blkCount, void* userdata), void* userdata); // unrequired for fs drivers. - obos_status(*query_user_readable_name)(dev_desc what, const char** name); // unrequired for fs drivers. - // The driver dictates what the request means, and what its parameters are. - obos_status(*ioctl)(dev_desc what, uint32_t request, void* argp); - obos_status(*ioctl_argp_size)(uint32_t request, size_t* ret); - // Called on driver unload. - // Frees all the driver's allocated resources, as the kernel - // does not keep a track of allocated resources, and cannot free them on driver unload, causing a - // memory leak. - void(*driver_cleanup_callback)(); - - // NOTE: These functions are optional for device drivers, and filesystem drivers shouldn't implement this. - // Although, it is still allowed, if for any reason these functions are required for a FS driver. - void(*on_suspend)(); - void(*on_wake)(); - - // -------- END GENERIC FUNCTIONS -------- - // --------------------------------------- - // ---------- START FS FUNCTIONS --------- - - // NOTE: FS Drivers must always return one from get_blk_size - // get_max_blk_count is the equivalent to get_filesize - // NOTE: Every function here must be pointing to something if the driver is an fs driver, otherwise they must be pointing to nullptr. - - // lifetime of *path is dictated by the driver. - obos_status(*query_path)(dev_desc desc, const char** path); - obos_status(*path_search)(dev_desc* found, void* vn, const char* what, dev_desc parent); - obos_status(*get_linked_path)(dev_desc desc, const char** linked); - obos_status(*vnode_search)(void** vn_found, dev_desc desc, void* dev_vn); // Not required to exist - - // vn is optional if parent is not UINTPTR_MAX (root directory). - union { - obos_status(*mk_file)(dev_desc* newDesc, dev_desc parent, void* vn, const char* name, file_type type, driver_file_perm perm); - obos_status(*pmk_file)(dev_desc* newDesc, const char* parent_path, void* vn, const char* name, file_type type, driver_file_perm perm); - }; - // If !new_parent && name, then we need to rename the file. - // If new_parent && !name, then we need to move the file to the new parent, and keep the filename. - // If new_parent && name, then we need to move the file to new parent and rename the file. - union { - obos_status(*move_desc_to)(dev_desc desc, dev_desc new_parent_desc, const char* name); - obos_status(*pmove_desc_to)(void* vn, const char* path, const char* newpath, const char* name); - }; - union { - // Really just unlinks the file. - obos_status(*remove_file)(dev_desc desc); - obos_status(*premove_file)(void* vn, const char* path); - }; - obos_status(*trunc_file)(dev_desc desc, size_t newsize /* note, newsize must be less than the filesize */); - - // hard links the file 'desc' to 'parent/name' - union { - obos_status(*hardlink_file)(dev_desc desc, dev_desc parent, const char* name); - obos_status(*phardlink_file)(dev_desc desc, const char* parent_path, void* vn, const char* name); - }; - obos_status(*symlink_set_path)(dev_desc desc, const char* to); - - obos_status(*get_file_perms)(dev_desc desc, driver_file_perm *perm); - obos_status(*get_file_owner)(dev_desc desc, uid *owner_uid, gid *group_uid); - obos_status(*get_file_type)(dev_desc desc, file_type *type); - obos_status(*get_file_inode)(dev_desc desc, uint32_t *ino); - // times is of type 'struct file_times' defined in vfs/vnode.h - obos_status(*get_file_times)(dev_desc desc, void* times); - - // times is of type 'struct file_times' defined in vfs/vnode.h - obos_status(*set_file_times)(dev_desc desc, const void* times); - obos_status(*set_file_perms)(dev_desc desc, driver_file_perm newperm); - // if an ID is -1, it means leave that field UNCHANGED - obos_status(*set_file_owner)(dev_desc desc, uid owner_uid, gid group_uid); - - // If dir is UINTPTR_MAX, it refers to the root directory. - obos_status(*list_dir)(dev_desc dir, void* vn, iterate_decision(*cb)(dev_desc desc, size_t blkSize, size_t blkCount, void* userdata, const char* name), void* userdata); - obos_status(*stat_fs_info)(void *vn, drv_fs_info *info); - - // Can only be nullptr for the InitRD driver. - // MUST be called before any operations on the filesystem for that vnode (e.g., list_dir, path_search). - bool(*probe)(void* vn); - // vn: vnode* - // target: dirent* - obos_status(*mount)(void* vn, void* target); - obos_status(*umount)(void* vn); - - // ----------- END FS FUNCTIONS ---------- - // --------------------------------------- -} driver_ftable; - -#define CURRENT_DRIVER_HEADER_VERSION (1) -typedef struct driver_header -{ - // Set to OBOS_DRIVER_MAGIC. - uint64_t magic; - // See driver_header_flags - uint32_t flags; - - // The PCI device associcated with this. - pci_hid pciId; - - struct - { - // These strings are not null-terminated. - // The PnP IDs for the driver. - // Each one of these is first compared with the HID. - // Then, each one of these is compared with the CID. - char pnpIds[32][8]; - // Ranges from 1-32 inclusive. - size_t nPnpIds; - } acpiId; - - size_t stackSize; // If DRIVER_HEADER_FLAGS_REQUEST_STACK_SIZE is set. - driver_ftable ftable; - char driverName[64]; - - uint32_t version; - - // If UACPI_INIT_LEVEL_EARLY, this field does nothing. - // If a uacpi symbol is used in the driver, and this field is specified, the kernel will the current uacpi init level against this. - // If the init level is < level, then the driver load is failed. - // Only valid if version >= 1, and the version field exists (flags & DRIVER_HEADER_HAS_VERSION_FIELD). - uint32_t uacpi_init_level_required; - - thread_affinity mainThreadAffinity; - - // Reserved for future use - char reserved[0x100-0x10]; -} driver_header; -typedef struct driver_header_node -{ - struct driver_header_node *next, *prev; - driver_header* data; -} driver_header_node; -typedef struct driver_header_list +namespace obos { - struct driver_header_node *head, *tail; - size_t nNodes; -} driver_header_list; -#define APPEND_DRIVER_HEADER_NODE(list, node) do {\ - (node)->next = nullptr;\ - (node)->prev = nullptr;\ - if ((list).tail)\ - (list).tail->next = (node);\ - if (!(list).head)\ - (list).head = (node);\ - (node)->prev = ((list).tail);\ - (list).tail = (node);\ - (list).nNodes++;\ -} while(0) -#define REMOVE_DRIVER_HEADER_NODE(list, node) do {\ - if ((list).tail == (node))\ - (list).tail = (node)->prev;\ - if ((list).head == (node))\ - (list).head = (node)->next;\ - if ((node)->prev)\ - (node)->prev->next = (node)->next;\ - if ((node)->next)\ - (node)->next->prev = (node)->prev;\ - (list).nNodes--;\ -} while(0) + namespace driverInterface + { + constexpr uint64_t g_driverHeaderMagic = 0x0002'7855'0650'CDAA; +#define OBOS_DRIVER_HEADER_SECTION ".obosDriverHeader" +#ifndef __INTELLISENSE__ +# define OBOS_DEFINE_IN_SECTION(s) __attribute__((section(s))) +#else +# define OBOS_DEFINE_IN_SECTION(section) +#endif + enum class driverType + { + Invalid = 0, + FilesystemDriver, + DeviceDriver, + NicDriver, + DriverLoader, + KernelExtension, + MaxValue = KernelExtension, + + }; + struct kernelLoaderPacket + { + struct acpi_hid + { + char id[8]; + operator char*() + { return id; } + }; + // Bit 0: PNP + // Bit 1: Whether the specified ACPI table exists + uint32_t howToIdentify = 0; + // PNP + struct __acpi_pnp + { + static constexpr size_t maxHIDs = 4; + static constexpr size_t maxCIDs = 16; + acpi_hid hid[maxHIDs] = {}; + acpi_hid cid[maxCIDs] = {}; + size_t nHIDs = 0; + size_t nCIDs = 0; + } acpi_pnp; + // Whether the specified ACPI table exists + union + { + char signature_str[4]; + uint32_t signature_int; + } acpi_table; + }; + // For the kernel to recognize the file as a file, some conditions must be met: + // A driver header exists in the section specified by the macro OBOS_DRIVER_HEADER_SECTION, with the magic field set to g_driverHeaderMagic. The section must be at least RW. + // The driver header must have all fields valid. + // The driver must have its symbols exported. + // The driver must be a dynamic elf file, matching the ABI's specifications for ELF files. + // Example: + // The x86_64 SystemV ABI specifies that ELF files must have e_ident[EI_CLASS] = ELFCLASS64, e_ident[EI_DATA] = ELFDATA2LSB, and e_machine = EM_X86_64 (62) + // If you are unsure on this, run readelf -h on the ELF file to verify the values are what the kernel expects. + // Make sure the driver is compiled with the same ABI as the kernel, or the kernel won't be able to call the driver. + // For example, the driver can't be using the Microsoft ABI when the kernel uses the SystemV ABI. + struct driverHeader + { + uint64_t magic = g_driverHeaderMagic; + driverType type = driverType::Invalid; + char friendlyName[33] = {}; + // The path of driver to detect whether this driver should be loaded. + // The "loader" driver. + // This can be the kernel path. + // If this is empty, this driver must be loaded manually. + char requestedLoader[257] = {}; + void* loaderPacket; // The data expected by the loader driver to know whether this driver should be loaded or not. + + const char* path = nullptr; // Initialized by the kernel. + const char* loader = nullptr; // Initialized by the kernel. + uint32_t id; // Initialized by the kernel. + }; + } +} \ No newline at end of file diff --git a/src/oboskrnl/driver_interface/loader.c b/src/oboskrnl/driver_interface/loader.c deleted file mode 100644 index bea44737..00000000 --- a/src/oboskrnl/driver_interface/loader.c +++ /dev/null @@ -1,514 +0,0 @@ -/* - * oboskrnl/driver_interface/loader.c - * - * Copyright (c) 2024-2025 Omar Berrow -*/ - -#include -#include -#include -#include -#include -#include - -#include - -#include -#include -#include -#include -#include - -#include - -#include -#include -#include - -#include -#include -#include - -#include -#include - -#include - -// Do it in two passes so that any macros can be expanded -#define token_concat_impl(tok1, tok2) tok1 ##tok2 -#define token_concat(tok1, tok2) token_concat_impl(tok1, tok2) -#define GetCurrentElfClass() token_concat(ELFCLASS, OBOS_ARCHITECTURE_BITS) - -symbol_table OBOS_KernelSymbolTable; -driver_list Drv_LoadedDrivers; -driver_list Drv_LoadedFsDrivers; -static int cmp_symbols(driver_symbol* left, driver_symbol* right) -{ - return strcmp_std(left->name, right->name); -} -RB_GENERATE(symbol_table, driver_symbol, rb_entry, cmp_symbols); - -#define OffsetPtr(ptr, off, type) ((type)(((uintptr_t)ptr) + ((intptr_t)off))) -// Please forgive me for this -#define Cast(what, to) ((to)what) - -static uint32_t nextDriverId; -static OBOS_NO_UBSAN OBOS_NO_KASAN driver_header* find_header(void* file, size_t szFile) -{ - for (driver_header* curr = Cast(file, driver_header*); - Cast(curr, uintptr_t) < (Cast(file, uintptr_t) + szFile); - curr = OffsetPtr(curr, 0x8, driver_header*)) - { - if (curr->magic == OBOS_DRIVER_MAGIC) - return curr; - } - return nullptr; -} -static size_t get_header_size(driver_header* header) -{ - size_t sizeof_header = 0; - if (~header->flags & DRIVER_HEADER_HAS_VERSION_FIELD) - sizeof_header = sizeof(*header)-0x100; // An old-style driver. - else - { - switch (header->version) - { - case CURRENT_DRIVER_HEADER_VERSION: - sizeof_header = sizeof(*header); - break; - default: - return SIZE_MAX; - } - } - return sizeof_header; -} -OBOS_NO_UBSAN obos_status Drv_LoadDriverHeader(const void* file_, size_t szFile, driver_header* header) -{ - if (!header) - return OBOS_STATUS_INVALID_ARGUMENT; - if (szFile < sizeof(Elf_Ehdr) || !file_) - return OBOS_STATUS_INVALID_FILE; - - uint8_t* file = (uint8_t*)file_; - Elf_Ehdr* ehdr = Cast(file, Elf_Ehdr*); - if (ehdr->e_ident[0] != ELFMAG0 || ehdr->e_ident[1] != ELFMAG1 || - ehdr->e_ident[2] != ELFMAG2 || ehdr->e_ident[3] != ELFMAG3 - ) - return OBOS_STATUS_INVALID_FILE; - if (ehdr->e_ident[EI_CLASS] != GetCurrentElfClass()) - return OBOS_STATUS_INVALID_FILE; - uint8_t ei_data_val = 0; - if (strcmp(OBOS_ARCHITECTURE_ENDIANNESS, "Little-Endian")) - ei_data_val = ELFDATA2LSB; - else if (strcmp(OBOS_ARCHITECTURE_ENDIANNESS, "Big-Endian")) - ei_data_val = ELFDATA2MSB; - else - ei_data_val = ELFDATANONE; - if (ehdr->e_ident[EI_DATA] != ei_data_val) - return OBOS_STATUS_INVALID_FILE; - if (ehdr->e_machine != EM_CURRENT) - return OBOS_STATUS_INVALID_FILE; - if (ehdr->e_type != ET_DYN) - return OBOS_STATUS_INVALID_FILE; - // First, search for the section containing the driver header. - Elf_Shdr* driverHeaderSection = nullptr; - { - Elf_Shdr* sectionTable = OffsetPtr(ehdr, ehdr->e_shoff, Elf_Shdr*); - if (!sectionTable) - { - // No section table! - // Skip. - goto noSectionTable; - } - const char* shstr_table = (const char*)(((uintptr_t)file) + (sectionTable + ehdr->e_shstrndx)->sh_offset); - for (size_t i = 0; i < ehdr->e_shnum; i++) - { - const char* section = shstr_table + sectionTable[i].sh_name; - if (strcmp(section, OBOS_DRIVER_HEADER_SECTION)) - driverHeaderSection = §ionTable[i]; - if (driverHeaderSection) - break; - } - } - noSectionTable: - (void)0; - driver_header* header_ = nullptr; - if (!driverHeaderSection) - goto manualSearch; - header_ = OffsetPtr(ehdr, driverHeaderSection->sh_offset, driver_header*); - manualSearch: - if (!header_) - header_ = find_header(file, szFile); - if (!header_) - return OBOS_STATUS_NOT_FOUND; - // We've found the header. - // Verify its contents. - if (header_->magic != OBOS_DRIVER_MAGIC) - return OBOS_STATUS_INVALID_HEADER; - size_t sizeof_header = get_header_size(header_); - if (sizeof_header == SIZE_MAX) - return OBOS_STATUS_INVALID_HEADER; - memcpy(header, header_, sizeof_header); - memzero((void*)((uintptr_t)header + sizeof_header), sizeof(*header) - sizeof_header); - return OBOS_STATUS_SUCCESS; -} -OBOS_NO_UBSAN driver_id *Drv_LoadDriver(const void* file_, size_t szFile, obos_status* status) -{ - driver_header* header; - driver_header header_; - obos_status st = Drv_LoadDriverHeader(file_, szFile, &header_); - if (obos_is_error(st)) - { - if (status) - *status = st; - return nullptr; - } - for (driver_node* curr = Drv_LoadedDrivers.head; curr; ) - { - if (strncmp(header_.driverName, curr->data->header.driverName, 64)) - { - OBOS_Error("%s: Refusing to load an already loaded driver.\n", __func__); - if (status) *status = OBOS_STATUS_ALREADY_INITIALIZED; - return nullptr; - } - curr = curr->next; - } - driver_id* driver = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(driver_id), nullptr); - Elf_Sym* dynamicSymbolTable = nullptr; - size_t nEntriesDynamicSymbolTable = 0; - const char* dynstrtab = nullptr; - void* top = nullptr; - // Temporarily do this. - driver->header.flags = header_.flags; - if (header_.flags & DRIVER_HEADER_HAS_VERSION_FIELD && header_.version >= 1) - driver->header.uacpi_init_level_required = header_.uacpi_init_level_required; - driver->base = DrvS_LoadRelocatableElf(driver, file_, szFile, &dynamicSymbolTable, &nEntriesDynamicSymbolTable, &dynstrtab, &top, status); - if (!driver->base) - { - Free(OBOS_KernelAllocator, driver, sizeof(*driver)); - return nullptr; - } - Elf_Shdr* driverHeaderSection = nullptr; - uint8_t* file = (uint8_t*)file_; - Elf_Ehdr* ehdr = Cast(file, Elf_Ehdr*); - { - Elf_Shdr* sectionTable = OffsetPtr(ehdr, ehdr->e_shoff, Elf_Shdr*); - if (!sectionTable) - { - // No section table! - // Skip. - goto noSectionTable; - } - const char* shstr_table = (const char*)(((uintptr_t)file) + (sectionTable + ehdr->e_shstrndx)->sh_offset); - for (size_t i = 0; i < ehdr->e_shnum; i++) - { - const char* section = shstr_table + sectionTable[i].sh_name; - if (strcmp(section, OBOS_DRIVER_HEADER_SECTION)) - driverHeaderSection = §ionTable[i]; - if (driverHeaderSection) - break; - } - } - noSectionTable: - driver->top = top; - driver->id = nextDriverId++; - driver->refCnt++; - // Find the header within memory. - { - if (driverHeaderSection) - header = OffsetPtr(driver->base, driverHeaderSection->sh_addr, driver_header*); - else - { - header = find_header(driver->base, (uintptr_t)driver->top - (uintptr_t)driver->base); - OBOS_ASSERT(header); // If this fails, something scuffed has happened. - } - } - //driver->header = *header; - size_t sizeof_header = get_header_size(header); - memcpy(&driver->header, header, sizeof_header); - memzero((void*)((uintptr_t)&driver->header + sizeof_header), sizeof(*header) - sizeof_header); - if (!(header->flags & DRIVER_HEADER_FLAGS_NO_ENTRY)) - driver->entryAddr = OffsetPtr(driver->base, ehdr->e_entry, uintptr_t); - for (size_t i = 0; i < nEntriesDynamicSymbolTable; i++) - { - Elf_Sym* esymbol = &dynamicSymbolTable[i]; - if (esymbol->st_shndx == 0) - continue; // Undefined symbol, do not add to the symbol list. - const char* forced_hidden_symbols[] = { - "OBOS_DriverEntry", - "Drv_Base", - "Drv_Top", - "Drv_Header", - }; - int symbolType = -1; - switch (ELF_ST_TYPE(esymbol->st_info)) - { - case STT_FUNC: - symbolType = SYMBOL_TYPE_FUNCTION; - break; - case STT_FILE: - symbolType = SYMBOL_TYPE_FILE; - break; - default: - symbolType = SYMBOL_TYPE_VARIABLE; - break; - } - driver_symbol* symbol = ZeroAllocate(OBOS_NonPagedPoolAllocator, 1, sizeof(driver_symbol), nullptr); - const char* name = dynstrtab + esymbol->st_name; - bool forceHidden = false; - for (size_t j = 0; j < sizeof(forced_hidden_symbols)/sizeof(*forced_hidden_symbols); j++) - { - if (strcmp(forced_hidden_symbols[j], name)) - { - forceHidden = true; - break; - } - } - size_t szName = strlen(name); - symbol->name = memcpy(ZeroAllocate(OBOS_NonPagedPoolAllocator, 1, szName + 1, nullptr), name, szName); - symbol->address = OffsetPtr(driver->base, esymbol->st_value, uintptr_t); - symbol->size = esymbol->st_size; - symbol->type = symbolType; - if (forceHidden) - { - symbol->visibility = SYMBOL_VISIBILITY_HIDDEN; - continue; - } - switch (esymbol->st_other) - { - case STV_DEFAULT: - case STV_EXPORTED: - // since this is the kernel, everyone already gets the same object - case STV_SINGLETON: - symbol->visibility = SYMBOL_VISIBILITY_DEFAULT; - break; - case STV_PROTECTED: - case STV_HIDDEN: - symbol->visibility = SYMBOL_VISIBILITY_HIDDEN; - break; - default: - OBOS_Debug("%s: Unrecognized visibility %d. Assuming hidden.\n", __func__, esymbol->st_other); - symbol->visibility = SYMBOL_VISIBILITY_HIDDEN; - break; - } - RB_INSERT(symbol_table, &driver->symbols, symbol); - } - driver->node.data = driver; - APPEND_DRIVER_NODE(Drv_LoadedDrivers, &driver->node); - driver->other_node.data = driver; - if (driver->header.ftable.probe) - APPEND_DRIVER_NODE(Drv_LoadedFsDrivers, &driver->other_node); // pretty high chance it is a fs driver - if (strlen(driver->header.driverName)) - OBOS_Log("%s: Loaded driver '%s' at 0x%p.\n", __func__, driver->header.driverName, driver->base); - else - OBOS_Log("%s: Loaded driver at 0x%p.\n", __func__, driver->header.driverName, driver->base); - driver->refCnt++; - return driver; -} - -typedef driver_init_status(*driver_entry)(driver_id* id); -__attribute__((no_instrument_function)) static void driver_trampoline(driver_id* id) -{ - OBOS_Debug("calling driver entry %p\n", id->entryAddr); - driver_init_status status = ((driver_entry)id->entryAddr)(id); - Drv_ExitDriver(id, &status); -} - -obos_status Drv_StartDriver(driver_id* driver, thread** mainThread) -{ - if (mainThread) - *mainThread = nullptr; - if (!driver) - return OBOS_STATUS_INVALID_ARGUMENT; - if (driver->header.flags & DRIVER_HEADER_FLAGS_NO_ENTRY) - return OBOS_STATUS_NO_ENTRY_POINT; - if (driver->started) - return OBOS_STATUS_ALREADY_INITIALIZED; - obos_status status = OBOS_STATUS_SUCCESS; - thread* thr = CoreH_ThreadAllocate(&status); - if (obos_is_error(status)) - return status; - thread_ctx ctx; - memzero(&ctx, sizeof(ctx)); - size_t stackSize = 0; - if (driver->header.flags & DRIVER_HEADER_FLAGS_REQUEST_STACK_SIZE) - stackSize = driver->header.stackSize; - if (!stackSize) - stackSize = 0x20000; - void* stack = Mm_VirtualMemoryAlloc(&Mm_KernelContext, nullptr, stackSize, 0, VMA_FLAGS_KERNEL_STACK, nullptr, &status); - status = CoreS_SetupThreadContext(&ctx, - (uintptr_t)driver_trampoline, - (uintptr_t)driver, - false, - stack, - stackSize); - if (obos_is_error(status)) - { - Mm_VirtualMemoryFree(&Mm_KernelContext, stack, stackSize); - return status; - } - status = CoreH_ThreadInitialize(thr, THREAD_PRIORITY_HIGH, !driver->header.mainThreadAffinity ? Core_DefaultThreadAffinity : driver->header.mainThreadAffinity, &ctx); - if (obos_is_error(status)) - { - Mm_VirtualMemoryFree(&Mm_KernelContext, stack, stackSize); - return status; - } - Core_ProcessAppendThread(OBOS_KernelProcess, thr); - thr->stackFree = CoreH_VMAStackFree; - thr->stackFreeUserdata = &Mm_KernelContext; - if (mainThread) - { - thr->references++; - *mainThread = thr; - } - driver->main_thread = thr; - thr->references++; - driver->started = true; - CoreH_ThreadReady(thr); // very low chance of failure. - return OBOS_STATUS_SUCCESS; -} - -obos_status Drv_UnloadDriver(driver_id* driver) -{ - // The kernel don't want the driver no more. - --driver->refCnt; - if (driver->refCnt != 1) - OBOS_Warning("Driver not unloaded because refcount=%ld\n", driver->refCnt); - return Drv_UnrefDriver(driver); -} - -obos_status Drv_UnrefDriver(driver_id* driver) -{ - if (!driver) - return OBOS_STATUS_INVALID_ARGUMENT; - if ((--driver->refCnt) > 0) - return OBOS_STATUS_SUCCESS; - if (driver->main_thread) - { - while (!(driver->main_thread->flags & THREAD_FLAGS_DIED)) - Core_Yield(); - if (!(--driver->main_thread->references) && driver->main_thread->free) - driver->main_thread->free(driver->main_thread); - driver->main_thread = 0; - } - if (driver->header.ftable.driver_cleanup_callback) - driver->header.ftable.driver_cleanup_callback(); - else - OBOS_Error("%s: memory leak! driver_cleanup_callback is nullptr!\n", __func__); - for (driver_node* node = driver->dependencies.head; node; ) - { - // node->data->refCnt--; - Drv_UnrefDriver(node->data); - - node = node->next; - } - REMOVE_DRIVER_NODE(Drv_LoadedDrivers, &driver->node); - size_t size = ((uintptr_t)driver->top-(uintptr_t)driver->base); - if (size % OBOS_PAGE_SIZE) - size += (OBOS_PAGE_SIZE-(size%OBOS_PAGE_SIZE)); - // Mm_VirtualMemoryFree(&Mm_KernelContext, driver->base, size); - Free(OBOS_KernelAllocator, driver, sizeof(*driver)); - return OBOS_STATUS_SUCCESS; -} - -driver_symbol* DrvH_ResolveSymbol(const char* name, struct driver_id** driver) -{ - OBOS_ASSERT(driver); - OBOS_ASSERT(name); - OBOS_ASSERT(strlen(name)); - // Search the kernel symbol table; - driver_symbol what = { .name = name }; - driver_symbol* sym = RB_FIND(symbol_table, &OBOS_KernelSymbolTable, &what); - if (sym) - { - *driver = nullptr; // the kernel - return sym; - } - for (driver_node* node = Drv_LoadedDrivers.head; node; ) - { - driver_id* drv = node->data; - OBOS_ASSERT(drv); - sym = RB_FIND(symbol_table, &drv->symbols, &what); - if (sym) - { - *driver = drv; // the current driver. - return sym; - } - - node = node->next; - } - *driver = nullptr; - return nullptr; // symbol unresolved. -} - -static __attribute__((no_instrument_function)) void unload_driver_dpc(dpc* unused, void* userdata) -{ - Drv_UnloadDriver(userdata); - CoreH_FreeDPC(unused, true); -} - -void Drv_ExitDriver(struct driver_id* id, const driver_init_status* status) -{ - if (!id || !status) - return; - if (id->main_thread != Core_GetCurrentThread()) - return; - // NOTE: It is impossible for this to be zero after this, because - // the main thread is referenced by the scheduler. - id->main_thread->references--; - id->main_thread = nullptr; - if (obos_is_error(status->status)) - { - if (OBOS_GetLogLevel() <= LOG_LEVEL_WARNING) - { - OBOS_Warning("Initialization of driver %d (%s) failed with status %d.\n", - id->id, - strnlen(id->header.driverName, 64) ? id->header.driverName : "Unknown", - status->status - ); - if (status->context) - printf("Note: %s\n", status->context); - if (status->fatal) - printf("Note: Fatal error. Unloading the driver.\n"); - } - } - if (!status->fatal || obos_is_success(status->status)) - Core_ExitCurrentThread(); - dpc* dpc = CoreH_AllocateDPC(nullptr); - irql oldIrql = Core_RaiseIrql(IRQL_DISPATCH); - OBOS_ASSERT(dpc); - dpc->userdata = id; - CoreH_InitializeDPC(dpc, unload_driver_dpc, CoreH_CPUIdToAffinity(CoreS_GetCPULocalPtr()->id) /* make sure this runs on this CPU. */); - Core_ExitCurrentThread(); - OBOS_UNREACHABLE; - OBOS_UNUSED(oldIrql); -} - -driver_symbol* DrvH_ResolveSymbolReverse(uintptr_t addr, struct driver_id** driver) -{ - OBOS_ASSERT(driver); - if (addr < OBOS_KERNEL_ADDRESS_SPACE_BASE) - goto unresolved; - *driver = nullptr; - driver_symbol* curr = nullptr; - RB_FOREACH(curr, symbol_table, &OBOS_KernelSymbolTable) - { - if (addr >= curr->address && addr < (curr->address+curr->size)) - return curr; - } - for (driver_node* node = Drv_LoadedDrivers.head; node; ) - { - driver_id* const drv = node->data; - node = node->next; - RB_FOREACH(curr, symbol_table, &drv->symbols) - { - if (addr >= curr->address && addr < (curr->address+curr->size)) - { - *driver = drv; - return curr; - } - } - } - unresolved: - return nullptr; -} diff --git a/src/oboskrnl/driver_interface/loader.h b/src/oboskrnl/driver_interface/loader.h index 8d252779..96e1c589 100644 --- a/src/oboskrnl/driver_interface/loader.h +++ b/src/oboskrnl/driver_interface/loader.h @@ -1,32 +1,43 @@ /* - * oboskrnl/driver_interface/loader.h - * - * Copyright (c) 2024 Omar Berrow + oboskrnl/driver_interface/loader.h + + Copyright (c) 2024 Omar Berrow */ #pragma once #include -#include - -#include -#include - -#include - -#include - -// gets the driver header of an unloaded driver -obos_status Drv_LoadDriverHeader(const void* file, size_t szFile, driver_header* header); -driver_id *Drv_LoadDriver(const void* file, size_t szFile, obos_status* status); -obos_status Drv_StartDriver(driver_id* driver, thread** mainThread); -obos_status Drv_UnloadDriver(driver_id* driver); -obos_status Drv_UnrefDriver(driver_id* driver); - -// returns the base of the elf. -// if this resolves a symbol from a driver while relocating, then it must add the driver to the dependency list. -OBOS_WEAK void* DrvS_LoadRelocatableElf(driver_id* driver, const void* file, size_t szFile, Elf_Sym** dynamicSymbolTable, size_t* nEntriesDynamicSymbolTable, const char** dynstrtab, void** top, obos_status* status); - -// if the value set at *driver is nullptr, then the symbol is from the kernel -driver_symbol* DrvH_ResolveSymbol(const char* name, struct driver_id** driver); -driver_symbol* DrvH_ResolveSymbolReverse(uintptr_t addr, struct driver_id** driver); \ No newline at end of file +#include + +namespace obos +{ + namespace driverInterface + { + /// + /// Verifies a file that claims to be a driver. + /// + /// The file's data. + /// The file size. + /// The driver header, or nullptr on failure. This object's lifetime is determined by the lifetime of the 'data' parameter. + OBOS_EXPORT const struct driverHeader* VerifyDriver(const void* data, size_t size); + /// + /// Loads a driver into memory. + /// + /// The file's data. + /// The file size. + /// The driver identification, or nullptr on failure. This object's lifetime is controlled by the kernel. + OBOS_EXPORT struct driverId* LoadDriver(const void* data, size_t size); + /// + /// Starts a driver by sending it to it's entry point. + /// + /// The driver's id. + /// Whether starting the driver succeeded (true) or not (false). + OBOS_EXPORT bool StartDriver(uint32_t id); + /// + /// Unloads a driver from memory. + /// + /// The driver's id. + /// Whether unloading the driver succeeded (true) or not (false). + OBOS_EXPORT bool UnloadDriver(uint32_t id); + } +} \ No newline at end of file diff --git a/src/oboskrnl/driver_interface/loader64.cpp b/src/oboskrnl/driver_interface/loader64.cpp new file mode 100644 index 00000000..ac7c4428 --- /dev/null +++ b/src/oboskrnl/driver_interface/loader64.cpp @@ -0,0 +1,291 @@ +/* + oboskrnl/driver_interface/loader.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include + +#include +#include +#include + +#include + +#include +#include + +#include +#include +#include + +#include + +#if OBOS_ARCHITECTURE_BITS != 64 +#error Cannot compile loader64.cpp when OBOS_ARCHITECTURE_BITS != 64. +#endif + +namespace obos +{ + namespace driverInterface + { + using namespace elf; + driverIdList g_driverTable[(int)driverType::MaxValue]; + driverIdList g_drivers; + uint32_t driverId::nextDriverId; +#define Ehdr(ptr) ((const Elf64_Ehdr*)ptr) +#define OffsetPtr(ptr, off) ((decltype(ptr))(((char*)(ptr)) + (off))) +#define OffsetOf(type, member) ((uintptr_t)&(((type*)nullptr)->member)) + static const char* getElfString(const Elf64_Ehdr* elfHeader, uintptr_t index) + { + const char* startAddress = reinterpret_cast(elfHeader); + Elf64_Shdr* stringTable = reinterpret_cast(const_cast(startAddress) + elfHeader->e_shoff); + stringTable += elfHeader->e_shstrndx; + return startAddress + stringTable->sh_offset + index; + } + const driverHeader* findDriverHeader(const void* _data, size_t) + { + const Elf64_Ehdr* ehdr = Ehdr(_data); + const Elf64_Shdr* sectionHeaders = OffsetPtr((const Elf64_Shdr*)_data, ehdr->e_shoff); + const Elf64_Shdr* driverHeaderSection = nullptr; + for (size_t i = 0; i < ehdr->e_shnum; i++) + { + if (strcmp(getElfString(ehdr, sectionHeaders[i].sh_name), OBOS_DRIVER_HEADER_SECTION)) + { + driverHeaderSection = §ionHeaders[i]; + break; + } + } + if (driverHeaderSection) + return OffsetPtr((const driverHeader*)_data, driverHeaderSection->sh_offset); + return nullptr; + } + const Elf64_Shdr* findDriverHeaderSection(const void* _data, size_t) + { + const Elf64_Ehdr* ehdr = Ehdr(_data); + const Elf64_Shdr* sectionHeaders = OffsetPtr((const Elf64_Shdr*)_data, ehdr->e_shoff); + for (size_t i = 0; i < ehdr->e_shnum; i++) + if (strcmp(getElfString(ehdr, sectionHeaders[i].sh_name), OBOS_DRIVER_HEADER_SECTION)) + return §ionHeaders[i]; + return nullptr; + } + struct tables + { + const Elf64_Shdr* symtab_section; + const Elf64_Shdr* strtab_section; + }; + static tables GetDriverSymbolStringTables(const uint8_t* file) + { + const Elf64_Ehdr* eheader = (Elf64_Ehdr*)file; + const Elf64_Shdr* iter = reinterpret_cast(file + eheader->e_shoff); + const Elf64_Shdr* symtab_section = nullptr; + const Elf64_Shdr* strtab_section = nullptr; + + for (size_t i = 0; i < eheader->e_shnum; i++, iter++) + { + const char* section_name = getElfString(eheader, iter->sh_name); + if (strcmp(".symtab", section_name)) + { + symtab_section = iter; + continue; + } + if (strcmp(".strtab", section_name)) + { + strtab_section = iter; + continue; + } + if (strtab_section != nullptr && symtab_section != nullptr) + break; + } + if (!symtab_section) + return { nullptr,nullptr }; + if (!strtab_section) + return { nullptr,nullptr }; + return { symtab_section,strtab_section }; + } + const struct driverHeader* VerifyDriver(const void* data, size_t size) + { + if (size < sizeof(Elf64_Ehdr)) + return nullptr; + if (!data) + return nullptr; + // Verify the ELF header. + const Elf64_Ehdr* header = Ehdr(data); + if (header->e_ident[EI_MAG0] != ELFMAG0 || + header->e_ident[EI_MAG1] != ELFMAG1 || + header->e_ident[EI_MAG2] != ELFMAG2 || + header->e_ident[EI_MAG3] != ELFMAG3) + return nullptr; + if (header->e_ident[EI_CLASS] != ELFCLASS64) + return nullptr; + uint8_t ei_data_val = 0; + if (arch::g_endianness == arch::endianness::LittleEndian) + ei_data_val = ELFDATA2LSB; + else if (arch::g_endianness == arch::endianness::BigEndian) + ei_data_val = ELFDATA2MSB; + else + ei_data_val = ELFDATANONE; + if (header->e_ident[EI_DATA] != ei_data_val) + return nullptr; +#ifdef __x86_64__ + if (header->e_machine != EM_X86_64) + return nullptr; +#else +#error TODO: Add current architecture +#endif + if (header->e_type != ET_DYN) + return nullptr; + // Verify the driver has exported symbols. + tables driver_tables = GetDriverSymbolStringTables((uint8_t*)data); + if (!driver_tables.strtab_section || !driver_tables.symtab_section) + return nullptr; + // Verify the driver header. + auto dheader_section = findDriverHeaderSection(data, size); + if (!dheader_section) + return nullptr; + if (!(dheader_section->sh_flags & 1 /*SHF_WRITE*/)) + return nullptr; + auto dheader = OffsetPtr((driverHeader*)data, dheader_section->sh_offset); + if (dheader->magic != g_driverHeaderMagic) + return nullptr; + if (dheader->type <= driverType::Invalid || dheader->type > driverType::MaxValue) + return nullptr; + // Nothing incorrect has been detected for the driver's file. + return dheader; + } + + driverId* LoadDriver(const void* data, size_t size) + { + const driverHeader* header = VerifyDriver(data, size); + if (!header) + return nullptr; + // Hand the loading part to the architecture-specific code. + void *base = arch::LoadDynamicElfFile(data, size); + // Initialize the driver id. + driverId* driver = new driverId{}; + // Initialize the driver's symbol table. + tables driver_tables = GetDriverSymbolStringTables((uint8_t*)data); + const Elf64_Sym* symbolTable = OffsetPtr((const Elf64_Sym*)data, driver_tables.symtab_section->sh_offset); + size_t szSymbolTable = driver_tables.symtab_section->sh_size; + const char* stringTable = OffsetPtr((const char*)data, driver_tables.strtab_section->sh_offset); + size_t countEntries = szSymbolTable / sizeof(Elf64_Sym); + for (size_t i = 0; i < countEntries; i++) + { + auto& symbol = symbolTable[i]; + if (symbol.st_name >= driver_tables.strtab_section->sh_size) + continue; + if ((symbol.st_info >> 4) != STB_GLOBAL) + continue; + auto stt = (symbol.st_info & 0xf); + if (stt != STT_FUNC && stt != STT_OBJECT) + continue; + driverSymbol sym{}; + sym.address = (uintptr_t)base + symbol.st_value; + sym.name = OffsetPtr(stringTable, symbol.st_name); + sym.type = stt == STT_FUNC ? driverSymbol::SYMBOL_FUNC : driverSymbol::SYMBOL_VARIABLE; + driver->symbols.push_back(sym); + } + auto dheader_section = findDriverHeaderSection(data, size); + driverHeader* rwHeader = OffsetPtr((driverHeader*)base, dheader_section->sh_addr); + // Initialize the miscellaneous fields. + driver->header = rwHeader; + driver->id = driverId::nextDriverId++; + driver->driverBaseAddress = base; + driver->driverEntry = OffsetPtr((void(*)())base, Ehdr(data)->e_entry); + // Initialize the driver's header. + rwHeader->id = driver->id; + // We've done what we need to, append the driver to its appropriate lists. + g_drivers.Append(driver); + g_driverTable[(int)driver->header->type - 1].Append(driver); + return driver; + } + bool StartDriver(uint32_t id) + { + driverIdNode* node = g_drivers.Find(id); + if (!node) + return false; + driverId* driver = node->data; + scheduler::Thread* thr = new scheduler::Thread{}; + thr->tid = 0; + thr->referenceCount = 0; + + thr->priority = scheduler::ThreadPriority::High; + thr->ogAffinity = scheduler::g_defaultAffinity; + + thr->affinity = thr->ogAffinity; + thr->status = scheduler::ThreadStatus::CanRun; + + thr->addressSpace = &vmm::g_kernelContext; + arch::SetupThreadContext(&thr->context, &thr->thread_stack, (uintptr_t)driver->driverEntry, (uintptr_t)driver, false, 0x1'0000, thr->addressSpace); + scheduler::g_threadPriorities[(int)thr->priority].Append(thr); + scheduler::yield(); + return true; + } + bool UnloadDriver(uint32_t) + { + return false; + } +#define removeNode(head,tail,nNodes,node) \ +do {\ + if (node)\ + {\ + if (node->prev)\ + node->prev->next = node->next;\ + if (node->next)\ + node->next->prev = node->prev;\ + if (head == node)\ + head = node;\ + if (tail == node)\ + tail = node;\ + nNodes--;\ + }\ +} while (0) + void driverIdList::Append(driverId* id) + { + if (!id) + return; + driverIdNode* node = new driverIdNode{ nullptr,nullptr,id }; + if (tail) + tail->next = node; + if (!head) + head = node; + node->prev = tail; + tail = node; + nNodes++; + } + void driverIdList::Remove(driverId* id) + { + if (!id) + return; + driverIdNode* node = Find(id); + removeNode(head,tail,nNodes, node); + } + void driverIdList::Remove(uint32_t id) + { + if (!id) + return; + driverIdNode* node = Find(id); + removeNode(head, tail, nNodes, node); + } + driverIdNode* driverIdList::Find(driverId* id) + { + for (driverIdNode* node = head; node; ) + { + if (node->data == id) + return node; + node = node->next; + } + return nullptr; + } + driverIdNode* driverIdList::Find(uint32_t id) + { + for (driverIdNode* node = head; node; ) + { + if (node->data->id == id) + return node; + node = node->next; + } + return nullptr; + } + } +} \ No newline at end of file diff --git a/src/oboskrnl/driver_interface/pci.c b/src/oboskrnl/driver_interface/pci.c deleted file mode 100644 index b2b74d0d..00000000 --- a/src/oboskrnl/driver_interface/pci.c +++ /dev/null @@ -1,646 +0,0 @@ -/* - * oboskrnl/driver_interface/pci.c - * - * Copyright (c) 2024-2025 Omar Berrow - */ - -#include -#include -#include -#include -#include - -#include - -#include - -#include - -#include - -#include - -#if OBOS_ARCHITECTURE_HAS_ACPI -#include -#include -#include -#include -#endif - -#include - -#if OBOS_ARCHITECTURE_HAS_PCI - -LIST_GENERATE_INTERNAL(pci_device_list, pci_device, node, OBOS_EXPORT); -LIST_GENERATE_INTERNAL(pci_resource_list, pci_resource, node, OBOS_EXPORT); - -pci_bus Drv_PCIBuses[256]; -size_t Drv_PCIBusCount; - -static void update_bar(pci_device* dev, pci_resource* resource) -{ - uint8_t bar = resource->bar->idx; - - uint64_t tmp = 0; - DrvS_ReadPCIRegister(dev->location, 0x10+bar*4, 4, &tmp); - if (!tmp) - return; - - // for (volatile bool b = true; b; ) - // asm volatile ("" : :"r"(b) :"memory"); - - resource->type = PCI_RESOURCE_BAR; - resource->owner = dev; - - uint8_t bar_flags = tmp & 0b111; - if (bar_flags & 0b1) - bar_flags &= ~BIT(2); // Only bits 0-1 of an IO space bar are reserved, so to prevent this from breaking, clear bit 2 - // OBOS_Debug("updating bar %d address\n", bar); - switch (bar_flags) { - case PCI_BAR_MASK_IOSPACE: - resource->bar->type = PCI_BARIO; - resource->bar->phys = tmp & ~0x3; - break; - case PCI_BAR_MASK_32BIT: - resource->bar->type = PCI_BAR32; - resource->bar->prefetchable = tmp & (1<<3); - resource->bar->phys = tmp & ~0xf; - break; - case PCI_BAR_MASK_64BIT: - resource->bar->type = PCI_BAR64; - resource->bar->prefetchable = tmp & (1<<3); - resource->bar->phys = tmp & ~0xf; - DrvS_ReadPCIRegister(dev->location, 0x10+(bar+1)*4, 4, &tmp); - resource->bar->phys |= (tmp<<32); - break; - default: return; - } - - // Read the size. - // NOTE: We need to make sure IO Space and memory decode is off before doing this. - - irql oldIrql = Core_RaiseIrql(IRQL_MASKED); - - uint64_t cmd_register = 0; - DrvS_ReadPCIRegister(dev->location, 0x4, 4, &cmd_register); - volatile uint64_t old_cmd_register = cmd_register; - cmd_register &= ~0b11; - DrvS_WritePCIRegister(dev->location, 0x4, 4, cmd_register); - - switch (bar_flags) - { - case PCI_BAR_MASK_IOSPACE: - case PCI_BAR_MASK_32BIT: - { - // Write all ones to the BAR. - DrvS_WritePCIRegister(dev->location, 0x10+bar*4, 4, 0xffffffff); - - // Decode size. - uint32_t sz = 0; - uint64_t tmp2 = 0; - DrvS_ReadPCIRegister(dev->location, 0x10+bar*4, 4, &tmp2); - sz = (uint32_t)tmp2; - sz = (~sz & 0xfffffff0); - resource->bar->size = sz; - break; - } - case PCI_BAR_MASK_64BIT: - { - // Write all ones to the BAR. - DrvS_WritePCIRegister(dev->location, 0x10+bar*4, 4, 0xffffffff); - DrvS_WritePCIRegister(dev->location, 0x10+(bar+1)*4, 4, 0xffffffff); - - // Decode size. - uint64_t sz = 0; - uint64_t tmp2 = 0; - DrvS_ReadPCIRegister(dev->location, 0x10+bar*4, 4, &tmp2); - sz = (uint32_t)tmp2; - DrvS_ReadPCIRegister(dev->location, 0x10+(bar+1)*4, 4, &tmp2); - sz |= (tmp2<<32); - sz = (~sz & 0xfffffffffffffff0) + 1; - resource->bar->size = sz; - - DrvS_WritePCIRegister(dev->location, 0x10+(bar+1)*4, 4, resource->bar->phys >> 32); - break; - } - } - - Core_LowerIrql(oldIrql); - - // Write back old values. - DrvS_WritePCIRegister(dev->location, 0x10+bar*4, 4, resource->bar->phys & 0xffffffff); - DrvS_WritePCIRegister(dev->location, 0x4, 4, old_cmd_register); -} - -static void initialize_bar_resources(pci_device* dev) -{ - for (uint8_t bar = 0; bar < 6; bar++) - { - uint64_t tmp = 0; - DrvS_ReadPCIRegister(dev->location, 0x10+bar*4, 4, &tmp); - if (!tmp) - continue; // No bar here :( - - pci_resource* resource = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(pci_resource), nullptr); - - resource->owner = dev; - - resource->bar = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(pci_bar), nullptr); - resource->bar->idx = bar; - - update_bar(dev, resource); - - const char* bar_type_str = nullptr; - switch (resource->bar->type) { - case PCI_BAR32: - if (resource->bar->prefetchable) - bar_type_str = "32-bit, prefetchable"; - else - bar_type_str = "32-bit"; - break; - case PCI_BAR64: - if (resource->bar->prefetchable) - bar_type_str = "64-bit, prefetchable"; - else - bar_type_str = "64-bit"; - break; - case PCI_BARIO: - bar_type_str = "I/O"; - break; - default: break; - } - - OBOS_Debug("PCI: %02x:%02x:%02x: Initialized %s BAR (BAR %d). BAR ranges from 0x%p-0x%p\n", - dev->location.bus,dev->location.slot,dev->location.function, - bar_type_str, - resource->bar->idx, - resource->bar->phys, resource->bar->phys+resource->bar->size); - - if (resource->bar->type == PCI_BAR64) - bar++; - - LIST_APPEND(pci_resource_list, &dev->resources, resource); - } -} - -static void update_cap(pci_device* dev, pci_resource* resource) -{ - uint64_t tmp = 0; - - DrvS_ReadPCIRegister(dev->location, resource->cap->offset, 4, &tmp); - uint16_t cap_hdr = (uint16_t)(tmp&0xffff); - resource->cap->id = cap_hdr & 0xff; -} - -static void initialize_capability_resources(pci_device* dev) -{ - uint64_t status_register = 0; - DrvS_ReadPCIRegister(dev->location, 0x4, 4, &status_register); - - if (~status_register & BIT(16+4 /* status.cap_list */)) - return; // No cap list. - - // Register each capability. - - // Capability header: - // Bits 8-15: Next Pointer - // Bits 0-7: Capability ID - - uint64_t tmp = 0; - DrvS_ReadPCIRegister(dev->location, 0x34, 4, &tmp); - - pci_capability* tail = nullptr; - - uint8_t visited_offsets[256/8]; - memzero(visited_offsets, sizeof(visited_offsets)); -#define set_visited_offset(off) (visited_offsets[(off)/8] |= BIT((off)%8)) -#define visited_offset(off) !!(visited_offsets[(off)/8] & BIT((off)%8)) - - for (uint8_t offset = tmp & 0xff; offset; ) - { - uint8_t next = 0; - uint8_t cap_id = 0; - - // works around certain PCs that have something like: - // a->b->c->b - if (visited_offset(offset)) - break; - set_visited_offset(offset); - - DrvS_ReadPCIRegister(dev->location, offset, 4, &tmp); - uint16_t cap_hdr = (uint16_t)(tmp&0xffff); - next = (cap_hdr>>8) & 0xff; - cap_id = cap_hdr & 0xff; - - if (!cap_id) - goto done; // don't register null capabilities, they are useless. - - pci_resource* resource = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(pci_resource), nullptr); - resource->type = PCI_RESOURCE_CAPABILITY; - - resource->cap = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(pci_capability), nullptr); - resource->cap->id = cap_id; - resource->cap->offset = offset; - - if (tail) - tail->next_cap = resource->cap; - if (!tail) - dev->first_capability = resource->cap; - tail = resource->cap; - - resource->owner = dev; - LIST_APPEND(pci_resource_list, &dev->resources, resource); - - static const char* const cap_ids[0x16] = { - "Null", - "PCI Power Management Interface", - "AGP", - "VPD", - "Slot identification", - "MSI", - "CompactPCI Hot Swap", - "PCI-X", - "HyperTransport", - "Vendor Specific", - "Debug Port", - "CompactPCI central resource control", - "PCI Hot-Plug", - "PCI Bridge Subsystem Vendor ID", - "AGP 8x", - "Secure Device", - "PCIe", - "MSI-X", - "SATA Data/Index Configuration", - "Advanced Features", - "Enhanced Allocation", - "Flattening Portal Bridge", - }; - - OBOS_Debug("PCI: %02x:%02x:%02x: Found %s Capability.\n", - dev->location.bus,dev->location.slot,dev->location.function, - cap_id > 0x15 ? "Unknown" : cap_ids[cap_id] - ); - - done: - offset = next; - } -#undef visited_offset -#undef set_visited_offset -} - -static void initialize_irq_resources(pci_device* dev) -{ - // First check if there is an interrupt pin or - // interrupt line in the header. - - uint64_t reg15 = 0; - DrvS_ReadPCIRegister(dev->location, 0xf*4, 4, ®15); - - if (!(reg15 & 0xff00) && ((reg15 & 0xff) == 0xff)) - goto check_msi; - else - goto done; - - check_msi: - bool found = false; - for (pci_capability* curr = dev->first_capability; curr; ) - { - if (curr->id == 0x05 || curr->id == 0x11) - { - found = true; - break; - } - - curr = curr->next_cap; - } - if (!found) - return; // No IRQ, return early. - - done: - (void)0; - - pci_resource* resource = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(pci_resource), nullptr); - resource->type = PCI_RESOURCE_IRQ; - - resource->owner = dev; - resource->irq = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(pci_irq_handle), nullptr); - - resource->owner = dev; - LIST_APPEND(pci_resource_list, &dev->resources, resource); - - OBOS_Debug("PCI: %02x:%02x:%02x: Device has IRQ capabilities.\n", - dev->location.bus,dev->location.slot,dev->location.function - ); -} - -static void initialize_cmd_register_resource(pci_device* dev) -{ - pci_resource* resource = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(pci_resource), nullptr); - resource->type = PCI_RESOURCE_CMD_REGISTER; - uint64_t cmd_register = 0; - DrvS_ReadPCIRegister(dev->location, 0x4, 4, &cmd_register); - resource->cmd_register = cmd_register & 0xffff; - - LIST_APPEND(pci_resource_list, &dev->resources, resource); - resource->owner = dev; - dev->resource_cmd_register = resource; -} - -static pci_iteration_decision bridge_cb(void* udata, pci_device_location loc); - -static pci_iteration_decision init_bus_cb(void* udata, pci_device_location loc) -{ - uint64_t tmp = 0; - DrvS_ReadPCIRegister(loc, 0xc, 4, &tmp); - if (((tmp >> 16) & 0x7f) != 0) - { - if (((tmp >> 16) & 0x7f) == 1 && (((pci_bus*)udata)->busNumber != 0)) - bridge_cb((void*)__func__, loc); - return PCI_ITERATION_DECISION_CONTINUE; - } - - pci_bus* bus = udata; - pci_device* dev = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(pci_device), nullptr); - - // Location info - dev->owner = bus; - dev->location = loc; - - // Initialize the HID. - DrvS_ReadPCIRegister(loc, 8, 4, &tmp); - uint8_t classCode = (tmp >> 24) & 0xff; - uint8_t subclass = (tmp >> 16) & 0xff; - uint8_t progIF = (tmp >> 8) & 0xff; - DrvS_ReadPCIRegister(loc, 0, 4, &tmp); - uint16_t deviceId = tmp >> 16; - uint16_t vendorId = tmp & 0xffff; - dev->hid.indiv.deviceId = deviceId; - dev->hid.indiv.vendorId = vendorId; - dev->hid.indiv.classCode = classCode; - dev->hid.indiv.subClass = subclass; - dev->hid.indiv.progIf = progIF; - - OBOS_Debug("PCI: %02x:%02x:%02x: Device HID: %02x:%02x:%02x, Vendor ID: 0x%04x, Device ID: 0x%04x\n", - dev->location.bus,dev->location.slot,dev->location.function, - classCode, subclass, progIF, - vendorId, deviceId - ); - - if (classCode == 0x3 && !(deviceId == 0x1111 && vendorId == 0x1234 /* Bochs VBE */)) - { - OBOS_Warning("PCI: %02x:%02x:%02x: Skipping probe of GPU.\n", - dev->location.bus,dev->location.slot,dev->location.function - ); - return PCI_ITERATION_DECISION_CONTINUE; - } - - // Initialize resources. - // NOTE: The order of these matters. - initialize_cmd_register_resource(dev); - initialize_bar_resources(dev); - initialize_capability_resources(dev); - // DEPENDS: Capability resources. - initialize_irq_resources(dev); - - LIST_APPEND(pci_device_list, &bus->devices, dev); - - if (OBOS_GetLogLevel() <= LOG_LEVEL_DEBUG) - printf("\n"); - - return PCI_ITERATION_DECISION_CONTINUE; -} -// Only initializes Bus 0. -// CAN and SHOULD be called before uACPI UACPI_INIT_LEVEL_NAMESPACE_LOADED -obos_status Drv_EarlyPCIInitialize() -{ - OBOS_ASSERT (uacpi_get_current_init_level() < UACPI_INIT_LEVEL_NAMESPACE_LOADED); - - Drv_PCIBusCount = 1; - Drv_PCIBuses[0].busNumber = 0; - // acpiNode is saved for later (Drv_PCIInitialize) - - return DrvS_EnumeratePCI(0, init_bus_cb, &Drv_PCIBuses[0]); -} - -static uacpi_iteration_decision acpi_bus_cb(void* udata, uacpi_namespace_node* node, uint32_t max_depth) -{ - OBOS_UNUSED(max_depth); - - uint64_t segment = 0, bus_number = 0; - uacpi_eval_simple_integer(node, "_SEG", &segment); - uacpi_eval_simple_integer(node, "_BBN", &bus_number); - - // TODO: PCIe - if (segment != 0) - return UACPI_ITERATION_DECISION_CONTINUE; - - if (bus_number == 0) - { - Drv_PCIBuses[0].acpiNode = node; - return UACPI_ITERATION_DECISION_CONTINUE; - } - - OBOS_Debug("%s: Initializing bus %d (from ACPI)\n\n", (const char*)udata, bus_number & 0xff); - - pci_bus* bus = &Drv_PCIBuses[Drv_PCIBusCount++]; - bus->busNumber = bus_number & 0xff; - bus->acpiNode = node; - DrvS_EnumeratePCI(bus->busNumber, init_bus_cb, bus); - - return UACPI_ITERATION_DECISION_CONTINUE; -} - -static pci_iteration_decision bridge_cb(void* udata, pci_device_location loc) -{ - uint64_t tmp = 0; - DrvS_ReadPCIRegister(loc, 0xc, 4, &tmp); - // OBOS_Debug("PCI: %02x:%02x:%02x: hdr type: 0x%x. register data: 0x%08x\n", - // loc.bus,loc.slot,loc.function, - // ((tmp >> 16) & 0x7f), tmp - // ); - if (((tmp >> 16) & 0x7f) != 0x1 /* PCI->PCI Bridge */) - return PCI_ITERATION_DECISION_CONTINUE; - - // wooo - // we found a pci->pci bridge. - - DrvS_ReadPCIRegister(loc, 0x18, 4, &tmp); - - uint8_t secondary_bus = (tmp >> 8) & 0xff; - - OBOS_Debug("%s: Initializing bus %d (from PCI->PCI Bridge)\n\n", (const char*)udata, secondary_bus); - pci_bus* bus = &Drv_PCIBuses[Drv_PCIBusCount++]; - bus->busNumber = secondary_bus & 0xff; - bus->acpiNode = nullptr; - DrvS_EnumeratePCI(bus->busNumber, init_bus_cb, bus); - - return PCI_ITERATION_DECISION_CONTINUE; -} - -// Should be called after UACPI_INIT_LEVEL_NAMESPACE_LOADED. -obos_status Drv_PCIInitialize() -{ - OBOS_ASSERT (uacpi_get_current_init_level() >= UACPI_INIT_LEVEL_NAMESPACE_LOADED); - - // If PCI Device 00:00.0 is multi-function, then the other functions are the bus numbers. - uint64_t tmp = 0; - DrvS_ReadPCIRegister((pci_device_location){0,0,0}, 0xC, 4, &tmp); - - uint8_t hdr_type = (tmp >> 24) & 0xff; - if (hdr_type & BIT(7)) - { - // Multi-function device, enumerate the other functions. - pci_device_location loc = {}; - for (loc.function = 1; loc.function < 8; loc.function++) - { - DrvS_ReadPCIRegister(loc, 0x0, 4, &tmp); - if ((tmp & 0xffff) != 0xffff) - { - OBOS_Debug("%s: Initializing bus %d (from multi-function host bridge)\n\n", __func__, loc.function); - pci_bus* bus = &Drv_PCIBuses[Drv_PCIBusCount++]; - bus->busNumber = loc.function; - bus->acpiNode = nullptr; - DrvS_EnumeratePCI(bus->busNumber, init_bus_cb, bus); - } - } - } - - // Look for PCI->PCI Bridges. - DrvS_EnumeratePCI(0, bridge_cb, (void*)__func__); - - // Look for devices NOT directly discoverable with pci. - static const char* hids[] = { - "PNP0A03", // PCI bus - "PNP0A08", // PCIe bus - nullptr, - }; - uacpi_find_devices_at(uacpi_namespace_get_predefined(UACPI_PREDEFINED_NAMESPACE_SB), hids, acpi_bus_cb, (void*)__func__); - - // for (volatile bool b = true; b; ) - // asm volatile ("" : :"r"(b) :"memory"); - - return OBOS_STATUS_SUCCESS; -} - -static obos_status writeback_bar(pci_device* device, pci_bar* bar) -{ - // Disable memory decode and IO Space access. - volatile uint64_t old_cmd_register = device->resource_cmd_register->cmd_register; - if (old_cmd_register & 0b11) - { - uint64_t cmd_register = 0; - DrvS_ReadPCIRegister(device->location, 0x4, 4, &cmd_register); - cmd_register &= ~0b11; - DrvS_WritePCIRegister(device->location, 0x4, 4, cmd_register); - } - - obos_status status = OBOS_STATUS_SUCCESS; - switch (bar->type) { - case PCI_BAR32: - status = DrvS_WritePCIRegister(device->location, 0x10+bar->idx*4, 0x4, bar->phys & 0xffffffff); - break; - case PCI_BAR64: - status = DrvS_WritePCIRegister(device->location, 0x10+bar->idx*4, 0x4, bar->phys & 0xffffffff); - status = DrvS_WritePCIRegister(device->location, 0x10+bar->idx*4+4, 0x4, bar->phys >> 32); - break; - case PCI_BARIO: - status = DrvS_WritePCIRegister(device->location, 0x10+bar->idx*4, 4, bar->phys & 0xffffffff); - break; - default: - return OBOS_STATUS_INVALID_ARGUMENT; - } - - if (old_cmd_register & 0b11) - { - // Writeback the old cmd register value. - DrvS_WritePCIRegister(device->location, 0x4, 4, old_cmd_register); - } - return status; -} - -// Writes a resource to the appropriate place in the PCI config space. -obos_status Drv_PCISetResource(const pci_resource* resource) -{ - if (!resource) - return OBOS_STATUS_INVALID_ARGUMENT; - if (!resource->owner) - return OBOS_STATUS_INVALID_ARGUMENT; - if (!resource->owner->owner) - return OBOS_STATUS_INVALID_ARGUMENT; - - obos_status status = OBOS_STATUS_SUCCESS; - switch (resource->type) { - case PCI_RESOURCE_BAR: - status = writeback_bar(resource->owner, resource->bar); - break; - case PCI_RESOURCE_CMD_REGISTER: - status = DrvS_WritePCIRegister(resource->owner->location, 0x4, 4, resource->cmd_register); - break; - case PCI_RESOURCE_CAPABILITY: - status = OBOS_STATUS_INVALID_OPERATION; - break; - case PCI_RESOURCE_IRQ: - status = Drv_UpdatePCIIrq(resource->irq->irq, resource->owner, resource->irq); - break; - default: - return OBOS_STATUS_INVALID_ARGUMENT; - } - - return status; -} -// Gets a resource from the appropriate place in the PCI config space. -obos_status Drv_PCIUpdateResource(pci_resource* resource) -{ - if (!resource) - return OBOS_STATUS_INVALID_ARGUMENT; - if (!resource->owner) - return OBOS_STATUS_INVALID_ARGUMENT; - if (!resource->owner->owner) - return OBOS_STATUS_INVALID_ARGUMENT; - - obos_status status = OBOS_STATUS_SUCCESS; - switch (resource->type) { - case PCI_RESOURCE_BAR: - update_bar(resource->owner, resource); - break; - case PCI_RESOURCE_CMD_REGISTER: - { - uint64_t tmp = 0; - status = DrvS_ReadPCIRegister(resource->owner->location, 0x4, 4, &tmp); - resource->cmd_register = tmp; - break; - } - case PCI_RESOURCE_CAPABILITY: - update_cap(resource->owner, resource); - break; - case PCI_RESOURCE_IRQ: - status = Drv_UpdatePCIIrq(resource->irq->irq, resource->owner, resource->irq); - break; - default: - return OBOS_STATUS_INVALID_ARGUMENT; - } - - return status; -} - -#else -#include -obos_status Drv_EarlyPCIInitialize() -{ - return OBOS_STATUS_UNIMPLEMENTED; -} -__attribute__((alias("Drv_EarlyPCIInitialize"))) obos_status Drv_PCIInitialize(); -#endif - -#include -#include - -char* DrvH_MakePCIDeviceName(pci_device_location loc, const char* prefix) -{ - const char* format = loc.function == 0 ? "%sp%ds%d" : "%sp%ds%df%d"; - size_t len = snprintf(NULL, 0, format, prefix, loc.bus, loc.slot, loc.function); - char* str = Allocate(OBOS_KernelAllocator, len+1, nullptr); - snprintf(str, len+1, format, prefix, loc.bus, loc.slot, loc.function); - str[len] = 0; // is this redundant? - return str; -} \ No newline at end of file diff --git a/src/oboskrnl/driver_interface/pci.h b/src/oboskrnl/driver_interface/pci.h deleted file mode 100644 index 2409adce..00000000 --- a/src/oboskrnl/driver_interface/pci.h +++ /dev/null @@ -1,210 +0,0 @@ -/* - * oboskrnl/driver_interface/pci.h - * - * Copyright (c) 2024-2025 Omar Berrow -*/ - -#pragma once - -#include -#include -#include - -#include - -#include - -#include - -typedef enum pci_iteration_decision -{ - PCI_ITERATION_DECISION_ABORT, - PCI_ITERATION_DECISION_CONTINUE, -} pci_iteration_decision; - -typedef struct pci_device_location -{ - uint8_t bus; - uint8_t slot; - uint8_t function; -} pci_device_location; - -typedef union pci_hid -{ - struct - { - uint8_t classCode; - uint8_t subClass; - uint8_t progIf; - uint8_t resv; - uint16_t vendorId; - uint16_t deviceId; - } OBOS_PACK indiv; - uint64_t id; -} pci_hid; - -typedef enum { - PCI_BARIO, - PCI_BAR32, - PCI_BAR64, -} pci_bar_type; -#define PCI_BAR_MASK_32BIT (0b000 /* Memory Space BAR, 32-bit */) -#define PCI_BAR_MASK_64BIT (0b100 /* Memory Space BAR, 64-bit */) -#define PCI_BAR_MASK_IOSPACE (0b001 /* IOSpace BAR */) -typedef struct pci_bar -{ - uint8_t idx; - union { - // We don't use uintptr_t here in case that is 32-bit, but a PCI device has a 64-bit BAR - // This can happen on a kernel compiled for i686 running on an x86-64 host. - uint64_t phys; - uint16_t iospace; - }; - size_t size; - pci_bar_type type; - bool prefetchable; -} pci_bar; - -typedef struct pci_capability -{ - struct pci_capability* next_cap; - uint8_t id; - uint8_t offset; -} pci_capability; - -typedef enum { - PCI_RESOURCE_INVALID, - PCI_RESOURCE_BAR, - PCI_RESOURCE_CMD_REGISTER, - PCI_RESOURCE_IRQ, - PCI_RESOURCE_CAPABILITY, -} pci_resource_type; -typedef struct pci_resource -{ - union { - pci_bar* bar; - uint16_t cmd_register; - struct pci_irq_handle* irq; - pci_capability* cap; - }; - pci_resource_type type; - struct pci_device* owner; - - LIST_NODE(pci_resource_list, struct pci_resource) node; -} pci_resource; - -typedef LIST_HEAD(pci_resource_list, pci_resource) pci_resource_list; -LIST_PROTOTYPE(pci_resource_list, pci_resource, node); - -typedef struct pci_device { - // Location info - pci_device_location location; - struct pci_bus* owner; - - // hid - pci_hid hid; - - // Resource info - pci_resource_list resources; - - // Resource shortcuts - pci_capability* first_capability; - pci_resource* resource_cmd_register; // command register - - LIST_NODE(pci_device_list, struct pci_device) node; -} pci_device; - -typedef LIST_HEAD(pci_device_list, pci_device) pci_device_list; -LIST_PROTOTYPE(pci_device_list, pci_device, node); - -typedef struct pci_bus { - uint8_t busNumber; - pci_device_list devices; - struct uacpi_namespace_node* acpiNode; -} pci_bus; - -extern OBOS_EXPORT pci_bus Drv_PCIBuses[256]; -extern OBOS_EXPORT size_t Drv_PCIBusCount; - -// Only initializes Bus 0. -// CAN and SHOULD be called before uACPI UACPI_INIT_LEVEL_NAMESPACE_LOADED -obos_status Drv_EarlyPCIInitialize(); -// Initializes all buses. -// Should be called after UACPI_INIT_LEVEL_NAMESPACE_LOADED. -obos_status Drv_PCIInitialize(); - -// Writes a resource to the appropriate place in the PCI config space. -OBOS_EXPORT obos_status Drv_PCISetResource(const pci_resource* resource); -// Gets a resource from the appropriate place in the PCI config space. -OBOS_EXPORT obos_status Drv_PCIUpdateResource(pci_resource* resource); - -typedef struct pci_irq_handle -{ - union { - uint32_t arch_handle; // from DrvS_RegisterIRQPin - uintptr_t msix_entry; // a pointer to the MSI-X table entry. - } un; - // If zero, assume arch_handle is valid. - // If non-zero, check msix_entry. - // If zero, this is an MSI-device, otherwise the device utilizes MSI-X. - pci_capability* msi_capability; - pci_device* dev; - uintptr_t msix_pending_entry; - irq* irq; - bool initialized; - bool masked; -} pci_irq_handle; -OBOS_WEAK bool DrvS_CheckIrqCallbackIrqPin(struct irq* i, void* userdata); -#if OBOS_ARCHITECTURE_HAS_PCI -OBOS_EXPORT obos_status DrvS_WriteIOSpaceBar(pci_bar* bar, uint16_t offset, uint32_t val, uint8_t byte_width); -OBOS_EXPORT obos_status DrvS_ReadIOSpaceBar(pci_bar* bar, uint16_t offset, uint32_t *val, uint8_t byte_width); -OBOS_EXPORT obos_status DrvS_EnumeratePCI(uint8_t bus, pci_iteration_decision(*cb)(void* udata, pci_device_location device), void *cb_udata); -OBOS_EXPORT obos_status DrvS_ReadPCIRegister(pci_device_location loc, uint8_t offset, size_t accessSize, uint64_t* val); -OBOS_EXPORT obos_status DrvS_WritePCIRegister(pci_device_location loc, uint8_t offset, size_t accessSize, uint64_t val); -OBOS_EXPORT size_t DrvS_GetBarSize(pci_device_location loc, uint8_t barIndex, bool is64bit, obos_status* status); -// Returns the address field that MSI(-X) expects, and outputs in *data the data expected by MSI(-X). -uint64_t DrvS_MSIAddressAndData(uint64_t* data, irq_vector_id vec, uint32_t processor, bool edgetrigger, bool deassert); -obos_status DrvS_RegisterIRQPin(const pci_device_location* dev, uint32_t* handle, irq_vector_id vector); -obos_status DrvS_MaskIRQPin(uint32_t handle, bool mask); -#ifdef __x86_64__ -# define PCI_IRQ_CAN_USE_ACPI 1 /* for stuff like PCI routing tables */ -# define PCI_IRQ_UACPI_INIT_LEVEL UACPI_INIT_LEVEL_NAMESPACE_LOADED -#endif -// Note: Overwrites irq->irqChecker as well as irq->irqCheckerUserdata. -// It also overwrites the IRQ move callback. -obos_status Drv_UpdatePCIIrq(irq* irq, pci_device* dev, pci_irq_handle* handle); -#else -OBOS_EXPORT obos_status DrvS_WriteIOSpaceBar(pci_bar* bar, uint16_t offset, uint32_t val, uint8_t byte_width); -OBOS_EXPORT obos_status DrvS_ReadIOSpaceBar(pci_bar* bar, uint16_t offset, uint32_t *val, uint8_t byte_width); -OBOS_EXPORT obos_status DrvS_EnumeratePCI(uint8_t bus, pci_iteration_decision(*cb)(void* udata, pci_device_location device), void *cb_udata); -OBOS_EXPORT obos_status DrvS_ReadPCIRegister(pci_device_location loc, uint8_t offset, size_t accessSize, uint64_t* val); -OBOS_EXPORT obos_status DrvS_WritePCIRegister(pci_device_location loc, uint8_t offset, size_t accessSize, uint64_t val); -OBOS_EXPORT size_t DrvS_GetBarSize(pci_device_location loc, uint8_t barIndex, bool is64bit, obos_status* status); -// Returns the address field that MSI(-X) expects, and outputs in *data the data expected by MSI(-X). -uint64_t DrvS_MSIAddressAndData(uint64_t* data, irq_vector_id vec, uint32_t processor, bool edgetrigger, bool deassert); -obos_status DrvS_RegisterIRQPin(const pci_device_location* dev, uint32_t* handle, irq_vector_id vector); -obos_status DrvS_MaskIRQPin(uint32_t handle, bool mask); -#ifdef __x86_64__ -# define PCI_IRQ_CAN_USE_ACPI 1 /* for stuff like PCI routing tables */ -# define PCI_IRQ_UACPI_INIT_LEVEL UACPI_INIT_LEVEL_NAMESPACE_LOADED -#endif -// Note: Overwrites irq->irqChecker as well as irq->irqCheckerUserdata. -// It also overwrites the IRQ move callback. -obos_status Drv_UpdatePCIIrq(irq* irq, pci_device* dev, pci_irq_handle* handle); -#endif - -#ifndef PCI_IRQ_CAN_USE_ACPI -# define PCI_IRQ_CAN_USE_ACPI 0 -#endif - -#ifndef PCI_IRQ_UACPI_INIT_LEVEL -# if !PCI_IRQ_CAN_USE_ACPI -# define PCI_IRQ_UACPI_INIT_LEVEL 0 -# else -# define PCI_IRQ_UACPI_INIT_LEVEL UACPI_INIT_LEVEL_NAMESPACE_LOADED -# endif -#endif - -#define ETHERNET_DEVICE_PREFIX "en" - -OBOS_EXPORT char* DrvH_MakePCIDeviceName(pci_device_location loc, const char* prefix); \ No newline at end of file diff --git a/src/oboskrnl/driver_interface/pci_irq.c b/src/oboskrnl/driver_interface/pci_irq.c deleted file mode 100644 index 046a9c60..00000000 --- a/src/oboskrnl/driver_interface/pci_irq.c +++ /dev/null @@ -1,240 +0,0 @@ -/* - * oboskrnl/driver_interface/pci_irq.c - * - * Copyright (c) 2024-2025 Omar Berrow -*/ - -#include -#include -#include - -#include - -#include - -#include -#include -#include - -#include - -#include - -#if OBOS_ARCHITECTURE_HAS_PCI -static void* map_registers(uintptr_t phys, size_t size, bool uc) -{ - size_t phys_page_offset = (phys % OBOS_PAGE_SIZE); - phys -= phys_page_offset; - size = size + (OBOS_PAGE_SIZE - (size % OBOS_PAGE_SIZE)); - size += phys_page_offset; - void* virt = Mm_VirtualMemoryAlloc( - &Mm_KernelContext, - nullptr, size, - uc ? OBOS_PROTECTION_CACHE_DISABLE : 0, VMA_FLAGS_NON_PAGED, - nullptr, - nullptr); - for (uintptr_t offset = 0; offset < size; offset += OBOS_PAGE_SIZE) - { - page_info page = {.virt=offset+(uintptr_t)virt}; - MmS_QueryPageInfo(Mm_KernelContext.pt, page.virt, &page, &phys); - page.prot.uc = uc; - MmS_SetPageMapping(Mm_KernelContext.pt, &page, phys + offset, false); - } - MmS_TLBShootdown(Mm_KernelContext.pt, (uintptr_t)virt, size); - return virt+phys_page_offset; -} -static void pci_irq_move_callback(struct irq* i, struct irq_vector* from, struct irq_vector* to, void* userdata) -{ - OBOS_UNUSED(i); - OBOS_UNUSED(from); - pci_irq_handle* hnd = (pci_irq_handle*)userdata; - if (!hnd->msi_capability) - { - DrvS_RegisterIRQPin(&hnd->dev->location, &hnd->un.arch_handle, to->id); - return; - } - uint64_t msi_data = 0; - uint64_t msi_address = DrvS_MSIAddressAndData(&msi_data, 0, 0, true, false); - if (hnd->un.msix_entry) - { - uint32_t* entry = (uint32_t*)hnd->un.msix_entry; - entry[0] = msi_address & UINT32_MAX; - entry[1] = msi_address >> 32; - entry[2] = msi_data; - return; - } - uint64_t header = 0; // 32-bit - DrvS_ReadPCIRegister(hnd->dev->location, hnd->msi_capability->offset+0, 4, &header); - uint8_t curr = 4; - DrvS_WritePCIRegister(hnd->dev->location, hnd->msi_capability->offset+curr, 4, msi_address & UINT32_MAX); - if (header & BIT(16+7)) - { - curr += 4; - DrvS_WritePCIRegister(hnd->dev->location, hnd->msi_capability->offset + curr, 4, msi_address >> 32); - } - curr += 4; - DrvS_WritePCIRegister(hnd->dev->location, hnd->msi_capability->offset+ curr, 4, msi_data & UINT16_MAX); - // Write back the header. - DrvS_WritePCIRegister(hnd->dev->location, hnd->msi_capability->offset+0, 4, header); -} -obos_status Drv_UpdatePCIIrq(irq* irq, pci_device* dev, pci_irq_handle* handle) -{ - if (!irq || !dev || !handle) - return OBOS_STATUS_INVALID_ARGUMENT; - irq->irqChecker = nullptr; - irq->moveCallback = pci_irq_move_callback; - irq->irqMoveCallbackUserdata = handle; - handle->dev = dev; - - bool has_msix = false; - bool has_msi = false; - uint8_t msix_offset = 0; - uint8_t msi_offset = 0; - - // uint64_t pci_status = 0; - // DrvS_ReadPCIRegister(*dev, 1*4+2, 2, &pci_status); - // if (!(pci_status & BIT(4))) - // goto fallback; - - if (!handle->initialized) - { - // Uninitialized object. - // Look for a msi capability. - for (pci_capability* curr = dev->first_capability; curr; ) - { - switch (curr->id) - { - case 0x05: - has_msi = true; - msi_offset = curr->offset; - handle->msi_capability = curr; - OBOS_Debug("Found MSI capability at 0x%02x.\n", msi_offset); - break; - case 0x11: - // has_msix = true; - msix_offset = curr->offset; - handle->msi_capability = curr; - OBOS_Debug("Found MSI-X capability at 0x%02x.\n", msix_offset); - break; - default: - break; - } - - if (has_msix) - break; - curr = curr->next_cap; - } - handle->initialized = true; - } - else - { - if (handle->msi_capability) - { - if (handle->un.msix_entry) - { - has_msix = true; - msix_offset = handle->msi_capability->offset; - } - else - { - has_msi = true; - msi_offset = handle->msi_capability->offset; - } - } - } - if (!has_msi && !has_msix) - goto fallback; - - cpu_local* target_cpu = nullptr; - for (int i = Core_CpuCount-1; i >= 0; i--) - if (!target_cpu || Core_CpuInfo[i].nMSIRoutedIRQs < target_cpu->nMSIRoutedIRQs) - target_cpu = &Core_CpuInfo[i]; - - uint64_t msi_data = 0; - uint64_t msi_address = DrvS_MSIAddressAndData(&msi_data, irq->vector->id, target_cpu->id, true, false); - if (has_msix) - { - // Prefer MSI-X over MSI. - uint64_t header = 0; // 32-bit - DrvS_ReadPCIRegister(dev->location, msix_offset+0, 4, &header); - header |= BIT(31) /* Enable */; - uint64_t bar_info = 0; // 32-bit register - DrvS_ReadPCIRegister(dev->location, 4+msix_offset, 4, &bar_info); - uint8_t bar_index = bar_info & 0x7; - uint64_t bar = 0; - DrvS_ReadPCIRegister(dev->location, (bar_index+4)*4, 4, &bar); - if (((bar >> 1) & 0b11) == 0x2) - DrvS_ReadPCIRegister(dev->location, (bar_index+5)*4, 4, (uint64_t*)(((uint32_t*)&bar) + 1)); - bar &= ~0xf; - uint32_t bar_offset = bar_info & ~0x7; - handle->un.msix_entry = (uintptr_t)map_registers(bar+bar_offset, OBOS_PAGE_SIZE, true); - bar_info = 0; // 32-bit register - DrvS_ReadPCIRegister(dev->location, 8+msix_offset, 4, &bar_info); - bar_index = bar_info & 0x7; - bar = 0; - DrvS_ReadPCIRegister(dev->location, (bar_index+4)*4, 4, &bar); - if (((bar >> 1) & 0b11) == 0x2) - DrvS_ReadPCIRegister(dev->location, (bar_index+5)*4, 4, (uint64_t*)(((uint32_t*)&bar) + 1)); - bar &= ~0xf; - bar_offset = bar_info & ~0x7; - handle->msix_pending_entry = (uintptr_t)map_registers(bar+bar_offset, OBOS_PAGE_SIZE, true); - uint32_t* entry = (uint32_t*)handle->un.msix_entry; - entry[0] = msi_address & UINT32_MAX; - entry[1] = msi_address >> 32; - entry[2] = msi_data; - if (handle->masked) - entry[3] |= BIT(0); // masked - else - entry[3] &= ~BIT(0); // unmasked - // Write back the header. - DrvS_WritePCIRegister(dev->location, msix_offset+0, 4, header); - target_cpu->nMSIRoutedIRQs++; - return OBOS_STATUS_SUCCESS; - } - if (has_msi) - { - // printf("choosing MSI for device %02x:%02x:%02x\n", - // dev->location.bus,dev->location.slot,dev->location.function - // ); - // Fallback to MSI. - uint64_t header = 0; // 32-bit - DrvS_ReadPCIRegister(dev->location, msi_offset+0, 4, &header); - OBOS_Debug("header=0x%x\n", header); - handle->un.msix_entry = 0; - handle->msix_pending_entry = 0; - header |= BIT(16) /* Enable */; - uint8_t curr = 4; - DrvS_WritePCIRegister(dev->location, msi_offset+curr, 4, msi_address & UINT32_MAX); - if (header & BIT(16+7)) - { - curr += 4; - DrvS_WritePCIRegister(dev->location, msi_offset + curr, 4, msi_address >> 32); - } - curr += 4; - DrvS_WritePCIRegister(dev->location, msi_offset + curr, 4, msi_data & UINT16_MAX); - // Write back the header. - DrvS_WritePCIRegister(dev->location, msi_offset+0, 4, header); - if (header & BIT(8+16)) - { - // Set Mask. - DrvS_WritePCIRegister(handle->dev->location, msi_offset + 0x10, 4, handle->masked); - } - target_cpu->nMSIRoutedIRQs++; - return OBOS_STATUS_SUCCESS; - } - fallback: - // printf("choosing legacy IRQs for device %02x:%02x:%02x\n", - // dev->location.bus,dev->location.slot,dev->location.function - // ); - // Use legacy IRQs. - if (DrvS_CheckIrqCallbackIrqPin) - { - irq->irqChecker = DrvS_CheckIrqCallbackIrqPin; - irq->irqCheckerUserdata = handle; - } - obos_status stat = DrvS_RegisterIRQPin(&dev->location, &handle->un.arch_handle, irq->vector->id); - if (obos_is_error(stat)) - return stat; - return DrvS_MaskIRQPin(handle->un.arch_handle, handle->masked); -} -#endif diff --git a/src/oboskrnl/driver_interface/pnp.c b/src/oboskrnl/driver_interface/pnp.c deleted file mode 100644 index d9cb7f2f..00000000 --- a/src/oboskrnl/driver_interface/pnp.c +++ /dev/null @@ -1,644 +0,0 @@ -/* - * oboskrnl/driver_interface/pnp.c - * - * Copyright (c) 2024-2025 Omar Berrow -*/ - -#include -#include -#include -#include - -#include -#include -#include -#include - -#include - -#include - -#include - -#include -#include - -#include -#include -#include -#include - -#if OBOS_ARCHITECTURE_HAS_ACPI -#include -#include -#include -#include -#endif - -#include - -typedef struct pnp_device -{ - pci_hid pci_key; // the class code, subclass, etc. - bool ignore_progif; - bool pci : 1; - bool acpi : 1; - char acpi_key[8]; // acpi pnp id - driver_header_list headers; - RB_ENTRY(pnp_device) acpi_node; - RB_ENTRY(pnp_device) pci_node; -} pnp_device; -typedef RB_HEAD(acpi_pnp_device_tree, pnp_device) acpi_pnp_device_tree; -typedef RB_HEAD(pci_pnp_device_tree, pnp_device) pci_pnp_device_tree; -static int pnp_pci_driver_cmp(pnp_device* a_, pnp_device* b_); -#if OBOS_ARCHITECTURE_HAS_ACPI -static int pnp_acpi_driver_compare(pnp_device* a, pnp_device* b); -RB_GENERATE_STATIC(acpi_pnp_device_tree, pnp_device, acpi_node, pnp_acpi_driver_compare); -#endif -RB_GENERATE_STATIC(pci_pnp_device_tree, pnp_device, pci_node, pnp_pci_driver_cmp); - -static int pnp_pci_driver_cmp(pnp_device* a_, pnp_device* b_) -{ - // NOTE(oberrow): If this fails, gl and have fun. - const pci_hid* a = &a_->pci_key; - const pci_hid* b = &b_->pci_key; - if (((int8_t)a->indiv.classCode - (int8_t)b->indiv.classCode) != 0) - return (int8_t)a->indiv.classCode - (int8_t)b->indiv.classCode; - if (((int8_t)a->indiv.subClass - (int8_t)b->indiv.subClass) != 0) - return (int8_t)a->indiv.subClass - (int8_t)b->indiv.subClass; - // if ((((int8_t)a->indiv.progIf - (int8_t)b->indiv.progIf) != 0)) - // return (int8_t)a->indiv.progIf - (int8_t)b->indiv.progIf; - return 0; -} - -#if OBOS_ARCHITECTURE_HAS_ACPI -static int pnp_acpi_driver_compare(pnp_device* a, pnp_device* b) -{ - return uacpi_strncmp(a->acpi_key, b->acpi_key, 8); -} -#endif - -struct allocation_header { - size_t size; -}; - -static void *malloc(size_t sz) -{ - struct allocation_header* hdr = ZeroAllocate(OBOS_KernelAllocator, 1, sz+sizeof(struct allocation_header), nullptr); - hdr->size = sz+sizeof(struct allocation_header); - return hdr + 1; -} - -static void *realloc(void* oldblk, size_t sz) -{ - struct allocation_header* hdr = oldblk; - hdr--; - size_t oldSz = hdr->size; - hdr->size += sz; - return Reallocate(OBOS_KernelAllocator, hdr, sz, oldSz, nullptr); -} - -static void free(void* blk) -{ - struct allocation_header* hdr = blk; - hdr--; - Free(OBOS_KernelAllocator, hdr, hdr->size); -} - -#if OBOS_ARCHITECTURE_HAS_ACPI -static void free_acpi_pnp_device(acpi_pnp_device_tree* map, pnp_device* dev) -{ - if (map) - RB_REMOVE(acpi_pnp_device_tree, map, dev); - for (driver_header_node* node = dev->headers.head; node; ) - { - driver_header_node* nextNode = node->next; - REMOVE_DRIVER_HEADER_NODE(dev->headers, node); - free(node); - node = nextNode; - } - // free(dev); -} -#endif -static void free_pci_pnp_device(pci_pnp_device_tree* map, pnp_device* dev) -{ - if (map) - RB_REMOVE(pci_pnp_device_tree, map, dev); - for (driver_header_node* node = dev->headers.head; node; ) - { - driver_header_node* nextNode = node->next; - REMOVE_DRIVER_HEADER_NODE(dev->headers, node); - free(node); - node = nextNode; - } - // free(dev); -} -static void append_driver_to_pnp_device(pnp_device* dev, driver_header* drv) -{ - OBOS_ASSERT(dev); - // NOTE(oberrow): I hate myself for not adding a kmalloc or something eariler - // Goddamn is this more convinient - // The time it took to write this comment is less than the time it takes to write all the - // '........' shit. - driver_header_node* node = malloc(sizeof(driver_header_node)); - memzero(node, sizeof(*node)); - node->data = drv; - APPEND_DRIVER_HEADER_NODE(dev->headers, node); -} -#if OBOS_ARCHITECTURE_HAS_ACPI -static obos_status acpi_driver_helper(acpi_pnp_device_tree* acpi_drivers, driver_header* drv, char pnpId[8]) -{ - pnp_device what = { - .acpi_key = { - pnpId[0], pnpId[1], pnpId[2], pnpId[3], - pnpId[4], pnpId[5], pnpId[6], pnpId[7], - }, - }; - pnp_device *dev = RB_FIND(acpi_pnp_device_tree, acpi_drivers, &what); - if (!dev) - { - dev = malloc(sizeof(pnp_device)); - *dev = what; - RB_INSERT(acpi_pnp_device_tree, acpi_drivers, dev); - } - append_driver_to_pnp_device(dev, drv); - return OBOS_STATUS_SUCCESS; -} -#endif - -static obos_status pci_driver_helper(pci_pnp_device_tree* pci_drivers, driver_header* drv, pci_hid key) -{ - pnp_device what = { - .pci_key = key, - }; - pnp_device *dev = RB_FIND(pci_pnp_device_tree, pci_drivers, &what); - if (!dev) - { - dev = memzero(malloc(sizeof(pnp_device)), sizeof(pnp_device)); - *dev = what; - RB_INSERT(pci_pnp_device_tree, pci_drivers, dev); - } - append_driver_to_pnp_device(dev, drv); - return OBOS_STATUS_SUCCESS; -} -struct callback_userdata -{ - pci_pnp_device_tree pci_drivers; - acpi_pnp_device_tree acpi_drivers; - driver_header_list* detected; -}; -static pci_iteration_decision pci_driver_callback(void* udata_, pci_device *device) -{ - struct callback_userdata* udata = (struct callback_userdata*)udata_; - - pnp_device what = { - .pci_key = device->hid, - }; - pnp_device *dev = RB_FIND(pci_pnp_device_tree, &udata->pci_drivers, &what); - if (!dev) - return PCI_ITERATION_DECISION_CONTINUE; - - // Add all of the drivers to the list. - for (driver_header_node* node = dev->headers.head; node; ) - { - driver_header_node* nextNode = node->next; - driver_header* hdr = node->data; - OBOS_ASSERT(hdr); - bool shouldAdd = false; - if (~hdr->flags & DRIVER_HEADER_PCI_IGNORE_PROG_IF) - { - if (hdr->pciId.indiv.progIf == device->hid.indiv.progIf) - shouldAdd = true; - } - else - shouldAdd = true; - if (!shouldAdd) - goto end; - - shouldAdd = false; - if ((hdr->flags & DRIVER_HEADER_PCI_HAS_VENDOR_ID)) - { - if (hdr->pciId.indiv.vendorId == device->hid.indiv.vendorId) - shouldAdd = true; - } - else - shouldAdd = true; - if (!shouldAdd) - goto end; - - shouldAdd = false; - if ((hdr->flags & DRIVER_HEADER_PCI_HAS_DEVICE_ID)) - { - if (hdr->pciId.indiv.deviceId == device->hid.indiv.deviceId) - shouldAdd = true; - } - else - shouldAdd = true; - if (!shouldAdd) - goto end; - - driver_header_node* newNode = malloc(sizeof(driver_header_node)); - newNode->data = hdr; - APPEND_DRIVER_HEADER_NODE(*udata->detected, newNode); - REMOVE_DRIVER_HEADER_NODE(dev->headers, node); - - end: - node = nextNode; - } - if (!dev->headers.nNodes) - { - // free the device. - - free_pci_pnp_device(&udata->pci_drivers, dev); - } - return PCI_ITERATION_DECISION_CONTINUE; -} -#if OBOS_ARCHITECTURE_HAS_ACPI -static obos_status probe_hid(const uacpi_id_string *hid, struct callback_userdata* udata) -{ - OBOS_ASSERT((hid->size - 1) <= 8); - if ((hid->size - 1) > 8) - return OBOS_STATUS_INVALID_ARGUMENT; - char* pnpId = hid->value; - pnp_device what = { - .acpi_key = { - pnpId[0], pnpId[1], pnpId[2], pnpId[3], - pnpId[4], pnpId[5], pnpId[6], pnpId[7], - } - }; - pnp_device *dev = RB_FIND(acpi_pnp_device_tree, &udata->acpi_drivers, &what); - if (!dev) - return OBOS_STATUS_NOT_FOUND; - // Add all of the drivers to the list. - for (driver_header_node* node = dev->headers.head; node; ) - { - driver_header_node* nextNode = node->next; - driver_header* hdr = node->data; - OBOS_ASSERT(hdr); - bool shouldAdd = true; - for (driver_header_node* n = udata->detected->head; n; ) - { - if (n->data == hdr) - { - shouldAdd = false; - break; - } - - n = n->next; - } - if (!shouldAdd) - goto end; - driver_header_node* newNode = malloc(sizeof(driver_header_node)); - newNode->data = hdr; - APPEND_DRIVER_HEADER_NODE(*udata->detected, newNode); - REMOVE_DRIVER_HEADER_NODE(dev->headers, node); - - end: - node = nextNode; - } - if (!dev->headers.nNodes) - { - // free the device. - - free_acpi_pnp_device(&udata->acpi_drivers, dev); - } - return OBOS_STATUS_SUCCESS; -} -static uacpi_iteration_decision acpi_enumerate_callback(void *ctx, uacpi_namespace_node *node, uint32_t max_depth) -{ - OBOS_UNUSED(max_depth); - - struct callback_userdata* userdata = (struct callback_userdata*)ctx; - - uacpi_namespace_node_info *info; - - uacpi_status ret = uacpi_get_namespace_node_info(node, &info); - if (uacpi_unlikely_error(ret)) - return UACPI_ITERATION_DECISION_CONTINUE; - - if (info->type != UACPI_OBJECT_DEVICE) - { - uacpi_free_namespace_node_info(info); - return UACPI_ITERATION_DECISION_CONTINUE; - } - - if (info->flags & UACPI_NS_NODE_INFO_HAS_HID) - probe_hid(&info->hid, userdata); // the result of this doesn't really matter. - if (info->flags & UACPI_NS_NODE_INFO_HAS_CID) - for (size_t i = 0; i < info->cid.num_ids; i++) - probe_hid(&info->cid.ids[i], userdata); // the result of this doesn't really matter. - - uacpi_free_namespace_node_info(info); - return UACPI_ITERATION_DECISION_CONTINUE; -} -#endif -obos_status Drv_PnpDetectDrivers(driver_header_list what, driver_header_list *toLoad) -{ - if (!toLoad) - return OBOS_STATUS_INVALID_ARGUMENT; -#if OBOS_ARCHITECTURE_HAS_ACPI - acpi_pnp_device_tree acpi_drivers = RB_INITIALIZER(x); -#else - bool acpi_drivers = 0; -#endif -#if OBOS_ARCHITECTURE_HAS_PCI - pci_pnp_device_tree pci_drivers = RB_INITIALIZER(x); -#else - bool pci_drivers = false; -#endif - // Divide the drivers into their respective hashmaps. - for (driver_header_node* node = what.head; node; ) - { - if (!node->data) - { - node = node->next; - continue; - } - driver_header* drv = node->data; - if (drv->flags & DRIVER_HEADER_PNP_IGNORE) - { - node = node->next; - continue; - } -#if OBOS_ARCHITECTURE_HAS_ACPI - if ((drv->flags & DRIVER_HEADER_FLAGS_DETECT_VIA_ACPI)) - for (size_t i = 0; i < drv->acpiId.nPnpIds; i++) - acpi_driver_helper(&acpi_drivers, drv, drv->acpiId.pnpIds[i]); -#endif -#if OBOS_ARCHITECTURE_HAS_PCI - if ((drv->flags & DRIVER_HEADER_FLAGS_DETECT_VIA_PCI)) - pci_driver_helper(&pci_drivers, drv, drv->pciId); -#endif - - node = node->next; - } - struct callback_userdata udata; - OBOS_UNUSED(udata); -#if OBOS_ARCHITECTURE_HAS_ACPI - udata.acpi_drivers = acpi_drivers; -#endif - udata.detected = toLoad; -#if OBOS_ARCHITECTURE_HAS_PCI - udata.pci_drivers = pci_drivers; - // Enumerate the PCI bus. - // DrvS_EnumeratePCI(pci_driver_callback, &udata); - do { - bool abort = false; - for (size_t curr = 0; curr < Drv_PCIBusCount && !abort; curr++) - { - pci_bus* bus = &Drv_PCIBuses[curr]; - for (pci_device* dev = LIST_GET_HEAD(pci_device_list, &bus->devices); dev && !abort; ) - { - switch (pci_driver_callback(&udata, dev)) - { - case PCI_ITERATION_DECISION_ABORT: - abort = true; - break; - default: - break; - } - - dev = LIST_GET_NEXT(pci_device_list, &bus->devices, dev); - } - } - } while(0); - // Free the pci driver map. - pnp_device* iter_pci = nullptr; - pnp_device* next_pci = nullptr; - RB_FOREACH_SAFE(iter_pci, pci_pnp_device_tree, &pci_drivers, next_pci) - free_pci_pnp_device(&pci_drivers, iter_pci); -#endif -#if OBOS_ARCHITECTURE_HAS_ACPI - // Enumerate ACPI - uacpi_namespace_for_each_child_simple( - uacpi_namespace_root(), - acpi_enumerate_callback, - &udata - ); - // Free the acpi driver map. - pnp_device* iter_acpi = nullptr; - pnp_device* next_acpi = nullptr; - RB_FOREACH_SAFE(iter_acpi, pci_pnp_device_tree, &pci_drivers, next_acpi) - free_pci_pnp_device(&pci_drivers, iter_acpi); -#endif - // Return success. - return OBOS_STATUS_SUCCESS; -} -struct driver_file -{ - driver_header* hdr; - driver_id* id; - void* base; - fd* file; - RB_ENTRY(driver_file) node; -}; -static int driver_file_compare(struct driver_file* a, struct driver_file* b) -{ - return (a->hdr < b->hdr) ? -1 : ((a->hdr > b->hdr) ? 1 : 0); -} -typedef RB_HEAD(driver_file_tree, driver_file) driver_file_tree; -RB_GENERATE_STATIC(driver_file_tree, driver_file, node, driver_file_compare); -static void driver_file_free(void* ele) -{ - struct driver_file* drv = ele; - if (drv->id) - Drv_UnrefDriver(drv->id); - Mm_VirtualMemoryFree(&Mm_KernelContext, drv->base, drv->file->vn->filesize); - // drv->hdr is invalid. - Vfs_FdClose(drv->file); -} -obos_status Drv_PnpLoadDriversAt(dirent* directory, bool wait) -{ - if (!directory) - return OBOS_STATUS_INVALID_ARGUMENT; - if (directory->vnode->vtype != VNODE_TYPE_DIR) - return OBOS_STATUS_INVALID_ARGUMENT; - Vfs_PopulateDirectory(directory); - driver_file_tree drivers = RB_INITIALIZER(x); - driver_header_list what = {}; - for (dirent* ent = directory->d_children.head; ent; ) - { - fd* file = Vfs_Calloc(1, sizeof(fd)); - obos_status status = Vfs_FdOpenDirent(file, ent, FD_OFLAGS_READ); - if (obos_is_error(status)) - { - if (status != OBOS_STATUS_NOT_A_FILE) - OBOS_Warning("Could not open file. Status: %d.\n", status); - Vfs_Free(file); - ent = ent->d_next_child; - continue; - } - Vfs_FdSeek(file, 0, SEEK_END); - size_t filesize = Vfs_FdTellOff(file); - Vfs_FdSeek(file, 0, SEEK_SET); - void* buf = Mm_VirtualMemoryAlloc(&Mm_KernelContext, nullptr, filesize, 0, VMA_FLAGS_PRIVATE, file, &status); - if (obos_is_error(status)) - { - OBOS_Warning("Could not allocate file contents. Status: %d.\n", status); - Vfs_FdClose(file); - Vfs_Free(file); - ent = ent->d_next_child; - continue; - } - driver_header* hdr = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(driver_header), nullptr); - status = Drv_LoadDriverHeader(buf, filesize, hdr); - if (obos_is_error(status)) - { - if (status != OBOS_STATUS_INVALID_FILE) - OBOS_Warning("Could not load driver header. Status: %d.\n", status); - Mm_VirtualMemoryFree(&Mm_KernelContext, buf, filesize); - Vfs_FdClose(file); - Vfs_Free(file); - ent = ent->d_next_child; - continue; - } - if (strnlen(hdr->driverName, 64)) - OBOS_Log("Found driver '%*s'\n", strnlen(hdr->driverName, 64), hdr->driverName); - else - OBOS_Log("Found a driver.\n", strnlen(hdr->driverName, 64), hdr->driverName); - struct driver_file *drv_file = memzero(malloc(sizeof(struct driver_file)), sizeof(struct driver_file)); - *drv_file = (struct driver_file){ .hdr=hdr, .base=buf, .file=file }; - driver_header_node* node = malloc(sizeof(driver_header_node)); - // hashmap_set(drivers, &drv_file); - RB_INSERT(driver_file_tree, &drivers, drv_file); - node->data = hdr; - APPEND_DRIVER_HEADER_NODE(what, node); - - ent = ent->d_next_child; - } - if (RB_EMPTY(&drivers)) - return OBOS_STATUS_SUCCESS; - driver_header_list toLoad = {}; - obos_status status = Drv_PnpDetectDrivers(what, &toLoad); - if (obos_is_success(status)) - { - for (driver_header_node* node = toLoad.head; node; ) - { - driver_header_node* next = node->next; - driver_header* const curr = node->data; - node = next; - struct driver_file ele = { .hdr=curr }; - // struct driver_file* file = (struct driver_file*)hashmap_get(drivers, &ele); - struct driver_file* file = RB_FIND(driver_file_tree, &drivers, &ele); - Vfs_FdSeek(file->file, 0, SEEK_END); - size_t filesize = Vfs_FdTellOff(file->file); - Vfs_FdSeek(file->file, 0, SEEK_SET); - obos_status loadStatus = OBOS_STATUS_SUCCESS; - if (strnlen(file->hdr->driverName, 64)) - OBOS_Log("Loading '%*s'\n", strnlen(file->hdr->driverName, 64), file->hdr->driverName); - else - OBOS_Log("Loading a driver...\n"); - driver_id* drv = Drv_LoadDriver(file->base, filesize, &loadStatus); - if (obos_is_error(loadStatus)) - { - OBOS_Warning("Could not load '%*s'. Status: %d\n", strnlen(file->hdr->driverName, 64), file->hdr->driverName, loadStatus); - continue; - } - drv->refCnt++; - file->id = drv; - loadStatus = Drv_StartDriver(drv, nullptr); - if (obos_is_error(loadStatus) && loadStatus != OBOS_STATUS_NO_ENTRY_POINT) - { - OBOS_Warning("Could not start '%*s'. Status: %d\n", strnlen(file->hdr->driverName, 64), file->hdr->driverName, loadStatus); - Drv_UnloadDriver(drv); - continue; - } - } - if (wait) - { - bool done = true; - while (1) - { - done = true; - struct driver_file *iter, *next; - // while (hashmap_iter(drivers, &i, &item)) - RB_FOREACH_SAFE(iter, driver_file_tree, &drivers, next) - { - if (!iter->id) - continue; - if (!iter->id->main_thread) - continue; - done = false; - } - if (done) - break; - } - } - } - for (driver_header_node* node = what.head; node; ) - { - driver_header_node* next = node->next; - free(node); - node = next; - } - struct driver_file *iter, *next; - RB_FOREACH_SAFE(iter, driver_file_tree, &drivers, next) - { - if (!iter->id) - continue; - if (!iter->id->main_thread) - continue; - driver_file_free(iter); - RB_REMOVE(driver_file_tree, &drivers, iter); - } - return status; -} - -#if OBOS_ENABLE_UHDA -#include - -UhdaController** Drv_uHDAControllers; -pci_device_location* Drv_uHDAControllersLocations; -size_t Drv_uHDAControllerCount; - -void OBOS_InitializeHDAAudioDev(); - -obos_status Drv_PnpLoad_uHDA() -{ - // pci_hid target_class = {}; - // target_class.indiv.classCode = UHDA_MATCHING_CLASS; - // target_class.indiv.subClass = UHDA_MATCHING_SUBCLASS; - - for (uint8_t bus = 0; bus < Drv_PCIBusCount; bus++) - { - for (pci_device* dev = LIST_GET_HEAD(pci_device_list, &Drv_PCIBuses[bus].devices); dev; ) - { - if (uhda_class_matches(dev->hid.indiv.classCode, dev->hid.indiv.subClass) || - uhda_device_matches(dev->hid.indiv.vendorId, dev->hid.indiv.deviceId)) - { - OBOS_Log("%02x:%02x:%02x: uHDA device match!\n", - dev->location.bus, dev->location.slot, dev->location.function - ); - UhdaController* controller = nullptr; - if (uhda_init(dev, &controller) == UHDA_STATUS_SUCCESS) - { - Drv_uHDAControllers = Reallocate(OBOS_KernelAllocator, - Drv_uHDAControllers, - (Drv_uHDAControllerCount+1)*sizeof(*Drv_uHDAControllers), - Drv_uHDAControllerCount*sizeof(*Drv_uHDAControllers), - nullptr); - - Drv_uHDAControllersLocations = Reallocate(OBOS_KernelAllocator, - Drv_uHDAControllersLocations, - (Drv_uHDAControllerCount+1)*sizeof(*Drv_uHDAControllersLocations), - Drv_uHDAControllerCount*sizeof(*Drv_uHDAControllersLocations), - nullptr); - Drv_uHDAControllers[Drv_uHDAControllerCount++] = controller; - Drv_uHDAControllersLocations[Drv_uHDAControllerCount - 1] = dev->location; - } - } - - dev = LIST_GET_NEXT(pci_device_list, &Drv_PCIBuses[bus].devices, dev); - } - } - - OBOS_InitializeHDAAudioDev(); - - return OBOS_STATUS_SUCCESS; -} -#else -obos_status Drv_PnpLoad_uHDA() -{ - return OBOS_STATUS_UNIMPLEMENTED; -} -#endif \ No newline at end of file diff --git a/src/oboskrnl/driver_interface/pnp.h b/src/oboskrnl/driver_interface/pnp.h deleted file mode 100644 index 11bdb3df..00000000 --- a/src/oboskrnl/driver_interface/pnp.h +++ /dev/null @@ -1,21 +0,0 @@ -/* - * oboskrnl/driver_interface/pnp.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include -#include - -#include - -// every driver header in 'toLoad' is one found in 'what' -// nodes are allocated using the general purpose kernel allocator. -obos_status Drv_PnpDetectDrivers(driver_header_list what, driver_header_list *toLoad); -obos_status Drv_PnpLoadDriversAt(dirent* directory, bool wait); -obos_status Drv_PnpLoad_uHDA(); \ No newline at end of file diff --git a/src/oboskrnl/elf/elf.h b/src/oboskrnl/elf/elf.h deleted file mode 100644 index d8bcb2fe..00000000 --- a/src/oboskrnl/elf/elf.h +++ /dev/null @@ -1,7 +0,0 @@ -#if OBOS_ARCHITECTURE_BITS == 64 -# include -#elif OBOS_ARCHITECTURE_BITS == 32 -# include -#else -# error Unsupported OBOS_ARCHITECTURE_BITS value for elf. -#endif diff --git a/src/oboskrnl/elf/elf32.h b/src/oboskrnl/elf/elf32.h deleted file mode 100644 index 29afaded..00000000 --- a/src/oboskrnl/elf/elf32.h +++ /dev/null @@ -1,287 +0,0 @@ -/* - * oboskrnl/elf/elf64.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include - -#define ELFMAG0 0x7f -#define ELFMAG1 'E' -#define ELFMAG2 'L' -#define ELFMAG3 'F' - -#define ELFCLASSNONE 0 -#define ELFCLASS32 1 -#define ELFCLASS64 2 - -#define ELFDATANONE 0 -#define ELFDATA2LSB 1 -#define ELFDATA2MSB 2 - -#define EV_NONE 0 -#define EV_CURRENT 1 - -#define EI_NIDENT 16 -#define EI_MAG0 0 -#define EI_MAG1 1 -#define EI_MAG2 2 -#define EI_MAG3 3 -#define EI_CLASS 4 -#define EI_DATA 5 -// Must be EV_CURRENT. -#define EI_VERSION 6 -#define EI_PAD 7 - -#define PF_X 0x1 -#define PF_W 0x2 -#define PF_R 0x4 -#define PF_OBOS_PAGEABLE 0x00010000 /* in PF_MASKOS */ -#define PF_MASKOS 0x00FF0000 - -typedef uintptr_t Elf32_Addr; -typedef uint16_t Elf32_Half; -typedef uintptr_t Elf32_Off; -typedef uint32_t Elf32_Word; -typedef int32_t Elf32_Sword; - -enum -{ - PT_NULL, - PT_LOAD, - PT_DYNAMIC, - PT_INTERP, - PT_NOTE, - PT_SHLIB, - PT_PHDR -}; - -enum -{ - ET_NONE, - ET_REL, - ET_EXEC, - ET_DYN, - ET_CORE, - ET_LOPROC = 0xff00, - ET_HIPROC = 0xffff, -}; - -enum -{ - SHT_NULL, - SHT_PROGBITS, - SHT_SYMTAB, - SHT_STRTAB, - SHT_RELA, - SHT_HASH, - SHT_DYNAMIC, - SHT_NOTE, - SHT_NOBITS, - SHT_REL, - SHT_SHLIB, - SHT_DYNSYM, - SHT_INIT_ARRAY, - SHT_FINI_ARRAY, - SHT_PREINIT_ARRAY, - SHT_GROUP, - SHT_SYMTAB_SHNDX, -}; - -enum -{ - STB_LOCAL, - STB_GLOBAL, - STB_WEAK, - STB_LOOS, - STB_HIOS, -}; - -enum -{ - STT_NOTYPE, - STT_OBJECT, - STT_FUNC, - STT_SECTION, - STT_FILE, - STT_COMMON, - STT_TLS, - STT_LOOS, - STT_HIOS, - STT_LOPROC, - STT_HIPROC, -}; - -enum -{ - STV_DEFAULT, - STV_INTERNAL, - STV_HIDDEN, - STV_PROTECTED, - STV_EXPORTED, - STV_SINGLETON, - STV_ELIMINATE, -}; - -enum -{ - EM_68K = 4, -#ifdef __m68k__ - EM_CURRENT = EM_68K, -#endif -}; - -#define ELF32_ST_BIND(info) ((info) >> 4) -#define ELF32_ST_TYPE(info) ((info) & 0xf) -#define ELF32_ST_INFO(bind, type) (((bind)<<4)+((type)&0xf)) -#define ELF_ST_BIND(info) ELF32_ST_BIND(info) -#define ELF_ST_TYPE(info) ELF32_ST_TYPE(info) -#define ELF_ST_INFO(bind, type) ELF32_ST_INFO(bind, type) - -typedef struct Elf32_Shdr -{ - Elf32_Word sh_name; - Elf32_Word sh_type; - Elf32_Word sh_flags; - Elf32_Addr sh_addr; - Elf32_Off sh_offset; - Elf32_Word sh_size; - Elf32_Word sh_link; - Elf32_Word sh_info; - Elf32_Word sh_addralign; - Elf32_Word sh_entsize; -} Elf32_Shdr, Elf_Shdr; - -typedef struct Elf32_Phdr -{ - Elf32_Word p_type; - Elf32_Off p_offset; - Elf32_Addr p_vaddr; - Elf32_Addr p_paddr; - Elf32_Word p_filesz; - Elf32_Word p_memsz; - Elf32_Word p_flags; - Elf32_Word p_align; -} ELf32_Phdr, Elf_Phdr; - -typedef struct Elf32_Sym -{ - Elf32_Word st_name; - Elf32_Addr st_value; - Elf32_Word st_size; - unsigned char st_info; - unsigned char st_other; - Elf32_Half st_shndx; -} Elf32_Sym, Elf_Sym; - -typedef struct { - Elf32_Addr r_offset; - Elf32_Word r_info; -} Elf32_Rel, Elf_Rel; - -typedef struct { - Elf32_Addr r_offset; - Elf32_Word r_info; - Elf32_Sword r_addend; -} Elf32_Rela, Elf_Rela; - -typedef struct -{ - unsigned char e_ident[EI_NIDENT]; - Elf32_Half e_type; - Elf32_Half e_machine; - Elf32_Word e_version; - Elf32_Addr e_entry; - Elf32_Off e_phoff; - Elf32_Off e_shoff; - Elf32_Word e_flags; - Elf32_Half e_ehsize; - Elf32_Half e_phentsize; - Elf32_Half e_phnum; - Elf32_Half e_shentsize; - Elf32_Half e_shnum; - Elf32_Half e_shstrndx; -} Elf32_Ehdr, Elf_Ehdr; - -typedef struct Elf32_Dyn -{ - Elf32_Sword d_tag; - union - { - Elf32_Word d_val; - Elf32_Addr d_ptr; - } d_un; -} Elf32_Dyn, Elf_Dyn; - -#ifdef __m68k__ -// refer to sysv-m68k-abi-part3.pdf page 6 for info on relocations -enum -{ - R_68K_NONE, - R_68K_32, - R_68K_16, - R_68K_8, - R_68K_PC32, - R_68K_PC16, - R_68K_PC8, - R_68K_GOT32, - R_68K_GOT16, - R_68K_GOT8, - R_68K_GOT320, - R_68K_GOT160, - R_68K_GOT80, - R_68K_PLT32, - R_68K_PLT16, - R_68K_PLT8, - R_68K_PLT320, - R_68K_PLT160, - R_68K_PLT80, - R_68K_COPY, - R_68K_GLOB_DAT, - R_68K_JUMP_SLOT, - R_68K_RELATIVE, -}; -#endif - -// maybe I shouldn't copy this from elf64 -enum -{ - DT_NULL, - DT_NEEDED, - DT_PLTRELSZ, - DT_PLTGOT, - DT_HASH, - DT_STRTAB, - DT_SYMTAB, - DT_RELA, - DT_RELASZ, - DT_RELAENT, - DT_STRSZ, - DT_SYMENT, - DT_INIT, - DT_FINI, - DT_SONAME, - DT_RPATH, - DT_SYMBOLIC, - DT_REL, - DT_RELSZ, - DT_RELENT, - DT_PLTREL, - DT_DEBUG, - DT_TEXTREL, - DT_JMPREL, - DT_BIND_NOW, - DT_INIT_ARRAY, - DT_FINI_ARRAY, - DT_INIT_ARRAYSZ, - DT_FINI_ARRAYSZ, - DT_RUNPATH, - DT_FLAGS, - DT_ENCODING, - DT_PREINIT_ARRAY, - - DT_RELACOUNT = 0x6ffffff9, - DT_RELCOUNT, -}; \ No newline at end of file diff --git a/src/oboskrnl/elf/elf64.h b/src/oboskrnl/elf/elf64.h index 8496174d..d0e97ad9 100644 --- a/src/oboskrnl/elf/elf64.h +++ b/src/oboskrnl/elf/elf64.h @@ -1,281 +1,263 @@ -/* - * oboskrnl/elf/elf64.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include - -#define ELFMAG0 0x7f -#define ELFMAG1 'E' -#define ELFMAG2 'L' -#define ELFMAG3 'F' - -#define ELFCLASSNONE 0 -#define ELFCLASS32 1 -#define ELFCLASS64 2 - -#define ELFDATANONE 0 -#define ELFDATA2LSB 1 -#define ELFDATA2MSB 2 - -#define EV_NONE 0 -#define EV_CURRENT 1 - -#define EI_NIDENT 16 -#define EI_MAG0 0 -#define EI_MAG1 1 -#define EI_MAG2 2 -#define EI_MAG3 3 -#define EI_CLASS 4 -#define EI_DATA 5 -// Must be EV_CURRENT. -#define EI_VERSION 6 -#define EI_PAD 7 - -#define PF_X 0x1 -#define PF_W 0x2 -#define PF_R 0x4 -#define PF_OBOS_PAGEABLE 0x00010000 /* in PF_MASKOS */ -#define PF_MASKOS 0x00FF0000 - -typedef uintptr_t Elf64_Addr; -typedef uintptr_t Elf64_Off; -typedef uint32_t Elf64_Word; -typedef uint64_t Elf64_Qword; -typedef int64_t Elf64_SQword; -typedef uint16_t Elf64_Half; - -enum -{ - PT_NULL, - PT_LOAD, - PT_DYNAMIC, - PT_INTERP, - PT_NOTE, - PT_SHLIB, - PT_PHDR -}; - -enum -{ - ET_NONE, - ET_REL, - ET_EXEC, - ET_DYN, - ET_CORE, - ET_LOPROC = 0xff00, - ET_HIPROC = 0xffff, -}; - -enum -{ - SHT_NULL, - SHT_PROGBITS, - SHT_SYMTAB, - SHT_STRTAB, - SHT_RELA, - SHT_HASH, - SHT_DYNAMIC, - SHT_NOTE, - SHT_NOBITS, - SHT_REL, - SHT_SHLIB, - SHT_DYNSYM, - SHT_INIT_ARRAY, - SHT_FINI_ARRAY, - SHT_PREINIT_ARRAY, - SHT_GROUP, - SHT_SYMTAB_SHNDX, -}; - -enum -{ - STB_LOCAL, - STB_GLOBAL, - STB_WEAK, - STB_LOOS, - STB_HIOS, -}; - -enum -{ - STT_NOTYPE, - STT_OBJECT, - STT_FUNC, - STT_SECTION, - STT_FILE, - STT_COMMON, - STT_TLS, - STT_LOOS, - STT_HIOS, - STT_LOPROC, - STT_HIPROC, -}; - -enum -{ - STV_DEFAULT, - STV_INTERNAL, - STV_HIDDEN, - STV_PROTECTED, - STV_EXPORTED, - STV_SINGLETON, - STV_ELIMINATE, -}; - -#define ELF64_ST_BIND(info) ((info) >> 4) -#define ELF64_ST_TYPE(info) ((info) & 0xf) -#define ELF64_ST_INFO(bind, type) (((bind)<<4)+((type)&0xf)) -#define ELF_ST_BIND(info) ELF64_ST_BIND(info) -#define ELF_ST_TYPE(info) ELF64_ST_TYPE(info) -#define ELF_ST_INFO(bind, type) ELF64_ST_INFO(bind, type) - -enum -{ - EM_X86_64 = 62, - -#ifdef __x86_64__ - EM_CURRENT = EM_X86_64, -#endif -}; - -typedef struct -{ - unsigned char e_ident[EI_NIDENT]; - Elf64_Half e_type; - Elf64_Half e_machine; - Elf64_Word e_version; - Elf64_Addr e_entry; - Elf64_Off e_phoff; - Elf64_Off e_shoff; - Elf64_Word e_flags; - Elf64_Half e_ehsize; - Elf64_Half e_phentsize; - Elf64_Half e_phnum; - Elf64_Half e_shentsize; - Elf64_Half e_shnum; - Elf64_Half e_shstrndx; -} Elf64_Ehdr, Elf_Ehdr; - -typedef struct -{ - Elf64_Word p_type; - Elf64_Word p_flags; - Elf64_Off p_offset; - Elf64_Addr p_vaddr; - Elf64_Addr p_paddr; - Elf64_Qword p_filesz; - Elf64_Qword p_memsz; - Elf64_Qword p_align; -} Elf64_Phdr, Elf_Phdr; - -typedef struct -{ - Elf64_Word sh_name; - Elf64_Word sh_type; - Elf64_Qword sh_flags; - Elf64_Addr sh_addr; - Elf64_Off sh_offset; - Elf64_Qword sh_size; - Elf64_Word sh_link; - Elf64_Word sh_info; - Elf64_Qword sh_addralign; - Elf64_Qword sh_entsize; -} Elf64_Shdr, Elf_Shdr; - -typedef struct -{ - Elf64_Word st_name; - unsigned char st_info; - unsigned char st_other; - Elf64_Half st_shndx; - Elf64_Addr st_value; - Elf64_Qword st_size; -} Elf64_Sym, Elf_Sym; - -typedef struct { - Elf64_Addr r_offset; - Elf64_Qword r_info; -} Elf64_Rel, Elf_Rel; - -typedef struct { - Elf64_Addr r_offset; - Elf64_Qword r_info; - Elf64_SQword r_addend; -} Elf64_Rela, Elf_Rela; - -typedef struct { - Elf64_Qword d_tag; - union { - Elf64_Qword d_val; - Elf64_Addr d_ptr; - } d_un; -} Elf64_Dyn, Elf_Dyn; - -enum -{ - DT_NULL, - DT_NEEDED, - DT_PLTRELSZ, - DT_PLTGOT, - DT_HASH, - DT_STRTAB, - DT_SYMTAB, - DT_RELA, - DT_RELASZ, - DT_RELAENT, - DT_STRSZ, - DT_SYMENT, - DT_INIT, - DT_FINI, - DT_SONAME, - DT_RPATH, - DT_SYMBOLIC, - DT_REL, - DT_RELSZ, - DT_RELENT, - DT_PLTREL, - DT_DEBUG, - DT_TEXTREL, - DT_JMPREL, - DT_BIND_NOW, - DT_INIT_ARRAY, - DT_FINI_ARRAY, - DT_INIT_ARRAYSZ, - DT_FINI_ARRAYSZ, - DT_RUNPATH, - DT_FLAGS, - DT_ENCODING, - DT_PREINIT_ARRAY, - DT_GNU_HASH = 0x6ffffef5, -}; - -#ifdef __x86_64__ -enum -{ - R_AMD64_NONE, - R_AMD64_64, - R_AMD64_PC32, - R_AMD64_GOT32, - R_AMD64_PLT32, - R_AMD64_COPY, - R_AMD64_GLOB_DAT, - R_AMD64_JUMP_SLOT, - R_AMD64_RELATIVE, - R_AMD64_GOTPCREL, - R_AMD64_32, - R_AMD64_32S, - R_AMD64_16, - R_AMD64_PC16, - R_AMD64_8, - R_AMD64_PC8, - R_AMD64_PC64 = 24, - R_AMD64_GOTOFF64 = 25, - R_AMD64_GOTPC32 = 26, - R_AMD64_SIZE32 = 32, - R_AMD64_SIZE64 = 33, -}; -#endif +/* + oboskrnl/elf/elf64.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include + +namespace obos +{ + namespace elf + { + constexpr uint8_t ELFMAG0 = 0x7f; + constexpr uint8_t ELFMAG1 = 'E'; + constexpr uint8_t ELFMAG2 = 'L'; + constexpr uint8_t ELFMAG3 = 'F'; + + constexpr uint8_t ELFCLASSNONE = 0; + constexpr uint8_t ELFCLASS32 = 1; + constexpr uint8_t ELFCLASS64 = 2; + + constexpr uint8_t ELFDATANONE = 0; + constexpr uint8_t ELFDATA2LSB = 1; + constexpr uint8_t ELFDATA2MSB = 2; + + constexpr uint8_t EV_NONE = 0; + constexpr uint8_t EV_CURRENT = 1; + + constexpr uint32_t EI_NIDENT = 16; + constexpr uint32_t EI_MAG0 = 0; + constexpr uint32_t EI_MAG1 = 1; + constexpr uint32_t EI_MAG2 = 2; + constexpr uint32_t EI_MAG3 = 3; + constexpr uint32_t EI_CLASS = 4; + constexpr uint32_t EI_DATA = 5; + // Must be EV_CURRENT. + constexpr uint32_t EI_VERSION = 6; + constexpr uint32_t EI_PAD = 7; + + constexpr uint32_t PF_R = 0x4; + constexpr uint32_t PF_W = 0x2; + constexpr uint32_t PF_X = 0x1; + + typedef uintptr_t Elf64_Addr; + typedef uintptr_t Elf64_Off; + typedef uint32_t Elf64_Word; + typedef uint64_t Elf64_Qword; + typedef int64_t Elf64_SQword; + typedef uint16_t Elf64_Half; + + enum + { + PT_NULL, + PT_LOAD, + PT_DYNAMIC, + PT_INTERP, + PT_NOTE, + PT_SHLIB, + PT_PHDR + }; + + enum + { + ET_NONE, + ET_REL, + ET_EXEC, + ET_DYN, + ET_CORE, + ET_LOPROC = 0xff00, + ET_HIPROC = 0xffff, + }; + + enum + { + SHT_NULL, + SHT_PROGBITS, + SHT_SYMTAB, + SHT_STRTAB, + SHT_RELA, + SHT_HASH, + SHT_DYNAMIC, + SHT_NOTE, + SHT_NOBITS, + SHT_REL, + SHT_SHLIB, + SHT_DYNSYM, + SHT_INIT_ARRAY, + SHT_FINI_ARRAY, + SHT_PREINIT_ARRAY, + SHT_GROUP, + SHT_SYMTAB_SHNDX, + }; + + enum + { + STB_LOCAL, + STB_GLOBAL, + STB_WEAK, + STB_LOOS, + STB_HIOS, + }; + + enum + { + STT_NOTYPE, + STT_OBJECT, + STT_FUNC, + STT_SECTION, + STT_FILE, + STT_COMMON, + STT_TLS, + STT_LOOS, + STT_HIOS, + STT_LOPROC, + STT_HIPROC, + }; + + enum + { + EM_X86_64 = 62, + // TODO: Add more machine types. + }; + + struct Elf64_Ehdr + { + unsigned char e_ident[EI_NIDENT]; + Elf64_Half e_type; + Elf64_Half e_machine; + Elf64_Word e_version; + Elf64_Addr e_entry; + Elf64_Off e_phoff; + Elf64_Off e_shoff; + Elf64_Word e_flags; + Elf64_Half e_ehsize; + Elf64_Half e_phentsize; + Elf64_Half e_phnum; + Elf64_Half e_shentsize; + Elf64_Half e_shnum; + Elf64_Half e_shstrndx; + }; + + struct Elf64_Phdr + { + Elf64_Word p_type; + Elf64_Word p_flags; + Elf64_Off p_offset; + Elf64_Addr p_vaddr; + Elf64_Addr p_paddr; + Elf64_Qword p_filesz; + Elf64_Qword p_memsz; + Elf64_Qword p_align; + }; + + struct Elf64_Shdr + { + Elf64_Word sh_name; + Elf64_Word sh_type; + Elf64_Qword sh_flags; + Elf64_Addr sh_addr; + Elf64_Off sh_offset; + Elf64_Qword sh_size; + Elf64_Word sh_link; + Elf64_Word sh_info; + Elf64_Qword sh_addralign; + Elf64_Qword sh_entsize; + }; + + struct Elf64_Sym + { + Elf64_Word st_name; + unsigned char st_info; + unsigned char st_other; + Elf64_Half st_shndx; + Elf64_Addr st_value; + Elf64_Qword st_size; + }; + + typedef struct { + Elf64_Addr r_offset; + Elf64_Qword r_info; + } Elf64_Rel; + + typedef struct { + Elf64_Addr r_offset; + Elf64_Qword r_info; + Elf64_SQword r_addend; + } Elf64_Rela; + + typedef struct { + Elf64_Qword d_tag; + union { + Elf64_Qword d_val; + Elf64_Addr d_ptr; + } d_un; + } Elf64_Dyn; + + enum + { + DT_NULL, + DT_NEEDED, + DT_PLTRELSZ, + DT_PLTGOT, + DT_HASH, + DT_STRTAB, + DT_SYMTAB, + DT_RELA, + DT_RELASZ, + DT_RELAENT, + DT_STRSZ, + DT_SYMENT, + DT_INIT, + DT_FINI, + DT_SONAME, + DT_RPATH, + DT_SYMBOLIC, + DT_REL, + DT_RELSZ, + DT_RELENT, + DT_PLTREL, + DT_DEBUG, + DT_TEXTREL, + DT_JMPREL, + DT_BIND_NOW, + DT_INIT_ARRAY, + DT_FINI_ARRAY, + DT_INIT_ARRAYSZ, + DT_FINI_ARRAYSZ, + DT_RUNPATH, + DT_FLAGS, + DT_ENCODING, + DT_PREINIT_ARRAY, + }; + +#ifdef __x86_64__ + enum + { + R_AMD64_NONE, + R_AMD64_64, + R_AMD64_PC32, + R_AMD64_GOT32, + R_AMD64_PLT32, + R_AMD64_COPY, + R_AMD64_GLOB_DAT, + R_AMD64_JUMP_SLOT, + R_AMD64_RELATIVE, + R_AMD64_GOTPCREL, + R_AMD64_32, + R_AMD64_32S, + R_AMD64_16, + R_AMD64_PC16, + R_AMD64_8, + R_AMD64_PC8, + R_AMD64_PC64 = 24, + R_AMD64_GOTOFF64 = 25, + R_AMD64_GOTPC32 = 26, + R_AMD64_SIZE32 = 32, + R_AMD64_SIZE64 = 33, + }; +#endif + } +} \ No newline at end of file diff --git a/src/oboskrnl/elf/load.c b/src/oboskrnl/elf/load.c deleted file mode 100644 index ebc69f37..00000000 --- a/src/oboskrnl/elf/load.c +++ /dev/null @@ -1,257 +0,0 @@ -/* - * oboskrnl/elf/load.c - * - * Copyright (c) 2024-2025 Omar Berrow - */ - -#include -#include -#include -#include - -#include -#include - -#include -#include - -#include -#include -#include - -// Do it in two passes so that any macros can be expanded -#define token_concat_impl(tok1, tok2) tok1 ##tok2 -#define token_concat(tok1, tok2) token_concat_impl(tok1, tok2) -#define GetCurrentElfClass() token_concat(ELFCLASS, OBOS_ARCHITECTURE_BITS) - -static obos_status verify_elf(const void* file, size_t szFile) -{ - const Elf_Ehdr* ehdr = file; - if (ehdr->e_ident[EI_MAG0] != ELFMAG0 || ehdr->e_ident[EI_MAG1] != ELFMAG1 || - ehdr->e_ident[EI_MAG2] != ELFMAG2 || ehdr->e_ident[EI_MAG3] != ELFMAG3) - return OBOS_STATUS_INVALID_FILE; - - if (ehdr->e_ident[EI_CLASS] != GetCurrentElfClass()) - return OBOS_STATUS_INVALID_FILE; - - uint8_t ei_data_val = 0; - if (strcmp(OBOS_ARCHITECTURE_ENDIANNESS, "Little-Endian")) - ei_data_val = ELFDATA2LSB; - else if (strcmp(OBOS_ARCHITECTURE_ENDIANNESS, "Big-Endian")) - ei_data_val = ELFDATA2MSB; - else - ei_data_val = ELFDATANONE; - if (ehdr->e_ident[EI_DATA] != ei_data_val) - return OBOS_STATUS_INVALID_FILE; - - if (ehdr->e_machine != EM_CURRENT) - return OBOS_STATUS_INVALID_FILE; - - if ((ehdr->e_phoff > szFile) || ((ehdr->e_phoff+ehdr->e_phentsize*ehdr->e_phnum) > szFile)) - return OBOS_STATUS_INVALID_FILE; - - return OBOS_STATUS_SUCCESS; -} - -obos_status OBOS_LoadELF(context* ctx, const void* file, size_t szFile, elf_info* info, bool dryRun, bool noLdr) -{ - if (!ctx || !file || !szFile) - return OBOS_STATUS_INVALID_ARGUMENT; - if (!info && !dryRun) - return OBOS_STATUS_INVALID_ARGUMENT; - - obos_status status = verify_elf(file, szFile); - if (obos_is_error(status)) - return status; - - const Elf_Ehdr* const ehdr = file; - - bool load_dynld = false; - - switch (ehdr->e_type) { - case ET_DYN: - case ET_EXEC: - load_dynld = !noLdr; - break; - default: - return OBOS_STATUS_INVALID_FILE; - } - - load: - (void)0; // thanks clang - uintptr_t limit = 0; - uintptr_t base = 0; - bool first_load = true; - const Elf_Phdr* phdrs = (Elf_Phdr*)((const char*)file+ehdr->e_phoff); - for (size_t i = 0; i < ehdr->e_phnum; i++) - { - if (phdrs[i].p_type != PT_LOAD) - continue; - if (phdrs[i].p_offset > szFile) - return OBOS_STATUS_INVALID_FILE; - if ((phdrs[i].p_offset + phdrs[i].p_filesz) > szFile) - return OBOS_STATUS_INVALID_FILE; - if ((phdrs[i].p_vaddr+ phdrs[i].p_memsz) > limit) - limit = phdrs[i].p_vaddr + phdrs[i].p_memsz; - if (phdrs[i].p_vaddr <= base || first_load) - base = phdrs[i].p_vaddr; - first_load = false; - } - - if (dryRun) - goto dry; - - uintptr_t real_entry = 0; - - limit += (OBOS_PAGE_SIZE-(limit%OBOS_PAGE_SIZE)); - base -= (base%OBOS_PAGE_SIZE); - - bool require_addend = false; - if (!base) - { - obos_status status = OBOS_STATUS_SUCCESS; - base = (uintptr_t)Mm_VirtualMemoryAlloc(ctx, nullptr, limit-base, OBOS_PROTECTION_USER_PAGE, 0, nullptr, &status); - if (!base) - return status; - require_addend = true; - limit += base; - } - - for (size_t i = 0; i < ehdr->e_phnum; i++) - { - if (phdrs[i].p_type != PT_LOAD) - continue; - void* kbase = nullptr; - void* ubase = nullptr; - obos_status status = OBOS_STATUS_SUCCESS; - if (!require_addend) - { - uintptr_t real_base = phdrs[i].p_vaddr; - real_base -= (real_base%OBOS_PAGE_SIZE); - uintptr_t real_limit = phdrs[i].p_vaddr+phdrs[i].p_memsz; - if (real_limit % OBOS_PAGE_SIZE) - real_limit += (OBOS_PAGE_SIZE-(real_limit%OBOS_PAGE_SIZE)); - // printf("ELF: Allocating 0x%p bytes (memsz=0x%p, real_base=0x%p, real_limit=0x%p, vaddr=0x%p)\n", real_limit-real_base, phdrs[i].p_memsz, real_base, real_limit, phdrs[i].p_vaddr); - ubase = Mm_VirtualMemoryAlloc(ctx, - (void*)real_base, - real_limit-real_base, - OBOS_PROTECTION_USER_PAGE, 0, nullptr, - &status) + (phdrs[i].p_vaddr%OBOS_PAGE_SIZE); - } - else - ubase = (void*)(phdrs[i].p_vaddr + base); - if (obos_is_error(status)) - { - // Clean up. - for (size_t j = 0; j < i; j++) - Mm_VirtualMemoryFree(ctx, (void*)phdrs[j].p_vaddr, phdrs[j].p_memsz); - return status; - } - - uintptr_t real_base = phdrs[i].p_vaddr; - real_base -= (real_base%OBOS_PAGE_SIZE); - uintptr_t real_limit = phdrs[i].p_vaddr+phdrs[i].p_memsz; - if (real_limit % OBOS_PAGE_SIZE) - real_limit += (OBOS_PAGE_SIZE-(real_limit%OBOS_PAGE_SIZE)); - if (require_addend) - { - real_base += base; - real_limit += base; - } - - OBOS_ENSURE(phdrs[i].p_filesz <= (real_limit - real_base)); - - kbase = Mm_MapViewOfUserMemory(ctx, - (void*)real_base, - nullptr, - real_limit-real_base, - 0, - false, // disrespect user protection flags. - &status) + (phdrs[i].p_vaddr % OBOS_PAGE_SIZE); - if (obos_is_error(status)) - { - // Clean up. - for (size_t j = 0; j < i; j++) - Mm_VirtualMemoryFree(ctx, (void*)phdrs[j].p_vaddr, phdrs[j].p_memsz); - return status; - } - - memcpy(kbase, (void*)((uintptr_t)file + phdrs[i].p_offset), phdrs[i].p_filesz); - if (phdrs[i].p_memsz-phdrs[i].p_filesz) - memzero((void*)((uintptr_t)kbase+phdrs[i].p_filesz), phdrs[i].p_memsz-phdrs[i].p_filesz); - Mm_VirtualMemoryFree(&Mm_KernelContext, kbase, phdrs[i].p_memsz); - - prot_flags prot = OBOS_PROTECTION_USER_PAGE; - if (phdrs[i].p_flags & PF_X) - prot |= OBOS_PROTECTION_EXECUTABLE; - if (~phdrs[i].p_flags & PF_W) - prot |= OBOS_PROTECTION_READ_ONLY; - - status = Mm_VirtualMemoryProtect(ctx, ubase, phdrs[i].p_memsz, prot, true); - if (obos_is_error(status)) - { - // Clean up. - for (size_t j = 0; j < i; j++) - Mm_VirtualMemoryFree(ctx, (void*)phdrs[j].p_vaddr, phdrs[j].p_memsz); - return status; - } - } - - if (load_dynld) - { - const Elf_Phdr* phdrs = (Elf_Phdr*)((const char*)file+ehdr->e_phoff); - const char* interp = nullptr; - for (size_t i = 0; i < ehdr->e_phnum; i++) - { - if (phdrs[i].p_type == PT_INTERP) - { - interp = (char*)file+phdrs[i].p_offset; - break; - } - } - - if (!interp) - { - load_dynld = false; - goto load; - } - - fd interp_fd = {}; - status = Vfs_FdOpen(&interp_fd, interp, FD_OFLAGS_READ); - if (obos_is_error(status)) - return status; - - // Load the interpreter instead, but also make sure to sanity check it. - // Assume the interpreter doesn't need an interpreter, that'd be pretty dumb. - - if (!VfsH_LockMountpoint(interp_fd.vn->mount_point)) - return OBOS_STATUS_INTERNAL_ERROR; - volatile size_t buff_size = interp_fd.vn->filesize; - VfsH_UnlockMountpoint(interp_fd.vn->mount_point); - - status = OBOS_STATUS_SUCCESS; - void* buff = Mm_VirtualMemoryAlloc(&Mm_KernelContext, nullptr, buff_size, OBOS_PROTECTION_READ_ONLY, VMA_FLAGS_PRIVATE|VMA_FLAGS_PREFAULT, &interp_fd, &status); - if (obos_is_error(status)) - { - Vfs_FdClose(&interp_fd); - return status; - } - Vfs_FdClose(&interp_fd); - - elf_info tmp_info = {}; - status = OBOS_LoadELF(ctx, buff, buff_size, &tmp_info, dryRun, true); - if (obos_is_error(status)) - return status; - info->rtld_base = tmp_info.base; - real_entry = tmp_info.entry; - Mm_VirtualMemoryFree(&Mm_KernelContext, (void*)buff, buff_size); - } - - info->base = (void*)base; - info->entry = (require_addend ? base : 0) + ehdr->e_entry; - info->real_entry = real_entry ? real_entry : info->entry; - - dry: - - return OBOS_STATUS_SUCCESS; -} diff --git a/src/oboskrnl/elf/load.h b/src/oboskrnl/elf/load.h deleted file mode 100644 index 2dc91df5..00000000 --- a/src/oboskrnl/elf/load.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * oboskrnl/elf/load.h - * - * Copyright (c) 2024 Omar Berrow - */ - -#pragma once - -#include -#include - -#include - -typedef struct elf_info { - void* base; - void* rtld_base; - uintptr_t entry; - uintptr_t real_entry; -} elf_info; - -// if dryRun is true, nothing is mapped, but checks are still made on the ELF. -// noLdr should always be false unless you know what you're doing. -obos_status OBOS_LoadELF(context* ctx, const void* file, size_t szFile, elf_info* info, bool dryRun, bool noLdr); diff --git a/src/oboskrnl/error.h b/src/oboskrnl/error.h deleted file mode 100644 index daef8078..00000000 --- a/src/oboskrnl/error.h +++ /dev/null @@ -1,216 +0,0 @@ -/* - * oboskrnl/error.h - * - * Copyright (c) 2024-2026 Omar Berrow -*/ - -#pragma once - -#define obos_is_error(status) ((status) != OBOS_STATUS_SUCCESS) -#define obos_is_success(status) ((status) == OBOS_STATUS_SUCCESS) - -typedef enum -{ - /// - /// The function succeeded. - /// - OBOS_STATUS_SUCCESS, - /// - /// An invalid IRQL value was passed to the function. - /// - OBOS_STATUS_INVALID_IRQL, - /// - /// An invalid argument was passed to the function. - /// - OBOS_STATUS_INVALID_ARGUMENT, - /// - /// A function was called, but an option passed to it had not been implemented yet, or - /// the function itself had not been implemented yet. - /// - OBOS_STATUS_UNIMPLEMENTED, - /// - /// A function was called before one of the prerequisite components was initialized. - /// - OBOS_STATUS_INVALID_INIT_PHASE, - /// - /// The affinity in the thread object is invalid. - /// - OBOS_STATUS_INVALID_AFFINITY, - /// - /// There is not enough memory to satisfy your request. - /// - OBOS_STATUS_NOT_ENOUGH_MEMORY, - /// - /// A mismatched pointer was passed. - /// - OBOS_STATUS_MISMATCH, - /// - /// An internal error happened. - /// - OBOS_STATUS_INTERNAL_ERROR, - /// - /// An error occurred, and a retry of the operation is needed. - /// - OBOS_STATUS_RETRY, - /// - /// The object is already initialized. - /// - OBOS_STATUS_ALREADY_INITIALIZED, - /// - /// The request could not be fulfilled, as a required resource was not found. - /// - OBOS_STATUS_NOT_FOUND, - /// - /// The resource is already in use. - /// - OBOS_STATUS_IN_USE, - /// - /// Access has been denied. - /// - OBOS_STATUS_ACCESS_DENIED, - /// - /// The object has not been initialized yet. - /// - OBOS_STATUS_UNINITIALIZED, - /// - /// The callback did not handle anything. - /// - OBOS_STATUS_UNHANDLED, - /// - /// The page cannot be paged in or out, as it's in the non-paged pool of memory. - /// - OBOS_STATUS_UNPAGED_POOL, - /// - /// The file has an invalid format. - /// - OBOS_STATUS_INVALID_FILE, - /// - /// The driver put an invalid value for a field in its header. - /// - OBOS_STATUS_INVALID_HEADER, - /// - /// The driver's code referenced an undefined symbol in the kernel. - /// - OBOS_STATUS_DRIVER_REFERENCED_UNRESOLVED_SYMBOL, - /// - /// The driver's code referenced a variable that had a different size in the kernel. - /// - OBOS_STATUS_DRIVER_SYMBOL_MISMATCH, - /// - /// The driver does not have an entry point. - /// - OBOS_STATUS_NO_ENTRY_POINT, - /// - /// The caller of driver->header.ftable.ioctl gave an invalid request. - /// - OBOS_STATUS_INVALID_IOCTL, - /// - /// The driver received an invalid operation. - /// An example is if a pipe-style device called get_max_blk_count. - /// - OBOS_STATUS_INVALID_OPERATION, - /// - /// The DPC object was already enqueued before the call of the function. - /// - OBOS_STATUS_DPC_ALREADY_ENQUEUED, - /// - /// The lock was already locked by the current thread. - /// - OBOS_STATUS_RECURSIVE_LOCK, - /// - /// The driver cannot satisfy the write request, as the device/filesystem it's manipulating is read only. - /// - OBOS_STATUS_READ_ONLY, - /// - /// The filesystem driver cannot satisfy the request, as the descriptor passed was not a file. - /// - OBOS_STATUS_NOT_A_FILE, - /// - /// The vnode already had a node mounted there. - /// - OBOS_STATUS_ALREADY_MOUNTED, - /// - /// The fd object hit EOF. - /// - OBOS_STATUS_EOF, - /// - /// The operation was aborted. - /// - OBOS_STATUS_ABORTED, - /// - /// A page fault happened accessing user memory passed in a syscall or anywhere similar. - /// - OBOS_STATUS_PAGE_FAULT, - /// - /// A driver timed out. - /// - OBOS_STATUS_TIMED_OUT, - /// - /// The pipe has no more readers. - /// Only returned if SIGPIPE is ignored by a process. - /// - OBOS_STATUS_PIPE_CLOSED, - /// - /// Returned if the write request could not be completed, as there is simply not enough space in the device - /// For example, this can happen if the size of a write to a pipe is greater than the pipe's size. - /// - OBOS_STATUS_NO_SPACE, - /// - /// There is no syscall at this syscall number :( - /// - OBOS_STATUS_NO_SYSCALL, - /// - /// The ACPI device is not capable to wake the system from the requested sleep state. - /// - OBOS_STATUS_WAKE_INCAPABLE, - /// - /// The ELF file has an invalid, or unsupported elf type in it's header. - /// - OBOS_STATUS_INVALID_ELF_TYPE, - /// - /// The operation would block. - /// - OBOS_STATUS_WOULD_BLOCK, - /// - /// The passed file or handle is not a TTY. - /// - OBOS_STATUS_NOT_A_TTY, - /// - /// The IRP operation has not completed, and the caller should wait on the event again. - /// - OBOS_STATUS_IRP_RETRY, - /// - /// The port is in use. - /// - OBOS_STATUS_PORT_IN_USE, - /// - /// The address is in use. - /// - OBOS_STATUS_ADDRESS_IN_USE, - /// - /// The address is not a local address. - /// - OBOS_STATUS_ADDRESS_NOT_AVAILABLE, - /// - /// There was no possible route to the host. - /// - OBOS_STATUS_NO_ROUTE_TO_HOST, - /// - /// The message that was to be sent couldn't fit in one single packet. - /// - OBOS_STATUS_MESSAGE_TOO_BIG, - /// - /// Connection from the peer was refused. Usually caused by ICMPv4 destination - /// unreachable (port unreachable) - /// - OBOS_STATUS_CONNECTION_REFUSED, - /// - /// The seek failed because the object is unseekable. - /// Translates to the ESPIPE errno. - /// - OBOS_STATUS_SEEK_UNALLOWED, - /// - /// The network cannot be reached. - /// - OBOS_STATUS_NETWORK_UNREACHABLE, -} obos_status; diff --git a/src/oboskrnl/execve.c b/src/oboskrnl/execve.c deleted file mode 100644 index 8d18bdcf..00000000 --- a/src/oboskrnl/execve.c +++ /dev/null @@ -1,460 +0,0 @@ -/* - * oboskrnl/execve.c - * - * Copyright (c) 2024-2026 Omar Berrow - */ - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include - -#include -#include -#include -#include - -#include -#include - -#include - -#include -#include - -#include -#include - -#include - -#undef OBOS_CROSSES_PAGE_BOUNDARY - -static bool OBOS_CROSSES_PAGE_BOUNDARY(void* ptr_, size_t sz) -{ - uintptr_t ptr = (uintptr_t)ptr_; - uintptr_t limit = ptr+(sz-1); - limit -= (limit % OBOS_PAGE_SIZE); - ptr -= (ptr % OBOS_PAGE_SIZE); - return ptr != limit; -} - -// vec is terminated with a nullptr entry. -static char** map_user_vector(context* ctx, char* const* vec, size_t* const szvec, obos_status* status) -{ - char** kstr = Mm_MapViewOfUserMemory(ctx, (void*)((uintptr_t)vec - ((uintptr_t)vec % OBOS_PAGE_SIZE)), nullptr, OBOS_PAGE_SIZE, OBOS_PROTECTION_READ_ONLY, true, status); - if (!kstr) - return nullptr; - kstr = (void*)(((uintptr_t)kstr) + (uintptr_t)vec % OBOS_PAGE_SIZE); - - char** iter = kstr; - char* const* uiter = vec; - OBOS_UNUSED(uiter); - uintptr_t offset = (uintptr_t)kstr-(uintptr_t)iter; - size_t currSize = OBOS_PAGE_SIZE; - - while ((*iter++) != 0) - { - if (OBOS_CROSSES_PAGE_BOUNDARY(iter, sizeof(*iter))) - { - Mm_VirtualMemoryFree(&Mm_KernelContext, (void*)kstr, currSize); - currSize += OBOS_PAGE_SIZE; - kstr = Mm_MapViewOfUserMemory(ctx, (void*)((uintptr_t)vec + (offset % OBOS_PAGE_SIZE) + sizeof(*iter)), nullptr, currSize, OBOS_PROTECTION_READ_ONLY, true, status); - if (!kstr) - return nullptr; - iter = (char**)((uintptr_t)kstr + (offset % OBOS_PAGE_SIZE)); - } - offset += sizeof(*iter); - (*szvec)++; - uiter++; - } - - return kstr; -} - -static char** get_user_string_vector(context* ctx, char* const* vec, size_t count, obos_status* statusp) -{ - OBOS_UNUSED(ctx); - - char** ret = ZeroAllocate(OBOS_KernelAllocator, count+1, sizeof(char*), statusp); - if (!ret) - return nullptr; - - for (size_t i = 0; i < count; i++) - { - size_t str_len = 0; - if (!vec[i]) - { - ret[i] = 0; - break; - } - obos_status status = OBOSH_ReadUserString(vec[i], nullptr, &str_len); - if (obos_is_error(status)) - { - Free(OBOS_KernelAllocator, ret, (count + 1) * sizeof(char*)); - if (statusp) *statusp = status; - return nullptr; - } - char* buf = Allocate(OBOS_KernelAllocator, str_len+1, nullptr); - OBOSH_ReadUserString(vec[i], buf, &str_len); - buf[str_len] = 0; - ret[i] = buf; - } - Mm_VirtualMemoryFree(&Mm_KernelContext, (void*)vec, count*sizeof(char*)); - return ret; -} - -static void free_user_string_vector(char** vec, size_t count) -{ - for (size_t i = 0; i < count; i++) - { - Free(OBOS_KernelAllocator, vec[i], strlen(vec[i]) + 1); - vec[i] = nullptr; - } - Free(OBOS_KernelAllocator, vec, (count+1) * sizeof(*vec)); -} - -bool isspace(char c) -{ - return c == ' ' || - c == '\t' || - c == '\v' || - c == '\f' || - c == '\r'; -} - -obos_status Sys_ExecVE(const char* upath, char* const* argv, char* const* envp) -{ - if (!OBOSS_HandControlTo) - return OBOS_STATUS_UNIMPLEMENTED; - if (!upath) - return OBOS_STATUS_INVALID_ARGUMENT; - - obos_status status = OBOS_STATUS_SUCCESS; - - context* ctx = CoreS_GetCPULocalPtr()->currentContext; - - size_t argc = 0; - size_t envpc = 0; - - // Read the file. - char* path = nullptr; - size_t sz_path = 0; - status = OBOSH_ReadUserString(upath, nullptr, &sz_path); - if (obos_is_error(status)) - return status; - path = ZeroAllocate(OBOS_KernelAllocator, sz_path+1, sizeof(char), nullptr); - OBOSH_ReadUserString(upath, path, nullptr); - fd file = {}; - status = Vfs_FdOpen(&file, path, FD_OFLAGS_READ|FD_OFLAGS_EXECUTE); - if (obos_is_error(status)) - { - Free(OBOS_KernelAllocator, path, sz_path+1); - return status; - } - size_t szBuf = file.vn->filesize; - void* kbuf = Mm_VirtualMemoryAlloc(&Mm_KernelContext, nullptr, szBuf, OBOS_PROTECTION_READ_ONLY, 0, &file, nullptr); - bool set_uid = file.vn->perm.set_uid; - bool set_gid = file.vn->perm.set_gid; - uid target_euid = set_uid ? file.vn->uid : Core_GetCurrentThread()->proc->euid; - gid target_egid = set_gid ? file.vn->gid : Core_GetCurrentThread()->proc->egid; - Vfs_FdClose(&file); - - char** kargv = map_user_vector(ctx, argv, &argc, &status); - if (obos_is_error(status)) - { - Free(OBOS_KernelAllocator, path, sz_path+1); - Mm_VirtualMemoryFree(&Mm_KernelContext, kbuf, szBuf); - return status; - } - - char** knvp = map_user_vector(ctx, envp, &envpc, &status); - if (obos_is_error(status)) - { - Free(OBOS_KernelAllocator, path, sz_path+1); - Mm_VirtualMemoryFree(&Mm_KernelContext, kbuf, szBuf); - Mm_VirtualMemoryFree(&Mm_KernelContext, kargv, argc*sizeof(char*)); - return status; - } - - // Reallocate kargv+knvp to only use kernel pointers - kargv = get_user_string_vector(ctx, kargv, argc, &status); - if (obos_is_error(status)) - { - Free(OBOS_KernelAllocator, path, sz_path+1); - Mm_VirtualMemoryFree(&Mm_KernelContext, kbuf, szBuf); - Mm_VirtualMemoryFree(&Mm_KernelContext, kargv, argc*sizeof(char*)); - Mm_VirtualMemoryFree(&Mm_KernelContext, knvp, envpc*sizeof(char*)); - return status; - } - - knvp = get_user_string_vector(ctx, knvp, envpc, &status); - if (obos_is_error(status)) - { - Free(OBOS_KernelAllocator, path, sz_path+1); - Mm_VirtualMemoryFree(&Mm_KernelContext, kbuf, szBuf); - Mm_VirtualMemoryFree(&Mm_KernelContext, knvp, envpc*sizeof(char*)); - free_user_string_vector(kargv, argc); - return status; - } - - bool has_interpreter = false; - bool chas_interpreter = false; - char* interpreter_path = nullptr; - char* interpreter_arg = nullptr; - size_t len_interpreter_path = 0; - size_t len_interpreter_arg = 0; - char* last_path = path; - size_t interpreter_recursion = 0; - - do { - chas_interpreter = memcmp(kbuf, "#!", 2); - if (!chas_interpreter) - break; - has_interpreter = true; - - const char* buf = kbuf+2; - size_t this_bufLen = szBuf-2; - size_t len1 = strnchr(buf, '\n', this_bufLen); - size_t len2 = strnchr(buf, ' ', this_bufLen); - if (buf[len2] != '\n') - len2 = SIZE_MAX; - len_interpreter_path = OBOS_MIN(len1, len2); - len_interpreter_path -= buf[len_interpreter_path-1] == '\n' || buf[len_interpreter_path-1] == ' '; - if (!len_interpreter_path) - { - if (interpreter_recursion > 0) - Free(OBOS_KernelAllocator, path, sz_path+1); - free_user_string_vector(kargv, argc); - free_user_string_vector(knvp, envpc); - return OBOS_STATUS_NOT_FOUND; - } - if (buf[len_interpreter_path] == ' ') - { - const char* arg = kbuf + 2 + len_interpreter_path + 1; - len_interpreter_arg = len1 - len_interpreter_path - 2; - interpreter_arg = Allocate(OBOS_KernelAllocator, len_interpreter_arg+1, nullptr); - memcpy(interpreter_arg, arg, len_interpreter_arg); - interpreter_arg[len_interpreter_arg] = 0; - } - size_t wsof = 0; - while (isspace(*(buf+wsof))) - wsof++; - len_interpreter_path -= wsof; - buf += wsof; - interpreter_path = Allocate(OBOS_KernelAllocator, len_interpreter_path+1, nullptr); - memcpy(interpreter_path, buf, len_interpreter_path); - interpreter_path[len_interpreter_path] = 0; - - size_t new_argc = argc+1; - if (len_interpreter_arg) - new_argc += 1; - char** new_argv = ZeroAllocate(OBOS_KernelAllocator, new_argc+1, sizeof(char*), nullptr); - memcpy(&new_argv[new_argc-argc], kargv, argc * sizeof(char*)); - new_argv[0] = interpreter_path; - if (len_interpreter_arg) - new_argv[1] = interpreter_arg; - new_argv[1+(len_interpreter_arg?1:0)] = last_path; - new_argv[new_argc] = nullptr; - - Free(OBOS_KernelAllocator, kargv, (argc+1)*sizeof(char*)); - argc = new_argc; - kargv = new_argv; - - last_path = interpreter_path; - - Mm_VirtualMemoryFree(&Mm_KernelContext, kbuf, szBuf); - kbuf = nullptr; - szBuf = 0; - - memzero(&file, sizeof(file)); - status = Vfs_FdOpen(&file, interpreter_path, FD_OFLAGS_READ); - if (obos_is_error(status)) - { - free_user_string_vector(kargv, argc); - free_user_string_vector(knvp, envpc); - return status; - } - - szBuf = file.vn->filesize; - kbuf = Mm_VirtualMemoryAlloc( - &Mm_KernelContext, - nullptr, szBuf, - OBOS_PROTECTION_READ_ONLY, VMA_FLAGS_PRIVATE, - &file, - nullptr); - OBOS_ENSURE(kbuf); - Vfs_FdClose(&file); - } while(chas_interpreter && interpreter_recursion++ <= 4); - - if (interpreter_recursion == 4) - { - Mm_VirtualMemoryFree(&Mm_KernelContext, kbuf, szBuf); - free_user_string_vector(kargv, argc); - free_user_string_vector(knvp, envpc); - return OBOS_STATUS_INVALID_FILE; - } - - status = OBOS_LoadELF(ctx, kbuf, szBuf, nullptr, true, false); - if (obos_is_error(status)) - { - Mm_VirtualMemoryFree(&Mm_KernelContext, kbuf, szBuf); - if (!has_interpreter) - Free(OBOS_KernelAllocator, path, sz_path+1); - free_user_string_vector(kargv, argc); - free_user_string_vector(knvp, envpc); - return status; - } - - do { - if (Core_GetCurrentThread()->proc->exec_file) - Free(OBOS_KernelAllocator, Core_GetCurrentThread()->proc->exec_file, strlen(Core_GetCurrentThread()->proc->exec_file)+1); - Core_GetCurrentThread()->proc->exec_file = VfsH_DirentPathKAlloc(VfsH_DirentLookup(last_path), nullptr); - if (Core_GetCurrentThread()->proc->cmdline) - Free(OBOS_KernelAllocator, Core_GetCurrentThread()->proc->cmdline, strlen(Core_GetCurrentThread()->proc->cmdline)+1); - Core_GetCurrentThread()->proc->cmdline = nullptr; - string cmd_line = {}; - process* proc = Core_GetCurrentThread()->proc; - OBOS_InitString(&cmd_line, proc->exec_file); - for (size_t i = 1; i < argc; i++) - { - size_t len_arg = strlen(kargv[i]); - if (strchr(kargv[i], ' ') == len_arg) - { - OBOS_AppendStringC(&cmd_line, " "); - OBOS_AppendStringC(&cmd_line, kargv[i]); - } - else - { - OBOS_AppendStringC(&cmd_line, " \""); - OBOS_AppendStringC(&cmd_line, kargv[i]); - OBOS_AppendStringC(&cmd_line, "\""); - } - } - proc->cmdline = memcpy(Allocate(OBOS_KernelAllocator, OBOS_GetStringSize(&cmd_line)+1, nullptr), - OBOS_GetStringCPtr(&cmd_line), - OBOS_GetStringSize(&cmd_line)+1); - OBOS_FreeString(&cmd_line); - } while(0); - - if (!has_interpreter) - { - Free(OBOS_KernelAllocator, path, sz_path+1); - path = nullptr; - } - - irql oldIrql = Core_RaiseIrql(IRQL_DISPATCH); - // For each thread in the current process, send SIGKILL, and wait for it to die. - for (thread_node* curr = Core_GetCurrentThread()->proc->threads.head; curr; ) - { - thread* const thr = curr->data; - curr = curr->next; - if (thr == Core_GetCurrentThread()) - continue; - - OBOS_Kill(Core_GetCurrentThread(), thr, SIGKILL); - while (~thr->flags & THREAD_FLAGS_DIED) - OBOSS_SpinlockHint(); - } - Core_LowerIrql(oldIrql); - - // Reset all signals to SIG_DFL - sigaction sigact = {.un.handler = SIG_DFL}; - for (int sigval = 1; sigval <= SIGMAX; sigval++) - OBOS_SigAction(sigval, &sigact, nullptr); - Core_GetCurrentThread()->signal_info->pending = 0; - Core_GetCurrentThread()->signal_info->mask = 0; - - // TODO: Cancel any outstanding async I/O. - // NOTE(oberrow): POSIX doesn't mandate this, but it is better do so. - - // Close the handles of the following types: - // - dirent - // - timer - // - driver_id - // - thread_ctx - // - context - - handle_table* tbl = &CoreS_GetCPULocalPtr()->currentThread->proc->handles; - - for (size_t i = 0; i < tbl->size; i++) - { - switch (tbl->arr[i].type) { - case HANDLE_TYPE_DIRENT: - case HANDLE_TYPE_TIMER: - case HANDLE_TYPE_THREAD_CTX: - case HANDLE_TYPE_VMM_CONTEXT: - Sys_HandleClose(i | (tbl->arr[i].type << HANDLE_TYPE_SHIFT)); - break; - case HANDLE_TYPE_FD: - if (!tbl->arr[i].un.fd) - break; - if (tbl->arr[i].un.fd->flags & FD_FLAGS_NOEXEC) - Sys_HandleClose(i | (tbl->arr[i].type << HANDLE_TYPE_SHIFT)); - break; - default: - break; - } - } - - oldIrql = Core_RaiseIrql(IRQL_DISPATCH); - - // Free all memory - page_range* rng = nullptr; - page_range* next = nullptr; - - for ((rng) = RB_MIN(page_tree, &ctx->pages); (rng) != nullptr; ) - { - next = RB_NEXT(page_tree, &ctx->pages, rng); - uintptr_t virt = rng->virt; - if (rng->hasGuardPage) - virt += (rng->prot.huge_page ? OBOS_HUGE_PAGE_SIZE : OBOS_PAGE_SIZE); - uintptr_t limit = rng->virt+rng->size; - Mm_VirtualMemoryFree(ctx, (void*)virt, limit-virt); - rng = next; - } - OBOS_ASSERT(!ctx->stat.committedMemory); - - // Load the ELF into the process. - elf_info info = {}; - status = OBOS_LoadELF(ctx, kbuf, szBuf, &info, false, false); - if (obos_is_error(status)) - { - OBOS_Error("OBOS_LoadELF failed in %s after already having verified ELF file status. Status: %d\n", __func__, status); - Core_ExitCurrentProcess(9 | (9<<8)); // SIGKILL -// OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "OBOS_LoadELF failed in %s after already having verified ELF file status.\nStatus: %d\n", __func__, status); - } - - struct exec_aux_values aux = {.elf=info}; - const Elf_Ehdr* ehdr = kbuf; - aux.phdr.ptr = (void*)((uintptr_t)info.base + ehdr->e_phoff); - aux.phdr.phent = ehdr->e_phentsize; - aux.phdr.phnum = ehdr->e_phnum; - - Mm_VirtualMemoryFree(&Mm_KernelContext, (void*)kbuf, szBuf); - - Core_GetCurrentThread()->context.stackBase = Mm_VirtualMemoryAlloc(ctx, nullptr, 4*1024*1024, OBOS_PROTECTION_USER_PAGE, VMA_FLAGS_GUARD_PAGE, nullptr, nullptr); - Core_GetCurrentThread()->context.stackSize = 4*1024*1024; - - aux.argc = argc; - aux.argv = kargv; - - aux.envp = knvp; - aux.envpc = envpc; - - aux.at_secure = set_gid || set_uid; - - Core_GetCurrentThread()->proc->euid = target_euid; - Core_GetCurrentThread()->proc->egid = target_egid; - Core_GetCurrentThread()->proc->sgid = target_euid; - Core_GetCurrentThread()->proc->sgid = target_egid; - - OBOSS_HandControlTo(ctx, &aux); -} diff --git a/src/oboskrnl/execve.h b/src/oboskrnl/execve.h deleted file mode 100644 index 26c1ac88..00000000 --- a/src/oboskrnl/execve.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * oboskrnl/execve.h - * - * Copyright (c) 2024 Omar Berrow - */ - -#pragma once - -#include -#include -#include - -#include - -// envp can be nullptr -// argv can be nullptr -obos_status Sys_ExecVE(const char* path, char* const* argv, char* const* envp); - -struct exec_aux_values { - elf_info elf; - - struct { - void* ptr; - size_t phnum; - size_t phent; - } phdr; - - // NOTE: Make sure to free the next fields after copying them to whereever the process expects them. - // Guaranteed to be allocated with OBOS_KernelAllocator. - - char** argv; - char** envp; - size_t argc; - size_t envpc; - - bool at_secure; -}; - -OBOS_NORETURN OBOS_WEAK void OBOSS_HandControlTo(struct context* ctx, struct exec_aux_values* aux); diff --git a/src/oboskrnl/export.h b/src/oboskrnl/export.h new file mode 100644 index 00000000..9834ba42 --- /dev/null +++ b/src/oboskrnl/export.h @@ -0,0 +1,18 @@ +#pragma once + +#ifndef __INTELLISENSE__ +# define OBOS_WEAK __attribute__((weak)) +#else +# define OBOS_WEAK +#endif +#ifdef OBOS_DRIVER +# ifndef __INTELLISENSE__ +# define OBOS_EXPORT __attribute__((weak)) +# else +# define OBOS_EXPORT +# endif +#elif defined(OBOS_KERNEL) +# define OBOS_EXPORT +#else +# define OBOS_EXPORT +#endif \ No newline at end of file diff --git a/src/oboskrnl/external/fixedptc.h b/src/oboskrnl/external/fixedptc.h deleted file mode 100644 index d8e79b56..00000000 --- a/src/oboskrnl/external/fixedptc.h +++ /dev/null @@ -1,412 +0,0 @@ -#ifndef _FIXEDPTC_H_ -#define _FIXEDPTC_H_ - -/* - * fixedptc.h is a 32-bit or 64-bit fixed point numeric library. - * - * The symbol FIXEDPT_BITS, if defined before this library header file - * is included, determines the number of bits in the data type (its "width"). - * The default width is 32-bit (FIXEDPT_BITS=32) and it can be used - * on any recent C99 compiler. The 64-bit precision (FIXEDPT_BITS=64) is - * available on compilers which implement 128-bit "long long" types. This - * precision has been tested on GCC 4.2+. - * - * The FIXEDPT_WBITS symbols governs how many bits are dedicated to the - * "whole" part of the number (to the left of the decimal point). The larger - * this width is, the larger the numbers which can be stored in the fixedpt - * number. The rest of the bits (available in the FIXEDPT_FBITS symbol) are - * dedicated to the fraction part of the number (to the right of the decimal - * point). - * - * Since the number of bits in both cases is relatively low, many complex - * functions (more complex than div & mul) take a large hit on the precision - * of the end result because errors in precision accumulate. - * This loss of precision can be lessened by increasing the number of - * bits dedicated to the fraction part, but at the loss of range. - * - * Adventurous users might utilize this library to build two data types: - * one which has the range, and one which has the precision, and carefully - * convert between them (including adding two number of each type to produce - * a simulated type with a larger range and precision). - * - * The ideas and algorithms have been cherry-picked from a large number - * of previous implementations available on the Internet. - * Tim Hartrick has contributed cleanup and 64-bit support patches. - * - * == Special notes for the 32-bit precision == - * Signed 32-bit fixed point numeric library for the 24.8 format. - * The specific limits are -8388608.999... to 8388607.999... and the - * most precise number is 0.00390625. In practice, you should not count - * on working with numbers larger than a million or to the precision - * of more than 2 decimal places. Make peace with the fact that PI - * is 3.14 here. :) - */ - -/*- - * Copyright (c) 2010-2012 Ivan Voras - * Copyright (c) 2012 Tim Hartrick - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef FIXEDPT_BITS -#define FIXEDPT_BITS 32 -#endif - -#include - -#if FIXEDPT_BITS == 32 -typedef int32_t fixedpt; -typedef int64_t fixedptd; -typedef uint32_t fixedptu; -typedef uint64_t fixedptud; -#elif FIXEDPT_BITS == 64 -typedef int64_t fixedpt; -typedef __int128_t fixedptd; -typedef uint64_t fixedptu; -typedef __uint128_t fixedptud; -#else -#error "FIXEDPT_BITS must be equal to 32 or 64" -#endif - -#ifndef FIXEDPT_WBITS -#define FIXEDPT_WBITS 24 -#endif - -#if FIXEDPT_WBITS >= FIXEDPT_BITS -#error "FIXEDPT_WBITS must be less than or equal to FIXEDPT_BITS" -#endif - -#define FIXEDPT_VCSID "$Id$" - -#define FIXEDPT_FBITS (FIXEDPT_BITS - FIXEDPT_WBITS) -#define FIXEDPT_FMASK (((fixedpt)1 << FIXEDPT_FBITS) - 1) - -#define fixedpt_rconst(R) ((fixedpt)((R) * FIXEDPT_ONE + ((R) >= 0 ? 0.5 : -0.5))) -#define fixedpt_fromint(I) ((fixedptd)(I) << FIXEDPT_FBITS) -#define fixedpt_toint(F) ((F) >> FIXEDPT_FBITS) -#define fixedpt_add(A,B) ((A) + (B)) -#define fixedpt_sub(A,B) ((A) - (B)) -#define fixedpt_xmul(A,B) \ - ((fixedpt)(((fixedptd)(A) * (fixedptd)(B)) >> FIXEDPT_FBITS)) -#define fixedpt_xdiv(A,B) \ - ((fixedpt)(((fixedptd)(A) << FIXEDPT_FBITS) / (fixedptd)(B))) -#define fixedpt_fracpart(A) ((fixedpt)(A) & FIXEDPT_FMASK) - -#define FIXEDPT_ONE ((fixedpt)((fixedpt)1 << FIXEDPT_FBITS)) -#define FIXEDPT_ONE_HALF (FIXEDPT_ONE >> 1) -#define FIXEDPT_TWO (FIXEDPT_ONE + FIXEDPT_ONE) -#define FIXEDPT_PI fixedpt_rconst(3.14159265358979323846) -#define FIXEDPT_TWO_PI fixedpt_rconst(2 * 3.14159265358979323846) -#define FIXEDPT_HALF_PI fixedpt_rconst(3.14159265358979323846 / 2) -#define FIXEDPT_E fixedpt_rconst(2.7182818284590452354) - -#define fixedpt_abs(A) ((A) < 0 ? -(A) : (A)) - -/* fixedpt is meant to be usable in environments without floating point support - * (e.g. microcontrollers, kernels), so we can't use floating point types directly. - * Putting them only in macros will effectively make them optional. */ -#define fixedpt_tofloat(T) ((float) ((T)*((float)(1)/(float)(1L << FIXEDPT_FBITS)))) - - -/* Multiplies two fixedpt numbers, returns the result. */ -static inline fixedpt -fixedpt_mul(fixedpt A, fixedpt B) -{ - return (((fixedptd)A * (fixedptd)B) >> FIXEDPT_FBITS); -} - - -/* Divides two fixedpt numbers, returns the result. */ -static inline fixedpt -fixedpt_div(fixedpt A, fixedpt B) -{ - return (((fixedptd)A << FIXEDPT_FBITS) / (fixedptd)B); -} - -/* - * Note: adding and substracting fixedpt numbers can be done by using - * the regular integer operators + and -. - */ - -/** - * Convert the given fixedpt number to a decimal string. - * The max_dec argument specifies how many decimal digits to the right - * of the decimal point to generate. If set to -1, the "default" number - * of decimal digits will be used (2 for 32-bit fixedpt width, 10 for - * 64-bit fixedpt width); If set to -2, "all" of the digits will - * be returned, meaning there will be invalid, bogus digits outside the - * specified precisions. - */ -static inline void -fixedpt_str(fixedpt A, char *str, int max_dec) -{ - int ndec = 0, slen = 0; - char tmp[12] = {0}; - fixedptud fr, ip; - const fixedptud one = (fixedptud)1 << FIXEDPT_BITS; - const fixedptud mask = one - 1; - - if (max_dec == -1) -#if FIXEDPT_BITS == 32 -#if FIXEDPT_WBITS > 16 - max_dec = 2; -#else - max_dec = 4; -#endif -#elif FIXEDPT_BITS == 64 - max_dec = 10; -#else -#error Invalid width -#endif - else if (max_dec == -2) - max_dec = 15; - - if (A < 0) { - str[slen++] = '-'; - A *= -1; - } - - ip = fixedpt_toint(A); - do { - tmp[ndec++] = '0' + ip % 10; - ip /= 10; - } while (ip != 0); - - while (ndec > 0) - str[slen++] = tmp[--ndec]; - str[slen++] = '.'; - - fr = (fixedpt_fracpart(A) << FIXEDPT_WBITS) & mask; - do { - fr = (fr & mask) * 10; - - str[slen++] = '0' + (fr >> FIXEDPT_BITS) % 10; - ndec++; - } while (fr != 0 && ndec < max_dec); - - if (ndec > 1 && str[slen-1] == '0') - str[slen-1] = '\0'; /* cut off trailing 0 */ - else - str[slen] = '\0'; -} - - -/* Converts the given fixedpt number into a string, using a static - * (non-threadsafe) string buffer */ -static inline char* -fixedpt_cstr(const fixedpt A, const int max_dec) -{ - static char str[25]; - - fixedpt_str(A, str, max_dec); - return (str); -} - - -/* Returns the square root of the given number, or -1 in case of error */ -static inline fixedpt -fixedpt_sqrt(fixedpt A) -{ - int invert = 0; - int iter = FIXEDPT_FBITS; - int l, i; - - if (A < 0) - return (-1); - if (A == 0 || A == FIXEDPT_ONE) - return (A); - if (A < FIXEDPT_ONE && A > 6) { - invert = 1; - A = fixedpt_div(FIXEDPT_ONE, A); - } - if (A > FIXEDPT_ONE) { - int s = A; - - iter = 0; - while (s > 0) { - s >>= 2; - iter++; - } - } - - /* Newton's iterations */ - l = (A >> 1) + 1; - for (i = 0; i < iter; i++) - l = (l + fixedpt_div(A, l)) >> 1; - if (invert) - return (fixedpt_div(FIXEDPT_ONE, l)); - return (l); -} - - -/* Returns the sine of the given fixedpt number. - * Note: the loss of precision is extraordinary! */ -static inline fixedpt -fixedpt_sin(fixedpt fp) -{ - int sign = 1; - fixedpt sqr, result; - const fixedpt SK[2] = { - fixedpt_rconst(7.61e-03), - fixedpt_rconst(1.6605e-01) - }; - - fp %= 2 * FIXEDPT_PI; - if (fp < 0) - fp = FIXEDPT_PI * 2 + fp; - if ((fp > FIXEDPT_HALF_PI) && (fp <= FIXEDPT_PI)) - fp = FIXEDPT_PI - fp; - else if ((fp > FIXEDPT_PI) && (fp <= (FIXEDPT_PI + FIXEDPT_HALF_PI))) { - fp = fp - FIXEDPT_PI; - sign = -1; - } else if (fp > (FIXEDPT_PI + FIXEDPT_HALF_PI)) { - fp = (FIXEDPT_PI << 1) - fp; - sign = -1; - } - sqr = fixedpt_mul(fp, fp); - result = SK[0]; - result = fixedpt_mul(result, sqr); - result -= SK[1]; - result = fixedpt_mul(result, sqr); - result += FIXEDPT_ONE; - result = fixedpt_mul(result, fp); - return sign * result; -} - - -/* Returns the cosine of the given fixedpt number */ -static inline fixedpt -fixedpt_cos(fixedpt A) -{ - return (fixedpt_sin(FIXEDPT_HALF_PI - A)); -} - - -/* Returns the tangens of the given fixedpt number */ -static inline fixedpt -fixedpt_tan(fixedpt A) -{ - return fixedpt_div(fixedpt_sin(A), fixedpt_cos(A)); -} - - -/* Returns the value exp(x), i.e. e^x of the given fixedpt number. */ -static inline fixedpt -fixedpt_exp(fixedpt fp) -{ - fixedpt xabs, k, z, R, xp; - const fixedpt LN2 = fixedpt_rconst(0.69314718055994530942); - const fixedpt LN2_INV = fixedpt_rconst(1.4426950408889634074); - const fixedpt EXP_P[5] = { - fixedpt_rconst(1.66666666666666019037e-01), - fixedpt_rconst(-2.77777777770155933842e-03), - fixedpt_rconst(6.61375632143793436117e-05), - fixedpt_rconst(-1.65339022054652515390e-06), - fixedpt_rconst(4.13813679705723846039e-08), - }; - - if (fp == 0) - return (FIXEDPT_ONE); - xabs = fixedpt_abs(fp); - k = fixedpt_mul(xabs, LN2_INV); - k += FIXEDPT_ONE_HALF; - k &= ~FIXEDPT_FMASK; - if (fp < 0) - k = -k; - fp -= fixedpt_mul(k, LN2); - z = fixedpt_mul(fp, fp); - /* Taylor */ - R = FIXEDPT_TWO + - fixedpt_mul(z, EXP_P[0] + fixedpt_mul(z, EXP_P[1] + - fixedpt_mul(z, EXP_P[2] + fixedpt_mul(z, EXP_P[3] + - fixedpt_mul(z, EXP_P[4]))))); - xp = FIXEDPT_ONE + fixedpt_div(fixedpt_mul(fp, FIXEDPT_TWO), R - fp); - if (k < 0) - k = FIXEDPT_ONE >> (-k >> FIXEDPT_FBITS); - else - k = FIXEDPT_ONE << (k >> FIXEDPT_FBITS); - return (fixedpt_mul(k, xp)); -} - - -/* Returns the natural logarithm of the given fixedpt number. */ -static inline fixedpt -fixedpt_ln(fixedpt x) -{ - fixedpt log2, xi; - fixedpt f, s, z, w, R; - const fixedpt LN2 = fixedpt_rconst(0.69314718055994530942); - const fixedpt LG[7] = { - fixedpt_rconst(6.666666666666735130e-01), - fixedpt_rconst(3.999999999940941908e-01), - fixedpt_rconst(2.857142874366239149e-01), - fixedpt_rconst(2.222219843214978396e-01), - fixedpt_rconst(1.818357216161805012e-01), - fixedpt_rconst(1.531383769920937332e-01), - fixedpt_rconst(1.479819860511658591e-01) - }; - - if (x < 0) - return (0); - if (x == 0) - return 0xffffffff; - - log2 = 0; - xi = x; - while (xi > FIXEDPT_TWO) { - xi >>= 1; - log2++; - } - f = xi - FIXEDPT_ONE; - s = fixedpt_div(f, FIXEDPT_TWO + f); - z = fixedpt_mul(s, s); - w = fixedpt_mul(z, z); - R = fixedpt_mul(w, LG[1] + fixedpt_mul(w, LG[3] - + fixedpt_mul(w, LG[5]))) + fixedpt_mul(z, LG[0] - + fixedpt_mul(w, LG[2] + fixedpt_mul(w, LG[4] - + fixedpt_mul(w, LG[6])))); - return (fixedpt_mul(LN2, (log2 << FIXEDPT_FBITS)) + f - - fixedpt_mul(s, f - R)); -} - - -/* Returns the logarithm of the given base of the given fixedpt number */ -static inline fixedpt -fixedpt_log(fixedpt x, fixedpt base) -{ - return (fixedpt_div(fixedpt_ln(x), fixedpt_ln(base))); -} - - -/* Return the power value (n^exp) of the given fixedpt numbers */ -static inline fixedpt -fixedpt_pow(fixedpt n, fixedpt exp) -{ - if (exp == 0) - return (FIXEDPT_ONE); - if (n < 0) - return 0; - return (fixedpt_exp(fixedpt_mul(fixedpt_ln(n), exp))); -} - -#endif diff --git a/src/oboskrnl/external/nanoprintf.h b/src/oboskrnl/external/nanoprintf.h deleted file mode 100644 index cbc325e4..00000000 --- a/src/oboskrnl/external/nanoprintf.h +++ /dev/null @@ -1,1140 +0,0 @@ -/* nanoprintf v0.5.3: a tiny embeddable printf replacement written in C. - https://github.com/charlesnicholson/nanoprintf - charles.nicholson+nanoprintf@gmail.com - dual-licensed under 0bsd and unlicense, take your pick. see eof for details. */ - -#ifndef NANOPRINTF_H_INCLUDED -#define NANOPRINTF_H_INCLUDED - -#include -#include - -// Define this to fully sandbox nanoprintf inside of a translation unit. -#ifdef NANOPRINTF_VISIBILITY_STATIC - #define NPF_VISIBILITY static -#else - #define NPF_VISIBILITY extern -#endif - -#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) - #define NPF_PRINTF_ATTR(FORMAT_INDEX, VARGS_INDEX) \ - __attribute__((format(printf, FORMAT_INDEX, VARGS_INDEX))) -#else - #define NPF_PRINTF_ATTR(FORMAT_INDEX, VARGS_INDEX) -#endif - -// Public API - -#ifdef __cplusplus -extern "C" { -#endif - -// The npf_ functions all return the number of bytes required to express the -// fully-formatted string, not including the null terminator character. -// The npf_ functions do not return negative values, since the lack of 'l' length -// modifier support makes encoding errors impossible. - -NPF_VISIBILITY int npf_snprintf( - char *buffer, size_t bufsz, const char *format, ...) NPF_PRINTF_ATTR(3, 4); - -NPF_VISIBILITY int npf_vsnprintf( - char *buffer, size_t bufsz, char const *format, va_list vlist) NPF_PRINTF_ATTR(3, 0); - -typedef void (*npf_putc)(int c, void *ctx); -NPF_VISIBILITY int npf_pprintf( - npf_putc pc, void *pc_ctx, char const *format, ...) NPF_PRINTF_ATTR(3, 4); - -NPF_VISIBILITY int npf_vpprintf( - npf_putc pc, void *pc_ctx, char const *format, va_list vlist) NPF_PRINTF_ATTR(3, 0); - -#ifdef __cplusplus -} -#endif - -#endif // NANOPRINTF_H_INCLUDED - -/* The implementation of nanoprintf begins here, to be compiled only if - NANOPRINTF_IMPLEMENTATION is defined. In a multi-file library what follows would - be nanoprintf.c. */ - -#ifdef NANOPRINTF_IMPLEMENTATION - -#ifndef NANOPRINTF_IMPLEMENTATION_INCLUDED -#define NANOPRINTF_IMPLEMENTATION_INCLUDED - -#include -#include - -// The conversion buffer must fit at least UINT64_MAX in octal format with the leading '0'. -#ifndef NANOPRINTF_CONVERSION_BUFFER_SIZE - #define NANOPRINTF_CONVERSION_BUFFER_SIZE 23 -#endif -#if NANOPRINTF_CONVERSION_BUFFER_SIZE < 23 - #error The size of the conversion buffer must be at least 23 bytes. -#endif - -// Pick reasonable defaults if nothing's been configured. -#if !defined(NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS) && \ - !defined(NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS) && \ - !defined(NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS) && \ - !defined(NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS) && \ - !defined(NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS) && \ - !defined(NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS) - #define NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS 1 - #define NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS 1 - #define NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS 1 - #define NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS 0 - #define NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS 0 - #define NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS 0 -#endif - -// If anything's been configured, everything must be configured. -#ifndef NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS - #error NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS must be #defined to 0 or 1 -#endif -#ifndef NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS - #error NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS must be #defined to 0 or 1 -#endif -#ifndef NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS - #error NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS must be #defined to 0 or 1 -#endif -#ifndef NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS - #error NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS must be #defined to 0 or 1 -#endif -#ifndef NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS - #error NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS must be #defined to 0 or 1 -#endif -#ifndef NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS - #error NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS must be #defined to 0 or 1 -#endif - -// Ensure flags are compatible. -#if (NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1) && \ - (NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 0) - #error Precision format specifiers must be enabled if float support is enabled. -#endif - -// intmax_t / uintmax_t require stdint from c99 / c++11 -#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1 - #ifndef _MSC_VER - #ifdef __cplusplus - #if __cplusplus < 201103L - #error large format specifier support requires C++11 or later. - #endif - #else - #if __STDC_VERSION__ < 199409L - #error nanoprintf requires C99 or later. - #endif - #endif - #endif -#endif - -// Figure out if we can disable warnings with pragmas. -#ifdef __clang__ - #define NANOPRINTF_CLANG 1 - #define NANOPRINTF_GCC_PAST_4_6 0 -#else - #define NANOPRINTF_CLANG 0 - #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 6))) - #define NANOPRINTF_GCC_PAST_4_6 1 - #else - #define NANOPRINTF_GCC_PAST_4_6 0 - #endif -#endif - -#if NANOPRINTF_CLANG || NANOPRINTF_GCC_PAST_4_6 - #define NANOPRINTF_HAVE_GCC_WARNING_PRAGMAS 1 -#else - #define NANOPRINTF_HAVE_GCC_WARNING_PRAGMAS 0 -#endif - -#if NANOPRINTF_HAVE_GCC_WARNING_PRAGMAS - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Wunused-function" - #pragma GCC diagnostic ignored "-Wimplicit-fallthrough" - #ifdef __cplusplus - #pragma GCC diagnostic ignored "-Wold-style-cast" - #endif - #pragma GCC diagnostic ignored "-Wpadded" - #pragma GCC diagnostic ignored "-Wfloat-equal" - #if NANOPRINTF_CLANG - #pragma GCC diagnostic ignored "-Wc++98-compat-pedantic" - #pragma GCC diagnostic ignored "-Wcovered-switch-default" - #pragma GCC diagnostic ignored "-Wdeclaration-after-statement" - #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant" - #ifndef __APPLE__ - #pragma GCC diagnostic ignored "-Wunsafe-buffer-usage" - #endif - #elif NANOPRINTF_GCC_PAST_4_6 - #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" - #endif -#endif - -#ifdef _MSC_VER - #pragma warning(push) - #pragma warning(disable:4619) // there is no warning number 'number' - // C4619 has to be disabled first! - #pragma warning(disable:4127) // conditional expression is constant - #pragma warning(disable:4505) // unreferenced local function has been removed - #pragma warning(disable:4514) // unreferenced inline function has been removed - #pragma warning(disable:4701) // potentially uninitialized local variable used - #pragma warning(disable:4706) // assignment within conditional expression - #pragma warning(disable:4710) // function not inlined - #pragma warning(disable:4711) // function selected for inline expansion - #pragma warning(disable:4820) // padding added after struct member - #pragma warning(disable:5039) // potentially throwing function passed to extern C function - #pragma warning(disable:5045) // compiler will insert Spectre mitigation for memory load - #pragma warning(disable:5262) // implicit switch fall-through - #pragma warning(disable:26812) // enum type is unscoped -#endif - -#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) - #define NPF_NOINLINE __attribute__((noinline)) -#elif defined(_MSC_VER) - #define NPF_NOINLINE __declspec(noinline) -#else - #define NPF_NOINLINE -#endif - -#if (NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1) || \ - (NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1) -enum { - NPF_FMT_SPEC_OPT_NONE, - NPF_FMT_SPEC_OPT_LITERAL, - NPF_FMT_SPEC_OPT_STAR, -}; -#endif - -enum { - NPF_FMT_SPEC_LEN_MOD_NONE, - NPF_FMT_SPEC_LEN_MOD_SHORT, // 'h' - NPF_FMT_SPEC_LEN_MOD_LONG_DOUBLE, // 'L' - NPF_FMT_SPEC_LEN_MOD_CHAR, // 'hh' - NPF_FMT_SPEC_LEN_MOD_LONG, // 'l' -#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1 - NPF_FMT_SPEC_LEN_MOD_LARGE_LONG_LONG, // 'll' - NPF_FMT_SPEC_LEN_MOD_LARGE_INTMAX, // 'j' - NPF_FMT_SPEC_LEN_MOD_LARGE_SIZET, // 'z' - NPF_FMT_SPEC_LEN_MOD_LARGE_PTRDIFFT, // 't' -#endif -}; - -enum { - NPF_FMT_SPEC_CONV_NONE, - NPF_FMT_SPEC_CONV_PERCENT, // '%' - NPF_FMT_SPEC_CONV_CHAR, // 'c' - NPF_FMT_SPEC_CONV_STRING, // 's' - NPF_FMT_SPEC_CONV_SIGNED_INT, // 'i', 'd' -#if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1 - NPF_FMT_SPEC_CONV_BINARY, // 'b' -#endif - NPF_FMT_SPEC_CONV_OCTAL, // 'o' - NPF_FMT_SPEC_CONV_HEX_INT, // 'x', 'X' - NPF_FMT_SPEC_CONV_UNSIGNED_INT, // 'u' - NPF_FMT_SPEC_CONV_POINTER, // 'p' -#if NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS == 1 - NPF_FMT_SPEC_CONV_WRITEBACK, // 'n' -#endif -#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1 - NPF_FMT_SPEC_CONV_FLOAT_DEC, // 'f', 'F' - NPF_FMT_SPEC_CONV_FLOAT_SCI, // 'e', 'E' - NPF_FMT_SPEC_CONV_FLOAT_SHORTEST, // 'g', 'G' - NPF_FMT_SPEC_CONV_FLOAT_HEX, // 'a', 'A' -#endif -}; - -typedef struct npf_format_spec { -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - int field_width; - uint8_t field_width_opt; - char left_justified; // '-' - char leading_zero_pad; // '0' -#endif -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - int prec; - uint8_t prec_opt; -#endif - char prepend; // ' ' or '+' - char alt_form; // '#' - char case_adjust; // 'a' - 'A' - uint8_t length_modifier; - uint8_t conv_spec; -} npf_format_spec_t; - -#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 0 - typedef long npf_int_t; - typedef unsigned long npf_uint_t; -#else - typedef intmax_t npf_int_t; - typedef uintmax_t npf_uint_t; -#endif - -typedef struct npf_bufputc_ctx { - char *dst; - size_t len; - size_t cur; -} npf_bufputc_ctx_t; - -#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1 - typedef char npf_size_is_ptrdiff[(sizeof(size_t) == sizeof(ptrdiff_t)) ? 1 : -1]; - typedef ptrdiff_t npf_ssize_t; -#endif - -#ifdef _MSC_VER - #include -#endif - -static int npf_max(int x, int y) { return (x > y) ? x : y; } - -static int npf_parse_format_spec(char const *format, npf_format_spec_t *out_spec) { - char const *cur = format; - -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - out_spec->left_justified = 0; - out_spec->leading_zero_pad = 0; -#endif - out_spec->case_adjust = 'a' - 'A'; // lowercase - out_spec->prepend = 0; - out_spec->alt_form = 0; - - while (*++cur) { // cur points at the leading '%' character - switch (*cur) { // Optional flags -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - case '-': out_spec->left_justified = '-'; out_spec->leading_zero_pad = 0; continue; - case '0': out_spec->leading_zero_pad = !out_spec->left_justified; continue; -#endif - case '+': out_spec->prepend = '+'; continue; - case ' ': if (out_spec->prepend == 0) { out_spec->prepend = ' '; } continue; - case '#': out_spec->alt_form = '#'; continue; - default: break; - } - break; - } - -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - out_spec->field_width_opt = NPF_FMT_SPEC_OPT_NONE; - if (*cur == '*') { - out_spec->field_width_opt = NPF_FMT_SPEC_OPT_STAR; - ++cur; - } else { - out_spec->field_width = 0; - while ((*cur >= '0') && (*cur <= '9')) { - out_spec->field_width_opt = NPF_FMT_SPEC_OPT_LITERAL; - out_spec->field_width = (out_spec->field_width * 10) + (*cur++ - '0'); - } - } -#endif - -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - out_spec->prec = 0; - out_spec->prec_opt = NPF_FMT_SPEC_OPT_NONE; - if (*cur == '.') { - ++cur; - if (*cur == '*') { - out_spec->prec_opt = NPF_FMT_SPEC_OPT_STAR; - ++cur; - } else { - if (*cur == '-') { - ++cur; - } else { - out_spec->prec_opt = NPF_FMT_SPEC_OPT_LITERAL; - } - while ((*cur >= '0') && (*cur <= '9')) { - out_spec->prec = (out_spec->prec * 10) + (*cur++ - '0'); - } - } - } -#endif - - uint_fast8_t tmp_conv = NPF_FMT_SPEC_CONV_NONE; - out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_NONE; - switch (*cur++) { // Length modifier - case 'h': - out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_SHORT; - if (*cur == 'h') { - out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_CHAR; - ++cur; - } - break; - case 'l': - out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LONG; -#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1 - if (*cur == 'l') { - out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LARGE_LONG_LONG; - ++cur; - } -#endif - break; -#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1 - case 'L': out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LONG_DOUBLE; break; -#endif -#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1 - case 'j': out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LARGE_INTMAX; break; - case 'z': out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LARGE_SIZET; break; - case 't': out_spec->length_modifier = NPF_FMT_SPEC_LEN_MOD_LARGE_PTRDIFFT; break; -#endif - default: --cur; break; - } - - switch (*cur++) { // Conversion specifier - case '%': out_spec->conv_spec = NPF_FMT_SPEC_CONV_PERCENT; -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - out_spec->prec_opt = NPF_FMT_SPEC_OPT_NONE; -#endif - break; - - case 'c': out_spec->conv_spec = NPF_FMT_SPEC_CONV_CHAR; -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - out_spec->prec_opt = NPF_FMT_SPEC_OPT_NONE; -#endif - break; - - case 's': out_spec->conv_spec = NPF_FMT_SPEC_CONV_STRING; -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - out_spec->leading_zero_pad = 0; -#endif - break; - - case 'i': - case 'd': tmp_conv = NPF_FMT_SPEC_CONV_SIGNED_INT; - case 'o': - if (tmp_conv == NPF_FMT_SPEC_CONV_NONE) { tmp_conv = NPF_FMT_SPEC_CONV_OCTAL; } - case 'u': - if (tmp_conv == NPF_FMT_SPEC_CONV_NONE) { tmp_conv = NPF_FMT_SPEC_CONV_UNSIGNED_INT; } - case 'X': - if (tmp_conv == NPF_FMT_SPEC_CONV_NONE) { out_spec->case_adjust = 0; } - case 'x': - if (tmp_conv == NPF_FMT_SPEC_CONV_NONE) { tmp_conv = NPF_FMT_SPEC_CONV_HEX_INT; } - out_spec->conv_spec = (uint8_t)tmp_conv; -#if (NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1) && \ - (NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1) - if (out_spec->prec_opt != NPF_FMT_SPEC_OPT_NONE) { out_spec->leading_zero_pad = 0; } -#endif - break; - -#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1 - case 'F': out_spec->case_adjust = 0; - case 'f': - out_spec->conv_spec = NPF_FMT_SPEC_CONV_FLOAT_DEC; - if (out_spec->prec_opt == NPF_FMT_SPEC_OPT_NONE) { out_spec->prec = 6; } - break; - - case 'E': out_spec->case_adjust = 0; - case 'e': - out_spec->conv_spec = NPF_FMT_SPEC_CONV_FLOAT_SCI; - if (out_spec->prec_opt == NPF_FMT_SPEC_OPT_NONE) { out_spec->prec = 6; } - break; - - case 'G': out_spec->case_adjust = 0; - case 'g': - out_spec->conv_spec = NPF_FMT_SPEC_CONV_FLOAT_SHORTEST; - if (out_spec->prec_opt == NPF_FMT_SPEC_OPT_NONE) { out_spec->prec = 6; } - break; - - case 'A': out_spec->case_adjust = 0; - case 'a': - out_spec->conv_spec = NPF_FMT_SPEC_CONV_FLOAT_HEX; - if (out_spec->prec_opt == NPF_FMT_SPEC_OPT_NONE) { out_spec->prec = 6; } - break; -#endif - -#if NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS == 1 - case 'n': - // todo: reject string if flags or width or precision exist - out_spec->conv_spec = NPF_FMT_SPEC_CONV_WRITEBACK; -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - out_spec->prec_opt = NPF_FMT_SPEC_OPT_NONE; -#endif - break; -#endif - - case 'p': - out_spec->conv_spec = NPF_FMT_SPEC_CONV_POINTER; -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - out_spec->prec_opt = NPF_FMT_SPEC_OPT_NONE; -#endif - break; - -#if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1 - case 'B': - out_spec->case_adjust = 0; - case 'b': - out_spec->conv_spec = NPF_FMT_SPEC_CONV_BINARY; - break; -#endif - - default: return 0; - } - - return (int)(cur - format); -} - -static NPF_NOINLINE int npf_utoa_rev( - npf_uint_t val, char *buf, uint_fast8_t base, char case_adj) { - uint_fast8_t n = 0; - do { - int_fast8_t const d = (int_fast8_t)(val % base); - *buf++ = (char)(((d < 10) ? '0' : ('A' - 10 + case_adj)) + d); - ++n; - val /= base; - } while (val); - return (int)n; -} - -#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1 - -#include - -#if (DBL_MANT_DIG <= 11) && (DBL_MAX_EXP <= 16) - typedef uint_fast16_t npf_double_bin_t; - typedef int_fast8_t npf_ftoa_exp_t; -#elif (DBL_MANT_DIG <= 24) && (DBL_MAX_EXP <= 128) - typedef uint_fast32_t npf_double_bin_t; - typedef int_fast8_t npf_ftoa_exp_t; -#elif (DBL_MANT_DIG <= 53) && (DBL_MAX_EXP <= 1024) - typedef uint_fast64_t npf_double_bin_t; - typedef int_fast16_t npf_ftoa_exp_t; -#else - #error Unsupported width of the double type. -#endif - -// The floating point conversion code works with an unsigned integer type of any size. -#ifndef NANOPRINTF_CONVERSION_FLOAT_TYPE - #define NANOPRINTF_CONVERSION_FLOAT_TYPE unsigned int -#endif -typedef NANOPRINTF_CONVERSION_FLOAT_TYPE npf_ftoa_man_t; - -#if (NANOPRINTF_CONVERSION_BUFFER_SIZE <= UINT_FAST8_MAX) && (UINT_FAST8_MAX <= INT_MAX) - typedef uint_fast8_t npf_ftoa_dec_t; -#else - typedef int npf_ftoa_dec_t; -#endif - -enum { - NPF_DOUBLE_EXP_MASK = DBL_MAX_EXP * 2 - 1, - NPF_DOUBLE_EXP_BIAS = DBL_MAX_EXP - 1, - NPF_DOUBLE_MAN_BITS = DBL_MANT_DIG - 1, - NPF_DOUBLE_BIN_BITS = sizeof(npf_double_bin_t) * CHAR_BIT, - NPF_FTOA_MAN_BITS = sizeof(npf_ftoa_man_t) * CHAR_BIT, - NPF_FTOA_SHIFT_BITS = - ((NPF_FTOA_MAN_BITS < DBL_MANT_DIG) ? NPF_FTOA_MAN_BITS : DBL_MANT_DIG) - 1 -}; - -/* Generally, floating-point conversion implementations use - grisu2 (https://bit.ly/2JgMggX) and ryu (https://bit.ly/2RLXSg0) algorithms, - which are mathematically exact and fast, but require large lookup tables. - - This implementation was inspired by Wojciech Muła's (zdjęcia@garnek.pl) - algorithm (http://0x80.pl/notesen/2015-12-29-float-to-string.html) and - extended further by adding dynamic scaling and configurable integer width by - Oskars Rubenis (https://github.com/Okarss). */ - -static int npf_ftoa_rev(char *buf, npf_format_spec_t const *spec, double f) { - char const *ret = NULL; - npf_double_bin_t bin; { // Union-cast is UB pre-C11, compiler optimizes byte-copy loop. - char const *src = (char const *)&f; - char *dst = (char *)&bin; - for (uint_fast8_t i = 0; i < sizeof(f); ++i) { dst[i] = src[i]; } - } - - // Unsigned -> signed int casting is IB and can raise a signal but generally doesn't. - npf_ftoa_exp_t exp = - (npf_ftoa_exp_t)((npf_ftoa_exp_t)(bin >> NPF_DOUBLE_MAN_BITS) & NPF_DOUBLE_EXP_MASK); - - bin &= ((npf_double_bin_t)0x1 << NPF_DOUBLE_MAN_BITS) - 1; - if (exp == (npf_ftoa_exp_t)NPF_DOUBLE_EXP_MASK) { // special value - ret = (bin) ? "NAN" : "FNI"; - goto exit; - } - if (spec->prec > (NANOPRINTF_CONVERSION_BUFFER_SIZE - 2)) { goto exit; } - if (exp) { // normal number - bin |= (npf_double_bin_t)0x1 << NPF_DOUBLE_MAN_BITS; - } else { // subnormal number - ++exp; - } - exp = (npf_ftoa_exp_t)(exp - NPF_DOUBLE_EXP_BIAS); - - uint_fast8_t carry; carry = 0; - npf_ftoa_dec_t end, dec; dec = (npf_ftoa_dec_t)spec->prec; - if (dec || spec->alt_form) { - buf[dec++] = '.'; - } - - { // Integer part - npf_ftoa_man_t man_i; - - if (exp >= 0) { - int_fast8_t shift_i = - (int_fast8_t)((exp > NPF_FTOA_SHIFT_BITS) ? (int)NPF_FTOA_SHIFT_BITS : exp); - npf_ftoa_exp_t exp_i = (npf_ftoa_exp_t)(exp - shift_i); - shift_i = (int_fast8_t)(NPF_DOUBLE_MAN_BITS - shift_i); - man_i = (npf_ftoa_man_t)(bin >> shift_i); - - if (exp_i) { - if (shift_i) { - carry = (bin >> (shift_i - 1)) & 0x1; - } - exp = NPF_DOUBLE_MAN_BITS; // invalidate the fraction part - } - - // Scale the exponent from base-2 to base-10. - for (; exp_i; --exp_i) { - if (!(man_i & ((npf_ftoa_man_t)0x1 << (NPF_FTOA_MAN_BITS - 1)))) { - man_i = (npf_ftoa_man_t)(man_i << 1); - man_i = (npf_ftoa_man_t)(man_i | carry); carry = 0; - } else { - if (dec >= NANOPRINTF_CONVERSION_BUFFER_SIZE) { goto exit; } - buf[dec++] = '0'; - carry = (((uint_fast8_t)(man_i % 5) + carry) > 2); - man_i /= 5; - } - } - } else { - man_i = 0; - } - end = dec; - - do { // Print the integer - if (end >= NANOPRINTF_CONVERSION_BUFFER_SIZE) { goto exit; } - buf[end++] = (char)('0' + (char)(man_i % 10)); - man_i /= 10; - } while (man_i); - } - - { // Fraction part - npf_ftoa_man_t man_f; - npf_ftoa_dec_t dec_f = (npf_ftoa_dec_t)spec->prec; - - if (exp < NPF_DOUBLE_MAN_BITS) { - int_fast8_t shift_f = (int_fast8_t)((exp < 0) ? -1 : exp); - npf_ftoa_exp_t exp_f = (npf_ftoa_exp_t)(exp - shift_f); - npf_double_bin_t bin_f = - bin << ((NPF_DOUBLE_BIN_BITS - NPF_DOUBLE_MAN_BITS) + shift_f); - - // This if-else statement can be completely optimized at compile time. - if (NPF_DOUBLE_BIN_BITS > NPF_FTOA_MAN_BITS) { - man_f = (npf_ftoa_man_t)(bin_f >> ((unsigned)(NPF_DOUBLE_BIN_BITS - - NPF_FTOA_MAN_BITS) % - NPF_DOUBLE_BIN_BITS)); - carry = (uint_fast8_t)((bin_f >> ((unsigned)(NPF_DOUBLE_BIN_BITS - - NPF_FTOA_MAN_BITS - 1) % - NPF_DOUBLE_BIN_BITS)) & 0x1); - } else { - man_f = (npf_ftoa_man_t)((npf_ftoa_man_t)bin_f - << ((unsigned)(NPF_FTOA_MAN_BITS - - NPF_DOUBLE_BIN_BITS) % NPF_FTOA_MAN_BITS)); - carry = 0; - } - - // Scale the exponent from base-2 to base-10 and prepare the first digit. - for (uint_fast8_t digit = 0; dec_f && (exp_f < 4); ++exp_f) { - if ((man_f > ((npf_ftoa_man_t)-4 / 5)) || digit) { - carry = (uint_fast8_t)(man_f & 0x1); - man_f = (npf_ftoa_man_t)(man_f >> 1); - } else { - man_f = (npf_ftoa_man_t)(man_f * 5); - if (carry) { man_f = (npf_ftoa_man_t)(man_f + 3); carry = 0; } - if (exp_f < 0) { - buf[--dec_f] = '0'; - } else { - ++digit; - } - } - } - man_f = (npf_ftoa_man_t)(man_f + carry); - carry = (exp_f >= 0); - dec = 0; - } else { - man_f = 0; - } - - if (dec_f) { - // Print the fraction - for (;;) { - buf[--dec_f] = (char)('0' + (char)(man_f >> (NPF_FTOA_MAN_BITS - 4))); - man_f = (npf_ftoa_man_t)(man_f & ~((npf_ftoa_man_t)0xF << (NPF_FTOA_MAN_BITS - 4))); - if (!dec_f) { break; } - man_f = (npf_ftoa_man_t)(man_f * 10); - } - man_f = (npf_ftoa_man_t)(man_f << 4); - } - if (exp < NPF_DOUBLE_MAN_BITS) { - carry &= (uint_fast8_t)(man_f >> (NPF_FTOA_MAN_BITS - 1)); - } - } - - // Round the number - for (; carry; ++dec) { - if (dec >= NANOPRINTF_CONVERSION_BUFFER_SIZE) { goto exit; } - if (dec >= end) { buf[end++] = '0'; } - if (buf[dec] == '.') { continue; } - carry = (buf[dec] == '9'); - buf[dec] = (char)(carry ? '0' : (buf[dec] + 1)); - } - - return (int)end; -exit: - if (!ret) { ret = "RRE"; } - uint_fast8_t i; - for (i = 0; ret[i]; ++i) { buf[i] = (char)(ret[i] + spec->case_adjust); } - return (int)i; -} - -#endif // NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS - -#if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1 -static int npf_bin_len(npf_uint_t u) { - // Return the length of the binary string format of 'u', preferring intrinsics. - if (!u) { return 1; } - -#ifdef _MSC_VER // Win64, use _BSR64 for everything. If x86, use _BSR when non-large. - #ifdef _M_X64 - #define NPF_HAVE_BUILTIN_CLZ - #define NPF_CLZ _BitScanReverse64 - #elif NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 0 - #define NPF_HAVE_BUILTIN_CLZ - #define NPF_CLZ _BitScanReverse - #endif - #ifdef NPF_HAVE_BUILTIN_CLZ - unsigned long idx; - NPF_CLZ(&idx, u); - return (int)(idx + 1); - #endif -#elif NANOPRINTF_CLANG || NANOPRINTF_GCC_PAST_4_6 - #define NPF_HAVE_BUILTIN_CLZ - #if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1 - #define NPF_CLZ(X) ((sizeof(long long) * CHAR_BIT) - (size_t)__builtin_clzll(X)) - #else - #define NPF_CLZ(X) ((sizeof(long) * CHAR_BIT) - (size_t)__builtin_clzl(X)) - #endif - return (int)NPF_CLZ(u); -#endif - -#ifndef NPF_HAVE_BUILTIN_CLZ - int n; - for (n = 0; u; ++n, u >>= 1); // slow but small software fallback - return n; -#else - #undef NPF_HAVE_BUILTIN_CLZ - #undef NPF_CLZ -#endif -} -#endif - -static void npf_bufputc(int c, void *ctx) { - npf_bufputc_ctx_t *bpc = (npf_bufputc_ctx_t *)ctx; - if (bpc->cur < bpc->len) { bpc->dst[bpc->cur++] = (char)c; } -} - -static void npf_bufputc_nop(int c, void *ctx) { (void)c; (void)ctx; } - -typedef struct npf_cnt_putc_ctx { - npf_putc pc; - void *ctx; - int n; -} npf_cnt_putc_ctx_t; - -static void npf_putc_cnt(int c, void *ctx) { - npf_cnt_putc_ctx_t *pc_cnt = (npf_cnt_putc_ctx_t *)ctx; - ++pc_cnt->n; - pc_cnt->pc(c, pc_cnt->ctx); // sibling-call optimization -} - -#define NPF_PUTC(VAL) do { npf_putc_cnt((int)(VAL), &pc_cnt); } while (0) - -#define NPF_EXTRACT(MOD, CAST_TO, EXTRACT_AS) \ - case NPF_FMT_SPEC_LEN_MOD_##MOD: val = (CAST_TO)va_arg(args, EXTRACT_AS); break - -#define NPF_WRITEBACK(MOD, TYPE) \ - case NPF_FMT_SPEC_LEN_MOD_##MOD: *(va_arg(args, TYPE *)) = (TYPE)pc_cnt.n; break - -int npf_vpprintf(npf_putc pc, void *pc_ctx, char const *format, va_list args) { - npf_format_spec_t fs; - char const *cur = format; - npf_cnt_putc_ctx_t pc_cnt; - pc_cnt.pc = pc; - pc_cnt.ctx = pc_ctx; - pc_cnt.n = 0; - - while (*cur) { - int const fs_len = (*cur != '%') ? 0 : npf_parse_format_spec(cur, &fs); - if (!fs_len) { NPF_PUTC(*cur++); continue; } - cur += fs_len; - - // Extract star-args immediately -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - if (fs.field_width_opt == NPF_FMT_SPEC_OPT_STAR) { - fs.field_width = va_arg(args, int); - if (fs.field_width < 0) { - fs.field_width = -fs.field_width; - fs.left_justified = 1; - } - } -#endif -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - if (fs.prec_opt == NPF_FMT_SPEC_OPT_STAR) { - fs.prec = va_arg(args, int); - if (fs.prec < 0) { fs.prec_opt = NPF_FMT_SPEC_OPT_NONE; } - } -#endif - - union { char cbuf_mem[NANOPRINTF_CONVERSION_BUFFER_SIZE]; npf_uint_t binval; } u; - char *cbuf = u.cbuf_mem, sign_c = 0; - int cbuf_len = 0, need_0x = 0; -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - int field_pad = 0; - char pad_c = 0; -#endif -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - int prec_pad = 0; -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - int zero = 0; -#endif -#endif - - // Extract and convert the argument to string, point cbuf at the text. - switch (fs.conv_spec) { - case NPF_FMT_SPEC_CONV_PERCENT: - *cbuf = '%'; - cbuf_len = 1; - break; - - case NPF_FMT_SPEC_CONV_CHAR: - *cbuf = (char)va_arg(args, int); - cbuf_len = 1; - break; - - case NPF_FMT_SPEC_CONV_STRING: { - cbuf = va_arg(args, char *); -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - for (char const *s = cbuf; - ((fs.prec_opt == NPF_FMT_SPEC_OPT_NONE) || (cbuf_len < fs.prec)) && *s; - ++s, ++cbuf_len); -#else - for (char const *s = cbuf; *s; ++s, ++cbuf_len); // strlen -#endif - } break; - - case NPF_FMT_SPEC_CONV_SIGNED_INT: { - npf_int_t val = 0; - switch (fs.length_modifier) { - NPF_EXTRACT(NONE, int, int); - NPF_EXTRACT(SHORT, short, int); - NPF_EXTRACT(LONG_DOUBLE, int, int); - NPF_EXTRACT(CHAR, char, int); - NPF_EXTRACT(LONG, long, long); -#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1 - NPF_EXTRACT(LARGE_LONG_LONG, long long, long long); - NPF_EXTRACT(LARGE_INTMAX, intmax_t, intmax_t); - NPF_EXTRACT(LARGE_SIZET, npf_ssize_t, npf_ssize_t); - NPF_EXTRACT(LARGE_PTRDIFFT, ptrdiff_t, ptrdiff_t); -#endif - default: break; - } - - sign_c = (val < 0) ? '-' : fs.prepend; - -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - zero = !val; -#endif - // special case, if prec and value are 0, skip - if (!val && (fs.prec_opt != NPF_FMT_SPEC_OPT_NONE) && !fs.prec) { - cbuf_len = 0; - } else -#endif - { - npf_uint_t uval = (npf_uint_t)val; - if (val < 0) { uval = 0 - uval; } - cbuf_len = npf_utoa_rev(uval, cbuf, 10, fs.case_adjust); - } - } break; - -#if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1 - case NPF_FMT_SPEC_CONV_BINARY: -#endif - case NPF_FMT_SPEC_CONV_OCTAL: - case NPF_FMT_SPEC_CONV_HEX_INT: - case NPF_FMT_SPEC_CONV_UNSIGNED_INT: { - npf_uint_t val = 0; - - switch (fs.length_modifier) { - NPF_EXTRACT(NONE, unsigned, unsigned); - NPF_EXTRACT(SHORT, unsigned short, unsigned); - NPF_EXTRACT(LONG_DOUBLE, unsigned, unsigned); - NPF_EXTRACT(CHAR, unsigned char, unsigned); - NPF_EXTRACT(LONG, unsigned long, unsigned long); -#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1 - NPF_EXTRACT(LARGE_LONG_LONG, unsigned long long, unsigned long long); - NPF_EXTRACT(LARGE_INTMAX, uintmax_t, uintmax_t); - NPF_EXTRACT(LARGE_SIZET, size_t, size_t); - NPF_EXTRACT(LARGE_PTRDIFFT, size_t, size_t); -#endif - default: break; - } - -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - zero = !val; -#endif - if (!val && (fs.prec_opt != NPF_FMT_SPEC_OPT_NONE) && !fs.prec) { - // Zero value and explicitly-requested zero precision means "print nothing". - if ((fs.conv_spec == NPF_FMT_SPEC_CONV_OCTAL) && fs.alt_form) { - fs.prec = 1; // octal special case, print a single '0' - } - } else -#endif -#if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1 - if (fs.conv_spec == NPF_FMT_SPEC_CONV_BINARY) { - cbuf_len = npf_bin_len(val); u.binval = val; - } else -#endif - { - uint_fast8_t const base = (fs.conv_spec == NPF_FMT_SPEC_CONV_OCTAL) ? - 8u : ((fs.conv_spec == NPF_FMT_SPEC_CONV_HEX_INT) ? 16u : 10u); - cbuf_len = npf_utoa_rev(val, cbuf, base, fs.case_adjust); - } - - if (val && fs.alt_form && (fs.conv_spec == NPF_FMT_SPEC_CONV_OCTAL)) { - cbuf[cbuf_len++] = '0'; // OK to add leading octal '0' immediately. - } - - if (val && fs.alt_form) { // 0x or 0b but can't write it yet. - if (fs.conv_spec == NPF_FMT_SPEC_CONV_HEX_INT) { need_0x = 'X'; } -#if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1 - else if (fs.conv_spec == NPF_FMT_SPEC_CONV_BINARY) { need_0x = 'B'; } -#endif - if (need_0x) { need_0x += fs.case_adjust; } - } - } break; - - case NPF_FMT_SPEC_CONV_POINTER: { - cbuf_len = - npf_utoa_rev((npf_uint_t)(uintptr_t)va_arg(args, void *), cbuf, 16, 'a' - 'A'); - //need_0x = 'x'; - } break; - -#if NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS == 1 - case NPF_FMT_SPEC_CONV_WRITEBACK: - switch (fs.length_modifier) { - NPF_WRITEBACK(NONE, int); - NPF_WRITEBACK(SHORT, short); - NPF_WRITEBACK(LONG, long); - NPF_WRITEBACK(LONG_DOUBLE, double); - NPF_WRITEBACK(CHAR, signed char); -#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1 - NPF_WRITEBACK(LARGE_LONG_LONG, long long); - NPF_WRITEBACK(LARGE_INTMAX, intmax_t); - NPF_WRITEBACK(LARGE_SIZET, size_t); - NPF_WRITEBACK(LARGE_PTRDIFFT, ptrdiff_t); -#endif - default: break; - } break; -#endif - -#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1 - case NPF_FMT_SPEC_CONV_FLOAT_DEC: - case NPF_FMT_SPEC_CONV_FLOAT_SCI: - case NPF_FMT_SPEC_CONV_FLOAT_SHORTEST: - case NPF_FMT_SPEC_CONV_FLOAT_HEX: { - double val; - if (fs.length_modifier == NPF_FMT_SPEC_LEN_MOD_LONG_DOUBLE) { - val = (double)va_arg(args, long double); - } else { - val = va_arg(args, double); - } - - sign_c = (val < 0.) ? '-' : fs.prepend; -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - zero = (val == 0.); -#endif - cbuf_len = npf_ftoa_rev(cbuf, &fs, val); - } break; -#endif - default: break; - } - -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - // Compute the field width pad character - if (fs.field_width_opt != NPF_FMT_SPEC_OPT_NONE) { - if (fs.leading_zero_pad) { // '0' flag is only legal with numeric types - if ((fs.conv_spec != NPF_FMT_SPEC_CONV_STRING) && - (fs.conv_spec != NPF_FMT_SPEC_CONV_CHAR) && - (fs.conv_spec != NPF_FMT_SPEC_CONV_PERCENT)) { -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - if ((fs.prec_opt != NPF_FMT_SPEC_OPT_NONE) && !fs.prec && zero) { - pad_c = ' '; - } else -#endif - { pad_c = '0'; } - } - } else { pad_c = ' '; } - } -#endif - - // Compute the number of bytes to truncate or '0'-pad. -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - if (fs.conv_spec != NPF_FMT_SPEC_CONV_STRING) { -#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1 - // float precision is after the decimal point - if ((fs.conv_spec != NPF_FMT_SPEC_CONV_FLOAT_DEC) && - (fs.conv_spec != NPF_FMT_SPEC_CONV_FLOAT_SCI) && - (fs.conv_spec != NPF_FMT_SPEC_CONV_FLOAT_SHORTEST) && - (fs.conv_spec != NPF_FMT_SPEC_CONV_FLOAT_HEX)) -#endif - { prec_pad = npf_max(0, fs.prec - cbuf_len); } - } -#endif - -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - // Given the full converted length, how many pad bytes? - field_pad = fs.field_width - cbuf_len - !!sign_c; - if (need_0x) { field_pad -= 2; } -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - field_pad -= prec_pad; -#endif - field_pad = npf_max(0, field_pad); - - // Apply right-justified field width if requested - if (!fs.left_justified && pad_c) { // If leading zeros pad, sign goes first. - if (pad_c == '0') { - if (sign_c) { NPF_PUTC(sign_c); sign_c = 0; } - // Pad byte is '0', write '0x' before '0' pad chars. - if (need_0x) { NPF_PUTC('0'); NPF_PUTC(need_0x); } - } - while (field_pad-- > 0) { NPF_PUTC(pad_c); } - // Pad byte is ' ', write '0x' after ' ' pad chars but before number. - if ((pad_c != '0') && need_0x) { NPF_PUTC('0'); NPF_PUTC(need_0x); } - } else -#endif - { if (need_0x) { NPF_PUTC('0'); NPF_PUTC(need_0x); } } // no pad, '0x' requested. - - // Write the converted payload - if (fs.conv_spec == NPF_FMT_SPEC_CONV_STRING) { - for (int i = 0; i < cbuf_len; ++i) { NPF_PUTC(cbuf[i]); } - } else { - if (sign_c) { NPF_PUTC(sign_c); } -#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 - while (prec_pad-- > 0) { NPF_PUTC('0'); } // int precision leads. -#endif -#if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1 - if (fs.conv_spec == NPF_FMT_SPEC_CONV_BINARY) { - while (cbuf_len) { NPF_PUTC('0' + ((u.binval >> --cbuf_len) & 1)); } - } else -#endif - { while (cbuf_len-- > 0) { NPF_PUTC(cbuf[cbuf_len]); } } // payload is reversed - } - -#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1 - if (fs.left_justified && pad_c) { // Apply left-justified field width - while (field_pad-- > 0) { NPF_PUTC(pad_c); } - } -#endif - } - - return pc_cnt.n; -} - -#undef NPF_PUTC -#undef NPF_EXTRACT -#undef NPF_WRITEBACK - -int npf_pprintf(npf_putc pc, void *pc_ctx, char const *format, ...) { - va_list val; - va_start(val, format); - int const rv = npf_vpprintf(pc, pc_ctx, format, val); - va_end(val); - return rv; -} - -int npf_snprintf(char *buffer, size_t bufsz, const char *format, ...) { - va_list val; - va_start(val, format); - int const rv = npf_vsnprintf(buffer, bufsz, format, val); - va_end(val); - return rv; -} - -int npf_vsnprintf(char *buffer, size_t bufsz, char const *format, va_list vlist) { - npf_bufputc_ctx_t bufputc_ctx; - bufputc_ctx.dst = buffer; - bufputc_ctx.len = bufsz; - bufputc_ctx.cur = 0; - - npf_putc const pc = buffer ? npf_bufputc : npf_bufputc_nop; - int const n = npf_vpprintf(pc, &bufputc_ctx, format, vlist); - pc('\0', &bufputc_ctx); - - if (buffer && bufsz) { -#ifdef NANOPRINTF_SNPRINTF_SAFE_EMPTY_STRING_ON_OVERFLOW - if (n >= (int)bufsz) { buffer[0] = '\0'; } -#else - buffer[bufsz - 1] = '\0'; -#endif - } - - return n; -} - -#if NANOPRINTF_HAVE_GCC_WARNING_PRAGMAS - #pragma GCC diagnostic pop -#endif - -#ifdef _MSC_VER - #pragma warning(pop) -#endif - -#endif // NANOPRINTF_IMPLEMENTATION_INCLUDED -#endif // NANOPRINTF_IMPLEMENTATION - -/* - nanoprintf is dual-licensed under both the "Unlicense" and the - "Zero-Clause BSD" (0BSD) licenses. The intent of this dual-licensing - structure is to make nanoprintf as consumable as possible in as many - environments / countries / companies as possible without any - encumberances. - - The text of the two licenses follows below: - - ============================== UNLICENSE ============================== - - This is free and unencumbered software released into the public domain. - - Anyone is free to copy, modify, publish, use, compile, sell, or - distribute this software, either in source code form or as a compiled - binary, for any purpose, commercial or non-commercial, and by any - means. - - In jurisdictions that recognize copyright laws, the author or authors - of this software dedicate any and all copyright interest in the - software to the public domain. We make this dedication for the benefit - of the public at large and to the detriment of our heirs and - successors. We intend this dedication to be an overt act of - relinquishment in perpetuity of all present and future rights to this - software under copyright law. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR - OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - OTHER DEALINGS IN THE SOFTWARE. - - For more information, please refer to - - ================================ 0BSD ================================= - - Copyright (C) 2019- by Charles Nicholson - - Permission to use, copy, modify, and/or distribute this software for - any purpose with or without fee is hereby granted. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ diff --git a/src/oboskrnl/external/stb_sprintf.h b/src/oboskrnl/external/stb_sprintf.h new file mode 100644 index 00000000..7847b8fd --- /dev/null +++ b/src/oboskrnl/external/stb_sprintf.h @@ -0,0 +1,1906 @@ +// stb_sprintf - v1.10 - public domain snprintf() implementation +// originally by Jeff Roberts / RAD Game Tools, 2015/10/20 +// http://github.com/nothings/stb +// +// allowed types: sc uidBboXx p AaGgEef n +// lengths : hh h ll j z t I64 I32 I +// +// Contributors: +// Fabian "ryg" Giesen (reformatting) +// github:aganm (attribute format) +// +// Contributors (bugfixes): +// github:d26435 +// github:trex78 +// github:account-login +// Jari Komppa (SI suffixes) +// Rohit Nirmal +// Marcin Wojdyr +// Leonard Ritter +// Stefano Zanotti +// Adam Allison +// Arvid Gerstmann +// Markus Kolb +// +// LICENSE: +// +// See end of file for license information. + +#ifndef STB_SPRINTF_H_INCLUDE +#define STB_SPRINTF_H_INCLUDE + +/* +Single file sprintf replacement. + +Originally written by Jeff Roberts at RAD Game Tools - 2015/10/20. +Hereby placed in public domain. + +This is a full sprintf replacement that supports everything that +the C runtime sprintfs support, including float/double, 64-bit integers, +hex floats, field parameters (%*.*d stuff), length reads backs, etc. + +Why would you need this if sprintf already exists? Well, first off, +it's *much* faster (see below). It's also much smaller than the CRT +versions code-space-wise. We've also added some simple improvements +that are super handy (commas in thousands, callbacks at buffer full, +for example). Finally, the format strings for MSVC and GCC differ +for 64-bit integers (among other small things), so this lets you use +the same format strings in cross platform code. + +It uses the standard single file trick of being both the header file +and the source itself. If you just include it normally, you just get +the header file function definitions. To get the code, you include +it from a C or C++ file and define STB_SPRINTF_IMPLEMENTATION first. + +It only uses va_args macros from the C runtime to do it's work. It +does cast doubles to S64s and shifts and divides U64s, which does +drag in CRT code on most platforms. + +It compiles to roughly 8K with float support, and 4K without. +As a comparison, when using MSVC static libs, calling sprintf drags +in 16K. + +API: +==== +int stbsp_sprintf( char * buf, char const * fmt, ... ) +int stbsp_snprintf( char * buf, int count, char const * fmt, ... ) + Convert an arg list into a buffer. stbsp_snprintf always returns + a zero-terminated string (unlike regular snprintf). + +int stbsp_vsprintf( char * buf, char const * fmt, va_list va ) +int stbsp_vsnprintf( char * buf, int count, char const * fmt, va_list va ) + Convert a va_list arg list into a buffer. stbsp_vsnprintf always returns + a zero-terminated string (unlike regular snprintf). + +int stbsp_vsprintfcb( STBSP_SPRINTFCB * callback, void * user, char * buf, char const * fmt, va_list va ) + typedef char * STBSP_SPRINTFCB( char const * buf, void * user, int len ); + Convert into a buffer, calling back every STB_SPRINTF_MIN chars. + Your callback can then copy the chars out, print them or whatever. + This function is actually the workhorse for everything else. + The buffer you pass in must hold at least STB_SPRINTF_MIN characters. + // you return the next buffer to use or 0 to stop converting + +void stbsp_set_separators( char comma, char period ) + Set the comma and period characters to use. + +FLOATS/DOUBLES: +=============== +This code uses a internal float->ascii conversion method that uses +doubles with error correction (double-doubles, for ~105 bits of +precision). This conversion is round-trip perfect - that is, an atof +of the values output here will give you the bit-exact double back. + +One difference is that our insignificant digits will be different than +with MSVC or GCC (but they don't match each other either). We also +don't attempt to find the minimum length matching float (pre-MSVC15 +doesn't either). + +If you don't need float or doubles at all, define STB_SPRINTF_NOFLOAT +and you'll save 4K of code space. + +64-BIT INTS: +============ +This library also supports 64-bit integers and you can use MSVC style or +GCC style indicators (%I64d or %lld). It supports the C99 specifiers +for size_t and ptr_diff_t (%jd %zd) as well. + +EXTRAS: +======= +Like some GCCs, for integers and floats, you can use a ' (single quote) +specifier and commas will be inserted on the thousands: "%'d" on 12345 +would print 12,345. + +For integers and floats, you can use a "$" specifier and the number +will be converted to float and then divided to get kilo, mega, giga or +tera and then printed, so "%$d" 1000 is "1.0 k", "%$.2d" 2536000 is +"2.53 M", etc. For byte values, use two $:s, like "%$$d" to turn +2536000 to "2.42 Mi". If you prefer JEDEC suffixes to SI ones, use three +$:s: "%$$$d" -> "2.42 M". To remove the space between the number and the +suffix, add "_" specifier: "%_$d" -> "2.53M". + +In addition to octal and hexadecimal conversions, you can print +integers in binary: "%b" for 256 would print 100. + +PERFORMANCE vs MSVC 2008 32-/64-bit (GCC is even slower than MSVC): +=================================================================== +"%d" across all 32-bit ints (4.8x/4.0x faster than 32-/64-bit MSVC) +"%24d" across all 32-bit ints (4.5x/4.2x faster) +"%x" across all 32-bit ints (4.5x/3.8x faster) +"%08x" across all 32-bit ints (4.3x/3.8x faster) +"%f" across e-10 to e+10 floats (7.3x/6.0x faster) +"%e" across e-10 to e+10 floats (8.1x/6.0x faster) +"%g" across e-10 to e+10 floats (10.0x/7.1x faster) +"%f" for values near e-300 (7.9x/6.5x faster) +"%f" for values near e+300 (10.0x/9.1x faster) +"%e" for values near e-300 (10.1x/7.0x faster) +"%e" for values near e+300 (9.2x/6.0x faster) +"%.320f" for values near e-300 (12.6x/11.2x faster) +"%a" for random values (8.6x/4.3x faster) +"%I64d" for 64-bits with 32-bit values (4.8x/3.4x faster) +"%I64d" for 64-bits > 32-bit values (4.9x/5.5x faster) +"%s%s%s" for 64 char strings (7.1x/7.3x faster) +"...512 char string..." ( 35.0x/32.5x faster!) +*/ + +#if defined(__clang__) + #if defined(__has_feature) && defined(__has_attribute) + #if __has_feature(address_sanitizer) + #if __has_attribute(__no_sanitize__) + #define STBSP__ASAN __attribute__((__no_sanitize__("address"))) + #elif __has_attribute(__no_sanitize_address__) + #define STBSP__ASAN __attribute__((__no_sanitize_address__)) + #elif __has_attribute(__no_address_safety_analysis__) + #define STBSP__ASAN __attribute__((__no_address_safety_analysis__)) + #endif + #endif + #endif +#elif defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) + #if defined(__SANITIZE_ADDRESS__) && __SANITIZE_ADDRESS__ + #define STBSP__ASAN __attribute__((__no_sanitize_address__)) + #endif +#endif + +#ifndef STBSP__ASAN +#define STBSP__ASAN +#endif + +#ifdef STB_SPRINTF_STATIC +#define STBSP__PUBLICDEC static +#define STBSP__PUBLICDEF static STBSP__ASAN +#else +#ifdef __cplusplus +#define STBSP__PUBLICDEC extern "C" __attribute__((no_sanitize("undefined"))) +#define STBSP__PUBLICDEF extern "C" __attribute__((no_sanitize("undefined"))) STBSP__ASAN +#else +#define STBSP__PUBLICDEC extern +#define STBSP__PUBLICDEF STBSP__ASAN +#endif +#endif + +#if defined(__has_attribute) + #if __has_attribute(format) + #define STBSP__ATTRIBUTE_FORMAT(fmt,va) __attribute__((format(printf,fmt,va))) + #endif +#endif + +#ifndef STBSP__ATTRIBUTE_FORMAT +#define STBSP__ATTRIBUTE_FORMAT(fmt,va) +#endif + +#ifdef _MSC_VER +#define STBSP__NOTUSED(v) (void)(v) +#else +#define STBSP__NOTUSED(v) (void)sizeof(v) +#endif + +#include // for va_arg(), va_list() +#include // size_t, ptrdiff_t + +#ifndef STB_SPRINTF_MIN +#define STB_SPRINTF_MIN 512 // how many characters per callback +#endif +typedef char *STBSP_SPRINTFCB(const char *buf, void *user, int len); + +#ifndef STB_SPRINTF_DECORATE +#define STB_SPRINTF_DECORATE(name) stbsp_##name // define this before including if you want to change the names +#endif + +STBSP__PUBLICDEC int STB_SPRINTF_DECORATE(vsprintf)(char *buf, char const *fmt, va_list va); +STBSP__PUBLICDEC int STB_SPRINTF_DECORATE(vsnprintf)(char *buf, int count, char const *fmt, va_list va); +STBSP__PUBLICDEC int STB_SPRINTF_DECORATE(sprintf)(char *buf, char const *fmt, ...) STBSP__ATTRIBUTE_FORMAT(2,3); +STBSP__PUBLICDEC int STB_SPRINTF_DECORATE(snprintf)(char *buf, int count, char const *fmt, ...) STBSP__ATTRIBUTE_FORMAT(3,4); + +STBSP__PUBLICDEC int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback, void *user, char *buf, char const *fmt, va_list va); +STBSP__PUBLICDEC void STB_SPRINTF_DECORATE(set_separators)(char comma, char period); + +#endif // STB_SPRINTF_H_INCLUDE + +#ifdef STB_SPRINTF_IMPLEMENTATION + +#define stbsp__uint32 unsigned int +#define stbsp__int32 signed int + +#ifdef _MSC_VER +#define stbsp__uint64 unsigned __int64 +#define stbsp__int64 signed __int64 +#else +#define stbsp__uint64 unsigned long long +#define stbsp__int64 signed long long +#endif +#define stbsp__uint16 unsigned short + +#ifndef stbsp__uintptr +#if defined(__ppc64__) || defined(__powerpc64__) || defined(__aarch64__) || defined(_M_X64) || defined(__x86_64__) || defined(__x86_64) || defined(__s390x__) +#define stbsp__uintptr stbsp__uint64 +#else +#define stbsp__uintptr stbsp__uint32 +#endif +#endif + +#ifndef STB_SPRINTF_MSVC_MODE // used for MSVC2013 and earlier (MSVC2015 matches GCC) +#if defined(_MSC_VER) && (_MSC_VER < 1900) +#define STB_SPRINTF_MSVC_MODE +#endif +#endif + +#ifdef STB_SPRINTF_NOUNALIGNED // define this before inclusion to force stbsp_sprintf to always use aligned accesses +#define STBSP__UNALIGNED(code) +#else +#define STBSP__UNALIGNED(code) code +#endif + +#ifndef STB_SPRINTF_NOFLOAT +// internal float utility functions +static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, char *out, stbsp__int32 *decimal_pos, double value, stbsp__uint32 frac_digits); +static stbsp__int32 stbsp__real_to_parts(stbsp__int64 *bits, stbsp__int32 *expo, double value); +#define STBSP__SPECIAL 0x7000 +#endif + +static char stbsp__period = '.'; +static char stbsp__comma = ','; +static struct +{ + short temp; // force next field to be 2-byte aligned + char pair[201]; +} stbsp__digitpair = +{ + 0, + "00010203040506070809101112131415161718192021222324" + "25262728293031323334353637383940414243444546474849" + "50515253545556575859606162636465666768697071727374" + "75767778798081828384858687888990919293949596979899" +}; + +STBSP__PUBLICDEF void STB_SPRINTF_DECORATE(set_separators)(char pcomma, char pperiod) +{ + stbsp__period = pperiod; + stbsp__comma = pcomma; +} + +#define STBSP__LEFTJUST 1 +#define STBSP__LEADINGPLUS 2 +#define STBSP__LEADINGSPACE 4 +#define STBSP__LEADING_0X 8 +#define STBSP__LEADINGZERO 16 +#define STBSP__INTMAX 32 +#define STBSP__TRIPLET_COMMA 64 +#define STBSP__NEGATIVE 128 +#define STBSP__METRIC_SUFFIX 256 +#define STBSP__HALFWIDTH 512 +#define STBSP__METRIC_NOSPACE 1024 +#define STBSP__METRIC_1024 2048 +#define STBSP__METRIC_JEDEC 4096 + +static void stbsp__lead_sign(stbsp__uint32 fl, char *sign) +{ + sign[0] = 0; + if (fl & STBSP__NEGATIVE) { + sign[0] = 1; + sign[1] = '-'; + } else if (fl & STBSP__LEADINGSPACE) { + sign[0] = 1; + sign[1] = ' '; + } else if (fl & STBSP__LEADINGPLUS) { + sign[0] = 1; + sign[1] = '+'; + } +} + +static STBSP__ASAN stbsp__uint32 stbsp__strlen_limited(char const *s, stbsp__uint32 limit) +{ + char const * sn = s; + + // get up to 4-byte alignment + for (;;) { + if (((stbsp__uintptr)sn & 3) == 0) + break; + + if (!limit || *sn == 0) + return (stbsp__uint32)(sn - s); + + ++sn; + --limit; + } + + // scan over 4 bytes at a time to find terminating 0 + // this will intentionally scan up to 3 bytes past the end of buffers, + // but becase it works 4B aligned, it will never cross page boundaries + // (hence the STBSP__ASAN markup; the over-read here is intentional + // and harmless) + while (limit >= 4) { + stbsp__uint32 v = *(stbsp__uint32 *)sn; + // bit hack to find if there's a 0 byte in there + if ((v - 0x01010101) & (~v) & 0x80808080UL) + break; + + sn += 4; + limit -= 4; + } + + // handle the last few characters to find actual size + while (limit && *sn) { + ++sn; + --limit; + } + + return (stbsp__uint32)(sn - s); +} + +STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback, void *user, char *buf, char const *fmt, va_list va) +{ + static char hex[] = "0123456789abcdefxp"; + static char hexu[] = "0123456789ABCDEFXP"; + char *bf; + char const *f; + int tlen = 0; + + bf = buf; + f = fmt; + for (;;) { + stbsp__int32 fw, pr, tz; + stbsp__uint32 fl; + + // macros for the callback buffer stuff + #define stbsp__chk_cb_bufL(bytes) \ + { \ + int len = (int)(bf - buf); \ + if ((len + (bytes)) >= STB_SPRINTF_MIN) { \ + tlen += len; \ + if (0 == (bf = buf = callback(buf, user, len))) \ + goto done; \ + } \ + } + #define stbsp__chk_cb_buf(bytes) \ + { \ + if (callback) { \ + stbsp__chk_cb_bufL(bytes); \ + } \ + } + #define stbsp__flush_cb() \ + { \ + stbsp__chk_cb_bufL(STB_SPRINTF_MIN - 1); \ + } // flush if there is even one byte in the buffer + #define stbsp__cb_buf_clamp(cl, v) \ + cl = v; \ + if (callback) { \ + int lg = STB_SPRINTF_MIN - (int)(bf - buf); \ + if (cl > lg) \ + cl = lg; \ + } + + // fast copy everything up to the next % (or end of string) + for (;;) { + while (((stbsp__uintptr)f) & 3) { + schk1: + if (f[0] == '%') + goto scandd; + schk2: + if (f[0] == 0) + goto endfmt; + stbsp__chk_cb_buf(1); + *bf++ = f[0]; + ++f; + } + for (;;) { + // Check if the next 4 bytes contain %(0x25) or end of string. + // Using the 'hasless' trick: + // https://graphics.stanford.edu/~seander/bithacks.html#HasLessInWord + stbsp__uint32 v, c; + v = *(stbsp__uint32 *)f; + c = (~v) & 0x80808080; + if (((v ^ 0x25252525) - 0x01010101) & c) + goto schk1; + if ((v - 0x01010101) & c) + goto schk2; + if (callback) + if ((STB_SPRINTF_MIN - (int)(bf - buf)) < 4) + goto schk1; + #ifdef STB_SPRINTF_NOUNALIGNED + if(((stbsp__uintptr)bf) & 3) { + bf[0] = f[0]; + bf[1] = f[1]; + bf[2] = f[2]; + bf[3] = f[3]; + } else + #endif + { + *(stbsp__uint32 *)bf = v; + } + bf += 4; + f += 4; + } + } + scandd: + + ++f; + + // ok, we have a percent, read the modifiers first + fw = 0; + pr = -1; + fl = 0; + tz = 0; + + // flags + for (;;) { + switch (f[0]) { + // if we have left justify + case '-': + fl |= STBSP__LEFTJUST; + ++f; + continue; + // if we have leading plus + case '+': + fl |= STBSP__LEADINGPLUS; + ++f; + continue; + // if we have leading space + case ' ': + fl |= STBSP__LEADINGSPACE; + ++f; + continue; + // if we have leading 0x + case '#': + fl |= STBSP__LEADING_0X; + ++f; + continue; + // if we have thousand commas + case '\'': + fl |= STBSP__TRIPLET_COMMA; + ++f; + continue; + // if we have kilo marker (none->kilo->kibi->jedec) + case '$': + if (fl & STBSP__METRIC_SUFFIX) { + if (fl & STBSP__METRIC_1024) { + fl |= STBSP__METRIC_JEDEC; + } else { + fl |= STBSP__METRIC_1024; + } + } else { + fl |= STBSP__METRIC_SUFFIX; + } + ++f; + continue; + // if we don't want space between metric suffix and number + case '_': + fl |= STBSP__METRIC_NOSPACE; + ++f; + continue; + // if we have leading zero + case '0': + fl |= STBSP__LEADINGZERO; + ++f; + goto flags_done; + default: goto flags_done; + } + } + flags_done: + + // get the field width + if (f[0] == '*') { + fw = va_arg(va, stbsp__uint32); + ++f; + } else { + while ((f[0] >= '0') && (f[0] <= '9')) { + fw = fw * 10 + f[0] - '0'; + f++; + } + } + // get the precision + if (f[0] == '.') { + ++f; + if (f[0] == '*') { + pr = va_arg(va, stbsp__uint32); + ++f; + } else { + pr = 0; + while ((f[0] >= '0') && (f[0] <= '9')) { + pr = pr * 10 + f[0] - '0'; + f++; + } + } + } + + // handle integer size overrides + switch (f[0]) { + // are we halfwidth? + case 'h': + fl |= STBSP__HALFWIDTH; + ++f; + if (f[0] == 'h') + ++f; // QUARTERWIDTH + break; + // are we 64-bit (unix style) + case 'l': + fl |= ((sizeof(long) == 8) ? STBSP__INTMAX : 0); + ++f; + if (f[0] == 'l') { + fl |= STBSP__INTMAX; + ++f; + } + break; + // are we 64-bit on intmax? (c99) + case 'j': + fl |= (sizeof(size_t) == 8) ? STBSP__INTMAX : 0; + ++f; + break; + // are we 64-bit on size_t or ptrdiff_t? (c99) + case 'z': + fl |= (sizeof(ptrdiff_t) == 8) ? STBSP__INTMAX : 0; + ++f; + break; + case 't': + fl |= (sizeof(ptrdiff_t) == 8) ? STBSP__INTMAX : 0; + ++f; + break; + // are we 64-bit (msft style) + case 'I': + if ((f[1] == '6') && (f[2] == '4')) { + fl |= STBSP__INTMAX; + f += 3; + } else if ((f[1] == '3') && (f[2] == '2')) { + f += 3; + } else { + fl |= ((sizeof(void *) == 8) ? STBSP__INTMAX : 0); + ++f; + } + break; + default: break; + } + + // handle each replacement + switch (f[0]) { + #define STBSP__NUMSZ 512 // big enough for e308 (with commas) or e-307 + char num[STBSP__NUMSZ]; + char lead[8]; + char tail[8]; + char *s; + char const *h; + stbsp__uint32 l, n, cs; + stbsp__uint64 n64; +#ifndef STB_SPRINTF_NOFLOAT + double fv; +#endif + stbsp__int32 dp; + char const *sn; + + case 's': + // get the string + s = va_arg(va, char *); + if (s == 0) + s = (char *)"null"; + // get the length, limited to desired precision + // always limit to ~0u chars since our counts are 32b + l = stbsp__strlen_limited(s, (pr >= 0) ? pr : ~0u); + lead[0] = 0; + tail[0] = 0; + pr = 0; + dp = 0; + cs = 0; + // copy the string in + goto scopy; + + case 'c': // char + // get the character + s = num + STBSP__NUMSZ - 1; + *s = (char)va_arg(va, int); + l = 1; + lead[0] = 0; + tail[0] = 0; + pr = 0; + dp = 0; + cs = 0; + goto scopy; + + case 'n': // weird write-bytes specifier + { + int *d = va_arg(va, int *); + *d = tlen + (int)(bf - buf); + } break; + +#ifdef STB_SPRINTF_NOFLOAT + case 'A': // float + case 'a': // hex float + case 'G': // float + case 'g': // float + case 'E': // float + case 'e': // float + case 'f': // float + va_arg(va, /*double*/uint64_t); // eat it + s = (char *)"No float"; + l = 8; + lead[0] = 0; + tail[0] = 0; + pr = 0; + cs = 0; + STBSP__NOTUSED(dp); + goto scopy; +#else + case 'A': // hex float + case 'a': // hex float + h = (f[0] == 'A') ? hexu : hex; + fv = va_arg(va, double); + if (pr == -1) + pr = 6; // default is 6 + // read the double into a string + if (stbsp__real_to_parts((stbsp__int64 *)&n64, &dp, fv)) + fl |= STBSP__NEGATIVE; + + s = num + 64; + + stbsp__lead_sign(fl, lead); + + if (dp == -1023) + dp = (n64) ? -1022 : 0; + else + n64 |= (((stbsp__uint64)1) << 52); + n64 <<= (64 - 56); + if (pr < 15) + n64 += ((((stbsp__uint64)8) << 56) >> (pr * 4)); +// add leading chars + +#ifdef STB_SPRINTF_MSVC_MODE + *s++ = '0'; + *s++ = 'x'; +#else + lead[1 + lead[0]] = '0'; + lead[2 + lead[0]] = 'x'; + lead[0] += 2; +#endif + *s++ = h[(n64 >> 60) & 15]; + n64 <<= 4; + if (pr) + *s++ = stbsp__period; + sn = s; + + // print the bits + n = pr; + if (n > 13) + n = 13; + if (pr > (stbsp__int32)n) + tz = pr - n; + pr = 0; + while (n--) { + *s++ = h[(n64 >> 60) & 15]; + n64 <<= 4; + } + + // print the expo + tail[1] = h[17]; + if (dp < 0) { + tail[2] = '-'; + dp = -dp; + } else + tail[2] = '+'; + n = (dp >= 1000) ? 6 : ((dp >= 100) ? 5 : ((dp >= 10) ? 4 : 3)); + tail[0] = (char)n; + for (;;) { + tail[n] = '0' + dp % 10; + if (n <= 3) + break; + --n; + dp /= 10; + } + + dp = (int)(s - sn); + l = (int)(s - (num + 64)); + s = num + 64; + cs = 1 + (3 << 24); + goto scopy; + + case 'G': // float + case 'g': // float + h = (f[0] == 'G') ? hexu : hex; + fv = va_arg(va, double); + if (pr == -1) + pr = 6; + else if (pr == 0) + pr = 1; // default is 6 + // read the double into a string + if (stbsp__real_to_str(&sn, &l, num, &dp, fv, (pr - 1) | 0x80000000)) + fl |= STBSP__NEGATIVE; + + // clamp the precision and delete extra zeros after clamp + n = pr; + if (l > (stbsp__uint32)pr) + l = pr; + while ((l > 1) && (pr) && (sn[l - 1] == '0')) { + --pr; + --l; + } + + // should we use %e + if ((dp <= -4) || (dp > (stbsp__int32)n)) { + if (pr > (stbsp__int32)l) + pr = l - 1; + else if (pr) + --pr; // when using %e, there is one digit before the decimal + goto doexpfromg; + } + // this is the insane action to get the pr to match %g semantics for %f + if (dp > 0) { + pr = (dp < (stbsp__int32)l) ? l - dp : 0; + } else { + pr = -dp + ((pr > (stbsp__int32)l) ? (stbsp__int32) l : pr); + } + goto dofloatfromg; + + case 'E': // float + case 'e': // float + h = (f[0] == 'E') ? hexu : hex; + fv = va_arg(va, double); + if (pr == -1) + pr = 6; // default is 6 + // read the double into a string + if (stbsp__real_to_str(&sn, &l, num, &dp, fv, pr | 0x80000000)) + fl |= STBSP__NEGATIVE; + doexpfromg: + tail[0] = 0; + stbsp__lead_sign(fl, lead); + if (dp == STBSP__SPECIAL) { + s = (char *)sn; + cs = 0; + pr = 0; + goto scopy; + } + s = num + 64; + // handle leading chars + *s++ = sn[0]; + + if (pr) + *s++ = stbsp__period; + + // handle after decimal + if ((l - 1) > (stbsp__uint32)pr) + l = pr + 1; + for (n = 1; n < l; n++) + *s++ = sn[n]; + // trailing zeros + tz = pr - (l - 1); + pr = 0; + // dump expo + tail[1] = h[0xe]; + dp -= 1; + if (dp < 0) { + tail[2] = '-'; + dp = -dp; + } else + tail[2] = '+'; +#ifdef STB_SPRINTF_MSVC_MODE + n = 5; +#else + n = (dp >= 100) ? 5 : 4; +#endif + tail[0] = (char)n; + for (;;) { + tail[n] = '0' + dp % 10; + if (n <= 3) + break; + --n; + dp /= 10; + } + cs = 1 + (3 << 24); // how many tens + goto flt_lead; + + case 'f': // float + fv = va_arg(va, double); + doafloat: + // do kilos + if (fl & STBSP__METRIC_SUFFIX) { + double divisor; + divisor = 1000.0f; + if (fl & STBSP__METRIC_1024) + divisor = 1024.0; + while (fl < 0x4000000) { + if ((fv < divisor) && (fv > -divisor)) + break; + fv /= divisor; + fl += 0x1000000; + } + } + if (pr == -1) + pr = 6; // default is 6 + // read the double into a string + if (stbsp__real_to_str(&sn, &l, num, &dp, fv, pr)) + fl |= STBSP__NEGATIVE; + dofloatfromg: + tail[0] = 0; + stbsp__lead_sign(fl, lead); + if (dp == STBSP__SPECIAL) { + s = (char *)sn; + cs = 0; + pr = 0; + goto scopy; + } + s = num + 64; + + // handle the three decimal varieties + if (dp <= 0) { + stbsp__int32 i; + // handle 0.000*000xxxx + *s++ = '0'; + if (pr) + *s++ = stbsp__period; + n = -dp; + if ((stbsp__int32)n > pr) + n = pr; + i = n; + while (i) { + if ((((stbsp__uintptr)s) & 3) == 0) + break; + *s++ = '0'; + --i; + } + while (i >= 4) { + *(stbsp__uint32 *)s = 0x30303030; + s += 4; + i -= 4; + } + while (i) { + *s++ = '0'; + --i; + } + if ((stbsp__int32)(l + n) > pr) + l = pr - n; + i = l; + while (i) { + *s++ = *sn++; + --i; + } + tz = pr - (n + l); + cs = 1 + (3 << 24); // how many tens did we write (for commas below) + } else { + cs = (fl & STBSP__TRIPLET_COMMA) ? ((600 - (stbsp__uint32)dp) % 3) : 0; + if ((stbsp__uint32)dp >= l) { + // handle xxxx000*000.0 + n = 0; + for (;;) { + if ((fl & STBSP__TRIPLET_COMMA) && (++cs == 4)) { + cs = 0; + *s++ = stbsp__comma; + } else { + *s++ = sn[n]; + ++n; + if (n >= l) + break; + } + } + if (n < (stbsp__uint32)dp) { + n = dp - n; + if ((fl & STBSP__TRIPLET_COMMA) == 0) { + while (n) { + if ((((stbsp__uintptr)s) & 3) == 0) + break; + *s++ = '0'; + --n; + } + while (n >= 4) { + *(stbsp__uint32 *)s = 0x30303030; + s += 4; + n -= 4; + } + } + while (n) { + if ((fl & STBSP__TRIPLET_COMMA) && (++cs == 4)) { + cs = 0; + *s++ = stbsp__comma; + } else { + *s++ = '0'; + --n; + } + } + } + cs = (int)(s - (num + 64)) + (3 << 24); // cs is how many tens + if (pr) { + *s++ = stbsp__period; + tz = pr; + } + } else { + // handle xxxxx.xxxx000*000 + n = 0; + for (;;) { + if ((fl & STBSP__TRIPLET_COMMA) && (++cs == 4)) { + cs = 0; + *s++ = stbsp__comma; + } else { + *s++ = sn[n]; + ++n; + if (n >= (stbsp__uint32)dp) + break; + } + } + cs = (int)(s - (num + 64)) + (3 << 24); // cs is how many tens + if (pr) + *s++ = stbsp__period; + if ((l - dp) > (stbsp__uint32)pr) + l = pr + dp; + while (n < l) { + *s++ = sn[n]; + ++n; + } + tz = pr - (l - dp); + } + } + pr = 0; + + // handle k,m,g,t + if (fl & STBSP__METRIC_SUFFIX) { + char idx; + idx = 1; + if (fl & STBSP__METRIC_NOSPACE) + idx = 0; + tail[0] = idx; + tail[1] = ' '; + { + if (fl >> 24) { // SI kilo is 'k', JEDEC and SI kibits are 'K'. + if (fl & STBSP__METRIC_1024) + tail[idx + 1] = "_KMGT"[fl >> 24]; + else + tail[idx + 1] = "_kMGT"[fl >> 24]; + idx++; + // If printing kibits and not in jedec, add the 'i'. + if (fl & STBSP__METRIC_1024 && !(fl & STBSP__METRIC_JEDEC)) { + tail[idx + 1] = 'i'; + idx++; + } + tail[0] = idx; + } + } + }; + + flt_lead: + // get the length that we copied + l = (stbsp__uint32)(s - (num + 64)); + s = num + 64; + goto scopy; +#endif + + case 'B': // upper binary + case 'b': // lower binary + h = (f[0] == 'B') ? hexu : hex; + lead[0] = 0; + if (fl & STBSP__LEADING_0X) { + lead[0] = 2; + lead[1] = '0'; + lead[2] = h[0xb]; + } + l = (8 << 4) | (1 << 8); + goto radixnum; + + case 'o': // octal + h = hexu; + lead[0] = 0; + if (fl & STBSP__LEADING_0X) { + lead[0] = 1; + lead[1] = '0'; + } + l = (3 << 4) | (3 << 8); + goto radixnum; + + case 'p': // pointer + fl |= (sizeof(void *) == 8) ? STBSP__INTMAX : 0; + pr = sizeof(void *) * 2; + fl &= ~STBSP__LEADINGZERO; // 'p' only prints the pointer with zeros + // fall through - to X + + case 'X': // upper hex + case 'x': // lower hex + h = (f[0] == 'X') ? hexu : hex; + l = (4 << 4) | (4 << 8); + lead[0] = 0; + if (fl & STBSP__LEADING_0X) { + lead[0] = 2; + lead[1] = '0'; + lead[2] = h[16]; + } + radixnum: + // get the number + if (fl & STBSP__INTMAX) + n64 = va_arg(va, stbsp__uint64); + else + n64 = va_arg(va, stbsp__uint32); + + s = num + STBSP__NUMSZ; + dp = 0; + // clear tail, and clear leading if value is zero + tail[0] = 0; + if (n64 == 0) { + lead[0] = 0; + if (pr == 0) { + l = 0; + cs = 0; + goto scopy; + } + } + // convert to string + for (;;) { + *--s = h[n64 & ((1 << (l >> 8)) - 1)]; + n64 >>= (l >> 8); + if (!((n64) || ((stbsp__int32)((num + STBSP__NUMSZ) - s) < pr))) + break; + if (fl & STBSP__TRIPLET_COMMA) { + ++l; + if ((l & 15) == ((l >> 4) & 15)) { + l &= ~15; + *--s = stbsp__comma; + } + } + }; + // get the tens and the comma pos + cs = (stbsp__uint32)((num + STBSP__NUMSZ) - s) + ((((l >> 4) & 15)) << 24); + // get the length that we copied + l = (stbsp__uint32)((num + STBSP__NUMSZ) - s); + // copy it + goto scopy; + + case 'u': // unsigned + case 'i': + case 'd': // integer + // get the integer and abs it + if (fl & STBSP__INTMAX) { + stbsp__int64 i64 = va_arg(va, stbsp__int64); + n64 = (stbsp__uint64)i64; + if ((f[0] != 'u') && (i64 < 0)) { + n64 = (stbsp__uint64)-i64; + fl |= STBSP__NEGATIVE; + } + } else { + stbsp__int32 i = va_arg(va, stbsp__int32); + n64 = (stbsp__uint32)i; + if ((f[0] != 'u') && (i < 0)) { + n64 = (stbsp__uint32)-i; + fl |= STBSP__NEGATIVE; + } + } + +#ifndef STB_SPRINTF_NOFLOAT + if (fl & STBSP__METRIC_SUFFIX) { + if (n64 < 1024) + pr = 0; + else if (pr == -1) + pr = 1; + fv = (double)(stbsp__int64)n64; + goto doafloat; + } +#endif + + // convert to string + s = num + STBSP__NUMSZ; + l = 0; + + for (;;) { + // do in 32-bit chunks (avoid lots of 64-bit divides even with constant denominators) + char *o = s - 8; + if (n64 >= 100000000) { + n = (stbsp__uint32)(n64 % 100000000); + n64 /= 100000000; + } else { + n = (stbsp__uint32)n64; + n64 = 0; + } + if ((fl & STBSP__TRIPLET_COMMA) == 0) { + do { + s -= 2; + *(stbsp__uint16 *)s = *(stbsp__uint16 *)&stbsp__digitpair.pair[(n % 100) * 2]; + n /= 100; + } while (n); + } + while (n) { + if ((fl & STBSP__TRIPLET_COMMA) && (l++ == 3)) { + l = 0; + *--s = stbsp__comma; + --o; + } else { + *--s = (char)(n % 10) + '0'; + n /= 10; + } + } + if (n64 == 0) { + if ((s[0] == '0') && (s != (num + STBSP__NUMSZ))) + ++s; + break; + } + while (s != o) + if ((fl & STBSP__TRIPLET_COMMA) && (l++ == 3)) { + l = 0; + *--s = stbsp__comma; + --o; + } else { + *--s = '0'; + } + } + + tail[0] = 0; + stbsp__lead_sign(fl, lead); + + // get the length that we copied + l = (stbsp__uint32)((num + STBSP__NUMSZ) - s); + if (l == 0) { + *--s = '0'; + l = 1; + } + cs = l + (3 << 24); + if (pr < 0) + pr = 0; + + scopy: + // get fw=leading/trailing space, pr=leading zeros + if (pr < (stbsp__int32)l) + pr = l; + n = pr + lead[0] + tail[0] + tz; + if (fw < (stbsp__int32)n) + fw = n; + fw -= n; + pr -= l; + + // handle right justify and leading zeros + if ((fl & STBSP__LEFTJUST) == 0) { + if (fl & STBSP__LEADINGZERO) // if leading zeros, everything is in pr + { + pr = (fw > pr) ? fw : pr; + fw = 0; + } else { + fl &= ~STBSP__TRIPLET_COMMA; // if no leading zeros, then no commas + } + } + + // copy the spaces and/or zeros + if (fw + pr) { + stbsp__int32 i; + stbsp__uint32 c; + + // copy leading spaces (or when doing %8.4d stuff) + if ((fl & STBSP__LEFTJUST) == 0) + while (fw > 0) { + stbsp__cb_buf_clamp(i, fw); + fw -= i; + while (i) { + if ((((stbsp__uintptr)bf) & 3) == 0) + break; + *bf++ = ' '; + --i; + } + while (i >= 4) { + *(stbsp__uint32 *)bf = 0x20202020; + bf += 4; + i -= 4; + } + while (i) { + *bf++ = ' '; + --i; + } + stbsp__chk_cb_buf(1); + } + + // copy leader + sn = lead + 1; + while (lead[0]) { + stbsp__cb_buf_clamp(i, lead[0]); + lead[0] -= (char)i; + while (i) { + *bf++ = *sn++; + --i; + } + stbsp__chk_cb_buf(1); + } + + // copy leading zeros + c = cs >> 24; + cs &= 0xffffff; + cs = (fl & STBSP__TRIPLET_COMMA) ? ((stbsp__uint32)(c - ((pr + cs) % (c + 1)))) : 0; + while (pr > 0) { + stbsp__cb_buf_clamp(i, pr); + pr -= i; + if ((fl & STBSP__TRIPLET_COMMA) == 0) { + while (i) { + if ((((stbsp__uintptr)bf) & 3) == 0) + break; + *bf++ = '0'; + --i; + } + while (i >= 4) { + *(stbsp__uint32 *)bf = 0x30303030; + bf += 4; + i -= 4; + } + } + while (i) { + if ((fl & STBSP__TRIPLET_COMMA) && (cs++ == c)) { + cs = 0; + *bf++ = stbsp__comma; + } else + *bf++ = '0'; + --i; + } + stbsp__chk_cb_buf(1); + } + } + + // copy leader if there is still one + sn = lead + 1; + while (lead[0]) { + stbsp__int32 i; + stbsp__cb_buf_clamp(i, lead[0]); + lead[0] -= (char)i; + while (i) { + *bf++ = *sn++; + --i; + } + stbsp__chk_cb_buf(1); + } + + // copy the string + n = l; + while (n) { + stbsp__int32 i; + stbsp__cb_buf_clamp(i, n); + n -= i; + STBSP__UNALIGNED(while (i >= 4) { + *(stbsp__uint32 volatile *)bf = *(stbsp__uint32 volatile *)s; + bf += 4; + s += 4; + i -= 4; + }) + while (i) { + *bf++ = *s++; + --i; + } + stbsp__chk_cb_buf(1); + } + + // copy trailing zeros + while (tz) { + stbsp__int32 i; + stbsp__cb_buf_clamp(i, tz); + tz -= i; + while (i) { + if ((((stbsp__uintptr)bf) & 3) == 0) + break; + *bf++ = '0'; + --i; + } + while (i >= 4) { + *(stbsp__uint32 *)bf = 0x30303030; + bf += 4; + i -= 4; + } + while (i) { + *bf++ = '0'; + --i; + } + stbsp__chk_cb_buf(1); + } + + // copy tail if there is one + sn = tail + 1; + while (tail[0]) { + stbsp__int32 i; + stbsp__cb_buf_clamp(i, tail[0]); + tail[0] -= (char)i; + while (i) { + *bf++ = *sn++; + --i; + } + stbsp__chk_cb_buf(1); + } + + // handle the left justify + if (fl & STBSP__LEFTJUST) + if (fw > 0) { + while (fw) { + stbsp__int32 i; + stbsp__cb_buf_clamp(i, fw); + fw -= i; + while (i) { + if ((((stbsp__uintptr)bf) & 3) == 0) + break; + *bf++ = ' '; + --i; + } + while (i >= 4) { + *(stbsp__uint32 *)bf = 0x20202020; + bf += 4; + i -= 4; + } + while (i--) + *bf++ = ' '; + stbsp__chk_cb_buf(1); + } + } + break; + + default: // unknown, just copy code + s = num + STBSP__NUMSZ - 1; + *s = f[0]; + l = 1; + fw = fl = 0; + lead[0] = 0; + tail[0] = 0; + pr = 0; + dp = 0; + cs = 0; + goto scopy; + } + ++f; + } +endfmt: + + if (!callback) + *bf = 0; + else + stbsp__flush_cb(); + +done: + return tlen + (int)(bf - buf); +} + +// cleanup +#undef STBSP__LEFTJUST +#undef STBSP__LEADINGPLUS +#undef STBSP__LEADINGSPACE +#undef STBSP__LEADING_0X +#undef STBSP__LEADINGZERO +#undef STBSP__INTMAX +#undef STBSP__TRIPLET_COMMA +#undef STBSP__NEGATIVE +#undef STBSP__METRIC_SUFFIX +#undef STBSP__NUMSZ +#undef stbsp__chk_cb_bufL +#undef stbsp__chk_cb_buf +#undef stbsp__flush_cb +#undef stbsp__cb_buf_clamp + +// ============================================================================ +// wrapper functions + +STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(sprintf)(char *buf, char const *fmt, ...) +{ + int result; + va_list va; + va_start(va, fmt); + result = STB_SPRINTF_DECORATE(vsprintfcb)(0, 0, buf, fmt, va); + va_end(va); + return result; +} + +typedef struct stbsp__context { + char *buf; + int count; + int length; + char tmp[STB_SPRINTF_MIN]; +} stbsp__context; + +static char *stbsp__clamp_callback(const char *buf, void *user, int len) +{ + stbsp__context *c = (stbsp__context *)user; + c->length += len; + + if (len > c->count) + len = c->count; + + if (len) { + if (buf != c->buf) { + const char *s, *se; + char *d; + d = c->buf; + s = buf; + se = buf + len; + do { + *d++ = *s++; + } while (s < se); + } + c->buf += len; + c->count -= len; + } + + if (c->count <= 0) + return c->tmp; + return (c->count >= STB_SPRINTF_MIN) ? c->buf : c->tmp; // go direct into buffer if you can +} + +static char * stbsp__count_clamp_callback( const char * buf, void * user, int len ) +{ + stbsp__context * c = (stbsp__context*)user; + (void) sizeof(buf); + + c->length += len; + return c->tmp; // go direct into buffer if you can +} + +STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, char const * fmt, va_list va ) +{ + stbsp__context c; + + if ( (count == 0) && !buf ) + { + c.length = 0; + + STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__count_clamp_callback, &c, c.tmp, fmt, va ); + } + else + { + int l; + + c.buf = buf; + c.count = count; + c.length = 0; + + STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__clamp_callback, &c, stbsp__clamp_callback(0,&c,0), fmt, va ); + + // zero-terminate + l = (int)( c.buf - buf ); + if ( l >= count ) // should never be greater, only equal (or less) than count + l = count - 1; + buf[l] = 0; + } + + return c.length; +} + +STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(snprintf)(char *buf, int count, char const *fmt, ...) +{ + int result; + va_list va; + va_start(va, fmt); + + result = STB_SPRINTF_DECORATE(vsnprintf)(buf, count, fmt, va); + va_end(va); + + return result; +} + +STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintf)(char *buf, char const *fmt, va_list va) +{ + return STB_SPRINTF_DECORATE(vsprintfcb)(0, 0, buf, fmt, va); +} + +// ======================================================================= +// low level float utility functions + +#ifndef STB_SPRINTF_NOFLOAT + +// copies d to bits w/ strict aliasing (this compiles to nothing on /Ox) +#define STBSP__COPYFP(dest, src) \ + { \ + int cn; \ + for (cn = 0; cn < 8; cn++) \ + ((char *)&dest)[cn] = ((char *)&src)[cn]; \ + } + +// get float info +static stbsp__int32 stbsp__real_to_parts(stbsp__int64 *bits, stbsp__int32 *expo, double value) +{ + double d; + stbsp__int64 b = 0; + + // load value and round at the frac_digits + d = value; + + STBSP__COPYFP(b, d); + + *bits = b & ((((stbsp__uint64)1) << 52) - 1); + *expo = (stbsp__int32)(((b >> 52) & 2047) - 1023); + + return (stbsp__int32)((stbsp__uint64) b >> 63); +} + +static double const stbsp__bot[23] = { + 1e+000, 1e+001, 1e+002, 1e+003, 1e+004, 1e+005, 1e+006, 1e+007, 1e+008, 1e+009, 1e+010, 1e+011, + 1e+012, 1e+013, 1e+014, 1e+015, 1e+016, 1e+017, 1e+018, 1e+019, 1e+020, 1e+021, 1e+022 +}; +static double const stbsp__negbot[22] = { + 1e-001, 1e-002, 1e-003, 1e-004, 1e-005, 1e-006, 1e-007, 1e-008, 1e-009, 1e-010, 1e-011, + 1e-012, 1e-013, 1e-014, 1e-015, 1e-016, 1e-017, 1e-018, 1e-019, 1e-020, 1e-021, 1e-022 +}; +static double const stbsp__negboterr[22] = { + -5.551115123125783e-018, -2.0816681711721684e-019, -2.0816681711721686e-020, -4.7921736023859299e-021, -8.1803053914031305e-022, 4.5251888174113741e-023, + 4.5251888174113739e-024, -2.0922560830128471e-025, -6.2281591457779853e-026, -3.6432197315497743e-027, 6.0503030718060191e-028, 2.0113352370744385e-029, + -3.0373745563400371e-030, 1.1806906454401013e-032, -7.7705399876661076e-032, 2.0902213275965398e-033, -7.1542424054621921e-034, -7.1542424054621926e-035, + 2.4754073164739869e-036, 5.4846728545790429e-037, 9.2462547772103625e-038, -4.8596774326570872e-039 +}; +static double const stbsp__top[13] = { + 1e+023, 1e+046, 1e+069, 1e+092, 1e+115, 1e+138, 1e+161, 1e+184, 1e+207, 1e+230, 1e+253, 1e+276, 1e+299 +}; +static double const stbsp__negtop[13] = { + 1e-023, 1e-046, 1e-069, 1e-092, 1e-115, 1e-138, 1e-161, 1e-184, 1e-207, 1e-230, 1e-253, 1e-276, 1e-299 +}; +static double const stbsp__toperr[13] = { + 8388608, + 6.8601809640529717e+028, + -7.253143638152921e+052, + -4.3377296974619174e+075, + -1.5559416129466825e+098, + -3.2841562489204913e+121, + -3.7745893248228135e+144, + -1.7356668416969134e+167, + -3.8893577551088374e+190, + -9.9566444326005119e+213, + 6.3641293062232429e+236, + -5.2069140800249813e+259, + -5.2504760255204387e+282 +}; +static double const stbsp__negtoperr[13] = { + 3.9565301985100693e-040, -2.299904345391321e-063, 3.6506201437945798e-086, 1.1875228833981544e-109, + -5.0644902316928607e-132, -6.7156837247865426e-155, -2.812077463003139e-178, -5.7778912386589953e-201, + 7.4997100559334532e-224, -4.6439668915134491e-247, -6.3691100762962136e-270, -9.436808465446358e-293, + 8.0970921678014997e-317 +}; + +#if defined(_MSC_VER) && (_MSC_VER <= 1200) +static stbsp__uint64 const stbsp__powten[20] = { + 1, + 10, + 100, + 1000, + 10000, + 100000, + 1000000, + 10000000, + 100000000, + 1000000000, + 10000000000, + 100000000000, + 1000000000000, + 10000000000000, + 100000000000000, + 1000000000000000, + 10000000000000000, + 100000000000000000, + 1000000000000000000, + 10000000000000000000U +}; +#define stbsp__tento19th ((stbsp__uint64)1000000000000000000) +#else +static stbsp__uint64 const stbsp__powten[20] = { + 1, + 10, + 100, + 1000, + 10000, + 100000, + 1000000, + 10000000, + 100000000, + 1000000000, + 10000000000ULL, + 100000000000ULL, + 1000000000000ULL, + 10000000000000ULL, + 100000000000000ULL, + 1000000000000000ULL, + 10000000000000000ULL, + 100000000000000000ULL, + 1000000000000000000ULL, + 10000000000000000000ULL +}; +#define stbsp__tento19th (1000000000000000000ULL) +#endif + +#define stbsp__ddmulthi(oh, ol, xh, yh) \ + { \ + double ahi = 0, alo, bhi = 0, blo; \ + stbsp__int64 bt; \ + oh = xh * yh; \ + STBSP__COPYFP(bt, xh); \ + bt &= ((~(stbsp__uint64)0) << 27); \ + STBSP__COPYFP(ahi, bt); \ + alo = xh - ahi; \ + STBSP__COPYFP(bt, yh); \ + bt &= ((~(stbsp__uint64)0) << 27); \ + STBSP__COPYFP(bhi, bt); \ + blo = yh - bhi; \ + ol = ((ahi * bhi - oh) + ahi * blo + alo * bhi) + alo * blo; \ + } + +#define stbsp__ddtoS64(ob, xh, xl) \ + { \ + double ahi = 0, alo, vh, t; \ + ob = (stbsp__int64)xh; \ + vh = (double)ob; \ + ahi = (xh - vh); \ + t = (ahi - xh); \ + alo = (xh - (ahi - t)) - (vh + t); \ + ob += (stbsp__int64)(ahi + alo + xl); \ + } + +#define stbsp__ddrenorm(oh, ol) \ + { \ + double s; \ + s = oh + ol; \ + ol = ol - (s - oh); \ + oh = s; \ + } + +#define stbsp__ddmultlo(oh, ol, xh, xl, yh, yl) ol = ol + (xh * yl + xl * yh); + +#define stbsp__ddmultlos(oh, ol, xh, yl) ol = ol + (xh * yl); + +static void stbsp__raise_to_power10(double *ohi, double *olo, double d, stbsp__int32 power) // power can be -323 to +350 +{ + double ph, pl; + if ((power >= 0) && (power <= 22)) { + stbsp__ddmulthi(ph, pl, d, stbsp__bot[power]); + } else { + stbsp__int32 e, et, eb; + double p2h, p2l; + + e = power; + if (power < 0) + e = -e; + et = (e * 0x2c9) >> 14; /* %23 */ + if (et > 13) + et = 13; + eb = e - (et * 23); + + ph = d; + pl = 0.0; + if (power < 0) { + if (eb) { + --eb; + stbsp__ddmulthi(ph, pl, d, stbsp__negbot[eb]); + stbsp__ddmultlos(ph, pl, d, stbsp__negboterr[eb]); + } + if (et) { + stbsp__ddrenorm(ph, pl); + --et; + stbsp__ddmulthi(p2h, p2l, ph, stbsp__negtop[et]); + stbsp__ddmultlo(p2h, p2l, ph, pl, stbsp__negtop[et], stbsp__negtoperr[et]); + ph = p2h; + pl = p2l; + } + } else { + if (eb) { + e = eb; + if (eb > 22) + eb = 22; + e -= eb; + stbsp__ddmulthi(ph, pl, d, stbsp__bot[eb]); + if (e) { + stbsp__ddrenorm(ph, pl); + stbsp__ddmulthi(p2h, p2l, ph, stbsp__bot[e]); + stbsp__ddmultlos(p2h, p2l, stbsp__bot[e], pl); + ph = p2h; + pl = p2l; + } + } + if (et) { + stbsp__ddrenorm(ph, pl); + --et; + stbsp__ddmulthi(p2h, p2l, ph, stbsp__top[et]); + stbsp__ddmultlo(p2h, p2l, ph, pl, stbsp__top[et], stbsp__toperr[et]); + ph = p2h; + pl = p2l; + } + } + } + stbsp__ddrenorm(ph, pl); + *ohi = ph; + *olo = pl; +} + +// given a float value, returns the significant bits in bits, and the position of the +// decimal point in decimal_pos. +/-INF and NAN are specified by special values +// returned in the decimal_pos parameter. +// frac_digits is absolute normally, but if you want from first significant digits (got %g and %e), or in 0x80000000 +static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, char *out, stbsp__int32 *decimal_pos, double value, stbsp__uint32 frac_digits) +{ + double d; + stbsp__int64 bits = 0; + stbsp__int32 expo, e, ng, tens; + + d = value; + STBSP__COPYFP(bits, d); + expo = (stbsp__int32)((bits >> 52) & 2047); + ng = (stbsp__int32)((stbsp__uint64) bits >> 63); + if (ng) + d = -d; + + if (expo == 2047) // is nan or inf? + { + *start = (bits & ((((stbsp__uint64)1) << 52) - 1)) ? "NaN" : "Inf"; + *decimal_pos = STBSP__SPECIAL; + *len = 3; + return ng; + } + + if (expo == 0) // is zero or denormal + { + if (((stbsp__uint64) bits << 1) == 0) // do zero + { + *decimal_pos = 1; + *start = out; + out[0] = '0'; + *len = 1; + return ng; + } + // find the right expo for denormals + { + stbsp__int64 v = ((stbsp__uint64)1) << 51; + while ((bits & v) == 0) { + --expo; + v >>= 1; + } + } + } + + // find the decimal exponent as well as the decimal bits of the value + { + double ph, pl; + + // log10 estimate - very specifically tweaked to hit or undershoot by no more than 1 of log10 of all expos 1..2046 + tens = expo - 1023; + tens = (tens < 0) ? ((tens * 617) / 2048) : (((tens * 1233) / 4096) + 1); + + // move the significant bits into position and stick them into an int + stbsp__raise_to_power10(&ph, &pl, d, 18 - tens); + + // get full as much precision from double-double as possible + stbsp__ddtoS64(bits, ph, pl); + + // check if we undershot + if (((stbsp__uint64)bits) >= stbsp__tento19th) + ++tens; + } + + // now do the rounding in integer land + frac_digits = (frac_digits & 0x80000000) ? ((frac_digits & 0x7ffffff) + 1) : (tens + frac_digits); + if ((frac_digits < 24)) { + stbsp__uint32 dg = 1; + if ((stbsp__uint64)bits >= stbsp__powten[9]) + dg = 10; + while ((stbsp__uint64)bits >= stbsp__powten[dg]) { + ++dg; + if (dg == 20) + goto noround; + } + if (frac_digits < dg) { + stbsp__uint64 r; + // add 0.5 at the right position and round + e = dg - frac_digits; + if ((stbsp__uint32)e >= 24) + goto noround; + r = stbsp__powten[e]; + bits = bits + (r / 2); + if ((stbsp__uint64)bits >= stbsp__powten[dg]) + ++tens; + bits /= r; + } + noround:; + } + + // kill long trailing runs of zeros + if (bits) { + stbsp__uint32 n; + for (;;) { + if (bits <= 0xffffffff) + break; + if (bits % 1000) + goto donez; + bits /= 1000; + } + n = (stbsp__uint32)bits; + while ((n % 1000) == 0) + n /= 1000; + bits = n; + donez:; + } + + // convert to string + out += 64; + e = 0; + for (;;) { + stbsp__uint32 n; + char *o = out - 8; + // do the conversion in chunks of U32s (avoid most 64-bit divides, worth it, constant denomiators be damned) + if (bits >= 100000000) { + n = (stbsp__uint32)(bits % 100000000); + bits /= 100000000; + } else { + n = (stbsp__uint32)bits; + bits = 0; + } + while (n) { + out -= 2; + *(stbsp__uint16 *)out = *(stbsp__uint16 *)&stbsp__digitpair.pair[(n % 100) * 2]; + n /= 100; + e += 2; + } + if (bits == 0) { + if ((e) && (out[0] == '0')) { + ++out; + --e; + } + break; + } + while (out != o) { + *--out = '0'; + ++e; + } + } + + *decimal_pos = tens; + *start = out; + *len = e; + return ng; +} + +#undef stbsp__ddmulthi +#undef stbsp__ddrenorm +#undef stbsp__ddmultlo +#undef stbsp__ddmultlos +#undef STBSP__SPECIAL +#undef STBSP__COPYFP + +#endif // STB_SPRINTF_NOFLOAT + +// clean up +#undef stbsp__uint16 +#undef stbsp__uint32 +#undef stbsp__int32 +#undef stbsp__uint64 +#undef stbsp__int64 +#undef STBSP__UNALIGNED + +#endif // STB_SPRINTF_IMPLEMENTATION + +/* +------------------------------------------------------------------------------ +This software is available under 2 licenses -- choose whichever you prefer. +------------------------------------------------------------------------------ +ALTERNATIVE A - MIT License +Copyright (c) 2017 Sean Barrett +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +------------------------------------------------------------------------------ +ALTERNATIVE B - Public Domain (www.unlicense.org) +This is free and unencumbered software released into the public domain. +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, +commercial or non-commercial, and by any means. +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to +this software under copyright law. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +------------------------------------------------------------------------------ +*/ diff --git a/src/oboskrnl/fb.h b/src/oboskrnl/fb.h new file mode 100644 index 00000000..8901c723 --- /dev/null +++ b/src/oboskrnl/fb.h @@ -0,0 +1,89 @@ +/* + oboskrnl/fb.h + + Copyright (c) 2024 Omar Berrow +*/ + +#pragma once + +#include + + +namespace obos +{ + enum class FramebufferFormat + { + FB_FORMAT_INVALID, + FB_FORMAT_RGB888, + FB_FORMAT_BGR888, + FB_FORMAT_RGBX8888, + FB_FORMAT_XRGB8888, + }; + struct Framebuffer + { + void* address; + // Width in bytes/bpp/8 + uint32_t width; + // Height in bytes/bpp/8 + uint32_t height; + // Pitch in bytes + uint32_t pitch; + // Bits Per Pixel. + int bpp; + // The format of the framebuffer. + FramebufferFormat format; + }; + union Pixel + { + Pixel() + { + raw.rgbx = 0; + ele.x = 0; + } + Pixel(uint32_t rgbx) + { + raw.rgbx = rgbx; + ele.x = 0; + } + Pixel(uint8_t r, + uint8_t g, + uint8_t b) + { + ele.r = r; + ele.g = g; + ele.b = b; + ele.x = 0x00; + } + struct + { + uint32_t rgbx; + } raw{ 0 }; + struct + { + uint8_t r; + uint8_t g; + uint8_t b; + uint8_t x; + } ele; + inline uint32_t ToFormat(FramebufferFormat format) + { + switch (format) + { + case FramebufferFormat::FB_FORMAT_RGB888: return raw.rgbx >> 8; + case FramebufferFormat::FB_FORMAT_BGR888: return ele.b | ((uint32_t)ele.g << 8) | ((uint32_t)ele.r << 16); + case FramebufferFormat::FB_FORMAT_RGBX8888: return raw.rgbx; + case FramebufferFormat::FB_FORMAT_XRGB8888: return ele.r | ((uint32_t)ele.g << 8) | ((uint32_t)ele.b << 16); + default: break; + } + return 0; + } + bool operator==(const Pixel& other) + { + return raw.rgbx == other.raw.rgbx; + } + bool operator!=(const Pixel& other) + { + return raw.rgbx != other.raw.rgbx; + } + }; +} \ No newline at end of file diff --git a/src/oboskrnl/gpt.c b/src/oboskrnl/gpt.c deleted file mode 100644 index 7b7d1ee8..00000000 --- a/src/oboskrnl/gpt.c +++ /dev/null @@ -1,199 +0,0 @@ -/* - * oboskrnl/gpt.c - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include - -#include -#include -#include - -#include - -static uint32_t crc32_bytes_from_previous(void *data, size_t sz, uint32_t previousChecksum); -static uint32_t crc32_bytes(void *data, size_t sz); -obos_status OBOS_IdentifyGPTPartitions(fd *desc, partition *partition_list, size_t *nPartitions, bool allow_checksum_fail) -{ - if (!desc || (!partition_list && !nPartitions)) - return OBOS_STATUS_INVALID_ARGUMENT; - size_t nRead = 0; - size_t filesize = desc->vn->filesize; - if (filesize < sizeof(mbr_t)) - return OBOS_STATUS_EOF; - OBOS_ASSERT(OBOS_PAGE_SIZE > sizeof(mbr_t)); - mbr_t *protective_mbr = Mm_QuickVMAllocate(OBOS_PAGE_SIZE, false); - obos_status status = - Vfs_FdRead(desc, protective_mbr, sizeof(*protective_mbr), &nRead); - if (obos_is_error(status)) - return status; - if (nRead != sizeof(mbr_t)) - return OBOS_STATUS_INTERNAL_ERROR; - if (protective_mbr->signature != MBR_BOOT_SIGNATURE) - return OBOS_STATUS_INVALID_FILE; - Mm_VirtualMemoryFree(&Mm_KernelContext, protective_mbr, OBOS_PAGE_SIZE); - - size_t blkSize = Vfs_FdGetBlkSz(desc); - OBOS_ASSERT(__builtin_popcount(blkSize) == 1); - uint8_t *buf = Mm_VirtualMemoryAlloc(&Mm_KernelContext, nullptr, blkSize, 0, 0, - nullptr, &status); - if (obos_is_error(status)) - return status; - gpt_header hdr = {}; - status = Vfs_FdRead(desc, buf, blkSize, &nRead); - if (obos_is_error(status)) - return status; - if (nRead != blkSize) - return OBOS_STATUS_INTERNAL_ERROR; - hdr = *(gpt_header *)buf; - if (hdr.signature != GPT_SIGNATURE) - { - Mm_VirtualMemoryFree(&Mm_KernelContext, buf, blkSize); - return OBOS_STATUS_INVALID_FILE; - } - if (!allow_checksum_fail) - { - bool retried = false; - retry: - (void)0; - uint32_t hdr_checksum = hdr.checksum; - hdr.checksum = 0; - uint32_t our_checksum = crc32_bytes(&hdr, sizeof(hdr)); - hdr.checksum = hdr_checksum; - if (retried && our_checksum != hdr_checksum) - { - Mm_VirtualMemoryFree(&Mm_KernelContext, buf, blkSize); - return OBOS_STATUS_INVALID_FILE; - } - if (our_checksum != hdr_checksum) - { - retried = true; - // bruh - Vfs_FdSeek(desc, hdr.alt_lba * blkSize, SEEK_SET); - status = Vfs_FdRead(desc, buf, blkSize, &nRead); - if (obos_is_error(status)) - { - Mm_VirtualMemoryFree(&Mm_KernelContext, buf, blkSize); - return status; - } - if (nRead != blkSize) - { - Mm_VirtualMemoryFree(&Mm_KernelContext, buf, blkSize); - return OBOS_STATUS_INTERNAL_ERROR; - } - hdr = *(gpt_header *)buf; - if (hdr.signature != GPT_SIGNATURE) - { - Mm_VirtualMemoryFree(&Mm_KernelContext, buf, blkSize); - return OBOS_STATUS_INVALID_FILE; - } - goto retry; - } - } - const size_t nPartitionEntriesPerSector = blkSize / hdr.sizeof_partiton_entry; - const size_t nSectorsForPartitionTable = hdr.part_entry_count / nPartitionEntriesPerSector + (hdr.part_entry_count % nPartitionEntriesPerSector != 0); - const uint64_t partitionTableLBA = hdr.part_table_lba; - Mm_VirtualMemoryFree(&Mm_KernelContext, buf, blkSize); - buf = Mm_VirtualMemoryAlloc(&Mm_KernelContext, nullptr, blkSize * nSectorsForPartitionTable, - 0, VMA_FLAGS_NON_PAGED, - nullptr, &status); - Vfs_FdSeek(desc, partitionTableLBA*blkSize, SEEK_SET); - Vfs_FdRead(desc, buf, blkSize*nSectorsForPartitionTable, nullptr); - if (!allow_checksum_fail) - { - uint32_t entriesCRC32 = crc32_bytes(buf, blkSize * nSectorsForPartitionTable); - if (entriesCRC32 != hdr.partition_entry_checksum) - { - Mm_VirtualMemoryFree(&Mm_KernelContext, buf, blkSize * nSectorsForPartitionTable); - return OBOS_STATUS_INVALID_FILE; - } - } - size_t nPartitionEntries = 0; - uint8_t* iter = buf; - for (size_t i = 0, id = 0; i < nSectorsForPartitionTable; i++) - { - - gpt_partition_entry* const volatile table = (gpt_partition_entry*)iter; - size_t nPartitionEntriesOnSector = 0; - for (; iter < (buf + blkSize*nSectorsForPartitionTable); iter += hdr.sizeof_partiton_entry, nPartitionEntriesOnSector++) - if (memcmp_b(iter, 0, sizeof(uuid))) - break; - nPartitionEntries += nPartitionEntriesOnSector; - if (!partition_list) - { - iter += (blkSize - (uintptr_t)iter % blkSize); - continue; - } - for (size_t partEntry = 0; partEntry < nPartitionEntriesOnSector; partEntry++, id++) - { - partition_list[id].vn = desc->vn; - partition_list[id].off = table[partEntry].begin_lba * blkSize; - partition_list[id].size = (table[partEntry].end_lba - table[partEntry].begin_lba) * blkSize; - memcpy(partition_list[id].part_uuid, &table[partEntry].part_uuid, sizeof(uuid)); - OBOS_InitString(&partition_list[id].part_name, ""); - for (size_t j = 0; table[partEntry].part_name[j] && j < 36; j++) - { - char ch[2] = { table[partEntry].part_name[j]&0xff, 0 }; - OBOS_AppendStringC(&partition_list[id].part_name, ch); - } - } - // iter = (uint8_t*)(((uintptr_t)iter + (blkSize - 1)) & ~(blkSize - 1)); - iter += (blkSize - (uintptr_t)iter % blkSize); - } - if (nPartitions) - *nPartitions = nPartitionEntries; - Mm_VirtualMemoryFree(&Mm_KernelContext, buf, blkSize * nSectorsForPartitionTable); - return OBOS_STATUS_SUCCESS; -} -static bool initialized_crc32 = false; -static uint32_t crctab[256]; - -// For future reference, we cannot hardware-accelerate the crc32 algorithm as -// x86-64's crc32 uses a different polynomial than that of GPT. - -static void crcInit() { - uint32_t crc = 0; - for (uint16_t i = 0; i < 256; ++i) { - crc = i; - for (uint8_t j = 0; j < 8; ++j) { - uint32_t mask = -(crc & 1); - crc = (crc >> 1) ^ (0xEDB88320 & mask); - } - crctab[i] = crc; - } -} -static uint32_t crc(const char *data, size_t len, uint32_t result) { - for (size_t i = 0; i < len; ++i) - result = (result >> 8) ^ crctab[(result ^ data[i]) & 0xFF]; - return ~result; -} -static uint32_t crc32_bytes_from_previous(void *data, size_t sz, - uint32_t previousChecksum) { - if (!initialized_crc32) { - crcInit(); - initialized_crc32 = true; - } - return crc((char *)data, sz, ~previousChecksum); -} -static uint32_t crc32_bytes(void *data, size_t sz) -{ - if (!initialized_crc32) { - crcInit(); - initialized_crc32 = true; - } - return crc((char *)data, sz, ~0U); -} diff --git a/src/oboskrnl/gpt.h b/src/oboskrnl/gpt.h deleted file mode 100644 index ef8f82e0..00000000 --- a/src/oboskrnl/gpt.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * oboskrnl/gpt.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include -#include -#include - -#include - -#define GPT_SIGNATURE 0x5452415020494645 -typedef struct gpt_header -{ - uint64_t signature; - uint32_t revision; - uint32_t size; // must be 92...blksize - uint32_t checksum; // CRC32 (without this field) - uint32_t resv1; - uint64_t this_lba; - uint64_t alt_lba; - uint64_t first_lba; - uint64_t last_lba; - char disk_uuid[16]; - uint64_t part_table_lba; - uint32_t part_entry_count; - uint32_t sizeof_partiton_entry; - uint32_t partition_entry_checksum; - // rest of fields are reserved. -} OBOS_PACK gpt_header; -typedef uint64_t gpt_partition_attrib; -#define GPT_ATTRIB_REQUIRED = BIT_TYPE(0, UL) -#define GPT_NO_BLOCK_IO = BIT_TYPE(1, UL) -#define GPT_LEGACY_BIOS_BOOTABLE = BIT_TYPE(2, UL) -#define GPT_TYPE_UUID_START = BIT_TYPE(48, UL) -#define GPT_TYPE_UUID_END = BIT_TYPE(63, UL) -typedef struct gpt_partition_entry -{ - char uuid[16]; - char part_uuid[16]; - uint64_t begin_lba; - uint64_t end_lba; - uint64_t attributes; - uint16_t part_name[36]; -} OBOS_PACK gpt_partition_entry; -obos_status OBOS_IdentifyGPTPartitions(fd* desc, partition* partition_list, size_t* nPartitions, bool allow_checksum_fail); \ No newline at end of file diff --git a/src/oboskrnl/handle.c b/src/oboskrnl/handle.c deleted file mode 100644 index f8ed5604..00000000 --- a/src/oboskrnl/handle.c +++ /dev/null @@ -1,354 +0,0 @@ -/* - * oboskrnl/handle.c - * - * Copyright (c) 2024-2026 Omar Berrow - */ - -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include - -#include - -#include -#include -#include -#include - -#include -#include - -#include -#include - -#include - -#include - -OBOS_NO_UBSAN void OBOS_ExpandHandleTable(handle_table* table, size_t size) -{ - if (size <= table->size) - return; - const size_t oldSize = table->size; - table->size = size; - handle_desc* tmp_arr = ZeroAllocate(OBOS_KernelAllocator, table->size, sizeof(*table->arr), nullptr); - if (table->arr) - memcpy(tmp_arr, table->arr, sizeof(*table->arr)*oldSize); - for (handle_desc* desc = table->head; desc; ) - { - handle_desc *next = desc->un.next; - if (next) - desc->un.next = tmp_arr + (next - table->arr); - desc = next; - } - if (table->head) - table->head = tmp_arr + (table->head - table->arr); - Free(OBOS_KernelAllocator, table->arr, sizeof(*table->arr)*oldSize); - table->arr = tmp_arr; - table->last_handle = oldSize; -} - -void OBOS_InitializeHandleTable(handle_table* table) -{ - memzero(table, sizeof(*table)); - table->lock = MUTEX_INITIALIZE(); - OBOS_ExpandHandleTable(table, 64); -} - -handle_table* OBOS_CurrentHandleTable() -{ - // can only access cpu local stuff at IRQL_DISPATCH - irql oldIrql = Core_GetIrql() < IRQL_DISPATCH ? Core_RaiseIrql(IRQL_DISPATCH) : IRQL_INVALID; - handle_table* table = &CoreS_GetCPULocalPtr()->currentThread->proc->handles; - if (oldIrql != IRQL_INVALID) - Core_LowerIrql(oldIrql); - return table; - -} - -void OBOS_LockHandleTable(handle_table* table) -{ - Core_MutexAcquire(&table->lock); -} - -void OBOS_UnlockHandleTable(handle_table* table) -{ - Core_MutexRelease(&table->lock); -} - -#define in_range(ra,rb,x) (((x) >= (ra)) && ((x) < (rb))) -handle_desc* OBOS_HandleLookup(handle_table* table, handle hnd, handle_type type, bool ignoreType, obos_status* status) -{ - OBOS_ASSERT(table); - // First, validate the handle type. - if (HANDLE_TYPE(hnd) >= LAST_VALID_HANDLE_TYPE) - { - if (status) - *status = OBOS_STATUS_INVALID_ARGUMENT; - return nullptr; - } - if (HANDLE_TYPE(hnd) != type && !ignoreType) - { - if (status) - *status = OBOS_STATUS_INVALID_ARGUMENT; - return nullptr; - } - hnd &= HANDLE_VALUE_MASK; - if (hnd >= table->size) - { - if (status) - *status = OBOS_STATUS_INVALID_ARGUMENT; - return nullptr; - } - if (in_range(table->arr, table->arr+table->size, table->arr[hnd].un.next)) - { - if (status) - *status = OBOS_STATUS_INVALID_ARGUMENT; - return nullptr; // use-after-free - } - if (!table->arr[hnd].un.next) - { - if (status) - *status = OBOS_STATUS_INVALID_ARGUMENT; - return nullptr; // use-after-free; it is impossible for a handle in-use to be nullptr - } - if (status) - *status = OBOS_STATUS_SUCCESS; - if (!ignoreType) - OBOS_ASSERT(table->arr[hnd].type == type); - return &table->arr[hnd]; -} - -handle OBOS_HandleAllocate(handle_table* table, handle_type type, handle_desc** const desc) -{ - OBOS_ASSERT(table); - OBOS_ASSERT(desc); - handle hnd = 0; - if (table->head) - { - hnd = table->head - table->arr; - table->head = table->head->un.next; - } - else - { - if ((table->last_handle + 1) >= table->size) - OBOS_ExpandHandleTable(table, OBOS_MAX(table->size + (table->size / 4), hnd)); - hnd = table->last_handle++; - } - *desc = &table->arr[hnd]; - memzero(&table->arr[hnd], sizeof(table->arr[hnd])); - table->arr[hnd].type = type; - hnd |= (type << HANDLE_TYPE_SHIFT); - return hnd; -} - -void OBOS_HandleFree(handle_table* table, handle_desc *curr) -{ - curr->type = HANDLE_TYPE_INVALID; - curr->un.next = table->head; - table->head = curr; - // any use of this handle past here is a use-after-free -} - -void unimpl_handle_clone(handle_desc *hnd, handle_desc *new) -{ - OBOS_UNUSED(new); - OBOS_Warning("Cannot clone handle descriptor %p. Unimplemented.\n", hnd); -} - -void fd_clone(handle_desc* hnd, handle_desc* new) -{ - new->un.fd = Vfs_Calloc(1, sizeof(fd)); - uint32_t oflags = 0; - if (hnd->un.fd->flags & FD_FLAGS_READ) - oflags |= FD_OFLAGS_READ; - if (hnd->un.fd->flags & FD_FLAGS_WRITE) - oflags |= FD_OFLAGS_WRITE; - if (hnd->un.fd->flags & FD_FLAGS_UNCACHED) - oflags |= FD_OFLAGS_UNCACHED; - if (hnd->un.fd->flags & FD_FLAGS_NOEXEC) - oflags |= FD_OFLAGS_NOEXEC; - if (hnd->un.fd->vn->flags & VFLAGS_PTMX) - { - memcpy(new->un.fd, hnd->un.fd->vn, sizeof(fd)); - memzero(&new->un.fd->node, sizeof(new->un.fd->node)); - LIST_APPEND(fd_list, &hnd->un.fd->vn->opened, new->un.fd); - driver_ftable* ftable = &Vfs_GetVnodeDriver(hnd->un.fd->vn)->ftable; - OBOS_ENSURE(ftable); - OBOS_ENSURE(ftable->reference_device); - ftable->reference_device(&new->un.fd->desc); - new->un.fd->offset = hnd->un.fd->offset; - return; - } - Vfs_FdOpenVnode(new->un.fd, hnd->un.fd->vn, oflags); - new->un.fd->offset = hnd->un.fd->offset; -} - -void(*OBOS_HandleCloneCallbacks[LAST_VALID_HANDLE_TYPE])(handle_desc *hnd, handle_desc *new) = { - fd_clone, - unimpl_handle_clone, - unimpl_handle_clone, - unimpl_handle_clone, - unimpl_handle_clone, - unimpl_handle_clone, - unimpl_handle_clone, - unimpl_handle_clone, - unimpl_handle_clone, - unimpl_handle_clone, - unimpl_handle_clone, - unimpl_handle_clone, -}; - -void fd_close(handle_desc* hnd) -{ - Vfs_FdClose(hnd->un.fd); - Vfs_Free(hnd->un.fd); - hnd->un.as_int = 0; -} -void dirent_close(handle_desc* hnd) -{ - Free(OBOS_KernelAllocator, hnd->un.dirent, sizeof(struct dirent_handle)); - hnd->un.as_int = 0; -} -void drv_close(handle_desc* hnd) -{ - Drv_UnrefDriver(hnd->un.driver_id); - hnd->un.as_int = 0; -} -void process_close(handle_desc* hnd) -{ - if (!(--hnd->un.process->refcount)) - Free(OBOS_NonPagedPoolAllocator, hnd->un.process, sizeof(process)); - hnd->un.as_int = 0; -} -void thread_ctx_close(handle_desc* hnd) -{ - Free(OBOS_KernelAllocator, hnd->un.thread_ctx, sizeof(*hnd->un.thread_ctx)); - hnd->un.as_int = 0; -} -void irp_close(handle_desc* hnd) -{ - user_irp* req = hnd->un.irp; - // if (req->obj->evnt) - // CoreH_AbortWaitingThreads(WAITABLE_OBJECT(*req->obj->evnt)); - if (req->obj->buff) - Mm_VirtualMemoryFree(&Mm_KernelContext, req->obj->buff, req->buff_size); - VfsH_IRPUnref(req->obj); - Vfs_Free(req); - hnd->un.as_int = 0; -} -extern void OBOS_ThreadHandleFree(handle_desc* hnd); -void unimpl_close(handle_desc* hnd) -{ - OBOS_Warning("could not close handle of type %d\n", hnd->type); -} - -void(*OBOS_HandleCloseCallbacks[LAST_VALID_HANDLE_TYPE])(handle_desc *hnd) = { - fd_close, - nullptr, - dirent_close, - OBOS_ThreadHandleFree, - process_close, - nullptr, // TODO: Refcount vmm contexts. - nullptr, - nullptr, - nullptr, - nullptr, - drv_close, - thread_ctx_close, - irp_close, -}; - -static obos_status handle_close_unlocked(handle_table* current_table, handle hnd); - -obos_status Sys_HandleClone(handle hnd, handle* unew) -{ - handle_table* const current_table = &CoreS_GetCPULocalPtr()->currentThread->proc->handles; - - OBOS_LockHandleTable(current_table); - obos_status status = OBOS_STATUS_SUCCESS; - handle_desc* desc = OBOS_HandleLookup(current_table, hnd, 0, true, &status); - if (obos_is_error(status)) - { - OBOS_UnlockHandleTable(current_table); - return status; - } - - handle_type type = HANDLE_TYPE(hnd); - if (!OBOS_HandleCloneCallbacks[type]) - { - OBOS_UnlockHandleTable(current_table); - return OBOS_STATUS_INVALID_OPERATION; - } - - handle_desc* new_desc = nullptr; - handle new = 0; - memcpy_usr_to_k(&new, unew, sizeof(handle)); - OBOS_Debug("%s: *unew=%d\n", __func__, new); - if (new == HANDLE_ANY) - { - new = OBOS_HandleAllocate(current_table, type, &new_desc); - status = memcpy_k_to_usr(unew, &new, sizeof(new)); - if (obos_is_error(status)) - { - OBOS_UnlockHandleTable(current_table); - OBOS_HandleFree(current_table, new_desc); - return status; - } - } - else - { - OBOS_ExpandHandleTable(current_table, new+1); - new_desc = ¤t_table->arr[new]; - desc = ¤t_table->arr[hnd]; - handle_type type = new_desc->type; - handle_close_unlocked(current_table, new | (type << 24)); - current_table->head = new_desc->un.next; - } - - void(*cb)(handle_desc *hnd, handle_desc *new) = OBOS_HandleCloneCallbacks[type]; - cb(desc, new_desc); - new_desc->type = type; - OBOS_UnlockHandleTable(current_table); - - return OBOS_STATUS_SUCCESS; -} -static obos_status handle_close_unlocked(handle_table* current_table, handle hnd) -{ - // Get the handle descriptor. - obos_status status = OBOS_STATUS_SUCCESS; - handle_desc* desc = OBOS_HandleLookup(current_table, hnd, 0, true, &status); - if (obos_is_error(status)) - { - OBOS_UnlockHandleTable(current_table); - return status; - } - - // Free the handle's underlying object as well as the handle itself. - handle_type type = desc->type; - void(*cb)(handle_desc *hnd) = OBOS_HandleCloseCallbacks[type]; - if (cb) - cb(desc); - OBOS_HandleFree(current_table, desc); - return status; -} -obos_status Sys_HandleClose(handle hnd) -{ - handle_table* const current_table = &CoreS_GetCPULocalPtr()->currentThread->proc->handles; - OBOS_LockHandleTable(current_table); - - obos_status status = handle_close_unlocked(current_table, hnd); - - OBOS_UnlockHandleTable(current_table); - - return status; -} diff --git a/src/oboskrnl/handle.h b/src/oboskrnl/handle.h deleted file mode 100644 index 9c4521c4..00000000 --- a/src/oboskrnl/handle.h +++ /dev/null @@ -1,114 +0,0 @@ -/* - * oboskrnl/handle.h - * - * Copyright (c) 2024-2025 Omar Berrow - */ - -#pragma once - -#include - -#include - -struct dirent_handle -{ - struct dirent* curr; - struct dirent* parent; -}; - -typedef enum handle_type -{ - // vfs/fd.h - HANDLE_TYPE_FD, - // irq/timer.h - HANDLE_TYPE_TIMER, - // vfs/dirent.h - HANDLE_TYPE_DIRENT, - // scheduler/thread.h - HANDLE_TYPE_THREAD, - // scheduler/process.h - HANDLE_TYPE_PROCESS, - // mm/context.h - HANDLE_TYPE_VMM_CONTEXT, - // locks/mutex.h - HANDLE_TYPE_MUTEX, - // locks/semaphore.h - HANDLE_TYPE_SEMAPHORE, - // locks/pushlock.h - HANDLE_TYPE_PUSHLOCK, - // locks/event.h - HANDLE_TYPE_EVENT, - // driver_interface/driverId.h - HANDLE_TYPE_DRIVER_ID, - // scheduler/thread_context_info.h - HANDLE_TYPE_THREAD_CTX, - // vfs/irp.h - HANDLE_TYPE_IRP, - - LAST_VALID_HANDLE_TYPE, - - HANDLE_TYPE_ANY = 0xfd, - HANDLE_TYPE_CURRENT = 0xfe, - HANDLE_TYPE_INVALID = 0xff, -} handle_type; - -typedef struct handle_desc -{ - union { - struct handle_desc* next; // for the freelist. - struct fd* fd; - struct timer* timer; - struct dirent_handle* dirent; - struct thread* thread; - struct process* process; - struct context* vmm_context; - struct mutex* mutex; - struct semaphore* semaphore; - struct pushlock* pushlock; - struct event* event; - struct driver_id* driver_id; - struct thread_ctx_handle* thread_ctx; - struct waitable_header* waitable; - struct user_irp* irp; - void* generic; // just in case - uintptr_t as_int; // just in case - } un; - // TODO: Integrate this in a way that it doesn't add extra memory overhead? - handle_type type; -} handle_desc; - -#define HANDLE_VALUE_MASK (0xffffff) -#define HANDLE_TYPE_SHIFT (24U) - -#define HANDLE_TYPE(hnd) (handle_type)((hnd) >> HANDLE_TYPE_SHIFT) -#define HANDLE_VALUE(hnd) (unsigned int)((hnd) & HANDLE_VALUE_MASK) -typedef uint32_t handle; -#define HANDLE_INVALID (handle)((handle)HANDLE_TYPE_INVALID << HANDLE_TYPE_SHIFT) -#define HANDLE_CURRENT (handle)((handle)HANDLE_TYPE_CURRENT << HANDLE_TYPE_SHIFT) -#define HANDLE_ANY (handle)((handle)HANDLE_TYPE_ANY << HANDLE_TYPE_SHIFT) - -typedef struct handle_table { - handle_desc* arr; - handle_desc* head; // freelist head - handle last_handle; - size_t size; - mutex lock; -} handle_table; - -void OBOS_ExpandHandleTable(handle_table* table, size_t size); - -// Call before manipulating a handle descriptor, or calling a handle table manipulation (allocate, free, lookup) function. -void OBOS_LockHandleTable(handle_table* table); -void OBOS_UnlockHandleTable(handle_table* table); - -void OBOS_InitializeHandleTable(handle_table* table); -handle_table* OBOS_CurrentHandleTable(); -handle_desc* OBOS_HandleLookup(handle_table* table, handle hnd, handle_type type, bool ignoreType, obos_status* status); -handle OBOS_HandleAllocate(handle_table* table, handle_type type, handle_desc** const desc); -void OBOS_HandleFree(handle_table* table, handle_desc *curr); - -extern void(*OBOS_HandleCloneCallbacks[LAST_VALID_HANDLE_TYPE])(handle_desc *hnd, handle_desc *new); -extern void(*OBOS_HandleCloseCallbacks[LAST_VALID_HANDLE_TYPE])(handle_desc *hnd); - -obos_status Sys_HandleClone(handle hnd, handle* new); -obos_status Sys_HandleClose(handle hnd); diff --git a/src/oboskrnl/init_proc.c b/src/oboskrnl/init_proc.c deleted file mode 100644 index 46fc9c3a..00000000 --- a/src/oboskrnl/init_proc.c +++ /dev/null @@ -1,162 +0,0 @@ -/* - * oboskrnl/init_proc.c - * - * Copyright (c) 2024-2026 Omar Berrow - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include - -#include -#include -#include -#include - -#include -#include - -#include - -#include -#include - -static struct exec_aux_values aux; - -static char* strcpy(const char* str) -{ - size_t len = strlen(str); - return memcpy(Allocate(OBOS_KernelAllocator, len+1, nullptr), str, len+1); -} - -void OBOS_LoadInit() -{ - if (!Core_GetCurrentThread()->proc->session || !Core_GetCurrentThread()->proc->session->controlling_tty) - { - OBOS_Error("%s: Cannot load init due to non-existent controlling tty.\n", __func__); - return; - } - - bool should_load_init = OBOS_GetOPTF("no-init"); - if (should_load_init) - { - OBOS_Log("%s: Not loading init due to kernel command line option 'no-init'\n", __func__); - return; - } - - // Freed while handing off control to init. - char* init_path = OBOS_GetOPTS("init-path"); - fd init_fd = {}; - - if (!init_path) - init_path = memcpy(Allocate(OBOS_KernelAllocator, 6, nullptr), "/init", 6); - - OBOS_Log("Loading %s\n", init_path); - - obos_status status = Vfs_FdOpen(&init_fd, init_path, FD_OFLAGS_READ|FD_OFLAGS_EXECUTE); - if (obos_is_error(status)) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "Could not open %s. Status: %d\n", init_path, status); - - context* new_ctx = ZeroAllocate(Mm_Allocator, 1, sizeof(context), nullptr); - Mm_ConstructContext(new_ctx); - process* new = Core_ProcessAllocate(nullptr); - new->ctx = new_ctx; - new_ctx->owner = new; - new_ctx->workingSet.capacity = 64*1024*1024; - Core_ProcessStart(new, nullptr); - new->umask = 0002; - new->exec_file = VfsH_DirentPath(VfsH_DirentLookup(init_path), nullptr); - - void* buf = Mm_VirtualMemoryAlloc(&Mm_KernelContext, nullptr, init_fd.vn->filesize, OBOS_PROTECTION_READ_ONLY, VMA_FLAGS_PRIVATE, &init_fd, nullptr); - if (obos_is_error(status)) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "Could not map init program. Status: %d\n", status); - - status = OBOS_LoadELF(new_ctx, buf, init_fd.vn->filesize, &aux.elf, false, false); - if (obos_is_error(status)) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "Could not load %s. Status: %d\n", init_path, status); - - OBOS_Log("Loaded %s at 0x%p\n", init_path, aux.elf.base); - OBOS_Debug("NOTE: Dynamic linker loaded at 0x%p\n", aux.elf.rtld_base); - - const Elf_Ehdr* ehdr = buf; - aux.phdr.ptr = (void*)((uintptr_t)aux.elf.base + ehdr->e_phoff); - aux.phdr.phent = ehdr->e_phentsize; - aux.phdr.phnum = ehdr->e_phnum; - - aux.argc = 1+OBOS_InitArgumentsCount; - aux.argv = ZeroAllocate(OBOS_KernelAllocator, aux.argc+1, sizeof(char*), nullptr); - aux.argv[0] = init_path; - for (size_t i = 1; i < (OBOS_InitArgumentsCount+1); i++) - aux.argv[i] = strcpy(OBOS_argv[OBOS_InitArgumentsStart+i-1]); - - do { - if (Core_GetCurrentThread()->proc->cmdline) - Free(OBOS_KernelAllocator, Core_GetCurrentThread()->proc->cmdline, strlen(Core_GetCurrentThread()->proc->cmdline)+1); - Core_GetCurrentThread()->proc->cmdline = nullptr; - string cmd_line = {}; - process* proc = new; - OBOS_InitString(&cmd_line, init_path); - for (size_t i = 1; i < aux.argc; i++) - { - size_t len_arg = strlen(aux.argv[i]); - if (strchr(aux.argv[i], ' ') == len_arg) - { - OBOS_AppendStringC(&cmd_line, " "); - OBOS_AppendStringC(&cmd_line, aux.argv[i]); - } - else - { - OBOS_AppendStringC(&cmd_line, " \""); - OBOS_AppendStringC(&cmd_line, aux.argv[i]); - OBOS_AppendStringC(&cmd_line, "\""); - } - } - proc->cmdline = memcpy(Allocate(OBOS_KernelAllocator, OBOS_GetStringSize(&cmd_line)+1, nullptr), - OBOS_GetStringCPtr(&cmd_line), - OBOS_GetStringSize(&cmd_line)+1); - OBOS_FreeString(&cmd_line); - } while(0); - - aux.envp = nullptr; - aux.envpc = 0; - - // aux.envpc = 2; - // aux.envp = ZeroAllocate(OBOS_KernelAllocator, aux.envpc+1, sizeof(char*), nullptr); - // aux.envp[0] = memcpy(Allocate(OBOS_KernelAllocator, sizeof("MLIBC_RTLD_DEBUG_VERBOSE=1"), nullptr), "MLIBC_RTLD_DEBUG_VERBOSE=1", sizeof("MLIBC_RTLD_DEBUG_VERBOSE=1")); - // aux.envp[1] = memcpy(Allocate(OBOS_KernelAllocator, sizeof("MLIBC_RTLD_DEBUG=1"), nullptr), "MLIBC_RTLD_DEBUG=1", sizeof("MLIBC_RTLD_DEBUG=1")); - // aux.envp[2] = nullptr; - - thread* thr = CoreH_ThreadAllocate(nullptr); - thread_ctx thr_ctx = {}; - - thr->kernelStack = Mm_AllocateKernelStack(new_ctx, &status); - if (obos_is_error(status)) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "Could not allocate kernel stack for init program.\n"); - - CoreS_SetupThreadContext(&thr_ctx, (uintptr_t)OBOSS_HandOffToInit, (uintptr_t)&aux, false, thr->kernelStack, 0x10000); - - // init takes the process group of the kernel. - Core_SetProcessGroup(new, 1); - if (new->session->controlling_tty) - new->session->controlling_tty->fg_job = new->pgrp; - - // CoreS_SetThreadPageTable(&thr_ctx, new_ctx->pt); - - CoreH_ThreadInitialize(thr, THREAD_PRIORITY_NORMAL, Core_DefaultThreadAffinity, &thr_ctx); - - Core_ProcessAppendThread(new, thr); - - thr->signal_info = OBOSH_AllocateSignalHeader(); - - CoreH_ThreadReady(thr); -} diff --git a/src/oboskrnl/init_proc.h b/src/oboskrnl/init_proc.h deleted file mode 100644 index 48571517..00000000 --- a/src/oboskrnl/init_proc.h +++ /dev/null @@ -1,14 +0,0 @@ -/* - * oboskrnl/init_proc.h - * - * Copyright (c) 2024 Omar Berrow - */ - -#pragma once - -#include -#include - -void OBOS_LoadInit(); - -OBOS_NORETURN OBOS_WEAK void OBOSS_HandOffToInit(struct exec_aux_values* info); diff --git a/src/oboskrnl/int.h b/src/oboskrnl/int.h index c2799310..bfebb537 100644 --- a/src/oboskrnl/int.h +++ b/src/oboskrnl/int.h @@ -1,132 +1,154 @@ -/* - * oboskrnl/int.h - * - * Copyright (c) 2024-2025 Omar Berrow -*/ - -#pragma once - -#include -#include - -#define obos_expect(expr, eval) __builtin_expect((expr), (eval)) - -#if defined(OBOS_KERNEL) || defined(OBOS_DRIVER) -# define OBOS_EXPORT __attribute__((visibility("default"))) -// Usually redundant. -# define OBOS_LOCAL __attribute__((visibility("hidden"))) - -# define DRV_EXPORT __attribute__((visibility("default"))) -// Usually redundant. -# define DRV_LOCAL __attribute__((visibility("hidden"))) -#else -# error Only the kernel and drivers can access kernel headers. -#endif - -#define OBOS_WEAK __attribute__((weak)) - -#if UINTPTR_MAX == UINT64_MAX -#define PTR_BITS 64 -#elif UINTPTR_MAX == UINT32_MAX -#define PTR_BITS 32 -#elif UINTPTR_MAX == UINT16_MAX -#define PTR_BITS 16 -#endif - -typedef int32_t uid, gid; -#define ROOT_UID 0 -#define ROOT_GID 0 - -#if !defined(__cplusplus) && !defined(true) && !defined(false) -// # define true (1) -// # define false (0) -enum {false=0,true=1}; -typedef _Bool bool; -#endif -#ifndef __cplusplus -# define nullptr ((void*)0) -# ifndef IS_UACPI_BUILD -# undef NULL -# endif -# define OBOS_MAYBE_UNUSED [[maybe_unused]] -// Do all this to make sure intellisense is happy. -# if __STDC_VERSION__ >= 201112L && __STDC_VERSION__ < 202311L -# define OBOS_ALIGNAS(x) _Alignas(x) -# elif __STDC_VERSION__ < 201112L -# ifdef __GNUC__ -# define OBOS_ALIGNAS(x) __attribute__((alignas(x))) -# define OBOS_MAYBE_UNUSED __attribute__((maybe_unused)) -# elif defined(_MSC_VER) -# define OBOS_ALIGNAS(x) __declspec(align(x)) -# define OBOS_MAYBE_UNUSED -# endif -# else -# define OBOS_ALIGNAS(x) alignas(x) -# endif -#else -# define OBOS_ALIGNAS(x) alignas(x) -# ifndef IS_UACPI_BUILD -# undef NULL -# endif -#endif - -#define OBOS_NORETURN [[noreturn]] -#define OBOS_UNUSED(...) (void)(__VA_ARGS__) -#ifdef __GNUC__ -# define OBOS_NO_KASAN __attribute__((no_sanitize("address"))) -# define OBOS_NO_UBSAN __attribute__((no_sanitize("undefined"))) -# define OBOS_NODISCARD __attribute__ ((warn_unused_result)) -# define OBOS_NODISCARD_REASON(why) __attribute__ ((warn_unused_result)) -# define OBOS_MIN(val, val2) \ - ({ typeof (val) _a = (val); \ - typeof (val2) _b = (val2); \ - _a < _b ? _a : _b; }) -# define OBOS_MAX(val, val2) \ - ({ typeof (val) _a = (val); \ - typeof (val2) _b = (val2); \ - _a > _b ? _a : _b; }) -#else -# define OBOS_NO_KASAN -# define OBOS_NO_UBSAN -# define OBOS_NODISCARD -# define OBOS_NODISCARD_REASON(why) -#endif -#if __STDC_VERSION__ < 202311L && __STDC_VERSION__ >= 201112L -# define OBOS_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg) -#elif __STDC_VERSION__ == 202311L -# define OBOS_STATIC_ASSERT(expr, msg) static_assert(expr, msg) -#else -# define OBOS_STATIC_ASSERT(expr, msg) -#endif - -#define BIT_TYPE(b, type) (1##type << (b)) -#define BIT(b) BIT_TYPE(b, U) -#if __STDC_NO_ATOMICS__ -# error No atomics supported by the compiler. -#endif -#if __STDC_HOSTED__ -# error Must be compiled as freestanding. -#endif -#if OBOS_KERNEL -# define OBOS_PAGEABLE_VARIABLE __attribute__((section(".pageable.data"))) -# define OBOS_PAGEABLE_RO_VARIABLE __attribute__((section(".pageable.rodata"))) -# define OBOS_PAGEABLE_FUNCTION __attribute__((section(".pageable.text"))) -#elif defined(OBOS_DRIVER) && !defined(__m68k__) -# define OBOS_PAGEABLE_VARIABLE __attribute__((section(".pageable.data"))) -# define OBOS_PAGEABLE_RO_VARIABLE __attribute__((section(".pageable.rodata"))) -# define OBOS_PAGEABLE_FUNCTION __attribute__((section(".pageable.text"))) -#else -# define OBOS_PAGEABLE_VARIABLE -# define OBOS_PAGEABLE_RO_VARIABLE -# define OBOS_PAGEABLE_FUNCTION -#endif - -#include - -#if defined(__x86_64__) || defined(__i386__) -# define OBOSS_SpinlockHint() asm("pause") -#elif defined(__m68k__) -# define OBOSS_SpinlockHint() asm("nop") -#else -# define OBOSS_SpinlockHint() asm("") +#pragma once + +#include +#include + +#if __GNUC__ +# define OBOS_NO_KASAN __attribute__((no_sanitize_address)) +#else +# define OBOS_NO_KASAN +#endif +#if __GNUC__ +# define OBOS_NO_UBSAN __attribute__((no_sanitize("undefined"))) +# define OBOS_NO_UBSAN_FOR(what) __attribute__((no_sanitize(#what))) +#else +# define OBOS_NO_UBSAN +# define OBOS_NO_UBSAN_FOR(what) +#endif +#if defined(__cplusplus) +namespace obos +{ + namespace arch + { + enum class endianness + { + LittleEndian = 0, + BigEndian = 1, + MixedEndian = 2, + }; + constexpr endianness g_endianness = OBOS_ARCHITECTURE_ENDIANNESS; + } +} +#endif + +#if defined(__cplusplus) && defined(OBOS_SLOW_ARTHIMETRIC) +// Don't even ask why this exists. +// - @oberrow, 8:30 PM on the 6th of april, 2024. +#ifdef __x86_64__ +#define spinlock_hint() asm volatile("pause") +#endif +#define sleep(a)\ +{\ + uint64_t wakeTime = _ZN4obos9scheduler7g_ticksE + a;\ + while (_ZN4obos9scheduler7g_ticksE < wakeTime)\ + spinlock_hint();\ +} +extern "C" uint64_t _ZN4obos9scheduler7g_ticksE; +template +inline T operator+(T a, T b) +{ + auto add_raw = [](T a, T b) { +#ifdef __x86_64__ + asm volatile("add %1, %0" : "=r"(a) : "r"(b)); + return a; +#endif + }; + if (!_ZN4obos9scheduler7g_ticksE) + return add_raw(a,b); + uint64_t begin = _ZN4obos9scheduler7g_ticksE; + sleep(a); + sleep(b); + return _ZN4obos9scheduler7g_ticksE-begin; +} +template +inline T operator-(T a, T b) +{ + auto sub_raw = [](T a, T b) { +#ifdef __x86_64__ + asm volatile("sub %1, %0" : "=r"(a) : "r"(b)); + return a; +#endif + }; + if (!_ZN4obos9scheduler7g_ticksE) + return sub_raw(a,b); + uint64_t begin = _ZN4obos9scheduler7g_ticksE; + sleep(a); + sleep(b); + return sub_raw(begin, _ZN4obos9scheduler7g_ticksE); +} +template +inline T operator*(T a, T b) +{ + T res = a; + for (int i = 0; i < b; i = i + 1) + res = res + a; + return res; +} +template +inline T operator/(T a, T b) +{ + T ret = 0; + for (; ; ret++, a = a - b) + spinlock_hint(); + return ret; +} +template +inline T operator%(T a, T b) +{ + T ret = 0; + for (; ; ret++, a = a - b) + spinlock_hint(); + return a; +} +template +inline T operator+=(T &a, T &b) +{ + return (a = a + b); +} +template +inline T operator-=(T &a, T &b) +{ + return (a = a - b); +} +template +inline T operator*=(T a, T b) +{ + return (a = a * b); +} +template +inline T operator/=(T &a, T &b) +{ + return (a = a / b); +} +template +inline T operator%(T &a, T &b) +{ + return (a = a % b); +} +template +inline T operator++(T& a, int) +{ + T copy = a; + a = a + 1; + return copy; +} +template +inline T operator++(T& a) +{ + return (a = a + 1); +} +template +inline T operator--(T& a, int) +{ + T copy = a; + a = a - 1; + return copy; +} +template +inline T operator--(T& a) +{ + return (a = a - 1); +} +#undef spinlock_hint +#undef sleep #endif \ No newline at end of file diff --git a/src/oboskrnl/irq/dpc.c b/src/oboskrnl/irq/dpc.c deleted file mode 100644 index 47ea978e..00000000 --- a/src/oboskrnl/irq/dpc.c +++ /dev/null @@ -1,69 +0,0 @@ -/* - * oboskrnl/irq/dpc.c - * - * Copyright (c) 2024 Omar Berrow -*/ - -#include -#include -#include - -#include - -#include -#include - -#include -#include - -#include - -#include - -LIST_GENERATE(dpc_queue, dpc, node); - -dpc* CoreH_AllocateDPC(obos_status* status) -{ - if (!OBOS_NonPagedPoolAllocator) - return ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(dpc), status); - return ZeroAllocate(OBOS_NonPagedPoolAllocator, 1, sizeof(dpc), status); -} -obos_status CoreH_InitializeDPC(dpc* dpc, void(*handler)(struct dpc* obj, void* userdata), thread_affinity affinity) -{ - if (!dpc || !handler) - return OBOS_STATUS_INVALID_ARGUMENT; - if (dpc->cpu) - return OBOS_STATUS_DPC_ALREADY_ENQUEUED; - const thread_affinity affinity_real = !(affinity & Core_DefaultThreadAffinity) ? (Core_DefaultThreadAffinity) : (affinity & Core_DefaultThreadAffinity); - dpc->handler = handler; - cpu_local* target = nullptr; - for (size_t i = 0; i < Core_CpuCount; i++) - { - if ((!target || Core_CpuInfo[i].dpcs.nNodes < target->dpcs.nNodes) && - (affinity_real & CoreH_CPUIdToAffinity(Core_CpuInfo[i].id))) - target = &Core_CpuInfo[i]; - } - // If this fails, something stupid has happened. - OBOS_ASSERT(target); - dpc->cpu = target; - irql oldIrql = Core_SpinlockAcquireExplicit(&target->dpc_queue_lock, IRQL_MASKED, false); - LIST_PREPEND(dpc_queue, &target->dpcs, dpc); - Core_SpinlockRelease(&target->dpc_queue_lock, oldIrql); - dpc->handler = handler; - return OBOS_STATUS_SUCCESS; -} -obos_status CoreH_FreeDPC(dpc* dpc, bool dealloc) -{ - if (!dpc) - return OBOS_STATUS_INVALID_ARGUMENT; - if (!dpc->handler || !dpc->cpu) - return OBOS_STATUS_UNINITIALIZED; - if (!LIST_IS_NODE_UNLINKED(dpc_queue, &dpc->cpu->dpcs, dpc)) - { - irql oldIrql = Core_SpinlockAcquire(&dpc->cpu->dpc_queue_lock); - LIST_REMOVE(dpc_queue, &dpc->cpu->dpcs, dpc); - Core_SpinlockRelease(&dpc->cpu->dpc_queue_lock, oldIrql); - dpc->cpu = nullptr; - } - return dealloc ? Free(OBOS_NonPagedPoolAllocator, dpc, sizeof(*dpc)) : OBOS_STATUS_SUCCESS; -} diff --git a/src/oboskrnl/irq/dpc.h b/src/oboskrnl/irq/dpc.h deleted file mode 100644 index 5956429f..00000000 --- a/src/oboskrnl/irq/dpc.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * oboskrnl/irq/dpc.h - * - * Copyright (c) 2024 Omar Berrow -*/ - -#pragma once - -#include -#include - -#include - -#include - -typedef LIST_HEAD(dpc_queue, struct dpc) dpc_queue; -LIST_PROTOTYPE(dpc_queue, struct dpc, node); - -typedef struct dpc -{ - LIST_NODE(dpc_queue, struct dpc) node; - // The handler mustn't ever lower the irql below IRQL_DISPATCH - // This is called at IRQL_DISPATCH. - void(*handler)(struct dpc* dpc, void* userdata); - void* userdata; - struct cpu_local* cpu; -} dpc; - -OBOS_EXPORT dpc* CoreH_AllocateDPC(obos_status* status); -OBOS_EXPORT obos_status CoreH_InitializeDPC(dpc* dpc, void(*handler)(struct dpc* obj, void* userdata), thread_affinity affinity); -OBOS_EXPORT obos_status CoreH_FreeDPC(dpc* dpc, bool dealloc); diff --git a/src/oboskrnl/irq/irq.c b/src/oboskrnl/irq/irq.c deleted file mode 100644 index e25f20e1..00000000 --- a/src/oboskrnl/irq/irq.c +++ /dev/null @@ -1,384 +0,0 @@ -/* - * oboskrnl/irq/irq.c - * - * Copyright (c) 2024 Omar Berrow - */ - -#include -#include -#include -#include - -#include -#include - -#include - -#include - -#include - -#include - -irq* Core_IrqObjectAllocate(obos_status* status) -{ - if (!OBOS_NonPagedPoolAllocator) - return ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(irq), status); - return ZeroAllocate(OBOS_NonPagedPoolAllocator, 1, sizeof(irq), status); -} -static irq_vector s_irqVectors[OBOS_IRQ_VECTOR_ID_MAX]; -static spinlock s_lock; -static bool s_irqInterfaceInitialized; -void Core_IRQDispatcher(interrupt_frame* frame) -{ -#if !OBOS_ARCH_EMULATED_IRQL && !OBOS_LAZY_IRQL - irql irql_ = OBOS_IRQ_VECTOR_ID_TO_IRQL(frame->vector); - irql oldIrql2 = Core_RaiseIrqlNoThread(irql_); - if (!CoreS_EnterIRQHandler(frame)) - return; - CoreS_SendEOI(frame); -#elif OBOS_LAZY_IRQL - irql irql_ = OBOS_IRQ_VECTOR_ID_TO_IRQL(frame->vector); - if (irql_ < CoreS_GetCPULocalPtr()->currentIrql) - { - CoreS_SetIRQL(irql_, CoreS_GetIRQL()); - CoreS_DeferIRQ(frame); - CoreS_SendEOI(frame); - return; - } - if (!CoreS_EnterIRQHandler(frame)) - return; - CoreS_SendEOI(frame); - irql oldIrql2 = Core_RaiseIrqlNoThread(irql_); -#else - irql irql_ = OBOS_IRQ_VECTOR_ID_TO_IRQL(frame->vector); - if (!CoreS_EnterIRQHandler(frame)) - return; // some archs do IRQL emulation this way. - if (irql_ <= Core_GetIrql()) - OBOS_Panic(OBOS_PANIC_FATAL_ERROR, "IRQL on call of the dispatcher is less than the IRQL of the vector reported by the architecture (\"irql_ <= Core_GetIrql()\")."); - CoreS_SendEOI(frame); - irql oldIrql2 = Core_RaiseIrqlNoThread(irql_); -#endif - // irql oldIrql = Core_SpinlockAcquireExplicit(&s_lock, irql_, false); - if (!s_irqVectors[frame->vector].allowWorkSharing) - { - irq* irq_obj = s_irqVectors[frame->vector].irqObjects.head->data; - OBOS_ASSERT(irq_obj->handler); - irq_obj->handler( - irq_obj, - frame, - irq_obj->handlerUserdata, - oldIrql2); - // Core_SpinlockRelease(&s_lock, oldIrql); - } - else - { - irq_vector* vector = &s_irqVectors[frame->vector]; - for (irq_node* node = vector->irqObjects.head; node; ) - { - irq* cur = node->data; - OBOS_ASSERT(cur->irqChecker); // to make sure the developer doesn't mess up; compiled out in release mode - if (cur->irqChecker) - { - irq* irq_obj = nullptr; - if (cur->irqChecker(cur, cur->irqCheckerUserdata)) - { - irq_obj = cur; - OBOS_ASSERT(irq_obj->handler); - irq_obj->handler( - irq_obj, - frame, - irq_obj->handlerUserdata, - oldIrql2); - } - } - - node = node->next; - } - // Core_SpinlockRelease(&s_lock, oldIrql); - } - CoreS_ExitIRQHandler(frame); - Core_LowerIrqlNoThread(oldIrql2); -} -obos_status Core_InitializeIRQInterface() -{ - if (s_irqInterfaceInitialized) - return OBOS_STATUS_ALREADY_INITIALIZED; - for (irq_vector_id i = 0; i < sizeof(s_irqVectors) / sizeof(*s_irqVectors); i++) - { - s_irqVectors[i].allowWorkSharing = true; - s_irqVectors[i].irqObjectsCapacity = 16; - s_irqVectors[i].id = i; - memzero(&s_irqVectors[i].irqObjects, sizeof(s_irqVectors[i].irqObjects)); - } - s_irqInterfaceInitialized = true; - return OBOS_STATUS_SUCCESS; -} -static void append_irq_to_vector(irq_vector* This, irq* what) -{ - OBOS_ASSERT(This); - OBOS_ASSERT(what); - irq_node* node = ZeroAllocate(OBOS_KernelAllocator, 1, sizeof(irq_node), nullptr); - OBOS_ASSERT(node); - node->data = what; - if (!This->irqObjects.head) - This->irqObjects.head = node; - if (This->irqObjects.tail) - This->irqObjects.tail->next = node; - node->prev = This->irqObjects.tail; - This->irqObjects.tail = node; - This->irqObjects.nNodes++; -} -static void remove_irq_from_vector(irq_vector* This, irq_node* what) -{ - OBOS_ASSERT(This); - OBOS_ASSERT(what); - if (what->next) - what->next->prev = what->prev; - if (what->prev) - what->prev->next = what->next; - if (This->irqObjects.head == what) - This->irqObjects.head = what->next; - if (This->irqObjects.tail == what) - This->irqObjects.tail = what->prev; - This->irqObjects.nNodes--; -} -static obos_status register_irq_vector_handler(irq_vector_id id, void(*handler)(interrupt_frame*)) -{ - obos_status s = OBOS_STATUS_SUCCESS; - if (CoreS_IsIRQVectorInUse(id) == OBOS_STATUS_IN_USE) - s = CoreS_RegisterIRQHandler(id, nullptr); - if (s == OBOS_STATUS_SUCCESS) - s = CoreS_RegisterIRQHandler(id, handler); - return s; -} -static obos_status register_irq_vector(irq* obj, irq_vector_id id, bool allowWorkSharing, bool force) -{ - irq_vector* vector = &s_irqVectors[id]; - obj->vector = vector; - if (allowWorkSharing && force) - force = false; - if ((allowWorkSharing && vector->allowWorkSharing) || !vector->irqObjects.nNodes) - { - append_irq_to_vector(vector, obj); - vector->allowWorkSharing = allowWorkSharing; - return register_irq_vector_handler(vector->id, Core_IRQDispatcher); - } - if (!force) - return OBOS_STATUS_IN_USE; - if (allowWorkSharing && !vector->allowWorkSharing) - { - if (vector->irqObjects.head->data->choseVector) - return OBOS_STATUS_IN_USE; - // The other object didn't choose its own vector, so we can move it to another vector and put us on this vector. - // Note: In this case, it is extremely likely that the vector id was chosen using Core_IrqObjectInitializeVector. - // Anyway, just move this irq object to another empty vector of the same IRQL. - // If another empty vector of the same IRQL does not exist, fail. - uint8_t requiredIrql = OBOS_IRQ_VECTOR_ID_TO_IRQL(vector->id); - irq_vector_id base = OBOS_IRQL_TO_IRQ_VECTOR_ID(requiredIrql); - irq_vector_id found = OBOS_IRQ_VECTOR_ID_MAX; - for (irq_vector_id vec = base; vec < OBOS_IRQ_VECTOR_ID_COUNT_PER_IRQL + base; vec++) - { - irq_vector* vector_ = &s_irqVectors[vec]; - OBOS_ASSERT(vector_->id == vec); - OBOS_ASSERT(vector_->irqObjectsCapacity); - if (!vector_->irqObjects.nNodes) - { - found = vec; - break; - } - } - if (found == OBOS_IRQ_VECTOR_ID_MAX) - return OBOS_STATUS_IN_USE; - // Move the irq object on the requested vector to another vector. - irq_vector* newVector = &s_irqVectors[found]; - irq* cur = vector->irqObjects.head->data; - if (cur->moveCallback) - cur->moveCallback(cur, vector, newVector, cur->irqMoveCallbackUserdata); - else - OBOS_Warning("%s: IRQ Object 0x%p (IRQ Vector %d) does not have a move callback, and was moved.\n", __func__, cur, cur->vector->id); - remove_irq_from_vector(vector, vector->irqObjects.head); - append_irq_to_vector(newVector, cur); - // Append this IRQ object to the vector. - append_irq_to_vector(vector, obj); - cur->vector = newVector; - newVector->allowWorkSharing = vector->allowWorkSharing; - vector->allowWorkSharing = allowWorkSharing; - - obos_status status = register_irq_vector_handler(vector->id, Core_IRQDispatcher); - if (obos_is_error(status)) - return status; - status = register_irq_vector_handler(newVector->id, Core_IRQDispatcher); - return status; - } - // We've forced this. - // Move the vectors in this vector to some other vector with the same IRQL. - // Note: This should fail if one of the vectors set its own vector. - if (vector->nIRQsWithChosenID) - return OBOS_STATUS_IN_USE; - uint8_t requiredIrql = OBOS_IRQ_VECTOR_ID_TO_IRQL(vector->id); - irq_vector_id found = OBOS_IRQ_VECTOR_ID_MAX; - irq_vector_id base = OBOS_IRQL_TO_IRQ_VECTOR_ID(requiredIrql); - bool shouldIgnoreObjectCapacity = false; -find: - for (irq_vector_id vec = base; vec < OBOS_IRQ_VECTOR_ID_COUNT_PER_IRQL + base; vec++) - { - irq_vector* vector_ = &s_irqVectors[vec]; - OBOS_ASSERT(vector_->id == vec); - OBOS_ASSERT(vector_->irqObjectsCapacity); - if (vector_ == vector) - continue; - if ((vector_->irqObjects.nNodes < vector_->irqObjectsCapacity || shouldIgnoreObjectCapacity) && (!vector_->nIRQsWithChosenID || (vector_->allowWorkSharing && allowWorkSharing))) - { - if (vector_->irqObjects.nNodes == 0) - goto l1; - if (vector_->allowWorkSharing && allowWorkSharing) - goto l1; - if (force) - goto l1; - l1: - found = vec; - if (shouldIgnoreObjectCapacity) - vector_->irqObjectsCapacity += vector_->irqObjectsCapacity / 4; // Same as multiplying by 1.25 - break; - } - } - if (found == OBOS_IRQ_VECTOR_ID_MAX) - { - if (!shouldIgnoreObjectCapacity) - goto find; - return OBOS_STATUS_NOT_FOUND; - } - irq_vector* newVector = &s_irqVectors[found]; - for (irq_node* node = vector->irqObjects.head; node; ) - { - irq* cur = node->data; - OBOS_ASSERT(cur); - if (cur->moveCallback) - cur->moveCallback(cur, vector, newVector, cur->irqMoveCallbackUserdata); - else - OBOS_Warning("%s: IRQ Object 0x%p (IRQ Vector %d) does not have a move callback, and was moved.\n", __func__, cur, cur->vector->id); - irq_node* nnode = node->next; - remove_irq_from_vector(vector, node); - node = nnode; - append_irq_to_vector(newVector, cur); - cur->vector = newVector; - } - newVector->allowWorkSharing = vector->allowWorkSharing; - vector->allowWorkSharing = false; - append_irq_to_vector(vector, obj); - obos_status status = register_irq_vector_handler(vector->id, Core_IRQDispatcher); - if (obos_is_error(status)) - return status; - status = register_irq_vector_handler(newVector->id, Core_IRQDispatcher); - return status; -} -obos_status Core_IrqObjectInitializeIRQL(irq* obj, irql requiredIrql, bool allowWorkSharing, bool force) -{ - if (!s_irqInterfaceInitialized) - return OBOS_STATUS_INVALID_INIT_PHASE; - if (!obj || requiredIrql > IRQL_MASKED || requiredIrql == 0 -#if OBOS_IRQL_COUNT == 16 - || requiredIrql == 1 -#endif - ) - return OBOS_STATUS_INVALID_ARGUMENT; - if (obj->vector) - return OBOS_STATUS_ALREADY_INITIALIZED; -#ifndef __m68k__ - irql oldIrql = Core_SpinlockAcquire(&s_lock); -#else - irql oldIrql = Core_SpinlockAcquireExplicit(&s_lock, IRQL_MASKED, false); -#endif - irq_vector_id found = OBOS_IRQ_VECTOR_ID_MAX; - irq_vector_id base = OBOS_IRQL_TO_IRQ_VECTOR_ID(requiredIrql); - bool shouldIgnoreObjectCapacity = false; - find: - for (irq_vector_id vec = base; vec < OBOS_IRQ_VECTOR_ID_COUNT_PER_IRQL + base; vec++) - { - irq_vector* vector = &s_irqVectors[vec]; - OBOS_ASSERT(vector->id == vec); - OBOS_ASSERT(vector->irqObjectsCapacity); - if ((vector->irqObjects.nNodes < vector->irqObjectsCapacity || shouldIgnoreObjectCapacity) && (!vector->nIRQsWithChosenID || (vector->allowWorkSharing && allowWorkSharing))) - { - if (vector->irqObjects.nNodes == 0) - goto l1; - if (vector->allowWorkSharing && allowWorkSharing) - goto l1; - if (force) - goto l1; - continue; - l1: - found = vec; - if (shouldIgnoreObjectCapacity) - vector->irqObjectsCapacity += vector->irqObjectsCapacity / 4; // Same as multiplying by 1.25 - break; - } - } - if (found == OBOS_IRQ_VECTOR_ID_MAX) - { - if (!shouldIgnoreObjectCapacity) - goto find; - Core_SpinlockRelease(&s_lock, oldIrql); - return OBOS_STATUS_NOT_FOUND; - } - obj->choseVector = false; - obos_status res = register_irq_vector(obj, found, allowWorkSharing, force); - Core_SpinlockRelease(&s_lock, oldIrql); - return res; -} -obos_status Core_IrqObjectInitializeVector(irq* obj, irq_vector_id vector, bool allowWorkSharing, bool force) -{ - if (!s_irqInterfaceInitialized) - return OBOS_STATUS_INVALID_INIT_PHASE; - if (!obj || vector >= OBOS_IRQ_VECTOR_ID_MAX) - return OBOS_STATUS_INVALID_ARGUMENT; - if (obj->vector) - return OBOS_STATUS_ALREADY_INITIALIZED; - obj->choseVector = true; - irql oldIrql = Core_SpinlockAcquire(&s_lock); - obos_status res = register_irq_vector(obj, vector, allowWorkSharing, force); - obj->vector->nIRQsWithChosenID++; - Core_SpinlockRelease(&s_lock, oldIrql); - return res; -} -obos_status Core_IrqObjectFree(irq* obj) -{ - if (!s_irqInterfaceInitialized) - return OBOS_STATUS_INVALID_INIT_PHASE; - if (!obj) - return OBOS_STATUS_INVALID_ARGUMENT; - if (obj->vector) - { - irql oldIrql = Core_SpinlockAcquire(&s_lock); - irq_node* node = nullptr; - if (!obj->vector->allowWorkSharing) - node = obj->vector->irqObjects.head; - else - { - for (irq_node* cur = obj->vector->irqObjects.head; cur && !node; ) - { - if (cur->data == obj) - node = cur; - cur = cur->next; - } - } - OBOS_ASSERT(node); - OBOS_ASSERT(node->data == obj); - remove_irq_from_vector(obj->vector, node); - if (!obj->vector->irqObjects.nNodes) - { - obj->vector->allowWorkSharing = true; - obj->vector->irqObjectsCapacity = 16; - } - obj->vector->nIRQsWithChosenID -= (size_t)obj->choseVector; - Core_SpinlockRelease(&s_lock, oldIrql); - } - // FIXME: Set a free callback in the irq object instead of assuming the kernel allocator. - Free(OBOS_KernelAllocator, obj, sizeof(*obj)); - return OBOS_STATUS_SUCCESS; -} -bool Core_IrqInterfaceInitialized() -{ - return s_irqInterfaceInitialized; -} diff --git a/src/oboskrnl/irq/irq.cpp b/src/oboskrnl/irq/irq.cpp new file mode 100644 index 00000000..7a3647fb --- /dev/null +++ b/src/oboskrnl/irq/irq.cpp @@ -0,0 +1,295 @@ +/* + oboskrnl/irq/irq.cpp + + Copyright (c) 2024 Omar Berrow +*/ + +#include + +#include +#include +#include + +#include +#include + +#include + +#include + +#include + +#include + +#include +#include +#include +#include + +namespace obos +{ + template + concept iframe = requires (T x) { + { x.vector }; + }; + template