1e9297d28Sopenharmony_ci/*
2e9297d28Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3e9297d28Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e9297d28Sopenharmony_ci * you may not use this file except in compliance with the License.
5e9297d28Sopenharmony_ci * You may obtain a copy of the License at
6e9297d28Sopenharmony_ci *
7e9297d28Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e9297d28Sopenharmony_ci *
9e9297d28Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e9297d28Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e9297d28Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e9297d28Sopenharmony_ci * See the License for the specific language governing permissions and
13e9297d28Sopenharmony_ci * limitations under the License.
14e9297d28Sopenharmony_ci */
15e9297d28Sopenharmony_ci
16e9297d28Sopenharmony_ci /**
17e9297d28Sopenharmony_ci * @file ashmem.h
18e9297d28Sopenharmony_ci *
19e9297d28Sopenharmony_ci * @brief Provide class Ashmem implemented in c_utils to operate the
20e9297d28Sopenharmony_ci * Anonymous Shared Memory(Ashmem).
21e9297d28Sopenharmony_ci */
22e9297d28Sopenharmony_ci
23e9297d28Sopenharmony_ci#ifndef UTILS_BASE_ASHMEM_H
24e9297d28Sopenharmony_ci#define UTILS_BASE_ASHMEM_H
25e9297d28Sopenharmony_ci
26e9297d28Sopenharmony_ci#include <cstddef>
27e9297d28Sopenharmony_ci#include <linux/ashmem.h>
28e9297d28Sopenharmony_ci#include "refbase.h"
29e9297d28Sopenharmony_ci#include "parcel.h"
30e9297d28Sopenharmony_ci
31e9297d28Sopenharmony_cinamespace OHOS {
32e9297d28Sopenharmony_ci/**
33e9297d28Sopenharmony_ci * @brief Create ashmem in kernel with specified name and size.
34e9297d28Sopenharmony_ci *
35e9297d28Sopenharmony_ci * @param name Name for identifying the ashmem in kernel.
36e9297d28Sopenharmony_ci * @param size Size of the ashmem region.
37e9297d28Sopenharmony_ci * @return File descriptor of the ashmem.
38e9297d28Sopenharmony_ci */
39e9297d28Sopenharmony_ciint AshmemCreate(const char *name, size_t size);
40e9297d28Sopenharmony_ci
41e9297d28Sopenharmony_ci/**
42e9297d28Sopenharmony_ci * @brief Set protection flag of specified ashmem region in kernel.
43e9297d28Sopenharmony_ci
44e9297d28Sopenharmony_ci * @param fd Corresponding file descriptor.
45e9297d28Sopenharmony_ci * @param prot Value of protection flag.
46e9297d28Sopenharmony_ci * @return Return 0 on success, -1 on failure.
47e9297d28Sopenharmony_ci */
48e9297d28Sopenharmony_ciint AshmemSetProt(int fd, int prot);
49e9297d28Sopenharmony_ci
50e9297d28Sopenharmony_ci/**
51e9297d28Sopenharmony_ci * @brief Get size of specified ashmem region in kernel.
52e9297d28Sopenharmony_ci *
53e9297d28Sopenharmony_ci * @param fd Corresponding file descriptor.
54e9297d28Sopenharmony_ci * @return Size of ashmem.
55e9297d28Sopenharmony_ci */
56e9297d28Sopenharmony_ciint AshmemGetSize(int fd);
57e9297d28Sopenharmony_ci
58e9297d28Sopenharmony_ci/**
59e9297d28Sopenharmony_ci * @brief Ashmem implemented in c_utils, to operate the
60e9297d28Sopenharmony_ci * Anonymous Shared Memory(Ashmem).
61e9297d28Sopenharmony_ci *
62e9297d28Sopenharmony_ci * Including create, map an ashmem region and thus write/read to/from it, etc.
63e9297d28Sopenharmony_ci *
64e9297d28Sopenharmony_ci * @note Ashmem object should be unmapped and closed manually, though managed
65e9297d28Sopenharmony_ci * by smart pointer.
66e9297d28Sopenharmony_ci */
67e9297d28Sopenharmony_ciclass Ashmem : public virtual RefBase {
68e9297d28Sopenharmony_cipublic:
69e9297d28Sopenharmony_ci    /**
70e9297d28Sopenharmony_ci     * @brief Create an Ashmem object with specified name and region size.
71e9297d28Sopenharmony_ci     *
72e9297d28Sopenharmony_ci     * File '/dev/ashmem' will be opened whose file descriptor will be held by
73e9297d28Sopenharmony_ci     * this Ashmem object.
74e9297d28Sopenharmony_ci     *
75e9297d28Sopenharmony_ci     * @param name Name for identifying the ashmem in kernel.
76e9297d28Sopenharmony_ci     * @param size Size of the ashmem region.
77e9297d28Sopenharmony_ci     * @return An Ashmem object referenced by a smart pointer.
78e9297d28Sopenharmony_ci     * @note Memory has not been mapped, use `MapAshmem()` before
79e9297d28Sopenharmony_ci     * writing/reading.
80e9297d28Sopenharmony_ci     */
81e9297d28Sopenharmony_ci    static sptr<Ashmem> CreateAshmem(const char *name, int32_t size);
82e9297d28Sopenharmony_ci
83e9297d28Sopenharmony_ci    /**
84e9297d28Sopenharmony_ci     * @brief Close the ashmem(i.e. close it via file descriptor).
85e9297d28Sopenharmony_ci     *
86e9297d28Sopenharmony_ci     * All inner parameters will be cleared.
87e9297d28Sopenharmony_ci     *
88e9297d28Sopenharmony_ci     * @note An ashmem  will be unmapped first by `UnmapAshmem()` before close.
89e9297d28Sopenharmony_ci     */
90e9297d28Sopenharmony_ci    void CloseAshmem();
91e9297d28Sopenharmony_ci
92e9297d28Sopenharmony_ci    /**
93e9297d28Sopenharmony_ci     * @brief Map the ashmem region in the kernel to user space.
94e9297d28Sopenharmony_ci     *
95e9297d28Sopenharmony_ci     * @param mapType Protection flag of the mapped region in user space.
96e9297d28Sopenharmony_ci     * @return True if mapping successful.
97e9297d28Sopenharmony_ci     */
98e9297d28Sopenharmony_ci    bool MapAshmem(int mapType);
99e9297d28Sopenharmony_ci
100e9297d28Sopenharmony_ci    /**
101e9297d28Sopenharmony_ci     * @brief Map the ashmem region in Read/Write mode.
102e9297d28Sopenharmony_ci     *
103e9297d28Sopenharmony_ci     * It calls `MapAshmem(PROT_READ | PROT_WRITE)`.
104e9297d28Sopenharmony_ci     *
105e9297d28Sopenharmony_ci     * @return True if mapping successful.
106e9297d28Sopenharmony_ci     */
107e9297d28Sopenharmony_ci    bool MapReadAndWriteAshmem();
108e9297d28Sopenharmony_ci
109e9297d28Sopenharmony_ci    /**
110e9297d28Sopenharmony_ci     * @brief Map the ashmem region in Read-Only mode.
111e9297d28Sopenharmony_ci     *
112e9297d28Sopenharmony_ci     * It calls `MapAshmem(PROT_READ)`.
113e9297d28Sopenharmony_ci     *
114e9297d28Sopenharmony_ci     * @return True if mapping successful.
115e9297d28Sopenharmony_ci     */
116e9297d28Sopenharmony_ci    bool MapReadOnlyAshmem();
117e9297d28Sopenharmony_ci
118e9297d28Sopenharmony_ci    /**
119e9297d28Sopenharmony_ci     * @brief Unmap the ashmem.
120e9297d28Sopenharmony_ci     *
121e9297d28Sopenharmony_ci     * Unmap when mapped ashmem exists, protection flag will be cleared
122e9297d28Sopenharmony_ci     * meanwhile.
123e9297d28Sopenharmony_ci     */
124e9297d28Sopenharmony_ci    void UnmapAshmem();
125e9297d28Sopenharmony_ci
126e9297d28Sopenharmony_ci    /**
127e9297d28Sopenharmony_ci     * @brief Set the protection flag of ashmem region in kernel.
128e9297d28Sopenharmony_ci     *
129e9297d28Sopenharmony_ci     * @param protectionType Value of protection flag.
130e9297d28Sopenharmony_ci     * @return True if set successful.
131e9297d28Sopenharmony_ci     */
132e9297d28Sopenharmony_ci    bool SetProtection(int protectionType);
133e9297d28Sopenharmony_ci
134e9297d28Sopenharmony_ci    /**
135e9297d28Sopenharmony_ci     * @brief Get the protection flag of ashmem region in kernel.
136e9297d28Sopenharmony_ci     *
137e9297d28Sopenharmony_ci     * @return Value of protection flag. Refer to linux manual.
138e9297d28Sopenharmony_ci     */
139e9297d28Sopenharmony_ci    int GetProtection();
140e9297d28Sopenharmony_ci
141e9297d28Sopenharmony_ci    /**
142e9297d28Sopenharmony_ci     * @brief Get the size of ashmem region in kernel.
143e9297d28Sopenharmony_ci     *
144e9297d28Sopenharmony_ci     * @return Value of size.
145e9297d28Sopenharmony_ci     */
146e9297d28Sopenharmony_ci    int32_t GetAshmemSize();
147e9297d28Sopenharmony_ci
148e9297d28Sopenharmony_ci    /**
149e9297d28Sopenharmony_ci     * @brief Write data to the `offset` position of ashmem region.
150e9297d28Sopenharmony_ci     *
151e9297d28Sopenharmony_ci     * Bounds and protection flag will be checked.
152e9297d28Sopenharmony_ci     *
153e9297d28Sopenharmony_ci     * @param data Pointer to input data.
154e9297d28Sopenharmony_ci     * @param size Size of the input data(bytes).
155e9297d28Sopenharmony_ci     * @param offset Offset from beginning of the region.
156e9297d28Sopenharmony_ci     * @return True if writting successful. False if data to be write overflows
157e9297d28Sopenharmony_ci     * or protection flag is illegal.
158e9297d28Sopenharmony_ci     * @note Require write authority of both ashmem in kernel and the mapped
159e9297d28Sopenharmony_ci     * one in user space.
160e9297d28Sopenharmony_ci     */
161e9297d28Sopenharmony_ci    bool WriteToAshmem(const void *data, int32_t size, int32_t offset);
162e9297d28Sopenharmony_ci
163e9297d28Sopenharmony_ci    /**
164e9297d28Sopenharmony_ci     * @brief Read data from the `offset` position of ashmem region.
165e9297d28Sopenharmony_ci     *
166e9297d28Sopenharmony_ci     * Bounds and protection flag will be checked.
167e9297d28Sopenharmony_ci     *
168e9297d28Sopenharmony_ci     * @param size Size of data to be read(bytes).
169e9297d28Sopenharmony_ci     * @param offset Offset from beginning of the region.
170e9297d28Sopenharmony_ci     * @return Void-type pointer to the data. `nullptr` returns if data to be
171e9297d28Sopenharmony_ci     * read overflows or protection flag is illegal.
172e9297d28Sopenharmony_ci     * @note Require read authority of both ashmem in kernel and the mapped one
173e9297d28Sopenharmony_ci     * in user space.
174e9297d28Sopenharmony_ci     */
175e9297d28Sopenharmony_ci    const void *ReadFromAshmem(int32_t size, int32_t offset);
176e9297d28Sopenharmony_ci
177e9297d28Sopenharmony_ci    /**
178e9297d28Sopenharmony_ci     * @brief Construct a new Ashmem object.
179e9297d28Sopenharmony_ci     *
180e9297d28Sopenharmony_ci     * @param fd File descriptor of an ashmem in kenrel.
181e9297d28Sopenharmony_ci     * @param size Size of the corresponding ashmem region in kernel.
182e9297d28Sopenharmony_ci     */
183e9297d28Sopenharmony_ci    Ashmem(int fd, int32_t size);
184e9297d28Sopenharmony_ci    ~Ashmem() override;
185e9297d28Sopenharmony_ci
186e9297d28Sopenharmony_ci    /**
187e9297d28Sopenharmony_ci     * @brief Get file descriptor of the corresponding ashmem in kernel.
188e9297d28Sopenharmony_ci     *
189e9297d28Sopenharmony_ci     * @return Corresponding file descriptor of this Ashmem object. It will be
190e9297d28Sopenharmony_ci     * 0 when ashmem is closed.
191e9297d28Sopenharmony_ci     */
192e9297d28Sopenharmony_ci    int GetAshmemFd() const
193e9297d28Sopenharmony_ci    {
194e9297d28Sopenharmony_ci        return memoryFd_;
195e9297d28Sopenharmony_ci    };
196e9297d28Sopenharmony_ci
197e9297d28Sopenharmony_ciprivate:
198e9297d28Sopenharmony_ci    int memoryFd_; // corresponding file descriptor of ashmem.
199e9297d28Sopenharmony_ci    int32_t memorySize_; // size of ashmem.
200e9297d28Sopenharmony_ci    int flag_; // protection flag of ashmem in user space.
201e9297d28Sopenharmony_ci    void *startAddr_; // start address of ashmem region.
202e9297d28Sopenharmony_ci    bool CheckValid(int32_t size, int32_t offset, int cmd);
203e9297d28Sopenharmony_ci};
204e9297d28Sopenharmony_ci} // namespace OHOS
205e9297d28Sopenharmony_ci#endif
206