GDT

From vegard.wiki
Jump to navigation Jump to search

You can get the (kernel mapped) address of the GDT using the sgdt instruction from userspace:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

struct desc_ptr {
        unsigned short size;
        unsigned long address;
} __attribute__((packed)) ;

static inline void sgdt(struct desc_ptr *dtr)
{
        asm volatile("sgdt %0" : "=m" (*dtr));
}

int main(int argc, char *argv[])
{
        while (1) {
                struct desc_ptr gdt_descr;
                sgdt(&gdt_descr);

                printf("%lx %u\n", gdt_descr.address, gdt_descr.size);

                usleep(100000);
        }

        return 0;
}

This program will typically bounce between CPUs and therefore print different addresses depending on which CPU the sgdt instructions runs on.