home *** CD-ROM | disk | FTP | other *** search
/ CD-X 1 / cdx_01.iso / demodisc / tyrant / docs / 386code / 10.386 < prev    next >
Encoding:
Text File  |  1994-01-20  |  28.8 KB  |  802 lines

  1. Chapter 10  Initialization
  2.  
  3. ────────────────────────────────────────────────────────────────────────────
  4.  
  5. After a signal on the RESET pin, certain registers of the 80386 are set to
  6. predefined values. These values are adequate to enable execution of a
  7. bootstrap program, but additional initialization must be performed by
  8. software before all the features of the processor can be utilized.
  9.  
  10.  
  11. 10.1  Processor State After Reset
  12.  
  13. The contents of EAX depend upon the results of the power-up self test. The
  14. self-test may be requested externally by assertion of BUSY# at the end of
  15. RESET. The EAX register holds zero if the 80386 passed the test. A nonzero
  16. value in EAX after self-test indicates that the particular 80386 unit is
  17. faulty. If the self-test is not requested, the contents of EAX after RESET
  18. is undefined.
  19.  
  20. DX holds a component identifier and revision number after RESET as Figure
  21. 10-1 illustrates. DH contains 3, which indicates an 80386 component. DL
  22. contains a unique identifier of the revision level.
  23.  
  24. Control register zero (CR0) contains the values shown in Figure 10-2. The
  25. ET bit of CR0 is set if an 80387 is present in the configuration (according
  26. to the state of the ERROR# pin after RESET). If ET is reset, the
  27. configuration either contains an 80287 or does not contain a coprocessor. A
  28. software test is required to distinguish between these latter two
  29. possibilities.
  30.  
  31. The remaining registers and flags are set as follows:
  32.  
  33.    EFLAGS             =00000002H
  34.    IP                 =0000FFF0H
  35.    CS selector        =000H
  36.    DS selector        =0000H
  37.    ES selector        =0000H
  38.    SS selector        =0000H
  39.    FS selector        =0000H
  40.    GS selector        =0000H
  41.    IDTR:
  42.               base    =0
  43.               limit   =03FFH
  44.  
  45. All registers not mentioned above are undefined.
  46.  
  47. These settings imply that the processor begins in real-address mode with
  48. interrupts disabled.
  49.  
  50.  
  51. Figure 10-1.  Contents of EDX after RESET
  52.  
  53.                                  EDX REGISTER
  54.  
  55.       31               23               15               7              0
  56.      ╔════════════════╪════════════════╪════════════════╪════════════════╗
  57.      ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│       DH       │      DL        ║
  58.      ║▒▒▒▒▒▒▒▒▒▒▒▒UNDEFINED▒▒▒▒▒▒▒▒▒▒▒▒│   DEVICE ID    │  STEPPING ID   ║
  59.      ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│       3        │   (UNIQUE)     ║
  60.      ╚════════════════╪════════════════╪════════════════╪════════════════╝
  61.  
  62.  
  63. Figure 10-2.  Initial Contents of CR0
  64.  
  65.                                CONTROL REGISTER ZERO
  66.  
  67.    31                23                15                  7     4 3   1  0
  68.   ╔═╤═══════════════╪═════════════════╪═══════════════════╪═════╤═╤═╤═╤═╤═╗
  69.   ║P│                                                           │E│T│E│M│P║
  70.   ║ │                            UNDEFINED                      │ │ │ │ │ ║
  71.   ║G│                                                           │T│S│M│P│E║
  72.   ╚╤╧═══════════════╪═════════════════╪═══════════════════╪═════╧╤╧╤╧╤╧╤╧╤╝
  73.    │                                                             │ │ │ │ │
  74.    └─────────────0 - PAGING DISABLED                             │ │ │ │ │
  75.                  * - INDICATES PRESENCE OF 80387─────────────────┘ │ │ │ │
  76.                  0 - NO TASK SWITCH────────────────────────────────┘ │ │ │
  77.                  0 - DO NOT MONITOR COPROCESSOR──────────────────────┘ │ │
  78.                  0 - COPROCESSOR NOT PRESENT───────────────────────────┘ │
  79.                  0 - PROTECTION NOT ENABLED (REAL ADDRESS MODE)──────────┘
  80.  
  81.  
  82. 10.2  Software Initialization for Real-Address Mode
  83.  
  84. In real-address mode a few structures must be initialized before a program
  85. can take advantage of all the features available in this mode.
  86.  
  87.  
  88. 10.2.1  Stack
  89.  
  90. No instructions that use the stack can be used until the stack-segment
  91. register (SS) has been loaded. SS must point to an area in RAM.
  92.  
  93.  
  94. 10.2.2  Interrupt Table
  95.  
  96. The initial state of the 80386 leaves interrupts disabled; however, the
  97. processor will still attempt to access the interrupt table if an exception
  98. or nonmaskable interrupt (NMI) occurs. Initialization software should take
  99. one of the following actions:
  100.  
  101.   ■  Change the limit value in the IDTR to zero. This will cause a shutdown
  102.      if an exception or nonmaskable interrupt occurs. (Refer to the 80386
  103.      Hardware Reference Manual to see how shutdown is signalled externally.)
  104.  
  105.   ■  Put pointers to valid interrupt handlers in all positions of the
  106.      interrupt table that might be used by exceptions or interrupts.
  107.  
  108.   ■  Change the IDTR to point to a valid interrupt table.
  109.  
  110.  
  111. 10.2.3  First Instructions
  112.  
  113. After RESET, address lines A{31-20} are automatically asserted for
  114. instruction fetches. This fact, together with the initial values of CS:IP,
  115. causes instruction execution to begin at physical address FFFFFFF0H. Near
  116. (intrasegment) forms of control transfer instructions may be used to pass
  117. control to other addresses in the upper 64K bytes of the address space. The
  118. first far (intersegment) JMP or CALL instruction causes A{31-20} to drop
  119. low, and the 80386 continues executing instructions in the lower one
  120. megabyte of physical memory. This automatic assertion of address lines
  121. A{31-20} allows systems designers to use a ROM at the high end of
  122. the address space to initialize the system.
  123.  
  124.  
  125. 10.3  Switching to Protected Mode
  126.  
  127. Setting the PE bit of the MSW in CR0 causes the 80386 to begin executing in
  128. protected mode. The current privilege level (CPL) starts at zero. The
  129. segment registers continue to point to the same linear addresses as in real
  130. address mode (in real address mode, linear addresses are the same physical
  131. addresses).
  132.  
  133. Immediately after setting the PE flag, the initialization code must flush
  134. the processor's instruction prefetch queue by executing a JMP instruction.
  135. The 80386 fetches and decodes instructions and addresses before they are
  136. used; however, after a change into protected mode, the prefetched
  137. instruction information (which pertains to real-address mode) is no longer
  138. valid. A JMP forces the processor to discard the invalid information.
  139.  
  140.  
  141. 10.4  Software Initialization for Protected Mode
  142.  
  143. Most of the initialization needed for protected mode can be done either
  144. before or after switching to protected mode. If done in protected mode,
  145. however, the initialization procedures must not use protected-mode features
  146. that are not yet initialized.
  147.  
  148.  
  149. 10.4.1  Interrupt Descriptor Table
  150.  
  151. The IDTR may be loaded in either real-address or protected mode. However,
  152. the format of the interrupt table for protected mode is different than that
  153. for real-address mode. It is not possible to change to protected mode and
  154. change interrupt table formats at the same time; therefore, it is inevitable
  155. that, if IDTR selects an interrupt table, it will have the wrong format at
  156. some time. An interrupt or exception that occurs at this time will have
  157. unpredictable results. To avoid this unpredictability, interrupts should
  158. remain disabled until interrupt handlers are in place and a valid IDT has
  159. been created in protected mode.
  160.  
  161.  
  162. 10.4.2  Stack
  163.  
  164. The SS register may be loaded in either real-address mode or protected
  165. mode. If loaded in real-address mode, SS continues to point to the same
  166. linear base-address after the switch to protected mode.
  167.  
  168.  
  169. 10.4.3  Global Descriptor Table
  170.  
  171. Before any segment register is changed in protected mode, the GDT register
  172. must point to a valid GDT. Initialization of the GDT and GDTR may be done in
  173. real-address mode. The GDT (as well as LDTs) should reside in RAM, because
  174. the processor modifies the accessed bit of descriptors.
  175.  
  176.  
  177. 10.4.4  Page Tables
  178.  
  179. Page tables and the PDBR in CR3 can be initialized in either real-address
  180. mode or in protected mode; however, the paging enabled (PG) bit of CR0
  181. cannot be set until the processor is in protected mode. PG may be set
  182. simultaneously with PE, or later. When PG is set, the PDBR in CR3 should
  183. already be initialized with a physical address that points to a valid page
  184. directory. The initialization procedure should adopt one of the following
  185. strategies to ensure consistent addressing before and after paging is
  186. enabled:
  187.  
  188.   ■  The page that is currently being executed should map to the same
  189.      physical addresses both before and after PG is set.
  190.  
  191.   ■  A JMP instruction should immediately follow the setting of PG.
  192.  
  193.  
  194. 10.4.5  First Task
  195.  
  196. The initialization procedure can run awhile in protected mode without
  197. initializing the task register; however, before the first task switch, the
  198. following conditions must prevail:
  199.  
  200.   ■  There must be a valid task state segment (TSS) for the new task. The
  201.      stack pointers in the TSS for privilege levels numerically less than or
  202.      equal to the initial CPL must point to valid stack segments.
  203.  
  204.   ■  The task register must point to an area in which to save the current
  205.      task state. After the first task switch, the information dumped in this
  206.      area is not needed, and the area can be used for other purposes.
  207.  
  208.  
  209. 10.5  Initialization Example
  210.  
  211. $TITLE ('Initial Task')
  212.  
  213.     NAME    INIT
  214.  
  215. init_stack  SEGMENT RW
  216.             DW  20  DUP(?)
  217. tos         LABEL   WORD
  218. init_stack  ENDS
  219.  
  220. init_data   SEGMENT RW PUBLIC
  221.             DW  20  DUP(?)
  222. init_data   ENDS
  223.  
  224. init_code   SEGMENT ER PUBLIC
  225.  
  226. ASSUME      DS:init_data
  227.  
  228.     nop
  229.     nop
  230.     nop
  231. init_start:
  232.                                     ; set up stack
  233.     mov ax, init_stack
  234.     mov ss, ax
  235.     mov esp, offset tos
  236.  
  237.     mov a1,1
  238. blink:
  239.     xor a1,1
  240.     out 0e4h,a1
  241.     mov cx,3FFFh
  242. here:
  243.     dec cx
  244.     jnz here
  245.  
  246.     jmp SHORT blink
  247.  
  248.     hlt
  249. init_code   ends
  250.  
  251.     END init_start, SS:init_stack, DS:init_data
  252.  
  253. $TITLE('Protected Mode Transition -- 386 initialization')
  254. NAME  RESET
  255.  
  256. ;*****************************************************************
  257. ; Upon reset the 386 starts executing at address 0FFFFFFF0H.  The
  258. ; upper 12 address bits remain high until a FAR call or jump is
  259. ; executed.
  260. ;
  261. ; Assume the following:
  262. ;
  263. ;
  264. ; -  a short jump at address 0FFFFFFF0H (placed there by the
  265. ;    system builder) causes execution to begin at START in segment
  266. ;    RESET_CODE.
  267. ;
  268. ;
  269. ; -  segment RESET_CODE is based at physical address 0FFFF0000H,
  270. ;    i.e.   at the start of the last  64K in the 4G address space.
  271. ;    Note that  this is the base of the CS register at reset.  If
  272. ;    you locate ROMcode above  this  address,  you  will  need  to
  273. ;    figure out an adjustment factor to address things within this
  274. ;    segment.
  275. ;
  276. ;*****************************************************************
  277. $EJECT ;
  278.  
  279. ; Define addresses to locate GDT and IDT in RAM.
  280. ; These addresses are also used in the BLD386 file that defines
  281. ; the GDT and IDT. If you change these addresses, make sure you
  282. ; change the base addresses specified in the build file.
  283.  
  284. GDTbase         EQU    00001000H   ; physical address for GDT base
  285. IDTbase         EQU    00000400H   ; physical address for IDT base
  286.  
  287. PUBLIC     GDT_EPROM
  288. PUBLIC     IDT_EPROM
  289. PUBLIC     START
  290.  
  291. DUMMY      segment rw      ; ONLY for ASM386 main module stack init
  292.            DW 0
  293. DUMMY   ends
  294.  
  295. ;*****************************************************************
  296. ;
  297. ; Note: RESET CODE must be USEl6 because the 386 initally executes
  298. ;       in real mode.
  299. ;
  300.  
  301. RESET_CODE segment er PUBLIC    USE16
  302.  
  303. ASSUME DS:nothing, ES:nothing
  304.  
  305. ;
  306. ; 386 Descriptor template
  307.  
  308. DESC       STRUC
  309.     lim_0_15    DW  0              ; limit bits (0..15)
  310.     bas_0_15    DW  0              ; base bits (0..15)
  311.     bas_16_23   DB  0              ; base bits (16..23)
  312.     access      DB  0              ; access byte
  313.     gran        DB  0              ; granularity byte
  314.     bas_24_31   DB  0              ; base bits (24..31)
  315. DESC       ENDS
  316.  
  317. ; The following is the layout of the real GDT created by BLD386.
  318. ; It is located in EPROM and will be copied to RAM.
  319. ;
  320. ; GDT[O]      ...  NULL
  321. ; GDT[1]      ...  Alias for RAM GDT
  322. ; GDT[2]      ...  Alias for RAM IDT
  323. ; GDT[2]      ...  initial task TSS
  324. ; GDT[3]      ...  initial task TSS alias
  325. ; GDT[4]      ...  initial task LDT
  326. ; GDT[5]      ...  initial task LDT alias
  327.  
  328. ;
  329. ; define entries in GDT and IDT.
  330.  
  331. GDT_ENTRIES    EQU    8
  332. IDT_ENTRIES    EQU    32
  333.  
  334. ; define some constants to index into the real GDT
  335.  
  336. GDT_ALIAS      EQU    1*SIZE DESC
  337. IDT_ALIAS      EQU    2*SIZE DESC
  338. INIT_TSS       EQU    3*SIZE DESC
  339. INIT_TSS_A     EQU    4*SIZE DESC
  340. INIT_LDT       EQU    5*SIZE DESC
  341. INIT_LDT_A     EQU    6*SIZE DESC
  342.  
  343. ;
  344. ; location of alias in INIT_LDT
  345.  
  346. INIT_LDT_ALIAS    EQU    1*SIZE DESC
  347.  
  348. ;
  349. ; access rights byte for DATA and TSS descriptors
  350.  
  351. DS_ACCESS   EQU   010010010B
  352. TSS_ACCESS  EQU   010001001B
  353.  
  354.  
  355. ;
  356. ; This temporary GDT will be used to set up the real GDT in RAM.
  357.  
  358. Temp_GDT    LABEL   BYTE        ; tag for begin of scratch GDT
  359.  
  360. NULL_DES    DESC <>             ; NULL descriptor
  361.  
  362.                                 ; 32-Gigabyte data segment based at 0
  363. FLAT_DES    DESC <0FFFFH,0,0,92h,0CFh,0>
  364.  
  365. GDT_eprom     DP    ?           ; Builder places GDT address and limit
  366.                                 ; in this 6 byte area.
  367.  
  368. IDT_eprom     DP    ?           ; Builder places IDT address and limit
  369.                                 ; in this 6 byte area.
  370.  
  371. ;
  372. ; Prepare operand for loadings GDTR and LDTR.
  373.  
  374.  
  375. TGDT_pword     LABEL  PWORD                 ; for temp GDT
  376.         DW     end_Temp_GDT_Temp_GDT -1
  377.         DD     0
  378.  
  379. GDT_pword      LABEL  PWORD                 ; for GDT in RAM
  380.         DW     GDT_ENTRIES * SIZE DESC -1
  381.         DD     GDTbase
  382.  
  383. IDT_pword      LABEL   PWORD                ; for IDT in RAM
  384.         DW     IDT_ENTRIES * SIZE DESC -1
  385.         DD     IDTbase
  386.  
  387.  
  388. end_Temp_GDT   LABEL   BYTE
  389.  
  390. ;
  391. ; Define equates for addressing convenience.
  392.  
  393. GDT_DES_FLAT        EQU DS:GDT_ALIAS +GDTbase
  394. IDT_DES_FLAT        EQU DS:IDT_ALIAS +GDTbase
  395.  
  396. INIT_TSS_A_OFFSET   EQU DS:INIT_TSS_A
  397. INIT_TSS_OFFSET     EQU DS:INIT_TSS
  398.  
  399. INIT_LDT_A_OFFSET   EQU DS:INIT_LDT_A
  400. INIT_LDT_OFFSET     EQU DS:INIT_LDT
  401.  
  402.  
  403. ; define pointer for first task switch
  404.  
  405. ENTRY POINTER LABEL DWORD
  406.              DW 0, INIT_TSS
  407.  
  408. ;******************************************************************
  409. ;
  410. ;   Jump from reset vector to here.
  411.  
  412. START:
  413.  
  414.     CLI                ;disable interrupts
  415.     CLD                ;clear direction flag
  416.  
  417.     LIDT    NULL_des   ;force shutdown on errors
  418.  
  419. ;
  420. ;   move scratch GDT to RAM at physical 0
  421.  
  422.     XOR DI,DI
  423.     MOV ES,DI           ;point ES:DI to physical location 0
  424.  
  425.  
  426.     MOV SI,OFFSET Temp_GDT
  427.     MOV CX,end_Temp_GDT-Temp_GDT        ;set byte count
  428.     INC CX
  429. ;
  430. ;   move table
  431.  
  432.     REP MOVS BYTE PTR ES:[DI],BYTE PTR CS:[SI]
  433.  
  434.     LGDT    tGDT_pword                ;load GDTR for Temp. GDT
  435.                                       ;(located at 0)
  436.  
  437. ;   switch to protected mode
  438.  
  439.     MOV EAX,CR0                       ;get current CRO
  440.     MOV EAX,1                         ;set PE bit
  441.     MOV CRO,EAX                       ;begin protected mode
  442. ;
  443. ;   clear prefetch queue
  444.  
  445.     JMP SHORT flush
  446. flush:
  447.  
  448. ; set DS,ES,SS to address flat linear space (0 ... 4GB)
  449.  
  450.     MOV BX,FLAT_DES-Temp_GDT
  451.     MOV US,BX
  452.     MOV ES,BX
  453.     MOV SS,BX
  454. ;
  455. ; initialize stack pointer to some (arbitrary) RAM location
  456.  
  457.     MOV ESP, OFFSET end_Temp_GDT
  458.  
  459. ;
  460. ; copy eprom GDT to RAM
  461.  
  462.     MOV ESI,DWORD PTR GDT_eprom +2 ; get base of eprom GDT
  463.                                    ; (put here by builder).
  464.  
  465.     MOV EDI,GDTbase                ; point ES:EDI to GDT base in RAM.
  466.  
  467.     MOV CX,WORD PTR gdt_eprom +0   ; limit of eprom GDT
  468.     INC CX
  469.     SHR CX,1                       ; easier to move words
  470.     CLD
  471.     REP MOVS   WORD PTR ES:[EDI],WORD PTR DS:[ESI]
  472.  
  473. ;
  474. ; copy eprom IDT to RAM
  475. ;
  476.     MOV ESI,DWORD PTR IDT_eprom +2 ; get base of eprom IDT
  477.                                    ; (put here by builder)
  478.  
  479.     MOV EDI,IDTbase                ; point ES:EDI to IDT base in RAM.
  480.  
  481.     MOV CX,WORD PTR idt_eprom +0   ; limit of eprom IDT
  482.     INC CX
  483.     SHR CX,1
  484.     CLD
  485.     REP MOVS   WORD PTR ES:[EDI],WORD PTR DS:[ESI]
  486.  
  487. ; switch to RAM GDT and IDT
  488. ;
  489.     LIDT IDT_pword
  490.     LGDT GDT_pword
  491.  
  492. ;
  493.     MOV BX,GDT_ALIAS               ; point DS to GDT alias
  494.     MOV DS,BX
  495. ;
  496. ; copy eprom TSS to RAM
  497. ;
  498.     MOV BX,INIT_TSS_A              ; INIT TSS A descriptor base
  499.                                    ; has RAM location of INIT TSS.
  500.  
  501.     MOV ES,BX                      ; ES points to TSS in RAM
  502.  
  503.     MOV BX,INIT_TSS                ; get inital task selector
  504.     LAR DX,BX                      ; save access byte
  505.     MOV [BX].access,DS_ACCESS      ; set access as data segment
  506.     MOV FS,BX                      ; FS points to eprom TSS
  507.  
  508.     XOR si,si                      ; FS:si points to eprom TSS
  509.     XOR di,di                      ; ES:di points to RAM TSS
  510.  
  511.     MOV CX,[BX].lim_0_15           ; get count to move
  512.     INC CX
  513.  
  514. ;
  515. ; move INIT_TSS to RAM.
  516.  
  517.     REP MOVS BYTE PTR ES:[di],BYTE PTR FS:[si]
  518.  
  519.     MOV [BX].access,DH             ; restore access byte
  520.  
  521. ;
  522. ; change base of INIT TSS descriptor to point to RAM.
  523.  
  524.     MOV AX,INIT_TSS_A_OFFSET.bas_0_15
  525.     MOV INIT_TSS_OFFSET.bas_0_15,AX
  526.     MOV AL,INIT_TSS_A_OFFSET.bas_16_23
  527.     MOV INIT_TSS_OFFSET.bas_16_23,AL
  528.     MOV AL,INIT_TSS_A_OFFSET.bas_24_31
  529.     MOV INIT_TSS_OFFSET.bas_24_31,AL
  530.  
  531. ;
  532. ; change INIT TSS A to form a save area for TSS on first task
  533. ; switch. Use RAM at location 0.
  534.  
  535.     MOV BX,INIT_TSS_A
  536.     MOV WORD PTR [BX].bas_0_15,0
  537.     MOV [BX].bas_16_23,0
  538.     MOV [BX].bas_24_31,0
  539.     MOV [BX].access,TSS_ACCESS
  540.     MOV [BX].gran,O
  541.     LTR BX                         ; defines save area for TSS
  542.  
  543. ;
  544. ; copy eprom LDT to RAM
  545.  
  546.     MOV BX,INIT_LDT_A              ; INIT_LDT_A descriptor has
  547.                                    ; base address in RAM for INIT_LDT.
  548.  
  549.     MOV ES,BX                      ; ES points LDT location in RAM.
  550.  
  551.     MOV AH,[BX].bas_24_31
  552.     MOV AL,[BX].bas_16_23
  553.     SHL EAX,16
  554.     MOV AX,[BX].bas_0_15           ; save INIT_LDT base (ram) in EAX
  555.  
  556.     MOV BX,INIT_LDT                ; get inital LDT selector
  557.     LAR DX,BX                      ; save access rights
  558.     MOV [BX].access,DS_ACCESS      ; set access as data segment
  559.     MOV FS,BX                      ; FS points to eprom LDT
  560.  
  561.     XOR si,si                      ; FS:SI points to eprom LDT
  562.     XOR di,di                      ; ES:DI points to RAM LDT
  563.  
  564.     MOV CX,[BX].lim_0_15           ; get count to move
  565.     INC CX
  566. ;
  567. ; move initial LDT to RAM
  568.  
  569.     REP MOVS BYTE PTR ES:[di],BYTE PTR FS:[si]
  570.  
  571.     MOV [BX].access,DH             ; restore access rights in
  572.                                    ; INIT_LDT descriptor
  573.  
  574. ;
  575. ; change base of alias (of INIT_LDT) to point to location in RAM.
  576.  
  577.     MOV ES:[INIT_LDT_ALIAS].bas_0_15,AX
  578.     SHR EAX,16
  579.     MOV ES:[INIT_LDT_ALIAS].bas_16_23,AL
  580.     MOV ES:[INIT_LDT_ALIAS].bas_24_31,AH
  581.  
  582. ;
  583. ; now set the base value in INIT_LDT descriptor
  584.  
  585.     MOV AX,INIT_LDT_A_OFFSET.bas_0_15
  586.     MOV INIT_LDT_OFFSET.bas_0_15,AX
  587.     MOV AL,INIT_LDT_A_OFFSET.bas_16_23
  588.     MOV INIT_LDT_OFFSET.bas_16_23,AL
  589.     MOV AL,INIT_LDT_A_OFFSET.bas_24_31
  590.     MOV INIT_LDT_OFFSET.bas_24_31,AL
  591.  
  592. ;
  593. ; Now GDT, IDT, initial TSS and initial LDT are all set up.
  594. ;
  595. ; Start the first task!
  596. '
  597.    JMP ENTRY_POINTER
  598.  
  599. RESET_CODE ends
  600.    END START, SS:DUMMY,DS:DUMMY
  601.  
  602.  
  603. 10.6  TLB Testing
  604.  
  605. The 80386 provides a mechanism for testing the Translation Lookaside Buffer
  606. (TLB), the cache used for translating linear addresses to physical
  607. addresses. Although failure of the TLB hardware is extremely unlikely, users
  608. may wish to include TLB confidence tests among other power-up confidence
  609. tests for the 80386.
  610.  
  611. ───────────────────────────────────────────────────────────────────────────
  612. NOTE
  613.   This TLB testing mechanism is unique to the 80386 and may not be
  614.   continued in the same way in future processors. Sortware that uses
  615.   this mechanism may be incompatible with future processors.
  616. ───────────────────────────────────────────────────────────────────────────
  617.  
  618. When testing the TLB it is recommended that paging be turned off (PG=0 in
  619. CR0) to avoid interference with the test data being written to the TLB.
  620.  
  621.  
  622. 10.6.1  Structure of the TLB
  623.  
  624. The TLB is a four-way set-associative memory. Figure 10-3 illustrates the
  625. structure of the TLB. There are four sets of eight entries each. Each entry
  626. consists of a tag and data. Tags are 24-bits wide. They contain the
  627. high-order 20 bits of the linear address, the valid bit, and three attribute
  628. bits. The data portion of each entry contains the high-order 20 bits of the
  629. physical address.
  630.  
  631.  
  632. 10.6.2  Test Registers
  633.  
  634. Two test registers, shown in Figure 10-4, are provided for the purpose of
  635. testing. TR6 is the test command register, and TR7 is the test data
  636. register. These registers are accessed by variants of the MOV
  637. instruction. A test register may be either the source operand or destination
  638. operand. The MOV instructions are defined in both real-address mode and
  639. protected mode. The test registers are privileged resources; in protected
  640. mode, the MOV instructions that access them can only be executed at
  641. privilege level 0. An attempt to read or write the test registers when
  642. executing at any other privilege level causes a general
  643. protection exception.
  644.  
  645. The test command register (TR6) contains a command and an address tag to
  646. use in performing the command:
  647.  
  648. C         This is the command bit. There are two TLB testing commands:
  649.           write entries into the TLB, and perform TLB lookups. To cause an
  650.           immediate write into the TLB entry, move a doubleword into TR6
  651.           that contains a 0 in this bit. To cause an immediate TLB lookup,
  652.           move a doubleword into TR6 that contains a 1 in this bit.
  653.  
  654. Linear    On a TLB write, a TLB entry is allocated to this linear address;
  655. Address   the rest of that TLB entry is set per the value of TR7 and the
  656.           value just written into TR6. On a TLB lookup, the TLB is
  657.           interrogated per this value; if one and only one TLB entry
  658.           matches, the rest of the fields of TR6 and TR7 are set from the
  659.           matching TLB entry.
  660.  
  661. V         The valid bit for this TLB entry. The TLB uses the valid bit to
  662.           identify entries that contain valid data. Entries of the TLB
  663.           that have not been assigned values have zero in the valid bit.
  664.           All valid bits can be cleared by writing to CR3.
  665.  
  666. D, D#     The dirty bit (and its complement) for/from the TLB entry.
  667.  
  668. U, U#     The U/S bit (and its complement) for/from the TLB entry.
  669.  
  670. W, W#     The R/W bit (and its complement) for/from the TLB entry.
  671.  
  672.           The meaning of these pairs of bits is given by Table 10-1,
  673.           where X represents D, U, or W.
  674.  
  675. The test data register (TR7) holds data read from or data to be written to
  676. the TLB.
  677.  
  678. Physical  This is the data field of the TLB. On a write to the TLB, the
  679. Address   TLB entry allocated to the linear address in TR6 is set to this
  680.           value. On a TLB lookup, if HT is set, the data field (physical
  681.           address) from the TLB is read out to this field. If HT is not
  682.           set, this field is undefined.
  683.  
  684. HT        For a TLB lookup, the HT bit indicates whether the lookup was a
  685.           hit (HT  1) or a miss (HT  0). For a TLB write, HT must be set
  686.           to 1.
  687.  
  688. REP       For a TLB write, selects which of four associative blocks of the
  689.           TLB is to be written. For a TLB read, if HT is set, REP reports
  690.           in which of the four associative blocks the tag was found; if HT
  691.           is not set, REP is undefined.
  692.  
  693.  
  694. Table 10-1. Meaning of D, U, and W Bit Pairs
  695.  
  696. X     X#      Effect during        Value of bit X
  697.               TLB Lookup           after TLB Write
  698.  
  699. 0     0       (undefined)          (undefined)
  700. 0     1       Match if X=0         Bit X becomes 0
  701. 1     0       Match if X=1         Bit X becomes 1
  702. 1     1       (undefined)          (undefined)
  703.  
  704.  
  705. Figure 10-3.  TLB Structure
  706.  
  707.                                    ╔═════════════════╦════════════════╗
  708.                                   7║       TAG       ║      DATA      ║
  709.                                    ╠═════════════════╬════════════════╣
  710.                                                                     
  711.                  ┌───────                                           
  712.                  │       SET 11                                     
  713.                  │    ┌──          ╠═════════════════╬════════════════╣
  714.                  │    │           1║       TAG       ║      DATA      ║
  715.                  │    │            ╠═════════════════╬════════════════╣
  716.                  │    │           0║       TAG       ║      DATA      ║
  717.                  │    │            ╚═════════════════╩════════════════╝
  718.                  │    │
  719.                  │    │            ╔═════════════════╦════════════════╗
  720.                  │    │           7║       TAG       ║      DATA      ║
  721.                  │    │            ╠═════════════════╬════════════════╣
  722.                  │    │                                             
  723.                  │    └──                                           
  724.                  │       SET 10                                     
  725.                  │    ┌──          ╠═════════════════╬════════════════╣
  726.                  │    │           1║       TAG       ║      DATA      ║
  727.       │ D │      │    │            ╠═════════════════╬════════════════╣
  728.       │ A │      │    │           0║       TAG       ║      DATA      ║
  729.       │ T └──────┘    │            ╚═════════════════╩════════════════╝
  730.       │ A             │
  731.       │   ┌──────┐    │            ╔═════════════════╦════════════════╗
  732.       │ B │      │    │           7║       TAG       ║      DATA      ║
  733.       │ U │      │    │            ╠═════════════════╬════════════════╣
  734.       │ S │      │    │                                             
  735.                  │    └──                                           
  736.                  │       SET 01                                     
  737.                  │    ┌──          ╠═════════════════╬════════════════╣
  738.                  │    │           1║       TAG       ║      DATA      ║
  739.                  │    │            ╠═════════════════╬════════════════╣
  740.                  │    │           0║       TAG       ║      DATA      ║
  741.                  │    │            ╚═════════════════╩════════════════╝
  742.                  │    │
  743.                  │    │            ╔═════════════════╦════════════════╗
  744.                  │    │           7║       TAG       ║      DATA      ║
  745.                  │    │            ╠═════════════════╬════════════════╣
  746.                  │    │                                             
  747.                  │    └──                                           
  748.                  │       SET 00                                     
  749.                  └───────          ╠═════════════════╬════════════════╣
  750.                                   1║       TAG       ║      DATA      ║
  751.                                    ╠═════════════════╬════════════════╣
  752.                                   0║       TAG       ║      DATA      ║
  753.                                    ╚═════════════════╩════════════════╝
  754.  
  755.  
  756. Figure 10-4.  Test Registers
  757.  
  758.       31                23              15   11      7             0
  759.      ╔═════════════════╪═══════════════╪════╪═══════╪═════╤═╤═══╤═══╗
  760.      ║                                      │             │H│   │   ║
  761.      ║           PHYSICAL ADDRESS           │0 0 0 0 0 0 0│ │REP│0 0║ TR7
  762.      ║                                      │             │T│   │   ║
  763.      ╟──────────────────────────────────────┼─┬─┬─┬─┬─┬─┬─┼─┴───┴─┬─╢
  764.      ║                                      │ │ │D│ │U│ │W│       │ ║
  765.      ║            LINEAR ADDRESS            │V│D│ │U│ │ │ │0 0 0 0│C║ TR8
  766.      ║                                      │ │ │#│ │#│ │#│       │ ║
  767.      ╚═════════════════╪═══════════════╪════╪═╧═╧═╧═╪═╧═╧═╧═══════╧═╝
  768.  
  769.      NOTE: 0 INDICATES INTEL RESERVED. NO NOT DEFINE
  770.  
  771.  
  772. 10.6.3  Test Operations
  773.  
  774. To write a TLB entry:
  775.  
  776.   1.  Move a doubleword to TR7 that contains the desired physical address,
  777.       HT, and REP values. HT must contain 1. REP must point to the
  778.       associative block in which to place the entry.
  779.  
  780.   2.  Move a doubleword to TR6 that contains the appropriate linear
  781.       address, and values for V, D, U, and W. Be sure C=0 for "write"
  782.       command.
  783.  
  784. Be careful not to write duplicate tags; the results of doing so are
  785. undefined.
  786.  
  787. To look up (read) a TLB entry:
  788.  
  789.   1.  Move a doubleword to TR6 that contains the appropriate linear address
  790.       and attributes. Be sure C=1 for "lookup" command.
  791.  
  792.   2.  Store TR7. If the HT bit in TR7 indicates a hit, then the other
  793.       values reveal the TLB contents. If HT indicates a miss, then the other
  794.       values in TR7 are indeterminate.
  795.  
  796. For the purposes of testing, the V bit functions as another bit of
  797. addresss.  The V bit for a lookup request should usually be set, so that
  798. uninitialized tags do not match. Lookups with V=0 are unpredictable if any
  799. tags are uninitialized.
  800.  
  801.  
  802.