aboutsummaryrefslogtreecommitdiffstats
path: root/vmwsvxd.c
blob: 2958b4492e2bd622bb486a668422b01148165cce (plain) (blame)
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
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
/*****************************************************************************

Copyright (c) 2023 Jaroslav Hensl <emulator@emulace.cz>

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.

*****************************************************************************/

#define VXD32
#define SVGA

#include "winhack.h"
#include "vmm.h"
#include "vmwsvxd.h"

#include "svga_all.h"

#include "minivdd32.h"

#include "version.h"

#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif

#define PTONPAGE (PAGE_SIZE/sizeof(DWORD))

#if 0
/* dynamic VXDs don't using this init function in discardable segment  */

#pragma data_seg("_ITEXT", "ICODE")
#pragma code_seg("_ITEXT", "ICODE")

BOOL __cdecl VMWS_Sys_Critical_Init(DWORD hVM, DWORD dwRefData,
                                    PSTR pCmdTail, PCRS_32 pCRS)
{
  return TRUE;
}

BOOL __cdecl VMWS_Device_Init(DWORD hVM, PSTR pCmdTail, PCRS_32 pCRS)
{
  return TRUE;
}

#endif

#include "code32.h"

void VMWS_Control();
void VMWS_API_Entry();

/*
  VXD structure
  this variable must be in first address in code segment.
  In other cases VXD isn't loadable (WLINK bug?)
*/
DDB VMWS_DDB = {
	NULL,                       // must be NULL
	DDK_VERSION,                // DDK_Version
	VMWSVXD_DEVICE_ID,         // Device ID
	VMWSVXD_MAJOR_VER,         // Major Version
	VMWSVXD_MINOR_VER,         // Minor Version
	NULL,
	"VMWSVXD",
	VDD_Init_Order, //Undefined_Init_Order,
	(DWORD)VMWS_Control,
	(DWORD)VMWS_API_Entry,
	(DWORD)VMWS_API_Entry,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL, // DDB_Win32_Service_Table
	NULL, // prev
	sizeof(DDB)
};

/* string tables */
#ifdef DBGPRINT
char dbg_hello[] = "Hello world!\n";
char dbg_version[] = "VMM version: ";
char dbg_region_err[] = "region create error\n";

char dbg_Device_Init_proc[] = "Device_Init_proc\n";
char dbg_Device_Init_proc_succ[] = "Device_Init_proc success\n";
char dbg_dic_ring[] = "DeviceIOControl: Ring\n";
char dbg_dic_sync[] = "DeviceIOControl: Sync\n";
char dbg_dic_unknown[] = "DeviceIOControl: Unknown: %d\n";
char dbg_dic_system[] = "DeviceIOControl: System code: %d\n";
char dbg_get_ppa[] = "%lx -> %lx\n";
char dbg_get_ppa_beg[] = "Virtual: %lx\n";
char dbg_mob_allocate[] = "Allocated OTable row: %d\n";

char dbg_str[] = "%s\n";

char dbg_submitcb_fail[] = "CB submit FAILED\n";
char dbg_submitcb[] = "CB submit %d\n";
char dbg_lockcb[] = "Reused CB (%d) with status: %d\n";

char dbg_lockcb_lasterr[] = "Error command: %lX\n";

char dbg_cb_on[] = "CB supported and allocated\n";
char dbg_gb_on[] = "GB supported and allocated\n";
char dbg_cb_ena[] = "CB context 0 enabled\n";

char dbg_region_info_1[] = "Region id = %d\n";
char dbg_region_info_2[] = "Region address = %lX, PPN = %lX, GMRBLK = %lX\n";

char dbg_mapping[] = "Memory mapping:\n";
char dbg_mapping_map[] = "  %X -> %X\n";

static char dbg_siz[] = "Size of gSVGA(2) = %d %d\n";

#endif

typedef struct _otinfo_entry_t
{
	DWORD   phy;
	void   *lin;
	DWORD   size;
	DWORD   flags;
} otinfo_entry_t;

typedef struct _cmd_buf_t
{
	DWORD   phy;
	void   *lin;
	DWORD   status;
} cmd_buf_t;

#define CB_STATUS_EMPTY   0 /* buffer was not used yet */
#define CB_STATUS_LOCKED  1 /* buffer is using by Ring-3 proccess */
#define CB_STATUS_PROCESS 2 /* buffer is register by VGPU */

#define CB_COUNT 4

uint64 cmd_buf_next_id = {0, 0};

cmd_buf_t cmd_bufs[CB_COUNT] = {{0}};

int queued_cmd_buf = -1;

BOOL cb_support = FALSE;
BOOL gb_support = FALSE;
BOOL cb_context0 = FALSE;

#define ROUND_TO_PAGES(_cb)((((_cb) + PAGE_SIZE - 1)/PAGE_SIZE)*PAGE_SIZE)

#define SVGA3D_MAX_MOBS (SVGA3D_MAX_CONTEXT_IDS + SVGA3D_MAX_CONTEXT_IDS + SVGA3D_MAX_SURFACE_IDS)
#define VBOX_VIDEO_MAX_SCREENS 64

