Add print function, add more memory prints at startup

This commit is contained in:
Maxime Bloch 2020-01-28 22:25:37 +01:00
parent 4f214b2d5a
commit a042169845
No known key found for this signature in database
GPG key ID: CE32A7D95B7D6418
2 changed files with 63 additions and 91 deletions

View file

@ -16,6 +16,7 @@
#include "memory.c" #include "memory.c"
#include "interrupts.c" #include "interrupts.c"
#include "shell.c" #include "shell.c"
#include "util/printer.c"
static inline bool are_interrupts_enabled() { static inline bool are_interrupts_enabled() {
unsigned long flags; unsigned long flags;
@ -25,8 +26,7 @@ static inline bool are_interrupts_enabled() {
return flags & (1 << 9); return flags & (1 << 9);
} }
void kernel_main(void) void kernel_main(void) {
{
/* Initialize terminal interface */ /* Initialize terminal interface */
terminal_initialize(); terminal_initialize();
@ -37,10 +37,10 @@ void kernel_main(void)
terminal_putchar('o'); terminal_putchar('o');
terminal_setcolor(vga_entry_color(VGA_COLOR_GREEN, VGA_COLOR_BLACK)); terminal_setcolor(vga_entry_color(VGA_COLOR_GREEN, VGA_COLOR_BLACK));
terminal_writestring(" kernel"); print(" kernel");
terminal_setcolor(vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK)); terminal_setcolor(vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK));
terminal_writestring(" World!\n"); print(" World!\n");
terminal_writestring("Newlines!\n"); print("Newlines!\n");
char *memory_str = alloc(sizeof(char) * 7); char *memory_str = alloc(sizeof(char) * 7);
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
@ -54,34 +54,17 @@ void kernel_main(void)
} }
management_str[13] = 0; management_str[13] = 0;
print(memory_str);
print(management_str);
terminal_writestring(memory_str); print("Mem after freeing\n");
terminal_writestring(management_str);
terminal_writestring("Mem after freeing!\n");
free(memory_str); free(memory_str);
print_memory(); print_memory();
free(management_str); free(management_str);
print_memory(); print_memory();
char* memory_after_free = alloc(sizeof(char) * 12);
for (int i = 0; i < 13; i++) {
memory_after_free[i] = " Some text\n"[i];
}
memory_after_free[13] = 0;
terminal_writestring(memory_after_free); print((are_interrupts_enabled()) ? "Interrupts!\n" : "No interrupts :(\n");
terminal_writestring("Memory after new allocation!\n");
alloc(1000);
print_memory();
terminal_writestring("Free again\n");
free(memory_after_free);
print_memory();
terminal_writestring((are_interrupts_enabled())? "Interrupts!\n": "No interrupts :(\n");
interrupt_init(); interrupt_init();

View file

@ -5,6 +5,7 @@
#include <stddef.h> #include <stddef.h>
#include "terminal.c" #include "terminal.c"
#include "util/printer.c"
#define MEMORY_START 0x200000 #define MEMORY_START 0x200000
//#define MEMORY_END 0x300000 //#define MEMORY_END 0x300000
@ -71,45 +72,33 @@ void print_memory() {
page_tag *curr_page = start; page_tag *curr_page = start;
int i = 0; int i = 0;
while (curr_page != NULL) { while (curr_page != NULL) {
terminal_writeint(i, 10); print("%d: [%x (%d)] [%x (%d)]\n",
terminal_write(": [", 3); i,
terminal_writeint((int) curr_page, 16); curr_page, sizeof(page_tag),
terminal_write(" ", 1); (curr_page + 1), curr_page->size);
terminal_writeint(sizeof(page_tag), 10); //
terminal_write("] [", 3); // void *empty_start = (void *) curr_page + sizeof(page_tag) + curr_page->size;
terminal_writeint((int) (curr_page + 1), 16); // if (empty_start + sizeof(page_tag) < (void *) curr_page->next) {
terminal_write(" ", 1); // terminal_writestring("_: empty_page (");
terminal_writeint(curr_page->size, 10); // terminal_writeint(sizeof(page_tag), 10);
terminal_writestring("]\n"); // terminal_writestring(", ");
// terminal_writeint(
// printf("%d: [%p (%zu)] [%p (%zu)]\n",
// i,
// curr_page, sizeof(page_tag),
// curr_page + 1, curr_page->size);
void *empty_start = (void *) curr_page + sizeof(page_tag) + curr_page->size;
if (empty_start + sizeof(page_tag) < (void *) curr_page->next) {
terminal_writestring("empty_page (");
terminal_writeint(sizeof(page_tag), 10);
terminal_writestring(", ");
terminal_writeint(
(void *) curr_page->next -
(empty_start + sizeof(page_tag)), 10);
terminal_writestring(")\n");
// printf("empty_page (%ld, %ld)\n",
// sizeof(page_tag),
// (void *) curr_page->next - // (void *) curr_page->next -
// (empty_start + sizeof(page_tag))); // (empty_start + sizeof(page_tag)), 10);
} else if (empty_start < (void *) curr_page->next) { // terminal_writestring(")\n");
terminal_writestring("not enough room ("); //// printf("empty_page (%ld, %ld)\n",
terminal_writeint((void *) curr_page->next - empty_start, 10); //// sizeof(page_tag),
terminal_writestring(")\n"); //// (void *) curr_page->next -
// printf("not enough room (%ld)\n", //// (empty_start + sizeof(page_tag)));
// (void *) curr_page->next - empty_start); // } else if (empty_start < (void *) curr_page->next) {
} // terminal_writestring("_: not enough room (");
// terminal_writeint((void *) curr_page->next - empty_start, 10);
// terminal_writestring(")\n");
//// printf("not enough room (%ld)\n",
//// (void *) curr_page->next - empty_start);
// }
curr_page = curr_page->next; curr_page = curr_page->next;
i++; i += 1;
} }
terminal_write("------------------\n\n", 20); terminal_write("------------------\n\n", 20);
} }