/dev/mem是物理記憶體的全映像,可以用來訪問物理記憶體,用mmap來訪問物理記憶體以及外設的io資源,是實現使用者空間驅動的一種方法
我們先用hexedit來看下/dev/mem,hexedit /dev/mem 可以物理記憶體的資訊,當然肉眼是無法看的畢竟是16進製制。
00000000 53 ff 00 f0 53 ff 00 f0 53 ff 00 f0 53 ff 00 f0 s...s...s...s...
00000010 53 ff 00f0 53 ff 00 f0 cc e9 00 f0 53 ff 00 f0 s...s.......s...
00000020 a5 fe 00 f0 87 e9 00 f0 53 ff 00 f0 46 e7 00 f0 ........s...f...
00000030 46 e7 00 f0 46 e7 00 f0 57 ef 00 f0 53 ff 00 f0 f...f...w...s...
00000040 22 00 00 c0 4d f8 00 f041 f8 00 f0 fe e3 00 f0 "...m...a.......
00000050 39 e7 00 f0 59 f8 00 f0 2e e8 00 f0 d4 ef 00 f0 9...y...........
00000060 a4 f0 00 f0 f2 e6 00 f0 6e fe 00 f0 53 ff 00 f0 ........n...s...
00000070 ed ef 00 f0 53 ff 00 f0 c7 ef 00 f0 ec 57 00 c0 ....s........w..
00000080 53 ff 00 f0 53 ff 00 f0 53 ff 00 f0 53 ff 00 f0 s...s...s...s...
00000090 53 ff 00 f0 53 ff 00 f0 53 ff 00 f0 53 ff 00 f0 s...s...s...s...
000000a0 53 ff 00 f0 53 ff 00 f0 53 ff 00 f0 53 ff 00 f0 s...s...s...s...
000000b0 53 ff 00 f0 53 ff 00 f0 53 ff 00 f0 53 ff 00 f0 s...s...s...s...
000000c0 53 ff 00 f0 53 ff 00 f0 53 ff 00 f0 53 ff 00 f0 s...s...s...s...
000000d0 53 ff 00 f0 53 ff 00 f0 53 ff 00 f0 53 ff 00 f0 s...s...s...s...
000000e0 53 ff 00 f0 53 ff 00 f0 53 ff 00 f0 53 ff 00 f0 s...s...s...s...
000000f0 53 ff 00 f0 53 ff 00 f0 53 ff 00 f0 53 ff 00 f0 s...s...s...s...
不過可以用mmap將/dev/mem 對映出來,然後可以對其讀寫可以實現使用者空間的核心操作。先來說下mmap函式,
void
*mmap(void
*
addr
,
size_t
length
,
int
prot
,
int
flags
,
int
fd,
off_t
offset
);
共6個引數含義分別如下:
laddr如果為null,那麼有核心選擇乙個對映的位址,如果不為null,那核心會把引數當做對映的提示(對映的位址就在所提示的附近,不會百分百確保的)
lâ â length表示對映長度
lprot表示對對映的保護, 可以是可執行,可讀,可寫或不可訪問,prot_exec,prot_read,prot_write,prot_none
lflag表示是否對其他程序可見,map_shared表示其他程序可見。
lfd需要對映的檔案描述符
loffset指向fd的編譯
接下去我們用mmap來對映/dev/mem,編寫**如下:
#include
#include
#include
#include
#include
#include
intmain ()
map_base =
mmap (
null
, 0xff
, prot_read | prot_write, map_shared, fd,
0x20000
);if
(map_base == 0)
else
unsigned
long
addr;
unsigned
char
content;
inti = 0;
for(; i <
0xf; ++i)
close (fd);
munmap (map_base,
0xff
);return(1
);}編譯後,執行如下:
map successfull!
address: 0x7fbaafb5e000 value: 0x0 address: 0x7fbaafb5e000 new value: 0x0
address: 0x7fbaafb5e001 value: 0x0 address: 0x7fbaafb5e001 new value: 0x1
address: 0x7fbaafb5e002 value: 0x0 address: 0x7fbaafb5e002 new value: 0x2
address: 0x7fbaafb5e003 value: 0x0 address: 0x7fbaafb5e003 new value: 0x3
address: 0x7fbaafb5e004 value: 0x0 address: 0x7fbaafb5e004 new value: 0x4
address: 0x7fbaafb5e005 value: 0x0 address: 0x7fbaafb5e005 new value: 0x5
address: 0x7fbaafb5e006 value: 0x0 address: 0x7fbaafb5e006 new value: 0x6
address: 0x7fbaafb5e007 value: 0x0 address: 0x7fbaafb5e007 new value: 0x7
address: 0x7fbaafb5e008 value: 0x0 address: 0x7fbaafb5e008 new value: 0x8
address: 0x7fbaafb5e009 value: 0x0 address: 0x7fbaafb5e009 new value: 0x9
address: 0x7fbaafb5e00a value: 0x0 address: 0x7fbaafb5e00a new value: 0xa
address: 0x7fbaafb5e00b value: 0x0 address: 0x7fbaafb5e00b new value: 0xb
address: 0x7fbaafb5e00c value: 0x0 address: 0x7fbaafb5e00c new value: 0xc
address: 0x7fbaafb5e00d value: 0x0 address: 0x7fbaafb5e00d new value: 0xd
address: 0x7fbaafb5e00e value: 0x0 address: 0x7fbaafb5e00e new value: 0xe
例子將實體地址起始位址0x20000, 長度為0xf對映出來了,然後進行了讀寫操作。這裡0x7fbaafb5e000
是mmap函式返回的對映位址。第二次執行的時候,會發現記憶體中的值已經是上次修改過的值了並非全0.。
/dev/mem還可以用來檢測系統甚至給系統打補丁,為了防止/dev/mem被注入**,可以設定系統配置選項config_strict_devmem=y
。此外還有port和kmem,/dev/port同/dev/mem,不過訪問的是i/o埠。
/dev/kmem也同/dev/mem,不過其訪問的是虛擬記憶體而不是物理記憶體。
通過muduo的Atomic h學習原子操作
最新在讀muduo原始碼,想寫乙個原始碼閱讀的系列,就以這個為開篇吧 原始碼如下 templateclass atomicintegert noncopyable uncomment if you need copying and assignment atomicintegert const at...
Linux實操命令
vi命令 3.vi 檔案後 set nu 則顯示檔案的行號 4.vi 檔案後 g是跳到檔案尾部 gg是跳到首行 nohup 不結束通話地執行命令 關閉終端 1.nohup command 2.在當前終端介面檢視執行的後台程序 jobs l 3.關閉終端重新開啟終端檢視執行的後台程序 ps ef gr...
Linux和Windows指令碼中的延時操作
sleep 2 系統會延時2秒才執行下面的命令。但是windows中卻沒有對應的命令。如何做到延時呢?自己寫乙個簡單的延時程式當然可以辦到,同時有另外乙個更簡單的辦法。比如要掩飾10秒,可以輸入 ping 127.0.0.1 n 10 null 127.0.0.1是本機位址,只要不做特殊設定是可以p...