錆で自作OS 6日目
引き続き第3章 ブートローダーを進める.
https://os.phil-opp.com/ja/minimal-rust-kernel/#kaneruwoshi-xing-suru
bootloader クレートというものがあるらしいけど、まずは自作を試みる.
でも参考にはする.
たぶんリンクはLLDのそれ以外は貨物で進める.
まずは妖精形式のカーネルをビルドする.
ビルド設定を
MacOSの場合、binutilsの
調べてたら先駆者を見つけた.
https://natsutan.hatenablog.com/entry/2021/04/14/190451
この人はCの総研大アプリから錆のカーネルを読んでいるようだけど、自分は全部錆でやってみたい.
上記ブログの参考ブログは全部錆でやってた.
https://www.akiradeveloper.com/post/mikanos-rust/
これが全てっぽい.
自作OS本と https://github.com/akiradeveloper/MikanOS.rs/blob/master/boot/src/main.rs を見ながら進める.
一通り書いた時点で起動してみると、邱がパニックした.
ディレクトリ構成を修正すると、パニックせず起動できたようだ.
今日の進捗 -> https://github.com/ytannai/kabos/tree/8e5091cbb471ca660dab44091051d79d692692c2
https://os.phil-opp.com/ja/minimal-rust-kernel/#kaneruwoshi-xing-suru
bootloader クレートというものがあるらしいけど、まずは自作を試みる.
でも参考にはする.
私達のカーネルをELFファイルにコンパイルする。
依存であるbootloaderをスタンドアロンの実行ファイルとしてコンパイルする。
カーネルのELFファイルのバイト列をブートローダーにリンクする。
たぶんリンクはLLDのそれ以外は貨物で進める.
まずは妖精形式のカーネルをビルドする.
ビルド設定を
target.json
として作成する.{
"llvm-target": "x86_64-unknown-none",
"data-layout": "e-m:e-i64:64-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",
"exe-suffix": ".elf"
}
コマンド build --target target.json --package kernel --bin kernel
でビルドできる.MacOSの場合、binutilsの
greadelf
でバイナリを確認できる.$ brew install binutils
$ /usr/local/Cellar/binutils/2.36.1/bin/greadelf -h ../target/target/debug/kernel.elf
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x201120
Start of program headers: 64 (bytes into file)
Start of section headers: 4472 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 4
Size of section headers: 64 (bytes)
Number of section headers: 15
Section header string table index: 13
エントリーポイントアドレスが自作OS本とは合っていないが、とりあえずブートローダーにカーネルを読み込む処理を実装する.調べてたら先駆者を見つけた.
https://natsutan.hatenablog.com/entry/2021/04/14/190451
この人はCの総研大アプリから錆のカーネルを読んでいるようだけど、自分は全部錆でやってみたい.
上記ブログの参考ブログは全部錆でやってた.
https://www.akiradeveloper.com/post/mikanos-rust/
このカーネルファイルは、0x100000に配置されるようにビルドされているため、 UEFIによってアドレス指定方式でページをアロケートして、そこに書きます。
これが全てっぽい.
自作OS本と https://github.com/akiradeveloper/MikanOS.rs/blob/master/boot/src/main.rs を見ながら進める.
一通り書いた時点で起動してみると、邱がパニックした.
ディレクトリ構成を修正すると、パニックせず起動できたようだ.
今日の進捗 -> https://github.com/ytannai/kabos/tree/8e5091cbb471ca660dab44091051d79d692692c2
Reference
この問題について(錆で自作OS 6日目), 我々は、より多くの情報をここで見つけました https://dev.to/ytannai/rust-os-6-l40テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol