Refactor the kernel files for the new terminal implementation

This commit is contained in:
Jan-Pieter Baert 2020-01-28 02:16:53 +01:00
parent dea250ee13
commit f9dcb7a5b7
No known key found for this signature in database
GPG key ID: B19186932178234A
3 changed files with 130 additions and 114 deletions

View file

@ -21,60 +21,65 @@ __attribute__((interrupt)) void NAME (interrupt_frame* frame, uint32_t err_code)
bsod(frame, STR, err_code); \
}
terminal_state state;
void exception_set_terminal_state(terminal_state* s){
terminal_state_copy(s,&state);
}
void bsod(interrupt_frame* frame, char* err_msg, int32_t err_code) {
terminal_initialize();
terminal_writestring("An exception occured: ");
terminal_writestring(err_msg);
terminal_write_str(&state,"An exception occured: ");
terminal_write_str(&state,err_msg);
if(err_code != -1) {
terminal_writestring(" (error code = 0x");
terminal_writeint(err_code, 16);
terminal_writestring(")");
terminal_write_str(&state," (error code = 0x");
terminal_write_int(&state,err_code, 16);
terminal_write_str(&state,")");
}
terminal_writestring("\nHere's what we know:\n");
terminal_writestring("eip = 0x");
terminal_writeint(frame->eip, 16);
terminal_writestring("\ncs = 0x");
terminal_writeint(frame->cs, 16);
terminal_writestring("\neflags = 0b");
terminal_writeint(frame->eflags, 2);
terminal_writestring("\n(");
terminal_write_str(&state,"\nHere's what we know:\n");
terminal_write_str(&state,"eip = 0x");
terminal_write_int(&state,frame->eip, 16);
terminal_write_str(&state,"\ncs = 0x");
terminal_write_int(&state,frame->cs, 16);
terminal_write_str(&state,"\neflags = 0b");
terminal_write_int(&state,frame->eflags, 2);
terminal_write_str(&state,"\n(");
terminal_writestring((0x0001 & frame->eflags) != 0 ? "NC,": "CY,");
terminal_writestring((0x0004 & frame->eflags) != 0 ? "PE,": "PO,");
terminal_writestring((0x0010 & frame->eflags) != 0 ? "AC,": "NA,");
terminal_writestring((0x0040 & frame->eflags) != 0 ? "ZR,": "NZ,");
terminal_writestring((0x0080 & frame->eflags) != 0 ? "NG,": "PL,");
terminal_writestring("TF:");
terminal_writeint((0x0100 & frame->eflags) >> 8, 2);
terminal_putchar(',');
terminal_writestring((0x0200 & frame->eflags) != 0 ? "EI,": "DI,");
terminal_writestring((0x0400 & frame->eflags) != 0 ? "DN,": "UP,");
terminal_writestring((0x0800 & frame->eflags) != 0 ? "OV,": "NV,");
terminal_writestring("IOPL:");
terminal_writeint((0x3000 & frame->eflags) >> 12, 10);
terminal_putchar(',');
terminal_writestring("NT:");
terminal_writeint((0x4000 & frame->eflags) >> 14, 2);
terminal_putchar(',');
terminal_writestring("RF:");
terminal_writeint((0x0001000 & frame->eflags) >> 16, 2);
terminal_putchar(',');
terminal_writestring("VM:");
terminal_writeint((0x0002000 & frame->eflags) >> 17, 2);
terminal_putchar(',');
terminal_writestring("AC:");
terminal_writeint((0x0004000 & frame->eflags) >> 18, 2);
terminal_putchar(',');
terminal_writestring("VIF:");
terminal_writeint((0x0008000 & frame->eflags) >> 19, 2);
terminal_putchar(',');
terminal_writestring("VIP:");
terminal_writeint((0x0010000 & frame->eflags) >> 20, 2);
terminal_putchar(',');
terminal_writestring("ID:");
terminal_writeint((0x0020000 & frame->eflags) >> 21, 2);
terminal_putchar(')');
terminal_write_str(&state,(0x0001 & frame->eflags) != 0 ? "NC,": "CY,");
terminal_write_str(&state,(0x0004 & frame->eflags) != 0 ? "PE,": "PO,");
terminal_write_str(&state,(0x0010 & frame->eflags) != 0 ? "AC,": "NA,");
terminal_write_str(&state,(0x0040 & frame->eflags) != 0 ? "ZR,": "NZ,");
terminal_write_str(&state,(0x0080 & frame->eflags) != 0 ? "NG,": "PL,");
terminal_write_str(&state,"TF:");
terminal_write_int(&state,(0x0100 & frame->eflags) >> 8, 2);
terminal_write_char(&state,',');
terminal_write_str(&state,(0x0200 & frame->eflags) != 0 ? "EI,": "DI,");
terminal_write_str(&state,(0x0400 & frame->eflags) != 0 ? "DN,": "UP,");
terminal_write_str(&state,(0x0800 & frame->eflags) != 0 ? "OV,": "NV,");
terminal_write_str(&state,"IOPL:");
terminal_write_int(&state,(0x3000 & frame->eflags) >> 12, 10);
terminal_write_char(&state,',');
terminal_write_str(&state,"NT:");
terminal_write_int(&state,(0x4000 & frame->eflags) >> 14, 2);
terminal_write_char(&state,',');
terminal_write_str(&state,"RF:");
terminal_write_int(&state,(0x0001000 & frame->eflags) >> 16, 2);
terminal_write_char(&state,',');
terminal_write_str(&state,"VM:");
terminal_write_int(&state,(0x0002000 & frame->eflags) >> 17, 2);
terminal_write_char(&state,',');
terminal_write_str(&state,"AC:");
terminal_write_int(&state,(0x0004000 & frame->eflags) >> 18, 2);
terminal_write_char(&state,',');
terminal_write_str(&state,"VIF:");
terminal_write_int(&state,(0x0008000 & frame->eflags) >> 19, 2);
terminal_write_char(&state,',');
terminal_write_str(&state,"VIP:");
terminal_write_int(&state,(0x0010000 & frame->eflags) >> 20, 2);
terminal_write_char(&state,',');
terminal_write_str(&state,"ID:");
terminal_write_int(&state,(0x0020000 & frame->eflags) >> 21, 2);
terminal_write_char(&state,')');
for(;;) {
asm("hlt");

View file

@ -27,20 +27,25 @@ static inline bool are_interrupts_enabled() {
void kernel_main(void)
{
/* Initialize terminal interface */
terminal_initialize();
terminal_state state;
terminal_initialize_state(&state);
terminal_putchar('H');
terminal_putchar('e');
terminal_putchar('l');
terminal_putchar('l');
terminal_putchar('o');
terminal_set_color(&state, VGA_COLOR_BLACK, VGA_COLOR_RED);
terminal_write_char(&state, 'H');
terminal_set_color(&state, VGA_COLOR_BLACK, VGA_COLOR_GREEN);
terminal_write_char(&state, 'e');
terminal_set_color(&state, VGA_COLOR_BLACK, VGA_COLOR_BLUE);
terminal_write_char(&state, 'l');
terminal_set_color(&state, VGA_COLOR_BLACK, VGA_COLOR_MAGENTA);
terminal_write_char(&state, 'l');
terminal_set_color(&state, VGA_COLOR_BLACK, VGA_COLOR_WHITE);
terminal_write_char(&state, 'o');
terminal_setcolor(vga_entry_color(VGA_COLOR_GREEN, VGA_COLOR_BLACK));
terminal_writestring(" kernel");
terminal_setcolor(vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK));
terminal_writestring(" World!\n");
terminal_writestring("Newlines!\n");
terminal_set_color(&state, VGA_COLOR_GREEN, VGA_COLOR_WHITE);
terminal_write_str(&state," kernel");
terminal_set_color(&state, VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);
terminal_write_str(&state," World!\n");
terminal_write_str(&state,"Newlines!\n");
char* memory_str = alloc(sizeof(char) * 7);
for (int i = 0; i < 6; i++) {
@ -54,11 +59,13 @@ void kernel_main(void)
}
management_str[13] = 0;
terminal_writestring(memory_str);
terminal_writestring(management_str);
/* terminal_write_str(&state,memory_str); */
/* terminal_write_str(&state,management_str); */
terminal_writestring((are_interrupts_enabled())? "Interrupts!\n": "No interrupts :(\n");
terminal_write_str(&state, (are_interrupts_enabled())? "Interrupts!\n": "No interrupts :(\n");
shell_set_terminal_state(&state);
exception_set_terminal_state(&state);
interrupt_init();
for(;;) {

View file

@ -11,31 +11,36 @@
char buffer[SHELL_CMD_BUFFER_SIZE];
int buffer_idx = 0;
terminal_state state;
void shell_set_terminal_state(terminal_state* s){
terminal_state_copy(s,&state);
}
int echo(char* input) {
terminal_writestring(input);
terminal_putchar('\n');
terminal_write_str(&state,input);
terminal_write_char(&state,'\n');
return 0;
}
int hello(char* unused) {
terminal_writestring("Hello, world!\n");
terminal_write_str(&state,"Hello, world!\n");
return 0;
}
int cls(char* unused) {
terminal_initialize();
terminal_clear_state(&state);
return 0;
}
int get_gdt(char* unused) {
gdt_desc desc = {2,2};
sgdt(&desc);
terminal_writestring("limit = ");
terminal_writeint(desc.limit, 10);
terminal_writestring("\nbase = 0x");
terminal_writeint(desc.base, 16);
terminal_putchar('\n');
terminal_write_str(&state,"limit = ");
terminal_write_int(&state,desc.limit, 10);
terminal_write_str(&state,"\nbase = 0x");
terminal_write_int(&state,desc.base, 16);
terminal_write_char(&state,'\n');
gdt_entry* entries = (gdt_entry*) desc.base;
int num_entries = (desc.limit+1) / 8;
@ -46,74 +51,73 @@ int get_gdt(char* unused) {
uint8_t flags = (entry.flags_limit_higher >> 4);
bool is_data = ((entry.access_byte & 0b00001000) >> 3) == 0;
//terminal_writestring("\nEntry ");
//terminal_writeint(entry_num, 10);
terminal_writestring("base = 0x");
terminal_writeint(base, 16);
terminal_writestring("\nlimit = 0x");
terminal_writeint(limit, 16);
terminal_writestring("\nflags = 0b");
terminal_writeint((entry.flags_limit_higher >> 4), 2);
//terminal_write_str(&state,"\nEntry ");
//terminal_write_int(&state,entry_num, 10);
terminal_write_str(&state,"base = 0x");
terminal_write_int(&state,base, 16);
terminal_write_str(&state,"\nlimit = 0x");
terminal_write_int(&state,limit, 16);
terminal_write_str(&state,"\nflags = 0b");
terminal_write_int(&state,(entry.flags_limit_higher >> 4), 2);
if ((flags & 0b1000) == 0) {
terminal_writestring(" (byte granularity");
terminal_write_str(&state," (byte granularity");
} else {
terminal_writestring(" (page granularity");
terminal_write_str(&state," (page granularity");
}
if ((flags & 0b0100) == 0) {
terminal_writestring(", 16 bit)");
terminal_write_str(&state,", 16 bit)");
} else {
terminal_writestring(", 32 bit)");
terminal_write_str(&state,", 32 bit)");
}
terminal_writestring("\naccess = 0b");
terminal_writeint(entry.access_byte, 2);
terminal_writestring(" (ring ");
terminal_writeint((entry.access_byte & 0b01100000) >> 5, 10);
terminal_write_str(&state,"\naccess = 0b");
terminal_write_int(&state,entry.access_byte, 2);
terminal_write_str(&state," (ring ");
terminal_write_int(&state,(entry.access_byte & 0b01100000) >> 5, 10);
if ((entry.access_byte & 0b00010000) == 0) {
terminal_writestring(", System");
terminal_write_str(&state,", System");
}
if (is_data) {
terminal_writestring(", Data");
terminal_write_str(&state,", Data");
if((entry.access_byte & 0b00000100) == 0) {
terminal_writestring(" (growing up, ");
terminal_write_str(&state," (growing up, ");
} else {
terminal_writestring(" (growing down, ");
terminal_write_str(&state," (growing down, ");
}
if((entry.access_byte & 0b00000010) == 0) {
terminal_writestring("r--)");
terminal_write_str(&state,"r--)");
} else {
terminal_writestring("rw-)");
terminal_write_str(&state,"rw-)");
}
} else {
terminal_writestring(", Code");
terminal_write_str(&state,", Code");
if((entry.access_byte & 0b00000100) == 0) {
terminal_writestring(" (non-conforming, ");
terminal_write_str(&state," (non-conforming, ");
} else {
terminal_writestring(" (conforming, ");
terminal_write_str(&state," (conforming, ");
}
if((entry.access_byte & 0b00000010) == 0) {
terminal_writestring("--x)");
terminal_write_str(&state,"--x)");
} else {
terminal_writestring("r-x)");
terminal_write_str(&state,"r-x)");
}
}
terminal_writestring(")\n");
terminal_write_str(&state,")\n");
}
return 0;
}
int ree(char* unused) {
terminal_initialize();
terminal_putchar('R');
terminal_write_char(&state,'R');
for (int i = 1; i < VGA_WIDTH * VGA_HEIGHT; i++) {
terminal_putchar('e');
terminal_write_char(&state,'e');
}
return 0;
}
@ -167,7 +171,7 @@ void shell_step() {
char curr_char = getchar();
if (curr_char == '\n') {
terminal_putchar(curr_char);
terminal_write_char(&state,curr_char);
buffer[buffer_idx] = 0;
int result = run_command(buffer);
for (int i = 0; i < SHELL_CMD_BUFFER_SIZE; i++) {
@ -176,18 +180,18 @@ void shell_step() {
buffer_idx = 0;
if (result == -1) {
terminal_writestring("No such command\n");
terminal_write_str(&state,"No such command\n");
}
} else if (curr_char == 0x08) {
if (buffer_idx != 0) {
buffer_idx--;
buffer[buffer_idx] = 0;
terminal_putchar(curr_char);
terminal_write_char(&state,curr_char);
}
} else {
buffer[buffer_idx] = curr_char;
buffer_idx++;
terminal_putchar(curr_char);
terminal_write_char(&state,curr_char);
}
}