‫Hello and welcome, and in this lecture, we are going to switch to protected mode, so the first thing
‫I'm going to do is I'm going to go to Google just to show you guys an article on OS Steve.
‫So I keep showing you this resource because it's incredibly valuable if you ever run into any problems,
‫obviously you can ask me a question, but this resource is very helpful indeed.
‫So this is how you enter to mode, right?
‫So there is an instruction called LGBT, right.
‫Which loads that the global descriptor table.
‫And then you set the protection table bit in the C zero register Kontorovich to zero.
‫Right.
‫And then you jump with the selector and the offset being the absolute address to where you want to load.
‫And then at this point in time, you're now in protected mode.
‫So that's how protective mode works.
‫Right.
‫So if we take a look at the global scripter table, you can see that the global descript a table contains
‫entries telling the CPU about memory segments.
‫The T is loaded using the LGBT assembly instruction.
‫It expects the location of a T description structure.
‫And this is how it looks.
‫You have a size and an offset, so this is essentially a pointer to the actual descriptor entries themselves.
‫And then you have a bunch of entries, right, that describe the memory and the access rights to that
‫memory.
‫So you don't have to worry about all of this stuff because we're just going to set it to some default
‫values.
‫And the reason being is we're going to create a kernel that uses paging.
‫We won't really care as much about this global descriptive table.
‫We'll just put in some default values and we'll work with that, because if you go back to Visual Studio
‫Code and the first thing we're going to do is we're going to get rid of what we did before when we were
‫loading sectors into memory, because we don't really need that right now.
‫So it's best to keep the code nice and clean.
‫Right.
‫If you don't know if you don't already have this and don't worry about it.
‫I just want to get rid of all of this stuff because it's just keeps it cleaner, right.
‫OK, so that's what we did last time, so let's now create the global descriptive table.
‫OK, so what we're basically making now is we're making these entries, OK?
‫These this represents bits, by the way.
‫So, you know, bets 16 to 31.
‫Right.
‫Bid zero to 15, you know, just to make that clear.
‫So, yeah, we're going to we're going to start now.
‫So we'll put our GDP by here.
‫Right.
‫And the very first thing we need is another segment GDP.
‫No.
‫And this is just going to have, you know, 64 bits of zeros, essentially just a null descriptor.
‫I also want to put a good start label here, because we're going to reference that later.
‫OK, so we're just going to go off Section eight.
‫This is the offset in the table.
‫We're about to make the code descriptor and now do GDP code, OK, and we're going to go W zero X FF,
‫OK, and this is the segment limit first zero to 15 bits.
‫And just by here, I'm just going to put Secesh point to this just to make things clearer, because
‫this is this is the code segment now.
‫Right.
‫So now we're going to go d w zero.
‫OK, so this is the base zero to 15 bits.
‫And I we're just going to go DB zero and this is the base 16 to 23 bits.
‫And you don't have to worry too much by this because like I said, we're just going to use the defaults
‫because we don't really want to work with these with these descriptors.
‫Very often we care more about the paging memory model, which we'll get to in time.
‫So here we're going to go DB 069 A.
‫This is the access byte.
‫OK, so if we go back to the Firefox OS stuff, we can see that the access byte is a bunch of bit mask's.
‫OK, it's a bit messy basically.
‫And we've set a bunch of flags in that.
‫These flags are represented here, you see.
‫So present bits must be one for all valid selectors privilege.
‫But, you know, this is the ring level.
‫Remember I was saying about the rings, so this is the ring level.
‫And obviously we want it all to be col space.
‫Right.
‫These bit fields.
‫Explain what we're doing here.
‫Right.
‫So the executable bid, if that's one and this is in the access byte, remember, if that's one and
‫the second could be executed and so on, so on.
‫Now let's carry on.
‫So we're going to go debe one one zero zero one one one one B, OK, and this is the high four bit flags
‫and the low forward flags and we'll just going to go DB zero and you have to like I so you don't have
‫to worry about this stuff very much.
‫Right.
‫Twenty four to 31, one a base, 241 bits.
‫You don't have to worry too much about this stuff because these are just default values.
‫Obviously you can play with these values if you want, but I don't recommend it because there's no benefit
‫to sticking with this memory model.
‫We are basically going to just make it so we can access all of the memory.
‫That's what we're doing here.
‫This is offset zero zero x ten.
‫OK, so this is for our data segment and so on the stack segment and all that.
‫So we're going to go get data wco x, f, f, f.
‫OK, and again, this is the segment limit, you know what, less copy and paste this, actually, because
‫it's just easier, right?
‫Copy and paste this down here, because the structure is exactly the same.
‫So so the only things we need to change are the access by to nine to OK, which change the flags a little
‫bit.
‫And that is that is actually it, and obviously GDP data is wrong, it should be data by my bed, OK?
‫And I'm just going to put a comment here on what should be what they should be linked to.
‫So this should be linked to these SS, ESFS, G.S..
‫OK, so that's all we need to worry about.
‫Now down here we go, G.T. and because remember, these are just labels guys, you know, assembly works
‫and then we go GDP to Scripter, OK, and that'll be W G.T. and minus TGT start minus one.
‫OK, and that'll give us the size of the descriptor.
‫And now here.
‫We go get Star, and that is the offset.
‫Now an important note, our origin is zero.
‫OK, so we need to take this.
‫Into account when making our descriptor.
‫So this is pretty easy to do.
‫We just need to scroll up, change our origin back to zero seven zero zero, OK?
‫And now after our jump shots start, instead of having our segment here, because remember, this is
‫16 bytecode of the moment, instead of having our segment here as the seven C0, we change it to zero.
‫OK, so then obviously one, when it does this jump, the code segment will change to zero.
‫And then in that way then our offsets at zero seven zero work absolutely fine.
‫Now obviously our data segment segment and all that sort of stuff also needs to now become zero for
‫that to work as expected.
‫And we can also get rid of this here.
‫OK, that should work absolutely fine.
‫So what we're going to do now is we're going to create a load protected label.
‫We're going to get rid of this jump as well.
‫OK, and now we're going to clear the interrupts.
‫And we're going to go our GDP lowed global descriptive table and we're going to go GDP descriptor.
‫So obviously what this will do is this a look down here, it'll S.A.T. descriptor, it will find the
‫size here and it'll finally offset here, OK?
‫And then it will look into there and load all of this because GDP starts.
‫That is the address to our label here, which describes the table.
‫Do you understand by doing the end minus GDP start, we get the sites and then it also expects us to
‫minus one on that on that size as well.
‫Now, if we go move IEX, c0, OK.
‫Or or X, CLX one.
‫Move Cosio X, so a reset in that register now, now that we've set up it and then we need to scroll
‫down ways to get rid of our little print routine here, we're not going to use it anymore.
‫We need to now go Bitz 32 so that all code underneath here is seen as 32 bit code.
‫And I will go low 32, OK, and we'll just do a Infonet jump there.
‫That's that's all I want to do at the moment.
‫So we go back up to low protected and at the very top of the file, we're just going to go code seg
‫equals.
‫Geeta Teko minus T starts.
‫Data sig equals GDP data minus GDP start.
‫OK, so what that'll do, that'll give us these offsets, xox eight zero 10, et cetera, et cetera.
‫Right.
‫So now what we can do is we can go and just do a jump jump cosac low 32 and there we are.
‫Code SEC gets replaced zero six eight six zero eight.
‫So what this what this code does, it switches to the code say to the code selector.
‫Right.
‫And then it jumps to the low 30s to absolute address which is here, OK, and then we just jump forever
‫and do nothing else.
‫That's what's happening.
‫So we now need to obviously test this.
‫If we go to the terminal and we type make OK, if we now do QM, you can see nothing crashes.
‫We're now going to have to have to attach GDB to ensure this is worked successfully.
‫So we're going to go pseudo and install gdb.
‫OK.
‫I already have it cool, so we're now going to go gdb target remote.
‫Kumu das systemin dash zae six and sixty four, that's a tough food dobbyn dash capital s.
‫Dash, gdb, WDIA and just press enter.
‫OK, and now type C for continue press enter.
‫OK, good.
‫And now the programs run, the operating systems running, go back to the terminal and tie and hold
‫control and then see.
‫OK, and we can now see where the program is currently executing seven C six a now go layout ASRM and
‫look at that.
‫We can see that we are now on our infinite jump that we wrote there.
‫So we should be in protective mode if we now go in four registers.
‫And just press enter and yes, we are now in protective mode, you can see the code segment is now said
‫to eight.
‫OK, but we haven't set up our data segment registers and all that stuff, so we're going to do that
‫now so we can exit out of the emulator now.
‫So to set the data registers is easy enough inside low 30 to here we just go move X data sec move,
‫dcx move, esx move FCX Move, G.S. X, move ss x.
‫And now just for good measure, we're going to set the base pointer.
‫It's a point to and we use SBP now, by the way, to point to zero zero zero to one, two, three,
‫four, five in memory, and then we're going to set the stat points of the base pointer as well.
‫So obviously, we're just setting the standpoint of further in memory now that because now we now we
‫can access more memory.
‫Right.
‫Let's just test it one more time.
‫We're going to go make.
‫And now we're going to go jadi gdb target remote kumu dash system, desex a six hundred sixty four Dash
‫HDA Doc Ford, Sasebo Dobbyn.
‫Dash capital capitalis dash gdb Stelio, we're going to proceed to continue control so we can see that's
‫why we're running low assim and yes, we can see our Infonet jump there if we now go in four registers.
‫We can see that the coat that the coat segments, eight stacked segments, 10 data segments, 10 flags
‫and all that, also zero x 10.
‫So, yeah, we are now in protected mode.
‫The is now running in a 32 bit mode that we can no longer access the BIOS if we attempt to access the
‫bios in our thirty two big code here, really bad things will happen.
‫Unfortunately this means that we come from the desk as we did in the in a few lectures ago.
‫We can't do that anymore.
‫We now have to write a disk driver of our own if we want to read from the disk.
‫So that's another important thing to note.
‫So yeah, congratulations.
‫You are now in protective mode.
‫The next few lectures will obviously have to write a disk driver of our own so that we can load the
‫rest of the kernel because remember, the bios only loads the first sector into memory for us.
‫So as soon as our program gets bigger than 512 bytes, we're going to certainly run into problems.
‫Congratulations.