Skip to content

Commit 2f6474e

Browse files
committed
x86/entry: Switch XEN/PV hypercall entry to IDTENTRY
Convert the XEN/PV hypercall to IDTENTRY: - Emit the ASM stub with DECLARE_IDTENTRY - Remove the ASM idtentry in 64-bit - Remove the open coded ASM entry code in 32-bit - Remove the old prototypes The handler stubs need to stay in ASM code as they need corner case handling and adjustment of the stack pointer. Provide a new C function which invokes the entry/exit handling and calls into the XEN handler on the interrupt stack if required. The exit code is slightly different from the regular idtentry_exit() on non-preemptible kernels. If the hypercall is preemptible and need_resched() is set then XEN provides a preempt hypercall scheduling function. Move this functionality into the entry code so it can use the existing idtentry functionality. [ mingo: Build fixes. ] Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Ingo Molnar <mingo@kernel.org> Acked-by: Andy Lutomirski <luto@kernel.org> Acked-by: Juergen Gross <jgross@suse.com> Tested-by: Juergen Gross <jgross@suse.com> Link: https://lore.kernel.org/r/20200521202118.055270078@linutronix.de
1 parent 1de16e0 commit 2f6474e

12 files changed

Lines changed: 151 additions & 86 deletions

File tree

‎arch/x86/entry/common.c‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
#include <linux/syscalls.h>
2828
#include <linux/uaccess.h>
2929

30+
#ifdef CONFIG_XEN_PV
31+
#include <xen/xen-ops.h>
32+
#include <xen/events.h>
33+
#endif
34+
3035
#include <asm/desc.h>
3136
#include <asm/traps.h>
3237
#include <asm/vdso.h>
@@ -35,6 +40,7 @@
3540
#include <asm/nospec-branch.h>
3641
#include <asm/io_bitmap.h>
3742
#include <asm/syscall.h>
43+
#include <asm/irq_stack.h>
3844

3945
#define CREATE_TRACE_POINTS
4046
#include <trace/events/syscalls.h>
@@ -680,3 +686,75 @@ void noinstr idtentry_exit_user(struct pt_regs *regs)
680686

681687
prepare_exit_to_usermode(regs);
682688
}
689+
690+
#ifdef CONFIG_XEN_PV
691+
#ifndef CONFIG_PREEMPTION
692+
/*
693+
* Some hypercalls issued by the toolstack can take many 10s of
694+
* seconds. Allow tasks running hypercalls via the privcmd driver to
695+
* be voluntarily preempted even if full kernel preemption is
696+
* disabled.
697+
*
698+
* Such preemptible hypercalls are bracketed by
699+
* xen_preemptible_hcall_begin() and xen_preemptible_hcall_end()
700+
* calls.
701+
*/
702+
DEFINE_PER_CPU(bool, xen_in_preemptible_hcall);
703+
EXPORT_SYMBOL_GPL(xen_in_preemptible_hcall);
704+
705+
/*
706+
* In case of scheduling the flag must be cleared and restored after
707+
* returning from schedule as the task might move to a different CPU.
708+
*/
709+
static __always_inline bool get_and_clear_inhcall(void)
710+
{
711+
bool inhcall = __this_cpu_read(xen_in_preemptible_hcall);
712+
713+
__this_cpu_write(xen_in_preemptible_hcall, false);
714+
return inhcall;
715+
}
716+
717+
static __always_inline void restore_inhcall(bool inhcall)
718+
{
719+
__this_cpu_write(xen_in_preemptible_hcall, inhcall);
720+
}
721+
#else
722+
static __always_inline bool get_and_clear_inhcall(void) { return false; }
723+
static __always_inline void restore_inhcall(bool inhcall) { }
724+
#endif
725+
726+
static void __xen_pv_evtchn_do_upcall(void)
727+
{
728+
irq_enter_rcu();
729+
inc_irq_stat(irq_hv_callback_count);
730+
731+
xen_hvm_evtchn_do_upcall();
732+
733+
irq_exit_rcu();
734+
}
735+
736+
__visible noinstr void xen_pv_evtchn_do_upcall(struct pt_regs *regs)
737+
{
738+
struct pt_regs *old_regs;
739+
bool inhcall, rcu_exit;
740+
741+
rcu_exit = idtentry_enter_cond_rcu(regs);
742+
old_regs = set_irq_regs(regs);
743+
744+
instrumentation_begin();
745+
run_on_irqstack_cond(__xen_pv_evtchn_do_upcall, NULL, regs);
746+
instrumentation_begin();
747+
748+
set_irq_regs(old_regs);
749+
750+
inhcall = get_and_clear_inhcall();
751+
if (inhcall && !WARN_ON_ONCE(rcu_exit)) {
752+
instrumentation_begin();
753+
idtentry_exit_cond_resched(regs, true);
754+
instrumentation_end();
755+
restore_inhcall(inhcall);
756+
} else {
757+
idtentry_exit_cond_rcu(regs, rcu_exit);
758+
}
759+
}
760+
#endif /* CONFIG_XEN_PV */

