/[gxemul]/trunk/src/cpus/cpu_dyntrans.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/src/cpus/cpu_dyntrans.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 22 by dpavlin, Mon Oct 8 16:19:37 2007 UTC revision 30 by dpavlin, Mon Oct 8 16:20:40 2007 UTC
# Line 25  Line 25 
25   *  SUCH DAMAGE.   *  SUCH DAMAGE.
26   *   *
27   *   *
28   *  $Id: cpu_dyntrans.c,v 1.54 2006/02/09 22:55:20 debug Exp $   *  $Id: cpu_dyntrans.c,v 1.120 2006/08/12 11:43:12 debug Exp $
29   *   *
30   *  Common dyntrans routines. Included from cpu_*.c.   *  Common dyntrans routines. Included from cpu_*.c.
31   */   */
32    
33    
34  #ifdef  DYNTRANS_CPU_RUN_INSTR  #ifndef STATIC_STUFF
35  #if 1   /*  IC statistics:  */  #define STATIC_STUFF
36    /*
37     *  gather_statistics():
38     */
39  static void gather_statistics(struct cpu *cpu)  static void gather_statistics(struct cpu *cpu)
40  {  {
41            char ch, buf[60];
42          struct DYNTRANS_IC *ic = cpu->cd.DYNTRANS_ARCH.next_ic;          struct DYNTRANS_IC *ic = cpu->cd.DYNTRANS_ARCH.next_ic;
43          static long long n = 0;          int i = 0;
         static FILE *f = NULL;  
   
         n++;  
         if (n < 100000000)  
                 return;  
   
         if (f == NULL) {  
                 f = fopen("instruction_call_statistics.raw", "w");  
                 if (f == NULL) {  
                         fatal("Unable to open statistics file for output.\n");  
                         exit(1);  
                 }  
         }  
         fwrite(&ic->f, 1, sizeof(void *), f);  
 }  
 #else   /*  PC statistics:  */  
 static void gather_statistics(struct cpu *cpu)  
 {  
44          uint64_t a;          uint64_t a;
45          int low_pc = ((size_t)cpu->cd.DYNTRANS_ARCH.next_ic - (size_t)          int low_pc = ((size_t)cpu->cd.DYNTRANS_ARCH.next_ic - (size_t)
46              cpu->cd.DYNTRANS_ARCH.cur_ic_page) / sizeof(struct DYNTRANS_IC);              cpu->cd.DYNTRANS_ARCH.cur_ic_page) / sizeof(struct DYNTRANS_IC);
         if (low_pc < 0 || low_pc >= DYNTRANS_IC_ENTRIES_PER_PAGE)  
                 return;  
   
 #if 0  
         /*  Use the physical address:  */  
         cpu->cd.DYNTRANS_ARCH.cur_physpage = (void *)  
             cpu->cd.DYNTRANS_ARCH.cur_ic_page;  
         a = cpu->cd.DYNTRANS_ARCH.cur_physpage->physaddr;  
 #else  
         /*  Use the PC (virtual address):  */  
         a = cpu->pc;  
 #endif  
47    
48          a &= ~((DYNTRANS_IC_ENTRIES_PER_PAGE-1) <<          buf[0] = '\0';
             DYNTRANS_INSTR_ALIGNMENT_SHIFT);  
         a += low_pc << DYNTRANS_INSTR_ALIGNMENT_SHIFT;  
49    
50          /*          while ((ch = cpu->machine->statistics_fields[i]) != '\0') {
51           *  TODO: Everything below this line should be cleaned up :-)                  if (i != 0)
52           */                          strlcat(buf, " ", sizeof(buf));
53  a &= 0x03ffffff;  
54  {                  switch (ch) {
55          static long long *array = NULL;                  case 'i':
56          static char *array_16kpage_in_use = NULL;                          snprintf(buf + strlen(buf), sizeof(buf),
57          static int n = 0;                              "%p", (void *)ic->f);
58          a >>= DYNTRANS_INSTR_ALIGNMENT_SHIFT;                          break;
59          if (array == NULL)                  case 'p':
60                  array = zeroed_alloc(sizeof(long long) * 16384*1024);                          /*  Physical program counter address:  */
61          if (array_16kpage_in_use == NULL)                          /*  (low_pc must be within the page!)  */
62                  array_16kpage_in_use = zeroed_alloc(sizeof(char) * 1024);                          if (low_pc < 0 ||
63          a &= (16384*1024-1);                              low_pc >= DYNTRANS_IC_ENTRIES_PER_PAGE)
64          array[a] ++;                                  strlcat(buf, "-", sizeof(buf));
65          array_16kpage_in_use[a / 16384] = 1;                          cpu->cd.DYNTRANS_ARCH.cur_physpage = (void *)
66          n++;                              cpu->cd.DYNTRANS_ARCH.cur_ic_page;
67          if ((n & 0x3fffffff) == 0) {                          a = cpu->cd.DYNTRANS_ARCH.cur_physpage->physaddr;
68                  FILE *f = fopen("statistics.out", "w");                          a &= ~((DYNTRANS_IC_ENTRIES_PER_PAGE-1) <<
69                  int i, j;                              DYNTRANS_INSTR_ALIGNMENT_SHIFT);
70                  printf("Saving statistics... "); fflush(stdout);                          a += low_pc << DYNTRANS_INSTR_ALIGNMENT_SHIFT;
71                  for (i=0; i<1024; i++)                          if (cpu->is_32bit)
72                          if (array_16kpage_in_use[i]) {                                  snprintf(buf + strlen(buf), sizeof(buf),
73                                  for (j=0; j<16384; j++)                                      "0x%016"PRIx32, (uint32_t)a);
74                                          if (array[i*16384 + j] > 0)                          else
75                                                  fprintf(f, "%lli\t0x%016llx\n",                                  snprintf(buf + strlen(buf), sizeof(buf),
76                                                      (long long)array[i*16384+j],                                      "0x%016"PRIx64, (uint64_t)a);
77                                                      (long long)((i*16384+j) <<                          break;
78                                  DYNTRANS_INSTR_ALIGNMENT_SHIFT));                  case 'v':
79                          }                          /*  Virtual program counter address:  */
80                  fclose(f);                          /*  (low_pc inside the page, or in a delay slot)  */
81                  printf("n=0x%08x\n", n);                          if (low_pc < 0 ||
82                                low_pc >= DYNTRANS_IC_ENTRIES_PER_PAGE + 2)
83                                    strlcat(buf, "-", sizeof(buf));
84                            a = cpu->pc;
85                            a &= ~((DYNTRANS_IC_ENTRIES_PER_PAGE-1) <<
86                                DYNTRANS_INSTR_ALIGNMENT_SHIFT);
87                            a += low_pc << DYNTRANS_INSTR_ALIGNMENT_SHIFT;
88                            if (cpu->is_32bit)
89                                    snprintf(buf + strlen(buf), sizeof(buf),
90                                        "0x%016"PRIx32, (uint32_t)a);
91                            else
92                                    snprintf(buf + strlen(buf), sizeof(buf),
93                                        "0x%016"PRIx64, (uint64_t)a);
94                            break;
95                    }
96                    i++;
97          }          }
98    
99            fprintf(cpu->machine->statistics_file, "%s\n", buf);
100  }  }
101  }  
 #endif  /*  PC statistics  */  
