summaryrefslogtreecommitdiffstats
path: root/userspace/lib/rpc_emulator.c
blob: 9f7f2c7a908148559ccd93fc0eea6a6f09eee428 (plain)
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
 * GSP vGPU-IO Userspace RPC Emulator
 * 
 * Emulates missing vGPU RPC handlers (BAR_TRAP, WPR, etc.) that exist only
 * in GRID host driver GSP firmware. Allows operation with consumer/OpenRM GSP.
 */

#define _GNU_SOURCE
#include "rpc_emulator.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>

/* ============================================================
 * BAR Emulation State
 * ============================================================ */

struct emu_bar_state {
    uint64_t aperture_offset;
    uint64_t aperture_size;
    bool force_access;
    uint32_t force_vf;
    void *backing_store;
    size_t backing_size;
    pthread_mutex_t lock;
};

static struct emu_bar_state g_bar_state = {
    .aperture_offset = 0,
    .aperture_size = 0,
    .force_access = false,
    .force_vf = 0,
    .backing_store = NULL,
    .backing_size = 0,
};

/* ============================================================
 * WPR Emulation State
 * ============================================================ */

struct emu_wpr_region {
    uint64_t base;
    uint64_t size;
    uint32_t flags;
    uint64_t timeout_ns;
    struct emu_wpr_region *next;
};

struct emu_wpr_state {
    struct emu_wpr_region *regions;
    pthread_mutex_t lock;
};

static struct emu_wpr_state g_wpr_state = {
    .regions = NULL,
};

/* ============================================================
 * Emulation Mode Control
 * ============================================================ */

static bool g_emulation_enabled = false;
static pthread_mutex_t g_emu_lock = PTHREAD_MUTEX_INITIALIZER;

void rpc_emu_enable(bool enable)
{
    pthread_mutex_lock(&g_emu_lock);
    g_emulation_enabled = enable;
    pthread_mutex_unlock(&g_emu_lock);
}

bool rpc_emu_is_enabled(void)
{
    bool enabled;
    pthread_mutex_lock(&g_emu_lock);
    enabled = g_emulation_enabled;
    pthread_mutex_unlock(&g_emu_lock);
    return enabled;
}

/* ============================================================
 * BAR Emulation Implementation
 * ============================================================ */

int rpc_emu_bar_config(uint32_t vf_id, uint64_t offset, uint64_t size)
{
    if (!rpc_emu_is_enabled())
        return -ENOTSUP;

    if (offset & 0xFFF || size & 0xFFF)
        return -EINVAL;

    if (size > 0x40000)  /* 256KB max per firmware spec */
        return -EINVAL;

    pthread_mutex_lock(&g_bar_state.lock);

    g_bar_state.aperture_offset = offset;
    g_bar_state.aperture_size = size;

    /* Allocate/resize backing store */
    if (g_bar_state.backing_store) {
        free(g_bar_state.backing_store);
        g_bar_state.backing_store = NULL;
        g_bar_state.backing_size = 0;
    }

    if (size > 0) {
        g_bar_state.backing_size = size;
        g_bar_state.backing_store = calloc(1, size);
        if (!g_bar_state.backing_store) {
            g_bar_state.backing_size = 0;
            pthread_mutex_unlock(&g_bar_state.lock);
            return -ENOMEM;
        }
    }

    pthread_mutex_unlock(&g_bar_state.lock);
    return 0;
}

int rpc_emu_bar_force_access(uint32_t vf_id, bool enable)
{
    if (!rpc_emu_is_enabled())
        return -ENOTSUP;

    pthread_mutex_lock(&g_bar_state.lock);
    g_bar_state.force_access = enable;
    g_bar_state.force_vf = enable ? vf_id : 0;
    pthread_mutex_unlock(&g_bar_state.lock);
    return 0;
}

