-
Notifications
You must be signed in to change notification settings - Fork 1
/
gdt.h
62 lines (53 loc) · 2.88 KB
/
gdt.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef GDT_H
#define GDT_H
#include <stdint.h>
#include "config.h"
/* Access byte bit masks
* TODO: use these
*/
#define PRESENT_MASK 0x80
#define DPL_MASK 0x60
#define DESCRIPTOR_TYPE_MASK 0x10
#define EXECUTABLE_MASK 0x08
#define DC_MASK 0x04
#define RW_MASK 0x02
#define ACCESSED_MASK 0x01
struct segment_descriptor_raw {
uint16_t segment_limit; // The size of the segment.
uint16_t base_first; // The location of byte 0 of the segment within the 4 GB linear address space
uint8_t base;
uint8_t access; // P (1 bit, msb). DPL (2 bits). S (1 bit). Type (4 bits, lsb).
// P: Indicates whether the segment is present in memory (set) or not present (clear).
// DPL: Specifies the privilege level of the segment and is used to control access.
// S: Specifies whether the segment descriptor is for a system segment (S flag is clear) or a code or data segment (S flag is set).
// Type: Indicates the segment or gate type and specifies the kinds of access that can be made to the segment and the direction of growth.
uint8_t high_flags; // G (1 bit, msb). D/B (1 bit). L (1 bit). AVL (1 bit). Seg Limit (4 bits).
// G: Granularity. If flag is clear, the segment limit is interpreted in byte units; when flag is set, the segment limit is interpreted in 4-KByte units
// D/B: Performs different functions depending on whether the segment descriptor is an executable code segment, an expand-down data segment, or a stack segment.
// L: 64-bit code segment flag
// AVL: reserved for system software
// Seg Limit: continuation of first 16 bits.
uint8_t base_24_31_bits;
} __attribute__((packed));
struct segment_descriptor {
/* These fields are byte granular */
/* Thus, base and limit can be max 4 GB. */
uint32_t base;
uint32_t limit;
uint8_t type; // Corresponds to Access byte
};
/* TODO: this is a good idea eventually - just have to figure out the assembly intepretation
struct gdt {
struct segment_descriptor_raw gdt_entry[TOTAL_GDT_SEGMENTS];
int num_entries;
};
*/
/* Converts a list of segment_descriptor into a list of segment_descriptor_raw
* total_entries is the length of each list
*/
void segment_descriptor_to_raw(struct segment_descriptor_raw* raw, struct segment_descriptor *desc, int total_entries);
/* Load gdt by updating gdtr to reference it. Size is the number of entries in the gdt
* TODO: this function should take gdt and do gdt_raw conversion itself
*/
void gdt_load(struct segment_descriptor_raw *gdt, uint16_t size);
#endif