No public description PiperOrigin-RevId: 1003183485 Change-Id: I1b8ed251a8f1da8e8e03b41057c49d438d2be616
diff --git a/riscv/BUILD b/riscv/BUILD index 3bef38f..aa9aee9 100644 --- a/riscv/BUILD +++ b/riscv/BUILD
@@ -30,6 +30,8 @@ "riscv_zvt.bin_fmt", "riscv_zicbom.isa", "riscv_zicbom.bin_fmt", + "riscv_zicboz.isa", + "riscv_zicboz.bin_fmt", "LICENSE", "riscv32g.bin_fmt", "riscv32g.isa", @@ -292,6 +294,7 @@ "riscv_zhintpause_instructions.cc", "riscv_zicbom_instructions.cc", "riscv_zicbop_instructions.cc", + "riscv_zicboz_instructions.cc", "riscv_zihintntl_instructions.cc", "riscv_zimop_instructions.cc", ], @@ -300,6 +303,7 @@ "riscv_zhintpause_instructions.h", "riscv_zicbom_instructions.h", "riscv_zicbop_instructions.h", + "riscv_zicboz_instructions.h", "riscv_zihintntl_instructions.h", "riscv_zimop_instructions.h", ], @@ -311,6 +315,7 @@ ":riscv_g", ":riscv_state", "@mpact-sim//mpact/sim/generic:arch_state", + "@mpact-sim//mpact/sim/generic:core", "@mpact-sim//mpact/sim/generic:instruction", ], ) @@ -507,6 +512,7 @@ "riscv_zhintpause.isa", "riscv_zicbom.isa", "riscv_zicbop.isa", + "riscv_zicboz.isa", "riscv_zicond.isa", "riscv_zihintntl.isa", "riscv_zimop.isa", @@ -536,6 +542,7 @@ "riscv_zhintpause.bin_fmt", "riscv_zicbom.bin_fmt", "riscv_zicbop.bin_fmt", + "riscv_zicboz.bin_fmt", "riscv_zicond.bin_fmt", "riscv_zihintntl.bin_fmt", "riscv_zimop.bin_fmt",
diff --git a/riscv/riscv_zicboz.bin_fmt b/riscv/riscv_zicboz.bin_fmt new file mode 100644 index 0000000..7540ff8 --- /dev/null +++ b/riscv/riscv_zicboz.bin_fmt
@@ -0,0 +1,22 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// This file defines the encoding for the cache block zero instruction in the +// Zicboz extension. + +#include "riscv_format32.bin_fmt" + +instruction group RiscVZicboz[32] : Inst32Format { + cbo_zero : IType : u_imm12 == 0b0000'0000'0100, rd == 0b00000, func3 == 0b010, opcode == 0b000'1111; +}
diff --git a/riscv/riscv_zicboz.isa b/riscv/riscv_zicboz.isa new file mode 100644 index 0000000..9dcc64a --- /dev/null +++ b/riscv/riscv_zicboz.isa
@@ -0,0 +1,46 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// This file defines the cache block zero instruction in the Zicboz extension. +// +// The cache block size zeroed by `cbo.zero` is implementation-specific. It is +// bound to the semantic function here as 64 bytes, the common cache line size +// across RISC-V profiles. ISAs for implementations with a different cache block +// size can override `cbo_zero` and bind their own size. + +slot riscv_zicboz32 { + includes { + #include "absl/functional/bind_front.h" + #include "riscv/riscv_zicboz_instructions.h" + } + default size = 4; + opcodes { + cbo_zero{: rs1 :}, + resources: { rs1 :}, + disasm: "cbo.zero", "0(%rs1)", + semfunc: "absl::bind_front(&RV32::RiscVCboZero, /*cache_block_size=*/64)"; + } +} + +slot riscv_zicboz64 : riscv_zicboz32 { + includes { + #include "absl/functional/bind_front.h" + #include "riscv/riscv_zicboz_instructions.h" + } + default size = 4; + opcodes { + cbo_zero = override, + semfunc: "absl::bind_front(&RV64::RiscVCboZero, /*cache_block_size=*/64)"; + } +}
diff --git a/riscv/riscv_zicboz_instructions.cc b/riscv/riscv_zicboz_instructions.cc new file mode 100644 index 0000000..28dea8a --- /dev/null +++ b/riscv/riscv_zicboz_instructions.cc
@@ -0,0 +1,75 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "riscv/riscv_zicboz_instructions.h" + +#include <cstdint> +#include <cstring> + +#include "mpact/sim/generic/data_buffer.h" +#include "mpact/sim/generic/instruction.h" +#include "riscv/riscv_state.h" + +// This file contains the definitions of the instruction semantic functions +// for the cache block zero instruction in the Zicboz extension. + +namespace mpact::sim::riscv { + +using ::mpact::sim::generic::DataBuffer; +using ::mpact::sim::generic::Instruction; + +namespace { + +// `cbo.zero` is a cache-block management instruction that writes zeros to the +// entire cache block containing the effective address in `rs1`. +// +// Unlike `Zicbom` cache management instructions (`cbo.clean`, `cbo.flush`, +// `cbo.inval`), which only affect microarchitectural cache state and can be +// modeled as no-ops when caches are absent, `cbo.zero` modifies architectural +// memory state so that subsequent loads from the block return zero. The +// instruction is modeled by aligning `rs1` down to a `cache_block_size`-byte +// boundary and storing `cache_block_size` bytes of zeros to memory. +template <typename UIntReg> +void CboZero(int cache_block_size, const Instruction* inst) { + auto* const state = static_cast<RiscVState*>(inst->state()); + const UIntReg address = generic::GetInstructionSource<UIntReg>(inst, 0); + const UIntReg block_mask = ~static_cast<UIntReg>(cache_block_size - 1); + const UIntReg block_address = address & block_mask; + DataBuffer* const db = state->db_factory()->Allocate(cache_block_size); + std::memset(db->raw_ptr(), 0, cache_block_size); + state->StoreMemory(inst, block_address, db); + db->DecRef(); +} + +} // namespace + +namespace RV32 { + +// Executes the RV32 `cbo.zero` instruction. +void RiscVCboZero(int cache_block_size, const Instruction* inst) { + CboZero<uint32_t>(cache_block_size, inst); +} + +} // namespace RV32 + +namespace RV64 { + +// Executes the RV64 `cbo.zero` instruction. +void RiscVCboZero(int cache_block_size, const Instruction* inst) { + CboZero<uint64_t>(cache_block_size, inst); +} + +} // namespace RV64 + +} // namespace mpact::sim::riscv
diff --git a/riscv/riscv_zicboz_instructions.h b/riscv/riscv_zicboz_instructions.h new file mode 100644 index 0000000..4f77640 --- /dev/null +++ b/riscv/riscv_zicboz_instructions.h
@@ -0,0 +1,45 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef THIRD_PARTY_MPACT_RISCV_RISCV_ZICBOZ_INSTRUCTIONS_H_ +#define THIRD_PARTY_MPACT_RISCV_RISCV_ZICBOZ_INSTRUCTIONS_H_ + +#include "mpact/sim/generic/instruction.h" + +// This file contains the declarations of the instruction semantic functions +// for the cache block zero instruction in the Zicboz extension. + +namespace mpact::sim::riscv { + +using ::mpact::sim::generic::Instruction; + +namespace RV32 { + +// Writes zeros to the `cache_block_size`-byte aligned cache block containing +// the effective address specified in `rs1`. +void RiscVCboZero(int cache_block_size, const Instruction* inst); + +} // namespace RV32 + +namespace RV64 { + +// Writes zeros to the `cache_block_size`-byte aligned cache block containing +// the effective address specified in `rs1`. +void RiscVCboZero(int cache_block_size, const Instruction* inst); + +} // namespace RV64 + +} // namespace mpact::sim::riscv + +#endif // THIRD_PARTY_MPACT_RISCV_RISCV_ZICBOZ_INSTRUCTIONS_H_
diff --git a/riscv/rvm23.bin_fmt b/riscv/rvm23.bin_fmt index 97f766f..1b129af 100644 --- a/riscv/rvm23.bin_fmt +++ b/riscv/rvm23.bin_fmt
@@ -36,6 +36,7 @@ // Cache prefetch instructions. RiscVZicbop, RiscVZicbom, + RiscVZicboz, }; RVM23Inst16 = { RiscVZcmop, @@ -54,6 +55,7 @@ #include "riscv_zhintpause.bin_fmt" #include "riscv_zicbom.bin_fmt" #include "riscv_zicbop.bin_fmt" +#include "riscv_zicboz.bin_fmt" #include "riscv_zicond.bin_fmt" #include "riscv_zihintntl.bin_fmt" #include "riscv_zimop.bin_fmt"
diff --git a/riscv/rvm23.isa b/riscv/rvm23.isa index 91fa8eb..ac3a9c1 100644 --- a/riscv/rvm23.isa +++ b/riscv/rvm23.isa
@@ -25,6 +25,7 @@ #include "riscv_zc.isa" #include "riscv_zicbom.isa" #include "riscv_zicbop.isa" +#include "riscv_zicboz.isa" #include "riscv_zicond.isa" #include "riscv_zimop.isa" #include "riscv_zhintpause.isa" @@ -54,7 +55,8 @@ riscv_zcmp32, riscv_zcmt32, riscv_zicbom32, - riscv_zicbop32 { + riscv_zicbop32, + riscv_zicboz32 { default opcode = disasm: "Illegal instruction at 0x%(@:08x)", semfunc: "&RiscVIllegalInstruction";
diff --git a/riscv/test/BUILD b/riscv/test/BUILD index 9a65dba..ff8842b 100644 --- a/riscv/test/BUILD +++ b/riscv/test/BUILD
@@ -1042,6 +1042,23 @@ ], ) +cc_test( + name = "riscv_zicboz_instructions_test", + size = "small", + srcs = ["riscv_zicboz_instructions_test.cc"], + deps = [ + "//riscv:riscv_state", + "//riscv:rvm23_instructions", + "@abseil-cpp//absl/functional:bind_front", + "@abseil-cpp//absl/strings:string_view", + "@abseil-cpp//absl/types:span", + "@com_google_googletest//:gtest_main", + "@mpact-sim//mpact/sim/generic:core", + "@mpact-sim//mpact/sim/generic:instruction", + "@mpact-sim//mpact/sim/util/memory", + ], +) + config_setting( name = "arm_cpu", values = {"cpu": "arm"},
diff --git a/riscv/test/riscv_zicboz_instructions_test.cc b/riscv/test/riscv_zicboz_instructions_test.cc new file mode 100644 index 0000000..180c54b --- /dev/null +++ b/riscv/test/riscv_zicboz_instructions_test.cc
@@ -0,0 +1,212 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "riscv/riscv_zicboz_instructions.h" + +#include <cstdint> +#include <type_traits> +#include <vector> + +#include "absl/functional/bind_front.h" +#include "absl/strings/string_view.h" +#include "absl/types/span.h" +#include "googlemock/include/gmock/gmock.h" +#include "mpact/sim/generic/data_buffer.h" +#include "mpact/sim/generic/instruction.h" +#include "mpact/sim/util/memory/flat_demand_memory.h" +#include "riscv/riscv_register.h" +#include "riscv/riscv_state.h" + +// This file contains tests for the Zicboz `cbo.zero` instruction on both RV32 +// and RV64. + +namespace { + +using ::mpact::sim::generic::DataBuffer; +using ::mpact::sim::generic::Instruction; +using ::mpact::sim::riscv::ExceptionCode; +using ::mpact::sim::riscv::RiscVState; +using ::mpact::sim::riscv::RiscVXlen; +using ::mpact::sim::riscv::RV32Register; +using ::mpact::sim::riscv::RV64Register; +using ::testing::Each; + +constexpr absl::string_view kRs1Name = "x1"; +constexpr uint64_t kInstAddress = 0x2468; +constexpr uint64_t kMemBase = 0x1000; +constexpr uint64_t kMaxPhysicalAddress = 0xFFFF; +// Cache block size bound to `cbo.zero` in the tests. +constexpr int kCacheBlockSizeBytes = 64; +// The starting address of the middle block within a three-block region spanning +// from `kMemBase` to `kMemBase + 3 * kCacheBlockSizeBytes` to verify that +// adjacent blocks remain untouched. +constexpr uint64_t kTargetBlock = kMemBase + kCacheBlockSizeBytes; +// Non-zero fill pattern to initialize the memory. +constexpr uint8_t kFilledValue = 0xA5; + +template <typename Reg> +class RiscVZicbozInstructionTest : public testing::Test { + protected: + using UIntReg = typename Reg::ValueType; + + RiscVZicbozInstructionTest() + : memory_(/*memory_size_in_units=*/0xffff'ffff'ffff'ffffULL, + /*base_address=*/0, /*addressable_unit_size=*/1, kFilledValue), + state_("test", + std::is_same_v<Reg, RV32Register> ? RiscVXlen::RV32 + : RiscVXlen::RV64, + &memory_), + instruction_(kInstAddress, &state_) { + instruction_.set_size(4); + instruction_.AppendSource(state_.template GetRegister<Reg>(kRs1Name) + .first->CreateSourceOperand()); + SetCacheBlockSize(kCacheBlockSizeBytes); + state_.set_on_trap([this](bool is_interrupt, uint64_t trap_value, + uint64_t exception_code, uint64_t epc, + const Instruction* inst) { + trap_taken_ = true; + trap_value_ = trap_value; + trap_code_ = exception_code; + return true; + }); + } + + // Binds `cache_block_size` to the `cbo.zero` semantic function under test. + void SetCacheBlockSize(int cache_block_size) { + using CboZeroFn = void (*)(int, const Instruction*); + constexpr CboZeroFn kCboZero = std::is_same_v<Reg, RV32Register> + ? &mpact::sim::riscv::RV32::RiscVCboZero + : &mpact::sim::riscv::RV64::RiscVCboZero; + instruction_.set_semantic_function( + absl::bind_front(kCboZero, cache_block_size)); + } + + // Sets `x1` (`rs1`) to `address` and executes `cbo.zero 0(x1)`. + void ExecuteCboZero(UIntReg address) { + Reg* const reg = state_.template GetRegister<Reg>(kRs1Name).first; + DataBuffer* const db = state_.db_factory()->template Allocate<UIntReg>(1); + db->template Set<UIntReg>(0, address); + reg->SetDataBuffer(db); + db->DecRef(); + instruction_.Execute(nullptr); + } + + // Reads and returns one `kCacheBlockSizeBytes`-byte block at `block_address`. + std::vector<uint8_t> ReadBlock(uint64_t block_address) { + DataBuffer* const db = state_.db_factory()->Allocate(kCacheBlockSizeBytes); + memory_.Load(block_address, db, nullptr, nullptr); + std::vector<uint8_t> bytes(kCacheBlockSizeBytes); + db->CopyTo(bytes.data()); + db->DecRef(); + return bytes; + } + + mpact::sim::util::FlatDemandMemory memory_; + RiscVState state_; + Instruction instruction_; + bool trap_taken_ = false; + uint64_t trap_value_ = 0; + uint64_t trap_code_ = 0; +}; + +using TestTypes = testing::Types<RV32Register, RV64Register>; +TYPED_TEST_SUITE(RiscVZicbozInstructionTest, TestTypes); + +// Verifies that `cbo.zero` with a block-aligned address zeros that block and +// leaves the preceding and following blocks unmodified. +TYPED_TEST(RiscVZicbozInstructionTest, AlignedAddressZerosBlock) { + this->ExecuteCboZero(kTargetBlock); + + EXPECT_THAT(this->ReadBlock(kTargetBlock), Each(0)); + EXPECT_THAT(this->ReadBlock(kTargetBlock - kCacheBlockSizeBytes), + Each(kFilledValue)); + EXPECT_THAT(this->ReadBlock(kTargetBlock + kCacheBlockSizeBytes), + Each(kFilledValue)); + EXPECT_FALSE(this->trap_taken_); +} + +// Verifies that an unaligned address within a cache block is aligned down to +// zero the entire containing block. +TYPED_TEST(RiscVZicbozInstructionTest, UnalignedAddressZerosContainingBlock) { + constexpr uint64_t kMisalignment = 0x2B; + this->ExecuteCboZero(kTargetBlock + kMisalignment); + + EXPECT_THAT(this->ReadBlock(kTargetBlock), Each(0)); + EXPECT_THAT(this->ReadBlock(kTargetBlock - kCacheBlockSizeBytes), + Each(kFilledValue)); + EXPECT_THAT(this->ReadBlock(kTargetBlock + kCacheBlockSizeBytes), + Each(kFilledValue)); + EXPECT_FALSE(this->trap_taken_); +} + +// Verifies that addressing the last byte of a cache block still zeros the +// entire containing block without spilling into the next block. +TYPED_TEST(RiscVZicbozInstructionTest, LastByteOfBlockZerosWholeBlock) { + this->ExecuteCboZero(kTargetBlock + kCacheBlockSizeBytes - 1); + + EXPECT_THAT(this->ReadBlock(kTargetBlock), Each(0)); + EXPECT_THAT(this->ReadBlock(kTargetBlock - kCacheBlockSizeBytes), + Each(kFilledValue)); + EXPECT_THAT(this->ReadBlock(kTargetBlock + kCacheBlockSizeBytes), + Each(kFilledValue)); + EXPECT_FALSE(this->trap_taken_); +} + +// Verifies that `cbo.zero` at address `0` zeros the first cache block. +TYPED_TEST(RiscVZicbozInstructionTest, ZeroBaseAddressZerosFirstBlock) { + this->ExecuteCboZero(0); + + EXPECT_THAT(this->ReadBlock(0), Each(0)); + EXPECT_THAT(this->ReadBlock(kCacheBlockSizeBytes), Each(kFilledValue)); + EXPECT_FALSE(this->trap_taken_); +} + +// Verifies that an address exceeding the maximum physical address raises a +// store access fault. +TYPED_TEST(RiscVZicbozInstructionTest, BeyondMaxPhysicalAddressTraps) { + this->state_.set_max_physical_address(kMaxPhysicalAddress); + constexpr uint64_t kFaultingBlock = + kMaxPhysicalAddress + 1 + kCacheBlockSizeBytes; + constexpr uint64_t kUnalignedOffset = 3; + + this->ExecuteCboZero(kFaultingBlock + kUnalignedOffset); + + EXPECT_TRUE(this->trap_taken_); + EXPECT_EQ(this->trap_code_, + static_cast<uint64_t>(ExceptionCode::kStoreAccessFault)); + EXPECT_EQ(this->trap_value_, kFaultingBlock); + EXPECT_THAT(this->ReadBlock(kFaultingBlock), Each(kFilledValue)); +} + +// Verifies that `cbo.zero` zeros exactly one block of the cache block size +// bound to the semantic function rather than a fixed size. +TYPED_TEST(RiscVZicbozInstructionTest, ZerosBlockOfBoundCacheBlockSize) { + constexpr int kSmallCacheBlockSizeBytes = 32; + constexpr uint64_t kUnalignedOffset = 5; + this->SetCacheBlockSize(kSmallCacheBlockSizeBytes); + + this->ExecuteCboZero(kTargetBlock + kSmallCacheBlockSizeBytes + + kUnalignedOffset); + + const std::vector<uint8_t> block = this->ReadBlock(kTargetBlock); + EXPECT_THAT(absl::MakeConstSpan(block).first(kSmallCacheBlockSizeBytes), + Each(kFilledValue)); + EXPECT_THAT(absl::MakeConstSpan(block).subspan(kSmallCacheBlockSizeBytes), + Each(0)); + EXPECT_THAT(this->ReadBlock(kTargetBlock + kCacheBlockSizeBytes), + Each(kFilledValue)); + EXPECT_FALSE(this->trap_taken_); +} + +} // namespace