procinit() and map them high up (below TRAMPOLNE) with an empty
mapping below each stack. Never free a kernel stack.
Another way would be to allocate and map them dynamically, but then we
need to reload page table when switching processes in scheduler()
and/or have a kernel pagetable per proc (if we want k->stack to be the
same virtual address in each process).
One gotcha: kernel addresses are not equal to physical addresses for
stack addresses. A stack address must be translated if we need its
physical address (e.g., virtio passes a stack address to the disk).
- during exit(), hold p's parent lock and p's lock across all changes
to p and its parent (e.g., reparenting and wakeup1). the lock
ordering between concurrent exits of children, parent, and great
parent might work out because processes form a tree.
- in wakeup1() test and set p->state atomically by asking caller to
have p locked.
a correctness proof would be desirable.
reading process acquired p->lock and released virtio lock in sleep(),
but before the process had set p->status to SLEEPING, because the
wakeup tested p->status without holding p's lock. Thus, wakeup can
complete without seeing any process SLEEPING and then p sets p->status
to SLEEPING.
Fix some other issues:
- Don't initialize proc lock in allocproc(), because freeproc() sets
np->state = UNUSED and allocproc() can choose np and calls initlock()
on the process's lock, releasing np's lock accidentally. Move
initializing proc's lock to init.
- Protect nextpid using ptable.lock (and move into its own function)
Some clean up:
- Don't acquire p->lock when it p is used in a private way (e.g., exit()/grow()).
- Move find_runnable() back into scheduler().
locking plan, which is a difficult to understand because ptable lock
protects many invariants. This implementation has a bug: once in a
while xv6 unlocks a proc lock that is locked by another core.