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?

0andriy
  • 4,183
  • 1
  • 24
  • 37
  • Ok, but I want "intel core i5" not 1 or 42. What is the combination of 42? I don't understand what does it mean 42. :) –  Nov 21 '20 at 11:14
  • Yep, that's why i deleted my comment, because irrelevant. But nontheless, you want it in one line, then you have to call printk only once (in the format you like). – Erdal Küçük Nov 21 '20 at 11:21
  • Yes of course. However in doc I don't find how 42 means. Where I can see it? –  Nov 21 '20 at 11:27
  • Some sort of id, i think, now there must be somewhere a translation table of some sort. – Erdal Küçük Nov 21 '20 at 11:29
  • I think so too, I search. Thanks.If is there something wrong in code tell me. It is compile under opensuse Lead 15 because on debian was not right. –  Nov 21 '20 at 11:32
  • Maybe it helps, maybe not. Have a look at [x86 Built-in Functions](https://gcc.gnu.org/onlinedocs/gcc/x86-Built-in-Functions.html#x86-Built-in-Functions), Section: __builtin_cpu_is. From there, have a look at the gcc sources (especially ./gcc/common/config/i386/cpuinfo.h), there in the function get_intel_cpu in the switch case under 0x2a (42) you'll find 'sandybridge corei7'). At least a starting point in my opinion. – Erdal Küçük Nov 21 '20 at 12:10
  • printk(KERN_INFO "x86 model name %s\n",cpu.x86_model_id); return this [31157.595324] x86 model name \xff\xff\xff\xff\xa4`\x9d\xff\xff\xff\xff\xb0`\x9d\xff\xff\xff\xff\xa4`\x9d\xff\xff\xff\xff\xb0`\x9d\xff\xff\xff\xff\xa4`\x9d\xff\xff\xff\xff instead correct model name –  Nov 21 '20 at 13:14
  • There's no way to translate a family-model-stepping id to the brand name. You have to use a database of known values. You can find some on the internet, [here](https://github.com/InstLatx64/InstLatx64/) there is a nice list of CPUID output that you can parse to associate a FMS id to the brand name. [This site](https://www.cpu-world.com/cgi-bin/CPUID.pl?MANUF=&FAMILY=&MODEL=&SIGNATURE=263761&PART=&ACTION=Filter&STEPPING=) lets you search by CPUID (i.e. `CPUID.1.EAX`). As you will see, all databases are incomplete. There's a Intel utility, rev engineering that may be worth it. – Margaret Bloom Nov 22 '20 at 14:56
  • 1
    However, if you want the brand name, `CPUID.80000002-4` will give you that. Do the proper check to see if those leaves are supported. – Margaret Bloom Nov 22 '20 at 14:58
  • "There's no way to translate a family-model-stepping id to the brand name. You have to use a database of known values." Ok thanks. I try cpuid_level, it return 13. In database cpuid maybe find it. –  Nov 22 '20 at 15:29
  • Though, if cat /proc/cpuinfo I get "Intel (R) Core(TM) 15-2557M CPU @ 1.70GHZ" Somewhere must be equivalent in model id e vendor id. –  Nov 22 '20 at 15:36
  • 2
    Look how */proc/cpuinfo* works. – 0andriy Nov 22 '20 at 20:44
  • @zabnicola: "Intel (R) Core(TM) 15-2557M CPU @ 1.70GHZ" is the *processor brand string* which Margaret Bloom's previous comment mentions (CPUID.80000002-4). See for instance https://en.wikipedia.org/wiki/CPUID#EAX=80000002h,80000003h,80000004h:_Processor_Brand_String – Nate Eldredge Nov 22 '20 at 21:04
  • For examplein linux code proc file system is generated print brand name I want get brand name because I would write a device manager and bootstrapping. I'm studying assembly language for it. –  Nov 26 '20 at 14:55

0 Answers0