Skip to content

Commit 337783a

Browse files
committed
FIXED
1 parent 49f2d57 commit 337783a

8 files changed

Lines changed: 147 additions & 44 deletions

File tree

build.ps1

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
param(
2+
[ValidateSet("all", "clean")]
3+
[string]$Target = "all",
4+
[string]$ModelBlob
5+
)
6+
7+
$ErrorActionPreference = "Stop"
8+
9+
function Require-Command([string]$Name) {
10+
if (-not (Get-Command $Name -ErrorAction SilentlyContinue)) {
11+
throw "$Name not found in PATH"
12+
}
13+
}
14+
15+
function Invoke-Checked([string]$Exe, [string[]]$Args) {
16+
& $Exe @Args
17+
if ($LASTEXITCODE -ne 0) {
18+
throw "$Exe failed with exit code $LASTEXITCODE"
19+
}
20+
}
21+
22+
function Get-ObjPath([string]$SrcPath, [string]$SrcRoot, [string]$BuildRoot) {
23+
$rel = $SrcPath.Substring($SrcRoot.Length)
24+
$rel = $rel.TrimStart([System.IO.Path]::DirectorySeparatorChar, [System.IO.Path]::AltDirectorySeparatorChar)
25+
$dir = Split-Path $rel -Parent
26+
$base = [System.IO.Path]::GetFileNameWithoutExtension($rel)
27+
if ([string]::IsNullOrEmpty($dir)) {
28+
return (Join-Path $BuildRoot ($base + ".o"))
29+
}
30+
return (Join-Path $BuildRoot (Join-Path $dir ($base + ".o")))
31+
}
32+
33+
$root = $PSScriptRoot
34+
if ([string]::IsNullOrEmpty($root)) {
35+
$root = (Get-Location).Path
36+
}
37+
38+
$srcDir = Join-Path $root "src"
39+
$buildDir = Join-Path $root "build"
40+
$kernelBin = Join-Path $root "kernel.bin"
41+
$ldScript = Join-Path $root "linker.ld"
42+
$assetsDir = Join-Path $root "assets"
43+
44+
if ($Target -eq "clean") {
45+
if (Test-Path $buildDir) {
46+
Remove-Item -Recurse -Force $buildDir
47+
}
48+
if (Test-Path $kernelBin) {
49+
Remove-Item -Force $kernelBin
50+
}
51+
exit 0
52+
}
53+
54+
Require-Command "i686-elf-gcc"
55+
Require-Command "nasm"
56+
Require-Command "i686-elf-objcopy"
57+
58+
if (-not (Test-Path $ldScript)) {
59+
throw "linker.ld not found"
60+
}
61+
62+
if (-not (Test-Path $buildDir)) {
63+
New-Item -ItemType Directory -Force -Path $buildDir | Out-Null
64+
}
65+
66+
$asmSrcs = Get-ChildItem -Path $srcDir -Recurse -Filter "*.asm"
67+
$cSrcs = Get-ChildItem -Path $srcDir -Recurse -Filter "*.c"
68+
69+
$objPaths = @()
70+
71+
$asFlags = @("-f", "elf32")
72+
$cFlags = @("-ffreestanding", "-O2", "-Wall", "-Wextra", "-m32", "-fno-pie", "-fno-stack-protector", "-nostdlib", "-nostdinc", "-Iinclude")
73+
74+
foreach ($src in $asmSrcs) {
75+
$obj = Get-ObjPath $src.FullName $srcDir $buildDir
76+
$objDir = Split-Path $obj -Parent
77+
if (-not (Test-Path $objDir)) {
78+
New-Item -ItemType Directory -Force -Path $objDir | Out-Null
79+
}
80+
Invoke-Checked "nasm" ($asFlags + @($src.FullName, "-o", $obj))
81+
$objPaths += $obj
82+
}
83+
84+
foreach ($src in $cSrcs) {
85+
$obj = Get-ObjPath $src.FullName $srcDir $buildDir
86+
$objDir = Split-Path $obj -Parent
87+
if (-not (Test-Path $objDir)) {
88+
New-Item -ItemType Directory -Force -Path $objDir | Out-Null
89+
}
90+
Invoke-Checked "i686-elf-gcc" ($cFlags + @("-c", $src.FullName, "-o", $obj))
91+
$objPaths += $obj
92+
}
93+
94+
$modelBlobPath = $ModelBlob
95+
if ([string]::IsNullOrEmpty($modelBlobPath)) {
96+
$candidateA = Join-Path $assetsDir "smollm-135m.gguf"
97+
$candidateB = Join-Path $assetsDir "SmolLM2-135M-Instruct-Q4_K_M.gguf"
98+
if (Test-Path $candidateA) {
99+
$modelBlobPath = $candidateA
100+
} elseif (Test-Path $candidateB) {
101+
$modelBlobPath = $candidateB
102+
}
103+
}
104+
105+
if (-not [string]::IsNullOrEmpty($modelBlobPath)) {
106+
if (-not (Test-Path $modelBlobPath)) {
107+
throw "Model blob not found: $modelBlobPath"
108+
}
109+
$modelObj = Join-Path $buildDir "model.o"
110+
Invoke-Checked "i686-elf-objcopy" @(
111+
"-I", "binary",
112+
"-O", "elf32-i386",
113+
"-B", "i386",
114+
"--rename-section", ".data=.ai_model,alloc,load,readonly,data,contents",
115+
$modelBlobPath,
116+
$modelObj
117+
)
118+
$objPaths += $modelObj
119+
}
120+
121+
$ldFlags = @("-T", $ldScript, "-ffreestanding", "-O2", "-nostdlib", "-Wl,--build-id=none")
122+
Invoke-Checked "i686-elf-gcc" ($ldFlags + @("-o", $kernelBin) + $objPaths)

