DEV Community

Shariful Islam Sourav
Shariful Islam Sourav

Posted on

π—§π—›π—˜ π—–π—’π—‘π—‘π—˜π—–π—§π—œπ—’π—‘ π—•π—˜π—§π—ͺπ—˜π—˜π—‘ π—¨π—¦π—˜π—₯ 𝗣π—₯π—’π—šπ—₯𝗔𝗠𝗦 𝗔𝗑𝗗 𝗒𝗦 π—žπ—˜π—₯π—‘π—˜π—Ÿ

The relation between the user mode and the OS kernel is a heavily guarded process. User mode is basically where the user program runs and does the basic things with limited privileges and kernel mode is unrestricted access to the whole system and hardware. By default, user programs can’t enter kernel mode to ensure the safety of the system.

π—¦π—¬π—¦π—§π—˜π—  π—–π—”π—Ÿπ—Ÿ - π—§π—›π—˜ 𝗕π—₯π—œπ——π—šπ—˜

As user programs are not directly given access to the Kernel. There is a bridge that is called sytem call where the OS is the guard who decides who gets to enter and where to go inside the kernel. System call is just an abstract concept, behind the scene this is just a instruction called β€œTrap” (syscall).

𝗧π—₯𝗔𝗣 π—Ÿπ—œπ—™π—˜ π—–π—¬π—–π—Ÿπ—˜:

  1. π—§π—›π—˜ π—₯π—˜π—€π—¨π—˜π—¦π—§: If a program wants to do something it does not have the privilege. It calls a standard library function to make this request.
  2. π—§π—›π—˜ 𝗧π—₯𝗔𝗣: Inside the library function, there is a machine instruction called trap or syscall. Which is executed.
  3. π—¦π—”π—©π—œπ—‘π—š π—¦π—§π—”π—§π—˜: When trap is executed, the CPU pauses the program and takes a snapshot of the current state by storing the Program Counter(PC), CPU register, status flag onto a secured kernel stack.
  4. 𝗣π—₯π—œπ—©π—œπ—Ÿπ—˜π—šπ—˜ 𝗦π—ͺπ—œπ—§π—–π—›: Meanwhile, the hardware switches a bit to increase the privilege level of CPU from user mode to kernel mode.
  5. 𝗧π—₯𝗔𝗣 π—§π—”π—•π—Ÿπ—˜: Then the OS decides the entry point, memory address by seeing the predefined manual called the Trap Table or the Interrupt Table.
  6. π—§π—›π—˜ π—˜π—«π—˜π—–π—¨π—§π—œπ—’π—‘: Then the CPU jumps to the specific entry point in the kernel mode and executes the required task.
  7. π—₯π—˜π—§π—¨π—₯𝗑-𝗙π—₯𝗒𝗠-𝗧π—₯𝗔𝗣: After the task is completed the control must be given back again. Then another instruction called β€œReturn-from-trap” is executed.
  8. π—₯π—˜π—¦π—§π—’π—₯π—˜ π—¦π—§π—”π—§π—˜ 𝗔𝗑𝗗 𝗣π—₯π—œπ—©π—œπ—Ÿπ—˜π—šπ—˜: The hardware takes control again. It takes the snapshot from kernel stack and restores the state in user program and flips the mode bit again and drops the privilege level to user mode.
  9. π—¨π—¦π—˜π—₯ 𝗣π—₯π—’π—šπ—₯𝗔𝗠 π—₯π—˜π—¦π—¨π— π—˜: User program is back where it was before the trap. Moreover, it can’t even tell what happened in between. The user program runs normally as if nothing happened.

𝗔𝗑 π—˜π—«π—”π— π—£π—Ÿπ—˜:

Printing something like β€œprintf in C”:

Our program cannot directly command the monitor to display the pixels showing β€œHello”. When we call printf("Hello"); the C library triggers a trap called write(). The OS comes in, it communicates with display drivers and puts β€œHello” on the screen and returns control to code.

Top comments (0)