‫Hello and welcome, everybody, and in this lecture, we're going to actually access the task stack,
‫so we're essentially going to push some values here and inside of our Mr. Cifas.
‫The son will retrieve those two values and we'll sum them together, OK?
‫And then we'll return that value as a result.
‫Before we continue, there's some minor bugs that need to be fixed in the code that I noticed.
‫And rather than let you guys go through the hassle of the bug in that we're going to fix them right
‫now.
‫Unfortunately, these things do happen.
‫This is kernel development.
‫So even I can sometimes make little typos, little mistakes on the keyboard.
‫So they're very simple things we're going to fix from now.
‫It's just a few typos, that's all.
‫So the first thing I want us to do is to go to memory.
‫Paging Dr. C and just search for paging Map to you'll see this and in here you'll see paging map range.
‫So just control left click to find that and you'll see here we say if wrests equals equals zero break.
‫Wow.
‫You can see the problem here, right?
‫We basically said if everything's OK, then we should stop.
‫We don't want that.
‫All right.
‫If REST's is below zero break, that's what it should be.
‫Because remember, if the value is below zero, it's an error, not equals equals zero.
‫So that was a mistake there.
‫Some of you might have already realized I did that and corrected it a few lectures ago.
‫However, that's my mistake.
‫As I as I say, I apologize.
‫These mistakes can happen because this is kernel development.
‫It's not your average hello program here.
‫You know, so little typos can happen.
‫OK, so that's one issue solved.
‫We now need to go to page to configures of Hajj.
‫And you'll see here the stack address and we are basically accessing the virtual address start, while
‫obviously it should be the stack address start, so just change that to the static address.
‫OK, and why is the stack end beneath the stack start?
‫Well, because the stack goes downwards in electronic systems by default, that's why.
‫OK, so now that we fix those minor bugs, we can now carry on and start developing this.
‫The very first thing we want to do is prepare our program to pass those arguments.
‫So just go to blank.
‫That, I assume was simply going to go push twenty, push thirty, and that will sum those together.
‫OK, so here we can say, come on, zero sum just to make it clear.
‫So it's common.
‫So what's going to happen?
‫We're going to push 20 to the task stack our program stack.
‫We're going to push 30 to the stack.
‫I remember when we used the stack, we're pushing for bytes at a time.
‫All right.
‫I know these numbers can fit into just one byte, but it doesn't matter.
‫Four bytes at a time being pushed.
‫Right.
‫So just remember that.
‫So we push twenty three to the stack.
‫We set the command to zero commands you as we call in at zero eight.
‫OK.
‫And then Xerox 80 will extract 20 and 30 from the tasks stack frame.
‫OK, well from the task stack I should say.
‫Now there's one more thing we need to do.
‫We need to go ADP eight y eight because four bytes for this push for bytes for that push.
‫So we just want to restore the stack.
‫All right.
‫You could also restore it by doing Pax Poppy.
‫But look how ugly that is.
‫Just to add ESP eight, that would be perfect.
‫And then now restore the stack to what it was because remember, the stack goes downwards.
‫That's why we're adding, not subtracting.
‫I use the program is now ready.
‫We now need to start modifying the kernel so that we can read these arguments that are pushed to the
‫SEC.
‫OK, so let's start implementing the actual kernel functionality, forgetting these task, forgetting
‫these stack items that the task has pushed.
‫So go to test, see.
‫And we're going to create that function at the bottom here, we're just going to go void.
‫Tasket stack item struck task task in index.
‫So while this will do is this will pull the stack item from the task, from the task stack, so if we
‫provide an index of zero and they only push one item, then it's going to pull that item from that stack.
‫If they post two items, then we'll have to use index zero and index one so we can pull from this stack.
‫OK, so it's basically like peeking where peeking into their stack.
‫OK, so that's what's going on here.
‫So we're going to go viewpoint results equals zero and we need to be in the kernel page when we run
‫this function.
‫OK, make sure that whenever you run that function, you're in the kernel page and we're just going
‫to give you an 80 pointer S.P. pointer.
‫So stack pointer equals unity cast here.
‫Now task registers dot ESPE.
‫So we're just making an array to the stack pointer of the task.
‫OK, and as you're aware, this is a virtual address, not a physical address, because this is this
‫is the tasks stack pointer register.
‫It was saved when we went into the interpreting.
‫If you remember, we go to IGT Dot.
‫See here I can show you, uh.
‫We saved the state, you say, of the task task currency state, and we provided the frame which has
‫all the registered Zaremba and we saved them here, you see, and it all got saved.
‫OK, so so where access in the stack as it was the moment that they called in interrupt on us.
‫OK, so that's what we're accessing here.
‫So back to Tasket Stack item then and we're just going to go tasc page
‫tasc, OK.
‫We haven't created this function yet and we're just going to provide the task in here.
‫OK, switch to the tasks page to the given tasks page.
‫So we're not switching to the current tasks.
‫Page was switching to the tasks page of the task provided to us.
‫OK, and then we should do Colonel Page by here.
‫To switch back to the colonel page.
‫OK, we're going to return the result here and we're going to set the result in between these two in
‫between Task Space Task and Colonel Page.
‫We're just going to go Resul equals Voie Point, a cast Hesp Poynter Index.
‫OK, so that's how that works.
‫So let me explain this then.
‫All right.
‫So we go in here, we grab the stack pointer of the current task.
‫OK, all of the tasks provided to us, the task by here.
‫Right.
‫We grab the stack pointer, we switch to their page directory.
‫So the paging system changes from the COL page to their to their page, OK.
‫We then we then know that if we access their stack points, we will be access in this stack.
‫And we know this because we've changed to the task directory.
‫OK, so to the page directory.
‫Sorry, not task directory.
‫Page directory.
‫We change to that page directory.
‫So, so we know that we can access Stackpole a directory now.
‫So we go Aspey Pointer Index.
‫OK, and, you know, if they pass in zero, we'll get the most recent element they they pushed to us,
‫if we pass in one, as the index will give us the next most recent item they pushed to us.
‫So remember, the stack goes downwards.
‫OK, so when you add to the stack, you push down and when you pop from the stack, you push up.
‫OK, so that that's how that's working.
‫All right.
‫Yes.
‫So then as you know, we just switch back to the kernel page so everything's back to normal and the
‫kernel is ready to continue whatever it's doing once it returns from this function.
‫So, OK, you've made that brilliant.
‫Let's copy and paste that into test.
‫OK, just by here.
‫OK, and that allow us to access it perfect.
‫OK, so we're now just going to go to Miss Dot, see, OK, and this is our some function and we're
‫simply going to go in it V1 for value one.
‫I want to I should say in it V two equals task and you can see we can't see it there.
‫So we're going to have to include that.
‫Right.
‫Includes task test stage.
‫So we're going to go TASC.
‫Get snack item, all right.
‫I was just going to go TASC Curran's because we only care about the current Tharsis running, right?
‫That's the task that called on some function, after all.
‫And because we're only mutualise one call, one C.P.U call, anything in a kernel interop, we can rely
‫on the task code because we know it's related to that task.
‫OK, so here we're going to access index one, which should give us the second argument first.
‫And here we're going to go in it V1, which should give us the first argument first.
‫And can you guess what number we provide here?
‫Zero, of course.
‫And remember, this returns of only pointis.
‫So we're going to have to cast this to an integer, right.
‫Because we want an integer here, not a void pointer.
‫Right.
‫And we're just going to return V1 plus V2 because this is a some function and that should be it for
‫a command zero.
‫However, we still need to make the task page task function, which, as I've already explained it,
‫changes the page directory to point to the tasks page directory, the one that you provide to it.
‫So if we just search for Tess Page in here and we're going to make it underneath here.
‫So this is very easy.
‫We're just going to go in it.
‫Tess page task, struct task task and we're going to go use the registers, OK, which switches all
‫the segments just to the user ones.
‫And then we're just going to go paging switch task page directory or just Claverton zero.
‫And that should be it for that.
‫Guys, we're going to copy the prototype and put it in tasks and just just on the test page here.
‫OK, let's just compile this and see if that compiles.
‫Fine.
‫So we're just going to go make clean bill to S.H..
‫And we can see there's a slight problem here, but it's very easy to fix that Gothamist not see, we're
‫actually seeing a viewpoint pointer here and here.
‫We try to return V one plus V two.
‫Obviously, that's not possible.
‫So we need to cast to avoid pointer and just put parentheses here.
‫OK, so we go V1 plus V two and we cast a viewpoint.
‫Now you guys should understand the C programming language enough to realize that we can return integers
‫and avoid points up.
‫And, you know, these types are just to help the compiler and we don't really have to obey them, you
‫know what I mean?
‫That we can return V1 plus V2.
‫That's an integer.
‫And then we cast to avoid points of the C, the C compiler will see that we're trying to point to some
‫address of V1 plus V V2, whatever the result is of that.
‫Right.
‫But that doesn't mean we have to access it as a viewpoint.
‫It can be cast back into an integer at another time.
‫OK, so that's why we return Voynov here so that we can return every possible type.
‫OK, so we're going to build that now.
‫And that builds absolutely fine, and we now need to do some other things here, we need to map the
‫stack of the user process because we're not currently doing that.
‫So if we just go to process, start see and you'll see process, map memory, we map the memory of the
‫actual process data that we loaded, but we don't map the stack.
‫Now, that's no good because obviously every program needs a stack that they can push and pull from,
‫OK, because we don't grant access to that memory.
‫The task will page fault if it tries to use its own stack and this can be demonstrated in on see, you
‫can see that we show you.
‫You can see we give access to the whole four gigabyte address space, OK?
‫But we only allow the pages to be present and accessible from all.
‫We don't allow right access.
‫So the user space application can view the entire address space, but it doesn't have write access.
‫OK.
‫And yes, I understand that's a bit of a security flaw, allowing them to access the whole address space,
‫by the way.
‫But, you know, this is a training exercise for now.
‫We can worry about this stuff later on.
‫Perhaps so.
‫Yes, you can see here that we have the paging you for gigabyte and we map the whole address space for
‫this task linearly to be present and accessible from all.
‫There's no right flag here.
‫OK, so the task is not able to write to its own stack because we haven't remap that memory yet.
‫Now, if you go to process, start see.
‫You can see that we do remap the binaries memory from the virtual address, 400000, OK, to the size
‫of the stack size of the program size, I mean.
‫So we do map that and we do map that as present, accessible from all and rideable.
‫So we need to do the same for the stack memory, because if we don't, as soon as the user program tries
‫to push to the stack, it will cause a page fault.
‫OK, so we need to fix that.
‫So just by here, then we're going to say if rest's below zero, then there was an error.
‫Go to our I'm going to have I here, OK?
‫Otherwise, we're going to go paging map two and we're going to pass in the tasks page directory.
‫We're going to pass in the virtual address.
‫And is this the stack end of the stack start, guys?
‫If you said the stack stopped, you got it wrong, it's the stack and because the stack goes downwards,
‫OK, that's why.
‫So we need to map from the end of the stack to the start of the stack.
‫OK, so let's just pretend the end of the stack selects 1000.
‫The start of the stack is 5000, just as an example.
‫So that's why we map like that.
‫OK, because the stack grows downwards each time it pushes.
‫Now we're getting a map from a physical address.
‫Now, what's the physical address?
‫Well, it's process stack, the physical address memory.
‫And now here we're going to map to the physical end.
‫So what's that then?
‫Well, it's paging a line address because we wanted to align to the next page up and it's going to be
‫process stack plus.
‫STAC.
‫Sighs OK.
‫That's what it is.
‫OK, and now we need the flags.
‫OK, so what are the flags going to be?
‫Well, it'll be paging his presence.
‫Paging access from all I'm paging is writable, OK?
‫That's what it's going to be.
‫And this rideable one is especially important because we want to give the process the rights to right
‫to its own STAC.
‫You know, the real address is stored in the Stackpole is stored in the stack frebel here.
‫OK, the virtual address that we're giving you access to starts from here, ok.
‫The program start address minus programs stack size, so we're mapping from the end address.
‫To the.
‫Programs stack size.
‫That's what we're doing here.
‫OK.
‫So that's how that's working, guys.
‫OK, so.
‫Yeah, that should be it.
‫Let's just make clean and build the research to see if that works, are we going to see a little bit
‫of an error argument to.
‫Makes points from year without a cast that's so easy.
‫OK, that's simple mistake INSEE definitions are integers.
‫OK, so we need to cast that to avoid pointer, OK, because that expects of a viewpoint.
‫It wants a viewpoint.
‫And we pass an integer because we pass the definition.
‫So we're going to rebuild that.
‫And now everything works fine.
‫Perfect.
‫So now we're going to test what we've done and see if everything works.
‫OK, OK, so we're going to go CDB in GDB and we're just going to break askaris Eurex 400000, OK,
‫and now we're just going to go target remote kumu.
‫So we're piping it I 386 ideas stopping dash capitalist task gdb WDIA.
‫Pressie to continue.
‫OK, we now broke we're on the USA program now, so go Leo ASRM, you can see this is I use the programs
‫assembly.
‫We're going to step by.
‫You can see that push to the stack step.
‫I again push to the stack step by again.
‫You can press enter to rerun that command.
‫By the way, you don't have to keep typing step by.
‫And we're about to invoke the colonel routine now.
‫OK, so we don't really want to step through that because we have an attached symbols.
‫So we're just going to break on the address after it where we're adding this the stack point eight to
‫the stack point.
‫So that is address asterisks 400000.
‫Uh, B, it looks like A, B suppressed C and now we're going to proceed to continue.
‫So now for the main test, we're going to go print X and look at that.
‫We see 50.
‫Why do we see 50?
‫We see 15 X register because we push 20 and 30 here.
‫And if you see some 20 and 30 together, you get 50.
‫So we've now made our first colonel command.
‫And these commands can be absolutely anything.
‫Guys, it could be pointing to the terminal.
‫It could be getting keys from the keyboard.
‫We provide the interface to the user programs to call us through the interrupts, which is exactly what
‫we want.
‫So, yeah, what's literally happening is our handler here is a command zero.
‫Some is getting cold and we are getting the tasks Stack Eitam, the ones that they push to us, those
‫two integers they push to us index unindexed one, we add them together and return the result that then
‫goes all the way back to the kernel, all the way out back to our user program.
‫In the register, so congratulations, this is a very simple command commanders can be anything really.
‫I do advise that when talking with the colonel in New Zealand, it should be for very abstract purposes.
‫You would never make us some function in the colonel like this.
‫This was just a sort of hello world to introduce to you, you know, examples that are acceptable things
‫like open functions, write functions, read functions.
‫They are perfectly acceptable.
‫OK, because then you can write to study out, for example, OK, so things like that.
‫So they're acceptable use cases.
‫Exit function in the colonel is also acceptable use case in this course.
‫We're just going to use it for everything because after this course you will know enough knowledge to
‫carry on by yourself and make much more in-depth programs.
‫And a colonel, of course, so well done on making this lecture.
‫I can understand that some people might be confused.
‫I tried to explain it the best I can in this video.
‫However, if there's a particular problem that you're facing some confusion, please drop me a Q and
‫a message and I can assist you.
‫So, yeah, essentially the tasks stack is different from the colonel's because the Stackpole points
‫somewhere different.
‫All right.
‫That's why we allocated the memory for the use of STAC in the first place.
‫So, yeah, if if you're confused, just let me know, guys.
‫I'll try and help you with that.
‫Thanks for watching.