Orange's implementation of an operating system learning note--ldt

Source: Internet
Author: User

The LDT (Local descriptor table) is similar to the GDT, which is the difference between the descriptor Table and the global and local differences only. A local descriptor table can have several sheets, and each task can have one.

The LDT and GDT are essentially the same, but the LDT is nested within the GDT. The LDTR records the starting position of the local descriptor descriptor, and the content that differs from the GDTR LDTR is a segment selector. Since the LDT itself is also a piece of memory, is also a segment, so it also has a descriptor to describe it, the descriptor is stored in the GDT, corresponding to the descriptor will also have a selector, LDTR loaded is such a selector. LDTR can be changed at any time in the program by using the LLDT directive.

; ==========================================
; Pmtest3.asm; Compiling method: Nasm pmtest3.asm-o pmtest3.com; ==========================================; constants, macros, and some instructions da_32 EQU 4000h; 32-bit segment Da_c EQU 98h; Only execution code snippet property value exists Da_ldt EQU 82h; Local Descriptor Descriptor Type value DA_DRW EQU 92h; Existing read-write data segment attribute value Da_drwa EQU 93h; There is access to read-write data segment type value ATCE32 EQU 4098h; only 32 code snippet attribute value is present da_dpl1 EQU 20h; DPL = 1 sa_tig EQU 0; Place ti position 0 sa_til EQU 4; place ti position 1; The following is a macro definition; There are three parameters: segment bounds, Segment base, segment properties where the macro definition of% 1 represents the parameter 1,%2 represents the parameter 2,%3 represents the parameter 3%macro descriptor 3 DW%2 & 0FFFFh; Segment Boundary 1 (low 16 bits of parameter 2) DW%1 & 0FFFFh; Segment Base 1 (low 16 bits of parameter 1) db (% 1 >>) & 0FFh; Segment Base 2 (16-23 bits of parameter 1) DW ((% 2 >> 8) & 0f00h) | (% 3 & 0f000h) | (% 3 & 000FFh); Attribute 1 (high 4 bit) + segment bounds 2 (high 4 bit) + attribute 2 (low 8 bit) db (% 1 >>) & 0FFh; Segment Base 3 (24-31 bits of parameter 1)%endmacro; Total 8 bytes; segment bounds 20 bits, subgrade address 30 bits, segment attribute total 16 bits (with segment bounds high4-bit) org 0100h jmp label_begin [section. GDT];                                         GDT; Segment base, segment bounds, attributes Label_gdt:descriptor 0, 0, 0; Null descriptor Label_desc_normal:descriptor 0, 0FFFFH, DA_DRW; Normal Descriptor Label_desc_code32:descriptor 0, segcode32len-1, Da_c + da_32; Non-Uniform Code snippet, Label_desc_code16:descriptor 0, 0FFFFH, Da_c; Non-Uniform Code snippet, Label_desc_data:descriptor 0, DataLen-1, DA_DRW+DA_DPL1; Data Label_desc_stack:descriptor 0, Topofstack, Da_drwa + da_32; Stack, 32-bit label_desc_ldt:descriptor 0, LDTLen-1, Da_ldt; LDT label_desc_video:descriptor 0b8000h, 0FFFFH, DA_DRW; Memory first address; GDT ends Gdtlen equ $-LABEL_GDT; GDT length gdtptr DW GdtLen-1; GDT limit DD 0; GDT base Site; 		GDT Selector Sub Selectornormal equ label_desc_normal-label_gdt SelectorCode32 equ LABEL_DESC_CODE32-LABEL_GDT SelectorCode16 Equ LABEL_DESC_CODE16-LABEL_GDT SelectOrdata equ label_desc_data-label_gdt selectorstack equ label_desc_stack-label_gdt Selectorldt equ LABEL_DESC_LDT -Label_gdt selectorvideo equ Label_desc_video-label_gdt; END of [section. GDT] [section. data1]; Data section ALIGN [BITS] label_data:spvalueinrealmode DW 0; String Pmmessage:db "in Protect Mode now. ^-^ ", 0; This string is displayed after entering protected mode offsetpmmessage equ pmmessage-$$;
Pmmessage start address offset pmmessage2:db "Hello world!" Pmmessage2len equ $-PMMessage2 OffsetPMMessage2 equ PMMessage2-$$ datalen equ $-label_data; END of [section. Data1]; Global stack segment [section. GS] ALIGN [BITS] label_stack:times db 0; stack size topofstack equ $-label_stack-1; stack top pointer; END of [section: GS] [section. s16] [BITS] Label_begin:mov ax, CS mov ds, ax mov es, AX mov ss, ax mov sp, 0100 H mov [label_go_back_to_real+3], ax; If you do not understand, please look at the previous article Analysis mov [spvalueinrealmode], SP; Initialize 16-bit code Snippet descriptor mov ax, CS movzx eax, ax; 0 extension directive shl eax, 4; Segment value *16 add eax, LAbel_seg_code16; segment value *16+ offset = 16-bit code snippet base Address MOV word [Label_desc_code16 + 2], ax; base 1 shr eax, + mov byte [Label_desc_code16 + 4], AL; Base Address 2 mov byte [Label_desc_code16 + 7], ah; base Address 3; Initialize 32-bit code snippet Descriptor xor eax, EAX mov ax, cs shl eax, 4; Segment value *16 add eax, label_seg_code32; segment value *16+ offset = 16-bit code snippet base Address MOV word  [Label_desc_code32 + 2], ax; base Address 1 shr eax, + mov byte [label_desc_code32 + 4], AL; Base Address 2 mov byte [label_desc_code32 + 7], ah; base Address 3; Initialize data segment Descriptor xor eax, EAX mov ax, ds shl eax, 4; Segment value *16 add eax, Label_data; segment value *16+ offset = 16-bit code snippet base Address MOV word [label_desc _data + 2], ax; base Address 1 shr eax, + mov byte [Label_desc_data + 4], AL; Base Address 2 mov byte [Label_desc_data + 7], ah; base Address 3; Initialize the stack segment descriptor xor eax, EAX mov ax, ds shl eax, 4; Segment value *16 add eax, Label_stack; segment value *16+ offset = 16-bit code snippet base Address MOV word [label_des C_stack + 2], ax shr eax, 4 mov byte [Label_desc_stack +], AL mov byte [label_desc_stack + 7], ah; Initialize the LDT descriptor in the GDT xor eax, eax mov ax,DS shl eax, 4; Segment value *16 add eax, Label_ldt; segment value *16+ offset = 16-bit code segment base address (LDT table base address) MOV word [Label_desc_ldt + 2], ax; base 1 shr ea x, + mov byte [Label_desc_ldt + 4], AL; Base Address 2 mov byte [Label_desc_ldt + 7], ah; base Address 3; 
	Initialize the descriptor in the LDT xor eax, eax mov ax, ds shl eax, 4; Segment value *16 add eax, label_code_a; segment value *16+ offset = 16 bit code segment base address (base address of the code snippet corresponding to the LDT table) mov Word [Label_ldt_desc_codea + 2], ax; base Address 1 shr eax, + mov byte [Label_ldt_desc_codea + 4], AL; Base Address 2 mov byte [L Abel_ldt_desc_codea + 7], ah; base Address 3 xor eax, eax mov ax, ds shl eax, 4; Segment value *16 add eax, Label_code_b; segment value *16+ offset = 16 bit  Code snippet base Address (LDT table corresponding code snippet base address) MOV word [Label_ldt_desc_codeb + 2], ax; base 1 shr eax, 4 mov byte [Label_ldt_desc_codeb +], Al; Base Address 2 mov byte [Label_ldt_desc_codeb + 7], ah; base Address 3; Prepare for loading GDTR xor eax, eax mov ax, ds shl eax, 4; Base Address 2 add eax, LABEL_GDT; EAX <-GDT Base Address mov DWORD [gdtptr + 2], eax; [Gdtptr + 2] <-GDT base site; Load GDTR LGDT [gdtptr]; Off interrupt CLI; Open Address line A20 in Al, 92h or AL, 00000010b out 92h, AL; Ready to switch to protected mode mov eax, CR0 or eax, 1 mov cr0, eax; Real access to protected mode in JMP DWORD selectorcode32:0;  Executing this sentence will load the SelectorCode32 into CS and jump to code32selector:0

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Label_real_entry:; Jump back from Protected mode to real mode right here. mov ax, CS mov ds, ax mov es, AX mov ss, ax mov sp, [Spvalueinrealmode] in Al, 92h; ┓and al, 11111101b; ┣ off A20 Address line out 92H, AL; ┛sti; Open Interrupt mov ax, 4c00h; ┓int 21h; ┛ back to DOS; END of [section. s16] [section. S32]; 32-bit code snippet.
Jumped in by real mode.

