Do these terms mean the same thing when talking about "kernel mode" and "user mode"? - operating-system

I am currently learning about kernel mode and user mode, and looks like each tutorial is using different terminology, and I am not sure if they are talking about the same thing.
Do these terms mean the same thing?:
kernel mode - system mode - privileged mode - supervisor mode - secure
mode - unrestricted mode
And do these terms mean the same thing?:
ordinary mode - user mode - restricted mode

They all mean more or less the same thing and may be used as synonyms in informal settings, but they do differ on certain levels. As it is the case with many words in the English language.
From Modern Operating Systems 4th Edition by Andrew S. Tanenbaum, p. 2-3:
Most computers have two modes of operations: kernel mode and user mode. The operating system, the most fundamental piece of software, runs in kernel mode (also called supervisor mode). In this mode, it has complete access to all hardware and can execute any instruction the machine is capable of executing. The rest of the software runs in user mode, in which only a subset of the machine instructions is available.
That's a general definition of the two groups of terms you enumerated. I'll go through the terms and I'll try to emphasize the quirks of each one.
Kernel mode is entered when the kernel is running. It is the exact opposite of user mode, which user programs run in.
System mode, privileged mode, and supervisor mode seem to be ARM terminology for different processor modes. For their exact meaning, you should read about them specifically. The quotation above outlines supervisor mode as a synonym of kernel mode too, but it also has a its own meaning in an ARM context. I think you may also casually refer to System Management Mode on x86 as "system mode," although that's imprecise. I've also seen "privileged mode" being used for anything higher than ring 3 in Protected Mode on x86.
Secure mode is apparently another type of ARM process modes. It's ARM-specific and I've never heard it in another context.
Unrestricted mode refers to a mode, where there are no restrictions; every instruction, all hardware is reachable (look at the quote). Restricted mode is the opposite.
Ordinary mode is foreign to me. Maybe an informal term for user mode.
As you see, it's pretty knotty. Different architectures use different terms, there is no general consensus, people use them differently and wrongly... it's a big hassle. Once you've read a number of books and papers about processor architectures and system programming, you'll get the hang of it, I figure, just a matter of experience.

Do these terms mean the same thing?:
kernel mode - system mode - privileged mode - supervisor mode - secure mode - unrestricted mode
What you are describing is system specific. The number of processor modes and their names varies among processors.
Most processors have multiple privilege levels (or modes) in which they execute (4 is common). Certain instructions can only be executed in certain privilege levels and memory access can be restricted by privilege levels.
The names of those modes depends entirely on the system and they often use conflicting names.
A well-designed operating system will use all four modes for increased protections. Poorly designed operating systems typically only use two modes.
The VMS operating system is one that uses four privilege modes.
User Mode
Supervisor Mode-The command interpreter is available to applications it invokes but is protected from the application itself.
Executive Mode-File level operations
Kernel Mode
Som processors call Kernel mode Supervisor mode.
Thus, the question of whether those terms are synonyms depends entirely upon the processor and operating system.

Related

Who decides which instructions are to be kept privileged? Is it the hardware manufacturer or the OS developers

I read that there are some privileged instructions in our system that can be executed in kernel mode. But I am unable to understand who make these instructions privileged . Is it the hardware manufacturer that hardwire some harmful instructions as privileged with the help of mode bit or is it the OS designers that make instructions privileged make them work only in privileged mode.
Kernel vs. user mode, and which instructions aren't allowed in user mode, is part of the ISA. That's baked in to the hardware.
CPU architects usually have a pretty good idea of what OSes need to do and want to prevent user-space from doing, so these choices at least make privilege levels possible, i.e. make it impossible for user-space to simply take over the machine.
But that's not the full picture: on some ISAs, such as x86, later ISA extensions have added control-register flag bits that let the OS choose whether some other instructions are privileged or not. On x86 that's done for instructions that could leak information about kernel ASLR, or make timing side-channels easier.
For example, rdpmc (read performance monitor counter) can only be used from user-space if specially enabled by the kernel. rdtsc (Read TimeStamp Counter) can be read from user-space by default, but the TSD (TimeStamp Disable) flag in CR4 can restrict its use to priv level 0 (kernel mode). Stopping user-space from using high-resolution timing is a brute-force way of defending against timing side-channel attacks.
Another x86 extension defends against leaking kernel addresses to make kernel ASLR more secret; CR4.UMIP (User Mode Instruction Prevention) disables instructions like sgdt that reads the virtual address of the GDT. Those instructions were basically useless for user-space in the first place, and unlike rdtsc easily could always have been privileged.
The Linux Kernel option to enable use of this extension describes it:
The User Mode Instruction Prevention (UMIP) is a security feature in newer Intel processors. If enabled, a general protection fault is issued if the SGDT, SLDT, SIDT, SMSW or STR instructions are executed in user mode. These instructions unnecessarily expose information about the hardware state.
The vast majority of applications do not use these instructions. For the very few that do, software emulation is provided in specific cases in protected and virtual-8086 modes. Emulated results are dummy.
Setting a new address for the IDT/GDT/LDT (e.g. lgdt/lidt) is of course a privileged instruction; those let you take over the machine. But until kernel ASLR was a thing, there wasn't any reason to stop user-space from reading the address. It could be in a page that had its page-table entry set to kernel only, preventing user-space from doing anything with that address. (... until Meltdown made it possible for user-space to use a speculative side-channel to read data from kernel-only pages that were hot in cache.)

