4 * Provides the implementation for the AMD64 ABI (eg prologue, epilogue, and
7 * Copyright (c) 2007 Nathan Keynes.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #ifndef lxdream_ia64abi_H
21 #define lxdream_ia64abi_H 1
25 #define load_ptr( reg, ptr ) load_imm64( reg, (uint64_t)ptr );
28 * Note: clobbers EAX to make the indirect call - this isn't usually
29 * a problem since the callee will usually clobber it anyway.
32 #define CALL_FUNC0_SIZE 12
33 static inline void call_func0( void *ptr )
35 load_imm64(R_EAX, (uint64_t)ptr);
39 #define CALL_FUNC1_SIZE 14
40 static inline void call_func1( void *ptr, int arg1 )
42 REXW(); MOV_r32_r32(arg1, R_EDI);
46 #define CALL_FUNC2_SIZE 16
47 static inline void call_func2( void *ptr, int arg1, int arg2 )
49 REXW(); MOV_r32_r32(arg1, R_EDI);
50 REXW(); MOV_r32_r32(arg2, R_ESI);
54 #define MEM_WRITE_DOUBLE_SIZE 35
56 * Write a double (64-bit) value into memory, with the first word in arg2a, and
59 static inline void MEM_WRITE_DOUBLE( int addr, int arg2a, int arg2b )
63 call_func2(sh4_write_long, addr, arg2a);
66 ADD_imm8s_r32(4, R_EDI);
67 call_func0(sh4_write_long);
70 #define MEM_READ_DOUBLE_SIZE 43
72 * Read a double (64-bit) value from memory, writing the first word into arg2a
73 * and the second into arg2b. The addr must not be in EAX
75 static inline void MEM_READ_DOUBLE( int addr, int arg2a, int arg2b )
77 REXW(); SUB_imm8s_r32( 8, R_ESP );
79 call_func1(sh4_read_long, addr);
82 ADD_imm8s_r32(4, R_EDI);
83 call_func0(sh4_read_long);
84 MOV_r32_r32(R_EAX, arg2b);
86 REXW(); ADD_imm8s_r32( 8, R_ESP );
91 * Emit the 'start of block' assembly. Sets up the stack frame and save
98 load_ptr( R_EBP, ((uint8_t *)&sh4r) + 128 );
102 * Exit the block with sh4r.pc already written
104 void exit_block_pcset( sh4addr_t pc )
106 load_imm32( R_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
107 ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) ); // 6
108 load_spreg( R_EAX, R_PC );
109 if( sh4_x86.tlb_on ) {
110 call_func1(xlat_get_code_by_vma,R_EAX);
112 call_func1(xlat_get_code,R_EAX);
119 * Exit the block with sh4r.new_pc written with the target address
121 void exit_block_newpcset( sh4addr_t pc )
123 load_imm32( R_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
124 ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) ); // 6
125 load_spreg( R_EAX, R_NEW_PC );
126 store_spreg( R_EAX, R_PC );
127 if( sh4_x86.tlb_on ) {
128 call_func1(xlat_get_code_by_vma,R_EAX);
130 call_func1(xlat_get_code,R_EAX);
136 #define EXIT_BLOCK_SIZE(pc) (25 + (IS_IN_ICACHE(pc)?10:CALL_FUNC1_SIZE))
138 * Exit the block to an absolute PC
140 void exit_block( sh4addr_t pc, sh4addr_t endpc )
142 load_imm32( R_ECX, pc ); // 5
143 store_spreg( R_ECX, REG_OFFSET(pc) ); // 3
144 if( IS_IN_ICACHE(pc) ) {
145 REXW(); MOV_moff32_EAX( xlat_get_lut_entry(pc) );
146 } else if( sh4_x86.tlb_on ) {
147 call_func1(xlat_get_code_by_vma, R_ECX);
149 call_func1(xlat_get_code,R_ECX);
151 REXW(); AND_imm8s_r32( 0xFC, R_EAX ); // 4
152 load_imm32( R_ECX, ((endpc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
153 ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) ); // 6
159 #define EXIT_BLOCK_REL_SIZE(pc) (28 + (IS_IN_ICACHE(pc)?10:CALL_FUNC1_SIZE))
162 * Exit the block to a relative PC
164 void exit_block_rel( sh4addr_t pc, sh4addr_t endpc )
166 load_imm32( R_ECX, pc - sh4_x86.block_start_pc ); // 5
167 ADD_sh4r_r32( R_PC, R_ECX );
168 store_spreg( R_ECX, REG_OFFSET(pc) ); // 3
169 if( IS_IN_ICACHE(pc) ) {
170 REXW(); MOV_moff32_EAX( xlat_get_lut_entry(GET_ICACHE_PHYS(pc)) ); // 5
171 } else if( sh4_x86.tlb_on ) {
172 call_func1(xlat_get_code_by_vma,R_ECX);
174 call_func1(xlat_get_code,R_ECX);
176 REXW(); AND_imm8s_r32( 0xFC, R_EAX ); // 4
177 load_imm32( R_ECX, ((endpc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
178 ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) ); // 6
184 * Write the block trailer (exception handling block)
186 void sh4_translate_end_block( sh4addr_t pc ) {
187 if( sh4_x86.branch_taken == FALSE ) {
188 // Didn't exit unconditionally already, so write the termination here
189 exit_block_rel( pc, pc );
191 if( sh4_x86.backpatch_posn != 0 ) {
194 uint8_t *end_ptr = xlat_output;
195 MOV_r32_r32( R_EDX, R_ECX );
196 ADD_r32_r32( R_EDX, R_ECX );
197 ADD_r32_sh4r( R_ECX, R_PC );
198 MOV_moff32_EAX( &sh4_cpu_period );
200 ADD_r32_sh4r( R_EAX, REG_OFFSET(slice_cycle) );
202 call_func0( sh4_raise_exception );
203 load_spreg( R_EAX, R_PC );
204 if( sh4_x86.tlb_on ) {
205 call_func1(xlat_get_code_by_vma,R_EAX);
207 call_func1(xlat_get_code,R_EAX);
212 // Exception already raised - just cleanup
213 uint8_t *preexc_ptr = xlat_output;
214 MOV_r32_r32( R_EDX, R_ECX );
215 ADD_r32_r32( R_EDX, R_ECX );
216 ADD_r32_sh4r( R_ECX, R_SPC );
217 MOV_moff32_EAX( &sh4_cpu_period );
219 ADD_r32_sh4r( R_EAX, REG_OFFSET(slice_cycle) );
220 load_spreg( R_EDI, R_PC );
221 if( sh4_x86.tlb_on ) {
222 call_func0(xlat_get_code_by_vma);
224 call_func0(xlat_get_code);
229 for( i=0; i< sh4_x86.backpatch_posn; i++ ) {
230 uint32_t *fixup_addr = (uint32_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset];
231 *fixup_addr = xlat_output - (uint8_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset] - 4;
232 if( sh4_x86.backpatch_list[i].exc_code < 0 ) {
233 load_imm32( R_EDX, sh4_x86.backpatch_list[i].fixup_icount );
234 int stack_adj = -1 - sh4_x86.backpatch_list[i].exc_code;
235 if( stack_adj > 0 ) {
236 ADD_imm8s_r32( stack_adj*4, R_ESP );
238 int rel = preexc_ptr - xlat_output;
241 load_imm32( R_EDI, sh4_x86.backpatch_list[i].exc_code );
242 load_imm32( R_EDX, sh4_x86.backpatch_list[i].fixup_icount );
243 int rel = end_ptr - xlat_output;
250 _Unwind_Reason_Code xlat_check_frame( struct _Unwind_Context *context, void *arg )
252 void *rbp = (void *)_Unwind_GetGR(context, 6);
253 void *expect = (((uint8_t *)&sh4r) + 128 );
254 if( rbp == expect ) {
255 void **result = (void **)arg;
256 *result = (void *)_Unwind_GetIP(context);
257 return _URC_NORMAL_STOP;
260 return _URC_NO_REASON;
263 void *xlat_get_native_pc()
265 struct _Unwind_Exception exc;
268 _Unwind_Backtrace( xlat_check_frame, &result );
272 #endif /* !lxdream_ia64abi_H */
.