‫Understanding, use the land, use the land is a term used to describe when the process is in a limited,
‫privileged state, use the land is what operating system processes run in.
‫Kernel land is the state that the kernel runs in.
‫It is a high, privileged state that's now explaining further what the use the land term actually means.
‫So they use the land term describes a limited process, the state.
‫So if you remember before I briefly went over the protection renderings rings of the process, so you
‫have ring zero and ring to and ring three.
‫Well, use the land refers to ring three.
‫Whilst the processor is in this state, it is unable to execute privileged instructions such as instructions.
‫The change the global descriptor table or the paging table's direct or access using in and out instructions
‫is also restricted use and is safe because if something goes wrong, the colonel is able to intervene.
‫For example, if I do something I'm not supposed to, it generates a protection fault and the protection
‫faults interrupt handler is invoked for that.
‫And then we, the Colonel, are expected to handle that exception.
‫And how will we handle something like this?
‫We would terminate the offending process.
‫So I just want to make clear the use the land is not a special place where process is run.
‫It's simply a term that we use to describe the process when it's in a privileged, limited state.
‫That's not explain the Colonel Lanterman, Colonel, and is when the process is in its maximum privileged
‫status, ring zero.
‫Wilston Colonel Land, any area of memory can be changed.
‫Any C.P.U instruction can be executed.
‫There's also a high risk of damage to the system if things were to go wrong.
‫Courland is when the process is in a privileged protection ring, such as ring zero or ring one or ring
‫two, however, when we talk about Colonel Lamb, we are generally referring to ring zero.
‫Let's now explain some of the U.S. restrictions.
‫Access to certain locations in memory can be restricted for use and processes.
‫We can do this using paging.
‫There's a supervisor bid in in the page tables that allow us to restrict access to the memory unless
‫they are in Colonel Land or essentially a supervisor mode, which which is the same thing as Colonel
‫Land.
‫So we can set the supervisor bit in any page using paging and then that four thousand ninety six by
‫page will only be accessible while seeing Colonel Land.
‫So access to certain CPU instructions are restricted from New Zealand.
‫As I said, we can't talk to hardware directly with in and out instructions.
‫We can't change the page tables and so on.
‫So using paging the Colonel can ensure all processes cannot access each other's memory.
‫And we do this by ensuring that every process has its own page tables and page directory.
‫And then when we switch process, we simply switch the page directory that the processor has loaded.
‫So, you know, when we first loaded the page directory and then enabled paging, while we can essentially
‫flip between the page directories as we swap processes.
‫So if we switch to process B or process C, we can switch the process at to point to their page directory
‫instead.
‫And then that gives the illusion that everyone has their own memory space, OK, because the physical
‫memory is not mapped to the virtual memory essentially address.
‫You see, Rosero could be map to address selects a thousand, for example.
‫They don't have to be linear and we can take advantage of this paging model to ensure that all processes
‫cannot see each other's memory at all.
‫And the best part about this process is can even share the same memory addresses because there's a guarantee
‫they won't overwrite each other because we switch the page directory whenever we switch to the process.
‫So zero x 1000 virtual address might point to zero 2000 physical address for processing, but selects
‫a thousand virtual address might point to zero x 5000 physical address for process B, if that makes
‫sense.
‫So attempting to run privileged instructions whilst in user land i.e. ring three will cause a protection
‫fold.
‫The protection for exception interest.
‫Tangela will then be responsible for solving the problem and in most cases you would terminate the offending
‫program at this stage because the user program shouldn't be trying to run in privileged instructions.
‫So this means that either either an error has happened in the user program or it's trying to do something
‫malicious.
‫So it absolutely should be terminated in most cases.
‫So getting to use the land can be quite complicated.
‫It's when you think about it is quite simple, but when you first implement this, it can be complicated.
‫So I'm going to briefly go through the steps in this presentation before we start writing any code.
‫OK, so if you remember at the very start of the protective code section, we set up kernel segments.
‫So Kernel Code segment and kernel data segment which explained the mapping of the whole memory and the
‫permissions that it had.
‫If you remember, we did that well.
‫User land has its own user code and data segments, much like we have kernel code and kernel data segments.
‫So we need to set those up.
‫That's the first thing we need to do.
‫The next step is to set up a task which segment more will be explained on that later.
‫The final step is to pretend we are returning from an interrupt and we push appropriate flags and data
‫to the stack before we execute an I instruction.
‫This will force the processor to change its privileged state.
‫So step one, setting up the user segment registers.
‫So firstly, we need to change the implementation of how we are loading our kernel segment and data
‫segment.
‫Currently it's done in the bootloader.
‫Ideally, we want something like this where we can specify this in C and we'll pass it through some
‫sort of function.
‫And it would look at these arguments and it would populate the the real structure here, which has the
‫bits all over the place like you see in the in the bootloader structure for the kernel code and kernel
‫data segments we made.
‫So essentially we just want to clean this up a bit so that we can pass values without having to worry
‫about bitwise logic.
‫And we'll have a function that basically reads this structure and converts it to ones compatible with
‫the processor.
‫So that's the first thing we want to do.
‫And you'll notice there's a user code segment, a user data.
‫Second here as well, so we'll be implementing something like this, you also see the TSA segment.
‫We will now explain what that is.
‫So TSA stands for tests, which segment and the response to the tests, which segments structure is
‫to hold information such as the Colonel Stack Pointer and the Colonel Stack segment in the event there's
‫an intercept in New Zealand.
‫What will happen is the task switch segment will be populated with some information and it will also
‫copy the Colonel Stack address that we've stored in the tasks which segment and the Colonel Stack segment
‫script.
‫And it will repopulate the registers in the CPU, preparing to run our colonel interrupt the process
‫that will then push certain things to the stack to allow it to understand how it can get back to use
‫the land and continue executing our user program when we return from the interrupt routine.
‫OK, so that's what you need to understand so far about the tasks.
‫Which segment?
‫So the tests, which segment structure looks a little like this in C, so we can see we have our stack
‫pointer and our stack segment, these are the only variables that we need to worry about in this test,
‫which segment structure.
‫So the final thing to do to get to use the land is we need to pretend we are returning from an interrupt.
‫OK, so, you know, I just explained how when you're in New Zealand and there's interrupt, it pushes
‫certain things to the stack so that when the kernel issues and I read it will run back into use the
‫land and continue where we left off in the user program.
‫Well.
‫This implementation doesn't require an interop to happen to work, so what we can do is we can push
‫to the stack and then issue an irate instruction ourself, even though we're not in an intrepid team.
‫And this will track the process into going into an unprivileged state user land.
‫So how we do that is we set our segment registers to the user data segment.
‫So it's likely to be Zwick's 23 and will set the data segment extra segment FS and G.S. registers.
‫But we will not change the stack segment because we still need to use the stack at this point.
‫So it would be dangerous to change the Stack segment, right.
‫So what we now do is we save our stack pointer into the register because we're about to modify the stack.
‫So now we push the user data segment to the stack, i.e. 063 and we push our stack point that we saved
‫earlier.
‫OK, and now we push the current flags to the stack, but not before we bitwise all the bit that re-enable
‫interrupts.
‫So this is important because at this point in time interrupts the disabled.
‫Right.
‫And then when we issue the IRS, we want entraps to be automatically re enabled, but we can't re-enable
‫interrupts manually because it's a chance.
‫And Interop could happen once whilst we've already changed the registers.
‫So it's dangerous.
‫So by setting the bitton, the flags before we push the flags back to the stack, we ensure that the
‫enabling of interrupts only happens when we issue an irate instruction.
‫The processor will then look at the flags and re-enable the interests for us.
‫So we're not issuing an IRA just yet.
‫We now need to push the user code segment, which should be zombi.
‫And then finally we push to address the function we want to run and use new land.
‫So if we're resuming a task that's already running, this will be the IP, basically the address that
‫was running before we saved the user tasks registers.
‫But more will be explained on that later.
‫So the last step is to call an IRA instruction, which will force the process into a user land unprivileged
‫state.
‫OK, so these are the function prototypes and structures for entering user land.
‫So we can see we have our registered structure here, OK?
‫And we have a restored general purpose registers function, which will take in a pointer to this structure
‫here, this register structure, and it will basically set all the registers in the process.
‫So the equivalent variable ones that you pass in here, OK.
‫Apart from the IP, we can't change that directly.
‫So here we have user mode.
‫Enter.
‫This is also a prototype.
‫This will be the function we use.
‫And to use the land, it expects a registered structure pointer provided to it and it will essentially
‫change all the general-purpose registers.
‫So the ones in the in the register structure you provided here, and it will cause the processes to
‫begin executing the address stored in the IP variable by here.
‫OK, so let's now look at the code for that.
‫So this is our user mode and to function you can see that we get the structure point that was passed
‫to us, which is at this address here, and we saw that pointer in SBX, OK, we now push the stack
‫selector to the stack, which can be seen here, the access variable, and we now push the stack point
‫to the stack, which can be seen here.
‫OK, and then we push the flags, we pop the flags back off into X and then we all the interest rates
‫to those flags and we push the flags back to the stack because remember, we need the Intrepid because
‫when we do in Iraq, we want any troops to be re enabled again.
‫Now we push the code segment, we push the IP we want to execute.
‫We then set up the segment registers and we pass the pointer provided to us.
‫This point here struck registers, we passed that.
‫To the stack, ready for one we call Restall General-Purpose registers so that I'll pass that to that
‫function.
‫That's what I'm trying to say.
‫And then we restore the stack pointer by bytes because we don't want to call a pop here because we might
‫corrupt some registers right now.
‫We IRAT and if you did that correctly, you should now be running in New Zealand at the IP that you
‫specified here.
‫This is the Restall General-Purpose registers function.
‫So you can see we just grab Essi from the structure and we set the real register S.I, we grab EBP and
‫we set the real register EBP and so on until all the general purpose registers have been set.
‫So getting back to use a land when an actual interrupt is very easy, we just call an irate because
‫all that stuff we pushed to the stack whilst we were entering use a land that's done automatically by
‫the processor.
‫When code and user land calls some sort of interest to happen, the interpreting will be called, but
‫not before the processor does exactly what we just did to get to use the land in the first place.
‫So then getting back is very easy.
‫You just do an Eretz at the end of your kind of routine and it will basically execute the use of program
‫again just after the the init instruction they use to get kind of land in the first place.
‫So in the multitasking system, use the land registers will need to be salvaged when entering col land.
‫So essentially you can take advantage of the time or interrupt and we can grab the current task.
‫We can save all of its registers in memory somewhere and then switch to another task.
‫And then as long as we switch all the registers back to the previous registers that we in the other
‫tasks that we saved, then we conflict between tasks and the the tasks will have no idea at all.
‫So that's how multitasking works, by the way.
‫So that's all for this lecture.
‫I understand it's a mouthful.
‫It will take a few lectures before we actually have some user code running.
‫So just stick with it.