[BITS 32] Label_seg_code32:mov ax, Selectordata mov ds, ax; Data Segment Selection Sub mov ax, selectorvideo mov gs, ax; Video Segment Select Sub mov ax, selectorstack mov ss, ax; Stack Segment Select Sub mov esp, topofstack; The following shows a string mov ah, 0Ch; 0000: Black Bottom 1100: Red Word xor esi, esi xor edi, EDI mov esi, offsetpmmessage; Source data offset mov edi, (80 * 10 + 0) * 2; Destination data offset.
	screen 10th, column No. 0.  CLD; Sets the direction of data transmission. 1:LODSB; from [Ds:esi] to a character into Al Test Al, AL      To determine whether to the end of the string (ending with 0) jz. 2; jump mov [Gs:edi], ax; no, input to video memory add EDI, 2; pointer plus 2 (one character is two bytes ) jmp. 1.2:; The call Dispreturn is displayed, and the line is changed; Load LDT mov ax, selectorldt Lldt ax; load LDT descriptor in GDT call selectorldtcodeb:0; Jump into local task push Selectorldtcodea xor eax,eax push eax retf; Jump into a local task; jmp selectorldtcodea:0; ------------------------------------------------------------------------dispreturn:push eax push EBX mov eax, EDI; get The current cursor's memory offset mov bl, 160, 80 characters per line, two bytes per character (character ascii + character attribute), so a line of total 80*2=160 bytes div bl; Gets the current number of lines (number of rows in video memory starting from 0) and EA x, 0FFh; take the lower 8 bits (on the current page, otherwise it is possible to output to another page, the monitor will not be displayed) Inc EAX; next line mov bl, mul bl; Determines the number of bytes to start on the next line mov edi, eax; update EDI Mailing Register pop ebx pop EAX ret; Dispreturn End---------------------------------------------------------Segcode32len equ $-label_seg_code32; END of [section. S32]; 16-bit code snippet. Jumped in from a 32-bit code snippet and jumped out into real mode [section. s16code] ALIGN [BITS] Label_seg_code16:; Jump back to Real mode: mov ax, selectornormal; Set the data segment in real mode mov ds, ax mov es, AX mov fs, AX MOV gs, AX mov ss, ax mov eax, CR0 and Al, 11111110b; set CR0 0 bits (PE bit, pe=0 ready to enter real mode) MOV cr0, eax; update cr0 label_go_back_to_real:jmp 0:label_real_entry; The segment address is set to the correct value at the beginning of the program Code16len equ $-label_seg_code16; END of [section. S16code];                            LDT [section. Ldt] ALIGN Label_ldt:; Segment Base segment bounds attribute Label_ldt_desc_codea:descriptor 0, CodeALen-1, Da_c + da_32; Code, 32-bit label_ldt_desc_codeb:descriptor 0, CodeBLen-1, Da_c + da_32; Code, 32-bit Ldtlen equ $-Label_ldt; Ldt table length; LDT Selector Sub Selectorldtcodea equ Label_ldt_desc_codea-label_ldt + sa_til selectorldtcodeb equ label_ldt_desc_codeb-label_l DT + sa_til; END of [section. Ldt]; Codea (LDT, 32-bit code snippet) [section. La] ALIGN [BITS] Label_code_a:mov ax, selectorvideo mov gs, ax; Video Segment Selector sub (purpose) MOV edi, (80 * 12 + 0) * 2;
	Screen 12th, column No. 0. mov ah, 0Ch;