102    
103  #define S               gather_statistics(cpu)  #define S               gather_statistics(cpu)
104    
105    
106  #ifdef DYNTRANS_VARIABLE_INSTRUCTION_LENGTH  #ifdef DYNTRANS_VARIABLE_INSTRUCTION_LENGTH
107  #define I               ic = cpu->cd.DYNTRANS_ARCH.next_ic;             \  #define I               ic = cpu->cd.DYNTRANS_ARCH.next_ic;             \
108                          cpu->cd.DYNTRANS_ARCH.next_ic += ic->arg[0];    \                          cpu->cd.DYNTRANS_ARCH.next_ic += ic->arg[0];    \
109                          ic->f(cpu, ic);                          ic->f(cpu, ic);
110  #else  #else
111  #define I               ic = cpu->cd.DYNTRANS_ARCH.next_ic ++; ic->f(cpu, ic);  
112    /*  The normal instruction execution core:  */
113    #define I       ic = cpu->cd.DYNTRANS_ARCH.next_ic ++; ic->f(cpu, ic);
114    
115    /*  For heavy debugging:  */
116    /*  #define I   ic = cpu->cd.DYNTRANS_ARCH.next_ic ++;  \
117                    {       \
118                            int low_pc = ((size_t)cpu->cd.DYNTRANS_ARCH.next_ic - \
119                                (size_t)cpu->cd.DYNTRANS_ARCH.cur_ic_page) / \
120                                sizeof(struct DYNTRANS_IC);                 \
121                            printf("cur_ic_page=%p ic=%p (low_pc=0x%x)\n",  \
122                                cpu->cd.DYNTRANS_ARCH.cur_ic_page,          \
123                                ic, low_pc << DYNTRANS_INSTR_ALIGNMENT_SHIFT); \
124                    } \
125                    ic->f(cpu, ic);  */
126    
127    /*  static long long nr_of_I_calls = 0;  */
128    
129    /*  Temporary hack for finding NULL bugs:  */
130    /*  #define I   ic = cpu->cd.DYNTRANS_ARCH.next_ic ++;                  \
131                    nr_of_I_calls ++;                                       \
132                    if (ic->f == NULL) {                                    \
133                            int low_pc = ((size_t)cpu->cd.DYNTRANS_ARCH.next_ic - \
134                                (size_t)cpu->cd.DYNTRANS_ARCH.cur_ic_page) / \
135                                sizeof(struct DYNTRANS_IC);                 \
136                            cpu->pc &= ~((DYNTRANS_IC_ENTRIES_PER_PAGE-1) << \
137                                DYNTRANS_INSTR_ALIGNMENT_SHIFT);            \
138                            cpu->pc += (low_pc << DYNTRANS_INSTR_ALIGNMENT_SHIFT);\
139                            printf("Crash at %016"PRIx64"\n", cpu->pc);     \
140                            printf("nr of I calls: %lli\n", nr_of_I_calls); \
141                            printf("Next ic = %p\n", cpu->cd.               \
142                                    DYNTRANS_ARCH.next_ic);                 \
143                            printf("cur ic page = %p\n", cpu->cd.           \
144                                    DYNTRANS_ARCH.cur_ic_page);             \
145                            cpu->running = 0;                               \
146                            return 0;                                       \
147                    }                                                       \
148                    ic->f(cpu, ic);  */
149    
150    /*  Temporary hack for MIPS, to hunt for 32-bit/64-bit sign-extension bugs:  */
151    /*  #define I           { int k; for (k=1; k<=31; k++)  \
152            cpu->cd.mips.gpr[k] = (int32_t)cpu->cd.mips.gpr[k];\
153            if (cpu->cd.mips.gpr[0] != 0) {                 \
154                    fatal("NOOOOOO\n"); exit(1);            \
155            }                                               \
156            ic = cpu->cd.DYNTRANS_ARCH.next_ic ++; ic->f(cpu, ic); }
157    */
158  #endif  #endif
159    #endif  /*  STATIC STUFF  */
160    
161    
162    
163    #ifdef  DYNTRANS_RUN_INSTR
164  /*  /*
165   *  XXX_cpu_run_instr():   *  XXX_run_instr():
166   *   *
167   *  Execute one or more instructions on a specific CPU, using dyntrans.   *  Execute one or more instructions on a specific CPU, using dyntrans.
168     *  (For dualmode archs, this function is included twice.)
169   *   *
170   *  Return value is the number of instructions executed during this call,   *  Return value is the number of instructions executed during this call,
171   *  0 if no instructions were executed.   *  0 if no instructions were executed.
172   */   */
173  int DYNTRANS_CPU_RUN_INSTR(struct emul *emul, struct cpu *cpu)  int DYNTRANS_RUN_INSTR(struct cpu *cpu)
174  {  {
175  #ifdef MODE32          MODE_uint_t cached_pc;
         uint32_t cached_pc;  
 #else  
         uint64_t cached_pc;  
 #endif  
176          int low_pc, n_instrs;          int low_pc, n_instrs;
177    
178            /*  Ugly... fix this some day.  */
179  #ifdef DYNTRANS_DUALMODE_32  #ifdef DYNTRANS_DUALMODE_32
180          if (cpu->is_32bit)  #ifdef MODE32
181                  DYNTRANS_PC_TO_POINTERS32(cpu);          DYNTRANS_PC_TO_POINTERS32(cpu);
182          else  #else
183            DYNTRANS_PC_TO_POINTERS(cpu);
184  #endif  #endif
185    #else
186          DYNTRANS_PC_TO_POINTERS(cpu);          DYNTRANS_PC_TO_POINTERS(cpu);
187    #endif
188    
189          /*          /*
190           *  Interrupt assertion?  (This is _below_ the initial PC to pointer           *  Interrupt assertion?  (This is _below_ the initial PC to pointer
191           *  conversion; if the conversion caused an exception of some kind           *  conversion; if the conversion caused an exception of some kind
192           *  then interrupts are probably disabled, and the exception will get           *  then interrupts are probably disabled, and the exception will get
193           *  priority over device interrupts.)           *  priority over device interrupts.)
194             *
195             *  TODO: Turn this into a family-specific function somewhere...
196           */           */
197  #ifdef DYNTRANS_ARM  #ifdef DYNTRANS_ARM
198          if (cpu->cd.arm.irq_asserted && !(cpu->cd.arm.cpsr & ARM_FLAG_I))          if (cpu->cd.arm.irq_asserted && !(cpu->cd.arm.cpsr & ARM_FLAG_I))
199                  arm_exception(cpu, ARM_EXCEPTION_IRQ);                  arm_exception(cpu, ARM_EXCEPTION_IRQ);
200  #endif  #endif
201    #ifdef DYNTRANS_MIPS
202            {
203                    int enabled, mask;
204                    int status = cpu->cd.mips.coproc[0]->reg[COP0_STATUS];
205                    if (cpu->cd.mips.cpu_type.exc_model == EXC3K) {
206                            /*  R3000:  */
207                            enabled = status & MIPS_SR_INT_IE;
208                    } else {
209                            /*  R4000 and others:  */
210                            enabled = (status & STATUS_IE)
211                                && !(status & STATUS_EXL) && !(status & STATUS_ERL);
212                            /*  Special case for R5900/C790/TX79:  */
213                            if (cpu->cd.mips.cpu_type.rev == MIPS_R5900 &&
214                                !(status & R5900_STATUS_EIE))
215                                    enabled = 0;
216                    }
217                    mask = status & cpu->cd.mips.coproc[0]->reg[COP0_CAUSE]
218                        & STATUS_IM_MASK;
219    
220                    if (enabled && mask)
221                            mips_cpu_exception(cpu, EXCEPTION_INT, 0, 0, 0, 0, 0,0);
222            }
223    #endif
224  #ifdef DYNTRANS_PPC  #ifdef DYNTRANS_PPC
225          if (cpu->cd.ppc.dec_intr_pending && cpu->cd.ppc.msr & PPC_MSR_EE) {          if (cpu->cd.ppc.dec_intr_pending && cpu->cd.ppc.msr & PPC_MSR_EE) {
226                  ppc_exception(cpu, PPC_EXCEPTION_DEC);                  if (!(cpu->cd.ppc.cpu_type.flags & PPC_NO_DEC))
227                            ppc_exception(cpu, PPC_EXCEPTION_DEC);
228                  cpu->cd.ppc.dec_intr_pending = 0;                  cpu->cd.ppc.dec_intr_pending = 0;
229          }          }
230          if (cpu->cd.ppc.irq_asserted && cpu->cd.ppc.msr & PPC_MSR_EE)          if (cpu->cd.ppc.irq_asserted && cpu->cd.ppc.msr & PPC_MSR_EE)
# Line 169  int DYNTRANS_CPU_RUN_INSTR(struct emul * Line 234  int DYNTRANS_CPU_RUN_INSTR(struct emul *
234          cached_pc = cpu->pc;          cached_pc = cpu->pc;
235    
236          cpu->n_translated_instrs = 0;          cpu->n_translated_instrs = 0;
         cpu->running_translated = 1;  
237    
238          cpu->cd.DYNTRANS_ARCH.cur_physpage = (void *)          cpu->cd.DYNTRANS_ARCH.cur_physpage = (void *)
239              cpu->cd.DYNTRANS_ARCH.cur_ic_page;              cpu->cd.DYNTRANS_ARCH.cur_ic_page;
240    
241          if (single_step || cpu->machine->instruction_trace) {          if (single_step || cpu->machine->instruction_trace
242                || cpu->machine->register_dump) {
243                  /*                  /*
244                   *  Single-step:                   *  Single-step:
245                   */                   */
246                  struct DYNTRANS_IC *ic = cpu->cd.DYNTRANS_ARCH.next_ic                  struct DYNTRANS_IC *ic = cpu->cd.DYNTRANS_ARCH.next_ic;
247  #ifndef DYNTRANS_VARIABLE_INSTRUCTION_LENGTH                  if (cpu->machine->register_dump) {
248                      ++                          debug("\n");
249  #endif                          cpu_register_dump(cpu->machine, cpu, 1, 0x1);
250                      ;                  }
251                  if (cpu->machine->instruction_trace) {                  if (cpu->machine->instruction_trace) {
252  #ifdef DYNTRANS_X86  #ifdef DYNTRANS_X86
253                          unsigned char instr[17];                          unsigned char instr[17];
# Line 195  int DYNTRANS_CPU_RUN_INSTR(struct emul * Line 260  int DYNTRANS_CPU_RUN_INSTR(struct emul *
260                          unsigned char instr[4];         /*  General case...  */                          unsigned char instr[4];         /*  General case...  */
261  #endif  #endif
262  #endif  #endif
263    
264                          if (!cpu->memory_rw(cpu, cpu->mem, cached_pc, &instr[0],                          if (!cpu->memory_rw(cpu, cpu->mem, cached_pc, &instr[0],
265                              sizeof(instr), MEM_READ, CACHE_INSTRUCTION)) {                              sizeof(instr), MEM_READ, CACHE_INSTRUCTION)) {
266                                  fatal("XXX_cpu_run_instr(): could not read "                                  fatal("XXX_run_instr(): could not read "
267                                      "the instruction\n");                                      "the instruction\n");
268                          } else {                          } else {
269                                  cpu_disassemble_instr(cpu->machine, cpu,                                  cpu_disassemble_instr(cpu->machine, cpu,
270                                      instr, 1, 0, 0);                                      instr, 1, 0);
271  #ifdef DYNTRANS_MIPS  #ifdef DYNTRANS_DELAYSLOT
 /*  TODO: generalize, not just MIPS  */  
272                                  /*  Show the instruction in the delay slot,                                  /*  Show the instruction in the delay slot,
273                                      if any:  */                                      if any:  */
274                                  fatal("TODO: check for delay slot!\n");                                  if (cpu->instruction_has_delayslot == NULL)
275                                            fatal("WARNING: ihd func not yet"
276                                                " implemented?\n");
277                                    else if (cpu->instruction_has_delayslot(cpu,
278                                        instr)) {
279                                            int saved_delayslot = cpu->delay_slot;
280                                            cpu->memory_rw(cpu, cpu->mem, cached_pc
281                                                + sizeof(instr), &instr[0],
282                                                sizeof(instr), MEM_READ,
283                                                CACHE_INSTRUCTION);
284                                            cpu->delay_slot = DELAYED;
285                                            cpu->pc += sizeof(instr);
286                                            cpu_disassemble_instr(cpu->machine,
287                                                cpu, instr, 1, 0);
288                                            cpu->delay_slot = saved_delayslot;
289                                            cpu->pc -= sizeof(instr);
290                                    }
291  #endif  #endif
292                          }                          }
293                  }                  }
294    
295                  /*  When single-stepping, multiple instruction calls cannot                  if (cpu->machine->statistics_enabled)
                     be combined into one. This clears all translations:  */  
                 if (cpu->cd.DYNTRANS_ARCH.cur_physpage->flags & COMBINATIONS) {  
                         int i;  
                         for (i=0; i<DYNTRANS_IC_ENTRIES_PER_PAGE; i++)  
                                 cpu->cd.DYNTRANS_ARCH.cur_physpage->ics[i].f =  
 #ifdef DYNTRANS_DUALMODE_32  
                                     cpu->is_32bit?  
                                         instr32(to_be_translated) :  
 #endif  
                                         instr(to_be_translated);  
                         fatal("[ Note: The translation of physical page 0x%llx"  
                             " contained combinations of instructions; these "  
                             "are now flushed because we are single-stepping."  
                             " ]\n", (long long)cpu->cd.DYNTRANS_ARCH.  
                             cur_physpage->physaddr);  
                         cpu->cd.DYNTRANS_ARCH.cur_physpage->flags &=  
                             ~(COMBINATIONS | TRANSLATIONS);  
                 }  
   
                 if (show_opcode_statistics)  
296                          S;                          S;
297    
298                  /*  Execute just one instruction:  */                  /*  Execute just one instruction:  */
299                  ic->f(cpu, ic);                  I;
300    
301                  n_instrs = 1;                  n_instrs = 1;
302          } else if (show_opcode_statistics) {          } else if (cpu->machine->cycle_accurate) {
303                    /*  Executing multiple instructions, and call devices'
304                        tick functions:  */
305                    n_instrs = 0;
306                    for (;;) {
307                            struct DYNTRANS_IC *ic;
308    /*  TODO: continue here  */
309    int64_t cycles = cpu->cd.avr.extra_cycles;
310                            I;
311                            n_instrs += 1;
312    cycles = cpu->cd.avr.extra_cycles - cycles + 1;
313    /*  The instruction took 'cycles' cycles.  */
314    /* printf("A\n"); */
315    while (cycles-- > 0)
316            cpu->machine->tick_func[1](cpu, cpu->machine->tick_extra[1]);
317    /* printf("B\n"); */
318    
319                            if (n_instrs + cpu->n_translated_instrs >=
320                                N_SAFE_DYNTRANS_LIMIT)
321                                    break;
322                    }
323            } else if (cpu->machine->statistics_enabled) {
324                  /*  Gather statistics while executing multiple instructions:  */                  /*  Gather statistics while executing multiple instructions:  */
325                  n_instrs = 0;                  n_instrs = 0;
326                  for (;;) {                  for (;;) {
# Line 250  int DYNTRANS_CPU_RUN_INSTR(struct emul * Line 333  int DYNTRANS_CPU_RUN_INSTR(struct emul *
333    
334                          n_instrs += 24;                          n_instrs += 24;
335    
336                          if (!cpu->running_translated ||                          if (n_instrs + cpu->n_translated_instrs >=
337                              n_instrs + cpu->n_translated_instrs >= 16384)                              N_SAFE_DYNTRANS_LIMIT)
338                                  break;                                  break;
339                  }                  }
340          } else {          } else {
341                  /*  Execute multiple instructions:  */                  /*  Execute multiple instructions:  */
342                  n_instrs = 0;                  int n = 0;
343                  for (;;) {                  for (;;) {
344                          struct DYNTRANS_IC *ic;                          struct DYNTRANS_IC *ic;
345    
# Line 268  int DYNTRANS_CPU_RUN_INSTR(struct emul * Line 351  int DYNTRANS_CPU_RUN_INSTR(struct emul *
351    
352                          I; I; I; I; I;   I; I; I; I; I;                          I; I; I; I; I;   I; I; I; I; I;
353    
354                          n_instrs += 60;                          n += 60;
355    
356                          if (!cpu->running_translated ||                          if (n + cpu->n_translated_instrs >=
357                              n_instrs + cpu->n_translated_instrs >= 16384)                              N_SAFE_DYNTRANS_LIMIT)
358                                  break;                                  break;
359                  }                  }
360                    n_instrs = n;
361          }          }
362    
363          n_instrs += cpu->n_translated_instrs;          n_instrs += cpu->n_translated_instrs;
# Line 300  int DYNTRANS_CPU_RUN_INSTR(struct emul * Line 384  int DYNTRANS_CPU_RUN_INSTR(struct emul *
384                      DYNTRANS_INSTR_ALIGNMENT_SHIFT);                      DYNTRANS_INSTR_ALIGNMENT_SHIFT);
385          }          }
386    
387    #ifdef DYNTRANS_MIPS
388            /*  Update the count register (on everything except EXC3K):  */
389            if (cpu->cd.mips.cpu_type.exc_model != EXC3K) {
390                    uint32_t old = cpu->cd.mips.coproc[0]->reg[COP0_COUNT];
391                    int32_t diff1 = cpu->cd.mips.coproc[0]->reg[COP0_COMPARE] - old;
392                    int32_t diff2;
393                    cpu->cd.mips.coproc[0]->reg[COP0_COUNT] =
394                        (int32_t) (old + n_instrs);
395                    diff2 = cpu->cd.mips.coproc[0]->reg[COP0_COMPARE] -
396                        cpu->cd.mips.coproc[0]->reg[COP0_COUNT];
397                    if (cpu->cd.mips.compare_register_set && diff1>0 && diff2<=0)
398                            cpu_interrupt(cpu, 7);
399            }
400    #endif
401  #ifdef DYNTRANS_PPC  #ifdef DYNTRANS_PPC
402          /*  Update the Decrementer and Time base registers:  */          /*  Update the Decrementer and Time base registers:  */
403          {          {
# Line 318  int DYNTRANS_CPU_RUN_INSTR(struct emul * Line 416  int DYNTRANS_CPU_RUN_INSTR(struct emul *
416          /*  Return the nr of instructions executed:  */          /*  Return the nr of instructions executed:  */
417          return n_instrs;          return n_instrs;
418  }  }
419  #endif  /*  DYNTRANS_CPU_RUN_INSTR  */  #endif  /*  DYNTRANS_RUN_INSTR  */
420    
421    
422    
# Line 336  void DYNTRANS_FUNCTION_TRACE(struct cpu Line 434  void DYNTRANS_FUNCTION_TRACE(struct cpu
434          char *symbol;          char *symbol;
435          uint64_t ot;          uint64_t ot;
436          int x, print_dots = 1, n_args_to_print =          int x, print_dots = 1, n_args_to_print =
437  #ifdef DYNTRANS_ALPHA  #if defined(DYNTRANS_ALPHA) || defined(DYNTRANS_SPARC)
438              6              6
439  #else  #else
440  #ifdef DYNTRANS_SH  #ifdef DYNTRANS_SH
# Line 365  void DYNTRANS_FUNCTION_TRACE(struct cpu Line 463  void DYNTRANS_FUNCTION_TRACE(struct cpu
463           */           */
464          for (x=0; x<n_args_to_print; x++) {          for (x=0; x<n_args_to_print; x++) {
465                  int64_t d;                  int64_t d;
466  #ifdef DYNTRANS_X86  #if defined(DYNTRANS_X86) || defined(DYNTRANS_TRANSPUTER)
467                  d = 0;          /*  TODO  */                  d = 0;          /*  TODO  */
468  #else  #else
469                  /*  Args in registers:  */                  /*  Args in registers:  */
# Line 418  void DYNTRANS_FUNCTION_TRACE(struct cpu Line 516  void DYNTRANS_FUNCTION_TRACE(struct cpu
516                          fatal("&%s", symbol);                          fatal("&%s", symbol);
517                  else {                  else {
518                          if (cpu->is_32bit)                          if (cpu->is_32bit)
519                                  fatal("0x%x", (int)d);                                  fatal("0x%"PRIx32, (uint32_t)d);
520                          else                          else
521                                  fatal("0x%llx", (long long)d);                                  fatal("0x%"PRIx64, (uint64_t)d);
522                  }                  }
523    
524                  if (x < n_args_to_print - 1)                  if (x < n_args_to_print - 1)
# Line 435  void DYNTRANS_FUNCTION_TRACE(struct cpu Line 533  void DYNTRANS_FUNCTION_TRACE(struct cpu
533    
534    
535  #ifdef DYNTRANS_TC_ALLOCATE_DEFAULT_PAGE  #ifdef DYNTRANS_TC_ALLOCATE_DEFAULT_PAGE
   
 /*  forward declaration of to_be_translated and end_of_page:  */  
 static void instr(to_be_translated)(struct cpu *, struct DYNTRANS_IC *);  
 static void instr(end_of_page)(struct cpu *,struct DYNTRANS_IC *);  
 #ifdef DYNTRANS_DUALMODE_32  
 static void instr32(to_be_translated)(struct cpu *, struct DYNTRANS_IC *);  
 static void instr32(end_of_page)(struct cpu *,struct DYNTRANS_IC *);  
 #endif  
   
 #ifdef DYNTRANS_DELAYSLOT  
 static void instr(end_of_page2)(struct cpu *,struct DYNTRANS_IC *);  
 #ifdef DYNTRANS_DUALMODE_32  
 static void instr32(end_of_page2)(struct cpu *,struct DYNTRANS_IC *);  
 #endif  
 #endif  
   
536  /*  /*
537   *  XXX_tc_allocate_default_page():   *  XXX_tc_allocate_default_page():
538   *   *
# Line 461  static void DYNTRANS_TC_ALLOCATE_DEFAULT Line 543  static void DYNTRANS_TC_ALLOCATE_DEFAULT
543          uint64_t physaddr)          uint64_t physaddr)
544  {  {
545          struct DYNTRANS_TC_PHYSPAGE *ppp;          struct DYNTRANS_TC_PHYSPAGE *ppp;
         int i;  
546    
         /*  Create the physpage header:  */  
547          ppp = (struct DYNTRANS_TC_PHYSPAGE *)(cpu->translation_cache          ppp = (struct DYNTRANS_TC_PHYSPAGE *)(cpu->translation_cache
548              + cpu->translation_cache_cur_ofs);              + cpu->translation_cache_cur_ofs);
         ppp->next_ofs = 0;  
         ppp->physaddr = physaddr;  
549    
550          /*  TODO: Is this faster than copying an entire template page?  */          /*  Copy the entire template page first:  */
551          for (i=0; i<DYNTRANS_IC_ENTRIES_PER_PAGE; i++)          memcpy(ppp, cpu->cd.DYNTRANS_ARCH.physpage_template, sizeof(
552                  ppp->ics[i].f =              struct DYNTRANS_TC_PHYSPAGE));
 #ifdef DYNTRANS_DUALMODE_32  
                     cpu->is_32bit? instr32(to_be_translated) :  
 #endif  
                     instr(to_be_translated);  
553    
554          ppp->ics[DYNTRANS_IC_ENTRIES_PER_PAGE + 0].f =          ppp->physaddr = physaddr & ~(DYNTRANS_PAGESIZE - 1);
 #ifdef DYNTRANS_DUALMODE_32  
             cpu->is_32bit? instr32(end_of_page) :  
 #endif  
             instr(end_of_page);  
   
 #ifdef DYNTRANS_DELAYSLOT  
         ppp->ics[DYNTRANS_IC_ENTRIES_PER_PAGE + 1].f =  
 #ifdef DYNTRANS_DUALMODE_32  
             cpu->is_32bit? instr32(end_of_page2) :  
 #endif  
             instr(end_of_page2);  
 #endif  
555    
556          cpu->translation_cache_cur_ofs += sizeof(struct DYNTRANS_TC_PHYSPAGE);          cpu->translation_cache_cur_ofs += sizeof(struct DYNTRANS_TC_PHYSPAGE);
557    
558          cpu->translation_cache_cur_ofs --;          cpu->translation_cache_cur_ofs --;
559          cpu->translation_cache_cur_ofs |= 63;          cpu->translation_cache_cur_ofs |= 127;
560          cpu->translation_cache_cur_ofs ++;          cpu->translation_cache_cur_ofs ++;
561  }  }
562  #endif  /*  DYNTRANS_TC_ALLOCATE_DEFAULT_PAGE  */  #endif  /*  DYNTRANS_TC_ALLOCATE_DEFAULT_PAGE  */
# Line 514  void DYNTRANS_PC_TO_POINTERS_GENERIC(str Line 576  void DYNTRANS_PC_TO_POINTERS_GENERIC(str
576  #else  #else
577          uint64_t          uint64_t
578  #endif  #endif
579              cached_pc, physaddr = 0;              cached_pc = cpu->pc, physaddr = 0;
580          uint32_t physpage_ofs;          uint32_t physpage_ofs;
581          int ok, pagenr, table_index;          int ok, pagenr, table_index;
582          uint32_t *physpage_entryp;          uint32_t *physpage_entryp;
583          struct DYNTRANS_TC_PHYSPAGE *ppp;          struct DYNTRANS_TC_PHYSPAGE *ppp;
584    
585  #ifdef MODE32  #ifdef MODE32
586          int index;          int index = DYNTRANS_ADDR_TO_PAGENR(cached_pc);
         cached_pc = cpu->pc;  
         index = DYNTRANS_ADDR_TO_PAGENR(cached_pc);  
