xref: /illumos-gate/usr/src/lib/libproc/common/pr_meminfo.c (revision c81d47afd05baeb768e2f032636019b717899efd)
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 2002 Sun Microsystems, Inc. All rights reserved.
24  * Use is subject to license terms.
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 #include "Pcontrol.h"
36 #include "Putil.h"
37 
38 
39 int
40 pr_meminfo(struct ps_prochandle *Pr, const uint64_t *addrs,
41 	int addr_count, const uint_t *info, int info_count,
42 	uint64_t *outdata, uint_t *validity)
43 {
44 
45 
46 	int error;
47 	sysret_t rval;
48 	argdes_t argd[7];
49 	argdes_t *adp = &argd[0];
50 	struct meminfo m;
51 #ifdef _LP64
52 	struct meminfo32 m32;
53 	int model;
54 #endif
55 	int retval = -1;
56 	uintptr_t inaddr, infoaddr, outaddr, validityaddr;
57 	size_t outarraysize, infoarraysize;
58 	size_t inarraysize, validityarraysize;
59 	size_t totalsize;
60 	char *totalmap = MAP_FAILED;
61 
62 	inarraysize = addr_count * sizeof (uint64_t);
63 	outarraysize = sizeof (uint64_t) * addr_count * info_count;
64 	infoarraysize = info_count * sizeof (uint_t);
65 	validityarraysize = sizeof (uint_t) * addr_count;
66 
67 
68 	totalsize = inarraysize + outarraysize + infoarraysize +
69 		validityarraysize;
70 
71 	if ((totalmap  = pr_zmap(Pr, 0, totalsize, PROT_READ | PROT_WRITE,
72 		MAP_PRIVATE)) == MAP_FAILED) {
73 		dprintf("pr_meminfo: mmap failed\n");
74 		goto out;
75 	}
76 
77 	inaddr = (uintptr_t)totalmap;
78 
79 	outaddr = inaddr + inarraysize;
80 
81 	infoaddr = outaddr + outarraysize;
82 
83 	validityaddr = infoaddr + infoarraysize;
84 
85 	if (Pwrite(Pr, addrs, inarraysize, inaddr) != inarraysize) {
86 		dprintf("pr_meminfo: Pwrite inaddr failed \n");
87 		goto out;
88 	}
89 
90 	if (Pwrite(Pr, info, infoarraysize, infoaddr) !=
91 	    infoarraysize) {
92 		dprintf("pr_meminfo: Pwrite info failed \n");
93 		goto out;
94 	}
95 
96 #ifdef _LP64
97 	model = Pr->status.pr_dmodel;
98 	if (model == PR_MODEL_ILP32) {
99 		m32.mi_info_count = info_count;
100 		m32.mi_inaddr = (caddr32_t)inaddr;
101 		m32.mi_outdata = (caddr32_t)outaddr;
102 		m32.mi_info_req = (caddr32_t)infoaddr;
103 		m32.mi_validity = (caddr32_t)validityaddr;
104 	} else
105 #endif
106 	{
107 		m.mi_info_count = info_count;
108 		m.mi_inaddr = (uint64_t *)inaddr;
109 		m.mi_outdata = (uint64_t *)outaddr;
110 		m.mi_info_req = (uint_t *)infoaddr;
111 		m.mi_validity = (uint_t *)validityaddr;
112 	}
113 
114 
115 	/*
116 	 * initial command
117 	 */
118 
119 	adp->arg_value = MISYS_MEMINFO;
120 	adp->arg_object = NULL;
121 	adp->arg_type = AT_BYVAL;
122 	adp->arg_inout = AI_INPUT;
123 	adp->arg_size = 0;
124 	adp++;
125 
126 	/*
127 	 * length of input address vector
128 	 */
129 
130 	adp->arg_value = addr_count;
131 	adp->arg_object = NULL;
132 	adp->arg_type = AT_BYVAL;
133 	adp->arg_inout = AI_INPUT;
134 	adp->arg_size = 0;
135 	adp++;
136 
137 	/*
138 	 * information wanted vector
139 	 */
140 
141 	adp->arg_value = 0;
142 #ifdef _LP64
143 	if (model == PR_MODEL_ILP32) {
144 		adp->arg_object = &m32;
145 		adp->arg_size = sizeof (struct meminfo32);
146 	} else
147 #endif
148 	{
149 		adp->arg_object = &m;
150 		adp->arg_size = sizeof (struct meminfo);
151 	}
152 	adp->arg_type = AT_BYREF;
153 	adp->arg_inout = AI_INPUT;
154 
155 
156 	error = Psyscall(Pr,  &rval, SYS_meminfosys, 3, &argd[0]);
157 
158 	if (error) {
159 		errno = (error > 0) ? error: ENOSYS;
160 		goto out;
161 	}
162 
163 	/* syscall was successful, copy out the data */
164 
165 	if ((Pread(Pr, outdata, outarraysize, outaddr)) != outarraysize) {
166 		dprintf("pr_meminfo: Pread of outarray failed\n");
167 		goto out;
168 	}
169 
170 	if (Pread(Pr, validity, validityarraysize, validityaddr)
171 	    != validityarraysize) {
172 		dprintf("pr_meminfo: Pread of validity array failed\n");
173 		goto out;
174 	}
175 
176 	retval = rval.sys_rval1;
177 
178 out:
179 
180 	if (totalmap != MAP_FAILED &&
181 	    pr_munmap(Pr, totalmap, totalsize) == -1) {
182 			dprintf("pr_meminfo: munmap failed\n");
183 			retval = -1;
184 		}
185 
186 	return (retval);
187 
188 }
189