‫Hello and welcome, everybody, and in this lecture, we are going to actually create a colonel command
‫that allows us to start processes.
‫So if we just go to process, start see, you'll see process load in here, we're essentially going
‫to make one of these in use a land.
‫OK, and what happened is it slowed the process and then it will switch the task so that it runs the
‫new process that was loaded.
‫So we're going to create that in this lecture.
‫So I know it seems complicated, but it probably won't be too bad for us because we've already made
‫over functionality.
‫You see process, load, switch and all that sort of stuff.
‫We've already made it all.
‫All right, guys.
‫So it shouldn't be too difficult to to get this working.
‫So what we're going to do then, we're just going to go to the ISI, the Hajj, and we're going to create
‫a new file called Process Start Hajj.
‫OK, and in here, we're going to say if and if ISI, the Hajj process, Hajj, the fine, ISI, the
‫Hajj process, Hajj hashtag indef.
‫OK, and we're just going to create a new file in here again called Process Dot.
‫See, uh, you guys saw that coming and we're going to include process w.h.
‫OK, so here we're going to put the the process load start command.
‫OK, now just go to Miss Stazi and we're going to copy and paste this prototype here.
‫So we, we have something to work with and it's going to be Comen six.
‫I think so.
‫Izadi Hajj commands six process loads start OK and we're just going to go voi pointer filename use pointer
‫equals task and we're going to need to include that guys just include task task start Hajj.
‫Get stack item task currents and we're going to take zero, so that'll give us the filename version
‫of this pointer, OK?
‫Now we obviously need the kernel version of the file name because as I've said plenty of times, this
‫is a virtual address pushed to the stack.
‫So we can actually use this value.
‫We have to copy it into a kernel address.
‫OK, so we're going to go char filename and in here we're just going to include status dot.
‫OK, and I think we have a max path, not a state, it's not a config, which just include both of them.
‫So I think we have a max path.
‫Yes, we do.
‫So Peatross max path.
‫OK, so now what we're going to do is we're just going to go copy string from task and we're going to
‫pass in the file name first.
‫First we pass in the task the current task and then we're going to pass in the the file name, use a
‫pointer.
‫That's the first thing C and then the file name and then finally the Max Bitts to copy, which is the
‫size of file name and we're just going to go in.
‫It equals that OK.
‫Now if the rest is below zero, we failed and we should just return zero here, OK, because we fail
‫to load it out or we could even just return wrests.
‫OK, so I guess we could go to I just to go to our and we'll have the label here where we return.
‫REST's perfect so we fail to copy the string go to out.
‫There was a problem.
‫Now to be completely honest, I know that I'm returning values here, but on a successful start of a
‫process, we could have a few issues.
‫And I'll tell you why, because when we go TASC next and then we drop back into user land, essentially
‫we're never going to return here.
‫So if everything went OK, we're never going to return.
‫However, when we go back to the other task, because the process is terminated or whatever, it will
‫continue running after and it's 80 and that's because we save the registers upon entry to ISI DH.
‫OK, but we wouldn't actually be returning from here if we drop into the end to use a land, which we
‫will have to do because we're going to due process slow start.
‫We're basically wanting to switch the task here.
‫OK, now we multitasking environment.
‫It would be a bit different because what we would do is we would just returned from here if we were
‫able to load the process or not.
‫OK, and because it's multitasking, we won't have to block or anything here.
‫We're technically blocking when we start the task.
‫Because what?
‫Because we're not flecking.
‫What are flicking between the tasks?
‫Sawy.
‫So I'm one.
‫The one.
‫The process we're creating here gets killed.
‫Obviously we'll switch back to the other task and its IP will be equal to the address just after entropy.
‫At that it called to stop this process in a multithreaded environment.
‫It's entirely different because because we're essentially we do drop into the the use of land, but
‫because the task gets switched.
‫When there's a new interrupt for the timer, it will then look from our perspective as if the task is
‫gone off and run in the background, OK, rather than a blocking, which makes sense.
‫OK, but you don't have to worry about all of that yet, so don't worry too much right now.
‫So, yeah, we have the string at this moment in time in the filename so we can now try to load that
‫task.
‫So we're going to go rest equals process load and we're going to need to include processes other guys.
‫So do include process, process, stage.
‫So no, it'll be task post to start.
‫I mean, so we're going to go process load and I think it's load switch.
‫Yes, load switch guys.
‫So we're going to be a process load switch.
‫We're going to pass in the filename and we're going to go ampersand process.
‫OK, and I, we're just going to go struct process process equals zero.
‫OK, so now we're going to say if recist is below zero, go to out.
‫So at this point in time we load the process, OK, that's what we do here.
‫So we now need to switch the task.
‫So we just go tasks switch and we're going to switch it to process task.
‫Uh, and that will switch to the that that will switch our task to the process is tasks that we just
‫created.
‫If we like process, load, load, switch, we can see that process switch, which just changes the
‫current process.
‫It doesn't change the current task.
‫Okay, guys, so we do have to do tasks, switch, OK, and once we've done tasks which we can then
‫drop into lower.
‫Bridges, so we drop into lower privileges very easily.
‫We just go tasc return ampersands process a task that registers and that will obviously change all the
‫CPU registers to the task.
‫We just created and it'll drop us into the lower privileged state.
‫We will never be able to return from here.
‫The execution flows completely changed at this point.
‫It's now executing the new tasks that we've just created, a new process.
‫Now granted, we could probably make a separate function for both of these and then call that it could
‫be something like a task, lower privilege or task, begin running, something like that.
‫I'm just thinking of things off the top of my head.
‫But I thought it would be best to show you how this is working a little more clearer.
‫So we switch to the current task, which simply put, all that does is it essentially.
‫Switches the current tasks and switches the paging directory, so that's what that does, OK?
‫And then we do test return, which, as I said, is an assembly instruction.
‫It basically switches all our registers to the ones we've specified here and then drops us into a lower
‫state.
‫And it will begin executing our task.
‫Because if we go to task today, Assen, because I know it's been a while since we covered this, so
‫I want to show you, you can see the task return.
‫It basically goes back into use a landmark and it pushes the IP in the registers we provided so it knows
‫which address to run.
‫And because we did switch to switch the paging, it's guaranteed that the virtual address is 400000
‫now points to the second process.
‫OK, and obviously when we switch back to the other task, then what happens then is and will continue
‫executing the other tasks.
‫OK, so so that's how that works.
‫We can play with the registers and that allows multiple processors to share the same virtual addresses.
‫OK, just by flipping the page directories and the registers, we can run multiple processes.
‫So OK, so that's all fine.
‫That looks like it will work.
‫It will work.
‫OK, so what we're going to do here now, then we're going to go to ASIC and we're going to go system
‫command six
‫process loads start just like that.
‫OK, and I'll go to ASIC to start see and we're just going to include process dot Hajj and then we're
‫going to go inside the register command system command six.
‫Process Loadstar, I said, hey, come on, six process loads start and we need to just put that in
‫the head of our guys to go to process.
‫See, we're going to copy and paste this prototype straight into process, start Hajj.
‫And we also need to forward to clear structure into a frame as well, guys.
‫OK, so it should look like that.
‫OK, so we can SkyCity Hajj includes process start Hajj, which registers the command process.
‫Loads start to come on six, which is in which is in the enumeration.
‫So that's perfect.
‫Congratulations on that.
‫So so we now need to add process Stazi to the makefile.
‫So just go to the makefile and we're going to search for Izadi.
‫Hey, I found it here.
‫Just copy and paste that and we're going to go process that O process, see process Stazi and finally
‫process start.
‫Oh, and we're going to copy and paste this into the files at the top here.
‫OK.
‫Just by there, so you'll notice that we've named it process, you know, we have to start see in the
‫task directory and then I say it's because I'm trying to keep it clear what's going on.
‫We know that in our society directory, everything in process, Stazi is responsible for processes.
‫So, you know, naming things correctly is important.
‫OK, so that's why we're doing that.
‫So, yeah, just make sure you don't forget to put the file name here at the top.
‫OK, in the object file dependencies, the files label here.
‫So we're just going to go make clean bill to the stage.
‫And we can see that there's an era that's no problem, go to the Stazi and we have to return the value
‫as a cast.
‫OK, now.
‫To be honest, let's just return zero because there's nothing we can do.
‫We can't actually, uh, return any value at all, because if it was successful, then we can't return
‫the value anyway.
‫So that's because this cow's never run, you know, unless we artificially change the X register before
‫we switch to the task.
‫But it's just pointless, you know, to just return zero there.
‫And if we want to worry about errors, we can do that in the future.
‫So just run Kumu here, check everything runs OK.
‫We can see everything still works as before and that's now compiled.
‫So perfect.
‫So now we're going to have to create some stuff to test.
‫Come on.
‫Six.
‫OK, so what we're going to do then is we're going to go to our study lab or DSM and we're going to
‫start writing code for that.
‫OK, so just to the bottom here, we're going to say, uh, Peatross process load start, OK?
‫And we're going to do a comment for how it should look.
‫So it's going to return void Peatross process lodestars const char filename.
‫So that's how it's going to look.
‫OK, and granted it's not a very good function.
‫I mean really it should be returning errors and stuff, but it's much more complicated for us to get
‫that right because we have to artificially set X register because as soon as we drop into user land
‫we, we've lost our stack trace, we we can no longer return from that command, you understand.
‫So you need to be very careful with how you manage that.
‫So for now, it's best we just have void.
‫So we're going to go push EBP movie BP E.S.P.
‫Uh, move six commands, six process load starts.
‫OK, uh, starts a process.
‫There you go.
‫And here we're going to go push the word EBP plus eight variable filename and at zero eight add ESP
‫for top return.
‫So as soon as we go into in Zaydi and we passed in command six, as I've already said, the process
‫will stop.
‫We won't return from this interrupt by the way.
‫We want, uh, however, because our registers were saved upon entering this interrupt.
‫As soon as that, as soon as our task gets run again, it'll execute from nine seventy one here, OK,
‫because our registers were saved.
‫So that's what matters.
‫OK, but yes, we won't actually be returning from that interrupt at all.
‫OK, the execution flow will be gone, it'll be gone to the next process and as soon as the next process
‫ends, we can just continue executing here.
‫But it's very artificial, OK, because as soon as we go to task next and we drop back in the usual
‫and again when the process is killed, that will then pull the IP from the registry structure and the
‫IP will just happen to be on line seventy one here.
‫So we're not actually returning from interim Zaydi, although it will give the illusion that we are,
‫but we're not.
‫And it's very important to remember that because we can't return values the same way as we can with
‫the other commands.
‫OK, because we're not actually returning from the interrupt, we're just changing the execution flow
‫and then changing the execution flow back.
‫OK, so that's all that's happening here.
‫We're manipulating the execution flow.
‫OK, so just copy and paste this label, go to the top and we're going to go global and a pace that
‫in and we're going to do a function.
‫OK, and we're now going to scroll down.
‫We're going to copy and paste that prototype here and we're going to put it impetuosity.
‫OK, so that we can access it through.
‫See, OK, so let's go to show that C and when we've successfully read in a line, we're now going to
‫go process a load Peatross process load start and we're going to pass in the buffer as the file name.
‫OK, we're not dealing with any command documents or any aardvarks VRC stuff right now, OK, which
‫is loads in the process.
‫We don't care so much about that right now because it's more complicated.
‫Right.
‫We need to keep things at a certain level and build up to that.
‫So we're going to go make clean build that S.H..
‫CD been Kumu.
‫And I just type something here and we can see it doesn't return control.
‫OK, uh, now that is a bit of a problem because that shouldn't have succeeded, OK?
‫Because that was an invalid program.
‫So I'm just going to use the debugger, find the problem for you and I'll tell you what it is, OK?
‫And, um, after debugging, I can see very clearly what's going on in our the line.
‫One hundred and eighty Wiecek if recist below are equal zero and then we go out.
‫However, in the case that f hope and return zero, it means there was an error.
‫OK, so we should be returning minus EIO.
‫So once you've done that, let's just make clean and build again.
‫KDDI been kumu, and now we can type something and we can see that nothing happens, it just returns.
‫Now that's expected because in this case, the function command is returning.
‫Command six is returning, OK, because, uh, process loads, the switch has an error.
‫So we just jump past the task switch and we return zero.
‫Straight away.
‫OK, but what we're going to do is we're going to artificially write some code here, OK, complete
‫artificial code that acts as kind of like an environment variable, like a path variable.
‫Now, bear in mind, ideally, you should do this properly and create some sort of environment variables
‫type of thing.
‫OK, but basically we don't have to put in the full path, OK, if we don't want to and we can get around
‫that by simply spending the drive.
‫No.
‫So test itself will become that OK.
‫And that will ensure then it can resolve it.
‫OK, so we're just going to do that.
‫But ideally you should make some sort of environment variables.
‫But after this course you'll have all the skills you need to start writing that particular functionality
‫and improving your own kernel.
‫You probably already have enough to get going on your own.
‫Now, however, I will take you through this a little bit.
‫Now we're going to we're going to make a temporary buffer where we'll put the zero and the drive number
‫here with the file.
‫OK, so then that will allow that to run.
‫So follow what I'm doing.
‫So we're just going to go Char Path, Peatross Max path and then we're going to copy in, uh, the actual
‫drive number.
‫So let's just go up here a minute, OK.
‫And we're going to actually include a string string string dot Hajj and we're going to go string copy
‫here, OK, and we're going to copy into the path zero forward slash like that.
‫OK, just like that.
‫OK, so now we're going to copy our file name.
‫So we're going to just go man copy or string copy I should say string copy, uh, path plus three.
‫OK, I'm going to copy the file name straight in there.
‫So why plus three.
‫Because we have three bytes here.
‫Now anyone can tell you this code is quite bad here on line 19, 20 and 21.
‫As I've already said, we should be having some sort of environment variables and then we can do some
‫sort of get path command, OK?
‫And in that get path command, if you pass some sort of just some sort of relative path like test study,
‫it'll go through and find the path variable, combine the root path here with your relative path and
‫then return that to you.
‫You know, that would be a clean way.
‫So I'm just guiding you here how to clean this up if you wanted to.
‫But my job here with this course is to teach you how to understand operating system development.
‫So anyway, let's continue now that you've done that.
‫So we should be loading the path now, not the filename.
‫So just change that the path, OK, and everything should be OK.
‫Now, suggest you make a clean build that S.H..
‫OK, and then once you've done that, we're going to say KDDI been kumu and we're going to put.
‫Lanctot Alphin here and press enter and what do you see?
‫My age is 98.
‫Congratulations.
‫You just run another process.
‫OK, you'll notice a handsome return control to us.
‫This is for a few reasons.
‫The first reason being is that Lanctot Elf is in a while loop.
‫OK, if we go to blanked out ELF again in our programs, you can see it's in a it's in a while loop.
‫Right.
‫Even if it did return.
‫I'm sorry guys, but you ain't going to return to this terminal.
‫OK, so this original program and the reason being is we don't handle program termination yet, OK?
‫The way the program should terminate is inside our assembly in start Dadaism in the in the standard
‫library by here after we call main rather than just return because we can't actually return.
‫There's nothing to return to.
‫Right.
‫We would just have an exit.
‫So we would call some sort of exit function in our standard library, which will basically maybe use
‫command zero.
‫We can get rid of the command zero sum and change it to commands.
‫You exit OK and basically invoke the command, which would terminate our process and switch to the next
‫available task.
‫And that's how it should be done.
‫And then our return will work.
‫So in bankruptcy, when we return zero here, then it would work OK, but not at the moment.
‫Not at the moment.
‫It's not going to work right now.
‫So we will implement the exact command soon and then we'll be able to terminate the processes, terminate
‫the process.
‫It can be a little tricky.
‫It's not so bad, but it can be a bit a bit tricky because we have we have to delete all the memory.
‫We have to care whether it's an alpha or not.
‫We have to detach the pages of the task.
‫You know, when you kill a process, you have to kill all the tasks with it.
‫But since we only support one task per process, it won't be too difficult to do OK.
‫But yes, essentially we need a function for terminating the process and we also need to handle program
‫crashes.
‫OK, now that's that's done in the interim Tandler, because if we go to I don't see inside this interim
‫Tangela, essentially one one, an exception is thrown away.
‫We will kill it.
‫OK, simply put, we will just kill it.
‫And you can find the list of exceptions at Osterberg.
‫I can show you that.
‫Now, you're already aware of what they are.
‫Uh oh.
‫Still have exceptions.
‫Yes, so all of these up until 06, 20, OK, there's an error.
‫OK, there's more errors by here, but we're not going to even bother dealing with those.
‫We're not even going to worry about it.
‫OK, we'll worry about these exceptions here.
‫Divide by zero, debug breakpoint, overflow invalid of code, double file invalid tests.
‫Any of these that get flagged.
‫We will check if we're in the kernel land or not, because if it was the kernel code that broke, then
‫we panicked.
‫The system absolutely panicked.
‫If we don't do that, we risk corrupting something.
‫If we was to be writing things to disk, which we don't.
‫But still, if we did decide to write things to disk in the future, we could correct something if we
‫don't deal with that error correctly.
‫And the correct thing to do is to panic the system to to avoid potentially bad code running, OK.
‫However, if it was done inside a process, OK, if the error was done inside a process that we know
‫that the process is just dodgy, it's done something bad, it's crashed or whatever.
‫So then we just terminate the process and we do this by erasing its memory and removing it from the
‫task list, essentially.
‫Right.
‫So it can't be run again.
‫And if it was the running task, which it obviously would be because it caused the exception, then
‫what we would do in that situation is switch the task.
‫We switch the task before we kill the process.
‫OK, when we return from that, that task will never run anymore.
‫It's process.
‫Memory is deleted.
‫OK, so that's how we handle the crashes.
‫We basically unloaded and make sure that it isn't the current task anymore and we unloaded by just deleting
‫the memory using a free.
‫So yeah, that's how termination works.
‫And the idea is simple, but it does need to be implemented.
‫OK, and you need to take care with doing that as well because certain exceptions are expected like
‫certain page faults and certain situations.
‫But in our implementation, any page felt we will classed as an error, OK, as we expect all pages
‫to be mapped at all times.
‫But in situations where maybe you want to page fault to allow you to load from the desk, then that
‫wouldn't be an error.
‫Right?
‫So these are things to consider in case you develop your kernel further.
‫But thanks so much for watching, guys, and I hope you're very happy now that you have a shell that's
‫able to run other programs.
‫So we'll soon focus on terminating the actual other programs and allowing the shell to execute.
‫So take care, everyone.
‫Thank you for watching.