Search
lxdream.org :: lxdream/src/sh4/ia64abi.h
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/ia64abi.h
changeset 736:a02d1475ccfd
prev670:5d277b7ad0df
next800:0d1be79c9b33
author nkeynes
date Mon Jul 14 07:44:42 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Re-indent everything consistently
Fix include guards for consistency as well
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@539
    42
    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@539
    49
    MOV_r32_r32(arg1, R_EDI);
nkeynes@539
    50
    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@539
    94
void sh4_translate_begin_block( sh4addr_t pc ) 
nkeynes@539
    95
{
nkeynes@539
    96
    PUSH_r32(R_EBP);
nkeynes@539
    97
    /* mov &sh4r, ebp */
nkeynes@669
    98
    load_ptr( R_EBP, ((uint8_t *)&sh4r) + 128 );
nkeynes@736
    99
nkeynes@539
   100
    sh4_x86.in_delay_slot = FALSE;
nkeynes@539
   101
    sh4_x86.priv_checked = FALSE;
nkeynes@539
   102
    sh4_x86.fpuen_checked = FALSE;
nkeynes@539
   103
    sh4_x86.branch_taken = FALSE;
nkeynes@539
   104
    sh4_x86.backpatch_posn = 0;
nkeynes@539
   105
    sh4_x86.block_start_pc = pc;
nkeynes@586
   106
    sh4_x86.tlb_on = IS_MMU_ENABLED();
nkeynes@539
   107
    sh4_x86.tstate = TSTATE_NONE;
nkeynes@539
   108
}
nkeynes@539
   109
nkeynes@539
   110
/**
nkeynes@539
   111
 * Exit the block with sh4r.pc already written
nkeynes@539
   112
 */
nkeynes@586
   113
void exit_block_pcset( sh4addr_t pc )
nkeynes@539
   114
{
nkeynes@539
   115
    load_imm32( R_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
nkeynes@539
   116
    ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );    // 6
nkeynes@590
   117
    load_spreg( R_EAX, R_PC );
nkeynes@590
   118
    if( sh4_x86.tlb_on ) {
nkeynes@736
   119
        call_func1(xlat_get_code_by_vma,R_EAX);
nkeynes@590
   120
    } else {
nkeynes@736
   121
        call_func1(xlat_get_code,R_EAX);
nkeynes@590
   122
    }
nkeynes@590
   123
    POP_r32(R_EBP);
nkeynes@590
   124
    RET();
nkeynes@590
   125
}
nkeynes@590
   126
nkeynes@590
   127
/**
nkeynes@590
   128
 * Exit the block with sh4r.new_pc written with the target address
nkeynes@590
   129
 */
nkeynes@590
   130
void exit_block_newpcset( sh4addr_t pc )
nkeynes@590
   131
{
nkeynes@590
   132
    load_imm32( R_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
nkeynes@590
   133
    ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );    // 6
nkeynes@590
   134
    load_spreg( R_EAX, R_NEW_PC );
nkeynes@590
   135
    store_spreg( R_EAX, R_PC );
nkeynes@586
   136
    if( sh4_x86.tlb_on ) {
nkeynes@736
   137
        call_func1(xlat_get_code_by_vma,R_EAX);
nkeynes@586
   138
    } else {
nkeynes@736
   139
        call_func1(xlat_get_code,R_EAX);
nkeynes@586
   140
    }
nkeynes@539
   141
    POP_r32(R_EBP);
nkeynes@539
   142
    RET();
nkeynes@539
   143
}
nkeynes@539
   144
nkeynes@586
   145
#define EXIT_BLOCK_SIZE(pc) (25 + (IS_IN_ICACHE(pc)?10:CALL_FUNC1_SIZE))
nkeynes@539
   146
/**
nkeynes@539
   147
 * Exit the block to an absolute PC
nkeynes@539
   148
 */
nkeynes@539
   149
