aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/kernel/acpi/acpi.c8
-rw-r--r--src/kernel/commands/command.c3
-rw-r--r--src/kernel/fs/ata_pio.c2
-rw-r--r--src/kernel/fs/atapi_pio.c4
-rw-r--r--src/kernel/fs/install.c4
-rw-r--r--src/kernel/fs/iso9660/iso9660.c2
-rw-r--r--src/kernel/fs/marfs/directory.c5
-rw-r--r--src/kernel/fs/marfs/disklevel.c2
-rw-r--r--src/kernel/fs/marfs/new_file.c4
-rw-r--r--src/kernel/fs/marfs/read_whole_file.c4
-rw-r--r--src/kernel/fs/marfs/sectorlevel.c4
-rw-r--r--src/kernel/graphics/vesa.c21
-rw-r--r--src/kernel/interrupts/isr.c2
-rw-r--r--src/kernel/io/io.c6
-rw-r--r--src/kernel/kernel.c10
-rw-r--r--src/kernel/lib/alloc.c446
-rw-r--r--src/kernel/lib/alloc.h24
-rw-r--r--src/kernel/lib/lib.h37
-rw-r--r--src/kernel/lib/memory.c2
-rw-r--r--src/kernel/lib/string.c52
-rw-r--r--src/kernel/linker.ld1
-rw-r--r--src/kernel/paging/paging.c17
-rw-r--r--src/kernel/paging/paging.h2
-rw-r--r--src/kernel/system.c25
-rw-r--r--src/kernel/system.h14
-rw-r--r--src/mlibc/stdlib/itoa.c7
26 files changed, 80 insertions, 628 deletions
diff --git a/src/kernel/acpi/acpi.c b/src/kernel/acpi/acpi.c
index 893b28e..efa6fcf 100644
--- a/src/kernel/acpi/acpi.c
+++ b/src/kernel/acpi/acpi.c
@@ -157,8 +157,8 @@ int acpi_install() {
}
if (memcmp((unsigned int *) *ptr, "HPET", 4) == 0) {
hpet = (struct HPET *) *ptr;
- serial_write(hpet->signature);
- serial_write_hex((int) hpet->base_address);
+ // serial_write(hpet->signature);
+ // serial_write_hex((int) hpet->base_address);
}
ptr++;
} // Else: no valid FADT present
@@ -194,7 +194,5 @@ void reboot() {
while (good & 0x02)
good = receive_b(0x64);
send_b(0x64, 0xFE);
- loop:
- asm volatile ("hlt");
- goto loop;
+ halt_loop();
}
diff --git a/src/kernel/commands/command.c b/src/kernel/commands/command.c
index 4ae8836..6b1afcc 100644
--- a/src/kernel/commands/command.c
+++ b/src/kernel/commands/command.c
@@ -4,6 +4,7 @@
#include <kernel/graphics/vesa.h>
#include <kernel/cmos/rtc.h>
#include <kernel/timer/timer.h>
+#include <mlibc/string.h>
int32_t starts_with(const char *a, const char *b) {
size_t length_pre = strlen(b);
@@ -25,7 +26,7 @@ void exec_command(char *command) {
else if (starts_with(command, "zzz"))
vesa_draw_string("Not implemented\n");
else if (starts_with(command, "time")) {
- vesa_draw_number(get_time());
+ vesa_draw_number((int) get_time());
vesa_draw_string("\n");
} else if (starts_with(command, "date"))
write_time();
diff --git a/src/kernel/fs/ata_pio.c b/src/kernel/fs/ata_pio.c
index d577ea1..d3544ba 100644
--- a/src/kernel/fs/ata_pio.c
+++ b/src/kernel/fs/ata_pio.c
@@ -1,5 +1,5 @@
#include <kernel/io/io.h>
-#include <kernel/lib/alloc.h>
+#include <mlibc/stdlib.h>
#include <kernel/fs/ata_pio.h>
#include <kernel/interrupts/interrupts.h>
diff --git a/src/kernel/fs/atapi_pio.c b/src/kernel/fs/atapi_pio.c
index 4f986d9..e64cd75 100644
--- a/src/kernel/fs/atapi_pio.c
+++ b/src/kernel/fs/atapi_pio.c
@@ -19,9 +19,7 @@ void ATAPI_read(uint16_t nblocks, uint32_t lba) {
regs.ds = 0;
regs.si = ATAPI_PIO_DAPACK;
- paging_disable();
- int32(LBA_READ_INT, &regs);
- paging_enable();
+ v86(LBA_READ_INT, &regs);
}
void ATAPI_granular_read(uint32_t nblocks, uint32_t lba, uint8_t *output) {
diff --git a/src/kernel/fs/install.c b/src/kernel/fs/install.c
index 5a2cc61..dfe3009 100644
--- a/src/kernel/fs/install.c
+++ b/src/kernel/fs/install.c
@@ -1,9 +1,9 @@
#include <kernel/graphics/vesa.h>
#include <kernel/fs/ata_pio.h>
#include <kernel/fs/marfs/marfs.h>
-#include <kernel/lib/alloc.h>
#include <kernel/fs/iso9660/iso9660.h>
-#include "atapi_pio.h"
+#include <kernel/fs/atapi_pio.h>
+#include <mlibc/stdlib.h>
void install_melvix() {
info("You're booting from a CD, Melvix will only run after an install");
diff --git a/src/kernel/fs/iso9660/iso9660.c b/src/kernel/fs/iso9660/iso9660.c
index 728d3cb..f8eddd4 100644
--- a/src/kernel/fs/iso9660/iso9660.c
+++ b/src/kernel/fs/iso9660/iso9660.c
@@ -2,7 +2,7 @@
#include <kernel/lib/lib.h>
#include <kernel/fs/atapi_pio.h>
#include <kernel/fs/iso9660/iso9660.h>
-#include <kernel/lib/alloc.h>
+#include <mlibc/stdlib.h>
struct ISO9660_entity *ISO9660_get(char **dirs, uint8_t dirs_sz) {
ATAPI_read(1, 0x10);
diff --git a/src/kernel/fs/marfs/directory.c b/src/kernel/fs/marfs/directory.c
index f7783bb..bf19fc7 100644
--- a/src/kernel/fs/marfs/directory.c
+++ b/src/kernel/fs/marfs/directory.c
@@ -1,8 +1,7 @@
#include <stdint.h>
#include <kernel/fs/ata_pio.h>
-#include <kernel/lib/alloc.h>
-#include <kernel/lib/lib.h>
-#include "marfs.h"
+#include <mlibc/stdlib.h>
+#include <kernel/fs/marfs/marfs.h>
uint32_t marfs_new_dir(uint32_t uid) { return marfs_new_file(0, 0, uid, 0, 1); }
diff --git a/src/kernel/fs/marfs/disklevel.c b/src/kernel/fs/marfs/disklevel.c
index b6fc8c2..f54dea1 100644
--- a/src/kernel/fs/marfs/disklevel.c
+++ b/src/kernel/fs/marfs/disklevel.c
@@ -1,6 +1,6 @@
#include <stdint.h>
#include <kernel/fs/ata_pio.h>
-#include "marfs.h"
+#include <kernel/fs/marfs/marfs.h>
void marfs_format(void) {
// Create superblock
diff --git a/src/kernel/fs/marfs/new_file.c b/src/kernel/fs/marfs/new_file.c
index 7b06621..ebc8152 100644
--- a/src/kernel/fs/marfs/new_file.c
+++ b/src/kernel/fs/marfs/new_file.c
@@ -1,7 +1,7 @@
#include <stdint.h>
#include <kernel/fs/ata_pio.h>
-#include <kernel/lib/alloc.h>
-#include "marfs.h"
+#include <mlibc/stdlib.h>
+#include <kernel/fs/marfs/marfs.h>
static uint8_t last_maxlevel = 0;
diff --git a/src/kernel/fs/marfs/read_whole_file.c b/src/kernel/fs/marfs/read_whole_file.c
index 86aae5f..0d3af26 100644
--- a/src/kernel/fs/marfs/read_whole_file.c
+++ b/src/kernel/fs/marfs/read_whole_file.c
@@ -1,7 +1,7 @@
#include <stdint.h>
#include <kernel/fs/ata_pio.h>
-#include <kernel/lib/alloc.h>
-#include "marfs.h"
+#include <mlibc/stdlib.h>
+#include <kernel/fs/marfs/marfs.h>
static uint8_t last_maxlevel = 0;
diff --git a/src/kernel/fs/marfs/sectorlevel.c b/src/kernel/fs/marfs/sectorlevel.c
index 7575f8c..d8795fa 100644
--- a/src/kernel/fs/marfs/sectorlevel.c
+++ b/src/kernel/fs/marfs/sectorlevel.c
@@ -1,7 +1,7 @@
#include <stdint.h>
-#include <kernel/lib/alloc.h>
+#include <mlibc/stdlib.h>
#include <kernel/fs/ata_pio.h>
-#include "marfs.h"
+#include <kernel/fs/marfs/marfs.h>
uint8_t marfs_init(struct ATA_INTERFACE *_iface) {
iface = _iface;
diff --git a/src/kernel/graphics/vesa.c b/src/kernel/graphics/vesa.c
index a43d486..9048469 100644
--- a/src/kernel/graphics/vesa.c
+++ b/src/kernel/graphics/vesa.c
@@ -4,7 +4,7 @@
#include <kernel/lib/lib.h>
#include <kernel/paging/paging.h>
#include <kernel/system.h>
-#include <kernel/lib/alloc.h>
+#include <mlibc/stdlib.h>
#include <kernel/commands/command.h>
void switch_to_vga() {
@@ -22,9 +22,7 @@ struct edid_data get_edid() {
regs.bx = 0x1; // BL
regs.es = 0;
regs.di = 0x7E00;
- paging_disable();
- int32(0x10, &regs);
- paging_enable();
+ v86(0x10, &regs);
if ((regs.ax & 0xFF) != 0x4F) {
warn("No EDID available!");
@@ -40,9 +38,7 @@ void vbe_set_mode(unsigned short mode) {
regs.ax = 0x4F02;
regs.bx = mode;
regs.bx |= 0x4000;
- paging_disable();
- int32(0x10, &regs);
- paging_enable();
+ v86(0x10, &regs);
if (regs.ax != 0x004F)
switch_to_vga();
@@ -57,9 +53,7 @@ uint16_t *vbe_get_modes() {
regs.ax = 0x4F00;
regs.es = 0;
regs.di = 0x7E00;
- paging_disable();
- int32(0x10, &regs);
- paging_enable();
+ v86(0x10, &regs);
struct vbe_info *info = (struct vbe_info *) info_address;
@@ -79,9 +73,7 @@ struct vbe_mode_info *vbe_get_mode_info(uint16_t mode) {
regs.cx = mode;
regs.es = 0;
regs.di = 0x7E00;
- paging_disable();
- int32(0x10, &regs);
- paging_enable();
+ v86(0x10, &regs);
struct vbe_mode_info *mode_info = (struct vbe_mode_info *) 0x7E00;
@@ -337,8 +329,7 @@ void vesa_draw_string(const char *data) {
}
void vesa_draw_number(int n) {
- char string[16];
- vesa_draw_string(itoa(n, string));
+ vesa_draw_string(itoa(n));
}
char *prev = 0;
diff --git a/src/kernel/interrupts/isr.c b/src/kernel/interrupts/isr.c
index 0893df7..28f67ae 100644
--- a/src/kernel/interrupts/isr.c
+++ b/src/kernel/interrupts/isr.c
@@ -3,6 +3,7 @@
#include <kernel/lib/lib.h>
#include <kernel/system.h>
#include <kernel/io/io.h>
+#include <mlibc/string.h>
// Defined in isr.asm
extern void isr0();
@@ -175,6 +176,7 @@ void fault_handler(struct regs *r) {
serial_write_hex(r->err_code);
char *message = (char *) exception_messages[r->int_no];
strcat(message, " Exception");
+ if (r->err_code == 2) halt_loop(); // Idk loop?
panic(message);
}
}
diff --git a/src/kernel/io/io.c b/src/kernel/io/io.c
index acbe3dc..1ee76a0 100644
--- a/src/kernel/io/io.c
+++ b/src/kernel/io/io.c
@@ -2,6 +2,8 @@
#include <kernel/lib/lib.h>
#include <kernel/io/io.h>
#include <kernel/system.h>
+#include <mlibc/string.h>
+#include <mlibc/stdlib.h>
uint8_t receive_b(uint16_t port) {
uint8_t value;
@@ -88,6 +90,6 @@ void serial_write_hex(int n) {
}
void serial_write_dec(int n) {
- char string[16];
- serial_write(itoa(n, string));
+ char *text = itoa(n);
+ serial_write(text);
}
diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c
index eb75d32..94d1c20 100644
--- a/src/kernel/kernel.c
+++ b/src/kernel/kernel.c
@@ -16,12 +16,11 @@ extern void switch_to_user();
void kernel_main() {
vga_log("Installing basic features of Melvix...", 0);
// Install features
- // memory_init();
- timer_install();
+ memory_init();
gdt_install();
init_serial();
paging_install();
- acpi_install();
+ // acpi_install();
idt_install();
isrs_install();
irq_install();
@@ -31,6 +30,7 @@ void kernel_main() {
asm volatile ("cli");
mouse_install();
keyboard_install();
+ timer_install();
asm volatile ("sti");
// Get hardware information
@@ -62,7 +62,5 @@ void kernel_main() {
*/
// asm volatile ("div %0" :: "r"(0)); // Exception testing x/0
- loop:
- asm volatile ("hlt");
- goto loop;
+ halt_loop();
}
diff --git a/src/kernel/lib/alloc.c b/src/kernel/lib/alloc.c
deleted file mode 100644
index 4266676..0000000
--- a/src/kernel/lib/alloc.c
+++ /dev/null
@@ -1,446 +0,0 @@
-#include <stddef.h>
-#include <stdint.h>
-#include <kernel/lib/alloc.h>
-#include <kernel/paging/paging.h>
-
-int liballoc_lock() {
- // asm volatile ("cli");
- return 0;
-}
-
-int liballoc_unlock() {
- // asm volatile ("sti");
- return 0;
-}
-
-void *liballoc_alloc(size_t p) {
- uint32_t ptr = paging_alloc_pages((uint32_t) p);
- return (void *) ptr;
-}
-
-int liballoc_free(void *ptr, size_t p) {
- paging_set_free((uint32_t) ptr, (uint32_t) p);
- return 0;
-}
-
-#define ALIGNMENT 16ul
-#define ALIGN_TYPE char
-#define ALIGN_INFO sizeof(ALIGN_TYPE) * 16
-#define USE_CASE1
-#define USE_CASE2
-#define USE_CASE3
-#define USE_CASE4
-#define USE_CASE5
-
-#define ALIGN(ptr) \
- if ( ALIGNMENT > 1 ) { \
- uintptr_t diff; \
- ptr = (void*) ((uintptr_t) ptr + ALIGN_INFO); \
- diff = (uintptr_t) ptr & (ALIGNMENT - 1); \
- if (diff != 0) { \
- diff = ALIGNMENT - diff; \
- ptr = (void*) ((uintptr_t) ptr + diff); \
- } \
- *((ALIGN_TYPE*) ((uintptr_t) ptr - ALIGN_INFO)) = diff + ALIGN_INFO; \
- }
-
-#define UNALIGN(ptr) \
- if (ALIGNMENT > 1) { \
- uintptr_t diff = *((ALIGN_TYPE*) ((uintptr_t) ptr - ALIGN_INFO)); \
- if (diff < (ALIGNMENT + ALIGN_INFO)) { \
- ptr = (void*) ((uintptr_t) ptr - diff); \
- } \
- }
-
-#define LIBALLOC_MAGIC 0xc001c0de
-#define LIBALLOC_DEAD 0xdeaddead
-
-struct liballoc_major {
- struct liballoc_major *prev;
- struct liballoc_major *next;
- unsigned int pages;
- unsigned int size;
- unsigned int usage;
- struct liballoc_minor *first;
-};
-
-struct liballoc_minor {
- struct liballoc_minor *prev;
- struct liballoc_minor *next;
- struct liballoc_major *block;
- unsigned int magic;
- unsigned int size;
- unsigned int req_size;
-};
-
-static struct liballoc_major *l_memRoot = NULL;
-static struct liballoc_major *l_bestBet = NULL;
-
-static unsigned int l_pageSize = 4096;
-static unsigned int l_pageCount = 16;
-static unsigned long long l_allocated = 0;
-static unsigned long long l_inuse = 0;
-
-static long long l_warningCount = 0;
-static long long l_errorCount = 0;
-static long long l_possibleOverruns = 0;
-
-static void *liballoc_memset(void *s, int c, size_t n) {
- unsigned int i;
- for (i = 0; i < n; i++)
- ((char *) s)[i] = c;
-
- return s;
-}
-
-static void *liballoc_memcpy(void *s1, const void *s2, size_t n) {
- char *cdest;
- char *csrc;
- unsigned int *ldest = (unsigned int *) s1;
- unsigned int *lsrc = (unsigned int *) s2;
-
- while (n >= sizeof(unsigned int)) {
- *ldest++ = *lsrc++;
- n -= sizeof(unsigned int);
- }
-
- cdest = (char *) ldest;
- csrc = (char *) lsrc;
-
- while (n > 0) {
- *cdest++ = *csrc++;
- n -= 1;
- }
- return s1;
-}
-
-static struct liballoc_major *allocate_new_page(unsigned int size) {
- unsigned int st;
- struct liballoc_major *maj;
-
- st = size + sizeof(struct liballoc_major);
- st += sizeof(struct liballoc_minor);
-
- if ((st % l_pageSize) == 0)
- st = st / (l_pageSize);
- else
- st = st / (l_pageSize) + 1;
-
- if (st < l_pageCount) st = l_pageCount;
-
- maj = (struct liballoc_major *) liballoc_alloc(st);
-
- if (maj == NULL) {
- l_warningCount += 1;
- return NULL;
- }
-
- maj->prev = NULL;
- maj->next = NULL;
- maj->pages = st;
- maj->size = st * l_pageSize;
- maj->usage = sizeof(struct liballoc_major);
- maj->first = NULL;
-
- l_allocated += maj->size;
-
- return maj;
-}
-
-void *PREFIX(malloc)(size_t req_size) {
- int startedBet = 0;
- unsigned long long bestSize = 0;
- void *p = NULL;
- uintptr_t diff;
- struct liballoc_major *maj;
- struct liballoc_minor *min;
- struct liballoc_minor *new_min;
- unsigned long size = req_size;
-
- if (ALIGNMENT > 1) {
- size += ALIGNMENT + ALIGN_INFO;
- }
-
- liballoc_lock();
-
- if (size == 0) {
- l_warningCount += 1;
- liballoc_unlock();
- return PREFIX(malloc)(1);
- }
-
- if (l_memRoot == NULL) {
- l_memRoot = allocate_new_page(size);
- if (l_memRoot == NULL) {
- liballoc_unlock();
- return NULL;
- }
- }
-
- maj = l_memRoot;
- startedBet = 0;
-
- if (l_bestBet != NULL) {
- bestSize = l_bestBet->size - l_bestBet->usage;
-
- if (bestSize > (size + sizeof(struct liballoc_minor))) {
- maj = l_bestBet;
- startedBet = 1;
- }
- }
-
- while (maj != NULL) {
- diff = maj->size - maj->usage;
- if (bestSize < diff) {
- l_bestBet = maj;
- bestSize = diff;
- }
-
-#ifdef USE_CASE1
- if (diff < (size + sizeof(struct liballoc_minor))) {
- if (maj->next != NULL) {
- maj = maj->next;
- continue;
- }
-
- if (startedBet == 1) {
- maj = l_memRoot;
- startedBet = 0;
- continue;
- }
-
- maj->next = allocate_new_page(size);
- if (maj->next == NULL) break;
- maj->next->prev = maj;
- maj = maj->next;
- }
-#endif
-
-#ifdef USE_CASE2
- if (maj->first == NULL) {
- maj->first = (struct liballoc_minor *) ((uintptr_t) maj + sizeof(struct liballoc_major));
-
- maj->first->magic = LIBALLOC_MAGIC;
- maj->first->prev = NULL;
- maj->first->next = NULL;
- maj->first->block = maj;
- maj->first->size = size;
- maj->first->req_size = req_size;
- maj->usage += size + sizeof(struct liballoc_minor);
- l_inuse += size;
- p = (void *) ((uintptr_t) (maj->first) + sizeof(struct liballoc_minor));
- ALIGN(p);
- liballoc_unlock();
- return p;
- }
-#endif
-
-#ifdef USE_CASE3
- diff = (uintptr_t) (maj->first);
- diff -= (uintptr_t) maj;
- diff -= sizeof(struct liballoc_major);
-
- if (diff >= (size + sizeof(struct liballoc_minor))) {
- maj->first->prev = (struct liballoc_minor *) ((uintptr_t) maj + sizeof(struct liballoc_major));
- maj->first->prev->next = maj->first;
- maj->first = maj->first->prev;
- maj->first->magic = LIBALLOC_MAGIC;
- maj->first->prev = NULL;
- maj->first->block = maj;
- maj->first->size = size;
- maj->first->req_size = req_size;
- maj->usage += size + sizeof(struct liballoc_minor);
- l_inuse += size;
- p = (void *) ((uintptr_t) (maj->first) + sizeof(struct liballoc_minor));
- ALIGN(p);
- liballoc_unlock();
- return p;
- }
-#endif
-
-#ifdef USE_CASE4
- min = maj->first;
-
- while (min != NULL) {
- if (min->next == NULL) {
- diff = (uintptr_t) (maj) + maj->size;
- diff -= (uintptr_t) min;
- diff -= sizeof(struct liballoc_minor);
- diff -= min->size;
- if (diff >= (size + sizeof(struct liballoc_minor))) {
- min->next = (struct liballoc_minor *) ((uintptr_t) min + sizeof(struct liballoc_minor) + min->size);
- min->next->prev = min;
- min = min->next;
- min->next = NULL;
- min->magic = LIBALLOC_MAGIC;
- min->block = maj;
- min->size = size;
- min->req_size = req_size;
- maj->usage += size + sizeof(struct liballoc_minor);
- l_inuse += size;
- p = (void *) ((uintptr_t) min + sizeof(struct liballoc_minor));
- ALIGN(p);
- liballoc_unlock();
- return p;
- }
- }
-
- if (min->next != NULL) {
- diff = (uintptr_t) (min->next);
- diff -= (uintptr_t) min;
- diff -= sizeof(struct liballoc_minor);
- diff -= min->size;
-
- if (diff >= (size + sizeof(struct liballoc_minor))) {
- new_min = (struct liballoc_minor *) ((uintptr_t) min + sizeof(struct liballoc_minor) + min->size);
- new_min->magic = LIBALLOC_MAGIC;
- new_min->next = min->next;
- new_min->prev = min;
- new_min->size = size;
- new_min->req_size = req_size;
- new_min->block = maj;
- min->next->prev = new_min;
- min->next = new_min;
- maj->usage += size + sizeof(struct liballoc_minor);
- l_inuse += size;
- p = (void *) ((uintptr_t) new_min + sizeof(struct liballoc_minor));
- ALIGN(p);
- liballoc_unlock();
- return p;
- }
- }
-
- min = min->next;
- }
-#endif
-
-#ifdef USE_CASE5
- if (maj->next == NULL) {
- if (startedBet == 1) {
- maj = l_memRoot;
- startedBet = 0;
- continue;
- }
- maj->next = allocate_new_page(size);
- if (maj->next == NULL) break;
- maj->next->prev = maj;
- }
-#endif
- maj = maj->next;
- }
-
- liballoc_unlock();
-
- return NULL;
-}
-
-void PREFIX(free)(void *ptr) {
- struct liballoc_minor *min;
- struct liballoc_major *maj;
-
- if (ptr == NULL) {
- l_warningCount += 1;
- return;
- }
-
- UNALIGN(ptr);
- liballoc_lock();
-
- min = (struct liballoc_minor *) ((uintptr_t) ptr - sizeof(struct liballoc_minor));
-
- if (min->magic != LIBALLOC_MAGIC) {
- l_errorCount += 1;
-
- if (((min->magic & 0xFFFFFF) == (LIBALLOC_MAGIC & 0xFFFFFF)) ||
- ((min->magic & 0xFFFF) == (LIBALLOC_MAGIC & 0xFFFF)) ||
- ((min->magic & 0xFF) == (LIBALLOC_MAGIC & 0xFF))) {
- l_possibleOverruns += 1;
- }
-
- liballoc_unlock();
- return;
- }
-
- maj = min->block;
- l_inuse -= min->size;
- maj->usage -= (min->size + sizeof(struct liballoc_minor));
- min->magic = LIBALLOC_DEAD;
-
- if (min->next != NULL) min->next->prev = min->prev;
- if (min->prev != NULL) min->prev->next = min->next;
- if (min->prev == NULL) maj->first = min->next;
- if (maj->first == NULL) {
- if (l_memRoot == maj) l_memRoot = maj->next;
- if (l_bestBet == maj) l_bestBet = NULL;
- if (maj->prev != NULL) maj->prev->next = maj->next;
- if (maj->next != NULL) maj->next->prev = maj->prev;
- l_allocated -= maj->size;
- liballoc_free(maj, maj->pages);
- } else {
- if (l_bestBet != NULL) {
- int bestSize = l_bestBet->size - l_bestBet->usage;
- int majSize = maj->size - maj->usage;
- if (majSize > bestSize) l_bestBet = maj;
- }
- }
- liballoc_unlock();
-}
-
-void *PREFIX(calloc)(size_t nobj, size_t size) {
- int real_size;
- void *p;
-
- real_size = nobj * size;
-
- p = PREFIX(malloc)(real_size);
-
- liballoc_memset(p, 0, real_size);
-
- return p;
-}
-
-void *PREFIX(realloc)(void *p, size_t size) {
- void *ptr;
- struct liballoc_minor *min;
- unsigned int real_size;
-
- if (size == 0) {
- PREFIX(free)(p);
- return NULL;
- }
-
- if (p == NULL) return PREFIX(malloc)(size);
-
- ptr = p;
- UNALIGN(ptr);
- liballoc_lock();
- min = (struct liballoc_minor *) ((uintptr_t) ptr - sizeof(struct liballoc_minor));
-
- if (min->magic != LIBALLOC_MAGIC) {
- l_errorCount += 1;
- if (((min->magic & 0xFFFFFF) == (LIBALLOC_MAGIC & 0xFFFFFF)) ||
- ((min->magic & 0xFFFF) == (LIBALLOC_MAGIC & 0xFFFF)) ||
- ((min->magic & 0xFF) == (LIBALLOC_MAGIC & 0xFF))) {
- l_possibleOverruns += 1;
- }
-
- liballoc_unlock();
- return NULL;
- }
-
- real_size = min->req_size;
-
- if (real_size >= size) {
- min->req_size = size;
- liballoc_unlock();
- return p;
- }
-
- liballoc_unlock();
-
- ptr = PREFIX(malloc)(size);
- liballoc_memcpy(ptr, p, real_size);
- PREFIX(free)(p);
-
- return ptr;
-}
diff --git a/src/kernel/lib/alloc.h b/src/kernel/lib/alloc.h
deleted file mode 100644
index 6ed9efb..0000000
--- a/src/kernel/lib/alloc.h
+++ /dev/null
@@ -1,24 +0,0 @@
-#ifndef MELVIX_ALLOC_H
-#define MELVIX_ALLOC_H
-
-#include <stddef.h>
-
-#define PREFIX(func) k ## func
-
-int liballoc_lock();
-
-int liballoc_unlock();
-
-void *liballoc_alloc(size_t);
-
-int liballoc_free(void *, size_t);
-
-void *PREFIX(malloc)(size_t);
-
-void *PREFIX(realloc)(void *, size_t);
-
-void *PREFIX(calloc)(size_t, size_t);
-
-void PREFIX(free)(void *);
-
-#endif
diff --git a/src/kernel/lib/lib.h b/src/kernel/lib/lib.h
index c431e1d..e94ad19 100644
--- a/src/kernel/lib/lib.h
+++ b/src/kernel/lib/lib.h
@@ -5,43 +5,6 @@
#include <stdint.h>
/**
- * Find the length of a string
- * @param str The string pointer which size should be calculated
- * @return The length of str
- */
-size_t strlen(const char *str);
-
-/**
- * Compare two strings
- * @param s1 The first string pointer
- * @param s2 The second string pointer
- * @return Non-zero if not equal
- */
-size_t strcmp(const char *s1, const char *s2);
-
-/**
- * Append the data of src to dest
- * @param dest The string destination pointer
- * @param src The string pointer that will get appended
- */
-void strcat(char *dest, const char *src);
-
-/**
- * Copy the data of src to dest
- * @param dest The copying destination pointer (gets replaced)
- * @param src The string pointer that will get copied
- */
-void strcpy(char *dest, const char *src);
-
-/**
- * Convert an integer to a char array (string)
- * @param i The integer
- * @param b The char array
- * @return The char pointer
- */
-char *itoa(int i, char b[]);
-
-/**
* Copy n data from src to dest
* @param dest The destination array pointer
* @param src The source array pointer of the data
diff --git a/src/kernel/lib/memory.c b/src/kernel/lib/memory.c
index 3132e05..0659406 100644
--- a/src/kernel/lib/memory.c
+++ b/src/kernel/lib/memory.c
@@ -1,7 +1,6 @@
#include <stddef.h>
#include <stdint.h>
#include <kernel/paging/paging.h>
-#include <kernel/io/io.h>
void *memcpy(void *dest, const void *src, size_t count) {
const char *sp = (const char *) src;
@@ -51,7 +50,6 @@ void memory_init() {
}
total_memory = maxbase + maxlength;
total_memory /= 1024;
- serial_write_dec(total_memory);
}
uint32_t memory_get_free() {
diff --git a/src/kernel/lib/string.c b/src/kernel/lib/string.c
deleted file mode 100644
index 12b4b06..0000000
--- a/src/kernel/lib/string.c
+++ /dev/null
@@ -1,52 +0,0 @@
-#include <stddef.h>
-#include <kernel/io/io.h>
-
-size_t strlen(const char *str) {
- size_t len = 0;
- while (str[len])
- len++;
- return len;
-}
-
-size_t strcmp(const char *s1, const char *s2) {
- size_t s_a = strlen(s1);
- for (size_t i = 0; i < s_a; i++)
- if (s1[i] != s2[i])
- return 1;
- return 0;
-}
-
-void strcat(char *dest, const char *src) {
- size_t s_dest = strlen(dest);
- size_t s_orig = strlen(src);
-
- for (size_t i = 0; i < s_orig; i++) dest[s_dest + i] = src[i];
- dest[s_dest + s_orig] = 0;
-}
-
-void strcpy(char *dest, const char *src) {
- size_t s_orig = strlen(src);
-
- for (size_t i = 0; i < s_orig; i++) dest[i] = src[i];
- dest[s_orig] = 0;
-}
-
-char *itoa(int i, char b[]) {
- char const digit[] = "0123456789";
- char *p = b;
- if (i < 0) {
- *p++ = '-';
- i *= -1;
- }
- int shifter = i;
- do {
- ++p;
- shifter = shifter / 10;
- } while (shifter);
- *p = '\0';
- do {
- *--p = digit[i % 10];
- i = i / 10;
- } while (i);
- return b;
-} \ No newline at end of file
diff --git a/src/kernel/linker.ld b/src/kernel/linker.ld
index 8ca4406..b338ba6 100644
--- a/src/kernel/linker.ld
+++ b/src/kernel/linker.ld
@@ -1,7 +1,6 @@
ENTRY(_start)
SECTIONS {
- /* Begin @ 1 MB */
. = 1M;
.text BLOCK(4K) : ALIGN(4K) {
diff --git a/src/kernel/paging/paging.c b/src/kernel/paging/paging.c
index fa0610e..a08d253 100644
--- a/src/kernel/paging/paging.c
+++ b/src/kernel/paging/paging.c
@@ -1,8 +1,10 @@
#include <stdint.h>
#include <kernel/paging/paging.h>
#include <kernel/system.h>
+#include <kernel/io/io.h>
#include <kernel/lib/lib.h>
+int paging_enabled = 0;
uint32_t page_directory[1024] __attribute__((aligned(4096)));
uint32_t page_tables[1024][1024] __attribute__((aligned(4096)));
@@ -18,26 +20,20 @@ void paging_install() {
}
// TODO: Calculate max memory
- // paging_set_present(0, memory_get_all() >> 2);
- paging_set_present(0, 0x1000000);
-
- paging_set_used(0, ((uint32_t) ASM_KERNEL_END >> 12) + 1);
+ paging_set_present(0, memory_get_all() >> 2); // /4
+ paging_set_used(0, ((uint32_t) ASM_KERNEL_END >> 12) + 1); // /4096
paging_enable();
+ serial_write_dec(memory_get_all() / 1024);
vga_log("Installed paging", 4);
}
-int paging_enabled() {
- uint32_t cr0;
- asm volatile("mov %%cr0, %0": "=r"(cr0));
- return (cr0 | 0x80000000) == cr0;
-}
-
void paging_disable() {
uint32_t cr0;
asm volatile("mov %%cr0, %0": "=r"(cr0));
cr0 &= 0x7fffffff;
asm volatile("mov %0, %%cr0"::"r"(cr0));
+ paging_enabled = 0;
}
void paging_enable() {
@@ -46,6 +42,7 @@ void paging_enable() {
asm volatile("mov %%cr0, %0": "=r"(cr0));
cr0 |= 0x80000000;
asm volatile("mov %0, %%cr0"::"r"(cr0));
+ paging_enabled = 1;
}
inline void invlpg(uint32_t addr) {
diff --git a/src/kernel/paging/paging.h b/src/kernel/paging/paging.h
index 85ef120..270c082 100644
--- a/src/kernel/paging/paging.h
+++ b/src/kernel/paging/paging.h
@@ -21,6 +21,8 @@
#define PT_GLOBAL 1 << 8
#define PT_USED 1 << 9
+int paging_enabled;
+
void paging_install();
void paging_enable();
diff --git a/src/kernel/system.c b/src/kernel/system.c
index 303fc83..560f6a5 100644
--- a/src/kernel/system.c
+++ b/src/kernel/system.c
@@ -1,7 +1,10 @@
#include <kernel/timer/timer.h>
#include <kernel/io/io.h>
#include <kernel/graphics/vesa.h>
-#include <kernel/lib/lib.h>
+#include <mlibc/string.h>
+#include <mlibc/stdlib.h>
+#include <kernel/paging/paging.h>
+#include <kernel/interrupts/interrupts.h>
char *vga_buffer = (char *) 0x500;
@@ -18,9 +21,8 @@ void vga_log(char *msg, int line) {
for (size_t i = 0; i < strlen(msg); i++)
terminal_buffer[line * 80 + i] = (uint16_t) msg[i] | (uint16_t) 0x700;
char string[80];
- char time[8];
strcpy(string, "[");
- strcat(string, itoa((int) get_time(), time));
+ strcat(string, itoa((int) get_time()));
strcat(string, "] ");
strcat(string, "INFORMATION: ");
strcat(string, msg);
@@ -68,9 +70,7 @@ void panic(char *msg) {
vesa_draw_string("PANIC: ");
vesa_draw_string(msg);
vesa_draw_string(" - System halted!\n");
- loop:
- asm volatile ("hlt");
- goto loop;
+ halt_loop();
}
void assert(int x) {
@@ -78,3 +78,16 @@ void assert(int x) {
panic("Assertion failed");
}
}
+
+void halt_loop() {
+ serial_write("\n!!! HALT !!!\n");
+ loop:
+ asm volatile ("hlt");
+ goto loop;
+}
+
+void v86(uint8_t code, regs16_t *regs) {
+ paging_disable();
+ int32(code, regs);
+ paging_enable();
+}
diff --git a/src/kernel/system.h b/src/kernel/system.h
index 18a013b..1fe2026 100644
--- a/src/kernel/system.h
+++ b/src/kernel/system.h
@@ -4,7 +4,7 @@
/**
* The kernel end
*/
-extern void *ASM_KERNEL_END;
+extern void ASM_KERNEL_END();
/**
* Initialize the basic features of the OS
@@ -76,4 +76,16 @@ void panic(char *msg);
*/
void assert(int x);
+/**
+ * Creates an infinite halt loop
+ */
+void halt_loop();
+
+/**
+ * Executes int32 with paging disable/enable
+ * @param code The interrupt code
+ * @param regs The registers
+ */
+void v86(uint8_t code, regs16_t *regs);
+
#endif
diff --git a/src/mlibc/stdlib/itoa.c b/src/mlibc/stdlib/itoa.c
index e9c40c1..8c557ab 100644
--- a/src/mlibc/stdlib/itoa.c
+++ b/src/mlibc/stdlib/itoa.c
@@ -2,11 +2,14 @@
#include <stdint.h>
#include <mlibc/string.h>
#include <mlibc/stdlib.h>
+#include <kernel/paging/paging.h>
static const char __ITOA_TABLE[] = "0123456789";
char *itoa(int n) {
- // Special cases
+ if (paging_enabled == 0)
+ return "0"; // kmalloc isn't available
+
if (!n) {
char *ret = kmalloc(2);
ret[0] = '0';
@@ -16,13 +19,11 @@ char *itoa(int n) {
uint8_t negative = (uint8_t) (n < 0);
if (negative) n *= -1;
- // First get the number of digits.
int sz;
for (sz = 0; n % pow(10, sz) != n; sz++) {}
char *ret = kmalloc(sz + 1);
- // Iterate all digits again.
for (int i = 0; i < sz; i++) {
int digit = (n % pow(10, i + 1)) / pow(10, i);
ret[i] = __ITOA_TABLE[digit];