Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export BUILD_DRIVERS_HPET = 1

export BUILD_DRIVERS_AHCI = 1

export BUILD_DRIVERS_GPT = 1
export BUILD_DRIVERS_EXT2 = 1

# End of options

ifdef BUILD_DRIVERS_AHCI
Expand Down Expand Up @@ -71,7 +74,7 @@ TEST_TARGETS := \
TEST_CANIDATES := $(patsubst $(OBJ_DIR)/test_%.a,test-%,$(TEST_TARGETS))
TEST_EXEC := $(basename $(TEST_TARGETS))

COPY_DOC_TO := $(OBJ_DIR)/rootfs/doc/ModulOS/
COPY_DOC_TO := $(OBJ_DIR)/rootfs/usr/share/doc/ModulOS/

SRC := $(shell find . -type f \( -name "*.c" -o -name "*.S" -o -name "*.h" \))

Expand All @@ -97,7 +100,7 @@ test-all: $(TEST_CANIDATES)

.PHONY: $(TEST_CANIDATES)
$(TEST_CANIDATES): test-%: $(OBJ_DIR)/test_%
./$<
-./$<

.PHONY: $(SUBDIRS)
$(SUBDIRS):
Expand Down Expand Up @@ -127,7 +130,7 @@ $(OBJ_DIR)/stub.img: | $(OBJ_DIR)/
parted -s $@ mklabel gpt
parted -s $@ mkpart ESP fat32 4MiB 52MiB
parted -s $@ set 1 esp on
parted -s $@ mkpart rootfs ext4 52MiB 100%
parted -s $@ mkpart rootfs ext2 52MiB 100%

$(OBJ_DIR)/stub-esp.dummy: $(OBJ_DIR)/stub.img $(OBJ_DIR)/modulos $(OBJ_DIR)/esp.img
mcopy -o -i $(OBJ_DIR)/esp.img $(OBJ_DIR)/modulos ::/
Expand All @@ -137,7 +140,7 @@ $(OBJ_DIR)/stub-esp.dummy: $(OBJ_DIR)/stub.img $(OBJ_DIR)/modulos $(OBJ_DIR)/esp
# 54525952 is for 52MiB offset (512 * 1024 * 1024)
# 1034240 is for 52MiB initial (4MiB align + 48MiB ESP) and 4MiB tail for gpt
$(OBJ_DIR)/stub-fs.dummy: $(OBJ_DIR)/stub.img copy-doc
yes | mke2fs -L rootfs -E offset=54525952 -d $(OBJ_DIR)/rootfs/ -t ext4 \
yes | mke2fs -L rootfs -E offset=54525952 -d $(OBJ_DIR)/rootfs/ -t ext2 \
-b 4096 $< 1034240
touch $@

Expand Down
11 changes: 11 additions & 0 deletions boot/multiboot2/boot.S
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ ktransfer:
pushq $0
pushq $0
call multiboot2_init
movq init_stack_vaddr, %rsp
addq $0x5000, %rsp
pushq $0
pushq $0
jmp kentry

.section .data.boot
Expand Down Expand Up @@ -330,3 +334,10 @@ stack:
.rept STACK_SIZE
.byte 0
.endr

/* pointer to guarded stack */
.globl init_stack_vaddr
init_stack_vaddr: .quad 0

.globl init_stack_paddr
init_stack_paddr: .quad 0
13 changes: 13 additions & 0 deletions boot/multiboot2/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <kernel/core/kentry.h>
#include <kernel/core/mm.h>
#include <kernel/core/paging.h>
#include <kernel/core/gdt.h>
#include <kernel/core/logging.h>
#include <kernel/core/proc_data.h>
Expand Down Expand Up @@ -104,6 +105,9 @@ extern volatile struct gdt_t gdt[GDT_NUM_ENTRIES];

static volatile struct mb2_tag_t* memmap_tag;

extern uint64_t init_stack_paddr;
extern uint64_t init_stack_vaddr;

static void first_segment(uint64_t* handle) {
*handle = 0;
}
Expand Down Expand Up @@ -202,6 +206,15 @@ void multiboot2_init(struct mb2_info_t* info) {
i = (i + 7) & ~MOD8_MASK;
}

init_stack_paddr = mm_alloc_p(PAGE_SIZE_4K * 4);
init_stack_vaddr = mm_alloc_v(PAGE_SIZE_4K * 5);

paging_map(init_stack_vaddr + PAGE_SIZE_4K * 1, init_stack_paddr + PAGE_SIZE_4K * 0, PAGE_PRESENT | PAGE_RW, PAGE_4K);
paging_map(init_stack_vaddr + PAGE_SIZE_4K * 2, init_stack_paddr + PAGE_SIZE_4K * 1, PAGE_PRESENT | PAGE_RW, PAGE_4K);
paging_map(init_stack_vaddr + PAGE_SIZE_4K * 3, init_stack_paddr + PAGE_SIZE_4K * 2, PAGE_PRESENT | PAGE_RW, PAGE_4K);
paging_map(init_stack_vaddr + PAGE_SIZE_4K * 4, init_stack_paddr + PAGE_SIZE_4K * 3, PAGE_PRESENT | PAGE_RW, PAGE_4K);
paging_install_guard(init_stack_vaddr);

