~ubuntu-branches/debian/squeeze/ffcall/squeeze

« back to all changes in this revision

Viewing changes to trampoline/PORTING

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Egger
  • Date: 2010-06-26 15:29:30 UTC
  • mfrom: (5.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20100626152930-c09y01gk3szcnykn
Tags: 1.10+cvs20100619-2
Ship to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
The list of CPUs and platforms TRAMPOLINE has been ported to can be found
 
2
at the top of file trampoline.c.
 
3
 
 
4
To port TRAMPOLINE to a new platform, three issues may have to be resolved:
 
5
A. a new CPU - how to build the trampoline?
 
6
B. a new OS - how to make code in malloc'ed memory executable?
 
7
C. a new CPU or OS - how to flush the instruction cache?
 
8
 
 
9
 
 
10
A. a new CPU - how to build the trampoline?
 
11
 
 
12
   The trampoline is a short sequence of machine instructions which puts
 
13
   the constant <data> into <variable>, then jumps to <address>. The only
 
14
   registers that are allowed to be modified are call-used-registers. No
 
15
   stack manipulations are allowed since the trampoline has to pass its
 
16
   arguments along to the function at <address>.
 
17
 
 
18
   1. To find out which instructions are available for "move"/"store" and
 
19
      "jump", compile proto.c for your CPU:
 
20
 
 
21
          make -f Makefile.devel proto-${CPU}.s
 
22
      or
 
23
          gcc -O2 -fomit-frame-pointer -S proto.c -o proto-${CPU}.s
 
24
 
 
25
   2. Write down the instructions for the trampoline in a file tramp-${CPU}.s,
 
26
      using constants for <data>, <variable>, <address>. Assemble it:
 
27
 
 
28
          gcc -c tramp-${CPU}.s
 
29
 
 
30
      Verify that the jump actually goes to <address>. (Beware: Some CPUs have
 
31
      program-counter relative jumps.)
 
32
 
 
33
          gdb tramp-${CPU}.o
 
34
          disassemble tramp
 
35
 
 
36
   3. Take a hex dump of tramp-${CPU}.o
 
37
 
 
38
          hexdump -e '"%06.6_ax " 16/1 " %02X" "\n"' < tramp-${CPU}.o
 
39
      or
 
40
          od -tx1 -Ax < tramp-${CPU}.o
 
41
      or
 
42
          od -x +x < tramp-${CPU}.o
 
43
 
 
44
      Look out for the magic numbers you used for <data>, <variable> and
 
45
      <address>.
 
46
 
 
47
   4. Write the code which builds up a trampoline in memory, in trampoline.c.
 
48
 
 
49
   5. Try it:
 
50
 
 
51
          make
 
52
          make check1
 
53
 
 
54
   6. Write the is_tramp() macro and the tramp_xxx() accessor macros
 
55
      in trampoline.c.
 
56
 
 
57
   7. Try it:
 
58
 
 
59
          make
 
60
          make check
 
61
 
 
62
 
 
63
B. a new OS - how to make code in malloc'ed memory executable?
 
64
 
 
65
   `configure' will find out whether code stored in malloc'ed memory is
 
66
   executable, or whether virtual memory protections have to be set in order
 
67
   to allow this. (The test is pretty simple: it copies a small function
 
68
   to malloc'ed memory and tries to executed it. The test could also fail
 
69
   because the compiler produced non-position-independent code or because
 
70
   of alignment issues.)
 
71
 
 
72
   To set virtual memory protections on a page of memory, your system should
 
73
   provide the mprotect() and getpagesize() functions. If it does not, find
 
74
   a substitute.
 
75
 
 
76
 
 
77
C. a new CPU or OS - how to flush the instruction cache?
 
78
 
 
79
   CPUs which have separate data and instruction caches need to flush
 
80
   (part of) the instruction cache when alloc_trampoline() is called.
 
81
   (There may have been an old trampoline at the same location, and the
 
82
   instruction cache is not updated when the new trampoline is built.
 
83
   The effect can be that when the new trampoline is called, the old one
 
84
   will still be executed.)
 
85
 
 
86
   To flush the instruction cache, some CPUs have special instruction which
 
87
   can be put into gcc "asm" statements. On some CPUs these instructions are
 
88
   privileged, you therefore need to call some system or library function.
 
89
   On other CPUs, the only way to flush the instruction cache is to execute
 
90
   a long sequence of "nop" or "jump" instructions. This is hairy.
 
91
 
 
92
 
 
93
When you are done with porting to a new platform, or even if TRAMPOLINE
 
94
passes the "make check" out of the box without modifications, please report
 
95
your results to the author of TRAMPOLINE, for inclusion in the next release.