NetDBSLuaLua in the NetBSD Kernel
Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Topics1 The Programming Language Lua

The Lua Interpreter
Syntax and such
Modules
2 Embedding Lua in C Programs

State Manipulation
Calling C from Lua
Calling Lua from C
3 Lua in the NetBSD Kernel

Use Cases
Implementation Overview
Implementation Details
Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Lua in the NetBSD Kernel

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

The Lua Interpreter

Lua Source

Lua in the NetBSD Kernel

iler
mpLua

1001101
001001001000111011010

BytecodeRulocal nn=n + 1
print(n)Co

Running Lua Sourcecode

iment

Lua

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

The Lua Interpreter

Lua Source

Lua in the NetBSD Kernel

iler
mpLua

1001101
001001001000111011010

BytecodeRulocal nn=n + 1
print(n)Co

Compiling / Running Lua Bytecode

iment

Lua

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

The Lua Interpreter

Running from C• int luaL dofile(lua State *L, const char *filename)• int luaL dostring(lua State *L, const char *str)

Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Syntax and such

Values, Variables, and, Data Types• Variables have no type
• Values do have a type
• Functions are first-class values

Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Syntax and such

Tables• Tables are THE data structure in Lua• Nice constructor syntax• Tables make Lua a good DDL• Metatables can be associated with every object

Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Syntax and such

Lua Table Constructor

Create and initialize a table, access a field:
mytable = {name = ’Marc’,surname = ’Balmer’,email = ’m@x.org’}print(mytable.email)Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Modules

Extending Lua Programs

Acess GPIO pins from Lua:require ’gpio’
g = gpio.open(’/dev/gpio0’)
g:write(4, gpio.PIN_HIGH)
g:close()Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

State Manipulation

Creating and Destroying a State• lua State *L = lua newstate()• luaopen module(L)• lua close(L)Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Calling C from Lua

Calling a C Function• Function has been registered in luaopen module()• int function(lua State *L)• Parameters popped from the stack• Return values pushed on the stack• Return Nr. of return valuesLua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Calling Lua from C

Calling a Lua Function• Find the function and make sure is *is* a function• Push parameters on the stack• Use lua call(lua State *L, int index)• or lua pcall(lua State *L, int index)• Pop return values from the stack

Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Calling Lua from C

Calling a Lua Function
The Lua function
function hello()
print(’Hello, world!’)
end

Call hello from C:
lua_getglobal(L, "hallo");
lua_pcall(L, 0, 0, 0);

Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Use Cases

Ideas for Users• Modifying software written in C is hard for users• Give users the power to modify and extend the system• Let users explore the system in an easy way

Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Use Cases

Ideas for Developers• ,,Rapid Application Development” approach to driverdevelopment• Modifying the system behaviour• Configuration of kernel subsystems

Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Use Cases

Alternatives• Python• JavaLua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Use Cases

Python• Not to difficult to integrate in C
• Huge library• Memory consumption• Difficult object mapping

Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Use Cases

Java• Easy to integrate• Difficult object mapping• Memory considerations• Has been used for driver development

Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Use Cases

Lua in NetBSD Userland• Library (liblua.so) and binaries (lua, luac) committed to-current• Will be part of NetBSD 6• No back port to NetBSD 5 stable planned

Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Use Cases

Lua in the NetBSD Kernel• Started as GSoC project, porting Lunatik from Linux toNetBSD• Proof that the Lua VM can run in the kernel, lack ofinfrastructure• Infrastructure beeing added nowLua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Use Cases

Running in Userland• Every process has its own address space• Lua states in different processes are isolated

Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Use Cases

Running in the Kernel• One address space• Every thread that is in the kernel“ uses the same memory”• Locking is an issue

Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Implementation Overview

The Big Picture

Kernel subsystems (Users)

Lua

ate
ate
ate
ate

St

Lua

ime
nt

Lua bindings

Stmodload(8)

iler
mp

Ru

KernelCo

Userland

Lua
Lua
Lua
Lua
St

St

luactl(8)sysctl(8)lua(4)
ioctl(2)sysctl(3)luac(1)
local nn=n + 1
print(n)Lua in the NetBSD Kernel

local nn=n + 1
print(n)local nn=n + 1
print(n)local nn=n + 1print(n)local nn=n + 1print(n)Filesystem

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Implementation Overview

The lua(4) Device Driveriler
mpRu

KernelCo

Userland

Lua

iment

Lualua(4)ioctl(2)sysctl(3)Filesystem

Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Implementation Overview

lua(4) ioctl(2) / sysctl(3) Interface
UserlandKernel

luactl(8)sysctl(8)lua(4)
ioctl(2)sysctl(3)Filesystem

Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Implementation Overview

Lua States
UserlandKernel

St

St

ate
ate
ate
ate

St

St

Lua
Lua
Lua
Lua

Filesystem

Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Implementation Overview

Lua Modules
Userlandmodload(8)KernelLua bindings

FilesystemLua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Implementation Overview

Lua Users
UserlandKernelKernel subsystems (Users)

Filesystem

Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Implementation Overview

The luactl(8) Command
UserlandKernelluactl(8)Filesystem

Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Implementation Details,,require”• Modules are kernel modules• ’require’ can be turned off• Modules must be assigned to Lua states by hand then• By default ’require’ is on and modules are even autoloaded• Module autoloading can be turned off

Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Implementation Details

sysctl(8) Variables• kern.lua.require=1• kern.lua.autoload=1• kern.lua.bytecode=0• kern.lua.maxcount=0

Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Implementation Details

Loading Lua Codeiler
mpRu

KernelCo

Userland

Lua

ime
nt

lua_Loader

Lua
St

ate

Lua

lua(4)
luactl(8)ioctl(2)local nn=n + 1
print(n)Lua in the NetBSD Kernel

local nn=n + 1
print(n)local nn=n + 1
print(n)local nn=n + 1
print(n)local nn=n + 1
print(n)Filesystem

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Implementation Details

Security• New Lua states are created empty• Full control over the loading of code
• No access to kernel memory, -functions but throughpredefined bindings

Lua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>

The Programming Language Lua

Embedding Lua in C Programs

Lua in the NetBSD Kernel

Implementation Details

Time for QuestionsNet

DBSLuaLua in the NetBSD Kernel

Marc Balmer <mbalmer@NetBSD.org>