• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use sys_util::{GuestAddress, GuestMemory, MemoryMapping};
6 
7 #[link(name = "rendernodehost")]
8 extern "C" {
start_render_node_host( gpu_host_mem: *mut u8, gpu_guest_mem_start: u64, gpu_guest_mem_size: u64, host_start: *const u8, host_4g_start: *const u8, )9     fn start_render_node_host(
10         gpu_host_mem: *mut u8,
11         gpu_guest_mem_start: u64,
12         gpu_guest_mem_size: u64,
13         host_start: *const u8,
14         host_4g_start: *const u8,
15     );
16 }
17 
18 /// The number of bytes in 4 GiB.
19 pub const FOUR_GB: u64 = (1 << 32);
20 /// The size required for the render node host in host and guest address space.
21 pub const RENDER_NODE_HOST_SIZE: u64 = FOUR_GB;
22 
23 /// A render node host device that interfaces with the guest render node forwarder.
24 pub struct RenderNodeHost {
25     #[allow(dead_code)]
26     guest_mem: GuestMemory,
27 }
28 
29 impl RenderNodeHost {
30     /// Starts the render node host forwarding service over the given guest and host address ranges.
start( mmap: &MemoryMapping, gpu_guest_address: u64, guest_mem: GuestMemory, ) -> RenderNodeHost31     pub fn start(
32         mmap: &MemoryMapping,
33         gpu_guest_address: u64,
34         guest_mem: GuestMemory,
35     ) -> RenderNodeHost {
36         // Render node forward library need to do address translation between host user space
37         // address and guest physical address. We could call Rust function from C library. But
38         // since it's actually a linear mapping now, we just pass the host start address to
39         // render node forward library. We need two start address here since there would be a
40         // hole below 4G if guest memory size is bigger than 4G.
41 
42         let host_start_addr = guest_mem.get_host_address(GuestAddress(0)).unwrap();
43         let host_4g_addr = if guest_mem.memory_size() > FOUR_GB {
44             guest_mem.get_host_address(GuestAddress(FOUR_GB)).unwrap()
45         } else {
46             host_start_addr
47         };
48         // Safe because only valid addresses are given.
49         unsafe {
50             start_render_node_host(
51                 mmap.as_ptr(),
52                 gpu_guest_address,
53                 mmap.size() as u64,
54                 host_start_addr,
55                 host_4g_addr,
56             )
57         }
58         RenderNodeHost { guest_mem }
59     }
60 }
61