[Music] in this video we look at an important type of interrupts known as software interrupts and their applications for in system calls in the previous video we had looked at Hardware interrupts we had seen how a device such as a keyboard or a network card could assert a particular signal in the CPU and this would cause the CPU to asynchronously execute an interrupt Handler corresponding to the device so as we have seen in the previous videos uh this device would typically send a signal to the CPU through an intermediate device such as a pi or
a programmable interrupt controller in much the similar way we have what is known as a software interrupt however unlike having an external device which causes the interrupt here an instruction in the program would trigger the interrupt in this particular case for example and instructions such as int would cause the interrupt to occur and the operating system to execute so here the instruction is intx so X here is the interrupt number it typically has a value less than 256 and it is used to specify or distinguish between software interrupts so where is the software interrupt used
so software interrupts are used to implement system calls so as we know a user process could invoke a system call to perform some kernel operation for example it could be to read a file or to write a file to print something to a monitor or to send a packet through the network and so on most specifically all operating systems Implement system calls through one particular software interrupt for example in the Linux operating systems the software interrupt 128 is used to specify system calls therefore in a Linux OS if I have a int 128 which is
executed in the user process it would lead to an interrupt that occurs and cause the kernel or the operating system to execute and thereafter the OS would execute code depending depending on the interrupt in xp6 the software interrupt used to implement system calls is 64 or instruction like in 64 in the user process would be meant to implement a system call so to take an example let us consider that our application has a print F statement present in it so print F would print the this string to the standard output which typically is the monitor
now print f is a function present in the library lipy and it would cause the lipy function to be invoked now in the lipy function there is a call to the right system call with the specifier STD out so the STD out here is the file descriptor and it's a special file descriptor which is is meant for the standard output or the monitor so in the right function it would invoke int 64 in xb6 or int 128 in Linux and cost a software interrupt to occur so the software interrupt as we know would cause the
transformation from the user space to the kernel space and would it would result in the operating system executing the OS would then determine that the interrupt was in fact due to a system call and then it would determine what system call it was from in this case it was uh from a right system call and it was a right to the STD out the standard output so the operating system would then invoke the Handler for the right system call and this Handler would take care of communicating with the various devices such as the video card
to display the string onto the monitor so after this Handler completes execution the IR instruction is executed which would result in a transformation back from kernel space to the user space and the program will continue to execute now typically Operating Systems Support several different types of system calls so this particular table over here uh shows the various system calls supported by xv6 so in a previous video we had seen some of them for example we had seen the faul exit weight so on and you are also familiar with several types of system calls such as
open read write close uh change directory make directory and so on so each of these system calls would be executed by having a software interrupt such as int64 because it is the xp6 so it is 64 so each time any of these system calls are invoked by a user process it would trigger the operating system to execute now the next obvious question that one would ask is from the OS perspective how does the OS distinguish between the various system calls so we mentioned that all these system calls will use either in int 64 for xv6
and int 128 for Linux so how does the OS determine whether the system call was with respect to faul wave sleep exic and so on essentially this distinguisher comes from the user process itself what happens is that before the int64 instruction the user process will move a system call number to the eax register so for example this instruction move X comma eax will move the system call number to the eax registered now each system call in the operating system will have a unique number system call number now the operating system when triggered by the int
instruction would look up the E register and then determine what system call was invoked for example in xv6 if you look up these particular header files we you would see the various system call numbers defined for example over here we have each system call given a specific number for example cisor F given one uh CIS uncore exit given two and so on now when the OS gets triggered due to the int64 instruction getting executed the OS will determine the system call using this system call numbers and then invoke the corresponding system call Handler so each
of these system calls also have a corresponding system call Handler this is shown over here corresponding to each of the system call numbers cisor F that is one CIS uncore exit it's two and so on so these are system called functions present in the operating system which gets triggered based on the type of the system Cod for example if eax had a value of 11 the operating system will look into the exx register and determine that this corresponded to the get PID system call being invoked and then it would look into this particular table and
see that the uh get P system call is handled by this function cisor getp ID and therefore it will then invoke this cisor getpid function now let us look at the typical prototype of a system call so a typical system call is as shown over here so of course it has a system called name that is a function name and then it is passed some resource descriptor and parameters and typically would return an integer so the resource descriptor specifies what operating system resource is the target here for example it could be a file or a
device and as we've seen in the previous slides it could also specify a particular monitor for example if the resource descriptor is STD out then the resource in uh use here is the monitor so some system calls also do not specify this resource descriptor in such a case the system call is meant for that resource itself for example if you use the Sleep System call we only specify the time and no specific descriptor such as the file a device and so on this means that the current process wants to sleep for that given interval of
time the next part is the parameters so these parameters are specific for the system call for example if we invoke read write open or close or any other system call the parameter specified here is going to be very specific for each of these system calls for example the right system call has the parameters buff that is a void pointer and the count so the open or the close or any other system call would have different set of parameters so essentially these parameters are very specific to the type of the system column the return type is
typically int or integer and sometimes it is a void so int is typically used because in this way the operating system will be able to send the completion status of the system call whether it had executed successfully or it had failed and so on so sometimes the return is also used to specify certain specific information about the system call for example in right uh the return is siore T which in fact is type theft to integer and it specifies the number of bytes that have been written to the file specified in Ft so the return
type could also vary depending on the type of system call the next thing what we will look at is how these parameters that is the resource descriptor and the parameters passed to the system call are sent to the kernel so note that system calls are invoked very differently from a standard function call so in a function call we as we know the instruction call would be used and the call would specify a address which is where the function would reside and the various parameters for the call are passed through the local stack similarly the int
return which is returned from the function call would be returned through the eax register so system calls on the other hand work very differently from function calls so as we have seen system calls invokes the kernel by the int 64 instruction as in the case of xv6 so how are the parameters such as the desri resource descriptor and the other parameters passed to the kernel so essentially there are three ways of doing so the first is by pass by registers which is typically done in Linux the second way is by passing through the user mode
stack which is done in xv6 and the Third Way is by passing through a designated memory region so in this particular case uh what is done is that in the user process itself a designated region most likely in the Heap would be used to save the various parameters that is needed to be passed to the system call and the address to this region in the Heap is passed through the registers so we will look at the other two cases that is pass by registers and pass by user mode stack in more detail now pass by
registers which is used by Linux system calls would use the registers present in the processor to pass parameters to the kernel so we know we have already seen an example of this of how the eax register is used to pass the system call number from the user process to the operating system Sim in a similar way other registers such as the ebx ECX ESI EDI and EBP are used to pass the various parameters of the system call from the user process to the kernel if the system call has more than six arguments then a pointer
to a block structure containing the argument list is passed to the kernel now let us look at the second case that is pass via the user mode stack and this is what is done in xp6 so in this particular way before the int64 instruction is present various par parameters in the system call are pushed onto the stack for example if the system call had three parameters param 1 param 2 and param 3 so these three parameters are pushed into the user space stack and then the system called number as we have seen is moved into
the E register so this here is the user space stack in of the user process containing the three parameters now when the int instruction is executed as we know it triggers an interrupt causing the switch from the user space into the kernel space Also as a result of this interrupt execution as we have seen there is a switch in the stack from the user space stack to the kernel space stack and what we have seen in the previous video that this kernel stack is used to create what is known as the Trap frame so this
trap frame is shown over here so what we have seen in the previous video that some of these entries in the Trap frame are pushed into this stack automatically by the CPU so in particular these registers specified in capitals are all pushed onto the kernel stack that is onto the the Trap frame by the CPU now the SS and ESP here is important for us so these are the stack segment and the stack pointer and these registers correspond to the user space stack so as we know before the int64 has been executed the last known
stack pointer was pointing to this particular location location and therefore the contents of ESP will also point to this location in the user space stack so in this way the kernel will then use the SS and the ESP from the trap frame to determine the various parameters for the system call the next thing we will look at is how the return value is passed from the system call back to the user process again we will recollect that the entire reason for creating this trap frame in the kernel stack for the process is due to the
reason that when the interrupt or the system fall call completes its execution the entire state in the Trap frame it is restored back into the CPU corresponding CPU registers and it would res in the user process continuing to execute from where it had stopped and also the context of the user process is restored with the help of the Trap frame now in order to return a value from the system call which is executing in the kernel space back to the user space so what is done is that the eax register in the Trap frame is
modified essentially we had seen that the ex register because of this particular uh instruction would contain the system called number now this system call number is overwritten by the return value of the system call so this could be a negative number like minus one or a positive number as we have seen in the earlier slide so now when the system call executes and completes its execution and the context is transferred back to the user process the entire trap frame including the new value of exex uh in the Trap frame is restored back into the registers
of the CPU the process continues to execute from this particular instruction with the new value of EX which contains the return from the system call thank you [Music] [Applause]