1

So one thing I'm not clear on is why transitioning from user space to kernel space is architecture-dependent. For example, the linux kernel v5.4 code for system calls entering kernel space is different for x86 vs different architectures, and contains architecture-dependent assembly instructions, rather than just C code.

Why is this exactly? Why can't the entire linux kernel just be written in C...?

Any thoughts appreciated.

Thanks.

1 Answer 1

1

Kernel mode is very different from user-mode. The kernel gets to do anything it wants / access any part of the hardware, but user space is not. How code tells the hardware to switch between modes is specific to the architecture - so that switching mechanism is implemented as machine code - but encapsulated in a C library. You'll find the same thing anywhere that the OS interfaces with the hardware - i.e. in device drivers.

C is meant to be a language for writing operating systems in and providing portability across architectures.

2
  • 1
    Different architectures have different privilege modes (e.g., 4 for x86_64, 7 for arm, 3 for risc-v, etc.). Thus different code for different architectures. What your answer didn't address is why entering these modes needs to be done in assembly. gcc has compile options to generate instructions entering privileged modes.
    – doneal24
    May 31 at 19:06
  • IMHO, the Lion's Commentary should be required reading for anybody interested in kernel design. Dated but understandable. Not 28M lines of codes.
    – doneal24
    May 31 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.