summaryrefslogtreecommitdiffstats
path: root/md/writeup/kernel_hello_world.md
blob: eb4fdc8b0bdb2f6f17171b44ce5a74d56d39e66d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
title: Kernel compile "Hello world"
# Kernel compile "Hello world"

Compile minimal linux kernel module. 

## Files

You need to create to files __Makefile__ and __hello_world.c__.

__Makefile__
```Makefile
obj-m += hello_world.o

KDIR ?= /lib/modules/$(shell uname -r)/build

all:
	make -C $(KDIR) M=$(PWD) modules

clean:
	make -C $(KDIR) M=$(PWD) clean
```


__hello_world.c__
```c
//http://www.tldp.org/LDP/lkmpg/2.4/html/c147.htm
#include <linux/module.h>  /* Needed by all modules */
#include <linux/kernel.h>

int hello_world_init( void )
{
	printk(KERN_DEBUG "Hello World!\n");
	return 0;
}

void hello_world_exit( void )
{
	printk(KERN_DEBUG "Exit Hello World!\n");
}

module_init( hello_world_init );
module_exit( hello_world_exit );

MODULE_LICENSE("GPL");
```



## Compile

Now if you havent done so ... install kernel headers of kernel that your system have now. And everything should be there.

```sh
make
```

## Load module

```sh
sudo insmod hello_world.ko
```

check that module is running

```sh
lsmod | grep hello
```

## Unload module

```sh
rmmod hello_world
```


### Install kernel headers raspbian

RaspberryPi4 ARMv8 

```bash
sudo apt-get install raspberrypi-kernel-headers
```