More work on x86_64 port of usermode
[tpg/acess2.git] / Usermode / Libraries / ld-acess.so_src / arch / x86.asm.h
1 ; ========================
2 ; AcssMicro - System Calls
3 ; ========================
4
5 %include "../../../Kernel/include/syscalls.inc.asm"
6
7 [bits 32]
8 [section .data]
9 [global _errno:data 4]
10 _errno: dw      0
11
12 [section .text]
13 ; DEST
14 ; SRC
15 _memcpy:
16         push ebp
17         mov ebp, esp
18         push edi
19         push esi        ; // DI and SI must be maintained, CX doesn't
20         
21         mov ecx, [ebp+16]
22         mov esi, [ebp+12]
23         mov edi, [ebp+8]
24         rep movsb
25         
26         pop esi
27         pop edi
28         pop ebp
29         ret
30
31 #define SYSCALL0(_name,_num)    SYSCALL0 _name, _num
32 #define SYSCALL1(_name,_num)    SYSCALL1 _name, _num
33 #define SYSCALL2(_name,_num)    SYSCALL2 _name, _num
34 #define SYSCALL3(_name,_num)    SYSCALL3 _name, _num
35 #define SYSCALL4(_name,_num)    SYSCALL4 _name, _num
36 #define SYSCALL5(_name,_num)    SYSCALL5 _name, _num
37 #define SYSCALL6(_name,_num)    SYSCALL6 _name, _num
38
39 ;%define SYSCALL_OP     jmp 0xCFFF0000
40 %define SYSCALL_OP      int 0xAC
41
42 ; System Call - No Arguments
43 %macro SYSCALL0 2
44 [global %1:func]
45 %1:
46         push ebx
47         mov eax, %2
48         SYSCALL_OP
49         mov [_errno], ebx
50         pop ebx
51         ret
52 %endmacro
53
54 %macro _SYSCALL_HEAD 2
55 [global %1:func]
56 %1:
57         push ebp
58         mov ebp, esp
59         push ebx
60         mov eax, %2
61 %endmacro
62 %macro _SYSCALL_TAIL 0
63         mov [_errno], ebx
64         pop ebx
65         pop ebp
66         ret
67 %endmacro
68
69 ; System Call - 1 Argument
70 %macro SYSCALL1 2
71 _SYSCALL_HEAD %1, %2
72         mov ebx, [ebp+8]
73         SYSCALL_OP
74 _SYSCALL_TAIL
75 %endmacro
76
77 ; System Call - 2 Arguments
78 %macro SYSCALL2 2
79 _SYSCALL_HEAD %1, %2
80         mov eax, %2
81         mov ebx, [ebp+8]
82         mov ecx, [ebp+12]
83         SYSCALL_OP
84 _SYSCALL_TAIL
85 %endmacro
86
87 ; System Call - 3 Arguments
88 %macro SYSCALL3 2
89 _SYSCALL_HEAD %1, %2
90         mov ebx, [ebp+8]
91         mov ecx, [ebp+12]
92         mov edx, [ebp+16]
93         SYSCALL_OP
94 _SYSCALL_TAIL
95 %endmacro
96
97 ; System Call - 4 Arguments
98 %macro SYSCALL4 2
99 _SYSCALL_HEAD %1, %2
100         push edi
101         mov ebx, [ebp+8]
102         mov ecx, [ebp+12]
103         mov edx, [ebp+16]
104         mov edi, [ebp+20]
105         SYSCALL_OP
106         pop edi
107 _SYSCALL_TAIL
108 %endmacro
109
110 ; System Call - 5 Arguments
111 %macro SYSCALL5 2
112 _SYSCALL_HEAD %1, %2
113         push edi
114         push esi
115         mov ebx, [ebp+8]
116         mov ecx, [ebp+12]
117         mov edx, [ebp+16]
118         mov edi, [ebp+20]
119         mov esi, [ebp+24]
120         SYSCALL_OP
121         pop esi
122         pop edi
123 _SYSCALL_TAIL
124 %endmacro
125
126 ; System Call - 6 Arguments
127 %macro SYSCALL6 2
128 _SYSCALL_HEAD %1, %2
129         push edi
130         push esi
131         mov ebx, [ebp+8]
132         mov ecx, [ebp+12]
133         mov edx, [ebp+16]
134         mov edi, [ebp+20]
135         mov esi, [ebp+24]
136         mov ebp, [ebp+28]
137         SYSCALL_OP
138         pop esi
139         pop edi
140 _SYSCALL_TAIL
141 %endmacro
142
143 ; // Override the clone syscall
144 #define clone   _clone_raw
145 #include "syscalls.s.h"
146 #undef clone
147
148 [global clone:func]
149 clone:
150         push ebp
151         mov ebp, esp
152         push ebx
153         
154         mov ebx, [ebp+12]       ; Get new stack pointer
155         
156         ; Check if the new stack is being used
157         test ebx, ebx
158         jz .doCall
159         ; Modify it to include the calling function (and this)
160         %if 0
161         mov eax, [ebp]  ; Get old stack frame
162         sub eax, ebp    ; Get size
163         sub ebx, eax    ; Alter new stack pointer
164         push eax        ; < Size
165         push DWORD [ebp]        ; < Source
166         push ebx        ; < Dest
167         call _memcpy
168         add esp, 4*3    ; Restore stack
169         ; EBX should still be the new stack pointer
170         mov eax, [ebp]  ; Save old stack frame pointer in new stack
171         mov [ebx-4], eax
172         mov eax, [ebp-4]        ; Save EBX there too
173         mov [ebx-8], eax
174         sub ebx, 8      ; Update stack pointer for system
175         %else
176         ; Quick hack, just this stack frame
177         mov eax, [ebp+4]
178         mov [ebx-4], eax        ; Return
179         mov [ebx-8], ebx        ; EBP
180         mov DWORD [ebx-12], 0   ; EBX
181         sub ebx, 12
182         %endif
183 .doCall:
184         mov eax, SYS_CLONE
185         mov ecx, ebx    ; Stack
186         mov ebx, [ebp+8]        ; Flags
187         SYSCALL_OP
188         mov [_errno], ebx
189         pop ebx
190         pop ebp
191         ret
192

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