diff options
author | Marvin Borner | 2020-09-08 15:29:38 +0200 |
---|---|---|
committer | Marvin Borner | 2020-09-08 15:29:38 +0200 |
commit | 36d7c86a1e9e72d689d672a0e8576adbc740d541 (patch) | |
tree | b4556b37b0f82d8e41b0ff43ec444c71fca0ccbd | |
parent | 9c2f40441e0cc909ebefe432ddc10e2de29b82ac (diff) |
Some work on CPU stuff (soon: SMP)
-rw-r--r-- | kernel/drivers/acpi.c | 49 | ||||
-rw-r--r-- | kernel/inc/acpi.h | 79 | ||||
-rw-r--r-- | kernel/main.c | 3 | ||||
-rw-r--r-- | libc/cpu.c | 35 | ||||
-rw-r--r-- | libc/inc/cpu.h | 6 | ||||
-rwxr-xr-x | run | 2 |
6 files changed, 167 insertions, 7 deletions
diff --git a/kernel/drivers/acpi.c b/kernel/drivers/acpi.c index 4c7a1a8..e853a6f 100644 --- a/kernel/drivers/acpi.c +++ b/kernel/drivers/acpi.c @@ -36,9 +36,10 @@ struct rsdp *find_rsdp() } // Or first KB of EBDA? - for (int i = 0x100000; i < 0x101000; i++) { - if (memcmp((u32 *)i, RSDP_MAGIC, 8) == 0) - return (struct rsdp *)i; + u8 *ebda = (void *)(*((u16 *)0x40E) << 4); + for (int i = 0; i < 1024; i += 16) { + if (memcmp(ebda + i, RSDP_MAGIC, 8) == 0) + return (struct rsdp *)(ebda + i); } return NULL; @@ -72,6 +73,8 @@ void acpi_install() madt = find_sdt(rsdt, MADT_MAGIC); fadt = find_sdt(rsdt, FADT_MAGIC); hpet = find_sdt(rsdt, HPET_MAGIC); + + madt_install(); } void hpet_install(int period) @@ -91,3 +94,43 @@ void hpet_install(int period) hpet = NULL; } } + +void madt_install() +{ + if (!madt) + return; + + struct madt_entry_header *entry = &madt->entry; + while (entry && entry->length) { + switch (entry->type) { + case MADT_LOCAL_APIC_ENTRY: { + struct madt_local_apic_entry *table = (struct madt_local_apic_entry *)entry; + printf("CPU %b\n", table->flags); + break; + } + case MADT_IO_APIC_ENTRY: { + /* struct madt_io_apic_entry *table = (struct madt_io_apic_entry *)entry; */ + break; + } + case MADT_INT_SRC_OVERRIDE_ENTRY: { + /* struct madt_int_src_override_entry *table = */ + /* (struct madt_int_src_override_entry *)entry; */ + break; + } + case MADT_NON_MASKABLE_INT_ENTRY: { + /* struct madt_non_maskable_int_entry *table = */ + /* (struct madt_non_maskable_int_entry *)entry; */ + break; + } + case MADT_LOCAL_APIC_OVERRIDE_ENTRY: { + /* struct madt_local_apic_override_entry *table = */ + /* (struct madt_local_apic_override_entry *)entry; */ + break; + } + default: { + break; + } + } + entry = (struct madt_entry_header *)((u32)entry + entry->length); + } +} diff --git a/kernel/inc/acpi.h b/kernel/inc/acpi.h index 6cebf9b..4e04037 100644 --- a/kernel/inc/acpi.h +++ b/kernel/inc/acpi.h @@ -21,6 +21,10 @@ struct address_structure { u32 phys; // Actually u64 }; +/** + * General headers + */ + struct sdt_header { char signature[4]; u32 length; @@ -40,21 +44,85 @@ struct sdp_header { u8 revision; }; +/** + * RSDT + */ + struct rsdt { struct sdt_header header; u32 sdt_pointer[]; }; +/** + * MADT + */ + +struct madt_entry_header { + u8 type; + u8 length; +}; + struct madt { struct sdt_header header; u32 local_address; u32 flags; -}; + struct madt_entry_header entry; +} __attribute__((packed)); + +#define MADT_LOCAL_APIC_ENTRY 0 +#define MADT_IO_APIC_ENTRY 1 +#define MADT_INT_SRC_OVERRIDE_ENTRY 2 +#define MADT_NON_MASKABLE_INT_ENTRY 4 // Where's 3? +#define MADT_LOCAL_APIC_OVERRIDE_ENTRY 5 + +struct madt_local_apic_entry { + struct madt_entry_header header; + u8 processor_id; + u8 id; + u32 flags; +} __attribute__((packed)); + +struct madt_io_apic_entry { + struct madt_entry_header header; + u8 id; + u8 reserved; + u32 address; + u32 global_system_interrupt_base; +} __attribute__((packed)); + +struct madt_int_src_override_entry { + struct madt_entry_header header; + u8 bus_source; + u8 irq_source; + u32 global_system_interrupt; + u16 flags; +} __attribute__((packed)); + +struct madt_non_maskable_int_entry { + struct madt_entry_header header; + u8 processor_id; + u16 flags; + u8 lint_number; +} __attribute__((packed)); + +struct madt_local_apic_override_entry { + struct madt_entry_header header; + u16 reserved; + u64 address; +} __attribute__((packed)); + +/** + * FADT + */ struct fadt { struct sdt_header header; // TODO: FADT table (big!) -}; +} __attribute__((packed)); + +/** + * HPET + */ struct hpet { struct sdt_header header; @@ -68,7 +136,7 @@ struct hpet { u8 hpet_number; u16 minimum_tick; u8 page_protection; -}; +} __attribute__((packed)); enum hpet_features { hpet_counter_size = 1 << 3, hpet_legacy_replacement_support = 1 << 5 }; enum hpet_config { hpet_enable = 1 << 0, hpet_legacy_replacement = 1 << 1 }; @@ -102,6 +170,10 @@ struct hpet_registers { u64 timer_comparator0; // In femtoseconds } __attribute__((packed)); +/** + * RSDP + */ + struct rsdp { struct sdp_header header; struct rsdt *rsdt; @@ -113,5 +185,6 @@ struct hpet *hpet; void acpi_install(); void hpet_install(int frequency); +void madt_install(); #endif diff --git a/kernel/main.c b/kernel/main.c index 34332e2..880c66c 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -9,6 +9,7 @@ #include <load.h> #include <mem.h> #include <mouse.h> +#include <print.h> #include <serial.h> #include <syscall.h> #include <timer.h> @@ -23,6 +24,8 @@ void kernel_main(struct vid_info *vid_info) serial_install(); serial_print("\nConnected.\n"); + cpu_print(); + // Install drivers acpi_install(); interrupts_install(); @@ -1,7 +1,9 @@ // MIT License, Copyright (c) 2020 Marvin Borner // This file is a wrapper around some CPU asm calls +#include <cpu.h> #include <def.h> +#include <print.h> u8 inb(u16 port) { @@ -47,6 +49,39 @@ void outl(u16 port, u32 data) __asm__ volatile("outl %0, %1" ::"a"(data), "Nd"(port)); } +void cpuid(int code, u32 *a, u32 *b, u32 *c, u32 *d) +{ + __asm__ volatile("cpuid" : "=a"(*a), "=b"(*b), "=c"(*c), "=d"(*d) : "a"(code)); +} + +char *cpu_string(char buf[12]) +{ + u32 a, b, c, d; + cpuid(CPUID_VENDOR_STRING, &a, &b, &c, &d); + char *ebx = (char *)&b; + char *ecx = (char *)&c; + char *edx = (char *)&d; + buf[0] = ebx[0]; + buf[1] = ebx[1]; + buf[2] = ebx[2]; + buf[3] = ebx[3]; + buf[4] = edx[0]; + buf[5] = edx[1]; + buf[6] = edx[2]; + buf[7] = edx[3]; + buf[8] = ecx[0]; + buf[9] = ecx[1]; + buf[10] = ecx[2]; + buf[11] = ecx[3]; + return buf; +} + +void cpu_print() +{ + char buf[12] = { 0 }; + printf("%s\n", cpu_string(buf)); +} + #ifdef kernel void cli() { diff --git a/libc/inc/cpu.h b/libc/inc/cpu.h index c25dc60..e742390 100644 --- a/libc/inc/cpu.h +++ b/libc/inc/cpu.h @@ -5,6 +5,8 @@ #include <def.h> +enum cpuid_requests { CPUID_VENDOR_STRING, CPUID_FEATURES, CPUID_TLB, CPUID_SERIAL }; + u8 inb(u16 port); u16 inw(u16 port); u32 inl(u16 port); @@ -14,6 +16,10 @@ void outb(u16 port, u8 data); void outw(u16 port, u16 data); void outl(u16 port, u32 data); +void cpuid(int code, u32 *a, u32 *b, u32 *c, u32 *d); +char *cpu_string(char buf[12]); +void cpu_print(); + #ifdef kernel void cli(); void sti(); @@ -10,7 +10,7 @@ no_ask="${2}" network="rtl8139" qemu_with_flags() { - SDL_VIDEO_X11_DGAMOUSE=0 qemu-system-i386 -no-reboot -vga std -rtc base=localtime -m 256M -net nic,model=${network},macaddr=42:42:42:42:42:42 -net user "$@" + SDL_VIDEO_X11_DGAMOUSE=0 qemu-system-i386 -no-reboot -vga std -rtc base=localtime -m 256M -smp 4 -net nic,model=${network},macaddr=42:42:42:42:42:42 -net user "$@" } make_cross() { |