﻿1
00:00:00,300 --> 00:00:03,990
‫Hello and welcome, everybody, to part four of loading the Alpha.

2
00:00:04,480 --> 00:00:10,830
‫Uh, let's just go back to the offloads and we're going to start creating some more functions in here

3
00:00:10,830 --> 00:00:11,250
‫now.

4
00:00:11,970 --> 00:00:16,350
‫So we're going to start with the offload function, which is responsible for loading.

5
00:00:16,350 --> 00:00:18,090
‫And I'll file by by name.

6
00:00:18,510 --> 00:00:19,650
‫So we just go in it.

7
00:00:19,650 --> 00:00:23,190
‫Offload const char filename.

8
00:00:24,390 --> 00:00:25,830
‫Struct, I'll file.

9
00:00:30,020 --> 00:00:35,850
‫Fall out, so this is a multidimensional pointer, OK, we're going to return zero here.

10
00:00:36,260 --> 00:00:41,840
‫So the idea is they pass the address of an of an alpha pointer, OK?

11
00:00:42,240 --> 00:00:45,360
‫And then we will set that pointer to some memory we declare.

12
00:00:45,740 --> 00:00:46,940
‫So here we're going to go struct.

13
00:00:46,940 --> 00:00:54,380
‫Our file of file equals kasar block size of structural files.

14
00:00:54,390 --> 00:00:57,500
‫So we're just making some room now in memory.

15
00:00:57,500 --> 00:01:00,380
‫Would decline some memory to store that file in.

16
00:01:01,610 --> 00:01:05,510
‫And fix that spelling mistake there, if you guys did the same.

17
00:01:07,720 --> 00:01:15,760
‫So now we're going to go in at 50 zero and it rascal's f open file name, so we're only interested in

18
00:01:15,760 --> 00:01:16,750
‫opening for reading.

19
00:01:16,750 --> 00:01:18,130
‫We're not going to change this file.

20
00:01:18,670 --> 00:01:23,770
‫If wrests is below equal zero, then there was a problem opening the files.

21
00:01:23,770 --> 00:01:27,910
‫So we're just going to go out and we're going to have an out label by here as well, guys.

22
00:01:28,300 --> 00:01:29,710
‫And we're just going to return wrasse.

23
00:01:30,220 --> 00:01:30,730
‫OK.

24
00:01:33,430 --> 00:01:38,500
‫Now, if everything went fine, then F.T. should equal terrorists, OK, which sets the fall points

25
00:01:38,560 --> 00:01:44,170
‫here and now we're just going to say struct file stat.

26
00:01:46,970 --> 00:01:55,850
‫Stats were basically about stap the file, OK, and then we're going to say rascal's f stat 50 percent

27
00:01:55,850 --> 00:01:57,500
‫stats, the address stat.

28
00:01:57,740 --> 00:02:00,290
‫So a stat in the fall because we want to get the file size.

29
00:02:00,770 --> 00:02:04,160
‫If that failed then go to out.

30
00:02:04,490 --> 00:02:12,240
‫OK, otherwise our file of memory equals K stat dot file size.

31
00:02:12,260 --> 00:02:18,650
‫So we're basically creating enough memory on the heap to store the entire file because we're loading

32
00:02:18,650 --> 00:02:22,060
‫the whole file into memory, the whole outfalls going to get loaded.

33
00:02:22,250 --> 00:02:28,490
‫As I said before, a better implementation would be to use some sort of memory mapping and map function,

34
00:02:28,490 --> 00:02:29,750
‫but we don't have that yet.

35
00:02:29,760 --> 00:02:30,930
‫So this will have to do.

36
00:02:31,340 --> 00:02:35,780
‫So now we're going to say rascal's Elfriede Alpha file of memory.

37
00:02:36,350 --> 00:02:38,930
‫So we're reading the entire file into memory.

38
00:02:38,930 --> 00:02:46,190
‫Now, start up file size one, OK, and then we'll just drift by that.

39
00:02:46,730 --> 00:02:51,110
‫So now if the rest is below zero, then go to out.

