minzkn/º¸È£¸ðµå
º¸È£¸ðµå (x86) ¶1.1. Àо±â Àü¿¡ ¶
1.2. Segment register ¶
1.3. Segment descriptor ¶
1.3.1. DescriptorÀÇ ±¸Á¶ ¶
1.3.2. SelectorÀÇ ±¸Á¶ ¶
1.3.3. Segment ÀåÄ¡ ¶
1.3.4. System descriptorÀÇ Á¾·ù ¶
1.3.5. Gate descriptor ¶
1.3.6. General protectionÀ§¹ÝÀÇ Á¾·ù ¶ÀÌ°ÍÀº Interrupt 0x0d¿Í ¹ÐÁ¢ÇÑ °ü·ÃÀÌ ÀÖ½À´Ï´Ù.
1.4. Paging ¶
1.5. È®ÀåµÈ Paging ¶
1.6. Three-level paging ¶
1.7. Hardware cache ¶
1.7.1. Translation Lookaside Buffers (TLB) ¶
1.8. GDT/IDT Setup -> º¸È£¸ðµå ÁøÀÔ ¹× ¸®¾ó¸ðµå µ¹¾Æ¿À±â ¿¹Á¦ ¶
; Copyright (c) MINZ
; Code by JaeHyuk Cho - <mailto:minzkn@infoeq.com> DEF_ASM_GO32 EQU "GO32.ASM" DEF_MAX_DefaultGDT = 2000h ; 8192d DEF_MAX_DefaultIDT = 0100h ; 256d PUBLIC RegisterIDT, RegisterGDT PUBLIC SetupIDT, SetupGDT PUBLIC Go32 PUBLIC L_Exit32 ; PUBLIC D_StackFrame PUBLIC D_IDTR, D_GDTR PUBLIC D_IDT_Item, D_GDT_Item PUBLIC D_IDT, D_GDT ASSUME CS:CODE_GO32, DS:DATA_GO32, ES:NOTHING, SS:STACK_DEFAULT CODE_GO32 SEGMENT
RegisterIDT PROC FAR ; void far pascal RegisterIDT(unsigned int s_intnum) ; IDT Ãß°¡ »ý¼º
PUSH BP MOV BP, SP PUSH EAX PUSH EDX XOR EAX, EAX MOV AX, WORD PTR [BP + 06h] ; s_intnum MOV DX, DEF_SIZE_Descriptor MUL DX ADD EAX, OFFSET DESC_GO32_IDT:D_IDT PUSH DESC_GO32_IDT PUSH AX PUSH DATA_GO32 PUSH OFFSET DATA_GO32:D_IDT_Item PUSH DEF_SIZE_Descriptor CALL FAR PTR CODE_MEMORY:MemCpy POP EDX POP EAX POP BP RETF 2 RegisterIDT ENDP
RegisterGDT PROC FAR ; void far pascal RegisterGDT(unsigned int s_descnum)
PUSH BP MOV BP, SP PUSH EAX PUSH EDX XOR EAX, EAX MOV AX, WORD PTR [BP + 06h] ; s_descnum MOV DX, DEF_SIZE_Descriptor MUL DX ADD EAX, OFFSET DESC_GO32_GDT:D_GDT PUSH DESC_GO32_GDT PUSH AX PUSH DATA_GO32 PUSH OFFSET DATA_GO32:D_GDT_Item PUSH DEF_SIZE_Descriptor CALL FAR PTR CODE_MEMORY:MemCpy POP EDX POP EAX POP BP RETF 2 RegisterGDT ENDP
SetupIDT PROC FAR ; void far pascal SetupIDT(void)
PUSH DS PUSH AX MOV AX, DATA_GO32 MOV DS, AX POP AX POP DS RETF SetupIDT ENDP
SetupGDT PROC FAR ; void far pascal SetupGDT(void)
PUSH DS PUSH AX PUSH BX MOV AX, DATA_GO32 MOV DS, AX ; 0000h - Null descriptor XOR AX, AX MOV WORD PTR DATA_GO32:D_GDT_Item.STRUC_LimitLow, AX MOV WORD PTR DATA_GO32:D_GDT_Item.STRUC_BaseLow, AX MOV BYTE PTR DATA_GO32:D_GDT_Item.STRUC_BaseMid, AL MOV BYTE PTR DATA_GO32:D_GDT_Item.STRUC_Access00, AL MOV BYTE PTR DATA_GO32:D_GDT_Item.STRUC_Access01, AL MOV BYTE PTR DATA_GO32:D_GDT_Item.STRUC_BaseHigh, AL PUSH AX CALL FAR PTR CODE_GO32:RegisterGDT ; 0001h - IDT descriptor ; 0002h - GDT descriptor ; 0003h - Real code descriptor PUSH CODE_GO32 PUSH DEF_Null CALL FAR PTR CODE_CALC:ToPhysical MOV WORD PTR DATA_GO32:D_GDT_Item.STRUC_LimitLow, 0FFFFh MOV WORD PTR DATA_GO32:D_GDT_Item.STRUC_BaseLow, AX MOV BYTE PTR DATA_GO32:D_GDT_Item.STRUC_BaseMid, DL MOV BYTE PTR DATA_GO32:D_GDT_Item.STRUC_Access00, 10011010b MOV BYTE PTR DATA_GO32:D_GDT_Item.STRUC_Access01, 00000000b MOV BYTE PTR DATA_GO32:D_GDT_Item.STRUC_BaseHigh, DH PUSH 0003h CALL FAR PTR CODE_GO32:RegisterGDT ; 0004h - Real data descriptor PUSH DATA_GO32 PUSH DEF_Null CALL FAR PTR CODE_CALC:ToPhysical MOV WORD PTR DATA_GO32:D_GDT_Item.STRUC_LimitLow, 0FFFFh MOV WORD PTR DATA_GO32:D_GDT_Item.STRUC_BaseLow, AX MOV BYTE PTR DATA_GO32:D_GDT_Item.STRUC_BaseMid, DL MOV BYTE PTR DATA_GO32:D_GDT_Item.STRUC_Access00, 10010010b MOV BYTE PTR DATA_GO32:D_GDT_Item.STRUC_Access01, 00000000b MOV BYTE PTR DATA_GO32:D_GDT_Item.STRUC_BaseHigh, DH PUSH 0004h CALL FAR PTR CODE_GO32:RegisterGDT ; 0005h - Full data descriptor MOV WORD PTR DATA_GO32:D_GDT_Item.STRUC_LimitLow, 0FFFFh MOV WORD PTR DATA_GO32:D_GDT_Item.STRUC_BaseLow, 00000h MOV BYTE PTR DATA_GO32:D_GDT_Item.STRUC_BaseMid, 000h MOV BYTE PTR DATA_GO32:D_GDT_Item.STRUC_Access00, 10010010b MOV BYTE PTR DATA_GO32:D_GDT_Item.STRUC_Access01, 11001111b MOV BYTE PTR DATA_GO32:D_GDT_Item.STRUC_BaseHigh, 000h PUSH 0005h CALL FAR PTR CODE_GO32:RegisterGDT ; 0006h - Video data(TEXT) descriptor ; 0007h - Video data(GRAPHICS) descriptor ; 0008h - Code descriptor PUSH CODE_KN32 PUSH OFFSET CODE_KN32:KernelMain32 CALL FAR PTR CODE_CALC:ToPhysical MOV WORD PTR DATA_GO32:D_GDT_Item.STRUC_LimitLow, 0FFFFh MOV WORD PTR DATA_GO32:D_GDT_Item.STRUC_BaseLow, AX MOV BYTE PTR DATA_GO32:D_GDT_Item.STRUC_BaseMid, DL MOV BYTE PTR DATA_GO32:D_GDT_Item.STRUC_Access00, 10011110b MOV BYTE PTR DATA_GO32:D_GDT_Item.STRUC_Access01, 11000000b MOV BYTE PTR DATA_GO32:D_GDT_Item.STRUC_BaseHigh, DH PUSH 0008h CALL FAR PTR CODE_GO32:RegisterGDT ; 0009h - Data descriptor ; 000Ah - Bss descriptor ; 000Bh - Stack descriptor ; 000Ch - Heap descriptor ; Load GDT MOV EAX, OFFSET DESC_GO32_GDT:D_GDT PUSH DESC_GO32_GDT PUSH AX CALL FAR PTR CODE_CALC:ToPhysical MOV WORD PTR DATA_GO32:D_GDTR.STRUC_LimitLow, (DEF_MAX_DefaultGDT * DEF_SIZE_Descriptor) - 1 MOV WORD PTR DATA_GO32:D_GDTR.STRUC_BaseLow, AX MOV BYTE PTR DATA_GO32:D_GDTR.STRUC_BaseMid, DL MOV BYTE PTR DATA_GO32:D_GDTR.STRUC_Access00, DH LGDT QWORD PTR DATA_GO32:D_GDTR POP BX POP AX POP DS RETF SetupGDT ENDP
Go32 PROC FAR ; void far pascal Go32(void)
PUSH DS PUSH ES PUSH FS PUSH GS PUSHAD PUSH DATA_GO32 PUSH OFFSET DATA_GO32:S_MSG_Go32_Enter CALL FAR PTR CODE_TEXT:Puts ; Clear descriptor PUSH DESC_GO32_IDT MOV EAX, OFFSET DESC_GO32_IDT:D_IDT ; 32bit offset PUSH AX PUSH DEF_Null PUSH (DEF_MAX_DefaultIDT * DEF_SIZE_Descriptor) SHR 01h CALL FAR PTR CODE_MEMORY:MemSetW PUSH DESC_GO32_GDT MOV EAX, OFFSET DESC_GO32_GDT:D_GDT ; 32bit offset PUSH AX PUSH DEF_Null PUSH (DEF_MAX_DefaultGDT * DEF_SIZE_Descriptor) SHR 01h CALL FAR PTR CODE_MEMORY:MemSetW ; Setup descriptor & environ CALL FAR PTR CODE_GO32:SetupIDT CALL FAR PTR CODE_GO32:SetupGDT ; Set PE MOV AX, DATA_GO32 MOV DS, AX MOV AX, SS MOV WORD PTR DATA_GO32:D_StackFrame[DEF_Far_Segment], AX MOV WORD PTR DATA_GO32:D_StackFrame[DEF_Far_Offset], SP MOV EAX, CR0 OR AL, 01h MOV CR0, EAX ; º¸È£¸ðµå ÁøÀÔÁ¡ MAC_ClearCache ; Code cache¸¦ Áö¿ì±â À§ÇØ ±×³É ¹Ù·Î ¾Õ¿¡ Á¡ÇÁ "jmp $+2" MAC_JumpFar <0003h * DEF_SIZE_Descriptor>, <OFFSET CODE_GO32:L_GO32_Enter> ; DWORD jump ÀÔ´Ï´Ù. L_GO32_Enter LABEL FAR MOV AX, 0004h * DEF_SIZE_Descriptor MOV DS, AX MOV ES, AX MOV FS, AX MOV GS, AX ; MOV AX, 000Bh * DEF_SIZE_Descriptor ; MOV SS, AX ; XOR ESP, ESP XOR EAX, EAX XOR EBX, EBX XOR ECX, ECX XOR EDX, EDX XOR ESI, ESI XOR EDI, EDI XOR EBP, EBP MAC_JumpFar <0008h * DEF_SIZE_Descriptor>, <OFFSET CODE_KN32:KernelMain32> ; ½ÇÁ¦ 32ºñÆ® µ¿ÀÛÀ» ÇÒ 32ºñÆ® Äڵ尡 ÀÖ´Â °÷ ; Clear PE L_Exit32 LABEL FAR MOV EAX, CR0 AND AL, 0FEh MOV CR0, EAX MAC_ClearCache MAC_JumpFar <CODE_GO32>, <OFFSET CODE_GO32:L_GO32_Return> ; Real mode·Î µ¹¾Æ¿É´Ï´Ù. ÇÏÁö¸¸ ¾ÆÁ÷Àº Real modeÀÇ ¿ÏÀüÇÑ ¹ÝȯÀº ¾Æ´Õ´Ï´Ù. L_GO32_Return LABEL FAR ; Restore environ MOV AX, DATA_GO32 ; ¿©±â¼ ½ÇÁ¦·Î Real mode¸¦ À§ÇÑ Segment¸¦ ¹èÄ¡ÇÏ¿© ÁÝ´Ï´Ù. MOV DS, AX XOR AX, AX MOV ES, AX MOV FS, AX MOV GS, AX LSS SP, DWORD PTR DATA_GO32:D_StackFrame PUSH DATA_GO32 PUSH OFFSET DATA_GO32:S_MSG_Go32_Return CALL FAR PTR CODE_TEXT:Puts POPAD POP GS POP FS POP ES POP DS RETF Go32 ENDP
CODE_GO32 ENDS
ASSUME CS:CODE_GO32, DS:DATA_GO32, ES:NOTHING, SS:STACK_DEFAULT DATA_GO32 SEGMENT S_MSG_Go32_Enter DB 0FEh, " Enter to 32bit processing." DB DEF_ASCII_CarrigeReturn, DEF_ASCII_LineFeed DB DEF_ASCII_EndOfString S_MSG_Go32_Return DB 0FEh, " Return to 16bit processing." DB DEF_ASCII_CarrigeReturn, DEF_ASCII_LineFeed DB DEF_ASCII_EndOfString D_StackFrame DW DEF_Null, STACK_DEFAULT D_IDTR STRUC_Descriptor <0000h, 0000h, 00h, 00h, 00h, 00h> D_GDTR STRUC_Descriptor <0000h, 0000h, 00h, 00h, 00h, 00h> D_IDT_Item STRUC_Descriptor <0000h, 0000h, 00h, 00h, 00h, 00h> D_GDT_Item STRUC_Descriptor <0000h, 0000h, 00h, 00h, 00h, 00h> DATA_GO32 ENDS ASSUME CS:CODE_GO32, DS:BSS_GO32, ES:NOTHING, SS:STACK_DEFAULT BSS_GO32 SEGMENT BSS_GO32 ENDS ASSUME CS:CODE_GO32, DS:DESC_GO32_IDT, ES:NOTHING, SS:STACK_DEFAULT DESC_GO32_IDT SEGMENT D_IDT STRUC_Descriptor DEF_MAX_DefaultIDT DUP (<>) DESC_GO32_IDT ENDS ASSUME CS:CODE_GO32, DS:DESC_GO32_GDT, ES:NOTHING, SS:STACK_DEFAULT DESC_GO32_GDT SEGMENT D_GDT STRUC_Descriptor DEF_MAX_DefaultGDT DUP (<>) DESC_GO32_GDT ENDS END ; End of source
1.9. ¹®¼¸¦ ¸¶Ä¡¸é¼ ¶
|
Troglodytism does not necessarily imply a low cultural level. |