mm: replace GRUB's GDT with our own
parent
66a1f8726e
commit
8129518640
@ -0,0 +1,111 @@
|
||||
/* See the end of this file for copyright and license terms. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <gay/config.h>
|
||||
|
||||
/** @brief Kernel code segment GDT selector */
|
||||
#define X86_KERN_CS 0x08
|
||||
/** @brief Kernel data segment GDT selector */
|
||||
#define X86_KERN_DS 0x10
|
||||
|
||||
/** @brief Userland code segment GDT selector */
|
||||
#define X86_USER_CS 0x18
|
||||
/** @brief Userland data segment GDT selector */
|
||||
#define X86_USER_DS 0x20
|
||||
|
||||
#ifndef _ASM_SOURCE
|
||||
|
||||
/*
|
||||
* This code is mostly obsolete (until we need to actually manipulate the GDT
|
||||
* when there is long mode support and we want to enable 32-bit compatibility
|
||||
* mode). I wrote this because i initially wanted to use it for an encoder that
|
||||
* would dynamically initialize the GDT entries at runtime, but fuck that.
|
||||
* I've lost way more than enough time on the utterly useless GDT, so we'll just
|
||||
* leave this here as it is until we actually need it. See arch/mm/segment.S
|
||||
* for the actual GDT entries and how the GDT is loaded.
|
||||
*/
|
||||
|
||||
#include <arch/page.h>
|
||||
|
||||
#include <gay/cdefs.h>
|
||||
#include <gay/types.h>
|
||||
|
||||
/** @brief x86 Global Descriptor Table entry as used by the kernel. */
|
||||
struct x86_gdt_entry {
|
||||
u32 base;
|
||||
unsigned limit:20;
|
||||
unsigned flags:4;
|
||||
#define X86_GDT_SIZE (1u << 2)
|
||||
#define X86_GDT_GRANULARITY (1u << 3)
|
||||
u8 access;
|
||||
#define X86_GDT_ACCESSED (1u << 0)
|
||||
#define X86_GDT_RW (1u << 1)
|
||||
#define X86_GDT_DIRECTION (1u << 2) /* for data selectors (executable not set) */
|
||||
#define X86_GDT_CONFORMING (1u << 2) /* for code selectors (executable set) */
|
||||
#define X86_GDT_EXEC (1u << 3)
|
||||
#define X86_GDT_TYPE (1u << 4) /* 1 for code/data, 0 for system (e.g. TSS) */
|
||||
#define X86_GDT_PRIVL_SHIFT (5)
|
||||
#define X86_GDT_PRIVL_MASK (3u << X86_GDT_PRIVL_SHIFT)
|
||||
#define X86_GDT_PRIVL(n) (((n) << X86_GDT_PRIVL_SHIFT) & X86_GDT_PRIVL_MASK)
|
||||
#define X86_GDT_PRESENT (1u << 7)
|
||||
};
|
||||
|
||||
/** @brief x86 Global Descriptor Table entry as laid out in hardware. */
|
||||
struct x86_insane_gdt_entry {
|
||||
u16 limit0;
|
||||
u16 base0;
|
||||
u8 base1;
|
||||
u8 access;
|
||||
unsigned limit1:4;
|
||||
unsigned flags:4;
|
||||
u8 base2;
|
||||
} __packed;
|
||||
|
||||
/** @brief The main GDT (defined in `arch/mm/segment.S`) */
|
||||
extern struct x86_insane_gdt_entry x86_gdt[5];
|
||||
|
||||
struct x86_tss {
|
||||
u16 link; u16 _reserved0;
|
||||
u32 esp0;
|
||||
u16 ss0; u16 _reserved1;
|
||||
u32 esp1;
|
||||
u16 ss1; u16 _reserved2;
|
||||
u32 esp2;
|
||||
u16 ss2; u16 _reserved3;
|
||||
u32 cr3;
|
||||
u32 eip;
|
||||
u32 eflags;
|
||||
u32 eax;
|
||||
u32 ecx;
|
||||
u32 edx;
|
||||
u32 ebx;
|
||||
u32 esp;
|
||||
u32 ebp;
|
||||
u32 esi;
|
||||
u32 edi;
|
||||
u16 es; u16 _reserved4;
|
||||
u16 cs; u16 _reserved5;
|
||||
u16 ss; u16 _reserved6;
|
||||
u16 ds; u16 _reserved7;
|
||||
u16 fs; u16 _reserved8;
|
||||
u16 gs; u16 _reserved9;
|
||||
u16 ldtr; u16 _reserved10;
|
||||
u16 _reserved11; u16 iopb_offset;
|
||||
};
|
||||
|
||||
#endif /* not _ASM_SOURCE */
|
||||
|
||||
/*
|
||||
* This file is part of GayBSD.
|
||||
* Copyright (c) 2021 fef <owo@fef.moe>.
|
||||
*
|
||||
* GayBSD is nonviolent software: you may only use, redistribute, and/or
|
||||
* modify it under the terms of the Cooperative Nonviolent Public License
|
||||
* (CNPL) as found in the LICENSE file in the source code root directory
|
||||
* or at <https://git.pixie.town/thufie/npl-builder>; either version 7
|
||||
* of the license, or (at your option) any later version.
|
||||
*
|
||||
* GayBSD comes with ABSOLUTELY NO WARRANTY, to the extent
|
||||
* permitted by applicable law. See the CNPL for details.
|
||||
*/
|
@ -0,0 +1,65 @@
|
||||
/* See the end of this file for copyright and license terms. */
|
||||
|
||||
#include <arch/segment.h>
|
||||
|
||||
#include <asm/common.h>
|
||||
|
||||
ASM_ENTRY(x86_replace_gdt)
|
||||
push %ebp
|
||||
mov %esp, %ebp
|
||||
|
||||
lgdt x86_gdt_desc
|
||||
|
||||
ljmp $(X86_KERN_CS), $1f
|
||||
1: movl $(X86_KERN_DS), %eax
|
||||
movw %ax, %ds
|
||||
movw %ax, %es
|
||||
movw %ax, %fs
|
||||
movw %ax, %gs
|
||||
movw %ax, %ss
|
||||
|
||||
pop %ebp
|
||||
ret
|
||||
ASM_END(x86_replace_gdt)
|
||||
|
||||
.data
|
||||
|
||||
.align 4
|
||||
.word 0 /* padding */
|
||||
x86_gdt_desc:
|
||||
.word x86_gdt - x86_gdt_end - 1 /* limit */
|
||||
.long x86_gdt /* base */
|
||||
|
||||
/*
|
||||
* TODO: The GDT entry structure is so fundamentally fucked up that i gave
|
||||
* up writing an encoder for it half way through, so we just use these
|
||||
* hardcoded values for now. They were generated using the code on
|
||||
* <https://wiki.osdev.org/GDT_Tutorial#Some_stuff_to_make_your_life_easy>
|
||||
* and assign the entire 4 GiB region for both code and data as well as
|
||||
* kernel and user mode. Even the Intel manual says you're not supposed
|
||||
* to use segmentation anymore and just rely on paging for memory
|
||||
* protection instead, so we gladly accept their advice.
|
||||
*/
|
||||
.align 8
|
||||
x86_gdt:
|
||||
.quad 0x0000000000000000 /* 0x00 null descriptor */
|
||||
.quad 0x00cf9a000000ffff /* 0x08 kernel code, full 4 GiB */
|
||||
.quad 0x00cf92000000ffff /* 0x10 kernel data, full 4 GiB */
|
||||
.quad 0x00cffa000000ffff /* 0x18 user code, full 4 GiB */
|
||||
.quad 0x00cff2000000ffff /* 0x20 user data, full 4 GiB */
|
||||
x86_gdt_end:
|
||||
.size x86_gdt, . - x86_gdt
|
||||
|
||||
/*
|
||||
* This file is part of GayBSD.
|
||||
* Copyright (c) 2021 fef <owo@fef.moe>.
|
||||
*
|
||||
* GayBSD is nonviolent software: you may only use, redistribute, and/or
|
||||
* modify it under the terms of the Cooperative Nonviolent Public License
|
||||
* (CNPL) as found in the LICENSE file in the source code root directory
|
||||
* or at <https://git.pixie.town/thufie/npl-builder>; either version 7
|
||||
* of the license, or (at your option) any later version.
|
||||
*
|
||||
* GayBSD comes with ABSOLUTELY NO WARRANTY, to the extent
|
||||
* permitted by applicable law. See the CNPL for details.
|
||||
*/
|
Loading…
Reference in New Issue