/[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 26 by dpavlin, Mon Oct 8 16:20:10 2007 UTC revision 34 by dpavlin, Mon Oct 8 16:21:17 2007 UTC
# Line 1  Line 1 
1  /*  /*
2   *  Copyright (C) 2005-2006  Anders Gavare.  All rights reserved.   *  Copyright (C) 2005-2007  Anders Gavare.  All rights reserved.
3   *   *
4   *  Redistribution and use in source and binary forms, with or without   *  Redistribution and use in source and binary forms, with or without
5   *  modification, are permitted provided that the following conditions are met:   *  modification, are permitted provided that the following conditions are met:
# Line 25  Line 25 
25   *  SUCH DAMAGE.   *  SUCH DAMAGE.
26   *   *
27   *   *
28   *  $Id: cpu_dyntrans.c,v 1.104 2006/06/25 00:15:44 debug Exp $   *  $Id: cpu_dyntrans.c,v 1.142 2007/02/11 10:47:31 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;  
47    
48  #if 0          if (cpu->machine->statistics_file == NULL) {
49          /*  Use the physical address:  */                  fatal("statistics gathering with no filename set is"
50          cpu->cd.DYNTRANS_ARCH.cur_physpage = (void *)                      " meaningless\n");
51              cpu->cd.DYNTRANS_ARCH.cur_ic_page;                  return;
52          a = cpu->cd.DYNTRANS_ARCH.cur_physpage->physaddr;          }
 #else  
         /*  Use the PC (virtual address):  */  
         a = cpu->pc;  
 #endif  
53    
54          a &= ~((DYNTRANS_IC_ENTRIES_PER_PAGE-1) <<          buf[0] = '\0';
             DYNTRANS_INSTR_ALIGNMENT_SHIFT);  
         a += low_pc << DYNTRANS_INSTR_ALIGNMENT_SHIFT;  
55    
56          /*          while ((ch = cpu->machine->statistics_fields[i]) != '\0') {
57           *  TODO: Everything below this line should be cleaned up :-)                  if (i != 0)
58           */                          strlcat(buf, " ", sizeof(buf));
59  a &= 0x03ffffff;  
60  {                  switch (ch) {
61          static long long *array = NULL;                  case 'i':
62          static char *array_16kpage_in_use = NULL;                          snprintf(buf + strlen(buf), sizeof(buf),
63          static int n = 0;                              "%p", (void *)ic->f);
64          a >>= DYNTRANS_INSTR_ALIGNMENT_SHIFT;                          break;
65          if (array == NULL)                  case 'p':
66                  array = zeroed_alloc(sizeof(long long) * 16384*1024);                          /*  Physical program counter address:  */
67          if (array_16kpage_in_use == NULL)                          /*  (low_pc must be within the page!)  */
68                  array_16kpage_in_use = zeroed_alloc(sizeof(char) * 1024);                          if (low_pc < 0 ||
69          a &= (16384*1024-1);                              low_pc >= DYNTRANS_IC_ENTRIES_PER_PAGE)
70          array[a] ++;                                  strlcat(buf, "-", sizeof(buf));
71          array_16kpage_in_use[a / 16384] = 1;                          cpu->cd.DYNTRANS_ARCH.cur_physpage = (void *)
72          n++;                              cpu->cd.DYNTRANS_ARCH.cur_ic_page;
73          if ((n & 0x3fffffff) == 0) {                          a = cpu->cd.DYNTRANS_ARCH.cur_physpage->physaddr;
74                  FILE *f = fopen("statistics.out", "w");                          a &= ~((DYNTRANS_IC_ENTRIES_PER_PAGE-1) <<
75                  int i, j;                              DYNTRANS_INSTR_ALIGNMENT_SHIFT);
76                  printf("Saving statistics... "); fflush(stdout);                          a += low_pc << DYNTRANS_INSTR_ALIGNMENT_SHIFT;
77                  for (i=0; i<1024; i++)                          if (cpu->is_32bit)
78                          if (array_16kpage_in_use[i]) {                                  snprintf(buf + strlen(buf), sizeof(buf),
79                                  for (j=0; j<16384; j++)                                      "0x%016"PRIx32, (uint32_t)a);
80                                          if (array[i*16384 + j] > 0)                          else
81                                                  fprintf(f, "%lli\t"                                  snprintf(buf + strlen(buf), sizeof(buf),
82                                                      "0x%016"PRIx64"\n",                                      "0x%016"PRIx64, (uint64_t)a);
83                                                      (uint64_t)array[i*16384+j],                          break;
84                                                      (uint64_t)((i*16384+j) <<                  case 'v':
85                                              DYNTRANS_INSTR_ALIGNMENT_SHIFT));                          /*  Virtual program counter address:  */
86                          }                          /*  (low_pc inside the page, or in a delay slot)  */
87                  fclose(f);                          if (low_pc < 0 ||
88                  printf("n=0x%08x\n", n);                              low_pc >= DYNTRANS_IC_ENTRIES_PER_PAGE + 2)
89                                    strlcat(buf, "-", sizeof(buf));
90                            a = cpu->pc;
91                            a &= ~((DYNTRANS_IC_ENTRIES_PER_PAGE-1) <<
92                                DYNTRANS_INSTR_ALIGNMENT_SHIFT);
93                            a += low_pc << DYNTRANS_INSTR_ALIGNMENT_SHIFT;
94                            if (cpu->is_32bit)
95                                    snprintf(buf + strlen(buf), sizeof(buf),
96                                        "0x%016"PRIx32, (uint32_t)a);
97                            else
98                                    snprintf(buf + strlen(buf), sizeof(buf),
99                                        "0x%016"PRIx64, (uint64_t)a);
100                            break;
101                    }
102                    i++;
103          }          }
104    
105            fprintf(cpu->machine->statistics_file, "%s\n", buf);
106  }  }
 }  
 #endif  /*  PC statistics  */  
