Commit 3b3b9e3c by triazo

Merge branch 'GDTLDT' into 'master'

Gdtldt

Still some unexplained weird things, but works.

See merge request !8
parents 6f768eab 8ffdb91a
#include "gdt.h"
#include "string.h"
#include "serial.h"
//#include "term.h"
uint16_t gdt_limit;
uint32_t gdt_base;
gdt_desc _gdt [MAX_GDT_DESC];
#define set_cs( cs ) asm volatile ( "ljmp %0, $1337f \n\t 1337: \n\t" :: "i"(cs) )
void gdt_install(){
struct{
uint16_t limit;
uint32_t base;
}__attribute__((packed)) gdtr;
gdtr.limit = gdt_limit;
gdtr.base = gdt_base;
asm volatile ("lgdt (%0)": :"r"(&gdtr));
set_cs(8);
asm volatile("mov $0x10, %ax; \
mov %ax, %ds; \
mov %ax, %es; \
mov %ax, %fs; \
mov %ax, %gs; \
mov %ax, %ss;");
strlen("");
}
void gdt_set_desc(uint32_t i, uint64_t base, uint64_t limit, uint8_t access, uint8_t gran){
if (i > MAX_GDT_DESC)
return;
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].high_base = (uint8_t)((base >> 24) & 0xff);
_gdt[i].lim = (uint16_t)(limit & 0xffff);
_gdt[i].flags = access;
_gdt[i].gran = (uint8_t)((limit >> 16) & 0x0f);
_gdt[i].gran |= gran & 0xf0;
}
gdt_desc* gdt_get_desc (int i) {
if (i > MAX_GDT_DESC)
return 0;
return &_gdt[i];
}
int gdt_init() {
gdt_limit = (sizeof (gdt_desc) * MAX_GDT_DESC)-1;
gdt_base = (uint32_t)&_gdt[0];
//null
gdt_set_desc(0, 0, 0, 0, 0);
//code
gdt_set_desc(1,0,0xffffffff, GDT_RW|GDT_EXE|GDT_CDT|GDT_MEM, GDT_GR_PG|GDT_GR_32|GDT_GR_HI);
//data
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");
gdt_install();
return 0;
}
#ifndef _GDT_INC
#define _GDT_INC
#include <stdint.h>
#define MAX_GDT_DESC 3
#define GDT_ACC 0x0001
#define GDT_RW 0x0002
#define GDT_DIR 0x0004
#define GDT_EXE 0x0008
#define GDT_CDT 0x0010
#define GDT_DPL 0x0060
#define GDT_MEM 0x0080
#define GDT_GR_HI 0x0f
#define GDT_GR_OS 0x10
#define GDT_GR_32 0x40
#define GDT_GR_PG 0x80
typedef struct{
uint16_t lim;
uint16_t low_base;
uint8_t mid_base;
uint8_t flags;
uint8_t gran;
uint8_t high_base;
} __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 gdt_desc* gdt_get_desc(int i);
extern int gdt_init();
#endif
#include "idt.h"
#include "string.h"
#include "serial.h"
uint16_t idt_limit;
uint32_t idt_base;
idt_desc _idt [MAX_IDT_INT];
void idt_install() {
struct{
uint16_t limit;
uint32_t base;
} __attribute__((packed)) idtr;
idtr.limit = idt_limit;
idtr.base = idt_base;
char f[20];
itoa(idtr.limit, f);
itoa(idtr.base, f);
asm volatile ("lidt (%0)": :"r"(&idtr));
}
static void default_handler () {
//DEBUG OUTPUT MAYBE?
serial_writestring("int caught\n");
return;
}
idt_desc* get_ir (uint32_t i) {
if (i>MAX_IDT_INT)
return 0;
return &_idt[i];
}
int install_ir (uint32_t i, uint16_t flags, uint16_t code_sel, IRQ_HANDLER irq) {
if (i>MAX_IDT_INT)
return 0;
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;
}
int idt_init (uint16_t code_sel) {
idt_limit = sizeof (idt_desc) * MAX_IDT_INT -1;
idt_base = (uint32_t)&_idt[0];
memset ((void*)&_idt[0], 0, sizeof (idt_desc) * MAX_IDT_INT-1);
for (int i=0; i<MAX_IDT_INT; i++)
install_ir (i, IDT_PR|IDT_32,code_sel,(IRQ_HANDLER)default_handler);
idt_install();
char f[20];
itoa((uintptr_t)&_idt,f);
//serial_writestring(f);
return 0;
}
#ifndef _IDT_INC
#define _IDT_INC
#include <stdint.h>
#define MAX_IDT_INT 256
#define IDT_16 0x06
#define IDT_32 0x0E
#define IDT_R1 0x40
#define IDT_R2 0x20
#define IDT_R3 0x60
#define IDT_PR 0x80
typedef void (*IRQ_HANDLER)(void);
typedef struct {
uint16_t low_base;
uint16_t code_sel;
uint8_t reserved;
uint8_t flags;
uint16_t high_base;
}__attribute__((packed)) idt_desc;
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 idt_init (uint16_t code_sel);
#endif
......@@ -2,25 +2,30 @@
#include <stddef.h>
#include <stdint.h>
#include "string.h"
#include "serial.h"
#include "term.h"
#include "mem.h"
#include "gdt.h"
#include "idt.h"
#if defined(__cplusplus)
extern "C" /* Use C linkage for kernel_main. */
#endif
void kernel_main() {
/* Initialize terminal and serial interfaces */
terminal_initialize();
/* Initialize terminal and serial interfaces */
terminal_initialize();
serial_init();
gdt_init();
idt_init(0x8);
/* Since there is no support for newlines in terminal_putchar
* yet, '\n' will produce some VGA specific character instead.
* This is normal.
*/
serial_writestring("First Serial Test!\n");
serial_writestring("IDT initialized\n");
terminal_writestring("Hello, kernel World!\n");
serial_writestring("Second Serial Test\n");
//serial_writestring("Second Serial Test\n");
// Enable paging of memory. For now there is only one virtual
// memory space defined by page_dir
......@@ -31,4 +36,7 @@ void kernel_main() {
enablePaging();
serial_writestring("Finished Initilizing memory\n");
asm ("int $0x80");
asm ("int $0x90");
asm ("hlt");
}
C=/home/adam/college/kernel/cross/bin/i686-elf-gcc
C=i686-elf-gcc
all:
/usr/bin/nasm -felf32 head.asm -o boot.o
......
#include "string.h"
void *memset(void *s, int c, size_t n){
unsigned char* p=s;
while(n--)
*p++=(unsigned char) c;
return s;
}
void itoa(int n, char s[])
{
int i, sign;
if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do { /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
//reverse(s);
}
size_t strlen(const char* str) {
size_t ret = 0;
while ( str[ret] != 0 )
ret++;
return ret;
}
#ifndef _STRING_INC
#define _STRING_ING
#include<stddef.h>
void* memset(void *s, int c, size_t n);
void itoa(int n, char s[]);
size_t strlen(const char* str);
#endif
......@@ -7,20 +7,13 @@
#include "term.h"
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 c16 = c;
uint16_t color16 = color;
return c16 | color16 << 8;
}
size_t strlen(const char* str) {
size_t ret = 0;
while ( str[ret] != 0 )
ret++;
return ret;
uint16_t c16 = c;
uint16_t color16 = color;
return c16 | color16 << 8;
}
static const size_t VGA_WIDTH = 80;
......@@ -32,39 +25,39 @@ uint8_t terminal_color;
uint16_t* terminal_buffer;
void terminal_initialize() {
terminal_row = 0;
terminal_column = 0;
terminal_color = make_color(COLOR_LIGHT_GREY, COLOR_BLACK);
terminal_buffer = (uint16_t*) 0xB8000;
for (size_t y = 0; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = make_vgaentry(' ', terminal_color);
}
terminal_row = 0;
terminal_column = 0;
terminal_color = make_color(COLOR_LIGHT_GREY, COLOR_BLACK);
terminal_buffer = (uint16_t*) 0xB8000;
for (size_t y = 0; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = make_vgaentry(' ', terminal_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) {
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = make_vgaentry(c, color);
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = make_vgaentry(c, color);
}
void terminal_putchar(char c) {
terminal_putentryat(c, terminal_color, terminal_column, terminal_row);
if (++terminal_column == VGA_WIDTH) {
terminal_column = 0;
if (++terminal_row == VGA_HEIGHT) {
terminal_row = 0;
}
terminal_putentryat(c, terminal_color, terminal_column, terminal_row);
if (++terminal_column == VGA_WIDTH) {
terminal_column = 0;
if (++terminal_row == VGA_HEIGHT) {
terminal_row = 0;
}
}
}
void terminal_writestring(const char* data) {
size_t datalen = strlen(data);
for (size_t i = 0; i < datalen; i++)
terminal_putchar(data[i]);
size_t datalen = strlen(data);
for (size_t i = 0; i < datalen; i++)
terminal_putchar(data[i]);
}
......@@ -6,30 +6,29 @@
#include <stdint.h>
#include <stddef.h>
#include "string.h"
/* Hardware text mode color constants. */
enum vga_color {
COLOR_BLACK = 0,
COLOR_BLUE = 1,
COLOR_GREEN = 2,
COLOR_CYAN = 3,
COLOR_RED = 4,
COLOR_MAGENTA = 5,
COLOR_BROWN = 6,
COLOR_LIGHT_GREY = 7,
COLOR_DARK_GREY = 8,
COLOR_LIGHT_BLUE = 9,
COLOR_LIGHT_GREEN = 10,
COLOR_LIGHT_CYAN = 11,
COLOR_LIGHT_RED = 12,
COLOR_LIGHT_MAGENTA = 13,
COLOR_LIGHT_BROWN = 14,
COLOR_WHITE = 15,
COLOR_BLACK = 0,
COLOR_BLUE = 1,
COLOR_GREEN = 2,
COLOR_CYAN = 3,
COLOR_RED = 4,
COLOR_MAGENTA = 5,
COLOR_BROWN = 6,
COLOR_LIGHT_GREY = 7,
COLOR_DARK_GREY = 8,
COLOR_LIGHT_BLUE = 9,
COLOR_LIGHT_GREEN = 10,
COLOR_LIGHT_CYAN = 11,
COLOR_LIGHT_RED = 12,
COLOR_LIGHT_MAGENTA = 13,
COLOR_LIGHT_BROWN = 14,
COLOR_WHITE = 15,
};
uint8_t make_color(enum vga_color fg, enum vga_color bg);
uint16_t make_vgaentry(char c, uint8_t color);
size_t strlen(const char* str);
void terminal_initialize();
void terminal_setcolor(uint8_t color);
......
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