﻿1
00:00:00,060 --> 00:00:05,400
‫Hello and welcome, and in this lecture, we are going to switch to protected mode, so the first thing

2
00:00:05,400 --> 00:00:10,590
‫I'm going to do is I'm going to go to Google just to show you guys an article on OS Steve.

3
00:00:16,850 --> 00:00:22,130
‫So I keep showing you this resource because it's incredibly valuable if you ever run into any problems,

4
00:00:22,370 --> 00:00:27,270
‫obviously you can ask me a question, but this resource is very helpful indeed.

5
00:00:27,800 --> 00:00:30,490
‫So this is how you enter to mode, right?

6
00:00:31,850 --> 00:00:35,720
‫So there is an instruction called LGBT, right.

7
00:00:36,260 --> 00:00:38,720
‫Which loads that the global descriptor table.

8
00:00:38,720 --> 00:00:44,600
‫And then you set the protection table bit in the C zero register Kontorovich to zero.

9
00:00:44,750 --> 00:00:45,100
‫Right.

10
00:00:46,010 --> 00:00:54,080
‫And then you jump with the selector and the offset being the absolute address to where you want to load.

11
00:00:54,290 --> 00:00:57,670
‫And then at this point in time, you're now in protected mode.

12
00:00:58,040 --> 00:01:00,230
‫So that's how protective mode works.

13
00:01:00,230 --> 00:01:00,500
‫Right.

14
00:01:00,680 --> 00:01:07,430
‫So if we take a look at the global scripter table, you can see that the global descript a table contains

15
00:01:07,430 --> 00:01:10,670
‫entries telling the CPU about memory segments.

16
00:01:11,360 --> 00:01:15,950
‫The T is loaded using the LGBT assembly instruction.

17
00:01:16,430 --> 00:01:20,690
‫It expects the location of a T description structure.

18
00:01:21,800 --> 00:01:22,970
‫And this is how it looks.

19
00:01:23,000 --> 00:01:30,370
‫You have a size and an offset, so this is essentially a pointer to the actual descriptor entries themselves.

20
00:01:31,130 --> 00:01:39,890
‫And then you have a bunch of entries, right, that describe the memory and the access rights to that

21
00:01:39,890 --> 00:01:40,350
‫memory.

22
00:01:40,670 --> 00:01:44,870
‫So you don't have to worry about all of this stuff because we're just going to set it to some default

23
00:01:44,870 --> 00:01:45,680
‫values.

24
00:01:45,950 --> 00:01:49,670
‫And the reason being is we're going to create a kernel that uses paging.

25
00:01:49,670 --> 00:01:54,400
‫We won't really care as much about this global descriptive table.

26
00:01:54,410 --> 00:02:00,920
‫We'll just put in some default values and we'll work with that, because if you go back to Visual Studio

27
00:02:00,920 --> 00:02:05,840
‫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

28
00:02:05,840 --> 00:02:09,730
‫loading sectors into memory, because we don't really need that right now.

29
00:02:09,740 --> 00:02:12,680
‫So it's best to keep the code nice and clean.

30
00:02:12,680 --> 00:02:12,980
‫Right.

31
00:02:13,430 --> 00:02:16,770
‫If you don't know if you don't already have this and don't worry about it.

32
00:02:17,600 --> 00:02:22,690
‫I just want to get rid of all of this stuff because it's just keeps it cleaner, right.

33
00:02:24,920 --> 00:02:30,930
‫OK, so that's what we did last time, so let's now create the global descriptive table.

34
00:02:30,980 --> 00:02:36,180
‫OK, so what we're basically making now is we're making these entries, OK?

35
00:02:36,440 --> 00:02:38,750
‫These this represents bits, by the way.

36
00:02:39,620 --> 00:02:42,440
‫So, you know, bets 16 to 31.

37
00:02:42,650 --> 00:02:42,990
‫Right.

38
00:02:43,070 --> 00:02:46,610
‫Bid zero to 15, you know, just to make that clear.

39
00:02:46,820 --> 00:02:49,080
‫So, yeah, we're going to we're going to start now.

