writing netbsd sound drivers in haskell

26
Writing NetBSD Sound Drivers in Haskell Writing NetBSD Sound Drivers in Haskell Writing NetBSD Sound Drivers in Haskell Writing NetBSD Sound Drivers in Haskell Writing NetBSD Sound Drivers in Haskell Kiwamu Okabe @ Metasepi Design & Takayuki Muranushi @ RIKEN AICS Kiwamu Okabe @ Metasepi Design & Takayuki Muranushi @ RIKEN AICS Kiwamu Okabe @ Metasepi Design & Takayuki Muranushi @ RIKEN AICS Kiwamu Okabe @ Metasepi Design & Takayuki Muranushi @ RIKEN AICS Kiwamu Okabe @ Metasepi Design & Takayuki Muranushi @ RIKEN AICS

Upload: kiwamu-okabe

Post on 15-Jan-2015

1.603 views

Category:

Technology


8 download

DESCRIPTION

Presentaion at Haskell Symposium 2014.

TRANSCRIPT

Writing NetBSD Sound Drivers in Haskell

Writing NetBSD Sound Drivers in Haskell

Writing NetBSD Sound Drivers in Haskell

Writing NetBSD Sound Drivers in Haskell

Writing NetBSD Sound Drivers in Haskell

Kiwamu Okabe @ Metasepi Design & Takayuki Muranushi @ RIKEN

AICS

Kiwamu Okabe @ Metasepi Design & Takayuki Muranushi @ RIKEN

AICS

Kiwamu Okabe @ Metasepi Design & Takayuki Muranushi @ RIKEN

AICS

Kiwamu Okabe @ Metasepi Design & Takayuki Muranushi @ RIKEN

AICS

Kiwamu Okabe @ Metasepi Design & Takayuki Muranushi @ RIKEN

AICS

Demo: NetBSD driver in HaskellDemo: NetBSD driver in HaskellDemo: NetBSD driver in HaskellDemo: NetBSD driver in HaskellDemo: NetBSD driver in Haskell

☆ NetBSD audio driver plays sound.☆ NetBSD audio driver plays sound.☆ NetBSD audio driver plays sound.☆ NetBSD audio driver plays sound.☆ NetBSD audio driver plays sound.

☆ The driver's interrupt handler rewrited using Haskell.☆ The driver's interrupt handler rewrited using Haskell.☆ The driver's interrupt handler rewrited using Haskell.☆ The driver's interrupt handler rewrited using Haskell.☆ The driver's interrupt handler rewrited using Haskell.

☆ GC occurs in interrupt handler.☆ GC occurs in interrupt handler.☆ GC occurs in interrupt handler.☆ GC occurs in interrupt handler.☆ GC occurs in interrupt handler.

☆ Watch the movie at following.☆ Watch the movie at following.☆ Watch the movie at following.☆ Watch the movie at following.☆ Watch the movie at following.https://www.youtube.com/watch?v=XEYcR5RG5cAhttps://www.youtube.com/watch?v=XEYcR5RG5cAhttps://www.youtube.com/watch?v=XEYcR5RG5cAhttps://www.youtube.com/watch?v=XEYcR5RG5cAhttps://www.youtube.com/watch?v=XEYcR5RG5cA

☆ This slide: http://bit.ly/ajhc-icfp2014☆ This slide: http://bit.ly/ajhc-icfp2014☆ This slide: http://bit.ly/ajhc-icfp2014☆ This slide: http://bit.ly/ajhc-icfp2014☆ This slide: http://bit.ly/ajhc-icfp2014

Demo architectureDemo architectureDemo architectureDemo architectureDemo architecture

Kernel developers want typeKernel developers want typeKernel developers want typeKernel developers want typeKernel developers want type

☆ Kernels are developed with C.☆ Kernels are developed with C.☆ Kernels are developed with C.☆ Kernels are developed with C.☆ Kernels are developed with C.

