[Haskell-cafe] [Help] Trying to rewrite the NetBSD kernel using Haskell language.

Kiwamu Okabe kiwamu at debian.or.jp
Mon Jan 20 13:55:36 UTC 2014

Hi Haskell hackers,

We start trying to rewrite NetBSD kernel using Haskell language.


But we find some problems.
Could you have any ideas for them?

## Status

* Trying rewrite AC''97 sound driver using Haskell
* Before rewriting. (C language)


* After rewriting. (Haskell language)


* Have rewrited 2 functions, auich_open() and auich_close()
* Not yet touch Haskell heap

## Problems

Some time, C programmer writes following code.

static int
auich_open(void *addr, int flags)
        struct auich_softc *sc;

        sc = (struct auich_softc *)addr;

Note the line "sc->codec_if->vtbl->lock(sc->codec_if);".
It traces pointer tree regionally.
Haskell's Strorable class is not good for the use case,
because it copies the entire pointer tree!

We avoid the problem using following messy code.


auichOpen :: Ptr AuichSoftc -> Int -> IO Int
auichOpen sc flags = do
  mutexp <- p_AuichSoftc_sc_intr_lock sc
  codeif <- peek =<< p_AuichSoftc_codec_if sc
  lock <- peek =<< p_Ac97CodecIfVtbl_lock =<< peek =<< p_Ac97CodecIf_vtbl codeif
  mutexSpinExit mutexp
  call_Ac97CodecIfVtbl_lock lock codeif
-- snip --
p_AuichSoftc_sc_intr_lock :: Ptr AuichSoftc -> IO (Ptr KmutexT)
p_AuichSoftc_sc_intr_lock p = return $ plusPtr p
p_AuichSoftc_codec_if :: Ptr AuichSoftc -> IO (Ptr (Ptr Ac97CodecIf))
p_AuichSoftc_codec_if p = return $ plusPtr p offsetOf_AuichSoftc_codec_if
p_Ac97CodecIf_vtbl :: Ptr Ac97CodecIf -> IO (Ptr (Ptr Ac97CodecIfVtbl))
p_Ac97CodecIf_vtbl p = return $ plusPtr p offsetOf_Ac97CodecIf_vtbl
p_Ac97CodecIfVtbl_lock :: Ptr Ac97CodecIfVtbl -> IO (Ptr (FunPtr
p_Ac97CodecIfVtbl_lock p = return $ plusPtr p offsetOf_Ac97CodecIfVtbl_lock

Haskell world has the answer for it already?

Best regards,
Kiwamu Okabe

More information about the Haskell-Cafe mailing list