Skip to content
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

haiku adding subset of cpu topology api. #3440

Merged
merged 1 commit into from Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 27 additions & 5 deletions libc-test/build.rs
Expand Up @@ -4644,6 +4644,7 @@ fn test_haiku(target: &str) {
("sigaction", "sa_sigaction") => true,
("sigevent", "sigev_value") => true,
("fpu_state", "_fpreg") => true,
("cpu_topology_node_info", "data") => true,
// these fields have a simplified data definition in libc
("fpu_state", "_xmm") => true,
("savefpu", "_fp_ymm") => true,
Expand All @@ -4664,13 +4665,33 @@ fn test_haiku(target: &str) {
cfg.type_name(move |ty, is_struct, is_union| {
match ty {
// Just pass all these through, no need for a "struct" prefix
"area_info" | "port_info" | "port_message_info" | "team_info" | "sem_info"
| "team_usage_info" | "thread_info" | "cpu_info" | "system_info"
| "object_wait_info" | "image_info" | "attr_info" | "index_info" | "fs_info"
| "FILE" | "DIR" | "Dl_info" => ty.to_string(),
"area_info"
| "port_info"
| "port_message_info"
| "team_info"
| "sem_info"
| "team_usage_info"
| "thread_info"
| "cpu_info"
| "system_info"
| "object_wait_info"
| "image_info"
| "attr_info"
| "index_info"
| "fs_info"
| "FILE"
| "DIR"
| "Dl_info"
| "topology_level_type"
| "cpu_topology_node_info"
| "cpu_topology_root_info"
| "cpu_topology_package_info"
| "cpu_topology_core_info" => ty.to_string(),

// enums don't need a prefix
"directory_which" | "path_base_directory" => ty.to_string(),
"directory_which" | "path_base_directory" | "cpu_platform" | "cpu_vendor" => {
ty.to_string()
}

// is actually a union
"sigval" => format!("union sigval"),
Expand All @@ -4689,6 +4710,7 @@ fn test_haiku(target: &str) {
"type_" if struct_ == "sem_t" => "type".to_string(),
"type_" if struct_ == "attr_info" => "type".to_string(),
"type_" if struct_ == "index_info" => "type".to_string(),
"type_" if struct_ == "cpu_topology_node_info" => "type".to_string(),
"image_type" if struct_ == "image_info" => "type".to_string(),
s => s.to_string(),
}
Expand Down
122 changes: 122 additions & 0 deletions src/unix/haiku/native.rs
Expand Up @@ -197,6 +197,50 @@ e! {
B_UTILITIES_DIRECTORY,
B_PACKAGE_LINKS_DIRECTORY,
}

// kernel/OS.h

pub enum topology_level_type {
B_TOPOLOGY_UNKNOWN,
B_TOPOLOGY_ROOT,
B_TOPOLOGY_SMT,
B_TOPOLOGY_CORE,
B_TOPOLOGY_PACKAGE,
}
Copy link
Member

Choose a reason for hiding this comment

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

Is this valid? enums in Rust and C have very different semantics -- in C they are integer types and some associated constants, in Rust a value of enum type must always be one of the variants or else there is immediate UB.

The e! macro does not even seem to add any repr so the layout of these enums seems to be largely undefined as well.


pub enum cpu_platform {
B_CPU_UNKNOWN,
B_CPU_x86,
B_CPU_x86_64,
B_CPU_PPC,
B_CPU_PPC_64,
B_CPU_M68K,
B_CPU_ARM,
B_CPU_ARM_64,
B_CPU_ALPHA,
B_CPU_MIPS,
B_CPU_SH,
B_CPU_SPARC,
B_CPU_RISC_V
}

pub enum cpu_vendor {
B_CPU_VENDOR_UNKNOWN,
B_CPU_VENDOR_AMD,
B_CPU_VENDOR_CYRIX,
B_CPU_VENDOR_IDT,
B_CPU_VENDOR_INTEL,
B_CPU_VENDOR_NATIONAL_SEMICONDUCTOR,
B_CPU_VENDOR_RISE,
B_CPU_VENDOR_TRANSMETA,
B_CPU_VENDOR_VIA,
B_CPU_VENDOR_IBM,
B_CPU_VENDOR_MOTOROLA,
B_CPU_VENDOR_NEC,
B_CPU_VENDOR_HYGON,
B_CPU_VENDOR_SUN,
B_CPU_VENDOR_FUJITSU
}
}

s! {
Expand Down Expand Up @@ -310,6 +354,19 @@ s! {
pub events: u16
}

pub struct cpu_topology_root_info {
pub platform: cpu_platform,
}

pub struct cpu_topology_package_info {
pub vendor: cpu_vendor,
pub cache_line_size: u32,
}

pub struct cpu_topology_core_info {
pub model: u32,
pub default_frequency: u64,
}
// kernel/fs_attr.h
pub struct attr_info {
pub type_: u32,
Expand Down Expand Up @@ -412,6 +469,23 @@ s_no_extra_traits! {
pub as_chars: [::c_char; 16],
pub regs: __c_anonymous_regs,
}

#[cfg(libc_union)]
pub union __c_anonymous_cpu_topology_info_data {
pub root: cpu_topology_root_info,
pub package: cpu_topology_package_info,
pub core: cpu_topology_core_info,
}

pub struct cpu_topology_node_info {
pub id: u32,
pub type_: topology_level_type,
pub level: u32,
#[cfg(libc_union)]
pub data: __c_anonymous_cpu_topology_info_data,
#[cfg(not(libc_union))]
pub data: cpu_topology_core_info,
}
}

cfg_if! {
Expand Down Expand Up @@ -446,6 +520,50 @@ cfg_if! {
}
}
}

#[cfg(libc_union)]
impl PartialEq for __c_anonymous_cpu_topology_info_data {
fn eq(&self, other: &__c_anonymous_cpu_topology_info_data) -> bool {
unsafe {
self.root == other.root
|| self.package == other.package
|| self.core == other.core
}
}
Copy link
Member

Choose a reason for hiding this comment

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

This is unsound, as the fields may be uninitialized. Safe functions must not access union fields that may be uninitialized.
Cc #2816

}
#[cfg(libc_union)]
impl Eq for __c_anonymous_cpu_topology_info_data {}
#[cfg(libc_union)]
impl ::fmt::Debug for __c_anonymous_cpu_topology_info_data {
fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result {
unsafe {
f.debug_struct("__c_anonymous_cpu_topology_info_data")
.field("root", &self.root)
.field("package", &self.package)
.field("core", &self.core)
.finish()
Copy link
Member

Choose a reason for hiding this comment

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

This looks equally unsound as it reads from potentially uninitialized fields.

}
}
}

impl PartialEq for cpu_topology_node_info {
fn eq(&self, other: &cpu_topology_node_info) -> bool {
self.id == other.id
&& self.type_ == other.type_
&& self.level == other.level
}
}

impl Eq for cpu_topology_node_info {}
impl ::fmt::Debug for cpu_topology_node_info {
fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result {
f.debug_struct("cpu_topology_node_info")
.field("id", &self.id)
.field("type", &self.type_)
.field("level", &self.level)
.finish()
}
}
}
}

Expand Down Expand Up @@ -1026,6 +1144,10 @@ extern "C" {
info: *mut cpu_info,
size: ::size_t,
) -> status_t;
pub fn get_cpu_topology_info(
topologyInfos: *mut cpu_topology_node_info,
topologyInfoCount: *mut u32,
) -> status_t;
pub fn is_computer_on() -> i32;
pub fn is_computer_on_fire() -> ::c_double;
pub fn send_signal(threadID: thread_id, signal: ::c_uint) -> ::c_int;
Expand Down