Understanding higher level call to systemcalls
I am going through the book by Galvin on OS . There is a section at the end of chapter 2 where the author writes about "adding a system call " to the kernel. He describes how using asmlinkage we can create a file containing a function and make it qualify as a system call . But in the next part about how to call the system call he writes the following : " Unfortunately, these are low-level operations that cannot be performed using C language statements and instead require assembly instructions. Fortunately, Linux provides macros for instantiating wrapper functions that contain the appropriate assembly instructions. For instance, the following C program uses the _syscallO() macro to invoke the newly defined system call: Basically , I want to understand how syscall() function generally works . Now , what I understand by Macros is a system for text substitution . (Please correct me If I am wrong) How does a macro call an assembly language instruction ? Is it so that syscallO() when compiled is translated into the address(op code) of the instruction to execute a trap ?(But this somehow doesn't fit with concept or definition of macros that I have ) What exactly are the wrapper functions that are contained inside and are they also written in assembly language ? Suppose , I want to create a function of my own which performs the system call then what are the things that I need to do . Do , I need to compile it to generate the machine code for performing Trap instructions ?
Man, you have to pay $156 dollars to by the thing, then you actually have to read it. You could probably get an VMS Internals and Data Structures book for under $30. That said, let me try to translate that gibberish into English. System calls do not use the same kind of linkage (i.e. method of passing parameters and calling functions) that other functions use. Rather than executing a call instruction of some kind, to execute a system service, you trigger an exception (which in Intel is bizarrely called an interrupt). The CPU expects the operating system to create a DISPATCH TABLE and store its location and size in a special hardware register(s). The dispatch table is an array of pointers to handlers for exceptions and interrupts. Exceptions and interrupts have numbers so, when exception or interrupt number #1 occurs, the CPU invokes the 2d exception handler (not #0, but #1) in the dispatch table in kernel mode. What exactly are the wrapper functions that are contained inside and are they also written in assembly language ? The operating system devotes usually one (but sometimes more) exceptions to system services. You need to do some thing like this in assembly language to invoke a system service: INT $80 ; Explicitly trigger exception 80h Because you have to execute a specific instruction, this has to be one in assembly language. Maybe your C compiler can do assembly language in line to call system service like that. But even if it could, it would be a royal PITA to have to do it each time you wanted to call a system service. Plus I have not filled in all the details here (only the actual call to the system service). Normally, when you call functions in C (or whatever), the arguments are pushed on the program stack. Because the stack usually changes when you enter kernel mode, arguments to system calls need to be stored in registers. PLUS you need to identify what system service you want to execute. Usually, system services have numbers. The number of the system service is loaded into the first register (e.g., R0 or AX). The full process when you need to invoke a system service is: Save the registers you are going to overwrite on the stack. Load the arguments you want to pass to the system service into hardware registers. Load the number of the system service into the lowest register. Trigger the exception to enter kernel mode. Unload the arguments returned by the system service from registers Possibly do some error checking Restore the registers you saved before. Instead of doing this each time you call a system service, operating systems provide wrapper functions for high level languages to use. You call the wrapper as you would normally call a function. The wrapper (in assembly language) does the steps above for you. Because these wrappers are pretty much the same (usually the only difference is the result of different numbers of arguments), wrappers can be created using macros. Some assemblers have powerful macro facilities that allow a single macro to define all wrappers, even with different numbers of arguments. Linux provides multiple _syscall C macros that create wrappers. There is one for each number of arguments. Note that these macros are just for operating system developers. Once the wrapper is there, everyone can use it. How does a macro call an assembly language instruction ? These _syscall macros have to generate in line assembly code. Finally, note that these wrappers do not define the actual system service. That has to be set up in the dispatch table and the system service exception handler.
What is the true difference between a real mode program and a protected mode program?
How are datatypes that need more than 32 bits stored in a 32 bit OS
How user programs like in Java, make system calls/ call kernel subroutines?
Solution to Critical Section Issue
How can Nucleus get the ID list?
To find execution time on a mult-icore machine
Raspberry Pi bare metal programming
mavericks macports installed outdated
Os fingerprinting with MAC address
How to get return value using Fork() in NachOs?
What exactly happens when an OS goes into kernel mode?
Need for Direct Memory Acess
techniques that can be used to protect critical sections
Why did Windows NT move away from the microkernel? [closed]
fork() in operating system
How does an instruction know if it's running in privilleged or user mode?