Will paging ruin my kernel code ?

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
matan
Posts: 13
Joined: Sat May 02, 2015 4:15 am
Libera.chat IRC: matan

Will paging ruin my kernel code ?

Post by matan »

When I enable paging , will my code be broken because pointers will not point to correct adresses in physical RAM ?
How can I switch paging and continue to run normally ?
Boris
Member
Member
Posts: 145
Joined: Sat Nov 07, 2015 3:12 pm

Re: Will paging ruin my kernel code ?

Post by Boris »

You need to "identity paging" so Physical=virtual for the whole area of your kernel (including your interrupts handlers).
I suggest you to map your pagination directory/tables also, so you can modify them.
If you need to have your kernel in the end of virtual memory address, i suggest you to read http://wiki.osdev.org/Higher_Half_bare_bones
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

Re: Will paging ruin my kernel code ?

Post by onlyonemac »

To add to what Boris said, you don't need to keep your kernel (or bootloader or wherever you're enabling paging) identity mapped. Just identity map the code where you're enabling paging. So if you enter your kernel with paging enabled, identity map your bootloader, map your kernel wherever you want it, then get the kernel's entry point to unmap the bootloader. If you enter the kernel with paging disabled and want to enable it in your kernel's initialisation routines (although I don't see any advantage to doing this), then map the kernel so that it's both identity mapped *and* mapped where you ultimately want it (you can map more than one virtual page to the same physical page), enable paging, jump to the other "virtual" copy of the kernel, then unmap the identity mapped kernel.

EDIT: Sorry I see your question is about breaking your existing code by enabling paging. Short answer: yes, it may well break your kernel code if you use any absolute addresses in your kernel. Long answer: you should re-write your kernel to allow paging now rather than later when it's longer - most assemblers and any decent linkers have an option to specify the "load address" of code, which the assembler uses to calculate the absolute addresses of labels, and any manually-calculated absolute addresses should be recalculated.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Will paging ruin my kernel code ?

Post by Brendan »

Hi,
matan wrote:When I enable paging , will my code be broken because pointers will not point to correct adresses in physical RAM ?
How can I switch paging and continue to run normally ?
In my opinion, your kernel is already ruined (because it wasn't designed to use paging); and fixing that (including fixing all the pointers, etc) will "un-ruin" the kernel. ;)


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply