1 #------------------------------------------------------------------------------
3 #* Copyright 2006, Intel Corporation
4 #* All rights reserved. This program and the accompanying materials
5 #* are licensed and made available under the terms and conditions of the BSD License
6 #* which accompanies this distribution. The full text of the license may be found at
7 #* http://opensource.org/licenses/bsd-license.php
9 #* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 #* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #------------------------------------------------------------------------------
18 ##############################################################################
19 # Now in 32-bit protected mode.
20 ##############################################################################
27 .equ DEFAULT_HANDLER_SIZE, INT1 - INT0
29 .macro jmpCommonIdtEntry
30 # jmp commonIdtEntry - this must be hand coded to keep the assembler from
31 # using a 8 bit reletive jump when the entries are
32 # within 255 bytes of the common entry. This must
33 # be done to maintain the consistency of the size
35 .byte 0xe9 # jmp 16 bit relative
36 .long commonIdtEntry - . - 4 # offset to jump to
45 movl $0x001ffff0, %esp
49 # Populate IDT with meaningful offsets for exception handlers...
53 movl %eax, %ebx # use bx to copy 15..0 to descriptors
54 shrl $16, %eax # use ax to copy 31..16 to descriptors
55 movl $0x78, %ecx # 78h IDT entries to initialize with unique entry points (exceptions)
59 LOOP_1: # loop through all IDT entries exception handlers and initialize to default handler
60 movw %bx, (%edi) # write bits 15..0 of offset
61 movw $0x20, 2(%edi) # SYS_CODE_SEL from GDT
62 movw $(0x0e00 | 0x8000), 4(%edi) # type = 386 interrupt gate, present
63 movw %ax, 6(%edi) # write bits 31..16 of offset
64 addl $8, %edi # move up to next descriptor
65 addw DEFAULT_HANDLER_SIZE, %bx # move to next entry point
66 loopl LOOP_1 # loop back through again until all descriptors are initialized
68 ## at this point edi contains the offset of the descriptor for INT 20
69 ## and bx contains the low 16 bits of the offset of the default handler
70 ## so initialize all the rest of the descriptors with these two values...
71 # mov ecx, 101 ; there are 100 descriptors left (INT 20 (14h) - INT 119 (77h)
72 #@@: ; loop through all IDT entries exception handlers and initialize to default handler
73 # mov word ptr [edi], bx ; write bits 15..0 of offset
74 # mov word ptr [edi+2], 20h ; SYS_CODE_SEL from GDT
75 # mov word ptr [edi+4], 0e00h OR 8000h ; type = 386 interrupt gate, present
76 # mov word ptr [edi+6], ax ; write bits 31..16 of offset
77 # add edi, 8 ; move up to next descriptor
78 # loop @b ; loop back through again until all descriptors are initialized
81 ## DUMP location of IDT and several of the descriptors
83 # mov eax, [offset Idtr + 2]
92 ## just for fun, let's do a software interrupt to see if we correctly land in the exception handler...
106 movl $0x22000, %esi # esi = 22000
107 movl 0x14(%esi), %eax # eax = [22014]
108 addl %eax, %esi # esi = 22000 + [22014] = Base of EFILDR.C
109 movl 0x3c(%esi), %ebp # ebp = [22000 + [22014] + 3c] = NT Image Header for EFILDR.C
111 movl 0x34(%ebp), %edi # edi = [[22000 + [22014] + 3c] + 30] = ImageBase
112 movl 0x28(%ebp), %eax # eax = [[22000 + [22014] + 3c] + 24] = EntryPoint
113 addl %edi, %eax # eax = ImageBase + EntryPoint
114 movl %eax, EfiLdrOffset # Modify far jump instruction for correct entry point
116 movw 6(%ebp), %bx # bx = Number of sections
118 movw 0x14(%ebp), %ax # ax = Optional Header Size
120 addl $0x18, %ebp # ebp = Start of 1st Section
123 pushl %esi # Save Base of EFILDR.C
124 pushl %edi # Save ImageBase
125 addl 0x14(%ebp), %esi # esi = Base of EFILDR.C + PointerToRawData
126 addl 0x0c(%ebp), %edi # edi = ImageBase + VirtualAddress
127 movl 0x10(%ebp), %ecx # ecs = SizeOfRawData
134 popl %edi # Restore ImageBase
135 popl %esi # Restore Base of EFILDR.C
137 addw $0x28, %bp # ebp = ebp + 028h = Pointer to next section record
142 movzwl (Idtr), %eax # get size of IDT
144 addl (Idtr + 2), %eax # add to base of IDT to get location of memory map...
145 pushl %eax # push memory map location on stack for call to EFILDR...
147 pushl %eax # push return address (useless, just for stack balance)
150 .long 0x00401000 # Offset of EFILDR
155 # db "**** DEFAULT IDT ENTRY ***",0
159 pushl $0x0 # push error code place holder on the stack
162 # db 0e9h ; jmp 16 bit reletive
163 # dd commonIdtEntry - $ - 4 ; offset to jump to
166 pushl $0x0 # push error code place holder on the stack
171 pushl $0x0 # push error code place holder on the stack
176 pushl $0x0 # push error code place holder on the stack
181 pushl $0x0 # push error code place holder on the stack
186 pushl $0x0 # push error code place holder on the stack
191 pushl $0x0 # push error code place holder on the stack
196 pushl $0x0 # push error code place holder on the stack
201 # Double fault causes an error code to be pushed so no phony push necessary
208 pushl $0x0 # push error code place holder on the stack
213 # Invalid TSS causes an error code to be pushed so no phony push necessary
220 # Segment Not Present causes an error code to be pushed so no phony push necessary
227 # Stack fault causes an error code to be pushed so no phony push necessary
234 # GP fault causes an error code to be pushed so no phony push necessary
241 # Page fault causes an error code to be pushed so no phony push necessary
248 pushl $0x0 # push error code place holder on the stack
253 pushl $0x0 # push error code place holder on the stack
258 # Alignment check causes an error code to be pushed so no phony push necessary
265 pushl $0x0 # push error code place holder on the stack
270 pushl $0x0 # push error code place holder on the stack
276 pushl $0x0 # push error code place holder on the stack
277 # push $0xxx # push vector number
279 .long ( . - INTUnknown - 3 ) / 9 + 20 # vector number
287 ## At this point the stack looks like this:
293 ## Int num or 0ffh for unknown int num
301 ## edi <------- ESP, EBP
307 movl 32(%ebp), %eax ## move Int number into EAX
309 ja PrintDefaultString
310 PrintExceptionString:
311 shll $2, %eax ## multiply by 4 to get offset from StringTable to actual string address
312 addl StringTable, %eax
316 movl IntUnknownString, %esi
329 movl 44(%ebp), %eax # CS
334 movl 40(%ebp), %eax # EIP
341 movl StringEax, %esi # eax
346 movl StringEbx, %esi # ebx
351 movl StringEcx, %esi # ecx
356 movl StringEdx, %esi # edx
361 movl StringEcode, %esi # error code
368 movl StringEsp, %esi # esp
373 movl StringEbp, %esi # ebp
378 movl StringEsi, %esi # esi
383 movl StringEdi, %esi # edi
388 movl StringEflags, %esi # eflags
422 movl 40(%ebp), %eax # EIP
424 movl %eax, %esi # esi = eip - 32 DWORD linear (total 64 DWORD)
449 # wbinvd ; this intruction does not support in early than 486 arch
457 addl $8, %esp # error code and INT number
476 ## EAX contains dword to print
477 ## EDI contains memory location (screen location) to print it to
531 String1: .asciz "*** INT "
533 Int0String: .asciz "00h Divide by 0 -"
534 Int1String: .asciz "01h Debug exception -"
535 Int2String: .asciz "02h NMI -"
536 Int3String: .asciz "03h Breakpoint -"
537 Int4String: .asciz "04h Overflow -"
538 Int5String: .asciz "05h Bound -"
539 Int6String: .asciz "06h Invalid opcode -"
540 Int7String: .asciz "07h Device not available -"
541 Int8String: .asciz "08h Double fault -"
542 Int9String: .asciz "09h Coprocessor seg overrun (reserved) -"
543 Int10String: .asciz "0Ah Invalid TSS -"
544 Int11String: .asciz "0Bh Segment not present -"
545 Int12String: .asciz "0Ch Stack fault -"
546 Int13String: .asciz "0Dh General protection fault -"
547 Int14String: .asciz "0Eh Page fault -"
548 Int15String: .asciz "0Fh (Intel reserved) -"
549 Int16String: .asciz "10h Floating point error -"
550 Int17String: .asciz "11h Alignment check -"
551 Int18String: .asciz "12h Machine check -"
552 Int19String: .asciz "13h SIMD Floating-Point Exception -"
553 IntUnknownString: .asciz "??h Unknown interrupt -"
555 StringTable: .long Int0String, Int1String, Int2String, Int3String, \
556 Int4String, Int5String, Int6String, Int7String, \
557 Int8String, Int9String, Int10String, Int11String, \
558 Int12String, Int13String, Int14String, Int15String,\
559 Int16String, Int17String, Int18String, Int19String
561 String2: .asciz " HALT!! *** ("
563 StringEax: .asciz "EAX="
564 StringEbx: .asciz "EBX="
565 StringEcx: .asciz "ECX="
566 StringEdx: .asciz "EDX="
567 StringEcode: .asciz "ECODE="
568 StringEsp: .asciz "ESP="
569 StringEbp: .asciz "EBP="
570 StringEsi: .asciz "ESI="
571 StringEdi: .asciz "EDI="
572 StringEflags: .asciz "EFLAGS="