@@ -61,9 +61,63 @@ DEFINE_PER_CPU(unsigned long, xen_cr3);
6161struct start_info * xen_start_info ;
6262EXPORT_SYMBOL_GPL (xen_start_info );
6363
64- void xen_vcpu_setup (int cpu )
64+ static /* __initdata */ struct shared_info dummy_shared_info ;
65+
66+ /*
67+ * Point at some empty memory to start with. We map the real shared_info
68+ * page as soon as fixmap is up and running.
69+ */
70+ struct shared_info * HYPERVISOR_shared_info = (void * )& dummy_shared_info ;
71+
72+ /*
73+ * Flag to determine whether vcpu info placement is available on all
74+ * VCPUs. We assume it is to start with, and then set it to zero on
75+ * the first failure. This is because it can succeed on some VCPUs
76+ * and not others, since it can involve hypervisor memory allocation,
77+ * or because the guest failed to guarantee all the appropriate
78+ * constraints on all VCPUs (ie buffer can't cross a page boundary).
79+ *
80+ * Note that any particular CPU may be using a placed vcpu structure,
81+ * but we can only optimise if the all are.
82+ *
83+ * 0: not available, 1: available
84+ */
85+ static int have_vcpu_info_placement = 1 ;
86+
87+ static void __init xen_vcpu_setup (int cpu )
6588{
89+ struct vcpu_register_vcpu_info info ;
90+ int err ;
91+ struct vcpu_info * vcpup ;
92+
6693 per_cpu (xen_vcpu , cpu ) = & HYPERVISOR_shared_info -> vcpu_info [cpu ];
94+
95+ if (!have_vcpu_info_placement )
96+ return ; /* already tested, not available */
97+
98+ vcpup = & per_cpu (xen_vcpu_info , cpu );
99+
100+ info .mfn = virt_to_mfn (vcpup );
101+ info .offset = offset_in_page (vcpup );
102+
103+ printk (KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %x, offset %d\n" ,
104+ cpu , vcpup , info .mfn , info .offset );
105+
106+ /* Check to see if the hypervisor will put the vcpu_info
107+ structure where we want it, which allows direct access via
108+ a percpu-variable. */
109+ err = HYPERVISOR_vcpu_op (VCPUOP_register_vcpu_info , cpu , & info );
110+
111+ if (err ) {
112+ printk (KERN_DEBUG "register_vcpu_info failed: err=%d\n" , err );
113+ have_vcpu_info_placement = 0 ;
114+ } else {
115+ /* This cpu is using the registered vcpu info, even if
116+ later ones fail to. */
117+ per_cpu (xen_vcpu , cpu ) = vcpup ;
118+ printk (KERN_DEBUG "cpu %d using vcpu_info at %p\n" ,
119+ cpu , vcpup );
120+ }
67121}
68122
69123static void __init xen_banner (void )
@@ -123,6 +177,20 @@ static unsigned long xen_save_fl(void)
123177 return (- flags ) & X86_EFLAGS_IF ;
124178}
125179
180+ static unsigned long xen_save_fl_direct (void )
181+ {
182+ unsigned long flags ;
183+
184+ /* flag has opposite sense of mask */
185+ flags = !x86_read_percpu (xen_vcpu_info .evtchn_upcall_mask );
186+
187+ /* convert to IF type flag
188+ -0 -> 0x00000000
189+ -1 -> 0xffffffff
190+ */
191+ return (- flags ) & X86_EFLAGS_IF ;
192+ }
193+
126194static void xen_restore_fl (unsigned long flags )
127195{
128196 struct vcpu_info * vcpu ;
@@ -149,6 +217,25 @@ static void xen_restore_fl(unsigned long flags)
149217 }
150218}
151219
220+ static void xen_restore_fl_direct (unsigned long flags )
221+ {
222+ /* convert from IF type flag */
223+ flags = !(flags & X86_EFLAGS_IF );
224+
225+ /* This is an atomic update, so no need to worry about
226+ preemption. */
227+ x86_write_percpu (xen_vcpu_info .evtchn_upcall_mask , flags );
228+
229+ /* If we get preempted here, then any pending event will be
230+ handled anyway. */
231+
232+ if (flags == 0 ) {
233+ barrier (); /* unmask then check (avoid races) */
234+ if (unlikely (x86_read_percpu (xen_vcpu_info .evtchn_upcall_pending )))
235+ force_evtchn_callback ();
236+ }
237+ }
238+
152239static void xen_irq_disable (void )
153240{
154241 /* There's a one instruction preempt window here. We need to
@@ -159,6 +246,12 @@ static void xen_irq_disable(void)
159246 preempt_enable_no_resched ();
160247}
161248
249+ static void xen_irq_disable_direct (void )
250+ {
251+ /* Atomic update, so preemption not a concern. */
252+ x86_write_percpu (xen_vcpu_info .evtchn_upcall_mask , 1 );
253+ }
254+
162255static void xen_irq_enable (void )
163256{
164257 struct vcpu_info * vcpu ;
@@ -179,6 +272,19 @@ static void xen_irq_enable(void)
179272 force_evtchn_callback ();
180273}
181274
275+ static void xen_irq_enable_direct (void )
276+ {
277+ /* Atomic update, so preemption not a concern. */
278+ x86_write_percpu (xen_vcpu_info .evtchn_upcall_mask , 0 );
279+
280+ /* Doesn't matter if we get preempted here, because any
281+ pending event will get dealt with anyway. */
282+
283+ barrier (); /* unmask then check (avoid races) */
284+ if (unlikely (x86_read_percpu (xen_vcpu_info .evtchn_upcall_pending )))
285+ force_evtchn_callback ();
286+ }
287+
182288static void xen_safe_halt (void )
183289{
184290 /* Blocking includes an implicit local_irq_enable(). */
@@ -551,11 +657,21 @@ static void xen_flush_tlb_others(const cpumask_t *cpus, struct mm_struct *mm,
551657 xen_mc_issue (PARAVIRT_LAZY_MMU );
552658}
553659
660+ static void xen_write_cr2 (unsigned long cr2 )
661+ {
662+ x86_read_percpu (xen_vcpu )-> arch .cr2 = cr2 ;
663+ }
664+
554665static unsigned long xen_read_cr2 (void )
555666{
556667 return x86_read_percpu (xen_vcpu )-> arch .cr2 ;
557668}
558669
670+ static unsigned long xen_read_cr2_direct (void )
671+ {
672+ return x86_read_percpu (xen_vcpu_info .arch .cr2 );
673+ }
674+
559675static void xen_write_cr4 (unsigned long cr4 )
560676{
561677 /* never allow TSC to be disabled */
@@ -753,8 +869,27 @@ static __init void xen_pagetable_setup_done(pgd_t *base)
753869 if (HYPERVISOR_mmuext_op (& op , 1 , NULL , DOMID_SELF ))
754870 BUG ();
755871 }
872+ }
756873
757- xen_vcpu_setup (smp_processor_id ());
874+ /* This is called once we have the cpu_possible_map */
875+ void __init xen_setup_vcpu_info_placement (void )
876+ {
877+ int cpu ;
878+
879+ for_each_possible_cpu (cpu )
880+ xen_vcpu_setup (cpu );
881+
882+ /* xen_vcpu_setup managed to place the vcpu_info within the
883+ percpu area for all cpus, so make use of it */
884+ if (have_vcpu_info_placement ) {
885+ printk (KERN_INFO "Xen: using vcpu_info placement\n" );
886+
887+ paravirt_ops .save_fl = xen_save_fl_direct ;
888+ paravirt_ops .restore_fl = xen_restore_fl_direct ;
889+ paravirt_ops .irq_disable = xen_irq_disable_direct ;
890+ paravirt_ops .irq_enable = xen_irq_enable_direct ;
891+ paravirt_ops .read_cr2 = xen_read_cr2_direct ;
892+ }
758893}
759894
760895static const struct paravirt_ops xen_paravirt_ops __initdata = {
@@ -788,7 +923,7 @@ static const struct paravirt_ops xen_paravirt_ops __initdata = {
788923 .write_cr0 = native_write_cr0 ,
789924
790925 .read_cr2 = xen_read_cr2 ,
791- .write_cr2 = native_write_cr2 ,
926+ .write_cr2 = xen_write_cr2 ,
792927
793928 .read_cr3 = xen_read_cr3 ,
794929 .write_cr3 = xen_write_cr3 ,
@@ -974,7 +1109,16 @@ asmlinkage void __init xen_start_kernel(void)
9741109 /* keep using Xen gdt for now; no urgent need to change it */
9751110
9761111 x86_write_percpu (xen_cr3 , __pa (pgd ));
977- xen_vcpu_setup (0 );
1112+
1113+ #ifdef CONFIG_SMP
1114+ /* Don't do the full vcpu_info placement stuff until we have a
1115+ possible map. */
1116+ per_cpu (xen_vcpu , 0 ) = & HYPERVISOR_shared_info -> vcpu_info [0 ];
1117+ #else
1118+ /* May as well do it now, since there's no good time to call
1119+ it later on UP. */
1120+ xen_setup_vcpu_info_placement ();
1121+ #endif
9781122
9791123 paravirt_ops .kernel_rpl = 1 ;
9801124 if (xen_feature (XENFEAT_supervisor_mode_kernel ))
0 commit comments