587  #else  #else
588  #ifdef DYNTRANS_ALPHA          const uint32_t mask1 = (1 << DYNTRANS_L1N) - 1;
589          uint32_t a, b;          const uint32_t mask2 = (1 << DYNTRANS_L2N) - 1;
590          int kernel = 0;          const uint32_t mask3 = (1 << DYNTRANS_L3N) - 1;
591          struct alpha_vph_page *vph_p;          uint32_t x1, x2, x3;
592          cached_pc = cpu->pc;          struct DYNTRANS_L2_64_TABLE *l2;
593          a = (cached_pc >> ALPHA_LEVEL0_SHIFT) & (ALPHA_LEVEL0 - 1);          struct DYNTRANS_L3_64_TABLE *l3;
594          b = (cached_pc >> ALPHA_LEVEL1_SHIFT) & (ALPHA_LEVEL1 - 1);  
595          if ((cached_pc >> ALPHA_TOPSHIFT) == ALPHA_TOP_KERNEL) {          x1 = (cached_pc >> (64-DYNTRANS_L1N)) & mask1;
596                  vph_p = cpu->cd.alpha.vph_table0_kernel[a];          x2 = (cached_pc >> (64-DYNTRANS_L1N-DYNTRANS_L2N)) & mask2;
597                  kernel = 1;          x3 = (cached_pc >> (64-DYNTRANS_L1N-DYNTRANS_L2N-DYNTRANS_L3N)) & mask3;
598          } else          /*  fatal("X3: cached_pc=%016"PRIx64" x1=%x x2=%x x3=%x\n",
599                  vph_p = cpu->cd.alpha.vph_table0[a];              (uint64_t)cached_pc, (int)x1, (int)x2, (int)x3);  */
600  #else          l2 = cpu->cd.DYNTRANS_ARCH.l1_64[x1];
601          fatal("Neither alpha nor 32-bit? 3\n");          /*  fatal("  l2 = %p\n", l2);  */
602          exit(1);          l3 = l2->l3[x2];
603  #endif          /*  fatal("  l3 = %p\n", l3);  */
604  #endif  #endif
605    
606          /*  Virtual to physical address translation:  */          /*  Virtual to physical address translation:  */
# Line 551  void DYNTRANS_PC_TO_POINTERS_GENERIC(str Line 611  void DYNTRANS_PC_TO_POINTERS_GENERIC(str
611                  ok = 1;                  ok = 1;
612          }          }
613  #else  #else
614  #ifdef DYNTRANS_ALPHA          if (l3->host_load[x3] != NULL) {
615          if (vph_p->host_load[b] != NULL) {                  physaddr = l3->phys_addr[x3];
                 physaddr = vph_p->phys_addr[b];  
616                  ok = 1;                  ok = 1;
617          }          }
 #else  
         fatal("Neither alpha nor 32-bit? 4\n");  
         exit(1);  
 #endif  
