简体   繁体   English

是否可以使用C编程访问物理内存地址?

[英]Is it possible to access a physical memory address using C programming?

For example, when we write: 例如,当我们写:
int a;
and doing &a gives us some address in hex form 0x12345678 which we call as virtual address. 并且做&a给我们一些十六进制形式的地址0x12345678 ,我们称之为虚拟地址。
Now when we try to do 现在,当我们尝试做
int *temp = 0xfe000000;
does this address acts like a virtual address or a physical address? 此地址的作用类似于虚拟地址还是物理地址? As far as I know virtual addresses are given by OS which are further converted to physical addresses by MMU. 据我所知,OS提供的虚拟地址由MMU进一步转换为物理地址。

As far as I know paging mechanism takes place in between which maps the virtual address to available physical address. 据我所知,在两者之间发生分页机制,将虚拟地址映射到可用的物理地址。 What happens when we give an address to a pointer? 当我们给指针一个地址时会发生什么? Does that address acts like virtual address which is then handled by MMU to point to a particular physical address or it acts like a physical address in itself? 该地址是否像虚拟地址一样,然后由MMU处理以指向特定的物理地址,或者它本身就像物理地址一样?

int temp;
printf("%x \n", &temp);
int *temp2 = 0xfe000000; //This is just an example address. It could be any address


Does this address (0xfe000000) acts like a real physical address or a virtual address? 此地址(0xfe000000)的作用是真实的物理地址还是虚拟地址?

In case you are using an operating system, then it is not possible because the OS is using virtual address and whatever you use will be mapped to the physical address via virtual address. 如果您使用的是操作系统,则无法使用操作系统,因为操作系统使用的是虚拟地址,您使用的任何内容都将通过虚拟地址映射到物理地址。

But if you are programming a microcontroller then there is no virtual address and you can access the physical memory location using pointers in C. 但是,如果您正在编程微控制器,那么没有虚拟地址,您可以使用C中的指针访问物理内存位置。

The address we get normally, say for example,a pointer in C programming in a PC is virtual address . 我们通常得到的地址,例如,PC中C编程中的指针是虚拟地址 The virtual to physical mapping and the other way round, is handled by the operating system internally. 虚拟到物理映射,反之亦然,由内部操作系统处理。 Usually, the user(root or otherwise) will have no say in the address mapping. 通常,用户(root或其他)在地址映射中没有发言权。

On the other hand, simple micro controllers usually have no TLB mapping, so the address you access is the actual physical address of the device, as said in other answer. 另一方面,简单的微控制器通常没有TLB映射,因此您访问的地址是设备的实际物理地址,如其他答案中所述。

Also, if you are planning to use a micro processor from ground-up, you can always have your own physical to virtual address mapping, where you can even set properties to the address blocks. 此外,如果您计划从头开始使用微处理器,您始终可以拥有自己的物理到虚拟地址映射,您甚至可以在地址块中设置属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM