sewardj | 192f114 | 2004-06-27 10:41:58 +0000 | [diff] [blame^] | 1 | |
| 2 | /*---------------------------------------------------------------*/ |
| 3 | /*--- ---*/ |
| 4 | /*--- This file (dispatch.c) is ---*/ |
| 5 | /*--- Copyright (c) 2004 OpenWorks LLP. All rights reserved. ---*/ |
| 6 | /*--- ---*/ |
| 7 | /*---------------------------------------------------------------*/ |
| 8 | |
| 9 | #include "basictypes.h" |
| 10 | |
| 11 | |
| 12 | /* --------------------------------------------------------- */ |
| 13 | /* TRANSLATION TABLE/CACHE */ |
| 14 | /* --------------------------------------------------------- */ |
| 15 | |
| 16 | static |
| 17 | char* find_translation ( char* orig ) |
| 18 | { |
| 19 | int i; |
| 20 | for (i = 0; i < n_transtab_used; i++) |
| 21 | if (transtab[i].orig == orig) |
| 22 | return transtab[i].trans; |
| 23 | return NULL; |
| 24 | } |
| 25 | |
| 26 | |
| 27 | #define N_TT_ENTRIES 1000 |
| 28 | |
| 29 | typedef |
| 30 | struct { |
| 31 | char* orig; |
| 32 | int orig_size; |
| 33 | char* trans; |
| 34 | int trans_size; |
| 35 | } |
| 36 | TTEntry; |
| 37 | |
| 38 | int n_transtab_used = 0; |
| 39 | TTEntry transtab[N_TT_ENTRIES]; |
| 40 | |
| 41 | |
| 42 | /* Call here to add a translation to the trans cache. |
| 43 | Supplied translation is in mallocville. add_translation should |
| 44 | copy it out as the caller will free it on return. */ |
| 45 | |
| 46 | /* EXPORTED */ |
| 47 | void add_translation ( char* orig, int orig_size, char* trans, int trans_size ) |
| 48 | { |
| 49 | int i; |
| 50 | assert(n_transtab_used < N_TT_ENTRIES); |
| 51 | transtab[n_transtab_used].orig = orig; |
| 52 | transtab[n_transtab_used].orig_size = orig_size; |
| 53 | transtab[n_transtab_used].trans_size = trans_size; |
| 54 | |
| 55 | transtab[n_transtab_used].trans = malloc(trans_size); |
| 56 | assert(transtab[n_transtab_used].trans != NULL); |
| 57 | for (i = 0; i < trans_size; i++) |
| 58 | transtab[n_transtab_used].trans[i] = trans[i]; |
| 59 | |
| 60 | #ifdef arm_TARGET_ARCH |
| 61 | arm_notify_new_code(transtab[n_transtab_used].trans, trans_size); |
| 62 | #endif |
| 63 | |
| 64 | n_transtab_used++; |
| 65 | } |
| 66 | |
| 67 | /* Run the simulated machine for a while. Returns when a new BB needs |
| 68 | to be translated, and returns its address. Returns NULL when we |
| 69 | want to stop. */ |
| 70 | |
| 71 | /* EXPORTED */ |
| 72 | char* run_machine ( void ) |
| 73 | { |
| 74 | char* nextpc_orig; |
| 75 | char* nextpc_trans; |
| 76 | while (1) { |
| 77 | nextpc_orig = (char*)(regs_arm[REG_PC]); |
| 78 | if (nextpc_orig == stop_at) |
| 79 | return NULL; |
| 80 | nextpc_trans = find_translation(nextpc_orig); |
| 81 | if (nextpc_trans == NULL) |
| 82 | return nextpc_orig; |
| 83 | run_translation(nextpc_trans, (char*) ®s_arm[0] ); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | |
| 88 | /* HOW TO USE: |
| 89 | |
| 90 | for a main fn :: void main ( void ) |
| 91 | |
| 92 | * load .o's, link, etc |
| 93 | |
| 94 | * call initialise_machine with & main |
| 95 | |
| 96 | * call run_machine repeatedly. If it returns NULL, stop. Else |
| 97 | make a translation of the returned address, pass it to |
| 98 | add_translation, and resume running by calling run_machine. |
| 99 | |
| 100 | */ |