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 stdクレートの実装調査
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Kenta IDA
October 24, 2021
Programming
2
810
ベアメタル向けRust stdクレートの実装調査
TOPPERS開発者会議2021のLTで発表した内容のスライドです。
Kenta IDA
October 24, 2021
Tweet
Share
More Decks by Kenta IDA
See All by Kenta IDA
日本の家電とMatterの世界
ciniml
0
460
Matter開発環境をざっくり眺める
ciniml
0
2.2k
組込みRustでも でかい?JSONを扱いたい!
ciniml
3
1.4k
ATOMDisplay_DisplayModule_M5Stack_UG_2023_Tokyo.pdf
ciniml
1
1.8k
Rusty Stack-chanのすすめ
ciniml
0
720
Raspberry Pi Picoデバッガ使用のすすめ
ciniml
0
2.3k
ESP32でSORACOM Arcつないでみた
ciniml
2
1.4k
RustでCMSIS-DAP実装してみた。
ciniml
3
1.1k
技書博初出展してみた
ciniml
0
460
Other Decks in Programming
See All in Programming
Windows on Ryzen and I
seosoft
0
290
nuget-server - あなたが必要だったNuGetサーバー
kekyo
PRO
0
250
grapheme_strrev関数が採択されました(あと雑感)
youkidearitai
PRO
1
220
社内規程RAGの精度を73.3% → 100%に改善した話
oharu121
13
8k
encoding/json/v2のUnmarshalはこう変わった:内部実装で見る設計改善
kurakura0916
0
410
技術検証結果の整理と解析をAIに任せよう!
keisukeikeda
0
120
AI時代のソフトウェア開発でも「人が仕様を書く」から始めよう-医療IT現場での実践とこれから
koukimiura
0
150
DevinとClaude Code、SREの現場で使い倒してみた件
karia
1
1k
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
480
The Ralph Wiggum Loop: First Principles of Autonomous Development
sembayui
0
3.7k
PostgreSQL を使った快適な go test 環境を求めて
otakakot
0
550
Angular-Apps smarter machen mit Gen AI: Lokal und offlinefähig - Hands-on Workshop!
christianliebel
PRO
0
110
Featured
See All Featured
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
1
1.3k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
150
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
110k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
390
Abbi's Birthday
coloredviolet
2
5.3k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
72
Building a Modern Day E-commerce SEO Strategy
aleyda
45
8.9k
Reality Check: Gamification 10 Years Later
codingconduct
0
2k
Building the Perfect Custom Keyboard
takai
2
710
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
120
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
960
How to train your dragon (web standard)
notwaldorf
97
6.6k
Transcript
ベアメタル向け Rust std クレートの実装調査 TOPPERS開発者会議2021 LT 2021/10/24 Kenta Ida (@ciniml)
自己紹介 •井田 健太 (@ciniml) •仕事:FPGAの論理設計 •使用言語:C++, SystemVerilog, Rust, C# •組込みRust本の1割くらい書きました
(サンプルアプリ作ったり、デバッガ試したり)→ 2021/10/24 ベアメタル向けRust std クレートの実装調査 2
ベアメタルRust 2021/10/24 ベアメタル向けRust std クレートの実装調査 •OSが無い環境 (ベアメタル) 向けのソフトウェアを Rustで開発する方法 •OS機能に依存している
std クレート無しでの開発 • core, alloc クレートのみに依存 • #[no_std] 3
stdクレートが提供する機能 2021/10/24 ベアメタル向けRust std クレートの実装調査 •process - プロセス操作 •thread -
スレッド操作 •fs - ファイル操作 • path - パス操作 •net - TCP/UDP通信 •io - I/O関連 4
no_stdの難点 2021/10/24 ベアメタル向けRust std クレートの実装調査 • stdクレートの提供する機能が使えない • stdクレートに依存しているクレートが使えない (e.g.
ネットワーク周りなど) • no_std対応クレートのみ使用可 • 一方、stdクレートの機能は ベアメタルの組込みシステムでも一般的な機能 • ファイル操作 • TCP/UDP通信 • スレッド (タスク) • RTOSがある場合 5
stdクレートのベアメタル向け実装 2021/10/24 ベアメタル向けRust std クレートの実装調査 •そもそもstdクレートが提供する機能が ベアメタル向けのフレームワークにあるのであれば stdクレートも実装できるはずでは? • というか、
今日のメインセッションでSOLID向け実装されてましたね。 • 今朝stdのソースを確認していて気づきました 6
stdクレートの構造 2021/10/24 ベアメタル向けRust std クレートの実装調査 •各トップレベルのモジュールは sys, sys_common, os サブモジュールを使って実装
• sys - 各プラットフォーム向けのstdクレート実装の実体 • sys_common - 各プラットフォームで共通の処理の実装 • socket APIによる通信処理など • os - OS固有の処理の実装 ... ├── os │ ├── espidf │ ├── linux │ ├── solid │ ├── unix │ ├── vxworks │ ├── wasi │ └── windows ├── sys │ ├── common │ ├── itron │ ├── solid │ ├── unix │ ├── unsupported │ ├── wasi │ └── windows ├── sys_common │ ├── fs.rs │ ├── io.rs │ ├── net.rs │ ├── thread.rs ... 7
stdクレートの依存関係 2021/10/24 ベアメタル向けRust std クレートの実装調査 • I/O周り • 大体のプラットフォームで libc
クレートにあるlibcが提供する関数を使って実装されて いる • net周り • 大体socket APIで実装されている • thread周り • OS依存 • POSIX向け - pthreads • つまり、libc, socket, pthreadsがある環境であれば それぞれの部分は比較的容易に実装可能…なはず 8
ESP32の開発環境 2021/10/24 ベアメタル向けRust std クレートの実装調査 •ESP32 • 中国Espressifが製造している無線機能付きMCU • ESP32
- CPUはXtensa LX6 (カスタム版rustcで対応) • ESP32-C3 - CPUはRISC-V RV32IMC 9
ESP32の開発環境 2021/10/24 ベアメタル向けRust std クレートの実装調査 •ESP-IDF • ESP32シリーズ向けの公式フレームワーク • C/C++で開発可能
• FreeRTOS, lwIP, mbedtlsを含む • libcとしてnewlibを使用 • pthreadsにある程度対応 • C++11のthread対応のために元々実装されていた。 • FreeRTOSの機能で実装 10
ESP32のRust対応 2021/10/24 ベアメタル向けRust std クレートの実装調査 • 2018年頃からLLVMをforkしてclang対応 • LLVMはXtensaに対応していない •
有志がfork版LLVMをつかってrustcをXtensa対応 • fork版LLVMの更新は継続中 • 最新のLLVMに対応 • upstreamにパッチを投げているが2021/5より進捗なし • RISC-V版ESP32 (ESP32-C3)登場後 stdクレート対応 • 元々第3者による開発だったが、開発者はEspressifに入った模様 • ESP-IDF向けだったので、Xtensa版でも動作する 11
ESP-IDF向けstdクレート 2021/10/24 ベアメタル向けRust std クレートの実装調査 •newlibによるlibc機能 unix向けに条件分岐を追加して対応 • https://github.com/rust-lang/rust/pull/87666 •libcクレートをESP-IDFのnewlib,
lwIPに合わせて修正 • https://github.com/rust-lang/libc/pull/2310 12
libcクレートの修正 (一部) 2021/10/24 ベアメタル向けRust std クレートの実装調査 •newlib向け実装 (src/newlib) を修正 •
ESP-IDF向け実装を追加 (src/newlib/espidf) •lwIP向けに定義を分岐 (例: bind関数 → lwip_bind) #[cfg_attr(target_os = "espidf", link_name = "lwip_bind")] pub fn bind(fd: ::c_int, addr: *const sockaddr, len: socklen_t) ‐> ::c_int; cfg_if! { if #[cfg(target_os = "espidf")] { mod espidf; pub use self::espidf::*; 13
stdクレートの修正 (一部) 2021/10/24 ベアメタル向けRust std クレートの実装調査 •processをサポートしないのでダミーの定義を追加 impl Command {
pub fn spawn( &mut self, default: Stdio, needs_stdin: bool, ) ‐> io::Result<(Process, StdioPipes)> { unsupported() // Unsupportedを返す } 14
stdクレートの修正 (一部) 2021/10/24 ベアメタル向けRust std クレートの実装調査 •thread周り • スタックサイズなどが違うので条件分岐 •
sleepの実装を分岐 #[cfg(target_os = "espidf")] pub fn sleep(dur: Duration) { let mut micros = dur.as_micros(); unsafe { while micros > 0 { let st = if micros > u32::MAX as u128 { u32::MAX } else { micros as u32 }; libc::usleep(st); micros ‐= st as u128; } } } 15
stdクレート実装を眺めた雑感 2021/10/24 ベアメタル向けRust std クレートの実装調査 •思ったよりも実装量は多くなさそう • libc, lwIP, pthreadsがあれば
• unixモジュールに対してひたすら条件分岐追加 • それ以外の場合は対応した実装が必要 •使いたいプラットフォーム向けの実装をしてみたい • ESP32向けは既に公式で出たので… 16
おしまい 2021/10/24 ベアメタル向けRust std クレートの実装調査 17