x86: add GDT and IDT

Signed-off-by: Michael Shaw <spencershawiscool@gmail.com>
This commit is contained in:
Michael Shaw 2026-04-13 04:23:14 -04:00
commit e5b55a6ba5
25 changed files with 1211 additions and 13 deletions

0
a.out Normal file
View file

View file

@ -1,5 +1,26 @@
.intel_syntax noprefix .intel_syntax noprefix
.data
.global __gdtr
__gdtr:
.word 0
.quad 0
.global __gdt
__gdt:
.skip 4096
.global __idtr
__idtr:
.word 0
.quad 0
.global __idt
__idt:
.skip 4096
.text
.global cr3_write .global cr3_write
cr3_write: cr3_write:
mov cr3, rdi mov cr3, rdi
@ -10,3 +31,17 @@ cr3_read:
mov rax, cr3 mov rax, cr3
ret ret
.global lgdt
lgdt:
lgdt __gdtr
ret
.global lidt
lidt:
lidt __idtr
ret
.global ltr
ltr:
ltr rdi
ret

View file

@ -4,7 +4,12 @@
#include <kernel/types.h> #include <kernel/types.h>
void cr3_write(); void cr2_write(u64 cr2);
u64 cr2_read();
void cr3_write(u64 cr3);
u64 cr3_read(); u64 cr3_read();
void lidt();
void lgdt();
void ltr();
#endif /* _REG_H_ */ #endif /* _REG_H_ */

View file

@ -5,5 +5,6 @@
#include <kernel/types.h> #include <kernel/types.h>
void arch_stack_init(void *new_stack_addr, size_t stack_size); void arch_stack_init(void *new_stack_addr, size_t stack_size);
void arch_init();
#endif /* _INIT_H_ */ #endif /* _INIT_H_ */

View file

@ -1,2 +1,6 @@
# SPDX-License-Identifier: GPL-2.0-only # SPDX-License-Identifier: GPL-2.0-only
obj-y += amd64_init.o obj-y += amd64_init.o
obj-y += gdt.o
obj-y += idt.o
obj-y += trap.o
obj-y += exception.o

View file

