1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*7c478bd9Sstevel@tonic-gate
29*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
30*7c478bd9Sstevel@tonic-gate #include <unistd.h>
31*7c478bd9Sstevel@tonic-gate #include <errno.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/mman.h>
34*7c478bd9Sstevel@tonic-gate #include "libproc.h"
35*7c478bd9Sstevel@tonic-gate #include "Pcontrol.h"
36*7c478bd9Sstevel@tonic-gate #include "Putil.h"
37*7c478bd9Sstevel@tonic-gate
38*7c478bd9Sstevel@tonic-gate
39*7c478bd9Sstevel@tonic-gate int
pr_meminfo(struct ps_prochandle * Pr,const uint64_t * addrs,int addr_count,const uint_t * info,int info_count,uint64_t * outdata,uint_t * validity)40*7c478bd9Sstevel@tonic-gate pr_meminfo(struct ps_prochandle *Pr, const uint64_t *addrs,
41*7c478bd9Sstevel@tonic-gate int addr_count, const uint_t *info, int info_count,
42*7c478bd9Sstevel@tonic-gate uint64_t *outdata, uint_t *validity)
43*7c478bd9Sstevel@tonic-gate {
44*7c478bd9Sstevel@tonic-gate
45*7c478bd9Sstevel@tonic-gate
46*7c478bd9Sstevel@tonic-gate int error;
47*7c478bd9Sstevel@tonic-gate sysret_t rval;
48*7c478bd9Sstevel@tonic-gate argdes_t argd[7];
49*7c478bd9Sstevel@tonic-gate argdes_t *adp = &argd[0];
50*7c478bd9Sstevel@tonic-gate struct meminfo m;
51*7c478bd9Sstevel@tonic-gate #ifdef _LP64
52*7c478bd9Sstevel@tonic-gate struct meminfo32 m32;
53*7c478bd9Sstevel@tonic-gate int model;
54*7c478bd9Sstevel@tonic-gate #endif
55*7c478bd9Sstevel@tonic-gate int retval = -1;
56*7c478bd9Sstevel@tonic-gate uintptr_t inaddr, infoaddr, outaddr, validityaddr;
57*7c478bd9Sstevel@tonic-gate size_t outarraysize, infoarraysize;
58*7c478bd9Sstevel@tonic-gate size_t inarraysize, validityarraysize;
59*7c478bd9Sstevel@tonic-gate size_t totalsize;
60*7c478bd9Sstevel@tonic-gate char *totalmap = MAP_FAILED;
61*7c478bd9Sstevel@tonic-gate
62*7c478bd9Sstevel@tonic-gate inarraysize = addr_count * sizeof (uint64_t);
63*7c478bd9Sstevel@tonic-gate outarraysize = sizeof (uint64_t) * addr_count * info_count;
64*7c478bd9Sstevel@tonic-gate infoarraysize = info_count * sizeof (uint_t);
65*7c478bd9Sstevel@tonic-gate validityarraysize = sizeof (uint_t) * addr_count;
66*7c478bd9Sstevel@tonic-gate
67*7c478bd9Sstevel@tonic-gate
68*7c478bd9Sstevel@tonic-gate totalsize = inarraysize + outarraysize + infoarraysize +
69*7c478bd9Sstevel@tonic-gate validityarraysize;
70*7c478bd9Sstevel@tonic-gate
71*7c478bd9Sstevel@tonic-gate if ((totalmap = pr_zmap(Pr, 0, totalsize, PROT_READ | PROT_WRITE,
72*7c478bd9Sstevel@tonic-gate MAP_PRIVATE)) == MAP_FAILED) {
73*7c478bd9Sstevel@tonic-gate dprintf("pr_meminfo: mmap failed\n");
74*7c478bd9Sstevel@tonic-gate goto out;
75*7c478bd9Sstevel@tonic-gate }
76*7c478bd9Sstevel@tonic-gate
77*7c478bd9Sstevel@tonic-gate inaddr = (uintptr_t)totalmap;
78*7c478bd9Sstevel@tonic-gate
79*7c478bd9Sstevel@tonic-gate outaddr = inaddr + inarraysize;
80*7c478bd9Sstevel@tonic-gate
81*7c478bd9Sstevel@tonic-gate infoaddr = outaddr + outarraysize;
82*7c478bd9Sstevel@tonic-gate
83*7c478bd9Sstevel@tonic-gate validityaddr = infoaddr + infoarraysize;
84*7c478bd9Sstevel@tonic-gate
85*7c478bd9Sstevel@tonic-gate if (Pwrite(Pr, addrs, inarraysize, inaddr) != inarraysize) {
86*7c478bd9Sstevel@tonic-gate dprintf("pr_meminfo: Pwrite inaddr failed \n");
87*7c478bd9Sstevel@tonic-gate goto out;
88*7c478bd9Sstevel@tonic-gate }
89*7c478bd9Sstevel@tonic-gate
90*7c478bd9Sstevel@tonic-gate if (Pwrite(Pr, info, infoarraysize, infoaddr) !=
91*7c478bd9Sstevel@tonic-gate infoarraysize) {
92*7c478bd9Sstevel@tonic-gate dprintf("pr_meminfo: Pwrite info failed \n");
93*7c478bd9Sstevel@tonic-gate goto out;
94*7c478bd9Sstevel@tonic-gate }
95*7c478bd9Sstevel@tonic-gate
96*7c478bd9Sstevel@tonic-gate #ifdef _LP64
97*7c478bd9Sstevel@tonic-gate model = Pr->status.pr_dmodel;
98*7c478bd9Sstevel@tonic-gate if (model == PR_MODEL_ILP32) {
99*7c478bd9Sstevel@tonic-gate m32.mi_info_count = info_count;
100*7c478bd9Sstevel@tonic-gate m32.mi_inaddr = (caddr32_t)inaddr;
101*7c478bd9Sstevel@tonic-gate m32.mi_outdata = (caddr32_t)outaddr;
102*7c478bd9Sstevel@tonic-gate m32.mi_info_req = (caddr32_t)infoaddr;
103*7c478bd9Sstevel@tonic-gate m32.mi_validity = (caddr32_t)validityaddr;
104*7c478bd9Sstevel@tonic-gate } else
105*7c478bd9Sstevel@tonic-gate #endif
106*7c478bd9Sstevel@tonic-gate {
107*7c478bd9Sstevel@tonic-gate m.mi_info_count = info_count;
108*7c478bd9Sstevel@tonic-gate m.mi_inaddr = (uint64_t *)inaddr;
109*7c478bd9Sstevel@tonic-gate m.mi_outdata = (uint64_t *)outaddr;
110*7c478bd9Sstevel@tonic-gate m.mi_info_req = (uint_t *)infoaddr;
111*7c478bd9Sstevel@tonic-gate m.mi_validity = (uint_t *)validityaddr;
112*7c478bd9Sstevel@tonic-gate }
113*7c478bd9Sstevel@tonic-gate
114*7c478bd9Sstevel@tonic-gate
115*7c478bd9Sstevel@tonic-gate /*
116*7c478bd9Sstevel@tonic-gate * initial command
117*7c478bd9Sstevel@tonic-gate */
118*7c478bd9Sstevel@tonic-gate
119*7c478bd9Sstevel@tonic-gate adp->arg_value = MISYS_MEMINFO;
120*7c478bd9Sstevel@tonic-gate adp->arg_object = NULL;
121*7c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYVAL;
122*7c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT;
123*7c478bd9Sstevel@tonic-gate adp->arg_size = 0;
124*7c478bd9Sstevel@tonic-gate adp++;
125*7c478bd9Sstevel@tonic-gate
126*7c478bd9Sstevel@tonic-gate /*
127*7c478bd9Sstevel@tonic-gate * length of input address vector
128*7c478bd9Sstevel@tonic-gate */
129*7c478bd9Sstevel@tonic-gate
130*7c478bd9Sstevel@tonic-gate adp->arg_value = addr_count;
131*7c478bd9Sstevel@tonic-gate adp->arg_object = NULL;
132*7c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYVAL;
133*7c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT;
134*7c478bd9Sstevel@tonic-gate adp->arg_size = 0;
135*7c478bd9Sstevel@tonic-gate adp++;
136*7c478bd9Sstevel@tonic-gate
137*7c478bd9Sstevel@tonic-gate /*
138*7c478bd9Sstevel@tonic-gate * information wanted vector
139*7c478bd9Sstevel@tonic-gate */
140*7c478bd9Sstevel@tonic-gate
141*7c478bd9Sstevel@tonic-gate adp->arg_value = 0;
142*7c478bd9Sstevel@tonic-gate #ifdef _LP64
143*7c478bd9Sstevel@tonic-gate if (model == PR_MODEL_ILP32) {
144*7c478bd9Sstevel@tonic-gate adp->arg_object = &m32;
145*7c478bd9Sstevel@tonic-gate adp->arg_size = sizeof (struct meminfo32);
146*7c478bd9Sstevel@tonic-gate } else
147*7c478bd9Sstevel@tonic-gate #endif
148*7c478bd9Sstevel@tonic-gate {
149*7c478bd9Sstevel@tonic-gate adp->arg_object = &m;
150*7c478bd9Sstevel@tonic-gate adp->arg_size = sizeof (struct meminfo);
151*7c478bd9Sstevel@tonic-gate }
152*7c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYREF;
153*7c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT;
154*7c478bd9Sstevel@tonic-gate
155*7c478bd9Sstevel@tonic-gate
156*7c478bd9Sstevel@tonic-gate error = Psyscall(Pr, &rval, SYS_meminfosys, 3, &argd[0]);
157*7c478bd9Sstevel@tonic-gate
158*7c478bd9Sstevel@tonic-gate if (error) {
159*7c478bd9Sstevel@tonic-gate errno = (error > 0) ? error: ENOSYS;
160*7c478bd9Sstevel@tonic-gate goto out;
161*7c478bd9Sstevel@tonic-gate }
162*7c478bd9Sstevel@tonic-gate
163*7c478bd9Sstevel@tonic-gate /* syscall was successful, copy out the data */
164*7c478bd9Sstevel@tonic-gate
165*7c478bd9Sstevel@tonic-gate if ((Pread(Pr, outdata, outarraysize, outaddr)) != outarraysize) {
166*7c478bd9Sstevel@tonic-gate dprintf("pr_meminfo: Pread of outarray failed\n");
167*7c478bd9Sstevel@tonic-gate goto out;
168*7c478bd9Sstevel@tonic-gate }
169*7c478bd9Sstevel@tonic-gate
170*7c478bd9Sstevel@tonic-gate if (Pread(Pr, validity, validityarraysize, validityaddr)
171*7c478bd9Sstevel@tonic-gate != validityarraysize) {
172*7c478bd9Sstevel@tonic-gate dprintf("pr_meminfo: Pread of validity array failed\n");
173*7c478bd9Sstevel@tonic-gate goto out;
174*7c478bd9Sstevel@tonic-gate }
175*7c478bd9Sstevel@tonic-gate
176*7c478bd9Sstevel@tonic-gate retval = rval.sys_rval1;
177*7c478bd9Sstevel@tonic-gate
178*7c478bd9Sstevel@tonic-gate out:
179*7c478bd9Sstevel@tonic-gate
180*7c478bd9Sstevel@tonic-gate if (totalmap != MAP_FAILED &&
181*7c478bd9Sstevel@tonic-gate pr_munmap(Pr, totalmap, totalsize) == -1) {
182*7c478bd9Sstevel@tonic-gate dprintf("pr_meminfo: munmap failed\n");
183*7c478bd9Sstevel@tonic-gate retval = -1;
184*7c478bd9Sstevel@tonic-gate }
185*7c478bd9Sstevel@tonic-gate
186*7c478bd9Sstevel@tonic-gate return (retval);
187*7c478bd9Sstevel@tonic-gate
188*7c478bd9Sstevel@tonic-gate }
189