SlideShare a Scribd company logo
1 of 26
Download to read offline
Writing NetBSD Sound Drivers 
in Haskell 
Kiwamu Okabe @ Metasepi Design 
& Takayuki Muranushi @ RIKEN 
AICS
Demo: NetBSD driver in Haskell 
☆ NetBSD audio driver plays sound. 
☆ The driver's interrupt handler 
rewrited using Haskell. 
☆ GC occurs in interrupt handler. 
☆ Watch the movie at following. 
hhhttttttpppsss::://////wwwwwwwww...yyyooouuutttuuubbbeee...cccooommm///wwwaaatttccchhh???vvv===XXXEEEYYYcccRRR555RRRGGG555cccAAA 
☆ This slide: http://bit.ly/ajhc-icfp2014
Demo architecture
Kernel developers want type 
☆ Kernels are developed with C. 
☆ We should design kernel with the 
greatest care. 
☆ C language is unsafe!
Kernel Bug #1: Buffer overrun 
☆ Pointer to array doesn't know the 
length.
Kernel Bug #2: Page fault 
☆ Page fault in user space => SEGV 
☆ Page fault in kernel space => Halt!
Kernel Bug #3: Weak type 
☆ Lots of (void *) and unsafe coercion. 
☆ NetBSD kernel uses it 45130 times! 
$ pwd 
/home/kiwamu/src/netbsd/sys 
$ grep "void *" `find . -name "*.c"` | wc -l 
45130 
☆ Kernel developers frequently use 
(void *) for flexibility. It's realy BAD, but 
there is no other option.
Metasepi Project 
http://metasepi.org/ 
☆ Unix-like OS designed by strong type. 
☆ We want to use Metasepi OS for daily 
desktop usage (e.g. web browser, 
programming, office suite, ... etc.) 
☆ We have surveyed may functional 
languages (e.g. Haskell, OCaml, 
MLton, ... etc.)
Scratch or Rewrite
Snatch-driven development #1 
http://en.wikipedia.org/wiki/Snatcher
Snatch-driven development #2
Why we use jhc ? 
Comparison of programs to print "hoge" 
on terminal. The smaller the values, the 
lesser is the dependency on POSIX, the 
more suitable for system programming.
Unix-like OS needs reentrancy 
☆ Why we need reentrancy? 
☆ Because interrupt handler should be 
reentrant. 
☆ Why we need interrupt handler? 
☆ Because preemptive multitasking 
uses it. 
☆ Why we need the multitasking? 
☆ Because Unix-like OS depends on it.
What's reentrancy ? 
Reentrant code can be interrupted in the middle of its execution 
and then safely called again ("re-entered") before its previous 
invocations complete execution.
How do we get reentrancy in C ? 
☆ C language contexts are isolated.
What's C language Context ?
Problem: Interrupt and GC
Root of the problem 
☆ GHC's Haskell context is global and 
single. 
☆ There is only one GC heap on GHC. 
☆ If interrupt occurs while GC is 
running, the interrupt context can't use 
GC heap, because the running GC is 
paused! 
☆ Therefore GHC's binary isn't 
reentrant.
How we can fix this problem 
We re-define the Haskell Context.
Context-Local Heaps (CLHs) 
☆ Idea: Isolate contexts by local heap
What's Haskell Context on CLHs?
Haskell Context life cycle (CLHs)
Isolated contexts are reentrant?
Benchmark 
(O) Original NetBSD 6.1.2 kernel 
(S) The kernel includes AC'97 and HD Audio driver snatched by Ajhc 
(N) (S) + using naive GC 
(B4) (S) + having GC block 16 Byte 
(B5) (S) + having GC block 32 Byte 
(B6) (S) + having GC block 64 Byte
Thank's for contributors ! 
☆ Music is "signal" created by Epopsan. 
☆ John Meacham as jhc author.
Conclusion 
☆ Can we write Unix in Haskell? 
☆ => Yes! 
☆ How we realize Reentrant GC? 
☆ => With Context-Local Heaps (CLHs)! 
☆ Can we implement CLHs in other 
compilers? 
☆ => Yes! Of course, GHC can do it!

More Related Content

What's hot

Apache2 BootCamp : Serving Dynamic Content with CGI
Apache2 BootCamp : Serving Dynamic Content with CGIApache2 BootCamp : Serving Dynamic Content with CGI
Apache2 BootCamp : Serving Dynamic Content with CGI
Wildan Maulana
 
