Difference between physical address and virtual address

Source: Internet
Author: User
Tags port number


(a) The concept of an address


1) Physical Address: The address of the CPU address bus, by the hardware circuit control its specific meaning. In the Physical address
A large portion is reserved for memory in memory strips, but is also often mapped to other storage (such as video memory,
BIOS, etc.). After the virtual address in the program instruction passes the segment map and the page map, the physical address is generated.
This physical address is placed on the address line of the CPU.
Physical address space, part of the physical RAM (memory) used, part of the bus, which is determined by the hardware design,
Therefore, in the x86 processor of the three-bits address line, the physical address space is 2 of 32 square, or 4GB, but the physical RAM is generally
Cannot go up to 4GB, because there is still a part to be used for the bus (there are many other devices hanging on the bus). In the PC,
In general, the low-end physical address to RAM, high-end physical address for the bus.


2) Bus address: The address line of the bus or the signal generated on the address cycle. The peripheral is using the bus address,
The CPU uses a physical address.
The relationship between the physical address and the bus address is determined by the design of the system. On the x86 platform, the physical address is the bus address,
This is because they share the same address space-a bit difficult to understand, as described in the following "stand-alone addressing". On other platforms,
Conversion/mapping may be required. For example: The CPU needs to access the physical address is 0xfa000 unit, then on the x86 platform,
Generates access to the 0xfa000 address on a PCI bus. Because the physical address and bus address are the same, the eye
It's not sure where the address is, it's in memory, or it's a storage unit on a card,
There may even be no corresponding memory on this address.


3) virtual Address: Modern operating systems generally adopt virtual memory management (Management) mechanism,
This requires the support of the MMU (Memory Management Unit). The MMU is usually part of the CPU, and if the processor does not have the MMU,
Or there is a MMU but not enabled, the memory address emitted by the CPU execution Unit will be transmitted directly to the chip pin, the memory chip
(physical memory), which is called the Physical Address (physical addresses), if the processor has the MMU enabled,
The memory address emitted by the CPU execution unit is intercepted by the MMU, and the address from the CPU to the MMU is called the virtual address,
The MMU translates this address to another address on the external address pin of the CPU chip, which is the mapping of the virtual address
into a physical address.
In Linux, 4GB (virtual) memory of the process is divided into user space, kernel space. The user space distribution is 0~3GB (
That is page_offset, which is equal to 0xC0000000 in 0x86)
, the remaining 1G is the kernel space. Programmers can only use virtual addresses. Each process in the system has its own private
Space (0~3G), which is not visible to other processes in the system.
The address of the CPU when the request is made is the virtual address of the current context, and the MMU finds the virtual address from the page table.
Physical address to complete the reference. Also read the data is a virtual address, such as Mov ax, var. compile-time Var
is a virtual address, but also through the MMU from the table to find the physical address, and then generate the bus timing, the completion of data collection.




(ii) addressing modalities
1) peripherals are performed through registers on the read-write device, and the peripheral registers are also referred to as "I/O ports", while IO ports have
Two ways of addressing: independent addressing and unified programming.


Unified addressing: The IO register (i.e. IO port) in the peripheral interface is treated just like the primary deposit, with each end share population with a
The address of the storage unit, which is used as an IO address space as part of the main memory, for example, in PDP-11, the highest
4K main memory as the IO device register address. The port occupies the memory address space, which reduces the storage capacity.
Unified addressing is also referred to as the "I/O memory" mode, where the peripheral registers are in "memory space" (many peripherals have their own memory,
buffers, the registers and memory of the peripherals are collectively referred to as "I/O space").
For example, Samsung's s3c2440 is a 32-bit ARM processor, and its 4GB address space is partitioned by peripherals, RAM, and so on:
0x8000 the address of the LED 8*8 dot Matrix
0x4800 0000 ~ 0x6000 0000 SFR (Special Register) address space
0x3800 1002 Keyboard Address
0x3000 0000 ~ 0x3400 0000 SDRAM Space
0x2000 0020 ~ 0x2000 002e IDE
0x1900 0300 CS8900