#define FLAG_ALLOCATED 1
#define FLAG_ACTIVE    2

otinfo_entry_t otable[SVGA_OTABLE_DX_MAX] = {
	{0, NULL, ROUND_TO_PAGES(SVGA3D_MAX_MOBS*sizeof(SVGAOTableMobEntry)),              0}, /* SVGA_OTABLE_MOB */
	{0, NULL, ROUND_TO_PAGES(SVGA3D_MAX_SURFACE_IDS*sizeof(SVGAOTableSurfaceEntry)),   0}, /* SVGA_OTABLE_SURFACE */
	{0, NULL, ROUND_TO_PAGES(SVGA3D_MAX_CONTEXT_IDS*sizeof(SVGAOTableContextEntry)),   0}, /* SVGA_OTABLE_CONTEXT */
	{0, NULL, 0,                                                                       0}, /* SVGA_OTABLE_SHADER - not used */
	{0, NULL, ROUND_TO_PAGES(VBOX_VIDEO_MAX_SCREENS*sizeof(SVGAOTableScreenTargetEntry)), 0}, /* SVGA_OTABLE_SCREENTARGET (VBOX_VIDEO_MAX_SCREENS) */
	{0, NULL, ROUND_TO_PAGES(SVGA3D_MAX_CONTEXT_IDS*sizeof(SVGAOTableDXContextEntry)), 0}, /* SVGA_OTABLE_DXCONTEXT */
};

DWORD *DispatchTable = 0;
DWORD DispatchTableLength = 0;

Bool svga_init_success = FALSE;

/**
 * VMM calls wrapers
 **/
DWORD Get_VMM_Version()
{
	static WORD ver;
	
	_asm push ecx
	_asm push eax
	_asm xor eax, eax
	VMMCall(Get_VMM_Version);
	_asm mov [ver],ax
	_asm pop ecx
	_asm pop eax
	
	return ver;
}
 
ULONG __declspec(naked) __cdecl _PageAllocate(ULONG nPages, ULONG pType, ULONG VM, ULONG AlignMask, ULONG minPhys, ULONG maxPhys, ULONG *PhysAddr, ULONG flags)
{
	VMMJmp(_PageAllocate);
}

ULONG __declspec(naked) __cdecl _PageFree(PVOID hMem, DWORD flags)
{
	VMMJmp(_PageFree);
}

ULONG __declspec(naked) __cdecl _CopyPageTable(ULONG LinPgNum, ULONG nPages, DWORD *PageBuf, ULONG flags)
{
	VMMJmp(_CopyPageTable);
}

ULONG __declspec(naked) __cdecl _LinPageLock(ULONG page, ULONG npages, ULONG flags)
{
	VMMJmp(_LinPageLock);
}

ULONG __declspec(naked) __cdecl _LinPageUnLock(ULONG page, ULONG npages, ULONG flags)
{
	VMMJmp(_LinPageUnLock);
}

ULONG __declspec(naked) __cdecl _MapPhysToLinear(ULONG PhysAddr, ULONG nBytes, ULONG flags)
{
	VMMJmp(_MapPhysToLinear);
}

/**
 * VDD calls wrapers
 **/
void VDD_Get_Mini_Dispatch_Table()
{
	VxDCall(VDD, Get_Mini_Dispatch_Table);
	_asm mov [DispatchTable],edi
	_asm mov [DispatchTableLength],ecx
}

/**
 * Control Handles
 **/
void Sys_Critical_Init_proc()
{
	// nop
}

/*
 * 32-bit DeviceIoControl ends here
 *
 */
#define SVGA_DBG             0x110B
#define SVGA_REGION_CREATE   0x1114
#define SVGA_REGION_FREE     0x1115
#define SVGA_SYNC            0x1116
#define SVGA_RING            0x1117
#define SVGA_ALLOCPHY        0x1200
#define SVGA_FREEPHY         0x1201
#define SVGA_OTABLE_QUERY    0x1202
#define SVGA_OTABLE_FLAGS    0x1203

#define SVGA_CB_LOCK         0x1204
#define SVGA_CB_SUBMIT       0x1205
#define SVGA_CB_SYNC         0x1206
#define SVGA_CB_SUBMIT_SYNC  0x1207

DWORD __stdcall Device_IO_Control_entry(struct DIOCParams *params);

void __declspec(naked) Device_IO_Control_proc()
{
	_asm {
		push esi /* struct DIOCParams */
		call Device_IO_Control_entry
		retn
	}
}

void Device_Init_proc();

/*
 * service module calls (init, exit, deviceIoControl, ...)
 * clear carry if succes (this is always success)
 */
