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