COS318 - Project #4OverviewReview - Processes and ThreadsReview - Processes and Threads cont’dOverview - Preemptive SchedulerOverview - System Call MechanismInterrupt Mechanism - In detailOverview - Context switchingOverview - Context switching cont’dContext Switching Details – MacrosContext Switching Details – ThreadsContext Switching Details – ProcessesContext Switching Details – Processes and ThreadsOverview - Thread SynchronizationOverview - Thread Synchronization cont’dOverview – Atomic ProcessesSlide 17Atomicity – DetailsAtomicity – Details cont’dAtomicity – Thread Synchronization FunctionsImplementation Details - Code FilesImplementation Details - Code Files cont’dImplementation Details - Extra Credit01/14/19 COS318 - Project #4Preemptive SchedulingFall 200201/14/19 OverviewImplement preemptive OS with:All the functionality of preview project’s OSAdditional thread synchronization:MESA style monitors (condition variables)Support for a preemptive scheduling:Implement the timed interrupt handler (irq0)Enforce all necessary atomicity01/14/19 Review -Processes and ThreadsThreads (trusted)Linked with the kernelCan share address space, variables, etc.Access kernel services (yield, exit, lock_*, condition_*,getpid(),*priority()) with direct calls to kernel functionsUse a single stack while running thread code and kernel code*Differences from previous project in italics01/14/19 Review -Processes and Threads cont’dProcesses (untrusted)Linked separately from kernelAppear after kernel in image file Cannot share address space, variables, etc.Access kernel services (yield, exit, getpid, *priority) via a unified system call mechanismUse a user stack while running process code and a kernel stack whilst running kernel level code*Differences from previous project in italics01/14/19 Overview -Preemptive SchedulerInitial proc/thread is dispatched at start timeProcesses voluntarily stop running by a system call interrupt invoking yield() or exit()Threads voluntarily stop running by a direct system call invoking yield(), exit() or block()block() is nested in lock_acquire() or condition_wait() Proc/threads involuntarily stop running due to a timer interrupt, irq0()Interrupt/Direct system calls invoke context savesScheduler is invoked to save kernel stack, select next available proc/thread from linked & dispatch*Differences from previous project in italics01/14/19 Overview -System Call MechanismThreads can call to system calls (yield(), exit(), lock_*(), condition_*(), getpid(), *priority()) directlyProcesses use system call interrupt mechanism to access system services (yield(), exit(), getpid(), *priority())Jump table, system_call_entry(), allows access to fixed set of kernel servicesAddress of jump table is specified in a Interrupt Descriptor Table (IDT)IDT’s location (in kernel) is specified to the CPUSoftware interrupt 48 (with desired service specified in eax) can then invoke system_call_entry()syslib.c maps system calls to int 48 with eax argument01/14/19 Interrupt Mechanism -In detail The following diagram shows how the interrupt mechanism works:0x1000 IDTInt 0Int 32Int 48system_call_entry()irq0()CPU*Every 10ms hardware interrupt IRQ0 invokes Int 3201/14/19 Overview -Context switching Contexts initialized for each PCB during startupUnique kernel and user stacks are assignedStarting addresses are storedContext (eflags and registers (including stack)) saved when execution is halted:For Threads At start of yield() call before call to schedule()At end of block() call before call to schedule()For Processes At start of system_call_entry() before potential call to _yield() (and hence to schedule())For BOTH:At start of irq0() before call to schedule()01/14/19 Overview -Context switching cont’dContext is swapped in by dispatcher but not as in the previous projectFirst time Set appropriate stack pointer and jump to starting address (as before)Subsequent times Restore the kernel_stack saved at start of schedule()Note that this is different from the stack saved as part of the context (regs.esp).Let dispatch() return so that the swapped in proc/thread returns from that schedule() call via return address stored on top of the saved stack. Context is restored by the code following said call to schedule().01/14/19 Context Switching Details –MacrosSAVE_GEN_REGS, RESTORE_GEN_REGSSaves and restores eax, ebx, ecx, edx, esi, edi, ebp, esp to current_running->regs.eax, ->regs.ebx, etc.Used in saving/restoring contextSAVE_EFLAGS, RESTORE_EFLAGSUses present stack to push and pop the processor status word to and from current_running->regs.eflagsUsed is saving/restoring thread context (interrupt mechanism automatically handles eflags for procs)SWITCH_TO_KERNEL_STACK, SAVE_KERNEL_STACKUses current_running->kernel_stack to restore and save the present stack, esp.Used in schedule()/dispatch(), system_call_entry(), and irq0() (for processes)01/14/19 Context Switching Details –Threadsyield()Context (eax,ebx,…,esp & eflags) saved.Schedule() is invoked and saves kernel_stack before moving to next proc/thread and dispatchingWhen swapped back in, dispatch returns to address on saved kernel stack, at point after schedule()Context (eax,ebx,…,esp & eflags) is restoredYield returns to address on top of stack (to point after yield call)block()PCB is enqueued on lock/condition linked listProceeds exactly as for yield()01/14/19 Context Switching Details –Processessystem_call_entry()eflags and eip are automatically saved because we enter via the interrupt mechanismContext (register set including esp) is saved.Switch to kernel stack.Invoke the desired system call.If scheduler is invoked (via _yield), it saves kernel_stack before moving to next proc/thread and dispatching (just as for threads)When swapped back in, dispatch returns to address on saved kernel stack, at point after schedule() in _yield()_yield() returns into system_call_entry()The kernel stack is savedContext (register set) is restored. ‘iret’ is invoked to restore eflags and return to eip automatically saved on stack upon interrupt entry.01/14/19 Context Switching Details –Processes and Threadsirq0() – What you must implement…eflags and eip are automatically saved
View Full Document