forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lwpmudrv.c
8034 lines (7074 loc) · 215 KB
/
lwpmudrv.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
/* ****************************************************************************
* Copyright(C) 2009-2018 Intel Corporation. All Rights Reserved.
*
* This file is part of SEP Development Kit
*
* SEP Development Kit 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.
*
* SEP Development Kit 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.
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you
* compile this file and link it with other files to produce an executable
* this file does not by itself cause the resulting executable to be
* covered by the GNU General Public License. This exception does not
* however invalidate any other reasons why the executable file might be
* covered by the GNU General Public License.
* ****************************************************************************
*/
#include "lwpmudrv_defines.h"
#include "lwpmudrv_version.h"
#include <linux/version.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <asm/page.h>
#include <linux/cdev.h>
#include <linux/proc_fs.h>
#include <linux/fcntl.h>
#include <linux/device.h>
#include <linux/ptrace.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
#include <linux/sched/clock.h>
#else
#include <linux/sched.h>
#endif
#include <linux/syscalls.h>
#include <asm/unistd.h>
#include <linux/compat.h>
#include <linux/vmalloc.h>
#include <linux/kthread.h>
#if defined(CONFIG_HYPERVISOR_GUEST)
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34)
#include <asm/hypervisor.h>
#endif
#endif
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32)
#include <xen/xen.h>
#endif
#if defined(CONFIG_XEN_HAVE_VPMU)
#include <asm/xen/hypercall.h>
#include <asm/xen/page.h>
#include <xen/interface/xenpmu.h>
#endif
#include "lwpmudrv_types.h"
#include "lwpmudrv_ecb.h"
#include "lwpmudrv_ioctl.h"
#include "lwpmudrv_struct.h"
#include "inc/ecb_iterators.h"
#include "inc/unc_common.h"
#if defined(BUILD_GFX)
#include "gfx.h"
#endif
#if defined(BUILD_CHIPSET)
#include "lwpmudrv_chipset.h"
#endif
#include "pci.h"
#include "apic.h"
#include "cpumon.h"
#include "lwpmudrv.h"
#include "utility.h"
#include "control.h"
#include "core2.h"
#include "pmi.h"
#include "output.h"
#include "linuxos.h"
#include "sys_info.h"
#include "eventmux.h"
#include "pebs.h"
MODULE_AUTHOR("Copyright(C) 2007-2018 Intel Corporation");
MODULE_VERSION(SEP_NAME "_" SEP_VERSION_STR);
MODULE_LICENSE("Dual BSD/GPL");
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,7,0)
#define MAX_ADDR_STR_LEN 16
#define MAX_CHARS_IN_LINE 512
char sym_lookup_func_addr[MAX_ADDR_STR_LEN+1];
#endif
static struct task_struct *abnormal_handler;
typedef struct LWPMU_DEV_NODE_S LWPMU_DEV_NODE;
typedef LWPMU_DEV_NODE * LWPMU_DEV;
struct LWPMU_DEV_NODE_S {
long buffer;
struct semaphore sem;
struct cdev cdev;
};
#define LWPMU_DEV_buffer(dev) ((dev)->buffer)
#define LWPMU_DEV_sem(dev) ((dev)->sem)
#define LWPMU_DEV_cdev(dev) ((dev)->cdev)
/* Global variables of the driver */
static SEP_VERSION_NODE drv_version;
U64 *read_counter_info;
U64 *prev_counter_data;
static U64 prev_counter_size;
VOID **desc_data;
U64 total_ram;
U32 output_buffer_size = OUTPUT_LARGE_BUFFER;
U32 saved_buffer_size;
static S32 desc_count;
uid_t uid;
DRV_CONFIG drv_cfg;
static DEV_CONFIG cur_pcfg;
volatile pid_t control_pid;
U64 *interrupt_counts;
static LWPMU_DEV lwpmu_control;
static LWPMU_DEV lwmod_control;
static LWPMU_DEV lwsamp_control;
static LWPMU_DEV lwsampunc_control;
static LWPMU_DEV lwsideband_control;
EMON_BUFFER_DRIVER_HELPER emon_buffer_driver_helper;
/* needed for multiple devices (core/uncore) */
U32 num_devices;
static U32 num_core_devs;
U32 cur_device;
LWPMU_DEVICE devices;
static U32 uncore_em_factor;
static unsigned long unc_timer_interval;
static struct timer_list *unc_read_timer;
static S32 max_groups_unc;
DRV_BOOL multi_pebs_enabled = FALSE;
DRV_BOOL unc_buf_init = FALSE;
DRV_BOOL NMI_mode = TRUE;
DRV_BOOL KVM_guest_mode = FALSE;
DRV_SETUP_INFO_NODE req_drv_setup_info;
/* needed for target agent support */
U32 osid = OS_ID_NATIVE;
DRV_BOOL sched_switch_enabled = FALSE;
#if defined(DRV_SEP_ACRN_ON)
struct profiling_vm_info_list *vm_info_list;
static struct timer_list *buffer_read_timer;
static unsigned long buffer_timer_interval;
shared_buf_t **samp_buf_per_cpu;
#endif
#define UNCORE_EM_GROUP_SWAP_FACTOR 100
#define PMU_DEVICES 2 // pmu, mod
extern U32 *cpu_built_sysinfo;
#define DRV_DEVICE_DELIMITER "!"
#if defined(DRV_USE_UNLOCKED_IOCTL)
static struct mutex ioctl_lock;
#endif
#if defined(BUILD_CHIPSET)
CHIPSET_CONFIG pma;
CS_DISPATCH cs_dispatch;
#endif
static S8 *cpu_mask_bits;
/*
* Global data: Buffer control structure
*/
BUFFER_DESC cpu_buf;
BUFFER_DESC unc_buf;
BUFFER_DESC module_buf;
BUFFER_DESC cpu_sideband_buf;
static dev_t lwpmu_DevNum; /* the major and minor parts for SEP3 base */
static dev_t lwsamp_DevNum; /* the major and minor parts for SEP3 percpu */
static dev_t lwsampunc_DevNum;
/* the major and minor parts for SEP3 per package */
static dev_t lwsideband_DevNum;
static struct class *pmu_class;
//extern volatile int config_done;
CPU_STATE pcb;
static size_t pcb_size;
U32 *core_to_package_map;
U32 *core_to_phys_core_map;
U32 *core_to_thread_map;
U32 *core_to_dev_map;
U32 *threads_per_core;
U32 num_packages;
U64 *pmu_state;
U64 *cpu_tsc;
static U64 *prev_cpu_tsc;
static U64 *diff_cpu_tsc;
U64 *restore_bl_bypass;
U32 **restore_ha_direct2core;
U32 **restore_qpi_direct2core;
U32 *occupied_core_ids;
UNCORE_TOPOLOGY_INFO_NODE uncore_topology;
PLATFORM_TOPOLOGY_PROG_NODE platform_topology_prog_node;
static PLATFORM_TOPOLOGY_PROG_NODE req_platform_topology_prog_node;
#if !defined(DRV_SEP_ACRN_ON)
static U8 *prev_set_CR4;
#endif
wait_queue_head_t wait_exit;
extern OS_STATUS SOCPERF_Switch_Group3(void);
#if !defined(DRV_USE_UNLOCKED_IOCTL)
#define MUTEX_INIT(lock)
#define MUTEX_LOCK(lock)
#define MUTEX_UNLOCK(lock)
#else
#define MUTEX_INIT(lock) mutex_init(&(lock));
#define MUTEX_LOCK(lock) mutex_lock(&(lock))
#define MUTEX_UNLOCK(lock) mutex_unlock(&(lock))
#endif
#if defined(CONFIG_XEN_HAVE_VPMU)
typedef struct xen_pmu_params xen_pmu_params_t;
typedef struct xen_pmu_data xen_pmu_data_t;
static DEFINE_PER_CPU(xen_pmu_data_t *, sep_xenpmu_shared);
#endif
/* ------------------------------------------------------------------------- */
/*!
* @fn void lwpmudrv_PWR_Info(IOCTL_ARGS arg)
*
* @param arg - Pointer to the IOCTL structure
*
* @return OS_STATUS
*
* @brief Make a copy of the Power control information that has been passed in.
*
* <I>Special Notes</I>
*/
static OS_STATUS lwpmudrv_PWR_Info(IOCTL_ARGS arg)
{
SEP_DRV_LOG_FLOW_IN("");
if (DEV_CONFIG_power_capture(cur_pcfg) == FALSE) {
SEP_DRV_LOG_WARNING_FLOW_OUT(
"'Success' (Power capture is disabled!).");
return OS_SUCCESS;
}
// make sure size of incoming arg is correct
if ((arg->len_usr_to_drv != sizeof(PWR_NODE)) ||
(arg->buf_usr_to_drv == NULL)) {
SEP_DRV_LOG_ERROR_FLOW_OUT(
"OS_FAULT (PWR capture has not been configured!).");
return OS_FAULT;
}
//
// First things first: Make a copy of the data for global use.
//
LWPMU_DEVICE_pwr(&devices[cur_device]) =
CONTROL_Allocate_Memory((int)arg->len_usr_to_drv);
if (!LWPMU_DEVICE_pwr(&devices[cur_device])) {
SEP_DRV_LOG_ERROR_FLOW_OUT("Memory allocation failure!");
return OS_NO_MEM;
}
if (copy_from_user(LWPMU_DEVICE_pwr(&devices[cur_device]),
(void __user *)arg->buf_usr_to_drv, arg->len_usr_to_drv)) {
SEP_DRV_LOG_ERROR_FLOW_OUT("Memory copy failure!");
return OS_FAULT;
}
SEP_DRV_LOG_FLOW_OUT("Success");
return OS_SUCCESS;
}
/*
* @fn void lwpmudrv_Allocate_Restore_Buffer
*
* @param
*
* @return OS_STATUS
*
* @brief allocate buffer space to save/restore the data (for JKT, QPILL and HA register) before collection
*/
static OS_STATUS lwpmudrv_Allocate_Restore_Buffer(void)
{
int i = 0;
SEP_DRV_LOG_TRACE_IN("");
if (!restore_ha_direct2core) {
restore_ha_direct2core = CONTROL_Allocate_Memory(
GLOBAL_STATE_num_cpus(driver_state) * sizeof(U32 *));
if (!restore_ha_direct2core) {
SEP_DRV_LOG_ERROR_TRACE_OUT(
"Memory allocation failure for restore_ha_direct2core!");
return OS_NO_MEM;
}
for (i = 0; i < GLOBAL_STATE_num_cpus(driver_state); i++) {
restore_ha_direct2core[i] = CONTROL_Allocate_Memory(
MAX_BUSNO * sizeof(U32));
}
}
if (!restore_qpi_direct2core) {
restore_qpi_direct2core = CONTROL_Allocate_Memory(
GLOBAL_STATE_num_cpus(driver_state) * sizeof(U32 *));
if (!restore_qpi_direct2core) {
SEP_DRV_LOG_ERROR_TRACE_OUT(
"Memory allocation failure for restore_qpi_direct2core!");
return OS_NO_MEM;
}
for (i = 0; i < GLOBAL_STATE_num_cpus(driver_state); i++) {
restore_qpi_direct2core[i] = CONTROL_Allocate_Memory(
2 * MAX_BUSNO * sizeof(U32));
}
}
if (!restore_bl_bypass) {
restore_bl_bypass = CONTROL_Allocate_Memory(
GLOBAL_STATE_num_cpus(driver_state) * sizeof(U64));
if (!restore_bl_bypass) {
SEP_DRV_LOG_ERROR_TRACE_OUT(
"Memory allocation failure for restore_bl_bypass!");
return OS_NO_MEM;
}
}
SEP_DRV_LOG_TRACE_OUT("Success");
return OS_SUCCESS;
}
/*
* @fn void lwpmudrv_Allocate_Uncore_Buffer
*
* @param
*
* @return OS_STATUS
*
* @brief allocate buffer space for writing/reading uncore data
*/
static OS_STATUS lwpmudrv_Allocate_Uncore_Buffer(void)
{
U32 i, j, k, l;
U32 max_entries = 0;
U32 num_entries;
ECB ecb;
SEP_DRV_LOG_TRACE_IN(
""); // this function is not checking memory allocations properly
for (i = num_core_devs; i < num_devices; i++) {
if (!LWPMU_DEVICE_pcfg(&devices[i])) {
continue;
}
LWPMU_DEVICE_acc_value(&devices[i]) =
CONTROL_Allocate_Memory(num_packages * sizeof(U64 **));
LWPMU_DEVICE_prev_value(&devices[i]) =
CONTROL_Allocate_Memory(num_packages * sizeof(U64 *));
for (j = 0; j < num_packages; j++) {
// Allocate memory and zero out accumulator array (one per group)
LWPMU_DEVICE_acc_value(&devices[i])[j] =
CONTROL_Allocate_Memory(
LWPMU_DEVICE_em_groups_count(
&devices[i]) *
sizeof(U64 *));
for (k = 0;
k < LWPMU_DEVICE_em_groups_count(&devices[i]);
k++) {
ecb = LWPMU_DEVICE_PMU_register_data(
&devices[i])[k];
num_entries =
ECB_num_events(ecb) *
LWPMU_DEVICE_num_units(&devices[i]);
LWPMU_DEVICE_acc_value(&devices[i])[j][k] =
CONTROL_Allocate_Memory(num_entries *
sizeof(U64));
for (l = 0; l < num_entries; l++) {
LWPMU_DEVICE_acc_value(
&devices[i])[j][k][l] = 0LL;
}
if (max_entries < num_entries) {
max_entries = num_entries;
}
}
// Allocate memory and zero out prev_value array (one across groups)
LWPMU_DEVICE_prev_value(&devices[i])[j] =
CONTROL_Allocate_Memory(max_entries *
sizeof(U64));
for (k = 0; k < max_entries; k++) {
LWPMU_DEVICE_prev_value(&devices[i])[j][k] =
0LL;
}
}
max_entries = 0;
}
SEP_DRV_LOG_TRACE_OUT("Success");
return OS_SUCCESS;
}
/*
* @fn void lwpmudrv_Free_Uncore_Buffer
*
* @param
*
* @return OS_STATUS
*
* @brief Free uncore data buffers
*/
static OS_STATUS lwpmudrv_Free_Uncore_Buffer(U32 i)
{
U32 j, k;
SEP_DRV_LOG_TRACE_IN("");
if (LWPMU_DEVICE_prev_value(&devices[i])) {
for (j = 0; j < num_packages; j++) {
LWPMU_DEVICE_prev_value(&devices[i])[j] =
CONTROL_Free_Memory(LWPMU_DEVICE_prev_value(
&devices[i])[j]);
}
LWPMU_DEVICE_prev_value(&devices[i]) = CONTROL_Free_Memory(
LWPMU_DEVICE_prev_value(&devices[i]));
}
if (LWPMU_DEVICE_acc_value(&devices[i])) {
for (j = 0; j < num_packages; j++) {
if (LWPMU_DEVICE_acc_value(&devices[i])[j]) {
for (k = 0; k < LWPMU_DEVICE_em_groups_count(
&devices[i]);
k++) {
LWPMU_DEVICE_acc_value(
&devices[i])[j][k] =
CONTROL_Free_Memory(
LWPMU_DEVICE_acc_value(
&devices[i])[j]
[k]);
}
LWPMU_DEVICE_acc_value(&devices[i])[j] =
CONTROL_Free_Memory(
LWPMU_DEVICE_acc_value(
&devices[i])[j]);
}
}
LWPMU_DEVICE_acc_value(&devices[i]) = CONTROL_Free_Memory(
LWPMU_DEVICE_acc_value(&devices[i]));
}
SEP_DRV_LOG_TRACE_OUT("Success");
return OS_SUCCESS;
}
/*
* @fn void lwpmudrv_Free_Restore_Buffer
*
* @param
*
* @return OS_STATUS
*
* @brief allocate buffer space to save/restore the data (for JKT, QPILL and HA register) before collection
*/
static OS_STATUS lwpmudrv_Free_Restore_Buffer(void)
{
U32 i = 0;
SEP_DRV_LOG_TRACE_IN("");
if (restore_ha_direct2core) {
for (i = 0; i < GLOBAL_STATE_num_cpus(driver_state); i++) {
restore_ha_direct2core[i] =
CONTROL_Free_Memory(restore_ha_direct2core[i]);
}
restore_ha_direct2core =
CONTROL_Free_Memory(restore_ha_direct2core);
}
if (restore_qpi_direct2core) {
for (i = 0; i < GLOBAL_STATE_num_cpus(driver_state); i++) {
restore_qpi_direct2core[i] =
CONTROL_Free_Memory(restore_qpi_direct2core[i]);
}
restore_qpi_direct2core =
CONTROL_Free_Memory(restore_qpi_direct2core);
}
if (restore_bl_bypass) {
restore_bl_bypass = CONTROL_Free_Memory(restore_bl_bypass);
}
SEP_DRV_LOG_TRACE_OUT("Success");
return OS_SUCCESS;
}
/* ------------------------------------------------------------------------- */
/*!
* @fn static OS_STATUS lwpmudrv_Initialize_State(void)
*
* @param none
*
* @return OS_STATUS
*
* @brief Allocates the memory needed at load time. Initializes all the
* @brief necessary state variables with the default values.
*
* <I>Special Notes</I>
*/
static OS_STATUS lwpmudrv_Initialize_State(void)
{
S32 i, max_cpu_id = 0;
SEP_DRV_LOG_INIT_IN("");
for_each_possible_cpu (i) {
if (cpu_present(i)) {
if (i > max_cpu_id) {
max_cpu_id = i;
}
}
}
max_cpu_id++;
/*
* Machine Initializations
* Abstract this information away into a separate entry point
*
* Question: Should we allow for the use of Hot-cpu
* add/subtract functionality while the driver is executing?
*/
if (max_cpu_id > num_present_cpus()) {
GLOBAL_STATE_num_cpus(driver_state) = max_cpu_id;
} else {
GLOBAL_STATE_num_cpus(driver_state) = num_present_cpus();
}
GLOBAL_STATE_active_cpus(driver_state) = num_online_cpus();
GLOBAL_STATE_cpu_count(driver_state) = 0;
GLOBAL_STATE_dpc_count(driver_state) = 0;
GLOBAL_STATE_num_em_groups(driver_state) = 0;
CHANGE_DRIVER_STATE(STATE_BIT_ANY, DRV_STATE_UNINITIALIZED);
SEP_DRV_LOG_INIT_OUT("Success: num_cpus=%d, active_cpus=%d.",
GLOBAL_STATE_num_cpus(driver_state),
GLOBAL_STATE_active_cpus(driver_state));
return OS_SUCCESS;
}
#if !defined(CONFIG_PREEMPT_COUNT) && !defined(DRV_SEP_ACRN_ON)
/* ------------------------------------------------------------------------- */
/*!
* @fn static void lwpmudrv_Fill_TSC_Info (PVOID param)
*
* @param param - pointer the buffer to fill in.
*
* @return none
*
* @brief Read the TSC and write into the correct array slot.
*
* <I>Special Notes</I>
*/
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 13, 0)
atomic_t read_now;
static wait_queue_head_t read_tsc_now;
#endif
static VOID lwpmudrv_Fill_TSC_Info(PVOID param)
{
U32 this_cpu;
SEP_DRV_LOG_TRACE_IN("");
preempt_disable();
this_cpu = CONTROL_THIS_CPU();
preempt_enable();
//
// Wait until all CPU's are ready to proceed
// This will serve as a synchronization point to compute tsc skews.
//
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 13, 0)
if (atomic_read(&read_now) >= 1) {
if (atomic_dec_and_test(&read_now) == FALSE) {
wait_event_interruptible(read_tsc_now,
(atomic_read(&read_now) >= 1));
}
} else {
wake_up_interruptible_all(&read_tsc_now);
}
#endif
UTILITY_Read_TSC(&cpu_tsc[this_cpu]);
SEP_DRV_LOG_TRACE("This cpu %d --- tsc --- 0x%llx.", this_cpu,
cpu_tsc[this_cpu]);
SEP_DRV_LOG_TRACE_OUT("Success");
}
#endif
/*********************************************************************
* Internal Driver functions
* Should be called only from the lwpmudrv_DeviceControl routine
*********************************************************************/
/* ------------------------------------------------------------------------- */
/*!
* @fn static void lwpmudrv_Dump_Tracer(const char *)
*
* @param Name of the tracer
*
* @return void
*
* @brief Function that handles the generation of markers into the ftrace stream
*
* <I>Special Notes</I>
*/
static void lwpmudrv_Dump_Tracer(const char *name, U64 tsc)
{
SEP_DRV_LOG_TRACE_IN("");
if (tsc == 0) {
preempt_disable();
UTILITY_Read_TSC(&tsc);
tsc -= TSC_SKEW(CONTROL_THIS_CPU());
preempt_enable();
}
SEP_DRV_LOG_TRACE_OUT("Success");
}
/* ------------------------------------------------------------------------- */
/*!
* @fn static OS_STATUS lwpmudrv_Version(IOCTL_ARGS arg)
*
* @param arg - pointer to the IOCTL_ARGS structure
*
* @return OS_STATUS
*
* @brief Local function that handles the LWPMU_IOCTL_VERSION call.
* @brief Returns the version number of the kernel mode sampling.
*
* <I>Special Notes</I>
*/
static OS_STATUS lwpmudrv_Version(IOCTL_ARGS arg)
{
OS_STATUS status;
SEP_DRV_LOG_FLOW_IN("");
// Check if enough space is provided for collecting the data
if ((arg->len_drv_to_usr != sizeof(U32)) ||
(arg->buf_drv_to_usr == NULL)) {
SEP_DRV_LOG_ERROR_FLOW_OUT("Invalid arguments.");
return OS_FAULT;
}
status = put_user(SEP_VERSION_NODE_sep_version(&drv_version),
(U32 __user *)arg->buf_drv_to_usr);
SEP_DRV_LOG_FLOW_OUT("Return value: %d", status);
return status;
}
/* ------------------------------------------------------------------------- */
/*!
* @fn static OS_STATUS lwpmudrv_Reserve(IOCTL_ARGS arg)
*
* @param arg - pointer to the IOCTL_ARGS structure
*
* @return OS_STATUS
*
* @brief
* @brief Local function that handles the LWPMU_IOCTL_RESERVE call.
* @brief Sets the state to RESERVED if possible. Returns BUSY if unable
* @brief to reserve the PMU.
*
* <I>Special Notes</I>
*/
static OS_STATUS lwpmudrv_Reserve(IOCTL_ARGS arg)
{
OS_STATUS status = OS_SUCCESS;
SEP_DRV_LOG_FLOW_IN("");
// Check if enough space is provided for collecting the data
if ((arg->len_drv_to_usr != sizeof(S32)) ||
(arg->buf_drv_to_usr == NULL)) {
SEP_DRV_LOG_ERROR_FLOW_OUT("Invalid arguments.");
return OS_FAULT;
}
status = put_user(!CHANGE_DRIVER_STATE(STATE_BIT_UNINITIALIZED,
DRV_STATE_RESERVED),
(int __user*)arg->buf_drv_to_usr);
SEP_DRV_LOG_FLOW_OUT("Return value: %d", status);
return status;
}
#if !defined(DRV_SEP_ACRN_ON)
/* ------------------------------------------------------------------------- */
/*!
* @fn static OS_STATUS lwpmudrv_Finish_Op(void)
*
* @param - none
*
* @return OS_STATUS
*
* @brief Finalize PMU after collection
*
* <I>Special Notes</I>
*/
static VOID lwpmudrv_Finish_Op(PVOID param)
{
U32 this_cpu = CONTROL_THIS_CPU();
U32 dev_idx = core_to_dev_map[this_cpu];
DISPATCH dispatch = LWPMU_DEVICE_dispatch(&devices[dev_idx]);
SEP_DRV_LOG_TRACE_IN("");
if (dispatch != NULL && dispatch->fini != NULL) {
dispatch->fini(&dev_idx);
}
SEP_DRV_LOG_TRACE_OUT("");
}
#endif
/* ------------------------------------------------------------------------- */
/*!
* @fn static VOID lwpmudrv_Clean_Up(DRV_BOOL)
*
* @param DRV_BOOL finish - Flag to call finish
*
* @return VOID
*
* @brief Cleans up the memory allocation.
*
* <I>Special Notes</I>
*/
static VOID lwpmudrv_Clean_Up(DRV_BOOL finish)
{
U32 i;
SEP_DRV_LOG_FLOW_IN("");
if (DRV_CONFIG_use_pcl(drv_cfg) == TRUE) {
drv_cfg = CONTROL_Free_Memory(drv_cfg);
goto signal_end;
}
if (devices) {
U32 id;
U32 num_groups = 0;
EVENT_CONFIG ec;
DISPATCH dispatch_unc = NULL;
for (id = 0; id < num_devices; id++) {
if (LWPMU_DEVICE_pcfg(&devices[id])) {
if (LWPMU_DEVICE_device_type(&devices[id]) ==
DEVICE_INFO_UNCORE) {
dispatch_unc = LWPMU_DEVICE_dispatch(
&devices[id]);
if (dispatch_unc &&
dispatch_unc->fini) {
SEP_DRV_LOG_TRACE(
"LWP: calling UNC Init.");
dispatch_unc->fini(
(PVOID *)&id);
}
lwpmudrv_Free_Uncore_Buffer(id);
} else if (finish) {
#if !defined(DRV_SEP_ACRN_ON)
CONTROL_Invoke_Parallel(
lwpmudrv_Finish_Op, NULL);
#endif
}
}
if (LWPMU_DEVICE_PMU_register_data(&devices[id])) {
ec = LWPMU_DEVICE_ec(&devices[id]);
if (LWPMU_DEVICE_device_type(&devices[id]) ==
DEVICE_INFO_CORE) {
num_groups =
EVENT_CONFIG_num_groups(ec);
} else {
num_groups =
EVENT_CONFIG_num_groups_unc(ec);
}
for (i = 0; i < num_groups; i++) {
LWPMU_DEVICE_PMU_register_data(
&devices[id])[i] =
CONTROL_Free_Memory(
LWPMU_DEVICE_PMU_register_data(
&devices[id])[i]);
}
LWPMU_DEVICE_PMU_register_data(&devices[id]) =
CONTROL_Free_Memory(
LWPMU_DEVICE_PMU_register_data(
&devices[id]));
}
LWPMU_DEVICE_pcfg(&devices[id]) = CONTROL_Free_Memory(
LWPMU_DEVICE_pcfg(&devices[id]));
LWPMU_DEVICE_ec(&devices[id]) = CONTROL_Free_Memory(
LWPMU_DEVICE_ec(&devices[id]));
if (LWPMU_DEVICE_lbr(&devices[id])) {
LWPMU_DEVICE_lbr(&devices[id]) =
CONTROL_Free_Memory(
LWPMU_DEVICE_lbr(&devices[id]));
}
if (LWPMU_DEVICE_pwr(&devices[id])) {
LWPMU_DEVICE_pwr(&devices[id]) =
CONTROL_Free_Memory(
LWPMU_DEVICE_pwr(&devices[id]));
}
if (LWPMU_DEVICE_cur_group(&devices[id])) {
LWPMU_DEVICE_cur_group(&devices[id]) =
CONTROL_Free_Memory(
LWPMU_DEVICE_cur_group(
&devices[id]));
}
}
devices = CONTROL_Free_Memory(devices);
}
if (desc_data) {
for (i = 0; i < GLOBAL_STATE_num_descriptors(driver_state);
i++) {
desc_data[i] = CONTROL_Free_Memory(desc_data[i]);
}
desc_data = CONTROL_Free_Memory(desc_data);
}
if (restore_bl_bypass) {
restore_bl_bypass = CONTROL_Free_Memory(restore_bl_bypass);
}
if (restore_qpi_direct2core) {
for (i = 0; i < GLOBAL_STATE_num_cpus(driver_state); i++) {
restore_qpi_direct2core[i] =
CONTROL_Free_Memory(restore_qpi_direct2core[i]);
}
restore_qpi_direct2core =
CONTROL_Free_Memory(restore_qpi_direct2core);
}
if (restore_ha_direct2core) {
for (i = 0; i < GLOBAL_STATE_num_cpus(driver_state); i++) {
restore_ha_direct2core[i] =
CONTROL_Free_Memory(restore_ha_direct2core[i]);
}
restore_ha_direct2core =
CONTROL_Free_Memory(restore_ha_direct2core);
}
drv_cfg = CONTROL_Free_Memory(drv_cfg);
pmu_state = CONTROL_Free_Memory(pmu_state);
cpu_mask_bits = CONTROL_Free_Memory(cpu_mask_bits);
core_to_dev_map = CONTROL_Free_Memory(core_to_dev_map);
#if defined(BUILD_CHIPSET)
pma = CONTROL_Free_Memory(pma);
#endif
signal_end:
GLOBAL_STATE_num_em_groups(driver_state) = 0;
GLOBAL_STATE_num_descriptors(driver_state) = 0;
num_devices = 0;
num_core_devs = 0;
max_groups_unc = 0;
control_pid = 0;
unc_buf_init = FALSE;
OUTPUT_Cleanup();
memset(pcb, 0, pcb_size);
SEP_DRV_LOG_FLOW_OUT("");
}
/* ------------------------------------------------------------------------- */
/*!
* @fn static NTSTATUS lwpmudrv_Initialize_Driver (PVOID buf_usr_to_drv, size_t len_usr_to_drv)
*
* @param buf_usr_to_drv - pointer to the input buffer
* @param len_usr_to_drv - size of the input buffer
*
* @return NTSTATUS
*
* @brief Local function that handles the LWPMU_IOCTL_INIT_DRIVER call.
* @brief Sets up the interrupt handler.
* @brief Set up the output buffers/files needed to make the driver
* @brief operational.
*
* <I>Special Notes</I>
*/
static OS_STATUS lwpmudrv_Initialize_Driver(PVOID buf_usr_to_drv,
size_t len_usr_to_drv)
{
S32 cpu_num;
int status = OS_SUCCESS;
SEP_DRV_LOG_FLOW_IN("");
if (buf_usr_to_drv == NULL) {
SEP_DRV_LOG_ERROR_FLOW_OUT("Invalid arguments.");
return OS_FAULT;
}
if (!CHANGE_DRIVER_STATE(STATE_BIT_RESERVED, DRV_STATE_IDLE)) {
SEP_DRV_LOG_ERROR_FLOW_OUT("Unexpected driver state!");
return OS_FAULT;
}
interrupt_counts = NULL;
pmu_state = NULL;
drv_cfg = CONTROL_Allocate_Memory(len_usr_to_drv);
if (!drv_cfg) {
status = OS_NO_MEM;
SEP_DRV_LOG_ERROR("Memory allocation failure for drv_cfg!");
goto clean_return;
}
if (copy_from_user(drv_cfg, (void __user *)buf_usr_to_drv,
len_usr_to_drv)) {
SEP_DRV_LOG_ERROR("Memory copy failure for drv_cfg!");
status = OS_FAULT;
goto clean_return;
}
if (DRV_CONFIG_enable_cp_mode(drv_cfg)) {
#if (defined(DRV_EM64T))
if (output_buffer_size == OUTPUT_LARGE_BUFFER) {
output_buffer_size = OUTPUT_CP_BUFFER;
}
#endif
interrupt_counts = CONTROL_Allocate_Memory(
GLOBAL_STATE_num_cpus(driver_state) *
DRV_CONFIG_num_events(drv_cfg) * sizeof(U64));
if (interrupt_counts == NULL) {
SEP_DRV_LOG_ERROR(
"Memory allocation failure for interrupt_counts!");
status = OS_NO_MEM;
goto clean_return;
}
} else if (output_buffer_size == OUTPUT_CP_BUFFER) {
output_buffer_size = OUTPUT_LARGE_BUFFER;
}
if (DRV_CONFIG_use_pcl(drv_cfg) == TRUE) {
SEP_DRV_LOG_FLOW_OUT("Success, using PCL.");
return OS_SUCCESS;
}
pmu_state = CONTROL_Allocate_KMemory(
GLOBAL_STATE_num_cpus(driver_state) * sizeof(U64) * 3);
if (!pmu_state) {
SEP_DRV_LOG_ERROR("Memory allocation failure for pmu_state!");
status = OS_NO_MEM;
goto clean_return;
}
uncore_em_factor = 0;
for (cpu_num = 0; cpu_num < GLOBAL_STATE_num_cpus(driver_state);
cpu_num++) {
CPU_STATE_accept_interrupt(&pcb[cpu_num]) = 1;
CPU_STATE_initial_mask(&pcb[cpu_num]) = 1;
CPU_STATE_group_swap(&pcb[cpu_num]) = 1;
CPU_STATE_reset_mask(&pcb[cpu_num]) = 0;
CPU_STATE_num_samples(&pcb[cpu_num]) = 0;
CPU_STATE_last_p_state_valid(&pcb[cpu_num]) = FALSE;
#if defined(DRV_CPU_HOTPLUG)
CPU_STATE_offlined(&pcb[cpu_num]) = TRUE;
#else
CPU_STATE_offlined(&pcb[cpu_num]) = FALSE;
#endif
CPU_STATE_nmi_handled(&pcb[cpu_num]) = 0;
}
DRV_CONFIG_seed_name(drv_cfg) = NULL;
DRV_CONFIG_seed_name_len(drv_cfg) = 0;
SEP_DRV_LOG_TRACE("Config : size = %d.", DRV_CONFIG_size(drv_cfg));
SEP_DRV_LOG_TRACE("Config : counting_mode = %d.",
DRV_CONFIG_counting_mode(drv_cfg));
control_pid = current->pid;
SEP_DRV_LOG_TRACE("Control PID = %d.", control_pid);
if (core_to_dev_map == NULL) {
core_to_dev_map = CONTROL_Allocate_Memory(