-
Notifications
You must be signed in to change notification settings - Fork 253
/
ntkrutils.h
1342 lines (1111 loc) · 28.1 KB
/
ntkrutils.h
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
/*
* Copyright 2019 Google LLC
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include <ntddk.h>
#include <intrin.h>
#include <aehd_types.h>
#include <string.h>
#include <dos.h>
#include <linux/list.h>
#include <uapi/asm/processor-flags.h>
// APC definitions (undocumented)
typedef enum _KAPC_ENVIRONMENT
{
OriginalApcEnvironment,
AttachedApcEnvironment,
CurrentApcEnvironment,
InsertApcEnvironment
} KAPC_ENVIRONMENT;
typedef
VOID
(NTAPI *PKNORMAL_ROUTINE)(
_In_ PVOID NormalContext,
_In_ PVOID SystemArgument1,
_In_ PVOID SystemArgument2
);
typedef
VOID
(NTAPI *PKKERNEL_ROUTINE)(
_In_ PKAPC Apc,
_Inout_ PKNORMAL_ROUTINE* NormalRoutine,
_Inout_ PVOID* NormalContext,
_Inout_ PVOID* SystemArgument1,
_Inout_ PVOID* SystemArgument2
);
typedef
VOID
(NTAPI *PKRUNDOWN_ROUTINE) (
_In_ PKAPC Apc
);
NTKERNELAPI
VOID
NTAPI
KeInitializeApc(
_Out_ PRKAPC Apc,
_In_ PETHREAD Thread,
_In_ KAPC_ENVIRONMENT Environment,
_In_ PKKERNEL_ROUTINE KernelRoutine,
_In_opt_ PKRUNDOWN_ROUTINE RundownRoutine,
_In_opt_ PKNORMAL_ROUTINE NormalRoutine,
_In_opt_ KPROCESSOR_MODE ApcMode,
_In_opt_ PVOID NormalContext
);
NTKERNELAPI
BOOLEAN
NTAPI
KeInsertQueueApc(
_Inout_ PRKAPC Apc,
_In_opt_ PVOID SystemArgument1,
_In_opt_ PVOID SystemArgument2,
_In_ KPRIORITY Increment
);
// MSDN recommends the string in reverse order
#define AEHD_POOL_TAG '_MVG'
// cpuid
static __forceinline void cpuid(unsigned int op,
unsigned int *eax,
unsigned int *ebx,
unsigned int *ecx,
unsigned int *edx)
{
int cpuInfo[4];
__cpuid(cpuInfo, op);
*eax = cpuInfo[0];
*ebx = cpuInfo[1];
*ecx = cpuInfo[2];
*edx = cpuInfo[3];
}
static __forceinline void cpuid_count(unsigned int op,
unsigned int count,
unsigned int *eax,
unsigned int *ebx,
unsigned int *ecx,
unsigned int *edx)
{
int cpuInfo[4];
__cpuidex(cpuInfo, op, count);
*eax = cpuInfo[0];
*ebx = cpuInfo[1];
*ecx = cpuInfo[2];
*edx = cpuInfo[3];
}
static __inline unsigned int cpuid_eax(unsigned int op)
{
unsigned int eax, ebx, ecx, edx;
cpuid(op, &eax, &ebx, &ecx, &edx);
return eax;
}
static __inline unsigned int cpuid_ebx(unsigned int op)
{
unsigned int eax, ebx, ecx, edx;
cpuid(op, &eax, &ebx, &ecx, &edx);
return ebx;
}
static __inline unsigned int cpuid_ecx(unsigned int op)
{
unsigned int eax, ebx, ecx, edx;
cpuid(op, &eax, &ebx, &ecx, &edx);
return ecx;
}
static __inline unsigned int cpuid_edx(unsigned int op)
{
unsigned int eax, ebx, ecx, edx;
cpuid(op, &eax, &ebx, &ecx, &edx);
return edx;
}
static __forceinline unsigned int x86_family(unsigned int sig)
{
unsigned int x86;
x86 = (sig >> 8) & 0xf;
if (x86 == 0xf)
x86 += (sig >> 20) & 0xff;
return x86;
}
static __forceinline unsigned int x86_cpuid_family(void)
{
return x86_family(cpuid_eax(1));
}
static __forceinline unsigned int x86_model(unsigned int sig)
{
unsigned int fam, model;
fam = x86_family(sig);
model = (sig >> 4) & 0xf;
if (fam >= 0x6)
model += ((sig >> 16) & 0xf) << 4;
return model;
}
static __forceinline unsigned int x86_cpuid_model(void)
{
return x86_model(cpuid_eax(1));
}
static __forceinline unsigned int x86_stepping(unsigned int sig)
{
return sig & 0xf;
}
/*
* cpu_has_vmx
*/
static __inline int cpu_has_vmx(void)
{
size_t ecx = cpuid_ecx(1);
return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
}
/*
* Memory Barriers
*/
#define smp_mb() _mm_mfence()
#define smp_rmb() _mm_lfence()
#define smp_wmb() _mm_sfence()
#define mb() _mm_mfence()
#define rmb() _mm_lfence()
#define wmb() _mm_sfence()
#define smp_mb__after_atomic() _mm_mfence();
// smp_processor_id
static __inline unsigned int raw_smp_processor_id(void)
{
return KeGetCurrentProcessorNumberEx(NULL);
}
static __inline unsigned int smp_processor_id(void)
{
return raw_smp_processor_id();
}
/*
* cpu_get/put for ensure vmx safety
*/
struct cpu_getput_cxt {
long count;
KIRQL irql;
};
DECLARE_PER_CPU(struct cpu_getput_cxt, cpu_getput_cxt);
static __inline unsigned int get_cpu()
{
KIRQL oldIrql = KeRaiseIrqlToDpcLevel();
unsigned int cpu = smp_processor_id();
long newcount = InterlockedIncrement(&per_cpu(cpu_getput_cxt, cpu).count);
if (newcount == 1)
per_cpu(cpu_getput_cxt, cpu).irql = oldIrql;
return cpu;
}
static __inline void put_cpu()
{
unsigned int cpu = smp_processor_id();
long newcount = InterlockedDecrement(&per_cpu(cpu_getput_cxt, cpu).count);
BUG_ON(newcount < 0);
if (newcount == 0) {
KIRQL oldIrql = per_cpu(cpu_getput_cxt, cpu).irql;
per_cpu(cpu_getput_cxt, cpu).irql = 0;
KeLowerIrql(oldIrql);
}
}
#define preempt_disable() KeRaiseIrqlToDpcLevel()
#define preempt_enable() KeLowerIrql(PASSIVE_LEVEL)
// msr access
static _forceinline void wrmsrl(unsigned int msr, u64 val)
{
__writemsr(msr, val);
}
extern struct cpumask *cpu_online_mask;
extern unsigned int cpu_online_count;
/*
* SpinLock Implementation
* Compared with Windows Native Support, this implementation does not raise IRQL to DPC level.
* KVM has nasty lock nesting that might work on Linux but not directly on Windows.
*/
struct spin_lock {
volatile LONG lock;
};
typedef struct spin_lock spinlock_t;
typedef struct spin_lock raw_spinlock_t;
#define DEFINE_SPINLOCK(x) spinlock_t x
#define DECLARE_SPINLOCK(x) extern spinlock_t x
#define DEFINE_RAW_SPINLOCK(x) spinlock_t x
#define DECLARE_RAW_SPINLOCK(x) extern spinlock_t x
static __forceinline void spin_lock_init(spinlock_t *lock)
{
lock->lock = 0;
}
extern __forceinline void __spin_lock(spinlock_t *lock);
static __forceinline void spin_lock(spinlock_t *lock)
{
__spin_lock(lock);
}
static __forceinline void spin_unlock(spinlock_t *lock)
{
lock->lock = 0;
}
static __forceinline void raw_spin_lock_init(spinlock_t *lock)
{
spin_lock_init(lock);
}
static __forceinline void raw_spin_lock(spinlock_t *lock)
{
spin_lock(lock);
}
static __forceinline void raw_spin_unlock(spinlock_t *lock)
{
spin_unlock(lock);
}
/*
Mutex Windows Implementation
*/
struct mutex
{
FAST_MUTEX mutex;
};
typedef struct mutex mutex;
static __forceinline void mutex_init(struct mutex *lock)
{
ExInitializeFastMutex(&lock->mutex);
}
static __forceinline void mutex_lock(struct mutex *lock)
{
ExAcquireFastMutex(&lock->mutex);
}
static __forceinline void mutex_unlock(struct mutex *lock)
{
ExReleaseFastMutex(&lock->mutex);
}
#define __KERNEL_CS 0x10
#define __KERNEL_DS 0x28
#define __KERNEL_SS 0x18
#define __KERNEL_FS 0x53
/*
MSR access
*/
static __inline void __rdmsr(u32 index, u32 *low, u32 *high)
{
u64 val = __readmsr(index);
*low = (u32)val;
*high = (u32)(val >> 32);
}
static __inline int __rdmsr_safe(u32 index, u32 *low, u32 *high)
{
u64 val = 0;
__try {
val = __readmsr(index);
*low = (u32)val;
*high = (u32)(val >> 32);
} __except(EXCEPTION_EXECUTE_HANDLER) {
return -1;
}
return 0;
}
static __inline int __rdmsrl_safe(u32 index, u64 *val)
{
__try {
*val = __readmsr(index);
} __except(EXCEPTION_EXECUTE_HANDLER) {
return -1;
}
return 0;
}
static __inline u64 native_read_msr_safe(u32 index, int *err)
{
u64 value = 0;
*err = __rdmsrl_safe(index, &value);
return value;
}
static __inline int __wrmsr_safe(u32 index, u32 low, u32 high)
{
u64 val = (((u64)high) << 32) | low;
__try {
__writemsr(index, val);
} __except(EXCEPTION_EXECUTE_HANDLER) {
return -1;
}
return 0;
}
static __inline int __wrmsrl_safe(u32 index, u64 val)
{
__try {
__writemsr(index, val);
} __except(EXCEPTION_EXECUTE_HANDLER) {
return -1;
}
return 0;
}
static __inline int native_write_msr_safe(u32 index, u32 low, u32 high)
{
return __wrmsr_safe(index, low, high);
}
#define rdmsr(a, b, c) __rdmsr(a, &b, &c)
#define rdmsr_safe(a, b, c) __rdmsr_safe(a, b, c)
#define rdmsrl(a, b) b=__readmsr(a)
#define rdmsrl_safe(a, b) __rdmsrl_safe(a, b)
#define wrmsr(a,b) __writemsr(a,b)
#define wrmsrl(a,b) __writemsr(a,b)
#define wrmsr_safe(a, b, c) __wrmsr_safe(a, b, c)
#define wrmsrl_safe(a,b) __wrmsrl_safe(a,b)
/*
Local Irq Disable
*/
static __forceinline void local_irq_disable(void)
{
_disable();
}
static __forceinline void local_irq_enable(void)
{
_enable();
}
/*
Timer Stuffs
*/
#define MSEC_PER_SEC 1000L
#define USEC_PER_MSEC 1000L
#define NSEC_PER_USEC 1000L
#define NSEC_PER_MSEC 1000000L
#define USEC_PER_SEC 1000000L
#define NSEC_PER_SEC 1000000000L
#define FSEC_PER_SEC 1000000000000000LL
union ktime
{
s64 tv64;
struct {
s32 nsec, sec;
} tv;
};
typedef union ktime ktime_t;
#define KTIME_MAX ((s64)~((u64)1 << 63))
#define KTIME_SEC_MAX LONG_MAX
#pragma warning(disable : 4204)
static __forceinline ktime_t ktime_set(const long secs, const size_t nsecs)
{
#if 0
if (unlikely(secs >= KTIME_SEC_MAX))
return (ktime_t){ .tv64 = KTIME_MAX };
#endif
return (ktime_t) { .tv64 = (s64)secs * NSEC_PER_SEC + (s64)nsecs };
}
/* Subtract two ktime_t variables. rem = lhs -rhs: */
#define ktime_sub(lhs, rhs) \
(ktime_t){ .tv64 = (lhs).tv64 - (rhs).tv64 }
/* Add two ktime_t variables. res = lhs + rhs: */
#define ktime_add(lhs, rhs) \
(ktime_t){ .tv64 = (lhs).tv64 + (rhs).tv64 }
/*
* Add a ktime_t variable and a scalar nanosecond value.
* res = kt + nsval:
*/
#define ktime_add_ns(kt, nsval) \
(ktime_t){ .tv64 = (kt).tv64 + (nsval) }
/*
* Subtract a scalar nanosecod from a ktime_t variable
* res = kt - nsval:
*/
#define ktime_sub_ns(kt, nsval) \
(ktime_t){ .tv64 = (kt).tv64 - (nsval) }
/* Map the ktime_t to timespec conversion to ns_to_timespec function */
#define ktime_to_timespec(kt) ns_to_timespec((kt).tv64)
/* Map the ktime_t to timeval conversion to ns_to_timeval function */
#define ktime_to_timeval(kt) ns_to_timeval((kt).tv64)
/* Convert ktime_t to nanoseconds - NOP in the scalar storage format: */
#define ktime_to_ns(kt) ((kt).tv64)
static __forceinline int ktime_equal(const ktime_t cmp1, const ktime_t cmp2)
{
return cmp1.tv64 == cmp2.tv64;
}
/**
* ktime_compare - Compares two ktime_t variables for less, greater or equal
* @cmp1: comparable1
* @cmp2: comparable2
*
* Returns ...
* cmp1 < cmp2: return <0
* cmp1 == cmp2: return 0
* cmp1 > cmp2: return >0
*/
static __forceinline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
{
if (cmp1.tv64 < cmp2.tv64)
return -1;
if (cmp1.tv64 > cmp2.tv64)
return 1;
return 0;
}
static __forceinline ktime_t ktime_add_us(const ktime_t kt, const u64 usec)
{
return ktime_add_ns(kt, usec * 1000);
}
static __forceinline ktime_t ktime_sub_us(const ktime_t kt, const u64 usec)
{
return ktime_sub_ns(kt, usec * 1000);
}
static __forceinline ktime_t ns_to_ktime(u64 ns)
{
static const ktime_t ktime_zero = { .tv64 = 0 };
return ktime_add_ns(ktime_zero, ns);
}
static __forceinline ktime_t ktime_get(void)
{
s64 nsecs = 0;
LARGE_INTEGER time;
KeQuerySystemTime(&time);
nsecs = time.QuadPart;
nsecs *= 100;
return (ktime_t){.tv64 = nsecs};
}
typedef size_t clockid_t;
#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 1
#define CLOCK_PROCESS_CPUTIME_ID 2
#define CLOCK_THREAD_CPUTIME_ID 3
#define CLOCK_MONOTONIC_RAW 4
#define CLOCK_REALTIME_COARSE 5
#define CLOCK_MONOTONIC_COARSE 6
#define CLOCK_BOOTTIME 7
#define CLOCK_REALTIME_ALARM 8
#define CLOCK_BOOTTIME_ALARM 9
enum hrtimer_mode
{
HRTIMER_MODE_ABS = 0x0, /* Time value is absolute */
HRTIMER_MODE_REL = 0x1, /* Time value is relative to now */
HRTIMER_MODE_PINNED = 0x02, /* Timer is bound to CPU */
HRTIMER_MODE_ABS_PINNED = 0x02,
HRTIMER_MODE_REL_PINNED = 0x03,
};
enum hrtimer_restart
{
HRTIMER_NORESTART, /* Timer is not restarted */
HRTIMER_RESTART, /* Timer must be restarted */
};
struct timerqueue_node
{
ktime_t expires;
};
struct hrtimer_clock_base
{
int index;
ktime_t resolution;
ktime_t (*get_time)(void);
ktime_t softirq_time;
ktime_t offset;
};
struct hrtimer
{
struct timerqueue_node node;
ktime_t _softexpires;
enum hrtimer_restart (*function)(struct hrtimer *);
struct hrtimer_clock_base *base;
size_t state;
PEX_TIMER ex_timer;
EXT_SET_PARAMETERS ext_set_parameters;
LARGE_INTEGER due_time;
struct hrtimer_clock_base base_hack;
};
int hrtimer_init(struct hrtimer *timer, clockid_t clock_id, enum hrtimer_mode mode);
int hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode);
int hrtimer_cancel(struct hrtimer *timer);
int hrtimer_restart(struct hrtimer* timer);
void hrtimer_delete(struct hrtimer* timer);
static __forceinline void hrtimer_add_expires_ns(struct hrtimer *timer, u64 delta)
{
timer->node.expires = ktime_add_ns(timer->node.expires, delta);
}
static __forceinline ktime_t hrtimer_get_expires(struct hrtimer *timer)
{
return timer->node.expires;
}
static __forceinline u64 hrtimer_get_expires_ns(struct hrtimer *timer)
{
return ktime_to_ns(timer->node.expires);
}
static __forceinline void hrtimer_start_expires(struct hrtimer *timer, int mode)
{
hrtimer_start(timer, timer->node.expires, mode);
}
static __forceinline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer)
{
return ktime_sub(timer->node.expires, timer->base->get_time());
}
static __forceinline ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
{
ktime_t rem;
rem = hrtimer_expires_remaining(timer);
return rem;
}
/*
* Wrap MmProbeAndLockPages
*/
static __inline bool __MmProbeAndLockPages(PMDL pmdl, KPROCESSOR_MODE mode,
LOCK_OPERATION op)
{
__try {
MmProbeAndLockPages(pmdl, mode, op);
} __except (EXCEPTION_EXECUTE_HANDLER) {
return false;
}
return true;
}
/*
Memory Management Stuffs
*/
#define BIT(nr) ((size_t)(1) << (nr))
#define GFP_KERNEL BIT(0)
#define GFP_ATOMIC BIT(1)
#define __GFP_ZERO BIT(3)
#define GFP_UNALLOC BIT(5)
/*
* Address types:
*
* gva - guest virtual address
* gpa - guest physical address
* gfn - guest frame number
* hva - host virtual address
* hpa - host physical address
* hfn - host frame number
*/
typedef size_t gva_t;
typedef u64 gpa_t;
typedef u64 gfn_t;
typedef u64 phys_addr_t;
typedef size_t hva_t;
typedef u64 hpa_t;
typedef u64 hfn_t;
typedef hfn_t pfn_t;
typedef struct page
{
void* hva;
void* kmap_hva;
size_t __private;
hpa_t hpa;
pfn_t pfn;
size_t gfp_mask;
PEPROCESS proc;
}page;
extern u64 max_pagen;
extern struct page** pglist;
DECLARE_RAW_SPINLOCK(global_page_lock);
#define page_private(page) ((page)->__private)
#define set_page_private(page, v) ((page)->__private = (v))
#define __free_page(page) __free_pages((page), 0)
#define free_page(addr) free_pages((addr), 0)
#define clear_page(page) memset((page), 0, PAGE_SIZE)
#define virt_to_page(kaddr) pfn_to_page((__pa(kaddr) >> PAGE_SHIFT))
static __inline void *kmalloc(size_t size, size_t flags)
{
void* ret = NULL;
int zero = 0;
if (flags & __GFP_ZERO)
zero = 1;
ret = ExAllocatePoolWithTag(NonPagedPool, size, AEHD_POOL_TAG);
if(ret && zero)
{
memset(ret, 0, size);
}
return ret;
}
static __inline void *kzalloc(size_t size, size_t flags)
{
return kmalloc(size, flags | __GFP_ZERO);
}
static __inline void kfree(void* hva)
{
if (!hva)
return;
ExFreePoolWithTag(hva, AEHD_POOL_TAG);
}
static __inline void *vmalloc(size_t size)
{
return ExAllocatePoolWithTag(NonPagedPool, size, AEHD_POOL_TAG);
}
static __inline void vfree(void* hva)
{
if (!hva)
return;
ExFreePoolWithTag(hva, AEHD_POOL_TAG);
}
static __inline void *vzalloc(size_t size)
{
void *addr = vmalloc(size);
if (addr)
{
memset(addr, 0, size);
}
return addr;
}
static __inline void *kmalloc_fast(size_t size, size_t flags)
{
return kmalloc(size, flags);
}
static __inline void *kzalloc_fast(size_t size, size_t flags)
{
return kmalloc_fast(size, flags | __GFP_ZERO);
}
static __inline void kfree_fast(void* hva)
{
if (!hva)
return;
ExFreePoolWithTag(hva, AEHD_POOL_TAG);
}
#define kvfree kfree_fast
#define VERIFY_READ 0
#define VERIFY_WRITE 1
static __inline pfn_t page_to_pfn(struct page* page)
{
return page->pfn;
}
static __inline void* page_to_hva(struct page* page)
{
return page->hva;
}
static __inline hpa_t page_to_phys(struct page* page)
{
return page->hpa;
}
static __inline hpa_t mdl_to_phys(PMDL mdl)
{
return (hpa_t)MmGetPhysicalAddress(mdl->StartVa).QuadPart;
}
static __inline struct page* pfn_to_page(pfn_t pfn)
{
return pglist[pfn];
}
static __inline hpa_t __pa(void* va)
{
PHYSICAL_ADDRESS addr_phys;
addr_phys = MmGetPhysicalAddress(va);
return (hpa_t)(addr_phys.QuadPart);
}
static __inline void* __va(hpa_t pa)
{
void* ret = 0;
ret = page_to_hva(pfn_to_page(pa >> PAGE_SHIFT));
if(!ret)
{
printk("vmmr0: __va: invalid hpa %p\n", pa);
}
return ret;
}
static __inline struct page *alloc_page(unsigned int gfp_mask)
{
void* page_hva = NULL;
PHYSICAL_ADDRESS pageaddr_phys;
int zero = 0;
struct page* page = ExAllocatePoolWithTag(NonPagedPool,
sizeof(*page),
AEHD_POOL_TAG);
if(!page)
goto out_error;
page_hva = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, AEHD_POOL_TAG);
if(!page_hva)
goto out_error_free;
if (gfp_mask & __GFP_ZERO)
zero = 0;
ASSERT(!((size_t)page_hva & 0xfffull));
if(zero)
memset(page_hva, 0, PAGE_SIZE);
pageaddr_phys = MmGetPhysicalAddress(page_hva);
page->hpa = pageaddr_phys.QuadPart;
page->pfn = page->hpa >> PAGE_SHIFT;
page->hva = page_hva;
page->gfp_mask = gfp_mask;
page->proc = IoGetCurrentProcess();
raw_spin_lock(&global_page_lock);
pglist[page->pfn] = page;
raw_spin_unlock(&global_page_lock);
return page;
out_error_free:
ExFreePoolWithTag(page, AEHD_POOL_TAG);
out_error:
return 0;
}
static __inline void __free_pages(struct page* page, unsigned int order)
{
raw_spin_lock(&global_page_lock);
pglist[page->pfn] = 0;
raw_spin_unlock(&global_page_lock);
ExFreePoolWithTag(page->hva, AEHD_POOL_TAG);
ExFreePoolWithTag(page, AEHD_POOL_TAG);
}
static __inline void free_pages(size_t addr, unsigned int order)
{
if (addr != 0)
{
__free_pages(virt_to_page((void *)addr), order);
}
}
static __inline void* kmap(PMDL mdl)
{
if (!mdl)
return NULL;
return MmGetSystemAddressForMdlSafe(mdl, NormalPagePriority);
}
static __inline void kunmap(PMDL mdl)
{
}
static __inline void* page_address(struct page* page)
{
BUG_ON(!page->hva);
return page->hva;
}
static __inline void* get_zeroed_page(unsigned int gfp_mask)
{
struct page* page = alloc_page(gfp_mask);
memset(page->hva, 0, PAGE_SIZE);
return page->hva;
}
static __inline size_t __get_free_page(unsigned int gfp_mask)
{
struct page *page;
page = alloc_page(gfp_mask);
if (!page)
return 0;
return (size_t) page_address(page);
}
static __inline int get_user_pages_fast(size_t start, int nr_pages, int write,
PMDL *mdl)
{
PMDL _mdl;
start &= PAGE_MASK;
_mdl = IoAllocateMdl((void *)start, nr_pages * PAGE_SIZE,
FALSE, FALSE, NULL);
if (!_mdl)
return 0;
if (!__MmProbeAndLockPages(_mdl, KernelMode, IoWriteAccess)) {
IoFreeMdl(_mdl);
return 0;
}
*mdl = _mdl;
return nr_pages;
}
static __inline void kvm_release_page(PMDL mdl)
{
if (!mdl)
return;
MmUnlockPages(mdl);
IoFreeMdl(mdl);
}
static __inline size_t __copy_user_safe(void *dst, const void *src, size_t size,
int from)
{
PMDL lock_mdl;
HANDLE handle;
size_t ret = size;
int clac = 0;
lock_mdl = IoAllocateMdl(from? src : dst, size, FALSE, FALSE, NULL);
if (!lock_mdl)
return size;
if (!__MmProbeAndLockPages(lock_mdl, UserMode, IoWriteAccess))
goto out_free;
handle = MmSecureVirtualMemory(from? src : dst, size, PAGE_READWRITE);
if (!handle)
goto out_unlock;
/*
* If Windows turns on SMAP, we need set AC flag before accessing
* user addr. However, since we do not know Windows's logic for AC
* flag, we only turned it on the CPU this piece of code is running
* and make sure we are not interrupted in the middle (in case Windows
* has the chance to change the AC flag).
*/
if (boot_cpu_has(X86_FEATURE_SMAP)) {
local_irq_disable();
if (__readcr4() & X86_CR4_SMAP &&
!(__readeflags() & X86_EFLAGS_AC)) {
clac = 1;
_stac();
} else
local_irq_enable();
}
memcpy(dst, src, size);
if (clac) {
_clac();
local_irq_enable();
}
ret = 0;
MmUnsecureVirtualMemory(handle);
out_unlock:
MmUnlockPages(lock_mdl);
out_free: