Hi,
FlashBurn wrote:The problem here is that with my system I have to keep track which areas of the address space are free and which not. So if I would implement a thing like letting the user program page fault and insert a page at that address I would also need to search for that virtual address and take it out of the free list. The problem I have with this way is, what is if at this address is something other mapped, like a library?
For "present" pages there's at least 3 "available" bits in page tables entries, etc. These three bits could hold a value from 0 to 7, and this value can tell your kernel what the page is used for. For example:
- 0 = normal RAM
- 1 = part of a memory mapped file
- 2 = part of a shared memory area
- ...
- 7 = part of a DLL or shared library
In a similar way, for "not present" pages there's at least 31 "available" bits in the page table entries, etc. In this case, maybe 2 of them could be used to describe what the page is used for (e.g. "not used at all", "memory mapped file" and "sent to swap") and the remaining bits could be some sort of ID (e.g. if it's part of a memory mapped file, bits 3 to 31 could an identifier used to find out which entry in the process' list of memory mapped files, and if it's a page sent to swap space then bits 3 to 31 could be the number of the page on the swap partition).
When a process asks the kernel to map something at a specific address, the kernel checks the area to see if there's problems (and returns an error if the caller is trying to do something completely insane). Otherwise the kernel frees or removes whatever was there (using the "available" bits to figure out how to free/remove something correctly) and maps something else there. The kernel does need to know which pages are being used for what and only really needs to know a more generic "page type". For example, the ".bss", heap space and stack/s are all just "read/write data" areas (or maybe "allocate on demand" areas) as far as the kernel cares.
When a new process is started, the process loader reads the executable file's header and parses it to figures out what should go where (and then loads shared libraries and does linking, etc). The kernel doesn't care how this works, and you could have several different process loaders and support several different executable file formats (or even start something like byte-code interpreter instead of a process loader, if the executable file is Java byte-code or something).
FlashBurn wrote:I mean I could easily implement a function like "put_something_at(virtual_address)", but I would also need to look if this address is free and so it would be the same.
In most cases the kernel should do what it's told. For example, if a process says "memory map a file at <foo>" then the kernel should wipe out whatever was already at "<foo>" and memory map the file there (rather than complaining that the area isn't free, and forcing the programmer to use an extra system call to free the area first).
FlashBurn wrote:I also don´t like the usual thing that a heap grows upwards and the stacks grow downwards. So where should I put in libraries and what is when I freed a stack somewhere in the middle of 2 other stacks and things like that. Every time I need to keep track what is used and what is not used and the user program can´t know all these things, so I keep it completely away from it.
A process *does* know these things. Someone writes source code and they probably have no idea what will end up where. A compiler and linker compiles the source code and splits it up into "sections" and creates an executable header saying where all of these "sections" go in the virtual address space. The process loader uses the executable file's header (and the headers in any shared libraries, etc) to decide where everything is before the executable file starts; and the standard library keeps track of everything that happens after the executable file is started (dynamically created stuff - heap space, etc). The person who wrote the source code may have no idea where things are, but everything else in user space does.
Cheers,
Brendan