1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* Copyright (c) 2020-2021 Peng Cheng Laboratory 4* 5* XiangShan is licensed under Mulan PSL v2. 6* You can use this software according to the terms and conditions of the Mulan PSL v2. 7* You may obtain a copy of Mulan PSL v2 at: 8* http://license.coscl.org.cn/MulanPSL2 9* 10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13* 14* See the Mulan PSL v2 for more details. 15***************************************************************************************/ 16package xiangshan.frontend 17 18import org.chipsalliance.cde.config.Parameters 19import chisel3._ 20import chisel3.util._ 21import utils._ 22import utility._ 23import xiangshan._ 24import xiangshan.frontend._ 25 26class RASEntry()(implicit p: Parameters) extends XSBundle { 27 val retAddr = UInt(VAddrBits.W) 28 val ctr = UInt(8.W) // layer of nested call functions 29 def =/=(that: RASEntry) = this.retAddr =/= that.retAddr || this.ctr =/= that.ctr 30} 31 32class RASPtr(implicit p: Parameters) extends CircularQueuePtr[RASPtr]( 33 p => p(XSCoreParamsKey).RasSpecSize 34){ 35} 36 37object RASPtr { 38 def apply(f: Bool, v: UInt)(implicit p: Parameters): RASPtr = { 39 val ptr = Wire(new RASPtr) 40 ptr.flag := f 41 ptr.value := v 42 ptr 43 } 44 def inverse(ptr: RASPtr)(implicit p: Parameters): RASPtr = { 45 apply(!ptr.flag, ptr.value) 46 } 47} 48 49class RASInternalMeta(implicit p: Parameters) extends XSBundle { 50 val ssp = UInt(log2Up(RasSize).W) 51 val sctr = UInt(RasCtrSize.W) 52 val TOSW = new RASPtr 53 val TOSR = new RASPtr 54 val NOS = new RASPtr 55} 56 57object RASInternalMeta { 58 def apply(ssp: UInt, sctr: UInt, TOSW: RASPtr, TOSR: RASPtr, NOS: RASPtr)(implicit p: Parameters):RASInternalMeta = { 59 val e = Wire(new RASInternalMeta) 60 e.ssp := ssp 61 e.TOSW := TOSW 62 e.TOSR := TOSR 63 e.NOS := NOS 64 e 65 } 66} 67 68class RASMeta(implicit p: Parameters) extends XSBundle { 69 val ssp = UInt(log2Up(RasSize).W) 70 val TOSW = new RASPtr 71} 72 73object RASMeta { 74 def apply(ssp: UInt, sctr: UInt, TOSW: RASPtr, TOSR: RASPtr, NOS: RASPtr)(implicit p: Parameters):RASMeta = { 75 val e = Wire(new RASMeta) 76 e.ssp := ssp 77 e.TOSW := TOSW 78 e 79 } 80} 81 82class RASDebug(implicit p: Parameters) extends XSBundle { 83 val spec_queue = Output(Vec(RasSpecSize, new RASEntry)) 84 val spec_nos = Output(Vec(RasSpecSize, new RASPtr)) 85 val commit_stack = Output(Vec(RasSize, new RASEntry)) 86} 87 88class RAS(implicit p: Parameters) extends BasePredictor { 89 override val meta_size = WireInit(0.U.asTypeOf(new RASMeta)).getWidth 90 91 object RASEntry { 92 def apply(retAddr: UInt, ctr: UInt): RASEntry = { 93 val e = Wire(new RASEntry) 94 e.retAddr := retAddr 95 e.ctr := ctr 96 e 97 } 98 } 99 100 101 class RASStack(rasSize: Int, rasSpecSize: Int) extends XSModule with HasCircularQueuePtrHelper { 102 val io = IO(new Bundle { 103 val spec_push_valid = Input(Bool()) 104 val spec_pop_valid = Input(Bool()) 105 val spec_push_addr = Input(UInt(VAddrBits.W)) 106 // for write bypass between s2 and s3 107 108 val s2_fire = Input(Bool()) 109 val s3_fire = Input(Bool()) 110 val s3_cancel = Input(Bool()) 111 val s3_meta = Input(new RASInternalMeta) 112 val s3_missed_pop = Input(Bool()) 113 val s3_missed_push = Input(Bool()) 114 val s3_pushAddr = Input(UInt(VAddrBits.W)) 115 val spec_pop_addr = Output(UInt(VAddrBits.W)) 116 117 val commit_push_valid = Input(Bool()) 118 val commit_pop_valid = Input(Bool()) 119 val commit_push_addr = Input(UInt(VAddrBits.W)) 120 val commit_meta_TOSW = Input(new RASPtr) 121 // for debug purpose only 122 val commit_meta_ssp = Input(UInt(log2Up(RasSize).W)) 123 124 val redirect_valid = Input(Bool()) 125 val redirect_isCall = Input(Bool()) 126 val redirect_isRet = Input(Bool()) 127 val redirect_meta_ssp = Input(UInt(log2Up(RasSize).W)) 128 val redirect_meta_sctr = Input(UInt(RasCtrSize.W)) 129 val redirect_meta_TOSW = Input(new RASPtr) 130 val redirect_meta_TOSR = Input(new RASPtr) 131 val redirect_meta_NOS = Input(new RASPtr) 132 val redirect_callAddr = Input(UInt(VAddrBits.W)) 133 134 val ssp = Output(UInt(log2Up(RasSize).W)) 135 val sctr = Output(UInt(RasCtrSize.W)) 136 val nsp = Output(UInt(log2Up(RasSize).W)) 137 val TOSR = Output(new RASPtr) 138 val TOSW = Output(new RASPtr) 139 val NOS = Output(new RASPtr) 140 val BOS = Output(new RASPtr) 141 142 val debug = new RASDebug 143 }) 144 145 val commit_stack = RegInit(VecInit(Seq.fill(RasSize)(RASEntry(0.U, 0.U)))) 146 val spec_queue = RegInit(VecInit(Seq.fill(rasSpecSize)(RASEntry(0.U, 0.U)))) 147 val spec_nos = RegInit(VecInit(Seq.fill(rasSpecSize)(RASPtr(false.B, 0.U)))) 148 149 val nsp = RegInit(0.U(log2Up(rasSize).W)) 150 val ssp = RegInit(0.U(log2Up(rasSize).W)) 151 152 val sctr = RegInit(0.U(RasCtrSize.W)) 153 val TOSR = RegInit(RASPtr(true.B, (RasSpecSize - 1).U)) 154 val TOSW = RegInit(RASPtr(false.B, 0.U)) 155 val BOS = RegInit(RASPtr(false.B, 0.U)) 156 157 val spec_overflowed = RegInit(false.B) 158 159 val writeBypassEntry = Reg(new RASEntry) 160 val writeBypassNos = Reg(new RASPtr) 161 162 val writeBypassValid = RegInit(0.B) 163 val writeBypassValidWire = Wire(Bool()) 164 165 def TOSRinRange(currentTOSR: RASPtr, currentTOSW: RASPtr) = { 166 val inflightValid = WireInit(false.B) 167 // if in range, TOSR should be no younger than BOS and strictly younger than TOSW 168 when (!isBefore(currentTOSR, BOS) && isBefore(currentTOSR, currentTOSW)) { 169 inflightValid := true.B 170 } 171 inflightValid 172 } 173 174 def getCommitTop(currentSsp: UInt) = { 175 commit_stack(currentSsp) 176 } 177 178 def getTopNos(currentTOSR: RASPtr, allowBypass: Boolean):RASPtr = { 179 val ret = Wire(new RASPtr) 180 if (allowBypass){ 181 when (writeBypassValid) { 182 ret := writeBypassNos 183 } .otherwise { 184 ret := spec_nos(TOSR.value) 185 } 186 } else { 187 ret := spec_nos(TOSR.value) // invalid when TOSR is not in range 188 } 189 ret 190 } 191 192 def getTop(currentSsp: UInt, currentSctr: UInt, currentTOSR: RASPtr, currentTOSW: RASPtr, allowBypass: Boolean):RASEntry = { 193 val ret = Wire(new RASEntry) 194 if (allowBypass) { 195 when (writeBypassValid) { 196 ret := writeBypassEntry 197 } .elsewhen (TOSRinRange(currentTOSR, currentTOSW)) { 198 ret := spec_queue(currentTOSR.value) 199 } .otherwise { 200 ret := getCommitTop(currentSsp) 201 } 202 } else { 203 when (TOSRinRange(currentTOSR, currentTOSW)) { 204 ret := spec_queue(currentTOSR.value) 205 } .otherwise { 206 ret := getCommitTop(currentSsp) 207 } 208 } 209 210 ret 211 } 212 213 // it would be unsafe for specPtr manipulation if specSize is not power of 2 214 assert(log2Up(RasSpecSize) == log2Floor(RasSpecSize)) 215 def ctrMax = ((1l << RasCtrSize) - 1).U 216 def ptrInc(ptr: UInt) = ptr + 1.U 217 def ptrDec(ptr: UInt) = ptr - 1.U 218 219 def specPtrInc(ptr: RASPtr) = ptr + 1.U 220 def specPtrDec(ptr: RASPtr) = ptr - 1.U 221 222 223 224 225 226 227 when (io.redirect_valid && io.redirect_isCall) { 228 writeBypassValidWire := true.B 229 writeBypassValid := true.B 230 } .elsewhen (io.redirect_valid) { 231 // clear current top writeBypass if doing redirect 232 writeBypassValidWire := false.B 233 writeBypassValid := false.B 234 } .elsewhen (io.s2_fire) { 235 writeBypassValidWire := io.spec_push_valid 236 writeBypassValid := io.spec_push_valid 237 } .elsewhen (io.s3_fire) { 238 writeBypassValidWire := false.B 239 writeBypassValid := false.B 240 } .otherwise { 241 writeBypassValidWire := writeBypassValid 242 } 243 244 245 246 val topEntry = getTop(ssp, sctr, TOSR, TOSW, true) 247 val topNos = getTopNos(TOSR, true) 248 val redirectTopEntry = getTop(io.redirect_meta_ssp, io.redirect_meta_sctr, io.redirect_meta_TOSR, io.redirect_meta_TOSW, false) 249 val redirectTopNos = io.redirect_meta_NOS 250 val s3TopEntry = getTop(io.s3_meta.ssp, io.s3_meta.sctr, io.s3_meta.TOSR, io.s3_meta.TOSW, false) 251 val s3TopNos = io.s3_meta.NOS 252 253 val writeEntry = Wire(new RASEntry) 254 val writeNos = Wire(new RASPtr) 255 writeEntry.retAddr := Mux(io.redirect_valid && io.redirect_isCall, io.redirect_callAddr, io.spec_push_addr) 256 writeEntry.ctr := Mux(io.redirect_valid && io.redirect_isCall, 257 Mux(redirectTopEntry.retAddr === io.redirect_callAddr && redirectTopEntry.ctr < ctrMax, io.redirect_meta_sctr + 1.U, 0.U), 258 Mux(topEntry.retAddr === io.spec_push_addr && topEntry.ctr < ctrMax, sctr + 1.U, 0.U)) 259 260 writeNos := Mux(io.redirect_valid && io.redirect_isCall, 261 io.redirect_meta_NOS, TOSR) 262 263 when (io.spec_push_valid || (io.redirect_valid && io.redirect_isCall)) { 264 writeBypassEntry := writeEntry 265 writeBypassNos := writeNos 266 } 267 268 val realPush = Wire(Bool()) 269 val realWriteEntry = Wire(new RASEntry) 270 val timingTop = RegInit(0.U.asTypeOf(new RASEntry)) 271 val timingNos = RegInit(0.U.asTypeOf(new RASPtr)) 272 273 when (writeBypassValidWire) { 274 when ((io.redirect_valid && io.redirect_isCall) || io.spec_push_valid) { 275 timingTop := writeEntry 276 timingNos := writeNos 277 } .otherwise { 278 timingTop := writeBypassEntry 279 timingNos := writeBypassNos 280 } 281 282 } .elsewhen (io.redirect_valid && io.redirect_isRet) { 283 // getTop using redirect Nos as TOSR 284 val popRedSsp = Wire(UInt(log2Up(rasSize).W)) 285 val popRedSctr = Wire(UInt(RasCtrSize.W)) 286 val popRedTOSR = io.redirect_meta_NOS 287 val popRedTOSW = io.redirect_meta_TOSW 288 289 when (io.redirect_meta_sctr > 0.U) { 290 popRedSctr := io.redirect_meta_sctr - 1.U 291 popRedSsp := io.redirect_meta_ssp 292 } .elsewhen (TOSRinRange(popRedTOSR, TOSW)) { 293 popRedSsp := ptrDec(io.redirect_meta_ssp) 294 popRedSctr := spec_queue(popRedTOSR.value).ctr 295 } .otherwise { 296 popRedSsp := ptrDec(io.redirect_meta_ssp) 297 popRedSctr := getCommitTop(ptrDec(io.redirect_meta_ssp)).ctr 298 } 299 // We are deciding top for the next cycle, no need to use bypass here 300 timingTop := getTop(popRedSsp, popRedSctr, popRedTOSR, popRedTOSW, false) 301 } .elsewhen (io.redirect_valid) { 302 // Neither call nor ret 303 val popSsp = io.redirect_meta_ssp 304 val popSctr = io.redirect_meta_sctr 305 val popTOSR = io.redirect_meta_TOSR 306 val popTOSW = io.redirect_meta_TOSW 307 308 timingTop := getTop(popSsp, popSctr, popTOSR, popTOSW, false) 309 310 } .elsewhen (io.spec_pop_valid) { 311 // getTop using current Nos as TOSR 312 val popSsp = Wire(UInt(log2Up(rasSize).W)) 313 val popSctr = Wire(UInt(RasCtrSize.W)) 314 val popTOSR = topNos 315 val popTOSW = TOSW 316 317 when (sctr > 0.U) { 318 popSctr := sctr - 1.U 319 popSsp := ssp 320 } .elsewhen (TOSRinRange(popTOSR, TOSW)) { 321 popSsp := ptrDec(ssp) 322 popSctr := spec_queue(popTOSR.value).ctr 323 } .otherwise { 324 popSsp := ptrDec(ssp) 325 popSctr := getCommitTop(ptrDec(ssp)).ctr 326 } 327 // We are deciding top for the next cycle, no need to use bypass here 328 timingTop := getTop(popSsp, popSctr, popTOSR, popTOSW, false) 329 } .elsewhen (realPush) { 330 // just updating spec queue, cannot read from there 331 timingTop := realWriteEntry 332 } .elsewhen (io.s3_cancel) { 333 // s3 is different with s2 334 timingTop := getTop(io.s3_meta.ssp, io.s3_meta.sctr, io.s3_meta.TOSR, io.s3_meta.TOSW, false) 335 when (io.s3_missed_push) { 336 val writeEntry_s3 = Wire(new RASEntry) 337 timingTop := writeEntry_s3 338 writeEntry_s3.retAddr := io.s3_pushAddr 339 writeEntry_s3.ctr := Mux(timingTop.retAddr === io.s3_pushAddr && io.s3_meta.sctr < ctrMax, io.s3_meta.sctr + 1.U, 0.U) 340 } .elsewhen (io.s3_missed_pop) { 341 val popRedSsp_s3 = Wire(UInt(log2Up(rasSize).W)) 342 val popRedSctr_s3 = Wire(UInt(RasCtrSize.W)) 343 val popRedTOSR_s3 = io.s3_meta.NOS 344 val popRedTOSW_s3 = io.s3_meta.TOSW 345 346 when (io.s3_meta.sctr > 0.U) { 347 popRedSctr_s3 := io.s3_meta.sctr - 1.U 348 popRedSsp_s3 := io.s3_meta.ssp 349 } .elsewhen (TOSRinRange(popRedTOSR_s3, popRedTOSW_s3)) { 350 popRedSsp_s3 := ptrDec(io.s3_meta.ssp) 351 popRedSctr_s3 := spec_queue(popRedTOSR_s3.value).ctr 352 } .otherwise { 353 popRedSsp_s3 := ptrDec(io.s3_meta.ssp) 354 popRedSctr_s3 := getCommitTop(ptrDec(io.s3_meta.ssp)).ctr 355 } 356 // We are deciding top for the next cycle, no need to use bypass here 357 timingTop := getTop(popRedSsp_s3, popRedSctr_s3, popRedTOSR_s3, popRedTOSW_s3, false) 358 } 359 } .otherwise { 360 // easy case 361 val popSsp = ssp 362 val popSctr = sctr 363 val popTOSR = TOSR 364 val popTOSW = TOSW 365 timingTop := getTop(popSsp, popSctr, popTOSR, popTOSW, false) 366 } 367 val diffTop = Mux(writeBypassValid, writeBypassEntry.retAddr, topEntry.retAddr) 368 369 XSPerfAccumulate("ras_top_mismatch", diffTop =/= timingTop.retAddr); 370 // could diff when more pop than push and a commit stack is updated with inflight info 371 372 val realWriteEntry_next = RegEnable(writeEntry, io.s2_fire || io.redirect_isCall) 373 val s3_missPushEntry = Wire(new RASEntry) 374 val s3_missPushAddr = Wire(new RASPtr) 375 val s3_missPushNos = Wire(new RASPtr) 376 377 s3_missPushEntry.retAddr := io.s3_pushAddr 378 s3_missPushEntry.ctr := Mux(s3TopEntry.retAddr === io.s3_pushAddr && s3TopEntry.ctr < ctrMax, io.s3_meta.sctr + 1.U, 0.U) 379 s3_missPushAddr := io.s3_meta.TOSW 380 s3_missPushNos := io.s3_meta.TOSR 381 382 383 384 realWriteEntry := Mux(io.redirect_isCall, realWriteEntry_next, 385 Mux(io.s3_missed_push, s3_missPushEntry, 386 realWriteEntry_next)) 387 388 val realWriteAddr_next = RegEnable(Mux(io.redirect_valid && io.redirect_isCall, io.redirect_meta_TOSW, TOSW), io.s2_fire || (io.redirect_valid && io.redirect_isCall)) 389 val realWriteAddr = Mux(io.redirect_isCall, realWriteAddr_next, 390 Mux(io.s3_missed_push, s3_missPushAddr, 391 realWriteAddr_next)) 392 val realNos_next = RegEnable(Mux(io.redirect_valid && io.redirect_isCall, io.redirect_meta_TOSR, TOSR), io.s2_fire || (io.redirect_valid && io.redirect_isCall)) 393 val realNos = Mux(io.redirect_isCall, realNos_next, 394 Mux(io.s3_missed_push, s3_missPushNos, 395 realNos_next)) 396 397 realPush := (io.s3_fire && (!io.s3_cancel && RegEnable(io.spec_push_valid, io.s2_fire) || io.s3_missed_push)) || RegNext(io.redirect_valid && io.redirect_isCall) 398 399 when (realPush) { 400 spec_queue(realWriteAddr.value) := realWriteEntry 401 spec_nos(realWriteAddr.value) := realNos 402 } 403 404 def specPush(retAddr: UInt, currentSsp: UInt, currentSctr: UInt, currentTOSR: RASPtr, currentTOSW: RASPtr, topEntry: RASEntry) = { 405 TOSR := currentTOSW 406 TOSW := specPtrInc(currentTOSW) 407 // spec sp and ctr should always be maintained 408 when (topEntry.retAddr === retAddr && currentSctr < ctrMax) { 409 sctr := currentSctr + 1.U 410 } .otherwise { 411 ssp := ptrInc(currentSsp) 412 sctr := 0.U 413 } 414 // if we are draining the capacity of spec queue, force move BOS forward 415 when (specPtrInc(currentTOSW) === BOS) { 416 BOS := specPtrInc(BOS) 417 spec_overflowed := true.B; 418 } 419 } 420 421 when (io.spec_push_valid) { 422 specPush(io.spec_push_addr, ssp, sctr, TOSR, TOSW, topEntry) 423 } 424 def specPop(currentSsp: UInt, currentSctr: UInt, currentTOSR: RASPtr, currentTOSW: RASPtr, currentTopNos: RASPtr) = { 425 // TOSR is only maintained when spec queue is not empty 426 when (TOSRinRange(currentTOSR, currentTOSW)) { 427 TOSR := currentTopNos 428 } 429 // spec sp and ctr should always be maintained 430 when (currentSctr > 0.U) { 431 sctr := currentSctr - 1.U 432 } .elsewhen (TOSRinRange(currentTopNos, currentTOSW)) { 433 // in range, use inflight data 434 ssp := ptrDec(currentSsp) 435 sctr := spec_queue(currentTopNos.value).ctr 436 } .otherwise { 437 // NOS not in range, use commit data 438 ssp := ptrDec(currentSsp) 439 sctr := getCommitTop(ptrDec(currentSsp)).ctr 440 // in overflow state, we cannot determine the next sctr, sctr here is not accurate 441 } 442 } 443 when (io.spec_pop_valid) { 444 specPop(ssp, sctr, TOSR, TOSW, topNos) 445 } 446 447 // io.spec_pop_addr := Mux(writeBypassValid, writeBypassEntry.retAddr, topEntry.retAddr) 448 449 io.spec_pop_addr := timingTop.retAddr 450 io.BOS := BOS 451 io.TOSW := TOSW 452 io.TOSR := TOSR 453 io.NOS := topNos 454 io.ssp := ssp 455 io.sctr := sctr 456 io.nsp := nsp 457 458 when (io.s3_cancel) { 459 // recovery of all related pointers 460 TOSR := io.s3_meta.TOSR 461 TOSW := io.s3_meta.TOSW 462 ssp := io.s3_meta.ssp 463 sctr := io.s3_meta.sctr 464 465 // for missing pop, we also need to do a pop here 466 when (io.s3_missed_pop) { 467 specPop(io.s3_meta.ssp, io.s3_meta.sctr, io.s3_meta.TOSR, io.s3_meta.TOSW, io.s3_meta.NOS) 468 } 469 when (io.s3_missed_push) { 470 // do not use any bypass from f2 471 specPush(io.s3_pushAddr, io.s3_meta.ssp, io.s3_meta.sctr, io.s3_meta.TOSR, io.s3_meta.TOSW, s3TopEntry) 472 } 473 } 474 475 val commitTop = commit_stack(nsp) 476 477 when (io.commit_pop_valid) { 478 479 val nsp_update = Wire(UInt(log2Up(rasSize).W)) 480 when (io.commit_meta_ssp =/= nsp) { 481 // force set nsp to commit ssp to avoid permanent errors 482 nsp_update := io.commit_meta_ssp 483 } .otherwise { 484 nsp_update := nsp 485 } 486 487 // if ctr > 0, --ctr in stack, otherwise --nsp 488 when (commitTop.ctr > 0.U) { 489 commit_stack(nsp_update).ctr := commitTop.ctr - 1.U 490 nsp := nsp_update 491 } .otherwise { 492 nsp := ptrDec(nsp_update); 493 } 494 // XSError(io.commit_meta_ssp =/= nsp, "nsp mismatch with expected ssp") 495 } 496 497 val commit_push_addr = spec_queue(io.commit_meta_TOSW.value).retAddr 498 499 500 501 when (io.commit_push_valid) { 502 val nsp_update = Wire(UInt(log2Up(rasSize).W)) 503 when (io.commit_meta_ssp =/= nsp) { 504 // force set nsp to commit ssp to avoid permanent errors 505 nsp_update := io.commit_meta_ssp 506 } .otherwise { 507 nsp_update := nsp 508 } 509 // if ctr < max && topAddr == push addr, ++ctr, otherwise ++nsp 510 when (commitTop.ctr < ctrMax && commitTop.retAddr === commit_push_addr) { 511 commit_stack(nsp_update).ctr := commitTop.ctr + 1.U 512 nsp := nsp_update 513 } .otherwise { 514 nsp := ptrInc(nsp_update) 515 commit_stack(ptrInc(nsp_update)).retAddr := commit_push_addr 516 commit_stack(ptrInc(nsp_update)).ctr := 0.U 517 } 518 // when overflow, BOS may be forced move forward, do not revert those changes 519 when (!spec_overflowed || isAfter(specPtrInc(io.commit_meta_TOSW), BOS)) { 520 BOS := specPtrInc(io.commit_meta_TOSW) 521 spec_overflowed := false.B 522 } 523 524 // XSError(io.commit_meta_ssp =/= nsp, "nsp mismatch with expected ssp") 525 // XSError(io.commit_push_addr =/= commit_push_addr, "addr from commit mismatch with addr from spec") 526 } 527 528 when (io.redirect_valid) { 529 TOSR := io.redirect_meta_TOSR 530 TOSW := io.redirect_meta_TOSW 531 ssp := io.redirect_meta_ssp 532 sctr := io.redirect_meta_sctr 533 534 when (io.redirect_isCall) { 535 specPush(io.redirect_callAddr, io.redirect_meta_ssp, io.redirect_meta_sctr, io.redirect_meta_TOSR, io.redirect_meta_TOSW, redirectTopEntry) 536 } 537 when (io.redirect_isRet) { 538 specPop(io.redirect_meta_ssp, io.redirect_meta_sctr, io.redirect_meta_TOSR, io.redirect_meta_TOSW, redirectTopNos) 539 } 540 } 541 542 io.debug.commit_stack.zipWithIndex.foreach{case (a, i) => a := commit_stack(i)} 543 io.debug.spec_nos.zipWithIndex.foreach{case (a, i) => a := spec_nos(i)} 544 io.debug.spec_queue.zipWithIndex.foreach{ case (a, i) => a := spec_queue(i)} 545 } 546 547 val stack = Module(new RASStack(RasSize, RasSpecSize)).io 548 549 val s2_spec_push = WireInit(false.B) 550 val s2_spec_pop = WireInit(false.B) 551 val s2_full_pred = io.in.bits.resp_in(0).s2.full_pred(2) 552 // when last inst is an rvi call, fall through address would be set to the middle of it, so an addition is needed 553 val s2_spec_new_addr = s2_full_pred.fallThroughAddr + Mux(s2_full_pred.last_may_be_rvi_call, 2.U, 0.U) 554 stack.spec_push_valid := s2_spec_push 555 stack.spec_pop_valid := s2_spec_pop 556 stack.spec_push_addr := s2_spec_new_addr 557 558 // confirm that the call/ret is the taken cfi 559 s2_spec_push := io.s2_fire(2) && s2_full_pred.hit_taken_on_call && !io.s3_redirect(2) 560 s2_spec_pop := io.s2_fire(2) && s2_full_pred.hit_taken_on_ret && !io.s3_redirect(2) 561 562 //val s2_jalr_target = io.out.s2.full_pred.jalr_target 563 //val s2_last_target_in = s2_full_pred.targets.last 564 // val s2_last_target_out = io.out.s2.full_pred(2).targets.last 565 val s2_is_jalr = s2_full_pred.is_jalr 566 val s2_is_ret = s2_full_pred.is_ret 567 val s2_top = stack.spec_pop_addr 568 // assert(is_jalr && is_ret || !is_ret) 569 when(s2_is_ret && io.ctrl.ras_enable) { 570 io.out.s2.full_pred.map(_.jalr_target).foreach(_ := s2_top) 571 // FIXME: should use s1 globally 572 } 573 //s2_last_target_out := Mux(s2_is_jalr, s2_jalr_target, s2_last_target_in) 574 io.out.s2.full_pred.zipWithIndex.foreach{ case (a, i) => 575 a.targets.last := Mux(s2_is_jalr, io.out.s2.full_pred(i).jalr_target, io.in.bits.resp_in(0).s2.full_pred(i).targets.last) 576 } 577 578 val s2_meta = Wire(new RASInternalMeta) 579 s2_meta.ssp := stack.ssp 580 s2_meta.sctr := stack.sctr 581 s2_meta.TOSR := stack.TOSR 582 s2_meta.TOSW := stack.TOSW 583 s2_meta.NOS := stack.NOS 584 585 val s3_top = RegEnable(stack.spec_pop_addr, io.s2_fire(2)) 586 val s3_spec_new_addr = RegEnable(s2_spec_new_addr, io.s2_fire(2)) 587 588 // val s3_jalr_target = io.out.s3.full_pred.jalr_target 589 // val s3_last_target_in = io.in.bits.resp_in(0).s3.full_pred(2).targets.last 590 // val s3_last_target_out = io.out.s3.full_pred(2).targets.last 591 val s3_is_jalr = io.in.bits.resp_in(0).s3.full_pred(2).is_jalr 592 val s3_is_ret = io.in.bits.resp_in(0).s3.full_pred(2).is_ret 593 // assert(is_jalr && is_ret || !is_ret) 594 when(s3_is_ret && io.ctrl.ras_enable) { 595 io.out.s3.full_pred.map(_.jalr_target).foreach(_ := s3_top) 596 // FIXME: should use s1 globally 597 } 598 // s3_last_target_out := Mux(s3_is_jalr, s3_jalr_target, s3_last_target_in) 599 io.out.s3.full_pred.zipWithIndex.foreach{ case (a, i) => 600 a.targets.last := Mux(s3_is_jalr, io.out.s3.full_pred(i).jalr_target, io.in.bits.resp_in(0).s3.full_pred(i).targets.last) 601 } 602 603 val s3_pushed_in_s2 = RegEnable(s2_spec_push, io.s2_fire(2)) 604 val s3_popped_in_s2 = RegEnable(s2_spec_pop, io.s2_fire(2)) 605 val s3_push = io.in.bits.resp_in(0).s3.full_pred(2).hit_taken_on_call 606 val s3_pop = io.in.bits.resp_in(0).s3.full_pred(2).hit_taken_on_ret 607 608 val s3_cancel = io.s3_fire(2) && (s3_pushed_in_s2 =/= s3_push || s3_popped_in_s2 =/= s3_pop) 609 stack.s2_fire := io.s2_fire(2) 610 stack.s3_fire := io.s3_fire(2) 611 612 stack.s3_cancel := s3_cancel 613 614 val s3_meta = RegEnable(s2_meta, io.s2_fire(2)) 615 616 stack.s3_meta := s3_meta 617 stack.s3_missed_pop := s3_pop && !s3_popped_in_s2 618 stack.s3_missed_push := s3_push && !s3_pushed_in_s2 619 stack.s3_pushAddr := s3_spec_new_addr 620 621 // no longer need the top Entry, but TOSR, TOSW, ssp sctr 622 // TODO: remove related signals 623 624 val last_stage_meta = Wire(new RASMeta) 625 last_stage_meta.ssp := s3_meta.ssp 626 last_stage_meta.TOSW := s3_meta.TOSW 627 628 io.out.last_stage_spec_info.sctr := s3_meta.sctr 629 io.out.last_stage_spec_info.ssp := s3_meta.ssp 630 io.out.last_stage_spec_info.TOSW := s3_meta.TOSW 631 io.out.last_stage_spec_info.TOSR := s3_meta.TOSR 632 io.out.last_stage_spec_info.NOS := s3_meta.NOS 633 io.out.last_stage_spec_info.topAddr := s3_top 634 io.out.last_stage_meta := last_stage_meta.asUInt 635 636 637 val redirect = RegNextWithEnable(io.redirect) 638 val do_recover = redirect.valid 639 val recover_cfi = redirect.bits.cfiUpdate 640 641 val retMissPred = do_recover && redirect.bits.level === 0.U && recover_cfi.pd.isRet 642 val callMissPred = do_recover && redirect.bits.level === 0.U && recover_cfi.pd.isCall 643 // when we mispredict a call, we must redo a push operation 644 // similarly, when we mispredict a return, we should redo a pop 645 stack.redirect_valid := do_recover 646 stack.redirect_isCall := callMissPred 647 stack.redirect_isRet := retMissPred 648 stack.redirect_meta_ssp := recover_cfi.ssp 649 stack.redirect_meta_sctr := recover_cfi.sctr 650 stack.redirect_meta_TOSW := recover_cfi.TOSW 651 stack.redirect_meta_TOSR := recover_cfi.TOSR 652 stack.redirect_meta_NOS := recover_cfi.NOS 653 stack.redirect_callAddr := recover_cfi.pc + Mux(recover_cfi.pd.isRVC, 2.U, 4.U) 654 655 val update = io.update.bits 656 val updateMeta = io.update.bits.meta.asTypeOf(new RASMeta) 657 val updateValid = io.update.valid 658 659 stack.commit_push_valid := updateValid && update.is_call_taken 660 stack.commit_pop_valid := updateValid && update.is_ret_taken 661 stack.commit_push_addr := update.ftb_entry.getFallThrough(update.pc) + Mux(update.ftb_entry.last_may_be_rvi_call, 2.U, 0.U) 662 stack.commit_meta_TOSW := updateMeta.TOSW 663 stack.commit_meta_ssp := updateMeta.ssp 664 665 666 XSPerfAccumulate("ras_s3_cancel", s3_cancel) 667 XSPerfAccumulate("ras_redirect_recover", redirect.valid) 668 XSPerfAccumulate("ras_s3_and_redirect_recover_at_the_same_time", s3_cancel && redirect.valid) 669 670 671 val spec_debug = stack.debug 672 XSDebug(io.s2_fire(2), "----------------RAS----------------\n") 673 XSDebug(io.s2_fire(2), " TopRegister: 0x%x\n",stack.spec_pop_addr) 674 XSDebug(io.s2_fire(2), " index addr ctr nos (spec part)\n") 675 for(i <- 0 until RasSpecSize){ 676 XSDebug(io.s2_fire(2), " (%d) 0x%x %d %d",i.U,spec_debug.spec_queue(i).retAddr,spec_debug.spec_queue(i).ctr, spec_debug.spec_nos(i).value) 677 when(i.U === stack.TOSW.value){XSDebug(io.s2_fire(2), " <----TOSW")} 678 when(i.U === stack.TOSR.value){XSDebug(io.s2_fire(2), " <----TOSR")} 679 when(i.U === stack.BOS.value){XSDebug(io.s2_fire(2), " <----BOS")} 680 XSDebug(io.s2_fire(2), "\n") 681 } 682 XSDebug(io.s2_fire(2), " index addr ctr (committed part)\n") 683 for(i <- 0 until RasSize){ 684 XSDebug(io.s2_fire(2), " (%d) 0x%x %d",i.U,spec_debug.commit_stack(i).retAddr,spec_debug.commit_stack(i).ctr) 685 when(i.U === stack.ssp){XSDebug(io.s2_fire(2), " <----ssp")} 686 when(i.U === stack.nsp){XSDebug(io.s2_fire(2), " <----nsp")} 687 XSDebug(io.s2_fire(2), "\n") 688 } 689 /* 690 XSDebug(s2_spec_push, "s2_spec_push inAddr: 0x%x inCtr: %d | allocNewEntry:%d | sp:%d \n", 691 s2_spec_new_addr,spec_debug.spec_push_entry.ctr,spec_debug.spec_alloc_new,spec_debug.sp.asUInt) 692 XSDebug(s2_spec_pop, "s2_spec_pop outAddr: 0x%x \n",io.out.s2.getTarget) 693 val s3_recover_entry = spec_debug.recover_push_entry 694 XSDebug(s3_recover && s3_push, "s3_recover_push inAddr: 0x%x inCtr: %d | allocNewEntry:%d | sp:%d \n", 695 s3_recover_entry.retAddr, s3_recover_entry.ctr, spec_debug.recover_alloc_new, s3_sp.asUInt) 696 XSDebug(s3_recover && s3_pop, "s3_recover_pop outAddr: 0x%x \n",io.out.s3.getTarget) 697 val redirectUpdate = redirect.bits.cfiUpdate 698 XSDebug(do_recover && callMissPred, "redirect_recover_push\n") 699 XSDebug(do_recover && retMissPred, "redirect_recover_pop\n") 700 XSDebug(do_recover, "redirect_recover(SP:%d retAddr:%x ctr:%d) \n", 701 redirectUpdate.rasSp,redirectUpdate.rasEntry.retAddr,redirectUpdate.rasEntry.ctr) 702 */ 703 704 generatePerfEvent() 705} 706