Talvos  0.1
SPIR-V interpreter and dynamic analysis framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Memory.h
Go to the documentation of this file.
1 // Copyright (c) 2018 the Talvos developers. All rights reserved.
2 //
3 // This file is distributed under a three-clause BSD license. For full license
4 // terms please see the LICENSE file distributed with this source code.
5 
8 
9 #ifndef TALVOS_MEMORY_H
10 #define TALVOS_MEMORY_H
11 
12 #include <cstdint>
13 #include <cstring>
14 #include <mutex>
15 #include <vector>
16 
17 namespace talvos
18 {
19 
20 class Device;
21 
23 enum class MemoryScope
24 {
25  Device,
26  Workgroup,
28 };
29 
37 class Memory
38 {
39 public:
42 
43  ~Memory();
44 
45  // Do not allow Memory objects to be copied.
47  Memory(const Memory &) = delete;
48  Memory &operator=(const Memory &) = delete;
50 
53  uint64_t allocate(uint64_t NumBytes);
54 
57  template <typename T>
58  T atomic(uint64_t Address, uint32_t Opcode, uint32_t Scope,
59  uint32_t Semantics, T Value = 0);
60 
63  uint32_t atomicCmpXchg(uint64_t Address, uint32_t Scope,
64  uint32_t EqualSemantics, uint32_t UnequalSemantics,
65  uint32_t Value, uint32_t Comparator);
66 
68  void dump() const;
69 
71  void dump(uint64_t Address) const;
72 
74  MemoryScope getScope() const { return Scope; }
75 
77  void load(uint8_t *Result, uint64_t Address, uint64_t NumBytes) const;
78 
80  uint8_t *map(uint64_t Base, uint64_t Offset, uint64_t NumBytes);
81 
83  void release(uint64_t Address);
84 
86  void store(uint64_t Address, uint64_t NumBytes, const uint8_t *Data);
87 
89  void unmap(uint64_t Base);
90 
93  static void copy(uint64_t DstAddress, Memory &DstMem, uint64_t SrcAddress,
94  const Memory &SrcMem, uint64_t NumBytes);
95 
97  static const char *scopeToString(MemoryScope Scope)
98  {
99  switch (Scope)
100  {
101  case MemoryScope::Device:
102  return "Device";
104  return "Workgroup";
106  return "Invocation";
107  default:
108  return "<invalid>";
109  }
110  }
111 
112 private:
114 
116 
117  std::mutex Mutex;
118 
120  static const uint32_t NUM_ATOMIC_MUTEXES = 100;
121 
124 
126  struct Buffer
127  {
128  uint64_t NumBytes;
129  uint8_t *Data;
130  };
131  std::vector<Buffer> Buffers;
132  std::vector<uint64_t> FreeBuffers;
133 
135  bool isAccessValid(uint64_t Address, uint64_t NumBytes) const;
136 };
137 
138 } // namespace talvos
139 
140 #endif
uint8_t * map(uint64_t Base, uint64_t Offset, uint64_t NumBytes)
Map a region of memory and return a pointer to it.
Definition: Memory.cpp:273
void unmap(uint64_t Base)
Unmap a previously mapped region of memory.
Definition: Memory.cpp:326
void dump() const
Dump the entire contents of this memory to stdout.
Definition: Memory.cpp:202
std::mutex AtomicMutexes[NUM_ATOMIC_MUTEXES]
Set of mutexes for synchronizing atomic operations.
Definition: Memory.h:123
static void copy(uint64_t DstAddress, Memory &DstMem, uint64_t SrcAddress, const Memory &SrcMem, uint64_t NumBytes)
Copy data between memory instances.
Definition: Memory.cpp:328
uint32_t atomicCmpXchg(uint64_t Address, uint32_t Scope, uint32_t EqualSemantics, uint32_t UnequalSemantics, uint32_t Value, uint32_t Comparator)
Perform an atomic compare-exchange operation at Address.
Definition: Memory.cpp:156
MemoryScope
Describes the scope of a memory instance.
Definition: Memory.h:23
void release(uint64_t Address)
Release the allocation with base address Address.
Definition: Memory.cpp:292
uint64_t NumBytes
The size of the allocation in bytes.
Definition: Memory.h:128
Memory & operator=(const Memory &)=delete
This class represents a single execution of a SPIR-V entry point.
Definition: Invocation.h:33
T atomic(uint64_t Address, uint32_t Opcode, uint32_t Scope, uint32_t Semantics, T Value=0)
Atomically apply operation defined by Opcode to Address.
Definition: Memory.cpp:81
This class represents an address space in the virtual device.
Definition: Memory.h:37
MemoryScope getScope() const
Get the scope of this memory instance.
Definition: Memory.h:74
std::vector< uint64_t > FreeBuffers
Base addresses available for reuse.
Definition: Memory.h:132
std::mutex Mutex
Mutex for guarding allocate/release operations.
Definition: Memory.h:117
A Device instance encapsulates properties and state for the virtual device.
Definition: Device.h:29
void load(uint8_t *Result, uint64_t Address, uint64_t NumBytes) const
Load NumBytes of data from Address into Result.
Definition: Memory.cpp:249
MemoryScope Scope
The scope of this memory instance.
Definition: Memory.h:115
uint64_t allocate(uint64_t NumBytes)
Allocate a new buffer of size NumBytes.
Definition: Memory.cpp:52
void store(uint64_t Address, uint64_t NumBytes, const uint8_t *Data)
Store NumBytes of data from Data to Address.
Definition: Memory.cpp:306
An allocation within this memory instance.
Definition: Memory.h:126
static const char * scopeToString(MemoryScope Scope)
Returns the string representation of Scope.
Definition: Memory.h:97
Memory(Device &D, MemoryScope Scope)
Create a new Memory instance.
Definition: Memory.cpp:39
bool isAccessValid(uint64_t Address, uint64_t NumBytes) const
Check whether an access resides in an allocated region of memory.
Definition: Memory.cpp:236
static const uint32_t NUM_ATOMIC_MUTEXES
Number of mutexes to use for synchronizing atomic operations.
Definition: Memory.h:120
uint8_t * Data
The raw data backing the allocation.
Definition: Memory.h:129
std::vector< Buffer > Buffers
List of allocations.
Definition: Memory.h:131
Device & Dev
The device this memory instance is part of.
Definition: Memory.h:113