forked from westerndigitalcorporation/swerv-ISS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Syscall.hpp
138 lines (108 loc) · 4.31 KB
/
Syscall.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright 2018 Western Digital Corporation or its affiliates.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>.
//
#pragma once
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <string>
namespace WdRiscv
{
template <typename URV>
class Hart;
template <typename URV>
class Syscall
{
public:
/// Signed register type corresponding to URV. For example, if URV
/// is uint32_t, then SRV will be int32_t.
typedef typename std::make_signed_t<URV> SRV;
Syscall(Hart<URV>& hart)
: hart_(hart)
{
auto mem_size = hart.getMemorySize();
mmap_blocks_.insert(std::make_pair(mem_size/2L, blk_t(mem_size/2L, true)));
}
// void print_mmap(const std::string prefix) {
// for(auto& it: mmap_blocks_)
// printf("%s --> 0x%llx: 0x%llx, %d\n",prefix.c_str(), it.first, it.second.length, it.second.free);
// fflush(stdout);
//
// }
/// Emulate a system call on the associated hart. Return an integer
/// value corresponding to the result.
URV emulate();
/// Redirect the given output file descriptor (typically stdout or
/// stderr) to the given file. Return true on success and false on
/// failure.
bool redirectOutputDescriptor(int fd, const std::string& path);
void enableLinux(bool flag)
{ linux_ = flag; }
/// Save the currently open file descriptors to the given file.
bool saveFileDescriptors(const std::string& path);
/// Load and open the file descriptors previously saved in given file.
bool loadFileDescriptors(const std::string& path);
/// Report the files opened by the target RISCV program during
/// current run.
void reportOpenedFiles(std::ostream& out);
uint64_t mmap_alloc(uint64_t size);
int mmap_dealloc(uint64_t addr, uint64_t size);
uint64_t mmap_remap(uint64_t addr, uint64_t old_size, uint64_t new_size, bool maymove);
void getUsedMemBlocks(std::vector<std::pair<uint64_t,uint64_t>>& used_blocks);
bool loadUsedMemBlocks(const std::string& filename, std::vector<std::pair<uint64_t,uint64_t>>& used_blocks);
bool saveUsedMemBlocks(const std::string& filename, std::vector<std::pair<uint64_t,uint64_t>>& used_blocks);
bool saveMmap(const std::string & filename);
bool loadMmap(const std::string & filename);
protected:
friend class Hart<URV>;
/// For Linux emulation: Set initial target program break to the
/// RISCV page address larger than or equal to the given address.
void setTargetProgramBreak(URV addr)
{ progBreak_ = addr; }
/// Return target program break.
URV targetProgramBreak() const
{ return progBreak_; }
/// Map Linux file descriptor to a RISCV file descriptor and install
/// the result in the riscv-to-linux fd map. Return remapped
/// descritpor or -1 if remapping is not possible.
int registerLinuxFd(int linuxFd, const std::string& path, bool isRead);
/// Return the effective (after redirection) file descriptor
/// corresponding to the target program file descriptor.
int effectiveFd(int fd)
{
if (fdMap_.count(fd))
return fdMap_.at(fd);
return fd;
}
private:
Hart<URV>& hart_;
bool linux_ = false;
URV progBreak_ = 0; // For brk Linux emulation.
struct blk_t {
blk_t(uint64_t length, bool free): length(length), free(free) {};
uint64_t length;
bool free;
};
using blk_map_t = std::map<uint64_t, blk_t>;
blk_map_t mmap_blocks_;
std::unordered_map<int, int> fdMap_;
std::unordered_map<int, bool> fdIsRead_;
std::unordered_map<int, std::string> fdPath_;
std::unordered_set<std::string> readPaths_;
std::unordered_set<std::string> writePaths_;
};
}