memory_manager.hpp 7.44 KB
Newer Older
1
2
3
#pragma once

#include <cstdint>
4
#include <string>
5
#include <map>
6
#include <stdexcept>
7
#include <unistd.h>
8

9
10
#include <villas/log.hpp>
#include <villas/directed_graph.hpp>
11
12
13

namespace villas {

14
15
16
17
18
19
20
21
22
/**
 * @brief Translation between a local (master) to a foreign (slave) address space
 *
 * Memory translations can be chained together using the `+=` operator which is
 * used internally by the MemoryManager to compute a translation through
 * multiple hops (memory mappings).
 */
class MemoryTranslation {
public:
23

24
25
26
27
28
29
30
31
	/**
	 * @brief MemoryTranslation
	 * @param src	Base address of local address space
	 * @param dst	Base address of foreign address space
	 * @param size	Size of "memory window"
	 */
	MemoryTranslation(uintptr_t src, uintptr_t dst, size_t size) :
	    src(src), dst(dst), size(size) {}
32

33
34
35
36
37
38
39
40
	template<typename ReturnType = uintptr_t>
	ReturnType
	getLocalAddr(uintptr_t addrInForeignAddrSpace) const
	{
		assert(addrInForeignAddrSpace >= dst);
		assert(addrInForeignAddrSpace < (dst + size));
		return reinterpret_cast<ReturnType>(src + addrInForeignAddrSpace - dst);
	}
41

42
43
	uintptr_t
	getForeignAddr(uintptr_t addrInLocalAddrSpace) const;
44

45
46
47
48
	size_t
	getSize() const
	{ return size; }

49
	friend std::ostream&
50
	operator<< (std::ostream& stream, const MemoryTranslation& translation)
51
	{
52
53
54
55
		return stream << std::hex
		              << "(src=0x"   << translation.src
		              << ", dst=0x"  << translation.dst
		              << ", size=0x" << translation.size
56
57
		              << ")";
	}
58

59
60
	/// Merge two MemoryTranslations together
	MemoryTranslation& operator+=(const MemoryTranslation& other);
61

62
private:
63
64
65
	uintptr_t	src;	///< Base address of local address space
	uintptr_t	dst;	///< Base address of foreign address space
	size_t		size;	///< Size of "memory window"
66
67
68
};


69
70
71
72
73
74
75
76
77
/**
 * @brief Global memory manager to resolve addresses across address spaces
 *
 * Every entity in the system has to register its (master) address space and
 * create mappings to other (slave) address spaces that it can access. A
 * directed graph is then constructed which allows to traverse addresses spaces
 * through multiple mappings and resolve addresses through this "tunnel" of
 * memory mappings.
 */
78
79
class MemoryManager {
private:
80
	// This is a singleton, so private constructor ...
81
	MemoryManager() :
82
	    memoryGraph("MemoryGraph"),
83
84
85
86
87
88
	    logger(loggerGetOrCreate("MemoryManager"))
	{
		pathCheckFunc = [&](const MemoryGraph::Path& path) {
			return this->pathCheck(path);
		};
	}
89

90
	// ... and no copying or assigning
91
	MemoryManager(const MemoryManager&) = delete;
92
	MemoryManager& operator=(const MemoryManager&) = delete;
93

94
95
96
97
98
99
100
101
102
103
104
105
106
	/**
	 * @brief Custom edge in memory graph representing a memory mapping
	 *
	 * A memory mapping maps from one address space into another and can only be
	 * traversed in the forward direction which reflects the nature of real
	 * memory mappings.
	 *
	 * Implementation Notes:
	 * The member #src is the address in the "from" address space, where the
	 * destination address space is mapped. The member #dest is the address in
	 * the destination address space, where the mapping points to. Often, #dest
	 * will be zero for mappings to hardware, but consider the example when
	 * mapping FPGA to application memory:
107
108
109
	 * The application allocates a block 1kB at address 0x843001000 in its
	 * address space. The mapping would then have a #dest address of 0x843001000
	 * and a #size of 1024.
110
111
112
113
114
115
116
117
118
119
120
121
122
123
	 */
	class Mapping : public graph::Edge {
	public:
		std::string	name;	///< Human-readable name
		uintptr_t	src;	///< Base address in "from" address space
		uintptr_t	dest;	///< Base address in "to" address space
		size_t		size;	///< Size of the mapping

		friend std::ostream&
		operator<< (std::ostream& stream, const Mapping& mapping)
		{
			return stream << static_cast<const Edge&>(mapping) << " = "
			              << mapping.name
			              << std::hex
124
			              << " (src=0x"  << mapping.src
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
			              << ", dest=0x" << mapping.dest
			              << ", size=0x" << mapping.size
			              << ")";
		}

	};


