summaryrefslogtreecommitdiff
path: root/src/arch
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-02-02 12:21:06 -0500
committerFreya Murphy <freya@freyacat.org>2024-02-02 12:21:06 -0500
commit7e62c50138c43af8f41578c0148762a7bb19a599 (patch)
tree76036d732c69c82f55b50b75527f67be07cd46dc /src/arch
parentbetter print and mem (diff)
downloadcorn-7e62c50138c43af8f41578c0148762a7bb19a599.tar.gz
corn-7e62c50138c43af8f41578c0148762a7bb19a599.tar.bz2
corn-7e62c50138c43af8f41578c0148762a7bb19a599.zip
better fb (wip), format panic, and pci
Diffstat (limited to 'src/arch')
-rw-r--r--src/arch/amd64/fb.c109
-rw-r--r--src/arch/amd64/idt.c25
-rw-r--r--src/arch/amd64/panic.c29
-rw-r--r--src/arch/amd64/pci.c164
4 files changed, 234 insertions, 93 deletions
diff --git a/src/arch/amd64/fb.c b/src/arch/amd64/fb.c
index 9dd2ddf..5e9ea5e 100644
--- a/src/arch/amd64/fb.c
+++ b/src/arch/amd64/fb.c
@@ -2,82 +2,59 @@
#include <memory.h>
#include <serial.h>
#include <stdint.h>
-#include <stddef.h>
#include "bindings.h"
#define INDEX 0x1CE
#define DATA 0x1CF
-#define FB_ADDR 0xE0000000
-#define FB_MAX 0xFF000000
-#define FB_LEN (FB_MAX - FB_ADDR)
-#define PREFERRED_VY 4096
-#define PREFERRED_B 32
+#define INDEX_ID 0
+#define INDEX_XRES 1
+#define INDEX_YRES 2
+#define INDEX_BPP 3
+#define INDEX_ENABLE 4
+#define INDEX_BANK 5
+#define INDEX_VIRT_WIDTH 6
+#define INDEX_VIRT_HEIGHT 7
+#define INDEX_X_OFFSET 8
+#define INDEX_Y_OFFSET 9
-uint16_t fb_res_x = 0;
-uint16_t fb_res_y = 0;
-uint16_t fb_res_b = 0;
+#define DATA_DISP_DISABLE 0x00
+#define DATA_DISP_ENABLE 0x01
+#define DATA_LFB_ENABLE 0x40
+#define DATA_NO_CLEAR_MEM 0x80
-uint8_t *fb_buffer = NULL;
+static void write(uint16_t index, uint16_t data) {
+ outw(INDEX, index);
+ outw(DATA, data);
+}
+
+static uint16_t read(uint16_t value) {
+ outw(INDEX, value);
+ return inw(DATA);
+}
+
+int is_available(void) {
+ return (read(INDEX_ID) >= 0xB0C4);
+}
+
+void set_mode(uint16_t width, uint16_t height, uint16_t bit_depth, int lfb, int clear) {
+ write(INDEX_ENABLE, DATA_DISP_DISABLE);
+ write(INDEX_XRES, width);
+ write(INDEX_YRES, height);
+ write(INDEX_BPP, bit_depth);
+ write(INDEX_ENABLE, DATA_DISP_ENABLE |
+ (lfb ? DATA_LFB_ENABLE : 0) |
+ (clear ? 0 : DATA_NO_CLEAR_MEM));
+}
+
+void set_bank(uint16_t bank) {
+ write(INDEX_BANK, bank);
+}
-int fb_init(uint16_t res_x, uint16_t res_y) {
+int fb_init(uint16_t width, uint16_t height) {
- outw(INDEX, 0x00);
- uint16_t i = inw(DATA);
- if (i < 0xB0C0 || i > 0xB0C6) {
- return -1;
- }
- outw(DATA, 0xB0C4);
- i = inw(DATA);
- /* Disable VBE */
- outw(INDEX, 0x04);
- outw(DATA, 0x00);
- /* Set X resolution to 1024 */
- outw(INDEX, 0x01);
- outw(DATA, res_x);
- /* Set Y resolution to 768 */
- outw(INDEX, 0x02);
- outw(DATA, res_y);
- /* Set bpp to 32 */
- outw(INDEX, 0x03);
- outw(DATA, PREFERRED_B);
- /* Set Virtual Height to stuff */
- outw(INDEX, 0x07);
- outw(DATA, PREFERRED_VY);
- /* Re-enable VBE */
- outw(INDEX, 0x04);
- outw(DATA, 0x41);
+ set_mode(width, height, 32, true, true);
-// uint32_t * text_vid_mem = (uint32_t *)0xA0000;
-// text_vid_mem[0] = 0xA5ADFACE;
-//
-// void *temp = mmap((void *) FB_ADDR, 0xFF1000);
-//
-// for (uintptr_t fb_offset = FB_ADDR; fb_offset < FB_MAX; fb_offset += 0x01000000) {
-// /* Enable the higher memory */
-// remap(temp, (void *)fb_offset, 0xFF1000);
-//
-// /* Go find it */
-// for (uintptr_t offset = 0; offset < 0xFF0000; offset += 0x1000) {
-// uintptr_t x = (uintptr_t)temp + offset;
-// if (((uintptr_t *)x)[0] == 0xA5ADFACE) {
-// fb_buffer = (uint8_t *) (fb_offset + offset);
-// goto mem_found;
-// }
-// }
-// }
-//
-// unmap(temp);
-//
-//mem_found:
-//
-// fb_res_x = res_x;
-// fb_res_y = res_y;
-// fb_res_b = PREFERRED_B;
-//
-// fb_buffer = mmap(fb_buffer, 0xFF0000);
-// memset(fb_buffer, 7, 0xFF0000);
-//
return 0;
}
diff --git a/src/arch/amd64/idt.c b/src/arch/amd64/idt.c
index c0ea5a8..a2ee2a7 100644
--- a/src/arch/amd64/idt.c
+++ b/src/arch/amd64/idt.c
@@ -118,26 +118,25 @@ void idt_exception_handler(uint64_t exception, uint64_t code, struct isr_regs *s
return;
}
- // TODO don't just panic
- char buf[24];
- char msg[256] = "Exception ";
-
- strcat(msg, EXCEPTIONS[exception]);
-
- strcat(msg, "\nError code 0x");
- ultoa(code, buf, 16);
- strcat(msg, buf);
+ char custom[64];
+ *custom = '\0';
// page faults store the offending address in cr2
if (exception == 0x0E) {
- strcat(msg, "\nPage fault address: 0x");
+ strcat(custom, "\nPage fault address: 0x");
void *addr;
__asm__ volatile ("mov %%cr2, %0" : "=r"(addr));
- ultoa((size_t)addr, buf, 16);
- strcat(msg, buf);
+ ultoa((size_t)addr, custom + 23, 16);
}
- panic_interrupt((void *)state->rip, (void *)state->rbp, msg);
+ _panic_interrupt(
+ (void *)state->rip,
+ (void *)state->rbp,
+ "Exception %s\nError code 0x%lu%s",
+ EXCEPTIONS[exception],
+ code,
+ custom
+ );
}
void idt_pic_eoi(uint8_t exception) {
diff --git a/src/arch/amd64/panic.c b/src/arch/amd64/panic.c
index 7f54f98..fa750bc 100644
--- a/src/arch/amd64/panic.c
+++ b/src/arch/amd64/panic.c
@@ -1,30 +1,31 @@
#include <panic.h>
#include <backtrace.h>
+#include <stdarg.h>
+#include <lib.h>
-#include "serial.h"
#include "bindings.h"
-_Noreturn void _panic_impl(char *line, char *file, char *msg) {
+void _panic_impl(char *line, char *file, char *format, ...) {
cli();
- serial_out_str("\n\n!!! PANIC !!!\n");
- serial_out_str("In file ");
- serial_out_str(file);
- serial_out_str(" at line ");
- serial_out_str(line);
- serial_out_str(":\n");
- serial_out_str(msg);
- serial_out_str("\n\n");
+ va_list list;
+ va_start(list, msg);
+ kprintf("\n\n!!! PANIC !!!\n");
+ kprintf("In file %s at line %s:\n", file, line);
+ kvprintf(format, list);
+ kprintf("\n\n");
log_backtrace();
while (1) {
halt();
}
}
-_Noreturn void panic_interrupt(void *ip, void *bp, char *msg) {
+void _panic_interrupt(void *ip, void *bp, char *format, ...) {
cli();
- serial_out_str("\n\n!!! PANIC !!!\n");
- serial_out_str(msg);
- serial_out_str("\n\n");
+ va_list list;
+ va_start(list, msg);
+ kprintf("\n\n!!! PANIC !!!\n");
+ kvprintf(format, list);
+ kprintf("\n\n");
log_backtrace_ex(ip, bp);
while (1) {
halt();
diff --git a/src/arch/amd64/pci.c b/src/arch/amd64/pci.c
new file mode 100644
index 0000000..0deac03
--- /dev/null
+++ b/src/arch/amd64/pci.c
@@ -0,0 +1,164 @@
+#include <stdint.h>
+#include <pci.h>
+#include <panic.h>
+#include <lib.h>
+
+#include "bindings.h"
+
+#define CONF_ADDR 0xCF8
+#define CONF_DATA 0xCFC
+
+#define TABLE_LEN 16
+
+struct pci_table_entry {
+ struct pci_device device;
+ uint16_t device_id;
+ uint16_t vendor_id;
+ uint8_t class;
+ uint8_t subclass;
+ uint8_t prog_if;
+ 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 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_rcfg_w(struct pci_device dev, uint8_t offset) {
+ uint32_t dword = pci_rcfg_d(dev, offset);
+ return (uint16_t)((dword >> ((offset & 2) * 8)) & 0xFFFF);
+}
+
+uint8_t pci_rcfg_b(struct pci_device dev, uint8_t offset) {
+ uint32_t dword = pci_rcfg_d(dev, offset);
+ return (uint8_t)((dword >> ((offset & 3) * 8)) & 0xFF);
+}
+
+void pci_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_wcfg_w(struct pci_device dev, uint8_t offset, uint16_t word) {
+ size_t shift = (offset & 2) * 8;
+ 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) {
+ size_t shift = (offset & 3) * 8;
+ uint32_t dword = pci_rcfg_d(dev, offset);
+ dword &= ~(0xFF << shift);
+ dword |= byte << shift;
+ pci_wcfg_d(dev, offset, dword);
+}
+
+static void print_device(struct pci_table_entry *entry) {
+ kprintf(
+ "BUS: %#-4x DEV: %#-4x FUNC: %#-4x ID: %04x:%04x CLASS: %02x:%02x:%02x REV: %#02x\n",
+ entry->device.bus,
+ entry->device.device,
+ entry->device.function,
+ entry->vendor_id,
+ entry->device_id,
+ entry->class,
+ entry->subclass,
+ entry->prog_if,
+ entry->revision
+ );
+}
+
+static struct pci_table_entry *load_device(struct pci_device dev) {
+ if(pci_table_next >= TABLE_LEN) panic("Too many PCI devices: limit is %d", TABLE_LEN);
+ struct pci_table_entry *entry = &pci_table[pci_table_next++];
+ entry->device = dev;
+ uint32_t dword0 = pci_rcfg_d(dev, 0);
+ uint32_t dword2 = pci_rcfg_d(dev, 8);
+
+ entry->device_id = (dword0 >> 16) & 0xFFFF;
+ entry->vendor_id = dword0 & 0xFFFF;
+
+ entry->class = (dword2 >> 24) & 0xFF;
+ entry->subclass = (dword2 >> 16) & 0xFF;
+ entry->prog_if = (dword2 >> 8) & 0xFF;
+ entry->revision = dword2 & 0xFF;
+
+ print_device(entry);
+ return entry;
+}
+
+void pci_init(void) {
+ kprintf("PCI DEVICES\n");
+ pci_table_next = 0;
+ struct pci_device pcidev;
+ for(int bus = 0; bus < 256; bus++) {
+ pcidev.bus = bus;
+ for(int dev = 0; dev < 32; dev++) {
+ pcidev.device = dev;
+ pcidev.function = 0;
+
+ uint16_t vendor = pci_rcfg_w(pcidev, 0);
+ if(vendor == 0xFFFF) continue;
+
+ load_device(pcidev);
+
+ uint8_t header_type = pci_rcfg_b(pcidev, 14);
+
+ if(!(header_type & 0x80)) continue;
+ for(int func = 1; func < 8; func++) {
+ pcidev.function = func;
+
+ uint16_t vendor = pci_rcfg_w(pcidev, 0);
+ if(vendor == 0xFFFF) continue;
+
+ load_device(pcidev);
+ }
+ }
+ }
+ kprintf("\n");
+}
+
+bool pci_findby_class(struct pci_device *dest, uint8_t class, uint8_t subclass, size_t *offset) {
+ size_t o = 0;
+ if(offset == NULL) offset = &o;
+ for(; *offset < pci_table_next; (*offset)++) {
+ struct pci_table_entry *entry = &pci_table[*offset];
+ if(entry->class == class && entry->subclass == subclass) {
+ *dest = entry->device;
+ return true;
+ }
+ }
+ return false;
+}
+
+bool pci_findby_id(struct pci_device *dest, uint16_t device, uint16_t vendor, size_t *offset) {
+ size_t o = 0;
+ if(offset == NULL) offset = &o;
+ for(; *offset < pci_table_next; (*offset)++) {
+ struct pci_table_entry *entry = &pci_table[*offset];
+ if(entry->device_id == device && entry->vendor_id == vendor) {
+ *dest = entry->device;
+ return true;
+ }
+ }
+ return false;
+}