void __declspec(naked) VMWS_Control()
{
	// eax = 0x00 - Sys Critical Init
	// eax = 0x01 - sys dynamic init
	// eax = 0x1B - dynamic init
	// eax = 0x1C - dynamic exit
	// eax = 0x23 - device IO control
	
	_asm {
		cmp eax,Sys_Critical_Init
		jnz control_1
			pushad
			call Sys_Critical_Init_proc
			popad
			clc
			ret
		control_1: 
		cmp eax,Device_Init
		jnz control_2
			pushad
		  call Device_Init_proc
			popad
			clc
			ret
		control_2:
		cmp eax,0x1B
		jnz control_3
			pushad
		  call Device_Init_proc
		  popad
			clc
			ret
		control_3:
		cmp eax,W32_DEVICEIOCONTROL
		jnz control_4
			jmp Device_IO_Control_proc
		control_4:
		clc
	  ret
	};
}

/**
 * Return PPN (physical page number) from virtual address
 * 
 **/
static DWORD getPPN(DWORD virtualaddr)
{
	DWORD phy = 0;
	_CopyPageTable(virtualaddr/P_SIZE, 1, &phy, 0);
	return phy/P_SIZE;
}

/**
 * Allocate guest memory region (GMR) - HW needs know memory physical
 * addressed of pages in (virtual) memory block.
 * Technically this allocate 2 memory block, 1st for data and 2nd as its
 * physical description.
 *
 * @param nPages: number of pages to allocate
 * @param outDataAddr: linear address (system space) of data block
 * @param outGMRAddr: linear address of GMR block
 * @param outPPN: physical page number of outGMRAddr (first page)
 * @param outMobAddr: linear address of MOB PT block - if is NOT null,
 *                    extra memory of MOB maybe allocated. If result 
 *                    is NULL, MOB is full flat (no PT needed).
 * @param outMobPPN: physical page number of MOB
 *
 * @return: TRUE on success
 *
 **/
static BOOL GMRAlloc(ULONG nPages, ULONG *outDataAddr, ULONG *outGMRAddr, ULONG *outPPN, 
	ULONG *outMobAddr, ULONG *outMobPPN)
{
	ULONG phy;
	ULONG pgblk_phy;
	ULONG laddr;
	ULONG pgblk;
	SVGAGuestMemDescriptor *desc;
	laddr = _PageAllocate(nPages, PG_SYS, 0, 0, 0x0, 0x100000, &phy, PAGECONTIG | PAGEUSEALIGN | PAGEFIXED);
	
	if(laddr)
	{
		pgblk = _PageAllocate(1, PG_SYS, 0, 0, 0x0, 0x100000, &pgblk_phy, PAGECONTIG | PAGEUSEALIGN | PAGEFIXED);
		if(pgblk)
		{
			desc = (SVGAGuestMemDescriptor*)pgblk;
			desc->ppn = (phy/P_SIZE);
			desc->numPages = nPages;
			desc++;
			desc->ppn = 0;
			desc->numPages = 0;
		
			*outDataAddr = laddr;
			*outGMRAddr = pgblk;
			*outPPN = (pgblk_phy/P_SIZE);
			
			if(outMobAddr)
				*outMobAddr = 0;
			
			if(outMobPPN)
				*outMobPPN = (phy/P_SIZE);

			return TRUE;
		}
		else
		{
			_PageFree((PVOID)laddr, 0);
			return FALSE;
		}
	}
	else
	{
		ULONG taddr;
		ULONG tppn;
		ULONG pgi;
		ULONG base_ppn;
		ULONG base_cnt;
		ULONG blocks = 1;
		ULONG blk_pages = 0;
		
		laddr = _PageAllocate(nPages, PG_SYS, 0, 0, 0x0, 0x100000, NULL, PAGEFIXED);
		
		if(laddr)
		{
			/* determine how many physical continuous blocks we have */
			base_ppn = getPPN(laddr);
			base_cnt = 1;
			for(pgi = 1; pgi < nPages; pgi++)
			{
				taddr = laddr + pgi*P_SIZE;
				tppn = getPPN(taddr);
				
				if(tppn != base_ppn + base_cnt)
				{
					base_ppn = tppn;
					base_cnt = 1;
					blocks++;
				}
				else
				{
					base_cnt++;
				}
			}
			
			// number of pages to store regions information
			blk_pages = ((blocks+1)/(P_SIZE/sizeof(SVGAGuestMemDescriptor))) + 1;
			
			// add extra descriptors for pages edge
			blk_pages = ((blocks+blk_pages+1)/(P_SIZE/sizeof(SVGAGuestMemDescriptor))) + 1;
			
			pgblk = _PageAllocate(blk_pages, PG_SYS, 0, 0, 0x0, 0x100000, &pgblk_phy, PAGECONTIG | PAGEUSEALIGN | PAGEFIXED);
			if(pgblk)
			{
				desc = (SVGAGuestMemDescriptor*)pgblk;
				desc->ppn = getPPN(laddr);
				desc->numPages = 1;
				blocks = 1;
				
				for(pgi = 1; pgi < nPages; pgi++)
				{
					taddr = laddr + pgi*P_SIZE;
					tppn = getPPN(taddr);
					
					if(tppn == desc->ppn + desc->numPages)
					{
						desc->numPages++;
					}
					else
					{
						/* next descriptor is on page edge */
						if((blocks+1) % (P_SIZE/sizeof(SVGAGuestMemDescriptor)) == 0)
						{
							desc++;
							desc->numPages = 0;
							desc->ppn = getPPN((DWORD)(desc+1));
							//dbg_printf(old_new_ppn, blocks, getPPN((DWORD)(desc)), getPPN((DWORD)(desc+1)), getPPN((DWORD)(desc+2)));
							blocks++;
						}
						
						desc++;
						desc->ppn = tppn;
						desc->numPages = 1;
						blocks++;
					}
				}
				
				desc++;
				desc->ppn = 0;
				desc->numPages = 0;
				
				*outDataAddr = laddr;
				*outGMRAddr = pgblk;
				*outPPN = (pgblk_phy/P_SIZE);
				
				if(outMobAddr)
				{
					DWORD mobphy;
					/* for simplicity we're always creating table of depth 2, first page is page of PPN pages */
					DWORD *mob = (DWORD *)_PageAllocate(1 + (nPages + PTONPAGE - 1)/PTONPAGE, PG_SYS, 0, 0, 0x0, 0x100000, &mobphy,
						PAGECONTIG | PAGEUSEALIGN | PAGEFIXED);
					
					if(mob)
					{
						int i;
						/* dim 1 */
						for(i = 0; i < (nPages + PTONPAGE - 1)/PTONPAGE; i++)
						{
							mob[i] = (mobphy/PAGE_SIZE) + i + 1;
						}
						
						/* dim 2 */
						for(i = 0; i < nPages; i++)
						{
							mob[PTONPAGE + i] = getPPN(laddr + i*PAGE_SIZE);
						}
						
						*outMobAddr = (DWORD)mob;
						if(outMobPPN) *outMobPPN = mobphy/PAGE_SIZE;
					}
					else
					{
						*outMobAddr = NULL;
						if(outMobPPN) *outMobPPN = getPPN(laddr);
					}
				}
				
				return TRUE;
			}
			else
			{
				_PageFree((PVOID)laddr, 0);
				return FALSE;
			}
		}
	}
	
	return FALSE;
}

