aboutsummaryrefslogtreecommitdiff
path: root/kernel/include/boot/gdt.h
blob: ff87981b250d1fbefc34ff0c5b1deab96f457eb5 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * bubbl
 * Copyright (C) 2024-2025  Raghuram Subramani <raghus2247@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __boot_gdt_h
#define __boot_gdt_h

#include <common.h>
#include <stdint.h>

/* Access Flags:
 * 7 PRESENT
 * 6 PRIVILEGE
 * 5 PRIVILEGE
 * 4 TYPE
 * 3 EXECUTABLE
 * 2 DIRECTION/CONFORMING
 * 1 READABLE/WRITABLE
 * 0 ACCESSED
 */

#define PRESENT(x) (x << 7)
#define PRIVILEGE(x) (x << 5)
#define TYPE(x) (x << 4)
#define EXECUTABLE(x) (x << 3)
#define DIRECTION_CONFORMING(x) (x << 2)
#define READABLE_WRITABLE(x) (x << 1)
#define ACCESSED(x) x

#define CODE_SEGMENT_ACCESS_FLAGS(privilege)                                  \
  PRESENT(1) | PRIVILEGE(privilege) | TYPE(1) | EXECUTABLE(1)                 \
      | DIRECTION_CONFORMING(0) | READABLE_WRITABLE(1) | ACCESSED(0)

#define DATA_SEGMENT_ACCESS_FLAGS(privilege)                                  \
  PRESENT(1) | PRIVILEGE(privilege) | TYPE(1) | EXECUTABLE(0)                 \
      | DIRECTION_CONFORMING(0) | READABLE_WRITABLE(1) | ACCESSED(0)

#define KERNEL_CODE_SEGMENT_ACCESS_FLAGS CODE_SEGMENT_ACCESS_FLAGS(0)
#define KERNEL_DATA_SEGMENT_ACCESS_FLAGS DATA_SEGMENT_ACCESS_FLAGS(0)

#define USER_CODE_SEGMENT_ACCESS_FLAGS CODE_SEGMENT_ACCESS_FLAGS(3)
#define USER_DATA_SEGMENT_ACCESS_FLAGS DATA_SEGMENT_ACCESS_FLAGS(3)

/* Other Flags:
 * 3 GRANULARITY
 * 2 SIZE
 * 1 LONGMODE
 * 0 RESERVED
 */

#define GRANULARITY (1 << 3)
#define SIZE (1 << 2)
#define LONGMODE (0 << 1)
#define RESERVED 0

#define FLAGS (GRANULARITY | SIZE | LONGMODE | RESERVED)

#define FLAGS_LIMIT_HIGH(flags, limit_high) ((flags << 4) | limit_high)

/* GDT Entry:
 * BASE  FLAGS  LIMIT  ACCESS_FLAGS  BASE  BASE  LIMIT
 * 8bit  4bit   4bit   8bit          8bit  16bit 16bit
 */

#define GDT_ENTRY(base, limit, access_flags, flags)                           \
  {                                                                           \
    (limit & 0xffff),          /* limit_low */                                \
        (base & 0xffff),       /* base_low */                                 \
        ((base >> 16) & 0xff), /* base_mid */                                 \
        access_flags,          /* access_flags */                             \
        FLAGS_LIMIT_HIGH(flags,                                               \
                         ((limit >> 16) & 0xff)), /* flags_limit_high */      \
        ((base >> 24) & 0xff)                     /* base_high */             \
  }

namespace GDT
{

typedef struct {
  uint16_t limit_low;
  uint16_t base_low;
  uint8_t base_mid;
  uint8_t access_flags;
  uint8_t flags_limit_high;
  uint8_t base_high;
} PACKED entry_t;

typedef struct {
  uint16_t limit; /* sizeof(GDT) - 1 */
  entry_t *ptr;   /* Address of GDT */
} PACKED descriptor_t;

extern "C" void _GDT_flush(descriptor_t *descriptor);
void load(void);

}

#endif