@ -1,4 +1,6 @@
/* SPDX-License-Identifier: GPL-2.0-only */ /* SPDX-License-Identifier: GPL-2.0-only */
#include "descriptor.h"
void yield() void yield()
{ {
for (;;) { for (;;) {
@ -10,3 +12,9 @@ void _panic()
{ {
yield(); yield();
} }
void arch_init()
{
gdt_init();
idt_init();
}

91
amd64/init/descriptor.h Normal file
View file

@ -0,0 +1,91 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _DESCRIPTOR_H_
#define _DESCRIPTOR_H_
#define GDT_ACCESS_DATA_RING0 0b10010010
#define GDT_ACCESS_CODE_RING0 0b10011010
#define GDT_ACCESS_DATA_RING3 0b11110010
#define GDT_ACCESS_CODE_RING3 0b11111010
#define GDT_SEGMENT_CODE_RING0 0x28
#define GDT_SEGMENT_DATA_RING0 0x30
#define GDT_SEGMENT_CODE_RING3 0x38
#define GDT_SEGMENT_DATA_RING3 0x40
#define GATE_INTR 0x8E
#define GATE_TRAP 0x8F
#define GATE_INTR_DPL3 0xEE
#include <kernel/common.h>
struct gdt_desc {
uint16_t limit;
uint16_t base;
uint8_t base_2;
uint8_t access;
uint8_t flags;
uint8_t base_3;
} PACKED;
struct gdt_desc_sys {
uint16_t limit;
uint16_t base;
uint8_t base_2;
uint8_t access;
uint8_t flags;
uint8_t base_3;
uint32_t base_4;
uint32_t reserved;
} PACKED;
struct tss {
uint32_t reserved;
uint64_t rsp0;
uint64_t rsp1;
uint64_t rsp2;
uint64_t reserved2;
uint64_t ist1;
uint64_t ist2;
uint64_t ist3;
uint64_t ist4;
uint64_t ist5;
uint64_t ist6;
uint64_t ist7;
uint64_t reserved3;
uint16_t reserved4;
uint16_t iomap_base;
} PACKED;
struct gdtr {
uint16_t size;
uint64_t offset;
} PACKED;
struct irq_desc {
uint16_t offset_low;
uint16_t segment;
uint8_t ist;
uint8_t attrs;
uint16_t offset_mid;
uint32_t offset_high;
uint32_t reserved;
} PACKED;
struct idtr {
uint16_t size;
uint64_t offset;
} PACKED;
extern struct gdtr __gdtr;
extern struct gdt_desc __gdt[64];
extern struct idtr __idtr;
extern struct irq_desc __idt[64];
void gdt_init();
void idt_init();
#endif /* _DESCRIPTOR_H_ */

42
amd64/init/exception.c Normal file
View file

@ -0,0 +1,42 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include <lib/stdio.h>
static const char *exception_names[] = {
"Divide-by-zero Error",
"Debug",
"Non-maskable Interrupt",
"Breakpoint",
"Overflow",
"Bound Range Exceeded",
"Invalid Opcode",
"Device Not Available",
"Double Fault",
"Coprocessor Segment Overrun",
"Invalid TSS",
"Segment Not Present",
"Stack-Segment Fault",
"General Protection Fault",
"Page Fault",
"Reserved",
"x87 Floating-Point Exception",
"Alignment Check",
"Machine Check",
"SIMD Floating-Point Exception",
"Virtualization Exception",
"Invalid exception",
"Invalid exception",
"Invalid exception",
"Invalid exception",
"Invalid exception",
"Invalid exception",
"Invalid exception",
"Security Exception",
"Reserved",
"Triple Fault",
};
void exception_handler(u64 no, u64 err)
{
panic("(Exception) %s(%02xh)\n", exception_names[no], no);
}

40
amd64/init/gdt.c Normal file
View file

@ -0,0 +1,40 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include "descriptor.h"
#include "../arch/reg.h"
static size_t gdt_index = 0;
void gdt_insert(uint8_t offset, uint8_t access, uint8_t flags)
{
struct gdt_desc *desc = (__gdt + offset);
desc->limit = 0;
desc->base = 0;
desc->base_2 = 0;
desc->base_3 = 0;
desc->access = access;
desc->flags = flags;
gdt_index++;
}
void gdt_init()
{
gdt_insert(0, 0, 0);
gdt_insert(1, GDT_ACCESS_CODE_RING0, 0xA0);
gdt_insert(2, GDT_ACCESS_DATA_RING0, 0xC0);
gdt_insert(3, GDT_ACCESS_CODE_RING0, 0xA0);
gdt_insert(4, GDT_ACCESS_DATA_RING0, 0xC0);
gdt_insert(5, GDT_ACCESS_CODE_RING0, 0xA0);
gdt_insert(6, GDT_ACCESS_DATA_RING0, 0xC0);
gdt_insert(7, GDT_ACCESS_DATA_RING3, 0xC0);
gdt_insert(8, GDT_ACCESS_CODE_RING3, 0xA0);
__gdtr.size = (sizeof(__gdt) - 1);
__gdtr.offset = (uint64_t)__gdt;
lgdt();
krdy("GDT initialized\n");
}

172
amd64/init/idt.c Normal file
View file

@ -0,0 +1,172 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include "descriptor.h"
#include "../arch/reg.h"
#define exception(_i) _exception_##_i
#define exception_decl(_j) void exception(_j)()
#define irq_decl(_i) void _irq_##_i()
#define __irq(_i) _irq_##_i
static bool irq_mapped[16] = { false };
static void *irq_handlers[16] = { NULL };
exception_decl(0x00);
exception_decl(0x01);
exception_decl(0x02);
exception_decl(0x03);
exception_decl(0x04);
exception_decl(0x05);
exception_decl(0x06);
exception_decl(0x07);
exception_decl(0x08);
exception_decl(0x09);
exception_decl(0x0A);
exception_decl(0x0B);
exception_decl(0x0C);
exception_decl(0x0D);
exception_decl(0x0E);
exception_decl(0x0F);
exception_decl(0x10);
exception_decl(0x11);
exception_decl(0x12);
exception_decl(0x13);
exception_decl(0x14);
exception_decl(0x15);
exception_decl(0x16);
exception_decl(0x17);
exception_decl(0x18);
exception_decl(0x19);
exception_decl(0x1A);
exception_decl(0x1B);
exception_decl(0x1C);
exception_decl(0x1D);
exception_decl(0x1E);
exception_decl(0x1F);
irq_decl(0x00);
irq_decl(0x01);
irq_decl(0x02);
irq_decl(0x03);
irq_decl(0x04);
irq_decl(0x05);
irq_decl(0x06);
irq_decl(0x07);
irq_decl(0x08);
irq_decl(0x09);
irq_decl(0x0A);
irq_decl(0x0B);
irq_decl(0x0C);
irq_decl(0x0D);
irq_decl(0x0E);
irq_decl(0x0F);
void idt_insert(uint16_t gate, uint8_t type, uint8_t ist, uint16_t segment, void *func)
{
uintptr_t f = (uintptr_t)(func);
struct irq_desc *irq_desc = (__idt + gate);
irq_desc->offset_low = f & 0xFFFF;
irq_desc->offset_mid = ((f >> 0x10) & 0xFFFF);
irq_desc->offset_high = ((f >> 0x20) & 0xFFFFFFFF);
irq_desc->segment = segment;
irq_desc->ist = ist;
irq_desc->attrs = type;
}
int irq_map(uint8_t irq, void *func)
{
if (irq_mapped[irq])
return 1;
irq_mapped[irq] = true;
irq_handlers[irq] = func;
return 0;
}
void irq_unmap(uint8_t irq)
{
irq_mapped[irq] = false;
irq_handlers[irq] = NULL;
idt_insert(irq + 0x20, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, NULL);
}
int irq_highest_free()
{
for (int i = 14; i >= 0; i--) {
if (!irq_mapped[i])
return i;
}
return -1;
}
void irq_handler(int irq)
{
if (irq_handlers[irq] != NULL) {
void (*handler)() = irq_handlers[irq];
handler();
}
}
void idt_init()
{
idt_insert(0x00, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x00));
idt_insert(0x01, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x01));
idt_insert(0x02, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x02));
idt_insert(0x03, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x03));
idt_insert(0x04, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x04));
idt_insert(0x05, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x05));
idt_insert(0x06, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x06));
idt_insert(0x07, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x07));
idt_insert(0x08, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x08));
idt_insert(0x09, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x09));
idt_insert(0x0A, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0A));
idt_insert(0x0B, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0B));
idt_insert(0x0C, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0C));
idt_insert(0x0D, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0D));
idt_insert(0x0E, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0E));
idt_insert(0x0F, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0F));
idt_insert(0x10, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x10));
idt_insert(0x11, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x11));
idt_insert(0x12, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x12));
idt_insert(0x13, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x13));
idt_insert(0x14, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x14));
idt_insert(0x15, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x15));
idt_insert(0x16, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x16));
idt_insert(0x17, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x17));
idt_insert(0x18, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x18));
idt_insert(0x19, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x19));
idt_insert(0x1A, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1A));
idt_insert(0x1B, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1B));
idt_insert(0x1C, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1C));
idt_insert(0x1D, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1D));
idt_insert(0x1E, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1E));
idt_insert(0x1F, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1F));
idt_insert(0x20, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x00));
idt_insert(0x21, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x01));
idt_insert(0x22, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x02));
idt_insert(0x23, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x03));
idt_insert(0x24, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x04));
idt_insert(0x25, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x05));
idt_insert(0x26, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x06));
idt_insert(0x27, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x07));
idt_insert(0x28, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x08));
idt_insert(0x29, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x09));
idt_insert(0x2A, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0A));
idt_insert(0x2B, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0B));
idt_insert(0x2C, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0C));
idt_insert(0x2D, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0D));
idt_insert(0x2E, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0E));
idt_insert(0x2F, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0F));
__idtr.size = sizeof(__idt) - 1;
__idtr.offset = (uint64_t)__idt;
lidt();
krdy("IDT loaded\n");
}

