2019-03-26 18:20:23 +01:00
|
|
|
//----------------------------------------------------------------------------//
|
2020-09-27 17:33:48 +02:00
|
|
|
// OS on Kaleid //
|
2019-03-26 18:20:23 +01:00
|
|
|
// //
|
2020-01-12 16:53:23 +01:00
|
|
|
// Desc: GDT related functions //
|
2019-03-26 18:20:23 +01:00
|
|
|
// //
|
|
|
|
// //
|
2021-02-18 19:54:35 +01:00
|
|
|
// Copyright © 2018-2021 The OS/K Team //
|
2019-03-26 18:20:23 +01:00
|
|
|
// //
|
|
|
|
// This file is part of OS/K. //
|
|
|
|
// //
|
|
|
|
// OS/K 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 //
|
|
|
|
// any later version. //
|
|
|
|
// //
|
|
|
|
// OS/K 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 OS/K. If not, see <https://www.gnu.org/licenses/>. //
|
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
|
2020-01-12 16:53:23 +01:00
|
|
|
#include <mm/gdt.h>
|
|
|
|
#include <mm/paging.h>
|
2019-05-22 00:38:04 +02:00
|
|
|
#include <init/boot.h>
|
2019-03-26 18:20:23 +01:00
|
|
|
|
2020-01-06 00:30:50 +01:00
|
|
|
GdtPtr_t gdtPtr;
|
2021-02-28 14:06:29 +01:00
|
|
|
GdtEntry_t gdt[7] __attribute__((__aligned__(KPAGESIZE)));
|
2020-01-06 00:30:50 +01:00
|
|
|
TssDescriptor_t tssDesc __attribute__((__aligned__(KPAGESIZE)));
|
|
|
|
Tss_t tss __attribute__((__aligned__(KPAGESIZE)));
|
2019-03-29 10:29:05 +01:00
|
|
|
|
2019-05-19 23:33:34 +02:00
|
|
|
void MmInitGdt(void)
|
|
|
|
{
|
2021-02-28 14:06:29 +01:00
|
|
|
ushort tssOffset = (ushort)((ulong)(&gdt[5]) - (ulong)(&gdt[0]));
|
2019-05-22 00:38:04 +02:00
|
|
|
|
|
|
|
gdtPtr.base = (ulong)&gdt[0];
|
|
|
|
gdtPtr.limit = sizeof(gdt) - 1;
|
|
|
|
|
|
|
|
memzero((void *)&gdt[0], sizeof(gdt));
|
|
|
|
memzero((void *)&tssDesc, sizeof(tssDesc));
|
|
|
|
memzero((void *)&tss, sizeof(tss));
|
2021-02-27 01:03:09 +01:00
|
|
|
|
2021-02-26 13:18:20 +01:00
|
|
|
// Kernel codeseg
|
2021-03-01 16:29:37 +01:00
|
|
|
gdt[1].access = PRESENTSEG | READABLE | RESERVED | RESERVED2;
|
2021-03-01 16:07:39 +01:00
|
|
|
gdt[1].flags = LONG;
|
2021-02-26 13:18:20 +01:00
|
|
|
|
2021-02-28 14:06:29 +01:00
|
|
|
// Kernel dataseg
|
2021-03-01 16:29:37 +01:00
|
|
|
gdt[2].access = PRESENTSEG | READABLE | RESERVED2;
|
|
|
|
gdt[2].flags = LONG;
|
2021-02-28 14:06:29 +01:00
|
|
|
|
2021-02-26 13:18:20 +01:00
|
|
|
// User codeseg
|
2021-03-01 16:07:39 +01:00
|
|
|
gdt[3].access = PRESENTSEG | RESERVED | RESERVED2 | USER | USER2;
|
|
|
|
gdt[3].flags = LONG;
|
|
|
|
|
|
|
|
// User dataseg
|
2021-03-01 16:29:37 +01:00
|
|
|
gdt[4].access = PRESENTSEG | READABLE | RESERVED2 | USER | USER2;
|
2020-01-07 16:56:46 +01:00
|
|
|
|
2021-03-01 16:07:39 +01:00
|
|
|
tssDesc.access = TSS_TYPE | PRESENTSEG;
|
2019-05-22 00:38:04 +02:00
|
|
|
tssDesc.lowBase = (ulong)&tss & 0xFFFF;
|
|
|
|
tssDesc.middleBase = ((ulong)&tss >> 16) & 0xFF;
|
|
|
|
tssDesc.highBase = ((ulong)&tss >> 24) & 0xFF;
|
|
|
|
tssDesc.veryHighBase = ((ulong)&tss >> 32) & 0xFFFFFFFF;
|
|
|
|
tssDesc.lowLimit = sizeof(tss);
|
|
|
|
|
2020-02-08 11:53:20 +01:00
|
|
|
tss.ist1 = (ulong)memalign(4*MB, 4*KB) + 4*MB; // ISR RESCUE STACK
|
|
|
|
tss.ist2 = (ulong)memalign(4*MB, 4*KB) + 4*MB; // ISR STACK
|
|
|
|
tss.ist3 = (ulong)memalign(4*MB, 4*KB) + 4*MB; // ISR STACK
|
2021-02-26 13:18:20 +01:00
|
|
|
tss.rsp0 = (ulong)memalign(4*MB, 4*KB) + 4*MB; // Another Stack
|
2019-05-22 00:38:04 +02:00
|
|
|
tss.iomap_base = sizeof(tss);
|
|
|
|
|
2021-02-28 14:06:29 +01:00
|
|
|
memmove(&gdt[5], &tssDesc, sizeof(TssDescriptor_t));
|
2019-05-22 00:38:04 +02:00
|
|
|
|
2021-02-26 13:18:20 +01:00
|
|
|
DebugLog("GDT & TSS initialized\n");
|
2021-02-28 14:06:29 +01:00
|
|
|
DebugLog("Null descriptor : %#p\n", &gdt[0]);
|
|
|
|
DebugLog("Kernel code descriptor : %#p\n", &gdt[1]);
|
|
|
|
DebugLog("Kernel data descriptor : %#p\n", &gdt[2]);
|
2021-03-01 16:07:39 +01:00
|
|
|
DebugLog("User code descriptor : %#p\n", &gdt[3]);
|
|
|
|
DebugLog("User data descriptor : %#p\n", &gdt[4]);
|
|
|
|
DebugLog("Task-switch descriptor : %#p\n", &gdt[5]);
|
2021-02-28 14:06:29 +01:00
|
|
|
DebugLog("ist1 : %#p\n", tss.ist1);
|
|
|
|
DebugLog("ist2 : %#p\n", tss.ist2);
|
|
|
|
DebugLog("ist3 : %#p\n", tss.ist3);
|
|
|
|
DebugLog("rsp0 : %#p\n", tss.ist1);
|
2019-05-22 00:38:04 +02:00
|
|
|
|
|
|
|
MmLoadGdt(&gdtPtr, tssOffset);
|
2019-03-29 10:29:05 +01:00
|
|
|
}
|