618  #endif  #endif
619    
620          if (!ok) {          if (!ok) {
621                  uint64_t paddr;                  uint64_t paddr;
622                  if (cpu->translate_address != NULL)                  if (cpu->translate_v2p != NULL) {
623                          ok = cpu->translate_address(cpu, cached_pc,                          uint64_t vaddr =
624                              &paddr, FLAG_INSTR);  #if defined(MODE32) && defined(DYNTRANS_MIPS)
625                  else {                          /*  32-bit MIPS is _sign_ extend, not zero.  */
626                                (int32_t)
627    #endif
628                                cached_pc;
629                            ok = cpu->translate_v2p(
630                                cpu, vaddr, &paddr, FLAG_INSTR);
631                    } else {
632                          paddr = cached_pc;                          paddr = cached_pc;
633                          ok = 1;                          ok = 1;
634                  }                  }
635                  if (!ok) {                  if (!ok) {
636                          /*  fatal("TODO: instruction vaddr=>paddr translation"                          /*
637                              " failed. vaddr=0x%llx\n", (long long)cached_pc);                           *  The PC is now set to the exception handler.
638                          fatal("!! cpu->pc=0x%llx\n", (long long)cpu->pc);  */                           *  Try to find the paddr in the translation arrays,
639                             *  or if that fails, call translate_v2p for the
640                             *  exception handler.
641                             */
642                            /*  fatal("TODO: instruction vaddr=>paddr translation "
643                                "failed. vaddr=0x%"PRIx64"\n", (uint64_t)cached_pc);
644                            fatal("!! cpu->pc=0x%"PRIx64"\n", (uint64_t)cpu->pc); */
645    
646                            /*  If there was an exception, the PC has changed.
647                                Update cached_pc:  */
648                            cached_pc = cpu->pc;
649    
650    #ifdef MODE32
651                            index = DYNTRANS_ADDR_TO_PAGENR(cached_pc);
652                            if (cpu->cd.DYNTRANS_ARCH.host_load[index] != NULL) {
653                                    paddr = cpu->cd.DYNTRANS_ARCH.phys_addr[index];
654                                    ok = 1;
655                            }
656    #else
657                            x1 = (cached_pc >> (64-DYNTRANS_L1N)) & mask1;
658                            x2 = (cached_pc >> (64-DYNTRANS_L1N-DYNTRANS_L2N)) & mask2;
659                            x3 = (cached_pc >> (64-DYNTRANS_L1N-DYNTRANS_L2N-DYNTRANS_L3N))
660                                & mask3;
661                            l2 = cpu->cd.DYNTRANS_ARCH.l1_64[x1];
662                            l3 = l2->l3[x2];
663                            if (l3->host_load[x3] != NULL) {
664                                    paddr = l3->phys_addr[x3];
665                                    ok = 1;
666                            }
667    #endif
668    
669                          ok = cpu->translate_address(cpu, cpu->pc, &paddr,                          if (!ok) {
670                              FLAG_INSTR);                                  ok = cpu->translate_v2p(cpu, cpu->pc, &paddr,
671                                        FLAG_INSTR);
672                            }
673    
674                          /*  printf("EXCEPTION HANDLER: vaddr = 0x%x ==> "                          /*  printf("EXCEPTION HANDLER: vaddr = 0x%x ==> "
675                              "paddr = 0x%x\n", (int)cpu->pc, (int)paddr);                              "paddr = 0x%x\n", (int)cpu->pc, (int)paddr);
676                          fatal("!? cpu->pc=0x%llx\n", (long long)cpu->pc);  */                          fatal("!? cpu->pc=0x%"PRIx64"\n", (uint64_t)cpu->pc); */
677    
678                          if (!ok) {                          if (!ok) {
679                                  fatal("FATAL: could not find physical"                                  fatal("FATAL: could not find physical"
# Line 589  void DYNTRANS_PC_TO_POINTERS_GENERIC(str Line 681  void DYNTRANS_PC_TO_POINTERS_GENERIC(str
681                                  exit(1);                                  exit(1);
682                          }                          }
683                  }                  }
684                  cached_pc = cpu->pc;  
 #ifdef MODE32  
                 index = DYNTRANS_ADDR_TO_PAGENR(cached_pc);  
 #endif  
685                  physaddr = paddr;                  physaddr = paddr;
686          }          }
687    
688            physaddr &= ~(DYNTRANS_PAGESIZE - 1);
689    
690  #ifdef MODE32  #ifdef MODE32
691          if (cpu->cd.DYNTRANS_ARCH.host_load[index] == NULL) {          if (cpu->cd.DYNTRANS_ARCH.host_load[index] == NULL) {
692    #else
693            if (l3->host_load[x3] == NULL) {
694    #endif
695                    int q = DYNTRANS_PAGESIZE - 1;
696                  unsigned char *host_page = memory_paddr_to_hostaddr(cpu->mem,                  unsigned char *host_page = memory_paddr_to_hostaddr(cpu->mem,
697                      physaddr, MEM_READ);                      physaddr, MEM_READ);
698                  if (host_page != NULL) {                  if (host_page != NULL) {
                         int q = DYNTRANS_PAGESIZE - 1;  
                         host_page += (physaddr &  
                             ((1 << BITS_PER_MEMBLOCK) - 1) & ~q);  
699                          cpu->update_translation_table(cpu, cached_pc & ~q,                          cpu->update_translation_table(cpu, cached_pc & ~q,
700                              host_page, TLB_CODE, physaddr & ~q);                              host_page, 0, physaddr);
701                  }                  }
702          }          }
 #endif  
703    
704          if (cpu->translation_cache_cur_ofs >= DYNTRANS_CACHE_SIZE) {          if (cpu->translation_cache_cur_ofs >= DYNTRANS_CACHE_SIZE) {
705                  debug("[ dyntrans: resetting the translation cache ]\n");  #ifdef UNSTABLE_DEVEL
706                    fatal("[ dyntrans: resetting the translation cache ]\n");
707    #endif
708                  cpu_create_or_reset_tc(cpu);                  cpu_create_or_reset_tc(cpu);
709          }          }
710    
# Line 626  void DYNTRANS_PC_TO_POINTERS_GENERIC(str Line 719  void DYNTRANS_PC_TO_POINTERS_GENERIC(str
719          while (physpage_ofs != 0) {          while (physpage_ofs != 0) {
720                  ppp = (struct DYNTRANS_TC_PHYSPAGE *)(cpu->translation_cache                  ppp = (struct DYNTRANS_TC_PHYSPAGE *)(cpu->translation_cache
721                      + physpage_ofs);                      + physpage_ofs);
722    
723                  /*  If we found the page in the cache, then we're done:  */                  /*  If we found the page in the cache, then we're done:  */
724                  if (ppp->physaddr == physaddr)                  if (ppp->physaddr == physaddr)
725                          break;                          break;
726    
727                  /*  Try the next page in the chain:  */                  /*  Try the next page in the chain:  */
728                  physpage_ofs = ppp->next_ofs;                  physpage_ofs = ppp->next_ofs;
729          }          }
# Line 637  void DYNTRANS_PC_TO_POINTERS_GENERIC(str Line 732  void DYNTRANS_PC_TO_POINTERS_GENERIC(str
732              new "default" empty translation page.  */              new "default" empty translation page.  */
733    
734          if (ppp == NULL) {          if (ppp == NULL) {
735                  /*  fatal("CREATING page %lli (physaddr 0x%llx), table index "                  /*  fatal("CREATING page %lli (physaddr 0x%"PRIx64"), table "
736                      "%i\n", (long long)pagenr, (long long)physaddr,                      "index %i\n", (long long)pagenr, (uint64_t)physaddr,
737                      (int)table_index);  */                      (int)table_index);  */
738                  *physpage_entryp = physpage_ofs =                  *physpage_entryp = physpage_ofs =
739                      cpu->translation_cache_cur_ofs;                      cpu->translation_cache_cur_ofs;
# Line 653  void DYNTRANS_PC_TO_POINTERS_GENERIC(str Line 748  void DYNTRANS_PC_TO_POINTERS_GENERIC(str
748  #ifdef MODE32  #ifdef MODE32
749          if (cpu->cd.DYNTRANS_ARCH.host_load[index] != NULL)          if (cpu->cd.DYNTRANS_ARCH.host_load[index] != NULL)
750                  cpu->cd.DYNTRANS_ARCH.phys_page[index] = ppp;                  cpu->cd.DYNTRANS_ARCH.phys_page[index] = ppp;
751    #else
752            if (l3->host_load[x3] != NULL)
753                    l3->phys_page[x3] = ppp;
754  #endif  #endif
755    
756  #ifdef DYNTRANS_ALPHA          /*
757          if (vph_p->host_load[b] != NULL)           *  If there are no translations yet on this page, then mark it
758                  vph_p->phys_page[b] = ppp;           *  as non-writable. If there are already translations, then it
759  #endif           *  should already have been marked as non-writable.
760             */
761  #ifdef MODE32          if (ppp->translations == 0) {
762          /*  Small optimization: only mark the physical page as non-writable                  cpu->invalidate_translation_caches(cpu, physaddr,
763              if it did not contain translations. (Because if it does contain                      JUST_MARK_AS_NON_WRITABLE | INVALIDATE_PADDR);
764              translations, it is already non-writable.)  */          }
         if (!cpu->cd.DYNTRANS_ARCH.phystranslation[pagenr >> 5] &  
             (1 << (pagenr & 31)))  
 #endif  
         cpu->invalidate_translation_caches(cpu, physaddr,  
             JUST_MARK_AS_NON_WRITABLE | INVALIDATE_PADDR);  
765    
766          cpu->cd.DYNTRANS_ARCH.cur_ic_page = &ppp->ics[0];          cpu->cd.DYNTRANS_ARCH.cur_ic_page = &ppp->ics[0];
767    
768          cpu->cd.DYNTRANS_ARCH.next_ic = cpu->cd.DYNTRANS_ARCH.cur_ic_page +          cpu->cd.DYNTRANS_ARCH.next_ic = cpu->cd.DYNTRANS_ARCH.cur_ic_page +
769              DYNTRANS_PC_TO_IC_ENTRY(cached_pc);              DYNTRANS_PC_TO_IC_ENTRY(cached_pc);
770    
771          /*  printf("cached_pc=0x%016llx  pagenr=%lli  table_index=%lli, "          /*  printf("cached_pc=0x%016"PRIx64"  pagenr=%lli  table_index=%lli, "
772              "physpage_ofs=0x%016llx\n", (long long)cached_pc, (long long)pagenr,              "physpage_ofs=0x%016"PRIx64"\n", (uint64_t)cached_pc, (long long)
773              (long long)table_index, (long long)physpage_ofs);  */              pagenr, (long long)table_index, (uint64_t)physpage_ofs);  */
774  }  }
775    
776    
# Line 711  void DYNTRANS_PC_TO_POINTERS_FUNC(struct Line 804  void DYNTRANS_PC_TO_POINTERS_FUNC(struct
804          if (ppp != NULL)          if (ppp != NULL)
805                  goto have_it;                  goto have_it;
806  #else  #else
807  #ifdef DYNTRANS_ALPHA          const uint32_t mask1 = (1 << DYNTRANS_L1N) - 1;
808          uint32_t a, b;          const uint32_t mask2 = (1 << DYNTRANS_L2N) - 1;
809          int kernel = 0;          const uint32_t mask3 = (1 << DYNTRANS_L3N) - 1;
810          struct alpha_vph_page *vph_p;          uint32_t x1, x2, x3;
811          a = (cached_pc >> ALPHA_LEVEL0_SHIFT) & (ALPHA_LEVEL0 - 1);          struct DYNTRANS_L2_64_TABLE *l2;
812          b = (cached_pc >> ALPHA_LEVEL1_SHIFT) & (ALPHA_LEVEL1 - 1);          struct DYNTRANS_L3_64_TABLE *l3;
813          if ((cached_pc >> ALPHA_TOPSHIFT) == ALPHA_TOP_KERNEL) {  
814                  vph_p = cpu->cd.alpha.vph_table0_kernel[a];          x1 = (cached_pc >> (64-DYNTRANS_L1N)) & mask1;
815                  kernel = 1;          x2 = (cached_pc >> (64-DYNTRANS_L1N-DYNTRANS_L2N)) & mask2;
816          } else          x3 = (cached_pc >> (64-DYNTRANS_L1N-DYNTRANS_L2N-DYNTRANS_L3N)) & mask3;
817                  vph_p = cpu->cd.alpha.vph_table0[a];          l2 = cpu->cd.DYNTRANS_ARCH.l1_64[x1];
818          if (vph_p != cpu->cd.alpha.vph_default_page) {          l3 = l2->l3[x2];
819                  ppp = vph_p->phys_page[b];          ppp = l3->phys_page[x3];
820                  if (ppp != NULL)          if (ppp != NULL)
821                          goto have_it;                  goto have_it;
         }  
 #else  
         fatal("X1: cached_pc=%016llx\n", (long long)cached_pc);  
         /*  Temporary, to avoid a compiler warning:  */  
         ppp = NULL;  
         fatal("Neither alpha nor 32-bit? 1\n");  
         exit(1);  
 #endif  
822  #endif  #endif
823    
824          DYNTRANS_PC_TO_POINTERS_GENERIC(cpu);          DYNTRANS_PC_TO_POINTERS_GENERIC(cpu);
825          return;          return;
826    
827          /*  Quick return path:  */          /*  Quick return path:  */
 #if defined(MODE32) || defined(DYNTRANS_ALPHA)  
828  have_it:  have_it:
829          cpu->cd.DYNTRANS_ARCH.cur_ic_page = &ppp->ics[0];          cpu->cd.DYNTRANS_ARCH.cur_ic_page = &ppp->ics[0];
830          cpu->cd.DYNTRANS_ARCH.next_ic = cpu->cd.DYNTRANS_ARCH.cur_ic_page +          cpu->cd.DYNTRANS_ARCH.next_ic = cpu->cd.DYNTRANS_ARCH.cur_ic_page +
831              DYNTRANS_PC_TO_IC_ENTRY(cached_pc);              DYNTRANS_PC_TO_IC_ENTRY(cached_pc);
832    
833          /*  printf("cached_pc=0x%016llx  pagenr=%lli  table_index=%lli, "          /*  printf("cached_pc=0x%016"PRIx64"  pagenr=%lli  table_index=%lli, "
834              "physpage_ofs=0x%016llx\n", (long long)cached_pc, (long long)pagenr,              "physpage_ofs=0x%016"PRIx64"\n", (uint64_t)cached_pc, (long long)
835              (long long)table_index, (long long)physpage_ofs);  */              pagenr, (long long)table_index, (uint64_t)physpage_ofs);  */
 #endif  
