-
Notifications
You must be signed in to change notification settings - Fork 48
/
int16.asm
884 lines (711 loc) · 11.8 KB
/
int16.asm
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
; --------------------------------------- int 0xF0 real ---------------------------------------
c16o dw 0
c16s dw 0
c16sts dw 0
c16sto dw 0
include 'directlong.asm'
Thread16C:
thread16header STACK16T1,stack16t1_end
mov ax,CODE16
mov ds,ax
mov ax,[c16sts]
mov ss,ax
mov ax,[c16sto]
mov sp,ax
mov ax,0x25F0
mov dx,int16
int 0x21
mov ax,CODE16
mov ds,ax
call far dword [c16o]
cli
hlt
hlt
Thread32P:
mov ax,page32_idx
mov ss,ax
; mov esp,xxxxxxxx
db 0x66
db 0xBC
c32st dd 0
mov ax,code16_idx
mov ds,ax
db 066h
db 09ah
c32 dd 0
dw vmx32_idx
cli
hlt
hlt
USE64
Thread64P:
; mov rsp,xxxxxxxx
;mov rsp,0x0000000012345678
db 0x48
db 0xC7
db 0xC4
c64st dd 0
linear rax,idt_LM_start
lidt [rax]
mov ax,page64_idx
mov ss,ax
linear rax,retx,CODE16
push rax; for returning
db 0x68; push
c64 dd 0
ret
retx:
cli
hlt
hlt
cv64vmode db 0
cv64vmode2 dw 0
Thread64PV:
; mov rsp,xxxxxxxx
;mov rsp,0x0000000012345678
db 0x48
db 0xC7
db 0xC4
cv64st dd 0
linear rax,idt_LM_start
lidt [rax]
mov ax,page64_idx
mov ss,ax
mov ds,ax
mov es,ax
; ---- VMX
; Existence test
linear rbx,vmt1,DATA16
mov byte [rbx],0
; VMX Preparation
linear rbx,vmt1,DATA16
mov byte [rbx],1
; VMX_Init
linear rax,vvr1,CODE16
push rax; for returning
db 0x68; push
cv64_vmxinit dd 0
ret
vvr1:
; VMX_Enable
linear rax,vvr2,CODE16
push rax; for returning
db 0x68; push
cv64_vmxenable dd 0
ret
vvr2:
; Load the revision
linear rdi,VMXRevision,VMXDATA64
mov ebx,[rdi];
; Initialize the region
linear rdi,VMXStructureData2,VMXDATA64
mov rcx,[rdi]; Get address of data1
mov rsi,rdi
mov rdi,rcx
mov [rdi],ebx ; // Put the revision
VMCLEAR [rsi]
mov [rdi],ebx ; // Put the revision
VMPTRLD [rsi]
mov [rdi],ebx ; // Put the revision
; EPT init
linear rax,vvr7,CODE16
push rax; for returning
db 0x68; push
cv64_vmxinitept dd 0
ret
vvr7:
linear rax,cv64vmode,CODE16
mov al,[rax]
cmp al,1
jz cPM
; Controls init ur
xor rdx,rdx
bts rdx,1
bts rdx,7
linear rax,vvr6,CODE16
push rax; for returning
db 0x68; push
cv64_vmxinitcontrols1 dd 0
ret
cPM:
; pm init controls
mov rdx,0x49
linear rax,vvr6,CODE16
push rax; for returning
db 0x68; push
cv64_vmxinitcontrols2 dd 0
ret
vvr6:
; Host Init
push gs
push fs
linear rcx,vretxx,CODE16
linear rax,vvr4,CODE16
push rax; for returning
db 0x68; push
cv64_vmxinithost dd 0
ret
vvr4:
pop fs
pop gs
; Guest Init
mov r8,raw32_idx
mov r9,0
linear rax,cv64vmode,CODE16
mov al,[rax]
cmp al,1
jz uPM
; UR
mov r10,vmentryx
mov r9,CODE16
linear rax,vvr5,CODE16
push rax; for returning
db 0x68; push
cv64_vmxinitguest1 dd 0
ret
uPM:
; PM
linear r10,vmentry,CODE16
linear rax,vvr5,CODE16
push rax; for returning
db 0x68; push
cv64_vmxinitguest2 dd 0
ret
vvr5:
; The EPT initialization for the guest
linear rax,PhysicalEptOffset64,DATA16
mov rax,[rax]
or rax,0 ; Memory Type 0
or rax,0x18 ; Page Walk Length 3
mov rbx,0x201A ; EPTP
vmwrite rbx,rax
; The Link Pointer -1 initialization
mov rax,0xFFFFFFFFFFFFFFFF
mov rbx,0x2800 ; LP
vmwrite rbx,rax
; One more RSP initialization of the host
xor rax,rax
mov rbx,0x6c14 ; RSP
mov rax,rsp
vmwrite rbx,rax
VMLAUNCH
jmp vretxx
; Virtual Machine Here, Protected mode
USE32
vmentry:
; set the stack
mov ax,page32_idx
mov ss,ax
; set the IDT
linear ebx,idt_PM_start,DATA16
lidt [ebx]
; mov esp,xxxxxxxx
db 0xBC
cv64vst1 dd 0
; call the address
db 09ah
cv64 dd 0
dw vmx32_idx
VMCALL
; Virtual Machine Here, Unrestricted mode
USE16
cv64vst0 dd 0
vmentryx:
mov ax,DATA16
mov ds,ax
; set the IDT
mov ebx,idt_RM_start
lidt [ebx]
; set the stack
mov eax,[cs:cv64vst0]
mov ss,ax
shr eax,16
mov sp,ax
; Check submode
mov ax,[cs:cv64vmode2]
cmp ax,0
je UR_Mode_0
cmp ax,1
je UR_Mode_1_P
cmp ax,2
je UR_Mode_2_P
VMCALL; Nothing else supported atm
; ------------ Long Submode
UR_Mode_2:
USE64
xchg bx,bx
vmcall
USE16
UR_Mode_2_P:
; Restore CS (remember it is loaded with a protected mode selector)
db 0eah
dw PM_VM_Entry4,CODE16
PM_VM_Entry4:
thread64header 1
db 066h
db 0eah
Thread64Ptr1V dd 0
dw code64_idx
; ------------
; ------------ Protected Submode
UR_Mode_1:
USE16
mov ax,page32_idx
mov ss,ax
; mov esp,xxxxxxxx
db 0x66
db 0xBC
c32stV dd 0
mov ax,code16_idx
mov ds,ax
db 066h
db 09ah
c32V dd 0
dw vmx32_idx
vmcall
USE16
UR_Mode_1_P: ; Protected Mode from Unrestricted guest
; Restore CS (remember it is loaded with a protected mode selector)
db 0eah
dw PM_VM_Entry3,CODE16
PM_VM_Entry3:
EnterProtected UR_Mode_1,code16_idx
; ------------
; ------------ Real Submode Mode
UR_Mode_0:
; call the address
db 09ah
cv64u dd 0
VMCALL
; ------------
USE64
vretxx:
; VMX_Disable
linear rax,vvr3,CODE16
push rax; for returning
db 0x68; push
cv64_vmxdisable dd 0
ret
vvr3:
cli
hlt
hlt
USE16
Thread32C:
thread16header STACK16T1,stack16t1_end
EnterProtected Thread32P,code16_idx
Thread64C:
thread64header
db 066h
db 0eah
Thread64Ptr1 dd 0
dw code64_idx
Thread64CV:
thread64header
db 066h
db 0eah
Thread64Ptr4 dd 0
dw code64_idx
int16:
jmp .ibegin
db 'dmmi'
db 10 dup(0x90)
.ibegin:
; AX 0, find interface
cmp ax,0
jnz .n0
dh_virtualization;
push ds
mov ax,DATA16
mov ds,ax
mov dl,[numcpus]
pop ds
mov ax,0xFACE
IRET
.n0:
; AH 1, begin thead
cmp ah,1
jnz .n1
cmp al,0
jnz .n10
; BL = CPU
; AL = 0 = Unreal mode thread
; ES:DX = Run address
; GS:CX = Stack
and ebx,0xFF
mov ax,CODE16
mov ds,ax
mov [c16s],es
mov [c16o],dx
mov [c16sts],gs
mov [c16sto],cx
linear eax,Thread16C,CODE16
call far CODE16:SendSIPIf
IRET
.n10:
cmp al,1
jnz .n11
; BL = CPU
; AL = 1 = Protected mode thread
; EDX = Linear Address
; ECX = Linear Stack
and ebx,0xFF
mov ax,CODE16
mov ds,ax
mov [c32],edx
mov [c32st],ecx
linear eax,Thread32C,CODE16
call far CODE16:SendSIPIf
IRET
.n11:
cmp al,2
jnz .n12
; BL = CPU
; AL = 2 = Long mode thread
; EDX = Linear Address
; ECX = Linear Stack
and ebx,0xFF
mov ax,CODE16
mov ds,ax
mov [c64],edx
mov [c64st],ecx
linear eax,Thread64C,CODE16
call far CODE16:SendSIPIf
IRET
.n12:
cmp al,3
jnz .n13
; BL = CPU
; AL = 3 = Virtualized Thread
; BH = mode (1 PM mode,0 UG mode)
; SI = submode (0 Unreal mode)
; EDX = Linear Address (or seg:ofs if submode 0)
; ECX = Linear Stack
; EDI = Virtualized Linear Stack (or seg:ofs if submode 0)
; Test existence
push eax
push ebx
push ecx
push edx
mov eax,1
cpuid
bt ecx,5
pop edx
pop ecx
pop ebx
pop eax
JC .okvm
iret; duh
.okvm:
mov ax,CODE16
mov ds,ax
mov [cv64u],edx
mov [cv64],edx
mov [cv64st],ecx
mov [c32V],edx
mov [c32stV],ecx
mov [cv64vst0],edi
mov [cv64vst1],edi
mov [cv64vmode],bh
mov [cv64vmode2],si
and ebx,0xFF
linear eax,Thread64CV,CODE16
call far CODE16:SendSIPIf
IRET
.n13:
IRET
.n1:
; AH 5, mutex functions
cmp ah,5
jnz .n5
; Initialize mutex
cmp al,0
jnz .n50
mov byte [es:di],0xFF
iret
.n50:
; lock mutex
cmp al,2
jnz .n52
dec byte [es:di]
iret
.n52:
; unlock mutex
cmp al,3
jnz .n53
cmp byte [es:di],0xFF
jz .okl
inc byte [es:di]
.okl:
iret
.n53:
; wait mutex
cmp al,4
jnz .n54
.Loop1:
CMP byte [es:di],0xff
JZ .OutLoop1
pause
JMP .Loop1
.OutLoop1:
iret
.n54:
IRET
.n5:
; AH 4, call real mode interrupt
; AL = INT NUM
; BP = AX VALUE
; CX,DX,SI,DI = Normal values
; Upper ESI,EDI => DS and ES
cmp ah,4
jnz nr4
push ds
push es
push ax
; Mutex Lock
mov ax,mut_i21
call far CODE16:qwaitlock16
push esi
shr esi,16
mov ds,si
pop esi
push edi
shr edi,16
mov es,di
pop edi
; Interrupt put
pop ax
mov [cs:inttr],al
push ax
cmp al,0
jz skip1
mov ax,bp
db 0xCD
inttr db 0
skip1:
pop ax
pop es
pop ds
; Unlock
qunlock16 mut_i21
iret
nr4:
; AX 9, switch to mode
cmp ah,9
jnz n9
; AL 0, unreal
cmp al,0
jnz .n90
push cs
cli
call EnterUnreal
sti
IRET
.n90:
; AL 1, protected
cmp al,1
jnz n91
mov [cs:n91aa],ecx
mov ax,DATA16
mov ds,ax
mov bx,gdt_start
lgdt [bx]
mov bx,idt_PM_start
lidt [bx]
mov eax,cr0
or al,1
mov cr0,eax
mov ax,page32_idx
mov gs,ax
db 066h
db 0eah
n91aa dd 0
dw vmx32_idx
n91:
; AL 2, long
; ECX = linear address
cmp al,2
jnz n92
mov [cs:Thread64F9],ecx
thread64header
mov ax,page64_idx
mov ss,ax
mov es,ax
mov ds,ax
linear eax,idt_LM_start
db 066h
db 0eah
Thread64F9 dd 0
dw code64_idx
IRET
n92:
IRET
n9:
; AX 0x1401 Helper for DISM to take dissassembly
cmp ax,0x1401
jnz n1401
; Input CX:DX output of what we had
; BX # of bytes dissassembled
;break
cmp cx,0
jz .nxx
cmp dx,0
jz .nxx
push ds
push es
push si
push di
mov ax,cx
mov ds,ax
mov si,dx
mov ax,DATA16
mov es,ax
mov di,dismdata2
mov [es:dismdatac],ebx
.rlp:
mov al,[ds:si]
mov [es:di],al
cmp al,0
jz .ee
inc si
inc di
jmp .rlp
.ee:
pop di
pop si
pop es
pop ds
IRET
.nxx:
; Return DS:SI the 100 byte buffer
mov si,DATA16
mov ds,si
xor esi,esi
mov si,dismdatac
shl esi,16
MOV si,dismdata2
; mov byte [ds:si],16
; mov byte [ds:si + 1],2
; mov byte [ds:si + 2],0x90
; mov byte [ds:si + 3],0x90
IRET
n1401:
IRET
TempBackRM:
mov eax,cr0
and al,not 1
and eax,7FFFFFFFh; Set PE=0
mov cr0,eax
db 0eah
dw .flush_ipq,CODE16
.flush_ipq:
mov ax,STACK16
mov ss,ax
xor esp,esp
mov sp,stack16dmmi2_end
mov ax, DATA16
mov ds,ax
mov es,ax
mov di,idt_RM_start
lidt [di]
sti
; execute the interrupt
mov ax,DATA32
mov ds,ax
mov bp,word [From32To16Regs]
mov bx,word [From32To16Regs + 2]
mov cx,word [From32To16Regs + 4]
mov dx,word [From32To16Regs + 6]
mov si,word [From32To16Regs + 8]
mov di,word [From32To16Regs + 10]
mov ax, word [From32To16Regs + 12]
mov gs,ax ; later DS
mov ax, word [From32To16Regs + 14]
mov fs,ax ; later ES
mov al, byte [From32To16Regs + 16]
mov [cs:inttt],al
cmp al,0
jz skip2
push bp
pop ax
push gs
pop ds
push fs
pop es
db 0xCD
inttt db 0
skip2:
; And again protected
; macro EnterProtected ofs32 = Start32,codeseg = code32_idx,noinits = 0
EnterProtected i4BackFromRM,code32_idx
TempBackLM:
mov eax,cr0
and al,not 1
and eax,7FFFFFFFh; Set PE=0
mov cr0,eax
db 0eah
dw .flush_ipq,CODE16
.flush_ipq:
mov ax,STACK16
mov ss,ax
mov sp,stack16dmmi2_end
mov ax, DATA16
mov ds,ax
mov es,ax
mov di,idt_RM_start
lidt [di]
push cs
call EnterUnreal
; execute the interrupt
mov ax,STACK16S
mov ss,ax
xor esp,esp
mov esp,stack16dmmi2_end
mov ax,DATA64
mov ds,ax
mov bp,word [From64To16Regs]
mov bx,word [From64To16Regs + 2]
mov cx,word [From64To16Regs + 4]
mov dx,word [From64To16Regs + 6]
mov si,word [From64To16Regs + 8]
mov di,word [From64To16Regs + 10]
mov ax, word [From64To16Regs + 12]
mov gs,ax ; later DS
mov ax, word [From64To16Regs + 14]
mov fs,ax ; later ES
mov al, byte [From64To16Regs + 16]
mov [cs:inttt2],al
cmp al,0
jz skip3
push bp
pop ax
push gs
pop ds
push fs
pop es
db 0xCD
inttt2 db 0
skip3:
; and again long mode
thread64header
db 066h
db 0eah
Thread64Ptr3 dd 0
dw code64_idx
TempBackLMnnn0:
mov eax,cr0
and al,not 1
and eax,7FFFFFFFh; Set PE=0
mov cr0,eax
db 0eah
dw .flush_ipq,CODE16
.flush_ipq:
mov ax, DATA16
mov ds,ax
mov es,ax
mov di,idt_RM_start
lidt [di]
; jmp 0x1234:0x5678
db 0xEA
segnnn0 dw 0
ofsnnn0 dw 0