1 /*
2  * Copyright (c) 2011 The Native Client Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6 
7 #ifndef NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_NACL_TEXT_H_
8 #define NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_NACL_TEXT_H_
9 
10 #include "native_client/src/include/portability.h"
11 #include "native_client/src/include/nacl_compiler_annotations.h"
12 #include "native_client/src/trusted/service_runtime/nacl_error_code.h"
13 
14 EXTERN_C_BEGIN
15 
16 struct NaClApp;
17 struct NaClAppThread;
18 
19 /*
20  * If/when we decide to support munmap of validated text regions
21  * into the dynamic code region, we will need another bit per page
22  * to know whether a page is backed by shm or by an mmapped file --
23  * i.e., if it was a file, we will need to restore the shm mapping
24  * (this is costly, so should be avoided if it's unnecessary;
25  * additionally, if it wasn't, the munmap should fail, and
26  * conversely, dyncode_delete should fail on an mmapped region).
27  */
28 
29 struct NaClDynamicRegion {
30   uintptr_t start;
31   size_t size;
32   int delete_generation;
33   int is_mmap;  /* cannot be deleted (for now) */
34 };
35 
36 /*
37  * Insert a new region into nap->dynamic regions, maintaining the sorted
38  * ordering. Returns 1 on success, 0 if there is a conflicting region
39  * Caller must hold nap->dynamic_load_mutex.
40  * Invalidates all previous NaClDynamicRegion pointers.
41  *
42  * is_mmap is 1 if the region is backed by a memory mapped file (and thus
43  * the shared memory view was unmapped), 0 otherwise.
44  */
45 int NaClDynamicRegionCreate(struct NaClApp *nap,
46                             uintptr_t start,
47                             size_t size,
48                             int is_mmap);
49 /*
50  * Find the last region overlapping with the given memory range, return 0 if
51  * region is unused.
52  * caller must hold nap->dynamic_load_mutex, and must discard result
53  * when lock is released.
54  */
55 struct NaClDynamicRegion* NaClDynamicRegionFind(struct NaClApp *nap,
56                                                 uintptr_t ptr,
57                                                 size_t size);
58 
59 /*
60  * Delete a region from nap->dynamic_regions, maintaining the sorted ordering
61  * Caller must hold nap->dynamic_load_mutex.
62  * Invalidates all previous NaClDynamicRegion pointers.
63  */
64 void NaClDynamicRegionDelete(struct NaClApp *nap, struct NaClDynamicRegion* r);
65 
66 struct NaClValidationMetadata;
67 
68 /*
69  * Create a shared memory descriptor and map it into the text region
70  * of the address space.  This implies that the text size must be a
71  * multiple of NACL_MAP_PAGESIZE.
72  */
73 NaClErrorCode NaClMakeDynamicTextShared(struct NaClApp *nap) NACL_WUR;
74 
75 struct NaClDescEffectorShm;
76 int NaClDescEffectorShmCtor(struct NaClDescEffectorShm *self) NACL_WUR;
77 
78 int NaClMinimumThreadGeneration(struct NaClApp *nap);
79 
80 int32_t NaClTextDyncodeCreate(
81     struct NaClApp *nap,
82     uint32_t       dest,
83     void           *code_copy,
84     uint32_t       size,
85     const struct NaClValidationMetadata *metadata) NACL_WUR;
86 
87 int32_t NaClSysDyncodeCreate(struct NaClAppThread *natp,
88                              uint32_t             dest,
89                              uint32_t             src,
90                              uint32_t             size) NACL_WUR;
91 
92 int32_t NaClSysDyncodeModify(struct NaClAppThread *natp,
93                              uint32_t             dest,
94                              uint32_t             src,
95                              uint32_t             size) NACL_WUR;
96 
97 int32_t NaClSysDyncodeDelete(struct NaClAppThread *natp,
98                              uint32_t             dest,
99                              uint32_t             size) NACL_WUR;
100 
101 void NaClDyncodeVisit(
102     struct NaClApp *nap,
103     void           (*fn)(void *state, struct NaClDynamicRegion *region),
104     void           *state);
105 
106 EXTERN_C_END
107 
108 #endif
109