off in your annual subscription, visit brilliant.org/coredumpt
brilliant.org/coredumpt
or scan the QR code on screen or click
on the link in the description below.
And now back to the video.
You might think we need to modify the
memory itself. But actually memory
remains untouched in this design. While
a process runs, the memory unit sees
only a stream of memory addresses coming
from the CPU through this set of
parallel wires called the address bus.
The main memory does not know how those
addresses are generated or what they are
for. So handling this process directly
in memory is not the best way. Now, I
don't want to draw a million wires, so
I'll use the simpler architecture I
usually show here in the channel.
There's little to no use for general
purpose components. In this context,
we'll use two special registers to
enforce memory boundaries. The base
register points to the smallest legal
physical memory address of the process.
The limit register defines the size of
the addressable range for that process.
With these registers, we can check
whether any address a process tries to
access falls within its own address space.
space.
In code terms, we're checking whether
the address is greater than or equal to
the base and lower than the sum of the
base plus the limit. It's the same logic
the OS would follow if the check were
done in software, but here we're
translating it into hardware.
Note that the sum of the base and limit
gives the upper bound of the address
space. But this bound is exclusive,
which is why we check that the address
is less than not equal to the upper bound.
bound.
The good news is that calculating this
upper bound is as simple as adding the
contents of the base and limit registers
using a binary adder which we covered in
a previous episode.
Remember we want to validate the address
being passed on every single memory
operation. And since every address
passes through the address bus, we can
intercept the address right there. Now
that we have all the values we need, we
can move on to the comparisons.
To compare the address with the
boundaries, we'll use binary
comparators, which is the most
straightforward method. If you're new to
this channel, don't let this circuit
intimidate you. It's just a combination
of logic gates arranged to produce a
specific result based on its inputs.
In this case, the comparator receives
two binary numbers, A and B, and
produces three outputs.
Only one of these outputs is active at a
time, depending on the inputs. One
output activates if A equals B, another
if A is less than B, and another if A is
greater than B.
We'll abstract this logic into a simple
component called a binary comparator.
We'll use one comparator to check
whether the address is greater than or
equal to the base and another to check
whether it's less than the upper bound.
But remember, both conditions must be
true for the address to be valid. So
we'll combine the outputs using an andgate.
And that's it. The wire at the end of
our circuit will only be active if the
address passed to memory falls within
the address space defined by the base
and limit registers.
But how can a single wire prevent
illegal memory access?
Let's quickly recall how memory works.
Sending an address through the address
bus doesn't trigger a memory operation
on its own. To actually perform a memory
operation, we must specify whether we
want to read from or write to that
address. This is done by activating
either the read enable or write enable signal.
signal.
When read enable is active, memory
places the contents of the address onto
the datab bus. When write enable is
active, memory overwrites the contents
at the specified address using the data
provided on the data bus.
So the simplest way to block illegal
memory access is to use the output of
our address validator circuit to gate
the read and write signals.
That way if an instruction attempts to
access an address outside the process's
address space, the hardware will block
the read or write signal from reaching
the memory module.
In other words, memory will only receive
the read or write enable signal if the
address is within the bounds of the
current processes address space.
Another good idea is to add a few extra
logic gates to detect when a process
attempts to access an illegal address.
That way, the circuitry not only blocks
the memory operation, but also triggers
a hardware signal to the CPU that
immediately interrupts the running
process and invokes the operating
system, which then handles the
violation, most likely by terminating
the process.
On Unix- like systems, when this signal
is caught by the operating system, it
terminates the process and displays the
error message, segmentation fault.
Meaning the process tried to access
memory it wasn't allowed to.
Now, something that should be very clear
at this point is that the operating
system doesn't need to execute any
additional instructions to make this
circuitry work. Because of the way
everything is wired, the circuit
automatically intercepts any memory
operation regardless of its purpose and
immediately performs the validation.
As I'll show in a moment, the only
responsibility of the operating system
is to set the correct values in the base
and limit registers before dispatching
the CPU to the user process.
All this circuitry is of course embedded
inside the CPU.
Just keep in mind that for any of this
to work as expected, the CPU must
support operational modes. These memory
management registers like the base and
limit registers must be implemented as
privileged registers which can only be
modified by the operating system.
This becomes another responsibility of
the operating system during a context
switch. When the CPU is reassigned from
one process to another, the OS not only
captures the state of the interrupted
process and restores the state for the
new process, but also updates the memory
bounds, the base and limit registers to
match the address space of the incoming process.
process.
Once everything is properly set up, the
OS switches the CPU into user mode,
allowing the user process to run. But in
this mode, the CPU cannot modify the
privileged registers. So the process is
limited to using only the memory that
the operating system has explicitly
allocated to it.
And that's the basic idea behind how the
operating system with support from the
hardware prevents user processes from
accessing each other's memory.
We're not done with memory just yet. So
far we've implemented protection for
processes running concurrently. But what
happens when multiple processes are
running and there's no more memory
available for new ones? That's a topic
for a future episode where we'll explore
paging, a memory management technique
that allows a computer to use more
memory than is physically available. So,
make sure to subscribe. You won't want
Click on any text or timestamp to jump to that moment in the video
Share:
Most transcripts ready in under 5 seconds
One-Click Copy125+ LanguagesSearch ContentJump to Timestamps
Paste YouTube URL
Enter any YouTube video link to get the full transcript
Transcript Extraction Form
Most transcripts ready in under 5 seconds
Get Our Chrome Extension
Get transcripts instantly without leaving YouTube. Install our Chrome extension for one-click access to any video's transcript directly on the watch page.