Page 1 of 1

Read-only pages in ring 0

Posted: Sat Jun 06, 2009 10:35 am
by Walling
Hi,

I've lurked these forums for too long, so here is a question, a thing that puzzles me.

I've setup basic identity-mapped paging. I use a page directory, where the first entry points to a page table. The page table identity maps the first 4 MB. I think it works (sort of), because if I mark a page Not Present and try to access it, it triple-faults. Also Bochs debugger tells me the right mapping by "info tab".

I want to map some pages read-only (the kernel .text/.rodata sections and the BIOS area except 0xB8000-0xB8FFF for text screen output). However when I do so they are still writable. I tried combinations of the Read/Write bit in the page directory and the page table, but I see no effect. I tested in QEMU and Bochs. They show the same behavior. Is it because I'm in ring 0 and this causes every page to be read-write without regard to the flag? Or is it because QEMU and Bochs doesn't pay attention to the flag? I haven't tested it on a real computer. Maybe I have to enable some bit in CR4?

I'll continue searching the web, but if you can provide some hints it would be nice.

Re: Read-only pages in ring 0

Posted: Sat Jun 06, 2009 11:02 am
by cyr1x
Yep, supervisor can write to read-only pages. You have to set the Write-Protect bit in CR4.

EDIT: Of course it was CR0 #-o

Re: Read-only pages in ring 0

Posted: Sat Jun 06, 2009 11:06 am
by Martijn
Walling wrote: Is it because I'm in ring 0 and this causes every page to be read-write without regard to the flag?
Correct. I suggest you download a copy of the Intel manuals which covers all you need to know about paging. ;)

Intel Manual 3A - 4.11:
"When the processor is in supervisor mode and the WP flag in register CR0 is clear (its state following reset initialization), all pages are both readable and writable (write-protection is ignored). When the processor is in user mode, it can write only to user-mode pages that are read/write accessible. User-mode pages which are read/write or read-only are readable; supervisor-mode pages are neither readable nor writable from user mode. A page-fault exception is generated on any attempt to violate the protection rules."

Re: Read-only pages in ring 0

Posted: Sat Jun 06, 2009 11:21 am
by Walling
I requested the Intel Manuels as books, but they were out of stock and said I should re-request them next year. Of course I should've looked in the PDFs about paging. When I set the WP bit (bit 16) in CR0 all sort of funny things happen. I think I have to debug now.

Edit: It now works as expected. I'll put a short note about it on the wiki.

Re: Read-only pages in ring 0

Posted: Sun Jun 07, 2009 12:08 am
by kop99
It now works as expected. I'll put a short note about it on the wiki.
Good idea...
I know that throgh this post.
And it'll help any others...