Beispiel #1
0
        public MemoryRegion FitToPageCeil()
        {
            var start = KMath.AlignValueCeil(Start, 4096);
            var end   = KMath.AlignValueCeil(Start + Size, 4096);

            return(FromLocation(start, end));
        }
Beispiel #2
0
        internal static unsafe void SetupAllocator()
        {
            Allocator = new RuntimeAllocator();
            var ptr = (byte *)SysCalls.RequestMemory(KMath.AlignValueCeil(Allocator.headSize, 4096));

            for (var i = 0; i < Allocator.headSize; i++)
            {
                *(ptr + i) = 0; // TODO: Optimize
            }
            Allocator.List_heads = (malloc_meta **)ptr;
            AllocatorInitialized = true;
        }
Beispiel #3
0
        private static uint Cmd_RequestMemory(SysCallContext *context, SystemMessage *args)
        {
            var size = args->Arg1;

            size = KMath.AlignValueCeil(size, 4096);
            var proc     = Scheduler.GetCurrentThread().Process;
            var map      = PhysicalPageManager.AllocateRegion(size);
            var virtAddr = proc.UserPageAllocator.AllocatePagesAddr(size / 4096);

            Scheduler.GetCurrentThread().Process.PageTable.Map(virtAddr, map.Start, PhysicalPageManager.GetAllocatorByAddr(map.Start));
            return(virtAddr);
        }
Beispiel #4
0
        public static void Setup()
        {
            KernelMessage.WriteLine("Multiboot Flags: {0:X}", Multiboot.Flags);
            BootInfo            = (BootInfoHeader *)Address.KernelBootInfo;
            BootInfo->Magic     = lonos.kernel.core.BootInfoHeader.BootInfoMagic;
            BootInfo->HeapStart = KMath.AlignValueCeil(Address.OriginalKernelElfSection + LoaderStart.OriginalKernelElf.TotalFileSize, 0x1000);
            BootInfo->HeapSize  = 0;

            BootInfo->InstalledPhysicalMemory = 128 * 1024 * 1024;

            SetupVideoInfo();
            SetupMemoryMap();
        }
Beispiel #5
0
        public static void Setup()
        {
            KernelMessage.WriteLine("Multiboot Flags: {0:X}", Multiboot.Flags);
            BootInfo            = (BootInfoHeader *)Address.KernelBootInfo;
            BootInfo->Magic     = BootInfoHeader.BootInfoMagic;
            BootInfo->HeapStart = KMath.AlignValueCeil(Address.OriginalKernelElfSection + LoaderStart.OriginalKernelElf.TotalFileSize, 0x1000);
            BootInfo->HeapSize  = 0;

            BootInfo->InstalledPhysicalMemory = 256 * 1024 * 1024;
            BootInfo->PageTableType           = KConfig.UsePAE ? PageTableType.PAE : PageTableType.X86;
            BootInfo->KernelBootStartCycles   = PerformanceCounter.KernelBootStartCycles;

            SetupVideoInfo();
            SetupMemoryMap();
        }
Beispiel #6
0
        public unsafe static void Setup()
        {
            kmallocAllocator = new Allocator();

            var ptr = (byte *)RawVirtualFrameAllocator.RequestRawVirtalMemoryPages(KMath.AlignValueCeil(Allocator.headSize, 4096));

            for (var i = 0; i < Allocator.headSize; i++)
            {
                *(ptr + i) = 0;
            }
            kmallocAllocator.list_heads = (malloc_meta **)ptr;
            ManagedMemoy.useAllocator   = true;

            KernelMessage.WriteLine("Memory free: {0} MB", (PageFrameManager.PagesAvailable * 4096) / 1024 / 1024);
        }
Beispiel #7
0
        public static unsafe void Setup()
        {
            kmallocAllocator = new KernelAllocator();

            var ptr = (byte *)VirtualPageManager.AllocatePages(KMath.AlignValueCeil(Allocator.headSize, 4096));

            for (var i = 0; i < Allocator.headSize; i++)
            {
                *(ptr + i) = 0;
            }
            kmallocAllocator.List_heads = (malloc_meta **)ptr;
            ManagedMemory.UseAllocator  = true;
            KernelMessage.WriteLine("EarlyBootBytesUsed: {0} bytes", ManagedMemory.EarlyBootBytesUsed);

            KernelMessage.WriteLine("Memory free: {0} MB", PhysicalPageManager.FreePages * 4096 / 1024 / 1024);
        }