‎arch/x86/entry/entry_32.S‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,13 @@ SYM_CODE_END(native_iret)
12981298
#endif
12991299

13001300
#ifdef CONFIG_XEN_PV
1301-
SYM_FUNC_START(xen_hypervisor_callback)
1301+
/*
1302+
* See comment in entry_64.S for further explanation
1303+
*
1304+
* Note: This is not an actual IDT entry point. It's a XEN specific entry
1305+
* point and therefore named to match the 64-bit trampoline counterpart.
1306+
*/
1307+
SYM_FUNC_START(xen_asm_exc_xen_hypervisor_callback)
13021308
/*
13031309
* Check to see if we got the event in the critical
13041310
* region in xen_iret_direct, after we've reenabled
@@ -1315,14 +1321,11 @@ SYM_FUNC_START(xen_hypervisor_callback)
13151321
pushl $-1 /* orig_ax = -1 => not a system call */
13161322
SAVE_ALL
13171323
ENCODE_FRAME_POINTER
1318-
TRACE_IRQS_OFF
1324+
13191325
mov %esp, %eax
1320-
call xen_evtchn_do_upcall
1321-
#ifndef CONFIG_PREEMPTION
1322-
call xen_maybe_preempt_hcall
1323-
#endif
1324-
jmp ret_from_intr
1325-
SYM_FUNC_END(xen_hypervisor_callback)
1326+
call xen_pv_evtchn_do_upcall
1327+
jmp handle_exception_return
1328+
SYM_FUNC_END(xen_asm_exc_xen_hypervisor_callback)
13261329

13271330
/*
13281331
* Hypervisor uses this for application faults while it executes.
@@ -1464,6 +1467,7 @@ SYM_CODE_START_LOCAL_NOALIGN(handle_exception)
14641467
movl %esp, %eax # pt_regs pointer
14651468
CALL_NOSPEC edi
14661469

1470+
handle_exception_return:
14671471
#ifdef CONFIG_VM86
14681472
movl PT_EFLAGS(%esp), %eax # mix EFLAGS and CS
14691473
movb PT_CS(%esp), %al

‎arch/x86/entry/entry_64.S‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,10 +1067,6 @@ apicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt
10671067

10681068
idtentry X86_TRAP_PF page_fault do_page_fault has_error_code=1
10691069

1070-
#ifdef CONFIG_XEN_PV
1071-
idtentry 512 /* dummy */ hypervisor_callback xen_do_hypervisor_callback has_error_code=0
1072-
#endif
1073-
10741070
/*
10751071
* Reload gs selector with exception handling
10761072
* edi: new selector
@@ -1158,9 +1154,10 @@ SYM_FUNC_END(asm_call_on_stack)
11581154
* So, on entry to the handler we detect whether we interrupted an
11591155
* existing activation in its critical region -- if so, we pop the current
11601156
* activation and restart the handler using the previous one.
1157+
*
1158+
* C calling convention: exc_xen_hypervisor_callback(struct *pt_regs)
11611159
*/
1162-
/* do_hypervisor_callback(struct *pt_regs) */
1163-
SYM_CODE_START_LOCAL(xen_do_hypervisor_callback)
1160+
SYM_CODE_START_LOCAL(exc_xen_hypervisor_callback)
11641161

11651162
/*
11661163
* Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
@@ -1170,15 +1167,10 @@ SYM_CODE_START_LOCAL(xen_do_hypervisor_callback)
11701167
movq %rdi, %rsp /* we don't return, adjust the stack frame */
11711168
UNWIND_HINT_REGS
11721169

1173-
ENTER_IRQ_STACK old_rsp=%r10
1174-
call xen_evtchn_do_upcall
1175-
LEAVE_IRQ_STACK
1170+
call xen_pv_evtchn_do_upcall
11761171

