Search
lxdream.org :: lxdream/src/sh4/ia64abi.h
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/ia64abi.h
changeset 927:17b6b9e245d8
prev926:68f3e0fe02f1
next930:07e5b11419db
next953:f4a156508ad1
author nkeynes
date Mon Dec 15 10:44:56 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Add return-address-modifying exception return code to mmu TLB lookups (a little bit faster)
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@539
    27
/**
nkeynes@539
    28
 * Note: clobbers EAX to make the indirect call - this isn't usually
nkeynes@539
    29
 * a problem since the callee will usually clobber it anyway.
nkeynes@539
    30
 * Size: 12 bytes
nkeynes@539
    31
 */
nkeynes@539
    32
#define CALL_FUNC0_SIZE 12
nkeynes@539
    33
static inline void call_func0( void *ptr )
nkeynes@539
    34
{
nkeynes@539
    35
    load_imm64(R_EAX, (uint64_t)ptr);
nkeynes@539
    36
    CALL_r32(R_EAX);
nkeynes@539
    37
}
nkeynes@539
    38
nkeynes@539
    39
#define CALL_FUNC1_SIZE 14
nkeynes@539
    40
static inline void call_func1( void *ptr, int arg1 )
nkeynes@539
    41
{
nkeynes@800
    42
    REXW(); MOV_r32_r32(arg1, R_EDI);
nkeynes@539
    43
    call_func0(ptr);
nkeynes@539
    44
}
nkeynes@539
    45
nkeynes@927
    46
static inline void call_func1_exc( void *ptr, int arg1, int pc )
nkeynes@927
    47
{
nkeynes@927
    48
    REXW(); MOV_r32_r32(arg1, R_EDI);
nkeynes@927
    49
    load_exc_backpatch(R_ESI);
nkeynes@927
    50
    call_func0(ptr);
nkeynes@927
    51
}
nkeynes@927
    52
nkeynes@539
    53
#define CALL_FUNC2_SIZE 16
nkeynes@539
    54
static inline void call_func2( void *ptr, int arg1, int arg2 )
nkeynes@539
    55
{
nkeynes@800
    56
    REXW(); MOV_r32_r32(arg1, R_EDI);
nkeynes@800
    57
    REXW(); MOV_r32_r32(arg2, R_ESI);
nkeynes@539
    58
    call_func0(ptr);
nkeynes@539
    59
}
nkeynes@539
    60
nkeynes@586
    61
#define MEM_WRITE_DOUBLE_SIZE 35
nkeynes@539
    62
/**
nkeynes@539
    63
 * Write a double (64-bit) value into memory, with the first word in arg2a, and
nkeynes@539
    64
 * the second in arg2b
nkeynes@539
    65
 */
nkeynes@539
    66
static inline void MEM_WRITE_DOUBLE( int addr, int arg2a, int arg2b )
nkeynes@539
    67
{
nkeynes@539
    68
    PUSH_r32(arg2b);
nkeynes@539
    69
    PUSH_r32(addr);
nkeynes@539
    70
    call_func2(sh4_write_long, addr, arg2a);
nkeynes@586
    71
    POP_r32(R_EDI);
nkeynes@586
    72
    POP_r32(R_ESI);
nkeynes@586
    73
    ADD_imm8s_r32(4, R_EDI);
nkeynes@586
    74
    call_func0(sh4_write_long);
nkeynes@539
    75
}
nkeynes@539
    76
nkeynes@547
    77
#define MEM_READ_DOUBLE_SIZE 43
nkeynes@539
    78
/**
nkeynes@539
    79
 * Read a double (64-bit) value from memory, writing the first word into arg2a
nkeynes@539
    80
 * and the second into arg2b. The addr must not be in EAX
nkeynes@539
    81
 */
nkeynes@539
    82
static inline void MEM_READ_DOUBLE( int addr, int arg2a, int arg2b )
nkeynes@539
    83
{
nkeynes@547
    84
    REXW(); SUB_imm8s_r32( 8, R_ESP );
nkeynes@539
    85
    PUSH_r32(addr);
nkeynes@539
    86
    call_func1(sh4_read_long, addr);
nkeynes@539
    87
    POP_r32(R_EDI);
nkeynes@539
    88
    PUSH_r32(R_EAX);
nkeynes@539
    89
    ADD_imm8s_r32(4, R_EDI);
nkeynes@539
    90
    call_func0(sh4_read_long);
nkeynes@539
    91
    MOV_r32_r32(R_EAX, arg2b);
nkeynes@539
    92
    POP_r32(arg2a);
nkeynes@547
    93
    REXW(); ADD_imm8s_r32( 8, R_ESP );
nkeynes@539
    94
}
nkeynes@539
    95
