1 // Copyright 2016 The Fuchsia Authors
2 //
3 // Use of this source code is governed by a MIT-style
4 // license that can be found in the LICENSE file or at
5 // https://opensource.org/licenses/MIT
6 
7 #pragma once
8 
9 #include <zircon/rights.h>
10 #include <zircon/types.h>
11 
12 #include <fbl/canary.h>
13 #include <object/dispatcher.h>
14 
15 #include <lib/user_copy/user_ptr.h>
16 #include <sys/types.h>
17 #include <vm/vm_object.h>
18 
19 class VmObjectDispatcher final : public SoloDispatcher<VmObjectDispatcher, ZX_DEFAULT_VMO_RIGHTS>,
20                                  public VmObjectChildObserver {
21 public:
22     static zx_status_t Create(fbl::RefPtr<VmObject> vmo,
23                               fbl::RefPtr<Dispatcher>* dispatcher,
24                               zx_rights_t* rights);
25     ~VmObjectDispatcher() final;
26 
27     // VmObjectChildObserver implementation.
28     void OnZeroChild() final;
29     void OnOneChild() final;
30 
31     // SoloDispatcher implementation.
get_type()32     zx_obj_type_t get_type() const final { return ZX_OBJ_TYPE_VMO; }
33     void get_name(char out_name[ZX_MAX_NAME_LEN]) const final;
34     zx_status_t set_name(const char* name, size_t len) final;
get_cookie_jar()35     CookieJar* get_cookie_jar() final { return &cookie_jar_; }
36 
37     // VmObjectDispatcher own methods.
38     zx_status_t Read(user_out_ptr<void> user_data, size_t length,
39                      uint64_t offset);
40     zx_status_t Write(user_in_ptr<const void> user_data, size_t length,
41                       uint64_t offset);
42     zx_status_t SetSize(uint64_t);
43     zx_status_t GetSize(uint64_t* size);
44     zx_status_t RangeOp(uint32_t op, uint64_t offset, uint64_t size, user_inout_ptr<void> buffer,
45                         size_t buffer_size, zx_rights_t rights);
46     zx_status_t Clone(
47         uint32_t options, uint64_t offset, uint64_t size, bool copy_name,
48         fbl::RefPtr<VmObject>* clone_vmo);
49 
50     zx_status_t SetMappingCachePolicy(uint32_t cache_policy);
51 
52     zx_info_vmo_t GetVmoInfo();
53 
vmo()54     const fbl::RefPtr<VmObject>& vmo() const { return vmo_; }
55 
56 private:
57     explicit VmObjectDispatcher(fbl::RefPtr<VmObject> vmo);
58 
59     fbl::Canary<fbl::magic("VMOD")> canary_;
60 
61     // The 'const' here is load bearing; we give a raw pointer to
62     // ourselves to |vmo_| so we have to ensure we don't reset vmo_
63     // except during destruction.
64     fbl::RefPtr<VmObject> const vmo_;
65 
66     // VMOs do not currently maintain any VMO-specific signal state,
67     // but do allow user signals to be set. In addition, the CookieJar
68     // shares the same lock.
69     CookieJar cookie_jar_;
70 };
71 
72 zx_info_vmo_t VmoToInfoEntry(const VmObject* vmo,
73                              bool is_handle, zx_rights_t handle_rights);
74