/*-1* Copyright (c) 2016 The FreeBSD Foundation2*3* This software was developed by Konstantin Belousov under sponsorship4* from the FreeBSD Foundation.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728.macro EH N, err=129.align 830.globl EXC\N\()_handler31EXC\N\()_handler:32.if \err != 133pushq $034.endif35pushq %rax36pushq %rdx37pushq %rcx38movl $\N,%ecx39jmp all_handlers40.endm4142.text43EH 0,044EH 1,045EH 2,046EH 3,047EH 4,048EH 5,049EH 6,050EH 7,051EH 852EH 9,053EH 1054EH 1155EH 1256EH 1357EH 1458EH 16,059EH 1760EH 18,061EH 19,062EH 20,06364.globl exc_rsp65all_handlers:66cmpq %rsp,exc_rsp(%rip)67je exception6869/*70* Interrupt, not exception.71* First, copy the hardware interrupt frame to the previous stack.72* Our handler always has private IST stack.73*/74movq (6*8)(%rsp),%rax /* saved %rsp value, AKA old stack */75subq (5*8),%rax76movq (3*8)(%rsp),%rdx /* copy %rip to old stack */77movq %rdx,(%rax)78movq (4*8)(%rsp),%rdx /* copy %cs */79movq %rdx,(1*8)(%rax)80movq (5*8)(%rsp),%rdx /* copy %rflags */81movq %rdx,(2*8)(%rax)82movq (6*8)(%rsp),%rdx /* copy %rsp */83movq %rdx,(3*8)(%rax)84movq (7*8)(%rsp),%rdx /* copy %ss */85movq %rdx,(4*8)(%rax)8687/*88* Now simulate invocation of the original interrupt handler89* with retq. We switch stacks and execute retq from the old90* stack since there is no free registers at the last moment.91*/92subq $16,%rax93leaq fw_intr_handlers(%rip),%rdx94movq (%rdx,%rcx,8),%rdx /* push intr handler address on old stack */95movq %rdx,8(%rax)96movq (2*8)(%rsp),%rcx /* saved %rax is put on top of old stack */97movq %rcx,(%rax)98movq (%rsp),%rcx99movq 8(%rsp),%rdx100101movq 32(%rsp),%rsp /* switch to old stack */102popq %rax103retq104105exception:106/*107* Form the struct trapframe on our IST stack.108* Skip three words, which are currently busy with temporal109* saves.110*/111pushq %r15112pushq %r14113pushq %r13114pushq %r12115pushq %r11116pushq %r10117pushq %rbp118pushq %rbx119pushq $0 /* %rax */120pushq %r9121pushq %r8122pushq $0 /* %rcx */123pushq $0 /* %rdx */124pushq %rsi125pushq %rdi126127/*128* Move %rax, %rdx, %rcx values into the final location,129* from the three words which were skipped above.130*/131movq 0x88(%rsp),%rax132movq %rax,0x30(%rsp) /* tf_rax */133movq 0x78(%rsp),%rax134movq %rax,0x18(%rsp) /* tf_rcx */135movq 0x80(%rsp),%rax136movq %rax,0x10(%rsp) /* tf_rdx */137138/*139* And fill the three words themself.140*/141movq %cr2,%rax142movq %rax,0x80(%rsp) /* tf_addr */143movl %ecx,0x78(%rsp) /* tf_trapno */144movw %ds,0x8e(%rsp)145movw %es,0x8c(%rsp)146movw %fs,0x7c(%rsp)147movw %gs,0x7e(%rsp)148movw $0,0x88(%rsp) /* tf_flags */149150/*151* Call dump routine.152*/153movq %rsp,%rdi154callq report_exc155156/*157* Hang after reporting. Interrupts are already disabled.158*/1591:160hlt161jmp 1b162163164