GraalVMの紹介とTruffleでPHPぽい言語を実装したら爆速だった話
GraalVMの紹介とTruffleでPHPぽい言語を実装したら爆速だった話GraalVMの紹介とTruffleでPHPぽい言語を実装したら爆速だった話
GraalVMの紹介とTruffleでPHPぽい言語を実装したら爆速だった話
なおき きしだ
 
tDiary annual report 2009 - Sapporo Ruby Kaigi02
tDiary annual report 2009 - Sapporo Ruby Kaigi02tDiary annual report 2009 - Sapporo Ruby Kaigi02
tDiary annual report 2009 - Sapporo Ruby Kaigi02
Hiroshi SHIBATA
 

What's hot (20)

Building GUI App with Electron and Lisp
Building GUI App with Electron and LispBuilding GUI App with Electron and Lisp
Building GUI App with Electron and Lisp
 
The art of concurrent programming
The art of concurrent programmingThe art of concurrent programming
The art of concurrent programming
 
PHP vs Node.js
PHP vs Node.jsPHP vs Node.js
PHP vs Node.js
 
Build Low-Latency Applications in Rust on ScyllaDB
Build Low-Latency Applications in Rust on ScyllaDBBuild Low-Latency Applications in Rust on ScyllaDB
Build Low-Latency Applications in Rust on ScyllaDB
 
RBD: What will the future bring? - Jason Dillaman
RBD: What will the future bring? - Jason DillamanRBD: What will the future bring? - Jason Dillaman
RBD: What will the future bring? - Jason Dillaman
 
what is docker
what is dockerwhat is docker
what is docker
 
Groovy a Scripting Language for Java
Groovy a Scripting Language for JavaGroovy a Scripting Language for Java
Groovy a Scripting Language for Java
 
Apache2 BootCamp : Serving Dynamic Content with CGI
Apache2 BootCamp : Serving Dynamic Content with CGIApache2 BootCamp : Serving Dynamic Content with CGI
Apache2 BootCamp : Serving Dynamic Content with CGI
 
Remote secured storage
Remote secured storageRemote secured storage
Remote secured storage
 
GraalVMの紹介とTruffleでPHPぽい言語を実装したら爆速だった話
GraalVMの紹介とTruffleでPHPぽい言語を実装したら爆速だった話GraalVMの紹介とTruffleでPHPぽい言語を実装したら爆速だった話
GraalVMの紹介とTruffleでPHPぽい言語を実装したら爆速だった話
 
tDiary annual report 2009 - Sapporo Ruby Kaigi02
tDiary annual report 2009 - Sapporo Ruby Kaigi02tDiary annual report 2009 - Sapporo Ruby Kaigi02
tDiary annual report 2009 - Sapporo Ruby Kaigi02
 
8 Ways Network Engineers use Snabb (RIPE 77)
8 Ways Network Engineers use Snabb (RIPE 77)8 Ways Network Engineers use Snabb (RIPE 77)
8 Ways Network Engineers use Snabb (RIPE 77)
 
Graal in GraalVM - A New JIT Compiler
Graal in GraalVM - A New JIT CompilerGraal in GraalVM - A New JIT Compiler
Graal in GraalVM - A New JIT Compiler
 
20140918 ruby kaigi2014
20140918 ruby kaigi201420140918 ruby kaigi2014
20140918 ruby kaigi2014
 
RISC-V on Edge: Porting EVE and Alpine Linux to RISC-V
RISC-V on Edge: Porting EVE and Alpine Linux to RISC-VRISC-V on Edge: Porting EVE and Alpine Linux to RISC-V
RISC-V on Edge: Porting EVE and Alpine Linux to RISC-V
 
Android kotlin coroutines
Android kotlin coroutinesAndroid kotlin coroutines
Android kotlin coroutines
 
Let's make it flow ... one way
Let's make it flow ... one wayLet's make it flow ... one way
Let's make it flow ... one way
 
PLAM 2015 - Evolving Backups Strategy, Devploying pyxbackup
PLAM 2015 - Evolving Backups Strategy, Devploying pyxbackupPLAM 2015 - Evolving Backups Strategy, Devploying pyxbackup
PLAM 2015 - Evolving Backups Strategy, Devploying pyxbackup
 
Evented Ruby VS Node.js
Evented Ruby VS Node.jsEvented Ruby VS Node.js
Evented Ruby VS Node.js
 
How to Measure Latency
How to Measure LatencyHow to Measure Latency
How to Measure Latency
 

Similar to Writing NetBSD Sound Drivers in Haskell

Functional MCU programming #0: Development environment
Functional MCU programming #0: Development environmentFunctional MCU programming #0: Development environment
Functional MCU programming #0: Development environment
Kiwamu Okabe
 
