-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdual_monitor_mode.c
1227 lines (1059 loc) · 33.9 KB
/
dual_monitor_mode.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
/* dual_monitor_mode.c: Interface to Intel's SMM Transfer Monitor (STM)
*
* This program contains functions that opt-in to STM and create STM policies
* to protect Linux's critical resources.
*
* Tejaswini Vibhute - <[email protected]> - Portland State University
*
* Copyright (C) 2018 Portland State University
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License along
* with this program; If not, see <http://www.gnu.org/licenses/>.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
#include "dual_monitor_mode.h"
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/kvm_host.h>
#include <linux/nmi.h>
#include <linux/reboot.h>
#include <linux/syscore_ops.h>
#include <linux/cpu.h>
#include <linux/suspend.h>
#include <linux/version.h>
#include <linux/printk.h>
#include <asm/apic.h>
#include <asm/processor.h>
#include <asm/msr.h>
#include <asm/vmx.h>
#include <asm/kvm_host.h>
#include <asm/uaccess.h>
#include <asm/virtext.h>
#include "vmx.h"
#include "vmx_ops.h"
MODULE_LICENSE ("GPL");
MODULE_SOFTDEP ("pre: kvm-intel");
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
extern struct kvm_x86_ops *kvm_x86_ops;
#endif
#define DEBUG
static DEFINE_PER_CPU (struct vmcs *, temp_vmcs);
static atomic_t stmbsp_done;
static u32 vmcs_revision_id;
static atomic_t bspready;
/* VMX_BASIC_MSR addition for Linux */
#define VMX_BASIC_DUAL_MONITOR (1ULL << 49)
#define MSR_VMX_BASIC_VMCS_REVISION_MASK 0x7FFFFFFFull
/* MSR_IA32_SMM_MONITOR_CTL addition for Linux */
#define IA32_SMM_MONITOR_CTL_VALID 0x0001
/*
* is_stm can take the following status codes. Each code represents the state of
* STM on the current system
* 0x00 : STM initialization not yet started
* 0x01 : STM successfully launched on all the logical CPUs
* 0x02 : STM launch failed
* 0x03 : STM not supported
* 0x04 : STM suspended
* 0x05 : STM suspended by PM_HIBERNATION_PREPARE
*/
#define STM_UINIT 0x00
#define STM_LAUNCHED 0x01
#define STM_FAILED 0x02
#define STM_NOT_SUP 0x03
#define STM_SHUTDOWN 0x04
#define STM_SHUTDOWN_PM 0x05
static atomic_t is_stm;
static atomic_t number_ap;
noinline void
dmm_fault ()
{
unsigned long cr4 = __read_cr4 ();
int cpu = smp_processor_id ();
pr_err ("%d STM-LINUX: DMM fault cr4: %lx\n", cpu, cr4);
if ((cr4 & X86_CR4_VMXE) == X86_CR4_VMXE)
pr_debug ("%d STM-LINUX VMXE bit set\n", cpu);
else
pr_debug ("%d STM-LINUX VMXE bit not set\n", cpu);
}
noinline void
vmclear_error (struct vmcs *vmcs, u64 phys_addr)
{
int cpu = smp_processor_id ();
pr_err ("%d STM-LINUX: vmclear failed: %p/%llx\n", cpu, vmcs, phys_addr);
}
/* from XEN */
#undef rdmsr_safe
/* rdmsr with exception handling */
#define rdmsr_safe(msr,val) ({\
int _rc; \
uint32_t lo, hi; \
__asm__ __volatile__( \
"1: rdmsr\n2:\n" \
".section .fixup,\"ax\"\n" \
"3: xorl %0,%0\n; xorl %1,%1\n" \
" movl %5,%2\n; jmp 2b\n" \
".previous\n" \
_ASM_EXTABLE(1b, 3b) \
: "=a" (lo), "=d" (hi), "=&r" (_rc) \
: "c" (msr), "2" (0), "i" (-EFAULT)); \
val = lo | ((uint64_t)hi << 32); \
_rc; })
/* from kvm */
struct vmcs *
alloc_vmcs_cpu (int cpu, gfp_t flags)
{
int node = cpu_to_node (cpu);
struct page *pages;
struct vmcs *vmcs;
pages = __alloc_pages_node (node, flags, get_order (4096));
if (!pages)
return NULL;
vmcs = page_address (pages);
memset (vmcs, 0, 4096);
vmcs->hdr.revision_id = vmcs_revision_id;
return vmcs;
}
struct vmcs *
vmx_temp_vmcs (void)
{
int cpu = smp_processor_id ();
return alloc_vmcs_cpu (cpu, GFP_KERNEL);
}
/*
* Set the temporary VMCS for the current CPU
*/
static void
set_temp_vmcs (void)
{
u64 msr_content = 0;
int cpu = raw_smp_processor_id ();
u64 phys_addr = __pa (per_cpu (temp_vmcs, cpu));
vmcs_clear (per_cpu (temp_vmcs, cpu));
//BUG - need to deal with extra VMCS when the STM is done with it
vmptrld (phys_addr);
rdmsr_safe (MSR_IA32_VMX_EXIT_CTLS, msr_content);
vmwrite (VM_EXIT_CONTROLS, msr_content);
return;
}
/*
* Add or Delete a VMCS entry to/from the VMCS Database in STM.
* @param add_remove determines whether to add an entry or remove the previously
* stored entry.
* While adding an entry specify the Domain protection policy in the appropriate
* fields.
*/
int
manage_vmcs_database (uint64_t vmcs_ptr, uint32_t add_remove)
{
STM_VMCS_DATABASE_REQUEST *vmcsdb_request = NULL;
void *request_list;
uint32_t eax_reg = 0;
uint32_t ebx_reg = 0;
uint32_t ecx_reg = 0;
int cpu = smp_processor_id ();
pr_info ("%d STM-LINUX: Invoking Operation on VMCS Database\n", cpu);
if (atomic_read (&is_stm) != 0x01) //is_stm != 0x01
{
pr_err ("%d STM-LINUX: STM not enabled\n", cpu);
return -1;
}
if ((request_list = alloc_pages (GFP_KERNEL, 0)) == NULL)
{
pr_err ("%d STM-LINUX: Failed to allocate resource page.\n", cpu);
return -1;
}
vmcsdb_request = (STM_VMCS_DATABASE_REQUEST *) request_list;
vmcsdb_request->VmcsPhysPointer = vmcs_ptr;
vmcsdb_request->DomainType = DOMAIN_UNPROTECTED;
vmcsdb_request->XStatePolicy = XSTATE_READONLY;
vmcsdb_request->DegradationPolicy = DOMAIN_UNPROTECTED;
vmcsdb_request->AddOrRemove = add_remove;
vmcsdb_request->Reserved1 = 0x0;
ebx_reg = (uint64_t) __pa ((unsigned long) request_list);
ecx_reg = ((uint64_t) __pa ((unsigned long) request_list)) >> 32;
asm volatile (".byte 0x0f,0x01,0xc1\n":"=a" (eax_reg):"a"
(STM_API_MANAGE_VMCS_DATABASE), "b" (ebx_reg), "c" (ecx_reg));
if (eax_reg != STM_SUCCESS)
{
pr_err
("%d STM-LINUX: STM_API_MANAGE_VMCS_DATABASE failed with error: 0x%lx\n",
cpu, (unsigned long) eax_reg);
clear_page (request_list);
free_pages ((unsigned long) request_list, 0);
return -1;
}
clear_page (request_list);
free_pages ((unsigned long) request_list, 0);
return 0;
}
/*
* protect_resources creates resource protection policy profile and invokes the
* PROTECT_RESOURCE VMCALL to apply these policy profiles over SMI handler.
* While creating the policy profile the adopter should check for STM
* capabilities reported after successful return from INITIALIZE_PROTECTION
* VMCALL. The capabilities value will indicate whether the underlying STM
* supports bit granular or whole MSR resource protection methodology. (Byte
* granular or entire page level protection for MMIO and Memory regions.)
* Intel's STM implementation currently only supports whole MSR or page level
* resource protection.
* Our sample policy profile implementation below is in synchronous with this
* idea.
*/
int
protect_resources (void)
{
struct page *resource_list;
uint8_t *linuxresources;
uint32_t eax_reg = STM_API_PROTECT_RESOURCE;
uint32_t ebx_reg = 0;
uint32_t ecx_reg = 0;
int page_index = 0;
int cpu = smp_processor_id ();
STM_RSC_MSR_DESC MsrDesc = { };
STM_RSC_IO_DESC IoDesc = { };
STM_RSC_END EndDesc = { };
pr_info ("%d STM-LINUX: Protect Linux Resources\n", cpu);
if ((resource_list = alloc_pages (GFP_KERNEL, 0)) == NULL)
{
pr_err ("%d STM-LINUX: Failed to allocate resource page.\n", cpu);
return -1;
}
linuxresources = page_address (resource_list);
memset (linuxresources, 0, 4096);
MsrDesc.Hdr.RscType = MACHINE_SPECIFIC_REG;
MsrDesc.Hdr.Length = sizeof (STM_RSC_MSR_DESC);
MsrDesc.Hdr.ReturnStatus = 0;
MsrDesc.Hdr.Reserved = 0;
MsrDesc.Hdr.IgnoreResource = 0;
MsrDesc.MsrIndex = 0x9b;
MsrDesc.KernelModeProcessing = 1;
MsrDesc.Reserved = 0;
MsrDesc.WriteMask = (uint64_t) - 1;
MsrDesc.ReadMask = 0;
memcpy (linuxresources, &MsrDesc, sizeof (MsrDesc));
linuxresources += MsrDesc.Hdr.Length;
MsrDesc.Hdr.RscType = MACHINE_SPECIFIC_REG;
MsrDesc.Hdr.Length = sizeof (STM_RSC_MSR_DESC);
MsrDesc.Hdr.ReturnStatus = 0;
MsrDesc.Hdr.Reserved = 0;
MsrDesc.Hdr.IgnoreResource = 0;
MsrDesc.MsrIndex = MSR_IA32_MISC_ENABLE; /* 0x1A0 */
MsrDesc.KernelModeProcessing = 0;
MsrDesc.Reserved = 0;
MsrDesc.WriteMask = (uint64_t) - 1;
MsrDesc.ReadMask = 0;
memcpy (linuxresources, &MsrDesc, sizeof (MsrDesc));
linuxresources += MsrDesc.Hdr.Length;
MsrDesc.Hdr.RscType = MACHINE_SPECIFIC_REG;
MsrDesc.Hdr.Length = sizeof (STM_RSC_MSR_DESC);
MsrDesc.Hdr.ReturnStatus = 0;
MsrDesc.Hdr.Reserved = 0;
MsrDesc.Hdr.IgnoreResource = 0;
MsrDesc.MsrIndex = MSR_IA32_SYSENTER_EIP; /* 0x176 */
MsrDesc.KernelModeProcessing = 0;
MsrDesc.Reserved = 0;
MsrDesc.WriteMask = (uint64_t) - 1;
MsrDesc.ReadMask = (uint64_t) - 1;
memcpy (linuxresources, &MsrDesc, sizeof (MsrDesc));
linuxresources += MsrDesc.Hdr.Length;
MsrDesc.Hdr.RscType = MACHINE_SPECIFIC_REG;
MsrDesc.Hdr.Length = sizeof (STM_RSC_MSR_DESC);
MsrDesc.Hdr.ReturnStatus = 0;
MsrDesc.Hdr.Reserved = 0;
MsrDesc.Hdr.IgnoreResource = 0;
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
MsrDesc.MsrIndex = MSR_IA32_FEATURE_CONTROL;
#else
MsrDesc.MsrIndex = MSR_IA32_FEAT_CTL;
#endif
MsrDesc.KernelModeProcessing = 0;
MsrDesc.Reserved = 0;
MsrDesc.ReadMask = (uint64_t) - 1;
MsrDesc.WriteMask = (uint64_t) - 1;
memcpy (linuxresources, &MsrDesc, sizeof (MsrDesc));
linuxresources += MsrDesc.Hdr.Length;
IoDesc.Hdr.RscType = IO_RANGE;
IoDesc.Hdr.Length = sizeof (STM_RSC_IO_DESC);
IoDesc.Hdr.ReturnStatus = 0;
IoDesc.Hdr.Reserved = 0;
IoDesc.Hdr.IgnoreResource = 0;
IoDesc.Base = 0x60; // 0x60 to 0x64
IoDesc.Length = 5;
IoDesc.Reserved = 0;
memcpy (linuxresources, &IoDesc, sizeof (IoDesc));
linuxresources += IoDesc.Hdr.Length;
// Termination
EndDesc.Hdr.RscType = END_OF_RESOURCES;
EndDesc.Hdr.Length = sizeof (STM_RSC_END);
EndDesc.Hdr.ReturnStatus = 0;
EndDesc.Hdr.Reserved = 0;
EndDesc.Hdr.IgnoreResource = 0;
EndDesc.ResourceListContinuation = 0;
memcpy (linuxresources, &EndDesc, sizeof (EndDesc));
pr_info ("\n%d STM-LINUX: Going to request protection for: %llx",
cpu, (uint64_t) resource_list);
dump_stm_resource (page_address (resource_list));
ebx_reg = (uint64_t) virt_to_phys (page_address (resource_list)) +
page_index * PAGE_SIZE;
ecx_reg = ((uint64_t) virt_to_phys (page_address (resource_list)) +
page_index * PAGE_SIZE) >> 32;
asm volatile (".byte 0x0f,0x01,0xc1\n":"=a" (eax_reg):"a" (eax_reg),
"b" (ebx_reg), "c" (ecx_reg):"memory");
if (eax_reg != STM_SUCCESS)
{
pr_err
("%d STM-LINUX: STM_API_PROTECT_RESOURCE failed with error: 0x%lx\n",
cpu, (unsigned long) eax_reg);
pr_err
("%d STM-LINUX: STM_API_PROTECT_RESOURCE return status in Hdr: %d\n",
cpu, EndDesc.Hdr.ReturnStatus);
__free_pages (resource_list, 0);
return -1;
}
clear_page (page_address (resource_list));
__free_pages (resource_list, 0);
return 0;
}
/*
* Obtain the BIOS resource protection list
*/
int
get_bios_resource (void)
{
struct page *resource_list;
STM_RSC *resource;
uint32_t eax_reg = 0;
uint32_t ebx_reg = 0;
uint32_t ecx_reg = 0;
uint32_t edx_reg = 0;
int page_index;
int cpu = smp_processor_id ();
pr_debug ("%d STM-LINUX: Obtaining BIOS resource list.\n", cpu);
if ((resource_list =
alloc_pages (GFP_KERNEL, get_order (MAX_RESOURCE_SIZE))) == NULL)
{
pr_err ("%d STM-LINUX: Failed to allocate resource page.\n", cpu);
return -1;
}
for (page_index = 0; page_index < MAX_RESOURCE_PAGES; page_index++)
{
eax_reg = STM_API_GET_BIOS_RESOURCES;
ebx_reg =
(uint64_t) virt_to_phys (page_address (resource_list)) +
page_index * 4096;
ecx_reg =
((uint64_t) virt_to_phys (page_address (resource_list)) +
page_index * 4096) >> 32;
edx_reg = page_index;
asm volatile (".byte 0x0f,0x01,0xc1\n":"=a" (eax_reg):"a"
(eax_reg), "b" (ebx_reg), "c" (ecx_reg),
"d" (edx_reg):"memory");
if (eax_reg != STM_SUCCESS)
{
pr_err ("%d STM-LINUX: STM_API_GET_BIOS_RESOURCES failed with error: \
0x%lx\n", cpu,
(unsigned long) eax_reg);
free_pages ((unsigned long) resource_list,
get_order (MAX_RESOURCE_SIZE));
return -1;
}
resource =
(STM_RSC *) ((uint64_t) page_address (resource_list) +
page_index * 4096);
dump_stm_resource (resource);
if (edx_reg == 0)
{
pr_debug ("%d STM-LINUX: Reached end of BIOS Resource list\n", cpu);
break;
}
}
__free_pages (resource_list, get_order (MAX_RESOURCE_SIZE));
return 0;
}
/*
* Opt-in to STM by invoking the INTIALIZE_PROTECTION VMCALL and STM_START
* VMCALL. Also, obtain the BIOS resource protection list from STM and define
* resource protection policies over MLE resources.
*/
static void
launch_stm (void *unused)
{
u64 msr_content = 0;
uint32_t eax_reg = 0;
uint32_t ebx_reg = 0;
uint32_t ecx_reg = 0;
uint32_t edx_reg = 0;
unsigned long eflags;
unsigned int cpu;
int ret;
cpu = smp_processor_id ();
if (cpu != 0)
while (atomic_read (&bspready) == 0)
{
};
pr_info ("%d STM-LINUX - starting launch_stm\n", cpu);
/* Consult MSR IA32_VMX_BASIC to find out if STM is supported.
* If STM is supported then bit 49 of this MSR will be set and
* MSR IA32_SMM_MONITOR_CTL exists on such a processor.
* Trying to access MSR IA32_SMM_MONITOR_CTL on a processor that does not
* support STM will result in a #GP Fault.
*/
rdmsr_safe (MSR_IA32_VMX_BASIC, msr_content);
if ((msr_content & VMX_BASIC_DUAL_MONITOR) == 0)
{
pr_err ("%d STM-LINUX: STM is not supported on the processor\n", cpu);
smp_mb__before_atomic ();
atomic_set (&is_stm, 0x03);
smp_mb__after_atomic ();
goto done;
}
vmcs_revision_id = msr_content & MSR_VMX_BASIC_VMCS_REVISION_MASK;
/* check to see if vmx is enabled */
if (!cpu_vmx_enabled ())
{
pr_err ("%d STM-LINUX: VMX is not enabled\n", cpu);
smp_mb__before_atomic ();
atomic_set (&is_stm, 0x03);
smp_mb__after_atomic ();
goto done;
}
msr_content = 0;
/* Proceed only if BIOS has opt-in to STM. */
rdmsr_safe (MSR_IA32_SMM_MONITOR_CTL, msr_content);
if ((msr_content & IA32_SMM_MONITOR_CTL_VALID) == 0)
{
pr_err ("%d STM-LINUX: No STM opt-in from BIOS\n", cpu);
smp_mb__before_atomic ();
atomic_set (&is_stm, 0x03);
smp_mb__after_atomic ();
goto done;
}
/* Allocate a temporary VMCS per CPU */
pr_info ("%d STM-LINUX: Opt-in to STM commences\n", cpu);
per_cpu (temp_vmcs, cpu) = vmx_temp_vmcs ();
if (!per_cpu (temp_vmcs, cpu))
{
pr_err ("%d STM-LINUX: Failed to create VMCS\n", cpu);
smp_mb__before_atomic ();
atomic_set (&is_stm, 0x02);
smp_mb__after_atomic ();
goto done;
}
set_temp_vmcs ();
if (cpu == 0)
{
pr_debug ("%d STM-LINUX: Initializing STM Resources\n", cpu);
asm volatile (".byte 0x0f,0x01,0xc1\n"
" pushf; "
" pop %2; ":"=a" (eax_reg),
"=b" (ebx_reg), "=c" (eflags),
"=d" (edx_reg):"a"
(STM_API_INITIALIZE_PROTECTION),
"b" (ebx_reg), "c" (ecx_reg), "d" (edx_reg):"cc");
if (eflags & X86_EFLAGS_CF)
pr_err ("%d STM-LINUX: vmfailinvalid error\n", cpu);
if (eflags & X86_EFLAGS_ZF)
pr_err ("%d STM-LINUX - vmfailvalid error\n", cpu);
if (eax_reg != STM_SUCCESS)
{
pr_err ("%d STM-LINUX: STM_API_INITIALIZE_PROTECTION failed with error: \
0x%lx\n", cpu,
(unsigned long) eax_reg);
smp_mb__after_atomic ();
atomic_set (&is_stm, 0x02);
smp_mb__after_atomic ();
goto done;
}
pr_info
("%d STM-LINUX: STM_API_INITIALIZE_PROTECTION succeeded\n", cpu);
/* Get Bios Resources */
ret = get_bios_resource ();
if (ret != 0)
{
pr_debug ("%d STM-LINUX: Exiting STM opt-in\n", cpu);
smp_mb__before_atomic ();
atomic_set (&is_stm, 0x02);
smp_mb__after_atomic ();
goto done;
}
/* Protect Linux Resources */
ret = protect_resources ();
if (ret != 0)
{
pr_debug ("%d STM-LINUX: Exiting STM opt-in\n", cpu);
smp_mb__before_atomic ();
atomic_set (&is_stm, 0x02);
smp_mb__after_atomic ();
goto done;
}
// have to start the others first
smp_mb__before_atomic ();
atomic_set (&bspready, 1);
smp_mb__after_atomic ();
/* Start STM */
asm volatile (".byte 0x0f,0x01,0xc1\n":"=a" (eax_reg):"a"
(STM_API_START));
if (eax_reg == STM_SUCCESS)
pr_debug ("%d STM-LINUX: STM_API_START (BSP) succeeded\n", cpu);
else
{
pr_err
("%d STM-LINUX: STM_API_START (BSP) failed with error: 0x%lx\n",
cpu, (unsigned long) eax_reg);
smp_mb__before_atomic ();
atomic_set (&is_stm, 0x02);
smp_mb__after_atomic ();
goto done;
}
}
else
{
// while(atomic_read(&bspready) == 0) {};
asm volatile (".byte 0x0f,0x01,0xc1\n":"=a" (eax_reg):"a"
(STM_API_START));
if (eax_reg == STM_SUCCESS)
pr_debug ("%d STM-LINUX: STM_API_START (AP) succeeded\n", cpu);
else
{
pr_err
("%d STM-LINUX: STM_API_START (AP) failed with error: 0x%lx\n",
cpu, (unsigned long) eax_reg);
goto done;
}
}
done:
if (cpu == 0)
{
pr_debug ("%d STM-LINUX - waiting for APs to finish\n", cpu);
while ((atomic_read (&number_ap) < ((int) num_online_cpus () - 1)))
{
// yield();
//spin_unlock(&cntr_lock);
//spin_lock(&cntr_lock);
}
pr_debug ("%d STM-LINUX - APs finished, BSP getting out\n", cpu);
smp_mb__before_atomic ();
atomic_set (&stmbsp_done, 1);
smp_mb__after_atomic ();
}
else
{
smp_mb__before_atomic ();
atomic_inc (&number_ap);
smp_mb__after_atomic ();
pr_debug ("%d STM-LINUX - (done-AP) waiting for BSP to finish\n", cpu);
while (atomic_read (&stmbsp_done) != 1)
{
} // hold till all done
}
//local_irq_enable();
return;
}
/*
* Shutdown STM
*/
void
teardown_stm (void *unused)
{
uint32_t eax_reg = 0;
uint32_t cpu;
cpu = smp_processor_id ();
pr_info ("%d STM-LINUX: teardown_stm started\n", cpu);
/* Teardown STM only if it has been previously enabled */
if (atomic_read (&is_stm) != 0x01)
{
pr_err
("%d STM-LINUX: teardown_stm - STM not enabled (%d)\n",
cpu, atomic_read (&is_stm));
goto done;
}
if (cpu == 0)
{
asm volatile (".byte 0x0f,0x01,0xc1\n":"=a" (eax_reg):"a"
(STM_API_STOP));
if (eax_reg == STM_SUCCESS)
pr_info ("%d STM-LINUX: teardown_stm STM_API_STOP succeeded\n", cpu);
else
{
pr_err
("%d STM-LINUX: teardown_stm STM_API_STOP failed with error: 0x%lx\n",
cpu, (unsigned long) eax_reg);
goto done;
}
}
else
{
/* Teardown STM */
asm volatile (".byte 0x0f,0x01,0xc1\n":"=a" (eax_reg):"a"
(STM_API_STOP));
if (eax_reg == STM_SUCCESS)
pr_info ("%d STM-LINUX: STM_API_STOP succeeded\n", cpu);
else
{
pr_err
("%d STM-LINUX: STM_API_STOP failed with error: 0x%lx\n",
cpu, (unsigned long) eax_reg);
goto done;
}
}
done:
if (cpu == 0)
{
pr_debug ("%d STM-LINUX - (teardown) waiting for APs to finish\n", cpu);
while (atomic_read (&number_ap) < ((int) num_online_cpus () - 1))
{
}
pr_debug
("%d STM-LINUX - (teardown) APs finished, BSP getting out\n", cpu);
smp_mb__before_atomic ();
atomic_set (&stmbsp_done, 1);
smp_mb__after_atomic ();
}
else
{
smp_mb__before_atomic ();
atomic_inc (&number_ap);
smp_mb__after_atomic ();
while (atomic_read (&stmbsp_done) != 1)
{
} // hold till all done
}
//local_irq_enable();
return;
}
/*
* This function dumps STM resource node header.
*/
void
dump_stm_resource_header (STM_RSC * Resource)
{
uint32_t cpu;
cpu = smp_processor_id ();
pr_info ("%d STM-LINUX: RscType : %08x\n", cpu,
Resource->Header.RscType);
pr_info ("%d STM-LINUX: RscLength : %04x\n", cpu,
Resource->Header.Length);
pr_info ("%d STM-LINUX: ReturnStatus : %04x\n", cpu,
Resource->Header.ReturnStatus);
pr_info ("%d STM-LINUX: IgnoreResource: %04x\n", cpu,
Resource->Header.IgnoreResource);
}
/*
* This function dumps STM resource node.
*/
void
dump_stm_resource_node (STM_RSC * Resource)
{
uint8_t pci_index;
uint32_t cpu;
cpu = smp_processor_id ();
switch (Resource->Header.RscType)
{
case END_OF_RESOURCES:
pr_info ("%d STM-LINUX: END_OF_RESOURCES:\n", cpu);
dump_stm_resource_header (Resource);
pr_info
("%d STM-LINUX: ResourceListContinuation : %016llx\n",
cpu, Resource->End.ResourceListContinuation);
break;
case MEM_RANGE:
pr_info ("%d STM-LINUX: MEM_RANGE:\n", cpu);
dump_stm_resource_header (Resource);
pr_info ("%d STM-LINUX: Base : %016llx\n", cpu,
Resource->Mem.Base);
pr_info ("%d STM-LINUX: Length : %016llx\n", cpu,
Resource->Mem.Length);
pr_info ("%d STM-LINUX: RWXAttributes : %08x\n", cpu,
(uint8_t) Resource->Mem.RWXAttributes);
break;
case IO_RANGE:
pr_info ("%d STM-LINUX: IO_RANGE:\n", cpu);
dump_stm_resource_header (Resource);
pr_info ("%d STM-LINUX: Base : %04x\n", cpu,
(int) Resource->Io.Base);
pr_info ("%d STM-LINUX: Length : %04x\n", cpu,
(int) Resource->Io.Length);
break;
case MMIO_RANGE:
pr_info ("%d STM-LINUX: MMIO_RANGE:\n", cpu);
dump_stm_resource_header (Resource);
pr_info ("%d STM-LINUX: Base : %016llx\n", cpu,
Resource->Mmio.Base);
pr_info ("%d STM-LINUX: Length : %016llx\n", cpu,
Resource->Mmio.Length);
pr_info ("%d STM-LINUX: RWXAttributes : %08x\n", cpu,
(uint8_t) Resource->Mmio.RWXAttributes);
break;
case MACHINE_SPECIFIC_REG:
pr_info ("%d STM-LINUX: MSR_RANGE:\n", cpu);
dump_stm_resource_header (Resource);
pr_info ("%d STM-LINUX: MsrIndex : %08x\n",
cpu, (uint8_t) Resource->Msr.MsrIndex);
pr_info ("%d STM-LINUX: KernelModeProc: %08x\n",
cpu, (uint8_t) Resource->Msr.KernelModeProcessing);
pr_info ("%d STM-LINUX: ReadMask : %016llx\n", cpu,
Resource->Msr.ReadMask);
pr_info ("%d STM-LINUX: WriteMask : %016llx\n", cpu,
Resource->Msr.WriteMask);
break;
case PCI_CFG_RANGE:
pr_info ("%d STM-LINUX: PCI_CFG_RANGE:\n", cpu);
dump_stm_resource_header (Resource);
pr_info ("%d STM-LINUX: RWAttributes : %04x\n",
cpu, (int) Resource->PciCfg.RWAttributes);
pr_info ("%d STM-LINUX: Base : %04x\n",
cpu, (int) Resource->PciCfg.Base);
pr_info ("%d STM-LINUX: Length : %04x\n",
cpu, (int) Resource->PciCfg.Length);
pr_info ("%d STM-LINUX: OriginatingBus: %02x\n",
cpu, (int) Resource->PciCfg.OriginatingBusNumber);
pr_info ("%d STM-LINUX: LastNodeIndex : %02x\n",
cpu, (int) Resource->PciCfg.LastNodeIndex);
for (pci_index = 0;
pci_index < Resource->PciCfg.LastNodeIndex + 1; pci_index++)
{
pr_info ("%d STM-LINUX: Type : %02x\n", cpu,
(int) Resource->PciCfg.PciDevicePath[pci_index].Type);
pr_info ("%d STM-LINUX: Subtype : %02x\n", cpu,
(int) Resource->PciCfg.PciDevicePath[pci_index].Subtype);
pr_info ("%d STM-LINUX: Length : %04x\n", cpu,
(int) Resource->PciCfg.PciDevicePath[pci_index].Length);
pr_info ("%d STM-LINUX: PciDevice : %02x\n", cpu,
(int) Resource->PciCfg.PciDevicePath[pci_index].PciDevice);
pr_info ("%d STM-LINUX: PciFunction : %02x\n", cpu,
(int) Resource->PciCfg.PciDevicePath[pci_index].
PciFunction);
}
break;
case TRAPPED_IO_RANGE:
pr_info ("%d STM-LINUX: TRAPPED_IO_RANGE:\n", cpu);
dump_stm_resource_header (Resource);
pr_info ("%d STM-LINUX: Base : %04x\n",
cpu, (int) Resource->TrappedIo.Base);
pr_info ("%d STM-LINUX: Length : %04x\n",
cpu, (int) Resource->TrappedIo.Length);
pr_info ("%d STM-LINUX: In : %04x\n",
cpu, (int) Resource->TrappedIo.In);
pr_info ("%d STM-LINUX: Out : %04x\n",
cpu, (int) Resource->TrappedIo.Out);
pr_info ("%d STM-LINUX: Api : %04x\n",
cpu, (int) Resource->TrappedIo.Api);
break;
case ALL_RESOURCES:
pr_info ("%d STM-LINUX: ALL_RESOURCES:\n", cpu);
dump_stm_resource_header (Resource);
break;
case REGISTER_VIOLATION:
pr_info ("%d STM-LINUX: REGISTER_VIOLATION:\n", cpu);
dump_stm_resource_header (Resource);
pr_info ("%d STM-LINUX: RegisterType : %08x\n",
cpu, (uint8_t) Resource->RegisterViolation.RegisterType);
pr_info ("%d STM-LINUX: ReadMask : %016llx\n", cpu,
Resource->RegisterViolation.ReadMask);
pr_info ("%d STM-LINUX: WriteMask : %016llx\n", cpu,
Resource->RegisterViolation.WriteMask);
break;
default:
dump_stm_resource_header (Resource);
break;
}
}
/*
* This function dumps STM resource list.
*/
void
dump_stm_resource (STM_RSC * Resource)
{
while (Resource->Header.RscType != END_OF_RESOURCES)
{
dump_stm_resource_node (Resource);
Resource = (STM_RSC *) ((uint64_t) Resource + Resource->Header.Length);
}
/* Dump End Node */
dump_stm_resource_node (Resource);
if (Resource->End.ResourceListContinuation != 0)
dump_stm_resource ((STM_RSC *) (uint64_t) Resource->End.
ResourceListContinuation);
}
static int
stm_shutdown (struct notifier_block *notifier, unsigned long val, void *v)
{
int cpu = smp_processor_id ();
pr_debug ("%d STM-LINUX: stm_shutdown entered\n", cpu);
if (atomic_read (&is_stm) != 0x01)
return 0;
smp_mb__before_atomic ();
atomic_set (&stmbsp_done, 0);
atomic_set (&number_ap, 0);
smp_mb__after_atomic ();
pr_info ("%d STM-LINUX Shutting down STM (%d)\n", cpu,
atomic_read (&is_stm));
smp_call_function (teardown_stm, NULL, 0);
teardown_stm (NULL);
while (atomic_read (&stmbsp_done) == 0)
{
};
smp_mb__before_atomic ();
atomic_set (&is_stm, STM_SHUTDOWN); // STM is shutdoqn
smp_mb__after_atomic ();
pr_info ("%d STM-LINUX STM shutdown complete\n", cpu);
return 0;
}
static int stm_start (void);
static int
stm_reboot (struct notifier_block *notifier, unsigned long val, void *v)
{
stm_start ();
local_irq_enable ();
return NOTIFY_OK;
}
static int
stm_pm_notifier (struct notifier_block *nb, unsigned long mode, void *_unused)
{
switch (mode)
{
case PM_HIBERNATION_PREPARE:
pr_debug ("STM-LINUX: stm_pm_notifier: PM_HIBERNATION_PREPARE\n");
stm_shutdown (nb, mode, _unused);
smp_mb__before_atomic ();
atomic_set (&is_stm, STM_SHUTDOWN_PM);
smp_mb__after_atomic ();
break;
case PM_POST_HIBERNATION:
pr_debug ("STM-LINUX: stm_pm_notifier: PM_POST_HIBERNATION\n");
stm_reboot (nb, mode, _unused);
break;
case PM_SUSPEND_PREPARE:
pr_debug ("STM-LINUX: stm_pm_notifier: PM_SUSPEND_PREPARE\n");
stm_shutdown (nb, mode, _unused);
smp_mb__before_atomic ();
atomic_set (&is_stm, STM_SHUTDOWN_PM);
smp_mb__after_atomic ();
break;
case PM_POST_SUSPEND:
pr_debug ("STM-LINUX: stm_pm_notifier: PM_POST_SUSPEND\n");
stm_reboot (nb, mode, _unused);
break;
case PM_RESTORE_PREPARE:
pr_debug
("STM-LINUX: stm_pm_notifier: PM_RESTORE_PREPARE - no action\n");
break;
case PM_POST_RESTORE:
pr_debug ("STM-LINUX: stm_pm_notifier: PM_POST_RESTORE - no action\n");
break;
default:
pr_debug ("STM-LINUX: stm_pm_notifier mode; %ld\n", mode);
break;
}
return 0;
}
static struct notifier_block stm_reboot_notifier = {
.notifier_call = stm_shutdown,
.priority = 1, // priority must be higher than kvm_reboot
};
static struct notifier_block stm_notifier_block = {
.notifier_call = stm_pm_notifier,
.priority = 1,
};
static int
stm_start (void)
{
int cpu = smp_processor_id ();
int result = 0;