Commit 51ad7510 by malcolm

Style applied to existing files

parent cac64f4b
...@@ -7,12 +7,14 @@ ...@@ -7,12 +7,14 @@
// Wrapper for outb // Wrapper for outb
// Stolen from http://wiki.osdev.org/Inline_Assembly/Examples // Stolen from http://wiki.osdev.org/Inline_Assembly/Examples
inline void outb(uint16_t port, uint8_t val) { inline void outb(uint16_t port, uint8_t val)
{
asm volatile ("outb %0, %1" : : "a"(val), "Nd"(port)); asm volatile ("outb %0, %1" : : "a"(val), "Nd"(port));
} }
// Wrapper for inb // Wrapper for inb
inline uint8_t inb(uint16_t port) { inline uint8_t inb(uint16_t port)
{
uint8_t ret; uint8_t ret;
asm volatile ("inb %1, %0" : "=a"(ret) : "Nd"(port)); asm volatile ("inb %1, %0" : "=a"(ret) : "Nd"(port));
return ret; return ret;
......
...@@ -10,50 +10,59 @@ uint32_t gdt_base; ...@@ -10,50 +10,59 @@ uint32_t gdt_base;
gdt_desc _gdt [MAX_GDT_DESC]; gdt_desc _gdt [MAX_GDT_DESC];
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));
asm volatile ( "ljmp %0, $1337f \n\t 1337: \n\t" :: "i"(8) ); asm volatile ( "ljmp %0, $1337f \n\t 1337: \n\t" :: "i" (8) );
asm volatile("mov $0x10, %ax; \ asm volatile ("mov $0x10, %ax; \
mov %ax, %ds; \ mov %ax, %ds; \
mov %ax, %es; \ mov %ax, %es; \
mov %ax, %fs; \ mov %ax, %fs; \
mov %ax, %gs; \ mov %ax, %gs; \
mov %ax, %ss;"); mov %ax, %ss;");
} }
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].mid_base = (uint8_t)((base >> 16) & 0xff); _gdt[i].low_base = (uint16_t)(base & 0xffff);
_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;
......
...@@ -17,7 +17,8 @@ ...@@ -17,7 +17,8 @@
#define GDT_GR_32 0x40 #define GDT_GR_32 0x40
#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;
......
...@@ -10,8 +10,10 @@ uint32_t idt_base; ...@@ -10,8 +10,10 @@ 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;
...@@ -20,38 +22,50 @@ void idt_install() { ...@@ -20,38 +22,50 @@ void idt_install() {
asm volatile ("lidt (%0)": :"r"(&idtr)); 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");
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; return 0;
uint64_t uiBase = (uintptr_t)(&(*irq)); }
uint64_t uiBase = (uintptr_t)(&(*irq));
_idt[i].low_base = (uint16_t)(uiBase & 0xffff); _idt[i].low_base = (uint16_t)(uiBase & 0xffff);
_idt[i].high_base = (uint16_t)((uiBase >> 16) & 0xffff); _idt[i].high_base = (uint16_t)((uiBase >> 16) & 0xffff);
_idt[i].reserved = 0; _idt[i].reserved = 0;
_idt[i].flags = (uint8_t)(flags); _idt[i].flags = (uint8_t)(flags);
_idt[i].code_sel = code_sel; _idt[i].code_sel = code_sel;
return 0; 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();
return 0; return 0;
} }
......
...@@ -14,7 +14,8 @@ ...@@ -14,7 +14,8 @@
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;
...@@ -22,8 +23,8 @@ typedef struct { ...@@ -22,8 +23,8 @@ typedef struct {
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);
extern int install_ir (uint32_t i, uint16_t flags, uint16_t code_sel, IRQ_HANDLER); extern int install_ir(uint32_t i, uint16_t flags, uint16_t code_sel, IRQ_HANDLER);
extern int idt_init (uint16_t code_sel); extern int idt_init(uint16_t code_sel);
#endif #endif
...@@ -12,7 +12,8 @@ ...@@ -12,7 +12,8 @@
#if defined(__cplusplus) #if defined(__cplusplus)
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();
......
...@@ -13,7 +13,8 @@ page_directory_entry page_directory[1024] __attribute__((aligned(4096))); ...@@ -13,7 +13,8 @@ page_directory_entry page_directory[1024] __attribute__((aligned(4096)));
// only page table in the page directory // only page table in the page directory
page_table_entry page_table_one[1024] __attribute__((aligned(4096))); page_table_entry page_table_one[1024] __attribute__((aligned(4096)));
page_directory_entry* initiate_directory() { page_directory_entry* initiate_directory()
{
// set each entry to not present // set each entry to not present
int i; int i;
for(i = 0; i < 1024; i++) for(i = 0; i < 1024; i++)
...@@ -30,7 +31,8 @@ page_directory_entry* initiate_directory() { ...@@ -30,7 +31,8 @@ page_directory_entry* initiate_directory() {
return page_directory; return page_directory;
} }
void initiate_page_table(page_directory_entry* page_dir) { void initiate_page_table(page_directory_entry* page_dir)
{
// holds the physical address where we want to start mapping these pages to. // holds the physical address where we want to start mapping these pages to.
// in this case, we want to map these pages to the very beginning of memory. // in this case, we want to map these pages to the very beginning of memory.
unsigned int i; unsigned int i;
......
...@@ -6,7 +6,8 @@ ...@@ -6,7 +6,8 @@
#define SERIAL_IO_PORT 0x3f8 // COM1 #define SERIAL_IO_PORT 0x3f8 // COM1
void serial_init() { void serial_init()
{
outb(SERIAL_IO_PORT + 1, 0x00); // Disable all interrupts outb(SERIAL_IO_PORT + 1, 0x00); // Disable all interrupts
outb(SERIAL_IO_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor) outb(SERIAL_IO_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
outb(SERIAL_IO_PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud outb(SERIAL_IO_PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
...@@ -16,33 +17,40 @@ void serial_init() { ...@@ -16,33 +17,40 @@ void serial_init() {
outb(SERIAL_IO_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set outb(SERIAL_IO_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
} }
int is_transmit_empty() { int is_transmit_empty()
{
return inb(SERIAL_IO_PORT + 5) & 0x20; return inb(SERIAL_IO_PORT + 5) & 0x20;
} }
void serial_writechar(char a) { void serial_writechar(char a)
{
while (is_transmit_empty() == 0); while (is_transmit_empty() == 0);
outb(SERIAL_IO_PORT,a); outb(SERIAL_IO_PORT,a);
} }
void serial_writestring(char* s) { void serial_writestring(char* s)
while (*s) { {
while (*s)
{
serial_writechar(*s); serial_writechar(*s);
s++; s++;
} }
} }
const char* hexchars = "0123456789abcdef"; const char* hexchars = "0123456789abcdef";
void serial_hexword(uint32_t x) { void serial_hexword(uint32_t x)
for (int i = 8; i != 0; i--) { {
for (int i = 8; i != 0; i--)
{
serial_writechar(hexchars[x>>i*4&0xf]); serial_writechar(hexchars[x>>i*4&0xf]);
} }
} }
void serial_hexstring(void* s, uint32_t n) { void serial_hexstring(void* s, uint32_t n) {
char* d = (char*)s; char* d = (char*)s;
for (uint32_t i = 0; i < n; i++) { for (uint32_t i = 0; i < n; i++)
{
serial_writechar(hexchars[(d[i]>>4)&0xf]); serial_writechar(hexchars[(d[i]>>4)&0xf]);
serial_writechar(hexchars[d[i]&0xf]); serial_writechar(hexchars[d[i]&0xf]);
} }
......
...@@ -8,22 +8,28 @@ void *memset(void *s, int c, size_t n){ ...@@ -8,22 +8,28 @@ void *memset(void *s, int c, size_t n){
} }
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;
do { /* generate digits in reverse order */ do /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */ {
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */ } while ((n /= 10) > 0); /* delete it */
if (sign < 0) if (sign < 0)
{
s[i++] = '-'; s[i++] = '-';
}
s[i] = '\0'; s[i] = '\0';
//reverse(s); //reverse(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++;
......
...@@ -6,11 +6,13 @@ ...@@ -6,11 +6,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;
...@@ -24,40 +26,51 @@ size_t terminal_column; ...@@ -24,40 +26,51 @@ size_t terminal_column;
uint8_t terminal_color; 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]);
}
} }
...@@ -8,7 +8,8 @@ ...@@ -8,7 +8,8 @@
#include <stddef.h> #include <stddef.h>
#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,
......
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