Commit 83b7c108 by malcolm

Style fixes

parent fca15660
...@@ -10,57 +10,51 @@ gdt_desc _gdt [MAX_GDT_DESC]; ...@@ -10,57 +10,51 @@ gdt_desc _gdt [MAX_GDT_DESC];
#define set_cs( cs ) asm volatile ( "ljmp %0, $1337f \n\t 1337: \n\t" :: "i"(cs) ) #define set_cs( cs ) asm volatile ( "ljmp %0, $1337f \n\t 1337: \n\t" :: "i"(cs) )
void gdt_install(){ void gdt_install(){
struct{ struct{
uint16_t limit; uint16_t limit;
uint32_t base; uint32_t base;
}__attribute__((packed)) gdtr; }__attribute__((packed)) gdtr;
gdtr.limit = gdt_limit; gdtr.limit = gdt_limit;
gdtr.base = gdt_base; gdtr.base = gdt_base;
asm volatile ("lgdt (%0)": :"r"(&gdtr)); asm volatile ("lgdt (%0)": :"r"(&gdtr));
//serial_writestring("gdt installed, now setting cs\n"); set_cs(8);
set_cs(8); asm volatile("mov $0x10, %ax;\
//serial_writestring("cs set, setting ds\n"); mov %ax, %ds; \
//asm volatile ("ljmp $0x8,$1f; mov %ax, %es; \
// 1: mov %ax, %fs; \
asm volatile(" mov $0x10, %ax;\ mov %ax, %gs; \
mov %ax, %ds;\
mov %ax, %es;\
mov %ax, %fs;\
mov %ax, %gs;\
mov %ax, %ss;"); mov %ax, %ss;");
//serial_writestring("ds set, we done here. moving on to idt\n"); strlen("");
//terminal_writestring("");
strlen("");
} }
void gdt_set_desc(uint32_t i, uint64_t base, uint64_t limit, uint8_t access, uint8_t gran){ void gdt_set_desc(uint32_t i, uint64_t base, uint64_t limit, uint8_t access, uint8_t gran){
if (i > MAX_GDT_DESC) if (i > MAX_GDT_DESC)
return; return;
memset ((void*)&_gdt[i], 0, sizeof (gdt_desc)); memset ((void*)&_gdt[i], 0, sizeof (gdt_desc));
_gdt[i].low_base = (uint16_t)(base & 0xffff); _gdt[i].low_base = (uint16_t)(base & 0xffff);
_gdt[i].mid_base = (uint8_t)((base >> 16) & 0xff); _gdt[i].mid_base = (uint8_t)((base >> 16) & 0xff);
_gdt[i].high_base = (uint8_t)((base >> 24) & 0xff); _gdt[i].high_base = (uint8_t)((base >> 24) & 0xff);
_gdt[i].lim = (uint16_t)(limit & 0xffff); _gdt[i].lim = (uint16_t)(limit & 0xffff);
_gdt[i].flags = access; _gdt[i].flags = access;
_gdt[i].gran = (uint8_t)((limit >> 16) & 0x0f); _gdt[i].gran = (uint8_t)((limit >> 16) & 0x0f);
_gdt[i].gran |= gran & 0xf0; _gdt[i].gran |= gran & 0xf0;
} }
gdt_desc* gdt_get_desc (int i) { gdt_desc* gdt_get_desc (int i) {
if (i > MAX_GDT_DESC) if (i > MAX_GDT_DESC)
return 0; return 0;
return &_gdt[i]; return &_gdt[i];
} }
int gdt_init() { int gdt_init() {
gdt_limit = (sizeof (gdt_desc) * MAX_GDT_DESC)-1; gdt_limit = (sizeof (gdt_desc) * MAX_GDT_DESC)-1;
gdt_base = (uint32_t)&_gdt[0]; gdt_base = (uint32_t)&_gdt[0];
//null //null
gdt_set_desc(0, 0, 0, 0, 0); gdt_set_desc(0, 0, 0, 0, 0);
//code //code
gdt_set_desc(1,0,0xffffffff, GDT_RW|GDT_EXE|GDT_CDT|GDT_MEM, GDT_GR_PG|GDT_GR_32|GDT_GR_HI); gdt_set_desc(1,0,0xffffffff, GDT_RW|GDT_EXE|GDT_CDT|GDT_MEM, GDT_GR_PG|GDT_GR_32|GDT_GR_HI);
//data //data
gdt_set_desc(2,0,0xffffffff, GDT_RW|GDT_CDT|GDT_MEM, GDT_GR_PG|GDT_GR_32|GDT_GR_HI); gdt_set_desc(2,0,0xffffffff, GDT_RW|GDT_CDT|GDT_MEM, GDT_GR_PG|GDT_GR_32|GDT_GR_HI);
//serial_writestring("gdt filled, now installing\n"); //serial_writestring("gdt filled, now installing\n");
gdt_install(); gdt_install();
return 0; return 0;
} }
...@@ -18,12 +18,12 @@ ...@@ -18,12 +18,12 @@
#define GDT_GR_PG 0x80 #define GDT_GR_PG 0x80
typedef struct{ typedef struct{
uint16_t lim; uint16_t lim;
uint16_t low_base; uint16_t low_base;
uint8_t mid_base; uint8_t mid_base;
uint8_t flags; uint8_t flags;
uint8_t gran; uint8_t gran;
uint8_t high_base; uint8_t high_base;
} __attribute__((packed)) gdt_desc; } __attribute__((packed)) gdt_desc;
extern void gdt_set_desc(uint32_t i, uint64_t base, uint64_t limit, uint8_t access, uint8_t gran); extern void gdt_set_desc(uint32_t i, uint64_t base, uint64_t limit, uint8_t access, uint8_t gran);
......
...@@ -8,61 +8,51 @@ uint32_t idt_base; ...@@ -8,61 +8,51 @@ uint32_t idt_base;
idt_desc _idt [MAX_IDT_INT]; idt_desc _idt [MAX_IDT_INT];
void idt_install() { void idt_install() {
struct{ struct{
uint16_t limit; uint16_t limit;
uint32_t base; uint32_t base;
} __attribute__((packed)) idtr; } __attribute__((packed)) idtr;
//char s[1]; idtr.limit = idt_limit;
//itoa(sizeof(idtr),s); idtr.base = idt_base;
//serial_writestring(s); char f[20];
idtr.limit = idt_limit; asm volatile ("lidt (%0)": :"r"(&idtr));
idtr.base = idt_base;
char f[20];
itoa(idtr.limit,f);
//serial_writestring(f);
//serial_writestring("\n");
itoa(idtr.base,f);
//serial_writestring(f);
//serial_writestring("\n");
asm volatile ("lidt (%0)": :"r"(&idtr));
} }
static void default_handler () { static void default_handler () {
//DEBUG OUTPUT MAYBE? //DEBUG OUTPUT MAYBE?
serial_writestring("int caught\n"); serial_writestring("int caught\n");
//for(;;); return;
return;
} }
idt_desc* get_ir (uint32_t i) { idt_desc* get_ir (uint32_t i) {
if (i>MAX_IDT_INT) if (i>MAX_IDT_INT)
return 0; return 0;
return &_idt[i]; return &_idt[i];
} }
int install_ir (uint32_t i, uint16_t flags, uint16_t code_sel, IRQ_HANDLER irq) { int install_ir (uint32_t i, uint16_t flags, uint16_t code_sel, IRQ_HANDLER irq) {
if (i>MAX_IDT_INT) if (i>MAX_IDT_INT)
return 0; return 0;
if (!irq) if (!irq)
return 0;
uint64_t uiBase = (uintptr_t)(&(*irq));
_idt[i].low_base = (uint16_t)(uiBase & 0xffff);
_idt[i].high_base = (uint16_t)((uiBase >> 16) & 0xffff);
_idt[i].reserved = 0;
_idt[i].flags = (uint8_t)(flags);
_idt[i].code_sel = code_sel;
return 0; return 0;
uint64_t uiBase = (uintptr_t)(&(*irq));
_idt[i].low_base = (uint16_t)(uiBase & 0xffff);
_idt[i].high_base = (uint16_t)((uiBase >> 16) & 0xffff);
_idt[i].reserved = 0;
_idt[i].flags = (uint8_t)(flags);
_idt[i].code_sel = code_sel;
return 0;
} }
int idt_init (uint16_t code_sel) { int idt_init (uint16_t code_sel) {
idt_limit = sizeof (idt_desc) * MAX_IDT_INT -1; idt_limit = sizeof (idt_desc) * MAX_IDT_INT -1;
idt_base = (uint32_t)&_idt[0]; idt_base = (uint32_t)&_idt[0];
memset ((void*)&_idt[0], 0, sizeof (idt_desc) * MAX_IDT_INT-1); memset ((void*)&_idt[0], 0, sizeof (idt_desc) * MAX_IDT_INT-1);
for (int i=0; i<MAX_IDT_INT; i++) for (int i=0; i<MAX_IDT_INT; i++)
install_ir (i, IDT_PR|IDT_32,code_sel,(IRQ_HANDLER)default_handler); install_ir (i, IDT_PR|IDT_32,code_sel,(IRQ_HANDLER)default_handler);
idt_install(); idt_install();
char f[20]; char f[20];
itoa((uintptr_t)&_idt,f); itoa((uintptr_t)&_idt,f);
//serial_writestring(f); //serial_writestring(f);
return 0; return 0;
} }
...@@ -15,11 +15,11 @@ ...@@ -15,11 +15,11 @@
typedef void (*IRQ_HANDLER)(void); typedef void (*IRQ_HANDLER)(void);
typedef struct { typedef struct {
uint16_t low_base; uint16_t low_base;
uint16_t code_sel; uint16_t code_sel;
uint8_t reserved; uint8_t reserved;
uint8_t flags; uint8_t flags;
uint16_t high_base; uint16_t high_base;
}__attribute__((packed)) idt_desc; }__attribute__((packed)) idt_desc;
extern idt_desc* get_ir (uint32_t i); extern idt_desc* get_ir (uint32_t i);
......
...@@ -13,14 +13,10 @@ ...@@ -13,14 +13,10 @@
extern "C" /* Use C linkage for kernel_main. */ extern "C" /* Use C linkage for kernel_main. */
#endif #endif
void kernel_main() { void kernel_main() {
/* Initialize terminal and serial interfaces */ /* Initialize terminal and serial interfaces */
terminal_initialize(); terminal_initialize();
serial_init(); serial_init();
char b[2];
int a = strlen("");
itoa(a,b);
terminal_writestring(b);
gdt_init(); gdt_init();
idt_init(0x8); idt_init(0x8);
......
#include "string.h" #include "string.h"
void *memset(void *s, int c, size_t n){ void *memset(void *s, int c, size_t n){
unsigned char* p=s; unsigned char* p=s;
while(n--) while(n--)
*p++=(unsigned char) c; *p++=(unsigned char) c;
return s; return s;
} }
void itoa(int n, char s[]) void itoa(int n, char s[])
{ {
int i, sign; int i, sign;
if ((sign = n) < 0) /* record sign */ if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */ n = -n; /* make n positive */
i = 0; i = 0;
...@@ -24,8 +24,8 @@ void itoa(int n, char s[]) ...@@ -24,8 +24,8 @@ void itoa(int n, char s[])
} }
size_t strlen(const char* str) { size_t strlen(const char* str) {
size_t ret = 0; size_t ret = 0;
while ( str[ret] != 0 ) while ( str[ret] != 0 )
ret++; ret++;
return ret; return ret;
} }
...@@ -7,13 +7,13 @@ ...@@ -7,13 +7,13 @@
#include "term.h" #include "term.h"
uint8_t make_color(enum vga_color fg, enum vga_color bg) { uint8_t make_color(enum vga_color fg, enum vga_color bg) {
return fg | bg << 4; return fg | bg << 4;
} }
uint16_t make_vgaentry(char c, uint8_t color) { uint16_t make_vgaentry(char c, uint8_t color) {
uint16_t c16 = c; uint16_t c16 = c;
uint16_t color16 = color; uint16_t color16 = color;
return c16 | color16 << 8; return c16 | color16 << 8;
} }
static const size_t VGA_WIDTH = 80; static const size_t VGA_WIDTH = 80;
...@@ -25,39 +25,39 @@ uint8_t terminal_color; ...@@ -25,39 +25,39 @@ uint8_t terminal_color;
uint16_t* terminal_buffer; uint16_t* terminal_buffer;
void terminal_initialize() { void terminal_initialize() {
terminal_row = 0; terminal_row = 0;
terminal_column = 0; terminal_column = 0;
terminal_color = make_color(COLOR_LIGHT_GREY, COLOR_BLACK); terminal_color = make_color(COLOR_LIGHT_GREY, COLOR_BLACK);
terminal_buffer = (uint16_t*) 0xB8000; terminal_buffer = (uint16_t*) 0xB8000;
for (size_t y = 0; y < VGA_HEIGHT; y++) { for (size_t y = 0; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) { for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = y * VGA_WIDTH + x; const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = make_vgaentry(' ', terminal_color); terminal_buffer[index] = make_vgaentry(' ', terminal_color);
}
} }
}
} }
void terminal_setcolor(uint8_t color) { void terminal_setcolor(uint8_t color) {
terminal_color = color; terminal_color = color;
} }
void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) { void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) {
const size_t index = y * VGA_WIDTH + x; const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = make_vgaentry(c, color); terminal_buffer[index] = make_vgaentry(c, color);
} }
void terminal_putchar(char c) { void terminal_putchar(char c) {
terminal_putentryat(c, terminal_color, terminal_column, terminal_row); terminal_putentryat(c, terminal_color, terminal_column, terminal_row);
if (++terminal_column == VGA_WIDTH) { if (++terminal_column == VGA_WIDTH) {
terminal_column = 0; terminal_column = 0;
if (++terminal_row == VGA_HEIGHT) { if (++terminal_row == VGA_HEIGHT) {
terminal_row = 0; terminal_row = 0;
}
} }
}
} }
void terminal_writestring(const char* data) { void terminal_writestring(const char* data) {
size_t datalen = strlen(data); size_t datalen = strlen(data);
for (size_t i = 0; i < datalen; i++) for (size_t i = 0; i < datalen; i++)
terminal_putchar(data[i]); terminal_putchar(data[i]);
} }
...@@ -9,22 +9,22 @@ ...@@ -9,22 +9,22 @@
#include "string.h" #include "string.h"
/* Hardware text mode color constants. */ /* Hardware text mode color constants. */
enum vga_color { enum vga_color {
COLOR_BLACK = 0, COLOR_BLACK = 0,
COLOR_BLUE = 1, COLOR_BLUE = 1,
COLOR_GREEN = 2, COLOR_GREEN = 2,
COLOR_CYAN = 3, COLOR_CYAN = 3,
COLOR_RED = 4, COLOR_RED = 4,
COLOR_MAGENTA = 5, COLOR_MAGENTA = 5,
COLOR_BROWN = 6, COLOR_BROWN = 6,
COLOR_LIGHT_GREY = 7, COLOR_LIGHT_GREY = 7,
COLOR_DARK_GREY = 8, COLOR_DARK_GREY = 8,
COLOR_LIGHT_BLUE = 9, COLOR_LIGHT_BLUE = 9,
COLOR_LIGHT_GREEN = 10, COLOR_LIGHT_GREEN = 10,
COLOR_LIGHT_CYAN = 11, COLOR_LIGHT_CYAN = 11,
COLOR_LIGHT_RED = 12, COLOR_LIGHT_RED = 12,
COLOR_LIGHT_MAGENTA = 13, COLOR_LIGHT_MAGENTA = 13,
COLOR_LIGHT_BROWN = 14, COLOR_LIGHT_BROWN = 14,
COLOR_WHITE = 15, COLOR_WHITE = 15,
}; };
uint8_t make_color(enum vga_color fg, enum vga_color bg); uint8_t make_color(enum vga_color fg, enum vga_color bg);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment