Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
ベアメタル向けRust stdクレートの実装調査
Search
Kenta IDA
October 24, 2021
Programming
870
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
ベアメタル向けRust stdクレートの実装調査
TOPPERS開発者会議2021のLTで発表した内容のスライドです。
Kenta IDA
October 24, 2021
More Decks by Kenta IDA
See All by Kenta IDA
日本の家電とMatterの世界
ciniml
0
770
Matter開発環境をざっくり眺める
ciniml
0
2.4k
組込みRustでも でかい?JSONを扱いたい!
ciniml
3
1.5k
ATOMDisplay_DisplayModule_M5Stack_UG_2023_Tokyo.pdf
ciniml
1
2k
Rusty Stack-chanのすすめ
ciniml
0
830
Raspberry Pi Picoデバッガ使用のすすめ
ciniml
0
2.4k
ESP32でSORACOM Arcつないでみた
ciniml
2
1.5k
RustでCMSIS-DAP実装してみた。
ciniml
3
1.2k
技書博初出展してみた
ciniml
0
530
Other Decks in Programming
See All in Programming
個人開発者よ、Honoを使え(おじさんver)
tanahiro2010
2
160
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Rails World 2026, Austin, Texas
marcoroth
0
190
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
950
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
910
Kubernetes祭り #2 OP/ED
bells17
0
110
dotNET_vulnclicker_intro.pdf
deka_morita
0
130
IBM Bob でAI駆動開発『QC-Playbook』
ichirokano2
0
100
GitHubハンズオン講座 — 実務レベルのチーム開発のフローを身につけよう
junhat6
0
130
sbt 2.0: takeoff
eed3si9n
0
110
自サークルのIdPからログアウトできるようになるまで
taiseiue
1
130
Jetpack Compose Mechanisms
skydoves
2
650
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
590
Featured
See All Featured
What the history of the web can teach us about the future of AI
inesmontani
PRO
2
730
Designing for humans not robots
tammielis
254
26k
What's in a price? How to price your products and services
michaelherold
247
13k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
510
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
68
58k
Darren the Foodie - Storyboard
khoart
PRO
4
4k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
We Have a Design System, Now What?
morganepeng
55
8.4k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.8k
Typedesign – Prime Four
hannesfritz
42
3.2k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3.2k
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