68
amd64/init/trap.S Normal file
View file

@ -0,0 +1,68 @@
/* SPDX-License-Identifier: GPL-2.0-only */
.intel_syntax noprefix
.macro exception nr
.global _exception_\nr
_exception_\nr:
mov rdi, \nr
call exception_handler
iretq
.endm
.macro irq nr
.global _irq_\nr
_irq_\nr:
mov rdi, \nr
call irq_handler
iretq
.endm
exception 0x00
exception 0x01
exception 0x02
exception 0x03
exception 0x04
exception 0x05
exception 0x06
exception 0x07
exception 0x08
exception 0x09
exception 0x0A
exception 0x0B
exception 0x0C
exception 0x0D
exception 0x0E
exception 0x0F
exception 0x10
exception 0x11
exception 0x12
exception 0x13
exception 0x14
exception 0x15
exception 0x16
exception 0x17
exception 0x18
exception 0x19
exception 0x1A
exception 0x1B
exception 0x1C
exception 0x1D
exception 0x1E
exception 0x1F
irq 0x00
irq 0x01
irq 0x02
irq 0x03
irq 0x04
irq 0x05
irq 0x06
irq 0x07
irq 0x08
irq 0x09
irq 0x0A
irq 0x0B
irq 0x0C
irq 0x0D
irq 0x0E
irq 0x0F

View file

@ -1,2 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-only # SPDX-License-Identifier: GPL-2.0-only
obj-y += acpi/
obj-y += tty/ obj-y += tty/
obj-y += ahci.o

2
drivers/acpi/Makefile Normal file
View file

@ -0,0 +1,2 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += acpi.o

73
drivers/acpi/acpi.c Normal file
View file

