PREEMPTIVE MULTITASKING!!!!

This commit is contained in:
2025-05-06 19:29:35 +02:00
parent a48fa362d0
commit c646404cf0
14 changed files with 215 additions and 103 deletions
+54 -42
View File
@@ -3,6 +3,10 @@
#include <kernel.h>
// Privilege levels
#define DPL_KERNEL 0
#define DPL_USER 3
/*
* Segment selector explanation (why kcode is 0x08?)
*
@@ -26,8 +30,11 @@
* s 0 for the GDT and the index is one, because it
* is the second entry in our GDT (after null desc.)
*/
#define SEGMENT_SEL(_priv, _tab, _i)\
(uint16_t)(((_i) << 3) | ((_tab) << 2) | (_priv))
#define SEGMENT_SEL(_idx, _tab, _dpl) (\
(((_dpl) ) & 3) | \
(((_tab) << 2) & 4) | \
(((_idx) << 3) ) \
)
/*
* Entries:
@@ -40,21 +47,43 @@
*/
#define GDT_ENTRIES 6
#define GDT_NULL SEGMENT_SEL(0, 0, 0)
#define GDT_KERNEL_CODE SEGMENT_SEL(0, 0, 1)
#define GDT_KERNEL_DATA SEGMENT_SEL(0, 0, 2)
#define GDT_USER_CODE SEGMENT_SEL(3, 0, 3)
#define GDT_USER_DATA SEGMENT_SEL(3, 0, 4)
#define GDT_TSS SEGMENT_SEL(0, 0, 5)
#define GDT_KERNEL_CODE SEGMENT_SEL(1, 0, DPL_KERNEL)
#define GDT_KERNEL_DATA SEGMENT_SEL(2, 0, DPL_KERNEL)
#define GDT_USER_CODE SEGMENT_SEL(3, 0, DPL_USER)
#define GDT_USER_DATA SEGMENT_SEL(4, 0, DPL_USER)
#define GDT_TSS SEGMENT_SEL(5, 0, DPL_KERNEL)
#define GDT_DESC(_lim, _base, _acc, _flag) (struct gdt_entry){\
.limit = (_lim) & 0xffff,\
.base_low = (uint16_t)(_base),\
.base_mid = (uint8_t)((_base) >> 16),\
.access = (_acc),\
.flags = ((_flag & 0x0f) << 4) | (((_lim) >> 16) & 0x0f),\
.base_high = (uint8_t)((_base) >> 24),\
#define GDT_DESC(_lim, _base, _access, _flags) (struct gdt_entry) { \
.base_low = (_base) & 0xFFFF, \
.base_mid = ((_base) >> 16) & 0xFF, \
.base_high = ((_base) >> 24) & 0xFF, \
.limit = (_lim) & 0xFFFF, \
.flags = ((_flags) & 0xF0) | (((_lim) >> 16) & 0x0F), \
.access = (_access) \
}
/*
* GDT descriptor/entry explanation
*
* 0-15 low 16 bits of limit
* 15-31 low 16 bits of base
* 32-39 mid 8 bits of base
* 40-47 access byte
* 40 Accessed
* 41 Read/Write
* 42 Direction/Conforming
* 43 Executable
* 44 Descriptor type
* 45-46 DPL
* 47 Present
* 48-51 high 4 bits of limit
* 52-55 flags
* 52 Reserved
* 53 Long-mode
* 54 Size (0 - 16-bit, 1 - 32-bit)
* 55 Granularity (0 - limit in bytes, 1 - limit in pages)
* 56-63 high 8 bits of base
*/
struct gdt_entry {
uint16_t limit;
uint16_t base_low;
@@ -70,40 +99,23 @@ struct gdt_pointer {
} __packed;
struct tss_pointer {
uint16_t link;
uint16_t __reserved0;
uint16_t prev_task, __reserved0;
uint32_t esp0;
uint16_t ss0;
uint16_t __reserved1;
uint16_t ss0, __reserved1;
uint32_t esp1;
uint16_t ss1;
uint16_t __reserved2;
uint16_t ss1, __reserved2;
uint32_t esp2;
uint16_t ss2;
uint16_t __reserved3;
uint16_t ss2, __reserved3;
uint32_t cr3;
uint32_t eip;
uint32_t eflags;
uint32_t eax;
uint32_t ecx;
uint32_t edx;
uint32_t ebx;
uint32_t esp;
uint32_t ebp;
uint32_t esi;
uint32_t edi;
uint16_t es;
uint16_t __reserved4;
uint16_t cs;
uint16_t __reserved5;
uint16_t ss;
uint16_t __reserved6;
uint16_t ds;
uint16_t __reserved7;
uint16_t fs;
uint16_t __reserved8;
uint16_t gs;
uint16_t __reserved9;
uint32_t eax, ecx, edx, ebx, esp, ebp, esi, edi;
uint16_t es, __reserved4;
uint16_t cs, __reserved5;
uint16_t ss, __reserved6;
uint16_t ds, __reserved7;
uint16_t fs, __reserved8;
uint16_t gs, __reserved9;
uint16_t ldtr;
uint32_t __reserved10;
uint16_t iopb;