1177-
#ifndef CONFIG_PREEMPTION
1178-
call xen_maybe_preempt_hcall
1179-
#endif
1180-
jmp error_exit
1181-
SYM_CODE_END(xen_do_hypervisor_callback)
1172+
jmp error_return
1173+
SYM_CODE_END(exc_xen_hypervisor_callback)
11821174

11831175
/*
11841176
* Hypervisor uses this for application faults while it executes.

‎arch/x86/include/asm/idtentry.h‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,21 @@ __visible noinstr void func(struct pt_regs *regs)
165165
#define DEFINE_IDTENTRY_RAW_ERRORCODE(func) \
166166
__visible noinstr void func(struct pt_regs *regs, unsigned long error_code)
167167

168+
/**
169+
* DECLARE_IDTENTRY_XENCB - Declare functions for XEN HV callback entry point
170+
* @vector: Vector number (ignored for C)
171+
* @func: Function name of the entry point
172+
*
173+
* Declares three functions:
174+
* - The ASM entry point: asm_##func
175+
* - The XEN PV trap entry point: xen_##func (maybe unused)
176+
* - The C handler called from the ASM entry point
177+
*
178+
* Maps to DECLARE_IDTENTRY(). Distinct entry point to handle the 32/64-bit
179+
* difference
180+
*/
181+
#define DECLARE_IDTENTRY_XENCB(vector, func) \
182+
DECLARE_IDTENTRY(vector, func)
168183

169184
#ifdef CONFIG_X86_64
170185
/**
@@ -307,6 +322,9 @@ __visible noinstr void func(struct pt_regs *regs, \
307322
# define DECLARE_IDTENTRY_DF(vector, func) \
308323
idtentry_df vector asm_##func func
309324

325+
# define DECLARE_IDTENTRY_XENCB(vector, func) \
326+
DECLARE_IDTENTRY(vector, func)
327+
310328
#else
311329
# define DECLARE_IDTENTRY_MCE(vector, func) \
312330
DECLARE_IDTENTRY(vector, func)
@@ -317,6 +335,9 @@ __visible noinstr void func(struct pt_regs *regs, \
317335
/* No ASM emitted for DF as this goes through a C shim */
318336
# define DECLARE_IDTENTRY_DF(vector, func)
319337

338+
/* No ASM emitted for XEN hypervisor callback */
339+
# define DECLARE_IDTENTRY_XENCB(vector, func)
340+
320341
#endif
321342

