TEMU  3.0
The Terma Emulator
Memory.h
Go to the documentation of this file.
1 //===------------------------------------------------------------*- C++ -*-===//
2 //
3 // TEMU: The Terma Emulator
4 // (c) Terma 2015
5 // Authors: Mattias Holm <maho (at) terma.com>
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef TEMU_MEMORY_IF_H
10 #define TEMU_MEMORY_IF_H
11 
12 #include "temu-c/Support/Memory.h"
13 #include "temu-c/Support/Objsys.h"
14 #include <stdbool.h>
15 #include <stdint.h>
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 typedef enum {
26 typedef enum {
31 // Memory transaction flags
32 #define TEMU_MT_CACHEABLE 1
33 #define TEMU_MT_BYPASS_CACHE (1 << 1)
34 #define TEMU_MT_FORCE_CACHE_MISS (1 << 2)
35 #define TEMU_MT_FAILED (1 << 3)
36 #define TEMU_MT_CACHE_HIT (1 << 4)
37 
38 #define TEMU_MT_PRIV_SHIFT (5)
39 
40 #define TEMU_MT_PRIV_MASK (7 << 5)
41 #define TEMU_MT_PRIV_USER (0 << 5)
42 #define TEMU_MT_PRIV_SUPER (1 << 5)
43 #define TEMU_MT_PRIV_HYPER (2 << 5)
44 // Note that we reserve an extra privilege level bit for future extension
45 
46 // Probing memory transaction, this is used to probe the memory system without
47 // actually triggering any faults
48 #define TEMU_MT_PROBE (1 << 8)
49 #define TEMU_MT_SELF_MODIFYING (1 << 9)
50 
51 // Bit 10 is used to indicate the endianness of the transaction.
52 // The memory space will automatically swap the bytes when it arrives at a
53 // device that is mapped as the opposite endianness. Note that swapping is done
54 // when arriving at the end point device, not when passing through nested memory
55 // spaces. Swapping is only done for small transactions, larger transactions
56 // must be swapped by the the target device itself. For large transactions we
57 // also need a "target endianness" / "host endianness".
58 
59 // Small transactions (i.e. those where the payload fit in Value):
60 // Always in host endianness when initiating, but have a "virtual endianness".
61 // Memory space swaps the endianness automatically.
62 // Large transactions:
63 // The value actually contains a pointer to a datablock
64 // The datablock may be in host endianness, or in target endianness
65 // Used for for example DMA transfers or transfers of bytes in the sim (image loading etc)
66 // Usually:
67 // - Data transfer in bytes of target endianness (image loading)
68 // - Transfers of memory blocks of words which the sim need to interpret (send lists)
69 // - Transfers of memory blocks of words of data
70 #define TEMU_MT_ENDIAN_BIT 10
71 #define TEMU_MT_BIG_ENDIAN (0 << 10)
72 #define TEMU_MT_LITTLE_ENDIAN (1 << 10)
73 static inline bool
74 temu_hasDifferentEndianness(uint32_t a, uint32_t b)
75 {
76  return ((a ^ b) >> 10) & 1;
77 }
78 
79 #define TEMU_MT_ISA_0 (0 << 11)
80 #define TEMU_MT_ISA_1 (1 << 11)
81 #define TEMU_MT_ISA_2 (2 << 11)
82 
83 #define TEMU_MT_GET_ISA(flags) ((flags >> 11) & 7)
84 
85 #define TEMU_MT_PROF_ACCESS (1 << 13)
86 #define TEMU_MT_PROF_ACCESS_BIT 13
87 
88 // Bypass builtin protections (e.g. write protections for ROM memories)
89 // This simplifies the use of large transactions for implementing ROM loading functionality
90 #define TEMU_MT_BYPASS_PROT (1 << 14)
91 
92 
103 typedef struct temu_MemTransaction {
104  uint64_t Va;
105  uint64_t Pa;
106 
110  uint64_t Value;
111 
137  uint64_t Size;
138 
143  uint64_t Offset;
144 
166  void *Page;
167  uint64_t Cycles;
168  uint32_t Flags;
169 
170  void *IR;
173 
174 // Fetch capabilities
175 #define TEMU_MEM_ACCESS_CAP_F8 (1 << 0)
176 #define TEMU_MEM_ACCESS_CAP_F16 (1 << 1)
177 #define TEMU_MEM_ACCESS_CAP_F32 (1 << 2)
178 #define TEMU_MEM_ACCESS_CAP_F64 (1 << 3)
179 #define TEMU_MEM_ACCESS_CAP_F_ALL (0xf)
180 
181 // Read
182 #define TEMU_MEM_ACCESS_CAP_R8 (1 << (0 + 4))
183 #define TEMU_MEM_ACCESS_CAP_R16 (1 << (1 + 4))
184 #define TEMU_MEM_ACCESS_CAP_R32 (1 << (2 + 4))
185 #define TEMU_MEM_ACCESS_CAP_R64 (1 << (3 + 4))
186 #define TEMU_MEM_ACCESS_CAP_R_ALL (0xf << 4)
187 
188 // Write
189 #define TEMU_MEM_ACCESS_CAP_W8 (1 << (0 + 8))
190 #define TEMU_MEM_ACCESS_CAP_W16 (1 << (1 + 8))
191 #define TEMU_MEM_ACCESS_CAP_W32 (1 << (2 + 8))
192 #define TEMU_MEM_ACCESS_CAP_W64 (1 << (3 + 8))
193 #define TEMU_MEM_ACCESS_CAP_W_ALL (0xf << 8)
194 
195 // Exchange
196 #define TEMU_MEM_ACCESS_CAP_E8 (1 << (0 + 12))
197 #define TEMU_MEM_ACCESS_CAP_E16 (1 << (1 + 12))
198 #define TEMU_MEM_ACCESS_CAP_E32 (1 << (2 + 12))
199 #define TEMU_MEM_ACCESS_CAP_E64 (1 << (3 + 12))
200 #define TEMU_MEM_ACCESS_CAP_E_ALL (0xf << 12)
201 
202 typedef struct {
204  uint16_t
209 
215  void (*fetch)(void *Obj, temu_MemTransaction *Mt);
216 
218  void (*read)(void *Obj, temu_MemTransaction *Mt);
219 
221  void (*write)(void *Obj, temu_MemTransaction *Mt);
222 
226  void (*exchange)(void *Obj, temu_MemTransaction *Mt);
227 
229  void (*mapped)(void *Obj, uint64_t Pa, uint64_t Len);
230 
237  const temu_MemAccessCapabilities *(*getCapabilities)(void *Obj);
238 };
239 #define TEMU_MEM_ACCESS_IFACE_TYPE "MemAccessIface"
240 TEMU_IFACE_REFERENCE_TYPE(temu_MemAccess);
241 
260 
261 typedef struct temu_MemoryIface {
262  int (*readBytes)(void *Obj, void *Dest, uint64_t Offs, uint32_t Size,
263  int Swap);
264  int (*writeBytes)(void *Obj, uint64_t Offs, uint32_t Size, const void *Src,
265  int Swap);
267 #define TEMU_MEMORY_IFACE_TYPE "MemoryIface"
268 TEMU_IFACE_REFERENCE_TYPE(temu_Memory);
269 
279 int temu_mapMemorySpace(void *Obj, uint64_t Addr, uint64_t Len,
280  temu_Object_ *MemObj);
281 
292 int temu_mapMemorySpaceFlags(void *Obj, uint64_t Addr, uint64_t Len,
293  temu_Object_ *MemObj, uint32_t Flags);
294 
303 void temu_setMemAttr(void *Obj, uint64_t Addr, uint64_t Len,
304  temu_MemoryAttr Attr);
305 
313 void temu_clearMemAttr(void *Obj, uint64_t Addr, uint64_t Len,
314  temu_MemoryAttr Attr);
315 
316 #ifdef __cplusplus
317 }
318 #endif
319 
320 #endif /* ! TEMU_MEMORY_IF_H */
void temu_setMemAttr(void *Obj, uint64_t Addr, uint64_t Len, temu_MemoryAttr Attr)
void(* write)(void *Obj, temu_MemTransaction *Mt)
Function called on writes.
Definition: Memory.h:221
int(* writeBytes)(void *Obj, uint64_t Offs, uint32_t Size, const void *Src, int Swap)
Definition: Memory.h:264
void * IR
Definition: Memory.h:170
Memory transaction initiated by CPU.
Definition: Memory.h:22
int temu_mapMemorySpaceFlags(void *Obj, uint64_t Addr, uint64_t Len, temu_Object_ *MemObj, uint32_t Flags)
void(* mapped)(void *Obj, uint64_t Pa, uint64_t Len)
Optional method, called when interface is mapped.
Definition: Memory.h:229
void temu_clearMemAttr(void *Obj, uint64_t Addr, uint64_t Len, temu_MemoryAttr Attr)
temu_MemoryEndianness
Definition: Memory.h:26
void(* read)(void *Obj, temu_MemTransaction *Mt)
Function called on reads.
Definition: Memory.h:218
temu_MemoryAttr
Memory attribute enumeration.
Definition: Memory.h:40
uint16_t LargeTransactions
Supports large transactions (e.g. RAM/ROM)
Definition: Memory.h:205
Memory transaction initiator unknown.
Definition: Memory.h:24
Memory access interface in little endian.
Definition: Memory.h:28
Definition: Memory.h:202
Definition: Memory.h:212
Definition: Memory.h:103
uint64_t Pa
64 bit physical address
Definition: Memory.h:105
uint32_t Flags
Flags for use in the memory hierarchy.
Definition: Memory.h:168
uint64_t Offset
Definition: Memory.h:143
struct temu_MemoryIface temu_MemoryIface
temu_InitiatorType InitiatorType
Definition: Memory.h:150
Memory access interface in undefined endian.
Definition: Memory.h:29
uint64_t Size
Definition: Memory.h:137
#define temu_Object_
Definition: Temu2Compat.h:13
temu_Object_ * Initiator
Definition: Memory.h:159
uint64_t Va
64 bit virtual for unified 32/64 bit interface.
Definition: Memory.h:104
temu_MemoryEndianness Endianness
Endianess of transaction interface.
Definition: Memory.h:207
Definition: Memory.h:261
void(* fetch)(void *Obj, temu_MemTransaction *Mt)
Definition: Memory.h:215
uint16_t TransactionBaseSizes
Flags indicating legal transaction sizes.
Definition: Memory.h:203
uint64_t Value
Definition: Memory.h:110
struct temu_MemTransaction temu_MemTransaction
TEMU_IFACE_REFERENCE_TYPE(temu_MemAccess)
temu_InitiatorType
Definition: Memory.h:21
Memory transaction initiated by device.
Definition: Memory.h:23
temu_MemoryKind
Definition: Memory.h:20
temu_MemoryKind Kind
Mapping type.
Definition: Memory.h:206
int temu_mapMemorySpace(void *Obj, uint64_t Addr, uint64_t Len, temu_Object_ *MemObj)
Memory access interface in big endian.
Definition: Memory.h:27
int(* readBytes)(void *Obj, void *Dest, uint64_t Offs, uint32_t Size, int Swap)
Definition: Memory.h:262
void * Page
Definition: Memory.h:166
uint64_t Cycles
Cycle cost for memory access (initialised to 0).
Definition: Memory.h:167
void(* exchange)(void *Obj, temu_MemTransaction *Mt)
Definition: Memory.h:226