50650b4192410b68f38d94f905b11593344c2d91
[virt-mem.git] / lib / virt_mem_mmap.mli
1 (** Memory map. *)
2 (* Memory info command for virtual domains.
3    (C) Copyright 2008 Richard W.M. Jones, Red Hat Inc.
4    http://libvirt.org/
5
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.
10
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.
15
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.
19  *)
20
21 (** {2 Memory maps}
22
23     Memory maps represent the virtual memory of a virtual machine.
24
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.
32
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).
36
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.
42
43     {3 Overlapping mappings and holes}
44
45     If mappings overlap, then the mapping which was added later
46     overrides/overwrites earlier mappings at any addresses which
47     coincide.
48
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.
55
56     {3 Word size and endianness}
57
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}.
63
64     {3 Efficiency}
65
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.
69
70     Some low level functions are written in C for speed.
71
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
77     very fast.
78 *)
79
80 (** {2 Types} *)
81
82 type ('ws,'e,'hm) t
83 (** Memory map.
84
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.
89
90     See also:
91     [http://camltastic.blogspot.com/2008/05/phantom-types.html] *)
92
93 type addr = int64
94 (** Virtual memory addresses (even on 32 bit machines). *)
95
96 (** {2 Create a memory map, add mappings} *)
97
98 val create : unit -> ([`NoWordsize], [`NoEndian], [`NoMappings]) t
99 (** Create a new, empty memory map. *)
100
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]. *)
104
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. *)
109
110 val of_string : string -> addr -> ([`NoWordsize], [`NoEndian], [`HasMapping]) t
111 (** Create a new memory map, mapping string at address [addr]. *)
112
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. *)
117
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
122     set only once. *)
123
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
128     set only once. *)
129
130 val get_wordsize : ([`Wordsize], 'e, 'hm) t -> Virt_mem_utils.wordsize
131 (** Return the wordsize previously set for this memory map. *)
132
133 val get_endian : ('ws, [`Endian], 'hm) t -> Bitstring.endian
134 (** Return the endianness previously set for this memory map. *)
135
136 (** {2 Searching} *)
137
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.
143
144     Any holes in the memory map are skipped automatically.
145
146     Note that this doesn't find strings which straddle the
147     boundary of two adjacent or overlapping mappings.
148
149     Note that because the string being matched is an OCaml
150     string it may contain NULs (zero bytes) and those are matched
151     properly. *)
152
153 val find_align : ([`Wordsize], 'e, [`HasMapping]) t -> ?start:addr -> string ->
154   addr option
155 (** Same as {!find}, but the string must be aligned to the word size of
156     the memory map. *)
157
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. *)
160
161 val find_all_align : ([`Wordsize], 'e, [`HasMapping]) t -> ?start:addr ->
162   string -> addr list
163 (** Same as {!find_all}, but the strings must be aligned to the word size. *)
164
165 val find_pointer : ([`Wordsize], [`Endian], [`HasMapping]) t -> ?start:addr ->
166   addr -> addr option
167 (** Find a pointer (address) in the memory map.
168     The pointer must be aligned to a word. *)
169
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. *)
173
174 (** {2 Get bytes and ranges of bytes} *)
175
176 val get_byte : ('ws, 'e, [`HasMapping]) t -> addr -> int
177 (** Return the byte at the given address.
178
179     This will raise [Invalid_argument "get_byte"] if the address is
180     a hole (not mapped). *)
181
182 val get_bytes : ('ws, 'e, [`HasMapping]) t -> addr -> int -> string
183 (** Return the sequence of bytes starting at the given address.
184
185     This will raise [Invalid_argument "get_bytes"] if the address range
186     contains holes. *)
187
188 val get_int32 : ('ws, [`Endian], [`HasMapping]) t -> addr -> int32
189 (** Return the 32-bit int at [addr]. *)
190
191 val get_int64 : ('ws, [`Endian], [`HasMapping]) t -> addr -> int64
192 (** Return the 64-bit int at [addr]. *)
193
194 val get_C_int : ([`Wordsize], [`Endian], [`HasMapping]) t -> addr -> int32
195 (** Return the C 32-bit int at [addr]. *)
196
197 val get_C_long : ([`Wordsize], [`Endian], [`HasMapping]) t -> addr -> int64
198 (** Return the C 32 or 64-bit long at [addr]. *)
199
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.
204
205     This may raise [Invalid_argument "get_string"] if we reach a
206     hole (unmapped address) before finding the end of the string.
207
208     See also {!get_bytes}, {!is_string} and {!is_C_identifier}. *)
209
210 val is_string : ('ws, 'e, [`HasMapping]) t -> addr -> bool
211 (** Return true or false if the address contains an ASCII NUL-terminated
212     string. *)
213
214 val is_C_identifier : ('ws, 'e, [`HasMapping]) t -> addr -> bool
215 (** Return true or false if the address contains a NUL-terminated
216     C identifier. *)
217
218 val is_mapped : ('ws, 'e, [`HasMapping]) t -> addr -> bool
219 (** Return true if the single address [addr] is mapped. *)
220
221 val follow_pointer : ([`Wordsize], [`Endian], [`HasMapping]) t -> addr -> addr
222 (** Follow (dereference) the pointer at [addr] and return
223     the address pointed to. *)
224
225 val succ_long : ([`Wordsize], 'e, [`HasMapping]) t -> addr -> addr
226 (** Add wordsize bytes to [addr] and return it. *)
227
228 val pred_long : ([`Wordsize], 'e, [`HasMapping]) t -> addr -> addr
229 (** Subtract wordsize bytes from [addr] and return it. *)
230
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]. *)
234
235 (** {2 Save and load memory maps} *)
236
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. *)
240
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]. *)