System Calls

System Calls

System Calls 1 Learning Outcomes • A high-level understanding of System Calls – Mostly from the user’s perspective • From textbook (section 1.6) • Exposure architectural details of the MIPS R3000 – Detailed understanding of the of exception handling mechanism • From “Hardware Guide” on class web site • Understanding of the existence of compiler function calling conventions – Including details of the MIPS ‘C’ compiler calling convention • Understanding of how the application kernel boundary is crossed with system calls in general • Including an appreciation of the relationship between a case study (OS/161 system call handling) and the general case. 2 Operating System System Calls Kernel Level Operating System Requests Applications (System Calls) User Level Applications Applications 3 System Calls • Can be viewed as special function calls – Provides for a controlled entry into the kernel – While in kernel, they perform a privileged operation – Returns to original caller with the result • The system call interface represents the abstract machine provided by the operating system. 4 A Brief Overview of Classes System Calls • From the user’s perspective – Process Management – File I/O – Directories management – Some other selected Calls – There are many more • On Linux, see man syscalls for a list 5 Some System Calls For Process Management 6 Some System Calls For File Management 7 Some System Calls For Directory Management 8 Some System Calls For Miscellaneous Tasks 9 System Calls • A stripped down shell: while (TRUE) { /* repeat forever */ type_prompt( ); /* display prompt */ read_command (command, parameters) /* input from terminal */ if (fork() != 0) { /* fork off child process */ /* Parent code */ waitpid( -1, &status, 0); /* wait for child to exit */ } else { /* Child code */ execve (command, parameters, 0); /* execute command */ } } 10 System Calls Some Win32 API calls 11 The MIPS R2000/R3000 • Before looking at system call mechanics in some detail, we need a basic understanding of the MIPS R3000 12 MIPS R3000 • Load/store architecture – No instructions that operate on memory except load and store – Simple load/stores to/from memory from/to registers • Store word: sw r4, (r5) – Store contents of r4 in memory using address contained in register r5 • Load word: lw r3,(r7) – Load contents of memory into r3 using address contained in r7 – Delay of one instruction after load before data available in destination register » Must always an instruction between a load from memory and the subsequent use of the register. – lw, sw, lb, sb, lh, sh ,…. 13 MIPS R3000 • Arithmetic and logical operations are register to register operations • E.g., add r3, r2, r1 • No arithmetic operations on memory • Example – add r3, r2, r1 ⇒ r3 = r2 + r1 • Some other instructions – add, sub, and, or, xor, sll, srl 14 MIPS R3000 • All instructions are encoded in 32-bit • Some instructions have immediate operands – Immediate values are constants encoded in the instruction itself – Only 16-bit value – Examples • Add Immediate: addi r2, r1, 2048 ⇒ r2 = r1 + 2048 • Load Immediate : li r2, 1234 ⇒ r2 = 1234 15 Example code Simple code example: a = a + 1 lw r4,32(r29) // r29 = stack pointer li r5, 1 add r4, r4, r1 sw r4,32(r29) Offset(Address) 16 MIPS Registers • User-mode accessible registers – 32 general purpose registers • r0 hardwired to zero • r31 the link register for jump- and-link (JAL) instruction – HI/LO • 2 * 32-bits for multiply and divide – PC • Not directly visible • Modified implicitly by jump and branch instructions 17 Branching and Jumping • Branching and li r2, 1 jumping have a sw r0,(r3) branch delay slot j 1f – The instruction li r2, 2 following a branch li r2, 3 or jump is always 1: sw r2, (r3) executed prior to destination of jump 18 MIPS R3000 • RISC architecture – 5 stage pipeline – Instruction partially through pipeline prior to jmp having an effect 19 Jump and Link Instruction MMM • JAL is used to implement function 0x10 jal 1f calls 0x14 nop – r31 = PC+8 0x18 lw r4,(r6) • Return Address MMM register (RA) is 1: used to return from 0x2a sw r2, (r3) function call MMM 0x38 jr r31 20 0x3a nop Coprocessor 0 • The processor control registers are located in CP0 – Exception/Interrupt management registers – Translation management registers CP0 • CP0 is manipulated using mtc0 CP1 (floating point) (move to) and mfc0 (move PC: 0x0300 from) instructions HI/LO – mtc0/mfc0 are only accessible in R1 kernel mode. 2 Rn 21 CP0 Registers • Exception Management • Miscellaneous • c0_cause – Cause of the recent – c0_prid exception • Processor Identifier • c0_status – Current status of the • Memory Management CPU • c0_epc • c0_index – Address of the • c0_random instruction that caused the exception • c0_entryhi • c0_badvaddr • c0_entrylo – Address accessed that • c0_context caused the exception – More about these later in course 22 c0_status • For practical purposes, you can ignore most bits – Green background is the focus 23 c0_status • IM • KU – Individual interrupt mask – 0 = kernel bits – 1 = user mode – 6 external • IE – 2 software – 0 = all interrupts masked – 1 = interrupts enable • Mask determined via IM bits • c, p, o = current, previous, old 24 c0_cause • IP • BD – Interrupts pending – If set, the instruction • 8 bits indicating current that caused the state of interrupt lines exception was in a • CE branch delay slot – Coprocessor error • ExcCode • Attempt to access disabled Copro. – The code number of the exception taken 25 Exception Codes 26 Exception Codes 27 c0_epc • The Exception Program nop Counter sw r3 (r4) – Points to address of where nop to restart execution after C0_epc handling the exception or C0_cause interrupt C0_status – Example CP1 (floating point) • Assume sw r3,(r4) causes a restartable fault PC: 0x0300 exception HI/LO Aside: We are ignore BD-bit in R1 c0_cause which is also used in 2 reality on rare occasions . Rn 28 Exception Vectors 29 Simple Exception Walk-through Application User Mode Kernel Mode Interrupt Handler 30 Hardware exception handling PC EPC 0x12345678 ? • Let’s now walk Cause through an exception ? – Assume an interrupt occurred as the Status KUo IEo KUp IEp KUc IEc previous instruction ? ? ? ? 1 1 completed – Note: We are in user mode with interrupts enabled 31 Hardware exception handling PC EPC 0x12345678 0x12345678 Cause ? • Instruction address at Status KUo IEo KUp IEp KUc IEc which to restart after ? ? ? ? 1 1 the interrupt is transferred to EPC 32 Hardware exception handling Interrupts PC EPC disabled 0x12345678 and previous0x12345678 state shifted Cause along ? Kernel Mode is Status KUo IEo KUp IEp KUc IEc set, and ? ? 1 1 0 0 previous mode shifted along 33 Hardware exception handling PC EPC 0x12345678 0x12345678 Cause 0 Status KUo IEo KUp IEp KUc IEc ? ? 1 1 0 0 Code for the exception placed in Cause. Note Interrupt code = 0 34 Hardware exception handling PC EPC 0x80000080 0x12345678 Cause 0 Status KUo IEo KUp IEp KUc IEc ? ? 1 1 0 0 Address of general exception vector placed in PC 35 Hardware exception handling PC EPC 0x80000080 0x12345678 • CPU is now running in Cause kernel mode at 0x80000080, with 0 interrupts disabled Status KUo IEo KUp IEp KUc IEc • All information required to – Find out what caused the ? ? 1 1 0 0 exception – Restart after exception Badvaddr handling is in coprocessor registers 36 Returning from an exception • For now, lets ignore – how the exception is actually handled – how user-level registers are preserved • Let’s simply look at how we return from the exception 37 Returning from an exception PC EPC 0x80001234 0x12345678 • This code to return is Cause 0 lw r27, saved_epc Status KUo IEo KUp IEp KUc IEc nop jr r27 Load the? contents? 1 1 of 0 0 rfe EPC which is usually moved earlier to somewhere in memory by the exception handler 38 Returning from an exception PC EPC 0x12345678 0x12345678 • This code to return is Cause 0 lw r27, saved_epc Status KUo IEo KUp IEp KUc IEc nop jr r27 ? ? 1 1 0 0 Store the EPC back in rfe the PC 39 Returning from an exception PC EPC 0x12345678 In the branch delay0x12345678 slot, execute a restore from • This code to return isexception Cause instruction 0 lw r27, saved_epc Status KUo IEo KUp IEp KUc IEc nop jr r27 ? ? ? ? 1 1 rfe 40 Returning from an exception PC EPC 0x12345678 0x12345678 • We are now back in the Cause same state we were in 0 when the exception happened Status KUo IEo KUp IEp KUc IEc ? ? ? ? 1 1 41 Function Stack Frames • Each function call allocates Stack a new stack frame for local Frame variables, the return Pointer address, previous frame f1() stack pointer etc. frame – Frame pointer: start of Stack current stack frame Pointer – Stack pointer: end of current stack frame • Example: assume f1() calls f2(), which calls f3(). 42 Function Stack Frames • Each function call allocates Stack a new stack frame for local variables, the return address, previous frame f1() stack pointer etc. frame – Frame pointer: start of Frame current stack frame Pointer – Stack pointer: end of current f2() stack stack frame frame Stack • Example: assume f1() calls Pointer f2(), which calls f3(). 43 Function Stack Frames • Each function call allocates Stack a new stack frame for local variables, the return address, previous frame f1() stack pointer etc. frame – Frame pointer: start of current stack frame – Stack pointer: end of current f2() stack stack frame Frame frame • Example: assume f1() calls Pointer f2(), which calls f3(). f3() stack Stack frame Pointer 44 Compiler Register Conventions • Given 32 registers, which registers are used for – Local variables?

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    84 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