;
;
;
+%include "arch/x86_64/include/common.inc.asm"
[BITS 64]
[extern Log]
MM_LOCALAPIC equ 0xFFFFFD0000000000
-%macro PUSH_GPR 0
- mov [rsp-0x60], rsp
- mov [rsp-0x08], r15
- mov [rsp-0x10], r14
- mov [rsp-0x18], r13
- mov [rsp-0x20], r12
- mov [rsp-0x28], r11
- mov [rsp-0x30], r10
- mov [rsp-0x38], r9
- mov [rsp-0x40], r8
- mov [rsp-0x48], rdi
- mov [rsp-0x50], rsi
- mov [rsp-0x58], rbp
- mov [rsp-0x68], rbx
- mov [rsp-0x70], rdx
- mov [rsp-0x78], rcx
- mov [rsp-0x80], rax
- sub rsp, 0x80
-%endmacro
-%macro POP_GPR 0
- add rsp, 0x80
- mov r15, [rsp-0x08]
- mov r14, [rsp-0x10]
- mov r13, [rsp-0x18]
- mov r12, [rsp-0x20]
- mov r11, [rsp-0x28]
- mov r10, [rsp-0x30]
- mov r9, [rsp-0x38]
- mov r8, [rsp-0x40]
- mov rdi, [rsp-0x48]
- mov rsi, [rsp-0x50]
- mov rbp, [rsp-0x58]
- ;mov rsp, [rsp-0x60]
- mov rbx, [rsp-0x68]
- mov rdx, [rsp-0x70]
- mov rcx, [rsp-0x78]
- mov rax, [rsp-0x80]
-%endmacro
-
[section .text]
[global Desctab_Init]
Desctab_Init:
; Start interrupts
sti
+
+ ; Initialise System Calls (SYSCALL/SYSRET)
+ ; Set IA32_EFER.SCE
+ mov ecx, 0xC0000080
+ rdmsr
+ or eax, 1
+ wrmsr
+ ; Set IA32_LSTAR (RIP of handler)
+ mov ecx, 0xC0000082 ; IA32_LSTAR
+ mov eax, SyscallStub - 0xFFFFFFFF00000000
+ mov edx, 0xFFFFFFFF
+ wrmsr
+ ; Set IA32_FMASK (flags mask)
+ mov ecx, 0xC0000084
+ rdmsr
+ mov eax, 0x202
+ wrmsr
+ ; Set IA32_STAR (Kernel/User CS)
+ mov ecx, 0xC0000081
+ rdmsr
+ mov edx, 0x8 | (0x18 << 16) ; Kernel CS (and Kernel DS/SS - 8), User CS
+ wrmsr
ret
; RDI - IRQ Number
; RSI - Callback
+ ; Check for RDI >= 16
cmp rdi, 16
jb .numOK
xor rax, rax
jmp .ret
.numOK:
- mov rax, rdi
- shr rax, 3+2
+ ; Get handler base into RAX
+ lea rax, [rdi*4]
mov rcx, gaIRQ_Handlers
- add rax, rcx
+ lea rax, [rcx+rax*8]
; Find a free callback slot
%rep NUM_IRQ_CALLBACKS
pop rsi
pop rdi
+ ; Assign and return
mov [rax], rsi
xor rax, rax
[global IrqCommon]
IrqCommon:
PUSH_GPR
+ push gs
+ push fs
- mov rbx, [rsp+16*8] ; Calculate address
- shr rbx, 3+2 ; *8*4
+ mov rbx, [rsp+(16+2)*8] ; Get interrupt number (16 GPRS + 2 SRs)
+; xchg bx, bx ; Bochs Magic break (NOTE: will clear the high-bits of RBX)
+ shl rbx, 2 ; *8*4
mov rax, gaIRQ_Handlers
- add rbx, rax
+ lea rbx, [rax+rbx*8]
; Check all callbacks
+ sub rsp, 8 ; Shadow of argument
%assign i 0
%rep NUM_IRQ_CALLBACKS
; Get callback address
test rax, rax ; Check if it exists
jz .skip.%[i]
; Set RDI to IRQ number
- mov rdi, [rsp+16*8] ; Get IRQ number
+ mov rdi, [rsp+(16+2+1)*8] ; Get IRQ number
call rax ; Call
.skip.%[i]:
add rbx, 8 ; Next!
%assign i i+1
%endrep
+ add rsp, 8
; ACK
+ mov al, 0x20
mov rdi, [rsp+16*8] ; Get IRQ number
cmp rdi, 8
- mov al, 0x20
jb .skipAckSecondary
mov dx, 0x00A0
out dx, al
mov dx, 0x0020
out dx, al
+ pop fs
+ pop gs
POP_GPR
add rsp, 8*2
;xchg bx, bx
[extern Proc_Scheduler]
[global SchedulerIRQ]
+;
+; NOTE: Proc_Scheduler makes assumptions about the stack state when called
+;
SchedulerIRQ:
+ push 0 ; Error code
+ push 0 ; IRQNum
PUSH_GPR
+ push gs
+ push fs
;PUSH_FPU
;PUSH_XMM
mov rax, dr0
push rax
- ; Get the CPU Number
- mov rdi, dr1
+ mov rdi, dr1 ; Get the CPU Number
+ mov rsi, rsp ; Save stack pointer
+ mov rdx, SchedulerIRQ.restoreState
; Call the Scheduler
call Proc_Scheduler
+.restoreState:
; Restore Thread Pointer
pop rax
;POP_XMM
;POP_FPU
+ pop fs
+ pop gs
POP_GPR
+ add rsp, 2*8 ; Dummy error code and IRQ num
iretq
+[extern ci_offsetof_tThread_KernelStack]
+[extern SyscallHandler]
+[global SyscallStub]
+SyscallStub:
+ mov rbp, dr0
+ mov ebx, [rel ci_offsetof_tThread_KernelStack]
+ mov rbp, [rbp+rbx] ; Get kernel stack
+ xchg rbp, rsp ; Swap stacks
+
+ push rbp ; Save User RSP
+ push rcx ; RIP
+ push r11 ; RFLAGS
+
+ ; RDI
+ ; RSI
+ ; RDX
+ ; R10 (RCX for non syscall)
+ ; R8
+ ; R9
+ sub rsp, (6+2)*8
+ mov [rsp+0x00], rax ; Number
+; mov [rsp+0x08], rax ; Errno (don't care really)
+ mov [rsp+0x10], rdi ; Arg1
+ mov [rsp+0x18], rsi ; Arg2
+ mov [rsp+0x20], rdx ; Arg3
+ mov [rsp+0x28], r10 ; Arg4
+ mov [rsp+0x30], r8 ; Arg5
+ mov [rsp+0x38], r9 ; Arg6
+
+ mov rdi, rsp
+ sub rsp, 8
+ call SyscallHandler
+ add rsp, 8
+ mov ebx, [rsp+8] ; Get errno
+ mov rax, [rsp+0] ; Get return
+ add rsp, (6+2)*8
+
+ pop r11
+ pop rcx
+ pop rsp ; Change back to user stack
+ sysret
+
[section .data]
gIDT:
; 64-bit Interrupt Gate, CS = 0x8, IST0 (Disabled)