‫Hello and welcome, everybody, and in this lecture, we're going to change our process Maalox, so
‫it actually maps the memory.
‫So currently what we do is we allocate the memory, but we don't map it to the process.
‫So what this means is if we tried to access the point to return to us, there's going to be page four.
‫OK, now, we obviously don't want that.
‫So what we're going to do is we're going to map the pages so that they're accessible from the user process.
‫So essentially, much like we map the processed memory into virtual address 400000 for our program,
‫we can do the same thing here.
‫But we're going to map the pointer address to itself so the virtual address will equal the physical
‫address and we're going to give it some flags so it'll be readable, present and accessible from all.
‫OK, and if we don't do this, then the user program won't be able to access the point we returned.
‫OK, so it's very important.
‫First, let's change this up a bit because there's this potential for memory leaks here.
‫OK, I can see that.
‫So what we're going to do is we're going to create an out free label.
‫No one out error, the label, I should say, out error.
‫And Altair is just going to return zero.
‫And it's going to say if the pointer is not null, then Kifri the pointer.
‫That's what I was going to do.
‫And just by here, we're going to say go to out error and we're going to do the same here, go to out
‫error.
‫So that fixes that memory leak.
‫Right.
‫Because now we we free the pointer.
‫If there was an error, OK, obviously in C++, you would use constructors and D.
‫D constructors, but we don't have that in C guys.
‫So there's only so many ways we can play the game.
‫You know, we could make a separate function.
‫Maybe we can try to create something else for managing the memory.
‫But, you know, this is an acceptable way of doing it in C, although you would never use go to in
‫many languages, in C, it's it's acceptable.
‫OK, so what we're going to say here is insurance equals paging map to process task.
‫Top page directory now the virtual address here will be pointer.
‫The physical address here will be pointer.
‫The fiscal end address we point to plus size and the flags will be paging is rideable.
‫Paging is present and paging access from all.
‫OK, now where the pointer size is, we need to align that to the next page up, assuming it is in page
‫four lines because as I've already told you on working web pages, it has to be page aligned.
‫So we're going to say paging
‫our line address point to plus size.
‫OK, and that that should align that address OK for us.
‫So this successfully maps the memory.
‫It maps the memory from the pointer virtual address to the point of physical address.
‫So they share the same birthday address and physical address and it maps it to the pointer plus size,
‫which is the end of the the allocation.
‫OK, so way declaring 60 bytes, it'll map it to the second page.
‫OK, so we're setting the pages right about page and is present paging access from all this bits.
‫Especially important paging axis from all because as I've already said, it allows us to access that
‫from axis of memory, from any ring level.
‫OK, so it's very important.
‫So yeah, we've now done that and we should now be able to access the memory pass back to us in process
‫malac.
‫That's just to make clean to make sure that works OK and build S.H..
‫And we see unused verbal rests, so when I was just going to check for an hour or so, we're going to
‫say if wrasse below zero go to Outtara because we failed to map the memory.
‫OK, now we're ready to try this now.
‫So let's just do a make clean.
‫And I built that S.H..
‫And we can see that compiles perfectly fine, OK, if we now go to I use a program, we're going to
‫go to the blank, blanked out, see and hear.
‫All we're going to say is we're going to get rid of this string talk stuff.
‫We're just going to say, Char Appointer equals Mallak, uh, shall we say 20 bytes and then we're going
‫to copy string copy into point.
‫Hello, World.
‫OK, and then we're just going to print out Hello World using print.
‫So let's just check all of that works.
‫True.
‫And make clean bill that S.H..
‫And then we're going to run that.
‫So we're going to go city and we're going to run Kumu.
‫And now we're just going to say Blankfort elf to load that program and we see world, so that's perfect.
‫We've successfully mapped the memory that was incredibly important because if we didn't do that, mallock
‫would declare a memory for us.
‫But we the used the program wouldn't be able to access that memory.
‫So the mapping of it is very important.
‫Now, there's something you need to be aware of and we will get to this in another lecture inside the
‫process, free function.
‫Let me get that up for you.
‫Process free.
‫You can see we can free the memory so we free all of the memory, OK?
‫However, we don't remove the page table well, we don't remove the page of the user process.
‫So what that means is when someone else comes along and they allocate some memory using process, mallock
‫again we will have access to another processes.
‫Memory.
‫That's a security hole, obviously.
‫OK, so ideally we need to change our page table to remove that.
‫OK, but it's not so important now.
‫It can wait.
‫You know, after all, you're not making windows right now, you know what I mean?
‫You're not making something that's going to be publicly usable, that needs to be very secure.
‫But I just want you to be aware of how security flaws happen in operating systems.
‫So this is one of them, right?
‫Because all we have to do is allocate some memory free, wait for another process to allocate that memory
‫and use it.
‫And Bob's your uncle.
‫We can actually access their memory.
‫And that's obviously very dangerous.
‫So essentially, when we free the memory that the process allocated, all the we have to do is just
‫before we came free is remove the link in the page table.
‫OK, now that's so important.
‫If we remove the link in the page table by changing the flag to supervisor only and we do that by removing
‫the paging axis from orbit, then our process won't be able to access that memory, OK?
‫And that's very important that we do that.
‫So quick overview then.
‫If we go to process Malac, we allocate the memory on the heap.
‫We map the memory with the virtual address equaling the physical address.
‫OK, so the pointer address here in Pointer is both the virtual address and the physical address for
‫our tasks page tables.
‫Now you'll notice we only map the processes task.
‫That's because processors can only have one task.
‫If you decided in the future to have multiple tasks preprocessed, then you would need to make some
‫sort of function here called process map two, which would loop through all of the tasks of the process
‫and do a paging map to on that.
‫And that will ensure that every task has access to the memory that we process malot here.
‫OK, so that's very important as well, by the way.
‫So these are things to consider for the future if you wish to develop your kind of further.
‫The reason I haven't shown you that is because that's pretty much common sense.
‫Once you've learned how the how to write kernels, which is what this course is teaching you.
‫So just keep that in mind in case you want to develop that further in the future.
‫OK.
‫Thanks for watching this lecture.
‫We will get round to doing the process free by here.
‫But it is a bit more tricky, guys, because we don't know the size of the points you provided to us.
‫So we're probably going to have to change some of the data here in the allocations, for example, so
‫that it tells us the size as well.
‫And how how do you unlink it from the page table?
‫That's so easy.
‫You do a page, a map to again, but you just don't have that bit, that bit that allows us to access
‫from everybody.
‫So it's not so difficult and we can do it.
‫So take care, everyone, and I'll see you in the next one.