xref: /aosp_15_r20/system/core/trusty/metrics/include/trusty/metrics/tipc.h (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
1 /*
2  * Copyright 2021, 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 #pragma once
18 
19 #include <stdint.h>
20 
21 /**
22  * DOC: Metrics
23  *
24  * Metrics interface provides a way for Android to get Trusty metrics data.
25  *
26  * Currently, only "push" model is supported. Clients are expected to connect to
27  * metrics service, listen for events, e.g. app crash events, and respond to
28  * every event with a &struct metrics_req.
29  *
30  * Communication is driven by metrics service, i.e. requests/responses are all
31  * sent from/to metrics service.
32  *
33  * Note that the type of the event is not known to the client ahead of time.
34  *
35  * In the future, if we need to have Android "pull" metrics data from Trusty,
36  * that can be done by introducing a separate port.
37  *
38  * This interface is shared between Android and Trusty. There is a copy in each
39  * repository. They must be kept in sync.
40  */
41 
42 #define METRICS_PORT "com.android.trusty.metrics.consumer"
43 
44 #define UUID_STR_SIZE (37)
45 
46 #define HASH_SIZE_BYTES 64
47 
48 /**
49  * enum metrics_cmd - command identifiers for metrics interface
50  * @METRICS_CMD_RESP_BIT:             message is a response
51  * @METRICS_CMD_REQ_SHIFT:            number of bits used by @METRICS_CMD_RESP_BIT
52  * @METRICS_CMD_REPORT_EVENT_DROP:    report gaps in the event stream
53  * @METRICS_CMD_REPORT_CRASH:         report an app crash event
54  * @METRICS_CMD_REPORT_EXIT:          report an app exit
55  * @METRICS_CMD_REPORT_STORAGE_ERROR: report trusty storage error
56  */
57 enum metrics_cmd {
58     METRICS_CMD_RESP_BIT = 1,
59     METRICS_CMD_REQ_SHIFT = 1,
60 
61     METRICS_CMD_REPORT_EVENT_DROP = (1 << METRICS_CMD_REQ_SHIFT),
62     METRICS_CMD_REPORT_CRASH = (2 << METRICS_CMD_REQ_SHIFT),
63     METRICS_CMD_REPORT_EXIT = (3 << METRICS_CMD_REQ_SHIFT),
64     METRICS_CMD_REPORT_STORAGE_ERROR = (4 << METRICS_CMD_REQ_SHIFT),
65 };
66 
67 /**
68  * enum metrics_error - metrics error codes
69  * @METRICS_NO_ERROR:        no error
70  * @METRICS_ERR_UNKNOWN_CMD: unknown or not implemented command
71  */
72 enum metrics_error {
73     METRICS_NO_ERROR = 0,
74     METRICS_ERR_UNKNOWN_CMD = 1,
75 };
76 
77 /**
78  * struct metrics_req - common structure for metrics requests
79  * @cmd:      command identifier - one of &enum metrics_cmd
80  * @reserved: must be 0
81  */
82 struct metrics_req {
83     uint32_t cmd;
84     uint32_t reserved;
85 } __attribute__((__packed__));
86 
87 /**
88  * struct metrics_resp - common structure for metrics responses
89  * @cmd: command identifier - %METRICS_CMD_RESP_BIT or'ed with a cmd in
90  *                            one of &enum metrics_cmd
91  * @status: response status, one of &enum metrics_error
92  */
93 struct metrics_resp {
94     uint32_t cmd;
95     uint32_t status;
96 } __attribute__((__packed__));
97 
98 /**
99  * struct metrics_report_exit_req - arguments of %METRICS_CMD_REPORT_EXIT
100  *                                   requests
101  * @app_id: app_id in the form UUID in ascii format
102  *          "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
103  * @exit_code: architecture-specific exit code
104  */
105 struct metrics_report_exit_req {
106     char app_id[UUID_STR_SIZE];
107     uint32_t exit_code;
108 } __attribute__((__packed__));
109 
110 /**
111  * struct metrics_report_crash_req - arguments of %METRICS_CMD_REPORT_CRASH
112  *                                   requests
113  * @app_id: app_id in the form UUID in ascii format
114  *          "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
115  * @crash_reason: architecture-specific code representing the reason for the
116  *                crash
117  * @far: Fault Address Register corresponding to the crash. It is set to 0 and
118  *       not always revealed
119  * @far_hash: Fault Address Register obfuscated, always revealed
120  * @elr: Exception Link Register corresponding to the crash. It is set to 0 and
121  *       not always revealed
122  * @elr_hash: Exception Link Register obfuscated, always revealed
123  * @is_hash: Boolean value indicating whether far and elr have been ob
124  */
125 struct metrics_report_crash_req {
126     char app_id[UUID_STR_SIZE];
127     uint32_t crash_reason;
128     uint64_t far;
129     uint8_t far_hash[HASH_SIZE_BYTES];
130     uint64_t elr;
131     uint8_t elr_hash[HASH_SIZE_BYTES];
132     bool is_hash;
133 } __attribute__((__packed__));
134 
135 enum TrustyStorageErrorType {
136   TRUSTY_STORAGE_ERROR_UNKNOWN = 0,
137   TRUSTY_STORAGE_ERROR_SUPERBLOCK_INVALID = 1,
138   TRUSTY_STORAGE_ERROR_BLOCK_MAC_MISMATCH = 2,
139   TRUSTY_STORAGE_ERROR_BLOCK_HEADER_INVALID = 3,
140   TRUSTY_STORAGE_ERROR_RPMB_COUNTER_MISMATCH = 4,
141   TRUSTY_STORAGE_ERROR_RPMB_COUNTER_MISMATCH_RECOVERED = 5,
142   TRUSTY_STORAGE_ERROR_RPMB_COUNTER_READ_FAILURE = 6,
143   TRUSTY_STORAGE_ERROR_RPMB_MAC_MISMATCH = 7,
144   TRUSTY_STORAGE_ERROR_RPMB_ADDR_MISMATCH = 8,
145   TRUSTY_STORAGE_ERROR_RPMB_FAILURE_RESPONSE = 9,
146   TRUSTY_STORAGE_ERROR_RPMB_UNKNOWN = 10,
147   TRUSTY_STORAGE_ERROR_RPMB_SCSI_ERROR = 11,
148   TRUSTY_STORAGE_ERROR_IO_ERROR = 12,
149   TRUSTY_STORAGE_ERROR_PROXY_COMMUNICATION_FAILURE = 13,
150 };
151 
152 enum TrustyFileSystem {
153   TRUSTY_FS_UNKNOWN = 0,
154   TRUSTY_FS_TP = 1,
155   TRUSTY_FS_TD = 2,
156   TRUSTY_FS_TDP = 3,
157   TRUSTY_FS_TDEA = 4,
158   TRUSTY_FS_NSP = 5,
159 };
160 
161 enum TrustyBlockType {
162   TRUSTY_BLOCKTYPE_UNKNOWN = 0,
163   TRUSTY_BLOCKTYPE_FILES_ROOT = 1,
164   TRUSTY_BLOCKTYPE_FREE_ROOT = 2,
165   TRUSTY_BLOCKTYPE_FILES_INTERNAL = 3,
166   TRUSTY_BLOCKTYPE_FREE_INTERNAL = 4,
167   TRUSTY_BLOCKTYPE_FILE_ENTRY = 5,
168   TRUSTY_BLOCKTYPE_FILE_BLOCK_MAP = 6,
169   TRUSTY_BLOCKTYPE_FILE_DATA = 7,
170   TRUSTY_BLOCKTYPE_CHECKPOINT_ROOT = 8,
171   TRUSTY_BLOCKTYPE_CHECKPOINT_FILES_ROOT = 9,
172   TRUSTY_BLOCKTYPE_CHECKPOINT_FREE_ROOT = 10,
173 };
174 
175 struct metrics_report_storage_error_req {
176     enum TrustyStorageErrorType error;
177     char app_id[UUID_STR_SIZE];
178     char client_app_id[UUID_STR_SIZE];
179     uint32_t write;
180     enum TrustyFileSystem file_system;
181     uint64_t file_path_hash;
182     enum TrustyBlockType block_type;
183     uint64_t repair_counter;
184 } __attribute__((__packed__));
185 
186 struct metrics_msg {
187     struct metrics_req req;
188     union {
189         struct metrics_report_crash_req crash_args;
190         struct metrics_report_exit_req exit_args;
191         struct metrics_report_storage_error_req storage_args;
192     };
193 } __attribute__((__packed__));