Wednesday, September 7, 2011

what is kernel space (vs userland)

(sound-byte: system calls -- kernel space; standard library functions -- userland, often wrappers over syscalls)

Executive summary -- kernel is special source code written by kernel developers, to run in special kernel mode.

Q: But what distinguish kernel source code from application source code?
A: Kernel functions (like syscall functions) are written with special access to hardware devices. Kernel functions are the Gatekeepers to hardware, just like app developers write DAO class as gatekeepers to a DB.

Q: Real examples of syscall source code?
A: I believe glibc source code includes either syscall source code or kernel source code. I guess some kernel source code modules aren't in glibc. See P364[[GCC]]
A: kernel32.dll ? 
A: I feel device drivers are just like kernel source code, though RAM/CPU tend to be considered the kernel of kernel.

My 2-liner definition of kernel -- A kernel can be thought of as a bunch of (perhaps hundreds of) API functions known as "syscalls". They internally call additional (10,000 to 100,000) internal functions. Together these 2 bodies of source code constitutes a kernel. On an Intel platform, kernel and userland source code both compile to Intel instructions. At the individual instruction level, they are indistinguishable, but looking at the source code, you can tell which is kernel code.

There are really 2 distinct views (2 blind men describing an elephant) of a kernel. Let's focus on run-time actions --
X) a kernel is seen as special runtime services in the form of syscalls, similiar to guest calls to hotel service desk. I think this is the view of a C developer.
Y) behind-the-scene, secret stream of CPU instructions executed on the CPU, but not invoked by any userland app. Example -- scheduler [4]

I don't think a kernel is "a kind of daemon". Such a description is misleading. Various "regular" daemons provide services. They call kernel functions to access hardware. If a daemon never interacts with user processes, then maybe it would live in "kernel space". I guess kernel thread scheduler might be among them.

I feel it's unwise (but not wrong) to think of kernel as a process. Kernel services are used by processes. I guess it's possible for a process to live exclusively in "kernel space" and never interact with user processes. describes some kernel processes.

P241 [[Pro .net performance]] describes how something like func3 in kernel32.dll is loaded into a c# application's code area. This dll and this func3 are treated similar to regular non-kernel libraries. In a unix C++ application, glibc is linked in just like any regular library. See also and

[4] Scheduler is one example of (Y) that's so extremely prominent that everyone feels kernel is like a daemon.

The term "kernel space" is misleading -- it is not a special part of memory. Things in kspace don't run under a privileged user.

-- call stack view --
Consider a c# P/Invoke function calling into kernel32.dll (some kernel func3). If you were to take a snapshot of an average thread stack, top of the stack would be functions written by app developers; middle of the stack are (standard) library functions; bottom of the stack are -- if hardware is busy -- unfinished kernel syscalls. Our func3 would be in the last 2 layers.

All stack frames below a kernel API is "kernel space". These stack frames are internal functions within the kernel_code_base. Beneath all the stack frames is possibly hardware. Hardware is the ultimate low-level.

Look at the bottom-most frame, it might be a syscall. It might be called from java, python, or some code written in assembly. At runtime, we don't care about the flavor of the soruce code. The object code loaded into the "text" section of the Process is always a stream of machine-language instructions, perhaps in intel or sparx InstructionSet

ANY process under any user can call kernel API to access hardware. When people say kernel has special privileges, it means kernel code base is written like your DAO.

No comments:

Total Pageviews

my favorite topics (labels)


About Me

New York (Time Square), NY, United States