对称多处理(SMP):多处理器系统的启动、MP 表与 ACPI 信息查找及 AP 启动序列
文章目录
对称多处理 (或 SMP)是在一个计算机系统中拥有多个处理器的一种方法。在 SMP 系统中(与 NUMA 系统相对),所有逻辑核心都能看到系统的全部内存。不过请注意,SMP 和 NUMA 并非互斥;正如 Brendan 在论坛上指出的,Intel 的 Core i7 同时实现了 SMP 和 NUMA,以及超线程。
旧式 SMP 系统的初始化
不同 CPU 的启动序列各不相同。Intel 的系统程序员手册(第 7.5.4 节)包含 Intel Xeon 处理器的初始化协议,但不涵盖较旧的 CPU。关于通用的“所有 CPU 类型”算法,请参阅 Intel 的 Multi-processor Specification。
对于 80486(带有外部 8249DX 本地 APIC),你必须使用一个 INIT IPI,随后发送一个“INIT level de-assert”IPI,且不发送任何 SIPI。这意味着你无法告诉它们从哪里开始执行(SIPI 的向量部分),它们总是从执行 BIOS 代码开始。在这种情况下,你要将 BIOS 的 CMOS 复位值设置为“warm start with far jump”(即把 CMOS 位置 0x0F 设置为值 10),这样 BIOS 就会执行一个 jmp far ~[0:0x0469]",然后把你的 AP 入口点的段和偏移放在 0x0469。
较新的 CPU(Pentium 4 和 Intel Xeon)不支持“INIT level de-assert”IPI,而且据我所知,在这些 CPU 上它会被完全忽略。
对于较新的 CPU(P6、Pentium 4),一个 SIPI 就足够了,但我不确定较旧的 Intel CPU(Pentium)或其他厂商的 CPU 是否需要第二个 SIPI。也有可能第二个 SIPI 的存在是为了防止第一个 SIPI 投递失败(总线噪声等)。
我通常先发送第一个 SIPI,然后等待看 AP CPU 是否增加“已启动 CPU 数量”计数器。如果它在几毫秒内没有增加这个计数器,我就发送第二个 SIPI。这与 Intel 的通用算法不同(该算法在两个 SIPI 之间有 200 微秒的延迟),但在早期启动阶段试图找到能够准确测量 200 微秒延迟的时钟源并不容易。我还发现,在真实硬件上,如果 SIPI 之间的延迟太长(而你没有使用我的方法),一个 AP CPU 可能会运行两次操作系统的早期 AP 启动代码(在我的情况下,这会导致操作系统认为 AP CPU 的数量是实际的两倍)。
你可以在总线上广播这些信号来启动所有存在的设备。但这样做的话,你也可能会启用那些被故意禁用的处理器(因为它们有缺陷)。
使用 MP 表查找信息
你可能想使用更新的 ACPI 而不是 MP 表。如果是这样,请参阅下一节。
有一些专门用于多处理的信息(在较新的机器上可能不存在)可用。首先必须找到 MP Floating Pointer Structure。它按 16 字节边界对齐,开头包含一个签名 "_MP_" 或 0x5F504D5F。操作系统必须在 EBDA、BIOS ROM 空间以及“基本内存”的最后千字节中搜索;基本内存的大小由 0x413 处的一个 2 字节值指定,单位为千字节,减去 1K。 该结构如下所示:
struct mp_floating_pointer_structure { char signature[4]; uint32_t configuration_table; uint8_t length; // In 16 bytes (e.g. 1 = 16 bytes, 2 = 32 bytes) uint8_t mp_specification_revision; uint8_t checksum; // This value should make all bytes in the table equal 0 when added together uint8_t default_configuration; // If this is not zero then configuration_table should be // ignored and a default configuration should be loaded instead uint32_t features; // If bit 7 is then the IMCR is present and PIC mode is being used, otherwise // virtual wire mode is; all other bits are reserved }
由浮动指针结构指向的配置表如下所示:
struct mp_configuration_table { char signature[4]; // "PCMP" uint16_t length; uint8_t mp_specification_revision; uint8_t checksum; // Again, the byte should be all bytes in the table add up to 0 char oem_id[8]; char product_id[12]; uint32_t oem_table; uint16_t oem_table_size; uint16_t entry_count; // This value represents how many entries are following this table uint32_t lapic_address; // This is the memory mapped address of the local APICs uint16_t extended_table_length; uint8_t extended_table_checksum; uint8_t reserved; }
在配置表之后,有 entry_count 个条目描述关于系统的更多信息,再之后是一个扩展表。条目要么是 20 字节表示一个处理器,要么是 8 字节表示其他内容。处理器和 IO APIC 条目如下所示。
struct entry_processor { uint8_t type; // Always 0 uint8_t local_apic_id; uint8_t local_apic_version; uint8_t flags; // If bit 0 is clear then the processor must be ignored // If bit 1 is set then the processor is the bootstrap processor uint32_t signature; uint32_t feature_flags; uint64_t reserved; }
这是一个 IO APIC 条目。
struct entry_io_apic { uint8_t type; // Always 2 uint8_t id; uint8_t version; uint8_t flags; // If bit 0 is set then the entry should be ignored uint32_t address; // The memory mapped address of the IO APIC is memory }
更多信息请参阅 http://www.intel.com/design/pentium/datashts/24201606.pdf,第 4 章。
使用 ACPI 查找信息
你应该能在 MADT 表中找到一个 RSDT 表,或者在 XSDT 表中。该表有一个本地 APIC 列表,其数量应与处理器上的核心数相同。这些表的详细信息未在此列出,但你可以很容易地在本 wiki 上找到它们。
AP 启动
收集完信息后,你需要禁用 PIC 并为 I/O APIC 做准备。你还需要设置 BSP 的本地 APIC。 然后,使用 SIPI 启动 AP。
启动序列
MP 规范包含一种启动 AP 的标准方法,但不建议使用,因为它包含非常精确的时序,如果操作不正确,可能会导致若干问题。Brendan 提供了一种替代方法,应对每个 AP 单独执行。首先发送一个 init IPI 并等待 10 毫秒。然后发送一个 SIPI,并轮询 AP 的跳板代码设置的标志,超时时间为 1 毫秒。如果达到超时,再发送另一个 SIPI,并轮询同一个标志,但这次超时时间为 1 秒。如果 AP 成功设置了标志,BSP 应设置另一个标志以允许 AP 继续(可能是等待调度器有一个需要执行的进程)。
然而那种替代方法过于复杂。反过来做要简单得多,只需发送两个 SIPI 并让 AP 等待 BSP,参见下面的示例代码。
时序
时序最简单的方法是使用 PIT 的模式 0。向 IO 端口 0x43 写入 0x30(为计数器 0 选择模式 0),然后将你的计数值写入 0x40,先写最低有效字节(例如,为了一毫秒,先写 0xA9 再写 0x4)。要检查计数器是否完成,向 IO 端口 0x43 写入 0xE2,然后从端口 0x40 读取一个状态字节。如果第 7 位被置位,则表示已完成。
发送 IPI
IPI 通过 BSP 的 LAPIC 发送。从 MP 表或 ACPI 表中找到 LAPIC 基地址,然后你可以向 base + 0x300 和 base + 0x310 写入 32 位字来发送 IPI。对于 init IPI 或 startup IPI,你必须首先将目标 LAPIC ID 写入 base + 0x310 的第 24-27 位。然后,对于 init IPI,向 base + 0x300 写入 0x00004500。对于 SIPI,向 base + 0x300 写入 0x00004600 与你希望 AP 开始执行的页号进行按位或的结果。更多信息请参阅 http://wiki.osdev.org/APIC#Local_APIC_registers。
示例代码
当你开始在真实机器上测试你的 SMP 代码时,你会发现它们并不遵守标准。你绝不能发送广播 INIT IPI 或广播 SIPI。以下示例包含相当数量的错误检查,可用于真实硬件。它需要三个变量
- numcores 有效核心的数量
- lapic_ids 一个本地 APIC ID 数组,numcores 个元素
- lapic_ptr 指向本地 APIC 寄存器的指针
你可以从上面的 PCMP 表获取这些,或者参阅 MADT 页面上的示例代码。
BSP 初始化代码
volatile uint8_t aprunning = 0; // count how many APs have started uint8_t bspid, bspdone = 0; // BSP id and spinlock flag // get the BSP's Local APIC ID __asm__ __volatile__ ("mov $1, %%eax; cpuid; shrl $24, %%ebx;": "=b"(bspid) : : ); // copy the AP trampoline code to a fixed address in low conventional memory (to address 0x0800:0x0000) memcpy((void*)0x8000, &ap_trampoline, 4096); // for each Local APIC ID we do... for(i = 0; i < numcores; i++) { // do not start BSP, that's already running this code if(lapic_ids[i] == bspid) continue; // send INIT IPI *((volatile uint32_t*)(lapic_ptr + 0x310)) = (*((volatile uint32_t*)(lapic_ptr + 0x310))) | (i << 24); // select AP *((volatile uint32_t*)(lapic_ptr + 0x300)) = (*((volatile uint32_t*)(lapic_ptr + 0x300)) & 0xfff00000) | 0x00C500; // trigger INIT IPI do { __asm__ __volatile__ ("pause" : : : "memory"); }while(*((volatile uint32_t*)(lapic_ptr + 0x300)) & (1 << 12)); // wait for delivery *((volatile uint32_t*)(lapic_ptr + 0x310)) = (*((volatile uint32_t*)(lapic_ptr + 0x310))) | (i << 24); // select AP *((volatile uint32_t*)(lapic_ptr + 0x300)) = (*((volatile uint32_t*)(lapic_ptr + 0x300)) & 0xfff00000) | 0x008500; // deassert do { __asm__ __volatile__ ("pause" : : : "memory"); }while(*((volatile uint32_t*)(lapic_ptr + 0x300)) & (1 << 12)); // wait for delivery mdelay(10); // wait 10 msec // send STARTUP IPI (twice) for(j = 0; j < 2; j++) { *((volatile uint32_t*)(lapic_ptr + 0x280)) = 0; // clear APIC errors *((volatile uint32_t*)(lapic_ptr + 0x310)) = (*((volatile uint32_t*)(lapic_ptr + 0x310))) | (i << 24); // select AP *((volatile uint32_t*)(lapic_ptr + 0x300)) = (*((volatile uint32_t*)(lapic_ptr + 0x300)) & 0xfff0f800) | 0x000608; // trigger STARTUP IPI for 0800:0000 udelay(200); // wait 200 usec do { __asm__ __volatile__ ("pause" : : : "memory"); }while(*((volatile uint32_t*)(lapic_ptr + 0x300)) & (1 << 12)); // wait for delivery } } // release the AP spinlocks bspdone = 1; // now you'll have the number of running APs in 'aprunning'
AP 初始化代码
由于应用处理器以实模式启动,需要少量汇编代码来进入保护模式。根据你的内核需求修改此示例。
; this code will be relocated to 0x8000, sets up environment for calling a C function .code16 ap_trampoline: cli cld ljmp $0, $0x8040 .align 16 _L8010_GDT_table: .long 0, 0 .long 0x0000FFFF, 0x00CF9A00 ; flat code .long 0x0000FFFF, 0x008F9200 ; flat data .long 0x00000068, 0x00CF8900 ; tss _L8030_GDT_value: .word _L8030_GDT_value - _L8010_GDT_table - 1 .long 0x8010 .long 0, 0 .align 64 _L8040: xorw %ax, %ax movw %ax, %ds lgdtl 0x8030 movl %cr0, %eax orl $1, %eax movl %eax, %cr0 ljmp $8, $0x8060 .align 32 .code32 _L8060: movw $16, %ax movw %ax, %ds movw %ax, %ss ; get our Local APIC ID mov $1, %eax cpuid shrl $24, %ebx movl %ebx, %edi ; set up 32k stack, one for each core. It is important that all core must have its own stack shll $15, %ebx movl stack_top, %esp subl %ebx, %esp pushl %edi ; spinlock, wait for the BSP to finish 1: pause cmpb $0, bspdone jz 1b lock incb aprunning ; jump into C code (should never return) ljmp $8, $ap_startup
// this C code can be anywhere you want it, no relocation needed void ap_startup(int apicid) { // do what you want to do on the AP while(1); }
另见
文章
线程
外部链接
来源与翻译说明
本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。
原文:Symmetric Multiprocessing;离线修订号:30332。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论