#author("2021-12-12T10:56:13+09:00","default:honma","honma") #author("2021-12-12T11:26:41+09:00","default:honma","honma") * ARMのCPU判定 [#a5ab70ea] Intel x86では、CPUID命令でCPUの種類を得ることができるが、ARMには存在しない。~ ARMではシステム制御コプロセッサの[[Main ID Register(MIDR):https://developer.arm.com/documentation/ddi0500/j/System-Control/AArch32-register-descriptions/Main-ID-Register?lang=en]]から取得する。~ おまけで、[[System Control Register(SCTLR):https://developer.arm.com/documentation/ddi0500/j/System-Control/AArch32-register-descriptions/System-Control-Register?lang=en)]]も取得してみる。~ #highlight(c){{ #include <linux/init.h> #include <linux/module.h> unsigned int get_MIDR_reg(void) { unsigned int reg; __asm__ __volatile__("mrc p15, 0, %0, c0, c0, 0" : "=r" (reg)); return reg; } unsigned int get_SCTLR_reg(void) { unsigned int reg; __asm__ __volatile__("mrc p15, 0, %0, c1, c0, 0" : "=r" (reg)); return reg; } static int armcpuinfo_init(void) { pr_info("%s\n", __FUNCTION__); pr_info("MIDR = %x\n", get_MIDR_reg()); pr_info("SCTLR = %x\n", get_SCTLR_reg()); return 0; } static void armcpuinfo_exit(void) { pr_info("%s\n", __FUNCTION__); } MODULE_LICENSE("GPL v2"); module_init(armcpuinfo_init); module_exit(armcpuinfo_exit); }} #ref(armcpuinfo.tgz) ** ビルド [#g24006d9] pi@raspberrypi:~/armcpuinfo $ make make -C /lib/modules/5.4.83-v7+/build M=/home/pi/armcpuinfo modules make[1]: Entering directory '/home/pi/linux' CC [M] /home/pi/armcpuinfo/armcpuinfo.o Building modules, stage 2. MODPOST 1 modules CC [M] /home/pi/armcpuinfo/armcpuinfo.mod.o LD [M] /home/pi/armcpuinfo/armcpuinfo.ko make[1]: Leaving directory '/home/pi/linux' ** 実行結果 [#sf4c92ad] pi@raspberrypi:~/armcpuinfo $ sudo insmod armcpuinfo.ko pi@raspberrypi:~/armcpuinfo $ sudo rmmod armcpuinfo pi@raspberrypi:~ $ dmesg | tail [ 4245.068818] armcpuinfo: loading out-of-tree module taints kernel. [ 4245.069367] armcpuinfo_init [ 4245.069379] MIDR = 410fd034 [ 4245.069388] SCTLR = 10c5383d [ 4255.571746] armcpuinfo_exit ** AArch64 [#u2829445] 手元のラズパイは32bitだが、64bitのAArch64では、アクセス方法が異なる。~ それぞれ、アクセスするレジスタが、[[Main ID Register, EL1(MIDR_EL1):https://developer.arm.com/documentation/ddi0500/j/System-Control/AArch64-register-descriptions/Main-ID-Register--EL1?lang=en]]と、 [[System Control Register, EL1(SCTLR_EL1):https://developer.arm.com/documentation/ddi0500/j/System-Control/AArch64-register-descriptions/System-Control-Register--EL1?lang=en]] となり、アクセス命令も変わる。 #highlight(c){{ #include <linux/init.h> #include <linux/module.h> unsigned long get_MIDR_EL1_reg(void) { unsigned long reg; __asm__ __volatile__("mrs %0, MIDR_EL1" : "=r" (reg)); return reg; } unsigned long get_SCTLR_EL1_reg(void) { unsigned long reg; __asm__ __volatile__("mrs %0, SCTLR_EL1" : "=r" (reg)); return reg; } static int armcpuinfo_init(void) { void __iomem *virt_addr; pr_info("%s\n", __FUNCTION__); pr_info("MIDR_EL1 = %lx\n", get_MIDR_EL1_reg()); pr_info("SCTLR_EL1 = %lx\n", get_SCTLR_EL1_reg()); return 0; } static void armcpuinfo_exit(void) { pr_info("%s\n", __FUNCTION__); } MODULE_LICENSE("GPL v2"); module_init(armcpuinfo_init); module_exit(armcpuinfo_exit); }} #highlight(end) ※Raspberry PiはRaspberry Pi財団の登録商標です。 #htmlinsert(rpi3b+.html);