File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
src/aero_kernel/src/arch/x86_64 Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 17
17
* along with Aero. If not, see <https://www.gnu.org/licenses/>.
18
18
*/
19
19
20
+ use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
21
+
20
22
use crate :: mem:: paging:: { PhysAddr , PhysFrame , VirtAddr } ;
21
23
22
24
bitflags:: bitflags! {
@@ -273,3 +275,34 @@ pub fn read_cr2() -> VirtAddr {
273
275
VirtAddr :: new ( value)
274
276
}
275
277
}
278
+
279
+ /// Returns true if Supervisor Mode Access Prevention (SMAP) is supported by the CPU and is enabled in Cr4.
280
+ #[ inline]
281
+ pub fn smap_enabled ( ) -> bool {
282
+ read_cr4 ( ) . contains ( Cr4Flags :: SUPERVISOR_MODE_ACCESS_PREVENTION )
283
+ }
284
+
285
+ #[ inline]
286
+ pub fn with_userspace_access < F , R > ( f : F ) -> R
287
+ where
288
+ F : FnOnce ( ) -> R ,
289
+ {
290
+ static REF_COUNT : AtomicUsize = AtomicUsize :: new ( 0 ) ;
291
+ if smap_enabled ( ) {
292
+ unsafe {
293
+ asm ! ( "stac" ) ;
294
+ }
295
+ REF_COUNT . fetch_add ( 1 , Ordering :: Acquire ) ;
296
+ } ;
297
+ let r = f ( ) ;
298
+ if smap_enabled ( ) {
299
+ REF_COUNT . fetch_sub ( 1 , Ordering :: Release ) ;
300
+
301
+ if REF_COUNT . load ( Ordering :: Relaxed ) == 0 {
302
+ unsafe {
303
+ asm ! ( "clac" ) ;
304
+ }
305
+ }
306
+ } ;
307
+ r
308
+ }
You can’t perform that action at this time.
0 commit comments