• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "gtest/gtest.h"
18 
19 #include <cstdint>
20 
21 #include "berberis/assembler/machine_code.h"
22 #include "berberis/guest_state/guest_addr.h"
23 #include "berberis/runtime_primitives/runtime_library.h"
24 #include "berberis/test_utils/scoped_exec_region.h"
25 
26 #include "lite_translator.h"
27 #include "register_maintainer.h"
28 
29 namespace berberis {
30 
31 namespace {
32 class Riscv64LiteTranslatorExecTest : public ::testing::Test {
33  public:
Riscv64LiteTranslatorExecTest()34   Riscv64LiteTranslatorExecTest()
35       : machine_code_(),
36         is_translated_(false),
37         state_{},
38         translator_(&machine_code_, kStartGuestAddr) {}
39 
Run()40   void Run() {
41     if (!is_translated_) {
42       FinalizeTranslation();
43     }
44     ScopedExecRegion exec(&machine_code_);
45     berberis_RunGeneratedCode(&state_, exec.get());
46   }
47 
48  private:
FinalizeTranslation()49   void FinalizeTranslation() {
50     is_translated_ = true;
51     translator_.as()->Finalize();
52   }
53 
54   // machine_code_ should precede translator_ for the correct construction.
55   MachineCode machine_code_;
56   bool is_translated_;
57 
58  protected:
59   // Upper 16-bits must be zero in a valid address.
60   constexpr static GuestAddr kStartGuestAddr = 0x0000aaaabbbbccccULL;
61   ThreadState state_;
62   LiteTranslator translator_;
63 };
64 
TEST_F(Riscv64LiteTranslatorExecTest,StoreMappedRegs)65 TEST_F(Riscv64LiteTranslatorExecTest, StoreMappedRegs) {
66   state_.cpu.x[1] = 0;
67 
68   translator_.as()->Movq(x86_64::Assembler::rax, 33);
69   translator_.SetReg(1, x86_64::Assembler::rax);
70   EXPECT_TRUE(translator_.gp_maintainer()->IsMapped(1));
71   EXPECT_TRUE(translator_.gp_maintainer()->IsModified(1));
72   translator_.StoreMappedRegs();
73   translator_.as()->Jmp(kEntryExitGeneratedCode);
74 
75   Run();
76   EXPECT_EQ(state_.cpu.x[1], 33ULL);
77 }
78 
TEST_F(Riscv64LiteTranslatorExecTest,StoreMappedFpRegs)79 TEST_F(Riscv64LiteTranslatorExecTest, StoreMappedFpRegs) {
80   state_.cpu.f[1] = 0;
81   LiteTranslator::FpRegister reg = translator_.AllocTempSimdReg();
82 
83   translator_.as()->Movq(x86_64::Assembler::rax, 33);
84   translator_.as()->Movq(reg, x86_64::Assembler::rax);
85   translator_.NanBoxAndSetFpReg<LiteTranslator::Float64>(1, reg);
86   EXPECT_TRUE(translator_.simd_maintainer()->IsMapped(1));
87   EXPECT_TRUE(translator_.simd_maintainer()->IsModified(1));
88   translator_.StoreMappedRegs();
89   translator_.as()->Jmp(kEntryExitGeneratedCode);
90 
91   Run();
92   EXPECT_EQ(state_.cpu.f[1], 33ULL);
93 }
94 
95 }  // namespace
96 
97 }  // namespace berberis