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

@ -2,7 +2,7 @@
#if defined(__linux__)
#error "You are not using a cross-compiler, you will most certainly run into trouble"
#endif
/* This tutorial will only work for the 32-bit ix86 targets. */
#if !defined(__i386__)
#error "This kernel needs to be compiled with a ix86-elf compiler"
@ -16,76 +16,59 @@
#include "memory.c"
#include "interrupts.c"
#include "shell.c"
#include "util/printer.c"
static inline bool are_interrupts_enabled() {
unsigned long flags;
asm volatile ( "pushf\n\t"
"pop %0"
: "=g"(flags) );
: "=g"(flags));
return flags & (1 << 9);
}
void kernel_main(void)
{
/* Initialize terminal interface */
terminal_initialize();
void kernel_main(void) {
/* Initialize terminal interface */
terminal_initialize();
terminal_putchar('H');
terminal_putchar('e');
terminal_putchar('l');
terminal_putchar('l');
terminal_putchar('o');
terminal_putchar('H');
terminal_putchar('e');
terminal_putchar('l');
terminal_putchar('l');
terminal_putchar('o');
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_writestring(" World!\n");
terminal_writestring("Newlines!\n");
print(" World!\n");
print("Newlines!\n");
char* memory_str = alloc(sizeof(char) * 7);
for (int i = 0; i < 6; i++) {
memory_str[i] = "Memory"[i];
}
memory_str[6] = 0;
char *memory_str = alloc(sizeof(char) * 7);
for (int i = 0; i < 6; i++) {
memory_str[i] = "Memory"[i];
}
memory_str[6] = 0;
char* management_str = alloc(sizeof(char) * 13);
for (int i = 0; i < 13; i++) {
management_str[i] = " management!\n"[i];
}
management_str[13] = 0;
char *management_str = alloc(sizeof(char) * 13);
for (int i = 0; i < 13; i++) {
management_str[i] = " management!\n"[i];
}
management_str[13] = 0;
print(memory_str);
print(management_str);
terminal_writestring(memory_str);
terminal_writestring(management_str);
terminal_writestring("Mem after freeing!\n");
print("Mem after freeing\n");
free(memory_str);
print_memory();
free(management_str);
free(management_str);
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];
print((are_interrupts_enabled()) ? "Interrupts!\n" : "No interrupts :(\n");
interrupt_init();
for (;;) {
shell_step();
}
memory_after_free[13] = 0;
terminal_writestring(memory_after_free);
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();
for(;;) {
shell_step();
}
}

View File

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