nkeynes@539
    96
nkeynes@539
    97
/**
nkeynes@539
    98
 * Emit the 'start of block' assembly. Sets up the stack frame and save
nkeynes@539
    99
 * SI/DI as required
nkeynes@539
   100
 */
nkeynes@901
   101
void enter_block( ) 
nkeynes@539
   102
{
nkeynes@539
   103
    PUSH_r32(R_EBP);
nkeynes@669
   104
    load_ptr( R_EBP, ((uint8_t *)&sh4r) + 128 );
nkeynes@926
   105
    // Minimum aligned allocation is 16 bytes
nkeynes@926
   106
    REXW(); SUB_imm8s_r32( 16, R_ESP );
nkeynes@926
   107
}
nkeynes@926
   108
nkeynes@926
   109
static inline void exit_block( )
nkeynes@926
   110
{
nkeynes@926
   111
    REXW(); ADD_imm8s_r32( 16, R_ESP );
nkeynes@926
   112
    POP_r32(R_EBP);
nkeynes@926
   113
    RET();
nkeynes@539
   114
}
nkeynes@908
   115
nkeynes@539
   116
/**
nkeynes@539
   117
 * Exit the block with sh4r.pc already written
nkeynes@539
   118
 */
nkeynes@586
   119
void exit_block_pcset( sh4addr_t pc )
nkeynes@539
   120
{
nkeynes@539
   121
    load_imm32( R_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
nkeynes@539
   122
    ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );    // 6
nkeynes@590
   123
    load_spreg( R_EAX, R_PC );
nkeynes@590
   124
    if( sh4_x86.tlb_on ) {
nkeynes@736
   125
        call_func1(xlat_get_code_by_vma,R_EAX);
nkeynes@590
   126
    } else {
nkeynes@736
   127
        call_func1(xlat_get_code,R_EAX);
nkeynes@590
   128
    }
nkeynes@926
   129
    exit_block();
nkeynes@590
   130
}
nkeynes@590
   131
nkeynes@590
   132
/**
nkeynes@590
   133
 * Exit the block with sh4r.new_pc written with the target address
nkeynes@590
   134
 */
nkeynes@590
   135
void exit_block_newpcset( sh4addr_t pc )
nkeynes@590
   136
{
nkeynes@590
   137
    load_imm32( R_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
nkeynes@590
   138
    ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );    // 6
nkeynes@590
   139
    load_spreg( R_EAX, R_NEW_PC );
nkeynes@590
   140
    store_spreg( R_EAX, R_PC );
nkeynes@586
   141
    if( sh4_x86.tlb_on ) {
nkeynes@736
   142
        call_func1(xlat_get_code_by_vma,R_EAX);
nkeynes@586
   143
    } else {
nkeynes@736
   144
        call_func1(xlat_get_code,R_EAX);
nkeynes@586
   145
    }
nkeynes@926
   146
    exit_block();
nkeynes@539
   147
}
nkeynes@539
   148
nkeynes@586
   149
#define EXIT_BLOCK_SIZE(pc) (25 + (IS_IN_ICACHE(pc)?10:CALL_FUNC1_SIZE))
nkeynes@539
   150
/**
nkeynes@539
   151
 * Exit the block to an absolute PC
nkeynes@539
   152
 */
nkeynes@926
   153
