Skip to content

Commit 7777d04

Browse files
committed
FIXED
1 parent 97aaafa commit 7777d04

7 files changed

Lines changed: 67 additions & 25 deletions

File tree

‎include/arch/x86/mmu.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void mmu_switch_space(uintptr_t space_phys);
3131

3232
// Get current address space
3333
uintptr_t* mmu_get_current_space(void);
34+
uintptr_t* mmu_get_kernel_space(void);
3435

3536
// Get physical address from virtual
3637
uintptr_t mmu_get_phys(uintptr_t virt);

‎src/ai/ai_model.c‎

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "drivers/vga.h"
1414
#include "arch/x86/smp_rally.h"
1515
#include "kernel/sched.h"
16+
#include "cpu.h"
1617

1718
#ifndef NO_AI_MODEL
1819
extern uint8_t _ai_model_start;
@@ -606,14 +607,34 @@ static q16_16_t ai_dot_q16_simd(const q16_16_t* a, const q16_16_t* b, uint32_t n
606607
bpack[2] = b[(p + 2) * n + col];
607608
bpack[3] = b[(p + 3) * n + col];
608609
asm volatile(
609-
"movdqu (%1), %%xmm0\n"
610-
"movdqu (%2), %%xmm1\n"
611-
"pmulld %%xmm1, %%xmm0\n"
612-
"psrad $16, %%xmm0\n"
613-
"movdqu %%xmm0, (%0)\n"
610+
"movdqu (%1), %%xmm0\n" // a0, a1, a2, a3
611+
"movdqu (%2), %%xmm1\n" // b0, b1, b2, b3
612+
613+
// First pair (indices 0 and 2)
614+
"movdqa %%xmm0, %%xmm2\n"
615+
"pmuldq %%xmm1, %%xmm2\n" // xmm2 = [a2*b2 (64), a0*b0 (64)]
616+
"psrlq $16, %%xmm2\n" // shift each 64-bit result
617+
618+
// Second pair (indices 1 and 3)
619+
"pshufd $0xF5, %%xmm0, %%xmm0\n" // [a3, a3, a1, a1]
620+
"pshufd $0xF5, %%xmm1, %%xmm1\n" // [b3, b3, b1, b1]
621+
"pmuldq %%xmm1, %%xmm0\n" // xmm0 = [a3*b3 (64), a1*b1 (64)]
622+
"psrlq $16, %%xmm0\n"
623+
624+
// Extract low 32 bits of results into temp
625+
"movd %%xmm2, %%eax\n"
626+
"movl %%eax, (%0)\n" // temp[0] = a0*b0 >> 16
627+
"pshufd $0xEE, %%xmm2, %%xmm2\n"
628+
"movd %%xmm2, %%eax\n"
629+
"movl %%eax, 4(%0)\n" // temp[1] = a2*b2 >> 16
630+
"movd %%xmm0, %%eax\n"
631+
"movl %%eax, 8(%0)\n" // temp[2] = a1*b1 >> 16
632+
"pshufd $0xEE, %%xmm0, %%xmm0\n"
633+
"movd %%xmm0, %%eax\n"
634+
"movl %%eax, 12(%0)\n" // temp[3] = a3*b3 >> 16
614635
:
615636
: "r"(temp), "r"(a + p), "r"(bpack)
616-
: "xmm0", "xmm1", "memory"
637+
: "xmm0", "xmm1", "xmm2", "memory", "eax"
617638
);
618639
sum = q16_add(sum, (q16_16_t)temp[0]);
619640
sum = q16_add(sum, (q16_16_t)temp[1]);
@@ -819,6 +840,15 @@ void ai_model_init(void) {
819840
serial_write_string("DEBUG: GGUF BAD\n");
820841
diag_log(DIAG_ERROR, "gguf header invalid");
821842
}
843+
844+
if (cpu_has_feature(CPU_FEATURE_SSE41)) {
845+
cpu_enable_feature(CPU_FEATURE_SSE);
846+
simd_enabled = 1;
847+
diag_log(DIAG_INFO, "AI SIMD acceleration enabled (SSE4.1)");
848+
} else {
849+
diag_log(DIAG_INFO, "AI SIMD acceleration disabled (SSE4.1 not found)");
850+
}
851+
822852
serial_write_string("DEBUG: ai_model_init finished\n");
823853
}
824854

‎src/arch/x86/cpu.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "cpu/syscall.h"
77
#include "arch/x86/mmu.h"
88
#include "arch/x86/timer.h"
9+
#include "paging.h"
910

