about summary refs log tree commit diff
path: root/library/std_detect/src/detect/os/linux/s390x.rs
blob: 9b53f526d619831790bf2ce66143396bff687c98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//! Run-time feature detection for s390x on Linux.

use super::auxvec;
use crate::detect::{Feature, bit, cache};

/// Try to read the features from the auxiliary vector
pub(crate) fn detect_features() -> cache::Initializer {
    let opt_hwcap: Option<AtHwcap> = auxvec::auxv().ok().map(Into::into);
    let facilities = ExtendedFacilityList::new();
    cache(opt_hwcap, facilities)
}

#[derive(Debug, Default, PartialEq)]
struct AtHwcap {
    esan3: bool,
    zarch: bool,
    stfle: bool,
    msa: bool,
    ldisp: bool,
    eimm: bool,
    dfp: bool,
    hpage: bool,
    etf3eh: bool,
    high_gprs: bool,
    te: bool,
    vxrs: bool,
    vxrs_bcd: bool,
    vxrs_ext: bool,
    gs: bool,
    vxrs_ext2: bool,
    vxrs_pde: bool,
    sort: bool,
    dflt: bool,
    vxrs_pde2: bool,
    nnpa: bool,
    pci_mio: bool,
    sie: bool,
}

impl From<auxvec::AuxVec> for AtHwcap {
    /// Reads AtHwcap from the auxiliary vector.
    fn from(auxv: auxvec::AuxVec) -> Self {
        AtHwcap {
            esan3: bit::test(auxv.hwcap, 0),
            zarch: bit::test(auxv.hwcap, 1),
            stfle: bit::test(auxv.hwcap, 2),
            msa: bit::test(auxv.hwcap, 3),
            ldisp: bit::test(auxv.hwcap, 4),
            eimm: bit::test(auxv.hwcap, 5),
            dfp: bit::test(auxv.hwcap, 6),
            hpage: bit::test(auxv.hwcap, 7),
            etf3eh: bit::test(auxv.hwcap, 8),
            high_gprs: bit::test(auxv.hwcap, 9),
            te: bit::test(auxv.hwcap, 10),
            vxrs: bit::test(auxv.hwcap, 11),
            vxrs_bcd: bit::test(auxv.hwcap, 12),
            vxrs_ext: bit::test(auxv.hwcap, 13),
            gs: bit::test(auxv.hwcap, 14),
            vxrs_ext2: bit::test(auxv.hwcap, 15),
            vxrs_pde: bit::test(auxv.hwcap, 16),
            sort: bit::test(auxv.hwcap, 17),
            dflt: bit::test(auxv.hwcap, 18),
            vxrs_pde2: bit::test(auxv.hwcap, 19),
            nnpa: bit::test(auxv.hwcap, 20),
            pci_mio: bit::test(auxv.hwcap, 21),
            sie: bit::test(auxv.hwcap, 22),
        }
    }
}

struct ExtendedFacilityList([u64; 4]);

impl ExtendedFacilityList {
    fn new() -> Self {
        let mut result: [u64; 4] = [0; 4];
        // SAFETY: rust/llvm only support s390x version with the `stfle` instruction.
        unsafe {
            core::arch::asm!(
                // equivalently ".insn s, 0xb2b00000, 0({1})",
                "stfle 0({})",
                in(reg_addr) result.as_mut_ptr() ,
                inout("r0") result.len() as u64 - 1 => _,
                options(nostack)
            );
        }
        Self(result)
    }

    const fn get_bit(&self, n: usize) -> bool {
        // NOTE: bits are numbered from the left.
        self.0[n / 64] & (1 << (63 - (n % 64))) != 0
    }
}

/// Initializes the cache from the feature bits.
///
/// These values are part of the platform-specific [asm/elf.h][kernel], and are a selection of the
/// fields found in the [Facility Indications].
///
/// [Facility Indications]: https://www.ibm.com/support/pages/sites/default/files/2021-05/SA22-7871-10.pdf#page=63
/// [kernel]: https://github.com/torvalds/linux/blob/b62cef9a5c673f1b8083159f5dc03c1c5daced2f/arch/s390/include/asm/elf.h#L129
fn cache(hwcap: Option<AtHwcap>, facilities: ExtendedFacilityList) -> cache::Initializer {
    let mut value = cache::Initializer::default();

    {
        let mut enable_if_set = |bit_index, f| {
            if facilities.get_bit(bit_index) {
                value.set(f as u32);
            }
        };

        // We use HWCAP for `vector` because it requires both hardware and kernel support.
        if let Some(AtHwcap { vxrs: true, .. }) = hwcap {
            // vector and related

            enable_if_set(129, Feature::vector);

            enable_if_set(135, Feature::vector_enhancements_1);
            enable_if_set(148, Feature::vector_enhancements_2);
            enable_if_set(198, Feature::vector_enhancements_3);

            enable_if_set(134, Feature::vector_packed_decimal);
            enable_if_set(152, Feature::vector_packed_decimal_enhancement);
            enable_if_set(192, Feature::vector_packed_decimal_enhancement_2);
            enable_if_set(199, Feature::vector_packed_decimal_enhancement_3);

            enable_if_set(165, Feature::nnp_assist);
        }

        // others

        enable_if_set(76, Feature::message_security_assist_extension3);
        enable_if_set(77, Feature::message_security_assist_extension4);
        enable_if_set(57, Feature::message_security_assist_extension5);
        enable_if_set(146, Feature::message_security_assist_extension8);
        enable_if_set(155, Feature::message_security_assist_extension9);
        enable_if_set(86, Feature::message_security_assist_extension12);

        enable_if_set(58, Feature::miscellaneous_extensions_2);
        enable_if_set(61, Feature::miscellaneous_extensions_3);
        enable_if_set(84, Feature::miscellaneous_extensions_4);

        enable_if_set(45, Feature::high_word);
        enable_if_set(73, Feature::transactional_execution);
        enable_if_set(133, Feature::guarded_storage);
        enable_if_set(150, Feature::enhanced_sort);
        enable_if_set(151, Feature::deflate_conversion);
        enable_if_set(201, Feature::concurrent_functions);
    }

    value
}