working memory manager

This commit is contained in:
Benji Dial 2019-12-26 15:50:21 -05:00
parent ef72082b2d
commit e0e08a12d4
7 changed files with 62 additions and 51 deletions

View file

@ -29,7 +29,7 @@ struct file_info {
uint32_t position; uint32_t position;
}; };
struct file_info *file_table; struct file_info file_table[65536];
uint16_t open_file(uint8_t *name); uint16_t open_file(uint8_t *name);
void close_file(uint16_t handle); void close_file(uint16_t handle);
void read_file(uint16_t handle, uint32_t length, void *buffer); void read_file(uint16_t handle, uint32_t length, void *buffer);

View file

@ -60,6 +60,7 @@ struct boot_device_tag {
enum mem_type { enum mem_type {
AVAILABLE = 1, AVAILABLE = 1,
HARDWARET = 2,
ACPI = 3, ACPI = 3,
PRESERVE = 4, PRESERVE = 4,
DEFECTIVE = 5 DEFECTIVE = 5
@ -78,7 +79,8 @@ bool have_mmap = false;
enum error_codes { enum error_codes {
NO_BOOT_DEVICE = 0x00000000, NO_BOOT_DEVICE = 0x00000000,
NO_MMAP = 0x00000001, NO_MMAP = 0x00000001,
INSUFF_MEMORY = 0x00000002 INSUFF_MEMORY = 0x00000002,
MMAP_TOO_SMALL = 0x00000003
}; };
struct tag_start *tag_pointer; struct tag_start *tag_pointer;
@ -128,7 +130,9 @@ uint32_t main(void) {
uint32_t size = *(uint32_t *)(tag_pointer + 1); uint32_t size = *(uint32_t *)(tag_pointer + 1);
struct mmap_tag_entry *tag = (struct mmap_tag_entry *)(tag_pointer + 2); 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_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; struct mmap_entry **last_next = &mmap_start;
uint32_t usable = 0; uint32_t usable = 0;
put_sz(": memory map\n Regions:\n"); put_sz(": memory map\n Regions:\n");
@ -147,20 +151,21 @@ uint32_t main(void) {
entry->whose = FREE; entry->whose = FREE;
usable += entry->length; usable += entry->length;
break; break;
case ACPI: case HARDWARET:
put_sz(": ACPI info\n");
entry->whose = HARDWARE;
break;
case PRESERVE: case PRESERVE:
put_sz(": hardware use\n"); put_sz(": hardware use\n");
entry->whose = HARDWARE; entry->whose = HARDWARE;
break; break;
case ACPI:
put_sz(": ACPI info\n");
entry->whose = HARDWARE;
break;
case DEFECTIVE: case DEFECTIVE:
put_sz(": defective\n"); put_sz(": defective\n");
entry->whose = HARDWARE; entry->whose = HARDWARE;
break; break;
default: default:
put_sz(": hardware use (0x"); put_sz(": assuming hardware use (0x");
put_32_hex(tag->type); put_32_hex(tag->type);
put_sz(")\n"); put_sz(")\n");
entry->whose = HARDWARE; entry->whose = HARDWARE;
@ -168,12 +173,18 @@ uint32_t main(void) {
} }
*last_next = entry; *last_next = entry;
last_next = &entry->next; last_next = &entry->next;
mmap_bitmap[byte] |= bitmask;
++entry; ++entry;
if (!(bitmask <<= 1)) {
bitmask = 0x00000001;
if (++byte == MMAP_SIZE)
return MMAP_TOO_SMALL;
}
} }
tag = (struct mmap_tag_entry *)((uint32_t)tag + size); 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_32_hex(usable);
put_char('\n'); put_char('\n');
@ -185,6 +196,7 @@ uint32_t main(void) {
if ((current->base + current->length == against->base) && if ((current->base + current->length == against->base) &&
(current->whose == against->whose)) { (current->whose == against->whose)) {
current->length += against->length; current->length += against->length;
unmark_entry(against);
if (against == mmap_start) if (against == mmap_start)
mmap_start = against->next; mmap_start = against->next;
else { else {
@ -211,17 +223,17 @@ uint32_t main(void) {
if (!have_mmap) if (!have_mmap)
return NO_MMAP; return NO_MMAP;
put_sz("Halting."); put_sz("\nReady!\n Memory map size: 0x");
while (1) 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');
if (!((proc_table = allocate_block(sizeof(struct proc_info) * 65536, KERNEL)) && put_sz("\nWelcome to Portland version 0.0.9!\n");
(file_table = allocate_block(sizeof(struct file_info) * 65536, KERNEL))))
return INSUFF_MEMORY;
put_sz("Welcome to Portland version 0.0.9!\n");
while (1) while (1)
put_sz("|\b/\b-\b\\\b"); asm ("hlt");
} }
void wrapped_main(void) { void wrapped_main(void) {

View file

@ -19,6 +19,9 @@ OF THIS SOFTWARE.
#include "mem.h" #include "mem.h"
uint32_t last_byte = 0;
uint32_t last_mask = 0x00000001;
void *allocate_block(uint32_t size, uint16_t proc_n) { void *allocate_block(uint32_t size, uint16_t proc_n) {
struct mmap_entry *i = mmap_start; struct mmap_entry *i = mmap_start;
while (i->whose || (i->length < size)) 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; uint32_t remaining = i->length - size;
i->whose = proc_n; i->whose = proc_n;
i->length = size; 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->base = i->base + size;
new->length = remaining; new->length = remaining;
new->whose = FREE; new->whose = FREE;
@ -57,6 +80,7 @@ void deallocate_block(void *start) {
if (!find_after->whose) { if (!find_after->whose) {
find->length += find_after->length; find->length += find_after->length;
find->next = find_after->next; find->next = find_after->next;
unmark_entry(find_after);
} }
break; break;
} }
@ -66,6 +90,7 @@ void deallocate_block(void *start) {
if (!find_before->whose) { if (!find_before->whose) {
find_before->length += find->length; find_before->length += find->length;
find_before->next = find->next; find_before->next = find->next;
unmark_entry(find);
} }
break; break;
} }

View file

@ -35,7 +35,11 @@ struct mmap_entry {
struct mmap_entry *next; struct mmap_entry *next;
}; };
#define MMAP_SIZE 65536
struct mmap_entry *mmap_start; 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 *allocate_block(uint32_t size, uint16_t proc_n);
void deallocate_block(void *start); void deallocate_block(void *start);

View file

@ -27,7 +27,7 @@ struct proc_info {
void *memory_end; void *memory_end;
}; };
struct proc_info *proc_table; struct proc_info proc_table[65536];
uint16_t new_proc(uint8_t *file); uint16_t new_proc(uint8_t *file);
void end_proc(uint16_t id); void end_proc(uint16_t id);

View file

@ -70,30 +70,6 @@ void put_sz(uint8_t *sz) {
put_char(*(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) { void put_32_hex(uint32_t n) {
for (uint8_t i = 0; i < 4; ++i) { for (uint8_t i = 0; i < 4; ++i) {
put_char("0123456789abcdef"[n >> 28]); put_char("0123456789abcdef"[n >> 28]);

View file

@ -22,16 +22,10 @@ OF THIS SOFTWARE.
#ifndef VGA_H #ifndef VGA_H
#define 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) #define VGA_BUFFER ((uint8_t *)0x000b8000)
void clear(void); void clear(void);
void put_char(uint8_t ch); void put_char(uint8_t ch);
void put_sz(uint8_t *s); void put_sz(uint8_t *s);
void put_16(uint16_t n);
void put_32_hex(uint32_t n); void put_32_hex(uint32_t n);
void move_cursor(uint8_t col, uint8_t row); void move_cursor(uint8_t col, uint8_t row);
void set_color(uint8_t c); void set_color(uint8_t c);