2

I have created a simple linux kernel module to get the base address of GDT table:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/const.h>
#include <linux/errno.h>

// try changing segment selector to  null 3.4.2
// try swapg in 64 bit 3.4.4


static inline uint64_t get_gdt_base1(void)
{
    struct desc_ptr gdt;
    __asm__ __volatile__("sgdt %[gdt]"
                 : /* output */ [gdt]"=m"(gdt));
    return gdt.address;
}


int __init start_init(void)
{

    uint64_t gdt_base = get_gdt_base1();
    printk(KERN_INFO "Address of gdt is %llx\n", (long long)gdt_base);
}

static void __exit end_exit(void)
{
    printk(KERN_INFO "Unloading the driver\n");
    return;
}

module_init(start_init);
module_exit(end_exit);


MODULE_LICENSE("GPL V3");
MODULE_AUTHOR("Shubham Dubey");
MODULE_DESCRIPTION("CRO register ");


When I have loaded and unloaded the module few times. The output that I am getting are as follows:
1st run: Address of gdt is ffff88024f249000
2nd run: Address of gdt is ffff88024f2c9000
3rd run: Address of gdt is ffff88024f249000
4th run: Address of gdt is ffff88024f2c9000
...
...

Technically, the GDT base should be constant. But I am receiving two different address which isn't the normal behavior. Can anyone have a good explanation for this?

PS: Running debian in 64 bit intel processor

2

IIRC on Linux GDT is separate for each processor so you’re probably seeing the addresses for different processors.

3
  • Oh right. Never thought about that. That is probably the correct explanation but do you know or point me to some link for why each processor has different GDT?
    – shubham0d
    Feb 17 '20 at 18:09
  • This is borderline off topic anyway, try Stack Overflow or LKML.
    – Igor Skochinsky
    Feb 17 '20 at 18:11
  • Ok. sure. Thanks for the help.
    – shubham0d
    Feb 17 '20 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.