1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "mojo/core/entrypoints.h"
6
7 #include <stdint.h>
8
9 #include "mojo/core/core.h"
10 #include "mojo/public/c/system/buffer.h"
11 #include "mojo/public/c/system/data_pipe.h"
12 #include "mojo/public/c/system/functions.h"
13 #include "mojo/public/c/system/message_pipe.h"
14 #include "mojo/public/c/system/platform_handle.h"
15 #include "mojo/public/c/system/quota.h"
16
17 namespace {
18
19 mojo::core::Core* g_core;
20
21 extern "C" {
22
MojoInitializeImpl(const struct MojoInitializeOptions * options)23 MojoResult MojoInitializeImpl(const struct MojoInitializeOptions* options) {
24 NOTREACHED() << "Do not call MojoInitialize() as an EDK embedder!";
25 return MOJO_RESULT_OK;
26 }
27
MojoGetTimeTicksNowImpl()28 MojoTimeTicks MojoGetTimeTicksNowImpl() {
29 return g_core->GetTimeTicksNow();
30 }
31
MojoCloseImpl(MojoHandle handle)32 MojoResult MojoCloseImpl(MojoHandle handle) {
33 return g_core->Close(handle);
34 }
35
MojoQueryHandleSignalsStateImpl(MojoHandle handle,MojoHandleSignalsState * signals_state)36 MojoResult MojoQueryHandleSignalsStateImpl(
37 MojoHandle handle,
38 MojoHandleSignalsState* signals_state) {
39 return g_core->QueryHandleSignalsState(handle, signals_state);
40 }
41
MojoCreateMessagePipeImpl(const MojoCreateMessagePipeOptions * options,MojoHandle * message_pipe_handle0,MojoHandle * message_pipe_handle1)42 MojoResult MojoCreateMessagePipeImpl(
43 const MojoCreateMessagePipeOptions* options,
44 MojoHandle* message_pipe_handle0,
45 MojoHandle* message_pipe_handle1) {
46 return g_core->CreateMessagePipe(options, message_pipe_handle0,
47 message_pipe_handle1);
48 }
49
MojoWriteMessageImpl(MojoHandle message_pipe_handle,MojoMessageHandle message,const MojoWriteMessageOptions * options)50 MojoResult MojoWriteMessageImpl(MojoHandle message_pipe_handle,
51 MojoMessageHandle message,
52 const MojoWriteMessageOptions* options) {
53 return g_core->WriteMessage(message_pipe_handle, message, options);
54 }
55
MojoReadMessageImpl(MojoHandle message_pipe_handle,const MojoReadMessageOptions * options,MojoMessageHandle * message)56 MojoResult MojoReadMessageImpl(MojoHandle message_pipe_handle,
57 const MojoReadMessageOptions* options,
58 MojoMessageHandle* message) {
59 return g_core->ReadMessage(message_pipe_handle, options, message);
60 }
61
MojoFuseMessagePipesImpl(MojoHandle handle0,MojoHandle handle1,const MojoFuseMessagePipesOptions * options)62 MojoResult MojoFuseMessagePipesImpl(
63 MojoHandle handle0,
64 MojoHandle handle1,
65 const MojoFuseMessagePipesOptions* options) {
66 return g_core->FuseMessagePipes(handle0, handle1, options);
67 }
68
MojoCreateMessageImpl(const MojoCreateMessageOptions * options,MojoMessageHandle * message)69 MojoResult MojoCreateMessageImpl(const MojoCreateMessageOptions* options,
70 MojoMessageHandle* message) {
71 return g_core->CreateMessage(options, message);
72 }
73
MojoDestroyMessageImpl(MojoMessageHandle message)74 MojoResult MojoDestroyMessageImpl(MojoMessageHandle message) {
75 return g_core->DestroyMessage(message);
76 }
77
MojoSerializeMessageImpl(MojoMessageHandle message,const MojoSerializeMessageOptions * options)78 MojoResult MojoSerializeMessageImpl(
79 MojoMessageHandle message,
80 const MojoSerializeMessageOptions* options) {
81 return g_core->SerializeMessage(message, options);
82 }
83
MojoAppendMessageDataImpl(MojoMessageHandle message,uint32_t additional_payload_size,const MojoHandle * handles,uint32_t num_handles,const MojoAppendMessageDataOptions * options,void ** buffer,uint32_t * buffer_size)84 MojoResult MojoAppendMessageDataImpl(
85 MojoMessageHandle message,
86 uint32_t additional_payload_size,
87 const MojoHandle* handles,
88 uint32_t num_handles,
89 const MojoAppendMessageDataOptions* options,
90 void** buffer,
91 uint32_t* buffer_size) {
92 return g_core->AppendMessageData(message, additional_payload_size, handles,
93 num_handles, options, buffer, buffer_size);
94 }
95
MojoGetMessageDataImpl(MojoMessageHandle message,const MojoGetMessageDataOptions * options,void ** buffer,uint32_t * num_bytes,MojoHandle * handles,uint32_t * num_handles)96 MojoResult MojoGetMessageDataImpl(MojoMessageHandle message,
97 const MojoGetMessageDataOptions* options,
98 void** buffer,
99 uint32_t* num_bytes,
100 MojoHandle* handles,
101 uint32_t* num_handles) {
102 return g_core->GetMessageData(message, options, buffer, num_bytes, handles,
103 num_handles);
104 }
105
MojoSetMessageContextImpl(MojoMessageHandle message,uintptr_t context,MojoMessageContextSerializer serializer,MojoMessageContextDestructor destructor,const MojoSetMessageContextOptions * options)106 MojoResult MojoSetMessageContextImpl(
107 MojoMessageHandle message,
108 uintptr_t context,
109 MojoMessageContextSerializer serializer,
110 MojoMessageContextDestructor destructor,
111 const MojoSetMessageContextOptions* options) {
112 return g_core->SetMessageContext(message, context, serializer, destructor,
113 options);
114 }
115
MojoGetMessageContextImpl(MojoMessageHandle message,const MojoGetMessageContextOptions * options,uintptr_t * context)116 MojoResult MojoGetMessageContextImpl(
117 MojoMessageHandle message,
118 const MojoGetMessageContextOptions* options,
119 uintptr_t* context) {
120 return g_core->GetMessageContext(message, options, context);
121 }
122
MojoNotifyBadMessageImpl(MojoMessageHandle message,const char * error,uint32_t error_num_bytes,const MojoNotifyBadMessageOptions * options)123 MojoResult MojoNotifyBadMessageImpl(
124 MojoMessageHandle message,
125 const char* error,
126 uint32_t error_num_bytes,
127 const MojoNotifyBadMessageOptions* options) {
128 return g_core->NotifyBadMessage(message, error, error_num_bytes, options);
129 }
130
MojoCreateDataPipeImpl(const MojoCreateDataPipeOptions * options,MojoHandle * data_pipe_producer_handle,MojoHandle * data_pipe_consumer_handle)131 MojoResult MojoCreateDataPipeImpl(const MojoCreateDataPipeOptions* options,
132 MojoHandle* data_pipe_producer_handle,
133 MojoHandle* data_pipe_consumer_handle) {
134 return g_core->CreateDataPipe(options, data_pipe_producer_handle,
135 data_pipe_consumer_handle);
136 }
137
MojoWriteDataImpl(MojoHandle data_pipe_producer_handle,const void * elements,uint32_t * num_elements,const MojoWriteDataOptions * options)138 MojoResult MojoWriteDataImpl(MojoHandle data_pipe_producer_handle,
139 const void* elements,
140 uint32_t* num_elements,
141 const MojoWriteDataOptions* options) {
142 return g_core->WriteData(data_pipe_producer_handle, elements, num_elements,
143 options);
144 }
145
MojoBeginWriteDataImpl(MojoHandle data_pipe_producer_handle,const MojoBeginWriteDataOptions * options,void ** buffer,uint32_t * buffer_num_elements)146 MojoResult MojoBeginWriteDataImpl(MojoHandle data_pipe_producer_handle,
147 const MojoBeginWriteDataOptions* options,
148 void** buffer,
149 uint32_t* buffer_num_elements) {
150 return g_core->BeginWriteData(data_pipe_producer_handle, options, buffer,
151 buffer_num_elements);
152 }
153
MojoEndWriteDataImpl(MojoHandle data_pipe_producer_handle,uint32_t num_elements_written,const MojoEndWriteDataOptions * options)154 MojoResult MojoEndWriteDataImpl(MojoHandle data_pipe_producer_handle,
155 uint32_t num_elements_written,
156 const MojoEndWriteDataOptions* options) {
157 return g_core->EndWriteData(data_pipe_producer_handle, num_elements_written,
158 options);
159 }
160
MojoReadDataImpl(MojoHandle data_pipe_consumer_handle,const MojoReadDataOptions * options,void * elements,uint32_t * num_elements)161 MojoResult MojoReadDataImpl(MojoHandle data_pipe_consumer_handle,
162 const MojoReadDataOptions* options,
163 void* elements,
164 uint32_t* num_elements) {
165 return g_core->ReadData(data_pipe_consumer_handle, options, elements,
166 num_elements);
167 }
168
MojoBeginReadDataImpl(MojoHandle data_pipe_consumer_handle,const MojoBeginReadDataOptions * options,const void ** buffer,uint32_t * buffer_num_elements)169 MojoResult MojoBeginReadDataImpl(MojoHandle data_pipe_consumer_handle,
170 const MojoBeginReadDataOptions* options,
171 const void** buffer,
172 uint32_t* buffer_num_elements) {
173 return g_core->BeginReadData(data_pipe_consumer_handle, options, buffer,
174 buffer_num_elements);
175 }
176
MojoEndReadDataImpl(MojoHandle data_pipe_consumer_handle,uint32_t num_elements_read,const MojoEndReadDataOptions * options)177 MojoResult MojoEndReadDataImpl(MojoHandle data_pipe_consumer_handle,
178 uint32_t num_elements_read,
179 const MojoEndReadDataOptions* options) {
180 return g_core->EndReadData(data_pipe_consumer_handle, num_elements_read,
181 options);
182 }
183
MojoCreateSharedBufferImpl(uint64_t num_bytes,const MojoCreateSharedBufferOptions * options,MojoHandle * shared_buffer_handle)184 MojoResult MojoCreateSharedBufferImpl(
185 uint64_t num_bytes,
186 const MojoCreateSharedBufferOptions* options,
187 MojoHandle* shared_buffer_handle) {
188 return g_core->CreateSharedBuffer(num_bytes, options, shared_buffer_handle);
189 }
190
MojoDuplicateBufferHandleImpl(MojoHandle buffer_handle,const MojoDuplicateBufferHandleOptions * options,MojoHandle * new_buffer_handle)191 MojoResult MojoDuplicateBufferHandleImpl(
192 MojoHandle buffer_handle,
193 const MojoDuplicateBufferHandleOptions* options,
194 MojoHandle* new_buffer_handle) {
195 return g_core->DuplicateBufferHandle(buffer_handle, options,
196 new_buffer_handle);
197 }
198
MojoMapBufferImpl(MojoHandle buffer_handle,uint64_t offset,uint64_t num_bytes,const MojoMapBufferOptions * options,void ** buffer)199 MojoResult MojoMapBufferImpl(MojoHandle buffer_handle,
200 uint64_t offset,
201 uint64_t num_bytes,
202 const MojoMapBufferOptions* options,
203 void** buffer) {
204 return g_core->MapBuffer(buffer_handle, offset, num_bytes, options, buffer);
205 }
206
MojoUnmapBufferImpl(void * buffer)207 MojoResult MojoUnmapBufferImpl(void* buffer) {
208 return g_core->UnmapBuffer(buffer);
209 }
210
MojoGetBufferInfoImpl(MojoHandle buffer_handle,const MojoGetBufferInfoOptions * options,MojoSharedBufferInfo * info)211 MojoResult MojoGetBufferInfoImpl(MojoHandle buffer_handle,
212 const MojoGetBufferInfoOptions* options,
213 MojoSharedBufferInfo* info) {
214 return g_core->GetBufferInfo(buffer_handle, options, info);
215 }
216
MojoCreateTrapImpl(MojoTrapEventHandler handler,const MojoCreateTrapOptions * options,MojoHandle * trap_handle)217 MojoResult MojoCreateTrapImpl(MojoTrapEventHandler handler,
218 const MojoCreateTrapOptions* options,
219 MojoHandle* trap_handle) {
220 return g_core->CreateTrap(handler, options, trap_handle);
221 }
222
MojoAddTriggerImpl(MojoHandle trap_handle,MojoHandle handle,MojoHandleSignals signals,MojoTriggerCondition condition,uintptr_t context,const MojoAddTriggerOptions * options)223 MojoResult MojoAddTriggerImpl(MojoHandle trap_handle,
224 MojoHandle handle,
225 MojoHandleSignals signals,
226 MojoTriggerCondition condition,
227 uintptr_t context,
228 const MojoAddTriggerOptions* options) {
229 return g_core->AddTrigger(trap_handle, handle, signals, condition, context,
230 options);
231 }
232
MojoRemoveTriggerImpl(MojoHandle trap_handle,uintptr_t context,const MojoRemoveTriggerOptions * options)233 MojoResult MojoRemoveTriggerImpl(MojoHandle trap_handle,
234 uintptr_t context,
235 const MojoRemoveTriggerOptions* options) {
236 return g_core->RemoveTrigger(trap_handle, context, options);
237 }
238
MojoArmTrapImpl(MojoHandle trap_handle,const MojoArmTrapOptions * options,uint32_t * num_blocking_events,MojoTrapEvent * blocking_events)239 MojoResult MojoArmTrapImpl(MojoHandle trap_handle,
240 const MojoArmTrapOptions* options,
241 uint32_t* num_blocking_events,
242 MojoTrapEvent* blocking_events) {
243 return g_core->ArmTrap(trap_handle, options, num_blocking_events,
244 blocking_events);
245 }
246
MojoWrapPlatformHandleImpl(const MojoPlatformHandle * platform_handle,const MojoWrapPlatformHandleOptions * options,MojoHandle * mojo_handle)247 MojoResult MojoWrapPlatformHandleImpl(
248 const MojoPlatformHandle* platform_handle,
249 const MojoWrapPlatformHandleOptions* options,
250 MojoHandle* mojo_handle) {
251 return g_core->WrapPlatformHandle(platform_handle, options, mojo_handle);
252 }
253
MojoUnwrapPlatformHandleImpl(MojoHandle mojo_handle,const MojoUnwrapPlatformHandleOptions * options,MojoPlatformHandle * platform_handle)254 MojoResult MojoUnwrapPlatformHandleImpl(
255 MojoHandle mojo_handle,
256 const MojoUnwrapPlatformHandleOptions* options,
257 MojoPlatformHandle* platform_handle) {
258 return g_core->UnwrapPlatformHandle(mojo_handle, options, platform_handle);
259 }
260
MojoWrapPlatformSharedMemoryRegionImpl(const MojoPlatformHandle * platform_handles,uint32_t num_platform_handles,uint64_t num_bytes,const MojoSharedBufferGuid * guid,MojoPlatformSharedMemoryRegionAccessMode access_mode,const MojoWrapPlatformSharedMemoryRegionOptions * options,MojoHandle * mojo_handle)261 MojoResult MojoWrapPlatformSharedMemoryRegionImpl(
262 const MojoPlatformHandle* platform_handles,
263 uint32_t num_platform_handles,
264 uint64_t num_bytes,
265 const MojoSharedBufferGuid* guid,
266 MojoPlatformSharedMemoryRegionAccessMode access_mode,
267 const MojoWrapPlatformSharedMemoryRegionOptions* options,
268 MojoHandle* mojo_handle) {
269 return g_core->WrapPlatformSharedMemoryRegion(
270 platform_handles, num_platform_handles, num_bytes, guid, access_mode,
271 options, mojo_handle);
272 }
273
MojoUnwrapPlatformSharedMemoryRegionImpl(MojoHandle mojo_handle,const MojoUnwrapPlatformSharedMemoryRegionOptions * options,MojoPlatformHandle * platform_handles,uint32_t * num_platform_handles,uint64_t * num_bytes,MojoSharedBufferGuid * guid,MojoPlatformSharedMemoryRegionAccessMode * access_mode)274 MojoResult MojoUnwrapPlatformSharedMemoryRegionImpl(
275 MojoHandle mojo_handle,
276 const MojoUnwrapPlatformSharedMemoryRegionOptions* options,
277 MojoPlatformHandle* platform_handles,
278 uint32_t* num_platform_handles,
279 uint64_t* num_bytes,
280 MojoSharedBufferGuid* guid,
281 MojoPlatformSharedMemoryRegionAccessMode* access_mode) {
282 return g_core->UnwrapPlatformSharedMemoryRegion(
283 mojo_handle, options, platform_handles, num_platform_handles, num_bytes,
284 guid, access_mode);
285 }
286
MojoCreateInvitationImpl(const MojoCreateInvitationOptions * options,MojoHandle * invitation_handle)287 MojoResult MojoCreateInvitationImpl(const MojoCreateInvitationOptions* options,
288 MojoHandle* invitation_handle) {
289 return g_core->CreateInvitation(options, invitation_handle);
290 }
291
MojoAttachMessagePipeToInvitationImpl(MojoHandle invitation_handle,const void * name,uint32_t name_num_bytes,const MojoAttachMessagePipeToInvitationOptions * options,MojoHandle * message_pipe_handle)292 MojoResult MojoAttachMessagePipeToInvitationImpl(
293 MojoHandle invitation_handle,
294 const void* name,
295 uint32_t name_num_bytes,
296 const MojoAttachMessagePipeToInvitationOptions* options,
297 MojoHandle* message_pipe_handle) {
298 return g_core->AttachMessagePipeToInvitation(
299 invitation_handle, name, name_num_bytes, options, message_pipe_handle);
300 }
301
MojoExtractMessagePipeFromInvitationImpl(MojoHandle invitation_handle,const void * name,uint32_t name_num_bytes,const MojoExtractMessagePipeFromInvitationOptions * options,MojoHandle * message_pipe_handle)302 MojoResult MojoExtractMessagePipeFromInvitationImpl(
303 MojoHandle invitation_handle,
304 const void* name,
305 uint32_t name_num_bytes,
306 const MojoExtractMessagePipeFromInvitationOptions* options,
307 MojoHandle* message_pipe_handle) {
308 return g_core->ExtractMessagePipeFromInvitation(
309 invitation_handle, name, name_num_bytes, options, message_pipe_handle);
310 }
311
MojoSendInvitationImpl(MojoHandle invitation_handle,const MojoPlatformProcessHandle * process_handle,const MojoInvitationTransportEndpoint * transport_endpoint,MojoProcessErrorHandler error_handler,uintptr_t error_handler_context,const MojoSendInvitationOptions * options)312 MojoResult MojoSendInvitationImpl(
313 MojoHandle invitation_handle,
314 const MojoPlatformProcessHandle* process_handle,
315 const MojoInvitationTransportEndpoint* transport_endpoint,
316 MojoProcessErrorHandler error_handler,
317 uintptr_t error_handler_context,
318 const MojoSendInvitationOptions* options) {
319 return g_core->SendInvitation(invitation_handle, process_handle,
320 transport_endpoint, error_handler,
321 error_handler_context, options);
322 }
323
MojoAcceptInvitationImpl(const MojoInvitationTransportEndpoint * transport_endpoint,const MojoAcceptInvitationOptions * options,MojoHandle * invitation_handle)324 MojoResult MojoAcceptInvitationImpl(
325 const MojoInvitationTransportEndpoint* transport_endpoint,
326 const MojoAcceptInvitationOptions* options,
327 MojoHandle* invitation_handle) {
328 return g_core->AcceptInvitation(transport_endpoint, options,
329 invitation_handle);
330 }
331
MojoSetQuotaImpl(MojoHandle handle,MojoQuotaType type,uint64_t limit,const MojoSetQuotaOptions * options)332 MojoResult MojoSetQuotaImpl(MojoHandle handle,
333 MojoQuotaType type,
334 uint64_t limit,
335 const MojoSetQuotaOptions* options) {
336 return g_core->SetQuota(handle, type, limit, options);
337 }
338
MojoQueryQuotaImpl(MojoHandle handle,MojoQuotaType type,const MojoQueryQuotaOptions * options,uint64_t * current_limit,uint64_t * current_usage)339 MojoResult MojoQueryQuotaImpl(MojoHandle handle,
340 MojoQuotaType type,
341 const MojoQueryQuotaOptions* options,
342 uint64_t* current_limit,
343 uint64_t* current_usage) {
344 return g_core->QueryQuota(handle, type, options, current_limit,
345 current_usage);
346 }
347
348 } // extern "C"
349
350 MojoSystemThunks g_thunks = {sizeof(MojoSystemThunks),
351 MojoInitializeImpl,
352 MojoGetTimeTicksNowImpl,
353 MojoCloseImpl,
354 MojoQueryHandleSignalsStateImpl,
355 MojoCreateMessagePipeImpl,
356 MojoWriteMessageImpl,
357 MojoReadMessageImpl,
358 MojoFuseMessagePipesImpl,
359 MojoCreateMessageImpl,
360 MojoDestroyMessageImpl,
361 MojoSerializeMessageImpl,
362 MojoAppendMessageDataImpl,
363 MojoGetMessageDataImpl,
364 MojoSetMessageContextImpl,
365 MojoGetMessageContextImpl,
366 MojoNotifyBadMessageImpl,
367 MojoCreateDataPipeImpl,
368 MojoWriteDataImpl,
369 MojoBeginWriteDataImpl,
370 MojoEndWriteDataImpl,
371 MojoReadDataImpl,
372 MojoBeginReadDataImpl,
373 MojoEndReadDataImpl,
374 MojoCreateSharedBufferImpl,
375 MojoDuplicateBufferHandleImpl,
376 MojoMapBufferImpl,
377 MojoUnmapBufferImpl,
378 MojoGetBufferInfoImpl,
379 MojoCreateTrapImpl,
380 MojoAddTriggerImpl,
381 MojoRemoveTriggerImpl,
382 MojoArmTrapImpl,
383 MojoWrapPlatformHandleImpl,
384 MojoUnwrapPlatformHandleImpl,
385 MojoWrapPlatformSharedMemoryRegionImpl,
386 MojoUnwrapPlatformSharedMemoryRegionImpl,
387 MojoCreateInvitationImpl,
388 MojoAttachMessagePipeToInvitationImpl,
389 MojoExtractMessagePipeFromInvitationImpl,
390 MojoSendInvitationImpl,
391 MojoAcceptInvitationImpl,
392 MojoSetQuotaImpl,
393 MojoQueryQuotaImpl};
394
395 } // namespace
396
397 namespace mojo {
398 namespace core {
399
400 // static
Get()401 Core* Core::Get() {
402 return g_core;
403 }
404
InitializeCore()405 void InitializeCore() {
406 g_core = new Core;
407 }
408
GetSystemThunks()409 const MojoSystemThunks& GetSystemThunks() {
410 return g_thunks;
411 }
412
413 } // namespace core
414 } // namespace mojo
415