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

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