293d2de2ab64ca7c506ef2bf0464bb6144997370
[virt-mem.git] / lib / virt_mem_types.ml
1 (** Common types. *)
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    Common types.
21  *)
22
23 module D = Libvirt.Domain
24
25 open Virt_mem_utils
26 open Virt_mem_mmap
27
28 type ksym = string
29
30 module Ksymmap = Map.Make (String)
31
32 type ksymmap = addr Ksymmap.t
33
34 type image = {
35   dom : Libvirt.ro D.t option;
36   domname : string;
37   arch : architecture;
38   mem : ([`Wordsize], [`Endian], [`HasMapping]) Virt_mem_mmap.t;
39   kernel_min : addr;
40   kernel_max : addr;
41 }
42
43 type utsname = {
44   kernel_name : string;
45   nodename : string;
46   kernel_release : string;
47   kernel_version : string;
48   machine : string;
49   domainname : string;
50 }
51
52 type kdata = {
53   ksyms : ksymmap option;
54   utsname : utsname option;
55   tasks : Virt_mem_mmap.addr option;
56 }
57
58 (* This is the maximum we can download in one go over the libvirt
59  * remote connection.
60  *
61  * XXX Should have a 'D.max_peek' function.
62  *)
63 let max_memory_peek = 65536
64
65 type load_memory_error =
66   | AddressOutOfRange
67   | DomIsNull
68
69 exception LoadMemoryError of load_memory_error * string
70
71 let _load_memory mem dom start size =
72   let str = String.create size in
73   let rec loop i =
74     let remaining = size - i in
75     if remaining > 0 then (
76       let size = min remaining max_memory_peek in
77       D.memory_peek dom [D.Virtual] (start +^ Int64.of_int i) size str i;
78       loop (i + size)
79     )
80   in
81   loop 0;
82
83   Virt_mem_mmap.add_string mem str start
84
85 let load_static_memory ~dom ~domname ~arch ~wordsize ~endian
86     ~kernel_min ~kernel_max start size =
87   if start < kernel_min then
88     raise (LoadMemoryError (AddressOutOfRange,
89                             "load_memory: start < kernel_min"))
90   else if start +^ Int64.of_int size > kernel_max then
91     raise (LoadMemoryError (AddressOutOfRange,
92                             "load_memory: start+size > kernel_max"))
93   else (
94     let mem = Virt_mem_mmap.create () in
95     let mem = Virt_mem_mmap.set_wordsize mem wordsize in
96     let mem = Virt_mem_mmap.set_endian mem endian in
97
98     let mem = _load_memory mem dom start size in
99
100     { dom = Some dom; domname = domname; mem = mem; arch = arch;
101       kernel_min = kernel_min; kernel_max = kernel_max }
102   )
103
104 let load_memory ({ dom = dom; mem = mem; kernel_min = kernel_min;
105                    kernel_max = kernel_max } as image) start size =
106   if start < kernel_min then
107     raise (LoadMemoryError (AddressOutOfRange,
108                             "load_memory: start < kernel_min"))
109   else if start +^ Int64.of_int size > kernel_max then
110     raise (LoadMemoryError (AddressOutOfRange,
111                             "load_memory: start+size > kernel_max"))
112   else if Virt_mem_mmap.is_mapped_range mem start size then image
113   else (
114     match dom with
115     | None ->
116         raise (LoadMemoryError (DomIsNull, "load_memory: dom = None"))
117     | Some dom ->
118         let mem = _load_memory mem dom start size in
119         { image with mem = mem }
120   )