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

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