Updated kernel parsers.
[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   uts_kernel_name : string;
45   uts_nodename : string;
46   uts_kernel_release : string;
47   uts_kernel_version : string;
48   uts_machine : string;
49   uts_domainname : string;
50 }
51
52 type task = {
53   task_state : int64;
54   task_prio : int64;
55   task_normal_prio : int64;
56   task_static_prio : int64;
57   task_comm : string;
58   task_pid : int64;
59 }
60
61 type net_device = {
62   netdev_name : string;
63   netdev_dev_addr : string;
64 }
65
66 type kdata = {
67   ksyms : ksymmap option;
68   utsname : utsname option;
69   tasks : task list option;
70   net_devices : net_device list option;
71 }
72
73 exception ParseError of string * string * string
74
75 type fieldsig = {
76   field_available : bool;
77   field_offset : int;
78 }
79
80 (* This is the maximum we can download in one go over the libvirt
81  * remote connection.
82  *
83  * XXX Should have a 'D.max_peek' function.
84  *)
85 let max_memory_peek = 65536
86
87 type load_memory_error =
88   | AddressOutOfRange
89   | DomIsNull
90
91 exception LoadMemoryError of load_memory_error * string
92
93 let _load_memory mem dom start size =
94   let str = String.create size in
95   let rec loop i =
96     let remaining = size - i in
97     if remaining > 0 then (
98       let size = min remaining max_memory_peek in
99       D.memory_peek dom [D.Virtual] (start +^ Int64.of_int i) size str i;
100       loop (i + size)
101     )
102   in
103   loop 0;
104
105   Virt_mem_mmap.add_string mem str start
106
107 let load_static_memory ~dom ~domname ~arch ~wordsize ~endian
108     ~kernel_min ~kernel_max start size =
109   if start < kernel_min then
110     raise (LoadMemoryError (AddressOutOfRange,
111                             "load_memory: start < kernel_min"))
112   else if start +^ Int64.of_int size > kernel_max then
113     raise (LoadMemoryError (AddressOutOfRange,
114                             "load_memory: start+size > kernel_max"))
115   else (
116     let mem = Virt_mem_mmap.create () in
117     let mem = Virt_mem_mmap.set_wordsize mem wordsize in
118     let mem = Virt_mem_mmap.set_endian mem endian in
119
120     let mem = _load_memory mem dom start size in
121
122     { dom = Some dom; domname = domname; mem = mem; arch = arch;
123       kernel_min = kernel_min; kernel_max = kernel_max }
124   )
125
126 let load_memory ({ dom = dom; mem = mem; kernel_min = kernel_min;
127                    kernel_max = kernel_max } as image) start size =
128   if start < kernel_min then
129     raise (LoadMemoryError (AddressOutOfRange,
130                             "load_memory: start < kernel_min"))
131   else if start +^ Int64.of_int size > kernel_max then
132     raise (LoadMemoryError (AddressOutOfRange,
133                             "load_memory: start+size > kernel_max"))
134   else if Virt_mem_mmap.is_mapped_range mem start size then image
135   else (
136     match dom with
137     | None ->
138         raise (LoadMemoryError (DomIsNull, "load_memory: dom = None"))
139     | Some dom ->
140         let mem = _load_memory mem dom start size in
141         { image with mem = mem }
142   )