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 | #include "tok.h" |
---|
15 | #include "page.h" |
---|
16 | |
---|
17 | unsigned long mmPage_tab[MAX_PAGE_TAB]; |
---|
18 | char mmUse_tab [MAX_PAGE_TAB]; |
---|
19 | int mmPage_tab_ind=0; |
---|
20 | int mmPage_tab_acc=0; |
---|
21 | static caddr_t startAddr = (caddr_t) 0; |
---|
22 | |
---|
23 | #ifndef PROT_NONE |
---|
24 | #define PROT_NONE 0 |
---|
25 | #endif |
---|
26 | |
---|
27 | #if ( !defined(MAP_ANONYMOUS) && defined(MAP_ANON) ) |
---|
28 | #define MAP_ANONYMOUS MAP_ANON |
---|
29 | #endif |
---|
30 | |
---|
31 | |
---|
32 | extern int sys_nerr; |
---|
33 | extern char * sys_errlist[]; |
---|
34 | |
---|
35 | static const char * mmStringErrorReport(void) |
---|
36 | { |
---|
37 | if ( errno > 0 && errno < sys_nerr ) |
---|
38 | return sys_errlist[errno]; |
---|
39 | else |
---|
40 | return "Unknown error.\n"; |
---|
41 | } |
---|
42 | |
---|
43 | /* |
---|
44 | * Create memory. |
---|
45 | */ |
---|
46 | #if defined(MAP_ANONYMOUS) |
---|
47 | void * |
---|
48 | mmPage_Create(size_t size) |
---|
49 | { |
---|
50 | caddr_t allocation; |
---|
51 | |
---|
52 | /* |
---|
53 | * In this version, "startAddr" is a _hint_, not a demand. |
---|
54 | * When the memory I map here is contiguous with other |
---|
55 | * mappings, the allocator can coalesce the memory from two |
---|
56 | * or more mappings into one large contiguous chunk, and thus |
---|
57 | * might be able to find a fit that would not otherwise have |
---|
58 | * been possible. I could _force_ it to be contiguous by using |
---|
59 | * the MMAP_FIXED flag, but I don't want to stomp on memory mappings |
---|
60 | * generated by other software, etc. |
---|
61 | */ |
---|
62 | allocation = (caddr_t) mmap( |
---|
63 | startAddr |
---|
64 | ,(int)size |
---|
65 | ,PROT_READ|PROT_WRITE |
---|
66 | ,MAP_PRIVATE|MAP_ANONYMOUS |
---|
67 | ,-1 |
---|
68 | ,0); |
---|
69 | |
---|
70 | /* |
---|
71 | * Set the "address hint" for the next mmap() so that it will abut |
---|
72 | * the mapping we just created. |
---|
73 | * |
---|
74 | * HP/UX 9.01 has a kernel bug that makes mmap() fail sometimes |
---|
75 | * when given a non-zero address hint, so we'll leave the hint set |
---|
76 | * to zero on that system. HP recently told me this is now fixed. |
---|
77 | * Someone please tell me when it is probable to assume that most |
---|
78 | * of those systems that were running 9.01 have been upgraded. |
---|
79 | */ |
---|
80 | startAddr = allocation + size; |
---|
81 | |
---|
82 | if ( allocation == (caddr_t)-1 ) |
---|
83 | printf("mmap() failed: %s", mmStringErrorReport()); |
---|
84 | |
---|
85 | if (mmPage_tab_ind<MAX_PAGE_TAB) |
---|
86 | { |
---|
87 | mmPage_tab[mmPage_tab_ind]=(long)allocation; |
---|
88 | if (mmPage_tab_ind==0) |
---|
89 | { |
---|
90 | memset(mmUse_tab,'0',MAX_PAGE_TAB); |
---|
91 | } |
---|
92 | mmPage_tab_ind++; |
---|
93 | } |
---|
94 | return (void *)allocation; |
---|
95 | } |
---|
96 | #else |
---|
97 | void * mmPage_Create(size_t size) |
---|
98 | { |
---|
99 | static int devZeroFd = -1; |
---|
100 | caddr_t allocation; |
---|
101 | |
---|
102 | if ( devZeroFd == -1 ) |
---|
103 | { |
---|
104 | devZeroFd = open("/dev/zero", O_RDWR); |
---|
105 | if ( devZeroFd < 0 ) |
---|
106 | printf( "open() on /dev/zero failed: %s",mmStringErrorReport()); |
---|
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", mmStringErrorReport()); |
---|
131 | |
---|
132 | if (mmPage_tab_ind<MAX_PAGE_TAB) |
---|
133 | { |
---|
134 | mmPage_tab[mmPage_tab_ind]=allocation; |
---|
135 | if (mmPage_tab_ind==0) |
---|
136 | { |
---|
137 | memset(mmUse_tab,'0',MAX_PAGE_TAB); |
---|
138 | } |
---|
139 | mmPage_tab_ind++; |
---|
140 | } |
---|
141 | return (void *)allocation; |
---|
142 | } |
---|
143 | #endif |
---|
144 | |
---|
145 | void mmPage_AllowAccess(void * address) |
---|
146 | { |
---|
147 | if ( mprotect((caddr_t)address, 4096, PROT_READ|PROT_WRITE) < 0 ) |
---|
148 | printf("mprotect(READ|WRITE) failed: %s", mmStringErrorReport()); |
---|
149 | } |
---|
150 | |
---|
151 | void mmPage_DenyAccess(void * address) |
---|
152 | { |
---|
153 | if ( mprotect((caddr_t)address, 4096, PROT_NONE) < 0 ) |
---|
154 | printf("mprotect(NONE) failed: %s", mmStringErrorReport()); |
---|
155 | } |
---|
156 | |
---|
157 | void mmPage_Delete(void * address) |
---|
158 | { |
---|
159 | if ( munmap((caddr_t)address, 4096) < 0 ) |
---|
160 | mmPage_DenyAccess(address); |
---|
161 | } |
---|
162 | |
---|
163 | FILE *mmStatFile=NULL; |
---|
164 | unsigned mmStatLines=0; |
---|
165 | void mmWriteStat() |
---|
166 | { |
---|
167 | int i,l,start; |
---|
168 | if (mmStatFile==NULL) |
---|
169 | { |
---|
170 | mmStatFile=fopen(MM_STAT_FILE,"w"); |
---|
171 | #if 0 |
---|
172 | fprintf(mmStatFile,"P1\n%d ???\n",MAX_PAGE_TAB); |
---|
173 | #endif |
---|
174 | } |
---|
175 | if ((mmUse_tab[MAX_PAGE_TAB-1]=='0') |
---|
176 | || (mmUse_tab[MAX_PAGE_TAB-1]=='1')) |
---|
177 | { |
---|
178 | mmStatLines++; |
---|
179 | l=MAX_PAGE_TAB; |
---|
180 | start=0; |
---|
181 | fprintf(mmStatFile,"%d\n",mmPage_tab_acc); |
---|
182 | /* fwrite("#\n", 2, 1, mmStatFile);*/ |
---|
183 | #if 0 |
---|
184 | fflush(mmStatFile); |
---|
185 | #endif |
---|
186 | /* fwrite("#\n", 2, 1, mmStatFile);*/ |
---|
187 | #if 0 |
---|
188 | do |
---|
189 | { |
---|
190 | i=min(70,l); |
---|
191 | fwrite(&(mmUse_tab[start]), i, 1, mmStatFile); |
---|
192 | fwrite("\n", 1, 1, mmStatFile); |
---|
193 | start+=i; |
---|
194 | l-=i; |
---|
195 | } while (l>0); |
---|
196 | #endif |
---|
197 | } |
---|
198 | memset(mmUse_tab,'0',MAX_PAGE_TAB); |
---|
199 | mmPage_tab_acc=0; |
---|
200 | } |
---|
201 | |
---|
202 | void mmEndStat() |
---|
203 | { |
---|
204 | if (mmStatFile!=NULL) |
---|
205 | { |
---|
206 | mmWriteStat(); |
---|
207 | #if 0 |
---|
208 | fprintf(mmStatFile,"# %d %d\n",MAX_PAGE_TAB,mmStatLines); |
---|
209 | #endif |
---|
210 | fclose(mmStatFile); |
---|
211 | } |
---|
212 | } |
---|
213 | #endif |
---|