Implement serial logging. Finish modularizing power management.
[monolithium.git] / kernel / src / memory / memory.c
index 73d0f708b7265efcb393da3c80227fdf065f9573..f2baea3b10abf584b3db8228a344dc16a27d604a 100644 (file)
@@ -18,6 +18,7 @@
  */
 
 #include <memory.h>
+#include <log.h>
 #include <exception.h>
 #include <process.h>
 #include <syscalls.h>
@@ -885,8 +886,10 @@ dword_t map_memory_in_address_space(memory_address_space_t *address_space,
 {
     dword_t ret;
     void *address = (void*)PAGE_ALIGN((uintptr_t)*virtual);
+    uintptr_t aligned_physical = PAGE_ALIGN((uintptr_t)physical);
+    if (*virtual != NULL && PAGE_OFFSET((uintptr_t)*virtual) != PAGE_OFFSET((uintptr_t)physical)) return ERR_INVALID;
 
-    size = PAGE_ALIGN_UP(size) >> 12;
+    size = (PAGE_ALIGN_UP((uintptr_t)physical + size - 1) - aligned_physical) >> 12;
     acquire_resource_exclusive(&address_space->resource);
 
     memory_block_t *block = find_free_block(address_space, address, size);
@@ -904,7 +907,7 @@ dword_t map_memory_in_address_space(memory_address_space_t *address_space,
     if (block_flags & MEMORY_BLOCK_WRITABLE) flags |= PAGE_WRITABLE;
     if (block_flags & MEMORY_BLOCK_USERMODE) flags |= PAGE_USERMODE;
 
-    ret = map_memory_internal(physical, (void*)real_address, size * PAGE_SIZE, flags);
+    ret = map_memory_internal((void*)aligned_physical, (void*)real_address, size * PAGE_SIZE, flags);
     if (ret != ERR_SUCCESS)
     {
         release_resource(&address_space->resource);
@@ -2594,8 +2597,8 @@ void memory_init(multiboot_tag_mmap_t *mmap, uintptr_t lowest_physical)
 
     fix_overlapping_sections(mmap);
 
-    puts("\nMemory map:\n\nBase\t\t\tLength\t\t\tType");
-    puts("------------------------------------------------------------");
+    log_write(LOG_NORMAL, "Memory map:\nBase\t\t\tLength\t\t\tType\n");
+    log_write(LOG_NORMAL, "------------------------------------------------------------\n");
 
     multiboot_mmap_entry_t *entry;
 
@@ -2603,12 +2606,12 @@ void memory_init(multiboot_tag_mmap_t *mmap, uintptr_t lowest_physical)
          (uintptr_t)entry < ((uintptr_t)mmap + mmap->size);
          entry = (multiboot_mmap_entry_t*)((uintptr_t)entry + mmap->entry_size))
     {
-        printf("0x%08X%08X\t0x%08X%08X\t%s\n",
-               entry->base_high,
-               entry->base_low,
-               entry->length_high,
-               entry->length_low,
-               (entry->type == 1) ? "Usable" : "Not Usable");
+        log_write(LOG_NORMAL, "0x%08X%08X\t0x%08X%08X\t%s\n",
+                  entry->base_high,
+                  entry->base_low,
+                  entry->length_high,
+                  entry->length_low,
+                  (entry->type == 1) ? "Usable" : "Not Usable");
 
         if (entry->type == 1
             && entry->base_high == 0
@@ -2654,7 +2657,7 @@ void memory_init(multiboot_tag_mmap_t *mmap, uintptr_t lowest_physical)
         }
     }
 
-    puts("------------------------------------------------------------");
+    log_write(LOG_NORMAL, "------------------------------------------------------------\n");
     total_physical_pages += num_free_pages;
     pages = (page_t*)(KERNEL_POOL_START - total_physical_pages * sizeof(page_t));