| 12 | } |
| 13 | |
| 14 | void MemoryManager::initialize() |
| 15 | { |
| 16 | this->totalMemory = 0; |
| 17 | this->totalMemory = getTotalMemory(); |
| 18 | |
| 19 | // 预留的内存 |
| 20 | int usedMemory = 256 * PAGE_SIZE + 0x100000; |
| 21 | if (this->totalMemory < usedMemory) |
| 22 | { |
| 23 | printf("memory is too small, halt.\n"); |
| 24 | asm_halt(); |
| 25 | } |
| 26 | // 剩余的空闲的内存 |
| 27 | int freeMemory = this->totalMemory - usedMemory; |
| 28 | |
| 29 | int freePages = freeMemory / PAGE_SIZE; |
| 30 | int kernelPages = freePages / 2; |
| 31 | int userPages = freePages - kernelPages; |
| 32 | |
| 33 | int kernelPhysicalStartAddress = usedMemory; |
| 34 | int userPhysicalStartAddress = usedMemory + kernelPages * PAGE_SIZE; |
| 35 | |
| 36 | int kernelPhysicalBitMapStart = BITMAP_START_ADDRESS; |
| 37 | int userPhysicalBitMapStart = kernelPhysicalBitMapStart + ceil(kernelPages, 8); |
| 38 | int kernelVirtualBitMapStart = userPhysicalBitMapStart + ceil(userPages, 8); |
| 39 | |
| 40 | kernelPhysical.initialize( |
| 41 | (char *)kernelPhysicalBitMapStart, |
| 42 | kernelPages, |
| 43 | kernelPhysicalStartAddress); |
| 44 | |
| 45 | userPhysical.initialize( |
| 46 | (char *)userPhysicalBitMapStart, |
| 47 | userPages, |
| 48 | userPhysicalStartAddress); |
| 49 | |
| 50 | kernelVirtual.initialize( |
| 51 | (char *)kernelVirtualBitMapStart, |
| 52 | kernelPages, |
| 53 | KERNEL_VIRTUAL_START); |
| 54 | |
| 55 | printf("total memory: %d bytes ( %d MB )\n", |
| 56 | this->totalMemory, |
| 57 | this->totalMemory / 1024 / 1024); |
| 58 | |
| 59 | printf("kernel pool\n" |
| 60 | " start address: 0x%x\n" |
| 61 | " total pages: %d ( %d MB )\n" |
| 62 | " bitmap start address: 0x%x\n", |
| 63 | kernelPhysicalStartAddress, |
| 64 | kernelPages, kernelPages * PAGE_SIZE / 1024 / 1024, |
| 65 | kernelPhysicalBitMapStart); |
| 66 | |
| 67 | printf("user pool\n" |
| 68 | " start address: 0x%x\n" |
| 69 | " total pages: %d ( %d MB )\n" |
| 70 | " bit map start address: 0x%x\n", |
| 71 | userPhysicalStartAddress, |