GDT

From vegard.wiki
Revision as of 22:32, 14 December 2019 by Vegard (talk | contribs) (new page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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.