How to rewrite the OS using C by strong type
How to rewrite the OS using C by strong typeHow to rewrite the OS using C by strong type
How to rewrite the OS using C by strong type
Kiwamu Okabe
 
Dynamic Change Data Capture with Flink CDC and Consistent Hashing
Dynamic Change Data Capture with Flink CDC and Consistent HashingDynamic Change Data Capture with Flink CDC and Consistent Hashing
Dynamic Change Data Capture with Flink CDC and Consistent Hashing
HostedbyConfluent
 
Dynamic Change Data Capture with Flink CDC and Consistent Hashing
Dynamic Change Data Capture with Flink CDC and Consistent HashingDynamic Change Data Capture with Flink CDC and Consistent Hashing
Dynamic Change Data Capture with Flink CDC and Consistent Hashing
Yaroslav Tkachenko
 
High-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uringHigh-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uring
ScyllaDB
 

Similar to Writing NetBSD Sound Drivers in Haskell (20)

Functional MCU programming
Functional MCU programmingFunctional MCU programming
Functional MCU programming
 
Metasepi team meeting #7: Snatch application on tiny OS
Metasepi team meeting #7: Snatch application on tiny OSMetasepi team meeting #7: Snatch application on tiny OS
Metasepi team meeting #7: Snatch application on tiny OS
 
Metasepi team meeting #13: NetBSD driver using Haskell
Metasepi team meeting #13: NetBSD driver using HaskellMetasepi team meeting #13: NetBSD driver using Haskell
Metasepi team meeting #13: NetBSD driver using Haskell
 
Metasepi team meeting: Ajhc Project Overview
Metasepi team meeting: Ajhc Project OverviewMetasepi team meeting: Ajhc Project Overview
Metasepi team meeting: Ajhc Project Overview
 
Functional MCU programming #0: Development environment
Functional MCU programming #0: Development environmentFunctional MCU programming #0: Development environment
Functional MCU programming #0: Development environment
 
London Spark Meetup Project Tungsten Oct 12 2015
London Spark Meetup Project Tungsten Oct 12 2015London Spark Meetup Project Tungsten Oct 12 2015
London Spark Meetup Project Tungsten Oct 12 2015
 
Crystal internals (part 1)
Crystal internals (part 1)Crystal internals (part 1)
Crystal internals (part 1)
 
Crystal internals (part 1)
Crystal internals (part 1)Crystal internals (part 1)
Crystal internals (part 1)
 
Crystal internals (part 1)
Crystal internals (part 1)Crystal internals (part 1)
Crystal internals (part 1)
 
Madrid Spark Big Data Bluemix Meetup - Spark Versus Hadoop @ 100 TB Daytona G...
Madrid Spark Big Data Bluemix Meetup - Spark Versus Hadoop @ 100 TB Daytona G...Madrid Spark Big Data Bluemix Meetup - Spark Versus Hadoop @ 100 TB Daytona G...
Madrid Spark Big Data Bluemix Meetup - Spark Versus Hadoop @ 100 TB Daytona G...
 
How to rewrite the OS using C by strong type
How to rewrite the OS using C by strong typeHow to rewrite the OS using C by strong type
How to rewrite the OS using C by strong type
 
Lecture 1 Compiler design , computation
Lecture 1 Compiler design , computation Lecture 1 Compiler design , computation
Lecture 1 Compiler design , computation
 
C++ on the Web: Run your big 3D game in the browser
C++ on the Web: Run your big 3D game in the browserC++ on the Web: Run your big 3D game in the browser
C++ on the Web: Run your big 3D game in the browser
 
fpga1 - What is.pptx
fpga1 - What is.pptxfpga1 - What is.pptx
fpga1 - What is.pptx
 
HKG18-419 - OpenHPC on Ansible
HKG18-419 - OpenHPC on AnsibleHKG18-419 - OpenHPC on Ansible
HKG18-419 - OpenHPC on Ansible
 
Road to sbt 1.0: Paved with server (2015 Amsterdam)
Road to sbt 1.0: Paved with server (2015 Amsterdam)Road to sbt 1.0: Paved with server (2015 Amsterdam)
Road to sbt 1.0: Paved with server (2015 Amsterdam)
 
Dynamic Change Data Capture with Flink CDC and Consistent Hashing
Dynamic Change Data Capture with Flink CDC and Consistent HashingDynamic Change Data Capture with Flink CDC and Consistent Hashing
Dynamic Change Data Capture with Flink CDC and Consistent Hashing
 
