Skip to content

Commit 9ad24a3

Browse files
committed
kernel version
1 parent 703f801 commit 9ad24a3

File tree

9 files changed

+122
-0
lines changed

9 files changed

+122
-0
lines changed

0_Linux_Version/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
obj-m := hello.o
2+
3+
all:
4+
make -C /lib/modules/`uname -r`/build M=$(PWD) modules
5+
clean:
6+
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

0_Linux_Version/hello.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <linux/kernel.h>
2+
#include <linux/module.h>
3+
#include <linux/version.h>
4+
5+
MODULE_LICENSE("GPL");
6+
static int __init test_hello_init(void)
7+
{
8+
pr_info("%s: In init\n", __func__);
9+
pr_info("LINUX_VERSION_CODE:%u\n", LINUX_VERSION_CODE);
10+
return 0;
11+
}
12+
13+
static void __exit test_hello_exit(void)
14+
{
15+
pr_info("%s: In exit\n", __func__);
16+
}
17+
18+
module_init(test_hello_init);
19+
module_exit(test_hello_exit);

0_Linux_Version/readme.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### 1. Explain the technical concept 📘
2+
When developing Linux Kernel Modules, it’s crucial to consider the Kernel Version the module is intended for as there are changes in function arguments, deprecations, and different implementations between versions. `access_ok` is one such function that witnessed a change in its signature with Linux version 5.0. To support multiple kernel versions within one module, conditional compilation directives are necessary. This involves comparing `LINUX_VERSION_CODE` with `KERNEL_VERSION` to manage differences in kernel API. The `LINUX_VERSION_CODE` represents the binary equivalent of the kernel version, and it's calculated from the top-level Makefile of the Kernel using `VERSION`, `PATCHLEVEL`, and `SUBLEVEL`.
3+
4+
### 2. Curious Questions 🤔
5+
**Q:** How is the `LINUX_VERSION_CODE` for a specific kernel version calculated?
6+
**A:** It’s calculated using the formula: `LINUX_VERSION_CODE = (VERSION * 65536) + (PATCHLEVEL * 256) + SUBLEVEL`, wherein `VERSION`, `PATCHLEVEL`, and `SUBLEVEL` are the major, minor, and sublevel version numbers of the kernel, respectively.
7+
8+
**Q:** Why is it important to consider `LINUX_VERSION_CODE` when developing kernel modules?
9+
**A:** It is important because it helps in managing the changes in function arguments, deprecations, and different implementations between kernel versions, allowing developers to write modules that are compatible with multiple kernel versions.
10+
11+
**Q:** What does adding `EXTRA_CFLAGS=’-save-temps’` in your Makefile do during the compilation of a kernel module?
12+
**A:** It will generate all intermediate files during the creation of `.ko` (Kernel Object) files, which can be helpful in debugging and understanding the preprocessing stage of compilation.
13+
14+
### 3. Explain the concept in simple words 🌟
15+
Imagine you are baking different versions of a cake 🍰 (kernel versions). Some people (modules) have different preferences or allergies (API changes). The `LINUX_VERSION_CODE` is like a recipe card 📜 that tells you which version of the cake you are baking. So, when you are baking (compiling), you need to check the recipe card (LINUX_VERSION_CODE) to make sure you are adding the right ingredients (using the right API) for the people (modules) who will eat the cake (run on the kernel). If you want to save every step of your baking process (compilation), adding `EXTRA_CFLAGS=’-save-temps’` is like taking pictures 📸 at every step, so you can go back and see what you did at each stage.
16+
17+
18+
The way to do this to compare the macro LINUX_VERSION_CODE to the macro KERNEL_VERSION.
19+
20+
LINUX_VERSION_CODE:This macro expands to the binary representation of the kernel version.
21+
One byte for each part of the version release number.
22+
Eg. 5.0.0 = 0x050000 = 327680
23+
24+
Header File: <linux/version.h>
25+
26+
From Kernel Top Level Makefile
27+
28+
LINUX_VERSION_CODE = $(VERSION) * 65536 + $(PATCHLEVEL) * 256 + $(SUBLEVEL)
29+
Eg: 5.0.0 = 5*65536+0*256+0 = 327680

