Read-only pages in ring 0

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
User avatar
Walling
Member
Member
Posts: 158
Joined: Mon Dec 04, 2006 6:06 am
Location: Berlin, Germany

Read-only pages in ring 0

Post 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.
cyr1x
Member
Member
Posts: 207
Joined: Tue Aug 21, 2007 1:41 am
Location: Germany

Re: Read-only pages in ring 0

Post 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
Last edited by cyr1x on Sun Jun 07, 2009 2:25 am, edited 1 time in total.
Martijn
Posts: 22
Joined: Tue Feb 26, 2008 3:43 am
Location: The Netherlands

Re: Read-only pages in ring 0

Post 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."
User avatar
Walling
Member
Member
Posts: 158
Joined: Mon Dec 04, 2006 6:06 am
Location: Berlin, Germany

Re: Read-only pages in ring 0

Post 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.
User avatar
kop99
Member
Member
Posts: 120
Joined: Fri May 15, 2009 2:58 am

Re: Read-only pages in ring 0

Post 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...
Post Reply