Search
lxdream.org :: lxdream/src/sh4/ia64abi.h
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/ia64abi.h
changeset 991:60c7fab9c880
prev957:0f6131f6cc3a
author nkeynes
date Wed Mar 04 23:12:21 2009 +0000 (13 years ago)
permissions -rw-r--r--
last change Move xltcache to xlat/ src directory
Commit new and improved x86 opcode file - cleaned up and added support for amd64 extended registers
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@991
    27
static inline void 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@991
    30
    MOVL_r32_r32( addr_reg, REG_RCX ); 
nkeynes@991
    31
    SHRL_imm_r32( 12, REG_RCX ); 
nkeynes@991
    32
    MOVP_immptr_rptr( base, REG_RDI );
nkeynes@991
    33
    MOVP_sib_rptr(3, REG_RCX, REG_RDI, 0, REG_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@991
    44
    MOVQ_imm64_r64((uint64_t)ptr, REG_RAX);
nkeynes@991
    45
    CALL_r32(REG_RAX);
nkeynes@539
    46
}
nkeynes@539
    47
nkeynes@539
    48
static inline void call_func1( void *ptr, int arg1 )
nkeynes@539
    49
{
nkeynes@991
    50
    MOVQ_r64_r64(arg1, REG_RDI);
nkeynes@539
    51
    call_func0(ptr);
nkeynes@539
    52
}
nkeynes@539
    53
nkeynes@927
    54
