|
1 | 1 | /* $Id$ */ |
2 | | -/* Copyright (c) 2018 Pierre Pronchery <[email protected]> */ |
| 2 | +/* Copyright (c) 2018-2025 Pierre Pronchery <[email protected]> */ |
3 | 3 | /* This file is part of DeforaOS uKernel */ |
4 | 4 |
|
5 | 5 |
|
6 | 6 |
|
7 | 7 | #if defined(__amd64__) || defined(__i386__) |
| 8 | +# include <limits.h> |
8 | 9 | # include <stdint.h> |
9 | 10 | # include <string.h> |
10 | 11 | # include <errno.h> |
11 | 12 | # include "arch/amd64/gdt.h" |
12 | 13 | # include "arch/i386/gdt.h" |
13 | 14 |
|
14 | 15 |
|
| 16 | +/* constants */ |
| 17 | +# define GDT_ENTRIES_MAX 8192 |
| 18 | +# define GDT_LIMIT_MAX 0x000fffff |
| 19 | + |
| 20 | +/* access */ |
| 21 | +# define GDT_ACCESS_SET 0x01 |
| 22 | +# define GDT_ACCESS_RW 0x02 |
| 23 | +# define GDT_ACCESS_X 0x08 |
| 24 | +# define GDT_ACCESS_SEGMENT 0x10 |
| 25 | +# define GDT_ACCESS_RING(level) ((level) << 5) |
| 26 | +# define GDT_ACCESS_PRESENT 0x80 |
| 27 | + |
| 28 | +/* flags */ |
| 29 | +# define GDT_FLAG_LONG_MODE 0x2 /* 64-bit code segment */ |
| 30 | +# define GDT_FLAG_PROTECTED_MODE 0x4 /* 32-bit protected mode |
| 31 | + code segment */ |
| 32 | +# define GDT_FLAG_PAGE_GRANULARITY 0x8 /* 4 KB page size */ |
| 33 | + |
| 34 | + |
| 35 | +/* types */ |
| 36 | +#pragma pack(1) |
| 37 | +typedef struct _GDTEntry |
| 38 | +{ |
| 39 | + uint8_t limit0; |
| 40 | + uint8_t limit1; |
| 41 | + uint8_t base0; |
| 42 | + uint8_t base1; |
| 43 | + uint8_t base2; |
| 44 | + uint8_t access; |
| 45 | + unsigned int limit2:4; |
| 46 | + unsigned int flags:4; |
| 47 | + uint8_t base3; |
| 48 | +} GDTEntry; |
| 49 | +#pragma pack() |
| 50 | + |
| 51 | +struct _GDT |
| 52 | +{ |
| 53 | + GDTEntry entries[GDT_ENTRIES_MAX]; |
| 54 | + size_t entries_cnt; |
| 55 | +}; |
| 56 | + |
| 57 | + |
15 | 58 | /* prototypes */ |
16 | | -extern void __arch_setgdt(uint8_t * buf, size_t count); |
| 59 | +extern void __arch_setgdt(GDTEntry const * entries, size_t count); |
17 | 60 |
|
18 | 61 |
|
19 | 62 | /* variables */ |
20 | | -static uint8_t _buf[65536]; |
| 63 | +static GDT _gdt; |
21 | 64 |
|
22 | 65 |
|
23 | 66 | /* functions */ |
24 | | -/* arch_setgdt */ |
25 | | -int _arch_setgdt(GDT const * gdt, size_t count) |
| 67 | +/* gdt_init */ |
| 68 | +GDT * gdt_init(void) |
| 69 | +{ |
| 70 | + memset(&_gdt.entries[0], 0, sizeof(_gdt.entries[0])); |
| 71 | + _gdt.entries_cnt = 1; |
| 72 | + return &_gdt; |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +/* gdt_init_table */ |
| 77 | +int gdt_init_table(GDTTable const * table, size_t count) |
26 | 78 | { |
27 | | - uint8_t * buf = _buf; |
| 79 | + int ret = 0; |
| 80 | + GDT * gdt; |
28 | 81 | size_t i; |
29 | | - GDT g; |
30 | 82 |
|
31 | | - memset(&_buf, 0, sizeof(_buf)); |
32 | | - /* check for errors */ |
33 | | - if(count == 0 || count > sizeof(_buf) / sizeof(*gdt)) |
34 | | - { |
35 | | - errno = ERANGE; |
36 | | - return -1; |
37 | | - } |
| 83 | + gdt = gdt_init(); |
38 | 84 | for(i = 0; i < count; i++) |
39 | | - { |
40 | | - g = gdt[i]; |
41 | | - buf = &_buf[sizeof(g) * i]; |
42 | | - if(g.limit > 65536) |
43 | | - { |
44 | | - /* make sure the limit can be encoded */ |
45 | | - if((g.limit & 0xfff) != 0xfff) |
46 | | - return -1; |
47 | | - g.limit = g.limit >> 12; |
48 | | - buf[6] = 0xc0; |
49 | | - } |
50 | | - else |
51 | | - buf[6] = 0x40; |
52 | | - //encode the limit |
53 | | - buf[0] = g.limit & 0xff; |
54 | | - buf[1] = (g.limit >> 8) & 0xff; |
55 | | - buf[6] |= (g.limit >> 16) & 0xf; |
56 | | - //encode the base |
57 | | - buf[2] = g.base & 0xff; |
58 | | - buf[3] = (g.base >> 8) & 0xff; |
59 | | - buf[4] = (g.base >> 16) & 0xff; |
60 | | - buf[7] = (g.base >> 24) & 0xff; |
61 | | - //encode the type |
62 | | - buf[5] = g.type; |
63 | | - } |
64 | | - __arch_setgdt(_buf, count); |
| 85 | + if((ret = gdt_append(gdt, table[i].base, table[i].size, |
| 86 | + table[i].prot)) != 0) |
| 87 | + return ret; |
| 88 | + gdt_apply(gdt); |
65 | 89 | return 0; |
66 | 90 | } |
67 | 91 |
|
68 | 92 |
|
69 | | -/* arch_setgdt64 */ |
70 | | -int _arch_setgdt64(GDT const * gdt, size_t count) |
| 93 | +/* useful */ |
| 94 | +/* gdt_append */ |
| 95 | +int gdt_append(GDT * gdt, vaddr_t base, size_t size, unsigned int prot) |
71 | 96 | { |
72 | | - uint8_t * buf; |
73 | | - size_t i; |
74 | | - GDT g; |
| 97 | + GDTEntry * entry; |
| 98 | + uint32_t limit; |
| 99 | + uint8_t access = GDT_ACCESS_PRESENT |
| 100 | + | GDT_ACCESS_SEGMENT | GDT_ACCESS_RING(0); |
| 101 | + uint8_t flags = GDT_FLAG_PROTECTED_MODE; |
75 | 102 |
|
76 | | - memset(&_buf, 0, sizeof(_buf)); |
77 | 103 | /* check for errors */ |
78 | | - if(count == 0 || count > sizeof(_buf) / sizeof(*gdt)) |
| 104 | + if(size == 0) |
79 | 105 | { |
80 | 106 | errno = ERANGE; |
81 | 107 | return -1; |
82 | 108 | } |
83 | | - for(i = 0; i < count; i++) |
| 109 | + if(gdt->entries_cnt >= sizeof(gdt->entries) / sizeof(*gdt->entries) |
| 110 | + || size > ULONG_MAX |
| 111 | + || ULONG_MAX - size < base) |
| 112 | + { |
| 113 | + errno = ENOMEM; |
| 114 | + return -1; |
| 115 | + } |
| 116 | + if(prot != PROT_READ |
| 117 | + && prot != (PROT_READ | PROT_WRITE) |
| 118 | + && prot != (PROT_READ | PROT_EXEC)) |
| 119 | + { |
| 120 | + errno = EPERM; |
| 121 | + return -1; |
| 122 | + } |
| 123 | + entry = &gdt->entries[gdt->entries_cnt]; |
| 124 | + /* base */ |
| 125 | + entry->base0 = base & 0xff; |
| 126 | + entry->base1 = (base & 0xff00) >> 8; |
| 127 | + entry->base2 = (base & 0xff0000) >> 16; |
| 128 | + entry->base3 = (base & 0xff000000) >> 24; |
| 129 | + /* limit */ |
| 130 | + if(size - 1 > GDT_LIMIT_MAX) |
84 | 131 | { |
85 | | - g = gdt[i]; |
86 | | - buf = &_buf[sizeof(g) * i]; |
87 | | - if(g.limit > 65536) |
88 | | - { |
89 | | - /* make sure the limit can be encoded */ |
90 | | - if((g.limit & 0xfff) != 0xfff) |
91 | | - return -1; |
92 | | - g.limit = g.limit >> 12; |
93 | | - buf[6] = 0xa0; |
94 | | - } |
95 | | - else |
96 | | - buf[6] = 0x20; |
97 | | - //encode the limit |
98 | | - buf[0] = g.limit & 0xff; |
99 | | - buf[1] = (g.limit >> 8) & 0xff; |
100 | | - buf[6] |= (g.limit >> 16) & 0xf; |
101 | | - //encode the base |
102 | | - buf[2] = g.base & 0xff; |
103 | | - buf[3] = (g.base >> 8) & 0xff; |
104 | | - buf[4] = (g.base >> 16) & 0xff; |
105 | | - buf[7] = (g.base >> 24) & 0xff; |
106 | | - //encode the type |
107 | | - buf[5] = g.type; |
| 132 | + limit = (size & 0xfff) == 0 |
| 133 | + ? (size >> 12) |
| 134 | + : (((size | 0xfff) + 1) >> 12); |
| 135 | + limit--; |
| 136 | + flags |= GDT_FLAG_PAGE_GRANULARITY; |
108 | 137 | } |
109 | | - __arch_setgdt(_buf, count); |
| 138 | + else |
| 139 | + limit = size - 1; |
| 140 | + entry->limit0 = limit & 0xff; |
| 141 | + entry->limit1 = (limit & 0xff00) >> 8; |
| 142 | + entry->limit2 = (limit & 0xf0000) >> 16; |
| 143 | + /* access */ |
| 144 | + if(prot == (PROT_READ | PROT_EXEC)) |
| 145 | + /* code segment */ |
| 146 | + access |= GDT_ACCESS_RW | GDT_ACCESS_X; |
| 147 | + else if(prot == (PROT_READ | PROT_WRITE)) |
| 148 | + /* data segment (read/write) */ |
| 149 | + access |= GDT_ACCESS_RW; |
| 150 | + else if(prot == PROT_READ) |
| 151 | + /* data segment (read-only) */ |
| 152 | + access |= GDT_ACCESS_SET; |
| 153 | + entry->access = access; |
| 154 | + /* flags */ |
| 155 | + entry->flags = flags; |
| 156 | + gdt->entries_cnt++; |
110 | 157 | return 0; |
111 | 158 | } |
| 159 | + |
| 160 | + |
| 161 | +/* gdt_apply */ |
| 162 | +void gdt_apply(GDT * gdt) |
| 163 | +{ |
| 164 | + __arch_setgdt(gdt->entries, gdt->entries_cnt); |
| 165 | +} |
112 | 166 | #endif |
0 commit comments