/**
 * Free data allocated by GMRAlloc
 *
 **/
static BOOL GMRFree(ULONG DataAddr, ULONG GMRAddr, ULONG MobAddr)
{
	BOOL ret = FALSE;

	if(_PageFree((PVOID)DataAddr, 0) != 0)
	{
		ret = TRUE;
	}
	
	if(_PageFree((PVOID)GMRAddr, 0) != 0)
	{
		ret = TRUE;
	}
	
	if(MobAddr != 0)
	{
		_PageFree((PVOID)MobAddr, 0);
	}
	
	return ret;
}

/**
 * Allocate OTable for GB objects
 **/
static void AllocateOTable()
{
	int i;
	for(i = 0; i < SVGA_OTABLE_DX_MAX; i++)
	{
		otinfo_entry_t *entry = &otable[i];
		if(entry->size != 0 && (entry->flags & FLAG_ALLOCATED) == 0)
		{
			entry->lin = (void*)_PageAllocate(entry->size/PAGE_SIZE, PG_SYS, 0, 0, 0x0, 0x100000, &entry->phy, PAGECONTIG | PAGEUSEALIGN | PAGEFIXED);
			if(entry->lin)
			{
				dbg_printf(dbg_mob_allocate, i);
				entry->flags |= FLAG_ALLOCATED;
			}
		}
	}
}

/**
 * Allocate Command Buffers
 **/
static void AllocateCB()
{
	int i;
	for(i = 0; i < CB_COUNT; i++)
	{
		if(cmd_bufs[i].phy == 0)
		{
			cmd_bufs[i].lin = (void *)_PageAllocate((SVGA_CB_MAX_SIZE+PAGE_SIZE-1)/PAGE_SIZE, PG_SYS, 0, 0, 0x0, 0x100000, &(cmd_bufs[i].phy), PAGECONTIG | PAGEUSEALIGN | PAGEFIXED);
			cmd_bufs[i].status = CB_STATUS_EMPTY;
		}
	}
}

/**
 * Test if selected CB is not used
 **/
static BOOL isCBfree(int index)
{
	switch(cmd_bufs[index].status)
	{
		case CB_STATUS_EMPTY:
			return TRUE;
		case CB_STATUS_PROCESS:
		{
			SVGACBHeader *cb = (SVGACBHeader *)cmd_bufs[index].lin;
			if(cb->status > SVGA_CB_STATUS_NONE)
			{
				return TRUE;
			}
			break;
		}
	}
	
	return FALSE;
}

/**
 * Increment 64-bit ID of CB segment
 **/
static void nextID_CB()
{
	_asm
	{
		inc dword ptr [cmd_buf_next_id]
		adc dword ptr [cmd_buf_next_id+4], 0
	}
}

/**
 * Lock one of buffers and return ptr for RING-3
 **/