40
00:02:51,410 --> 00:02:52,070
‫There was an error.

41
00:02:52,070 --> 00:02:52,660
‫Go to out.

42
00:02:52,670 --> 00:02:53,990
‫OK, so brilliant.

43
00:02:53,990 --> 00:02:56,930
‫At this point, the entire alpha is loaded into memory.

44
00:02:57,320 --> 00:03:03,650
‫So when I was just going to say rascal's alpha process loaded and we haven't created this yet, this

45
00:03:03,650 --> 00:03:10,850
‫function, but essentially it processes the loaded LTH file and if that fails, then we're just going

46
00:03:10,850 --> 00:03:13,090
‫to go to our OK.

47
00:03:13,370 --> 00:03:16,670
‫And then here we're going to go ask this file out Excel file.

48
00:03:17,000 --> 00:03:19,380
‫So we're setting the point they provided to us.

49
00:03:19,400 --> 00:03:26,750
‫OK, so that's what we're doing there because they basically go ampersand on the pointer to get the

50
00:03:26,750 --> 00:03:28,250
‫address with the pointer variable.

51
00:03:28,430 --> 00:03:34,100
‫They pass that to us and then we set what the pointer value is, essentially pointing the pointer variable

52
00:03:34,100 --> 00:03:34,850
‫to the alpha.

53
00:03:34,850 --> 00:03:37,010
‫We create it up here on line one, two, eight.

54
00:03:41,070 --> 00:03:46,830
‫One last thing here, we're just going to go f close F.T. because once the whole file is being loaded

55
00:03:46,830 --> 00:03:49,820
‫into memory, we don't really care about that file pointer anymore.

56
00:03:50,340 --> 00:03:51,850
‫We don't plan to change the Alpha.

57
00:03:52,020 --> 00:03:55,440
‫We just wanted to read it or read it all into memory, essentially, guys.

58
00:03:56,040 --> 00:03:58,780
‫OK, so now we're going to create our process loaded function.

59
00:03:58,800 --> 00:04:04,710
‫Just go in it process loaded struct and file our file.

60
00:04:05,550 --> 00:04:10,660
‫OK, and here we're just going to go in and rescue zero return address, OK?

61
00:04:11,610 --> 00:04:17,660
‫And now here we're going to say struct Elfreda had equals Elfrida our file.

62
00:04:18,180 --> 00:04:20,730
‫So that gets the el-Fadl from the file.

63
00:04:20,730 --> 00:04:28,410
‫Basically DKA pointed to it and as you know that's just a cast on to the actual Alaf memory, the alpha

64
00:04:28,440 --> 00:04:29,710
‫memory variable in the file.

65
00:04:29,730 --> 00:04:37,730
‫OK, it's just a case to see if we're going to say an address equals l validate loaded header.

66
00:04:39,090 --> 00:04:45,240
‫So we're just validating that this file is supported by us and we're going to say if this is below zero,

67
00:04:45,240 --> 00:04:49,200
‫go to out and we're going to make the out the out label here again, guys.

68
00:04:51,120 --> 00:04:57,750
‫So this will first validate the file is is loaded correctly and and it's a valid L file.

69
00:04:58,140 --> 00:05:03,660
‫And then after that, we just need to process the program headers because that's all we care about.

70
00:05:03,670 --> 00:05:08,320
‫So we go equals F process P headers and we haven't created this function yet.

71
00:05:08,760 --> 00:05:10,230
‫I'm going to pass in the file.

72
00:05:10,710 --> 00:05:14,610
‫If the rest is below zero, then there was an error and we should go to our.

73
00:05:15,510 --> 00:05:21,060
‫So as I just explained in the previous lecture, we're going to basically be loading the program ahead

74
00:05:21,140 --> 00:05:21,930
‫into memory.

75
00:05:22,590 --> 00:05:27,030
‫What we've already done that essentially we're going to be processing the program had is in memory.

76
00:05:27,030 --> 00:05:28,070
‫That's what I meant to say.

