VM8086 stub for x86_64 build
[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 [section .usertext]
67 User_Syscall_RetAndExit:
68         mov rdi, rax
69         jmp User_Syscall_Exit
70 User_Syscall_Exit:
71         xor rax, rax
72         ; RDI: Return Value
73         int 0xAC
74
75 [section .bss]
76 [global gInitialKernelStack]
77         resd    1024*1  ; 1 Page
78 gInitialKernelStack:
79

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