127 Commits

Author SHA1 Message Date
zxq5 ecdc76b068 Merge remote-tracking branch 'origin' into unified 2025-03-06 01:18:43 +00:00
zxq5 8ee4af1a48 someone broke a mod.rs so i fixed it. 2025-03-05 21:10:36 +00:00
zxq5 03c07cb5c4 Merging with origin/unified 2025-03-05 21:04:58 +00:00
zxq5 ef70bcf51e - heap allocator now makes sure virtual memory pages are mapped before using them. this means we no longer have to use page faults to allocate frames, it is done automatically.
- fixed the apic code that caused a page fault. it now writes the correct values at the correct physical address
- improved startup logging including displaying how much RAM is installed.
2025-03-05 20:49:21 +00:00
nullndvoid 2fb1741100 Begin setting up stack unwinding/tracing.
This actually worked so I am chuffed, but it doesn't read the necessary
DWARF structures just yet. Still a good step forwards.
2025-03-05 20:44:09 +00:00
nullndvoid af995d1d76 Tried and failed to use unwinding crate: shall do it myself tomorrow. 2025-03-05 03:24:26 +00:00
nullndvoid 014ec5310c Some more clippy fixes, warnings are noisy 2025-03-05 00:57:06 +00:00
nullndvoid d53661b9a0 Ran cargo fmt, clippy fixes, suppressed some warns
I will start working on stack traces tonight and tomorrow.

