Debugging: Add code to print backtrace for guest on SIGSEGV
[nativeclient.git] / intermodule_comm / nacl_htp.h
blob505b9d2b798cb7ccfc6df50d5d86ef30fa566fa3
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
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
14 * distribution.
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 Handle Transfer Protocol
35 // The NaCl sel_ldr uses the NaCl Handle Transfer Protocol to send/receive
36 // messages between NaCl modules. This module provides the utility functions
37 // to communicate with a NaCl module from the trusted code.
39 #ifndef NATIVE_CLIENT_INTERMODULE_COMM_NACL_HTP_H_
40 #define NATIVE_CLIENT_INTERMODULE_COMM_NACL_HTP_H_
43 #include <stdlib.h>
44 #ifdef __native_client__
45 #include <unistd.h>
46 #include <nacl/nacl_imc.h>
47 #else
48 #include "native_client/intermodule_comm/nacl_imc.h"
49 #endif // __native_client__
51 #ifdef __native_client__
53 namespace nacl {
55 typedef Handle HtpHandle;
56 typedef MessageHeader HtpHeader;
58 // Creates a host I/O descriptor from the handle.
59 inline HtpHandle CreateIoDesc(Handle handle) {
60 return handle;
63 // Creates a shared memory descriptor from the handle.
64 inline HtpHandle CreateShmDesc(Handle handle, off_t length) {
65 return handle;
68 // Creates an IMC memory descriptor from the handle.
69 inline HtpHandle CreateImcDesc(Handle handle) {
70 return handle;
73 // Reads up to count bytes from the handle into buffer.
74 inline int Read(HtpHandle handle, void* buffer, size_t count) {
75 return read(handle, buffer, count);
78 // Writes up to count bytes to the handle from buffer.
79 inline int Write(HtpHandle handle, const void* buffer, size_t count) {
80 return write(handle, buffer, count);
83 const HtpHandle kInvalidHtpHandle = kInvalidHandle;
85 } // namespace nacl
87 #else // __native_client__
89 struct NaClDesc;
91 namespace nacl {
93 // NaCl resource descriptor type compatible with sel_ldr.
94 typedef struct NaClDesc* HtpHandle;
96 // Message header used by SendDatagram() and ReceiveDatagram().
97 // Note the member layout is compatible with NaClImcTypedMsgHdr{}.
98 struct HtpHeader {
99 IOVec* iov; // scatter/gather array
100 size_t iov_length; // number of elements in iov
101 HtpHandle* handles; // array of handles to be transferred
102 size_t handle_count; // number of handles in handles
103 int flags;
106 // Sends a message on a socket. Except for the fact that HtpHeader is used,
107 // the runtime behavior of this function is the same as SendDatagram() for
108 // MessageHeader.
109 // On success, SendDatagram() returns the number of bytes sent.
110 int SendDatagram(HtpHandle socket, const HtpHeader* message, int flags);
112 // Receives a message from a socket. Except for the fact that HtpHeader is
113 // used, the runtime behavior of this function is the same as ReceiveDatagram()
114 // for MessageHeader.
115 // On success, ReceiveDatagram() returns the number of bytes received.
116 int ReceiveDatagram(HtpHandle socket, HtpHeader* message, int flags);
118 // Closes a sel_ldr compatible NaCl descriptor.
119 int Close(HtpHandle handle);
121 // Maps the specified memory object into the process address space.
122 // Map() returns a pointer to the mapped area, or kMapFailed upon error.
123 // For prot, the bitwise OR of the kProt* bits must be specified.
124 // For flags, either kMapShared or kMapPrivate must be specified. If kMapFixed
125 // is also set, Map() tries to map the memory object at the address specified by
126 // start.
127 void* Map(void* start, size_t length, int prot, int flags,
128 HtpHandle memory, off_t offset);
130 // Creates a host I/O descriptor from the handle.
131 HtpHandle CreateIoDesc(Handle handle);
133 // Creates a shared memory descriptor from the handle.
134 HtpHandle CreateShmDesc(Handle handle, off_t length);
136 // Creates an IMC memory descriptor from the handle.
137 HtpHandle CreateImcDesc(Handle handle);
139 // Reads up to count bytes from the handle into buffer.
140 int Read(HtpHandle handle, void* buffer, size_t count);
142 // Writes up to count bytes to the handle from buffer.
143 int Write(HtpHandle handle, const void* buffer, size_t count);
145 const HtpHandle kInvalidHtpHandle = NULL;
147 } // namespace nacl
149 #endif // __native_client__
151 #endif // NATIVE_CLIENT_INTERMODULE_COMM_NACL_HTP_H_