109c6f1ddSLingrui98/*************************************************************************************** 209c6f1ddSLingrui98* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 309c6f1ddSLingrui98* Copyright (c) 2020-2021 Peng Cheng Laboratory 409c6f1ddSLingrui98* 509c6f1ddSLingrui98* XiangShan is licensed under Mulan PSL v2. 609c6f1ddSLingrui98* You can use this software according to the terms and conditions of the Mulan PSL v2. 709c6f1ddSLingrui98* You may obtain a copy of Mulan PSL v2 at: 809c6f1ddSLingrui98* http://license.coscl.org.cn/MulanPSL2 909c6f1ddSLingrui98* 1009c6f1ddSLingrui98* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 1109c6f1ddSLingrui98* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 1209c6f1ddSLingrui98* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 1309c6f1ddSLingrui98* 1409c6f1ddSLingrui98* See the Mulan PSL v2 for more details. 1509c6f1ddSLingrui98***************************************************************************************/ 1609c6f1ddSLingrui98 1709c6f1ddSLingrui98package xiangshan.frontend 1809c6f1ddSLingrui98 1909c6f1ddSLingrui98import chipsalliance.rocketchip.config.Parameters 2009c6f1ddSLingrui98import chisel3._ 2109c6f1ddSLingrui98import chisel3.experimental.chiselName 2209c6f1ddSLingrui98import chisel3.util._ 2309c6f1ddSLingrui98import utils._ 2409c6f1ddSLingrui98import xiangshan._ 2509c6f1ddSLingrui98 2609c6f1ddSLingrui98class RASEntry()(implicit p: Parameters) extends XSBundle { 2709c6f1ddSLingrui98 val retAddr = UInt(VAddrBits.W) 2809c6f1ddSLingrui98 val ctr = UInt(8.W) // layer of nested call functions 2909c6f1ddSLingrui98} 3009c6f1ddSLingrui98 3109c6f1ddSLingrui98@chiselName 3209c6f1ddSLingrui98class RAS(implicit p: Parameters) extends BasePredictor { 3309c6f1ddSLingrui98 object RASEntry { 3409c6f1ddSLingrui98 def apply(retAddr: UInt, ctr: UInt): RASEntry = { 3509c6f1ddSLingrui98 val e = Wire(new RASEntry) 3609c6f1ddSLingrui98 e.retAddr := retAddr 3709c6f1ddSLingrui98 e.ctr := ctr 3809c6f1ddSLingrui98 e 3909c6f1ddSLingrui98 } 4009c6f1ddSLingrui98 } 4109c6f1ddSLingrui98 4209c6f1ddSLingrui98 @chiselName 4309c6f1ddSLingrui98 class RASStack(val rasSize: Int) extends XSModule { 4409c6f1ddSLingrui98 val io = IO(new Bundle { 4509c6f1ddSLingrui98 val push_valid = Input(Bool()) 4609c6f1ddSLingrui98 val pop_valid = Input(Bool()) 4709c6f1ddSLingrui98 val spec_new_addr = Input(UInt(VAddrBits.W)) 4809c6f1ddSLingrui98 4909c6f1ddSLingrui98 val recover_sp = Input(UInt(log2Up(rasSize).W)) 5009c6f1ddSLingrui98 val recover_top = Input(new RASEntry) 5109c6f1ddSLingrui98 val recover_valid = Input(Bool()) 5209c6f1ddSLingrui98 val recover_push = Input(Bool()) 5309c6f1ddSLingrui98 val recover_pop = Input(Bool()) 5409c6f1ddSLingrui98 val recover_new_addr = Input(UInt(VAddrBits.W)) 5509c6f1ddSLingrui98 5609c6f1ddSLingrui98 val sp = Output(UInt(log2Up(rasSize).W)) 5709c6f1ddSLingrui98 val top = Output(new RASEntry) 5809c6f1ddSLingrui98 }) 5909c6f1ddSLingrui98 6009c6f1ddSLingrui98 val debugIO = IO(new Bundle{ 617e8709feSLingrui98 val spec_push_entry = Output(new RASEntry) 627e8709feSLingrui98 val spec_alloc_new = Output(Bool()) 637e8709feSLingrui98 val recover_push_entry = Output(new RASEntry) 647e8709feSLingrui98 val recover_alloc_new = Output(Bool()) 6509c6f1ddSLingrui98 val sp = Output(UInt(log2Up(rasSize).W)) 6609c6f1ddSLingrui98 val topRegister = Output(new RASEntry) 6709c6f1ddSLingrui98 val out_mem = Output(Vec(RasSize, new RASEntry)) 6809c6f1ddSLingrui98 }) 6909c6f1ddSLingrui98 7009c6f1ddSLingrui98 val stack = Mem(RasSize, new RASEntry) 7109c6f1ddSLingrui98 val sp = RegInit(0.U(log2Up(rasSize).W)) 727e8709feSLingrui98 val top = RegInit(RASEntry(0x80000000L.U, 0.U)) 7309c6f1ddSLingrui98 val topPtr = RegInit(0.U(log2Up(rasSize).W)) 7409c6f1ddSLingrui98 7509c6f1ddSLingrui98 def ptrInc(ptr: UInt) = Mux(ptr === (rasSize-1).U, 0.U, ptr + 1.U) 7609c6f1ddSLingrui98 def ptrDec(ptr: UInt) = Mux(ptr === 0.U, (rasSize-1).U, ptr - 1.U) 7709c6f1ddSLingrui98 787e8709feSLingrui98 val spec_alloc_new = io.spec_new_addr =/= top.retAddr || top.ctr.andR 7909c6f1ddSLingrui98 val recover_alloc_new = io.recover_new_addr =/= io.recover_top.retAddr || io.recover_top.ctr.andR 8009c6f1ddSLingrui98 8109c6f1ddSLingrui98 // TODO: fix overflow and underflow bugs 8209c6f1ddSLingrui98 def update(recover: Bool)(do_push: Bool, do_pop: Bool, do_alloc_new: Bool, 8309c6f1ddSLingrui98 do_sp: UInt, do_top_ptr: UInt, do_new_addr: UInt, 8409c6f1ddSLingrui98 do_top: RASEntry) = { 8509c6f1ddSLingrui98 when (do_push) { 8609c6f1ddSLingrui98 when (do_alloc_new) { 8709c6f1ddSLingrui98 sp := ptrInc(do_sp) 8809c6f1ddSLingrui98 topPtr := do_sp 8909c6f1ddSLingrui98 top.retAddr := do_new_addr 907e8709feSLingrui98 top.ctr := 0.U 917e8709feSLingrui98 stack.write(do_sp, RASEntry(do_new_addr, 0.U)) 9209c6f1ddSLingrui98 }.otherwise { 9309c6f1ddSLingrui98 when (recover) { 9409c6f1ddSLingrui98 sp := do_sp 9509c6f1ddSLingrui98 topPtr := do_top_ptr 9609c6f1ddSLingrui98 top.retAddr := do_top.retAddr 9709c6f1ddSLingrui98 } 9809c6f1ddSLingrui98 top.ctr := do_top.ctr + 1.U 9909c6f1ddSLingrui98 stack.write(do_top_ptr, RASEntry(do_new_addr, do_top.ctr + 1.U)) 10009c6f1ddSLingrui98 } 10109c6f1ddSLingrui98 }.elsewhen (do_pop) { 1027e8709feSLingrui98 when (do_top.ctr === 0.U) { 10309c6f1ddSLingrui98 sp := ptrDec(do_sp) 10409c6f1ddSLingrui98 topPtr := ptrDec(do_top_ptr) 10509c6f1ddSLingrui98 top := stack.read(ptrDec(do_top_ptr)) 10609c6f1ddSLingrui98 }.otherwise { 10709c6f1ddSLingrui98 when (recover) { 10809c6f1ddSLingrui98 sp := do_sp 10909c6f1ddSLingrui98 topPtr := do_top_ptr 11009c6f1ddSLingrui98 top.retAddr := do_top.retAddr 11109c6f1ddSLingrui98 } 11209c6f1ddSLingrui98 top.ctr := do_top.ctr - 1.U 11309c6f1ddSLingrui98 stack.write(do_top_ptr, RASEntry(do_top.retAddr, do_top.ctr - 1.U)) 11409c6f1ddSLingrui98 } 11509c6f1ddSLingrui98 }.otherwise { 11609c6f1ddSLingrui98 when (recover) { 11709c6f1ddSLingrui98 sp := do_sp 11809c6f1ddSLingrui98 topPtr := do_top_ptr 11909c6f1ddSLingrui98 top := do_top 12009c6f1ddSLingrui98 stack.write(do_top_ptr, do_top) 12109c6f1ddSLingrui98 } 12209c6f1ddSLingrui98 } 12309c6f1ddSLingrui98 } 12409c6f1ddSLingrui98 125d717fd1eSLingrui98 12609c6f1ddSLingrui98 update(io.recover_valid)( 12709c6f1ddSLingrui98 Mux(io.recover_valid, io.recover_push, io.push_valid), 12809c6f1ddSLingrui98 Mux(io.recover_valid, io.recover_pop, io.pop_valid), 1297e8709feSLingrui98 Mux(io.recover_valid, recover_alloc_new, spec_alloc_new), 13009c6f1ddSLingrui98 Mux(io.recover_valid, io.recover_sp, sp), 13109c6f1ddSLingrui98 Mux(io.recover_valid, io.recover_sp - 1.U, topPtr), 13209c6f1ddSLingrui98 Mux(io.recover_valid, io.recover_new_addr, io.spec_new_addr), 13309c6f1ddSLingrui98 Mux(io.recover_valid, io.recover_top, top)) 13409c6f1ddSLingrui98 13509c6f1ddSLingrui98 io.sp := sp 13609c6f1ddSLingrui98 io.top := top 13709c6f1ddSLingrui98 138d717fd1eSLingrui98 val resetIdx = RegInit(0.U(log2Ceil(RasSize).W)) 139d717fd1eSLingrui98 val do_reset = RegInit(true.B) 140d717fd1eSLingrui98 when (do_reset) { 141d717fd1eSLingrui98 stack.write(resetIdx, RASEntry(0x80000000L.U, 0.U)) 142d717fd1eSLingrui98 } 143d717fd1eSLingrui98 resetIdx := resetIdx + do_reset 144d717fd1eSLingrui98 when (resetIdx === (RasSize-1).U) { 145d717fd1eSLingrui98 do_reset := false.B 146d717fd1eSLingrui98 } 147d717fd1eSLingrui98 1487e8709feSLingrui98 debugIO.spec_push_entry := RASEntry(io.spec_new_addr, Mux(spec_alloc_new, 1.U, top.ctr + 1.U)) 1497e8709feSLingrui98 debugIO.spec_alloc_new := spec_alloc_new 1507e8709feSLingrui98 debugIO.recover_push_entry := RASEntry(io.recover_new_addr, Mux(recover_alloc_new, 1.U, io.recover_top.ctr + 1.U)) 1517e8709feSLingrui98 debugIO.recover_alloc_new := recover_alloc_new 15209c6f1ddSLingrui98 debugIO.sp := sp 15309c6f1ddSLingrui98 debugIO.topRegister := top 15409c6f1ddSLingrui98 for (i <- 0 until RasSize) { 15509c6f1ddSLingrui98 debugIO.out_mem(i) := stack.read(i.U) 15609c6f1ddSLingrui98 } 15709c6f1ddSLingrui98 } 15809c6f1ddSLingrui98 15909c6f1ddSLingrui98 val spec = Module(new RASStack(RasSize)) 16009c6f1ddSLingrui98 val spec_ras = spec.io 161cb4f77ceSLingrui98 val spec_top_addr = spec_ras.top.retAddr 16209c6f1ddSLingrui98 16309c6f1ddSLingrui98 164cb4f77ceSLingrui98 val s2_spec_push = WireInit(false.B) 165cb4f77ceSLingrui98 val s2_spec_pop = WireInit(false.B) 166f4ebc4b2SLingrui98 val s2_full_pred = io.in.bits.resp_in(0).s2.full_pred 167f4ebc4b2SLingrui98 // when last inst is an rvi call, fall through address would be set to the middle of it, so an addition is needed 168f4ebc4b2SLingrui98 val s2_spec_new_addr = s2_full_pred.fallThroughAddr + Mux(s2_full_pred.last_may_be_rvi_call, 2.U, 0.U) 169cb4f77ceSLingrui98 spec_ras.push_valid := s2_spec_push 170cb4f77ceSLingrui98 spec_ras.pop_valid := s2_spec_pop 171cb4f77ceSLingrui98 spec_ras.spec_new_addr := s2_spec_new_addr 17209c6f1ddSLingrui98 17309c6f1ddSLingrui98 // confirm that the call/ret is the taken cfi 174f4ebc4b2SLingrui98 s2_spec_push := io.s2_fire && s2_full_pred.hit_taken_on_call && !io.s3_redirect 175f4ebc4b2SLingrui98 s2_spec_pop := io.s2_fire && s2_full_pred.hit_taken_on_ret && !io.s3_redirect 17609c6f1ddSLingrui98 177cb4f77ceSLingrui98 val s2_jalr_target = io.out.resp.s2.full_pred.jalr_target 178f4ebc4b2SLingrui98 val s2_last_target_in = s2_full_pred.targets.last 179cb4f77ceSLingrui98 val s2_last_target_out = io.out.resp.s2.full_pred.targets.last 180f4ebc4b2SLingrui98 val s2_is_jalr = s2_full_pred.is_jalr 181f4ebc4b2SLingrui98 val s2_is_ret = s2_full_pred.is_ret 182b30c10d6SLingrui98 // assert(is_jalr && is_ret || !is_ret) 183*6ee06c7aSSteve Gou when(s2_is_ret && io.ctrl.ras_enable) { 184cb4f77ceSLingrui98 s2_jalr_target := spec_top_addr 185b30c10d6SLingrui98 // FIXME: should use s1 globally 18609c6f1ddSLingrui98 } 187cb4f77ceSLingrui98 s2_last_target_out := Mux(s2_is_jalr, s2_jalr_target, s2_last_target_in) 18809c6f1ddSLingrui98 189cb4f77ceSLingrui98 val s3_top = RegEnable(spec_ras.top, io.s2_fire) 190cb4f77ceSLingrui98 val s3_sp = RegEnable(spec_ras.sp, io.s2_fire) 191cb4f77ceSLingrui98 val s3_spec_new_addr = RegEnable(s2_spec_new_addr, io.s2_fire) 192cb4f77ceSLingrui98 193cb4f77ceSLingrui98 val s3_jalr_target = io.out.resp.s3.full_pred.jalr_target 194cb4f77ceSLingrui98 val s3_last_target_in = io.in.bits.resp_in(0).s3.full_pred.targets.last 195cb4f77ceSLingrui98 val s3_last_target_out = io.out.resp.s3.full_pred.targets.last 196cb4f77ceSLingrui98 val s3_is_jalr = io.in.bits.resp_in(0).s3.full_pred.is_jalr 197cb4f77ceSLingrui98 val s3_is_ret = io.in.bits.resp_in(0).s3.full_pred.is_ret 198cb4f77ceSLingrui98 // assert(is_jalr && is_ret || !is_ret) 199*6ee06c7aSSteve Gou when(s3_is_ret && io.ctrl.ras_enable) { 200cb4f77ceSLingrui98 s3_jalr_target := s3_top.retAddr 201cb4f77ceSLingrui98 // FIXME: should use s1 globally 202cb4f77ceSLingrui98 } 203cb4f77ceSLingrui98 s3_last_target_out := Mux(s3_is_jalr, s3_jalr_target, s3_last_target_in) 204cb4f77ceSLingrui98 205cb4f77ceSLingrui98 val s3_pushed_in_s2 = RegEnable(s2_spec_push, io.s2_fire) 206cb4f77ceSLingrui98 val s3_popped_in_s2 = RegEnable(s2_spec_pop, io.s2_fire) 2075df98e43SLingrui98 val s3_push = io.in.bits.resp_in(0).s3.full_pred.hit_taken_on_call 2085df98e43SLingrui98 val s3_pop = io.in.bits.resp_in(0).s3.full_pred.hit_taken_on_ret 209cb4f77ceSLingrui98 2105df98e43SLingrui98 val s3_recover = io.s3_fire && (s3_pushed_in_s2 =/= s3_push || s3_popped_in_s2 =/= s3_pop) 2115df98e43SLingrui98 io.out.resp.s3.rasSp := s3_sp 2125df98e43SLingrui98 io.out.resp.s3.rasTop := s3_top 21309c6f1ddSLingrui98 21409c6f1ddSLingrui98 21509c6f1ddSLingrui98 val redirect = RegNext(io.redirect) 216cb4f77ceSLingrui98 val do_recover = redirect.valid || s3_recover 21709c6f1ddSLingrui98 val recover_cfi = redirect.bits.cfiUpdate 21809c6f1ddSLingrui98 21909c6f1ddSLingrui98 val retMissPred = do_recover && redirect.bits.level === 0.U && recover_cfi.pd.isRet 22009c6f1ddSLingrui98 val callMissPred = do_recover && redirect.bits.level === 0.U && recover_cfi.pd.isCall 22109c6f1ddSLingrui98 // when we mispredict a call, we must redo a push operation 22209c6f1ddSLingrui98 // similarly, when we mispredict a return, we should redo a pop 22309c6f1ddSLingrui98 spec_ras.recover_valid := do_recover 2245df98e43SLingrui98 spec_ras.recover_push := Mux(redirect.valid, callMissPred, s3_push) 2255df98e43SLingrui98 spec_ras.recover_pop := Mux(redirect.valid, retMissPred, s3_pop) 22609c6f1ddSLingrui98 227cb4f77ceSLingrui98 spec_ras.recover_sp := Mux(redirect.valid, recover_cfi.rasSp, s3_sp) 228cb4f77ceSLingrui98 spec_ras.recover_top := Mux(redirect.valid, recover_cfi.rasEntry, s3_top) 229cb4f77ceSLingrui98 spec_ras.recover_new_addr := Mux(redirect.valid, recover_cfi.pc + Mux(recover_cfi.pd.isRVC, 2.U, 4.U), s3_spec_new_addr) 23009c6f1ddSLingrui98 231cb4f77ceSLingrui98 232cb4f77ceSLingrui98 XSPerfAccumulate("ras_s3_recover", s3_recover) 233cb4f77ceSLingrui98 XSPerfAccumulate("ras_redirect_recover", redirect.valid) 234cb4f77ceSLingrui98 XSPerfAccumulate("ras_s3_and_redirect_recover_at_the_same_time", s3_recover && redirect.valid) 23509c6f1ddSLingrui98 // TODO: back-up stack for ras 23609c6f1ddSLingrui98 // use checkpoint to recover RAS 23709c6f1ddSLingrui98 23809c6f1ddSLingrui98 val spec_debug = spec.debugIO 23909c6f1ddSLingrui98 XSDebug("----------------RAS----------------\n") 24009c6f1ddSLingrui98 XSDebug(" TopRegister: 0x%x %d \n",spec_debug.topRegister.retAddr,spec_debug.topRegister.ctr) 24109c6f1ddSLingrui98 XSDebug(" index addr ctr \n") 24209c6f1ddSLingrui98 for(i <- 0 until RasSize){ 24309c6f1ddSLingrui98 XSDebug(" (%d) 0x%x %d",i.U,spec_debug.out_mem(i).retAddr,spec_debug.out_mem(i).ctr) 24409c6f1ddSLingrui98 when(i.U === spec_debug.sp){XSDebug(false,true.B," <----sp")} 24509c6f1ddSLingrui98 XSDebug(false,true.B,"\n") 24609c6f1ddSLingrui98 } 2477e8709feSLingrui98 XSDebug(s2_spec_push, "s2_spec_push inAddr: 0x%x inCtr: %d | allocNewEntry:%d | sp:%d \n", 2487e8709feSLingrui98 s2_spec_new_addr,spec_debug.spec_push_entry.ctr,spec_debug.spec_alloc_new,spec_debug.sp.asUInt) 2497e8709feSLingrui98 XSDebug(s2_spec_pop, "s2_spec_pop outAddr: 0x%x \n",io.out.resp.s2.getTarget) 2507e8709feSLingrui98 val s3_recover_entry = spec_debug.recover_push_entry 2517e8709feSLingrui98 XSDebug(s3_recover && s3_push, "s3_recover_push inAddr: 0x%x inCtr: %d | allocNewEntry:%d | sp:%d \n", 2527e8709feSLingrui98 s3_recover_entry.retAddr, s3_recover_entry.ctr, spec_debug.recover_alloc_new, s3_sp.asUInt) 2537e8709feSLingrui98 XSDebug(s3_recover && s3_pop, "s3_recover_pop outAddr: 0x%x \n",io.out.resp.s3.getTarget) 25409c6f1ddSLingrui98 val redirectUpdate = redirect.bits.cfiUpdate 2557e8709feSLingrui98 XSDebug(do_recover && callMissPred, "redirect_recover_push\n") 2567e8709feSLingrui98 XSDebug(do_recover && retMissPred, "redirect_recover_pop\n") 2577e8709feSLingrui98 XSDebug(do_recover, "redirect_recover(SP:%d retAddr:%x ctr:%d) \n", 2587e8709feSLingrui98 redirectUpdate.rasSp,redirectUpdate.rasEntry.retAddr,redirectUpdate.rasEntry.ctr) 2594813e060SLingrui98 2604813e060SLingrui98 generatePerfEvent() 26109c6f1ddSLingrui98} 262