Commit Graph

85 Commits

Author SHA1 Message Date
Thomas A.
36da4da465 If Count Exceeds MAX_SCHED_CPUS, Set Count To MAX_SCHED_CPUS
Fixes out of bounds issues when running Darling on a device/virutal machine that reports more then 64 cores. Fixes #7

Co-Authored-By: Janrupf <business.janrupf@gmail.com>
2024-03-28 22:04:48 -07:00
Ariel Abreu
99d3c764bf Force duct-tape to be built as static library 2023-10-20 13:42:51 -04:00
Ariel Abreu
f738dde463
A slightly better stub for HOST_VM_INFO
Instead of returning failure, just return success with a zeroed-out struct.
2023-10-16 17:21:36 -04:00
Ariel Abreu
16c10a30fa
Remove debugging from dtape_thread_*_state_*_user 2023-10-07 11:02:18 -04:00
Ariel Abreu
db65a1b009
Add a new tool for debugging darlingserver
This new tool (`dserverdbg`) runs on the host but connects to
darlingserver and makes unmanaged calls to retrieve debugging
information.

The initial set of subcommands available in this tool are `ps`,
`lsport`, `lspset`, and `lsmsg`:
  * `ps` lists processes currently registered with the server and how
    many Mach ports they have
  * `lsport` lists the ports of a given process (via PID) and their
    rights and messages counts (for receive rights)
  * `lspset` lists the members of a given portset (via PID and port
    name) and provides the same information about each port as `lsport`
  * `lsmsg` lists the messages of a given port (via PID and port name),
    providing sender PID (if available) and size

This tool may be expanded later to allow e.g. modifying logging settings
while darlingserver is running or perhaps searching through and
filtering the logs.
2023-10-04 00:23:56 -04:00
Ariel Abreu
7dba61bfb8
Implement task_ident functions
These are required for `secd` sessions (i.e. for Security).
2023-07-31 19:30:14 -04:00
Thomas A
0506ffb5af Relocate dtape_init/deinit Code Into init.c Source 2023-07-01 18:46:28 -07:00
Ariel Abreu
ce156a461f Consider mach_vm_map_external a safe stub
This is how it was in the LKM and everything was fine, so let's do the
same. This allows `jsc` (JavaScriptCore REPL) to work again.
2023-05-02 16:23:37 -07:00
Ariel Abreu
349e074b10 [dtape] Implement TASK_FLAGS_INFO 2023-05-02 16:23:37 -07:00
Thomas A
a4a5319723 Add TASK_AUDIT_TOKEN code from xnu source 2023-05-02 16:23:37 -07:00
Thomas A
afbcdfc14e Add implementation for mach_vm_remap_new_external
Code was copied over from xnu/osfmk/vm/vm_user.c. I'll admit that I'm not sure if this is the right approach, but it seems to allow me to progress with debugging notifyd.
2023-05-02 16:23:37 -07:00
Thomas A
0acd58b514 Fix int-conversion error 2022-11-20 11:58:22 -08:00
Gianluigi Tiesi
f4837bf507 Use dtape_stub_safe() for HOST_CPU_LOAD_INFO
Avoid darlingserver crash when calling host_statistics()
asking for HOST_CPU_LOAD_INFO

`top` makes this call, at least cash top and not the
server
2022-08-19 15:05:14 +02:00
Ariel Abreu
2101f7a5a5
Implement some processor info duct-tape code 2022-07-12 19:03:17 -04:00
Ariel Abreu
13695c79b2
Report correct thread states 2022-07-05 16:42:40 -04:00
Ariel Abreu
e7c383758a
Fix some leaks in duct-taped code
One significant change made here is that lck_mtx structures now directly
contain the internals of dtape_mutex structures. This was changed
because the old way of storing in a malloc'ed object led to memory leaks.
The problem is that there's a lot of XNU code that uses simple locks and
does not destroy them (because it doesn't need to in the XNU
implementation). Since the only structure that really cares about the
lock size is the waitq structure, we just patch that up. Besides, we
had modified the waitq structure in the LKM before and nothing blew up,
so this should be fine.
2022-06-30 22:49:17 -04:00
Ariel Abreu
5bd9a87821
Merge pull request #2 from rdrpenguin04/main
Add mach_vm_msync
2022-06-18 23:01:21 -04:00
Ray Redondo
58b0391a2c
update duct-tape/src/memory.c
Because I don't understand this and someone else does