77
00:05:28,650 --> 00:05:34,050
‫That's all we care about is the program headers because we don't care about anything else at the moment.

78
00:05:34,050 --> 00:05:37,380
‫We don't care about dynamic linking of runtime.

79
00:05:37,830 --> 00:05:44,520
‫We just want to be able to run static our files with all statically compiled code, statically linked

80
00:05:44,520 --> 00:05:52,200
‫code, essentially, guys, OK, we will not be able to load in the files that rely on shared libraries

81
00:05:52,710 --> 00:05:54,600
‫or relocation files.

82
00:05:54,930 --> 00:06:00,650
‫Essentially, all symbols must be known in the file we are loading before runtime.

83
00:06:00,990 --> 00:06:05,190
‫OK, so now just just here we're going to say in itself, process P header.

84
00:06:05,190 --> 00:06:12,750
‫So we're creating that function now struct our file, our file and we're going to say an address equals

85
00:06:12,750 --> 00:06:14,220
‫zero return address.

86
00:06:16,360 --> 00:06:22,330
‫And then here we're going to say, struct Elfreda header equals Elfreda, I'll file.

87
00:06:24,380 --> 00:06:33,140
‫And I was going to say French, I cruzeiro I had to be numb, I plus plus so that a loop through all

88
00:06:33,140 --> 00:06:38,150
‫of the program had is that we have OK, because remember the program head is stored in the.

89
00:06:38,660 --> 00:06:46,910
‫OK, so now we're going to go struct of 32 for program header and we're going to call that variable

90
00:06:47,210 --> 00:06:54,470
‫HDR and we're going to say equals elf program header and we're going to pass in the head and then we're

91
00:06:54,470 --> 00:06:55,480
‫going to pass in AI.

92
00:06:56,210 --> 00:07:00,550
‫OK, so that'll get us the program header with the given index.

93
00:07:01,010 --> 00:07:06,440
‫And now here we're going to say Rascal's Elf Process P header and we're going to pass in the ELF.

94
00:07:06,770 --> 00:07:13,760
‫And the program had a I remember if you're confused by any of this, check the specification because

95
00:07:13,760 --> 00:07:14,490
‫it's huge.

96
00:07:14,490 --> 00:07:16,910
‫It's thousands, probably a thousand pages long.

97
00:07:17,270 --> 00:07:23,360
‫It'll explain everything in much more detail because this course is an operating system development

98
00:07:23,360 --> 00:07:29,630
‫course, not an elf, cos I'm only teaching you enough about ELF to be able to load ELF programs.

99
00:07:30,350 --> 00:07:36,320
‫If you want more of an in-depth explanation on that, you should check that out as it's true nesh to

100
00:07:36,320 --> 00:07:40,250
‫be covered in a lot of death in this chaos.

101
00:07:40,850 --> 00:07:42,770
‫OK, and we haven't created the process.

102
00:07:42,780 --> 00:07:47,300
‫P had a function yet so just go f rest below zero break.

103
00:07:48,170 --> 00:07:54,350
‫And if you do get confused guys, just let me know and I can explain all of this stuff in more detail.

104
00:07:55,190 --> 00:08:00,560
‫Essentially what looping through the total number of program headers and then we're going to process

105
00:08:00,560 --> 00:08:02,810
‫them by calling elf process P head.

106
00:08:03,230 --> 00:08:09,110
‫So here we're going to go in it elf process P header and notice how we break everything down.

107
00:08:09,110 --> 00:08:10,970
‫This is how code is supposed to be written.

108
00:08:10,970 --> 00:08:12,620
‫We should be breaking everything down.

109
00:08:12,620 --> 00:08:22,160
‫OK, struct our file, our file struct elf thirty two HDR program header and then we're going to say

110
00:08:22,160 --> 00:08:28,610
‫an address equals zero switch r p type.

111
00:08:29,150 --> 00:08:30,110
‫OK as well.

112
00:08:30,110 --> 00:08:32,690
‫We care about the type of program had to type.

