xref: /XiangShan/src/main/scala/xiangshan/mem/lsqueue/StoreMisalignBuffer.scala (revision d78a17c1d883132bf47d00d463dc9817c6a2dd0b)
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***************************************************************************************/
16
17package xiangshan.mem
18
19import org.chipsalliance.cde.config.Parameters
20import chisel3._
21import chisel3.util._
22import utils._
23import utility._
24import xiangshan._
25import xiangshan.ExceptionNO._
26import xiangshan.frontend.FtqPtr
27import xiangshan.backend.fu.FuConfig._
28import xiangshan.backend.fu.fpu.FPU
29import xiangshan.backend.rob.RobLsqIO
30import xiangshan.backend.rob.RobPtr
31import xiangshan.backend.Bundles._
32import xiangshan.backend.fu.FuConfig.StaCfg
33import xiangshan.backend.fu.FuType.isVStore
34import xiangshan.mem.Bundles._
35import xiangshan.cache._
36import xiangshan.cache.wpu.ReplayCarry
37
38class StoreMisalignBuffer(implicit p: Parameters) extends XSModule
39  with HasCircularQueuePtrHelper
40{
41  private val enqPortNum = StorePipelineWidth
42  private val maxSplitNum = 2
43
44  require(maxSplitNum == 2)
45
46  private val SB = "b00".U(2.W)
47  private val SH = "b01".U(2.W)
48  private val SW = "b10".U(2.W)
49  private val SD = "b11".U(2.W)
50
51  // encode of how many bytes to shift or truncate
52  private val BYTE0 = "b000".U(3.W)
53  private val BYTE1 = "b001".U(3.W)
54  private val BYTE2 = "b010".U(3.W)
55  private val BYTE3 = "b011".U(3.W)
56  private val BYTE4 = "b100".U(3.W)
57  private val BYTE5 = "b101".U(3.W)
58  private val BYTE6 = "b110".U(3.W)
59  private val BYTE7 = "b111".U(3.W)
60
61  def getMask(sizeEncode: UInt) = LookupTree(sizeEncode, List(
62    SB -> 0x1.U,
63    SH -> 0x3.U,
64    SW -> 0xf.U,
65    SD -> 0xff.U
66  ))
67
68  def selectOldest[T <: LsPipelineBundle](valid: Seq[Bool], bits: Seq[T], index: Seq[UInt]): (Seq[Bool], Seq[T], Seq[UInt]) = {
69    assert(valid.length == bits.length)
70    if (valid.length == 0 || valid.length == 1) {
71      (valid, bits, index)
72    } else if (valid.length == 2) {
73      val res = Seq.fill(2)(Wire(ValidIO(chiselTypeOf(bits(0)))))
74      val resIndex = Seq.fill(2)(Wire(chiselTypeOf(index(0))))
75      for (i <- res.indices) {
76        res(i).valid := valid(i)
77        res(i).bits := bits(i)
78        resIndex(i) := index(i)
79      }
80      val oldest = Mux(valid(0) && valid(1),
81        Mux(isAfter(bits(0).uop.robIdx, bits(1).uop.robIdx) ||
82          (isNotBefore(bits(0).uop.robIdx, bits(1).uop.robIdx) && bits(0).uop.uopIdx > bits(1).uop.uopIdx), res(1), res(0)),
83        Mux(valid(0) && !valid(1), res(0), res(1)))
84
85      val oldestIndex = Mux(valid(0) && valid(1),
86        Mux(isAfter(bits(0).uop.robIdx, bits(1).uop.robIdx) ||
87          (bits(0).uop.robIdx === bits(1).uop.robIdx && bits(0).uop.uopIdx > bits(1).uop.uopIdx), resIndex(1), resIndex(0)),
88        Mux(valid(0) && !valid(1), resIndex(0), resIndex(1)))
89      (Seq(oldest.valid), Seq(oldest.bits), Seq(oldestIndex))
90    } else {
91      val left = selectOldest(valid.take(valid.length / 2), bits.take(bits.length / 2), index.take(index.length / 2))
92      val right = selectOldest(valid.takeRight(valid.length - (valid.length / 2)), bits.takeRight(bits.length - (bits.length / 2)), index.takeRight(index.length - (index.length / 2)))
93      selectOldest(left._1 ++ right._1, left._2 ++ right._2, left._3 ++ right._3)
94    }
95  }
96
97  val io = IO(new Bundle() {
98    val redirect        = Flipped(Valid(new Redirect))
99    val req             = Vec(enqPortNum, Flipped(Decoupled(new LsPipelineBundle)))
100    val rob             = Flipped(new RobLsqIO)
101    val splitStoreReq   = Decoupled(new LsPipelineBundle)
102    val splitStoreResp  = Flipped(Valid(new SqWriteBundle))
103    val writeBack       = Decoupled(new MemExuOutput)
104    val vecWriteBack    = Vec(VecStorePipelineWidth, Decoupled(new VecPipelineFeedbackIO(isVStore = true)))
105    val storeOutValid    = Input(Bool())
106    val storeVecOutValid = Input(Bool())
107    val overwriteExpBuf = Output(new XSBundle {
108      val valid = Bool()
109      val vaddr = UInt(XLEN.W)
110      val isHyper = Bool()
111      val gpaddr = UInt(XLEN.W)
112      val isForVSnonLeafPTE = Bool()
113    })
114    val sqControl       = new StoreMaBufToSqControlIO
115
116    val toVecStoreMergeBuffer = Vec(VecStorePipelineWidth, new StoreMaBufToVecStoreMergeBufferIO)
117    val full = Bool()
118  })
119
120  io.rob.mmio := 0.U.asTypeOf(Vec(LoadPipelineWidth, Bool()))
121  io.rob.uop  := 0.U.asTypeOf(Vec(LoadPipelineWidth, new DynInst))
122
123  class StoreMisalignBufferEntry(implicit p: Parameters) extends LsPipelineBundle {
124    val portIndex = UInt(log2Up(enqPortNum).W)
125  }
126  val req_valid = RegInit(false.B)
127  val req = Reg(new StoreMisalignBufferEntry)
128
129  val cross4KBPageBoundary = Wire(Bool())
130  val needFlushPipe = RegInit(false.B)
131
132  // buffer control:
133  //  - s_idle:  Idle
134  //  - s_split: Split miss-aligned store into aligned stores
135  //  - s_req:   Send split store to sta and get result from sta
136  //  - s_resp:  Responds to a split store access request
137  //  - s_wb:    writeback yo rob/vecMergeBuffer
138  //  - s_block: Wait for this instr to reach the head of Rob.
139  val s_idle :: s_split :: s_req :: s_resp :: s_wb :: s_block :: Nil = Enum(6)
140  val bufferState    = RegInit(s_idle)
141
142  // enqueue
143  // s1:
144  val s1_req = VecInit(io.req.map(_.bits))
145  val s1_valid = VecInit(io.req.map(x => x.valid))
146
147  val s1_index = (0 until io.req.length).map(_.asUInt)
148  val reqSel = selectOldest(s1_valid, s1_req, s1_index)
149
150  val reqSelValid = reqSel._1(0)
151  val reqSelBits  = reqSel._2(0)
152  val reqSelPort  = reqSel._3(0)
153
154  val reqRedirect = reqSelBits.uop.robIdx.needFlush(io.redirect)
155
156  val canEnq = !req_valid && !reqRedirect && reqSelValid
157  val robMatch = req_valid && io.rob.pendingst && (io.rob.pendingPtr === req.uop.robIdx)
158
159  when(canEnq) {
160    connectSamePort(req, reqSelBits)
161    req.portIndex := reqSelPort
162    req_valid := true.B
163  }
164  val cross4KBPageEnq = WireInit(false.B)
165  when (cross4KBPageBoundary && !reqRedirect) {
166    when(
167      reqSelValid &&
168      (isAfter(req.uop.robIdx, reqSelBits.uop.robIdx) || (isNotBefore(req.uop.robIdx, reqSelBits.uop.robIdx) && req.uop.uopIdx > reqSelBits.uop.uopIdx)) &&
169      bufferState === s_idle
170    ) {
171      connectSamePort(req, reqSelBits)
172      req.portIndex := reqSelPort
173      cross4KBPageEnq := true.B
174      needFlushPipe   := true.B
175    } .otherwise {
176      req := req
177      cross4KBPageEnq := false.B
178    }
179  }
180
181  val reqSelCanEnq = UIntToOH(reqSelPort)
182
183  io.req.zipWithIndex.map{
184    case (reqPort, index) => reqPort.ready := reqSelCanEnq(index) && (!req_valid || cross4KBPageBoundary && cross4KBPageEnq)
185  }
186
187  io.toVecStoreMergeBuffer.zipWithIndex.map{
188    case (toStMB, index) => {
189      toStMB.flush   := req_valid && cross4KBPageBoundary && cross4KBPageEnq && UIntToOH(req.portIndex)(index)
190      toStMB.mbIndex := req.mbIndex
191    }
192  }
193  io.full := req_valid
194
195  //logic
196  val splitStoreReqs = RegInit(VecInit(List.fill(maxSplitNum)(0.U.asTypeOf(new LsPipelineBundle))))
197  val splitStoreResp = RegInit(VecInit(List.fill(maxSplitNum)(0.U.asTypeOf(new SqWriteBundle))))
198  val isCrossPage    = RegInit(false.B)
199  val exceptionVec   = RegInit(0.U.asTypeOf(ExceptionVec()))
200  val unSentStores   = RegInit(0.U(maxSplitNum.W))
201  val unWriteStores  = RegInit(0.U(maxSplitNum.W))
202  val curPtr = RegInit(0.U(log2Ceil(maxSplitNum).W))
203
204  // if there is exception or mmio in split store
205  val globalException = RegInit(false.B)
206  val globalMMIO = RegInit(false.B)
207
208  val hasException = io.splitStoreResp.bits.vecActive && !io.splitStoreResp.bits.need_rep &&
209    ExceptionNO.selectByFu(io.splitStoreResp.bits.uop.exceptionVec, StaCfg).asUInt.orR || TriggerAction.isDmode(io.splitStoreResp.bits.uop.trigger)
210  val isMMIO = io.splitStoreResp.bits.mmio && !io.splitStoreResp.bits.need_rep
211
212  io.sqControl.toStoreQueue.crossPageWithHit := io.sqControl.toStoreMisalignBuffer.sqPtr === req.uop.sqIdx && isCrossPage
213  io.sqControl.toStoreQueue.crossPageCanDeq := !isCrossPage || bufferState === s_block
214  io.sqControl.toStoreQueue.paddr := Cat(splitStoreResp(1).paddr(splitStoreResp(1).paddr.getWidth - 1, 3), 0.U(3.W))
215
216  io.sqControl.toStoreQueue.withSameUop := io.sqControl.toStoreMisalignBuffer.uop.robIdx === req.uop.robIdx && io.sqControl.toStoreMisalignBuffer.uop.uopIdx === req.uop.uopIdx && req.isvec && robMatch && isCrossPage
217
218  //state transition
219  switch(bufferState) {
220    is (s_idle) {
221      when(cross4KBPageBoundary) {
222        when(robMatch) {
223          bufferState := s_split
224          isCrossPage := true.B
225        }
226      } .otherwise {
227        when (req_valid) {
228          bufferState := s_split
229          isCrossPage := false.B
230        }
231      }
232
233    }
234
235    is (s_split) {
236      bufferState := s_req
237    }
238
239    is (s_req) {
240      when (io.splitStoreReq.fire) {
241        bufferState := s_resp
242      }
243    }
244
245    is (s_resp) {
246      when (io.splitStoreResp.valid) {
247        val clearOh = UIntToOH(curPtr)
248        when (hasException || isMMIO) {
249          // commit directly when exception ocurs
250          // if any split store reaches mmio space, delegate to software storeAddrMisaligned exception
251          bufferState := s_wb
252          globalException := hasException
253          globalMMIO := isMMIO
254        } .elsewhen(io.splitStoreResp.bits.need_rep || (unSentStores & (~clearOh).asUInt).orR) {
255          // need replay or still has unsent requests
256          bufferState := s_req
257        } .otherwise {
258          // got result, goto calculate data and control sq
259          bufferState := s_wb
260        }
261      }
262    }
263
264    is (s_wb) {
265      when (req.isvec) {
266        when (io.vecWriteBack.map(x => x.fire).reduce( _ || _)) {
267          bufferState := s_idle
268          req_valid := false.B
269          curPtr := 0.U
270          unSentStores := 0.U
271          unWriteStores := 0.U
272          globalException := false.B
273          globalMMIO := false.B
274          isCrossPage := false.B
275          needFlushPipe := false.B
276        }
277
278      }.otherwise {
279        when (io.writeBack.fire && (!isCrossPage || globalMMIO || globalException)) {
280          bufferState := s_idle
281          req_valid := false.B
282          curPtr := 0.U
283          unSentStores := 0.U
284          unWriteStores := 0.U
285          globalException := false.B
286          globalMMIO := false.B
287          isCrossPage := false.B
288          needFlushPipe := false.B
289        } .elsewhen(io.writeBack.fire && isCrossPage) {
290          bufferState := s_block
291        } .otherwise {
292          bufferState := s_wb
293        }
294
295      }
296    }
297
298    is (s_block) {
299      when (io.sqControl.toStoreMisalignBuffer.doDeq) {
300        bufferState := s_idle
301        req_valid := false.B
302        curPtr := 0.U
303        unSentStores := 0.U
304        unWriteStores := 0.U
305        globalException := false.B
306        globalMMIO := false.B
307        isCrossPage := false.B
308      }
309    }
310  }
311
312  val alignedType = Mux(req.isvec, req.alignedType(1,0), req.uop.fuOpType(1, 0))
313
314  val highAddress = LookupTree(alignedType, List(
315    SB -> 0.U,
316    SH -> 1.U,
317    SW -> 3.U,
318    SD -> 7.U
319  )) + req.vaddr(4, 0)
320
321  val highPageAddress = LookupTree(alignedType, List(
322    SB -> 0.U,
323    SH -> 1.U,
324    SW -> 3.U,
325    SD -> 7.U
326  )) + req.vaddr(12, 0)
327  // to see if (vaddr + opSize - 1) and vaddr are in the same 16 bytes region
328  val cross16BytesBoundary = req_valid && (highAddress(4) =/= req.vaddr(4))
329  cross4KBPageBoundary := req_valid && (highPageAddress(12) =/= req.vaddr(12))
330  val aligned16BytesAddr   = (req.vaddr >> 4) << 4// req.vaddr & ~("b1111".U)
331  val aligned16BytesSel    = req.vaddr(3, 0)
332
333  // meta of 128 bit store
334  val new128Store = WireInit(0.U.asTypeOf(new LsPipelineBundle))
335  // meta of split loads
336  val lowAddrStore  = WireInit(0.U.asTypeOf(new LsPipelineBundle))
337  val highAddrStore = WireInit(0.U.asTypeOf(new LsPipelineBundle))
338  // final lowResult = Cat(`lowResultWidth` of store data, 0.U(make it to fill total length of Vlen))
339  val lowResultWidth = RegInit(0.U(3.W)) // how many bytes should we take from the store data
340  // final highResult = Zero extend to Vlen(`highResultWidth` of (store data >> lowResultWidth))
341  val highResultWidth = RegInit(0.U(3.W)) // how many bytes should we take from the store data
342
343  when (bufferState === s_split) {
344    when (!cross16BytesBoundary) {
345      assert(false.B, s"There should be no non-aligned access that does not cross 16Byte boundaries.")
346    } .otherwise {
347      // split this unaligned store into `maxSplitNum` aligned stores
348      unWriteStores := Fill(maxSplitNum, 1.U(1.W))
349      unSentStores := Fill(maxSplitNum, 1.U(1.W))
350      curPtr := 0.U
351      lowAddrStore.uop := req.uop
352      lowAddrStore.uop.exceptionVec(storeAddrMisaligned) := false.B
353      highAddrStore.uop := req.uop
354      highAddrStore.uop.exceptionVec(storeAddrMisaligned) := false.B
355
356      switch (alignedType(1, 0)) {
357        is (SB) {
358          assert(false.B, "lb should not trigger miss align")
359        }
360
361        is (SH) {
362          lowAddrStore.uop.fuOpType := SB
363          lowAddrStore.vaddr := req.vaddr
364          lowAddrStore.mask  := 0x1.U << lowAddrStore.vaddr(3, 0)
365          lowResultWidth    := BYTE1
366
367          highAddrStore.uop.fuOpType := SB
368          highAddrStore.vaddr := req.vaddr + 1.U
369          highAddrStore.mask  := 0x1.U << highAddrStore.vaddr(3, 0)
370          highResultWidth    := BYTE1
371        }
372
373        is (SW) {
374          switch (req.vaddr(1, 0)) {
375            is ("b00".U) {
376              assert(false.B, "should not trigger miss align")
377            }
378
379            is ("b01".U) {
380              lowAddrStore.uop.fuOpType := SW
381              lowAddrStore.vaddr := req.vaddr - 1.U
382              lowAddrStore.mask  := 0xf.U << lowAddrStore.vaddr(3, 0)
383              lowResultWidth    := BYTE3
384
385              highAddrStore.uop.fuOpType := SB
386              highAddrStore.vaddr := req.vaddr + 3.U
387              highAddrStore.mask  := 0x1.U << highAddrStore.vaddr(3, 0)
388              highResultWidth    := BYTE1
389            }
390
391            is ("b10".U) {
392              lowAddrStore.uop.fuOpType := SH
393              lowAddrStore.vaddr := req.vaddr
394              lowAddrStore.mask  := 0x3.U << lowAddrStore.vaddr(3, 0)
395              lowResultWidth    := BYTE2
396
397              highAddrStore.uop.fuOpType := SH
398              highAddrStore.vaddr := req.vaddr + 2.U
399              highAddrStore.mask  := 0x3.U << highAddrStore.vaddr(3, 0)
400              highResultWidth    := BYTE2
401            }
402
403            is ("b11".U) {
404              lowAddrStore.uop.fuOpType := SB
405              lowAddrStore.vaddr := req.vaddr
406              lowAddrStore.mask  := 0x1.U << lowAddrStore.vaddr(3, 0)
407              lowResultWidth    := BYTE1
408
409              highAddrStore.uop.fuOpType := SW
410              highAddrStore.vaddr := req.vaddr + 1.U
411              highAddrStore.mask  := 0xf.U << highAddrStore.vaddr(3, 0)
412              highResultWidth    := BYTE3
413            }
414          }
415        }
416
417        is (SD) {
418          switch (req.vaddr(2, 0)) {
419            is ("b000".U) {
420              assert(false.B, "should not trigger miss align")
421            }
422
423            is ("b001".U) {
424              lowAddrStore.uop.fuOpType := SD
425              lowAddrStore.vaddr := req.vaddr - 1.U
426              lowAddrStore.mask  := 0xff.U << lowAddrStore.vaddr(3, 0)
427              lowResultWidth    := BYTE7
428
429              highAddrStore.uop.fuOpType := SB
430              highAddrStore.vaddr := req.vaddr + 7.U
431              highAddrStore.mask  := 0x1.U << highAddrStore.vaddr(3, 0)
432              highResultWidth    := BYTE1
433            }
434
435            is ("b010".U) {
436              lowAddrStore.uop.fuOpType := SD
437              lowAddrStore.vaddr := req.vaddr - 2.U
438              lowAddrStore.mask  := 0xff.U << lowAddrStore.vaddr(3, 0)
439              lowResultWidth    := BYTE6
440
441              highAddrStore.uop.fuOpType := SH
442              highAddrStore.vaddr := req.vaddr + 6.U
443              highAddrStore.mask  := 0x3.U << highAddrStore.vaddr(3, 0)
444              highResultWidth    := BYTE2
445            }
446
447            is ("b011".U) {
448              lowAddrStore.uop.fuOpType := SD
449              lowAddrStore.vaddr := req.vaddr - 3.U
450              lowAddrStore.mask  := 0xff.U << lowAddrStore.vaddr(3, 0)
451              lowResultWidth    := BYTE5
452
453              highAddrStore.uop.fuOpType := SW
454              highAddrStore.vaddr := req.vaddr + 5.U
455              highAddrStore.mask  := 0xf.U << highAddrStore.vaddr(3, 0)
456              highResultWidth    := BYTE3
457            }
458
459            is ("b100".U) {
460              lowAddrStore.uop.fuOpType := SW
461              lowAddrStore.vaddr := req.vaddr
462              lowAddrStore.mask  := 0xf.U << lowAddrStore.vaddr(3, 0)
463              lowResultWidth    := BYTE4
464
465              highAddrStore.uop.fuOpType := SW
466              highAddrStore.vaddr := req.vaddr + 4.U
467              highAddrStore.mask  := 0xf.U << highAddrStore.vaddr(3, 0)
468              highResultWidth    := BYTE4
469            }
470
471            is ("b101".U) {
472              lowAddrStore.uop.fuOpType := SD
473              lowAddrStore.vaddr := req.vaddr - 5.U
474              lowAddrStore.mask  := 0xff.U << lowAddrStore.vaddr(3, 0)
475              lowResultWidth    := BYTE3
476
477              highAddrStore.uop.fuOpType := SD
478              highAddrStore.vaddr := req.vaddr + 3.U
479              highAddrStore.mask  := 0xff.U << highAddrStore.vaddr(3, 0)
480              highResultWidth    := BYTE5
481            }
482
483            is ("b110".U) {
484              lowAddrStore.uop.fuOpType := SD
485              lowAddrStore.vaddr := req.vaddr - 6.U
486              lowAddrStore.mask  := 0xff.U << lowAddrStore.vaddr(3, 0)
487              lowResultWidth    := BYTE2
488
489              highAddrStore.uop.fuOpType := SD
490              highAddrStore.vaddr := req.vaddr + 2.U
491              highAddrStore.mask  := 0xff.U << highAddrStore.vaddr(3, 0)
492              highResultWidth    := BYTE6
493            }
494
495            is ("b111".U) {
496              lowAddrStore.uop.fuOpType := SD
497              lowAddrStore.vaddr := req.vaddr - 7.U
498              lowAddrStore.mask  := 0xff.U << lowAddrStore.vaddr(3, 0)
499              lowResultWidth    := BYTE1
500
501              highAddrStore.uop.fuOpType := SD
502              highAddrStore.vaddr := req.vaddr + 1.U
503              highAddrStore.mask  := 0xff.U << highAddrStore.vaddr(3, 0)
504              highResultWidth    := BYTE7
505            }
506          }
507        }
508      }
509
510      splitStoreReqs(0) := lowAddrStore
511      splitStoreReqs(1) := highAddrStore
512    }
513  }
514
515  io.splitStoreReq.valid := req_valid && (bufferState === s_req)
516  io.splitStoreReq.bits  := splitStoreReqs(curPtr)
517  io.splitStoreReq.bits.isvec  := req.isvec
518  // Restore the information of H extension store
519  // bit encoding: | hsv 1 | store 00 | size(2bit) |
520  val reqIsHsv  = LSUOpType.isHsv(req.uop.fuOpType)
521  io.splitStoreReq.bits.uop.fuOpType := Mux(req.isvec, req.uop.fuOpType, Cat(reqIsHsv, 0.U(2.W), splitStoreReqs(curPtr).uop.fuOpType(1, 0)))
522  io.splitStoreReq.bits.alignedType  := Mux(req.isvec, splitStoreReqs(curPtr).uop.fuOpType(1, 0), req.alignedType)
523  io.splitStoreReq.bits.isFinalSplit := curPtr(0)
524
525  when (io.splitStoreResp.valid) {
526    val resp = io.splitStoreResp.bits
527    splitStoreResp(curPtr) := io.splitStoreResp.bits
528    when (isMMIO) {
529      unWriteStores := 0.U
530      unSentStores := 0.U
531      exceptionVec := ExceptionNO.selectByFu(0.U.asTypeOf(exceptionVec.cloneType), StaCfg)
532      // delegate to software
533      exceptionVec(storeAddrMisaligned) := true.B
534    } .elsewhen (hasException) {
535      unWriteStores := 0.U
536      unSentStores := 0.U
537      StaCfg.exceptionOut.map(no => exceptionVec(no) := exceptionVec(no) || resp.uop.exceptionVec(no))
538    } .elsewhen (!io.splitStoreResp.bits.need_rep) {
539      unSentStores := unSentStores & (~UIntToOH(curPtr)).asUInt
540      curPtr := curPtr + 1.U
541      exceptionVec := 0.U.asTypeOf(ExceptionVec())
542    }
543  }
544
545  val splitStoreData = RegInit(VecInit(List.fill(maxSplitNum)(0.U.asTypeOf(new XSBundle {
546    val wdata = UInt(VLEN.W)
547    val wmask = UInt((VLEN / 8).W)
548  }))))
549
550  val wmaskLow  = Wire(Vec(VLEN / 8, Bool()))
551  val wmaskHigh = Wire(Vec(VLEN / 8, Bool()))
552  (0 until (VLEN / 8)).map {
553    case i  => {
554      when (i.U < highResultWidth) {
555        wmaskHigh(i) := true.B
556      } .otherwise {
557        wmaskHigh(i) := false.B
558      }
559      when (i.U < lowResultWidth) {
560        wmaskLow(i) := true.B
561      } .otherwise {
562        wmaskLow(i) := false.B
563      }
564    }
565  }
566
567  io.writeBack.valid := req_valid && (bufferState === s_wb) && !io.storeOutValid && !req.isvec
568  io.writeBack.bits.uop := req.uop
569  io.writeBack.bits.uop.exceptionVec := DontCare
570  StaCfg.exceptionOut.map(no => io.writeBack.bits.uop.exceptionVec(no) := (globalMMIO || globalException) && exceptionVec(no))
571  io.writeBack.bits.uop.flushPipe := needFlushPipe
572  io.writeBack.bits.uop.replayInst := false.B
573  io.writeBack.bits.data := DontCare
574  io.writeBack.bits.isFromLoadUnit := DontCare
575  io.writeBack.bits.debug.isMMIO := globalMMIO
576  // FIXME lyq: temporarily set to false
577  io.writeBack.bits.debug.isNC := false.B
578  io.writeBack.bits.debug.isPerfCnt := false.B
579  io.writeBack.bits.debug.paddr := req.paddr
580  io.writeBack.bits.debug.vaddr := req.vaddr
581
582  io.vecWriteBack.zipWithIndex.map{
583    case (wb, index) => {
584      wb.valid := req_valid && (bufferState === s_wb) && req.isvec && !io.storeVecOutValid && UIntToOH(req.portIndex)(index)
585
586      wb.bits.mBIndex           := req.mbIndex
587      wb.bits.hit               := true.B
588      wb.bits.isvec             := true.B
589      wb.bits.sourceType        := RSFeedbackType.tlbMiss
590      wb.bits.flushState        := DontCare
591      wb.bits.trigger           := TriggerAction.None
592      wb.bits.mmio              := globalMMIO
593      wb.bits.exceptionVec      := ExceptionNO.selectByFu(exceptionVec, VstuCfg)
594      wb.bits.hasException      := globalException
595      wb.bits.usSecondInv       := req.usSecondInv
596      wb.bits.vecFeedback       := true.B
597      wb.bits.elemIdx           := req.elemIdx
598      wb.bits.alignedType       := req.alignedType
599      wb.bits.mask              := req.mask
600      wb.bits.vaddr             := req.vaddr
601      wb.bits.vaNeedExt         := req.vaNeedExt
602      wb.bits.gpaddr            := req.gpaddr
603      wb.bits.isForVSnonLeafPTE := req.isForVSnonLeafPTE
604      wb.bits.vstart            := req.uop.vpu.vstart
605      wb.bits.vecTriggerMask    := 0.U
606      wb.bits.nc                := false.B
607    }
608  }
609
610  val flush = req_valid && req.uop.robIdx.needFlush(io.redirect)
611
612  when (flush) {
613    bufferState := s_idle
614    req_valid := Mux(cross4KBPageEnq && cross4KBPageBoundary && !reqRedirect, req_valid, false.B)
615    curPtr := 0.U
616    unSentStores := 0.U
617    unWriteStores := 0.U
618    globalException := false.B
619    globalMMIO := false.B
620    isCrossPage := false.B
621    needFlushPipe := false.B
622  }
623
624  // NOTE: spectial case (unaligned store cross page, page fault happens in next page)
625  // if exception happens in the higher page address part, overwrite the storeExceptionBuffer vaddr
626  val shouldOverwrite = req_valid && cross16BytesBoundary && globalException && (curPtr === 1.U)
627  val overwriteExpBuf = GatedValidRegNext(shouldOverwrite)
628  val overwriteVaddr = RegEnable(splitStoreResp(curPtr).vaddr, shouldOverwrite)
629  val overwriteIsHyper = RegEnable(splitStoreResp(curPtr).isHyper, shouldOverwrite)
630  val overwriteGpaddr = RegEnable(splitStoreResp(curPtr).gpaddr, shouldOverwrite)
631  val overwriteIsForVSnonLeafPTE = RegEnable(splitStoreResp(curPtr).isForVSnonLeafPTE, shouldOverwrite)
632
633  //TODO In theory, there is no need to overwrite, but for now, the signal is retained in the code in this way.
634  // and the signal will be removed after sufficient verification.
635  io.overwriteExpBuf.valid := false.B
636  io.overwriteExpBuf.vaddr := overwriteVaddr
637  io.overwriteExpBuf.isHyper := overwriteIsHyper
638  io.overwriteExpBuf.gpaddr := overwriteGpaddr
639  io.overwriteExpBuf.isForVSnonLeafPTE := overwriteIsForVSnonLeafPTE
640
641  XSPerfAccumulate("alloc",                  RegNext(!req_valid) && req_valid)
642  XSPerfAccumulate("flush",                  flush)
643  XSPerfAccumulate("flush_idle",             flush && (bufferState === s_idle))
644  XSPerfAccumulate("flush_non_idle",         flush && (bufferState =/= s_idle))
645}
646