ãã¼ãããã®OSèªä½å ¥éãã Rust ã§ãã (第7ç« )
å¼ãç¶ã Rust 㧠OS ãä½ã£ã¦ããã¾ãã ä»åã¯ãRust 1.39 ã§å®å®åããããã®æ©è½ãç»å ´ãã¾ãï¼ï¼ï¼
- ã·ãªã¼ãºæåã®è¨äº: 「ゼロからのOS自作入門」を Rust でやる (第1章~第4章) - gifnksmの雑多なメモ
- åå: 「ゼロからのOS自作入門」を Rust でやる (第6章 その2) - gifnksmの雑多なメモ
- 次å: 「ゼロからのOS自作入門」を Rust でやる (第9章) - gifnksmの雑多なメモ
- é¢é£è¨äºä¸è¦§: ゼロからのOS自作入門 カテゴリーの記事一覧 - gifnksmの雑多なメモ
第7ç«
å²ãè¾¼ã¿ãã³ããªã³ã°ã®ç« ã§ãã è¬ã®ç¾è±¡ãèµ·ãã¦ãªããªãå®è£ ã«è¦å´ããç« ã§ãã
å²ãè¾¼ã¿å¥æ©ã§ãã¦ã¹ã«ã¼ã½ã«ãåããããã«ãã (day07a)
6ç« ã¾ã§ã®å®è£ ã§ã¯ç¡éã«ã¼ãã§ãã¼ãªã³ã°ãããã¨ã§ xHC ã®ã¤ãã³ãçºçãæ¤ç¥ãã¦ãã¾ããã æ¬ç« ã§ã¯ xHCI ã§å®ããããå²ãè¾¼ã¿ã®çºçæ¹æ³ã§ããã MSI (Message Signaled Interrupts) ã«å¯¾å¿ãã MSI å²ãè¾¼ã¿ãåãåã£ãå¥æ©ã§ãã¦ã¹ã«ã¼ã½ã«ãåããããã«ãã¾ãã
C++å®è£ ã®éãã«å²ãè¾¼ã¿ãã³ãã©ãå®ç¾©ãã IDT (Interrupt Descriptor Table) ãè¨å®ãã PCI ã³ã³ãã£ã°ã¬ã¼ã·ã§ã³ç©ºéã®èªã¿æ¸ã㧠MSI ãè¨å®ãã¾ããã
ãã£ããQEMUã§åããã¦ã¿ãã¨ãããå²ãè¾¼ã¿å¼ã³åºãç®æ㧠Page Fault ã Double Fault ã Segment Not Present ããã®ä¾å¤ãçºçãã¦æ£ããåä½ãã¾ããã ãªã«ãèµ·ãã¦ããã®ãããåãããªãã®ã§ãããåä¾å¤çºçæã®æ å ±ãè¦ã¦æ³åããã«ãã©ãããã»ã°ã¡ã³ããããããã®ãåå ã®ããã§ãã
ã¨ãããã㧠GDT (Global Descriptor Table) ã§ã»ã°ã¡ã³ãè¨å®ããããã«ãã¦ã¿ã¾ããã ã³ã¼ãã»ã°ã¡ã³ããè¨å®ããã ãã§ã¯ã ãã ã£ãã®ã§ãã¹ã¿ãã¯ã»ã°ã¡ã³ããè¨å®ããã¨ããããã¾ãåä½ããããã«ãªãã¾ããã çå±ã¯å ¨ãç解ã§ãã¦ãã¾ããããã²ã¨ã¾ãåãããã«ãªã£ãã®ã§ã¨ã·ï¼ã¨ãããã¨ã§å ã«ãããããã¨æãã¾ãã
ãã¼ãã¸å¯¾å¿
C++ å®è£
ã§ãã¼ãã«å¯¾å¿ããã®ã¯ç¬¬9ç« ã§ããããã®æ¬¡ã«å®è£
ãããã®ã§ãã¼ããå¿
è¦ã ã£ãã®ã§å
ã«ãã¼ããå®è£
ãã¦ãã¾ãã¾ãã
C++ å®è£
ã§ã¯ brk
ãå®è£
ãããã¨ã§ newlib ã® malloc
ãå©ç¨ã§ããããã«ãã¦ããã®ã§ããã Rust ã§ã¯ããä»¥ä¸ newlib
ã«ä¾åããã®ã¯é¿ãããã£ãã®ã§ã "Writing an OS in Rust" ã® "Allocator Designs" ã®è¨äº ãåèã«ç¬èªã®ã¡ã¢ãªã¢ãã±ã¼ã¿ãä½æãã¾ãã (ã»ã¼ã»ã¼ã³ããã§ãã...)ã
ãã®ã¡ã¢ãªã¢ãã±ã¼ã¿å®è£
ã§ã¯ãã¼ãé åã¨ãããã¬ã¼ã ã®æ°ã¨åãåæ°ã ã FrameAllocator::allocate()
ãå¼ã³åºãã¾ãã
ä»åãã¼ãé å㯠128MiB ã¨ãã¦ããã®ã§å½è©²é¢æ°ã¯ 128MiB / 4KiB = 32,768 åå¼ã³åºããããã¨ã«ãªãã¾ãã
FrameAllocator
(BitmapMemoryManager
) ã®å®è£
ãã®ã¾ã¾ã§ã¯å¦çã«æéãæãããããOSã®èµ·åã«é·æéãããããã«ãªã£ã¦ãã¾ã£ãããã BitmapMemoryManager
ã®å®è£
ãé«éåãã¦ãã¾ãã
å
·ä½çã«ã¯ãå²ãå½ã¦å¯è½ãªãã¬ã¼ã ãæ¢ç´¢ããç¯å²ãæå³ãã BitmapMemoryAllocator::range
ã®å¤ãããã¬ã¼ã ã®å²ãå½ã¦ã®åº¦ã«æ´æ°ãããã¨ã§ä¸è¦ãªæ¢ç´¢ãåæ¸ããããã«ãã¦ãã¾ãã
(ãã¬ã¼ã ã®å²ãå½ã¦åæ° ã«å¯¾ããå¾æ¥ã¯ åå¦çãçºçãã¦ããã¨ããã åã®å¦çã§æ¸ãããã«æ¹åãã¾ãã)
ãã㧠Box
ã Rc
ã Arc
ã使ããããã«ãªãã¾ããã
BIOS ã§èµ·åããªããªã£ã¦ããã®ãä¿®æ£ãã (ä¿®æ£ã§ããªãã£ã)
ããã¾ã§éçºãã¦ããæ©è½ã¯ä¸»ã« UEFI ã®ãã¼ããã¼ãã¼ã§åä½ããã確èªãã¦ããã®ã§ããã BIOS ã®ãã¼ããã¼ãã¼ã§ç¢ºèªããã¨ãããèµ·åå¦çä¸ã« panic ãçºçããããã«ãªã£ã¦ãã¾ã£ã¦ãã¾ããã®ã§ä¿®æ£ãã¾ãã
ä¸ã¤ç®ã®åé¡ã¯ããã¼ããã¼ãã¼ã«æ¸¡ãããã¡ã¢ãªã®ãããã³ã°æ å ±ããåãã¬ã¼ã ã®ä½¿ç¨ç¶æ³ãæ´æ°ããå¦çã«ããã¾ããã å ·ä½çã«ã¯ãBIOS ã®ãã¼ããã¼ãã¼ãã渡ãããã¡ã¢ãªã®ãããã³ã°æ å ±ã«å«ã¾ããã¢ãã¬ã¹ããã¬ã¼ã å¢çã«æã£ã¦ããªãã£ãããã¨ã©ã¼ã¨ãªã£ã¦ããã®ã§ããã ãã®ã¢ãã¬ã¹ããã¬ã¼ã å¢çã«åãããã«ä¸¸ãããã¨ã§åé¡ã«å¯¾å¦ãã¾ããã ãªããä»ã®ç¨éã§ä½¿ããã¦ãããã¬ã¼ã ã使ç¨å¯è½ã¨ãã¦æ±ã£ã¦ã¯ãããªãããããã¬ã¼ã å ¨ä½ã使ç¨å¯è½ãªãã¬ã¼ã ã®ã¿ä½¿ç¨å¯è½ã¨ãã¦ãã¾ãã
ããã§ä¸ã¤ç®ã®åé¡ã¯è§£æ±ºããã®ã§ãããä¾ç¶ã¨ã㦠C++ å´å¦çã®ãã°åºåå¦ç㧠Segment Not Present ã®ä¾å¤ãçºçãã¦ãã¾ãã¾ãã
ã©ããæµ®åå°æ°é¢é£ã®ã¬ã¸ã¹ã¿ãæä½ãã¦ããç®æãåé¡ã§ã va_start
ãªã©ãé¢ä¿ãã¦ããããªæ°ãããã®ã§ãããåå ãããåãããªãã§ããã UEFI å´ã§ã¯åé¡ãªãåä½ãã¦ãããããªã®ã§ãã²ã¨ã¾ãæ¾ã£ã¦ãããã¨ã«ãã¾ãã
async/await ã使ããããã«ãã
"Writing an OS in Rust" ã® "Async/Await" ã®ç« ã«æéãåããã®ã§ã sabios ã«ãå®è£ ãã¾ãã å²ãè¾¼ã¿å¥æ©ã®ã¤ãã³ãå¦çã®ããã« async/await ã使ããã¨é常ã«ä¾¿å©ã§ç¶ºéºã«æ¸ããã¨æãã¾ãã ãããããããã£ããããã« MikanOS ã® Rust 移æ¤ãå§ããã¨è¨ã£ã¦ãéè¨ã§ã¯ããã¾ããã
Executor
çã®å®è£
㯠"Writing an OS in Rust" ã®ã»ã¼ã³ããã§ãã
ããã ãã®ã³ã¼ãã§éåæã©ã³ã¿ã¤ã ãä½ãã¦ãã¾ãã®ã¯ãããã§ãããã
ã¿ã¹ã¯ãæå³ããæ§é ä½ã®ååã¯ã CoTask
(Cooperative Task)ã¨ãã¾ããã
Task
ã§ã¯ãªã CoTask
ã¨ããã®ã¯ãå¾ã®ç« ã§åºã¦ããããªã¨ã³ããã£ããªã¿ã¹ã¯ã¨åºå¥ããããã§ãã
async/await ã§å²ãè¾¼ã¿ãå¦çãã (day07b)
å²ãè¾¼ã¿ãã³ãã©å ã§ã¤ãã³ãå¦çãè¡ã£ã¦ããã®ããå²ãè¾¼ã¿ãã³ãã©å ã§ã¯ã¤ãã³ããéç¥ãã¡ã¤ã³ã®ã¿ã¹ã¯ã§éç¥ãåãåãã¤ãã³ãå¦çããããã«å¤æ´ããç¯ã§ãã Rust å®è£ ã§ã¯ã°ãã¼ãã«ãªã¤ãã³ããã¥ã¼ã§ã¯ãªã async/await ã使ãã¾ãï¼
ä»åã¯å²ãè¾¼ã¿ãçºçãããå¦ãã¨ããæ
å ±ã ããå²ãè¾¼ã¿ãã³ãã©ãã CoTask
ã¸éç¥ããã°è¯ãã®ã§ããã¥ã¼ã§ã¯ãªã AtomicBool
ã§å²ãè¾¼ã¿çºçæç¡ãéç¥ããããã«ãã¾ããã
Ordering
㯠Relaxed
ã§è¯ã...ã¯ã... (ãã¾ãèªä¿¡ããªãã§ã)
ã¤ãã§ã«ãã¦ã¹ã«ã¼ã½ã«ç§»åæã®ã¤ãã³ãå¦çã«ã¤ãã¦ã CoTask
ã«ãã¾ããã
å¾æ¥å¦çã§ã¯ C++ ããå¼ã³åºãããã³ã¼ã«ããã¯é¢æ°å
㧠mouse_cursor
ã framebuffer
ã®ããã¯ãåå¾ãã¦ãããããããã¯é åºã®æ´åæ§ãã¨ãã¦ãããä¸å®ã ã£ãã®ã§ãããä»åã®æ¹é ã«ããã³ã¼ã«ããã¯é¢æ°ã®ä¸ã§ã¯ããã¯ãåå¾ããªããªã£ãã®ã§ãå®å¿åº¦ãä¸ããã¾ããã
ã¾ã¨ã
å²ãè¾¼ã¿ã«å¯¾å¿ããå²ãè¾¼ã¿å¥æ©ã§ãã¦ã¹ã«ã¼ã½ã«ãåãããããã«ãã¾ããã ã¾ãã async/await ã§å²ãè¾¼ã¿ãã¨ã¬ã¬ã³ãã«å¦çã§ããããã«ãã¾ããã ä¸æ¹ãBIOS ã®ãã¼ããã¼ãã¼ã§ã¯ãªããåä½ããªããªã£ã¦ãã¾ãã¾ãããè¬ã§ãããã
ãã¦ã8ç« ã®å 容ã¯å æ¥ãã£ã¦ãã¾ã£ãã®ã§ã次ã¯9ç« ã§ãã ã©ãã©ã OS ãé«æ©è½åãã¦ãã£ã¦ Rust ã§å®è£ ãã楽ãã¿ãå¢ãããã§ããï¼ æ¬¡åãã楽ãã¿ã«ã
- ã·ãªã¼ãºæåã®è¨äº: 「ゼロからのOS自作入門」を Rust でやる (第1章~第4章) - gifnksmの雑多なメモ
- åå: 「ゼロからのOS自作入門」を Rust でやる (第6章 その2) - gifnksmの雑多なメモ
- 次å: 「ゼロからのOS自作入門」を Rust でやる (第9章) - gifnksmの雑多なメモ
- é¢é£è¨äºä¸è¦§: ゼロからのOS自作入門 カテゴリーの記事一覧 - gifnksmの雑多なメモ