int rpc_emu_bar_save(uint32_t vf_id, void *buf, size_t *size)
{
    if (!rpc_emu_is_enabled())
        return -ENOTSUP;

    if (!buf || !size)
        return -EINVAL;

    pthread_mutex_lock(&g_bar_state.lock);

    size_t needed = sizeof(struct emu_bar_state) + g_bar_state.backing_size;
    if (*size < needed) {
        *size = needed;
        pthread_mutex_unlock(&g_bar_state.lock);
        return -ENOSPC;
    }

    memcpy(buf, &g_bar_state, sizeof(struct emu_bar_state));
    if (g_bar_state.backing_store && g_bar_state.backing_size > 0)
        memcpy(buf + sizeof(struct emu_bar_state), g_bar_state.backing_store, g_bar_state.backing_size);

    *size = needed;
    pthread_mutex_unlock(&g_bar_state.lock);
    return 0;
}

int rpc_emu_bar_restore(uint32_t vf_id, const void *buf, size_t size)
{
    if (!rpc_emu_is_enabled())
        return -ENOTSUP;

    if (!buf || size < sizeof(struct emu_bar_state))
        return -EINVAL;

    pthread_mutex_lock(&g_bar_state.lock);

    memcpy(&g_bar_state, buf, sizeof(struct emu_bar_state));

    if (size > sizeof(struct emu_bar_state) && g_bar_state.backing_size > 0) {
        if (!g_bar_state.backing_store) {
            g_bar_state.backing_store = calloc(1, g_bar_state.backing_size);
            if (!g_bar_state.backing_store) {
                pthread_mutex_unlock(&g_bar_state.lock);
                return -ENOMEM;
            }
        }
        memcpy(g_bar_state.backing_store, buf + sizeof(struct emu_bar_state),
               size - sizeof(struct emu_bar_state));
    }

    pthread_mutex_unlock(&g_bar_state.lock);
    return 0;
}

int rpc_emu_bar_get_state(uint32_t vf_id, uint64_t *offset, uint64_t *size)
{
    if (!rpc_emu_is_enabled())
        return -ENOTSUP;

    pthread_mutex_lock(&g_bar_state.lock);
    if (offset) *offset = g_bar_state.aperture_offset;
    if (size) *size = g_bar_state.aperture_size;
    pthread_mutex_unlock(&g_bar_state.lock);
    return 0;
}

/* ============================================================
 * WPR Emulation Implementation
 * ============================================================ */

static struct emu_wpr_region *emu_wpr_find(uint64_t base)
{
    struct emu_wpr_region *rgn = g_wpr_state.regions;
    while (rgn) {
        if (rgn->base == base)
            return rgn;
        rgn = rgn->next;
    }
    return NULL;
}

static struct emu_wpr_region *emu_wpr_find_containing(uint64_t addr)
{
    struct emu_wpr_region *rgn = g_wpr_state.regions;
    while (rgn) {
        if (addr >= rgn->base && addr < rgn->base + rgn->size)
            return rgn;
        rgn = rgn->next;
    }
    return NULL;
}

int rpc_emu_wpr_protect_write(uint32_t vf_id, uint64_t base, uint64_t size)
{
    return rpc_emu_wpr_protect(vf_id, base, size, VGPU_WPR_WRITE);
}

int rpc_emu_wpr_protect_read(uint32_t vf_id, uint64_t base, uint64_t size)
{
    return rpc_emu_wpr_protect(vf_id, base, size, VGPU_WPR_READ);
}

int rpc_emu_wpr_protect(uint32_t vf_id, uint64_t base, uint64_t size, uint32_t flags)
{
    if (!rpc_emu_is_enabled())
        return -ENOTSUP;

    if (base & 0xFFF || size & 0xFFF)
        return -EINVAL;

    pthread_mutex_lock(&g_wpr_state.lock);

    /* Check for overlap */
    struct emu_wpr_region *rgn = g_wpr_state.regions;
    while (rgn) {
        if (base < rgn->base + rgn->size && base + size > rgn->base) {
            pthread_mutex_unlock(&g_wpr_state.lock);
            return -EBUSY;  /* Overlaps existing region */
        }
        rgn = rgn->next;
    }

    struct emu_wpr_region *new_rgn = calloc(1, sizeof(*new_rgn));
    if (!new_rgn) {
        pthread_mutex_unlock(&g_wpr_state.lock);
        return -ENOMEM;
    }

    new_rgn->base = base;
    new_rgn->size = size;
    new_rgn->flags = flags;
    new_rgn->timeout_ns = 10000000;  /* 10ms default */

    new_rgn->next = g_wpr_state.regions;
    g_wpr_state.regions = new_rgn;

    pthread_mutex_unlock(&g_wpr_state.lock);
    return 0;
}