1_Linux_Version/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
obj-m := hello.o
2+
3+
all:
4+
make -C /lib/modules/`uname -r`/build M=$(PWD) modules
5+
clean:
6+
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

1_Linux_Version/hello.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <linux/kernel.h>
2+
#include <linux/module.h>
3+
#include <linux/version.h>
4+
5+
MODULE_LICENSE("GPL");
6+
static int __init test_hello_init(void)
7+
{
8+
pr_info("%s: In init\n", __func__);
9+
pr_info("KERNEL VERSION for 2.6.2 is :%u\n", KERNEL_VERSION(2, 6, 2));
10+
pr_info("KERNEL VERSION for 5.0.18 is :%u\n", KERNEL_VERSION(5, 0, 18));
11+
return 0;
12+
}
13+
14+
static void __exit test_hello_exit(void)
15+
{
16+
pr_info("%s: In exit\n", __func__);
17+
}
18+
19+
module_init(test_hello_init);
20+
module_exit(test_hello_exit);

1_Linux_Version/readme.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
### 1. Explain the technical concept 📘
2+
The `KERNEL_VERSION` macro is an integral part of kernel module development, especially when dealing with modules that aim to be compatible with different versions of the Linux kernel. This macro is used to construct a single integer value representing the kernel version from the major, minor, and patch levels of the version number. It's defined as `#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))`, and it's included through the `<linux/version.h>` header file. This integer value is crucial for performing comparisons with the `LINUX_VERSION_CODE` to ensure compatibility and manage API changes between different kernel versions.
3+
4+
### 2. Curious Questions 🤔
5+
**Q:** How is the `KERNEL_VERSION` macro used to evaluate whether the kernel version of the running system is above a certain level?
6+
**A:** Developers can compare the result of `KERNEL_VERSION(a, b, c)` with the `LINUX_VERSION_CODE` to determine the running kernel’s version. If `LINUX_VERSION_CODE >= KERNEL_VERSION(a, b, c)`, then the running kernel's version is equal to or higher than the specified version `a.b.c`.
7+
8+
**Q:** Can you explain how the shifting and addition in the `KERNEL_VERSION` macro lead to a unique integer representing the kernel version?
9+
**A:** The `KERNEL_VERSION` macro takes three integers representing the major, minor, and patch levels. It shifts the major version `a` 16 bits to the left, the minor version `b` 8 bits to the left, and then adds them together with the patch level `c`. This ensures a unique integer value for each different version, which can be easily compared.
10+
11+
### 3. Explain the concept in simple words 🌟
12+
Think of `KERNEL_VERSION` as a special formula 🧪 that takes three ingredients: the major, minor, and patch levels of a kernel version, and mixes them to create a unique number label 🏷️, representing that specific version of the kernel. It’s like having a unique recipe name for each variation of a dish 🍲 you cook. This way, you can easily check 🕵️‍♂️ if the recipe (kernel version) you have is the one you need for your guests (modules). So, if you know your dish's name (kernel version), you can quickly tell if it’s the right one by checking the label (comparing `KERNEL_VERSION` and `LINUX_VERSION_CODE`).

2_UTS_release/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
obj-m := hello.o
2+
3+
all:
4+
make -C /lib/modules/`uname -r`/build M=$(PWD) modules
5+
clean:
6+
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

2_UTS_release/hello.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <linux/kernel.h>
2+
#include <linux/module.h>
3+
#include <generated/utsrelease.h>
4+
5+
MODULE_LICENSE("GPL");
6+
static int __init test_hello_init(void)
7+
{
8+
pr_info("UTS_RELEASE:%s\n", UTS_RELEASE);
9+
return 0;
10+
}
11+
12+
static void __exit test_hello_exit(void)
13+
{
14+
pr_info("%s: In exit\n", __func__);
15+
}
16+
17+
module_init(test_hello_init);
18+
module_exit(test_hello_exit);

2_UTS_release/notes.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
UTS_RELEASE
2+
============
3+
4+
This macro expands to a string describing the version of this kernel tree.
5+
6+
Header File: #include <generated/utsrelease.h>

0 commit comments

Comments
 (0)