836  }  }
837  #endif  /*  DYNTRANS_PC_TO_POINTERS_FUNC  */  #endif  /*  DYNTRANS_PC_TO_POINTERS_FUNC  */
838    
839    
840    
841    #ifdef DYNTRANS_INIT_TABLES
842    
843    /*  forward declaration of to_be_translated and end_of_page:  */
844    static void instr(to_be_translated)(struct cpu *, struct DYNTRANS_IC *);
845    static void instr(end_of_page)(struct cpu *,struct DYNTRANS_IC *);
846    #ifdef DYNTRANS_DUALMODE_32
847    static void instr32(to_be_translated)(struct cpu *, struct DYNTRANS_IC *);
848    static void instr32(end_of_page)(struct cpu *,struct DYNTRANS_IC *);
849    #endif
850    
851    #ifdef DYNTRANS_DELAYSLOT
852    static void instr(end_of_page2)(struct cpu *,struct DYNTRANS_IC *);
853    #ifdef DYNTRANS_DUALMODE_32
854    static void instr32(end_of_page2)(struct cpu *,struct DYNTRANS_IC *);
855    #endif
856    #endif
857    
858    /*
859     *  XXX_init_tables():
860     *
861     *  Initializes the default translation page (for newly allocated pages), and
862     *  for 64-bit emulation it also initializes 64-bit dummy tables and pointers.
863     */
864    void DYNTRANS_INIT_TABLES(struct cpu *cpu)
865    {
866    #ifndef MODE32
867            struct DYNTRANS_L2_64_TABLE *dummy_l2;
868            struct DYNTRANS_L3_64_TABLE *dummy_l3;
869            int x1, x2;
870    #endif
871            int i;
872            struct DYNTRANS_TC_PHYSPAGE *ppp = malloc(sizeof(
873                struct DYNTRANS_TC_PHYSPAGE));
874    
875            if (ppp == NULL) {
876                    fprintf(stderr, "out of memory\n");
877                    exit(1);
878            }
879    
880            ppp->next_ofs = 0;
881            ppp->translations = 0;
882            /*  ppp->physaddr is filled in by the page allocator  */
883    
884            for (i=0; i<DYNTRANS_IC_ENTRIES_PER_PAGE; i++) {
885                    ppp->ics[i].f =
886    #ifdef DYNTRANS_DUALMODE_32
887                        cpu->is_32bit? instr32(to_be_translated) :
888    #endif
889                        instr(to_be_translated);
890    #ifdef DYNTRANS_VARIABLE_INSTRUCTION_LENGTH
891                    ppp->ics[i].arg[0] = 0;
892    #endif
893            }
894    
895            /*  End-of-page:  */
896            ppp->ics[DYNTRANS_IC_ENTRIES_PER_PAGE + 0].f =
897    #ifdef DYNTRANS_DUALMODE_32
898                cpu->is_32bit? instr32(end_of_page) :
899    #endif
900                instr(end_of_page);
901    
902    #ifdef DYNTRANS_VARIABLE_INSTRUCTION_LENGTH
903            ppp->ics[DYNTRANS_IC_ENTRIES_PER_PAGE + 0].arg[0] = 0;
904    #endif
905    
906            /*  End-of-page-2, for delay-slot architectures:  */
907    #ifdef DYNTRANS_DELAYSLOT
908            ppp->ics[DYNTRANS_IC_ENTRIES_PER_PAGE + 1].f =
909    #ifdef DYNTRANS_DUALMODE_32
910                cpu->is_32bit? instr32(end_of_page2) :
911    #endif
912                instr(end_of_page2);
913    #endif
914    
915            cpu->cd.DYNTRANS_ARCH.physpage_template = ppp;
916    
917    
918            /*  Prepare 64-bit virtual address translation tables:  */
919    #ifndef MODE32
920            if (cpu->is_32bit)
921                    return;
922    
923            dummy_l2 = zeroed_alloc(sizeof(struct DYNTRANS_L2_64_TABLE));
924            dummy_l3 = zeroed_alloc(sizeof(struct DYNTRANS_L3_64_TABLE));
925    
926            cpu->cd.DYNTRANS_ARCH.l2_64_dummy = dummy_l2;
927            cpu->cd.DYNTRANS_ARCH.l3_64_dummy = dummy_l3;
928    
929            for (x1 = 0; x1 < (1 << DYNTRANS_L1N); x1 ++)
930                    cpu->cd.DYNTRANS_ARCH.l1_64[x1] = dummy_l2;
931    
932            for (x2 = 0; x2 < (1 << DYNTRANS_L2N); x2 ++)
933                    dummy_l2->l3[x2] = dummy_l3;
934    #endif
935    }
936    #endif  /*  DYNTRANS_INIT_TABLES  */
937    
938    
939    
940  #ifdef DYNTRANS_INVAL_ENTRY  #ifdef DYNTRANS_INVAL_ENTRY
941  /*  /*
942   *  XXX_invalidate_tlb_entry():   *  XXX_invalidate_tlb_entry():
# Line 785  static void DYNTRANS_INVALIDATE_TLB_ENTR Line 967  static void DYNTRANS_INVALIDATE_TLB_ENTR
967                      (int)vaddr_page);  */                      (int)vaddr_page);  */
968                  cpu->cd.DYNTRANS_ARCH.host_store[index] = NULL;                  cpu->cd.DYNTRANS_ARCH.host_store[index] = NULL;
969          } else {          } else {
970                    int tlbi = cpu->cd.DYNTRANS_ARCH.vaddr_to_tlbindex[index];
971                  cpu->cd.DYNTRANS_ARCH.host_load[index] = NULL;                  cpu->cd.DYNTRANS_ARCH.host_load[index] = NULL;
972                  cpu->cd.DYNTRANS_ARCH.host_store[index] = NULL;                  cpu->cd.DYNTRANS_ARCH.host_store[index] = NULL;
973                  cpu->cd.DYNTRANS_ARCH.phys_addr[index] = 0;                  cpu->cd.DYNTRANS_ARCH.phys_addr[index] = 0;
974                  cpu->cd.DYNTRANS_ARCH.phys_page[index] = NULL;                  cpu->cd.DYNTRANS_ARCH.phys_page[index] = NULL;
975                    if (tlbi > 0)
976                            cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[tlbi-1].valid = 0;
977                  cpu->cd.DYNTRANS_ARCH.vaddr_to_tlbindex[index] = 0;                  cpu->cd.DYNTRANS_ARCH.vaddr_to_tlbindex[index] = 0;
978          }          }
979  #else  #else
980          /*  2-level:  */          const uint32_t mask1 = (1 << DYNTRANS_L1N) - 1;
981  #ifdef DYNTRANS_ALPHA          const uint32_t mask2 = (1 << DYNTRANS_L2N) - 1;
982          struct alpha_vph_page *vph_p;          const uint32_t mask3 = (1 << DYNTRANS_L3N) - 1;
983          uint32_t a, b;          uint32_t x1, x2, x3;
984          int kernel = 0;          struct DYNTRANS_L2_64_TABLE *l2;
985            struct DYNTRANS_L3_64_TABLE *l3;
986          a = (vaddr_page >> ALPHA_LEVEL0_SHIFT) & (ALPHA_LEVEL0 - 1);  
987          b = (vaddr_page >> ALPHA_LEVEL1_SHIFT) & (ALPHA_LEVEL1 - 1);          x1 = (vaddr_page >> (64-DYNTRANS_L1N)) & mask1;
988          if ((vaddr_page >> ALPHA_TOPSHIFT) == ALPHA_TOP_KERNEL) {          x2 = (vaddr_page >> (64-DYNTRANS_L1N-DYNTRANS_L2N)) & mask2;
989                  vph_p = cpu->cd.alpha.vph_table0_kernel[a];          x3 = (vaddr_page >> (64-DYNTRANS_L1N-DYNTRANS_L2N-DYNTRANS_L3N))& mask3;
                 kernel = 1;  
         } else  
                 vph_p = cpu->cd.alpha.vph_table0[a];  
990    
991          if (vph_p == cpu->cd.alpha.vph_default_page) {          l2 = cpu->cd.DYNTRANS_ARCH.l1_64[x1];
992                  fatal("alpha_invalidate_tlb_entry(): huh? Problem 1.\n");          if (l2 == cpu->cd.DYNTRANS_ARCH.l2_64_dummy)
993                  exit(1);                  return;
994          }  
995            l3 = l2->l3[x2];
996            if (l3 == cpu->cd.DYNTRANS_ARCH.l3_64_dummy)
997                    return;
998    
999          if (flags & JUST_MARK_AS_NON_WRITABLE) {          if (flags & JUST_MARK_AS_NON_WRITABLE) {
1000                  vph_p->host_store[b] = NULL;                  l3->host_store[x3] = NULL;
1001                  return;                  return;
1002          }          }
1003          vph_p->host_load[b] = NULL;  
1004          vph_p->host_store[b] = NULL;  #ifdef BUGHUNT
1005          vph_p->phys_addr[b] = 0;  
1006          vph_p->phys_page[b] = NULL;  {
1007          vph_p->refcount --;          /*  Consistency check, for debugging:  */
1008          if (vph_p->refcount < 0) {          int x1, x1b; // x2, x3;
1009                  fatal("alpha_invalidate_tlb_entry(): huh? Problem 2.\n");          struct DYNTRANS_L2_64_TABLE *l2;
1010            //struct DYNTRANS_L3_64_TABLE *l3;
1011    
1012            for (x1 = 0; x1 <= mask1; x1 ++) {
1013                    l2 = cpu->cd.DYNTRANS_ARCH.l1_64[x1];
1014                    if (l2 == cpu->cd.DYNTRANS_ARCH.l2_64_dummy)
1015                            continue;
1016                    /*  Make sure that this l2 isn't used more than 1 time!  */
1017                    for (x1b = 0; x1b <= mask1; x1b ++)
1018                            if (x1 != x1b &&
1019                                l2 == cpu->cd.DYNTRANS_ARCH.l1_64[x1b]) {
1020                                    fatal("L2 reuse: %p\n", l2);
1021                                    exit(1);
1022                            }
1023            }
1024    }
1025    
1026    /*  Count how many pages are actually in use:  */
1027    {
1028            int n=0, i;
1029            for (i=0; i<=mask3; i++)
1030                    if (l3->vaddr_to_tlbindex[i])
1031                            n++;
1032            if (n != l3->refcount) {
1033                    printf("Z: %i in use, but refcount = %i!\n", n, l3->refcount);
1034                  exit(1);                  exit(1);
1035          }          }
1036          if (vph_p->refcount == 0) {  
1037                  vph_p->next = cpu->cd.alpha.vph_next_free_page;          n = 0;
1038                  cpu->cd.alpha.vph_next_free_page = vph_p;          for (i=0; i<=mask3; i++)
1039                  if (kernel)                  if (l3->host_load[i] != NULL)
1040                          cpu->cd.alpha.vph_table0_kernel[a] =                          n++;
1041                              cpu->cd.alpha.vph_default_page;          if (n != l3->refcount) {
1042                  else                  printf("ZHL: %i in use, but refcount = %i!\n", n, l3->refcount);
1043                          cpu->cd.alpha.vph_table0[a] =                  exit(1);
1044                              cpu->cd.alpha.vph_default_page;          }
1045    }
1046    #endif
1047    
1048            l3->host_load[x3] = NULL;
1049            l3->host_store[x3] = NULL;
1050            l3->phys_addr[x3] = 0;
1051            l3->phys_page[x3] = NULL;
1052            if (l3->vaddr_to_tlbindex[x3] != 0) {
1053                    cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[
1054                        l3->vaddr_to_tlbindex[x3] - 1].valid = 0;
1055                    l3->refcount --;
1056            }
1057            l3->vaddr_to_tlbindex[x3] = 0;
1058    
1059            if (l3->refcount < 0) {
1060                    fatal("xxx_invalidate_tlb_entry(): huh? Refcount bug.\n");
1061                    exit(1);
1062            }
1063    
1064            if (l3->refcount == 0) {
1065                    l3->next = cpu->cd.DYNTRANS_ARCH.next_free_l3;
1066                    cpu->cd.DYNTRANS_ARCH.next_free_l3 = l3;
1067                    l2->l3[x2] = cpu->cd.DYNTRANS_ARCH.l3_64_dummy;
1068    
1069    #ifdef BUGHUNT
1070    /*  Make sure that we're placing a CLEAN page on the
1071        freelist:  */
1072    {
1073            int i;
1074            for (i=0; i<=mask3; i++)
1075                    if (l3->host_load[i] != NULL) {
1076                            fatal("TRYING TO RETURN A NON-CLEAN L3 PAGE!\n");
1077                            exit(1);
1078                    }
1079    }
1080    #endif
1081                    l2->refcount --;
1082                    if (l2->refcount < 0) {
1083                            fatal("xxx_invalidate_tlb_entry(): Refcount bug L2.\n");
1084                            exit(1);
1085                    }
1086                    if (l2->refcount == 0) {
1087                            l2->next = cpu->cd.DYNTRANS_ARCH.next_free_l2;
1088                            cpu->cd.DYNTRANS_ARCH.next_free_l2 = l2;
1089                            cpu->cd.DYNTRANS_ARCH.l1_64[x1] =
1090                                cpu->cd.DYNTRANS_ARCH.l2_64_dummy;
1091                    }
1092          }          }
 #else   /*  !DYNTRANS_ALPHA  */  
         fatal("Not yet for non-1-level, non-Alpha\n");  
 #endif  /*  !DYNTRANS_ALPHA  */  
1093  #endif  #endif
1094  }  }
1095  #endif  #endif
# Line 966  void DYNTRANS_INVALIDATE_TC_CODE(struct Line 1219  void DYNTRANS_INVALIDATE_TC_CODE(struct
1219                  struct DYNTRANS_TC_PHYSPAGE *ppp, *prev_ppp;                  struct DYNTRANS_TC_PHYSPAGE *ppp, *prev_ppp;
1220    
1221                  pagenr = DYNTRANS_ADDR_TO_PAGENR(addr);                  pagenr = DYNTRANS_ADDR_TO_PAGENR(addr);
   
 #ifdef MODE32  
                 /*  If this page isn't marked as having any translations,  
                     then return immediately.  */  
                 if (!(cpu->cd.DYNTRANS_ARCH.phystranslation[pagenr >> 5]  
                     & 1 << (pagenr & 31)))  
                         return;  
                 /*  Remove the mark:  */  
                 cpu->cd.DYNTRANS_ARCH.phystranslation[pagenr >> 5] &=  
                     ~ (1 << (pagenr & 31));  
 #endif  
   
