xref: /illumos-gate/usr/src/cmd/ptools/pmap/pmap.c (revision e443d926fa6c5807f868cf128c5cc66e3e171630)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Copyright 2025 Oxide Computer Company
29  */
30 
31 #include <stdio.h>
32 #include <stdio_ext.h>
33 #include <stdlib.h>
34 #include <stddef.h>
35 #include <unistd.h>
36 #include <ctype.h>
37 #include <fcntl.h>
38 #include <string.h>
39 #include <dirent.h>
40 #include <limits.h>
41 #include <link.h>
42 #include <libelf.h>
43 #include <sys/types.h>
44 #include <signal.h>
45 #include <sys/stat.h>
46 #include <sys/mkdev.h>
47 #include <sys/mman.h>
48 #include <sys/lgrp_user.h>
49 #include <sys/debug.h>
50 #include <libproc.h>
51 
52 #include "pmap_common.h"
53 
54 #define	KILOBYTE	1024
55 #define	MEGABYTE	(KILOBYTE * KILOBYTE)
56 #define	GIGABYTE	(KILOBYTE * KILOBYTE * KILOBYTE)
57 
58 /*
59  * Round up the value to the nearest kilobyte
60  */
61 #define	ROUNDUP_KB(x)	(((x) + (KILOBYTE - 1)) / KILOBYTE)
62 
63 /*
64  * The alignment should be a power of 2.
65  */
66 #define	P2ALIGN(x, align)		((x) & -(align))
67 
68 #define	INVALID_ADDRESS			(uintptr_t)(-1)
69 
70 struct totals {
71 	ulong_t total_size;
72 	ulong_t total_swap;
73 	ulong_t total_rss;
74 	ulong_t total_anon;
75 	ulong_t total_locked;
76 };
77 
78 /*
79  * -L option requires per-page information. The information is presented in an
80  * array of page_descr structures.
81  */
82 typedef struct page_descr {
83 	uintptr_t	pd_start;	/* start address of a page */
84 	size_t		pd_pagesize;	/* page size in bytes */
85 	lgrp_id_t	pd_lgrp;	/* lgroup of memory backing the page */
86 	int		pd_valid;	/* valid page description if non-zero */
87 } page_descr_t;
88 
89 /*
90  * Per-page information for a memory chunk.
91  * The meminfo(2) system call accepts up to MAX_MEMINFO_CNT pages at once.
92  * When we need to scan larger ranges we divide them in MAX_MEMINFO_CNT sized
93  * chunks. The chunk information is stored in the memory_chunk structure.
94  */
95 typedef struct memory_chunk {
96 	page_descr_t	page_info[MAX_MEMINFO_CNT];
97 	uintptr_t	end_addr;
98 	uintptr_t	chunk_start;	/* Starting address */
99 	uintptr_t	chunk_end;	/* chunk_end is always <= end_addr */
100 	size_t		page_size;
101 	int		page_index;	/* Current page */
102 	int		page_count;	/* Number of pages */
103 } memory_chunk_t;
104 
105 static volatile int interrupt;
106 
107 typedef int proc_xmap_f(void *, const prxmap_t *, const char *, int, int);
108 
109 static	int	xmapping_iter(struct ps_prochandle *, proc_xmap_f *, void *,
110     int);
111 static	int	rmapping_iter(struct ps_prochandle *, proc_map_f *, void *);
112 
113 static	int	look_map(void *, const prmap_t *, const char *);
114 static	int	look_smap(void *, const prxmap_t *, const char *, int, int);
115 static	int	look_xmap(void *, const prxmap_t *, const char *, int, int);
116 static	int	look_xmap_nopgsz(void *, const prxmap_t *, const char *,
117     int, int);
118 
119 static int gather_map(void *, const prmap_t *, const char *);
120 static int gather_xmap(void *, const prxmap_t *, const char *, int, int);
121 static int iter_map(proc_map_f *, void *);
122 static int iter_xmap(proc_xmap_f *, void *);
123 static int parse_addr_range(char *, uintptr_t *, uintptr_t *);
124 static void mem_chunk_init(memory_chunk_t *, uintptr_t, size_t);
125 
126 static	int	perr(char *);
127 static	void	printK(long, int);
128 static	char	*mflags(uint_t);
129 
130 static size_t get_contiguous_region(memory_chunk_t *, uintptr_t,
131     uintptr_t, size_t, lgrp_id_t *);
132 static void	mem_chunk_get(memory_chunk_t *, uintptr_t);
133 static lgrp_id_t addr_to_lgrp(memory_chunk_t *, uintptr_t, size_t *);
134 static char	*lgrp2str(lgrp_id_t);
135 
136 static int	address_in_range(uintptr_t, uintptr_t, size_t);
137 static size_t	adjust_addr_range(uintptr_t, uintptr_t, size_t,
138     uintptr_t *, uintptr_t *);
139 
140 static	int	lflag = 0;
141 static	int	Lflag = 0;
142 static	int	aflag = 0;
143 
144 /*
145  * The -A address range is represented as a pair of addresses
146  * <start_addr, end_addr>. Either one of these may be unspecified (set to
147  * INVALID_ADDRESS). If both are unspecified, no address range restrictions are
148  * in place.
149  */
150 static  uintptr_t start_addr = INVALID_ADDRESS;
151 static	uintptr_t end_addr = INVALID_ADDRESS;
152 static	uintptr_t comm_page = INVALID_ADDRESS;
153 
154 static	int	addr_width, size_width;
155 static	char	*command;
156 static	char	*procname;
157 static	struct ps_prochandle *Pr;
158 
159 static void intr(int);
160 
161 typedef struct {
162 	prxmap_t	md_xmap;
163 	prmap_t		md_map;
164 	char		*md_objname;
165 	boolean_t	md_last;
166 	int		md_doswap;
167 } mapdata_t;
168 
169 static	mapdata_t	*maps;
170 static	int		map_count;
171 static	int		map_alloc;
172 
173 static	lwpstack_t *stacks = NULL;
174 static	uint_t	nstacks = 0;
175 
176 #define	MAX_TRIES	5
177 
178 static boolean_t
reallocstacks(uint_t newcount)179 reallocstacks(uint_t newcount)
180 {
181 	lwpstack_t *newstacks;
182 
183 	newstacks = recallocarray(stacks, nstacks, newcount,
184 	    sizeof (lwpstack_t));
185 	if (newstacks != NULL) {
186 		stacks = newstacks;
187 		nstacks = newcount;
188 		return (B_TRUE);
189 	}
190 	return (B_FALSE);
191 }
192 
193 static int
getstack(void * data,const lwpstatus_t * lsp)194 getstack(void *data, const lwpstatus_t *lsp)
195 {
196 	uint_t *np = (uint_t *)data;
197 
198 	/*
199 	 * In the unlikely event that the number of LWPs has increased since we
200 	 * allocated the stacks array to hold them, expand the space for these
201 	 * next two entries.
202 	 */
203 	if (*np + 2 > nstacks && !reallocstacks(nstacks + 2)) {
204 		(void) fprintf(stderr, "%s: warning: "
205 		    "number of LWPs changed during execution, some details "
206 		    "have been omitted.\n", command);
207 		/* Terminate the walk */
208 		return (1);
209 	}
210 
211 	if (Plwp_alt_stack(Pr, lsp->pr_lwpid, &stacks[*np].lwps_stack) == 0) {
212 		stacks[*np].lwps_stack.ss_flags |= SS_ONSTACK;
213 		stacks[*np].lwps_lwpid = lsp->pr_lwpid;
214 		(*np)++;
215 	}
216 
217 	if (Plwp_main_stack(Pr, lsp->pr_lwpid, &stacks[*np].lwps_stack) == 0) {
218 		stacks[*np].lwps_lwpid = lsp->pr_lwpid;
219 		(*np)++;
220 	}
221 
222 	VERIFY3U(*np, <=, nstacks);
223 
224 	return (0);
225 }
226 
227 int
main(int argc,char ** argv)228 main(int argc, char **argv)
229 {
230 	int rflag = 0, sflag = 0, xflag = 0, Fflag = 0;
231 	int errflg = 0, Sflag = 0;
232 	int rc = 0;
233 	int opt;
234 	const char *bar8 = "-------";
235 	const char *bar16 = "----------";
236 	const char *bar;
237 	struct rlimit rlim;
238 	struct stat64 statbuf;
239 	char buf[128];
240 	int mapfd;
241 	int prg_gflags = PGRAB_RDONLY;
242 	int prr_flags = 0;
243 	boolean_t use_agent_lwp = B_FALSE;
244 
245 	if ((command = strrchr(argv[0], '/')) != NULL)
246 		command++;
247 	else
248 		command = argv[0];
249 
250 	while ((opt = getopt(argc, argv, "arsxSlLFA:")) != EOF) {
251 		switch (opt) {
252 		case 'a':		/* include shared mappings in -[xS] */
253 			aflag = 1;
254 			break;
255 		case 'r':		/* show reserved mappings */
256 			rflag = 1;
257 			break;
258 		case 's':		/* show hardware page sizes */
259 			sflag = 1;
260 			break;
261 		case 'S':		/* show swap reservations */
262 			Sflag = 1;
263 			break;
264 		case 'x':		/* show extended mappings */
265 			xflag = 1;
266 			break;
267 		case 'l':		/* show unresolved link map names */
268 			lflag = 1;
269 			break;
270 		case 'L':		/* show lgroup information */
271 			Lflag = 1;
272 			use_agent_lwp = B_TRUE;
273 			break;
274 		case 'F':		/* force grabbing (no O_EXCL) */
275 			Fflag = PGRAB_FORCE;
276 			break;
277 		case 'A':
278 			if (parse_addr_range(optarg, &start_addr, &end_addr)
279 			    != 0)
280 				errflg++;
281 			break;
282 		default:
283 			errflg = 1;
284 			break;
285 		}
286 	}
287 
288 	argc -= optind;
289 	argv += optind;
290 
291 	if ((Sflag && (xflag || rflag || sflag)) || (xflag && rflag) ||
292 	    (aflag && (!xflag && !Sflag)) ||
293 	    (Lflag && (xflag || Sflag))) {
294 		errflg = 1;
295 	}
296 
297 	if (errflg || argc <= 0) {
298 		(void) fprintf(stderr,
299 		    "usage:\t%s [-rslF] [-A start[,end]] { pid | core } ...\n",
300 		    command);
301 		(void) fprintf(stderr,
302 		    "\t\t(report process address maps)\n");
303 		(void) fprintf(stderr,
304 		    "\t%s -L [-rslF] [-A start[,end]] pid ...\n", command);
305 		(void) fprintf(stderr,
306 		    "\t\t(report process address maps lgroups mappings)\n");
307 		(void) fprintf(stderr,
308 		    "\t%s -x [-aslF] [-A start[,end]] pid ...\n", command);
309 		(void) fprintf(stderr,
310 		    "\t\t(show resident/anon/locked mapping details)\n");
311 		(void) fprintf(stderr,
312 		    "\t%s -S [-alF] [-A start[,end]] { pid | core } ...\n",
313 		    command);
314 		(void) fprintf(stderr,
315 		    "\t\t(show swap reservations)\n\n");
316 		(void) fprintf(stderr,
317 		    "\t-a: include shared mappings in -[xS] summary\n");
318 		(void) fprintf(stderr,
319 		    "\t-r: show reserved address maps\n");
320 		(void) fprintf(stderr,
321 		    "\t-s: show hardware page sizes\n");
322 		(void) fprintf(stderr,
323 		    "\t-l: show unresolved dynamic linker map names\n");
324 		(void) fprintf(stderr,
325 		    "\t-F: force grabbing of the target process\n");
326 		(void) fprintf(stderr,
327 		    "\t-L: show lgroup mappings\n");
328 		(void) fprintf(stderr,
329 		    "\t-A start,end: limit output to the specified range\n");
330 		return (2);
331 	}
332 
333 	/*
334 	 * Make sure we'll have enough file descriptors to handle a target
335 	 * that has many many mappings.
336 	 */
337 	if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
338 		rlim.rlim_cur = rlim.rlim_max;
339 		(void) setrlimit(RLIMIT_NOFILE, &rlim);
340 		(void) enable_extended_FILE_stdio(-1, -1);
341 	}
342 
343 	/*
344 	 * The implementation of -L option creates an agent LWP in the target
345 	 * process address space. The agent LWP issues meminfo(2) system calls
346 	 * on behalf of the target process. If we are interrupted prematurely,
347 	 * the target process remains in the stopped state with the agent still
348 	 * attached to it. To prevent such situation we catch signals from
349 	 * terminal and terminate gracefully.
350 	 */
351 	if (use_agent_lwp) {
352 		/*
353 		 * Buffer output to stdout, stderr while process is grabbed.
354 		 * Prevents infamous deadlocks due to pmap `pgrep xterm` and
355 		 * other variants.
356 		 */
357 		(void) proc_initstdio();
358 
359 		prg_gflags = PGRAB_RETAIN | Fflag;
360 		prr_flags = PRELEASE_RETAIN;
361 
362 		if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
363 			(void) sigset(SIGHUP, intr);
364 		if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
365 			(void) sigset(SIGINT, intr);
366 		if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
367 			(void) sigset(SIGQUIT, intr);
368 		(void) sigset(SIGPIPE, intr);
369 		(void) sigset(SIGTERM, intr);
370 	}
371 
372 	while (argc-- > 0) {
373 		char *arg;
374 		int gcode;
375 		psinfo_t psinfo;
376 		int tries = 0;
377 
378 		if (use_agent_lwp)
379 			(void) proc_flushstdio();
380 
381 		if ((Pr = proc_arg_grab(arg = *argv++, PR_ARG_ANY,
382 		    prg_gflags, &gcode)) == NULL) {
383 			(void) fprintf(stderr, "%s: cannot examine %s: %s\n",
384 			    command, arg, Pgrab_error(gcode));
385 			rc++;
386 			continue;
387 		}
388 
389 		procname = arg;		/* for perr() */
390 
391 		addr_width = (Pstatus(Pr)->pr_dmodel == PR_MODEL_LP64) ? 16 : 8;
392 		size_width = (Pstatus(Pr)->pr_dmodel == PR_MODEL_LP64) ? 11 : 8;
393 		bar = addr_width == 8 ? bar8 : bar16;
394 		(void) memcpy(&psinfo, Ppsinfo(Pr), sizeof (psinfo_t));
395 		proc_unctrl_psinfo(&psinfo);
396 
397 		if (Pstate(Pr) != PS_DEAD) {
398 			(void) snprintf(buf, sizeof (buf),
399 			    "/proc/%d/map", (int)psinfo.pr_pid);
400 			if ((mapfd = open(buf, O_RDONLY)) < 0) {
401 				(void) fprintf(stderr, "%s: cannot "
402 				    "examine %s: lost control of "
403 				    "process\n", command, arg);
404 				rc++;
405 				Prelease(Pr, prr_flags);
406 				continue;
407 			}
408 		} else {
409 			mapfd = -1;
410 		}
411 
412 again:
413 		map_count = 0;
414 
415 		if (Pstate(Pr) == PS_DEAD) {
416 			(void) printf("core '%s' of %d:\t%.70s\n",
417 			    arg, (int)psinfo.pr_pid, psinfo.pr_psargs);
418 
419 			if (rflag || sflag || xflag || Sflag || Lflag) {
420 				(void) printf("  -%c option is not compatible "
421 				    "with core files\n", xflag ? 'x' :
422 				    sflag ? 's' : rflag ? 'r' :
423 				    Lflag ? 'L' : 'S');
424 				Prelease(Pr, prr_flags);
425 				rc++;
426 				continue;
427 			}
428 
429 		} else {
430 			(void) printf("%d:\t%.70s\n",
431 			    (int)psinfo.pr_pid, psinfo.pr_psargs);
432 		}
433 
434 		if (!(Pstatus(Pr)->pr_flags & PR_ISSYS)) {
435 			struct totals t;
436 
437 			/*
438 			 * Since we're grabbing the process readonly, we need
439 			 * to make sure the address space doesn't change during
440 			 * execution.
441 			 */
442 			if (Pstate(Pr) != PS_DEAD) {
443 				if (tries++ == MAX_TRIES) {
444 					Prelease(Pr, prr_flags);
445 					(void) close(mapfd);
446 					(void) fprintf(stderr, "%s: cannot "
447 					    "examine %s: address space is "
448 					    "changing\n", command, arg);
449 					continue;
450 				}
451 
452 				if (fstat64(mapfd, &statbuf) != 0) {
453 					Prelease(Pr, prr_flags);
454 					(void) close(mapfd);
455 					(void) fprintf(stderr, "%s: cannot "
456 					    "examine %s: lost control of "
457 					    "process\n", command, arg);
458 					continue;
459 				}
460 			}
461 
462 			/*
463 			 * Multiplied by 2 to accomodate the main and alt
464 			 * stack for each LWP.
465 			 */
466 			if (reallocstacks(psinfo.pr_nlwp * 2)) {
467 				uint_t n = 0;
468 				(void) Plwp_iter(Pr, getstack, &n);
469 				qsort(stacks, nstacks, sizeof (stacks[0]),
470 				    cmpstacks);
471 			}
472 
473 			(void) memset(&t, 0, sizeof (t));
474 
475 			if (Pgetauxval(Pr, AT_BASE) != -1L &&
476 			    Prd_agent(Pr) == NULL) {
477 				(void) fprintf(stderr, "%s: warning: "
478 				    "librtld_db failed to initialize; "
479 				    "shared library information will not be "
480 				    "available\n", command);
481 			}
482 
483 			/*
484 			 * Gather data
485 			 */
486 			comm_page = Pgetauxval(Pr, AT_SUN_COMMPAGE);
487 			if (xflag)
488 				rc += xmapping_iter(Pr, gather_xmap, NULL, 0);
489 			else if (Sflag)
490 				rc += xmapping_iter(Pr, gather_xmap, NULL, 1);
491 			else {
492 				if (rflag)
493 					rc += rmapping_iter(Pr, gather_map,
494 					    NULL);
495 				else if (sflag)
496 					rc += xmapping_iter(Pr, gather_xmap,
497 					    NULL, 0);
498 				else if (lflag)
499 					rc += Pmapping_iter(Pr,
500 					    gather_map, NULL);
501 				else
502 					rc += Pmapping_iter_resolved(Pr,
503 					    gather_map, NULL);
504 			}
505 
506 			/*
507 			 * Ensure mappings are consistent.
508 			 */
509 			if (Pstate(Pr) != PS_DEAD) {
510 				struct stat64 newbuf;
511 
512 				if (fstat64(mapfd, &newbuf) != 0 ||
513 				    memcmp(&newbuf.st_mtim, &statbuf.st_mtim,
514 				    sizeof (newbuf.st_mtim)) != 0) {
515 					if (stacks != NULL) {
516 						free(stacks);
517 						stacks = NULL;
518 						nstacks = 0;
519 					}
520 					goto again;
521 				}
522 			}
523 
524 			/*
525 			 * Display data.
526 			 */
527 			if (xflag) {
528 				(void) printf("%*s%*s%*s%*s%*s "
529 				    "%sMode   Mapped File\n",
530 				    addr_width, "Address",
531 				    size_width, "Kbytes",
532 				    size_width, "RSS",
533 				    size_width, "Anon",
534 				    size_width, "Locked",
535 				    sflag ? "Pgsz " : "");
536 
537 				rc += iter_xmap(sflag ?  look_xmap :
538 				    look_xmap_nopgsz, &t);
539 
540 				(void) printf("%s%s %s %s %s %s\n",
541 				    addr_width == 8 ? "-" : "------",
542 				    bar, bar, bar, bar, bar);
543 
544 				(void) printf("%stotal Kb", addr_width == 16 ?
545 				    "        " : "");
546 
547 				printK(t.total_size, size_width);
548 				printK(t.total_rss, size_width);
549 				printK(t.total_anon, size_width);
550 				printK(t.total_locked, size_width);
551 
552 				(void) printf("\n");
553 
554 			} else if (Sflag) {
555 				(void) printf("%*s%*s%*s Mode"
556 				    " Mapped File\n",
557 				    addr_width, "Address",
558 				    size_width, "Kbytes",
559 				    size_width, "Swap");
560 
561 				rc += iter_xmap(look_xmap_nopgsz, &t);
562 
563 				(void) printf("%s%s %s %s\n",
564 				    addr_width == 8 ? "-" : "------",
565 				    bar, bar, bar);
566 
567 				(void) printf("%stotal Kb", addr_width == 16 ?
568 				    "        " : "");
569 
570 				printK(t.total_size, size_width);
571 				printK(t.total_swap, size_width);
572 
573 				(void) printf("\n");
574 
575 			} else {
576 
577 				if (rflag) {
578 					rc += iter_map(look_map, &t);
579 				} else if (sflag) {
580 					if (Lflag) {
581 						(void) printf("%*s %*s %4s"
582 						    " %-6s %s %s\n",
583 						    addr_width, "Address",
584 						    size_width,
585 						    "Bytes", "Pgsz", "Mode ",
586 						    "Lgrp", "Mapped File");
587 						rc += iter_xmap(look_smap, &t);
588 					} else {
589 						(void) printf("%*s %*s %4s"
590 						    " %-6s %s\n",
591 						    addr_width, "Address",
592 						    size_width,
593 						    "Bytes", "Pgsz", "Mode ",
594 						    "Mapped File");
595 						rc += iter_xmap(look_smap, &t);
596 					}
597 				} else {
598 					rc += iter_map(look_map, &t);
599 				}
600 
601 				(void) printf(" %stotal  %*luK\n",
602 				    addr_width == 16 ?
603 				    "        " : "",
604 				    size_width, t.total_size);
605 			}
606 
607 			if (stacks != NULL) {
608 				free(stacks);
609 				stacks = NULL;
610 				nstacks = 0;
611 			}
612 
613 		}
614 
615 		Prelease(Pr, prr_flags);
616 		if (mapfd != -1)
617 			(void) close(mapfd);
618 	}
619 
620 	if (use_agent_lwp)
621 		(void) proc_finistdio();
622 
623 	return (rc);
624 }
625 
626 static int
rmapping_iter(struct ps_prochandle * Pr,proc_map_f * func,void * cd)627 rmapping_iter(struct ps_prochandle *Pr, proc_map_f *func, void *cd)
628 {
629 	char mapname[PATH_MAX];
630 	int mapfd, nmap, i, rc;
631 	struct stat st;
632 	prmap_t *prmapp, *pmp;
633 	ssize_t n;
634 
635 	(void) snprintf(mapname, sizeof (mapname),
636 	    "/proc/%d/rmap", (int)Pstatus(Pr)->pr_pid);
637 
638 	if ((mapfd = open(mapname, O_RDONLY)) < 0 || fstat(mapfd, &st) != 0) {
639 		if (mapfd >= 0)
640 			(void) close(mapfd);
641 		return (perr(mapname));
642 	}
643 
644 	nmap = st.st_size / sizeof (prmap_t);
645 	prmapp = malloc((nmap + 1) * sizeof (prmap_t));
646 
647 	if ((n = pread(mapfd, prmapp, (nmap + 1) * sizeof (prmap_t), 0L)) < 0) {
648 		(void) close(mapfd);
649 		free(prmapp);
650 		return (perr("read rmap"));
651 	}
652 
653 	(void) close(mapfd);
654 	nmap = n / sizeof (prmap_t);
655 
656 	for (i = 0, pmp = prmapp; i < nmap; i++, pmp++) {
657 		if ((rc = func(cd, pmp, NULL)) != 0) {
658 			free(prmapp);
659 			return (rc);
660 		}
661 	}
662 
663 	free(prmapp);
664 	return (0);
665 }
666 
667 static int
xmapping_iter(struct ps_prochandle * Pr,proc_xmap_f * func,void * cd,int doswap)668 xmapping_iter(struct ps_prochandle *Pr, proc_xmap_f *func, void *cd, int doswap)
669 {
670 	char mapname[PATH_MAX];
671 	int mapfd, nmap, i, rc;
672 	struct stat st;
673 	prxmap_t *prmapp, *pmp;
674 	ssize_t n;
675 
676 	(void) snprintf(mapname, sizeof (mapname),
677 	    "/proc/%d/xmap", (int)Pstatus(Pr)->pr_pid);
678 
679 	if ((mapfd = open(mapname, O_RDONLY)) < 0 || fstat(mapfd, &st) != 0) {
680 		if (mapfd >= 0)
681 			(void) close(mapfd);
682 		return (perr(mapname));
683 	}
684 
685 	nmap = st.st_size / sizeof (prxmap_t);
686 	nmap *= 2;
687 again:
688 	prmapp = malloc((nmap + 1) * sizeof (prxmap_t));
689 
690 	if ((n = pread(mapfd, prmapp, (nmap + 1) * sizeof (prxmap_t), 0)) < 0) {
691 		(void) close(mapfd);
692 		free(prmapp);
693 		return (perr("read xmap"));
694 	}
695 
696 	if (nmap < n / sizeof (prxmap_t)) {
697 		free(prmapp);
698 		nmap *= 2;
699 		goto again;
700 	}
701 
702 	(void) close(mapfd);
703 	nmap = n / sizeof (prxmap_t);
704 
705 	for (i = 0, pmp = prmapp; i < nmap; i++, pmp++) {
706 		if ((rc = func(cd, pmp, NULL, i == nmap - 1, doswap)) != 0) {
707 			free(prmapp);
708 			return (rc);
709 		}
710 	}
711 
712 	/*
713 	 * Mark the last element.
714 	 */
715 	if (map_count > 0)
716 		maps[map_count - 1].md_last = B_TRUE;
717 
718 	free(prmapp);
719 	return (0);
720 }
721 
722 static const char *
mapping_name(const prmap_t * pmp,boolean_t brief,char * buf,size_t bufsz)723 mapping_name(const prmap_t *pmp, boolean_t brief, char *buf, size_t bufsz)
724 {
725 	const pstatus_t *Psp = Pstatus(Pr);
726 	uintptr_t vaddr = pmp->pr_vaddr;
727 	size_t size = pmp->pr_size;
728 	uintptr_t segment_end = vaddr + size;
729 	const char *lname = NULL;
730 
731 	/*
732 	 * If the mapping is not anon or not part of the heap, make a name
733 	 * for it.  We don't want to report the heap as a.out's data.
734 	 */
735 	if (!(pmp->pr_mflags & MA_ANON) || segment_end <= Psp->pr_brkbase ||
736 	    vaddr >= Psp->pr_brkbase + Psp->pr_brksize) {
737 		lname = make_name(Pr, lflag, vaddr, pmp->pr_mapname,
738 		    buf, bufsz);
739 		if (lname != NULL) {
740 			if (brief) {
741 				char *ln;
742 
743 				if ((ln = strrchr(lname, '/')) != NULL)
744 					lname = ln + 1;
745 			}
746 			return (lname);
747 		}
748 	}
749 
750 	if ((pmp->pr_mflags & MA_ANON) || Pstate(Pr) == PS_DEAD) {
751 		lname = anon_name(buf, Psp, stacks, nstacks, vaddr, size,
752 		    pmp->pr_mflags, pmp->pr_shmid, NULL);
753 		if (lname != NULL)
754 			return (lname);
755 	}
756 
757 	if (comm_page != INVALID_ADDRESS && vaddr == comm_page) {
758 		(void) strlcpy(buf, "  [ comm ]", bufsz);
759 		return (buf);
760 	}
761 
762 	return (NULL);
763 }
764 
765 /*
766  * We simplify things by casting prxmap_t into prmap_t and re-using
767  * mapping_name(). Ensure that that the fields we need remain in the same place
768  * in both.
769  */
770 CTASSERT(offsetof(prmap_t, pr_vaddr) == offsetof(prxmap_t, pr_vaddr));
771 CTASSERT(offsetof(prmap_t, pr_size) == offsetof(prxmap_t, pr_size));
772 CTASSERT(offsetof(prmap_t, pr_mapname) == offsetof(prxmap_t, pr_mapname));
773 CTASSERT(offsetof(prmap_t, pr_mflags) == offsetof(prxmap_t, pr_mflags));
774 CTASSERT(offsetof(prmap_t, pr_shmid) == offsetof(prxmap_t, pr_shmid));
775 
776 static const char *
mapping_xname(const prxmap_t * pmp,boolean_t brief,char * buf,size_t bufsz)777 mapping_xname(const prxmap_t *pmp, boolean_t brief, char *buf, size_t bufsz)
778 {
779 	return (mapping_name((const prmap_t *)pmp, brief, buf, bufsz));
780 }
781 
782 static int
look_map(void * data,const prmap_t * pmp,const char * object_name)783 look_map(void *data, const prmap_t *pmp, const char *object_name)
784 {
785 	struct totals *t = data;
786 	size_t size;
787 	char mname[PATH_MAX];
788 	const char *lname;
789 	size_t	psz = pmp->pr_pagesize;
790 	uintptr_t vaddr = pmp->pr_vaddr;
791 	uintptr_t segment_end = vaddr + pmp->pr_size;
792 	lgrp_id_t lgrp;
793 	memory_chunk_t mchunk;
794 
795 	lname = mapping_name(pmp, B_FALSE, mname, sizeof (mname));
796 
797 	/*
798 	 * Adjust the address range if -A is specified.
799 	 */
800 	size = adjust_addr_range(pmp->pr_vaddr, segment_end, psz,
801 	    &vaddr, &segment_end);
802 
803 	if (size == 0)
804 		return (0);
805 
806 	if (!Lflag) {
807 		/*
808 		 * Display the whole mapping
809 		 */
810 		size = ROUNDUP_KB(size);
811 
812 		(void) printf(lname ?
813 		    "%.*lX %*luK %-6s %s\n" :
814 		    "%.*lX %*luK %s\n",
815 		    addr_width, vaddr,
816 		    size_width - 1, size, mflags(pmp->pr_mflags), lname);
817 
818 		t->total_size += size;
819 		return (0);
820 	}
821 
822 	/*
823 	 * We need to display lgroups backing physical memory, so we break the
824 	 * segment into individual pages and coalesce pages with the same lgroup
825 	 * into one "segment".
826 	 */
827 
828 	/*
829 	 * Initialize address descriptions for the mapping.
830 	 */
831 	mem_chunk_init(&mchunk, segment_end, psz);
832 	size = 0;
833 
834 	/*
835 	 * Walk mapping (page by page) and display contiguous ranges of memory
836 	 * allocated to same lgroup.
837 	 */
838 	do {
839 		size_t		size_contig;
840 
841 		/*
842 		 * Get contiguous region of memory starting from vaddr allocated
843 		 * from the same lgroup.
844 		 */
845 		size_contig = get_contiguous_region(&mchunk, vaddr,
846 		    segment_end, pmp->pr_pagesize, &lgrp);
847 
848 		(void) printf(lname ? "%.*lX %*luK %-6s%s %s\n" :
849 		    "%.*lX %*luK %-6s%s\n",
850 		    addr_width, vaddr,
851 		    size_width - 1, size_contig / KILOBYTE,
852 		    mflags(pmp->pr_mflags),
853 		    lgrp2str(lgrp), lname);
854 
855 		vaddr += size_contig;
856 		size += size_contig;
857 	} while (vaddr < segment_end && !interrupt);
858 
859 	/* Update the total size */
860 	t->total_size += ROUNDUP_KB(size);
861 	return (0);
862 }
863 
864 static void
printK(long value,int width)865 printK(long value, int width)
866 {
867 	if (value == 0)
868 		(void) printf(width == 8 ? "       -" : "          -");
869 	else
870 		(void) printf(" %*lu", width - 1, value);
871 }
872 
873 static const char *
pagesize(const prxmap_t * pmp)874 pagesize(const prxmap_t *pmp)
875 {
876 	int pagesize = pmp->pr_hatpagesize;
877 	static char buf[32];
878 
879 	if (pagesize == 0) {
880 		return ("-"); /* no underlying HAT mapping */
881 	}
882 
883 	if (pagesize >= KILOBYTE && (pagesize % KILOBYTE) == 0) {
884 		if ((pagesize % GIGABYTE) == 0)
885 			(void) snprintf(buf, sizeof (buf), "%dG",
886 			    pagesize / GIGABYTE);
887 		else if ((pagesize % MEGABYTE) == 0)
888 			(void) snprintf(buf, sizeof (buf), "%dM",
889 			    pagesize / MEGABYTE);
890 		else
891 			(void) snprintf(buf, sizeof (buf), "%dK",
892 			    pagesize / KILOBYTE);
893 	} else
894 		(void) snprintf(buf, sizeof (buf), "%db", pagesize);
895 
896 	return (buf);
897 }
898 
899 static int
look_smap(void * data,const prxmap_t * pmp,const char * object_name,int last,int doswap)900 look_smap(void *data, const prxmap_t *pmp, const char *object_name, int last,
901     int doswap)
902 {
903 	struct totals *t = data;
904 	size_t size;
905 	char mname[PATH_MAX];
906 	const char *lname;
907 	const char *format;
908 	size_t	psz = pmp->pr_pagesize;
909 	uintptr_t vaddr = pmp->pr_vaddr;
910 	uintptr_t segment_end = vaddr + pmp->pr_size;
911 	lgrp_id_t lgrp;
912 	memory_chunk_t mchunk;
913 
914 	lname = mapping_xname(pmp, B_FALSE, mname, sizeof (mname));
915 
916 	/*
917 	 * Adjust the address range if -A is specified.
918 	 */
919 	size = adjust_addr_range(pmp->pr_vaddr, segment_end, psz,
920 	    &vaddr, &segment_end);
921 
922 	if (size == 0)
923 		return (0);
924 
925 	if (!Lflag) {
926 		/*
927 		 * Display the whole mapping
928 		 */
929 		if (lname != NULL)
930 			format = "%.*lX %*luK %4s %-6s %s\n";
931 		else
932 			format = "%.*lX %*luK %4s %s\n";
933 
934 		size = ROUNDUP_KB(size);
935 
936 		(void) printf(format, addr_width, vaddr, size_width - 1, size,
937 		    pagesize(pmp), mflags(pmp->pr_mflags), lname);
938 
939 		t->total_size += size;
940 		return (0);
941 	}
942 
943 	if (lname != NULL)
944 		format = "%.*lX %*luK %4s %-6s%s %s\n";
945 	else
946 		format = "%.*lX %*luK %4s%s %s\n";
947 
948 	/*
949 	 * We need to display lgroups backing physical memory, so we break the
950 	 * segment into individual pages and coalesce pages with the same lgroup
951 	 * into one "segment".
952 	 */
953 
954 	/*
955 	 * Initialize address descriptions for the mapping.
956 	 */
957 	mem_chunk_init(&mchunk, segment_end, psz);
958 	size = 0;
959 
960 	/*
961 	 * Walk mapping (page by page) and display contiguous ranges of memory
962 	 * allocated to same lgroup.
963 	 */
964 	do {
965 		size_t		size_contig;
966 
967 		/*
968 		 * Get contiguous region of memory starting from vaddr allocated
969 		 * from the same lgroup.
970 		 */
971 		size_contig = get_contiguous_region(&mchunk, vaddr,
972 		    segment_end, pmp->pr_pagesize, &lgrp);
973 
974 		(void) printf(format, addr_width, vaddr,
975 		    size_width - 1, size_contig / KILOBYTE,
976 		    pagesize(pmp), mflags(pmp->pr_mflags),
977 		    lgrp2str(lgrp), lname);
978 
979 		vaddr += size_contig;
980 		size += size_contig;
981 	} while (vaddr < segment_end && !interrupt);
982 
983 	t->total_size += ROUNDUP_KB(size);
984 	return (0);
985 }
986 
987 #define	ANON(x)	((aflag || (((x)->pr_mflags & MA_SHARED) == 0)) ? \
988 	    ((x)->pr_anon) : 0)
989 
990 static int
look_xmap(void * data,const prxmap_t * pmp,const char * object_name,int last,int doswap)991 look_xmap(void *data, const prxmap_t *pmp, const char *object_name, int last,
992     int doswap)
993 {
994 	struct totals *t = data;
995 	char mname[PATH_MAX];
996 	const char *lname;
997 
998 	lname = mapping_xname(pmp, B_TRUE, mname, sizeof (mname));
999 
1000 	(void) printf("%.*lX", addr_width, (ulong_t)pmp->pr_vaddr);
1001 
1002 	printK(ROUNDUP_KB(pmp->pr_size), size_width);
1003 	printK(pmp->pr_rss * (pmp->pr_pagesize / KILOBYTE), size_width);
1004 	printK(ANON(pmp) * (pmp->pr_pagesize / KILOBYTE), size_width);
1005 	printK(pmp->pr_locked * (pmp->pr_pagesize / KILOBYTE), size_width);
1006 	(void) printf(lname ? " %4s %-6s %s\n" : " %4s %s\n",
1007 	    pagesize(pmp), mflags(pmp->pr_mflags), lname);
1008 
1009 	t->total_size += ROUNDUP_KB(pmp->pr_size);
1010 	t->total_rss += pmp->pr_rss * (pmp->pr_pagesize / KILOBYTE);
1011 	t->total_anon += ANON(pmp) * (pmp->pr_pagesize / KILOBYTE);
1012 	t->total_locked += (pmp->pr_locked * (pmp->pr_pagesize / KILOBYTE));
1013 
1014 	return (0);
1015 }
1016 
1017 static int
look_xmap_nopgsz(void * data,const prxmap_t * pmp,const char * object_name,int last,int doswap)1018 look_xmap_nopgsz(void *data, const prxmap_t *pmp, const char *object_name,
1019     int last, int doswap)
1020 {
1021 	struct totals *t = data;
1022 	char mname[PATH_MAX];
1023 	const char *lname;
1024 	static uintptr_t prev_vaddr;
1025 	static size_t prev_size;
1026 	static offset_t prev_offset;
1027 	static int prev_mflags;
1028 	static char *prev_lname;
1029 	static char prev_mname[PATH_MAX];
1030 	static ulong_t prev_rss;
1031 	static ulong_t prev_anon;
1032 	static ulong_t prev_locked;
1033 	static ulong_t prev_swap;
1034 	int merged = 0;
1035 	static int first = 1;
1036 	ulong_t swap = 0;
1037 	int kperpage;
1038 
1039 	/*
1040 	 * Calculate swap reservations
1041 	 */
1042 	if (pmp->pr_mflags & MA_SHARED) {
1043 		if (aflag && (pmp->pr_mflags & MA_NORESERVE) == 0) {
1044 			/* Swap reserved for entire non-ism SHM */
1045 			swap = pmp->pr_size / pmp->pr_pagesize;
1046 		}
1047 	} else if (pmp->pr_mflags & MA_NORESERVE) {
1048 		/* Swap reserved on fault for each anon page */
1049 		swap = pmp->pr_anon;
1050 	} else if (pmp->pr_mflags & MA_WRITE) {
1051 		/* Swap reserve for entire writable segment */
1052 		swap = pmp->pr_size / pmp->pr_pagesize;
1053 	}
1054 
1055 	lname = mapping_xname(pmp, B_TRUE, mname, sizeof (mname));
1056 
1057 	kperpage = pmp->pr_pagesize / KILOBYTE;
1058 
1059 	t->total_size += ROUNDUP_KB(pmp->pr_size);
1060 	t->total_rss += pmp->pr_rss * kperpage;
1061 	t->total_anon += ANON(pmp) * kperpage;
1062 	t->total_locked += pmp->pr_locked * kperpage;
1063 	t->total_swap += swap * kperpage;
1064 
1065 	if (first == 1) {
1066 		first = 0;
1067 		prev_vaddr = pmp->pr_vaddr;
1068 		prev_size = pmp->pr_size;
1069 		prev_offset = pmp->pr_offset;
1070 		prev_mflags = pmp->pr_mflags;
1071 		if (lname == NULL) {
1072 			prev_lname = NULL;
1073 		} else {
1074 			(void) strcpy(prev_mname, lname);
1075 			prev_lname = prev_mname;
1076 		}
1077 		prev_rss = pmp->pr_rss * kperpage;
1078 		prev_anon = ANON(pmp) * kperpage;
1079 		prev_locked = pmp->pr_locked * kperpage;
1080 		prev_swap = swap * kperpage;
1081 		if (last == 0) {
1082 			return (0);
1083 		}
1084 		merged = 1;
1085 	} else if (prev_vaddr + prev_size == pmp->pr_vaddr &&
1086 	    prev_mflags == pmp->pr_mflags &&
1087 	    ((prev_mflags & MA_ISM) ||
1088 	    prev_offset + prev_size == pmp->pr_offset) &&
1089 	    ((lname == NULL && prev_lname == NULL) ||
1090 	    (lname != NULL && prev_lname != NULL &&
1091 	    strcmp(lname, prev_lname) == 0))) {
1092 		prev_size += pmp->pr_size;
1093 		prev_rss += pmp->pr_rss * kperpage;
1094 		prev_anon += ANON(pmp) * kperpage;
1095 		prev_locked += pmp->pr_locked * kperpage;
1096 		prev_swap += swap * kperpage;
1097 		if (last == 0) {
1098 			return (0);
1099 		}
1100 		merged = 1;
1101 	}
1102 
1103 	(void) printf("%.*lX", addr_width, (ulong_t)prev_vaddr);
1104 	printK(ROUNDUP_KB(prev_size), size_width);
1105 
1106 	if (doswap)
1107 		printK(prev_swap, size_width);
1108 	else {
1109 		printK(prev_rss, size_width);
1110 		printK(prev_anon, size_width);
1111 		printK(prev_locked, size_width);
1112 	}
1113 	(void) printf(prev_lname ? " %-6s %s\n" : " %s\n",
1114 	    mflags(prev_mflags), prev_lname);
1115 
1116 	if (last == 0) {
1117 		prev_vaddr = pmp->pr_vaddr;
1118 		prev_size = pmp->pr_size;
1119 		prev_offset = pmp->pr_offset;
1120 		prev_mflags = pmp->pr_mflags;
1121 		if (lname == NULL) {
1122 			prev_lname = NULL;
1123 		} else {
1124 			(void) strcpy(prev_mname, lname);
1125 			prev_lname = prev_mname;
1126 		}
1127 		prev_rss = pmp->pr_rss * kperpage;
1128 		prev_anon = ANON(pmp) * kperpage;
1129 		prev_locked = pmp->pr_locked * kperpage;
1130 		prev_swap = swap * kperpage;
1131 	} else if (merged == 0) {
1132 		(void) printf("%.*lX", addr_width, (ulong_t)pmp->pr_vaddr);
1133 		printK(ROUNDUP_KB(pmp->pr_size), size_width);
1134 		if (doswap)
1135 			printK(swap * kperpage, size_width);
1136 		else {
1137 			printK(pmp->pr_rss * kperpage, size_width);
1138 			printK(ANON(pmp) * kperpage, size_width);
1139 			printK(pmp->pr_locked * kperpage, size_width);
1140 		}
1141 		(void) printf(lname ? " %-6s %s\n" : " %s\n",
1142 		    mflags(pmp->pr_mflags), lname);
1143 	}
1144 
1145 	if (last != 0)
1146 		first = 1;
1147 
1148 	return (0);
1149 }
1150 
1151 static int
perr(char * s)1152 perr(char *s)
1153 {
1154 	if (s)
1155 		(void) fprintf(stderr, "%s: ", procname);
1156 	else
1157 		s = procname;
1158 	perror(s);
1159 	return (1);
1160 }
1161 
1162 static char *
mflags(uint_t arg)1163 mflags(uint_t arg)
1164 {
1165 	static char code_buf[80];
1166 	char *str = code_buf;
1167 
1168 	/*
1169 	 * rwxsR
1170 	 *
1171 	 * r - segment is readable
1172 	 * w - segment is writable
1173 	 * x - segment is executable
1174 	 * s - segment is shared
1175 	 * R - segment is mapped MAP_NORESERVE
1176 	 *
1177 	 */
1178 	(void) sprintf(str, "%c%c%c%c%c%c",
1179 	    arg & MA_READ ? 'r' : '-',
1180 	    arg & MA_WRITE ? 'w' : '-',
1181 	    arg & MA_EXEC ? 'x' : '-',
1182 	    arg & MA_SHARED ? 's' : '-',
1183 	    arg & MA_NORESERVE ? 'R' : '-',
1184 	    arg & MA_RESERVED1 ? '*' : ' ');
1185 
1186 	return (str);
1187 }
1188 
1189 static mapdata_t *
nextmap(void)1190 nextmap(void)
1191 {
1192 	mapdata_t *newmaps;
1193 	int next;
1194 
1195 	if (map_count == map_alloc) {
1196 		if (map_alloc == 0)
1197 			next = 16;
1198 		else
1199 			next = map_alloc * 2;
1200 
1201 		newmaps = realloc(maps, next * sizeof (mapdata_t));
1202 		if (newmaps == NULL) {
1203 			(void) perr("failed to allocate maps");
1204 			exit(1);
1205 		}
1206 		(void) memset(newmaps + map_alloc, '\0',
1207 		    (next - map_alloc) * sizeof (mapdata_t));
1208 
1209 		map_alloc = next;
1210 		maps = newmaps;
1211 	}
1212 
1213 	return (&maps[map_count++]);
1214 }
1215 
1216 static int
gather_map(void * ignored,const prmap_t * map,const char * objname)1217 gather_map(void *ignored, const prmap_t *map, const char *objname)
1218 {
1219 	mapdata_t *data;
1220 
1221 	/* Skip mappings which are outside the range specified by -A */
1222 	if (!address_in_range(map->pr_vaddr,
1223 	    map->pr_vaddr + map->pr_size, map->pr_pagesize))
1224 		return (0);
1225 
1226 	data = nextmap();
1227 	data->md_map = *map;
1228 	if (data->md_objname != NULL)
1229 		free(data->md_objname);
1230 	data->md_objname = objname ? strdup(objname) : NULL;
1231 
1232 	return (0);
1233 }
1234 
1235 static int
gather_xmap(void * ignored,const prxmap_t * xmap,const char * objname,int last,int doswap)1236 gather_xmap(void *ignored, const prxmap_t *xmap, const char *objname,
1237     int last, int doswap)
1238 {
1239 	mapdata_t *data;
1240 
1241 	/* Skip mappings which are outside the range specified by -A */
1242 	if (!address_in_range(xmap->pr_vaddr,
1243 	    xmap->pr_vaddr + xmap->pr_size, xmap->pr_pagesize))
1244 		return (0);
1245 
1246 	data = nextmap();
1247 	data->md_xmap = *xmap;
1248 	if (data->md_objname != NULL)
1249 		free(data->md_objname);
1250 	data->md_objname = objname ? strdup(objname) : NULL;
1251 	data->md_last = last;
1252 	data->md_doswap = doswap;
1253 
1254 	return (0);
1255 }
1256 
1257 static int
iter_map(proc_map_f * func,void * data)1258 iter_map(proc_map_f *func, void *data)
1259 {
1260 	int i;
1261 	int ret;
1262 
1263 	for (i = 0; i < map_count; i++) {
1264 		if (interrupt)
1265 			break;
1266 		if ((ret = func(data, &maps[i].md_map,
1267 		    maps[i].md_objname)) != 0)
1268 			return (ret);
1269 	}
1270 
1271 	return (0);
1272 }
1273 
1274 static int
iter_xmap(proc_xmap_f * func,void * data)1275 iter_xmap(proc_xmap_f *func, void *data)
1276 {
1277 	int i;
1278 	int ret;
1279 
1280 	for (i = 0; i < map_count; i++) {
1281 		if (interrupt)
1282 			break;
1283 		if ((ret = func(data, &maps[i].md_xmap, maps[i].md_objname,
1284 		    maps[i].md_last, maps[i].md_doswap)) != 0)
1285 			return (ret);
1286 	}
1287 
1288 	return (0);
1289 }
1290 
1291 /*
1292  * Convert lgroup ID to string.
1293  * returns dash when lgroup ID is invalid.
1294  */
1295 static char *
lgrp2str(lgrp_id_t lgrp)1296 lgrp2str(lgrp_id_t lgrp)
1297 {
1298 	static char lgrp_buf[20];
1299 	char *str = lgrp_buf;
1300 
1301 	(void) sprintf(str, lgrp == LGRP_NONE ? "   -" : "%4d", lgrp);
1302 	return (str);
1303 }
1304 
1305 /*
1306  * Parse address range specification for -A option.
1307  * The address range may have the following forms:
1308  *
1309  * address
1310  *	start and end is set to address
1311  * address,
1312  *	start is set to address, end is set to INVALID_ADDRESS
1313  * ,address
1314  *	start is set to 0, end is set to address
1315  * address1,address2
1316  *	start is set to address1, end is set to address2
1317  *
1318  */
1319 static int
parse_addr_range(char * input_str,uintptr_t * start,uintptr_t * end)1320 parse_addr_range(char *input_str, uintptr_t *start, uintptr_t *end)
1321 {
1322 	char *startp = input_str;
1323 	char *endp = strchr(input_str, ',');
1324 	ulong_t	s = (ulong_t)INVALID_ADDRESS;
1325 	ulong_t e = (ulong_t)INVALID_ADDRESS;
1326 
1327 	if (endp != NULL) {
1328 		/*
1329 		 * Comma is present. If there is nothing after comma, the end
1330 		 * remains set at INVALID_ADDRESS. Otherwise it is set to the
1331 		 * value after comma.
1332 		 */
1333 		*endp = '\0';
1334 		endp++;
1335 
1336 		if ((*endp != '\0') && sscanf(endp, "%lx", &e) != 1)
1337 			return (1);
1338 	}
1339 
1340 	if (startp != NULL) {
1341 		/*
1342 		 * Read the start address, if it is specified. If the address is
1343 		 * missing, start will be set to INVALID_ADDRESS.
1344 		 */
1345 		if ((*startp != '\0') && sscanf(startp, "%lx", &s) != 1)
1346 			return (1);
1347 	}
1348 
1349 	/* If there is no comma, end becomes equal to start */
1350 	if (endp == NULL)
1351 		e = s;
1352 
1353 	/*
1354 	 * ,end implies 0..end range
1355 	 */
1356 	if (e != INVALID_ADDRESS && s == INVALID_ADDRESS)
1357 		s = 0;
1358 
1359 	*start = (uintptr_t)s;
1360 	*end = (uintptr_t)e;
1361 
1362 	/* Return error if neither start nor end address were specified */
1363 	return (! (s != INVALID_ADDRESS || e != INVALID_ADDRESS));
1364 }
1365 
1366 /*
1367  * Check whether any portion of [start, end] segment is within the
1368  * [start_addr, end_addr] range.
1369  *
1370  * Return values:
1371  *   0 - address is outside the range
1372  *   1 - address is within the range
1373  */
1374 static int
address_in_range(uintptr_t start,uintptr_t end,size_t psz)1375 address_in_range(uintptr_t start, uintptr_t end, size_t psz)
1376 {
1377 	int rc = 1;
1378 
1379 	/*
1380 	 *  Nothing to do if there is no address range specified with -A
1381 	 */
1382 	if (start_addr != INVALID_ADDRESS || end_addr != INVALID_ADDRESS) {
1383 		/* The segment end is below the range start */
1384 		if ((start_addr != INVALID_ADDRESS) &&
1385 		    (end < P2ALIGN(start_addr, psz)))
1386 			rc = 0;
1387 
1388 		/* The segment start is above the range end */
1389 		if ((end_addr != INVALID_ADDRESS) &&
1390 		    (start > P2ALIGN(end_addr + psz, psz)))
1391 			rc = 0;
1392 	}
1393 	return (rc);
1394 }
1395 
1396 /*
1397  * Returns an intersection of the [start, end] interval and the range specified
1398  * by -A flag [start_addr, end_addr]. Unspecified parts of the address range
1399  * have value INVALID_ADDRESS.
1400  *
1401  * The start_addr address is rounded down to the beginning of page and end_addr
1402  * is rounded up to the end of page.
1403  *
1404  * Returns the size of the resulting interval or zero if the interval is empty
1405  * or invalid.
1406  */
1407 static size_t
adjust_addr_range(uintptr_t start,uintptr_t end,size_t psz,uintptr_t * new_start,uintptr_t * new_end)1408 adjust_addr_range(uintptr_t start, uintptr_t end, size_t psz,
1409     uintptr_t *new_start, uintptr_t *new_end)
1410 {
1411 	uintptr_t from;		/* start_addr rounded down */
1412 	uintptr_t to;		/* end_addr rounded up */
1413 
1414 	/*
1415 	 * Round down the lower address of the range to the beginning of page.
1416 	 */
1417 	if (start_addr == INVALID_ADDRESS) {
1418 		/*
1419 		 * No start_addr specified by -A, the lower part of the interval
1420 		 * does not change.
1421 		 */
1422 		*new_start = start;
1423 	} else {
1424 		from = P2ALIGN(start_addr, psz);
1425 		/*
1426 		 * If end address is outside the range, return an empty
1427 		 * interval
1428 		 */
1429 		if (end <  from) {
1430 			*new_start = *new_end = 0;
1431 			return (0);
1432 		}
1433 		/*
1434 		 * The adjusted start address is the maximum of requested start
1435 		 * and the aligned start_addr of the -A range.
1436 		 */
1437 		*new_start = start < from ? from : start;
1438 	}
1439 
1440 	/*
1441 	 * Round up the higher address of the range to the end of page.
1442 	 */
1443 	if (end_addr == INVALID_ADDRESS) {
1444 		/*
1445 		 * No end_addr specified by -A, the upper part of the interval
1446 		 * does not change.
1447 		 */
1448 		*new_end = end;
1449 	} else {
1450 		/*
1451 		 * If only one address is specified and it is the beginning of a
1452 		 * segment, get information about the whole segment. This
1453 		 * function is called once per segment and the 'end' argument is
1454 		 * always the end of a segment, so just use the 'end' value.
1455 		 */
1456 		to = (end_addr == start_addr && start == start_addr) ?
1457 		    end :
1458 		    P2ALIGN(end_addr + psz, psz);
1459 		/*
1460 		 * If start address is outside the range, return an empty
1461 		 * interval
1462 		 */
1463 		if (start > to) {
1464 			*new_start = *new_end = 0;
1465 			return (0);
1466 		}
1467 		/*
1468 		 * The adjusted end address is the minimum of requested end
1469 		 * and the aligned end_addr of the -A range.
1470 		 */
1471 		*new_end = end > to ? to : end;
1472 	}
1473 
1474 	/*
1475 	 * Make sure that the resulting interval is legal.
1476 	 */
1477 	if (*new_end < *new_start)
1478 			*new_start = *new_end = 0;
1479 
1480 	/* Return the size of the interval */
1481 	return (*new_end - *new_start);
1482 }
1483 
1484 /*
1485  * Initialize memory_info data structure with information about a new segment.
1486  */
1487 static void
mem_chunk_init(memory_chunk_t * chunk,uintptr_t end,size_t psz)1488 mem_chunk_init(memory_chunk_t *chunk, uintptr_t end, size_t psz)
1489 {
1490 	chunk->end_addr = end;
1491 	chunk->page_size = psz;
1492 	chunk->page_index = 0;
1493 	chunk->chunk_start = chunk->chunk_end = 0;
1494 }
1495 
1496 /*
1497  * Create a new chunk of addresses starting from vaddr.
1498  * Pass the whole chunk to pr_meminfo to collect lgroup and page size
1499  * information for each page in the chunk.
1500  */
1501 static void
mem_chunk_get(memory_chunk_t * chunk,uintptr_t vaddr)1502 mem_chunk_get(memory_chunk_t *chunk, uintptr_t vaddr)
1503 {
1504 	page_descr_t	*pdp = chunk->page_info;
1505 	size_t		psz = chunk->page_size;
1506 	uintptr_t	addr = vaddr;
1507 	uint64_t	inaddr[MAX_MEMINFO_CNT];
1508 	uint64_t	outdata[2 * MAX_MEMINFO_CNT];
1509 	uint_t		info[2] = { MEMINFO_VLGRP, MEMINFO_VPAGESIZE };
1510 	uint_t		validity[MAX_MEMINFO_CNT];
1511 	uint64_t	*dataptr = inaddr;
1512 	uint64_t	*outptr = outdata;
1513 	uint_t		*valptr = validity;
1514 	int		i, j, rc;
1515 
1516 	chunk->chunk_start = vaddr;
1517 	chunk->page_index = 0;	/* reset index for the new chunk */
1518 
1519 	/*
1520 	 * Fill in MAX_MEMINFO_CNT wotrh of pages starting from vaddr. Also,
1521 	 * copy starting address of each page to inaddr array for pr_meminfo.
1522 	 */
1523 	for (i = 0, pdp = chunk->page_info;
1524 	    (i < MAX_MEMINFO_CNT) && (addr <= chunk->end_addr);
1525 	    i++, pdp++, dataptr++, addr += psz) {
1526 		*dataptr = (uint64_t)addr;
1527 		pdp->pd_start = addr;
1528 		pdp->pd_lgrp = LGRP_NONE;
1529 		pdp->pd_valid = 0;
1530 		pdp->pd_pagesize = 0;
1531 	}
1532 
1533 	/* Mark the number of entries in the chunk and the last address */
1534 	chunk->page_count = i;
1535 	chunk->chunk_end = addr - psz;
1536 
1537 	if (interrupt)
1538 		return;
1539 
1540 	/* Call meminfo for all collected addresses */
1541 	rc = pr_meminfo(Pr, inaddr, i, info, 2, outdata, validity);
1542 	if (rc < 0) {
1543 		(void) perr("can not get memory information");
1544 		return;
1545 	}
1546 
1547 	/* Verify validity of each result and fill in the addrs array */
1548 	pdp = chunk->page_info;
1549 	for (j = 0; j < i; j++, pdp++, valptr++, outptr += 2) {
1550 		/* Skip invalid address pointers */
1551 		if ((*valptr & 1) == 0) {
1552 			continue;
1553 		}
1554 
1555 		/* Is lgroup information available? */
1556 		if ((*valptr & 2) != 0) {
1557 			pdp->pd_lgrp = (lgrp_id_t)*outptr;
1558 			pdp->pd_valid = 1;
1559 		}
1560 
1561 		/* Is page size informaion available? */
1562 		if ((*valptr & 4) != 0) {
1563 			pdp->pd_pagesize = *(outptr + 1);
1564 		}
1565 	}
1566 }
1567 
1568 /*
1569  * Starting from address 'vaddr' find the region with pages allocated from the
1570  * same lgroup.
1571  *
1572  * Arguments:
1573  *	mchunk		Initialized memory chunk structure
1574  *	vaddr		Starting address of the region
1575  *	maxaddr		Upper bound of the region
1576  *	pagesize	Default page size to use
1577  *	ret_lgrp	On exit contains the lgroup ID of all pages in the
1578  *			region.
1579  *
1580  * Returns:
1581  *	Size of the contiguous region in bytes
1582  *	The lgroup ID of all pages in the region in ret_lgrp argument.
1583  */
1584 static size_t
get_contiguous_region(memory_chunk_t * mchunk,uintptr_t vaddr,uintptr_t maxaddr,size_t pagesize,lgrp_id_t * ret_lgrp)1585 get_contiguous_region(memory_chunk_t *mchunk, uintptr_t vaddr,
1586     uintptr_t maxaddr, size_t pagesize, lgrp_id_t *ret_lgrp)
1587 {
1588 	size_t		size_contig = 0;
1589 	lgrp_id_t	lgrp;		/* Lgroup of the region start */
1590 	lgrp_id_t	curr_lgrp;	/* Lgroup of the current page */
1591 	size_t		psz = pagesize;	/* Pagesize to use */
1592 
1593 	/* Set both lgroup IDs to the lgroup of the first page */
1594 	curr_lgrp = lgrp = addr_to_lgrp(mchunk, vaddr, &psz);
1595 
1596 	/*
1597 	 * Starting from vaddr, walk page by page until either the end
1598 	 * of the segment is reached or a page is allocated from a different
1599 	 * lgroup. Also stop if interrupted from keyboard.
1600 	 */
1601 	while ((vaddr < maxaddr) && (curr_lgrp == lgrp) && !interrupt) {
1602 		/*
1603 		 * Get lgroup ID and the page size of the current page.
1604 		 */
1605 		curr_lgrp = addr_to_lgrp(mchunk, vaddr, &psz);
1606 		/* If there is no page size information, use the default */
1607 		if (psz == 0)
1608 			psz = pagesize;
1609 
1610 		if (curr_lgrp == lgrp) {
1611 			/*
1612 			 * This page belongs to the contiguous region.
1613 			 * Increase the region size and advance to the new page.
1614 			 */
1615 			size_contig += psz;
1616 			vaddr += psz;
1617 		}
1618 	}
1619 
1620 	/* Return the region lgroup ID and the size */
1621 	*ret_lgrp = lgrp;
1622 	return (size_contig);
1623 }
1624 
1625 /*
1626  * Given a virtual address, return its lgroup and page size. If there is meminfo
1627  * information for an address, use it, otherwise shift the chunk window to the
1628  * vaddr and create a new chunk with known meminfo information.
1629  */
1630 static lgrp_id_t
addr_to_lgrp(memory_chunk_t * chunk,uintptr_t vaddr,size_t * psz)1631 addr_to_lgrp(memory_chunk_t *chunk, uintptr_t vaddr, size_t *psz)
1632 {
1633 	page_descr_t *pdp;
1634 	lgrp_id_t lgrp = LGRP_NONE;
1635 	int i;
1636 
1637 	*psz = chunk->page_size;
1638 
1639 	if (interrupt)
1640 		return (0);
1641 
1642 	/*
1643 	 * Is there information about this address? If not, create a new chunk
1644 	 * starting from vaddr and apply pr_meminfo() to the whole chunk.
1645 	 */
1646 	if (vaddr < chunk->chunk_start || vaddr > chunk->chunk_end) {
1647 		/*
1648 		 * This address is outside the chunk, get the new chunk and
1649 		 * collect meminfo information for it.
1650 		 */
1651 		mem_chunk_get(chunk, vaddr);
1652 	}
1653 
1654 	/*
1655 	 * Find information about the address.
1656 	 */
1657 	pdp = &chunk->page_info[chunk->page_index];
1658 	for (i = chunk->page_index; i < chunk->page_count; i++, pdp++) {
1659 		if (pdp->pd_start == vaddr) {
1660 			if (pdp->pd_valid) {
1661 				lgrp = pdp->pd_lgrp;
1662 				/*
1663 				 * Override page size information if it is
1664 				 * present.
1665 				 */
1666 				if (pdp->pd_pagesize > 0)
1667 					*psz = pdp->pd_pagesize;
1668 			}
1669 			break;
1670 		}
1671 	}
1672 	/*
1673 	 * Remember where we ended - the next search will start here.
1674 	 * We can query for the lgrp for the same address again, so do not
1675 	 * advance index past the current value.
1676 	 */
1677 	chunk->page_index = i;
1678 
1679 	return (lgrp);
1680 }
1681 
1682 static void
intr(int sig)1683 intr(int sig)
1684 {
1685 	interrupt = 1;
1686 }
1687