centos 自定义内核模块 编译运行
生活随笔
收集整理的這篇文章主要介紹了
centos 自定义内核模块 编译运行
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
簡單記錄一下 centos 自定義內核模塊的一些編譯運行記錄,代碼如下:
主要功能是通過rdtsc 指令直接從 CPU MSR 寄存器中獲取時鐘,嘗試獲取兩次,兩次之間會做一些賦值操作什么的,并記錄一下時差。
#include <linux/hardirq.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/preempt.h>
#include <linux/sched.h>
void inline measured_function(volatile int *var) { (*var) = 1; }static int __init hello_start(void) {unsigned long flags;uint64_t start, end;int variable = 0;unsigned cycles_low, cycles_high, cycles_low1, cycles_high1;printk(KERN_INFO "Loading test module...\n");preempt_disable(); /*we disable preemption on our CPU*/raw_local_irq_save(flags); /*we disable hard interrupts on our CPU*//*at this stage we exclusively own the CPU*/asm volatile("RDTSC\n\t""mov %%edx, %0\n\t""mov %%eax, %1\n\t": "=r"(cycles_high), "=r"(cycles_low));measured_function(&variable);asm volatile("RDTSC\n\t""mov %%edx, %0\n\t""mov %%eax, %1\n\t": "=r"(cycles_high1), "=r"(cycles_low1));raw_local_irq_restore(flags);/*we enable hard interrupts on our CPU*/preempt_enable(); /*we enable preemption*/start = (((uint64_t)cycles_high << 32) | cycles_low);end = (((uint64_t)cycles_high1 << 32) | cycles_low1);printk(KERN_INFO "\n function execution time is %llu clock cycles",(end - start));return 0;
}static void __exit hello_end(void) { printk(KERN_INFO "Goodbye Mr.\n"); }
module_init(hello_start);
module_exit(hello_end);
編譯方式,Makefile
# obj-m 必須存在,后面的 .o 文件可以是自己的文件名,其他無需更改
obj-m := rdtsc_mode.o
KERNELBUILD :=/lib/modules/$(shell uname -r)/build
default:make -C $(KERNELBUILD) M=$(shell pwd) modulesclean:rm -rf *.o *.ko *.mod.c .*.cmd
將源代碼文件和 makefile 放在一個目錄之下,
sudo make 執行即可,執行成功之后會在當前目錄下生成一個 rdtsc_mode.ko 文件。
執行 sudo insmod rdtsc_mode.ko 加載模塊,以及卸載模塊 rmmod rdtsc_mode.ko,通過 dmesg -Tw 能看到自定模塊的輸出信息:
[Fri Jan 14 16:41:52 2022] Loading test module...
[Fri Jan 14 16:41:52 2022] function execution time is 3504353503 clock cycles
[Fri Jan 14 16:42:11 2022] Goodbye Mr.
當然, 這個只是方便對內核的一些知識做一些簡單測試,穩定性則需要自己保證。
總結
以上是生活随笔為你收集整理的centos 自定义内核模块 编译运行的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 成都一般的医院打胎要多少钱?
- 下一篇: Pliops XDP(Extreme D