1. Writing NetBSD Sound Drivers
Kiwamu Okabe @ Metasepi Design
& Takayuki Muranushi @ RIKEN
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.
☆ This slide: http://bit.ly/ajhc-icfp2014
3. Demo architecture
4. Kernel developers want type
☆ Kernels are developed with C.
☆ We should design kernel with the
☆ C language is unsafe!
5. Kernel Bug #1: Buffer overrun
☆ Pointer to array doesn't know the
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!
$ grep "void *" `find . -name "*.c"` | wc -l
☆ Kernel developers frequently use
(void *) for flexibility. It's realy BAD, but
there is no other option.
8. Metasepi Project
☆ 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.)
9. Scratch or Rewrite
10. Snatch-driven development #1
11. Snatch-driven development #2
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
☆ Why we need interrupt handler?
☆ Because preemptive multitasking
☆ 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 ?
17. Problem: Interrupt and GC
18. Root of the problem
☆ GHC's Haskell context is global and
☆ 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
☆ Therefore GHC's binary isn't
19. How we can fix this problem
We re-define the Haskell Context.
20. Context-Local Heaps (CLHs)
☆ Idea: Isolate contexts by local heap
21. What's Haskell Context on CLHs?
22. Haskell Context life cycle (CLHs)
23. Isolated contexts are reentrant?
(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.
☆ Can we write Unix in Haskell?
☆ => Yes!
☆ How we realize Reentrant GC?
☆ => With Context-Local Heaps (CLHs)!
☆ Can we implement CLHs in other
☆ => Yes! Of course, GHC can do it!