File: justintime.tex

package info (click to toggle)
faumachine 20100527-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 53,836 kB
  • ctags: 20,552
  • sloc: ansic: 179,550; asm: 3,645; makefile: 3,611; perl: 2,103; sh: 1,529; python: 600; xml: 563; lex: 210; vhdl: 204
file content (510 lines) | stat: -rw-r--r-- 14,021 bytes parent folder | download | duplicates (2)
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
% $Id: justintime.tex,v 1.7 2009-03-02 15:22:20 potyra Exp $
%
% Copyright (C) 2003-2009 FAUmachine Team <info@faumachine.org>.
% This program is free software. You can redistribute it and/or modify it
% under the terms of the GNU General Public License, either version 2 of
% the License, or (at your option) any later version. See COPYING.

\documentclass[a4paper,10pt]{article}
\usepackage{german,epsfig}
% \parindent0pt

\title{FAUmachine Just-In-Time Compiler}
\author{Volkmar Sieh \\{\tt Volkmar.Sieh@informatik.uni-erlangen.de}}

\begin{document}
\maketitle
\tableofcontents

%---------------------
\section{Introduction}
%---------------------

...

%-----------------
\section{Overview}
%-----------------

The simulator needs some registers and some memory for simulation.
Which registers and which part of memory is used is decribed in
section XXXXX.

Most of the code can be executed without any change. Nevertheless some
code must be changed just-in-time to allow execution in user-mode.
Section XXXXX describes how this transformation should be done.

This code transformation has to be done every time new code has
to be executed. This leads to questions when to compile new
instructions, how many instructions should be compiled at once,
what code to remove and so on. Section XXXXX deals with these questions.

Data structures used by the just-in-time compiler are described in
section XXXXX.

%------------------------------
\section{Register/Memory Usage}
%------------------------------

Segment registers \%fs and \%gs are use by simulator itself. All
other (user writeable) registers (\%eax, \%ebx, \%ecx, \%edx, \%edi,
\%esi, \%ebp, \%esp, \%cs, \%ds, \%es, \%ss) are used in the standard
way.

\%fs will be used by the simulator if \%fs or \%gs is used by user code.
\%gs allways contains the segment selector of a data segment 0-4 GByte.

%----------------------------
\section{Code Transformation}
%----------------------------

Most of the code can be executed without any change. Some instructions
can be modified easily such that an in-place-replacement seems
reasonable. Nevertheless all code must be copied from the original
location to some kind of cache to allow code modifications (see
subsection XXXXX).

Some instructions (see subsection XXXXX) will get a prefix which is a
jump into the simulator itself as they are too complicated to be
transformed into a small number of different instructions.

Instructions affecting the control-flow must be changed to reflect new
target addresses and to correctly push/pop return addresses on stack.
See subsection XXXXX for more information.

At the end of all compiled code the {\tt compile} subroutine of the
simulator must be called. See subsection XXXXX for information on
these subroutine calls.

Subsection XXXXX shows some examples of modified code.

\subsection{Simple Code Modification}
%------------------------------------

Code using the \%fs or \%gs prefix must be replaced because the
registers are already used by the simulator.

It might be modified very simply:
\begin{verbatim}
    xyz %fs:ea
\end{verbatim}
should be replaced by
\begin{verbatim}
    movw %gs:fs, %fs
    xyz  %fs:ea
\end{verbatim}

Similar:
\begin{verbatim}
    xyz %gs:ea
\end{verbatim}
should be replaced by
\begin{verbatim}
    movw %gs:gs, %fs
    xyz  %fs:ea
\end{verbatim}

% \begin{verbatim}
% If running in real-mode:
% Doesn't work - FIXME VOSSI
%     xyz	%cs:ea			movw	%cs, %fs
% 				xyz	%fs:ea
% \end{verbatim}

\subsection{Simulator Calls}
%---------------------------

All instructions which
\begin{itemize}
\item save or load segment selectors
	(e.g. {\tt pushw \%ds; movw \%ax, \%es; lss \%esp, stack})
\item have different semantics in user-mode and supervisor mode
	(e.g. {\tt pushf, popf})
\item are priviledged instructions
	(e.g. {\tt movl \%eax, \%cr3; cli})
\end{itemize}
must be modified. These more complicated instructions have to be
simulated. They will be compiled by replacing them with

\begin{verbatim}
x:  movl $x, %gs:eip
    jmp  simulator
\end{verbatim}

Example:

\begin{verbatim}
    pushf
\end{verbatim}

will be replaced by

\begin{verbatim}
x:  movl $x, %gs:eip
    jmp  simulator
\end{verbatim}

Using the address saved in \%gs:eip the simulator can determine
the instruction, simulate it and can return to the next instruction
following the simulated one.

