| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2005,2006 Christophe Fillot ([email protected]) |
| 4 |
* |
| 5 |
* MIPS Coprocessor 0 (System Coprocessor) implementation. |
| 6 |
* We don't use the JIT here, since there is no high performance needed. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <stdio.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <unistd.h> |
| 12 |
#include <string.h> |
| 13 |
#include <sys/types.h> |
| 14 |
#include <sys/stat.h> |
| 15 |
#include <fcntl.h> |
| 16 |
|
| 17 |
#include "device.h" |
| 18 |
#include "mips64.h" |
| 19 |
#include "mips64_cp0.h" |
| 20 |
#include "dynamips.h" |
| 21 |
#include "memory.h" |
| 22 |
|
| 23 |
/* MIPS cp0 registers names */ |
| 24 |
char *mips64_cp0_reg_names[MIPS64_CP0_REG_NR] = { |
| 25 |
"index" , |
| 26 |
"random", |
| 27 |
"entry_lo0", |
| 28 |
"entry_lo1", |
| 29 |
"context", |
| 30 |
"pagemask", |
| 31 |
"wired", |
| 32 |
"info", |
| 33 |
"badvaddr", |
| 34 |
"count", |
| 35 |
"entry_hi", |
| 36 |
"compare", |
| 37 |
"status", |
| 38 |
"cause", |
| 39 |
"epc", |
| 40 |
"prid", |
| 41 |
"config", |
| 42 |
"ll_addr", |
| 43 |
"watch_lo", |
| 44 |
"watch_hi", |
| 45 |
"xcontext", |
| 46 |
"cp0_r21", |
| 47 |
"cp0_r22", |
| 48 |
"cp0_r23", |
| 49 |
"cp0_r24", |
| 50 |
"cp0_r25", |
| 51 |
"ecc", |
| 52 |
"cache_err", |
| 53 |
"tag_lo", |
| 54 |
"tag_hi", |
| 55 |
"err_epc", |
| 56 |
"cp0_r31", |
| 57 |
}; |
| 58 |
|
| 59 |
/* Get cp0 register index given its name */ |
| 60 |
int mips64_cp0_get_reg_index(char *name) |
| 61 |
{ |
| 62 |
int i; |
| 63 |
|
| 64 |
for(i=0;i<MIPS64_CP0_REG_NR;i++) |
| 65 |
if (!strcmp(mips64_cp0_reg_names[i],name)) |
| 66 |
return(i); |
| 67 |
|
| 68 |
return(-1); |
| 69 |
} |
| 70 |
|
| 71 |
/* Check that we are running in kernel mode */ |
| 72 |
int mips64_cp0_check_kernel_mode(cpu_mips_t *cpu) |
| 73 |
{ |
| 74 |
u_int cpu_mode; |
| 75 |
|
| 76 |
cpu_mode = mips64_cp0_get_mode(cpu); |
| 77 |
|
| 78 |
if (cpu_mode != MIPS_CP0_STATUS_KM) { |
| 79 |
mips64_general_exception(cpu,MIPS_CP0_CAUSE_ILLOP); |
| 80 |
return(1); |
| 81 |
} |
| 82 |
|
| 83 |
return(0); |
| 84 |
} |
| 85 |
|
| 86 |
/* Get value of random register */ |
| 87 |
static inline u_int mips64_cp0_get_random_reg(cpu_mips_t *cpu) |
| 88 |
{ |
| 89 |
u_int wired; |
| 90 |
|
| 91 |
/* We use the virtual count register as a basic "random" value */ |
| 92 |
wired = cpu->cp0.reg[MIPS_CP0_WIRED]; |
| 93 |
return(wired + (cpu->cp0_virt_cnt_reg % (cpu->cp0.tlb_entries - wired))); |
| 94 |
} |
| 95 |
|
| 96 |
/* Get a cp0 register (fast version) */ |
| 97 |
static inline m_uint64_t mips64_cp0_get_reg_fast(cpu_mips_t *cpu,u_int cp0_reg) |
| 98 |
{ |
| 99 |
mips_cp0_t *cp0 = &cpu->cp0; |
| 100 |
m_uint32_t delta,res; |
| 101 |
|
| 102 |
switch(cp0_reg) { |
| 103 |
case MIPS_CP0_COUNT: |
| 104 |
delta = cpu->cp0_virt_cmp_reg - cpu->cp0_virt_cnt_reg; |
| 105 |
res = (m_uint32_t)cp0->reg[MIPS_CP0_COMPARE]; |
| 106 |
res -= cpu->vm->clock_divisor * delta; |
| 107 |
return(sign_extend(res,32)); |
| 108 |
|
| 109 |
#if 1 |
| 110 |
case MIPS_CP0_COMPARE: |
| 111 |
return(sign_extend(cp0->reg[MIPS_CP0_COMPARE],32)); |
| 112 |
#else |
| 113 |
/* really useful and logical ? */ |
| 114 |
case MIPS_CP0_COMPARE: |
| 115 |
delta = cpu->cp0_virt_cmp_reg - cpu->cp0_virt_cnt_reg; |
| 116 |
res = (m_uint32_t)cp0->reg[MIPS_CP0_COUNT]; |
| 117 |
res += (cpu->vm->clock_divisor * delta); |
| 118 |
return(res); |
| 119 |
#endif |
| 120 |
case MIPS_CP0_INFO: |
| 121 |
return(MIPS64_R7000_TLB64_ENABLE); |
| 122 |
|
| 123 |
case MIPS_CP0_RANDOM: |
| 124 |
return(mips64_cp0_get_random_reg(cpu)); |
| 125 |
|
| 126 |
default: |
| 127 |
return(cp0->reg[cp0_reg]); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/* Get a cp0 register */ |
| 132 |
m_uint64_t mips64_cp0_get_reg(cpu_mips_t *cpu,u_int cp0_reg) |
| 133 |
{ |
| 134 |
return(mips64_cp0_get_reg_fast(cpu,cp0_reg)); |
| 135 |
} |
| 136 |
|
| 137 |
/* Set a cp0 register */ |
| 138 |
static inline void mips64_cp0_set_reg(cpu_mips_t *cpu,u_int cp0_reg, |
| 139 |
m_uint64_t val) |
| 140 |
{ |
| 141 |
mips_cp0_t *cp0 = &cpu->cp0; |
| 142 |
m_uint32_t delta; |
| 143 |
|
| 144 |
switch(cp0_reg) { |
| 145 |
case MIPS_CP0_STATUS: |
| 146 |
case MIPS_CP0_CAUSE: |
| 147 |
cp0->reg[cp0_reg] = val; |
| 148 |
mips64_update_irq_flag(cpu); |
| 149 |
break; |
| 150 |
|
| 151 |
case MIPS_CP0_PAGEMASK: |
| 152 |
cp0->reg[cp0_reg] = val & MIPS_TLB_PAGE_MASK; |
| 153 |
break; |
| 154 |
|
| 155 |
case MIPS_CP0_COMPARE: |
| 156 |
mips64_clear_irq(cpu,7); |
| 157 |
mips64_update_irq_flag(cpu); |
| 158 |
cp0->reg[cp0_reg] = val; |
| 159 |
|
| 160 |
delta = val - cp0->reg[MIPS_CP0_COUNT]; |
| 161 |
cpu->cp0_virt_cnt_reg = 0; |
| 162 |
cpu->cp0_virt_cmp_reg = delta / cpu->vm->clock_divisor; |
| 163 |
break; |
| 164 |
|
| 165 |
case MIPS_CP0_COUNT: |
| 166 |
cp0->reg[cp0_reg] = val; |
| 167 |
|
| 168 |
delta = cp0->reg[MIPS_CP0_COMPARE] - val; |
| 169 |
cpu->cp0_virt_cnt_reg = 0; |
| 170 |
cpu->cp0_virt_cmp_reg = delta / cpu->vm->clock_divisor; |
| 171 |
break; |
| 172 |
|
| 173 |
case MIPS_CP0_TLB_HI: |
| 174 |
cp0->reg[cp0_reg] = val & MIPS_CP0_HI_SAFE_MASK; |
| 175 |
break; |
| 176 |
|
| 177 |
case MIPS_CP0_TLB_LO_0: |
| 178 |
case MIPS_CP0_TLB_LO_1: |
| 179 |
cp0->reg[cp0_reg] = val & MIPS_CP0_LO_SAFE_MASK; |
| 180 |
break; |
| 181 |
|
| 182 |
case MIPS_CP0_RANDOM: |
| 183 |
case MIPS_CP0_PRID: |
| 184 |
case MIPS_CP0_CONFIG: |
| 185 |
/* read only registers */ |
| 186 |
break; |
| 187 |
|
| 188 |
case MIPS_CP0_WIRED: |
| 189 |
cp0->reg[cp0_reg] = val & MIPS64_TLB_IDX_MASK; |
| 190 |
break; |
| 191 |
|
| 192 |
default: |
| 193 |
cp0->reg[cp0_reg] = val; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/* Get a cp0 "set 1" register (R7000) */ |
| 198 |
m_uint64_t mips64_cp0_s1_get_reg(cpu_mips_t *cpu,u_int cp0_s1_reg) |
| 199 |
{ |
| 200 |
switch(cp0_s1_reg) { |
| 201 |
case MIPS_CP0_S1_CONFIG: |
| 202 |
return(0x7F << 25); |
| 203 |
|
| 204 |
case MIPS_CP0_S1_IPLLO: |
| 205 |
return(cpu->cp0.ipl_lo); |
| 206 |
|
| 207 |
case MIPS_CP0_S1_IPLHI: |
| 208 |
return(cpu->cp0.ipl_hi); |
| 209 |
|
| 210 |
case MIPS_CP0_S1_INTCTL: |
| 211 |
return(cpu->cp0.int_ctl); |
| 212 |
|
| 213 |
case MIPS_CP0_S1_DERRADDR0: |
| 214 |
return(cpu->cp0.derraddr0); |
| 215 |
|
| 216 |
case MIPS_CP0_S1_DERRADDR1: |
| 217 |
return(cpu->cp0.derraddr1); |
| 218 |
|
| 219 |
default: |
| 220 |
/* undefined register */ |
| 221 |
cpu_log(cpu->gen,"CP0_S1","trying to read unknown register %u\n", |
| 222 |
cp0_s1_reg); |
| 223 |
return(0); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
/* Set a cp0 "set 1" register (R7000) */ |
| 228 |
static inline void mips64_cp0_s1_set_reg(cpu_mips_t *cpu,u_int cp0_s1_reg, |
| 229 |
m_uint64_t val) |
| 230 |
{ |
| 231 |
mips_cp0_t *cp0 = &cpu->cp0; |
| 232 |
|
| 233 |
switch(cp0_s1_reg) { |
| 234 |
case MIPS_CP0_S1_IPLLO: |
| 235 |
cp0->ipl_lo = val; |
| 236 |
break; |
| 237 |
|
| 238 |
case MIPS_CP0_S1_IPLHI: |
| 239 |
cp0->ipl_hi = val; |
| 240 |
break; |
| 241 |
|
| 242 |
case MIPS_CP0_S1_INTCTL: |
| 243 |
cp0->int_ctl = val; |
| 244 |
break; |
| 245 |
|
| 246 |
case MIPS_CP0_S1_DERRADDR0: |
| 247 |
cp0->derraddr0 = val; |
| 248 |
break; |
| 249 |
|
| 250 |
case MIPS_CP0_S1_DERRADDR1: |
| 251 |
cp0->derraddr1 = val; |
| 252 |
break; |
| 253 |
|
| 254 |
default: |
| 255 |
cpu_log(cpu->gen, |
| 256 |
"CP0_S1","trying to set unknown register %u (val=0x%llx)\n", |
| 257 |
cp0_s1_reg,val); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
/* DMFC0 */ |
| 262 |
fastcall void mips64_cp0_exec_dmfc0(cpu_mips_t *cpu,u_int gp_reg,u_int cp0_reg) |
| 263 |
{ |
| 264 |
cpu->gpr[gp_reg] = mips64_cp0_get_reg_fast(cpu,cp0_reg); |
| 265 |
} |
| 266 |
|
| 267 |
/* DMTC0 */ |
| 268 |
fastcall void mips64_cp0_exec_dmtc0(cpu_mips_t *cpu,u_int gp_reg,u_int cp0_reg) |
| 269 |
{ |
| 270 |
mips64_cp0_set_reg(cpu,cp0_reg,cpu->gpr[gp_reg]); |
| 271 |
} |
| 272 |
|
| 273 |
/* MFC0 */ |
| 274 |
fastcall void mips64_cp0_exec_mfc0(cpu_mips_t *cpu,u_int gp_reg,u_int cp0_reg) |
| 275 |
{ |
| 276 |
cpu->gpr[gp_reg] = sign_extend(mips64_cp0_get_reg_fast(cpu,cp0_reg),32); |
| 277 |
} |
| 278 |
|
| 279 |
/* MTC0 */ |
| 280 |
fastcall void mips64_cp0_exec_mtc0(cpu_mips_t *cpu,u_int gp_reg,u_int cp0_reg) |
| 281 |
{ |
| 282 |
mips64_cp0_set_reg(cpu,cp0_reg,cpu->gpr[gp_reg] & 0xffffffff); |
| 283 |
} |
| 284 |
|
| 285 |
/* CFC0 */ |
| 286 |
fastcall void mips64_cp0_exec_cfc0(cpu_mips_t *cpu,u_int gp_reg,u_int cp0_reg) |
| 287 |
{ |
| 288 |
cpu->gpr[gp_reg] = sign_extend(mips64_cp0_s1_get_reg(cpu,cp0_reg),32); |
| 289 |
} |
| 290 |
|
| 291 |
/* CTC0 */ |
| 292 |
fastcall void mips64_cp0_exec_ctc0(cpu_mips_t *cpu,u_int gp_reg,u_int cp0_reg) |
| 293 |
{ |
| 294 |
mips64_cp0_s1_set_reg(cpu,cp0_reg,cpu->gpr[gp_reg] & 0xffffffff); |
| 295 |
} |
| 296 |
|
| 297 |
/* Get the page size corresponding to a page mask */ |
| 298 |
static inline m_uint32_t get_page_size(m_uint32_t page_mask) |
| 299 |
{ |
| 300 |
return((page_mask + 0x2000) >> 1); |
| 301 |
} |
| 302 |
|
| 303 |
/* Write page size in buffer */ |
| 304 |
static char *get_page_size_str(char *buffer,size_t len,m_uint32_t page_mask) |
| 305 |
{ |
| 306 |
m_uint32_t page_size; |
| 307 |
|
| 308 |
page_size = get_page_size(page_mask); |
| 309 |
|
| 310 |
/* Mb ? */ |
| 311 |
if (page_size >= (1024*1024)) |
| 312 |
snprintf(buffer,len,"%uMB",page_size >> 20); |
| 313 |
else |
| 314 |
snprintf(buffer,len,"%uKB",page_size >> 10); |
| 315 |
|
| 316 |
return buffer; |
| 317 |
} |
| 318 |
|
| 319 |
/* Execute a callback for the specified entry */ |
| 320 |
static inline void mips64_cp0_tlb_callback(cpu_mips_t *cpu,tlb_entry_t *entry, |
| 321 |
int action) |
| 322 |
{ |
| 323 |
_maybe_used m_uint64_t vaddr,paddr0,paddr1; |
| 324 |
_maybe_used m_uint32_t psize; |
| 325 |
|
| 326 |
vaddr = entry->hi & mips64_cp0_get_vpn2_mask(cpu); |
| 327 |
psize = get_page_size(entry->mask); |
| 328 |
|
| 329 |
if (entry->lo0 & MIPS_TLB_V_MASK) { |
| 330 |
paddr0 = (entry->lo0 & MIPS_TLB_PFN_MASK) << 6; |
| 331 |
|
| 332 |
/*printf("TLB: vaddr=0x%8.8llx -> paddr0=0x%10.10llx (size=0x%8.8x), " |
| 333 |
"action=%s\n", |
| 334 |
vaddr,paddr0,psize, |
| 335 |
(action == 0) ? "ADD" : "DELETE");*/ |
| 336 |
} |
| 337 |
|
| 338 |
if (entry->lo1 & MIPS_TLB_V_MASK) { |
| 339 |
paddr1 = (entry->lo1 & MIPS_TLB_PFN_MASK) << 6; |
| 340 |
|
| 341 |
/*printf("TLB: vaddr=0x%8.8llx -> paddr1=0x%10.10llx (size=0x%8.8x), " |
| 342 |
"action=%s\n", |
| 343 |
vaddr,paddr1,psize, |
| 344 |
(action == 0) ? "ADD" : "DELETE");*/ |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
/* TLB lookup */ |
| 349 |
int mips64_cp0_tlb_lookup(cpu_mips_t *cpu,m_uint64_t vaddr, |
| 350 |
u_int op_type,mts_map_t *res) |
| 351 |
{ |
| 352 |
mips_cp0_t *cp0 = &cpu->cp0; |
| 353 |
m_uint64_t vpn_addr,vpn2_mask; |
| 354 |
m_uint64_t page_mask,hi_addr; |
| 355 |
m_uint32_t page_size,pca; |
| 356 |
tlb_entry_t *entry; |
| 357 |
u_int asid; |
| 358 |
int i; |
| 359 |
|
| 360 |
vpn2_mask = mips64_cp0_get_vpn2_mask(cpu); |
| 361 |
vpn_addr = vaddr & vpn2_mask; |
| 362 |
|
| 363 |
asid = cp0->reg[MIPS_CP0_TLB_HI] & MIPS_TLB_ASID_MASK; |
| 364 |
|
| 365 |
for(i=0;i<cp0->tlb_entries;i++) { |
| 366 |
entry = &cp0->tlb[i]; |
| 367 |
|
| 368 |
page_mask = ~entry->mask; |
| 369 |
hi_addr = entry->hi & vpn2_mask & page_mask; |
| 370 |
|
| 371 |
if (((vpn_addr & page_mask) == hi_addr) && |
| 372 |
((entry->hi & MIPS_TLB_G_MASK) || |
| 373 |
((entry->hi & MIPS_TLB_ASID_MASK) == asid))) |
| 374 |
{ |
| 375 |
page_size = get_page_size(entry->mask); |
| 376 |
|
| 377 |
if ((vaddr & page_size) == 0) { |
| 378 |
/* Even Page */ |
| 379 |
if (entry->lo0 & MIPS_TLB_V_MASK) |
| 380 |
{ |
| 381 |
/* Check write protection */ |
| 382 |
if ((op_type == MTS_WRITE) && !(entry->lo0 & MIPS_TLB_D_MASK)) |
| 383 |
return MIPS_TLB_LOOKUP_MOD; |
| 384 |
|
| 385 |
res->flags = 0; |
| 386 |
res->vaddr = vaddr & MIPS_MIN_PAGE_MASK; |
| 387 |
res->paddr = (entry->lo0 & MIPS_TLB_PFN_MASK) << 6; |
| 388 |
res->paddr += (res->vaddr & (page_size-1)); |
| 389 |
res->paddr &= cpu->addr_bus_mask; |
| 390 |
|
| 391 |
res->offset = vaddr & MIPS_MIN_PAGE_IMASK; |
| 392 |
|
| 393 |
pca = (entry->lo0 & MIPS_TLB_C_MASK); |
| 394 |
pca >>= MIPS_TLB_C_SHIFT; |
| 395 |
res->cached = mips64_cca_cached(pca); |
| 396 |
|
| 397 |
if (!(entry->lo0 & MIPS_TLB_D_MASK)) |
| 398 |
res->flags |= MTS_FLAG_RO; |
| 399 |
|
| 400 |
return(MIPS_TLB_LOOKUP_OK); |
| 401 |
} |
| 402 |
} else { |
| 403 |
/* Odd Page */ |
| 404 |
if (entry->lo1 & MIPS_TLB_V_MASK) |
| 405 |
{ |
| 406 |
/* Check write protection */ |
| 407 |
if ((op_type == MTS_WRITE) && !(entry->lo1 & MIPS_TLB_D_MASK)) |
| 408 |
return MIPS_TLB_LOOKUP_MOD; |
| 409 |
|
| 410 |
res->flags = 0; |
| 411 |
res->vaddr = vaddr & MIPS_MIN_PAGE_MASK; |
| 412 |
res->paddr = (entry->lo1 & MIPS_TLB_PFN_MASK) << 6; |
| 413 |
res->paddr += (res->vaddr & (page_size-1)); |
| 414 |
res->paddr &= cpu->addr_bus_mask; |
| 415 |
|
| 416 |
res->offset = vaddr & MIPS_MIN_PAGE_IMASK; |
| 417 |
|
| 418 |
pca = (entry->lo1 & MIPS_TLB_C_MASK); |
| 419 |
pca >>= MIPS_TLB_C_SHIFT; |
| 420 |
res->cached = mips64_cca_cached(pca); |
| 421 |
|
| 422 |
if (!(entry->lo0 & MIPS_TLB_D_MASK)) |
| 423 |
res->flags |= MTS_FLAG_RO; |
| 424 |
|
| 425 |
return(MIPS_TLB_LOOKUP_OK); |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
/* Invalid entry */ |
| 430 |
return(MIPS_TLB_LOOKUP_INVALID); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
/* No matching entry */ |
| 435 |
return(MIPS_TLB_LOOKUP_MISS); |
| 436 |
} |
| 437 |
|
| 438 |
/* TLBP: Probe a TLB entry */ |
| 439 |
fastcall void mips64_cp0_exec_tlbp(cpu_mips_t *cpu) |
| 440 |
{ |
| 441 |
mips_cp0_t *cp0 = &cpu->cp0; |
| 442 |
m_uint64_t hi_reg,asid; |
| 443 |
m_uint64_t vpn2,vpn2_mask; |
| 444 |
m_uint64_t page_mask; |
| 445 |
tlb_entry_t *entry; |
| 446 |
int i; |
| 447 |
|
| 448 |
vpn2_mask = mips64_cp0_get_vpn2_mask(cpu); |
| 449 |
hi_reg = cp0->reg[MIPS_CP0_TLB_HI]; |
| 450 |
asid = hi_reg & MIPS_TLB_ASID_MASK; |
| 451 |
vpn2 = hi_reg & vpn2_mask; |
| 452 |
|
| 453 |
cp0->reg[MIPS_CP0_INDEX] = 0xffffffff80000000ULL; |
| 454 |
|
| 455 |
for(i=0;i<cp0->tlb_entries;i++) { |
| 456 |
entry = &cp0->tlb[i]; |
| 457 |
page_mask = ~entry->mask; |
| 458 |
|
| 459 |
if (((entry->hi & vpn2_mask & page_mask) == (vpn2 & page_mask)) && |
| 460 |
((entry->hi & MIPS_TLB_G_MASK) || |
| 461 |
((entry->hi & MIPS_TLB_ASID_MASK) == asid))) |
| 462 |
{ |
| 463 |
cp0->reg[MIPS_CP0_INDEX] = i; |
| 464 |
#if DEBUG_TLB_ACTIVITY |
| 465 |
printf("CPU: CP0_TLBP returned %u\n",i); |
| 466 |
tlb_dump(cpu); |
| 467 |
#endif |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
/* TLBR: Read Indexed TLB entry */ |
| 473 |
fastcall void mips64_cp0_exec_tlbr(cpu_mips_t *cpu) |
| 474 |
{ |
| 475 |
mips_cp0_t *cp0 = &cpu->cp0; |
| 476 |
tlb_entry_t *entry; |
| 477 |
u_int index; |
| 478 |
|
| 479 |
index = cp0->reg[MIPS_CP0_INDEX]; |
| 480 |
|
| 481 |
#if DEBUG_TLB_ACTIVITY |
| 482 |
cpu_log(cpu,"TLB","CP0_TLBR: reading entry %u.\n",index); |
| 483 |
#endif |
| 484 |
|
| 485 |
if (index < cp0->tlb_entries) |
| 486 |
{ |
| 487 |
entry = &cp0->tlb[index]; |
| 488 |
|
| 489 |
cp0->reg[MIPS_CP0_PAGEMASK] = entry->mask; |
| 490 |
cp0->reg[MIPS_CP0_TLB_HI] = entry->hi; |
| 491 |
cp0->reg[MIPS_CP0_TLB_LO_0] = entry->lo0; |
| 492 |
cp0->reg[MIPS_CP0_TLB_LO_1] = entry->lo1; |
| 493 |
|
| 494 |
/* |
| 495 |
* The G bit must be reported in both Lo0 and Lo1 registers, |
| 496 |
* and cleared in Hi register. |
| 497 |
*/ |
| 498 |
if (entry->hi & MIPS_TLB_G_MASK) { |
| 499 |
cp0->reg[MIPS_CP0_TLB_LO_0] |= MIPS_CP0_LO_G_MASK; |
| 500 |
cp0->reg[MIPS_CP0_TLB_LO_1] |= MIPS_CP0_LO_G_MASK; |
| 501 |
cp0->reg[MIPS_CP0_TLB_HI] &= ~MIPS_TLB_G_MASK; |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
/* TLBW: Write a TLB entry */ |
| 507 |
static inline void mips64_cp0_exec_tlbw(cpu_mips_t *cpu,u_int index) |
| 508 |
{ |
| 509 |
mips_cp0_t *cp0 = &cpu->cp0; |
| 510 |
tlb_entry_t *entry; |
| 511 |
|
| 512 |
#if DEBUG_TLB_ACTIVITY |
| 513 |
cpu_log(cpu,"TLB","CP0_TLBWI: writing entry %u " |
| 514 |
"[mask=0x%8.8llx,hi=0x%8.8llx,lo0=0x%8.8llx,lo1=0x%8.8llx]\n", |
| 515 |
index,cp0->reg[MIPS_CP0_PAGEMASK],cp0->reg[MIPS_CP0_TLB_HI], |
| 516 |
cp0->reg[MIPS_CP0_TLB_LO_0],cp0->reg[MIPS_CP0_TLB_LO_1]); |
| 517 |
#endif |
| 518 |
|
| 519 |
if (index < cp0->tlb_entries) |
| 520 |
{ |
| 521 |
entry = &cp0->tlb[index]; |
| 522 |
|
| 523 |
mips64_cp0_tlb_callback(cpu,entry,TLB_ZONE_ADD); |
| 524 |
|
| 525 |
entry->mask = cp0->reg[MIPS_CP0_PAGEMASK] & MIPS_TLB_PAGE_MASK; |
| 526 |
entry->hi = cp0->reg[MIPS_CP0_TLB_HI]; |
| 527 |
entry->lo0 = cp0->reg[MIPS_CP0_TLB_LO_0]; |
| 528 |
entry->lo1 = cp0->reg[MIPS_CP0_TLB_LO_1]; |
| 529 |
|
| 530 |
/* if G bit is set in lo0 and lo1, set it in hi */ |
| 531 |
if ((entry->lo0 & entry->lo1) & MIPS_CP0_LO_G_MASK) |
| 532 |
entry->hi |= MIPS_TLB_G_MASK; |
| 533 |
else |
| 534 |
entry->hi &= ~MIPS_TLB_G_MASK; |
| 535 |
|
| 536 |
/* Clear G bit in TLB lo0 and lo1 */ |
| 537 |
entry->lo0 &= ~MIPS_CP0_LO_G_MASK; |
| 538 |
entry->lo1 &= ~MIPS_CP0_LO_G_MASK; |
| 539 |
|
| 540 |
/* Inform the MTS subsystem */ |
| 541 |
cpu->mts_invalidate(cpu); |
| 542 |
|
| 543 |
mips64_cp0_tlb_callback(cpu,entry,TLB_ZONE_DELETE); |
| 544 |
|
| 545 |
#if DEBUG_TLB_ACTIVITY |
| 546 |
mips64_tlb_dump_entry(cpu,index); |
| 547 |
#endif |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
/* TLBWI: Write Indexed TLB entry */ |
| 552 |
fastcall void mips64_cp0_exec_tlbwi(cpu_mips_t *cpu) |
| 553 |
{ |
| 554 |
mips64_cp0_exec_tlbw(cpu,cpu->cp0.reg[MIPS_CP0_INDEX]); |
| 555 |
} |
| 556 |
|
| 557 |
/* TLBWR: Write Random TLB entry */ |
| 558 |
fastcall void mips64_cp0_exec_tlbwr(cpu_mips_t *cpu) |
| 559 |
{ |
| 560 |
mips64_cp0_exec_tlbw(cpu,mips64_cp0_get_random_reg(cpu)); |
| 561 |
} |
| 562 |
|
| 563 |
/* Raw dump of the TLB */ |
| 564 |
void mips64_tlb_raw_dump(cpu_gen_t *cpu) |
| 565 |
{ |
| 566 |
cpu_mips_t *mcpu = CPU_MIPS64(cpu); |
| 567 |
tlb_entry_t *entry; |
| 568 |
u_int i; |
| 569 |
|
| 570 |
printf("TLB dump:\n"); |
| 571 |
|
| 572 |
for(i=0;i<mcpu->cp0.tlb_entries;i++) { |
| 573 |
entry = &mcpu->cp0.tlb[i]; |
| 574 |
printf(" %2d: mask=0x%16.16llx hi=0x%16.16llx " |
| 575 |
"lo0=0x%16.16llx lo1=0x%16.16llx\n", |
| 576 |
i, entry->mask, entry->hi, entry->lo0, entry->lo1); |
| 577 |
} |
| 578 |
|
| 579 |
printf("\n"); |
| 580 |
} |
| 581 |
|
| 582 |
/* Dump the specified TLB entry */ |
| 583 |
void mips64_tlb_dump_entry(cpu_mips_t *cpu,u_int index) |
| 584 |
{ |
| 585 |
tlb_entry_t *entry; |
| 586 |
char buffer[256]; |
| 587 |
|
| 588 |
entry = &cpu->cp0.tlb[index]; |
| 589 |
|
| 590 |
/* virtual Address */ |
| 591 |
printf(" %2d: vaddr=0x%8.8llx ", |
| 592 |
index, entry->hi & mips64_cp0_get_vpn2_mask(cpu)); |
| 593 |
|
| 594 |
/* global or ASID */ |
| 595 |
if (entry->hi & MIPS_TLB_G_MASK) |
| 596 |
printf("(global) "); |
| 597 |
else |
| 598 |
printf("(asid 0x%2.2llx) ",entry->hi & MIPS_TLB_ASID_MASK); |
| 599 |
|
| 600 |
/* 1st page: Lo0 */ |
| 601 |
printf("p0="); |
| 602 |
|
| 603 |
if (entry->lo0 & MIPS_TLB_V_MASK) |
| 604 |
printf("0x%9.9llx",(entry->lo0 & MIPS_TLB_PFN_MASK) << 6); |
| 605 |
else |
| 606 |
printf("(invalid) "); |
| 607 |
|
| 608 |
printf(" %c ",(entry->lo0 & MIPS_TLB_D_MASK) ? 'D' : ' '); |
| 609 |
|
| 610 |
/* 2nd page: Lo1 */ |
| 611 |
printf("p1="); |
| 612 |
|
| 613 |
if (entry->lo1 & MIPS_TLB_V_MASK) |
| 614 |
printf("0x%9.9llx",(entry->lo1 & MIPS_TLB_PFN_MASK) << 6); |
| 615 |
else |
| 616 |
printf("(invalid) "); |
| 617 |
|
| 618 |
printf(" %c ",(entry->lo1 & MIPS_TLB_D_MASK) ? 'D' : ' '); |
| 619 |
|
| 620 |
/* page size */ |
| 621 |
printf(" (%s)\n",get_page_size_str(buffer,sizeof(buffer),entry->mask)); |
| 622 |
} |
| 623 |
|
| 624 |
/* Human-Readable dump of the TLB */ |
| 625 |
void mips64_tlb_dump(cpu_gen_t *cpu) |
| 626 |
{ |
| 627 |
cpu_mips_t *mcpu = CPU_MIPS64(cpu); |
| 628 |
u_int i; |
| 629 |
|
| 630 |
printf("TLB dump:\n"); |
| 631 |
|
| 632 |
for(i=0;i<mcpu->cp0.tlb_entries;i++) |
| 633 |
mips64_tlb_dump_entry(mcpu,i); |
| 634 |
|
| 635 |
printf("\n"); |
| 636 |
} |