Writing a Linux driver in the D ecosystem
Eduard Staniloiu
edi33416 at gmail.com
Mon Oct 29 15:51:49 UTC 2018
On Thursday, 25 October 2018 at 12:35:56 UTC, Eduard Staniloiu
wrote:
> On Wednesday, 24 October 2018 at 06:36:51 UTC, sarn wrote:
>> On Tuesday, 23 October 2018 at 12:06:48 UTC, Eduard Staniloiu
>> wrote:
>>> Hello, everyone!
>>>
>>> We, here at UPB, were thinking if it would be possible to
>>> write a simple Linux driver.
>>
>> Hi, I gave this a casual try once. The difficulty is the same
>> difficulty you get trying to write Linux kernel modules in
>> C++: the Linux build system is a mess of make files that are
>> only designed for C, and don't represent a stable API between
>> kernel source versions.
>>
>> If I had to write a Linux module in D, I'd write an interface
>> layer in C that gets built using the normal Linux build system
>> and can be linked to my D code. The interface would have to
>> provide alternatives to the various Linux preprocessor macros.
>> (I think trying to refactor the build system to work with dpp
>> would be a total waste of time.)
>
> Thank you all for your replies!
>
> We too, had the same feeling that it should be possible to do,
> based on the ABI and `-betterC`.
>
> Our concern was the build system, and how everything can be
> glued together.
> Thank you for the thin-layer C interface suggestion; I'll try
> to give it a go ("Hello, insmod") in the weekend.
>
> I'll give an update as soon as I have one.
>
> Cheers,
> Edi
Hello, everyone!
I am trying to get a simple dummy module to work.
I have a simple function written in D, named `call_d`, that I
want to call
from the `module_init` C function.
I had the following plan:
* build a .o from the D source file, using `dmd -c -betterC
dsrc.d`
* add `dsrc.o` to the list of objects in Kbuild
* let make do it's magic
Based on this stackoverflow Q/A [0], in order to tell Kbuild to
use a prebuilt object,
the object needs to end in `.o_shipped`; so, I named my `dsrc.o`
accordingly -> dsrc.o_shipped
Here are my files:
// C Module
```
// hellomod.c
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
MODULE_DESCRIPTION("My kernel module");
MODULE_AUTHOR("Me");
MODULE_LICENSE("GPL");
static int dummy_init(void)
{
int r = call_d();
printk( KERN_ALERT "Hi. got %d from D\n", r);
return 0;
}
static void dummy_exit(void)
{
printk( KERN_ALERT "Bye\n" );
}
module_init(dummy_init);
module_exit(dummy_exit);
```
// D Source
```
// dsrc.d
extern(C) int call_d()
{
return 10;
}
```
// Kbuild
```
EXTRA_CFLAGS = -Wall -g
obj-m = hellomod.o
hellomod-y = dsrc.o_shipped
```
// Makefile
```
KDIR = /lib/modules/`uname -r`/build
kbuild:
» make -C $(KDIR) M=`pwd`
clean:
» make -C $(KDIR) M=`pwd` clean
```
I'm building my dsrc.o_shipped with `dmd -c -betterC dsrc.d
-of=dsrc.o_shipped`
When I `make` the build, I get the following warning:
```
WARNING: could not find
/home/fawkes/ws/dlang/hello/.dsrc.o_shipped.cmd for
/home/fawkes/ws/dlang/hello/dsrc.o_shipped
```
The hellomod.ko get's built, but when I `insmod` it, it doesn't
print anything at `dmesg`.
I don't get a Kernel panic/oops either.
I know it's a long post, but any suggestions?
Cheers,
Edi
More information about the Digitalmars-d
mailing list