87 lines
1.6 KiB
C
87 lines
1.6 KiB
C
#ifndef _VISOR_GDT_H_
|
|
#define _VISOR_GDT_H_
|
|
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
|
|
__attribute__ ((packed))
|
|
struct segdescr {
|
|
uint16_t limit0;
|
|
uint16_t base0;
|
|
uint8_t base1;
|
|
uint8_t access;
|
|
uint8_t flags;
|
|
uint8_t base2;
|
|
};
|
|
|
|
__attribute__ ((packed))
|
|
struct GDTR {
|
|
uint16_t limit;
|
|
struct segdescr *base;
|
|
};
|
|
|
|
__attribute__ ((packed))
|
|
struct TSS {
|
|
uint32_t reserved0;
|
|
uint64_t rsp0;
|
|
uint64_t rsp1;
|
|
uint64_t rsp2;
|
|
uint64_t reserved1;
|
|
uint64_t ist1;
|
|
uint64_t ist2;
|
|
uint64_t ist3;
|
|
uint64_t ist4;
|
|
uint64_t ist5;
|
|
uint64_t ist6;
|
|
uint64_t ist7;
|
|
uint64_t reserved2;
|
|
uint16_t reserved3;
|
|
uint16_t iopb;
|
|
};
|
|
|
|
static inline struct GDTR
|
|
storegdt(void)
|
|
{
|
|
struct GDTR gdtr;
|
|
__asm__ ("sgdt %0\n\t" : "=m"(gdtr));
|
|
return gdtr;
|
|
}
|
|
|
|
static inline void
|
|
setsegdescr(struct segdescr *descr, uint32_t base, uint32_t limit, uint8_t access, uint8_t flags)
|
|
{
|
|
descr->limit0 = limit;
|
|
descr->base0 = base;
|
|
descr->base1 = base >> 16;
|
|
descr->access = access;
|
|
descr->flags = flags | (limit >> 16);
|
|
descr->base2 = base >> 24;
|
|
}
|
|
|
|
static inline uint32_t
|
|
getsegbase(struct segdescr *descr)
|
|
{
|
|
return descr->base0 | ((uint32_t)descr->base1 << 16) | ((uint32_t)descr->base2 << 24);
|
|
}
|
|
|
|
static inline uint32_t
|
|
getsegbaseext(struct segdescr *descr)
|
|
{
|
|
uint32_t *words = (void *)descr;
|
|
return words[0];
|
|
}
|
|
|
|
static inline uint32_t
|
|
getseglimit(struct segdescr *descr)
|
|
{
|
|
return descr->limit0 | ((uint32_t)(descr->flags & 0xf) << 16);
|
|
}
|
|
|
|
static inline uint32_t
|
|
getsegaccess(struct segdescr *descr, bool unusable)
|
|
{
|
|
uint32_t *words = (void *)descr;
|
|
return (words[1] & 0x00ffffff) | (unusable ? 0x10000 : 0);
|
|
}
|
|
|
|
#endif
|