a2ffb19b58e8ecf0e98ae697dd54275db234af14
[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         ; Set kernel stack
10         ; Call main
11         jmp $
12
13 [global GetRIP]
14 GetRIP:
15         mov rax, [rsp]
16         ret
17
18 [global GetCPUNum]
19 GetCPUNum:
20         str ax
21         mov gs, ax
22         xor rax, rax
23         mov al, [gs:104]        ; End of TSS
24         ret
25
26 KSTACK_USERSTATE_SIZE   equ     (16+1+5)*8      ; GPRegs, CPU, IRET
27 [global Proc_ReturnToUser]
28 [extern Proc_GetCurThread]
29 Proc_ReturnToUser:
30         ; RBP is the handler to use
31         
32         call Proc_GetCurThread
33         
34         ; EAX is the current thread
35         mov rbx, rax
36         mov rax, [rbx+40]       ; Get Kernel Stack
37         sub rax, KSTACK_USERSTATE_SIZE
38         
39         ;
40         ; NOTE: This can cause corruption if the signal happens while the user
41         ;       has called a kernel operation.
42         ; Good thing this can only be called on a user fault.
43         ;
44         
45         ; Get and alter User SP
46         mov rcx, [rax+KSTACK_USERSTATE_SIZE-3*8]
47         mov rdx, [rbx+60]       ; Get Signal Number
48         mov [rcx-8], rdx
49         mov rax, User_Syscall_RetAndExit
50         mov [rcx-16], rax
51         sub rcx, 16
52         
53         ; Restore Segment Registers
54         mov ax, 0x23
55         mov ds, ax
56         mov es, ax
57         
58         push 0x23       ; SS
59         push rcx        ; RSP
60         push 0x202      ; RFLAGS (IF and Rsvd)
61         push 0x1B       ; CS
62         push rbp        ; RIP
63         
64         iret
65
66 ; int CallWithArgArray(void *Ptr, int NArgs, Uint *Args)
67 ; Call a function passing the array as arguments
68 [global CallWithArgArray]
69 CallWithArgArray:
70         push rbp
71         mov rbp, rsp
72         mov rcx, [rbp+3*8]      ; Get NArgs
73         mov rdx, [rbp+4*8]
74
75 .top:
76         mov rax, [rdx+rcx*8-8]
77         push rax
78         loop .top
79         
80         mov rax, [rbp+2*8]
81         call rax
82         lea rsp, [rbp]
83         pop rbp
84         ret
85
86 [section .usertext]
87 User_Syscall_RetAndExit:
88         mov rdi, rax
89         jmp User_Syscall_Exit
90 User_Syscall_Exit:
91         xor rax, rax
92         ; RDI: Return Value
93         int 0xAC
94
95 [section .bss]
96 [global gInitialKernelStack]
97         resd    1024*1  ; 1 Page
98 gInitialKernelStack:
99

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