2006-06-12 15:22:12 +00:00
|
|
|
#include "types.h"
|
|
|
|
#include "mmu.h"
|
|
|
|
#include "x86.h"
|
|
|
|
#include "param.h"
|
2006-06-27 14:35:53 +00:00
|
|
|
#include "fd.h"
|
2006-06-22 20:47:23 +00:00
|
|
|
#include "proc.h"
|
2006-06-12 15:22:12 +00:00
|
|
|
#include "defs.h"
|
2006-07-12 01:48:35 +00:00
|
|
|
#include "spinlock.h"
|
|
|
|
|
|
|
|
struct spinlock proc_table_lock;
|
2006-06-12 15:22:12 +00:00
|
|
|
|
|
|
|
struct proc proc[NPROC];
|
2006-06-22 20:47:23 +00:00
|
|
|
struct proc *curproc[NCPU];
|
2006-06-15 19:58:01 +00:00
|
|
|
int next_pid = 1;
|
2006-06-12 15:22:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* set up a process's task state and segment descriptors
|
|
|
|
* correctly, given its current size and address in memory.
|
|
|
|
* this should be called whenever the latter change.
|
|
|
|
* doesn't change the cpu's current segmentation setup.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
setupsegs(struct proc *p)
|
|
|
|
{
|
|
|
|
memset(&p->ts, 0, sizeof(struct Taskstate));
|
|
|
|
p->ts.ts_ss0 = SEG_KDATA << 3;
|
|
|
|
p->ts.ts_esp0 = (unsigned)(p->kstack + KSTACKSIZE);
|
|
|
|
|
2006-06-13 15:50:06 +00:00
|
|
|
// XXX it may be wrong to modify the current segment table!
|
|
|
|
|
2006-06-12 15:22:12 +00:00
|
|
|
p->gdt[0] = SEG_NULL;
|
|
|
|
p->gdt[SEG_KCODE] = SEG(STA_X|STA_R, 0, 0xffffffff, 0);
|
|
|
|
p->gdt[SEG_KDATA] = SEG(STA_W, 0, 0xffffffff, 0);
|
2006-06-15 16:02:20 +00:00
|
|
|
p->gdt[SEG_TSS] = SEG16(STS_T32A, (unsigned) &p->ts,
|
|
|
|
sizeof(p->ts), 0);
|
2006-06-12 15:22:12 +00:00
|
|
|
p->gdt[SEG_TSS].sd_s = 0;
|
|
|
|
p->gdt[SEG_UCODE] = SEG(STA_X|STA_R, (unsigned)p->mem, p->sz, 3);
|
|
|
|
p->gdt[SEG_UDATA] = SEG(STA_W, (unsigned)p->mem, p->sz, 3);
|
|
|
|
p->gdt_pd.pd__garbage = 0;
|
|
|
|
p->gdt_pd.pd_lim = sizeof(p->gdt) - 1;
|
|
|
|
p->gdt_pd.pd_base = (unsigned) p->gdt;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern void trapret();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* internal fork(). does not copy kernel stack; instead,
|
|
|
|
* sets up the stack to return as if from system call.
|
2006-07-12 01:48:35 +00:00
|
|
|
* caller must set state to RUNNABLE.
|
2006-06-12 15:22:12 +00:00
|
|
|
*/
|
|
|
|
struct proc *
|
2006-06-15 16:02:20 +00:00
|
|
|
newproc()
|
2006-06-12 15:22:12 +00:00
|
|
|
{
|
|
|
|
struct proc *np;
|
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 01:07:40 +00:00
|
|
|
struct proc *op;
|
2006-06-27 14:35:53 +00:00
|
|
|
int fd;
|
2006-06-12 15:22:12 +00:00
|
|
|
|
2006-07-12 01:48:35 +00:00
|
|
|
acquire(&proc_table_lock);
|
|
|
|
|
|
|
|
for(np = &proc[1]; np < &proc[NPROC]; np++){
|
|
|
|
if(np->state == UNUSED){
|
|
|
|
np->state = EMBRYO;
|
2006-06-12 15:22:12 +00:00
|
|
|
break;
|
2006-07-12 01:48:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(np >= &proc[NPROC]){
|
|
|
|
release(&proc_table_lock);
|
2006-06-12 15:22:12 +00:00
|
|
|
return 0;
|
2006-07-12 01:48:35 +00:00
|
|
|
}
|
2006-06-12 15:22:12 +00:00
|
|
|
|
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 01:07:40 +00:00
|
|
|
// copy from proc[0] if we're bootstrapping
|
|
|
|
op = curproc[cpu()];
|
|
|
|
if(op == 0)
|
|
|
|
op = &proc[0];
|
|
|
|
|
2006-06-15 19:58:01 +00:00
|
|
|
np->pid = next_pid++;
|
2006-06-22 20:47:23 +00:00
|
|
|
np->ppid = op->pid;
|
2006-07-12 01:48:35 +00:00
|
|
|
|
|
|
|
release(&proc_table_lock);
|
|
|
|
|
2006-06-22 20:47:23 +00:00
|
|
|
np->sz = op->sz;
|
|
|
|
np->mem = kalloc(op->sz);
|
2006-06-12 15:22:12 +00:00
|
|
|
if(np->mem == 0)
|
|
|
|
return 0;
|
2006-06-22 20:47:23 +00:00
|
|
|
memcpy(np->mem, op->mem, np->sz);
|
2006-06-12 15:22:12 +00:00
|
|
|
np->kstack = kalloc(KSTACKSIZE);
|
|
|
|
if(np->kstack == 0){
|
2006-06-22 20:47:23 +00:00
|
|
|
kfree(np->mem, op->sz);
|
2006-07-12 01:48:35 +00:00
|
|
|
np->state = UNUSED;
|
2006-06-12 15:22:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
setupsegs(np);
|
|
|
|
|
|
|
|
// set up kernel stack to return to user space
|
2006-06-15 16:02:20 +00:00
|
|
|
np->tf = (struct Trapframe *) (np->kstack + KSTACKSIZE - sizeof(struct Trapframe));
|
2006-06-22 20:47:23 +00:00
|
|
|
*(np->tf) = *(op->tf);
|
2006-06-26 20:31:52 +00:00
|
|
|
np->tf->tf_regs.reg_eax = 0; // so fork() returns 0 in child
|
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 01:07:40 +00:00
|
|
|
cprintf("newproc pid=%d return to %x:%x tf-%p\n", np->pid, np->tf->tf_cs, np->tf->tf_eip, np->tf);
|
|
|
|
|
|
|
|
// set up new jmpbuf to start executing at trapret with esp pointing at tf
|
|
|
|
memset(&np->jmpbuf, 0, sizeof np->jmpbuf);
|
|
|
|
np->jmpbuf.jb_eip = (unsigned) trapret;
|
|
|
|
np->jmpbuf.jb_esp = (unsigned) np->tf - 4; // -4 for the %eip that isn't actually there
|
2006-06-12 15:22:12 +00:00
|
|
|
|
2006-06-27 14:35:53 +00:00
|
|
|
// copy file descriptors
|
|
|
|
for(fd = 0; fd < NOFILE; fd++){
|
|
|
|
np->fds[fd] = op->fds[fd];
|
|
|
|
if(np->fds[fd])
|
2006-07-12 01:48:35 +00:00
|
|
|
fd_reference(np->fds[fd]);
|
2006-06-27 14:35:53 +00:00
|
|
|
}
|
|
|
|
|
2006-06-15 16:02:20 +00:00
|
|
|
cprintf("newproc %x\n", np);
|
2006-06-12 15:22:12 +00:00
|
|
|
|
|
|
|
return np;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
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 01:07:40 +00:00
|
|
|
scheduler(void)
|
2006-06-12 15:22:12 +00:00
|
|
|
{
|
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 01:07:40 +00:00
|
|
|
struct proc *op, *np;
|
2006-07-01 21:26:01 +00:00
|
|
|
int i;
|
|
|
|
|
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 01:07:40 +00:00
|
|
|
cprintf("start scheduler on cpu %d jmpbuf %p\n", cpu(), &cpus[cpu()].jmpbuf);
|
|
|
|
cpus[cpu()].lastproc = &proc[0];
|
2006-07-01 21:26:01 +00:00
|
|
|
|
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 01:07:40 +00:00
|
|
|
setjmp(&cpus[cpu()].jmpbuf);
|
2006-07-12 01:48:35 +00:00
|
|
|
|
|
|
|
op = curproc[cpu()];
|
|
|
|
if(op){
|
|
|
|
if(op->newstate <= 0 || op->newstate > ZOMBIE)
|
|
|
|
panic("scheduler");
|
|
|
|
op->state = op->newstate;
|
|
|
|
op->newstate = -1;
|
|
|
|
}
|
|
|
|
|
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 01:07:40 +00:00
|
|
|
// find a runnable process and switch to it
|
|
|
|
curproc[cpu()] = 0;
|
|
|
|
np = cpus[cpu()].lastproc + 1;
|
2006-06-12 15:22:12 +00:00
|
|
|
while(1){
|
2006-07-12 01:48:35 +00:00
|
|
|
acquire(&proc_table_lock);
|
2006-07-01 21:26:01 +00:00
|
|
|
for(i = 0; i < NPROC; i++){
|
|
|
|
if(np >= &proc[NPROC])
|
|
|
|
np = &proc[0];
|
2006-06-12 15:22:12 +00:00
|
|
|
if(np->state == RUNNABLE)
|
|
|
|
break;
|
2006-06-22 20:47:23 +00:00
|
|
|
np++;
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
2006-07-12 01:48:35 +00:00
|
|
|
|
|
|
|
if(i < NPROC){
|
|
|
|
np->state = RUNNING;
|
2006-06-12 15:22:12 +00:00
|
|
|
break;
|
2006-07-12 01:48:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
release(&proc_table_lock);
|
2006-07-01 21:26:01 +00:00
|
|
|
np = &proc[0];
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
|
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 01:07:40 +00:00
|
|
|
cpus[cpu()].lastproc = np;
|
2006-06-22 20:47:23 +00:00
|
|
|
curproc[cpu()] = np;
|
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 01:07:40 +00:00
|
|
|
|
2006-07-12 09:10:25 +00:00
|
|
|
release(&proc_table_lock);
|
|
|
|
|
2006-06-15 19:58:01 +00:00
|
|
|
// h/w sets busy bit in TSS descriptor sometimes, and faults
|
|
|
|
// if it's set in LTR. so clear tss descriptor busy bit.
|
2006-06-22 20:47:23 +00:00
|
|
|
np->gdt[SEG_TSS].sd_type = STS_T32A;
|
2006-06-15 19:58:01 +00:00
|
|
|
|
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 01:07:40 +00:00
|
|
|
// XXX should probably have an lgdt() function in x86.h
|
|
|
|
// to confine all the inline assembly.
|
2006-06-15 19:58:01 +00:00
|
|
|
// XXX probably ought to lgdt on trap return too, in case
|
|
|
|
// a system call has moved a program or changed its size.
|
2006-06-13 15:50:06 +00:00
|
|
|
asm volatile("lgdt %0" : : "g" (np->gdt_pd.pd_lim));
|
|
|
|
ltr(SEG_TSS << 3);
|
|
|
|
|
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 01:07:40 +00:00
|
|
|
if(0) cprintf("cpu%d: run %d esp=%p callerpc=%p\n", cpu(), np-proc);
|
|
|
|
longjmp(&np->jmpbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
// give up the cpu by switching to the scheduler,
|
|
|
|
// which runs on the per-cpu stack.
|
|
|
|
void
|
2006-07-12 01:48:35 +00:00
|
|
|
swtch(int newstate)
|
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 01:07:40 +00:00
|
|
|
{
|
|
|
|
struct proc *p = curproc[cpu()];
|
|
|
|
if(p == 0)
|
|
|
|
panic("swtch");
|
2006-07-12 01:48:35 +00:00
|
|
|
p->newstate = newstate; // basically an argument to scheduler()
|
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 01:07:40 +00:00
|
|
|
if(setjmp(&p->jmpbuf) == 0)
|
|
|
|
longjmp(&cpus[cpu()].jmpbuf);
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
2006-06-15 19:58:01 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
sleep(void *chan)
|
|
|
|
{
|
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 01:07:40 +00:00
|
|
|
struct proc *p = curproc[cpu()];
|
|
|
|
if(p == 0)
|
|
|
|
panic("sleep");
|
|
|
|
p->chan = chan;
|
2006-07-12 01:48:35 +00:00
|
|
|
swtch(WAITING);
|
2006-06-15 19:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wakeup(void *chan)
|
|
|
|
{
|
|
|
|
struct proc *p;
|
|
|
|
|
2006-07-12 01:48:35 +00:00
|
|
|
acquire(&proc_table_lock);
|
2006-06-15 19:58:01 +00:00
|
|
|
for(p = proc; p < &proc[NPROC]; p++)
|
|
|
|
if(p->state == WAITING && p->chan == chan)
|
|
|
|
p->state = RUNNABLE;
|
2006-07-12 01:48:35 +00:00
|
|
|
release(&proc_table_lock);
|
2006-06-15 19:58:01 +00:00
|
|
|
}
|
2006-07-11 17:39:45 +00:00
|
|
|
|
|
|
|
// give up the CPU but stay marked as RUNNABLE
|
|
|
|
void
|
|
|
|
yield()
|
|
|
|
{
|
|
|
|
if(curproc[cpu()] == 0 || curproc[cpu()]->state != RUNNING)
|
|
|
|
panic("yield");
|
2006-07-12 01:48:35 +00:00
|
|
|
swtch(RUNNABLE);
|
2006-07-11 17:39:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
proc_exit()
|
|
|
|
{
|
|
|
|
struct proc *p;
|
|
|
|
struct proc *cp = curproc[cpu()];
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
cprintf("exit %x\n", cp);
|
|
|
|
|
|
|
|
for(fd = 0; fd < NOFILE; fd++){
|
|
|
|
if(cp->fds[fd]){
|
|
|
|
fd_close(cp->fds[fd]);
|
|
|
|
cp->fds[fd] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-12 01:48:35 +00:00
|
|
|
acquire(&proc_table_lock);
|
2006-07-11 17:39:45 +00:00
|
|
|
|
|
|
|
// wake up parent
|
|
|
|
for(p = proc; p < &proc[NPROC]; p++)
|
|
|
|
if(p->pid == cp->ppid)
|
|
|
|
wakeup(p);
|
|
|
|
|
|
|
|
// abandon children
|
|
|
|
for(p = proc; p < &proc[NPROC]; p++)
|
|
|
|
if(p->ppid == cp->pid)
|
|
|
|
p->pid = 1;
|
|
|
|
|
2006-07-12 01:48:35 +00:00
|
|
|
acquire(&proc_table_lock);
|
|
|
|
|
2006-07-11 17:39:45 +00:00
|
|
|
// switch into scheduler
|
2006-07-12 01:48:35 +00:00
|
|
|
swtch(ZOMBIE);
|
2006-07-11 17:39:45 +00:00
|
|
|
}
|