commit e94672f6ba805d1cc432d72eb8e3e57d80a2d872
parent f725f4dd2163eba563bd2346933562a19c06593c
Author: Christos Margiolis <christos@margiolis.net>
Date: Mon, 29 Nov 2021 04:31:47 +0200
start working on paging
Diffstat:
11 files changed, 68 insertions(+), 30 deletions(-)
diff --git a/kern/Makefile b/kern/Makefile
@@ -18,7 +18,8 @@ OBJ = kern_main.o \
intr.o \
vga.o \
kbd.o \
- timer.o
+ timer.o \
+ page.o
all: options ${BIN}
diff --git a/kern/idt.c b/kern/idt.c
@@ -18,7 +18,6 @@
#define PIC_SLAVE_CMD 0xa0
#define PIC_SLAVE_DATA (PIC_SLAVE_CMD + 1)
-static void print_regs(struct reg *);
static void idt_set_gate(struct gate_desc *, void *, uint16_t, uint8_t);
static void pic_remap(void);
@@ -26,21 +25,6 @@ static struct gate_desc idt[NINT];
static intrhand_t isr[NISR] = { NULL };
static void
-print_regs(struct reg *r)
-{
- printf("eax=%#08x\tebx=%#08x\tecx=%#08x\tedx=%#08x\n",
- r->r_eax, r->r_ebx, r->r_ecx, r->r_edx);
- printf("esp=%#08x\tebp=%#08x\tesi=%#08x\tedi=%#08x\n",
- r->r_esp, r->r_ebp, r->r_esi, r->r_edi);
- printf("ds=%#08x \tes=%#08x \tfs=%#08x \tgs=%#08x\n",
- r->r_ds, r->r_es, r->r_fs, r->r_gs);
- printf("eip=%#08x\tcs=%#08x \tss=%#08x \teflags=%08x\n",
- r->r_eip, r->r_cs, r->r_ss, r->r_eflags);
- printf("int=%#08x\terr=%#08x\tuesp=%#08x\n",
- r->r_intrno, r->r_err, r->r_uesp);
-}
-
-static void
idt_set_gate(struct gate_desc *gd, void *func, uint16_t sel, uint8_t flags)
{
gd->gd_off_lo = (uint32_t)func & 0xffff;
@@ -129,7 +113,6 @@ idt_init(void)
pic_remap();
- /* FIXME: how is this related to irq 0 -> timer etc? */
idt_set_gate(&idt[32], &INTVEC(irq0), SEL_KCODE,
GT_FLAGS(PL_KERN, TP_386IGT));
idt_set_gate(&idt[33], &INTVEC(irq1), SEL_KCODE,
@@ -210,16 +193,15 @@ intr_handler(struct reg *r)
{
intrhand_t handler;
- /*
- * We'll call the handler only if the interrupt number is > 32,
- * which means that we're dealing with an IRQ and not an exception.
- */
- if (r->r_intrno >= 32 && (handler = isr[r->r_intrno - 32]) != NULL)
+ if (r->r_intrno < 32)
+ printf("%s\n", exceptmsg[r->r_intrno]);
+ if ((handler = isr[r->r_intrno]) != NULL)
handler(r);
else if (r->r_intrno < 32) {
print_regs(r);
panic("%s: system halted...\n", exceptmsg[r->r_intrno]);
}
+ /* EOI */
if (r->r_intrno >= 40)
outb(PIC_SLAVE_CMD, 0x20);
outb(PIC_MASTER_CMD, 0x20);
@@ -230,3 +212,18 @@ intr_register_handler(uint8_t intrno, intrhand_t handler)
{
isr[intrno] = handler;
}
+
+void
+print_regs(struct reg *r)
+{
+ printf("eax=%#08x\tebx=%#08x\tecx=%#08x\tedx=%#08x\n",
+ r->r_eax, r->r_ebx, r->r_ecx, r->r_edx);
+ printf("esp=%#08x\tebp=%#08x\tesi=%#08x\tedi=%#08x\n",
+ r->r_esp, r->r_ebp, r->r_esi, r->r_edi);
+ printf("ds=%#08x \tes=%#08x \tfs=%#08x \tgs=%#08x\n",
+ r->r_ds, r->r_es, r->r_fs, r->r_gs);
+ printf("eip=%#08x\tcs=%#08x \tss=%#08x \teflags=%08x\n",
+ r->r_eip, r->r_cs, r->r_ss, r->r_eflags);
+ printf("int=%#08x\terr=%#08x\tuesp=%#08x\n",
+ r->r_intrno, r->r_err, r->r_uesp);
+}
diff --git a/kern/idt.h b/kern/idt.h
@@ -1,6 +1,8 @@
#ifndef _KERNEL_IDT_H_
#define _KERNEL_IDT_H_
+#include <stdint.h>
+
#define INTVEC(name) __CONCAT(intr_, name)
/* IA-32 */
@@ -47,6 +49,25 @@ struct reg {
uint32_t r_ss;
};
+enum {
+ IRQ0 = 32,
+ IRQ1,
+ IRQ2,
+ IRQ3,
+ IRQ4,
+ IRQ5,
+ IRQ6,
+ IRQ7,
+ IRQ8,
+ IRQ9,
+ IRQ10,
+ IRQ11,
+ IRQ12,
+ IRQ13,
+ IRQ14,
+ IRQ15,
+};
+
typedef void (*intrhand_t)(struct reg *);
/* Called by `kern_main`. */
@@ -55,5 +76,6 @@ void idt_init(void);
/* Called by drivers. */
void intr_handler(struct reg *);
void intr_register_handler(uint8_t, intrhand_t);
+void print_regs(struct reg *); /* FIXME: move elsewhere? */
#endif /* _KERNEL_IDT_H_ */
diff --git a/kern/intr.s b/kern/intr.s
@@ -84,3 +84,5 @@ intr_common_stub:
add esp, 8 ; Clean up ISR info.
sti
iret ; I spent many hours debugging this...
+
+; TODO: irq_common_stub bx?
diff --git a/kern/kbd.c b/kern/kbd.c
@@ -104,6 +104,6 @@ kbd_callback(struct reg *r)
void
kbd_init(void)
{
- intr_register_handler(1, kbd_callback);
- printf("kbd on irq %d\n", 1);
+ intr_register_handler(IRQ1, kbd_callback);
+ printf("kbd on irq 1\n");
}
diff --git a/kern/kern_main.c b/kern/kern_main.c
@@ -1,6 +1,7 @@
#include "libk.h"
#include "kbd.h"
#include "idt.h"
+#include "page.h"
#include "timer.h"
void
@@ -8,6 +9,8 @@ kern_main(void)
{
vga_clear(VGA_BLACK, VGA_WHITE);
idt_init();
+ page_init();
+
timer_init();
kbd_init();
diff --git a/kern/libk.c b/kern/libk.c
@@ -337,7 +337,6 @@ printf(const char *fmt, ...)
return (n);
}
-/* TODO: print regs */
void
panic(const char *fmt, ...)
{
diff --git a/kern/libk.h b/kern/libk.h
@@ -23,7 +23,6 @@ int isdigit(char);
int vsprintf(char *, const char *, va_list);
int sprintf(char *, const char *, ...);
int printf(const char *, ...);
-//int kprintf(const char *, ...);
void panic(const char *, ...);
#endif /* _KERNEL_LIBK_H_ */
diff --git a/kern/page.c b/kern/page.c
@@ -0,0 +1,8 @@
+#include "libk.h"
+#include "page.h"
+#include "idt.h"
+
+void
+page_init(void)
+{
+}
diff --git a/kern/page.h b/kern/page.h
@@ -0,0 +1,6 @@
+#ifndef _KERNEL_PAGE_H_
+#define _KERNEL_PAGE_H_
+
+void page_init(void);
+
+#endif /* _KERNEL_PAGE_H_ */
diff --git a/kern/timer.c b/kern/timer.c
@@ -18,12 +18,13 @@ timer_callback(struct reg *r)
void
timer_init(void)
{
- const uint32_t hz = 60;
+ const uint32_t hz = 100;
uint32_t div = 1193180 / hz;
- intr_register_handler(0, timer_callback);
+ intr_register_handler(IRQ0, timer_callback);
+ /* Repating mode. */
outb(TIMER_CMD, 0x36);
outb(TIMER_DATA, (uint8_t)(div & 0xff));
outb(TIMER_DATA, (uint8_t)((div >> 8) & 0xff));
- printf("timer on irq %d\n", 0);
+ printf("timer on irq 0\n");
}