Skip to content

Commit 5dea49a

Browse files
committed
fix
1 parent 52f5e7e commit 5dea49a

4 files changed

Lines changed: 41 additions & 13 deletions

File tree

.traerules

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# OS Development Rules for Cursor
2+
3+
## Environment Context
4+
- We are building a **freestanding x86 kernel**.
5+
- **NO Standard Library:** Do not use <stdio.h>, <stdlib.h>, <string.h>, etc.
6+
- **Allowed Headers:** <stdint.h>, <stddef.h>, <stdbool.h>, and <limits.h>.
7+
- **Target Architecture:** x86 (32-bit/i686).
8+
- **Bootloader:** Multiboot compliant (GRUB).
9+
10+
## Coding Preferences
11+
- Use **Pointer Arithmetic** for memory-mapped I/O (e.g., VGA buffer at 0xB8000).
12+
- Use **Inline Assembly** (`__asm__ __volatile__`) for CPU instructions like `inb`, `outb`, `cli`, `hlt`, and `lgdt`.
13+
- Prefer `uint8_t`, `uint16_t`, and `uint32_t` for hardware-specific structures to ensure exact sizing.
14+
- All structures representing hardware tables (GDT, IDT) MUST be marked with `__attribute__((packed))`.
15+
16+
## Project Structure
17+
- `/src/boot`: Assembly entry files (.s)
18+
- `/src/kernel`: Main C logic (.c)
19+
- `/src/drivers`: Hardware drivers (VGA, Keyboard, etc.)
20+
- `/include`: Header files (.h)

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
cmake_minimum_required(VERSION 3.16)
22

3+
# For freestanding projects (kernels), we don't have a standard library yet.
4+
# Tell CMake to only test the compiler by creating a static library instead of an executable.
5+
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
6+
37
# Force the NASM object format to 32-bit ELF
48
set(CMAKE_ASM_NASM_OBJECT_FORMAT elf32)
59
set(CMAKE_ASM_NASM_FLAGS "-f elf32")
@@ -32,6 +36,11 @@ include_directories(include)
3236
file(GLOB_RECURSE ASM_SOURCES "src/*.asm")
3337
file(GLOB_RECURSE C_SOURCES "src/*.c")
3438

39+
# Ensure boot.asm is ALWAYS FIRST in the link order to keep Multiboot header at the top
40+
set(BOOT_ASM "${CMAKE_CURRENT_SOURCE_DIR}/src/boot.asm")
41+
list(REMOVE_ITEM ASM_SOURCES "${BOOT_ASM}")
42+
set(ASM_SOURCES "${BOOT_ASM}" ${ASM_SOURCES})
43+
3544
# Exclude specific files if needed
3645
list(FILTER C_SOURCES EXCLUDE REGEX "src/drivers/vga.c")
3746

linker.ld

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ SECTIONS
77

88
.text BLOCK(4K) : ALIGN(4K)
99
{
10-
*(.multiboot)
11-
*(.text*)
10+
/* MUST be at the very top of the binary */
11+
KEEP(*(.multiboot))
12+
*(.text)
1213
}
1314

1415
.rodata BLOCK(4K) : ALIGN(4K)

src/boot.asm

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
[bits 32]
2-
31
section .multiboot
42
align 4
5-
dd 0x1BADB002
6-
dd 0x00000000
7-
dd 0xE4524FFE
3+
dd 0x1BADB002 ; Magic number
4+
dd 0x00000003 ; Flags (align modules + mem info)
5+
dd -(0x1BADB002 + 0x00000003) ; Checksum
86

97
section .text
108
global _start
119
extern kernel_main
1210

1311
_start:
14-
cli
12+
; Setup stack and call kernel_main
1513
mov esp, stack_top
16-
push ebx
17-
push eax
14+
push ebx ; Pointer to Multiboot info
1815
call kernel_main
19-
add esp, 8
20-
jmp $
16+
cli
17+
.hang: hlt
18+
jmp .hang
2119

2220
section .bss
2321
align 16
2422
stack_bottom:
25-
resb 16384
23+
resb 16384 ; 16 KB stack
2624
stack_top:

0 commit comments

Comments
 (0)