A tiny bit of clean up (e.g., move code searching cpu array from lapic.c into

mycpu() in proc.c.
This commit is contained in:
Frans Kaashoek 2017-02-01 20:36:41 -05:00
parent 2e2d14c235
commit c9fa90f7e5
4 changed files with 17 additions and 17 deletions

View file

@ -112,7 +112,7 @@ panic(char *s)
cli(); cli();
cons.locking = 0; cons.locking = 0;
// use lapiccpunum so that we can call panic from mycpu() // use lapiccpunum so that we can call panic from mycpu()
cprintf("cpu %d: panic: ", lapiccpunum()); cprintf("lapicid %d: panic: ", lapicid());
cprintf(s); cprintf(s);
cprintf("\n"); cprintf("\n");
getcallerpcs(&s, pcs); getcallerpcs(&s, pcs);

2
defs.h
View file

@ -74,7 +74,7 @@ void kbdintr(void);
// lapic.c // lapic.c
void cmostime(struct rtcdate *r); void cmostime(struct rtcdate *r);
int lapiccpunum(void); int lapicid(void);
extern volatile uint* lapic; extern volatile uint* lapic;
void lapiceoi(void); void lapiceoi(void);
void lapicinit(void); void lapicinit(void);

15
lapic.c
View file

@ -9,7 +9,6 @@
#include "traps.h" #include "traps.h"
#include "mmu.h" #include "mmu.h"
#include "x86.h" #include "x86.h"
#include "proc.h" // ncpu
// Local APIC registers, divided by 4 for use as uint[] indices. // Local APIC registers, divided by 4 for use as uint[] indices.
#define ID (0x0020/4) // ID #define ID (0x0020/4) // ID
@ -98,22 +97,12 @@ lapicinit(void)
lapicw(TPR, 0); lapicw(TPR, 0);
} }
// Should be called with interrupts disabled: the calling thread shouldn't be
// rescheduled between reading lapic[ID] and checking against cpu array.
int int
lapiccpunum(void) lapicid(void)
{ {
int apicid, i;
if (!lapic) if (!lapic)
return 0; return 0;
return lapic[ID] >> 24;
apicid = lapic[ID] >> 24;
for (i = 0; i < ncpu; ++i) {
if (cpus[i].apicid == apicid)
return i;
}
panic("unknown apicid\n");
} }
// Acknowledge interrupt. // Acknowledge interrupt.

15
proc.c
View file

@ -32,13 +32,24 @@ cpuid() {
return mycpu()-cpus; return mycpu()-cpus;
} }
// Must be called with interrupts disabled // Must be called with interrupts disabled to avoid the caller being rescheduled
// between reading lapicid and running through the loop.
struct cpu* struct cpu*
mycpu(void) mycpu(void)
{ {
int apicid, i;
if(readeflags()&FL_IF) if(readeflags()&FL_IF)
panic("mycpu called with interrupts enabled\n"); panic("mycpu called with interrupts enabled\n");
return &cpus[lapiccpunum()];
apicid = lapicid();
// APIC IDs are not guaranteed to be contiguous. Maybe we should have
// a reverse map, or reserve a register to store &cpus[i].
for (i = 0; i < ncpu; ++i) {
if (cpus[i].apicid == apicid)
return &cpus[i];
}
panic("unknown apicid\n");
} }
// Disable interrupts so that we are not rescheduled // Disable interrupts so that we are not rescheduled