1011
void arch_init(void) {
1112
gdt_init();

‎src/arch/x86/hw_detect.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static uint32_t acpi_rsdt_phys = 0;
1313
static uint32_t acpi_rsdt_count = 0;
1414

1515
// x86 PCI Config Space access
16-
static uint32_t pci_config_read(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) {
16+
uint32_t pci_config_read(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) {
1717
uint32_t address = (uint32_t)(0x80000000u | ((uint32_t)bus << 16)
1818
| ((uint32_t)slot << 11) | ((uint32_t)func << 8) | (offset & 0xFC));
1919
outl(0xCF8, address);

‎src/arch/x86/mmu.c‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,28 +112,22 @@ void mmu_init(void) {
112112
// Identity map ACPI tables
113113
uint32_t rsdt = hw_acpi_get_rsdt_addr();
114114
if (rsdt) {
115-
// Map RSDT header and its entries
116-
mmu_map_page_dir((uintptr_t*)page_directory, rsdt, rsdt, PAGE_PRESENT | PAGE_RW);
115+
acpi_sdt_header_t* rsdt_hdr = (acpi_sdt_header_t*)rsdt;
116+
// Map all pages covered by RSDT
117+
for (uint32_t p = rsdt & ~0xFFF; p < rsdt + rsdt_hdr->length; p += 4096) {
118+
mmu_map_page_dir((uintptr_t*)page_directory, p, p, PAGE_PRESENT | PAGE_RW);
119+
}
117120

118121
uint32_t count = hw_acpi_get_rsdt_entries();
119122
if (count > 256) count = 256; // Sanity check
120123

121-
uint32_t entries_ptr = rsdt + sizeof(acpi_sdt_header_t);
122-
// Map the page containing the entry pointers
123-
mmu_map_page_dir((uintptr_t*)page_directory, entries_ptr, entries_ptr, PAGE_PRESENT | PAGE_RW);
124-
125124
for (uint32_t i = 0; i < count; i++) {
126125
uint32_t entry = hw_acpi_get_rsdt_entry(i);
127126
if (entry) {
128-
// Map the table header (usually enough to read signature/length)
129-
mmu_map_page_dir((uintptr_t*)page_directory, entry, entry, PAGE_PRESENT | PAGE_RW);
130-
131-
// If the table is larger than one page, map subsequent pages based on length
132127
acpi_sdt_header_t* hdr = (acpi_sdt_header_t*)entry;
133-
if (hdr->length > 4096) {
134-
for (uint32_t j = 4096; j < hdr->length; j += 4096) {
135-
mmu_map_page_dir((uintptr_t*)page_directory, entry + j, entry + j, PAGE_PRESENT | PAGE_RW);
136-
}
128+
// Map all pages covered by this table
129+
for (uint32_t p = entry & ~0xFFF; p < entry + hdr->length; p += 4096) {
130+
mmu_map_page_dir((uintptr_t*)page_directory, p, p, PAGE_PRESENT | PAGE_RW);
137131
}
138132
}
139133
}
@@ -229,6 +223,10 @@ uintptr_t* mmu_get_current_space(void) {
229223
return (uintptr_t*)cr3;
230224
}
231225

226+
uintptr_t* mmu_get_kernel_space(void) {
227+
return (uintptr_t*)page_directory;
228+
}
229+
232230
uintptr_t mmu_get_phys_dir(uintptr_t* dir, uintptr_t virt) {
233231
if (!dir) return 0;
234232
uint32_t pd_index = virt >> 22;

‎src/arch/x86/smp_rally.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void smp_ap_entry(void) {
3737
uint32_t cpu_id = (*(volatile uint32_t*)(LAPIC_BASE + LAPIC_ID)) >> 24;
3838

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

4242
// Enable PSE first for 4MB pages
4343
mmu_enable_pse();

‎src/drivers/vga.c‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ static void vga_put_at(char ch, uint8_t color, uint8_t x, uint8_t y) {
5959
vga_buffer[idx] = (uint16_t)ch | ((uint16_t)color << 8);
6060
}
6161

62+
static void vga_scroll(void) {
63+
for (uint8_t y = 0; y < vga_height - 1; y++) {
64+
for (uint8_t x = 0; x < vga_width; x++) {
65+
vga_buffer[y * vga_width + x] = vga_buffer[(y + 1) * vga_width + x];
66+
}
67+
}
68+
uint16_t blank = (uint16_t)' ' | ((uint16_t)vga_current_color << 8);
69+
for (uint8_t x = 0; x < vga_width; x++) {
70+
vga_buffer[(vga_height - 1) * vga_width + x] = blank;
71+
}
72+
}
73+
6274
void vga_putc(char ch) {
6375
if (ch == '\n') {
6476
vga_cursor_x = 0;
@@ -76,11 +88,11 @@ void vga_putc(char ch) {
7688
}
7789

7890
if (vga_cursor_y >= vga_height) {
79-
// Simple scroll: just reset to top or stay at bottom?
80-
// For now, let's just stay at bottom and clear or similar.
81-
// Real scrolling would involve memmove.
91+
vga_scroll();
8292
vga_cursor_y = vga_height - 1;
8393
}
94+
95+
vga_set_cursor(vga_cursor_x, vga_cursor_y);
8496
}
8597

8698
void vga_puts(const char* text) {

0 commit comments

Comments
 (0)