xref: /openbmc/linux/arch/x86/xen/xen-asm.S (revision 901181b7)
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Asm versions of Xen pv-ops, suitable for direct use.
4 *
5 * We only bother with direct forms (ie, vcpu in percpu data) of the
6 * operations here; the indirect forms are better handled in C.
7 */
8
9#include <asm/errno.h>
10#include <asm/asm-offsets.h>
11#include <asm/percpu.h>
12#include <asm/processor-flags.h>
13#include <asm/segment.h>
14#include <asm/thread_info.h>
15#include <asm/asm.h>
16#include <asm/frame.h>
17#include <asm/unwind_hints.h>
18
19#include <xen/interface/xen.h>
20
21#include <linux/init.h>
22#include <linux/linkage.h>
23
24.pushsection .noinstr.text, "ax"
25/*
26 * Disabling events is simply a matter of making the event mask
27 * non-zero.
28 */
29SYM_FUNC_START(xen_irq_disable_direct)
30	movb $1, PER_CPU_VAR(xen_vcpu_info) + XEN_vcpu_info_mask
31	ret
32SYM_FUNC_END(xen_irq_disable_direct)
33
34/*
35 * Force an event check by making a hypercall, but preserve regs
36 * before making the call.
37 */
38SYM_FUNC_START(check_events)
39	FRAME_BEGIN
40	push %rax
41	push %rcx
42	push %rdx
43	push %rsi
44	push %rdi
45	push %r8
46	push %r9
47	push %r10
48	push %r11
49	call xen_force_evtchn_callback
50	pop %r11
51	pop %r10
52	pop %r9
53	pop %r8
54	pop %rdi
55	pop %rsi
56	pop %rdx
57	pop %rcx
58	pop %rax
59	FRAME_END
60	ret
61SYM_FUNC_END(check_events)
62
63/*
64 * Enable events.  This clears the event mask and tests the pending
65 * event status with one and operation.  If there are pending events,
66 * then enter the hypervisor to get them handled.
67 */
68SYM_FUNC_START(xen_irq_enable_direct)
69	FRAME_BEGIN
70	/* Unmask events */
71	movb $0, PER_CPU_VAR(xen_vcpu_info) + XEN_vcpu_info_mask
72
73	/*
74	 * Preempt here doesn't matter because that will deal with any
75	 * pending interrupts.  The pending check may end up being run
76	 * on the wrong CPU, but that doesn't hurt.
77	 */
78
79	/* Test for pending */
80	testb $0xff, PER_CPU_VAR(xen_vcpu_info) + XEN_vcpu_info_pending
81	jz 1f
82
83	call check_events
841:
85	FRAME_END
86	ret
87SYM_FUNC_END(xen_irq_enable_direct)
88
89/*
90 * (xen_)save_fl is used to get the current interrupt enable status.
91 * Callers expect the status to be in X86_EFLAGS_IF, and other bits
92 * may be set in the return value.  We take advantage of this by
93 * making sure that X86_EFLAGS_IF has the right value (and other bits
94 * in that byte are 0), but other bits in the return value are
95 * undefined.  We need to toggle the state of the bit, because Xen and
96 * x86 use opposite senses (mask vs enable).
97 */
98SYM_FUNC_START(xen_save_fl_direct)
99	testb $0xff, PER_CPU_VAR(xen_vcpu_info) + XEN_vcpu_info_mask
100	setz %ah
101	addb %ah, %ah
102	ret
103SYM_FUNC_END(xen_save_fl_direct)
104
105SYM_FUNC_START(xen_read_cr2)
106	FRAME_BEGIN
107	_ASM_MOV PER_CPU_VAR(xen_vcpu), %_ASM_AX
108	_ASM_MOV XEN_vcpu_info_arch_cr2(%_ASM_AX), %_ASM_AX
109	FRAME_END
110	ret
111SYM_FUNC_END(xen_read_cr2);
112
113SYM_FUNC_START(xen_read_cr2_direct)
114	FRAME_BEGIN
115	_ASM_MOV PER_CPU_VAR(xen_vcpu_info) + XEN_vcpu_info_arch_cr2, %_ASM_AX
116	FRAME_END
117	ret
118SYM_FUNC_END(xen_read_cr2_direct);
119.popsection
120
121.macro xen_pv_trap name
122SYM_CODE_START(xen_\name)
123	UNWIND_HINT_EMPTY
124	pop %rcx
125	pop %r11
126	jmp  \name
127SYM_CODE_END(xen_\name)
128_ASM_NOKPROBE(xen_\name)
129.endm
130
131xen_pv_trap asm_exc_divide_error
132xen_pv_trap asm_xenpv_exc_debug
133xen_pv_trap asm_exc_int3
134xen_pv_trap asm_xenpv_exc_nmi
135xen_pv_trap asm_exc_overflow
136xen_pv_trap asm_exc_bounds
137xen_pv_trap asm_exc_invalid_op
138xen_pv_trap asm_exc_device_not_available
139xen_pv_trap asm_xenpv_exc_double_fault
140xen_pv_trap asm_exc_coproc_segment_overrun
141xen_pv_trap asm_exc_invalid_tss
142xen_pv_trap asm_exc_segment_not_present
143xen_pv_trap asm_exc_stack_segment
144xen_pv_trap asm_exc_general_protection
145xen_pv_trap asm_exc_page_fault
146xen_pv_trap asm_exc_spurious_interrupt_bug
147xen_pv_trap asm_exc_coprocessor_error
148xen_pv_trap asm_exc_alignment_check
149#ifdef CONFIG_X86_MCE
150xen_pv_trap asm_xenpv_exc_machine_check
151#endif /* CONFIG_X86_MCE */
152xen_pv_trap asm_exc_simd_coprocessor_error
153#ifdef CONFIG_IA32_EMULATION
154xen_pv_trap entry_INT80_compat
155#endif
156xen_pv_trap asm_exc_xen_unknown_trap
157xen_pv_trap asm_exc_xen_hypervisor_callback
158
159	__INIT
160SYM_CODE_START(xen_early_idt_handler_array)
161	i = 0
162	.rept NUM_EXCEPTION_VECTORS
163	UNWIND_HINT_EMPTY
164	pop %rcx
165	pop %r11
166	jmp early_idt_handler_array + i*EARLY_IDT_HANDLER_SIZE
167	i = i + 1
168	.fill xen_early_idt_handler_array + i*XEN_EARLY_IDT_HANDLER_SIZE - ., 1, 0xcc
169	.endr
170SYM_CODE_END(xen_early_idt_handler_array)
171	__FINIT
172
173hypercall_iret = hypercall_page + __HYPERVISOR_iret * 32
174/*
175 * Xen64 iret frame:
176 *
177 *	ss
178 *	rsp
179 *	rflags
180 *	cs
181 *	rip		<-- standard iret frame
182 *
183 *	flags
184 *
185 *	rcx		}
186 *	r11		}<-- pushed by hypercall page
187 * rsp->rax		}
188 */
189SYM_CODE_START(xen_iret)
190	UNWIND_HINT_EMPTY
191	pushq $0
192	jmp hypercall_iret
193SYM_CODE_END(xen_iret)
194
195/*
196 * Xen handles syscall callbacks much like ordinary exceptions, which
197 * means we have:
198 * - kernel gs
199 * - kernel rsp
200 * - an iret-like stack frame on the stack (including rcx and r11):
201 *	ss
202 *	rsp
203 *	rflags
204 *	cs
205 *	rip
206 *	r11
207 * rsp->rcx
208 */
209
210/* Normal 64-bit system call target */
211SYM_CODE_START(xen_syscall_target)
212	UNWIND_HINT_EMPTY
213	popq %rcx
214	popq %r11
215
216	/*
217	 * Neither Xen nor the kernel really knows what the old SS and
218	 * CS were.  The kernel expects __USER_DS and __USER_CS, so
219	 * report those values even though Xen will guess its own values.
220	 */
221	movq $__USER_DS, 4*8(%rsp)
222	movq $__USER_CS, 1*8(%rsp)
223
224	jmp entry_SYSCALL_64_after_hwframe
225SYM_CODE_END(xen_syscall_target)
226
227#ifdef CONFIG_IA32_EMULATION
228
229/* 32-bit compat syscall target */
230SYM_CODE_START(xen_syscall32_target)
231	UNWIND_HINT_EMPTY
232	popq %rcx
233	popq %r11
234
235	/*
236	 * Neither Xen nor the kernel really knows what the old SS and
237	 * CS were.  The kernel expects __USER32_DS and __USER32_CS, so
238	 * report those values even though Xen will guess its own values.
239	 */
240	movq $__USER32_DS, 4*8(%rsp)
241	movq $__USER32_CS, 1*8(%rsp)
242
243	jmp entry_SYSCALL_compat_after_hwframe
244SYM_CODE_END(xen_syscall32_target)
245
246/* 32-bit compat sysenter target */
247SYM_CODE_START(xen_sysenter_target)
248	UNWIND_HINT_EMPTY
249	/*
250	 * NB: Xen is polite and clears TF from EFLAGS for us.  This means
251	 * that we don't need to guard against single step exceptions here.
252	 */
253	popq %rcx
254	popq %r11
255
256	/*
257	 * Neither Xen nor the kernel really knows what the old SS and
258	 * CS were.  The kernel expects __USER32_DS and __USER32_CS, so
259	 * report those values even though Xen will guess its own values.
260	 */
261	movq $__USER32_DS, 4*8(%rsp)
262	movq $__USER32_CS, 1*8(%rsp)
263
264	jmp entry_SYSENTER_compat_after_hwframe
265SYM_CODE_END(xen_sysenter_target)
266
267#else /* !CONFIG_IA32_EMULATION */
268
269SYM_CODE_START(xen_syscall32_target)
270SYM_CODE_START(xen_sysenter_target)
271	UNWIND_HINT_EMPTY
272	lea 16(%rsp), %rsp	/* strip %rcx, %r11 */
273	mov $-ENOSYS, %rax
274	pushq $0
275	jmp hypercall_iret
276SYM_CODE_END(xen_sysenter_target)
277SYM_CODE_END(xen_syscall32_target)
278
279#endif	/* CONFIG_IA32_EMULATION */
280