blog_os icon indicating copy to clipboard operation
blog_os copied to clipboard

How to communicate between rings?

Open bingmatv opened this issue 9 months ago • 6 comments

User-space programs should run in CPU ring3, but kernel runs in ring0, how to communicate between rings?

bingmatv avatar Feb 07 '25 12:02 bingmatv

The kernel can go to userspace using iret/sysret and userspace can call into the kernel using the syscall instruction or by causing an exception.

bjorn3 avatar Feb 07 '25 13:02 bjorn3

The kernel can go to userspace using iret/sysret and userspace can call into the kernel using the syscall instruction or by causing an exception.

What is needed to do before userspace and what are the best resources @bjorn3 ?

ViktorPopp avatar Feb 07 '25 19:02 ViktorPopp

Maybe https://wiki.osdev.org/Getting_to_Ring_3 would help you?

bjorn3 avatar Feb 07 '25 19:02 bjorn3

  • https://nfil.dev/kernel/rust/coding/rust-kernel-to-userspace-and-back/
  • https://github.com/nikofil/rust-os

My OS: https://github.com/ChocolateLoverRaj/code-runner/tree/edition-3. Note that I am still working on the ELF stuff.

ChocolateLoverRaj avatar Feb 07 '25 19:02 ChocolateLoverRaj

Does userspace run slower than or as fast as kernelspace? Since userspace has less privilege, the hardware or kernel needs to check the privilege.

ghost avatar Feb 16 '25 11:02 ghost

Same speed. Only transitions between rings are relatively slow (couple hundred cycles if you optimize our kernel a bit).

bjorn3 avatar Feb 16 '25 12:02 bjorn3