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

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