1222                  table_index = PAGENR_TO_TABLE_INDEX(pagenr);                  table_index = PAGENR_TO_TABLE_INDEX(pagenr);
1223    
1224                  physpage_entryp = &(((uint32_t *)cpu->                  physpage_entryp = &(((uint32_t *)cpu->
# Line 990  void DYNTRANS_INVALIDATE_TC_CODE(struct Line 1231  void DYNTRANS_INVALIDATE_TC_CODE(struct
1231                          prev_ppp = ppp;                          prev_ppp = ppp;
1232                          ppp = (struct DYNTRANS_TC_PHYSPAGE *)                          ppp = (struct DYNTRANS_TC_PHYSPAGE *)
1233                              (cpu->translation_cache + physpage_ofs);                              (cpu->translation_cache + physpage_ofs);
1234    
1235                          /*  If we found the page in the cache,                          /*  If we found the page in the cache,
1236                              then we're done:  */                              then we're done:  */
1237                          if (ppp->physaddr == addr)                          if (ppp->physaddr == addr)
1238                                  break;                                  break;
1239    
1240                          /*  Try the next page in the chain:  */                          /*  Try the next page in the chain:  */
1241                          physpage_ofs = ppp->next_ofs;                          physpage_ofs = ppp->next_ofs;
1242                  }                  }
# Line 1001  void DYNTRANS_INVALIDATE_TC_CODE(struct Line 1244  void DYNTRANS_INVALIDATE_TC_CODE(struct
1244                  if (physpage_ofs == 0)                  if (physpage_ofs == 0)
1245                          ppp = NULL;                          ppp = NULL;
1246    
1247  #if 1  #if 0
1248                  /*                  /*
1249                   *  "Bypass" the page, removing it from the code cache.                   *  "Bypass" the page, removing it from the code cache.
1250                   *                   *
# Line 1023  void DYNTRANS_INVALIDATE_TC_CODE(struct Line 1266  void DYNTRANS_INVALIDATE_TC_CODE(struct
1266                   *  it might be faster since we don't risk wasting cache                   *  it might be faster since we don't risk wasting cache
1267                   *  memory as quickly (which would force unnecessary Restarts).                   *  memory as quickly (which would force unnecessary Restarts).
1268                   */                   */
1269                  if (ppp != NULL) {                  if (ppp != NULL && ppp->translations != 0) {
1270                          /*  TODO: Is this faster than copying an entire                          uint32_t x = ppp->translations; /*  TODO:
1271                              template page?  */                                  urk Should be same type as ppp->translations */
1272                          int i;                          int i, j, n, m;
1273                          for (i=0; i<DYNTRANS_IC_ENTRIES_PER_PAGE; i++)                          n = 8 * sizeof(x);
1274                                  ppp->ics[i].f =                          m = DYNTRANS_IC_ENTRIES_PER_PAGE / n;
1275    
1276                            for (i=0; i<n; i++) {
1277                                    if (x & 1) {
1278                                            for (j=0; j<m; j++)
1279                                                    ppp->ics[i*m + j].f =
1280  #ifdef DYNTRANS_DUALMODE_32  #ifdef DYNTRANS_DUALMODE_32
1281                                      cpu->is_32bit? instr32(to_be_translated) :                                                      cpu->is_32bit?
1282                                                        instr32(to_be_translated) :
1283  #endif  #endif
1284                                      instr(to_be_translated);                                                      instr(to_be_translated);
1285                                    }
1286    
1287                                    x >>= 1;
1288                            }
1289    
1290                            ppp->translations = 0;
1291                  }                  }
1292  #endif  #endif
1293          }          }
1294    
1295          /*  Invalidate entries (NOTE: only code entries) in the VPH table:  */          /*  Invalidate entries in the VPH table:  */
1296          for (r = DYNTRANS_MAX_VPH_TLB_ENTRIES/2;          for (r = 0; r < DYNTRANS_MAX_VPH_TLB_ENTRIES; r ++) {
              r < DYNTRANS_MAX_VPH_TLB_ENTRIES; r ++) {  
1297                  if (cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].valid) {                  if (cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].valid) {
1298                          vaddr_page = cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r]                          vaddr_page = cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r]
1299                              .vaddr_page & ~(DYNTRANS_PAGESIZE-1);                              .vaddr_page & ~(DYNTRANS_PAGESIZE-1);
# Line 1053  void DYNTRANS_INVALIDATE_TC_CODE(struct Line 1307  void DYNTRANS_INVALIDATE_TC_CODE(struct
1307                                  uint32_t index =                                  uint32_t index =
1308                                      DYNTRANS_ADDR_TO_PAGENR(vaddr_page);                                      DYNTRANS_ADDR_TO_PAGENR(vaddr_page);
1309                                  cpu->cd.DYNTRANS_ARCH.phys_page[index] = NULL;                                  cpu->cd.DYNTRANS_ARCH.phys_page[index] = NULL;
                                 /*  Remove the mark:  */  
                                 index = DYNTRANS_ADDR_TO_PAGENR(paddr_page);  
                                 cpu->cd.DYNTRANS_ARCH.phystranslation[  
                                     index >> 5] &= ~ (1 << (index & 31));  
1310  #else  #else
1311                                  /*  2-level:  */                                  const uint32_t mask1 = (1 << DYNTRANS_L1N) - 1;
1312  #ifdef DYNTRANS_ALPHA                                  const uint32_t mask2 = (1 << DYNTRANS_L2N) - 1;
1313                                  struct alpha_vph_page *vph_p;                                  const uint32_t mask3 = (1 << DYNTRANS_L3N) - 1;
1314                                  uint32_t a, b;                                  uint32_t x1, x2, x3;
1315                                  int kernel = 0;                                  struct DYNTRANS_L2_64_TABLE *l2;
1316                                    struct DYNTRANS_L3_64_TABLE *l3;
1317                                  a = (vaddr_page >> ALPHA_LEVEL0_SHIFT)  
1318                                      & (ALPHA_LEVEL0 - 1);                                  x1 = (vaddr_page >> (64-DYNTRANS_L1N)) & mask1;
1319                                  b = (vaddr_page >> ALPHA_LEVEL1_SHIFT)                                  x2 = (vaddr_page >> (64-DYNTRANS_L1N -
1320                                      & (ALPHA_LEVEL1 - 1);                                      DYNTRANS_L2N)) & mask2;
1321                                  if ((vaddr_page >> ALPHA_TOPSHIFT) ==                                  x3 = (vaddr_page >> (64-DYNTRANS_L1N -
1322                                      ALPHA_TOP_KERNEL) {                                      DYNTRANS_L2N - DYNTRANS_L3N)) & mask3;
1323                                          vph_p = cpu->cd.alpha.                                  l2 = cpu->cd.DYNTRANS_ARCH.l1_64[x1];
1324                                              vph_table0_kernel[a];                                  l3 = l2->l3[x2];
1325                                          kernel = 1;                                  l3->phys_page[x3] = NULL;
                                 } else  
                                         vph_p = cpu->cd.alpha.vph_table0[a];  
                                 vph_p->phys_page[b] = NULL;  
 #else   /*  !DYNTRANS_ALPHA  */  
                                 fatal("Not yet for non-1-level, non-Alpha\n");  
 #endif  /*  !DYNTRANS_ALPHA  */  
1326  #endif  #endif
1327                          }                          }
1328                  }                  }
# Line 1097  void DYNTRANS_INVALIDATE_TC_CODE(struct Line 1341  void DYNTRANS_INVALIDATE_TC_CODE(struct
1341  void DYNTRANS_UPDATE_TRANSLATION_TABLE(struct cpu *cpu, uint64_t vaddr_page,  void DYNTRANS_UPDATE_TRANSLATION_TABLE(struct cpu *cpu, uint64_t vaddr_page,
1342          unsigned char *host_page, int writeflag, uint64_t paddr_page)          unsigned char *host_page, int writeflag, uint64_t paddr_page)
1343  {  {
1344  #ifndef MODE32          int found, r, lowest_index, useraccess = 0;
         int64_t lowest, highest = -1;  
 #endif  
         int found, r, lowest_index, start, end, useraccess = 0;  
1345    
 #ifdef DYNTRANS_ALPHA  
         uint32_t a, b;  
         struct alpha_vph_page *vph_p;  
         int kernel = 0;  
         /*  fatal("update_translation_table(): v=0x%llx, h=%p w=%i"  
             " p=0x%llx\n", (long long)vaddr_page, host_page, writeflag,  
             (long long)paddr_page);  */  
 #else  
1346  #ifdef MODE32  #ifdef MODE32
1347          uint32_t index;          uint32_t index;
1348          vaddr_page &= 0xffffffffULL;          vaddr_page &= 0xffffffffULL;
# Line 1118  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s Line 1351  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s
1351              " p=0x%x\n", (int)vaddr_page, host_page, writeflag,              " p=0x%x\n", (int)vaddr_page, host_page, writeflag,
1352              (int)paddr_page);  */              (int)paddr_page);  */
1353  #else   /*  !MODE32  */  #else   /*  !MODE32  */
1354          fatal("Neither 32-bit nor Alpha? 2\n");          const uint32_t mask1 = (1 << DYNTRANS_L1N) - 1;
1355          exit(1);          const uint32_t mask2 = (1 << DYNTRANS_L2N) - 1;
1356  #endif          const uint32_t mask3 = (1 << DYNTRANS_L3N) - 1;
1357            uint32_t x1, x2, x3;
1358            struct DYNTRANS_L2_64_TABLE *l2;
1359            struct DYNTRANS_L3_64_TABLE *l3;
1360    
1361            /*  fatal("update_translation_table(): v=0x%016"PRIx64", h=%p w=%i"
1362                " p=0x%016"PRIx64"\n", (uint64_t)vaddr_page, host_page, writeflag,
1363                (uint64_t)paddr_page);  */
1364  #endif  #endif
1365    
1366            assert((vaddr_page & (DYNTRANS_PAGESIZE-1)) == 0);
1367            assert((paddr_page & (DYNTRANS_PAGESIZE-1)) == 0);
1368    
1369          if (writeflag & MEMORY_USER_ACCESS) {          if (writeflag & MEMORY_USER_ACCESS) {
1370                  writeflag &= ~MEMORY_USER_ACCESS;                  writeflag &= ~MEMORY_USER_ACCESS;
1371                  useraccess = 1;                  useraccess = 1;
1372          }          }
1373    
         start = 0; end = DYNTRANS_MAX_VPH_TLB_ENTRIES / 2;  
 #if 1  
         /*  Half of the TLB used for data, half for code:  */  
         if (writeflag & TLB_CODE) {  
                 writeflag &= ~TLB_CODE;  
                 start = end; end = DYNTRANS_MAX_VPH_TLB_ENTRIES;  
         }  
 #else  
         /*  Data and code entries are mixed.  */  
         end = DYNTRANS_MAX_VPH_TLB_ENTRIES;  
 #endif  
   
1374          /*  Scan the current TLB entries:  */          /*  Scan the current TLB entries:  */
1375          lowest_index = start;          lowest_index = 0;
1376    
1377  #ifdef MODE32  #ifdef MODE32
1378          /*          /*
# Line 1154  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s Line 1385  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s
1385           */           */
1386          found = (int)cpu->cd.DYNTRANS_ARCH.vaddr_to_tlbindex[          found = (int)cpu->cd.DYNTRANS_ARCH.vaddr_to_tlbindex[
1387              DYNTRANS_ADDR_TO_PAGENR(vaddr_page)] - 1;              DYNTRANS_ADDR_TO_PAGENR(vaddr_page)] - 1;
         if (found < 0) {  
                 static unsigned int x = 0;  
                 lowest_index = (x % (end-start)) + start;  
                 x ++;  
         }  
1388  #else  #else
1389          lowest = cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[0].timestamp;          x1 = (vaddr_page >> (64-DYNTRANS_L1N)) & mask1;
1390          found = -1;          x2 = (vaddr_page >> (64-DYNTRANS_L1N-DYNTRANS_L2N)) & mask2;
1391          for (r=start; r<end; r++) {          x3 = (vaddr_page >> (64-DYNTRANS_L1N-DYNTRANS_L2N-DYNTRANS_L3N))
1392                  if (cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].timestamp < lowest) {              & mask3;
1393                          lowest = cpu->cd.DYNTRANS_ARCH.  
1394                              vph_tlb_entry[r].timestamp;          l2 = cpu->cd.DYNTRANS_ARCH.l1_64[x1];
1395                          lowest_index = r;          if (l2 == cpu->cd.DYNTRANS_ARCH.l2_64_dummy)
1396                  }                  found = -1;
1397                  if (cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].timestamp > highest)          else {
1398                          highest = cpu->cd.DYNTRANS_ARCH.                  l3 = l2->l3[x2];
1399                              vph_tlb_entry[r].timestamp;                  if (l3 == cpu->cd.DYNTRANS_ARCH.l3_64_dummy)
1400                  if (cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].valid &&                          found = -1;
1401                      cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].vaddr_page ==                  else
1402                      vaddr_page) {                          found = (int)l3->vaddr_to_tlbindex[x3] - 1;
                         found = r;  
                         break;  
                 }  
1403          }          }
1404  #endif  #endif
1405    
1406          if (found < 0) {          if (found < 0) {
1407                    static unsigned int x = 0;
1408                    lowest_index = (x++) % DYNTRANS_MAX_VPH_TLB_ENTRIES;
1409            }
1410    
1411            if (found < 0) {
1412                  /*  Create the new TLB entry, overwriting the oldest one:  */                  /*  Create the new TLB entry, overwriting the oldest one:  */
1413                  r = lowest_index;                  r = lowest_index;
1414                  if (cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].valid) {                  if (cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].valid) {
# Line 1196  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s Line 1424  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s
1424                  cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].vaddr_page = vaddr_page;                  cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].vaddr_page = vaddr_page;
1425                  cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].writeflag =                  cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].writeflag =
1426                      writeflag & MEM_WRITE;                      writeflag & MEM_WRITE;
 #ifndef MODE32  
                 cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].timestamp = highest + 1;  
 #endif  
