Questions about Memory Allocation
- TheChuckster
Questions about Memory Allocation
Post by TheChuckster »
Code: Select all
void* free; //<- initialize this with a block of free size obtained by any
void* end_of_free_mem; // detection technique
void* bottom_up_alloc(int size) {
void *ret=NULL;
if (free+size<end_of_free_mem) {
ret=free;
free+=size;
}
return ret;
}
How do I know which address is the one where the first MB is?
Basicly I need to know how to translate between bytes and addresses.
- jamesb.au
Re:Questions about Memory Allocation
Post by jamesb.au »
Are you using paging in your kernel? If so, you have all memory mapped into pages and you need a different algorithm to Pype.Clicker's, and it will need to allocate PAGE_SIZE chunks, usually 4KB in Intel arch systems. You can set the page size you want that is allowed by the Intel architecture. A process may like to ask for X bytes, and you can determine the number of free pages of memory the process will be allocated.
If no paging, e.g. segments, um, you can use a simple algorithm like Pype.Clicker's, that should work fine. You keep a list of free blocks of memory, allocate from there, stick the list of allocated blocks in an allocated memory list, and when the process exits, add the allocated blocks of that exited process back to the list of free blocks.
Let's say you aren't using paging, and simply setup a linked list with each node representing a block of say 4KB. Those in the list are allocated, and the list can be sorted, so the last block in the list plus one is your first block of free memory, do some sanity checks with the last 4KB block based on how much memory is in the system, add it to the list of allocated blocks, and return the pointer to the requesting process, and ... done. Well, that's a rather crude way of doing it. If you aren't using paging then those pointers are physical addresses. (XXX?)
Experienced kernel devers, can you correct any mistakes above?
These are some thoughts off the top of my head to hopefully get you thinking about possible ways to do this... have fun :D
- Pype.Clicker
- Member
Member - Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Questions about Memory Allocation
Post by Pype.Clicker »
Just have your memory-manager reference those symbols and the linker will fill the values nicely. The "signature" approach may lead to strange results as you cannot exclude that the signature will appear within the binary code, for instance (until you check your binary file and make sure the signature is never in it ...)
- bkilgore
Re:Questions about Memory Allocation
Post by bkilgore »
There's a much better way than this. When linking your kernel together, use a linker script. The linker script allows you to define veriables that are exported into your kernel, so if you want to know where the end of your kernel is, you can export a symbol after the last section. For example, a simple loader script might be:James Buchanan wrote: When you compile your kernel you could define a couple of bytes, or a couple of words even, like a boot signature scheme determines a valid bootsector, and stick it at the very end of your compiled kernel image. Start at address 0x00000000 and scan up, looking for that signature, counting from the start of your kernel.
Code: Select all
SECTIONS
{
__start = .;
.text 0x100000
{
/* kernel code */
*(.text)
}
.data
{
/* kernel data */
*(.data)
}
.bss
{
/* kernel BSS */
*(.bss)
*(COMMON)
}
__end = .;
}
You can safely assume that anything past the end of your kernel is available when your kernel starts. So if I understand correctly what you were looking for, the chunk of memory from __end to the end of your memory is your initial free block.
- bkilgore
Re:Questions about Memory Allocation
Post by bkilgore »
- TheChuckster
Re:Questions about Memory Allocation
Post by TheChuckster »
- mystran
Re:Questions about Memory Allocation
Post by mystran »
There might be holes, especially when there is a lot of memory in the system.
In any case, the easiest way is to ask BIOS to tell you where the free RAM is, which is easiest to do by booting with GRUB, since it can get it for you. It handles problems like finding out which BIOS call to use and such.
If you don't want to use GRUB, you might still want to get the stuff before going to pmode though. GRUB source might be a good place to see how it's done. One other place to look would be Linux source.
Old versions probed the memory, but modern kernels just get the BIOS map.
I actually decided to go straight to "virtual memory", since it seemed easier for me, since on top of that I have a malloc-library that depends on a single system call morecore(). The morecore basicly works the way of your bottom_up_alloc, only in continuous kernel heap that it can try to extend by calling vm_enlarge_kheap() which maps more pages there, getting those pages from a page allocator that basicly works just like morecore, only it deals with the
BIOS map, and returns 4k pages (and keeps a free-list)
- TheChuckster
Re:Questions about Memory Allocation
Post by TheChuckster »
- bkilgore
Re:Questions about Memory Allocation
Post by bkilgore »
When I was a lot younger I always wanted to write my own operating system. I wrote qbasic shells on top of dos and a lot of other stuff, but I never felt I understood enough about hardware and theory to write an actual operating system.
Instead I decided to focus on learning as much as i could about good programming technique and about how other operating systems work, pouring over code until i understood what it was doing and, i think more importantly, why it was doing it.
So don't give up. If you really want it you'll learn eventually. Just don't get frustrated if it doesn't come easily...
- Pype.Clicker
- Member
Member - Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Questions about Memory Allocation
Post by Pype.Clicker »
hum, except for the 'ROM & VRAM' hole between 0xA0000 and 0x100000, are there any other 'holes' ? from what i've learned from PCI bus, the PCI devices get mapped in memory where you tell the to map to ... and ususally, this is from 0xffff0000 ... so ?mystran wrote: Actually, it's not safe to assume that the memory is continuous.
There might be holes, especially when there is a lot of memory in the system.
- mystran
Re:Questions about Memory Allocation
Post by mystran »
This can be skipped of course, but then there might be some cases where BIOS maps a PCI/VLB card (old VGA cards mostly) to below the top of memory if you have a lot of RAM (some older cards are hardcoded to some address).
Cirrus Logic cards are one example, some low places they might want to be are 32MB, 64MB (VLB), then some ET4000 PCI cards want to map at little below 1GB I think, etc.
I also remember some older systems where touching some areas in memory could cause the system to reboot. Friend used to have one. Indeed, IIRC BIOS did report usable RAM correctly, but probing it would never work.
Ok, now, I'm sorry if I caused confusion, but just wanted to mention
that this might actually happen, especially when legacy hardware is involved.
- Pype.Clicker
- Member
Member - Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Questions about Memory Allocation
Post by Pype.Clicker »
- mystran
Re:Questions about Memory Allocation
Post by mystran »
falling back to the older BIOS calls and/or probing when it's not available..
Now to be exact, the memory map GRUB returns actually is actually constructed of several pieces of information, including things like EISA memory map (int 0x15, ax=0xe801). This assuming I have understood correctly,
given the nature of GRUB source, that is.
Anyhow, my main point was merely to say that there are strange configurations, and GRUB has already been tested to work with most of them (if not all), which makes kernel
development a little easier, at least in the beginning.
If you want to do it yourself, I'd suggest looking at Linux source, arch/i386/kernel/setup.c and navigating from there. It's far easier to read than GRUB source..