| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- /* Copyright (C) 2021 Rain */
- /* This file is part of XNIX. */
- /*
- XNIX is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) and later version.
- */
- /*
- XNIX is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- */
- /*
- You should have received a copy of the GNU General Public License
- along with XNIX. If not, see <https://www.gnu.org/licenses/>.
- */
- #include <kernel/init.h>
- #include <kernel/errno.h>
- #include <kernel/types.h>
- #include <kernel/graphic.h>
- #include <kernel/int.h>
- #include <kernel/mm.h>
- #include <lib/bitmap.h>
- #include <lib/string.h>
- #include <arch/x86/int.h>
- #include <arch/io.h>
- #include <arch/sys.h>
- struct sysmm *mm;
- void mm_init(void) {
- mm = (struct sysmm *) PAGE_4K_ALIGN((__uint64_t) &_end);
-
- struct e820_entry *e820 = E820_ADDR;
- struct zone *zones = (struct zone *) (((char *) mm) + sizeof(struct sysmm));
- int i;
- __uint64_t total_ram = 0;
- struct e820_entry *e = e820;
- memset(mm, 0x00, sizeof(struct sysmm));
- for (i = 0; i < 32; i++) {
- if (!e->type || e->type >= E820_INV)
- break;
- printk(0x00ffffff, 0x00000000, "address %q, length %q KB, type %d\n", e->addr, e->len >> 10, e->type);
- /* create a `struct zone` for this */
- #define z (zones + mm->total_zones)
- z->type = e->type;
- z->total_pages = e->len >> PAGE_SHIFT;
- z->start_addr = e->addr;
- z->end_addr = e->addr + e->len;
- z->size = e->len;
- z->using = 0;
- mm->total_zones++;
- if (e->type == E820_RAM)
- total_ram += e->len;
- mm->total_pages += e->len >> PAGE_SHIFT;
- e++;
- }
- printk(0x00ffffff, 0x00000000, "total usable ram: %d MB. \n", total_ram >> 20);
- printk(0x00ffffff, 0x00000000, "total pages: %d (includes unusable pages) = %d MB\n", mm->total_pages, mm->total_pages << PAGE_SHIFT >> 20);
- printk(0x00ffffff, 0x00000000, "total zones: %d\n", mm->total_zones);
- /* setup page bitmap */
- /* first we should allocate some memory for it */
- /* at end of zones */
- mm->bitmap = (__uint8_t *) (zones + mm->total_zones);
- /* set all bits. if this bit points to a RAM page, clear it */
- /* it always not bad. */
- #define BYTESNEED ((mm->total_pages >> 3) + 1)
- memset(mm->bitmap, 0xff, BYTESNEED);
- e = e820;
- for (i = 0; i < mm->total_zones; i++) {
- #undef z
- #define z (zones + i)
- if (z->type == E820_RAM) {
- /* clear bits in bitmap */
- int j;
- __uint64_t start_page = PAGE_ALIGN(z->start_addr) >> PAGE_SHIFT;
- for (j = start_page; j < start_page + z->total_pages; j++)
- bitmap_set(mm->bitmap, j, 0);
- }
- }
- /*
- * the first and second page is for kernel,
- * and the 3rd, 4th, 5th are for frame buffer (they setup in boot/loader.asm).
- * */
- /* 0MB */
- bitmap_set(mm->bitmap, 0, 1);
- /* 2MB */
- bitmap_set(mm->bitmap, 1, 1);
- /* 4MB (FB_ADDR) */
- bitmap_set(mm->bitmap, 2, 1);
- /* 6MB */
- bitmap_set(mm->bitmap, 3, 1);
- /* 8MB */
- bitmap_set(mm->bitmap, 4, 1);
- /* so next free page is 10MB (0xa00000) */
- return;
- #undef z
- }
- void *alloc_page(void) {
- __uint64_t i;
- for (i = 0; bitmap_read(mm->bitmap, i); i++) {
- if (i > mm->total_pages)
- return NULL;
- }
- bitmap_set(mm->bitmap, i, 1);
- return (void *) (i << PAGE_SHIFT);
- }
- void free_page(void *p) {
- __uint64_t i;
- i = (__uint64_t) p >> PAGE_SHIFT;
- bitmap_set(mm->bitmap, i, 0);
- return;
- }
|