	/**
	 * @brief Custom vertex in memory graph representing an address space
	 *
	 * Since most information in the memory graph is stored in the edges (memory
	 * mappings), this is just a small extension to the default vertex. It only
	 * associates an additional string #name for human-readability.
	 */
	class AddressSpace : public graph::Vertex {
	public:
		std::string name;	///< Human-readable name

		friend std::ostream&
		operator<< (std::ostream& stream, const AddressSpace& addrSpace)
		{
			return stream << static_cast<const Vertex&>(addrSpace) << " = "
			              << addrSpace.name;
		}
	};

	/// Memory graph with custom edges and vertices for address resolution
153
154
	using MemoryGraph = graph::DirectedGraph<AddressSpace, Mapping>;

155
public:
156
157
158
	using AddressSpaceId = MemoryGraph::VertexIdentifier;
	using MappingId = MemoryGraph::EdgeIdentifier;

159
160
	struct InvalidTranslation : public std::exception {};

161
162
163
	/// Get singleton instance
	static MemoryManager&
	get();
164

165
166
167
	AddressSpaceId
	getProcessAddressSpace()
	{ return getOrCreateAddressSpace("villas-fpga"); }
168

169
170
171
172
	AddressSpaceId
	getPciAddressSpace()
	{ return getOrCreateAddressSpace("PCIe"); }

173
	AddressSpaceId
174
	getProcessAddressSpaceMemoryBlock(const std::string& memoryBlock)
175
176
177
	{ return getOrCreateAddressSpace(getSlaveAddrSpaceName("villas-fpga", memoryBlock)); }


178
179
	AddressSpaceId
	getOrCreateAddressSpace(std::string name);
180

181
182
183
184
	void
	removeAddressSpace(AddressSpaceId addrSpaceId)
	{ memoryGraph.removeVertex(addrSpaceId); }

185
	/// Create a default mapping
186
	MappingId
187
188
	createMapping(uintptr_t src, uintptr_t dest, size_t size,
	              const std::string& name,
189
190
	              AddressSpaceId fromAddrSpace,
	              AddressSpaceId toAddrSpace);
191
192
193
194
195

	/// Add a mapping
	///
	/// Can be used to derive from Mapping in order to implement custom
	/// constructor/destructor.
196
197
198
199
200
	MappingId
	addMapping(std::shared_ptr<Mapping> mapping,
	           AddressSpaceId fromAddrSpace,
	           AddressSpaceId toAddrSpace);

201

202
	AddressSpaceId
203
	findAddressSpace(const std::string& name);
204

205
206
207
	std::list<AddressSpaceId>
	findPath(AddressSpaceId fromAddrSpaceId, AddressSpaceId toAddrSpaceId);

208
209
210
211
212
213
214
215
	MemoryTranslation
	getTranslation(AddressSpaceId fromAddrSpaceId, AddressSpaceId toAddrSpaceId);

	MemoryTranslation
	getTranslationFromProcess(AddressSpaceId foreignAddrSpaceId)
	{ return getTranslation(getProcessAddressSpace(), foreignAddrSpaceId); }

	static std::string
216
	getSlaveAddrSpaceName(const std::string& ipInstance, const std::string& memoryBlock)
217
218
	{ return ipInstance + "/" + memoryBlock; }

219
220
221
222
	static std::string
	getMasterAddrSpaceName(const std::string& ipInstance, const std::string& busInterface)
	{ return ipInstance + ":" + busInterface; }

223
224
	void
	dump()
225
	{ memoryGraph.dump(); }
226

227
228
229
	void
	dumpToFile(const std::string& fileName)
	{ memoryGraph.dump(fileName); }
230

231
private:
232
233
234
235
236
	/// Convert a Mapping to MemoryTranslation for calculations
	static MemoryTranslation
	getTranslationFromMapping(const Mapping& mapping)
	{ return MemoryTranslation(mapping.src, mapping.dest, mapping.size); }

237
238
	bool
	pathCheck(const MemoryGraph::Path& path);
239
240
241

private:
	/// Directed graph that stores address spaces and memory mappings
242
	MemoryGraph memoryGraph;
243
244
245
246
247
248
249

	/// Cache mapping of names to address space ids for fast lookup
	std::map<std::string, AddressSpaceId> addrSpaceLookup;

	/// Logger for universal access in this class
	SpdLogger logger;

250
251
	MemoryGraph::check_path_fn pathCheckFunc;

252
	/// Static pointer to global instance, because this is a singleton
253
	static MemoryManager* instance;
254
255
256
};

} // namespace villas