145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org;; ######################################################################## 
245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org;; Macros
345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org;; ########################################################################
445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org; descriptor type, base, limit, p_dpl_s, g_db_a
645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org%macro descriptor 5 
745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw (%3 & 0xFFFF)       			; Limit 0-15
845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw (%2 & 0xFFFF) 			; Base  0-15
945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	db ((%2 & 0xFF0000) >> 16)		; Base  16-23
1045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	db ((%4 & 0xF) << 4) | (%1 & 0xF )	; p_dpl_s_type  
1145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	db (%5 << 4) | ((%3 & 0xF0000) >> 16) 	; g_db_a limit 19:16
1245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	db ((%2 & 0xFF000000) >> 24)    	; Base  24-31	
1345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org%endmacro
1445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
1545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org; cdesc32 base, limit, dpl
1645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org%macro cdesc32 3
1745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org       descriptor 0xB, %1, %2, 0x9 | (%3 & 0x3) << 1, 0xD
1845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org%endmacro
1945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	
2045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org; ddesc32 base, limit, dpl
2145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org%macro ddesc32 3
2245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org       descriptor 0x3, %1, %2, 0x9 | (%3 & 0x3) << 1, 0xD
2345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org%endmacro
2445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
2545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org; gates type, offset, selector, p_dpl_s
2645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org%macro gates 4 
2745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw %2 		      			; Offset 0-15
2845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw (%3 & 0xFFFF) 			; Selector 0-15
2945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	db 0					; Reserved
3045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	db ((%4 & 0xF) << 4) | (%1 & 0xF) 	; p_dpl_s_type  
3145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw 0					; Offset 16-31
3245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org%endmacro
3345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	
3445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org%macro idesc32 3
3545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org       gates 0xE, %1, %2, 0x8 | ((%3 & 0x3) << 1)
3645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org%endmacro
3745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	
3845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	
3945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org;; ######################################################################## 
4045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org;; Code Section
4145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org;; ########################################################################
4245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgSECTION CODE ABSOLUTE=0x00400000 FLAT USE32
4445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgtest_code:	
4645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	;; Your Code Goes Here
4845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	hlt			 
4945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		
5045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org;; ######################################################################## 
5145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org;; Setup Section
5245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org;; ########################################################################
5345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgSECTION SETUP ALIGN=16 FLAT USE16
5545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgsetup:	
5745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	mov edx, cr0
5945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        or  dl,  0x21		; Protect Mode On, Int 16 for FPU
6045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	and edx, 0x9FFFFFFF     ; Turn Caches on
6145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	mov cr0, edx
6245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
6345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	xor edx, edx		; Enable Var MTRRs
6445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	mov eax, 0x0806		; WriteBack
6545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	mov ecx, 0x2FF		
6645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	wrmsr
6745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
6845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	lgdt [pgdt]		; Set GDT
6945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	lidt [pidt]		; Set IDT
7045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	
7145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	jmp 0x8:protect_mode
7245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	
7345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgprotect_mode:		
7445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
7545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        BITS 32
7645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
7745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	mov esp, 0x01000000	; Get some stack space
7845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	mov ax,  0x0010		; Set data selectors
7945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	mov ss, ax
8045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	mov ds, ax
8145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	mov es, ax
8245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	mov fs, ax
8345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	mov gs, ax
8445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
8545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	jmp test_code		; Jmp to test code
8645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		
8745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org;; ######################################################################## 
8845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org;; Protect Mode IDT 
8945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org;; ########################################################################
9045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	 
9145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgSECTION IDTP ALIGN=16 FLAT USE32
9245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
9345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	;; cdesc32 base, limit, dpl
9445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orggdt0:	dq 0.0				; 0x0000 - Null descriptor
9545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org     	cdesc32 0x00000000, 0xFFFFF, 0	; 0x0008 - Code Selector 
9645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org     	ddesc32 0x00000000, 0xFFFFF, 0	; 0x0010 - Data Selector
9745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org     	cdesc32 0x00000000, 0xFFFFF, 3  ; 0x0018 - Code Select Ring 3 
9845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org     	ddesc32 0x00000000, 0xFFFFF, 3	; 0x0020 - Data Select Ring 3
9945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	
10045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	;; idesc32 offset, selector, dpl
10145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgidt0:	idesc32 isrP, 0x0008, 0		; 0x00,  0   #DE, Divide Error	
10245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org     	idesc32 isrP, 0x0008, 0		; 0x01,  1   #DB, Debug Fault
10345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org     	idesc32 isrP, 0x0008, 0		; 0x02,  2,  ---, NMI
10445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org     	idesc32 isrP, 0x0008, 0		; 0x03,  3,  #BP, Breakpoint
10545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org     	idesc32 isrP, 0x0008, 0		; 0x04,  4,  #OF, INTO detected Overflow
10645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org     	idesc32 isrP, 0x0008, 0	        ; 0x05,  5,  #BR, Bound Range Exceeded
10745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org     	idesc32 isrP, 0x0008, 0		; 0x06,  6,  #UD, Invalid Opcode
10845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org     	idesc32 isrP, 0x0008, 0		; 0x07,  7,  #NM, Device Not Available
10945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org     	idesc32 isrP, 0x0008, 0		; 0x08,  8,  #DF, Double Fault
11045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org     	idesc32 isrP, 0x0008, 0		; 0x09   9,  ---, Coprocessor Segment Overrun
11145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      	idesc32 isrP, 0x0008, 0		; 0x0A,  10, #TS, Invalid TSS 
11245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      	idesc32 isrP, 0x0008, 0		; 0x0B,  11, #NP, Segment Not Present
11345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      	idesc32 isrP, 0x0008, 0		; 0x0C,  12, #SS, Stack Fault
11445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      	idesc32 isrP, 0x0008, 0	        ; 0x0D,  13, #GP, General Protection Fault
11545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      	idesc32 isrP, 0x0008, 0		; 0x0E,  14, #PF, Page Fault
11645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      	idesc32 isrP, 0x0008, 0		; 0x0F,  15, ---, Reserved
11745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      	idesc32 isrP, 0x0008, 0		; 0x10,  16, #MF, Floating Point Fault
11845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      	idesc32 isrP, 0x0008, 0		; 0x11,  17, #AC, Alignment Check
11945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      	idesc32 isrP, 0x0008, 0		; 0x12   18, #MC, Machine Check
12045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      	idesc32 isrP, 0x0008, 0		; 0x13,  19, #XF, SSE Fault
12145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
12245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgpgdt:   dw 6 * 8			; Limit 
12345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dd gdt0			        ; base
12445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	
12545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgpidt:   dw 20 * 8			; Limit 
12645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dd idt0			        ; base
12745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
12845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgisrP:	mov eax, 0xDEADBEEF ; Default Real Mode Interrupt Handler 
12945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	out 0x80, eax
13045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	hlt
13145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org			
13245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org;; ######################################################################## 
13345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org;; Real Mode IDT 
13445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org;; ########################################################################
13545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	
13645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgSECTION IDTR ABSOLUTE=0x00000000 FLAT USE16 
13745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
13845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	;; FORMAT IP:CS 
13945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw isrR, 0			; 0x00,  0   #DE, Divide Error	
14045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw isrR, 0			; 0x01,  1   #DB, Debug Fault
14145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw isrR, 0			; 0x02,  2,  ---, NMI
14245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw isrR, 0			; 0x03,  3,  #BP, Breakpoint
14345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw isrR, 0			; 0x04,  4,  #OF, INTO detected Overflow
14445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw isrR, 0			; 0x05,  5,  #BR, Bound Range Exceeded
14545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw isrR, 0			; 0x06,  6,  #UD, Invalid Opcode
14645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw isrR, 0			; 0x07,  7,  #NM, Device Not Available
14745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw isrR, 0			; 0x08,  8,  #DF, Double Fault
14845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw isrR, 0			; 0x09   9,  ---, Coprocessor Segment Overrun
14945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw isrR, 0			; 0x0A,  10, #TS, Invalid TSS 
15045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw isrR, 0			; 0x0B,  11, #NP, Segment Not Present
15145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw isrR, 0			; 0x0C,  12, #SS, Stack Fault
15245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw isrR, 0			; 0x0D,  13, #GP, General Protection Fault
15345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw isrR, 0			; 0x0E,  14, #PF, Page Fault
15445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw isrR, 0			; 0x0F,  15, ---, Reserved
15545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw isrR, 0			; 0x10,  16, #MF, Floating Point Fault
15645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw isrR, 0			; 0x11,  17, #AC, Alignment Check
15745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw isrR, 0			; 0x12   18, #MC, Machine Check
15845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	dw isrR, 0			; 0x13,  19, #XF, SSE Fault
15945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
16045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgisrR:	mov eax, 0xDEADBEEF ; Default Real Mode Interrupt Handler 
16145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	out 0x80, eax
16245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	hlt
16345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	
16445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org;; ######################################################################## 
16545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org;; SMM Handler
16645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org;; ########################################################################
16745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
16845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgSECTION SMM ABSOLUTE=0x00038000 USE16
16945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	
17045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	rsm
17145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	
17245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org;; ######################################################################## 
17345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org;; Reset Vector
17445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org;; ########################################################################
17545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
17645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgSECTION RESET ABSOLUTE=0xFFFFFFF0 USE16
17745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	
17845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	jmp far setup
179