Skip to content

Commit c9fa90f

Browse files
committedFeb 2, 2017
A tiny bit of clean up (e.g., move code searching cpu array from lapic.c into
mycpu() in proc.c.
1 parent 2e2d14c commit c9fa90f

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed
 

‎console.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ panic(char *s)
112112
cli();
113113
cons.locking = 0;
114114
// use lapiccpunum so that we can call panic from mycpu()
115-
cprintf("cpu %d: panic: ", lapiccpunum());
115+
cprintf("lapicid %d: panic: ", lapicid());
116116
cprintf(s);
117117
cprintf("\n");
118118
getcallerpcs(&s, pcs);

‎defs.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void kbdintr(void);
7474

7575
// lapic.c
7676
void cmostime(struct rtcdate *r);
77-
int lapiccpunum(void);
77+
int lapicid(void);
7878
extern volatile uint* lapic;
7979
void lapiceoi(void);
8080
void lapicinit(void);

‎lapic.c

+2-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "traps.h"
1010
#include "mmu.h"
1111
#include "x86.h"
12-
#include "proc.h" // ncpu
1312

1413
// Local APIC registers, divided by 4 for use as uint[] indices.
1514
#define ID (0x0020/4) // ID
@@ -98,22 +97,12 @@ lapicinit(void)
9897
lapicw(TPR, 0);
9998
}
10099

101-
// Should be called with interrupts disabled: the calling thread shouldn't be
102-
// rescheduled between reading lapic[ID] and checking against cpu array.
103100
int
104-
lapiccpunum(void)
101+
lapicid(void)
105102
{
106-
int apicid, i;
107-
108103
if (!lapic)
109104
return 0;
110-
111-
apicid = lapic[ID] >> 24;
112-
for (i = 0; i < ncpu; ++i) {
113-
if (cpus[i].apicid == apicid)
114-
return i;
115-
}
116-
panic("unknown apicid\n");
105+
return lapic[ID] >> 24;
117106
}
118107

119108
// Acknowledge interrupt.

‎proc.c

+13-2
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,24 @@ cpuid() {
3232
return mycpu()-cpus;
3333
}
3434

35-
// Must be called with interrupts disabled
35+
// Must be called with interrupts disabled to avoid the caller being rescheduled
36+
// between reading lapicid and running through the loop.
3637
struct cpu*
3738
mycpu(void)
3839
{
40+
int apicid, i;
41+
3942
if(readeflags()&FL_IF)
4043
panic("mycpu called with interrupts enabled\n");
41-
return &cpus[lapiccpunum()];
44+
45+
apicid = lapicid();
46+
// APIC IDs are not guaranteed to be contiguous. Maybe we should have
47+
// a reverse map, or reserve a register to store &cpus[i].
48+
for (i = 0; i < ncpu; ++i) {
49+
if (cpus[i].apicid == apicid)
50+
return &cpus[i];
51+
}
52+
panic("unknown apicid\n");
4253
}
4354

4455
// Disable interrupts so that we are not rescheduled

0 commit comments

Comments
 (0)
Please sign in to comment.