os

Toy OS
git clone git://git.margiolis.net/os.git
Log | Files | Refs | README | LICENSE

commit 30f15bfc930e222f54a829b81a0fe24d2256cd33
parent e94672f6ba805d1cc432d72eb8e3e57d80a2d872
Author: Christos Margiolis <christos@margiolis.net>
Date:   Sun,  5 Dec 2021 04:47:47 +0200

header file organization

Diffstat:
Ai386/reg.h | 26++++++++++++++++++++++++++
Ai386/u.h | 30++++++++++++++++++++++++++++++
Ainclude/libc.h | 10++++++++++
Dinclude/stdarg.h | 14--------------
Mkern/Makefile | 9++++++---
Mkern/idt.c | 20+++++++++++---------
Mkern/idt.h | 59+++++++++++++----------------------------------------------
Mkern/intr.s | 3---
Mkern/io.h | 42+++++++++++++++++-------------------------
Mkern/kbd.c | 5++++-
Mkern/kbd.h | 6+++---
Mkern/kern_main.c | 8+++++---
Mkern/libk.c | 25+++++++++++++++++++++++++
Mkern/libk.h | 23+++++++++--------------
Dkern/page.c | 8--------
Dkern/page.h | 6------
Mkern/timer.c | 12+++++++-----
Mkern/timer.h | 6+++---
Mkern/vga.c | 29+++++++++++++++--------------
Mkern/vga.h | 18+++++++++---------
Akern/vm_page.c | 8++++++++
Akern/vm_page.h | 15+++++++++++++++
22 files changed, 216 insertions(+), 166 deletions(-)

