Search
lxdream.org :: lxdream/src/sh4/ia64abi.h
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/ia64abi.h
changeset 953:f4a156508ad1
prev927:17b6b9e245d8
next957:0f6131f6cc3a
author nkeynes
date Tue Jan 13 11:56:28 2009 +0000 (11 years ago)
permissions -rw-r--r--
last change Merge lxdream-mem branch back to trunk
view annotate diff log raw
     1 /**
     2  * $Id$
     3  * 
     4  * Provides the implementation for the AMD64 ABI (eg prologue, epilogue, and
     5  * calling conventions)
     6  *
     7  * Copyright (c) 2007 Nathan Keynes.
     8  *
     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.
    13  *
    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.
    18  */
    20 #ifndef lxdream_ia64abi_H
    21 #define lxdream_ia64abi_H 1
    23 #include <unwind.h>
    25 #define load_ptr( reg, ptr ) load_imm64( reg, (uint64_t)ptr );
    27 static inline decode_address( int addr_reg )
    28 {
    29     uintptr_t base = (sh4r.xlat_sh4_mode&SR_MD) ? (uintptr_t)sh4_address_space : (uintptr_t)sh4_user_address_space;
    30     MOV_r32_r32( addr_reg, R_ECX ); 
    31     SHR_imm8_r32( 12, R_ECX ); 
    32     load_ptr( R_EDI, base );
    33     REXW(); OP(0x8B); OP(0x0C); OP(0xCF);   // mov.q [%rdi + %rcx*8], %rcx
    34 }
    36 /**
    37  * Note: clobbers EAX to make the indirect call - this isn't usually
    38  * a problem since the callee will usually clobber it anyway.
    39  * Size: 12 bytes
    40  */
    41 #define CALL_FUNC0_SIZE 12
    42 static inline void call_func0( void *ptr )
    43 {
    44     load_imm64(R_EAX, (uint64_t)ptr);
    45     CALL_r32(R_EAX);
    46 }
    48 #define CALL_FUNC1_SIZE 14
    49 static inline void call_func1( void *ptr, int arg1 )
    50 {
    51     REXW(); MOV_r32_r32(arg1, R_EDI);
    52     call_func0(ptr);
    53 }
    55 static inline void call_func1_exc( void *ptr, int arg1, int pc )
    56 {
    57     REXW(); MOV_r32_r32(arg1, R_EDI);
    58     load_exc_backpatch(R_ESI);
    59     call_func0(ptr);
    60 }
    62 static inline void call_func1_r32disp8( int preg, uint32_t disp8, int arg1 )
    63 {
    64     REXW(); MOV_r32_r32(arg1, R_EDI);
    65     CALL_r32disp8(preg, disp8);    
    66 }
    68 static inline void call_func1_r32disp8_exc( int preg, uint32_t disp8, int arg1, int pc )
    69 {
    70     REXW(); MOV_r32_r32(arg1, R_EDI);
    71     load_exc_backpatch(R_ESI);
    72     CALL_r32disp8(preg, disp8);
    73 }
    75 #define CALL_FUNC2_SIZE 16
    76 static inline void call_func2( void *ptr, int arg1, int arg2 )
    77 {
    78     REXW(); MOV_r32_r32(arg1, R_EDI);
    79     REXW(); MOV_r32_r32(arg2, R_ESI);
    80     call_func0(ptr);
    81 }
    83 static inline void call_func2_r32disp8( int preg, uint32_t disp8, int arg1, int arg2 )
    84 {
    85     REXW(); MOV_r32_r32(arg1, R_EDI);
    86     REXW(); MOV_r32_r32(arg2, R_ESI);
    87     CALL_r32disp8(preg, disp8);    
    88 }
    90 static inline void call_func2_r32disp8_exc( int preg, uint32_t disp8, int arg1, int arg2, int pc )
    91 {
    92     REXW(); MOV_r32_r32(arg1, R_EDI);
    93     REXW(); MOV_r32_r32(arg2, R_ESI);
    94     load_exc_backpatch(R_EDX);
    95     CALL_r32disp8(preg, disp8);
    96 }
   100 /**
   101  * Emit the 'start of block' assembly. Sets up the stack frame and save
   102  * SI/DI as required
   103  */
   104 void enter_block( ) 
   105 {
   106     PUSH_r32(R_EBP);
   107     load_ptr( R_EBP, ((uint8_t *)&sh4r) + 128 );
   108     // Minimum aligned allocation is 16 bytes
   109     REXW(); SUB_imm8s_r32( 16, R_ESP );
   110 }
   112 static inline void exit_block( )
   113 {
   114     REXW(); ADD_imm8s_r32( 16, R_ESP );
   115     POP_r32(R_EBP);
   116     RET();
   117 }
   119 /**
   120  * Exit the block with sh4r.pc already written
   121  */
   122 void exit_block_pcset( sh4addr_t pc )
   123 {
   124     load_imm32( R_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
   125     ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );    // 6
   126     load_spreg( R_EAX, R_PC );
   127     if( sh4_x86.tlb_on ) {
   128         call_func1(xlat_get_code_by_vma,R_EAX);
   129     } else {
   130         call_func1(xlat_get_code,R_EAX);
   131     }
   132     exit_block();
   133 }
   135 /**
   136  * Exit the block with sh4r.new_pc written with the target address
   137  */
   138 void exit_block_newpcset( sh4addr_t pc )
   139 {
   140     load_imm32( R_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
   141     ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );    // 6
   142     load_spreg( R_EAX, R_NEW_PC );
   143     store_spreg( R_EAX, R_PC );
   144     if( sh4_x86.tlb_on ) {
   145         call_func1(xlat_get_code_by_vma,R_EAX);
   146     } else {
   147         call_func1(xlat_get_code,R_EAX);
   148     }
   149     exit_block();
   150 }
   152 #define EXIT_BLOCK_SIZE(pc) (25 + (IS_IN_ICACHE(pc)?10:CALL_FUNC1_SIZE))
   153 /**
   154  * Exit the block to an absolute PC
   155  */
   156 void exit_block_abs( sh4addr_t pc, sh4addr_t endpc )
   157 {
   158     load_imm32( R_ECX, pc );                            // 5
   159     store_spreg( R_ECX, REG_OFFSET(pc) );               // 3
   160     if( IS_IN_ICACHE(pc) ) {
   161         REXW(); MOV_moff32_EAX( xlat_get_lut_entry(pc) );
   162         REXW(); AND_imm8s_r32( 0xFC, R_EAX ); // 4
   163     } else if( sh4_x86.tlb_on ) {
   164         call_func1(xlat_get_code_by_vma, R_ECX);
   165     } else {
   166         call_func1(xlat_get_code,R_ECX);
   167     }
   168     load_imm32( R_ECX, ((endpc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
   169     ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );     // 6
   170     exit_block();
   171 }
   174 #define EXIT_BLOCK_REL_SIZE(pc)  (28 + (IS_IN_ICACHE(pc)?10:CALL_FUNC1_SIZE))
   176 /**
   177  * Exit the block to a relative PC
   178  */
   179 void exit_block_rel( sh4addr_t pc, sh4addr_t endpc )
   180 {
   181     load_imm32( R_ECX, pc - sh4_x86.block_start_pc );   // 5
   182     ADD_sh4r_r32( R_PC, R_ECX );
   183     store_spreg( R_ECX, REG_OFFSET(pc) );               // 3
   184     if( IS_IN_ICACHE(pc) ) {
   185         REXW(); MOV_moff32_EAX( xlat_get_lut_entry(GET_ICACHE_PHYS(pc)) ); // 5
   186         REXW(); AND_imm8s_r32( 0xFC, R_EAX ); // 4
   187     } else if( sh4_x86.tlb_on ) {
   188         call_func1(xlat_get_code_by_vma,R_ECX);
   189     } else {
   190         call_func1(xlat_get_code,R_ECX);
   191     }
   192     load_imm32( R_ECX, ((endpc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
   193     ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );     // 6
   194     exit_block();
   195 }
   197 /**
   198  * Write the block trailer (exception handling block)
   199  */
   200 void sh4_translate_end_block( sh4addr_t pc ) {
   201     if( sh4_x86.branch_taken == FALSE ) {
   202         // Didn't exit unconditionally already, so write the termination here
   203         exit_block_rel( pc, pc );
   204     }
   205     if( sh4_x86.backpatch_posn != 0 ) {
   206         unsigned int i;
   207         // Raise exception
   208         uint8_t *end_ptr = xlat_output;
   209         MOV_r32_r32( R_EDX, R_ECX );
   210         ADD_r32_r32( R_EDX, R_ECX );
   211         ADD_r32_sh4r( R_ECX, R_PC );
   212         MOV_moff32_EAX( &sh4_cpu_period );
   213         MUL_r32( R_EDX );
   214         ADD_r32_sh4r( R_EAX, REG_OFFSET(slice_cycle) );
   216         call_func0( sh4_raise_exception );
   217         load_spreg( R_EAX, R_PC );
   218         if( sh4_x86.tlb_on ) {
   219             call_func1(xlat_get_code_by_vma,R_EAX);
   220         } else {
   221             call_func1(xlat_get_code,R_EAX);
   222         }
   223         exit_block();
   225         // Exception already raised - just cleanup
   226         uint8_t *preexc_ptr = xlat_output;
   227         MOV_r32_r32( R_EDX, R_ECX );
   228         ADD_r32_r32( R_EDX, R_ECX );
   229         ADD_r32_sh4r( R_ECX, R_SPC );
   230         MOV_moff32_EAX( &sh4_cpu_period );
   231         MUL_r32( R_EDX );
   232         ADD_r32_sh4r( R_EAX, REG_OFFSET(slice_cycle) );
   233         load_spreg( R_EDI, R_PC );
   234         if( sh4_x86.tlb_on ) {
   235             call_func0(xlat_get_code_by_vma);
   236         } else {
   237             call_func0(xlat_get_code);
   238         }
   239         exit_block();
   241         for( i=0; i< sh4_x86.backpatch_posn; i++ ) {
   242             uint32_t *fixup_addr = (uint32_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset];
   243             if( sh4_x86.backpatch_list[i].exc_code < 0 ) {
   244                 if( sh4_x86.backpatch_list[i].exc_code == -2 ) {
   245                     *((uintptr_t *)fixup_addr) = (uintptr_t)xlat_output; 
   246                 } else {
   247                     *fixup_addr = xlat_output - (uint8_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset] - 4;
   248                 }
   249                 load_imm32( R_EDX, sh4_x86.backpatch_list[i].fixup_icount );
   250                 int rel = preexc_ptr - xlat_output;
   251                 JMP_rel(rel);
   252             } else {
   253                 *fixup_addr = xlat_output - (uint8_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset] - 4;
   254                 load_imm32( R_EDI, sh4_x86.backpatch_list[i].exc_code );
   255                 load_imm32( R_EDX, sh4_x86.backpatch_list[i].fixup_icount );
   256                 int rel = end_ptr - xlat_output;
   257                 JMP_rel(rel);
   258             }
   259         }
   260     }
   261 }
   263 struct UnwindInfo {
   264     uintptr_t block_start;
   265     uintptr_t block_end;
   266     void *pc;
   267 };
   269 _Unwind_Reason_Code xlat_check_frame( struct _Unwind_Context *context, void *arg )
   270 {
   271     struct UnwindInfo *info = arg;
   272     void *pc = (void *)_Unwind_GetIP(context);
   273     if( ((uintptr_t)pc) >= info->block_start && ((uintptr_t)pc) < info->block_end ) {
   274         info->pc = pc;
   275         return _URC_NORMAL_STOP;
   276     }
   278     return _URC_NO_REASON;
   279 }
   281 void *xlat_get_native_pc( void *code, uint32_t code_size )
   282 {
   283     struct _Unwind_Exception exc;
   284     struct UnwindInfo info;
   286     info.pc = NULL;
   287     info.block_start = (uintptr_t)code;
   288     info.block_end = info.block_start + code_size;
   289     void *result = NULL;
   290     _Unwind_Backtrace( xlat_check_frame, &info );
   291     return info.pc;
   292 }
   294 #endif /* !lxdream_ia64abi_H */
.