Dynamic Change Data Capture with Flink CDC and Consistent Hashing
Dynamic Change Data Capture with Flink CDC and Consistent HashingDynamic Change Data Capture with Flink CDC and Consistent Hashing
Dynamic Change Data Capture with Flink CDC and Consistent Hashing
 
Compiler Construction | Lecture 1 | What is a compiler?
Compiler Construction | Lecture 1 | What is a compiler?Compiler Construction | Lecture 1 | What is a compiler?
Compiler Construction | Lecture 1 | What is a compiler?
 
High-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uringHigh-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uring
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 

Writing NetBSD Sound Drivers in Haskell

  • 1. Writing NetBSD Sound Drivers in Haskell Kiwamu Okabe @ Metasepi Design & Takayuki Muranushi @ RIKEN AICS
  • 2. Demo: NetBSD driver in Haskell ☆ NetBSD audio driver plays sound. ☆ The driver's interrupt handler rewrited using Haskell. ☆ GC occurs in interrupt handler. ☆ Watch the movie at following. hhhttttttpppsss::://////wwwwwwwww...yyyooouuutttuuubbbeee...cccooommm///wwwaaatttccchhh???vvv===XXXEEEYYYcccRRR555RRRGGG555cccAAA ☆ This slide: http://bit.ly/ajhc-icfp2014
  • 4. Kernel developers want type ☆ Kernels are developed with C. ☆ We should design kernel with the greatest care. ☆ C language is unsafe!
  • 5. Kernel Bug #1: Buffer overrun ☆ Pointer to array doesn't know the length.
  • 6. Kernel Bug #2: Page fault ☆ Page fault in user space => SEGV ☆ Page fault in kernel space => Halt!
  • 7. Kernel Bug #3: Weak type ☆ Lots of (void *) and unsafe coercion. ☆ NetBSD kernel uses it 45130 times! $ pwd /home/kiwamu/src/netbsd/sys $ grep "void *" `find . -name "*.c"` | wc -l 45130 ☆ Kernel developers frequently use (void *) for flexibility. It's realy BAD, but there is no other option.
  • 8. Metasepi Project http://metasepi.org/ ☆ Unix-like OS designed by strong type. ☆ We want to use Metasepi OS for daily desktop usage (e.g. web browser, programming, office suite, ... etc.) ☆ We have surveyed may functional languages (e.g. Haskell, OCaml, MLton, ... etc.)
  • 10. Snatch-driven development #1 http://en.wikipedia.org/wiki/Snatcher
  • 12. Why we use jhc ? Comparison of programs to print "hoge" on terminal. The smaller the values, the lesser is the dependency on POSIX, the more suitable for system programming.
  • 13. Unix-like OS needs reentrancy ☆ Why we need reentrancy? ☆ Because interrupt handler should be reentrant. ☆ Why we need interrupt handler? ☆ Because preemptive multitasking uses it. ☆ Why we need the multitasking? ☆ Because Unix-like OS depends on it.
  • 14. What's reentrancy ? Reentrant code can be interrupted in the middle of its execution and then safely called again ("re-entered") before its previous invocations complete execution.
  • 15. How do we get reentrancy in C ? ☆ C language contexts are isolated.
  • 16. What's C language Context ?
  • 18. Root of the problem ☆ GHC's Haskell context is global and single. ☆ There is only one GC heap on GHC. ☆ If interrupt occurs while GC is running, the interrupt context can't use GC heap, because the running GC is paused! ☆ Therefore GHC's binary isn't reentrant.
  • 19. How we can fix this problem We re-define the Haskell Context.
  • 20. Context-Local Heaps (CLHs) ☆ Idea: Isolate contexts by local heap
  • 22. Haskell Context life cycle (CLHs)
  • 23. Isolated contexts are reentrant?
  • 24. Benchmark (O) Original NetBSD 6.1.2 kernel (S) The kernel includes AC'97 and HD Audio driver snatched by Ajhc (N) (S) + using naive GC (B4) (S) + having GC block 16 Byte (B5) (S) + having GC block 32 Byte (B6) (S) + having GC block 64 Byte
  • 25. Thank's for contributors ! ☆ Music is "signal" created by Epopsan. ☆ John Meacham as jhc author.
  • 26. Conclusion ☆ Can we write Unix in Haskell? ☆ => Yes! ☆ How we realize Reentrant GC? ☆ => With Context-Local Heaps (CLHs)! ☆ Can we implement CLHs in other compilers? ☆ => Yes! Of course, GHC can do it!