diff --git a/i386/reg.h b/i386/reg.h @@ -0,0 +1,26 @@ +#ifndef _REG_H_ +#define _REG_H_ + +struct reg { + u_int32_t r_gs; + u_int32_t r_fs; + u_int32_t r_es; + u_int32_t r_ds; + u_int32_t r_edi; + u_int32_t r_esi; + u_int32_t r_ebp; + u_int32_t r_esp; + u_int32_t r_ebx; + u_int32_t r_edx; + u_int32_t r_ecx; + u_int32_t r_eax; + u_int32_t r_intrno; + u_int32_t r_err; + u_int32_t r_eip; + u_int32_t r_cs; + u_int32_t r_eflags; + u_int32_t r_uesp; + u_int32_t r_ss; +}; + +#endif /* _REG_H_ */ diff --git a/i386/u.h b/i386/u.h @@ -0,0 +1,30 @@ +#ifndef _U_H_ +#define _U_H_ + +#include <stddef.h> /* XXX: keep for now... */ + +typedef unsigned char u_char; +typedef unsigned short u_short; +typedef unsigned int u_int; +typedef unsigned long u_long; +typedef unsigned long long u_vlong; +typedef long long vlong; + +typedef signed char int8_t; +typedef unsigned char u_int8_t; +typedef unsigned short u_int16_t; +typedef short int16_t; +typedef unsigned int u_int32_t; +typedef int int32_t; +typedef unsigned long u_int64_t; +typedef long int64_t; + +//typedef u_int64_t size_t; + +typedef __va_list va_list; +#define va_start(ap, last) __builtin_va_start((ap), (last)) +#define va_arg(ap, type) __builtin_va_arg((ap), type) +#define va_copy(dest, src) __builtin_va_copy((dest), (src)) +#define va_end(ap) __builtin_va_end(ap) + +#endif /* _U_H_ */ diff --git a/include/libc.h b/include/libc.h @@ -0,0 +1,10 @@ +#ifndef _LIBC_H_ +#define _LIBC_H_ + +#define NULL ((void *)0) +#define UNUSED(x) ((void)(x)) +#define ARRLEN(x) (sizeof(x) / sizeof(*(x))) +#define __CONCAT(x, y) x ## y +#define offsetof(s, t) ((u_long)(&(((s *)0->t)))) + +#endif /* _LIBC_H_ */ diff --git a/include/stdarg.h b/include/stdarg.h @@ -1,14 +0,0 @@ -#ifndef _KERNEL_STDARG_H_ -#define _KERNEL_STDARG_H_ - -#ifndef _VA_LIST_DECLARED -#define _VA_LIST_DECLARED -typedef __va_list va_list; -#endif /* _VA_LIST_DECLARED */ - -#define va_start(ap, last) __builtin_va_start((ap), (last)) -#define va_arg(ap, type) __builtin_va_arg((ap), type) -#define va_copy(dest, src) __builtin_va_copy((dest), (src)) -#define va_end(ap) __builtin_va_end(ap) - -#endif /* _KERNEL_STDARG_H_ */ diff --git a/kern/Makefile b/kern/Makefile @@ -4,8 +4,11 @@ BINDIR = ../build CC = cc ASM = nasm LD = ld -INCDIR = ../include -CFLAGS = -g -m32 -nostdlib -ffreestanding -Wall -Wextra -std=c99 -O2 -I${INCDIR} +ARCH=i386 +ARCHINCDIR = ../${ARCH} +INCDIR=../include +CFLAGS = -g -m32 -nostdlib -ffreestanding -Wall -Wextra -std=c99 -O2 \ + -I${ARCHINCDIR} -I${INCDIR} LDFLAGS = -Ttext 0x1000 --oformat binary BOOTFILE = boot.s @@ -19,7 +22,7 @@ OBJ = kern_main.o \ vga.o \ kbd.o \ timer.o \ - page.o + vm_page.o all: options ${BIN} diff --git a/kern/idt.c b/kern/idt.c @@ -1,9 +1,9 @@ #include "libk.h" #include "idt.h" +#include "io.h" #define NINT 256 #define NRSVINT 32 -#define NISR 16 #define PL_KERN 0 /* Kernel privilege level */ #define PL_DRV1 1 /* Device driver privilege level 1 */ #define PL_DRV2 2 /* Device driver privilege level 2 */ @@ -18,20 +18,20 @@ #define PIC_SLAVE_CMD 0xa0 #define PIC_SLAVE_DATA (PIC_SLAVE_CMD + 1) -static void idt_set_gate(struct gate_desc *, void *, uint16_t, uint8_t); +static void idt_set_gate(struct gate_desc *, void *, u_int16_t, u_int8_t); static void pic_remap(void); static struct gate_desc idt[NINT]; -static intrhand_t isr[NISR] = { NULL }; +static intrhand_t isr[NINT] = { NULL }; static void -idt_set_gate(struct gate_desc *gd, void *func, uint16_t sel, uint8_t flags) +idt_set_gate(struct gate_desc *gd, void *func, u_int16_t sel, u_int8_t flags) { - gd->gd_off_lo = (uint32_t)func & 0xffff; + gd->gd_off_lo = (u_int32_t)func & 0xffff; gd->gd_sel = sel; gd->gd_rsvd = 0; gd->gd_flags = flags; - gd->gd_off_hi = ((uint32_t)func >> 16) & 0xffff; + gd->gd_off_hi = ((u_int32_t)func >> 16) & 0xffff; } /* TODO: explain. */ @@ -148,9 +148,9 @@ idt_init(void) /*idt_set_gate(&idt[127], &syscall, SEL_KCODE, GT_FLAGS(PL_KERN, TP_386IGT));*/ - r_idt.rd_base = (uint32_t)&idt; + r_idt.rd_base = (u_int32_t)&idt; r_idt.rd_limit = NINT * sizeof(struct gate_desc) - 1; - lidt(&r_idt); + __asm__ __volatile("lidt (%0)" : : "r" (&r_idt)); } static const char *exceptmsg[] = { @@ -193,6 +193,7 @@ intr_handler(struct reg *r) { intrhand_t handler; + /* TODO: dprintf? */ if (r->r_intrno < 32) printf("%s\n", exceptmsg[r->r_intrno]); if ((handler = isr[r->r_intrno]) != NULL) @@ -208,11 +209,12 @@ intr_handler(struct reg *r) } void -intr_register_handler(uint8_t intrno, intrhand_t handler) +intr_register_handler(u_int8_t intrno, intrhand_t handler) { isr[intrno] = handler; } +/* FIXME: not 8? */ void print_regs(struct reg *r) { diff --git a/kern/idt.h b/kern/idt.h @@ -1,54 +1,24 @@ -#ifndef _KERNEL_IDT_H_ -#define _KERNEL_IDT_H_ +#ifndef _IDT_H_ +#define _IDT_H_ -#include <stdint.h> +#include <u.h> +#include <reg.h> #define INTVEC(name) __CONCAT(intr_, name) -/* IA-32 */ struct gate_desc { - uint16_t gd_off_lo; - uint16_t gd_sel; - uint8_t gd_rsvd; - uint8_t gd_flags; /* type, dpl, p */ - uint16_t gd_off_hi; + u_int16_t gd_off_lo; + u_int16_t gd_sel; + u_int8_t gd_rsvd; + u_int8_t gd_flags; /* type, dpl, p */ + u_int16_t gd_off_hi; } __packed; struct region_desc { - uint16_t rd_limit; - uint32_t rd_base; + u_int16_t rd_limit; + u_int32_t rd_base; } __packed; -/* TODO: keep counter? */ - -/* This will be populated by `intr_common_stub` in `intr.asm`. */ -/* FIXME: move to i386 dir? */ -struct reg { - /* Will be popped last. */ - uint32_t r_gs; - uint32_t r_fs; - uint32_t r_es; - uint32_t r_ds; - /* Pushed by `pusha`. */ - uint32_t r_edi; - uint32_t r_esi; - uint32_t r_ebp; - uint32_t r_esp; - uint32_t r_ebx; - uint32_t r_edx; - uint32_t r_ecx; - uint32_t r_eax; - /* Interrupt info. Pushed by `push byte`. */ - uint32_t r_intrno; - uint32_t r_err; - /* Pushed by the CPU. */ - uint32_t r_eip; - uint32_t r_cs; - uint32_t r_eflags; - uint32_t r_uesp; - uint32_t r_ss; -}; - enum { IRQ0 = 32, IRQ1, @@ -70,12 +40,9 @@ enum { typedef void (*intrhand_t)(struct reg *); -/* Called by `kern_main`. */ void idt_init(void); - -/* Called by drivers. */ void intr_handler(struct reg *); -void intr_register_handler(uint8_t, intrhand_t); +void intr_register_handler(u_int8_t, intrhand_t); void print_regs(struct reg *); /* FIXME: move elsewhere? */ -#endif /* _KERNEL_IDT_H_ */ +#endif /* _IDT_H_ */ diff --git a/kern/intr.s b/kern/intr.s @@ -4,7 +4,6 @@ %macro intr_noerr 2 global intr_%1 intr_%1: - cli push byte 0 push byte %2 jmp intr_common_stub @@ -13,7 +12,6 @@ intr_%1: %macro intr_err 2 global intr_%1 intr_%1: - cli push byte %2 jmp intr_common_stub %endmacro @@ -82,7 +80,6 @@ intr_common_stub: pop ds popa add esp, 8 ; Clean up ISR info. - sti iret ; I spent many hours debugging this... ; TODO: irq_common_stub bx? diff --git a/kern/io.h b/kern/io.h @@ -1,58 +1,50 @@ -#ifndef _KERNEL_IO_H_ -#define _KERNEL_IO_H_ +#ifndef _IO_H_ +#define _IO_H_ -#include <stdint.h> -#include "idt.h" +#include <u.h> +#include <reg.h> -struct region_descriptor; - -static inline void -lidt(struct region_desc *rd) -{ - __asm__ __volatile("lidt (%0)" : : "r" (rd)); -} - -static inline uint8_t -inb(uint16_t port) +static inline u_int8_t +inb(u_int16_t port) { - uint8_t res; + u_int8_t res; __asm__ __volatile("in %%dx, %%al" : "=a" (res) : "d" (port)); return (res); } static inline void -outb(uint16_t port, uint8_t v) +outb(u_int16_t port, u_int8_t v) { __asm__ __volatile("out %%al, %%dx" : : "a" (v), "d" (port)); } -static inline uint16_t -inw(uint16_t port) +static inline u_int16_t +inw(u_int16_t port) { - uint16_t res; + u_int16_t res; __asm__ __volatile("in %%dx, %%ax" : "=a" (res) : "d" (port)); return (res); } static inline void -outw(uint16_t port, uint16_t v) +outw(u_int16_t port, u_int16_t v) { __asm__ __volatile("out %%ax, %%dx" : : "a" (v), "d" (port)); } -static inline uint32_t -inl(uint16_t port) +static inline u_int32_t +inl(u_int16_t port) { - uint32_t res; + u_int32_t res; __asm__ __volatile("in %%dx, %%eax" : "=a" (res) : "d" (port)); return (res); } static inline void -outl(uint16_t port, uint32_t v) +outl(u_int16_t port, u_int32_t v) { __asm__ __volatile("out %%eax, %%dx" : : "a" (v), "d" (port)); } @@ -75,4 +67,4 @@ sti(void) __asm__ __volatile("sti"); } -#endif /* _KERNEL_IO_H_ */ +#endif /* _IO_H_ */ diff --git a/kern/kbd.c b/kern/kbd.c @@ -1,6 +1,9 @@ +#include <libc.h> #include "libk.h" #include "kbd.h" #include "idt.h" +#include "io.h" +#include "vga.h" #define KBD_CMD 0x60 #define KBD_PRESSED 0x80 @@ -90,7 +93,7 @@ static unsigned char kbdus_lower[128] = { static void kbd_callback(struct reg *r) { - uint8_t sc; + u_int8_t sc; int shift = 0; if ((sc = inb(KBD_CMD)) & KBD_PRESSED) { diff --git a/kern/kbd.h b/kern/kbd.h @@ -1,6 +1,6 @@ -#ifndef _KERNEL_KBD_H_ -#define _KERNEL_KBD_H_ +#ifndef _KBD_H_ +#define _KBD_H_ void kbd_init(void); -#endif /* _KERNEL_KBD_H_ */ +#endif /* _KBD_H_ */ diff --git a/kern/kern_main.c b/kern/kern_main.c @@ -1,15 +1,17 @@ -#include "libk.h" #include "kbd.h" #include "idt.h" -#include "page.h" +#include "io.h" #include "timer.h" +#include "vm_page.h" +#include "vga.h" +/* TODO: make sysctl */ void kern_main(void) { vga_clear(VGA_BLACK, VGA_WHITE); idt_init(); - page_init(); + vm_page_init(); timer_init(); kbd_init(); diff --git a/kern/libk.c b/kern/libk.c @@ -1,4 +1,6 @@ #include "libk.h" +#include "io.h" +#include "vga.h" #define ZEROPAD 0x01 #define SIGN 0x02 @@ -151,6 +153,29 @@ strnlen(const char *str, size_t maxlen) return (s - str); } +size_t +strlcpy(char *dst, const char *src, size_t dsize) +{ + const char *osrc = src; + size_t nleft = dsize; + + if (nleft != 0) { + while (--nleft != 0) { + if ((*dst++ = *src++) == '\0') + break; + } + } + + if (nleft == 0) { + if (dsize != 0) + *dst = '\0'; + while (*src++) + ; + } + + return (src - osrc - 1); +} + int strcmp(const char *s1, const char *s2) { diff --git a/kern/libk.h b/kern/libk.h @@ -1,28 +1,23 @@ -#ifndef _KERNEL_LIBK_H_ -#define _KERNEL_LIBK_H_ +#ifndef _LIBK_H_ +#define _LIBK_H_ -#include <stddef.h> -#include <stdint.h> -#include <stdarg.h> +#include <u.h> -#include "io.h" -#include "vga.h" - -#define BUFSIZ 1024 - -#define ARRLEN(x) (sizeof(x) / sizeof(*(x))) -#define UNUSED(x) ((void)(x)) -#define sizeof_field(s, f) (sizeof(((t *)0)->f)) +#define BUFSIZ 1024 void *memset(void *, int, size_t); void *memcpy(void *, const void *, size_t); + size_t strlen(const char *); size_t strnlen(const char *, size_t); +size_t strlcpy(char *, const char *, size_t); int strcmp(const char *, const char *); int isdigit(char); + int vsprintf(char *, const char *, va_list); int sprintf(char *, const char *, ...); int printf(const char *, ...); + void panic(const char *, ...); -#endif /* _KERNEL_LIBK_H_ */ +#endif /* _LIBK_H_ */ diff --git a/kern/page.c b/kern/page.c @@ -1,8 +0,0 @@ -#include "libk.h" -#include "page.h" -#include "idt.h" - -void -page_init(void) -{ -} diff --git a/kern/page.h b/kern/page.h @@ -1,6 +0,0 @@ -#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 @@ -1,12 +1,14 @@ +#include <libc.h> #include "libk.h" #include "idt.h" +#include "io.h" #define TIMER_CMD 0x43 #define TIMER_DATA 0x40 static void timer_callback(struct reg *); -static uint32_t timer_ticks = 0; +static u_int32_t timer_ticks = 0; static void timer_callback(struct reg *r) @@ -18,13 +20,13 @@ timer_callback(struct reg *r) void timer_init(void) { - const uint32_t hz = 100; - uint32_t div = 1193180 / hz; + const u_int32_t hz = 100; + u_int32_t div = 1193180 / hz; 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)); + outb(TIMER_DATA, (u_int8_t)(div & 0xff)); + outb(TIMER_DATA, (u_int8_t)((div >> 8) & 0xff)); printf("timer on irq 0\n"); } diff --git a/kern/timer.h b/kern/timer.h @@ -1,6 +1,6 @@ -#ifndef _KERNEL_TIMER_H_ -#define _KERNEL_TIMER_H_ +#ifndef _TIMER_H_ +#define _TIMER_H_ void timer_init(void); -#endif /* _KERNEL_TIMER_H_ */ +#endif /* _TIMER_H_ */ diff --git a/kern/vga.c b/kern/vga.c @@ -1,24 +1,25 @@ -#include "libk.h" +#include "vga.h" +#include "io.h" #define VGA_MEM 0xb8000; #define VGA_COLS 80 #define VGA_ROWS 25 -#define VGA_PUTC(c) (((uint16_t)vga.color << 8) | (c)) +#define VGA_PUTC(c) (((u_int16_t)vga.color << 8) | (c)) #define CURS_CMD 0x3d4 #define CURS_DATA 0x3d5 struct vga_info { - volatile uint16_t *buf; + volatile u_int16_t *buf; size_t row; size_t col; - uint8_t color; + u_int8_t color; }; static struct vga_info vga; void -vga_clear(uint8_t fg, uint8_t bg) +vga_clear(u_int8_t fg, u_int8_t bg) { size_t y, x; @@ -26,7 +27,7 @@ vga_clear(uint8_t fg, uint8_t bg) vga_curs_setpos(0, 0); vga.row = 0; vga.col = 0; - vga.buf = (uint16_t *)VGA_MEM; + vga.buf = (u_int16_t *)VGA_MEM; vga_set_color(fg, bg); for (x = 0; x < VGA_COLS; x++) for (y = 0; y < VGA_ROWS; y++) @@ -34,7 +35,7 @@ vga_clear(uint8_t fg, uint8_t bg) } void -vga_set_color(uint8_t fg, uint8_t bg) +vga_set_color(u_int8_t fg, u_int8_t bg) { vga.color = fg | (bg << 4); } @@ -88,7 +89,7 @@ vga_write(const char *str) * with `start = 0x00` and `end = 0x0f` is effectively a block cursor. */ void -vga_curs_enable(uint8_t start, uint8_t end) +vga_curs_enable(u_int8_t start, u_int8_t end) { /* 0x0a: Low cursor shape */ outb(CURS_CMD, 0x0a); @@ -114,13 +115,13 @@ vga_curs_disable(void) * x = pos % VGA_COLS * y = pos / VGA_COLS */ -uint16_t +u_int16_t vga_curs_getpos(void) { - uint16_t pos = 0; + u_int16_t pos = 0; outb(CURS_CMD, 0x0e); - pos |= (uint16_t)inb(CURS_DATA) << 8; + pos |= (u_int16_t)inb(CURS_DATA) << 8; outb(CURS_CMD, 0x0f); pos |= inb(CURS_DATA); @@ -130,10 +131,10 @@ vga_curs_getpos(void) void vga_curs_setpos(int x, int y) { - uint16_t pos = y * VGA_COLS + x; + u_int16_t pos = y * VGA_COLS + x; outb(CURS_CMD, 0x0e); - outb(CURS_DATA, (uint8_t)((pos >> 8) & 0xff)); + outb(CURS_DATA, (u_int8_t)((pos >> 8) & 0xff)); outb(CURS_CMD, 0x0f); - outb(CURS_DATA, (uint8_t)(pos & 0xff)); + outb(CURS_DATA, (u_int8_t)(pos & 0xff)); } diff --git a/kern/vga.h b/kern/vga.h @@ -1,9 +1,9 @@ -#ifndef _KERNEL_VGA_H_ -#define _KERNEL_VGA_H_ +#ifndef _VGA_H_ +#define _VGA_H_ -#include <stddef.h> +#include <u.h> -enum vga_color: uint8_t { +enum vga_color { VGA_BLACK = 0, VGA_BLUE, VGA_GREEN, @@ -22,13 +22,13 @@ enum vga_color: uint8_t { VGA_WHITE, }; -void vga_clear(uint8_t, uint8_t); -void vga_set_color(uint8_t, uint8_t); +void vga_clear(u_int8_t, u_int8_t); +void vga_set_color(u_int8_t, u_int8_t); void vga_putc(char); void vga_write(const char *); -void vga_curs_enable(uint8_t, uint8_t); +void vga_curs_enable(u_int8_t, u_int8_t); void vga_curs_disable(void); void vga_curs_setpos(int, int); -uint16_t vga_curs_getpos(void); +u_int16_t vga_curs_getpos(void); -#endif /* _KERNEL_VGA_H_ */ +#endif /* _VGA_H_ */ diff --git a/kern/vm_page.c b/kern/vm_page.c @@ -0,0 +1,8 @@ +#include "libk.h" +#include "idt.h" +#include "vm_page.h" + +void +vm_page_init(void) +{ +} diff --git a/kern/vm_page.h b/kern/vm_page.h @@ -0,0 +1,15 @@ +#ifndef _VM_PAGE_H_ +#define _VM_PAGE_H_ + +struct vm_page_entry { +}; + +struct vm_page_table { +}; + +struct vm_page_dir { +}; + +void vm_page_init(void); + +#endif /* _VM_PAGE_H_ */