Skip to content

Stabilize keylocker #140766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

sayantn
Copy link
Contributor

@sayantn sayantn commented May 7, 2025

This PR stabilizes the feature flag keylocker_x86 (tracking issue #134813).

Public API

The 2 x86 target features kl and widekl, and the associated intrinsics in stdarch.

These target features are very specialized, and are only used to signal the presence of the corresponding CPU instruction. They don't have any nontrivial interaction with the ABI (contrary to something like AVX), and serve the only purpose of enabling 11 stdarch intrinsics, all of which have been implemented and propagated to rustc via a stdarch submodule update.

Also, these were added way back in LLVM12, and as the minimum LLVM required for rustc is LLVM19, we are safe in that front too!

Associated PRs

As all of the required tasks have been done (adding the target features to rustc, implementing their runtime detection in std_detect and implementing the associated intrinsics in core_arch), these target features can be stabilized now.

cc @rust-lang/lang
cc @rust-lang/libs-api for the intrinsics and runtime detection

I don't think anyone else worked on this feature, so no one else to ping, maybe cc @Amanieu. I will send the reference pr soon.

Do not merge now, contains a temporary fix to make the tests pass until the stdarch stabilization PR is merged.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. A-target-feature Area: Enabling/disabling target features like AVX, Neon, etc. I-lang-nominated Nominated for discussion during a lang team meeting. O-x86_32 Target: x86 processors, 32 bit (like i686-*) (IA-32) O-x86_64 Target: x86-64 processors (like x86_64-*) (also known as amd64 and x64) T-lang Relevant to the language team, which will review and decide on the PR/issue. labels May 7, 2025
@rustbot
Copy link
Collaborator

rustbot commented May 7, 2025

⚠️ Warning ⚠️

  • Some commits in this PR modify submodules.

@rustbot rustbot removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 7, 2025
@traviscross
Copy link
Contributor

traviscross commented May 13, 2025

Stabilizing target features are somewhat minimal as far as stabilizations go, but still, this is going to need something more in the way of a stabilization report in the description of this PR. We need some narrative about what we're stabilizing here and a review of the evidence about why it's OK for us to stabilize this now. Have a look at our new template for this in rust-lang/rustc-dev-guide#2219.

Please think too about who else in the Project has worked on this or related things and who might therefore be interested in this, and please ping those people here.

This will also need a PR to the Reference documenting the change, and that should be linked from the stabilization report as well.

@rustbot labels +S-waiting-on-documentation

Please renominate when the stabilization report is available.

@rustbot rustbot added the S-waiting-on-documentation Status: Waiting on approved PRs to documentation before merging label May 13, 2025
@traviscross traviscross added I-lang-radar Items that are on lang's radar and will need eventual work or consideration. and removed I-lang-nominated Nominated for discussion during a lang team meeting. labels May 13, 2025
@sayantn
Copy link
Contributor Author

sayantn commented May 15, 2025

@rustbot label I-lang-nominated
cc @traviscross

@rustbot rustbot added the I-lang-nominated Nominated for discussion during a lang team meeting. label May 15, 2025
@traviscross
Copy link
Contributor

url = https://github.com/rust-lang/stdarch.git
url = https://github.com/sayantn/stdarch.git
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just leaving a note so we remember to pull out this "do not merge" commit.

@@ -257,6 +257,8 @@ declare_features! (
/// Allows some increased flexibility in the name resolution rules,
/// especially around globs and shadowing (RFC 1560).
(accepted, item_like_imports, "1.15.0", Some(35120)),
// Allows using the `kl` and `widekl` target features and the associated intrinsics
(accepted, keylocker_x86, "1.86.0", Some(134813)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(accepted, keylocker_x86, "1.86.0", Some(134813)),
(accepted, keylocker_x86, "CURRENT_RUSTC_VERSION", Some(134813)),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, forgot changing it, will update

@traviscross
Copy link
Contributor

traviscross commented May 15, 2025

@sayantn, are these kl and widekl names semi-standard elsewhere, or how did we pick these? What does the Linux kernel show in /proc/cpuinfo?

@sayantn
Copy link
Contributor Author

sayantn commented May 15, 2025

These names are used by both gcc and llvm, but Intel uses the names KEYLOCKER and KEYLOCKER_WIDE, so we can do some bikeshedding here, although I would say following the convention of the C compilers would be better (this has been the common convention in Rust for quite some time, another example off the top of my head will be avx512ifma, intel uses AVX512_IFMA52)

@RalfJung
Copy link
Member

They don't have any nontrivial interaction with the ABI

Beyond ABI, is there any possibility for trouble when mixing code built with and without these target features?

@sayantn
Copy link
Contributor Author

sayantn commented May 15, 2025

They don't have any nontrivial interaction with the ABI

Beyond ABI, is there any possibility for trouble when mixing code built with and without these target features?

The only job of these target features is enabling the use of those 11 intrinsics, or equivalently, 11 instructions. They don't affect normal codegen in any way, so I don't think it will be problematic mixing code built with and without them.

The only nontrivial thing I can think of about these is that the encodekey instructions don't have any output registers, they just clobber xmm{0..=7} (or xmm{0..=5} for encodekey128). But llvm knows about this, and generates appropriate instructions for it, so it should have no problems.

@traviscross
Copy link
Contributor

traviscross commented May 15, 2025

What does the Linux kernel show in /proc/cpuinfo?

It looks like the kernel uses aes_keylocker and aes_keylocker_wide. From here:

# Leaf 19H
# Intel Key Locker enumeration

      0x19,         0,  eax,       0,    kl_cpl0_only           , CPL0-only key Locker restriction supported
      0x19,         0,  eax,       1,    kl_no_encrypt          , No-encrypt key locker restriction supported
      0x19,         0,  eax,       2,    kl_no_decrypt          , No-decrypt key locker restriction supported
      0x19,         0,  ebx,       0,    aes_keylocker          , AES key locker instructions supported
      0x19,         0,  ebx,       2,    aes_keylocker_wide     , AES wide key locker instructions supported
      0x19,         0,  ebx,       4,    kl_msr_iwkey           , Key locker MSRs and IWKEY backups supported
      0x19,         0,  ecx,       0,    loadiwkey_no_backup    , LOADIWKEY NoBackup parameter supported
      0x19,         0,  ecx,       1,    iwkey_rand             , IWKEY randomization (KeySource encoding 1) supported

This follows the nomenclature from a project for exhaustively documenting and tracking this CPUID data:

https://x86-cpuid.org/

In comparing it with the features that we support, most but not all of these match.

Here's the LLVM list:

https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/X86/X86.td

These names are used by both gcc and llvm, but Intel uses the names KEYLOCKER and KEYLOCKER_WIDE, so we can do some bikeshedding here, although I would say following the convention of the C compilers would be better

Yes, I suppose I'm happy enough to follow LLVM here.

@traviscross
Copy link
Contributor

This sounds OK to me, so let's propose to do it.

@rfcbot fcp merge

@rfcbot
Copy link
Collaborator

rfcbot commented May 15, 2025

Team member @traviscross has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels May 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-target-feature Area: Enabling/disabling target features like AVX, Neon, etc. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. I-lang-nominated Nominated for discussion during a lang team meeting. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. O-x86_32 Target: x86 processors, 32 bit (like i686-*) (IA-32) O-x86_64 Target: x86-64 processors (like x86_64-*) (also known as amd64 and x64) proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. S-waiting-on-documentation Status: Waiting on approved PRs to documentation before merging S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants