summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/kernel/files.h2
-rw-r--r--src/kernel/main.c46
-rw-r--r--src/kernel/mem.c29
-rw-r--r--src/kernel/mem.h4
-rw-r--r--src/kernel/proc.h2
-rw-r--r--src/kernel/vga.c24
-rw-r--r--src/kernel/vga.h6
7 files changed, 62 insertions, 51 deletions
diff --git a/src/kernel/files.h b/src/kernel/files.h
index 38e59f1..f7665dc 100644
--- a/src/kernel/files.h
+++ b/src/kernel/files.h
@@ -29,7 +29,7 @@ struct file_info {
uint32_t position;
};
-struct file_info *file_table;
+struct file_info file_table[65536];
uint16_t open_file(uint8_t *name);
void close_file(uint16_t handle);
void read_file(uint16_t handle, uint32_t length, void *buffer);
diff --git a/src/kernel/main.c b/src/kernel/main.c
index ab8eb91..97226bd 100644
--- a/src/kernel/main.c
+++ b/src/kernel/main.c
@@ -60,6 +60,7 @@ struct boot_device_tag {
enum mem_type {
AVAILABLE = 1,
+ HARDWARET = 2,
ACPI = 3,
PRESERVE = 4,
DEFECTIVE = 5
@@ -78,7 +79,8 @@ bool have_mmap = false;
enum error_codes {
NO_BOOT_DEVICE = 0x00000000,
NO_MMAP = 0x00000001,
- INSUFF_MEMORY = 0x00000002
+ INSUFF_MEMORY = 0x00000002,
+ MMAP_TOO_SMALL = 0x00000003
};
struct tag_start *tag_pointer;
@@ -128,7 +130,9 @@ uint32_t main(void) {
uint32_t size = *(uint32_t *)(tag_pointer + 1);
struct mmap_tag_entry *tag = (struct mmap_tag_entry *)(tag_pointer + 2);
struct mmap_tag_entry *end = (struct mmap_tag_entry *)((uint32_t)tag_pointer + tag_pointer->size);
- struct mmap_entry *entry = (struct mmap_entry *)tag;
+ struct mmap_entry *entry = mmap_bss;
+ uint32_t bitmask = 0x00000001;
+ uint32_t byte = 0;
struct mmap_entry **last_next = &mmap_start;
uint32_t usable = 0;
put_sz(": memory map\n Regions:\n");
@@ -147,20 +151,21 @@ uint32_t main(void) {
entry->whose = FREE;
usable += entry->length;
break;
- case ACPI:
- put_sz(": ACPI info\n");
- entry->whose = HARDWARE;
- break;
+ case HARDWARET:
case PRESERVE:
put_sz(": hardware use\n");
entry->whose = HARDWARE;
break;
+ case ACPI:
+ put_sz(": ACPI info\n");
+ entry->whose = HARDWARE;
+ break;
case DEFECTIVE:
put_sz(": defective\n");
entry->whose = HARDWARE;
break;
default:
- put_sz(": hardware use (0x");
+ put_sz(": assuming hardware use (0x");
put_32_hex(tag->type);
put_sz(")\n");
entry->whose = HARDWARE;
@@ -168,12 +173,18 @@ uint32_t main(void) {
}
*last_next = entry;
last_next = &entry->next;
+ mmap_bitmap[byte] |= bitmask;
++entry;
+ if (!(bitmask <<= 1)) {
+ bitmask = 0x00000001;
+ if (++byte == MMAP_SIZE)
+ return MMAP_TOO_SMALL;
+ }
}
tag = (struct mmap_tag_entry *)((uint32_t)tag + size);
}
- put_sz(" Total usable memory: ");
+ put_sz(" Total usable memory: 0x");
put_32_hex(usable);
put_char('\n');
@@ -185,6 +196,7 @@ uint32_t main(void) {
if ((current->base + current->length == against->base) &&
(current->whose == against->whose)) {
current->length += against->length;
+ unmark_entry(against);
if (against == mmap_start)
mmap_start = against->next;
else {
@@ -211,17 +223,17 @@ uint32_t main(void) {
if (!have_mmap)
return NO_MMAP;
- put_sz("Halting.");
- while (1)
- ;
-
- if (!((proc_table = allocate_block(sizeof(struct proc_info) * 65536, KERNEL)) &&
- (file_table = allocate_block(sizeof(struct file_info) * 65536, KERNEL))))
- return INSUFF_MEMORY;
+ put_sz("\nReady!\n Memory map size: 0x");
+ put_32_hex(sizeof(struct mmap_entry) * MMAP_SIZE);
+ put_sz("\n Process table size: 0x");
+ put_32_hex(sizeof(struct proc_info) * 65536);
+ put_sz("\n File table size: 0x");
+ put_32_hex(sizeof(struct file_info) * 65536);
+ put_char('\n');
- put_sz("Welcome to Portland version 0.0.9!\n");
+ put_sz("\nWelcome to Portland version 0.0.9!\n");
while (1)
- put_sz("|\b/\b-\b\\\b");
+ asm ("hlt");
}
void wrapped_main(void) {
diff --git a/src/kernel/mem.c b/src/kernel/mem.c
index 636c23e..99586d9 100644
--- a/src/kernel/mem.c
+++ b/src/kernel/mem.c
@@ -19,6 +19,9 @@ OF THIS SOFTWARE.
#include "mem.h"
+uint32_t last_byte = 0;
+uint32_t last_mask = 0x00000001;
+
void *allocate_block(uint32_t size, uint16_t proc_n) {
struct mmap_entry *i = mmap_start;
while (i->whose || (i->length < size))
@@ -33,8 +36,28 @@ void *allocate_block(uint32_t size, uint16_t proc_n) {
uint32_t remaining = i->length - size;
i->whose = proc_n;
i->length = size;
- struct mmap_entry *new;
- //TODO: ALLOCATE NEW
+
+ uint32_t byte = last_byte;
+ uint32_t mask = last_mask;
+ do {
+ if (!(mask <<= 1)) {
+ mask = 0x00000001;
+ if (++byte == MMAP_SIZE)
+ byte = 0;
+ }
+ if ((byte == last_byte) && (mask == last_mask))
+ return 0;
+ } while (mmap_bitmap[byte] & mask);
+ last_byte = byte;
+ last_mask = mask;
+ mmap_bitmap[byte] |= mask;
+ uint8_t bit = 0;
+ while (!(mask & 0x00000001)) {
+ ++bit;
+ mask >>= 1;
+ }
+
+ struct mmap_entry *new = mmap_bss + (byte << 5) + bit;
new->base = i->base + size;
new->length = remaining;
new->whose = FREE;
@@ -57,6 +80,7 @@ void deallocate_block(void *start) {
if (!find_after->whose) {
find->length += find_after->length;
find->next = find_after->next;
+ unmark_entry(find_after);
}
break;
}
@@ -66,6 +90,7 @@ void deallocate_block(void *start) {
if (!find_before->whose) {
find_before->length += find->length;
find_before->next = find->next;
+ unmark_entry(find);
}
break;
}
diff --git a/src/kernel/mem.h b/src/kernel/mem.h
index 106db90..5c0d387 100644
--- a/src/kernel/mem.h
+++ b/src/kernel/mem.h
@@ -35,7 +35,11 @@ struct mmap_entry {
struct mmap_entry *next;
};
+#define MMAP_SIZE 65536
struct mmap_entry *mmap_start;
+struct mmap_entry mmap_bss[MMAP_SIZE];
+uint32_t mmap_bitmap[MMAP_SIZE / 32];
+#define unmark_entry(entry) mmap_bitmap[((entry) - mmap_bss) >> 5] ^= 1 << (((entry) - mmap_bss) & 0x0000001f);
void *allocate_block(uint32_t size, uint16_t proc_n);
void deallocate_block(void *start);
diff --git a/src/kernel/proc.h b/src/kernel/proc.h
index 261ae6b..bbff903 100644
--- a/src/kernel/proc.h
+++ b/src/kernel/proc.h
@@ -27,7 +27,7 @@ struct proc_info {
void *memory_end;
};
-struct proc_info *proc_table;
+struct proc_info proc_table[65536];
uint16_t new_proc(uint8_t *file);
void end_proc(uint16_t id);
diff --git a/src/kernel/vga.c b/src/kernel/vga.c
index 78da721..5fd8a46 100644
--- a/src/kernel/vga.c
+++ b/src/kernel/vga.c
@@ -70,30 +70,6 @@ void put_sz(uint8_t *sz) {
put_char(*(sz++));
}
-void put_16(uint16_t n) {
- bool s = false;
- if (n / 10000) {
- put_char((uint8_t)'0' + n / 10000);
- s = true;
- }
- n %= 10000;
- if (n / 1000 || s) {
- put_char((uint8_t)'0' + n / 1000);
- s = true;
- }
- n %= 1000;
- if (n / 100 || s) {
- put_char((uint8_t)'0' + n / 100);
- s = true;
- }
- n %= 100;
- if (n / 10 || s) {
- put_char((uint8_t)'0' + n / 10);
- s = true;
- }
- put_char((uint8_t)'0' + n % 10);
-}
-
void put_32_hex(uint32_t n) {
for (uint8_t i = 0; i < 4; ++i) {
put_char("0123456789abcdef"[n >> 28]);
diff --git a/src/kernel/vga.h b/src/kernel/vga.h
index 5658437..2504128 100644
--- a/src/kernel/vga.h
+++ b/src/kernel/vga.h
@@ -22,16 +22,10 @@ OF THIS SOFTWARE.
#ifndef VGA_H
#define VGA_H
-#define put_debug(name, value) \
- put_sz(name ": 0x"); \
- put_32_hex(value); \
- put_char('\n');
-
#define VGA_BUFFER ((uint8_t *)0x000b8000)
void clear(void);
void put_char(uint8_t ch);
void put_sz(uint8_t *s);
-void put_16(uint16_t n);
void put_32_hex(uint32_t n);
void move_cursor(uint8_t col, uint8_t row);
void set_color(uint8_t c);