1 // Copyright 2017 The Fuchsia 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 #pragma once
6 
7 #include <stdint.h>
8 
9 #include <zircon/types.h>
10 #include <zircon/compiler.h>
11 
12 __BEGIN_CDECLS
13 
14 // mini-process available commands. Use mini_process_cmd() to send them.
15 //
16 // The process echoes a canned message.
17 // The return value upon success is ZX_OK.
18 #define MINIP_CMD_ECHO_MSG                   (1 << 0)
19 // The process creates an event and sends it back on |handle|.
20 // The return value upon success is ZX_OK.
21 #define MINIP_CMD_CREATE_EVENT               (1 << 1)
22 // The process creates a channel and sends one end back on |handle|.
23 // The return value upon success is ZX_OK.
24 #define MINIP_CMD_CREATE_CHANNEL             (1 << 2)
25 // The following two commands cause the process to call a syscall with an
26 // invalid handle value.  The return value is the result of that syscall.
27 #define MINIP_CMD_USE_BAD_HANDLE_CLOSED      (1 << 3)
28 #define MINIP_CMD_USE_BAD_HANDLE_TRANSFERRED (1 << 4)
29 // The process will execute __builtin_trap() which causes a fatal exception.
30 // The return value upon success is ZX_ERR_PEER_CLOSED.
31 #define MINIP_CMD_BUILTIN_TRAP               (1 << 5)
32 // The process just calls zx_process_exit() immediately without replying.
33 // The return value upon success is ZX_ERR_PEER_CLOSED.
34 #define MINIP_CMD_EXIT_NORMAL                (1 << 6)
35 // The process calls zx_object_info( ZX_INFO_HANDLE_VALID) on a closed handle.
36 #define MINIP_CMD_VALIDATE_CLOSED_HANDLE     (1 << 7)
37 
38 // Create and run a minimal process with one thread that blocks forever.
39 // Does not require a host binary.
40 zx_status_t start_mini_process(zx_handle_t job, zx_handle_t transferred_handle,
41                                zx_handle_t* process, zx_handle_t* thread);
42 
43 // Like start_mini_process() but requires caller to create the process,
44 // thread and object to transfer.  Pass NULL in |cntrl_channel| to create
45 // a minimal process that has no VDSO and loops forever. If |cntrl_channel|
46 // is valid then upon successful return it contains the handle to a channel
47 // that the new process is listening to for commands via mini_process_cmd().
48 zx_status_t start_mini_process_etc(zx_handle_t process, zx_handle_t thread,
49                                    zx_handle_t vmar,
50                                    zx_handle_t transferred_handle,
51                                    zx_handle_t* cntrl_channel);
52 
53 // Starts a no-VDSO infinite-loop thread.
54 zx_status_t start_mini_process_thread(zx_handle_t thread, zx_handle_t vmar);
55 
56 // Execute in the mini process any set of the MINIP_CMD_ commands above.
57 // The |cntrl_channel| should be the same as the one returned by
58 // start_mini_process_etc().  The |handle| is an in/out parameter
59 // dependent on the command.
60 zx_status_t mini_process_cmd(zx_handle_t cntrl_channel,
61                              uint32_t what, zx_handle_t* handle);
62 
63 // The following pair of functions is equivalent to mini_process_cmd(), but
64 // they allow sending the request and receiving the reply to be done
65 // separately.  This allows handling the case where the mini process gets
66 // suspended as a result of executing the command.
67 zx_status_t mini_process_cmd_send(zx_handle_t cntrl_channel, uint32_t what);
68 zx_status_t mini_process_cmd_read_reply(zx_handle_t cntrl_channel,
69                                         zx_handle_t* handle);
70 
71 __END_CDECLS
72