107    
108    
109  #define S               gather_statistics(cpu)  #define S               gather_statistics(cpu)
# Line 170  a &= 0x03ffffff; Line 162  a &= 0x03ffffff;
162          ic = cpu->cd.DYNTRANS_ARCH.next_ic ++; ic->f(cpu, ic); }          ic = cpu->cd.DYNTRANS_ARCH.next_ic ++; ic->f(cpu, ic); }
163  */  */
164  #endif  #endif
165    #endif  /*  STATIC STUFF  */
166    
167    
168    
169    #ifdef  DYNTRANS_RUN_INSTR
170  /*  /*
171   *  XXX_cpu_run_instr():   *  XXX_run_instr():
172   *   *
173   *  Execute one or more instructions on a specific CPU, using dyntrans.   *  Execute one or more instructions on a specific CPU, using dyntrans.
174     *  (For dualmode archs, this function is included twice.)
175   *   *
176   *  Return value is the number of instructions executed during this call,   *  Return value is the number of instructions executed during this call,
177   *  0 if no instructions were executed.   *  0 if no instructions were executed.
178   */   */
179  int DYNTRANS_CPU_RUN_INSTR(struct emul *emul, struct cpu *cpu)  int DYNTRANS_RUN_INSTR(struct cpu *cpu)
180  {  {
181          /*          MODE_uint_t cached_pc;
          *  TODO:  Statistics stuff!  
          */  
         int show_opcode_statistics = 0;  
   
 #ifdef MODE32  
         uint32_t cached_pc;  
 #else  
         uint64_t cached_pc;  
 #endif  
182          int low_pc, n_instrs;          int low_pc, n_instrs;
183    
184            /*  Ugly... fix this some day.  */
185  #ifdef DYNTRANS_DUALMODE_32  #ifdef DYNTRANS_DUALMODE_32
186          if (cpu->is_32bit)  #ifdef MODE32
187                  DYNTRANS_PC_TO_POINTERS32(cpu);          DYNTRANS_PC_TO_POINTERS32(cpu);
188          else  #else
189            DYNTRANS_PC_TO_POINTERS(cpu);
190  #endif  #endif
191    #else
192          DYNTRANS_PC_TO_POINTERS(cpu);          DYNTRANS_PC_TO_POINTERS(cpu);
193    #endif
194    
195          /*          /*
196           *  Interrupt assertion?  (This is _below_ the initial PC to pointer           *  Interrupt assertion?  (This is _below_ the initial PC to pointer
# Line 238  int DYNTRANS_CPU_RUN_INSTR(struct emul * Line 229  int DYNTRANS_CPU_RUN_INSTR(struct emul *
229  #endif  #endif
230  #ifdef DYNTRANS_PPC  #ifdef DYNTRANS_PPC
231          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) {
232                  ppc_exception(cpu, PPC_EXCEPTION_DEC);                  if (!(cpu->cd.ppc.cpu_type.flags & PPC_NO_DEC))
233                            ppc_exception(cpu, PPC_EXCEPTION_DEC);
234                  cpu->cd.ppc.dec_intr_pending = 0;                  cpu->cd.ppc.dec_intr_pending = 0;
235          }          }
236          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)
237                  ppc_exception(cpu, PPC_EXCEPTION_EI);                  ppc_exception(cpu, PPC_EXCEPTION_EI);
238  #endif  #endif
239    #ifdef DYNTRANS_SH
240            if (cpu->cd.sh.int_to_assert > 0 && !(cpu->cd.sh.sr & SH_SR_BL)
241                && ((cpu->cd.sh.sr & SH_SR_IMASK) >> SH_SR_IMASK_SHIFT)
242                < cpu->cd.sh.int_level)
243                    sh_exception(cpu, 0, cpu->cd.sh.int_to_assert, 0);
244    #endif
245    
246          cached_pc = cpu->pc;          cached_pc = cpu->pc;
247    
248          cpu->n_translated_instrs = 0;          cpu->n_translated_instrs = 0;
         cpu->running_translated = 1;  
249    
250          cpu->cd.DYNTRANS_ARCH.cur_physpage = (void *)          cpu->cd.DYNTRANS_ARCH.cur_physpage = (void *)
251              cpu->cd.DYNTRANS_ARCH.cur_ic_page;              cpu->cd.DYNTRANS_ARCH.cur_ic_page;
# Line 264  int DYNTRANS_CPU_RUN_INSTR(struct emul * Line 261  int DYNTRANS_CPU_RUN_INSTR(struct emul *
261                          cpu_register_dump(cpu->machine, cpu, 1, 0x1);                          cpu_register_dump(cpu->machine, cpu, 1, 0x1);
262                  }                  }
263                  if (cpu->machine->instruction_trace) {                  if (cpu->machine->instruction_trace) {
264  #ifdef DYNTRANS_X86                          /*  TODO/Note: This must be large enough to hold
265                          unsigned char instr[17];                              any instruction for any ISA:  */
266                          cpu->cd.x86.cursegment = X86_S_CS;                          unsigned char instr[1 <<
267                          cpu->cd.x86.seg_override = 0;                              DYNTRANS_INSTR_ALIGNMENT_SHIFT];
 #else  
 #ifdef DYNTRANS_M68K  
                         unsigned char instr[16];        /*  TODO: 16?  */  
 #else  
                         unsigned char instr[4];         /*  General case...  */  
 #endif  
 #endif  
   
268                          if (!cpu->memory_rw(cpu, cpu->mem, cached_pc, &instr[0],                          if (!cpu->memory_rw(cpu, cpu->mem, cached_pc, &instr[0],
269                              sizeof(instr), MEM_READ, CACHE_INSTRUCTION)) {                              sizeof(instr), MEM_READ, CACHE_INSTRUCTION)) {
270                                  fatal("XXX_cpu_run_instr(): could not read "                                  fatal("XXX_run_instr(): could not read "
271                                      "the instruction\n");                                      "the instruction\n");
272                          } else {                          } else {
273                                  cpu_disassemble_instr(cpu->machine, cpu,  #ifdef DYNTRANS_DELAYSLOT
274                                      instr, 1, 0);                                  int len =
275    #endif
276                                        cpu_disassemble_instr(
277                                        cpu->machine, cpu, instr, 1, 0);
278  #ifdef DYNTRANS_DELAYSLOT  #ifdef DYNTRANS_DELAYSLOT
279                                  /*  Show the instruction in the delay slot,                                  /*  Show the instruction in the delay slot,
280                                      if any:  */                                      if any:  */
# Line 293  int DYNTRANS_CPU_RUN_INSTR(struct emul * Line 285  int DYNTRANS_CPU_RUN_INSTR(struct emul *
285                                      instr)) {                                      instr)) {
286                                          int saved_delayslot = cpu->delay_slot;                                          int saved_delayslot = cpu->delay_slot;
287                                          cpu->memory_rw(cpu, cpu->mem, cached_pc                                          cpu->memory_rw(cpu, cpu->mem, cached_pc
288                                              + sizeof(instr), &instr[0],                                              + len, &instr[0],
289                                              sizeof(instr), MEM_READ,                                              sizeof(instr), MEM_READ,
290                                              CACHE_INSTRUCTION);                                              CACHE_INSTRUCTION);
291                                          cpu->delay_slot = DELAYED;                                          cpu->delay_slot = DELAYED;
292                                          cpu->pc += sizeof(instr);                                          cpu->pc += len;
293                                          cpu_disassemble_instr(cpu->machine,                                          cpu_disassemble_instr(cpu->machine,
294                                              cpu, instr, 1, 0);                                              cpu, instr, 1, 0);
295                                          cpu->delay_slot = saved_delayslot;                                          cpu->delay_slot = saved_delayslot;
296                                          cpu->pc -= sizeof(instr);                                          cpu->pc -= len;
297                                  }                                  }
298  #endif  #endif
299                          }                          }
300                  }                  }
301    
302                  /*  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);  
 #ifdef DYNTRANS_VARIABLE_INSTRUCTION_LENGTH  
                                 cpu->cd.DYNTRANS_ARCH.cur_physpage->ics[i].  
                                     arg[0] = 0;  
 #endif  
                         }  
                         fatal("[ Note: The translation of physical page 0x%"  
                             PRIx64" 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)  
303                          S;                          S;
304    
305                  /*  Execute just one instruction:  */                  /*  Execute just one instruction:  */
# Line 356  while (cycles-- > 0) Line 323  while (cycles-- > 0)
323          cpu->machine->tick_func[1](cpu, cpu->machine->tick_extra[1]);          cpu->machine->tick_func[1](cpu, cpu->machine->tick_extra[1]);
324  /* printf("B\n"); */  /* printf("B\n"); */
325    
326                          if (!cpu->running_translated ||                          if (n_instrs + cpu->n_translated_instrs >=
                             n_instrs + cpu->n_translated_instrs >=  
327                              N_SAFE_DYNTRANS_LIMIT)                              N_SAFE_DYNTRANS_LIMIT)
328                                  break;                                  break;
329                  }                  }
330          } else if (show_opcode_statistics) {          } else if (cpu->machine->statistics_enabled) {
331                  /*  Gather statistics while executing multiple instructions:  */                  /*  Gather statistics while executing multiple instructions:  */
332                  n_instrs = 0;                  n_instrs = 0;
333                  for (;;) {                  for (;;) {
# Line 374  while (cycles-- > 0) Line 340  while (cycles-- > 0)
340    
341                          n_instrs += 24;                          n_instrs += 24;
342    
343                          if (!cpu->running_translated ||                          if (n_instrs + cpu->n_translated_instrs >=
                             n_instrs + cpu->n_translated_instrs >=  
344                              N_SAFE_DYNTRANS_LIMIT)                              N_SAFE_DYNTRANS_LIMIT)
345                                  break;                                  break;
346                  }                  }
# Line 393  while (cycles-- > 0) Line 358  while (cycles-- > 0)
358    
359                          I; I; I; I; I;   I; I; I; I; I;                          I; I; I; I; I;   I; I; I; I; I;
360    
361                          n_instrs += 60;                          I; I; I; I; I;   I; I; I; I; I;
362                            I; I; I; I; I;   I; I; I; I; I;
363                            I; I; I; I; I;   I; I; I; I; I;
364                            I; I; I; I; I;   I; I; I; I; I;
365                            I; I; I; I; I;   I; I; I; I; I;
366    
367                          if (!cpu->running_translated ||                          I; I; I; I; I;   I; I; I; I; I;
368                              n_instrs + cpu->n_translated_instrs >=  
369                              N_SAFE_DYNTRANS_LIMIT)                          cpu->n_translated_instrs += 120;
370                            if (cpu->n_translated_instrs >= N_SAFE_DYNTRANS_LIMIT)
371                                  break;                                  break;
372                  }                  }
373          }          }
# Line 436  while (cycles-- > 0) Line 406  while (cycles-- > 0)
406                      (int32_t) (old + n_instrs);                      (int32_t) (old + n_instrs);
407                  diff2 = cpu->cd.mips.coproc[0]->reg[COP0_COMPARE] -                  diff2 = cpu->cd.mips.coproc[0]->reg[COP0_COMPARE] -
408                      cpu->cd.mips.coproc[0]->reg[COP0_COUNT];                      cpu->cd.mips.coproc[0]->reg[COP0_COUNT];
409                  if (cpu->cd.mips.compare_register_set && diff1>0 && diff2<=0)  
410                          cpu_interrupt(cpu, 7);                  if (cpu->cd.mips.compare_register_set) {
411    #if 1
412    /*  Not yet.  TODO  */
413                            if (cpu->machine->emulated_hz > 0) {
414                                    if (cpu->cd.mips.compare_interrupts_pending > 0)
415                                            INTERRUPT_ASSERT(
416                                                cpu->cd.mips.irq_compare);
417                            } else
418    #endif
419                            {
420                                    if (diff1 > 0 && diff2 <= 0)
421                                            INTERRUPT_ASSERT(
422                                                cpu->cd.mips.irq_compare);
423                            }
424                    }
425          }          }
426  #endif  #endif
427  #ifdef DYNTRANS_PPC  #ifdef DYNTRANS_PPC
# Line 458  while (cycles-- > 0) Line 442  while (cycles-- > 0)
442          /*  Return the nr of instructions executed:  */          /*  Return the nr of instructions executed:  */
443          return n_instrs;          return n_instrs;
444  }  }
445  #endif  /*  DYNTRANS_CPU_RUN_INSTR  */  #endif  /*  DYNTRANS_RUN_INSTR  */
446    
447    
448    
# Line 480  void DYNTRANS_FUNCTION_TRACE(struct cpu Line 464  void DYNTRANS_FUNCTION_TRACE(struct cpu
464              6              6
465  #else  #else
466  #ifdef DYNTRANS_SH  #ifdef DYNTRANS_SH
467              8              8   /*  Both for 32-bit and 64-bit SuperH  */
468  #else  #else
469              4   /*  Default value for most archs  */              4   /*  Default value for most archs  */
470  #endif  #endif
# Line 505  void DYNTRANS_FUNCTION_TRACE(struct cpu Line 489  void DYNTRANS_FUNCTION_TRACE(struct cpu
489           */           */
490          for (x=0; x<n_args_to_print; x++) {          for (x=0; x<n_args_to_print; x++) {
491                  int64_t d;                  int64_t d;
492  #ifdef DYNTRANS_X86  #if defined(DYNTRANS_TRANSPUTER)
493                  d = 0;          /*  TODO  */                  d = 0;          /*  TODO  */
494  #else  #else
495                  /*  Args in registers:  */                  /*  Args in registers:  */
# Line 521  void DYNTRANS_FUNCTION_TRACE(struct cpu Line 505  void DYNTRANS_FUNCTION_TRACE(struct cpu
505                          they go downwards, ie. 22,23 and so on  */                          they go downwards, ie. 22,23 and so on  */
506                      r[24                      r[24
507  #endif  #endif
 #ifdef DYNTRANS_HPPA  
                     r[0         /*  TODO  */  
 #endif  
 #ifdef DYNTRANS_I960  
                     r[0         /*  TODO  */  
 #endif  
 #ifdef DYNTRANS_IA64  
                     r[0         /*  TODO  */  
 #endif  
508  #ifdef DYNTRANS_M68K  #ifdef DYNTRANS_M68K
509                      d[0         /*  TODO  */                      d[0         /*  TODO  */
510  #endif  #endif
# Line 539  void DYNTRANS_FUNCTION_TRACE(struct cpu Line 514  void DYNTRANS_FUNCTION_TRACE(struct cpu
514  #ifdef DYNTRANS_PPC  #ifdef DYNTRANS_PPC
515                      gpr[3                      gpr[3
516  #endif  #endif
517    #ifdef DYNTRANS_RCA180X
518                        r[0         /*  TODO  */
519    #endif
520  #ifdef DYNTRANS_SH  #ifdef DYNTRANS_SH
521                      r[2                      r[4         /*  NetBSD seems to use 4? But 2 seems
522                                            to be used by other code? TODO  */
523  #endif  #endif
524  #ifdef DYNTRANS_SPARC  #ifdef DYNTRANS_SPARC
525                      r[24                      r[8         /*  o0..o5  */
526  #endif  #endif
527                      + x];                      + x];
528  #endif  #endif
# Line 586  static void DYNTRANS_TC_ALLOCATE_DEFAULT Line 565  static void DYNTRANS_TC_ALLOCATE_DEFAULT
565  {  {
566          struct DYNTRANS_TC_PHYSPAGE *ppp;          struct DYNTRANS_TC_PHYSPAGE *ppp;
567    
568            native_commit(cpu);
569    
570          ppp = (struct DYNTRANS_TC_PHYSPAGE *)(cpu->translation_cache          ppp = (struct DYNTRANS_TC_PHYSPAGE *)(cpu->translation_cache
571              + cpu->translation_cache_cur_ofs);              + cpu->translation_cache_cur_ofs);
572    
# Line 662  void DYNTRANS_PC_TO_POINTERS_GENERIC(str Line 643  void DYNTRANS_PC_TO_POINTERS_GENERIC(str
643          if (!ok) {          if (!ok) {
644                  uint64_t paddr;                  uint64_t paddr;
645                  if (cpu->translate_v2p != NULL) {                  if (cpu->translate_v2p != NULL) {
646                            uint64_t vaddr =
647    #if defined(MODE32) && defined(DYNTRANS_MIPS)
648                            /*  32-bit MIPS is _sign_ extend, not zero.  */
649                                (int32_t)
650    #endif
651                                cached_pc;
652                          ok = cpu->translate_v2p(                          ok = cpu->translate_v2p(
653                              cpu, cached_pc, &paddr, FLAG_INSTR);                              cpu, vaddr, &paddr, FLAG_INSTR);
654                  } else {                  } else {
655                          paddr = cached_pc;                          paddr = cached_pc;
656                          ok = 1;                          ok = 1;
657                  }                  }
658                  if (!ok) {                  if (!ok) {
659                            /*
660                             *  The PC is now set to the exception handler.
661                             *  Try to find the paddr in the translation arrays,
662                             *  or if that fails, call translate_v2p for the
663                             *  exception handler.
664                             */
665                          /*  fatal("TODO: instruction vaddr=>paddr translation "                          /*  fatal("TODO: instruction vaddr=>paddr translation "
666                              "failed. vaddr=0x%"PRIx64"\n", (uint64_t)cached_pc);                              "failed. vaddr=0x%"PRIx64"\n", (uint64_t)cached_pc);
667                          fatal("!! cpu->pc=0x%"PRIx64"\n", (uint64_t)cpu->pc); */                          fatal("!! cpu->pc=0x%"PRIx64"\n", (uint64_t)cpu->pc); */
668    
669                          ok = cpu->translate_v2p(cpu, cpu->pc, &paddr,                          /*  If there was an exception, the PC has changed.
670                              FLAG_INSTR);                              Update cached_pc:  */
671                            cached_pc = cpu->pc;
672    
673    #ifdef MODE32
674                            index = DYNTRANS_ADDR_TO_PAGENR(cached_pc);
675                            if (cpu->cd.DYNTRANS_ARCH.host_load[index] != NULL) {
676                                    paddr = cpu->cd.DYNTRANS_ARCH.phys_addr[index];
677                                    ok = 1;
678                            }
679    #else
680                            x1 = (cached_pc >> (64-DYNTRANS_L1N)) & mask1;
681                            x2 = (cached_pc >> (64-DYNTRANS_L1N-DYNTRANS_L2N))
682                                & mask2;
683                            x3 = (cached_pc >> (64-DYNTRANS_L1N-DYNTRANS_L2N
684                                - DYNTRANS_L3N)) & mask3;
685                            l2 = cpu->cd.DYNTRANS_ARCH.l1_64[x1];
686                            l3 = l2->l3[x2];
687                            if (l3->host_load[x3] != NULL) {
688                                    paddr = l3->phys_addr[x3];
689                                    ok = 1;
690                            }
691    #endif
692    
693                            if (!ok) {
694                                    ok = cpu->translate_v2p(cpu, cpu->pc, &paddr,
695                                        FLAG_INSTR);
696                            }
697    
698                          /*  printf("EXCEPTION HANDLER: vaddr = 0x%x ==> "                          /*  printf("EXCEPTION HANDLER: vaddr = 0x%x ==> "
699                              "paddr = 0x%x\n", (int)cpu->pc, (int)paddr);                              "paddr = 0x%x\n", (int)cpu->pc, (int)paddr);
# Line 687  void DYNTRANS_PC_TO_POINTERS_GENERIC(str Line 706  void DYNTRANS_PC_TO_POINTERS_GENERIC(str
706                          }                          }
707                  }                  }
708    
                 /*  If there was an exception, the PC can have changed.  
                     Update cached_pc:  */  
                 cached_pc = cpu->pc;  
   
 #ifdef MODE32  
                 index = DYNTRANS_ADDR_TO_PAGENR(cached_pc);  
 #else  
                 x1 = (cached_pc >> (64-DYNTRANS_L1N)) & mask1;  
                 x2 = (cached_pc >> (64-DYNTRANS_L1N-DYNTRANS_L2N)) & mask2;  
                 x3 = (cached_pc >> (64-DYNTRANS_L1N-DYNTRANS_L2N-DYNTRANS_L3N))  
                     & mask3;  
 #endif  
   
709                  physaddr = paddr;                  physaddr = paddr;
710          }          }
711    
712            physaddr &= ~(DYNTRANS_PAGESIZE - 1);
713    
714  #ifdef MODE32  #ifdef MODE32
715          if (cpu->cd.DYNTRANS_ARCH.host_load[index] == NULL) {          if (cpu->cd.DYNTRANS_ARCH.host_load[index] == NULL) {
716  #else  #else
717          if (l3->host_load[x3] == NULL) {          if (l3->host_load[x3] == NULL) {
718  #endif  #endif
719                    int q = DYNTRANS_PAGESIZE - 1;
720                  unsigned char *host_page = memory_paddr_to_hostaddr(cpu->mem,                  unsigned char *host_page = memory_paddr_to_hostaddr(cpu->mem,
721                      physaddr, MEM_READ);                      physaddr, MEM_READ);
722                  if (host_page != NULL) {                  if (host_page != NULL) {
                         int q = DYNTRANS_PAGESIZE - 1;  
                         host_page += (physaddr &  
                             ((1 << BITS_PER_MEMBLOCK) - 1) & ~q);  
723                          cpu->update_translation_table(cpu, cached_pc & ~q,                          cpu->update_translation_table(cpu, cached_pc & ~q,
724                              host_page, 0, physaddr & ~q);                              host_page, 0, physaddr);
 #ifndef MODE32  
                         /*  Recalculate l2 and l3, since they might have  
                             changed now:  */  
                         l2 = cpu->cd.DYNTRANS_ARCH.l1_64[x1];  
                         l3 = l2->l3[x2];  
 #endif  
725                  }                  }
726          }          }
727    
728          if (cpu->translation_cache_cur_ofs >= DYNTRANS_CACHE_SIZE) {          if (cpu->translation_cache_cur_ofs >= dyntrans_cache_size) {
729  #ifdef UNSTABLE_DEVEL  #ifdef UNSTABLE_DEVEL
730                  fatal("[ dyntrans: resetting the translation cache ]\n");                  fatal("[ dyntrans: resetting the translation cache ]\n");
731  #endif  #endif
732                  cpu_create_or_reset_tc(cpu);                  cpu_create_or_reset_tc(cpu);
733          }          }
734    
         physaddr &= ~(DYNTRANS_PAGESIZE - 1);  
   
735          pagenr = DYNTRANS_ADDR_TO_PAGENR(physaddr);          pagenr = DYNTRANS_ADDR_TO_PAGENR(physaddr);
736          table_index = PAGENR_TO_TABLE_INDEX(pagenr);          table_index = PAGENR_TO_TABLE_INDEX(pagenr);
737    
# Line 761  void DYNTRANS_PC_TO_POINTERS_GENERIC(str Line 759  void DYNTRANS_PC_TO_POINTERS_GENERIC(str
759                  /*  fatal("CREATING page %lli (physaddr 0x%"PRIx64"), table "                  /*  fatal("CREATING page %lli (physaddr 0x%"PRIx64"), table "
760                      "index %i\n", (long long)pagenr, (uint64_t)physaddr,                      "index %i\n", (long long)pagenr, (uint64_t)physaddr,
761                      (int)table_index);  */                      (int)table_index);  */
762                    native_commit(cpu);
763                  *physpage_entryp = physpage_ofs =                  *physpage_entryp = physpage_ofs =
764                      cpu->translation_cache_cur_ofs;                      cpu->translation_cache_cur_ofs;
765    
# Line 779  void DYNTRANS_PC_TO_POINTERS_GENERIC(str Line 778  void DYNTRANS_PC_TO_POINTERS_GENERIC(str
778                  l3->phys_page[x3] = ppp;                  l3->phys_page[x3] = ppp;
779  #endif  #endif
780    
781  #ifdef MODE32          /*
782          /*  Small optimization: only mark the physical page as non-writable           *  If there are no translations yet on this page, then mark it
783              if it did not contain translations. (Because if it does contain           *  as non-writable. If there are already translations, then it
784              translations, it is already non-writable.)  */           *  should already have been marked as non-writable.
785          if (!cpu->cd.DYNTRANS_ARCH.phystranslation[pagenr >> 5] &           */
786              (1 << (pagenr & 31)))          if (ppp->translations == 0) {
787  #endif                  cpu->invalidate_translation_caches(cpu, physaddr,
788          cpu->invalidate_translation_caches(cpu, physaddr,                      JUST_MARK_AS_NON_WRITABLE | INVALIDATE_PADDR);
789              JUST_MARK_AS_NON_WRITABLE | INVALIDATE_PADDR);          }
790    
791          cpu->cd.DYNTRANS_ARCH.cur_ic_page = &ppp->ics[0];          cpu->cd.DYNTRANS_ARCH.cur_ic_page = &ppp->ics[0];
792    
# Line 904  void DYNTRANS_INIT_TABLES(struct cpu *cp Line 903  void DYNTRANS_INIT_TABLES(struct cpu *cp
903          }          }
904    
905          ppp->next_ofs = 0;          ppp->next_ofs = 0;
906            ppp->translations = 0;
907          /*  ppp->physaddr is filled in by the page allocator  */          /*  ppp->physaddr is filled in by the page allocator  */
908    
909          for (i=0; i<DYNTRANS_IC_ENTRIES_PER_PAGE; i++) {          for (i=0; i<DYNTRANS_IC_ENTRIES_PER_PAGE; i++) {
# Line 1025  static void DYNTRANS_INVALIDATE_TLB_ENTR Line 1025  static void DYNTRANS_INVALIDATE_TLB_ENTR
1025                  l3->host_store[x3] = NULL;                  l3->host_store[x3] = NULL;
1026                  return;                  return;
1027          }          }
1028    
1029    #ifdef BUGHUNT
1030    
1031    {
1032            /*  Consistency check, for debugging:  */
1033            int x1, x1b; // x2, x3;
1034            struct DYNTRANS_L2_64_TABLE *l2;
1035            //struct DYNTRANS_L3_64_TABLE *l3;
1036    
1037            for (x1 = 0; x1 <= mask1; x1 ++) {
1038                    l2 = cpu->cd.DYNTRANS_ARCH.l1_64[x1];
1039                    if (l2 == cpu->cd.DYNTRANS_ARCH.l2_64_dummy)
1040                            continue;
1041                    /*  Make sure that this l2 isn't used more than 1 time!  */
1042                    for (x1b = 0; x1b <= mask1; x1b ++)
1043                            if (x1 != x1b &&
1044                                l2 == cpu->cd.DYNTRANS_ARCH.l1_64[x1b]) {
1045                                    fatal("L2 reuse: %p\n", l2);
1046                                    exit(1);
1047                            }
1048            }
1049    }
1050    
1051    /*  Count how many pages are actually in use:  */
1052    {
1053            int n=0, i;
1054            for (i=0; i<=mask3; i++)
1055                    if (l3->vaddr_to_tlbindex[i])
1056                            n++;
1057            if (n != l3->refcount) {
1058                    printf("Z: %i in use, but refcount = %i!\n", n, l3->refcount);
1059                    exit(1);
1060            }
1061    
1062            n = 0;
1063            for (i=0; i<=mask3; i++)
1064                    if (l3->host_load[i] != NULL)
1065                            n++;
1066            if (n != l3->refcount) {
1067                    printf("ZHL: %i in use, but refcount = %i!\n", n, l3->refcount);
1068                    exit(1);
1069            }
1070    }
1071    #endif
1072    
1073          l3->host_load[x3] = NULL;          l3->host_load[x3] = NULL;
1074          l3->host_store[x3] = NULL;          l3->host_store[x3] = NULL;
1075          l3->phys_addr[x3] = 0;          l3->phys_addr[x3] = 0;
1076          l3->phys_page[x3] = NULL;          l3->phys_page[x3] = NULL;
1077          l3->refcount --;          if (l3->vaddr_to_tlbindex[x3] != 0) {
1078                    cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[
1079                        l3->vaddr_to_tlbindex[x3] - 1].valid = 0;
1080                    l3->refcount --;
1081            }
1082            l3->vaddr_to_tlbindex[x3] = 0;
1083    
1084          if (l3->refcount < 0) {          if (l3->refcount < 0) {
1085                  fatal("xxx_invalidate_tlb_entry(): huh? Refcount bug.\n");                  fatal("xxx_invalidate_tlb_entry(): huh? Refcount bug.\n");
1086                  exit(1);                  exit(1);
1087          }          }
1088    
1089          if (l3->refcount == 0) {          if (l3->refcount == 0) {
1090                  l3->next = cpu->cd.DYNTRANS_ARCH.next_free_l3;                  l3->next = cpu->cd.DYNTRANS_ARCH.next_free_l3;
1091                  cpu->cd.DYNTRANS_ARCH.next_free_l3 = l3;                  cpu->cd.DYNTRANS_ARCH.next_free_l3 = l3;
1092                  l2->l3[x2] = cpu->cd.DYNTRANS_ARCH.l3_64_dummy;                  l2->l3[x2] = cpu->cd.DYNTRANS_ARCH.l3_64_dummy;
1093    
1094    #ifdef BUGHUNT
1095    /*  Make sure that we're placing a CLEAN page on the
1096        freelist:  */
1097    {
1098            int i;
1099            for (i=0; i<=mask3; i++)
1100                    if (l3->host_load[i] != NULL) {
1101                            fatal("TRYING TO RETURN A NON-CLEAN L3 PAGE!\n");
1102                            exit(1);
1103                    }
1104    }
1105    #endif
1106                  l2->refcount --;                  l2->refcount --;
1107                  if (l2->refcount < 0) {                  if (l2->refcount < 0) {
1108                          fatal("xxx_invalidate_tlb_entry(): Refcount bug L2.\n");                          fatal("xxx_invalidate_tlb_entry(): Refcount bug L2.\n");
# Line 1180  void DYNTRANS_INVALIDATE_TC_CODE(struct Line 1244  void DYNTRANS_INVALIDATE_TC_CODE(struct
1244                  struct DYNTRANS_TC_PHYSPAGE *ppp, *prev_ppp;                  struct DYNTRANS_TC_PHYSPAGE *ppp, *prev_ppp;
1245    
1246                  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  
   
1247                  table_index = PAGENR_TO_TABLE_INDEX(pagenr);                  table_index = PAGENR_TO_TABLE_INDEX(pagenr);
1248    
1249                  physpage_entryp = &(((uint32_t *)cpu->                  physpage_entryp = &(((uint32_t *)cpu->
1250                      translation_cache)[table_index]);                      translation_cache)[table_index]);
1251                  physpage_ofs = *physpage_entryp;                  physpage_ofs = *physpage_entryp;
1252    
1253                    /*  Return immediately if there is no code translation
1254                        for this page.  */
1255                    if (physpage_ofs == 0)
1256                            return;
1257    
1258                  prev_ppp = ppp = NULL;                  prev_ppp = ppp = NULL;
1259    
1260                  /*  Traverse the physical page chain:  */                  /*  Traverse the physical page chain:  */
# Line 1214  void DYNTRANS_INVALIDATE_TC_CODE(struct Line 1272  void DYNTRANS_INVALIDATE_TC_CODE(struct
1272                          physpage_ofs = ppp->next_ofs;                          physpage_ofs = ppp->next_ofs;
1273                  }                  }
1274    
1275                  if (physpage_ofs == 0)                  /*  If there is no translation, there is no need to go
1276                          ppp = NULL;                      on and try to remove it from the vph_tlb_entry array:  */
1277                    if (ppp == NULL)
1278                            return;
1279    
1280  #if 1  #if 0
1281                  /*                  /*
1282                   *  "Bypass" the page, removing it from the code cache.                   *  "Bypass" the page, removing it from the code cache.
1283                   *                   *
# Line 1239  void DYNTRANS_INVALIDATE_TC_CODE(struct Line 1299  void DYNTRANS_INVALIDATE_TC_CODE(struct
1299                   *  it might be faster since we don't risk wasting cache                   *  it might be faster since we don't risk wasting cache
1300                   *  memory as quickly (which would force unnecessary Restarts).                   *  memory as quickly (which would force unnecessary Restarts).
1301                   */                   */
1302                  if (ppp != NULL) {                  if (ppp != NULL && ppp->translations != 0) {
1303                          /*  TODO: Is this faster than copying an entire                          uint32_t x = ppp->translations; /*  TODO:
1304                              template page?  */                                  urk Should be same type as ppp->translations */
1305                          int i;                          int i, j, n, m;
1306                          for (i=0; i<DYNTRANS_IC_ENTRIES_PER_PAGE; i++)                          n = 8 * sizeof(x);
1307                                  ppp->ics[i].f =                          m = DYNTRANS_IC_ENTRIES_PER_PAGE / n;
1308    
1309                            for (i=0; i<n; i++) {
1310                                    if (x & 1) {
1311                                            for (j=0; j<m; j++)
1312                                                    ppp->ics[i*m + j].f =
1313  #ifdef DYNTRANS_DUALMODE_32  #ifdef DYNTRANS_DUALMODE_32
1314                                      cpu->is_32bit? instr32(to_be_translated) :                                                      cpu->is_32bit?
1315                                                        instr32(to_be_translated) :
1316  #endif  #endif
1317                                      instr(to_be_translated);                                                      instr(to_be_translated);
1318                                    }
1319    
1320                                    x >>= 1;
1321                            }
1322    
1323                            ppp->translations = 0;
1324                  }                  }
1325  #endif  #endif
1326          }          }
# Line 1268  void DYNTRANS_INVALIDATE_TC_CODE(struct Line 1340  void DYNTRANS_INVALIDATE_TC_CODE(struct
1340                                  uint32_t index =                                  uint32_t index =
1341                                      DYNTRANS_ADDR_TO_PAGENR(vaddr_page);                                      DYNTRANS_ADDR_TO_PAGENR(vaddr_page);
1342                                  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));  
1343  #else  #else
1344                                  const uint32_t mask1 = (1 << DYNTRANS_L1N) - 1;                                  const uint32_t mask1 = (1 << DYNTRANS_L1N) - 1;
1345                                  const uint32_t mask2 = (1 << DYNTRANS_L2N) - 1;                                  const uint32_t mask2 = (1 << DYNTRANS_L2N) - 1;
# Line 1306  void DYNTRANS_INVALIDATE_TC_CODE(struct Line 1374  void DYNTRANS_INVALIDATE_TC_CODE(struct
1374  void DYNTRANS_UPDATE_TRANSLATION_TABLE(struct cpu *cpu, uint64_t vaddr_page,  void DYNTRANS_UPDATE_TRANSLATION_TABLE(struct cpu *cpu, uint64_t vaddr_page,
1375          unsigned char *host_page, int writeflag, uint64_t paddr_page)          unsigned char *host_page, int writeflag, uint64_t paddr_page)
1376  {  {
1377  #ifndef MODE32          int found, r, useraccess = 0;
         int64_t lowest, highest = -1;  
 #endif  
         int found, r, lowest_index, useraccess = 0;  
1378    
1379  #ifdef MODE32  #ifdef MODE32
1380          uint32_t index;          uint32_t index;
# Line 1325  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s Line 1390  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s
1390          uint32_t x1, x2, x3;          uint32_t x1, x2, x3;
1391          struct DYNTRANS_L2_64_TABLE *l2;          struct DYNTRANS_L2_64_TABLE *l2;
1392          struct DYNTRANS_L3_64_TABLE *l3;          struct DYNTRANS_L3_64_TABLE *l3;
1393          /*  fatal("update_translation_table(): v=0x%"PRIx64", h=%p w=%i"  
1394              " p=0x%"PRIx64"\n", (uint64_t)vaddr_page, host_page, writeflag,          /*  fatal("update_translation_table(): v=0x%016"PRIx64", h=%p w=%i"
1395                " p=0x%016"PRIx64"\n", (uint64_t)vaddr_page, host_page, writeflag,
1396              (uint64_t)paddr_page);  */              (uint64_t)paddr_page);  */
1397  #endif  #endif
1398    
# Line 1339  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s Line 1405  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s
1405          }          }
1406    
1407          /*  Scan the current TLB entries:  */          /*  Scan the current TLB entries:  */
         lowest_index = 0;  
1408    
1409  #ifdef MODE32  #ifdef MODE32
1410          /*          /*
# Line 1352  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s Line 1417  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s
1417           */           */
1418          found = (int)cpu->cd.DYNTRANS_ARCH.vaddr_to_tlbindex[          found = (int)cpu->cd.DYNTRANS_ARCH.vaddr_to_tlbindex[
1419              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++) % DYNTRANS_MAX_VPH_TLB_ENTRIES;  
         }  
1420  #else  #else
1421          lowest = cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[0].timestamp;          x1 = (vaddr_page >> (64-DYNTRANS_L1N)) & mask1;
1422          found = -1;          x2 = (vaddr_page >> (64-DYNTRANS_L1N-DYNTRANS_L2N)) & mask2;
1423          for (r=0; r<DYNTRANS_MAX_VPH_TLB_ENTRIES; r++) {          x3 = (vaddr_page >> (64-DYNTRANS_L1N-DYNTRANS_L2N-DYNTRANS_L3N))
1424                  if (cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].timestamp < lowest) {              & mask3;
1425                          lowest = cpu->cd.DYNTRANS_ARCH.  
1426                              vph_tlb_entry[r].timestamp;          l2 = cpu->cd.DYNTRANS_ARCH.l1_64[x1];
1427                          lowest_index = r;          if (l2 == cpu->cd.DYNTRANS_ARCH.l2_64_dummy)
1428                  }                  found = -1;
1429                  if (cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].timestamp > highest)          else {
1430                          highest = cpu->cd.DYNTRANS_ARCH.                  l3 = l2->l3[x2];
1431                              vph_tlb_entry[r].timestamp;                  if (l3 == cpu->cd.DYNTRANS_ARCH.l3_64_dummy)
1432                  if (cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].valid &&                          found = -1;
1433                      cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].vaddr_page ==                  else
1434                      vaddr_page) {                          found = (int)l3->vaddr_to_tlbindex[x3] - 1;
                         found = r;  
                         break;  
                 }  
1435          }          }
1436  #endif  #endif
1437    
1438          if (found < 0) {          if (found < 0) {
1439                  /*  Create the new TLB entry, overwriting the oldest one:  */                  /*  Create the new TLB entry, overwriting a "random" entry:  */
1440                  r = lowest_index;                  static unsigned int x = 0;
1441                    r = (x++) % DYNTRANS_MAX_VPH_TLB_ENTRIES;
1442    
1443                  if (cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].valid) {                  if (cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].valid) {
1444                          /*  This one has to be invalidated first:  */                          /*  This one has to be invalidated first:  */
1445                          DYNTRANS_INVALIDATE_TLB_ENTRY(cpu,                          DYNTRANS_INVALIDATE_TLB_ENTRY(cpu,
# Line 1393  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s Line 1453  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s
1453                  cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].vaddr_page = vaddr_page;                  cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].vaddr_page = vaddr_page;
1454                  cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].writeflag =                  cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].writeflag =
1455                      writeflag & MEM_WRITE;                      writeflag & MEM_WRITE;
 #ifndef MODE32  
                 cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].timestamp = highest + 1;  
 #endif  
1456    
1457                  /*  Add the new translation to the table:  */                  /*  Add the new translation to the table:  */
1458  #ifdef MODE32  #ifdef MODE32
# Line 1412  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s Line 1469  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s
1469                              |= 1 << (index & 31);                              |= 1 << (index & 31);
1470  #endif  #endif
1471  #else   /* !MODE32  */  #else   /* !MODE32  */
                 x1 = (vaddr_page >> (64-DYNTRANS_L1N)) & mask1;  
                 x2 = (vaddr_page >> (64-DYNTRANS_L1N-DYNTRANS_L2N)) & mask2;  
                 x3 = (vaddr_page >> (64-DYNTRANS_L1N-DYNTRANS_L2N-DYNTRANS_L3N))  
                     & mask3;  
1472                  l2 = cpu->cd.DYNTRANS_ARCH.l1_64[x1];                  l2 = cpu->cd.DYNTRANS_ARCH.l1_64[x1];
1473                  if (l2 == cpu->cd.DYNTRANS_ARCH.l2_64_dummy) {                  if (l2 == cpu->cd.DYNTRANS_ARCH.l2_64_dummy) {
1474                          if (cpu->cd.DYNTRANS_ARCH.next_free_l2 != NULL) {                          if (cpu->cd.DYNTRANS_ARCH.next_free_l2 != NULL) {
# Line 1426  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s Line 1479  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s
1479                                  int i;                                  int i;
1480                                  l2 = cpu->cd.DYNTRANS_ARCH.l1_64[x1] =                                  l2 = cpu->cd.DYNTRANS_ARCH.l1_64[x1] =
1481                                      malloc(sizeof(struct DYNTRANS_L2_64_TABLE));                                      malloc(sizeof(struct DYNTRANS_L2_64_TABLE));
1482                                    l2->refcount = 0;
1483                                  for (i=0; i<(1 << DYNTRANS_L2N); i++)                                  for (i=0; i<(1 << DYNTRANS_L2N); i++)
1484                                          l2->l3[i] = cpu->cd.DYNTRANS_ARCH.                                          l2->l3[i] = cpu->cd.DYNTRANS_ARCH.
1485                                              l3_64_dummy;                                              l3_64_dummy;
1486                          }                          }
1487                            if (l2->refcount != 0) {
1488                                    fatal("Huh? l2 Refcount problem.\n");
1489                                    exit(1);
1490                            }
1491                    }
1492                    if (l2 == cpu->cd.DYNTRANS_ARCH.l2_64_dummy) {
1493                            fatal("INTERNAL ERROR L2 reuse\n");
1494                            exit(1);
1495                  }                  }
1496                  l3 = l2->l3[x2];                  l3 = l2->l3[x2];
1497                  if (l3 == cpu->cd.DYNTRANS_ARCH.l3_64_dummy) {                  if (l3 == cpu->cd.DYNTRANS_ARCH.l3_64_dummy) {
# Line 1441  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s Line 1503  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s
1503                                  l3 = l2->l3[x2] = zeroed_alloc(sizeof(                                  l3 = l2->l3[x2] = zeroed_alloc(sizeof(
1504                                      struct DYNTRANS_L3_64_TABLE));                                      struct DYNTRANS_L3_64_TABLE));
1505                          }                          }
1506                            if (l3->refcount != 0) {
1507                                    fatal("Huh? l3 Refcount problem.\n");
1508                                    exit(1);
1509                            }
1510                          l2->refcount ++;                          l2->refcount ++;
1511                  }                  }
1512                    if (l3 == cpu->cd.DYNTRANS_ARCH.l3_64_dummy) {
1513                            fatal("INTERNAL ERROR L3 reuse\n");
1514                            exit(1);
1515                    }
1516    
1517                  l3->host_load[x3] = host_page;                  l3->host_load[x3] = host_page;
1518                  l3->host_store[x3] = writeflag? host_page : NULL;                  l3->host_store[x3] = writeflag? host_page : NULL;
1519                  l3->phys_addr[x3] = paddr_page;                  l3->phys_addr[x3] = paddr_page;
1520                  l3->phys_page[x3] = NULL;                  l3->phys_page[x3] = NULL;
1521                  l3->vaddr_to_tlbindex[x3] = r + 1;                  l3->vaddr_to_tlbindex[x3] = r + 1;
1522                  l3->refcount ++;                  l3->refcount ++;
1523    
1524    #ifdef BUGHUNT
1525    /*  Count how many pages are actually in use:  */
1526    {
1527            int n=0, i;
1528            for (i=0; i<=mask3; i++)
1529                    if (l3->vaddr_to_tlbindex[i])
1530                            n++;
1531            if (n != l3->refcount) {
1532                    printf("X: %i in use, but refcount = %i!\n", n, l3->refcount);
1533                    exit(1);
1534            }
1535    
1536            n = 0;
1537            for (i=0; i<=mask3; i++)
1538                    if (l3->host_load[i] != NULL)
1539                            n++;
1540            if (n != l3->refcount) {
1541                    printf("XHL: %i in use, but refcount = %i!\n", n, l3->refcount);
1542                    exit(1);
1543            }
1544    }
1545    #endif
1546    
1547  #endif  /* !MODE32  */  #endif  /* !MODE32  */
1548          } else {          } else {
1549                  /*                  /*
# Line 1458  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s Line 1553  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s
1553                   *      Writeflag = MEM_DOWNGRADE: Downgrade to readonly.                   *      Writeflag = MEM_DOWNGRADE: Downgrade to readonly.
1554                   */                   */
1555                  r = found;                  r = found;
 #ifndef MODE32  
                 cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].timestamp = highest + 1;  
 #endif  
1556                  if (writeflag & MEM_WRITE)                  if (writeflag & MEM_WRITE)
1557                          cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].writeflag = 1;                          cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].writeflag = 1;
1558                  if (writeflag & MEM_DOWNGRADE)                  if (writeflag & MEM_DOWNGRADE)
# Line 1501  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s Line 1593  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s
1593                                  l3->host_store[x3] = NULL;                                  l3->host_store[x3] = NULL;
1594                  } else {                  } else {
1595                          /*  Change the entire physical/host mapping:  */                          /*  Change the entire physical/host mapping:  */
1596    printf("HOST LOAD 2 set to %p\n", host_page);
1597                          l3->host_load[x3] = host_page;                          l3->host_load[x3] = host_page;
1598                          l3->host_store[x3] = writeflag? host_page : NULL;                          l3->host_store[x3] = writeflag? host_page : NULL;
1599                          l3->phys_addr[x3] = paddr_page;                          l3->phys_addr[x3] = paddr_page;
1600                  }                  }
1601    
1602    #ifdef BUGHUNT
1603    /*  Count how many pages are actually in use:  */
1604    {
1605            int n=0, i;
1606            for (i=0; i<=mask3; i++)
1607                    if (l3->vaddr_to_tlbindex[i])
1608                            n++;
1609            if (n != l3->refcount) {
1610                    printf("Y: %i in use, but refcount = %i!\n", n, l3->refcount);
1611                    exit(1);
1612            }
1613    
1614            n = 0;
1615            for (i=0; i<=mask3; i++)
1616                    if (l3->host_load[i] != NULL)
1617                            n++;
1618            if (n != l3->refcount) {
1619                    printf("YHL: %i in use, but refcount = %i!\n", n, l3->refcount);
1620                    printf("Entry r = %i\n", r);
1621                    printf("Valid = %i\n",
1622    cpu->cd.DYNTRANS_ARCH.vph_tlb_entry[r].valid);
1623                    exit(1);
1624            }
1625    }
1626    #endif
1627    
1628  #endif  /*  !MODE32  */  #endif  /*  !MODE32  */
1629          }          }
1630  }  }
# Line 1551  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s Line 1671  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s
1671    
1672  #ifdef DYNTRANS_TO_BE_TRANSLATED_TAIL  #ifdef DYNTRANS_TO_BE_TRANSLATED_TAIL
1673          /*          /*
1674           *  If we end up here, then an instruction was translated.           *  If we end up here, then an instruction was translated. Let's mark
1675           *  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.)  
1676           */           */
1677    
1678          /*  Make sure cur_physpage is in synch:  */          /*  Make sure cur_physpage is in synch:  */
1679          cpu->cd.DYNTRANS_ARCH.cur_physpage = (void *)          cpu->cd.DYNTRANS_ARCH.cur_physpage = (void *)
1680              cpu->cd.DYNTRANS_ARCH.cur_ic_page;              cpu->cd.DYNTRANS_ARCH.cur_ic_page;
 #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;  
