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