xv6-cs450/mp.c

213 lines
4.5 KiB
C
Raw Normal View History

2006-06-21 03:53:07 +02:00
#include "types.h"
#include "mp.h"
#include "defs.h"
#include "param.h"
#include "x86.h"
2006-06-28 18:35:03 +02:00
#include "traps.h"
#include "mmu.h"
#include "proc.h"
2006-06-21 03:53:07 +02:00
static char* buses[] = {
"CBUSI ",
"CBUSII",
"EISA ",
"FUTURE",
"INTERN",
"ISA ",
"MBI ",
"MBII ",
"MCA ",
"MPI ",
"MPSA ",
"NUBUS ",
"PCI ",
"PCMCIA",
"TC ",
"VL ",
"VME ",
"XPRESS",
0,
};
#define APBOOTCODE 0x7000 // XXX hack
static struct mp* mp; // The MP floating point structure
struct cpu cpus[NCPU];
int ncpu;
static struct cpu *bcpu;
static struct mp*
2006-06-21 03:53:07 +02:00
mp_scan(uint8_t *addr, int len)
{
uint8_t *e, *p, sum;
int i;
cprintf("scanning: 0x%x\n", (uint32_t)addr);
e = addr+len;
for(p = addr; p < e; p += sizeof(struct mp)){
2006-06-21 03:53:07 +02:00
if(memcmp(p, "_MP_", 4))
continue;
sum = 0;
for(i = 0; i < sizeof(struct mp); i++)
2006-06-21 03:53:07 +02:00
sum += p[i];
if(sum == 0)
return (struct mp *)p;
2006-06-21 03:53:07 +02:00
}
return 0;
}
static struct mp*
2006-06-21 03:53:07 +02:00
mp_search(void)
{
uint8_t *bda;
uint32_t p;
struct mp *mp;
2006-06-21 03:53:07 +02:00
/*
* 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.
*/
bda = (uint8_t*) 0x400;
2006-06-21 03:53:07 +02:00
if((p = (bda[0x0F]<<8)|bda[0x0E])){
if((mp = mp_scan((uint8_t*) p, 1024)))
2006-06-21 03:53:07 +02:00
return mp;
}
else{
p = ((bda[0x14]<<8)|bda[0x13])*1024;
if((mp = mp_scan((uint8_t*)p-1024, 1024)))
2006-06-21 03:53:07 +02:00
return mp;
}
return mp_scan((uint8_t*)0xF0000, 0x10000);
2006-06-21 03:53:07 +02:00
}
static int
mp_detect(void)
{
struct mpctb *pcmp;
2006-06-21 03:53:07 +02:00
uint8_t *p, sum;
uint32_t length;
/*
* Search for an MP configuration table. For now,
* 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.
*/
if((mp = mp_search()) == 0 || mp->physaddr == 0)
2006-06-21 03:53:07 +02:00
return 1;
pcmp = (struct mpctb *) mp->physaddr;
2006-06-21 03:53:07 +02:00
if(memcmp(pcmp, "PCMP", 4))
return 2;
length = pcmp->length;
sum = 0;
for(p = (uint8_t*)pcmp; length; length--)
sum += *p++;
if(sum || (pcmp->version != 1 && pcmp->version != 4))
return 3;
cprintf("Mp spec rev #: %x imcrp 0x%x\n", mp->specrev, mp->imcrp);
2006-06-21 03:53:07 +02:00
return 0;
}
void
2006-07-17 03:25:22 +02:00
mp_init(void)
2006-06-21 03:53:07 +02:00
{
int r;
uint8_t *p, *e;
struct mpctb *mpctb;
struct mppe *proc;
struct mpbe *bus;
int i;
2006-06-21 03:53:07 +02:00
ncpu = 0;
if ((r = mp_detect()) != 0) return;
cprintf ("This computer is a multiprocessor!\n");
2006-06-21 03:53:07 +02:00
/*
* Run through the table saving information needed for starting
* application processors and initialising any I/O APICs. The table
* is guaranteed to be in order such that only one pass is necessary.
*/
mpctb = (struct mpctb *) mp->physaddr;
lapicaddr = (uint32_t *) mpctb->lapicaddr;
cprintf("apicaddr: %x\n", lapicaddr);
p = ((uint8_t*)mpctb)+sizeof(struct mpctb);
e = ((uint8_t*)mpctb)+mpctb->length;
2006-06-21 03:53:07 +02:00
while(p < e) {
switch(*p){
case MPPROCESSOR:
proc = (struct mppe *) p;
2006-06-22 22:47:23 +02:00
cpus[ncpu].apicid = proc->apicid;
cprintf("a processor %x\n", cpus[ncpu].apicid);
if (proc->flags & MPBP) {
2006-06-22 22:47:23 +02:00
bcpu = &cpus[ncpu];
}
2006-06-21 03:53:07 +02:00
ncpu++;
p += sizeof(struct mppe);
2006-06-21 03:53:07 +02:00
continue;
case MPBUS:
bus = (struct mpbe *) p;
for(i = 0; buses[i]; i++){
if(strncmp(buses[i], bus->string, sizeof(bus->string)) == 0)
break;
}
cprintf("a bus %d\n", i);
p += sizeof(struct mpbe);
2006-06-21 03:53:07 +02:00
continue;
case MPIOAPIC:
cprintf("an I/O APIC\n");
p += sizeof(struct mpioapic);
2006-06-21 03:53:07 +02:00
continue;
case MPIOINTR:
cprintf("an I/O intr\n");
p += sizeof(struct mpie);
2006-06-21 03:53:07 +02:00
continue;
default:
cprintf("mpinit: unknown PCMP type 0x%x (e-p 0x%x)\n", *p, e-p);
while(p < e){
cprintf("%uX ", *p);
p++;
}
break;
}
}
2006-06-22 22:47:23 +02:00
cprintf("ncpu: %d boot %d\n", ncpu, bcpu-cpus);
}
2006-06-21 03:53:07 +02:00
2006-07-12 19:00:54 +02:00
int
mp_bcpu(void)
{
return bcpu-cpus;
}
extern void mpmain(void);
void
2006-07-17 03:25:22 +02:00
mp_startthem(void)
{
extern uint8_t _binary_bootother_start[], _binary_bootother_size[];
extern int main();
int c;
memmove((void *) APBOOTCODE,_binary_bootother_start,
(uint32_t) _binary_bootother_size);
for(c = 0; c < ncpu; c++){
if (c == cpu()) continue;
cprintf ("starting processor %d\n", c);
*(uint *)(APBOOTCODE-4) = (uint) (cpus[c].mpstack) + MPSTACK; // tell it what to use for %esp
*(uint *)(APBOOTCODE-8) = (uint)mpmain; // tell it where to jump to
2006-07-12 19:00:54 +02:00
lapic_startap(cpus[c].apicid, (uint32_t) APBOOTCODE);
}
2006-06-21 03:53:07 +02:00
}