Skip to content

Commit

Permalink
now driver can be correctly unloaded and uses newer character devices…
Browse files Browse the repository at this point in the history
… API

+ build process of module as it in ulp-lua
  • Loading branch information
mihalicyn committed Mar 19, 2019
1 parent 9505589 commit f000215
Showing 7 changed files with 101 additions and 24 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
*.o
*.mod.o
*.ko
*.so*
*.d
.tmp_versions
.*.cmd
.*.mk
*.a
*.mod.c
*.out
modules.order
Module.symvers
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "dependencies/lunatik"]
path = dependencies/lunatik
url = https://github.com/luainkernel/lunatik.git
17 changes: 17 additions & 0 deletions Kbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
LUNATIK := dependencies/lunatik/lua

subdir-ccflags-y := -I${PWD}/${LUNATIK} \
-Wall \
-D_KERNEL \
-D_MODULE \
-D'CHAR_BIT=(8)' \
-D'MIN=min' \
-D'MAX=max' \
-D'UCHAR_MAX=(255)' \
-D'UINT64_MAX=((u64)~0ULL)'

obj-y += dependencies/lunatik/

poc-driver-objs := luadrv.o \

obj-m += poc-driver.o
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
all:
make -C /lib/modules/${shell uname -r}/build M=${PWD} \
CONFIG_LUNATIK=m

clean:
make -C /lib/modules/${shell uname -r}/build M=${PWD} clean
7 changes: 7 additions & 0 deletions compile-n-reload.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
make
sudo rmmod poc-driver
sudo rmmod lunatik
sudo insmod ./dependencies/lunatik/lunatik.ko
sudo insmod poc-driver.ko

1 change: 1 addition & 0 deletions dependencies/lunatik
Submodule lunatik added at 0eaa4f
78 changes: 54 additions & 24 deletions luadrv.c
Original file line number Diff line number Diff line change
@@ -7,24 +7,25 @@
#include <linux/cdev.h>
#include <linux/uaccess.h>

#include <lua/lua.h>
#include <lua/lualib.h>
#include <lua/lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

MODULE_LICENSE("Dual MIT/GPL");
MODULE_AUTHOR("Pedro Tammela <[email protected]>");
MODULE_DESCRIPTION("sample driver for lunatik proof of concepts");

#define DEVICE_NAME "luadrv"
#define CLASS_NAME "lua"
#define LUA_MAX_MINORS 1

#define raise_err(msg) pr_warn("[lua] %s - %s\n", __func__, msg);

static DEFINE_MUTEX(mtx);

static int major;
static lua_State *L;
static bool hasreturn = 0; /* does the lua state have anything for us? */
static dev_t dev;
static struct device *luadev;
static struct class *luaclass;
static struct cdev luacdev;
@@ -44,38 +45,66 @@ static struct file_operations fops =

static int __init luadrv_init(void)
{
L = luaL_newstate();
if (L == NULL) {
raise_err("no memory");
return -ENOMEM;
int ret;

ret = alloc_chrdev_region(&dev, 0, LUA_MAX_MINORS, "lua");
if (ret) {
raise_err("alloc_chrdev_region failed");
goto error;
}
luaL_openlibs(L);
major = register_chrdev(0, DEVICE_NAME, &fops);
if (major < 0) {
raise_err("major number failed");
return -ECANCELED;

cdev_init(&luacdev, &fops);
ret = cdev_add(&luacdev, dev, LUA_MAX_MINORS);
if (ret) {
raise_err("cdev_add failed");
goto error_free_region;
}

luaclass = class_create(THIS_MODULE, CLASS_NAME);
if (IS_ERR(luaclass)) {
unregister_chrdev(major, DEVICE_NAME);
raise_err("class failed");
return PTR_ERR(luaclass);
raise_err("class_create failed");
ret = PTR_ERR(luaclass);
goto error_free_cdev;
}
luadev = device_create(luaclass, NULL, MKDEV(major, 1),

luadev = device_create(luaclass, NULL, dev,
NULL, "%s", DEVICE_NAME);
if (IS_ERR(luadev)) {
class_destroy(luaclass);
cdev_del(&luacdev);
unregister_chrdev(major, DEVICE_NAME);
raise_err("device failed");
return PTR_ERR(luaclass);
raise_err("device_create failed");
ret = PTR_ERR(luadev);
goto error_free_class;
}

L = luaL_newstate();
if (L == NULL) {
raise_err("no memory");
ret = -ENOMEM;
goto error_free_device;
}
luaL_openlibs(L);

return 0;

error_free_device:
device_destroy(luaclass, dev);
error_free_class:
class_destroy(luaclass);
error_free_cdev:
cdev_del(&luacdev);
error_free_region:
unregister_chrdev_region(dev, LUA_MAX_MINORS);
error:
return ret;
}

static void __exit luadrv_exit(void)
{
return;
lua_close(L);

device_destroy(luaclass, dev);
class_destroy(luaclass);
cdev_del(&luacdev);
unregister_chrdev_region(dev, LUA_MAX_MINORS);
}

static int dev_open(struct inode *i, struct file *f)
@@ -113,9 +142,10 @@ static int flushL(void)
return 1;
}
luaL_openlibs(L);
raise_err("flushed lua state!!");
raise_err("lua state flushed");
return 0;
}

static ssize_t dev_write(struct file *f, const char *buf, size_t len,
loff_t* off)
{

0 comments on commit f000215

Please sign in to comment.