mirror of
https://git.stationery.faith/corn/corn.git
synced 2024-11-23 15:52:20 +00:00
refactor arch
This commit is contained in:
parent
a717dbdd00
commit
1b09896afc
27 changed files with 343 additions and 264 deletions
46
Makefile
46
Makefile
|
@ -1,10 +1,14 @@
|
||||||
KERNEL=kernel.bin
|
#
|
||||||
ISO=os_image.iso
|
# Makefile Configuration
|
||||||
|
#
|
||||||
|
|
||||||
ARCH=amd64
|
|
||||||
CC=cc
|
CC=cc
|
||||||
LD=ld
|
LD=ld
|
||||||
AS=nasm
|
AS=nasm
|
||||||
|
ARCH=amd64
|
||||||
|
ARCH_COMMON=x86_common
|
||||||
|
|
||||||
|
BUILD=build
|
||||||
|
|
||||||
CFLAGS += -std=c2x -ffreestanding -lgcc -isystem include -pipe
|
CFLAGS += -std=c2x -ffreestanding -lgcc -isystem include -pipe
|
||||||
CFLAGS += -Wall -Wextra -pedantic
|
CFLAGS += -Wall -Wextra -pedantic
|
||||||
|
@ -13,24 +17,37 @@ CFLAGS += -DPAGE_SIZE=4096
|
||||||
|
|
||||||
LDFLAGS += -nmagic --no-warn-rwx-segments -nostdlib
|
LDFLAGS += -nmagic --no-warn-rwx-segments -nostdlib
|
||||||
|
|
||||||
H_SRC = $(shell find src include -type f -name "*.h")
|
#
|
||||||
|
# Makefile
|
||||||
|
#
|
||||||
|
|
||||||
C_SRC = $(shell find src -type f -name "*.c")
|
ARCH_COMMON += $(ARCH)
|
||||||
|
|
||||||
|
KERNEL=kernel.bin
|
||||||
|
ISO=os_image.iso
|
||||||
|
|
||||||
|
ARCH_DIRS = $(patsubst %,src/arch/%,$(ARCH_COMMON))
|
||||||
|
ARCH_INCLUDES = $(shell find $(ARCH_DIRS) -type d -name "include")
|
||||||
|
CFLAGS_ARCH = $(patsubst %, -isystem %,$(ARCH_INCLUDES))
|
||||||
|
|
||||||
|
H_SRC = $(shell find src include -type f -name "*.h" -not -path "src/arch/*")
|
||||||
|
HA_SRC = $(shell find $(ARCH_DIRS) -type f -name "*.h")
|
||||||
|
|
||||||
|
C_SRC = $(shell find src -type f -name "*.c" -not -path "src/arch/*")
|
||||||
C_OBJ = $(patsubst %.c,build/%.o,$(C_SRC))
|
C_OBJ = $(patsubst %.c,build/%.o,$(C_SRC))
|
||||||
|
CA_SRC = $(shell find $(ARCH_DIRS) -type f -name "*.c")
|
||||||
|
CA_OBJ = $(patsubst %.c,build/%.o,$(CA_SRC))
|
||||||
|
|
||||||
A_SRC = $(shell find src -type f -name "*.S" -not -path "src/arch/*")
|
A_SRC = $(shell find $(ARCH_DIRS) -type f -name "*.S")
|
||||||
A_SRC += $(shell find src/arch/$(ARCH) -type f -name "*.S")
|
|
||||||
A_OBJ = $(patsubst %.S,build/%.S.o,$(A_SRC))
|
A_OBJ = $(patsubst %.S,build/%.S.o,$(A_SRC))
|
||||||
|
|
||||||
.PHONY: all clean
|
.PHONY: all clean
|
||||||
|
|
||||||
all: build/$(ISO)
|
all: build/$(ISO)
|
||||||
|
|
||||||
build: build/$(ISO)
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@printf "\033[31m RM \033[0m%s\n" build
|
@printf "\033[31m RM \033[0m%s\n" build
|
||||||
@rm -rf build/*
|
@rm -fr ./build
|
||||||
|
|
||||||
$(A_OBJ): build/%.S.o : %.S
|
$(A_OBJ): build/%.S.o : %.S
|
||||||
@mkdir -p $(@D)
|
@mkdir -p $(@D)
|
||||||
|
@ -42,10 +59,15 @@ $(C_OBJ): build/%.o : %.c
|
||||||
@printf "\033[34m CC \033[0m%s\n" $<
|
@printf "\033[34m CC \033[0m%s\n" $<
|
||||||
@$(CC) -c $(CFLAGS) -o $@ $<
|
@$(CC) -c $(CFLAGS) -o $@ $<
|
||||||
|
|
||||||
build/$(KERNEL): arch/$(ARCH)/linker.ld $(A_OBJ) $(C_OBJ) $(H_SRC)
|
$(CA_OBJ): build/%.o : %.c
|
||||||
|
@mkdir -p $(@D)
|
||||||
|
@printf "\033[34m CC \033[0m%s\n" $<
|
||||||
|
@$(CC) -c $(CFLAGS) $(CFLAGS_ARCH) -o $@ $<
|
||||||
|
|
||||||
|
build/$(KERNEL): arch/$(ARCH)/linker.ld $(A_OBJ) $(C_OBJ) $(CA_OBJ) $(H_SRC) $(HA_SRC)
|
||||||
@mkdir -p $(@D)
|
@mkdir -p $(@D)
|
||||||
@printf "\033[32m LD \033[0m%s\n" $@
|
@printf "\033[32m LD \033[0m%s\n" $@
|
||||||
@$(LD) $(LDFLAGS) -T arch/$(ARCH)/linker.ld -o build/$(KERNEL) $(A_OBJ) $(C_OBJ)
|
@$(LD) $(LDFLAGS) -T arch/$(ARCH)/linker.ld -o build/$(KERNEL) $(A_OBJ) $(C_OBJ) $(CA_OBJ)
|
||||||
|
|
||||||
build/$(ISO): build/$(KERNEL) grub.cfg
|
build/$(ISO): build/$(KERNEL) grub.cfg
|
||||||
@mkdir -p $(@D)
|
@mkdir -p $(@D)
|
||||||
|
|
BIN
debugger.o
Normal file
BIN
debugger.o
Normal file
Binary file not shown.
|
@ -1,3 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
void enable_fpu(void);
|
|
177
include/sys/acpi.h
Normal file
177
include/sys/acpi.h
Normal file
|
@ -0,0 +1,177 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#ifndef ACPI_INTERNAL
|
||||||
|
#error "Do not include <sys/acpi.h>, only use <acpi.h>"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct acpi_header {
|
||||||
|
uint32_t signature;
|
||||||
|
uint32_t length;
|
||||||
|
uint8_t revision;
|
||||||
|
uint8_t checksum;
|
||||||
|
uint8_t oem_id[6];
|
||||||
|
uint8_t oem_table_id[8];
|
||||||
|
uint32_t oem_revision;
|
||||||
|
uint32_t creator_id;
|
||||||
|
uint32_t creator_revision;
|
||||||
|
} __attribute__((packed));
|
||||||
|
|
||||||
|
// root system descriptor pointer
|
||||||
|
// ACPI 1.0
|
||||||
|
struct rsdp {
|
||||||
|
uint8_t signature[8];
|
||||||
|
uint8_t checksum;
|
||||||
|
uint8_t oemid[6];
|
||||||
|
uint8_t revision;
|
||||||
|
uint32_t rsdt_addr;
|
||||||
|
} __attribute__((packed));
|
||||||
|
|
||||||
|
// eXtended system descriptor pointer
|
||||||
|
// ACPI 2.0
|
||||||
|
struct xsdp {
|
||||||
|
char signature[8];
|
||||||
|
uint8_t checksum;
|
||||||
|
char oemid[6];
|
||||||
|
uint8_t revision;
|
||||||
|
uint32_t rsdt_addr;
|
||||||
|
uint32_t length;
|
||||||
|
uint64_t xsdt_addr;
|
||||||
|
uint8_t extendeid_checksum;
|
||||||
|
uint8_t reserved[3];
|
||||||
|
} __attribute__((packed));
|
||||||
|
|
||||||
|
// root system descriptor table
|
||||||
|
// ACPI 1.0
|
||||||
|
struct rsdt {
|
||||||
|
struct acpi_header h;
|
||||||
|
uint32_t sdt_pointers[];
|
||||||
|
} __attribute__((packed));
|
||||||
|
|
||||||
|
// eXtended system descriptor table
|
||||||
|
// ACPI 2.0
|
||||||
|
struct xsdt {
|
||||||
|
struct acpi_header h;
|
||||||
|
uint64_t sdt_pointers[];
|
||||||
|
} __attribute__((packed));
|
||||||
|
|
||||||
|
|
||||||
|
// generic address structure
|
||||||
|
struct gas {
|
||||||
|
uint8_t address_space;
|
||||||
|
uint8_t bit_width;
|
||||||
|
uint8_t bit_offset;
|
||||||
|
uint8_t access_size;
|
||||||
|
uint64_t address;
|
||||||
|
};
|
||||||
|
|
||||||
|
// differentiated system description table
|
||||||
|
struct dsdt {
|
||||||
|
struct acpi_header h;
|
||||||
|
char s5_addr[];
|
||||||
|
} __attribute__((packed));
|
||||||
|
|
||||||
|
struct apic {
|
||||||
|
struct acpi_header h;
|
||||||
|
// todo
|
||||||
|
} __attribute__((packed));
|
||||||
|
|
||||||
|
struct hept {
|
||||||
|
struct acpi_header h;
|
||||||
|
// todo
|
||||||
|
} __attribute__((packed));
|
||||||
|
|
||||||
|
struct waet {
|
||||||
|
struct acpi_header h;
|
||||||
|
// todo
|
||||||
|
} __attribute__((packed));
|
||||||
|
|
||||||
|
// fixed acpi description table
|
||||||
|
struct fadt {
|
||||||
|
struct acpi_header h;
|
||||||
|
uint32_t firmware_ctrl;
|
||||||
|
uint32_t dsdt;
|
||||||
|
|
||||||
|
// field used in ACPI 1.0; no longer in use, for compatibility only
|
||||||
|
uint8_t reserved;
|
||||||
|
|
||||||
|
uint8_t preferred_power_management_profile;
|
||||||
|
uint16_t sci_interrupt;
|
||||||
|
uint32_t smi_command_port;
|
||||||
|
uint8_t acpi_enable;
|
||||||
|
uint8_t acpi_disable;
|
||||||
|
uint8_t s4bios_req;
|
||||||
|
uint8_t pstate_control;
|
||||||
|
uint32_t pm1_a_event_block;
|
||||||
|
uint32_t pm1_b_event_block;
|
||||||
|
uint32_t pm1_a_control_block;
|
||||||
|
uint32_t pm1_b_control_block;
|
||||||
|
uint32_t pm2_control_block;
|
||||||
|
uint32_t pm_timer_block;
|
||||||
|
uint32_t gpe0_block;
|
||||||
|
uint32_t gpe1_block;
|
||||||
|
uint8_t pm1_event_length;
|
||||||
|
uint8_t pm1_control_length;
|
||||||
|
uint8_t pm2_control_length;
|
||||||
|
uint8_t pm_timer_length;
|
||||||
|
uint8_t gpe0_length;
|
||||||
|
uint8_t gpe1_length;
|
||||||
|
uint8_t gpe1_base;
|
||||||
|
uint8_t cstate_control;
|
||||||
|
uint16_t worst_c2_latency;
|
||||||
|
uint16_t worst_c3_latency;
|
||||||
|
uint16_t flush_size;
|
||||||
|
uint16_t flush_stride;
|
||||||
|
uint8_t duty_offset;
|
||||||
|
uint8_t duty_width;
|
||||||
|
uint8_t day_alarm;
|
||||||
|
uint8_t month_alarm;
|
||||||
|
uint8_t century;
|
||||||
|
|
||||||
|
// reserved in ACPI 1.0; used since ACPI 2.0+
|
||||||
|
uint16_t boot_architecture_flags;
|
||||||
|
|
||||||
|
uint8_t reserved_2;
|
||||||
|
uint32_t flags;
|
||||||
|
|
||||||
|
// 12 byte structure; see below for details
|
||||||
|
struct gas reset_reg;
|
||||||
|
|
||||||
|
uint8_t reset_value;
|
||||||
|
uint8_t reserved_3[3];
|
||||||
|
|
||||||
|
// 64bit pointers - Available on ACPI 2.0+
|
||||||
|
uint64_t x_firmware_control;
|
||||||
|
uint64_t x_dsdt;
|
||||||
|
|
||||||
|
struct gas x_pm1_a_event_block;
|
||||||
|
struct gas x_pm1_b_event_block;
|
||||||
|
struct gas x_pm1_a_control_block;
|
||||||
|
struct gas x_pm1_b_control_block;
|
||||||
|
struct gas x_pm2_control_block;
|
||||||
|
struct gas x_pm_timer_block;
|
||||||
|
struct gas x_gpe0_block;
|
||||||
|
struct gas x_gpe1_block;
|
||||||
|
} __attribute__((packed));
|
||||||
|
|
||||||
|
struct acpi_state {
|
||||||
|
union {
|
||||||
|
struct xsdt *xsdt;
|
||||||
|
struct rsdt *rsdt;
|
||||||
|
} sdt;
|
||||||
|
struct fadt *fadt;
|
||||||
|
struct dsdt *dsdt;
|
||||||
|
struct apic *apic;
|
||||||
|
struct hept *hept;
|
||||||
|
struct waet *waet;
|
||||||
|
uint8_t version;
|
||||||
|
|
||||||
|
uint16_t SLP_TYPa;
|
||||||
|
uint16_t SLP_TYPb;
|
||||||
|
uint16_t SLP_EN;
|
||||||
|
uint16_t SCI_EN;
|
||||||
|
};
|
||||||
|
|
||||||
|
void acpi_sys_enable(struct acpi_state *state);
|
||||||
|
int acpi_sys_shutdown(struct acpi_state *state);
|
16
include/sys/pci.h
Normal file
16
include/sys/pci.h
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef PCI_INTERNAL
|
||||||
|
#error "do not include <sys/pci.h>, only use <pci.h>"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <pci.h>
|
||||||
|
|
||||||
|
uint32_t pci_sys_rcfg_d(struct pci_device dev, uint8_t offset);
|
||||||
|
uint16_t pci_sys_rcfg_w(struct pci_device dev, uint8_t offset);
|
||||||
|
uint8_t pci_sys_rcfg_b(struct pci_device dev, uint8_t offset);
|
||||||
|
|
||||||
|
void pci_sys_wcfg_d(struct pci_device dev, uint8_t offset, uint32_t dword);
|
||||||
|
void pci_sys_wcfg_w(struct pci_device dev, uint8_t offset, uint16_t word);
|
||||||
|
void pci_sys_wcfg_b(struct pci_device dev, uint8_t offset, uint8_t byte);
|
|
@ -1,7 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifndef MEMORY_INTERNAL
|
#ifndef MEMORY_INTERNAL
|
||||||
#error "Do not include headers from <memory/___.h>, only use <memory.h>"
|
#error "Do not include headers from <sys/physalloc.h>, only use <memory.h>"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
|
@ -6,174 +6,8 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <panic.h>
|
#include <panic.h>
|
||||||
|
|
||||||
#include "bindings.h"
|
#define ACPI_INTERNAL
|
||||||
|
#include <sys/acpi.h>
|
||||||
struct acpi_header {
|
|
||||||
uint32_t signature;
|
|
||||||
uint32_t length;
|
|
||||||
uint8_t revision;
|
|
||||||
uint8_t checksum;
|
|
||||||
uint8_t oem_id[6];
|
|
||||||
uint8_t oem_table_id[8];
|
|
||||||
uint32_t oem_revision;
|
|
||||||
uint32_t creator_id;
|
|
||||||
uint32_t creator_revision;
|
|
||||||
} __attribute__((packed));
|
|
||||||
|
|
||||||
// root system descriptor pointer
|
|
||||||
// ACPI 1.0
|
|
||||||
struct rsdp {
|
|
||||||
uint8_t signature[8];
|
|
||||||
uint8_t checksum;
|
|
||||||
uint8_t oemid[6];
|
|
||||||
uint8_t revision;
|
|
||||||
uint32_t rsdt_addr;
|
|
||||||
} __attribute__((packed));
|
|
||||||
|
|
||||||
// eXtended system descriptor pointer
|
|
||||||
// ACPI 2.0
|
|
||||||
struct xsdp {
|
|
||||||
char signature[8];
|
|
||||||
uint8_t checksum;
|
|
||||||
char oemid[6];
|
|
||||||
uint8_t revision;
|
|
||||||
uint32_t rsdt_addr;
|
|
||||||
uint32_t length;
|
|
||||||
uint64_t xsdt_addr;
|
|
||||||
uint8_t extendeid_checksum;
|
|
||||||
uint8_t reserved[3];
|
|
||||||
} __attribute__((packed));
|
|
||||||
|
|
||||||
// root system descriptor table
|
|
||||||
// ACPI 1.0
|
|
||||||
struct rsdt {
|
|
||||||
struct acpi_header h;
|
|
||||||
uint32_t sdt_pointers[];
|
|
||||||
} __attribute__((packed));
|
|
||||||
|
|
||||||
// eXtended system descriptor table
|
|
||||||
// ACPI 2.0
|
|
||||||
struct xsdt {
|
|
||||||
struct acpi_header h;
|
|
||||||
uint64_t sdt_pointers[];
|
|
||||||
} __attribute__((packed));
|
|
||||||
|
|
||||||
|
|
||||||
// generic address structure
|
|
||||||
struct gas {
|
|
||||||
uint8_t address_space;
|
|
||||||
uint8_t bit_width;
|
|
||||||
uint8_t bit_offset;
|
|
||||||
uint8_t access_size;
|
|
||||||
uint64_t address;
|
|
||||||
};
|
|
||||||
|
|
||||||
// differentiated system description table
|
|
||||||
struct dsdt {
|
|
||||||
struct acpi_header h;
|
|
||||||
char s5_addr[];
|
|
||||||
} __attribute__((packed));
|
|
||||||
|
|
||||||
struct apic {
|
|
||||||
struct acpi_header h;
|
|
||||||
// todo
|
|
||||||
} __attribute__((packed));
|
|
||||||
|
|
||||||
struct hept {
|
|
||||||
struct acpi_header h;
|
|
||||||
// todo
|
|
||||||
} __attribute__((packed));
|
|
||||||
|
|
||||||
struct waet {
|
|
||||||
struct acpi_header h;
|
|
||||||
// todo
|
|
||||||
} __attribute__((packed));
|
|
||||||
|
|
||||||
// fixed acpi description table
|
|
||||||
struct fadt {
|
|
||||||
struct acpi_header h;
|
|
||||||
uint32_t firmware_ctrl;
|
|
||||||
uint32_t dsdt;
|
|
||||||
|
|
||||||
// field used in ACPI 1.0; no longer in use, for compatibility only
|
|
||||||
uint8_t reserved;
|
|
||||||
|
|
||||||
uint8_t preferred_power_management_profile;
|
|
||||||
uint16_t sci_interrupt;
|
|
||||||
uint32_t smi_command_port;
|
|
||||||
uint8_t acpi_enable;
|
|
||||||
uint8_t acpi_disable;
|
|
||||||
uint8_t s4bios_req;
|
|
||||||
uint8_t pstate_control;
|
|
||||||
uint32_t pm1_a_event_block;
|
|
||||||
uint32_t pm1_b_event_block;
|
|
||||||
uint32_t pm1_a_control_block;
|
|
||||||
uint32_t pm1_b_control_block;
|
|
||||||
uint32_t pm2_control_block;
|
|
||||||
uint32_t pm_timer_block;
|
|
||||||
uint32_t gpe0_block;
|
|
||||||
uint32_t gpe1_block;
|
|
||||||
uint8_t pm1_event_length;
|
|
||||||
uint8_t pm1_control_length;
|
|
||||||
uint8_t pm2_control_length;
|
|
||||||
uint8_t pm_timer_length;
|
|
||||||
uint8_t gpe0_length;
|
|
||||||
uint8_t gpe1_length;
|
|
||||||
uint8_t gpe1_base;
|
|
||||||
uint8_t cstate_control;
|
|
||||||
uint16_t worst_c2_latency;
|
|
||||||
uint16_t worst_c3_latency;
|
|
||||||
uint16_t flush_size;
|
|
||||||
uint16_t flush_stride;
|
|
||||||
uint8_t duty_offset;
|
|
||||||
uint8_t duty_width;
|
|
||||||
uint8_t day_alarm;
|
|
||||||
uint8_t month_alarm;
|
|
||||||
uint8_t century;
|
|
||||||
|
|
||||||
// reserved in ACPI 1.0; used since ACPI 2.0+
|
|
||||||
uint16_t boot_architecture_flags;
|
|
||||||
|
|
||||||
uint8_t reserved_2;
|
|
||||||
uint32_t flags;
|
|
||||||
|
|
||||||
// 12 byte structure; see below for details
|
|
||||||
struct gas reset_reg;
|
|
||||||
|
|
||||||
uint8_t reset_value;
|
|
||||||
uint8_t reserved_3[3];
|
|
||||||
|
|
||||||
// 64bit pointers - Available on ACPI 2.0+
|
|
||||||
uint64_t x_firmware_control;
|
|
||||||
uint64_t x_dsdt;
|
|
||||||
|
|
||||||
struct gas x_pm1_a_event_block;
|
|
||||||
struct gas x_pm1_b_event_block;
|
|
||||||
struct gas x_pm1_a_control_block;
|
|
||||||
struct gas x_pm1_b_control_block;
|
|
||||||
struct gas x_pm2_control_block;
|
|
||||||
struct gas x_pm_timer_block;
|
|
||||||
struct gas x_gpe0_block;
|
|
||||||
struct gas x_gpe1_block;
|
|
||||||
} __attribute__((packed));
|
|
||||||
|
|
||||||
struct acpi_state {
|
|
||||||
union {
|
|
||||||
struct xsdt *xsdt;
|
|
||||||
struct rsdt *rsdt;
|
|
||||||
} sdt;
|
|
||||||
struct fadt *fadt;
|
|
||||||
struct dsdt *dsdt;
|
|
||||||
struct apic *apic;
|
|
||||||
struct hept *hept;
|
|
||||||
struct waet *waet;
|
|
||||||
uint8_t version;
|
|
||||||
|
|
||||||
uint16_t SLP_TYPa;
|
|
||||||
uint16_t SLP_TYPb;
|
|
||||||
uint16_t SLP_EN;
|
|
||||||
uint16_t SCI_EN;
|
|
||||||
};
|
|
||||||
|
|
||||||
/* global state, idk a better way rn */
|
/* global state, idk a better way rn */
|
||||||
static struct acpi_state state;
|
static struct acpi_state state;
|
||||||
|
@ -284,7 +118,6 @@ static void acpi_handle_table(struct acpi_header *header) {
|
||||||
static void acpi_load_table(uint64_t addr) {
|
static void acpi_load_table(uint64_t addr) {
|
||||||
struct acpi_header *temp, *mapped;
|
struct acpi_header *temp, *mapped;
|
||||||
uint32_t length;
|
uint32_t length;
|
||||||
|
|
||||||
temp = (struct acpi_header * ) (uintptr_t) addr;
|
temp = (struct acpi_header * ) (uintptr_t) addr;
|
||||||
mapped = mmap(temp, sizeof(struct acpi_header));
|
mapped = mmap(temp, sizeof(struct acpi_header));
|
||||||
length = mapped->length;
|
length = mapped->length;
|
||||||
|
@ -299,18 +132,14 @@ static void acpi_load_table(uint64_t addr) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int acpi_init(void *rootsdp) {
|
int acpi_init(void *rootsdp) {
|
||||||
|
|
||||||
memset(&state, 0, sizeof(struct acpi_state));
|
memset(&state, 0, sizeof(struct acpi_state));
|
||||||
|
|
||||||
struct rsdp *rsdp = (struct rsdp *) rootsdp;
|
struct rsdp *rsdp = (struct rsdp *) rootsdp;
|
||||||
|
if (!checksum((uint8_t *)rsdp, sizeof(struct rsdp))) {
|
||||||
if (!checksum((uint8_t *)rsdp, sizeof(struct rsdp)))
|
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
if (memcmp(rsdp->signature, "RSD PTR ", 8) != 0) {
|
if (memcmp(rsdp->signature, "RSD PTR ", 8) != 0) {
|
||||||
panic("invalid acpi rsdp signature: %.*s\n", 8, rsdp->signature);
|
panic("invalid acpi rsdp signature: %.*s\n", 8, rsdp->signature);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rsdp->revision == 0) {
|
if (rsdp->revision == 0) {
|
||||||
state.version = 0;
|
state.version = 0;
|
||||||
kprintf("ACPI 1.0\n");
|
kprintf("ACPI 1.0\n");
|
||||||
|
@ -323,15 +152,11 @@ int acpi_init(void *rootsdp) {
|
||||||
} else {
|
} else {
|
||||||
panic("invalid acpi rev: %d\n", rsdp->revision);
|
panic("invalid acpi rev: %d\n", rsdp->revision);
|
||||||
}
|
}
|
||||||
|
|
||||||
kprintf("\n");
|
kprintf("\n");
|
||||||
|
acpi_sys_enable(&state);
|
||||||
outb(state.fadt->smi_command_port,state.fadt->acpi_enable);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int acpi_shutdown(void) {
|
int acpi_shutdown(void) {
|
||||||
outw((unsigned int) state.fadt->pm1_a_control_block, state.SLP_TYPb | state.SLP_EN);
|
return acpi_sys_shutdown(&state);
|
||||||
return -1;
|
|
||||||
}
|
}
|
|
@ -1,14 +1,14 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <lib.h>
|
#include <lib.h>
|
||||||
|
#include <bindings.h>
|
||||||
|
#include <pic.h>
|
||||||
|
|
||||||
#include "idt.h"
|
#include "idt.h"
|
||||||
#include "backtrace.h"
|
#include "backtrace.h"
|
||||||
#include "debugger.h"
|
#include "debugger.h"
|
||||||
#include "registers.h"
|
#include "registers.h"
|
||||||
#include "../paging.h"
|
#include "../paging.h"
|
||||||
#include "../bindings.h"
|
|
||||||
#include "../drivers/pic.h"
|
|
||||||
|
|
||||||
#define IDT_SIZE 256
|
#define IDT_SIZE 256
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
#include <fpu.h>
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <lib.h>
|
#include <lib.h>
|
||||||
|
|
||||||
void enable_fpu(void) {
|
#include "fpu.h"
|
||||||
|
|
||||||
|
void fpu_enable(void) {
|
||||||
size_t cr4;
|
size_t cr4;
|
||||||
uint16_t cw = 0x37F;
|
uint16_t cw = 0x37F;
|
||||||
__asm__ volatile ("mov %%cr4, %0" : "=r"(cr4));
|
__asm__ volatile ("mov %%cr4, %0" : "=r"(cr4));
|
||||||
|
|
3
src/arch/amd64/fpu.h
Normal file
3
src/arch/amd64/fpu.h
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
void fpu_enable(void);
|
|
@ -3,13 +3,13 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <lib.h>
|
#include <lib.h>
|
||||||
#include <memory.h>
|
#include <memory.h>
|
||||||
|
#include <bindings.h>
|
||||||
|
|
||||||
#define MEMORY_INTERNAL
|
#define MEMORY_INTERNAL
|
||||||
#include <memory/physalloc.h>
|
#include <sys/physalloc.h>
|
||||||
#include <memory/virtalloc.h>
|
#include <sys/virtalloc.h>
|
||||||
|
|
||||||
#include "paging.h"
|
#include "paging.h"
|
||||||
#include "bindings.h"
|
|
||||||
|
|
||||||
// PAGE MAP LEVEL 4 ENTRY
|
// PAGE MAP LEVEL 4 ENTRY
|
||||||
struct pml4e {
|
struct pml4e {
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
#include <panic.h>
|
#include <panic.h>
|
||||||
#include <backtrace.h>
|
#include <backtrace.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include <bindings.h>
|
||||||
#include <lib.h>
|
#include <lib.h>
|
||||||
|
|
||||||
#include "bindings.h"
|
|
||||||
|
|
||||||
_Noreturn void _panic_impl(char *line, char *file, char *format, ...) {
|
_Noreturn void _panic_impl(char *line, char *file, char *format, ...) {
|
||||||
cli();
|
cli();
|
||||||
va_list list;
|
va_list list;
|
||||||
|
|
|
@ -3,11 +3,12 @@
|
||||||
#include <lib.h>
|
#include <lib.h>
|
||||||
#include <shim.h>
|
#include <shim.h>
|
||||||
#include <memory.h>
|
#include <memory.h>
|
||||||
|
#include <mboot.h>
|
||||||
|
#include <pic.h>
|
||||||
|
|
||||||
#include "paging.h"
|
#include "paging.h"
|
||||||
#include "mboot.h"
|
#include "fpu.h"
|
||||||
#include "cpu/idt.h"
|
#include "cpu/idt.h"
|
||||||
#include "drivers/pic.h"
|
|
||||||
|
|
||||||
static struct boot_info boot_info;
|
static struct boot_info boot_info;
|
||||||
|
|
||||||
|
@ -16,6 +17,7 @@ void *amd64_shim(long mboot_magic, volatile void *mboot_data_ptr) {
|
||||||
paging_init();
|
paging_init();
|
||||||
pic_remap();
|
pic_remap();
|
||||||
idt_init();
|
idt_init();
|
||||||
|
fpu_enable();
|
||||||
|
|
||||||
mboot_load_info(mboot_magic, mboot_data_ptr, &boot_info);
|
mboot_load_info(mboot_magic, mboot_data_ptr, &boot_info);
|
||||||
|
|
||||||
|
|
13
src/arch/x86_common/acpi.c
Normal file
13
src/arch/x86_common/acpi.c
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
#include <bindings.h>
|
||||||
|
|
||||||
|
#define ACPI_INTERNAL
|
||||||
|
#include <sys/acpi.h>
|
||||||
|
|
||||||
|
void acpi_sys_enable(struct acpi_state *state) {
|
||||||
|
outb(state->fadt->smi_command_port, state->fadt->acpi_enable);
|
||||||
|
}
|
||||||
|
|
||||||
|
int acpi_sys_shutdown(struct acpi_state *state) {
|
||||||
|
outw((unsigned int) state->fadt->pm1_a_control_block, state->SLP_TYPb | state->SLP_EN);
|
||||||
|
return -1;
|
||||||
|
}
|
|
@ -4,8 +4,7 @@
|
||||||
#include <panic.h>
|
#include <panic.h>
|
||||||
#include <pci.h>
|
#include <pci.h>
|
||||||
#include <bochs.h>
|
#include <bochs.h>
|
||||||
|
#include <bindings.h>
|
||||||
#include "../bindings.h"
|
|
||||||
|
|
||||||
#define INDEX 0x1CE
|
#define INDEX 0x1CE
|
||||||
#define DATA 0x1CF
|
#define DATA 0x1CF
|
57
src/arch/x86_common/drivers/pci.c
Normal file
57
src/arch/x86_common/drivers/pci.c
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
#include <pci.h>
|
||||||
|
#include <bindings.h>
|
||||||
|
|
||||||
|
#define PCI_INTERNAL
|
||||||
|
#include <sys/pci.h>
|
||||||
|
|
||||||
|
#define CONF_ADDR 0xCF8
|
||||||
|
#define CONF_DATA 0xCFC
|
||||||
|
|
||||||
|
uint32_t pci_sys_rcfg_d(struct pci_device dev, uint8_t offset) {
|
||||||
|
uint32_t addr = 0x80000000;
|
||||||
|
addr |= ((uint32_t)dev.bus) << 16;
|
||||||
|
addr |= ((uint32_t)dev.device) << 11;
|
||||||
|
addr |= ((uint32_t)dev.function) << 8;
|
||||||
|
addr |= offset & 0xFC;
|
||||||
|
|
||||||
|
outl(CONF_ADDR, addr);
|
||||||
|
uint32_t in = inl(CONF_DATA);
|
||||||
|
return in;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t pci_sys_rcfg_w(struct pci_device dev, uint8_t offset) {
|
||||||
|
uint32_t dword = pci_sys_rcfg_d(dev, offset);
|
||||||
|
return (uint16_t)((dword >> ((offset & 2) * 8)) & 0xFFFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t pci_sys_rcfg_b(struct pci_device dev, uint8_t offset) {
|
||||||
|
uint32_t dword = pci_sys_rcfg_d(dev, offset);
|
||||||
|
return (uint8_t)((dword >> ((offset & 3) * 8)) & 0xFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pci_sys_wcfg_d(struct pci_device dev, uint8_t offset, uint32_t dword) {
|
||||||
|
uint32_t addr = 0x80000000;
|
||||||
|
addr |= ((uint32_t)dev.bus) << 16;
|
||||||
|
addr |= ((uint32_t)dev.device) << 11;
|
||||||
|
addr |= ((uint32_t)dev.function) << 8;
|
||||||
|
addr |= offset & 0xFC;
|
||||||
|
|
||||||
|
outl(CONF_ADDR, addr);
|
||||||
|
outl(CONF_DATA, dword);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pci_sys_wcfg_w(struct pci_device dev, uint8_t offset, uint16_t word) {
|
||||||
|
size_t shift = (offset & 2) * 8;
|
||||||
|
uint32_t dword = pci_sys_rcfg_d(dev, offset);
|
||||||
|
dword &= ~(0xFFFF << shift);
|
||||||
|
dword |= word << shift;
|
||||||
|
pci_sys_wcfg_d(dev, offset, dword);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pci_sys_wcfg_b(struct pci_device dev, uint8_t offset, uint8_t byte) {
|
||||||
|
size_t shift = (offset & 3) * 8;
|
||||||
|
uint32_t dword = pci_sys_rcfg_d(dev, offset);
|
||||||
|
dword &= ~(0xFF << shift);
|
||||||
|
dword |= byte << shift;
|
||||||
|
pci_sys_wcfg_d(dev, offset, dword);
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
#include "../bindings.h"
|
#include <bindings.h>
|
||||||
#include "pic.h"
|
#include <pic.h>
|
||||||
|
|
||||||
#define PIC1 0x20 /* IO base address for master PIC */
|
#define PIC1 0x20 /* IO base address for master PIC */
|
||||||
#define PIC2 0xA0 /* IO base address for slave PIC */
|
#define PIC2 0xA0 /* IO base address for slave PIC */
|
|
@ -1,6 +1,5 @@
|
||||||
#include <serial.h>
|
#include <serial.h>
|
||||||
|
#include <bindings.h>
|
||||||
#include "../bindings.h"
|
|
||||||
|
|
||||||
#define PORT 0x3F8
|
#define PORT 0x3F8
|
||||||
|
|
|
@ -2,8 +2,7 @@
|
||||||
#include <lib.h>
|
#include <lib.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <panic.h>
|
#include <panic.h>
|
||||||
|
#include <mboot.h>
|
||||||
#include "mboot.h"
|
|
||||||
|
|
||||||
#define MBOOT_HEADER_MAGIC 0x36D76289
|
#define MBOOT_HEADER_MAGIC 0x36D76289
|
||||||
|
|
|
@ -1,12 +1,9 @@
|
||||||
#include <stdint.h>
|
|
||||||
#include <pci.h>
|
#include <pci.h>
|
||||||
#include <panic.h>
|
|
||||||
#include <lib.h>
|
#include <lib.h>
|
||||||
|
#include <panic.h>
|
||||||
|
|
||||||
#include "../bindings.h"
|
#define PCI_INTERNAL
|
||||||
|
#include <sys/pci.h>
|
||||||
#define CONF_ADDR 0xCF8
|
|
||||||
#define CONF_DATA 0xCFC
|
|
||||||
|
|
||||||
#define TABLE_LEN 16
|
#define TABLE_LEN 16
|
||||||
|
|
||||||
|
@ -20,58 +17,34 @@ struct pci_table_entry {
|
||||||
uint8_t revision;
|
uint8_t revision;
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct pci_table_entry pci_table[TABLE_LEN];
|
|
||||||
static size_t pci_table_next = 0;
|
|
||||||
|
|
||||||
uint32_t pci_rcfg_d(struct pci_device dev, uint8_t offset) {
|
uint32_t pci_rcfg_d(struct pci_device dev, uint8_t offset) {
|
||||||
uint32_t addr = 0x80000000;
|
return pci_sys_rcfg_d(dev, offset);
|
||||||
addr |= ((uint32_t)dev.bus) << 16;
|
|
||||||
addr |= ((uint32_t)dev.device) << 11;
|
|
||||||
addr |= ((uint32_t)dev.function) << 8;
|
|
||||||
addr |= offset & 0xFC;
|
|
||||||
|
|
||||||
outl(CONF_ADDR, addr);
|
|
||||||
uint32_t in = inl(CONF_DATA);
|
|
||||||
return in;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t pci_rcfg_w(struct pci_device dev, uint8_t offset) {
|
uint16_t pci_rcfg_w(struct pci_device dev, uint8_t offset) {
|
||||||
uint32_t dword = pci_rcfg_d(dev, offset);
|
return pci_sys_rcfg_w(dev, offset);
|
||||||
return (uint16_t)((dword >> ((offset & 2) * 8)) & 0xFFFF);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t pci_rcfg_b(struct pci_device dev, uint8_t offset) {
|
uint8_t pci_rcfg_b(struct pci_device dev, uint8_t offset) {
|
||||||
uint32_t dword = pci_rcfg_d(dev, offset);
|
return pci_sys_rcfg_b(dev, offset);
|
||||||
return (uint8_t)((dword >> ((offset & 3) * 8)) & 0xFF);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void pci_wcfg_d(struct pci_device dev, uint8_t offset, uint32_t dword) {
|
void pci_wcfg_d(struct pci_device dev, uint8_t offset, uint32_t dword) {
|
||||||
uint32_t addr = 0x80000000;
|
pci_sys_wcfg_d(dev, offset, dword);
|
||||||
addr |= ((uint32_t)dev.bus) << 16;
|
|
||||||
addr |= ((uint32_t)dev.device) << 11;
|
|
||||||
addr |= ((uint32_t)dev.function) << 8;
|
|
||||||
addr |= offset & 0xFC;
|
|
||||||
|
|
||||||
outl(CONF_ADDR, addr);
|
|
||||||
outl(CONF_DATA, dword);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void pci_wcfg_w(struct pci_device dev, uint8_t offset, uint16_t word) {
|
void pci_wcfg_w(struct pci_device dev, uint8_t offset, uint16_t word) {
|
||||||
size_t shift = (offset & 2) * 8;
|
pci_sys_wcfg_w(dev, offset, word);
|
||||||
uint32_t dword = pci_rcfg_d(dev, offset);
|
|
||||||
dword &= ~(0xFFFF << shift);
|
|
||||||
dword |= word << shift;
|
|
||||||
pci_wcfg_d(dev, offset, dword);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void pci_wcfg_b(struct pci_device dev, uint8_t offset, uint8_t byte) {
|
void pci_wcfg_b(struct pci_device dev, uint8_t offset, uint8_t byte) {
|
||||||
size_t shift = (offset & 3) * 8;
|
pci_sys_wcfg_b(dev, offset, byte);
|
||||||
uint32_t dword = pci_rcfg_d(dev, offset);
|
|
||||||
dword &= ~(0xFF << shift);
|
|
||||||
dword |= byte << shift;
|
|
||||||
pci_wcfg_d(dev, offset, dword);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static struct pci_table_entry pci_table[TABLE_LEN];
|
||||||
|
static size_t pci_table_next = 0;
|
||||||
|
|
||||||
|
|
||||||
static void print_device(struct pci_table_entry *entry) {
|
static void print_device(struct pci_table_entry *entry) {
|
||||||
kprintf(
|
kprintf(
|
||||||
"BUS: %#-4x DEV: %#-4x FUNC: %#-4x ID: %04x:%04x CLASS: %02x:%02x:%02x REV: %#02x\n",
|
"BUS: %#-4x DEV: %#-4x FUNC: %#-4x ID: %04x:%04x CLASS: %02x:%02x:%02x REV: %#02x\n",
|
|
@ -1,5 +1,4 @@
|
||||||
#include <backtrace.h>
|
#include <backtrace.h>
|
||||||
#include <fpu.h>
|
|
||||||
#include <acpi.h>
|
#include <acpi.h>
|
||||||
#include <memory.h>
|
#include <memory.h>
|
||||||
#include <lib.h>
|
#include <lib.h>
|
||||||
|
@ -7,7 +6,6 @@
|
||||||
#include <screen.h>
|
#include <screen.h>
|
||||||
|
|
||||||
void kmain(struct boot_info *info) {
|
void kmain(struct boot_info *info) {
|
||||||
enable_fpu();
|
|
||||||
memory_init(&info->map);
|
memory_init(&info->map);
|
||||||
pci_init();
|
pci_init();
|
||||||
screen_init();
|
screen_init();
|
||||||
|
|
|
@ -4,8 +4,8 @@
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
#define MEMORY_INTERNAL
|
#define MEMORY_INTERNAL
|
||||||
#include <memory/physalloc.h>
|
#include <sys/physalloc.h>
|
||||||
#include <memory/virtalloc.h>
|
#include <sys/virtalloc.h>
|
||||||
|
|
||||||
extern char kernel_start;
|
extern char kernel_start;
|
||||||
extern char kernel_end;
|
extern char kernel_end;
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define MEMORY_INTERNAL
|
#define MEMORY_INTERNAL
|
||||||
#include <memory/virtalloc.h>
|
#include <sys/virtalloc.h>
|
||||||
|
|
||||||
struct addr_node {
|
struct addr_node {
|
||||||
uintptr_t start;
|
uintptr_t start;
|
||||||
|
|
Loading…
Reference in a new issue