‫Welcome back, everybody.
‫So we're going to now start creating a new kernel command that basically acts as a system command,
‫you know, the old system command you can use in the C library system and you can provide the command
‫and the it gets executed.
‫Well, we're going to be implementing a system command today that can deal with that correctly.
‫So just go to isolated dot org and we're going to go system command seven system command.
‫OK, or I'll tell you what, it could be invoked.
‫System command, we'll call it, OK.
‫And, um, in Cyprus, it starts c we're going to create that command.
‫So just down here, we're going to say, uh, Voyt point to Izadi, hedge command seven was a command
‫seven.
‫Yes, it was invoke system command.
‫And we're going to go straight into a frame frame and we're just going to return zero here.
‫OK, so I now want us to just head over to the standard library again yesterday and we're going to copy
‫and paste command document here into processed Hage and Colonel Task.
‫OK, and that's pretty important.
‫All right.
‫We're also going to create a new structure here called process argument, OK, like this.
‫And it's going to contain an integer of oxy and a char point travaglini, OK?
‫And inside out process structure, we're going to have this process argument.
‫And you know what?
‫We should call it process arguments, actually.
‫And it's a double the pointer here, not a single one.
‫That was my mistake.
‫OK, so two asterisks here for Aragvi and name it process arguments.
‫And all we're going to do is go the process now, the process structure and just under the keyboard
‫buffer, we're going to go struct process arguments, arguments.
‫OK, uh, the arguments of the process.
‫So the idea is that when our process runs, we will be able to essentially call the kernel command that
‫will return this structure to us.
‫This process argument structure so that we have direct access to Öcsi pornography.
‫Now, that's one way of many another way to do this would be to actually push on to the task stack.
‫OK, you can do it that way if you wanted to.
‫However, I've done it that way before, and I can tell you it's a bit more difficult to get right and
‫it's not so clean.
‫OK, this is much cleaner by having a command where they can say, give me the process arguments, OK,
‫so we can then call that command in, let's say, start today, a seminar start label and then pass
‫those arguments to the main function.
‫OK, so that's the idea.
‫And it's much cleaner guys and that's the way we're going to do this.
‫And it's the same reason why we've put the struct process arguments here in the process is structure.
‫OK, head over to process start C and we're going to start writing some functions.
‫So we're going to start with making a function that will extract our process arguments from our process.
‫So we're going to go void process gets arguments, struct process, process in it.
‫OK, see this is a pointer and char arg v.
‫And we're just going to go ask risk oxy equals process arguments, see, and then we're going to go
‫ask we ask cosac V equals process arguments to be OK.
‫And that's how that's what guys, we're going to copy and paste this and put it in persistent hate.
‫So that's accessible outside of process to go back to process.
‫See we are now going to implement the injection function that's in charge of injecting the arguments
‫into the process.
‫We're going to go in it process, inject arguments, start the process, process destruct command document
‫route argument, OK.
‫We're going to go and address equals zero return address.
‫Now here we're going to go struct command document, current equals rule document and it equals zero
‫and it occy equals process count command documents.
‫And we're going to make this function in a moment.
‫Make sure you pass through a document that when I now going to make that function, we're going to go
‫in it process count command documents, struct command document good argument struct command document
‫current equals rule document while current currency equals current next.
‫OK I plus plus and now we're going to make the variable by here and try to zero and then we're just
‫going to return I down here and that will count the arguments for us.
‫When I was going to say if the agcy is equal to zero then grass equals minus will go to out and we're
‫going to have an output level down here, guys.
‫Now we're going to say char aragvi.
‫So we're actually creating the OK three pointer now equals process.
‫Mallak process size of const char.
‫OK, make sure that's the actual.
‫Like this, yes, size of Consed shot and then and I'm here will have an ass and ask this for multiplication
‫and we'll do OK, see OK and just give it to that one.
‫That's a little tricky to see.
‫That's a bit unclear.
‫But yes, you can see it, it's size of const char pointer and then we multiply it by Öcsi.
‫OK, so that's what's going on there.
‫And you might want to put extra brackets here just to make it a bit clearer what's going on now.
‫It doesn't make it clearer, does it?
‫Anyway, that'll work.
‫OK, and essentially we're making enough memory to store the total number of arguments that we have.
‫OK, so we know that ARG V is a basically an array of pointers.
‫OK, so we're doing the size of Concertgebouw point A multiplied by the total arguments, OK, and that
‫will ensure we'll have enough memory for all of the pointers for each of them.
‫So if the Aragvi is invalid, you know, we fail to declare the memory brassicas casino men go to out
‫and put a semicolon there.
‫Two guys if you haven't already.
‫So now going to go while current
‫char arguments string equals process Mallak process size of current argument.
‫OK, so we're creating a string of 512 bytes.
‫OK, and that will store the entire argument.
‫OK, so here we're going to say if we fail to make the argument string rather Sequenom
‫go to throughout.
‫So I know we're potentially storing more than we need to in this allocation process malac.
‫But if you ask me, it's just an easier way of doing it than checking the size.
‫You know, this is a simple solution to to a problem.
‫And because our Marlock is always going to allocate at least four thousand eighty six bytes because
‫of paging will may as well just allocate the size of the command document, which is five, 10, 12
‫bytes.
‫And that way we can store any string we like in the four any argument provided to us.
‫So here we're going to go string and copy argument string, current arguments, size of current arguments
‫so that we'll copy the entire string across into our arguments.
‫String pointer we just created.
‫OK, so we're going through all of the command arguments and we're basically creating new memory and
‫we're copying them across.
‫And then we're going to put them in the odd v pointer that we made on line one five seven here.
‫And we're going to do that now by going Argh!
‫V equals arguments string and then we're going to Curran's equals current next A plus plus.
‫And now the final thing to do is to set the process arguments, Occy and aragvi structures.
‫So we go OK, ecosoc c process arguments dot org v equals aragvi.
‫OK, so that's perfect.
‫So this function will take any command argument.
‫We pass it and it will create an and cienaga out of that and it will store that in the process.
‫Arguments for us ready for a new command we can make in the future that will extract those process arguments
‫for us and tell the user process what they are so that they can access them.
‫Because we use process MALLAK, we're guaranteed to be able to access that memory.
‫There's no need for us to do any memory mapping because process matlab does that for us.
‫So you've now created the process, inject arguments, function, copy and paste that into the header
‫file.
‫We're now going to go to Canada at C and we're going to create an example command argument that we will
‫pass to shelled out Alaf.
‫So just after we load and switched Jesco struct command document argument arguments dot next equals
‫now and we're just going to go string copy argument that argument.
‫Testing.
‫OK, and then we're going to go process, inject arguments, process and present arguments.
‫So now, just before we run that task, we should find that the argument is injected correctly and we
‫can see we handle the errors here as well.
‫Perfect.
‫OK, so now that you've done that, just scroll up to ASIC page and we're going to create a new command
‫here, system command date, get program arguments, OK?
‫And this will be in charge of turning that process, that Hajj arguments values to us.
‫OK, so once you've created that command, go to I say the that and we're just going to go ISI to register
‫command system command seven nonono system command eight gate program arguments.
‫OK, and we're going to go Izadi Hajj Command eight GEP program arguments.
‫And we also need to make one four copy and paste that.
‫We also need to make one for system command seven, which we haven't done yet, which is from Vokes
‫System Command I Asadi Hajee Command seven.
‫Command seven, yeah, we can see that it's not exploded in the head of fire yet, go to start see and
‫just put Izadi Command seven invoked system command in the header file, OK, so it can be seen and
‫go back to Izadi and then we should be able to do it.
‫So I Assadi command seven invoked system command.
‫We want to register that command and we also want to register the command for command date, which we
‫haven't created this function for yet.
‫So once you've once you've done all that code to start see here and I say d.H and we're going to paste
‫that here, OK, just like that.
‫And we're going to create the placeholder for that so we can see invoke command doesn't do anything
‫yet.
‫Will implement that later.
‫Let's focus on get program arguments for now.
‫OK, so in here we're going to go struct process process equals task current process struct process
‫arguments, arguments equals task, virtual address to physical and we haven't created that function
‫yet.
‫It will take the virtual address the user space provided us and it will convert it to a physical address
‫to the kernel can use the points to that same memory.
‫OK, and now we're going to go tasc current tasks currence get stack item zero and no, we don't have
‫that function the way it should be a task get stack item uh task currence zero.
‫So that little function I just did there, that was an abstraction.
‫You might want to consider doing that for yourself, but this one is completely fine.
‫So as you know, task at Stack Item gets a stack item from a given task stack and we pass our current
‫task in here.
‫So we're getting the current stacks.
‫First item on on this stack.
‫Well, I'm going to take that virtual dress that we that we get return to us using tasket stack item,
‫and we're going to convert that to a physical address, OK, because the virtual address they provided
‫to us is appointed to the process arguments.
‫OK, now you know, the kernel page tables a map linearly.
‫OK, so the virtual address is exactly the same as the physical address.
‫OK, so this function will get us the physical address of that tasks, virtual address.
‫And obviously, obviously we will be able to access that physical address as a virtual address in the
‫kernel page tables because we've mapped the whole thing linearly.
‫So it's no problem for us.
‫So after this task, virtual address to physical functions called, we'll have returned the physical
‫address and we will be able to access that directly.
‫OK, so that's what that function is going to do.
‫So we're going to have to implement that function shortly.
‫But before we do, let's just complete this function here now.
‫So to complete this function, we're just going to go, uh, return zero.
‫And just above that, we're going to go process, get arguments.
‫OK, uh, process.
‫And then we're going to go ampersand arguments, OK, and ampersand arguments.
‫ARG v so we're changing process memory here.
‫OK, they pass the process arguments.
‫We convert to a physical address so that we can access it.
‫We the kernel and then we call process get arguments which as you're aware, overwrites arg cienaga
‫the with the ones of the process as you can see here.
‫OK, so that function is complete.
‫Now we just need to create tasks first to address the physical.
‫So go to task start and we will do that.
‫OK, so just by here we're going to go void task.
‫Virtual address to physical.
‫Struck task task, void, virtual address.
‫OK, that's a low point I want to avoid physical address for pointer equals zero task page task task
‫physical address equals paging get physical address.
‫We haven't created that function yet.
‫Don't worry, we'll get to that shortly.
‫TASC page directory.
‫Directory entry and in fact, no, we can just to yeah, no, it should be it should be directory entry
‫really for this is quite a low level function.
‫And we're going to Colonel Page here, which will switch us back to the kernel page, OK, because we
‫switched to the last page here and then we're going to return the physical address just like that.
‫OK, so that's what that looks like.
‫So you can see we switch to the tasks page we grab.
‫Oh, one more thing before I forget, guys, we also need to pass the virtual address here, OK, just
‫like that.
‫So that's going to grab the physical address from the page table and set it into physical address here.
‫And then we can return the physical address just like that.
‫So we're now going to implement paging get physical address.
‫But before we do, just put that in the header file.
‫That prototype there, guys.
‫OK.
‫So head over to paging Dr. C and we're now going to implement that paging get physical address function.
‫So just here, we're going to go void's paging get physical address unit 30 to 40 directory has appointed
‫guys voit virtual.
‫That's point to two.
‫And we're going to say void virt address new equals void pointer.
‫Paging a line to lower page Viotti virtual.
‫You know the virtual variable we're going to go void difference again.
‫Appointer equals void point to cast unit 32 T
‫VRT Viotti minus unit 32 t the address new
‫and then we're going to go return by appointer paging get directory first address new ampersand zero
‫x f f f f f zero zero zero.
‫And then just by here we're going to go plus difference.
‫Now I caution you to pay attention here because if one of these brackets is in the wrong place, you
‫might find the math doesn't work correctly for you.
‫So make sure you pay attention here.
‫So I'm now going to explain this to you.
‫We first align the virtual address to the lower page.
‫OK, we then calculate the difference between the lower page and the virtual address, provided this
‫basically gives us an offset.
‫OK, so it might be one bite into the page, for example.
‫OK, so that's what that does.
‫And we need that for later because as I said, the pages they work on four thousand eighty six byte
‫boundaries, OK?
‫If someone provides us a difference, then we need to take that into account.
‫Right.
‫Maybe they provide the address.
‫Four thousand ninety seven.
‫Now that doesn't align to a page, does it?
‫So we bring it down so the lines to a page, we calculate the difference, which is one byte.
‫OK, because we need that later because we need to append that to the physical address.
‫We get use paging get.
‫So we call paging get.
‫We pull out the all of these bits here, ignoring the flags, OK, and then we just add on the difference,
‫the difference we calculate which was one.
‫OK, so then the address returned will be the physical address.
‫OK, because remember we work on page boundaries, boundaries of four thousand eighty six bytes.
‫We can't deal with values that on page aligned.
‫OK, so we have to convert it to the lower page.
‫But then there's a difference.
‫What if they provide for nine seven?
‫We can't return the physical address for four nine six.
‫So that's why we calculate the difference and we add on the difference as soon as we have the physical
‫address.
‫OK, so that's what's going on there.
‫Guys, if you're still confused about that, send me a message, but try to read that function carefully
‫and make sure that it's correct, because if you get that wrong, you're going to run into problems.
‫So we're going to put this in the head of file for paging Dr. Hage now.
‫And now we're going to go back to test to see and you'll notice I switched to the page task in the COL
‫page.
‫I don't know why I did that.
‫There was no need for that.
‫Maybe a force of habit.
‫So you can get rid of that because it's not needed.
‫So we just need to set the physical address to page and get physical address, essentially, guys.
‫OK, so you can even just go return page paging, get physical address.
‫You know, it's probably cleaner to do that just like that.
‫Perfect.
‫So we're now going to just head over to process Stazi and ISI, the Hajj, and we're going to take the
‫program arguments here and we're just going to put it in the header files so that we can access that
‫outside of process, start see.
‫OK, and now we're going to go make a clean bill just as Hajj and we should see that compound, OK?
‫Yes, it does.
‫Perfect.
‫OK, so we now have command, a GED program arguments.
‫So we're going to test that command now because I cannot see.
‫You can see we passed testing as the first command document we injected into the process of shelled
‫out Alph.
‫So we should be able to access that now.
‫OK, so what we're going to do then we're going to go to our standard library in WSC and we're going
‫to make a function for getting the arguments.
‫OK, so the very first thing is we need to have the same structure to go to tasks, process, start
‫Hajj and we're going to just copy and paste the process arguments here into the process of Hajj file
‫in the standard library just by here, OK?
‫And then impetuous WSM, we're going to actually write that function now.
‫So we're going to go Void's Peatross process.
‫Get arguments, struct process arguments.
‫Arguments, that's the common for the assembly, that's what a functional look like, and we're just
‫going to go PTW process get arguments.
‫We're going to push the base point, we're going to move the stack point to the base pointer, and then
‫we're going to move command eight to here, OK, and then we're going to go push D what EPPY plus eight.
‫And that is variable arguments.
‫It's good to comment these things for your own sake.
‫Guys, I'm going to go interrupt you and we're going to go at Hesp fought to restore the stack pointer.
‫Now we're going to go pop and return, OK?
‫And that is our assembly waiting for getting arguments.
‫So we're now going to just want to copy and paste this and we're going to put that in our.
‫OK, just like that.
‫And now we should be able to access our program arguments with this code.
‫It should work absolutely fine.
‫So remember, it's command date, not command seven.
‫Don't don't be fooled here.
‫We haven't made the assembly routine for Command seven yet.
‫So go to blanked out, see, and we're going to test that out.
‫So we're just going to get rid of this and we're going to go Peatross.
‫Process good arguments, and we're going to provide ampersand arguments and then we're going to make
‫that variable here, so we're going to go struct process arguments, arguments, and that should fill
‫in.
‫So if we now go print F, uh, percent, I and we'll do percent s and then we do a new line and we go
‫arguments that are agcy arguments.
‫Doug V..
‫We should see one and then string by this.
‫So let's test that now.
‫We're going to go make clean build that S.H..
‫Oh, and I need to we need to make that global again, guys, go to Peatross today, scroll up and we're
‫going to go Global Peatross process good arguments and that's a function.
‫And now that we've done that before, we make clean and build that again.
‫I just remembered go to Colonel that sea and you'll notice we're loading Sheldon out.
‫OK, but we're passing our arguments to shelter lf not blank.
‫So this little code example thing we did in blank today is not going to work.
‫So because because we're passing the arguments to Sheldon, that's why.
‫So just change this to blank for a moment so that our blank program is loaded and not the shell.
‫OK, so just do that and now we're going to do a clean bill that S.H. everything should compile.
‫OK, perfect.
‫And we're going to go KDB and kumu.
‫And we see one we see one outputted, so let's just go to blank dot and yeah, we can see that this
‫outputted one, there's a little mistake here.
‫IPASS dog the I should have passed Aragvi Zero.
‫OK, so just change that to V0.
‫We're going to compile and run that again.
‫Citibank.
‫And there we go, look at that now you see one and testing so you can see our colonel is now, now aware
‫of scenography.
‫So I know what question you're asking now is, well, how do we get them to be passed to the main function?
‫Well, as I said before, there's two ways to do that.
‫We can inject it into the test stack not advised.
‫I've tried that route before.
‫It is doable, but it's messy.
‫This is a much better implementation.
‫So our way of doing this will be to have a separate start.
‫That's also written in C..
‫OK, so if you go to us actually live and you go to start today and you can see we've written some sort
‫of start routine in just assembly language, it main.
‫And that said, well, we can make a start loda in C, OK, and call that C start a function, OK,
‫which then gets the arguments and finally calls me.
‫That's one way.
‫The other way is we can do the whole thing in assembly, which will be a bit more tricky to get right.
‫Since we're working with structures, we will make a separate starter program essentially.
‫OK, so it'll be it'll be similar to this apart from we won't call Main, we'll call C and the go start
‫or something like that.
‫And then that will allow the C function C start to take over and that will basically do what we did
‫on Blanchot.
‫C it will get the process arguments and instead of printing them out it will basically just cold mean
‫and it'll, it'll basically pass C and aragvi just like that.
‫OK, so that's what we're going to be doing in the next lecture.
‫And then once we finally have that done, we'll be able to focus on our system command that's currently
‫left blank on its own in the ISI d.H.
‫By here you can see it's currently blank.
‫We'll focus on that once we've done that.
‫And this system command will be in charge of starting the process and inject in the arguments doing
‫two things at once.
‫OK, so thanks for watching, guys, and take care.