mirror of
https://github.com/gbowne1/ClassicOS.git
synced 2026-03-09 08:25:19 -07:00
Compare commits
106 Commits
gbowne1-ad
...
98cb663939
| Author | SHA1 | Date | |
|---|---|---|---|
| 98cb663939 | |||
| c2859d5618 | |||
| 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 | |||
| a37f94de44 | |||
| 401a19143c | |||
| cc92ade8fd | |||
| a9f6d5fa05 | |||
| 4e8b13ad77 | |||
| 9f8ca3a60c | |||
| 574980035e | |||
| a0bd0941d6 |
9
Makefile
9
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))
|
||||||
@@ -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 $@ $<
|
||||||
|
|||||||
@@ -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,9 +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
|
||||||
|
|||||||
107
kernel/fat16.c
Normal file
107
kernel/fat16.c
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
#include "fat16.h"
|
||||||
|
#include "ata.h" // Use ata_read_sector and ata_write_sector
|
||||||
|
#include "print.h" // For debugging
|
||||||
|
#include <string.h> // For string manipulation
|
||||||
|
|
||||||
|
// Global variables
|
||||||
|
static fat16_boot_sector_t boot_sector;
|
||||||
|
static uint32_t root_dir_sector = FAT16_ROOT_DIR_SECTOR;
|
||||||
|
|
||||||
|
// Read a sector from the disk using ATA
|
||||||
|
bool read_sector(uint32_t lba, uint8_t* buffer) {
|
||||||
|
return ata_read_sector(lba, buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write a sector to the disk using ATA
|
||||||
|
bool write_sector(uint32_t lba, const uint8_t* buffer) {
|
||||||
|
return ata_write_sector(lba, buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the boot sector to retrieve basic file system info
|
||||||
|
bool parse_fat16_boot_sector(void) {
|
||||||
|
uint8_t sector_buffer[FAT16_SECTOR_SIZE];
|
||||||
|
|
||||||
|
// Read the boot sector
|
||||||
|
if (!read_sector(FAT16_BOOT_SECTOR, sector_buffer)) {
|
||||||
|
print_string("[FAT16] Failed to read boot sector\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cast to boot sector structure
|
||||||
|
memcpy(&boot_sector, sector_buffer, sizeof(fat16_boot_sector_t));
|
||||||
|
|
||||||
|
// Check for FAT16 signature
|
||||||
|
if (boot_sector.oem_name[0] != 'F' || boot_sector.oem_name[1] != 'A' || boot_sector.oem_name[2] != 'T') {
|
||||||
|
print_string("[FAT16] Invalid FAT16 boot sector signature\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
print_string("[FAT16] FAT16 boot sector parsed successfully\n");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the root directory
|
||||||
|
bool parse_fat16_root_dir(void) {
|
||||||
|
uint8_t sector_buffer[FAT16_SECTOR_SIZE];
|
||||||
|
|
||||||
|
for (int i = 0; i < (boot_sector.max_root_entries / (FAT16_SECTOR_SIZE / sizeof(fat16_dir_entry_t))); i++) {
|
||||||
|
// Read root directory sector
|
||||||
|
if (!read_sector(root_dir_sector + i, sector_buffer)) {
|
||||||
|
print_string("[FAT16] Failed to read root directory sector\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the root directory entries
|
||||||
|
for (int j = 0; j < (FAT16_SECTOR_SIZE / sizeof(fat16_dir_entry_t)); j++) {
|
||||||
|
fat16_dir_entry_t* entry = (fat16_dir_entry_t*)§or_buffer[j * sizeof(fat16_dir_entry_t)];
|
||||||
|
if (entry->name[0] == 0x00) {
|
||||||
|
// End of directory entries
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (entry->name[0] != 0xE5) {
|
||||||
|
// Print file name (8.3 format)
|
||||||
|
char filename[12];
|
||||||
|
strncpy(filename, (char*)entry->name, 8);
|
||||||
|
filename[8] = '.';
|
||||||
|
strncpy(&filename[9], (char*)entry->ext, 3);
|
||||||
|
filename[11] = '\0';
|
||||||
|
print_string(filename);
|
||||||
|
print_string("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read a specific directory entry from the FAT16 root directory
|
||||||
|
bool read_fat16_entry(uint16_t entry_index, fat16_dir_entry_t* entry) {
|
||||||
|
uint8_t sector_buffer[FAT16_SECTOR_SIZE];
|
||||||
|
uint32_t sector_num = FAT16_ROOT_DIR_SECTOR + (entry_index / (FAT16_SECTOR_SIZE / sizeof(fat16_dir_entry_t)));
|
||||||
|
uint16_t entry_offset = entry_index % (FAT16_SECTOR_SIZE / sizeof(fat16_dir_entry_t));
|
||||||
|
|
||||||
|
// Read the sector
|
||||||
|
if (!read_sector(sector_num, sector_buffer)) {
|
||||||
|
print_string("[FAT16] Failed to read root directory sector\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the entry
|
||||||
|
memcpy(entry, §or_buffer[entry_offset * sizeof(fat16_dir_entry_t)], sizeof(fat16_dir_entry_t));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mount the FAT16 filesystem
|
||||||
|
bool mount_fat16(void) {
|
||||||
|
// Parse the boot sector
|
||||||
|
if (!parse_fat16_boot_sector()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the root directory
|
||||||
|
if (!parse_fat16_root_dir()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
print_string("[FAT16] Filesystem mounted successfully\n");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
60
kernel/fat16.h
Normal file
60
kernel/fat16.h
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
#ifndef FAT16_H
|
||||||
|
#define FAT16_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
/* FAT16 Constants */
|
||||||
|
#define FAT16_SECTOR_SIZE 512
|
||||||
|
#define FAT16_CLUSTER_SIZE 1
|
||||||
|
#define FAT16_MAX_FILENAME_LEN 11 // 8.3 format
|
||||||
|
#define FAT16_ROOT_DIR_ENTRIES 224 // Fat16 root directory entries (typically 512 bytes per entry)
|
||||||
|
|
||||||
|
#define FAT16_BOOT_SECTOR 0
|
||||||
|
#define FAT16_FAT1_SECTOR 1
|
||||||
|
#define FAT16_FAT2_SECTOR 2
|
||||||
|
#define FAT16_ROOT_DIR_SECTOR 19 // First sector of root directory
|
||||||
|
|
||||||
|
/* Boot Sector */
|
||||||
|
typedef struct {
|
||||||
|
uint8_t jmp[3]; // Jump instruction to code
|
||||||
|
uint8_t oem_name[8]; // OEM Name
|
||||||
|
uint16_t bytes_per_sector; // Bytes per sector (512)
|
||||||
|
uint8_t sectors_per_cluster; // Sectors per cluster
|
||||||
|
uint16_t reserved_sectors; // Reserved sectors
|
||||||
|
uint8_t num_fats; // Number of FAT tables
|
||||||
|
uint16_t max_root_entries; // Max number of root directory entries
|
||||||
|
uint16_t total_sectors_16; // Total sectors in FAT16
|
||||||
|
uint8_t media_type; // Media type (0xF8 = fixed drive)
|
||||||
|
uint16_t sectors_per_fat; // Sectors per FAT table
|
||||||
|
uint16_t sectors_per_track; // Sectors per track (for CHS addressing)
|
||||||
|
uint16_t num_heads; // Number of heads (for CHS addressing)
|
||||||
|
uint32_t hidden_sectors; // Hidden sectors (before the partition)
|
||||||
|
uint32_t total_sectors_32; // Total sectors in FAT16 (extended)
|
||||||
|
} __attribute__((packed)) fat16_boot_sector_t;
|
||||||
|
|
||||||
|
/* FAT16 Directory Entry */
|
||||||
|
typedef struct {
|
||||||
|
uint8_t name[8]; // File name (8 chars)
|
||||||
|
uint8_t ext[3]; // File extension (3 chars)
|
||||||
|
uint8_t attributes; // File attributes (e.g., directory, read-only)
|
||||||
|
uint8_t reserved; // Reserved
|
||||||
|
uint8_t creation_time[2]; // Creation time
|
||||||
|
uint8_t creation_date[2]; // Creation date
|
||||||
|
uint8_t last_access_date[2]; // Last access date
|
||||||
|
uint8_t first_cluster_high[2]; // High part of first cluster number
|
||||||
|
uint8_t last_mod_time[2]; // Last modification time
|
||||||
|
uint8_t last_mod_date[2]; // Last modification date
|
||||||
|
uint8_t first_cluster_low[2]; // Low part of first cluster number
|
||||||
|
uint32_t file_size; // File size in bytes
|
||||||
|
} __attribute__((packed)) fat16_dir_entry_t;
|
||||||
|
|
||||||
|
/* Function Prototypes */
|
||||||
|
bool mount_fat16(void);
|
||||||
|
bool read_sector(uint32_t lba, uint8_t* buffer);
|
||||||
|
bool write_sector(uint32_t lba, const uint8_t* buffer);
|
||||||
|
bool parse_fat16_boot_sector(void);
|
||||||
|
bool parse_fat16_root_dir(void);
|
||||||
|
bool read_fat16_entry(uint16_t entry_index, fat16_dir_entry_t* entry);
|
||||||
|
|
||||||
|
#endif // FAT16_H
|
||||||
@@ -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);
|
||||||
|
|||||||
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
|
||||||
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
|
||||||
@@ -4,8 +4,8 @@
|
|||||||
#include "print.h"
|
#include "print.h"
|
||||||
#include "threading.h"
|
#include "threading.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];
|
||||||
@@ -16,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);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -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