Beispiel #8
0
        static void SetupMemoryMap()
        {
            uint customMaps = 7;
            var  mbMapCount = Multiboot.MemoryMapCount;

            BootInfo->MemoryMapLength = mbMapCount + customMaps;
            BootInfo->MemoryMapArray  = (BootInfoMemory *)MallocBootInfoData((USize)(sizeof(MultiBootMemoryMap) * MemoryMapReserve));

            for (uint i = 0; i < mbMapCount; i++)
            {
                BootInfo->MemoryMapArray[i].Start = Multiboot.GetMemoryMapBase(i);
                BootInfo->MemoryMapArray[i].Size  = Multiboot.GetMemoryMapLength(i);
                var memType = BootInfoMemoryType.Reserved;
                var type    = (BIOSMemoryMapType)Multiboot.GetMemoryMapType(i);

                switch (type)
                {
                case BIOSMemoryMapType.Usable:
                    memType = BootInfoMemoryType.SystemUsable;
                    break;

                case BIOSMemoryMapType.Reserved:
                    memType = BootInfoMemoryType.Reserved;
                    break;

                case BIOSMemoryMapType.ACPI_Relaimable:
                    memType = BootInfoMemoryType.ACPI_Relaimable;
                    break;

                case BIOSMemoryMapType.ACPI_NVS_Memory:
                    memType = BootInfoMemoryType.ACPI_NVS_Memory;
                    break;

                case BIOSMemoryMapType.BadMemory:
                    memType = BootInfoMemoryType.BadMemory;
                    break;

                default:
                    memType = BootInfoMemoryType.Unknown;
                    break;
                }
                BootInfo->MemoryMapArray[i].Type = memType;
            }

            var idx = mbMapCount + 0;

            BootInfo->MemoryMapArray[idx].Start = Address.OriginalKernelElfSection;
            BootInfo->MemoryMapArray[idx].Size  = KMath.AlignValueCeil(LoaderStart.OriginalKernelElf.TotalFileSize, 0x1000);
            BootInfo->MemoryMapArray[idx].Type  = BootInfoMemoryType.OriginalKernelElfImage;

            idx++;
            BootInfo->MemoryMapArray[idx].Start = Address.KernelElfSection;
            BootInfo->MemoryMapArray[idx].Size  = KMath.AlignValueCeil(LoaderStart.OriginalKernelElf.TotalFileSize, 0x1000);
            BootInfo->MemoryMapArray[idx].Type  = BootInfoMemoryType.KernelElf;

            idx++;
            BootInfo->MemoryMapArray[idx].Start = Address.KernelBootInfo;
            BootInfo->MemoryMapArray[idx].Size  = 0x1000;
            BootInfo->MemoryMapArray[idx].Type  = BootInfoMemoryType.BootInfoHeader;

            idx++;
            BootInfo->MemoryMapArray[idx].Start = BootInfo->HeapStart;
            BootInfo->MemoryMapArray[idx].Size  = 0x1000; //TODO: Recaluclate after Setup all Infos
            BootInfo->MemoryMapArray[idx].Type  = BootInfoMemoryType.BootInfoHeap;

            idx++;
            uint stackSize = 0x100000; // 1MB

            BootInfo->MemoryMapArray[idx].Start = Address.InitialStack - stackSize;
            BootInfo->MemoryMapArray[idx].Size  = stackSize;
            BootInfo->MemoryMapArray[idx].Type  = BootInfoMemoryType.InitialStack;

            idx++;
            BootInfo->MemoryMapArray[idx].Start = Address.GCInitialMemory;
            BootInfo->MemoryMapArray[idx].Size  = Address.GCInitialMemorySize;
            BootInfo->MemoryMapArray[idx].Type  = BootInfoMemoryType.InitialGCMemory;

            idx++;
            BootInfo->MemoryMapArray[idx].Start = 0x0;
            BootInfo->MemoryMapArray[idx].Size  = 0xA0000; // 640 KB
            BootInfo->MemoryMapArray[idx].Type  = BootInfoMemoryType.KernelReserved;
        }
