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