Rustで作成した学習目的のx86_64 bare metalカーネルです。
VGA画面出力、割り込みハンドリング、PS/2キーボード入力、シェルコマンドに対応しています。
Welcome to Rust OS!
===================
Kernel initialization complete!
> help
=== Available Commands ===
help - Show this help
echo <text> - Print text
clear - Clear screen
exit - Shutdown kernel
> echo Hello World
Hello World
> exit
Goodbye!
| 項目 | 要件 |
|---|---|
| OS | macOS または Linux (Ubuntu 20.04+) |
| Rust | nightly toolchain |
| QEMU | qemu-system-x86_64 |
| RAM | 4GB以上推奨 |
# 1. リポジトリをクローン
git clone https://github.com/Aventern/rust-os.git
cd rust-os
# 2. セットアップスクリプトを実行(依存関係を自動インストール)
./setup.sh
# 3. 実行
./run_os.shセットアップスクリプトを使わない場合:
# Rust nightly をインストール
rustup toolchain install nightly
rustup default nightly
# 必要なコンポーネントをインストール
rustup component add rust-src
rustup component add llvm-tools-preview
# bootimage ツールをインストール
cargo install bootimage
# QEMU をインストール
# macOS:
brew install qemu
# Ubuntu:
sudo apt install qemu-system-x86# ビルド(bootimageを作成)
cargo bootimage
# QEMU で実行
./run_os.sh
# または直接実行
qemu-system-x86_64 \
-drive format=raw,file=target/x86_64-unknown-none/debug/bootimage-rust_os.bin \
-m 256M \
-nographic \
-serial stdio# websockify をインストール
pip3 install websockify
# noVNC をクローン
git clone --depth 1 https://github.com/novnc/noVNC.git novnc
# QEMU を VNC モードで起動
qemu-system-x86_64 \
-drive format=raw,file=target/x86_64-unknown-none/debug/bootimage-rust_os.bin \
-m 256M \
-vnc :0 \
-serial file:serial.log &
# websockify を起動
websockify --web=novnc 6080 localhost:5900 &
# ブラウザで開く
# http://localhost:6080/vnc.html?host=localhost&port=6080カーネルの起動ログはシリアルポートに出力されます:
=== Rust OS Boot ===
[OK] VGA initialized
[..] Initializing IDT...
[OK] IDT initialized
[..] Initializing PIC...
[OK] PIC initialized
[..] Enabling interrupts...
[OK] Interrupts enabled
[OK] Kernel ready
[..] Entering main loop
rust-os/
├── src/
│ ├── main.rs # カーネルエントリーポイント (_start)
│ ├── lib.rs # ライブラリ + println!/serial_println! マクロ
│ ├── vga_buffer.rs # VGA テキストモード出力 (80x25)
│ ├── serial.rs # シリアルポート出力 (COM1, 0x3f8)
│ ├── interrupts.rs # IDT設定、割り込みハンドラー
│ ├── keyboard.rs # PS/2 キーボードドライバー
│ └── keyboard_input.rs # シェルコマンド処理
├── .cargo/
│ └── config.toml # ビルド設定(ターゲット、rustflags)
├── Cargo.toml # 依存関係
├── setup.sh # 自動セットアップ(OS自動判定)
├── setup_macos.sh # macOS用セットアップ
├── setup_linux.sh # Linux用セットアップ
└── run_os.sh # QEMU実行スクリプト
- x86_64 bare metal ターゲット
- bootloader 0.9 統合
- VGA テキストバッファ出力
- シリアルポート出力(デバッグログ)
- println! / serial_println! マクロ
- パニックハンドラー
- 割り込みハンドリング(IDT, PIC)
- PS/2 キーボードドライバー
- シェルコマンド(help, echo, clear, exit)
rustup component add rust-src --toolchain nightly.cargo/config.toml に以下が設定されていることを確認:
[target.x86_64-unknown-none]
rustflags = ["-C", "relocation-model=static", "-C", "link-arg=-no-pie"]これがないと、カーネルがPIE(Position Independent Executable)としてコンパイルされ、 bootloader 0.9 がメモリマッピングに失敗してパニックします。
# bootimage が作成されているか確認
ls -la target/x86_64-unknown-none/debug/bootimage-rust_os.bin
# なければ再ビルド
cargo clean
cargo bootimage-nographic -serial stdio オプションを使用するか、-serial file:serial.log でファイルに出力:
qemu-system-x86_64 \
-drive format=raw,file=target/x86_64-unknown-none/debug/bootimage-rust_os.bin \
-m 256M \
-serial file:serial.log &
cat serial.log- Writing an OS in Rust - Phil Oppのチュートリアル
- x86_64 crate - x86_64 CPU機能のRustラッパー
- bootloader crate - Rustカーネル用ブートローダー
MIT License