Build - Restructured configuration
[tpg/acess2.git] / Kernel / arch / x86_64 / start64.asm
1 ;
2 ; Acess2 x86_64 Port
3 ;
4 %include "arch/x86_64/include/common.inc.asm"
5 [bits 64]
6 ;KERNEL_BASE    equ     0xFFFF800000000000
7 KERNEL_BASE     equ     0xFFFFFFFF80000000
8
9 [extern kmain]
10
11 [extern gMultibootPtr]
12 [extern gMultibootMagic]
13
14 [section .text]
15 [global start64]
16 start64:
17         ; Load Registers
18         mov ax, 0x10
19         mov ds, ax
20         mov es, ax
21         mov fs, ax
22         mov gs, ax
23         
24         ; Go to high memory
25         mov rax, start64.himem
26         jmp rax
27 .himem:
28         
29         xor rax, rax
30         mov dr0, rax    ; Set CPU0
31         
32         ; Clear the screen
33         mov rax, 0x1F201F201F201F20     ; Set the screen to White on blue, space (4 characters)
34         mov edi, 0xB8000
35         mov ecx, 80*25*2/8
36         rep stosq
37         
38         ; Set kernel stack
39         mov rsp, 0xFFFFA00000000000 + INITIAL_KSTACK_SIZE*0x1000
40         
41         ; Call main
42         mov edi, [gMultibootMagic - KERNEL_BASE]
43         mov esi, [gMultibootPtr - KERNEL_BASE]
44         call kmain
45         
46         cli
47 .hlt:
48         hlt
49         jmp .hlt
50
51 [global GetCPUNum]
52 GetCPUNum:
53         xor rax, rax
54         str ax
55         sub ax, 0x38    ; TSS Base
56         shr ax, 4       ; One 16-byte TSS per CPU
57         ret
58
59 KSTACK_USERSTATE_SIZE   equ     (5+2+16+2)*8    ; IRET, ErrorNum, ErrorCode, GPRs, FS&GS
60 [global Proc_ReturnToUser]
61 Proc_ReturnToUser:
62         ; RDI - Handler
63         ; RSI - Kernel Stack
64         ; RDX - Signal num
65         
66         ;
67         ; NOTE: This can cause corruption if the signal happens while the user
68         ;       has called a kernel operation.
69         ; Good thing this can only be called on a user fault.
70         ;
71
72         xchg bx, bx     
73         ; Get and alter User SP
74         mov rcx, [rsi-0x20]     ; Get user SP
75         xor eax, eax
76         mov [rcx-16], rax
77         sub rcx, 16
78         
79         ; Drop down to user mode
80         cli
81         mov rsp, rcx    ; Set SP
82         mov rcx, rdi    ; SYSRET IP
83         
84         mov rdi, rdx    ; Argument for handler
85         mov r11, 0x202  ; RFlags
86         db 0x48
87         sysret
88
89 ; int CallWithArgArray(void *Ptr, int NArgs, Uint *Args)
90 ; Call a function passing the array as arguments
91 [global CallWithArgArray]
92 CallWithArgArray:
93         push rbp
94         mov rbp, rsp
95         push r10
96         push r11
97         
98         mov [rbp+2*8], rdi      ; Save Ptr to stack
99         
100         mov r11, rsi    ; NArgs
101         mov r10, rdx    ; Args
102
103         ; Arg 1: RDI
104         mov rdi, [r10]
105         add r10, 8
106         dec r11
107         jz .call
108         ; Arg 2: RSI
109         mov rsi, [r10]
110         add r10, 8
111         dec r11
112         jz .call
113         ; Arg 3: RDX
114         mov rdx, [r10]
115         add r10, 8
116         dec r11
117         jz .call
118         ; Arg 4: RCX
119         mov rcx, [r10]
120         add r10, 8
121         dec r11
122         jz .call
123         ; Arg 5: R8
124         mov r8, [r10]
125         add r10, 8
126         dec r11
127         jz .call
128         ; Arg 6: R9
129         mov r9, [r10]
130         add r10, 8
131         dec r11
132         jz .call
133         ; No support for more
134
135 .call:
136         mov rax, [rbp+2*8]      ; Ptr
137         call rax
138         
139         pop r11
140         pop r10
141         
142         lea rsp, [rbp]
143         pop rbp
144         ret
145
146 ; vim: ft=nasm

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