Main File
- Unaltered except loops for stability.
OS in Rust
qemu working at all.
This is great, because you can use the same boot logic across all machines from the last century.
Writing a bootloader is a bit cumbersome as it requires assembly language and “write this magic value to this processor register”.
Instead use a bootimage that automatically prepends a bootloader to your kernel.
host triple is x86_64-unknown-linux-gnu
x86_64),unknown) - It’s Intel #Portlandlinux)gnu).{
"llvm-target": "x86_64-unknown-linux-gnu",
"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": "linux",
"executables": true,
"linker-flavor": "gcc",
"pre-link-args": ["-m64"],
"morestack": false
}target-pointer-width.pre-link-args field specifies arguments passed to the linker.x86_64.llvm-target and the os field to none, because we will run on bare metal.panic = "abort" option in our Cargo.tomlcore, an architecture specific library, and we need core to also panic abort.features enables/disables target features.mmx and sse features by prefixing them with a minussoft-float feature by prefixing it with a plus.mmx and sse features are performance optimizing vector operations from when Intel though they’d be able to hold off NVIDIA in the 90s.numpyx86_64 require SIMD registers by default.
soft-float feature, which emulates all floating point operations through software functions based on normal integers.
f16rustc that we want to use the corresponding ABI.x86_64-osirs.json
{
"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",
"rustc-abi": "x86-softfloat"
}curlrustc.rustc!core
core to also panic abort?src/main.rs we do have a core reference.ld.lld “linker-flavor” instructs LLVM to compile with the -flavor gnu flag._start - same as before!--target:$ cargo b --target x86_64-osirs.json
error: failed to run `rustc` to learn about target-specific information
Caused by:
process didn't exit successfully: `/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc - --crate-name ___ --print=file-names --target /home/user/tmp/32/x86_64-osirs.json --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=split-debuginfo --print=crate-name --print=cfg -Wwarnings` (exit status: 1)
--- stderr
error: Error loading target specification: Field target-pointer-width in target specification is required. Run `rustc --print target-list` for a list of built-in targetsrustc expect pointer width as a JSON string and not a JSON integer.
$ cargo b --target x86_64-osirs.json
Compiling osirs v0.1.0 (/home/user/tmp/32)
error[E0463]: can't find crate for `core`
|
= note: the `x86_64-osirs` target may not be installed
= help: consider downloading the target with `rustup target add x86_64-osirs`
For more information about this error, try `rustc --explain E0463`.
error: could not compile `osirs` (bin "osirs") due to 1 previous error
core library.
Result, Option, and iterators, and is implicitly linked to all no_std crates.core is usually pre-compiled (and then, of course, linked).rustc to do some compilation..cargo/config.tomlcargo to do, and store them in TOML file in the hidden .cargo folder..cargo first?build-std Optionbuild-std is a feature of Cargo.core and other standard library crates on demand.
build-stdcore$ cargo b --target x86_64-osirs.json
Compiling osirs v0.1.0 (/home/user/tmp/32)
error[E0463]: can't find crate for `core`
|
= note: the `x86_64-osirs` target may not be installed
= help: consider downloading the target with `rustup target add x86_64-osirs`
For more information about this error, try `rustc --explain E0463`.
error: could not compile `osirs` (bin "osirs") due to 1 previous errorbuild-std is not a stable feature of the Rust language.
cargo they are unstable.[unstable] label to our build-std configuration.$ cargo b --target x86_64-osirs.json
Compiling osirs v0.1.0 (/home/user/tmp/32)
error[E0463]: can't find crate for `core`
|
= note: the `x86_64-osirs` target may not be installed
= help: consider downloading the target with `rustup target add x86_64-osirs`
For more information about this error, try `rustc --explain E0463`.
error: could not compile `osirs` (bin "osirs") due to 1 previous errorbuild-std - which we need - is unstable and[unstable] is only available in nightly Rust.
+nightly right after cargo.error: `.json` target specs require -Zjson-target-spec
Compiling compiler_builtins v0.1.160 (/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/compiler-builtins/compiler-builtins)
Compiling core v0.0.0 (/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core)
Compiling osirs v0.1.0 (/home/user/tmp/32)
error: linking with `cc` failed: exit status: 1
|
= note: "cc" "-m64" "/home/user/tmp/32/target/debug/deps/rustcMmXrbF/symbols.o" "<1 object files omitted>" "-Wl,--as-needed" "-Wl,-Bstatic" "/home/user/tmp/32/target/debug/deps/{libcore-0c26ef2bd74962c1,libcompiler_builtins-40a77a01cbdbd500}.rlib" "-L" "/home/user/tmp/32/target/debug/deps/rustcMmXrbF/raw-dylibs" "-Wl,-Bdynamic" "-B<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/bin/gcc-ld" "-fuse-ld=lld" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/home/user/tmp/32/target/debug/deps/osirs-62b17f4aa5d3b391" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs"
= note: some arguments are omitted. use `--verbose` to show all linker arguments
= note: rust-lld: error: duplicate symbol: _start
>>> defined at /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/Scrt1.o:(_start)
>>> defined at main.rs:6 (src/main.rs:6)
>>> /home/user/tmp/32/target/debug/deps/osirs-62b17f4aa5d3b391.3hpwl9lytayxk9wu897na7tu0.0wpyfaj.rcgu.o:(.text._start+0x0)
collect2: error: ld returned 1 exit status
error: could not compile `osirs` (bin "osirs") due to 1 previous error-Zjson-target-spec in there.error: failed to run `rustc` to learn about target-specific information
Caused by:
process didn't exit successfully: `/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc - --crate-name ___ --print=file-names --target /home/user/tmp/32/x86_64-osirs.json -Zunstable-options --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=split-debuginfo --print=crate-name --print=cfg -Wwarnings` (exit status: 1)
--- stderr
error: error loading target specification: target-pointer-width: invalid type: string "64", expected u16 at line 6 column 32
|
= help: run `rustc --print target-list` for a list of built-in targets-Zjson-target-spec looks an awful lot like a .cargo/config.toml option…
error: failed to run `rustc` to learn about target-specific information
Caused by:
process didn't exit successfully: `/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc - --crate-name ___ --print=file-names --target /home/user/tmp/32/x86_64-osirs.json -Zunstable-options --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=split-debuginfo --print=crate-name --print=cfg -Wwarnings` (exit status: 1)
--- stderr
error: error loading target specification: target-pointer-width: invalid type: string "64", expected u16 at line 6 column 32
|
= help: run `rustc --print target-list` for a list of built-in targets$ cargo +nightly b --target x86_64-osirs.json
Compiling compiler_builtins v0.1.160 (/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/compiler-builtins/compiler-builtins)
Compiling core v0.0.0 (/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core)
^[[A Building [==========> ] 2/5: core, compiler_builtins Compiling osirs v0.1.0 (/home/user/tmp/32)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 10.18s.cargo/config.toml!.cargo/config.toml to use nightly!
soft-float lines and it still worked for now.compiler-builtinsqemu$ qemu-system-x86_64 -kernel target/x86_64-osirs/debug/osirs
Command 'qemu-system-x86_64' not found, but can be installed with:
sudo apt install qemu-system-x86 # version 1:6.2+dfsg-2ubuntu6.27, or
sudo apt install qemu-system-x86-xen # version 1:6.2+dfsg-2ubuntu6.27qemu
apt$ qemu-system-x86_64 -kernel target/x86_64-osirs/debug/osirs
qemu-system-x86_64: Error loading uncompressed kernel without PVH ELF Notex86_64-osirs.json
{
"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
}