-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagplib.c
More file actions
854 lines (761 loc) · 23.4 KB
/
agplib.c
File metadata and controls
854 lines (761 loc) · 23.4 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
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <linux/types.h>
#include "agpgart.h"
#include <asm/mtrr.h>
#include <errno.h>
#include <pwd.h>
#include <signal.h>
#include "agplib.h"
/* Global pointer to gart. */
unsigned char *gart;
int gartfd;
int device_type = 0;
int agpgartSupportsGetMap = TEST_GET_MAP_SUPPORT;
agp_map reserved_map;
static int coverRangeWithMTRR(int base, int range, int type)
{
int count;
int mtrr;
if ((mtrr = open("/proc/mtrr", O_WRONLY, 0)) == -1) {
if (errno == ENOENT) {
perror("/proc/mtrr not found: MTRR not enabled\n");
} else {
perror("Error opening /proc/mtrr:");
perror("MTRR not enabled\n");
}
mtrr = -1;
}
/* set it if we aren't just checking the number */
if (mtrr != -1 && type != -1 ) {
struct mtrr_sentry sentry;
sentry.base = base;
sentry.size = range;
sentry.type = type;
if(ioctl(mtrr, MTRRIOC_ADD_ENTRY, &sentry) == -1 ) {
perror("Error during ioctl MTRR_ADD_ENTRY\n");
exit(1);
}
}
}
/* Call xchg and cpuid asm instructions to flush the write combining cache.
* Could be replaced with sfence on some cpus or perhaps just the code
* for mb() from the kernel
*/
void flushWriteCombining(void)
{
int xchangeDummy;
__asm__ volatile(" push %%eax ; "
" xchg %%eax, %0 ;"
" pop %%eax" : : "m" (xchangeDummy));
__asm__ volatile(" push %%eax ; "
" push %%ebx ; "
" push %%ecx ; "
" push %%edx ; "
" movl $0,%%eax ; "
" cpuid ; "
" pop %%edx ; "
" pop %%ecx ; "
" pop %%ebx ; "
" pop %%eax" : /* no outputs */ : /* no inputs */ );
}
int bindMemory(unsigned long key, int page)
{
agp_bind bind;
bind.key = key;
bind.pg_start = page;
#ifdef DEBUG
printf("Using AGPIOC_BIND\n");
#endif
if(ioctl(gartfd, AGPIOC_BIND, &bind) != 0) {
perror("ioctl(AGPIOC_BIND)");
exit(1);
}
return 0;
}
int unbindMemory(unsigned long key)
{
agp_unbind unbind;
unbind.key = key;
#ifdef DEBUG
printf("Using AGPIOC_UNBIND\n");
#endif
if(ioctl(gartfd, AGPIOC_UNBIND, &unbind) != 0) {
perror("ioctl(AGPIOC_UNBIND)");
exit(1);
}
return 0;
}
unsigned long allocMemory(int size)
{
agp_allocate entry;
entry.type = 0;
entry.pg_count = size;
#ifdef DEBUG
printf("Using AGPIOC_ALLOCATE\n");
#endif
if(ioctl(gartfd, AGPIOC_ALLOCATE, &entry) != 0) {
perror("ioctl(AGPIOC_ALLOCATE)");
exit(1);
}
return (unsigned long)entry.key;
}
unsigned long allocAndBind(int page, int size)
{
unsigned long key = allocMemory(size);
bindMemory(key, page);
return key;
}
int freeMemory(unsigned long key)
{
#ifdef DEBUG
printf("Using AGPIOC_DEALLOCATE\n");
#endif
if(ioctl(gartfd, AGPIOC_DEALLOCATE, key) != 0) {
perror("ioctl(AGPIOCREMOVE)");
exit(1);
}
return 0;
}
static void getAgpInfo(agp_info *info)
{
#ifdef DEBUG
printf("Using AGPIOC_INFO\n");
#endif
if(ioctl(gartfd, AGPIOC_INFO, info) != 0) {
perror("ioctl(AGPIOC_INFO)");
exit(1);
}
}
int getCurrentPages(void)
{
agp_info info;
getAgpInfo(&info);
return info.pg_used;
}
static int openGartDevice(int flags)
{
gartfd = open("/dev/agpgart", flags);
if (gartfd == -1) {
perror("Open of /dev/agpgart failed!");
exit(1);
}
return AGPGART_DEVICE;
}
int openAgpDevice(int flags, int argc, char **argv)
{
return openGartDevice(flags);
}
size_t getApertureSize(void)
{
agp_info info;
getAgpInfo(&info);
return info.aper_size;
}
static void unmapAgpAperture(size_t aper_size)
{
if(gartfd != -1) {
munmap(gart, aper_size);
gart = (char *)0xffffffff;
close(gartfd);
gartfd = -1;
}
}
static void mapAgpAperture(size_t aper_size, int prot)
{
gart = mmap(NULL, aper_size * 0x100000,
prot, MAP_SHARED, gartfd, 0);
#ifdef DEBUG
if(gart == (unsigned char *)0xffffffff) {
perror("mmap failed with ");
}
#endif
}
int supportsGetMap(void)
{
if(agpgartSupportsGetMap == TEST_GET_MAP_SUPPORT) {
memset(&reserved_map, 0, sizeof(reserved_map));
reserved_map.key = AGP_RESERVED_KEY;
if(ioctl(gartfd, AGPIOC_GETMAP, &reserved_map) == -1 &&
errno == ENOTTY) {
agpgartSupportsGetMap = FALSE;
} else {
agpgartSupportsGetMap = TRUE;
}
}
return agpgartSupportsGetMap;
}
int checkPageAvailable(int page)
{
agp_map map;
off_t pg_start;
off_t pg_end;
/* If we don't support get map, the page is available */
if(supportsGetMap() == FALSE) return TRUE;
/* Reserved map is of zero size or reserved region not bound, so page
* is available */
if(reserved_map.page_count == 0 ||
reserved_map.is_bound == FALSE) return TRUE;
/* If we are equal to or greater than the first page but
* less then the first page + page count then we are within the
* reserved region. This page isn't available. Otherwise it
* is available for use.
*/
pg_start = reserved_map.pg_start;
pg_end = reserved_map.pg_start + reserved_map.page_count - 1;
if(pg_start <= page &&
page <= pg_end) return FALSE;
return TRUE;
}
/* Only call if supportsGetMap returns TRUE */
int printReserved(void)
{
printf("Reserved agp memory block: \n");
printf("bound: %s, ", reserved_map.is_bound ? "yes" : "no");
printf("offset: 0x%08x, ", reserved_map.pg_start * AGP_PAGE_SIZE);
printf("size: 0x%08x\n", reserved_map.page_count * AGP_PAGE_SIZE);
}
/* Returns the size of the aperture in megabytes */
int initializeAgp(void)
{
agp_info info;
agp_setup setup;
#ifdef DEBUG
printf("Using AGPIOC_ACQUIRE\n");
#endif
if(ioctl(gartfd, AGPIOC_ACQUIRE) != 0) {
perror("ioctl(AGPIOC_ACQUIRE)");
exit(1);
}
getAgpInfo(&info);
printf("Basic information extracted from /dev/agpgart:\n");
printf("Agpgart Interface Version: %i.%i\n",
info.version.major,
info.version.minor);
printf("Bridge pci id: 0x%lx\n", info.bridge_id);
printf("Agp mode: 0x%lx\n", info.agp_mode);
printf("Aperture base: 0x%lx\n", info.aper_base);
printf("Aperture size: %iMB\n", info.aper_size);
printf("Pages allowed total: %i\n", info.pg_total);
printf("Pages allowed from memory: %i\n", info.pg_system);
printf("Pages currently used: %i\n", info.pg_used);
coverRangeWithMTRR(info.aper_base, info.aper_size * 0x100000,
MTRR_TYPE_WRCOMB);
mapAgpAperture(info.aper_size, PROT_READ | PROT_WRITE);
gart = mmap(NULL, info.aper_size * 0x100000,
PROT_READ | PROT_WRITE, MAP_SHARED, gartfd, 0);
if(gart == (unsigned char *) 0xffffffff) {
close(gartfd);
exit(1);
}
setup.agp_mode = info.agp_mode;
#ifdef DEBUG
printf("Using AGPIOC_SETUP\n");
#endif
if(ioctl(gartfd, AGPIOC_SETUP, &setup) != 0) {
perror("ioctl(AGPIOC_SETUP)");
exit(1);
}
if(supportsGetMap() == TRUE) {
printReserved();
}
return info.aper_size;
}
/* A set of tests of the agp 3.0 functionality if it is available
* from this /dev/agpgart.
*/
void *agp_alloc_driver_info(int ctx)
{
agp_query_request rq;
agp_driver_info *drv;
rq.ctx = ctx;
if(ioctl(gartfd, AGPIOC_QUERY_SIZE, &rq) != 0) {
perror("query_size");
return NULL;
}
drv = malloc(rq.size);
return drv;
}
int agp_copy_driver_info(int ctx, void *buffer)
{
agp_query_request rq;
rq.ctx = ctx;
rq.buffer = (void *)buffer;
if(ioctl(gartfd, AGPIOC_QUERY_CTX, &rq) != 0) {
perror("query ctx");
return errno;
}
return 0;
}
static void agp_print_one_master(agp_master *info)
{
printf("\nAgp Master Information:\n");
printf("Agp version %d.%d\n", info->agp_major_version,
info->agp_minor_version);
printf("Request Depth : %d\n", info->num_requests_enqueue);
printf("Pci Vender ID : 0x%04x\n", info->master_pci_id >> 16);
printf("Pci Device ID : 0x%04x\n", info->master_pci_id & 0xffff);
if(info->agp_major_version >= 3) {
printf("Calibration cycle : %d ms\n", info->calibration_cycle_ms);
if(info->flags & AGP_SUPPORTS_AGP_3_0_ENABLED) {
printf("Agp Modes Supported : %s%s\n",
(info->flags & AGP_SUPPORTS_SPEED_4X) ? "4X " : "",
(info->flags & AGP_SUPPORTS_SPEED_8X) ? "8X " : "");
} else {
printf("Agp Modes Supported : %s%s%s\n",
(info->flags & AGP_SUPPORTS_SPEED_1X) ? "1X " : "",
(info->flags & AGP_SUPPORTS_SPEED_2X) ? "2X " : "",
(info->flags & AGP_SUPPORTS_SPEED_4X) ? "4X " : "");
}
printf("Supports isochronous operation mode : %s\n",
(info->flags & AGP_SUPPORTS_ISOCHRONOUS) ? "true" : "false");
printf("Supports Sideband addressing : %s\n",
(info->flags & AGP_SUPPORTS_SBA) ? "true" : "false");
printf("Supports Fast write : %s\n",
(info->flags & AGP_SUPPORTS_FAST_WRITE) ? "true" : "false");
printf("Supports over 4G addressing : %s\n",
(info->flags & AGP_SUPPORTS_OVER4G_ADDR) ? "true" : "false");
} else {
printf("Agp Modes Supported : %s%s%s\n",
(info->flags & AGP_SUPPORTS_SPEED_1X) ? "1X " : "",
(info->flags & AGP_SUPPORTS_SPEED_2X) ? "2X " : "",
(info->flags & AGP_SUPPORTS_SPEED_4X) ? "4X " : "");
printf("Supports Sideband addressing : %s\n",
(info->flags & AGP_SUPPORTS_SBA) ? "true" : "false");
printf("Supports Fast write : %s\n",
(info->flags & AGP_SUPPORTS_FAST_WRITE) ? "true" : "false");
printf("Supports over 4G addressing : %s\n",
(info->flags & AGP_SUPPORTS_OVER4G_ADDR) ? "true" : "false");
}
}
void agp_output_driver_info(void *ptr)
{
agp_driver_info *info = (agp_driver_info *)ptr;
agp_master *masters = info->masters;
int i, num_masters = info->num_masters;
printf("Agp Driver Name : %s\n", info->driver_name);
printf("Agp context id : %d\n", info->context_id);
printf("Agp page size : %d\n", 1 << info->agp_page_shift);
printf("Alloc page size : %d\n", 1 << info->alloc_page_shift);
printf("Agp page mask : 0x%lx\n", info->agp_page_mask);
printf("Alloc page mask : 0x%lx\n", info->alloc_page_mask);
printf("Maximum system pages for Agp : %d\n", info->max_system_pages);
printf("Current system pages used by Agp : %d\n", info->current_memory);
printf("\nAgp Target Information:\n");
printf("Agp version %d.%d\n", info->agp_major_version,
info->agp_minor_version);
printf("Pci Vender ID : 0x%04x\n", info->target_pci_id >> 16);
printf("Pci Device ID : 0x%04x\n", info->target_pci_id & 0xffff);
printf("Agp aperture base : 0x%lx\n", info->aper_base);
printf("Agp aperture size (MB) : %d\n", info->aper_size);
printf("Request Depth : %d\n", info->num_requests_enqueue);
if(info->agp_major_version >= 3) {
printf("Optimum agp request size : %d\n", info->optimum_request_size);
printf("Calibration cycle : %d ms\n", info->calibration_cycle_ms);
if(info->target_flags & AGP_SUPPORTS_AGP_3_0_ENABLED) {
printf("Agp Modes Supported : %s%s\n",
(info->target_flags & AGP_SUPPORTS_SPEED_4X) ? "4X " : "",
(info->target_flags & AGP_SUPPORTS_SPEED_8X) ? "8X " : "");
} else {
printf("Agp Modes Supported : %s%s%s\n",
(info->target_flags & AGP_SUPPORTS_SPEED_1X) ? "1X " : "",
(info->target_flags & AGP_SUPPORTS_SPEED_2X) ? "2X " : "",
(info->target_flags & AGP_SUPPORTS_SPEED_4X) ? "4X " : "");
}
printf("Supports isochronous operation mode : %s\n",
(info->target_flags & AGP_SUPPORTS_ISOCHRONOUS) ?
"true" : "false");
printf("Supports cached memory accesses : %s\n",
(info->target_flags & AGP_SUPPORTS_CACHED_MEMORY) ?
"true" : "false");
printf("Supports Sideband addressing : %s\n",
(info->target_flags & AGP_SUPPORTS_SBA) ? "true" : "false");
printf("Supports Fast write : %s\n",
(info->target_flags & AGP_SUPPORTS_FAST_WRITE) ?
"true" : "false");
printf("Supports over 4G addressing : %s\n",
(info->target_flags & AGP_SUPPORTS_OVER4G_ADDR) ?
"true" : "false");
printf("Supports directly mapping the agp aperture : %s\n",
(info->target_flags & AGP_SUPPORTS_APER_MMAP) ?
"true" : "false");
} else {
printf("Agp Modes Supported : %s%s%s\n",
(info->target_flags & AGP_SUPPORTS_SPEED_1X) ? "1X " : "",
(info->target_flags & AGP_SUPPORTS_SPEED_2X) ? "2X " : "",
(info->target_flags & AGP_SUPPORTS_SPEED_4X) ? "4X " : "");
printf("Supports Sideband addressing : %s\n",
(info->target_flags & AGP_SUPPORTS_SBA) ? "true" : "false");
printf("Supports Fast write : %s\n",
(info->target_flags & AGP_SUPPORTS_FAST_WRITE) ?
"true" : "false");
printf("Supports over 4G addressing : %s\n",
(info->target_flags & AGP_SUPPORTS_OVER4G_ADDR) ?
"true" : "false");
printf("Supports directly mapping the agp aperture : %s\n",
(info->target_flags & AGP_SUPPORTS_APER_MMAP) ?
"true" : "false");
}
printf("Number of detected agp masters : %d\n", num_masters);
if(num_masters) {
agp_print_one_master(masters);
masters++;
}
}
unsigned char *agp_map_memory(unsigned long key, off_t pg_ofs, size_t pages,
unsigned long prot, unsigned long flags)
{
agp_map_request rq;
rq.key = key;
rq.pg_start = pg_ofs;
rq.page_count = pages;
rq.prot = prot;
rq.flags = flags;
if(ioctl(gartfd, AGPIOC_MAP, &rq) != 0) {
perror("map ioctl");
return NULL;
}
return (unsigned char *)rq.addr;
}
int agp_unmap_memory(int key, unsigned char *ptr)
{
agp_map_request rq;
rq.key = key;
rq.addr = (unsigned long)ptr;
if(ioctl(gartfd, AGPIOC_UNMAP, &rq) != 0) {
perror("unmap ioctl");
return errno;
}
return 0;
}
int agp_get_num_contexts(void)
{
int num_contexts;
num_contexts = ioctl(gartfd, AGPIOC_NUM_CTXS);
return num_contexts;
}
int agp_change_context(int ctx)
{
int ret_val;
ret_val = ioctl(gartfd, AGPIOC_CHG_CTX, 0);
if(ret_val == -1) return errno;
return ret_val;
}
void agp_close_and_release(void)
{
if(ioctl(gartfd, AGPIOC_RELEASE) != 0) {
perror("ioctl(AGPIOC_RELEASE)");
exit(1);
}
close(gartfd);
}
/* agpgart specific tests */
#define CLIENTS_TO_TEST 2
#define CLIENT_SLEEP_PERIOD 3
static int initializeClient(agp_region *region)
{
#ifdef DEBUG
printf("Using AGPIOC_RESERVE\n");
#endif
if(ioctl(gartfd, AGPIOC_RESERVE, region) != 0) {
perror("ioctl(AGPIOC_RESERVE)");
exit(1);
}
return 0;
}
static void clientTestOne(int aper_size, int create_segments)
{
unsigned long apertureSize = aper_size * 0x100000;
int numberPages = apertureSize / AGP_PAGE_SIZE;
pid_t clients[CLIENTS_TO_TEST];
struct passwd *userNobody;
agp_region region;
agp_segment segment;
int passed = 1;
int i;
region.seg_count = 1;
region.seg_list = &segment;
segment.pg_start = 0;
segment.pg_count = numberPages;
segment.prot = PROT_READ;
userNobody = getpwnam("nobody");
if(!userNobody) {
printf("failed!\n");
printf("Can not perform client test since user nobody can't be found\n");
return;
}
for(i = 0; i < CLIENTS_TO_TEST; i++) {
pid_t cPid = fork();
if(cPid == 0) {
/* Client path */
/* Just test to see if the client could map the aperture */
unmapAgpAperture(aper_size);
setuid(userNobody->pw_uid);
sleep(CLIENT_SLEEP_PERIOD);
openGartDevice(O_RDONLY);
mapAgpAperture(aper_size, PROT_READ);
if(gart == (unsigned char *)0xffffffff) {
exit(1);
} else {
exit(0);
}
} else if(cPid == -1) {
/* Error path */
int k;
for(k = 0; k < i; k++) {
kill(clients[k], SIGKILL);
}
printf("failed!\n");
printf("Couldn't create enough clients\n");
return;
} else {
/* Normal Path */
clients[i] = cPid;
}
}
/* Let the clients do their thing */
sleep(1);
if(create_segments == 1) {
/* Setup the segments with the proper pids */
for(i = 0; i < CLIENTS_TO_TEST; i++) {
region.pid = clients[i];
initializeClient(®ion);
}
}
for(i = 0; i < CLIENTS_TO_TEST; i++) {
int status;
waitpid(clients[i], &status, 0);
if(WIFEXITED(status) && WEXITSTATUS(status) != 0) {
/* Failure */
if(create_segments && passed) {
printf("failed!\n");
printf("%d failed to map the agp aperture\n", clients[i]);
}
passed = 0;
}
}
if(passed && create_segments) printf("passed.\n");
if(!passed && !create_segments) printf("passed.\n");
if(passed && !create_segments) printf("failed.\n");
}
void clientTest(int aper_size)
{
printf("\nClient Permissions Test:\n");
printf("\tThis test tests the ability of /dev/agpgart to mmap the "
"aperture\n");
printf("into a clients process space.\n");
printf("\tThis test will fail if the user nobody can't read the\n");
printf("/dev/agpgart device. This test only tests read mappings\n");
printf("since most installations have set permissions as to only allow "
"reads.\n");
printf("The first test checks to see if permission setting works. "
" While\n");
printf("the second test checks to see if the mmap is blocked properly.\n");
printf("\nNow peforming client permissions test: ");
fflush(stdout);
clientTestOne(aper_size, 1);
printf("\nNow testing permission failure case: ");
fflush(stdout);
clientTestOne(aper_size, 0);
}
void testMemoryFailures(int aper_size)
{
unsigned long apertureSize = aper_size * 0x100000;
int numberPages = apertureSize / AGP_PAGE_SIZE;
int allocKey1, allocKey2, allocKey3;
int firstPage = 0;
agp_bind bind;
/* This test set check for some bad things are handled properly. */
allocKey1 = allocMemory(1);
allocKey2 = allocMemory(2);
allocKey3 = allocMemory(3);
printf("\nMemory Ioctl Sanity Test:\n");
printf("\tThis set of tests checks that the proper error values are "
"returned\n");
printf("from /dev/agpgart when several incorrect requests are performed.\n");
/* Testing double freeing */
printf("\nNow testing double freeing a block of agp memory: ");
fflush(stdout);
freeMemory(allocKey3);
if(ioctl(gartfd, AGPIOC_DEALLOCATE, allocKey3) != -1 &&
errno != EINVAL) {
printf("failed!\n");
printf("Deallocate ioctl didn't return expected error value.\n");
} else {
printf("passed.\n");
}
/* Testing inserting memory past the aperture end */
bind.key = allocKey1;
bind.pg_start = numberPages;
printf("Now testing binding a single page past the agp aperture end: ");
fflush(stdout);
if(ioctl(gartfd, AGPIOC_BIND, &bind) != -1 &&
errno != EINVAL) {
printf("failed!\n");
printf("Bind ioctl didn't return expected error value.\n");
printf("Inserting past the end of the agp aperture didn't fail\n");
printf("properly.\n");
} else {
printf("passed.\n");
}
bind.key = allocKey2;
bind.pg_start = numberPages - 1;
printf("Now testing bind with a block lying inside and outside: ");
fflush(stdout);
if(ioctl(gartfd, AGPIOC_BIND, &bind) != -1 &&
errno != EINVAL) {
printf("failed!\n");
printf("Bind ioctl didn't return expected error value.\n");
printf("Inserting past the end of the agp aperture didn't fail\n");
printf("properly for two pages.\n");
} else {
printf("passed.\n");
}
/* Test several busy conditions */
firstPage = findFirstRegionOfSize(2);
if(firstPage == -1) {
printf("Fatal error, no regions of 2 pages inside aperture, exiting\n");
exit(1);
}
bind.key = allocKey1;
bind.pg_start = firstPage;
bindMemory(allocKey1, firstPage);
printf("Now testing double binding of the same block of memory: ");
fflush(stdout);
if(ioctl(gartfd, AGPIOC_BIND, &bind) != -1 &&
errno != EINVAL) {
printf("failed!\n");
printf("Bind ioctl didn't return the expected error value.\n");
} else {
printf("passed.\n");
}
bind.key = allocKey2;
bind.pg_start = firstPage;
printf("Now testing binding another block of memory to the same place: ");
fflush(stdout);
if(ioctl(gartfd, AGPIOC_BIND, &bind) != -1 &&
errno != EBUSY) {
printf("failed!\n");
printf("Bind ioctl didn't return a busy condition as expected.\n");
} else {
printf("passed\n");
}
unbindMemory(allocKey1);
bindMemory(allocKey1, firstPage + 1);
printf("Now testing binding a block of memory within anothers bounds: ");
fflush(stdout);
if(ioctl(gartfd, AGPIOC_BIND, &bind) != -1 &&
errno != EBUSY) {
printf("failed!\n");
printf("Bind ioctl didn't return a busy condition as expected.\n");
} else {
printf("passed\n");
}
freeMemory(allocKey1);
printf("Now testing if free automatically unbound a bound page: ");
if(ioctl(gartfd, AGPIOC_BIND, &bind) != 0) {
printf("failed!\n");
perror("Bind shouldn't have failed. Reason");
} else {
printf("passed\n");
}
freeMemory(allocKey2);
}
#define RECYCLE_KEYS_TO_TEST 10
/* Test key recycling mechanism to make sure its working correctly. */
void keyRecycleTest(void)
{
int keys[RECYCLE_KEYS_TO_TEST];
int newKeys[3];
int midpt, start, end;
int i;
printf("\nKey Recycle Test:\n");
printf("This test insures that the key recycling is functioning "
"properly.\n");
printf("This is needed to insure that an Xserver can continue to "
"recycle\n");
printf("and not leak keys, since there are only a finite amount.\n");
printf("\nNow peforming key recycle test: ");
for(i = 0; i < RECYCLE_KEYS_TO_TEST; i++) {
int key = allocMemory(1);
if(key < 0) {
printf("failed!\n");
printf("Failed to allocate key to test with.\n");
return;
}
keys[i] = key;
}
/* Hold onto the keys values */
midpt = keys[RECYCLE_KEYS_TO_TEST / 2];
start = keys[0];
end = keys[RECYCLE_KEYS_TO_TEST - 1];
freeMemory(midpt);
freeMemory(start);
freeMemory(end);
for(i = 0; i < 3; i++) {
int key = allocMemory(1);
if(key < 0) {
printf("failed!\n");
printf("Failed to allocate key to test with.\n");
return;
}
newKeys[i] = key;
}
if(start != newKeys[0] ||
midpt != newKeys[1] ||
end != newKeys[2]) {
printf("failed!\n");
} else {
printf("passed.\n");
}
keys[RECYCLE_KEYS_TO_TEST / 2] = newKeys[1];
keys[0] = newKeys[0];
keys[RECYCLE_KEYS_TO_TEST - 1] = newKeys[2];
for(i = 0; i < RECYCLE_KEYS_TO_TEST; i++) {
freeMemory(keys[i]);
}
printf("\n");
}
static int getNumberOfPages(void)
{
unsigned long apertureSize = getApertureSize() * 0x100000;
int numberPages = apertureSize / AGP_PAGE_SIZE;
return numberPages;
}
/* Very stupid brute force approach to finding a slot of size,
* if we want this to be fast we need to make a memory allocator of some
* kind. This test code doesn't have to be terribly efficent so we
* don't care.
* This function only handles the reserved memory, nothing else at the
* moment. Its all we need so don't worry.
*
* returns -1 if we can't find such a region.
*/
int findFirstRegionOfSize(int numberPages)
{
int pagesTotal = getNumberOfPages();
int i, k;
for(i = 0; i < pagesTotal; i++) {
if(checkPageAvailable(i) == TRUE) {
/* We already know the first page is available */
for(k = 1; k < numberPages; k++) {
if(checkPageAvailable(i + k) == FALSE) break;
}
/* Full region is available, return i as the index */
if(k == numberPages) return i;
/* Okay we didn't get the full region and i + k is not available,
* so we continue testing at i + k + 1
*/
i = i + k + 1;
}
}
return -1;
}