The following is a list of instructions which must be simulated:

\begin{itemize}
\item movw \%?s, ea
\item movw ea, \%?s
\item pushw \%?s
\item popw \%?s
\item lcall *
\item lret
\item ljmp *
\item l?s \%reg, ea
\item sti
\item cli
\item pushf
\item popf
\item iret
\item in?
\item ins?
\item out?
\item outs?
\item *	\%cs:* /* if running in real-mode */
\item int *
\item rdtsc
\item lmsr
\item smsr
\item rdmsr
\item wrmsr
\item rdpmc
\item cpuid
\item lfence
\item mfence
\item rep nop
\item hlt
\item lgdt
\item sgdt
\item lldt
\item sldt
\item lidt
\item sidt
\item str
\item ltr
\item invlpg
\item wbinvd
\item clts
\item mov *,\%cr?
\item mov \%cr?,*
\item mov *,\%db?
\item mov \%db?,*
\end{itemize}

\subsection{Control-flow Instructions}
%-------------------------------------

All instructions modifying the control flow must be checked and
compiled correctly.

There are two strategies:

\begin{itemize}
\item Replace target addresses.
\item Stop before the call, ret, or jump instruction and simulate it.
\end{itemize}

If jump addresses are known at compile-time we should replace the
addresses by the new addresses. This is fairly simple and will give
no performance penalty.

The more complicated case is when the addresses are not known at
compile-time (e.g. during {\tt call}, {\tt ret}, {\tt jmp *xx}).
We must not execute a standard {\tt call} instruction because it will
push a modified return address onto the stack. This bad address
might be seen by the subroutine called.

Therefore we should simulate all such instructions. Because
these instructions will be called very often any optimization is
welcome. This optimization might be done by using a different
simulator call:

\begin{verbatim}
x:  movl $x, %gs:eip
    jmp  control_flow
    ...
\end{verbatim}

Example:
\begin{verbatim}
    call func
\end{verbatim}
should be replaced by
\begin{verbatim}
x:  movl $x, %gs:eip
    jmp  control_flow
    call func
\end{verbatim}

\subsection{End of Compiled Code}
%------------------------------------------

There might be a chance that there is a huge basic block in user code
not containing any simulator call. The block might be that big that the
compiled code doesn't fit into one cache line. This might happen if
the original code has a programming error leading to execution of bad
code (e.g. data).

In this case a jump instruction can jump to the next cache line
containing the following of the compiled code.

\subsection{Examples}
%--------------------

Original code:
\begin{verbatim}
    x
    ...
    y
    z
\end{verbatim}

Cache line 1:
\begin{verbatim}
    x
    ...
    y
    jmp next
\end{verbatim}

Cache line 2:
\begin{verbatim}
next:
    z
\end{verbatim}

%-----------------------
\section{Compiling Code}
%-----------------------

There are several questions:
\begin{itemize}
\item When do we have to load and compile code?
\item How many and which instructions should we load and compile?
\item When do we have to throw away compiled code?
\item What code should be thrown away?
\end{itemize}

\subsection{When do we have to load and compile code?}
%-----------------------------------------------------

We have to load and compile code (at least one instruction) if we
want to continue simulation. If we return from the simulation of one
instruction we return to compiled code. There must be at least one
executable instruction in the buffer.

\subsection{How many instructions should be loaded and compiled?}
%----------------------------------------------------------------

There might be several different strategies:

Load and compile
\begin{itemize}
\item exactly one instruction
\item a stream of instructions up to the next control flow instruction
	with special address ({\tt call}, {\tt ret}, {\tt iret}, or
	{\tt jmp *} instruction) or up to the next special (prefixed)
	instruction
\item one basic block (up to the next jump, call or conditional branch
	instruction)
\item one procedure (up to the next {\tt ret} or {\tt iret} instruction)
\item as many instruction as we have space in buffer
\end{itemize}

Different strategies will lead to different overhead:

If we only load an compile one instruction we don't load any
instruction which is not really needed. On the other hand we have to
call the simulator very often to load next instructions.

Filling the buffer with instructions may lead to loading of many
instructions not executed.

\subsection{When do we have to throw away code?}
%-----------------------------------------------

We have to throw away compiled code if the real code is modified
somehow. This has to be detected. It might be detected by changing
access rights of the pages containing the real code. Pages may be
set to be read-only in case we read any byte of an instruction out
of the page. Any write access to that page later on will lead to a
page-fault. In the page-fault handler we can throw away the
pre-compiled code of that page and set the read-write flag again.

This detection is not needed in case we only load and compile exactly
one instruction and throw it away after execution.

The other case in which we must flush pre-compiled code is when the
buffer gets full. Then we have to free code compiled previously.

