Page 1 of 1

Accessing the physical memory and bypassing virtual memory

Posted: Thu Aug 08, 2019 8:37 am
by nightcrawler
Let me start with quick background;
I was always interested in in writing some C code to pint to a memory location like "0x0" and loop to infinity and set every byte of memory to 0, and hence crash the OS while it's running, just for fun. This is of course not possible due to virtual memory and user address space and so on.

Fast forwarding until now, I have played around with written my own "hello world" kernel as well as linux kernel modules just so I can touch the physical memory but i'm still not sure if I have succeeded.

Now here are my questions:

1- Going through this beginner tutorial https://wiki.osdev.org/Bare_Bones, it says that "The bootloader has loaded us into 32-bit protected mode on a x86 machine. Interrupts are disabled. Paging is disabled." Does the "Paging is disabled" part mean i'm using physical memory at this point? I have followed everything int he above page and managed to see "Hello world" on my screen, but I'm not sure if i'm using virtual memory at that point or physical memory.
If GRUB loads me into protected mode how can virtual memory be turned off? I thought thats the whole point of having a protected mode in x86.

2- Is it possible to touch physical memory when writing Kernel Modules?

3- I have also read that "The Kernel Virtual Address Space (KVA) is the virtual memory area in which all Linux kernel threads reside", why does an OS need to use virtual addressing for itself? Why doesn't it use the physical memory directly?

Sorry if questions seem to be all over the place, they are all sort of related to memory management.

Re: Accessing the physical memory and bypassing virtual memo

Posted: Thu Aug 08, 2019 8:52 am
by LtG
If paging is disabled then yes, you are using physical memory.

The paging is either on or it's off, you could switch it on/off for all kernel accesses, but that's just slow and gets you no benefits. The reason to use virtual memory is so that when a program (or kernel) asks for 1MiB of memory, you don't have to find a 1 MiB contiguous block (or worse, start defragging RAM), but instead just 256x4KiB pages, and map those. I'm not aware of any good reason not to use virtual memory over physical (for GP OS's, embedded in some cases is different).

I don't know enough about kernel modules, but I'm pretty sure you can do your original description in Linux, that is, overwrite the entire RAM with zeroes. Of course with multicore you probably won't reach the end of RAM. Short explanation is that some core will be executing some code that you've set to zero and eventually that will almost certainly lead to a triple fault --> system reset.

Re: Accessing the physical memory and bypassing virtual memo

Posted: Thu Aug 08, 2019 8:53 am
by iansjack
nightcrawler wrote:Does the "Paging is disabled" part mean i'm using physical memory at this point?
Yes. Paging and Protected Mode are not the same thing.
Is it possible to touch physical memory when writing Kernel Modules?
Yes. Many device drivers will need to access physical memory as devices that use memory buffers tend to need physical addresses. Also, any code that manipulates Page Tables will need to access physical addresses.
Why doesn't it use the physical memory directly?
There are a number of reasons. When paging is enabled it is more tortuous to access physical memory than just using a virtual address. Also, paging is about protecting areas of memory, which is bypassed when physical addresses are used. (There are more reasons than that, but it's probably beyond a simple forum post.)

The main thing to remember is that paging is not necessary in Protected Mode, although almost any OS will use it. And if you are working in 64-bit mode then paging is compulsory.

Re: Accessing the physical memory and bypassing virtual memo

Posted: Thu Aug 08, 2019 9:03 am
by nightcrawler
iansjack wrote:
nightcrawler wrote:Yes. Paging and Protected Mode are not the same
Thank you, so then what's the point of being loaded into protected mode here? will real mode be equivalent in this case?

Re: Accessing the physical memory and bypassing virtual memo

Posted: Thu Aug 08, 2019 9:56 am
by iansjack
Protected Mode allows you to access more memory, and provides various protection mechanisms on segments. It also let's you enable paging, which provides better mechanisms to protect memory.

Re: Accessing the physical memory and bypassing virtual memo

Posted: Thu Aug 08, 2019 10:10 am
by GMorgan
In 64 bit mode at least you must use paging. If you are looking to just access the physical address use an offset identity mapped page table as described at the link below. It will enable you to access any page while in kernel mode.

viewtopic.php?f=1&t=33813

Re: Accessing the physical memory and bypassing virtual memo

Posted: Thu Aug 08, 2019 10:26 am
by LtG
In addition protected mode gives protection for I/O and the "rings", it boils down to being able to present each program a type of virtual machine, so the programs can co-exist.

Btw, isn't paging itself a pretty good reason? Does there need to be more?

Re: Accessing the physical memory and bypassing virtual memo

Posted: Thu Aug 08, 2019 8:07 pm
by linguofreak
nightcrawler wrote:
iansjack wrote:
nightcrawler wrote:Yes. Paging and Protected Mode are not the same
Thank you, so then what's the point of being loaded into protected mode here? will real mode be equivalent in this case?
Well, the CPU will throw a General Protection Fault if you try to enable paging without being in protected mode or long mode. Most of the other stuff that's part of protected mode is left over from the memory management design Intel had used on the 286, which had protected mode, but not paging, and isn't present on CPUs of other architectures that were designed for paging from the ground up. On such architectures, paging and the architecture's equivalent to protected mode are often one and the same.