405 lines
32 KiB
HTML
405 lines
32 KiB
HTML
|
|
|||
|
<!DOCTYPE html>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<html lang="en">
|
|||
|
<head>
|
|||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|||
|
|
|||
|
<title>2.3. Kernel Mechanics — Computer Systems Fundamentals</title>
|
|||
|
|
|||
|
<link rel="stylesheet" href="_static/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous" />
|
|||
|
<link rel="stylesheet" href="_static/css/pygments.css" type="text/css" />
|
|||
|
<link rel="stylesheet" href="_static/css/normalize.css" type="text/css" />
|
|||
|
<link rel="stylesheet" href="../../../JSAV/css/JSAV.css" type="text/css" />
|
|||
|
<link rel="stylesheet" href="../../../lib/odsaMOD-min.css" type="text/css" />
|
|||
|
<link rel="stylesheet" href="_static/css/jquery-1.11.4-smoothness-ui.css" type="text/css" />
|
|||
|
<link rel="stylesheet" href="../../../lib/odsaStyle-min.css" type="text/css" />
|
|||
|
<link rel="stylesheet" href="_static/css/csf.css" type="text/css" />
|
|||
|
|
|||
|
<style>
|
|||
|
.underline { text-decoration: underline; }
|
|||
|
</style>
|
|||
|
|
|||
|
<script type="text/javascript">
|
|||
|
var DOCUMENTATION_OPTIONS = {
|
|||
|
URL_ROOT: './',
|
|||
|
VERSION: '0.4.1',
|
|||
|
COLLAPSE_INDEX: false,
|
|||
|
FILE_SUFFIX: '.html',
|
|||
|
HAS_SOURCE: true
|
|||
|
};
|
|||
|
</script>
|
|||
|
|
|||
|
<script type="text/x-mathjax-config">
|
|||
|
MathJax.Hub.Config({
|
|||
|
tex2jax: {
|
|||
|
inlineMath: [['$','$'], ['\\(','\\)']],
|
|||
|
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
|
|||
|
processEscapes: true
|
|||
|
},
|
|||
|
"HTML-CSS": {
|
|||
|
scale: "80"
|
|||
|
}
|
|||
|
});
|
|||
|
</script>
|
|||
|
<link rel="shortcut icon" href="_static/favicon.ico"/>
|
|||
|
<link rel="index" title="Index" href="genindex.html" />
|
|||
|
<link rel="search" title="Search" href="search.html" />
|
|||
|
<link rel="index" title="Computer Systems Fundamentals" href="index.html" />
|
|||
|
<link rel="next" title="4. System Call Interface" href="Syscall.html" />
|
|||
|
<link rel="prev" title="2. Processes and Multiprogramming" href="Multiprogramming.html" />
|
|||
|
|
|||
|
</head><body>
|
|||
|
|
|||
|
<nav class="navbar navbar-expand-md navbar-dark navbar-custom fixed-top">
|
|||
|
|
|||
|
<a class="navbar-brand py-0" href="index.html"><img src="_static/CSF-Logo-Square-Text.png" alt="OpenCSF Logo" height="40em" class="py-1 px-2 mb-0 align-center rounded-lg bg-white" /></a>
|
|||
|
<!-- Show a navbar toggler on mobile -->
|
|||
|
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#defaultNavbars" aria-controls="defaultNavbars" aria-expanded="false" aria-label="Toggle navigation">
|
|||
|
<span class="navbar-toggler-icon"></span>
|
|||
|
</button>
|
|||
|
<div class="collapse navbar-collapse" id="defaultNavbars">
|
|||
|
<ul class="navbar-nav mr-auto">
|
|||
|
<li class="nav-item dropdown">
|
|||
|
<a class="nav-link dropdown-toggle jmu-gold rounded" href="KernelMechanics.html#" id="navbarDropdownChapters" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Contents</a>
|
|||
|
<div class="dropdown-menu scrollable-menu" role="menu" aria-labelledby="navbarDropdownChapters">
|
|||
|
<a class="dropdown-item" tabindex="-1" href="KernelMechanics.html#"><b>Chapter 1</b></a>
|
|||
|
<a class="dropdown-item" href="IntroConcSysOverview.html"> 1.1. Introduction to Concurrent Systems</a>
|
|||
|
<a class="dropdown-item" href="SysAndModels.html"> 1.2. Systems and Models</a>
|
|||
|
<a class="dropdown-item" href="Themes.html"> 1.3. Themes and Guiding Principles</a>
|
|||
|
<a class="dropdown-item" href="Architectures.html"> 1.4. System Architectures</a>
|
|||
|
<a class="dropdown-item" href="StateModels.html"> 1.5. State Models in UML</a>
|
|||
|
<a class="dropdown-item" href="SequenceModels.html"> 1.6. Sequence Models in UML</a>
|
|||
|
<a class="dropdown-item" href="StateModelImplementation.html"> 1.7. Extended Example: State Model Implementation</a>
|
|||
|
<div class="dropdown-divider"></div>
|
|||
|
<a class="dropdown-item disabled"><b>Chapter 2</b></a>
|
|||
|
<a class="dropdown-item" href="ProcessesOverview.html"> 2.1. Processes and OS Basics</a>
|
|||
|
<a class="dropdown-item" href="Multiprogramming.html"> 2.2. Processes and Multiprogramming</a>
|
|||
|
<a class="dropdown-item" href="KernelMechanics.html"> 2.3. Kernel Mechanics</a>
|
|||
|
<a class="dropdown-item" href="Syscall.html"> 2.4. System Call Interface</a>
|
|||
|
<a class="dropdown-item" href="ProcessCycle.html"> 2.5. Process Life Cycle</a>
|
|||
|
<a class="dropdown-item" href="UnixFile.html"> 2.6. The UNIX File Abstraction</a>
|
|||
|
<a class="dropdown-item" href="EventsSignals.html"> 2.7. Events and Signals</a>
|
|||
|
<a class="dropdown-item" href="Extended2Processes.html"> 2.8. Extended Example: Listing Files with Processes</a>
|
|||
|
<div class="dropdown-divider"></div>
|
|||
|
<a class="dropdown-item disabled"><b>Chapter 3</b></a>
|
|||
|
<a class="dropdown-item" href="IPCOverview.html"> 3.1. Concurrency with IPC</a>
|
|||
|
<a class="dropdown-item" href="IPCModels.html"> 3.2. IPC Models</a>
|
|||
|
<a class="dropdown-item" href="Pipes.html"> 3.3. Pipes and FIFOs</a>
|
|||
|
<a class="dropdown-item" href="MMap.html"> 3.4. Shared Memory With Memory-mapped Files</a>
|
|||
|
<a class="dropdown-item" href="POSIXvSysV.html"> 3.5. POSIX vs. System V IPC</a>
|
|||
|
<a class="dropdown-item" href="MQueues.html"> 3.6. Message Passing With Message Queues</a>
|
|||
|
<a class="dropdown-item" href="ShMem.html"> 3.7. Shared Memory</a>
|
|||
|
<a class="dropdown-item" href="IPCSems.html"> 3.8. Semaphores</a>
|
|||
|
<a class="dropdown-item" href="Extended3Bash.html"> 3.9. Extended Example: Bash-lite: A Simple Command-line Shell</a>
|
|||
|
<div class="dropdown-divider"></div>
|
|||
|
<a class="dropdown-item disabled"><b>Chapter 4</b></a>
|
|||
|
<a class="dropdown-item" href="SocketsOverview.html"> 4.1. Networked Concurrency</a>
|
|||
|
<a class="dropdown-item" href="FiveLayer.html"> 4.2. The TCP/IP Internet Model</a>
|
|||
|
<a class="dropdown-item" href="NetApps.html"> 4.3. Network Applications and Protocols</a>
|
|||
|
<a class="dropdown-item" href="Sockets.html"> 4.4. The Socket Interface</a>
|
|||
|
<a class="dropdown-item" href="TCPSockets.html"> 4.5. TCP Socket Programming: HTTP</a>
|
|||
|
<a class="dropdown-item" href="UDPSockets.html"> 4.6. UDP Socket Programming: DNS</a>
|
|||
|
<a class="dropdown-item" href="AppBroadcast.html"> 4.7. Application-Layer Broadcasting: DHCP</a>
|
|||
|
<a class="dropdown-item" href="Extended4CGI.html"> 4.8. Extended Example: CGI Web Server</a>
|
|||
|
<div class="dropdown-divider"></div>
|
|||
|
<a class="dropdown-item disabled"><b>Chapter 5</b></a>
|
|||
|
<a class="dropdown-item" href="InternetOverview.html"> 5.1. The Internet and Connectivity</a>
|
|||
|
<a class="dropdown-item" href="AppLayer.html"> 5.2. Application Layer: Overlay Networks</a>
|
|||
|
<a class="dropdown-item" href="TransLayer.html"> 5.3. Transport Layer</a>
|
|||
|
<a class="dropdown-item" href="NetSec.html"> 5.4. Network Security Fundamentals</a>
|
|||
|
<a class="dropdown-item" href="NetLayer.html"> 5.5. Network Layer: IP</a>
|
|||
|
<a class="dropdown-item" href="LinkLayer.html"> 5.6. Link Layer</a>
|
|||
|
<a class="dropdown-item" href="Wireless.html"> 5.7. Wireless Connectivity: Wi-Fi, Bluetooth, and Zigbee</a>
|
|||
|
<a class="dropdown-item" href="Extended5DNS.html"> 5.8. Extended Example: DNS client</a>
|
|||
|
<div class="dropdown-divider"></div>
|
|||
|
<a class="dropdown-item disabled"><b>Chapter 6</b></a>
|
|||
|
<a class="dropdown-item" href="ThreadsOverview.html"> 6.1. Concurrency with Multithreading</a>
|
|||
|
<a class="dropdown-item" href="ProcVThreads.html"> 6.2. Processes vs. Threads</a>
|
|||
|
<a class="dropdown-item" href="RaceConditions.html"> 6.3. Race Conditions and Critical Sections</a>
|
|||
|
<a class="dropdown-item" href="POSIXThreads.html"> 6.4. POSIX Thread Library</a>
|
|||
|
<a class="dropdown-item" href="ThreadArgs.html"> 6.5. Thread Arguments and Return Values</a>
|
|||
|
<a class="dropdown-item" href="ImplicitThreads.html"> 6.6. Implicit Threading and Language-based Threads</a>
|
|||
|
<a class="dropdown-item" href="Extended6Input.html"> 6.7. Extended Example: Keyboard Input Listener</a>
|
|||
|
<a class="dropdown-item" href="Extended6Primes.html"> 6.8. Extended Example: Concurrent Prime Number Search</a>
|
|||
|
<div class="dropdown-divider"></div>
|
|||
|
<a class="dropdown-item disabled"><b>Chapter 7</b></a>
|
|||
|
<a class="dropdown-item" href="SynchOverview.html"> 7.1. Synchronization Primitives</a>
|
|||
|
<a class="dropdown-item" href="CritSect.html"> 7.2. Critical Sections and Peterson's Solution</a>
|
|||
|
<a class="dropdown-item" href="Locks.html"> 7.3. Locks</a>
|
|||
|
<a class="dropdown-item" href="Semaphores.html"> 7.4. Semaphores</a>
|
|||
|
<a class="dropdown-item" href="Barriers.html"> 7.5. Barriers</a>
|
|||
|
<a class="dropdown-item" href="Condvars.html"> 7.6. Condition Variables</a>
|
|||
|
<a class="dropdown-item" href="Deadlock.html"> 7.7. Deadlock</a>
|
|||
|
<a class="dropdown-item" href="Extended7Events.html"> 7.8. Extended Example: Event Log File</a>
|
|||
|
<div class="dropdown-divider"></div>
|
|||
|
<a class="dropdown-item disabled"><b>Chapter 8</b></a>
|
|||
|
<a class="dropdown-item" href="SynchProblemsOverview.html"> 8.1. Synchronization Patterns and Problems</a>
|
|||
|
<a class="dropdown-item" href="SynchDesign.html"> 8.2. Basic Synchronization Design Patterns</a>
|
|||
|
<a class="dropdown-item" href="ProdCons.html"> 8.3. Producer-Consumer Problem</a>
|
|||
|
<a class="dropdown-item" href="ReadWrite.html"> 8.4. Readers-Writers Problem</a>
|
|||
|
<a class="dropdown-item" href="DiningPhil.html"> 8.5. Dining Philosophers Problem and Deadlock</a>
|
|||
|
<a class="dropdown-item" href="CigSmokers.html"> 8.6. Cigarette Smokers Problem and the Limits of Semaphores and Locks</a>
|
|||
|
<a class="dropdown-item" href="Extended8ModExp.html"> 8.7. Extended Example: Parallel Modular Exponentiation</a>
|
|||
|
<div class="dropdown-divider"></div>
|
|||
|
<a class="dropdown-item disabled"><b>Chapter 9</b></a>
|
|||
|
<a class="dropdown-item" href="ParallelDistributedOverview.html"> 9.1. Parallel and Distributed Systems</a>
|
|||
|
<a class="dropdown-item" href="ParVConc.html"> 9.2. Parallelism vs. Concurrency</a>
|
|||
|
<a class="dropdown-item" href="ParallelDesign.html"> 9.3. Parallel Design Patterns</a>
|
|||
|
<a class="dropdown-item" href="Scaling.html"> 9.4. Limits of Parallelism and Scaling</a>
|
|||
|
<a class="dropdown-item" href="DistTiming.html"> 9.5. Timing in Distributed Environments</a>
|
|||
|
<a class="dropdown-item" href="DistDataStorage.html"> 9.6. Reliable Data Storage and Location</a>
|
|||
|
<a class="dropdown-item" href="DistConsensus.html"> 9.7. Consensus in Distributed Systems</a>
|
|||
|
<a class="dropdown-item" href="Extended9Blockchain.html"> 9.8. Extended Example: Blockchain Proof-of-Work</a>
|
|||
|
<div class="dropdown-divider"></div>
|
|||
|
<a class="dropdown-item disabled"><b>Appendix A</b></a>
|
|||
|
<a class="dropdown-item" href="CLangOverview.html"> A.1. C Language Reintroduction</a>
|
|||
|
<a class="dropdown-item" href="Debugging.html"> A.2. Documentation and Debugging</a>
|
|||
|
<a class="dropdown-item" href="BasicTypes.html"> A.3. Basic Types and Pointers</a>
|
|||
|
<a class="dropdown-item" href="Arrays.html"> A.4. Arrays, Structs, Enums, and Type Definitions</a>
|
|||
|
<a class="dropdown-item" href="Functions.html"> A.5. Functions and Scope</a>
|
|||
|
<a class="dropdown-item" href="Pointers.html"> A.6. Pointers and Dynamic Allocation</a>
|
|||
|
<a class="dropdown-item" href="Strings.html"> A.7. Strings</a>
|
|||
|
<a class="dropdown-item" href="FunctionPointers.html"> A.8. Function Pointers</a>
|
|||
|
<a class="dropdown-item" href="Files.html"> A.9. Files</a>
|
|||
|
</div>
|
|||
|
</li>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
|
|||
|
<ul class="navbar-nav flex-row ml-md-auto d-none d-md-flex">
|
|||
|
<li class="nav-item"><a class="nav-link jmu-gold" href="https://w3.cs.jmu.edu/kirkpams/OpenCSF/Books/csf/source/KernelMechanics.rst"
|
|||
|
target="_blank" rel="nofollow">Show Source</a></li>
|
|||
|
|
|||
|
</ul>
|
|||
|
</nav>
|
|||
|
|
|||
|
|
|||
|
<div class="container center">
|
|||
|
«  <a id="prevmod" href="Multiprogramming.html">2.2. Processes and Multiprogramming</a>
|
|||
|
  ::  
