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