Crate privilege_level

Source
Expand description

This library provides a simple, architecture independent way to detect the current privilege level (CPL).

§Example

use privilege_level::{privilege_level, PrivilegeLevel};

match privilege_level() {
    PrivilegeLevel::Hypervisor => println!("Currently a hypervisor"),
    PrivilegeLevel::Kernel => println!("Currently in kernel space"),
    PrivilegeLevel::Driver => println!("Currently in a driver"),
    PrivilegeLevel::User => println!("Currently in user space"),
}

§Details

The main entrypoint into this crate is through the privilege_level function. That will use the architecture dependent code necessary to determine the current privilege level then converts it into the common API.

The common API (PrivilegeLevel) is meant to be usable on any platform. It contains generic privilege levels that are common on most architectures. However, it’s not always possible to include support for every architecture. As a compromise, depending on the target architecture, certain functions will be made available that return architecture-specific structures.

If you only want the raw privilege level number rather than a symbolic structure, you can also use the raw_privilege_level function. It uses architecture specific code to read the privilege level, converts it into a u16, then returns it. It’s important to note that it doesn’t do anything to make the output common in any way!

Enums§

PrivilegeLevel
Represents a generic CPU privilege level.
x86_64PrivilegeLevelx86-64
Represents a protection ring level.

Functions§

privilege_level
Gets the current privilege level.
raw_privilege_level
Gets the current privilege level as a raw number.
x86_64_privilege_levelx86-64
Gets the current privilege level for x86_64 systems.