@ -0,0 +1,73 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include <kernel/mem.h>
#include <kernel/requests.h>
#include <drivers/acpi.h>
#include <lib/string.h>
struct rsdp {
/* REV 0 */
char signature[8];
u8 checksum;
char oem_id[6];
u8 revision;
u32 rsdt_address;
/* REV: 2 */
u32 length;
u64 xsdt_address;
u8 extended_checksum;
u8 reserved[3];
} PACKED;
struct rsdt *rsdt = NULL;
struct xsdt *xsdt = NULL;
void *find_table(const char *signature)
{
u32 entries = (xsdt->header.length - sizeof(struct acpi_header)) / 8;
for (u32 i = 0; i < entries; i++) {
struct acpi_header *h;
if (xsdt)
h = (struct acpi_header *)TO_VIRT((xsdt->tables[i]));
else
h = (struct acpi_header *)TO_VIRT((rsdt->tables[i]));
if (memcmp(h->signature, signature, 4) == 0)
return h;
}
return NULL;
}
void acpi_load_pci()
{
struct acpi_mcfg *mcfg = find_table("MCFG");
u32 n_entries = (mcfg->header.length - sizeof(struct acpi_header) - 8) / sizeof(struct acpi_mcfg_entry);
for (u32 i = 0; i < n_entries; i++) {
struct acpi_mcfg_entry *entry = &mcfg->entries[i];
u64 pci_virt_base = TO_VIRT(entry->base_address);
kdebug("MCFG entry: base=%p, start_bus=%d,end_bus=%d,seg_group=%d\n", pci_virt_base, entry->start_bus_number,
entry->end_bus_number, entry->pci_segment_group);
}
}
void acpi_init()
{
struct rsdp *rsdp;
rsdp = kern_rsdp_res->address;
if (kern_rsdp_res->revision == 3) {
vaddr_t p = (paddr_t)rsdp | hhdm_start;
rsdp = (void *)p;
}
rsdt = (struct rsdt *)(rsdp->rsdt_address | hhdm_start);
if (rsdp->revision == 2)
xsdt = (struct xsdt *)(rsdp->xsdt_address | hhdm_start);
acpi_load_pci();
}

215
drivers/ahci.c Normal file
View file