☆ We should design kernel with the greatest care.☆ We should design kernel with the greatest care.☆ We should design kernel with the greatest care.☆ We should design kernel with the greatest care.☆ We should design kernel with the greatest care.

☆ C language is unsafe!☆ C language is unsafe!☆ C language is unsafe!☆ C language is unsafe!☆ C language is unsafe!

Kernel Bug #1: Buffer overrunKernel Bug #1: Buffer overrunKernel Bug #1: Buffer overrunKernel Bug #1: Buffer overrunKernel Bug #1: Buffer overrun

☆ Pointer to array doesn't know the length.☆ Pointer to array doesn't know the length.☆ Pointer to array doesn't know the length.☆ Pointer to array doesn't know the length.☆ Pointer to array doesn't know the length.

Kernel Bug #2: Page faultKernel Bug #2: Page faultKernel Bug #2: Page faultKernel Bug #2: Page faultKernel Bug #2: Page fault

☆ Page fault in user space => SEGV☆ Page fault in user space => SEGV☆ Page fault in user space => SEGV☆ Page fault in user space => SEGV☆ Page fault in user space => SEGV

☆ Page fault in kernel space => Halt!☆ Page fault in kernel space => Halt!☆ Page fault in kernel space => Halt!☆ Page fault in kernel space => Halt!☆ Page fault in kernel space => Halt!

Kernel Bug #3: Weak typeKernel Bug #3: Weak typeKernel Bug #3: Weak typeKernel Bug #3: Weak typeKernel Bug #3: Weak type

☆ Lots of (void *) and unsafe coercion.☆ Lots of (void *) and unsafe coercion.☆ Lots of (void *) and unsafe coercion.☆ Lots of (void *) and unsafe coercion.☆ Lots of (void *) and unsafe coercion.

☆ NetBSD kernel uses it 45130 times!☆ NetBSD kernel uses it 45130 times!☆ NetBSD kernel uses it 45130 times!☆ NetBSD kernel uses it 45130 times!☆ NetBSD kernel uses it 45130 times!$ pwd/home/kiwamu/src/netbsd/sys$ grep "void \*" `find . -name "*.c"` | wc -l45130

$ pwd/home/kiwamu/src/netbsd/sys$ grep "void \*" `find . -name "*.c"` | wc -l45130

$ pwd/home/kiwamu/src/netbsd/sys$ grep "void \*" `find . -name "*.c"` | wc -l45130

$ pwd/home/kiwamu/src/netbsd/sys$ grep "void \*" `find . -name "*.c"` | wc -l45130

$ pwd/home/kiwamu/src/netbsd/sys$ grep "void \*" `find . -name "*.c"` | wc -l45130

☆ Kernel developers frequently use (void *) for flexibility. It's realy BAD, but there is no other option.

☆ Kernel developers frequently use (void *) for flexibility. It's realy BAD, but there is no other option.

☆ Kernel developers frequently use (void *) for flexibility. It's realy BAD, but there is no other option.

☆ Kernel developers frequently use (void *) for flexibility. It's realy BAD, but there is no other option.

☆ Kernel developers frequently use (void *) for flexibility. It's realy BAD, but there is no other option.

Metasepi ProjectMetasepi ProjectMetasepi ProjectMetasepi ProjectMetasepi Project

http://metasepi.org/http://metasepi.org/http://metasepi.org/http://metasepi.org/http://metasepi.org/

☆ Unix-like OS designed by strong type.☆ Unix-like OS designed by strong type.☆ Unix-like OS designed by strong type.☆ Unix-like OS designed by strong type.☆ 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 want to use Metasepi OS for daily desktop usage (e.g. web browser, programming, office suite, ... etc.)

☆ We want to use Metasepi OS for daily desktop usage (e.g. web browser, programming, office suite, ... etc.)

☆ We want to use Metasepi OS for daily desktop usage (e.g. web browser, programming, office suite, ... etc.)

