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

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