|
|||
|
<a class="uplink" href="index.html">Contents</a>
|
|||
|
  ::  
|
|||
|
<a id="nextmod" href="Syscall.html">2.4. System Call Interface</a>  »
|
|||
|
|
|||
|
</div>
|
|||
|
<br />
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<script type="text/javascript" src="_static/js/jquery-2.1.4.min.js"></script>
|
|||
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
|
|||
|
<script type="text/javascript" src="_static/js/jquery-1.11.4-ui.min.js"></script>
|
|||
|
<script type="text/javascript" src="_static/js/forge-0.7.0.min.js"></script>
|
|||
|
<script type="text/javascript" src="../../../JSAV/lib/jquery.transit.js"></script>
|
|||
|
<script type="text/javascript" src="../../../JSAV/lib/raphael.js"></script>
|
|||
|
<script type="text/javascript" src="../../../JSAV/build/JSAV-min.js"></script>
|
|||
|
<script type="text/javascript" src="_static/js/config.js"></script>
|
|||
|
<script type="text/javascript" src="../../../lib/odsaUtils-min.js"></script>
|
|||
|
<script type="text/javascript" src="../../../lib/odsaMOD-min.js"></script>
|
|||
|
<script type="text/javascript" src="_static/js/d3-4.13.0.min.js"></script>
|
|||
|
<script type="text/javascript" src="_static/js/d3-selection-multi.v1.min.js"></script>
|
|||
|
<script type="text/javascript" src="../../../lib/dataStructures.js"></script>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="container">
|
|||
|
|
|||
|
<script>ODSA.SETTINGS.DISP_MOD_COMP = true;ODSA.SETTINGS.MODULE_NAME = "KernelMechanics";ODSA.SETTINGS.MODULE_LONG_NAME = "Kernel Mechanics";ODSA.SETTINGS.MODULE_CHAPTER = "Processes and OS Basics"; ODSA.SETTINGS.BUILD_DATE = "2021-06-01 15:31:50"; ODSA.SETTINGS.BUILD_CMAP = false;JSAV_OPTIONS['lang']='en';JSAV_EXERCISE_OPTIONS['code']='java';</script><div class="section" id="kernel-mechanics">
|
|||
|
<h1>2.3. Kernel Mechanics<a class="headerlink" href="KernelMechanics.html#kernel-mechanics" title="Permalink to this headline">¶</a></h1>
|
|||
|
<p>The <a class="reference internal" href="Glossary.html#term-kernel"><span class="xref std std-term">kernel</span></a> is a program that runs with full access privileges to the entire computer. The
|
|||
|
kernel controls access to all shared system resources, including physical memory, the file system,
|
|||
|
and I/O devices. The kernel is also responsible for handling all exceptional system and software
|
|||
|
events, such as power disruption or the addition of new <em>plug-and-play</em> peripheral components. To be
|
|||
|
precise, the kernel is primarily responsible for two functions. It acts as a <a class="reference internal" href="Glossary.html#term-resource-manager"><span class="xref std std-term">resource manager</span></a>,
|
|||
|
providing access to shared system hardware resources as needed. The kernel also acts as a
|
|||
|
<a class="reference internal" href="Glossary.html#term-control-program"><span class="xref std std-term">control program</span></a>, handling errors and access violations in a safe manner.</p>
|
|||
|
<p>The origins of the kernel date back to the earliest days of mainframe computing, when it was known
|
|||
|
as the <a class="reference internal" href="Glossary.html#term-monitor"><span class="xref std std-term">monitor</span></a>. The monitor was a collection of software routines for standard operation
|
|||
|
that got included with every program when it was run; these included routines for clearing out
|
|||
|
memory and loading stored data. The term was later changed to <a class="reference internal" href="Glossary.html#term-resident-monitor"><span class="xref std std-term">resident monitor</span></a> to reflect
|
|||
|
the fact that this code was always present (resident) in memory.</p>
|
|||
|
<p>In the family of x86 architectures, the CPU’s operating mode is stored as a 2-bit value known as the
|
|||
|
<a class="reference internal" href="Glossary.html#term-current-privilege-level"><span class="xref std std-term">current privilege level</span></a> (CPL), which is also called a <a class="reference internal" href="Glossary.html#term-ring"><span class="xref std std-term">ring</span></a>. Although these
|
|||
|
architectures make it possible to have four rings, only two are used in practice. When the system is
|
|||
|
in ring 3 (<a class="reference internal" href="Glossary.html#term-user-mode"><span class="xref std std-term">user mode</span></a>), the set of instructions that are allowed is restricted and no
|
|||
|
instruction can access any part of memory owned by the kernel.</p>
|
|||
|
<p>In ring 0 (<a class="reference internal" href="Glossary.html#term-kernel-mode"><span class="xref std std-term">kernel mode</span></a>), all valid memory addresses can be accessed and an additional set of
|
|||
|
<a class="reference internal" href="Glossary.html#term-privileged-instruction"><span class="xref std std-term">privileged instructions</span></a> can be performed. Examples of privileged
|
|||
|
instructions include <code class="docutils literal notranslate"><span class="pre">hlt</span></code>, which halts the CPU, and <code class="docutils literal notranslate"><span class="pre">invd</span></code>, which can be used to invalidate the
|
|||
|
CPU cache. In addition, some normal instructions behave differently in ring 0 than they do in ring
|
|||
|
3; <code class="docutils literal notranslate"><span class="pre">popf</span></code> (pops a word from the stack into the status flag register) is one example, as some
|
|||
|
status bits are not updated in ring 3.</p>
|
|||
|
<div class="topic border border-dark rounded-lg bg-light px-2 mb-3">
|
|||
|
<div class="figure align-left">
|
|||
|
<a class="reference internal image-reference" href="_images/CSF-Images-Note.png"><img alt="Decorative note icon" src="_images/CSF-Images-Note.png" style="width: 100%;" /></a>
|
|||
|
</div>
|
|||
|
<p class="topic-title first pt-2 mb-1">Note</p><hr class="mt-1" />
|
|||
|
<p>To further illustrate the distinction between the kernel and the common usage
|
|||
|
of the term OS, consider the notion of OS versions or distributions. Windows
|
|||
|
users are probably familiar with names such as XP, Vista, or Windows 10.
|
|||
|
Similarly, Mac users might distinguish macOS Sierra or OS X El Capitan, just as
|
|||
|
Linux users may talk about Linux Mint, Ubuntu, or Red Hat Linux.</p>
|
|||
|
<p>In all three of these cases, all of the versions and distributions share a
|
|||
|
common kernel. In the case of Windows, the kernel is known as the NT kernel,
|
|||
|
which was first released in 1993. macOS is the most recent name of the Mac OS X
|
|||
|
kernel, which was first released in 2001. All distributions of Linux use the
|
|||
|
Linux kernel, which was first released in 1991. The various OS distributions
|
|||
|
are primarily distinguished by the non-kernel programs and services that are
|
|||
|
included. Although the kernel may contain some updates, the internal structure
|
|||
|
and services have remained moderately consistent.</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="kernel-memory-structure-and-protections">
|
|||
|
<h2>2.3.1. Kernel Memory Structure and Protections<a class="headerlink" href="KernelMechanics.html#kernel-memory-structure-and-protections" title="Permalink to this headline">¶</a></h2>
|
|||
|
<div class="figure mb-2 align-right" id="id2" style="width: 30%">
|
|||
|
<span id="prockernelmode"></span><a class="reference internal image-reference" href="_images/CSF-Images.2.2.png"><img class="p-3 mb-2 align-center border border-dark rounded-lg" alt="Application code running in user-mode cannot access any part of kernel memory" src="_images/CSF-Images.2.2.png" style="width: 90%;" /></a>
|
|||
|
<p class="caption align-center px-3"><span class="caption-text"> Figure 2.3.2: Application code running in user-mode cannot access any part of kernel memory</span></p>
|
|||
|
</div>
|
|||
|
<p>The kernel exists as a protected region of virtual memory within the context of every process. Just
|
|||
|
like a normal user-mode program, the kernel contains a code segment, global data, and a heap for
|
|||
|
dynamic memory allocation. Rather than having a single stack, however, the kernel contains many
|
|||
|
stacks; for each user-mode process, the kernel contains at least one stack.</p>
|
|||
|
<p>In general, the kernel interacts with its memory regions the same way as normal programs interact
|
|||
|
with theirs. The CPU uses the <code class="docutils literal notranslate"><span class="pre">%rip</span></code> register to load the next instruction from the code segment.
|
|||
|
Dynamically allocated data structures are stored on the heap and local variables are stored on the
|
|||
|
stack that is currently in use.</p>
|
|||
|
<p>Since the kernel contains information about all processes and system resources, user-mode programs
|
|||
|
must be prevented from tampering with it. For instance, you would not want a faulty program to
|
|||
|
reformat your hard drive or shut off power at random times; only the kernel should be able to
|
|||
|
perform these actions. To prevent tampering from other programs, the kernel configures the CPU to
|
|||
|
restrict access to the portions of physical memory that are storing the kernel’s virtual memory
|
|||
|
contents. As a result, if an instruction tries to access a memory location within the kernel while
|
|||
|
the CPU is set to user mode, the CPU itself will detect this invalid access and trigger an exception.</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="the-boot-procedure">
|
|||
|
<h2>2.3.2. The Boot Procedure<a class="headerlink" href="KernelMechanics.html#the-boot-procedure" title="Permalink to this headline">¶</a></h2>
|
|||
|
<p>The kernel is loaded and begins executing as a part of the <a class="reference internal" href="Glossary.html#term-boot-sequence"><span class="xref std std-term">boot sequence</span></a>. When a computer is
|
|||
|
first turned on, the CPU begins to execute <a class="reference internal" href="Glossary.html#term-firmware"><span class="xref std std-term">firmware</span></a> code routines stored in non-volatile
|
|||
|
hardware storage. Examples of firmware include BIOS and UEFI. These firmware routines locate
|
|||
|
OS-specific <a class="reference internal" href="Glossary.html#term-boot-loader"><span class="xref std std-term">boot loaders</span></a>, such as GRUB (used by Linux), BOOTMGR (Windows), or
|
|||
|
BootX (macOS). If a system is configured to support <a class="reference internal" href="Glossary.html#term-dual-booting"><span class="xref std std-term">dual booting</span></a> (the ability to choose from
|
|||
|
more than one OS), it is the boot loader that provides this capability.</p>
|
|||
|
<p>Once the boot loader determines the kernel to load, it locates a file containing the kernel on a
|
|||
|
storage device. In many systems, the kernel is stored in a compressed format (such as a <code class="docutils literal notranslate"><span class="pre">tar</span></code>
|
|||
|
archive file compressed with gzip), so the boot loader must decompress it when loading it into
|
|||
|
memory. Once the kernel has been loaded, the boot loader calls the kernel’s <code class="docutils literal notranslate"><span class="pre">main()</span></code> entry point. <a class="footnote-reference" href="KernelMechanics.html#f9" id="id1">[1]</a>
|
|||
|
Calling the kernel’s <code class="docutils literal notranslate"><span class="pre">main()</span></code> is the last action performed by the boot loader, and the
|
|||
|
kernel then takes over full control of the system. The kernel begins by initializing its own data
|
|||
|
structures for managing the system and launching its initial system services as separate processes.
|
|||
|
These system services include the service that allows users to login to the computer.</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="kernel-invocation">
|
|||
|
<h2>2.3.3. Kernel Invocation<a class="headerlink" href="KernelMechanics.html#kernel-invocation" title="Permalink to this headline">¶</a></h2>
|
|||
|
<p>The kernel can be invoked in two ways. First, the user-mode program may initiate a <a class="reference internal" href="Glossary.html#term-system-call"><span class="xref std std-term">system call</span></a>,
|
|||
|
which is a request for a specific service. As an example, calling the <code class="docutils literal notranslate"><span class="pre">printf()</span></code> function will
|
|||
|
eventually lead to a system call that makes a request for the kernel to write to a particular file,
|
|||
|
typically <code class="docutils literal notranslate"><span class="pre">stdout</span></code>. System calls invoke the kernel by executing a <a class="reference internal" href="Glossary.html#term-trap-instruction"><span class="xref std std-term">trap instruction</span></a>. On the
|
|||
|
x86 family of architectures, this instruction is known as <code class="docutils literal notranslate"><span class="pre">syscall</span></code>.</p>
|
|||
|
<p>The second way that the kernel can be invoked is in response to either an <a class="reference internal" href="Glossary.html#term-interrupt"><span class="xref std std-term">interrupt</span></a> or an
|
|||
|
<a class="reference internal" href="Glossary.html#term-exception"><span class="xref std std-term">exception</span></a>. An interrupt is an asynchronous notification from a hardware component that
|
|||
|
indicates service is needed. For example, every time you press a key on the keyboard or click a
|
|||
|
mouse button, that hardware device triggers an interrupt to make the kernel aware of the event. An
|
|||
|
exception (sometimes called a software interrupt) is a synchronous notification of a problem with
|
|||
|
the software. Exceptions include faults and aborts, such as segmentation faults, dividing by zero,
|
|||
|
or illegal memory values that may be the result of hardware failures.</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="mode-switches-and-privileged-instructions">
|
|||
|
<h2>2.3.4. Mode Switches and Privileged Instructions<a class="headerlink" href="KernelMechanics.html#mode-switches-and-privileged-instructions" title="Permalink to this headline">¶</a></h2>
|
|||
|
<p>A <a class="reference internal" href="Glossary.html#term-mode-switch"><span class="xref std std-term">mode switch</span></a> refers to a change in the CPL. System calls and interrupts both trigger a mode
|
|||
|
switch from ring 3 to ring 0. At the same time that the CPL changes, the <code class="docutils literal notranslate"><span class="pre">%rip</span></code> register is
|
|||
|
updated to begin reading from the kernel’s code segment. The address loaded into the <code class="docutils literal notranslate"><span class="pre">%rip</span></code> is
|
|||
|
determined by a data structure that the kernel sets up during the boot process. In addition to
|
|||
|
updating the CPL and the <code class="docutils literal notranslate"><span class="pre">%rip</span></code>, the CPU makes a copy of the user-mode program status (such as its
|
|||
|
<code class="docutils literal notranslate"><span class="pre">%rip</span></code> value).</p>
|
|||
|
<p>One important aspect to note about a mode switch is how quickly it occurs. Specifically, mode
|
|||
|
switches occur within a single execution of the von Neumann instruction cycle. Once the <code class="docutils literal notranslate"><span class="pre">%rip</span></code> has
|
|||
|
been updated at the end of one instruction’s cycle, the CPU checks if an interrupt needs processing.
|
|||
|
If there is a pending interrupt, the CPU triggers a mode switch before fetching the next
|
|||
|
instruction; if not, then the next instruction is fetched.</p>
|
|||
|
<p>After the system call or interrupt has been processed, the kernel forces a mode switch by executing
|
|||
|
the <code class="docutils literal notranslate"><span class="pre">iret</span></code> instruction. Just as <code class="docutils literal notranslate"><span class="pre">ret</span></code> updated the <code class="docutils literal notranslate"><span class="pre">%rip</span></code> to return to the portion of code that
|
|||
|
called a function, <code class="docutils literal notranslate"><span class="pre">iret</span></code> acts as a return from an interrupt to get back to the appropriate
|
|||
|
location in the user-mode program. The <code class="docutils literal notranslate"><span class="pre">iret</span></code> instruction restores the user-mode program’s status
|
|||
|
that it had stored previously and lowers the CPL back to ring 3.</p>
|
|||
|
<table class="docutils footnote" frame="void" id="f9" rules="none">
|
|||
|
<colgroup><col class="label" /><col /></colgroup>
|
|||
|
<tbody valign="top">
|
|||
|
<tr><td class="label"><a class="fn-backref" href="KernelMechanics.html#id1">[1]</a></td><td>More recent x86 processors have also added another bit to the CPL that is used by certain
|
|||
|
types of virtualization technologies. This additional bit is used to distinguish between “guest
|
|||
|
mode” and “host mode.” In these types of systems, multiple guest <em>virtual machines</em>
|
|||
|
may be running as “guests” while a single <em>hypervisor</em> manages them as the “host.”
|
|||
|
In these types of environments, ring 0 refers to kernel mode within a guest, whereas the hypervisor
|
|||
|
operates in “ring -1,” which is kernel mode within the host.</td></tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<div
|
|||
|
id="KernelMechSumm"
|
|||
|
class="embedContainer"
|
|||
|
data-exer-name="KernelMechSumm"
|
|||
|
data-long-name="Kernel mechanics questions"
|
|||
|
data-short-name="KernelMechSumm"
|
|||
|
data-frame-src="../../../Exercises/Processes/KernelMechSumm.html?selfLoggingEnabled=false&localMode=true&module=KernelMechanics&JXOP-debug=true&JOP-lang=en&JXOP-code=java"
|
|||
|
data-frame-width="950"
|
|||
|
data-frame-height="550"
|
|||
|
data-external="false"
|
|||
|
data-points="1.0"
|
|||
|
data-required="True"
|
|||
|
data-showhide="show"
|
|||
|
data-threshold="5"
|
|||
|
data-type="ka"
|
|||
|
data-exer-id="">
|
|||
|
|
|||
|
<div class="center">
|
|||
|
<div id="KernelMechSumm_iframe"></div>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="container">
|
|||
|
|
|||
|
<div class="mt-4 container center">
|
|||
|
«  <a id="prevmod1" href="Multiprogramming.html">2.2. Processes and Multiprogramming</a>
|
|||
|
  ::  
|
|||
|
<a class="uplink" href="index.html">Contents</a>
|
|||
|
  ::  
|
|||
|
<a id="nextmod1" href="Syscall.html">2.4. System Call Interface</a>  »
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
<br />
|
|||
|
|
|||
|
<div class="row jmu-dark-purple-bg">
|
|||
|
<div class="col-md-12">
|
|||
|
<center>
|
|||
|
<a id="contact_us" class="btn button-link-no-blue jmu-gold" rel="nofollow" href="mailto:webmaster@opencsf.org" role="button">Contact Us</a>
|
|||
|
<a id="license" class="btn button-link-no-blue jmu-gold" rel="nofollow" href="https://w3.cs.jmu.edu/kirkpams/OpenCSF/lib/license.html" target="_blank">License</a>
|
|||
|
</center>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<script src="_static/js/popper.js-1.14.7-min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
|
|||
|
<script src="_static/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
|
|||
|
</body>
|
|||
|
</html>
|