Formating tweaks overall

This commit is contained in:
Thomas Lovén 2022-01-18 13:42:01 +01:00
parent 8dd5e81399
commit ba85d9337d
30 changed files with 930 additions and 825 deletions

View File

@ -10,6 +10,6 @@
"**/*.d": true, "**/*.d": true,
}, },
"C_Cpp.default.includePath": ["src/kernel/include"], "C_Cpp.default.includePath": ["src/kernel/include", "sysroot/usr/include"],
"C_Cpp.default.cStandard": "c2x", "C_Cpp.default.cStandard": "c17",
} }

View File

@ -10,9 +10,14 @@ int *position = (void *)0x20000;
void thread1() { void thread1() {
int a = 1; int a = 1;
gfx_context *ctx = framebuffer_make_subcontext(&kernel_fb, 700, 300, 100, 100); gfx_context *ctx =
framebuffer_make_subcontext(&kernel_fb, 700, 300, 100, 100);
while(1) { while(1) {
putCharacter(term_fb, *position, *position, RGB(255,255,0), RGB(0,0,0), '0'+(a++%10)); putCharacter(term_fb,
*position, *position,
RGB(255,255,0), RGB(0,0,0),
'0'+(a++%10)
);
flip(term_fb); flip(term_fb);
draw_line(ctx, 0, 100, 50, ((a-1)%100), RGB(0,0,0)); draw_line(ctx, 0, 100, 50, ((a-1)%100), RGB(0,0,0));
draw_line(ctx, 0, 100, 50, (a%100), RGB(255,0,0)); draw_line(ctx, 0, 100, 50, (a%100), RGB(255,0,0));
@ -24,7 +29,11 @@ void thread1() {
void thread2() { void thread2() {
int a = 0; int a = 0;
while(1) { while(1) {
putCharacter(term_fb, *position, *position, RGB(0,255,255), RGB(0,0,0), 'A'+(a++%10)); putCharacter(term_fb,
*position, *position,
RGB(0,255,255), RGB(0,0,0),
'A'+(a++%10)
);
flip(term_fb); flip(term_fb);
sched_yield(); sched_yield();
} }

View File

@ -28,7 +28,8 @@ void kmain(uint64_t multiboot_magic, void *multiboot_data) {
multiboot_init(multiboot_magic, P2V(multiboot_data)); multiboot_init(multiboot_magic, P2V(multiboot_data));
debug_info("Kernel loaded with command line: \"%s\" by <%s>\n", debug_info("Kernel loaded with command line: \"%s\" by <%s>\n",
kernel_boot_data.commandline, kernel_boot_data.commandline,
kernel_boot_data.bootloader); kernel_boot_data.bootloader
);
early_cpu_init(); early_cpu_init();
debug_info("Set up boot CPU\n"); debug_info("Set up boot CPU\n");

View File

@ -59,7 +59,8 @@ static int parse_multiboot2(struct taglist *tags) {
break; break;
} }
// Tags are 8 byte alligned, so make sure we look for the next one in the right place // Tags are 8 byte alligned, so make sure we look for the next one
// in the right place
int padded_size = tag->size + ((tag->size % 8)?(8-(tag->size%8)):0); int padded_size = tag->size + ((tag->size % 8)?(8-(tag->size%8)):0);
tag = incptr(tag, padded_size); tag = incptr(tag, padded_size);
} }
@ -77,7 +78,12 @@ int multiboot_init(uint64_t magic, void *mboot_info) {
return 0; return 0;
} }
int multiboot_get_memory_area(size_t index, uintptr_t *start, uintptr_t *end, uint32_t *type) { int multiboot_get_memory_area(
size_t index,
uintptr_t *start,
uintptr_t *end,
uint32_t *type
) {
if(index >= kernel_boot_data.mmap_len) return 1; if(index >= kernel_boot_data.mmap_len) return 1;
struct mmap *mmap = kernel_boot_data.mmap; struct mmap *mmap = kernel_boot_data.mmap;
@ -91,22 +97,29 @@ int multiboot_get_memory_area(size_t index, uintptr_t *start, uintptr_t *end, ui
} }
int multiboot_page_used(uintptr_t page) { int multiboot_page_used(uintptr_t page) {
#define within_page(st, l) (((uintptr_t)st + l) > page && (uintptr_t)st < (page + PAGE_SIZE)) #define within_page(st, l) (((uintptr_t)st + l) > page && \
(uintptr_t)st < (page + PAGE_SIZE))
size_t fb_size = 0; size_t fb_size = 0;
uintptr_t fb_start = 0; uintptr_t fb_start = 0;
if(kernel_boot_data.fbinfo) { if(kernel_boot_data.fbinfo) {
fb_start = kernel_boot_data.fbinfo->framebuffer_addr; fb_start = kernel_boot_data.fbinfo->framebuffer_addr;
fb_size = kernel_boot_data.fbinfo->framebuffer_pitch*(kernel_boot_data.fbinfo->framebuffer_height + 1); fb_size = kernel_boot_data.fbinfo->framebuffer_pitch *
(kernel_boot_data.fbinfo->framebuffer_height + 1);
} }
if( if(
within_page(kernel_boot_data.mboot_tags, kernel_boot_data.tags_length) || within_page(kernel_boot_data.mboot_tags,
within_page(kernel_boot_data.bootloader, strlen(kernel_boot_data.bootloader)) || kernel_boot_data.tags_length
within_page(kernel_boot_data.commandline, strlen(kernel_boot_data.commandline)) || ) ||
within_page(kernel_boot_data.bootloader,
strlen(kernel_boot_data.bootloader)
) ||
within_page(kernel_boot_data.commandline,
strlen(kernel_boot_data.commandline)
) ||
within_page(kernel_boot_data.mmap, kernel_boot_data.mmap_size) || within_page(kernel_boot_data.mmap, kernel_boot_data.mmap_size) ||
within_page(fb_start, fb_size) || within_page(fb_start, fb_size) ||
0 0) {
) {
return 1; return 1;
} }
return 0; return 0;

View File

@ -4,7 +4,8 @@
#include <string.h> #include <string.h>
#include <debug.h> #include <debug.h>
// ACPI Specification https://uefi.org/sites/default/files/resources/ACPI_6_3_final_Jan30.pdf // ACPI Specification:
// https://uefi.org/sites/default/files/resources/ACPI_6_3_final_Jan30.pdf
struct rsdp { // 5.2.5.3 struct rsdp { // 5.2.5.3
char signature[8]; char signature[8];
@ -32,21 +33,18 @@ struct madt_field {
uint8_t type; uint8_t type;
uint8_t length; uint8_t length;
union { union {
// Processor Local APIC (5.2.12.2) struct { // Processor Local APIC (5.2.12.2)
struct {
uint8_t id; uint8_t id;
uint8_t apic; uint8_t apic;
uint32_t flags; uint32_t flags;
}__attribute__((packed)) lapic; }__attribute__((packed)) lapic;
// I/O APIC (5.2.12.3) struct { // I/O APIC (5.2.12.3)
struct {
uint8_t id; uint8_t id;
uint8_t reserved; uint8_t reserved;
uint32_t address; uint32_t address;
uint32_t base; uint32_t base;
}__attribute__((packed)) ioapic; }__attribute__((packed)) ioapic;
// Interrupt source override (5.2.12.5) struct { // Interrupt source override (5.2.12.5)
struct {
uint8_t bus; uint8_t bus;
uint8_t source; uint8_t source;
uint32_t interrupt; uint32_t interrupt;
@ -65,8 +63,7 @@ struct madt_header {
static void madt_parse(struct madt_header *madt, size_t length) static void madt_parse(struct madt_header *madt, size_t length)
{ {
int cpu_i=0; int cpu_i=0;
for( for(struct madt_field *f = madt->fields;
struct madt_field *f = madt->fields;
(size_t)f <= (size_t)madt + length; (size_t)f <= (size_t)madt + length;
f = incptr(f, f->length) f = incptr(f, f->length)
) { ) {

View File

@ -73,7 +73,11 @@ void irq_unmask(int irq) {
void ioapic_init() { void ioapic_init() {
// Map ioapic offset into kernel memory // Map ioapic offset into kernel memory
vmm_set_page(kernel_P4, (uintptr_t)P2V(ioapic.addr), ioapic.addr, PAGE_PRESENT | PAGE_WRITE | PAGE_GLOBAL); vmm_set_page(kernel_P4,
(uintptr_t)P2V(ioapic.addr),
ioapic.addr,
PAGE_PRESENT | PAGE_WRITE | PAGE_GLOBAL
);
union iored iored; union iored iored;
for(int i = 0; i < 24; i++) { for(int i = 0; i < 24; i++) {
@ -88,7 +92,11 @@ void ioapic_init() {
void apic_init() { void apic_init() {
// Map apic offset into kernel memory // Map apic offset into kernel memory
vmm_set_page(kernel_P4, (uintptr_t)P2V(APIC_BASE), APIC_BASE, PAGE_PRESENT | PAGE_WRITE | PAGE_GLOBAL); vmm_set_page(kernel_P4,
(uintptr_t)P2V(APIC_BASE),
APIC_BASE,
PAGE_PRESENT | PAGE_WRITE | PAGE_GLOBAL
);
// Allow LAPIC to receive interrupts // Allow LAPIC to receive interrupts
APIC_REG(APIC_SPURIOUS) = APIC_REG(APIC_SPURIOUS) | 0x100 | IRQ_SPURIOUS; APIC_REG(APIC_SPURIOUS) = APIC_REG(APIC_SPURIOUS) | 0x100 | IRQ_SPURIOUS;

View File

@ -4,8 +4,10 @@
struct cpu *cpus[16]; struct cpu *cpus[16];
struct ioapic ioapic = {0,0,0}; struct ioapic ioapic = {0,0,0};
uint8_t irq_redirects[MAX_IRQS] = \ uint8_t irq_redirects[MAX_IRQS] = {
{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23}; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23
};
#define PIC1_ADDR 0x20 #define PIC1_ADDR 0x20
#define PIC2_ADDR 0xA0 #define PIC2_ADDR 0xA0

View File

@ -62,7 +62,10 @@ registers *int_handler(registers *r) {
case 6: case 6:
debug_error("Invalid opcode\n"); debug_error("Invalid opcode\n");
} }
debug_error("Interrupt number: %d, Error code: %d\n", r->int_no, r->err_code); debug_error("Interrupt number: %d, Error code: %d\n",
r->int_no,
r->err_code
);
PANIC("Unhandled interrupt\n"); PANIC("Unhandled interrupt\n");
} else if(r->int_no < IRQ_BASE + MAX_IRQS) { } else if(r->int_no < IRQ_BASE + MAX_IRQS) {
debug_error("UNHANDLED IRQ\n"); debug_error("UNHANDLED IRQ\n");

View File

@ -16,32 +16,48 @@ void fbterm_flush(struct vga_cell *buffer) {
int i = 0; int i = 0;
for(int row = 0; row < VGA_ROWS; row++) { for(int row = 0; row < VGA_ROWS; row++) {
for(int col=0; col < VGA_COLS; col++) { for(int col=0; col < VGA_COLS; col++) {
putCharacter(term_fb, col*8, row*16, 0xebdbb2, 0x282828, (char)buffer[i++].c); putCharacter(term_fb,
col*8, row*16,
0xebdbb2, 0x282828,
(char)buffer[i++].c
);
} }
} }
flip(term_fb); flip(term_fb);
} }
void fbterm_init(struct fbinfo *fbinfo) { void fbterm_init(struct fbinfo *fbinfo) {
kernel_fb.width = fbinfo->framebuffer_width; kernel_fb = (gfx_context){
kernel_fb.height = fbinfo->framebuffer_height; .width = fbinfo->framebuffer_width,
kernel_fb.bpp = fbinfo->framebuffer_bpp/8; .height = fbinfo->framebuffer_height,
kernel_fb.pitch = fbinfo->framebuffer_pitch/kernel_fb.bpp; .bpp = fbinfo->framebuffer_bpp/8,
kernel_fb.addr = P2V(fbinfo->framebuffer_addr); .pitch = fbinfo->framebuffer_pitch/(fbinfo->framebuffer_bpp/8),
kernel_fb.size = fbinfo->framebuffer_pitch * (kernel_fb.height); .addr = P2V(fbinfo->framebuffer_addr),
.size = fbinfo->framebuffer_pitch * (fbinfo->framebuffer_height),
};
kernel_fb.buffer = calloc(1, kernel_fb.size); kernel_fb.buffer = calloc(1, kernel_fb.size);
for( for(uintptr_t p = (uintptr_t)kernel_fb.addr;
uintptr_t p = (uintptr_t)kernel_fb.addr;
p < ((uintptr_t)kernel_fb.addr + kernel_fb.size); p < ((uintptr_t)kernel_fb.addr + kernel_fb.size);
p += PAGE_SIZE p += PAGE_SIZE
) { ) {
vmm_set_page(kernel_P4, p, V2P(p), PAGE_GLOBAL | PAGE_WRITE | PAGE_PRESENT); vmm_set_page(kernel_P4,
p,
V2P(p),
PAGE_GLOBAL | PAGE_WRITE | PAGE_PRESENT
);
} }
draw_rect(&kernel_fb, 20, 20, 8*VGA_COLS+8, 16*VGA_ROWS+8, RGB(255, 0, 0)); draw_rect(&kernel_fb,
20, 20,
8*VGA_COLS+8, 16*VGA_ROWS+8,
RGB(255, 0, 0)
);
flip(&kernel_fb); flip(&kernel_fb);
term_fb = framebuffer_make_subcontext(&kernel_fb, 24, 24, 8*VGA_COLS, 16*VGA_ROWS); term_fb = framebuffer_make_subcontext(&kernel_fb,
24, 24,
8*VGA_COLS, 16*VGA_ROWS
);
flip(term_fb); flip(term_fb);
setup = 1; setup = 1;

View File

@ -21,8 +21,14 @@ static int terminal_type;
static void scroll() { static void scroll() {
while(cursor >= VGA_SIZE) { while(cursor >= VGA_SIZE) {
memmove(buffer, &buffer[VGA_POS(1,0)], VGA_COLS*(VGA_ROWS-1)*sizeof(struct vga_cell)); memmove(buffer,
memset(&buffer[VGA_POS(VGA_ROWS-1, 0)], 0, VGA_COLS*sizeof(struct vga_cell)); &buffer[VGA_POS(1,0)],
VGA_COLS*(VGA_ROWS-1)*sizeof(struct vga_cell)
);
memset(&buffer[VGA_POS(VGA_ROWS-1, 0)],
0,
VGA_COLS*sizeof(struct vga_cell)
);
cursor -= VGA_COLS; cursor -= VGA_COLS;
} }
} }
@ -75,8 +81,7 @@ long k_ioctl(long fd, long cmd, long arg3, long, long, long) {
} }
long k_writev(long fd, long iov, long iovcnt, long, long, long) { long k_writev(long fd, long iov, long iovcnt, long, long, long) {
if(fd == 1) if(fd == 1) {
{
size_t len = 0; size_t len = 0;
struct iovec *v = (void *) iov; struct iovec *v = (void *) iov;
for(int i = 0; i < iovcnt; i++) { for(int i = 0; i < iovcnt; i++) {

View File

@ -65,7 +65,9 @@ long syscall5(long num, long a1, long a2, long a3, long a4, long a5) {
if(syscall_handlers[num]) if(syscall_handlers[num])
retval = syscall_handlers[num](a1, a2, a3, a4, a5, 0); retval = syscall_handlers[num](a1, a2, a3, a4, a5, 0);
else else
PANIC("Unknown syscall: %d(%x, %x, %x, %x, %x)\n", num, a1, a2, a3, a4, a5); PANIC("Unknown syscall: %d(%x, %x, %x, %x, %x)\n",
num, a1, a2, a3, a4, a5
);
return retval; return retval;
} }
@ -74,7 +76,9 @@ long syscall6(long num, long a1, long a2, long a3, long a4, long a5, long a6) {
if(syscall_handlers[num]) if(syscall_handlers[num])
retval = syscall_handlers[num](a1, a2, a3, a4, a5, a6); retval = syscall_handlers[num](a1, a2, a3, a4, a5, a6);
else else
PANIC("Unknown syscall: %d(%x, %x, %x, %x, %x, %x)\n", num, a1, a2, a3, a4, a5, a6); PANIC("Unknown syscall: %d(%x, %x, %x, %x, %x, %x)\n",
num, a1, a2, a3, a4, a5, a6
);
return retval; return retval;
} }

View File

@ -7,23 +7,38 @@ static long _mmap = KERNEL_MMAP;
long k_brk(long brk, long, long, long, long, long) { long k_brk(long brk, long, long, long, long, long) {
if(brk) { if(brk) {
while(_brk < brk) { while(_brk < brk) {
vmm_set_page(kernel_P4, _brk, pmm_alloc(), PAGE_GLOBAL | PAGE_WRITE | PAGE_PRESENT); vmm_set_page(kernel_P4,
_brk,
pmm_alloc(),
PAGE_GLOBAL | PAGE_WRITE | PAGE_PRESENT
);
_brk += PAGE_SIZE; _brk += PAGE_SIZE;
} }
} }
return _brk; return _brk;
} }
long k_mmap(long addr, long length, long prot, long flags, long fd, long offset) { long k_mmap(
long addr,
long length,
long prot,
long flags,
long fd,
long offset
) {
(void)addr; (void)addr;
(void)prot; (void)prot;
(void)flags; (void)flags;
(void)offset; (void)offset;
if(fd != -1) if(fd != -1)
PANIC("Unknown mmap request\n"); PANIC("Unknown mmap request\n");
long retval = _mmap; long retval = _mmap;
while(length > 0) { while(length > 0) {
vmm_set_page(kernel_P4, _mmap, pmm_alloc(), PAGE_GLOBAL | PAGE_WRITE | PAGE_PRESENT); vmm_set_page(kernel_P4,
_mmap, pmm_alloc(),
PAGE_GLOBAL | PAGE_WRITE | PAGE_PRESENT
);
_mmap += PAGE_SIZE; _mmap += PAGE_SIZE;
length -= PAGE_SIZE; length -= PAGE_SIZE;
} }

View File

@ -17,7 +17,6 @@
static int P1_exists(uint64_t P4, uint64_t addr) { static int P1_exists(uint64_t P4, uint64_t addr) {
if (P4 && PRESENT(P4E(P4, addr)) && PRESENT(P3E(P4, addr)) && PRESENT(P2E(P4, addr))) if (P4 && PRESENT(P4E(P4, addr)) && PRESENT(P3E(P4, addr)) && PRESENT(P2E(P4, addr)))
return 1; return 1;
return 0; return 0;

View File

@ -11,13 +11,22 @@
#define STEP_X(ctx) 1 #define STEP_X(ctx) 1
#define STEP_Y(ctx) (ctx)->pitch #define STEP_Y(ctx) (ctx)->pitch
void putpixel(gfx_context *ctx, uint64_t x, uint64_t y, uint32_t clr) { void putpixel(
gfx_context *ctx,
uint64_t x, uint64_t y,
uint32_t clr
) {
if(x >= ctx->width || y >= ctx->height) return; if(x >= ctx->width || y >= ctx->height) return;
uint32_t *fb = (uint32_t *)ctx->buffer; uint32_t *fb = (uint32_t *)ctx->buffer;
fb[PXL(ctx, x, y)] = clr; fb[PXL(ctx, x, y)] = clr;
} }
void draw_line(gfx_context *ctx, uint64_t x0, uint64_t x1, uint64_t y0, uint64_t y1, uint32_t clr) { void draw_line(
gfx_context *ctx,
uint64_t x0, uint64_t x1,
uint64_t y0, uint64_t y1,
uint32_t clr
) {
int64_t dx = x1 > x0 ? x1 - x0 : x0 - x1; int64_t dx = x1 > x0 ? x1 - x0 : x0 - x1;
int64_t dy = y1 > y0 ? y1 - y0 : y0 - y1; int64_t dy = y1 > y0 ? y1 - y0 : y0 - y1;
int sx = x1 > x0 ? 1 : -1; int sx = x1 > x0 ? 1 : -1;
@ -39,7 +48,12 @@ void draw_line(gfx_context *ctx, uint64_t x0, uint64_t x1, uint64_t y0, uint64_t
} }
} }
void draw_rect(gfx_context *ctx, uint64_t x, uint64_t y, uint64_t width, uint64_t height, uint32_t clr) { void draw_rect(
gfx_context *ctx,
uint64_t x, uint64_t y,
uint64_t width, uint64_t height,
uint32_t clr
) {
uint32_t *fb = (uint32_t *)ctx->buffer; uint32_t *fb = (uint32_t *)ctx->buffer;
uint64_t l1 = PXL(ctx, x, y); uint64_t l1 = PXL(ctx, x, y);
uint64_t l2 = PXL(ctx, x, y + height); uint64_t l2 = PXL(ctx, x, y + height);
@ -57,7 +71,12 @@ void draw_rect(gfx_context *ctx, uint64_t x, uint64_t y, uint64_t width, uint64_
} }
} }
void fill_rect(gfx_context *ctx, uint64_t x, uint64_t y, uint64_t width, uint64_t height, uint32_t clr) { void fill_rect(
gfx_context *ctx,
uint64_t x, uint64_t y,
uint64_t width, uint64_t height,
uint32_t clr
) {
uint32_t *fb = (uint32_t *)ctx->buffer; uint32_t *fb = (uint32_t *)ctx->buffer;
uint64_t loc = PXL(ctx, x, y); uint64_t loc = PXL(ctx, x, y);
for(uint64_t _y = 0; _y <= height; _y++) { for(uint64_t _y = 0; _y <= height; _y++) {
@ -68,7 +87,12 @@ void fill_rect(gfx_context *ctx, uint64_t x, uint64_t y, uint64_t width, uint64_
} }
} }
void putCharacter(gfx_context *ctx, uint64_t x, uint64_t y, uint32_t clr_fg, uint32_t clr_bg, char c) { void putCharacter(
gfx_context *ctx,
uint64_t x, uint64_t y,
uint32_t clr_fg, uint32_t clr_bg,
char c
) {
unsigned char *chr = c ? font[(int)c-0x20]: font[0]; unsigned char *chr = c ? font[(int)c-0x20]: font[0];
if(x >= ctx->width || y >= ctx->height) return; if(x >= ctx->width || y >= ctx->height) return;
uint32_t *fb = (uint32_t *)ctx->buffer; uint32_t *fb = (uint32_t *)ctx->buffer;
@ -86,11 +110,16 @@ void flip(gfx_context *ctx) {
memcpy( memcpy(
incptr(ctx->addr, y*ctx->pitch*ctx->bpp), incptr(ctx->addr, y*ctx->pitch*ctx->bpp),
incptr(ctx->buffer, y*ctx->pitch*ctx->bpp), incptr(ctx->buffer, y*ctx->pitch*ctx->bpp),
ctx->width*ctx->bpp); ctx->width*ctx->bpp
);
} }
} }
gfx_context *framebuffer_make_subcontext(gfx_context *ctx, uint64_t x, uint64_t y, uint64_t width, uint64_t height) { gfx_context *framebuffer_make_subcontext(
gfx_context *ctx,
uint64_t x, uint64_t y,
uint64_t width, uint64_t height
) {
gfx_context *out = malloc(sizeof(gfx_context)); gfx_context *out = malloc(sizeof(gfx_context));
uint64_t loc = y * ctx->pitch + x; uint64_t loc = y * ctx->pitch + x;

View File

@ -24,10 +24,14 @@ static __inline long __syscall3(long n, long a1, long a2, long a3) {
static __inline long __syscall4(long n, long a1, long a2, long a3, long a4) { static __inline long __syscall4(long n, long a1, long a2, long a3, long a4) {
return syscall4(n, a1, a2, a3, a4); return syscall4(n, a1, a2, a3, a4);
} }
static __inline long __syscall5(long n, long a1, long a2, long a3, long a4, long a5) { static __inline long __syscall5(long n, long a1, long a2, long a3, long a4,
long a5
) {
return syscall5(n, a1, a2, a3, a4, a5); return syscall5(n, a1, a2, a3, a4, a5);
} }
static __inline long __syscall6(long n, long a1, long a2, long a3, long a4, long a5, long a6) { static __inline long __syscall6(long n, long a1, long a2, long a3, long a4,
long a5, long a6
) {
return syscall6(n, a1, a2, a3, a4, a5, a6); return syscall6(n, a1, a2, a3, a4, a5, a6);
} }