mirrored from git://xenbits.xen.org/xen.git
-
Notifications
You must be signed in to change notification settings - Fork 325
/
setup.c
2175 lines (1808 loc) · 65.5 KB
/
setup.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 <xen/init.h>
#include <xen/lib.h>
#include <xen/err.h>
#include <xen/grant_table.h>
#include <xen/param.h>
#include <xen/sched.h>
#include <xen/domain.h>
#include <xen/serial.h>
#include <xen/softirq.h>
#include <xen/acpi.h>
#include <xen/efi.h>
#include <xen/console.h>
#include <xen/serial.h>
#include <xen/trace.h>
#include <xen/multiboot.h>
#include <xen/domain_page.h>
#include <xen/version.h>
#include <xen/hypercall.h>
#include <xen/keyhandler.h>
#include <xen/numa.h>
#include <xen/rcupdate.h>
#include <xen/vga.h>
#include <xen/dmi.h>
#include <xen/pfn.h>
#include <xen/nodemask.h>
#include <xen/virtual_region.h>
#include <xen/watchdog.h>
#include <public/version.h>
#ifdef CONFIG_COMPAT
#include <compat/platform.h>
#include <compat/xen.h>
#endif
#include <xen/bitops.h>
#include <asm/smp.h>
#include <asm/processor.h>
#include <asm/mpspec.h>
#include <asm/apic.h>
#include <asm/msi.h>
#include <asm/desc.h>
#include <asm/paging.h>
#include <asm/e820.h>
#include <xen/kexec.h>
#include <asm/edd.h>
#include <xsm/xsm.h>
#include <asm/tboot.h>
#include <asm/bzimage.h> /* for bzimage_headroom */
#include <asm/mach-generic/mach_apic.h> /* for generic_apic_probe */
#include <asm/setup.h>
#include <xen/cpu.h>
#include <xen/cpuidle.h>
#include <asm/nmi.h>
#include <asm/alternative.h>
#include <asm/mc146818rtc.h>
#include <asm/cpu-policy.h>
#include <asm/invpcid.h>
#include <asm/spec_ctrl.h>
#include <asm/guest.h>
#include <asm/microcode.h>
#include <asm/prot-key.h>
#include <asm/pv/domain.h>
/* opt_nosmp: If true, secondary processors are ignored. */
static bool __initdata opt_nosmp;
boolean_param("nosmp", opt_nosmp);
/* maxcpus: maximum number of CPUs to activate. */
static unsigned int __initdata max_cpus;
integer_param("maxcpus", max_cpus);
int8_t __read_mostly opt_smt = -1;
boolean_param("smt", opt_smt);
/* opt_invpcid: If false, don't use INVPCID instruction even if available. */
static bool __initdata opt_invpcid = true;
boolean_param("invpcid", opt_invpcid);
bool __read_mostly use_invpcid;
unsigned long __read_mostly cr4_pv32_mask;
/* **** Linux config option: propagated to domain0. */
/* "acpi=off": Sisables both ACPI table parsing and interpreter. */
/* "acpi=force": Override the disable blacklist. */
/* "acpi=ht": Limit ACPI just to boot-time to enable HT. */
/* "acpi=noirq": Disables ACPI interrupt routing. */
/* "acpi=verbose": Enables more verbose ACPI boot time logging. */
/* **** Linux config option: propagated to domain0. */
/* noapic: Disable IOAPIC setup. */
boolean_param("noapic", skip_ioapic_setup);
/* **** Linux config option: propagated to domain0. */
/* xen_cpuidle: xen control cstate. */
int8_t __read_mostly xen_cpuidle = -1;
boolean_param("cpuidle", xen_cpuidle);
#ifndef NDEBUG
unsigned long __initdata highmem_start;
size_param("highmem-start", highmem_start);
#endif
static int8_t __initdata opt_xen_shstk = -IS_ENABLED(CONFIG_XEN_SHSTK);
#ifdef CONFIG_XEN_IBT
static bool __initdata opt_xen_ibt = true;
#else
#define opt_xen_ibt false
#endif
static int __init cf_check parse_cet(const char *s)
{
const char *ss;
int val, rc = 0;
do {
ss = strchr(s, ',');
if ( !ss )
ss = strchr(s, '\0');
if ( (val = parse_boolean("shstk", s, ss)) >= 0 )
{
#ifdef CONFIG_XEN_SHSTK
opt_xen_shstk = val;
#else
no_config_param("XEN_SHSTK", "cet", s, ss);
#endif
}
else if ( (val = parse_boolean("ibt", s, ss)) >= 0 )
{
#ifdef CONFIG_XEN_IBT
opt_xen_ibt = val;
#else
no_config_param("XEN_IBT", "cet", s, ss);
#endif
}
else
rc = -EINVAL;
s = ss + 1;
} while ( *ss );
return rc;
}
custom_param("cet", parse_cet);
cpumask_t __read_mostly cpu_present_map;
unsigned long __read_mostly xen_phys_start;
char __section(".init.bss.stack_aligned") __aligned(STACK_SIZE)
cpu0_stack[STACK_SIZE];
/* Used by the BSP/AP paths to find the higher half stack mapping to use. */
void *stack_start = cpu0_stack + STACK_SIZE - sizeof(struct cpu_info);
/* Used by the boot asm to stash the relocated multiboot info pointer. */
unsigned int __initdata multiboot_ptr;
struct cpuinfo_x86 __read_mostly boot_cpu_data = { 0, 0, 0, 0, -1 };
unsigned long __read_mostly mmu_cr4_features = XEN_MINIMAL_CR4;
/* smep: Enable/disable Supervisor Mode Execution Protection */
#define SMEP_HVM_ONLY (-2)
static s8 __initdata opt_smep = -1;
/*
* Initial domain place holder. Needs to be global so it can be created in
* __start_xen and unpaused in init_done.
*/
static struct domain *__initdata dom0;
static int __init cf_check parse_smep_param(const char *s)
{
if ( !*s )
{
opt_smep = 1;
return 0;
}
switch ( parse_bool(s, NULL) )
{
case 0:
opt_smep = 0;
return 0;
case 1:
opt_smep = 1;
return 0;
}
if ( !strcmp(s, "hvm") )
opt_smep = SMEP_HVM_ONLY;
else
return -EINVAL;
return 0;
}
custom_param("smep", parse_smep_param);
/* smap: Enable/disable Supervisor Mode Access Prevention */
#define SMAP_HVM_ONLY (-2)
static s8 __initdata opt_smap = -1;
static int __init cf_check parse_smap_param(const char *s)
{
if ( !*s )
{
opt_smap = 1;
return 0;
}
switch ( parse_bool(s, NULL) )
{
case 0:
opt_smap = 0;
return 0;
case 1:
opt_smap = 1;
return 0;
}
if ( !strcmp(s, "hvm") )
opt_smap = SMAP_HVM_ONLY;
else
return -EINVAL;
return 0;
}
custom_param("smap", parse_smap_param);
bool __read_mostly acpi_disabled;
bool __initdata acpi_force;
static char __initdata acpi_param[10] = "";
static int __init cf_check parse_acpi_param(const char *s)
{
/* Interpret the parameter for use within Xen. */
if ( !parse_bool(s, NULL) )
{
disable_acpi();
}
else if ( !strcmp(s, "force") )
{
acpi_force = true;
acpi_ht = 1;
acpi_disabled = false;
}
else if ( !strcmp(s, "ht") )
{
if ( !acpi_force )
disable_acpi();
acpi_ht = 1;
}
else if ( !strcmp(s, "noirq") )
{
acpi_noirq_set();
}
else if ( !strcmp(s, "verbose") )
{
opt_acpi_verbose = true;
return 0;
}
else
return -EINVAL;
/* Save the parameter so it can be propagated to domain0. */
safe_strcpy(acpi_param, s);
return 0;
}
custom_param("acpi", parse_acpi_param);
static const module_t *__initdata initial_images;
static unsigned int __initdata nr_initial_images;
unsigned long __init initial_images_nrpages(nodeid_t node)
{
unsigned long node_start = node_start_pfn(node);
unsigned long node_end = node_end_pfn(node);
unsigned long nr;
unsigned int i;
for ( nr = i = 0; i < nr_initial_images; ++i )
{
unsigned long start = initial_images[i].mod_start;
unsigned long end = start + PFN_UP(initial_images[i].mod_end);
if ( end > node_start && node_end > start )
nr += min(node_end, end) - max(node_start, start);
}
return nr;
}
void __init discard_initial_images(void)
{
unsigned int i;
for ( i = 0; i < nr_initial_images; ++i )
{
uint64_t start = (uint64_t)initial_images[i].mod_start << PAGE_SHIFT;
init_domheap_pages(start,
start + PAGE_ALIGN(initial_images[i].mod_end));
}
nr_initial_images = 0;
initial_images = NULL;
}
extern unsigned char __init_begin[], __init_end[];
static void __init init_idle_domain(void)
{
scheduler_init();
set_current(idle_vcpu[0]);
this_cpu(curr_vcpu) = current;
}
void srat_detect_node(int cpu)
{
nodeid_t node;
u32 apicid = x86_cpu_to_apicid[cpu];
node = apicid < MAX_LOCAL_APIC ? apicid_to_node[apicid] : NUMA_NO_NODE;
if ( node == NUMA_NO_NODE )
node = 0;
node_set_online(node);
numa_set_node(cpu, node);
if ( opt_cpu_info && acpi_numa > 0 )
printk("CPU %d APIC %d -> Node %d\n", cpu, apicid, node);
}
/*
* Sort CPUs by <node,package,core,thread> tuple. Fortunately this hierarchy is
* reflected in the structure of modern APIC identifiers, so we sort based on
* those. This is slightly complicated by the fact that the BSP must remain
* CPU 0. Hence we do a variation on longest-prefix matching to do the best we
* can while keeping CPU 0 static.
*/
static void __init normalise_cpu_order(void)
{
unsigned int i, j, min_cpu;
uint32_t apicid, diff, min_diff;
for_each_present_cpu ( i )
{
apicid = x86_cpu_to_apicid[i];
min_diff = min_cpu = ~0u;
/*
* Find remaining CPU with longest-prefix match on APIC ID.
* Among identical longest-prefix matches, pick the smallest APIC ID.
*/
for ( j = cpumask_next(i, &cpu_present_map);
j < nr_cpu_ids;
j = cpumask_next(j, &cpu_present_map) )
{
diff = x86_cpu_to_apicid[j] ^ apicid;
while ( diff & (diff-1) )
diff &= diff-1;
if ( (diff < min_diff) ||
((diff == min_diff) &&
(x86_cpu_to_apicid[j] < x86_cpu_to_apicid[min_cpu])) )
{
min_diff = diff;
min_cpu = j;
}
}
/* If no match then there must be no CPUs remaining to consider. */
if ( min_cpu >= nr_cpu_ids )
{
BUG_ON(cpumask_next(i, &cpu_present_map) < nr_cpu_ids);
break;
}
/* Switch the best-matching CPU with the next CPU in logical order. */
j = cpumask_next(i, &cpu_present_map);
apicid = x86_cpu_to_apicid[min_cpu];
x86_cpu_to_apicid[min_cpu] = x86_cpu_to_apicid[j];
x86_cpu_to_apicid[j] = apicid;
}
}
#define BOOTSTRAP_MAP_BASE (16UL << 20)
#define BOOTSTRAP_MAP_LIMIT (1UL << L3_PAGETABLE_SHIFT)
/*
* Ensure a given physical memory range is present in the bootstrap mappings.
* Use superpage mappings to ensure that pagetable memory needn't be allocated.
*/
void *__init bootstrap_map(const module_t *mod)
{
static unsigned long __initdata map_cur = BOOTSTRAP_MAP_BASE;
uint64_t start, end, mask = (1L << L2_PAGETABLE_SHIFT) - 1;
void *ret;
if ( system_state != SYS_STATE_early_boot )
return mod ? mfn_to_virt(mod->mod_start) : NULL;
if ( !mod )
{
destroy_xen_mappings(BOOTSTRAP_MAP_BASE, BOOTSTRAP_MAP_LIMIT);
map_cur = BOOTSTRAP_MAP_BASE;
return NULL;
}
start = (uint64_t)mod->mod_start << PAGE_SHIFT;
end = start + mod->mod_end;
if ( start >= end )
return NULL;
ret = (void *)(map_cur + (unsigned long)(start & mask));
start &= ~mask;
end = (end + mask) & ~mask;
if ( end - start > BOOTSTRAP_MAP_LIMIT - map_cur )
return NULL;
map_pages_to_xen(map_cur, maddr_to_mfn(start),
PFN_DOWN(end - start), PAGE_HYPERVISOR);
map_cur += end - start;
return ret;
}
static void __init move_memory(
uint64_t dst, uint64_t src, unsigned int size)
{
unsigned int blksz = BOOTSTRAP_MAP_LIMIT - BOOTSTRAP_MAP_BASE;
unsigned int mask = (1L << L2_PAGETABLE_SHIFT) - 1;
if ( src + size > BOOTSTRAP_MAP_BASE )
blksz >>= 1;
while ( size )
{
module_t mod;
unsigned int soffs = src & mask;
unsigned int doffs = dst & mask;
unsigned int sz;
void *d, *s;
mod.mod_start = (src - soffs) >> PAGE_SHIFT;
mod.mod_end = soffs + size;
if ( mod.mod_end > blksz )
mod.mod_end = blksz;
sz = mod.mod_end - soffs;
s = bootstrap_map(&mod);
mod.mod_start = (dst - doffs) >> PAGE_SHIFT;
mod.mod_end = doffs + size;
if ( mod.mod_end > blksz )
mod.mod_end = blksz;
if ( sz > mod.mod_end - doffs )
sz = mod.mod_end - doffs;
d = bootstrap_map(&mod);
memmove(d + doffs, s + soffs, sz);
dst += sz;
src += sz;
size -= sz;
bootstrap_map(NULL);
}
}
static void __init noinline move_xen(void)
{
l4_pgentry_t *pl4e;
l3_pgentry_t *pl3e;
l2_pgentry_t *pl2e;
unsigned long tmp;
unsigned int i, j, k;
/*
* The caller has selected xen_phys_start, ensuring that the old and new
* locations do not overlap, and mapped the new location into the
* directmap.
*
* Prevent the compiler from reordering anything across this point. Such
* things will end badly.
*/
barrier();
/*
* Copy out of the current alias, into the directmap at the new location.
* This includes a snapshot of the current stack.
*/
memcpy(__va(__pa(_start)), _start, _end - _start);
/*
* We are now in a critical region. Any write modifying global state
* inside the main Xen image via the current alias will get lost when we
* switch to the new pagetables.
*
* This puts printk()'s in a weird position. Xen's record of the printk()
* will get lost (e.g. from the console ring), but messages which properly
* escape the system (e.g. through the UART) may be of some use for
* debugging purposes.
*
* Walk the soon-to-be-used pagetables in the new location, relocating all
* intermediate (non-leaf) entries to point to their new-location
* equivalents. All writes are via the directmap alias.
*/
pl4e = __va(__pa(idle_pg_table));
for ( i = 0 ; i < L4_PAGETABLE_ENTRIES; i++, pl4e++ )
{
if ( !(l4e_get_flags(*pl4e) & _PAGE_PRESENT) )
continue;
*pl4e = l4e_from_intpte(l4e_get_intpte(*pl4e) + xen_phys_start);
pl3e = __va(l4e_get_paddr(*pl4e));
for ( j = 0; j < L3_PAGETABLE_ENTRIES; j++, pl3e++ )
{
if ( !(l3e_get_flags(*pl3e) & _PAGE_PRESENT) ||
(l3e_get_flags(*pl3e) & _PAGE_PSE) )
continue;
*pl3e = l3e_from_intpte(l3e_get_intpte(*pl3e) + xen_phys_start);
pl2e = __va(l3e_get_paddr(*pl3e));
for ( k = 0; k < L2_PAGETABLE_ENTRIES; k++, pl2e++ )
{
if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) ||
(l2e_get_flags(*pl2e) & _PAGE_PSE) )
continue;
*pl2e = l2e_from_intpte(l2e_get_intpte(*pl2e) + xen_phys_start);
}
}
}
/*
* Walk the soon-to-be-used l2_xenmap[], relocating all the leaf superpage
* mappings so text/data/bss etc refer to the new location in memory.
* Non-leaf mappings, e.g. fixmap_x, were relocated above.
*/
pl2e = __va(__pa(l2_xenmap));
for ( i = 0; i < L2_PAGETABLE_ENTRIES; i++, pl2e++ )
{
if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) ||
!(l2e_get_flags(*pl2e) & _PAGE_PSE) )
continue;
*pl2e = l2e_from_intpte(l2e_get_intpte(*pl2e) + xen_phys_start);
}
asm volatile (
/*
* Resync the local stack frame. The compiler expects any spilled
* expression to retain its value. This is usually only a few words.
*/
"mov %%rsp, %%rsi\n\t" /* SRC = %rsp */
"mov %%esp, %k[tmp]\n\t"
"and %[mask], %k[tmp]\n\t"
"add %[tmp], %%rdi\n\t" /* DST = cpu0_stack + (%rsp & 0x7fff) */
"sub %%esp, %%ecx\n\t" /* NR = frame - %rsp */
"rep movsb\n\t"
/*
* Switch to the relocated pagetables, shooting down global mappings.
*/
"mov %%cr4, %[tmp]\n\t"
"andb $~%c[pge], %b[tmp]\n\t"
"mov %[tmp], %%cr4\n\t" /* CR4.PGE = 0 */
"mov %[cr3], %%cr3\n\t" /* CR3 = new pagetables */
"orb %[pge], %b[tmp]\n\t"
"mov %[tmp], %%cr4\n\t" /* CR4.PGE = 1 */
: [tmp] "=&a" (tmp), /* Could be "r", but "a" makes better asm */
[dst] "=&D" (tmp),
[frame] "=&c" (tmp),
"=&S" (tmp)
: [cr3] "r" (__pa(idle_pg_table)),
[pge] "i" (X86_CR4_PGE),
[mask] "i" (STACK_SIZE - 1),
"[dst]" (__va(__pa(cpu0_stack))),
"[frame]" (__builtin_frame_address(0))
: "memory" );
/*
* End of the critical region. Updates to globals now work as expected.
*/
printk("New Xen image base address: %#lx\n", xen_phys_start);
}
#undef BOOTSTRAP_MAP_LIMIT
static uint64_t __init consider_modules(
uint64_t s, uint64_t e, uint32_t size, const module_t *mod,
unsigned int nr_mods, unsigned int this_mod)
{
unsigned int i;
if ( s > e || e - s < size )
return 0;
for ( i = 0; i < nr_mods ; ++i )
{
uint64_t start = (uint64_t)mod[i].mod_start << PAGE_SHIFT;
uint64_t end = start + PAGE_ALIGN(mod[i].mod_end);
if ( i == this_mod )
continue;
if ( s < end && start < e )
{
end = consider_modules(end, e, size, mod + i + 1,
nr_mods - i - 1, this_mod - i - 1);
if ( end )
return end;
return consider_modules(s, start, size, mod + i + 1,
nr_mods - i - 1, this_mod - i - 1);
}
}
return e;
}
static void __init setup_max_pdx(unsigned long top_page)
{
max_pdx = pfn_to_pdx(top_page - 1) + 1;
if ( max_pdx > (DIRECTMAP_SIZE >> PAGE_SHIFT) )
max_pdx = DIRECTMAP_SIZE >> PAGE_SHIFT;
if ( max_pdx > FRAMETABLE_NR )
max_pdx = FRAMETABLE_NR;
if ( max_pdx > MPT_VIRT_SIZE / sizeof(unsigned long) )
max_pdx = MPT_VIRT_SIZE / sizeof(unsigned long);
#ifdef PAGE_LIST_NULL
if ( max_pdx >= PAGE_LIST_NULL )
max_pdx = PAGE_LIST_NULL - 1;
#endif
max_page = pdx_to_pfn(max_pdx - 1) + 1;
}
/* A temporary copy of the e820 map that we can mess with during bootstrap. */
static struct e820map __initdata boot_e820;
#ifdef CONFIG_VIDEO
# include "boot/video.h"
extern struct boot_video_info boot_vid_info;
#endif
static void __init parse_video_info(void)
{
#ifdef CONFIG_VIDEO
struct boot_video_info *bvi = &bootsym(boot_vid_info);
/* vga_console_info is filled directly on EFI platform. */
if ( efi_enabled(EFI_BOOT) )
return;
if ( (bvi->orig_video_isVGA == 1) && (bvi->orig_video_mode == 3) )
{
vga_console_info.video_type = XEN_VGATYPE_TEXT_MODE_3;
vga_console_info.u.text_mode_3.font_height = bvi->orig_video_points;
vga_console_info.u.text_mode_3.cursor_x = bvi->orig_x;
vga_console_info.u.text_mode_3.cursor_y = bvi->orig_y;
vga_console_info.u.text_mode_3.rows = bvi->orig_video_lines;
vga_console_info.u.text_mode_3.columns = bvi->orig_video_cols;
}
else if ( bvi->orig_video_isVGA == 0x23 )
{
vga_console_info.video_type = XEN_VGATYPE_VESA_LFB;
vga_console_info.u.vesa_lfb.width = bvi->lfb_width;
vga_console_info.u.vesa_lfb.height = bvi->lfb_height;
vga_console_info.u.vesa_lfb.bytes_per_line = bvi->lfb_linelength;
vga_console_info.u.vesa_lfb.bits_per_pixel = bvi->lfb_depth;
vga_console_info.u.vesa_lfb.lfb_base = bvi->lfb_base;
vga_console_info.u.vesa_lfb.lfb_size = bvi->lfb_size;
vga_console_info.u.vesa_lfb.red_pos = bvi->red_pos;
vga_console_info.u.vesa_lfb.red_size = bvi->red_size;
vga_console_info.u.vesa_lfb.green_pos = bvi->green_pos;
vga_console_info.u.vesa_lfb.green_size = bvi->green_size;
vga_console_info.u.vesa_lfb.blue_pos = bvi->blue_pos;
vga_console_info.u.vesa_lfb.blue_size = bvi->blue_size;
vga_console_info.u.vesa_lfb.rsvd_pos = bvi->rsvd_pos;
vga_console_info.u.vesa_lfb.rsvd_size = bvi->rsvd_size;
vga_console_info.u.vesa_lfb.gbl_caps = bvi->capabilities;
vga_console_info.u.vesa_lfb.mode_attrs = bvi->vesa_attrib;
}
#endif
}
static void __init kexec_reserve_area(void)
{
#ifdef CONFIG_KEXEC
unsigned long kdump_start = kexec_crash_area.start;
unsigned long kdump_size = kexec_crash_area.size;
static bool __initdata is_reserved = false;
kdump_size = (kdump_size + PAGE_SIZE - 1) & PAGE_MASK;
if ( (kdump_start == 0) || (kdump_size == 0) || is_reserved )
return;
is_reserved = true;
if ( !reserve_e820_ram(&boot_e820, kdump_start, kdump_start + kdump_size) )
{
printk("Kdump: DISABLED (failed to reserve %luMB (%lukB) at %#lx)"
"\n", kdump_size >> 20, kdump_size >> 10, kdump_start);
kexec_crash_area.start = kexec_crash_area.size = 0;
}
else
{
printk("Kdump: %luMB (%lukB) at %#lx\n",
kdump_size >> 20, kdump_size >> 10, kdump_start);
}
#endif
}
static inline bool using_2M_mapping(void)
{
return !l1_table_offset((unsigned long)__2M_text_end) &&
!l1_table_offset((unsigned long)__2M_rodata_start) &&
!l1_table_offset((unsigned long)__2M_rodata_end) &&
!l1_table_offset((unsigned long)__2M_init_start) &&
!l1_table_offset((unsigned long)__2M_init_end) &&
!l1_table_offset((unsigned long)__2M_rwdata_start) &&
!l1_table_offset((unsigned long)__2M_rwdata_end);
}
static void noreturn init_done(void)
{
void *va;
unsigned long start, end;
int err;
if ( (err = xsm_set_system_active()) != 0 )
panic("xsm: unable to switch to SYSTEM_ACTIVE privilege: %d\n", err);
system_state = SYS_STATE_active;
domain_unpause_by_systemcontroller(dom0);
/* MUST be done prior to removing .init data. */
unregister_init_virtual_region();
/* Zero the .init code and data. */
for ( va = __init_begin; va < _p(__init_end); va += PAGE_SIZE )
clear_page(va);
/* Destroy Xen's mappings, and reuse the pages. */
if ( using_2M_mapping() )
{
start = (unsigned long)&__2M_init_start,
end = (unsigned long)&__2M_init_end;
}
else
{
start = (unsigned long)&__init_begin;
end = (unsigned long)&__init_end;
}
destroy_xen_mappings(start, end);
init_xenheap_pages(__pa(start), __pa(end));
printk("Freed %lukB init memory\n", (end - start) >> 10);
/* Mark .rodata/ro_after_init as RO. Maybe reform the superpage. */
modify_xen_mappings((unsigned long)&__2M_rodata_start,
(unsigned long)&__2M_rodata_end,
PAGE_HYPERVISOR_RO);
startup_cpu_idle_loop();
}
#if defined(CONFIG_XEN_SHSTK) || defined(CONFIG_XEN_IBT)
/*
* Used by AP and S3 asm code to calcualte the appropriate MSR_S_CET setting.
* Do not use on the BSP before reinit_bsp_stack(), or it may turn SHSTK on
* too early.
*/
unsigned int xen_msr_s_cet_value(void)
{
return ((cpu_has_xen_shstk ? CET_SHSTK_EN | CET_WRSS_EN : 0) |
(cpu_has_xen_ibt ? CET_ENDBR_EN : 0));
}
#else
unsigned int xen_msr_s_cet_value(void); /* To avoid ifdefary */
#endif
/* Reinitalise all state referring to the old virtual address of the stack. */
static void __init noreturn reinit_bsp_stack(void)
{
unsigned long *stack = (void*)(get_stack_bottom() & ~(STACK_SIZE - 1));
int rc;
/* Update TSS and ISTs */
load_system_tables();
/* Update SYSCALL trampolines */
percpu_traps_init();
stack_base[0] = stack;
rc = setup_cpu_root_pgt(0);
if ( rc )
panic("Error %d setting up PV root page table\n", rc);
if ( IS_ENABLED(CONFIG_XEN_SHSTK) && cpu_has_xen_shstk )
{
wrmsrl(MSR_PL0_SSP,
(unsigned long)stack + (PRIMARY_SHSTK_SLOT + 1) * PAGE_SIZE - 8);
wrmsrl(MSR_S_CET, xen_msr_s_cet_value());
asm volatile ("setssbsy" ::: "memory");
}
reset_stack_and_jump(init_done);
}
/*
* x86 early command line parsing in xen/arch/x86/boot/cmdline.c
* has options that are only used during the very initial boot process,
* so they can be ignored now.
*/
ignore_param("real-mode");
ignore_param("edd");
ignore_param("edid");
/*
* Some scripts add "placeholder" to work around a grub error where it ate the
* first parameter.
*/
ignore_param("placeholder");
static bool __init loader_is_grub2(const char *loader_name)
{
/* GRUB1="GNU GRUB 0.xx"; GRUB2="GRUB 1.xx" */
const char *p = strstr(loader_name, "GRUB ");
return (p != NULL) && (p[5] != '0');
}
static char * __init cmdline_cook(char *p, const char *loader_name)
{
p = p ? : "";
/* Strip leading whitespace. */
while ( *p == ' ' )
p++;
/* GRUB2 and PVH don't not include image name as first item on command line. */
if ( xen_guest || loader_is_grub2(loader_name) )
return p;
/* Strip image name plus whitespace. */
while ( (*p != ' ') && (*p != '\0') )
p++;
while ( *p == ' ' )
p++;
return p;
}
static unsigned int __init copy_bios_e820(struct e820entry *map, unsigned int limit)
{
unsigned int n = min(bootsym(bios_e820nr), limit);
if ( n )
memcpy(map, bootsym(bios_e820map), sizeof(*map) * n);
return n;
}
static struct domain *__init create_dom0(const module_t *image,
unsigned long headroom,
module_t *initrd, const char *kextra,
const char *loader)
{
struct xen_domctl_createdomain dom0_cfg = {
.flags = IS_ENABLED(CONFIG_TBOOT) ? XEN_DOMCTL_CDF_s3_integrity : 0,
.max_evtchn_port = -1,
.max_grant_frames = -1,
.max_maptrack_frames = -1,
.grant_opts = XEN_DOMCTL_GRANT_version(opt_gnttab_max_version),
.max_vcpus = dom0_max_vcpus(),
.arch = {
.misc_flags = opt_dom0_msr_relaxed ? XEN_X86_MSR_RELAXED : 0,
},
};
struct domain *d;
char *cmdline;
domid_t domid;
if ( opt_dom0_pvh )
{
dom0_cfg.flags |= (XEN_DOMCTL_CDF_hvm |
((hvm_hap_supported() && !opt_dom0_shadow) ?
XEN_DOMCTL_CDF_hap : 0));
dom0_cfg.arch.emulation_flags |=
XEN_X86_EMU_LAPIC | XEN_X86_EMU_IOAPIC | XEN_X86_EMU_VPCI;
}
if ( iommu_enabled )
dom0_cfg.flags |= XEN_DOMCTL_CDF_iommu;
/* Create initial domain. Not d0 for pvshim. */
domid = get_initial_domain_id();
d = domain_create(domid, &dom0_cfg, pv_shim ? 0 : CDF_privileged);
if ( IS_ERR(d) )
panic("Error creating d%u: %ld\n", domid, PTR_ERR(d));
init_dom0_cpuid_policy(d);
if ( alloc_dom0_vcpu0(d) == NULL )
panic("Error creating d%uv0\n", domid);
/* Grab the DOM0 command line. */
cmdline = image->string ? __va(image->string) : NULL;
if ( cmdline || kextra )
{
static char __initdata dom0_cmdline[MAX_GUEST_CMDLINE];
cmdline = cmdline_cook(cmdline, loader);
safe_strcpy(dom0_cmdline, cmdline);
if ( kextra )
/* kextra always includes exactly one leading space. */
safe_strcat(dom0_cmdline, kextra);
/* Append any extra parameters. */
if ( skip_ioapic_setup && !strstr(dom0_cmdline, "noapic") )
safe_strcat(dom0_cmdline, " noapic");
if ( (strlen(acpi_param) == 0) && acpi_disabled )
{
printk("ACPI is disabled, notifying Domain 0 (acpi=off)\n");
safe_strcpy(acpi_param, "off");
}
if ( (strlen(acpi_param) != 0) && !strstr(dom0_cmdline, "acpi=") )
{
safe_strcat(dom0_cmdline, " acpi=");
safe_strcat(dom0_cmdline, acpi_param);
}
cmdline = dom0_cmdline;
}
/*
* Temporarily clear SMAP in CR4 to allow user-accesses in construct_dom0().
* This saves a large number of corner cases interactions with
* copy_from_user().
*/
if ( cpu_has_smap )
{
cr4_pv32_mask &= ~X86_CR4_SMAP;
write_cr4(read_cr4() & ~X86_CR4_SMAP);
}
if ( construct_dom0(d, image, headroom, initrd, cmdline) != 0 )
panic("Could not construct domain 0\n");
if ( cpu_has_smap )
{
write_cr4(read_cr4() | X86_CR4_SMAP);
cr4_pv32_mask |= X86_CR4_SMAP;
}
return d;
}
/* How much of the directmap is prebuilt at compile time. */
#define PREBUILT_MAP_LIMIT (1 << L2_PAGETABLE_SHIFT)
/* SAF-1-safe */
void __init noreturn __start_xen(unsigned long mbi_p)
{
const char *memmap_type = NULL;
char *cmdline, *kextra, *loader;
void *bsp_stack;
struct cpu_info *info = get_cpu_info(), *bsp_info;
unsigned int initrdidx, num_parked = 0;
multiboot_info_t *mbi;
module_t *mod;
unsigned long nr_pages, raw_max_page, modules_headroom, module_map[1];
int i, j, e820_warn = 0, bytes = 0;
unsigned long eb_start, eb_end;
bool acpi_boot_table_init_done = false, relocated = false;
int ret;
struct ns16550_defaults ns16550 = {
.data_bits = 8,
.parity = 'n',
.stop_bits = 1
};
const char *hypervisor_name;
/* Critical region without IDT or TSS. Any fault is deadly! */
init_shadow_spec_ctrl_state();
percpu_init_areas();
init_idt_traps();
load_system_tables();