blob: 274dec2e26a0dfeba1829eb2af666b176b530ecc [file] [log] [blame]
John Stultz16100f62017-05-03 11:12:18 -07001/*
John Stultz18814f62018-02-22 16:02:49 -08002 * Copyright (C) 2014-2017 ARM Limited. All rights reserved.
John Stultz16100f62017-05-03 11:12:18 -07003 *
4 * Copyright (C) 2008 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#include <cutils/ashmem.h>
20#include <cutils/log.h>
21#include <sys/mman.h>
John Stultz16100f62017-05-03 11:12:18 -070022
John Stultz18814f62018-02-22 16:02:49 -080023#if GRALLOC_USE_GRALLOC1_API == 1
24#include <hardware/gralloc1.h>
25#else
26#include <hardware/gralloc.h>
27#endif
28
29#include "mali_gralloc_module.h"
30#include "mali_gralloc_private_interface_types.h"
31#include "mali_gralloc_buffer.h"
32#include "gralloc_buffer_priv.h"
John Stultz16100f62017-05-03 11:12:18 -070033
34/*
35 * Allocate shared memory for attribute storage. Only to be
36 * used by gralloc internally.
37 *
38 * Return 0 on success.
39 */
John Stultz18814f62018-02-22 16:02:49 -080040int gralloc_buffer_attr_allocate(private_handle_t *hnd)
John Stultz16100f62017-05-03 11:12:18 -070041{
42 int rval = -1;
43
John Stultz18814f62018-02-22 16:02:49 -080044 if (!hnd)
John Stultz16100f62017-05-03 11:12:18 -070045 {
John Stultz18814f62018-02-22 16:02:49 -080046 goto out;
John Stultz16100f62017-05-03 11:12:18 -070047 }
48
John Stultz18814f62018-02-22 16:02:49 -080049 if (hnd->share_attr_fd >= 0)
50 {
51 ALOGW("Warning share attribute fd already exists during create. Closing.");
52 close(hnd->share_attr_fd);
53 }
54
55 hnd->share_attr_fd = ashmem_create_region("gralloc_shared_attr", PAGE_SIZE);
56
57 if (hnd->share_attr_fd < 0)
John Stultz16100f62017-05-03 11:12:18 -070058 {
59 ALOGE("Failed to allocate page for shared attribute region");
60 goto err_ashmem;
61 }
62
63 /*
64 * Default protection on the shm region is PROT_EXEC | PROT_READ | PROT_WRITE.
65 *
66 * Personality flag READ_IMPLIES_EXEC which is used by some processes, namely gdbserver,
67 * causes a mmap with PROT_READ to be translated to PROT_READ | PROT_EXEC.
68 *
69 * If we were to drop PROT_EXEC here with a call to ashmem_set_prot_region()
70 * this can potentially cause clients to fail importing this gralloc attribute buffer
71 * with EPERM error since PROT_EXEC is not allowed.
72 *
73 * Because of this we keep the PROT_EXEC flag.
74 */
75
John Stultz18814f62018-02-22 16:02:49 -080076 hnd->attr_base = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, hnd->share_attr_fd, 0);
77
78 if (hnd->attr_base != MAP_FAILED)
John Stultz16100f62017-05-03 11:12:18 -070079 {
80 /* The attribute region contains signed integers only.
81 * The reason for this is because we can set a value less than 0 for
82 * not-initialized values.
83 */
John Stultz18814f62018-02-22 16:02:49 -080084 attr_region *region = (attr_region *)hnd->attr_base;
John Stultz16100f62017-05-03 11:12:18 -070085
86 memset(hnd->attr_base, 0xff, PAGE_SIZE);
John Stultz18814f62018-02-22 16:02:49 -080087 munmap(hnd->attr_base, PAGE_SIZE);
John Stultz16100f62017-05-03 11:12:18 -070088 hnd->attr_base = MAP_FAILED;
89 }
90 else
91 {
92 ALOGE("Failed to mmap shared attribute region");
93 goto err_ashmem;
94 }
95
96 rval = 0;
97 goto out;
98
99err_ashmem:
John Stultz18814f62018-02-22 16:02:49 -0800100
101 if (hnd->share_attr_fd >= 0)
John Stultz16100f62017-05-03 11:12:18 -0700102 {
John Stultz18814f62018-02-22 16:02:49 -0800103 close(hnd->share_attr_fd);
John Stultz16100f62017-05-03 11:12:18 -0700104 hnd->share_attr_fd = -1;
105 }
106
107out:
108 return rval;
109}
110
111/*
112 * Frees the shared memory allocated for attribute storage.
113 * Only to be used by gralloc internally.
114
115 * Return 0 on success.
116 */
John Stultz18814f62018-02-22 16:02:49 -0800117int gralloc_buffer_attr_free(private_handle_t *hnd)
John Stultz16100f62017-05-03 11:12:18 -0700118{
119 int rval = -1;
120
John Stultz18814f62018-02-22 16:02:49 -0800121 if (!hnd)
122 {
John Stultz16100f62017-05-03 11:12:18 -0700123 goto out;
John Stultz18814f62018-02-22 16:02:49 -0800124 }
John Stultz16100f62017-05-03 11:12:18 -0700125
John Stultz18814f62018-02-22 16:02:49 -0800126 if (hnd->share_attr_fd < 0)
John Stultz16100f62017-05-03 11:12:18 -0700127 {
128 ALOGE("Shared attribute region not avail to free");
129 goto out;
130 }
John Stultz18814f62018-02-22 16:02:49 -0800131
132 if (hnd->attr_base != MAP_FAILED)
John Stultz16100f62017-05-03 11:12:18 -0700133 {
134 ALOGW("Warning shared attribute region mapped at free. Unmapping");
John Stultz18814f62018-02-22 16:02:49 -0800135 munmap(hnd->attr_base, PAGE_SIZE);
John Stultz16100f62017-05-03 11:12:18 -0700136 hnd->attr_base = MAP_FAILED;
137 }
138
John Stultz18814f62018-02-22 16:02:49 -0800139 close(hnd->share_attr_fd);
John Stultz16100f62017-05-03 11:12:18 -0700140 hnd->share_attr_fd = -1;
141 rval = 0;
142
143out:
144 return rval;
145}