This document discusses writing NetBSD sound drivers in Haskell. It describes a demo where a NetBSD audio driver that plays sound has its interrupt handler rewritten in Haskell. The interrupt handler uses garbage collection, which can occur within the interrupt context. It also discusses issues with C kernels like buffer overruns and weak typing, and how the Metasepi project aims to build an OS with strong typing using a functional language like Haskell. It presents context-local heaps as a way to achieve reentrancy in Haskell by isolating contexts to local garbage collected heaps.
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.)
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.
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.
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!