1 | #include "mod2.h" |
---|
2 | #ifdef PAGE_TEST |
---|
3 | #include <stdlib.h> |
---|
4 | #include <unistd.h> |
---|
5 | #include <fcntl.h> |
---|
6 | #include <sys/mman.h> |
---|
7 | #include <stdio.h> |
---|
8 | #include <errno.h> |
---|
9 | #include <string.h> |
---|
10 | #include <stddef.h> |
---|
11 | #include <signal.h> |
---|
12 | #include <sys/types.h> |
---|
13 | #include <sys/times.h> |
---|
14 | |
---|
15 | unsigned long page_tab[2048]; |
---|
16 | char use_tab [2048]; |
---|
17 | int page_tab_ind=2049; |
---|
18 | void * |
---|
19 | Page_Create(size_t size); |
---|
20 | void |
---|
21 | Page_AllowAccess(void * address, size_t size); |
---|
22 | void |
---|
23 | Page_DenyAccess(void * address, size_t size); |
---|
24 | size_t |
---|
25 | Page_Size(void); |
---|
26 | #ifndef PROT_NONE |
---|
27 | #define PROT_NONE 0 |
---|
28 | #endif |
---|
29 | |
---|
30 | #if ( !defined(MAP_ANONYMOUS) && defined(MAP_ANON) ) |
---|
31 | #define MAP_ANONYMOUS MAP_ANON |
---|
32 | #endif |
---|
33 | |
---|
34 | static caddr_t startAddr = (caddr_t) 0; |
---|
35 | |
---|
36 | extern int sys_nerr; |
---|
37 | extern char * sys_errlist[]; |
---|
38 | |
---|
39 | static const char * |
---|
40 | stringErrorReport(void) |
---|
41 | { |
---|
42 | if ( errno > 0 && errno < sys_nerr ) |
---|
43 | return sys_errlist[errno]; |
---|
44 | else |
---|
45 | return "Unknown error.\n"; |
---|
46 | } |
---|
47 | |
---|
48 | /* |
---|
49 | * Create memory. |
---|
50 | */ |
---|
51 | #if defined(MAP_ANONYMOUS) |
---|
52 | void * |
---|
53 | Page_Create(size_t size) |
---|
54 | { |
---|
55 | caddr_t allocation; |
---|
56 | |
---|
57 | /* |
---|
58 | * In this version, "startAddr" is a _hint_, not a demand. |
---|
59 | * When the memory I map here is contiguous with other |
---|
60 | * mappings, the allocator can coalesce the memory from two |
---|
61 | * or more mappings into one large contiguous chunk, and thus |
---|
62 | * might be able to find a fit that would not otherwise have |
---|
63 | * been possible. I could _force_ it to be contiguous by using |
---|
64 | * the MMAP_FIXED flag, but I don't want to stomp on memory mappings |
---|
65 | * generated by other software, etc. |
---|
66 | */ |
---|
67 | allocation = (caddr_t) mmap( |
---|
68 | startAddr |
---|
69 | ,(int)size |
---|
70 | ,PROT_READ|PROT_WRITE |
---|
71 | ,MAP_PRIVATE|MAP_ANONYMOUS |
---|
72 | ,-1 |
---|
73 | ,0); |
---|
74 | |
---|
75 | /* |
---|
76 | * Set the "address hint" for the next mmap() so that it will abut |
---|
77 | * the mapping we just created. |
---|
78 | * |
---|
79 | * HP/UX 9.01 has a kernel bug that makes mmap() fail sometimes |
---|
80 | * when given a non-zero address hint, so we'll leave the hint set |
---|
81 | * to zero on that system. HP recently told me this is now fixed. |
---|
82 | * Someone please tell me when it is probable to assume that most |
---|
83 | * of those systems that were running 9.01 have been upgraded. |
---|
84 | */ |
---|
85 | startAddr = allocation + size; |
---|
86 | |
---|
87 | if ( allocation == (caddr_t)-1 ) |
---|
88 | printf("mmap() failed: %s", stringErrorReport()); |
---|
89 | |
---|
90 | page_tab[page_tab_ind]=allocation; |
---|
91 | page_tab_ind++; |
---|
92 | return (void *)allocation; |
---|
93 | } |
---|
94 | #else |
---|
95 | void * |
---|
96 | Page_Create(size_t size) |
---|
97 | { |
---|
98 | static int devZeroFd = -1; |
---|
99 | caddr_t allocation; |
---|
100 | |
---|
101 | if ( devZeroFd == -1 ) { |
---|
102 | devZeroFd = open("/dev/zero", O_RDWR); |
---|
103 | if ( devZeroFd < 0 ) |
---|
104 | printf( |
---|
105 | "open() on /dev/zero failed: %s" |
---|
106 | ,stringErrorReport()); |
---|
107 | } |
---|
108 | |
---|
109 | /* |
---|
110 | * In this version, "startAddr" is a _hint_, not a demand. |
---|
111 | * When the memory I map here is contiguous with other |
---|
112 | * mappings, the allocator can coalesce the memory from two |
---|
113 | * or more mappings into one large contiguous chunk, and thus |
---|
114 | * might be able to find a fit that would not otherwise have |
---|
115 | * been possible. I could _force_ it to be contiguous by using |
---|
116 | * the MMAP_FIXED flag, but I don't want to stomp on memory mappings |
---|
117 | * generated by other software, etc. |
---|
118 | */ |
---|
119 | allocation = (caddr_t) mmap( |
---|
120 | startAddr |
---|
121 | ,(int)size |
---|
122 | ,PROT_READ|PROT_WRITE |
---|
123 | ,MAP_PRIVATE |
---|
124 | ,devZeroFd |
---|
125 | ,0); |
---|
126 | |
---|
127 | startAddr = allocation + size; |
---|
128 | |
---|
129 | if ( allocation == (caddr_t)-1 ) |
---|
130 | printf("mmap() failed: %s", stringErrorReport()); |
---|
131 | |
---|
132 | page_tab[page_tab_ind]=allocation; |
---|
133 | page_tab_ind++; |
---|
134 | return (void *)allocation; |
---|
135 | } |
---|
136 | #endif |
---|
137 | |
---|
138 | static void |
---|
139 | mprotectFailed(void) |
---|
140 | { |
---|
141 | printf("mprotect() failed: %s", stringErrorReport()); |
---|
142 | } |
---|
143 | |
---|
144 | void |
---|
145 | Page_AllowAccess(void * address, size_t size) |
---|
146 | { |
---|
147 | if ( mprotect((caddr_t)address, size, PROT_READ|PROT_WRITE) < 0 ) |
---|
148 | mprotectFailed(); |
---|
149 | } |
---|
150 | |
---|
151 | void |
---|
152 | Page_DenyAccess(void * address, size_t size) |
---|
153 | { |
---|
154 | if ( mprotect((caddr_t)address, size, PROT_NONE) < 0 ) |
---|
155 | mprotectFailed(); |
---|
156 | } |
---|
157 | |
---|
158 | void |
---|
159 | Page_Delete(void * address, size_t size) |
---|
160 | { |
---|
161 | if ( munmap((caddr_t)address, size) < 0 ) |
---|
162 | Page_DenyAccess(address, size); |
---|
163 | } |
---|
164 | |
---|
165 | #if defined(_SC_PAGESIZE) |
---|
166 | size_t |
---|
167 | Page_Size(void) |
---|
168 | { |
---|
169 | return (size_t)sysconf(_SC_PAGESIZE); |
---|
170 | } |
---|
171 | #elif defined(_SC_PAGE_SIZE) |
---|
172 | size_t |
---|
173 | Page_Size(void) |
---|
174 | { |
---|
175 | return (size_t)sysconf(_SC_PAGE_SIZE); |
---|
176 | } |
---|
177 | #else |
---|
178 | /* extern int getpagesize(); */ |
---|
179 | size_t |
---|
180 | Page_Size(void) |
---|
181 | { |
---|
182 | return getpagesize(); |
---|
183 | } |
---|
184 | #endif |
---|
185 | |
---|
186 | #endif |
---|