-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuserclient.hpp
1150 lines (1031 loc) · 42.7 KB
/
userclient.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Kextgizmo IOUserClient helpers.
Dual-licensed under the MIT and zLib licenses.
Copyright 2013-2016 Phillip Dennis-Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Copyright (c) 2013-2016 Phillip Dennis-Jordan
This software is provided 'as-is', without any express or implied warranty. In
no event will the authors be held liable for any damages arising from the use
of this software.
Permission is granted to anyone to use this software for any purpose, including
commercial applications, and to alter it and redistribute it freely, subject
to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim
that you wrote the original software. If you use this software in a product,
an acknowledgment in the product documentation would be appreciated but is not
required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#pragma once
#include <IOKit/IOReturn.h>
#include <libkern/c++/OSObject.h>
#include <stdint.h>
#include <sys/types.h>
class IOMemoryMap;
/* I don't like casting function pointers, it's fragile as accidental prototype
* changes are not caught. External methods are expected to be of type
* IOExternalMethodAction. So wrap each member function in this shim with the
* correct type that forwards the arguments:
* &userclient_external_methods<SomeServiceUserClient>::external_method<&SomeServiceUserClient::startReceivingEvents>
* is of type IOExternalMethodAction, even though
* SomeServiceUserClient::startReceivingEvents isn't.
*
* Eventually, I'd like to use variadic templating to auto-generate the
* wrapping/unwrapping?
*/
namespace
{
IOReturn map_struct_arguments(IOMemoryMap*& in_map, IOMemoryMap*& out_map, IOExternalMethodArguments* arguments)
{
if (arguments->structureInputSize == 0 && arguments->structureInputDescriptor != nullptr)
{
in_map = arguments->structureInputDescriptor->createMappingInTask(kernel_task, 0, kIOMapAnywhere | kIOMapReadOnly);
if (in_map != nullptr)
{
arguments->structureInputSize = static_cast<uint32_t>(in_map->getLength());
arguments->structureInput = reinterpret_cast<const void*>(in_map->getAddress());
kprintf("mapped struct input at %p, %u\n", arguments->structureInput, arguments->structureInputSize);
}
else
{
kprintf("mapping struct input failed\n");
}
}
if (arguments->structureOutputSize == 0 && arguments->structureOutputDescriptor != nullptr)
{
out_map = arguments->structureOutputDescriptor->createMappingInTask(kernel_task, 0, kIOMapAnywhere);
if (out_map != nullptr)
{
arguments->structureOutputSize = static_cast<uint32_t>(out_map->getLength());
arguments->structureOutput = reinterpret_cast<void*>(out_map->getAddress());
kprintf("mapped struct output at %p, %u\n", arguments->structureOutput, arguments->structureOutputSize);
}
else
{
kprintf("mapping struct output failed");
}
}
return kIOReturnSuccess;
}
template <class UCC> struct userclient_external_methods
{
template <IOReturn (UCC::*method)(void* reference, IOExternalMethodArguments* arguments)>
static IOReturn external_method(OSObject* target, void* reference, IOExternalMethodArguments* arguments)
{
UCC* uc = OSDynamicCast(UCC, target);
if (!uc)
return kIOReturnBadArgument;
return (uc->*method)(reference, arguments);
}
};
#if (__cplusplus >= 201103L) || __has_feature(cxx_variadic_templates)
template<typename... Args> struct scalar_input_arg_count;
// termination condition
template<>
struct scalar_input_arg_count<> {
static const unsigned count = 0;
};
// ignore output scalar
template<typename... Args>
struct scalar_input_arg_count<uint64_t*, Args...> {
static const int count = scalar_input_arg_count<Args...>::count;
};
// ignore variable sized input struct
template<typename... Args>
struct scalar_input_arg_count<const void*, size_t, Args...> {
static const int count = scalar_input_arg_count<Args...>::count;
};
// ignore variable sized output struct
template<typename... Args>
struct scalar_input_arg_count<void*, size_t, Args...> {
static const int count = scalar_input_arg_count<Args...>::count;
};
// ignore output struct
template<typename T, typename... Args>
struct scalar_input_arg_count<T*, Args...> {
static const int count = scalar_input_arg_count<Args...>::count;
};
// ignore the async argument triple
template<typename... Args>
struct scalar_input_arg_count<mach_port_t, io_user_reference_t*, uint32_t, Args...> {
static const int count = scalar_input_arg_count<Args...>::count;
};
// everything else must be a scalar input
template<typename T, typename... Args>
struct scalar_input_arg_count<T, Args...> {
static const int count = 1 + scalar_input_arg_count<Args...>::count;
};
template<typename... Args> struct struct_input_arg_size;
template<>
struct struct_input_arg_size<> {
static const unsigned size = 0;
};
template<typename... Args>
struct struct_input_arg_size<const void*, size_t, Args...> {
static_assert(struct_input_arg_size<Args...>::size == 0,"Only one struct input allowed.");
static const int size = kIOUCVariableStructureSize;
};
template<typename T, typename... Args>
struct struct_input_arg_size<const T*, Args...> {
static_assert(struct_input_arg_size<Args...>::size == 0,"Only one struct input allowed.");
static const int size = sizeof(T);
static_assert(size != 0,"zero-sized input structs not allowed");
};
template<typename T, typename... Args>
struct struct_input_arg_size<T, Args...> {
static const int size = struct_input_arg_size<Args...>::size;
};
template<typename... Args> struct struct_output_arg_size;
template<>
struct struct_output_arg_size<> {
static const unsigned size = 0;
};
template<typename... Args>
struct struct_output_arg_size<const void*, size_t, Args...> {
static const int size = struct_output_arg_size<Args...>::size;
};
template<typename T, typename... Args>
struct struct_output_arg_size<const T*, Args...> {
static const int size = struct_output_arg_size<Args...>::size;
};
// ignore the async argument triple
template<typename... Args>
struct struct_output_arg_size<mach_port_t, io_user_reference_t*, uint32_t, Args...> {
static const int size = struct_output_arg_size<Args...>::size;
};
template<typename... Args>
struct struct_output_arg_size<void*, size_t, Args...> {
static_assert(struct_output_arg_size<Args...>::size == 0,"Only one struct output allowed.");
static const int size = kIOUCVariableStructureSize;
};
template<typename T, typename... Args>
struct struct_output_arg_size<T*, Args...> {
static_assert(struct_output_arg_size<Args...>::size == 0,"Only one struct output allowed.");
static const int size = sizeof(T);
static_assert(size != 0, "No zero-sized output struct types");
};
template<typename... Args>
struct struct_output_arg_size<uint64_t*, Args...> {
static const int size = struct_output_arg_size<Args...>::size;
};
template<typename T, typename... Args>
struct struct_output_arg_size<T, Args...> {
static const int size = struct_output_arg_size<Args...>::size;
};
template<typename... Args> struct scalar_output_arg_count;
template<>
struct scalar_output_arg_count<> {
static const unsigned count = 0;
};
template<typename... Args>
struct scalar_output_arg_count<uint64_t*, Args...> {
static const int count = 1 + scalar_output_arg_count<Args...>::count;
};
template<typename T, typename... Args>
struct scalar_output_arg_count<T, Args...> {
static const int count = scalar_output_arg_count<Args...>::count;
};
// ignore the async argument triple
template<typename... Args>
struct scalar_output_arg_count<mach_port_t, io_user_reference_t*, uint32_t, Args...> {
static const int count = scalar_output_arg_count<Args...>::count;
};
template<typename... Args> struct is_async_method;
// termination condition for non-async methods
template<> struct is_async_method<> {
static const bool value = false;
};
template<typename... Args> struct is_async_method<mach_port_t, io_user_reference_t*, uint32_t, Args...> {
static const bool value = true;
};
template<typename T, typename... Args> struct is_async_method<T, Args...> {
static const bool value = is_async_method<Args...>::value;
};
#endif
/* We want the wrapped function call to expand to something like this:
* return (target->*METHOD)(
* static_cast<int>(arguments->scalarInput[0]),
* static_cast<enum blah>(arguments->scalarInput[1]));
*
* Or something like that. Whatever matches the argument types of the method.
* C++ lets you iteratively apply a template function for each item in a
* type or value pack (typename... Typepack, or int... Valuepack).
* So given a PACK where each item encapsulates the necessary information for
* get_element<>(), our call can look like this:
* return (target->*METHOD)(
* get_element<PACK>(arguments->scalarInput) ...);
*
* So let's define get_element such that its template argument is an argument
* selector type which encapsulates the argument type, and the index in the
* inputs array, which we'll call arg_sel:
*/
template <typename ARGSEL>
static typename ARGSEL::type get_element(const IOExternalMethodArguments* arguments)
{
return static_cast<typename ARGSEL::type>(ARGSEL::extract_argument(arguments));
}
template <typename T, int I>
struct arg_sel_scalar_input
{
typedef T type;
static const int index = I;
static uint64_t extract_argument(const IOExternalMethodArguments* arguments)
{
return arguments->scalarInput[I];
}
};
template <int I>
struct arg_sel_scalar_output
{
typedef uint64_t* type;
static const int index = I;
static uint64_t* extract_argument(const IOExternalMethodArguments* arguments)
{
return &arguments->scalarOutput[I];
}
};
struct arg_sel_variable_struct_input_ptr
{
typedef const void* type;
static const void* extract_argument(const IOExternalMethodArguments* arguments)
{
return arguments->structureInput;
}
};
template <typename T>
struct arg_sel_fixed_struct_input_ptr
{
typedef const T* type;
static const void* extract_argument(const IOExternalMethodArguments* arguments)
{
return arguments->structureInput;
}
};
struct arg_sel_variable_struct_input_size
{
typedef size_t type;
static size_t extract_argument(const IOExternalMethodArguments* arguments)
{
return arguments->structureInputSize;
}
};
template <typename T>
struct arg_sel_fixed_struct_output_ptr
{
typedef T* type;
static_assert(sizeof(T) > 0,"");
static void* extract_argument(const IOExternalMethodArguments* arguments)
{
return arguments->structureOutput;
}
};
struct arg_sel_variable_struct_output_ptr
{
typedef void* type;
static void* extract_argument(const IOExternalMethodArguments* arguments)
{
kprintf("arg_sel_variable_struct_output_ptr: %p\n", arguments->structureOutput);
return arguments->structureOutput;
}
};
struct arg_sel_variable_struct_output_size
{
typedef size_t type;
static size_t extract_argument(const IOExternalMethodArguments* arguments)
{
kprintf("arg_sel_variable_struct_output_size: %u\n", arguments->structureOutputSize);
return arguments->structureOutputSize;
}
};
struct arg_sel_async_mach_port
{
typedef mach_port_t type;
static mach_port_t extract_argument(const IOExternalMethodArguments* arguments)
{
return arguments->asyncWakePort;
}
};
struct arg_sel_async_ref
{
typedef io_user_reference_t* type;
static io_user_reference_t* extract_argument(const IOExternalMethodArguments* arguments)
{
return arguments->asyncReference;
}
};
struct arg_sel_async_ref_count
{
typedef uint32_t type;
static uint32_t extract_argument(const IOExternalMethodArguments* arguments)
{
return arguments->asyncReferenceCount;
}
};
/* That leaves us with the problem of generating a pack of arg_sel types that
* corresponds to our wrapped method's parameters.
*
* We define a template gen_arg_seq, parameterised only on the original
* parameter pack, which uses an accumulator type that recursively adds
* items to an arg_seq<> whose template argument is indeed a pack of arg_sel
* types.
* The accumulator keeps track the index of the next array element, as well
* as the remaining parameter types, with the completion condition of
* no remaining parameters.
*/
#if __cplusplus >= 201103L || __has_feature(cxx_variadic_templates)
// dummy type that we only use for its type pack. (which will be a sequence of arg_sel types)
template <typename... ARGSELS> struct arg_seq
{};
// Recursively invoked type; result will be in the 'seq' member type.
template <int NEXT_INPUT, int NEXT_OUTPUT, typename ARG_SEQ, typename... REMAIN_ARGS>
struct arg_seq_accum;
// The recurrence relation; current argument has type T and index NEXT_INPUT
template <int NEXT_INPUT, int NEXT_OUTPUT, typename... ARGSELS, typename T, typename... REMAIN_ARGS>
struct arg_seq_accum<NEXT_INPUT, NEXT_OUTPUT, arg_seq<ARGSELS...>, T, REMAIN_ARGS...>
{
typedef typename arg_seq_accum<
NEXT_INPUT + 1, // increment index
NEXT_OUTPUT,
arg_seq<ARGSELS..., arg_sel_scalar_input<T, NEXT_INPUT> >, // create a new arg_sel and append it to the existing ones
REMAIN_ARGS... // Drop 'T' from parameters
>::seq seq;
};
// The recurrence relation for fixed-sized struct inputs
template <int NEXT_INPUT, int NEXT_OUTPUT, typename T, typename... ARGSELS, typename... REMAIN_ARGS>
struct arg_seq_accum<NEXT_INPUT, NEXT_OUTPUT, arg_seq<ARGSELS...>, const T*, REMAIN_ARGS...>
{
typedef typename arg_seq_accum<
NEXT_INPUT,
NEXT_OUTPUT,
arg_seq<ARGSELS..., arg_sel_fixed_struct_input_ptr<T> >, // create a new arg_sel and append it to the existing ones
REMAIN_ARGS... // Drop 'const T*' from parameters
>::seq seq;
};
// The recurrence relation for variable-sized struct inputs
template <int NEXT_INPUT, int NEXT_OUTPUT, typename... ARGSELS, typename... REMAIN_ARGS>
struct arg_seq_accum<NEXT_INPUT, NEXT_OUTPUT, arg_seq<ARGSELS...>, const void*, size_t, REMAIN_ARGS...>
{
typedef typename arg_seq_accum<
NEXT_INPUT,
NEXT_OUTPUT,
arg_seq<ARGSELS..., arg_sel_variable_struct_input_ptr, arg_sel_variable_struct_input_size>, // create a new arg_sel and append it to the existing ones
REMAIN_ARGS... // Drop 'T' from parameters
>::seq seq;
};
// The recurrence relation for variable-sized struct outputs
template <int NEXT_INPUT, int NEXT_OUTPUT, typename... ARGSELS, typename... REMAIN_ARGS>
struct arg_seq_accum<NEXT_INPUT, NEXT_OUTPUT, arg_seq<ARGSELS...>, void*, size_t, REMAIN_ARGS...>
{
typedef typename arg_seq_accum<
NEXT_INPUT,
NEXT_OUTPUT,
arg_seq<ARGSELS..., arg_sel_variable_struct_output_ptr, arg_sel_variable_struct_output_size>, // create a new arg_sel and append it to the existing ones
REMAIN_ARGS... // Drop 'T' from parameters
>::seq seq;
};
// The recurrence relation for the async triple
template <int NEXT_INPUT, int NEXT_OUTPUT, typename... ARGSELS, typename... REMAIN_ARGS>
struct arg_seq_accum<NEXT_INPUT, NEXT_OUTPUT, arg_seq<ARGSELS...>, mach_port_t, io_user_reference_t*, uint32_t, REMAIN_ARGS...>
{
typedef typename arg_seq_accum<
NEXT_INPUT,
NEXT_OUTPUT,
arg_seq<ARGSELS..., arg_sel_async_mach_port, arg_sel_async_ref, arg_sel_async_ref_count>, // create a new arg_sel and append it to the existing ones
REMAIN_ARGS... // Drop the triple from parameters
>::seq seq;
};
// The recurrence relation for scalar outputs
template <int NEXT_INPUT, int NEXT_OUTPUT, typename... ARGSELS, typename... REMAIN_ARGS>
struct arg_seq_accum<NEXT_INPUT, NEXT_OUTPUT, arg_seq<ARGSELS...>, uint64_t*, REMAIN_ARGS...>
{
typedef typename arg_seq_accum<
NEXT_INPUT,
NEXT_OUTPUT + 1, // increment index
arg_seq<ARGSELS..., arg_sel_scalar_output<NEXT_OUTPUT> >, // create a new arg_sel and append it to the existing ones
REMAIN_ARGS... // Drop 'T' from parameters
>::seq seq;
};
// The recurrence relation for fixed-sized struct outputs
template <int NEXT_INPUT, int NEXT_OUTPUT, typename T, typename... ARGSELS, typename... REMAIN_ARGS>
struct arg_seq_accum<NEXT_INPUT, NEXT_OUTPUT, arg_seq<ARGSELS...>, T*, REMAIN_ARGS...>
{
typedef typename arg_seq_accum<
NEXT_INPUT,
NEXT_OUTPUT,
arg_seq<ARGSELS..., arg_sel_fixed_struct_output_ptr<T> >, // create a new arg_sel and append it to the existing ones
REMAIN_ARGS... // Drop 'T' from parameters
>::seq seq;
};
// Termination condition
template <int NEXT_INPUT, int NEXT_OUTPUT, typename... ARGSELS>
struct arg_seq_accum<NEXT_INPUT, NEXT_OUTPUT, arg_seq<ARGSELS...> /* note: no remaining args */>
{
typedef arg_seq<ARGSELS...> seq; // our arg_sel sequence is complete, wrapped in a type
};
// This is really just a convenience for starting the accumulator off with the right initial conditions
template <typename... ARGS> struct gen_arg_seq
{
// start with an empty arg_sel sequence, index 0, and all parameters remaining
typedef typename arg_seq_accum<0, 0, arg_seq<>, ARGS...>::seq type;
};
template <typename MethodPointerSignature, MethodPointerSignature METHOD> struct userclient_method;
template <class UCC, typename... Args, IOReturn(UCC::*METHOD)(Args...)>
struct userclient_method<IOReturn(UCC::*)(Args...), METHOD>
{
static constexpr const unsigned inputs = scalar_input_arg_count<Args...>::count;
static constexpr const unsigned outputs = scalar_output_arg_count<Args...>::count;
static constexpr const uint32_t struct_input_size = struct_input_arg_size<Args...>::size;
static constexpr const uint32_t struct_output_size = struct_output_arg_size<Args...>::size;
static constexpr const bool is_async = is_async_method<Args...>::value;
template <typename... ARGSELS>
static IOReturn apply_fn(UCC* target, IOExternalMethodArguments* arguments, arg_seq<ARGSELS...>)
{
IOMemoryMap* in_map = nullptr;
IOMemoryMap* out_map = nullptr;
IOReturn result = map_struct_arguments(in_map, out_map, arguments);
if (result != kIOReturnSuccess)
return result;
result = (target->*METHOD)(get_element<ARGSELS>(arguments) ...);
OSSafeReleaseNULL(in_map);
OSSafeReleaseNULL(out_map);
return result;
}
static IOReturn external_method(OSObject* target, void* reference, IOExternalMethodArguments* arguments)
{
UCC* uc = OSDynamicCast(UCC, target);
if (!uc)
return kIOReturnBadArgument;
if (is_async_method<Args...>::value && (arguments->asyncWakePort == MACH_PORT_NULL || arguments->asyncReference == nullptr || arguments->asyncReferenceCount == 0))
return kIOReturnBadArgument;
return apply_fn(uc, arguments, typename gen_arg_seq<Args...>::type());
}
#if __cplusplus >= 201103L
constexpr static const IOExternalMethodDispatch dispatch = {
external_method,
inputs,
struct_input_size,
outputs,
struct_output_size
};
#else
template <IOExternalMethodAction action, uint32_t num_inputs, uint32_t num_outputs, uint32_t input_size, uint32_t output_size>
struct IOExternalMethodDispatchGenerator
{
constexpr operator IOExternalMethodDispatch() const
{
return (IOExternalMethodDispatch){ action, num_inputs, input_size, num_outputs, output_size };
}
};
static const IOExternalMethodDispatchGenerator<external_method, inputs, outputs, struct_input_size, struct_output_size> dispatch;
#endif
};
#else
template <typename MethodPointerSignature, MethodPointerSignature METHOD> struct userclient_method;
struct EmptyTypeList;
template <typename T, typename REST>
struct TypeListItem
{
typedef T first;
typedef REST rest;
};
template <typename ArgumentTypeList> struct scalar_input_arg_count;
// termination condition
template <> struct scalar_input_arg_count<EmptyTypeList>
{
static const unsigned count = 0;
};
// ignore output scalar
template<typename REST>
struct scalar_input_arg_count<TypeListItem<uint64_t*, REST> > {
static const int count = scalar_input_arg_count<REST>::count;
};
// ignore variable sized input struct
template<typename REST>
struct scalar_input_arg_count<TypeListItem<const void*, TypeListItem<size_t, REST> > > {
static const int count = scalar_input_arg_count<REST>::count;
};
// ignore variable sized output struct
template<typename REST>
struct scalar_input_arg_count<TypeListItem<void*, TypeListItem<size_t, REST > > > {
static const int count = scalar_input_arg_count<REST>::count;
};
// ignore output struct
template<typename STRUCT_T, typename REST>
struct scalar_input_arg_count<TypeListItem<STRUCT_T*, REST> > {
static const int count = scalar_input_arg_count<REST>::count;
};
// ignore the async argument triple
template<typename REST>
struct scalar_input_arg_count<TypeListItem<mach_port_t, TypeListItem<io_user_reference_t*, TypeListItem<uint32_t, REST> > > > {
static const int count = scalar_input_arg_count<REST>::count;
};
// everything else must be a scalar input
template<typename T, typename REST>
struct scalar_input_arg_count<TypeListItem<T, REST> > {
static const int count = 1 + scalar_input_arg_count<REST>::count;
};
template <typename ArgumentTypeList> struct scalar_output_arg_count;
template <> struct scalar_output_arg_count<EmptyTypeList>
{
static const unsigned count = 0;
};
template<typename REST>
struct scalar_output_arg_count<TypeListItem<uint64_t*, REST> > {
static const int count = 1 + scalar_output_arg_count<REST>::count;
};
template<typename T, typename REST>
struct scalar_output_arg_count<TypeListItem<T, REST> > {
static const int count = scalar_output_arg_count<REST>::count;
};
// ignore the async argument triple
template<typename REST>
struct scalar_output_arg_count<TypeListItem<mach_port_t, TypeListItem<io_user_reference_t*, TypeListItem<uint32_t, REST> > > > {
static const int count = scalar_output_arg_count<REST>::count;
};
template<typename REST> struct is_async_method;
// termination condition for non-async methods
template<> struct is_async_method<EmptyTypeList> {
static const bool value = false;
};
template<typename REST> struct is_async_method<TypeListItem<mach_port_t, TypeListItem<io_user_reference_t*, TypeListItem<uint32_t, REST> > > > {
static const bool value = true;
};
template<typename T, typename REST> struct is_async_method<TypeListItem<T, REST> > {
static const bool value = is_async_method<REST>::value;
};
template<typename ArgumentTypeList> struct struct_input_arg_size;
template<>
struct struct_input_arg_size<EmptyTypeList> {
static const unsigned size = 0;
};
template<typename RestTypeList>
struct struct_input_arg_size<TypeListItem<const void*, TypeListItem<size_t, RestTypeList> > > {
static_assert(struct_input_arg_size<RestTypeList>::size == 0,"Only one struct input allowed.");
static const int size = kIOUCVariableStructureSize;
};
template<typename T, typename RestTypeList>
struct struct_input_arg_size<TypeListItem<const T*, RestTypeList> > {
static_assert(struct_input_arg_size<RestTypeList>::size == 0,"Only one struct input allowed.");
static const int size = sizeof(T);
static_assert(size != 0,"zero-sized input structs not allowed");
};
template<typename T, typename RestTypeList>
struct struct_input_arg_size<TypeListItem<T, RestTypeList> > {
static const int size = struct_input_arg_size<RestTypeList>::size;
};
template<typename ArgTypeList> struct struct_output_arg_size;
template<>
struct struct_output_arg_size<EmptyTypeList> {
static const unsigned size = 0;
};
template<typename RestArgTypeList>
struct struct_output_arg_size<TypeListItem<const void*, TypeListItem<size_t, RestArgTypeList> > > {
static const int size = struct_output_arg_size<RestArgTypeList>::size;
};
template<typename T, typename RestArgTypeList>
struct struct_output_arg_size<TypeListItem<const T*, RestArgTypeList> > {
static const int size = struct_output_arg_size<RestArgTypeList>::size;
};
// ignore the async argument triple
template<typename RestArgTypeList>
struct struct_output_arg_size<TypeListItem<mach_port_t, TypeListItem<io_user_reference_t*, TypeListItem<uint32_t, RestArgTypeList> > > > {
static const int size = struct_output_arg_size<RestArgTypeList>::size;
};
template<typename RestArgTypeList>
struct struct_output_arg_size<TypeListItem<void*, TypeListItem<size_t, RestArgTypeList> > > {
static_assert(struct_output_arg_size<RestArgTypeList>::size == 0,"Only one struct output allowed.");
static const int size = kIOUCVariableStructureSize;
};
template<typename T, typename RestArgTypeList>
struct struct_output_arg_size<TypeListItem<T*, RestArgTypeList> > {
static_assert(struct_output_arg_size<RestArgTypeList>::size == 0,"Only one struct output allowed.");
static const int size = sizeof(T);
static_assert(size != 0, "No zero-sized output struct types");
};
template<typename RestArgTypeList>
struct struct_output_arg_size<TypeListItem<uint64_t*, RestArgTypeList> > {
static const int size = struct_output_arg_size<RestArgTypeList>::size;
};
template<typename T, typename RestArgTypeList>
struct struct_output_arg_size<TypeListItem<T, RestArgTypeList> > {
static const int size = struct_output_arg_size<RestArgTypeList>::size;
};
// Recursively invoked type; result will be in the 'seq' member type.
template <int NEXT_INPUT, int NEXT_OUTPUT, typename ARG_SEQ, typename REMAIN_ARGLIST>
struct arg_seq_accum;
// The recurrence relation; current argument has type T and index NEXT_INPUT
template <int NEXT_INPUT, int NEXT_OUTPUT, typename ARGSELS, typename T, typename REMAIN_ARGLIST>
struct arg_seq_accum<NEXT_INPUT, NEXT_OUTPUT, ARGSELS, TypeListItem<T, REMAIN_ARGLIST> >
{
typedef typename arg_seq_accum<
NEXT_INPUT + 1, // increment index
NEXT_OUTPUT,
TypeListItem<arg_sel_scalar_input<T, NEXT_INPUT>, ARGSELS>, // create a new arg_sel and prepend it to the existing ones
REMAIN_ARGLIST // Drop 'T' from parameters
>::seq seq;
};
// The recurrence relation for fixed-sized struct inputs
template <int NEXT_INPUT, int NEXT_OUTPUT, typename T, typename ARGSELS, typename REMAIN_ARGS>
struct arg_seq_accum<NEXT_INPUT, NEXT_OUTPUT, ARGSELS, TypeListItem<const T*, REMAIN_ARGS> >
{
typedef typename arg_seq_accum<
NEXT_INPUT,
NEXT_OUTPUT,
TypeListItem<arg_sel_fixed_struct_input_ptr<T>, ARGSELS>, // create a new arg_sel and append it to the existing ones
REMAIN_ARGS // Drop 'const T*' from parameters
>::seq seq;
};
// The recurrence relation for variable-sized struct inputs
template <int NEXT_INPUT, int NEXT_OUTPUT, typename ARGSELS, typename REMAIN_ARGS>
struct arg_seq_accum<NEXT_INPUT, NEXT_OUTPUT, ARGSELS, TypeListItem<const void*, TypeListItem<size_t, REMAIN_ARGS> > >
{
typedef typename arg_seq_accum<
NEXT_INPUT,
NEXT_OUTPUT,
TypeListItem<arg_sel_variable_struct_input_size, TypeListItem<arg_sel_variable_struct_input_ptr, ARGSELS> >, // create a new arg_sel and append it to the existing ones
REMAIN_ARGS // Drop 'T' from parameters
>::seq seq;
};
// The recurrence relation for variable-sized struct outputs
template <int NEXT_INPUT, int NEXT_OUTPUT, typename ARGSELS, typename REMAIN_ARGS>
struct arg_seq_accum<NEXT_INPUT, NEXT_OUTPUT, ARGSELS, TypeListItem<void*, TypeListItem<size_t, REMAIN_ARGS> > >
{
typedef typename arg_seq_accum<
NEXT_INPUT,
NEXT_OUTPUT,
TypeListItem<arg_sel_variable_struct_output_size, TypeListItem<arg_sel_variable_struct_output_ptr, ARGSELS> >, // create a new arg_sel and append it to the existing ones
REMAIN_ARGS // Drop 'T' from parameters
>::seq seq;
};
// The recurrence relation for the async triple
template <int NEXT_INPUT, int NEXT_OUTPUT, typename ARGSELS, typename REMAIN_ARGS>
struct arg_seq_accum<NEXT_INPUT, NEXT_OUTPUT, ARGSELS, TypeListItem<mach_port_t, TypeListItem<io_user_reference_t*, TypeListItem<uint32_t, REMAIN_ARGS> > > >
{
typedef typename arg_seq_accum<
NEXT_INPUT,
NEXT_OUTPUT,
TypeListItem<arg_sel_async_ref_count, TypeListItem<arg_sel_async_ref, TypeListItem<arg_sel_async_mach_port, ARGSELS> > >, // create a new arg_sel and append it to the existing ones
REMAIN_ARGS // Drop the triple from parameters
>::seq seq;
};
// The recurrence relation for scalar outputs
template <int NEXT_INPUT, int NEXT_OUTPUT, typename ARGSELS, typename REMAIN_ARGS>
struct arg_seq_accum<NEXT_INPUT, NEXT_OUTPUT, ARGSELS, TypeListItem<uint64_t*, REMAIN_ARGS> >
{
typedef typename arg_seq_accum<
NEXT_INPUT,
NEXT_OUTPUT + 1, // increment index
TypeListItem<arg_sel_scalar_output<NEXT_OUTPUT>, ARGSELS>, // create a new arg_sel and append it to the existing ones
REMAIN_ARGS // Drop 'T' from parameters
>::seq seq;
};
// The recurrence relation for fixed-sized struct outputs
template <int NEXT_INPUT, int NEXT_OUTPUT, typename T, typename ARGSELS, typename REMAIN_ARGS>
struct arg_seq_accum<NEXT_INPUT, NEXT_OUTPUT, ARGSELS, TypeListItem<T*, REMAIN_ARGS> >
{
typedef typename arg_seq_accum<
NEXT_INPUT,
NEXT_OUTPUT,
TypeListItem<arg_sel_fixed_struct_output_ptr<T>, ARGSELS>, // create a new arg_sel and append it to the existing ones
REMAIN_ARGS // Drop 'T' from parameters
>::seq seq;
};
// Termination condition
template <int NEXT_INPUT, int NEXT_OUTPUT, typename ARGSELS>
struct arg_seq_accum<NEXT_INPUT, NEXT_OUTPUT, ARGSELS, EmptyTypeList>
{
typedef ARGSELS seq;
};
template <typename TYPELIST, typename REVERSED = EmptyTypeList> struct reverse_typelist;
template <typename REVERSED> struct reverse_typelist<EmptyTypeList, REVERSED>
{
typedef REVERSED type;
};
template <typename T, typename REST, typename REVERSED> struct reverse_typelist<TypeListItem<T, REST>, REVERSED>
{
typedef typename reverse_typelist<REST, TypeListItem<T, REVERSED> >::type type;
};
// This is really just a convenience for starting the accumulator off with the right initial conditions
template <typename ARGS> struct gen_arg_seq
{
// start with an empty arg_sel sequence, index 0, and all parameters remaining
typedef typename reverse_typelist<typename arg_seq_accum<0, 0, EmptyTypeList, ARGS>::seq>::type type;
};
template <typename TYPE_LIST, unsigned index> struct TypeListItemIndex;
template <typename T, typename REST_TYPE_LIST> struct TypeListItemIndex<TypeListItem<T, REST_TYPE_LIST>, 0>
{
typedef T type;
};
template <typename T, typename REST, unsigned index> struct TypeListItemIndex<TypeListItem<T, REST>, index>
{
typedef typename TypeListItemIndex<REST, index - 1>::type type;
};
template <class UCC, IOReturn(UCC::*METHOD)()>
struct userclient_method<IOReturn(UCC::*)(), METHOD>
{
typedef EmptyTypeList argument_types;
typedef typename gen_arg_seq<argument_types>::type gen_args;
static const unsigned inputs = scalar_input_arg_count<argument_types>::count;
static const unsigned outputs = scalar_output_arg_count<argument_types>::count;
static const uint32_t struct_input_size = struct_input_arg_size<argument_types>::size;
static const uint32_t struct_output_size = struct_output_arg_size<argument_types>::size;
static const bool is_async = is_async_method<argument_types>::value;
static IOReturn apply_fn(UCC* target, IOExternalMethodArguments* arguments)
{
IOMemoryMap* in_map = nullptr;
IOMemoryMap* out_map = nullptr;
IOReturn result = map_struct_arguments(in_map, out_map, arguments);
if (result != kIOReturnSuccess)
return result;
result = (target->*METHOD)();
OSSafeReleaseNULL(in_map);
OSSafeReleaseNULL(out_map);
return result;
}
static IOReturn external_method(OSObject* target, void* reference, IOExternalMethodArguments* arguments)
{
UCC* uc = OSDynamicCast(UCC, target);
if (!uc)
return kIOReturnBadArgument;
if (is_async_method<argument_types>::value && (arguments->asyncWakePort == MACH_PORT_NULL || arguments->asyncReference == nullptr || arguments->asyncReferenceCount == 0))
return kIOReturnBadArgument;
return apply_fn(uc, arguments);
}
static IOExternalMethodDispatch get_dispatch()
{
return (IOExternalMethodDispatch){ external_method, inputs, 0, outputs, 0 };
}
};
template <class UCC, typename AT1, IOReturn(UCC::*METHOD)(AT1)>
struct userclient_method<IOReturn(UCC::*)(AT1), METHOD>
{
typedef TypeListItem<AT1, EmptyTypeList> argument_types;
typedef typename gen_arg_seq<argument_types>::type gen_args;
static const unsigned inputs = scalar_input_arg_count<argument_types>::count;
static const unsigned outputs = scalar_output_arg_count<argument_types>::count;
static const uint32_t struct_input_size = struct_input_arg_size<argument_types>::size;
static const uint32_t struct_output_size = struct_output_arg_size<argument_types>::size;
static const bool is_async = is_async_method<argument_types>::value;
static IOReturn apply_fn(UCC* target, IOExternalMethodArguments* arguments)
{
IOMemoryMap* in_map = nullptr;
IOMemoryMap* out_map = nullptr;
IOReturn result = map_struct_arguments(in_map, out_map, arguments);
if (result != kIOReturnSuccess)
return result;
result = (target->*METHOD)(get_element<typename TypeListItemIndex<gen_args, 0>::type>(arguments));
OSSafeReleaseNULL(in_map);
OSSafeReleaseNULL(out_map);
return result;
}
static IOReturn external_method(OSObject* target, void* reference, IOExternalMethodArguments* arguments)
{
UCC* uc = OSDynamicCast(UCC, target);
if (!uc)
return kIOReturnBadArgument;
if (is_async_method<argument_types>::value && (arguments->asyncWakePort == MACH_PORT_NULL || arguments->asyncReference == nullptr || arguments->asyncReferenceCount == 0))
return kIOReturnBadArgument;
return apply_fn(uc, arguments);
}
static IOExternalMethodDispatch get_dispatch()
{
return (IOExternalMethodDispatch){ external_method, inputs, struct_input_size, outputs, struct_output_size };
}
};
#define UC_METHOD_PROPERTY_DEFS \
typedef typename gen_arg_seq<argument_types>::type gen_args; \
static const unsigned inputs = scalar_input_arg_count<argument_types>::count; \
static const unsigned outputs = scalar_output_arg_count<argument_types>::count; \
static const uint32_t struct_input_size = struct_input_arg_size<argument_types>::size; \
static const uint32_t struct_output_size = struct_output_arg_size<argument_types>::size; \
static const bool is_async = is_async_method<argument_types>::value;
#define UC_METHOD_APPLY_DEF(...) \
static IOReturn apply_fn(UCC* target, IOExternalMethodArguments* arguments) \
{ \
IOMemoryMap* in_map = nullptr; \
IOMemoryMap* out_map = nullptr; \
IOReturn result = map_struct_arguments(in_map, out_map, arguments); \
if (result != kIOReturnSuccess) \
return result; \
result = (target->*METHOD)(__VA_ARGS__); \
OSSafeReleaseNULL(in_map); \
OSSafeReleaseNULL(out_map); \
return result; \
} \
static IOReturn external_method(OSObject* target, void* reference, IOExternalMethodArguments* arguments) \
{ \
UCC* uc = OSDynamicCast(UCC, target); \
if (!uc) \
return kIOReturnBadArgument; \
if (is_async_method<argument_types>::value && (arguments->asyncWakePort == MACH_PORT_NULL || arguments->asyncReference == nullptr || arguments->asyncReferenceCount == 0)) \
return kIOReturnBadArgument; \
return apply_fn(uc, arguments); \
} \
static IOExternalMethodDispatch get_dispatch() \
{ \
return (IOExternalMethodDispatch){ external_method, inputs, struct_input_size, outputs, struct_output_size }; \
}
template <
class UCC,
typename AT1,
typename AT2,
IOReturn(UCC::*METHOD)(AT1, AT2)>
struct userclient_method<IOReturn(UCC::*)(AT1, AT2), METHOD>
{
typedef TypeListItem<AT1, TypeListItem<AT2, EmptyTypeList> > argument_types;
UC_METHOD_PROPERTY_DEFS
UC_METHOD_APPLY_DEF(
get_element<typename TypeListItemIndex<gen_args, 0>::type>(arguments),
get_element<typename TypeListItemIndex<gen_args, 1>::type>(arguments))
};
template <
class UCC,
typename AT1,
typename AT2,
typename AT3,
IOReturn(UCC::*METHOD)(AT1, AT2, AT3)>
struct userclient_method<IOReturn(UCC::*)(AT1, AT2, AT3), METHOD>
{
typedef TypeListItem<AT1, TypeListItem<AT2, TypeListItem<AT3, EmptyTypeList> > > argument_types;
UC_METHOD_PROPERTY_DEFS
UC_METHOD_APPLY_DEF(
get_element<typename TypeListItemIndex<gen_args, 0>::type>(arguments),
get_element<typename TypeListItemIndex<gen_args, 1>::type>(arguments),
get_element<typename TypeListItemIndex<gen_args, 2>::type>(arguments)
)
};
template <
class UCC,
typename AT1,
typename AT2,
typename AT3,
typename AT4,
IOReturn(UCC::*METHOD)(
AT1, AT2, AT3, AT4
)>
struct userclient_method<IOReturn(UCC::*)(
AT1, AT2, AT3, AT4
), METHOD>
{
typedef
TypeListItem<AT1, TypeListItem<AT2, TypeListItem<AT3, TypeListItem<AT4, EmptyTypeList> > > > argument_types;
UC_METHOD_PROPERTY_DEFS
UC_METHOD_APPLY_DEF(
get_element<typename TypeListItemIndex<gen_args, 0>::type>(arguments),
get_element<typename TypeListItemIndex<gen_args, 1>::type>(arguments),
get_element<typename TypeListItemIndex<gen_args, 2>::type>(arguments),
get_element<typename TypeListItemIndex<gen_args, 3>::type>(arguments)
)
};
template <