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

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