|
|
@@ -161,6 +161,8 @@ void mm_init(void) {
|
|
|
|
|
|
pool.bitmap = (__uint8_t *) (SYSLOG_BUFFER + 32768);
|
|
|
|
|
|
+ memset(pool.bitmap, 0, 0x200000 / 64 / 8);
|
|
|
+
|
|
|
pool.page = alloc_page();
|
|
|
|
|
|
pool.next = NULL;
|
|
|
@@ -175,62 +177,29 @@ void mm_init(void) {
|
|
|
void *mapping(void *addr, __uint64_t attr) {
|
|
|
__uint64_t i;
|
|
|
|
|
|
- /* well... */
|
|
|
-
|
|
|
-#define PML4E_ADDR ((__uint64_t *) 0x0000)
|
|
|
-#define PDPT_ADDR ((__uint64_t *) 0x1000)
|
|
|
-#define PT_ADDR ((__uint64_t *) 0x2000);
|
|
|
-
|
|
|
- __uint64_t *pml4e = PML4E_ADDR;
|
|
|
- __uint64_t *pdpt = PDPT_ADDR;
|
|
|
- __uint64_t *pt = PT_ADDR;
|
|
|
-
|
|
|
-#define TABLE_ENTRIES 512
|
|
|
+ /* we just use the (max) 1 GB memory in kernel (512 pages) */
|
|
|
|
|
|
- for (i = 0; ((__uint64_t *) pt)[i]; i++) {
|
|
|
- if (i > TABLE_ENTRIES) {
|
|
|
- /* reload pt */
|
|
|
- pt = (__uint64_t *) *(++pdpt);
|
|
|
+#define PT_ADDR ((__uint64_t *) 0x2000)
|
|
|
|
|
|
- if (pdpt == PDPT_ADDR + TABLE_ENTRIES) {
|
|
|
- pdpt = (__uint64_t *) *(++pml4e);
|
|
|
- pt = (__uint64_t *) *pdpt;
|
|
|
-
|
|
|
- if (pml4e == PML4E_ADDR + TABLE_ENTRIES)
|
|
|
- return 0;
|
|
|
- }
|
|
|
+ for (i = 0; i < 512; i++) {
|
|
|
+ if (!PT_ADDR[i]) {
|
|
|
+ PT_ADDR[i] = (__uint64_t) addr | attr;
|
|
|
+ invalidate();
|
|
|
+ goto get_vaddr;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /* map it with attribute attr */
|
|
|
-
|
|
|
- pt[i] = (__uint64_t) addr | attr;
|
|
|
+ return NULL;
|
|
|
|
|
|
+get_vaddr:
|
|
|
/* get virtual address */
|
|
|
+ /* drunk, do it later. */
|
|
|
|
|
|
-#define PT_NR i
|
|
|
-#define PDPT_NR ((pdpt - PDPT_ADDR) >> 3)
|
|
|
-#define PML4E_NR ((pml4e - PML4E_ADDR) >> 3)
|
|
|
-
|
|
|
-#define TABLE_SHIFT 9ul
|
|
|
-
|
|
|
- __uint64_t vaddr = (PT_NR << PAGE_SHIFT)
|
|
|
- + (PDPT_NR << TABLE_SHIFT << PAGE_SHIFT)
|
|
|
- + (PML4E_NR << TABLE_SHIFT << PAGE_SHIFT << TABLE_SHIFT << PAGE_SHIFT);
|
|
|
+ __uint64_t vaddr = i << PAGE_SHIFT;
|
|
|
|
|
|
return (void *) vaddr;
|
|
|
|
|
|
-#undef TABLE_SHIFT
|
|
|
-
|
|
|
-#undef PML4E_NR
|
|
|
-#undef PDPT_NR
|
|
|
-#undef PT_NR
|
|
|
-
|
|
|
-#undef TABLE_ENTRIES
|
|
|
-
|
|
|
#undef PT_ADDR
|
|
|
-#undef PDPT_ADDR
|
|
|
-#undef PML4E_ADDR
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -288,6 +257,8 @@ void *kmalloc(struct mempool *pool, __uint64_t size) {
|
|
|
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++) {
|
|
|
@@ -299,6 +270,10 @@ repeat:
|
|
|
for (j = 0; j < points; j++) {
|
|
|
if (bitmap_read(pool->bitmap, i + j)) {
|
|
|
i++;
|
|
|
+
|
|
|
+ if (i == pool->entries)
|
|
|
+ goto panic;
|
|
|
+
|
|
|
goto repeat;
|
|
|
}
|
|
|
}
|
|
|
@@ -306,13 +281,18 @@ repeat:
|
|
|
/* it will here when we found enough */
|
|
|
|
|
|
for (j = 0; j < points; j++)
|
|
|
- bitmap_set(pool->bitmap, j, 1);
|
|
|
+ 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;
|
|
|
}
|
|
|
|
|
|
@@ -330,3 +310,24 @@ void kfree(struct mempool *pool, void *p, __uint64_t size) {
|
|
|
|
|
|
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;
|
|
|
+}
|