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