-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux_syscall.html
251 lines (208 loc) · 8.33 KB
/
linux_syscall.html
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="Habibutsu">
<title>Habibutsu / Системный вызов в Linux</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<!-- Custom styles for this template -->
<link href="static/theme.css" rel="stylesheet">
<!-- Just for debugging purposes. Don't actually copy this line! -->
<!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<link rel="stylesheet" href="static/highlight/styles/solarized_dark.css">
<script src="static/highlight/highlight.pack.js"></script>
<script type="text/javascript">
hljs.initHighlightingOnLoad();
//hljs.configure({useBR: true});
$('div.code').each(function(i, e) {hljs.highlightBlock(e)});
</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-51121455-1', 'habibutsu.github.io');
ga('send', 'pageview');
</script>
</head>
<body role="document">
<!-- Fixed navbar -->
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Alexander Verbitsky</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="index.html">Главная</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Заметки<b class="caret"></b></a>
<ul class="dropdown-menu">
<li class="dropdown-header">Системный вызов в Linux</li>
<li><a href="#">Модель памяти</a></li>
<li><a href="git_help.html">Шпаргалка по Git</a></li>
</ul>
</li>
<li><a href="slides.html">Слайды</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
<div class="container theme-showcase" role="main">
<div class="row well">
<div class="col-sm-8 blog-main">
<div class="blog-post">
<h2 class="blog-post-title">Системный вызов в Linux</h2>
<p class="blog-post-meta">Март 13, 2014 <a href="#">Habibutsu</a></p>
<p>
Небольшая справка по реализации системного вызова на разных платформах в Linux.
</p>
<p>
<h4>Aссемблерная инструкция</h4>
<ul>
<li>intel x86 <b>int $0x80</b></li>
<li>intel x86-64 <b>syscall</b></li>
<li>armv71 <b>svc 0x0</b></li>
</ul>
<h4>Номер вызова</h4>
<ul>
<li>intel x86 <b>регистр EAX</b></li>
<li>intel x86-64 <b>регистр RAX</b></li>
<li>armv71 <b>регистр r7</b></li>
</ul>
<h4>Передача параметров</h4>
Менее 6 параметров:
<ul>
<li>intel x86 <b>регистры EBX, ECX, EDX, ESI, EDI, EBP</b></li>
<li>intel x86-64 <b>регистры RDI, RSI, RDX, R10, R8, R9</b></li>
<li>armv71 <b>регистры r0, r1, r2, r3, r4, r5, r6</b></li>
</ul>
Более 6 параметров:
<ul>
<li>intel x86 <b>указатель на область памяти через регистр EBX </b></li>
<li>intel x86-64 <b style="color:red">запрещено</b></li>
</ul>
<h4>Возврат результата</h4>
<ul>
<li>intel x86 <b>регистр EAX</b></li>
<li>intel x86-64 <b>регистр RAX</b></li>
<li>armv71 <b>регистр r0</b></li>
</ul>
<h4>Примеры</h4>
Реализация на C:
<pre class="c">
<code>#include <sys/syscall.h>
int main(void)
{
syscall(SYS_write, 1, "Hello, world\n", 14);
return 0;
}
</code></pre>
intel x86:
<pre class="avrasm">
<code>#include <asm/unistd.h>
#include <syscall.h>
.global _start
.text
message:
.string "Hello, world\n"
_start:
# syscall SYS_write
mov $SYS_write, %eax
mov $1, %ebx # stdout
mov $message, %ecx
mov $14, %edx # length
int $0x80
# exit 0
mov $SYS_exit, %eax
mov $0, %ebx
int $0x80
</code></pre>
intel x86-64:
<pre class="avrasm">
<code>#include <asm/unistd.h>
#include <syscall.h>
.global _start
.text
message:
.string "Hello, world\n"
_start:
# syscall SYS_write
movl $SYS_write, %eax
movl $1, %edi # stdout
movl $message, %esi
movl $14, %edx # length
syscall
# exit 0
movl $0, %edi
movl $SYS_exit, %eax
syscall</code></pre>
armv71:
<pre class="avrasm">
<code>message:
.ascii "Hello, world\012\000"
.text
.align 2
.global _start
_start:
mov r7, #4
mov r0, #1
movw r1, #:lower16:message
movt r1, #:upper16:message
movl r2, #14
svc 0x0
# exit 0
mov r7, #1
mov r0, #0
svc 0x0</code></pre>
Для компиляции
<pre class="bash"><code>$ gcc -c -o hello.o hello.S
$ ld -o hello hello.o</code></pre>
</p>
</div>
</div>
<!-- /blog-main -->
<div class="col-sm-3 col-sm-offset-1 blog-sidebar">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title" style="font-size: 18px">Архив</h4>
</div>
<div class="panel-body">
<ol class="list-unstyled">
<li><a href="#">Январь 2014</a></li>
</ol>
</div>
</div>
<div class="sidebar-module well">
<h4>Мой профиль</h4>
<ol class="list-unstyled">
<li><a href="https://plus.google.com/+AlexanderVerbitsky">G+</a></li>
<li><a href="https://github.com/habibutsu">GitHub</a></li>
<li><a href="#">Linkedin</a></li>
</ol>
</div>
</div><!-- /.blog-sidebar -->
</div><!-- /.row -->
</div> <!-- /container -->
</body>
</html>