Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
RustでOS開発はじめの一歩
Search
nasa
December 19, 2023
Technology
7.6k
8
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
RustでOS開発はじめの一歩
https://uniquevision.connpass.com/event/303687/
nasa
December 19, 2023
More Decks by nasa
See All by nasa
難解な自己紹介プログラムを書く
nasa_desu
1
380
鉄は熱いうちに打て - Kaigi Effect LT大会
nasa_desu
2
580
リンカを変えてgo buildを 速く出来るか
nasa_desu
2
3.9k
ログから学ぶgo build
nasa_desu
4
1.5k
goのメモリアロケーターの話
nasa_desu
1
820
GoとRust - 並行処理編
nasa_desu
5
4.2k
Other Decks in Technology
See All in Technology
「気づいたら仕事が終わっている」バクラクAIエージェント本番運用の裏側 / layerx-bakuraku-aie2026
yuya4
19
11k
AI-DLCを活用した高品質・安全なAI駆動開発実践 / AI Driven Development
yoshidashingo
1
380
Agentic Defenseとともにセキュリティエンジニアが輝き続けるには / How Security Engineers Can Keep Excelling with Agentic Defense
yuj1osm
0
130
サイバーセキュリティ概論 / Introduction to Cybersecurity
ks91
PRO
0
170
Rubyで音を視る
ydah
1
110
MIERUNE JCT 発表資料「宇宙から伊能忠敬ごっこ」
syuchimu
0
190
エンジニアリング戦略の作り方 / Crafting Engineering Strategy
iwashi86
7
1.2k
DevOps Agentで始めるAWS運用 〜フロンティアエージェントが変える運用の現場〜
nyankotaro
1
320
Mastering Ruby Box
tagomoris
3
150
LLMにもCAP定理があるという話
harukasakihara
0
160
Platform engineering for developers, architects & the rest of us (AI agents)
danielbryantuk
0
190
新しいVibe Codingと”自走”について
watany
5
120
Featured
See All Featured
ラッコキーワード サービス紹介資料
rakko
1
3.6M
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
62
54k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
231
23k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.9k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
3.4k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
570
So, you think you're a good person
axbom
PRO
2
2.1k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
770
Making the Leap to Tech Lead
cromwellryan
135
9.9k
Product Roadmaps are Hard
iamctodd
PRO
55
12k
Prompt Engineering for Job Search
mfonobong
0
330
Transcript
RustでOS開発はじめの一歩 UV Study: Rust LT会 Dec. 19 2023 - nasa
発表のモチベ • 最近RustでOSを書いている • これまでCLIツールやライブラリをRustで実装してきた • OSだと普段書いているコードが普通には動作しない • どんな違いがあるのか話したい
動作環境 • platform: QEMU riscv64 • build target: riscv64gc-unknown-none-elf #
.cargo/config.toml [build] target = "riscv64gc-unknown-none-elf" [target.riscv64gc-unknown-none-elf] runner = """ qemu-system-riscv64 \ -machine virt \ -bios default \ --no-reboot \ -nographic \ -serial mon:stdio \ -kernel """
ベアメタル環境 • OSによりプログラムがホスティングされない • 自分の書いたプログラムを動かし始めるまでの環境がない • time, io, fsなどOSの上に構成されている標準機能が使えない •
メモリアロケーターも無いのでVec, String等が使えない
ベアメタル環境
main関数を動かす
main関数を動かす QEMU riscvのdefault biosは0x80200000を実行する
main関数を動かす プログラムを0x8020_0000に配置し呼出す
main関数を動かす リンカースクリプトで任意の アドレスにプログラムを配置する .entryシンボルを8020_0000に配置 OUTPUT_ARCH( "riscv" ) ENTRY( _entry )
SECTIONS { . = 0x80200000; .text : { *(.entry) *(.text .text.*) } .rodata : { *(.rdata .rodata .rodata.*) } .data : { *(.data .data.*) } .bss : { *(.bss bss.*) } }
main関数を動かす main.rsで.entryを定義 これで関数_entryが呼ばれる // src/main.rs #![no_std] #![no_main] #[no_mangle] #[link_section =
".entry"] pub fn _entry() { main(); } #[inline] fn main() { loop {} } #[panic_handler] fn panic(_info: &core::panic::PanicInfo) -> ! { loop {} }
関数呼び出しをサポートしたい
関数呼び出し みんな大好き関数呼び出し。 というかプログラムを書く上でほぼ必須 • 関数呼び出しが出来ない。。 • inlineオプションを付けると呼び出せる fn a() {
b() } fn b() { c() } pub fn _entry() { main(); } #[inline] fn main() { loop {} }
関数呼び出し • 関数呼び出しが出来ない。。 • inlineオプションを付けると呼び出せる • (正確には関数を呼ぶとローカル変数の値がおかしくなる) なぜこれらが起きるのか
関数呼び出し • 関数呼び出し時にスタック領域が確保される • しかしSPの初期値は0 • SPを初期化する必要があった
関数呼び出し // src/main.rs static INIT_SP: [u8; 4096 * 1024] =
[0; 4096 * 1024]; static STACK_SIZE: usize = 4096 * 1024; // 4MB pub unsafe fn _entry() { // NOTE: スタックポインタの初期値を設定する // NOTE: スタックは下位に伸びていくのでINIT_SP + STACK_SIZEを設定 しSTACK_SIZE分の領域を確保 asm!("la sp, INIT_SP", "ld a0, STACK_SIZE", "add sp, sp, a0",); main(); } SPを初期化 誰からも変更されないように配列を定義し末尾アドレスを利用
動的メモリ確保したい
動的メモリ確保 StringやVec,Boxが普通に使いたくなる これらはallocクレートに定義されている (stdが存在する場合はstdの一部)
動的メモリ確保 allocを使うにはメモリアロケーターを実装する必要がある (heaplessクレートを利用するとアロケーターの実装無しでString, Vec等が使 える)
メモリアロケーターの実装 global_allocatorディレクティブで登録出来る GlobalAllocトレイトを実装している必要がある #[global_allocator] static mut ALLOCATOR: BumpAllocator = BumpAllocator::new();
pub struct BumpAllocator { arena: RefCell<[u8; ARENA_SIZE]>, next: Cell<usize>, } unsafe impl GlobalAlloc for BumpAllocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8 {} unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} }
メモリアロケーターの実装 bump allocatorを採用 heapの始点から終点まで順次割り当 てていくだけ フラグメンテーションを一切考慮してい ない 実装が超簡単なので採用
まとめ
まとめ • 普段Rustを書く時に色んなものに乗っかっている事が分かった • OSの機能以外にブート、メモリアロケーター、スタックポインタを気にする 必要があった 「俺の戦いはこれからだ!!」
自己紹介 HN: nasa (Asan Kondo) nasaが欲しい • github: k-nasa •
x(twitter): nasa_desu お仕事: MLOpsっぽいこと、DSの生産性改善
(余談) 遭遇したバグたち • 一度出力した文字が出力できない 「Hel?o W?r?d.」が出力される ◦ 原因: SP初期化していないから •
カーネル起動時に有効化していないタイマ割り込みが発生して死ぬ ◦ 原因1: エラーコードがズレてた。。本当は`address misaligned` ◦ 原因2: アライメントを考慮していなかった • プロセス切り替え時にSPが意図通り切り替わらない ◦ 原因: 調査中。涙
ご清聴ありがとうございました!