322343
/* No ASM code emitted for NMI */
@@ -337,6 +358,13 @@ __visible noinstr void func(struct pt_regs *regs, \
337358
* This avoids duplicate defines and ensures that everything is consistent.
338359
*/
339360

361+
/*
362+
* Dummy trap number so the low level ASM macro vector number checks do not
363+
* match which results in emitting plain IDTENTRY stubs without bells and
364+
* whistels.
365+
*/
366+
#define X86_TRAP_OTHER 0xFFFF
367+
340368
/* Simple exception entry points. No hardware error code */
341369
DECLARE_IDTENTRY(X86_TRAP_DE, exc_divide_error);
342370
DECLARE_IDTENTRY(X86_TRAP_OF, exc_overflow);
@@ -376,4 +404,10 @@ DECLARE_IDTENTRY_XEN(X86_TRAP_DB, debug);
376404
/* #DF */
377405
DECLARE_IDTENTRY_DF(X86_TRAP_DF, exc_double_fault);
378406

407+
#ifdef CONFIG_XEN_PV
408+
DECLARE_IDTENTRY_XENCB(X86_TRAP_OTHER, exc_xen_hypervisor_callback);
409+
#endif
410+
411+
#undef X86_TRAP_OTHER
412+
379413
#endif

‎arch/x86/xen/setup.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <asm/setup.h>
2121
#include <asm/acpi.h>
2222
#include <asm/numa.h>
23+
#include <asm/idtentry.h>
2324
#include <asm/xen/hypervisor.h>
2425
#include <asm/xen/hypercall.h>
2526

@@ -993,7 +994,8 @@ static void __init xen_pvmmu_arch_setup(void)
993994
HYPERVISOR_vm_assist(VMASST_CMD_enable,
994995
VMASST_TYPE_pae_extended_cr3);
995996

996-
if (register_callback(CALLBACKTYPE_event, xen_hypervisor_callback) ||
997+
if (register_callback(CALLBACKTYPE_event,
998+
xen_asm_exc_xen_hypervisor_callback) ||
997999
register_callback(CALLBACKTYPE_failsafe, xen_failsafe_callback))
9981000
BUG();
9991001

‎arch/x86/xen/smp_pv.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/pgtable.h>
2727

2828
#include <asm/paravirt.h>
29+
#include <asm/idtentry.h>
2930
#include <asm/desc.h>
3031
#include <asm/cpu.h>
3132

@@ -348,7 +349,7 @@ cpu_initialize_context(unsigned int cpu, struct task_struct *idle)
348349
ctxt->gs_base_kernel = per_cpu_offset(cpu);
349350
#endif
350351
ctxt->event_callback_eip =
351-
(unsigned long)xen_hypervisor_callback;
352+
(unsigned long)xen_asm_exc_xen_hypervisor_callback;
352353
ctxt->failsafe_callback_eip =
353354
(unsigned long)xen_failsafe_callback;
354355
per_cpu(xen_cr3, cpu) = __pa(swapper_pg_dir);

‎arch/x86/xen/xen-asm_32.S‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ xen_iret_start_crit:
9393

9494
/*
9595
* If there's something pending, mask events again so we can
96-
* jump back into xen_hypervisor_callback. Otherwise do not
96+
* jump back into exc_xen_hypervisor_callback. Otherwise do not
9797
* touch XEN_vcpu_info_mask.
9898
*/
9999
jne 1f
@@ -113,7 +113,7 @@ iret_restore_end:
113113
* Events are masked, so jumping out of the critical region is
114114
* OK.
115115
*/
116-
je xen_hypervisor_callback
116+
je xen_asm_exc_xen_hypervisor_callback
117117

118118
1: iret
119119
xen_iret_end_crit:
@@ -127,7 +127,7 @@ SYM_CODE_END(xen_iret)
127127
.globl xen_iret_start_crit, xen_iret_end_crit
128128

129129
/*
130-
* This is called by xen_hypervisor_callback in entry_32.S when it sees
130+
* This is called by xen_asm_exc_xen_hypervisor_callback in entry_32.S when it sees
131131
* that the EIP at the time of interrupt was between
132132
* xen_iret_start_crit and xen_iret_end_crit.
133133
*
@@ -144,15 +144,16 @@ SYM_CODE_END(xen_iret)
144144
* eflags }
145145
* cs } nested exception info
146146
* eip }
147-
* return address : (into xen_hypervisor_callback)
147+
* return address : (into xen_asm_exc_xen_hypervisor_callback)
148148
*
149149
* In order to deliver the nested exception properly, we need to discard the
150150
* nested exception frame such that when we handle the exception, we do it
151151
* in the context of the outer exception rather than starting a new one.
152152
*
153153
* The only caveat is that if the outer eax hasn't been restored yet (i.e.
154154
* it's still on stack), we need to restore its value here.
155-
*/
155+
*/
156+
.pushsection .noinstr.text, "ax"
156157
SYM_CODE_START(xen_iret_crit_fixup)
157158
/*
158159
* Paranoia: Make sure we're really coming from kernel space.
@@ -181,3 +182,4 @@ SYM_CODE_START(xen_iret_crit_fixup)
181182
2:
182183
ret
183184
SYM_CODE_END(xen_iret_crit_fixup)
185+
.popsection

‎arch/x86/xen/xen-asm_64.S‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ xen_pv_trap asm_exc_simd_coprocessor_error
5454
#ifdef CONFIG_IA32_EMULATION
5555
xen_pv_trap entry_INT80_compat
5656
#endif
57-
xen_pv_trap hypervisor_callback
57+
xen_pv_trap asm_exc_xen_hypervisor_callback
5858

5959
__INIT
6060
SYM_CODE_START(xen_early_idt_handler_array)

‎arch/x86/xen/xen-ops.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <xen/xen-ops.h>
99

1010
/* These are code, but not functions. Defined in entry.S */
11-
extern const char xen_hypervisor_callback[];
1211
extern const char xen_failsafe_callback[];
1312

1413
void xen_sysenter_target(void);

‎drivers/xen/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SPDX-License-Identifier: GPL-2.0
22
obj-$(CONFIG_HOTPLUG_CPU) += cpu_hotplug.o
3-
obj-y += grant-table.o features.o balloon.o manage.o preempt.o time.o
3+
obj-y += grant-table.o features.o balloon.o manage.o time.o
44
obj-y += mem-reservation.o
55
obj-y += events/
66
obj-y += xenbus/

0 commit comments

Comments
 (0)