forked from SanseroGames/LetsGo-OS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegments.go
239 lines (195 loc) · 5.22 KB
/
segments.go
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"unsafe"
)
type GdtEntry struct {
limitLow uint16
baseLow uint16
baseMid uint8
access uint8
limitHighAndFlags uint8
baseHigh uint8
}
func (e *GdtEntry) IsPresent() bool {
return e.access&PRESENT != 0
}
func (e *GdtEntry) Fill(base uint32, limit uint32, access uint8, flags uint8) {
e.limitHighAndFlags = uint8((limit >> 16) & 0x000F)
e.limitHighAndFlags |= flags
e.access = access
e.baseMid = uint8(base >> 16)
e.baseHigh = uint8(base >> 24)
e.baseLow = uint16(base & 0xFFFF)
e.limitLow = uint16(limit & 0x0000FFFF)
}
func (e *GdtEntry) Clear() {
e.limitHighAndFlags = 0
e.access = 0
e.baseMid = 0
e.baseHigh = 0
e.baseLow = 0
e.limitLow = 0
}
type GdtSegment struct {
base uintptr
limit uintptr
access uint8
flags uint8
}
func (s *GdtSegment) IsPresent() bool {
return s.access&PRESENT != 0
}
func (s *GdtSegment) Clear() {
s.base = 0
s.limit = 0
s.access = 0
s.flags = 0
}
type GdtDescriptor struct {
GdtSize uint16
GdtAddressLow uint16
GdtAddressHigh uint16
}
type UserDesc struct {
EntryNumber uint32
BaseAddr uint32
Limit uint32
Flags uint8
}
type TlsEntry struct {
Desc UserDesc
Present bool
}
const (
KCS_INDEX = 1
KDS_INDEX = 2
KGS_INDEX = 3
KCS_SELECTOR = KCS_INDEX * 8
KDS_SELECTOR = KDS_INDEX * 8
KGS_SELECTOR = KGS_INDEX * 8
// flags
SEG_GRAN_BYTE = 0 << 7
SEG_GRAN_4K_PAGE = 1 << 7
// access
SEG_NORW = 0 << 1
SEG_R = 1 << 1
SEG_W = 1 << 1
SEG_SYSTEM = 0 << 4
SEG_NORMAL = 1 << 4
SEG_NOEXEC = 0 << 3
SEG_EXEC = 1 << 3
PRIV_KERNEL = 0 << 5
PRIV_USER = 3 << 5
SEG_BIG_MODE = 1 << 6
PRESENT = 1 << 7
// userDesc flags
UDESC_32SEG = 1 << 0 // I ignore this
UDESC_CONTENTS = 3 << 1 // don't know what those are
UDESC_RX_ONLY = 1 << 3
UDESC_LIMIT_IN_PAGES = 1 << 4
UDESC_SEG_NOT_PRESENT = 1 << 5
UDESC_USABLE = 1 << 6
// Misc
GDT_ENTRIES = 256
TLS_START = 16
)
var (
gdtTable = [GDT_ENTRIES]GdtEntry{}
gdtDescriptor GdtDescriptor
gdtTableLen = 0
)
func installGDT(descriptor *GdtDescriptor)
func getGDT() *GdtDescriptor
func InitSegments() {
var gdtEntry GdtEntry
Memclr(uintptr(unsafe.Pointer(&gdtTable)), len(gdtTable))
gdtDescriptor = *getGDT()
oldGdtAddr := uintptr(uint32(gdtDescriptor.GdtAddressLow) |
uint32(gdtDescriptor.GdtAddressHigh)<<16)
oldGdtLen := int(uintptr(gdtDescriptor.GdtSize+1) / unsafe.Sizeof(gdtEntry))
oldGdt := unsafe.Slice((*GdtEntry)(unsafe.Pointer(oldGdtAddr)), oldGdtLen)
copy(gdtTable[:], oldGdt)
gdtAddr := uintptr(unsafe.Pointer(&gdtTable))
gdtDescriptor.GdtAddressLow = uint16(gdtAddr)
gdtDescriptor.GdtAddressHigh = uint16(gdtAddr >> 16)
gdtDescriptor.GdtSize = GDT_ENTRIES - 1
gdtTableLen += oldGdtLen
installGDT(&gdtDescriptor)
//printGdt(gdtTable[:gdtTableLen])
}
func GetSegment(index int, res *GdtSegment) int {
if index < 0 || index > len(gdtTable) {
return -1
}
res.limit = uintptr(gdtTable[index].limitLow)
res.limit += uintptr((gdtTable[index].limitHighAndFlags & 0xF)) << 16
res.base = uintptr(gdtTable[index].baseLow)
res.base += uintptr(gdtTable[index].baseMid) << 16
res.base += uintptr(gdtTable[index].baseHigh) << 24
res.access = gdtTable[index].access
res.flags = gdtTable[index].limitHighAndFlags & 0xF0
return 0
}
func AddSegment(base uintptr, limit uintptr, access uint8, flags uint8) int {
if gdtTableLen < 0 || gdtTableLen > TLS_START {
return -1
}
doUpdateSegment(gdtTableLen, base, limit, access, flags)
gdtTableLen++
return gdtTableLen - 1
}
func UpdateSegment(index int, base uintptr, limit uintptr, access uint8, flags uint8) bool {
if index < 0 || index > gdtTableLen {
return false
}
doUpdateSegment(index, base, limit, access, flags)
return true
}
// Does not check if index in range
func doUpdateSegment(index int, base uintptr, limit uintptr, access uint8, flags uint8) {
gdtTable[index].limitHighAndFlags = uint8((limit >> 16) & 0x000F)
bigMode := uint8(0)
if access&SEG_NORMAL != 0 {
bigMode = 1 << 6
}
gdtTable[index].limitHighAndFlags |= flags | bigMode
gdtTable[index].access = access | PRESENT
gdtTable[index].baseMid = uint8(base >> 16)
gdtTable[index].baseHigh = uint8(base >> 24)
gdtTable[index].baseLow = uint16(base & 0xFFFF)
gdtTable[index].limitLow = uint16(limit & 0x0000FFFF)
}
func SetTlsSegment(index uint32, desc *UserDesc, table []GdtEntry) bool {
if index < TLS_START || index > uint32(len(gdtTable)) {
return false
}
if desc.Flags == UDESC_RX_ONLY|UDESC_SEG_NOT_PRESENT {
// Desc is considered emtpy. Clear entry
table[index].Clear()
return true
}
flags := uint8(SEG_GRAN_BYTE)
if desc.Flags&UDESC_LIMIT_IN_PAGES != 0 {
flags = SEG_GRAN_4K_PAGE
}
access := uint8(PRIV_USER | SEG_NORMAL | PRESENT)
if desc.Flags&UDESC_RX_ONLY != 0 {
access |= SEG_EXEC
} else {
access |= SEG_W
}
if access&SEG_NORMAL != 0 {
flags |= SEG_BIG_MODE
}
table[index].Fill(desc.BaseAddr, desc.Limit, access, flags)
FlushTlsTable(table)
return true
}
func FlushTlsTable(table []GdtEntry) {
copy(gdtTable[TLS_START:], table[TLS_START:])
}
func printGdt(gdt []GdtEntry) {
for _, n := range gdt {
kdebugln(n.limitLow, " ", n.baseLow, " ", n.baseMid, " ", n.access, " ", n.limitHighAndFlags, " ", n.baseHigh)
}
}