OSDev.org

The Place to Start for Operating System Developers
It is currently Sun Nov 17, 2019 2:27 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Accessing the physical memory and bypassing virtual memory
PostPosted: Thu Aug 08, 2019 8:37 am 
Offline

Joined: Thu Aug 08, 2019 8:21 am
Posts: 2
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.


Top
 Profile  
 
 Post subject: Re: Accessing the physical memory and bypassing virtual memo
PostPosted: Thu Aug 08, 2019 8:52 am 
Offline
Member
Member

Joined: Thu Aug 13, 2015 4:57 pm
Posts: 371
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.


Top
 Profile  
 
 Post subject: Re: Accessing the physical memory and bypassing virtual memo
PostPosted: Thu Aug 08, 2019 8:53 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 3563
Location: Chichester, UK
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.
Quote:
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.
Quote:
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.


Top
 Profile  
 
 Post subject: Re: Accessing the physical memory and bypassing virtual memo
PostPosted: Thu Aug 08, 2019 9:03 am 
Offline

Joined: Thu Aug 08, 2019 8:21 am
Posts: 2
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?


Top
 Profile  
 
 Post subject: Re: Accessing the physical memory and bypassing virtual memo
PostPosted: Thu Aug 08, 2019 9:56 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 3563
Location: Chichester, UK
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.


Top
 Profile  
 
 Post subject: Re: Accessing the physical memory and bypassing virtual memo
PostPosted: Thu Aug 08, 2019 10:10 am 
Offline

Joined: Sun Jul 14, 2019 4:27 pm
Posts: 22
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


Top
 Profile  
 
 Post subject: Re: Accessing the physical memory and bypassing virtual memo
PostPosted: Thu Aug 08, 2019 10:26 am 
Offline
Member
Member

Joined: Thu Aug 13, 2015 4:57 pm
Posts: 371
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?


Top
 Profile  
 
 Post subject: Re: Accessing the physical memory and bypassing virtual memo
PostPosted: Thu Aug 08, 2019 8:07 pm 
Offline
Member
Member

Joined: Wed Mar 09, 2011 3:55 am
Posts: 332
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group