1 /*
2  * Copyright (C) 2020 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 <lib/coverage/common/cov_shm.h>
20 #include <lib/tipc/tipc_srv.h>
21 #include <lk/list.h>
22 #include <stddef.h>
23 #include <string.h>
24 #include <trusty/uuid.h>
25 
26 /* Assume we have no more than 256 TAs running at the same time */
27 #define MAX_NUM_APPS 256
28 
29 /**
30  * struct srv_state - global state of the coverage server
31  * @hset:                 handle set
32  * @coverage_record_list: list of coverage records
33  * @mailbox:              mailbox used to broadcast events
34  */
35 struct srv_state {
36     struct tipc_hset* hset;
37     /*
38      * TODO: A hash map would be nice. But we only have a list implementation
39      * that's readily available. It should be good enough though, since the list
40      * is short (<100) and lookups/insertions are very infrequent.
41      */
42     struct list_node coverage_record_list;
43     struct cov_shm mailbox;
44 };
45 
set_srv_state(struct tipc_port * port,struct srv_state * state)46 static inline void set_srv_state(struct tipc_port* port,
47                                  struct srv_state* state) {
48     port->priv = state;
49 }
50 
get_srv_state(const struct tipc_port * port)51 static inline struct srv_state* get_srv_state(const struct tipc_port* port) {
52     return (struct srv_state*)(port->priv);
53 }
54 
55 /**
56  * struct coverage_record - code coverage record about a given TA
57  * @node:       list node
58  * @uuid:       UUID of target TA
59  * @idx:        unique index assigned to this record and corresponding TA
60  * @data:       shared memory region holding the coverage record
61  * @record_len: length of coverage record within @data
62  */
63 struct coverage_record {
64     struct list_node node;
65     struct uuid uuid;
66     size_t idx;
67     struct cov_shm data;
68     size_t record_len;
69 };
70 
equal_uuid(const struct uuid * a,const struct uuid * b)71 static inline bool equal_uuid(const struct uuid* a, const struct uuid* b) {
72     return memcmp(a, b, sizeof(struct uuid)) == 0;
73 }
74 
find_coverage_record(struct list_node * head,const struct uuid * uuid)75 static inline struct coverage_record* find_coverage_record(
76         struct list_node* head,
77         const struct uuid* uuid) {
78     struct coverage_record* record;
79 
80     list_for_every_entry(head, record, struct coverage_record, node) {
81         if (equal_uuid(&record->uuid, uuid)) {
82             return record;
83         }
84     }
85     return NULL;
86 }
87 
88 /**
89  * coverage_aggregator_init() - initialize coverage aggregator service
90  * @state: pointer to global &struct srv_state
91  *
92  * Return: 0 on success, negative error code on error
93  */
94 int coverage_aggregator_init(struct srv_state* state);
95 
96 /**
97  * coverage_client_init() - initialize coverage client service
98  * @state: pointer to global &struct srv_state
99  *
100  * Return: 0 on success, negative error code on error
101  */
102 int coverage_client_init(struct srv_state* state);
103