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
|
/*
* GSP vGPU-IO Driver - Comprehensive Test Suite
* Tests all driver components: RPC, VF, BAR, WPR, Migration
* Compiles against kernel headers only (no driver dependency)
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/ktime.h>
#include <linux/atomic.h>
#include <linux/list.h>
#include <linux/idr.h>
#include <linux/mutex.h>
#include <linux/kref.h>
#include <linux/completion.h>
#include <linux/wait.h>
#include <linux/spinlock.h>
#include <linux/debugfs.h>
#include <linux/vmalloc.h>
#include <gsp_core.h>
static int test_count = 0;
static int pass_count = 0;
static int fail_count = 0;
#define TEST_ASSERT(cond, msg) \
do { \
test_count++; \
if (cond) { \
pass_count++; \
pr_info("PASS: %s\n", msg); \
} else { \
fail_count++; \
pr_err("FAIL: %s\n", msg); \
} \
} while (0)
#define TEST_ASSERT_EQ(a, b, msg) \
TEST_ASSERT((a) == (b), msg " (expected " #b ", got " #a ")")
#define TEST_ASSERT_NE(a, b, msg) \
TEST_ASSERT((a) != (b), msg " (expected not " #b ", got " #a ")")
/* ============================================================
* Test: Kernel API Availability
* ============================================================ */
static int test_kernel_apis(void)
{
pr_info("=== Testing Kernel API Availability ===\n");
/* Test kref */
struct kref *ref = kmalloc(sizeof(*ref), GFP_KERNEL);
TEST_ASSERT(ref != NULL, "kref allocation");
kref_init(ref);
TEST_ASSERT(kref_read(ref) == 1, "kref_init sets refcount to 1");
kref_get(ref);
TEST_ASSERT(kref_read(ref) == 2, "kref_get increments refcount");
kref_put(ref, (void (*)(struct kref *))kfree);
TEST_ASSERT(kref_read(ref) == 1, "kref_put decrements refcount");
kref_put(ref, (void (*)(struct kref *))kfree);
TEST_ASSERT(kref_read(ref) == 0, "kref_put reaches zero");
/* Test completion */
struct completion comp;
init_completion(&comp);
TEST_ASSERT(!completion_done(&comp), "completion initially not done");
complete(&comp);
TEST_ASSERT(completion_done(&comp), "completion marked done");
init_completion(&comp);
TEST_ASSERT(wait_for_completion_timeout(&comp, msecs_to_jiffies(100)) > 0,
"wait_for_completion_timeout works");
/* Test atomic64 */
atomic64_t a64 = ATOMIC64_INIT(0);
atomic64_inc(&a64);
TEST_ASSERT_EQ(atomic64_read(&a64), 1, "atomic64_inc");
atomic64_add(5, &a64);
TEST_ASSERT_EQ(atomic64_read(&a64), 6, "atomic64_add");
atomic64_sub(3, &a64);
TEST_ASSERT_EQ(atomic64_read(&a64), 3, "atomic64_sub");
/* Test mutex */
struct mutex mtx;
mutex_init(&mtx);
mutex_lock(&mtx);
TEST_ASSERT(mutex_is_locked(&mtx), "mutex_is_locked after lock");
mutex_unlock(&mtx);
TEST_ASSERT(!mutex_is_locked(&mtx), "mutex_is_locked after unlock");
/* Test spinlock */
spinlock_t sl = __SPIN_LOCK_UNLOCKED(sl);
unsigned long flags;
spin_lock_irqsave(&sl, flags);
TEST_ASSERT(spin_is_locked(&sl), "spin_is_locked after lock");
spin_unlock_irqrestore(&sl, flags);
TEST_ASSERT(!spin_is_locked(&sl), "spin_is_locked after unlock");
/* Test idr */
struct idr idr;
idr_init(&idr);
int id = idr_alloc(&idr, (void *)0xdeadbeef, 1, 100, GFP_KERNEL);
TEST_ASSERT(id >= 1 && id < 100, "idr_alloc returns valid id");
void *ptr = idr_find(&idr, id);
TEST_ASSERT(ptr == (void *)0xdeadbeef, "idr_find returns correct pointer");
idr_remove(&idr, id);
TEST_ASSERT(idr_find(&idr, id) == NULL, "idr_remove works");
idr_destroy(&idr);
/* Test list */
struct list_head head;
INIT_LIST_HEAD(&head);
TEST_ASSERT(list_empty(&head), "list_empty on new list");
struct list_head item;
INIT_LIST_HEAD(&item);
list_add(&item, &head);
TEST_ASSERT(!list_empty(&head), "list not empty after add");
list_del(&item);
TEST_ASSERT(list_empty(&head), "list empty after del");
/* Test ktime */
ktime_t start = ktime_get();
ktime_t end = ktime_get();
s64 diff = ktime_to_ns(ktime_sub(end, start));
TEST_ASSERT(diff >= 0, "ktime_get and ktime_to_ns work");
return 0;
}
/* ============================================================
* Test: Structure Sizes (from driver header)
* ============================================================ */
static int test_structure_sizes(void)
{
pr_info("=== Testing Structure Sizes ===\n");
/* These sizes must match driver/include/gsp_core.h */
TEST_ASSERT_EQ(sizeof(struct gsp_rpc_header), 64, "gsp_rpc_header size");
TEST_ASSERT_EQ(sizeof(struct gsp_rpc_message), 256, "gsp_rpc_message size");
TEST_ASSERT_EQ(sizeof(struct gsp_rpc_response), 32, "gsp_rpc_response size");
TEST_ASSERT_EQ(sizeof(struct gsp_alloc_payload), 40, "gsp_alloc_payload size");
TEST_ASSERT_EQ(sizeof(struct gsp_alloc_response), 24, "gsp_alloc_response size");
TEST_ASSERT_EQ(sizeof(struct gsp_pin_payload), 40, "gsp_pin_payload size");
TEST_ASSERT_EQ(sizeof(struct gsp_pin_response), 8, "gsp_pin_response size");
TEST_ASSERT_EQ(sizeof(struct gsp_bar_trap_payload), 40, "gsp_bar_trap_payload size");
TEST_ASSERT_EQ(sizeof(struct gsp_bar_trap_response), 8, "gsp_bar_trap_response size");
TEST_ASSERT_EQ(sizeof(struct gsp_migrate_payload), 56, "gsp_migrate_payload size");
TEST_ASSERT_EQ(sizeof(struct gsp_subcmd_desc), 8, "gsp_subcmd_desc size");
TEST_ASSERT_EQ(sizeof(struct gsp_vf_config), 56, "gsp_vf_config size");
return 0;
}
/* ============================================================
* Test: Enum Values (from driver header)
* ============================================================ */
static int test_enum_values(void)
{
pr_info("=== Testing Enum Values ===\n");
TEST_ASSERT_EQ(GSP_RPC_ALLOC, 0x01, "RPC_ALLOC");
TEST_ASSERT_EQ(GSP_RPC_FREE, 0x02, "RPC_FREE");
TEST_ASSERT_EQ(GSP_RPC_MAP, 0x03, "RPC_MAP");
TEST_ASSERT_EQ(GSP_RPC_UNMAP, 0x04, "RPC_UNMAP");
TEST_ASSERT_EQ(GSP_RPC_PIN, 0x05, "RPC_PIN");
TEST_ASSERT_EQ(GSP_RPC_UNPIN, 0x06, "RPC_UNPIN");
TEST_ASSERT_EQ(GSP_RPC_SCHEDULE, 0x07, "RPC_SCHEDULE");
TEST_ASSERT_EQ(GSP_RPC_YIELD, 0x08, "RPC_YIELD");
TEST_ASSERT_EQ(GSP_RPC_NOTIFY, 0x09, "RPC_NOTIFY");
TEST_ASSERT_EQ(GSP_RPC_BAR_TRAP, 0x0A, "RPC_BAR_TRAP");
TEST_ASSERT_EQ(GSP_RPC_MIGRATE, 0x0B, "RPC_MIGRATE");
TEST_ASSERT_EQ(GSP_RPC_DEBUG, 0x0C, "RPC_DEBUG");
TEST_ASSERT_EQ(GSP_MEM_VRAM, 0, "MEM_VRAM");
TEST_ASSERT_EQ(GSP_MEM_SYSMEM, 1, "MEM_SYSMEM");
TEST_ASSERT_EQ(GSP_MEM_UNCACHED, 2, "MEM_UNCACHED");
TEST_ASSERT_EQ(GSP_PIN_READ, (1 << 0), "PIN_READ");
TEST_ASSERT_EQ(GSP_PIN_WRITE, (1 << 1), "PIN_WRITE");
TEST_ASSERT_EQ(GSP_PIN_LONGTERM, (1 << 2), "PIN_LONGTERM");
TEST_ASSERT_EQ(GSP_MIG_QUERY, 0, "MIG_QUERY");
TEST_ASSERT_EQ(GSP_MIG_PREPARE, 1, "MIG_PREPARE");
TEST_ASSERT_EQ(GSP_MIG_TRANSFER, 2, "MIG_TRANSFER");
TEST_ASSERT_EQ(GSP_MIG_COMMIT, 3, "MIG_COMMIT");
TEST_ASSERT_EQ(GSP_MIG_CLEANUP, 4, "MIG_CLEANUP");
TEST_ASSERT_EQ(GSP_MIG_LIVE, (1 << 0), "MIG_LIVE");
TEST_ASSERT_EQ(GSP_MIG_POSTCOPY, (1 << 1), "MIG_POSTCOPY");
TEST_ASSERT_EQ(GSP_MIG_AUTO_COMMIT, (1 << 2), "MIG_AUTO_COMMIT");
TEST_ASSERT_EQ(GSP_MIG_VERIFY, (1 << 3), "MIG_VERIFY");
TEST_ASSERT_EQ(GSP_VF_CREATING, 0, "VF_CREATING");
TEST_ASSERT_EQ(GSP_VF_ACTIVE, 1, "VF_ACTIVE");
TEST_ASSERT_EQ(GSP_VF_MIGRATING, 2, "VF_MIGRATING");
TEST_ASSERT_EQ(GSP_VF_SUSPENDED, 3, "VF_SUSPENDED");
TEST_ASSERT_EQ(GSP_VF_DESTROYING, 4, "VF_DESTROYING");
return 0;
}
/* ============================================================
* Test: Error Code Mapping
* ============================================================ */
static int test_error_codes(void)
{
pr_info("=== Testing Error Codes ===\n");
TEST_ASSERT_EQ(GSP_OK, 0, "GSP_OK");
TEST_ASSERT_EQ(GSP_EPERM, 0xFFFFFFFF, "GSP_EPERM");
TEST_ASSERT_EQ(GSP_ENOENT, 0xFFFFFFFE, "GSP_ENOENT");
TEST_ASSERT_EQ(GSP_EINVAL, 0xFFFFFFFD, "GSP_EINVAL");
TEST_ASSERT_EQ(GSP_ENODEV, 0xFFFFFFFA, "GSP_ENODEV");
TEST_ASSERT_EQ(GSP_ENOMEM, 0xFFFFFFF4, "GSP_ENOMEM");
TEST_ASSERT_EQ(GSP_EBUSY, 0xFFFFFFEE, "GSP_EBUSY");
TEST_ASSERT_EQ(GSP_ENOSPC, 0xFFFFFFE4, "GSP_ENOSPC");
TEST_ASSERT_EQ(GSP_ETIMEDOUT, 0x0000001E, "GSP_ETIMEDOUT");
TEST_ASSERT_EQ(GSP_EOVERFLOW, 0x00000025, "GSP_EOVERFLOW");
TEST_ASSERT_EQ(GSP_EDEV, 0x0000001F, "GSP_EDEV");
return 0;
}
/* ============================================================
* Test: Firmware Constants
* ============================================================ */
static int test_firmware_constants(void)
{
pr_info("=== Testing Firmware Constants ===\n");
TEST_ASSERT_EQ(GSP_FW_BASE_ADDR, 0x04000000ULL, "FW_BASE_ADDR");
TEST_ASSERT_EQ(GSP_FW_SIZE, 0x02000000ULL, "FW_SIZE");
TEST_ASSERT_EQ(GSP_ENTRY_POINT, 0x04009000ULL, "ENTRY_POINT");
TEST_ASSERT_EQ(GSP_PRIMARY_DISPATCHER, 0x057A1AC0ULL, "PRIMARY_DISPATCHER");
TEST_ASSERT_EQ(GSP_SECONDARY_DISPATCHER, 0x04ED39DCULL, "SECONDARY_DISPATCHER");
TEST_ASSERT_EQ(GSP_RPC_JUMP_TABLE, 0x059BFEB0ULL, "RPC_JUMP_TABLE");
TEST_ASSERT_EQ(GSP_BAR0_DOORBELL, 0x1000, "BAR0_DOORBELL");
TEST_ASSERT_EQ(GSP_BAR0_MSIX, 0x2000, "BAR0_MSIX");
TEST_ASSERT_EQ(GSP_MSG_RING_SIZE, 256, "MSG_RING_SIZE");
TEST_ASSERT_EQ(GSP_MSG_MAX_SIZE, 256, "MSG_MAX_SIZE");
TEST_ASSERT_EQ(GSP_MAX_PENDING, 64, "MAX_PENDING");
return 0;
}
/* ============================================================
* Test: Migration State Enum (internal)
* ============================================================ */
static int test_migration_states(void)
{
pr_info("=== Testing Migration States ===\n");
TEST_ASSERT_EQ(GSP_MIG_S_IDLE, 0, "MIG_S_IDLE");
TEST_ASSERT_EQ(GSP_MIG_S_QUERY, 1, "MIG_S_QUERY");
TEST_ASSERT_EQ(GSP_MIG_S_PREPARE, 2, "MIG_S_PREPARE");
TEST_ASSERT_EQ(GSP_MIG_S_TRANSFER, 3, "MIG_S_TRANSFER");
TEST_ASSERT_EQ(GSP_MIG_S_COMMIT, 4, "MIG_S_COMMIT");
TEST_ASSERT_EQ(GSP_MIG_S_CLEANUP, 5, "MIG_S_CLEANUP");
TEST_ASSERT_EQ(GSP_MIG_S_FAILED, 6, "MIG_S_FAILED");
TEST_ASSERT_EQ(GSP_MIG_S_COMPLETED, 7, "MIG_S_COMPLETED");
return 0;
}
/* ============================================================
* Test: WPR Flags and Events
* ============================================================ */
static int test_wpr_flags(void)
{
pr_info("=== Testing WPR Flags ===\n");
TEST_ASSERT_EQ(GSP_WPR_WRITE, (1 << 0), "WPR_WRITE");
TEST_ASSERT_EQ(GSP_WPR_READ, (1 << 1), "WPR_READ");
TEST_ASSERT_EQ(GSP_WPR_EXECUTE, (1 << 2), "WPR_EXECUTE");
TEST_ASSERT_EQ(GSP_WPR_WRITE_COMPLETE, 0, "WPR_WRITE_COMPLETE");
TEST_ASSERT_EQ(GSP_WPR_READ_COMPLETE, 1, "WPR_READ_COMPLETE");
TEST_ASSERT_EQ(GSP_WPR_WRITE_TIMEOUT, 2, "WPR_WRITE_TIMEOUT");
TEST_ASSERT_EQ(GSP_WPR_READ_TIMEOUT, 3, "WPR_READ_TIMEOUT");
return 0;
}
/* ============================================================
* Test: Message Flags
* ============================================================ */
static int test_message_flags(void)
{
pr_info("=== Testing Message Flags ===\n");
TEST_ASSERT_EQ(GSP_MSG_SYNC, (1 << 0), "MSG_SYNC");
TEST_ASSERT_EQ(GSP_MSG_ASYNC, (1 << 1), "MSG_ASYNC");
TEST_ASSERT_EQ(GSP_MSG_NOREPLY, (1 << 2), "MSG_NOREPLY");
return 0;
}
/* ============================================================
* Test: BAR Trap Types
* ============================================================ */
static int test_bar_trap_types(void)
{
pr_info("=== Testing BAR Trap Types ===\n");
TEST_ASSERT_EQ(GSP_BAR_READ, 0, "BAR_READ");
TEST_ASSERT_EQ(GSP_BAR_WRITE, 1, "BAR_WRITE");
TEST_ASSERT_EQ(GSP_BAR_ATOMIC, 2, "BAR_ATOMIC");
return 0;
}
/* ============================================================
* Test: Memory Allocation Patterns
* ============================================================ */
static int test_memory_patterns(void)
{
pr_info("=== Testing Memory Allocation Patterns ===\n");
/* Test vzalloc/vfree */
void *ptr = vzalloc(4096);
TEST_ASSERT(ptr != NULL, "vzalloc allocates memory");
vfree(ptr);
/* Test kmalloc/kfree */
ptr = kmalloc(1024, GFP_KERNEL);
TEST_ASSERT(ptr != NULL, "kmalloc allocates memory");
kfree(ptr);
/* Test kcalloc */
ptr = kcalloc(10, 64, GFP_KERNEL);
TEST_ASSERT(ptr != NULL, "kcalloc allocates zeroed memory");
kfree(ptr);
return 0;
}
/* ============================================================
* Test: String Operations
* ============================================================ */
static int test_string_ops(void)
{
pr_info("=== Testing String Operations ===\n");
char buf[64];
int len = snprintf(buf, sizeof(buf), "test_%d", 42);
TEST_ASSERT(len > 0 && len < sizeof(buf), "snprintf works");
TEST_ASSERT(strcmp(buf, "test_42") == 0, "snprintf formats correctly");
char *dup = kstrdup("test_string", GFP_KERNEL);
TEST_ASSERT(dup != NULL, "kstrdup works");
TEST_ASSERT(strcmp(dup, "test_string") == 0, "kstrdup copies correctly");
kfree(dup);
return 0;
}
/* ============================================================
* Test Suite Runner
* ============================================================ */
static int run_all_tests(void)
{
pr_info("\n============================================================\n");
pr_info("GSP vGPU-IO Comprehensive Test Suite Starting\n");
pr_info("============================================================\n\n");
test_kernel_apis();
test_structure_sizes();
test_enum_values();
test_error_codes();
test_firmware_constants();
test_migration_states();
test_wpr_flags();
test_message_flags();
test_bar_trap_types();
test_memory_patterns();
test_string_ops();
pr_info("\n============================================================\n");
pr_info("TEST RESULTS: %d total, %d passed, %d failed\n",
test_count, pass_count, fail_count);
pr_info("============================================================\n");
if (fail_count == 0) {
pr_info("TEST SUITE PASSED\n");
return 0;
} else {
pr_err("TEST SUITE FAILED\n");
return -1;
}
}
/* ============================================================
* Module Init/Exit
* ============================================================ */
static int __init gsp_test_init(void)
{
return run_all_tests();
}
static void __exit gsp_test_exit(void)
{
pr_info("GSP vGPU-IO test module unloaded\n");
}
module_init(gsp_test_init);
module_exit(gsp_test_exit);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("NVIDIA vGPU-IO Team");
MODULE_DESCRIPTION("GSP vGPU-IO Comprehensive Test Suite");
MODULE_VERSION("1.0.0");
|