include/arch/x86/idt.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,10 @@ typedef struct {
1717
uint32_t base;
1818
} __attribute__((packed)) idt_ptr_t;
1919

20-
typedef registers_t* (*isr_t)(registers_t* regs);
21-
2220
void idt_set_gate(uint8_t num, uint32_t base, uint16_t selector, uint8_t type, uint8_t dpl, uint8_t present);
2321
void idt_init(void);
2422
void idt_load_current(void);
2523
void idt_load(uint32_t idt_ptr_addr);
26-
void register_interrupt_handler(uint8_t n, isr_t handler);
27-
void isr_init(void);
28-
uint64_t timer_get_ticks(void);
2924
void pic_remap(void);
3025

3126
#endif

include/arch/x86/interrupts.h

Lines changed: 0 additions & 16 deletions
This file was deleted.

include/mmu.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@ void mmu_destroy_space(uintptr_t* dir);
4545
// Fault handling
4646
uintptr_t mmu_get_fault_addr(void);
4747

48+
// x86 Specific (move to arch/x86/mmu.h if exists)
49+
void mmu_enable_pse(void);
50+
void mmu_enable_paging(void);
51+
4852
#endif

src/arch/x86/idt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ void pic_remap(void) {
7474
outb(0xA1, 0x01);
7575
io_wait();
7676

77-
outb(0x21, 0x0);
78-
outb(0xA1, 0x0);
77+
outb(0x21, 0xFB); // Mask all except slave PIC (IRQ2)
78+
outb(0xA1, 0xFF); // Mask all slave PIC IRQs
7979
}
8080

8181
void idt_set_gate(uint8_t num, uint32_t base, uint16_t selector, uint8_t type, uint8_t dpl, uint8_t present) {

src/arch/x86/mmu.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ static uint32_t* alloc_table_any(void) {
4747
return table;
4848
}
4949

50+
void mmu_enable_pse(void) {
51+
uint32_t cr4;
52+
asm volatile("mov %%cr4, %0" : "=r"(cr4));
53+
cr4 |= 0x10;
54+
asm volatile("mov %0, %%cr4" : : "r"(cr4));
55+
}
56+
57+
void mmu_enable_paging(void) {
58+
uint32_t cr0;
59+
asm volatile("mov %%cr0, %0" : "=r"(cr0));
60+
cr0 |= 0x80000000;
61+
asm volatile("mov %0, %%cr0" : : "r"(cr0));
62+
}
63+
5064
void mmu_init(void) {
5165
for (uint32_t i = 0; i < 1024; ++i) {
5266
page_directory[i] = PAGE_RW;
@@ -87,17 +101,8 @@ void mmu_init(void) {
87101

88102
paging_switch_directory((uintptr_t)page_directory);
89103

90-
// Enable PSE
91-
uint32_t cr4;
92-
asm volatile("mov %%cr4, %0" : "=r"(cr4));
93-
cr4 |= 0x10;
94-
asm volatile("mov %0, %%cr4" : : "r"(cr4));
95-
96-
// Enable Paging
97-
uint32_t cr0;
98-
asm volatile("mov %%cr0, %0" : "=r"(cr0));
99-
cr0 |= 0x80000000;
100-
asm volatile("mov %0, %%cr0" : : "r"(cr0));
104+
mmu_enable_pse();
105+
mmu_enable_paging();
101106
}
102107

103108
void mmu_map_page_dir(uintptr_t* dir, uintptr_t virt, uintptr_t phys, uint32_t flags) {

src/arch/x86/smp_rally.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ void smp_ap_entry(void) {
3737
uint32_t cpu_id = (*(volatile uint32_t*)(LAPIC_BASE + LAPIC_ID)) >> 24;
3838

3939
// Load page directory
40-
load_cr3(paging_get_directory());
40+
mmu_switch_space((uintptr_t)mmu_get_current_space());
4141

4242
// Enable PSE first for 4MB pages
43-
enable_pse();
43+
mmu_enable_pse();
4444

4545
// Enable paging
46-
paging_enable();
46+
mmu_enable_paging();
4747

4848
// Load IDT
4949
idt_load_current();

src/cpu/isr_handler.c

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)