40
00:02:49,550 --> 00:02:51,620
‫So we'll put our GDP by here.

41
00:02:51,620 --> 00:02:51,920
‫Right.

42
00:02:52,850 --> 00:02:57,630
‫And the very first thing we need is another segment GDP.

43
00:02:57,710 --> 00:02:58,040
‫No.

44
00:03:04,030 --> 00:03:10,690
‫And this is just going to have, you know, 64 bits of zeros, essentially just a null descriptor.

45
00:03:11,170 --> 00:03:15,940
‫I also want to put a good start label here, because we're going to reference that later.

46
00:03:16,540 --> 00:03:19,860
‫OK, so we're just going to go off Section eight.

47
00:03:20,340 --> 00:03:21,820
‫This is the offset in the table.

48
00:03:21,820 --> 00:03:31,870
‫We're about to make the code descriptor and now do GDP code, OK, and we're going to go W zero X FF,

49
00:03:32,500 --> 00:03:38,380
‫OK, and this is the segment limit first zero to 15 bits.

50
00:03:38,800 --> 00:03:47,590
‫And just by here, I'm just going to put Secesh point to this just to make things clearer, because

51
00:03:47,590 --> 00:03:49,390
‫this is this is the code segment now.

52
00:03:49,390 --> 00:03:49,660
‫Right.

53
00:03:50,080 --> 00:03:52,480
‫So now we're going to go d w zero.

54
00:03:52,950 --> 00:04:01,060
‫OK, so this is the base zero to 15 bits.

55
00:04:01,330 --> 00:04:10,090
‫And I we're just going to go DB zero and this is the base 16 to 23 bits.

56
00:04:10,090 --> 00:04:14,260
‫And you don't have to worry too much by this because like I said, we're just going to use the defaults

57
00:04:14,620 --> 00:04:18,400
‫because we don't really want to work with these with these descriptors.

58
00:04:18,400 --> 00:04:24,000
‫Very often we care more about the paging memory model, which we'll get to in time.

59
00:04:24,520 --> 00:04:27,860
‫So here we're going to go DB 069 A.

60
00:04:29,110 --> 00:04:30,920
‫This is the access byte.

61
00:04:31,640 --> 00:04:41,200
‫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.

62
00:04:41,450 --> 00:04:43,710
‫OK, it's a bit messy basically.

63
00:04:43,720 --> 00:04:45,630
‫And we've set a bunch of flags in that.

64
00:04:45,700 --> 00:04:48,220
‫These flags are represented here, you see.

65
00:04:48,220 --> 00:04:54,250
‫So present bits must be one for all valid selectors privilege.

66
00:04:54,250 --> 00:04:55,960
‫But, you know, this is the ring level.

67
00:04:56,260 --> 00:05:00,070
‫Remember I was saying about the rings, so this is the ring level.

68
00:05:01,510 --> 00:05:04,030
‫And obviously we want it all to be col space.

69
00:05:04,030 --> 00:05:04,330
‫Right.

70
00:05:04,990 --> 00:05:06,160
‫These bit fields.

71
00:05:06,160 --> 00:05:07,540
‫Explain what we're doing here.

72
00:05:07,540 --> 00:05:07,820
‫Right.

73
00:05:08,170 --> 00:05:13,180
‫So the executable bid, if that's one and this is in the access byte, remember, if that's one and

74
00:05:13,180 --> 00:05:16,020
‫the second could be executed and so on, so on.

75
00:05:16,420 --> 00:05:18,030
‫Now let's carry on.

76
00:05:19,330 --> 00:05:29,650
‫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

77
00:05:29,680 --> 00:05:35,890
‫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

78
00:05:35,890 --> 00:05:37,630
‫to worry about this stuff very much.

79
00:05:37,630 --> 00:05:37,910
‫Right.

80
00:05:38,560 --> 00:05:41,680
‫Twenty four to 31, one a base, 241 bits.

81
00:05:42,040 --> 00:05:45,880
‫You don't have to worry too much about this stuff because these are just default values.

82
00:05:46,300 --> 00:05:51,220
‫Obviously you can play with these values if you want, but I don't recommend it because there's no benefit

