PDBR During Interrupts

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
pacman
Posts: 17
Joined: Sun Jul 08, 2007 10:23 am

PDBR During Interrupts

Post by pacman »

Hi

During an interrupt, dont I have to save the PDBR and then load CR3 with the PDBR of the kernel?

Thanks
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

Have you thought through:
1) what loading CR3 does
2) why you would actually want to do that
3) whether that is the best way to solve the problem in 2)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
pacman
Posts: 17
Joined: Sun Jul 08, 2007 10:23 am

Post by pacman »

From my understanding....
1) what loading CR3 does
- CR3 contains the address of the Page Directory
2) why you would actually want to do that
- If the CPU is running process A, CR3 would contain the address of process A's Page Directory.
- When an interrupt occurs, to use kernel functions, I would need to load CR3 with the Page Directory of the kernel.
3) whether that is the best way to solve the problem in 2)
- One of the reasons why I'm posting here

Thanks
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

Next question:
Why isn't the kernel mapped in every process' page tables?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
pacman
Posts: 17
Joined: Sun Jul 08, 2007 10:23 am

Post by pacman »

Combuster wrote:Why isn't the kernel mapped in every process' page tables?
I haven't thought of that, but it makes sense...

Ok I've just read about mapping the kernel to every process' address space. Now if the kernel is mapped to the virtual address 0x500000 (I know its a wierd address, but to make things easier to understand) of every process' address space, would'nt it refuse to work since every memory reference would now have to be: <memory reference>+0x500000?
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Post by jal »

pacman wrote:[Now if the kernel is mapped to the virtual address 0x500000 (I know its a wierd address, but to make things easier to understand) of every process' address space, would'nt it refuse to work since every memory reference would now have to be: <memory reference>+0x500000?
Of course not. You can tell your linker that your kernel start address is 0x500000 (still don't know why that's easier, but ok), and it will link it for you using that address. Alternatively, you can use e.g. a relocatable ELF for your kernel image and relocate it yourself.

JAL
pacman
Posts: 17
Joined: Sun Jul 08, 2007 10:23 am

Post by pacman »

You can tell your linker that your kernel start address is 0x500000
What if I wanted my kernel to be located (in virtual memory) from 2GB? I would tell my linkerscript that the start address is 2GB. Then, when GRUB tries to load my kernel, wouldn't it refuse to work since virtual memory is not enabled at that time (hence, the 2GB address doesnt actually exist)?

Also, if I didnt map my kernel to each address space, would interrupts still work? If they did, couldnt I simply change CR3 at each interrupt?
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Post by jal »

pacman wrote:What if I wanted my kernel to be located (in virtual memory) from 2GB?
That is so common, there's a Wiki page about it. It's called "higher half kernel". Go check it out, and come back if you have more questions.


JAL
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Post by bewing »

Combuster wrote:Next question:
Why isn't the kernel mapped in every process' page tables?
Because that seems like a serious weakening of kernel security? I am looking to separate the kernel from userspace -- not bring them closer.
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Post by Korona »

There are some flags in every page table entry to restrict access to pages to ring0/1/2 tasks. That way the kernel can be mapped into all page directories without any security problems.
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Post by bewing »

Which is precisely the problem. I don't want ring 1 (untrusted) drivers having kernel access. Which means I can't map the kernel into the memory of those tasks using that mechanism. Which means I might as well not map it into the memory of any task -- since no task except the scheduler (in my OS) is allowed to call the kernel directly anyway.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

I wouldn't want to use ring 1 because of the same reasons.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Post by jal »

bewing wrote:Which is precisely the problem. I don't want ring 1 (untrusted) drivers having kernel access.
I think the whole ring 0/1/2/3 is a result of the 286 segmentation model (or did it start with the 386?), and is rather obsolete by now. Most architectures have only two levels, so you put your drivers either in ring 0 (kernel space) or ring 3 (user space). Ring 1/2 can be ignored, also because it's not very portable.


JAL
User avatar
Zenith
Member
Member
Posts: 224
Joined: Tue Apr 10, 2007 4:42 pm

Post by Zenith »

Kind of hijacking this thread, but:

What's the advantage of mapping the kernel into every user process? I mean, it adds the additional overhead of having to:
- Actually mapping the kernel virtual memory to every user page directory
- (Depending on kernel type) Having to deal with the hassle of higher-half addressing
- Weakened security, entwining the kernel/userspace, and loss of address space for an application

All the wiki article (on higher half kernels) says is that it's "traditional" and "generally good" to map the kernel to every user process, but can anyone give me reasons why someone would do this?
"Sufficiently advanced stupidity is indistinguishable from malice."
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Post by bewing »

It is one way of handling this problem:
- When an interrupt occurs, to use kernel functions, I would need to load CR3 with the Page Directory of the kernel.
If every process has the kernel mapped, and an interrupt happens, then Ring 0 handlers have immediate access to kernelspace.

Also, if your API allows (for speed reasons) userspace apps to call kernelspace functions directly (at a reduced privelege level?) -- then you need the kernelspace mappng to call the functions.
Post Reply