2 (* Memory info command for virtual domains.
3 (C) Copyright 2008 Richard W.M. Jones, Red Hat Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 Memory maps represent the virtual memory of a virtual machine.
25 We are mostly interested in the kernel memory and kernel data
26 structures. In Linux this stays at the same virtual memory
27 address whichever task is actually running (eg. on i386 machines,
28 the kernel is often found at virtual address 0xC0100000). Kernel
29 memory is spread out over several ranges of addresses, with gaps
30 of uninteresting or non-existant virtual addresses in between, and
31 this structure captures that.
33 A memory map is a range of 64 bit addresses from [0] to [2^64-1].
34 (Note that 64 bit addresses are used even for 32 bit virtual
35 machines - just ignore everything above 0xFFFFFFFF).
37 A memory map consists of zero or more {b mappings} of data. A
38 mapping starts at some address and has some size, and the data for
39 a mapping can come from some source such as a file or OCaml
40 string. Use {!of_file}, {!of_string}, {!add_file}, {!add_string}
41 to create a memory map from mappings.
43 {3 Overlapping mappings and holes}
45 If mappings overlap, then the mapping which was added later
46 overrides/overwrites earlier mappings at any addresses which
49 Where there is no mapping for a particular address, the memory map
50 is said to have a hole. (Typically almost all of a memory map is
51 holes). In general, the searching functions such as {!find} skip
52 over holes, while the accessor functions such as {!get_bytes}
53 raise an error if you try to read a hole, but read the individual
54 function documentation.
56 {3 Word size and endianness}
58 Memory maps may (or may not) have an associated word size and
59 endianness for the whole map. These are used when we look at
60 integers and pointers in the memory. See {!get_endian},
61 {!set_endian}, {!get_wordsize} and {!set_wordsize}, and accessor
62 functions such as {!get_int32} and {!follow_pointer}.
66 Mappings' data are stored in 1D Bigarrays. The advantages of
67 using a Bigarray are: (a) hidden from the garbage collector, (b)
68 easily accessible from C, (c) uses mmap(2) where possible.
70 Some low level functions are written in C for speed.
72 Mappings are stored in a segment tree for efficient access, but
73 the segment tree has to be rebuilt from scratch each time you add
74 a new mapping. It is not known if there is a more efficient way
75 to incrementally update a segment tree. In any case, as long as
76 you are mainly doing lookups / searches / getting bytes, this is
85 The ['ws], ['e] and ['hm] type parameters are phantom types
86 designed to ensure you don't try illegal operations before
87 initializing certain parts of the memory map. If you are not
88 familiar with phantom types, you can just ignore them.
91 [http://camltastic.blogspot.com/2008/05/phantom-types.html] *)
94 (** Virtual memory addresses (even on 32 bit machines). *)
96 (** {2 Create a memory map, add mappings} *)
98 val create : unit -> ([`NoWordsize], [`NoEndian], [`NoMappings]) t
99 (** Create a new, empty memory map. *)
101 val of_file : Unix.file_descr -> addr ->
102 ([`NoWordsize], [`NoEndian], [`HasMapping]) t
103 (** Create a new memory map, mapping file [fd] at address [addr]. *)
105 val add_file : ('ws, 'e, 'hm) t -> Unix.file_descr -> addr ->
106 ('ws, 'e, [`HasMapping]) t
107 (** Add file [fd] at address [addr] to an existing memory map.
108 The new mapping can overwrite all or part of an existing mapping. *)
110 val of_string : string -> addr -> ([`NoWordsize], [`NoEndian], [`HasMapping]) t
111 (** Create a new memory map, mapping string at address [addr]. *)
113 val add_string : ('ws, 'e, 'hm) t -> string -> addr ->
114 ('ws, 'e, [`HasMapping]) t
115 (** Add string at address [addr] to an existing memory map.
116 The new mapping can overwrite all or part of an existing mapping. *)
118 val set_wordsize : ([`NoWordsize], 'e, 'hm) t -> Virt_mem_utils.wordsize ->
119 ([`Wordsize], 'e, 'hm) t
120 (** Set the natural wordsize of the memory map. This is used
121 for matching integers and pointers within the map and can be
124 val set_endian : ('ws, [`NoEndian], 'hm) t -> Bitstring.endian ->
125 ('ws, [`Endian], 'hm) t
126 (** Set the natural endianness of the memory map. This is used
127 for matching integers and pointers within the map and can be
130 val get_wordsize : ([`Wordsize], 'e, 'hm) t -> Virt_mem_utils.wordsize
131 (** Return the wordsize previously set for this memory map. *)
133 val get_endian : ('ws, [`Endian], 'hm) t -> Bitstring.endian
134 (** Return the endianness previously set for this memory map. *)
138 val find : ('ws, 'e, [`HasMapping]) t -> ?start:addr -> string -> addr option
139 (** Find string in a memory map and return its address (if found)
140 or [None] (if not found). You can pass an optional starting
141 address. If no start address is given, we begin searching at
142 the beginning of the first mapping.
144 Any holes in the memory map are skipped automatically.
146 Note that this doesn't find strings which straddle the
147 boundary of two adjacent or overlapping mappings.
149 Note that because the string being matched is an OCaml
150 string it may contain NULs (zero bytes) and those are matched
153 val find_align : ([`Wordsize], 'e, [`HasMapping]) t -> ?start:addr -> string ->
155 (** Same as {!find}, but the string must be aligned to the word size of
158 val find_all : ('ws, 'e, [`HasMapping]) t -> ?start:addr -> string -> addr list
159 (** Same as {!find}, but returns all occurrences of a string in a memory map. *)
161 val find_all_align : ([`Wordsize], 'e, [`HasMapping]) t -> ?start:addr ->
163 (** Same as {!find_all}, but the strings must be aligned to the word size. *)
165 val find_pointer : ([`Wordsize], [`Endian], [`HasMapping]) t -> ?start:addr ->
167 (** Find a pointer (address) in the memory map.
168 The pointer must be aligned to a word. *)
170 val find_pointer_all : ([`Wordsize], [`Endian], [`HasMapping]) t ->
171 ?start:addr -> addr -> addr list
172 (** Find all occurrences of a pointer in the memory map. *)
174 (** {2 Get bytes and ranges of bytes} *)
176 val get_byte : ('ws, 'e, [`HasMapping]) t -> addr -> int
177 (** Return the byte at the given address.
179 This will raise [Invalid_argument "get_byte"] if the address is
180 a hole (not mapped). *)
182 val get_bytes : ('ws, 'e, [`HasMapping]) t -> addr -> int -> string
183 (** Return the sequence of bytes starting at the given address.
185 This will raise [Invalid_argument "get_bytes"] if the address range
188 val get_int32 : ('ws, [`Endian], [`HasMapping]) t -> addr -> int32
189 (** Return the 32-bit int at [addr]. *)
191 val get_int64 : ('ws, [`Endian], [`HasMapping]) t -> addr -> int64
192 (** Return the 64-bit int at [addr]. *)
194 val get_C_int : ([`Wordsize], [`Endian], [`HasMapping]) t -> addr -> int32
195 (** Return the C 32-bit int at [addr]. *)
197 val get_C_long : ([`Wordsize], [`Endian], [`HasMapping]) t -> addr -> int64
198 (** Return the C 32 or 64-bit long at [addr]. *)
200 val get_string : ('ws, 'e, [`HasMapping]) t -> addr -> string
201 (** Return the sequence of bytes starting at [addr] up to (but not
202 including) the first ASCII NUL character. In other words, this
203 returns a C-style string.
205 This may raise [Invalid_argument "get_string"] if we reach a
206 hole (unmapped address) before finding the end of the string.
208 See also {!get_bytes}, {!is_string} and {!is_C_identifier}. *)
210 val is_string : ('ws, 'e, [`HasMapping]) t -> addr -> bool
211 (** Return true or false if the address contains an ASCII NUL-terminated
214 val is_C_identifier : ('ws, 'e, [`HasMapping]) t -> addr -> bool
215 (** Return true or false if the address contains a NUL-terminated
218 val is_mapped : ('ws, 'e, [`HasMapping]) t -> addr -> bool
219 (** Return true if the single address [addr] is mapped. *)
221 val follow_pointer : ([`Wordsize], [`Endian], [`HasMapping]) t -> addr -> addr
222 (** Follow (dereference) the pointer at [addr] and return
223 the address pointed to. *)
225 val succ_long : ([`Wordsize], 'e, [`HasMapping]) t -> addr -> addr
226 (** Add wordsize bytes to [addr] and return it. *)
228 val pred_long : ([`Wordsize], 'e, [`HasMapping]) t -> addr -> addr
229 (** Subtract wordsize bytes from [addr] and return it. *)
231 val align : ([`Wordsize], 'e, [`HasMapping]) t -> addr -> addr
232 (** Align the [addr] to the next wordsize boundary. If it already
233 aligned, this just returns [addr]. *)
235 (** {2 Save and load memory maps} *)
237 (*val to_channel : ('ws, 'e, [`HasMapping]) t -> out_channel -> unit*)
238 (** Write the memory map and data to the given output channel in
239 a reasonably efficient and stable binary format. *)
241 (*val from_channel : in_channel -> ('?, '?, [`HasMapping]) t*)
242 (** Read a previously saved memory map. If the input channel does
243 not contain a memory map, this raises [Invalid_argument]. *)