Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


fix potential overflow condition in VMX assist
[palacios.git] / bios / seabios / src / smp.c
1 // CPU count detection
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2006 Fabrice Bellard
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8 #include "util.h" // dprintf
9 #include "config.h" // CONFIG_*
10 #include "cmos.h" // CMOS_BIOS_SMP_COUNT
11 #include "paravirt.h"
12
13 #define APIC_ICR_LOW ((u8*)BUILD_APIC_ADDR + 0x300)
14 #define APIC_SVR     ((u8*)BUILD_APIC_ADDR + 0x0F0)
15 #define APIC_LINT0   ((u8*)BUILD_APIC_ADDR + 0x350)
16 #define APIC_LINT1   ((u8*)BUILD_APIC_ADDR + 0x360)
17
18 #define APIC_ENABLED 0x0100
19
20 struct { u32 ecx, eax, edx; } smp_mtrr[32] VAR16VISIBLE;
21 u32 smp_mtrr_count VAR16VISIBLE;
22
23 void
24 wrmsr_smp(u32 index, u64 val)
25 {
26     wrmsr(index, val);
27     if (smp_mtrr_count >= ARRAY_SIZE(smp_mtrr)) {
28         warn_noalloc();
29         return;
30     }
31     smp_mtrr[smp_mtrr_count].ecx = index;
32     smp_mtrr[smp_mtrr_count].eax = val;
33     smp_mtrr[smp_mtrr_count].edx = val >> 32;
34     smp_mtrr_count++;
35 }
36
37 u32 CountCPUs VAR16VISIBLE;
38 u32 MaxCountCPUs VAR16VISIBLE;
39 extern void smp_ap_boot_code(void);
40 ASM16(
41     "  .global smp_ap_boot_code\n"
42     "smp_ap_boot_code:\n"
43
44     // Setup data segment
45     "  movw $" __stringify(SEG_BIOS) ", %ax\n"
46     "  movw %ax, %ds\n"
47
48     // MTRR setup
49     "  movl $smp_mtrr, %esi\n"
50     "  movl smp_mtrr_count, %ebx\n"
51     "1:testl %ebx, %ebx\n"
52     "  jz 2f\n"
53     "  movl 0(%esi), %ecx\n"
54     "  movl 4(%esi), %eax\n"
55     "  movl 8(%esi), %edx\n"
56     "  wrmsr\n"
57     "  addl $12, %esi\n"
58     "  decl %ebx\n"
59     "  jmp 1b\n"
60     "2:\n"
61
62     // Increment the cpu counter
63     "  lock incl CountCPUs\n"
64
65     // Halt the processor.
66     "1:hlt\n"
67     "  jmp 1b\n"
68     );
69
70 // find and initialize the CPUs by launching a SIPI to them
71 void
72 smp_probe(void)
73 {
74     ASSERT32FLAT();
75     u32 eax, ebx, ecx, cpuid_features;
76     cpuid(1, &eax, &ebx, &ecx, &cpuid_features);
77     if (eax < 1 || !(cpuid_features & CPUID_APIC)) {
78         // No apic - only the main cpu is present.
79         dprintf(1, "No apic - only the main cpu is present.\n");
80         CountCPUs= 1;
81         MaxCountCPUs = 1;
82         return;
83     }
84
85     // Init the counter.
86     writel(&CountCPUs, 1);
87
88     // Setup jump trampoline to counter code.
89     u64 old = *(u64*)BUILD_AP_BOOT_ADDR;
90     // ljmpw $SEG_BIOS, $(smp_ap_boot_code - BUILD_BIOS_ADDR)
91     u64 new = (0xea | ((u64)SEG_BIOS<<24)
92                | (((u32)smp_ap_boot_code - BUILD_BIOS_ADDR) << 8));
93     *(u64*)BUILD_AP_BOOT_ADDR = new;
94
95     // enable local APIC
96     u32 val = readl(APIC_SVR);
97     writel(APIC_SVR, val | APIC_ENABLED);
98
99     if (! CONFIG_COREBOOT) {
100         /* Set LINT0 as Ext_INT, level triggered */
101         writel(APIC_LINT0, 0x8700);
102
103         /* Set LINT1 as NMI, level triggered */
104         writel(APIC_LINT1, 0x8400);
105     }
106
107     // broadcast SIPI
108     barrier();
109     writel(APIC_ICR_LOW, 0x000C4500);
110     u32 sipi_vector = BUILD_AP_BOOT_ADDR >> 12;
111     writel(APIC_ICR_LOW, 0x000C4600 | sipi_vector);
112
113     // Wait for other CPUs to process the SIPI.
114     if (CONFIG_COREBOOT) {
115         msleep(10);
116     } else {
117         u8 cmos_smp_count = inb_cmos(CMOS_BIOS_SMP_COUNT);
118         while (cmos_smp_count + 1 != readl(&CountCPUs))
119             yield();
120     }
121
122     // Restore memory.
123     *(u64*)BUILD_AP_BOOT_ADDR = old;
124
125     MaxCountCPUs = qemu_cfg_get_max_cpus();
126     if (!MaxCountCPUs || MaxCountCPUs < CountCPUs)
127         MaxCountCPUs = CountCPUs;
128
129     dprintf(1, "Found %d cpu(s) max supported %d cpu(s)\n", readl(&CountCPUs),
130         MaxCountCPUs);
131 }