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 #PUBLIC SystemTimerHandler
19 #PUBLIC SystemExceptionHandler
20 #EXTERNDEF mExceptionCodeSize:DWORD
22 #EXTERN TimerHandler: NEAR
23 #EXTERN ExceptionHandler: NEAR
24 #EXTERN mTimerVector: DWORD
27 .globl ASM_PFX(mExceptionCodeSize)
28 ASM_PFX(mExceptionCodeSize): .long 9
31 .globl ASM_PFX(InitDescriptor)
33 ASM_PFX(InitDescriptor):
34 movl $GDT_BASE,%eax # EAX=PHYSICAL address of gdt
35 movl %eax, gdtr + 2 # Put address of gdt into the gdtr
37 movl $IDT_BASE,%eax # EAX=PHYSICAL address of idt
38 movl %eax, idtr + 2 # Put address of idt into the idtr
43 # InstallInterruptHandler (
45 # VOID (*Handler)(VOID)
47 .globl ASM_PFX(InstallInterruptHandler)
48 ASM_PFX(InstallInterruptHandler):
49 # Vector:DWORD @ 4(%esp)
50 # Handler:DWORD @ 8(%esp)
54 cli # turn off interrupts
55 subl $6,%esp # open some space on the stack
57 sidt (%edi) # get fword address of IDT
58 movl 2(%edi), %edi # move offset of IDT into EDI
59 addl $6,%esp # correct stack
60 movl 12(%esp),%eax # Get vector number
61 shl $3,%eax # multiply by 8 to get offset
62 addl %eax,%edi # add to IDT base to get entry
63 movl 16(%esp),%eax # load new address into IDT entry
64 movw %ax,(%edi) # write bits 15..0 of offset
65 shrl $16,%eax # use ax to copy 31..16 to descriptors
66 movw %ax,6(%edi) # write bits 31..16 of offset
67 popf # restore flags (possible enabling interrupts)
71 .macro JmpCommonIdtEntry
72 # jmp commonIdtEntry - this must be hand coded to keep the assembler from
73 # using a 8 bit reletive jump when the entries are
74 # within 255 bytes of the common entry. This must
75 # be done to maintain the consistency of the size
77 .byte 0xe9 # jmp 16 bit reletive
78 .long commonIdtEntry - . - 4 # offset to jump to
82 .globl ASM_PFX(SystemExceptionHandler)
83 ASM_PFX(SystemExceptionHandler):
85 pushl $0x0 # push error code place holder on the stack
88 # db 0e9h # jmp 16 bit reletive
89 # dd commonIdtEntry - $ - 4 # offset to jump to
92 pushl $0x0 # push error code place holder on the stack
97 pushl $0x0 # push error code place holder on the stack
102 pushl $0x0 # push error code place holder on the stack
107 pushl $0x0 # push error code place holder on the stack
112 pushl $0x0 # push error code place holder on the stack
117 pushl $0x0 # push error code place holder on the stack
122 pushl $0x0 # push error code place holder on the stack
127 # Double fault causes an error code to be pushed so no phony push necessary
134 pushl $0x0 # push error code place holder on the stack
139 # Invalid TSS causes an error code to be pushed so no phony push necessary
146 # Segment Not Present causes an error code to be pushed so no phony push necessary
153 # Stack fault causes an error code to be pushed so no phony push necessary
160 # GP fault causes an error code to be pushed so no phony push necessary
167 # Page fault causes an error code to be pushed so no phony push necessary
174 pushl $0x0 # push error code place holder on the stack
179 pushl $0x0 # push error code place holder on the stack
184 # Alignment check causes an error code to be pushed so no phony push necessary
191 pushl $0x0 # push error code place holder on the stack
196 pushl $0x0 # push error code place holder on the stack
202 pushl $0x0 # push error code place holder on the stack
203 # push xxh # push vector number
205 .byte ( . - INTUnknown - 3 ) / 9 + 20 # vector number
209 .globl ASM_PFX(SystemTimerHandler)
210 ASM_PFX(SystemTimerHandler):
216 # +---------------------+
218 # +---------------------+
220 # +---------------------+
222 # +---------------------+
224 # +---------------------+
226 # +---------------------+
228 # +---------------------+ <-- EBP
235 # Align stack to make sure that EFI_FX_SAVE_STATE_IA32 of EFI_SYSTEM_CONTEXT_IA32
238 andl $0xfffffff0,%esp
241 ## UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax#
252 ## UINT32 Gs, Fs, Es, Ds, Cs, Ss#
269 ## UINT32 Gdtr[2], Idtr[2]#
285 ## UINT32 Cr0, Cr1, Cr2, Cr3, Cr4#
299 ## UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7#
302 ## clear Dr7 while executing debugger itself
308 ## insure all status bits in dr6 are clear...
321 ## FX_SAVE_STATE_IA32 FxSaveState;
326 ## UINT32 ExceptionData;
329 ## Prepare parameter and call
339 call ExceptionHandler
344 ## UINT32 ExceptionData;
347 ## FX_SAVE_STATE_IA32 FxSaveState;
352 #; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
361 ## skip restore of dr6. We cleared dr6 during the context save.
366 ## UINT32 Cr0, Cr1, Cr2, Cr3, Cr4;
369 addl $4,%esp # not for Cr1
381 ## UINT32 Gdtr[2], Idtr[2];
382 ## Best not let anyone mess with these particular registers...
388 ## UINT32 Gs, Fs, Es, Ds, Cs, Ss;
389 ## NOTE - modified segment registers could hang the debugger... We
390 ## could attempt to insulate ourselves against this possibility,
391 ## but that poses risks as well.
400 ## UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;
403 addl $4,%esp # not for ebp
404 addl $4,%esp # not for esp
416 #;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
418 #;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
423 gdtr: .short GDT_END - GDT_BASE - 1 # GDT limit
424 .long 0 # (GDT base gets set above)
425 #;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
426 # global descriptor table (GDT)
427 #;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
434 NULL_SEL = .-GDT_BASE
435 .short 0 # limit 15:0
439 .byte 0 # limit 19:16, flags
442 # linear data segment descriptor
443 LINEAR_SEL = .-GDT_BASE
444 .short 0x0FFFF # limit 0xFFFFF
447 .byte 0x092 # present, ring 0, data, expand-up, writable
448 .byte 0x0CF # page-granular, 32-bit
451 # linear code segment descriptor
452 LINEAR_CODE_SEL = .-GDT_BASE
453 .short 0x0FFFF # limit 0xFFFFF
456 .byte 0x09A # present, ring 0, data, expand-up, writable
457 .byte 0x0CF # page-granular, 32-bit
460 # system data segment descriptor
461 SYS_DATA_SEL = .-GDT_BASE
462 .short 0x0FFFF # limit 0xFFFFF
465 .byte 0x092 # present, ring 0, data, expand-up, writable
466 .byte 0x0CF # page-granular, 32-bit
469 # system code segment descriptor
470 SYS_CODE_SEL = .-GDT_BASE
471 .short 0x0FFFF # limit 0xFFFFF
474 .byte 0x09A # present, ring 0, data, expand-up, writable
475 .byte 0x0CF # page-granular, 32-bit
478 # spare segment descriptor
479 SPARE3_SEL = .-GDT_BASE
480 .short 0 # limit 0xFFFFF
483 .byte 0 # present, ring 0, data, expand-up, writable
484 .byte 0 # page-granular, 32-bit
487 # spare segment descriptor
488 SPARE4_SEL = .-GDT_BASE
489 .short 0 # limit 0xFFFFF
492 .byte 0 # present, ring 0, data, expand-up, writable
493 .byte 0 # page-granular, 32-bit
496 # spare segment descriptor
497 SPARE5_SEL = .-GDT_BASE
498 .short 0 # limit 0xFFFFF
501 .byte 0 # present, ring 0, data, expand-up, writable
502 .byte 0 # page-granular, 32-bit
511 idtr: .short IDT_END - IDT_BASE - 1 # IDT limit
512 .long 0 # (IDT base gets set above)
513 #;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
514 # interrupt descriptor table (IDT)
516 # Note: The hardware IRQ's specified in this table are the normal PC/AT IRQ
517 # mappings. This implementation only uses the system timer and all other
518 # IRQs will remain masked. The descriptors for vectors 33+ are provided
520 #;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
522 #idt_tag .byte "IDT",0
527 # divide by zero (INT 0)
528 DIV_ZERO_SEL = .-IDT_BASE
529 .short 0 # offset 15:0
530 .short SYS_CODE_SEL # selector 15:0
531 .byte 0 # 0 for interrupt gate
532 .byte 0x0e | 0x80 # type = 386 interrupt gate, present
533 .short 0 # offset 31:16
535 # debug exception (INT 1)
536 DEBUG_EXCEPT_SEL = .-IDT_BASE
537 .short 0 # offset 15:0
538 .short SYS_CODE_SEL # selector 15:0
539 .byte 0 # 0 for interrupt gate
540 .byte 0x0e | 0x80 # type = 386 interrupt gate, present
541 .short 0 # offset 31:16
545 .short 0 # offset 15:0
546 .short SYS_CODE_SEL # selector 15:0
547 .byte 0 # 0 for interrupt gate
548 .byte 0x0e | 0x80 # type = 386 interrupt gate, present
549 .short 0 # offset 31:16
551 # soft breakpoint (INT 3)
552 BREAKPOINT_SEL = .-IDT_BASE
553 .short 0 # offset 15:0
554 .short SYS_CODE_SEL # selector 15:0
555 .byte 0 # 0 for interrupt gate
556 .byte 0x0e | 0x80 # type = 386 interrupt gate, present
557 .short 0 # offset 31:16
560 OVERFLOW_SEL = .-IDT_BASE
561 .short 0 # offset 15:0
562 .short SYS_CODE_SEL # selector 15:0
563 .byte 0 # 0 for interrupt gate
564 .byte 0x0e | 0x80 # type = 386 interrupt gate, present
565 .short 0 # offset 31:16
567 # bounds check (INT 5)
568 BOUNDS_CHECK_SEL = .-IDT_BASE
569 .short 0 # offset 15:0
570 .short SYS_CODE_SEL # selector 15:0
571 .byte 0 # 0 for interrupt gate
572 .byte 0x0e | 0x80 # type = 386 interrupt gate, present
573 .short 0 # offset 31:16
575 # invalid opcode (INT 6)
576 INVALID_OPCODE_SEL = .-IDT_BASE
577 .short 0 # offset 15:0
578 .short SYS_CODE_SEL # selector 15:0
579 .byte 0 # 0 for interrupt gate
580 .byte 0x0e | 0x80 # type = 386 interrupt gate, present
581 .short 0 # offset 31:16
583 # device not available (INT 7)
584 DEV_NOT_AVAIL_SEL = .-IDT_BASE
585 .short 0 # offset 15:0
586 .short SYS_CODE_SEL # selector 15:0
587 .byte 0 # 0 for interrupt gate
588 .byte 0x0e | 0x80 # type = 386 interrupt gate, present
589 .short 0 # offset 31:16
591 # double fault (INT 8)
592 DOUBLE_FAULT_SEL = .-IDT_BASE
593 .short 0 # offset 15:0
594 .short SYS_CODE_SEL # selector 15:0
595 .byte 0 # 0 for interrupt gate
596 .byte 0x0e | 0x80 # type = 386 interrupt gate, present
597 .short 0 # offset 31:16
599 # Coprocessor segment overrun - reserved (INT 9)
600 RSVD_INTR_SEL1 = .-IDT_BASE
601 .short 0 # offset 15:0
602 .short SYS_CODE_SEL # selector 15:0
603 .byte 0 # 0 for interrupt gate
604 .byte 0x0e | 0x80 # type = 386 interrupt gate, present
605 .short 0 # offset 31:16
607 # invalid TSS (INT 0ah)
608 INVALID_TSS_SEL = .-IDT_BASE
609 .short 0 # offset 15:0
610 .short SYS_CODE_SEL # selector 15:0
611 .byte 0 # 0 for interrupt gate
612 .byte 0x0e | 0x80 # type = 386 interrupt gate, present
613 .short 0 # offset 31:16
615 # segment not present (INT 0bh)
616 SEG_NOT_PRESENT_SEL = .-IDT_BASE
617 .short 0 # offset 15:0
618 .short SYS_CODE_SEL # selector 15:0
619 .byte 0 # 0 for interrupt gate
620 .byte 0x0e | 0x80 # type = 386 interrupt gate, present
621 .short 0 # offset 31:16
623 # stack fault (INT 0ch)
624 STACK_FAULT_SEL = .-IDT_BASE
625 .short 0 # offset 15:0
626 .short SYS_CODE_SEL # selector 15:0
627 .byte 0 # 0 for interrupt gate
628 .byte 0x0e | 0x80 # type = 386 interrupt gate, present
629 .short 0 # offset 31:16
631 # general protection (INT 0dh)
632 GP_FAULT_SEL = .-IDT_BASE
633 .short 0 # offset 15:0
634 .short SYS_CODE_SEL # selector 15:0
635 .byte 0 # 0 for interrupt gate
636 .byte 0x0e | 0x80 # type = 386 interrupt gate, present
637 .short 0 # offset 31:16
639 # page fault (INT 0eh)
640 PAGE_FAULT_SEL = .-IDT_BASE
641 .short 0 # offset 15:0
642 .short SYS_CODE_SEL # selector 15:0
643 .byte 0 # 0 for interrupt gate
644 .byte 0x0e | 0x80 # type = 386 interrupt gate, present
645 .short 0 # offset 31:16
647 # Intel reserved - do not use (INT 0fh)
648 RSVD_INTR_SEL2 = .-IDT_BASE
649 .short 0 # offset 15:0
650 .short SYS_CODE_SEL # selector 15:0
651 .byte 0 # 0 for interrupt gate
652 .byte 0x0e | 0x80 # type = 386 interrupt gate, present
653 .short 0 # offset 31:16
655 # floating point error (INT 0x10)
656 FLT_POINT_ERR_SEL = .-IDT_BASE
657 .short 0 # offset 15:0
658 .short SYS_CODE_SEL # selector 15:0
659 .byte 0 # 0 for interrupt gate
660 .byte 0x0e | 0x80 # type = 386 interrupt gate, present
661 .short 0 # offset 31:16
663 # alignment check (INT 0x11)
664 ALIGNMENT_CHECK_SEL = .-IDT_BASE
665 .short 0 # offset 15:0
666 .short SYS_CODE_SEL # selector 15:0
667 .byte 0 # 0 for interrupt gate
668 .byte 0x0e | 0x80 # (10001110)type = 386 interrupt gate, present
669 .short 0 # offset 31:16
671 # machine check (INT 0x12)
672 MACHINE_CHECK_SEL = .-IDT_BASE
673 .short 0 # offset 15:0
674 .short SYS_CODE_SEL # selector 15:0
675 .byte 0 # 0 for interrupt gate
676 .byte 0x0e | 0x80 # (10001110)type = 386 interrupt gate, present
677 .short 0 # offset 31:16
679 # SIMD floating-point exception (INT 0x13)
680 SIMD_EXCEPTION_SEL = .-IDT_BASE
681 .short 0 # offset 15:0
682 .short SYS_CODE_SEL # selector 15:0
683 .byte 0 # 0 for interrupt gate
684 .byte 0x0e | 0x80 # (10001110)type = 386 interrupt gate, present
685 .short 0 # offset 31:16
688 .short 0 # offset 15:0
689 .short SYS_CODE_SEL # selector 15:0
690 .byte 0 # 0 for interrupt gate
691 .byte 0x0e | 0x80 # (10001110)type = 386 interrupt gate, present
692 .short 0 # offset 31:16
695 # 72 unspecified descriptors
700 # IRQ 0 (System timer) - (INT 0x68)
701 IRQ0_SEL = .-IDT_BASE
702 .short 0 # offset 15:0
703 .short SYS_CODE_SEL # selector 15:0
704 .byte 0 # 0 for interrupt gate
705 .byte 0x0e | 0x80 # (10001110)type = 386 interrupt gate, present
706 .short 0 # offset 31:16
708 # IRQ 1 (8042 Keyboard controller) - (INT 0x69)
709 IRQ1_SEL = .-IDT_BASE
710 .short 0 # offset 15:0
711 .short SYS_CODE_SEL # selector 15:0
712 .byte 0 # 0 for interrupt gate
713 .byte 0x0e | 0x80 # (10001110)type = 386 interrupt gate, present
714 .short 0 # offset 31:16
716 # Reserved - IRQ 2 redirect (IRQ 2) - DO NOT USE!!! - (INT 6ah)
717 IRQ2_SEL = .-IDT_BASE
718 .short 0 # offset 15:0
719 .short SYS_CODE_SEL # selector 15:0
720 .byte 0 # 0 for interrupt gate
721 .byte 0x0e | 0x80 # (10001110)type = 386 interrupt gate, present
722 .short 0 # offset 31:16
724 # IRQ 3 (COM 2) - (INT 6bh)
725 IRQ3_SEL = .-IDT_BASE
726 .short 0 # offset 15:0
727 .short SYS_CODE_SEL # selector 15:0
728 .byte 0 # 0 for interrupt gate
729 .byte 0x0e | 0x80 # (10001110)type = 386 interrupt gate, present
730 .short 0 # offset 31:16
732 # IRQ 4 (COM 1) - (INT 6ch)
733 IRQ4_SEL = .-IDT_BASE
734 .short 0 # offset 15:0
735 .short SYS_CODE_SEL # selector 15:0
736 .byte 0 # 0 for interrupt gate
737 .byte 0x0e | 0x80 # (10001110)type = 386 interrupt gate, present
738 .short 0 # offset 31:16
740 # IRQ 5 (LPT 2) - (INT 6dh)
741 IRQ5_SEL = .-IDT_BASE
742 .short 0 # offset 15:0
743 .short SYS_CODE_SEL # selector 15:0
744 .byte 0 # 0 for interrupt gate
745 .byte 0x0e | 0x80 # (10001110)type = 386 interrupt gate, present
746 .short 0 # offset 31:16
748 # IRQ 6 (Floppy controller) - (INT 6eh)
749 IRQ6_SEL = .-IDT_BASE
750 .short 0 # offset 15:0
751 .short SYS_CODE_SEL # selector 15:0
752 .byte 0 # 0 for interrupt gate
753 .byte 0x0e | 0x80 # (10001110)type = 386 interrupt gate, present
754 .short 0 # offset 31:16
756 # IRQ 7 (LPT 1) - (INT 6fh)
757 IRQ7_SEL = .-IDT_BASE
758 .short 0 # offset 15:0
759 .short SYS_CODE_SEL # selector 15:0
760 .byte 0 # 0 for interrupt gate
761 .byte 0x0e | 0x80 # (10001110)type = 386 interrupt gate, present
762 .short 0 # offset 31:16
764 # IRQ 8 (RTC Alarm) - (INT 0x70)
765 IRQ8_SEL = .-IDT_BASE
766 .short 0 # offset 15:0
767 .short SYS_CODE_SEL # selector 15:0
768 .byte 0 # 0 for interrupt gate
769 .byte 0x0e | 0x80 # (10001110)type = 386 interrupt gate, present
770 .short 0 # offset 31:16
773 IRQ9_SEL = .-IDT_BASE
774 .short 0 # offset 15:0
775 .short SYS_CODE_SEL # selector 15:0
776 .byte 0 # 0 for interrupt gate
777 .byte 0x0e | 0x80 # (10001110)type = 386 interrupt gate, present
778 .short 0 # offset 31:16
780 # IRQ 10 - (INT 0x72)
781 IRQ10_SEL = .-IDT_BASE
782 .short 0 # offset 15:0
783 .short SYS_CODE_SEL # selector 15:0
784 .byte 0 # 0 for interrupt gate
785 .byte 0x0e | 0x80 # (10001110)type = 386 interrupt gate, present
786 .short 0 # offset 31:16
788 # IRQ 11 - (INT 0x73)
789 IRQ11_SEL = .-IDT_BASE
790 .short 0 # offset 15:0
791 .short SYS_CODE_SEL # selector 15:0
792 .byte 0 # 0 for interrupt gate
793 .byte 0x0e | 0x80 # (10001110)type = 386 interrupt gate, present
794 .short 0 # offset 31:16
796 # IRQ 12 (PS/2 mouse) - (INT 0x74)
797 IRQ12_SEL = .-IDT_BASE
798 .short 0 # offset 15:0
799 .short SYS_CODE_SEL # selector 15:0
800 .byte 0 # 0 for interrupt gate
801 .byte 0x0e | 0x80 # (10001110)type = 386 interrupt gate, present
802 .short 0 # offset 31:16
804 # IRQ 13 (Floating point error) - (INT 0x75)
805 IRQ13_SEL = .-IDT_BASE
806 .short 0 # offset 15:0
807 .short SYS_CODE_SEL # selector 15:0
808 .byte 0 # 0 for interrupt gate
809 .byte 0x0e | 0x80 # (10001110)type = 386 interrupt gate, present
810 .short 0 # offset 31:16
812 # IRQ 14 (Secondary IDE) - (INT 0x76)
813 IRQ14_SEL = .-IDT_BASE
814 .short 0 # offset 15:0
815 .short SYS_CODE_SEL # selector 15:0
816 .byte 0 # 0 for interrupt gate
817 .byte 0x0e | 0x80 # (10001110)type = 386 interrupt gate, present
818 .short 0 # offset 31:16
820 # IRQ 15 (Primary IDE) - (INT 0x77)
821 IRQ15_SEL = .-IDT_BASE
822 .short 0 # offset 15:0
823 .short SYS_CODE_SEL # selector 15:0
824 .byte 0 # 0 for interrupt gate
825 .byte 0x0e | 0x80 # (10001110)type = 386 interrupt gate, present
826 .short 0 # offset 31:16