SlideShare a Scribd company logo
1 of 37
Download to read offline
Metasepi team meeting #7: 
#7:
Snatch application on tiny OS
Kiwamu Okabe
Who am I?
☆ http://www.masterq.net/
☆ Twitter: @master_q
☆ Organizer of Metasepi project
☆ A developer of Ajhc Haskell compiler
☆ A Debian Maintainer
☆ 10 years' experience in developing
OS using NetBSD.
Agenda
☆ [1] Demo
☆ [2] What is Ajhc?
☆ [3] What is Metasepi?
☆ [4] What is compiler to build OS
☆ [5] How to use Ajhc
☆ [6] Snatch application on tiny OS
☆ [7] Let's Snatch it!
[1] Demo
☆ RSS reader running on mbed (ARM).
☆ Show reddit articles on LCD display.
☆ You can watch the movie following.
http://bit.ly/mbedmov
Demo hardware
Architecture: ARM Cortex-M3
RAM size: 64kB
IO: Ethernet, LED, LCD, SD Card, USB
host/device, Serial
Demo software
github.com/ajhc/demo-cortex-m3
[2] What is Ajhc?
http://ajhc.metasepi.org/
☆ Ajhc := A fork of jhc
☆ jhc := John's Haskell Compiler
☆ http://repetae.net/computer/jhc/
☆ Jhc outputs binary that has lowmemory-footprint and runs fast.
☆ Good for embedded software.
Why need Ajhc?
☆ GHC is de facto standard on Haskell.
☆ GHC := Glasgow Haskell Compiler
☆ http://www.haskell.org/ghc/
☆ Why need another Haskell compiler?
☆ To develop kernel named "Metasepi".
[3] What is Metasepi?
http://metasepi.org/
☆ Unix-like OS designed by strong type.
☆ Using ML or more strong type lang.
Haskell http://www.haskell.org/
OCaml http://caml.inria.fr/
MLton http://mlton.org/
. . . and suchlike.
Why need Metasepi?
☆ We have already Linux or Windows.
☆ But the developers are suffering.
☆ If use the kernel changed by you,
☆ you will get many runtime error.
☆ Difficult even to reproduce it.
Doesn't OSS have good quality?
☆ "The Cathedral and the Bazaar"
☆ "Given enough eyeballs, all bugs are
shallow."
http://cruel.org/freeware/cathedral.html

☆ But if you develop your own product
reusing OSS...
Low quality out of OSS umbrella
Type safety
☆ Less runtime errors.
☆ "数理科学的バグ撲滅方法論のすすめ"
http://itpro.nikkeibp.co.jp/article/COLUMN/20060915/248230/
Kernel desperately wants type
☆ Kernels are developed with C lang.
☆ Error on user space => SEGV
☆ Error on kernel space => halt!
☆ Should design kernel with the
greatest care.
☆ C language is safe?
[4] What is compiler to build OS
☆ Need strong type.
☆ Need flexibility such as C language.
☆ Create it if there are not!
☆ From scratch? No thank you...
☆ Look for our compiler base.
Want POSIX free compiler
Programs to print "hoge" on terminal.

The lesser depends on POSIX, the
smaller values.
Jhc output has only 20 undef
$ nm hs.out | grep
U
U
U
U
U
U
U
U
U
U
U
U
U
U
U
U
U
U
U
U

