Monolithic Kernels and the Unix API

Monolithic Kernels and the Unix API

Monolithic Kernels and the Unix API 1 homework xv6 introduction: due Friday 2 anonymous feedback “It would be really helpful if homework directions were less vague. Things don’t have to be hard and confusing just for the sake of it. it probably is, but I can only make (probably bad) guesses about how it’s vague trying to maintain balance: giving complete homework requirements (without saying “modify line X of file Y ”) not having walls of text that no one reads not copying all the lecture material, etc. into homework writeup 3 homework steps system call implementation: sys_writecount hint in writeup: imitate sys_uptime need a counter for number of writes add writecount to several tables/lists (list of handlers, list of library functions to create, etc.) recommendation: imitate how other system calls are listed create a userspace program that calls writecount recommendation: copy from given programs 4 note on locks some existing code uses acquire/release you do not have to do this only for multiprocessor support …but, copying what’s done for ticks would be correct 5 xv6 context switch and saving user mode kernel mode start trap handler save A’s user regs to kernel stack running A swtch() — switch kernel stacks/kernel registers running B exit trap handler restore B’s user regs from kernel stack 6 where things go in context switch ‘from’ user stack ‘from’ kernel stack ‘to’ kernel stack ‘to’ user stack main’s return addr. saved user registers saved user registers main’s return addr. main’s vars trap return addr. trap return addr. main’s vars … … … … caller-saved registers caller-saved registers swtch arguments swtch arguments swtch return addr. swtch return addr. %esp value saved ebp saved ebp %esp value after just before exception saved ebx saved ebx return-from-exception saved esi saved esi saved edi saved edi last %esp value first %esp value for ‘from’ process for ‘to’ process (saved by swtch) (argument to swtch) 7 where things go in context switch ‘from’ user stack ‘from’ kernel stack ‘to’ kernel stack ‘to’ user stack main’s return addr. saved user registers saved user registers main’s return addr. main’s vars trap return addr. trap return addr. main’s vars … … … … caller-saved registers caller-saved registers swtch arguments swtch arguments swtch return addr. swtch return addr. %esp value saved ebp saved ebp %esp value after just before exception saved ebx saved ebx return-from-exception saved esi saved esi saved edi saved edi last %esp value first %esp value for ‘from’ process for ‘to’ process (saved by swtch) (argument to swtch) 7 (fromsetlidttrap theit to mmu.h):—returnsT_SYSCALL use the to kernelalltraps(= “code0x40 segment”) interrupt to alltrapsmeaning:// Set up restores runa normal in registers kernelinterrupt mode from/traptf,gate thendescriptor returns to. user-mode //befunction- callableistrap (in: from x86.h)1 for usera wrappingtrap modegate via lidt,int0 instructionforinstructionan interrupt gate. (otherwise:(yes,// interrupt code triggers segmentsgate faultclears specifies like privilegedFL_IF ,more instruction)trap thangate leavesthatFL_IF — nothingalone we care about) sets// vectors.S- thesel:interruptCode segmenttrapasm.S descriptorselector tablefor interrupttrap.c/trap handler vector64:// - off: Offset alltraps:in code segmentvoidfor interrupt/trap handler table//pushl- dpl of $0:handlerDescriptor functions...Privilegefor eachtrap(Level interruptstruct- trapframe type *tf) //pushl $64the privilegecall traplevel required{ for software to invoke //jmp alltrapsthis interrupt... /trap gate... explicitly using an int instruction. ...#define SETGATE(gate,iret istrap, sel, off, d) \ write syscall in xv6: interrupt table setup trap.c (run on boot) ... lidt(idt, sizeof(idt)); ... SETGATE(idt[T_SYSCALL], 1, SEG_KCODE<<3, vectors[T_SYSCALL], DPL_USER); ... vectors[T_SYSCALL] — OS function for processor to run set to pointer to assembly function vector64 8 interrupt descriptor table x86’s interrupt descriptor table has an entry for each kind of exception segmentation fault timer expired (“your program ran too long”) divide-by-zero system calls … xv6 sets all the table entries …and they always call the trap() function 9 xv6: struct proc process control block some data structure needed to represent a process called Process Control Block 10 process control block some data structure needed to represent a process called Process Control Block xv6: struct proc 10 current registers/PCprocessinformationthe kernel of IDis process processstack about for (user running?open this and files, process kernel) etc. enum procstatestored { on (pointertoevery identify to) process itsor process waiting?kernel has stack one in systemn kernel stack calls UNUSED,(if EMBRYO, not currently SLEEPING, running)or finished? RUNNABLE, RUNNING, ZOMBIE }; if waiting, ≈ thread’s state waiting for what (chan)? information about address space pgdir — used by processor sz — used by OS only xv6: struct proc struct proc { uint sz; // Size of process memory (bytes) pde_t* pgdir; // Page table char *kstack; // Bottom of kernel stack for this process enum procstate state; // Process state int pid; // Process ID struct proc *parent; // Parent process struct trapframe *tf; // Trap frame for current syscall struct context *context; // swtch() here to run process void *chan; // If non-zero, sleeping on chan int killed; // If non-zero, have been killed struct file *ofile[NOFILE]; // Open files struct inode *cwd; // Current directory char name[16]; // Process name (debugging) }; 11 processinformationthe kernel IDis processstack about for running?open this files, process etc. enum procstate { toevery identify processor process waiting? has one in systemn kernel stack calls UNUSED, EMBRYO, SLEEPING, or finished? RUNNABLE, RUNNING, ZOMBIE }; if waiting, waiting for what (chan)? information about address space pgdir — used by processor sz — used by OS only xv6: struct proc current registers/PC of process (user and kernel) stored on (pointer to) its kernel stack struct proc { uint sz; (if not currently// Size of running)process memory (bytes) pde_t* pgdir; // Page table char *kstack; // Bottom of kernel stack for this process enum procstate state;≈ thread’s// stateProcess state int pid; // Process ID struct proc *parent; // Parent process struct trapframe *tf; // Trap frame for current syscall struct context *context; // swtch() here to run process void *chan; // If non-zero, sleeping on chan int killed; // If non-zero, have been killed struct file *ofile[NOFILE]; // Open files struct inode *cwd; // Current directory char name[16]; // Process name (debugging) }; 11 current registers/PCprocessinformation of IDis process process about (user running?open and files, kernel) etc. enum procstatestored { on (pointerto identify to) itsor process waiting?kernel stack in systemn calls UNUSED,(if EMBRYO, not currently SLEEPING, running)or finished? RUNNABLE, RUNNING, ZOMBIE }; if waiting, ≈ thread’s state waiting for what (chan)? information about address space pgdir — used by processor sz — used by OS only xv6: struct proc the kernel stack for this process every process has one kernel stack struct proc { uint sz; // Size of process memory (bytes) pde_t* pgdir; // Page table char *kstack; // Bottom of kernel stack for this process enum procstate state; // Process state int pid; // Process ID struct proc *parent; // Parent process struct trapframe *tf; // Trap frame for current syscall struct context *context; // swtch() here to run process void *chan; // If non-zero, sleeping on chan int killed; // If non-zero, have been killed struct file *ofile[NOFILE]; // Open files struct inode *cwd; // Current directory char name[16]; // Process name (debugging) }; 11 current registers/PCprocessinformationthe kernel of ID process stack about for (user open this and files, process kernel) etc. stored on (pointertoevery identify to) process its process kernel has stack one in systemn kernel stack calls (if not currently running) ≈ thread’s state information about address space pgdir — used by processor sz — used by OS only xv6: struct proc is process running? or waiting? struct proc {enum procstate { uint sz; UNUSED, EMBRYO,// SLEEPING,Size of processor finished?memory (bytes) pde_t* pgdir; RUNNABLE, RUNNING,// Page ZOMBIEtable if waiting, char *kstack;}; // Bottom of kernel stack for this process enum procstate state; // Process statewaiting for what (chan)? int pid; // Process ID struct proc *parent; // Parent process struct trapframe *tf; // Trap frame for current syscall struct context *context; // swtch() here to run process void *chan; // If non-zero, sleeping on chan int killed; // If non-zero, have been killed struct file *ofile[NOFILE]; // Open files struct inode *cwd; // Current directory char name[16]; // Process name (debugging) }; 11 current registers/PCinformationthe kernel ofis process processstack about for (user running?open this and files, process kernel) etc. enum procstatestored { on (pointerevery to) process itsor waiting?kernel has stack one kernel stack UNUSED,(if EMBRYO, not currently SLEEPING, running)or finished? RUNNABLE, RUNNING, ZOMBIE }; if waiting, ≈ thread’s state waiting for what (chan)? information about address space pgdir — used by processor sz — used by OS only xv6: struct proc process ID to identify process in systemn calls struct proc { uint sz; // Size of process memory (bytes) pde_t* pgdir; // Page table char *kstack; // Bottom of kernel stack for this process enum procstate state; // Process state int pid; // Process ID struct proc *parent; // Parent

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    79 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us