We need to be able to 'unwind' by finding calling functions.
2025-03-04 23:06:47 +00:00
zxq5 8704b5d249 FIXED THE APIC HELL YEAH 2025-03-04 12:44:42 +00:00
zxq5 2186b829aa - made improvements to memory code (refactored)
- started on improvements to the page frame allocator. it should be able to provide a usable page for any given virtual memory address requested.
2025-03-04 01:28:39 +00:00
zxq5 f502104a6e changes 2025-03-04 00:53:38 +00:00
zxq5 48dcde0c02 minor changes & work on threading 2025-03-04 00:53:06 +00:00
nullndvoid 6e913b15d7 Update macro to use relative working dirs, Spans 2025-03-03 15:57:16 +00:00
nullndvoid 0a5269eeb0 Fix clippy errors 2025-03-03 15:51:39 +00:00
zxq5 a0f5ce8797 reorganised memory. starting on threads 2025-03-03 15:50:58 +00:00
nullndvoid 2d3bc56962 Fix some clippy errors 2025-03-03 14:53:30 +00:00
nullndvoid 5703032f87 Fix module inception 2025-03-03 14:47:24 +00:00
zxq5 3966e697da refactor mega-commit.
- reorganised the entire project so that the entire kernel is a single codebase rather than a kernel and a libk.
2025-03-03 02:49:56 +00:00
aha480 e8237f4610 run_debug.sh now checks if it running on wsl and disables boot_flags if it is since it was causing issues with qemu 2025-03-01 12:49:42 +00:00
nullndvoid 53d325749d Fix incorrect target name in config 2025-02-28 16:15:26 +00:00
zxq5 8a3e9e3afc - implemented a custom allocator (fixed size block) with a fallback (linked list allocator) for larger block sizes
- apic code still not working (commented out, check lib.rs)
2025-02-28 04:18:35 +00:00
zxq5 e38c20dbd3 Merge pull request 'editor' (#6) from editor into dev
Reviewed-on: OsDev/FoundryOS#6
2025-02-28 03:05:38 +00:00
zxq5 c8bb85364c - updated to-dos
- removed unnecessary debugging code
- moved x86_64-kernel.json to the project root
2025-02-28 03:05:10 +00:00
zxq5 fe18004f7d Merge remote-tracking branch 'origin' into editor
merging into dev
2025-02-27 23:58:01 +00:00
zxq5 192100be7a changed page allocation to happen on page fault for performance reasons 2025-02-27 23:57:23 +00:00
nullndvoid 2915d0c879 Add tips on debugging/disassembling kernel sources 2025-02-27 23:32:05 +00:00
zxq5 9852cb14eb updated build scripts 2025-02-27 22:58:10 +00:00
zxq5 db8dbff9f2 added memory allocation flag to run command ``VM_MEMORY=<amount>G cargo run`` 2025-02-27 21:57:58 +00:00
zxq5 2178215a01 fixed triple fault 2025-02-27 16:52:31 +00:00
zxq5 821759ec63 apic broken pushing to debug 2025-02-27 16:19:43 +00:00
zxq5 0b3dbed4be updated documentation 2025-02-27 02:30:48 +00:00
zxq5 3b6e272fd2 not working 2025-02-27 01:16:07 +00:00
zxq5 ac0b47a45c merge 2025-02-26 04:00:00 +00:00
zxq5 15f59e68d5 initial apic implementaion (Commented out / NOT WORKING) 2025-02-26 03:48:11 +00:00
nullndvoid b7397d8a1b Remove unused clippy lints 2025-02-25 03:20:05 +00:00
nullndvoid 4fe6109e5b Rename scheduling to 'async_io', general cleanup 2025-02-25 03:18:12 +00:00
nullndvoid e294a13a91 Cleanup the README a little more 2025-02-25 02:59:55 +00:00
nullndvoid 752800a3ca Merge UEFI support for qemu into our runner scripts/build system 2025-02-25 02:56:48 +00:00
nullndvoid 177fddcf7d Add warnings to hardware script (dd implies 'doubly dangerous') 2025-02-25 02:55:52 +00:00
nullndvoid 375c5aa561 Update limine.conf to boot up faster 2025-02-25 02:54:19 +00:00
nullndvoid b945416665 Add qemu UEFI firmware support. 2025-02-25 02:53:28 +00:00
zxq5 27ee8226d8 implemented stdin methods for reading a string (async) and reading keystrokes (sync + async). added a very basic shell on top of it for debugging 2025-02-25 02:16:01 +00:00
nullndvoid 00d3a1de72 fb: Added support for typing backspaces. 2025-02-24 23:24:03 +00:00
zxq5 f7723a3944 making the keyboard module more flexible, still needs work 2025-02-24 18:24:21 +00:00
zxq5 438ef7a748 stop banning mod.rs 2025-02-24 16:30:38 +00:00
zxq5 2f7ab8fe6b Merge remote-tracking branch 'refs/remotes/origin/dev' into dev 2025-02-24 16:29:58 +00:00
zxq5 ca1c695e32 created stdio module, added hardware script 2025-02-24 16:28:56 +00:00
zxq5 1c22f89665 reverted dumb change 2025-02-24 15:34:46 +00:00
nullndvoid 1c147dd933 Ban module inception, this is annoying.
@zxq5 thoughts?
2025-02-24 15:34:29 +00:00
zxq5 91044f5fad merged dumb change 2025-02-24 15:33:59 +00:00
zxq5 d5b15826e2 adding std io idk 2025-02-24 15:33:42 +00:00
nullndvoid 39f1f4905d Ban mod.rs. 2025-02-24 15:29:39 +00:00
nullndvoid 03a3469d20 Formatting changes to task.rs 2025-02-24 15:19:01 +00:00
nullndvoid 2cbe9641aa Use prelude lol. 2025-02-24 15:14:12 +00:00
zxq5 e303ecf5a4 fixed merge commit tomfoolery. 2025-02-24 15:14:21 +00:00
zxq5 2f08835d69 merge commit. probably broken tbh 2025-02-24 15:10:58 +00:00
nullndvoid 8d57540566 Fix clippy errors 2025-02-24 15:02:44 +00:00
zxq5 03290e52a3 fixed a dumb error about me hardcoding paths 2025-02-24 13:07:11 +00:00
zxq5 8908a6a281 Update README.md 2025-02-24 03:45:02 +00:00
zxq5 aefc368d41 updated readme for more comprehensive build instructions 2025-02-24 03:44:22 +00:00
zxq5 76ec29b7ec changed #usage back to being a tag
(was not a markdown fail, it's an obsidian tag)
2025-02-24 03:36:28 +00:00
zxq5 d9bbdff08c - added a new libary libm containing procedural macros for the kernel.
these should be used to include external files and resources in the kernel binary
  at compile time.
  - libm currently supports loading psf-1 formatted fonts
- added two fonts that are included in the binary at compile time
- refactored libk to make the crate structure more organised and maintainable in future.
  new structure:
    - drivers   (hardware interaction)
    - resources (consts and statics included either manually or via macros)
    - std       (standard functions for higher level interaction with the os, for example creating windows)
- added geometry.rs
  - provides the Vec2<T> struct for use with dimensions, coordinates etc.
- added window.rs
  - provides the Window struct for rendering the state of an application to the screen
- added application.rs
  - provides the Application trait for custom programs to implement in order to run
2025-02-24 03:26:49 +00:00
zxq5 7ff33659fe async works! 2025-02-23 19:27:13 +00:00
zxq5 efe3034ad0 Merge pull request 'libk now replaces the plethora of lib crates' (#5) from nullndvoid/FoundryOS:dev into dev
Reviewed-on: OsDev/FoundryOS#5
2025-02-23 17:07:25 +00:00
nullndvoid d9b66f8008 Update README to remove refs to submodules. 2025-02-23 12:06:50 +00:00
nullndvoid 2be5b63af7 Fix some more clippy lints. 2025-02-23 11:58:52 +00:00
nullndvoid 7e62bdae35 Make rust-analyzer use clippy as the check command. 2025-02-23 11:53:42 +00:00
nullndvoid b8aa203c05 Bump edition to now stable 2024 edition (shiny!). 2025-02-23 11:52:54 +00:00
nullndvoid 9f83c5f295 Merge GDB support (and updated runner scripts) into dev. 2025-02-23 11:47:51 +00:00
nullndvoid 914cb4e409 Enable support for $USE_GDB when running in debug mode. 2025-02-23 11:43:26 +00:00
nullndvoid 43b1db41ca Introducing the hottest library crate in town (libk!) 2025-02-23 04:43:22 +00:00
nullndvoid c763f512f1 Accidentally forgot to add lib alloc lol 2025-02-23 03:16:22 +00:00
nullndvoid 5dc73e7000 Update Cargo.toml to use new registry 2025-02-23 03:15:19 +00:00
nullndvoid ca906767db Update docs (markdown fail) and checkout libraries again 2025-02-23 02:19:38 +00:00
nullndvoid ef8d028211 Update liballoc and libapplication. 2025-02-23 02:15:44 +00:00
nullndvoid 2886fe5ecf Update 2025-02-23 02:07:01 +00:00
nullndvoid c2c552ef25 Fixed most of the clippy warnings/suggestions. 2025-02-23 02:05:13 +00:00
nullndvoid e722874cee Formatting and fixing warnings 2025-02-23 01:38:21 +00:00
zxq5 27b0ed634c setup but not working 2025-02-22 23:12:46 +00:00
zxq5 72fe78cbc6 Merge pull request 'dev' (#4) from dev into main
Continuous integration / build (push) Failing after 2m12s
Reviewed-on: OsDev/FoundryOS#4
2025-02-22 21:32:27 +00:00
zxq5 f9bc75c4f3 added boot messages 2025-02-22 21:30:13 +00:00
zxq5 494d00c53b Update .gitea/workflows/rust.yml 2025-02-22 21:18:14 +00:00
zxq5 114c70ffe9 updated submodules
Continuous integration / build (push) Failing after 2m7s
2025-02-22 21:03:17 +00:00
zxq5 361c67764d totally didn't import an allocator... 2025-02-22 21:02:29 +00:00
zxq5 36cb118933 paging done, starting on allocation.
Continuous integration / build (push) Failing after 1m51s
2025-02-22 16:56:01 +00:00
zxq5 7b3ba170f0 changes to submodules
Continuous integration / build (push) Failing after 1m56s
2025-02-22 15:43:06 +00:00
zxq5 49880fa9d7 Merge remote-tracking branch 'refs/remotes/origin/dev' into dev
merging into dev
2025-02-22 15:41:54 +00:00
zxq5 0bbbf653f8 setup GDT & fixed a deadlock 2025-02-22 15:41:41 +00:00
nullndvoid 40ad5dbbf4 Add page fault handler that does nothing, I am tired
Continuous integration / build (push) Failing after 38m24s
2025-02-22 05:00:18 +00:00
nullndvoid 2b2219f5be Update submodules
Continuous integration / build (push) Failing after 7m47s
2025-02-22 04:00:37 +00:00
nullndvoid c1a8afb836 Formatted random JSON file whoops
Continuous integration / build (push) Has been cancelled
2025-02-22 03:59:28 +00:00
zxq5 28afe25cca check boot was successful else panic
Continuous integration / build (push) Has been cancelled
2025-02-22 03:52:32 +00:00
zxq5 90faace7a2 changed name to snake case
Continuous integration / build (push) Has been cancelled
2025-02-22 03:50:13 +00:00
zxq5 5b1f04c1da changed some code order
Continuous integration / build (push) Has been cancelled
2025-02-22 03:47:54 +00:00
zxq5 3a232c8023 Merge remote-tracking branch 'refs/remotes/origin/dev' into dev (and add cargo fmt on save)
Continuous integration / build (push) Has been cancelled
2025-02-22 03:39:22 +00:00
zxq5 5eaf0d7c69 Merge remote-tracking branch 'refs/remotes/origin/dev' into dev
Continuous integration / build (push) Has been cancelled
2025-02-22 03:36:32 +00:00
zxq5 b4d0b05e13 updated submodule 2025-02-22 03:35:02 +00:00
nullndvoid 969756c691 Suppress erroneous build errors from rust-analyzer.
Continuous integration / build (push) Has been cancelled
2025-02-22 03:33:54 +00:00
zxq5 5c9717d384 cargo fmt
Continuous integration / build (push) Has been cancelled
2025-02-22 03:33:19 +00:00
zxq5 bb5bf9115b fixed weird merge issues
Continuous integration / build (push) Has been cancelled
2025-02-22 03:29:33 +00:00
zxq5 ae82e9c24b idk 2025-02-22 03:27:08 +00:00
zxq5 ab0ec35094 updated submodules
Continuous integration / build (push) Has been cancelled
2025-02-22 03:24:48 +00:00
zxq5 2ee21dea05 did interrupts stuff 2025-02-22 03:19:05 +00:00
zxq5 d12160c5d0 setup TSS 2025-02-22 03:16:13 +00:00
nullndvoid f5f5aeb8dc Formatting changes, called 'cargo fmt'
Continuous integration / build (push) Has been cancelled
2025-02-22 03:09:46 +00:00
nullndvoid 34213ca744 Merge remote into local dev, I should probably find a new branch lol
Continuous integration / build (push) Has been cancelled
2025-02-22 02:44:09 +00:00
nullndvoid 3aca8fd720 Add some docs on building, bumped limine to latest version
No issues occurred having bumped the version, all seems well.
2025-02-22 02:41:23 +00:00
zxq5 a88059c5ff workflow builds nightly
Continuous integration / build (push) Has been cancelled
2025-02-22 02:13:38 +00:00
zxq5 68c3d2fa0e workflow changes
Continuous integration / build (push) Failing after 1m37s
2025-02-22 01:55:36 +00:00
zxq5 af3215d887 changed order of steps in rust.yml
Continuous integration / build (push) Failing after 23m3s
2025-02-22 01:28:31 +00:00
zxq5 11d63535e9 added submodule cloning to rust.yml
Continuous integration / build (push) Has been cancelled
2025-02-22 01:27:08 +00:00
zxq5 ee8dae4981 added rust.yml workflow, probably wont work.
Continuous integration / build (push) Failing after 7m45s
2025-02-22 01:12:15 +00:00
zxq5 650f34f354 removed redundant script 2025-02-22 01:08:07 +00:00
zxq5 1b313c67e0 first interrupt handler + setup IDT 2025-02-22 01:06:55 +00:00
zxq5 cb77883f4c done 2025-02-22 01:06:27 +00:00
zxq5 16d94f37cb moving libs to submodules 2025-02-22 01:05:18 +00:00
zxq5 d9f6b4b69a e 2025-02-22 00:58:51 +00:00
zxq5 cc56dc8072 idk 2025-02-22 00:58:40 +00:00
zxq5 f0e25c7a5d started work on lib_application - progress limited as requires alloc support to make further progress 2025-02-21 16:13:06 +00:00
zxq5 e626a3bcf2 working on colour support 2025-02-21 16:13:27 +00:00
zxq5 34b960c20a println works 2025-02-20 22:59:24 +00:00
zxq5 ee8de361dc Merge remote-tracking branch 'refs/remotes/origin/dev' into dev
merge
2025-02-20 17:13:57 +00:00
zxq5 60efcf39b6 started setting up support for C code in the kernel 2025-02-20 17:13:40 +00:00
zxq5 b028a97a44 vscode workspace setup 2025-02-20 17:12:25 +00:00
zxq5 294e6f0e33 created example lib for sake of testing 2025-02-20 15:52:42 +00:00
zxq5 76f070af8b idk 2025-02-20 02:54:01 +00:00
zxq5 2f0e09b9bc it compiles! 2025-02-20 02:47:18 +00:00
150 changed files with 5136 additions and 316 deletions
+24
View File
@@ -0,0 +1,24 @@
[build]
target = "x86_64-kernel.json"
target-dir = "build/target"
[unstable]
build-std = ["core", "compiler_builtins", "alloc"]
build-std-features = ["compiler-builtins-mem"]
[env]
RUST_TARGET_PATH = { value = "kernel", relative = true }
# Run in debug mode.
[target.'cfg(all(target_arch = "x86_64", target_os = "none", debug_assertions))']
runner = "scripts/run_debug.sh"
# Otherwise we run in release mode.
[target.'cfg(all(target_arch = "x86_64", target_os = "none", not(debug_assertions)))']
runner = "scripts/run_release.sh"
# [registry]
# default = "gitea"
[registries.gitea]
index = "sparse+https://git.zxq5.dev/api/packages/OsDev/cargo/" # Sparse index
+22
View File
@@ -0,0 +1,22 @@
on:
push:
branches: [ main ]
pull-request:
branches: [ main ]
name: Continuous integration
jobs:
build:
name: build
runs-on: ubuntu-latest
steps:
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
target: x86_64-unknown-none
components: rust-src, llvm-tools-preview
toolchain: nightly
- uses: actions/checkout@v4
with:
submodules: true
- run: cargo build
+15 -1
View File
@@ -1 +1,15 @@
/target
# Build output directories
/build/
/target/
# Generated files
*.o
*.bin
*.iso
*.img
*.elf
*.efi
*.log
# Rust specific
Cargo.lock
+8
View File
@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
+15
View File
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="EMPTY_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/kernel/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/lib_example/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/libk/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/libm/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/kernel_fonts/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
+5
View File
@@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/FoundryOS.iml" filepath="$PROJECT_DIR$/.idea/FoundryOS.iml" />
</modules>
</component>
</project>
Generated
+7
View File
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
<mapping directory="$PROJECT_DIR$/build/limine" vcs="Git" />
</component>
</project>
+10
View File
@@ -0,0 +1,10 @@
{
"rust-analyzer.cargo.allTargets": false,
"rust-analyzer.cargo.target": "x86_64-kernel.json",
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer",
"editor.formatOnSave": true
},
"rust-analyzer.check.command": "clippy",
"rust-analyzer.cargo.buildScripts.enable": true
}
Generated
+302 -1
View File
@@ -2,6 +2,18 @@
# It is not intended for manual editing.
version = 4
[[package]]
name = "autocfg"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]]
name = "bit_field"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61"
[[package]]
name = "bitflags"
version = "2.8.0"
@@ -9,10 +21,158 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36"
[[package]]
name = "foundryos_kernel"
name = "cc"
version = "1.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c736e259eea577f443d5c86c304f9f4ae0295c43f3ba05c21f1d66b5f06001af"
dependencies = [
"shlex",
]
[[package]]
name = "crossbeam"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1137cd7e7fc0fb5d3c5a8678be38ec56e819125d8d7907411fe24ccb943faca8"
dependencies = [
"crossbeam-epoch",
"crossbeam-queue",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-queue"
version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f58bbc28f91df819d0aa2a2c00cd19754769c2fad90579b3592b1c9ba7a3115"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
[[package]]
name = "darling"
version = "0.20.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989"
dependencies = [
"darling_core",
"darling_macro",
]
[[package]]
name = "darling_core"
version = "0.20.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5"
dependencies = [
"fnv",
"ident_case",
"proc-macro2",
"quote",
"strsim",
"syn",
]
[[package]]
name = "darling_macro"
version = "0.20.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806"
dependencies = [
"darling_core",
"quote",
"syn",
]
[[package]]
name = "elf"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4445909572dbd556c457c849c4ca58623d84b27c8fff1e74b0b4227d8b90d17b"
[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "foundry_os"
version = "0.1.0"
dependencies = [
"cc",
"crossbeam",
"elf",
"futures-util",
"gimli",
"libm",
"limine",
"linked_list_allocator",
"pc-keyboard",
"pic8259",
"spin",
"x86_64",
]
[[package]]
name = "futures-core"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
[[package]]
name = "futures-task"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988"
[[package]]
name = "futures-util"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
dependencies = [
"futures-core",
"futures-task",
"pin-project-lite",
"pin-utils",
]
[[package]]
name = "gimli"
version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
[[package]]
name = "ident_case"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]]
name = "libm"
version = "0.1.0"
dependencies = [
"darling",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -23,3 +183,144 @@ checksum = "9ca87cab008b8efeebdbe037cd4d1438037d48c5cb6fed939ffa5aa06315a321"
dependencies = [
"bitflags",
]
[[package]]
name = "linked_list_allocator"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9afa463f5405ee81cdb9cc2baf37e08ec7e4c8209442b5d72c04cfb2cd6e6286"
dependencies = [
"spinning_top",
]
[[package]]
name = "lock_api"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "pc-keyboard"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0ca629cbb3f0d5b699c338f0129ff78c9bfd7ea8b1258ad529bff490dc8ed5a"
[[package]]
name = "pic8259"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62d9a86c292b165f757e47e7fd66855def189b2564609bc4203727b27c33db22"
dependencies = [
"x86_64",
]
[[package]]
name = "pin-project-lite"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b"
[[package]]
name = "pin-utils"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "proc-macro2"
version = "1.0.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rustversion"
version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4"
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "spin"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
dependencies = [
"lock_api",
]
[[package]]
name = "spinning_top"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b9eb1a2f4c41445a3a0ff9abc5221c5fcd28e1f13cd7c0397706f9ac938ddb0"
dependencies = [
"lock_api",
]
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "2.0.98"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00e2473a93778eb0bad35909dff6a10d28e63f792f16ed15e404fca9d5eeedbe"
[[package]]
name = "volatile"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "442887c63f2c839b346c192d047a7c87e73d0689c9157b00b53dcc27dd5ea793"
[[package]]
name = "x86_64"
version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f042214de98141e9c8706e8192b73f56494087cc55ebec28ce10f26c5c364ae"
dependencies = [
"bit_field",
"bitflags",
"rustversion",
"volatile",
]
+23 -4
View File
@@ -1,10 +1,29 @@
[workspace]
members = [
"kernel"
]
members = ["kernel", "libm"]
resolver = "2"
[workspace.package]
version = "0.1.0"
edition = "2021"
edition = "2024"
authors = ["The FoundryOS Contributors"]
[profile.dev]
opt-level = "z"
debug = true
# Leave this on to force Cargo to use the debug runner,
# which adds the necessary flags for GDB debugging if you
# set $USE_GDB (to any value) e.g. USE_GDB=1 cargo run
debug-assertions = true
overflow-checks = true
lto = false
incremental = false
codegen-units = 1
[profile.release]
opt-level = 3
debug = false
debug-assertions = false
overflow-checks = false
lto = true
incremental = false
codegen-units = 1
+50
View File
@@ -1,2 +1,52 @@
# FoundryOS
## Cloning
```sh
git clone https://git.zxq5.dev/OsDev/FoundryOS.git
```
## Build dependencies
* latest rust nightly release
* all necessary rust components installed
* xorriso: creates ISO images to be booted from.
* (Optional / Recommended) qemu: to run the kernel. (this may be packaged as qemu-desktop)
* (Optional) GDB: for debugging the kernel.
```sh
rustup update
rustup override set nightly
rustup component add rust-src
rustup component add llvm-tools-preview
```
## Building & Running in qemu
```sh
cargo run
```
## Running in GDB
```sh
USE_GDB=1 cargo run
```
## Build errors
If you see a qemu error like this:
```
qemu-system-x86_64: -drive if=pflash,format=raw,readonly=on,file=<...>/OVMF_CODE.fd: Could not open '<...>/OVMF_CODE.fd': No such file or directory
```
Simply delete the ./build directory and try to rebuild the program. Using the runner script or `cargo run` will download the required files for you, this is because the script only checks for the presence of one file and not the VARS file.
Alternatively, you may disable using a UEFI firmware with qemu like so:
```sh
USE_LEGACY_BIOS=1 cargo run
```
## Debugging
See [debugging](docs/Debugging/DEBUGGING.md) for some help with this, including commands to help with disassembly.
If you have any other issues, feel free to create an issue or a PR.
+8 -2
View File
@@ -1,5 +1,11 @@
timeout: 1
# Timeout in seconds that Limine will use before automatically booting.
timeout: 0
# The entry name that will be displayed in the boot menu.
/foundry-os
# We use the Limine boot protocol.
protocol: limine
kernel_path: boot()/boot/kernel
# Path to the kernel to boot. boot():/ represents the partition on which limine.conf is located.
kernel_path: boot():/boot/kernel
-30
View File
@@ -1,30 +0,0 @@
{
"llvm-target": "x86_64-unknown-none",
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
"arch": "x86_64",
"target-endian": "little",
"target-pointer-width": "64",
"target-c-int-width": "32",
"os": "none",
"executables": true,
"linker-flavor": "ld.lld",
"linker": "rust-lld",
"panic-strategy": "abort",
"disable-redzone": true,
"features": "-mmx,-sse,+soft-float",
"relocation-model": "static",
"code-model": "kernel",
"pre-link-args": {
"ld.lld": [
"--script=linker/linker.ld",
"-nostdlib",
"--no-dynamic-linker",
"-static",
"--no-pie",
"--gc-sections",
"--build-id=none",
"-z", "max-page-size=0x1000"
]
}
}
+17
View File
@@ -0,0 +1,17 @@
 Running kernel in debug mode.
info: Creating build directory structure
info: Copying files to ISO root
 Copying: /home/fantasypvp/Projects/OSdev/FoundryOS/build/target/x86_64-kernel/debug/kernel to /home/fantasypvp/Projects/OSdev/FoundryOS/build/iso_root/boot/kernel
 Copying: /home/fantasypvp/Projects/OSdev/FoundryOS/config/limine.conf to /home/fantasypvp/Projects/OSdev/FoundryOS/build/iso_root/boot/limine/limine.conf
 Copying: /home/fantasypvp/Projects/OSdev/FoundryOS/build/limine/limine-bios-cd.bin to /home/fantasypvp/Projects/OSdev/FoundryOS/build/iso_root/boot/limine/
 Copying: /home/fantasypvp/Projects/OSdev/FoundryOS/build/limine/limine-uefi-cd.bin to /home/fantasypvp/Projects/OSdev/FoundryOS/build/iso_root/boot/limine/
 Copying: /home/fantasypvp/Projects/OSdev/FoundryOS/build/limine/limine-bios.sys to /home/fantasypvp/Projects/OSdev/FoundryOS/build/iso_root/boot/limine/
 Copying: /home/fantasypvp/Projects/OSdev/FoundryOS/build/limine/BOOTX64.EFI to /home/fantasypvp/Projects/OSdev/FoundryOS/build/iso_root/EFI/BOOT/
 Copying: /home/fantasypvp/Projects/OSdev/FoundryOS/build/limine/BOOTIA32.EFI to /home/fantasypvp/Projects/OSdev/FoundryOS/build/iso_root/EFI/BOOT/
Building: bootable ISO image
info: Installing Limine bootloader
info: KVM acceleration enabled
info: Running OS in QEMU...
[=3h[=3h[=3hBdsDxe: loading Boot0001 "UEFI QEMU DVD-ROM QM00005 " from PciRoot(0x0)/Pci(0x1F,0x2)/Sata(0x2,0xFFFF,0x0)
BdsDxe: starting Boot0001 "UEFI QEMU DVD-ROM QM00005 " from PciRoot(0x0)/Pci(0x1F,0x2)/Sata(0x2,0xFFFF,0x0)
map worked!got apic base
+1
View File
@@ -0,0 +1 @@
{}
+3
View File
@@ -0,0 +1,3 @@
{
"cssTheme": "Dark Moss"
}
+1
View File
@@ -0,0 +1 @@
[]
+31
View File
@@ -0,0 +1,31 @@
{
"file-explorer": true,
"global-search": true,
"switcher": true,
"graph": true,
"backlink": true,
"canvas": true,
"outgoing-link": true,
"tag-pane": true,
"properties": false,
"page-preview": true,
"daily-notes": true,
"templates": true,
"note-composer": true,
"command-palette": true,
"slash-command": false,
"editor-status": true,
"bookmarks": true,
"markdown-importer": false,
"zk-prefixer": false,
"random-note": false,
"outline": true,
"word-count": true,
"slides": false,
"audio-recorder": false,
"workspaces": false,
"file-recovery": true,
"publish": false,
"sync": true,
"webviewer": false
}
+22
View File
@@ -0,0 +1,22 @@
{
"collapse-filter": true,
"search": "",
"showTags": false,
"showAttachments": false,
"hideUnresolved": false,
"showOrphans": true,
"collapse-color-groups": true,
"colorGroups": [],
"collapse-display": true,
"showArrow": false,
"textFadeMultiplier": 0,
"nodeSizeMultiplier": 1,
"lineSizeMultiplier": 1,
"collapse-forces": true,
"centerStrength": 0.518713248970312,
"repelStrength": 10,
"linkStrength": 1,
"linkDistance": 250,
"scale": 1,
"close": true
}
File diff suppressed because one or more lines are too long
@@ -0,0 +1,12 @@
{
"id": "obsidian-excalidraw-plugin",
"name": "Excalidraw",
"version": "2.8.3",
"minAppVersion": "1.1.6",
"description": "An Obsidian plugin to edit and view Excalidraw drawings",
"author": "Zsolt Viczian",
"authorUrl": "https://www.zsolt.blog",
"fundingUrl": "https://ko-fi.com/zsolt",
"helpUrl": "https://github.com/zsviczian/obsidian-excalidraw-plugin#readme",
"isDesktopOnly": false
}
File diff suppressed because one or more lines are too long
+185
View File
@@ -0,0 +1,185 @@
{
"main": {
"id": "20da7b1c0adc4114",
"type": "split",
"children": [
{
"id": "c178a2dff57ae0aa",
"type": "tabs",
"children": [
{
"id": "6d395e6a4c72e1c8",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "Planning & To-Dos.md",
"mode": "source",
"source": false
},
"icon": "lucide-file",
"title": "Planning & To-Dos"
}
}
]
}
],
"direction": "vertical"
},
"left": {
"id": "8c92fa79437d4f31",
"type": "split",
"children": [
{
"id": "e4a9f8318da249b0",
"type": "tabs",
"children": [
{
"id": "da6fd2aac65f47f1",
"type": "leaf",
"state": {
"type": "file-explorer",
"state": {
"sortOrder": "alphabetical",
"autoReveal": false
},
"icon": "lucide-folder-closed",
"title": "Files"
}
},
{
"id": "3f86b26f003a0d1a",
"type": "leaf",
"state": {
"type": "search",
"state": {
"query": "",
"matchingCase": false,
"explainSearch": false,
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical"
},
"icon": "lucide-search",
"title": "Search"
}
},
{
"id": "39755dd9db653e17",
"type": "leaf",
"state": {
"type": "bookmarks",
"state": {},
"icon": "lucide-bookmark",
"title": "Bookmarks"
}
}
]
}
],
"direction": "horizontal",
"width": 307.5
},
"right": {
"id": "baa2a8e61b737fa6",
"type": "split",
"children": [
{
"id": "b628e7116722c59a",
"type": "tabs",
"children": [
{
"id": "eb1781be59af8c30",
"type": "leaf",
"state": {
"type": "backlink",
"state": {
"file": "Usage/Building The Kernel.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
"showSearch": false,
"searchQuery": "",
"backlinkCollapsed": false,
"unlinkedCollapsed": true
},
"icon": "links-coming-in",
"title": "Backlinks for Building The Kernel"
}
},
{
"id": "18e4602ee5825e11",
"type": "leaf",
"state": {
"type": "outgoing-link",
"state": {
"file": "Usage/Building The Kernel.md",
"linksCollapsed": false,
"unlinkedCollapsed": true
},
"icon": "links-going-out",
"title": "Outgoing links from Building The Kernel"
}
},
{
"id": "df5aadad9f62dff1",
"type": "leaf",
"state": {
"type": "tag",
"state": {
"sortOrder": "frequency",
"useHierarchy": false,
"showSearch": false,
"searchQuery": ""
},
"icon": "lucide-tags",
"title": "Tags"
}
},
{
"id": "9bc68cb074d39ab5",
"type": "leaf",
"state": {
"type": "outline",
"state": {
"file": "Usage/Building The Kernel.md",
"followCursor": false,
"showSearch": false,
"searchQuery": ""
},
"icon": "lucide-list",
"title": "Outline of Building The Kernel"
}
}
],
"currentTab": 2
}
],
"direction": "horizontal",
"width": 300,
"collapsed": true
},
"left-ribbon": {
"hiddenItems": {
"switcher:Open quick switcher": false,
"graph:Open graph view": false,
"canvas:Create new canvas": false,
"daily-notes:Open today's daily note": false,
"command-palette:Open command palette": false,
"templates:Insert template": false
}
},
"active": "6d395e6a4c72e1c8",
"lastOpenFiles": [
"Usage/Building The Kernel.md",
"Planning & To-Dos.md",
"structure/idk.canvas",
"structure/kernel.md",
"structure/libk.md",
"structure/libm.md",
"structure/libk",
"structure",
"Usage",
"Welcome.md"
]
}
+16
View File
@@ -0,0 +1,16 @@
# Debugging the Kernel
Here we will add some helpful tips on debugging the kernel.
## Disassembling a public function
To disassemble a public function, first we need a symbol in the public symbol table, so start by making the function fully public (including any parent modules). Do this as though you are trying to make a public function for a library crate (this includes the `kernel` crate). Simply mark the function and any parent modules as public, up until the point of [lib.rs (kernel link)](../../kernel/src/lib.rs).
Then, we need to find the specific demangled symbol to disassemble, because the default objdump output can be very verbose.
```sh
# Change as required, I pipe to less and /SEARCH FOR FUNCTION HERE.
nm --demangle ./build/target/x86_64-kernel/debug/kernel | less
# Now just paste the symbol where it says YOUR_SYMBOL_HERE and profit. Use -Mintel for Intel assembly syntax.
objdump -Matt --source --line-numbers --visualize-jumps ./build/target/x86_64-kernel/debug/kernel --demangle=rust --disassemble="YOUR_SYMBOL_HERE"
```
+135
View File
@@ -0,0 +1,135 @@
> [!todo] Testing
> - [ ] setup custom test frameworks (Use CrystalOS-V2 as a reference)
> - [ ] write a sub-crate for tests
> - [ ] implement several unit tests on things that we are likely to break often
> - [ ] setup Git CI/CD pipeline with testing
> - [ ] figure out how to run QEMU in the test environment
> - [ ] write a script to run tests
> - [ ] a release should only be published if all tests pass
> [!todo] Memory
> - [ ] make sure page tables are correct. we're still getting the annoying page fault errors for the APIC
> - [ ] page allocator
> - [ ] e
> [!todo] Allocator
> - [x] learn about several allocator designs and decide on the optimal one
> - [x] implementation
> - [x] implement an allocate method
> - [x] implement a deallocate method
> - [x] set the global allocator
> - [ ] testing
> - [ ] write unit tests
> - [ ] many allocations
> - [ ] large allocations
> - [ ] allocating more memory than is available
> - [ ] pass unit tests
> [!todo] Threading
> - [ ] implement thread switching functionality
> - [ ] switch stacks
> - [ ] implement multi-threading
> - [ ] figure out how to turn on another core
> - [ ] Scheduler
> - [ ] which task goes on which thread?
> - [ ] round robin / time slicing algorithm
> - [ ] switch tasks on a given core
> - [ ] push registers
> - [ ] switch stacks
> - [ ] ensure the scheduler knows where to find the stack for each thread
> - [ ] move a given task to a new core
> - [ ] differentiate between kernel threads and user threads
> - [ ] create syscall to create a new thread
> [!todo] Filesystem
> - [ ] AHCI Implementation
> - [ ] figure out what hard drives are attached
> - [ ] read data
> - [ ] write data
> - [ ] FAT32 Implementation
> - [ ] parse FAT header
> - [ ] figure out where files are / what files are what
> - [ ] parse file metadata / figure out type of file
> - [ ] reading the entirety of a file into memory
> - [ ] *(extension)* ext2 Implementation
> - [ ] Syscall API
> - [ ] provide a user program with file read / write access given a filename
> - [ ] translate a filename into a disk location of data & the size of the data
> - [ ] provide an interface to read from a file or write to a file
> [!todo] APIC
> - [ ] Enable APIC
> - [x] Get apic location from MSR
> - [x] Enable APIC bit flag
> - [x] Disable Legacy PIC
> - [ ] Map physical APIC address to virtual memory
>
>> [!error]
>> Page fault exception occurs when APIC is activated
>
>- [ ] Setup Interrupt handlers to handle interrupts generated by the APIC
> - [ ] Timer Interrupts
> - [ ] Keyboard Interrupts
> [!todo] Userspace (req: Threading, Filesystem)
> - [ ] Load program into memory from disk
> - [ ] requires:
> - [ ] Program Loader
> - [ ] Parse ELF File
> - [ ] Load file into memory
> - [ ] Filesystem
> - [ ] Scheduler (to start and manage the process)
> - [ ] Start a user process
> - [ ] switch stacks
> - [ ] jump to user code
>[!todo] User Interface
>- This will be a library so that programs in userspace can provide a user interface rather than relying on text commands from a basic terminal
>- features required:
> - [ ] Rendering frames to a display buffer
> - [ ] requires: Frame rendering Syscall
> - [ ] I/O
> - [ ] syncio
> - [ ] keystrokes
> - [ ] asyncio
> - [ ] keystrokes
> - [ ] requires: I/O Syscalls
> - [ ] Window struct
> - [ ] stores it's size and location
> - [ ] handles rendering pixels to the screen
> - [ ] Widgets
> - [ ] textbox / label
> - [ ] single line text input
> - [ ] how do we capture keyboard inputs?
> - [ ] multiline text input
> - [ ] how do we capture keyboard inputs?
> - [ ] button
> - [ ] dialog
> - [ ] box widget
> - [ ] figure out how to nest other widgets inside it
>```rust
>pub struct Window {
> position;
> dimensions;
>}
>
>impl Window {
> fn display(&self, frame: &Frame) -> Result<(), RenderError>;
>}
>
>pub struct Frame<'a> {
> window: &'a Window,
> data: <"... a suitable type to store pixels">
>}
>
>impl<'a> Frame<'a> {
> pub fn display(&self) -> Result<(), RenderError> {
> self.window.display(self)
> }
>}
>
> pub trait Widget {
> fn render(&self) -> Frame;
> }
>```
+11
View File
@@ -0,0 +1,11 @@
#usage
### Requirements
- Latest rust nightly release
> rustup update
> rustup override set nightly
- Specific rustup components that can be installed with the following commands:
> rustup component add rust-src
> rustup component add llvm-tools-preview
### Building
run the following command (in the root directory of the project)
> ./run.sh
+41
View File
@@ -0,0 +1,41 @@
{
"nodes":[
{"id":"8391ed519ab6cb3d","type":"group","x":-920,"y":600,"width":1595,"height":360,"label":"Hardware Layer"},
{"id":"967decfbdbf166e2","type":"group","x":-920,"y":120,"width":1595,"height":320,"label":"Kernel Layer (kernelspace)"},
{"id":"efd3cf75db396678","type":"group","x":-920,"y":-280,"width":1595,"height":240,"label":"System Layer (userspace)"},
{"id":"d577e6c4fa3d7479","type":"group","x":-920,"y":-600,"width":1595,"height":140,"label":"User Layer (userspace)"},
{"id":"549099b4188ff073","type":"text","text":"Interrupt handling & initialisation","x":-204,"y":320,"width":250,"height":60,"color":"2"},
{"id":"d0c73e9e86edba62","type":"text","text":"Kernel #kernel","x":-220,"y":260,"width":281,"height":140,"color":"2"},
{"id":"4fdc8ae730503ad0","type":"text","text":"libk #kernel","x":-880,"y":260,"width":280,"height":140,"color":"2"},
{"id":"4839a4f4f0980254","type":"text","text":"System Libraries #userspace","x":-220,"y":-240,"width":281,"height":160,"color":"3"},
{"id":"ba48ee2ccc891a62","type":"text","text":"Scheduler","x":-880,"y":160,"width":280,"height":60,"color":"2"},
{"id":"3d31fc276e0ba4a0","type":"text","text":"UI Shell #userspace","x":-880,"y":-560,"width":280,"height":60,"color":"4"},
{"id":"6b5e17c10589a7b6","type":"text","text":"Generic Application #userspace","x":-219,"y":-560,"width":281,"height":60,"color":"4"},
{"id":"0a47f47a2c7b3609","type":"text","text":"CPU #hardware","x":-880,"y":640,"width":280,"height":280,"color":"1"},
{"id":"a7002e4427f0540d","type":"text","text":"lib_io","x":-200,"y":-160,"width":122,"height":60,"color":"3"},
{"id":"e09d0bccffd19ed9","type":"text","text":"lib_ui","x":-78,"y":-160,"width":118,"height":60,"color":"3"},
{"id":"cbfeda8989b260d8","type":"text","text":"Userspace Drivers #userspace","x":360,"y":-240,"width":281,"height":160,"color":"3"},
{"id":"3acced35498279a0","type":"text","text":"Hardware #hardware\n- Display\n- Hard drives\n- Keyboard\n- Mouse","x":360,"y":640,"width":281,"height":280,"color":"1"},
{"id":"31548bb91b4977eb","type":"text","text":"Syscall API","x":-219,"y":160,"width":281,"height":60,"color":"2"},
{"id":"110b7bee2ba99819","type":"text","text":"Drivers & low level kernel libraries","x":-860,"y":320,"width":240,"height":60,"color":"2"}
],
"edges":[
{"id":"6a8d6b7540c2898d","fromNode":"3d31fc276e0ba4a0","fromSide":"right","toNode":"4839a4f4f0980254","toSide":"left","color":"3"},
{"id":"57aedf427a150ff1","fromNode":"6b5e17c10589a7b6","fromSide":"bottom","toNode":"4839a4f4f0980254","toSide":"top","color":"3","label":"Applications are statically \nlinked to system libraries"},
{"id":"d292a19f88032809","fromNode":"4839a4f4f0980254","fromSide":"bottom","toNode":"31548bb91b4977eb","toSide":"top","color":"2","label":"system libraries make syscalls"},
{"id":"8b437f1c08e1caf2","fromNode":"ba48ee2ccc891a62","fromSide":"top","toNode":"3d31fc276e0ba4a0","toSide":"bottom","color":"4","label":"Scheduler Starts\nUI in userspace"},
{"id":"3e2c6f6d5dcd7613","fromNode":"4839a4f4f0980254","fromSide":"right","toNode":"cbfeda8989b260d8","toSide":"left","color":"3"},
{"id":"88f6e95c288c933d","fromNode":"31548bb91b4977eb","fromSide":"top","toNode":"4839a4f4f0980254","toSide":"bottom","color":"2"},
{"id":"156c8a89b537e219","fromNode":"cbfeda8989b260d8","fromSide":"left","toNode":"4839a4f4f0980254","toSide":"right","color":"3","label":"some libs talk to\nhardware through\nuserspace drivers"},
{"id":"0d04c5b0f76a5d9b","fromNode":"cbfeda8989b260d8","fromSide":"bottom","toNode":"3acced35498279a0","toSide":"top","fromEnd":"arrow","color":"1","label":"Userspace drivers can talk\ndirectly to hardware so\nin the case of a driver crash\nthe kernel is unaffected"},
{"id":"e9f1afb45bf17b77","fromNode":"4fdc8ae730503ad0","fromSide":"bottom","toNode":"3acced35498279a0","toSide":"left","fromEnd":"arrow","color":"1","label":"Libk handles most hardware\ninteraction, keeping the core kernel codebase as minimal\nas possible"},
{"id":"99414d640e519c8c","fromNode":"4fdc8ae730503ad0","fromSide":"right","toNode":"d0c73e9e86edba62","toSide":"left","fromEnd":"arrow","color":"2","label":"kernel static linked to libk\nmost kernel functionality\nisfound in libk"},
{"id":"85cd865c914dd125","fromNode":"549099b4188ff073","fromSide":"top","toNode":"d0c73e9e86edba62","toSide":"top","color":"2"},
{"id":"e882694ec774c0d6","fromNode":"4fdc8ae730503ad0","fromSide":"top","toNode":"ba48ee2ccc891a62","toSide":"bottom","color":"2"},
{"id":"c689b667be8cdfab","fromNode":"d0c73e9e86edba62","fromSide":"top","toNode":"31548bb91b4977eb","toSide":"bottom","color":"2"},
{"id":"0c9da0cce11e4bd1","fromNode":"ba48ee2ccc891a62","fromSide":"bottom","toNode":"4fdc8ae730503ad0","toSide":"top","color":"2"},
{"id":"c45efb0ad498f8a8","fromNode":"ba48ee2ccc891a62","fromSide":"right","toNode":"6b5e17c10589a7b6","toSide":"left","color":"4","label":"Scheduler\nstarts applications"},
{"id":"aee2cd6ef9682f0f","fromNode":"31548bb91b4977eb","fromSide":"left","toNode":"ba48ee2ccc891a62","toSide":"right","color":"2","label":"system libs can talk to\nthe scheduler to manage\nprocesses"},
{"id":"b1b2b96d8ac2b490","fromNode":"4fdc8ae730503ad0","fromSide":"bottom","toNode":"0a47f47a2c7b3609","toSide":"top","fromEnd":"arrow","color":"1"}
]
}
+16
View File
@@ -0,0 +1,16 @@
- contains all proc macros for building the kernel
## Current Features
### font importer:
- a macro that takes a filename to a font file and turns it into a font.
```rust
pub struct Font {
width: usize,
height: usize,
length: u16,
data: [[u8; 16]; 512],
}
pub static FONT_CP850_8X16: Font =
Font::new(include_font!("./libk/resources/font/cp850-8x16.psf"));
```
-7
View File
@@ -1,7 +0,0 @@
[build]
target = "../customos"
target-dir = "./target"
[unstable]
build-std = ["core", "compiler_builtins"]
build-std-features = ["compiler-builtins-mem"]
+30 -3
View File
@@ -1,7 +1,34 @@
[package]
name = "foundryos_kernel"
version = "0.1.0"
edition = "2021"
name = "foundry_os"
edition.workspace = true
version.workspace = true
authors.workspace = true
[dependencies]
limine = "0.3.1"
x86_64 = "0.15.2"
spin = "0.9.8"
pic8259 = "0.11.0"
pc-keyboard = "0.8.0"
libm = { path = "../libm" }
crossbeam = { version = "0.8.4", default-features = false, features = [
"alloc",
"crossbeam-queue",
] }
futures-util = { version = "0.3.31", default-features = false, features = [
"alloc",
] }
linked_list_allocator = { version = "0.10.5", features = ["use_spin"] }
gimli = { version = "0.31.1", default-features = false, features = ["read"] }
elf = { version = "0.7.4", default-features = false, features = ["nightly"] }
[build-dependencies]
cc = "1.2.14"
[features]
default = []
[[bin]]
name = "kernel"
path = "src/main.rs"
test = false
+4 -5
View File
@@ -1,7 +1,6 @@
fn main() {
let arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap();
// Tell cargo to pass the linker script to the linker..
println!("cargo:rustc-link-arg=-Tlinker-{arch}.ld");
// ..and to re-run if it changes.
println!("cargo:rerun-if-changed=linker-{arch}.ld");
// Tell cargo to rerun if these files change
println!("cargo:rerun-if-changed=src");
println!("cargo:rerun-if-changed=linker.ld");
println!("cargo:rerun-if-changed=../config/limine.conf");
}
-3
View File
@@ -1,3 +0,0 @@
Signature: 8a477f597d28d172789f06886806bc55
# This file is a cache directory tag created by cargo.
# For information about cache directory tags see https://bford.info/cachedir/
@@ -1 +0,0 @@
{"rustc":5637828655848808591,"features":"","declared_features":"","target":0,"profile":0,"path":0,"deps":[[16831489395241507197,"build_script_build",false,2449660372133931133]],"local":[{"RerunIfChanged":{"output":"../customos/debug/build/compiler_builtins-7ff902764ead3159/output","paths":["build.rs","configure.rs"]}}],"rustflags":[],"config":0,"compile_kind":0}
@@ -1 +0,0 @@
This file has an mtime of when this was started.
@@ -1 +0,0 @@
{"$message_type":"diagnostic","message":"Error loading target specification: Could not find specification for target \"../customos\". Run `rustc --print target-list` for a list of built-in targets","code":null,"level":"error","spans":[],"children":[],"rendered":"\u001b[0m\u001b[1m\u001b[38;5;9merror\u001b[0m\u001b[0m\u001b[1m: Error loading target specification: Could not find specification for target \"../customos\". Run `rustc --print target-list` for a list of built-in targets\u001b[0m\n\n"}
@@ -1 +0,0 @@
{"rustc":5637828655848808591,"features":"","declared_features":"","target":0,"profile":0,"path":0,"deps":[[16831489395241507197,"build_script_build",false,15090704965883138604],[4566724075159146729,"build_script_build",false,12086878114467551400]],"local":[{"RerunIfChanged":{"output":"../customos/debug/build/foundryos_kernel-15da3dc5b1d972d1/output","paths":["linker-x86_64.ld"]}}],"rustflags":[],"config":0,"compile_kind":0}
@@ -1 +0,0 @@
This file has an mtime of when this was started.
@@ -1,151 +0,0 @@
cargo::rerun-if-changed=build.rs
cargo::rerun-if-changed=configure.rs
cargo::rustc-check-cfg=cfg(__ashldi3, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__ashlsi3, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__ashrdi3, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__ashrsi3, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__bswapsi2, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__bswapdi2, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__bswapti2, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__divdi3, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__divsi3, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__divmoddi4, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__divmodsi4, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__divmodsi4, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__divmodti4, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__lshrdi3, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__lshrsi3, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__moddi3, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__modsi3, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__muldi3, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__udivdi3, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__udivmoddi4, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__udivmodsi4, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__udivsi3, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__umoddi3, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__umodsi3, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_cas1_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_cas1_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_cas1_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_cas1_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_cas2_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_cas2_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_cas2_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_cas2_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_cas4_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_cas4_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_cas4_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_cas4_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_cas8_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_cas8_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_cas8_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_cas8_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_cas16_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_cas16_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_cas16_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_cas16_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldadd1_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldadd1_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldadd1_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldadd1_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldadd2_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldadd2_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldadd2_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldadd2_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldadd4_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldadd4_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldadd4_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldadd4_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldadd8_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldadd8_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldadd8_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldadd8_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldclr1_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldclr1_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldclr1_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldclr1_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldclr2_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldclr2_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldclr2_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldclr2_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldclr4_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldclr4_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldclr4_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldclr4_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldclr8_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldclr8_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldclr8_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldclr8_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldeor1_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldeor1_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldeor1_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldeor1_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldeor2_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldeor2_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldeor2_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldeor2_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldeor4_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldeor4_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldeor4_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldeor4_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldeor8_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldeor8_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldeor8_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldeor8_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldset1_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldset1_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldset1_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldset1_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldset2_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldset2_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldset2_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldset2_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldset4_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldset4_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldset4_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldset4_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldset8_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldset8_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldset8_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_ldset8_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_swp1_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_swp1_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_swp1_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_swp1_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_swp2_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_swp2_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_swp2_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_swp2_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_swp4_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_swp4_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_swp4_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_swp4_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_swp8_relax, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_swp8_acq, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_swp8_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(__aarch64_swp8_acq_rel, values("optimized-c"))
cargo::rustc-check-cfg=cfg(target_feature, values("vis3"))
cargo::rustc-check-cfg=cfg(feature, values("checked"))
cargo::rustc-check-cfg=cfg(assert_no_panic)
cargo::rustc-check-cfg=cfg(f16_enabled)
cargo::rustc-check-cfg=cfg(f128_enabled)
cargo::rustc-cfg=f16_enabled
cargo::rustc-cfg=f128_enabled
cargo:rustc-check-cfg=cfg(intrinsics_enabled)
cargo:rustc-check-cfg=cfg(arch_enabled)
cargo:rustc-check-cfg=cfg(optimizations_enabled)
cargo:rustc-check-cfg=cfg(feature, values("unstable-public-internals"))
cargo:rustc-cfg=intrinsics_enabled
cargo:rustc-cfg=arch_enabled
cargo:rustc-check-cfg=cfg(optimizations_enabled)
cargo:rustc-check-cfg=cfg(x86_no_sse)
cargo:rustc-env=CFG_CARGO_FEATURES=["compiler-builtins", "core", "default", "mem", "rustc-dep-of-std"]
cargo:rustc-env=CFG_OPT_LEVEL=0
cargo:rustc-env=CFG_TARGET_FEATURES=["x87"]
cargo:rustc-cfg=feature="unstable-intrinsics"
cargo:compiler-rt=/home/zxq5/.cargo/bin/registry/src/index.crates.io-1949cf8c6b5b557f/compiler_builtins-0.1.146/compiler-rt
cargo::rustc-check-cfg=cfg(feature, values("mem-unaligned"))
cargo:rustc-cfg=feature="mem-unaligned"
cargo::rustc-check-cfg=cfg(thumb)
cargo::rustc-check-cfg=cfg(thumb_1)
cargo::rustc-check-cfg=cfg(kernel_user_helpers)
@@ -1 +0,0 @@
/home/zxq5/Projects/OSDev/FoundryOS/kernel/./target/../customos/debug/build/compiler_builtins-7ff902764ead3159/out
@@ -1 +0,0 @@
This file has an mtime of when this was started.
@@ -1,2 +0,0 @@
cargo:rustc-link-arg=-Tlinker-x86_64.ld
cargo:rerun-if-changed=linker-x86_64.ld
@@ -1 +0,0 @@
/home/zxq5/Projects/OSDev/FoundryOS/kernel/./target/../customos/debug/build/foundryos_kernel-15da3dc5b1d972d1/out
@@ -9,9 +9,10 @@ ENTRY(kmain)
/* process. */
PHDRS
{
text PT_LOAD;
rodata PT_LOAD;
data PT_LOAD;
limine_requests PT_LOAD;
text PT_LOAD;
rodata PT_LOAD;
data PT_LOAD;
}
SECTIONS
@@ -22,6 +23,18 @@ SECTIONS
/* that is the beginning of the region. */
. = 0xffffffff80000000;
/* Define a section to contain the Limine requests and assign it to its own PHDR */
.limine_requests : {
KEEP(*(.limine_requests_start))
KEEP(*(.limine_requests))
KEEP(*(.limine_requests_end))
} :limine_requests
/* Move to the next memory page for .text */
. = ALIGN(CONSTANT(MAXPAGESIZE));
.text : {
*(.text .text.*)
} :text
@@ -38,12 +51,6 @@ SECTIONS
.data : {
*(.data .data.*)
/* Place the sections that contain the Limine requests as part of the .data */
/* output section. */
KEEP(*(.requests_start_marker))
KEEP(*(.requests))
KEEP(*(.requests_end_marker))
} :data
/* NOTE: .bss needs to be the last thing mapped to :data, otherwise lots of */
@@ -55,9 +62,8 @@ SECTIONS
*(COMMON)
} :data
/* Discard .note.* and .eh_frame* since they may cause issues on some hosts. */
/* Discard .note.* and ~~.eh_frame*~~ since they may cause issues on some hosts. */
/DISCARD/ : {
*(.eh_frame*)
*(.note .note.*)
}
}
Binary file not shown.
Binary file not shown.
-4
View File
@@ -1,4 +0,0 @@
[toolchain]
channel = "nightly"
targets = ["rust-src", "llvm-tools-preview", "rustfmt", "clippy"]
components = []
+1
View File
@@ -0,0 +1 @@
pub mod x86_64;
+100
View File
@@ -0,0 +1,100 @@
#![expect(unused)]
use core::arch::x86_64::__cpuid;
use crate::arch::x86_64::memory::mapping::PHYSICAL_MEMORY_OFFSET;
use crate::{debugln, serial_print, serial_println};
use x86_64::{
PhysAddr, VirtAddr,
structures::paging::{Mapper, Page, PageTableFlags, PhysFrame, Size4KiB},
};
use x86_64::structures::paging::Translate;
use crate::arch::x86_64::cpu::msr::*;
use crate::arch::x86_64::memory::{FRAME_ALLOCATOR, OFFSET_PAGE_TABLE};
const IA32_APIC_BASE_MSR: u32 = 0x1b;
const IA32_APIC_BASE_MSR_BSP: u64 = 0x100;
const IA32_APIC_BASE_MSR_ENABLE: u64 = 0x800;
const IA32_APIC_BASE_MSR_DISABLE: u64 = !IA32_APIC_BASE_MSR_ENABLE;
const CPUID_FEAT_EDX_APIC: u64 = 1 << 9; // the cpuid instruction will return this flag if it supports APIC
// const APIC_VIRTUAL_ADDRESS: Lazy<VirtAddr> = Lazy::new(|| {
// let apic_base = get_apic_base();
// let virt_addr = unsafe { phys_to_virt(apic_base) };
// virt_addr
// });
fn set_apic_base_enable(apic: PhysAddr) {
let rax = (apic.as_u64() & 0xfffff0000) | IA32_APIC_BASE_MSR_ENABLE;
debugln!("apic {:?}", PhysAddr::new(rax));
cpu_set_msr(IA32_APIC_BASE_MSR, rax);
}
#[allow(dead_code)]
fn set_apic_base_disable(apic: PhysAddr) {
let rax = (apic.as_u64() & 0xfffff0000) & IA32_APIC_BASE_MSR_DISABLE;
cpu_set_msr(IA32_APIC_BASE_MSR, rax);
}
fn get_apic_base() -> PhysAddr {
let mut value: u64 = 0;
cpu_get_msr(IA32_APIC_BASE_MSR, &mut value);
debugln!("apic base {:#x}", value);
PhysAddr::new(value & 0xfffff0000)
}
fn write_apic_register(apic_base: &PhysAddr, reg: u64, value: u32) {
let apic_base = apic_base.as_u64();
let reg_addr = (apic_base & 0xFFFFF0000) + reg;
let virt_addr = unsafe { phys_to_virt(PhysAddr::new(reg_addr)) };
let phys_check = OFFSET_PAGE_TABLE.get().unwrap().lock().translate(virt_addr);
debugln!("{:?}", phys_check);
unsafe { *(virt_addr.as_u64() as *mut u32) = value };
}
fn read_apic_register(apic_base: &PhysAddr, reg: u64) -> u32 {
let apic_base = apic_base.as_u64();
let reg_addr = (apic_base & 0xFFFFF0000) + reg;
let virt_addr = unsafe { phys_to_virt(PhysAddr::new(reg_addr)) };
unsafe { *(virt_addr.as_u64() as *const u32) }
}
pub fn check_apic() -> bool {
let res = unsafe { __cpuid(1) };
res.edx as u64 & CPUID_FEAT_EDX_APIC != 0
}
#[inline(always)]
unsafe fn phys_to_virt(phys: PhysAddr) -> VirtAddr {
let phys = phys.as_u64();
phys.checked_add(*PHYSICAL_MEMORY_OFFSET)
.map_or_else(|| panic!(" overflow"), VirtAddr::new)
}
pub fn enable_apic() {
let apic_base_physical_addr = get_apic_base();
set_apic_base_enable(apic_base_physical_addr);
debugln!("{:?}", apic_base_physical_addr);
enable_timer();
write_apic_register(
&apic_base_physical_addr,
0xF0,
read_apic_register(&apic_base_physical_addr, 0xF0) | 0x1FF
);
}
pub fn enable_timer() {
let apic_base = get_apic_base();
let current = read_apic_register(&apic_base, 0x320);
write_apic_register(&get_apic_base(), 0x320, (current & 0xFFFFFF00) | 0x20);
}
+3
View File
@@ -0,0 +1,3 @@
pub mod apic;
mod msr;
pub mod pic;
+25
View File
@@ -0,0 +1,25 @@
#![expect(unused)]
use core::arch::x86_64::__cpuid;
use spin::Lazy;
use x86_64::registers::model_specific::Msr;
const CPUID_FLAG_MSR: u32 = 1 << 5;
static EDX: Lazy<u32> = Lazy::new(|| unsafe { __cpuid(1).edx });
pub fn cpu_has_msr() -> bool {
*EDX & CPUID_FLAG_MSR != 0
}
pub fn cpu_get_msr(msr: u32, value: &mut u64) {
let msr = Msr::new(msr);
unsafe {
*value = msr.read();
}
}
pub fn cpu_set_msr(msr: u32, value: u64) {
let mut msr = Msr::new(msr);
unsafe {
msr.write(value);
}
}
+162
View File
@@ -0,0 +1,162 @@
#![allow(clippy::missing_safety_doc)]
use x86_64::instructions::port::Port;
const CMD_INIT: u8 = 0x11;
const CMD_END_OF_INT: u8 = 0x20;
const MODE_8086: u8 = 0x01;
struct Pic {
offset: u8,
data: Port<u8>,
command: Port<u8>,
}
impl Pic {
/// Are we in charge of handling the specified interrupt?
/// (Each PIC handles 8 interrupts.)
const fn handles_interrupt(&self, interrupt_id: u8) -> bool {
self.offset <= interrupt_id && interrupt_id < self.offset + 8
}
/// Notify us that an interrupt has been handled and that we're ready
/// for more.
unsafe fn end_of_interrupt(&mut self) {
unsafe { self.command.write(CMD_END_OF_INT) };
}
/// Reads the interrupt mask of this PIC.
unsafe fn read_mask(&mut self) -> u8 {
unsafe { self.data.read() }
}
/// Writes the interrupt mask of this PIC.
unsafe fn write_mask(&mut self, mask: u8) {
unsafe { self.data.write(mask) }
}
}
/// A pair of chained PICs. This is the standard setup on x86.
pub struct ChainedPics {
pics: [Pic; 2],
}
impl ChainedPics {
/// Construct a new `ChainedPics` given the base offsets of the two PICs.
///
/// # Safety
///
/// This function is unsafe because it requires you to pass valid offsets.
/// If you pass offsets that are out of range, or if you pass offsets that
/// conflict with one another, the resulting object will be useless and
/// will likely cause problems for your system.
pub const unsafe fn new(offset1: u8, offset2: u8) -> Self {
Self {
pics: [
Pic {
offset: offset1,
command: Port::new(0x20),
data: Port::new(0x21),
},
Pic {
offset: offset2,
command: Port::new(0xA0),
data: Port::new(0xA1),
},
],
}
}
/// .
///
/// # Safety
///
/// .
pub const unsafe fn new_contiguous(primary_offset: u8) -> Self {
unsafe { Self::new(primary_offset, primary_offset + 8) }
}
/// Returns a new instance of [`ChainedPics`].
///
/// # Safety
///
/// this is unsafe because it required writing to CPU I/O ports.
pub unsafe fn initialize(&mut self) {
unsafe {
let mut wait_port: Port<u8> = Port::new(0x80);
let mut wait = || wait_port.write(0);
// Save our original interrupt masks, because I'm too lazy to
// figure out reasonable values. We'll restore these when we're
// done.
let saved_masks = self.read_masks();
// Tell each PIC that we're going to send it a three-byte
// initialization sequence on its data port.
self.pics[0].command.write(CMD_INIT);
wait();
self.pics[1].command.write(CMD_INIT);
wait();
// Byte 1: Set up our base offsets.
self.pics[0].data.write(self.pics[0].offset);
wait();
self.pics[1].data.write(self.pics[1].offset);
wait();
// Byte 2: Configure chaining between PIC1 and PIC2.
self.pics[0].data.write(4);
wait();
self.pics[1].data.write(2);
wait();
// Byte 3: Set our mode.
self.pics[0].data.write(MODE_8086);
wait();
self.pics[1].data.write(MODE_8086);
wait();
// Restore our saved masks.
self.write_masks(saved_masks[0], saved_masks[1])
}
}
/// Reads the interrupt masks of both PICs.
pub unsafe fn read_masks(&mut self) -> [u8; 2] {
unsafe { [self.pics[0].read_mask(), self.pics[1].read_mask()] }
}
/// Writes the interrupt masks of both PICs.
pub unsafe fn write_masks(&mut self, mask1: u8, mask2: u8) {
unsafe {
self.pics[0].write_mask(mask1);
self.pics[1].write_mask(mask2);
}
}
/// Disables both PICs by masking all interrupts.
pub unsafe fn disable(&mut self) {
unsafe { self.write_masks(u8::MAX, u8::MAX) }
}
/// Do we handle this interrupt?
pub fn handles_interrupt(&self, interrupt_id: u8) -> bool {
self.pics.iter().any(|p| p.handles_interrupt(interrupt_id))
}
/// Figure out which (if any) PICs in our chain need to know about this
/// interrupt. This is tricky, because all interrupts from `pics[1]`
/// get chained through `pics[0]`.
pub unsafe fn notify_end_of_interrupt(&mut self, interrupt_id: u8) {
if self.handles_interrupt(interrupt_id) {
if self.pics[1].handles_interrupt(interrupt_id) {
unsafe {
self.pics[1].end_of_interrupt();
}
}
unsafe {
self.pics[0].end_of_interrupt();
}
}
}
}
+248
View File
@@ -0,0 +1,248 @@
use core::fmt;
use spin::{Lazy, Mutex};
use x86_64::instructions::interrupts;
use crate::arch::x86_64::drivers::framebuffer::{
colour::Colour, display::FRAMEBUFFER_WRITER,
};
use crate::resources::font::{FONT_SPLEEN_8X16, Font};
static FONT_WIDTH: u32 = 8;
static FONT_HEIGHT: u32 = 16;
pub static WRITER: Lazy<Mutex<Writer>> =
Lazy::new(|| Mutex::new(Writer::new()));
pub fn screensize_chars() -> (u32, u32) {
let writer = WRITER.lock();
(writer.screen_width, writer.screen_height)
}
pub struct Writer {
font: &'static Font,
/// Measured in chars not pixels.
screen_width: u32,
/// Measured in chars not pixels.
screen_height: u32,
/// 16 pixels tall.
text_line: u32,
/// 8 pixels wide.
text_col: u32,
fg_color: Colour,
bg_color: Colour,
}
impl Default for Writer {
fn default() -> Self {
Self::new()
}
}
impl Writer {
pub fn new() -> Self {
FRAMEBUFFER_WRITER.lock().as_mut().map_or_else(
|| {
panic!("Framebuffer writer not initialized.");
},
|writer| Self {
font: &FONT_SPLEEN_8X16,
screen_width: writer.width() / 8,
screen_height: writer.height() / 16,
text_line: 0,
text_col: 0,
fg_color: Colour::White,
bg_color: Colour::Black,
},
)
}
pub const fn set_font(&mut self, font: &'static Font) {
self.font = font;
}
/// This is sent when the user types a backspace.
const BACKSPACE: u8 = 8;
pub fn write_glyph(&mut self, c: u8) {
if c == b'\n' {
self.newline();
return;
} else if c == Self::BACKSPACE {
self.backspace();
return;
}
// Get the character data from the font array. -- each byte is a row of
// pixels
let data: &[u8] = self.font.glyph_for(c as u16);
if let Some(writer) = FRAMEBUFFER_WRITER.lock().as_mut() {
for (row, line) in data.iter().enumerate().take(16) {
for col in 0..8 {
let pixel_x: u32 = self.text_col * FONT_WIDTH + col;
let pixel_y: u32 =
self.text_line * FONT_HEIGHT + row as u32;
if line & (0x80 >> col) != 0 {
// Write the foreground color
writer.write_pixel(
pixel_x as usize,
pixel_y as usize,
self.fg_color,
);
} else {
// Write the background color
writer.write_pixel(
pixel_x as usize,
pixel_y as usize,
self.bg_color,
);
}
}
}
}
// Go to next position
if self.text_col + 1 >= self.screen_width {
self.newline();
} else {
self.text_col += 1;
}
}
pub const fn dimensions(&self) -> (u32, u32) {
(self.screen_width, self.screen_height)
}
pub const fn next_char(&mut self) {
self.text_col += 1;
}
pub const fn newline(&mut self) {
self.text_col = 0;
if self.text_line + 1 >= self.screen_height {
self.text_line = 0;
} else {
self.text_line += 1;
}
}
/// Handles the backspace character. TODO: Implement VT-100 style terminal
/// control codes alongside a shell. Not simple.
pub fn backspace(&mut self) {
if self.text_col > 0 {
self.text_col -= 1;
// Blank out the previous char.
self.write_glyph(b' ');
self.text_col -= 1;
}
}
pub fn write_string(&mut self, s: &str) {
for c in s.chars() {
self.write_glyph(c as u8);
}
}
pub const fn set_colour(&mut self, fg: Colour, bg: Colour) {
self.fg_color = fg;
self.bg_color = bg;
}
pub const fn reset_colour(&mut self) {
self.fg_color = Colour::White;
self.bg_color = Colour::Black;
}
}
impl fmt::Write for Writer {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
self.write_string(s);
Ok(())
}
}
fn write(args: fmt::Arguments, fg: Colour, bg: Colour) {
use core::fmt::Write;
interrupts::without_interrupts(|| {
let mut writer = WRITER.lock();
writer.set_colour(fg, bg);
writer.write_fmt(args).unwrap();
writer.reset_colour();
});
}
pub fn _print(args: fmt::Arguments) {
interrupts::without_interrupts(|| {
write(args, Colour::White, Colour::Black);
})
}
pub fn _print_err(args: fmt::Arguments) {
interrupts::without_interrupts(|| {
write(args, Colour::Red, Colour::Black);
})
}
pub fn _print_log(args: fmt::Arguments) {
interrupts::without_interrupts(|| {
write(args, Colour::Yellow, Colour::Black);
})
}
pub fn clear_screen() {
interrupts::without_interrupts(|| {
let mut writer = WRITER.lock();
writer.text_line = 0;
writer.text_col = 0;
if let Some(writer) = FRAMEBUFFER_WRITER.lock().as_mut() {
writer.clear();
}
});
}
pub fn reset_cursor() {
interrupts::without_interrupts(|| {
let mut writer = WRITER.lock();
writer.text_line = 0;
writer.text_col = 0;
});
}
#[macro_export]
macro_rules! println_log {
() => ($crate::print_log!("\n"));
($($arg:tt)*) => ($crate::print_log!("{}\n", format_args!($($arg)*)));
}
#[macro_export]
macro_rules! print_log {
($($arg:tt)*) => ($crate::prelude::_print_log(format_args!($($arg)*)));
}
#[macro_export]
macro_rules! println {
() => ($crate::print!("\n"));
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
}
#[macro_export]
macro_rules! print {
($($arg:tt)*) => ($crate::prelude::_print(format_args!($($arg)*)));
}
#[macro_export]
macro_rules! eprintln {
() => ($crate::printerr!("\n"));
($($arg:tt)*) => ($crate::eprint!("{}\n", format_args!($($arg)*)));
}
#[macro_export]
macro_rules! eprint {
($($arg:tt)*) => ($crate::prelude::_print_err(format_args!($($arg)*)));
}
@@ -0,0 +1,61 @@
#[repr(u32)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Colour {
ARGB(u8, u8, u8, u8),
RGB(u8, u8, u8),
HexARGB(u32),
Black = 0x000000FF,
Blue = 0x0000FFFF,
Green = 0x00FF00FF,
Cyan = 0x00FFFFFF,
Red = 0xFF0000FF,
Magenta = 0xFF00FFFF,
Yellow = 0xFFFF00FF,
White = 0xFFFFFFFF,
}
#[allow(clippy::use_self)]
impl From<Colour> for u32 {
fn from(val: Colour) -> Self {
match val {
Colour::ARGB(a, r, g, b) => {
(a as u32) << 24
| (r as u32) << 16
| (g as u32) << 8
| (b as u32)
}
Colour::RGB(r, g, b) => {
((r as u32) << 16) | (g as u32) << 8 | (b as u32)
}
Colour::HexARGB(hex) => hex,
Colour::Black => 0xFF000000,
Colour::Blue => 0xFF0000FF,
Colour::Green => 0xFF00FF00,
Colour::Cyan => 0xFF00FFFF,
Colour::Red => 0xFFFF0000,
Colour::Magenta => 0xFFFF00FF,
Colour::Yellow => 0xFFFFFF00,
Colour::White => 0xFFFFFFFF,
}
}
}
impl core::fmt::Display for Colour {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::ARGB(r, g, b, a) => {
write!(f, "RGBA(#{:x}{:x}{:x}{:x})", r, g, b, a)
}
Self::RGB(r, g, b) => write!(f, "RGB(#{:x}{:x}{:x})", r, g, b),
Self::HexARGB(hex) => write!(f, "Hex(#{:x})", hex),
Self::Black => write!(f, "Black"),
Self::Blue => write!(f, "Blue"),
Self::Green => write!(f, "Green"),
Self::Cyan => write!(f, "Cyan"),
Self::Red => write!(f, "Red"),
Self::Magenta => write!(f, "Magenta"),
Self::Yellow => write!(f, "Yellow"),
Self::White => write!(f, "White"),
}
}
}
@@ -0,0 +1,107 @@
use limine::request::FramebufferRequest;
#[used]
#[unsafe(link_section = ".requests")]
static FRAMEBUFFER_REQUEST: FramebufferRequest = FramebufferRequest::new();
use super::colour::Colour;
use core::panic;
use limine::framebuffer::Framebuffer;
use spin::{Lazy, Mutex};
pub static FRAMEBUFFER_WRITER: Lazy<Mutex<Option<FramebufferWriter>>> =
Lazy::new(|| {
Mutex::new(FRAMEBUFFER_REQUEST.get_response().map_or_else(
|| {
panic!("Framebuffer request failed");
},
|framebuffer_response| {
let framebuffer =
framebuffer_response.framebuffers().next().unwrap();
Some(FramebufferWriter::new(framebuffer))
},
))
});
/// The updated writer stores necessary fields from the [Framebuffer].
/// This ensures that the contained types are Send, as Framebuffer was
/// not marked as Send.
///
/// It also avoids the requirement for lifetimes.
///
/// Note this does not implement Writer as these functions only handle drawing
/// pixels.
pub struct FramebufferWriter {
pitch: u64,
bpp: u16,
addr: *mut u8,
width: u64,
height: u64,
}
unsafe impl Send for FramebufferWriter {}
unsafe impl Sync for FramebufferWriter {}
impl FramebufferWriter {
pub fn new(framebuffer: Framebuffer) -> Self {
Self {
pitch: framebuffer.pitch(),
bpp: framebuffer.bpp(),
addr: framebuffer.addr(),
width: framebuffer.width(),
height: framebuffer.height(),
}
}
pub fn write_pixel(&self, x: usize, y: usize, color: Colour) {
let pitch = self.pitch as usize;
let bpp = (self.bpp / 8) as usize;
let pixel_offset = y * pitch + x * bpp;
unsafe {
*(self.addr.add(pixel_offset) as *mut u32) = color.into();
}
}
pub fn render_frame(&self, buffer: &[&[Colour]]) {
// TODO: this should return errors
for (y, &row) in buffer.iter().enumerate() {
if y >= self.height() as usize {
break;
}
for (x, pixel) in row.iter().enumerate() {
if x >= self.width() as usize {
break;
}
self.write_pixel(x, y, *pixel);
}
}
}
pub const fn width(&self) -> u32 {
self.width as u32
}
pub const fn height(&self) -> u32 {
self.height as u32
}
pub fn clear(&self) {
let width = self.width as usize;
let height = self.height as usize;
for y in 0..height {
for x in 0..width {
self.write_pixel(x, y, Colour::Black);
}
}
}
}
pub fn screensize_px() -> (u32, u32) {
FRAMEBUFFER_WRITER
.lock()
.as_mut()
.map_or_else(|| (0, 0), |writer| (writer.width(), writer.height()))
}
@@ -0,0 +1,2 @@
pub mod colour;
pub mod display;
+209
View File
@@ -0,0 +1,209 @@
use core::{
pin::Pin,
task::{Context, Poll},
};
use crate::println;
use crossbeam::queue::ArrayQueue;
use futures_util::{Stream, StreamExt, task::AtomicWaker};
use pc_keyboard::{
DecodedKey, HandleControl, KeyCode, Keyboard, ScancodeSet1,
layouts::Uk105Key,
};
use spin::{Lazy, Mutex, Once};
static KBD_QUEUE: Once<ArrayQueue<u8>> = Once::new();
static WAKER: AtomicWaker = AtomicWaker::new();
pub static KEYBOARD: Lazy<Mutex<Keyboard<Uk105Key, ScancodeSet1>>> =
Lazy::new(|| {
Mutex::new(Keyboard::new(
ScancodeSet1::new(),
// TODO: Expose an API to change the default KB layout.
Uk105Key,
HandleControl::Ignore,
))
});
pub static SCANCODE_STREAM: Lazy<Mutex<ScancodeStream>> =
Lazy::new(|| Mutex::new(ScancodeStream::new()));
pub fn add_scancode(scancode: u8) {
if let Some(queue) = KBD_QUEUE.get() {
if queue.push(scancode).is_err() {
println!("WARNING: scancode queue full; dropping keyboard input");
} else {
WAKER.wake();
}
} else {
println!("WARNING: scancode queue not initialized");
}
}
pub struct ScancodeStream {
_private: (),
}
impl ScancodeStream {
pub fn new() -> Self {
KBD_QUEUE.call_once(|| ArrayQueue::new(5));
Self { _private: () }
}
pub fn try_next(&mut self) -> Option<u8> {
KBD_QUEUE.get().and_then(|queue| queue.pop())
}
}
impl Default for ScancodeStream {
fn default() -> Self {
Self::new()
}
}
impl Stream for ScancodeStream {
type Item = u8;
fn poll_next(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Self::Item>> {
let queue = KBD_QUEUE.get().unwrap();
if let Some(scancode) = queue.pop() {
return Poll::Ready(Some(scancode));
}
WAKER.register(cx.waker());
WAKER.register(cx.waker());
queue.pop().map_or(Poll::Pending, |scancode| {
WAKER.take();
Poll::Ready(Some(scancode))
})
}
}
pub async fn get_keystroke_async() -> KeyStroke {
loop {
if let Some(scancode) = SCANCODE_STREAM.lock().next().await {
if let Ok(keystroke) = KeyStroke::try_from(scancode) {
return keystroke;
}
}
}
}
pub fn get_keystroke_optional() -> Option<KeyStroke> {
if let Some(scancode) = SCANCODE_STREAM.lock().try_next() {
if let Ok(keystroke) = KeyStroke::try_from(scancode) {
return Some(keystroke);
}
}
None
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyStroke {
Char(char),
Ctrl,
RCtrl,
Alt,
RAlt,
Shift,
RShift,
Meta,
RMeta,
Backspace,
Left,
Right,
Up,
Down,
None,
Enter,
Escape,
Del,
}
impl KeyStroke {
pub const fn from_keycode(key: KeyCode) -> Self {
match key {
KeyCode::LControl => Self::Ctrl,
KeyCode::RControl => Self::RCtrl,
KeyCode::LAlt => Self::Alt,
KeyCode::RAlt2 => Self::RAlt,
KeyCode::LShift => Self::Shift,
KeyCode::RShift => Self::RShift,
KeyCode::LWin => Self::Meta,
KeyCode::RWin => Self::RMeta,
KeyCode::Backspace => Self::Backspace,
KeyCode::ArrowLeft => Self::Left,
KeyCode::ArrowRight => Self::Right,
KeyCode::ArrowUp => Self::Up,
KeyCode::ArrowDown => Self::Down,
KeyCode::Return => Self::Enter,
KeyCode::Escape => Self::Escape,
KeyCode::Delete => Self::Del,
_ => Self::None,
}
}
}
impl TryFrom<u8> for KeyStroke {
type Error = ();
fn try_from(code: u8) -> Result<Self, Self::Error> {
let mut keyboard = KEYBOARD.lock();
let key = match keyboard.add_byte(code) {
Ok(Some(event)) => match keyboard.process_keyevent(event) {
Some(key) => key,
_ => return Err(()),
},
_ => return Err(()),
};
match key {
DecodedKey::Unicode(ch) => Ok(Self::Char(ch)),
DecodedKey::RawKey(key) => match Self::from_keycode(key) {
Self::None => Err(()),
key => Ok(key),
},
}
}
}
impl TryInto<char> for KeyStroke {
type Error = ();
fn try_into(self) -> Result<char, Self::Error> {
match self {
Self::Char(c) => Ok(c),
_ => Err(()),
}
}
}
impl core::fmt::Display for KeyStroke {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Char(c) => write!(f, "{}", c),
Self::Ctrl => write!(f, "CTRL"),
Self::RCtrl => write!(f, "RCtrl"),
Self::Alt => write!(f, "ALT"),
Self::RAlt => write!(f, "RAlt"),
Self::Shift => write!(f, "SHIFT"),
Self::RShift => write!(f, "RShift"),
Self::Meta => write!(f, "META"),
Self::RMeta => write!(f, "RMeta"),
Self::Backspace => write!(f, "BACKSPACE"),
Self::Left => write!(f, "LEFT"),
Self::Right => write!(f, "RIGHT"),
Self::Up => write!(f, "UP"),
Self::Down => write!(f, "DOWN"),
Self::Enter => write!(f, "ENTER"),
Self::Escape => write!(f, "ESCAPE"),
Self::None => write!(f, "NONE"),
Self::Del => write!(f, "DEL"),
}
}
}
+5
View File
@@ -0,0 +1,5 @@
pub mod ascii;
pub mod framebuffer;
pub mod keyboard;
pub mod port;
pub mod serial;
+29
View File
@@ -0,0 +1,29 @@
//! Functions for IO using ports.
use core::arch::asm;
#[inline]
pub fn inb(port: u16) -> u8 {
let value: u8;
unsafe {
asm!(
"in al, dx",
out("al") value,
in("dx") port,
options(nomem, nostack, preserves_flags)
);
}
value
}
#[inline]
pub fn outb(port: u16, value: u8) {
unsafe {
asm!(
"out dx, al",
in("dx") port,
in("al") value,
options(nomem, nostack, preserves_flags)
);
}
}
+155
View File
@@ -0,0 +1,155 @@
use core::{
fmt,
sync::atomic::{AtomicUsize, Ordering},
};
use spin::{Lazy, Mutex};
#[macro_export]
macro_rules! serial_print {
($($arg:tt)*) => ($crate::_serial_write(format_args!($($arg)*)));
}
#[macro_export]
macro_rules! serial_println {
() => ($crate::serial_print!("\n"));
($($arg:tt)*) => (serial_print!("{}\n", format_args!($($arg)*)));
}
use crate::arch::x86_64::drivers::port::{inb, outb};
use x86_64::instructions::interrupts;
pub fn _serial_write(args: fmt::Arguments) {
use core::fmt::Write;
interrupts::without_interrupts(|| {
if let Some(writer) = WRITER.lock().as_mut() {
writer.write_fmt(args).unwrap();
}
})
}
pub fn serial_read() -> &'static str {
serial_println!("getting value!");
interrupts::without_interrupts(|| {
if let Some(reader) = READER.lock().as_mut() {
serial_println!("stuff happnin.");
reader.read_str_to_buffer();
} else {
serial_println!("failed to get writer");
}
});
serial_println!("eee");
let i = BUFFER_LEN.load(Ordering::SeqCst);
unsafe {
if i != 0 {
core::str::from_utf8(&BUFFER[..i - 1]).unwrap()
} else {
serial_println!("empty string");
""
}
}
}
static PORT: u16 = 0x3f8;
static mut BUFFER: [u8; 256] = [0; 256];
static BUFFER_LEN: AtomicUsize = AtomicUsize::new(0);
static READER: Lazy<Mutex<Option<Reader>>> = Lazy::new(|| Mutex::new(None));
static WRITER: Lazy<Mutex<Option<Writer>>> = Lazy::new(|| Mutex::new(None));
struct Reader;
struct Writer;
impl fmt::Write for Writer {
fn write_str(&mut self, s: &str) -> fmt::Result {
for c in s.chars() {
self.write_byte(c as u8);
}
Ok(())
}
}
impl Writer {
unsafe fn write_success(&self) -> bool {
inb(PORT + 5) & 0x20 != 0
}
pub fn write_byte(&self, data: u8) {
unsafe {
while !self.write_success() {}
outb(PORT, data);
}
}
}
pub fn init() -> Result<(), &'static str> {
test()?;
if READER.lock().is_none() {
*READER.lock() = Some(Reader);
}
if WRITER.lock().is_none() {
*WRITER.lock() = Some(Writer);
}
Ok(())
}
pub fn test() -> Result<(), &'static str> {
outb(PORT + 1, 0x00); // Disable all interrupts
outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
outb(PORT, 0x03); // Set divisor to 3 (lo byte) 38400 baud
outb(PORT + 1, 0x00); // (hi byte)
outb(PORT + 3, 0x03); // 8 bits, no parity, one stop bit
outb(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-bytethreshold
outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip
outb(PORT, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte)
if inb(PORT) != 0xAE {
return Err("serial test failed");
}
outb(PORT + 4, 0x0F);
Ok(())
}
impl Reader {
pub fn read_str_to_buffer(&mut self) {
unsafe {
while !self.read_ready() {}
BUFFER_LEN.store(0, Ordering::SeqCst);
while BUFFER_LEN.load(Ordering::SeqCst) < 256 {
let c = self.read();
BUFFER[BUFFER_LEN.load(Ordering::SeqCst)] = c;
if c as char == '\r' {
break;
}
BUFFER_LEN.fetch_add(1, Ordering::SeqCst);
}
serial_println!("returning")
}
}
unsafe fn read_ready(&self) -> bool {
inb(PORT + 5) & 1 != 0
}
pub fn read(&self) -> u8 {
unsafe {
while !self.read_ready() {}
inb(PORT)
}
}
}
+69
View File
@@ -0,0 +1,69 @@
use x86_64::{
VirtAddr,
instructions::tables::load_tss,
registers::segmentation::{CS, DS, ES, SS, Segment},
structures::{
gdt::{Descriptor, GlobalDescriptorTable, SegmentSelector},
tss::TaskStateSegment,
},
};
use spin::Lazy;
pub const DOUBLE_FAULT_1ST_INDEX: u16 = 0;
static TSS: Lazy<TaskStateSegment> = Lazy::new(|| {
let mut tss = TaskStateSegment::new();
tss.interrupt_stack_table[DOUBLE_FAULT_1ST_INDEX as usize] = {
const STACK_SIZE: usize = 4096 * 8;
static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE];
let stack_start = VirtAddr::from_ptr(&raw const STACK);
stack_start + STACK_SIZE.try_into().unwrap()
};
tss
});
static GDT: Lazy<(GlobalDescriptorTable, Selectors)> = Lazy::new(|| {
let mut gdt = GlobalDescriptorTable::new();
let code_selector = gdt.append(Descriptor::kernel_code_segment());
let data_selector = gdt.append(Descriptor::kernel_data_segment());
let user_code_selector = gdt.append(Descriptor::user_code_segment());
let user_data_selector = gdt.append(Descriptor::user_data_segment());
let tss_selector = gdt.append(Descriptor::tss_segment(&TSS));
(
gdt,
Selectors {
code_selector,
data_selector,
_user_code_selector: user_code_selector,
_user_data_selector: user_data_selector,
tss_selector,
},
)
});
struct Selectors {
code_selector: SegmentSelector,
data_selector: SegmentSelector,
_user_code_selector: SegmentSelector,
_user_data_selector: SegmentSelector,
tss_selector: SegmentSelector,
}
pub fn init() {
GDT.0.load();
unsafe {
CS::set_reg(GDT.1.code_selector);
load_tss(GDT.1.tss_selector);
DS::set_reg(self::GDT.1.data_selector);
ES::set_reg(self::GDT.1.data_selector);
SS::set_reg(self::GDT.1.data_selector);
}
}
+156
View File
@@ -0,0 +1,156 @@
use crate::{debug, serial_print};
use pic8259::ChainedPics;
use x86_64::registers::control::Cr2;
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame, PageFaultErrorCode};
use x86_64::structures::paging::mapper::MapperFlushAll;
use x86_64::structures::paging::{FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB};
use super::gdt;
use crate::arch::x86_64::memory::{FRAME_ALLOCATOR, OFFSET_PAGE_TABLE};
use crate::{println_log, serial_println};
use spin::{Lazy, Mutex};
static IDT: Lazy<InterruptDescriptorTable> = Lazy::new(|| {
let mut idt = InterruptDescriptorTable::new();
idt.breakpoint.set_handler_fn(breakpoint_handler);
unsafe {
idt.double_fault
.set_handler_fn(double_fault_handler)
.set_stack_index(gdt::DOUBLE_FAULT_1ST_INDEX);
}
idt.general_protection_fault
.set_handler_fn(general_protection_fault_handler);
idt.page_fault.set_handler_fn(page_fault_handler);
idt[InterruptIndex::Timer.as_u8()].set_handler_fn(timer_interrupt_handler);
idt[InterruptIndex::Keyboard.as_u8()].set_handler_fn(keyboard_interrupt_handler);
idt
});
pub const PIC_1_OFFSET: u8 = 32;
pub const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8;
pub static PICS: Mutex<ChainedPics> =
Mutex::new(unsafe { ChainedPics::new(PIC_1_OFFSET, PIC_2_OFFSET) });
#[derive(Debug, Clone, Copy)]
#[repr(u8)]
pub enum InterruptIndex {
Timer = PIC_1_OFFSET,
Keyboard,
}
impl InterruptIndex {
const fn as_u8(self) -> u8 {
self as u8
}
fn _as_usize(self) -> usize {
usize::from(self.as_u8())
}
}
pub fn init_idt() {
IDT.load();
}
pub fn enable_pic() {
unsafe {
PICS.lock().initialize();
PICS.lock().write_masks(0xfc, 0xff);
}
}
pub fn disable_pic() {
unsafe {
PICS.lock().disable();
}
}
extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) {
serial_println!("Exception: Breakpoint\n{:#?}", stack_frame);
println_log!("Exception: Breakpoint\n{:#?}", stack_frame);
}
extern "x86-interrupt" fn general_protection_fault_handler(
stack_frame: InterruptStackFrame,
_error_code: u64,
) {
serial_println!("Exception: General Protection Fault\n{:#?}", stack_frame);
panic!("Exception: General Protection Fault\n{:#?}", stack_frame);
}
extern "x86-interrupt" fn double_fault_handler(
stack_frame: InterruptStackFrame,
_error_code: u64,
) -> ! {
serial_println!("Exception: Double Fault\n{:#?}", stack_frame);
panic!("Exception: Double Fault\n{:#?}", stack_frame);
}
extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStackFrame) {
use pc_keyboard::{HandleControl, Keyboard, ScancodeSet1, layouts};
// use pc_keyboard::DecodedKey;
use spin::Mutex;
use x86_64::instructions::port::Port;
static KEYBOARD: Lazy<Mutex<Keyboard<layouts::Uk105Key, ScancodeSet1>>> = Lazy::new(|| {
Mutex::new(Keyboard::new(
ScancodeSet1::new(),
layouts::Uk105Key,
HandleControl::Ignore,
))
});
let _keyboard = KEYBOARD.lock();
let mut port = Port::new(0x60);
let scancode: u8 = unsafe { port.read() };
crate::arch::x86_64::drivers::keyboard::add_scancode(scancode);
unsafe {
PICS.lock()
.notify_end_of_interrupt(InterruptIndex::Keyboard.as_u8());
}
}
extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: InterruptStackFrame) {
debug!("Timer Interrupt");
unsafe {
PICS.lock()
.notify_end_of_interrupt(InterruptIndex::Timer.as_u8());
}
}
extern "x86-interrupt" fn page_fault_handler(
_stack_frame: InterruptStackFrame,
_error_code: PageFaultErrorCode,
) {
serial_println!("Exception: Page Fault");
serial_println!("Accessed Address: {:?}", Cr2::read());
serial_println!("Error Code: {:?}", _error_code);
serial_println!("{:#?}", _stack_frame);
if let Some(frame_allocator) = FRAME_ALLOCATOR.get() {
let mut f = frame_allocator.lock();
let frame = f.allocate_frame().unwrap();
let flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE;
let page: Page<Size4KiB> =
Page::containing_address(Cr2::read().unwrap());
unsafe {
let mut mapper = OFFSET_PAGE_TABLE.get().unwrap().lock();
match mapper.map_to(page, frame, flags, &mut *f) {
Ok(_) => {}
Err(why) => panic!("failed to map page: {:?}", why),
}
}
MapperFlushAll::new().flush_all();
} else {
panic!("failed to get frame allocator");
}
}
@@ -0,0 +1,306 @@
use crate::{debugln, serial_print};
use core::alloc::{GlobalAlloc, Layout};
use core::ptr;
use spin::{Mutex, MutexGuard};
use x86_64::structures::paging::{Size4KiB, mapper::MapToError, PageTableFlags};
use x86_64::VirtAddr;
use crate::arch::x86_64::memory::{FRAME_ALLOCATOR, HEAP_SIZE, HEAP_VIRTUAL_SPACE};
use crate::arch::x86_64::memory::allocation::page_alloc::FoundryOSFrameAllocator;
use crate::arch::x86_64::memory::units::MemoryUnits;
use crate::serial_println;
/// We are currently using a linked list heap allocator which uses our underlying page allocator.
#[global_allocator]
/// This is now Rust's global allocator, so we can use stuff requiring heap allocations.
static ALLOCATOR: Locked<FoundryAllocator> = Locked::new(FoundryAllocator::new());
/// Initializes the heap.
///
/// This function must be called once, before any functions that
/// require a heap are called. It sets up the heap by allocating
/// the necessary frames from the page allocator. The heap is
/// then ready for use.
///
/// # Safety
///
/// This function must be called only once, and must be called before any types
/// are constructed that require dynamic memory allocation.
///
/// # Parameters
///
/// None
///
/// # Returns
///
/// Returns a `Result` containing a `MapToError` if the heap
/// could not be initialized, or `Ok` if the heap was
/// initialized successfully.
pub unsafe fn init_heap() -> Result<(), MapToError<Size4KiB>> {
unsafe {
// code to allocate frames is now done in the page fault interrupt handler!
ALLOCATOR.lock().init(HEAP_VIRTUAL_SPACE, HEAP_SIZE);
Ok(())
}
}
pub struct Locked<T> {
inner: Mutex<T>,
}
impl<T> Locked<T> {
pub const fn new(inner: T) -> Self {
Self {
inner: Mutex::new(inner),
}
}
pub fn lock(&self) -> MutexGuard<T> {
self.inner.lock()
}
}
const BLOCK_SIZES: &[usize] = &[8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096];
struct ListNode {
next: Option<&'static mut ListNode>,
}
pub struct FoundryAllocator {
list_heads: [Option<&'static mut ListNode>; BLOCK_SIZES.len()],
fallback: Locked<FoundryFallbackAllocator>,
}
impl Default for FoundryAllocator {
fn default() -> Self {
Self::new()
}
}
impl FoundryAllocator {
pub const fn new() -> Self {
const EMPTY: Option<&'static mut ListNode> = None;
Self {
list_heads: [EMPTY; BLOCK_SIZES.len()],
fallback: Locked::new(FoundryFallbackAllocator::new()),
}
}
/// Initializes the fallback allocator with the given heap start address and size.
///
/// # Safety
///
/// This function is unsafe because it does not check whether the given heap start address and size are valid.
pub unsafe fn init(&mut self, heap_start: usize, heap_size: usize) {
debugln!(" => Start: {:?}", VirtAddr::new(heap_start as u64));
debugln!(" => Size: {}", MemoryUnits::from_bytes(heap_size));
unsafe {
self.fallback.lock().init(heap_start, heap_size);
}
}
unsafe fn fallback_alloc(&mut self, layout: Layout) -> *mut u8 {
unsafe { self.fallback.alloc(layout) }
}
fn block_size(&self, layout: &Layout) -> Option<usize> {
let required_block_size = layout.size().max(layout.align());
BLOCK_SIZES.iter().position(|&s| s >= required_block_size)
}
}
unsafe impl GlobalAlloc for Locked<FoundryAllocator> {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let mut allocator = self.lock();
match allocator.block_size(&layout) {
Some(index) => {
match allocator.list_heads[index].take() {
Some(node) => {
allocator.list_heads[index] = node.next.take();
let ptr = node as *mut ListNode as *mut u8;
ensure_mapped(VirtAddr::new(ptr as u64));
ptr
}
None => {
// no block exists in list => allocate new block
let block_size = BLOCK_SIZES[index];
// only works if all block sizes are a power of 2
let block_align = block_size;
let layout = Layout::from_size_align(block_size, block_align).unwrap();
unsafe { allocator.fallback_alloc(layout) }
}
}
}
None => unsafe { allocator.fallback_alloc(layout) },
}
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
let mut allocator = self.lock();
match allocator.block_size(&layout) {
Some(idx) => {
let new_node = ListNode {
next: allocator.list_heads[idx].take(),
};
let new_ptr = ptr as *mut ListNode;
unsafe { new_ptr.write(new_node) };
allocator.list_heads[idx] = Some(unsafe { &mut *new_ptr });
}
None => {
unsafe { allocator.fallback.dealloc(ptr, layout) };
}
}
}
}
struct FallbackListNode {
size: usize,
next: Option<&'static mut FallbackListNode>,
}
impl FallbackListNode {
const fn new(size: usize) -> Self {
Self { size, next: None }
}
fn start_addr(&self) -> usize {
self as *const Self as usize
}
fn end_addr(&self) -> usize {
self.start_addr() + self.size
}
}
pub struct FoundryFallbackAllocator {
head: FallbackListNode,
}
impl Default for FoundryFallbackAllocator {
fn default() -> Self {
Self::new()
}
}
impl FoundryFallbackAllocator {
pub const fn new() -> Self {
Self {
head: FallbackListNode::new(0),
}
}
/// Initializes the allocator with a given heap start address and size.
///
/// # Safety
///
/// This function is unsafe because it does not check whether the given heap start address and size are valid.
pub unsafe fn init(&mut self, heap_start: usize, heap_size: usize) {
unsafe { self.add_region(heap_start, heap_size) };
}
unsafe fn add_region(&mut self, addr: usize, size: usize) {
unsafe {
let mut node = FallbackListNode::new(size);
node.next = self.head.next.take();
let virt_addr = VirtAddr::new(addr as u64);
ensure_mapped(virt_addr);
let node_ptr = addr as *mut FallbackListNode;
node_ptr.write(node);
self.head.next = Some(&mut *node_ptr);
}
}
fn find_region(
&mut self,
size: usize,
align: usize,
) -> Option<(&'static mut FallbackListNode, usize)> {
let mut current = &mut self.head;
// look for a large enough memory region in linked list
while let Some(ref mut region) = current.next {
if let Ok(alloc_start) = Self::alloc_from_region(region, size, align) {
// region suitable for allocation -> remove node from list
let next = region.next.take();
let ret = Some((current.next.take().unwrap(), alloc_start));
current.next = next;
return ret;
} else {
// region not suitable -> continue with next region
current = current.next.as_mut().unwrap();
}
}
None
}
const fn align_up(addr: usize, align: usize) -> usize {
(addr + align - 1) & !(align - 1)
}
fn size_align(layout: Layout) -> (usize, usize) {
let layout = layout
.align_to(align_of::<ListNode>())
.expect("adjusting alignment failed")
.pad_to_align();
let size = layout.size().max(size_of::<ListNode>());
(size, layout.align())
}
fn alloc_from_region(
region: &FallbackListNode,
size: usize,
align: usize,
) -> Result<usize, ()> {
let alloc_start = Self::align_up(region.start_addr(), align);
let alloc_end = alloc_start.checked_add(size).ok_or(())?;
if alloc_end > region.end_addr() {
return Err(());
}
let excess_size = region.end_addr() - alloc_end;
if excess_size > 0 && excess_size < size_of::<ListNode>() {
return Err(());
}
Ok(alloc_start)
}
}
unsafe impl GlobalAlloc for Locked<FoundryFallbackAllocator> {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let mut allocator = self.lock();
// perform layout adjustments
let (size, align) = FoundryFallbackAllocator::size_align(layout);
if let Some((region, alloc_start)) = allocator.find_region(size, align) {
let alloc_end = alloc_start.checked_add(size).expect("overflow");
let excess_size = region.end_addr() - alloc_end;
if excess_size > 0 {
unsafe { allocator.add_region(alloc_end, excess_size) };
}
let ptr = alloc_start as *mut u8;
ensure_mapped(VirtAddr::new(ptr as u64));
ptr
} else {
ptr::null_mut()
}
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
let mut allocator = self.lock();
// perform layout adjustments
let (size, _) = FoundryFallbackAllocator::size_align(layout);
unsafe { allocator.add_region(ptr as usize, size) }
}
}
fn ensure_mapped(virt_addr: VirtAddr) {
if ! FoundryOSFrameAllocator::is_mapped(virt_addr) {
let mut foundry_alloc = FRAME_ALLOCATOR.get().unwrap().lock();
foundry_alloc.allocate_page(virt_addr, PageTableFlags::PRESENT | PageTableFlags::WRITABLE).unwrap();
}
}
@@ -0,0 +1,3 @@
pub mod heap_alloc;
pub(crate) mod page_alloc;
pub mod stack_alloc;
@@ -0,0 +1,89 @@
use x86_64::structures::paging::{FrameAllocator, Mapper, Page, PageTableFlags, PhysFrame, Size4KiB, Translate};
use limine::response::MemoryMapResponse;
use spin::Mutex;
use limine::memory_map::EntryType;
use x86_64::{PhysAddr, VirtAddr};
use x86_64::structures::paging::mapper::{MapToError, TranslateResult};
use crate::arch::x86_64::memory::{FRAME_ALLOCATOR, OFFSET_PAGE_TABLE};
pub struct FoundryOSFrameAllocator {
memory_map: &'static MemoryMapResponse,
next: usize,
}
impl FoundryOSFrameAllocator {
/// Creates a new `FoundryOSFrameAllocator` from a memory map.
///
/// This function takes a reference to a `MemoryMapResponse` and initializes
/// a `FoundryOSFrameAllocator` with it. The `next` field is set to 0,
/// indicating that the first frame to be allocated is the first frame
/// in the memory map.
pub fn init(memory_map: &'static MemoryMapResponse) {
FRAME_ALLOCATOR.call_once(|| {
Mutex::new(Self {
memory_map,
next: 0,
})
});
}
pub fn count_usable_frames(&self) -> u64 {
self.usable_frames().count() as u64
}
pub fn available_memory(&self) -> u64 {
self.memory_map
.entries()
.iter()
.map(|region| region.base..region.base + region.length)
.flat_map(|r| r.step_by(4096)).count() as u64 * 4096
}
/// An iterator over all usable frames in the memory map.
///
/// Yields one `PhysFrame` for each available 4KiB frame in the memory map.
///
/// This function is used to allocate frames for the page map.
fn usable_frames(&self) -> impl Iterator<Item = PhysFrame> + use<> {
let regions = self.memory_map.entries().iter();
let usable_regions = regions.filter(|region| region.entry_type == EntryType::USABLE);
let addr_ranges = usable_regions.map(|region| region.base..region.base + region.length);
let frame_addresses = addr_ranges.flat_map(|r| r.step_by(4096));
frame_addresses.map(|addr| PhysFrame::from_start_address(PhysAddr::new(addr)).unwrap())
}
pub(crate) fn is_mapped(virt_addr: VirtAddr) -> bool {
let mapper = OFFSET_PAGE_TABLE.get().unwrap().lock();
matches!(mapper.translate(virt_addr), TranslateResult::Mapped { .. })
}
pub(crate) fn allocate_page(&mut self, start_addr: VirtAddr, flags: PageTableFlags) -> Result<(), MapToError<Size4KiB>> {
let page = Page::containing_address(start_addr);
let frame = self.allocate_frame().ok_or(MapToError::<Size4KiB>::FrameAllocationFailed)?;
let mut mapper = OFFSET_PAGE_TABLE.get().unwrap().lock();
unsafe {
mapper.map_to(page, frame, flags, self)?
}.flush();
Ok(())
}
}
unsafe impl FrameAllocator<Size4KiB> for FoundryOSFrameAllocator {
/// Allocates a frame from the list of usable frames.
///
/// This function returns the next available `PhysFrame` from the memory
/// map, if one exists. Once a frame is allocated, the internal counter
/// is incremented to point to the next frame for future allocations.
///
/// # Returns
///
/// - `Some(PhysFrame)`: If a usable frame is available.
/// - `None`: If there are no more usable frames to allocate.
fn allocate_frame(&mut self) -> Option<PhysFrame> {
let frame = self.usable_frames().nth(self.next);
self.next += 1;
frame
}
}
@@ -0,0 +1,86 @@
use crate::arch::x86_64::memory::STACK_VIRTUAL_SPACE;
use x86_64::VirtAddr;
use x86_64::structures::paging::{
FrameAllocator, Mapper, Page, Size4KiB, mapper,
};
fn reserve_stack_memory(size_in_pages: u64) -> Page {
use core::sync::atomic::{AtomicU64, Ordering};
static STACK_ALLOC_NEXT: AtomicU64 =
AtomicU64::new(STACK_VIRTUAL_SPACE as u64);
let start_addr =
VirtAddr::new(STACK_ALLOC_NEXT.fetch_add(
size_in_pages * Page::<Size4KiB>::SIZE,
Ordering::Relaxed,
));
Page::from_start_address(start_addr)
.expect("`STACK_ALLOC_NEXT` not page aligned")
}
/// Allocates a stack in the virtual address space, mapped to physical pages.
///
/// This function allocates a stack in the virtual address space, mapped to
/// physical pages. The stack is allocated as a sequence of pages, with the
/// first page allocated as a guard page. The stack is then mapped to the
/// allocated physical frame.
///
/// The function takes the size of the stack in pages, a mutable reference to a
/// mapper, and a mutable reference to a frame allocator. It returns a `Result`
/// containing a `StackBounds` struct, which contains the start and end virtual
/// addresses of the allocated stack.
///
/// # Safety
///
/// This function is unsafe because it maps physical frames to virtual addresses
/// without any protection. This can lead to bugs if the physical frames are not
/// correctly allocated, or if the virtual addresses are not correctly aligned.
///
/// # Panics
///
/// This function will panic if the allocation of the physical frame fails.
pub unsafe fn alloc_stack(
size_in_pages: u64,
mapper: &mut impl Mapper<Size4KiB>,
frame_allocator: &mut impl FrameAllocator<Size4KiB>,
) -> Result<StackBounds, mapper::MapToError<Size4KiB>> {
unsafe {
use x86_64::structures::paging::PageTableFlags as Flags;
let guard_page = reserve_stack_memory(size_in_pages + 1);
let stack_start = guard_page + 1;
let stack_end = stack_start + size_in_pages;
for page in Page::range(stack_start, stack_end) {
let frame = frame_allocator
.allocate_frame()
.ok_or(mapper::MapToError::FrameAllocationFailed)?;
let flags = Flags::PRESENT | Flags::WRITABLE;
mapper.map_to(page, frame, flags, frame_allocator)?.flush();
}
Ok(StackBounds {
start: stack_start.start_address(),
end: stack_end.start_address(),
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct StackBounds {
start: VirtAddr,
end: VirtAddr,
}
impl StackBounds {
pub const fn new(start: VirtAddr, end: VirtAddr) -> Self {
Self { start, end }
}
pub const fn start(&self) -> VirtAddr {
self.start
}
pub const fn end(&self) -> VirtAddr {
self.end
}
}
+56
View File
@@ -0,0 +1,56 @@
//! Sets up a memory map using Limine.
use limine::{
request::{HhdmRequest, KernelAddressRequest, MemoryMapRequest},
response::MemoryMapResponse,
};
use spin::Lazy;
#[used]
#[unsafe(link_section = ".requests")]
static MEMORY_MAP_REQUEST: MemoryMapRequest = MemoryMapRequest::new();
#[used]
#[unsafe(link_section = ".requests")]
static HIGHER_HALF_DIRECT_MAP_REQUEST: HhdmRequest = HhdmRequest::new();
#[used]
#[unsafe(link_section = ".requests")]
pub static KERNEL_ADDRESS_REQUEST: KernelAddressRequest =
KernelAddressRequest::new();
/// ```rs
/// let virt_addr = phys_addr + offset;
/// let phys_addr = virt_addr - offset; // (given VA is in the HHDM). Do not use for executable code.
/// ```
pub static PHYSICAL_MEMORY_OFFSET: Lazy<u64> = Lazy::new(|| {
HIGHER_HALF_DIRECT_MAP_REQUEST
.get_response()
.unwrap()
.offset()
});
/// Converts virtual addresses in the kernel to a physical address like this:
/// ```rs
/// let phys_addr = virt_addr - virtual_base + physical_base;
/// ```
///
/// Returns (virtual_base, physical_base)
pub static _KERNEL_PHYSICAL_MEMORY_OFFSET: Lazy<(u64, u64)> = Lazy::new(|| {
let resp = KERNEL_ADDRESS_REQUEST.get_response().unwrap();
// These are base addresses, using Limine's built in page table.
(resp.virtual_base(), resp.physical_base())
});
/// Fetches the memory map from Limine.
///
/// # Panics
///
/// Panics if the memory map was not found in MEMORY_MAP_REQUEST.
pub fn get_memory_map() -> &'static MemoryMapResponse {
MEMORY_MAP_REQUEST.get_response().map_or_else(
|| unreachable!("Could not fetch memory map from Limine."),
|memory_map| memory_map,
)
}
+62
View File
@@ -0,0 +1,62 @@
pub mod allocation;
pub mod mapping;
pub mod units;
use allocation::page_alloc::FoundryOSFrameAllocator;
use spin::{Mutex, Once};
use units::MemoryUnits::*;
use x86_64::{
VirtAddr,
registers::control::Cr3,
structures::paging::{OffsetPageTable, PageTable},
};
pub const STACK_VIRTUAL_SPACE: usize = 0x5555_5555_0000; // start address of the memory space where we store allocated stacks
pub const HEAP_VIRTUAL_SPACE: usize = 0x4444_4444_0000; // start address of heap allocated memory
pub const HEAP_SIZE: usize = MiB(1).to_bytes();
pub static FRAME_ALLOCATOR: Once<Mutex<FoundryOSFrameAllocator>> = Once::new();
pub static OFFSET_PAGE_TABLE: Once<Mutex<OffsetPageTable>> = Once::new();
/// Returns a mutable reference to the current level 4 page table.
///
/// # Safety
///
/// The caller must ensure that the level 4 page table is not modified
/// simultaneously. The caller must also ensure that the physical memory offset
/// is correct, to ensure that the correct virtual address is constructed.
unsafe fn active_l4_table(
physical_memory_offset: VirtAddr,
) -> &'static mut PageTable {
let (level_4_frame, _) = Cr3::read();
let phys_addr = level_4_frame.start_address();
let virt = phys_addr.as_u64() + physical_memory_offset.as_u64();
unsafe { &mut *(virt as *mut PageTable) }
}
/// Initializes the `OffsetPageTable` for the current CPU architecture.
///
/// # Safety
///
/// This function must be called only once and should be called before any
/// memory operations are performed that rely on virtual memory management.
/// The provided `physical_memory_offset` must be accurate to ensure correct
/// translation of physical addresses.
///
/// # Parameters
///
/// - `physical_memory_offset`: The offset to convert physical addresses to
/// virtual addresses in the higher-half direct map.
///
/// # Returns
///
/// Returns an `OffsetPageTable` that allows for manipulation of the page
/// tables for the current CPU architecture.
pub fn init_page_table(physical_memory_offset: VirtAddr) {
unsafe {
let l4_table = active_l4_table(physical_memory_offset);
let offset_table =
OffsetPageTable::new(l4_table, physical_memory_offset);
OFFSET_PAGE_TABLE.call_once(|| Mutex::new(offset_table));
}
}
+49
View File
@@ -0,0 +1,49 @@
pub enum MemoryUnits {
B(usize),
KiB(usize),
MiB(usize),
GiB(usize),
}
impl MemoryUnits {
pub const fn to_bytes(&self) -> usize {
match self {
Self::B(b) => *b,
Self::KiB(kib) => *kib * 1024,
Self::MiB(mib) => *mib * 1024 * 1024,
Self::GiB(gib) => *gib * 1024 * 1024 * 1024,
}
}
pub const fn from_bytes(bytes: usize) -> Self {
if bytes < 1024 {
Self::B(bytes)
} else if bytes < 1024 * 1024 {
Self::KiB(bytes / 1024)
} else if bytes < 1024 * 1024 * 1024 {
Self::MiB(bytes / (1024 * 1024))
} else {
Self::GiB(bytes / (1024 * 1024 * 1024))
}
}
pub const fn convert(&mut self) {
match self {
Self::B(b) if *b > 1024 => *self = Self::KiB(*b / 1024),
Self::KiB(kib) if *kib > 1024 => *self = Self::MiB(*kib / 1024),
Self::MiB(mib) if *mib > 1024 => *self = Self::GiB(*mib / 1024),
_ => (),
}
}
}
impl core::fmt::Display for MemoryUnits {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::B(b) => write!(f, "{} B", b),
Self::KiB(kib) => write!(f, "{} KiB", kib),
Self::MiB(mib) => write!(f, "{} MiB", mib),
Self::GiB(gib) => write!(f, "{} GiB", gib),
}
}
}
+6
View File
@@ -0,0 +1,6 @@
pub mod cpu;
pub mod drivers;
pub mod gdt;
pub mod interrupts;
pub mod memory;
pub mod processing;
@@ -0,0 +1 @@
pub mod task;
@@ -0,0 +1,150 @@
//! Allows creation of asynchronous IO bound tasks.
//!
//! Written by @zxq5 for the most part with code from
//! [here](https://github.com/phil-opp/blog_os/).
use alloc::boxed::Box;
use alloc::collections::BTreeMap;
use alloc::sync::Arc;
use alloc::task::Wake;
use core::{
future::Future,
pin::Pin,
sync::atomic::AtomicU64,
task::{Context, Poll, Waker},
};
use crossbeam::queue::ArrayQueue;
use x86_64::instructions::interrupts::{self, enable_and_hlt};
pub struct Task {
id: TaskId,
future: Pin<Box<dyn Future<Output = ()>>>,
}
impl Task {
pub fn new(future: impl Future<Output = ()> + 'static) -> Self {
Self {
id: TaskId::new(),
future: Box::pin(future),
}
}
fn poll(&mut self, context: &mut Context) -> Poll<()> {
self.future.as_mut().poll(context)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct TaskId(u64);
impl TaskId {
fn new() -> Self {
static NEXT: AtomicU64 = AtomicU64::new(0);
Self(NEXT.fetch_add(1, core::sync::atomic::Ordering::Relaxed))
}
}
pub struct Executor {
tasks: BTreeMap<TaskId, Task>,
task_queue: Arc<ArrayQueue<TaskId>>,
waker_cache: BTreeMap<TaskId, Waker>,
}
impl Executor {
pub fn new() -> Self {
Self {
tasks: BTreeMap::new(),
task_queue: Arc::new(ArrayQueue::new(100)),
waker_cache: BTreeMap::new(),
}
}
pub fn spawn(&mut self, task: Task) {
let task_id = task.id;
if self.tasks.insert(task.id, task).is_some() {
panic!("task with same id already in tasks");
}
self.task_queue.push(task_id).expect("queue full");
}
fn run_ready_tasks(&mut self) {
// destructure `self` to avoid borrow checker errors
let Self {
tasks,
task_queue,
waker_cache,
} = self;
while let Some(task_id) = task_queue.pop() {
let task = match tasks.get_mut(&task_id) {
Some(task) => task,
None => continue, // task no longer exists
};
let waker = waker_cache.entry(task_id).or_insert_with(|| {
TaskWaker::new_waker(task_id, task_queue.clone())
});
let mut context = Context::from_waker(waker);
match task.poll(&mut context) {
Poll::Ready(()) => {
// task done -> remove it and its cached waker
tasks.remove(&task_id);
waker_cache.remove(&task_id);
}
Poll::Pending => {}
}
}
}
pub fn run(&mut self) -> ! {
loop {
self.run_ready_tasks();
self.sleep_if_idle();
}
}
fn sleep_if_idle(&self) {
interrupts::disable();
if self.task_queue.is_empty() {
enable_and_hlt();
} else {
interrupts::enable();
}
}
}
impl Default for Executor {
fn default() -> Self {
Self::new()
}
}
struct TaskWaker {
task_id: TaskId,
task_queue: Arc<ArrayQueue<TaskId>>,
}
impl TaskWaker {
fn wake_task(&self) {
self.task_queue.push(self.task_id).expect("task_queue full");
}
fn new_waker(
task_id: TaskId,
task_queue: Arc<ArrayQueue<TaskId>>,
) -> Waker {
Waker::from(Arc::new(Self {
task_id,
task_queue,
}))
}
}
impl Wake for TaskWaker {
fn wake(self: Arc<Self>) {
self.wake_task();
}
fn wake_by_ref(self: &Arc<Self>) {
self.wake_task();
}
}
+4
View File
@@ -0,0 +1,4 @@
#![expect(unused)]
pub mod async_io;
mod taskrunner;
pub mod threading;
@@ -0,0 +1,12 @@
use alloc::boxed::Box;
pub struct Task {
pid: usize,
task: Box<dyn Fn() + Send + 'static>,
}
impl Task {
// pub const fn new() {}
pub const fn run() {}
}
@@ -0,0 +1,116 @@
mod switch;
mod deprecated;
use core::arch::asm;
#[repr(C)]
pub struct Thread {
id: usize,
/// This shall be default before the program is interrupted, otherwise it will store
/// CPU registers etc to be restored on context switch.
ctx: ThreadContext,
}
/// CPU state to be saved on context switches.
#[repr(C)]
#[derive(Default)]
pub struct ThreadContext {
/// Accumulator register.
rax: u64,
/// Base register.
rbx: u64,
/// Counter register.
rcx: u64,
/// Data register.
rdx: u64,
/// Source index register.
rsi: u64,
/// Destination index register.
rdi: u64,
/// Base pointer register.
rbp: u64,
/// Stack pointer register.
rsp: u64,
/// An extended register.
r8: u64,
/// An extended register.
r9: u64,
/// An extended register.
r10: u64,
/// An extended register.
r11: u64,
/// An extended register.
r12: u64,
/// An extended register.
r13: u64,
/// An extended register.
r14: u64,
/// An extended register.
r15: u64,
/// The instruction pointer.
rip: u64,
/// RFLAGS register.
rflags: u64,
}
impl ThreadContext {
/// Saves the current registers of the CPU before a context switch
/// to be restored later.
///
/// # Notes
///
/// This function should ONLY be called in interrupt handlers such
/// as that of the timer. This will then save registers as required
///
///
/// # Safety
///
/// This function is unsafe because of the usage of inline ASM.
#[inline(always)]
pub unsafe fn save_registers() -> Self {
let mut context = Self::default();
unsafe {
asm!(
"mov {0}, rax",
"mov {1}, rbx",
"mov {2}, rcx",
"mov {3}, rdx",
"mov {4}, rsi",
"mov {5}, rdi",
"mov {6}, rbp",
"mov {7}, rsp",
"mov {8}, r8",
"mov {9}, r9",
"mov {10}, r10",
"mov {11}, r11",
"mov {12}, r12",
"mov {13}, r13",
"mov {14}, r14",
"mov {15}, r15",
"lea {16}, [rip]",
"pushf",
"pop {17}",
out(reg) context.rax,
out(reg) context.rbx,
out(reg) context.rcx,
out(reg) context.rdx,
out(reg) context.rsi,
out(reg) context.rdi,
out(reg) context.rbp,
out(reg) context.rsp,
out(reg) context.r8,
out(reg) context.r9,
out(reg) context.r10,
out(reg) context.r11,
out(reg) context.r12,
out(reg) context.r13,
out(reg) context.r14,
out(reg) context.r15,
out(reg) context.rip,
out(reg) context.rflags,
);
}
context
}
}
@@ -0,0 +1,26 @@
use crate::arch::x86_64::memory::allocation::stack_alloc::StackBounds;
use x86_64::VirtAddr;
mod switch;
#[derive(Debug)]
pub struct Thread {
id: ThreadId,
stack_ptr: Option<VirtAddr>,
stack_bounds: Option<StackBounds>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct ThreadId(u64);
impl ThreadId {
pub const fn as_u64(&self) -> u64 {
self.0
}
fn new() -> Self {
use core::sync::atomic::{AtomicU64, Ordering};
static NEXT_THREAD_ID: AtomicU64 = AtomicU64::new(1);
Self(NEXT_THREAD_ID.fetch_add(1, Ordering::Relaxed))
}
}
@@ -0,0 +1,13 @@
.intel_syntax noprefix
switch_thread:
pushfq
mov rax, rsp
mov rsp, rdi
mov rdi, rax
call add_paused_thread
popfq
ret
@@ -0,0 +1 @@
+149
View File
@@ -0,0 +1,149 @@
#![no_std]
#![feature(abi_x86_interrupt)]
#![warn(
clippy::correctness,
clippy::nursery,
clippy::unnecessary_cast,
clippy::all,
clippy::suspicious,
clippy::perf,
rustdoc::missing_errors_doc,
rustdoc::missing_panics_doc
)]
extern crate alloc;
use crate::{arch::x86_64::memory::init_page_table, prelude::*};
use arch::x86_64::memory::allocation::heap_alloc::init_heap;
use arch::x86_64::memory::mapping;
use core::arch::asm;
use limine::BaseRevision;
use std::unwind;
use std::unwind::eh_info::ELF;
use x86_64::VirtAddr;
use arch::x86_64::memory::allocation::page_alloc::FoundryOSFrameAllocator;
use crate::arch::x86_64::cpu::apic::enable_apic;
use crate::arch::x86_64::drivers::ascii::screensize_chars;
use crate::arch::x86_64::drivers::framebuffer::display::screensize_px;
use crate::arch::x86_64::memory::FRAME_ALLOCATOR;
use crate::arch::x86_64::memory::units::MemoryUnits;
pub mod arch;
mod panic;
pub mod resources;
#[allow(unused)] // We aren't using much of this right now.
pub mod std;
pub mod util;
pub mod prelude {
pub use crate::{
eprint, eprintln, print, print_log, println, println_log, serial_print,
serial_println,
debug, debugln,
std::io::{_print, _print_log, _serial_write, _print_err},
std::debug::_debug,
};
}
/// Sets the base revision to the latest revision supported by the crate.
/// See specification for further info.
/// Be sure to mark all limine requests with #[used], otherwise they may be
/// removed by the compiler.
#[used]
// The .requests section allows limine to find the requests faster and more
// safely.
#[unsafe(link_section = ".requests")]
static BASE_REVISION: BaseRevision = BaseRevision::new();
#[panic_handler]
fn rust_panic(_info: &core::panic::PanicInfo) -> ! {
println!("Kernel panic: {}", _info);
serial_println!("Kernel panic: {}", _info);
hcf();
}
pub fn hcf() -> ! {
loop {
unsafe {
#[cfg(target_arch = "x86_64")]
asm!("hlt");
}
}
}
pub fn boot() -> Result<(), &'static str> {
if !BASE_REVISION.is_supported() {
return Err("Base revision not supported");
}
use arch::x86_64::{gdt, interrupts};
let memory_map = mapping::get_memory_map();
print_log!(" Initialising Serial... ");
let res = arch::x86_64::drivers::serial::init();
serial_print!(" Initialising Serial... ");
if res.is_err() {
debugln!("[Not Detected]")
} else {
debugln!("[Success]");
}
debugln!(" Display...");
let dimensions = screensize_chars();
let dimensions2 = screensize_px();
debugln!(" => (px) : {}x{} ", dimensions2.0, dimensions2.1);
debugln!(" => (chars) : {}x{} ", dimensions.0, dimensions.1);
debugln!(" [Success]");
debug!(" Setting Up Global Descriptor Table... ");
gdt::init();
debugln!("[Success]");
debug!(" Setting Up Interrupt Descriptor Table... ");
interrupts::init_idt();
debugln!("[Success]");
debugln!(" Initialising Memory Subsystem... ");
let physical_memory_offset = VirtAddr::new(*mapping::PHYSICAL_MEMORY_OFFSET);
init_page_table(physical_memory_offset);
FoundryOSFrameAllocator::init(memory_map);
let available_bytes = FRAME_ALLOCATOR.get().unwrap().lock().available_memory();
debugln!(" => Available Memory: {}", MemoryUnits::from_bytes(available_bytes as usize));
debugln!("[Success]");
debugln!(" Initialising Heap... ");
if unsafe { init_heap() }.is_err() {
return Err("Failed to initialise heap: error");
}
debugln!(" [Success]");
// debug!(" Enabling PICs... ");
// interrupts::enable_pic();
// debugln!("[Success]");
debug!(" Disabling PICs... ");
interrupts::disable_pic();
debugln!("[Success]");
debug!(" Initialising APIC... ");
enable_apic();
debugln!("[Success]");
debug!(" Enabling Interrupts... ");
x86_64::instructions::interrupts::enable();
debugln!("[Success]");
debug!(" Setting up stack unwinder, panic handler... ");
// Setup stack traces and proper panic handler. TODO: Handle panics
// differently if not initialised.
let eh_frame_ptr = ELF
.get_section_addr(".eh_frame_hdr")
.expect("Could not get `.eh_frame_hdr` address.");
let _eh_info =
unsafe { unwind::eh_info::EhInfo::from_hdr_ptr(eh_frame_ptr) };
debugln!("[Success]");
Ok(())
}
+20 -58
View File
@@ -1,67 +1,29 @@
#![no_std]
#![no_main]
use core::arch::asm;
extern crate alloc;
use foundry_os::arch::x86_64::drivers::ascii::screensize_chars;
use foundry_os::arch::x86_64::drivers::framebuffer::display::screensize_px;
use foundry_os::arch::x86_64::processing::async_io::task::{Executor, Task};
use foundry_os::util::shell::shell;
use foundry_os::{println, println_log};
use limine::request::{FramebufferRequest, RequestsEndMarker, RequestsStartMarker};
use limine::BaseRevision;
/// Sets the base revision to the latest revision supported by the crate.
/// See specification for further info.
/// Be sure to mark all limine requests with #[used], otherwise they may be removed by the compiler.
#[used]
// The .requests section allows limine to find the requests faster and more safely.
#[link_section = ".requests"]
static BASE_REVISION: BaseRevision = BaseRevision::new();
#[used]
#[link_section = ".requests"]
static FRAMEBUFFER_REQUEST: FramebufferRequest = FramebufferRequest::new();
/// Define the stand and end markers for Limine requests.
#[used]
#[link_section = ".requests_start_marker"]
static _START_MARKER: RequestsStartMarker = RequestsStartMarker::new();
#[used]
#[link_section = ".requests_end_marker"]
static _END_MARKER: RequestsEndMarker = RequestsEndMarker::new();
#[no_mangle]
unsafe extern "C" fn kmain() -> ! {
// All limine requests must also be referenced in a called function, otherwise they may be
// removed by the linker.
assert!(BASE_REVISION.is_supported());
if let Some(framebuffer_response) = FRAMEBUFFER_REQUEST.get_response() {
if let Some(framebuffer) = framebuffer_response.framebuffers().next() {
for i in 0..100_u64 {
// Calculate the pixel offset using the framebuffer information we obtained above.
// We skip `i` scanlines (pitch is provided in bytes) and add `i * 4` to skip `i` pixels forward.
let pixel_offset = i * framebuffer.pitch() + i * 4;
// Write 0xFFFFFFFF to the provided pixel offset to fill it white.
*(framebuffer.addr().add(pixel_offset as usize) as *mut u32) = 0xFFFFFFFF;
}
}
#[unsafe(no_mangle)]
extern "C" fn kmain() -> ! {
println_log!(" [ Initialising Kernel Systems ] ");
if let Err(err) = foundry_os::boot() {
panic!("{}", err);
}
hcf();
}
println_log!(" [ Kernel Initialised Successfully ] ");
#[panic_handler]
fn rust_panic(_info: &core::panic::PanicInfo) -> ! {
hcf();
}
// println!("TESTING :: Allocation");
// let somevec = vec![0; 1_000_000];
// println!("{:?}", somevec);
// println!("{}", somevec.len());
// println!("PASSED!");
fn hcf() -> ! {
loop {
unsafe {
#[cfg(target_arch = "x86_64")]
asm!("hlt");
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
asm!("wfi");
#[cfg(target_arch = "loongarch64")]
asm!("idle 0");
}
}
let mut executor = Executor::new();
executor.spawn(Task::new(shell()));
executor.run()
}
+75
View File
@@ -0,0 +1,75 @@
// //! Stack-unwinding code for the Kernel. This is a modified version of https://github.com/nbdd0121/unwinding/blob/trunk/src/panic_handler.rs
// //! which does not support environment variables for obvious reasons, however we
// //! may implement a cmdline similar to Linux in the near future.
// #![expect(
// clippy::empty_loop,
// reason = "We don't yet have working power management. This is OK for now."
// )]
// use crate::prelude::*;
// use alloc::boxed::Box;
// use core::any::Any;
// use core::cell::Cell;
// use core::ffi::c_void;
// use core::panic::{Location, PanicInfo};
// use unwinding::abi::*;
// use unwinding::panic::begin_panic;
// #[thread_local]
// static PANIC_COUNT: Cell<usize> = Cell::new(0);
// #[link(name = "c")]
// unsafe extern "C" {}
// fn stack_trace() {
// struct CallbackData {
// counter: usize,
// }
// extern "C" fn callback(
// unwind_ctx: &UnwindContext<'_>,
// arg: *mut c_void,
// ) -> UnwindReasonCode {
// let data = unsafe { &mut *(arg as *mut CallbackData) };
// data.counter += 1;
// eprintln!(
// "{:4}:{:#19x} - <unknown>",
// data.counter,
// _Unwind_GetIP(unwind_ctx)
// );
// UnwindReasonCode::NO_REASON
// }
// let mut data = CallbackData { counter: 0 };
// _Unwind_Backtrace(callback, &mut data as *mut _ as _);
// }
// fn do_panic(msg: Box<dyn Any + Send>) -> ! {
// if PANIC_COUNT.get() >= 1 {
// stack_trace();
// eprintln!("Thread panicked while processing panic. aborting.");
// loop {}
// }
// PANIC_COUNT.set(1);
// stack_trace();
// let code = begin_panic(Box::new(msg));
// eprintln!("Failed to initiate panic: error {}", code.0);
// loop {}
// }
// #[panic_handler]
// fn panic(info: &PanicInfo<'_>) -> ! {
// eprintln!("{}", info);
// struct NoPayload;
// do_panic(Box::new(NoPayload))
// }
// #[track_caller]
// #[expect(unused, reason = "May still be used in the future.")]
// pub fn panic_any<M: 'static + Any + Send>(msg: M) -> ! {
// eprintln!("Panicked at {}", Location::caller());
// do_panic(Box::new(msg))
// }
+48
View File
@@ -0,0 +1,48 @@
use libm::include_font;
pub const FONT_SPLEEN_8X16: Font =
Font::new(include_font!("../../../resources/font/spleen-8x16.psf"));
pub const FONT_CP850_8X16: Font =
Font::new(include_font!("../../../resources/font/cp850-8x16.psf"));
// pub struct Font(pub [[u8; 16]; 512]);
pub struct Font {
width: usize,
height: usize,
length: u16,
data: [[u8; 16]; 512],
}
impl Font {
pub const fn new(data: [[u8; 16]; 512]) -> Self {
Self {
width: 8,
height: 16,
length: data.len() as u16,
data,
}
}
pub const fn glyph_for(&self, c: u16) -> &[u8] {
if c > self.length {
return &self.data[0];
}
&self.data[c as usize]
}
pub const fn width(&self) -> usize {
self.width
}
pub const fn height(&self) -> usize {
self.height
}
}
impl Default for Font {
fn default() -> Self {
FONT_CP850_8X16
}
}
+1
View File
@@ -0,0 +1 @@
pub mod font;
+22
View File
@@ -0,0 +1,22 @@
use alloc::string::String;
use alloc::vec::Vec;
pub mod frame;
pub mod render;
pub mod window;
pub trait Application {
type Output;
fn run(
&mut self,
args: Vec<String>,
) -> impl Future<Output = Result<Self::Output, Error>> + Send;
}
#[derive(Debug)]
pub enum Error {
UnknownCommand(String),
ApplicationFailed(String),
KernelError(String),
}
+48
View File
@@ -0,0 +1,48 @@
use super::{render::RenderError, window::Window};
use crate::arch::x86_64::drivers::framebuffer::colour::Colour;
use crate::std::maths::geometry::Vec2;
use alloc::{vec, vec::Vec};
pub struct Frame<'f> {
data: Vec<Vec<Colour>>,
window: &'f Window,
}
impl<'a> Frame<'a> {
pub fn new(window: &'a Window) -> Self {
Self {
data: vec![
vec![Colour::Black; window.dimensions().x()];
window.dimensions().y()
],
window,
}
}
pub fn render(&self) -> Result<(), RenderError> {
let data: Vec<&[Colour]> =
self.data.iter().map(|v| v.as_slice()).collect::<Vec<_>>();
self.window
.render(data.as_slice())
.map_err(|_| RenderError::Generic)
}
pub fn write_pixel(
&mut self,
x: usize,
y: usize,
color: Colour,
) -> Result<(), RenderError> {
if x >= self.window.dimensions().x()
|| y >= self.window.dimensions().y()
{
return Err(RenderError::Generic);
}
self.data[y][x] = color;
Ok(())
}
pub const fn dimensions(&self) -> Vec2<usize> {
self.window.dimensions()
}
}
+22
View File
@@ -0,0 +1,22 @@
use crate::arch::x86_64::drivers::framebuffer::colour::Colour;
use core::fmt::Display;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RenderError {
Generic,
}
impl Display for RenderError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Generic => write!(f, "Generic render error"),
}
}
}
impl core::error::Error for RenderError {}
pub struct ColouredChar {
ch: u8,
colour: Colour,
}
+93
View File
@@ -0,0 +1,93 @@
use super::render::RenderError;
use crate::arch::x86_64::drivers::framebuffer::colour::Colour;
use crate::arch::x86_64::drivers::framebuffer::display::FRAMEBUFFER_WRITER;
use crate::std::maths::geometry::Vec2;
use alloc::string::String;
pub struct Window {
dimensions: Vec2<usize>,
position: Vec2<usize>,
bordered: bool,
opened: bool,
title: String,
}
impl Window {
pub const fn new() -> Self {
Self {
dimensions: Vec2::new(0, 0),
position: Vec2::new(0, 0),
bordered: true,
opened: false,
title: String::new(),
}
}
pub fn render(&self, _data: &[&[Colour]]) -> Result<(), RenderError> {
// TODO: error handling!! the kernel should return an error in some
// cases
if let Some(fb) = FRAMEBUFFER_WRITER.lock().as_mut() {
fb.render_frame(_data);
}
Ok(())
}
pub const fn is_bordered(&self) -> bool {
self.bordered
}
pub const fn is_open(&self) -> bool {
self.opened
}
pub const fn open(&mut self) {
self.opened = true;
}
pub const fn close(&mut self) {
self.opened = false;
}
// some basic getters and setters for utility.
pub fn title(&'static self) -> &'static str {
self.title.as_str()
}
pub const fn dimensions(&self) -> Vec2<usize> {
self.dimensions
}
pub const fn position(&self) -> Vec2<usize> {
self.position
}
pub fn set_title(&mut self, title: String) {
self.title = title;
}
pub fn move_window(&mut self, offset: Vec2<usize>) {
self.position += offset;
}
pub const fn set_position(&mut self, position: Vec2<usize>) {
self.position = position;
}
pub const fn set_dimensions(&mut self, dimensions: Vec2<usize>) {
self.dimensions = dimensions;
}
}
impl Default for Window {
fn default() -> Self {
Self::new()
}
}
impl Drop for Window {
fn drop(&mut self) {
if self.opened {
self.close();
}
}
}
+61
View File
@@ -0,0 +1,61 @@
use super::{
application::{frame::Frame, render::RenderError},
maths::geometry::Vec2,
};
use crate::arch::x86_64::drivers::framebuffer::colour::Colour;
use crate::resources::font::Font;
pub struct Writer<'a> {
font: &'a Font,
}
impl<'a> Writer<'a> {
pub const fn new(font: &'a Font) -> Self {
Self { font }
}
pub const fn set_font(&mut self, font: &'a Font) {
self.font = font;
}
pub const fn font_size(&self) -> Vec2<usize> {
Vec2::new(self.font.width(), self.font.height())
}
pub fn render_glyph(
&self,
frame: &mut Frame,
pos: Vec2<usize>,
c: u8,
scale: usize,
) -> Result<(), RenderError> {
// get a reference to the character glyph from the font.
let data: &[u8] = self.font.glyph_for(c as u16);
if pos.x() + self.font.width() * scale > frame.dimensions().x()
|| pos.y() + self.font.height() * scale > frame.dimensions().y()
{
return Err(RenderError::Generic);
}
for (row, line) in data.iter().enumerate().take(self.font.height()) {
for col in 0..self.font.width() {
let pixel_x: usize = pos.x() + col * scale;
let pixel_y: usize = pos.y() + row * scale;
if line & (0x80 >> col) != 0 {
for i in 0..scale {
for j in 0..scale {
frame.write_pixel(
pixel_x + i,
pixel_y + j,
Colour::White,
)?;
}
}
}
}
}
Ok(())
}
}
+22
View File
@@ -0,0 +1,22 @@
use core::fmt;
use x86_64::instructions::interrupts;
use crate::prelude::{_print_log, _serial_write};
#[macro_export]
macro_rules! debugln {
() => ($crate::print_log!("\n"));
($($arg:tt)*) => ($crate::debug!("{}\n", format_args!($($arg)*)));
}
#[macro_export]
macro_rules! debug {
($($arg:tt)*) => ($crate::prelude::_debug(format_args!($($arg)*)));
}
pub fn _debug(args: fmt::Arguments) {
interrupts::without_interrupts(|| {
_print_log(args);
_serial_write(args);
});
}
+114
View File
@@ -0,0 +1,114 @@
//! Basic ELF parsing functionality using the `elf` crate.
//!
//! This may be extended in the future to support loading programs, however we
//! currently use this for getting the sizes of sections in our kernel ELF at
//! runtime.
//!
//! This is used for implementing stacktraces in std::unwind.
//!
//! # TODO
//!
//! * Add support for loading binary programs (this should probably be written
//! in a different module)
use alloc::format;
use elf::{
ElfBytes, ParseError,
endian::LittleEndian,
parse::{ParseAt, ParsingTable},
section::{SectionHeader, SectionHeaderTable},
string_table::StringTable,
};
use limine::request::KernelFileRequest;
use crate::prelude::*;
#[cfg(target_arch = "x86_64")]
/// The length of the ELF header in bytes.
pub const ELF_HEADER_LEN: usize = 64;
/// Information about our own ELF file to make ELF parsing easier, such as the
/// length of the file and a pointer to the contents.
#[used]
pub static KERNEL_FILE_REQUEST: KernelFileRequest = KernelFileRequest::new();
/// A list of errors that may occur when parsing ELF files.
#[derive(Debug)]
pub enum ElfError {
/// Returned if a section did not exist in [ElfReader::get_section_size].
SectionNotExists,
/// Parse errors returned by the `elf` crate.
OtherParseError(elf::ParseError),
}
impl From<elf::ParseError> for ElfError {
fn from(err: elf::ParseError) -> Self {
Self::OtherParseError(err)
}
}
pub struct ElfReader {
/// Structure returned by the `elf` crate having parsed the ELF header.
elf: ElfBytes<'static, LittleEndian>,
}
impl ElfReader {
/// Parses the ELF file for the kernel, this uses data from Limine's Kernel
/// File Request to get a slice over the whole executable file.
///
/// # Safety
///
/// Assumes a properly formed ELF file, and that Limine returns a correct
/// pointer to the start of the file as well as a valid length in bytes.
///
/// Both of these should be satisfied, but this function is marked unsafe
/// just in case, because we are derefererencing arbitrary pointers.
pub unsafe fn new() -> Result<Self, ElfError> {
let response = KERNEL_FILE_REQUEST
.get_response()
.expect("Didn't get the kernel file from Limine. That's odd.");
// We fetch these from Limine and use them to parse our own ELF file.
let file = response.file();
let file_start_ptr = file.addr();
let file_size = file.size() as usize;
// Safety: This slice should contain the whole bytes of the ELF file.
let elf_hdr_slice =
unsafe { core::slice::from_raw_parts(file_start_ptr, file_size) };
let elf: ElfBytes<'static, LittleEndian> =
elf::ElfBytes::minimal_parse(elf_hdr_slice)?;
Ok(Self { elf })
}
/// Gets the section size of `section_name` in bytes.
pub fn get_section_size(
&self,
section_name: &'static str,
) -> Result<u64, ElfError> {
Ok(self.get_section_header(section_name)?.sh_size)
}
/// Gets the start address of the section `section_name` in memory.
pub fn get_section_addr(
&self,
section_name: &'static str,
) -> Result<*const u8, ElfError> {
Ok(self.get_section_header(section_name)?.sh_addr as *const u8)
}
/// Gets the section header of `section_name`.
pub fn get_section_header(
&self,
section_name: &'static str,
) -> Result<SectionHeader, ElfError> {
let section_hdr = self
.elf
.section_header_by_name(section_name)
.map_err(|_e| ElfError::SectionNotExists)?;
section_hdr.ok_or(ElfError::SectionNotExists)
}
}
+6
View File
@@ -0,0 +1,6 @@
pub use crate::arch::x86_64::drivers::{
ascii::{_print, _print_err, _print_log},
serial::_serial_write,
};
pub mod stdin;
+75
View File
@@ -0,0 +1,75 @@
use crate::arch::x86_64::drivers::{
ascii::WRITER,
keyboard::{KeyStroke, get_keystroke_async, get_keystroke_optional},
};
use alloc::string::String;
/// Reads a line of input from standard input asynchronously, returning a
/// `String` containing the input line. Does not include the newline
/// character at the end of the line.
///
/// If the user presses the abort key (usually Ctrl+C), the returned string
/// will be empty.
pub async fn read_line() -> String {
let mut writer = WRITER.lock();
let mut buff = String::new();
loop {
match get_keystroke_async().await {
KeyStroke::Char(c) => match c {
'\n' => {
writer.write_glyph(c as u8);
return buff;
}
'\r' => {
writer.write_glyph(c as u8);
return buff;
}
'\x08' => {
if !buff.is_empty() {
buff.pop();
writer.backspace();
}
}
c => {
writer.write_glyph(c as u8);
buff.push(c)
}
},
KeyStroke::Enter => {
writer.write_glyph(b'\n');
return buff;
}
KeyStroke::Backspace => {
if !buff.is_empty() {
buff.pop();
writer.backspace();
}
}
_ => continue,
}
}
}
/// Reads a character from standard input and blocks the current task until
/// a character is available.
///
/// # Note
///
/// This function is not yet implemented.
pub async fn async_keystroke() -> KeyStroke {
get_keystroke_async().await
}
/// Attempt to read a character from standard input without blocking the
/// current task.
///
/// If no character is available, returns `None`.
///
/// # Note
///
/// This function is not yet implemented.
pub fn keystroke() -> Option<KeyStroke> {
get_keystroke_optional()
}
+79
View File
@@ -0,0 +1,79 @@
use core::ops::{AddAssign, DivAssign, MulAssign, SubAssign};
pub trait Coordinate:
Copy + Clone + PartialEq + AddAssign + MulAssign + SubAssign + DivAssign
{
}
impl Coordinate for usize {}
impl Coordinate for isize {}
impl Coordinate for u8 {}
impl Coordinate for i8 {}
impl Coordinate for u16 {}
impl Coordinate for i16 {}
impl Coordinate for u32 {}
impl Coordinate for i32 {}
impl Coordinate for u64 {}
impl Coordinate for i64 {}
impl Coordinate for u128 {}
impl Coordinate for i128 {}
impl Coordinate for f32 {}
impl Coordinate for f64 {}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Debug)]
pub struct Vec2<T: Coordinate> {
x: T,
y: T,
}
impl<T: Coordinate> Vec2<T> {
pub const fn new(x: T, y: T) -> Self {
Self { x, y }
}
pub fn into<S: Coordinate + From<T>>(&self) -> Vec2<S> {
Vec2::new(self.x.into(), self.y.into())
}
pub const fn x(&self) -> T {
self.x
}
pub const fn y(&self) -> T {
self.y
}
}
impl<T: Coordinate> AddAssign for Vec2<T> {
fn add_assign(&mut self, rhs: Self) {
self.x += rhs.x;
self.y += rhs.y;
}
}
impl<T: Coordinate> SubAssign for Vec2<T> {
fn sub_assign(&mut self, rhs: Self) {
self.x -= rhs.x;
self.y -= rhs.y;
}
}
impl<T: Coordinate> MulAssign<T> for Vec2<T> {
fn mul_assign(&mut self, rhs: T) {
self.x *= rhs;
self.y *= rhs;
}
}
impl<T: Coordinate> DivAssign<T> for Vec2<T> {
fn div_assign(&mut self, rhs: T) {
self.x /= rhs;
self.y /= rhs;
}
}
impl<T: Coordinate> From<Vec2<T>> for (T, T) {
fn from(value: Vec2<T>) -> Self {
(value.x, value.y)
}
}
+1
View File
@@ -0,0 +1 @@
pub mod geometry;
+7
View File
@@ -0,0 +1,7 @@
pub mod application;
pub mod ascii;
pub mod elf;
pub mod io;
pub mod maths;
pub mod unwind;
pub mod debug;

Some files were not shown because too many files have changed in this diff Show More