This commit is contained in:
Murphy 2025-04-03 23:04:38 -04:00
parent 876970bcfd
commit d0854aa095
Signed by: freya
GPG key ID: 9FBC6FFD6D2DBF17
13 changed files with 161 additions and 148 deletions

View file

@ -3,7 +3,8 @@
#include "pic.h"
#include "idt.h"
void cpu_init(void) {
void cpu_init(void)
{
pic_remap();
idt_init();
fpu_init();

View file

@ -2,7 +2,8 @@
#include "fpu.h"
void fpu_init(void) {
void fpu_init(void)
{
size_t cr4;
uint16_t cw = 0x37F;
__asm__ volatile("mov %%cr4, %0" : "=r"(cr4));

View file

@ -37,15 +37,15 @@ struct idtr {
// interrupt is present in IDT
#define PRESENT 0x80
__attribute__((aligned(0x10)))
static struct idt_entry idt[256];
__attribute__((aligned(0x10))) static struct idt_entry idt[256];
static struct idtr idtr;
// from idt.S
extern void *isr_stub_table[];
// initialize and load the IDT
void idt_init(void) {
void idt_init(void)
{
// initialize idtr
idtr.address = (uint64_t)&idt;
idtr.size = (uint16_t)sizeof(struct idt_entry) * IDT_SIZE - 1;
@ -70,7 +70,8 @@ void idt_init(void) {
__asm__ volatile("lidt %0" : : "m"(idtr));
}
static void isr_print_regs(struct isr_regs *regs) {
static void isr_print_regs(struct isr_regs *regs)
{
printf("rax: %#016lx (%lu)\n", regs->rax, regs->rax);
printf("rbx: %#016lx (%lu)\n", regs->rbx, regs->rbx);
printf("rcx: %#016lx (%lu)\n", regs->rcx, regs->rcx);
@ -91,24 +92,42 @@ static void isr_print_regs(struct isr_regs *regs) {
printf("rflags: %#016lx (%lu)\n", regs->rflags, regs->rflags);
struct rflags *rflags = (struct rflags *)regs->rflags;
puts("rflags: ");
if (rflags->cf) puts("CF ");
if (rflags->pf) puts("PF ");
if (rflags->af) puts("AF ");
if (rflags->zf) puts("ZF ");
if (rflags->sf) puts("SF ");
if (rflags->tf) puts("TF ");
if (rflags->if_) puts("IF ");
if (rflags->df) puts("DF ");
if (rflags->of) puts("OF ");
if (rflags->iopl) puts("IOPL ");
if (rflags->nt) puts("NT ");
if (rflags->md) puts("MD ");
if (rflags->rf) puts("RF ");
if (rflags->vm) puts("VM ");
if (rflags->ac) puts("AC ");
if (rflags->vif) puts("VIF ");
if (rflags->vip) puts("VIP ");
if (rflags->id) puts("ID ");
if (rflags->cf)
puts("CF ");
if (rflags->pf)
puts("PF ");
if (rflags->af)
puts("AF ");
if (rflags->zf)
puts("ZF ");
if (rflags->sf)
puts("SF ");
if (rflags->tf)
puts("TF ");
if (rflags->if_)
puts("IF ");
if (rflags->df)
puts("DF ");
if (rflags->of)
puts("OF ");
if (rflags->iopl)
puts("IOPL ");
if (rflags->nt)
puts("NT ");
if (rflags->md)
puts("MD ");
if (rflags->rf)
puts("RF ");
if (rflags->vm)
puts("VM ");
if (rflags->ac)
puts("AC ");
if (rflags->vif)
puts("VIF ");
if (rflags->vip)
puts("VIP ");
if (rflags->id)
puts("ID ");
puts("\n");
}
@ -152,7 +171,9 @@ char *EXCEPTIONS[] = {
"Reserved",
};
void idt_exception_handler(uint64_t exception, uint64_t code, struct isr_regs *state) {
void idt_exception_handler(uint64_t exception, uint64_t code,
struct isr_regs *state)
{
uint64_t cr2;
switch (exception) {
@ -182,13 +203,15 @@ void idt_exception_handler(uint64_t exception, uint64_t code, struct isr_regs *s
}
}
void idt_pic_eoi(uint8_t exception) {
void idt_pic_eoi(uint8_t exception)
{
pic_eoi(exception - PIC_REMAP_OFFSET);
}
int counter = 0;
void idt_pic_timer(void) {
void idt_pic_timer(void)
{
// print a message once we know the timer works
// but avoid spamming the logs
if (counter == 3) {
@ -199,6 +222,10 @@ void idt_pic_timer(void) {
}
}
void idt_pic_keyboard(void) {}
void idt_pic_keyboard(void)
{
}
void idt_pic_mouse(void) {}
void idt_pic_mouse(void)
{
}

View file

@ -36,33 +36,13 @@ struct isr_regs {
};
struct rflags {
uint64_t cf : 1,
: 1,
pf : 1,
: 1,
af : 1,
: 1,
zf : 1,
sf : 1,
uint64_t cf : 1, : 1, pf : 1, : 1, af : 1, : 1, zf : 1, sf : 1,
tf : 1,
if_ : 1,
df : 1,
of : 1,
iopl : 2,
nt : 1,
md : 1,
tf : 1, if_ : 1, df : 1, of : 1, iopl : 2, nt : 1, md : 1,
rf : 1,
vm : 1,
ac : 1,
vif : 1,
vip : 1,
id : 1,
: 42;
rf : 1, vm : 1, ac : 1, vif : 1, vip : 1, id : 1, : 42;
};
void idt_init(void);
#endif /* idt.h */

View file

@ -23,13 +23,16 @@
#define ICW4_BUF_MASTER 0x0C /* Buffered mode/master */
#define ICW4_SFNM 0x10 /* Special fully nested (not) */
void pic_remap(void) {
void pic_remap(void)
{
uint8_t a1, a2;
a1 = inb(PIC1_DATA); // save masks
a2 = inb(PIC2_DATA);
outb(PIC1_COMMAND, ICW1_INIT | ICW1_ICW4); // starts the initialization sequence (in cascade mode)
outb(PIC1_COMMAND,
ICW1_INIT |
ICW1_ICW4); // starts the initialization sequence (in cascade mode)
io_wait();
outb(PIC2_COMMAND, ICW1_INIT | ICW1_ICW4);
io_wait();
@ -37,12 +40,15 @@ void pic_remap(void) {
io_wait();
outb(PIC2_DATA, PIC_REMAP_OFFSET + 8); // ICW2: Slave PIC vector offset
io_wait();
outb(PIC1_DATA, 4); // ICW3: tell Master PIC that there is a slave PIC at IRQ2 (0000 0100)
outb(
PIC1_DATA,
4); // ICW3: tell Master PIC that there is a slave PIC at IRQ2 (0000 0100)
io_wait();
outb(PIC2_DATA, 2); // ICW3: tell Slave PIC its cascade identity (0000 0010)
io_wait();
outb(PIC1_DATA, ICW4_8086); // ICW4: have the PICs use 8086 mode (and not 8080 mode)
outb(PIC1_DATA,
ICW4_8086); // ICW4: have the PICs use 8086 mode (and not 8080 mode)
io_wait();
outb(PIC2_DATA, ICW4_8086);
io_wait();
@ -51,7 +57,8 @@ void pic_remap(void) {
outb(PIC2_DATA, a2);
}
void pic_mask(int irq) {
void pic_mask(int irq)
{
uint16_t port;
uint8_t mask;
if (irq < 8) {
@ -64,7 +71,8 @@ void pic_mask(int irq) {
outb(port, mask);
}
void pic_unmask(int irq) {
void pic_unmask(int irq)
{
uint16_t port;
uint8_t mask;
if (irq < 8) {
@ -77,12 +85,14 @@ void pic_unmask(int irq) {
outb(port, mask);
}
void pic_disable(void) {
void pic_disable(void)
{
outb(PIC1_DATA, 0xff);
outb(PIC2_DATA, 0xff);
}
void pic_eoi(int irq) {
void pic_eoi(int irq)
{
if (irq >= 8) {
outb(PIC2_COMMAND, PIC_EOI);
}

View file

@ -3,13 +3,16 @@
#include <comus/asm.h>
#define PORT 0x3F8
static void serial_out(uint8_t ch) {
static void serial_out(uint8_t ch)
{
// wait for output to be free
while ((inb(PORT + 5) & 0x20) == 0);
while ((inb(PORT + 5) & 0x20) == 0)
;
outb(PORT, ch);
}
void fputc(FILE *stream, char c) {
void fputc(FILE *stream, char c)
{
(void)stream;
serial_out(c);

View file

@ -2,8 +2,8 @@
#include <stdarg.h>
#include <comus/asm.h>
__attribute__((noreturn))
void panic(const char *format, ...) {
__attribute__((noreturn)) void panic(const char *format, ...)
{
cli();
va_list list;
va_start(list, format);

View file

@ -70,7 +70,6 @@ struct mboot_tag_mmap {
struct mboot_mmap_entry entries[];
};
struct mboot_tag_old_rsdp {
uint32_t type;
uint32_t size;

View file

@ -5,15 +5,10 @@
#include <stdint.h>
#include <stdio.h>
static const char *segment_type[] = {
"Reserved",
"Free",
"Reserved",
"ACPI Reserved",
"Hibernation",
"Defective",
"Unknown"
};
static const char *segment_type[] = { "Reserved", "Free",
"Reserved", "ACPI Reserved",
"Hibernation", "Defective",
"Unknown" };
void mboot_load_mmap(volatile void *mboot, struct memory_map *res)
{
@ -24,22 +19,16 @@ void mboot_load_mmap(volatile void *mboot, struct memory_map *res)
uintptr_t i = (uintptr_t)mmap->entries;
printf("MEMORY MAP\n");
char buf[20];
for (;
i < (uintptr_t)mmap->entries + mmap->size;
i += mmap->entry_size, idx++
) {
for (; i < (uintptr_t)mmap->entries + mmap->size;
i += mmap->entry_size, idx++) {
struct mboot_mmap_entry *seg = (struct mboot_mmap_entry *)i;
const char *type = NULL;
if (seg->type > 6)
type = segment_type[6];
else
type = segment_type[seg->type];
printf("ADDR: %16p LEN: %4s TYPE: %s (%d)\n",
(void *)seg->addr,
btoa(seg->len, buf),
type,
seg->type
);
printf("ADDR: %16p LEN: %4s TYPE: %s (%d)\n", (void *)seg->addr,
btoa(seg->len, buf), type, seg->type);
if (seg->type != 1 || seg->len < 1)
continue;
res->entries[idx].addr = seg->addr;

View file

@ -1,7 +1,7 @@
#include <string.h>
volatile void *memcpyv(volatile void *restrict dest, const volatile void *restrict src,
size_t n)
volatile void *memcpyv(volatile void *restrict dest,
const volatile void *restrict src, size_t n)
{
volatile char *d = dest;
volatile const char *s = src;

View file

@ -286,7 +286,6 @@ static int printf_lltoa(char *buf, options_t *opts, bool is_neg,
}
}
// print zeros if needed
if (opts->width_set && len < opts->width && opts->zero) {
while (len++ < opts->width)
@ -351,7 +350,8 @@ static void handle_char_specifier(context_t *ctx, data_t c)
printf_putc(ctx, c.c);
}
static void handle_string_specifier(context_t *ctx, options_t *opts, data_t data)
static void handle_string_specifier(context_t *ctx, options_t *opts,
data_t data)
{
char *str = data.str;
int str_len = 0;
@ -585,15 +585,18 @@ void vfprintf(FILE *stream, const char *format, va_list args)
do_printf(&ctx, args);
}
void putc(char c) {
void putc(char c)
{
fputc(stdout, c);
}
void puts(const char *str) {
void puts(const char *str)
{
fputs(stdout, str);
}
void fputs(FILE *stream, const char *s) {
void fputs(FILE *stream, const char *s)
{
while (*s)
fputc(stream, *s++);
}