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