1// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "MemFd.hpp"
16#include "../Debug.hpp"
17
18#include <errno.h>
19#include <fcntl.h>
20#include <string.h>
21#include <sys/mman.h>
22#include <unistd.h>
23
24#ifndef MFD_CLOEXEC
25#define MFD_CLOEXEC 0x0001U
26#endif
27
28#if __aarch64__
29#define __NR_memfd_create 279
30#elif __arm__
31#define __NR_memfd_create 279
32#elif __powerpc64__
33#define __NR_memfd_create 360
34#elif __i386__
35#define __NR_memfd_create 356
36#elif __x86_64__
37#define __NR_memfd_create 319
38#endif /* __NR_memfd_create__ */
39
40LinuxMemFd::~LinuxMemFd()
41{
42 close();
43}
44
45void LinuxMemFd::importFd(int fd)
46{
47 close();
48 fd_ = fd;
49}
50
51int LinuxMemFd::exportFd() const
52{
53 if (fd_ < 0)
54 {
55 return fd_;
56 }
57
58 // Duplicate file descriptor while setting the clo-on-exec flag (Linux specific).
59 return ::fcntl(fd_, F_DUPFD_CLOEXEC, 0);
60}
61
62bool LinuxMemFd::allocate(const char* name, size_t size)
63{
64 close();
65
66#ifndef __NR_memfd_create
67 TRACE("memfd_create() not supported on this system!");
68 return false;
69#else
70 // In the event of no system call this returns -1 with errno set
71 // as ENOSYS.
72 fd_ = syscall(__NR_memfd_create, name, MFD_CLOEXEC);
73 if (fd_ < 0)
74 {
75 TRACE("memfd_create() returned %d: %s", errno, strerror(errno));
76 return false;
77 }
78 // Ensure there is enough space.
79 if (size > 0 && ::ftruncate(fd_, size) < 0)
80 {
81 TRACE("ftruncate() %lld returned %d: %s", (long long)size, errno, strerror(errno));
82 close();
83 return false;
84 }
85#endif
86 return true;
87}
88
89void LinuxMemFd::close()
90{
91 if (fd_ >= 0)
92 {
93 // WARNING: Never retry on close() failure, even with EINTR, see
94 // https://lwn.net/Articles/576478/ for example.
95 int ret = ::close(fd_);
96 if (ret < 0) {
97 TRACE("LinuxMemFd::close() failed with: %s", strerror(errno));
98 assert(false);
99 }
100 fd_ = -1;
101 }
102}
103
104void* LinuxMemFd::mapReadWrite(size_t offset, size_t size)
105{
106 void* addr = ::mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd_,
107 static_cast<off_t>(offset));
108 return (addr == MAP_FAILED) ? nullptr : addr;
109}
110
111bool LinuxMemFd::unmap(void* addr, size_t size)
112{
113 return ::munmap(addr, size) == 0;
114}
115