Hi,
lolxdfly wrote:
I struggle at finishing my paging code. I am not sure if I understood the complete concept of paging.
I have already the basic functionalities like allocating new page directories. Furthermore I can register kernel or user tasks and each task will allocate a new page directory. The directories will be switched if tasks are switched.
My problem is to fill the new allocated page directory of user tasks. Kernel tasks just get the kernel page directory, but I am not sure how to fill the user page directory right.
Usually the kernel creates a new task with nothing in user-space; then switches to that task (still with nothing in user-space) and runs some sort of executable loader or a "mini loader" (that's built into the kernel). The executable loader looks at the executable file and figures out which pieces of the executable file get mapped where (from the executable's header). Once the executable file is mapped into user-space, kernel can "return" to user-space at the executable's entry point.
lolxdfly wrote:
I know that it needs the kernel to be mapped for Interrupts and so on, but how do I prevent accessing this memory. Or is it normal, that user tasks can modify kernel memory?
Usually there's 2 (or more) privilege levels, where the CPU is running in one (e.g. "CPL=3" or user) and all of the kernel's pages are marked as "supervisor only" so that if code running with user privileges tries to access those pages it causes a page fault (because supervisor access is needed), and kernel code (which has supervisor access - e.g. CPL=0) can access those pages.
lolxdfly wrote:
And how can I map the user task code itself? E.g. my elf modules are loaded to 0x300000. How can I divide their memory so that they dont override each other?
Normally every process is given its own private virtual address space (with "kernel space" mapped into it). For 80x86 this is mostly done by loading CR3 with a different value for each different process during task switches (unless the tasks are different threads in the same process). One process can use virtual address 0x12345678 in its own private virtual address space for something and this has nothing to do with address 0x12345678 in any other virtual address space (in the same way that I could write "Hello" on the third page of a book, and this wouldn't effect the third page of any other book).
Cheers,
Brendan