2 * Copyright 2008, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * NaCl Service Runtime. I/O Descriptor / Handle abstraction. Memory
34 * mapping using descriptors.
37 #ifndef NATIVE_CLIENT_SERVICE_RUNTIME_NACL_HOST_DESC_H__
38 #define NATIVE_CLIENT_SERVICE_RUNTIME_NACL_HOST_DESC_H__
42 #include "native_client/include/portability.h"
43 #include "native_client/include/nacl_platform.h"
44 #include "native_client/include/nacl_base.h"
46 #include "native_client/service_runtime/nacl_sync.h"
48 #include "native_client/service_runtime/include/bits/mman.h"
50 #if NACL_LINUX || NACL_OSX
51 # include "native_client/service_runtime/linux/nacl_host_desc_types.h"
53 # include "native_client/service_runtime/win/nacl_host_desc_types.h"
57 * see NACL_MAP_PAGESIZE from nacl_config.h; map operations must be aligned
64 static INLINE
int NaClIsNegErrno(int val
)
67 * On 64-bit Linux, the app has the entire 32-bit address space
68 * (kernel no longer occupies the top 1G), so what is an errno and
69 * what is an address is trickier: we require that our NACL_ABI_
70 * errno values be at most 64K.
72 return ((unsigned) val
>= 0xffff0000u
);
75 extern int NaClXlateErrno(int errnum
);
77 extern int NaClXlateNaClSyncStatus(NaClSyncStatus status
);
80 * Mapping data from a file.
82 * start_addr and len must be multiples of NACL_MAP_PAGESIZE.
84 * Of prot bits, only PROT_READ and PROT_WRITE are allowed. Of flags,
85 * only MAP_ANONYMOUS is allowed. start_addr must be specified, and
86 * this code will add in MAP_FIXED. start_address, len, and offset
87 * must be a multiple of NACL_MAP_PAGESIZE.
89 * Note that in Windows, in order for the mapping to be coherent, the
90 * mapping must arise from the same mapping handle and just using the
91 * same file handle won't ensure coherence. If the file mapping
92 * object were created and destroyed inside of NaClHostDescMap, there
93 * would never be any chance at coherence. One alternative is to
94 * create a file mapping object for each mapping mode. Native
95 * descriptors are then shareable, but only when the mode matches (!).
96 * A read-only shared mapping not seeing the changes made by a
97 * read-write mapping seem rather ugly.
99 * Instead of this ugliness, we just say that a map operation cannot
100 * request MAP_SHARED. Anonymous mappings ignore the descriptor
103 * Underlying host-OS syscalls: mmap / MapViewOfFileEx
106 extern int NaClHostDescMap(struct NaClHostDesc
*d
,
111 off_t offset
); /* 4GB file max */
114 * Undo a file mapping. The memory range specified by start_address,
115 * len must be memory that came from NaClHostDescMap.
117 * start_addr and len must be multiples of NACL_MAP_PAGESIZE.
119 * Underlying host-OS syscalls: mmap / UnmapViewOfFile/VirtualAlloc
121 extern int NaClHostDescUnmap(void *start_addr
,
125 * Undo a file mapping. The memory range specified by start_address,
126 * len must be memory that came from NaClHostDescMap.
128 * start_addr and len must be multiples of NACL_MAP_PAGESIZE.
130 * Underlying host-OS syscalls: munmap / UnmapViewOfFile
132 extern int NaClHostDescUnmapUnsafe(void *start_addr
,
136 * Constructor for a NaClHostDesc object.
138 * path should be a host-OS pathname to a file. No validation is
139 * done. mode should contain one of O_RDONLY, O_WRONLY, and O_RDWR,
140 * and can additionally contain O_CREAT, O_TRUNC, and O_APPEND.
142 * Uses raw syscall return convention, so returns 0 for success and
143 * non-zero (usually -NACL_ABI_EINVAL) for failure.
145 * We cannot return the platform descriptor/handle and be consistent
146 * with a largely POSIX-ish interface, since e.g. windows handles may
147 * be negative and might look like negative errno returns. Currently
148 * we use the posix API on windows, so it could work, but we may need
151 * Underlying host-OS functions: open / _s_open_s
153 extern int NaClHostDescOpen(struct NaClHostDesc
*d
,
159 * Constructor for a NaClHostDesc object.
161 * Uses raw syscall return convention, so returns 0 for success and
162 * non-zero (usually -NACL_ABI_EINVAL) for failure.
164 * d is a POSIX-interface descriptor
166 * mode may only contain one of NACL_ABI_O_RDONLY, NACL_ABI_O_WRONLY,
167 * or NACL_ABI_O_RDWR, and must be the actual mode that d was opened
168 * with. Note that these are host OS access mode bits.
170 * Underlying host-OS functions: dup / _dup; mode is what posix_d was
173 extern int NaClHostDescPosixDup(struct NaClHostDesc
*d
,
178 * Essentially the same as NaClHostDescPosixDup, but without the dup
179 * -- takes ownership of the descriptor rather than making a dup.
181 extern int NaClHostDescPosixTake(struct NaClHostDesc
*d
,
187 * Allocates a NaClHostDesc and invokes NaClHostDescPosixTake on it.
188 * Aborts process if no memory.
190 extern struct NaClHostDesc
*NaClHostDescPosixMake(int posix_d
,
193 * Read data from an opened file into a memory buffer.
195 * buf is not validated.
197 * Underlying host-OS functions: read / _read
199 extern ssize_t
NaClHostDescRead(struct NaClHostDesc
*d
,
205 * Write data from a memory buffer into an opened file.
207 * buf is not validated.
209 * Underlying host-OS functions: write / _write
211 extern ssize_t
NaClHostDescWrite(struct NaClHostDesc
*d
,
215 extern int NaClHostDescSeek(struct NaClHostDesc
*d
,
220 * TODO: Need to enumerate which request is supported and the
221 * size of the argument, as well as whether the arg is input or
224 extern int NaClHostDescIoctl(struct NaClHostDesc
*d
,
231 extern int NaClHostDescFstat(struct NaClHostDesc
*d
,
232 struct nacl_abi_stat
*nasp
);
235 * Dtor for the NaClHostFile object. Close the file.
237 * Underlying host-OS functions: close(2) / _close
239 extern int NaClHostDescClose(struct NaClHostDesc
*d
);
241 extern int NaClHostDescStat(char const *host_os_pathname
,
242 struct nacl_abi_stat
*nasp
);
245 * should DCE away when unused.
247 static INLINE
int NaClProtMap(int abi_prot
)
253 if (0 != (abi_prot & NACL_ABI_ ## H)) { \
261 # error "NaClProtMap: PROT_NONE is not zero -- are mprotect flags bit values?"