83
00:05:51,220 --> 00:05:52,710
‫to sticking with this memory model.

84
00:05:52,960 --> 00:05:57,130
‫We are basically going to just make it so we can access all of the memory.

85
00:05:57,130 --> 00:05:58,120
‫That's what we're doing here.

86
00:05:58,390 --> 00:06:00,850
‫This is offset zero zero x ten.

87
00:06:01,000 --> 00:06:06,460
‫OK, so this is for our data segment and so on the stack segment and all that.

88
00:06:06,850 --> 00:06:14,320
‫So we're going to go get data wco x, f, f, f.

89
00:06:15,370 --> 00:06:21,460
‫OK, and again, this is the segment limit, you know what, less copy and paste this, actually, because

90
00:06:21,460 --> 00:06:22,530
‫it's just easier, right?

91
00:06:23,020 --> 00:06:26,140
‫Copy and paste this down here, because the structure is exactly the same.

92
00:06:26,150 --> 00:06:36,400
‫So so the only things we need to change are the access by to nine to OK, which change the flags a little

93
00:06:36,400 --> 00:06:36,690
‫bit.

94
00:06:38,640 --> 00:06:46,370
‫And that is that is actually it, and obviously GDP data is wrong, it should be data by my bed, OK?

95
00:06:46,590 --> 00:06:51,720
‫And I'm just going to put a comment here on what should be what they should be linked to.

96
00:06:52,560 --> 00:06:59,370
‫So this should be linked to these SS, ESFS, G.S..

97
00:06:59,610 --> 00:07:01,790
‫OK, so that's all we need to worry about.

98
00:07:01,950 --> 00:07:07,470
‫Now down here we go, G.T. and because remember, these are just labels guys, you know, assembly works

99
00:07:07,890 --> 00:07:18,630
‫and then we go GDP to Scripter, OK, and that'll be W G.T. and minus TGT start minus one.

100
00:07:18,870 --> 00:07:22,390
‫OK, and that'll give us the size of the descriptor.

101
00:07:23,310 --> 00:07:24,420
‫And now here.

102
00:07:25,400 --> 00:07:28,730
‫We go get Star, and that is the offset.

103
00:07:29,030 --> 00:07:34,330
‫Now an important note, our origin is zero.

104
00:07:34,550 --> 00:07:40,040
‫OK, so we need to take this.

105
00:07:42,020 --> 00:07:45,030
‫Into account when making our descriptor.

106
00:07:45,230 --> 00:07:46,920
‫So this is pretty easy to do.

107
00:07:46,970 --> 00:07:52,330
‫We just need to scroll up, change our origin back to zero seven zero zero, OK?

108
00:07:53,180 --> 00:07:58,010
‫And now after our jump shots start, instead of having our segment here, because remember, this is

109
00:07:58,010 --> 00:08:05,450
‫16 bytecode of the moment, instead of having our segment here as the seven C0, we change it to zero.

110
00:08:05,900 --> 00:08:12,590
‫OK, so then obviously one, when it does this jump, the code segment will change to zero.

111
00:08:13,100 --> 00:08:19,040
‫And then in that way then our offsets at zero seven zero work absolutely fine.

112
00:08:19,400 --> 00:08:26,720
‫Now obviously our data segment segment and all that sort of stuff also needs to now become zero for

113
00:08:26,720 --> 00:08:28,760
‫that to work as expected.

114
00:08:28,760 --> 00:08:30,560
‫And we can also get rid of this here.

115
00:08:31,920 --> 00:08:35,670
‫OK, that should work absolutely fine.

116
00:08:35,780 --> 00:08:43,460
‫So what we're going to do now is we're going to create a load protected label.

117
00:08:43,850 --> 00:08:45,590
‫We're going to get rid of this jump as well.

118
00:08:47,310 --> 00:08:51,500
‫OK, and now we're going to clear the interrupts.

119
00:08:53,100 --> 00:09:02,670
‫And we're going to go our GDP lowed global descriptive table and we're going to go GDP descriptor.