Is kernel mode the time when OS gains full control of the system?

I find that computer system is utilizing in dual-mode way, which comprises kernel and user mode. I wonder if kernel mode and the time OS achieves full control of the computer system are equivalent or not.
Is kernel mode the time when OS gains full control of the system?
That's not a good way of looking at things. Processors generally have multiple modes of operation (usually 2 or 4 but sometimes others) that have different levels of privilege. One of those modes is Kernel mode.
So kernel mode is the the time when the processor is executing at the highest privilege level.
The operating system may not have full control when executing in kernel mode. It is possible on some systems for application code to run in kernel mode IF the process or application has sufficient privilege.
I recommend you check Chapter 1.4.2 Dual-mode and Multimode operation from the famous Dinosaur book: Operating System Concepts,10th by Abraham Silberschatz.
CPUs from the different vendor have different implementation of kernel mode. for example, Intel processors have four different modes, 0 being a kernel mode, 3 being a user mode and ARM v8 has 7 different modes. Also, we have a sperate mode for virtual machine manager, it has more privileges than user mode but fewer than the kernel.
also, your question is not clear to me. hopefully, this will give you a decent start point.

Why we need binary translation in full virtualization?

In hardware assisted virtualization, guest operation system runs on Ring 0, therefore it can run privileged instruction directly, am I right?
So why in full virtualization, VMM just won't run guest privileged instructions on Ring 0? why we need to translate them?
One reason which come into mind is different architectures (Different guest and host). is there anything more?
therefore it can run privileged instruction directly, am I right?
No, it is not completely true. Privileged instructions would still attempt accessing privileged resources and thus cannot be allowed to see/change them behind VMM's back. Therefore they would trap. That is why a classic VMM executes guests with "trap-and-emulate" approach. The majority of guest instructions that are non-privileged are executed directly, and privileged ones trap and a emulated one-by-one. No translation, that is, transformation of large (>1 guest instruction) blocks of the code is required in any case.
Alternatively, a system resource can be made non-privileged and thus instructions accessing it turn into innocuous inside the virtualized environment.
So why in full virtualization, VMM just won't run guest privileged instructions on Ring 0?
"Ring 0" is just a number, it does not mean much except that certain instructions receive new semantics: instead of faulting as they would do on the higher rings they are allowed to access system resources. But inside a VMM, they are not allowed to do that.
why we need to translate them?
We don't, individual privileged instructions may be trapped and then emulated, or interpreted. "Translation" as a term has meaning only for blocks of instructions.
One reason which come into mind is different architectures
That is a some sort of a degenerative case when 100% of guest instructions are "privileged", i.e. they will not behave as expected on chosen host. It does not make sense to attempt executing them directly, and interpreting each and every of them is too slow for many applications. This is where translation == compilation of bigger blocks starts making sense.
is there anything more?
For Intel architecture, there are certain architectural idiosyncrasies that sometimes make the idea of (temporarily) disabling hardware-assisted virtualization and falling back to binary translation beneficial in terms of speed and correctness. However, I assume this topic to be part of another, more specific question, as the answer is quite involved and requires deep understanding of Intel VT-x.

what is the difference between user mode and kernel mode in terms of total number of machine instructions available?

