1570af302Sopenharmony_ci#include <unistd.h>
2570af302Sopenharmony_ci#include <sys/mman.h>
3570af302Sopenharmony_ci#include <errno.h>
4570af302Sopenharmony_ci#include <stdint.h>
5570af302Sopenharmony_ci#include <limits.h>
6570af302Sopenharmony_ci#include "syscall.h"
7570af302Sopenharmony_ci
8570af302Sopenharmony_cistatic void dummy(void) { }
9570af302Sopenharmony_ciweak_alias(dummy, __vm_wait);
10570af302Sopenharmony_ci
11570af302Sopenharmony_ci#define UNIT SYSCALL_MMAP2_UNIT
12570af302Sopenharmony_ci#define OFF_MASK ((-0x2000ULL << (8*sizeof(syscall_arg_t)-1)) | (UNIT-1))
13570af302Sopenharmony_ci
14570af302Sopenharmony_civoid *__mmap(void *start, size_t len, int prot, int flags, int fd, off_t off)
15570af302Sopenharmony_ci{
16570af302Sopenharmony_ci	long ret;
17570af302Sopenharmony_ci	if (off & OFF_MASK) {
18570af302Sopenharmony_ci		errno = EINVAL;
19570af302Sopenharmony_ci		return MAP_FAILED;
20570af302Sopenharmony_ci	}
21570af302Sopenharmony_ci	if (len >= PTRDIFF_MAX) {
22570af302Sopenharmony_ci		errno = ENOMEM;
23570af302Sopenharmony_ci		return MAP_FAILED;
24570af302Sopenharmony_ci	}
25570af302Sopenharmony_ci	if (len == 0) {
26570af302Sopenharmony_ci                errno = EINVAL;
27570af302Sopenharmony_ci                return MAP_FAILED;
28570af302Sopenharmony_ci	}
29570af302Sopenharmony_ci	if (flags & MAP_FIXED) {
30570af302Sopenharmony_ci		__vm_wait();
31570af302Sopenharmony_ci	}
32570af302Sopenharmony_ci#ifdef SYS_mmap2
33570af302Sopenharmony_ci	ret = __syscall(SYS_mmap2, start, len, prot, flags, fd, off/UNIT);
34570af302Sopenharmony_ci#else
35570af302Sopenharmony_ci	ret = __syscall(SYS_mmap, start, len, prot, flags, fd, off);
36570af302Sopenharmony_ci#endif
37570af302Sopenharmony_ci	/* Fixup incorrect EPERM from kernel. */
38570af302Sopenharmony_ci	if (ret == -EPERM && !start && (flags&MAP_ANON) && !(flags&MAP_FIXED))
39570af302Sopenharmony_ci		ret = -ENOMEM;
40570af302Sopenharmony_ci	return (void *)__syscall_ret(ret);
41570af302Sopenharmony_ci}
42570af302Sopenharmony_ci
43570af302Sopenharmony_ci
44570af302Sopenharmony_ci#ifdef HOOK_ENABLE
45570af302Sopenharmony_civoid* __libc_mmap(void*, size_t, int, int, int, off_t);
46570af302Sopenharmony_ciweak_alias(__mmap, __libc_mmap);
47570af302Sopenharmony_ciweak_alias(__libc_mmap, mmap64);
48570af302Sopenharmony_ci#else
49570af302Sopenharmony_ciweak_alias(__mmap, mmap);
50570af302Sopenharmony_ciweak_alias(mmap, mmap64);
51570af302Sopenharmony_ci#endif // HOOK_ENABLE
52570af302Sopenharmony_ci
53570af302Sopenharmony_ci/*
54570af302Sopenharmony_ciAdapter function for lldb to not deal with 64-bit arguments on 32-bit systems
55570af302Sopenharmony_ciIssue I661Z1
56570af302Sopenharmony_ciTest: Build and boot devices.
57570af302Sopenharmony_ci*/
58570af302Sopenharmony_civoid *__lldb_mmap(void *start, size_t len, int prot, int flags, int fd, intptr_t off) {
59570af302Sopenharmony_ci	return mmap(start, len, prot, flags, fd, off);
60570af302Sopenharmony_ci}
61