55#include <stddef.h>
66#include <errno.h>
77
8- // When we want the argument to be translated, the cageid supplied must have the MSB set to 1.
9- // These masks help check this.
8+ // With the new semantics, a cageid with its MSB set indicates that the
9+ // associated argument should be translated. These helpers manage that flag.
10+ //
11+ // Flag indicating the argument requires translation (MSB set).
1012#define LIND_ARG_TRANSLATE_FLAG (1ULL << 63)
13+
14+ // Mask to extract the actual cageid (clear the translation flag).
1115#define LIND_ARG_CAGEID_MASK (~LIND_ARG_TRANSLATE_FLAG)
1216
1317#ifdef __cplusplus
@@ -50,8 +54,12 @@ extern "C"
5054 static inline uint64_t
5155 __lind_translate_uaddr_to_host (const uint64_t uaddr , const uint64_t cageid )
5256 {
57+ // Extract actual cageid (without the translation flag)
5358 uint64_t __cageid = cageid & LIND_ARG_CAGEID_MASK ;
54-
59+
60+ // Translate only if:
61+ // 1. The argument originates from the current cage, and
62+ // 2. MSB of cageid is set.
5563 if (__cageid == __lind_cageid && ((cageid & LIND_ARG_TRANSLATE_FLAG ) != 0 ))
5664 return __lind_base + uaddr ;
5765
@@ -62,18 +70,24 @@ extern "C"
6270#define TRANSLATE_GUEST_POINTER_TO_HOST (p ) \
6371 __lind_translate_ptr_to_host ((const void *) (p))
6472
65- // Converts (uaddr, cageid) pair to host address.
66- // Useful when address space (cage vs host) is ambigious.
73+ // Convert a (uaddr, cageid) pair to (host_addr, cageid).
6774//
68- // This is called by copy data where the arguments are already addresses
69- // so we implicitly update the cageid argument before passing it to the helper.
75+ // Used by copy_data_between_cages where arguments are already addresses. This macro
76+ // sets the translation flag before invoking the helper.
77+ //
78+ // Input: (uaddr, cageid)
79+ // Output: (host_addr, cageid)
7080#define TRANSLATE_UADDR_TO_HOST (uaddr , cageid ) \
71- __lind_translate_uaddr_to_host ((uaddr), (cageid | LIND_ARG_TRANSLATE_FLAG)), (cageid)
81+ __lind_translate_uaddr_to_host ((uaddr), (( cageid) | LIND_ARG_TRANSLATE_FLAG)), (cageid)
7282
73- // This is used by make_threei_call, we do not modify the flag before checking if translation
74- // is needed. We do modify the cage on output so that other threei/lind calls see a correct cageid
83+ // Used by make_threei_call to translate arguments if required.
84+ //
85+ // Translation occurs only when the MSB of cageid is set.
86+ //
87+ // Input: (uaddr, cageid)
88+ // Output: (host_addr, actual_cageid)
7589#define TRANSLATE_ARG_TO_HOST (uaddr , cageid ) \
76- __lind_translate_uaddr_to_host ((uaddr), (cageid)), (cageid & LIND_ARG_CAGEID_MASK)
90+ __lind_translate_uaddr_to_host ((uaddr), (cageid)), (( cageid) & LIND_ARG_CAGEID_MASK)
7791
7892/* Translate an array of guest iovec structures to host layout.
7993 Each iov_base is a wasm32 guest pointer; we split the translated
0 commit comments