More work on x86_64 port, near compiling now
[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 KSTACK_USERSTATE_SIZE   equ     (4+8+1+5)*4     ; SRegs, GPRegs, CPU, IRET
19 [global Proc_ReturnToUser]
20 [extern Proc_GetCurThread]
21 Proc_ReturnToUser:
22         ; RBP is the handler to use
23         
24         call Proc_GetCurThread
25         
26         ; EAX is the current thread
27         mov rbx, rax
28         mov rax, [rbx+40]       ; Get Kernel Stack
29         sub rax, KSTACK_USERSTATE_SIZE
30         
31         ;
32         ; NOTE: This can cause corruption if the signal happens while the user
33         ;       has called a kernel operation.
34         ; Good thing this can only be called on a user fault.
35         ;
36         
37         ; Get and alter User SP
38         mov rcx, [rax+KSTACK_USERSTATE_SIZE-3*8]
39         mov rdx, [rbx+60]       ; Get Signal Number
40         mov [rcx-8], rdx
41         mov QWORD [rcx-16], User_Syscall_RetAndExit
42         sub rcx, 16
43         
44         ; Restore Segment Registers
45         mov ax, 0x23
46         mov ds, ax
47         mov es, ax
48         
49         push 0x23       ; SS
50         push rcx        ; RSP
51         push 0x202      ; RFLAGS (IF and Rsvd)
52         push 0x1B       ; CS
53         push rbp        ; RIP
54         
55         iret
56
57 [section .usertext]
58 User_Syscall_RetAndExit:
59         mov rdi, rax
60         jmp User_Syscall_Exit
61 User_Syscall_Exit:
62         xor rax, rax
63         ; RDI: Return Value
64         int 0xAC
65
66 [section .bss]
67 [global gInitialKernelStack]
68         resd    1024*1  ; 1 Page
69 gInitialKernelStack:
70

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