Co-authored-by: Ariel Abreu <facekapow@outlook.com>
2022-06-18 20:44:33 -05:00
Ariel Abreu
a0115601ee
Call dtape_timer_fired within a microthread
This function calls other duct-taped functions that might need to wait
for a bit, so it needs to run in a microthread so it can do so.
2022-06-18 17:22:48 -04:00
Ariel Abreu
f2ad6c2cc2
Fix duct-tape timer scheduling
We were previously always updating the timer deadline. This meant that,
when a later deadline than the current one came along, we would update
the deadline to the later one. In effect, we were scheduling a timer for
the latest deadline available rather than the earliest.

The fix involves keeping track of the current deadline and not updating
it if the new deadline is later than the current one. There is an option
to override this behavior, however, because sometimes the timer_call code
changes the deadline on us to a later time and we *do* want to update it
when it tells us to do so explicitly. For example, the deadline returned
by timer_queue_expire is definitive: that's definitely the next deadline
we want. The deadline passed to timer_queue_assign, on the other hand,
is merely is a suggestion.
2022-06-18 16:51:59 -04:00
Ray Redondo
b37026af03 add mach_vm_msync 2022-06-17 23:18:21 -05:00
Ariel Abreu
2ba5a41c4e
Properly implement knote_vanish
This fixes some crashes with syslogd because the mqueue was vanishing
and calling knote_vanish, indicating its klist was going to be emptied.
However, since we weren't storing this flag in the knote,
filt_machportdetach thought the knote was still attached and tried to
detach it, causing a NULL pointer access.
2022-05-14 01:38:22 -04:00
Ariel Abreu
fdc3420249
Make sure processes stay alive until kqchannels die 2022-05-10 21:02:07 -04:00
Ariel Abreu
a46079444c
Report correct arch values in host_info
We're actually supposed to report plain x86, even on x86_64 processors.
2022-05-10 10:15:57 -04:00
Ariel Abreu
9123bad915
Handle thread and process death asynchronously
What this means is that we no longer release and destroy Thread and
Process instances when the threads and processes they manage die.
Instead, we keep them alive to perform some cleanup (like finishing
active calls).

This should fix the duct-tape panic where threads and tasks are still
referenced at death.

