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

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