"U "
_IO_putc@@GLIBC_2.2.5
__libc_start_main@@GLIBC_2.2.5
_setjmp@@GLIBC_2.2.5
abort@@GLIBC_2.2.5
ctime@@GLIBC_2.2.5
exit@@GLIBC_2.2.5
fflush@@GLIBC_2.2.5
fprintf@@GLIBC_2.2.5
fputc@@GLIBC_2.2.5
fputs@@GLIBC_2.2.5
free@@GLIBC_2.2.5
fwrite@@GLIBC_2.2.5
getenv@@GLIBC_2.2.5
malloc@@GLIBC_2.2.5
memset@@GLIBC_2.2.5
posix_memalign@@GLIBC_2.2.5
realloc@@GLIBC_2.2.5
setlocale@@GLIBC_2.2.5
sysconf@@GLIBC_2.2.5
times@@GLIBC_2.2.5
Jhc is translator to C language
Easy to cross build
Survive burning out
Let's develop in dogfooding style. (The
method is called "Snatch".)
[5] How to use Ajhc
Case of Ubuntu 12.04 amd64.
$ sudo apt-get install haskell-platform libncurses5-dev gcc m4
$ cabal update
$ export PATH=$HOME/.cabal/bin/:$PATH
$ cabal install ajhc
$ which ajhc
/home/USER/.cabal/bin/ajhc
$ echo 'main = print "hoge"' > Hoge.hs
$ ajhc Hoge.hs
$ ./hs.out
"hoge"

