JOS lab4 Lapic与Intel多核系统

来源:互联网 发布:怪物猎人ol运营数据 编辑:程序博客网 时间:2024/06/05 01:20

JOS Lab4 SMP system and LAPIC


JOS进入Lab4过后困惑我的一个问题是SMP(symmetric multiprocessing)究竟是怎么实现的,即操作系统是如何管理多CPU工作的?多CPU将如何影响我们设计JOS。

我下面写一下我目前的理解,如果有错误,请读者斧正。

APIC

首先要明确一点:当我们要新运行一个进程时,选用哪个CPU不归OS管,这全由Intel的提供的APIC硬件帮助你决定。

APIC是”Advanced Programmable Interrupt Controller”(高级可编程中断控制器),它位于Intel CPU内部,每一个CPU都有一个Local的APIC(叫做LAPIC),APIC是可编程(正如名字里的programmable)的,我们通过对LAPIC的寄存器进行读写,来和CPU进行交互。

LAPIC寄存器 memory-maped IO

一个问题是——既然我们是要读写LAPIC的寄存器,那他们的地址是在哪里呢?

Intel默认把他们的地址映射到了虚拟内存空间FEC00000的地方。(而在jos实验中我们把它重新map到了一个比较低的地址,因为他太高了,对于我们一kernbase为基础的映射方式来说,这段话对于理解LAPIC无关。)

于是我们就像读写内存一样,可以读写APIC的寄存器。

那么下一个问题是,究竟APIC为我们提供了哪些信息呢?

在kern/lapic.c的顶部可以看到,大概包括了目前这个CPU的ID等一些列寄存器。在后续的BSP(最先启动的CPU)启动APs(其他CPU)的过程代码中,我们都看得到这些寄存器被使用,通过读写内存上的响应地址。

好了差不多,你就可以很顺畅的自己通过阅读JOS代码,开启LAB4了。:-D


提供一个介绍APIC的链接供参考:http://wiki.osdev.org/APIC

后附寄存器定义 位于Kern/lapic.c

    // Local APIC registers, divided by 4 for use as uint32_t[] indices.    #define ID      (0x0020/4)   // ID    #define VER     (0x0030/4)   // Version    #define TPR     (0x0080/4)   // Task Priority    #define EOI     (0x00B0/4)   // EOI    #define SVR     (0x00F0/4)   // Spurious Interrupt Vector    #define ENABLE  0x00000100   // Unit Enable    #define ESR     (0x0280/4)   // Error Status    #define ICRLO   (0x0300/4)   // Interrupt Command    #define INIT     0x00000500   // INIT/RESET    #define STARTUP  0x00000600   // Startup IPI    #define DELIVS   0x00001000   // Delivery status    #define ASSERT   0x00004000   // Assert interrupt (vs deassert)    #define DEASSERT 0x00000000    #define LEVEL    0x00008000   // Level triggered    #define BCAST    0x00080000   // Send to all APICs, including self.    #define OTHERS   0x000C0000   // Send to all APICs, excluding self.    #define BUSY     0x00001000    #define FIXED    0x00000000    #define ICRHI   (0x0310/4)   // Interrupt Command [63:32]    #define TIMER   (0x0320/4)   // Local Vector Table 0 (TIMER)    #define X1       0x0000000B   // divide counts by 1    #define PERIODIC 0x00020000   // Periodic    #define PCINT   (0x0340/4)   // Performance Counter LVT    #define LINT0   (0x0350/4)   // Local Vector Table 1 (LINT0)    #define LINT1   (0x0360/4)   // Local Vector Table 2 (LINT1)    #define ERROR   (0x0370/4)   // Local Vector Table 3 (ERROR)    #define MASKED   0x00010000   // Interrupt masked    #define TICR    (0x0380/4)   // Timer Initial Count    #define TCCR    (0x0390/4)   // Timer Current Count    #define TDCR    (0x03E0/4)   // Timer Divide Configuration
0 0