1. 程式人生 > >虛擬空間地址與頁面號的轉換

虛擬空間地址與頁面號的轉換

       當程式中出現記憶體寫入訪問錯誤時,ExcetionRecord.ExceptionInfomation陣列第一個元素標識為寫入;若是讀取錯誤時,則 標識為讀取狀態。第二個元素,則是當前讀取或寫入的虛擬空間地址。在某些狀況下,我們需要知道出錯的虛擬地址所對應的虛擬記憶體頁面,然後針對此頁面做一些處理。那麼如何由當前虛擬地址計算出對應的虛擬記憶體頁面?

             delphi程式碼:
var
   pAddr, pPage: Pointer;
begin
   //此處省略若干行
   pAddr := Pointer(ExcetionInfo^.ExcetionRecord^.ExceptionInformation[1]);
   pPage := Pointer(Integer(pAddr) and (not (Page_Size - 1)))//Page_Size = 4096
end;

下面的內容,則是有關計算的原理和依據

Virtual Addresses

When a program accesses memory, it does not know or care where the physical memory backing the address is stored. It knows it is up to the operating system and hardware to work together to map locate the right physical address and thus provide access to the data it wants. Thus we term the address a program is using to access memory a virtual address

. A virtual address consists of two parts; the page and an offset into that page.

Page

Since the entire possible address space is divided up into regular sized pages, every possible address resides within a page. The page component of the virtual address acts as an index into the page table. Since the page is the smallest unit of memory allocation within the system there is a trade-off between making pages very small, and thus having very many pages for the operating-system to manage, and making pages larger but potentially wasting memory

Offset

The last bits of the virtual address are called the offset which is the location difference between the byte address you want and the start of the page. You require enough bits in the offset to be able to get to any byte in the page. For a 4K page you require (4K == (4 * 1024) == 4096 == 212 ==) 12 bits of offset. Remember that the smallest amount of memory that the operating system or hardware deals with is a page, so each of these 4096 bytes reside within a single page and are dealt with as "one".

Virtual Address Translation

Virtual address translation refers to the process of finding out which physical page maps to which virtual page.

When translating a virtual-address to a physical-address we only deal with the page number . The essence of the procedure is to take the page number of the given address and look it up in the page-table to find a pointer to a physical address, to which the offset from the virtual address is added, giving the actual location in system memory.

Since the page-tables are under the control of the operating system, if the virtual-address doesn't exist in the page-table then the operating-system knows the process is trying to access memory that has not been allocated to it and the access will not be allowed.

Figure 6.3. Virtual Address Translation Converting a virtual address to a physical address

We can follow this through for our previous example of a simple linear page-table. We calculated that a 32-bit address-space would require a table of 1048576 entries when using 4KiB pages. Thus to map a theoretical address of 0x80001234, the first step would be to remove the offset bits. In this case, with 4KiB pages, we know we have 12-bits (212 == 4096) of offset. So we would right-shift out 12-bits of the virtual address, leaving us with 0x80001. Thus (in decimal) the value in row 524289 of the linear page table would be the physical frame corresponding to this page.

You might see a problem with a linear page-table : since every page must be accounted for, whether in use or not, a physically linear page-table is completely impractical with a 64-bit address space. Consider a 64-bit address space divided into (generously large) 64 KiB pages creates 264/216 = 252 pages to be managed; assuming each page requires an 8-byte pointer to a physical location a total of 252/23 = 249 or 512 GiB of contiguous memory is required just for the page table!

相關推薦

虛擬空間地址頁面轉換

       當程式中出現記憶體寫入訪問錯誤時,ExcetionRecord.ExceptionInfomation陣列第一個元素標識為寫入;若是讀取錯誤時,則 標識為讀取狀態。第二個元素,則是當前讀取或寫入的虛擬空間地址。在某些狀況下,我們需要知道出錯的虛擬地址所對應

ip地址整數相互轉換

1、ip地址轉成整數 原理:ip地址的每段可以看成是一個0-255的整數,把每段拆分成一個二進位制形式組合起來,然後把這個二進位制數轉變成整數。 舉例:一個ip地址為10.0.3.193每段數字   

關於繫結IP地址的見解

客戶和伺服器通過呼叫函式bind時可以指定IP地址或埠號,可以都指定,也可以都 不指定,根據期望的結果,對sin_addr和sin_port,或sin6_addr和sin6_port應置為什麼值,下面

RGB顏色空間Lab顏色空間區別聯絡(附轉換程式碼)

RGB顏色空間 RGB顏色是紅色(Red)、綠色(Green)和藍色(Blue)三基色的字母縮寫。RGB色彩模式是通過三種基本顏色的不同程度的迭加來產生各種各樣的不同顏色。這個標準能夠涵蓋人類視力所能

VC程式設計實現色彩空間XYZLAB相互轉換-----改正版

 文章VC程式設計實現色彩空間RGB與XYZ相互轉換已經介紹了RGB與XYZ色彩空間的轉換演算法以及實際的VC原始碼,在上一篇文章已經提到,在PhotoShop中經常使用有RGB(紅色、綠色、藍色)、CMYK(青色、洋紅、黃 色、黑色)、HSB(色相、飽和度、亮度)和

虛擬地址物理地址的映射

