diff options
author | Jakub Staron <jstaron@google.com> | 2019-04-11 14:09:39 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2019-04-27 01:36:38 -0700 |
commit | d99cd0ae0b21d29d4e5145b889f97e9f6cd11b69 (patch) | |
tree | 0f402840368ce36462d21a17632326e434b6c6b7 /vm_control/src | |
parent | 1f828d7cd738a0bd38eb5342130510e2acb24afd (diff) | |
download | crosvm-d99cd0ae0b21d29d4e5145b889f97e9f6cd11b69.tar crosvm-d99cd0ae0b21d29d4e5145b889f97e9f6cd11b69.tar.gz crosvm-d99cd0ae0b21d29d4e5145b889f97e9f6cd11b69.tar.bz2 crosvm-d99cd0ae0b21d29d4e5145b889f97e9f6cd11b69.tar.lz crosvm-d99cd0ae0b21d29d4e5145b889f97e9f6cd11b69.tar.xz crosvm-d99cd0ae0b21d29d4e5145b889f97e9f6cd11b69.tar.zst crosvm-d99cd0ae0b21d29d4e5145b889f97e9f6cd11b69.zip |
crosvm: Extracts Wayland commands from from VmRequest.
BUG=None TEST=cargo test TEST=cargo test --package msg_socket TEST=cargo test --package devices TEST=cargo test --package vm_control TEST=tast -verbose run ${IP} vm.CrostiniStartEverything Change-Id: I07f034b1cc41e30b9deae68ea9c510b0923e17a8 Reviewed-on: https://chromium-review.googlesource.com/1565299 Commit-Ready: Jakub StaroĊ <jstaron@google.com> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: Zach Reizner <zachr@chromium.org>
Diffstat (limited to 'vm_control/src')
-rw-r--r-- | vm_control/src/lib.rs | 149 |
1 files changed, 92 insertions, 57 deletions
diff --git a/vm_control/src/lib.rs b/vm_control/src/lib.rs index 9d68f52..25902ea 100644 --- a/vm_control/src/lib.rs +++ b/vm_control/src/lib.rs @@ -158,6 +158,95 @@ impl Display for UsbControlResult { } } +#[derive(MsgOnSocket, Debug)] +pub enum WlDriverRequest { + /// Register shared memory represented by the given fd into guest address space. The response + /// variant is `VmResponse::RegisterMemory`. + RegisterMemory(MaybeOwnedFd, usize), + /// Unregister the given memory slot that was previously registereed with `RegisterMemory`. + UnregisterMemory(u32), + /// Allocate GPU buffer of a given size/format and register the memory into guest address space. + /// The response variant is `VmResponse::AllocateAndRegisterGpuMemory` + AllocateAndRegisterGpuMemory { + width: u32, + height: u32, + format: u32, + }, +} + +impl WlDriverRequest { + /// Executes this request on the given Vm. + /// + /// # Arguments + /// * `vm` - The `Vm` to perform the request on. + /// * `allocator` - Used to allocate addresses. + /// + /// This does not return a result, instead encapsulating the success or failure in a + /// `WlDriverResponse` with the intended purpose of sending the response back over the socket + /// that received this `WlDriverResponse`. + pub fn execute(&self, vm: &mut Vm, sys_allocator: &mut SystemAllocator) -> WlDriverResponse { + use self::WlDriverRequest::*; + match *self { + RegisterMemory(ref fd, size) => match register_memory(vm, sys_allocator, fd, size) { + Ok((pfn, slot)) => WlDriverResponse::RegisterMemory { pfn, slot }, + Err(e) => WlDriverResponse::Err(e), + }, + UnregisterMemory(slot) => match vm.remove_device_memory(slot) { + Ok(_) => WlDriverResponse::Ok, + Err(e) => WlDriverResponse::Err(e), + }, + AllocateAndRegisterGpuMemory { + width, + height, + format, + } => { + let (mut fd, desc) = match sys_allocator.gpu_memory_allocator() { + Some(gpu_allocator) => match gpu_allocator.allocate(width, height, format) { + Ok(v) => v, + Err(e) => return WlDriverResponse::Err(e), + }, + None => return WlDriverResponse::Err(SysError::new(ENODEV)), + }; + // Determine size of buffer using 0 byte seek from end. This is preferred over + // `stride * height` as it's not limited to packed pixel formats. + let size = match fd.seek(SeekFrom::End(0)) { + Ok(v) => v, + Err(e) => return WlDriverResponse::Err(SysError::from(e)), + }; + match register_memory(vm, sys_allocator, &fd, size as usize) { + Ok((pfn, slot)) => WlDriverResponse::AllocateAndRegisterGpuMemory { + fd: MaybeOwnedFd::Owned(fd), + pfn, + slot, + desc, + }, + Err(e) => WlDriverResponse::Err(e), + } + } + } + } +} + +#[derive(MsgOnSocket, Debug)] +pub enum WlDriverResponse { + /// The request to register memory into guest address space was successfully done at page frame + /// number `pfn` and memory slot number `slot`. + RegisterMemory { + pfn: u64, + slot: u32, + }, + /// The request to allocate and register GPU memory into guest address space was successfully + /// done at page frame number `pfn` and memory slot number `slot` for buffer with `desc`. + AllocateAndRegisterGpuMemory { + fd: MaybeOwnedFd, + pfn: u64, + slot: u32, + desc: GpuMemoryDesc, + }, + Ok, + Err(SysError), +} + pub type BalloonControlRequestSocket = MsgSocket<BalloonControlCommand, ()>; pub type BalloonControlResponseSocket = MsgSocket<(), BalloonControlCommand>; @@ -166,6 +255,9 @@ pub type DiskControlResponseSocket = MsgSocket<DiskControlResult, DiskControlCom pub type UsbControlSocket = MsgSocket<UsbControlCommand, UsbControlResult>; +pub type WlControlRequestSocket = MsgSocket<WlDriverRequest, WlDriverResponse>; +pub type WlControlResponseSocket = MsgSocket<WlDriverResponse, WlDriverRequest>; + pub type VmControlRequestSocket = MsgSocket<VmRequest, VmResponse>; pub type VmControlResponseSocket = MsgSocket<VmResponse, VmRequest>; @@ -180,18 +272,6 @@ pub enum VmRequest { Suspend, /// Resume the VM's VCPUs that were previously suspended. Resume, - /// Register shared memory represented by the given fd into guest address space. The response - /// variant is `VmResponse::RegisterMemory`. - RegisterMemory(MaybeOwnedFd, usize), - /// Unregister the given memory slot that was previously registereed with `RegisterMemory`. - UnregisterMemory(u32), - /// Allocate GPU buffer of a given size/format and register the memory into guest address space. - /// The response variant is `VmResponse::AllocateAndRegisterGpuMemory` - AllocateAndRegisterGpuMemory { - width: u32, - height: u32, - format: u32, - }, /// Command for balloon driver. BalloonCommand(BalloonControlCommand), /// Send a command to a disk chosen by `disk_index`. @@ -235,18 +315,11 @@ fn register_memory( impl VmRequest { /// Executes this request on the given Vm and other mutable state. /// - /// # Arguments - /// * `vm` - The `Vm` to perform the request on. - /// * `allocator` - Used to allocate addresses. - /// * `run_mode` - Out argument that is set to a run mode if one was requested. - /// /// This does not return a result, instead encapsulating the success or failure in a /// `VmResponse` with the intended purpose of sending the response back over the socket that /// received this `VmRequest`. pub fn execute( &self, - vm: &mut Vm, - sys_allocator: &mut SystemAllocator, run_mode: &mut Option<VmRunMode>, balloon_host_socket: &BalloonControlRequestSocket, disk_host_sockets: &[DiskControlRequestSocket], @@ -265,48 +338,10 @@ impl VmRequest { *run_mode = Some(VmRunMode::Running); VmResponse::Ok } - VmRequest::RegisterMemory(ref fd, size) => { - match register_memory(vm, sys_allocator, fd, size) { - Ok((pfn, slot)) => VmResponse::RegisterMemory { pfn, slot }, - Err(e) => VmResponse::Err(e), - } - } - VmRequest::UnregisterMemory(slot) => match vm.remove_device_memory(slot) { - Ok(_) => VmResponse::Ok, - Err(e) => VmResponse::Err(e), - }, VmRequest::BalloonCommand(ref command) => match balloon_host_socket.send(command) { Ok(_) => VmResponse::Ok, Err(_) => VmResponse::Err(SysError::last()), }, - VmRequest::AllocateAndRegisterGpuMemory { - width, - height, - format, - } => { - let (mut fd, desc) = match sys_allocator.gpu_memory_allocator() { - Some(gpu_allocator) => match gpu_allocator.allocate(width, height, format) { - Ok(v) => v, - Err(e) => return VmResponse::Err(e), - }, - None => return VmResponse::Err(SysError::new(ENODEV)), - }; - // Determine size of buffer using 0 byte seek from end. This is preferred over - // `stride * height` as it's not limited to packed pixel formats. - let size = match fd.seek(SeekFrom::End(0)) { - Ok(v) => v, - Err(e) => return VmResponse::Err(SysError::from(e)), - }; - match register_memory(vm, sys_allocator, &fd, size as usize) { - Ok((pfn, slot)) => VmResponse::AllocateAndRegisterGpuMemory { - fd: MaybeOwnedFd::Owned(fd), - pfn, - slot, - desc, - }, - Err(e) => VmResponse::Err(e), - } - } VmRequest::DiskCommand { disk_index, ref command, |