-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathaddr_translation.h
More file actions
103 lines (85 loc) · 3.77 KB
/
addr_translation.h
File metadata and controls
103 lines (85 loc) · 3.77 KB
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
#ifndef _LIND_ADDR_TRANSLATION_H
#define _LIND_ADDR_TRANSLATION_H
#include <stdint.h>
#include <stddef.h>
#include <errno.h>
// When we want the argument to be translated, the cageid supplied must have the MSB set to 1.
// These masks help check this.
#define LIND_ARG_TRANSLATE_FLAG (1ULL << 63)
#define LIND_ARG_CAGEID_MASK (~LIND_ARG_TRANSLATE_FLAG)
#ifdef __cplusplus
extern "C"
{
#endif
// Imported host function to get the base address of the current cage's
// linear memory Module: "lind", name: "lind-get-memory-base"
unsigned long long __imported_lind_get_memory_base (void)
__attribute__ ((__import_module__ ("lind"),
__import_name__ ("lind-get-memory-base")));
// Imported host function to get the current cage id (pid)
// Module: "lind", name: "lind-get-cage-id"
unsigned long long __imported_lind_get_cage_id (void)
__attribute__ ((__import_module__ ("lind"),
__import_name__ ("lind-get-cage-id")));
// Cached base address for this process (cage). Set once per instance.
extern uint64_t __lind_base;
// Cached cage id (pid) for this process (cage). Set once per instance.
extern uint64_t __lind_cageid;
// Initialize address translation (idempotent). Queries base from host once.
void __lind_init_addr_translation (void);
// Translate a guest pointer (offset in wasm32 linear memory) to a host
// pointer (u64)
static inline uint64_t
__lind_translate_ptr_to_host (const void *p)
{
if (p == NULL)
return 0ULL;
// Cast pointer value as an offset within the linear memory and add base
return __lind_base + (uint64_t) (uintptr_t) p;
}
static inline uint64_t
__lind_translate_uaddr_to_host (const uint64_t uaddr, const uint64_t cageid)
{
uint64_t __cageid = cageid & LIND_ARG_CAGEID_MASK;
if (__cageid == __lind_cageid && ((cageid & LIND_ARG_TRANSLATE_FLAG) != 0))
return __lind_base + uaddr;
return uaddr;
}
// Convenience macro for call sites
#define TRANSLATE_GUEST_POINTER_TO_HOST(p) \
__lind_translate_ptr_to_host ((const void *) (p))
// Converts (uaddr, cageid) pair to host address.
// Useful when address space (cage vs host) is ambigious.
//
// This is called by copy data where the arguments are already addresses
// so we implicitly update the cageid argument before passing it to the helper.
#define TRANSLATE_UADDR_TO_HOST(uaddr, cageid) \
__lind_translate_uaddr_to_host ((uaddr), (cageid | LIND_ARG_TRANSLATE_FLAG)), (cageid)
// This is used by make_threei_call, we do not modify the flag before checking if translation
// is needed. We do modify the cage on output so that other threei/lind calls see a correct cageid
#define TRANSLATE_ARG_TO_HOST(uaddr, cageid) \
__lind_translate_uaddr_to_host ((uaddr), (cageid)), (cageid & LIND_ARG_CAGEID_MASK)
/* Translate an array of guest iovec structures to host layout.
Each iov_base is a wasm32 guest pointer; we split the translated
64-bit host address across iov_base (low32) and __padding1 (high32).
Requires <sys/uio.h> (or <bits/types/struct_iovec.h>) for struct iovec. */
#ifdef __iovec_defined
static inline void
__lind_translate_iov (const struct iovec *guest_iov,
struct iovec *host_iov, int iovcnt)
{
for (int i = 0; i < iovcnt; ++i)
{
host_iov[i].iov_len = guest_iov[i].iov_len;
uint32_t g = (uint32_t)(uintptr_t) guest_iov[i].iov_base;
uint64_t h = __lind_translate_ptr_to_host ((const void *)(uintptr_t) g);
host_iov[i].iov_base = (void *)(uintptr_t)(uint32_t)(h & 0xFFFFFFFFULL);
host_iov[i].__padding1 = (int)(uint32_t)(h >> 32);
host_iov[i].__padding2 = 0;
}
}
#endif
#ifdef __cplusplus
}
#endif
#endif // _LIND_ADDR_TRANSLATION_H