2 * Physical memory allocation
3 * Copyright (c) 2001,2003,2004 David H. Hovemeyer <daveho@cs.umd.edu>
4 * Copyright (c) 2003, Jeffrey K. Hollingsworth <hollings@cs.umd.edu>
7 * This is free software. You are permitted to use,
8 * redistribute, and modify it as specified in the file "COPYING".
11 #include <geekos/defs.h>
12 #include <geekos/ktypes.h>
13 #include <geekos/kassert.h>
14 #include <geekos/bootinfo.h>
15 #include <geekos/gdt.h>
16 #include <geekos/screen.h>
17 #include <geekos/int.h>
18 #include <geekos/malloc.h>
19 #include <geekos/string.h>
20 #include <geekos/mem.h>
23 #include <geekos/serial.h>
25 /* ----------------------------------------------------------------------
27 * ---------------------------------------------------------------------- */
30 * List of Page structures representing each page of physical memory.
32 struct Page* g_pageList;
35 * Number of pages currently available on the freelist.
37 uint_t g_freePageCount = 0;
39 /* ----------------------------------------------------------------------
40 * Private data and functions
41 * ---------------------------------------------------------------------- */
46 extern int debugFaults;
47 #define Debug(args...) if (debugFaults) Print(args)
50 * List of pages available for allocation.
52 static struct Page_List s_freeList;
55 * Total number of physical pages.
57 int unsigned s_numPages;
60 * Add a range of pages to the inventory of physical memory.
62 static void Add_Page_Range(ulong_t start, ulong_t end, int flags)
66 PrintBoth("Start: %u, End: %u\n", (unsigned int)start, (unsigned int)end);
68 KASSERT(Is_Page_Multiple(start));
69 KASSERT(Is_Page_Multiple(end));
72 //Print("Adding %lu pages\n", (end - start) / PAGE_SIZE);
74 for (addr = start; addr < end; addr += PAGE_SIZE) {
75 // Print("Adding Page at %u\n", (unsigned int)addr);
76 struct Page *page = Get_Page(addr);
80 if (flags == PAGE_AVAIL) {
81 /* Add the page to the freelist */
82 Add_To_Back_Of_Page_List(&s_freeList, page);
84 /* Update free page count */
87 Set_Next_In_Page_List(page, 0);
88 Set_Prev_In_Page_List(page, 0);
92 // Print("%d pages now in freelist\n", g_freePageCount);
96 /* ----------------------------------------------------------------------
98 * ---------------------------------------------------------------------- */
101 * The linker defines this symbol to indicate the end of
102 * the executable image.
107 * Initialize memory management data structures.
108 * Enables the use of Alloc_Page() and Free_Page() functions.
110 void Init_Mem(struct Boot_Info* bootInfo)
112 ulong_t numPages = bootInfo->memSizeKB >> 2;
113 ulong_t endOfMem = numPages * PAGE_SIZE;
114 unsigned numPageListBytes = sizeof(struct Page) * numPages;
115 ulong_t pageListAddr;
120 KASSERT(bootInfo->memSizeKB > 0);
122 if (bootInfo->memSizeKB != TOP_OF_MEM/1024) {
123 PrintBoth("Kernel compiled for %d KB machine, but machine claims %d KB\n",TOP_OF_MEM/1024,bootInfo->memSizeKB);
124 if (bootInfo->memSizeKB < TOP_OF_MEM/1024) {
125 PrintBoth("Kernel compiled for more memory than machine has. Panicking\n");
131 bootInfo->memSizeKB = TOP_OF_MEM / 1024;
134 * Before we do anything, switch from setup.asm's temporary GDT
135 * to the kernel's permanent GDT.
140 * We'll put the list of Page objects right after the end
141 * of the kernel, and mark it as "kernel". This will bootstrap
142 * us sufficiently that we can start allocating pages and
143 * keeping track of them.
146 // JRL: This is stupid...
147 // with large mem sizes the page list overruns into the ISA
148 // hole. By blind luck this causes an unrelated assertion failure, otherwise
149 // I might never have caught it...
150 // We fix it by moving the page list after the kernel heap...
151 // For now we'll make our own stupid assumption that the mem size
152 // is large enough to accomodate the list in high mem.
154 PrintBoth("Total Memory Size: %u MBytes\n", bootInfo->memSizeKB/1024);
158 PrintBoth("Page struct size: %u bytes\n", sizeof(struct Page));
159 PrintBoth("Page List Size: %u bytes\n", numPageListBytes);
162 //pageListAddr = Round_Up_To_Page((ulong_t) &end);
163 //pageListAddr = Round_Up_To_Page(HIGHMEM_START + KERNEL_HEAP_SIZE);
165 // Note that this is now moved to be just above the kernel heap
166 // see defs.h for layout
167 pageListAddr=Round_Up_To_Page(KERNEL_PAGELIST);
169 pageListEnd = Round_Up_To_Page(pageListAddr + numPageListBytes);
171 g_pageList = (struct Page*) pageListAddr;
172 // kernEnd = Round_Up_To_Page(pageListAddr + numPageListBytes);
174 // PAD - Note: I am changing this so that everything through the end of
175 // the VM boot package (bioses/vmxassist) is off limits
176 //kernEnd = Round_Up_To_Page((ulong_t) &end);
177 kernEnd = Round_Up_To_Page(end);
178 s_numPages = numPages;
180 PrintBoth("Pagelist addr: %p\n", g_pageList);
181 PrintBoth("index: %p\n", &g_pageList[3]);
182 PrintBoth("direct offset: %p\n", g_pageList + (sizeof(struct Page) * 2));
183 // PrintBoth("Kernel Size=%lx\n", (kernEnd - KERNEL_START_ADDR));
184 // PrintBoth("Kernel Start=%x\n", KERNEL_START_ADDR);
185 PrintBoth("Kernel End=%lx\n", kernEnd);
186 //PrintBoth("end=%x\n", end);
190 * The initial kernel thread and its stack are placed
191 * just beyond the ISA hole.
193 // This is no longer true
194 // KASSERT(ISA_HOLE_END == KERN_THREAD_OBJ);
196 //KASSERT(KERN_THREAD_OBJ==(START_OF_VM+VM_SIZE));
197 //KASSERT(KERN_STACK == KERN_THREAD_OBJ + PAGE_SIZE);
200 * Memory looks like this:
201 * 0 - start: available (might want to preserve BIOS data area)
202 * start - end: kernel
203 * end - ISA_HOLE_START: available
204 * ISA_HOLE_START - ISA_HOLE_END: used by hardware (and ROM BIOS?)
205 * ISA_HOLE_END - HIGHMEM_START: used by initial kernel thread
206 * HIGHMEM_START - end of memory: available
207 * (the kernel heap is located at HIGHMEM_START; any unused memory
208 * beyond that is added to the freelist)
214 // The kernel is still in low memory at this point, in the VM region
215 // Thus we will mark it as kernel use
216 // Add_Page_Range(KERNEL_START_ADDR, kernEnd, PAGE_KERN);
219 //Add_Page_Range(kernEnd, ISA_HOLE_START, PAGE_AVAIL);
220 // ISA hole remains closed (no actual memory)
221 // Add_Page_Range(ISA_HOLE_START, ISA_HOLE_END, PAGE_HW);
223 //Add_Page_Range(ISA_HOLE_END, HIGHMEM_START, PAGE_ALLOCATED);
224 // Add_Page_Range(HIGHMEM_START, HIGHMEM_START + KERNEL_HEAP_SIZE, PAGE_HEAP);
225 //Add_Page_Range(HIGHMEM_START + KERNEL_HEAP_SIZE, endOfMem, PAGE_AVAIL);
226 /* JRL: move page list after kernel heap */
228 //Now, above the VM region...
230 // Kernel thread object
231 Add_Page_Range(KERNEL_THREAD_OBJECT,KERNEL_THREAD_OBJECT+KERNEL_THREAD_OBJECT_SIZE,PAGE_ALLOCATED);
233 Add_Page_Range(KERNEL_STACK,KERNEL_STACK+KERNEL_STACK_SIZE,PAGE_ALLOCATED);
235 Add_Page_Range(KERNEL_HEAP,KERNEL_HEAP+KERNEL_HEAP_SIZE,PAGE_HEAP);
237 Add_Page_Range(pageListAddr, pageListEnd, PAGE_KERN);
239 Add_Page_Range(pageListEnd,Round_Down_To_Page(FINAL_KERNEL_START), PAGE_AVAIL);
241 Add_Page_Range(Round_Down_To_Page(FINAL_KERNEL_START),Round_Up_To_Page(FINAL_VMBOOTEND+1),PAGE_KERN);
243 // IDT (this should be one page)
244 Add_Page_Range(IDT_LOCATION,TSS_LOCATION,PAGE_KERN);
245 // TSS (this should be one page)
246 Add_Page_Range(TSS_LOCATION,GDT_LOCATION, PAGE_KERN);
247 // GDT (this should be one page)
248 Add_Page_Range(GDT_LOCATION,TOP_OF_MEM, PAGE_KERN);
250 /* Initialize the kernel heap */
251 Init_Heap(KERNEL_HEAP, KERNEL_HEAP_SIZE);
253 PrintBoth("%uKB memory detected, %u pages in freelist, %d bytes in kernel heap\n",
254 bootInfo->memSizeKB, g_freePageCount, KERNEL_HEAP_SIZE);
256 PrintBoth("Memory Layout:\n");
258 PrintBoth("%x to %x - INITIAL THREAD\n",KERNEL_THREAD_OBJECT,KERNEL_THREAD_OBJECT+KERNEL_THREAD_OBJECT_SIZE-1);
259 PrintBoth("%x to %x - KERNEL STACK\n",KERNEL_STACK,KERNEL_STACK+KERNEL_STACK_SIZE-1);
260 PrintBoth("%x to %x - KERNEL HEAP\n",KERNEL_HEAP,KERNEL_HEAP+KERNEL_HEAP_SIZE-1);
261 PrintBoth("%lx to %lx - PAGE LIST\n",pageListAddr,pageListEnd-1);
262 PrintBoth("%lx to %x - FREE\n",pageListEnd,FINAL_KERNEL_START-1);
263 PrintBoth("%x to %x - KERNEL CODE\n",FINAL_KERNEL_START,FINAL_KERNEL_END);
264 PrintBoth("%x to %x - IDT\n",IDT_LOCATION,TSS_LOCATION-1);
265 PrintBoth("%x to %x - TSS\n",TSS_LOCATION,GDT_LOCATION-1);
266 PrintBoth("%x to %x - GDT\n",GDT_LOCATION,TOP_OF_MEM-1);
272 * Initialize the .bss section of the kernel executable image.
276 extern char BSS_START, BSS_END;
278 /* Fill .bss with zeroes */
279 memset(&BSS_START, '\0', &BSS_END - &BSS_START);
280 PrintBoth("BSS Inited, BSS_START=%x, BSS_END=%x\n",BSS_START,BSS_END);
284 * Allocate a page of physical memory.
286 void* Alloc_Page(void)
291 bool iflag = Begin_Int_Atomic();
293 /* See if we have a free page */
294 if (!Is_Page_List_Empty(&s_freeList)) {
295 /* Remove the first page on the freelist. */
296 page = Get_Front_Of_Page_List(&s_freeList);
297 KASSERT((page->flags & PAGE_ALLOCATED) == 0);
298 Remove_From_Front_Of_Page_List(&s_freeList);
300 /* Mark page as having been allocated. */
301 page->flags |= PAGE_ALLOCATED;
303 result = (void*) Get_Page_Address(page);
306 End_Int_Atomic(iflag);
312 * Free a page of physical memory.
314 void Free_Page(void* pageAddr)
316 ulong_t addr = (ulong_t) pageAddr;
320 iflag = Begin_Int_Atomic();
322 KASSERT(Is_Page_Multiple(addr));
324 /* Get the Page object for this page */
325 page = Get_Page(addr);
326 KASSERT((page->flags & PAGE_ALLOCATED) != 0);
328 /* Clear the allocation bit */
329 page->flags &= ~(PAGE_ALLOCATED);
331 /* Put the page back on the freelist */
332 Add_To_Back_Of_Page_List(&s_freeList, page);
335 End_Int_Atomic(iflag);