1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
/*
* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <linux/string.h>
#include "eventlib_init.h"
static int priv_init(struct eventlib_ctx *ctx)
{
uint32_t priv_size = ALIGN_64_UP(sizeof(struct eventlib_init));
if (sizeof(ctx->local_mem) < priv_size)
return -ENOMEM;
ctx->priv = (struct eventlib_init *)ctx->local_mem;
memset(ctx->priv, 0, priv_size);
return 0;
}
static int shm_writer_setup_start(shmptr struct eventlib_shared *shm,
uint32_t size, uint32_t *offset)
{
if (size < sizeof(struct eventlib_shared))
return -ENOSPC;
memset(shm, 0, sizeof(struct eventlib_shared));
*offset = ALIGN_64_UP(sizeof(struct eventlib_shared));
return 0;
}
static void shm_writer_setup_done(struct eventlib_shared *copy,
shmptr struct eventlib_shared *shm, uint32_t magic)
{
/* Publish subsystem config to shared memory. Note that all local
* accesses to subsystem config are done through local values to
* protect against unexpected asynchronous changes.
*/
memcpy(shm->subsys, copy->subsys, sizeof(shm->subsys));
write_barrier();
shm->magic = magic;
write_barrier();
}
static int shm_reader_check(struct eventlib_shared *copy,
shmptr struct eventlib_shared *shm, uint32_t size, uint32_t magic)
{
int i;
if (size < sizeof(struct eventlib_shared))
return -ENOSPC;
if (shm->magic != magic)
return -EIO;
read_barrier();
/* Query subsystem config from shared memory and verify it. Further
* accesses to subsystem config must be done through these values in
* order to protect against unexpected asynchronous changes.
*/
memcpy(©->num_buffers, &shm->num_buffers,
sizeof(copy->num_buffers));
memcpy(copy->subsys, shm->subsys, sizeof(copy->subsys));
for (i = 0; i < EVENTLIB_SUBSYS_MAX; i++) {
if (!copy->subsys[i].offset || !copy->subsys[i].size)
continue;
if (copy->subsys[i].offset > size)
return -ENOSPC;
if (copy->subsys[i].offset + copy->subsys[i].size > size)
return -ENOSPC;
}
return 0;
}
static int shm_region_start(struct eventlib_ctx *ctx)
{
int ret;
struct eventlib_shared *shm;
if (ctx->direction == EVENTLIB_DIRECTION_WRITER) {
ret = shm_writer_setup_start(ctx->w2r_shm,
ctx->w2r_shm_size,
&ctx->priv->w2r_offset);
if (!ret && ctx->r2w_shm)
ret = shm_writer_setup_start(ctx->r2w_shm,
ctx->r2w_shm_size,
&ctx->priv->r2w_offset);
/* Update shared memory do that value is visible to reader */
shm = (struct eventlib_shared *)ctx->w2r_shm;
shm->num_buffers = ctx->num_buffers;
} else {
ret = shm_reader_check(&ctx->priv->w2r_copy, ctx->w2r_shm,
ctx->w2r_shm_size, EVENTLIB_MAGIC_W2R);
if (!ret && ctx->r2w_shm)
ret = shm_reader_check(&ctx->priv->r2w_copy,
ctx->r2w_shm, ctx->r2w_shm_size,
EVENTLIB_MAGIC_R2W);
}
return ret;
}
static void shm_region_done(struct eventlib_ctx *ctx)
{
if (ctx->direction == EVENTLIB_DIRECTION_WRITER) {
shm_writer_setup_done(&ctx->priv->w2r_copy,
ctx->w2r_shm, EVENTLIB_MAGIC_W2R);
if (ctx->r2w_shm)
shm_writer_setup_done(&ctx->priv->r2w_copy,
ctx->r2w_shm, EVENTLIB_MAGIC_R2W);
}
}
static bool mem_ok(void *p, uint32_t size)
{
if (!size)
return true;
else if (!p)
return false;
else if (!ALIGN_64_CHECK((uintptr_t)p))
return false;
else
return true;
}
int _eventlib_init(struct eventlib_ctx *ctx, uint32_t ctx_version,
uint32_t ctx_size)
{
int ret;
/* TODO: need a better size check */
if (ctx_version != EVENTLIB_CTX_VERSION ||
ctx_size == 0)
return -EFAULT;
if (ctx->direction != EVENTLIB_DIRECTION_WRITER &&
ctx->direction != EVENTLIB_DIRECTION_READER)
return -EINVAL;
if (!ctx->w2r_shm ||
!mem_ok(ctx->w2r_shm, ctx->w2r_shm_size) ||
!mem_ok(ctx->r2w_shm, ctx->r2w_shm_size))
return -EINVAL;
if (ctx->priv != NULL)
return -EINVAL;
ctx->w2r_shm_size = ALIGN_64_DOWN(ctx->w2r_shm_size);
ctx->r2w_shm_size = ALIGN_64_DOWN(ctx->r2w_shm_size);
if (ctx->direction == EVENTLIB_DIRECTION_WRITER &&
ctx->num_buffers == 0)
ctx->num_buffers = 1;
ret = priv_init(ctx);
if (ret)
return ret;
ret = shm_region_start(ctx);
if (ret) {
ctx->priv = NULL;
return ret;
}
if (flt_init) {
ret = flt_init(ctx);
if (ret) {
ctx->priv = NULL;
return ret;
}
}
ret = tbuf_init(ctx);
if (ret) {
eventlib_close(ctx);
return ret;
}
shm_region_done(ctx);
return 0;
}
void eventlib_close(struct eventlib_ctx *ctx)
{
if (flt_fini)
flt_fini(ctx);
ctx->priv = NULL;
}
|