xref: /illumos-gate/usr/src/boot/efi/loader/arch/amd64/exc.S (revision 22028508)
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
30	.macro	EH	N, err=1
31	.align	8
32	.globl	EXC\N\()_handler
33EXC\N\()_handler:
34	.if	\err != 1
35	pushq	$0
36	.endif
37	pushq	%rax
38	pushq	%rdx
39	pushq	%rcx
40	movl	$\N,%ecx
41	jmp	all_handlers
42	.endm
43
44	.text
45	EH	0,0
46	EH	1,0
47	EH	2,0
48	EH	3,0
49	EH	4,0
50	EH	5,0
51	EH	6,0
52	EH	7,0
53	EH	8
54	EH	9,0
55	EH	10
56	EH	11
57	EH	12
58	EH	13
59	EH	14
60	EH	16,0
61	EH	17
62	EH	18,0
63	EH	19,0
64	EH	20,0
65
66	.globl	exc_rsp
67all_handlers:
68	cmpq	%rsp,exc_rsp(%rip)
69	je	exception
70
71	/*
72	 * Interrupt, not exception.
73	 * First, copy the hardware interrupt frame to the previous stack.
74	 * Our handler always has private IST stack.
75	 */
76	movq	(6*8)(%rsp),%rax	/* saved %rsp value, AKA old stack */
77	subq	(5*8),%rax
78	movq	(3*8)(%rsp),%rdx	/* copy %rip to old stack */
79	movq	%rdx,(%rax)
80	movq	(4*8)(%rsp),%rdx	/* copy %cs */
81	movq	%rdx,(1*8)(%rax)
82	movq	(5*8)(%rsp),%rdx	/* copy %rflags */
83	movq	%rdx,(2*8)(%rax)
84	movq	(6*8)(%rsp),%rdx	/* copy %rsp */
85	movq	%rdx,(3*8)(%rax)
86	movq	(7*8)(%rsp),%rdx	/* copy %ss */
87	movq	%rdx,(4*8)(%rax)
88
89	/*
90	 * Now simulate invocation of the original interrupt handler
91	 * with retq.  We switch stacks and execute retq from the old
92	 * stack since there is no free registers at the last moment.
93	 */
94	subq	$16,%rax
95	leaq	fw_intr_handlers(%rip),%rdx
96	movq	(%rdx,%rcx,8),%rdx /* push intr handler address on old stack */
97	movq	%rdx,8(%rax)
98	movq	(2*8)(%rsp),%rcx   /* saved %rax is put on top of old stack */
99	movq	%rcx,(%rax)
100	movq	(%rsp),%rcx
101	movq	8(%rsp),%rdx
102
103	movq	32(%rsp),%rsp	/* switch to old stack */
104	popq	%rax
105	retq
106
107exception:
108	/*
109	 * Form the struct trapframe on our IST stack.
110	 * Skip three words, which are currently busy with temporal
111	 * saves.
112	 */
113	pushq	%r15
114	pushq	%r14
115	pushq	%r13
116	pushq	%r12
117	pushq	%r11
118	pushq	%r10
119	pushq	%rbp
120	pushq	%rbx
121	pushq	$0	/* %rax	*/
122	pushq	%r9
123	pushq	%r8
124	pushq	$0	/* %rcx */
125	pushq	$0	/* %rdx	*/
126	pushq	%rsi
127	pushq	%rdi
128
129	/*
130	 * Move %rax, %rdx, %rcx values into the final location,
131	 * from the three words which were skipped above.
132	 */
133	movq	0x88(%rsp),%rax
134	movq	%rax,0x30(%rsp)	/* tf_rax */
135	movq	0x78(%rsp),%rax
136	movq	%rax,0x18(%rsp)	/* tf_rcx */
137	movq	0x80(%rsp),%rax
138	movq	%rax,0x10(%rsp)	/* tf_rdx */
139
140	/*
141	 * And fill the three words themself.
142	 */
143	movq	%cr2,%rax
144	movq	%rax,0x80(%rsp)	/* tf_addr */
145	movl	%ecx,0x78(%rsp)	/* tf_trapno */
146	movw	%ds,0x8e(%rsp)
147	movw	%es,0x8c(%rsp)
148	movw	%fs,0x7c(%rsp)
149	movw	%gs,0x7e(%rsp)
150	movw	$0,0x88(%rsp)	/* tf_flags */
151
152	/*
153	 * Call dump routine.
154	 */
155	movq	%rsp,%rdi
156	callq	report_exc
157
158	/*
159	 * Hang after reporting. Interrupts are already disabled.
160	 */
1611:
162	hlt
163	jmp	1b
164