@ -0,0 +1,215 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include <kernel/mem.h>
#include <kernel/slab.h>
#include <lib/string.h>
#include <lib/stdio.h>
#include <drivers/ahci.h>
static uint8_t sata_device_count = 0;
static struct sata_device sata_devices[32] = { 0 };
static paddr_t ahci_pbase = 0;
static vaddr_t ahci_vbase = 0;
static int ahci_port_type(hbaport_t *port)
{
uint32_t ssts = port->ssts;
uint8_t det = ssts & 0x0F;
if (det != HBA_PORT_DET_PRESENT)
return AHCI_DEV_NULL;
if (port->clb == 0)
return AHCI_DEV_NULL;
switch (port->sig) {
case SATA_SIG_ATAPI:
return AHCI_DEV_SATAPI;
case SATA_SIG_SEMB:
return AHCI_DEV_SEMB;
case SATA_SIG_PM:
return AHCI_DEV_PM;
default:
return AHCI_DEV_SATA;
}
}
static void halt_cmd(hbaport_t *port)
{
port->cmd &= ~HBA_PORT_CMD_ST;
port->cmd &= ~HBA_PORT_CMD_FRE;
while (port->cmd & (HBA_PORT_CMD_FR | HBA_PORT_CMD_CR))
;
}
static void start_cmd(hbaport_t *port)
{
while (port->cmd & HBA_PORT_CMD_CR)
;
port->cmd |= HBA_PORT_CMD_FRE;
port->cmd |= HBA_PORT_CMD_ST;
}
static void ahci_rebase(hbaport_t *port, int n)
{
halt_cmd(port);
port->clb = (ahci_pbase) + (n << 10);
memset((void *)(port->clb | hhdm_start), 0, 1024);
port->fb = ahci_pbase + (32 << 10) + (n << 8);
memset((void *)(port->fb | hhdm_start), 0, 256);
struct hba_cmd_header *cmdheader = (struct hba_cmd_header *)(port->clb | hhdm_start);
for (int i = 0; i < 32; i++) {
cmdheader[i].prdt_len = 8;
cmdheader[i].cmd_table_base = (ahci_pbase) + (n << 13) + (40 << 10) + (i << 8);
void *ctba = (void *)((uintptr_t)cmdheader[i].cmd_table_base | hhdm_start);
memset(ctba, 0, 256);
}
start_cmd(port);
}
static void ahci_probe_ports(struct pci_device *dev, hbamem_t *abar)
{
uint32_t pi = abar->pi;
for (int i = 0; i < 32; i++) {
if (pi & 1) {
uint32_t type = ahci_port_type(&abar->ports[i]);
if (type == AHCI_DEV_SATA) {
struct sata_device *sdev = &sata_devices[sata_device_count];
sdev->port = i;
sdev->controller = dev;
sdev->abar = abar;
/* char *name = kzalloc(16, ALLOC_KERN);
name[15] = 0;
snprintf(name, "ahci%d", 15, sata_device_count);*/
size_t sector_count = ((size_t)abar->ports[i].ssts) << 32 | abar->ports[i].ssts;
sdev->sector_count = sector_count;
sdev->sector_size = 512;
/* bdev = block_register(name, &ahci_block_ops, sdev, sector_count, sdev->sector_size);
block_gpt_init(bdev);*/
ahci_rebase(&abar->ports[i], i);
sata_device_count++;
kprintf(LOG_INFO "SATA drive found at port %d\n", i);
} else if (type == AHCI_DEV_SATAPI) {
kprintf(LOG_INFO "SATAPI drive found at port %d\n", i);
} else if (type == AHCI_DEV_SEMB) {
kprintf(LOG_INFO "SEMB drive found at port %d\n", i);
} else if (type == AHCI_DEV_PM) {
kprintf(LOG_INFO "PM drive found at port %d\n", i);
}
}
pi >>= 1;
}
}
static int cmdslot(hbaport_t *port)
{
uint32_t slots = (port->sact | port->ci);
for (int i = 0; i < 32; i++) {
if ((slots & 1))
slots >>= 1;
else
return i;
}
return -1;
}
int ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf, bool write)
{
hbaport_t *port = &dev->abar->ports[dev->port];
/* clear pending interrupt */
port->is = (uint32_t)-1;
int spin = 0;
int slot = cmdslot(port);
if (slot == -1)
return false;
struct hba_cmd_header *cmdheader = (struct hba_cmd_header *)(port->clb | hhdm_start);
cmdheader += slot;
cmdheader->cmd_fis_len = sizeof(struct fis_reg_h2d) / sizeof(uint32_t);
cmdheader->write = 0;
cmdheader->prdt_len = (uint16_t)((count - 1) >> 4) + 1;
struct hba_cmd_tbl *cmdtbl = (struct hba_cmd_tbl *)(cmdheader->cmd_table_base | hhdm_start);
memset(cmdtbl, 0, sizeof(struct hba_cmd_tbl) + (cmdheader->prdt_len - 1) * sizeof(struct hba_prdt_entry));
struct fis_reg_h2d *cmdfis = (struct fis_reg_h2d *)(&cmdtbl->cfis);
cmdfis->fis_type = FIS_REG_H2D;
cmdfis->cmd_mode = 1;
cmdfis->command = write ? ATA_CMD_WRITE_DMA_EX : ATA_CMD_READ_DMA_EX;
cmdfis->lba0 = (uint8_t)lba;
cmdfis->lba1 = (uint8_t)(lba >> 8);
cmdfis->lba2 = (uint8_t)(lba >> 16);
cmdfis->device = 1 << 6; /* LBA mode */
cmdfis->lba3 = (uint8_t)(lba >> 24);
cmdfis->lba4 = (uint8_t)(lba >> 32);
cmdfis->lba5 = (uint8_t)(lba >> 40);
cmdfis->count_low = count & 0xff;
cmdfis->count_high = (count >> 8) & 0xff;
int i;
for (i = 0; i < cmdheader->prdt_len; i++) {
cmdtbl->prdt_entry[i].data_base = buf;
cmdtbl->prdt_entry[i].byte_count = 8 * 1024 - 1;
cmdtbl->prdt_entry[i].interrupt = 1;
buf += 8 * 1024;
count -= 16;
}
/* wait until port is free */
while ((port->tfd & (ATA_DEV_BUSY | ATA_DEV_DRQ)) && spin < 1000000) {
spin++;
}
if (spin == 1000000) {
kprintf(LOG_ERROR "AHCI: Port is hung\n");
return false;
}
/* issue command */
port->ci = 1 << slot;
/* wait for completion */
while (1) {
if (port->is & HBA_PORT_IS_TFES) {
kprintf(LOG_ERROR "AHCI: Read disk error\n");
return false;
}
if ((port->ci & (1 << slot)) == 0)
break;
/* ELSE IF: reschedule */
}
/* check again */
if (port->is & HBA_PORT_IS_TFES) {
kprintf(LOG_ERROR "AHCI: Read disk error\n");
return false;
}
return true;
}
void ahci_init()
{
}

1
drivers/pci/pci.c Normal file
View file

@ -0,0 +1 @@
/* SPDX-License-Identifier: GPL-2.0-only */

45
include/drivers/acpi.h Normal file
View file

@ -0,0 +1,45 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _ACPI_H_
#define _ACPI_H_
#include <kernel/common.h>
struct acpi_header {
char signature[4];
u32 length;
u8 revision;
u8 checksum;
char oem_id[6];
char oem_table_id[8];
u32 oem_revision;
u32 creator_id;
u32 creator_revision;
} PACKED;
struct rsdt {
struct acpi_header header;
u32 tables[];
};
struct xsdt {
struct acpi_header header;
u64 tables[];
} PACKED;
struct acpi_mcfg_entry {
u64 base_address;
u16 pci_segment_group;
u8 start_bus_number;
u8 end_bus_number;
u32 reserved;
} PACKED;
struct acpi_mcfg {
struct acpi_header header;
u64 reserved;
struct acpi_mcfg_entry entries[];
} PACKED;
void acpi_init();
#endif /* _ACPI_H_ */

344
include/drivers/ahci.h Normal file
View file

