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

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