Stand-alone Addressing (separate address): The IO address is separate from the storage address, the i/0 port address does not occupy the storage space
Address range, so that there is another IO address unrelated to the storage address in the system, and the CPU must have
IO instructions (in, out, etc.) and control logic with input and output operations. Independent address, address bus came over a
Address, the device does not know whether to give the IO port, or to the memory, so the processor through the MEMR/MEMW and IOR
/iow two sets of control signals to achieve different addressing of I/O ports and memory. For example, the Intel 80x86 uses a separate addressing
CPU memory and I/O are addressed together, that is, the memory portion of the address and I/O address overlap.
Stand-alone addressing is also known as an I/O port, and the peripheral registers are located in the I/O (address) space.
For the x86 architecture, it is accessed through the In/out directive. PC architecture A total of 65,536 8bit I/O ports, composed of 64K
I/O address space, numbered from 0~0XFFFF, has 16 bits, 80x86 is addressed with a low 16-bit address line a0-a15. Two consecutive
A 8bit port can be composed of a 16bit port, with 4 consecutive ports comprising a 32bit. I/O address space and
The physical address space of the CPU is two different concepts, such as the I/O address space is 64K, a 32bit CPU physical address
Space is 4G. For example, under Intel 8086+redhat9.0, use "More/proc/ioports" to see:
0000-001f:dma1
0020-003f:pic1
0040-005f:timer
0060-006f:keyboard
0070-007f:rtc
0080-008F:DMA Page Reg
00a0-00bf:pic2
00c0-00df:dma2
00f0-00ff:fpu
0170-0177:ide1
......


However, the Intel x86 platform generally uses a technology called Memory Mapping (MMIO), which is part of the PCI specification,
IO device ports are mapped to memory space, and after mapping, the CPU accesses the IO port as if it were accessing memory. See Intel
TA 719 Document gives the x86/x64 system a typical memory address allocation table:
System resource Consumption
------------------------------------------------------------------------
BIOS 1M
Local APIC 4K
Chipset retention 2M
IO APIC 4K
PCI Device 256M
PCI Express Device 256M
PCI device (optional) 256M
Display Frame Cache 16M
Tseg 1M




For a given system, it is either a stand-alone address or a unified address, depending on
The architecture of the CPU. For example, PowerPC, m68k and so on unified address, and X86 and so on the use of independent addressing, storage
The concept of Io space. Currently, most embedded microcontrollers such as ARM, PowerPC, etc. do not provide I/O space.
Only memory space, can be directly accessed with the address, pointer. But for the Linux kernel, it may be used for different CPUs,
So it has to consider both of these ways, so it takes a new approach that will be based on I/O mapping or internal
The I/O port in which the map is stored is known as the I/O zone (I/O region), regardless of which method you use, first
Request IO Area: Request_resource (), release it at the end: Release_resource ().




2) Access to peripherals


1. The process of accessing I/O memory is: request_mem_region (), Ioremap (), Ioread8 ()/iowrite8 ()
Iounmap (), Release_mem_region ().
As mentioned earlier, IO memory is a unified address concept, for unified addressing, IO address space is a part of the physical main memory,
For programming, we can only manipulate virtual memory, so the first step to access is to put the physical address of the device
Map to virtual address, Linux2.6 with Ioremap ():
void *ioremap (unsigned long offset, unsigned long size);
We can then access these addresses directly through pointers, but we can also use a set of functions from the Linux kernel to read and write:
Ioread8 (), Iowrite16 (), Ioread8_rep (), Iowrite8_rep () ...


2. Access the I/O port
There are 2 ways to access the IO port: I/O mapping mode (i/o-mapped), Memory Mapping (memory-mapped).
The previous path does not map to memory space and directly uses functions such as INTB ()/outb () to read and write IO ports;
The Mmio is to first map the IO port to IO memory ("Memory space") and then access the IO port using a function that accesses IO memory.
void Ioport_map (unsigned long port, unsigned int count);
With this function, you can map the count of successive IO ports starting with Port to a "memory space",
These IO ports can then be accessed in the same way that the addresses they return are like accessing IO memory.






