xref: /freebsd/stand/efi/loader/arch/amd64/exc.S (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1/*-
2 * Copyright (c) 2016 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Konstantin Belousov under sponsorship
6 * from the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD$
30 */
31
32	.macro	EH	N, err=1
33	.align	8
34	.globl	EXC\N\()_handler
35EXC\N\()_handler:
36	.if	\err != 1
37	pushq	$0
38	.endif
39	pushq	%rax
40	pushq	%rdx
41	pushq	%rcx
42	movl	$\N,%ecx
43	jmp	all_handlers
44	.endm
45
46	.text
47	EH	0,0
48	EH	1,0
49	EH	2,0
50	EH	3,0
51	EH	4,0
52	EH	5,0
53	EH	6,0
54	EH	7,0
55	EH	8
56	EH	9,0
57	EH	10
58	EH	11
59	EH	12
60	EH	13
61	EH	14
62	EH	16,0
63	EH	17
64	EH	18,0
65	EH	19,0
66	EH	20,0
67
68	.globl	exc_rsp
69all_handlers:
70	cmpq	%rsp,exc_rsp(%rip)
71	je	exception
72
73	/*
74	 * Interrupt, not exception.
75	 * First, copy the hardware interrupt frame to the previous stack.
76	 * Our handler always has private IST stack.
77	 */
78	movq	(6*8)(%rsp),%rax	/* saved %rsp value, AKA old stack */
79	subq	(5*8),%rax
80	movq	(3*8)(%rsp),%rdx	/* copy %rip to old stack */
81	movq	%rdx,(%rax)
82	movq	(4*8)(%rsp),%rdx	/* copy %cs */
83	movq	%rdx,(1*8)(%rax)
84	movq	(5*8)(%rsp),%rdx	/* copy %rflags */
85	movq	%rdx,(2*8)(%rax)
86	movq	(6*8)(%rsp),%rdx	/* copy %rsp */
87	movq	%rdx,(3*8)(%rax)
88	movq	(7*8)(%rsp),%rdx	/* copy %ss */
89	movq	%rdx,(4*8)(%rax)
90
91	/*
92	 * Now simulate invocation of the original interrupt handler
93	 * with retq.  We switch stacks and execute retq from the old
94	 * stack since there is no free registers at the last moment.
95	 */
96	subq	$16,%rax
97	leaq	fw_intr_handlers(%rip),%rdx
98	movq	(%rdx,%rcx,8),%rdx /* push intr handler address on old stack */
99	movq	%rdx,8(%rax)
100	movq	(2*8)(%rsp),%rcx   /* saved %rax is put on top of old stack */
101	movq	%rcx,(%rax)
102	movq	(%rsp),%rcx
103	movq	8(%rsp),%rdx
104
105	movq	32(%rsp),%rsp	/* switch to old stack */
106	popq	%rax
107	retq
108
109exception:
110	/*
111	 * Form the struct trapframe on our IST stack.
112	 * Skip three words, which are currently busy with temporal
113	 * saves.
114	 */
115	pushq	%r15
116	pushq	%r14
117	pushq	%r13
118	pushq	%r12
119	pushq	%r11
120	pushq	%r10
121	pushq	%rbp
122	pushq	%rbx
123	pushq	$0	/* %rax	*/
124	pushq	%r9
125	pushq	%r8
126	pushq	$0	/* %rcx */
127	pushq	$0	/* %rdx	*/
128	pushq	%rsi
129	pushq	%rdi
130
131	/*
132	 * Move %rax, %rdx, %rcx values into the final location,
133	 * from the three words which were skipped above.
134	 */
135	movq	0x88(%rsp),%rax
136	movq	%rax,0x30(%rsp)	/* tf_rax */
137	movq	0x78(%rsp),%rax
138	movq	%rax,0x18(%rsp)	/* tf_rcx */
139	movq	0x80(%rsp),%rax
140	movq	%rax,0x10(%rsp)	/* tf_rdx */
141
142	/*
143	 * And fill the three words themself.
144	 */
145	movq	%cr2,%rax
146	movq	%rax,0x80(%rsp)	/* tf_addr */
147	movl	%ecx,0x78(%rsp)	/* tf_trapno */
148	movw	%ds,0x8e(%rsp)
149	movw	%es,0x8c(%rsp)
150	movw	%fs,0x7c(%rsp)
151	movw	%gs,0x7e(%rsp)
152	movw	$0,0x88(%rsp)	/* tf_flags */
153
154	/*
155	 * Call dump routine.
156	 */
157	movq	%rsp,%rdi
158	callq	report_exc
159
160	/*
161	 * Hang after reporting. Interrupts are already disabled.
162	 */
1631:
164	hlt
165	jmp	1b
166