Compare commits

...

3 Commits

27 changed files with 68 additions and 151 deletions
+6 -29
View File
@@ -21,7 +21,8 @@ KERNEL_CPP = kernel/entry.cpp kernel/main.cpp kernel/serial.cpp kernel/fs.cpp \
kernel/interrupt/gdt.cpp kernel/interrupt/idt.cpp \
kernel/interrupt/pic.cpp kernel/interrupt/pit.cpp \
graphics/context.cpp graphics/draw.cpp \
fonts/pixel_font.cpp
fonts/pixel_font.cpp \
desktop/prepare.cpp
KERNEL_ASM = kernel/scheduler/context_switch.S kernel/interrupt/isr.S kernel/interrupt/idt_helpers.S
KERNEL_OBJ = $(KERNEL_CPP:%.cpp=build/%.o) $(KERNEL_ASM:%.S=build/%.o)
@@ -47,7 +48,8 @@ all: _bd $(EFI_OBJ) $(BOOT_OBJ) $(KERNEL_OBJ)
@echo "Done."
_bd:
@mkdir -p build/graphics build/kernel build/fonts build/kernel/memory \
@mkdir -p build/graphics build/kernel build/fonts build/desktop \
build/kernel/memory \
build/kernel/scheduler build/kernel/interrupt \
build/efi/lib build/efi/lib/x86_64 build/efi/lib/runtime build/efi/gnuefi
@@ -83,40 +85,15 @@ build/%.o: %.c
@echo "Compile C $<"
@gcc $(CFLAGS) -c $< -o $@
build/kernel/%.o: kernel/%.cpp | _bd
build/%.o: %.cpp
@echo "Compile CPP $<"
@g++ $(KERNEL_CXXFLAGS) -c $< -o $@
build/kernel/memory/%.o: kernel/memory/%.cpp | _bd
@echo "Compile CPP $<"
@g++ $(KERNEL_CXXFLAGS) -c $< -o $@
build/kernel/scheduler/%.o: kernel/scheduler/%.cpp | _bd
@echo "Compile CPP $<"
@g++ $(KERNEL_CXXFLAGS) -c $< -o $@
build/kernel/scheduler/%.o: kernel/scheduler/%.S | _bd
build/%.o: %.S
@echo "Compile AS $<"
@gcc -Iinclude -Iefi/inc -ffreestanding -fno-stack-protector -fno-stack-check \
-fshort-wchar -mno-red-zone -fcf-protection=none -c $< -o $@
build/kernel/interrupt/%.o: kernel/interrupt/%.cpp | _bd
@echo "Compile CPP $<"
@g++ $(KERNEL_CXXFLAGS) -c $< -o $@
build/kernel/interrupt/%.o: kernel/interrupt/%.S | _bd
@echo "Compile AS $<"
@gcc -Iinclude -Iefi/inc -ffreestanding -fno-stack-protector -fno-stack-check \
-fshort-wchar -mno-red-zone -fcf-protection=none -c $< -o $@
build/graphics/%.o: graphics/%.cpp | _bd
@echo "Compile CPP $<"
@g++ $(KERNEL_CXXFLAGS) -c $< -o $@
build/fonts/%.o: fonts/%.cpp | _bd
@echo "Compile CPP $<"
@g++ $(KERNEL_CXXFLAGS) -c $< -o $@
vdir: all
@mkdir -p vdir/EFI/BOOT
@cp build/BOOTX64.EFI vdir/EFI/BOOT
+9
View File
@@ -0,0 +1,9 @@
#include <graphics/draw.h>
void prepare(UINT32 width, UINT32 height, COLOR_RGB color) {
for (UINT32 i = 0; i < height; i++) {
for (UINT32 j = 0; j < width; j++) {
draw_pixel(i, j, color);
}
}
}
+3 -30
View File
@@ -3,16 +3,7 @@
#include <graphics/draw.h>
#include <common.h>
static unsigned int pf_x = 0;
static unsigned int pf_y = 1;
void pf_reset_position(void) {
// 其实应该还要清一下屏,后面思考一个安全的做法
pf_x = 0;
pf_y = 1;
}
void pf_print_char(char c, unsigned int basex, unsigned int basey, EFI_GRAPHICS_OUTPUT_BLT_PIXEL color) {
void pf_print_char(char c, uint16_t basex, uint16_t basey, COLOR_RGB color) {
for (unsigned int y = 0; y < 16; y++) {
unsigned char data = hankaku_pixels[c][y];
for (int x = 7; x >= 0; x--) {
@@ -31,27 +22,9 @@ void pf_print_char(char c, unsigned int basex, unsigned int basey, EFI_GRAPHICS_
}
}
void pf_print(const char* text, EFI_GRAPHICS_OUTPUT_BLT_PIXEL color) {
void pf_print(const char* text, uint16_t basex, uint16_t basey, COLOR_RGB color) {
for (unsigned int i = 0; i < strlen(text); i++) {
char c = text[i];
if (c == '\n') {
pf_x = 0;
pf_y++;
if ((pf_y - 1) * 16 >= g_gfx.vr) {
gfx_clear();
pf_y = 1;
}
continue;
}
if (pf_x * 9 + 9 > g_gfx.hr) {
pf_x = 0;
pf_y++;
if ((pf_y - 1) * 16 >= g_gfx.vr) {
gfx_clear();
pf_y = 1;
}
}
pf_print_char(c, pf_x * 9, (pf_y - 1) * 16, color);
pf_x++;
pf_print_char(c, basex + i * 8, basey, color);
}
}
+3 -2
View File
@@ -1,4 +1,5 @@
#include <graphics/context.h>
#include <graphics/draw.h>
gfx_context g_gfx;
@@ -6,10 +7,10 @@ void gfx_init(EFI_GRAPHICS_OUTPUT_PROTOCOL *GOP) {
g_gfx.GOP = GOP;
g_gfx.hr = GOP->Mode->Info->HorizontalResolution;
g_gfx.vr = GOP->Mode->Info->VerticalResolution;
g_gfx.base = (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *) GOP->Mode->FrameBufferBase;
g_gfx.base = (COLOR_RGB *) GOP->Mode->FrameBufferBase;
}
void gfx_clear(void) {
EFI_GRAPHICS_OUTPUT_BLT_PIXEL black = {0, 0, 0, 0};
COLOR_RGB black = {0, 0, 0, 0};
g_gfx.GOP->Blt(g_gfx.GOP, &black, EfiBltVideoFill, 0, 0, 0, 0, g_gfx.hr, g_gfx.vr, 0);
}
+2 -2
View File
@@ -1,7 +1,7 @@
#include <graphics/draw.h>
void draw_pixel(unsigned int x, unsigned int y, EFI_GRAPHICS_OUTPUT_BLT_PIXEL color) {
EFI_GRAPHICS_OUTPUT_BLT_PIXEL *p = g_gfx.base + (g_gfx.hr * y) + x;
void draw_pixel(unsigned int x, unsigned int y, COLOR_RGB color) {
COLOR_RGB *p = g_gfx.base + (g_gfx.hr * y) + x;
p->Blue = color.Blue;
p->Green = color.Green;
+1
View File
@@ -1,6 +1,7 @@
#pragma once
#define ASM asm volatile
#define COLOR_RGB EFI_GRAPHICS_OUTPUT_BLT_PIXEL
static unsigned int strlen(const char* arr) { // 获取string长度
int i = 0;
+3
View File
@@ -0,0 +1,3 @@
#pragma once
void prepare(UINT32 width, UINT32 height, COLOR_RGB color = {34, 139, 34});
+5 -4
View File
@@ -1,8 +1,9 @@
#pragma once
#include <graphics/context.h>
#include <graphics/draw.h>
void pf_print_char(char c, unsigned int basex, unsigned int basey,
EFI_GRAPHICS_OUTPUT_BLT_PIXEL color = {255, 255, 255, 255}); // Pixel Font 打印字符
void pf_print(const char* text, EFI_GRAPHICS_OUTPUT_BLT_PIXEL color = {255, 255, 255, 255}); // Pixel Font 打印string
void pf_reset_position(void); // 重设Pixel Font Cursor位置
void pf_print_char(char c, uint16_t basex, uint16_t basey,
COLOR_RGB color = {255, 255, 255, 255}); // Pixel Font 打印字符
void pf_print(const char* text, uint16_t basex, uint16_t basey, COLOR_RGB color = {255, 255, 255, 255}); // Pixel Font 打印string
// void pf_reset_position(void); // 重设Pixel Font Cursor位置
+2 -1
View File
@@ -3,12 +3,13 @@
// 这个文件存在的目的是让graphics的draw功能不用每次传 GOP hr vr base
#include <efi.h>
#include <common.h>
struct gfx_context {
EFI_GRAPHICS_OUTPUT_PROTOCOL *GOP;
unsigned int hr;
unsigned int vr;
EFI_GRAPHICS_OUTPUT_BLT_PIXEL *base;
COLOR_RGB *base;
};
extern gfx_context g_gfx;
+3 -1
View File
@@ -3,4 +3,6 @@
#include <efi.h>
#include <graphics/context.h>
void draw_pixel(unsigned int x, unsigned int y, EFI_GRAPHICS_OUTPUT_BLT_PIXEL color); // 画像素
#define COLOR_RGB EFI_GRAPHICS_OUTPUT_BLT_PIXEL
void draw_pixel(unsigned int x, unsigned int y, COLOR_RGB color); // 画像素
+2 -2
View File
@@ -1,5 +1,5 @@
#include <fs.h>
#include <serial.h>
#include <kernel/fs.h>
#include <kernel/serial.h>
#include <memory/heap.h>
#include <common.h>
+2 -2
View File
@@ -1,6 +1,6 @@
#include <gdt.h>
#include <kernel/gdt.h>
#include <common.h>
#include <serial.h>
#include <kernel/serial.h>
static gdt_entry g_gdt[7]; // 5 segments + TSS (2 entries)
static gdt_ptr g_gdt_ptr;
+3 -3
View File
@@ -1,7 +1,7 @@
#include <idt.h>
#include <kernel/idt.h>
#include <common.h>
#include <pic.h>
#include <serial.h>
#include <kernel/pic.h>
#include <kernel/serial.h>
// Defined in isr.S — 256 ISR stubs
extern "C" void* isr_stub_table[256];
+2 -2
View File
@@ -1,6 +1,6 @@
#include <pic.h>
#include <kernel/pic.h>
#include <common.h>
#include <serial.h>
#include <kernel/serial.h>
static inline void outb(UINT16 port, UINT8 val) {
ASM("outb %0, %1" : : "a"(val), "Nd"(port));
+3 -3
View File
@@ -1,7 +1,7 @@
#include <pit.h>
#include <kernel/pit.h>
#include <common.h>
#include <pic.h>
#include <serial.h>
#include <kernel/pic.h>
#include <kernel/serial.h>
static inline void outb(UINT16 port, UINT8 val) {
ASM("outb %0, %1" : : "a"(val), "Nd"(port));
+12 -63
View File
@@ -2,16 +2,17 @@
#include <graphics/context.h>
#include <graphics/draw.h>
#include <fonts/pixel_font.h>
#include <serial.h>
#include <kernel/serial.h>
#include <common.h>
#include <memory/pmm.h>
#include <memory/heap.h>
#include <scheduler.h>
#include <fs.h>
#include <gdt.h>
#include <idt.h>
#include <pic.h>
#include <pit.h>
#include <kernel/scheduler.h>
#include <kernel/fs.h>
#include <kernel/gdt.h>
#include <kernel/idt.h>
#include <kernel/pic.h>
#include <kernel/pit.h>
#include <desktop/prepare.h>
extern EFI_SYSTEM_TABLE *ST;
@@ -93,30 +94,6 @@ extern "C" void kernel_main() {
serial_write("Sylva: init heap...\n");
init_heap();
// test kmalloc/kfree
serial_write("Sylva: kmalloc test...\n");
void* p1 = kmalloc(64);
void* p2 = kmalloc(128);
void* p3 = kmalloc(256);
serial_write("Sylva: p1 = ");
serial_write_hex((UINTN)p1);
serial_write(" p2 = ");
serial_write_hex((UINTN)p2);
serial_write(" p3 = ");
serial_write_hex((UINTN)p3);
serial_write("\n");
serial_write("Sylva: kfree test...\n");
kfree(p2);
kfree(p1);
kfree(p3);
void* p4 = kmalloc(32);
serial_write("Sylva: realloc p4 = ");
serial_write_hex((UINTN)p4);
serial_write("\n");
kfree(p4);
serial_write("Sylva: memory init done.\n");
serial_write("Sylva: FS init...\n");
@@ -128,9 +105,11 @@ extern "C" void kernel_main() {
fs_list();
}
pf_print("Welcome to Sylva OS!\n");
serial_write(" Kernel prepared well.\n");
prepare((UINT32) g_gfx.vr, (UINT32) g_gfx.hr);
pf_print("Hello from SylvaOS!", 100, 100);
// --- Interrupt infrastructure ---
serial_write("Sylva: init GDT...\n");
gdt_init();
@@ -152,37 +131,7 @@ extern "C" void kernel_main() {
ASM("sti");
serial_write("Sylva: interrupts enabled\n");
// --- Multitasking demo ---
serial_write("Sylva: creating tasks...\n");
// Task A: prints messages — preemption handles time slicing
task_create("taskA", []() {
for (int i = 0; i < 10; i++) {
serial_write("[taskA] running iteration ");
serial_write_hex(i);
serial_write("\n");
for (volatile int j = 0; j < 50000000; j++) {}
}
serial_write("[taskA] done\n");
});
// Task B: prints messages
task_create("taskB", []() {
for (int i = 0; i < 5; i++) {
serial_write("[taskB] hello from taskB #");
serial_write_hex(i);
serial_write("\n");
for (volatile int j = 0; j < 50000000; j++) {}
}
serial_write("[taskB] done\n");
});
// Task C: short task
task_create("taskC", []() {
serial_write("[taskC] quick task\n");
for (volatile int j = 0; j < 50000000; j++) {}
serial_write("[taskC] finished\n");
});
// 现在还没有任何tasks
serial_write("Sylva: starting preemptive scheduler\n");
scheduler_run(); // never returns
+1 -1
View File
@@ -1,6 +1,6 @@
#include <memory/heap.h>
#include <memory/pmm.h>
#include <serial.h>
#include <kernel/serial.h>
struct heap_block {
UINTN size; // includes header; bit 0 = 1 used, 0 free
+1 -1
View File
@@ -1,5 +1,5 @@
#include <memory/pmm.h>
#include <serial.h>
#include <kernel/serial.h>
extern EFI_SYSTEM_TABLE *ST;
+4 -4
View File
@@ -1,10 +1,10 @@
#include <scheduler.h>
#include <idt.h>
#include <pic.h>
#include <kernel/scheduler.h>
#include <kernel/idt.h>
#include <kernel/pic.h>
#include <memory/heap.h>
#include <memory/pmm.h>
#include <common.h>
#include <serial.h>
#include <kernel/serial.h>
// Assembly: context_switch(UINT64* old_rsp, UINT64 new_rsp)
extern "C" void context_switch(UINT64* old_rsp, UINT64 new_rsp);
+1 -1
View File
@@ -1,4 +1,4 @@
#include <serial.h>
#include <kernel/serial.h>
extern EFI_SYSTEM_TABLE *ST;