1 /*
2  * Copyright (c) 2022, Google Inc. All rights reserved
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #pragma once
24 
25 #include <kernel/vm.h>
26 #include <lib/ktipc/ktipc.h>
27 #include <lk/compiler.h>
28 
29 __BEGIN_CDECLS
30 
31 struct vmm_obj_service;
32 
33 /**
34  * vmm_obj_service_create_ro() - Creates a new read-only vmm_obj_service.
35  * @port: Name of the port for the new service.
36  * @acl: ACLs for the service.
37  * @obj: VMM object to serve over IPC.
38  * @offset: Offset into @obj.
39  * @size: Size of slice of @obj to serve.
40  * @srv_out: Pointer to struct vmm_obj_service to receive
41  *           the created object.
42  *
43  * @offset and @size must be multiples of the page size, otherwise
44  * the function will return %ERR_INVALID_ARGS.
45  *
46  * The function increments the reference count of @obj.
47  *
48  * The protocol for services created with this function works as follows:
49  * * The client opens a connection to the service,
50  *   without sending anything
51  * * The service immediately responds with a single message containing:
52  *   * An 8-byte unsigned integer encoding the size of the object
53  *   * A single memref handle for the underlying vmm_obj object
54  * * The client can then close the connection; any further messages
55  *   will cause the service to close the channel on its end.
56  *
57  * Return: %NO_ERROR in case of success, an error code otherwise.
58  */
59 int vmm_obj_service_create_ro(const char* port,
60                               const struct ktipc_port_acl* acl,
61                               struct vmm_obj* obj,
62                               size_t offset,
63                               size_t size,
64                               struct vmm_obj_service** srv_out);
65 
66 /**
67  * vmm_obj_service_destroy() - Destroys a vmm_obj_service created by
68  *                             vmm_obj_service_create().
69  * @srv: Pointer to a pointer to a service.
70  *
71  * This function destroys the service pointed to by the pointer at @srv
72  * and then sets the pointer to %NULL to prevent double frees.
73  */
74 void vmm_obj_service_destroy(struct vmm_obj_service** srv);
75 
76 /**
77  * vmm_obj_service_add() - Add a struct ktipc_port initialized by
78  *                         vmm_obj_service_init() to a server.
79  * @srv: Pointer to service.
80  * @server: ktipc server object to add the new service to.
81  *
82  * The caller is responsible for calling ktipc_server_start() to
83  * start @server. The caller must also preserve the lifetimes of
84  * the port names and ACL structure as long as the service is alive.
85  *
86  * Return: %NO_ERROR in case of success, an error code otherwise.
87  */
88 int vmm_obj_service_add(struct vmm_obj_service* srv,
89                         struct ktipc_server* server);
90 
91 __END_CDECLS
92