better fb (wip), format panic, and pci

This commit is contained in:
Freya Murphy 2024-02-02 12:21:06 -05:00
parent 95f52a55ad
commit 7e62c50138
Signed by: freya
GPG key ID: 744AB800E383AE52
8 changed files with 306 additions and 98 deletions

View file

@ -3,9 +3,11 @@
#define _PANIC_STR(x) _PANIC_STR2(x)
#define _PANIC_STR2(x) #x
#define panic(msg) _panic_impl(_PANIC_STR(__LINE__), __FILE__, msg)
#define kassert(val, msg) do { if (!(val)) { panic(msg); } } while(0)
#define panic(msg, ...) _panic_impl(_PANIC_STR(__LINE__), __FILE__, msg, ## __VA_ARGS__)
#define kassert(val, msg, ...) do { if (!(val)) { panic(msg, ## __VA_ARGS__); } } while(0)
_Noreturn void _panic_impl(char *line, char *file, char *msg);
__attribute__((noreturn,format(printf, 3, 4)))
void _panic_impl(char *line, char *file, char *msg, ...);
_Noreturn void panic_interrupt(void *ip, void *bp, char *msg);
__attribute__((noreturn,format(printf, 3, 4)))
void _panic_interrupt(void *ip, void *bp, char *msg, ...);

55
include/pci.h Normal file
View file

@ -0,0 +1,55 @@
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
// common
#define PCI_VENDOR_W 0x00
#define PCI_DEVICE_W 0x02
#define PCI_COMMAND_W 0x04
#define PCI_STATUS_W 0x06
#define PCI_REVISION_B 0x08
#define PCI_PROG_IF_B 0x09
#define PCI_SUBCLASS_B 0x0A
#define PCI_CLASS_B 0x0B
#define PCI_CACHE_SIZE_B 0x0C
#define PCI_LATENCY_TIMER_B 0x0D
#define PCI_HEADER_TYPE_B 0x0E
#define PCI_BIST_B 0x0F
// header type 0
#define PCI_BAR0_D 0x10
#define PCI_BAR1_D 0x14
#define PCI_BAR2_D 0x18
#define PCI_BAR3_D 0x1C
#define PCI_BAR4_D 0x20
#define PCI_BAR5_D 0x24
#define PCI_CARDBUS_CIS_D 0x28
#define PCI_SUBSYSTEM_VENDOR_W 0x2C
#define PCI_SUBSYSTEM_W 0x2E
#define PCI_EXPANSION_ROM_D 0x30
#define PCI_CAP_PTR_B 0x34
#define PCI_INT_LINE_B 0x3C
#define PCI_INT_PIN_B 0x3D
#define PCI_MIN_GRANT_B 0x3E
#define PCI_MAX_LATENCY_B 0x3F
struct pci_device {
uint8_t bus: 8;
uint8_t device: 5;
uint8_t function: 3;
};
void pci_init(void);
bool pci_findby_class(struct pci_device *dest, uint8_t class, uint8_t subclass, size_t *offset);
bool pci_findby_id(struct pci_device *dest, uint16_t device, uint16_t vendor, size_t *offset);
uint32_t pci_rcfg_d(struct pci_device dev, uint8_t offset);
uint16_t pci_rcfg_w(struct pci_device dev, uint8_t offset);
uint8_t pci_rcfg_b(struct pci_device dev, uint8_t offset);
void pci_wcfg_d(struct pci_device dev, uint8_t offset, uint32_t dword);
void pci_wcfg_w(struct pci_device dev, uint8_t offset, uint16_t word);
void pci_wcfg_b(struct pci_device dev, uint8_t offset, uint8_t byte);

View file

@ -15,6 +15,14 @@ struct memory_map {
struct memory_segment entries[MMAP_MAX_ENTRY];
};
//struct framebuffer {
// uint64_t addr;
// uint32_t pitch;
// uint32_t width;
// uint32_t height;
// uint8_t bit_depth;
//};
struct boot_info {
struct memory_map map;
void *symbol_table;

View file

@ -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);
}
int fb_init(uint16_t res_x, uint16_t res_y) {
static uint16_t read(uint16_t value) {
outw(INDEX, value);
return inw(DATA);
}
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);
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 width, uint16_t height) {
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;
}

View file

@ -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) {

View file

@ -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();

164
src/arch/amd64/pci.c Normal file
View file

@ -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;
}

View file

@ -5,10 +5,12 @@
#include <fb.h>
#include <shim.h>
#include <panic.h>
#include <pci.h>
void kmain(struct boot_info *info) {
memory_init(&info->map);
//fb_init(1024, 768);
pci_init();
fb_init(1024, 768);
//acpi_init(info->acpi_table);
kprintf("enterd kmain\n");