120
00:09:04,170 --> 00:09:11,100
‫So obviously what this will do is this a look down here, it'll S.A.T. descriptor, it will find the

121
00:09:11,100 --> 00:09:14,670
‫size here and it'll finally offset here, OK?

122
00:09:14,910 --> 00:09:20,180
‫And then it will look into there and load all of this because GDP starts.

123
00:09:20,880 --> 00:09:25,860
‫That is the address to our label here, which describes the table.

124
00:09:26,040 --> 00:09:34,590
‫Do you understand by doing the end minus GDP start, we get the sites and then it also expects us to

125
00:09:34,590 --> 00:09:36,950
‫minus one on that on that size as well.

126
00:09:39,120 --> 00:09:43,590
‫Now, if we go move IEX, c0, OK.

127
00:09:44,660 --> 00:09:48,260
‫Or or X, CLX one.

128
00:09:50,940 --> 00:09:58,380
‫Move Cosio X, so a reset in that register now, now that we've set up it and then we need to scroll

129
00:09:58,380 --> 00:10:03,350
‫down ways to get rid of our little print routine here, we're not going to use it anymore.

130
00:10:05,740 --> 00:10:11,920
‫We need to now go Bitz 32 so that all code underneath here is seen as 32 bit code.

131
00:10:14,510 --> 00:10:19,460
‫And I will go low 32, OK, and we'll just do a Infonet jump there.

132
00:10:20,270 --> 00:10:22,010
‫That's that's all I want to do at the moment.

133
00:10:22,280 --> 00:10:30,230
‫So we go back up to low protected and at the very top of the file, we're just going to go code seg

134
00:10:30,590 --> 00:10:31,460
‫equals.

135
00:10:33,330 --> 00:10:35,730
‫Geeta Teko minus T starts.

136
00:10:38,250 --> 00:10:44,880
‫Data sig equals GDP data minus GDP start.

137
00:10:48,220 --> 00:10:56,230
‫OK, so what that'll do, that'll give us these offsets, xox eight zero 10, et cetera, et cetera.

138
00:10:56,240 --> 00:10:56,540
‫Right.

139
00:10:57,070 --> 00:11:07,250
‫So now what we can do is we can go and just do a jump jump cosac low 32 and there we are.

140
00:11:07,660 --> 00:11:11,520
‫Code SEC gets replaced zero six eight six zero eight.

141
00:11:11,560 --> 00:11:16,540
‫So what this what this code does, it switches to the code say to the code selector.

142
00:11:16,540 --> 00:11:16,900
‫Right.

143
00:11:17,290 --> 00:11:24,400
‫And then it jumps to the low 30s to absolute address which is here, OK, and then we just jump forever

144
00:11:24,400 --> 00:11:25,390
‫and do nothing else.

145
00:11:25,910 --> 00:11:26,840
‫That's what's happening.

146
00:11:27,430 --> 00:11:29,410
‫So we now need to obviously test this.

147
00:11:29,470 --> 00:11:37,000
‫If we go to the terminal and we type make OK, if we now do QM, you can see nothing crashes.

148
00:11:37,300 --> 00:11:42,360
‫We're now going to have to have to attach GDB to ensure this is worked successfully.

149
00:11:42,820 --> 00:11:45,820
‫So we're going to go pseudo and install gdb.

150
00:11:46,720 --> 00:11:47,220
‫OK.

151
00:11:48,600 --> 00:11:55,890
‫I already have it cool, so we're now going to go gdb target remote.

152
00:11:58,580 --> 00:12:09,710
‫Kumu das systemin dash zae six and sixty four, that's a tough food dobbyn dash capital s.

153
00:12:11,190 --> 00:12:15,330
‫Dash, gdb, WDIA and just press enter.

154
00:12:16,320 --> 00:12:21,450
‫OK, and now type C for continue press enter.

155
00:12:22,080 --> 00:12:23,050
‫OK, good.

156
00:12:23,100 --> 00:12:29,610
‫And now the programs run, the operating systems running, go back to the terminal and tie and hold

157
00:12:29,610 --> 00:12:31,380
‫control and then see.