\subsection{What code should be thrown away?}
%--------------------------------------------

We can throw away {\em any} code as we can reload it later if it is
used again. But of cause we should throw away that code, that is not
used any longer. This seems to be difficult to decide.

So in the first step it should be ok to implement any strategie. A
simple but nevertheless good strategie seems to be the strategie
removing the oldest code in cache.

%------------------------
\section{Data Structures}
%------------------------

The data used by the just-in-time module can be divided into two parts.
First there's a cache with {\tt CACHE\_LINES} cache lines where code
is compiled into. The other part is an array of {\tt CACHE\_LINES}
records containing informations about all these cache lines.
\begin{verbatim}
struct {
    unsigned char cache[N][CACHE_LINE_SIZE];
    struct cache_info cache_info[N];
};
\end{verbatim}

All cache lines have the same size ({\tt CACHE\_LINE\_SIZE}). It should
be a power of 2 to speed up calculation of real-eip / cache-eip.

Cache line info is stored in records of the following type:
\begin{verbatim}
struct cache_info {
    unsigned long eip;
    unsigned long len;

    struct cache_info *hash_next;
    struct cache_info *lru_next;
    struct cache_info *ref1_first;
    struct cache_info *ref2_first;
    struct cache_info *ref1_next;
    struct cache_info *ref2_next;
};
\end{verbatim}

{\tt eip} is the original address of the first instruction in cache.
It is used for calculation the hash of the record (see below).

The length of the cache line is given by the {\tt len} variable with
the following semantics:

Non-modified instructions of length {\tt N} will be counted with {\tt N}.
Only one modified instruction will be in one cache line as last
instruction. This instruction (original length {\tt M} bytes) will
only count with 1 byte. Jumps to another cache line at the end of a
cache line will {\em not} be counted. Same with fetch instructions at
the end of a cache line.

{\tt hash\_next} is used to find cache lines containing code for a given
eip quickly. The hash function is
\begin{verbatim}
unsigned long hash(unsigned long eip)
{
    return (eip / CACHE_LINE_SIZE) % HASHSIZE;
}
\end{verbatim}

{\tt lru\_next} is a pointer to the next cache line to be re-used in case
of running out of cache lines for new compiled code.

Every cache line can reference at most two following cache lines. This is
when one cache line ends with a {\tt jcc} instruction which directs
control flow to at most two different locations.
Referencing cache lines are stored using two single linked lists
({\tt ref1\_first}, {\tt ref1\_next} and {\tt ref2\_first},
{\tt ref2\_next}). If a cache line references itself this is not stored
in these lists.

%------------------
\section{Simulator}
%------------------

Simulator entry:

\begin{verbatim}
simulator:
    /*
     * Switch stack.
     */
    /* Save program stack. */
    /* movw %ss, %gs:ss */	/* No need to save. Cannot be changed... */
    movl %esp, %gs:esp

    /* Setup simulator stack. */
    movw %gs:ss_sim, %ss
    movl %gs:esp_sim, %esp

    /*
     * Switch data segments.
     */
    /* Save program data segments. */
    /* movw %ds, %gs:ds */ /* No need to save. Cannot be changed... */
    /* movw %es, %gs:es */
    /* movw %fs, %gs:fs */ /* No need to save. Will be reloaded... */
    /* movw %gs, %gs:gs */

    /* Load simulator data segments. */
    movw %gs:ds_sim, %ds
    movw %gs:es_sim, %es
    /* movw %gs:fs_sim, %fs */ /* Not used. */
    /* movw %gs:gs_sim, %gs */

    /*
     * Save registers.
     */
    pushfl
    popl eflags
    movl %eax, eax
    movl %ebx, ebx
    movl %ecx, ecx
    movl %edx, edx
    movl %edi, edi
    movl %esi, esi
    movl %ebp, ebp

    /*
     * Do simulation...
     */
    movl %esp, %eax
    pushl %eax
    call sim
    addl $4, %esp

    /*
     * Restore registers.
     */
    movl ebp, %ebp
    movl esi, %esi
    movl edi, %edi
    movl edx, %edx
    movl ecx, %ecx
    movl ebx, %ebx
    movl eax, %eax
    pushl eflags
    popfl

    /*
     * Switch data segments.
     */
    movw %gs:es, %es
    movw %gs:ds, %ds
    /* movw %gs:fs, %fs */ /* Will be reloaded before use. */
    /* movw %gs:gs, %gs */

    /*
     * Switch stack.
     */
    movl %gs:esp, %esp
    movw %gs:ss, %ss

    /* Return. */
    ljmp *%gs:cs_sim:%gs:eip_sim
\end{verbatim}

\end{document}