@ -0,0 +1,344 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _AHCI_H_
#define _AHCI_H_
#include <kernel/common.h>
/* Frame information structure (FIS) */
#define FIS_REG_H2D 0x27
#define FIS_REG_D2H 0x34
#define FIS_DMA_ACT 0x39
#define FIS_DMA_SETUP 0x41
#define FIS_DATA 0x46
#define FIS_BIST 0x58
#define FIS_PIO_SETUP 0x5F
#define FIS_DEV_BITS 0xA1
#define ATA_DEV_BUSY 0x80
#define ATA_DEV_DRQ 0x08
#define ATA_CMD_READ_DMA_EX 0x25
#define ATA_CMD_WRITE_DMA_EX 0x35
#define ATA_CMD_IDENT 0xEC
#define SATA_SIG_ATA 0x00000101
#define SATA_SIG_ATAPI 0xEB140101
#define SATA_SIG_SEMB 0xC33C0101
#define SATA_SIG_PM 0x96690101
#define AHCI_DEV_NULL 0
#define AHCI_DEV_SATA 1
#define AHCI_DEV_SEMB 2
#define AHCI_DEV_PM 3
#define AHCI_DEV_SATAPI 4
#define AHCI_GHC_IE 0x2
#define AHCI_GHC_AE 0x80000000
#define HBA_PORT_IPM_ACTIVE 0x1
#define HBA_PORT_DET_PRESENT 0x3
#define HBA_PORT_CMD_ST 0x1
#define HBA_PORT_CMD_FRE 0x10
#define HBA_PORT_CMD_FR 0x4000
#define HBA_PORT_CMD_CR 0x8000
#define HBA_PORT_IS_TFES 0x40000000
struct fis_reg_h2d {
/* 0x00 */
uint8_t fis_type;
uint8_t pmult : 4;
uint8_t reserved : 3;
uint8_t cmd_mode : 1; /* high: command, low: control */
uint8_t command;
uint8_t feature_low;
/* 0x04 */
uint8_t lba0;
uint8_t lba1;
uint8_t lba2;
uint8_t device;
/* 0x08 */
uint8_t lba3;
uint8_t lba4;
uint8_t lba5;
uint8_t feature_high;
/* 0x0C */
uint8_t count_low;
uint8_t count_high;
uint8_t icc;
uint8_t control;
/* 0x10 */
uint32_t reserved2;
} PACKED;
struct fis_reg_d2h {
/* 0x00 */
uint8_t fis_type;
uint8_t pmult : 4;
uint8_t reserved : 2;
uint8_t intr : 1;
uint8_t reserved2 : 1;
uint8_t status;
uint8_t error;
/* 0x04 */
uint8_t lba0;
uint8_t lba1;
uint8_t lba2;
uint8_t device;
/* 0x08 */
uint8_t lba3;
uint8_t lba4;
uint8_t lba5;
uint8_t feature_high;
/* 0x0C */
uint8_t count_low;
uint8_t count_high;
uint8_t icc;
uint8_t control;
/* 0x10 */
uint32_t reserved3;
} PACKED;
struct fis_data {
/* 0x00 */
uint8_t fis_type;
uint8_t pmult : 4;
uint8_t reserved : 4;
uint8_t reserved2[2];
/* 0x04 */
uint32_t data[1]; /* Variable length */
} PACKED;
struct fis_dma_setup {
/* 0x00 */
uint8_t fis_type;
uint8_t pmult : 4;
uint8_t reserved : 1;
uint8_t data_dir : 1;
uint8_t intr : 1;
uint8_t auto_act : 1;
uint16_t reserved2;
/* 0x04 */
uint64_t dma_buffer_id;
/* 0x0C */
uint32_t reserved3;
/* 0x10 */
uint32_t dma_buffer_offset;
/* 0x14 */
uint32_t transfer_count;
/* 0x18 */
uint32_t reserved4;
} PACKED;
struct fis_pio_setup {
/* 0x00 */
uint8_t fis_type;
uint8_t pmult : 4;
uint8_t reserved : 1;
uint8_t data_dir : 1;
uint8_t intr : 1;
uint8_t reserved2 : 1;
uint8_t status;
uint8_t error;
/* 0x04 */
uint8_t lba0;
uint8_t lba1;
uint8_t lba2;
uint8_t device;
/* 0x08 */
uint8_t lba3;
uint8_t lba4;
uint8_t lba5;
uint8_t reserved3;
/* 0x0C */
uint8_t count_low;
uint8_t count_high;
uint8_t reserved4;
uint8_t e_status;
/* 0x10 */
uint16_t transfer_count;
uint16_t reserved5;
} PACKED;
struct fis_set_device_bits {
/* 0x00 */
uint8_t fis_type;
uint8_t pmult : 4;
uint8_t reserved : 2;
uint8_t interrupt : 1;
uint8_t n : 1;
uint8_t statusl : 3;
uint8_t reserved2 : 1;
uint8_t statush : 3;
uint8_t reserved3 : 1;
/* 0x04 */
uint8_t errorl : 3;
} PACKED;
struct hba_cmd_header {
uint8_t cmd_fis_len : 5;
uint8_t atapi : 1;
uint8_t write : 1;
uint8_t prefetch : 1;
uint8_t reset : 1;
uint8_t bist : 1;
uint8_t clear_busy : 1;
uint8_t reserved : 1;
uint8_t pmp : 4;
uint16_t prdt_len;
volatile uint32_t prdb_count;
paddr_t cmd_table_base;
uint32_t reserved2[4];
} PACKED;
struct hba_prdt_entry {
paddr_t data_base;
uint32_t reserved;
uint32_t byte_count : 22;
uint32_t reserved2 : 9;
uint32_t interrupt : 1;
} PACKED;
struct hba_cmd_tbl {
uint8_t cfis[64];
uint8_t acmd[16];
uint8_t reserved[48];
struct hba_prdt_entry prdt_entry[1];
} PACKED;
typedef volatile struct {
/* 0x00 */
paddr_t clb;
paddr_t fb;
/* 0x10 */
uint32_t is;
uint32_t ie;
uint32_t cmd;
uint32_t reserved;
/* 0x20 */
uint32_t tfd;
uint32_t sig;
uint32_t ssts;
uint32_t sctl;
/* 0x30 */
uint32_t serr;
uint32_t sact;
uint32_t ci;
uint32_t sntf;
/* 0x40 */
uint32_t fbs;
uint32_t reserved1[11];
uint32_t vendor[4];
} PACKED hbaport_t;
typedef volatile struct {
/* 0x00 */
uint32_t cap;
uint32_t ghc;
uint32_t is;
uint32_t pi;
uint32_t vs;
/* 0x14 */
uint32_t ccc_ctl;
uint32_t ccc_ports;
uint32_t em_loc;
uint32_t em_ctl;
uint32_t cap2;
/* 0x28 */
uint32_t bohc;
/* 0x2C */
uint8_t reserved[0x74];
/* 0xA0 */
uint8_t vendor[0x60];
/* 0x100 */
hbaport_t ports[32];
} PACKED hbamem_t;
typedef volatile struct {
/* 0x00 */
struct fis_dma_setup dfis;
uint32_t reserved;
/* 0x20 */
struct fis_pio_setup pfis;
uint32_t reserved1[3];
/* 0x40 */
struct fis_reg_d2h rfis;
uint32_t reserved2;
struct fis_set_device_bits sdbfis;
/* 0x60 */
uint8_t ufis[64];
/* 0xA0 */
uint8_t reserved3[0x60];
} PACKED hbafis_t;
struct sata_device {
struct pci_device *controller;
uint8_t port;
size_t sector_size;
size_t sector_count;
hbamem_t *abar;
};
int ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf, bool write);
void ahci_init();
#endif /* _AHCI_H_ */

