1*5c2921b0SApple OSS Distributions #include <darwintest.h>
2*5c2921b0SApple OSS Distributions
3*5c2921b0SApple OSS Distributions #include <mach/mach_error.h>
4*5c2921b0SApple OSS Distributions #include <mach/mach_host.h>
5*5c2921b0SApple OSS Distributions
6*5c2921b0SApple OSS Distributions #include "drop_priv.h"
7*5c2921b0SApple OSS Distributions
8*5c2921b0SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.debugging"));
9*5c2921b0SApple OSS Distributions
10*5c2921b0SApple OSS Distributions /*
11*5c2921b0SApple OSS Distributions * The low 8 bits may be in use, so modify one
12*5c2921b0SApple OSS Distributions * of the upper 8 bits to ensure round-tripping.
13*5c2921b0SApple OSS Distributions */
14*5c2921b0SApple OSS Distributions #define LIBTRACE_PRIVATE_DATA 0x01000000
15*5c2921b0SApple OSS Distributions
16*5c2921b0SApple OSS Distributions static bool _needs_reset;
17*5c2921b0SApple OSS Distributions static uint32_t _original;
18*5c2921b0SApple OSS Distributions
19*5c2921b0SApple OSS Distributions static uint32_t
_save_atm_diagnostic_flag(void)20*5c2921b0SApple OSS Distributions _save_atm_diagnostic_flag(void)
21*5c2921b0SApple OSS Distributions {
22*5c2921b0SApple OSS Distributions kern_return_t kr;
23*5c2921b0SApple OSS Distributions kr = host_get_atm_diagnostic_flag(mach_host_self(), &_original);
24*5c2921b0SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "host_get_atm_diagnostic_flag()");
25*5c2921b0SApple OSS Distributions T_LOG("Original ATM diagnostic flag: 0x%08x", _original);
26*5c2921b0SApple OSS Distributions return _original;
27*5c2921b0SApple OSS Distributions }
28*5c2921b0SApple OSS Distributions
29*5c2921b0SApple OSS Distributions static kern_return_t
_mutate_atm_diagnostic_flag(uint32_t v)30*5c2921b0SApple OSS Distributions _mutate_atm_diagnostic_flag(uint32_t v)
31*5c2921b0SApple OSS Distributions {
32*5c2921b0SApple OSS Distributions T_LOG("Try to set ATM diagnostic flag to: 0x%08x", v);
33*5c2921b0SApple OSS Distributions kern_return_t kr = host_set_atm_diagnostic_flag(mach_host_self(), v);
34*5c2921b0SApple OSS Distributions if (kr == KERN_SUCCESS) {
35*5c2921b0SApple OSS Distributions _needs_reset = true;
36*5c2921b0SApple OSS Distributions }
37*5c2921b0SApple OSS Distributions return kr;
38*5c2921b0SApple OSS Distributions }
39*5c2921b0SApple OSS Distributions
40*5c2921b0SApple OSS Distributions static void
_reset_atm_diagnostic_flag(void)41*5c2921b0SApple OSS Distributions _reset_atm_diagnostic_flag(void)
42*5c2921b0SApple OSS Distributions {
43*5c2921b0SApple OSS Distributions if (!_needs_reset) {
44*5c2921b0SApple OSS Distributions return;
45*5c2921b0SApple OSS Distributions }
46*5c2921b0SApple OSS Distributions T_LOG("Reset ATM diagnostic flag to: 0x%08x", _original);
47*5c2921b0SApple OSS Distributions kern_return_t kr;
48*5c2921b0SApple OSS Distributions kr = host_set_atm_diagnostic_flag(mach_host_self(), _original);
49*5c2921b0SApple OSS Distributions if (kr != KERN_SUCCESS) {
50*5c2921b0SApple OSS Distributions T_ASSERT_FAIL("host_set_atm_diagnostic_flag() failed: %s",
51*5c2921b0SApple OSS Distributions mach_error_string(kr));
52*5c2921b0SApple OSS Distributions }
53*5c2921b0SApple OSS Distributions }
54*5c2921b0SApple OSS Distributions
55*5c2921b0SApple OSS Distributions static void
_toggle_atm_diagnostic_flag(void)56*5c2921b0SApple OSS Distributions _toggle_atm_diagnostic_flag(void)
57*5c2921b0SApple OSS Distributions {
58*5c2921b0SApple OSS Distributions T_ATEND(_reset_atm_diagnostic_flag);
59*5c2921b0SApple OSS Distributions uint32_t f = _save_atm_diagnostic_flag();
60*5c2921b0SApple OSS Distributions f ^= LIBTRACE_PRIVATE_DATA;
61*5c2921b0SApple OSS Distributions kern_return_t kr = _mutate_atm_diagnostic_flag(f);
62*5c2921b0SApple OSS Distributions if (kr == KERN_NOT_SUPPORTED) {
63*5c2921b0SApple OSS Distributions T_SKIP("Seems ATM is disabled on this platform. "
64*5c2921b0SApple OSS Distributions "Ignoring host_set_atm_diagnostic_flag functionality. "
65*5c2921b0SApple OSS Distributions "Bailing gracefully.");
66*5c2921b0SApple OSS Distributions }
67*5c2921b0SApple OSS Distributions T_EXPECT_MACH_SUCCESS(kr, "Set atm_diagnostic_flag");
68*5c2921b0SApple OSS Distributions }
69*5c2921b0SApple OSS Distributions
70*5c2921b0SApple OSS Distributions T_DECL(atm_diagnostic_flag_entitled_privileged,
71*5c2921b0SApple OSS Distributions "change the atm_diagnostic_flag (entitled, privileged)",
72*5c2921b0SApple OSS Distributions T_META_ASROOT(true))
73*5c2921b0SApple OSS Distributions {
74*5c2921b0SApple OSS Distributions _toggle_atm_diagnostic_flag();
75*5c2921b0SApple OSS Distributions }
76*5c2921b0SApple OSS Distributions
77*5c2921b0SApple OSS Distributions T_DECL(atm_diagnostic_flag_entitled_unprivileged,
78*5c2921b0SApple OSS Distributions "change the atm_diagnostic_flag (entitled, unprivileged)",
79*5c2921b0SApple OSS Distributions T_META_ASROOT(false))
80*5c2921b0SApple OSS Distributions {
81*5c2921b0SApple OSS Distributions if (running_as_root()) {
82*5c2921b0SApple OSS Distributions drop_priv();
83*5c2921b0SApple OSS Distributions }
84*5c2921b0SApple OSS Distributions _toggle_atm_diagnostic_flag();
85*5c2921b0SApple OSS Distributions }
86