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