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 ##############################################################################
24 .equ DEFAULT_HANDLER_SIZE, INT1 - INT0
26 .macro jmpCommonIdtEntry
27 # jmp commonIdtEntry - this must be hand coded to keep the assembler from
28 # using a 8 bit reletive jump when the entries are
29 # within 255 bytes of the common entry. This must
30 # be done to maintain the consistency of the size
32 .byte 0xe9 # jmp 16 bit relative
33 .long commonIdtEntry - . - 4 # A problem
42 movl $0x001ffff0, %esp
46 # Populate IDT with meaningful offsets for exception handlers...
50 movl %eax, %ebx # use bx to copy 15..0 to descriptors
51 shrl $16, %eax # use ax to copy 31..16 to descriptors
52 movl $0x78, %ecx # 78h IDT entries to initialize with unique entry points (exceptions)
56 LOOP_1: # loop through all IDT entries exception handlers and initialize to default handler
57 movw %bx, (%edi) # write bits 15..0 of offset
58 movw $0x20, 2(%edi) # SYS_CODE_SEL from GDT
59 movw $(0x0e00 | 0x8000), 4(%edi) # type = 386 interrupt gate, present
60 movw %ax, 6(%edi) # write bits 31..16 of offset
61 addl $8, %edi # move up to next descriptor
62 addw DEFAULT_HANDLER_SIZE, %bx # move to next entry point
63 loopl LOOP_1 # loop back through again until all descriptors are initialized
65 ## at this point edi contains the offset of the descriptor for INT 20
66 ## and bx contains the low 16 bits of the offset of the default handler
67 ## so initialize all the rest of the descriptors with these two values...
68 # mov ecx, 101 ; there are 100 descriptors left (INT 20 (14h) - INT 119 (77h)
69 #@@: ; loop through all IDT entries exception handlers and initialize to default handler
70 # mov word ptr [edi], bx ; write bits 15..0 of offset
71 # mov word ptr [edi+2], 20h ; SYS_CODE_SEL from GDT
72 # mov word ptr [edi+4], 0e00h OR 8000h ; type = 386 interrupt gate, present
73 # mov word ptr [edi+6], ax ; write bits 31..16 of offset
74 # add edi, 8 ; move up to next descriptor
75 # loop @b ; loop back through again until all descriptors are initialized
78 ## DUMP location of IDT and several of the descriptors
80 # mov eax, [offset Idtr + 2]
89 ## just for fun, let's do a software interrupt to see if we correctly land in the exception handler...
103 movl $0x22000, %esi # esi = 22000
104 movl 0x14(%esi), %eax # eax = [22014]
105 addl %eax, %esi # esi = 22000 + [22014] = Base of EFILDR.C
106 movl 0x3c(%esi), %ebp # ebp = [22000 + [22014] + 3c] = NT Image Header for EFILDR.C
108 movl 0x34(%ebp), %edi # edi = [[22000 + [22014] + 3c] + 30] = ImageBase
109 movl 0x28(%ebp), %eax # eax = [[22000 + [22014] + 3c] + 24] = EntryPoint
110 addl %edi, %eax # eax = ImageBase + EntryPoint
111 movl %eax, EfiLdrOffset # Modify far jump instruction for correct entry point
113 movw 6(%ebp), %bx # bx = Number of sections
115 movw 0x14(%ebp), %ax # ax = Optional Header Size
117 addl $0x18, %ebp # ebp = Start of 1st Section
120 pushl %esi # Save Base of EFILDR.C
121 pushl %edi # Save ImageBase
122 addl 0x14(%ebp), %esi # esi = Base of EFILDR.C + PointerToRawData
123 addl 0x0c(%ebp), %edi # edi = ImageBase + VirtualAddress
124 movl 0x10(%ebp), %ecx # ecs = SizeOfRawData
131 popl %edi # Restore ImageBase
132 popl %esi # Restore Base of EFILDR.C
134 addw $0x28, %bp # ebp = ebp + 028h = Pointer to next section record
139 movzwl Idtr, %eax # get size of IDT
141 addl Idtr + 2, %eax # add to base of IDT to get location of memory map...
142 pushl %eax # push memory map location on stack for call to EFILDR...
144 pushl %eax # push return address (useless, just for stack balance)
147 .long 0x00401000 # Offset of EFILDR
152 # db "**** DEFAULT IDT ENTRY ***",0
156 pushl $0x0 # push error code place holder on the stack
159 # db 0e9h ; jmp 16 bit reletive
160 # dd commonIdtEntry - $ - 4 ; offset to jump to
163 pushl $0x0 # push error code place holder on the stack
168 pushl $0x0 # push error code place holder on the stack
173 pushl $0x0 # push error code place holder on the stack
178 pushl $0x0 # push error code place holder on the stack
183 pushl $0x0 # push error code place holder on the stack
188 pushl $0x0 # push error code place holder on the stack
193 pushl $0x0 # push error code place holder on the stack
198 # Double fault causes an error code to be pushed so no phony push necessary
205 pushl $0x0 # push error code place holder on the stack
210 # Invalid TSS causes an error code to be pushed so no phony push necessary
217 # Segment Not Present causes an error code to be pushed so no phony push necessary
224 # Stack fault causes an error code to be pushed so no phony push necessary
231 # GP fault causes an error code to be pushed so no phony push necessary
238 # Page fault causes an error code to be pushed so no phony push necessary
245 pushl $0x0 # push error code place holder on the stack
250 pushl $0x0 # push error code place holder on the stack
255 # Alignment check causes an error code to be pushed so no phony push necessary
262 pushl $0x0 # push error code place holder on the stack
267 pushl $0x0 # push error code place holder on the stack
273 pushl $0x0 # push error code place holder on the stack
274 # push $0xxx # push vector number
276 .long ( . - INTUnknown - 3 ) / 9 + 20 # vector number
284 ## At this point the stack looks like this:
290 ## Int num or 0ffh for unknown int num
298 ## edi <------- ESP, EBP
304 movl 32(%ebp), %eax ## move Int number into EAX
306 ja PrintDefaultString
307 PrintExceptionString:
308 shll $2, %eax ## multiply by 4 to get offset from StringTable to actual string address
309 addl StringTable, %eax
313 movl IntUnknownString, %esi
326 movl 44(%ebp), %eax # CS
331 movl 40(%ebp), %eax # EIP
338 movl StringEax, %esi # eax
343 movl StringEbx, %esi # ebx
348 movl StringEcx, %esi # ecx
353 movl StringEdx, %esi # edx
358 movl StringEcode, %esi # error code
365 movl StringEsp, %esi # esp
370 movl StringEbp, %esi # ebp
375 movl StringEsi, %esi # esi
380 movl StringEdi, %esi # edi
385 movl StringEflags, %esi # eflags
419 movl 40(%ebp), %eax # EIP
421 movl %eax, %esi # esi = eip - 32 DWORD linear (total 64 DWORD)
446 # wbinvd ; this intruction does not support in early than 486 arch
454 addl $8, %esp # error code and INT number
473 ## EAX contains dword to print
474 ## EDI contains memory location (screen location) to print it to
528 String1: .asciz "*** INT "
530 Int0String: .asciz "00h Divide by 0 -"
531 Int1String: .asciz "01h Debug exception -"
532 Int2String: .asciz "02h NMI -"
533 Int3String: .asciz "03h Breakpoint -"
534 Int4String: .asciz "04h Overflow -"
535 Int5String: .asciz "05h Bound -"
536 Int6String: .asciz "06h Invalid opcode -"
537 Int7String: .asciz "07h Device not available -"
538 Int8String: .asciz "08h Double fault -"
539 Int9String: .asciz "09h Coprocessor seg overrun (reserved) -"
540 Int10String: .asciz "0Ah Invalid TSS -"
541 Int11String: .asciz "0Bh Segment not present -"
542 Int12String: .asciz "0Ch Stack fault -"
543 Int13String: .asciz "0Dh General protection fault -"
544 Int14String: .asciz "0Eh Page fault -"
545 Int15String: .asciz "0Fh (Intel reserved) -"
546 Int16String: .asciz "10h Floating point error -"
547 Int17String: .asciz "11h Alignment check -"
548 Int18String: .asciz "12h Machine check -"
549 Int19String: .asciz "13h SIMD Floating-Point Exception -"
550 IntUnknownString: .asciz "??h Unknown interrupt -"
552 StringTable: .long Int0String, Int1String, Int2String, Int3String, \
553 Int4String, Int5String, Int6String, Int7String, \
554 Int8String, Int9String, Int10String, Int11String, \
555 Int12String, Int13String, Int14String, Int15String,\
556 Int16String, Int17String, Int18String, Int19String
558 String2: .asciz " HALT!! *** ("
560 StringEax: .asciz "EAX="
561 StringEbx: .asciz "EBX="
562 StringEcx: .asciz "ECX="
563 StringEdx: .asciz "EDX="
564 StringEcode: .asciz "ECODE="
565 StringEsp: .asciz "ESP="
566 StringEbp: .asciz "EBP="
567 StringEsi: .asciz "ESI="
568 StringEdi: .asciz "EDI="
569 StringEflags: .asciz "EFLAGS="