mirror of
https://github.com/gbowne1/ClassicOS.git
synced 2026-03-09 08:25:19 -07:00
Compare commits
151 Commits
gbowne1-cp
...
3e6f8d0072
| Author | SHA1 | Date | |
|---|---|---|---|
| 3e6f8d0072 | |||
| 56faa3143d | |||
| 9be6a4e64b | |||
| 255ac93cae | |||
|
|
bfc9911a85 | ||
|
|
9d394d984b | ||
|
|
45abf9418b | ||
|
|
0584f7d49d | ||
| 4700e3f11a | |||
| 769f96acbc | |||
| 1575c0dd12 | |||
| 37e672a1c2 | |||
| 375dd9afdf | |||
| a42822dd54 | |||
| 6f0fee4182 | |||
| 01f130227b | |||
| f473fd5c32 | |||
| 576f5be8a6 | |||
| 538b97b877 | |||
| 4d2eec9e93 | |||
| d30bc6a7f5 | |||
| 96050d6a99 | |||
| e9e3237ba9 | |||
| 3c6b2a4e8c | |||
|
|
8db5173495 | ||
| c0220b3afc | |||
| f9ef99d11b | |||
| 17ce531b62 | |||
| 4aa6fa3b59 | |||
|
|
6d78074060 | ||
|
|
3fde819c49 | ||
|
|
aac87e30e0 | ||
|
|
975e21f9be | ||
|
|
db2c17d4b4 | ||
|
|
c805d24dd0 | ||
|
|
b3831b6c69 | ||
| 4e1197356b | |||
| a765fe5238 | |||
| 175c2949be | |||
| 2dd82e69a7 | |||
|
|
aeecf2b0b7 | ||
|
|
091391907f | ||
|
|
5dcf5d24b2 | ||
|
|
a7e536a6d3 | ||
|
|
b99cb27092 | ||
|
|
a3267d2ac3 | ||
|
|
bc08701e63 | ||
|
|
e8f6beef8a | ||
|
|
bd427dc7e9 | ||
|
|
619430d4bf | ||
|
|
84e28d8871 | ||
| dee91ca211 | |||
| 59dfca9ebd | |||
| 42f0b62e50 | |||
| 0a4f0e59e3 | |||
| 9d37b7a944 | |||
| eecc2561da | |||
| cae3731f91 | |||
| df33351d60 | |||
| d2b19ce376 | |||
| 8aea3c3c0e | |||
| f64e40d460 | |||
| 45da511bfa | |||
| 0f2528e07b | |||
| b36356f10c | |||
| 4b981a5d18 | |||
| fa66433179 | |||
|
|
f08204a2b3 | ||
| bb6f9d1a16 | |||
| 57ac458a4f | |||
| 6ad8644752 | |||
| 40462e55e6 | |||
|
|
4d2197aa2d | ||
|
|
f7b6a78b59 | ||
|
|
2b32a29890 | ||
|
|
4ee0ddb9ef | ||
|
|
13b915a99d | ||
|
|
d480fbcc80 | ||
|
|
d0c9c9c4e0 | ||
| 8be984d565 | |||
| 4f992c8fc5 | |||
| 16057d41d6 | |||
| 41281de743 | |||
|
|
dfb161a15a | ||
|
|
a450ac06f2 | ||
|
|
235fd2636d | ||
|
|
8c529c6fe4 | ||
|
|
155563e434 | ||
|
|
8b1ea16c56 | ||
|
|
7bda5c25b8 | ||
|
|
ac0fde28a0 | ||
|
|
a182bbca19 | ||
|
|
c503709ff4 | ||
|
|
4fa82854dd | ||
| 903061551c | |||
| 4393fbf6cb | |||
| f572101d6b | |||
| a37f94de44 | |||
| 401a19143c | |||
| 3cd2ff6e1e | |||
| aafd4efcb2 | |||
| d6eb5115d5 | |||
| cc92ade8fd | |||
| a9f6d5fa05 | |||
| 84705fd225 | |||
| 6c69b5fd6a | |||
| 1037ba4f54 | |||
| 745deeddde | |||
| f9e281a7ae | |||
| 18801a742f | |||
| a08648eff5 | |||
| 5a664c6e31 | |||
| 4c7de228f9 | |||
| af3f20485f | |||
| 09b2b8cd11 | |||
| 4079d18a45 | |||
| b6c158957e | |||
| cca6aafd65 | |||
| 49c1bad935 | |||
| 7adb798c17 | |||
| 43b5cacd09 | |||
|
|
a7b0d1152f | ||
|
|
e38f1aa2ee | ||
|
|
78d5e9a7ab | ||
|
|
daead5ee57 | ||
|
|
4fb81d2e57 | ||
|
|
19ef0a8627 | ||
| c5f7e4e563 | |||
| f516334e0d | |||
| 19f7c7b213 | |||
| bc9d84a93e | |||
| 9066ceaddb | |||
|
|
3b67e81ed0 | ||
|
|
841892398a | ||
| 86608ef48c | |||
| 785c8920d8 | |||
| c0e7ab6be0 | |||
| f78bc27f35 | |||
| 507b4f5511 | |||
|
|
12046ce96b | ||
|
|
a9b8ac7066 | ||
| d6ab8c91f8 | |||
|
|
35ebd5fd72 | ||
| 10d3761be1 | |||
| cc2e967a4d | |||
| 4e8b13ad77 | |||
| 9f8ca3a60c | |||
| 574980035e | |||
| a0bd0941d6 | |||
| 4047bc3936 | |||
| 7e54f0de66 |
24
.clang-format
Normal file
24
.clang-format
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
BasedOnStyle: Google
|
||||||
|
IndentWidth: 4
|
||||||
|
TabWidth: 4
|
||||||
|
UseTab: Never
|
||||||
|
ColumnLimit: 80
|
||||||
|
|
||||||
|
DerivePointerAlignment: false
|
||||||
|
PointerAlignment: Right
|
||||||
|
ReferenceAlignment: Right
|
||||||
|
|
||||||
|
AlignConsecutiveMacros: Consecutive
|
||||||
|
AlignTrailingComments:
|
||||||
|
Kind: Always
|
||||||
|
OverEmptyLines: 0
|
||||||
|
|
||||||
|
IncludeBlocks: Regroup
|
||||||
|
IncludeCategories:
|
||||||
|
# Std headers
|
||||||
|
- Regex: '<[[:alnum:]_.]+>'
|
||||||
|
Priority: 2
|
||||||
|
|
||||||
|
# Other headers
|
||||||
|
- Regex: '.*'
|
||||||
|
Priority: 1
|
||||||
6
.clangd
Normal file
6
.clangd
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
CompileFlags:
|
||||||
|
CompilationDatabase: build
|
||||||
|
|
||||||
|
Diagnostics:
|
||||||
|
UnusedIncludes: Strict
|
||||||
|
MissingIncludes: Strict
|
||||||
12
.editorconfig
Normal file
12
.editorconfig
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
charset = utf-8
|
||||||
|
end_of_line = lf
|
||||||
|
insert_final_newline = true
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
|
||||||
|
[Makefile]
|
||||||
|
indent_style = tab
|
||||||
|
indent_size = 8
|
||||||
|
tab_width = 8
|
||||||
15
Makefile
15
Makefile
@@ -8,8 +8,13 @@ OBJCOPY = i386-elf-objcopy
|
|||||||
BUILD_DIR = build
|
BUILD_DIR = build
|
||||||
CROSS_DIR = cross
|
CROSS_DIR = cross
|
||||||
DISK_IMG = $(BUILD_DIR)/disk.img
|
DISK_IMG = $(BUILD_DIR)/disk.img
|
||||||
|
|
||||||
|
STAGE2_ADDR = 0x7e00
|
||||||
STAGE2_SIZE = 2048
|
STAGE2_SIZE = 2048
|
||||||
|
|
||||||
|
# Place the memory map (e820) past stage2 bl in memory
|
||||||
|
MEMMAP_BASE = $(shell echo $$(($(STAGE2_ADDR) + $(STAGE2_SIZE))))
|
||||||
|
|
||||||
KERNEL_C_SRC = $(wildcard kernel/*.c)
|
KERNEL_C_SRC = $(wildcard kernel/*.c)
|
||||||
KERNEL_ASM_SRC = $(wildcard kernel/*.asm)
|
KERNEL_ASM_SRC = $(wildcard kernel/*.asm)
|
||||||
KERNEL_OBJ = $(patsubst kernel/%.c, $(BUILD_DIR)/%.o, $(KERNEL_C_SRC))
|
KERNEL_OBJ = $(patsubst kernel/%.c, $(BUILD_DIR)/%.o, $(KERNEL_C_SRC))
|
||||||
@@ -18,9 +23,9 @@ KERNEL_OBJ += $(patsubst kernel/%.asm, $(BUILD_DIR)/asm_%.o, $(KERNEL_ASM_SRC))
|
|||||||
KLIBC_SRC = $(wildcard klibc/src/*.c)
|
KLIBC_SRC = $(wildcard klibc/src/*.c)
|
||||||
KLIBC_OBJ = $(patsubst klibc/src/%.c, $(BUILD_DIR)/klibc/%.o, $(KLIBC_SRC))
|
KLIBC_OBJ = $(patsubst klibc/src/%.c, $(BUILD_DIR)/klibc/%.o, $(KLIBC_SRC))
|
||||||
|
|
||||||
|
.PHONY: all stage1 stage2 kernel compile-commands $(BUILD_DIR)/compile_commands.json run gdb clean clean-cross clean-all
|
||||||
all: $(DISK_IMG)
|
all: $(DISK_IMG)
|
||||||
|
|
||||||
.PHONY: stage1 stage2 kernel run gdb clean
|
|
||||||
stage1: $(BUILD_DIR)
|
stage1: $(BUILD_DIR)
|
||||||
$(AS) $(ASFLAGS) -o $(BUILD_DIR)/$@.o bootloader/$@.asm
|
$(AS) $(ASFLAGS) -o $(BUILD_DIR)/$@.o bootloader/$@.asm
|
||||||
$(LD) -Ttext=0x7c00 -melf_i386 -o $(BUILD_DIR)/$@.elf $(BUILD_DIR)/$@.o
|
$(LD) -Ttext=0x7c00 -melf_i386 -o $(BUILD_DIR)/$@.elf $(BUILD_DIR)/$@.o
|
||||||
@@ -29,7 +34,7 @@ stage1: $(BUILD_DIR)
|
|||||||
# NOTE: Stage2 final size should be checked against `$(STAGE2_SIZE)` by the build system to avoid an overflow.
|
# NOTE: Stage2 final size should be checked against `$(STAGE2_SIZE)` by the build system to avoid an overflow.
|
||||||
# Alternatively, convey the final stage2 size through other means to stage1.
|
# Alternatively, convey the final stage2 size through other means to stage1.
|
||||||
stage2: $(BUILD_DIR)
|
stage2: $(BUILD_DIR)
|
||||||
$(AS) $(ASFLAGS) -o $(BUILD_DIR)/stage2.o bootloader/stage2.asm
|
$(AS) $(ASFLAGS) -DMEMMAP_BASE=$(MEMMAP_BASE) -o $(BUILD_DIR)/stage2.o bootloader/stage2.asm
|
||||||
$(CC) -std=c11 -ffreestanding -nostdlib -nostdinc -fno-stack-protector -m32 -Iklibc/include -g -c -o $(BUILD_DIR)/stage2_load.o bootloader/stage2_load.c
|
$(CC) -std=c11 -ffreestanding -nostdlib -nostdinc -fno-stack-protector -m32 -Iklibc/include -g -c -o $(BUILD_DIR)/stage2_load.o bootloader/stage2_load.c
|
||||||
$(LD) -Tbootloader/stage2.ld -melf_i386 -o $(BUILD_DIR)/$@.elf $(BUILD_DIR)/stage2.o $(BUILD_DIR)/stage2_load.o
|
$(LD) -Tbootloader/stage2.ld -melf_i386 -o $(BUILD_DIR)/$@.elf $(BUILD_DIR)/stage2.o $(BUILD_DIR)/stage2_load.o
|
||||||
$(OBJCOPY) -O binary $(BUILD_DIR)/$@.elf $(BUILD_DIR)/$@.bin
|
$(OBJCOPY) -O binary $(BUILD_DIR)/$@.elf $(BUILD_DIR)/$@.bin
|
||||||
@@ -39,7 +44,7 @@ $(BUILD_DIR)/asm_%.o: kernel/%.asm
|
|||||||
$(AS) $(ASFLAGS) -o $@ $<
|
$(AS) $(ASFLAGS) -o $@ $<
|
||||||
|
|
||||||
$(BUILD_DIR)/%.o: kernel/%.c
|
$(BUILD_DIR)/%.o: kernel/%.c
|
||||||
$(CC) -std=c11 -ffreestanding -nostdlib -nostdinc -fno-stack-protector -m32 -Iklibc/include -g -c -o $@ $<
|
$(CC) -DMEMMAP_BASE=$(MEMMAP_BASE) -std=c11 -ffreestanding -nostdlib -nostdinc -fno-stack-protector -m32 -Iklibc/include -g -c -o $@ $<
|
||||||
|
|
||||||
$(BUILD_DIR)/klibc/%.o: klibc/src/%.c
|
$(BUILD_DIR)/klibc/%.o: klibc/src/%.c
|
||||||
$(CC) -std=c11 -ffreestanding -nostdlib -nostdinc -fno-stack-protector -m32 -Iklibc/include -g -c -o $@ $<
|
$(CC) -std=c11 -ffreestanding -nostdlib -nostdinc -fno-stack-protector -m32 -Iklibc/include -g -c -o $@ $<
|
||||||
@@ -57,6 +62,10 @@ $(BUILD_DIR):
|
|||||||
mkdir -p $@
|
mkdir -p $@
|
||||||
mkdir -p $(BUILD_DIR)/klibc
|
mkdir -p $(BUILD_DIR)/klibc
|
||||||
|
|
||||||
|
compile-commands: $(BUILD_DIR)/compile_commands.json
|
||||||
|
$(BUILD_DIR)/compile_commands.json: $(BUILD_DIR)
|
||||||
|
bear --output $@ -- make -B
|
||||||
|
|
||||||
run:
|
run:
|
||||||
qemu-system-i386 -s -S $(DISK_IMG)
|
qemu-system-i386 -s -S $(DISK_IMG)
|
||||||
|
|
||||||
|
|||||||
@@ -11,16 +11,16 @@ Bootloader documentation for ClassicOS
|
|||||||
|
|
||||||
## Stage 1 (`stage1.asm`)
|
## Stage 1 (`stage1.asm`)
|
||||||
|
|
||||||
Responsible for loading the second stage using BIOS routines, and switching to protected mode.
|
|
||||||
|
|
||||||
- Queries CHS parameters from BIOS
|
- Queries CHS parameters from BIOS
|
||||||
- Loads the second stage bootloader (2048 B) to `0x7c00`
|
- Loads the second stage bootloader (2048 B) to `0x7c00`
|
||||||
- Sets up a GDT with descriptor entries for code and data both covering the whole 32-bit address space
|
|
||||||
- Enables A20
|
- Enables A20
|
||||||
- Set CR0.PE (enable protected mode) and jump to stage 2
|
- Jumps to stage2
|
||||||
|
|
||||||
## Stage 2 (`stage2.asm, stage2_load.c`)
|
## Stage 2 (`stage2.asm, stage2_load.c`)
|
||||||
|
|
||||||
|
- Read and store E820 memory map from BIOS
|
||||||
|
- Sets up a GDT with descriptor entries for code and data both covering the whole 32-bit address space
|
||||||
|
- Set CR0.PE (enable protected mode)
|
||||||
- Set up segment registers
|
- Set up segment registers
|
||||||
- Load the kernel ELF header
|
- Load the kernel ELF header
|
||||||
- Parse the program headers, and load all `PT_LOAD` segments from disk
|
- Parse the program headers, and load all `PT_LOAD` segments from disk
|
||||||
|
|||||||
@@ -40,11 +40,8 @@ _start:
|
|||||||
call enable_a20
|
call enable_a20
|
||||||
jc a20_error ; Jump if A20 enable fails
|
jc a20_error ; Jump if A20 enable fails
|
||||||
|
|
||||||
; Setup Global Descriptor Table
|
; Jump to s2
|
||||||
call setup_gdt
|
jmp 0x7e00
|
||||||
|
|
||||||
; Switch to protected mode and jump to second stage at 0x08:0x7E00
|
|
||||||
call switch_to_pm
|
|
||||||
|
|
||||||
disk_error:
|
disk_error:
|
||||||
mov si, disk_error_msg
|
mov si, disk_error_msg
|
||||||
@@ -241,30 +238,6 @@ check_a20:
|
|||||||
clc ; Clear carry flag to indicate success
|
clc ; Clear carry flag to indicate success
|
||||||
ret
|
ret
|
||||||
|
|
||||||
; ----------------------------------------------------------------
|
|
||||||
gdt_start:
|
|
||||||
dq 0x0000000000000000 ; Null descriptor
|
|
||||||
dq 0x00CF9A000000FFFF ; 32-bit code segment (selector 0x08)
|
|
||||||
dq 0x00CF92000000FFFF ; 32-bit data segment (selector 0x10)
|
|
||||||
dq 0x00009A000000FFFF ; 16-bit code segment for real mode (selector 0x18)
|
|
||||||
|
|
||||||
gdt_descriptor:
|
|
||||||
dw gdt_end - gdt_start - 1
|
|
||||||
dd gdt_start
|
|
||||||
gdt_end:
|
|
||||||
|
|
||||||
setup_gdt:
|
|
||||||
lgdt [gdt_descriptor]
|
|
||||||
ret
|
|
||||||
|
|
||||||
; ----------------------------------------------------------------
|
|
||||||
switch_to_pm:
|
|
||||||
cli
|
|
||||||
mov eax, cr0
|
|
||||||
or eax, 1
|
|
||||||
mov cr0, eax
|
|
||||||
jmp 0x08:0x7E00 ; jump to S2
|
|
||||||
|
|
||||||
; ----------------------------------------------------------------
|
; ----------------------------------------------------------------
|
||||||
print_string_16:
|
print_string_16:
|
||||||
.loop:
|
.loop:
|
||||||
|
|||||||
@@ -1,10 +1,80 @@
|
|||||||
[BITS 32]
|
|
||||||
global _start
|
global _start
|
||||||
global ata_lba_read
|
|
||||||
|
|
||||||
extern load_kernel
|
extern load_kernel
|
||||||
|
|
||||||
|
%define e820_magic 0x534d4150 ; "SMAP"
|
||||||
|
%define e820_entry_size 24
|
||||||
|
%define e820_max_entries 128
|
||||||
|
|
||||||
|
; ----------------------------------------------------------------
|
||||||
|
; Real mode
|
||||||
|
; ----------------------------------------------------------------
|
||||||
|
[BITS 16]
|
||||||
_start:
|
_start:
|
||||||
|
call read_e820
|
||||||
|
call setup_gdt
|
||||||
|
call switch_to_pm
|
||||||
|
|
||||||
|
read_e820:
|
||||||
|
xor ebx, ebx
|
||||||
|
mov es, bx
|
||||||
|
mov di, MEMMAP_BASE+4 ; ES=0 DI=MEMMAP_BASE+4
|
||||||
|
xor bp, bp ; Keeping count in bp
|
||||||
|
|
||||||
|
.e820_loop:
|
||||||
|
mov eax, 0xe820
|
||||||
|
mov ecx, e820_entry_size
|
||||||
|
mov edx, e820_magic
|
||||||
|
int 0x15
|
||||||
|
jc .done ; Error?
|
||||||
|
|
||||||
|
cmp eax, e820_magic ; Verify "SMAP"
|
||||||
|
jne .done
|
||||||
|
|
||||||
|
test ecx, ecx ; Skip 0-sized entries
|
||||||
|
jz .skip
|
||||||
|
|
||||||
|
add di, e820_entry_size ; Advance write addr
|
||||||
|
inc bp ; Increment count
|
||||||
|
|
||||||
|
cmp bp, e820_max_entries ; Stop if we're at capacity
|
||||||
|
jae .done
|
||||||
|
.skip:
|
||||||
|
test ebx, ebx
|
||||||
|
jne .e820_loop
|
||||||
|
.done:
|
||||||
|
mov [MEMMAP_BASE], bp ; Store count
|
||||||
|
ret
|
||||||
|
|
||||||
|
setup_gdt:
|
||||||
|
lgdt [gdt_descriptor]
|
||||||
|
ret
|
||||||
|
|
||||||
|
switch_to_pm:
|
||||||
|
cli
|
||||||
|
mov eax, cr0
|
||||||
|
or eax, 1
|
||||||
|
mov cr0, eax
|
||||||
|
jmp 0x08:pm_entry
|
||||||
|
|
||||||
|
e820_count:
|
||||||
|
dw 0
|
||||||
|
|
||||||
|
gdt_start:
|
||||||
|
dq 0x0000000000000000 ; Null descriptor
|
||||||
|
dq 0x00CF9A000000FFFF ; 32-bit code segment (selector 0x08)
|
||||||
|
dq 0x00CF92000000FFFF ; 32-bit data segment (selector 0x10)
|
||||||
|
dq 0x00009A000000FFFF ; 16-bit code segment for real mode (selector 0x18)
|
||||||
|
gdt_descriptor:
|
||||||
|
dw gdt_end - gdt_start - 1
|
||||||
|
dd gdt_start
|
||||||
|
gdt_end:
|
||||||
|
|
||||||
|
; ----------------------------------------------------------------
|
||||||
|
; Protected mode
|
||||||
|
; ----------------------------------------------------------------
|
||||||
|
[BITS 32]
|
||||||
|
|
||||||
|
pm_entry:
|
||||||
; Set up segments
|
; Set up segments
|
||||||
; Data segments
|
; Data segments
|
||||||
mov ax, 0x10
|
mov ax, 0x10
|
||||||
@@ -18,90 +88,8 @@ _start:
|
|||||||
mov ax, 0x08
|
mov ax, 0x08
|
||||||
mov cs, ax
|
mov cs, ax
|
||||||
|
|
||||||
; Stack (must be identity-mapped)
|
; Stack
|
||||||
mov esp, 0x90000
|
mov esp, 0x90000
|
||||||
|
|
||||||
call load_kernel
|
call load_kernel
|
||||||
|
|
||||||
jmp eax
|
jmp eax
|
||||||
|
|
||||||
; ----------------------------------------------------------------------------
|
|
||||||
; ATA read sectors (LBA mode)
|
|
||||||
;
|
|
||||||
; sysv32 abi signature:
|
|
||||||
; void ata_lba_read(uint32_t lba, uint8_t nsect, void *addr);
|
|
||||||
; ----------------------------------------------------------------------------
|
|
||||||
ata_lba_read:
|
|
||||||
push ebp
|
|
||||||
mov ebp, esp
|
|
||||||
|
|
||||||
push ebx
|
|
||||||
push ecx
|
|
||||||
push edx
|
|
||||||
push edi
|
|
||||||
|
|
||||||
; Wait BSY=0 before proceeding to write the regs
|
|
||||||
.wait_rdy:
|
|
||||||
mov edx, 0x1F7
|
|
||||||
in al, dx
|
|
||||||
test al, 0x80
|
|
||||||
jnz .wait_rdy
|
|
||||||
|
|
||||||
mov eax, [ebp+8] ; arg #1 = LBA
|
|
||||||
mov cl, [ebp+12] ; arg #2 = # of sectors
|
|
||||||
mov edi, [ebp+16] ; arg #3 = buffer address
|
|
||||||
and eax, 0x0FFFFFFF
|
|
||||||
|
|
||||||
mov ebx, eax ; Save LBA in RBX
|
|
||||||
|
|
||||||
mov edx, 0x01F6 ; Port to send drive and bit 24 - 27 of LBA
|
|
||||||
shr eax, 24 ; Get bit 24 - 27 in al
|
|
||||||
or al, 11100000b ; Set bit 6 in al for LBA mode
|
|
||||||
out dx, al
|
|
||||||
|
|
||||||
mov edx, 0x01F2 ; Port to send number of sectors
|
|
||||||
mov al, cl ; Get number of sectors from CL
|
|
||||||
out dx, al
|
|
||||||
|
|
||||||
mov edx, 0x1F3 ; Port to send bit 0 - 7 of LBA
|
|
||||||
mov eax, ebx ; Get LBA from EBX
|
|
||||||
out dx, al
|
|
||||||
|
|
||||||
mov edx, 0x1F4 ; Port to send bit 8 - 15 of LBA
|
|
||||||
mov eax, ebx ; Get LBA from EBX
|
|
||||||
shr eax, 8 ; Get bit 8 - 15 in AL
|
|
||||||
out dx, al
|
|
||||||
|
|
||||||
mov edx, 0x1F5 ; Port to send bit 16 - 23 of LBA
|
|
||||||
mov eax, ebx ; Get LBA from EBX
|
|
||||||
shr eax, 16 ; Get bit 16 - 23 in AL
|
|
||||||
out dx, al
|
|
||||||
|
|
||||||
mov edx, 0x1F7 ; Command port
|
|
||||||
mov al, 0x20 ; Read with retry.
|
|
||||||
out dx, al
|
|
||||||
|
|
||||||
mov bl, cl ; Save # of sectors in BL
|
|
||||||
|
|
||||||
.wait_rdy2:
|
|
||||||
mov edx, 0x1F7
|
|
||||||
.do_wait_rdy2:
|
|
||||||
in al, dx
|
|
||||||
test al, 0x80 ; BSY?
|
|
||||||
jnz .do_wait_rdy2
|
|
||||||
test al, 0x8 ; DRQ?
|
|
||||||
jz .do_wait_rdy2
|
|
||||||
|
|
||||||
mov edx, 0x1F0 ; Data port, in and out
|
|
||||||
mov ecx, 256
|
|
||||||
rep insw ; in to [RDI]
|
|
||||||
|
|
||||||
dec bl ; are we...
|
|
||||||
jnz .wait_rdy2 ; ...done?
|
|
||||||
|
|
||||||
pop edi
|
|
||||||
pop edx
|
|
||||||
pop ecx
|
|
||||||
pop ebx
|
|
||||||
pop ebp
|
|
||||||
ret
|
|
||||||
|
|||||||
@@ -9,6 +9,4 @@ SECTIONS {
|
|||||||
*(.bss*)
|
*(.bss*)
|
||||||
*(COMMON)
|
*(COMMON)
|
||||||
}
|
}
|
||||||
|
|
||||||
read_buf = .;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,39 @@
|
|||||||
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
// ATA IO Ports
|
||||||
|
#define ATA_PRIMARY_DATA 0x1F0
|
||||||
|
#define ATA_PRIMARY_ERR_FEATURES 0x1F1
|
||||||
|
#define ATA_PRIMARY_SEC_COUNT 0x1F2
|
||||||
|
#define ATA_PRIMARY_LBA_LOW 0x1F3
|
||||||
|
#define ATA_PRIMARY_LBA_MID 0x1F4
|
||||||
|
#define ATA_PRIMARY_LBA_HIGH 0x1F5
|
||||||
|
#define ATA_PRIMARY_DRIVE_SEL 0x1F6
|
||||||
|
#define ATA_PRIMARY_COMM_STAT 0x1F7
|
||||||
|
|
||||||
|
// ATA Commands
|
||||||
|
#define ATA_CMD_READ_PIO 0x20
|
||||||
|
#define ATA_CMD_WRITE_PIO 0x30
|
||||||
|
|
||||||
// ELF Ident indexes
|
// ELF Ident indexes
|
||||||
#define EI_NIDENT 16
|
#define EI_NIDENT 16
|
||||||
|
|
||||||
// Program header types
|
// Program header types
|
||||||
#define PT_NULL 0
|
#define PT_NULL 0
|
||||||
#define PT_LOAD 1
|
#define PT_LOAD 1
|
||||||
|
|
||||||
|
// Disk sector size
|
||||||
|
#define SECTOR_SIZE 512
|
||||||
|
#define PH_PER_SECTOR (SECTOR_SIZE / sizeof(Elf32_Phdr))
|
||||||
|
|
||||||
|
// Kernel start LBA
|
||||||
|
#define KERN_START_SECT 5
|
||||||
|
|
||||||
|
// VGA
|
||||||
|
// Expects bios initialization for text mode (3), buffer at 0xb8000
|
||||||
|
#define VGA_ADDRESS 0xB8000
|
||||||
|
#define VGA_COLS 80
|
||||||
|
#define VGA_ROWS 25
|
||||||
|
|
||||||
// ELF Header (32-bit)
|
// ELF Header (32-bit)
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -37,82 +65,171 @@ typedef struct {
|
|||||||
uint32_t p_align;
|
uint32_t p_align;
|
||||||
} __attribute__((packed)) Elf32_Phdr;
|
} __attribute__((packed)) Elf32_Phdr;
|
||||||
|
|
||||||
|
static inline uint8_t inb(uint16_t port)
|
||||||
|
{
|
||||||
|
uint8_t ret;
|
||||||
|
__asm__ volatile ("inb %1, %0"
|
||||||
|
: "=a"(ret)
|
||||||
|
: "Nd"(port));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void outb(uint16_t port, uint8_t val)
|
||||||
|
{
|
||||||
|
__asm__ volatile ("outb %0, %1"
|
||||||
|
:
|
||||||
|
: "a"(val), "Nd"(port));
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint16_t inw(uint16_t port)
|
||||||
|
{
|
||||||
|
uint16_t ret;
|
||||||
|
__asm__ volatile ("inw %1, %0"
|
||||||
|
: "=a"(ret)
|
||||||
|
: "Nd"(port));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void ata_wait_bsy() {
|
||||||
|
while (inb(ATA_PRIMARY_COMM_STAT) & 0x80);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void ata_wait_drq() {
|
||||||
|
while (!(inb(ATA_PRIMARY_COMM_STAT) & 0x08));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ata_read_sector(void *addr, uint32_t lba) {
|
||||||
|
ata_wait_bsy();
|
||||||
|
|
||||||
|
outb(ATA_PRIMARY_DRIVE_SEL, 0xE0 | ((lba >> 24) & 0x0F));
|
||||||
|
outb(ATA_PRIMARY_SEC_COUNT, 1);
|
||||||
|
outb(ATA_PRIMARY_LBA_LOW, (uint8_t)lba);
|
||||||
|
outb(ATA_PRIMARY_LBA_MID, (uint8_t)(lba >> 8));
|
||||||
|
outb(ATA_PRIMARY_LBA_HIGH, (uint8_t)(lba >> 16));
|
||||||
|
outb(ATA_PRIMARY_COMM_STAT, ATA_CMD_READ_PIO);
|
||||||
|
|
||||||
|
uint16_t* ptr = (uint16_t*)addr;
|
||||||
|
ata_wait_bsy();
|
||||||
|
ata_wait_drq();
|
||||||
|
for (int i = 0; i < 256; i++) {
|
||||||
|
*ptr++ = inw(ATA_PRIMARY_DATA);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ata_read_sectors(uint8_t *addr, uint32_t offset, uint32_t size)
|
||||||
|
{
|
||||||
|
// Reads are offset from the starting sector of the kernel
|
||||||
|
uint32_t lba = KERN_START_SECT + offset / SECTOR_SIZE;
|
||||||
|
uint32_t off = offset % 512;
|
||||||
|
uint8_t data[512];
|
||||||
|
|
||||||
|
while (size > 0) {
|
||||||
|
ata_read_sector(data, lba);
|
||||||
|
|
||||||
|
uint32_t copy = 512 - off;
|
||||||
|
if (copy > size) {
|
||||||
|
copy = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < copy; i++) {
|
||||||
|
addr[i] = data[off + i];
|
||||||
|
}
|
||||||
|
|
||||||
|
addr += copy;
|
||||||
|
size -= copy;
|
||||||
|
lba++;
|
||||||
|
off = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_error(const char *msg)
|
||||||
|
{
|
||||||
|
uint16_t *ptr = (uint16_t *)VGA_ADDRESS;
|
||||||
|
|
||||||
|
// Clear
|
||||||
|
uint16_t val = 0x0f00 | (uint8_t)' ';
|
||||||
|
for (size_t i = 0; i < VGA_COLS * VGA_ROWS; i++) {
|
||||||
|
ptr[i] = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print error
|
||||||
|
for (size_t i = 0; msg[i]; i++) {
|
||||||
|
ptr[i] = 0xf00 | (uint8_t)msg[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Halt
|
||||||
|
while (1) {
|
||||||
|
__asm__("hlt");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Load an ELF executable into memory.
|
// Load an ELF executable into memory.
|
||||||
static int elf_load(const void* data, void (*load_segment)(uint8_t *vaddr, uint32_t src, uint32_t size)) {
|
// NOTE: Only 32-byte program headers are supported.
|
||||||
const Elf32_Ehdr* header = (const Elf32_Ehdr*)data;
|
// Returns the entry point to the program.
|
||||||
const Elf32_Phdr* ph = (const Elf32_Phdr*)((uint8_t*)data + header->e_phoff);
|
static void *elf_load(const void *data) {
|
||||||
|
const Elf32_Ehdr *header = (const Elf32_Ehdr*)data;
|
||||||
|
|
||||||
|
if (header->e_phentsize != sizeof(Elf32_Phdr)) {
|
||||||
|
// The bootloader only handles 32-byte program header entries
|
||||||
|
on_error("ERROR: Unsupported program header entry size, halting...");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Buffer for the program headers
|
||||||
|
uint8_t file_buf[SECTOR_SIZE];
|
||||||
|
|
||||||
|
// Current file offset to the next program header
|
||||||
|
uint32_t file_offset = header->e_phoff;
|
||||||
|
|
||||||
for (int i = 0; i < header->e_phnum; i++) {
|
for (int i = 0; i < header->e_phnum; i++) {
|
||||||
if (ph[i].p_type != PT_LOAD)
|
// Check for sector boundary.
|
||||||
|
// Program headers are read in a sector at a time
|
||||||
|
// 512 / 32 = 16 PH per sector
|
||||||
|
if (i % PH_PER_SECTOR == 0) {
|
||||||
|
uint32_t count = (header->e_phnum - i) * sizeof(Elf32_Phdr);
|
||||||
|
if (count > SECTOR_SIZE) {
|
||||||
|
count = SECTOR_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reads
|
||||||
|
ata_read_sectors(file_buf, file_offset, count);
|
||||||
|
file_offset += count;
|
||||||
|
}
|
||||||
|
|
||||||
|
// PH being processed currently, index mod 16 as headers
|
||||||
|
// are being loaded in sector by sector.
|
||||||
|
const Elf32_Phdr *ph = (const Elf32_Phdr *)file_buf + (i % PH_PER_SECTOR);
|
||||||
|
|
||||||
|
// Discard non-load segments
|
||||||
|
if (ph->p_type != PT_LOAD)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
uint32_t offset = ph[i].p_offset;
|
// Load in the segment
|
||||||
uint32_t vaddr = ph[i].p_vaddr;
|
uint32_t offset = ph->p_offset;
|
||||||
uint32_t filesz = ph[i].p_filesz;
|
uint32_t filesz = ph->p_filesz;
|
||||||
uint32_t memsz = ph[i].p_memsz;
|
uint32_t memsz = ph->p_memsz;
|
||||||
|
uint8_t *vaddr = (uint8_t *)ph->p_vaddr;
|
||||||
// Copy data segment
|
ata_read_sectors(vaddr, offset, filesz);
|
||||||
//load_segment((uint8_t *)vaddr, offset, filesz);
|
|
||||||
load_segment((uint8_t *)vaddr, offset, filesz);
|
|
||||||
|
|
||||||
// Zero remaining BSS (if any)
|
// Zero remaining BSS (if any)
|
||||||
if (memsz > filesz) {
|
if (memsz > filesz) {
|
||||||
uint8_t* bss_start = (uint8_t*)(vaddr + filesz);
|
uint8_t* bss_start = vaddr + filesz;
|
||||||
for (uint32_t j = 0; j < memsz - filesz; j++) {
|
for (uint32_t j = 0; j < memsz - filesz; j++) {
|
||||||
bss_start[j] = 0;
|
bss_start[j] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return header->e_entry;
|
// Return the entry point
|
||||||
}
|
return (void *)header->e_entry;
|
||||||
|
|
||||||
#define KERN_START_SECT 5
|
|
||||||
#define MAX(a, b) ((a)>(b) ? (a) : (b))
|
|
||||||
|
|
||||||
extern void ata_lba_read(uint32_t lba, uint8_t nsect, void *addr);
|
|
||||||
extern uint8_t read_buf[];
|
|
||||||
|
|
||||||
static uint32_t
|
|
||||||
total_header_size(const Elf32_Ehdr *header) {
|
|
||||||
uint32_t phend = header->e_phoff + header->e_phentsize*header->e_phnum;
|
|
||||||
|
|
||||||
// Align to 512
|
|
||||||
return (phend + 511) & ~511;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void read_sectors(uint8_t *vaddr, uint32_t offset, uint32_t size) {
|
|
||||||
// # of sectors to read
|
|
||||||
uint32_t rem_nsect = ((size + 511) & ~511) / 512;
|
|
||||||
|
|
||||||
// Current lba address, offset by the first sector already read
|
|
||||||
uint32_t lba = KERN_START_SECT + offset / 512;
|
|
||||||
|
|
||||||
// Max 255 sectors at a time
|
|
||||||
while (rem_nsect) {
|
|
||||||
uint8_t nsect = rem_nsect > 255 ? 255 : rem_nsect;
|
|
||||||
ata_lba_read(lba, nsect, vaddr);
|
|
||||||
|
|
||||||
vaddr += nsect * 512;
|
|
||||||
rem_nsect -= nsect;
|
|
||||||
lba += nsect;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void *load_kernel(void) {
|
void *load_kernel(void) {
|
||||||
// Read the first sector
|
// ELF header buffer
|
||||||
ata_lba_read(KERN_START_SECT, 1, read_buf);
|
uint8_t header_buf[SECTOR_SIZE];
|
||||||
|
|
||||||
const Elf32_Ehdr* header = (const Elf32_Ehdr*)read_buf;
|
// Read the first sector (contains the ELF header)
|
||||||
|
ata_read_sector(header_buf, KERN_START_SECT);
|
||||||
|
|
||||||
// Remaining data size, subtract the first 512B already read
|
// `elf_load()` returns the entry point
|
||||||
uint32_t rem = total_header_size(header) - 512;
|
return elf_load(header_buf);
|
||||||
|
|
||||||
// Read the rest if necessary
|
|
||||||
if (rem)
|
|
||||||
read_sectors(read_buf+512, 512, rem);
|
|
||||||
|
|
||||||
elf_load(read_buf, read_sectors);
|
|
||||||
|
|
||||||
return (void *)header->e_entry;
|
|
||||||
}
|
}
|
||||||
|
|||||||
119
kernel/ata.c
Normal file
119
kernel/ata.c
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
#include "ata.h"
|
||||||
|
#include "io.h"
|
||||||
|
#include "print.h"
|
||||||
|
|
||||||
|
#define ATA_TIMEOUT 100000
|
||||||
|
|
||||||
|
static inline void ata_delay(void) {
|
||||||
|
/* 400ns delay by reading alternate status */
|
||||||
|
inb(ATA_PRIMARY_CTRL);
|
||||||
|
inb(ATA_PRIMARY_CTRL);
|
||||||
|
inb(ATA_PRIMARY_CTRL);
|
||||||
|
inb(ATA_PRIMARY_CTRL);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ata_wait_ready(void) {
|
||||||
|
for (int i = 0; i < ATA_TIMEOUT; i++) {
|
||||||
|
uint8_t status = inb(ATA_PRIMARY_IO + ATA_REG_STATUS);
|
||||||
|
/* Must NOT be busy AND must be ready */
|
||||||
|
if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRDY))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool ata_wait(uint8_t mask) {
|
||||||
|
for (int i = 0; i < ATA_TIMEOUT; i++) {
|
||||||
|
uint8_t status = inb(ATA_PRIMARY_IO + ATA_REG_STATUS);
|
||||||
|
/* If ERR is set, stop waiting and return failure */
|
||||||
|
if (status & ATA_SR_ERR) return false;
|
||||||
|
|
||||||
|
if (!(status & ATA_SR_BSY) && (status & mask))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ata_init(void) {
|
||||||
|
/* Select drive */
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_HDDEVSEL, ATA_MASTER);
|
||||||
|
ata_delay();
|
||||||
|
|
||||||
|
/* Check if drive exists */
|
||||||
|
uint8_t status = inb(ATA_PRIMARY_IO + ATA_REG_STATUS);
|
||||||
|
if (status == 0xFF || status == 0) return false;
|
||||||
|
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY);
|
||||||
|
ata_delay();
|
||||||
|
|
||||||
|
if (!ata_wait(ATA_SR_DRQ))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
uint16_t identify[256];
|
||||||
|
for (int i = 0; i < 256; i++)
|
||||||
|
identify[i] = inw(ATA_PRIMARY_IO);
|
||||||
|
|
||||||
|
print_string("[ATA] Primary master detected\n");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ata_read_sector(uint32_t lba, uint8_t* buffer) {
|
||||||
|
if (!buffer) return false;
|
||||||
|
|
||||||
|
/* 1. Wait for drive to be ready for command */
|
||||||
|
if (!ata_wait_ready()) return false;
|
||||||
|
|
||||||
|
/* 2. Setup Task File (LBA28) */
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_HDDEVSEL, 0xE0 | ((lba >> 24) & 0x0F));
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_SECCOUNT0, 1);
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_LBA0, (uint8_t)(lba));
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_LBA1, (uint8_t)(lba >> 8));
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_LBA2, (uint8_t)(lba >> 16));
|
||||||
|
|
||||||
|
/* 3. Issue Read Command */
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO);
|
||||||
|
|
||||||
|
/* 4. Wait for Data Request (DRQ) */
|
||||||
|
if (!ata_wait(ATA_SR_DRQ))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
/* 5. Transfer data */
|
||||||
|
for (int i = 0; i < 256; i++) {
|
||||||
|
uint16_t data = inw(ATA_PRIMARY_IO);
|
||||||
|
buffer[i * 2] = data & 0xFF;
|
||||||
|
buffer[i * 2 + 1] = (data >> 8) & 0xFF;
|
||||||
|
}
|
||||||
|
|
||||||
|
ata_delay();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ata_write_sector(uint32_t lba, const uint8_t* buffer) {
|
||||||
|
if (!buffer) return false;
|
||||||
|
|
||||||
|
/* 1. Wait for drive to be ready for command */
|
||||||
|
if (!ata_wait_ready()) return false;
|
||||||
|
|
||||||
|
/* 2. Setup Task File */
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_HDDEVSEL, 0xE0 | ((lba >> 24) & 0x0F));
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_SECCOUNT0, 1);
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_LBA0, (uint8_t)(lba));
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_LBA1, (uint8_t)(lba >> 8));
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_LBA2, (uint8_t)(lba >> 16));
|
||||||
|
|
||||||
|
/* 3. Issue Write Command */
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO);
|
||||||
|
|
||||||
|
/* 4. Wait for drive to request data */
|
||||||
|
if (!ata_wait(ATA_SR_DRQ))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
/* 5. Transfer data */
|
||||||
|
for (int i = 0; i < 256; i++) {
|
||||||
|
uint16_t word = buffer[i * 2] | (buffer[i * 2 + 1] << 8);
|
||||||
|
outw(ATA_PRIMARY_IO, word);
|
||||||
|
}
|
||||||
|
|
||||||
|
ata_delay();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
44
kernel/ata.h
Normal file
44
kernel/ata.h
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#ifndef ATA_H
|
||||||
|
#define ATA_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
/* ATA I/O ports */
|
||||||
|
#define ATA_PRIMARY_IO 0x1F0
|
||||||
|
#define ATA_PRIMARY_CTRL 0x3F6
|
||||||
|
|
||||||
|
/* ATA registers */
|
||||||
|
#define ATA_REG_DATA 0x00
|
||||||
|
#define ATA_REG_ERROR 0x01
|
||||||
|
#define ATA_REG_FEATURES 0x01
|
||||||
|
#define ATA_REG_SECCOUNT0 0x02
|
||||||
|
#define ATA_REG_LBA0 0x03
|
||||||
|
#define ATA_REG_LBA1 0x04
|
||||||
|
#define ATA_REG_LBA2 0x05
|
||||||
|
#define ATA_REG_HDDEVSEL 0x06
|
||||||
|
#define ATA_REG_COMMAND 0x07
|
||||||
|
#define ATA_REG_STATUS 0x07
|
||||||
|
|
||||||
|
/* ATA commands */
|
||||||
|
#define ATA_CMD_READ_PIO 0x20
|
||||||
|
#define ATA_CMD_WRITE_PIO 0x30
|
||||||
|
#define ATA_CMD_IDENTIFY 0xEC
|
||||||
|
|
||||||
|
/* Status flags */
|
||||||
|
#define ATA_SR_BSY 0x80
|
||||||
|
#define ATA_SR_DRDY 0x40
|
||||||
|
#define ATA_SR_DRQ 0x08
|
||||||
|
#define ATA_SR_ERR 0x01
|
||||||
|
|
||||||
|
/* Drive select */
|
||||||
|
#define ATA_MASTER 0xA0
|
||||||
|
#define ATA_SLAVE 0xB0
|
||||||
|
|
||||||
|
/* Public API */
|
||||||
|
bool ata_init(void);
|
||||||
|
bool ata_read_sector(uint32_t lba, uint8_t* buffer);
|
||||||
|
bool ata_write_sector(uint32_t lba, const uint8_t* buffer);
|
||||||
|
bool ata_wait_ready(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
92
kernel/cpu.c
92
kernel/cpu.c
@@ -2,36 +2,106 @@
|
|||||||
#include "serial.h"
|
#include "serial.h"
|
||||||
#include "terminal.h"
|
#include "terminal.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "print.h"
|
|
||||||
|
|
||||||
void cpuid(uint32_t function, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx) {
|
void cpuid(uint32_t leaf, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx) {
|
||||||
__asm__(
|
__asm__(
|
||||||
"cpuid"
|
"cpuid"
|
||||||
: "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx)
|
: "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx)
|
||||||
: "a"(function)
|
: "a"(leaf)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper to print a labeled decimal value
|
||||||
|
void print_val(const char* label, uint32_t val) {
|
||||||
|
char buf[12];
|
||||||
|
utoa(val, buf, 10);
|
||||||
|
terminal_write(label);
|
||||||
|
terminal_write(buf);
|
||||||
|
terminal_write(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Safely check if CPUID is supported by attempting to flip bit 21 of EFLAGS
|
||||||
|
int check_cpuid_supported() {
|
||||||
|
uint32_t f1, f2;
|
||||||
|
__asm__ volatile (
|
||||||
|
"pushfl\n\t"
|
||||||
|
"pushfl\n\t"
|
||||||
|
"popl %0\n\t"
|
||||||
|
"movl %0, %1\n\t"
|
||||||
|
"xorl $0x200000, %0\n\t"
|
||||||
|
"pushl %0\n\t"
|
||||||
|
"popfl\n\t"
|
||||||
|
"pushfl\n\t"
|
||||||
|
"popl %0\n\t"
|
||||||
|
"popfl\n\t"
|
||||||
|
: "=&r" (f1), "=&r" (f2));
|
||||||
|
return ((f1 ^ f2) & 0x200000) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
void identify_cpu() {
|
void identify_cpu() {
|
||||||
|
if (!check_cpuid_supported()) {
|
||||||
|
terminal_write("CPUID not supported. Likely a 386 or early 486.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t eax, ebx, ecx, edx;
|
uint32_t eax, ebx, ecx, edx;
|
||||||
char vendor[13];
|
char vendor[13];
|
||||||
|
|
||||||
|
// Leaf 0: Vendor String & Max Leaf
|
||||||
cpuid(0, &eax, &ebx, &ecx, &edx);
|
cpuid(0, &eax, &ebx, &ecx, &edx);
|
||||||
|
uint32_t max_leaf = eax;
|
||||||
*(uint32_t *)&vendor[0] = ebx;
|
*(uint32_t *)&vendor[0] = ebx;
|
||||||
*(uint32_t *)&vendor[4] = edx;
|
*(uint32_t *)&vendor[4] = edx;
|
||||||
*(uint32_t *)&vendor[8] = ecx;
|
*(uint32_t *)&vendor[8] = ecx;
|
||||||
vendor[12] = '\0';
|
vendor[12] = '\0';
|
||||||
|
|
||||||
terminal_write("CPU Vendor: ");
|
terminal_write("Vendor: ");
|
||||||
terminal_write(vendor);
|
terminal_write(vendor);
|
||||||
terminal_write("\n");
|
terminal_write("\n");
|
||||||
|
|
||||||
serial_write("CPU Vendor: ");
|
// Leaf 1: Family, Model, Stepping
|
||||||
serial_write(vendor);
|
if (max_leaf >= 1) {
|
||||||
serial_write("\n");
|
cpuid(1, &eax, &ebx, &ecx, &edx);
|
||||||
|
|
||||||
|
uint32_t stepping = eax & 0xF;
|
||||||
|
uint32_t model = (eax >> 4) & 0xF;
|
||||||
|
uint32_t family = (eax >> 8) & 0xF;
|
||||||
|
uint32_t type = (eax >> 12) & 0x3;
|
||||||
|
|
||||||
terminal_write("CPUID max leaf: ");
|
// Handle Extended Family/Model (Required for Pentium 4 and newer)
|
||||||
print_hex(eax, false, false); // You must implement this (see below)
|
if (family == 0xF) {
|
||||||
terminal_write("\n");
|
family += (eax >> 20) & 0xFF;
|
||||||
|
model += ((eax >> 16) & 0xF) << 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
print_val("Family:", family);
|
||||||
|
print_val("Model:", model);
|
||||||
|
print_val("Step:", stepping);
|
||||||
|
terminal_write("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Leaf 2: Cache Descriptors
|
||||||
|
if (max_leaf >= 2) {
|
||||||
|
cpuid(2, &eax, &ebx, &ecx, &edx);
|
||||||
|
|
||||||
|
terminal_write("Cache Descriptors: ");
|
||||||
|
// Note: Leaf 2 returns a list of 1-byte descriptors in the registers.
|
||||||
|
// We look for common Intel ones:
|
||||||
|
uint32_t regs[4] = {eax, ebx, ecx, edx};
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
if (regs[i] & 0x80000000) continue; // Reserved bit
|
||||||
|
for (int j = 0; j < 4; j++) {
|
||||||
|
uint8_t desc = (regs[i] >> (j * 8)) & 0xFF;
|
||||||
|
if (desc == 0) continue;
|
||||||
|
|
||||||
|
// Example decoding for specific chips you mentioned:
|
||||||
|
if (desc == 0x06) terminal_write("8KB L1 I-Cache ");
|
||||||
|
if (desc == 0x0A) terminal_write("8KB L1 D-Cache ");
|
||||||
|
if (desc == 0x41) terminal_write("128KB L2 ");
|
||||||
|
if (desc == 0x43) terminal_write("512KB L2 ");
|
||||||
|
if (desc == 0x2C) terminal_write("32KB L1 D-Cache ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
terminal_write("\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
36
kernel/cpu.h
36
kernel/cpu.h
@@ -2,8 +2,42 @@
|
|||||||
#define CPU_H
|
#define CPU_H
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
void cpuid(uint32_t function, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx);
|
// Specific Intel Model Definitions for your targets
|
||||||
|
#define INTEL_FAM4_486_DX 0x00 // Also 0x01
|
||||||
|
#define INTEL_FAM4_486_SX 0x02
|
||||||
|
#define INTEL_FAM4_486_DX2 0x03
|
||||||
|
#define INTEL_FAM4_486_DX4 0x08
|
||||||
|
#define INTEL_FAM5_PENTIUM 0x01 // P5
|
||||||
|
#define INTEL_FAM5_PENTIUM_MMX 0x04 // P55C
|
||||||
|
#define INTEL_FAM6_PENTIUM_PRO 0x01 // P6
|
||||||
|
#define INTEL_FAM6_PENTIUM_II 0x05 // Deschutes
|
||||||
|
#define INTEL_FAM6_PENTIUM_III 0x07 // Katmai/Coppermine
|
||||||
|
#define INTEL_FAM15_P4_WILLY 0x00 // Willamette
|
||||||
|
#define INTEL_FAM15_P4_NORTH 0x02 // Northwood
|
||||||
|
#define INTEL_FAM15_P4_PRES 0x03 // Prescott
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char vendor[13];
|
||||||
|
uint32_t family;
|
||||||
|
uint32_t model;
|
||||||
|
uint32_t stepping;
|
||||||
|
uint32_t type;
|
||||||
|
uint32_t max_leaf;
|
||||||
|
|
||||||
|
// Feature flags (optional, but very helpful later)
|
||||||
|
bool has_fpu;
|
||||||
|
bool has_mmx;
|
||||||
|
bool has_sse;
|
||||||
|
} cpu_info_t;
|
||||||
|
|
||||||
|
// Function Prototypes
|
||||||
|
void cpuid(uint32_t leaf, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx);
|
||||||
|
bool cpu_check_cpuid_support(void);
|
||||||
void identify_cpu(void);
|
void identify_cpu(void);
|
||||||
|
|
||||||
|
// Helper to get the current CPU info after identification
|
||||||
|
cpu_info_t* cpu_get_info(void);
|
||||||
|
|
||||||
#endif // CPU_H
|
#endif // CPU_H
|
||||||
|
|||||||
@@ -1,36 +1,80 @@
|
|||||||
|
#include <string.h>
|
||||||
#include "display.h"
|
#include "display.h"
|
||||||
#include "io.h" // Include your I/O header for port access
|
#include "io.h"
|
||||||
#include "vga.h"
|
#include "vga.h"
|
||||||
|
|
||||||
// Initialize the display
|
// Initialize the display
|
||||||
void init_display(void) {
|
void init_display(void) {
|
||||||
// Initialize VGA settings, if necessary
|
// Initialize the VGA driver. This typically sets up the 80x25 text mode,
|
||||||
// This could involve setting up the VGA mode, etc.
|
// clears the screen, and sets the cursor.
|
||||||
set_display_mode(0x13); // Example: Set to 320x200 256-color mode
|
vga_init();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enumerate connected displays
|
// Enumerate connected displays
|
||||||
void enumerate_displays(void) {
|
void enumerate_displays(void) {
|
||||||
// This is a simplified example. Actual enumeration may require
|
// This function is often a complex operation in a real driver.
|
||||||
// reading from specific VGA registers or using BIOS interrupts.
|
// In this simplified kernel/VGA text mode environment, we use printf
|
||||||
|
// to output a message and rely on the fact that VGA is present.
|
||||||
|
|
||||||
// For demonstration, we will just print a message
|
// Clear the display before printing a message
|
||||||
// In a real driver, you would check the VGA registers
|
vga_clear(vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK));
|
||||||
// to determine connected displays.
|
|
||||||
clear_display();
|
// Output a simplified enumeration message
|
||||||
// Here you would typically read from VGA registers to find connected displays
|
vga_printf("Display: Standard VGA Text Mode (80x25) Detected.\n");
|
||||||
// For example, using inb() to read from VGA ports
|
|
||||||
|
// In a real driver, you would use inb() and outb() with specific VGA ports
|
||||||
|
// to read information (e.g., from the CRTC registers 0x3D4/0x3D5)
|
||||||
|
// to check for display presence or configuration.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the display mode
|
// Set the display mode
|
||||||
|
// NOTE: Setting arbitrary VGA modes (like 0x13 for 320x200) is very complex
|
||||||
|
// and requires writing hundreds of register values, often done via BIOS in
|
||||||
|
// real mode. Since we are in protected mode and have a simple text driver,
|
||||||
|
// this function is kept simple or treated as a placeholder for full mode changes.
|
||||||
void set_display_mode(uint8_t mode) {
|
void set_display_mode(uint8_t mode) {
|
||||||
// Set the VGA mode by writing to the appropriate registers
|
// Check if the requested mode is a known mode (e.g., VGA Text Mode 3)
|
||||||
outb(VGA_PORT, mode); // Example function to write to a port
|
// For this example, we simply acknowledge the call.
|
||||||
|
// A true mode set would involve complex register sequencing.
|
||||||
|
|
||||||
|
// The provided vga.c is a Text Mode driver, so a graphical mode set
|
||||||
|
// like 0x13 (320x200 256-color) would break the existing vga_printf functionality.
|
||||||
|
|
||||||
|
// A simplified text-mode-specific response:
|
||||||
|
if (mode == 0x03) { // Mode 3 is standard 80x25 text mode
|
||||||
|
vga_printf("Display mode set to 80x25 Text Mode (Mode 0x03).\n");
|
||||||
|
vga_init(); // Re-initialize the text mode
|
||||||
|
} else {
|
||||||
|
// Simple I/O example based on the original structure (Caution: Incomplete for full mode set)
|
||||||
|
outb(VGA_PORT, mode); // Example function to write to a port
|
||||||
|
vga_printf("Attempting to set display mode to 0x%x. (Warning: May break current display)\n", mode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear the display
|
// Clear the display
|
||||||
void clear_display(void) {
|
void clear_display(void) {
|
||||||
// Clear the display by filling it with a color
|
// Use the VGA driver's clear function, typically clearing to black on light grey
|
||||||
// This is a placeholder for actual clearing logic
|
// or black on black. We'll use the black on light grey from vga_init for consistency.
|
||||||
// You would typically write to video memory here
|
vga_clear(vga_entry_color(VGA_COLOR_BLACK, VGA_COLOR_LIGHT_GREY));
|
||||||
|
// Reset cursor to 0, 0
|
||||||
|
vga_set_cursor_position(0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to write a string
|
||||||
|
void display_write_string(const char* str) {
|
||||||
|
// Use the VGA driver's string writing function
|
||||||
|
vga_write_string(str, strlen(str));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to print a formatted string
|
||||||
|
void display_printf(const char* format, ...) {
|
||||||
|
// Use the VGA driver's printf function
|
||||||
|
va_list args;
|
||||||
|
va_start(args, format);
|
||||||
|
|
||||||
|
// The vga_printf function already handles the va_list internally,
|
||||||
|
// so we can just call it directly.
|
||||||
|
vga_printf(format, args);
|
||||||
|
|
||||||
|
va_end(args);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,13 +2,21 @@
|
|||||||
#define DISPLAY_H
|
#define DISPLAY_H
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include "vga.h" // Include VGA functions
|
||||||
|
|
||||||
#define VGA_PORT 0x3C0 // Base port for VGA
|
#define VGA_PORT 0x3C0 // Base port for VGA (Often used for general control, though 0x3D4/0x3D5 are used for cursor)
|
||||||
|
|
||||||
// Function prototypes
|
// Function prototypes
|
||||||
void init_display(void);
|
void init_display(void);
|
||||||
void enumerate_displays(void);
|
void enumerate_displays(void);
|
||||||
void set_display_mode(uint8_t mode);
|
void set_display_mode(uint8_t mode); // In this context, modes are typically BIOS or VESA modes, which are complex.
|
||||||
|
// We'll treat this as a placeholder/simple mode call.
|
||||||
void clear_display(void);
|
void clear_display(void);
|
||||||
|
|
||||||
|
// New function to write a string using the VGA driver
|
||||||
|
void display_write_string(const char* str);
|
||||||
|
|
||||||
|
// New function to print a formatted string using the VGA driver
|
||||||
|
void display_printf(const char* format, ...);
|
||||||
|
|
||||||
#endif // DISPLAY_H
|
#endif // DISPLAY_H
|
||||||
|
|||||||
@@ -15,9 +15,16 @@ static uint8_t g_sector_buffer[FAT12_SECTOR_SIZE];
|
|||||||
static int k_memcmp(const void *s1, const void *s2, uint32_t n) {
|
static int k_memcmp(const void *s1, const void *s2, uint32_t n) {
|
||||||
const uint8_t *p1 = (const uint8_t *)s1;
|
const uint8_t *p1 = (const uint8_t *)s1;
|
||||||
const uint8_t *p2 = (const uint8_t *)s2;
|
const uint8_t *p2 = (const uint8_t *)s2;
|
||||||
|
|
||||||
for (uint32_t i = 0; i < n; i++) {
|
for (uint32_t i = 0; i < n; i++) {
|
||||||
if (p1[i] != p2[i]) return p1[i] - p2[i];
|
if (p1[i] != p2[i]) {
|
||||||
|
// Correct way to return the difference:
|
||||||
|
// If p1[i] > p2[i], returns positive.
|
||||||
|
// If p1[i] < p2[i], returns negative.
|
||||||
|
return (int)p1[i] - (int)p2[i];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,3 +189,8 @@ uint32_t fat12_read(file_t *file, uint8_t *buffer, uint32_t bytes_to_read) {
|
|||||||
|
|
||||||
return total_read;
|
return total_read;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int disk_read_sector(uint32_t lba, uint8_t *buffer) {
|
||||||
|
// For now, do nothing and return success
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ typedef struct {
|
|||||||
|
|
||||||
// You must implement this in your disk driver (e.g., floppy.c)
|
// You must implement this in your disk driver (e.g., floppy.c)
|
||||||
// Returns 0 on success, non-zero on error.
|
// Returns 0 on success, non-zero on error.
|
||||||
extern int disk_read_sector(uint32_t lba, uint8_t *buffer);
|
int disk_read_sector(uint32_t lba, uint8_t *buffer);
|
||||||
|
|
||||||
void fat12_init();
|
void fat12_init();
|
||||||
file_t fat12_open(const char *filename);
|
file_t fat12_open(const char *filename);
|
||||||
|
|||||||
66
kernel/gui.c
66
kernel/gui.c
@@ -0,0 +1,66 @@
|
|||||||
|
#include "gui.h"
|
||||||
|
#include "vga.h" // VGA functions for drawing and clearing screen
|
||||||
|
#include "framebuffer.h" // For pixel manipulation if needed
|
||||||
|
|
||||||
|
// Initialize the GUI (could set up any global state or variables here)
|
||||||
|
void gui_init(void) {
|
||||||
|
// Clear the screen with black or any color
|
||||||
|
gui_clear(vga_entry_color(VGA_COLOR_BLACK, VGA_COLOR_WHITE));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw a window (simple rectangle with a title)
|
||||||
|
void gui_draw_window(gui_window_t* window) {
|
||||||
|
// Draw the window's border
|
||||||
|
for (uint32_t y = 0; y < window->height; ++y) {
|
||||||
|
for (uint32_t x = 0; x < window->width; ++x) {
|
||||||
|
// Check if we are at the border
|
||||||
|
if (x == 0 || y == 0 || x == window->width - 1 || y == window->height - 1) {
|
||||||
|
vga_put_entry_at('#', vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK), window->x + x, window->y + y);
|
||||||
|
} else {
|
||||||
|
// Fill the inside of the window
|
||||||
|
vga_put_entry_at(' ', vga_entry_color(VGA_COLOR_BLACK, VGA_COLOR_BLACK), window->x + x, window->y + y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw the title at the top
|
||||||
|
if (window->title) {
|
||||||
|
size_t i = 0;
|
||||||
|
while (window->title[i] != '\0' && i < window->width - 2) {
|
||||||
|
vga_put_entry_at(window->title[i], vga_entry_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK), window->x + i + 1, window->y);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw a button (a simple rectangle with text in the middle)
|
||||||
|
void gui_draw_button(gui_button_t* button) {
|
||||||
|
for (uint32_t y = 0; y < button->height; ++y) {
|
||||||
|
for (uint32_t x = 0; x < button->width; ++x) {
|
||||||
|
// Check if we are at the border
|
||||||
|
if (x == 0 || y == 0 || x == button->width - 1 || y == button->height - 1) {
|
||||||
|
vga_put_entry_at('#', vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK), button->x + x, button->y + y);
|
||||||
|
} else {
|
||||||
|
// Fill the inside of the button
|
||||||
|
vga_put_entry_at(' ', vga_entry_color(VGA_COLOR_BLACK, VGA_COLOR_BLACK), button->x + x, button->y + y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw the label in the center of the button
|
||||||
|
size_t label_len = 0;
|
||||||
|
while (button->label[label_len] != '\0') {
|
||||||
|
label_len++;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t start_x = button->x + (button->width - label_len) / 2;
|
||||||
|
size_t start_y = button->y + (button->height - 1) / 2;
|
||||||
|
for (size_t i = 0; i < label_len; ++i) {
|
||||||
|
vga_put_entry_at(button->label[i], vga_entry_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK), start_x + i, start_y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear the screen with a color
|
||||||
|
void gui_clear(uint32_t color) {
|
||||||
|
vga_clear(color); // Just clear the VGA screen with a solid color
|
||||||
|
}
|
||||||
|
|||||||
34
kernel/gui.h
34
kernel/gui.h
@@ -0,0 +1,34 @@
|
|||||||
|
#ifndef GUI_H
|
||||||
|
#define GUI_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#define GUI_WINDOW_WIDTH 80
|
||||||
|
#define GUI_WINDOW_HEIGHT 25
|
||||||
|
#define GUI_BUTTON_WIDTH 10
|
||||||
|
#define GUI_BUTTON_HEIGHT 3
|
||||||
|
|
||||||
|
// Window structure
|
||||||
|
typedef struct {
|
||||||
|
uint32_t x, y;
|
||||||
|
uint32_t width, height;
|
||||||
|
uint32_t color; // Background color
|
||||||
|
const char* title;
|
||||||
|
} gui_window_t;
|
||||||
|
|
||||||
|
// Button structure
|
||||||
|
typedef struct {
|
||||||
|
uint32_t x, y;
|
||||||
|
uint32_t width, height;
|
||||||
|
uint32_t color; // Background color
|
||||||
|
const char* label;
|
||||||
|
} gui_button_t;
|
||||||
|
|
||||||
|
// Function prototypes for GUI elements
|
||||||
|
void gui_init(void);
|
||||||
|
void gui_draw_window(gui_window_t* window);
|
||||||
|
void gui_draw_button(gui_button_t* button);
|
||||||
|
void gui_clear(uint32_t color);
|
||||||
|
|
||||||
|
#endif // GUI_H
|
||||||
|
|||||||
65
kernel/hid.c
Normal file
65
kernel/hid.c
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
#include "hid.h"
|
||||||
|
#include "usb.h"
|
||||||
|
#include "mouse.h"
|
||||||
|
#include "keyboard.h"
|
||||||
|
#include "print.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
// Global variables
|
||||||
|
static bool hid_initialized = false;
|
||||||
|
|
||||||
|
void hid_init(void) {
|
||||||
|
if (hid_initialized) return;
|
||||||
|
hid_initialized = true;
|
||||||
|
|
||||||
|
// Initialize keyboard and mouse HID handling
|
||||||
|
keyboard_init();
|
||||||
|
// Assume USB mouse has been initialized and is connected.
|
||||||
|
usb_hid_init(); // Initializes USB HID for both keyboard and mouse
|
||||||
|
}
|
||||||
|
|
||||||
|
void hid_process_report(uint8_t* report, uint8_t length) {
|
||||||
|
// Process the HID report based on its type
|
||||||
|
if (length == 8) { // Assuming a standard 8-byte report for HID keyboard
|
||||||
|
keyboard_hid_report_t* k_report = (keyboard_hid_report_t*) report;
|
||||||
|
hid_process_keyboard_report(k_report);
|
||||||
|
} else if (length == 3) { // Assuming a standard 3-byte report for HID mouse
|
||||||
|
mouse_hid_report_t* m_report = (mouse_hid_report_t*) report;
|
||||||
|
hid_process_mouse_report(m_report);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle HID keyboard report
|
||||||
|
void hid_process_keyboard_report(const keyboard_hid_report_t* report) {
|
||||||
|
// Iterate over the keycodes and process key presses
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
uint8_t keycode = report->keycodes[i];
|
||||||
|
if (keycode != 0) {
|
||||||
|
char key = scancode_map[keycode];
|
||||||
|
if (key) {
|
||||||
|
keyboard_buffer_add(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle HID mouse report
|
||||||
|
void hid_process_mouse_report(const mouse_hid_report_t* report) {
|
||||||
|
// Process mouse movement and button clicks
|
||||||
|
mouse_data.x += report->x;
|
||||||
|
mouse_data.y += report->y;
|
||||||
|
mouse_data.left_button = (report->buttons & 0x01) != 0;
|
||||||
|
mouse_data.right_button = (report->buttons & 0x02) != 0;
|
||||||
|
|
||||||
|
print_hex((uint32_t)mouse_data.x, 1, 1);
|
||||||
|
print_hex((uint32_t)mouse_data.y, 1, 1);
|
||||||
|
print_hex((uint32_t)report->buttons, 1, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the HID descriptor (for parsing USB HID device descriptors)
|
||||||
|
bool hid_parse_descriptor(uint8_t* descriptor, uint32_t length) {
|
||||||
|
// HID descriptors are defined in the USB HID specification, we'll need to parse them here.
|
||||||
|
// For now, just return true assuming we have a valid descriptor.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
46
kernel/hid.h
Normal file
46
kernel/hid.h
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
#ifndef HID_H
|
||||||
|
#define HID_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
// HID Report types
|
||||||
|
#define HID_REPORT_INPUT 0x01
|
||||||
|
#define HID_REPORT_OUTPUT 0x02
|
||||||
|
#define HID_REPORT_FEATURE 0x03
|
||||||
|
|
||||||
|
// HID usage page constants (USB HID)
|
||||||
|
#define HID_USAGE_PAGE_GENERIC 0x01
|
||||||
|
#define HID_USAGE_KEYBOARD 0x06
|
||||||
|
#define HID_USAGE_MOUSE 0x02
|
||||||
|
|
||||||
|
// HID keyboard and mouse data
|
||||||
|
typedef struct {
|
||||||
|
uint8_t modifier; // Modifier keys (shift, ctrl, alt, etc.)
|
||||||
|
uint8_t reserved; // Reserved byte
|
||||||
|
uint8_t keycodes[6]; // Keycodes for keys pressed
|
||||||
|
} keyboard_hid_report_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t buttons; // Mouse buttons (bitwise: 0x01 = left, 0x02 = right, 0x04 = middle)
|
||||||
|
int8_t x; // X axis movement
|
||||||
|
int8_t y; // Y axis movement
|
||||||
|
int8_t wheel; // Mouse wheel
|
||||||
|
} mouse_hid_report_t;
|
||||||
|
|
||||||
|
// Initialize the HID subsystem
|
||||||
|
void hid_init(void);
|
||||||
|
|
||||||
|
// Process an incoming HID report
|
||||||
|
void hid_process_report(uint8_t* report, uint8_t length);
|
||||||
|
|
||||||
|
// Process HID keyboard report
|
||||||
|
void hid_process_keyboard_report(const keyboard_hid_report_t* report);
|
||||||
|
|
||||||
|
// Process HID mouse report
|
||||||
|
void hid_process_mouse_report(const mouse_hid_report_t* report);
|
||||||
|
|
||||||
|
// USB HID report descriptor parsing
|
||||||
|
bool hid_parse_descriptor(uint8_t* descriptor, uint32_t length);
|
||||||
|
|
||||||
|
#endif // HID_H
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#ifndef IRQ_H
|
#ifndef IRQ_H
|
||||||
#define IRQ_H
|
#define IRQ_H
|
||||||
|
|
||||||
#include "types.h"
|
#include <stdint.h>
|
||||||
|
|
||||||
void irq_remap(void);
|
void irq_remap(void);
|
||||||
void irq_install(void);
|
void irq_install(void);
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#include <stdbool.h>
|
||||||
#include "terminal.h"
|
#include "terminal.h"
|
||||||
#include "serial.h"
|
#include "serial.h"
|
||||||
#include "isr.h"
|
#include "isr.h"
|
||||||
|
|||||||
@@ -2,64 +2,91 @@
|
|||||||
#include "io.h"
|
#include "io.h"
|
||||||
#include "isr.h"
|
#include "isr.h"
|
||||||
#include "terminal.h"
|
#include "terminal.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
#define KEYBOARD_DATA_PORT 0x60
|
#define KEYBOARD_DATA_PORT 0x60
|
||||||
#define KEY_BUFFER_SIZE 256
|
#define KEY_BUFFER_SIZE 256
|
||||||
|
|
||||||
static char key_buffer[KEY_BUFFER_SIZE];
|
// Use volatile so the compiler knows these change inside interrupts
|
||||||
static uint8_t buffer_head = 0; // Write position (interrupt)
|
static volatile char key_buffer[KEY_BUFFER_SIZE];
|
||||||
static uint8_t buffer_tail = 0; // Read position (get_char)
|
static volatile uint8_t buffer_head = 0;
|
||||||
static uint8_t buffer_count = 0;
|
static volatile uint8_t buffer_tail = 0;
|
||||||
static uint8_t buffer_index = 0;
|
static volatile uint8_t buffer_count = 0;
|
||||||
|
|
||||||
// Basic US QWERTY keymap (scancode to ASCII)
|
// Exported map: Removed 'static' so hid.c can reference it if needed
|
||||||
static const char scancode_map[128] = {
|
const char scancode_map[128] = {
|
||||||
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', // 0x00 - 0x09
|
0, 27, '1', '2', '3', '4', '5', '6', '7', '8',
|
||||||
'9', '0', '-', '=', '\b', '\t', 'q', 'w', 'e', 'r', // 0x0A - 0x13
|
'9', '0', '-', '=', '\b', '\t', 'q', 'w', 'e', 'r',
|
||||||
't', 'y', 'z', 'u', 'i', 'o', 'p', '[', ']', '\n', // 0x14 - 0x1D
|
't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', 0,
|
||||||
0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', // 0x1E - 0x27
|
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
|
||||||
';', '\'', '`', 0, '\\', 'x', 'c', 'v', 'b', // 0x28 - 0x31
|
'\'', '`', 0, '\\', 'z', 'x', 'c', 'v', 'b', 'n',
|
||||||
'n', 'm', ',', '.', '/', 0, '*', 0, ' ', 0, // 0x32 - 0x3B
|
'm', ',', '.', '/', 0, '*', 0, ' ', 0
|
||||||
// rest can be filled as needed
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Interrupt handler for IRQ1
|
/**
|
||||||
void keyboard_callback(void) {
|
* Shared function used by both PS/2 (callback) and USB (hid.c)
|
||||||
uint8_t scancode = inb(KEYBOARD_DATA_PORT);
|
* This fixes the "undefined reference to keyboard_buffer_add" error.
|
||||||
|
*/
|
||||||
if (scancode & 0x80) return; // Ignore key release
|
void keyboard_buffer_add(char c) {
|
||||||
|
|
||||||
char c = scancode_map[scancode];
|
|
||||||
if (!c) return;
|
if (!c) return;
|
||||||
|
|
||||||
uint8_t next_head = (buffer_head + 1) % KEY_BUFFER_SIZE;
|
uint8_t next_head = (buffer_head + 1) % KEY_BUFFER_SIZE;
|
||||||
|
|
||||||
// Drop key if buffer full
|
// If buffer is full, we must drop the key
|
||||||
if (next_head == buffer_tail) return;
|
if (next_head == buffer_tail) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
key_buffer[buffer_head] = c;
|
key_buffer[buffer_head] = c;
|
||||||
buffer_head = next_head;
|
buffer_head = next_head;
|
||||||
buffer_count++;
|
buffer_count++;
|
||||||
|
|
||||||
|
// Echo to terminal
|
||||||
terminal_putchar(c);
|
terminal_putchar(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
void keyboard_init() {
|
/**
|
||||||
register_interrupt_handler(33, keyboard_callback); // IRQ1 = int 33 (0x21)
|
* Hardware Interrupt Handler for PS/2
|
||||||
|
*/
|
||||||
|
void keyboard_callback(void) {
|
||||||
|
uint8_t scancode = inb(KEYBOARD_DATA_PORT);
|
||||||
|
|
||||||
|
// Ignore break codes (key release)
|
||||||
|
if (scancode & 0x80) return;
|
||||||
|
|
||||||
|
char c = scancode_map[scancode];
|
||||||
|
keyboard_buffer_add(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Blocking read (returns one char)
|
void keyboard_init(void) {
|
||||||
|
buffer_head = 0;
|
||||||
|
buffer_tail = 0;
|
||||||
|
buffer_count = 0;
|
||||||
|
// IRQ1 is usually mapped to IDT entry 33
|
||||||
|
register_interrupt_handler(33, keyboard_callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Blocking read with a safe HLT to prevent CPU 100% usage
|
||||||
|
*/
|
||||||
char keyboard_get_char(void) {
|
char keyboard_get_char(void) {
|
||||||
while (buffer_count == 0) {
|
|
||||||
__asm__ __volatile__("hlt"); // Better than busy loop
|
|
||||||
}
|
|
||||||
|
|
||||||
char c;
|
char c;
|
||||||
__asm__ __volatile__("cli");
|
|
||||||
c = key_buffer[buffer_tail];
|
|
||||||
buffer_tail = (buffer_tail + 1) % KEY_BUFFER_SIZE;
|
|
||||||
buffer_count--;
|
|
||||||
__asm__ __volatile__("sti");
|
|
||||||
|
|
||||||
return c;
|
while (1) {
|
||||||
|
__asm__ __volatile__("cli"); // Disable interrupts to check buffer_count safely
|
||||||
|
|
||||||
|
if (buffer_count > 0) {
|
||||||
|
c = key_buffer[buffer_tail];
|
||||||
|
buffer_tail = (buffer_tail + 1) % KEY_BUFFER_SIZE;
|
||||||
|
buffer_count--;
|
||||||
|
__asm__ __volatile__("sti"); // Re-enable interrupts after reading
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* * IMPORTANT: 'sti' followed by 'hlt' is guaranteed by x86
|
||||||
|
* to execute 'hlt' BEFORE the next interrupt can trigger.
|
||||||
|
* This prevents the race condition hang.
|
||||||
|
*/
|
||||||
|
__asm__ __volatile__("sti; hlt");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
#ifndef KEYBOARD_H
|
#ifndef KEYBOARD_H
|
||||||
#define KEYBOARD_H
|
#define KEYBOARD_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
void keyboard_init(void);
|
void keyboard_init(void);
|
||||||
char keyboard_get_char(void); // Blocking read from buffer
|
void keyboard_buffer_add(char c);
|
||||||
|
char keyboard_get_char(void);
|
||||||
|
|
||||||
|
extern const char scancode_map[128];
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,21 +1,18 @@
|
|||||||
#include "memmap.h"
|
#include "memmap.h"
|
||||||
|
|
||||||
|
#define BOOTLOADER_MEMMAP_COUNT_ADDR MEMMAP_BASE
|
||||||
|
#define BOOTLOADER_MEMMAP_ADDR (MEMMAP_BASE + 4)
|
||||||
|
|
||||||
uint32_t get_memory_map(memory_map_entry_t *map, uint32_t max_entries) {
|
uint32_t get_memory_map(memory_map_entry_t *map, uint32_t max_entries) {
|
||||||
|
// Read the number of entries found by the bootloader
|
||||||
|
uint32_t entries_found = *(uint32_t*)BOOTLOADER_MEMMAP_COUNT_ADDR;
|
||||||
|
memory_map_entry_t *bios_data = (memory_map_entry_t*)BOOTLOADER_MEMMAP_ADDR;
|
||||||
|
|
||||||
uint32_t count = 0;
|
uint32_t count = 0;
|
||||||
|
while (count < entries_found && count < max_entries) {
|
||||||
if (max_entries >= 1) {
|
map[count] = bios_data[count];
|
||||||
map[count].base_addr = 0x00000000;
|
|
||||||
map[count].length = 0x0009FC00;
|
|
||||||
map[count].type = 1;
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (max_entries >= 2) {
|
|
||||||
map[count].base_addr = 0x00100000;
|
|
||||||
map[count].length = 0x1FF00000;
|
|
||||||
map[count].type = 1;
|
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ typedef struct {
|
|||||||
uint64_t base_addr;
|
uint64_t base_addr;
|
||||||
uint64_t length;
|
uint64_t length;
|
||||||
uint32_t type;
|
uint32_t type;
|
||||||
|
uint32_t ext;
|
||||||
} __attribute__((packed)) memory_map_entry_t;
|
} __attribute__((packed)) memory_map_entry_t;
|
||||||
|
|
||||||
uint32_t get_memory_map(memory_map_entry_t *map, uint32_t max_entries);
|
uint32_t get_memory_map(memory_map_entry_t *map, uint32_t max_entries);
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
// Mouse buffer
|
// Mouse buffer
|
||||||
static mouse_data_t mouse_data;
|
mouse_data_t mouse_data;
|
||||||
|
|
||||||
// Read USB mouse data
|
// Read USB mouse data
|
||||||
mouse_data_t usb_read_mouse(void) {
|
mouse_data_t usb_read_mouse(void) {
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ typedef struct {
|
|||||||
bool right_button;
|
bool right_button;
|
||||||
} mouse_data_t;
|
} mouse_data_t;
|
||||||
|
|
||||||
|
extern mouse_data_t mouse_data;
|
||||||
|
|
||||||
// Function declarations for USB 1.x HID mouse support
|
// Function declarations for USB 1.x HID mouse support
|
||||||
bool usb_mouse_init(void);
|
bool usb_mouse_init(void);
|
||||||
bool usb_mouse_detected(void);
|
bool usb_mouse_detected(void);
|
||||||
|
|||||||
@@ -1,21 +1,17 @@
|
|||||||
#include "paging.h"
|
|
||||||
#include "io.h"
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stddef.h>
|
#include <string.h>
|
||||||
|
#include "io.h"
|
||||||
|
#include "paging.h"
|
||||||
|
|
||||||
page_directory_entry_t *page_directory = (page_directory_entry_t *)0x200000;
|
page_directory_entry_t *page_directory = (page_directory_entry_t *)0x200000;
|
||||||
page_table_entry_t *page_table = (page_table_entry_t *)0x201000;
|
page_table_entry_t *page_table = (page_table_entry_t *)0x201000;
|
||||||
page_table_entry_t *heap_page_table = (page_table_entry_t *)0x202000;
|
|
||||||
|
|
||||||
// Helper function to set up the page directory entry
|
// Helper function to set up the page directory entry
|
||||||
void set_page_directory(page_directory_entry_t *dir) {
|
void set_page_directory(page_directory_entry_t *dir) {
|
||||||
for (int i = 0; i < PAGE_DIRECTORY_SIZE; i++) {
|
// Set first PDE
|
||||||
dir[i].present = 0;
|
|
||||||
}
|
|
||||||
dir[0].present = 1;
|
dir[0].present = 1;
|
||||||
dir[0].rw = 1;
|
dir[0].rw = 1;
|
||||||
dir[0].user = 0;
|
dir[0].addr = (uint32_t)page_table >> 12;
|
||||||
dir[0].frame = (uint32_t)page_table >> 12;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to set up the page table entry
|
// Helper function to set up the page table entry
|
||||||
@@ -23,12 +19,8 @@ void set_page_table(page_table_entry_t *table) {
|
|||||||
for (int i = 0; i < PAGE_TABLE_SIZE; i++) {
|
for (int i = 0; i < PAGE_TABLE_SIZE; i++) {
|
||||||
// Set up page table entries with identity mapping
|
// Set up page table entries with identity mapping
|
||||||
table[i].present = 1;
|
table[i].present = 1;
|
||||||
table[i].rw = 1; // Read/Write
|
table[i].rw = 1; // Read/Write
|
||||||
table[i].user = 0; // Kernel mode
|
table[i].addr = i; // Identity mapping
|
||||||
table[i].write_through = 0;
|
|
||||||
table[i].cache_disabled = 0;
|
|
||||||
table[i].accessed = 0;
|
|
||||||
table[i].frame = i; // Identity mapping
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,26 +39,13 @@ void enable_paging() {
|
|||||||
|
|
||||||
// Initialize paging: set up the page directory and enable paging
|
// Initialize paging: set up the page directory and enable paging
|
||||||
void paging_init() {
|
void paging_init() {
|
||||||
|
// Zero out the tables
|
||||||
|
memset(page_directory, 0x00, PAGE_DIRECTORY_SIZE * sizeof *page_directory);
|
||||||
|
memset(page_table, 0x00, PAGE_TABLE_SIZE * sizeof *page_table);
|
||||||
|
|
||||||
// Set up identity-mapped page directory + table
|
// Set up identity-mapped page directory + table
|
||||||
set_page_directory(page_directory);
|
set_page_directory(page_directory);
|
||||||
set_page_table(page_table);
|
set_page_table(page_table);
|
||||||
|
|
||||||
// === Set up heap mapping at 0xC0100000 ===
|
|
||||||
for (int i = 0; i < PAGE_TABLE_SIZE; i++) {
|
|
||||||
heap_page_table[i].present = 1;
|
|
||||||
heap_page_table[i].rw = 1;
|
|
||||||
heap_page_table[i].user = 0;
|
|
||||||
heap_page_table[i].write_through = 0;
|
|
||||||
heap_page_table[i].cache_disabled = 0;
|
|
||||||
heap_page_table[i].accessed = 0;
|
|
||||||
heap_page_table[i].frame = (256 + i); // Start physical heap at 1MB (256*4KB = 1MB)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Index 772 = 0xC0100000 / 4MB
|
|
||||||
page_directory[772].present = 1;
|
|
||||||
page_directory[772].rw = 1;
|
|
||||||
page_directory[772].user = 0;
|
|
||||||
page_directory[772].frame = (uint32_t)heap_page_table >> 12;
|
|
||||||
|
|
||||||
enable_paging();
|
enable_paging();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,31 +10,31 @@
|
|||||||
|
|
||||||
// Page Directory and Page Table structure
|
// Page Directory and Page Table structure
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint32_t present : 1; // Present bit (1: page is present in memory)
|
uint32_t present : 1; // Present bit (1: page is present in memory)
|
||||||
uint32_t rw : 1; // Read-Write bit (1: page is read-write)
|
uint32_t rw : 1; // Read-Write bit (1: page is read-write)
|
||||||
uint32_t user : 1; // User-supervisor bit (1: user mode access)
|
uint32_t user : 1; // User-supervisor bit (1: user mode access)
|
||||||
uint32_t write_through : 1; // Write-through cache
|
uint32_t write_through : 1; // Write-through cache
|
||||||
uint32_t cache_disabled : 1; // Cache disabled
|
uint32_t cache_disabled : 1; // Cache disabled
|
||||||
uint32_t accessed : 1; // Accessed bit
|
uint32_t accessed : 1; // Accessed bit
|
||||||
uint32_t reserved : 1; // Reserved bit
|
uint32_t dirty : 1; // Dirty bit
|
||||||
uint32_t page_size : 1; // Page size (0: 4KB, 1: 4MB)
|
uint32_t attribute : 1; // Page size (0: 4KB, 1: 4MB)
|
||||||
uint32_t global : 1; // Global page (can be used across different processes)
|
uint32_t global : 1; // Global page (can be used across different processes)
|
||||||
uint32_t available : 3; // Available bits for the system
|
uint32_t reserved : 3; // Unused
|
||||||
uint32_t frame : 20; // Frame address (physical address)
|
uint32_t addr : 20; // Page frame address (physical address)
|
||||||
} __attribute__((packed)) page_table_entry_t;
|
} __attribute__((packed)) page_table_entry_t;
|
||||||
|
|
||||||
// Define page directory entry
|
// Define page directory entry
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint32_t present : 1;
|
uint32_t present : 1; // Present bit (1: PTE is present in memory)
|
||||||
uint32_t rw : 1;
|
uint32_t rw : 1; // Read-Write bit (1: pages are read-write)
|
||||||
uint32_t user : 1;
|
uint32_t user : 1; // User-supervisor bit (1: user mode access)
|
||||||
uint32_t write_through : 1;
|
uint32_t write_through : 1; // Write-through cache
|
||||||
uint32_t cache_disabled : 1;
|
uint32_t cache_disabled : 1; // Cache disabled
|
||||||
uint32_t accessed : 1;
|
uint32_t accessed : 1; // Accessed bit
|
||||||
uint32_t reserved : 1;
|
uint32_t available : 1; // Unused
|
||||||
uint32_t zero : 5; // Must be zero for page directory
|
uint32_t page_size : 1; // Page size (0: 4KB, 1: 4MB)
|
||||||
uint32_t reserved_2 : 7; // Reserved bits
|
uint32_t available_2 : 4; // Unused
|
||||||
uint32_t frame : 20; // Frame address of the page table
|
uint32_t addr : 20; // Page table address
|
||||||
} __attribute__((packed)) page_directory_entry_t;
|
} __attribute__((packed)) page_directory_entry_t;
|
||||||
|
|
||||||
extern page_directory_entry_t *page_directory;
|
extern page_directory_entry_t *page_directory;
|
||||||
|
|||||||
109
kernel/pci.c
Normal file
109
kernel/pci.c
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
#include "pci.h"
|
||||||
|
#include "io.h"
|
||||||
|
|
||||||
|
/* --- Configuration Access Functions --- */
|
||||||
|
|
||||||
|
uint32_t pci_config_read_dword(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) {
|
||||||
|
uint32_t address = (uint32_t)((uint32_t)1 << 31) |
|
||||||
|
((uint32_t)bus << 16) |
|
||||||
|
((uint32_t)slot << 11) |
|
||||||
|
((uint32_t)func << 8) |
|
||||||
|
(offset & 0xFC);
|
||||||
|
outl(PCI_CONFIG_ADDRESS, address);
|
||||||
|
return inl(PCI_CONFIG_DATA);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pci_config_write_dword(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint32_t data) {
|
||||||
|
uint32_t address = (uint32_t)((uint32_t)1 << 31) |
|
||||||
|
((uint32_t)bus << 16) |
|
||||||
|
((uint32_t)slot << 11) |
|
||||||
|
((uint32_t)func << 8) |
|
||||||
|
(offset & 0xFC);
|
||||||
|
outl(PCI_CONFIG_ADDRESS, address);
|
||||||
|
outl(PCI_CONFIG_DATA, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* To read a word or byte, we read the Dword and shift/mask */
|
||||||
|
uint16_t pci_config_read_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) {
|
||||||
|
uint32_t dword = pci_config_read_dword(bus, slot, func, offset);
|
||||||
|
return (uint16_t)((dword >> ((offset & 2) * 8)) & 0xFFFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t pci_config_read_byte(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) {
|
||||||
|
uint32_t dword = pci_config_read_dword(bus, slot, func, offset);
|
||||||
|
return (uint8_t)((dword >> ((offset & 3) * 8)) & 0xFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --- BAR Decoding Logic --- */
|
||||||
|
|
||||||
|
pci_bar_t pci_get_bar(uint8_t bus, uint8_t slot, uint8_t func, uint8_t bar_index) {
|
||||||
|
pci_bar_t bar = {0};
|
||||||
|
uint8_t offset = PCI_REG_BAR0 + (bar_index * 4);
|
||||||
|
|
||||||
|
uint32_t initial_val = pci_config_read_dword(bus, slot, func, offset);
|
||||||
|
|
||||||
|
// The Size Masking Trick
|
||||||
|
pci_config_write_dword(bus, slot, func, offset, 0xFFFFFFFF);
|
||||||
|
uint32_t mask = pci_config_read_dword(bus, slot, func, offset);
|
||||||
|
pci_config_write_dword(bus, slot, func, offset, initial_val); // Restore
|
||||||
|
|
||||||
|
if (initial_val & 0x1) {
|
||||||
|
// I/O Space BAR
|
||||||
|
bar.is_io = true;
|
||||||
|
bar.base_address = initial_val & 0xFFFFFFFC;
|
||||||
|
bar.size = ~(mask & 0xFFFFFFFC) + 1;
|
||||||
|
} else {
|
||||||
|
// Memory Space BAR
|
||||||
|
bar.is_io = false;
|
||||||
|
bar.base_address = initial_val & 0xFFFFFFF0;
|
||||||
|
bar.is_prefetchable = (initial_val & 0x8) != 0;
|
||||||
|
bar.size = ~(mask & 0xFFFFFFF0) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return bar;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --- Enumeration and Discovery --- */
|
||||||
|
|
||||||
|
void pci_check_function(uint8_t bus, uint8_t slot, uint8_t func) {
|
||||||
|
uint16_t vendor_id = pci_config_read_word(bus, slot, func, PCI_REG_VENDOR_ID);
|
||||||
|
if (vendor_id == 0xFFFF) return;
|
||||||
|
|
||||||
|
uint16_t device_id = pci_config_read_word(bus, slot, func, PCI_REG_DEVICE_ID);
|
||||||
|
uint8_t class_code = pci_config_read_byte(bus, slot, func, PCI_REG_CLASS);
|
||||||
|
|
||||||
|
/* Optional: Set Master Latency Timer if it is 0.
|
||||||
|
A value of 32 (0x20) or 64 (0x40) is typical.
|
||||||
|
*/
|
||||||
|
uint8_t latency = pci_config_read_byte(bus, slot, func, PCI_REG_LATENCY_TIMER);
|
||||||
|
if (latency == 0) {
|
||||||
|
// pci_config_write_byte would be needed here, or write a dword with the byte modified
|
||||||
|
uint32_t reg_0c = pci_config_read_dword(bus, slot, func, 0x0C);
|
||||||
|
reg_0c |= (0x20 << 8); // Set latency to 32
|
||||||
|
pci_config_write_dword(bus, slot, func, 0x0C, reg_0c);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace with your kernel's print/logging function
|
||||||
|
// printf("Found PCI Device: %x:%x Class: %x at %d:%d:%d\n", vendor_id, device_id, class_code, bus, slot, func);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pci_init(void) {
|
||||||
|
for (uint16_t bus = 0; bus < 256; bus++) {
|
||||||
|
for (uint8_t slot = 0; slot < 32; slot++) {
|
||||||
|
// Check Function 0 first
|
||||||
|
uint16_t vendor = pci_config_read_word(bus, slot, 0, PCI_REG_VENDOR_ID);
|
||||||
|
if (vendor == 0xFFFF) continue;
|
||||||
|
|
||||||
|
pci_check_function(bus, slot, 0);
|
||||||
|
|
||||||
|
// Check if this is a multi-function device
|
||||||
|
uint8_t header_type = pci_config_read_byte(bus, slot, 0, PCI_REG_HEADER_TYPE);
|
||||||
|
if (header_type & 0x80) {
|
||||||
|
// Check functions 1-7
|
||||||
|
for (uint8_t func = 1; func < 8; func++) {
|
||||||
|
pci_check_function(bus, slot, func);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
60
kernel/pci.h
Normal file
60
kernel/pci.h
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
#ifndef PCI_H
|
||||||
|
#define PCI_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
/* I/O Ports for PCI Configuration Mechanism #1 */
|
||||||
|
#define PCI_CONFIG_ADDRESS 0xCF8
|
||||||
|
#define PCI_CONFIG_DATA 0xCFC
|
||||||
|
|
||||||
|
/* Common PCI Configuration Register Offsets */
|
||||||
|
#define PCI_REG_VENDOR_ID 0x00
|
||||||
|
#define PCI_REG_DEVICE_ID 0x02
|
||||||
|
#define PCI_REG_COMMAND 0x04
|
||||||
|
#define PCI_REG_STATUS 0x06
|
||||||
|
#define PCI_REG_REVISION_ID 0x08
|
||||||
|
#define PCI_REG_PROG_IF 0x09
|
||||||
|
#define PCI_REG_SUBCLASS 0x0A
|
||||||
|
#define PCI_REG_CLASS 0x0B
|
||||||
|
#define PCI_REG_CACHE_LINE_SIZE 0x0C
|
||||||
|
#define PCI_REG_LATENCY_TIMER 0x0D
|
||||||
|
#define PCI_REG_HEADER_TYPE 0x0E
|
||||||
|
#define PCI_REG_BIST 0x0F
|
||||||
|
#define PCI_REG_BAR0 0x10
|
||||||
|
#define PCI_REG_BAR1 0x14
|
||||||
|
#define PCI_REG_BAR2 0x18
|
||||||
|
#define PCI_REG_BAR3 0x1C
|
||||||
|
#define PCI_REG_BAR4 0x20
|
||||||
|
#define PCI_REG_BAR5 0x24
|
||||||
|
#define PCI_REG_INTERRUPT_LINE 0x3C
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t base_address;
|
||||||
|
uint32_t size;
|
||||||
|
bool is_io;
|
||||||
|
bool is_prefetchable; // Only for Memory BARs
|
||||||
|
} pci_bar_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t bus;
|
||||||
|
uint8_t device;
|
||||||
|
uint8_t function;
|
||||||
|
uint16_t vendor_id;
|
||||||
|
uint16_t device_id;
|
||||||
|
uint8_t class_code;
|
||||||
|
uint8_t subclass;
|
||||||
|
uint8_t interrupt_line;
|
||||||
|
} pci_dev_t;
|
||||||
|
|
||||||
|
/* Function Prototypes */
|
||||||
|
uint32_t pci_config_read_dword(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset);
|
||||||
|
void pci_config_write_dword(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint32_t data);
|
||||||
|
|
||||||
|
uint16_t pci_config_read_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset);
|
||||||
|
uint8_t pci_config_read_byte(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset);
|
||||||
|
|
||||||
|
pci_bar_t pci_get_bar(uint8_t bus, uint8_t slot, uint8_t func, uint8_t bar_index);
|
||||||
|
void pci_init(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#ifndef PRINT_H
|
#ifndef PRINT_H
|
||||||
#define PRINT_H
|
#define PRINT_H
|
||||||
|
|
||||||
#include "types.h"
|
#include <stdint.h>
|
||||||
|
|
||||||
void print_string(const char *str);
|
void print_string(const char *str);
|
||||||
void my_printf(const char *format, ...);
|
void my_printf(const char *format, ...);
|
||||||
|
|||||||
107
kernel/ps2.c
Normal file
107
kernel/ps2.c
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
#include "ps2.h"
|
||||||
|
#include "io.h"
|
||||||
|
|
||||||
|
/* --- Controller Synchronization --- */
|
||||||
|
|
||||||
|
// Wait until the controller is ready to receive a byte
|
||||||
|
static void ps2_wait_write() {
|
||||||
|
while (inb(PS2_STATUS_REG) & PS2_STATUS_INPUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait until the controller has a byte for us to read
|
||||||
|
static void ps2_wait_read() {
|
||||||
|
while (!(inb(PS2_STATUS_REG) & PS2_STATUS_OUTPUT));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --- Initialization --- */
|
||||||
|
|
||||||
|
void ps2_write_device(uint8_t command) {
|
||||||
|
ps2_wait_write();
|
||||||
|
outb(PS2_DATA_PORT, command);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ps2_write_mouse(uint8_t data) {
|
||||||
|
ps2_wait_write();
|
||||||
|
outb(PS2_COMMAND_REG, PS2_CMD_WRITE_MOUSE); // "Next byte goes to mouse"
|
||||||
|
ps2_wait_write();
|
||||||
|
outb(PS2_DATA_PORT, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ps2_init(void) {
|
||||||
|
// 1. Disable Devices
|
||||||
|
ps2_wait_write();
|
||||||
|
outb(PS2_COMMAND_REG, PS2_CMD_DISABLE_KB);
|
||||||
|
ps2_wait_write();
|
||||||
|
outb(PS2_COMMAND_REG, PS2_CMD_DISABLE_MS);
|
||||||
|
|
||||||
|
// 2. Flush Output Buffer
|
||||||
|
while (inb(PS2_STATUS_REG) & PS2_STATUS_OUTPUT) {
|
||||||
|
inb(PS2_DATA_PORT);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Set Controller Configuration Byte
|
||||||
|
// Bit 0: KB Interrupt, Bit 1: Mouse Interrupt, Bit 6: Translation
|
||||||
|
ps2_wait_write();
|
||||||
|
outb(PS2_COMMAND_REG, PS2_CMD_READ_CONFIG);
|
||||||
|
ps2_wait_read();
|
||||||
|
uint8_t status = inb(PS2_DATA_PORT);
|
||||||
|
status |= (1 << 0) | (1 << 1); // Enable IRQ 1 and IRQ 12
|
||||||
|
|
||||||
|
ps2_wait_write();
|
||||||
|
outb(PS2_COMMAND_REG, PS2_CMD_WRITE_CONFIG);
|
||||||
|
ps2_wait_write();
|
||||||
|
outb(PS2_DATA_PORT, status);
|
||||||
|
|
||||||
|
// 4. Enable Devices
|
||||||
|
ps2_wait_write();
|
||||||
|
outb(PS2_COMMAND_REG, PS2_CMD_ENABLE_KB);
|
||||||
|
ps2_wait_write();
|
||||||
|
outb(PS2_COMMAND_REG, PS2_CMD_ENABLE_MS);
|
||||||
|
|
||||||
|
// 5. Initialize Mouse (The mouse won't send IRQs until you tell it to)
|
||||||
|
ps2_write_mouse(MOUSE_CMD_SET_DEFAULTS);
|
||||||
|
ps2_wait_read(); inb(PS2_DATA_PORT); // Read ACK (0xFA)
|
||||||
|
|
||||||
|
ps2_write_mouse(MOUSE_CMD_ENABLE_SCAN);
|
||||||
|
ps2_wait_read(); inb(PS2_DATA_PORT); // Read ACK (0xFA)
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --- IRQ Handlers --- */
|
||||||
|
|
||||||
|
// Called from IRQ 1 (Keyboard)
|
||||||
|
void ps2_keyboard_handler(void) {
|
||||||
|
uint8_t scancode = inb(PS2_DATA_PORT);
|
||||||
|
// Process scancode (e.g., put it into a circular buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Called from IRQ 12 (Mouse)
|
||||||
|
static uint8_t mouse_cycle = 0;
|
||||||
|
static uint8_t mouse_bytes[3];
|
||||||
|
|
||||||
|
void ps2_mouse_handler(void) {
|
||||||
|
uint8_t status = inb(PS2_STATUS_REG);
|
||||||
|
|
||||||
|
// Ensure this is actually mouse data
|
||||||
|
if (!(status & PS2_STATUS_MOUSE)) return;
|
||||||
|
|
||||||
|
mouse_bytes[mouse_cycle++] = inb(PS2_DATA_PORT);
|
||||||
|
|
||||||
|
if (mouse_cycle == 3) {
|
||||||
|
mouse_cycle = 0;
|
||||||
|
|
||||||
|
// Byte 0: Flags (Buttons, Signs)
|
||||||
|
// Byte 1: X Delta
|
||||||
|
// Byte 2: Y Delta
|
||||||
|
|
||||||
|
mouse_state_t state;
|
||||||
|
state.left_button = (mouse_bytes[0] & 0x01);
|
||||||
|
state.right_button = (mouse_bytes[0] & 0x02);
|
||||||
|
state.middle_button = (mouse_bytes[0] & 0x04);
|
||||||
|
|
||||||
|
// Handle negative deltas (signed 9-bit logic)
|
||||||
|
state.x_delta = (int8_t)mouse_bytes[1];
|
||||||
|
state.y_delta = (int8_t)mouse_bytes[2];
|
||||||
|
|
||||||
|
// Update your kernel's internal mouse position here
|
||||||
|
}
|
||||||
|
}
|
||||||
45
kernel/ps2.h
Normal file
45
kernel/ps2.h
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
#ifndef PS2_H
|
||||||
|
#define PS2_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
/* I/O Ports */
|
||||||
|
#define PS2_DATA_PORT 0x60
|
||||||
|
#define PS2_STATUS_REG 0x64
|
||||||
|
#define PS2_COMMAND_REG 0x64
|
||||||
|
|
||||||
|
/* Status Register Bits */
|
||||||
|
#define PS2_STATUS_OUTPUT 0x01 // 1 = Data ready to be read
|
||||||
|
#define PS2_STATUS_INPUT 0x02 // 1 = Controller busy, don't write yet
|
||||||
|
#define PS2_STATUS_SYS 0x04 // System flag
|
||||||
|
#define PS2_STATUS_CMD_DATA 0x08 // 0 = Data written to 0x60, 1 = Cmd to 0x64
|
||||||
|
#define PS2_STATUS_MOUSE 0x20 // 1 = Mouse data, 0 = Keyboard data
|
||||||
|
|
||||||
|
/* Controller Commands */
|
||||||
|
#define PS2_CMD_READ_CONFIG 0x20
|
||||||
|
#define PS2_CMD_WRITE_CONFIG 0x60
|
||||||
|
#define PS2_CMD_DISABLE_MS 0xA7
|
||||||
|
#define PS2_CMD_ENABLE_MS 0xA8
|
||||||
|
#define PS2_CMD_DISABLE_KB 0xAD
|
||||||
|
#define PS2_CMD_ENABLE_KB 0xAE
|
||||||
|
#define PS2_CMD_WRITE_MOUSE 0xD4
|
||||||
|
|
||||||
|
/* Mouse Commands */
|
||||||
|
#define MOUSE_CMD_SET_DEFAULTS 0xF6
|
||||||
|
#define MOUSE_CMD_ENABLE_SCAN 0xF4
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int8_t x_delta;
|
||||||
|
int8_t y_delta;
|
||||||
|
bool left_button;
|
||||||
|
bool right_button;
|
||||||
|
bool middle_button;
|
||||||
|
} mouse_state_t;
|
||||||
|
|
||||||
|
/* Public API */
|
||||||
|
void ps2_init(void);
|
||||||
|
void ps2_keyboard_handler(void);
|
||||||
|
void ps2_mouse_handler(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,12 +1,11 @@
|
|||||||
|
#include <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
#include "malloc.h"
|
#include "malloc.h"
|
||||||
#include "print.h"
|
#include "print.h"
|
||||||
#include "threading.h"
|
#include "threading.h"
|
||||||
#include "types.h"
|
|
||||||
#include "utils.h"
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
#define MAX_THREADS 16 // Maximum number of threads
|
#define MAX_THREADS 16 // Maximum number of threads
|
||||||
#define THREAD_STACK_SIZE 8192 // Stack size for each thread
|
#define THREAD_STACK_SIZE 8192 // Stack size for each thread
|
||||||
|
|
||||||
// The thread table stores information about all threads
|
// The thread table stores information about all threads
|
||||||
static Thread thread_table[MAX_THREADS];
|
static Thread thread_table[MAX_THREADS];
|
||||||
@@ -17,103 +16,106 @@ static uint32_t num_threads = 0; // Number of active threads
|
|||||||
static volatile int mutex_locked = 0;
|
static volatile int mutex_locked = 0;
|
||||||
|
|
||||||
// Function declaration for context_switch
|
// Function declaration for context_switch
|
||||||
void context_switch(Thread *next);
|
void context_switch(Thread* next);
|
||||||
|
|
||||||
// Initialize the threading system
|
// Initialize the threading system
|
||||||
void thread_init(void) {
|
void thread_init(void) {
|
||||||
memset(thread_table, 0, sizeof(thread_table));
|
memset(thread_table, 0, sizeof(thread_table));
|
||||||
num_threads = 0;
|
num_threads = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new thread
|
// Create a new thread
|
||||||
void thread_create(Thread *thread __attribute__((unused)), void (*start_routine)(void *), void *arg) {
|
void thread_create(Thread* thread __attribute__((unused)),
|
||||||
if (num_threads >= MAX_THREADS) {
|
void (*start_routine)(void*), void* arg) {
|
||||||
my_printf("Error: Maximum thread count reached.\n");
|
if (num_threads >= MAX_THREADS) {
|
||||||
return;
|
my_printf("Error: Maximum thread count reached.\n");
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Find an empty slot for the new thread
|
// Find an empty slot for the new thread
|
||||||
int index = num_threads++;
|
int index = num_threads++;
|
||||||
thread_table[index] = (Thread){0};
|
thread_table[index] = (Thread){0};
|
||||||
|
|
||||||
// Set up the new thread
|
|
||||||
thread_table[index].start_routine = start_routine;
|
|
||||||
thread_table[index].arg = arg;
|
|
||||||
thread_table[index].stack_size = THREAD_STACK_SIZE;
|
|
||||||
thread_table[index].stack = (uint32_t*)malloc(THREAD_STACK_SIZE);
|
|
||||||
thread_table[index].stack_top = thread_table[index].stack + THREAD_STACK_SIZE / sizeof(uint32_t);
|
|
||||||
|
|
||||||
// Initialize the stack (simulate pushing the function's return address)
|
// Set up the new thread
|
||||||
uint32_t *stack_top = thread_table[index].stack_top;
|
thread_table[index].start_routine = start_routine;
|
||||||
*(--stack_top) = (uint32_t)start_routine; // Return address (the thread's entry point)
|
thread_table[index].arg = arg;
|
||||||
*(--stack_top) = (uint32_t)arg; // Argument to pass to the thread
|
thread_table[index].stack_size = THREAD_STACK_SIZE;
|
||||||
|
thread_table[index].stack = (uint32_t*)malloc(THREAD_STACK_SIZE);
|
||||||
|
thread_table[index].stack_top =
|
||||||
|
thread_table[index].stack + THREAD_STACK_SIZE / sizeof(uint32_t);
|
||||||
|
|
||||||
// Set the thread's state to ready
|
// Initialize the stack (simulate pushing the function's return address)
|
||||||
thread_table[index].state = THREAD_READY;
|
uint32_t* stack_top = thread_table[index].stack_top;
|
||||||
|
*(--stack_top) =
|
||||||
|
(uint32_t)start_routine; // Return address (the thread's entry point)
|
||||||
|
*(--stack_top) = (uint32_t)arg; // Argument to pass to the thread
|
||||||
|
|
||||||
// If this is the first thread, switch to it
|
// Set the thread's state to ready
|
||||||
if (index == 0) {
|
thread_table[index].state = THREAD_READY;
|
||||||
scheduler();
|
|
||||||
}
|
// If this is the first thread, switch to it
|
||||||
|
if (index == 0) {
|
||||||
|
scheduler();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Yield the CPU to another thread
|
// Yield the CPU to another thread
|
||||||
void thread_yield(void) {
|
void thread_yield(void) {
|
||||||
// Find the next thread in a round-robin manner
|
// Find the next thread in a round-robin manner
|
||||||
uint32_t next_thread = (current_thread + 1) % num_threads;
|
uint32_t next_thread = (current_thread + 1) % num_threads;
|
||||||
while (next_thread != current_thread && thread_table[next_thread].state != THREAD_READY) {
|
while (next_thread != current_thread &&
|
||||||
next_thread = (next_thread + 1) % num_threads;
|
thread_table[next_thread].state != THREAD_READY) {
|
||||||
}
|
next_thread = (next_thread + 1) % num_threads;
|
||||||
|
}
|
||||||
|
|
||||||
if (next_thread != current_thread) {
|
if (next_thread != current_thread) {
|
||||||
current_thread = next_thread;
|
current_thread = next_thread;
|
||||||
scheduler();
|
scheduler();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exit the current thread
|
// Exit the current thread
|
||||||
void thread_exit(void) {
|
void thread_exit(void) {
|
||||||
thread_table[current_thread].state = THREAD_BLOCKED; // Mark the thread as blocked (finished)
|
thread_table[current_thread].state =
|
||||||
free(thread_table[current_thread].stack); // Free the thread's stack
|
THREAD_BLOCKED; // Mark the thread as blocked (finished)
|
||||||
num_threads--; // Decrease thread count
|
free(thread_table[current_thread].stack); // Free the thread's stack
|
||||||
|
num_threads--; // Decrease thread count
|
||||||
|
|
||||||
// Yield to the next thread
|
// Yield to the next thread
|
||||||
thread_yield();
|
thread_yield();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scheduler: This function selects the next thread to run
|
// Scheduler: This function selects the next thread to run
|
||||||
void scheduler(void) {
|
void scheduler(void) {
|
||||||
// Find the next ready thread
|
// Find the next ready thread
|
||||||
uint32_t next_thread = (current_thread + 1) % num_threads;
|
uint32_t next_thread = (current_thread + 1) % num_threads;
|
||||||
while (thread_table[next_thread].state != THREAD_READY) {
|
while (thread_table[next_thread].state != THREAD_READY) {
|
||||||
next_thread = (next_thread + 1) % num_threads;
|
next_thread = (next_thread + 1) % num_threads;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (next_thread != current_thread) {
|
if (next_thread != current_thread) {
|
||||||
current_thread = next_thread;
|
current_thread = next_thread;
|
||||||
context_switch(&thread_table[current_thread]);
|
context_switch(&thread_table[current_thread]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Context switch to the next thread (assembly would go here to save/load registers)
|
// Context switch to the next thread (assembly would go here to save/load
|
||||||
void context_switch(Thread *next) {
|
// registers)
|
||||||
// For simplicity, context switching in this example would involve saving/restoring registers.
|
void context_switch(Thread* next) {
|
||||||
// In a real system, you would need to save the CPU state (registers) and restore the next thread's state.
|
// For simplicity, context switching in this example would involve
|
||||||
my_printf("Switching to thread...\n");
|
// saving/restoring registers. In a real system, you would need to save the
|
||||||
next->start_routine(next->arg); // Start running the next thread
|
// CPU state (registers) and restore the next thread's state.
|
||||||
|
my_printf("Switching to thread...\n");
|
||||||
|
next->start_routine(next->arg); // Start running the next thread
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simple mutex functions (spinlock)
|
// Simple mutex functions (spinlock)
|
||||||
void mutex_init(void) {
|
void mutex_init(void) { mutex_locked = 0; }
|
||||||
mutex_locked = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void mutex_lock(void) {
|
void mutex_lock(void) {
|
||||||
while (__sync_lock_test_and_set(&mutex_locked, 1)) {
|
while (__sync_lock_test_and_set(&mutex_locked, 1)) {
|
||||||
// Busy wait (spinlock)
|
// Busy wait (spinlock)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void mutex_unlock(void) {
|
void mutex_unlock(void) { __sync_lock_release(&mutex_locked); }
|
||||||
__sync_lock_release(&mutex_locked);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
#include "types.h"
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
#ifndef TYPES_H
|
|
||||||
#define TYPES_H
|
|
||||||
|
|
||||||
// ----------------------------
|
|
||||||
// Fixed-width integer types
|
|
||||||
// ----------------------------
|
|
||||||
typedef unsigned char uint8_t;
|
|
||||||
typedef signed char int8_t;
|
|
||||||
typedef unsigned short uint16_t;
|
|
||||||
typedef signed short int16_t;
|
|
||||||
typedef unsigned int uint32_t;
|
|
||||||
typedef signed int int32_t;
|
|
||||||
typedef unsigned long long uint64_t;
|
|
||||||
typedef signed long long int64_t;
|
|
||||||
|
|
||||||
// ----------------------------
|
|
||||||
// Boolean & NULL definitions
|
|
||||||
// ----------------------------
|
|
||||||
#ifndef __cplusplus
|
|
||||||
typedef enum { false = 0, true = 1 } bool;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef NULL
|
|
||||||
#define NULL ((void*)0)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// ----------------------------
|
|
||||||
// OS subsystem types
|
|
||||||
// ----------------------------
|
|
||||||
typedef int32_t ssize_t;
|
|
||||||
|
|
||||||
typedef uint32_t phys_addr_t; // Physical address
|
|
||||||
typedef uint32_t virt_addr_t; // Virtual address
|
|
||||||
|
|
||||||
typedef uint32_t pid_t; // Process ID
|
|
||||||
typedef uint32_t tid_t; // Thread ID
|
|
||||||
|
|
||||||
// ----------------------------
|
|
||||||
// Bitfield & utility macros
|
|
||||||
// ----------------------------
|
|
||||||
#define BIT(n) (1U << (n))
|
|
||||||
#define BITS(m, n) (((1U << ((n) - (m) + 1)) - 1) << (m))
|
|
||||||
|
|
||||||
// Align value to next multiple of alignment
|
|
||||||
#define ALIGN_UP(val, align) (((val) + ((align)-1)) & ~((align)-1))
|
|
||||||
#define ALIGN_DOWN(val, align) ((val) & ~((align)-1))
|
|
||||||
|
|
||||||
// ----------------------------
|
|
||||||
// Attributes for structures
|
|
||||||
// ----------------------------
|
|
||||||
#define PACKED __attribute__((packed))
|
|
||||||
#define ALIGN(x) __attribute__((aligned(x)))
|
|
||||||
|
|
||||||
// ----------------------------
|
|
||||||
// Likely/unlikely branch hints
|
|
||||||
// (for future optimization use)
|
|
||||||
// ----------------------------
|
|
||||||
#define likely(x) __builtin_expect(!!(x), 1)
|
|
||||||
#define unlikely(x) __builtin_expect(!!(x), 0)
|
|
||||||
|
|
||||||
#endif // TYPES_H
|
|
||||||
@@ -3,14 +3,10 @@
|
|||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
#include "types.h"
|
|
||||||
|
|
||||||
// Convert integer to string (base is typically 10, 16, etc.)
|
// Convert integer to string (base is typically 10, 16, etc.)
|
||||||
char* itoa(int value, char* str, int base);
|
char* itoa(int value, char* str, int base);
|
||||||
|
|
||||||
// Convert unsigned integer to string (base is typically 10, 16, etc.)
|
// Convert unsigned integer to string (base is typically 10, 16, etc.)
|
||||||
char* utoa(unsigned int value, char* str, int base);
|
char* utoa(unsigned int value, char* str, int base);
|
||||||
|
|
||||||
void *memset(void *dest, int value, size_t len);
|
|
||||||
|
|
||||||
#endif // UTILS_H
|
#endif // UTILS_H
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
#include "vga.h"
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include "string_utils.h"
|
#include "string_utils.h"
|
||||||
|
#include "vga.h"
|
||||||
|
|
||||||
void outb(uint16_t port, uint8_t value) {
|
void outb(uint16_t port, uint8_t value) {
|
||||||
__asm__ volatile("outb %0, %1" : : "a"(value), "Nd"(port));
|
__asm__ volatile("outb %0, %1" : : "a"(value), "Nd"(port));
|
||||||
@@ -134,7 +134,7 @@ void vga_printf(const char* format, ...) {
|
|||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
// Now you can use the buffer with vga_write_string
|
// Now you can use the buffer with vga_write_string
|
||||||
vga_write_string(buffer, my_strlen(buffer)); // Use my_strlen instead of strlen
|
vga_write_string(buffer, strlen(buffer)); // Use my_strlen instead of strlen
|
||||||
}
|
}
|
||||||
|
|
||||||
void vga_init(void) {
|
void vga_init(void) {
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ typedef enum {
|
|||||||
// Function prototypes
|
// Function prototypes
|
||||||
uint8_t vga_entry_color(vga_color fg, vga_color bg);
|
uint8_t vga_entry_color(vga_color fg, vga_color bg);
|
||||||
uint16_t vga_entry(unsigned char uc, uint8_t color);
|
uint16_t vga_entry(unsigned char uc, uint8_t color);
|
||||||
|
void vga_init(void);
|
||||||
|
|
||||||
void vga_put_entry_at(char c, uint8_t color, size_t x, size_t y);
|
void vga_put_entry_at(char c, uint8_t color, size_t x, size_t y);
|
||||||
void vga_clear(uint8_t color);
|
void vga_clear(uint8_t color);
|
||||||
@@ -50,4 +51,4 @@ void vga_set_cursor_blink_rate(uint8_t rate);
|
|||||||
|
|
||||||
void vga_printf(const char* format, ...);
|
void vga_printf(const char* format, ...);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
#ifndef CLASSICOS_KLIBC_STDBOOL_H
|
#ifndef CLASSICOS_KLIBC_STDBOOL_H
|
||||||
#define CLASSICOS_KLIBC_STDBOOL_H
|
#define CLASSICOS_KLIBC_STDBOOL_H
|
||||||
|
|
||||||
typedef enum { false = 0, true = 1 } bool;
|
#ifndef __cplusplus
|
||||||
|
#define bool _Bool
|
||||||
|
#define true 1
|
||||||
|
#define false 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define __bool_true_false_are_defined 1
|
||||||
|
|
||||||
#endif // CLASSICOS_KLIBC_STDBOOL_H
|
#endif // CLASSICOS_KLIBC_STDBOOL_H
|
||||||
|
|||||||
@@ -3,12 +3,13 @@
|
|||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
extern int memcmp(const void* s1, const void* s2, size_t n);
|
extern int memcmp(const void *s1, const void *s2, size_t n);
|
||||||
extern void* memmove(void* dst, const void* src, size_t n);
|
extern void *memmove(void *dst, const void *src, size_t n);
|
||||||
extern void* memcpy(void* dst, const void* src, size_t n);
|
extern void *memcpy(void *dst, const void *src, size_t n);
|
||||||
extern void* memset(void* dst, int c, size_t n);
|
extern void *memset(void *dst, int c, size_t n);
|
||||||
|
|
||||||
extern size_t strlen(const char* s);
|
extern size_t strlen(const char *s);
|
||||||
extern int strcmp(const char* s1, const char* s2);
|
extern int strcmp(const char *s1, const char *s2);
|
||||||
|
extern char *strncpy(char *dst, const char *src, size_t n);
|
||||||
|
|
||||||
#endif // CLASSICOS_KLIBC_STRING_H
|
#endif // CLASSICOS_KLIBC_STRING_H
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
int memcmp(const void* s1, const void* s2, size_t n) {
|
int memcmp(const void *s1, const void *s2, size_t n) {
|
||||||
const unsigned char* c1 = s1;
|
const unsigned char *c1 = s1;
|
||||||
const unsigned char* c2 = s2;
|
const unsigned char *c2 = s2;
|
||||||
int d = 0;
|
int d = 0;
|
||||||
|
|
||||||
while (n--) {
|
while (n--) {
|
||||||
@@ -13,9 +13,9 @@ int memcmp(const void* s1, const void* s2, size_t n) {
|
|||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* memmove(void* dst, const void* src, size_t n) {
|
void *memmove(void *dst, const void *src, size_t n) {
|
||||||
const char* p = src;
|
const char *p = src;
|
||||||
char* q = dst;
|
char *q = dst;
|
||||||
#if defined(__i386__) || defined(__x86_64__)
|
#if defined(__i386__) || defined(__x86_64__)
|
||||||
if (q < p) {
|
if (q < p) {
|
||||||
__asm__ volatile("cld; rep; movsb" : "+c"(n), "+S"(p), "+D"(q));
|
__asm__ volatile("cld; rep; movsb" : "+c"(n), "+S"(p), "+D"(q));
|
||||||
@@ -41,19 +41,19 @@ void* memmove(void* dst, const void* src, size_t n) {
|
|||||||
return dst;
|
return dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* memcpy(void* dst, const void* src, size_t n) {
|
void *memcpy(void *dst, const void *src, size_t n) {
|
||||||
const char* p = src;
|
const char *p = src;
|
||||||
char* q = dst;
|
char *q = dst;
|
||||||
#if defined(__i386__)
|
#if defined(__i386__)
|
||||||
size_t nl = n >> 2;
|
size_t nl = n >> 2;
|
||||||
__asm__ volatile("cld ; rep ; movsl ; movl %3,%0 ; rep ; movsb"
|
__asm__ volatile("cld ; rep ; movsl ; movl %3,%0 ; rep ; movsb"
|
||||||
: "+c"(nl), "+S"(p), "+D"(q)
|
: "+c"(nl), "+S"(p), "+D"(q)
|
||||||
: "r"(n & 3));
|
: "r"(n & 3));
|
||||||
#elif defined(__x86_64__)
|
#elif defined(__x86_64__)
|
||||||
size_t nq = n >> 3;
|
size_t nq = n >> 3;
|
||||||
__asm__ volatile("cld ; rep ; movsq ; movl %3,%%ecx ; rep ; movsb"
|
__asm__ volatile("cld ; rep ; movsq ; movl %3,%%ecx ; rep ; movsb"
|
||||||
: "+c"(nq), "+S"(p), "+D"(q)
|
: "+c"(nq), "+S"(p), "+D"(q)
|
||||||
: "r"((uint32_t)(n & 7)));
|
: "r"((uint32_t)(n & 7)));
|
||||||
#else
|
#else
|
||||||
while (n--) {
|
while (n--) {
|
||||||
*q++ = *p++;
|
*q++ = *p++;
|
||||||
@@ -63,20 +63,20 @@ void* memcpy(void* dst, const void* src, size_t n) {
|
|||||||
return dst;
|
return dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* memset(void* dst, int c, size_t n) {
|
void *memset(void *dst, int c, size_t n) {
|
||||||
char* q = dst;
|
char *q = dst;
|
||||||
|
|
||||||
#if defined(__i386__)
|
#if defined(__i386__)
|
||||||
size_t nl = n >> 2;
|
size_t nl = n >> 2;
|
||||||
__asm__ volatile("cld ; rep ; stosl ; movl %3,%0 ; rep ; stosb"
|
__asm__ volatile("cld ; rep ; stosl ; movl %3,%0 ; rep ; stosb"
|
||||||
: "+c"(nl), "+D"(q)
|
: "+c"(nl), "+D"(q)
|
||||||
: "a"((unsigned char)c * 0x01010101U), "r"(n & 3));
|
: "a"((unsigned char)c * 0x01010101U), "r"(n & 3));
|
||||||
#elif defined(__x86_64__)
|
#elif defined(__x86_64__)
|
||||||
size_t nq = n >> 3;
|
size_t nq = n >> 3;
|
||||||
__asm__ volatile("cld ; rep ; stosq ; movl %3,%%ecx ; rep ; stosb"
|
__asm__ volatile("cld ; rep ; stosq ; movl %3,%%ecx ; rep ; stosb"
|
||||||
: "+c"(nq), "+D"(q)
|
: "+c"(nq), "+D"(q)
|
||||||
: "a"((unsigned char)c * 0x0101010101010101U),
|
: "a"((unsigned char)c * 0x0101010101010101U),
|
||||||
"r"((uint32_t)n & 7));
|
"r"((uint32_t)n & 7));
|
||||||
#else
|
#else
|
||||||
while (n--) {
|
while (n--) {
|
||||||
*q++ = c;
|
*q++ = c;
|
||||||
@@ -86,15 +86,15 @@ void* memset(void* dst, int c, size_t n) {
|
|||||||
return dst;
|
return dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t strlen(const char* s) {
|
size_t strlen(const char *s) {
|
||||||
const char* ss = s;
|
const char *ss = s;
|
||||||
while (*ss) ss++;
|
while (*ss) ss++;
|
||||||
return ss - s;
|
return ss - s;
|
||||||
}
|
}
|
||||||
|
|
||||||
int strcmp(const char* s1, const char* s2) {
|
int strcmp(const char *s1, const char *s2) {
|
||||||
const unsigned char* c1 = (const unsigned char*)s1;
|
const unsigned char *c1 = (const unsigned char *)s1;
|
||||||
const unsigned char* c2 = (const unsigned char*)s2;
|
const unsigned char *c2 = (const unsigned char *)s2;
|
||||||
unsigned char ch;
|
unsigned char ch;
|
||||||
int d = 0;
|
int d = 0;
|
||||||
|
|
||||||
@@ -105,3 +105,19 @@ int strcmp(const char* s1, const char* s2) {
|
|||||||
|
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *strncpy(char *dst, const char *src, size_t n) {
|
||||||
|
char *q = dst;
|
||||||
|
const char *p = src;
|
||||||
|
char ch;
|
||||||
|
|
||||||
|
while (n) {
|
||||||
|
n--;
|
||||||
|
*q++ = ch = *p++;
|
||||||
|
if (!ch) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(q, 0, n);
|
||||||
|
|
||||||
|
return dst;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user