Allocating a memory area for vmalloc

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
mrjbom
Member
Member
Posts: 315
Joined: Sun Jul 21, 2019 7:34 am

Allocating a memory area for vmalloc

Post by mrjbom »

I plan to implement vmalloc for the kernel memory, but I can't decide which part of the virtual memory to define for this.
I want to do like Linux, allocate a 128 megabyte virtual memory area from 0xF8000000(3 GB + 896 MB) to 0xFFFFFFFF(3 GB + 1024 MB).
I have a x86 protected mode higher half kernel, in the kernel space VA = PA + 0xC0000000,
which means that if there is memory in the physical memory area from 0x38000000(896 MB) to 0x3FFFFFFF(1024 MB) that I need to put into the kernel (for example ACPI or something like that),
then I will not be able to map it so easily as with all other physical memory related to the kernel.
I will give all the free physical memory from this interval for user applications and there will be no such problems with it.

According to the wiki(https://wiki.osdev.org/Memory_Map_(x86)), the first gigabyte of physical memory will not contain anything that drivers will need.
Can I just hope that the last 128 megabytes of the first gigabyte of physical memory will not contain data that I will have to map for drivers (for example, ACPI and so on)?
rdos
Member
Member
Posts: 3296
Joined: Wed Oct 01, 2008 1:55 pm

Re: Allocating a memory area for vmalloc

Post by rdos »

Memory that drivers need should be marked as unavailable by UEFI or BIOS memory map, so I don't see the issue. Also, I don't see how virtual memory management relates to physical memory. You should add available physical memory to a pool. You then have a physical memory allocator that you use to get memory for a virtual memory object. For things like PCI BARs, you read out the BAR physical address and then map it to a virtual memory address, and mark it as "don't free" if something happens to try to free the virtual memory address.

A virtual memory manager that allocates 4k pages could be implemented by scanning page tables, and then either mapping them to physical addresses (using the physical memory manager), or marking them as allocated, and letting the page fault handler allocate physical memory on demand.
Octocontrabass
Member
Member
Posts: 5560
Joined: Mon Mar 25, 2013 7:01 pm

Re: Allocating a memory area for vmalloc

Post by Octocontrabass »

mrjbom wrote:Can I just hope that the last 128 megabytes of the first gigabyte of physical memory will not contain data that I will have to map for drivers (for example, ACPI and so on)?
APIC MMIO is usually located within that address range.
User avatar
mrjbom
Member
Member
Posts: 315
Joined: Sun Jul 21, 2019 7:34 am

Re: Allocating a memory area for vmalloc

Post by mrjbom »

Octocontrabass wrote:
mrjbom wrote:Can I just hope that the last 128 megabytes of the first gigabyte of physical memory will not contain data that I will have to map for drivers (for example, ACPI and so on)?
APIC MMIO is usually located within that address range.
What is the approximate size it use?
Octocontrabass
Member
Member
Posts: 5560
Joined: Mon Mar 25, 2013 7:01 pm

Re: Allocating a memory area for vmalloc

Post by Octocontrabass »

It depends on how many APICs are in your system, but they don't use much - maybe 4kB for the local APIC and 4kB for each IOAPIC.

But why do the physical addresses matter? You can use paging to assign whatever virtual addresses you like.
Post Reply