Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kernel/include/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ void cmd_handle(const char* command);
void cmd_ping();
void cmd_crash();
void cmd_reboot();
void cmd_halt();
void cmd_clear();
void cmd_stack();
6 changes: 6 additions & 0 deletions kernel/src/command/halt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "command.h"
#include "io.h"

void cmd_halt() {
outw(0x604, 0x2000); // QEMU specific
}
35 changes: 19 additions & 16 deletions kernel/src/command/handle.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
#include "command.h"
#include "io.h"
#include "utils.h"
#include <stdio.h>
#include <string.h>

typedef struct {
const char* name;
void (*handler)(void);
} command_t;

static const command_t commands[] = {
{"ping", &cmd_ping}, {"crash", &cmd_crash}, {"reboot", &cmd_reboot},
{"halt", &cmd_halt}, {"clear", &cmd_clear}, {"stack", &cmd_stack},
{NULL, NULL} // sentinel
};

void cmd_handle(const char* command) {
if (strcmp(command, "ping") == 0) {
cmd_ping();
} else if (strcmp(command, "crash") == 0) {
cmd_crash();
} else if (strcmp(command, "reboot") == 0) {
cmd_reboot();
} else if (strcmp(command, "halt") == 0) {
outw(0x604, 0x2000); // QEMU specific
} else if (strcmp(command, "clear") == 0) {
cmd_clear();
} else if (strcmp(command, "stack") == 0) {
cmd_stack();
} else {
if (strlen(command) > 0)
printf("%s: command not found\n", command);
for (size_t i = 0; commands[i].name != NULL; i++) {
if (strcmp(command, commands[i].name) == 0) {
commands[i].handler();
return;
}
}

if (strlen(command) > 0)
printf("%s: command not found\n", command);
}
33 changes: 13 additions & 20 deletions kernel/src/memory/paging.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ static uintptr_t get_page_table_phys(uintptr_t virt_addr, bool create) {
return pt_phys;
}

static inline pte_t* get_page_table(uint32_t pd_idx, uintptr_t pt_phys) {
if (recursive_active()) {
return pt_virt_for_index(pd_idx);
} else {
return (pte_t*)pt_phys;
}
}

void map_page(uintptr_t virt_addr, uintptr_t phys_addr, uint32_t flags) {
virt_addr = page_align_down(virt_addr);
phys_addr = page_align_down(phys_addr);
Expand All @@ -50,12 +58,7 @@ void map_page(uintptr_t virt_addr, uintptr_t phys_addr, uint32_t flags) {
uint32_t pd_idx = pd_index(virt_addr);
uint32_t pt_idx = pt_index(virt_addr);

pte_t* pt;
if (recursive_active()) {
pt = pt_virt_for_index(pd_idx);
} else {
pt = (pte_t*)pt_phys;
}
pte_t* pt = get_page_table(pd_idx, pt_phys);

pt[pt_idx] = phys_addr | flags;
invlpg(virt_addr);
Expand All @@ -70,13 +73,8 @@ void unmap_page(uintptr_t virt_addr) {
if (!(page_directory[pd_idx] & PAGE_PRESENT))
return;

pte_t* pt;
if (recursive_active()) {
pt = pt_virt_for_index(pd_idx);
} else {
uintptr_t pt_phys = page_align_down(page_directory[pd_idx]);
pt = (pte_t*)pt_phys;
}
uintptr_t pt_phys = page_align_down(page_directory[pd_idx]);
pte_t* pt = get_page_table(pd_idx, pt_phys);

pt[pt_idx] = 0;
invlpg(virt_addr);
Expand Down Expand Up @@ -104,13 +102,8 @@ uintptr_t get_phys_addr(uintptr_t virt_addr) {
return 0;
}

const pte_t* pt;
if (recursive_active()) {
pt = pt_virt_for_index(pd_idx);
} else {
uintptr_t pt_phys = page_align_down(page_directory[pd_idx]);
pt = (pte_t*)pt_phys;
}
uintptr_t pt_phys = page_align_down(page_directory[pd_idx]);
const pte_t* pt = get_page_table(pd_idx, pt_phys);

if (!(pt[pt_idx] & PAGE_PRESENT)) {
return 0;
Expand Down
25 changes: 10 additions & 15 deletions kernel/src/pic.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "pic.h"
#include "io.h"
#include <stdbool.h>

void outb_wait(uint16_t port, uint8_t val) {
outb(port, val);
Expand All @@ -12,7 +13,7 @@ void pic_eoi(uint8_t irq) {
outb(PIC_PARENT_COMMAND, PIC_EOI);
}

void pic_enable_irq(uint8_t irq) {
static void pic_set_irq_mask(uint8_t irq, bool enable) {
uint16_t port;
uint8_t mask;

Expand All @@ -23,25 +24,19 @@ void pic_enable_irq(uint8_t irq) {
irq -= 8;
}

mask = inb(port) & ~(1 << irq);
outb(port, mask);
}

void pic_disable_irq(uint8_t irq) {
uint16_t port;
uint8_t mask;

if (irq < 8) {
port = PIC_PARENT_DATA;
mask = inb(port);
if (enable) {
mask &= ~(1 << irq);
} else {
port = PIC_CHILD_DATA;
irq -= 8;
mask |= (1 << irq);
}

mask = inb(port) | (1 << irq);
outb(port, mask);
}

void pic_enable_irq(uint8_t irq) { pic_set_irq_mask(irq, true); }

void pic_disable_irq(uint8_t irq) { pic_set_irq_mask(irq, false); }

void pic_remap(int parent_offset, int child_offset) {
outb_wait(PIC_PARENT_COMMAND, PIC_ICW1_INIT);
outb_wait(PIC_CHILD_COMMAND, PIC_ICW1_INIT);
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/tty/printk.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ char* level_name[] = {"EMERG", "ALERT", "CRIT", "ERR", "WARN",
"NOTICE", "INFO", "DEBUG", "", "CONT"};

static int get_level(char c) {
if (c >= '0' || c <= '7')
if (c >= '0' && c <= '7')
return c - '0';
else if (c == 'c')
return 9;
Expand Down
8 changes: 2 additions & 6 deletions kernel/src/tty/vprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@ int putnbr_base_unsigned(unsigned int n, const char* base) {
int putnbr_base(long n, const char* base) {
if (!base)
return 0;
int len = 1;
size_t base_size = strlen(base);
int len = 0;
if (n < 0) {
tty_putchar('-');
n = -n;
++len;
}
if (n / base_size)
len += putnbr_base(n / base_size, base);
tty_putchar(base[n % base_size]);
return len;
return len + putnbr_base_unsigned((unsigned int)n, base);
Comment thread
iamantoinev marked this conversation as resolved.
}

int putstr_count(char* str) {
Expand Down