changeset 18:6129643f5c34

Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
author windel
date Sun, 20 Nov 2011 20:35:51 +0100
parents f3e3e0e9c4bc
children f454e3c592dd
files cos/Makefile cos/kernel/asmcode.asm cos/kernel/goto64.asm cos/kernel/handlers.c cos/kernel/kernel.c cos/kernel/snprintf.c cos/kernel/video.c
diffstat 7 files changed, 57 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/cos/Makefile	Sat Nov 19 20:01:28 2011 +0100
+++ b/cos/Makefile	Sun Nov 20 20:35:51 2011 +0100
@@ -15,6 +15,7 @@
 			-nostdinc \
 			-nostdlib \
 			-nostartfiles \
+			-mno-red-zone \
 			-fno-builtin \
 			-mcmodel=large \
 			-Iinclude
--- a/cos/kernel/asmcode.asm	Sat Nov 19 20:01:28 2011 +0100
+++ b/cos/kernel/asmcode.asm	Sun Nov 20 20:35:51 2011 +0100
@@ -5,21 +5,6 @@
 section .text
 align 4
 
-; Port helpers:
-;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
-
 global halt
 halt:
   cli
@@ -63,11 +48,16 @@
 global %1
 %1:
  ; Do some saving:
+ cli
  pushAll
+ ;xchg bx,bx
+
  extern %2
  call %2
  ; Do restoration
  popAll
+ ;xchg bx, bx
+ sti
  iretq
 
 %endmacro
--- a/cos/kernel/goto64.asm	Sat Nov 19 20:01:28 2011 +0100
+++ b/cos/kernel/goto64.asm	Sun Nov 20 20:35:51 2011 +0100
@@ -33,25 +33,20 @@
 ; GDT, three entries: one for code, one for data
 GDT64:
 .Null: equ $ - GDT64
-dw 0
-dw 0
-db 0
-db 0
-db 0
-db 0
+dq 0
 .Code: equ $ - GDT64
-dw 0
-dw 0
-db 0
-db 10011000b ; access
-db 00100000b ; granularity
+dw 0 ; Segment limit 15-0
+dw 0 ; Base 15 - 0
+db 0 ; Base 23 - 16
+db 10011000b ; access 0x98 (P=1 => Present)
+db 00100000b ; granularity 0x20 (L=1 => long mode)
 db 0
 .Data: equ $ - GDT64
 dw 0
 dw 0
 db 0
-db 10010000b ; access
-db 00000000b ; granularity
+db 10010000b ; access ; 0x90
+db 00000000b ; granularity 0x00
 db 0
 .Pointer: ; GDT pointer
 dw $ - GDT64 - 1 ; Limit
@@ -61,6 +56,22 @@
 global loader
 loader:
 
+; Check that the CPU supports long mode:
+mov eax, 80000000h
+cpuid
+cmp eax, 80000000h
+jbe no_long_mode
+mov eax, 80000001h
+cpuid
+bt edx, 29
+jnc no_long_mode
+jmp long_mode
+
+no_long_mode:
+hlt
+
+long_mode:
+
 ; Prepare paging:
 ; PML4T - 0x1000
 ; PDPT - 0x2000
@@ -97,6 +108,9 @@
 or eax, 1 << 5 ; PAE-bit is bit 5
 mov cr4, eax
 
+; Load the GDT:
+lgdt [GDT64.Pointer]
+
 ; Set LM-bit (Long Mode bit):
 mov ecx, 0xC0000080
 rdmsr
@@ -108,8 +122,6 @@
 or eax, 0x80000000 ; Set bit 31 (PG-bit)
 mov cr0, eax
 
-; Load the GDT:
-lgdt [GDT64.Pointer]
 
 ; Jump to 64 bits kernel:
 jmp GDT64.Code:Realm64
@@ -119,13 +131,17 @@
 ; realm64
 Realm64:
 
-cli
-mov ax, GDT64.Data
+; Clear segment registers:
+xor ax, ax
 mov ds, ax
 mov es, ax
+mov ss, ax
 mov fs, ax
 mov gs, ax
 
+; Reoad the GDT:
+lgdt [GDT64.Pointer]
+
 ; Done!
 
 ; Setup stack pointer:
--- a/cos/kernel/handlers.c	Sat Nov 19 20:01:28 2011 +0100
+++ b/cos/kernel/handlers.c	Sun Nov 20 20:35:51 2011 +0100
@@ -3,6 +3,7 @@
 void panic(char *msg) {
   printf("Kernel panic: ");
   printf(msg);
+  magicBochsBreak();
   halt();
 }
 
@@ -89,9 +90,9 @@
   setIDTentry(19, INT19, 0x08, 0x8E);
   /* reserved interrupts: */
   // From int20 - int31
-  setIDTentry(32, INT32, 0x08, 0x8E);
-  setIDTentry(33, INT33, 0x08, 0x8E);
-  setIDTentry(34, INT34, 0x08, 0x8E);
+  setIDTentry(32, INT32, 0x08, 0x8F);
+  setIDTentry(33, INT33, 0x08, 0x8F);
+  setIDTentry(34, INT34, 0x08, 0x8F);
 
   // Set the correct values in the IDT pointer:
   idtP.base = (uint64_t)&idt;
@@ -100,6 +101,7 @@
   loadIDT();
 
   PICremap();
+  magicBochsBreak();
   printf("enable ints\n");
   asm("sti");
   printf("Done!\n");
@@ -278,6 +280,7 @@
   //printf("INT32 called!\n");
   // called very frequent, what is this?
   timerDriverUpdate();
+  // Acknowledge int:
   outb(0x20, 0x20); // EOI to master
 }
 
--- a/cos/kernel/kernel.c	Sat Nov 19 20:01:28 2011 +0100
+++ b/cos/kernel/kernel.c	Sun Nov 20 20:35:51 2011 +0100
@@ -147,7 +147,6 @@
 void kmain()
 {
   init_screen();
-  clear_screen();
   printf("Welcome! .. ");
 
   printf("Enabling interrupts .. ");
--- a/cos/kernel/snprintf.c	Sat Nov 19 20:01:28 2011 +0100
+++ b/cos/kernel/snprintf.c	Sun Nov 20 20:35:51 2011 +0100
@@ -128,7 +128,7 @@
          break;
 
           case 'c':		/* single character */
-         *b++ = 'x'; // TODO: va_arg(pvar, char);
+         *b++ = (char)va_arg(pvar, int);
          l--;
          break;
 
--- a/cos/kernel/video.c	Sat Nov 19 20:01:28 2011 +0100
+++ b/cos/kernel/video.c	Sun Nov 20 20:35:51 2011 +0100
@@ -10,6 +10,15 @@
 
 static int row, col;
 
+void move_cursor()
+{
+   uint16_t cursorLocation = row * 80 + col;
+   outb(0x3D4, 14);
+   outb(0x3D5, cursorLocation >> 8);
+   outb(0x3D4, 15);
+   outb(0x3D5, cursorLocation & 0xFF);
+}
+
 void
 clear_screen()
 {
@@ -27,6 +36,7 @@
 {
 	row = col = 0;
 	clear_screen();
+   move_cursor();
 }
 
 
@@ -89,6 +99,7 @@
 		else
 			new_line();
 	}
+   move_cursor();
 }
 
 void