Best of all, there don't seem to be any leaks with this approach: for
each `process dying` or `thread dying` message in the log, there's a
`process being destroyed` or `thread being destroyed` message later
on. This means we're not leaking any processes or threads.
2022-04-08 10:38:36 -04:00
Ariel Abreu
7d6f0ed991
dtape: Forgot to unlock shared entry lock in special case for LLDB 2022-04-08 10:31:52 -04:00
Ariel Abreu
7e1dd74cd2
Implement memory sharing (in mach_vm_remap)
This commit allows Darling processes to convert private memory in other
Darling processes into shared memory that they can access. This is
necessary, e.g. for LLDB.
2022-04-01 02:21:32 -04:00
Ariel Abreu
be203fd569
Allow threads to perform S2C calls at any time
This is possible now because we can signal threads with a real-time
signal that libsystem_kernel handles.
2022-04-01 02:11:17 -04:00
Ariel Abreu
5b0bf0ff13
dtape: Safely stub thread_abort_safely 2022-04-01 01:51:31 -04:00
Ariel Abreu
e46c682d50
Add some dyld info debug messages and fix a typo in TASK_DYLD_INFO
I had forgotten to add `return KERN_SUCCESS`, so it was continuing into
TASK_VM_INFO and presumably failing with KERN_INVALID_ARGUMENT.
2022-03-28 14:19:14 -04:00
Ariel Abreu
f099fe3390
Fix mach_vm_region{,_recurse} to use the target task
They were using the current task, but that's not always the case.
LLDB, for example, calls mach_vm_region_recurse with the map of the task
it's debugging.
2022-03-28 14:17:48 -04:00
Ariel Abreu
71a247e51e
Add support for nested interrupts 2022-03-27 14:50:23 -04:00
Ariel Abreu
f5b97b9192
Implement mach_vm_region_recurse
This is only a subset of its actual behavior, but this is all that the
LKM supported and everything (read: LLDB) seemed to run fine with that,
so that should be enough for us as well.
2022-03-25 01:09:37 -04:00
Ariel Abreu
fd71ed2bfa
Implement task and thread suspension and resumption
These should be the final missing bits for sigexc from the LKM
2022-03-24 14:04:56 -04:00
Ariel Abreu
337935e64c
Add a ptrace_sigexc call 2022-03-24 14:02:08 -04:00
Ariel Abreu
f1ba799ba9
Add a set_dyld_info call 2022-03-24 13:57:07 -04:00
Ariel Abreu
ffcd17e5a7
Implement TASK_VM_INFO 2022-03-24 13:42:04 -04:00
Ariel Abreu
69c5a1d7ed
Add a method and a hook to send a signal to a thread 2022-03-24 13:40:27 -04:00
Ariel Abreu
be6e1921d2
dtape: Add a basic condition variable implementation 2022-03-24 13:35:06 -04:00
Ariel Abreu
df94cff467
psynch: Log to the server log file rather than stdout 2022-03-24 13:06:37 -04:00
Ariel Abreu
11fc38e931
thread_block_parameter: Handle continuation when not waiting
This is actually a valid state for `thread_block_parameter` to enter.
If the caller gave us a continuation but we were unable to wait, we
should simply invoke the continuation with the wait result, much like
we would if we were returning the result.
2022-03-24 13:05:50 -04:00
Ariel Abreu
42f778c9d0
thread_policy and thread_policy_set are actually safe to stub 2022-03-21 00:20:34 -04:00
Ariel Abreu
e450df2d9d
dtape: Stub vm_stats in a less crash-y way 2022-03-21 00:20:09 -04:00
Ariel Abreu
127442f146
dtape: host_info: Fix how RAM size is reported
`basic_info->memory_size` is 32-bit; using it for `basic_info->max_mem`
truncates the actual value.
2022-03-21 00:19:24 -04:00
Ariel Abreu
878918aa18
dtape: Implement a few duct-taped functions for neofetch
`mach_vm_region`, `thread_info`, and `TASK_THREAD_TIMES_INFO`
2022-03-15 01:59:46 -04:00
Ariel Abreu
0c08310cc5
dtape: Fix mach_vm_remap
It was using the byte size as the page count, which resulted in mapping many more fixed pages than necessary, overwriting existing memory.
2022-03-10 10:50:49 -05:00
Ariel Abreu
cb4353fcd2
dtape: Implement a few more duct-taped XNU calls
Also, make sure to terminate the IPC space when destroying a task;
this is what actually terminates ports and turns them into dead names.
2022-03-09 17:35:29 -05:00
Ariel Abreu
ddac152b11
Add some code useful for debugging
The main debugging code added is for keeping track of port names and
their associated IPC objects, as well as keeping track of the members of
port sets.

Additionally, when extended debugging is enabled, the server can now
wait for a debugger with the new env var `DSERVER_WAIT4DEBUGGER`.

The AsyncWriter class was originally written for some additional
debugging code I wrote but later decided wouldn't really be useful.
I kept the AsyncWriter class, however, as it seems it might be useful
for future code (it's basically fire-and-forget asynchronous writing).
Note that it has not been tested at all.
2022-03-09 17:28:37 -05:00
Ariel Abreu
1be73cf3ae
Remove threads from ksyn queues upon death 2022-03-04 14:24:44 -05:00
Ariel Abreu
bce5f3049b
Re-work sigexc handling code
We now handle the sigexc calls as normal calls, with the exception that
it's okay for them to become active while another call was active.

We also set the thread's wait result to THREAD_INTERRUPTED and handle
syscall returns in interrupted continuations by jumping back to the
sigexc_enter code.
2022-03-04 12:51:57 -05:00