int rpc_emu_wpr_unprotect(uint32_t vf_id, uint64_t base)
{
    if (!rpc_emu_is_enabled())
        return -ENOTSUP;

    pthread_mutex_lock(&g_wpr_state.lock);

    struct emu_wpr_region **prev = &g_wpr_state.regions;
    struct emu_wpr_region *rgn = g_wpr_state.regions;
    bool found = false;

    while (rgn) {
        if (rgn->base == base) {
            *prev = rgn->next;
            free(rgn);
            found = true;
            break;
        }
        prev = &rgn->next;
        rgn = rgn->next;
    }

    pthread_mutex_unlock(&g_wpr_state.lock);
    return found ? 0 : -ENOENT;
}

int rpc_emu_wpr_set_timeout(uint32_t vf_id, uint64_t timeout_ns)
{
    if (!rpc_emu_is_enabled())
        return -ENOTSUP;

    pthread_mutex_lock(&g_wpr_state.lock);
    struct emu_wpr_region *rgn = g_wpr_state.regions;
    while (rgn) {
        rgn->timeout_ns = timeout_ns;
        rgn = rgn->next;
    }
    pthread_mutex_unlock(&g_wpr_state.lock);
    return 0;
}

int rpc_emu_wpr_check_access(uint32_t vf_id, uint64_t addr, uint32_t access_type)
{
    if (!rpc_emu_is_enabled())
        return 0;  /* Allow if emulation disabled */

    pthread_mutex_lock(&g_wpr_state.lock);
    struct emu_wpr_region *rgn = emu_wpr_find_containing(addr);
    int ret = 0;

    if (rgn) {
        if ((access_type == VGPU_WPR_WRITE && (rgn->flags & VGPU_WPR_WRITE)) ||
            (access_type == VGPU_WPR_READ && (rgn->flags & VGPU_WPR_READ))) {
            ret = -EPERM;  /* Access violation */
        }
    }

    pthread_mutex_unlock(&g_wpr_state.lock);
    return 0;
}

/* ============================================================
 * Scheduling Emulation Implementation
 * ============================================================ */

struct emu_sched_work {
    uint64_t work_handle;
    uint64_t callback;
    uint64_t arg;
    uint32_t priority;
    uint32_t flags;
    struct emu_sched_work *next;
};

static struct emu_sched_work *g_sched_queue = NULL;
static pthread_mutex_t g_sched_lock = PTHREAD_MUTEX_INITIALIZER;

int rpc_emu_schedule(uint32_t vf_id, uint64_t work_handle, uint64_t callback, uint64_t arg, uint32_t priority, uint32_t flags)
{
    if (!rpc_emu_is_enabled())
        return -ENOTSUP;

    struct emu_sched_work *work = calloc(1, sizeof(*work));
    if (!work)
        return -ENOMEM;

    work->work_handle = work_handle;
    work->callback = callback;
    work->arg = arg;
    work->priority = priority;
    work->flags = flags;

    pthread_mutex_lock(&g_sched_lock);
    work->next = g_sched_queue;
    g_sched_queue = work;
    pthread_mutex_unlock(&g_sched_lock);

    /* In emulation, we just return success - work is queued locally */
    (void)vf_id;
    return 0;
}

int rpc_emu_yield(uint32_t vf_id)
{
    if (!rpc_emu_is_enabled())
        return -ENOTSUP;

    /* In emulation, yield is a no-op */
    (void)vf_id;
    return 0;
}

/* ============================================================
 * Notification Emulation Implementation
 * ============================================================ */

struct emu_notify_cb {
    uint32_t type;
    rpc_emu_notify_cb_t callback;
    void *ctx;
    struct emu_notify_cb *next;
};

