This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathx86seg.js
1656 lines (1538 loc) · 67.8 KB
/
x86seg.js
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
/**
* @fileoverview Implements PCjs X86 Segment Registers
* @author <a href="mailto:[email protected]">Jeff Parsons</a>
* @version 1.0
* Created 2014-Sep-10
*
* Copyright © 2012-2016 Jeff Parsons <[email protected]>
*
* This file is part of PCjs, which is part of the JavaScript Machines Project (aka JSMachines)
* at <http://jsmachines.net/> and <http://pcjs.org/>.
*
* PCjs is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* PCjs is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCjs. If not,
* see <http://www.gnu.org/licenses/gpl.html>.
*
* You are required to include the above copyright notice in every source code file of every
* copy or modified version of this work, and to display that copyright notice on every screen
* that loads or runs any version of this software (see Computer.COPYRIGHT).
*
* Some PCjs files also attempt to load external resource files, such as character-image files,
* ROM files, and disk image files. Those external resource files are not considered part of the
* PCjs program for purposes of the GNU General Public License, and the author does not claim
* any copyright as to their contents.
*/
"use strict";
if (NODE) {
var str = require("../../shared/lib/strlib");
var Messages = require("./messages");
var X86 = require("./x86");
}
/*
* NOTE: The protected-mode support in this module was initially added for 80286 support, and is
* currently being upgraded for 80386 support. In a perfect world, all 80386-related support would
* be disabled/skipped whenever the processor is merely an 80286. And in fact, that's the case
* with some of the early changes (eg, skipping X86.DESC.EXT.BASE2431 and X86.DESC.EXT.LIMIT1619
* fields unless the processor is an 80386).
*
* However, the reality is that I won't always be that strict, either because I'm lazy or I don't
* want to risk a run-time performance hit or (more pragmatically) because any 80286 code you're likely
* to run probably won't attempt to use descriptor types or other features unique to the 80386 anyway,
* so the extra paranoia may not be worth the effort. Ultimately, I would like to see the code tailor
* itself to the current CPU model, generally with model-specific functions, but that's a lot of work.
*/
/**
* X86Seg "public" properties
*
* @class X86Seg
* @property {number} sel
* @property {number} limit (in protected-mode, this comes from descriptor word 0x0)
* @property {number} base (in protected-mode, this comes from descriptor word 0x2)
* @property {number} acc (in protected-mode, this comes from descriptor word 0x4; bits 0-7 supplement base bits 16-23)
* @property {number} ext (in protected-mode, this is descriptor word 0x6, 80386 only; supplements limit bits 16-19 and base bits 24-31)
* @property {number} type (this is a subset of acc, using X86.DESC.ACC.TYPE.MASK)
*
* TODO: Determine what good, if any, these class annotations are for either an IDE like WebStorm or a tool like
* the Closure Compiler. More importantly, what good do they do at runtime? Is it better to simply ensure that all
* object properties are explicitly initialized in the constructor, and document them there instead? I started by
* listing only what might be considered "public" properties above, in an effort to eliminate WebStorm inspection
* warnings, but it didn't seem to help, so I stopped.
*/
/**
* X86Seg(cpu, sName)
*
* @constructor
* @param {X86CPU} cpu
* @param {number} id
* @param {string} [sName] segment register name
* @param {boolean} [fProt] true if segment register used exclusively in protected-mode (eg, segLDT)
*/
function X86Seg(cpu, id, sName, fProt)
{
this.cpu = cpu;
this.dbg = cpu.dbg;
this.id = id;
this.sName = sName || "";
this.sel = 0;
this.limit = 0xffff;
this.offMax = this.limit + 1;
this.base = 0;
this.acc = this.type = 0;
this.ext = 0;
this.cpl = this.dpl = 0;
this.addrDesc = X86.ADDR_INVALID;
this.sizeData = this.sizeAddr = 2;
this.maskData = this.maskAddr = 0xffff;
this.loadV86 = this.loadReal;
this.checkReadV86 = this.checkReadReal;
this.checkWriteV86 = this.checkWriteReal;
/*
* Preallocated object for "probed" segment loads
*/
this.probe = {
sel: -1, base: 0, limit: 0, acc: 0, type: 0, ext: 0, addrDesc: X86.ADDR_INVALID
};
/*
* The following properties are used for CODE segments only (ie, segCS); if the process of loading
* CS also requires a stack switch, then fStackSwitch will be set to true; additionally, if the stack
* switch was the result of a CALL (ie, fCall is true) and one or more (up to 32) parameters are on
* the old stack, they will be copied to awParms, and then once the stack is switched, the parameters
* will be pushed from awParms onto the new stack.
*
* The typical ways of loading a new segment into CS are JMPF, CALLF (or INT), and RETF (or IRET),
* via CPU functions setCSIP() and fnINT(), which use segCS.loadCode() and segCS.loadIDT(), respectively.
*
* loadCode() requires an fCall value: null means NO privilege level transition may occur, true
* allows a stack switch and a privilege transition to a numerically lower privilege, and false allows
* a stack restore and a privilege transition to a numerically greater privilege.
*
* loadIDT() sets fCall to true unconditionally in protected-mode (fCall has no meaning in real-mode).
*/
if (this.id == 1) { // X86Seg.ID.CODE (don't use until it's defined, or the Closure Compiler won't inline it)
this.offIP = 0;
this.fCall = null;
this.fStackSwitch = false;
this.awParms = new Array(32);
this.aCallBreaks = [];
}
this.updateMode(true, fProt);
}
X86Seg.ID = {
NULL: 0, // "NULL"
CODE: 1, // "CS"
DATA: 2, // "DS", "ES", "FS", "GS"
STACK: 3, // "SS"
TSS: 4, // "TSS"
LDT: 5, // "LDT"
VER: 6, // "VER"
DBG: 7 // "DBG"
};
X86Seg.CALLBREAK_SEL = 0x0001;
/**
* addCallBreak(fn)
*
* Returns a "call break" address in an [off, sel] array. The given function, fn(), is called
* whenever that address is called, and if fn() returns false, then the call is skipped. Otherwise,
* the call is performed (ie, the old CS:[E]IP is pushed on the stack, and CS:[E]IP is set to the
* "call break" address. Which is probably a bad idea, so your function should probably always
* return false. Just sayin'. TODO: Should probably just force all "call break" calls to be skipped.
*
* @this {X86Seg}
* @param {function()} fn
* @return {Array.<number>} containing offset and selector of call-break address
*/
X86Seg.prototype.addCallBreak = function(fn)
{
this.aCallBreaks.push(fn);
return [this.aCallBreaks.length, X86Seg.CALLBREAK_SEL];
};
/**
* loadCode(off, sel, fCall)
*
* A simple wrapper function that encapsulates setting offIP and fCall for segCS loads.
*
* @this {X86Seg}
* @param {number} off
* @param {number} sel
* @param {boolean|undefined} fCall is true if CALLF in progress, false if RETF/IRET in progress, undefined otherwise
* @return {number} base address of selected segment, or X86.ADDR_INVALID if error
*/
X86Seg.prototype.loadCode = function loadCode(off, sel, fCall)
{
this.offIP = off;
this.fCall = fCall;
return this.load(sel);
};
/**
* loadReal(sel, fProbe)
*
* The default segment load() function for real-mode.
*
* @this {X86Seg}
* @param {number} sel
* @param {boolean} [fProbe] (here only to make the function signatures of loadReal() and loadProt() match)
* @return {number} base address of selected segment
*/
X86Seg.prototype.loadReal = function loadReal(sel, fProbe)
{
this.sel = sel & 0xffff;
/*
* Loading a new value into a segment register in real-mode alters ONLY the selector and the base;
* all other attributes (eg, limit, operand size, address size, etc) are unchanged. If you run any
* code that switches to protected-mode, loads a 32-bit code segment, and then switches back to
* real-mode, it is THAT code's responsibility to load a 16-bit segment into CS before returning to
* real-mode; otherwise, your machine will probably be toast.
*/
return this.base = this.sel << 4;
};
/**
* loadProt(sel, fProbe)
*
* This replaces the segment's default load() function whenever the segment is notified via updateMode() by the
* CPU's setProtMode() that the processor is now in protected-mode.
*
* Segments in protected-mode are referenced by selectors, which are indexes into descriptor tables (GDT or LDT)
* whose descriptors are 4-word (8-byte) entries:
*
* word 0: segment limit (0-15)
* word 1: base address low
* word 2: base address high (0-7), segment type (8-11), descriptor type (12), DPL (13-14), present bit (15)
* word 3: used only on 80386 and up (should be set to zero for upward compatibility)
*
* See X86.DESC for offset and bit definitions.
*
* IDT descriptor entries are handled separately by loadIDT(), which is mapped to loadIDTReal() or loadIDTProt().
*
* @this {X86Seg}
* @param {number} sel
* @param {boolean} [fProbe]
* @return {number} base address of selected segment, or X86.ADDR_INVALID if error
*/
X86Seg.prototype.loadProt = function loadProt(sel, fProbe)
{
var addrDT;
var addrDTLimit;
var cpu = this.cpu;
/*
* Some instructions (eg, CALLF) load a 32-bit value for the selector, while others (eg, LDS) do not;
* however, in ALL cases, only the low 16 bits are significant.
*/
sel &= 0xffff;
if (!(sel & X86.SEL.LDT)) {
addrDT = cpu.addrGDT;
addrDTLimit = cpu.addrGDTLimit;
} else {
addrDT = cpu.segLDT.base;
addrDTLimit = (addrDT + cpu.segLDT.limit)|0;
}
/*
* The ROM BIOS POST executes some test code in protected-mode without properly initializing the LDT,
* which has no bearing on the ROM's own code, because it never loads any LDT selectors, but if at the same
* time our Debugger attempts to validate a selector in one of its breakpoints, that could cause some grief.
*
* Fortunately, the Debugger now has its own interface, probeDesc(), so that should no longer be a concern.
*/
if (addrDT) {
var addrDesc = (addrDT + (sel & X86.SEL.MASK))|0;
if ((addrDTLimit - addrDesc)|0 >= 7) {
/*
* TODO: This is the first of many steps toward accurately counting cycles in protected mode;
* I simply noted that "POP segreg" takes 5 cycles in real mode and 20 in protected mode, so I'm
* starting with a 15-cycle difference. Obviously the difference will vary with the instruction,
* and will be much greater whenever the load fails.
*/
cpu.nStepCycles -= 15;
return this.loadDesc8(addrDesc, sel, fProbe);
}
if (this.id < X86Seg.ID.VER) {
X86.fnFault.call(cpu, fProbe && this.id == X86Seg.ID.STACK? X86.EXCEPTION.TS_FAULT : X86.EXCEPTION.GP_FAULT, sel & X86.ERRCODE.SELMASK);
}
}
return X86.ADDR_INVALID;
};
/**
* loadIDTReal(nIDT)
*
* @this {X86Seg}
* @param {number} nIDT
* @return {number} address from selected vector
*/
X86Seg.prototype.loadIDTReal = function loadIDTReal(nIDT)
{
var cpu = this.cpu;
/*
* NOTE: The Compaq DeskPro 386 ROM loads the IDTR for the real-mode IDT with a limit of 0xffff instead
* of the normal 0x3ff. A limit higher than 0x3ff is OK, since all real-mode IDT entries are 4 bytes, and
* there's no way to issue an interrupt with a vector > 0xff. Just something to be aware of.
*/
cpu.assert(nIDT >= 0 && nIDT < 256 && !cpu.addrIDT && cpu.addrIDTLimit >= 0x3ff);
/*
* Intel documentation for INT/INTO under "REAL ADDRESS MODE EXCEPTIONS" says:
*
* "[T]he 80286 will shut down if the SP = 1, 3, or 5 before executing the INT or INTO instruction--due to lack of stack space"
*
* TODO: Verify that 80286 real-mode actually enforces the above. See http://www.pcjs.org/pubs/pc/reference/intel/80286/progref/#page-260
*/
var addrIDT = cpu.addrIDT + (nIDT << 2);
var off = cpu.getShort(addrIDT);
cpu.regPS &= ~(X86.PS.TF | X86.PS.IF);
return (this.load(cpu.getShort(addrIDT + 2)) + off)|0;
};
/**
* loadIDTProt(nIDT)
*
* @this {X86Seg}
* @param {number} nIDT
* @return {number} address from selected vector, or X86.ADDR_INVALID if error
*/
X86Seg.prototype.loadIDTProt = function loadIDTProt(nIDT)
{
var cpu = this.cpu;
cpu.assert(nIDT >= 0 && nIDT < 256);
nIDT <<= 3;
var addrDesc = (cpu.addrIDT + nIDT)|0;
if (((cpu.addrIDTLimit - addrDesc)|0) >= 7) {
this.fCall = true;
var addr = this.loadDesc8(addrDesc, nIDT);
if (addr !== X86.ADDR_INVALID) addr += this.offIP;
return addr;
}
X86.fnFault.call(cpu, X86.EXCEPTION.GP_FAULT, nIDT | X86.ERRCODE.IDT);
return X86.ADDR_INVALID;
};
/**
* checkReadReal(off, cb)
*
* TODO: Invoke X86.fnFault.call(this.cpu, X86.EXCEPTION.GP_FAULT) if off+cb is beyond offMax on 80186 and up;
* also, determine whether fnFault() call should include an error code, since this is happening in real-mode.
*
* @this {X86Seg}
* @param {number} off is a segment-relative offset
* @param {number} cb is number of bytes to check (1, 2 or 4)
* @return {number} corresponding linear address if valid, or X86.ADDR_INVALID if error (TODO: No error conditions yet)
*/
X86Seg.prototype.checkReadReal = function checkReadReal(off, cb)
{
return (this.base + off)|0;
};
/**
* checkWriteReal(off, cb)
*
* TODO: Invoke X86.fnFault.call(this.cpu, X86.EXCEPTION.GP_FAULT) if off+cb is beyond offMax on 80186 and up;
* also, determine whether fnFault() call should include an error code, since this is happening in real-mode.
*
* @this {X86Seg}
* @param {number} off is a segment-relative offset
* @param {number} cb is number of bytes to check (1, 2 or 4)
* @return {number} corresponding linear address if valid, or X86.ADDR_INVALID if error (TODO: No error conditions yet)
*/
X86Seg.prototype.checkWriteReal = function checkWriteReal(off, cb)
{
return (this.base + off)|0;
};
/**
* checkReadProt(off, cb)
*
* @this {X86Seg}
* @param {number} off is a segment-relative offset
* @param {number} cb is number of bytes to check (1, 2 or 4)
* @return {number} corresponding linear address if valid, or X86.ADDR_INVALID if not
*/
X86Seg.prototype.checkReadProt = function checkReadProt(off, cb)
{
/*
* Since off could be a 32-bit value with the sign bit (bit 31) set, we must convert
* it to an unsigned value using ">>>"; offMax was already converted at segment load time.
*/
if ((off >>> 0) + cb <= this.offMax) {
return (this.base + off)|0;
}
return this.checkReadProtDisallowed(off, cb);
};
/**
* checkReadProtDown(off, cb)
*
* @this {X86Seg}
* @param {number} off is a segment-relative offset
* @param {number} cb is number of bytes to check (1, 2 or 4)
* @return {number} corresponding linear address if valid, X86.ADDR_INVALID if not
*/
X86Seg.prototype.checkReadProtDown = function checkReadProtDown(off, cb)
{
/*
* Since off could be a 32-bit value with the sign bit (bit 31) set, we must convert
* it to an unsigned value using ">>>"; offMax was already converted at segment load time.
*/
if ((off >>> 0) + cb > this.offMax) {
return (this.base + off)|0;
}
return this.checkReadProtDisallowed(off, cb);
};
/**
* checkReadProtDisallowed(off, cb)
*
* @this {X86Seg}
* @param {number} off is a segment-relative offset
* @param {number} cb is number of bytes to check (1, 2 or 4)
* @return {number} corresponding linear address if valid, X86.ADDR_INVALID if not
*/
X86Seg.prototype.checkReadProtDisallowed = function checkReadProtDisallowed(off, cb)
{
X86.fnFault.call(this.cpu, X86.EXCEPTION.GP_FAULT, 0);
return X86.ADDR_INVALID;
};
/**
* checkWriteProt(off, cb)
*
* @this {X86Seg}
* @param {number} off is a segment-relative offset
* @param {number} cb is number of bytes to check (1, 2 or 4)
* @return {number} corresponding linear address if valid, X86.ADDR_INVALID if not
*/
X86Seg.prototype.checkWriteProt = function checkWriteProt(off, cb)
{
/*
* Since off could be a 32-bit value with the sign bit (bit 31) set, we must convert
* it to an unsigned value using ">>>"; offMax was already converted at segment load time.
*/
if ((off >>> 0) + cb <= this.offMax) {
return (this.base + off)|0;
}
return this.checkWriteProtDisallowed(off, cb);
};
/**
* checkWriteProtDown(off, cb)
*
* @this {X86Seg}
* @param {number} off is a segment-relative offset
* @param {number} cb is number of bytes to check (1, 2 or 4)
* @return {number} corresponding linear address if valid, X86.ADDR_INVALID if not
*/
X86Seg.prototype.checkWriteProtDown = function checkWriteProtDown(off, cb)
{
/*
* Since off could be a 32-bit value with the sign bit (bit 31) set, we must convert
* it to an unsigned value using ">>>"; offMax was already converted at segment load time.
*/
if ((off >>> 0) + cb > this.offMax) {
return (this.base + off)|0;
}
return this.checkWriteProtDisallowed(off, cb);
};
/**
* checkWriteProtDisallowed(off, cb)
*
* @this {X86Seg}
* @param {number} off is a segment-relative offset
* @param {number} cb is number of bytes to check (1, 2 or 4)
* @return {number} corresponding linear address if valid, X86.ADDR_INVALID if not
*/
X86Seg.prototype.checkWriteProtDisallowed = function checkWriteProtDisallowed(off, cb)
{
X86.fnFault.call(this.cpu, X86.EXCEPTION.GP_FAULT, 0);
return X86.ADDR_INVALID;
};
/**
* checkReadDebugger(off, cb)
*
* @this {X86Seg}
* @param {number} off is a segment-relative offset
* @param {number} cb is number of bytes to check (1, 2 or 4)
* @return {number} corresponding linear address if valid, or X86.ADDR_INVALID if error
*/
X86Seg.prototype.checkReadDebugger = function checkReadDebugger(off, cb)
{
/*
* The Debugger doesn't have separate "check" interfaces for real and protected mode,
* since it's not performance-critical. If addrDesc is invalid, then we assume real mode.
*
* TODO: This doesn't actually check the segment for readability.
*/
if (DEBUGGER) {
if (this.addrDesc === X86.ADDR_INVALID ||
this.fExpDown && (off >>> 0) + cb > this.offMax ||
!this.fExpDown && (off >>> 0) + cb <= this.offMax) {
return (this.base + off)|0;
}
}
return X86.ADDR_INVALID;
};
/**
* checkWriteDebugger(off, cb)
*
* @this {X86Seg}
* @param {number} off is a segment-relative offset
* @param {number} cb is number of bytes to check (1, 2 or 4)
* @return {number} corresponding linear address if valid, or X86.ADDR_INVALID if error
*/
X86Seg.prototype.checkWriteDebugger = function checkWriteDebugger(off, cb)
{
/*
* The Debugger doesn't have separate "check" interfaces for real and protected mode,
* since it's not performance-critical. If addrDesc is invalid, then we assume real mode.
*
* TODO: This doesn't actually check the segment for writability.
*/
if (DEBUGGER) {
if (this.addrDesc === X86.ADDR_INVALID ||
this.fExpDown && (off >>> 0) + cb > this.offMax ||
!this.fExpDown && (off >>> 0) + cb <= this.offMax) {
return (this.base + off)|0;
}
}
return X86.ADDR_INVALID;
};
/**
* loadAcc(sel, fGDT)
*
* @this {X86Seg}
* @param {number} sel (protected-mode only)
* @param {boolean} [fGDT] is true if sel must be in the GDT
* @return {number} ACC field from descriptor, or X86.DESC.ACC.INVALID if error
*
X86Seg.prototype.loadAcc = function(sel, fGDT)
{
var addrDT;
var addrDTLimit;
var cpu = this.cpu;
if (!(sel & X86.SEL.LDT)) {
addrDT = cpu.addrGDT;
addrDTLimit = cpu.addrGDTLimit;
} else if (!fGDT) {
addrDT = cpu.segLDT.base;
addrDTLimit = (addrDT + cpu.segLDT.limit)|0;
}
if (addrDT !== undefined) {
var addrDesc = (addrDT + (sel & X86.SEL.MASK))|0;
if (((addrDTLimit - addrDesc)|0) >= 7) {
return cpu.getShort(addrDesc + X86.DESC.ACC.OFFSET);
}
}
X86.fnFault.call(cpu, X86.EXCEPTION.GP_FAULT, sel & X86.ERRCODE.SELMASK);
return X86.DESC.ACC.INVALID;
};
*/
/**
* loadDesc(sel, acc, base, limit)
*
* Used to manually load a segment register from the data provided (see LOADALL386).
*
* @this {X86Seg}
* @param {number} sel
* @param {number} acc
* @param {number} base
* @param {number} limit
*/
X86Seg.prototype.loadDesc = function(sel, acc, base, limit)
{
this.sel = sel;
this.base = base;
this.limit = limit;
this.offMax = (limit >>> 0) + 1;
this.acc = acc;
this.type = (acc & X86.DESC.ACC.TYPE.MASK);
this.ext = (acc >> 16) & (X86.DESC.EXT.BIG | X86.DESC.EXT.LIMITPAGES);
var addrDT = (sel & X86.SEL.LDT)? this.cpu.segLDT.base : this.cpu.addrGDT;
this.addrDesc = (addrDT + (sel & X86.SEL.MASK))|0;
/*
* NOTE: This code must take care to leave the mode of the TSS, LDT, and VER segment registers alone;
* in particular, we must not allow a real-mode LOADALL to modify their mode, because the rest of PCjs
* assumes that their mode will never change (they were allocated with fProt set to true).
*/
if (this.id < X86Seg.ID.TSS) this.updateMode(true);
if (DEBUG) this.messageSeg(sel, base, limit, this.type);
};
/**
* loadDesc6(addrDesc, sel)
*
* Used to load a protected-mode selector that refers to a 6-byte "descriptor cache" entry (see LOADALL286):
*
* word 0: base address low
* word 1: base address high (0-7), segment type (8-11), descriptor type (12), DPL (13-14), present bit (15)
* word 2: segment limit (0-15)
*
* @this {X86Seg}
* @param {number} addrDesc is the descriptor address
* @param {number} sel is the associated selector
* @return {number} base address of selected segment
*/
X86Seg.prototype.loadDesc6 = function(addrDesc, sel)
{
var cpu = this.cpu;
var acc = cpu.getShort(addrDesc + 2);
var base = cpu.getShort(addrDesc) | ((acc & 0xff) << 16);
var limit = cpu.getShort(addrDesc + 4);
this.sel = sel;
this.base = base;
this.limit = limit;
this.offMax = (limit >>> 0) + 1;
this.acc = acc;
this.type = (acc & X86.DESC.ACC.TYPE.MASK);
this.ext = 0;
this.addrDesc = addrDesc;
/*
* NOTE: This code must take care to leave the mode of the TSS, LDT, and VER segment registers alone;
* in particular, we must not allow a real-mode LOADALL to modify their mode, because the rest of PCjs
* assumes that their mode will never change (they were allocated with fProt set to true).
*/
if (this.id < X86Seg.ID.TSS) this.updateMode(true);
if (DEBUG) this.messageSeg(sel, base, limit, this.type);
return base;
};
/**
* loadDesc8(addrDesc, sel, fProbe)
*
* Used to load a protected-mode selector that refers to an 8-byte "descriptor table" (GDT, LDT, IDT) entry:
*
* word 0: segment limit (0-15)
* word 1: base address low
* word 2: base address high (0-7), segment type (8-11), descriptor type (12), DPL (13-14), present bit (15)
* word 3: used only on 80386 and up (should be set to zero for upward compatibility)
*
* See X86.DESC for offset and bit definitions.
*
* When fProbe is set, we do NOT modify the public properties of the X86Seg object (see class X86Seg above).
* We will generate a fault if any of the usual error conditions are detected (and return X86.ADDR_INVALID), but
* otherwise, we merely stash all the descriptor values it reads in the X86Seg's private "probe" object.
*
* Probed loads allow us to deal with complex segment load operations (ie, those involving an implied stack-switch
* or task-switch), by allowing us to probe all the new selectors and generate the necessary faults before modifying
* any segment registers; if all the probes succeed, then all the loads can proceed.
*
* The next non-probed load of a probed selector will move those probed descriptor values into the X86Seg object,
* saving us from having to reload and reparse the descriptor. However, if a different selector is loaded between
* the probed and non-probed loads, the probed data is tossed.
*
* @this {X86Seg}
* @param {number} addrDesc is the descriptor address
* @param {number} sel is the associated selector, or nIDT*8 if IDT descriptor
* @param {boolean} [fProbe] (true if this is a probe)
* @return {number} base address of selected segment, or X86.ADDR_INVALID if error
*/
X86Seg.prototype.loadDesc8 = function(addrDesc, sel, fProbe)
{
var cpu = this.cpu;
/*
* If the previous load was a successful "probed" load of the same segment, then we simply load
* up all the cached descriptor values from the probe and return.
*/
if (!fProbe && sel === this.probe.sel) {
this.sel = sel;
this.base = this.probe.base;
this.limit = this.probe.limit;
this.offMax = (this.probe.limit >>> 0) + 1;
this.acc = this.probe.acc;
this.type = this.probe.type;
this.ext = this.probe.ext;
this.addrDesc = this.probe.addrDesc;
this.probe.sel = -1;
this.updateMode(true, true, false);
return this.base;
}
/*
* Any other load, probed or otherwise, should "flush" the probe cache, by setting probe.sel to -1.
*/
this.probe.sel = -1;
/*
* Load the descriptor from memory.
*/
var limit = cpu.getShort(addrDesc + X86.DESC.LIMIT.OFFSET);
var acc = cpu.getShort(addrDesc + X86.DESC.ACC.OFFSET);
var type = (acc & X86.DESC.ACC.TYPE.MASK);
var base = cpu.getShort(addrDesc + X86.DESC.BASE.OFFSET) | ((acc & X86.DESC.ACC.BASE1623) << 16);
var ext = cpu.getShort(addrDesc + X86.DESC.EXT.OFFSET);
var selMasked = sel & X86.SEL.MASK;
if (I386 && cpu.model >= X86.MODEL_80386) {
var limitOrig = limit;
base |= (ext & X86.DESC.EXT.BASE2431) << 16;
limit |= (ext & X86.DESC.EXT.LIMIT1619) << 16;
if (ext & X86.DESC.EXT.LIMITPAGES) limit = (limit << 12) | 0xfff;
}
switch (this.id) {
case X86Seg.ID.CODE:
var fCall = this.fCall;
this.fStackSwitch = false;
/*
* This special bit of code is currently used only by the Debugger, when it needs to inject
* a 16:32 callback address into the machine that it can intercept calls to. We call these
* "call break" addresses, because they're like private breakpoints that only operate when
* a particular address is called; specifically, an address with selector 0x0001 and an offset
* that forms an index (1-based) into the aCallBreaks function table.
*
* In protected-mode, 0x0001 is an invalid code selector (a null selector with an RPL of 1),
* and while it's not inconceivable that an operating system might use such a selector for
* some strange purpose, I've not seen such an operating system. And in any case, those
* operating systems are not likely to trigger the Debugger's call to addCallBreak(), so no
* call breaks will be generated, and this code will never execute.
*
* TODO: If we ever need this to be mode-independent, it can be moved somewhere where it will
* trigger for both real and protected-mode code segment loads, because CALLBREAK_SEL (0x0001)
* is also a very unlikely real-mode CS value (but again, not inconceivable). I think this is
* a reasonable solution, and it's likely the best we can do without injecting code into the
* machine that we could address -- and even then, it would not be a mode-independent address.
*/
if (fCall && sel == X86Seg.CALLBREAK_SEL && this.aCallBreaks.length) {
var iBreak = this.offIP - 1;
var fnCallBreak = this.aCallBreaks[iBreak];
cpu.assert(fnCallBreak);
if (fnCallBreak && !fnCallBreak()) {
return X86.ADDR_INVALID;
}
}
var rpl = sel & X86.SEL.RPL;
var dpl = (acc & X86.DESC.ACC.DPL.MASK) >> X86.DESC.ACC.DPL.SHIFT;
var sizeGate = -1, selCode, cplOld, cplNew, fIDT;
var addrTSS, offSP, lenSP, regSPPrev, regSSPrev, regPSClear, regSP;
if (!selMasked) {
/*
* selMasked is really the descriptor table offset, and a zero offset is fine for the IDT;
* it MAY even be OK for the LDT. But it's definitely not OK for the GDT; a null selector
* is allowed in any of DS, ES, SS, FS, or GS, but never CS). Since there's no parameter
* that tells us which table we're using, we have to check manually.
*
* If we ARE attempting to load a null selector from the GDT, then we zero type, which ensures
* that sizeGate will remain invalid, triggering a GP_FAULT below.
*/
if (addrDesc >= cpu.addrGDT && addrDesc < cpu.addrGDTLimit) type = 0;
}
/*
* Since we are X86Seg.ID.CODE, we can use this.cpl instead of the more generic cpu.segCS.cpl
*/
if (type >= X86.DESC.ACC.TYPE.CODE_EXECONLY) {
sizeGate = 0;
if (rpl > this.cpl) {
/*.
* If fCall is false, then we must have a RETF to a less privileged segment, which is OK.
*
* Otherwise, we must be dealing with a CALLF or JMPF to a less privileged segment, in which
* case either DPL == CPL *or* the new segment is conforming and DPL <= CPL.
*/
sizeGate = -1;
if (fCall === false || dpl == this.cpl || (type & X86.DESC.ACC.TYPE.CONFORMING) && dpl <= this.cpl) {
/*
* It's critical that any stack switch occur with the operand size in effect at the time of
* the current instruction, BEFORE any calls to updateMode() and resetSizes(), otherwise the
* operand size (or operand override) in effect on an instruction like IRETD will be ignored.
*/
regSP = cpu.popWord();
cpu.setSS(cpu.popWord(), true);
cpu.setSP(regSP);
this.fStackSwitch = true;
sizeGate = 0;
}
}
}
else if (type == X86.DESC.ACC.TYPE.TSS286 || type == X86.DESC.ACC.TYPE.TSS386) {
if (!this.switchTSS(sel, fCall)) {
return X86.ADDR_INVALID;
}
return this.base;
}
else if (type == X86.DESC.ACC.TYPE.GATE_CALL) {
sizeGate = 2;
regPSClear = 0;
if (rpl < this.cpl) rpl = this.cpl; // set RPL to max(RPL,CPL) for call gates
}
else if (type == X86.DESC.ACC.TYPE.GATE386_CALL) {
sizeGate = 4;
regPSClear = 0;
if (rpl < this.cpl) rpl = this.cpl; // set RPL to max(RPL,CPL) for call gates
}
else if (type == X86.DESC.ACC.TYPE.GATE286_INT) {
sizeGate = 2;
regPSClear = (X86.PS.VM | X86.PS.NT | X86.PS.TF | X86.PS.IF);
cpu.assert(!(acc & 0x1f));
}
else if (type == X86.DESC.ACC.TYPE.GATE386_INT) {
sizeGate = 4;
regPSClear = (X86.PS.VM | X86.PS.NT | X86.PS.TF | X86.PS.IF);
cpu.assert(!(acc & 0x1f));
}
else if (type == X86.DESC.ACC.TYPE.GATE286_TRAP) {
sizeGate = 2;
regPSClear = (X86.PS.VM | X86.PS.NT | X86.PS.TF);
cpu.assert(!(acc & 0x1f));
}
else if (type == X86.DESC.ACC.TYPE.GATE386_TRAP) {
sizeGate = 4;
regPSClear = (X86.PS.VM | X86.PS.NT | X86.PS.TF);
cpu.assert(!(acc & 0x1f));
}
else if (type == X86.DESC.ACC.TYPE.GATE_TASK) {
if (!this.switchTSS(base & 0xffff, fCall)) {
return X86.ADDR_INVALID;
}
return this.base;
}
if (sizeGate > 0 && !(acc & X86.DESC.ACC.PRESENT)) sizeGate = 0;
if (sizeGate > 0) {
/*
* Note that since GATE_INT/GATE_TRAP descriptors should appear in the IDT only, that means sel
* will actually be nIDT * 8, which means the rpl will always be zero; additionally, the nWords
* portion of ACC should always be zero, but that's really dependent on the descriptor being properly
* set (which we assert above).
*/
cplOld = this.cpl;
fIDT = (addrDesc == cpu.addrIDT + sel);
/*
* Software interrupts (where fIDT is true and cpu.nFault < 0) require an additional test:
* if DPL < CPL, then we must fall into the GP_FAULT code at the end of this case.
*/
if (rpl <= dpl && (!fIDT || cpu.nFault >= 0 || cplOld <= dpl)) {
/*
* For gates, there is no "base" and "limit", but rather "selector" and "offset"; the selector
* is located where the first 16 bits of base are normally stored, and the offset comes from the
* original limit and ext fields.
*
* TODO: Verify the PRESENT bit of the gate descriptor, and issue NP_FAULT as appropriate.
*/
selCode = base & 0xffff;
if (I386 && (type & X86.DESC.ACC.NONSEG_386)) {
limit = limitOrig | (ext << 16);
}
var selStack = 0, offStack = 0;
cplNew = (selCode & X86.SEL.RPL);
/*
* If a stack switch is required, we must perform "probed" loads of both the new selCode
* and selStack segments, so that if either probe fails, a fault will be generated while the
* old code segment is still loaded.
*/
if (cplNew < cplOld) {
/*
* Intel pseudo-code suggests that selStack should be "probed" before selCode, but it also
* implies that we need to have the DPL of selCode in order to select the correct selStack,
* so who knows...?
*/
if (this.loadProt(selCode, true) === X86.ADDR_INVALID) {
return X86.ADDR_INVALID;
}
/*
* Intel pseudo-code suggests that the TSS stack pointer offset is based on the DPL of selCode
* rather than the RPL of selCode. TODO: Check for instances where DPL and RPL of selCode differ,
* and then figure out which should really be used.
*/
addrTSS = cpu.segTSS.base;
if (!I386 || !(cpu.segTSS.type & X86.DESC.ACC.NONSEG_386)) {
offSP = (cplNew << 2) + X86.TSS286.CPL0_SP;
lenSP = 2;
} else {
offSP = (cplNew << 3) + X86.TSS386.CPL0_ESP;
lenSP = 4;
}
selStack = cpu.getShort(addrTSS + offSP + lenSP);
/*
* Intel pseudo-code indicates at least FIVE discrete selStack tests that could trigger
* a TS_FAULT at this point:
*
* 1) Selector must not be null else #TS(O)
* 2) Selector index must be within its descriptor table limits else #TS (SS selector)
* 3) Selector's RPL must equal DPL of code segment else #TS (SS selector)
* 4) Stack segment DPL must equal DPL of code segment else #TS (SS selector)
* 5) Descriptor must indicate writable data segment else #TS (SS selector)
*/
if (!selStack) {
X86.fnFault.call(cpu, X86.EXCEPTION.TS_FAULT, selStack);
return X86.ADDR_INVALID;
}
if (cpu.segSS.loadProt(selStack, true) === X86.ADDR_INVALID) {
return X86.ADDR_INVALID;
}
/*
* Both probes succeeded, so we can proceed with "normal" loads for both selCode and
* selStack (which should automatically use the values cached by the "probed" loads above).
*/
offStack = (lenSP == 2)? cpu.getShort(addrTSS + offSP) : cpu.getLong(addrTSS + offSP);
}
/*
* Now that we're past all the probes, it should be safe to clear all flags that need clearing.
*/
var regPS = cpu.regPS;
cpu.regPS &= ~regPSClear;
if (regPS & X86.PS.VM) {
cpu.setProtMode(true, false);
}
/*
* TODO: Consider whether we can skip this loadProt() call if this.sel already contains selCode
* (and the previous mode matches, which might require we cache the mode in the X86Seg object, too).
*/
if (this.loadProt(selCode) === X86.ADDR_INVALID) {
return X86.ADDR_INVALID;
}
cpu.setDataSize(sizeGate);
this.offIP = limit;
cpu.assert(this.cpl == cplNew);
if (this.cpl < cplOld) {
if (fCall !== true) {
cpu.assert(false);
return X86.ADDR_INVALID;
}
regSP = cpu.getSP();
var i = 0, nWords = (acc & 0x1f);
while (nWords--) {
this.awParms[i++] = cpu.getSOWord(cpu.segSS, regSP);
regSP += 2;
}
regSSPrev = cpu.getSS();
regSPPrev = cpu.getSP();
cpu.setSS(selStack, true);
cpu.setSP(offStack);
if (regPS & X86.PS.VM) {
/*
* Frames coming from V86-mode ALWAYS contain 32-bit values, and look like this:
*
* low: EIP
* CS (upper 16 bits undefined)
* EFLAGS
* ESP
* SS (upper 16 bits undefined)
* ES (upper 16 bits undefined)
* DS (upper 16 bits undefined)
* FS (upper 16 bits undefined)
* high: GS (upper 16 bits undefined)
*
* Our caller (eg, fnINT()) will take care of pushing the final bits (EFLAGS, CS, and EIP).
*/
cpu.setDataSize(4);
cpu.assert(I386 && cpu.model >= X86.MODEL_80386);
cpu.pushData(cpu.segGS.sel, 4, 2);
cpu.setGS(0);
cpu.pushData(cpu.segFS.sel, 4, 2);
cpu.setFS(0);
cpu.pushData(cpu.segDS.sel, 4, 2);
cpu.setDS(0);
cpu.pushData(cpu.segES.sel, 4, 2);
cpu.setES(0);
}
cpu.pushData(regSSPrev, cpu.sizeData, 2);
cpu.pushWord(regSPPrev);
while (i) cpu.pushWord(this.awParms[--i]);
this.fStackSwitch = true;
}
return this.base;
}
}
if (sizeGate != 0) {
X86.fnFault.call(cpu, X86.EXCEPTION.GP_FAULT, (sel & X86.ERRCODE.SELMASK) | (fIDT? X86.ERRCODE.IDT : 0));
return X86.ADDR_INVALID;
}
if (!(acc & X86.DESC.ACC.PRESENT)) {
X86.fnFault.call(cpu, X86.EXCEPTION.NP_FAULT, (sel & X86.ERRCODE.SELMASK) | (fIDT? X86.ERRCODE.IDT : 0));