Beispiel #9
0
        private static void SetupMemoryMap()
        {
            var mbMapCount = Multiboot.MemoryMapCount;

            BootInfo->MemoryMapArray = (BootInfoMemory *)MallocBootInfoData((USize)(sizeof(MultiBootMemoryMap) * MemoryMapReserve));

            for (uint i = 0; i < mbMapCount; i++)
            {
                BootInfo->MemoryMapArray[i].Start = Multiboot.GetMemoryMapBase(i);
                BootInfo->MemoryMapArray[i].Size  = Multiboot.GetMemoryMapLength(i);
                var memType          = BootInfoMemoryType.Reserved;
                var type             = (BIOSMemoryMapType)Multiboot.GetMemoryMapType(i);
                var addressSpaceKind = AddressSpaceKind.Physical;
                var preMap           = false;

                switch (type)
                {
                case BIOSMemoryMapType.Usable:
                    memType = BootInfoMemoryType.SystemUsable;
                    break;

                case BIOSMemoryMapType.Reserved:
                    memType = BootInfoMemoryType.Reserved;
                    break;

                case BIOSMemoryMapType.ACPI_Relaimable:
                    memType = BootInfoMemoryType.ACPI_Relaimable;
                    break;

                case BIOSMemoryMapType.ACPI_NVS_Memory:
                    memType = BootInfoMemoryType.ACPI_NVS_Memory;
                    break;

                case BIOSMemoryMapType.BadMemory:
                    memType = BootInfoMemoryType.BadMemory;
                    break;

                default:
                    memType = BootInfoMemoryType.Unknown;
                    break;
                }
                BootInfo->MemoryMapArray[i].Type             = memType;
                BootInfo->MemoryMapArray[i].AddressSpaceKind = addressSpaceKind;
                BootInfo->MemoryMapArray[i].PreMap           = preMap;
            }

            // It's possible, that the BIOS-Area (@640K) is not correctly setup by Multiboot. So add it here manually to be sure
            var idx = mbMapCount + 0;

            BootInfo->MemoryMapArray[idx].Start            = 640 * 1024;
            BootInfo->MemoryMapArray[idx].Size             = (1024 - 640) * 1024;
            BootInfo->MemoryMapArray[idx].Type             = BootInfoMemoryType.CustomReserved;
            BootInfo->MemoryMapArray[idx].AddressSpaceKind = AddressSpaceKind.Both; //TODO: Physical
            BootInfo->MemoryMapArray[idx].PreMap           = true;

            idx++;
            BootInfo->MemoryMapArray[idx].Start            = Address.KernelElfSectionPhys;
            BootInfo->MemoryMapArray[idx].Size             = KMath.AlignValueCeil(LoaderStart.OriginalKernelElf.TotalFileSize, 0x1000);
            BootInfo->MemoryMapArray[idx].Type             = BootInfoMemoryType.KernelElf;
            BootInfo->MemoryMapArray[idx].AddressSpaceKind = AddressSpaceKind.Physical;
            BootInfo->MemoryMapArray[idx].PreMap           = true;

            idx++;
            BootInfo->MemoryMapArray[idx].Start            = Address.KernelBootInfo;
            BootInfo->MemoryMapArray[idx].Size             = 0x1000;
            BootInfo->MemoryMapArray[idx].Type             = BootInfoMemoryType.BootInfoHeader;
            BootInfo->MemoryMapArray[idx].AddressSpaceKind = AddressSpaceKind.Both;
            BootInfo->MemoryMapArray[idx].PreMap           = true;

            idx++;
            BootInfo->MemoryMapArray[idx].Start            = BootInfo->HeapStart;
            BootInfo->MemoryMapArray[idx].Size             = 0x1000; //TODO: Recalculate after Setup all Infos
            BootInfo->MemoryMapArray[idx].Type             = BootInfoMemoryType.BootInfoHeap;
            BootInfo->MemoryMapArray[idx].AddressSpaceKind = AddressSpaceKind.Both;
            BootInfo->MemoryMapArray[idx].PreMap           = true;

            idx++;
            uint stackSize = 0x100000; // 1MB

            BootInfo->MemoryMapArray[idx].Start            = Address.InitialStack - stackSize;
            BootInfo->MemoryMapArray[idx].Size             = stackSize;
            BootInfo->MemoryMapArray[idx].Type             = BootInfoMemoryType.InitialStack;
            BootInfo->MemoryMapArray[idx].AddressSpaceKind = AddressSpaceKind.Both;
            BootInfo->MemoryMapArray[idx].PreMap           = true;

            idx++;
            BootInfo->MemoryMapArray[idx].Start            = Address.GCInitialMemory;
            BootInfo->MemoryMapArray[idx].Size             = Address.GCInitialMemorySize;
            BootInfo->MemoryMapArray[idx].Type             = BootInfoMemoryType.InitialGCMemory;
            BootInfo->MemoryMapArray[idx].AddressSpaceKind = AddressSpaceKind.Both;
            BootInfo->MemoryMapArray[idx].PreMap           = true;

            idx++;
            BootInfo->MemoryMapArray[idx].Start            = LoaderStart.OriginalKernelElf.GetSectionHeader(".bss")->Addr;
            BootInfo->MemoryMapArray[idx].Size             = LoaderStart.OriginalKernelElf.GetSectionHeader(".bss")->Size;
            BootInfo->MemoryMapArray[idx].Type             = BootInfoMemoryType.KernelBssSegment;
            BootInfo->MemoryMapArray[idx].AddressSpaceKind = AddressSpaceKind.Virtual;
            BootInfo->MemoryMapArray[idx].PreMap           = false;

            idx++;
            BootInfo->MemoryMapArray[idx].Start            = LoaderStart.OriginalKernelElf.GetSectionHeader(".text")->Addr;
            BootInfo->MemoryMapArray[idx].Size             = LoaderStart.OriginalKernelElf.GetSectionHeader(".text")->Size;
            BootInfo->MemoryMapArray[idx].Type             = BootInfoMemoryType.KernelTextSegment;
            BootInfo->MemoryMapArray[idx].AddressSpaceKind = AddressSpaceKind.Virtual;
            BootInfo->MemoryMapArray[idx].PreMap           = false;

            idx++;
            BootInfo->MemoryMapArray[idx].Start            = LoaderStart.OriginalKernelElf.GetSectionHeader(".rodata")->Addr;
            BootInfo->MemoryMapArray[idx].Size             = LoaderStart.OriginalKernelElf.GetSectionHeader(".rodata")->Size;
            BootInfo->MemoryMapArray[idx].Type             = BootInfoMemoryType.KernelROdataSegment;
            BootInfo->MemoryMapArray[idx].AddressSpaceKind = AddressSpaceKind.Virtual;
            BootInfo->MemoryMapArray[idx].PreMap           = false;

            idx++;
            BootInfo->MemoryMapArray[idx].Start            = LoaderStart.OriginalKernelElf.GetSectionHeader(".data")->Addr;
            BootInfo->MemoryMapArray[idx].Size             = LoaderStart.OriginalKernelElf.GetSectionHeader(".data")->Size;
            BootInfo->MemoryMapArray[idx].Type             = BootInfoMemoryType.KernelDataSegment;
            BootInfo->MemoryMapArray[idx].AddressSpaceKind = AddressSpaceKind.Virtual;
            BootInfo->MemoryMapArray[idx].PreMap           = false;

            // Avoiding the use of the first megabyte of RAM
            idx++;
            BootInfo->MemoryMapArray[idx].Start            = 0x0;
            BootInfo->MemoryMapArray[idx].Size             = Address.ReserveMemory;
            BootInfo->MemoryMapArray[idx].Type             = BootInfoMemoryType.KernelReserved;
            BootInfo->MemoryMapArray[idx].AddressSpaceKind = AddressSpaceKind.Both;
            BootInfo->MemoryMapArray[idx].PreMap           = false;

            BootInfo->MemoryMapLength = idx + 1;
        }