指令 mod 外部 虛擬地址 就會 art 定義 nts tex 對於DSP的每一個CPU而言,當由CPU發起的對corePac內部內存(L1,L2內存,片上寄存器地址)的訪問(通過store或load指令)時,所訪問內存的地址就是物理地址;但當CPU訪問coreP

32位二進制IP地址十進制IP地址互相轉換

bin parseint 轉換 idt result 32位 temp str ann 代碼: 1 import java.util.List; 2 import java.util.ArrayList; 3 import java.util.Scanner;

php 版本 整數化 mysql存儲入庫 比較大小【版本處理類,提供版本數字互相轉換

tro mysql存儲 nload target 版本 存儲 ron 整數 php 下載地址:https://download.csdn.net/download/a724008158/10659015php 版本號 整數化 mysql存儲入庫 比較大小【版本處理類,提供版

通過xml處理sql語句時對小於大於的處理轉換

當我們需要通過xml格式處理sql語句時,經常會用到< ,<=,>,>=等符號,但是很容易引起xml格式的錯誤,這樣會導致後臺將xml字串轉換為xml文件時報錯,從而導致程式錯誤。 這樣的問題在iBatiS中或者自定義的xml處理sql的程式中經常需要我們來處理。其實很簡

android 路徑地址Uri的相互轉換 uri轉string

一個android檔案的Uri地址一般如下: content://media/external/images/media/62026 這是一張圖片的Uri,那麼我們如何根據這個Uri獲得其在檔案系統中的路徑呢? 其實很簡單,直接上程式碼: /** * Try t

xml處理sql語句時對小於大於的處理轉換

當我們需要通過xml格式處理sql語句時,經常會用到< ,<=,>,>=等符號,但是很容易引起xml格式的錯誤,這樣會導致後臺將xml字串轉換為xml文件時報錯,從而導致程式錯誤。 這樣的問題在iBatiS中或者自定義的xml處理

2.6 使用for迴圈遍歷檔案 2.7 使用while迴圈遍歷檔案 2.8 統計系統剩餘的記憶體 2.9 資料型別轉換計算(計算mac地址) 3.0 資料型別轉換(列表字典相互轉換

2.6 使用for迴圈遍歷檔案 open r:以只讀方式開啟 w: 以寫方式開啟 a: 以追加模式開啟 r+: 以讀寫模式開啟 w+: 以讀寫模式開啟(參見w) a+: 以讀寫模式開啟(參見a) rb: 以二進位制模式開啟 read 我們先寫一個檔案,叫1.txt 內容如下 111 22

嵌入式Linux網路程式設計,網路基礎,套接字socket(SOCK_STREAM、SOCK_DGRAM、SOCK_RAW),IP地址,埠,位元組序,位元組序轉換函式,IP地址轉換

文章目錄 1,socket 1.1,socket的型別(SOCK_STREAM、SOCK_DGRAM、SOCK_RAW) 1.2,socket的位置 2,IP地址 2.1,特殊IP地址: 3,埠號

虛擬機器安裝Ubuntu及“二進位制轉換此平臺長模式不相容”的解決

1、虛擬機器的安裝 虛擬機器的安裝軟體版本為 VMware-worksation-full 12.1.1,雙擊 “VMware-workstation-full-12.1.1-3770994.exe”,使用者叧要按照預設安裝項一直點"Next"鍵來進行安裝。安裝完成的最後一

邏輯地址 實體地址轉換

邏輯地址=頁號+頁內地址=p+d; 實體地址=塊號+頁內地址=f+d; (兩個d是一樣的,頁表中儲存有p與f的對應關係)   地址總長度位數=頁號佔位數m+頁內地址佔位數n    邏輯地址中的頁數=2^m         (2的 頁號位數 次冪)   一個頁面(業內

Java Excel 列數字字母互相轉換

class ExcelColumn { public static void main(String[] args) { String colstr = "AA"; int colIndex = excelColStrToNum(colstr, colstr.leng

ip地址整數之間的轉換

IP地址轉換為整數:IP地址每段可以堪稱是8位無符號整數即0~255,把每段拆分成一個二進位制形式,組合起來,然後把這個二進位制數變啊層一個無符號32位整數。整數轉換為IP地址:把這個整數轉換成一個無符號32位二進位制數。從左到右,每八位進行一下分割,得到4段8位的二進位制數,把這些二進位制數轉換成整數然

演算法練習Ip地址整數的互相轉換ipstrToint

int ipstr2int(const char *ip, unsigned int *ipvalue) { if (ip == NULL || ipvalue==NULL) return -

邏輯地址實體地址轉換

最近一直在學8086,上課老師突然問了個這。對於問題“8086 CPU 能提供20位的地址資訊,可直接對1M個儲存單元進行訪問,而CPU內部可用來提供地址資訊的暫存器都是16位,那怎樣用16位暫存器來實現20位地址定址呢"明白了不少。    關於邏輯地址是允許在程式中編排的地

虛擬儲存器--虛擬地址實體地址

計算機在執行程式時,需將程式碼載入入記憶體中,CPU讀取記憶體中的程式碼並執行。 早期的計算機在沒有引入 虛擬儲存器之前,需將整個待執行的程式載入到記憶體中,因為記憶體空間有限,當待載入的程式過大時就會出現問題(多程序,則需要佔用更多的記憶體空間)。 現代計算機引入虛