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