I read this paragraph from " Modern Operating Systems , Tanenbaum "
Most computers have two modes of operation: kernel
mode and user mode. The operating system is the most fundamental piece of software and runs in kernel mode (also called supervisor mode). In this mode it has complete access to all the hardware and can execute any instruction the machine is capable of executing. The rest of the software runs in user mode, in which only a subset of the machine instructions is available.
I am unable to get how they are describing difference in these two modes on basis of machine instructions available , at user end any software has the capability to make any changes at the hardware level ,like we have software which can affect the functioning of CPU , can play with registry details , so how can we say that at user mode , we have only subset of machine instructions available ?
The instructions that are available only in kernel mode are tend to be very few. These instructions are those that are only needed to manage the system.
For example, most processors have a HALT instruction that stops the CPU that is used for system shutdowns. Obviously you would not want any user to be able to execute HALT and stop the computer for everyone. Such instructions are then made only accessible in kernel mode.
Processors use a table of handlers for interrupt and exceptions. The Operating system creates such a table listing the handlers for these events. Then it loads register(s) giving the location(and size) of the table. The instructions for loading this register(s) are kernel mode only. Otherwise, any application could create total havoc on the system.
Instructions of these nature will trigger an exception if executing in user mode.
Such instructions tend to be few in number.
Well, in user-mode, there is definitely a subset of instructions available. This is the reason we have System Calls.
Example:
A user wants to create a new process in C. He cannot do that without entering kernel-mode, because certain set of instructions are only available to kernel-mode, So he uses the system call fork, that executes instructions for creating a new process (not available in user-mode). So System call is a mechanism of requesting a service from kernel of the OS to do something for the user, which he/she cannot write code for.
Following excerpt from above link sums it up in the best way:
A program is usually limited to its own address space so that it
cannot access or modify other running programs or the operating system
itself, and is usually prevented from directly manipulating hardware
devices (e.g. the frame buffer or network devices).
However, many normal applications obviously need access to these
components, so system calls are made available by the operating system
to provide well defined, safe implementations for such operations. The
operating system executes at the highest level of privilege, and
allows applications to request services via system calls, which are
often initiated via interrupts. An interrupt automatically puts the
CPU into some elevated privilege level, and then passes control to the
kernel, which determines whether the calling program should be granted
the requested service. If the service is granted, the kernel executes
a specific set of instructions over which the calling program has no
direct control, returns the privilege level to that of the calling
program, and then returns control to the calling program.

Is it possible or a common technique to sandbox kernel mode drivers?

I see that kernel mode drivers are risky as they run in privileged mode, but are there any monolithic kernel's that do any form of driver/loadable module sandboxing or is this really the domain of microkernels?
Yes, there are platforms with "monolithic" (for some definition of monolithic) kernels that do driver sandboxing for some drivers. Windows does this in more recent versions with the user mode driver framework. There are two reasons for doing this:-
It allows isolation. A failure in a user mode driver need not bring down the whole system. This is important for drivers for hardware which is not considered system critical. An example of this might be your printer, or your soundcard. In those cases if the driver fails, it can often simply be restarted and the user often won't even notice this happened.
It makes writing and debugging drivers much easier. Driver writers can use regular user mode libraries and regular user mode debuggers, without having to worry about things like IRQL and DPCs.
The other poster said there is no sense to this. Hopefully the above explains why you might want to do this. Additionally, the other poster said their is a performance concern. Again, this depends on the type of the driver. In Windows this is typically used for USB drivers. In the case of USB drivers, the driver is not talking directly to the hardware directly anyway regardless of the mode that the driver operates in - they are talking to another driver which is talking to the USB host controller, so there is much less overhead of user mode communication than there would be if you were writing a driver that had to bit bang IO ports from user mode. Also, you would avoid writing user mode drivers for hardware which was performance critical - in the case of printers and audio hardware the user mode transitions are so much faster than the hardware itself, that the performance cost of the one or two additional mode context switches is probably irrelevant.
So sometimes it is worth doing simply because the additional robustness and ease of development make the small and often unnoticeable performance reduction worthwhile.
There are no sense in this sandboxing, OS fully trust to drivers code. Basically this drivers become part of kernel. You can't failover after FS crash or any major subsystem of kernel. Basically it`s bad (failover after crash, imagine that you can do after storage driver of boot disk crash?), because can lead to data loss for example.
And second - sandboxing lead to perfomance hit to all kernel code.