1427    
1428                  /*  Add the new translation to the table:  */                  /*  Add the new translation to the table:  */
 #ifdef DYNTRANS_ALPHA  
                 a = (vaddr_page >> ALPHA_LEVEL0_SHIFT) & (ALPHA_LEVEL0 - 1);  
                 b = (vaddr_page >> ALPHA_LEVEL1_SHIFT) & (ALPHA_LEVEL1 - 1);  
                 if ((vaddr_page >> ALPHA_TOPSHIFT) == ALPHA_TOP_KERNEL) {  
                         vph_p = cpu->cd.alpha.vph_table0_kernel[a];  
                         kernel = 1;  
                 } else  
                         vph_p = cpu->cd.alpha.vph_table0[a];  
                 if (vph_p == cpu->cd.alpha.vph_default_page) {  
                         if (cpu->cd.alpha.vph_next_free_page != NULL) {  
                                 if (kernel)  
                                         vph_p = cpu->cd.alpha.vph_table0_kernel  
                                             [a] = cpu->cd.alpha.  
                                             vph_next_free_page;  
                                 else  
                                         vph_p = cpu->cd.alpha.vph_table0[a] =  
                                             cpu->cd.alpha.vph_next_free_page;  
                                 cpu->cd.alpha.vph_next_free_page = vph_p->next;  
                         } else {  
                                 if (kernel)  
                                         vph_p = cpu->cd.alpha.vph_table0_kernel  
                                             [a] = malloc(sizeof(struct  
                                             alpha_vph_page));  
                                 else  
                                         vph_p = cpu->cd.alpha.vph_table0[a] =  
                                             malloc(sizeof(struct  
                                             alpha_vph_page));  
                                 memset(vph_p, 0, sizeof(struct alpha_vph_page));  
                         }  
                 }  
                 vph_p->refcount ++;  
                 vph_p->host_load[b] = host_page;  
                 vph_p->host_store[b] = writeflag? host_page : NULL;  
                 vph_p->phys_addr[b] = paddr_page;  
                 vph_p->phys_page[b] = NULL;  
 #else  
1429  #ifdef MODE32  #ifdef MODE32
1430                  index = DYNTRANS_ADDR_TO_PAGENR(vaddr_page);                  index = DYNTRANS_ADDR_TO_PAGENR(vaddr_page);
1431                  cpu->cd.DYNTRANS_ARCH.host_load[index] = host_page;                  cpu->cd.DYNTRANS_ARCH.host_load[index] = host_page;
# Line 1250  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s Line 1439  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s
1439                          cpu->cd.DYNTRANS_ARCH.is_userpage[index >> 5]                          cpu->cd.DYNTRANS_ARCH.is_userpage[index >> 5]
1440                              |= 1 << (index & 31);                              |= 1 << (index & 31);
1441  #endif  #endif
1442  #endif  /*  32  */  #else   /* !MODE32  */
1443  #endif  /*  !ALPHA  */                  l2 = cpu->cd.DYNTRANS_ARCH.l1_64[x1];
1444                    if (l2 == cpu->cd.DYNTRANS_ARCH.l2_64_dummy) {
1445                            if (cpu->cd.DYNTRANS_ARCH.next_free_l2 != NULL) {
1446                                    l2 = cpu->cd.DYNTRANS_ARCH.l1_64[x1] =
1447                                        cpu->cd.DYNTRANS_ARCH.next_free_l2;
1448                                    cpu->cd.DYNTRANS_ARCH.next_free_l2 = l2->next;
1449                            } else {
1450                                    int i;
1451                                    l2 = cpu->cd.DYNTRANS_ARCH.l1_64[x1] =
1452                                        malloc(sizeof(struct DYNTRANS_L2_64_TABLE));
1453                                    l2->refcount = 0;
1454                                    for (i=0; i<(1 << DYNTRANS_L2N); i++)
1455                                            l2->l3[i] = cpu->cd.DYNTRANS_ARCH.
1456                                                l3_64_dummy;
1457                            }
1458                            if (l2->refcount != 0) {
1459                                    fatal("Huh? l2 Refcount problem.\n");
1460                                    exit(1);
1461                            }
1462                    }
1463                    if (l2 == cpu->cd.DYNTRANS_ARCH.l2_64_dummy) {
1464                            fatal("INTERNAL ERROR L2 reuse\n");
1465                            exit(1);
1466                    }
1467                    l3 = l2->l3[x2];
1468                    if (l3 == cpu->cd.DYNTRANS_ARCH.l3_64_dummy) {
1469                            if (cpu->cd.DYNTRANS_ARCH.next_free_l3 != NULL) {
1470                                    l3 = l2->l3[x2] =
1471                                        cpu->cd.DYNTRANS_ARCH.next_free_l3;
1472                                    cpu->cd.DYNTRANS_ARCH.next_free_l3 = l3->next;
1473                            } else {
1474                                    l3 = l2->l3[x2] = zeroed_alloc(sizeof(
1475                                        struct DYNTRANS_L3_64_TABLE));
1476                            }
1477                            if (l3->refcount != 0) {
1478                                    fatal("Huh? l3 Refcount problem.\n");
1479                                    exit(1);
1480                            }
1481                            l2->refcount ++;
1482                    }
1483                    if (l3 == cpu->cd.DYNTRANS_ARCH.l3_64_dummy) {
1484                            fatal("INTERNAL ERROR L3 reuse\n");
1485                            exit(1);
1486                    }
1487    
1488                    l3->host_load[x3] = host_page;
1489                    l3->host_store[x3] = writeflag? host_page : NULL;
1490                    l3->phys_addr[x3] = paddr_page;
1491                    l3->phys_page[x3] = NULL;
1492                    l3->vaddr_to_tlbindex[x3] = r + 1;
1493                    l3->refcount ++;
1494    
1495    #ifdef BUGHUNT
1496    /*  Count how many pages are actually in use:  */
1497    {
1498            int n=0, i;
1499            for (i=0; i<=mask3; i++)
1500                    if (l3->vaddr_to_tlbindex[i])
1501                            n++;
1502            if (n != l3->refcount) {
1503                    printf("X: %i in use, but refcount = %i!\n", n, l3->refcount);
1504                    exit(1);
1505            }
1506    
1507            n = 0;
1508            for (i=0; i<=mask3; i++)
1509                    if (l3->host_load[i] != NULL)
1510                            n++;
1511            if (n != l3->refcount) {
1512                    printf("XHL: %i in use, but refcount = %i!\n", n, l3->refcount);
1513                    exit(1);
1514            }
1515    }
1516    #endif
1517    
1518    #endif  /* !MODE32  */
1519          } else {          } else {
1520                  /*                  /*
1521                   *  The translation was already in the TLB.                   *  The translation was already in the TLB.
# Line 1260  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s Line 1524  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s
1524                   *      Writeflag = MEM_DOWNGRADE: Downgrade to readonly.                   *      Writeflag = MEM_DOWNGRADE: Downgrade to readonly.
1525                   */                   */
1526                  r = found;                  r = found;
 #ifndef MODE32  
                 cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].timestamp = highest + 1;  
 #endif  
1527                  if (writeflag & MEM_WRITE)                  if (writeflag & MEM_WRITE)
1528                          cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].writeflag = 1;                          cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].writeflag = 1;
1529                  if (writeflag & MEM_DOWNGRADE)                  if (writeflag & MEM_DOWNGRADE)
1530                          cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].writeflag = 0;                          cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].writeflag = 0;
 #ifdef DYNTRANS_ALPHA  
                 a = (vaddr_page >> ALPHA_LEVEL0_SHIFT) & (ALPHA_LEVEL0 - 1);  
                 b = (vaddr_page >> ALPHA_LEVEL1_SHIFT) & (ALPHA_LEVEL1 - 1);  
                 if ((vaddr_page >> ALPHA_TOPSHIFT) == ALPHA_TOP_KERNEL) {  
                         vph_p = cpu->cd.alpha.vph_table0_kernel[a];  
                         kernel = 1;  
                 } else  
                         vph_p = cpu->cd.alpha.vph_table0[a];  
                 vph_p->phys_page[b] = NULL;  
                 if (vph_p->phys_addr[b] == paddr_page) {  
                         if (writeflag & MEM_WRITE)  
                                 vph_p->host_store[b] = host_page;  
                         if (writeflag & MEM_DOWNGRADE)  
                                 vph_p->host_store[b] = NULL;  
                 } else {  
                         /*  Change the entire physical/host mapping:  */  
                         vph_p->host_load[b] = host_page;  
                         vph_p->host_store[b] = writeflag? host_page : NULL;  
                         vph_p->phys_addr[b] = paddr_page;  
                 }  
 #else  
