blob: 54a6a76d6a8307b8c23b053a3df2ccb529e45f8b [file] [log] [blame]
njna7598f62005-06-18 03:27:58 +00001
sewardjb9bce632005-06-21 01:41:34 +00002/*--------------------------------------------------------------------*/
3/*--- Trampoline code page stuff. m_trampoline.S ---*/
4/*--------------------------------------------------------------------*/
sewardjde4a1d02002-03-22 01:27:54 +00005
6/*
njnb9c427c2004-12-01 14:14:42 +00007 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
sewardjde4a1d02002-03-22 01:27:54 +00009
njn53612422005-03-12 16:22:54 +000010 Copyright (C) 2000-2005 Julian Seward
sewardjde4a1d02002-03-22 01:27:54 +000011 jseward@acm.org
sewardjde4a1d02002-03-22 01:27:54 +000012
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
27
njn25e49d8e72002-09-23 09:36:25 +000028 The GNU General Public License is contained in the file COPYING.
sewardjde4a1d02002-03-22 01:27:54 +000029*/
30
sewardj45f4e7c2005-09-27 19:20:21 +000031#include "pub_core_basics_asm.h"
sewardjb5f6f512005-03-10 23:59:00 +000032#include "vki_unistd.h"
sewardjde4a1d02002-03-22 01:27:54 +000033
sewardj54cacf02002-04-12 23:24:59 +000034/* ------------------ SIMULATED CPU HELPERS ------------------ */
sewardjb9bce632005-06-21 01:41:34 +000035/*
36 Replacements for some functions to do with vsyscalls and signals.
37 This code runs on the simulated CPU.
sewardj54cacf02002-04-12 23:24:59 +000038*/
sewardja48a4932005-09-29 11:09:56 +000039
40/*---------------------- x86-linux ----------------------*/
41#if defined(VGP_x86_linux)
sewardjb9bce632005-06-21 01:41:34 +000042
sewardj45f4e7c2005-09-27 19:20:21 +000043# define UD2_16 ud2 ; ud2 ; ud2 ; ud2 ;ud2 ; ud2 ; ud2 ; ud2
44# define UD2_64 UD2_16 ; UD2_16 ; UD2_16 ; UD2_16
45# define UD2_256 UD2_64 ; UD2_64 ; UD2_64 ; UD2_64
46# define UD2_1024 UD2_256 ; UD2_256 ; UD2_256 ; UD2_256
47# define UD2_PAGE UD2_1024 ; UD2_1024 ; UD2_1024 ; UD2_1024
48
49 /* a leading page of unexecutable code */
50 UD2_PAGE
sewardjb9bce632005-06-21 01:41:34 +000051
52.global VG_(trampoline_stuff_start)
53VG_(trampoline_stuff_start):
54
55.global VG_(x86_linux_SUBST_FOR_sigreturn)
56VG_(x86_linux_SUBST_FOR_sigreturn):
sewardjb5f6f512005-03-10 23:59:00 +000057 /* This is a very specific sequence which GDB uses to
58 recognize signal handler frames. */
59 popl %eax
60 movl $__NR_sigreturn, %eax
61 int $0x80
62 ud2
63
sewardjb9bce632005-06-21 01:41:34 +000064.global VG_(x86_linux_SUBST_FOR_rt_sigreturn)
65VG_(x86_linux_SUBST_FOR_rt_sigreturn):
sewardjb5f6f512005-03-10 23:59:00 +000066 /* Likewise for rt signal frames */
67 movl $__NR_rt_sigreturn, %eax
68 int $0x80
69 ud2
fitzhardinge98abfc72003-12-16 02:05:15 +000070
sewardjb9bce632005-06-21 01:41:34 +000071.global VG_(x86_linux_REDIR_FOR__dl_sysinfo_int80)
72VG_(x86_linux_REDIR_FOR__dl_sysinfo_int80):
73 /* We can point our sysinfo stuff here */
fitzhardinge92360792003-12-24 10:11:11 +000074 int $0x80
75 ret
sewardjb9bce632005-06-21 01:41:34 +000076
77.global VG_(trampoline_stuff_end)
78VG_(trampoline_stuff_end):
njna7598f62005-06-18 03:27:58 +000079
sewardja48a4932005-09-29 11:09:56 +000080 /* and a trailing page of unexecutable code */
81 UD2_PAGE
82
83# undef UD2_16
84# undef UD2_64
85# undef UD2_256
86# undef UD2_1024
87# undef UD2_PAGE
sewardjb9bce632005-06-21 01:41:34 +000088
sewardja48a4932005-09-29 11:09:56 +000089/*---------------------- amd64-linux ----------------------*/
sewardjb9bce632005-06-21 01:41:34 +000090#else
91#if defined(VGP_amd64_linux)
njna7598f62005-06-18 03:27:58 +000092
sewardja48a4932005-09-29 11:09:56 +000093# define UD2_16 ud2 ; ud2 ; ud2 ; ud2 ;ud2 ; ud2 ; ud2 ; ud2
94# define UD2_64 UD2_16 ; UD2_16 ; UD2_16 ; UD2_16
95# define UD2_256 UD2_64 ; UD2_64 ; UD2_64 ; UD2_64
96# define UD2_1024 UD2_256 ; UD2_256 ; UD2_256 ; UD2_256
97# define UD2_PAGE UD2_1024 ; UD2_1024 ; UD2_1024 ; UD2_1024
98
99 /* a leading page of unexecutable code */
100 UD2_PAGE
101
sewardjb9bce632005-06-21 01:41:34 +0000102.global VG_(trampoline_stuff_start)
103VG_(trampoline_stuff_start):
104
105.global VG_(amd64_linux_SUBST_FOR_rt_sigreturn)
106VG_(amd64_linux_SUBST_FOR_rt_sigreturn):
107 /* This is a very specific sequence which GDB uses to
108 recognize signal handler frames. */
njna7598f62005-06-18 03:27:58 +0000109 movq $__NR_rt_sigreturn, %rax
110 syscall
111 ud2
112
sewardjb9bce632005-06-21 01:41:34 +0000113.global VG_(amd64_linux_REDIR_FOR_vgettimeofday)
114VG_(amd64_linux_REDIR_FOR_vgettimeofday):
njna7598f62005-06-18 03:27:58 +0000115 movq $__NR_gettimeofday, %rax
116 syscall
117 ret
118
sewardjb9bce632005-06-21 01:41:34 +0000119.global VG_(amd64_linux_REDIR_FOR_vtime)
120VG_(amd64_linux_REDIR_FOR_vtime):
njna7598f62005-06-18 03:27:58 +0000121 movq $__NR_time, %rax
122 syscall
123 ret
124
sewardjb9bce632005-06-21 01:41:34 +0000125.global VG_(trampoline_stuff_end)
126VG_(trampoline_stuff_end):
njna7598f62005-06-18 03:27:58 +0000127
sewardja48a4932005-09-29 11:09:56 +0000128 /* and a trailing page of unexecutable code */
129 UD2_PAGE
130
131# undef UD2_16
132# undef UD2_64
133# undef UD2_256
134# undef UD2_1024
135# undef UD2_PAGE
sewardjb9bce632005-06-21 01:41:34 +0000136
sewardj05b5fc12005-06-21 09:56:56 +0000137/*---------------- ppc32-linux ----------------*/
sewardjb9bce632005-06-21 01:41:34 +0000138#else
139#if defined(VGP_ppc32_linux)
140
sewardja48a4932005-09-29 11:09:56 +0000141# define UD2_16 trap ; trap ; trap; trap
142# define UD2_64 UD2_16 ; UD2_16 ; UD2_16 ; UD2_16
143# define UD2_256 UD2_64 ; UD2_64 ; UD2_64 ; UD2_64
144# define UD2_1024 UD2_256 ; UD2_256 ; UD2_256 ; UD2_256
145# define UD2_PAGE UD2_1024 ; UD2_1024 ; UD2_1024 ; UD2_1024
146
147 /* a leading page of unexecutable code */
148 UD2_PAGE
149
sewardj05b5fc12005-06-21 09:56:56 +0000150.global VG_(trampoline_stuff_start)
151VG_(trampoline_stuff_start):
cerion85665ca2005-06-20 15:51:07 +0000152
sewardjad50be32005-08-18 11:54:30 +0000153/* There's no particular reason that this needs to be handwritten
154 assembly, but since that's what this file contains, here's a
155 simple strlen implementation (written in C and compiled by gcc.)
156*/
157.global VG_(ppc32_linux_REDIR_FOR_strlen)
sewardj0b6941e2005-10-12 10:00:56 +0000158.type VG_(ppc32_linux_REDIR_FOR_strlen), @function
sewardjad50be32005-08-18 11:54:30 +0000159VG_(ppc32_linux_REDIR_FOR_strlen):
160 lbz 4,0(3)
161 li 9,0
162 cmpwi 0,4,0
sewardj5e7b2302005-10-01 19:12:08 +0000163 beq- 0,.L18
164.L19:
sewardjad50be32005-08-18 11:54:30 +0000165 lbzu 5,1(3)
166 addi 9,9,1
167 cmpwi 0,5,0
sewardj5e7b2302005-10-01 19:12:08 +0000168 bne+ 0,.L19
169.L18:
sewardjad50be32005-08-18 11:54:30 +0000170 mr 3,9
171 blr
sewardj0b6941e2005-10-12 10:00:56 +0000172.size VG_(ppc32_linux_REDIR_FOR_strlen), .-VG_(ppc32_linux_REDIR_FOR_strlen)
sewardjad50be32005-08-18 11:54:30 +0000173
sewardj5e7b2302005-10-01 19:12:08 +0000174/* Ditto strcmp */
175.global VG_(ppc32_linux_REDIR_FOR_strcmp)
sewardj0b6941e2005-10-12 10:00:56 +0000176.type VG_(ppc32_linux_REDIR_FOR_strcmp), @function
sewardj5e7b2302005-10-01 19:12:08 +0000177VG_(ppc32_linux_REDIR_FOR_strcmp):
178.L20:
179 lbz 0,0(3)
180 cmpwi 7,0,0
181 bne- 7,.L21
182 lbz 0,0(4)
183 li 11,0
184 cmpwi 7,0,0
185 beq- 7,.L22
186.L21:
187 lbz 0,0(3)
188 li 11,-1
189 cmpwi 7,0,0
190 beq- 7,.L22
191 lbz 0,0(4)
192 li 11,1
193 cmpwi 7,0,0
194 beq- 7,.L22
195 lbz 9,0(3)
196 lbz 0,0(4)
197 li 11,-1
198 cmplw 7,9,0
199 blt- 7,.L22
200 lbz 9,0(3)
201 lbz 0,0(4)
202 li 11,1
203 addi 3,3,1
204 addi 4,4,1
205 cmplw 7,9,0
206 ble+ 7,.L20
207.L22:
208 mr 3,11
209 blr
sewardj0b6941e2005-10-12 10:00:56 +0000210.size VG_(ppc32_linux_REDIR_FOR_strcmp), .-VG_(ppc32_linux_REDIR_FOR_strcmp)
sewardj5e7b2302005-10-01 19:12:08 +0000211
sewardj31d83422005-10-15 02:00:41 +0000212/* Ditto index/strchr */
213.global VG_(ppc32_linux_REDIR_FOR_strchr)
214.type VG_(ppc32_linux_REDIR_FOR_strchr), @function
215VG_(ppc32_linux_REDIR_FOR_strchr):
216 lbz 0,0(3)
217 cmpw 7,4,0
218 beqlr 7
219 cmpwi 7,0,0
220 beq 7,.L307
221 .p2align 4,,15
222.L310:
223 lbzu 0,1(3)
224 cmpw 7,0,4
225 cmpwi 6,0,0
226 beq 7,.L313
227 bne 6,.L310
228.L307:
229 li 3,0
230 blr
231.L313:
232 blr
233.size VG_(ppc32_linux_REDIR_FOR_strchr),.-VG_(ppc32_linux_REDIR_FOR_strchr)
234
sewardj05b5fc12005-06-21 09:56:56 +0000235.global VG_(trampoline_stuff_end)
236VG_(trampoline_stuff_end):
237
sewardja48a4932005-09-29 11:09:56 +0000238# undef UD2_16
239# undef UD2_64
240# undef UD2_256
241# undef UD2_1024
242# undef UD2_PAGE
sewardj05b5fc12005-06-21 09:56:56 +0000243
sewardj05b5fc12005-06-21 09:56:56 +0000244/*---------------- unknown ----------------*/
njna7598f62005-06-18 03:27:58 +0000245#else
sewardjb9bce632005-06-21 01:41:34 +0000246# error Unknown platform
247
njna7598f62005-06-18 03:27:58 +0000248#endif
sewardjb9bce632005-06-21 01:41:34 +0000249#endif
250#endif
251
sewardjde4a1d02002-03-22 01:27:54 +0000252
thughes4ad52d02004-06-27 17:37:21 +0000253/* Let the linker know we don't need an executable stack */
254.section .note.GNU-stack,"",@progbits
255
sewardjb9bce632005-06-21 01:41:34 +0000256/*--------------------------------------------------------------------*/
257/*--- end ---*/
258/*--------------------------------------------------------------------*/