2006-09-07 16:12:30 +02:00
|
|
|
// Mutual exclusion spin locks.
|
|
|
|
|
2006-06-22 03:28:57 +02:00
|
|
|
#include "types.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "x86.h"
|
2006-07-06 23:47:22 +02:00
|
|
|
#include "mmu.h"
|
2006-07-12 03:48:35 +02:00
|
|
|
#include "param.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "spinlock.h"
|
2006-06-22 03:28:57 +02:00
|
|
|
|
2006-07-16 17:50:13 +02:00
|
|
|
extern int use_console_lock;
|
2006-07-12 13:15:38 +02:00
|
|
|
|
2006-08-11 00:08:14 +02:00
|
|
|
void
|
|
|
|
initlock(struct spinlock *lock, char *name)
|
|
|
|
{
|
|
|
|
lock->name = name;
|
|
|
|
lock->locked = 0;
|
|
|
|
lock->cpu = 0xffffffff;
|
|
|
|
}
|
|
|
|
|
2006-09-07 16:12:30 +02:00
|
|
|
// Record the current call stack in pcs[] by following the %ebp chain.
|
2006-08-08 21:58:06 +02:00
|
|
|
void
|
|
|
|
getcallerpcs(void *v, uint pcs[])
|
2006-07-16 03:15:28 +02:00
|
|
|
{
|
2006-08-08 21:58:06 +02:00
|
|
|
uint *ebp = (uint*)v - 2;
|
|
|
|
int i;
|
2006-09-06 21:08:14 +02:00
|
|
|
for(i = 0; i < 10; i++){
|
|
|
|
if(ebp == 0 || ebp == (uint*)0xffffffff)
|
|
|
|
break;
|
|
|
|
pcs[i] = ebp[1]; // saved %eip
|
|
|
|
ebp = (uint*)ebp[0]; // saved %ebp
|
2006-08-08 21:58:06 +02:00
|
|
|
}
|
2006-09-06 21:08:14 +02:00
|
|
|
for(; i < 10; i++)
|
2006-08-08 21:58:06 +02:00
|
|
|
pcs[i] = 0;
|
Changes to allow use of native x86 ELF compilers, which on my
Linux 2.4 box using gcc 3.4.6 don't seem to follow the same
conventions as the i386-jos-elf-gcc compilers.
Can run make 'TOOLPREFIX=' or edit the Makefile.
curproc[cpu()] can now be NULL, indicating that no proc is running.
This seemed safer to me than having curproc[0] and curproc[1]
both pointing at proc[0] potentially.
The old implementation of swtch depended on the stack frame layout
used inside swtch being okay to return from on the other stack
(exactly the V6 you are not expected to understand this).
It also could be called in two contexts: at boot time, to schedule
the very first process, and later, on behalf of a process, to sleep
or schedule some other process.
I split this into two functions: scheduler and swtch.
The scheduler is now a separate never-returning function, invoked
by each cpu once set up. The scheduler looks like:
scheduler() {
setjmp(cpu.context);
pick proc to schedule
blah blah blah
longjmp(proc.context)
}
The new swtch is intended to be called only when curproc[cpu()] is not NULL,
that is, only on behalf of a user proc. It does:
swtch() {
if(setjmp(proc.context) == 0)
longjmp(cpu.context)
}
to save the current proc context and then jump over to the scheduler,
running on the cpu stack.
Similarly the system call stubs are now in assembly in usys.S to avoid
needing to know the details of stack frame layout used by the compiler.
Also various changes in the debugging prints.
2006-07-11 03:07:40 +02:00
|
|
|
}
|
|
|
|
|
2006-09-07 16:12:30 +02:00
|
|
|
// Acquire the lock.
|
|
|
|
// Loops (spins) until the lock is acquired.
|
|
|
|
// (Because contention is handled by spinning, must not
|
|
|
|
// go to sleep holding any locks.)
|
Changes to allow use of native x86 ELF compilers, which on my
Linux 2.4 box using gcc 3.4.6 don't seem to follow the same
conventions as the i386-jos-elf-gcc compilers.
Can run make 'TOOLPREFIX=' or edit the Makefile.
curproc[cpu()] can now be NULL, indicating that no proc is running.
This seemed safer to me than having curproc[0] and curproc[1]
both pointing at proc[0] potentially.
The old implementation of swtch depended on the stack frame layout
used inside swtch being okay to return from on the other stack
(exactly the V6 you are not expected to understand this).
It also could be called in two contexts: at boot time, to schedule
the very first process, and later, on behalf of a process, to sleep
or schedule some other process.
I split this into two functions: scheduler and swtch.
The scheduler is now a separate never-returning function, invoked
by each cpu once set up. The scheduler looks like:
scheduler() {
setjmp(cpu.context);
pick proc to schedule
blah blah blah
longjmp(proc.context)
}
The new swtch is intended to be called only when curproc[cpu()] is not NULL,
that is, only on behalf of a user proc. It does:
swtch() {
if(setjmp(proc.context) == 0)
longjmp(cpu.context)
}
to save the current proc context and then jump over to the scheduler,
running on the cpu stack.
Similarly the system call stubs are now in assembly in usys.S to avoid
needing to know the details of stack frame layout used by the compiler.
Also various changes in the debugging prints.
2006-07-11 03:07:40 +02:00
|
|
|
void
|
2006-09-06 19:27:19 +02:00
|
|
|
acquire(struct spinlock *lock)
|
2006-06-22 03:28:57 +02:00
|
|
|
{
|
2006-08-08 21:58:06 +02:00
|
|
|
if(holding(lock))
|
|
|
|
panic("acquire");
|
2006-07-17 07:00:25 +02:00
|
|
|
|
2006-08-10 04:07:10 +02:00
|
|
|
if(cpus[cpu()].nlock == 0)
|
|
|
|
cli();
|
|
|
|
cpus[cpu()].nlock++;
|
2006-09-06 19:27:19 +02:00
|
|
|
|
2006-08-29 16:45:45 +02:00
|
|
|
while(cmpxchg(0, 1, &lock->locked) == 1)
|
|
|
|
;
|
2006-09-07 18:53:49 +02:00
|
|
|
|
2006-09-08 16:36:44 +02:00
|
|
|
// Serialize instructions: now that lock is acquired, make sure
|
|
|
|
// we wait for all pending writes from other processors.
|
|
|
|
cpuid(0, 0, 0, 0, 0); // memory barrier (see Ch 7 of IA-32 manual, vol 3)
|
2006-09-07 16:12:30 +02:00
|
|
|
|
|
|
|
// Record info about lock acquisition for debugging.
|
|
|
|
// The +10 is only so that we can tell the difference
|
|
|
|
// between forgetting to initialize lock->cpu
|
|
|
|
// and holding a lock on cpu 0.
|
2006-08-29 16:45:45 +02:00
|
|
|
lock->cpu = cpu() + 10;
|
2006-09-07 16:12:30 +02:00
|
|
|
getcallerpcs(&lock, lock->pcs);
|
2006-06-22 03:28:57 +02:00
|
|
|
}
|
|
|
|
|
2006-09-07 16:12:30 +02:00
|
|
|
// Release the lock.
|
2006-06-22 03:28:57 +02:00
|
|
|
void
|
2006-09-06 19:27:19 +02:00
|
|
|
release(struct spinlock *lock)
|
2006-06-22 03:28:57 +02:00
|
|
|
{
|
2006-08-29 16:45:45 +02:00
|
|
|
if(!holding(lock))
|
|
|
|
panic("release");
|
2006-07-17 07:00:25 +02:00
|
|
|
|
2006-08-29 16:45:45 +02:00
|
|
|
lock->pcs[0] = 0;
|
|
|
|
lock->cpu = 0xffffffff;
|
2006-09-07 18:53:49 +02:00
|
|
|
|
2006-09-08 16:36:44 +02:00
|
|
|
// Serialize instructions: before unlocking the lock, make sure
|
|
|
|
// to flush any pending memory writes from this processor.
|
|
|
|
cpuid(0, 0, 0, 0, 0); // memory barrier (see Ch 7 of IA-32 manual, vol 3)
|
2006-09-07 18:53:49 +02:00
|
|
|
|
2006-08-29 16:45:45 +02:00
|
|
|
lock->locked = 0;
|
|
|
|
if(--cpus[cpu()].nlock == 0)
|
|
|
|
sti();
|
2006-07-15 14:03:57 +02:00
|
|
|
}
|
|
|
|
|
2006-09-07 16:12:30 +02:00
|
|
|
// Check whether this cpu is holding the lock.
|
2006-07-17 07:00:25 +02:00
|
|
|
int
|
|
|
|
holding(struct spinlock *lock)
|
|
|
|
{
|
2006-08-29 16:45:45 +02:00
|
|
|
return lock->locked && lock->cpu == cpu() + 10;
|
2006-07-17 07:00:25 +02:00
|
|
|
}
|