-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrappers.S
214 lines (196 loc) · 4.09 KB
/
wrappers.S
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <asm.h>
#include <segment.h>
/**************************************************/
/**** Save & Restore ******************************/
/** **/
/** When we change to privilege level 0 (kernel) **/
/** (through an interrupt, a system call, an **/
/** exception ...) we must save the state of the **/
/** currently running task (save). **/
/** **/
/** Stack layout in 'systemCall': **/
/** **/
/** 0(%esp) - %ebx \ **/
/** 4(%esp) - %ecx | **/
/** 8(%esp) - %edx | **/
/** C(%esp) - %esi | Register saved **/
/** 10(%esp) - %edi | by 'save' **/
/** 14(%esp) - %ebp | **/
/** 18(%esp) - %eax | **/
/** 1C(%esp) - %ds | **/
/** 20(%esp) - %es | **/
/** 24(%esp) - %fs | **/
/** 28(%esp) - %gs / **/
/** 2C(%esp) - %eip \ **/
/** 30(%esp) - %cs | **/
/** 34(%esp) - %eflags | Return context saved **/
/** 38(%esp) - %oldesp | by the processor. **/
/** 3C(%esp) - %oldss / **/
/** **/
/**************************************************/
#define SAVE_ALL \
pushl %gs; \
pushl %fs; \
pushl %es; \
pushl %ds; \
pushl %eax; \
pushl %ebp; \
pushl %edi; \
pushl %esi; \
pushl %edx; \
pushl %ecx; \
pushl %ebx; \
movl $__KERNEL_DS, %edx; \
movl %edx, %ds; \
movl %edx, %es
#define RESTORE_ALL \
popl %ebx;\
popl %ecx;\
popl %edx;\
popl %esi;\
popl %edi;\
popl %ebp;\
popl %eax;\
popl %ds; \
popl %es; \
popl %fs; \
popl %gs
#define SYSCALL(syscalln) \
leal syscall_ret##syscalln, %eax; \
push %eax; \
movl $syscalln, %eax; \
push %ebp; \
movl %esp, %ebp; \
sysenter; \
syscall_ret##syscalln: pop %ebp; \
addl $4, %esp
#define CHECK_ERROR(end) \
cmpl $0, %eax; \
jge end; \
negl %eax; \
leal errno, %ebx; \
movl %eax, (%ebx); \
movl $-1, %eax
ENTRY(write)
push %ebp
movl %esp, %ebp
push %ebx
movl 8(%ebp), %ebx
movl 12(%ebp), %ecx
movl 16(%ebp), %edx
SYSCALL(4);
CHECK_ERROR(write.no_error);
write.no_error:
pop %ebx
pop %ebp
ret
ENTRY(read)
push %ebp
movl %esp, %ebp
push %ebx
movl 8(%ebp), %ebx
movl 12(%ebp), %ecx
movl 16(%ebp), %edx
SYSCALL(3);
CHECK_ERROR(read.no_error);
read.no_error:
pop %ebx
pop %ebp
ret
ENTRY(gettime)
push %ebp
movl %esp, %ebp
SYSCALL(10);
pop %ebp
ret
ENTRY(getpid)
push %ebp
movl %esp, %ebp
SYSCALL(20)
pop %ebp
ret
ENTRY(fork)
push %ebp
movl %esp, %ebp
push %ebx
SYSCALL(2)
CHECK_ERROR(fork.no_error)
fork.no_error:
pop %ebx
pop %ebp
ret
ENTRY(clone)
push %ebp
movl %esp,% ebp
push %ebx
movl 8(%ebp), %ebx
movl 12(%ebp), %ecx
SYSCALL(19)
CHECK_ERROR(clone.no_error)
clone.no_error:
pop %ebx
pop %ebp
ret
ENTRY(get_stats)
push %ebp
movl %esp, %ebp
push %ebx
movl 8(%ebp), %ebx
movl 12(%ebp), %ecx
SYSCALL(35)
CHECK_ERROR(get_stats.no_error)
get_stats.no_error:
pop %ebx
pop %ebp
ret
ENTRY(sem_init)
push %ebp
movl %esp, %ebp
push %ebx
movl 8(%ebp), %ebx
movl 12(%ebp), %ecx
SYSCALL(21)
CHECK_ERROR(sem_init.no_error)
sem_init.no_error:
pop %ebx
pop %ebp
ret
ENTRY(sem_wait)
push %ebp
movl %esp, %ebp
push %ebx
movl 8(%ebp), %ebx
SYSCALL(22)
CHECK_ERROR(sem_wait.no_error)
sem_wait.no_error:
pop %ebx
pop %ebp
ret
ENTRY(sem_signal)
push %ebp
movl %esp, %ebp
push %ebx
movl 8(%ebp), %ebx
SYSCALL(23)
CHECK_ERROR(sem_signal.no_error)
sem_signal.no_error:
pop %ebx
pop %ebp
ret
ENTRY(sem_destroy)
push %ebp
movl %esp, %ebp
push %ebx
movl 8(%ebp), %ebx
SYSCALL(24)
CHECK_ERROR(sem_destroy.no_error)
sem_destroy.no_error:
pop %ebx
pop %ebp
ret
ENTRY(exit)
push %ebp
movl %esp, %ebp
SYSCALL(1)
pop %ebp
ret