0000: Black Bottom 1100: Red Word mov al, ' L ' mov [Gs:edi], ax
	;
Ready to jump back to real mode via 16-bit code snippet JMP selectorcode16:0 Codealen equ $-label_code_a; END of [section. La]; Codeb (LDT, 32-bit code snippet) [section. lb] ALIGN [BITS] Label_code_b:mov ax, selectorvideo mov gs, ax;
	Video Segment Selection sub (purpose) MOV ax,selectordata; mov ds,ax mov esi,offsetpmmessage2; MOV esi,pmmessage2 note cannot write mov Ecx,pmmessage2len mov edi, (80 * 1 1 + 0) * 2;
	Screen 11th, column No. 0. mov ah, 0Ch; 0000: Black Bottom 1100: Red word. Loop mov Al,[ds:esi] mov [Gs:edi], Ax add edi,2 inc ESI loop. Loop RETF Codeblen E Qu $-label_code_b; END of [section. lb]

Here are two local tasks, corresponding to two LDT tables, a local task is the output character ' L ', a task is output ' Hello world! ', of course, there is a global task, the input character ' in Protect mode now. ' Program first initializes the GDT in real LDT table, set the relevant register, enter the protection mode, output a string, and then load the local description of the LDT, into the local task B, return after execution, and then perform task A, task a completed after jumping into the 16-bit code snippet, set the relevant register value, back to real mode, end back to Dos.

