k-chow/k/gdt.h

69 lines
2.1 KiB
C
Raw Normal View History

2021-07-13 03:05:58 +00:00
#ifndef GDT_H
#define GDT_H
#include <stdint.h>
#define GDT_NB_ENTRIES 3
#define GDT_SIZE (GDT_NB_ENTRIES * sizeof(struct gdt_entry))
#define KERNEL_CS_INDEX 1
#define KERNEL_DS_INDEX 2
#define KERNEL_CS_SEGMENT_SELECTOR {0, 0, KERNEL_CS_INDEX}
#define KERNEL_DS_SEGMENT_SELECTOR {0, 0, KERNEL_DS_INDEX}
2021-07-13 03:05:58 +00:00
struct gdt_entry
{
uint16_t limit_1 : 16; // 16 lsbs of the segment limit
uint16_t base_1 : 16; // 16 lsbs of the segment base address
uint8_t base_2 : 8; // 8 next bits of the segment base address
/* Access Byte */
uint8_t accessed : 1; // set to 0, is set to 1 by CPU when segment is
// accessed
uint8_t rw : 1; // read/write bit, 1 to grant read acces on code
// segments or write access on data segments
uint8_t dc : 1; // Direction/Conforming bit
uint8_t ex : 1; // Executable bit
uint8_t desc_type : 1; // Descriptor type: 1 for data or code segmentm
// 0 for system segments (like TSS)
uint8_t desc_priv : 2; // Descriptor privilege
uint8_t present : 1; // Always set to 1
uint8_t limit_2 : 4; // 4 msbs of the segment limit
/* Flags */
uint8_t available : 1; // Available for use by system software
uint8_t l : 1; // 64-bit code segment
uint8_t db : 1; // size, 0 for 16 bits, 1 for 32 bits
uint8_t granularity : 1; // limit unit, 0 for bytes, 1 for 4KiB (1 page)
uint8_t base_3 : 8; // 8 msbs of the segment base
} __attribute__ ((packed));
2021-07-13 03:05:58 +00:00
struct gdt
{
struct gdt_entry entries[GDT_NB_ENTRIES];
};
struct gdt_r
{
uint16_t limit; // total size of the GDT - 1 in bytes
uint32_t addr; // base address of the DGT
} __attribute__ ((packed));
struct segment_selector
{
uint8_t rpl : 2; // request privilege level
uint8_t table_indicator : 1; // 0 for GDT, 1 for LDT
uint16_t index : 13; // index of the entry in the table
} __attribute__ ((packed));
void init_gdt(void);
2021-07-13 03:05:58 +00:00
#endif /* !GDT_H */