1
0

feat(lab02): add ex04

This commit is contained in:
2026-03-31 10:51:27 +02:00
parent 841b7dead5
commit da2639d6f3
3 changed files with 71 additions and 17 deletions

View File

@@ -234,7 +234,7 @@ The `/proc/modules` file give us more details about the state of the module. We
|> dmesg | tail -5 |> dmesg | tail -5
[ 3559.279143] number: 1 [ 3559.279143] number: 1
[ 3581.198562] Linux module skeleton unloaded [ 3581.198562] Linux module skeleton unloaded
[ 3583.616662] Linux module skeleton ex02 loaded [ 3583.616662] Linux module skeleton ex03 loaded
[ 3583.621085] text: The answer to the Ultimate Question of Life, The Universe, and Everything [ 3583.621085] text: The answer to the Ultimate Question of Life, The Universe, and Everything
[ 3583.621085] number: 42 [ 3583.621085] number: 42
|> modprobe -r mymodule |> modprobe -r mymodule
@@ -297,6 +297,22 @@ This number matches with this table (#link("https://www.kernel.org/doc/html/late
] ]
) )
To allocate memory in the kernel, we can use the `kcalloc` function. It allows to allocate directly the memory for all element. It's also possible to use `kzalloc` in a loop to allocate memory for each element. We prefer allocate all the memory at once to avoid fragmentation and to be sure all the memory can be allocated.
```bash
struct element* element_ptr = kcalloc(elements, sizeof(struct element), GFP_KERNEL);
for (int i = 0; i < elements; i++) {
struct element* e = element_ptr + i;
if (e != 0) {
strncpy(e->text, text, TEXT_LENGTH_MAX - 1);
e->unique_number = i;
list_add_tail(&e->node, &list_unique_elements);
pr_info ("add element %d: %s\n", e->unique_number, e->text);
}
}
```
//------------------- //-------------------
// Exercise 5: Display the processor chip ID, CPU temperature and the MAC adress of the Ethernet controller // Exercise 5: Display the processor chip ID, CPU temperature and the MAC adress of the Ethernet controller
//------------------- //-------------------

View File

@@ -5,27 +5,65 @@
#include <linux/moduleparam.h> // needed for module parameters #include <linux/moduleparam.h> // needed for module parameters
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/string.h>
#define TEXT_LENGTH_MAX 255
static char* text = "dummy text"; static char* text = "dummy text";
module_param(text, charp, 0664); module_param(text, charp, 0664);
static int number = 1; static int elements = 1;
module_param(number, int, 0); module_param(elements, int, 0);
static int __init skeleton_init(void) struct element {
{ char text[TEXT_LENGTH_MAX];
pr_info ("Linux module skeleton ex02 loaded\n"); int unique_number;
pr_debug (" text: %s\n number: %d\n", text, number); struct list_head node;
};
static LIST_HEAD (list_unique_elements);
static int __init skeleton_init(void) {
pr_info("Linux module skeleton ex04 loaded\n");
pr_debug(" text: %s\n elements: %d\n", text, elements);
struct element* element_ptr = kcalloc(elements, sizeof(struct element), GFP_KERNEL);
if (element_ptr == 0) {
pr_err("Failed to allocate memory for %d elements\n", elements);
return -ENOMEM;
}
int i;
const int length = TEXT_LENGTH_MAX - 1;
for (i = 0; i < elements; i++) {
struct element* e = element_ptr + i;
if (e != 0) {
strncpy(e->text, text, length);
e->unique_number = i;
list_add_tail(&e->node, &list_unique_elements);
pr_info ("add element %d: %s\n", e->unique_number, e->text);
}
}
return 0; return 0;
} }
static void __exit skeleton_exit(void) static void __exit skeleton_exit(void) {
{ struct element* e;
pr_info ("Linux module skeleton unloaded\n");
while (!list_empty(&list_unique_elements)) {
e = list_entry(list_unique_elements.next, struct element, node);
pr_info ("delete element %d: %s\n", e->unique_number, e->text);
list_del(&e->node);
kfree(e);
}
pr_info ("Linux module skeleton unloaded\n");
} }
module_init (skeleton_init); module_init(skeleton_init);
module_exit (skeleton_exit); module_exit(skeleton_exit);
MODULE_AUTHOR ("Fastium <fastium.pro@proton.me>"); MODULE_AUTHOR("Fastium <fastium.pro@proton.me>");
MODULE_AUTHOR ("Klagarge <remi@heredero.ch>"); MODULE_AUTHOR("Klagarge <remi@heredero.ch>");
MODULE_DESCRIPTION ("Module skeleton"); MODULE_DESCRIPTION("Module skeleton");
MODULE_LICENSE ("GPL"); MODULE_LICENSE("GPL");

View File

@@ -1 +1 @@
options mymodule number=42 text="The answer to the Ultimate Question of Life, The Universe, and Everything" options mymodule elements=5 text="Some element"