IO ports and IO memory under Linux


There are two ways the CPU addresses the physical address of the peripheral port: one is IO mapping and the other is memory mapping.
Linux will collectively refer to IO ports as IO regions (IO region) based on IO-mapped and memory-mapped methods.
Io region is still an IO resource, so it can still be described using the resource structure type.


Linux Management IO Region:


1) request_region ()


Assigns an IO port of a given interval to an IO device.


2) check_region ()


Check that the IO ports for a given interval are idle, or that some of them are already assigned to an IO device.


3) Release_region ()


Releases the IO ports that were previously assigned to a given interval for an IO device.


The IO ports can be accessed through the following auxiliary functions in Linux:


INB (), INW (), INL (), Outb (), OUTW (), Outl ()


"B" "W" "L" represent 8-bit, 16-bit, 32-bit respectively.


Access to IO memory resources


1) request_mem_region ()


The request allocates the specified IO memory resource.


2) check_mem_region ()


Checks whether the specified IO memory resource is already in use.


3) Release_mem_region ()


Frees the specified IO memory resource.


The start address parameter passed to the function is the physical address of the memory area (the above function parameter table has been omitted).


Driver developers can treat memory-mapped IO ports and peripheral memory uniformly as IO memory resources.


Ioremap () is used to map the physical address of the IO resource to the kernel virtual address space (3GB-4GB).
The parameter addr is a pointer to the kernel virtual address.


The following helper functions are available for accessing IO memory resources in Linux:


READB (), READW (), Readl (), Writeb (), Writew (), Writel ().


Linux defines the global variables Ioport_resource and Iomem_resource in the kernel/resource.c file.
The entire IO port space based on IO mapping and the IO Memory resource space based on the memory mapping method are described separately (
Includes IO port and peripheral memory).


1) about IO and memory space:
The concept of I/O space exists in the X86 processor, and I/O space is relative to the memory space, which is defined by a specific instruction in,
Out to access. The port number identifies the register address of the peripheral. The in and out instruction formats for Intel syntax are:
In accumulator, {port number │DX}
Out {port number │DX}, accumulator
Currently, most embedded microcontrollers, such as ARM, PowerPC, and so on, do not provide I/O space, and only memory space exists.
Memory spaces can be accessed directly through addresses, pointers, variables and other data used in program and program runs, and stored
is in memory space.
Even in the X86 processor, although I/O space is provided, if we design the board ourselves, the peripherals can still
Only hook up in memory space. At this point, the CPU can access the peripheral I/O port as if it were accessing a memory unit without the need to set
Specific I/O directives. Therefore, the memory space is required, and the I/O space is optional.


(2) INB and OUTB:


In Linux device drivers, it is advisable to use the functions provided by the Linux kernel to access the ports located in the I/O space, including:
· Read-Write Byte port (8-bit width)
Unsigned inb (unsigned port);
void Outb (unsigned char byte, unsigned port);
· Read-Write port (16-bit width)
Unsigned inw (unsigned port);
void outw (unsigned short word, unsigned port);
· Read-write Long word port (32-bit width)
unsigned inl (unsigned port);
void Outl (unsigned longword, unsigned port);
· Read and write a string of bytes
void InSb (unsigned port, void *addr, unsigned long count);
void outsb (unsigned port, void *addr, unsigned long count);
· INSB () reads the count byte port from port ports and writes the read result to addr pointing memory;
OUTSB () writes the count bytes of the memory pointed to by addr continuously to the port that ports begin with.
· Read and write a string of words
void Insw (unsigned port, void *addr, unsigned long count);
void Outsw (unsigned port, void *addr, unsigned long count);
· Read and write a string of long words
void inSL (unsigned port, void *addr, unsigned long count);
void Outsl (unsigned port, void *addr, unsigned long count);
The types of I/O port numbers in the above functions are highly dependent on the specific hardware platform, so
Just wrote the unsigned.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.