☆ 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.)

☆ We have surveyed may functional languages (e.g. Haskell, OCaml, MLton, ... etc.)

☆ We have surveyed may functional languages (e.g. Haskell, OCaml, MLton, ... etc.)

☆ We have surveyed may functional languages (e.g. Haskell, OCaml, MLton, ... etc.)

☆ We have surveyed may functional languages (e.g. Haskell, OCaml, MLton, ... etc.)

Scratch or RewriteScratch or RewriteScratch or RewriteScratch or RewriteScratch or Rewrite

Snatch-driven development #1Snatch-driven development #1Snatch-driven development #1Snatch-driven development #1Snatch-driven development #1

http://en.wikipedia.org/wiki/Snatcherhttp://en.wikipedia.org/wiki/Snatcherhttp://en.wikipedia.org/wiki/Snatcherhttp://en.wikipedia.org/wiki/Snatcherhttp://en.wikipedia.org/wiki/Snatcher

Snatch-driven development #2Snatch-driven development #2Snatch-driven development #2Snatch-driven development #2Snatch-driven development #2

Why we use jhc ?Why we use jhc ?Why we use jhc ?Why we use jhc ?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.

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.

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.

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.

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 reentrancyUnix-like OS needs reentrancyUnix-like OS needs reentrancyUnix-like OS needs reentrancyUnix-like OS needs reentrancy

☆ Why we need reentrancy?☆ Why we need reentrancy?☆ Why we need reentrancy?☆ Why we need reentrancy?☆ Why we need reentrancy?

☆ Because interrupt handler should be reentrant.☆ Because interrupt handler should be reentrant.☆ Because interrupt handler should be reentrant.☆ Because interrupt handler should be reentrant.☆ Because interrupt handler should be reentrant.

☆ Why we need interrupt handler?☆ Why we need interrupt handler?☆ Why we need interrupt handler?☆ Why we need interrupt handler?☆ Why we need interrupt handler?

☆ Because preemptive multitasking uses it.☆ Because preemptive multitasking uses it.☆ Because preemptive multitasking uses it.☆ Because preemptive multitasking uses it.☆ Because preemptive multitasking uses it.

☆ Why we need the multitasking?☆ Why we need the multitasking?☆ Why we need the multitasking?☆ Why we need the multitasking?☆ Why we need the multitasking?

☆ Because Unix-like OS depends on it.☆ Because Unix-like OS depends on it.☆ Because Unix-like OS depends on it.☆ Because Unix-like OS depends on it.☆ Because Unix-like OS depends on it.

What's reentrancy ?What's reentrancy ?What's reentrancy ?What's reentrancy ?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.

Reentrant code can be interrupted in the middle of its execution and then safely called again ("re-entered") before its previous invocations complete execution.

Reentrant code can be interrupted in the middle of its execution and then safely called again ("re-entered") before its previous invocations complete execution.

Reentrant code can be interrupted in the middle of its execution and then safely called again ("re-entered") before its previous invocations complete execution.

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 ?How do we get reentrancy in C ?How do we get reentrancy in C ?How do we get reentrancy in C ?How do we get reentrancy in C ?

☆ C language contexts are isolated.☆ C language contexts are isolated.☆ C language contexts are isolated.☆ C language contexts are isolated.☆ C language contexts are isolated.

What's C language Context ?What's C language Context ?What's C language Context ?What's C language Context ?What's C language Context ?

Problem: Interrupt and GCProblem: Interrupt and GCProblem: Interrupt and GCProblem: Interrupt and GCProblem: Interrupt and GC

Root of the problemRoot of the problemRoot of the problemRoot of the problemRoot of the problem

☆ GHC's Haskell context is global and single.☆ GHC's Haskell context is global and single.☆ GHC's Haskell context is global and single.☆ GHC's Haskell context is global and single.☆ GHC's Haskell context is global and single.

