1 00:00:01,380 --> 00:00:08,039 In the lecture, we will see how to free memory. Suppose we want to free a page, 2 00:00:08,039 --> 00:00:14,310 we first locate the entry in the page directory table and free the page using the physical address saved in the entry. 3 00:00:16,200 --> 00:00:21,720 When we want to free the whole memory, we will free the physical pages first, 4 00:00:21,720 --> 00:00:27,360 then we free page directory tables, page directory pointer tables and the page map level four table. 5 00:00:28,380 --> 00:00:34,890 Generally, we could have a few page directory pointer tables and page directory tables. 6 00:00:34,890 --> 00:00:36,840 So we need to loop through each of the tables in the code. 7 00:00:37,920 --> 00:00:39,260 OK, let's get started. 8 00:00:41,170 --> 00:00:43,150 We opened the memory.c file. 9 00:00:45,900 --> 00:00:49,530 The first function we are going to talk about is free pages. 10 00:00:51,070 --> 00:00:57,820 It is like the reverse process of mapping pages. The index is used to locate the correct entry in the page directory table 11 00:00:57,820 --> 00:00:58,600 . 12 00:00:59,720 --> 00:01:05,880 When the function is called, we assume that the vstart and vend are page aligned. So we use assert 13 00:01:05,900 --> 00:01:07,040 to check the condition. 14 00:01:08,880 --> 00:01:15,810 Ok, after the checks pass, we find the page directory table. Notice that we specify the alloc with value 0, 15 00:01:15,810 --> 00:01:21,330 which means it returns null if the entry does not exist. 16 00:01:21,330 --> 00:01:23,430 Because what we want to do here is free the existing page. 17 00:01:24,520 --> 00:01:28,900 If the return table is valid, we use the index to find the corresponding entry. 18 00:01:29,990 --> 00:01:34,940 The assert tests the present bit which should be set when we are about to free it. 19 00:01:36,010 --> 00:01:41,800 Because the table in this case is page directory table, the entry in the page directory table is 20 00:01:41,800 --> 00:01:43,510 pointing to 2m physical page. 21 00:01:43,870 --> 00:01:47,190 The lower 21 bits should be cleared before we use the address. 22 00:01:47,590 --> 00:01:48,730 So we use the macro 23 00:01:48,760 --> 00:01:50,370 pte address to do that. 24 00:01:51,220 --> 00:01:57,520 Then we convert the physical address to virtual address and call function kfree to free the page. 25 00:01:57,520 --> 00:01:57,820 After that, 26 00:01:57,850 --> 00:02:02,530 don’t forget to clear the entry in the table indicating that the entry now is unused. 27 00:02:03,730 --> 00:02:08,530 Ok we move to the next page by adding the page size and continue the process until we reach 28 00:02:08,530 --> 00:02:09,759 the end of the memory region. 29 00:02:11,220 --> 00:02:14,160 What we are going to talk about next is the function 30 00:02:16,400 --> 00:02:22,580 free vm. When we build a process in the following lectures, we will create vm and free it when the process exits 31 00:02:22,580 --> 00:02:23,660 . 32 00:02:24,680 --> 00:02:30,680 To free the vm, we will free the physical pages in the user space as well as page translation tables. 33 00:02:31,850 --> 00:02:35,530 Because we need the page translation tables to locate the physical pages, 34 00:02:35,900 --> 00:02:39,500 we will first free the pages by calling function free pages. 35 00:02:40,040 --> 00:02:42,860 Then we free the page directory tables 36 00:02:42,890 --> 00:02:44,270 and page directory pointer tables, 37 00:02:45,400 --> 00:02:51,910 the last one is page map level 4 table. You see, the higher-level page tables are freed after the lower-level ones, 38 00:02:51,910 --> 00:02:55,690 because we need them to locate the lower-level tables. 39 00:02:56,730 --> 00:03:02,060 It’s worth mentioning that we comment out the free pages function at this point 40 00:03:02,070 --> 00:03:08,130 because we haven’t implemented processes and user space. There is no memory pages allocated in the user space so far 41 00:03:08,130 --> 00:03:08,490 . 42 00:03:09,880 --> 00:03:10,980 OK, let's move on. 43 00:03:13,640 --> 00:03:19,490 In the function free pdt, we will loop through the pml4 table and page directory pointer tables 44 00:03:19,550 --> 00:03:26,720 to find page directory tables. We define a variable map entry pointing to pml4 table. 45 00:03:27,470 --> 00:03:32,300 Since each entry in the page map level 4 table points to page directory pointer table, 46 00:03:32,630 --> 00:03:37,000 we could have a total of 512 pdp tables. 47 00:03:37,010 --> 00:03:39,430 Here we use for loop to loop through each of the entries. 48 00:03:40,130 --> 00:03:43,430 If the entry is present, we will retrieve the address of the entry. 49 00:03:44,410 --> 00:03:45,640 This address is 50 00:03:45,800 --> 00:03:47,470 the address of page directory pointer table. 51 00:03:48,360 --> 00:03:54,000 The pdp table also includes 512 entries which point to page directory tables. 52 00:03:55,240 --> 00:04:00,760 So we loop through the entries as well. If this is a valid entry, we just free the page 53 00:04:00,760 --> 00:04:07,000 and clear the entry meaning that this entry is not used. When we return, the page directory tables are freed. 54 00:04:09,270 --> 00:04:13,550 The free pdpt function is used to free page directory pointer tables. 55 00:04:14,070 --> 00:04:18,360 So we define a variable map entry which holds the address of pml4 table. 56 00:04:19,140 --> 00:04:22,410 Then we loop through each of the 512 entries in the table. 57 00:04:23,410 --> 00:04:28,960 If the entry is used, we will free the page pointed to by the entry, which is the page directory pointer table. 58 00:04:28,960 --> 00:04:32,550 Then we clear the entry. 59 00:04:34,990 --> 00:04:40,690 The free pml4t function is simple, we do nothing but call function kfree to 60 00:04:40,690 --> 00:04:42,100 free the page map level 4 table. 61 00:04:43,330 --> 00:04:49,960 Ok that’s it. In this example, we don’t do the test because we haven’t implemented processes. 62 00:04:49,960 --> 00:04:54,070 When we have user space in the processes, we will use this function to free the vm. 63 00:04:55,140 --> 00:05:00,980 Before we end this lecture, one more thing I need to mention is that the page translation table is 4k in size 64 00:05:00,990 --> 00:05:01,440 . 65 00:05:02,460 --> 00:05:08,640 The page size we use in the sytem is 2m. In the examples, for simplicity, 66 00:05:08,640 --> 00:05:10,700 we allocated a new page to each page translation table. 67 00:05:11,340 --> 00:05:15,510 Therefore, a large chuck of memory in the translation table pages is wasted. 68 00:05:16,850 --> 00:05:18,470 OK, that's it for this lecture.