xref: /illumos-gate/usr/src/lib/libproc/common/pr_memcntl.c (revision fe4627ef755b7c263f91a0e6f07cdca5d7083501)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1998-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <sys/types.h>
33 #include <sys/mman.h>
34 #include "libproc.h"
35 
36 /*
37  * memcntl() system call -- executed by subject process
38  */
39 
40 int
41 pr_memcntl(struct ps_prochandle *Pr,
42 	caddr_t addr, size_t len, int cmd, caddr_t arg, int attr, int mask)
43 {
44 	sysret_t rval;			/* return value from memcntl() */
45 	argdes_t argd[6];		/* arg descriptors for memcntl() */
46 	argdes_t *adp;
47 	int error;
48 
49 	if (Pr == NULL)		/* no subject process */
50 		return (memcntl(addr, len, cmd, arg, attr, mask));
51 
52 	adp = &argd[0];		/* addr argument */
53 	adp->arg_value = (uintptr_t)addr;
54 	adp->arg_object = NULL;
55 	adp->arg_type = AT_BYVAL;
56 	adp->arg_inout = AI_INPUT;
57 	adp->arg_size = 0;
58 
59 	adp++;			/* len argument */
60 	adp->arg_value = len;
61 	adp->arg_object = NULL;
62 	adp->arg_type = AT_BYVAL;
63 	adp->arg_inout = AI_INPUT;
64 	adp->arg_size = 0;
65 
66 	adp++;			/* cmd argument */
67 	adp->arg_value = cmd;
68 	adp->arg_object = NULL;
69 	adp->arg_type = AT_BYVAL;
70 	adp->arg_inout = AI_INPUT;
71 	adp->arg_size = 0;
72 
73 	adp++;			/* arg argument */
74 	if (cmd == MC_HAT_ADVISE) {
75 		adp->arg_value = 0;
76 		adp->arg_object = arg;
77 		adp->arg_type = AT_BYREF;
78 		adp->arg_inout = AI_INPUT;
79 #ifdef _LP64
80 		if (Pstatus(Pr)->pr_dmodel == PR_MODEL_ILP32)
81 			adp->arg_size = sizeof (struct memcntl_mha32);
82 		else
83 			adp->arg_size = sizeof (struct memcntl_mha);
84 #else
85 		adp->arg_size = sizeof (struct memcntl_mha);
86 #endif
87 	} else {
88 		adp->arg_value = (uintptr_t)arg;
89 		adp->arg_object = NULL;
90 		adp->arg_type = AT_BYVAL;
91 		adp->arg_inout = AI_INPUT;
92 		adp->arg_size = 0;
93 	}
94 
95 	adp++;			/* attr argument */
96 	adp->arg_value = attr;
97 	adp->arg_object = NULL;
98 	adp->arg_type = AT_BYVAL;
99 	adp->arg_inout = AI_INPUT;
100 	adp->arg_size = 0;
101 
102 	adp++;			/* mask argument */
103 	adp->arg_value = mask;
104 	adp->arg_object = NULL;
105 	adp->arg_type = AT_BYVAL;
106 	adp->arg_inout = AI_INPUT;
107 	adp->arg_size = 0;
108 
109 	error = Psyscall(Pr, &rval, SYS_memcntl, 6, &argd[0]);
110 
111 	if (error) {
112 		errno = (error > 0)? error : ENOSYS;
113 		return (-1);
114 	}
115 	return (0);
116 }
117