1 /*
2 +----------------------------------------------------------------------+
3 | Zend OPcache |
4 +----------------------------------------------------------------------+
5 | Copyright (c) The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | https://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Andi Gutmans <andi@php.net> |
16 | Zeev Suraski <zeev@php.net> |
17 | Stanislav Malyshev <stas@zend.com> |
18 | Dmitry Stogov <dmitry@php.net> |
19 +----------------------------------------------------------------------+
20 */
21
22 #include "zend_shared_alloc.h"
23
24 #ifdef USE_MMAP
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/mman.h>
31
32 #ifdef __APPLE__
33 #include <mach/vm_statistics.h>
34 #endif
35
36 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
37 # define MAP_ANONYMOUS MAP_ANON
38 #endif
39 #if defined(MAP_ALIGNED_SUPER)
40 # define MAP_HUGETLB MAP_ALIGNED_SUPER
41 #endif
42
create_segments(size_t requested_size,zend_shared_segment *** shared_segments_p,int * shared_segments_count,char ** error_in)43 static int create_segments(size_t requested_size, zend_shared_segment ***shared_segments_p, int *shared_segments_count, char **error_in)
44 {
45 zend_shared_segment *shared_segment;
46 int flags = PROT_READ | PROT_WRITE, fd = -1;
47 void *p;
48 #ifdef PROT_MPROTECT
49 flags |= PROT_MPROTECT(PROT_EXEC);
50 #endif
51 #ifdef VM_MAKE_TAG
52 /* allows tracking segments via tools such as vmmap */
53 fd = VM_MAKE_TAG(251U);
54 #endif
55 #ifdef PROT_MAX
56 flags |= PROT_MAX(PROT_READ | PROT_WRITE | PROT_EXEC);
57 #endif
58 #ifdef MAP_HUGETLB
59 size_t huge_page_size = 2 * 1024 * 1024;
60
61 /* Try to allocate huge pages first to reduce dTLB misses.
62 * OSes has to be configured properly
63 * on Linux
64 * (e.g. https://wiki.debian.org/Hugepages#Enabling_HugeTlbPage)
65 * You may verify huge page usage with the following command:
66 * `grep "Huge" /proc/meminfo`
67 * on FreeBSD
68 * sysctl vm.pmap.pg_ps_enabled entry
69 * (boot time config only, but enabled by default on most arches).
70 */
71 if (requested_size >= huge_page_size && requested_size % huge_page_size == 0) {
72 # if defined(__x86_64__) && defined(MAP_32BIT)
73 /* to got HUGE PAGES in low 32-bit address we have to reserve address
74 space and then remap it using MAP_HUGETLB */
75
76 p = mmap(NULL, requested_size, flags, MAP_SHARED|MAP_ANONYMOUS|MAP_32BIT, fd, 0);
77 if (p != MAP_FAILED) {
78 munmap(p, requested_size);
79 p = (void*)(ZEND_MM_ALIGNED_SIZE_EX((ptrdiff_t)p, huge_page_size));
80 p = mmap(p, requested_size, flags, MAP_SHARED|MAP_ANONYMOUS|MAP_32BIT|MAP_HUGETLB|MAP_FIXED, -1, 0);
81 if (p != MAP_FAILED) {
82 goto success;
83 } else {
84 p = mmap(NULL, requested_size, flags, MAP_SHARED|MAP_ANONYMOUS|MAP_32BIT, fd, 0);
85 if (p != MAP_FAILED) {
86 goto success;
87 }
88 }
89 }
90 # endif
91 p = mmap(0, requested_size, flags, MAP_SHARED|MAP_ANONYMOUS|MAP_HUGETLB, fd, 0);
92 if (p != MAP_FAILED) {
93 goto success;
94 }
95 }
96 #elif defined(PREFER_MAP_32BIT) && defined(__x86_64__) && defined(MAP_32BIT)
97 p = mmap(NULL, requested_size, flags, MAP_SHARED|MAP_ANONYMOUS|MAP_32BIT, fd, 0);
98 if (p != MAP_FAILED) {
99 goto success;
100 }
101 #endif
102
103 p = mmap(0, requested_size, flags, MAP_SHARED|MAP_ANONYMOUS, fd, 0);
104 if (p == MAP_FAILED) {
105 *error_in = "mmap";
106 return ALLOC_FAILURE;
107 }
108
109 success: ZEND_ATTRIBUTE_UNUSED;
110 *shared_segments_count = 1;
111 *shared_segments_p = (zend_shared_segment **) calloc(1, sizeof(zend_shared_segment) + sizeof(void *));
112 if (!*shared_segments_p) {
113 munmap(p, requested_size);
114 *error_in = "calloc";
115 return ALLOC_FAILURE;
116 }
117 shared_segment = (zend_shared_segment *)((char *)(*shared_segments_p) + sizeof(void *));
118 (*shared_segments_p)[0] = shared_segment;
119
120 shared_segment->p = p;
121 shared_segment->pos = 0;
122 shared_segment->size = requested_size;
123
124 return ALLOC_SUCCESS;
125 }
126
detach_segment(zend_shared_segment * shared_segment)127 static int detach_segment(zend_shared_segment *shared_segment)
128 {
129 munmap(shared_segment->p, shared_segment->size);
130 return 0;
131 }
132
segment_type_size(void)133 static size_t segment_type_size(void)
134 {
135 return sizeof(zend_shared_segment);
136 }
137
138 zend_shared_memory_handlers zend_alloc_mmap_handlers = {
139 create_segments,
140 detach_segment,
141 segment_type_size
142 };
143
144 #endif /* USE_MMAP */
145