static inline void call_func1_exc( void *ptr, int arg1, int pc )
nkeynes@927
    55
{
nkeynes@991
    56
    MOVQ_r64_r64(arg1, REG_RDI);
nkeynes@991
    57
    MOVP_immptr_rptr(0, REG_RSI);
nkeynes@991
    58
    sh4_x86_add_backpatch( xlat_output, pc, -2 );
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@991
    64
    MOVQ_r64_r64(arg1, REG_RDI);
nkeynes@991
    65
    CALL_r32disp(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@991
    70
    MOVQ_r64_r64(arg1, REG_RDI);
nkeynes@991
    71
    MOVP_immptr_rptr(0, REG_RSI);
nkeynes@991
    72
    sh4_x86_add_backpatch( xlat_output, pc, -2 );
nkeynes@991
    73
    CALL_r32disp(preg, disp8);
nkeynes@953
    74
}
nkeynes@953
    75
nkeynes@539
    76
static inline void call_func2( void *ptr, int arg1, int arg2 )
nkeynes@539
    77
{
nkeynes@991
    78
    MOVQ_r64_r64(arg1, REG_RDI);
nkeynes@991
    79
    MOVQ_r64_r64(arg2, REG_RSI);
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@991
    85
    MOVQ_r64_r64(arg1, REG_RDI);
nkeynes@991
    86
    MOVQ_r64_r64(arg2, REG_RSI);
nkeynes@991
    87
    CALL_r32disp(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@991
    92
    MOVQ_r64_r64(arg1, REG_RDI);
nkeynes@991
    93
    MOVQ_r64_r64(arg2, REG_RSI);
nkeynes@991
    94
    MOVP_immptr_rptr(0, REG_RDX);
nkeynes@991
    95
    sh4_x86_add_backpatch( xlat_output, pc, -2 );
nkeynes@991
    96
    CALL_r32disp(preg, disp8);
nkeynes@539
    97
}
nkeynes@539
    98
nkeynes@539
    99
nkeynes@953
   100
nkeynes@539
   101
/**
nkeynes@539
   102
 * Emit the 'start of block' assembly. Sets up the stack frame and save
nkeynes@539
   103
 * SI/DI as required
nkeynes@539
   104
 */
nkeynes@901
   105
void enter_block( ) 
nkeynes@539
   106
{
nkeynes@991
   107
    PUSH_r32(REG_RBP);
nkeynes@991
   108
    load_ptr( REG_RBP, ((uint8_t *)&sh4r) + 128 );
nkeynes@926
   109
    // Minimum aligned allocation is 16 bytes
nkeynes@991
   110
    SUBQ_imms_r64( 16, REG_RSP );
nkeynes@926
   111
}
nkeynes@926
   112
nkeynes@926
   113
static inline void exit_block( )
nkeynes@926
   114
{
nkeynes@991
   115
    ADDQ_imms_r64( 16, REG_RSP );
nkeynes@991
   116
    POP_r32(REG_RBP);
nkeynes@926
   117
    RET();
nkeynes@539
   118
}
nkeynes@908
   119
nkeynes@539
   120
/**
nkeynes@539
   121
 * Exit the block with sh4r.pc already written
nkeynes@539
   122
 */
nkeynes@586
   123
void exit_block_pcset( sh4addr_t pc )
nkeynes@539
   124
{
nkeynes@991
   125
    load_imm32( REG_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
nkeynes@991
   126
    ADDL_r32_rbpdisp( REG_ECX, REG_OFFSET(slice_cycle) );    // 6
nkeynes@991
   127
    load_spreg( REG_RAX, R_PC );
nkeynes@590
   128
    if( sh4_x86.tlb_on ) {
nkeynes@991
   129
        call_func1(xlat_get_code_by_vma,REG_RAX);
nkeynes@590
   130
    } else {
nkeynes@991
   131
        call_func1(xlat_get_code,REG_RAX);
nkeynes@590
   132
    }
nkeynes@926
   133
    exit_block();
nkeynes@590
   134
}
nkeynes@590
   135
nkeynes@590
   136
/**
nkeynes@590
   137
 * Exit the block with sh4r.new_pc written with the target address
nkeynes@590
   138
 */
nkeynes@590
   139
void exit_block_newpcset( sh4addr_t pc )
nkeynes@590
   140
{
nkeynes@991
   141
    load_imm32( REG_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
nkeynes@991
   142
    ADDL_r32_rbpdisp( REG_ECX, REG_OFFSET(slice_cycle) );    // 6
nkeynes@991
   143
    load_spreg( REG_RAX, R_NEW_PC );
nkeynes@991
   144
    store_spreg( REG_RAX, R_PC );
nkeynes@586
   145
    if( sh4_x86.tlb_on ) {
nkeynes@991
   146
        call_func1(xlat_get_code_by_vma,REG_RAX);
nkeynes@586
   147
    } else {
nkeynes@991
   148
        call_func1(xlat_get_code,REG_RAX);
nkeynes@586
   149
    }
nkeynes@926
   150
    exit_block();
nkeynes@539
   151
}
nkeynes@539
   152
nkeynes@586
   153
#define EXIT_BLOCK_SIZE(pc) (25 + (IS_IN_ICACHE(pc)?10:CALL_FUNC1_SIZE))
nkeynes@539
   154
/**
nkeynes@539
   155
 * Exit the block to an absolute PC
nkeynes@539
   156
 */
nkeynes@926
   157
void exit_block_abs( sh4addr_t pc, sh4addr_t endpc )
nkeynes@539
   158
{
nkeynes@991
   159
    load_imm32( REG_RCX, pc );                            // 5
nkeynes@991
   160
    store_spreg( REG_RCX, REG_OFFSET(pc) );               // 3
nkeynes@586
   161
    if( IS_IN_ICACHE(pc) ) {
nkeynes@991
   162
        MOVP_moffptr_rax( xlat_get_lut_entry(pc) );
nkeynes@991
   163
        ANDQ_imms_r64( 0xFFFFFFFC, REG_RAX ); // 4
nkeynes@586
   164
    } else if( sh4_x86.tlb_on ) {
nkeynes@991
   165
        call_func1(xlat_get_code_by_vma, REG_RCX);
nkeynes@586
   166
    } else {
nkeynes@991
   167
        call_func1(xlat_get_code,REG_RCX);
nkeynes@586
   168
    }
nkeynes@991
   169
    load_imm32( REG_ECX, ((endpc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
nkeynes@991
   170
    ADDL_r32_rbpdisp( REG_ECX, REG_OFFSET(slice_cycle) );     // 6
nkeynes@926
   171
    exit_block();
nkeynes@539
   172
}
nkeynes@539
   173
nkeynes@539
   174
nkeynes@586
   175
#define EXIT_BLOCK_REL_SIZE(pc)  (28 + (IS_IN_ICACHE(pc)?10:CALL_FUNC1_SIZE))
nkeynes@586
   176
nkeynes@586
   177
/**
nkeynes@586
   178
 * Exit the block to a relative PC
nkeynes@586
   179
 */
nkeynes@586
   180
void exit_block_rel( sh4addr_t pc, sh4addr_t endpc )
nkeynes@586
   181
{
nkeynes@991
   182
    load_imm32( REG_ECX, pc - sh4_x86.block_start_pc );   // 5
nkeynes@991
   183
    ADDL_rbpdisp_r32( R_PC, REG_ECX );
nkeynes@991
   184
    store_spreg( REG_ECX, REG_OFFSET(pc) );               // 3
nkeynes@586
   185
    if( IS_IN_ICACHE(pc) ) {
nkeynes@991
   186
        MOVP_moffptr_rax( xlat_get_lut_entry(GET_ICACHE_PHYS(pc)) ); // 5
nkeynes@991
   187
        ANDQ_imms_r64( 0xFFFFFFFC, REG_RAX ); // 4
nkeynes@586
   188
    } else if( sh4_x86.tlb_on ) {
nkeynes@991
   189
        call_func1(xlat_get_code_by_vma,REG_RCX);
nkeynes@586
   190
    } else {
nkeynes@991
   191
        call_func1(xlat_get_code,REG_RCX);
nkeynes@586
   192
    }
nkeynes@991
   193
    load_imm32( REG_ECX, ((endpc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
nkeynes@991
   194
    ADDL_r32_rbpdisp( REG_ECX, REG_OFFSET(slice_cycle) );     // 6
nkeynes@926
   195
    exit_block();
nkeynes@586
   196
}
nkeynes@586
   197
nkeynes@539
   198
/**
nkeynes@957
   199
 * Exit unconditionally with a general exception
nkeynes@957
   200
 */
nkeynes@957
   201
void exit_block_exc( int code, sh4addr_t pc )
nkeynes@957
   202
{
nkeynes@991
   203
    load_imm32( REG_ECX, pc - sh4_x86.block_start_pc );   // 5
nkeynes@991
   204
    ADDL_r32_rbpdisp( REG_ECX, R_PC );
nkeynes@991
   205
    load_imm32( REG_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
nkeynes@991
   206
    ADDL_r32_rbpdisp( REG_ECX, REG_OFFSET(slice_cycle) );     // 6
nkeynes@991
   207
    load_imm32( REG_RAX, code );
nkeynes@991
   208
    call_func1( sh4_raise_exception, REG_RAX );
nkeynes@957
   209
    
nkeynes@991
   210
    load_spreg( REG_RAX, R_PC );
nkeynes@957
   211
    if( sh4_x86.tlb_on ) {
nkeynes@991
   212
        call_func1(xlat_get_code_by_vma,REG_RAX);
nkeynes@957
   213
    } else {
nkeynes@991
   214
        call_func1(xlat_get_code,REG_RAX);
nkeynes@957
   215
    }
nkeynes@957
   216
nkeynes@957
   217
    exit_block();
nkeynes@957
   218
}    
nkeynes@957
   219
nkeynes@957
   220
nkeynes@957
   221
/**
nkeynes@539
   222
 * Write the block trailer (exception handling block)
nkeynes@539
   223
 */
nkeynes@539
   224
void sh4_translate_end_block( sh4addr_t pc ) {
nkeynes@539
   225
    if( sh4_x86.branch_taken == FALSE ) {
nkeynes@736
   226
        // Didn't exit unconditionally already, so write the termination here
nkeynes@736
   227
        exit_block_rel( pc, pc );
nkeynes@539
   228
    }
nkeynes@539
   229
    if( sh4_x86.backpatch_posn != 0 ) {
nkeynes@736
   230
        unsigned int i;
nkeynes@736
   231
        // Raise exception
nkeynes@736
   232
        uint8_t *end_ptr = xlat_output;
nkeynes@991
   233
        MOVL_r32_r32( REG_RDX, REG_RCX );
nkeynes@991
   234
        ADDL_r32_r32( REG_RDX, REG_RCX );
nkeynes@991
   235
        ADDL_r32_rbpdisp( REG_RCX, R_PC );
nkeynes@991
   236
        MOVL_moffptr_eax( &sh4_cpu_period );
nkeynes@991
   237
        MULL_r32( REG_RDX );
nkeynes@991
   238
        ADDL_r32_rbpdisp( REG_RAX, REG_OFFSET(slice_cycle) );
nkeynes@539
   239
nkeynes@736
   240
        call_func0( sh4_raise_exception );
nkeynes@991
   241
        load_spreg( REG_RAX, R_PC );
nkeynes@736
   242
        if( sh4_x86.tlb_on ) {
nkeynes@991
   243
            call_func1(xlat_get_code_by_vma,REG_RAX);
nkeynes@736
   244
        } else {
nkeynes@991
   245
            call_func1(xlat_get_code,REG_RAX);
nkeynes@736
   246
        }
nkeynes@926
   247
        exit_block();
nkeynes@926
   248
        
nkeynes@736
   249
        // Exception already raised - just cleanup
nkeynes@736
   250
        uint8_t *preexc_ptr = xlat_output;
nkeynes@991
   251
        MOVL_r32_r32( REG_EDX, REG_ECX );
nkeynes@991
   252
        ADDL_r32_r32( REG_EDX, REG_ECX );
nkeynes@991
   253
        ADDL_r32_rbpdisp( REG_ECX, R_SPC );
nkeynes@991
   254
        MOVL_moffptr_eax( &sh4_cpu_period );
nkeynes@991
   255
        MULL_r32( REG_EDX );
nkeynes@991
   256
        ADDL_r32_rbpdisp( REG_EAX, REG_OFFSET(slice_cycle) );
nkeynes@991
   257
        load_spreg( REG_RDI, R_PC );
nkeynes@736
   258
        if( sh4_x86.tlb_on ) {
nkeynes@736
   259
            call_func0(xlat_get_code_by_vma);
nkeynes@736
   260
        } else {
nkeynes@736
   261
            call_func0(xlat_get_code);
nkeynes@736
   262
        }
nkeynes@926
   263
        exit_block();
nkeynes@586
   264
nkeynes@736
   265
        for( i=0; i< sh4_x86.backpatch_posn; i++ ) {
nkeynes@736
   266
            uint32_t *fixup_addr = (uint32_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset];
nkeynes@736
   267
            if( sh4_x86.backpatch_list[i].exc_code < 0 ) {
nkeynes@927
   268
                if( sh4_x86.backpatch_list[i].exc_code == -2 ) {
nkeynes@927
   269
                    *((uintptr_t *)fixup_addr) = (uintptr_t)xlat_output; 
nkeynes@927
   270
                } else {
nkeynes@927
   271
                    *fixup_addr = xlat_output - (uint8_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset] - 4;
nkeynes@927
   272
                }
nkeynes@991
   273
                load_imm32( REG_RDX, sh4_x86.backpatch_list[i].fixup_icount );
nkeynes@736
   274
                int rel = preexc_ptr - xlat_output;
nkeynes@991
   275
                JMP_prerel(rel);
nkeynes@736
   276
            } else {
nkeynes@927
   277
                *fixup_addr = xlat_output - (uint8_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset] - 4;
nkeynes@991
   278
                load_imm32( REG_RDI, sh4_x86.backpatch_list[i].exc_code );
nkeynes@991
   279
                load_imm32( REG_RDX, sh4_x86.backpatch_list[i].fixup_icount );
nkeynes@736
   280
                int rel = end_ptr - xlat_output;
nkeynes@991
   281
                JMP_prerel(rel);
nkeynes@736
   282
            }
nkeynes@736
   283
        }
nkeynes@539
   284
    }
nkeynes@539
   285
}
nkeynes@539
   286
nkeynes@926
   287
struct UnwindInfo {
nkeynes@926
   288
    uintptr_t block_start;
nkeynes@926
   289
    uintptr_t block_end;
nkeynes@926
   290
    void *pc;
nkeynes@926
   291
};
nkeynes@926
   292
nkeynes@586
   293
_Unwind_Reason_Code xlat_check_frame( struct _Unwind_Context *context, void *arg )
nkeynes@586
   294
{
nkeynes@926
   295
    struct UnwindInfo *info = arg;
nkeynes@926
   296
    void *pc = (void *)_Unwind_GetIP(context);
nkeynes@926
   297
    if( ((uintptr_t)pc) >= info->block_start && ((uintptr_t)pc) < info->block_end ) {
nkeynes@926
   298
        info->pc = pc;
nkeynes@586
   299
        return _URC_NORMAL_STOP;
nkeynes@586
   300
    }
nkeynes@926
   301
   
nkeynes@586
   302
    return _URC_NO_REASON;
nkeynes@586
   303
}
nkeynes@586
   304
nkeynes@926
   305
void *xlat_get_native_pc( void *code, uint32_t code_size )
nkeynes@586
   306
{
nkeynes@586
   307
    struct _Unwind_Exception exc;
nkeynes@926
   308
    struct UnwindInfo info;
nkeynes@736
   309
nkeynes@926
   310
    info.pc = NULL;
nkeynes@926
   311
    info.block_start = (uintptr_t)code;
nkeynes@926
   312
    info.block_end = info.block_start + code_size;
nkeynes@586
   313
    void *result = NULL;
nkeynes@926
   314
    _Unwind_Backtrace( xlat_check_frame, &info );
nkeynes@926
   315
    return info.pc;
nkeynes@586
   316
}
nkeynes@586
   317
nkeynes@736
   318
#endif /* !lxdream_ia64abi_H */
.