☆ There is only one GC heap on GHC.☆ There is only one GC heap on GHC.☆ There is only one GC heap on GHC.☆ There is only one GC heap on GHC.☆ 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!

☆ If interrupt occurs while GC is running, the interrupt context can't use GC heap, because the running GC is paused!

☆ If interrupt occurs while GC is running, the interrupt context can't use GC heap, because the running GC is paused!

☆ If interrupt occurs while GC is running, the interrupt context can't use GC heap, because the running GC is paused!

☆ 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.☆ Therefore GHC's binary isn't reentrant.☆ Therefore GHC's binary isn't reentrant.☆ Therefore GHC's binary isn't reentrant.☆ Therefore GHC's binary isn't reentrant.

How we can fix this problemHow we can fix this problemHow we can fix this problemHow we can fix this problemHow we can fix this problem

We re-define the Haskell Context.We re-define the Haskell Context.We re-define the Haskell Context.We re-define the Haskell Context.We re-define the Haskell Context.

Context-Local Heaps (CLHs)Context-Local Heaps (CLHs)Context-Local Heaps (CLHs)Context-Local Heaps (CLHs)Context-Local Heaps (CLHs)

☆ Idea: Isolate contexts by local heap☆ Idea: Isolate contexts by local heap☆ Idea: Isolate contexts by local heap☆ Idea: Isolate contexts by local heap☆ Idea: Isolate contexts by local heap

What's Haskell Context on CLHs?What's Haskell Context on CLHs?What's Haskell Context on CLHs?What's Haskell Context on CLHs?What's Haskell Context on CLHs?

Haskell Context life cycle (CLHs)Haskell Context life cycle (CLHs)Haskell Context life cycle (CLHs)Haskell Context life cycle (CLHs)Haskell Context life cycle (CLHs)

Isolated contexts are reentrant?Isolated contexts are reentrant?Isolated contexts are reentrant?Isolated contexts are reentrant?Isolated contexts are reentrant?

BenchmarkBenchmarkBenchmarkBenchmarkBenchmark(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

(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

(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

(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

(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 !Thank's for contributors !Thank's for contributors !Thank's for contributors !Thank's for contributors !

☆ Music is "signal" created by Epopsan.☆ Music is "signal" created by Epopsan.☆ Music is "signal" created by Epopsan.☆ Music is "signal" created by Epopsan.☆ Music is "signal" created by Epopsan.

☆ John Meacham as jhc author.☆ John Meacham as jhc author.☆ John Meacham as jhc author.☆ John Meacham as jhc author.☆ John Meacham as jhc author.

ConclusionConclusionConclusionConclusionConclusion

☆ Can we write Unix in Haskell?☆ Can we write Unix in Haskell?☆ Can we write Unix in Haskell?☆ Can we write Unix in Haskell?☆ Can we write Unix in Haskell?

☆ => Yes!☆ => Yes!☆ => Yes!☆ => Yes!☆ => Yes!

☆ How we realize Reentrant GC?☆ How we realize Reentrant GC?☆ How we realize Reentrant GC?☆ How we realize Reentrant GC?☆ How we realize Reentrant GC?

☆ => With Context-Local Heaps (CLHs)!☆ => With Context-Local Heaps (CLHs)!☆ => With Context-Local Heaps (CLHs)!☆ => With Context-Local Heaps (CLHs)!☆ => With Context-Local Heaps (CLHs)!

☆ Can we implement CLHs in other compilers?☆ Can we implement CLHs in other compilers?☆ Can we implement CLHs in other compilers?☆ Can we implement CLHs in other compilers?☆ Can we implement CLHs in other compilers?

☆ => Yes! Of course, GHC can do it!☆ => Yes! Of course, GHC can do it!☆ => Yes! Of course, GHC can do it!☆ => Yes! Of course, GHC can do it!☆ => Yes! Of course, GHC can do it!