‫Hello and welcome in in this lecture, we are going to implement the ability to copy strings from tasks,
‫we will implement the functions we need to do this.
‫However, we won't use them yet.
‫We'll use them in another lecture.
‫Let's now begin and implement those functions.
‫So the very first thing we're going to do is we're going to go to test and see.
‫We're now going to go to create a new function.
‫We're going to simply go in it.
‫Copy string to task struct task task void virtual.
‫So it's a virtual pointer void physical.
‫And then we're going to have an integer Max.
‫And get rid of the hashtag them so inside kopi string to task.
‫We're going to go if Max is above all, equal page size, then return and valid arguments, because
‫we're going to limit this to up to a page size, essentially, OK?
‫And that's because we only want to be changing one page, not multiple pages.
‫It's easier to implement that way.
‫So firstly, before we can send you notes that, uh, you need to be in the kernel page before entering
‫this function.
‫OK, so we're going to go in address equals zero return address char temp equals Kazal.
‫OK, Max.
‫If temp is no more than rascal's minus NMM, so it's inhumane.
‫OK, and we're going to go to outby here as well.
‫Guys go out.
‫We're going to make the out label by here as well.
‫OK, so we're allocating some memory for us to copy a string into OK.
‫This memory will be shared by both the task page tables and the kernel page tables, allowing them to
‫talk to the exact same physical memory.
‫So now we're going to go Unit 32 t task directory equals task page directory.
‫Directory entry.
‫Unit 32 tea old entry equals paging get task directory temp.
‫OK, now let's just check paging get actually exists, just go to.
‫Memory paging paging Dr. C, unless you search for paging get OK, we don't have a paging get in here.
‫OK, we will have to make that up for now.
‫For now will carry on writing this function.
‫So we're just going to go paging map task directory Tim Tam Paging Page Rideable.
‫So now it'll be page rideable, it's not actually showing here.
‫Let's go to the head of file, paging Dr. Hajj.
‫And we see paging is right about, OK, we need to include the head of them, scroll to the top here
‫and we're just going to go include memory paging paging Dr. Hatch.
‫OK, and now we're going to carry on with this function.
‫So we're going to go paging right writable.
‫Paging his rideable paging is present.
‫And finally, paging access from all which will allow us to access that memory from userspace, although
‫we might not need to access it from userspace because we're going to free this memory very quickly as
‫soon as we've copied it across from the user page.
‫OK, so now we're just going to switch the page table to be equal to the tasks page directory and then
‫we're going to go string and copy and we need to include string.
‫Hey guys, just go to the top here.
‫We're going to go include string, string, dot, Hajj, OK?
‫And I'm just going to put that just below memory.
‫Derg the.
‫So we're going to go stringer and copy Tembe Virtual Max, OK, and now we're going to switch back to
‫the Colonel Page and we're just going to go rascal's paging set tasks, directory 10, old entry.
‫And then if that fails, we're going to say if rest's below zero, it failed brassicas minus CIO go
‫to go to out free.
‫And we're going to create that label in a minute where everything's fine at this point.
‫We're going to copy to the physical address from the temp buffer.
‫OK, I'll explain more about this entire function a moment.
‫Just copy exactly what I'm doing when I going to create out free label and we're just going to OK,
‫free time.
‫Simple as that.
‫OK, so let's explain why this function does the virtual address provided to us.
‫We unfortunately cannot access from Conolan directly because the virtual address will be mapped in the
‫process address frames.
‫So it'll be 400000 hexadecimal something.
‫But our 400000 hexadecimal something points to physical address, 400000 hexadecimal something because
‫we're in kernel where we're on the kernel page.
‫So what are we going to do?
‫Because all of the processes share that same virtual address.
‫You 400000 Hecks.
‫What can we do?
‫How are we going to copy the string?
‫Because we can't just copy from zero x 400000 hex and whiffing or something.
‫We can't just copy from that.
‫It's not going to work because the kernel page tables are different.
‫So what we do is we create some memory.
‫This memory will be shared with the tasks memory, OK?
‫And we do that by using paging.
‫So we essentially first allocate some memory in the kernel.
‫And here, you know, we have access to that because the kernel page tables are mapped linearly.
‫OK, zero x 1000 virtual address points to zero x 1000 physical address.
‫OK, so we have access to this.
‫OK, but the task doesn't unfortunately.
‫OK, so what we do is we get the entry, the paging entry from the task force for the same address that
‫we've allocated here with Cosulich.
‫Now the virtual address, one for the user land is going to point somewhere different.
‫OK, you know that the value Cazorla return here, it might point somewhere entirely different for the
‫user process.
‫We don't know.
‫OK, now with the current design, only the address space, you x 400000 hex to the file size is mapped
‫elsewhere in memory.
‫Most likely the page tables already match.
‫OK, but we don't assume that.
‫OK, so what we do is we get the old entry from the task for that particular page.
‫OK, for the temp address here.
‫And that will return us to the physical address with the flags appended to that.
‫OK, we then we then map the physical address temp right to the physical, the virtual just temp to
‫the physical duress temp for that task.
‫OK, and we make it readable, present and accessible from from everybody from all privileged levels.
‫So what that does is that allows tasks to see the memory we just created in the kernel here because
‫we map the virtual address of temp to the physical address of temp.
‫OK, and because we've done that, because we did that like that, the task can now see what we can
‫see.
‫OK, the task page can see what we can see.
‫So now we switch to the tasks page because remember, we still have the the string at Seahawks' form
‫2015, for example.
‫We still have that string, that VirTra address provided to us.
‫And, you know, we have to switch the tasks page so that we can see that memory.
‫OK, so we switch the tasks page and because we just mapped the temporary variable here.
‫On the task page, we see that memory as well.
‫OK, which is good for us, so we can safely copy from the virtual address straight into the temporal,
‫because both the colonel and the task see the same memory pointing to the same place.
‫We then switch back to the Colonel Page, because once we've copied the memory from USERSPACE into the
‫colonel space, we don't have to do anything else.
‫OK, so we then switch back to the Colonel Page.
‫So we see the whole colonel memory as it's supposed to be again.
‫Now, remember both that both the U.S.
‫Task and the colonel, they both sought help.
‫For what?
‫For what it is, OK?
‫They both understood they were both mapped to the same address for the temporal.
‫So inside our temporal, now we switch back to the colonel page.
‫We still see the string that we copied here on line 147.
‫So we're back in the Colonel Page now and we need to restore the old task page back to the old entry
‫for that address, because maybe he was pointing somewhere important, like a resource or something
‫you don't know.
‫You know, it might have been being used for something.
‫So we have to send it back to the old century to avoid the use of process, pointing to this point forever.
‫OK, the page tables, I mean, pointing to this point of forever.
‫So what we then do is we copy from our time before we just created into the physical address provided
‫to us here in the function prototype.
‫And then we don't need the Temba for anymore.
‫It's done.
‫It's done his job.
‫So we just caffery that and return and it's as simple as that.
‫Now, if you guys struggle to understand that, please let me know and I'll try to explain it.
‫But that's what's going on there, guys.
‫OK, so let's just scroll up a minute and we're going to take Kopi String to task.
‫We're just going to paste it in the header file.
‫We're then going to go to paging Dr. C..
‫And we're going to create the page and get function, so we're just going to go unit 30 to 40 page and
‫get even a 30 to 40 directory.
‫So this is a point to avoid virtual address.
‫So it's going to take the virtual address and it's going to grab us the actual entry in that page table.
‫So it will basically include the address and the flags.
‫OK, that's what that's going to do.
‫So if we pass in the year 2000 here, it's going to return the physical address for the pay for the
‫virtual address of the page two thousand and the address the returns will be the physical address with
‫the flags Ordon.
‫OK, so that's what this function is going to be for.
‫So we're just going to go unit three to T directory index equals zero.
‫Unit thirty two T table index equals zero and address equals paging get indexes, virtual address directory
‫index and the table index.
‫OK, so that I'll get the indexes for us.
‫We now say Unit 32 T entry equals directory directory index.
‫Now we say Unit 32 T. And this is appointer table equals Unit 32 T case that we're going to cast the
‫entry with zero x f f f f f zero zero zero.
‫OK, so that I resolve the table for us.
‫The page table, OK.
‫So now we want to get the page table entry and we just do that by going return table table index and
‫that will return the physical address of any virtual address with the flags as well.
‫So I just want us to copy and paste this and put it in the header file.
‫OK, and there we go, perfect, and we are now ready to actually use copy string to test function.
‫Oh, and guys, I can't believe I did this this entire time.
‫I've named it Copy Strength Task, while it should be copy string from task.
‫So I don't know how I made that mistake and not realize.
‫But as you as you've gathered, we were implementing a function for getting a string from the task,
‫not for setting a string for the task that that function comes later.
‫Now, I just want you to rename that copy string from task and we're going to change the one intestacy
‫as well.
‫OK, well, good.
‫Just going to see if that builds now so you can see there's an error here on line one four five passing
‫argument, one of paging map from incompatible point to type.
‫OK, so that's OK, just scroll down to line 145.
‫We can see that one four five uses the task directory, however, the paging map function expects a
‫page in four gigabyte chunk.
‫So I just want you to change that to task page directory.
‫OK.
‫Just like that, rather than use the task directory here, so that's Philine one four five paging maps.
‫So we're going to recompile that now and we can see one more error.
‫Unused variable rests on paging dot C line one hundred and sixty three.
‫So go to line one hundred and sixty three.
‫We're going to scroll down.
‫And we can see that it's by here.
‫So just get rid of it as equals and we'll just keep pushing.
‫That index is OK and we now we can power.
‫We can see that works perfectly fine.
‫OK, so excellent, guys.
‫Well done on completing this lecture.