113
00:08:32,690 --> 00:08:35,060
‫It is we only care about the load.

114
00:08:35,420 --> 00:08:39,710
‫Remember I've explained this before, we're only going to be dealing with the load.

115
00:08:40,160 --> 00:08:47,270
‫So here we're going to say wrasse equals elf process program had a load and we're going to pass in our

116
00:08:47,270 --> 00:08:48,800
‫file and the program header.

117
00:08:49,130 --> 00:08:51,620
‫So we're now soon to implement this function.

118
00:08:52,040 --> 00:08:58,070
‫But before we do, let's just get the specification of ELF again so I can show you these things.

119
00:08:59,060 --> 00:09:01,040
‫We can see peaty load.

120
00:09:02,010 --> 00:09:02,650
‫By here.

121
00:09:04,530 --> 00:09:11,820
‫So what is load the array element specifies a loadable segment described by peafowl size and PIMS size

122
00:09:12,030 --> 00:09:15,800
‫the bytes from the file, a map to the beginning of memory of the segment.

123
00:09:16,320 --> 00:09:23,490
‫If the segments memory size PIMS Z is larger in the file size, the extra box to define to hold the

124
00:09:23,490 --> 00:09:24,330
‫value zero.

125
00:09:24,810 --> 00:09:27,540
‫The file size may not be larger than the memory size.

126
00:09:27,550 --> 00:09:28,990
‫So it explains all of this.

127
00:09:29,110 --> 00:09:29,270
‫Right.

128
00:09:29,310 --> 00:09:35,220
‫So essentially Pete Load describes a laudable segment and we want to process loadable segments.

129
00:09:37,400 --> 00:09:40,940
‫OK, so we're almost done just at the very top here.

130
00:09:40,970 --> 00:09:49,610
‫Now we're going to say in our process, we had a pretty loud struct elf file elf file struct ELF 30

131
00:09:49,610 --> 00:09:50,910
‫to FDP.

132
00:09:52,370 --> 00:10:00,860
‫So we're dealing with the program head for peaty load instances now and we're just going to return zero

133
00:10:00,860 --> 00:10:01,310
‫here.

134
00:10:01,520 --> 00:10:07,730
‫And this function will be in charge of calculating the virtual base address and the physical base address.

135
00:10:07,760 --> 00:10:15,530
‫OK, so here we're just going to say if our file virtual base address is above our equal and we're going

136
00:10:15,530 --> 00:10:24,800
‫to have to cast this to avoid pointer program had a P v address or the L file virtual base address is

137
00:10:24,800 --> 00:10:25,820
‫equal to nothing.

138
00:10:27,740 --> 00:10:29,550
‫In other words, we haven't set it yet.

139
00:10:30,170 --> 00:10:35,840
‫Then our file virtual based address equals void pointer.

140
00:10:38,250 --> 00:10:49,080
‫Program had a Peevey address, our file physical based address equals LTH memory, our file plus the

141
00:10:49,080 --> 00:10:50,750
‫program had upset.

142
00:10:51,990 --> 00:10:59,010
‫OK, so this here calculates the virtual based address and the physical base address.

143
00:10:59,280 --> 00:11:04,020
‫Now we know the physical base address starts from the edge of memory, the one that we've loaded.

144
00:11:04,170 --> 00:11:10,890
‫OK, and we know the virtual base address is specified in Peevey address because if we just go to the

145
00:11:10,890 --> 00:11:20,650
‫dump health utility and we just go back in the programs into blank and we just dump that file dump health

146
00:11:20,670 --> 00:11:24,090
‫plan, lf we scroll up in here.

147
00:11:25,830 --> 00:11:30,330
‫You know, we can see the virtual base dressier zero six 400000.

148
00:11:30,630 --> 00:11:34,570
‫OK, so we can see that zero x 400000.

149
00:11:35,040 --> 00:11:38,290
‫So the virtual base dress should be zero x 400000.

150
00:11:38,310 --> 00:11:43,410
‫OK, and so we essentially loop through all of these program load headers.