static void *LockCB()
{
	int index;
	for(index = 0; index < CB_COUNT; index++)
	{
		if(isCBfree(index))
		{
#ifdef DBGPRINT
			{
				SVGACBHeader *cb = (SVGACBHeader *)cmd_bufs[index].lin;
				if(cb->status > 1)
				{
					dbg_printf(dbg_lockcb, index, cb->status);
					
					if(cb->status == SVGA_CB_STATUS_COMMAND_ERROR)
					{
						DWORD *ptr = (DWORD*)(((unsigned char*)cmd_bufs[index].lin) + sizeof(SVGACBHeader) + cb->errorOffset);
						dbg_printf(dbg_lockcb_lasterr, *ptr);
					}
					
				}
			}
#endif
			cmd_bufs[index].status = CB_STATUS_LOCKED;
			
			if(queued_cmd_buf == index)
			{
				queued_cmd_buf = -1;
			}
			
			return cmd_bufs[index].lin;
		}
	}
	
	return NULL;
}

/**
 * Submit CB to GPU.
 * If length is 0, buffer not executed
 * cbctx_id = SVGA_CB_CONTEXT_0,...
 **/

static BOOL submitCB(void *ptr, DWORD cbctx_id, BOOL sync)
{
	int index;
	
	/* context 0 is turned on and of by driver and if it is off don't use it! */
	if(cb_context0 == FALSE && cbctx_id == SVGA_CB_CONTEXT_0)
	{
		return FALSE;
	}
	
	for(index = 0; index < CB_COUNT; index++)
	{
		if(cmd_bufs[index].lin == ptr)
		{
			SVGACBHeader *cb = (SVGACBHeader *)cmd_bufs[index].lin;
			if(cb->length > 0)
			{
				cb->status = SVGA_CB_STATUS_NONE;
				cb->ptr.pa.hi   = 0;
				cb->ptr.pa.low  = cmd_bufs[index].phy + sizeof(SVGACBHeader);
				cb->flags |= SVGA_CB_FLAG_NO_IRQ;
				cb->id.low = cmd_buf_next_id.low;
				cb->id.hi  = cmd_buf_next_id.hi;
				
#if 0
				if(queued_cmd_buf >= 0)
				{
					/* wait to complete the last command */
					SVGACBHeader *cb_queued = (SVGACBHeader *)cmd_bufs[queued_cmd_buf].lin;
					
					while(cb_queued->status == SVGA_CB_STATUS_NONE)
					{
						SVGA_WriteReg(SVGA_REG_SYNC, 1);
					}
				}
#endif
				
				SVGA_WriteReg(SVGA_REG_COMMAND_HIGH, 0); // high part of 64-bit memory address...
				SVGA_WriteReg(SVGA_REG_COMMAND_LOW, cmd_bufs[index].phy | cbctx_id);
				cmd_bufs[index].status = CB_STATUS_PROCESS;
				
				if(sync)
				{
					while(cb->status == SVGA_CB_STATUS_NONE)
					{
						SVGA_WriteReg(SVGA_REG_SYNC, 1);
					}
					queued_cmd_buf = -1;
				}
				else
				{
					queued_cmd_buf = index;
				}
				
				//dbg_printf(dbg_submitcb, cbctx_id);
				
				nextID_CB();
			}
			else
			{
				cmd_bufs[index].status = CB_STATUS_EMPTY;
			}
			
			return TRUE;
		}
	}
	
	dbg_printf(dbg_submitcb_fail);
	
	return FALSE;
}

/**
 * Wait until all CB are completed
 **/
static void syncCB()
{
	int index;
	BOOL synced;
	do
	{
		synced = TRUE;
		for(index = 0; index < CB_COUNT; index++)
		{
			switch(cmd_bufs[index].status)
			{
				case CB_STATUS_EMPTY:
				case CB_STATUS_LOCKED:
					break;
				case CB_STATUS_PROCESS:
				{
					SVGACBHeader *cb = (SVGACBHeader *)cmd_bufs[index].lin;
					if(cb->status == SVGA_CB_STATUS_NONE)
					{
						synced = FALSE;
					}
					break;
				}
			}
		}
		
		if(!synced)
		{
			SVGA_WriteReg(SVGA_REG_SYNC, 1);
		}
	} while(!synced);
}

/**
 * PM16 driver RING0 calls
 **/
BOOL CreateRegion(unsigned int nPages, ULONG *lpLAddr, ULONG *lpPPN, ULONG *lpPGBLK)
{	
	return GMRAlloc(nPages, lpLAddr, lpPGBLK, lpPPN, NULL, NULL);
}

BOOL FreeRegion(ULONG LAddr, ULONG PGBLK, ULONG MobAddr)
{
	return GMRFree(LAddr, PGBLK, MobAddr);
}

/* some CRT */
void memset(void *dst, int c, unsigned int size)
{
	unsigned int par;
	unsigned int i;
	unsigned int dw_count;
	unsigned int dw_size;
	
	c &= 0xFF;
	par = (c << 24) | (c << 16) | (c << 8) | c;
	dw_count = size >> 2;
	dw_size = size & 0xFFFFFFFCUL;
	
	for(i = 0; i < dw_count; i++)
	{
		((unsigned int*)dst)[i] = par; 
	}
	
	for(i = dw_size; i < size; i++)
	{
		((unsigned char*)dst)[i] = c; 
	}
}


