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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use crate::proto::Protocol;
use crate::{unsafe_guid, Result, Status};
use bitflags::bitflags;
use core::ffi::c_void;
use core::ptr;
use core::time::Duration;
pub type Procedure = extern "efiapi" fn(*mut c_void);
bitflags! {
#[derive(Default)]
struct StatusFlag: u32 {
const PROCESSOR_AS_BSP_BIT = 1;
const PROCESSOR_ENABLED_BIT = 1 << 1;
const PROCESSOR_HEALTH_STATUS_BIT = 1 << 2;
}
}
#[derive(Default, Debug)]
pub struct ProcessorCount {
pub total: usize,
pub enabled: usize,
}
#[repr(C)]
#[derive(Default, Debug)]
pub struct ProcessorInformation {
pub processor_id: u64,
status_flag: StatusFlag,
pub location: CpuPhysicalLocation,
}
impl ProcessorInformation {
pub fn is_bsp(&self) -> bool {
self.status_flag.contains(StatusFlag::PROCESSOR_AS_BSP_BIT)
}
pub fn is_enabled(&self) -> bool {
self.status_flag.contains(StatusFlag::PROCESSOR_ENABLED_BIT)
}
pub fn is_healthy(&self) -> bool {
self.status_flag
.contains(StatusFlag::PROCESSOR_HEALTH_STATUS_BIT)
}
}
#[repr(C)]
#[derive(Default, Debug)]
pub struct CpuPhysicalLocation {
pub package: u32,
pub core: u32,
pub thread: u32,
}
#[repr(C)]
#[unsafe_guid("3fdda605-a76e-4f46-ad29-12f4531b3d08")]
#[derive(Protocol)]
pub struct MpServices {
get_number_of_processors: extern "efiapi" fn(
this: *const MpServices,
number_of_processors: *mut usize,
number_of_enabled_processors: *mut usize,
) -> Status,
get_processor_info: extern "efiapi" fn(
this: *const MpServices,
processor_number: usize,
processor_info_buffer: *mut ProcessorInformation,
) -> Status,
startup_all_aps: extern "efiapi" fn(
this: *const MpServices,
procedure: Procedure,
single_thread: bool,
wait_event: *mut c_void,
timeout_in_micro_seconds: usize,
procedure_argument: *mut c_void,
failed_cpu_list: *mut *mut usize,
) -> Status,
startup_this_ap: extern "efiapi" fn(
this: *const MpServices,
procedure: Procedure,
processor_number: usize,
wait_event: *mut c_void,
timeout_in_micro_seconds: usize,
procedure_argument: *mut c_void,
finished: *mut bool,
) -> Status,
switch_bsp: extern "efiapi" fn(
this: *const MpServices,
processor_number: usize,
enable_old_bsp: bool,
) -> Status,
enable_disable_ap: extern "efiapi" fn(
this: *const MpServices,
processor_number: usize,
enable_ap: bool,
health_flag: *const u32,
) -> Status,
who_am_i: extern "efiapi" fn(this: *const MpServices, processor_number: *mut usize) -> Status,
}
impl MpServices {
pub fn get_number_of_processors(&self) -> Result<ProcessorCount> {
let mut total: usize = 0;
let mut enabled: usize = 0;
(self.get_number_of_processors)(self, &mut total, &mut enabled)
.into_with_val(|| ProcessorCount { total, enabled })
}
pub fn get_processor_info(&self, processor_number: usize) -> Result<ProcessorInformation> {
let mut pi: ProcessorInformation = Default::default();
(self.get_processor_info)(self, processor_number, &mut pi).into_with_val(|| pi)
}
pub fn startup_all_aps(
&self,
single_thread: bool,
procedure: Procedure,
procedure_argument: *mut c_void,
timeout: Option<Duration>,
) -> Result {
let timeout_arg = match timeout {
Some(timeout) => timeout.as_micros().try_into().unwrap(),
None => 0,
};
(self.startup_all_aps)(
self,
procedure,
single_thread,
ptr::null_mut(),
timeout_arg,
procedure_argument,
ptr::null_mut(),
)
.into()
}
pub fn startup_this_ap(
&self,
processor_number: usize,
procedure: Procedure,
procedure_argument: *mut c_void,
timeout: Option<Duration>,
) -> Result {
let timeout_arg = match timeout {
Some(timeout) => timeout.as_micros().try_into().unwrap(),
None => 0,
};
(self.startup_this_ap)(
self,
procedure,
processor_number,
ptr::null_mut(),
timeout_arg,
procedure_argument,
ptr::null_mut(),
)
.into()
}
pub fn switch_bsp(&self, processor_number: usize, enable_old_bsp: bool) -> Result {
(self.switch_bsp)(self, processor_number, enable_old_bsp).into()
}
pub fn enable_disable_ap(
&self,
processor_number: usize,
enable_ap: bool,
healthy: Option<bool>,
) -> Result {
let health_flag_raw: u32;
let health_flag_ptr = match healthy {
Some(healthy) => {
let mut sf = StatusFlag::empty();
sf.set(StatusFlag::PROCESSOR_HEALTH_STATUS_BIT, healthy);
health_flag_raw = sf.bits();
&health_flag_raw
}
None => ptr::null(),
};
(self.enable_disable_ap)(self, processor_number, enable_ap, health_flag_ptr).into()
}
pub fn who_am_i(&self) -> Result<usize> {
let mut processor_number: usize = 0;
(self.who_am_i)(self, &mut processor_number).into_with_val(|| processor_number)
}
}