| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- /* 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/x86/paging.h>
- #include <arch/io.h>
- #include <arch/sys.h>
- struct sysmm *mm;
- struct mempool pool;
- 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);
- /* so next free page is 10MB (0xa00000) */
- /* now we setup that memory pool */
- /* default pool size: 2MB (1 page) */
- pool.size = 0x200000;
- pool.used = 0;
- pool.entries = 0x200000 >> POOL_ENTRY_SHIFT;
- /* 32 bytes per bit */
- /* we don't want use alloc_page() here, because it just want
- * 4 KB, but alloc_page() gives us 2MB.
- *
- * so we must find a place to store this.
- *
- * and after that we can use kmalloc() to alloc this.
- * (this is only for starting-up)
- *
- * it's at the end of syslog-buffer.
- * */
- pool.bitmap = (__uint8_t *) (SYSLOG_BUFFER + 32768);
- memset(pool.bitmap, 0, 0x200000 / 64 / 8);
- pool.page = alloc_page();
- pool.next = NULL;
- return;
- #undef z
- }
- /* find an empty page table entry, and return its number */
- void *mapping(void *addr, __uint64_t attr) {
- __uint64_t i;
- /* we just use the (max) 1 GB memory in kernel (512 pages) */
- #define PT_ADDR ((__uint64_t *) 0x2000)
- for (i = 0; i < 512; i++) {
- if (!PT_ADDR[i]) {
- PT_ADDR[i] = (__uint64_t) addr | attr;
- invalidate();
- goto get_vaddr;
- }
- }
- return NULL;
- get_vaddr:
- /* get virtual address */
- /* drunk, do it later. */
- __uint64_t vaddr = i << PAGE_SHIFT;
- return (void *) vaddr;
- #undef PT_ADDR
- }
- 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);
- /* we must map it to page-table */
- void *addr = mapping((void *) (i << PAGE_SHIFT), PATT_KERNEL);
- if (!addr) {
- bitmap_set(mm->bitmap, i, 0);
- return NULL;
- }
- return addr;
- }
- void free_page(void *p) {
- __uint64_t i;
- i = (__uint64_t) p >> PAGE_SHIFT;
- bitmap_set(mm->bitmap, i, 0);
- return;
- }
- void *kmalloc(struct mempool *pool, __uint64_t size) {
- if (!size || size > pool->size - pool->used)
- return NULL;
- __uint64_t points = ((size + 63) & ~63) >> POOL_ENTRY_SHIFT;
- __uint64_t i;
- if (points == 1) {
- for (i = 0; bitmap_read(pool->bitmap, i); i++) {
- if (i > pool->entries)
- return NULL;
- }
- bitmap_set(pool->bitmap, i, 1);
- pool->used++;
- return pool->page + (i << POOL_ENTRY_SHIFT);
- }
- printk(0x00ffffff, 0x00000000, "need %d points. \n", points);
- /* it's not so good... */
- for (i = 0; i < pool->entries; i++) {
- repeat:
- /* if all 0... */
- __uint64_t j;
- for (j = 0; j < points; j++) {
- if (bitmap_read(pool->bitmap, i + j)) {
- i++;
- if (i == pool->entries)
- goto panic;
- goto repeat;
- }
- }
- /* it will here when we found enough */
- for (j = 0; j < points; j++)
- bitmap_set(pool->bitmap, i + j, 1);
- pool->used += points;
- return pool->page + (i << POOL_ENTRY_SHIFT);
- }
- panic:
- printk(0x00ff0000, 0x00000000, "kmalloc: no enough memory (total %d KB, %d bytes used, requested %d bytes)\n",
- pool->size >> 10, pool->used << 6, points << 6);
- return NULL;
- }
- void kfree(struct mempool *pool, void *p, __uint64_t size) {
- __uint64_t points = ((size + 63) & ~63) >> POOL_ENTRY_SHIFT;
- __uint64_t i;
-
- i = (p - pool->page) >> POOL_ENTRY_SHIFT;
- for (; points; points--, i++)
- bitmap_set(pool->bitmap, i, 0);
- pool->used -= points;
- return;
- }
- void trace_mm(void) {
- printk(0x0000ff00, 0x00000000, "tracing system memory: \n");
- __uint64_t *cr3 = (__uint64_t *) get_cr3();
- printk(0x0000ff00, 0x00000000, "pml4: 0x%q pdp: 0x%q pt: 0x%q\n",
- cr3, *cr3 & ~0xff, *(__uint64_t *) (*cr3 & ~0xff) & ~0xff);
- __uint64_t *pt = (__uint64_t *) (*(__uint64_t *) (*cr3 & ~0xff) & ~0xff);
- int i;
- for (i = 0; i < 512; i++) {
- if (pt[i])
- printk(0x0000ff00, 0x00000000, "%q -> %q\n", i << PAGE_SHIFT, pt[i] & ~0xff);
- }
-
- return;
- }
|