#if 0
/* I'm using this sometimes for developing, not for end user! */
BOOL AllocPhysical(DWORD nPages, DWORD *outLinear, DWORD *outPhysical)
{
	*outLinear = _PageAllocate(nPages, PG_SYS, 0, 0, 0x0, 0x100000, outPhysical, PAGECONTIG | PAGEUSEALIGN | PAGEFIXED);
	
	if(*outLinear != NULL)
	{
		return TRUE;
	}
	
	return FALSE;
}

void FreePhysical(DWORD linear)
{
	_PageFree((void*)linear, 0);
}
#endif

#pragma pack(push)
#pragma pack(1)
typedef struct _cb_enable_t
{
	SVGACBHeader       cbheader;
	uint32             cmd;
	SVGADCCmdStartStop cbstart;
} cb_enable_t;
#pragma pack(pop)

/* process V86/PM16 calls */
WORD __stdcall VMWS_API_Proc(PCRS_32 state)
{
	WORD rc = 0xFFFF;
	WORD service = state->Client_EDX & 0xFFFF;
	switch(service)
	{
		case VMWSVXD_PM16_VERSION:
			rc = (VMWSVXD_MAJOR_VER << 8) | VMWSVXD_MINOR_VER;
			break;
		case VMWSVXD_PM16_VMM_VERSION:
			rc = Get_VMM_Version();
			break;
		/* create region = input: ECX - num_pages; output: EDX - lin. address of descriptor, ECX - PPN of descriptor */
		case VMWSVXD_PM16_CREATE_REGION:
		{
			ULONG lAddr;
			ULONG PPN;
			ULONG PGBLK;
				
			if(CreateRegion(state->Client_ECX, &lAddr, &PPN, &PGBLK))
			{
				state->Client_ECX = PPN;
				state->Client_EDX = lAddr;
				state->Client_EBX = PGBLK;
				rc = 1;
			}
			else
			{
				state->Client_ECX = 0;
				state->Client_EDX = 0;
				state->Client_EBX = 0;
				
				dbg_printf(dbg_region_err);
				
				rc = 0;
			}
			break;
		}
		/* free region = input: ECX: lin. address */
		case VMWSVXD_PM16_DESTROY_REGION: 
		{
			if(FreeRegion(state->Client_ECX, state->Client_EBX, 0))
			{
				rc = 1;
			}
			else
			{
				rc = 0;
			}
			break;			
		}
		/* clear memory on linear address (ESI) by defined size (ECX) */
		case VMWSVXD_PM16_ZEROMEM:
			memset((void*)state->Client_ESI, 0, state->Client_ECX);
			rc = 1;
			break;
		/* set ECX to actual api version */
		case VMWSVXD_PM16_APIVER:
			state->Client_ECX = DRV_API_LEVEL;
			rc = 1;
			break;
		case VMWSVXD_PM16_CB_START:
			if(cb_support)
			{
				cb_enable_t *cbe;
				do
				{
					cbe = LockCB();
				} while(cbe == NULL);
				memset(cbe, 0, sizeof(cb_enable_t));
				cbe->cbheader.length = sizeof(SVGADCCmdStartStop) + sizeof(uint32);
				cbe->cmd = SVGA_DC_CMD_START_STOP_CONTEXT;
				cbe->cbstart.enable  = 1;
				cbe->cbstart.context = SVGA_CB_CONTEXT_0;
				submitCB(cbe, SVGA_CB_CONTEXT_DEVICE, TRUE);

				dbg_printf(dbg_cb_ena);
				cb_context0 = TRUE;
			}
			rc = 1;
			break;
		case VMWSVXD_PM16_CB_STOP:
			if(cb_support)
			{
				cb_enable_t *cbe;
				cb_context0 = FALSE;
				
				syncCB();
				do
				{
					cbe = LockCB();
				} while(cbe == NULL);
				memset(cbe, 0, sizeof(cb_enable_t));
				cbe->cbheader.length = sizeof(SVGADCCmdStartStop) + sizeof(uint32);
				cbe->cmd = SVGA_DC_CMD_START_STOP_CONTEXT;
				cbe->cbstart.enable  = 0;
				cbe->cbstart.context = SVGA_CB_CONTEXT_0;
				submitCB(cbe, SVGA_CB_CONTEXT_DEVICE, FALSE); // FALSE?
			}
			rc = 1;
			break;
		case VMWSVXD_PM16_GET_ADDR:
			state->Client_ECX = gSVGA.fbLinear;
			state->Client_EDI = gSVGA.fifoLinear;
			state->Client_ESI = gSVGA.fifo.bounceLinear + PAGE_SIZE; /* first page is for CB header (if CB supported) */
			
			rc = 1;
			break;
		case VMWSVXD_PM16_FIFO_COMMIT:
			
			rc = 1;
			break;
	}
	
	if(rc == 0xFFFF)
	{
		state->Client_EFlags |= 0x1; // set carry
	}
	else
	{
		state->Client_EFlags &= 0xFFFFFFFEUL; // clear carry
	}
	
	return rc;
}

