1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright (c) 2015 Joyent, Inc.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/types.h>
#include <sys/bitmap.h>
#include <sys/sysmacros.h>
#include <sys/kmem.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/user.h>
#include <sys/unistd.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/mman.h>
#include <sys/tuneable.h>
#include <sys/cmn_err.h>
#include <sys/cred.h>
#include <sys/vmsystm.h>
#include <sys/debug.h>
#include <sys/policy.h>
#include <vm/as.h>
#include <vm/seg.h>
static uint_t mem_getpgszc(size_t);
/*
* Memory control operations
*/
int
memcntl(caddr_t addr, size_t len, int cmd, caddr_t arg, int attr, int mask)
{
struct as *as = ttoproc(curthread)->p_as;
struct proc *p = ttoproc(curthread);
size_t pgsz;
uint_t szc, oszc, pgcmd;
int error = 0;
faultcode_t fc;
uintptr_t iarg;
STRUCT_DECL(memcntl_mha, mha);
if (mask)
return (set_errno(EINVAL));
if ((cmd == MC_LOCKAS) || (cmd == MC_UNLOCKAS)) {
if ((addr != 0) || (len != 0)) {
return (set_errno(EINVAL));
}
} else if (cmd != MC_HAT_ADVISE) {
if (((uintptr_t)addr & PAGEOFFSET) != 0 || len == 0) {
return (set_errno(EINVAL));
}
/*
* We're only concerned with the address range
* here, not the protections. The protections
* are only used as a "filter" in this code,
* they aren't set or modified here.
*/
if (valid_usr_range(addr, len, 0, as,
as->a_userlimit) != RANGE_OKAY) {
return (set_errno(ENOMEM));
}
}
if (cmd == MC_HAT_ADVISE) {
if (attr != 0 || mask != 0) {
return (set_errno(EINVAL));
}
} else {
if ((VALID_ATTR & attr) != attr) {
return (set_errno(EINVAL));
}
if ((attr & SHARED) && (attr & PRIVATE)) {
return (set_errno(EINVAL));
}
if (((cmd == MC_LOCKAS) || (cmd == MC_LOCK) ||
(cmd == MC_UNLOCKAS) || (cmd == MC_UNLOCK)) &&
(error = secpolicy_lock_memory(CRED())) != 0)
return (set_errno(error));
}
if (attr) {
attr |= PROT_USER;
}
oszc = 0;
switch (cmd) {
case MC_SYNC:
/*
* MS_SYNC used to be defined to be zero but is now non-zero.
* For binary compatibility we still accept zero
* (the absence of MS_ASYNC) to mean the same thing.
* Binary compatibility is not an issue for MS_INVALCURPROC.
*/
iarg = (uintptr_t)arg;
if ((iarg & ~MS_INVALIDATE) == 0)
iarg |= MS_SYNC;
if (((iarg &
~(MS_SYNC|MS_ASYNC|MS_INVALIDATE|MS_INVALCURPROC)) != 0) ||
((iarg & (MS_SYNC|MS_ASYNC)) == (MS_SYNC|MS_ASYNC)) ||
((iarg & (MS_INVALIDATE|MS_INVALCURPROC)) ==
(MS_INVALIDATE|MS_INVALCURPROC))) {
error = set_errno(EINVAL);
} else {
error = as_ctl(as, addr, len, cmd, attr, iarg, NULL, 0);
if (error) {
(void) set_errno(error);
}
}
return (error);
case MC_LOCKAS:
if ((uintptr_t)arg & ~(MCL_FUTURE|MCL_CURRENT) ||
(uintptr_t)arg == 0) {
return (set_errno(EINVAL));
}
break;
case MC_LOCK:
case MC_UNLOCKAS:
case MC_UNLOCK:
break;
case MC_HAT_ADVISE:
/*
* Set prefered page size.
*/
STRUCT_INIT(mha, get_udatamodel());
if (copyin(arg, STRUCT_BUF(mha), STRUCT_SIZE(mha))) {
return (set_errno(EFAULT));
}
pgcmd = STRUCT_FGET(mha, mha_cmd);
/*
* Currently only MHA_MAPSIZE_VA, MHA_MAPSIZE_STACK
* and MHA_MAPSIZE_BSSBRK are supported. Only one
* command may be specified at a time.
*/
if ((~(MHA_MAPSIZE_VA|MHA_MAPSIZE_STACK|MHA_MAPSIZE_BSSBRK) &
pgcmd) || pgcmd == 0 || !ISP2(pgcmd) ||
STRUCT_FGET(mha, mha_flags))
return (set_errno(EINVAL));
pgsz = STRUCT_FGET(mha, mha_pagesize);
/*
* call platform specific map_pgsz() routine to get the
* optimal pgsz if pgsz is 0.
*
* For stack and heap operations addr and len must be zero.
*/
if ((pgcmd & (MHA_MAPSIZE_BSSBRK|MHA_MAPSIZE_STACK)) != 0) {
if (addr != NULL || len != 0) {
return (set_errno(EINVAL));
}
/*
* Disable autompss for this process unless pgsz == 0,
* which means the system should pick. In the
* pgsz == 0 case, leave the SAUTOLPG setting alone, as
* we don't want to enable it when someone has
* disabled automatic large page selection for the
* whole system.
*/
mutex_enter(&p->p_lock);
if (pgsz != 0) {
p->p_flag &= ~SAUTOLPG;
}
mutex_exit(&p->p_lock);
as_rangelock(as);
if (pgsz == 0) {
int type;
if (pgcmd == MHA_MAPSIZE_BSSBRK)
type = MAPPGSZ_HEAP;
else
type = MAPPGSZ_STK;
pgsz = map_pgsz(type, p, 0, 0, 1);
}
} else {
/*
* addr and len must be valid for range specified.
*/
if (valid_usr_range(addr, len, 0, as,
as->a_userlimit) != RANGE_OKAY) {
return (set_errno(ENOMEM));
}
/*
* Note that we don't disable automatic large page
* selection for anon segments based on use of
* memcntl().
*/
if (pgsz == 0) {
error = as_set_default_lpsize(as, addr, len);
if (error) {
(void) set_errno(error);
}
return (error);
}
/*
* addr and len must be prefered page size aligned
*/
if (!IS_P2ALIGNED(addr, pgsz) ||
!IS_P2ALIGNED(len, pgsz)) {
return (set_errno(EINVAL));
}
}
szc = mem_getpgszc(pgsz);
if (szc == (uint_t)-1) {
if ((pgcmd & (MHA_MAPSIZE_BSSBRK|MHA_MAPSIZE_STACK))
!= 0) {
as_rangeunlock(as);
}
return (set_errno(EINVAL));
}
/*
* For stack and heap operations we first need to pad
* out existing range (create new mappings) to the new
* prefered page size boundary. Also the start of the
* .bss for the heap or user's stack base may not be on
* the new prefered page size boundary. For these cases
* we align the base of the request on the new prefered
* page size.
*/
if (pgcmd & MHA_MAPSIZE_BSSBRK) {
if (szc == p->p_brkpageszc) {
as_rangeunlock(as);
return (0);
}
if (szc > p->p_brkpageszc) {
error = brk_internal(p->p_brkbase
+ p->p_brksize, szc);
if (error) {
as_rangeunlock(as);
return (set_errno(error));
}
}
/*
* It is possible for brk_internal to silently fail to
* promote the heap size, so don't panic or ASSERT.
*/
if (!IS_P2ALIGNED(p->p_brkbase + p->p_brksize, pgsz)) {
as_rangeunlock(as);
return (set_errno(ENOMEM));
}
oszc = p->p_brkpageszc;
p->p_brkpageszc = szc;
addr = (caddr_t)P2ROUNDUP((uintptr_t)p->p_bssbase,
pgsz);
len = (p->p_brkbase + p->p_brksize) - addr;
ASSERT(IS_P2ALIGNED(len, pgsz));
/*
* Perhaps no existing pages to promote.
*/
if (len == 0) {
as_rangeunlock(as);
return (0);
}
}
/*
* The code below, as does grow.c, assumes stacks always grow
* downward.
*/
if (pgcmd & MHA_MAPSIZE_STACK) {
if (szc == p->p_stkpageszc) {
as_rangeunlock(as);
return (0);
}
if (szc > p->p_stkpageszc) {
error = grow_internal(p->p_usrstack -
p->p_stksize, szc);
if (error) {
as_rangeunlock(as);
return (set_errno(error));
}
}
/*
* It is possible for grow_internal to silently fail to
* promote the stack size, so don't panic or ASSERT.
*/
if (!IS_P2ALIGNED(p->p_usrstack - p->p_stksize, pgsz)) {
as_rangeunlock(as);
return (set_errno(ENOMEM));
}
oszc = p->p_stkpageszc;
p->p_stkpageszc = szc;
addr = p->p_usrstack - p->p_stksize;
len = P2ALIGN(p->p_stksize, pgsz);
/*
* Perhaps nothing to promote.
*/
if (len == 0 || addr >= p->p_usrstack ||
(addr + len) < addr) {
as_rangeunlock(as);
return (0);
}
}
ASSERT(IS_P2ALIGNED(addr, pgsz));
ASSERT(IS_P2ALIGNED(len, pgsz));
error = as_setpagesize(as, addr, len, szc, B_TRUE);
/*
* On stack or heap failures restore original
* pg size code.
*/
if (error) {
if ((pgcmd & MHA_MAPSIZE_BSSBRK) != 0) {
p->p_brkpageszc = oszc;
}
if ((pgcmd & MHA_MAPSIZE_STACK) != 0) {
p->p_stkpageszc = oszc;
}
(void) set_errno(error);
}
if ((pgcmd & (MHA_MAPSIZE_BSSBRK|MHA_MAPSIZE_STACK)) != 0) {
as_rangeunlock(as);
}
return (error);
case MC_ADVISE:
if ((uintptr_t)arg == MADV_FREE ||
(uintptr_t)arg == MADV_PURGE) {
len &= PAGEMASK;
}
switch ((uintptr_t)arg) {
case MADV_WILLNEED:
fc = as_faulta(as, addr, len);
if (fc) {
if (FC_CODE(fc) == FC_OBJERR)
error = set_errno(FC_ERRNO(fc));
else if (FC_CODE(fc) == FC_NOMAP)
error = set_errno(ENOMEM);
else
error = set_errno(EINVAL);
return (error);
}
break;
case MADV_DONTNEED:
/*
* For now, don't need is turned into an as_ctl(MC_SYNC)
* operation flagged for async invalidate.
*/
error = as_ctl(as, addr, len, MC_SYNC, attr,
MS_ASYNC | MS_INVALIDATE, NULL, 0);
if (error)
(void) set_errno(error);
return (error);
default:
error = as_ctl(as, addr, len, cmd, attr,
(uintptr_t)arg, NULL, 0);
if (error)
(void) set_errno(error);
return (error);
}
break;
case MC_INHERIT_ZERO:
if (arg != 0 || attr != 0 || mask != 0)
return (set_errno(EINVAL));
break;
default:
return (set_errno(EINVAL));
}
error = as_ctl(as, addr, len, cmd, attr, (uintptr_t)arg, NULL, 0);
if (error)
(void) set_errno(error);
return (error);
}
/*
* Return page size code for page size passed in. If
* matching page size not found or supported, return -1.
*/
static uint_t
mem_getpgszc(size_t pgsz) {
return ((uint_t)page_szc_user_filtered(pgsz));
}
|