💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
https://stackoverflow.com/questions/7415515/how-to-access-the-control-registers-cr0-cr2-cr3-from-a-program-getting-segmenta # 写C文件 ``` /* hello.c */ #include <linux/module.h> #include <linux/kernel.h> int init_module(void) { #ifdef __x86_64__ u64 cr0, cr2, cr3; __asm__ __volatile__ ( "mov %%cr0, %%rax\n\t" "mov %%eax, %0\n\t" "mov %%cr2, %%rax\n\t" "mov %%eax, %1\n\t" "mov %%cr3, %%rax\n\t" "mov %%eax, %2\n\t" : "=m" (cr0), "=m" (cr2), "=m" (cr3) : /* no input */ : "%rax" ); #elif defined(__i386__) u32 cr0, cr2, cr3; __asm__ __volatile__ ( "mov %%cr0, %%eax\n\t" "mov %%eax, %0\n\t" "mov %%cr2, %%eax\n\t" "mov %%eax, %1\n\t" "mov %%cr3, %%eax\n\t" "mov %%eax, %2\n\t" : "=m" (cr0), "=m" (cr2), "=m" (cr3) : /* no input */ : "%eax" ); #endif printk(KERN_INFO "cr0 = 0x%8.8X\n", cr0); printk(KERN_INFO "cr2 = 0x%8.8X\n", cr2); printk(KERN_INFO "cr3 = 0x%8.8X\n", cr3); return 0; } void cleanup_module(void) { } ``` # 写Makefile ``` KERNEL_DIR:=/usr/src/linux-source-4.19 EXTRA_CFLAGS = -m64 obj-m += hello.o all: $(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules clean: $(RM).*.cmd *.mod.c *.o -r .tmp test: all sudo insmod ./hello.ko sudo rmmod hello dmesg | tail ``` # 执行 ``` make make test ``` 结果: ``` [ 159.020960] Disabling L1 and L2 caches. [ 1943.428022] hello: module license 'unspecified' taints kernel. [ 1943.428024] Disabling lock debugging due to kernel taint [ 1943.428585] cr0 = 0x80050033 [ 1943.428585] cr2 = 0x46894148 [ 1943.428586] cr3 = 0x7A46A002 [ 2150.322403] Enabling L1 and L2 caches. [ 2155.893980] cr0 = 0xC0050033 [ 2155.893981] cr2 = 0x20167D19 [ 2155.893982] cr3 = 0x271DC003 ``` 相关结果可以使用微软自带的程序员计算器查看。 今晚测试修改CR0成功。