1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions * testthreadcall.cpp
3*1b191cb5SApple OSS Distributions * testkext
4*1b191cb5SApple OSS Distributions *
5*1b191cb5SApple OSS Distributions */
6*1b191cb5SApple OSS Distributions
7*1b191cb5SApple OSS Distributions #include "testthreadcall.h"
8*1b191cb5SApple OSS Distributions
9*1b191cb5SApple OSS Distributions #include <kern/thread_call.h>
10*1b191cb5SApple OSS Distributions #include <pexpert/pexpert.h>
11*1b191cb5SApple OSS Distributions
12*1b191cb5SApple OSS Distributions #define super IOService
13*1b191cb5SApple OSS Distributions OSDefineMetaClassAndStructors(testthreadcall, super);
14*1b191cb5SApple OSS Distributions
15*1b191cb5SApple OSS Distributions extern "C" {
16*1b191cb5SApple OSS Distributions static void thread_call_test_func(thread_call_param_t param0,
17*1b191cb5SApple OSS Distributions thread_call_param_t param1);
18*1b191cb5SApple OSS Distributions
19*1b191cb5SApple OSS Distributions static void thread_call_test_func2(thread_call_param_t param0,
20*1b191cb5SApple OSS Distributions thread_call_param_t param1);
21*1b191cb5SApple OSS Distributions }
22*1b191cb5SApple OSS Distributions
23*1b191cb5SApple OSS Distributions static int my_event;
24*1b191cb5SApple OSS Distributions
25*1b191cb5SApple OSS Distributions bool
start(IOService * provider)26*1b191cb5SApple OSS Distributions testthreadcall::start( IOService * provider )
27*1b191cb5SApple OSS Distributions {
28*1b191cb5SApple OSS Distributions boolean_t ret;
29*1b191cb5SApple OSS Distributions uint64_t deadline;
30*1b191cb5SApple OSS Distributions int sleepret;
31*1b191cb5SApple OSS Distributions uint32_t kernel_configuration;
32*1b191cb5SApple OSS Distributions
33*1b191cb5SApple OSS Distributions IOLog("%s\n", __PRETTY_FUNCTION__);
34*1b191cb5SApple OSS Distributions
35*1b191cb5SApple OSS Distributions if (!super::start(provider)) {
36*1b191cb5SApple OSS Distributions return false;
37*1b191cb5SApple OSS Distributions }
38*1b191cb5SApple OSS Distributions
39*1b191cb5SApple OSS Distributions kernel_configuration = PE_i_can_has_kernel_configuration();
40*1b191cb5SApple OSS Distributions IOLog("%s: Assertions %s\n", __PRETTY_FUNCTION__,
41*1b191cb5SApple OSS Distributions (kernel_configuration & kPEICanHasAssertions) ? "enabled" : "disabled");
42*1b191cb5SApple OSS Distributions IOLog("%s: Statistics %s\n", __PRETTY_FUNCTION__,
43*1b191cb5SApple OSS Distributions (kernel_configuration & kPEICanHasStatistics) ? "enabled" : "disabled");
44*1b191cb5SApple OSS Distributions IOLog("%s: Diagnostic API %s\n", __PRETTY_FUNCTION__,
45*1b191cb5SApple OSS Distributions (kernel_configuration & kPEICanHasDiagnosticAPI) ? "enabled" : "disabled");
46*1b191cb5SApple OSS Distributions
47*1b191cb5SApple OSS Distributions IOLog("Attempting thread_call_allocate\n");
48*1b191cb5SApple OSS Distributions tcall = thread_call_allocate(thread_call_test_func, this);
49*1b191cb5SApple OSS Distributions IOLog("thread_call_t %p\n", tcall);
50*1b191cb5SApple OSS Distributions
51*1b191cb5SApple OSS Distributions tlock = IOSimpleLockAlloc();
52*1b191cb5SApple OSS Distributions IOLog("tlock %p\n", tlock);
53*1b191cb5SApple OSS Distributions
54*1b191cb5SApple OSS Distributions clock_interval_to_deadline(5, NSEC_PER_SEC, &deadline);
55*1b191cb5SApple OSS Distributions IOLog("%d sec deadline is %llu\n", 5, deadline);
56*1b191cb5SApple OSS Distributions
57*1b191cb5SApple OSS Distributions ret = thread_call_enter_delayed(tcall, deadline);
58*1b191cb5SApple OSS Distributions
59*1b191cb5SApple OSS Distributions IOLog("Attempting thread_call_allocate\n");
60*1b191cb5SApple OSS Distributions tcall2 = thread_call_allocate(thread_call_test_func2, this);
61*1b191cb5SApple OSS Distributions IOLog("thread_call_t %p\n", tcall);
62*1b191cb5SApple OSS Distributions
63*1b191cb5SApple OSS Distributions tlock2 = IOLockAlloc();
64*1b191cb5SApple OSS Distributions IOLog("tlock2 %p\n", tlock2);
65*1b191cb5SApple OSS Distributions
66*1b191cb5SApple OSS Distributions clock_interval_to_deadline(2, NSEC_PER_SEC, &deadline);
67*1b191cb5SApple OSS Distributions IOLog("%d sec deadline is %llu\n", 2, deadline);
68*1b191cb5SApple OSS Distributions
69*1b191cb5SApple OSS Distributions ret = thread_call_enter_delayed(tcall2, deadline);
70*1b191cb5SApple OSS Distributions
71*1b191cb5SApple OSS Distributions IOLockLock(tlock2);
72*1b191cb5SApple OSS Distributions
73*1b191cb5SApple OSS Distributions clock_interval_to_deadline(3, NSEC_PER_SEC, &deadline);
74*1b191cb5SApple OSS Distributions IOLog("%d sec deadline is %llu\n", 3, deadline);
75*1b191cb5SApple OSS Distributions sleepret = IOLockSleepDeadline(tlock2, &my_event, deadline, THREAD_INTERRUPTIBLE);
76*1b191cb5SApple OSS Distributions IOLog("IOLockSleepDeadline(&my_event, %llu) returned %d, expected 0\n", deadline, sleepret);
77*1b191cb5SApple OSS Distributions
78*1b191cb5SApple OSS Distributions IOLockUnlock(tlock2);
79*1b191cb5SApple OSS Distributions
80*1b191cb5SApple OSS Distributions clock_interval_to_deadline(4, NSEC_PER_SEC, &deadline);
81*1b191cb5SApple OSS Distributions IOLog("%d sec deadline is %llu\n", 4, deadline);
82*1b191cb5SApple OSS Distributions
83*1b191cb5SApple OSS Distributions ret = thread_call_enter_delayed(tcall2, deadline);
84*1b191cb5SApple OSS Distributions
85*1b191cb5SApple OSS Distributions IOLockLock(tlock2);
86*1b191cb5SApple OSS Distributions
87*1b191cb5SApple OSS Distributions clock_interval_to_deadline(3, NSEC_PER_SEC, &deadline);
88*1b191cb5SApple OSS Distributions IOLog("%d sec deadline is %llu\n", 3, deadline);
89*1b191cb5SApple OSS Distributions sleepret = IOLockSleepDeadline(tlock2, &my_event, deadline, THREAD_INTERRUPTIBLE);
90*1b191cb5SApple OSS Distributions IOLog("IOLockSleepDeadline(&my_event, %llu) returned %d, expected 1\n", deadline, sleepret);
91*1b191cb5SApple OSS Distributions
92*1b191cb5SApple OSS Distributions IOLockUnlock(tlock2);
93*1b191cb5SApple OSS Distributions
94*1b191cb5SApple OSS Distributions return true;
95*1b191cb5SApple OSS Distributions }
96*1b191cb5SApple OSS Distributions
97*1b191cb5SApple OSS Distributions static void
thread_call_test_func(thread_call_param_t param0,thread_call_param_t param1)98*1b191cb5SApple OSS Distributions thread_call_test_func(thread_call_param_t param0,
99*1b191cb5SApple OSS Distributions thread_call_param_t param1)
100*1b191cb5SApple OSS Distributions {
101*1b191cb5SApple OSS Distributions testthreadcall *self = (testthreadcall *)param0;
102*1b191cb5SApple OSS Distributions
103*1b191cb5SApple OSS Distributions IOLog("thread_call_test_func %p %p\n", param0, param1);
104*1b191cb5SApple OSS Distributions
105*1b191cb5SApple OSS Distributions IOSimpleLockLock(self->tlock);
106*1b191cb5SApple OSS Distributions IOSimpleLockUnlock(self->tlock);
107*1b191cb5SApple OSS Distributions }
108*1b191cb5SApple OSS Distributions
109*1b191cb5SApple OSS Distributions static void
thread_call_test_func2(thread_call_param_t param0,thread_call_param_t param1)110*1b191cb5SApple OSS Distributions thread_call_test_func2(thread_call_param_t param0,
111*1b191cb5SApple OSS Distributions thread_call_param_t param1)
112*1b191cb5SApple OSS Distributions {
113*1b191cb5SApple OSS Distributions testthreadcall *self = (testthreadcall *)param0;
114*1b191cb5SApple OSS Distributions
115*1b191cb5SApple OSS Distributions IOLog("thread_call_test_func2 %p %p\n", param0, param1);
116*1b191cb5SApple OSS Distributions
117*1b191cb5SApple OSS Distributions IOLockWakeup(self->tlock2, &my_event, false);
118*1b191cb5SApple OSS Distributions }
119