Enable SMEP CPU feature support for XEN itself
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
"Li, Xin" 06/02/11 6:20 AM >>>
(quoted diff) +1
-0+boolean_param("nosmep", disable_smep); struct cpu_dev * cpu_devs[X86_VENDOR_NUM] = {};@@ -222,6 +225,17 @@ c->x86_capability[4] = cap4; }
Quoted 8 lines
Quoted 6 lines
Review line 231
Quoted 4 lines
(quoted diff) +4
-0+static void __cpuinit setup_smep(struct cpuinfo_x86 *c) +{ + if ( cpu_has(c, X86_FEATURE_SMEP) ) { + if( unlikely(disable_smep) ) {... a
__cpuinitfunction?If change
disable_smepto__cpuinitdata, this should be ok.
You would be okay, but as I wrote further down both are really only needed on the BP.
Quoted 14 lines
Quoted 5 lines
Review line 235
Quoted 4 lines
(quoted diff) +4
-0+ setup_clear_cpu_cap(X86_FEATURE_SMEP); + clear_in_cr4(X86_CR4_SMEP); + } else + set_in_cr4(X86_CR4_SMEP);Quoted 8 lines
Anyway, the whole thing is overkill - {set,clear}_in_cr4() write the updated bits to
mmu_cr4_features, and these get loaded on secondary CPUs *before* you have any chance of looking at the CPUID bits. As with everything else, it's assumed that APs don't have less features than the BP, and hence you only need toset_in_cr4()once (on the BP). And then the function can be__init.
Quoted 4 lines
Do you mean?
if (cpu_has(c, X86_FEATURE_SMEP))if( likely(!disable_smep) ){mmu_cr4_features|=X86_CR4_SMEP;
Why?
Quoted 1 line
set_in_cr4(0);
set_in_cr4(X86_CR4_SMEP) does exactly what you need.
Quoted 5 lines
} else
setup_clear_cpu_cap(X86_FEATURE_SMEP);Sounds good ... but the code will be harder to read, as it implicitly set smep? Also where to put
setup_smepthus it's only called in BP?
early_cpu_detect() would seem to be the most logical place, though it doesn't have all the x86_capabilities[] fields set up yet. The BP-only part at the end of identify_cpu() would also be a possible place.
trap_init() would be another possible (and reasonably logical) place.
Quoted 8 lines
Quoted 4 lines
The further down I get the uglier this looks. Can't you simply accumulate the user bit into a separate variable? That way the compiler also doesn't need to keep around all the l[1234]e variables.
At the beginning we did accumulate the user bit into a separate variable. However SMEP faults hardly happen while we keep accumulating user bit no matter it's a spurious fault or not, and even spurious faults are rare I guess.
Remember that we're going through this function for almost every page fault happening in Xen, and also for the majority of those originating from certain pv guests (when they have suppress_spurious_page_faults set).
Also, my comment was to a large part aiming at better legibility of the code you add.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Quoted 3 lines
Quoted 1 line
mmu_cr4_features|=X86_CR4_SMEP;Why?
I replied in another reply to you, but just repeat here:
But it is a good idea to set X86_CR4_SMEP in mmu_cr4_features when SMEP Is available. thus
1) for PV, we can make patch like pv_guest_cr4_to_real_cr4
#define pv_guest_cr4_to_real_cr4(v) \
(((v)->arch.pv_vcpu.ctrlreg[4] \
| (mmu_cr4_features & (X86_CR4_PGE | X86_CR4_PSE | X86_CR4_SMEP)) \
| ((v)->domain->arch.vtsc ? X86_CR4_TSD : 0) \
| ((xsave_enabled(v))? X86_CR4_OSXSAVE : 0)) \
& ~X86_CR4_DE)when set cr4.
2) For HVM, we don't need to explicitly add SMEP when write to HOST_CR4.
Quoted 3 lines
Quoted 1 line
set_in_cr4(0);
set_in_cr4(X86_CR4_SMEP)does exactly what you need.
Yes, but once we have X86_CR4_SMEP in mmu_cr4_features, set_in_cr4(0) does the same thing except looks ugly.
Quoted 2 lines
} else
Quoted 1 line
setup_clear_cpu_cap(X86_FEATURE_SMEP);
This needs to be done on APs too. Thus I think we still need define setup_smep as __cpuinit.
Quoted 11 lines
Quoted 3 lines
At the beginning we did accumulate the user bit into a separate variable. However SMEP faults hardly happen while we keep accumulating user bit no matter it's a spurious fault or not, and even spurious faults are rare I guess.
Remember that we're going through this function for almost every page fault happening in Xen, and also for the majority of those originating from certain pv guests (when they have
suppress_spurious_page_faultsset).Also, my comment was to a large part aiming at better legibility of the code you add.
Yes, for legibility we may change it back.
Thanks!
-Xin
Quoted 6 lines
Quoted 3 lines
} else
Quoted 1 line
setup_clear_cpu_cap(X86_FEATURE_SMEP);This needs to be done on APs too. Thus I think we still need define
setup_smepas__cpuinit.
This only applies to BP only, and actually all feature check is against BP even AP has a corresponding bit set. So your suggestion should work. Thanks!
-Xin
Quoted 5 lines · Xin
Quoted 2 lines
} else
Quoted 1 line
setup_clear_cpu_cap(X86_FEATURE_SMEP);This needs to be done on APs too. Thus I think we still need define
setup_smepas__cpuinit.
The whole point of setup_clear_cpu_cap() is that it only needs to run on the BP and the change gets picked up by every AP automatically as it boots.
-- Keir
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
} else
Quoted 1 line
setup_clear_cpu_cap(X86_FEATURE_SMEP);This needs to be done on APs too. Thus I think we still need define
setup_smepas__cpuinit.The whole point of
setup_clear_cpu_cap()is that it only needs to run on the BP and the change gets picked up by every AP automatically as it boots.
Yeah, and we can't do leaf 7.0 detect in generic_identify. Or will again set SMEP in capabilities, but it should be ok. Thanks!
-Xin
Quoted 5 lines
Quoted 2 lines
The whole point of
setup_clear_cpu_cap()is that it only needs to run on the BP and the change gets picked up by every AP automatically as it boots.Yeah, and we can't do leaf 7.0 detect in
generic_identify. Or will again set SMEP in capabilities, but it should be ok.
I tried to not do leaf 7.0 detect on AP booting code, but system behaves abnormally (not hang, but "su -" never returns, after just revert the change, it runs well).
While at this point I want to focus on SMEP logic, we can make the improvements, to move such initialization to BP code only, later. Also it makes no much sense to only move disable_smep that way, we should move all such variables.
Thanks!
-Xin
Quoted 12 lines · Xin
Quoted 6 lines
Quoted 2 lines
The whole point of
setup_clear_cpu_cap()is that it only needs to run on the BP and the change gets picked up by every AP automatically as it boots.Yeah, and we can't do leaf 7.0 detect in
generic_identify. Or will again set SMEP in
capabilities, but it should be ok.I tried to not do leaf 7.0 detect on AP booting code, but system behaves abnormally
(not hang, but "su -" never returns, after just revert the change, it runs well).
Quoted 5 lines
While at this point I want to focus on SMEP logic, we can make the improvements,
to move such initialization to BP code only, later. Also it makes no much sense to only
movedisable_smepthat way, we should move all such variables.
Just get another rev of the patches out and we'll iterate if necessary.
-- Keir
Quoted 2 lines
Thanks!
-Xin