static struct emu_notify_cb *g_notify_callbacks = NULL;
static pthread_mutex_t g_notify_lock = PTHREAD_MUTEX_INITIALIZER;

int rpc_emu_notify_register(uint32_t vf_id, uint32_t type, rpc_emu_notify_cb_t cb, void *ctx)
{
    if (!rpc_emu_is_enabled())
        return -ENOTSUP;

    struct emu_notify_cb *nc = calloc(1, sizeof(*nc));
    if (!nc)
        return -ENOMEM;

    nc->type = type;
    nc->callback = cb;
    nc->ctx = ctx;

    pthread_mutex_lock(&g_notify_lock);
    nc->next = g_notify_callbacks;
    g_notify_callbacks = nc;
    pthread_mutex_unlock(&g_notify_lock);

    (void)vf_id;
    return 0;
}

int rpc_emu_notify_unregister(uint32_t vf_id, uint32_t type)
{
    if (!rpc_emu_is_enabled())
        return -ENOTSUP;

    pthread_mutex_lock(&g_notify_lock);
    struct emu_notify_cb **prev = &g_notify_callbacks;
    struct emu_notify_cb *nc = g_notify_callbacks;
    bool found = false;

    while (nc) {
        if (nc->type == type) {
            *prev = nc->next;
            free(nc);
            found = true;
            break;
        }
        prev = &nc->next;
        nc = nc->next;
    }
    pthread_mutex_unlock(&g_notify_lock);

    (void)vf_id;
    return found ? 0 : -ENOENT;
}

int rpc_emu_notify_send(uint32_t vf_id, uint32_t type, void *data, size_t size)
{
    if (!rpc_emu_is_enabled())
        return -ENOTSUP;

    pthread_mutex_lock(&g_notify_lock);
    struct emu_notify_cb *nc = g_notify_callbacks;
    while (nc) {
        if (nc->type == type || nc->type == 0) {
            nc->callback(vf_id, type, data, size, nc->ctx);
        }
        nc = nc->next;
    }
    pthread_mutex_unlock(&g_notify_lock);

    return 0;
}

/* ============================================================
 * Initialization / Cleanup
 * ============================================================ */

int rpc_emu_init(void)
{
    pthread_mutex_init(&g_bar_state.lock, NULL);
    pthread_mutex_init(&g_wpr_state.lock, NULL);
    pthread_mutex_init(&g_sched_lock, NULL);
    pthread_mutex_init(&g_notify_lock, NULL);
    return 0;
}

void rpc_emu_fini(void)
{
    pthread_mutex_lock(&g_bar_state.lock);
    if (g_bar_state.backing_store) {
        free(g_bar_state.backing_store);
        g_bar_state.backing_store = NULL;
        g_bar_state.backing_size = 0;
    }
    pthread_mutex_unlock(&g_bar_state.lock);
    pthread_mutex_destroy(&g_bar_state.lock);

    pthread_mutex_lock(&g_wpr_state.lock);
    struct emu_wpr_region *rgn = g_wpr_state.regions;
    while (rgn) {
        struct emu_wpr_region *next = rgn->next;
        free(rgn);
        rgn = next;
    }
    g_wpr_state.regions = NULL;
    pthread_mutex_unlock(&g_wpr_state.lock);
    pthread_mutex_destroy(&g_wpr_state.lock);

    pthread_mutex_lock(&g_sched_lock);
    struct emu_sched_work *work = g_sched_queue;
    while (work) {
        struct emu_sched_work *next = work->next;
        free(work);
        work = next;
    }
    g_sched_queue = NULL;
    pthread_mutex_unlock(&g_sched_lock);
    pthread_mutex_destroy(&g_sched_lock);

    pthread_mutex_lock(&g_notify_lock);
    struct emu_notify_cb *nc = g_notify_callbacks;
    while (nc) {
        struct emu_notify_cb *next = nc->next;
        free(nc);
        nc = next;
    }
    g_notify_callbacks = NULL;
    pthread_mutex_unlock(&g_notify_lock);
    pthread_mutex_destroy(&g_notify_lock);

    pthread_mutex_destroy(&g_emu_lock);
}