Skip to content

Commit 051a18a

Browse files
authored
Merge pull request #483 from rust-osdev/arch-specific-instructions
Only enable instructions on `x86_64`
2 parents de3188d + 91f9810 commit 051a18a

File tree

13 files changed

+73
-48
lines changed

13 files changed

+73
-48
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
strategy:
4545
fail-fast: false
4646
matrix:
47-
platform: [ubuntu-latest, macos-latest, windows-latest]
47+
platform: [ubuntu-latest, macos-12, macos-latest, windows-latest]
4848

4949
runs-on: ${{ matrix.platform }}
5050
timeout-minutes: 15

src/instructions/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![cfg(feature = "instructions")]
1+
#![cfg(all(feature = "instructions", target_arch = "x86_64"))]
22

33
//! Special x86_64 instructions.
44

src/registers/control.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ bitflags! {
159159
}
160160
}
161161

162-
#[cfg(feature = "instructions")]
162+
#[cfg(all(feature = "instructions", target_arch = "x86_64"))]
163163
mod x86_64 {
164164
use super::*;
165165
use crate::{

src/registers/debug.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Functions to read and write debug registers.
22
3-
#[cfg(feature = "instructions")]
3+
#[cfg(all(feature = "instructions", target_arch = "x86_64"))]
44
use core::arch::asm;
55
use core::ops::Range;
66

@@ -15,11 +15,11 @@ pub trait DebugAddressRegister {
1515
const NUM: DebugAddressRegisterNumber;
1616

1717
/// Reads the current breakpoint address.
18-
#[cfg(feature = "instructions")]
18+
#[cfg(all(feature = "instructions", target_arch = "x86_64"))]
1919
fn read() -> u64;
2020

2121
/// Writes the provided breakpoint address.
22-
#[cfg(feature = "instructions")]
22+
#[cfg(all(feature = "instructions", target_arch = "x86_64"))]
2323
fn write(addr: u64);
2424
}
2525

@@ -34,7 +34,7 @@ macro_rules! debug_address_register {
3434
impl DebugAddressRegister for $Dr {
3535
const NUM: DebugAddressRegisterNumber = DebugAddressRegisterNumber::$Dr;
3636

37-
#[cfg(feature = "instructions")]
37+
#[cfg(all(feature = "instructions", target_arch = "x86_64"))]
3838
#[inline]
3939
fn read() -> u64 {
4040
let addr;
@@ -44,7 +44,7 @@ macro_rules! debug_address_register {
4444
addr
4545
}
4646

47-
#[cfg(feature = "instructions")]
47+
#[cfg(all(feature = "instructions", target_arch = "x86_64"))]
4848
#[inline]
4949
fn write(addr: u64) {
5050
unsafe {
@@ -437,7 +437,7 @@ impl Dr7Value {
437437
#[derive(Debug)]
438438
pub struct Dr7;
439439

440-
#[cfg(feature = "instructions")]
440+
#[cfg(all(feature = "instructions", target_arch = "x86_64"))]
441441
mod x86_64 {
442442
use super::*;
443443

src/registers/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ pub mod rflags;
88
pub mod segmentation;
99
pub mod xcontrol;
1010

11-
#[cfg(feature = "instructions")]
11+
#[cfg(all(feature = "instructions", target_arch = "x86_64"))]
1212
pub use crate::instructions::read_rip;

src/registers/model_specific.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use bitflags::bitflags;
66
use crate::registers::segmentation::{FS, GS};
77

88
/// A model specific register.
9-
#[cfg_attr(not(feature = "instructions"), allow(dead_code))] // FIXME
9+
#[cfg_attr(
10+
not(all(feature = "instructions", target_arch = "x86_64")),
11+
allow(dead_code)
12+
)] // FIXME
1013
#[derive(Debug)]
1114
pub struct Msr(u32);
1215

@@ -29,7 +32,7 @@ pub struct FsBase;
2932
/// [GS].Base Model Specific Register.
3033
///
3134
#[cfg_attr(
32-
feature = "instructions",
35+
all(feature = "instructions", target_arch = "x86_64"),
3336
doc = "[`GS::swap`] swaps this register with [`KernelGsBase`]."
3437
)]
3538
#[derive(Debug)]
@@ -38,7 +41,7 @@ pub struct GsBase;
3841
/// KernelGsBase Model Specific Register.
3942
///
4043
#[cfg_attr(
41-
feature = "instructions",
44+
all(feature = "instructions", target_arch = "x86_64"),
4245
doc = "[`GS::swap`] swaps this register with [`GsBase`]."
4346
)]
4447
#[derive(Debug)]
@@ -158,7 +161,7 @@ bitflags! {
158161
}
159162
}
160163

161-
#[cfg(feature = "instructions")]
164+
#[cfg(all(feature = "instructions", target_arch = "x86_64"))]
162165
mod x86_64 {
163166
use super::*;
164167
use crate::addr::VirtAddr;

src/registers/mxcsr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Functions to read and write MXCSR register.
22
3-
#[cfg(feature = "instructions")]
3+
#[cfg(all(feature = "instructions", target_arch = "x86_64"))]
44
pub use self::x86_64::*;
55

66
use bitflags::bitflags;
@@ -60,7 +60,7 @@ impl Default for MxCsr {
6060
}
6161
}
6262

63-
#[cfg(feature = "instructions")]
63+
#[cfg(all(feature = "instructions", target_arch = "x86_64"))]
6464
mod x86_64 {
6565
use super::*;
6666
use core::arch::asm;

src/registers/rflags.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Processor state stored in the RFLAGS register.
22
3-
#[cfg(feature = "instructions")]
3+
#[cfg(all(feature = "instructions", target_arch = "x86_64"))]
44
pub use self::x86_64::*;
55

66
use bitflags::bitflags;
@@ -64,7 +64,7 @@ bitflags! {
6464
}
6565
}
6666

67-
#[cfg(feature = "instructions")]
67+
#[cfg(all(feature = "instructions", target_arch = "x86_64"))]
6868
mod x86_64 {
6969
use super::*;
7070
use core::arch::asm;

src/registers/xcontrol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ bitflags! {
5050
}
5151
}
5252

53-
#[cfg(feature = "instructions")]
53+
#[cfg(all(feature = "instructions", target_arch = "x86_64"))]
5454
mod x86_64 {
5555
use super::*;
5656
use core::arch::asm;

src/structures/gdt.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use core::fmt;
1010
#[cfg(doc)]
1111
use crate::registers::segmentation::{Segment, CS, SS};
1212

13-
#[cfg(feature = "instructions")]
13+
#[cfg(all(feature = "instructions", target_arch = "x86_64"))]
1414
use core::sync::atomic::{AtomicU64 as EntryValue, Ordering};
15-
#[cfg(not(feature = "instructions"))]
15+
#[cfg(not(all(feature = "instructions", target_arch = "x86_64")))]
1616
use u64 as EntryValue;
1717

1818
/// 8-byte entry in a descriptor table.
@@ -28,7 +28,7 @@ pub struct Entry(EntryValue);
2828
impl Entry {
2929
// Create a new Entry from a raw value.
3030
const fn new(raw: u64) -> Self {
31-
#[cfg(feature = "instructions")]
31+
#[cfg(all(feature = "instructions", target_arch = "x86_64"))]
3232
let raw = EntryValue::new(raw);
3333
Self(raw)
3434
}
@@ -37,9 +37,9 @@ impl Entry {
3737
/// bits may correspond to those in [`DescriptorFlags`].
3838
pub fn raw(&self) -> u64 {
3939
// TODO: Make this const fn when AtomicU64::load is const.
40-
#[cfg(feature = "instructions")]
40+
#[cfg(all(feature = "instructions", target_arch = "x86_64"))]
4141
let raw = self.0.load(Ordering::SeqCst);
42-
#[cfg(not(feature = "instructions"))]
42+
#[cfg(not(all(feature = "instructions", target_arch = "x86_64")))]
4343
let raw = self.0;
4444
raw
4545
}
@@ -153,7 +153,10 @@ impl<const MAX: usize> GlobalDescriptorTable<MAX> {
153153
/// * the provided slice has more than `MAX` entries
154154
/// * the provided slice is empty
155155
/// * the first entry is not zero
156-
#[cfg_attr(not(feature = "instructions"), allow(rustdoc::broken_intra_doc_links))]
156+
#[cfg_attr(
157+
not(all(feature = "instructions", target_arch = "x86_64")),
158+
allow(rustdoc::broken_intra_doc_links)
159+
)]
157160
#[inline]
158161
pub const fn from_raw_entries(slice: &[u64]) -> Self {
159162
let len = slice.len();
@@ -215,7 +218,7 @@ impl<const MAX: usize> GlobalDescriptorTable<MAX> {
215218
/// segment registers; you **must** (re)load them yourself using [the appropriate
216219
/// functions](crate::instructions::segmentation):
217220
/// [`SS::set_reg()`] and [`CS::set_reg()`].
218-
#[cfg(feature = "instructions")]
221+
#[cfg(all(feature = "instructions", target_arch = "x86_64"))]
219222
#[inline]
220223
pub fn load(&'static self) {
221224
// SAFETY: static lifetime ensures no modification after loading.
@@ -233,7 +236,7 @@ impl<const MAX: usize> GlobalDescriptorTable<MAX> {
233236
/// this means its up to the user to ensure that there will be no modifications
234237
/// after loading and that the GDT will live for as long as it's loaded.
235238
///
236-
#[cfg(feature = "instructions")]
239+
#[cfg(all(feature = "instructions", target_arch = "x86_64"))]
237240
#[inline]
238241
pub unsafe fn load_unsafe(&self) {
239242
use crate::instructions::tables::lgdt;
@@ -261,7 +264,7 @@ impl<const MAX: usize> GlobalDescriptorTable<MAX> {
261264

262265
/// Creates the descriptor pointer for this table. This pointer can only be
263266
/// safely used if the table is never modified or destroyed while in use.
264-
#[cfg(feature = "instructions")]
267+
#[cfg(all(feature = "instructions", target_arch = "x86_64"))]
265268
fn pointer(&self) -> super::DescriptorTablePointer {
266269
super::DescriptorTablePointer {
267270
base: crate::VirtAddr::new(self.table.as_ptr() as u64),

0 commit comments

Comments
 (0)