human readable struct cpuinfo_x86 kernel linux

0
/*file m_example.h*/

void cpu_detect(struct cpuinfo_x86 *c);
/*file m_example.c*/
#include <linux/module.h>    // included for all kernel modules
#include <linux/kernel.h>    // included for KERN_INFO
#include <linux/init.h>      // included for __init and __exit macros
#include "m_example.h"

unsigned int x86_family(unsigned int sig){
   unsigned int x86;

   x86 = (sig >> 8) & 0xf;

   if (x86 == 0xf)
        x86 += (sig >> 20) & 0xff;

   return x86;
   }

 unsigned int x86_model(unsigned int sig){
     unsigned int fam, model;

     fam = x86_family(sig);

     model = (sig >> 4) & 0xf;



     if (fam >= 0x6)
        model += ((sig >> 16) & 0xf) << 4;

    return model;
}

unsigned int x86_stepping(unsigned int sig){
    return sig & 0xf;
}


void cpu_detect(struct cpuinfo_x86 *c){
    // Get vendor name
    cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
         (unsigned int *)&c->x86_vendor_id[0],
         (unsigned int *)&c->x86_vendor_id[8],
         (unsigned int *)&c->x86_vendor_id[4]);

    c->x86 = 4;
    // Intel-defined flags: level 0x00000001
    if (c->cpuid_level >= 0x00000001) {
        u32 junk, tfms, cap0, misc;

        cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
        c->x86      = x86_family(tfms);
        c->x86_model    = x86_model(tfms);
        c->x86_stepping = x86_stepping(tfms);

        if (cap0 & (1<<19)) {
           c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
           c->x86_cache_alignment = c->x86_clflush_size;
        }
    }
}

MODULE_LICENSE("GPL");
MODULE_AUTHOR("zabnicola");
MODULE_DESCRIPTION("A Simple Hello World module");

static int __init hello_init(void){
    struct cpuinfo_x86 cpu;
   printk(KERN_INFO "Load module 1\n");
   cpu_detect(&cpu);

   printk(KERN_INFO "x86 %u\n",cpu.x86);
   printk(KERN_INFO "x86 vendor %u\n",cpu.x86_vendor);
   printk(KERN_INFO "x86 model %u\n",cpu.x86_model);
   printk(KERN_INFO "x86 vendor name %s\n",cpu.x86_vendor_id);
   printk(KERN_INFO "x86 model name %s\n",cpu.x86_model_id);

   return 0;    // Non-zero return means that the module couldn't be loaded.
}

static void __exit hello_cleanup(void){
     printk(KERN_INFO "Cleaning up module 1\n");
}

module_init(hello_init);
module_exit(hello_cleanup);

It'possibile see message once is load module.

make

make -C /lib/modules/5.3.18-lp152.50-default/build M=/home/zabnicola/hello_mod_zab/hello modules

make[1]: Entering directory '/usr/src/linux-5.3.18-lp152.50-obj/x86_64/default'
CC [M]  /home/zabnicola/hello_mod_zab/hello/m_example.o
Building modules, stage 2.
MODPOST 1 modules
LD [M]  /home/zabnicola/hello_mod_zab/hello/m_example.ko

make[1]: Leaving directory '/usr/src/linux-5.3.18-lp152.50-obj/x86_64/default'

rmmod m_example.ko
insmod m_example.ko

dmesg | tail -5

[25691.619857] x86 6
[25691.619860] x86 **vendor 1**
[25691.619862] x86 **model 42**

How can I get vendor and model in human readable like CPU: Intel Core i5 1.7 GHz?

c
linux
linux-kernel
cpu
human-readable
asked on Stack Overflow Nov 21, 2020 by zabnicola • edited Nov 22, 2020 by 0andriy

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0