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