Repository navigation
Expand file tree
/
Copy pathring_stream.cpp
More file actions
466 lines (378 loc) · 15.2 KB
/
Copy pathring_stream.cpp
File metadata and controls
466 lines (378 loc) · 15.2 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ring_stream.h"
#include <assert.h>
#include <memory.h>
#include "gfxstream/host/dma_device.h"
#include "gfxstream/common/logging.h"
#include "gfxstream/host/stream_utils.h"
#include "gfxstream/system/System.h"
#include "render-utils/dma_device.h"
#include "render-utils/stream.h"
namespace gfxstream {
namespace {
struct asg_context CreateContext(const AsgConsumerCreateInfo& info) {
struct asg_context context = asg_context_create(info.ring_storage, info.buffer, info.buffer_size);
context.ring_config->buffer_size = info.buffer_size;
context.ring_config->flush_interval = info.buffer_flush_interval;
context.ring_config->host_consumed_pos = 0;
context.ring_config->guest_write_pos = 0;
context.ring_config->transfer_mode = 1;
context.ring_config->transfer_size = 0;
context.ring_config->in_error = 0;
return context;
}
void SaveRingConfig(Stream* stream, const struct asg_ring_config& config) {
stream->putBe32(config.buffer_size);
stream->putBe32(config.flush_interval);
stream->putBe32(config.host_consumed_pos);
stream->putBe32(config.guest_write_pos);
stream->putBe32(config.transfer_mode);
stream->putBe32(config.transfer_size);
stream->putBe32(config.in_error);
}
void LoadRingConfig(Stream* stream, struct asg_ring_config* config) {
config->buffer_size = stream->getBe32();
config->flush_interval = stream->getBe32();
config->host_consumed_pos = stream->getBe32();
config->guest_write_pos = stream->getBe32();
config->transfer_mode = stream->getBe32();
config->transfer_size = stream->getBe32();
config->in_error = stream->getBe32();
}
void SaveAsgContext(Stream* stream, const struct asg_context& context) {
stream->write(context.to_host, sizeof(struct ring_buffer));
stream->write(context.to_host_large_xfer.ring, sizeof(struct ring_buffer));
stream->write(context.from_host_large_xfer.ring, sizeof(struct ring_buffer));
stream->putBe32(context.buffer_size);
stream->write(context.buffer, context.buffer_size);
}
void LoadAsgContext(Stream* stream, struct asg_context* context) {
stream->read(context->to_host, sizeof(struct ring_buffer));
stream->read(context->to_host_large_xfer.ring, sizeof(struct ring_buffer));
stream->read(context->from_host_large_xfer.ring, sizeof(struct ring_buffer));
context->buffer_size = stream->getBe32();
stream->read(context->buffer, context->buffer_size);
}
} // namespace
RingStream::RingStream(const AsgConsumerCreateInfo& info, size_t bufsize) :
IOStream(bufsize),
mContext(CreateContext(info)),
mSavedRingConfig(*mContext.ring_config),
mCallbacks(info.callbacks),
mBufSize(info.buffer_size) {}
RingStream::~RingStream() = default;
void RingStream::reloadRingConfig() {
*mContext.ring_config = mSavedRingConfig;
}
void* RingStream::allocBuffer(size_t minSize) {
if (mWriteBuffer.size() < minSize) {
mWriteBuffer.resize_noinit(minSize);
}
return mWriteBuffer.data();
}
int RingStream::commitBuffer(size_t size) {
size_t sent = 0;
auto data = mWriteBuffer.data();
size_t iters = 0;
size_t backedOffIters = 0;
const size_t kBackoffIters = 10000000ULL;
while (sent < size) {
++iters;
auto avail = ring_buffer_available_write(
mContext.from_host_large_xfer.ring,
&mContext.from_host_large_xfer.view);
// Check if the guest process crashed.
if (!avail) {
if (*(mContext.host_state) == ASG_HOST_STATE_EXIT) {
return sent;
} else {
ring_buffer_yield();
if (iters > kBackoffIters) {
gfxstream::base::sleepUs(10);
++backedOffIters;
}
}
continue;
}
auto remaining = size - sent;
auto todo = remaining < avail ? remaining : avail;
ring_buffer_view_write(
mContext.from_host_large_xfer.ring,
&mContext.from_host_large_xfer.view,
data + sent, todo, 1);
sent += todo;
}
if (backedOffIters > 0) {
GFXSTREAM_WARNING(
"Backed off %zu times to avoid overloading the guest system. This "
"may indicate resource constraints or performance issues.",
backedOffIters);
}
return sent;
}
const unsigned char* RingStream::readRaw(void* buf, size_t* inout_len) {
size_t wanted = *inout_len;
size_t count = 0U;
auto dst = static_cast<char*>(buf);
uint32_t ringAvailable = 0;
uint32_t ringLargeXferAvailable = 0;
const uint32_t maxSpins = 30;
uint32_t spins = 0;
bool inLargeXfer = true;
*(mContext.host_state) = ASG_HOST_STATE_CAN_CONSUME;
while (count < wanted) {
if (mReadBufferLeft) {
size_t avail = std::min<size_t>(wanted - count, mReadBufferLeft);
memcpy(dst + count,
mReadBuffer.data() + (mReadBuffer.size() - mReadBufferLeft),
avail);
count += avail;
mReadBufferLeft -= avail;
continue;
}
mReadBuffer.clear();
// no read buffer left...
if (count > 0) { // There is some data to return.
break;
}
*(mContext.host_state) = ASG_HOST_STATE_CAN_CONSUME;
if (mShouldExit) {
return nullptr;
}
ringAvailable =
ring_buffer_available_read(mContext.to_host, 0);
ringLargeXferAvailable =
ring_buffer_available_read(
mContext.to_host_large_xfer.ring,
&mContext.to_host_large_xfer.view);
auto current = dst + count;
auto ptrEnd = dst + wanted;
if (ringAvailable) {
inLargeXfer = false;
uint32_t transferMode =
mContext.ring_config->transfer_mode;
switch (transferMode) {
case 1:
type1Read(ringAvailable, dst, &count, ¤t, ptrEnd);
break;
case 2:
type2Read(ringAvailable, &count, ¤t, ptrEnd);
break;
case 3:
// emugl::emugl_crash_reporter(
// "Guest should never set to "
// "transfer mode 3 with ringAvailable != 0\n");
default:
// emugl::emugl_crash_reporter(
// "Unknown transfer mode %u\n",
// transferMode);
break;
}
} else if (ringLargeXferAvailable) {
type3Read(ringLargeXferAvailable,
&count, ¤t, ptrEnd);
inLargeXfer = true;
if (0 == __atomic_load_n(&mContext.ring_config->transfer_size, __ATOMIC_ACQUIRE)) {
inLargeXfer = false;
}
} else {
if (inLargeXfer && 0 != __atomic_load_n(&mContext.ring_config->transfer_size, __ATOMIC_ACQUIRE)) {
continue;
}
if (inLargeXfer && 0 == __atomic_load_n(&mContext.ring_config->transfer_size, __ATOMIC_ACQUIRE)) {
inLargeXfer = false;
}
if (++spins < maxSpins) {
ring_buffer_yield();
continue;
} else {
spins = 0;
}
if (mShouldExit) {
return nullptr;
}
if (mShouldExitForSnapshot && mInSnapshotOperation) {
return nullptr;
}
++mUnavailableReadCount;
if (mUnavailableReadCount >= kMaxUnavailableReads) {
*(mContext.host_state) = ASG_HOST_STATE_NEED_NOTIFY;
bool sleeping = false;
do {
const AsgOnUnavailableReadStatus status = mCallbacks.onUnavailableRead();
switch (status) {
case AsgOnUnavailableReadStatus::kContinue: {
*(mContext.host_state) = ASG_HOST_STATE_CAN_CONSUME;
break;
}
case AsgOnUnavailableReadStatus::kExit: {
*(mContext.host_state) = ASG_HOST_STATE_EXIT;
mShouldExit = true;
break;
}
case AsgOnUnavailableReadStatus::kSleep: {
sleeping = true;
break;
}
case AsgOnUnavailableReadStatus::kPauseForSnapshot: {
mShouldExitForSnapshot = true;
break;
}
case AsgOnUnavailableReadStatus::kResumeAfterSnapshot: {
mShouldExitForSnapshot = false;
break;
}
}
} while (sleeping);
}
continue;
}
}
*inout_len = count;
++mXmits;
mTotalRecv += count;
*(mContext.host_state) = ASG_HOST_STATE_RENDERING;
return (const unsigned char*)buf;
}
void RingStream::type1Read(
uint32_t available,
char* begin,
size_t* count, char** current, const char* ptrEnd) {
uint32_t xferTotal = available / sizeof(struct asg_type1_xfer);
if (mType1Xfers.size() < xferTotal) {
mType1Xfers.resize(xferTotal * 2);
}
auto xfersPtr = mType1Xfers.data();
ring_buffer_copy_contents(
mContext.to_host, 0, xferTotal * sizeof(struct asg_type1_xfer), (uint8_t*)xfersPtr);
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunreachable-code-loop-increment"
#endif // __clang__
for (uint32_t i = 0; i < xferTotal; ++i) {
const asg_type1_xfer& xfer = xfersPtr[i];
const uint32_t buffer_size = mContext.buffer_size;
// Guest controls offset/size via shared memory; validate against the
// host-allocated auxiliary buffer before dereferencing.
if (xfer.offset >= buffer_size || xfer.size > buffer_size - xfer.offset) {
GFXSTREAM_ERROR("Invalid type1 xfer: offset %u, size %u, buffer_size %u\n",
xfer.offset, xfer.size, buffer_size);
__atomic_store_n(&mContext.ring_config->in_error, 1, __ATOMIC_RELEASE);
return;
}
if (*current + xfer.size > ptrEnd) {
// Save in a temp buffer or we'll get stuck
if (begin == *current && i == 0) {
const char* src = mContext.buffer + xfer.offset;
mReadBuffer.resize_noinit(xfer.size);
memcpy(mReadBuffer.data(), src, xfer.size);
mReadBufferLeft = xfer.size;
ring_buffer_advance_read(
mContext.to_host, sizeof(struct asg_type1_xfer), 1);
__atomic_fetch_add(&mContext.ring_config->host_consumed_pos, xfersPtr[i].size, __ATOMIC_RELEASE);
}
return;
}
const char* src = mContext.buffer + xfer.offset;
memcpy(*current, src, xfer.size);
ring_buffer_advance_read(
mContext.to_host, sizeof(struct asg_type1_xfer), 1);
__atomic_fetch_add(&mContext.ring_config->host_consumed_pos, xfer.size, __ATOMIC_RELEASE);
*current += xfer.size;
*count += xfer.size;
// TODO: Figure out why running multiple xfers here can result in data
// corruption and remove clang diagnostic block.
return;
}
#ifdef __clang__
#pragma clang diagnostic pop
#endif // __clang__
}
void RingStream::type2Read(
uint32_t available,
size_t* count, char** current,const char* ptrEnd) {
GFXSTREAM_FATAL("nyi. abort");
uint32_t xferTotal = available / sizeof(struct asg_type2_xfer);
if (mType2Xfers.size() < xferTotal) {
mType2Xfers.resize(xferTotal * 2);
}
auto xfersPtr = mType2Xfers.data();
ring_buffer_copy_contents(
mContext.to_host, 0, available, (uint8_t*)xfersPtr);
for (uint32_t i = 0; i < xferTotal; ++i) {
if (*current + xfersPtr[i].size > ptrEnd) return;
const char* src =
mCallbacks.getPtr(xfersPtr[i].physAddr);
memcpy(*current, src, xfersPtr[i].size);
ring_buffer_advance_read(
mContext.to_host, sizeof(struct asg_type1_xfer), 1);
*current += xfersPtr[i].size;
*count += xfersPtr[i].size;
}
}
void RingStream::type3Read(
uint32_t available,
size_t* count, char** current, const char* ptrEnd) {
uint32_t xferTotal = __atomic_load_n(&mContext.ring_config->transfer_size, __ATOMIC_ACQUIRE);
uint32_t maxCanRead = ptrEnd - *current;
uint32_t ringAvail = available;
uint32_t actuallyRead = std::min(ringAvail, std::min(xferTotal, maxCanRead));
// Decrement transfer_size before letting the guest proceed in ring_buffer funcs or we will race
// to the next time the guest sets transfer_size
__atomic_fetch_sub(&mContext.ring_config->transfer_size, actuallyRead, __ATOMIC_RELEASE);
ring_buffer_read_fully_with_abort(
mContext.to_host_large_xfer.ring,
&mContext.to_host_large_xfer.view,
*current, actuallyRead,
1, &mContext.ring_config->in_error);
*current += actuallyRead;
*count += actuallyRead;
}
void* RingStream::getDmaForReading(uint64_t guest_paddr) {
return gfxstream::host::g_gfxstream_dma_get_host_addr(guest_paddr);
}
void RingStream::unlockDma(uint64_t guest_paddr) {
gfxstream::host::g_gfxstream_dma_unlock(guest_paddr);
}
int RingStream::writeFully(const void* buf, size_t len) {
void* dstBuf = alloc(len);
memcpy(dstBuf, buf, len);
flush();
return 0;
}
const unsigned char *RingStream::readFully( void *buf, size_t len) {
GFXSTREAM_FATAL("not intended for use with RingStream");
return nullptr;
}
void RingStream::onSave(gfxstream::Stream* stream) {
stream->putBe32(mReadBufferLeft);
stream->write(mReadBuffer.data() + mReadBuffer.size() - mReadBufferLeft,
mReadBufferLeft);
gfxstream::host::saveBuffer(stream, mWriteBuffer);
stream->putBe32(mUnavailableReadCount);
mSavedRingConfig = *mContext.ring_config;
SaveRingConfig(stream, mSavedRingConfig);
SaveAsgContext(stream, mContext);
}
unsigned char* RingStream::onLoad(gfxstream::Stream* stream) {
gfxstream::host::loadBuffer(stream, &mReadBuffer);
mReadBufferLeft = mReadBuffer.size();
gfxstream::host::loadBuffer(stream, &mWriteBuffer);
mUnavailableReadCount = stream->getBe32();
LoadRingConfig(stream, &mSavedRingConfig);
LoadAsgContext(stream, &mContext);
return reinterpret_cast<unsigned char*>(mWriteBuffer.data());
}
} // namespace gfxstream