Before entering the protection mode, we need to initialize the Gdt,ldt table, because in the protection mode, according to the corresponding section of the table, the location of the code or data, when there is a local task, the GDT should contain the location of the LDT table, so that when the processor in the Segment Descriptor table found in the LDT description table is found to be a local task , so that the LDT table can be found by the location of its recorded Ldt table, and then the corresponding segment descriptor is found in the LDT table, as shown in the following figure:


Segment Descriptor Table = Global Task (Gdt+ldt) The length value of the 32-bit linear base address and 16-bit tables used in the GDTR register to hold the Global descriptor descriptor GDT. The length value of the 32-bit linear base address and 16-bit table used in the LDTR register to hold the local descriptor descriptor LDT. By means of the system instruction, LGDT loads the linear base and length values of the GDT into the GDTR register, and Lldt loads the linear base and length values of the LDT into the LDTR register.

The following explains some of the code:

Call	selectorldtcodeb:0      ; jump into a local task
Through call into the local task B (inter-segment transfer), complete the corresponding character output.
Label_code_b:
	mov	ax, selectorvideo
	mov	gs, ax			; Video band selector (purpose)
	mov 	ax,selectordata;
	mov 	ds,ax

	mov     esi,offsetpmmessage2    ; mov     esi,pmmessage2 note can not write
	
	mov     ecx like this, Pmmessage2len
	mov	EDI, (* + 0) * 2	; screen line 11th, column No. 0.
	mov	ah, 0Ch			; 0000: Black Bottom    1100: Red word
 . Loop  mov     al,[ds:esi]
	mov	[Gs:edi], Ax
	Add     edi,2
	inc esi
	loop. loop
	
In task B, initialize the corresponding segment register and set the string pointer note the following statement:

mov     esi,offsetpmmessage2    ; mov     esi,pmmessage2 Note can't write like this
It is not possible to use the following statement, the offset address of the following statement in real mode, in the protected mode is relative to the current section of the offset address, which is described earlier, you can refer to the previous article.
When task B is complete, it is returned by RETF (Restore segment selector and offset). Next, get ready to go to mission a.

        Push    Selectorldtcodea
	xor eax,eax
	push eax
	retf	; jump into a local task
	; jmp	Selectorldtcodea :0</span>
Here the simulation is CALL-RETF instruction, the segment selector and offset into the stack, through the RETF stack data set CS,IP, to achieve the purpose of the jump. This is two local tasks A and B.

The following code is the "declaring" task, a, a, a, in the Ldt table, pointing to the corresponding code snippet, and then setting the appropriate selector:

; LDT
[section. Ldt]
ALIGN
Label_ldt:
;                            Segment base       segment bounds      attribute
label_ldt_desc_codea:descriptor 0, CodeALen-1, Da_c + da_32; Code, 32-bit
label_ldt_desc_codeb:descriptor 0, CodeBLen-1, Da_c + da_32; Code, 32-bit
Ldtlen		equ	$-Label_ldt; Ldt table length

; LDT Selector Sub
selectorldtcodea	equ	Label_ldt_desc_codea	-Label_ldt + sa_til
selectorldtcodeb	equ	Label_ldt_desc_codeb	-Label_ldt + sa_til
; END of [section. Ldt]
Operating results in pure DOS:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.