158
00:12:32,790 --> 00:12:42,480
‫OK, and we can now see where the program is currently executing seven C six a now go layout ASRM and

159
00:12:42,480 --> 00:12:43,200
‫look at that.

160
00:12:43,200 --> 00:12:48,240
‫We can see that we are now on our infinite jump that we wrote there.

161
00:12:48,240 --> 00:12:51,720
‫So we should be in protective mode if we now go in four registers.

162
00:12:53,080 --> 00:13:00,700
‫And just press enter and yes, we are now in protective mode, you can see the code segment is now said

163
00:13:00,700 --> 00:13:01,300
‫to eight.

164
00:13:02,440 --> 00:13:08,110
‫OK, but we haven't set up our data segment registers and all that stuff, so we're going to do that

165
00:13:08,110 --> 00:13:11,140
‫now so we can exit out of the emulator now.

166
00:13:13,420 --> 00:13:23,950
‫So to set the data registers is easy enough inside low 30 to here we just go move X data sec move,

167
00:13:23,950 --> 00:13:34,600
‫dcx move, esx move FCX Move, G.S. X, move ss x.

168
00:13:34,600 --> 00:13:37,960
‫And now just for good measure, we're going to set the base pointer.

169
00:13:39,060 --> 00:13:47,710
‫It's a point to and we use SBP now, by the way, to point to zero zero zero to one, two, three,

170
00:13:47,710 --> 00:13:52,910
‫four, five in memory, and then we're going to set the stat points of the base pointer as well.

171
00:13:53,320 --> 00:14:00,130
‫So obviously, we're just setting the standpoint of further in memory now that because now we now we

172
00:14:00,130 --> 00:14:01,180
‫can access more memory.

173
00:14:01,180 --> 00:14:01,400
‫Right.

174
00:14:02,290 --> 00:14:03,730
‫Let's just test it one more time.

175
00:14:03,730 --> 00:14:04,600
‫We're going to go make.

176
00:14:05,570 --> 00:14:14,810
‫And now we're going to go jadi gdb target remote kumu dash system, desex a six hundred sixty four Dash

177
00:14:14,890 --> 00:14:17,270
‫HDA Doc Ford, Sasebo Dobbyn.

178
00:14:19,100 --> 00:14:28,160
‫Dash capital capitalis dash gdb Stelio, we're going to proceed to continue control so we can see that's

179
00:14:28,160 --> 00:14:36,140
‫why we're running low assim and yes, we can see our Infonet jump there if we now go in four registers.

180
00:14:38,860 --> 00:14:47,110
‫We can see that the coat that the coat segments, eight stacked segments, 10 data segments, 10 flags

181
00:14:47,110 --> 00:14:49,540
‫and all that, also zero x 10.

182
00:14:49,840 --> 00:14:52,180
‫So, yeah, we are now in protected mode.

183
00:14:53,410 --> 00:14:59,560
‫The is now running in a 32 bit mode that we can no longer access the BIOS if we attempt to access the

184
00:14:59,560 --> 00:15:03,700
‫bios in our thirty two big code here, really bad things will happen.

185
00:15:04,600 --> 00:15:10,510
‫Unfortunately this means that we come from the desk as we did in the in a few lectures ago.

186
00:15:10,870 --> 00:15:12,110
‫We can't do that anymore.

187
00:15:12,280 --> 00:15:17,220
‫We now have to write a disk driver of our own if we want to read from the disk.

188
00:15:17,560 --> 00:15:19,480
‫So that's another important thing to note.

189
00:15:20,020 --> 00:15:21,670
‫So yeah, congratulations.

190
00:15:21,670 --> 00:15:23,440
‫You are now in protective mode.

191
00:15:23,700 --> 00:15:29,320
‫The next few lectures will obviously have to write a disk driver of our own so that we can load the

192
00:15:29,320 --> 00:15:34,570
‫rest of the kernel because remember, the bios only loads the first sector into memory for us.

193
00:15:34,960 --> 00:15:40,840
‫So as soon as our program gets bigger than 512 bytes, we're going to certainly run into problems.

194
00:15:41,710 --> 00:15:42,850
‫Congratulations.

