Hi
i am implementing some memory management stuff. in that, for the user task, i am planning to implement a function which will lock the page/memory area requested by the user task and won't allow any write access [most probably used in the malloc data structure for tyhe appliacation level memory manager]. for that, i am simply making the Writable bit of the given page entry in the page tables as zero. I am also invalidating the TLB. then also, if thru the user task i try to access the page for writing, i expect a PGF, but it doesnt! Instead, if i make the present bit zero, it gives me a PGF, that means the code is correct.
Do i also have to deal with RW bit in the corresponding page directory entry for the page table?
thanx in advance.
[PSB]
Page Writable bit not working
Re:Page Writable bit not working
If the page directory entry has the writable bit turned off then all subpages will automatically be readonly as well.
Are you testing from kernel space or user space? In Ring 0, the CPU ignores the writable bit anyway.
Are you testing from kernel space or user space? In Ring 0, the CPU ignores the writable bit anyway.
Re:Page Writable bit not working
then how to provide readonly memory areas in the kernel itself [like the non wrtitable code and the .rodata sections] so that buggy section of kernel code can be trapped in early stage?
Re:Page Writable bit not working
Hi Prashant,
In order for the Read/Write flags to be effective you need to enable the Write Protect bit in CR0. When this bit is 0 all pages are read/write wether their PTE read/write bits are set or not.
See section 4.11 in the Intel System Prog manual for further info. I had the same problem when I was testing my paging code until I found that one out
Shecks
In order for the Read/Write flags to be effective you need to enable the Write Protect bit in CR0. When this bit is 0 all pages are read/write wether their PTE read/write bits are set or not.
See section 4.11 in the Intel System Prog manual for further info. I had the same problem when I was testing my paging code until I found that one out
Shecks
Re:Page Writable bit not working
It did work finally.
and, for that write protect bit in CR0, AFAIK, it is used for (dis)allowing kernel to write into user pages which are read only in the user's memory area, and not of the kernel.
anyway, thanx
and, for that write protect bit in CR0, AFAIK, it is used for (dis)allowing kernel to write into user pages which are read only in the user's memory area, and not of the kernel.
anyway, thanx
Re:Page Writable bit not working
But refering to your second question re: kernel mode write protection for kernel code :prashant wrote: and, for that write protect bit in CR0, AFAIK, it is used for (dis)allowing kernel to write into user pages which are read only in the user's memory area, and not of the kernel.
If the WP bit in CR0 is clear then your kernel code will be able to overwrite any pages even if you have marked the pages as read-only.prashant wrote: then how to provide readonly memory areas in the kernel itself [like the non wrtitable code and the .rodata sections] so that buggy section of kernel code can be trapped in early stage?
Shecks
Re:Page Writable bit not working
It would be nice if there was a way to make the Write Protect effective on kernel (supervisor, ring 0-2) pages but not user pages.
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Page Writable bit not working
you mean like preventing the OS code to overwrite itself but give it the ability to overwrite pages that are marked 'read-only' at user level ?
There are ways to do that:
- don't enable "kernel write protect" at paging, and enforce read-only things by segmentation.
- create a secondary mapping of the write-protected user pages so that the kernel only can see them (e.g. a kernel-level page directory entry) but has the right to write to them.
The main reason for making the kernel sensitive to user-level "readonly" bit is that you may want to use it for "copy-on-write" purpose and that if the kernel should write to a copy-on-write buffer, it should do the copy too.
There are ways to do that:
- don't enable "kernel write protect" at paging, and enforce read-only things by segmentation.
- create a secondary mapping of the write-protected user pages so that the kernel only can see them (e.g. a kernel-level page directory entry) but has the right to write to them.
The main reason for making the kernel sensitive to user-level "readonly" bit is that you may want to use it for "copy-on-write" purpose and that if the kernel should write to a copy-on-write buffer, it should do the copy too.