void exit_block( sh4addr_t pc, sh4addr_t endpc )
nkeynes@539
   150
{
nkeynes@539
   151
    load_imm32( R_ECX, pc );                            // 5
nkeynes@539
   152
    store_spreg( R_ECX, REG_OFFSET(pc) );               // 3
nkeynes@586
   153
    if( IS_IN_ICACHE(pc) ) {
nkeynes@736
   154
        REXW(); MOV_moff32_EAX( xlat_get_lut_entry(pc) );
nkeynes@586
   155
    } else if( sh4_x86.tlb_on ) {
nkeynes@736
   156
        call_func1(xlat_get_code_by_vma, R_ECX);
nkeynes@586
   157
    } else {
nkeynes@736
   158
        call_func1(xlat_get_code,R_ECX);
nkeynes@586
   159
    }
nkeynes@586
   160
    REXW(); AND_imm8s_r32( 0xFC, R_EAX ); // 4
nkeynes@539
   161
    load_imm32( R_ECX, ((endpc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
nkeynes@539
   162
    ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );     // 6
nkeynes@539
   163
    POP_r32(R_EBP);
nkeynes@539
   164
    RET();
nkeynes@539
   165
}
nkeynes@539
   166
nkeynes@539
   167
nkeynes@586
   168
#define EXIT_BLOCK_REL_SIZE(pc)  (28 + (IS_IN_ICACHE(pc)?10:CALL_FUNC1_SIZE))
nkeynes@586
   169
nkeynes@586
   170
/**
nkeynes@586
   171
 * Exit the block to a relative PC
nkeynes@586
   172
 */
nkeynes@586
   173
void exit_block_rel( sh4addr_t pc, sh4addr_t endpc )
nkeynes@586
   174
{
nkeynes@586
   175
    load_imm32( R_ECX, pc - sh4_x86.block_start_pc );   // 5
nkeynes@586
   176
    ADD_sh4r_r32( R_PC, R_ECX );
nkeynes@586
   177
    store_spreg( R_ECX, REG_OFFSET(pc) );               // 3
nkeynes@586
   178
    if( IS_IN_ICACHE(pc) ) {
nkeynes@736
   179
        REXW(); MOV_moff32_EAX( xlat_get_lut_entry(GET_ICACHE_PHYS(pc)) ); // 5
nkeynes@586
   180
    } else if( sh4_x86.tlb_on ) {
nkeynes@736
   181
        call_func1(xlat_get_code_by_vma,R_ECX);
nkeynes@586
   182
    } else {
nkeynes@736
   183
        call_func1(xlat_get_code,R_ECX);
nkeynes@586
   184
    }
nkeynes@586
   185
    REXW(); AND_imm8s_r32( 0xFC, R_EAX ); // 4
nkeynes@586
   186
    load_imm32( R_ECX, ((endpc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
nkeynes@586
   187
    ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );     // 6
nkeynes@586
   188
    POP_r32(R_EBP);
nkeynes@586
   189
    RET();
nkeynes@586
   190
}
nkeynes@586
   191
nkeynes@539
   192
/**
nkeynes@539
   193
 * Write the block trailer (exception handling block)
nkeynes@539
   194
 */
nkeynes@539
   195
void sh4_translate_end_block( sh4addr_t pc ) {
nkeynes@539
   196
    if( sh4_x86.branch_taken == FALSE ) {
nkeynes@736
   197
        // Didn't exit unconditionally already, so write the termination here
nkeynes@736
   198
        exit_block_rel( pc, pc );
nkeynes@539
   199
    }
nkeynes@539
   200
    if( sh4_x86.backpatch_posn != 0 ) {
nkeynes@736
   201
        unsigned int i;
nkeynes@736
   202
        // Raise exception
nkeynes@736
   203
        uint8_t *end_ptr = xlat_output;
nkeynes@736
   204
        MOV_r32_r32( R_EDX, R_ECX );
nkeynes@736
   205
        ADD_r32_r32( R_EDX, R_ECX );
nkeynes@736
   206
        ADD_r32_sh4r( R_ECX, R_PC );
nkeynes@736
   207
        MOV_moff32_EAX( &sh4_cpu_period );
nkeynes@736
   208
        MUL_r32( R_EDX );
nkeynes@736
   209
        ADD_r32_sh4r( R_EAX, REG_OFFSET(slice_cycle) );
nkeynes@539
   210
nkeynes@736
   211
        call_func0( sh4_raise_exception );
nkeynes@736
   212
        load_spreg( R_EAX, R_PC );
nkeynes@736
   213
        if( sh4_x86.tlb_on ) {
nkeynes@736
   214
            call_func1(xlat_get_code_by_vma,R_EAX);
nkeynes@736
   215
        } else {
nkeynes@736
   216
            call_func1(xlat_get_code,R_EAX);
nkeynes@736
   217
        }
nkeynes@736
   218
        POP_r32(R_EBP);
nkeynes@736
   219
        RET();
nkeynes@539
   220
nkeynes@736
   221
        // Exception already raised - just cleanup
nkeynes@736
   222
        uint8_t *preexc_ptr = xlat_output;
nkeynes@736
   223
        MOV_r32_r32( R_EDX, R_ECX );
nkeynes@736
   224
        ADD_r32_r32( R_EDX, R_ECX );
nkeynes@736
   225
        ADD_r32_sh4r( R_ECX, R_SPC );
nkeynes@736
   226
        MOV_moff32_EAX( &sh4_cpu_period );
nkeynes@736
   227
        MUL_r32( R_EDX );
nkeynes@736
   228
        ADD_r32_sh4r( R_EAX, REG_OFFSET(slice_cycle) );
nkeynes@736
   229
        load_spreg( R_EDI, R_PC );
nkeynes@736
   230
        if( sh4_x86.tlb_on ) {
nkeynes@736
   231
            call_func0(xlat_get_code_by_vma);
nkeynes@736
   232
        } else {
nkeynes@736
   233
            call_func0(xlat_get_code);
nkeynes@736
   234
        }
nkeynes@736
   235
        POP_r32(R_EBP);
nkeynes@736
   236
        RET();
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
            *fixup_addr = xlat_output - (uint8_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset] - 4;
nkeynes@736
   241
            if( sh4_x86.backpatch_list[i].exc_code < 0 ) {
nkeynes@736
   242
                load_imm32( R_EDX, sh4_x86.backpatch_list[i].fixup_icount );
nkeynes@736
   243
                int stack_adj = -1 - sh4_x86.backpatch_list[i].exc_code;
nkeynes@736
   244
                if( stack_adj > 0 ) { 
nkeynes@736
   245
                    ADD_imm8s_r32( stack_adj*4, R_ESP );
nkeynes@736
   246
                }
nkeynes@736
   247
                int rel = preexc_ptr - xlat_output;
nkeynes@736
   248
                JMP_rel(rel);
nkeynes@736
   249
            } else {
nkeynes@736
   250
                load_imm32( R_EDI, sh4_x86.backpatch_list[i].exc_code );
nkeynes@736
   251
                load_imm32( R_EDX, sh4_x86.backpatch_list[i].fixup_icount );
nkeynes@736
   252
                int rel = end_ptr - xlat_output;
nkeynes@736
   253
                JMP_rel(rel);
nkeynes@736
   254
            }
nkeynes@736
   255
        }
nkeynes@539
   256
    }
nkeynes@539
   257
}
nkeynes@539
   258
nkeynes@586
   259
_Unwind_Reason_Code xlat_check_frame( struct _Unwind_Context *context, void *arg )
nkeynes@586
   260
{
nkeynes@586
   261
    void *rbp = (void *)_Unwind_GetGR(context, 6);
nkeynes@670
   262
    void *expect = (((uint8_t *)&sh4r) + 128 );
nkeynes@669
   263
    if( rbp == expect ) { 
nkeynes@586
   264
        void **result = (void **)arg;
nkeynes@586
   265
        *result = (void *)_Unwind_GetIP(context);
nkeynes@586
   266
        return _URC_NORMAL_STOP;
nkeynes@586
   267
    }
nkeynes@736
   268
nkeynes@586
   269
    return _URC_NO_REASON;
nkeynes@586
   270
}
nkeynes@586
   271
nkeynes@586
   272
void *xlat_get_native_pc()
nkeynes@586
   273
{
nkeynes@586
   274
    struct _Unwind_Exception exc;
nkeynes@736
   275
nkeynes@586
   276
    void *result = NULL;
nkeynes@586
   277
    _Unwind_Backtrace( xlat_check_frame, &result );
nkeynes@586
   278
    return result;
nkeynes@586
   279
}
nkeynes@586
   280
nkeynes@736
   281
#endif /* !lxdream_ia64abi_H */
.