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

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