# HG changeset patch # User windel # Date 1321729288 -3600 # Node ID f3e3e0e9c4bcde596aaa85acded809894872adaa # Parent ddefe6d97cd717987496a6b00dd5a422b2efe39c First attempt IDT loader 64 bits. INT13 occurs diff -r ddefe6d97cd7 -r f3e3e0e9c4bc cos/kernel/asmcode.asm --- a/cos/kernel/asmcode.asm Fri Nov 18 14:41:53 2011 +0100 +++ b/cos/kernel/asmcode.asm Sat Nov 19 20:01:28 2011 +0100 @@ -6,44 +6,69 @@ align 4 ; Port helpers: -global outb -outb: - mov eax, [esp + 8] - mov edx, [esp + 4] - out dx, al - ret +;global outb +;outb: +; mov eax, [esp + 8] +; mov edx, [esp + 4] +; out dx, al +; ret -global inb -inb: - xor eax, eax - mov edx, [esp + 4] - in al, dx - ret - -; Helper functions: -global enableinterrupts -enableinterrupts: - sti - ret +;global inb +;inb: +; xor eax, eax +; mov edx, [esp + 4] +; in al, dx +; ret global halt halt: cli hlt +global magicBochsBreak +magicBochsBreak: + xchg bx,bx + ret + global loadIDT loadIDT: + extern idtP + ; TODO: make this pointer thing more insightfull: + ;xchg bx, bx ; For debugging with bochs + lidt [idtP] ret +%macro pushAll 0 + push rax + push rcx + push rdx + push rbx + push rbp + push rsi + push rdi +%endmacro + +%macro popAll 0 + pop rdi + pop rsi + pop rbp + pop rbx + pop rdx + pop rcx + pop rax +%endmacro + ; Define macro with two arguments: %macro INTX 2 global %1 %1: ; Do some saving: -extern %2 - call %2 + pushAll + extern %2 + call %2 ; Do restoration - iret + popAll + iretq %endmacro diff -r ddefe6d97cd7 -r f3e3e0e9c4bc cos/kernel/handlers.c --- a/cos/kernel/handlers.c Fri Nov 18 14:41:53 2011 +0100 +++ b/cos/kernel/handlers.c Sat Nov 19 20:01:28 2011 +0100 @@ -34,66 +34,75 @@ void INT34(void); // THE interrupt descriptor table: -static struct IDT_entry idt[256]; +static IDT_entry idt[256]; +volatile idtPointer idtP; -void setIDTentry(int num, void (*handler)()) +void setIDTentry(int num, void (*handler)(), uint16_t selector, uint8_t flags) { // Fill one entry with IDT info: uint64_t offset; - unsigned short selector = 0x8; // Code selector - unsigned char type = 0x8E; // 32 bits interrupt gate. Thingy is present - offset = (uint64_t) handler; + // Typecast the function pointer to a number: + offset = (uint64_t)handler; - idt[num].b[0] = offset & 0xFF; - idt[num].b[1] = (offset >> 8) & 0xFF; - idt[num].b[2] = selector & 0xFF; - idt[num].b[3] = (selector >> 8) & 0xFF; - idt[num].b[4] = 0; // reserved - idt[num].b[5] = type; - idt[num].b[6] = (offset >> 16) & 0xFF; - idt[num].b[7] = (offset >> 24) & 0xFF; + // Set offset: + idt[num].baseLow = offset & 0xFFFF; + idt[num].baseMid = (offset >> 16) & 0xFFFF; + idt[num].baseHigh = (offset >> 32) & 0xFFFFFFFF; + + // Set flags and selector: + idt[num].selector = selector; + idt[num].flags = flags; + + // Set reserved fields: + idt[num].reserved1 = 0; + idt[num].reserved2 = 0; } void setupIDT(void) { int i; // Fill all vectors with the default handler: for (i=0; i<256; i++) { - setIDTentry(i, &INTDEF); + setIDTentry(i, &INTDEF, 0x08, 0x8E); } // Now set other then default handler: - setIDTentry(0, INT0); - setIDTentry(1, INT1); - setIDTentry(2, INT2); - setIDTentry(3, INT3); - setIDTentry(4, INT4); - setIDTentry(5, INT5); - setIDTentry(6, INT6); - setIDTentry(7, INT7); - setIDTentry(8, INT8); - setIDTentry(9, INT9); - setIDTentry(10, INT10); - setIDTentry(11, INT11); - setIDTentry(12, INT12); - setIDTentry(13, INT13); - setIDTentry(14, INT14); - setIDTentry(15, INT15); - setIDTentry(16, INT16); - setIDTentry(17, INT17); - setIDTentry(18, INT18); - setIDTentry(19, INT19); + setIDTentry(0, INT0, 0x08, 0x8E); + setIDTentry(1, INT1, 0x08, 0x8E); + setIDTentry(2, INT2, 0x08, 0x8E); + setIDTentry(3, INT3, 0x08, 0x8E); + setIDTentry(4, INT4, 0x08, 0x8E); + setIDTentry(5, INT5, 0x08, 0x8E); + setIDTentry(6, INT6, 0x08, 0x8E); + setIDTentry(7, INT7, 0x08, 0x8E); + setIDTentry(8, INT8, 0x08, 0x8E); + setIDTentry(9, INT9, 0x08, 0x8E); + setIDTentry(10, INT10, 0x08, 0x8E); + setIDTentry(11, INT11, 0x08, 0x8E); + setIDTentry(12, INT12, 0x08, 0x8E); + setIDTentry(13, INT13, 0x08, 0x8E); + setIDTentry(14, INT14, 0x08, 0x8E); + setIDTentry(15, INT15, 0x08, 0x8E); + setIDTentry(16, INT16, 0x08, 0x8E); + setIDTentry(17, INT17, 0x08, 0x8E); + setIDTentry(18, INT18, 0x08, 0x8E); + setIDTentry(19, INT19, 0x08, 0x8E); /* reserved interrupts: */ // From int20 - int31 - setIDTentry(32, INT32); - setIDTentry(33, INT33); - setIDTentry(34, INT34); + setIDTentry(32, INT32, 0x08, 0x8E); + setIDTentry(33, INT33, 0x08, 0x8E); + setIDTentry(34, INT34, 0x08, 0x8E); + // Set the correct values in the IDT pointer: + idtP.base = (uint64_t)&idt; + idtP.limit = (sizeof(IDT_entry) * 256) - 1; // call load IDT asm function: - loadIDT(idt, 256*8-1); + loadIDT(); PICremap(); - enableinterrupts(); + printf("enable ints\n"); + asm("sti"); + printf("Done!\n"); } // PIC functions: @@ -208,19 +217,17 @@ panic("Unhandled exception!"); } -void* getUnusedPage() { +void* getUnusedPage() +{ return 0; - } -void mappage(uint32_t address) { +void mappage(uint64_t address) { uint32_t pageDirIndex = address >> 22; uint32_t pageTableIndex = (address >> 12) & 0x3FF; printf("Allocating page at %d, tableindex=%d\n", pageDirIndex, pageTableIndex); //uint32_t *pageTable = (uint32_t*)(pageDirectory[pageDirIndex] & 0xFFFFFC00); - - } void INT14handler(unsigned int address) diff -r ddefe6d97cd7 -r f3e3e0e9c4bc cos/kernel/kernel.c --- a/cos/kernel/kernel.c Fri Nov 18 14:41:53 2011 +0100 +++ b/cos/kernel/kernel.c Sat Nov 19 20:01:28 2011 +0100 @@ -1,7 +1,7 @@ #include "kernel.h" static int shiftstate = 0; -static volatile unsigned char charAvail = 0; +static volatile uint8_t charAvail = 0; static volatile char kbdchar = ' '; static char keymap[128] = { @@ -28,6 +28,27 @@ '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?' }; +// IO port helpers: +void outb(uint16_t port, uint8_t value) +{ + asm volatile ("outb %1, %0" : : "dN" (port), "a" (value)); +} + +uint8_t inb(uint16_t port) +{ + uint8_t ret; + asm volatile ("inb %1, %0" : "=a" (ret) : "dN" (port)); + return ret; +} + +uint16_t inw(uint16_t port) +{ + uint16_t ret; + asm volatile ("inw %1, %0" : "=a" (ret) : "dN" (port)); + return ret; +} + + static uint64_t ticks = 0; void timerDriverUpdate() { @@ -114,7 +135,7 @@ /* Allocates 'size' bytes and returns the pointer if succesfull. Kernelpanic in case of failure.. */ -void* malloc(size_t size) { +void* malloc(uint64_t size) { printf("Malloc %d bytes\n", size); return kernel_heap; } @@ -127,10 +148,9 @@ { init_screen(); clear_screen(); - printf("Welcome!\n"); - printf("sizeof(uint32_t)=%u, sizeof(uint64_t)=%u\n", sizeof(uint32_t), sizeof(uint64_t)); + printf("Welcome! .. "); - printf("Enabling interrupts\n"); + printf("Enabling interrupts .. "); setupIDT(); printf("Entering mainloop!\n"); diff -r ddefe6d97cd7 -r f3e3e0e9c4bc cos/kernel/kernel.h --- a/cos/kernel/kernel.h Fri Nov 18 14:41:53 2011 +0100 +++ b/cos/kernel/kernel.h Sat Nov 19 20:01:28 2011 +0100 @@ -5,45 +5,60 @@ #define NULL ((void*)0) // Type defs: +typedef unsigned char uint8_t; +typedef unsigned short uint16_t; typedef unsigned int uint32_t; -typedef unsigned long long uint64_t; -typedef unsigned short ushort_t; -typedef unsigned char uchar_t; -typedef unsigned int uint_t; -typedef unsigned long ulong_t; -typedef unsigned long long ulonglong_t; -typedef unsigned long off_t; -typedef unsigned long size_t; +typedef unsigned long int uint64_t; +// IDT related structures: +typedef struct { + uint16_t baseLow; + uint16_t selector; + uint8_t reserved1; + uint8_t flags; + uint16_t baseMid; + uint32_t baseHigh; + uint32_t reserved2; +} __attribute__((packed)) IDT_entry; + +typedef struct { + uint16_t limit; + uint64_t base; +} __attribute__((packed)) idtPointer; + +// memory alloc functions: +void* malloc(uint64_t size); +void free(void* ptr); + +// STDout funcs: void printf(const char* fmt, ... ); void memset(void* ptr, uint32_t value, uint32_t num); void memcpy(void* dst, void* src, uint32_t num); -struct IDT_entry { - unsigned char b[8]; -}; - -// memory alloc functions: -void* malloc(size_t size); -void free(void* ptr); - +// Screen related: void clear_screen(); void init_screen(); void print_string(const char *); // For IO ports: -unsigned char inb(unsigned short); -void outb(unsigned short, unsigned char); +uint8_t inb(uint16_t); +uint16_t inw(uint16_t); +void outb(uint16_t, uint8_t); + +// Interrupt functions: +void setupIDT(void); +void PICremap(void); // ASM helper: -int loadIDT(struct IDT_entry *table, unsigned short size); +void loadIDT(void); + +// Panic exit: void halt(void); -void setupIDT(void); -void PICremap(void); +// Bochs xchg bx,bx breakpoint: +void magicBochsBreak(); + // Assembler util functions: -void enableinterrupts(void); -void callint49(void); void doCPUID(int eax, int *ebx, int *ecx, int *edx); // Keyboard driver: @@ -51,7 +66,7 @@ void timerDriverUpdate(void); // Memory functions: -void mappage(uint32_t address); +void mappage(uint64_t address); int querymode(void); int getcs(void); diff -r ddefe6d97cd7 -r f3e3e0e9c4bc cos/kernel/snprintf.c --- a/cos/kernel/snprintf.c Fri Nov 18 14:41:53 2011 +0100 +++ b/cos/kernel/snprintf.c Sat Nov 19 20:01:28 2011 +0100 @@ -18,7 +18,7 @@ #define _SP 0x80 /* hard space (0x20) */ -unsigned char _ctype[] = { +uint8_t _ctype[] = { _C,_C,_C,_C,_C,_C,_C,_C, /* 0-7 */ _C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C, /* 8-15 */ _C,_C,_C,_C,_C,_C,_C,_C, /* 16-23 */ @@ -44,7 +44,7 @@ _L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L, /* 224-239 */ _L,_L,_L,_L,_L,_L,_L,_P,_L,_L,_L,_L,_L,_L,_L,_L}; /* 240-255 */ -#define __ismask(x) (_ctype[(int)(unsigned char)(x)]) +#define __ismask(x) (_ctype[(int)(uint8_t)(x)]) #define isalnum(c) ((__ismask(c)&(_U|_L|_D)) != 0) #define isalpha(c) ((__ismask(c)&(_U|_L)) != 0) @@ -61,14 +61,14 @@ #define isascii(c) (((unsigned char)(c))<=0x7f) #define toascii(c) (((unsigned char)(c))&0x7f) -static inline unsigned char tolower(unsigned char c) +static inline uint8_t tolower(uint8_t c) { if (isupper(c)) c -= 'A'-'a'; return c; } -static inline unsigned char toupper(unsigned char c) +static inline uint8_t toupper(uint8_t c) { if (islower(c)) c -= 'a'-'A'; @@ -86,8 +86,8 @@ void va_snprintf(char *b, int l, const char *fmt, va_list pvar) { int n, i; - uint_t u; - ulonglong_t ull; + uint32_t u; + uint64_t ull; char *t; char d[10]; char mod_l; @@ -165,7 +165,7 @@ l = 0; break; } - ull = va_arg(pvar, unsigned long long); + ull = va_arg(pvar, uint64_t); for (i = 15; i >= 0; i--) { b[i] = hexmap[ull & 0x0f]; ull >>= 4;