Skip to content

Add SPTM Emulator - #1

Closed
niklassheth wants to merge 23 commits into
ADevWithAnIdea:mainfrom
niklassheth:main
Closed

niklassheth wants to merge 23 commits into
ADevWithAnIdea:mainfrom
niklassheth:main

Conversation

@niklassheth

Copy link
Copy Markdown

No description provided.

@ADevWithAnIdea ADevWithAnIdea left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Took a brief glance, on the whole, looks good to me, just a few general things (specifics left inline):

  • comments need human review
  • new files are gpl2
  • there's the sptm emulator itself (sptm.py and the sptm/whatever.c stuff) and the scaffolding that goes around it in hv.c, hv_exc.c, whatever, the sptm emulator can be a monolithic comment but the stuff around it should be broken out, unfortunately this is likely quite a bit of human labor on your part but otherwsie review is difficult
  • how bad would it be to prototype an all C sptm emulator? this would remove a lot of complexity, and tbh it seems pretty one shottable with codex

Comment thread proxyclient/m1n1/hv/__init__.py Resolved
Comment thread proxyclient/m1n1/hv/__init__.py Outdated
self.p.mask32(base + off, 0x3ff, (data | 0xf) & ~(0x80000400))
_pmgr[base + off] = (data & 0xfffffc0f) | ((data & 0xf) << 4)
addr = base + off
if not shadow_pmgr:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

why is this necessary exactly? so we allow the xnu cpustart why? why do we need this setting, and if so it should be its own commit.

Comment thread proxyclient/m1n1/hv/sptm.py Outdated

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

toss the comment, change the spdx license identifer to gpl 2 only (i'll update the license globally, and same for all new files) and say that this is the boot handoff. you also need to describe how the python half of the emulator talks to the C endpoints. TBH I'm somewhat in favor of just rewitig the whole emulator in C and tossing the python entirely, still undecided here, but it removes the need for the python/c communication entirely

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

oh also i basically didn't look at the file itself at all because idrc as long as it works

Comment thread proxyclient/tools/run_guest.py Outdated
parser.add_argument('-l', '--logfile', type=pathlib.Path)
parser.add_argument('--fake-disp0', action='store_true',
help='Alias every T8132 dart-disp0 SID 0 mapping to one '
'of 8192 read-only zero pages')

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

is this a leftover from the dcp hang after login debugging? if so is this still needed/functional/whatever?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

also if so this should be its own commit

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I just got display out working so this is no longer needed! This will be removed

Comment thread src/arm_cpu_regs.h

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

make this its own separate commit, first in the series, just call it add more sysregs or something

Comment thread src/hv.c
#include "cpu_regs.h"
#include "display.h"
#include "gxf.h"
#include "hv_sptm.h"

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

nothing from here is actually used? if i'm reading this right

Comment thread src/hv.c Outdated
reg_set(CNTHCTL_EL2,
CNTHCTL_EL1NVVCT | CNTHCTL_EL1NVPCT | CNTHCTL_EL1TVT | CNTHCTL_EL1PCTEN);
/*
* Guarded M4 guests use the architectural virtual timer as the

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

shorten the comment, and why not

set CNTHCTL_EL2, CNTHCTL_EL1TVT)
if apple sysregs unlocked:
set the rest

makes more sense to me than the clear, but ti's a minor detail

Comment thread src/hv.h Outdated
int hv_map_hook(u64 from, hv_hook_t *hook, u64 size);

/* T8140 CPU/ACC/CPM write guard (no-op on other SoCs); see hv_t8140.c. */
/* CPU/ACC/CPM write guards for hypervisor-managed power state. */

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

hypervisor-managed is wrong, it causes el2 serrors

Comment thread src/memory.c Outdated
ram_size, MAIR_IDX_NORMAL, PERM_RX_EL0,
handoff_start, handoff_size);

/*

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

this is entirely for the hv? if so we should gate it on the hv

Comment thread src/pcie.c

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

this just looks like yuka's patchset? it should be its own separate thing, not squashed with yoru sptm emulator

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch, there are some upstream changes I pulled in and accidentally squashed. I'm not sure if they're even needed, but it would be good to rebase on upstream since some t8132 changes were made

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

yeah I'll do this likely tomorrow, i'll pull in upstream changes to main, and change the license/readme, and then you can rebase then

@ADevWithAnIdea

Copy link
Copy Markdown
Owner

Oh also I didn't review the sptm emulator itself at all, or the larger general HV changes, especially hv_exc.c, there's just a lot of them and i'm currently short on time

@ADevWithAnIdea

Copy link
Copy Markdown
Owner

whoops forgot to select the appropriate regions, apologies about that, knock wood most of it should be codexable anyways (review the comments and commit breakdown manually)

@niklassheth

Copy link
Copy Markdown
Author

Thanks for the detailed review! On moving the emulator into C, Python constructs the initial environment, but all of the endpoints are already in C. So C and Python don't need to communicate after boot. The current code with a bunch of added proxy endpoints can definitely be improved, but I think it's a good setup as long as we only support one SPTM version at a time. I can't think of a good reason to keep 26.6 support around after 27.0 comes out anyways

@ADevWithAnIdea

Copy link
Copy Markdown
Owner

I can't think of a good reason to keep 26.6 around

This is extremely incorrect, we're going to keep 26.6 around forever because we're going to bless 26.6 as the M4/A18 Pro firmware version. We'll bless a different 27.X version whenever the M5 SKUs are fixed. We can assume the number of versions to support is small (one per soc generation).

By communication, I don't mean runtime communication, but things like where stuff lives in memory. m1n1 has a very clear THIS IS NOT ABI STABLE promise so it's not a huge deal

@ADevWithAnIdea

Copy link
Copy Markdown
Owner

Oh also can you add a commit to just disable sapt universally? that should be its own thing and applies for hv or for no hv, should be a pretty trivial patch, just if m4+ or a18 pro + check memory size, find sapt region, zero the proper range

@ADevWithAnIdea

Copy link
Copy Markdown
Owner

Oh also, add a Signed off by, just run git commit -s when you actually make the commit to have it added automatically

Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Program the guarded guest event-stream state through CNTKCTL_EL12 and rescale
XNU's 24 MHz event index to the architectural counter frequency.

Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Rearm the physical EL2 timer after guest deadline changes, time-offset updates,
and host tick programming so the emulated Apple deadline is serviced on time.

Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
Locate XNU AMX capability symbols and retain their valid state through kernel
initialization. Once userspace shared regions are nested, suppress AMX in
the kernel and commpage capability policy.

Also configure SMCR_EL2 for M4 SME guests so primary and secondary CPUs
expose the maximum streaming vector length.

Assisted-By: OpenAI Codex (GPT-5.6 Sol)
Signed-off-by: Niklas Sheth <hi@niklassheth.com>
@ADevWithAnIdea

Copy link
Copy Markdown
Owner

Closing in favor of GravityLinux/bootloader#8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants