I am trying to write directly to a physical memory location, so I am using an assembly function to first disable paging, write the value, and then re-enable paging, but for some reason a page fault is still triggered when trying to write the value.
As I understand it, in x86-32bit, paging is set on and off by flipping bit 32 in cr0, so here is my assembly function:
mov 4(%esp), %ecx //address
mov 8(%esp), %edx //value
mov %cr0, %eax
and $0x7fffffff, %eax
mov %eax, %cr0
mov %edx, (%ecx) //this line still triggers a page fault somehow
or $0x80000000, %eax
mov %eax, %cr0
ret
Is this the correct way to achieve what I am wanting to do? If so, why is a page fault still being triggered with the bit in cr0 flipped?
The change in the CR0 register will become active when a jump instruction (far jump only?) is done.
Disabling the paging however is not a good idea: You have to guarantee that the code is located in 1:1 mapped memory and that interrupts are disabled.
If you use the stack you must also ensure that the stack is mapped 1:1.
It is much easier to modify the page tables in a way that the physical address in ecx is mapped to a virtual address and then to write to the virtual address.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With