Pridana verze v0.1

This commit is contained in:
2026-07-12 05:44:42 +02:00
commit 1266a65b82
40 changed files with 6126 additions and 0 deletions
+161
View File
@@ -0,0 +1,161 @@
/*
* AsterOS Kernel
* Autor: Pavel Kalaš
* Rok: 2026
*
*/
/*
* Tento architekturalni modul inicializuje GDT, IDT a remapuje PIC.
* Obsahuje centralni vstup pro obsluhu vyjimek, hardwarovych preruseni
* i syscall vektoru, vcetne predani rizeni scheduleru pri timer IRQ.
*/
#include "cpu.h"
#include "panic.h"
#include "printk.h"
#include "scheduler.h"
#include "syscall.h"
#include "timer.h"
#include "types.h"
extern void arch_load_gdt(void *ptr);
extern void arch_reload_segments(void);
extern void arch_load_idt(void *ptr);
extern void *isr_stub_table[];
#define IDT_ENTRIES 256
static inline void outb(u16 port, u8 value) {
__asm__ volatile ("outb %0, %1" : : "a"(value), "Nd"(port));
}
static inline void io_wait(void) {
__asm__ volatile ("outb %%al, $0x80" : : "a"(0));
}
struct __attribute__((packed)) gdt_ptr {
u16 limit;
u64 base;
};
struct __attribute__((packed)) idt_entry {
u16 offset_low;
u16 selector;
u8 ist;
u8 type_attr;
u16 offset_mid;
u32 offset_high;
u32 zero;
};
struct __attribute__((packed)) idt_ptr {
u16 limit;
u64 base;
};
static u64 gdt_table[3];
static struct idt_entry idt[IDT_ENTRIES];
static void set_idt_gate(u8 vec, void *isr, u8 flags) {
u64 addr = (u64)isr;
idt[vec].offset_low = (u16)(addr & 0xFFFF);
idt[vec].selector = 0x08;
idt[vec].ist = 0;
idt[vec].type_attr = flags;
idt[vec].offset_mid = (u16)((addr >> 16) & 0xFFFF);
idt[vec].offset_high = (u32)((addr >> 32) & 0xFFFFFFFF);
idt[vec].zero = 0;
}
static void pic_remap(void) {
outb(0x20, 0x11);
io_wait();
outb(0xA0, 0x11);
io_wait();
outb(0x21, 0x20);
io_wait();
outb(0xA1, 0x28);
io_wait();
outb(0x21, 0x04);
io_wait();
outb(0xA1, 0x02);
io_wait();
outb(0x21, 0x01);
io_wait();
outb(0xA1, 0x01);
io_wait();
outb(0x21, 0x00);
outb(0xA1, 0x00);
}
void cpu_init(void) {
struct gdt_ptr gp;
gdt_table[0] = 0x0000000000000000ULL;
gdt_table[1] = 0x00AF9A000000FFFFULL;
gdt_table[2] = 0x00CF92000000FFFFULL;
gp.limit = sizeof(gdt_table) - 1;
gp.base = (u64)&gdt_table[0];
arch_load_gdt(&gp);
arch_reload_segments();
}
void interrupts_init(void) {
struct idt_ptr ip;
u32 i;
for (i = 0; i < IDT_ENTRIES; ++i) {
set_idt_gate((u8)i, isr_stub_table[0], 0x8E);
}
for (i = 0; i < 34; ++i) {
set_idt_gate((u8)i, isr_stub_table[i], 0x8E);
}
set_idt_gate(128, isr_stub_table[34], 0xEE);
ip.limit = sizeof(idt) - 1;
ip.base = (u64)&idt[0];
arch_load_idt(&ip);
pic_remap();
__asm__ volatile ("sti");
}
void interrupt_dispatch(interrupt_frame_t *frame) {
if (frame->vector < 32) {
printk("[EXC] vector=%d error=%x\n", (int)frame->vector, (u32)frame->error);
panic("CPU exception");
}
if (frame->vector == 32) {
scheduler_tick();
outb(0x20, 0x20);
return;
}
if (frame->vector == 33) {
outb(0x20, 0x20);
return;
}
if (frame->vector == 128) {
frame->rax = syscall_dispatch(frame->rax, frame->rbx, frame->rcx, frame->rdx, frame->rsi);
return;
}
}
void cpu_halt(void) {
__asm__ volatile ("cli");
for (;;) {
__asm__ volatile ("hlt");
}
}
+29
View File
@@ -0,0 +1,29 @@
; /*
; * AsterOS Kernel
; * Autor: Pavel Kalaš
; * Rok: 2026
; *
; * Vytvořeno jen tak z nudy a z chuti zkoušet nové věci.
; */
[bits 64]
section .text.boot
global _start
extern kmain
_start:
cli
mov word [0xB800A], 0x0F4B
mov rsp, stack_top
call kmain
.halt:
hlt
jmp .halt
section .bss
align 16
stack_bottom:
resb 16384
stack_top:
+31
View File
@@ -0,0 +1,31 @@
; /*
; * AsterOS Kernel
; * Autor: Pavel Kalaš
; * Rok: 2026
; *
; * Vytvořeno jen tak z nudy a z chuti zkoušet nové věci.
; */
[bits 64]
section .text
global arch_load_gdt
global arch_reload_segments
arch_load_gdt:
lgdt [rdi]
ret
arch_reload_segments:
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
push qword 0x08
lea rax, [rel .flush]
push rax
retfq
.flush:
ret
+15
View File
@@ -0,0 +1,15 @@
; /*
; * AsterOS Kernel
; * Autor: Pavel Kalaš
; * Rok: 2026
; *
; * Vytvořeno jen tak z nudy a z chuti zkoušet nové věci.
; */
[bits 64]
section .text
global arch_load_idt
arch_load_idt:
lidt [rdi]
ret
+143
View File
@@ -0,0 +1,143 @@
; /*
; * AsterOS Kernel
; * Autor: Pavel Kalaš
; * Rok: 2026
; *
; * Vytvořeno jen tak z nudy a z chuti zkoušet nové věci.
; */
[bits 64]
section .text
extern interrupt_dispatch
global isr_stub_table
%macro ISR_NOERR 1
global isr_%1
isr_%1:
push qword 0
push qword %1
jmp isr_common
%endmacro
%macro ISR_ERR 1
global isr_%1
isr_%1:
push qword %1
jmp isr_common
%endmacro
ISR_NOERR 0
ISR_NOERR 1
ISR_NOERR 2
ISR_NOERR 3
ISR_NOERR 4
ISR_NOERR 5
ISR_NOERR 6
ISR_NOERR 7
ISR_ERR 8
ISR_NOERR 9
ISR_ERR 10
ISR_ERR 11
ISR_ERR 12
ISR_ERR 13
ISR_ERR 14
ISR_NOERR 15
ISR_NOERR 16
ISR_ERR 17
ISR_NOERR 18
ISR_NOERR 19
ISR_NOERR 20
ISR_ERR 21
ISR_NOERR 22
ISR_NOERR 23
ISR_NOERR 24
ISR_NOERR 25
ISR_NOERR 26
ISR_NOERR 27
ISR_NOERR 28
ISR_NOERR 29
ISR_ERR 30
ISR_NOERR 31
ISR_NOERR 32
ISR_NOERR 33
ISR_NOERR 128
isr_common:
cld
push rax
push rbx
push rcx
push rdx
push rbp
push rdi
push rsi
push r8
push r9
push r10
push r11
push r12
push r13
push r14
push r15
mov rdi, rsp
call interrupt_dispatch
pop r15
pop r14
pop r13
pop r12
pop r11
pop r10
pop r9
pop r8
pop rsi
pop rdi
pop rbp
pop rdx
pop rcx
pop rbx
pop rax
add rsp, 16
iretq
section .rodata
isr_stub_table:
dq isr_0
dq isr_1
dq isr_2
dq isr_3
dq isr_4
dq isr_5
dq isr_6
dq isr_7
dq isr_8
dq isr_9
dq isr_10
dq isr_11
dq isr_12
dq isr_13
dq isr_14
dq isr_15
dq isr_16
dq isr_17
dq isr_18
dq isr_19
dq isr_20
dq isr_21
dq isr_22
dq isr_23
dq isr_24
dq isr_25
dq isr_26
dq isr_27
dq isr_28
dq isr_29
dq isr_30
dq isr_31
dq isr_32
dq isr_33
dq isr_128