Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does the processor know an instruction is making a system call

system call -- It is an instruction that generates an interrupt that causes OS to gain control of processor.

so if a running process issue a system call (e.g. create/terminate/read/write etc), a interrupt is generated which cause the KERNEL TO TAKE CONTROL of the processor which then executes the required interrupt handler routine. correct?

then can anyone tell me how the processor known that this instruction is supposed to block the process, go to privileged mode, and bring kernel code.

I mean as a programmer i would just type stream1=system.io.readfile(ABC) or something, which translates to open and read file ABC.

Now what is monitoring the execution of this process, is there a magical power in the cpu to detect this?

As from what i have read a PROCESSOR can only execute only process at a time, so WHERE IS THE MONITOR PROGRAM RUNNING?

How can the KERNEL monitor if a system call is made or not when IT IS NOT IN RUNNING STATE!!

or does the computer have a SYSTEM CALL INSTRUCTION TABLE which it compares with before executing any instruction?

please help

thanku

like image 699
Doopy Doo Avatar asked Sep 01 '12 10:09

Doopy Doo


People also ask

How does the CPU respond to system call?

When responding to system calls, other traps/exceptions, and interrupts, OS code is run. The CPU automatically switches to monitor mode whenever an interrupt or trap occurs. So, the OS code is run in monitor mode. Input/output protection: Input/output is protected by making all input/output instructions privileged.

How does processor work when call instruction is executed?

Step 1: When a CALL instruction is executed first ,save the returning address on the top of the stack . Step2: Then microprocessor jumps into the specified CALL location where the subroutine is present. Step3: Execute the subroutine. The subroutine is terminated by the RET instruction.

What happens when a process makes a system call?

When a user program invokes a system call, a system call instruction is executed, which causes the processor to begin executing the system call handler in the kernel protection domain.

How are system calls triggered?

A system call (or fault or trap) is triggered synchronously by executing code.


2 Answers

The kernel doesn't monitor the process to detect a system call. Instead, the process generates an interrupt which transfers control to the kernel, because that's what software-generated interrupts do according to the instruction set reference manual.

For example, on Unix the process stuffs the syscall number in eax and runs an an int 0x80 instruction, which generates interrupt 0x80. The CPU reacts to this by looking in the Interrupt Descriptor Table to find the kernel's handler for that interrupt. This handler is the entry point for system calls.

So, to call _exit(0) (the raw system call, not the glibc exit() function which flushes buffers) in 32-bit x86 Linux:

movl  $1, %eax   # The system-call number.  __NR_exit is 1 for 32-bit
xor   %ebx,%ebx  # put the arg (exit status) in ebx
int   $0x80
like image 55
cnicutar Avatar answered Oct 23 '22 12:10

cnicutar


Let's analyse each questions you have posed.

  1. Yes, your understanding is correct.

  2. See, if any process/thread wants to get inside kernel there are only two mechanisms, one is by executing TRAP machine instruction and other is through interrupts. Usually interrupts are generated by the hardware, so any other process/threads wants to get into kernel it goes through TRAP. So as usual when TRAP is executed by the process it issues interrupt (mostly software interrupt) to your kernel. Along with trap you will also mentions the system call number, this acts as input to your interrupt handler inside kernel. Based on system call number your kernel finds the system call function inside system call table and it starts to execute that function. Kernel will set the mode bit inside cs register as soon as it starts to handle interrupts to intimate the processor as current instruction is a privileged instruction. By this your processor will comes to know whether the current instruction is privileged or not. Once your system call function finished it's execution your kernel will execute IRET instruction. Which will clear mode bit inside CS register to inform whatever instruction from now inwards are from user mode.

  3. There is no magical power inside processor, switching between user and kernel context makes us to think that processor is a magical thing. It is just a piece of hardware which has the capability to execute tons of instructions at a very high rate.

4..5..6. Answers for all these questions are answered in above cases.

I hope I've answered your questions up to some extent.

like image 20
Srinivas Desai Avatar answered Oct 23 '22 12:10

Srinivas Desai