WEBVTT 1 00:00:01.469 --> 00:00:10.889 William Cheng: This is the second part of lecture 13. So the first part we finish the first part of chapter six. And we're going to skip all the other stuff in chapter six. 2 00:00:11.250 --> 00:00:20.460 William Cheng: And now we're going to go through chapter seven, so that you can, you know, you can learn all the stuff that you're supposed to know and get ready to do Colonel assignment number three. Yeah. 3 00:00:21.060 --> 00:00:28.680 William Cheng: So Chapter seven basically sort of shows you how to implement virtual memory. So let's take a look at what's inside. 4 00:00:29.460 --> 00:00:37.770 William Cheng: So lots of software on your machine you're running, you know, 50 different programs simultaneously and they all need a lot of memory. 5 00:00:38.100 --> 00:00:48.990 William Cheng: So, so in this case, you know, and as we mentioned before they're using virtual memory than real physical memory over here, this is on your laptop, you might have four gigabytes eight gigabytes 16 gigabyte. 6 00:00:49.680 --> 00:01:03.510 William Cheng: But, you know, compared to the, you know, you end up at all your virtual address spaces every program has four gigabytes of virtual address if you have 50 program that's 200 gigabyte the physical memory is only you know 16 gigabytes. So again, we don't have enough memory. 7 00:01:04.530 --> 00:01:11.250 William Cheng: Okay. So, so what we need to do is that we need to do sort of considered a to an important issue number one is what do you do when you run out of memory. 8 00:01:11.490 --> 00:01:17.910 William Cheng: Right. So clearly you know all the memory that you need that the fit and physical memory. So what do we do, so I always talk about, you know, the basic idea over here. 9 00:01:18.180 --> 00:01:22.770 William Cheng: And chapter seven. We're going to sort of take a look into more detail. Yeah. The second one is, how do you actually 10 00:01:23.190 --> 00:01:29.700 William Cheng: You know, provide your protection. Right. You have all these 50 different policies, you want to protect the process from each other. And you also want to 11 00:01:30.210 --> 00:01:33.210 William Cheng: Protect the operating system kernel from the application. 12 00:01:33.810 --> 00:01:43.230 William Cheng: Okay, so, so how do you do it. As it turns out, you know, the debate, the basic solution is to use the address space and use the address space abstraction, or the address space concept. 13 00:01:43.680 --> 00:01:50.400 William Cheng: Okay, so, so, so having the idea of address space is the key to provide protection and also make sure you don't run out of memory. Okay. 14 00:01:50.850 --> 00:01:58.230 William Cheng: So we're gonna see how this is implement it, they can put that processes within each other that can protect the operating system from the user processes. 15 00:01:58.710 --> 00:02:04.170 William Cheng: They also have, you know, so, so once you start using this is the address space concept. They also a lot of advantages. 16 00:02:04.530 --> 00:02:13.320 William Cheng: Such as providing the illusion of large memory, right, the address space and then you have four gigabytes of memory, when in reality you might be only using a very, very small, not a physical memory. 17 00:02:13.560 --> 00:02:24.600 William Cheng: So again, using the address space. Well, you know, abstraction, but you can actually do that. Yeah, well, we also allow to have sharing, you can share the tech segment, we can share files. All these things are 18 00:02:24.900 --> 00:02:26.700 William Cheng: Done, you know, using the address space abstraction. 19 00:02:27.390 --> 00:02:38.490 William Cheng: Then we can also you know math things into memory we have memory manifold and use pipe, all these things that are achieved using other the address space abstraction. Okay, so, so we're going to talk about how to actually implement that. 20 00:02:39.300 --> 00:02:47.970 William Cheng: So before we do that, let's take a look at, you know, the idea of using a virtual address or remember you know you're what you're running your program or you're always using virtual address 21 00:02:48.330 --> 00:02:52.320 William Cheng: So the question is I, you know, when do you ever actually get to use physical address 22 00:02:52.620 --> 00:02:56.100 William Cheng: OK, so the key over here is to understand this picture over here, right. 23 00:02:56.340 --> 00:03:01.440 William Cheng: What, where do you need physical address, while the physical address is used to access physical memory. 24 00:03:01.530 --> 00:03:12.480 William Cheng: So therefore, right when you try to access physical memory you need physical address right here. So here's this physical address. Yeah, so this address appear on the bus. So therefore, anything on the bus has to be physical address 25 00:03:12.930 --> 00:03:20.400 William Cheng: Okay, so where do you use virtual address well virtual addresses the abstraction that's used by your program your program run your program runs 26 00:03:21.270 --> 00:03:29.640 William Cheng: So again, when a program runs you're running a thread inside of CPU. So somewhere deep inside the CPU. They're using virtual address this was somewhere right here we're using virtual address 27 00:03:30.360 --> 00:03:35.010 William Cheng: Yeah, so somewhere along the line, we need to cover the virtual address the physical drives we saw before. 28 00:03:35.160 --> 00:03:43.950 William Cheng: That these data structure called a stable that can provide address translation. And in chapter seven. We're going to see all the other data structure that will be used Adobe using to perform a determination. 29 00:03:44.130 --> 00:03:50.790 William Cheng: We also going to see what what kind of computer hardware, are you required in order for you to plug before you to provide documentation. Okay. 30 00:03:51.600 --> 00:03:58.890 William Cheng: Alright, so we're here says, you know, anything is physical address, you know, as it turns out, you know, I mentioned that before, right, the operating system also use virtual address 31 00:03:59.100 --> 00:04:08.220 William Cheng: If you give a physical address to the operating system. The system says, I have no idea what to do with it. Okay. Because the operating system. Can I use physical address. Right. So who can use physical address 32 00:04:08.580 --> 00:04:14.130 William Cheng: Well, so look at this picture, sort of tells you that, you know, the only thing that can use physical address over here. Are you know 33 00:04:14.340 --> 00:04:20.610 William Cheng: Because the CPU is sort of connected to the bus over here. So the CPU after you put home address translation, you can use physical address. Yeah. 34 00:04:21.150 --> 00:04:33.570 William Cheng: But also these devices. These devices out there, they can actually use physical address. Right. So for example, if you want to transfer data from device into memory. You have to tell the device to say where to transfer to the memory into using physical address 35 00:04:34.680 --> 00:04:43.140 William Cheng: That. So this data transfer from the device into memory. You have to use physical address because it goes over the bus. So if you use a virtual address under the bus law, then the bus doesn't know what to do with it. 36 00:04:44.070 --> 00:04:50.310 William Cheng: Okay, so therefore the you know what you should do with the physical address is that is that, you know, as you know, when 37 00:04:51.000 --> 00:05:00.300 William Cheng: When, when you're, you know, the opportunities that has a physical address what do you what do is how to a pass that information to the device rather this way that device can actually copy data from 38 00:05:00.570 --> 00:05:04.920 William Cheng: You know, from a full profile divine address, you know, you know, you need to physical memory addresses. 39 00:05:05.340 --> 00:05:09.600 William Cheng: That so you know what the operating system can do with physical address that it can only pass it around. 40 00:05:09.990 --> 00:05:17.550 William Cheng: Okay, because there's no way for the operating system to use physical address right so again we saw before in Linux blue in the middle of the second phase of the Linux blue 41 00:05:17.730 --> 00:05:25.290 William Cheng: It turns will address translation. Once you turn on after translation, even the operating system. Can I use physical address, guys. Okay. Very important to understand that. Yeah. 42 00:05:26.100 --> 00:05:30.990 William Cheng: Right, so be here. I write down over here is that the operating system manages the physical address space. 43 00:05:31.230 --> 00:05:38.490 William Cheng: Okay, the physical address space is the physical address for you to access physical memory. So the opportunities I mean URL for you to tell the device. The device controller. 44 00:05:38.670 --> 00:05:50.190 William Cheng: Were to copy data into it needs to know about physical addresses right even though the offices and can I use it in managers physical address space and the way it does. It does. That is to put a buddy system on top of it, right. The buddy list. 45 00:05:50.700 --> 00:05:56.040 William Cheng: Okay, well, you put a buddy list over here on top of the the the memory over here. Now you're going to get physical address it. 46 00:05:56.940 --> 00:06:05.940 William Cheng: was alive. And as I got we tend to physical memory over here, we sort of build that team data structure will keep taking them divided by half. As we mentioned before, we talked about you buddy list. 47 00:06:06.660 --> 00:06:18.000 William Cheng: Okay, so what it will do is it says, oh, so what it will do is that it will take the physical memory chopping down and also for every physical address, they will use a virtual address that they use a virtual address in order to access that physical address 48 00:06:18.390 --> 00:06:22.920 William Cheng: Okay, so we'll go. Why do you have to use a virtual address because the operating system can only use virtual address 49 00:06:24.000 --> 00:06:30.030 William Cheng: OK, so the operating system manages the physical address space and they provide virtual addresses to access this physical memory locations. 50 00:06:30.930 --> 00:06:36.840 William Cheng: Okay. All right. So it's a little weird. But again, that that's the way it is. You don't you try to access memory. 51 00:06:37.200 --> 00:06:46.080 William Cheng: Yeah. So in order for you to access memory. You need to provide a virtual address where do these virtual addresses come from guys okay here's somebody examples over here, a hardware of virtual address come from. 52 00:06:46.560 --> 00:06:48.570 William Cheng: So for example, you need to fetch a machine instruction. 53 00:06:49.140 --> 00:06:57.210 William Cheng: Okay, so how do you fit the machine CHARGING RIGHT, YOU KNOW, WE HAVE A IP inside CPU. There's a yeah IP register right IP sensible instruction pointers or two points to the tech segment. 54 00:06:57.420 --> 00:07:02.490 William Cheng: And the tech segment is not the address space, so therefore he IP contains a virtual address 55 00:07:03.270 --> 00:07:09.960 William Cheng: Okay, so, so, so what we call the virtual address your IP has a virtual address it tells you where to fetch the missing surgeon 56 00:07:10.200 --> 00:07:17.460 William Cheng: Right. So in this case, the instructions that you're sitting across the bus sitting in physical memory. So what you will need to do is that you need to convert the virtual address 57 00:07:17.730 --> 00:07:27.750 William Cheng: That that's inside the IP cover that into physical address startup bus operation and then take data from memory copy. They need to CPU and now the CPU can actually execute that instruction. 58 00:07:29.040 --> 00:07:36.630 William Cheng: Okay, so therefore that's one source of virtual address it come from the IP yeah alright the next example over here is that, you know, 59 00:07:37.830 --> 00:07:39.330 William Cheng: You know, so, so 60 00:07:42.240 --> 00:07:47.160 William Cheng: The next example here is the push yes onto the stack. Right. We saw instruction like this where I push 61 00:07:47.970 --> 00:07:55.350 William Cheng: You know, push, push. All right, yay. X or something like that, right. So this one takes EAs and then push it onto the bus so so again. 62 00:07:56.070 --> 00:08:05.220 William Cheng: What does that look like, right. So since I want to push it onto the stack. Right. So the stock look like this right ESP over here. Is that a CPU point to the top of the stack. 63 00:08:05.520 --> 00:08:17.460 William Cheng: Right when you execute push out. Yes. What happens right we decorate ESP by four and then we copy the value of us into this memory location that are not funded by by by the ESP pointer. 64 00:08:18.000 --> 00:08:20.190 William Cheng: Okay, so yeah ESP, or we here I you know 65 00:08:20.700 --> 00:08:31.560 William Cheng: The way I draw this picture is that it points into the address space. So therefore ESP also has a virtual address right so in order for you to push this data onto the stack, you need to document ESP, you still have a virtual address 66 00:08:31.830 --> 00:08:38.130 William Cheng: You convert that into physical address that you put it onto the bus and then you push content and then a CP will be driving the bus. 67 00:08:38.310 --> 00:08:45.060 William Cheng: And then it will take the content OBS and then write it out to the data portion of the bus, and now the memory will take the data and then right into physical memory. 68 00:08:45.840 --> 00:08:56.040 William Cheng: Okay, so that's how you execute this particular instruction. So again, inside ESP register container container virtual dress. Right. That's another. That's another place where virtual desktop or up 69 00:08:56.610 --> 00:09:03.900 William Cheng: The third example over here, x equal to 123 right you execute the insertion all the time in your code, right, where x is a local variable. 70 00:09:04.020 --> 00:09:13.740 William Cheng: Since it's a local variable we here then again we draw that picture over here. Right here is ESP ESPN money right here right and then EDP points in the middle of a stack rank, whereas local variable. 71 00:09:14.070 --> 00:09:22.860 William Cheng: Right, so remember function arguments are at the bottom of the stack frame logo gray bar at the top of the stock room over here. So we're going to take the EDP registers subtract a few memory location over here. 72 00:09:23.040 --> 00:09:26.490 William Cheng: And then we're going to get the address of x. That's right here. So, sitting over here. 73 00:09:26.880 --> 00:09:32.610 William Cheng: We have it again. We'll get a virtual address because EDP over here, point to our address space. So therefore, it contains the virtual address 74 00:09:32.820 --> 00:09:41.370 William Cheng: We subtract a few memory location from it was still have a virtual address would convert that into physical address over here and then again we perform it right operation across the bus. 75 00:09:41.550 --> 00:09:49.140 William Cheng: And now when we drive the bus. We're going to drive that data pattern 123 onto the bus. We're going to end our writing to this memory location 123 into physical memory. 76 00:09:50.370 --> 00:09:57.510 William Cheng: Okay, so, so it looks like inside the CPU inside of CPU all the CPU registers over here. They all contain virtual addresses. 77 00:09:58.440 --> 00:10:02.370 William Cheng: Okay. And as it turns out, that's true. Right. Whenever you run, what will I will well 78 00:10:02.820 --> 00:10:11.520 William Cheng: Whenever you write a program. And you see that in chapter three. Right. We saw the code get combined to assembly code in the assembly call all the addresses are virtual addresses. 79 00:10:11.970 --> 00:10:22.470 William Cheng: Okay, so, so, so, so it's kind of a fair question to ask. Inside the CPU. Are there any register that contain physical address. Okay. And of course the answer is gonna be yes. But so far, we haven't seen any yet. 80 00:10:23.040 --> 00:10:27.360 William Cheng: Okay, so later on we're going to see a registry is that the CPU that actually has a physical address right 81 00:10:27.960 --> 00:10:32.160 William Cheng: Alright so far it seems that everything contained virtual dress. Okay, so that's why virtual just come from. 82 00:10:32.940 --> 00:10:37.260 William Cheng: So the basic idea over here is called EGGO transition. We're going to take a virtual address. I'm going to cover them to 83 00:10:37.470 --> 00:10:45.030 William Cheng: physical address. So here's a picture that I draw right here is the processor again in this class processors CPU core. They all mean the same thing. 84 00:10:45.330 --> 00:10:50.940 William Cheng: Okay. So inside the inner part of the processor over here. I call it the inner core of the CPU. 85 00:10:51.120 --> 00:11:01.320 William Cheng: This is where they have all these register ESP I PvP all these registered over here. They all you know they're all using virtual address. So coming out of the unicorn this up over here. I'm going to use a virtual address 86 00:11:01.800 --> 00:11:07.710 William Cheng: Okay, but the CPU and the LB get connected a bus ride the bus is right here. Right. And on the right hand side of yours physical memory. 87 00:11:08.250 --> 00:11:14.370 William Cheng: That. So I need to cover virtual address to physical address because when I try to use the bus. I need to use physical address 88 00:11:15.090 --> 00:11:27.840 William Cheng: Okay. So, therefore, if something does happen somewhere between the inner core of the CPU and the bus. That's why the address translation happen we translate a virtual address into a physical address or we convert a virtual address into into physical address 89 00:11:28.680 --> 00:11:32.490 William Cheng: Yeah. So in this case, you know, why would we try to you know to 90 00:11:33.540 --> 00:11:39.630 William Cheng: Try to access memory over here. So in this example over here we have a virtual address over here called virtual address with the address of 100 91 00:11:40.170 --> 00:11:50.520 William Cheng: Okay, and we're running two different processes. One of the pool process over here. And what is the pink process. So we're going to sort of pretend that these two processes. They are really, really old processes their addresses are very, very small. 92 00:11:51.090 --> 00:12:06.570 William Cheng: The blue process over here the address space is only 300 bytes. OK, so maybe 1970 1980 over here with the Intel CPU, we can actually have an address space that's very tiny the paint process over here. I mean, by, by the way. Today, all the process. He has four gigabytes or address 93 00:12:08.430 --> 00:12:18.120 William Cheng: Of address space okay but but but but back in, you know, 3040 years ago they are processes have very, very tiny address space as well. So that's what I mean is there as as an example. 94 00:12:18.570 --> 00:12:24.330 William Cheng: The pink process over here is only 500 bytes all okay so what we're gonna do is we're actually we're gonna you know who 95 00:12:24.630 --> 00:12:30.480 William Cheng: We were going to allocate physical memory so that the entire program for the blue and the pink process that can fit into physical memory. 96 00:12:30.870 --> 00:12:41.490 William Cheng: Okay. So on the right hand side of your physical memory. So as it turns out that for the blue process. I'm going to add okay 300 by starting a physical address 1200s over here and this will be on to the proof process. 97 00:12:42.270 --> 00:12:54.840 William Cheng: That and also when I tried to allocate 500 bytes for the paint process. I found a location 9500 and again this is physical address starting and location 9500 over here I 500 pounds over here that belong to the pink process. 98 00:12:55.170 --> 00:13:06.870 William Cheng: OK, so again this is long time ago. All these memory location over here there contiguous. And that's the entire address space for the blue process and then the pink process so so so this guy is the map the different parts inside physical memory. 99 00:13:07.530 --> 00:13:11.700 William Cheng: Okay. So, by the way, when I tried to allocate memory like this, how would I allocate memory and physical memory. 100 00:13:12.060 --> 00:13:18.270 William Cheng: How do I allocate these memory segments of your I mean this case I'm allocating address space in the physical memory. What algorithm would I use 101 00:13:18.600 --> 00:13:24.360 William Cheng: When I use the buddy system know what I use the slab alligator know I will use the first fit memory alligator. 102 00:13:25.020 --> 00:13:32.610 William Cheng: Okay, so, so again 3040 years ago the memory allocated for physical memory was also using the first pseudonym alligator. 103 00:13:32.850 --> 00:13:39.630 William Cheng: Okay, so this case, I can allocate a segment that's exactly the size that I want. And in this case, this will be the entire address space for my program. 104 00:13:40.530 --> 00:13:49.560 William Cheng: Okay. So I think in the good old days when you have a DOS program. Well, we have a good old days in the Macintosh program so so so the address space look very, very different from today's address space. 105 00:13:50.640 --> 00:13:57.780 William Cheng: So the idea here is that when I tried to access virtual address 100 over here. How do I cover that in the physical memory location. 106 00:13:58.290 --> 00:14:04.110 William Cheng: What it depends on what process you're running right because if you're running the pink process over here the paint process starts at 9500 107 00:14:04.470 --> 00:14:21.330 William Cheng: So if you want, you know, you know, address 100 so it was just 190 500 over here will be virtual address zero for the paint process and then come go 1234 all the way to 499 490 days going to map to 99999 and then I have the 108 00:14:21.990 --> 00:14:26.610 William Cheng: You know, the physical address that. So that's where it needs to match. So in this case, what is 100 109 00:14:26.850 --> 00:14:38.430 William Cheng: Right 100 is simply 100 bytes starting from the beginning of the address space that's only the beginning or dragger space and 9500 so 100 over here will be accessing physical memory location 9600 110 00:14:39.240 --> 00:14:44.700 William Cheng: Okay that's if you are running the pink process inside the inner core the CPU. What if you're running the blue process. 111 00:14:45.000 --> 00:14:50.580 William Cheng: Well, if you're running the blue process over here. I want 100 bytes, you know, starting 100 bytes. 112 00:14:50.850 --> 00:15:02.040 William Cheng: Away from the beginning of the address space and beginning of addresses over here is physical address 1200. So in this case, you know, virtual it goes one hundreds over here need to turn into 1300 for the blue process. 113 00:15:02.850 --> 00:15:09.930 William Cheng: Well, so the question is, how can you have one virtual address translate into two different physical address. I didn't know who's running out of CPU. 114 00:15:10.920 --> 00:15:20.190 William Cheng: Okay, so therefore I mean that's, you know that that's. This is the empty spot over here, the empty spot over here. It's going to allow you to translate virtual just 100 to 1200 if I run into 115 00:15:20.790 --> 00:15:25.560 William Cheng: 1300 grand Pooh bah says, and he will translate into 9600 if you run a process. 116 00:15:26.250 --> 00:15:35.520 William Cheng: That. So the solution over here. We also put that into the into the CPU so so again the, the solution over here is going to be one level indirection. 117 00:15:35.970 --> 00:15:41.160 William Cheng: Okay, we're going to start with the virtual address going through one level in direction. And now we're going to end up with the physical address 118 00:15:41.460 --> 00:15:49.260 William Cheng: Then this piece of hardware inside of CPU is known as the memory management unit right that's all configurable going over caller calling this memory management. 119 00:15:49.680 --> 00:15:59.160 William Cheng: In chapter seven. Chapter seven is called memory management. So this unit over here is called memory management unit and the purpose of that is to convert a virtual address to a physical address 120 00:16:00.510 --> 00:16:12.630 William Cheng: Okay, so, so get don't address the physical memory directly, you know the address out of CPU coming on the, you know, quote, the CPU is virtual as it goes through the memory management unit, he will translate virtual address into physical address 121 00:16:13.920 --> 00:16:21.870 William Cheng: That. So, so, so, so, so, so in this case, you know, how do we actually do it right. I mean, if you look at this picture over here, he said, and then you over here. 122 00:16:22.110 --> 00:16:31.920 William Cheng: That's performing the, the other translation. There are a bunch of MMU register. So if you load the register with different value is going to perform address translation differently. 123 00:16:32.340 --> 00:16:40.920 William Cheng: Okay, so for example, if I'm running the, the blue process over here. I'm going to know the enemy over here with the register that are associated with a blue process. 124 00:16:41.190 --> 00:16:52.500 William Cheng: Okay, so over here will look like this over here. I'm going to turn the inner call or the CPU to blue to show you that I'm running the blue process. So this guy is I will also low the MMU registered over here with the blue register. 125 00:16:53.400 --> 00:16:57.660 William Cheng: Okay, so this way virtual address 100 is going to get converted into 1300 126 00:16:58.620 --> 00:17:03.480 William Cheng: Or so they don't want to see how you do this exactly right. But that, but that's the basic idea of address translation. 127 00:17:03.750 --> 00:17:19.110 William Cheng: If I am loading me registered with the right register value corresponding to the blue process now 100 is going to translate into 1300 I also, you know, zero is going to translate into 1200 right and therefore guess, in this case 299 is going to convert into 1499 128 00:17:20.190 --> 00:17:28.440 William Cheng: Okay. And also, more importantly, if you try to access your data outside of your address space what in that case MMU is actually going to make sure that you trapped into the operating system. 129 00:17:29.460 --> 00:17:34.080 William Cheng: Okay, so that would be the job for the MMU to also protect also provide protection. 130 00:17:34.500 --> 00:17:41.070 William Cheng: So this way you're blue process would not be able to access any memory location that's outside the blue region that are mapping to the physical memory. 131 00:17:42.000 --> 00:17:47.730 William Cheng: Then similarly when you switch to the pink process. They, in this case, I'm going to load the meal with a pink register over here. 132 00:17:47.940 --> 00:17:56.430 William Cheng: So they will look like this. Right. So again, that, you know, called the CPU term pig now and the enemy of your I don't change the rest of them are you the only thing I tell you, them and you are me you register. 133 00:17:56.700 --> 00:18:03.060 William Cheng: Right, so if I load it with a different set of values over here now virtual address 100 is going to get covered into 9600 134 00:18:03.270 --> 00:18:12.690 William Cheng: Which is 100 bytes, you know, into the pink address space. So again, if you use virtual address zero. Now you're going to get 9500 if you use virtual just 499 135 00:18:12.870 --> 00:18:18.870 William Cheng: You're going to get 9999 which the last memory location over here the allocated to the pink process. And if you use any 136 00:18:19.470 --> 00:18:29.370 William Cheng: You know, any virtual address that's that's not within range now between zero and 499. Well, in that case, you know, the enemy or we will refuse to perform the address ization and he will force the 137 00:18:29.820 --> 00:18:34.440 William Cheng: CPU to go execute our trapping to the ordinances and so the audiences and can kill your user space forward. 138 00:18:35.970 --> 00:18:40.080 William Cheng: Alright so what we're gonna do is, I'm going to sort of talk about how to actually implement this MMU 139 00:18:40.290 --> 00:18:47.280 William Cheng: What are what actually go into the menu register. What kind of different registers are there. So this guy's got this, you know, this picture of some sort of show you that. 140 00:18:47.460 --> 00:18:51.960 William Cheng: This is inside the CPU. So everything we're going to talk about today is going to be talking about hardware. 141 00:18:52.710 --> 00:19:04.710 William Cheng: Is actually, you know, Chapter seven has two sides of slides. The first set aside is mainly about computer hardware. So we need to understand the hardware and then in the second part of chapter seven was a little sort of talk about how does the operating system use all these hardware. 142 00:19:05.820 --> 00:19:16.860 William Cheng: Okay, so, so, you know, today's lecture. And also, you know, a part of the next lecture, we're going to be staying in talking about the, the idea that the hardware that implements the MMU that 143 00:19:19.140 --> 00:19:25.740 William Cheng: All right, so, so we're going to talk about how to do that. So I guess at once you start using this menu. As it turns out, if you do things this way. 144 00:19:25.950 --> 00:19:32.730 William Cheng: It's going to give you all the stuff that we mentioned before, it's gonna give you protection, which I talked about already, right, if you don't use your address within the right range. 145 00:19:32.940 --> 00:19:40.050 William Cheng: Then we're going to type into the operating system. So this work and provide protection. They will also allow you to have illusional large memory allow you to sharing all this 146 00:19:41.100 --> 00:19:46.740 William Cheng: stuff I talked about before. Okay, so let's take a look at how do you actually implement at the mmm hardware. 147 00:19:50.610 --> 00:20:01.260 William Cheng: Alright, so let's talk about like the oldest and then you on the old exam you is, you know, in this case the menu is actually a very, very simple. The only has one register and the register is known as the fence register. 148 00:20:01.560 --> 00:20:06.390 William Cheng: OK. So the idea here is that, I mean, this is so old that there's really no address translation. 149 00:20:07.020 --> 00:20:13.380 William Cheng: There's only protection. There's no address translation. So virtual address equal to physical address. Okay, so this is my physical address 150 00:20:13.740 --> 00:20:17.760 William Cheng: This is my physical memory over here. I mean, my virtual address a look exactly the same. 151 00:20:18.000 --> 00:20:26.910 William Cheng: Part of my physical, you know, memory over here is allocated to the user programs over here. This is running the user program and the bottom part over here is running the operating system or the operating system kernel. 152 00:20:27.270 --> 00:20:28.320 William Cheng: Guys writing the code over here. 153 00:20:28.890 --> 00:20:37.680 William Cheng: Okay. So in this case, when you're running the user space program, or in this case, what do you have to worry about that. You just want to worry about whether the user space, who can access operating system memory or not. 154 00:20:37.860 --> 00:20:42.930 William Cheng: If the user space program and try to access the opposite of memory. Well, then, in this case, you have to kill us just baseball bat. 155 00:20:43.470 --> 00:20:51.720 William Cheng: That. So this guy is Robin is that, here's a user space. Well, here's the user portion of the address space. And here is the Colonel. So the line over here. 156 00:20:52.110 --> 00:20:55.380 William Cheng: You know, beyond which is the operating system that's called the fence. 157 00:20:56.100 --> 00:21:04.950 William Cheng: Yeah, so all we need to do is to store the memory locations over here inside the fence registering. Is that a CP over here. So will you try to generate a virtual address again virtual address equal to physical address 158 00:21:05.220 --> 00:21:10.080 William Cheng: If the virtual address is less than the fence. In this case, virtual address is going to become physical address 159 00:21:10.920 --> 00:21:18.330 William Cheng: Okay, if the virtual just over here is bigger than or equal to fence. Well, then in that case the user space program has tried to access operating system memory over here. 160 00:21:18.510 --> 00:21:27.960 William Cheng: In that case, what you will do is that you will generate a fall into the inner core the CPU and now I'm going to tap into the ordinances that was you are going to audiences and the opposite over here will kill the user Facebook. 161 00:21:28.770 --> 00:21:36.450 William Cheng: Okay, because he is a space program does not have, you know, the privilege to access your virtual addresses that belong to the operating system now. 162 00:21:37.140 --> 00:21:46.110 William Cheng: Right. So in this case, there's only there's only one user space program. What about other user space. Well, well, well. As it turns out, this is so. Oh, you know, this is like you know dos. 163 00:21:46.740 --> 00:21:53.490 William Cheng: You know, in the good old days, you can only run one program at a time. So in that case, you know that you know that the there's there's no other user space program. Right. 164 00:21:53.760 --> 00:21:58.950 William Cheng: All we need to worry about is that we don't need to worry about protecting user program from each other because there's no other user 165 00:21:59.400 --> 00:22:04.350 William Cheng: There's no other user program. Okay. All we need to worry about is protecting operating system from user space memory. 166 00:22:04.740 --> 00:22:10.830 William Cheng: So what my user space program. Okay and this case also we don't you know, how do we actually create the illusion of large memory. 167 00:22:11.820 --> 00:22:18.270 William Cheng: Okay, so. So in this case, you know, the virtual space is the same as physical address space. So this case there's no illusion of a large memory. 168 00:22:18.720 --> 00:22:28.680 William Cheng: What if the you know the the program that you try to write doesn't fit into physical memory, right, this area over here has limited amount of memory. What if your program actually Yo yo yo yo space is much bigger. 169 00:22:29.400 --> 00:22:39.120 William Cheng: Okay, so in that case in the good old days, again, they don't really have virtual the virtual addresses. They don't have virtual memory. So they come up with a sort of a solution is known as overlays. 170 00:22:40.140 --> 00:22:46.710 William Cheng: So the idea of yours that you know so. So again, the bottom hobby is happening. Sam, the user area over here. I'm going to divide them into two areas. 171 00:22:46.920 --> 00:22:53.550 William Cheng: One is called overlay. The other one is called the resident part of your user space. Well, why it's called the resident part because there's always a memory. 172 00:22:53.940 --> 00:22:57.570 William Cheng: Okay, so what you will do is it will take your entire audio space over here for your users Facebook. 173 00:22:58.080 --> 00:23:06.210 William Cheng: Divided into multiple parts. The bottom one over here is the is the residents. Why so this one you copy them into memory and once you copy them into memory. They never move 174 00:23:06.660 --> 00:23:09.780 William Cheng: That the top while we're here, I'm going to divide them into overlays. 175 00:23:10.020 --> 00:23:19.920 William Cheng: Only one of them will fit into physical memory at a time. Right, so maybe over here, this is the overlay number zero is over, the number one is number two and this is number three. So, so anytime 176 00:23:20.130 --> 00:23:28.170 William Cheng: The resident part over here is in physical memory and also one of the overlay over here is that it's actually a physical memory. Yeah, well, all the other overlay they're sitting on this. 177 00:23:28.800 --> 00:23:37.470 William Cheng: Okay, so this way when you try to, you know, so what's inside the overlay is going to be a function is going to be your data, and will you need to call a function that's not inside your current overlay. What do you have to do. 178 00:23:37.830 --> 00:23:44.640 William Cheng: Okay, so what you can do is that you can actually call a function inside the read the rest of them are the resident part of 179 00:23:46.170 --> 00:23:52.650 William Cheng: The residence hall, your address space, right, and then you give an instruction to say where to go on to this to copy another overlay over here into memory. 180 00:23:53.310 --> 00:24:00.240 William Cheng: Okay. And also, what you can do is that, so. So once you call that function over here. What do we do, is that it will take the overlay data will be here if you have been modified 181 00:24:00.420 --> 00:24:05.670 William Cheng: For example, some of the data that you have changed over here. So we need to write it back to the desk right and we need to wait for it to get da 182 00:24:05.820 --> 00:24:15.810 William Cheng: Well we finished doing that if the next part that you want is overlay. Number two, what do you do, is that it will take data overland number to over here transfer that into memory over here and wait for it to get done when it 183 00:24:16.650 --> 00:24:22.620 William Cheng: When it finishes, what you will do is that you will return from this function. Will you read up on this notion, you're going to end up executing code inside the overlay. 184 00:24:24.090 --> 00:24:29.220 William Cheng: Okay, so, so again, so, so, so the bad news. Over here is that this is not done by the app. 185 00:24:29.460 --> 00:24:33.630 William Cheng: By by the organism, because everything happened over here is done by the user space program. 186 00:24:33.810 --> 00:24:39.990 William Cheng: Then, so you as a programmer, you have to write code like this, right. So that will you try to switch that off on a distant memory. 187 00:24:40.170 --> 00:24:46.380 William Cheng: You have to write all the code that will talk to the dentist to get the data over here. And then you have to worry about you know where your data is are you 188 00:24:47.250 --> 00:24:51.240 William Cheng: Is a function that you're calling is a city on this or is a city on Korean Korean in memory. 189 00:24:51.630 --> 00:25:00.000 William Cheng: Okay. I actually had the pleasure of writing a program like this, you know, somewhere in 1980s. I don't want to don't want to do that. Do that again because this is too painful. 190 00:25:00.630 --> 00:25:06.000 William Cheng: Now alright so this is really, really old so that we don't we don't see this anymore and that's a good thing. 191 00:25:06.990 --> 00:25:13.020 William Cheng: Then come something called the base in the bound register and that will that's putting my studio that the one that I 192 00:25:13.470 --> 00:25:22.440 William Cheng: I presenter when I draw the address translation picture over here. Okay, so again, in this example, we hear a physical memory. I'm going to allocate the address space for both the blue program. 193 00:25:22.710 --> 00:25:25.050 William Cheng: So the blue address space and a pig address space. Okay. 194 00:25:25.260 --> 00:25:35.220 William Cheng: For the blue eyes starts at 1200 and the address space is only 300 by stall for the penguin over here is only 500 bucks on. So again, these are really silly, silly example. In reality, the other spaces will pick. Other than that. 195 00:25:35.520 --> 00:25:45.060 William Cheng: Yeah, but the idea over here, stay the same. Over here, so. So in this case, when I tried to run the blue for a while. What do I have to know that. So I need a pair register over here to remember 196 00:25:45.510 --> 00:25:49.500 William Cheng: You know where you know where his memory allocated over here in physical memory. 197 00:25:49.830 --> 00:25:58.440 William Cheng: Okay, so don't register know as the the base register and the boundary register. So that's what this is called the patient about register. Okay, the base. What does the bass, bass is the array. 198 00:25:58.800 --> 00:26:05.190 William Cheng: Is like basically array. Right. It's the first memory location inside your address space, but in this case it will be a physical address 199 00:26:05.460 --> 00:26:14.190 William Cheng: Yeah, so the base registers over here should contain for the blue process. This one should contain 400 mile vs. As for the blue process that base register content all hundred 200 00:26:14.400 --> 00:26:21.210 William Cheng: And where's the bounce the bounce is the size of the outer space, and this guy is equal to 300 bytes over here. So, therefore I'm going to write 300 201 00:26:21.510 --> 00:26:33.810 William Cheng: Yeah, so will you. Will you ready to run the run the blue process. I'm going to copy these to register values over here into the mm you so now the base registering. Sorry. Mm. You will have 100 and then the band's over here with 300 202 00:26:34.590 --> 00:26:42.540 William Cheng: That so from now when you try to perform address translation. So coming up the inner core the CPU. I'm trying to generate a memory reference or virtual address 100 203 00:26:42.750 --> 00:26:54.180 William Cheng: What I would do is our first I will compare this virtual address against the bounce to see if it's within bow of it's out of bounds. If it's out of bounds. Then in this case I'm going to generate a truck into Albany system for the purposes of until my program. 204 00:26:54.630 --> 00:27:03.720 William Cheng: Okay, so in this example 100 is less than 300 so therefore it's within bow. So what I was simply do is to take the virtual address over here and add it to the base register. 205 00:27:04.470 --> 00:27:14.280 William Cheng: Okay so 100 plus basis 1200. So in this case, I'm going to end up with a number 1300 100 plus 1200 1300 and that's exactly 100 bytes into my address face. 206 00:27:14.850 --> 00:27:27.300 William Cheng: OK. So again, if the virtual address over here is equal to 00 plus 400 will be the first memory location in the virtual address is 299 it will be the last memory location over here. Is there any way I can access any memory location outside my address space. 207 00:27:27.750 --> 00:27:36.630 William Cheng: Okay, the answer will be no I cannot generate a negative offset anything bigger than or equal to 300 I'm going to end up trapping to the audience's them and the system will kill my program. 208 00:27:37.800 --> 00:27:44.700 William Cheng: Okay, so therefore this give you protection, right, because now I'm guarantee that the blue process can only access those 300 memory locations. 209 00:27:45.000 --> 00:27:51.930 William Cheng: That one of our solutions are pink process right the pink boxes over here also has the base of the register. So, by the way, this information. Where are they stored 210 00:27:52.920 --> 00:27:55.650 William Cheng: Well me clearly they go into the process control, blah. 211 00:27:56.580 --> 00:27:58.740 William Cheng: Right. So this way when I when I tried to switch it around the 212 00:27:59.010 --> 00:28:10.380 William Cheng: Other the pink process over here, what I need to do is I need to take this information outside the process control blah and loaded into the MMU register. So there are special instructions for loading this information from you know from 213 00:28:12.240 --> 00:28:19.590 William Cheng: For your address space over here. So, sorry, or you're already scissor into the Emory register. Okay, so in this case the base register, it's going to go. 214 00:28:19.800 --> 00:28:26.700 William Cheng: It's not become 90 9300 because that's the first memory location over here and then the bounds over here is go to 500 because that's the size of the address face. 215 00:28:26.970 --> 00:28:32.760 William Cheng: Okay. So by now you should see that inside the MMU the base registered contain a physical address 216 00:28:33.300 --> 00:28:38.160 William Cheng: Right, so I was asking the question over here is that, you know, inside a CPU over here. Is there anything that has, you know, 217 00:28:38.370 --> 00:28:46.890 William Cheng: We saw everything has virtual address. Is there any register container physical address. Well, we also saw before, you know, the MMU is actually inside, inside the processor 218 00:28:47.550 --> 00:28:52.350 William Cheng: Okay, so therefore inside the process over here in the inner core all they have are the 219 00:28:52.980 --> 00:28:58.920 William Cheng: Register that contained virtual address but outside over here and then you will have registered that can actually contain physical address 220 00:28:59.400 --> 00:29:03.750 William Cheng: That. So the physical address over there. What is it for its for doing to to address translation. 221 00:29:04.020 --> 00:29:13.170 William Cheng: Now, so, so again, similar I'll be here when you switch to the pink process over here. I'm going to load these register value into the menu and then we provide our transition. We're going to do exactly the same thing. 222 00:29:13.380 --> 00:29:21.900 William Cheng: But compare 100 guests the bounce bounce over here you go to 500 within bow, so therefore it's okay. Otherwise, we're going to try to the operating system. So obviously the material program. 223 00:29:22.350 --> 00:29:33.300 William Cheng: Since this is within bounds. We simply going to add this number to the base register over here and that is 300 and the I'm going to end up with 9400 and that's exactly 100 bytes into outer space. And that's exactly what you want. 224 00:29:34.710 --> 00:29:43.530 William Cheng: Okay, so, so, so, so, so again you know these information stored inside process control blah, so therefore the MMA register as part of the context of your 225 00:29:44.400 --> 00:29:50.760 William Cheng: Part of the context of your process. So this way when you switch to a different process. Whereas when we saw the function called swap contact 226 00:29:51.060 --> 00:29:56.520 William Cheng: So strong content was the need to swap out all the register over here will include the menu registered. We also need to change it. 227 00:29:56.910 --> 00:30:00.750 William Cheng: Right, because otherwise, you know, we're going to end up performing the wrong address translation. 228 00:30:01.110 --> 00:30:13.590 William Cheng: Okay, so, so again, when you swapping contacts you not only have to either you have to, you know, save and restore all the CPU register. You also need to worry about other register that you have to save and restore okay and this will include them. Are you registered 229 00:30:16.410 --> 00:30:24.930 William Cheng: So after. So the base and bounds over here. I guess the program, get a little more complicated over here. So instead of having, you know, just one contiguous address space. 230 00:30:25.350 --> 00:30:30.570 William Cheng: People find it more convenient to actually divided address space into multiple memory segments. Right. Why is that 231 00:30:30.810 --> 00:30:43.350 William Cheng: Good. Because when the problem gets bigger and bigger and bigger, right, we saw before the address space we have tech segment. I mean, the way we think about the other side tech segment data plus BSS the heap segment. And then there's a big gap. And then at the end over here. Their stack. 232 00:30:44.430 --> 00:30:52.080 William Cheng: Okay, so in this case if we allocate this entire address space at a time, then Robin is that we're going to end up having segmentation, you know, the 233 00:30:53.910 --> 00:31:05.250 William Cheng: We're going to end up with external factors at NASA potential fragmentation. We're gonna have external fragmentation and physical memory and if all the outer space over here that are pretty good, pretty big. Pretty soon we're going to run on a physical memory. 234 00:31:06.090 --> 00:31:14.220 William Cheng: Okay, so it's much better to take our address space over here divided them into multiple memory segments over here and we're going to allocate each memory segments separately. 235 00:31:15.240 --> 00:31:19.230 William Cheng: Okay, so this way and this example we're going to divide our memory segue into forward. 236 00:31:19.800 --> 00:31:27.000 William Cheng: That email address space into four different segments. Here's the tech segment. Here's the data plus BSS here's the key. But here is a stock over here. 237 00:31:27.270 --> 00:31:32.160 William Cheng: And there for each one of them. We're going to use the first film memory allocated to allocate physical memory. 238 00:31:32.790 --> 00:31:42.270 William Cheng: That. So in this example over here to text adventures right here is 200 bytes law that data SPSS over here. This one is 5100 bytes law over here sitting over here. 239 00:31:42.510 --> 00:31:46.260 William Cheng: And then, are they the heap size went over here is sitting at the top over here. So, yeah. 240 00:31:46.530 --> 00:31:55.410 William Cheng: When you are you know so allocating these individual memory segment you know separately, then you know they only get sorta in the physical address space anymore. Even though they're sorting logic. 241 00:31:55.740 --> 00:32:05.730 William Cheng: virtual address space physical address way that can be allocated anywhere Russell guy was simply follow the first remember allocate a rule, wherever we can find room inside our, you know, 242 00:32:06.930 --> 00:32:15.960 William Cheng: Our physical memory. Then, then that's where these memories segments going to get located there. So this guy is a huge segment is right here and the stacks up against right here. Right. So again, they're separate managed 243 00:32:16.650 --> 00:32:27.000 William Cheng: Separately managed over here, since all the memory segments are smaller than is going to take us longer to run out of memory. Okay, so that's why we decided that that's why people go to segmentation. 244 00:32:28.290 --> 00:32:40.830 William Cheng: Alright, so. So in this case, how do you actually implement that. Okay, so you go to the segmentation over your all you need to do is that he said MMU, you have to use one pair of segment one pair of base and VAL register per memory segment. 245 00:32:41.940 --> 00:32:50.070 William Cheng: Right, because in this guy's we have four different memory segments over here. So inside MMU we're going to have for our peers of baseball register each one of them for each 246 00:32:50.640 --> 00:32:59.880 William Cheng: memory. Memory segment and we're going to use them exactly the same way as before. Right. Well, we generate our virtual address, we're going to compare that against about if it's within bow. We're going to add it to the base register. 247 00:33:01.050 --> 00:33:10.230 William Cheng: Okay, so, so this is called the generalization of the base amount register and this is known as segmentation. Because when I end up not using one address space. We're going to chop it into multiple segments that 248 00:33:11.430 --> 00:33:15.060 William Cheng: So, so, you know, so in the good old days, you don't want this one. This happen. 249 00:33:15.630 --> 00:33:22.350 William Cheng: You know so. So one thing that we can sort of take a look at is that, you know, if we think about our address a look at this from virtual address zero to 200 and have 250 00:33:22.560 --> 00:33:32.040 William Cheng: You know, there's a gap right here. And then from 300 to 400 over here is going to be a data segment. There's another gap right here from 600 to 1600 is going to be the heap. And then there's a gap over here and then come to stack. 251 00:33:32.940 --> 00:33:37.530 William Cheng: Right, so in that case in the good old days, the address space is actually doesn't really look like that. 252 00:33:37.710 --> 00:33:49.590 William Cheng: Okay, so will you try to access in this example access, you know, that's sort of the data segment over here. So this address the way we think about it today is that it has virtual address 250 so that will be 50 memory location over here. 253 00:33:49.890 --> 00:33:51.660 William Cheng: You know, starting from the beginning of the data segment. 254 00:33:52.440 --> 00:33:54.690 William Cheng: Okay, in the good old days, I think for Intel 255 00:33:55.050 --> 00:34:05.070 William Cheng: The way they generally memory reference over here. They actually they actually spells out which space and been registered to us. And also, what is the offset into that base that the memory segment. 256 00:34:05.430 --> 00:34:10.410 William Cheng: Okay, so even though. Today we sort of think about is that we want to generate a memory reference over here or 350 257 00:34:10.680 --> 00:34:18.990 William Cheng: Coming out of the inner core the CPU. Number one is that is how you which base involved register to us. And then the second one is that it tells you what is the offset into the memory segment. 258 00:34:19.380 --> 00:34:27.360 William Cheng: Okay. So in this case, what's coming on the inner core the CPU will say that I want to use a second pair of base amount registers with this one says the data segment register over here. 259 00:34:27.660 --> 00:34:35.250 William Cheng: Okay, so. So again, these have name. This is a tech segment registered as a 32nd register. Here's a heap segment registered over here. Here's a stack of my register over here. 260 00:34:35.760 --> 00:34:46.470 William Cheng: Okay. So coming out of the air, called the CPU said that in order for me to perform address translation. I need to use the data segment register over here and the author says equal to 50 and the rest of it. It's exactly the same interface and 261 00:34:46.980 --> 00:35:01.740 William Cheng: Then I'm going to compare the offset over here against the bounce. If they're within bound, then I will add the offset over here to the base. So in this case, I'm going to end up with 1310300 plus 50 which will be exactly 50 bytes into the you know the 262 00:35:02.610 --> 00:35:15.210 William Cheng: Into the status hagner I hear that. So again, if the you know the the the offset over here is going to be bigger or equal to the bounce. Why, in that case, again, I'm going to generate fall into the inner core of the CPU trying to the operating system, the offices have been killed us 263 00:35:16.410 --> 00:35:20.490 William Cheng: Now, so in this case, how does the unicorn over here knows that you need to access this memory location. 264 00:35:20.790 --> 00:35:27.900 William Cheng: Well, in the good old days, you know, compiler is completed for and the compiler actually knows about what the CPU will look like it would generate code based on the 265 00:35:28.170 --> 00:35:35.430 William Cheng: Based on what kind of CPU that you have run off if you need to compile your code into a different CPU. So in this case, that the code generator will be very, very different. 266 00:35:35.760 --> 00:35:43.890 William Cheng: Okay. So the compiler actually need to figure out over here that when you try to generate a memory reference is no longer just a virtual address but you need to specify which 267 00:35:44.160 --> 00:35:52.740 William Cheng: Which baseball register us. Also, what's the offsetting that in throughout register, guys. So this is just the way was around 1970s or something like that. Yeah. 268 00:35:54.270 --> 00:35:58.260 William Cheng: All right, so, so let's take a live example of yourself want to execute 269 00:35:58.650 --> 00:36:11.160 William Cheng: You know, virtual I just 150 right. So again, I know I can execute it because the inside the type of the CO segment of the tech segment over here and this is 150 bucks into the CO segment over here. Right, so, so, so, so when 270 00:36:11.700 --> 00:36:19.170 William Cheng: When I try to generate revenue refers I say I want to use the coast of my register over here. And also, I need to say I need to use 150 into the address base. 271 00:36:19.380 --> 00:36:24.720 William Cheng: I'm going to compare 150 against the bounce. If it's smaller than the boss. I will add it to the base our guys over here in January. 272 00:36:25.710 --> 00:36:35.970 William Cheng: Okay, so, so remember that there's something different about the between the CO segment and the data and the heat and the stock right the sex. I've been over here that the coast that went over here needs to be read only. 273 00:36:36.660 --> 00:36:47.280 William Cheng: Okay, so in this case is really, really easy to do with the segmentation over here every pair of bass inbound register. I can also add one more bit to it to say that whether this particular memory segment is read only or rewrite 274 00:36:47.940 --> 00:36:58.920 William Cheng: Okay, so in order for me to provide access control and protection for different for individual memory said man. All I need to do is that inside the MMU every base inbound register. I need one more bit 275 00:36:59.460 --> 00:37:09.090 William Cheng: Okay, so in this example for the tech segment over here. I said this picture read only. And for the data and the heap and the stag over here the bed over here says read and write. 276 00:37:10.050 --> 00:37:18.180 William Cheng: Okay, so this way when I tried to execute code over here, I will, you know, I will use this particular the base about register, I will also need to compare 277 00:37:18.510 --> 00:37:23.940 William Cheng: The operation. I need to perform against the spirit that this this access bits over here to see if they're compatible. 278 00:37:24.750 --> 00:37:28.920 William Cheng: That. So in this example here. I'm trying to execute on virtual address. So what to execute 279 00:37:29.130 --> 00:37:36.150 William Cheng: Actually, you mean that go to memory and read from a memory location over year. So therefore executed is compatible with reality. So therefore nothing bad happens. 280 00:37:36.330 --> 00:37:47.340 William Cheng: OK. So again, I need a computer to see if they're compatible and then I need to compare the offset against the balance. And finally, everything works out. Then I will take the offset over here editor, the base register and generate a memory reference 281 00:37:48.030 --> 00:37:50.820 William Cheng: There. What if I tried to write to this memory location over here. 282 00:37:54.690 --> 00:38:00.630 William Cheng: Okay, so then when I tried to write a virtual address 100 coming out of the inner core of the CPU would say that I want to write 283 00:38:00.870 --> 00:38:08.070 William Cheng: Using the first pair of base amount register with the offset 150 so in this case right will be incompatible with read only. 284 00:38:08.190 --> 00:38:18.390 William Cheng: So in this case, you know, when you try to do this comparison is that MMU what it would do is that it would generate a fault into the inner core of the CPU. So this guy. You were trapped into the oven in society and the operating system can kill your program. 285 00:38:19.920 --> 00:38:26.100 William Cheng: Alright, so again, by adding this one more bit into every pair of bits and bobs register and now we actually kind of access control. 286 00:38:26.280 --> 00:38:36.420 William Cheng: We can set individual you know memory segment to read only or we right i mean we can actually even set a bit cold call exit to bed over here as this guy, we need to beds can be either, you know, read 287 00:38:38.070 --> 00:38:41.970 William Cheng: Read Only versus rewrite right and the other behavior can be to execute bit 288 00:38:42.690 --> 00:38:48.390 William Cheng: That. So actually you have been, it means that we're using the coast segment over here. So in this case, you know, you're only allowed to fetch instruction. 289 00:38:48.630 --> 00:39:01.380 William Cheng: From the one that has the execute business at all. Alright, so again, there's all depends on the CPU some CPU actually in the me register happy execute on and some of them, guys. Okay, that they have different manufacturer, they get to decide whether CPU. 290 00:39:05.430 --> 00:39:11.430 William Cheng: And also, you know, once you have a multiple segments over here. We can also allow the memory segment, to be sure. 291 00:39:11.580 --> 00:39:18.960 William Cheng: Especially with the memory segment is read only. Right. So if you read only connect you share it as many times as you want, you can share with as many processes as you want. 292 00:39:19.200 --> 00:39:25.140 William Cheng: Okay, so now we can see these two programs over here. Maybe they are the parent and a child processes over here. So in this case, they were sure 293 00:39:25.410 --> 00:39:30.090 William Cheng: You know the tech segment, but they will not share the data segment and the heat segment this next segment. 294 00:39:30.420 --> 00:39:39.480 William Cheng: Right. So this case is that physical memory over here. Again, I'm going to sort of draw the picture over here the tech segment over here and share right so therefore I draw them both in blue and pink over here. 295 00:39:39.720 --> 00:39:46.290 William Cheng: And then for the blue on here is the heap segue. Here's the data segment for the pink one over here, you know, this one is 296 00:39:47.070 --> 00:39:51.990 William Cheng: I guess you know the smaller ones over here. It's going to be the data side rather larger one over here is going to be the heat second 297 00:39:52.920 --> 00:39:54.420 William Cheng: Okay. So in this case, when I'm 298 00:39:54.660 --> 00:39:59.580 William Cheng: Trying to run the blue process. I'm going to take all the MMA register over here from the process control, blah. 299 00:39:59.730 --> 00:40:08.310 William Cheng: Loaded into the MMU over here, right. So now you know the the tech segment over here will be pointing to the share memories that man and later on we tried to sushi. The pink segment. 300 00:40:08.490 --> 00:40:12.180 William Cheng: Where we choose a pink segment over here, we need to, you know, take the other the 301 00:40:12.450 --> 00:40:21.690 William Cheng: Me register from the process control blah, again, copy it over here. When we do that, we're going to see that this one over here, stay exactly the same value. So I'm going to point to the same memory segment over here. 302 00:40:21.870 --> 00:40:25.080 William Cheng: One of the other segment over here there rewrites so they pointed different memory segment. 303 00:40:26.220 --> 00:40:33.960 William Cheng: OK. So again, this way, though, you know, the blue processes are pretty process, they can actually share the tech side man I'll why they all have their individual data side. 304 00:40:35.160 --> 00:40:37.590 William Cheng: The visual data segment and the heat and the second segment. 305 00:40:38.580 --> 00:40:47.640 William Cheng: There. So you can see that, you know, using the segmentation over here. What you have a lot of advantages over here and you know so. So for a while segmentation was actually really, really popular. Yeah. 306 00:40:48.150 --> 00:40:51.090 William Cheng: So I think, you know, intelligent, they, they also they they implement segmentation. 307 00:40:51.810 --> 00:41:00.360 William Cheng: Alright, so this is also where the terminology segmentation fault comes in right what a segmentation. All right, we already see one of the segmentation of all we hear that. So when you try to 308 00:41:00.690 --> 00:41:10.380 William Cheng: You know, write to the tech segment. Well, in that case, you know, right, is incompatible with read only. So, therefore, that's a segmentation for you talking to the ordinances and the UK your program that 309 00:41:10.830 --> 00:41:15.390 William Cheng: The other possibility over here is that what if we address memory location over here that's in the gap. 310 00:41:15.990 --> 00:41:23.820 William Cheng: Guys over here again conceptually what sort of things about each other access memory location 450 that's between the data and the heaps Edwin's over here. So in this case, 311 00:41:24.150 --> 00:41:34.110 William Cheng: What would the compiler actually generate code for. Okay. So in this case, one possibility is that a compiler will generate code where you're just say use the data segment register over here with the offset 150 312 00:41:34.860 --> 00:41:39.450 William Cheng: Okay, why would the compiler generate code OB or didn't the compiler actually know how big the data segment as 313 00:41:39.990 --> 00:41:46.380 William Cheng: Well, the compiler should not be the data segment is so maybe you should just general, you know, an error, right at the time when you compile code. 314 00:41:46.590 --> 00:41:53.160 William Cheng: But this is really happen all the time because sometimes you know the reason that you generate this offset over here is because you're using an array index right 315 00:41:53.340 --> 00:42:00.060 William Cheng: There are really the the base of the array or but you actually point to something that's within bow. But as it turns out, your array indexes out of 316 00:42:00.750 --> 00:42:06.630 William Cheng: The rain that is too big, you're going to end up generating a reference over here just happened to be in the bat in the just having to be in the back you 317 00:42:07.350 --> 00:42:18.120 William Cheng: Just have to be in the gap. So you're going to end up with. I want the asset data segment over here with offset 150. So in this case, I'm going to compare the access over here with the protection because they're compatible. 318 00:42:18.300 --> 00:42:30.060 William Cheng: And then I'm going to compare the offset against the bounce when this case, this you know this number is out of bounds. So therefore, again I trapping the operating system. And now it turns into a segmentation fall and then the opposite will kill you. 319 00:42:31.650 --> 00:42:42.000 William Cheng: So this is the origin of the term segmentation fall. We'd like to term so much, even today, we are not using segmentation. What we're using today. We're using paging right because we will actually allocating a page. 320 00:42:42.390 --> 00:42:45.150 William Cheng: Well, I'll keep the memory, you know, four kilobytes at a time. 321 00:42:45.630 --> 00:42:49.710 William Cheng: Okay, but we'd like this term so much. Today we're going to review. We're going to continue to use the term. 322 00:42:49.890 --> 00:42:59.340 William Cheng: Segmentation fall. Well, you try to access the memory location over here either. You don't have access, you have the wrong kind of access in the I'm going to end up with a segmentation fault. Yeah. 323 00:43:02.340 --> 00:43:12.270 William Cheng: All right, I'll be here. Another example over here, if I write two. This is the one that we had before I if you write a memory location 150 right is incompatible with read only. So again, these fall coming inside 324 00:43:12.750 --> 00:43:22.650 William Cheng: Coming from the MMU as it turns out that this one is the segmentation ball because the segment register over here is incompatible. So we're going to end up talking to the ordinances that are your program. Yeah. 325 00:43:25.350 --> 00:43:28.020 William Cheng: Alright. So as it turns out that, you know, using 326 00:43:28.380 --> 00:43:39.300 William Cheng: You know your base and bound register. You can also with the you know the read only builds on the rabbit over here. We can also implement memory map file guys will get memory metaphoric can be done in the application program using the 327 00:43:39.720 --> 00:43:44.730 William Cheng: System call. So we're going to map the entire file or part of a fall into a memory segment. 328 00:43:45.360 --> 00:43:51.360 William Cheng: Okay. So in this case, what do we have to do, actually, to implement that. Right. We already have, you know, the tech segment that data segment the BSS 329 00:43:52.230 --> 00:44:04.050 William Cheng: The heap size they are plus b s as the human the stock over here if you want to implement memory map is it turns out that's really not sufficient. Okay. So even though we have the basic hardware, but we need additional base inbound register. 330 00:44:04.890 --> 00:44:14.730 William Cheng: Okay, so they are actually CPU out there, you know, not only to have four pairs of Bessemer register. They actually have additional periods of base of marriage or so this way you can map a file into your address space. 331 00:44:14.970 --> 00:44:22.200 William Cheng: Yeah. So in this case, would you have to do is that you actually have to specify what I tried to allocate a file and mapping my address space. So again, the idea here is that 332 00:44:22.380 --> 00:44:26.670 William Cheng: Well you map a foreign to you. I just space accessing your outer space. The same thing accessing the file. 333 00:44:27.630 --> 00:44:33.840 William Cheng: Okay. So, and this guy is what we have to do is that we need to allocate a new pair of base amount register. So instead of CPU, I guess. 334 00:44:34.080 --> 00:44:41.670 William Cheng: The 80s, they're actually CPU that has 16 pairs of business register. So the first fall over here for text data, they'll even BSS 335 00:44:41.850 --> 00:44:47.940 William Cheng: And then we have additional top here, based on my register. So this way you can actually get a handle 12 different kinds of memory map files. 336 00:44:48.180 --> 00:44:53.490 William Cheng: Yeah so. So how do you, how do you implement that. Right. So here, here's a sort of a very, very simple picture. 337 00:44:54.180 --> 00:45:02.700 William Cheng: Well, here's the file. So let's say that this you know the size of this fall over here is 120 bytes law. I just made up a random number here 120 bytes. The law here. 338 00:45:02.970 --> 00:45:13.110 William Cheng: A hard. Well, not the into my address space. Okay. So in this example, what I will do is I will allocate 120 bytes in physical memory and then map that physical memory over here into my address space. 339 00:45:13.620 --> 00:45:18.450 William Cheng: Okay, so we're here. I'm going to go to my physical memory over here, use my first memory allocated over here. 340 00:45:18.660 --> 00:45:32.040 William Cheng: Allocate 120 bucks over here, right, the pace is is 400 over here. So again, I need a bass and don't register with a basic will to 400 and the fallacy would 120 so now I will ask the hardware to allocate a new pair of base and not registered over here. 341 00:45:32.280 --> 00:45:40.080 William Cheng: Well, actually, I know I tried. I actually don't do that because the the operation will be here will keep track of. So I would need to add the operating system for how 342 00:45:40.290 --> 00:45:49.200 William Cheng: To find out which pair of base amount register, I can use and then Robin is that you know is inside my data structure over here. I need to create a new memory segment for this memory map out 343 00:45:49.830 --> 00:46:04.020 William Cheng: Okay, so this new memory second over here, in this case the virtual address the first virtual address and this example starts at 1000 and it's 140 bytes all so therefore the final memory location over here is going to be 1819 344 00:46:04.920 --> 00:46:07.710 William Cheng: That. So here is a memory segment. So now you know 345 00:46:08.490 --> 00:46:14.040 William Cheng: My address space. In addition to those for Mary said, Man, I also need to add another memory segment over here for the memory map out 346 00:46:14.250 --> 00:46:24.390 William Cheng: There. So over here I call it the memory of my phone number one over here. So in this case I will also the operating system also remember which pair of basis inbound register is allocated to this particular 347 00:46:24.600 --> 00:46:34.620 William Cheng: You know memories that man. So in this case, when I tried to access the virtual address a 171 78 which is 178 bytes into this memory map our memories said man. 348 00:46:34.920 --> 00:46:42.030 William Cheng: Right coming out of the inner core of the CPU, it will reference the new pair of base amount register with the offset up or 178 349 00:46:42.630 --> 00:46:47.940 William Cheng: OK, so the rest of is going to be exactly the same, right, if I know that this is going to be the pace of personal register that I need to use 350 00:46:48.150 --> 00:46:57.390 William Cheng: The base over here is going to be equal to 422 pounds over here is equal to 120 820. So what I will do is I need to compare the access to see if they're compatible with rewrite 351 00:46:57.570 --> 00:47:10.860 William Cheng: I'm pretty much everything is compatible with the rewrite over here. And then I'm going to compare 178 I guess the balance is this lesson about. I'm going to add 178 to 400 I'm going to end up access exactly 178 bytes into this memory segment. 352 00:47:12.090 --> 00:47:21.450 William Cheng: Okay, so in order for me to support memory math or we hear all I need to do is that I need to, you know, increase the number of days and Bell register inside of CPU. 353 00:47:22.800 --> 00:47:33.660 William Cheng: Okay, so. So in this case, you know, as turns out that you know for the harder part is actually pretty simple right if you may and may make an additional 12 you know peers abysmal register with a read only rewrite this available, you know, 354 00:47:34.200 --> 00:47:39.180 William Cheng: To to ordinances and what's what's tricky over here is that the compiler over here needs to generate the right code. 355 00:47:39.600 --> 00:47:44.310 William Cheng: Well guys forget the long time ago, the operating system, the compiler people the hard people, they all got together. 356 00:47:44.460 --> 00:47:55.710 William Cheng: And they did decide the other. However, he was going to do this and the operating system is going to do that. And then the compiler. People will make sure that the memory reference that generate over here is going to be correct. So this way we can end up, you know, supporting memory that 357 00:47:57.090 --> 00:48:06.780 William Cheng: All right. Okay. So I guess this is sort of a good time to break. So next time we're gonna in part three of lecture 30 I'm going to continue 358 00:48:07.260 --> 00:48:16.740 William Cheng: To talk about the first part of chapter seven. So we're still talking to me a hardware. So we're going to finish you know all the segmentation stuff and then we're going to start talking about patient. Okay, all right, be back in a bit.