You can use on Windows or Mac OS X.
Detail of usage
Please read "Ajhc User's Manual".
☆ ajhc.metasepi.org/manual.html
Also you can read in Japanese.
☆ ajhc.metasepi.org/manual_ja.html
[6] Snatch application on tiny OS
Snatch only the LED blinker thread.
☆ Board: STM32F4 Discovery
http://www.st.com/stm32f4-discovery
☆ OS: ChibiOS/RT
http://www.chibios.org/
Application code before Snatch
// File: main.c
#include "ch.h"
#include "hal.h"
#include "test.h"
static adcsample_t samples[ADC_GRP1_NUM_CHANNELS
ADC_GRP1_BUF_DEPTH];
static const ADCConversionGroup adcgrpcfg = {
// --snip-static PWMConfig pwmcfg = {
// --snip-static const SPIConfig spi2cfg = {
// --snip-static void pwmpcb(PWMDriver *pwmp) {
// --snip-void adccb(ADCDriver *adcp, adcsample_t *buffer,
// --snip-static void spicb(SPIDriver *spip) {
// --snip-static WORKING_AREA(waThread1, 128);
static msg_t Thread1(void *arg) {
// --snip-int main(void) {
// --snip--

*

size_t n) {
LED blinker thread
// File: main.c
static WORKING_AREA(waThread1, 128);
static msg_t Thread1(void *arg) {
(void)arg;
chRegSetThreadName("blinker");
while (TRUE) {
palSetPad(GPIOD, GPIOD_LED3);
chThdSleepMilliseconds(500);
palClearPad(GPIOD, GPIOD_LED3);
chThdSleepMilliseconds(500);
}
}

/* Orange. */
/* Orange. */

int main(void) {
// --snip-chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO,
Thread1, NULL);
Application design
[7] Let's Snatch it!
The source code at the following.
github.com/metasepi/chibios-arafura
Run simple Haskell code #1
-- File: hs_src/Main.hs
main :: IO ()
main = return ()
// File: main.c
static char malloc_heapstart[(2*1024)];
static MemoryHeap heap_desc;
void malloc_init(void) {
chHeapInit(&heap_desc, (void *)malloc_heapstart, (2*1024));
}
int main(void) {
// --snip-{ /* Init Ajhc RTS (Haskell) */
int hsargc = 1;
char *hsargv = "q";
char **hsargvp = &hsargv;
malloc_init();
hs_init(&hsargc, &hsargvp);
_amain();
}
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO,
Thread1, NULL);
Run simple Haskell code #2
Blink LED only once #1
-- File: hs_src/Main.hs
foreign export ccall "blinkOrange" blinkOrange :: IO ()
blinkOrange :: IO ()
blinkOrange = do
c_palSetPad c_GPIOD c_GPIOD_LED3
c_chThdSleepMilliseconds 500
c_palClearPad c_GPIOD c_GPIOD_LED3
c_chThdSleepMilliseconds 500
// File: main.c
static WORKING_AREA(waThread1, 128);
static msg_t Thread1(void *arg) {
(void)arg;
chRegSetThreadName("blinker");
while (TRUE) {
blinkOrange(); // Haskell
}
}
Blink LED only once #2
Snatch blink thread code #1
-- File: hs_src/Main.hs
foreign export ccall "threadBlinkOrange" threadBlinkOrange :: IO ()
threadBlinkOrange :: IO ()
threadBlinkOrange = forever blinkOrange
// File: main.c
static WORKING_AREA(waThread1, 128);
static msg_t Thread1(void *arg) {
(void)arg;
threadBlinkOrange();
}
Snatch blink thread code #2
Use forkOS API #1
-- File: hs_src/Main.hs
main :: IO ()
main = void $ forkOS threadBlinkOrange
// File: conc_custom.c
void
forkOS_createThread_init()
{
chPoolInit(&pooldesc, THD_WA_SIZE(STACKSIZE), NULL);
chPoolLoadArray(&pooldesc, (void *) pool_buf, NTHREADS);
}
jhc_threadid_t
forkOS_createThread(void *(*wrapper) (void *),void *entry,int *err)
{
Thread *tid;
tid = chThdCreateFromMemoryPool(&pooldesc, NORMALPRIO,
(tfunc_t) wrapper, entry);
if (NULL == tid) { abort(); }
return tid;
}
Use forkOS API #2
GOAL !
PR: Call For Articles
☆ http://www.paraiso-lang.org/ikmsm/
☆ Fanzine of functional programming.
☆ About Haskell or OCaml or . . .
☆ Article about Ajhc in C84 book.
☆ Call me if you read it!
http://www.paraiso-lang.org/ikmsm/books/c85.html

More Related Content

What's hot

Why is Python slow? Python Nordeste 2013
Why is Python slow? Python Nordeste 2013Why is Python slow? Python Nordeste 2013
Why is Python slow? Python Nordeste 2013
Daker Fernandes
 
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
 
Evdokimov python arsenal for re
Evdokimov   python arsenal for reEvdokimov   python arsenal for re
Evdokimov python arsenal for re
DefconRussia
 
The future of server side JavaScript
The future of server side JavaScriptThe future of server side JavaScript
The future of server side JavaScript
Oleg Podsechin
 
Php code profiling_using_x_debug
Php code profiling_using_x_debugPhp code profiling_using_x_debug
Php code profiling_using_x_debug
Gennady Feldman
 
Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015
Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015
Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015
curryon
 

What's hot (20)

How to Write Node.js Module
How to Write Node.js ModuleHow to Write Node.js Module
How to Write Node.js Module
 
Why is Python slow? Python Nordeste 2013
Why is Python slow? Python Nordeste 2013Why is Python slow? Python Nordeste 2013
Why is Python slow? Python Nordeste 2013
 
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
 
OVHcloud TechTalks - ML serving
OVHcloud TechTalks - ML servingOVHcloud TechTalks - ML serving
OVHcloud TechTalks - ML serving
 
#Include os - From bootloader to REST API with the new C++
#Include os - From bootloader to REST API with the new C++#Include os - From bootloader to REST API with the new C++
#Include os - From bootloader to REST API with the new C++
 
C++ in kernel mode
C++ in kernel modeC++ in kernel mode
C++ in kernel mode
 
AOT-compilation of JavaScript with V8
AOT-compilation of JavaScript with V8AOT-compilation of JavaScript with V8
AOT-compilation of JavaScript with V8
 
Evdokimov python arsenal for re
Evdokimov   python arsenal for reEvdokimov   python arsenal for re
Evdokimov python arsenal for re
 
Tools
ToolsTools
Tools
 
"Развитие ветки PHP-7"
"Развитие ветки PHP-7""Развитие ветки PHP-7"
"Развитие ветки PHP-7"
 
Groovy a Scripting Language for Java
Groovy a Scripting Language for JavaGroovy a Scripting Language for Java
Groovy a Scripting Language for Java
 
Nix: What even is it though?
Nix: What even is it though?Nix: What even is it though?
Nix: What even is it though?
 
Give me 15 minutes and i'll change your view of gdb
Give me 15 minutes and i'll change your view of gdbGive me 15 minutes and i'll change your view of gdb
Give me 15 minutes and i'll change your view of gdb
 
The future of server side JavaScript
The future of server side JavaScriptThe future of server side JavaScript
The future of server side JavaScript
 
GDB: A Lot More Than You Knew
GDB: A Lot More Than You KnewGDB: A Lot More Than You Knew
GDB: A Lot More Than You Knew
 
Php code profiling_using_x_debug
Php code profiling_using_x_debugPhp code profiling_using_x_debug
Php code profiling_using_x_debug
 
Embedded. What Why How
Embedded. What Why HowEmbedded. What Why How
Embedded. What Why How
 
Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015
Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015
Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015
 
Why use JavaScript in Hardware? GoTo Conf - Berlin
Why use JavaScript in Hardware? GoTo Conf - Berlin Why use JavaScript in Hardware? GoTo Conf - Berlin
Why use JavaScript in Hardware? GoTo Conf - Berlin
 
Open-Source Analytics Stack on MongoDB, with Schema, Pierre-Alain Jachiet and...
Open-Source Analytics Stack on MongoDB, with Schema, Pierre-Alain Jachiet and...Open-Source Analytics Stack on MongoDB, with Schema, Pierre-Alain Jachiet and...
Open-Source Analytics Stack on MongoDB, with Schema, Pierre-Alain Jachiet and...
 

Similar to Metasepi team meeting #7: Snatch application on tiny OS

Share the Experience of Using Embedded Development Board
Share the Experience of Using Embedded Development BoardShare the Experience of Using Embedded Development Board
Share the Experience of Using Embedded Development Board
Jian-Hong Pan
 
Defcon 22 - Stitching numbers - generating rop payloads from in memory numbers
Defcon 22 - Stitching numbers - generating rop payloads from in memory numbersDefcon 22 - Stitching numbers - generating rop payloads from in memory numbers
Defcon 22 - Stitching numbers - generating rop payloads from in memory numbers
Alexandre Moneger
 
01 linux-quick-start
01 linux-quick-start01 linux-quick-start
01 linux-quick-start
Nguyen Vinh
 

Similar to Metasepi team meeting #7: Snatch application on tiny OS (20)

Metasepi team meeting #6: "Snatch-driven development"
Metasepi team meeting #6: "Snatch-driven development"Metasepi team meeting #6: "Snatch-driven development"
Metasepi team meeting #6: "Snatch-driven development"
 
Metasepi team meeting #8': Haskell apps on Android NDK
Metasepi team meeting #8': Haskell apps on Android NDKMetasepi team meeting #8': Haskell apps on Android NDK
Metasepi team meeting #8': Haskell apps on Android NDK
 
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
 
How to Use OpenMP on Native Activity
How to Use OpenMP on Native ActivityHow to Use OpenMP on Native Activity
How to Use OpenMP on Native Activity
 
Share the Experience of Using Embedded Development Board
Share the Experience of Using Embedded Development BoardShare the Experience of Using Embedded Development Board
Share the Experience of Using Embedded Development Board
 
Metasepi team meeting #19: ATS application on Arduino
Metasepi team meeting #19: ATS application on ArduinoMetasepi team meeting #19: ATS application on Arduino
Metasepi team meeting #19: ATS application on Arduino
 
Metasepi team meeting: Ajhc Project Overview
Metasepi team meeting: Ajhc Project OverviewMetasepi team meeting: Ajhc Project Overview
Metasepi team meeting: Ajhc Project Overview
 
Functional IoT: Hardware and Platform
Functional IoT: Hardware and PlatformFunctional IoT: Hardware and Platform
Functional IoT: Hardware and Platform
 
Get your teeth into Plack
Get your teeth into PlackGet your teeth into Plack
Get your teeth into Plack
 
Raspberry Pi - HW/SW Application Development
Raspberry Pi - HW/SW Application DevelopmentRaspberry Pi - HW/SW Application Development
Raspberry Pi - HW/SW Application Development
 
Writing NetBSD Sound Drivers in Haskell
Writing NetBSD Sound Drivers in HaskellWriting NetBSD Sound Drivers in Haskell
Writing NetBSD Sound Drivers in Haskell
 
不深不淺,帶你認識 LLVM (Found LLVM in your life)
不深不淺,帶你認識 LLVM (Found LLVM in your life)不深不淺,帶你認識 LLVM (Found LLVM in your life)
不深不淺,帶你認識 LLVM (Found LLVM in your life)
 
Oh the compilers you'll build
Oh the compilers you'll buildOh the compilers you'll build
Oh the compilers you'll build
 
Defcon 22 - Stitching numbers - generating rop payloads from in memory numbers
Defcon 22 - Stitching numbers - generating rop payloads from in memory numbersDefcon 22 - Stitching numbers - generating rop payloads from in memory numbers
Defcon 22 - Stitching numbers - generating rop payloads from in memory numbers
 
07 - Bypassing ASLR, or why X^W matters
07 - Bypassing ASLR, or why X^W matters07 - Bypassing ASLR, or why X^W matters
07 - Bypassing ASLR, or why X^W matters
 
Using GPUs to handle Big Data with Java by Adam Roberts.
Using GPUs to handle Big Data with Java by Adam Roberts.Using GPUs to handle Big Data with Java by Adam Roberts.
Using GPUs to handle Big Data with Java by Adam Roberts.
 
01 linux-quick-start
01 linux-quick-start01 linux-quick-start
01 linux-quick-start
 
[CONFidence 2016] Sławomir Kosowski - Introduction to iOS Application Securit...
[CONFidence 2016] Sławomir Kosowski - Introduction to iOS Application Securit...[CONFidence 2016] Sławomir Kosowski - Introduction to iOS Application Securit...
[CONFidence 2016] Sławomir Kosowski - Introduction to iOS Application Securit...
 
Nodejs
NodejsNodejs
Nodejs
 
Challenges in GPU compilers
Challenges in GPU compilersChallenges in GPU compilers
Challenges in GPU compilers
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Recently uploaded (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

Metasepi team meeting #7: Snatch application on tiny OS

  • 1. Metasepi team meeting #7:  #7: Snatch application on tiny OS Kiwamu Okabe
  • 2. Who am I? ☆ http://www.masterq.net/ ☆ Twitter: @master_q ☆ Organizer of Metasepi project ☆ A developer of Ajhc Haskell compiler ☆ A Debian Maintainer ☆ 10 years' experience in developing OS using NetBSD.
  • 3. Agenda ☆ [1] Demo ☆ [2] What is Ajhc? ☆ [3] What is Metasepi? ☆ [4] What is compiler to build OS ☆ [5] How to use Ajhc ☆ [6] Snatch application on tiny OS ☆ [7] Let's Snatch it!
  • 4. [1] Demo ☆ RSS reader running on mbed (ARM). ☆ Show reddit articles on LCD display. ☆ You can watch the movie following. http://bit.ly/mbedmov
  • 5. Demo hardware Architecture: ARM Cortex-M3 RAM size: 64kB IO: Ethernet, LED, LCD, SD Card, USB host/device, Serial
  • 7. [2] What is Ajhc? http://ajhc.metasepi.org/ ☆ Ajhc := A fork of jhc ☆ jhc := John's Haskell Compiler ☆ http://repetae.net/computer/jhc/ ☆ Jhc outputs binary that has lowmemory-footprint and runs fast. ☆ Good for embedded software.
  • 8. Why need Ajhc? ☆ GHC is de facto standard on Haskell. ☆ GHC := Glasgow Haskell Compiler ☆ http://www.haskell.org/ghc/ ☆ Why need another Haskell compiler? ☆ To develop kernel named "Metasepi".
  • 9. [3] What is Metasepi? http://metasepi.org/ ☆ Unix-like OS designed by strong type. ☆ Using ML or more strong type lang. Haskell http://www.haskell.org/ OCaml http://caml.inria.fr/ MLton http://mlton.org/ . . . and suchlike.
  • 10. Why need Metasepi? ☆ We have already Linux or Windows. ☆ But the developers are suffering. ☆ If use the kernel changed by you, ☆ you will get many runtime error. ☆ Difficult even to reproduce it.
  • 11. Doesn't OSS have good quality? ☆ "The Cathedral and the Bazaar" ☆ "Given enough eyeballs, all bugs are shallow." http://cruel.org/freeware/cathedral.html ☆ But if you develop your own product reusing OSS...
  • 12. Low quality out of OSS umbrella
  • 13. Type safety ☆ Less runtime errors. ☆ "数理科学的バグ撲滅方法論のすすめ" http://itpro.nikkeibp.co.jp/article/COLUMN/20060915/248230/
  • 14. Kernel desperately wants type ☆ Kernels are developed with C lang. ☆ Error on user space => SEGV ☆ Error on kernel space => halt! ☆ Should design kernel with the greatest care. ☆ C language is safe?
  • 15. [4] What is compiler to build OS ☆ Need strong type. ☆ Need flexibility such as C language. ☆ Create it if there are not! ☆ From scratch? No thank you... ☆ Look for our compiler base.
  • 16. Want POSIX free compiler Programs to print "hoge" on terminal. The lesser depends on POSIX, the smaller values.
  • 17. Jhc output has only 20 undef $ nm hs.out | grep U U U U U U U U U U U U U U U U U U U U "U " _IO_putc@@GLIBC_2.2.5 __libc_start_main@@GLIBC_2.2.5 _setjmp@@GLIBC_2.2.5 abort@@GLIBC_2.2.5 ctime@@GLIBC_2.2.5 exit@@GLIBC_2.2.5 fflush@@GLIBC_2.2.5 fprintf@@GLIBC_2.2.5 fputc@@GLIBC_2.2.5 fputs@@GLIBC_2.2.5 free@@GLIBC_2.2.5 fwrite@@GLIBC_2.2.5 getenv@@GLIBC_2.2.5 malloc@@GLIBC_2.2.5 memset@@GLIBC_2.2.5 posix_memalign@@GLIBC_2.2.5 realloc@@GLIBC_2.2.5 setlocale@@GLIBC_2.2.5 sysconf@@GLIBC_2.2.5 times@@GLIBC_2.2.5
  • 18. Jhc is translator to C language
  • 19. Easy to cross build
  • 20. Survive burning out Let's develop in dogfooding style. (The method is called "Snatch".)
  • 21. [5] How to use Ajhc Case of Ubuntu 12.04 amd64. $ sudo apt-get install haskell-platform libncurses5-dev gcc m4 $ cabal update $ export PATH=$HOME/.cabal/bin/:$PATH $ cabal install ajhc $ which ajhc /home/USER/.cabal/bin/ajhc $ echo 'main = print "hoge"' > Hoge.hs $ ajhc Hoge.hs $ ./hs.out "hoge" You can use on Windows or Mac OS X.
  • 22. Detail of usage Please read "Ajhc User's Manual". ☆ ajhc.metasepi.org/manual.html Also you can read in Japanese. ☆ ajhc.metasepi.org/manual_ja.html
  • 23. [6] Snatch application on tiny OS Snatch only the LED blinker thread. ☆ Board: STM32F4 Discovery http://www.st.com/stm32f4-discovery ☆ OS: ChibiOS/RT http://www.chibios.org/
  • 24. Application code before Snatch // File: main.c #include "ch.h" #include "hal.h" #include "test.h" static adcsample_t samples[ADC_GRP1_NUM_CHANNELS ADC_GRP1_BUF_DEPTH]; static const ADCConversionGroup adcgrpcfg = { // --snip-static PWMConfig pwmcfg = { // --snip-static const SPIConfig spi2cfg = { // --snip-static void pwmpcb(PWMDriver *pwmp) { // --snip-void adccb(ADCDriver *adcp, adcsample_t *buffer, // --snip-static void spicb(SPIDriver *spip) { // --snip-static WORKING_AREA(waThread1, 128); static msg_t Thread1(void *arg) { // --snip-int main(void) { // --snip-- * size_t n) {
  • 25. LED blinker thread // File: main.c static WORKING_AREA(waThread1, 128); static msg_t Thread1(void *arg) { (void)arg; chRegSetThreadName("blinker"); while (TRUE) { palSetPad(GPIOD, GPIOD_LED3); chThdSleepMilliseconds(500); palClearPad(GPIOD, GPIOD_LED3); chThdSleepMilliseconds(500); } } /* Orange. */ /* Orange. */ int main(void) { // --snip-chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  • 27. [7] Let's Snatch it! The source code at the following. github.com/metasepi/chibios-arafura
  • 28. Run simple Haskell code #1 -- File: hs_src/Main.hs main :: IO () main = return () // File: main.c static char malloc_heapstart[(2*1024)]; static MemoryHeap heap_desc; void malloc_init(void) { chHeapInit(&heap_desc, (void *)malloc_heapstart, (2*1024)); } int main(void) { // --snip-{ /* Init Ajhc RTS (Haskell) */ int hsargc = 1; char *hsargv = "q"; char **hsargvp = &hsargv; malloc_init(); hs_init(&hsargc, &hsargvp); _amain(); } chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  • 30. Blink LED only once #1 -- File: hs_src/Main.hs foreign export ccall "blinkOrange" blinkOrange :: IO () blinkOrange :: IO () blinkOrange = do c_palSetPad c_GPIOD c_GPIOD_LED3 c_chThdSleepMilliseconds 500 c_palClearPad c_GPIOD c_GPIOD_LED3 c_chThdSleepMilliseconds 500 // File: main.c static WORKING_AREA(waThread1, 128); static msg_t Thread1(void *arg) { (void)arg; chRegSetThreadName("blinker"); while (TRUE) { blinkOrange(); // Haskell } }
  • 31. Blink LED only once #2
  • 32. Snatch blink thread code #1 -- File: hs_src/Main.hs foreign export ccall "threadBlinkOrange" threadBlinkOrange :: IO () threadBlinkOrange :: IO () threadBlinkOrange = forever blinkOrange // File: main.c static WORKING_AREA(waThread1, 128); static msg_t Thread1(void *arg) { (void)arg; threadBlinkOrange(); }
  • 34. Use forkOS API #1 -- File: hs_src/Main.hs main :: IO () main = void $ forkOS threadBlinkOrange // File: conc_custom.c void forkOS_createThread_init() { chPoolInit(&pooldesc, THD_WA_SIZE(STACKSIZE), NULL); chPoolLoadArray(&pooldesc, (void *) pool_buf, NTHREADS); } jhc_threadid_t forkOS_createThread(void *(*wrapper) (void *),void *entry,int *err) { Thread *tid; tid = chThdCreateFromMemoryPool(&pooldesc, NORMALPRIO, (tfunc_t) wrapper, entry); if (NULL == tid) { abort(); } return tid; }
  • 37. PR: Call For Articles ☆ http://www.paraiso-lang.org/ikmsm/ ☆ Fanzine of functional programming. ☆ About Haskell or OCaml or . . . ☆ Article about Ajhc in C84 book. ☆ Call me if you read it! http://www.paraiso-lang.org/ikmsm/books/c85.html