Skip to content
Snippets Groups Projects
mp.c 3.11 KiB
Newer Older
rsc's avatar
rsc committed
// Multiprocessor bootstrap.
// Search memory for MP description structures.
rsc's avatar
rsc committed
// http://developer.intel.com/design/pentium/datashts/24201606.pdf

#include "types.h"
#include "defs.h"
rsc's avatar
rsc committed
#include "mp.h"
rtm's avatar
rtm committed
#include "proc.h"
rtm's avatar
rtm committed
struct cpu cpus[NCPU];
rsc's avatar
rsc committed
static struct cpu *bcpu;
rtm's avatar
rtm committed
int ncpu;
rsc's avatar
rsc committed
int
mp_bcpu(void)
{
  return bcpu-cpus;
}

static uchar
sum(uchar *addr, int len)
{
  int i, sum;
  
  sum = 0;
  for(i=0; i<len; i++)
    sum += addr[i];
  return sum;
}
rsc's avatar
rsc committed
// Look for an MP structure in the len bytes at addr.
static struct mp*
rsc's avatar
rsc committed
mp_search1(uchar *addr, int len)
rsc's avatar
rsc committed
  uchar *e, *p;

  e = addr+len;
rsc's avatar
rsc committed
  for(p = addr; p < e; p += sizeof(struct mp))
    if(memcmp(p, "_MP_", 4) == 0 && sum(p, sizeof(struct mp)) == 0)
rsc's avatar
rsc committed
      return (struct mp*)p;
rsc's avatar
rsc committed
// Search for the MP Floating Pointer Structure, which according to the
// spec is in one of the following three locations:
// 1) in the first KB of the EBDA;
// 2) in the last KB of system base memory;
// 3) in the BIOS ROM between 0xE0000 and 0xFFFFF.
static struct mp*
mp_search(void)
{
rtm's avatar
rtm committed
  uchar *bda;
  uint p;
  struct mp *mp;
rsc's avatar
rsc committed
  bda = (uchar*)0x400;
rsc's avatar
 
rsc committed
  if((p = ((bda[0x0F]<<8)|bda[0x0E]) << 4)){
rsc's avatar
rsc committed
    if((mp = mp_search1((uchar*)p, 1024)))
      return mp;
rsc's avatar
rsc committed
  } else {
    p = ((bda[0x14]<<8)|bda[0x13])*1024;
rsc's avatar
rsc committed
    if((mp = mp_search1((uchar*)p-1024, 1024)))
      return mp;
  }
rsc's avatar
rsc committed
  return mp_search1((uchar*)0xF0000, 0x10000);
rsc's avatar
rsc committed
// Search for an MP configuration table.  For now,
rsc's avatar
rsc committed
// don't accept the default configurations (physaddr == 0).
// Check for correct signature, calculate the checksum and,
// if correct, check the version.
// To do: check extended table checksum.
rsc's avatar
rsc committed
static struct mpconf*
mp_config(struct mp **pmp)
rsc's avatar
rsc committed
  struct mpconf *conf;
  struct mp *mp;
  if((mp = mp_search()) == 0 || mp->physaddr == 0)
rsc's avatar
rsc committed
    return 0;
  conf = (struct mpconf*)mp->physaddr;
  if(memcmp(conf, "PCMP", 4) != 0)
    return 0;
  if(conf->version != 1 && conf->version != 4)
    return 0;
  if(sum((uchar*)conf, conf->length) != 0)
    return 0;
  *pmp = mp;
  return conf;
rsc's avatar
rsc committed
mp_init(void)
rsc's avatar
rsc committed
{
rtm's avatar
rtm committed
  uchar *p, *e;
rsc's avatar
rsc committed
  struct mp *mp;
  struct mpconf *conf;
  struct mpproc *proc;
  struct mpioapic *ioapic;
rsc's avatar
rsc committed
  bcpu = &cpus[ncpu];
  if((conf = mp_config(&mp)) == 0)
rsc's avatar
rsc committed
    return;
rsc's avatar
rsc committed
  lapic = (uint*)conf->lapicaddr;
rsc's avatar
rsc committed

rsc's avatar
rsc committed
  for(p=(uchar*)(conf+1), e=(uchar*)conf+conf->length; p<e; ){
    switch(*p){
rsc's avatar
rsc committed
    case MPPROC:
      proc = (struct mpproc*)p;
rtm's avatar
rtm committed
      cpus[ncpu].apicid = proc->apicid;
rsc's avatar
rsc committed
      if(proc->flags & MPBOOT)
        bcpu = &cpus[ncpu];
      ncpu++;
rsc's avatar
rsc committed
      p += sizeof(struct mpproc);
      continue;
rsc's avatar
rsc committed
      ioapic = (struct mpioapic*)p;
      ioapic_id = ioapic->apicno;
      p += sizeof(struct mpioapic);
      continue;
rsc's avatar
rsc committed
    case MPBUS:
rsc's avatar
rsc committed
    case MPLINTR:
      p += 8;
      continue;
    default:
rsc's avatar
rsc committed
      cprintf("mp_init: unknown config type %x\n", *p);
      panic("mp_init");
rsc's avatar
rsc committed
  if(mp->imcrp){
rsc's avatar
rsc committed
    // Bochs doesn't support IMCR, so this doesn't run on Bochs.
    // But it would on real hardware.
rsc's avatar
rsc committed
    outb(0x22, 0x70);   // Select IMCR
rsc's avatar
rsc committed
    outb(0x23, inb(0x23) | 1);  // Mask external interrupts.