/*
 * Service calls from protected mode (usually 16 bit) and virtual 86 mode
 * CPU state before call is already on stack (EBP points it).
 * Converts the call to __stdcall and call 'VMWS_API_Proc', result of this
 * function is placed to saved AX register.
 *
 */
void __declspec(naked) VMWS_API_Entry()
{
	_asm {
		push ebp
		call VMWS_API_Proc
		mov [ebp+1Ch], ax
		retn
	}
}

/* generate all entry pro VDD function */
#define VDDFUNC(_fnname, _procname) void __declspec(naked) _procname ## _entry() { \
	_asm { push ebp }; \
	_asm { call _procname ## _proc }; \
	_asm { retn }; \
	}
#include "minivdd_func.h"
#undef VDDFUNC

#define VDDFUNC(id, procname) DispatchTable[id] = (DWORD)(procname ## _entry);

/* init device and fill dispatch table */
void Device_Init_proc()
{
	dbg_printf(dbg_Device_Init_proc);
	
	// VMMCall _Allocate_Device_CB_Area
	
	if(SVGA_Init(FALSE) == 0)
	{
		dbg_printf(dbg_Device_Init_proc_succ);
		svga_init_success = TRUE;
		
		dbg_printf(dbg_mapping_map, gSVGA.fbPhy, gSVGA.fifoPhy);
		
		/* map phys FB to linear */
		gSVGA.fbLinear = _MapPhysToLinear(gSVGA.fbPhy, gSVGA.vramSize, 0);
		
		/* map phys FIFO to linear */
		gSVGA.fifoLinear = _MapPhysToLinear(gSVGA.fifoPhy, gSVGA.fifoSize, 0);
		
		/* allocate fifo bounce buffer */
		gSVGA.fifo.bounceLinear =
			_PageAllocate(
				(SVGA_BOUNCE_SIZE + PAGE_SIZE)/PAGE_SIZE,
				PG_SYS, 0, 0, 0x0, 0x100000, &(gSVGA.fifo.bouncePhy), PAGECONTIG | PAGEUSEALIGN | PAGEFIXED
			);
		
		/* system linear addres == PM32 address */
		gSVGA.fbMem = (uint8 *)gSVGA.fbLinear;
		gSVGA.fifoMem = (uint32 *)gSVGA.fifoLinear;
		gSVGA.fifo.bounceMem = (uint8 *)(gSVGA.fifo.bounceLinear + PAGE_SIZE); /* first page is for CB header (if CB supported) */
		
		
		dbg_printf(dbg_mapping);
		dbg_printf(dbg_mapping_map, gSVGA.fbPhy, gSVGA.fbMem);
		dbg_printf(dbg_mapping_map, gSVGA.fifoPhy, gSVGA.fifoMem);
		dbg_printf(dbg_mapping_map, gSVGA.fifo.bouncePhy, gSVGA.fifo.bounceMem);
		dbg_printf(dbg_siz, sizeof(gSVGA), sizeof(uint8 FARP *));
		
		/* enable FIFO */
		SVGA_Enable();
		
		/* allocate GB tables, if supported */
		if(SVGA_ReadReg(SVGA_REG_CAPABILITIES) & SVGA_CAP_GBOBJECTS)
		{
			AllocateOTable();
			gb_support = TRUE;
			dbg_printf(dbg_gb_on);
		}
		
		/* allocate command buffers if supported */
		if(SVGA_ReadReg(SVGA_REG_CAPABILITIES) & (SVGA_CAP_COMMAND_BUFFERS | SVGA_CAP_CMD_BUFFERS_2))
		{
			AllocateCB();
			cb_support = TRUE;
			dbg_printf(dbg_cb_on);
		}
		
		/* register miniVDD functions */
		VDD_Get_Mini_Dispatch_Table();
		if(DispatchTableLength >= 0x31)
		{
			#include "minivdd_func.h"
		}
	}
}
#undef VDDFUNC

