#![allow(dead_code)]
use crate::machine_profile::{
APPEARANCE_MANAGER_VERSION_BCD, KEYBOARD_ENVIRON_TYPE, POWERPC_GESTALT_MACHINE_TYPE,
POWERPC_SYSTEM_VERSION_BCD, QUICKTIME_NUM_VERSION, REFERENCE_MACHINE_PROFILE,
};
use crate::systems::macintosh::machine_profile::{
POWERPC_CARBON_VERSION_BCD, REFERENCE_M68K_EXECUTION_CAPABILITIES,
REFERENCE_POWERPC_CPU_CLOCK_HZ, REFERENCE_POWERPC_EXECUTION_CAPABILITIES,
};
use crate::trap::dispatch::{OS_TRAP_TABLE_BASE, TOOLBOX_TRAP_TABLE_BASE};
pub const NO_ERR: i16 = 0;
pub const PARAM_ERR: i16 = -50;
pub const GESTALT_UNDEF_SELECTOR_ERR: i16 = -5551;
pub const GESTALT_DUP_SELECTOR_ERR: i16 = -5552;
pub const GESTALT_LOCATION_ERR: i16 = -5553;
pub const GESTALT_UNDEF_SELECTOR_ERR_U32: u32 = 0xFFFF_EA51;
pub const GESTALT_DUP_SELECTOR_ERR_U32: u32 = 0xFFFF_EA50;
pub const GESTALT_LOCATION_ERR_U32: u32 = 0xFFFF_EA4F;
pub const GESTALT_SYS_ARCH_68K: u32 = 1;
pub const GESTALT_SYS_ARCH_POWERPC: u32 = 2;
pub const GESTALT_68040_LOGICAL_PAGE_SIZE_BYTES: u32 = 4096;
pub const GESTALT_QD3D_VERSION: u32 = crate::loader::ppc::PPC_QD3D_VERSION;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GestaltArchitecture {
M68k {
mmu_mode_32: bool,
},
PowerPc,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GestaltEvaluationContext {
pub architecture: GestaltArchitecture,
pub system_version_bcd: u32,
pub system_architecture: Option<u32>,
pub native_cpu_type: u32,
pub processor_type: u32,
pub gestalt_machine_type: u32,
pub fpu_type: u32,
pub mmu_type: u32,
pub physical_ram_size: u32,
pub addressing_mode_attributes: u32,
pub hardware_attributes: u32,
pub thread_manager_attributes: u32,
pub has_powerpc_quicktime: bool,
pub os_trap_table_base: u32,
pub toolbox_trap_table_base: u32,
}
impl GestaltEvaluationContext {
pub fn for_m68k(physical_ram_size: u32, mmu_mode_32: bool) -> Self {
Self {
architecture: GestaltArchitecture::M68k { mmu_mode_32 },
system_version_bcd: u32::from(REFERENCE_MACHINE_PROFILE.system_version_bcd),
system_architecture: REFERENCE_M68K_EXECUTION_CAPABILITIES.system_architecture,
native_cpu_type: REFERENCE_M68K_EXECUTION_CAPABILITIES.native_cpu_type,
processor_type: REFERENCE_M68K_EXECUTION_CAPABILITIES.processor_type,
gestalt_machine_type: u32::from(REFERENCE_MACHINE_PROFILE.gestalt_machine_type),
fpu_type: REFERENCE_M68K_EXECUTION_CAPABILITIES.fpu_type,
mmu_type: REFERENCE_M68K_EXECUTION_CAPABILITIES.mmu_type,
physical_ram_size,
addressing_mode_attributes: 0b110 | u32::from(mmu_mode_32),
hardware_attributes: (1 << 0) | (1 << 1) | (1 << 3) | (1 << 4) | (1 << 7),
thread_manager_attributes: 1,
has_powerpc_quicktime: false,
os_trap_table_base: OS_TRAP_TABLE_BASE,
toolbox_trap_table_base: TOOLBOX_TRAP_TABLE_BASE,
}
}
pub fn for_powerpc(physical_ram_size: u32) -> Self {
Self {
architecture: GestaltArchitecture::PowerPc,
system_version_bcd: u32::from(POWERPC_SYSTEM_VERSION_BCD),
system_architecture: REFERENCE_POWERPC_EXECUTION_CAPABILITIES.system_architecture,
native_cpu_type: REFERENCE_POWERPC_EXECUTION_CAPABILITIES.native_cpu_type,
processor_type: REFERENCE_POWERPC_EXECUTION_CAPABILITIES.processor_type,
gestalt_machine_type: u32::from(POWERPC_GESTALT_MACHINE_TYPE),
fpu_type: REFERENCE_POWERPC_EXECUTION_CAPABILITIES.fpu_type,
mmu_type: REFERENCE_POWERPC_EXECUTION_CAPABILITIES.mmu_type,
physical_ram_size,
addressing_mode_attributes: 0b111,
hardware_attributes: (1 << 0) | (1 << 1) | (1 << 4) | (1 << 19) | (1 << 21) | (1 << 22),
thread_manager_attributes: 0b101,
has_powerpc_quicktime: true,
os_trap_table_base: OS_TRAP_TABLE_BASE,
toolbox_trap_table_base: TOOLBOX_TRAP_TABLE_BASE,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GestaltQueryResult {
pub response: u32,
pub error_code: i16,
}
impl GestaltQueryResult {
pub const fn ok(response: u32) -> Self {
Self {
response,
error_code: NO_ERR,
}
}
pub const fn err(error_code: i16) -> Self {
Self {
response: 0,
error_code,
}
}
}
pub fn evaluate_gestalt_selector(
selector: u32,
context: &GestaltEvaluationContext,
) -> Option<GestaltQueryResult> {
let sel = selector.to_be_bytes();
match &sel {
b"vers" => Some(GestaltQueryResult::ok(0x0001)),
b"sysv" => Some(GestaltQueryResult::ok(context.system_version_bcd)),
b"sysa" => context.system_architecture.map(GestaltQueryResult::ok),
b"ostt" => Some(GestaltQueryResult::ok(context.os_trap_table_base)),
b"tbtt" => Some(GestaltQueryResult::ok(context.toolbox_trap_table_base)),
b"evnt" => Some(GestaltQueryResult::ok(0x0001)),
b"edtn" => Some(GestaltQueryResult::ok(1)),
b"cput" => Some(GestaltQueryResult::ok(context.native_cpu_type)),
b"proc" => Some(GestaltQueryResult::ok(context.processor_type)),
b"mach" => Some(GestaltQueryResult::ok(context.gestalt_machine_type)),
b"kbd " => Some(GestaltQueryResult::ok(u32::from(KEYBOARD_ENVIRON_TYPE))),
b"qd " => Some(GestaltQueryResult::ok(0x0230)),
b"qdrw" => Some(GestaltQueryResult::ok(0x000F)),
b"ram " => Some(GestaltQueryResult::ok(context.physical_ram_size)),
b"lram" => Some(GestaltQueryResult::ok(context.physical_ram_size)),
b"fpu " => Some(GestaltQueryResult::ok(context.fpu_type)),
b"mmu " => Some(GestaltQueryResult::ok(context.mmu_type)),
b"snd " => Some(GestaltQueryResult::ok(0x1CFB)),
b"tmgr" => Some(GestaltQueryResult::ok(2)),
b"thds" => Some(GestaltQueryResult::ok(context.thread_manager_attributes)),
b"dplv" => Some(GestaltQueryResult::ok(0x0002_0006)),
b"dply" => Some(GestaltQueryResult::ok(0x0000_0007)),
b"alis" => Some(GestaltQueryResult::ok(1)),
b"fs " => Some(GestaltQueryResult::ok((1 << 0) | (1 << 1))),
b"fold" => Some(GestaltQueryResult::ok(1)),
b"qtim" => Some(GestaltQueryResult::ok(QUICKTIME_NUM_VERSION)),
b"qtrs" => Some(GestaltQueryResult::ok(if context.has_powerpc_quicktime {
1
} else {
0
})),
b"drag" => Some(GestaltQueryResult::ok(0)),
b"os " => Some(GestaltQueryResult::ok(0x00FF)),
b"powr" => Some(GestaltQueryResult::ok(0)),
b"appr" => Some(GestaltQueryResult::ok(1)),
b"apvr" => Some(GestaltQueryResult::ok(u32::from(
APPEARANCE_MANAGER_VERSION_BCD,
))),
b"addr" => Some(GestaltQueryResult::ok(context.addressing_mode_attributes)),
b"hdwr" => Some(GestaltQueryResult::ok(context.hardware_attributes)),
b"sdev" => Some(GestaltQueryResult::ok(0)),
b"stdf" => Some(GestaltQueryResult::ok(1)),
b"help" => Some(GestaltQueryResult::ok(1)),
b"vm " => Some(GestaltQueryResult::ok(0)),
b"a/ux" => Some(GestaltQueryResult::err(GESTALT_UNDEF_SELECTOR_ERR)),
_ => match context.architecture {
GestaltArchitecture::M68k { .. } => match &sel {
b"cltn" => Some(GestaltQueryResult::ok(0x0100_0000)),
b"ppc " => Some(GestaltQueryResult::ok(0x0001)),
b"pgsz" => Some(GestaltQueryResult::ok(
GESTALT_68040_LOGICAL_PAGE_SIZE_BYTES,
)),
b"SAVR" => Some(GestaltQueryResult::err(GESTALT_UNDEF_SELECTOR_ERR)),
b"ttsc" => Some(GestaltQueryResult::ok(0)),
b"te " => Some(GestaltQueryResult::ok(5)),
b"teat" => Some(GestaltQueryResult::ok(0)),
b"rsrc" => Some(GestaltQueryResult::ok(1)),
b"scr#" => Some(GestaltQueryResult::ok(1)),
_ => None,
},
GestaltArchitecture::PowerPc => match &sel {
b"scri" => Some(GestaltQueryResult::ok(0x0700)),
b"tsmv" => Some(GestaltQueryResult::ok(0x0150)),
b"cbon" => Some(GestaltQueryResult::ok(u32::from(
POWERPC_CARBON_VERSION_BCD,
))),
b"cpuf" => Some(GestaltQueryResult::ok(context.native_cpu_type)),
b"pclk" => Some(GestaltQueryResult::ok(REFERENCE_POWERPC_CPU_CLOCK_HZ)),
b"ppcf" => Some(GestaltQueryResult::ok(0)),
b"cfrg" => Some(GestaltQueryResult::ok(1)),
b"mixd" => Some(GestaltQueryResult::ok(1)),
b"qd3d" => Some(GestaltQueryResult::ok(1)),
b"q3v " => Some(GestaltQueryResult::ok(GESTALT_QD3D_VERSION)),
_ => None,
},
},
}
}
pub fn is_builtin_gestalt_selector(selector: u32, context: &GestaltEvaluationContext) -> bool {
matches!(
evaluate_gestalt_selector(selector, context),
Some(GestaltQueryResult {
error_code: NO_ERR,
..
})
)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GestaltQueryParameters {
pub selector: u32,
pub response_ptr: u32,
}
pub fn evaluate_gestalt_query_parameters(
selector: u32,
response_ptr: u32,
) -> Result<GestaltQueryParameters, i16> {
if response_ptr == 0 {
return Err(PARAM_ERR);
}
Ok(GestaltQueryParameters {
selector,
response_ptr,
})
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GestaltQueryEvaluation {
pub selector: u32,
pub response: u32,
pub error_code: i16,
}
impl GestaltQueryEvaluation {
pub fn error_code_u32(&self) -> u32 {
(self.error_code as i32) as u32
}
}
pub fn evaluate_gestalt_query(
selector: u32,
context: &GestaltEvaluationContext,
dynamic_value: Option<u32>,
) -> GestaltQueryEvaluation {
if let Some(builtin) = evaluate_gestalt_selector(selector, context) {
GestaltQueryEvaluation {
selector,
response: builtin.response,
error_code: builtin.error_code,
}
} else if let Some(value) = dynamic_value {
GestaltQueryEvaluation {
selector,
response: value,
error_code: NO_ERR,
}
} else {
GestaltQueryEvaluation {
selector,
response: 0,
error_code: GESTALT_UNDEF_SELECTOR_ERR,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NewGestaltAction {
DuplicateSelector,
Register {
selector: u32,
value: u32,
},
}
impl NewGestaltAction {
pub fn error_code(&self) -> i16 {
match self {
Self::DuplicateSelector => GESTALT_DUP_SELECTOR_ERR,
Self::Register { .. } => NO_ERR,
}
}
}
pub fn evaluate_new_gestalt(selector: u32, value: u32, is_already_known: bool) -> NewGestaltAction {
if is_already_known {
NewGestaltAction::DuplicateSelector
} else {
NewGestaltAction::Register { selector, value }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReplaceGestaltAction {
ReplaceCustom {
old_handler: u32,
new_handler: u32,
},
ReplaceBuiltin {
new_handler: u32,
},
UndefinedSelector,
}
impl ReplaceGestaltAction {
pub fn error_code(&self) -> i16 {
match self {
Self::ReplaceCustom { .. } | Self::ReplaceBuiltin { .. } => NO_ERR,
Self::UndefinedSelector => GESTALT_UNDEF_SELECTOR_ERR,
}
}
pub fn old_handler_or_default(&self, default_value: u32) -> u32 {
match self {
Self::ReplaceCustom { old_handler, .. } => *old_handler,
Self::ReplaceBuiltin { .. } => 0,
Self::UndefinedSelector => default_value,
}
}
}
pub fn evaluate_replace_gestalt(
_selector: u32,
new_handler: u32,
existing_custom_handler: Option<u32>,
is_builtin: bool,
) -> ReplaceGestaltAction {
if let Some(old_handler) = existing_custom_handler {
ReplaceGestaltAction::ReplaceCustom {
old_handler,
new_handler,
}
} else if is_builtin {
ReplaceGestaltAction::ReplaceBuiltin { new_handler }
} else {
ReplaceGestaltAction::UndefinedSelector
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn gestalt_evaluation_context_profiles() {
let m68k_ctx = GestaltEvaluationContext::for_m68k(8 * 1024 * 1024, true);
assert_eq!(m68k_ctx.system_architecture, Some(GESTALT_SYS_ARCH_68K));
assert_eq!(m68k_ctx.physical_ram_size, 8 * 1024 * 1024);
assert_eq!(m68k_ctx.addressing_mode_attributes, 0b111);
assert!(!m68k_ctx.has_powerpc_quicktime);
let m68k_24bit_ctx = GestaltEvaluationContext::for_m68k(4 * 1024 * 1024, false);
assert_eq!(m68k_24bit_ctx.addressing_mode_attributes, 0b110);
let ppc_ctx = GestaltEvaluationContext::for_powerpc(32 * 1024 * 1024);
assert_eq!(ppc_ctx.system_architecture, Some(GESTALT_SYS_ARCH_POWERPC));
assert_eq!(ppc_ctx.physical_ram_size, 32 * 1024 * 1024);
assert_eq!(ppc_ctx.addressing_mode_attributes, 0b111);
assert!(ppc_ctx.has_powerpc_quicktime);
}
#[test]
fn gestalt_selector_universal_and_architecture_evaluation() {
let m68k_ctx = GestaltEvaluationContext::for_m68k(16 * 1024 * 1024, true);
let ppc_ctx = GestaltEvaluationContext::for_powerpc(16 * 1024 * 1024);
for selector in [
*b"vers", *b"sysv", *b"cput", *b"mach", *b"kbd ", *b"qd ", *b"qdrw", *b"ram ",
*b"lram", *b"snd ", *b"tmgr", *b"alis", *b"fs ", *b"fold", *b"qtim", *b"os ",
*b"appr", *b"apvr", *b"addr", *b"hdwr", *b"stdf", *b"help", *b"vm ",
] {
let sel_u32 = u32::from_be_bytes(selector);
let m68k_res = evaluate_gestalt_selector(sel_u32, &m68k_ctx);
let ppc_res = evaluate_gestalt_selector(sel_u32, &ppc_ctx);
assert!(
m68k_res.is_some(),
"selector {:?} missing on 68k",
std::str::from_utf8(&selector)
);
assert!(
ppc_res.is_some(),
"selector {:?} missing on PPC",
std::str::from_utf8(&selector)
);
assert_eq!(m68k_res.unwrap().error_code, NO_ERR);
assert_eq!(ppc_res.unwrap().error_code, NO_ERR);
}
for selector in [
*b"cltn", *b"ppc ", *b"pgsz", *b"ttsc", *b"te ", *b"rsrc", *b"scr#",
] {
let sel_u32 = u32::from_be_bytes(selector);
let res = evaluate_gestalt_selector(sel_u32, &m68k_ctx);
assert!(
res.is_some(),
"68k selector {:?} not found",
std::str::from_utf8(&selector)
);
assert_eq!(res.unwrap().error_code, NO_ERR);
}
for selector in [
*b"scri", *b"tsmv", *b"cbon", *b"cpuf", *b"pclk", *b"cfrg", *b"mixd", *b"qd3d",
*b"q3v ",
] {
let sel_u32 = u32::from_be_bytes(selector);
let res = evaluate_gestalt_selector(sel_u32, &ppc_ctx);
assert!(
res.is_some(),
"PPC selector {:?} not found",
std::str::from_utf8(&selector)
);
assert_eq!(res.unwrap().error_code, NO_ERR);
}
let aux_res = evaluate_gestalt_selector(u32::from_be_bytes(*b"a/ux"), &m68k_ctx).unwrap();
assert_eq!(aux_res.error_code, GESTALT_UNDEF_SELECTOR_ERR);
assert_eq!(aux_res.response, 0);
let savr_res = evaluate_gestalt_selector(u32::from_be_bytes(*b"SAVR"), &m68k_ctx).unwrap();
assert_eq!(savr_res.error_code, GESTALT_UNDEF_SELECTOR_ERR);
assert_eq!(savr_res.response, 0);
assert!(evaluate_gestalt_selector(u32::from_be_bytes(*b"????"), &m68k_ctx).is_none());
}
#[test]
fn builtin_gestalt_selector_filtering() {
let m68k_ctx = GestaltEvaluationContext::for_m68k(4 * 1024 * 1024, true);
assert!(is_builtin_gestalt_selector(
u32::from_be_bytes(*b"vers"),
&m68k_ctx
));
assert!(is_builtin_gestalt_selector(
u32::from_be_bytes(*b"sysv"),
&m68k_ctx
));
assert!(is_builtin_gestalt_selector(
u32::from_be_bytes(*b"mach"),
&m68k_ctx
));
assert!(is_builtin_gestalt_selector(
u32::from_be_bytes(*b"cltn"),
&m68k_ctx
));
assert!(!is_builtin_gestalt_selector(
u32::from_be_bytes(*b"a/ux"),
&m68k_ctx
));
assert!(!is_builtin_gestalt_selector(
u32::from_be_bytes(*b"SAVR"),
&m68k_ctx
));
assert!(!is_builtin_gestalt_selector(
u32::from_be_bytes(*b"zzzz"),
&m68k_ctx
));
}
#[test]
fn gestalt_query_parameters_validation() {
assert_eq!(
evaluate_gestalt_query_parameters(0x12345678, 0),
Err(PARAM_ERR)
);
let params = evaluate_gestalt_query_parameters(0x12345678, 0x1000).unwrap();
assert_eq!(params.selector, 0x12345678);
assert_eq!(params.response_ptr, 0x1000);
}
#[test]
fn gestalt_mutation_actions_evaluation() {
assert_eq!(
evaluate_new_gestalt(0x11112222, 0x33334444, true),
NewGestaltAction::DuplicateSelector
);
assert_eq!(
evaluate_new_gestalt(0x11112222, 0x33334444, false),
NewGestaltAction::Register {
selector: 0x11112222,
value: 0x33334444,
}
);
assert_eq!(
evaluate_replace_gestalt(0x11112222, 0x55556666, Some(0x77778888), false),
ReplaceGestaltAction::ReplaceCustom {
old_handler: 0x77778888,
new_handler: 0x55556666,
}
);
assert_eq!(
evaluate_replace_gestalt(0x11112222, 0x55556666, None, true),
ReplaceGestaltAction::ReplaceBuiltin {
new_handler: 0x55556666,
}
);
assert_eq!(
evaluate_replace_gestalt(0x11112222, 0x55556666, None, false),
ReplaceGestaltAction::UndefinedSelector
);
}
}