1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18 This defines the messages exchanged between the non-secure world coverage controller
19 and the secure world coverage TA (aggregator). Coverage controller connects to the TA
20 and then uses that connection for sending coverage records.
21 */
22 
23 #pragma once
24 
25 #include <lk/compiler.h>
26 #include <stdint.h>
27 #include <trusty/uuid.h>
28 
29 __BEGIN_CDECLS
30 
31 #define LINE_COVERAGE_CLIENT_PORT "com.android.trusty.linecoverage.client"
32 
33 /**
34  * enum line_coverage_client_cmd - command identifiers for coverage client interface
35  * @LINE_COVERAGE_CLIENT_CMD_RESP_BIT:     response bit set as part of response
36  * @LINE_COVERAGE_CLIENT_CMD_SHIFT:        number of bits used by response bit
37  * @LINE_COVERAGE_CLIENT_CMD_OPEN:         command to open coverage record
38  * @LINE_COVERAGE_CLIENT_CMD_SHARE_RECORD: command to register a shared memory region
39  *                                    where coverage record will be written to
40  */
41 enum line_coverage_client_cmd {
42     LINE_COVERAGE_CLIENT_CMD_RESP_BIT = 1U,
43     LINE_COVERAGE_CLIENT_CMD_SHIFT = 1U,
44     LINE_COVERAGE_CLIENT_CMD_OPEN = (1U << LINE_COVERAGE_CLIENT_CMD_SHIFT),
45     LINE_COVERAGE_CLIENT_CMD_SHARE_RECORD = (2U << LINE_COVERAGE_CLIENT_CMD_SHIFT),
46     LINE_COVERAGE_CLIENT_CMD_SEND_LIST = (3U << LINE_COVERAGE_CLIENT_CMD_SHIFT),
47 };
48 
49 /**
50  * struct line_coverage_client_hdr - header for coverage client messages
51  * @cmd: command identifier
52  *
53  * Note that no messages return a status code. Any error on the server side
54  * results in the connection being closed. So, operations can be assumed to be
55  * successful if they return a response.
56  */
57 struct line_coverage_client_hdr {
58     uint32_t cmd;
59 };
60 
61 /**
62  * struct line_coverage_client_open_req - arguments for request to open coverage
63  *                                   record
64  * @uuid: UUID of target TA
65  *
66  * There is one coverage record per TA. @uuid is used to identify both the TA
67  * and corresponding coverage record.
68  */
69 struct line_coverage_client_open_req {
70     struct uuid uuid;
71 };
72 
73 /**
74  * struct line_coverage_client_open_resp - arguments for response to open coverage
75  *                                    record
76  * @record_len: length of coverage record that will be emitted by target TA
77  *
78  * Shared memory allocated for this coverage record must larger than
79  * @record_len.
80  */
81 struct line_coverage_client_open_resp {
82     uint32_t record_len;
83 };
84 
85 /**
86  * struct line_coverage_client_send_list_resp - arguments for response to send list
87  *                                   record
88  * @uuid: UUID of TA that connected to aggregator
89  */
90 struct line_coverage_client_send_list_resp {
91     struct uuid uuid;
92 };
93 
94 /**
95  * struct line_coverage_client_send_list_resp - arguments for response to send list
96  *                                   record
97  * @index: index of the list being requested
98  */
99 struct line_coverage_client_send_list_req {
100     uint32_t index;
101 };
102 
103 /**
104  * struct line_coverage_client_share_record_req - arguments for request to share
105  *                                           memory for coverage record
106  * @shm_len: length of memory region being shared
107  *
108  * A handle to a memory region must be sent along with this message. This memory
109  * is used to store coverage record.
110  *
111  * Upon success, this memory region can be assumed to be shared between the
112  * client and target TA.
113  */
114 struct line_coverage_client_share_record_req {
115     uint32_t shm_len;
116 };
117 
118 /**
119  * struct line_coverage_client_req - structure for a coverage client request
120  * @hdr:               message header
121  * @open_args:         arguments for %COVERAGE_CLIENT_CMD_OPEN request
122  * @share_record_args: arguments for %COVERAGE_CLIENT_CMD_SHARE_RECORD request
123  * @index: arguments for %COVERAGE_CLIENT_CMD_SHARE_RECORD request
124  */
125 struct line_coverage_client_req {
126     struct line_coverage_client_hdr hdr;
127     union {
128         struct line_coverage_client_open_req open_args;
129         struct line_coverage_client_share_record_req share_record_args;
130         struct line_coverage_client_send_list_req send_list_args;
131     };
132 };
133 
134 /**
135  * struct line_coverage_client_resp - structure for a coverage client response
136  * @hdr:       message header
137  * @open_args: arguments for %COVERAGE_CLIENT_CMD_OPEN response
138  * @send_list_args: arguments for %COVERAGE_CLIENT_CMD_SHARE_RECORD response
139  */
140 struct line_coverage_client_resp {
141     struct line_coverage_client_hdr hdr;
142     union {
143         struct line_coverage_client_open_resp open_args;
144         struct line_coverage_client_send_list_resp send_list_args;
145     };
146 };
147 
148 __END_CDECLS
149