/* process user space (PM32, RING-3) call */
DWORD __stdcall Device_IO_Control_entry(struct DIOCParams *params)
{
	switch(params->dwIoControlCode)
	{
		case DIOC_OPEN:
		case DIOC_CLOSEHANDLE:
			dbg_printf(dbg_dic_system, params->dwIoControlCode);
			return 0;
		case SVGA_SYNC:
			SVGA_Flush();
			return 0;
		case SVGA_RING:
			SVGA_WriteReg(SVGA_REG_SYNC, 1);
			return 0;
#if 0
		case SVGA_ALLOCPHY:
		{
			DWORD nPages = ((DWORD*)params->lpInBuffer)[0];
			DWORD *linear = &((DWORD*)params->lpOutBuffer)[0];
			DWORD *phy = &((DWORD*)params->lpOutBuffer)[1];
			if(AllocPhysical(nPages, linear, phy))
			{
				return 0;
			}
			return 1;
		}
		case SVGA_FREEPHY:
		{
			DWORD linear = ((DWORD*)params->lpInBuffer)[0];
			FreePhysical(linear);
			return 0;
		}
#endif
		/* input:
		    - id 
		   output:
		    - linear
		    - physical
		    - size
		    - flags
		 */
		case SVGA_OTABLE_QUERY:
		{
			DWORD   id = ((DWORD*)params->lpInBuffer)[0];
			DWORD *out = (DWORD*)params->lpOutBuffer;
			
			if(!gb_support)
				return 1;
			
			if(id < SVGA_OTABLE_DX_MAX)
			{
				out[0] = (DWORD)otable[id].lin;
				out[1] = otable[id].phy;
				out[2] = otable[id].size;
				out[3] = otable[id].flags;
				return 0;
			}
			return 1;
		}
		/* input:
		   - id
		   - new flags
		 */
		case SVGA_OTABLE_FLAGS:
		{
			DWORD   id  = ((DWORD*)params->lpInBuffer)[0];
			DWORD flags = ((DWORD*)params->lpInBuffer)[1];
			
			if(!gb_support)
				return 1;
			
			if(id < SVGA_OTABLE_DX_MAX)
			{
				otable[id].flags = flags;
				return 0;
			}
			return 1;
		}
		/*
		 input:
		 output:
		  - linear address of buffer
		 */
		case SVGA_CB_LOCK:
		{
			if(cb_support)
			{
				DWORD *out = (DWORD*)params->lpOutBuffer;
				out[0] = (DWORD)LockCB();
				return 0;
			}
			return 1;
		}
		/*
		 input:
		  - linear address of buffer
		  - CB context ID
		 output:
		 */
		case SVGA_CB_SUBMIT:
		{
			if(cb_support)
			{
				DWORD *in = (DWORD*)params->lpInBuffer;
				
				if(submitCB((void*)in[0], in[1], FALSE))
				{
					return 0;
				}
			}
			return 1;
		}
		case SVGA_CB_SUBMIT_SYNC:
		{
			if(cb_support)
			{
				DWORD *in = (DWORD*)params->lpInBuffer;
				
				if(submitCB((void*)in[0], in[1], TRUE))
				{
					return 0;
				}
			}
			return 1;
		}
		/* 
		 none arguments
		*/
		case SVGA_CB_SYNC:
			if(cb_support)
			{
				syncCB();
				return 0;
			}
			return 1;
		/*
		 input:
			- region id
			- pages
		 output:
			- region id
			- user page address
			- page block
			- mob page address
			- mob ppn
		*/
		case SVGA_REGION_CREATE:
		{
  		DWORD *lpIn  = (DWORD*)params->lpInBuffer;
  		DWORD *lpOut = (DWORD*)params->lpOutBuffer;
  		DWORD gmrPPN = 0;
  		
  		//dbg_printf(dbg_region_info_1, lpIn[0]);
  		
			if(GMRAlloc(lpIn[1], &lpOut[1], &lpOut[2], &gmrPPN, &lpOut[3], &lpOut[4]))
			{
				if(lpIn[0] != 0)
				{
					DWORD gmr_max = SVGA_ReadReg(SVGA_REG_GMR_MAX_IDS);
					
					/* if region id extends GRM max, don't register region and only use it as MOB */
					if(lpIn[0] < gmr_max)
					{
		    		SVGA_WriteReg(SVGA_REG_GMR_ID, lpIn[0]);
		    		SVGA_WriteReg(SVGA_REG_GMR_DESCRIPTOR, gmrPPN);
		    	}
		    	
		    	//dbg_printf(dbg_region_info_2, lpOut[1], gmrPPN, lpOut[2]);
		    	
		    	/* refresh all register, so make sure that new commands will accepts this region */
		    	SVGA_Flush();
		    	
		    	lpOut[0] = lpIn[0]; // copy GMR ID
				}
				return 0;
			}
			else
			{
				dbg_printf(dbg_region_err);
			}
  		
  		return 1;
		}
		/*
		 input
			- region id
			- user page address
			- page block address
			- mob PT address
		*/
		case SVGA_REGION_FREE:
		{
			DWORD *lpIn = (DWORD*)params->lpInBuffer;
			
			/* flush all register inc. fifo so make sure, that all commands are processed */
    	SVGA_Flush();
    
    	SVGA_WriteReg(SVGA_REG_GMR_ID, lpIn[0]);
    	SVGA_WriteReg(SVGA_REG_GMR_DESCRIPTOR, 0);
			
			/* sync again */
    	SVGA_Flush();
			
			if(FreeRegion(lpIn[1], lpIn[2], lpIn[3]))
			{
				return 0;
			}
			return 1;
		}
		case SVGA_DBG:
#ifdef DBGPRINT
			dbg_printf(dbg_str, params->lpInBuffer);
#endif			
			return 0;
	}
		
	dbg_printf(dbg_dic_unknown, params->dwIoControlCode);
	
	return 1;
}