151
00:11:43,860 --> 00:11:51,450
‫And if we find an address that is lower than the current address we have, then we know that the virtual

152
00:11:51,450 --> 00:11:55,510
‫base address that we currently have is incorrect and we'll choose the lower value.

153
00:11:55,710 --> 00:11:57,300
‫So that's what this statement is for.

154
00:11:57,330 --> 00:12:05,820
‫OK, so for example, if it costs 400, 1000 and then it costs 400000, well, we've just set our virtual

155
00:12:05,820 --> 00:12:08,040
‫base address to zero x thousand.

156
00:12:08,370 --> 00:12:11,220
‫Well, we know that 400000 is smaller than that.

157
00:12:11,220 --> 00:12:11,500
‫Right.

158
00:12:11,790 --> 00:12:13,670
‫So we are above that in size.

159
00:12:13,950 --> 00:12:16,380
‫So then we run this and correct ourselves.

160
00:12:16,380 --> 00:12:21,480
‫We correct the mistake we made because we found a lower address and address that's lower than the one

161
00:12:21,480 --> 00:12:22,440
‫we previously had.

162
00:12:22,860 --> 00:12:29,160
‫So obviously, at the end of processing all of the program load headers, we will have a virtual base

163
00:12:29,160 --> 00:12:35,160
‫address as the lowest possible virtual address for the program, which is exactly what we wanted because

164
00:12:35,160 --> 00:12:36,720
‫we're calculating the base address.

165
00:12:37,110 --> 00:12:39,900
‫OK, so we now want to calculate the end address as well.

166
00:12:39,930 --> 00:12:41,760
‫So we're just going to go unsign in it.

167
00:12:42,270 --> 00:12:51,200
‫And Virtual Address equals program had a P V address plus P program had a PDF file size.

168
00:12:51,720 --> 00:13:03,180
‫OK, and now we're going to say if our file virtual end address is below our equal void pointer and

169
00:13:03,180 --> 00:13:11,130
‫virtual address or the file virtual end address is equal to zero.

170
00:13:12,600 --> 00:13:21,840
‫So we go our file virtual and address equals void pointer and the virtual address.

171
00:13:22,410 --> 00:13:32,150
‫And then we also set the physical and address to the elf memory, the elf file plus the program header

172
00:13:34,140 --> 00:13:38,310
‫P offset, plus the program had a PDF file size.

173
00:13:39,600 --> 00:13:44,160
‫OK, so if you, if you use the dumbbell futility and go through this law makes sense.

174
00:13:44,160 --> 00:13:50,460
‫OK, because for example, with the fiscal end address here, we're doing program based memory, the

175
00:13:50,460 --> 00:13:53,490
‫elf memory, the physical based memory.

176
00:13:54,120 --> 00:14:01,230
‫Plus the program had a P offset, which is four thousand eighty six plus the file size, which is thirty

177
00:14:01,230 --> 00:14:01,430
‫one.

178
00:14:01,440 --> 00:14:03,720
‫So that will obviously give us the physical address.

179
00:14:03,720 --> 00:14:04,050
‫Right.

180
00:14:04,610 --> 00:14:11,780
‫OK, and the virtual address here is calculated simply by taking the virtual the rest of the program

181
00:14:11,790 --> 00:14:19,950
‫and adding on the size and then seeing if the end address is below the new the new one that we've calculated.

182
00:14:20,190 --> 00:14:24,620
‫So we're basically doing the exact opposite is the one that we're doing to calculate the virtual based

183
00:14:24,630 --> 00:14:25,100
‫address.

184
00:14:25,920 --> 00:14:30,210
‫So that should be it for calculating the virtual base and end addresses.

185
00:14:30,750 --> 00:14:32,930
‫So let's just make clean that now.

186
00:14:33,210 --> 00:14:36,930
‫And if all works fine, then we're ready to continue.

187
00:14:36,960 --> 00:14:38,580
‫So we're going to go build that LSH.