void exit_block_abs( sh4addr_t pc, sh4addr_t endpc )
nkeynes@539
   154
{
nkeynes@539
   155
    load_imm32( R_ECX, pc );                            // 5
nkeynes@539
   156
    store_spreg( R_ECX, REG_OFFSET(pc) );               // 3
nkeynes@586
   157
    if( IS_IN_ICACHE(pc) ) {
nkeynes@736
   158
        REXW(); MOV_moff32_EAX( xlat_get_lut_entry(pc) );
nkeynes@926
   159
        REXW(); AND_imm8s_r32( 0xFC, R_EAX ); // 4
nkeynes@586
   160
    } else if( sh4_x86.tlb_on ) {
nkeynes@736
   161
        call_func1(xlat_get_code_by_vma, R_ECX);
nkeynes@586
   162
    } else {
nkeynes@736
   163
        call_func1(xlat_get_code,R_ECX);
nkeynes@586
   164
    }
nkeynes@539
   165
    load_imm32( R_ECX, ((endpc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
nkeynes@539
   166
    ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );     // 6
nkeynes@926
   167
    exit_block();
nkeynes@539
   168
}
nkeynes@539
   169
nkeynes@539
   170
nkeynes@586
   171
#define EXIT_BLOCK_REL_SIZE(pc)  (28 + (IS_IN_ICACHE(pc)?10:CALL_FUNC1_SIZE))
nkeynes@586
   172
nkeynes@586
   173
/**
nkeynes@586
   174
 * Exit the block to a relative PC
nkeynes@586
   175
 */
nkeynes@586
   176
void exit_block_rel( sh4addr_t pc, sh4addr_t endpc )
nkeynes@586
   177
{
nkeynes@586
   178
    load_imm32( R_ECX, pc - sh4_x86.block_start_pc );   // 5
nkeynes@586
   179
    ADD_sh4r_r32( R_PC, R_ECX );
nkeynes@586
   180
    store_spreg( R_ECX, REG_OFFSET(pc) );               // 3
nkeynes@586
   181
    if( IS_IN_ICACHE(pc) ) {
nkeynes@736
   182
        REXW(); MOV_moff32_EAX( xlat_get_lut_entry(GET_ICACHE_PHYS(pc)) ); // 5
nkeynes@926
   183
        REXW(); AND_imm8s_r32( 0xFC, R_EAX ); // 4
nkeynes@586
   184
    } else if( sh4_x86.tlb_on ) {
nkeynes@736
   185
        call_func1(xlat_get_code_by_vma,R_ECX);
nkeynes@586
   186
    } else {
nkeynes@736
   187
        call_func1(xlat_get_code,R_ECX);
nkeynes@586
   188
    }
nkeynes@586
   189
    load_imm32( R_ECX, ((endpc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
nkeynes@586
   190
    ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );     // 6
nkeynes@926
   191
    exit_block();
nkeynes@586
   192
}
nkeynes@586
   193
nkeynes@539
   194
/**
nkeynes@539
   195
 * Write the block trailer (exception handling block)
nkeynes@539
   196
 */
nkeynes@539
   197
void sh4_translate_end_block( sh4addr_t pc ) {
nkeynes@539
   198
    if( sh4_x86.branch_taken == FALSE ) {
nkeynes@736
   199
        // Didn't exit unconditionally already, so write the termination here
nkeynes@736
   200
        exit_block_rel( pc, pc );
nkeynes@539
   201
    }
nkeynes@539
   202
    if( sh4_x86.backpatch_posn != 0 ) {
nkeynes@736
   203
        unsigned int i;
nkeynes@736
   204
        // Raise exception
nkeynes@736
   205
        uint8_t *end_ptr = xlat_output;
nkeynes@736
   206
        MOV_r32_r32( R_EDX, R_ECX );
nkeynes@736
   207
        ADD_r32_r32( R_EDX, R_ECX );
nkeynes@736
   208
        ADD_r32_sh4r( R_ECX, R_PC );
nkeynes@736
   209
        MOV_moff32_EAX( &sh4_cpu_period );
nkeynes@736
   210
        MUL_r32( R_EDX );
nkeynes@736
   211
        ADD_r32_sh4r( R_EAX, REG_OFFSET(slice_cycle) );
nkeynes@539
   212
nkeynes@736
   213
        call_func0( sh4_raise_exception );
nkeynes@736
   214
        load_spreg( R_EAX, R_PC );
nkeynes@736
   215
        if( sh4_x86.tlb_on ) {
nkeynes@736
   216
            call_func1(xlat_get_code_by_vma,R_EAX);
nkeynes@736
   217
        } else {
nkeynes@736
   218
            call_func1(xlat_get_code,R_EAX);
nkeynes@736
   219
        }
nkeynes@926
   220
        exit_block();
nkeynes@926
   221
        
nkeynes@736
   222
        // Exception already raised - just cleanup
nkeynes@736
   223
        uint8_t *preexc_ptr = xlat_output;
nkeynes@736
   224
        MOV_r32_r32( R_EDX, R_ECX );
nkeynes@736
   225
        ADD_r32_r32( R_EDX, R_ECX );
nkeynes@736
   226
        ADD_r32_sh4r( R_ECX, R_SPC );
nkeynes@736
   227
        MOV_moff32_EAX( &sh4_cpu_period );
nkeynes@736
   228
        MUL_r32( R_EDX );
nkeynes@736
   229
        ADD_r32_sh4r( R_EAX, REG_OFFSET(slice_cycle) );
nkeynes@736
   230
        load_spreg( R_EDI, R_PC );
nkeynes@736
   231
        if( sh4_x86.tlb_on ) {
nkeynes@736
   232
            call_func0(xlat_get_code_by_vma);
nkeynes@736
   233
        } else {
nkeynes@736
   234
            call_func0(xlat_get_code);
nkeynes@736
   235
        }
nkeynes@926
   236
        exit_block();
nkeynes@586
   237
nkeynes@736
   238
        for( i=0; i< sh4_x86.backpatch_posn; i++ ) {
nkeynes@736
   239
            uint32_t *fixup_addr = (uint32_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset];
nkeynes@736
   240
            if( sh4_x86.backpatch_list[i].exc_code < 0 ) {
nkeynes@927
   241
                if( sh4_x86.backpatch_list[i].exc_code == -2 ) {
nkeynes@927
   242
                    *((uintptr_t *)fixup_addr) = (uintptr_t)xlat_output; 
nkeynes@927
   243
                } else {
nkeynes@927
   244
                    *fixup_addr = xlat_output - (uint8_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset] - 4;
nkeynes@927
   245
                }
nkeynes@736
   246
                load_imm32( R_EDX, sh4_x86.backpatch_list[i].fixup_icount );
nkeynes@736
   247
                int rel = preexc_ptr - xlat_output;
nkeynes@736
   248
                JMP_rel(rel);
nkeynes@736
   249
            } else {
nkeynes@927
   250
                *fixup_addr = xlat_output - (uint8_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset] - 4;
nkeynes@736
   251
                load_imm32( R_EDI, sh4_x86.backpatch_list[i].exc_code );
nkeynes@736
   252
                load_imm32( R_EDX, sh4_x86.backpatch_list[i].fixup_icount );
nkeynes@736
   253
                int rel = end_ptr - xlat_output;
nkeynes@736
   254
                JMP_rel(rel);
nkeynes@736
   255
            }
nkeynes@736
   256
        }
nkeynes@539
   257
    }
nkeynes@539
   258
}
nkeynes@539
   259
nkeynes@926
   260
struct UnwindInfo {
nkeynes@926
   261
    uintptr_t block_start;
nkeynes@926
   262
    uintptr_t block_end;
nkeynes@926
   263
    void *pc;
nkeynes@926
   264
};
nkeynes@926
   265
nkeynes@586
   266
_Unwind_Reason_Code xlat_check_frame( struct _Unwind_Context *context, void *arg )
nkeynes@586
   267
{
nkeynes@926
   268
    struct UnwindInfo *info = arg;
nkeynes@926
   269
    void *pc = (void *)_Unwind_GetIP(context);
nkeynes@926
   270
    if( ((uintptr_t)pc) >= info->block_start && ((uintptr_t)pc) < info->block_end ) {
nkeynes@926
   271
        info->pc = pc;
nkeynes@586
   272
        return _URC_NORMAL_STOP;
nkeynes@586
   273
    }
nkeynes@926
   274
   
nkeynes@586
   275
    return _URC_NO_REASON;
nkeynes@586
   276
}
nkeynes@586
   277
nkeynes@926
   278
void *xlat_get_native_pc( void *code, uint32_t code_size )
nkeynes@586
   279
{
nkeynes@586
   280
    struct _Unwind_Exception exc;
nkeynes@926
   281
    struct UnwindInfo info;
nkeynes@736
   282
nkeynes@926
   283
    info.pc = NULL;
nkeynes@926
   284
    info.block_start = (uintptr_t)code;
nkeynes@926
   285
    info.block_end = info.block_start + code_size;
nkeynes@586
   286
    void *result = NULL;
nkeynes@926
   287
    _Unwind_Backtrace( xlat_check_frame, &info );
nkeynes@926
   288
    return info.pc;
nkeynes@586
   289
}
nkeynes@586
   290
nkeynes@736
   291
#endif /* !lxdream_ia64abi_H */
.