Does the kernel process need to save something when switching to user mode?

6

2

According to my professor,

"while switching from user mode to kernel mode, the status of the user process has to be saved completely. But when switching from kernel mode to user mode, nothing has to be saved, as the kernel process has nothing to save."

Is he right? Doesn't the kernel process have anything to save?

Vishnu Vivek

Posted 2013-01-07T22:33:14.403

Reputation: 493

Question was closed 2013-01-09T00:30:43.573

Good question, but I don't know if this is the best site. I think http://programmers.stackexchange.com may get you a better answer. You can flag your own question for a moderator and he can move the question there for you.

– Scott Chamberlain – 2013-01-07T22:40:54.080

Should be on StackOverflow. – user541686 – 2013-01-07T23:15:47.940

Was he talking about a particular OS by the way? I think this is OS-dependent... one could write an OS that does need to save some things, I think. – user541686 – 2013-01-07T23:17:23.947

1

This question has nothing specific to do with programming and thus suggesting migrations to Programmers and Stack Overflow aren't the right way to deal with this question; it lies more to computers and there internals, I wouldn't say it's on-topic on Super User and it feels almost like http://cs.stackexchange.com/ or http://cstheory.stackexchange.com/. But well, let's see what the community decides to do with it...

– Tamara Wijsman – 2013-01-08T01:09:49.293

It's more computer science than anything else, voting to close doesn't offer the beta site as an option. – Tog – 2013-01-08T09:26:40.653

Answers

5

This is better understood if we dissect your story and attempt to use better terminology:

  • while switching from user mode to kernel mode

    This is also called a mode switch.

    When does this happen? This happens when a kernel routine is called; which can for instance happen when it is called from the user mode through elevation or through a hardware interrupt.

    What is such a routine? It's a piece of code that handles the call / interrupt.

  • the status of the user process has to be saved completely.

    Exactly, since the kernel will be running a kernel routine we need to temporarily save the status of the user process that was running.

  • But when switching from kernel mode to user mode, nothing has to be saved

    There is nothing to be saved since we called a routine which has done executing, there is no longer a status in the kernel process since the routine is no longer running.

    What does happen instead is that the status of the user process needs to be loaded again to continue executing the user process.

So, we have two different things here:

  • an user process, which needs to continue executing on a mode switch, so its status is stored.

  • a kernel routine, which is done executing on a mode switch, so its status is disposed .

As to answer your question "Doesn't the kernel process have anything to save?"; there isn't really a kernel process running, it's just a shim that houses kernel memory, routines and some other magic.

Tamara Wijsman

Posted 2013-01-07T22:33:14.403

Reputation: 54 163

1Switching between kernel mode and user mode is called "mode switching." "Context switching" is changing from one process to another. People often use "context switch" when they refer to a change in mode, but this is not correct. – Patrick Seymour – 2013-01-08T00:46:53.907

@PatrickS.: You are wrong when you state that this is not correct, the task context has an equivalent relation to the process status. What happens when you "context switch" is that you either load or save the context (and thus the process status), which is exactly what happens when you do a "mode switch". Therefore, the statement "a context switch happens when the processor does a mode switch" which I've expanded further at the bottom of my answer is therefore true. – Tamara Wijsman – 2013-01-08T00:51:37.280

Sorry, but I'm not wrong. Mode switching is changing modes. Context switching is changing processes. If change in mode does not require a change in the process being executed, there is no context switch required. – Patrick Seymour – 2013-01-08T00:56:10.113

@PatrickS.: Context switching is changing contexts, not processes; full process switches are irrelevant. – Tamara Wijsman – 2013-01-08T01:16:40.797

Changing contexts in order to.... ? – Patrick Seymour – 2013-01-08T01:35:00.640

1

I’ve never been fond of the “kernel process” terminology, but that’s just my personal issue.  I believe that the answer is that all processes are user processes at their base, and the “kernel process” is activated only when a kernel entry point is called from user mode.  (I suspect that your professor might say that I haven’t worded that exactly correctly, but I believe that I’m close enough to give you a reasonably accurate idea of the relationship.) 

It’s like what happens in an ordinary program: If you have a function hypot and it calls sqrt, the system has to save hypot’s local variables and the return point (program counter / instruction pointer from which sqrt was called).  But when sqrt returns and hypot resumes, sqrt’s stack frame can be discarded.

Scott

Posted 2013-01-07T22:33:14.403

Reputation: 17 653

1

This sounds like it's in the context of pre-emptive multitasking.

The OS/kernel is always running, so there is nothing to be saved. Other programs are running too (on top of the OS), but they each get a fraction of a second at a time, in turn. As each program gets its turn, the stack for that program needs to be loaded so processing can occur, then the state of all the registers need to be saved back to RAM from the processor so the next program can load.

Bigbio2002

Posted 2013-01-07T22:33:14.403

Reputation: 3 804

No, the OS/kernel is not "always running". Nor are other programs (threads). Most threads spend most of their time waiting. When they're waiting, they're not running. Most of the OS/kernel is a bunch of routines that get called from application code - when such a call is not in progress, those routines aren't "running". Some of the OS/kernel is run in response to interrupts, but once an interrupt is handled, the interrupt service routine isn't running either. – Jamie Hanrahan – 2015-12-06T15:59:21.850

1

The professor is partially correct.

The data that is being saved when switching from user mode to kernel mode is the state of the CPU, not the process. User mode and kernel mode are CPU-related, not process-related. When the kernel executes some function on behalf of the user, the state of the CPU is saved beforehand. That is, the state of the CPU when it was executing the user mode code. Then, the kernel performs its action based on the user mode code's request. Finally, the kernel restores the state of the CPU back to the previously-saved state. The kernel may return the CPU back to user mode, depending on the kernel in question.

The kernel process has nothing to save, because it was not running beforehand. It (the kernel process) does a job on behalf of the user mode process, and then it is finished. The CPU can return to user mode, as far as the kernel is concerned.

Operating system components typically run in kernel mode, also called Ring 0 on x86 processors. User mode contains applications and is sometimes referred to as Ring 3.

Patrick Seymour

Posted 2013-01-07T22:33:14.403

Reputation: 7 662