188
00:14:43,290 --> 00:14:46,430
‫Uh, you can see it fails to copy across Blang Dobbyn.

189
00:14:47,460 --> 00:14:52,590
‫That's because we in the last lecture, we changed the file to blank today.

190
00:14:52,830 --> 00:14:54,540
‫OK, so we'll fix that in a moment.

191
00:14:54,840 --> 00:15:00,040
‫But anyway, just scroll down through here because we need to make sure our float is available in the

192
00:15:00,840 --> 00:15:02,520
‫offloaded files.

193
00:15:02,530 --> 00:15:08,450
‫So just paste that here like that, OK?

194
00:15:08,460 --> 00:15:10,290
‫And we just need to do one more thing.

195
00:15:10,290 --> 00:15:17,060
‫Actually, we need to make an out of clothes, so we're going to go in it elf clothes, OK?

196
00:15:17,070 --> 00:15:18,930
‫And that's going to take an our file.

197
00:15:21,180 --> 00:15:29,910
‫And all we're going to do here is basically say if the file is not set, then we'll just return zero

198
00:15:29,910 --> 00:15:30,570
‫and ignore it.

199
00:15:30,570 --> 00:15:40,530
‫OK, because they passed us on no pointer in here, otherwise will free the file of memory and we will

200
00:15:40,830 --> 00:15:45,190
‫free the file pointer itself for the elf file.

201
00:15:45,630 --> 00:15:50,040
‫So that should be it for the elf clothes, because we declare memory here for the file and we declare

202
00:15:50,040 --> 00:15:51,440
‫memory here for the elf memory.

203
00:15:51,750 --> 00:15:53,220
‫So that should be absolutely fine.

204
00:15:53,220 --> 00:15:57,650
‫And just copy and paste the elf clothes as well guys and know to return zero here as well guys.

205
00:15:57,660 --> 00:15:57,940
‫OK.

206
00:15:58,980 --> 00:16:01,440
‫In fact this can be void's, this can be void.

207
00:16:01,440 --> 00:16:02,510
‫We don't care about that.

208
00:16:02,940 --> 00:16:03,720
‫This can be void.

209
00:16:03,720 --> 00:16:05,200
‫Just just use a void.

210
00:16:05,220 --> 00:16:08,910
‫Guys, how am I going to copy and paste this into the header now?

211
00:16:09,850 --> 00:16:15,370
‫OK, perfect, so let's just check to that builds one small.

212
00:16:17,320 --> 00:16:18,220
‫Yes, it does.

213
00:16:18,220 --> 00:16:18,820
‫Perfect.

214
00:16:19,060 --> 00:16:21,760
‫So let's just fix that makefile problem quickly.

215
00:16:21,760 --> 00:16:27,880
‫So just scroll down into the makefile here and you'll see that we're copying blank dobbyn just change.

216
00:16:27,880 --> 00:16:30,210
‫That's a blank tarof, OK?

217
00:16:30,820 --> 00:16:35,150
‫And then that'll mean that we're copying blanked out after Mounty then OK.

218
00:16:35,150 --> 00:16:36,340
‫And we now just build again.

219
00:16:36,340 --> 00:16:43,590
‫We can see that that works correctly now and we've copied blank to delve into the operating system code.

220
00:16:44,560 --> 00:16:46,360
‫So we've kind of made our low load.

221
00:16:46,470 --> 00:16:48,110
‫Now it's correct.

222
00:16:48,580 --> 00:16:52,990
‫So what we're going to do on the next lecture, we're going to actually use the functionality we've

223
00:16:52,990 --> 00:16:56,940
‫made here to load the file.

224
00:16:57,400 --> 00:16:59,030
‫So that's in task process.

225
00:16:59,030 --> 00:17:04,330
‫Start see, you'll see that we have the process slow binary or we're going to make a new one called

226
00:17:04,330 --> 00:17:10,270
‫Process Load LTH, OK, and we will basically load the whole file essentially.

227
00:17:10,690 --> 00:17:12,820
‫So thanks so much for watching and take care.