1531  #ifdef MODE32  #ifdef MODE32
1532                  index = DYNTRANS_ADDR_TO_PAGENR(vaddr_page);                  index = DYNTRANS_ADDR_TO_PAGENR(vaddr_page);
1533                  cpu->cd.DYNTRANS_ARCH.phys_page[index] = NULL;                  cpu->cd.DYNTRANS_ARCH.phys_page[index] = NULL;
# Line 1310  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s Line 1550  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s
1550                              writeflag? host_page : NULL;                              writeflag? host_page : NULL;
1551                          cpu->cd.DYNTRANS_ARCH.phys_addr[index] = paddr_page;                          cpu->cd.DYNTRANS_ARCH.phys_addr[index] = paddr_page;
1552                  }                  }
1553  #endif  /*  32  */  #else   /*  !MODE32  */
1554  #endif  /*  !ALPHA  */                  x1 = (vaddr_page >> (64-DYNTRANS_L1N)) & mask1;
1555                    x2 = (vaddr_page >> (64-DYNTRANS_L1N-DYNTRANS_L2N)) & mask2;
1556                    x3 = (vaddr_page >> (64-DYNTRANS_L1N-DYNTRANS_L2N-DYNTRANS_L3N))
1557                        & mask3;
1558                    l2 = cpu->cd.DYNTRANS_ARCH.l1_64[x1];
1559                    l3 = l2->l3[x2];
1560                    if (l3->phys_addr[x3] == paddr_page) {
1561                            if (writeflag & MEM_WRITE)
1562                                    l3->host_store[x3] = host_page;
1563                            if (writeflag & MEM_DOWNGRADE)
1564                                    l3->host_store[x3] = NULL;
1565                    } else {
1566                            /*  Change the entire physical/host mapping:  */
1567    printf("HOST LOAD 2 set to %p\n", host_page);
1568                            l3->host_load[x3] = host_page;
1569                            l3->host_store[x3] = writeflag? host_page : NULL;
1570                            l3->phys_addr[x3] = paddr_page;
1571                    }
1572    
1573    #ifdef BUGHUNT
1574    /*  Count how many pages are actually in use:  */
1575    {
1576            int n=0, i;
1577            for (i=0; i<=mask3; i++)
1578                    if (l3->vaddr_to_tlbindex[i])
1579                            n++;
1580            if (n != l3->refcount) {
1581                    printf("Y: %i in use, but refcount = %i!\n", n, l3->refcount);
1582                    exit(1);
1583            }
1584    
1585            n = 0;
1586            for (i=0; i<=mask3; i++)
1587                    if (l3->host_load[i] != NULL)
1588                            n++;
1589            if (n != l3->refcount) {
1590                    printf("YHL: %i in use, but refcount = %i!\n", n, l3->refcount);
1591                    printf("Entry r = %i\n", r);
1592                    printf("Valid = %i\n",
1593    cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].valid);
1594                    exit(1);
1595            }
1596    }
1597    #endif
1598    
1599    #endif  /*  !MODE32  */
1600          }          }
1601  }  }
1602  #endif  /*  DYNTRANS_UPDATE_TRANSLATION_TABLE  */  #endif  /*  DYNTRANS_UPDATE_TRANSLATION_TABLE  */
# Line 1325  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s Line 1610  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s
1610           *  Check for breakpoints.           *  Check for breakpoints.
1611           */           */
1612          if (!single_step_breakpoint) {          if (!single_step_breakpoint) {
1613  #ifdef MODE32                  MODE_uint_t curpc = cpu->pc;
                 uint32_t curpc = cpu->pc;  
 #else  
                 uint64_t curpc = cpu->pc;  
 #endif  
1614                  int i;                  int i;
1615                  for (i=0; i<cpu->machine->n_breakpoints; i++)                  for (i=0; i<cpu->machine->n_breakpoints; i++)
1616                          if (curpc ==                          if (curpc == (MODE_uint_t)
 #ifdef MODE32  
                             (uint32_t)  
 #endif  
1617                              cpu->machine->breakpoint_addr[i]) {                              cpu->machine->breakpoint_addr[i]) {
1618                                  if (!cpu->machine->instruction_trace) {                                  if (!cpu->machine->instruction_trace) {
1619                                          int old_quiet_mode = quiet_mode;                                          int old_quiet_mode = quiet_mode;
1620                                          quiet_mode = 0;                                          quiet_mode = 0;
1621                                          DISASSEMBLE(cpu, ib, 1, 0, 0);                                          DISASSEMBLE(cpu, ib, 1, 0);
1622                                          quiet_mode = old_quiet_mode;                                          quiet_mode = old_quiet_mode;
1623                                  }                                  }
1624                                  fatal("BREAKPOINT: pc = 0x%llx\n(The "                                  fatal("BREAKPOINT: pc = 0x%"PRIx64"\n(The "
1625                                      "instruction has not yet executed.)\n",                                      "instruction has not yet executed.)\n",
1626                                      (long long)cpu->pc);                                      (uint64_t)cpu->pc);
1627  #ifdef DYNTRANS_DELAYSLOT  #ifdef DYNTRANS_DELAYSLOT
1628                                  if (cpu->cd.DYNTRANS_ARCH.delay_slot !=                                  if (cpu->delay_slot != NOT_DELAYED)
                                     NOT_DELAYED)  
1629                                          fatal("ERROR! Breakpoint in a delay"                                          fatal("ERROR! Breakpoint in a delay"
1630                                              " slot! Not yet supported.\n");                                              " slot! Not yet supported.\n");
1631  #endif  #endif
1632                                  single_step_breakpoint = 1;                                  single_step_breakpoint = 1;
1633                                  single_step = 1;                                  single_step = ENTER_SINGLE_STEPPING;
1634                                  goto stop_running_translated;                                  goto stop_running_translated;
1635                          }                          }
1636          }          }
# Line 1365  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s Line 1642  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s
1642    
1643  #ifdef DYNTRANS_TO_BE_TRANSLATED_TAIL  #ifdef DYNTRANS_TO_BE_TRANSLATED_TAIL
1644          /*          /*
1645           *  If we end up here, then an instruction was translated.           *  If we end up here, then an instruction was translated. Let's mark
1646           *  Mark the page as containing a translation.           *  the page as containing a translation at this part of the page.
          *  
          *  (Special case for 32-bit mode: set the corresponding bit in the  
          *  phystranslation[] array.)  
1647           */           */
 #ifdef MODE32  
         if (!(cpu->cd.DYNTRANS_ARCH.cur_physpage->flags & TRANSLATIONS)) {  
                 uint32_t index = DYNTRANS_ADDR_TO_PAGENR((uint32_t)addr);  
                 cpu->cd.DYNTRANS_ARCH.phystranslation[index >> 5] |=  
                     (1 << (index & 31));  
         }  
 #endif  
         cpu->cd.DYNTRANS_ARCH.cur_physpage->flags |= TRANSLATIONS;  
   
   
 #ifdef DYNTRANS_BACKEND  
         /*  
          *  "Empty"/simple native dyntrans backend stuff:  
          *  
          *  1) If no translation is currently being done, but the translated  
          *     instruction was simple enough, then let's start making a new  
          *     native translation block.  
          *  
          *  2) If a native translation block is currently being constructed,  
          *     but this instruction wasn't simple enough, then end the block  
          *     (without including this instruction).  
          *  
          *  3) If a native translation block is currently being constructed,  
          *     and this is a simple instruction, then add it.  
          */  
         if (simple && cpu->translation_context.p == NULL &&  
             dyntrans_backend_enable) {  
                 size_t s = 0;  
   
                 if (cpu->translation_context.translation_buffer == NULL) {  
                         cpu->translation_context.translation_buffer =  
                             zeroed_alloc(DTB_TRANSLATION_SIZE_MAX +  
                             DTB_TRANSLATION_SIZE_MARGIN);  
                 }  
   
                 cpu->translation_context.p =  
                     cpu->translation_context.translation_buffer;  
   
                 cpu->translation_context.ic_page =  
                     cpu->cd.DYNTRANS_ARCH.cur_ic_page;  
                 cpu->translation_context.start_instr_call_index =  
                     ((size_t)ic - (size_t)cpu->cd.DYNTRANS_ARCH.cur_ic_page)  
                     / (sizeof(*ic));  
1648    
1649                  dtb_function_prologue(&cpu->translation_context, &s);          /*  Make sure cur_physpage is in synch:  */
1650                  cpu->translation_context.p += s;          cpu->cd.DYNTRANS_ARCH.cur_physpage = (void *)
1651                  cpu->translation_context.n_simple = 0;              cpu->cd.DYNTRANS_ARCH.cur_ic_page;
         }  
   
         /*  If this is not a continuation of a simple translation, then  
             stop now!  */  
         if (cpu->translation_context.ic_page != cpu->cd.DYNTRANS_ARCH.  
             cur_ic_page || ic != &cpu->cd.DYNTRANS_ARCH.cur_ic_page[  
             cpu->translation_context.start_instr_call_index +  
             cpu->translation_context.n_simple])  
                 simple = 0;  
   
         if (cpu->translation_context.p != NULL && !simple) {  
                 size_t s = 0, total;  
   
                 if (cpu->translation_context.n_simple > 1) {  
                         dtb_generate_ptr_inc(cpu, &cpu->translation_context,  
                             &s, &cpu->cd.DYNTRANS_ARCH.next_ic,  
                             (cpu->translation_context.n_simple - 1) *  
                             sizeof(*(cpu->cd.DYNTRANS_ARCH.next_ic)));  
                         cpu->translation_context.p += s;  
                 }  
   
                 dtb_function_epilogue(&cpu->translation_context, &s);  
                 cpu->translation_context.p += s;  
1652    
1653                  cpu_dtb_do_fixups(cpu);          {
1654  #if 0                  int x = addr & (DYNTRANS_PAGESIZE - 1);
1655  {                  int addr_per_translation_range = DYNTRANS_PAGESIZE / (8 *
1656  int i;                      sizeof(cpu->cd.DYNTRANS_ARCH.cur_physpage->translations));
1657  unsigned char *addr = cpu->translation_context.translation_buffer;                  x /= addr_per_translation_range;
 printf("index = %i\n", cpu->translation_context.start_instr_call_index);  
 quiet_mode = 0;  
 for (i=0; i<4*32; i+=4)  
         alpha_cpu_disassemble_instr(cpu, (unsigned char *)addr + i,  
         0, i, 0);  
 }  
 #endif  
                 total = (size_t)cpu->translation_context.p -  
                     (size_t)cpu->translation_context.translation_buffer;  
1658    
1659                  /*  Copy the translated block to the translation cache:  */                  cpu->cd.DYNTRANS_ARCH.cur_physpage->translations |= (1 << x);
                 /*  Align first:  */  
                 cpu->translation_cache_cur_ofs --;  
                 cpu->translation_cache_cur_ofs |= 31;  
                 cpu->translation_cache_cur_ofs ++;  
   
                 memcpy(cpu->translation_cache + cpu->translation_cache_cur_ofs,  
                     cpu->translation_context.translation_buffer, total);  
   
                 /*  Set the ic pointer:  */  
                 ((struct DYNTRANS_IC *)cpu->translation_context.ic_page)  
                     [cpu->translation_context.start_instr_call_index].f =  
                     (void *)  
                     (cpu->translation_cache + cpu->translation_cache_cur_ofs);  
   
                 /*  Align cur_ofs afterwards as well, just to be safe.  */  
                 cpu->translation_cache_cur_ofs += total;  
                 cpu->translation_cache_cur_ofs --;  
                 cpu->translation_cache_cur_ofs |= 31;  
                 cpu->translation_cache_cur_ofs ++;  
   
                 /*  Set the "combined instruction" flag for this page:  */  
                 cpu->cd.DYNTRANS_ARCH.cur_physpage = (void *)  
                     cpu->cd.DYNTRANS_ARCH.cur_ic_page;  
                 cpu->cd.DYNTRANS_ARCH.cur_physpage->flags |= COMBINATIONS;  
   
                 dtb_host_cacheinvalidate(0,0); /* p , size ... ); */  
   
                 cpu->translation_context.p = NULL;  
         }  
         if (cpu->translation_context.p != NULL) {  
                 size_t s = 0;  
                 dtb_generate_fcall(cpu, &cpu->translation_context,  
                     &s, (size_t)ic->f, (size_t)ic);  
                 cpu->translation_context.p += s;  
                 cpu->translation_context.n_simple ++;  
1660          }          }
 #endif  /*  DYNTRANS_BACKEND  */  
   
1661    
1662          /*          /*
1663           *  Now it is time to check for combinations of instructions that can           *  Now it is time to check for combinations of instructions that can
1664           *  be converted into a single function call.           *  be converted into a single function call.
1665           *           *
1666           *  Note: Single-stepping or instruction tracing doesn't work with           *  Note: Single-stepping or instruction tracing doesn't work with
1667           *  instruction combination.           *  instruction combination. For architectures with delay slots,
1668             *  we also ignore combinations if the delay slot is across a page
1669             *  boundary.
1670           */           */
1671          if (!single_step && !cpu->machine->instruction_trace) {          if (!single_step && !cpu->machine->instruction_trace
1672                  if (cpu->cd.DYNTRANS_ARCH.combination_check != NULL &&  #ifdef DYNTRANS_DELAYSLOT
1673                      cpu->machine->speed_tricks)              && !in_crosspage_delayslot
1674                          cpu->cd.DYNTRANS_ARCH.combination_check(cpu, ic,  #endif
1675                              addr & (DYNTRANS_PAGESIZE - 1));              && cpu->cd.DYNTRANS_ARCH.combination_check != NULL
1676                  cpu->cd.DYNTRANS_ARCH.combination_check = NULL;              && cpu->machine->allow_instruction_combinations) {
1677                    cpu->cd.DYNTRANS_ARCH.combination_check(cpu, ic,
1678                        addr & (DYNTRANS_PAGESIZE - 1));
1679            }
1680    
1681            cpu->cd.DYNTRANS_ARCH.combination_check = NULL;
1682    
1683            /*  An additional check, to catch some bugs:  */
1684            if (ic->f == (
1685    #ifdef DYNTRANS_DUALMODE_32
1686                cpu->is_32bit? instr32(to_be_translated) :
1687    #endif
1688                instr(to_be_translated))) {
1689                    fatal("INTERNAL ERROR: ic->f not set!\n");
1690                    goto bad;
1691            }
1692            if (ic->f == NULL) {
1693                    fatal("INTERNAL ERROR: ic->f == NULL!\n");
1694                    goto bad;
1695          }          }
1696    
1697          /*  ... and finally execute the translated instruction:  */          /*  ... and finally execute the translated instruction:  */
1698          if (single_step_breakpoint) {          if ((single_step_breakpoint && cpu->delay_slot == NOT_DELAYED)
1699    #ifdef DYNTRANS_DELAYSLOT
1700                || in_crosspage_delayslot
1701    #endif
1702                ) {
1703                  /*                  /*
1704                   *  Special case when single-stepping: Execute the translated                   *  Special case when single-stepping: Execute the translated
1705                   *  instruction, but then replace it with a "to be translated"                   *  instruction, but then replace it with a "to be translated"
1706                   *  directly afterwards.                   *  directly afterwards.
1707                   */                   */
1708                  single_step_breakpoint = 0;                  single_step_breakpoint = 0;
1709    #ifdef DYNTRANS_VARIABLE_INSTRUCTION_LENGTH
1710                    cpu->cd.DYNTRANS_ARCH.next_ic = ic + ic->arg[0];
1711    #endif
1712                  ic->f(cpu, ic);                  ic->f(cpu, ic);
1713                  ic->f =                  ic->f =
1714  #ifdef DYNTRANS_DUALMODE_32  #ifdef DYNTRANS_DUALMODE_32
1715                      cpu->is_32bit? instr32(to_be_translated) :                      cpu->is_32bit? instr32(to_be_translated) :
1716  #endif  #endif
1717                      instr(to_be_translated);                      instr(to_be_translated);
1718          } else  #ifdef DYNTRANS_VARIABLE_INSTRUCTION_LENGTH
1719                    ic->arg[0] = 0;
1720    #endif
1721            } else {
1722    #ifdef DYNTRANS_VARIABLE_INSTRUCTION_LENGTH
1723                    cpu->cd.DYNTRANS_ARCH.next_ic = ic + ic->arg[0];
1724    
1725                    /*  Additional check, for variable length ISAs:  */
1726                    if (ic->arg[0] == 0) {
1727                            fatal("INTERNAL ERROR: instr len = 0!\n");
1728                            goto bad;
1729                    }
1730    #endif
1731    
1732                    /*  Finally finally :-), execute the instruction:  */
1733                  ic->f(cpu, ic);                  ic->f(cpu, ic);
1734            }
1735    
1736          return;          return;
1737    
# Line 1541  bad:   /* Line 1745  bad:   /*
1745    
1746          if (cpu->machine->instruction_trace)          if (cpu->machine->instruction_trace)
1747  #ifdef MODE32  #ifdef MODE32
1748                  fatal(" at 0x%x\n", (int)cpu->pc);                  fatal(" at 0x%"PRIx32"\n", (uint32_t)cpu->pc);
1749  #else  #else
1750                  fatal(" at 0x%llx\n", (long long)cpu->pc);                  fatal(" at 0x%"PRIx64"\n", (uint64_t)cpu->pc);
1751  #endif  #endif
1752          else {          else {
1753                  fatal(":\n");                  fatal(":\n");
1754                  DISASSEMBLE(cpu, ib, 1, 0, 0);                  DISASSEMBLE(cpu, ib, 1, 0);
1755          }          }
1756    
1757          cpu->running = 0;          cpu->running = 0;
1758          cpu->dead = 1;  
1759            /*  Note: Single-stepping can jump here.  */
1760  stop_running_translated:  stop_running_translated:
1761    
1762          debugger_n_steps_left_before_interaction = 0;          debugger_n_steps_left_before_interaction = 0;
1763          cpu->running_translated = 0;  
1764          ic = cpu->cd.DYNTRANS_ARCH.next_ic = &nothing_call;          ic = cpu->cd.DYNTRANS_ARCH.next_ic = &nothing_call;
1765          cpu->cd.DYNTRANS_ARCH.next_ic ++;          cpu->cd.DYNTRANS_ARCH.next_ic ++;
1766    
1767          /*  Execute the "nothing" instruction:  */          /*  Execute the "nothing" instruction:  */
1768          ic->f(cpu, ic);          ic->f(cpu, ic);
1769    
1770  #endif  /*  DYNTRANS_TO_BE_TRANSLATED_TAIL  */  #endif  /*  DYNTRANS_TO_BE_TRANSLATED_TAIL  */
1771    

Legend:
Removed from v.22  
changed lines
  Added in v.30

  ViewVC Help
Powered by ViewVC 1.1.26