Debian Codegraph @

third_party/rust/minidump-writer/src/mac/streams/system_info.rs

raw
1 use super::*;
2 use crate::minidump_format::*;
3
4 /// Retrieve the OS version information.
5 ///
6 /// Note that this only works on 10.13.4+, but that release is over 4 years old
7 /// and 1 version behind the latest unsupported release at the time of this writing
8 ///
9 /// Note that Breakpad/Crashpad use a private API in CoreFoundation to do this
10 /// via _CFCopySystemVersionDictionary->_kCFSystemVersionProductVersionKey
11 fn os_version() -> (u32, u32, u32) {
12 let vers = mach::sysctl_string(b"kern.osproductversion\0");
13
14 let inner = || {
15 let mut it = vers.split('.');
16
17 let major: u32 = it.next()?.parse().ok()?;
18 let minor: u32 = it.next()?.parse().ok()?;
19 let patch: u32 = it.next().and_then(|p| p.parse().ok()).unwrap_or_default();
20
21 Some((major, minor, patch))
22 };
23
24 inner().unwrap_or_default()
25 }
26
27 /// Retrieves the OS build version.
28 ///
29 /// Note that Breakpad/Crashpad use a private API in CoreFoundation to do this
30 /// via _CFCopySystemVersionDictionary->_kCFSystemVersionBuildVersionKey. I have
31 /// no idea how long this has been the case, but the same information can be
32 /// retrieved via `sysctlbyname` via the `kern.osversion` key as seen by comparing
33 /// its value versus the output of the `sw_vers -buildVersion` command
34 #[inline]
35 fn build_version() -> String {
36 mach::sysctl_string(b"kern.osversion\0")
37 }
38
39 /// Retrieves more detailed information on the cpu.
40 ///
41 /// Note that this function is only implemented on `x86_64` as Apple doesn't
42 /// expose similar info on `aarch64` (or at least, not via the same mechanisms)
43 fn read_cpu_info(cpu: &mut format::CPU_INFORMATION) {
44 if !cfg!(target_arch = "x86_64") {
45 return;
46 }
47
48 let mut md_feats: u64 = 1 << 2 /*PF_COMPARE_EXCHANGE_DOUBLE*/;
49 let features: u64 = mach::sysctl_by_name(b"machdep.cpu.feature_bits\0");
50
51 // Map the cpuid feature to its equivalent minidump cpu feature.
52 // See https://en.wikipedia.org/wiki/CPUID for where the values for the
53 // various cpuid bits come from, and
54 // https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-isprocessorfeaturepresent
55 // for where the bits for the the minidump come from
56 macro_rules! map_feature {
57 ($set:expr, $cpuid_bit:expr, $md_bit:expr) => {
58 if $set & (1 << $cpuid_bit) != 0 {
59 md_feats |= 1 << $md_bit;
60 }
61 };
62 }
63
64 map_feature!(
65 features, 4, /*TSC*/
66 8 /* PF_RDTSC_INSTRUCTION_AVAILABLE */
67 );
68 map_feature!(features, 6 /*PAE*/, 9 /* PF_PAE_ENABLED */);
69 map_feature!(
70 features, 23, /*MMX*/
71 3 /* PF_MMX_INSTRUCTIONS_AVAILABLE */
72 );
73 map_feature!(
74 features, 25, /*SSE*/
75 6 /* PF_XMMI_INSTRUCTIONS_AVAILABLE */
76 );
77 map_feature!(
78 features, 26, /*SSE2*/
79 10 /* PF_XMMI64_INSTRUCTIONS_AVAILABLE */
80 );
81 map_feature!(
82 features, 32, /*SSE3*/
83 13 /* PF_SSE3_INSTRUCTIONS_AVAILABLE */
84 );
85 map_feature!(
86 features, 45, /*CX16*/
87 14 /* PF_COMPARE_EXCHANGE128 */
88 );
89 map_feature!(features, 58 /*XSAVE*/, 17 /* PF_XSAVE_ENABLED */);
90 map_feature!(
91 features, 62, /*RDRAND*/
92 28 /* PF_RDRAND_INSTRUCTION_AVAILABLE */
93 );
94
95 let ext_features: u64 = mach::sysctl_by_name(b"machdep.cpu.extfeature_bits\0");
96
97 map_feature!(
98 ext_features,
99 27, /* RDTSCP */
100 32 /* PF_RDTSCP_INSTRUCTION_AVAILABLE */
101 );
102 map_feature!(
103 ext_features,
104 31, /* 3DNOW */
105 7 /* PF_3DNOW_INSTRUCTIONS_AVAILABLE */
106 );
107
108 let leaf_features: u32 = mach::sysctl_by_name(b"machdep.cpu.leaf7_feature_bits\0");
109 map_feature!(
110 leaf_features,
111 0, /* F7_FSGSBASE */
112 22 /* PF_RDWRFSGSBASE_AVAILABLE */
113 );
114
115 // In newer production kernels, NX is always enabled.
116 // See 10.15.0 xnu-6153.11.26/osfmk/x86_64/pmap.c nx_enabled.
117 md_feats |= 1 << 12 /* PF_NX_ENABLED */;
118
119 // All CPUs that Apple is known to have shipped should support DAZ.
120 md_feats |= 1 << 11 /* PF_SSE_DAZ_MODE_AVAILABLE */;
121
122 // minidump_common::format::OtherCpuInfo is just 2 adjacent u64's, we only
123 // set the first, so just do a direct write to the bytes
124 cpu.data[..std::mem::size_of::<u64>()].copy_from_slice(&md_feats.to_ne_bytes());
125 }
126
127 impl MinidumpWriter {
128 /// Writes the [`MDStreamType::SystemInfoStream`] stream.
129 ///
130 /// On MacOS we includes basic CPU information, though some of it is not
131 /// available on `aarch64` at the time of this writing, as well as kernel
132 /// version information.
133 pub(crate) fn write_system_info(
134 &mut self,
135 buffer: &mut DumpBuf,
136 _dumper: &TaskDumper,
137 ) -> Result<MDRawDirectory, WriterError> {
138 let mut info_section = MemoryWriter::<MDRawSystemInfo>::alloc(buffer)?;
139 let dirent = MDRawDirectory {
140 stream_type: MDStreamType::SystemInfoStream as u32,
141 location: info_section.location(),
142 };
143
144 let number_of_processors: u8 = mach::int_sysctl_by_name(b"hw.ncpu\0");
145 // SAFETY: POD buffer
146 let mut cpu: format::CPU_INFORMATION = unsafe { std::mem::zeroed() };
147 read_cpu_info(&mut cpu);
148
149 cfg_if::cfg_if! {
150 if #[cfg(target_arch = "x86_64")] {
151 let processor_architecture = MDCPUArchitecture::PROCESSOR_ARCHITECTURE_AMD64;
152
153 // machdep.cpu.family and machdep.cpu.model already take the extended family
154 // and model IDs into account. See 10.9.2 xnu-2422.90.20/osfmk/i386/cpuid.c
155 // cpuid_set_generic_info().
156 let processor_level: u16 = mach::int_sysctl_by_name(b"machdep.cpu.family\0");
157 let model: u8 = mach::int_sysctl_by_name(b"machdep.cpu.model\0");
158 let stepping: u8 = mach::int_sysctl_by_name(b"machdep.cpu.stepping\0");
159
160 let processor_revision = ((model as u16) << 8) | stepping as u16;
161 } else if #[cfg(target_arch = "aarch64")] {
162 let processor_architecture = MDCPUArchitecture::PROCESSOR_ARCHITECTURE_ARM64_OLD;
163
164 let family: u32 = mach::sysctl_by_name(b"hw.cpufamily\0");
165
166 let processor_level = (family & 0xffff0000 >> 16) as u16;
167 let processor_revision = (family & 0x0000ffff) as u16;
168 } else {
169 compile_error!("unsupported target architecture");
170 }
171 }
172
173 let (major_version, minor_version, build_number) = os_version();
174 let os_version_loc = write_string_to_location(buffer, &build_version())?;
175
176 let info = MDRawSystemInfo {
177 // CPU
178 processor_architecture: processor_architecture as u16,
179 processor_level,
180 processor_revision,
181 number_of_processors,
182 cpu,
183
184 // OS
185 platform_id: PlatformId::MacOs as u32,
186 product_type: 1, // VER_NT_WORKSTATION, could also be VER_NT_SERVER but...seriously?
187 major_version,
188 minor_version,
189 build_number,
190 csd_version_rva: os_version_loc.rva,
191
192 suite_mask: 0,
193 reserved2: 0,
194 };
195
196 info_section.set_value(buffer, info)?;
197
198 Ok(dirent)
199 }
200 }