-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibs-core.c
1646 lines (1340 loc) · 44.2 KB
/
ibs-core.c
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
#include <linux/types.h>
#include <linux/cpu.h>
#include <linux/smp.h>
#include <linux/percpu.h>
#include <linux/sched.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/interrupt.h>
#include <linux/kprobes.h>
#include <linux/device.h>
#include <linux/delay.h>
#include <linux/version.h>
#include <asm/irq_regs.h>
#include <asm/irq.h>
#include <asm/desc.h>
#include <asm/apic.h>
#include <asm/apicdef.h>
#include <asm/uaccess.h>
#include "ibs-api.h"
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
#include "ibs-vtpmo.h"
#include "ibs-disassemble.h"
#endif
#define IBS_OP 0
#define IBS_FETCH 1
/********************************************************************************/
/* CPUID REGISTERS & MASKS */
/* (check IBS support) */
#define CPUID_AMD_FAM10h 0x10
#define CPUID_AMD_FAM17h 0x17
#define FAM17H_MSR_WA_1 0xc0011020
#define FAM17H_MSR_WA_1_BITS 0x40000000000000ULL
#define FAM17H_MSR_WA_2 0xc0011029
#define FAM17H_MSR_WA_2_BITS 0x80000ULL
#define FAM17H_MSR_WA_3 0xc0010296
#define FAM17H_MSR_WA_3_BITS 0x404040ULL
#define CPUID_EXT_FEATURES 0xc0011005
#ifndef topology_sibling_cpumask
#define topology_sibling_cpumask(cpu) (per_cpu(cpu_sibling_map, cpu))
#endif
#define CPUID_Fn8000_001B_EAX cpuid_eax(0x8000001B)
#define IBS_CPUID_IBSFFV 1ULL
#define IBS_CPUID_FetchSam (1ULL<<1)
#define IBS_CPUID_OpSam (1ULL<<2)
#define IBS_CPUID_RdWrOpCnt (1ULL<<3)
#define IBS_CPUID_OpCnt (1ULL<<4)
#define IBS_CPUID_BrnTrgt (1ULL<<5)
#define CPUID_Fn8000_0001_ECX cpuid_ecx(0x80000001)
#define IBS_SUPPORT_EXIST (1ULL<<10)
/********************************************************************************/
/* IBS REGISTERS & MASKS */
/* (enable/disable/configure MSR registers) */
#define MSR_IBS_CONTROL 0xc001103a
#define IBS_LVT_OFFSET_VAL (1ULL<<8)
#define IBS_LVT_OFFSET 0xfULL
#define MSR_IBS_FETCH_CTL 0xc0011030
#define IBS_FETCH_CNT_CTL (0xffffULL<<16)
#define IBS_FETCH_VAL (1ULL<<49)
#define IBS_FETCH_EN (1ULL<<48)
#define IBS_FETCH_CNT_MAX 0xffffULL
#define MSR_IBS_OP_CTL 0xc0011033
#define IBS_OP_CNT_CTL (1ULL << 19) /* When 1, count dispatched. When 0, count clock cycles. */
#define IBS_OP_VAL (1ULL << 18) /* Set-by-HW. When 1, execution data available. Stop counting until SW clear. */
#define IBS_OP_EN (1ULL << 17) /* When 1, execution sampling enables. */
#define IBS_OP_CNT_MAX 0xfffffULL /* 20 Bit. 1048576 increments. */
#define IBS_OP_MAX_CNT_MAX (IBS_OP_CNT_MAX >> 4) /* Bits 15:0 are programmed. The 4 LSb are always zero. */
#define IBS_OP_CUR_CNT_MAX (IBS_OP_CNT_MAX) /* Bits 51:32 are programmed. */
#define DEFAULT_MIN_CNT 0xd000UL
#define DEFAULT_MAX_CNT 0xfffffUL
int ibs_open(struct inode *, struct file *);
int ibs_release(struct inode *, struct file *);
long ibs_ioctl(struct file *, unsigned int, unsigned long);
void handle_ibs_irq(struct pt_regs *regs);
/********************************************************************************/
/* IBS DEVICES */
/* (macros) */
#define IBS_MINOR(flavor, cpu) ((cpu << 1) | flavor)
#define IBS_CPU(minor) (minor >> 1)
#define IBS_FLAVOR(minor) (minor & 1)
/********************************************************************************/
/* APIC CONFIGURATION AND IBS REGISTERS INITIALIZATION */
/* (variables and functions) */
static unsigned ibs_vector = NR_VECTORS-1;
static int ibs_op_supported;
static int ibs_fetch_supported;
static int workaround_fam10h_err_420 = 0;
static int workaround_fam17h_zn = 0;
static int workarounds_started = 0;
static int* pcpu_num_devices_enabled;
static spinlock_t * pcpu_workaround_lock;
static u64 fam17h_old_1 = 0;
static u64 fam17h_old_2 = 0;
static u64 fam17h_old_3 = 0;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
static unsigned int old_call_operand = 0x0;
static unsigned int new_call_operand = 0x0;
static unsigned int *call_operand_address = NULL;
#else
static gate_desc old_ibs;
/* The following entry point is goind to be
removed and the management of the interrupt
replaced with the newest hooking strategy
that overwrite the call to the spurious
interrupt handler. */
extern void ibs_entry(void);
asm(
" .globl ibs_entry \n"
" .align 4, 0x90 \n"
"ibs_entry: \n"
" pushq $0 \n" // Error code. Don't care.
" cld \n"
" addq $-(15*8), %rsp \n"
" movq %r11, 6*8(%rsp) \n"
" movq %r10, 7*8(%rsp) \n"
" movq %r9, 8*8(%rsp) \n"
" movq %r8, 9*8(%rsp) \n"
" movq %rax, 10*8(%rsp) \n"
" movq %rcx, 11*8(%rsp) \n"
" movq %rdx, 12*8(%rsp) \n"
" movq %rsi, 13*8(%rsp) \n"
" movq %rdi, 14*8(%rsp) \n"
" movq %r15, 0*8(%rsp) \n"
" movq %r14, 1*8(%rsp) \n"
" movq %r13, 2*8(%rsp) \n"
" movq %r12, 3*8(%rsp) \n"
" movq %rbp, 4*8(%rsp) \n"
" movq %rbx, 5*8(%rsp) \n"
" testb $3, 17*8(%rsp) \n"
" jz 1f \n"
" swapgs \n"
"1: \n"
" mov %rsp, %rdi \n" // Points to pt_regs struct.
" pushq %rdi \n"
" call handle_ibs_irq \n"
" cli \n" // https://lwn.net/Articles/484932/
" popq %rsp \n"
" testb $3, 17*8(%rsp) \n"
" jz 2f \n"
" swapgs \n"
"2: \n"
" movq 0*8(%rsp), %r15 \n"
" movq 1*8(%rsp), %r14 \n"
" movq 2*8(%rsp), %r13 \n"
" movq 3*8(%rsp), %r12 \n"
" movq 4*8(%rsp), %rbp \n"
" movq 5*8(%rsp), %rbx \n"
" movq 6*8(%rsp), %r11 \n"
" movq 7*8(%rsp), %r10 \n"
" movq 8*8(%rsp), %r9 \n"
" movq 9*8(%rsp), %r8 \n"
" movq 10*8(%rsp), %rax \n"
" movq 11*8(%rsp), %rcx \n"
" movq 12*8(%rsp), %rdx \n"
" movq 13*8(%rsp), %rsi \n"
" movq 14*8(%rsp), %rdi \n"
" subq $-(15*8+8), %rsp \n"
" iretq \n"
" .size ibs_entry, .-ibs_entry \n"
);
#endif
static inline u64 custom_rdmsrl_on_cpu(unsigned int cpu, u32 msr_no)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,13,0)
u64 ret_val;
rdmsrl_on_cpu(cpu, msr_no, &ret_val);
return ret_val;
#else
u32 lo, hi;
rdmsr_on_cpu(cpu, msr_no, &lo, &hi);
return (u64)lo | ((u64)hi << 32ULL);
#endif
}
static inline void custom_wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 val)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,13,0)
wrmsrl_on_cpu(cpu, msr_no, val);
#else
u32 lo, hi;
lo = val & 0xffffffff;
hi = val >> 32;
wrmsr_on_cpu(cpu, msr_no, lo, hi);
#endif
}
static void init_fam17h_zn_workaround(void)
{
rdmsrl(FAM17H_MSR_WA_1, fam17h_old_1);
rdmsrl(FAM17H_MSR_WA_2, fam17h_old_2);
rdmsrl(FAM17H_MSR_WA_3, fam17h_old_3);
}
static int init_workaround_structs(void)
{
struct cpuinfo_x86 *c;
if (workarounds_started)
return 0;
c = &boot_cpu_data;
if (c->x86_vendor == X86_VENDOR_AMD && c->x86 == CPUID_AMD_FAM17h && c->x86_model == 0x1)
{
init_fam17h_zn_workaround();
}
pcpu_num_devices_enabled = alloc_percpu(int);
if (!pcpu_num_devices_enabled)
return -1;
pcpu_workaround_lock = alloc_percpu(spinlock_t);
if (!pcpu_workaround_lock)
{
free_percpu(pcpu_num_devices_enabled);
return -1;
}
workarounds_started = 1;
return 0;
}
static void free_workaround_structs(void)
{
if (workarounds_started)
{
free_percpu(pcpu_num_devices_enabled);
free_percpu(pcpu_workaround_lock);
}
}
static void init_workaround_initialize(void)
{
unsigned int cpu;
if (!workarounds_started)
return;
cpu = 0;
for_each_possible_cpu(cpu)
{
spinlock_t *workaround_lock;
int *num_devs = per_cpu_ptr(pcpu_num_devices_enabled, cpu);
*num_devs = 0;
workaround_lock = per_cpu_ptr(pcpu_workaround_lock, cpu);
spin_lock_init(workaround_lock);
}
}
static void start_fam17h_zn_static_workaround(const int cpu)
{
u64 cur;
int cpu_to_offline = -1, cpu_to_online = -1;
if (!workarounds_started)
{
init_workaround_structs();
init_workaround_initialize();
}
cur = custom_rdmsrl_on_cpu(cpu, CPUID_EXT_FEATURES);
cur |= (1ULL << 42);
custom_wrmsrl_on_cpu(cpu, CPUID_EXT_FEATURES, cur);
cur = custom_rdmsrl_on_cpu(cpu, FAM17H_MSR_WA_2);
if (cur & FAM17H_MSR_WA_2_BITS)
return;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
for_each_cpu(cpu_to_offline, topology_sibling_cpumask(cpu))
#else
for_each_cpu_mask(cpu_to_offline, topology_core_siblings(cpu))
#endif
{
if (cpu_to_offline != cpu)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,7,0)
remove_cpu(cpu_to_offline);
#else
cpu_down(cpu_to_offline);
#endif
cpu_to_online = cpu_to_offline;
}
}
custom_wrmsrl_on_cpu(cpu, FAM17H_MSR_WA_2, (cur | FAM17H_MSR_WA_2_BITS));
if (cpu_to_online != -1)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,7,0)
add_cpu(cpu_to_online);
#else
cpu_up(cpu_to_online);
#endif
}
}
static void stop_fam17h_zn_static_workaround(const int cpu)
{
u64 cur;
unsigned int cpu_to_use;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
cpu_to_use = cpumask_first(topology_sibling_cpumask(cpu));
#else
cpu_to_use = first_cpu(topology_core_siblings(cpu));
#endif
if (cpu_to_use == cpu)
{
cur = custom_rdmsrl_on_cpu(cpu_to_use, FAM17H_MSR_WA_2);
cur = fam17h_old_2 | (cur & ~FAM17H_MSR_WA_2_BITS);
custom_wrmsrl_on_cpu(cpu, FAM17H_MSR_WA_2, cur);
}
cur = custom_rdmsrl_on_cpu(cpu, CPUID_EXT_FEATURES);
cur &= ~(1ULL << 42);
custom_wrmsrl_on_cpu(cpu, CPUID_EXT_FEATURES, cur);
}
int check_for_ibs_support(void)
{
struct cpuinfo_x86 *c;
unsigned int feature_id;
c = &boot_cpu_data;
if (c->x86_vendor != X86_VENDOR_AMD)
{
pr_err("IBS: required AMD processor.\n");
return -EINVAL;
}
feature_id = CPUID_Fn8000_0001_ECX;
if (c->x86 == CPUID_AMD_FAM10h)
{
if (!(feature_id & IBS_SUPPORT_EXIST))
{
pr_err("IBS: CPUID_Fn8000_0001 indicates no IBS support\n");
return -EINVAL;
}
else
{
pr_info("IBS: Startup enabling workaround for Family 10h CPUs\n");
workaround_fam10h_err_420 = 1;
}
}
else if (c->x86 == CPUID_AMD_FAM17h)
{
if (!(feature_id & IBS_SUPPORT_EXIST))
{
if ((c->x86_model >= 0x0 && c->x86_model <= 0x2f) || (c->x86_model >= 0x50 && c->x86_model < 0x5f))
{
unsigned int cpu = 0;
pr_info("IBS: Startup enabling workaround for Family 17h first-gen CPUs\n");
workaround_fam17h_zn = 1;
for_each_online_cpu(cpu)
{
start_fam17h_zn_static_workaround(cpu);
}
}
else
{
pr_err("IBS: CPUID_Fn8000_0001 indicates no IBS support\n");
return -EINVAL;
}
}
}
else
{
pr_err("IBS: this module is designed only for Fam 10h and 17h\n");
return -EINVAL;
}
feature_id = CPUID_Fn8000_001B_EAX;
if (!(feature_id & IBS_CPUID_IBSFFV))
{
pr_err("IBS: CPUID_Fn8000_001B indicates no IBS support\n");
return -EINVAL;
}
ibs_fetch_supported = feature_id & IBS_CPUID_FetchSam;
ibs_op_supported = feature_id & IBS_CPUID_OpSam;
ibs_op_supported |= feature_id & IBS_CPUID_RdWrOpCnt;
ibs_op_supported |= feature_id & IBS_CPUID_OpCnt;
if (!ibs_fetch_supported)
{
pr_err("IBS: CPUID_Fn800_001B says no Op support\n");
return -EINVAL;
}
if (!ibs_op_supported)
{
pr_err("IBS: CPUID_Fn800_001B says no Fetch support\n");
return -EINVAL;
}
return 0;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
static int select_vector_by_inspect_push_operand(unsigned char *byte)
{
unsigned int opcode;
unsigned int operand;
opcode = ((unsigned int) byte[0]) & 0xff;
if (opcode == 0x68) /* push imm16/imm32 */
{
operand = ~(((unsigned int) byte[1]) | (((unsigned int) byte[2]) << 8) |
(((unsigned int) byte[3]) << 16) | (((unsigned int) byte[4]) << 24));
if (operand == 0xFF) /* SPURIOUS_APIC_VECTOR */
return 1;
}
else if (opcode == 0x6A) /* push imm8 */
{
operand = ~(((unsigned int) byte[1]) & 0xff);
if (operand == 0xFF) /* SPURIOUS_APIC_VECTOR */
return 1;
}
return 0;
}
# if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,2)
static long select_vector_by_counting_jmp_addresses(unsigned char *byte)
{
unsigned int opcode;
unsigned int operand;
opcode = ((unsigned int) byte[0]) & 0xff;
if (opcode == 0x68) /* push imm16/imm32 */
{
opcode = ((unsigned int) byte[5]) & 0xff;
if (opcode == 0xE9) /* jmp imm16/imm32 */
{
operand = ((unsigned int) byte[6]) | (((unsigned int) byte[7]) << 8) |
(((unsigned int) byte[8]) << 16) | (((unsigned int) byte[9]) << 24);
return ((long) &byte[10]) + (long) operand; /* RIP + operand */
}
else if (opcode == 0xEB) /* jmp imm8 */
{
operand = ((unsigned int) byte[6]) & 0xff;
return ((long) &byte[7]) + (long) operand; /* RIP + operand */
}
}
else if (opcode == 0x6A) /* push imm8 */
{
opcode = ((unsigned int) byte[2]) & 0xff;
if (opcode == 0xE9) /* jmp imm16/imm32 */
{
operand = ((unsigned int) byte[3]) | (((unsigned int) byte[4]) << 8) |
(((unsigned int) byte[5]) << 16) | (((unsigned int) byte[6]) << 24);
return ((long) &byte[7]) + (long) operand; /* RIP + operand */
}
else if (opcode == 0xEB) /* jmp imm8 */
{
operand = ((unsigned int) byte[3]) & 0xff;
return ((long) &byte[4]) + (long) operand; /* RIP + operand */
}
}
return 0;
}
# endif
#endif
static int acquire_free_vector(void)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
struct desc_ptr idtr;
gate_desc *gate_ptr;
# if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,2)
int i;
long addr;
unsigned int max_count;
long address[NR_VECTORS-FIRST_SYSTEM_VECTOR+1] = { 0 };
unsigned int vector[NR_VECTORS-FIRST_SYSTEM_VECTOR+1] = { 0 };
unsigned int counter[NR_VECTORS-FIRST_SYSTEM_VECTOR+1] = { 0 };
# endif
store_idt(&idtr);
while (1)
{
if (ibs_vector < FIRST_SYSTEM_VECTOR)
{
# if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,2)
for (max_count=1, i=0; i<NR_VECTORS-FIRST_SYSTEM_VECTOR+1; i++)
{
if (counter[i] > max_count)
{
ibs_vector = vector[i];
max_count = counter[i];
}
}
if (max_count > 1)
break;
# endif
pr_err("IBS: no free IDT vector is found.\n");
return -ENODATA;
}
gate_ptr = (gate_desc *) (idtr.address + ibs_vector * sizeof(gate_desc));
if (select_vector_by_inspect_push_operand((unsigned char *) (((unsigned long) gate_ptr->offset_low) |
((unsigned long) gate_ptr->offset_middle << 16) |
((unsigned long) gate_ptr->offset_high << 32))))
break;
# if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,2)
addr = select_vector_by_counting_jmp_addresses((unsigned char *) (((unsigned long) gate_ptr->offset_low) |
((unsigned long) gate_ptr->offset_middle << 16) |
((unsigned long) gate_ptr->offset_high << 32)));
for (i=0; i<NR_VECTORS-FIRST_SYSTEM_VECTOR+1; i++)
{
if (address[i] == 0 || address[i] == addr)
{
counter[i] += 1;
if (address[i] == 0)
{
vector[i] = ibs_vector;
address[i] = addr;
}
break;
}
}
# endif
ibs_vector--;
}
#else
while (test_bit(ibs_vector, used_vectors))
{
if (ibs_vector == 0x40)
{
pr_err("IBS: no free IDT vector is found.\n");
return -1;
}
ibs_vector--;
}
set_bit(ibs_vector, used_vectors);
#endif
pr_info("IBS: IDT vector 0x%x acquired.\n", ibs_vector);
return 0;
}
static unsigned long _force_order_;
static inline unsigned long _read_cr0(void)
{
unsigned long val;
asm volatile("mov %%cr0, %0" : "=r" (val), "=m" (_force_order_));
return val;
}
static inline void _write_cr0(unsigned long val)
{
asm volatile("mov %0, %%cr0" : : "r" (val), "m" (_force_order_));
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
static inline long get_address_entry_idt(unsigned int vector)
{
struct desc_ptr idtr;
gate_desc *gate_ptr;
store_idt(&idtr);
gate_ptr = (gate_desc *) (idtr.address + vector * sizeof(gate_desc));
return (long) ((unsigned long) (gate_ptr->offset_low) |
((unsigned long) (gate_ptr->offset_middle) << 16) |
((unsigned long) (gate_ptr->offset_high) << 32));
}
static inline int get_address_from_symbol(unsigned long *address, const char *symbol)
{
int ret;
struct kprobe kp = {};
kp.symbol_name = symbol;
ret = register_kprobe(&kp);
if (ret < 0)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,7,7)
pr_info("[IPI Module INFO] - Symbol %s not found. Returned value %d.\n", symbol, ret);
return ret;
#else
# ifdef CONFIG_KALLSYMS
unsigned long addr;
if ((addr = kallsyms_lookup_name(symbol)) == 0UL)
{
# endif
pr_err("[IPI Module INFO] - Symbol %s not found. Returned value %d.\n", symbol, ret);
return ret;
# ifdef CONFIG_KALLSYMS
}
else
{
*address = addr;
}
# endif
#endif
}
else
{
*address = (unsigned long) kp.addr;
unregister_kprobe(&kp);
}
pr_info("[IPI Module INFO] - Symbol %s found at 0x%lx.\n", symbol, *address);
return 0;
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
static unsigned long replace_call_address_through_binary_inspection(unsigned long entry_address, unsigned long spurious_address)
{
unsigned int b;
unsigned int count;
unsigned int level = 0;
unsigned int level_0_call_count = 0;
unsigned long cr0;
unsigned char disassembled[BUFFER_SIZE];
unsigned char *byte;
unsigned long level_address[MAX_LEVEL + 1] = { 0UL };
unsigned long address = 0UL;
if (spurious_address)
pr_info("[IPI Module INFO] - Try to find the target CALL instruction with the knowledge obtained from source code analysis.\n");
if (sys_vtpmo(entry_address) != NO_MAP)
{
level_address[level] = entry_address;
follow_the_flow:
byte = (unsigned char *) level_address[level];
for (b=0, count=0; b<SEQUENCE_MAX_BYTES; b+=count)
{
count = disassemble(&byte[b], SEQUENCE_MAX_BYTES - b, ((unsigned int) (((unsigned long) &byte[b]) & 0xffffffffUL)), disassembled);
if (byte[b] == 0xC2 || byte[b] == 0xC3 || byte[b] == 0xCA || byte[b] == 0xCB || byte[b] == 0xCF) // RET
{
if (level)
{
level_address[level--] = 0UL;
goto follow_the_flow;
}
else
break;
}
else if (byte[b] == 0xE9 || byte[b] == 0xEA || byte[b] == 0xEB) // JMP
{
long jmp_address;
if ((jmp_address = resolve_jmp_address(&byte[b], count)) == 0)
break;
if (sys_vtpmo(jmp_address) != NO_MAP)
{
level_address[level] = *((unsigned long *) &jmp_address);
goto follow_the_flow;
}
else
break;
}
else if (byte[b] == 0x9A || byte[b] == 0xE8) // CALL
{
long call_address;
if ((call_address = resolve_call_address(&byte[b], count)) == 0)
break;
if (spurious_address)
{
/* Either symbols have been exported or the address
has been recovered via the kprobe service ... in
any case we know which is the operand to replace
within spurious irq-entry routine. */
if (call_address == spurious_address)
{
old_call_operand = get_call_operand(&byte[b], count);
new_call_operand = (unsigned int) ((long) handle_ibs_irq - ((long) &byte[b+count]));
call_operand_address = (unsigned int *) &byte[b+1];
cr0 = _read_cr0();
_write_cr0(cr0 & ~X86_CR0_WP);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,17,0)
arch_cmpxchg(call_operand_address, old_call_operand, new_call_operand);
#else
cmpxchg(call_operand_address, old_call_operand, new_call_operand);
#endif
_write_cr0(cr0);
pr_info("[IPI Module INFO] - Address 0x%lx of the spurious interrupt handler is called at 0x%lx.\n", spurious_address, (long) &byte[b]);
address = call_address;
break;
}
}
else if (level == 0)
{
/* Symbols may also be non-exported, nor readable
by kallsysm_lookup, but the entry_64.S source code
doesn's lie ... the second CALL instruction encountered
within the spurious irq-entry routine gives control to
the spurious interrupt handler. */
if ((++level_0_call_count) == 2)
{
old_call_operand = get_call_operand(&byte[b], count);
new_call_operand = (unsigned int) ((long) handle_ibs_irq - ((long) &byte[b+count]));
call_operand_address = (unsigned int *) &byte[b+1];
cr0 = _read_cr0();
_write_cr0(cr0 & ~X86_CR0_WP);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,17,0)
arch_cmpxchg(call_operand_address, old_call_operand, new_call_operand);
#else
cmpxchg(call_operand_address, old_call_operand, new_call_operand);
#endif
_write_cr0(cr0);
pr_info("[IPI Module INFO] - Address of the spurious interrupt handler (unknown symbol) is called at 0x%lx.\n", (long) &byte[b]);
address = call_address;
break;
}
}
if (call_address)
{
if (sys_vtpmo(call_address) != NO_MAP)
{
if (level < MAX_LEVEL)
{
level_address[level++] = (unsigned long) &byte[b + count];
level_address[level] = *((unsigned long *) &call_address);
goto follow_the_flow;
}
}
}
}
}
}
return address;
}
#endif
static int setup_idt_entry(void)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
unsigned long entry_spurious_address;
unsigned long smp_spurious_address;
if (!(entry_spurious_address = get_address_entry_idt(ibs_vector)))
return -ENODATA;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,8,0)
if (get_address_from_symbol(&smp_spurious_address, "sysvec_spurious_apic_interrupt"))
#else
if (get_address_from_symbol(&smp_spurious_address, "smp_spurious_interrupt"))
#endif
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,7,7)
if (replace_call_address_through_binary_inspection(entry_spurious_address, 0UL))
return 0;
#endif
pr_err("IBS: Unable to find and replace the operand of the CALL instruction.\n");
return -ENODATA;
}
if (replace_call_address_through_binary_inspection(entry_spurious_address, smp_spurious_address))
return 0;
return -ENODATA;
#else
struct desc_ptr idtr;
gate_desc ibs_desc;
unsigned long cr0;
store_idt(&idtr);
memcpy(&old_ibs, (void*)(idtr.address + ibs_vector * sizeof(gate_desc)), sizeof(gate_desc));
pack_gate(&ibs_desc, GATE_INTERRUPT, (unsigned long)ibs_entry, 0, 0, 0);
cr0 = _read_cr0();
_write_cr0(cr0 & ~X86_CR0_WP);
write_idt_entry((gate_desc*)idtr.address, ibs_vector, &ibs_desc);
_write_cr0(cr0);
return 0;
#endif
}
static void restore_idt_entry(void)
{
unsigned long cr0;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
cr0 = _read_cr0();
_write_cr0(cr0 & ~X86_CR0_WP);
# if LINUX_VERSION_CODE >= KERNEL_VERSION(4,17,0)
arch_cmpxchg(call_operand_address, new_call_operand, old_call_operand);
# else
cmpxchg(call_operand_address, new_call_operand, old_call_operand);
# endif
_write_cr0(cr0);
#else
struct desc_ptr idtr;
store_idt(&idtr);
cr0 = _read_cr0();
_write_cr0(cr0 & ~X86_CR0_WP);
write_idt_entry((gate_desc*)idtr.address, ibs_vector, &old_ibs);
_write_cr0(cr0);
#endif
}
static void setup_ibs_lvt(void *err)
{
u64 reg;
u64 ibs_ctl;
u32 entry;
u32 new_entry;
u8 offset;
rdmsrl(MSR_IBS_CONTROL, ibs_ctl);
if (!(ibs_ctl & IBS_LVT_OFFSET_VAL))
{
pr_err("IBS: APIC setup failed - invalid offset by MSR_bits: %llu\n", ibs_ctl);
goto no_offset;
}
offset = ibs_ctl & IBS_LVT_OFFSET;
pr_info("IBS: IBS_CTL offset is %u\n", offset);
reg = APIC_EILVTn(offset);
entry = apic_read(reg);
/* Print the 2 LSB in APIC register : | mask | msg_type | vector | */
pr_info("IBS: APIC of CPU %u - READ offset %u -> | %lu | %lu | %lu |\n",
smp_processor_id(), offset, ((entry >> 16) & 0xFUL), ((entry >> 8) & 0xFUL), (entry & 0xFFUL));
new_entry = (0UL) | (APIC_EILVT_MSG_FIX << 8) | (ibs_vector);
if (entry != new_entry || !((entry >> 16) & 0xFUL))
{
if (!setup_APIC_eilvt(offset, 0, 0, 1))
{
pr_info("IBS: cleared LVT entry 0x%x on cpu %i\n", offset, smp_processor_id());
reg = APIC_EILVTn(offset);
entry = apic_read(reg);
}
else
goto fail;
}
if (!setup_APIC_eilvt(offset, ibs_vector, APIC_EILVT_MSG_FIX, 0))
pr_info("IBS: LVT entry 0x%x setup on cpu %i\n", offset, smp_processor_id());
else
goto fail;
return;
fail:
pr_err("IBS: APIC setup failed - cannot set up the LVT entry 0x%x on cpu %i\n", offset, smp_processor_id());
no_offset:
*((int*)err) = -1;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,15,0)
static void mask_ibs_lvt(void *err)
{
u64 reg;
u64 ibs_ctl;
u32 entry;
u8 offset;
rdmsrl(MSR_IBS_CONTROL, ibs_ctl);
offset = ibs_ctl & IBS_LVT_OFFSET;
reg = APIC_EILVTn(offset);
entry = apic_read(reg);
if (setup_APIC_eilvt(offset, (entry & 0xFFUL), ((entry >> 8) & 0xFUL), 1UL))
goto fail;
return;
fail:
*((int*)err) = -1;
pr_err("IBS: APIC setup failed - cannot mask the LVT entry #%i on cpu %i\n", offset, smp_processor_id());
}
#endif
int setup_ibs_irq(void)
{
int err;
err = acquire_free_vector();
if (err)
goto out;
err = setup_idt_entry();
if (err)
goto out;
on_each_cpu(setup_ibs_lvt, &err, 1);
out:
return err;
}
void cleanup_ibs_irq(void)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
restore_idt_entry();
pr_info("IBS: call to smp_spurious_interrupt has been correctly restored.\n");
#else
int err = 0;