19
include/drivers/pci.h Normal file
View file

@ -0,0 +1,19 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _PCI_H_
#define _PCI_H_
#include <kernel/common.h>
struct pci_device {
u64 class;
u64 vendor;
u8 bus;
u8 slot;
u8 func;
paddr_t bar0;
paddr_t bar1;
paddr_t bar2;
paddr_t bar3;
} PACKED;
#endif /* _PCI_H_ */

View file

@ -7,6 +7,7 @@
extern struct limine_executable_address_response *kern_addr_res; extern struct limine_executable_address_response *kern_addr_res;
extern struct limine_memmap_response *kern_map_res; extern struct limine_memmap_response *kern_map_res;
extern struct limine_hhdm_response *kern_hhdm_res; extern struct limine_hhdm_response *kern_hhdm_res;
extern struct limine_rsdp_response *kern_rsdp_res ;
void requests_init(); void requests_init();

View file

@ -10,6 +10,7 @@
#include <drivers/tty.h> #include <drivers/tty.h>
#include <drivers/serial.h> #include <drivers/serial.h>
#include <drivers/acpi.h>
#include <drivers/apic.h> #include <drivers/apic.h>
@ -20,16 +21,13 @@
struct tty *tty_printf = NULL; struct tty *tty_printf = NULL;
void kmain() static void requires_bootloader_mem()
{ {
/* init tty ASAP */ acpi_init();
requests_init(); }
mem_init();
buddy_init();
void *ap_stack = buddy_alloc(P_STACK_SIZE);
arch_stack_init(ap_stack, P_STACK_SIZE);
static void kmain()
{
rbt_slab_init(); rbt_slab_init();
device_layer_init(); device_layer_init();
tty_layer_init(); tty_layer_init();
@ -46,8 +44,29 @@ void kmain()
tty_set_cursor_pos(tty_printf, 0, 0); tty_set_cursor_pos(tty_printf, 0, 0);
/* start the kernel */ /* start the kernel */
arch_init();
requires_bootloader_mem();
page_init(); page_init();
kprintf(COL_CYAN_BLACK "Welcome to vbos!\n" COL_RESET); kprintf(COL_CYAN_BLACK "Welcome to vbos!\n" COL_RESET);
yield(); yield();
} }
void _before_stack()
{
/* these first few functions must be called in this order
*
* after calling arch_stack_init, we will lose all of our stack
* variables. only the most minimum things needed to init the stack
* here.
*/
requests_init();
mem_init();
buddy_init();
void *ap_stack = buddy_alloc(P_STACK_SIZE);
arch_stack_init(ap_stack, P_STACK_SIZE);
/* end previous note */
kmain();
}

View file

@ -3,17 +3,19 @@
#include "limine/limine.h" #include "limine/limine.h"
#include <kernel/common.h> #include <kernel/common.h>
#define REQUEST __attribute__((used, section(".limine_requests"))) static volatile #define REQUEST __attribute__((used, section(".limine_requests"))) volatile
REQUEST struct limine_executable_address_request kern_addr_req = { .id = LIMINE_EXECUTABLE_ADDRESS_REQUEST_ID, .revision = 0 }; REQUEST struct limine_executable_address_request kern_addr_req = { .id = LIMINE_EXECUTABLE_ADDRESS_REQUEST_ID, .revision = 0 };
REQUEST struct limine_memmap_request memmap_req = { .id = LIMINE_MEMMAP_REQUEST_ID, .revision = 0 }; REQUEST struct limine_memmap_request memmap_req = { .id = LIMINE_MEMMAP_REQUEST_ID, .revision = 0 };
REQUEST struct limine_hhdm_request hhdm_req = { .id = LIMINE_HHDM_REQUEST_ID, .revision = 0 }; REQUEST struct limine_hhdm_request hhdm_req = { .id = LIMINE_HHDM_REQUEST_ID, .revision = 0 };
REQUEST struct limine_rsdp_request rsdp_req = { .id = LIMINE_RSDP_REQUEST_ID, .revision = 0 };
REQUEST u64 limine_base_revision[] = LIMINE_BASE_REVISION(6); REQUEST u64 limine_base_revision[] = LIMINE_BASE_REVISION(6);
struct limine_executable_address_response *kern_addr_res = NULL; struct limine_executable_address_response *kern_addr_res = NULL;
struct limine_memmap_response *kern_map_res = NULL; struct limine_memmap_response *kern_map_res = NULL;
struct limine_hhdm_response *kern_hhdm_res = NULL; struct limine_hhdm_response *kern_hhdm_res = NULL;
struct limine_rsdp_response *kern_rsdp_res = NULL;
vaddr_t hhdm_start = 0; vaddr_t hhdm_start = 0;
@ -22,6 +24,7 @@ void requests_init()
kern_addr_res = kern_addr_req.response; kern_addr_res = kern_addr_req.response;
kern_map_res = memmap_req.response; kern_map_res = memmap_req.response;
kern_hhdm_res = hhdm_req.response; kern_hhdm_res = hhdm_req.response;
kern_rsdp_res = rsdp_req.response;
hhdm_start = kern_hhdm_res->offset; hhdm_start = kern_hhdm_res->offset;
} }

View file

@ -2,7 +2,7 @@
OUTPUT_FORMAT(elf64-x86-64) OUTPUT_FORMAT(elf64-x86-64)
OUTPUT_ARCH(i386:x86-64) OUTPUT_ARCH(i386:x86-64)
ENTRY(kmain) ENTRY(_before_stack)
PHDRS PHDRS
{ {

View file

@ -4,7 +4,10 @@
# #
# Important to remember that this is run at the project-root-level, # Important to remember that this is run at the project-root-level,
# all of it, as it is invoked with $(MAKE) -f # all of it, as it is invoked with $(MAKE) -f
include .config ifneq ($(shell test -e .config && echo -n yes),yes)
$(error Please type `make config` to generate a config)
endif
-include .config
export export
obj-y := obj-y :=

View file

@ -23,11 +23,16 @@ run_menuconfig() {
done done
# 2. Show the menu # 2. Show the menu
CHOICES=$(dialog --stdout --backtitle "Project Setup" \ tput smcup
CHOICES=$(dialog --stdout --keep-tite --backtitle "Project Setup" \
--title "$TITLE" \ --title "$TITLE" \
--checklist "Use SPACE to toggle, ENTER to save:" 15 70 10 \ --checklist "Use SPACE to toggle, ENTER to save:" 15 70 10 \
"${DIALOG_ARGS[@]}") "${DIALOG_ARGS[@]}")
tput rmcup
# 3. Save if user didn't cancel # 3. Save if user didn't cancel
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "# Generated Configuration" > "$CONFIG_FILE" echo "# Generated Configuration" > "$CONFIG_FILE"