Bugfixing the x86_64 port
[tpg/acess2.git] / Kernel / arch / x86_64 / desctab.asm
index 838f67e..816d14e 100644 (file)
@@ -3,6 +3,8 @@
 ;
 [BITS 64]
 
+[extern Log]
+
 %define NUM_IRQ_CALLBACKS      4
 
 MM_LOCALAPIC   equ     0xFFFFFD0000000000
@@ -78,15 +80,22 @@ Desctab_Init:
        %endrep
        
        ; Install IRQs
-       %macro SETIRQ 1
-       SETIDT 0xF0+%1, Irq%1
-       %endmacro
-       
-       %assign i 0
-       %rep 16
-       SETIRQ i
-       %assign i i+1
-       %endrep
+       SETIDT  0xF0, SchedulerIRQ
+       SETIDT  0xF1, Irq1
+       SETIDT  0xF2, Irq2
+       SETIDT  0xF3, Irq3
+       SETIDT  0xF4, Irq4
+       SETIDT  0xF5, Irq5
+       SETIDT  0xF6, Irq6
+       SETIDT  0xF7, Irq7
+       SETIDT  0xF8, Irq8
+       SETIDT  0xF9, Irq9
+       SETIDT  0xFA, Irq10
+       SETIDT  0xFB, Irq11
+       SETIDT  0xFC, Irq12
+       SETIDT  0xFD, Irq13
+       SETIDT  0xFE, Irq14
+       SETIDT  0xFF, Irq15
 
        ; Remap PIC
        push rdx        ; Save RDX
@@ -164,21 +173,41 @@ IRQ_AddHandler:
        
        ; Assign the IRQ Callback
 .assign:
+       ; A little bit of debug
+       push rdi
+       push rsi
+       push rax
+       sub rsp, 8
+       mov rcx, rdi    ; IRQ Number
+       mov rdx, rsi    ; Callback
+       mov rsi, rax    ; Pointer
+       mov rdi, csIRQ_Assigned
+       call Log
+       add rsp, 8
+       pop rax
+       pop rsi
+       pop rdi
+
        mov [rax], rsi
        xor rax, rax
 
 .ret:
        ret
+       
+[section .rodata]
+csIRQ_Assigned:
+       db      "IRQ %p := %p (IRQ %i)",0
+[section .text]
 
 %macro ISR_NOERRNO     1
 Isr%1:
-       push    0
-       push    %1
+       push    QWORD 0
+       push    QWORD %1
        jmp     ErrorCommon
 %endmacro
 %macro ISR_ERRNO       1
 Isr%1:
-       push    %1
+       push    QWORD %1
        jmp     ErrorCommon
 %endmacro
 
@@ -224,7 +253,8 @@ ErrorCommon:
        ;PUSH_FPU
        ;PUSH_XMM
        
-       mov rsi, rsp
+       mov rdi, rsp
+       xchg bx, bx
        call Error_Handler
        
        ;POP_XMM
@@ -233,7 +263,7 @@ ErrorCommon:
        pop gs
        POP_GPR
        add rsp, 2*8
-       iret
+       iretq
 
 %macro DEFIRQ  1
 Irq%1:
@@ -248,6 +278,7 @@ DEFIRQ      i
 %assign i i+1
 %endrep
 
+[global IrqCommon]
 IrqCommon:
        PUSH_GPR
        
@@ -256,18 +287,22 @@ IrqCommon:
        mov rax, gaIRQ_Handlers
        add rbx, rax
        
+       ; Check all callbacks
        %assign i 0
        %rep NUM_IRQ_CALLBACKS
+       ; Get callback address
        mov rax, [rbx]
-       test rax, rax
-       mov rdi, [rsp+16*8]     ; Get IRQ number
+       test rax, rax   ; Check if it exists
        jz .skip.%[i]
-       call rax        ; 2 Bytes (Op and Mod/RM)
+       ; Set RDI to IRQ number
+       mov rdi, [rsp+16*8]     ; Get IRQ number
+       call rax        ; Call
 .skip.%[i]:
-       add rbx, 8
+       add rbx, 8      ; Next!
        %assign i i+1
        %endrep
        
+       ; ACK
        mov rdi, [rsp+16*8]     ; Get IRQ number
        cmp rdi, 8
        mov al, 0x20
@@ -279,29 +314,56 @@ IrqCommon:
        out dx, al
        
        POP_GPR
-       add rsp, 16
-       iret
+       add rsp, 8*2
+       ;xchg bx, bx
+       iretq
 
 [extern Proc_Scheduler]
+[global SchedulerIRQ]
 SchedulerIRQ:
-       ; TODO: Find Current CPU
        PUSH_GPR
        ;PUSH_FPU
        ;PUSH_XMM
        
-       xor rsi, rsi
-       mov rdi, MM_LOCALAPIC+0x20
-       mov esi, [rdi]
+       ; Save Thread Pointer
+       mov rax, dr0
+       push rax
+       
+       ; Get the CPU Number
+       mov rdi, dr1
+       ; Call the Scheduler
        call Proc_Scheduler
        
+       ; Restore Thread Pointer
+       pop rax
+       mov dr0, rax
+       
+       ; Send EOI (To either the APIC or the PIC)
+       %if USE_MP
+       test ebx, ebx
+       jnz .sendEOI
+       %endif
+       ; PIC
+       mov al, 0x20
+       out 0x20, al            ; ACK IRQ
+       %if USE_MP
+       jmp .ret
+       ; APIC
+.sendEOI:
+       mov eax, DWORD [gpMP_LocalAPIC]
+       mov DWORD [eax+0x0B0], 0
+       %endif
+.ret:
+       
        ;POP_XMM
        ;POP_FPU
        POP_GPR
-       iret
+       iretq
 
 [section .data]
 gIDT:
-       times 256       dd      0x00080000, 0x00000E00, 0, 0    ; 64-bit Interrupt Gate, CS = 0x8, IST0
+       ; 64-bit Interrupt Gate, CS = 0x8, IST0 (Disabled)
+       times 256       dd      0x00080000, 0x00000E00, 0, 0
 gIDTPtr:
        dw      256*16-1
        dq      gIDT

UCC git Repository :: git.ucc.asn.au