1681    
1682            {
1683                    int x = addr & (DYNTRANS_PAGESIZE - 1);
1684                    int addr_per_translation_range = DYNTRANS_PAGESIZE / (8 *
1685                        sizeof(cpu->cd.DYNTRANS_ARCH.cur_physpage->translations));
1686                    x /= addr_per_translation_range;
1687    
1688                    cpu->cd.DYNTRANS_ARCH.cur_physpage->translations |= (1 << x);
1689            }
1690    
1691          /*          /*
1692           *  Now it is time to check for combinations of instructions that can           *  Now it is time to check for combinations of instructions that can
# Line 1583  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s Line 1701  void DYNTRANS_UPDATE_TRANSLATION_TABLE(s
1701  #ifdef DYNTRANS_DELAYSLOT  #ifdef DYNTRANS_DELAYSLOT
1702              && !in_crosspage_delayslot              && !in_crosspage_delayslot
1703  #endif  #endif
1704              ) {              && cpu->cd.DYNTRANS_ARCH.combination_check != NULL
1705                  if (cpu->cd.DYNTRANS_ARCH.combination_check != NULL &&              && cpu->machine->allow_instruction_combinations) {
1706                      cpu->machine->speed_tricks)                  cpu->cd.DYNTRANS_ARCH.combination_check(cpu, ic,
1707                          cpu->cd.DYNTRANS_ARCH.combination_check(cpu, ic,                      addr & (DYNTRANS_PAGESIZE - 1));
                             addr & (DYNTRANS_PAGESIZE - 1));  
1708          }          }
1709    
1710          cpu->cd.DYNTRANS_ARCH.combination_check = NULL;          cpu->cd.DYNTRANS_ARCH.combination_check = NULL;
# Line 1667  bad:   /* Line 1784  bad:   /*
1784          }          }
1785    
1786          cpu->running = 0;          cpu->running = 0;
1787          cpu->dead = 1;  
1788            /*  Note: Single-stepping can jump here.  */
1789  stop_running_translated:  stop_running_translated:
1790    
1791          debugger_n_steps_left_before_interaction = 0;          debugger_n_steps_left_before_interaction = 0;
1792          cpu->running_translated = 0;  
1793          ic = cpu->cd.DYNTRANS_ARCH.next_ic = &nothing_call;          ic = cpu->cd.DYNTRANS_ARCH.next_ic = &nothing_call;
1794          cpu->cd.DYNTRANS_ARCH.next_ic ++;          cpu->cd.DYNTRANS_ARCH.next_ic ++;
1795    
1796          /*  Execute the "nothing" instruction:  */          /*  Execute the "nothing" instruction:  */
1797          ic->f(cpu, ic);          ic->f(cpu, ic);
1798    
1799  #endif  /*  DYNTRANS_TO_BE_TRANSLATED_TAIL  */  #endif  /*  DYNTRANS_TO_BE_TRANSLATED_TAIL  */
1800    

Legend:
Removed from v.26  
changed lines
  Added in v.34

  ViewVC Help
Powered by ViewVC 1.1.26