Video
Đăng bởi Quản trị viên OpenER ·
Tác giả giữ toàn bộ bản quyền (All Rights Reserved) — không phải giấy phép mở. Bạn vẫn xem và học miễn phí trên OpenER; muốn tái sử dụng cần xin phép tác giả.
PROFESSOR
Hello. Welcome back to the virtual memory and operating system module. So far we have seen what are some of the basic principles of operation of the operating system.
We have also gotten up to a point where we booted an operating system from an external flash ROM device into the main memory and loaded the operating system into our processor.
Now what? We would like to go ahead and start executing some programs. Okay. One thing to keep in mind, this module is a bit narrative.
It may be worthwhile rewatching it after you go through the entire virtual memory and I/O segments because it may make more sense at that point.
But for now, I'm laying out a bunch of principles that need to be done, many of the functions that need to be performed by the operating system, so hang in with me.
A lot of things are going to get clearer a bit later. So as we said, we booted the operating system and we would like to launch applications. So how do we launch an application?
Remember where we left off. We loaded some kind of a shell and that shell is just waiting for the user input.
That input may be a set of keyboard strokes that are going to launch the application.
First thing, applications are generally called processes in most operating systems. Processes have separate memory — work in separate memory locations as opposed to threads.
Threads are always in the shared memory. Both threads and processes run pseudo simultaneously. What do I mean by that?
From a user perspective, from a user's point of view, threads and processes look like they're running simultaneously, but underneath we generally will have much, much lower number of processor cores than the number of processes or threads that we are running.
So these processes are going to be sharing the same core. How is that going to happen? Well, the operating system is going to manage that.
It is going to run a process for a little bit, typically 10 milliseconds or so, on a processor core.
And then we'll swap it out and put in a new process to run on it.
From a user's perspective, really, humans often cannot see that things like this were happening underneath as long as the application is running smoothly for us, from a user's point of view.
So applications are started by generally a shell that calls an OS routine by using a syscall in Linux.
This process really depends on the OS. If it is a Linux, Linux uses something that is called a fork to create a new process and execfi, execute file command, loads the application.
So what happens then?
We load the executable file, the binary file, from the disk by using some of the file system service functions and puts the instructions and the data in their appropriate locations in the memory.
So those are be our text and data sections. And then prepares the stack and the heap. That then sets argc and argv, and then jumps to the main.
From there, the program counter picks up from the main, executes the instructions, and shell just waits for the program to complete.
So it waits for this main to return or in Linux language, waits for it to join.
And the important thing here is there are many processes running simultaneously and it's really important to manage them. We need to make sure that they don't run on top of each other.
So that's why we have a supervisor mode. So if something goes wrong with the application, we really don't want it to crash the entire machine and start scribbling over the disk.
It would not be good. It's terrible. We also have to have ways to stop the malware from doing the same things to us.
So there is OS that manages a bunch of applications from doing something wrong — prevents them from doing something wrong.
So it puts constraints on the applications on where they can run into, which part of the memory they can access, and which devices can they access.
In order to do that, operating system has a higher mode or more privileged mode of operation that is called the supervisor mode.
And the supervisor mode is something relatively straightforward that is supported by the hardware.
And the hardware will find out that supervisor mode is entered by just flipping a few bits in the status register.
So a general process has access only to a subset of instructions in the processor, and those instructions are the ones that we have seen so far.
Supervisor mode has access to a few more instructions. These instructions are nothing particularly fancy. They're typically setting some CSRs, some instructions that are associated with manipulating CSRs.
And then the supervisor mode also has access to the special set of CSR registers.
So this supervisory mode is kind of a super user mode or SU mode in Unix.
Now keep in mind that most of the OS applications are running in a regular user mode. Very rarely we enter the supervisor mode.
If there is an error in the supervisor mode, it is often catastrophic.
That is what causes the blue screen of death in Windows or scribbling over our disk drives.
So it's a good idea not to too often use supervisor mode because errors there are fatal, generally.
Now the next thing that is worth mentioning are the syscalls. We have encountered syscalls before, but now they start making a little bit more sense.
So when we want OS to do something, for example, to read a file, launch a new process, malloc, send data to the network or to the wired network or to Wi-Fi, what do we do?
It's really inconvenient to write our own low level routine that is going to take a frame of data and pass it on to the Wi-Fi modem.
I mean, it's totally doable, but we generally don't want to do that. Also it is not practical to do that because multiple processes may end up contending for the same shared resource.
What we do instead we make a syscall to the operating system and operating system is going to manage that for us.
So it's much easier to make a syscall than to write this individual routine that accesses a device.
The way how we make a syscall, a system call, is by placing some arguments in the registers, generally where the information can be found and placed and then create something that's called the software interrupt.
There is a special instruction that does this interrupt.
And at that point the operating system takes over and performs the required operation, whether it's printing on a printer or sending a block of data via Wi-Fi, and then returns to the user mode.
So this way the operating system can manage multiple processes who are trying to do similar things or the same things. Multiple processes can print or access network and so on.
Operating system make sure that there are no collisions between them and that they're ordered properly.
One really important mechanism that we have hinted to is the mechanism of interrupts and exceptions.
We generally need to transition to the supervisor mode when something important happens. And these important things can be either external or internal.
External ones are so-called interrupts, that there is something that happened in the outside world that we should know. While we are doing this, somebody might have rang the doorbell.
We should check what is going on in there.
The other ones are exceptions.
The other class of events that we should care about are exceptions, and exceptions are something internal, something done by running the program by executing a wrong instruction, trying to divide by 0, for example, or read from a memory location that should not exist.
So those are two differences between interrupts and exceptions. Also this puts ecalls and ebreaks in their perspective space.
Ecall triggers an exception to the higher privilege and ebreak triggers an exception within the current privilege. So ecall is the way how we communicate to the operating system.
This is what we basically use to influence syscalls. So we will set a set of arguments to our ecall and that is what is going to cause a system call in Linux.
On the other hand, ebreak transfers control within the current level of privilege. Okay.
A little bit more about the terminology that is used in 61C, different places or different cores may use a different terminology. So one more time.
Interrupt is something that is caused by an external event, that event that is completely external to the running of the current program.
It may be a key press or disk I/O event or arrival of a Wi-Fi frame that is completely asynchronous to the current program.
And we generally don't have terrific urgency in handling an interrupt like that.
There are some higher priority interrupts, but generally in Linux context we don't have to immediately handle that interrupt we can wait to finish what we are doing.
If there is a doorbell rang, I can finish this current sentence and then I can pause it and then I can go answer the door — only perhaps if I were not recording this.
On the other hand, exceptions are usually internal events, something happened to the program. And usually it's something pretty terrible.
There is a memory error, bus error, legal instruction, and that is going to raise the exception. That usually has to be treated immediately.
So we need to drop immediately everything we are doing there is a fire alarm or something like that and go figure out what happened.
In both cases, we are handling this in a very similar way. The action of servicing an interrupt or an exception is called a trap.
So generally what we do in order to service either an interrupt or the exception, we jump to an interrupt or trap handler.
That's a piece of code that resides in memory that is designated for handling particular interrupts or traps.
If you don't have an interrupt, handler, or a trap handler, we will either ignore that event or will crash.
Trap handling is very similar to what we have seen in, say, function calls, but generally needs to be more precise.
So trap handling involves altering the regular execution flow. There is our main program that is running instructions here, instruction i minus 1, instruction i, and instruction i plus 1.
Interrupt or exception happens.
We generally would like to finish all the instructions that precede this trap, and then we jump over to the handler.
We execute the Handler and return back to the point where we stopped.
So this is something really important and we are going to see examples of some of these handlers later when we introduce I/O devices.
These are going to be essentially pieces of code that are responding to unknown external event.
So there is another important concept here that goes along with all of this. This is something that is related to precise traps.
Traps often raised by the exceptions have to be dealt with immediately.
So trap handlers view of machine code state is that every instruction that precedes that trap has been completed has been executed and none of the instructions that follow have started their execution.
So that sounds relatively straightforward, although it's not that straightforward in super scalar processors where we have multiple instructions in flight.
So in general here, the handler can return from an interrupt by restoring user registers and jumping back to the interrupted instruction.
What does that mean?
Every time we have an interrupt handler we have to restore complete state of the machine, all the registers like we do in functional calls, but in this case we generally will save them all, both saved and temporary ones.
We'll handle the trap and then we'll restore the state of the machine like the interrupt never happened and our program is going to continue executing.
This is a bit tricky in super scalar processors because, remember, we have to precisely insert, convert, some of these instructions into nops. We know how to do that.
We have done that when we were dealing with hazards.
So essentially at the point, if there is an instruction that raises an exception, all the instructions that follow that instruction are going to be converted to nops.
We are going to, at that point, transfer the control to the trap handler.
Trap handler is going to save the state of the registers, do whatever it needs to do, restore the registers, and return and re-execute the instruction that we have converted to knob.
Okay, a little bit more of a view how these things can happen and they can happen in different stages. For example, here are a few exceptions that may happen in a five stage pipeline.
Perhaps we may end up addressing wrong instruction space so we can have a PC address exception, or we may be trying to execute an illegal instruction by having a wrong opcode.
We will decode that and raise an exception. By the way, this is a way how we can execute some instructions that we don't have supported by our hardware.
So for example, if we don't have an instruction for multiplication, we can handle it with the trap handler for that.
We'll detect that it would like to do the multiplication and we do it in software. It's slower, but it doesn't crash the machine.
We can divide by zero and we can raise an exception with that or we may address the wrong part of the memory space by addressing some part of a memory where we should not have our data.
That all is going to cause exceptions. All right. So in the relatively brief, trap handling is very similar to pipeline hazards followed by function calls.
So when we are handling a trap, we complete the execution of the instruction that precedes the exception.
We flush all the instructions that we have currently in the pipeline by converting them to nops, and there is an optional step.
If we would like to know why the exception has been raised, we usually store a cause in a control and status register so we know what has happened there.
Then we transfer execution to the trap handler. Trap handler saves all the registers.
When it finishes, restores the registers, and if appropriate, if it has not decided to terminate the program, returns to the original program and re-executes the next instruction that we had in flight.
By the way, since we understand a lot of this stuff now, this is also the way how we support multiple processes or multiprogramming, how do we concurrently execute multiple programs.
So all this is going to help us run multiple applications at the same time and does that by either scheduling them to different cores, if you have them.
But in general, in a single core machine or where there is a number of cores, the number of cores is smaller than the number of processes, it is going to schedule them to be executed on the same core.
And this is something that happens very quickly.
So the way how this is done, it runs a piece of a code, few millions of instructions, generally of the order of millions of instructions, and then switches context.
What does that context switch mean? It goes to service the next program, runs a few millions of instructions of the next program, and switches back.
So every time we jump into a program in this multiprogramming mode, the operating system sets a timer. And timers are, again, accessed through control and status registers.
Sets a timer like a kitchen timer for 10 milliseconds and goes into executing one program.
In a kitchen, well, we were probably not doing that at 10 millisecond intervals, but we would put something in the oven.
If the oven is the shared resource, we would keep something in the oven for 10 minutes.
And then we'll take it out of the oven after our kitchen timer tells us to do that and put something else in the oven.
We will set the kitchen timer again and take it out and keep moving multiple things through that oven.
This works in a very similar way in the processors except in a very tiny time scale, generally 10 milliseconds. So every time the timer goes off, what we do?
We save the state of the program that we were running, so we save all of its state, we load the state of a new program and start running it.
Of course, we set the timer such that we know when we should take it out from the processor.
Deciding how to do this and how much time to allocate to these processes is the job of the operating system, which is called scheduling.
Another really important thing here, since we have so many things in flight here — there are things that are going into the oven, into the processor all the time, are being taken out of the time.
It is important that these things don't run on top of each other. So that's the reason why supervisor mode is not alone. We need to provide protection, translation, and paging.
Our goal of that is not to allow one program to run over another. One application should never corrupt other applications data.
So generally all programs are going to start from some fixed address, say, 8FFFFF in hex, and all of them are going to start from that instruction.
Now, we have to have a way to translate these virtual addresses into physical addresses.
And some of these programs or multitude of programs may be using more memory than we physically have. The solution to that is a concept of a virtual memory.
The concept of virtual memory we're going to dive into great detail in the next set of modules, in the next set of video segments.
But in general, the concept of virtual memory is there to provide the illusion of the higher memory pyramid, which says that our memory is infinite and as vast as our caches.
So each program is going to think that it has all the memory available to itself while all the other programs are going to think the same. So how does that happen?
It's done through the hardware with support of a software, and we're going to see that in a bit.
Time to perhaps take a bit longer break because we are going to continue with a set of modules that deal with virtual memory. See you then.
Đăng nhập để đánh giá học liệu này.
Đăng nhập để bình luận — chưa ai bình luận về học liệu này.
Không có khóa học liên quan.