Merge branch 'odin-lang:master' into darwin-foundation-support-for-handmade-hero-port

This commit is contained in:
cardboardguru76
2025-07-30 00:28:31 +01:00
committed by GitHub
9 changed files with 425 additions and 40 deletions
+335 -14
View File
@@ -5,15 +5,34 @@ foreign import mach "system:System"
import "core:c"
import "base:intrinsics"
kern_return_t :: distinct c.int
mach_port_t :: distinct c.uint
task_t :: mach_port_t
semaphore_t :: distinct u64
kern_return_t :: distinct c.int
thread_act_t :: distinct u64
thread_state_t :: distinct ^u32
thread_list_t :: [^]thread_act_t
vm_region_recurse_info_t :: distinct ^i32
task_info_t :: distinct ^i32
MACH_PORT_NULL :: 0
MACH_PORT_DEAD :: ~mach_port_t(0)
MACH_MSG_PORT_DESCRIPTOR :: 0
X86_THREAD_STATE32 :: 1
X86_THREAD_STATE64 :: 4
ARM_THREAD_STATE64 :: 6
mach_msg_option_t :: distinct i32
name_t :: distinct cstring
vm_map_t :: mach_port_t
mem_entry_name_port_t :: mach_port_t
ipc_space_t :: mach_port_t
thread_t :: mach_port_t
task_t :: mach_port_t
semaphore_t :: mach_port_t
vm_size_t :: distinct c.uintptr_t
@@ -29,11 +48,279 @@ vm_inherit_t :: distinct c.uint
mach_port_name_t :: distinct c.uint
mach_port_right_t :: distinct c.uint
sync_policy_t :: distinct c.int
mach_msg_port_descriptor_t :: struct {
name: mach_port_t,
_: u32,
using _: bit_field u32 {
_: u32 | 16,
disposition: u32 | 8,
type: u32 | 8,
},
}
Task_Port_Type :: enum u32 {
Kernel = 1,
Host,
Name,
Bootstrap,
Seatbelt = 7,
Access = 9,
}
Bootstrap_Error :: enum u32 {
Success,
Not_Privileged = 1100,
Name_In_Use = 1101,
Unknown_Service = 1102,
Service_Active = 1103,
Bad_Count = 1104,
No_Memory = 1105,
No_Children = 1106,
}
Msg_Type :: enum u32 {
Unstructured = 0,
Bit = 0,
Boolean = 0,
Integer_16 = 1,
Integer_32 = 2,
Char = 8,
Byte = 9,
Integer_8 = 9,
Real = 10,
Integer_64 = 11,
String = 12,
String_C = 12,
Port_Name = 15,
Move_Receive = 16,
Port_Receive = 16,
Move_Send = 17,
Port_Send = 17,
Move_Send_Once = 18,
Port_Send_Once = 18,
Copy_Send = 19,
Make_Send = 20,
Make_Send_Once = 21,
}
Msg_Header_Bits :: enum u32 {
Zero = 0,
Remote_Mask = 0xff,
Local_Mask = 0xff00,
Migrated = 0x08000000,
Unused = 0x07ff0000,
Complex_Data = 0x10000000,
Complex_Ports = 0x20000000,
Circular = 0x40000000,
Complex = 0x80000000,
}
mach_msg_type_t :: struct {
using _: bit_field u32 {
name: u32 | 8,
size: u32 | 8,
number: u32 | 12,
inline: u32 | 1,
longform: u32 | 1,
deallocate: u32 | 1,
unused: u32 | 1,
},
}
mach_msg_header_t :: struct {
msgh_bits: u32,
msgh_size: u32,
msgh_remote_port: mach_port_t,
msgh_local_port: mach_port_t,
msgh_voucher_port: u32,
msgh_id: i32,
}
mach_msg_body_t :: struct {
msgh_descriptor_count: u32,
}
mach_msg_trailer_t :: struct {
msgh_trailer_type: u32,
msgh_trailer_size: u32,
}
x86_thread_state32_t :: struct {
eax: u32,
ebx: u32,
ecx: u32,
edx: u32,
edi: u32,
esi: u32,
ebp: u32,
esp: u32,
ss: u32,
eflags: u32,
eip: u32,
cs: u32,
ds: u32,
es: u32,
fs: u32,
gs: u32,
}
X86_THREAD_STATE32_COUNT :: size_of(x86_thread_state32_t) / size_of(u32)
x86_thread_state64_t :: struct #packed {
rax: u64,
rbx: u64,
rcx: u64,
rdx: u64,
rdi: u64,
rsi: u64,
rbp: u64,
rsp: u64,
r8: u64,
r9: u64,
r10: u64,
r11: u64,
r12: u64,
r13: u64,
r14: u64,
r15: u64,
rip: u64,
rflags: u64,
cs: u64,
fs: u64,
gs: u64,
}
X86_THREAD_STATE64_COUNT :: size_of(x86_thread_state64_t) / size_of(u32)
arm_thread_state64_t :: struct #packed {
x: [29]u64,
fp: u64,
lr: u64,
sp: u64,
pc: u64,
cpsr: u32,
pad: u32,
}
ARM_THREAD_STATE64_COUNT :: size_of(arm_thread_state64_t) / size_of(u32)
THREAD_IDENTIFIER_INFO :: 4
thread_identifier_info :: struct {
thread_id: u64,
thread_handler: u64,
dispatch_qaddr: u64,
}
THREAD_IDENTIFIER_INFO_COUNT :: size_of(thread_identifier_info) / size_of(u32)
vm_region_submap_info_64 :: struct {
protection: u32,
max_protection: u32,
inheritance: u32,
offset: u64,
user_tag: u32,
pages_residept: u32,
pages_shared_now_private: u32,
pages_swapped_out: u32,
pages_dirtied: u32,
ref_count: u32,
shadow_depth: u16,
external_pager: u8,
share_mode: u8,
is_submap: b32,
behavior: i32,
object_id: u32,
user_wired_count: u16,
pages_reusable: u32,
}
VM_REGION_SUBMAP_INFO_COUNT_64 :: size_of(vm_region_submap_info_64) / size_of(u32)
TASK_DYLD_INFO :: 17
task_dyld_info :: struct {
all_image_info_addr: u64,
all_image_info_size: u64,
all_image_info_format: i32,
}
TASK_DYLD_INFO_COUNT :: size_of(task_dyld_info) / size_of(u32)
dyld_image_info :: struct {
image_load_addr: u64,
image_file_path: cstring,
image_file_mod_date: u64,
}
dyld_uuid_info :: struct {
image_load_addr: u64,
image_uuid: [16]u8,
}
dyld_all_image_infos :: struct {
version: u32,
info_array_count: u32,
info_array: rawptr,
notification: rawptr,
process_detached_from_shared_region: b32,
libSystem_initialized: b32,
dyld_image_load_addr: u64,
jit_info: rawptr,
dyld_version: cstring,
error_message: cstring,
termination_flags: u64,
core_symbolication_shm_page: rawptr,
system_order_flag: u64,
uuid_array_count: u64,
uuid_array: rawptr,
dyld_all_image_infos_addr: u64,
initial_image_count: u64,
error_kind: u64,
error_client_of_dylib_path: cstring,
error_target_dylib_path: cstring,
error_symbol: cstring,
shared_cache_slide: u64,
shared_cache_uuid: [16]u8,
shared_cache_base_addr: u64,
info_array_change_timestamp: u64,
dyld_path: cstring,
notify_ports: [8]mach_port_t,
reserved: [7]u64,
shared_cache_fsid: u64,
shared_cache_fsobjid: u64,
compact_dyld_image_info_addr: u64,
compact_dyld_image_info_size: u64,
platform: u32,
aot_info_count: u32,
aot_info_array: rawptr,
aot_info_array_change_timestamp: u64,
aot_shared_cache_base_address: u64,
aot_shared_cache_uuid: [16]u8,
}
@(default_calling_convention="c")
foreign mach {
mach_task_self :: proc() -> mach_port_t ---
mach_task_self :: proc() -> mach_port_t ---
mach_msg :: proc(header: rawptr, option: Msg_Option_Flags, send_size: u32, receive_limit: u32, receive_name: mach_port_t, timeout: u32, notify: mach_port_t) -> Kern_Return ---
mach_msg_send :: proc(header: rawptr) -> Kern_Return ---
mach_vm_allocate :: proc(target_task: task_t, adddress: u64, size: u64, flags: i32) -> Kern_Return ---
mach_vm_deallocate :: proc(target_task: task_t, adddress: ^u64, size: u64) -> Kern_Return ---
mach_vm_remap :: proc(target_task: task_t, page: rawptr, size: u64, mask: u64, flags: i32, src_task: task_t, src_address: u64, copy: b32, cur_protection: ^i32, max_protection: ^i32, inheritance: VM_Inherit) -> Kern_Return ---
mach_vm_region_recurse :: proc(target_task: task_t, address: ^u64, size: ^u64, depth: ^u32, info: vm_region_recurse_info_t, count: ^u32) -> Kern_Return ---
vm_page_size: u64
vm_page_mask: u64
vm_page_shift: i32
mach_port_allocate :: proc(task: task_t, right: Port_Right, name: rawptr) -> Kern_Return ---
mach_port_deallocate :: proc(task: task_t, name: u32) -> Kern_Return ---
mach_port_extract_right :: proc(task: task_t, name: u32, msgt_name: u32, poly: ^mach_port_t, poly_poly: ^mach_port_t) -> Kern_Return ---
task_get_special_port :: proc(task: task_t, port: i32, special_port: ^mach_port_t) -> Kern_Return ---
task_suspend :: proc(task: task_t) -> Kern_Return ---
task_resume :: proc(task: task_t) -> Kern_Return ---
task_threads :: proc(task: task_t, thread_list: ^thread_list_t, list_count: ^u32) -> Kern_Return ---
task_info :: proc(task: task_t, flavor: i32, info: task_info_t, count: ^u32) -> Kern_Return ---
task_terminate :: proc(task: task_t) -> Kern_Return ---
semaphore_create :: proc(task: task_t, semaphore: ^semaphore_t, policy: Sync_Policy, value: c.int) -> Kern_Return ---
semaphore_destroy :: proc(task: task_t, semaphore: semaphore_t) -> Kern_Return ---
@@ -44,9 +331,11 @@ foreign mach {
semaphore_wait :: proc(semaphore: semaphore_t) -> Kern_Return ---
vm_allocate :: proc (target_task : vm_map_t, address: ^vm_address_t, size: vm_size_t, flags: VM_Flags) -> Kern_Return ---
thread_get_state :: proc(thread: thread_act_t, flavor: i32, thread_state: thread_state_t, old_state_count: ^u32) -> Kern_Return ---
thread_info :: proc(thread: thread_act_t, flavor: u32, thread_info: ^thread_identifier_info, info_count: ^u32) -> Kern_Return ---
vm_deallocate :: proc(target_task: vm_map_t, address: vm_address_t, size: vm_size_t) -> Kern_Return ---
bootstrap_register2 :: proc(bp: mach_port_t, service_name: name_t, sp: mach_port_t, flags: u64) -> Kern_Return ---
bootstrap_look_up :: proc(bp: mach_port_t, service_name: name_t, sp: ^mach_port_t) -> Kern_Return ---
vm_map :: proc(
target_task: vm_map_t,
@@ -70,15 +359,10 @@ foreign mach {
object_handle: ^mem_entry_name_port_t,
parent_entry: mem_entry_name_port_t,
) -> Kern_Return ---
mach_port_deallocate :: proc(
task: ipc_space_t,
name: mach_port_name_t,
) -> Kern_Return ---
vm_page_size: vm_size_t
}
Kern_Return :: enum kern_return_t {
Success,
@@ -500,6 +784,39 @@ VM_PROT_NONE :: VM_Prot_Flags{}
VM_PROT_DEFAULT :: VM_Prot_Flags{.Read, .Write}
VM_PROT_ALL :: VM_Prot_Flags{.Read, .Write, .Execute}
/*
* Mach msg options, defined as bits within the mach_msg_option_t type
*/
Msg_Option :: enum mach_msg_option_t {
Send_Msg,
Receive_Msg,
Send_Timeout = LOG2(0x10),
Send_Notify = LOG2(0x20),
Send_Interrupt = LOG2(0x40),
Send_Cancel = LOG2(0x80),
Receive_Timeout = LOG2(0x100),
Receive_Notify = LOG2(0x200),
Receive_Interrupt = LOG2(0x400),
Receive_Large = LOG2(0x800),
Send_Always = LOG2(0x10000),
}
Msg_Option_Flags :: distinct bit_set[Msg_Option; mach_msg_option_t]
/*
* Enumeration of valid values for mach_port_right_t
*/
Port_Right :: enum mach_port_right_t {
Send,
Receive,
Send_Once,
Port_Set,
Dead_Name,
}
/*
* Enumeration of valid values for vm_inherit_t.
*/
@@ -522,3 +839,7 @@ Sync_Policy :: enum sync_policy_t {
Lifo = Fifo | Reversed,
}
mach_vm_trunc_page :: proc(v: u64) -> u64 {
return v & ~vm_page_mask
}
+20
View File
@@ -0,0 +1,20 @@
package posix
when ODIN_OS == .Darwin {
foreign import lib "system:System.framework"
} else {
foreign import lib "system:c"
}
foreign lib {
/*
Creates a child process from a provided filepath
spawnp searches directories on the path for the file
Returns: 0 on success, with the child pid returned in the pid argument, or error values on failure.
[[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn.html ]]
*/
posix_spawn :: proc(pid: ^pid_t, path: cstring, file_actions: rawptr, attrp: rawptr, argv: [^]cstring, envp: [^]cstring) -> Errno ---
posix_spawnp :: proc(pid: ^pid_t, file: cstring, file_actions: rawptr, attrp: rawptr, argv: [^]cstring, envp: [^]cstring) -> Errno ---
}