Working on 64-bit IDT
[tpg/acess2.git] / Kernel / arch / x86_64 / start64.asm
1 ;
2 ; Acess2 x86_64 Port
3 ;
4 [bits 64]
5
6 [extern kmain]
7
8 [section .text]
9 [global start64]
10 start64:
11         ; Load Registers
12         mov ax, 0x10
13         mov ds, ax
14         mov es, ax
15         mov fs, ax
16         mov gs, ax
17         
18         ; Go to high memory
19         mov rax, start64.himem
20         jmp rax
21 .himem:
22         
23         ; Clear the screen
24         mov rax, 0x1F201F201F201F20     ; Set the screen to White on blue, space (4 characters)
25         mov edi, 0xB8000
26         mov ecx, 80*25*2/8
27         rep stosq
28         
29         ; Set kernel stack
30         mov rsp, gInitialKernelStack
31         
32         ; Call main
33         call kmain
34         
35         cli
36 .hlt:
37         hlt
38         jmp .hlt
39
40 [global GetRIP]
41 GetRIP:
42         mov rax, [rsp]
43         ret
44
45 [global GetCPUNum]
46 GetCPUNum:
47         str ax
48         mov gs, ax
49         xor rax, rax
50         mov al, [gs:104]        ; End of TSS
51         ret
52
53 KSTACK_USERSTATE_SIZE   equ     (16+1+5)*8      ; GPRegs, CPU, IRET
54 [global Proc_ReturnToUser]
55 [extern Proc_GetCurThread]
56 Proc_ReturnToUser:
57         ; RBP is the handler to use
58         
59         call Proc_GetCurThread
60         
61         ; EAX is the current thread
62         mov rbx, rax
63         mov rax, [rbx+40]       ; Get Kernel Stack
64         sub rax, KSTACK_USERSTATE_SIZE
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         ; Get and alter User SP
73         mov rcx, [rax+KSTACK_USERSTATE_SIZE-3*8]
74         mov rdx, [rbx+60]       ; Get Signal Number
75         mov [rcx-8], rdx
76         mov rax, User_Syscall_RetAndExit
77         mov [rcx-16], rax
78         sub rcx, 16
79         
80         ; Restore Segment Registers
81         mov ax, 0x23
82         mov ds, ax
83         mov es, ax
84         
85         push 0x23       ; SS
86         push rcx        ; RSP
87         push 0x202      ; RFLAGS (IF and Rsvd)
88         push 0x1B       ; CS
89         push rbp        ; RIP
90         
91         iret
92
93 ; int CallWithArgArray(void *Ptr, int NArgs, Uint *Args)
94 ; Call a function passing the array as arguments
95 [global CallWithArgArray]
96 CallWithArgArray:
97         push rbp
98         mov rbp, rsp
99         mov rcx, [rbp+3*8]      ; Get NArgs
100         mov rdx, [rbp+4*8]
101
102 .top:
103         mov rax, [rdx+rcx*8-8]
104         push rax
105         loop .top
106         
107         mov rax, [rbp+2*8]
108         call rax
109         lea rsp, [rbp]
110         pop rbp
111         ret
112
113 [section .usertext]
114 User_Syscall_RetAndExit:
115         mov rdi, rax
116         jmp User_Syscall_Exit
117 User_Syscall_Exit:
118         xor rax, rax
119         ; RDI: Return Value
120         int 0xAC
121
122 [section .bss]
123 [global gInitialKernelStack]
124         resd    1024*1  ; 1 Page
125 gInitialKernelStack:
126

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