|
| 1 | +pub mod error; |
| 2 | +pub mod support; |
| 3 | + |
| 4 | +#[macro_use] |
| 5 | +mod macros; |
| 6 | + |
1 | 7 | extern crate xenctrl_sys;
|
2 |
| -use std::io::Error; |
3 |
| -use std::ptr::{null_mut}; |
4 |
| -use std::os::raw::{c_void, c_ulong}; |
| 8 | +use std::{ |
| 9 | + mem, |
| 10 | + ptr::{null_mut, NonNull}, |
| 11 | +}; |
| 12 | + |
| 13 | +use xenctrl_sys::{ |
| 14 | + xc_clear_last_error, xc_domain_maximum_gpfn, xc_domain_pause, xc_domain_unpause, xc_error_code, |
| 15 | + xc_get_last_error, xc_interface, xc_interface_close, xc_interface_open, xc_monitor_disable, |
| 16 | + xc_monitor_enable, xentoollog_logger, |
| 17 | +}; |
5 | 18 |
|
6 |
| -pub const PAGE_SHIFT: u32 = xenctrl_sys::XC_PAGE_SHIFT; |
7 |
| -pub const PAGE_SIZE: u32 = xenctrl_sys::XC_PAGE_SIZE; |
| 19 | +use error::Error; |
| 20 | +use support::PageInfo; |
| 21 | + |
| 22 | +type Result<T> = std::result::Result<T, Error>; |
8 | 23 |
|
9 | 24 | #[derive(Debug)]
|
10 |
| -pub struct Xc { |
11 |
| - handle: *mut xenctrl_sys::xc_interface, |
12 |
| - evtchn_port: *mut u32, |
| 25 | +pub struct XenControl { |
| 26 | + handle: NonNull<xc_interface>, |
| 27 | + evtchn_port: u32, |
13 | 28 | }
|
14 | 29 |
|
15 |
| -impl Xc { |
16 |
| - |
17 |
| - pub fn new() -> Result<Self,Error> { |
| 30 | +impl XenControl { |
| 31 | + pub fn from( |
| 32 | + logger: Option<&mut xentoollog_logger>, |
| 33 | + dombuild_logger: Option<&mut xentoollog_logger>, |
| 34 | + open_flags: u32, |
| 35 | + ) -> Result<Self> { |
18 | 36 | let xc_handle = unsafe {
|
19 |
| - xenctrl_sys::xc_interface_open(null_mut(), null_mut(), 0) |
| 37 | + xc_interface_open( |
| 38 | + logger.map_or_else(|| null_mut(), |l| l as *mut _), |
| 39 | + dombuild_logger.map_or_else(|| null_mut(), |l| l as *mut _), |
| 40 | + open_flags, |
| 41 | + ) |
20 | 42 | };
|
21 |
| - if xc_handle == null_mut() { |
22 |
| - return Err(Error::last_os_error()); |
23 |
| - } |
24 |
| - Ok(Xc { |
25 |
| - handle: xc_handle, |
26 |
| - evtchn_port: null_mut() |
27 |
| - }) |
| 43 | + |
| 44 | + NonNull::new(xc_handle) |
| 45 | + .ok_or_else(|| Error::new(xc_error_code::XC_INTERNAL_ERROR)) |
| 46 | + .map(|handle| XenControl { |
| 47 | + handle, |
| 48 | + evtchn_port: 0u32, |
| 49 | + }) |
28 | 50 | }
|
29 | 51 |
|
30 |
| - pub fn monitor_enable(&self, domid: u32) -> *mut c_void { |
31 |
| - unsafe { |
32 |
| - xenctrl_sys::xc_monitor_enable(self.handle, domid, self.evtchn_port) |
33 |
| - } |
| 52 | + pub fn default() -> Result<Self> { |
| 53 | + Self::from(None, None, 0) |
34 | 54 | }
|
35 | 55 |
|
36 |
| - pub fn monitor_disable(&self, domid: u32) { |
| 56 | + pub fn monitor_enable(&mut self, domid: u32) -> Result<&PageInfo> { |
| 57 | + let xc = self.handle.as_ptr(); |
| 58 | + let ring_page = unsafe { |
| 59 | + xc_clear_last_error(xc); |
| 60 | + xc_monitor_enable(xc, domid, &mut self.evtchn_port as _) as *const PageInfo |
| 61 | + }; |
| 62 | + last_error!(xc, &*ring_page) |
| 63 | + } |
| 64 | + |
| 65 | + pub fn monitor_disable(&self, domid: u32) -> Result<()> { |
| 66 | + let xc = self.handle.as_ptr(); |
37 | 67 | unsafe {
|
38 |
| - xenctrl_sys::xc_monitor_disable(self.handle, domid); |
| 68 | + xc_clear_last_error(xc); |
| 69 | + xc_monitor_disable(xc, domid); |
39 | 70 | };
|
| 71 | + last_error!(xc, ()) |
40 | 72 | }
|
41 | 73 |
|
42 |
| - pub fn domain_pause(&self, domid: u32) -> Result<(),&str> { |
| 74 | + pub fn domain_pause(&self, domid: u32) -> Result<()> { |
| 75 | + let xc = self.handle.as_ptr(); |
43 | 76 | unsafe {
|
44 |
| - match xenctrl_sys::xc_domain_pause(self.handle, domid) { |
45 |
| - 0 => Ok(()), |
46 |
| - -1 => Err("Fail to pause domain"), |
47 |
| - _ => panic!("unexpected value"), |
48 |
| - } |
49 |
| - } |
| 77 | + xc_clear_last_error(xc); |
| 78 | + xc_domain_pause(xc, domid); |
| 79 | + }; |
| 80 | + last_error!(xc, ()) |
50 | 81 | }
|
51 | 82 |
|
52 |
| - pub fn domain_unpause(&self, domid: u32) -> Result<(),&str> { |
| 83 | + pub fn domain_unpause(&self, domid: u32) -> Result<()> { |
| 84 | + let xc = self.handle.as_ptr(); |
53 | 85 | unsafe {
|
54 |
| - match xenctrl_sys::xc_domain_unpause(self.handle, domid) { |
55 |
| - 0 => Ok(()), |
56 |
| - -1 => Err("Fail to unpause domain"), |
57 |
| - _ => panic!("unexpected value"), |
58 |
| - } |
| 86 | + xc_clear_last_error(xc); |
| 87 | + xc_domain_unpause(xc, domid); |
59 | 88 | }
|
| 89 | + last_error!(xc, ()) |
60 | 90 | }
|
61 | 91 |
|
62 |
| - pub fn domain_maximum_gpfn(&self, domid: u32) -> Result<u64,&str> { |
63 |
| - let mut max_gpfn: c_ulong = 0; |
64 |
| - let ptr_max_gpfn: *mut c_ulong = &mut max_gpfn; |
65 |
| - let result = unsafe { |
66 |
| - xenctrl_sys::xc_domain_maximum_gpfn(self.handle, domid, ptr_max_gpfn) |
67 |
| - }; |
68 |
| - match result { |
69 |
| - 0 => Ok(max_gpfn as u64), |
70 |
| - -1 => Err("Fail to get max gpfn"), |
71 |
| - _ => panic!("unexpected value"), |
| 92 | + pub fn domain_maximum_gpfn(&self, domid: u32) -> Result<u64> { |
| 93 | + let xc = self.handle.as_ptr(); |
| 94 | + let mut max_gpfn: u64; |
| 95 | + unsafe { |
| 96 | + max_gpfn = mem::uninitialized(); |
| 97 | + xc_clear_last_error(xc); |
| 98 | + xc_domain_maximum_gpfn(xc, domid, &mut max_gpfn as _); |
72 | 99 | }
|
| 100 | + last_error!(xc, max_gpfn) |
73 | 101 | }
|
74 | 102 |
|
75 |
| - fn close(&mut self) -> Result<(),&str>{ |
76 |
| - let result = unsafe { |
77 |
| - xenctrl_sys::xc_interface_close(self.handle) |
78 |
| - }; |
79 |
| - match result { |
80 |
| - 0 => Ok(()), |
81 |
| - -1 => Err("Fail to close xc interface"), |
82 |
| - _ => panic!("unexpected value"), |
| 103 | + fn close(&mut self) -> Result<()> { |
| 104 | + let xc = self.handle.as_ptr(); |
| 105 | + unsafe { |
| 106 | + xc_clear_last_error(xc); |
| 107 | + xc_interface_close(xc); |
83 | 108 | }
|
| 109 | + last_error!(xc, ()) |
84 | 110 | }
|
85 | 111 | }
|
86 | 112 |
|
87 |
| -impl Drop for Xc { |
| 113 | +impl Drop for XenControl { |
88 | 114 | fn drop(&mut self) {
|
89 | 115 | self.close().unwrap();
|
90 | 116 | }
|
|
0 commit comments