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

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