boot_context.gdt = &gdt;
return;
}
8 changes: 8 additions & 0 deletions drivers/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ ifdef BUILD_DRIVERS_SATA
$(call add_directory,sata,SATA)
endif

ifdef BUILD_DRIVERS_GPT
$(call add_directory,gpt,GPT)
endif

ifdef BUILD_DRIVERS_EXT2
$(call add_directory,ext2,EXT2)
endif

include $(SRC_TREE_ROOT)/scripts/Makefile.kcflags
include $(SRC_TREE_ROOT)/scripts/Makefile.targets

Expand Down
17 changes: 9 additions & 8 deletions drivers/ahci/ahci.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
#define SSTS_DET_MASK 0x000Fu
#define SSTS_DET_EST 0x0003u

#define CL_SIZE 256
#define FIS_SIZE 1024
#define CL_SIZE 1024
#define FIS_SIZE 256

#define SLOT_NO_SLOT 0xFF

Expand Down Expand Up @@ -174,11 +174,12 @@ static uint8_t find_slot(struct ahci_t* ahci, uint32_t port) {
read = hba_read(ahci, PXCI_OFF(port));

for (slot = 0; slot < ahci->num_com_slots; slot++) {
if (!(ahci->used_com && (1u << slot)) && !(read && (1u << slot))) {
if (!(ahci->used_com & (1u << slot)) && !(read & (1u << slot))) {
return slot;
}
}

//TODO: assert valid slot before using
return SLOT_NO_SLOT;
}

Expand Down Expand Up @@ -245,7 +246,7 @@ static enum disk_error_t ahci_read_lba(void* cntx, void* buffer, uint64_t lba, u

port_clear_errors(ahci, port);

kmemset(&ahci->ports[port]->recv_fis, 0, sizeof(struct ahci_recv_fis_t));
kmemset(ahci->ports[port]->recv_fis, 0, sizeof(struct ahci_recv_fis_t));

ahci->ports[port]->com_list[slot].flg = 5;
ahci->ports[port]->com_list[slot].prdtl = 1;
Expand Down Expand Up @@ -371,7 +372,7 @@ static enum disk_error_t ahci_write_lba(void* cntx, void* buffer, uint64_t lba,

port_clear_errors(ahci, port);

kmemset(&ahci->ports[port]->recv_fis, 0, sizeof(struct ahci_recv_fis_t));
kmemset(ahci->ports[port]->recv_fis, 0, sizeof(struct ahci_recv_fis_t));

ahci->ports[port]->com_list[slot].flg = 5 | SATA_FIS_CMD_W;
ahci->ports[port]->com_list[slot].prdtl = 1;
Expand Down Expand Up @@ -453,7 +454,7 @@ static enum disk_error_t ahci_flush_cache(void* cntx) {

port_clear_errors(ahci, port);

kmemset(&ahci->ports[port]->recv_fis, 0, sizeof(struct ahci_recv_fis_t));
kmemset(ahci->ports[port]->recv_fis, 0, sizeof(struct ahci_recv_fis_t));

ahci->ports[port]->com_list[slot].flg = 5;
ahci->ports[port]->com_list[slot].prdtl = 0;
Expand Down Expand Up @@ -534,7 +535,7 @@ static void port_identify(struct ahci_t* ahci, uint32_t port) {

port_clear_errors(ahci, port);

kmemset(&ahci->ports[port]->recv_fis, 0, sizeof(struct ahci_recv_fis_t));
kmemset(ahci->ports[port]->recv_fis, 0, sizeof(struct ahci_recv_fis_t));

ahci->ports[port]->com_list[slot].flg = 5;
ahci->ports[port]->com_list[slot].prdtl = 1;
Expand Down Expand Up @@ -565,7 +566,7 @@ static void port_identify(struct ahci_t* ahci, uint32_t port) {

lba48 = *(uint64_t*)&identity[100];

logging_log_info("Found ATA drive %s 0x%lx", &model[0], lba48);
logging_log_debug("Found ATA drive %s 0x%lx", &model[0], lba48);

paging_unmap((uint64_t)identity, PAGE_4K);

Expand Down
53 changes: 47 additions & 6 deletions drivers/apic/apic_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ extern uint8_t gdt;
extern uint8_t gdt_end;
extern uint8_t kernel_pml4;

uint64_t* init_stacks;
uint64_t* init_stacks_paddr;
uint64_t* init_stacks_vaddr;
volatile struct gdt_t(** ap_gdts)[GDT_NUM_ENTRIES];
volatile struct gdt_ptr_64_t** ap_gdt_ptr_64;
uint8_t* ap_init_locks;
Expand All @@ -113,13 +114,16 @@ void apic_init(void) {
error_vector = idt_get_vector();

// install idts
idt_install(timer_vector, (uint64_t)apic_isr_timer, GDT_CODE_SEL, IST_SCHED, IDT_GATE_INT, 0);
idt_install(error_vector, (uint64_t)apic_isr_error, GDT_CODE_SEL, 0, IDT_GATE_INT, 0);
idt_install(timer_vector, (uint64_t)apic_isr_timer, GDT_CODE_SEL, IST_SCHED, IDT_GATE_TRP, 0);
idt_install(error_vector, (uint64_t)apic_isr_error, GDT_CODE_SEL, 0, IDT_GATE_TRP, 0);

apic_init_shootdowns(num_apic);

apic_init_ap();

// init stacks
init_stacks = kmalloc(sizeof(uint64_t*) * num_apic);
init_stacks_vaddr = kmalloc(sizeof(uint64_t) * num_apic);
init_stacks_paddr = kmalloc(sizeof(uint64_t) * num_apic);
proc_data_ptr = kmalloc(sizeof(struct proc_data_t*) * num_apic);
proc_data_ptr[0] = &bsp_proc_data;

Expand All @@ -130,7 +134,42 @@ void apic_init(void) {

for (--num_apic; num_apic; num_apic--) {
proc_data_ptr[num_apic] = kmalloc(sizeof(struct proc_data_t));
init_stacks[num_apic] = (uint64_t)kmalloc(INIT_STACK_SIZE) + INIT_STACK_SIZE;
init_stacks_vaddr[num_apic] = mm_alloc_v(PAGE_SIZE_4K * 5);

if (!init_stacks_vaddr[num_apic]) {
logging_log_error("Failed to allocate stack");
panic(PANIC_NO_MEM);
}

init_stacks_paddr[num_apic] = mm_alloc_p(PAGE_SIZE_4K * 4);

if (!init_stacks_paddr[num_apic]) {
logging_log_error("Failed to allocate stack");
mm_free_v(init_stacks_vaddr[num_apic], PAGE_SIZE_4K * 5);
panic(PANIC_NO_MEM);
}

paging_map(
init_stacks_vaddr[num_apic] + PAGE_SIZE_4K * 1,
init_stacks_paddr[num_apic] + PAGE_SIZE_4K * 0,
PAGE_PRESENT | PAGE_RW,
PAGE_4K);
paging_map(
init_stacks_vaddr[num_apic] + PAGE_SIZE_4K * 2,
init_stacks_paddr[num_apic] + PAGE_SIZE_4K * 1,
PAGE_PRESENT | PAGE_RW,
PAGE_4K);
paging_map(
init_stacks_vaddr[num_apic] + PAGE_SIZE_4K * 3,
init_stacks_paddr[num_apic] + PAGE_SIZE_4K * 2,
PAGE_PRESENT | PAGE_RW,
PAGE_4K);
paging_map(
init_stacks_vaddr[num_apic] + PAGE_SIZE_4K * 4,
init_stacks_paddr[num_apic] + PAGE_SIZE_4K * 3,
PAGE_PRESENT | PAGE_RW,
PAGE_4K);
paging_install_guard(init_stacks_vaddr[num_apic]);

ap_gdts[num_apic] = kmalloc(sizeof(struct gdt_t[GDT_NUM_ENTRIES]));
ap_gdt_ptr_64[num_apic] = kmalloc(sizeof(struct gdt_ptr_64_t));
Expand Down Expand Up @@ -242,6 +281,8 @@ void apic_init_ap(void) {
apic_write_lve(APIC_REG_ERE, error_vector,
APIC_LVT_MT_FIXED | APIC_LVT_TRG_EDGE, 0);

apic_register_barrier(apic_id);

// enable apic
apic_write_reg(APIC_REG_ESR, 0);
apic_write_reg(APIC_REG_SPR, PIC_SPURIOUS_VEC | APIC_ASE); // pic and apic spurious both only iret, so reuse
Expand Down Expand Up @@ -311,7 +352,7 @@ void apic_start_ap(void) {
(uint16_t)((uint64_t)&gdt_end - (uint64_t)&gdt - 1); // gdt64 ptr
*(volatile uint64_t*)paging_ident(AP_ARB_BASE + 0xa) = (uint64_t)&gdt;
*(volatile uint32_t*)paging_ident(AP_ARB_BASE + 0x12) = (uint32_t)(uint64_t)&kernel_pml4;
*(volatile uint64_t*)paging_ident(AP_ARB_BASE + 0x16) = (uint64_t)init_stacks;
*(volatile uint64_t*)paging_ident(AP_ARB_BASE + 0x16) = (uint64_t)init_stacks_vaddr;

uint64_t handle;
struct acpi_madt_ics_local_apic_t* local_apic;
Expand Down
Loading
Loading